2 a596b957 2022-07-14 tracey * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 a596b957 2022-07-14 tracey * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 58381f70 2022-09-03 op * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 a596b957 2022-07-14 tracey * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 a596b957 2022-07-14 tracey * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
9 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
10 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
12 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 a596b957 2022-07-14 tracey #include <net/if.h>
22 a596b957 2022-07-14 tracey #include <netinet/in.h>
23 b4c20a19 2022-07-15 naddy #include <sys/queue.h>
24 a596b957 2022-07-14 tracey #include <sys/stat.h>
25 a596b957 2022-07-14 tracey #include <sys/types.h>
27 58381f70 2022-09-03 op #include <ctype.h>
28 a596b957 2022-07-14 tracey #include <dirent.h>
29 a596b957 2022-07-14 tracey #include <errno.h>
30 a596b957 2022-07-14 tracey #include <event.h>
31 3b81530f 2022-11-22 op #include <fcntl.h>
32 a596b957 2022-07-14 tracey #include <imsg.h>
33 a596b957 2022-07-14 tracey #include <sha1.h>
34 5822e79e 2023-02-23 op #include <sha2.h>
35 a596b957 2022-07-14 tracey #include <stdio.h>
36 a596b957 2022-07-14 tracey #include <stdlib.h>
37 a596b957 2022-07-14 tracey #include <string.h>
38 a596b957 2022-07-14 tracey #include <unistd.h>
40 a596b957 2022-07-14 tracey #include "got_error.h"
41 a596b957 2022-07-14 tracey #include "got_object.h"
42 a596b957 2022-07-14 tracey #include "got_reference.h"
43 a596b957 2022-07-14 tracey #include "got_repository.h"
44 a596b957 2022-07-14 tracey #include "got_path.h"
45 a596b957 2022-07-14 tracey #include "got_cancel.h"
46 a596b957 2022-07-14 tracey #include "got_worktree.h"
47 a596b957 2022-07-14 tracey #include "got_diff.h"
48 a596b957 2022-07-14 tracey #include "got_commit_graph.h"
49 a596b957 2022-07-14 tracey #include "got_blame.h"
50 a596b957 2022-07-14 tracey #include "got_privsep.h"
52 a596b957 2022-07-14 tracey #include "gotwebd.h"
53 1220d7ea 2024-05-21 op #include "log.h"
54 1abb18e1 2022-12-20 op #include "tmpl.h"
56 a596b957 2022-07-14 tracey static const struct querystring_keys querystring_keys[] = {
57 a596b957 2022-07-14 tracey { "action", ACTION },
58 a596b957 2022-07-14 tracey { "commit", COMMIT },
59 a596b957 2022-07-14 tracey { "file", RFILE },
60 a596b957 2022-07-14 tracey { "folder", FOLDER },
61 a596b957 2022-07-14 tracey { "headref", HEADREF },
62 a596b957 2022-07-14 tracey { "index_page", INDEX_PAGE },
63 a596b957 2022-07-14 tracey { "path", PATH },
66 a596b957 2022-07-14 tracey static const struct action_keys action_keys[] = {
67 a596b957 2022-07-14 tracey { "blame", BLAME },
68 a596b957 2022-07-14 tracey { "blob", BLOB },
69 298f95fb 2023-01-05 op { "blobraw", BLOBRAW },
70 a596b957 2022-07-14 tracey { "briefs", BRIEFS },
71 a596b957 2022-07-14 tracey { "commits", COMMITS },
72 a596b957 2022-07-14 tracey { "diff", DIFF },
73 a596b957 2022-07-14 tracey { "error", ERR },
74 a596b957 2022-07-14 tracey { "index", INDEX },
75 7f65bb55 2023-12-01 op { "patch", PATCH },
76 a596b957 2022-07-14 tracey { "summary", SUMMARY },
77 a596b957 2022-07-14 tracey { "tag", TAG },
78 a596b957 2022-07-14 tracey { "tags", TAGS },
79 a596b957 2022-07-14 tracey { "tree", TREE },
80 1abb18e1 2022-12-20 op { "rss", RSS },
83 a596b957 2022-07-14 tracey static const struct got_error *gotweb_init_querystring(struct querystring **);
84 1552435e 2024-05-28 op static const struct got_error *gotweb_parse_querystring(struct querystring *,
86 1552435e 2024-05-28 op static const struct got_error *gotweb_assign_querystring(struct querystring *,
87 a596b957 2022-07-14 tracey char *, char *);
88 df2d3cd2 2023-03-11 op static int gotweb_render_index(struct template *);
89 2959f418 2024-04-28 op static const struct got_error *gotweb_load_got_path(struct repo_dir **,
90 2959f418 2024-04-28 op const char *, struct request *);
91 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_repo_description(char **,
92 3b81530f 2022-11-22 op struct server *, const char *, int);
93 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_clone_url(char **, struct server *,
94 3b81530f 2022-11-22 op const char *, int);
96 a596b957 2022-07-14 tracey static void gotweb_free_querystring(struct querystring *);
97 a596b957 2022-07-14 tracey static void gotweb_free_repo_dir(struct repo_dir *);
99 c8af7691 2023-06-22 op struct server *gotweb_get_server(const char *);
102 6cdf29f9 2023-01-21 op gotweb_reply(struct request *c, int status, const char *ctype,
103 6cdf29f9 2023-01-21 op struct gotweb_url *location)
105 6cdf29f9 2023-01-21 op const char *csp;
107 62eab86e 2023-09-13 op if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
110 6cdf29f9 2023-01-21 op if (location) {
111 62eab86e 2023-09-13 op if (tp_writes(c->tp, "Location: ") == -1 ||
112 6cdf29f9 2023-01-21 op gotweb_render_url(c, location) == -1 ||
113 62eab86e 2023-09-13 op tp_writes(c->tp, "\r\n") == -1)
117 6cdf29f9 2023-01-21 op csp = "Content-Security-Policy: default-src 'self'; "
118 6cdf29f9 2023-01-21 op "script-src 'none'; object-src 'none';\r\n";
119 62eab86e 2023-09-13 op if (tp_writes(c->tp, csp) == -1)
122 62eab86e 2023-09-13 op if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
125 62eab86e 2023-09-13 op return tp_writes(c->tp, "\r\n");
129 6cdf29f9 2023-01-21 op gotweb_reply_file(struct request *c, const char *ctype, const char *file,
130 6cdf29f9 2023-01-21 op const char *suffix)
134 62eab86e 2023-09-13 op r = tp_writef(c->tp, "Content-Disposition: attachment; "
135 6cdf29f9 2023-01-21 op "filename=%s%s\r\n", file, suffix ? suffix : "");
138 6cdf29f9 2023-01-21 op return gotweb_reply(c, 200, ctype, NULL);
142 a596b957 2022-07-14 tracey gotweb_process_request(struct request *c)
144 8f37175d 2023-03-11 op const struct got_error *error = NULL;
145 a596b957 2022-07-14 tracey struct server *srv = NULL;
146 a596b957 2022-07-14 tracey struct querystring *qs = NULL;
147 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
148 f9b5f5fb 2023-04-01 op const char *rss_ctype = "application/rss+xml;charset=utf-8";
149 f9b5f5fb 2023-04-01 op const uint8_t *buf;
151 f9b5f5fb 2023-04-01 op int r, binary = 0;
153 a596b957 2022-07-14 tracey /* init the transport */
154 a596b957 2022-07-14 tracey error = gotweb_init_transport(&c->t);
155 a596b957 2022-07-14 tracey if (error) {
156 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
159 a596b957 2022-07-14 tracey /* get the gotwebd server */
160 c8af7691 2023-06-22 op srv = gotweb_get_server(c->server_name);
161 a596b957 2022-07-14 tracey if (srv == NULL) {
162 a596b957 2022-07-14 tracey log_warnx("%s: error server is NULL", __func__);
163 a596b957 2022-07-14 tracey goto err;
165 a596b957 2022-07-14 tracey c->srv = srv;
166 a596b957 2022-07-14 tracey /* parse our querystring */
167 a596b957 2022-07-14 tracey error = gotweb_init_querystring(&qs);
168 a596b957 2022-07-14 tracey if (error) {
169 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
170 a596b957 2022-07-14 tracey goto err;
172 a596b957 2022-07-14 tracey c->t->qs = qs;
173 1552435e 2024-05-28 op error = gotweb_parse_querystring(qs, c->querystring);
174 a596b957 2022-07-14 tracey if (error) {
175 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
176 a596b957 2022-07-14 tracey goto err;
180 a596b957 2022-07-14 tracey * certain actions require a commit id in the querystring. this stops
181 a596b957 2022-07-14 tracey * bad actors from exploiting this by manually manipulating the
182 a596b957 2022-07-14 tracey * querystring.
185 298f95fb 2023-01-05 op if (qs->action == BLAME || qs->action == BLOB ||
186 7f65bb55 2023-12-01 op qs->action == BLOBRAW || qs->action == DIFF ||
187 7f65bb55 2023-12-01 op qs->action == PATCH) {
188 298f95fb 2023-01-05 op if (qs->commit == NULL) {
189 de2b82f3 2023-06-18 op error = got_error(GOT_ERR_BAD_QUERYSTRING);
194 a596b957 2022-07-14 tracey if (qs->action != INDEX) {
195 2959f418 2024-04-28 op error = gotweb_load_got_path(&repo_dir, qs->path, c);
196 a596b957 2022-07-14 tracey c->t->repo_dir = repo_dir;
197 a596b957 2022-07-14 tracey if (error && error->code != GOT_ERR_LONELY_PACKIDX)
198 a596b957 2022-07-14 tracey goto err;
201 f9b5f5fb 2023-04-01 op if (qs->action == BLOBRAW || qs->action == BLOB) {
202 26b163a0 2024-01-30 op if (qs->folder == NULL || qs->file == NULL) {
203 26b163a0 2024-01-30 op error = got_error(GOT_ERR_BAD_QUERYSTRING);
207 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
208 a596b957 2022-07-14 tracey if (error)
211 8f37175d 2023-03-11 op error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
212 ac15152e 2023-12-08 op &binary, c, qs->folder, qs->file, qs->commit);
217 7607b8e0 2023-11-17 stsp switch (qs->action) {
219 26b163a0 2024-01-30 op if (qs->folder == NULL || qs->file == NULL) {
220 26b163a0 2024-01-30 op error = got_error(GOT_ERR_BAD_QUERYSTRING);
223 f9b5f5fb 2023-04-01 op error = got_get_repo_commits(c, 1);
225 f9b5f5fb 2023-04-01 op log_warnx("%s: %s", __func__, error->msg);
228 f9b5f5fb 2023-04-01 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
230 f9b5f5fb 2023-04-01 op gotweb_render_page(c->tp, gotweb_render_blame);
233 f9b5f5fb 2023-04-01 op if (binary) {
234 f9b5f5fb 2023-04-01 op struct gotweb_url url = {
235 f9b5f5fb 2023-04-01 op .index_page = -1,
236 f9b5f5fb 2023-04-01 op .action = BLOBRAW,
237 f9b5f5fb 2023-04-01 op .path = qs->path,
238 f9b5f5fb 2023-04-01 op .commit = qs->commit,
239 f9b5f5fb 2023-04-01 op .folder = qs->folder,
240 f9b5f5fb 2023-04-01 op .file = qs->file,
243 f9b5f5fb 2023-04-01 op gotweb_reply(c, 302, NULL, &url);
247 f9b5f5fb 2023-04-01 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
249 f9b5f5fb 2023-04-01 op gotweb_render_page(c->tp, gotweb_render_blob);
251 f9b5f5fb 2023-04-01 op case BLOBRAW:
253 6cdf29f9 2023-01-21 op r = gotweb_reply_file(c, "application/octet-stream",
254 6cdf29f9 2023-01-21 op qs->file, NULL);
256 6cdf29f9 2023-01-21 op r = gotweb_reply(c, 200, "text/plain", NULL);
259 62eab86e 2023-09-13 op if (template_flush(c->tp) == -1)
263 df2d3cd2 2023-03-11 op error = got_object_blob_read_block(&len, c->t->blob);
266 76007998 2023-01-15 op if (len == 0)
268 df2d3cd2 2023-03-11 op buf = got_object_blob_get_read_buf(c->t->blob);
269 62eab86e 2023-09-13 op if (fcgi_write(c, buf, len) == -1)
273 a596b957 2022-07-14 tracey case BRIEFS:
274 13d9dc7e 2023-06-26 op error = got_get_repo_commits(c, srv->max_commits_display);
277 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
279 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_briefs);
281 a596b957 2022-07-14 tracey case COMMITS:
282 156a1144 2022-12-17 op error = got_get_repo_commits(c, srv->max_commits_display);
283 a596b957 2022-07-14 tracey if (error) {
284 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
285 a596b957 2022-07-14 tracey goto err;
287 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
289 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_commits);
291 a596b957 2022-07-14 tracey case DIFF:
292 587550a5 2023-01-10 op error = got_get_repo_commits(c, 1);
294 169b1631 2023-01-06 op log_warnx("%s: %s", __func__, error->msg);
297 18069c98 2023-05-16 op error = got_open_diff_for_output(&c->t->fp, c);
299 587550a5 2023-01-10 op log_warnx("%s: %s", __func__, error->msg);
302 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
304 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_diff);
306 a596b957 2022-07-14 tracey case INDEX:
307 df2d3cd2 2023-03-11 op c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
309 df2d3cd2 2023-03-11 op if (c->t->nrepos == -1) {
310 df2d3cd2 2023-03-11 op c->t->repos = NULL;
311 df2d3cd2 2023-03-11 op error = got_error_from_errno2("scandir",
312 df2d3cd2 2023-03-11 op srv->repos_path);
313 a596b957 2022-07-14 tracey goto err;
315 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
317 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_index);
320 7f65bb55 2023-12-01 op error = got_get_repo_commits(c, 1);
322 7f65bb55 2023-12-01 op log_warnx("%s: %s", __func__, error->msg);
325 7f65bb55 2023-12-01 op error = got_open_diff_for_output(&c->t->fp, c);
327 7f65bb55 2023-12-01 op log_warnx("%s: %s", __func__, error->msg);
330 7f65bb55 2023-12-01 op if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
332 7f65bb55 2023-12-01 op gotweb_render_patch(c->tp);
335 f9b5f5fb 2023-04-01 op error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
338 f9b5f5fb 2023-04-01 op if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
341 f9b5f5fb 2023-04-01 op gotweb_render_rss(c->tp);
343 a596b957 2022-07-14 tracey case SUMMARY:
344 df2d3cd2 2023-03-11 op error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
345 69525b4e 2023-01-13 op got_ref_cmp_by_name, NULL);
346 a596b957 2022-07-14 tracey if (error) {
347 69525b4e 2023-01-13 op log_warnx("%s: got_ref_list: %s", __func__,
349 a596b957 2022-07-14 tracey goto err;
351 8762929a 2023-12-29 op error = got_get_repo_commits(c, srv->summary_commits_display);
354 69525b4e 2023-01-13 op qs->action = TAGS;
355 8762929a 2023-12-29 op error = got_get_repo_tags(c, srv->summary_tags_display);
357 69525b4e 2023-01-13 op log_warnx("%s: got_get_repo_tags: %s", __func__,
361 69525b4e 2023-01-13 op qs->action = SUMMARY;
362 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
364 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_summary);
366 a596b957 2022-07-14 tracey case TAG:
367 dc07f76c 2023-01-09 op error = got_get_repo_tags(c, 1);
368 a596b957 2022-07-14 tracey if (error) {
369 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
372 dc07f76c 2023-01-09 op if (c->t->tag_count == 0) {
373 dc07f76c 2023-01-09 op error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
374 dc07f76c 2023-01-09 op "bad commit id");
375 a596b957 2022-07-14 tracey goto err;
377 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
379 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_tag);
381 a596b957 2022-07-14 tracey case TAGS:
382 d60961d2 2023-01-13 op error = got_get_repo_tags(c, srv->max_commits_display);
383 a596b957 2022-07-14 tracey if (error) {
384 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
385 a596b957 2022-07-14 tracey goto err;
387 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
389 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_tags);
391 a596b957 2022-07-14 tracey case TREE:
392 43d421de 2023-01-05 op error = got_get_repo_commits(c, 1);
393 a596b957 2022-07-14 tracey if (error) {
394 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
395 a596b957 2022-07-14 tracey goto err;
397 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
399 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_tree);
401 a596b957 2022-07-14 tracey case ERR:
403 8f37175d 2023-03-11 op error = got_error(GOT_ERR_BAD_QUERYSTRING);
407 8f37175d 2023-03-11 op c->t->error = error;
408 8f37175d 2023-03-11 op if (gotweb_reply(c, 400, "text/html", NULL) == -1)
410 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_error);
413 a596b957 2022-07-14 tracey struct server *
414 c8af7691 2023-06-22 op gotweb_get_server(const char *server_name)
416 c8af7691 2023-06-22 op struct server *srv;
418 95a4a5a1 2022-08-30 op /* check against the server name first */
419 4448825a 2023-06-16 op if (*server_name != '\0')
420 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
421 a596b957 2022-07-14 tracey if (strcmp(srv->name, server_name) == 0)
424 c8af7691 2023-06-22 op /* otherwise, use the first server */
425 c8af7691 2023-06-22 op return TAILQ_FIRST(&gotwebd_env->servers);
428 a596b957 2022-07-14 tracey const struct got_error *
429 a596b957 2022-07-14 tracey gotweb_init_transport(struct transport **t)
431 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
433 a596b957 2022-07-14 tracey *t = calloc(1, sizeof(**t));
434 a596b957 2022-07-14 tracey if (*t == NULL)
435 50f6148a 2023-05-29 op return got_error_from_errno2(__func__, "calloc");
437 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_commits);
438 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_tags);
439 df2d3cd2 2023-03-11 op TAILQ_INIT(&(*t)->refs);
441 df2d3cd2 2023-03-11 op (*t)->fd = -1;
443 a596b957 2022-07-14 tracey return error;
446 a596b957 2022-07-14 tracey static const struct got_error *
447 a596b957 2022-07-14 tracey gotweb_init_querystring(struct querystring **qs)
449 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
451 a596b957 2022-07-14 tracey *qs = calloc(1, sizeof(**qs));
452 a596b957 2022-07-14 tracey if (*qs == NULL)
453 50f6148a 2023-05-29 op return got_error_from_errno2(__func__, "calloc");
455 a596b957 2022-07-14 tracey (*qs)->headref = strdup("HEAD");
456 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
459 50f6148a 2023-05-29 op return got_error_from_errno2(__func__, "strdup");
462 6c37ad7b 2022-09-01 op (*qs)->action = INDEX;
464 a596b957 2022-07-14 tracey return error;
467 a596b957 2022-07-14 tracey static const struct got_error *
468 1552435e 2024-05-28 op gotweb_parse_querystring(struct querystring *qs, char *qst)
470 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
471 a596b957 2022-07-14 tracey char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
472 a596b957 2022-07-14 tracey char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
474 a596b957 2022-07-14 tracey if (qst == NULL)
475 a596b957 2022-07-14 tracey return error;
477 a596b957 2022-07-14 tracey tok1 = strdup(qst);
478 a596b957 2022-07-14 tracey if (tok1 == NULL)
479 50f6148a 2023-05-29 op return got_error_from_errno2(__func__, "strdup");
481 a596b957 2022-07-14 tracey tok1_pair = tok1;
482 a596b957 2022-07-14 tracey tok1_end = tok1;
484 a596b957 2022-07-14 tracey while (tok1_pair != NULL) {
485 a596b957 2022-07-14 tracey strsep(&tok1_end, "&");
487 a596b957 2022-07-14 tracey tok2 = strdup(tok1_pair);
488 a596b957 2022-07-14 tracey if (tok2 == NULL) {
489 a596b957 2022-07-14 tracey free(tok1);
490 50f6148a 2023-05-29 op return got_error_from_errno2(__func__, "strdup");
493 a596b957 2022-07-14 tracey tok2_pair = tok2;
494 a596b957 2022-07-14 tracey tok2_end = tok2;
496 a596b957 2022-07-14 tracey while (tok2_pair != NULL) {
497 a596b957 2022-07-14 tracey strsep(&tok2_end, "=");
498 a596b957 2022-07-14 tracey if (tok2_end) {
499 a596b957 2022-07-14 tracey error = gotweb_assign_querystring(qs, tok2_pair,
500 a596b957 2022-07-14 tracey tok2_end);
501 a596b957 2022-07-14 tracey if (error)
502 a596b957 2022-07-14 tracey goto err;
504 a596b957 2022-07-14 tracey tok2_pair = tok2_end;
506 a596b957 2022-07-14 tracey free(tok2);
507 a596b957 2022-07-14 tracey tok1_pair = tok1_end;
509 a596b957 2022-07-14 tracey free(tok1);
510 a596b957 2022-07-14 tracey return error;
512 a596b957 2022-07-14 tracey free(tok2);
513 a596b957 2022-07-14 tracey free(tok1);
514 a596b957 2022-07-14 tracey return error;
518 58381f70 2022-09-03 op * Adapted from usr.sbin/httpd/httpd.c url_decode.
520 a596b957 2022-07-14 tracey static const struct got_error *
521 58381f70 2022-09-03 op gotweb_urldecode(char *url)
525 58381f70 2022-09-03 op unsigned long x;
527 58381f70 2022-09-03 op hex[2] = '\0';
530 58381f70 2022-09-03 op while (*p != '\0') {
531 58381f70 2022-09-03 op switch (*p) {
533 58381f70 2022-09-03 op /* Encoding character is followed by two hex chars */
534 58381f70 2022-09-03 op if (!isxdigit((unsigned char)p[1]) ||
535 58381f70 2022-09-03 op !isxdigit((unsigned char)p[2]) ||
536 58381f70 2022-09-03 op (p[1] == '0' && p[2] == '0'))
537 58381f70 2022-09-03 op return got_error(GOT_ERR_BAD_QUERYSTRING);
539 58381f70 2022-09-03 op hex[0] = p[1];
540 58381f70 2022-09-03 op hex[1] = p[2];
543 58381f70 2022-09-03 op * We don't have to validate "hex" because it is
544 58381f70 2022-09-03 op * guaranteed to include two hex chars followed by nul.
546 58381f70 2022-09-03 op x = strtoul(hex, NULL, 16);
547 58381f70 2022-09-03 op *q = (char)x;
562 58381f70 2022-09-03 op static const struct got_error *
563 1552435e 2024-05-28 op gotweb_assign_querystring(struct querystring *qs, char *key, char *value)
565 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
566 a596b957 2022-07-14 tracey const char *errstr;
567 a596b957 2022-07-14 tracey int a_cnt, el_cnt;
569 58381f70 2022-09-03 op error = gotweb_urldecode(value);
571 58381f70 2022-09-03 op return error;
573 03e70dd4 2023-11-16 op for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
574 a596b957 2022-07-14 tracey if (strcmp(key, querystring_keys[el_cnt].name) != 0)
575 a596b957 2022-07-14 tracey continue;
577 a596b957 2022-07-14 tracey switch (querystring_keys[el_cnt].element) {
578 a596b957 2022-07-14 tracey case ACTION:
579 0c6bdfca 2023-12-18 op for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
580 a596b957 2022-07-14 tracey if (strcmp(value, action_keys[a_cnt].name) != 0)
581 a596b957 2022-07-14 tracey continue;
582 c2f5b3e6 2024-05-28 op qs->action = action_keys[a_cnt].action;
583 c2f5b3e6 2024-05-28 op goto qa_found;
585 1552435e 2024-05-28 op qs->action = ERR;
586 a596b957 2022-07-14 tracey qa_found:
588 a596b957 2022-07-14 tracey case COMMIT:
589 1552435e 2024-05-28 op qs->commit = strdup(value);
590 1552435e 2024-05-28 op if (qs->commit == NULL) {
591 50f6148a 2023-05-29 op error = got_error_from_errno2(__func__,
593 a596b957 2022-07-14 tracey goto done;
596 a596b957 2022-07-14 tracey case RFILE:
597 1552435e 2024-05-28 op qs->file = strdup(value);
598 1552435e 2024-05-28 op if (qs->file == NULL) {
599 50f6148a 2023-05-29 op error = got_error_from_errno2(__func__,
601 a596b957 2022-07-14 tracey goto done;
604 a596b957 2022-07-14 tracey case FOLDER:
605 1552435e 2024-05-28 op qs->folder = strdup(value);
606 1552435e 2024-05-28 op if (qs->folder == NULL) {
607 50f6148a 2023-05-29 op error = got_error_from_errno2(__func__,
609 a596b957 2022-07-14 tracey goto done;
612 a596b957 2022-07-14 tracey case HEADREF:
613 1552435e 2024-05-28 op free(qs->headref);
614 1552435e 2024-05-28 op qs->headref = strdup(value);
615 1552435e 2024-05-28 op if (qs->headref == NULL) {
616 50f6148a 2023-05-29 op error = got_error_from_errno2(__func__,
618 a596b957 2022-07-14 tracey goto done;
621 a596b957 2022-07-14 tracey case INDEX_PAGE:
622 4448825a 2023-06-16 op if (*value == '\0')
624 1552435e 2024-05-28 op qs->index_page = strtonum(value, INT64_MIN,
625 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
626 a596b957 2022-07-14 tracey if (errstr) {
627 50f6148a 2023-05-29 op error = got_error_from_errno3(__func__,
628 50f6148a 2023-05-29 op "strtonum", errstr);
629 a596b957 2022-07-14 tracey goto done;
631 1552435e 2024-05-28 op if (qs->index_page < 0)
632 1552435e 2024-05-28 op qs->index_page = 0;
634 a596b957 2022-07-14 tracey case PATH:
635 1552435e 2024-05-28 op qs->path = strdup(value);
636 1552435e 2024-05-28 op if (qs->path == NULL) {
637 50f6148a 2023-05-29 op error = got_error_from_errno2(__func__,
639 a596b957 2022-07-14 tracey goto done;
644 03e70dd4 2023-11-16 op /* entry found */
648 a596b957 2022-07-14 tracey return error;
652 a596b957 2022-07-14 tracey gotweb_free_repo_tag(struct repo_tag *rt)
654 a596b957 2022-07-14 tracey if (rt != NULL) {
655 a596b957 2022-07-14 tracey free(rt->commit_id);
656 625e5896 2022-09-01 op free(rt->tag_name);
657 625e5896 2022-09-01 op free(rt->tag_commit);
658 625e5896 2022-09-01 op free(rt->commit_msg);
659 a596b957 2022-07-14 tracey free(rt->tagger);
661 a596b957 2022-07-14 tracey free(rt);
665 a596b957 2022-07-14 tracey gotweb_free_repo_commit(struct repo_commit *rc)
667 a596b957 2022-07-14 tracey if (rc != NULL) {
668 a596b957 2022-07-14 tracey free(rc->path);
669 a596b957 2022-07-14 tracey free(rc->refs_str);
670 a596b957 2022-07-14 tracey free(rc->commit_id);
671 a596b957 2022-07-14 tracey free(rc->parent_id);
672 a596b957 2022-07-14 tracey free(rc->tree_id);
673 a596b957 2022-07-14 tracey free(rc->author);
674 a596b957 2022-07-14 tracey free(rc->committer);
675 a596b957 2022-07-14 tracey free(rc->commit_msg);
677 a596b957 2022-07-14 tracey free(rc);
680 a596b957 2022-07-14 tracey static void
681 a596b957 2022-07-14 tracey gotweb_free_querystring(struct querystring *qs)
683 a596b957 2022-07-14 tracey if (qs != NULL) {
684 a596b957 2022-07-14 tracey free(qs->commit);
685 a596b957 2022-07-14 tracey free(qs->file);
686 a596b957 2022-07-14 tracey free(qs->folder);
687 a596b957 2022-07-14 tracey free(qs->headref);
688 a596b957 2022-07-14 tracey free(qs->path);
690 a596b957 2022-07-14 tracey free(qs);
693 a596b957 2022-07-14 tracey static void
694 a596b957 2022-07-14 tracey gotweb_free_repo_dir(struct repo_dir *repo_dir)
696 a596b957 2022-07-14 tracey if (repo_dir != NULL) {
697 a596b957 2022-07-14 tracey free(repo_dir->name);
698 a596b957 2022-07-14 tracey free(repo_dir->owner);
699 a596b957 2022-07-14 tracey free(repo_dir->description);
700 a596b957 2022-07-14 tracey free(repo_dir->url);
701 a596b957 2022-07-14 tracey free(repo_dir->path);
703 a596b957 2022-07-14 tracey free(repo_dir);
707 a596b957 2022-07-14 tracey gotweb_free_transport(struct transport *t)
709 df2d3cd2 2023-03-11 op const struct got_error *err;
710 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL, *trc = NULL;
711 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
714 df2d3cd2 2023-03-11 op got_ref_list_free(&t->refs);
715 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
716 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_commits, rc, entry);
717 a596b957 2022-07-14 tracey gotweb_free_repo_commit(rc);
719 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
720 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
721 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
723 a596b957 2022-07-14 tracey gotweb_free_repo_dir(t->repo_dir);
724 a596b957 2022-07-14 tracey gotweb_free_querystring(t->qs);
725 e3662697 2023-02-03 op free(t->more_id);
726 723721e2 2023-12-08 op free(t->tags_more_id);
728 df2d3cd2 2023-03-11 op got_object_blob_close(t->blob);
730 24a4d801 2023-05-16 op err = got_gotweb_closefile(t->fp);
732 24a4d801 2023-05-16 op log_warnx("%s: got_gotweb_closefile failure: %s",
733 df2d3cd2 2023-03-11 op __func__, err->msg);
735 276bccc4 2023-05-16 op if (t->fd != -1 && close(t->fd) == -1)
736 276bccc4 2023-05-16 op log_warn("%s: close", __func__);
737 df2d3cd2 2023-03-11 op if (t->repos) {
738 df2d3cd2 2023-03-11 op for (i = 0; i < t->nrepos; ++i)
739 df2d3cd2 2023-03-11 op free(t->repos[i]);
740 df2d3cd2 2023-03-11 op free(t->repos);
742 1632f50a 2023-11-17 stsp if (t->repo)
743 1632f50a 2023-11-17 stsp got_repo_close(t->repo);
748 723721e2 2023-12-08 op gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
749 b4c0bd72 2022-12-17 op struct gotweb_url *next, int *have_next)
751 a596b957 2022-07-14 tracey struct transport *t = c->t;
752 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
753 a596b957 2022-07-14 tracey struct server *srv = c->srv;
755 b4c0bd72 2022-12-17 op *have_prev = *have_next = 0;
757 723721e2 2023-12-08 op if (qs->index_page > 0) {
758 723721e2 2023-12-08 op *have_prev = 1;
759 723721e2 2023-12-08 op *prev = (struct gotweb_url){
760 723721e2 2023-12-08 op .action = -1,
761 723721e2 2023-12-08 op .index_page = qs->index_page - 1,
764 723721e2 2023-12-08 op if (t->next_disp == srv->max_repos_display &&
765 723721e2 2023-12-08 op t->repos_total != (qs->index_page + 1) *
766 723721e2 2023-12-08 op srv->max_repos_display) {
767 723721e2 2023-12-08 op *have_next = 1;
768 723721e2 2023-12-08 op *next = (struct gotweb_url){
769 723721e2 2023-12-08 op .action = -1,
770 723721e2 2023-12-08 op .index_page = qs->index_page + 1,
776 df2d3cd2 2023-03-11 op gotweb_render_index(struct template *tp)
778 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
779 df2d3cd2 2023-03-11 op struct request *c = tp->tp_arg;
780 a596b957 2022-07-14 tracey struct server *srv = c->srv;
781 a596b957 2022-07-14 tracey struct transport *t = c->t;
782 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
783 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
784 df2d3cd2 2023-03-11 op struct dirent **sd_dent = t->repos;
785 df2d3cd2 2023-03-11 op unsigned int d_i, d_disp = 0;
786 525dfdf4 2022-11-22 op unsigned int d_skipped = 0;
789 ed619ca0 2022-12-14 op if (gotweb_render_repo_table_hdr(c->tp) == -1)
792 df2d3cd2 2023-03-11 op for (d_i = 0; d_i < t->nrepos; d_i++) {
793 a596b957 2022-07-14 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
794 525dfdf4 2022-11-22 op strcmp(sd_dent[d_i]->d_name, "..") == 0) {
799 525dfdf4 2022-11-22 op error = got_path_dirent_type(&type, srv->repos_path,
800 525dfdf4 2022-11-22 op sd_dent[d_i]);
803 525dfdf4 2022-11-22 op if (type != DT_DIR) {
805 a596b957 2022-07-14 tracey continue;
808 a596b957 2022-07-14 tracey if (qs->index_page > 0 && (qs->index_page *
809 a596b957 2022-07-14 tracey srv->max_repos_display) > t->prev_disp) {
810 a596b957 2022-07-14 tracey t->prev_disp++;
811 a596b957 2022-07-14 tracey continue;
814 2959f418 2024-04-28 op error = gotweb_load_got_path(&repo_dir, sd_dent[d_i]->d_name,
816 ba55351b 2023-04-28 op if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
817 df2d3cd2 2023-03-11 op if (error->code != GOT_ERR_NOT_GIT_REPO)
818 df2d3cd2 2023-03-11 op log_warnx("%s: %s: %s", __func__,
819 df2d3cd2 2023-03-11 op sd_dent[d_i]->d_name, error->msg);
820 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
821 a596b957 2022-07-14 tracey repo_dir = NULL;
823 a596b957 2022-07-14 tracey continue;
826 a596b957 2022-07-14 tracey d_disp++;
827 a596b957 2022-07-14 tracey t->prev_disp++;
829 df2d3cd2 2023-03-11 op r = gotweb_render_repo_fragment(c->tp, repo_dir);
830 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
831 1632f50a 2023-11-17 stsp repo_dir = NULL;
832 1632f50a 2023-11-17 stsp got_repo_close(t->repo);
833 1632f50a 2023-11-17 stsp t->repo = NULL;
837 a596b957 2022-07-14 tracey t->next_disp++;
838 a596b957 2022-07-14 tracey if (d_disp == srv->max_repos_display)
841 df2d3cd2 2023-03-11 op t->repos_total = t->nrepos - d_skipped;
843 e114f3d1 2023-12-29 op if (srv->max_repos_display == 0 ||
844 a596b957 2022-07-14 tracey t->repos_total <= srv->max_repos_display)
847 b4c0bd72 2022-12-17 op if (gotweb_render_navs(c->tp) == -1)
853 8d02314f 2022-09-07 op static inline int
854 8d02314f 2022-09-07 op should_urlencode(int c)
856 8d02314f 2022-09-07 op if (c <= ' ' || c >= 127)
860 8d02314f 2022-09-07 op /* gen-delim */
868 8d02314f 2022-09-07 op /* sub-delims */
880 4a7f5bae 2023-01-05 op /* needed because the URLs are embedded into the HTML */
888 8d02314f 2022-09-07 op static char *
889 8d02314f 2022-09-07 op gotweb_urlencode(const char *str)
891 8d02314f 2022-09-07 op const char *s;
892 8d02314f 2022-09-07 op char *escaped;
893 8d02314f 2022-09-07 op size_t i, len;
897 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
899 8d02314f 2022-09-07 op if (should_urlencode(*s))
903 8d02314f 2022-09-07 op escaped = calloc(1, len + 1);
904 8d02314f 2022-09-07 op if (escaped == NULL)
908 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
909 8d02314f 2022-09-07 op if (should_urlencode(*s)) {
910 8d02314f 2022-09-07 op a = (*s & 0xF0) >> 4;
911 8d02314f 2022-09-07 op b = (*s & 0x0F);
913 8d02314f 2022-09-07 op escaped[i++] = '%';
914 8d02314f 2022-09-07 op escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
915 8d02314f 2022-09-07 op escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
917 8d02314f 2022-09-07 op escaped[i++] = *s;
920 8d02314f 2022-09-07 op return escaped;
924 ed619ca0 2022-12-14 op gotweb_action_name(int action)
926 8d02314f 2022-09-07 op switch (action) {
928 8d02314f 2022-09-07 op return "blame";
930 8d02314f 2022-09-07 op return "blob";
931 298f95fb 2023-01-05 op case BLOBRAW:
932 298f95fb 2023-01-05 op return "blobraw";
934 8d02314f 2022-09-07 op return "briefs";
935 8d02314f 2022-09-07 op case COMMITS:
936 8d02314f 2022-09-07 op return "commits";
938 8d02314f 2022-09-07 op return "diff";
940 8d02314f 2022-09-07 op return "err";
942 8d02314f 2022-09-07 op return "index";
944 7f65bb55 2023-12-01 op return "patch";
945 8d02314f 2022-09-07 op case SUMMARY:
946 8d02314f 2022-09-07 op return "summary";
948 8d02314f 2022-09-07 op return "tag";
950 8d02314f 2022-09-07 op return "tags";
952 8d02314f 2022-09-07 op return "tree";
954 1abb18e1 2022-12-20 op return "rss";
961 ed619ca0 2022-12-14 op gotweb_render_url(struct request *c, struct gotweb_url *url)
963 8d02314f 2022-09-07 op const char *sep = "?", *action;
967 ed619ca0 2022-12-14 op action = gotweb_action_name(url->action);
968 8d02314f 2022-09-07 op if (action != NULL) {
969 62eab86e 2023-09-13 op if (tp_writef(c->tp, "?action=%s", action) == -1)
974 8d02314f 2022-09-07 op if (url->commit) {
975 62eab86e 2023-09-13 op if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
980 8d02314f 2022-09-07 op if (url->file) {
981 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->file);
982 8d02314f 2022-09-07 op if (tmp == NULL)
984 62eab86e 2023-09-13 op r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
991 8d02314f 2022-09-07 op if (url->folder) {
992 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->folder);
993 8d02314f 2022-09-07 op if (tmp == NULL)
995 62eab86e 2023-09-13 op r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1002 8d02314f 2022-09-07 op if (url->headref) {
1003 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->headref);
1004 8d02314f 2022-09-07 op if (tmp == NULL)
1006 62eab86e 2023-09-13 op r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1008 8d02314f 2022-09-07 op if (r == -1)
1013 8d02314f 2022-09-07 op if (url->index_page != -1) {
1014 62eab86e 2023-09-13 op if (tp_writef(c->tp, "%sindex_page=%d", sep,
1015 8d02314f 2022-09-07 op url->index_page) == -1)
1020 8d02314f 2022-09-07 op if (url->path) {
1021 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->path);
1022 8d02314f 2022-09-07 op if (tmp == NULL)
1024 62eab86e 2023-09-13 op r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1026 8d02314f 2022-09-07 op if (r == -1)
1035 1abb18e1 2022-12-20 op gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1037 1abb18e1 2022-12-20 op struct template *tp = c->tp;
1038 1abb18e1 2022-12-20 op const char *proto = c->https ? "https" : "http";
1040 62eab86e 2023-09-13 op if (tp_writes(tp, proto) == -1 ||
1041 62eab86e 2023-09-13 op tp_writes(tp, "://") == -1 ||
1042 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->server_name) == -1 ||
1043 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->document_uri) == -1)
1046 1abb18e1 2022-12-20 op return gotweb_render_url(c, url);
1049 a596b957 2022-07-14 tracey static const struct got_error *
1050 2959f418 2024-04-28 op gotweb_load_got_path(struct repo_dir **rp, const char *dir,
1051 2959f418 2024-04-28 op struct request *c)
1053 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1054 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1055 a596b957 2022-07-14 tracey struct transport *t = c->t;
1056 2959f418 2024-04-28 op struct repo_dir *repo_dir;
1057 a596b957 2022-07-14 tracey DIR *dt;
1058 a596b957 2022-07-14 tracey char *dir_test;
1060 2959f418 2024-04-28 op *rp = calloc(1, sizeof(**rp));
1061 2959f418 2024-04-28 op if (*rp == NULL)
1062 2959f418 2024-04-28 op return got_error_from_errno("calloc");
1063 2959f418 2024-04-28 op repo_dir = *rp;
1065 2959f418 2024-04-28 op if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, dir,
1066 a596b957 2022-07-14 tracey GOTWEB_GIT_DIR) == -1)
1067 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1069 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1070 a596b957 2022-07-14 tracey if (dt == NULL) {
1071 a596b957 2022-07-14 tracey free(dir_test);
1072 2959f418 2024-04-28 op if (asprintf(&dir_test, "%s/%s", srv->repos_path, dir) == -1)
1073 260fd73e 2023-12-01 op return got_error_from_errno("asprintf");
1074 260fd73e 2023-12-01 op dt = opendir(dir_test);
1075 260fd73e 2023-12-01 op if (dt == NULL) {
1076 260fd73e 2023-12-01 op free(dir_test);
1077 2959f418 2024-04-28 op if (asprintf(&dir_test, "%s/%s%s", srv->repos_path,
1078 2959f418 2024-04-28 op dir, GOTWEB_GIT_DIR) == -1)
1079 2959f418 2024-04-28 op return got_error_from_errno("asprintf");
1080 2959f418 2024-04-28 op dt = opendir(dir_test);
1081 2959f418 2024-04-28 op if (dt == NULL) {
1082 2959f418 2024-04-28 op free(dir_test);
1083 2959f418 2024-04-28 op return got_error_path(dir,
1084 2959f418 2024-04-28 op GOT_ERR_NOT_GIT_REPO);
1089 260fd73e 2023-12-01 op repo_dir->path = dir_test;
1090 260fd73e 2023-12-01 op dir_test = NULL;
1092 2959f418 2024-04-28 op repo_dir->name = strdup(repo_dir->path + strlen(srv->repos_path) + 1);
1093 2959f418 2024-04-28 op if (repo_dir->name == NULL) {
1094 2959f418 2024-04-28 op error = got_error_from_errno("strdup");
1098 d5996b9e 2022-10-31 landry if (srv->respect_exportok &&
1099 d5996b9e 2022-10-31 landry faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1100 d5996b9e 2022-10-31 landry error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1101 d5996b9e 2022-10-31 landry goto err;
1104 2a83fef7 2024-05-27 op error = got_repo_open(&t->repo, repo_dir->path, NULL,
1105 2a83fef7 2024-05-27 op gotwebd_env->pack_fds);
1106 1632f50a 2023-11-17 stsp if (error)
1108 a596b957 2022-07-14 tracey error = gotweb_get_repo_description(&repo_dir->description, srv,
1109 3b81530f 2022-11-22 op repo_dir->path, dirfd(dt));
1110 a596b957 2022-07-14 tracey if (error)
1111 a596b957 2022-07-14 tracey goto err;
1112 c127fc49 2022-11-22 op error = got_get_repo_owner(&repo_dir->owner, c);
1113 a596b957 2022-07-14 tracey if (error)
1114 a596b957 2022-07-14 tracey goto err;
1115 417c8923 2023-08-11 op if (srv->show_repo_age) {
1116 417c8923 2023-08-11 op error = got_get_repo_age(&repo_dir->age, c, NULL);
1120 3b81530f 2022-11-22 op error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1123 a596b957 2022-07-14 tracey free(dir_test);
1124 0fad85dd 2022-09-01 op if (dt != NULL && closedir(dt) == EOF && error == NULL)
1125 0fad85dd 2022-09-01 op error = got_error_from_errno("closedir");
1126 1632f50a 2023-11-17 stsp if (error && t->repo) {
1127 1632f50a 2023-11-17 stsp got_repo_close(t->repo);
1128 1632f50a 2023-11-17 stsp t->repo = NULL;
1130 a596b957 2022-07-14 tracey return error;
1133 a596b957 2022-07-14 tracey static const struct got_error *
1134 3b81530f 2022-11-22 op gotweb_get_repo_description(char **description, struct server *srv,
1135 3b81530f 2022-11-22 op const char *dirpath, int dir)
1137 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1138 3b81530f 2022-11-22 op struct stat sb;
1139 3b81530f 2022-11-22 op int fd = -1;
1142 a596b957 2022-07-14 tracey *description = NULL;
1143 a596b957 2022-07-14 tracey if (srv->show_repo_description == 0)
1144 a596b957 2022-07-14 tracey return NULL;
1146 3b81530f 2022-11-22 op fd = openat(dir, "description", O_RDONLY);
1147 3b81530f 2022-11-22 op if (fd == -1) {
1148 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1149 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1150 3b81530f 2022-11-22 op dirpath, "description");
1152 a596b957 2022-07-14 tracey goto done;
1155 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1156 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1157 3b81530f 2022-11-22 op dirpath, "description");
1158 a596b957 2022-07-14 tracey goto done;
1161 3b81530f 2022-11-22 op len = sb.st_size;
1162 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXDESCRSZ - 1)
1163 270c41a2 2022-12-01 op len = GOTWEBD_MAXDESCRSZ - 1;
1165 a596b957 2022-07-14 tracey *description = calloc(len + 1, sizeof(**description));
1166 a596b957 2022-07-14 tracey if (*description == NULL) {
1167 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1168 a596b957 2022-07-14 tracey goto done;
1171 3b81530f 2022-11-22 op if (read(fd, *description, len) == -1)
1172 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1174 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1175 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1176 a596b957 2022-07-14 tracey return error;
1179 a596b957 2022-07-14 tracey static const struct got_error *
1180 3b81530f 2022-11-22 op gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1183 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1184 3b81530f 2022-11-22 op struct stat sb;
1185 3b81530f 2022-11-22 op int fd = -1;
1188 a596b957 2022-07-14 tracey *url = NULL;
1189 a596b957 2022-07-14 tracey if (srv->show_repo_cloneurl == 0)
1190 a596b957 2022-07-14 tracey return NULL;
1192 3b81530f 2022-11-22 op fd = openat(dir, "cloneurl", O_RDONLY);
1193 3b81530f 2022-11-22 op if (fd == -1) {
1194 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1195 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1196 3b81530f 2022-11-22 op dirpath, "cloneurl");
1198 a596b957 2022-07-14 tracey goto done;
1201 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1202 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1203 3b81530f 2022-11-22 op dirpath, "cloneurl");
1204 a596b957 2022-07-14 tracey goto done;
1207 3b81530f 2022-11-22 op len = sb.st_size;
1208 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1209 270c41a2 2022-12-01 op len = GOTWEBD_MAXCLONEURLSZ - 1;
1211 a596b957 2022-07-14 tracey *url = calloc(len + 1, sizeof(**url));
1212 a596b957 2022-07-14 tracey if (*url == NULL) {
1213 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1214 a596b957 2022-07-14 tracey goto done;
1217 3b81530f 2022-11-22 op if (read(fd, *url, len) == -1)
1218 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1220 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1221 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1222 a596b957 2022-07-14 tracey return error;
1226 7781b991 2023-10-05 op gotweb_render_age(struct template *tp, time_t committer_time)
1228 cb93ab40 2023-01-22 op struct request *c = tp->tp_arg;
1229 fced5a66 2022-07-20 naddy long long diff_time;
1230 a596b957 2022-07-14 tracey const char *years = "years ago", *months = "months ago";
1231 a596b957 2022-07-14 tracey const char *weeks = "weeks ago", *days = "days ago";
1232 a596b957 2022-07-14 tracey const char *hours = "hours ago", *minutes = "minutes ago";
1233 a596b957 2022-07-14 tracey const char *seconds = "seconds ago", *now = "right now";
1235 7781b991 2023-10-05 op diff_time = time(NULL) - committer_time;
1236 7781b991 2023-10-05 op if (diff_time > 60 * 60 * 24 * 365 * 2) {
1237 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s",
1238 7781b991 2023-10-05 op (diff_time / 60 / 60 / 24 / 365), years) == -1)
1240 7781b991 2023-10-05 op } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1241 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s",
1242 7781b991 2023-10-05 op (diff_time / 60 / 60 / 24 / (365 / 12)),
1243 7781b991 2023-10-05 op months) == -1)
1245 7781b991 2023-10-05 op } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1246 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s",
1247 7781b991 2023-10-05 op (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1249 7781b991 2023-10-05 op } else if (diff_time > 60 * 60 * 24 * 2) {
1250 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s",
1251 7781b991 2023-10-05 op (diff_time / 60 / 60 / 24), days) == -1)
1253 7781b991 2023-10-05 op } else if (diff_time > 60 * 60 * 2) {
1254 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s",
1255 7781b991 2023-10-05 op (diff_time / 60 / 60), hours) == -1)
1257 7781b991 2023-10-05 op } else if (diff_time > 60 * 2) {
1258 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1259 7781b991 2023-10-05 op minutes) == -1)
1261 7781b991 2023-10-05 op } else if (diff_time > 2) {
1262 7781b991 2023-10-05 op if (tp_writef(c->tp, "%lld %s", diff_time,
1263 7781b991 2023-10-05 op seconds) == -1)
1266 7781b991 2023-10-05 op if (tp_writes(tp, now) == -1)