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 "proc.h"
53 a596b957 2022-07-14 tracey #include "gotwebd.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 },
64 a596b957 2022-07-14 tracey { "page", PAGE },
67 a596b957 2022-07-14 tracey static const struct action_keys action_keys[] = {
68 a596b957 2022-07-14 tracey { "blame", BLAME },
69 a596b957 2022-07-14 tracey { "blob", BLOB },
70 298f95fb 2023-01-05 op { "blobraw", BLOBRAW },
71 a596b957 2022-07-14 tracey { "briefs", BRIEFS },
72 a596b957 2022-07-14 tracey { "commits", COMMITS },
73 a596b957 2022-07-14 tracey { "diff", DIFF },
74 a596b957 2022-07-14 tracey { "error", ERR },
75 a596b957 2022-07-14 tracey { "index", INDEX },
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 a596b957 2022-07-14 tracey static const struct got_error *gotweb_parse_querystring(struct querystring **,
86 a596b957 2022-07-14 tracey 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 a596b957 2022-07-14 tracey static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 a596b957 2022-07-14 tracey const char *);
91 a596b957 2022-07-14 tracey static const struct got_error *gotweb_load_got_path(struct request *c,
92 a596b957 2022-07-14 tracey struct repo_dir *);
93 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_repo_description(char **,
94 3b81530f 2022-11-22 op struct server *, const char *, int);
95 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 3b81530f 2022-11-22 op const char *, int);
98 a596b957 2022-07-14 tracey static void gotweb_free_querystring(struct querystring *);
99 a596b957 2022-07-14 tracey static void gotweb_free_repo_dir(struct repo_dir *);
101 95a4a5a1 2022-08-30 op struct server *gotweb_get_server(uint8_t *, uint8_t *);
104 6cdf29f9 2023-01-21 op gotweb_reply(struct request *c, int status, const char *ctype,
105 6cdf29f9 2023-01-21 op struct gotweb_url *location)
107 6cdf29f9 2023-01-21 op const char *csp;
109 6cdf29f9 2023-01-21 op if (status != 200 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
112 6cdf29f9 2023-01-21 op if (location) {
113 6cdf29f9 2023-01-21 op if (fcgi_puts(c->tp, "Location: ") == -1 ||
114 6cdf29f9 2023-01-21 op gotweb_render_url(c, location) == -1 ||
115 6cdf29f9 2023-01-21 op fcgi_puts(c->tp, "\r\n") == -1)
119 6cdf29f9 2023-01-21 op csp = "Content-Security-Policy: default-src 'self'; "
120 6cdf29f9 2023-01-21 op "script-src 'none'; object-src 'none';\r\n";
121 6cdf29f9 2023-01-21 op if (fcgi_puts(c->tp, csp) == -1)
124 6cdf29f9 2023-01-21 op if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
127 6cdf29f9 2023-01-21 op return fcgi_puts(c->tp, "\r\n");
131 6cdf29f9 2023-01-21 op gotweb_reply_file(struct request *c, const char *ctype, const char *file,
132 6cdf29f9 2023-01-21 op const char *suffix)
136 6cdf29f9 2023-01-21 op r = fcgi_printf(c, "Content-Disposition: attachment; "
137 6cdf29f9 2023-01-21 op "filename=%s%s\r\n", file, suffix ? suffix : "");
140 6cdf29f9 2023-01-21 op return gotweb_reply(c, 200, ctype, NULL);
144 a596b957 2022-07-14 tracey gotweb_process_request(struct request *c)
146 8f37175d 2023-03-11 op const struct got_error *error = NULL;
147 a596b957 2022-07-14 tracey struct server *srv = NULL;
148 a596b957 2022-07-14 tracey struct querystring *qs = NULL;
149 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
151 a596b957 2022-07-14 tracey /* init the transport */
152 a596b957 2022-07-14 tracey error = gotweb_init_transport(&c->t);
153 a596b957 2022-07-14 tracey if (error) {
154 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
157 a596b957 2022-07-14 tracey /* don't process any further if client disconnected */
158 a596b957 2022-07-14 tracey if (c->sock->client_status == CLIENT_DISCONNECT)
160 a596b957 2022-07-14 tracey /* get the gotwebd server */
161 95a4a5a1 2022-08-30 op srv = gotweb_get_server(c->server_name, c->http_host);
162 a596b957 2022-07-14 tracey if (srv == NULL) {
163 a596b957 2022-07-14 tracey log_warnx("%s: error server is NULL", __func__);
164 a596b957 2022-07-14 tracey goto err;
166 a596b957 2022-07-14 tracey c->srv = srv;
167 a596b957 2022-07-14 tracey /* parse our querystring */
168 a596b957 2022-07-14 tracey error = gotweb_init_querystring(&qs);
169 a596b957 2022-07-14 tracey if (error) {
170 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
171 a596b957 2022-07-14 tracey goto err;
173 a596b957 2022-07-14 tracey c->t->qs = qs;
174 a596b957 2022-07-14 tracey error = gotweb_parse_querystring(&qs, c->querystring);
175 a596b957 2022-07-14 tracey if (error) {
176 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
177 a596b957 2022-07-14 tracey goto err;
181 a596b957 2022-07-14 tracey * certain actions require a commit id in the querystring. this stops
182 a596b957 2022-07-14 tracey * bad actors from exploiting this by manually manipulating the
183 a596b957 2022-07-14 tracey * querystring.
186 298f95fb 2023-01-05 op if (qs->action == BLAME || qs->action == BLOB ||
187 298f95fb 2023-01-05 op qs->action == BLOBRAW || qs->action == DIFF) {
188 298f95fb 2023-01-05 op if (qs->commit == NULL) {
189 8f37175d 2023-03-11 op error = got_error(GOT_ERR_QUERYSTRING);
194 a596b957 2022-07-14 tracey if (qs->action != INDEX) {
195 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, qs->path);
196 a596b957 2022-07-14 tracey if (error)
198 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
199 a596b957 2022-07-14 tracey c->t->repo_dir = repo_dir;
200 a596b957 2022-07-14 tracey if (error && error->code != GOT_ERR_LONELY_PACKIDX)
201 a596b957 2022-07-14 tracey goto err;
204 298f95fb 2023-01-05 op if (qs->action == BLOBRAW) {
205 76007998 2023-01-15 op const uint8_t *buf;
207 6cdf29f9 2023-01-21 op int binary, r;
209 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
210 a596b957 2022-07-14 tracey if (error)
213 8f37175d 2023-03-11 op error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
219 6cdf29f9 2023-01-21 op r = gotweb_reply_file(c, "application/octet-stream",
220 6cdf29f9 2023-01-21 op qs->file, NULL);
222 6cdf29f9 2023-01-21 op r = gotweb_reply(c, 200, "text/plain", NULL);
227 df2d3cd2 2023-03-11 op error = got_object_blob_read_block(&len, c->t->blob);
230 76007998 2023-01-15 op if (len == 0)
232 df2d3cd2 2023-03-11 op buf = got_object_blob_get_read_buf(c->t->blob);
233 76007998 2023-01-15 op if (fcgi_gen_binary_response(c, buf, len) == -1)
240 298f95fb 2023-01-05 op if (qs->action == BLOB) {
242 298f95fb 2023-01-05 op struct gotweb_url url = {
243 298f95fb 2023-01-05 op .index_page = -1,
245 298f95fb 2023-01-05 op .action = BLOBRAW,
246 298f95fb 2023-01-05 op .path = qs->path,
247 298f95fb 2023-01-05 op .commit = qs->commit,
248 298f95fb 2023-01-05 op .folder = qs->folder,
249 298f95fb 2023-01-05 op .file = qs->file,
252 298f95fb 2023-01-05 op error = got_get_repo_commits(c, 1);
256 8f37175d 2023-03-11 op error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
260 298f95fb 2023-01-05 op if (binary) {
261 6cdf29f9 2023-01-21 op gotweb_reply(c, 302, NULL, &url);
266 1abb18e1 2022-12-20 op if (qs->action == RSS) {
267 6cdf29f9 2023-01-21 op const char *ctype = "application/rss+xml;charset=utf-8";
269 6cdf29f9 2023-01-21 op if (gotweb_reply_file(c, ctype, repo_dir->name, ".rss") == -1)
272 1abb18e1 2022-12-20 op error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
273 a596b957 2022-07-14 tracey if (error) {
274 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
277 8f37175d 2023-03-11 op gotweb_render_rss(c->tp);
281 a596b957 2022-07-14 tracey switch(qs->action) {
282 a596b957 2022-07-14 tracey case BLAME:
283 8319855f 2023-01-15 op error = got_get_repo_commits(c, 1);
284 a596b957 2022-07-14 tracey if (error) {
285 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
286 a596b957 2022-07-14 tracey goto err;
288 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
290 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_blame);
293 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
295 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_blob);
297 a596b957 2022-07-14 tracey case BRIEFS:
298 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
300 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_briefs);
302 a596b957 2022-07-14 tracey case COMMITS:
303 156a1144 2022-12-17 op error = got_get_repo_commits(c, srv->max_commits_display);
304 a596b957 2022-07-14 tracey if (error) {
305 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
306 a596b957 2022-07-14 tracey goto err;
308 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
310 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_commits);
312 a596b957 2022-07-14 tracey case DIFF:
313 587550a5 2023-01-10 op error = got_get_repo_commits(c, 1);
315 169b1631 2023-01-06 op log_warnx("%s: %s", __func__, error->msg);
318 df2d3cd2 2023-03-11 op error = got_open_diff_for_output(&c->t->fp, &c->t->fd, c);
320 587550a5 2023-01-10 op log_warnx("%s: %s", __func__, error->msg);
323 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
325 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_diff);
327 a596b957 2022-07-14 tracey case INDEX:
328 df2d3cd2 2023-03-11 op c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
330 df2d3cd2 2023-03-11 op if (c->t->nrepos == -1) {
331 df2d3cd2 2023-03-11 op c->t->repos = NULL;
332 df2d3cd2 2023-03-11 op error = got_error_from_errno2("scandir",
333 df2d3cd2 2023-03-11 op srv->repos_path);
334 a596b957 2022-07-14 tracey goto err;
336 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
338 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_index);
340 a596b957 2022-07-14 tracey case SUMMARY:
341 df2d3cd2 2023-03-11 op error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
342 69525b4e 2023-01-13 op got_ref_cmp_by_name, NULL);
343 a596b957 2022-07-14 tracey if (error) {
344 69525b4e 2023-01-13 op log_warnx("%s: got_ref_list: %s", __func__,
346 a596b957 2022-07-14 tracey goto err;
348 69525b4e 2023-01-13 op qs->action = TAGS;
349 69525b4e 2023-01-13 op error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
351 69525b4e 2023-01-13 op log_warnx("%s: got_get_repo_tags: %s", __func__,
355 69525b4e 2023-01-13 op qs->action = SUMMARY;
356 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
358 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_summary);
360 a596b957 2022-07-14 tracey case TAG:
361 dc07f76c 2023-01-09 op error = got_get_repo_tags(c, 1);
362 a596b957 2022-07-14 tracey if (error) {
363 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
366 dc07f76c 2023-01-09 op if (c->t->tag_count == 0) {
367 dc07f76c 2023-01-09 op error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
368 dc07f76c 2023-01-09 op "bad commit id");
369 a596b957 2022-07-14 tracey goto err;
371 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
373 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_tag);
375 a596b957 2022-07-14 tracey case TAGS:
376 d60961d2 2023-01-13 op error = got_get_repo_tags(c, srv->max_commits_display);
377 a596b957 2022-07-14 tracey if (error) {
378 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
379 a596b957 2022-07-14 tracey goto err;
381 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
383 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_tags);
385 a596b957 2022-07-14 tracey case TREE:
386 43d421de 2023-01-05 op error = got_get_repo_commits(c, 1);
387 a596b957 2022-07-14 tracey if (error) {
388 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
389 a596b957 2022-07-14 tracey goto err;
391 8f37175d 2023-03-11 op if (gotweb_reply(c, 200, "text/html", NULL) == -1)
393 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_tree);
395 a596b957 2022-07-14 tracey case ERR:
397 8f37175d 2023-03-11 op error = got_error(GOT_ERR_BAD_QUERYSTRING);
401 8f37175d 2023-03-11 op c->t->error = error;
402 8f37175d 2023-03-11 op if (gotweb_reply(c, 400, "text/html", NULL) == -1)
404 8f37175d 2023-03-11 op gotweb_render_page(c->tp, gotweb_render_error);
407 a596b957 2022-07-14 tracey struct server *
408 95a4a5a1 2022-08-30 op gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
410 a596b957 2022-07-14 tracey struct server *srv = NULL;
412 95a4a5a1 2022-08-30 op /* check against the server name first */
413 a596b957 2022-07-14 tracey if (strlen(server_name) > 0)
414 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
415 a596b957 2022-07-14 tracey if (strcmp(srv->name, server_name) == 0)
416 a596b957 2022-07-14 tracey goto done;
418 95a4a5a1 2022-08-30 op /* check against subdomain second */
419 a596b957 2022-07-14 tracey if (strlen(subdomain) > 0)
420 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
421 a596b957 2022-07-14 tracey if (strcmp(srv->name, subdomain) == 0)
422 a596b957 2022-07-14 tracey goto done;
424 a596b957 2022-07-14 tracey /* if those fail, send first server */
425 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
426 a596b957 2022-07-14 tracey if (srv != NULL)
429 a596b957 2022-07-14 tracey return srv;
432 a596b957 2022-07-14 tracey const struct got_error *
433 a596b957 2022-07-14 tracey gotweb_init_transport(struct transport **t)
435 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
437 a596b957 2022-07-14 tracey *t = calloc(1, sizeof(**t));
438 a596b957 2022-07-14 tracey if (*t == NULL)
439 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
441 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_commits);
442 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_tags);
443 df2d3cd2 2023-03-11 op TAILQ_INIT(&(*t)->refs);
445 a596b957 2022-07-14 tracey (*t)->repo = NULL;
446 a596b957 2022-07-14 tracey (*t)->repo_dir = NULL;
447 a596b957 2022-07-14 tracey (*t)->qs = NULL;
448 a596b957 2022-07-14 tracey (*t)->next_id = NULL;
449 a596b957 2022-07-14 tracey (*t)->prev_id = NULL;
450 a596b957 2022-07-14 tracey (*t)->next_disp = 0;
451 a596b957 2022-07-14 tracey (*t)->prev_disp = 0;
453 df2d3cd2 2023-03-11 op (*t)->fd = -1;
455 a596b957 2022-07-14 tracey return error;
458 a596b957 2022-07-14 tracey static const struct got_error *
459 a596b957 2022-07-14 tracey gotweb_init_querystring(struct querystring **qs)
461 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
463 a596b957 2022-07-14 tracey *qs = calloc(1, sizeof(**qs));
464 a596b957 2022-07-14 tracey if (*qs == NULL)
465 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
467 a596b957 2022-07-14 tracey (*qs)->headref = strdup("HEAD");
468 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
471 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
474 6c37ad7b 2022-09-01 op (*qs)->action = INDEX;
475 6c37ad7b 2022-09-01 op (*qs)->commit = NULL;
476 6c37ad7b 2022-09-01 op (*qs)->file = NULL;
477 6c37ad7b 2022-09-01 op (*qs)->folder = NULL;
478 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
479 a596b957 2022-07-14 tracey (*qs)->path = NULL;
481 a596b957 2022-07-14 tracey return error;
484 a596b957 2022-07-14 tracey static const struct got_error *
485 a596b957 2022-07-14 tracey gotweb_parse_querystring(struct querystring **qs, char *qst)
487 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
488 a596b957 2022-07-14 tracey char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
489 a596b957 2022-07-14 tracey char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
491 a596b957 2022-07-14 tracey if (qst == NULL)
492 a596b957 2022-07-14 tracey return error;
494 a596b957 2022-07-14 tracey tok1 = strdup(qst);
495 a596b957 2022-07-14 tracey if (tok1 == NULL)
496 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
498 a596b957 2022-07-14 tracey tok1_pair = tok1;
499 a596b957 2022-07-14 tracey tok1_end = tok1;
501 a596b957 2022-07-14 tracey while (tok1_pair != NULL) {
502 a596b957 2022-07-14 tracey strsep(&tok1_end, "&");
504 a596b957 2022-07-14 tracey tok2 = strdup(tok1_pair);
505 a596b957 2022-07-14 tracey if (tok2 == NULL) {
506 a596b957 2022-07-14 tracey free(tok1);
507 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
510 a596b957 2022-07-14 tracey tok2_pair = tok2;
511 a596b957 2022-07-14 tracey tok2_end = tok2;
513 a596b957 2022-07-14 tracey while (tok2_pair != NULL) {
514 a596b957 2022-07-14 tracey strsep(&tok2_end, "=");
515 a596b957 2022-07-14 tracey if (tok2_end) {
516 a596b957 2022-07-14 tracey error = gotweb_assign_querystring(qs, tok2_pair,
517 a596b957 2022-07-14 tracey tok2_end);
518 a596b957 2022-07-14 tracey if (error)
519 a596b957 2022-07-14 tracey goto err;
521 a596b957 2022-07-14 tracey tok2_pair = tok2_end;
523 a596b957 2022-07-14 tracey free(tok2);
524 a596b957 2022-07-14 tracey tok1_pair = tok1_end;
526 a596b957 2022-07-14 tracey free(tok1);
527 a596b957 2022-07-14 tracey return error;
529 a596b957 2022-07-14 tracey free(tok2);
530 a596b957 2022-07-14 tracey free(tok1);
531 a596b957 2022-07-14 tracey return error;
535 58381f70 2022-09-03 op * Adapted from usr.sbin/httpd/httpd.c url_decode.
537 a596b957 2022-07-14 tracey static const struct got_error *
538 58381f70 2022-09-03 op gotweb_urldecode(char *url)
542 58381f70 2022-09-03 op unsigned long x;
544 58381f70 2022-09-03 op hex[2] = '\0';
547 58381f70 2022-09-03 op while (*p != '\0') {
548 58381f70 2022-09-03 op switch (*p) {
550 58381f70 2022-09-03 op /* Encoding character is followed by two hex chars */
551 58381f70 2022-09-03 op if (!isxdigit((unsigned char)p[1]) ||
552 58381f70 2022-09-03 op !isxdigit((unsigned char)p[2]) ||
553 58381f70 2022-09-03 op (p[1] == '0' && p[2] == '0'))
554 58381f70 2022-09-03 op return got_error(GOT_ERR_BAD_QUERYSTRING);
556 58381f70 2022-09-03 op hex[0] = p[1];
557 58381f70 2022-09-03 op hex[1] = p[2];
560 58381f70 2022-09-03 op * We don't have to validate "hex" because it is
561 58381f70 2022-09-03 op * guaranteed to include two hex chars followed by nul.
563 58381f70 2022-09-03 op x = strtoul(hex, NULL, 16);
564 58381f70 2022-09-03 op *q = (char)x;
579 58381f70 2022-09-03 op static const struct got_error *
580 a596b957 2022-07-14 tracey gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
582 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
583 a596b957 2022-07-14 tracey const char *errstr;
584 a596b957 2022-07-14 tracey int a_cnt, el_cnt;
586 58381f70 2022-09-03 op error = gotweb_urldecode(value);
588 58381f70 2022-09-03 op return error;
590 a596b957 2022-07-14 tracey for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
591 a596b957 2022-07-14 tracey if (strcmp(key, querystring_keys[el_cnt].name) != 0)
592 a596b957 2022-07-14 tracey continue;
594 a596b957 2022-07-14 tracey switch (querystring_keys[el_cnt].element) {
595 a596b957 2022-07-14 tracey case ACTION:
596 a596b957 2022-07-14 tracey for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
597 a596b957 2022-07-14 tracey if (strcmp(value, action_keys[a_cnt].name) != 0)
598 a596b957 2022-07-14 tracey continue;
599 a596b957 2022-07-14 tracey else if (strcmp(value,
600 a596b957 2022-07-14 tracey action_keys[a_cnt].name) == 0){
601 a596b957 2022-07-14 tracey (*qs)->action =
602 a596b957 2022-07-14 tracey action_keys[a_cnt].action;
603 a596b957 2022-07-14 tracey goto qa_found;
606 a596b957 2022-07-14 tracey (*qs)->action = ERR;
607 a596b957 2022-07-14 tracey qa_found:
609 a596b957 2022-07-14 tracey case COMMIT:
610 a596b957 2022-07-14 tracey (*qs)->commit = strdup(value);
611 a596b957 2022-07-14 tracey if ((*qs)->commit == NULL) {
612 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
613 a596b957 2022-07-14 tracey __func__);
614 a596b957 2022-07-14 tracey goto done;
617 a596b957 2022-07-14 tracey case RFILE:
618 a596b957 2022-07-14 tracey (*qs)->file = strdup(value);
619 a596b957 2022-07-14 tracey if ((*qs)->file == NULL) {
620 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
621 a596b957 2022-07-14 tracey __func__);
622 a596b957 2022-07-14 tracey goto done;
625 a596b957 2022-07-14 tracey case FOLDER:
626 a596b957 2022-07-14 tracey (*qs)->folder = strdup(value);
627 a596b957 2022-07-14 tracey if ((*qs)->folder == NULL) {
628 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
629 a596b957 2022-07-14 tracey __func__);
630 a596b957 2022-07-14 tracey goto done;
633 a596b957 2022-07-14 tracey case HEADREF:
634 f8faf9f1 2022-09-01 op free((*qs)->headref);
635 a596b957 2022-07-14 tracey (*qs)->headref = strdup(value);
636 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
637 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
638 a596b957 2022-07-14 tracey __func__);
639 a596b957 2022-07-14 tracey goto done;
642 a596b957 2022-07-14 tracey case INDEX_PAGE:
643 a596b957 2022-07-14 tracey if (strlen(value) == 0)
645 a596b957 2022-07-14 tracey (*qs)->index_page = strtonum(value, INT64_MIN,
646 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
647 a596b957 2022-07-14 tracey if (errstr) {
648 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
649 a596b957 2022-07-14 tracey __func__, errstr);
650 a596b957 2022-07-14 tracey goto done;
652 03f6a843 2022-12-17 op if ((*qs)->index_page < 0)
653 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
655 a596b957 2022-07-14 tracey case PATH:
656 a596b957 2022-07-14 tracey (*qs)->path = strdup(value);
657 a596b957 2022-07-14 tracey if ((*qs)->path == NULL) {
658 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
659 a596b957 2022-07-14 tracey __func__);
660 a596b957 2022-07-14 tracey goto done;
663 a596b957 2022-07-14 tracey case PAGE:
664 a596b957 2022-07-14 tracey if (strlen(value) == 0)
666 a596b957 2022-07-14 tracey (*qs)->page = strtonum(value, INT64_MIN,
667 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
668 a596b957 2022-07-14 tracey if (errstr) {
669 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
670 a596b957 2022-07-14 tracey __func__, errstr);
671 a596b957 2022-07-14 tracey goto done;
673 03f6a843 2022-12-17 op if ((*qs)->page < 0)
674 a596b957 2022-07-14 tracey (*qs)->page = 0;
681 a596b957 2022-07-14 tracey return error;
685 a596b957 2022-07-14 tracey gotweb_free_repo_tag(struct repo_tag *rt)
687 a596b957 2022-07-14 tracey if (rt != NULL) {
688 a596b957 2022-07-14 tracey free(rt->commit_id);
689 625e5896 2022-09-01 op free(rt->tag_name);
690 625e5896 2022-09-01 op free(rt->tag_commit);
691 625e5896 2022-09-01 op free(rt->commit_msg);
692 a596b957 2022-07-14 tracey free(rt->tagger);
694 a596b957 2022-07-14 tracey free(rt);
698 a596b957 2022-07-14 tracey gotweb_free_repo_commit(struct repo_commit *rc)
700 a596b957 2022-07-14 tracey if (rc != NULL) {
701 a596b957 2022-07-14 tracey free(rc->path);
702 a596b957 2022-07-14 tracey free(rc->refs_str);
703 a596b957 2022-07-14 tracey free(rc->commit_id);
704 a596b957 2022-07-14 tracey free(rc->parent_id);
705 a596b957 2022-07-14 tracey free(rc->tree_id);
706 a596b957 2022-07-14 tracey free(rc->author);
707 a596b957 2022-07-14 tracey free(rc->committer);
708 a596b957 2022-07-14 tracey free(rc->commit_msg);
710 a596b957 2022-07-14 tracey free(rc);
713 a596b957 2022-07-14 tracey static void
714 a596b957 2022-07-14 tracey gotweb_free_querystring(struct querystring *qs)
716 a596b957 2022-07-14 tracey if (qs != NULL) {
717 a596b957 2022-07-14 tracey free(qs->commit);
718 a596b957 2022-07-14 tracey free(qs->file);
719 a596b957 2022-07-14 tracey free(qs->folder);
720 a596b957 2022-07-14 tracey free(qs->headref);
721 a596b957 2022-07-14 tracey free(qs->path);
723 a596b957 2022-07-14 tracey free(qs);
726 a596b957 2022-07-14 tracey static void
727 a596b957 2022-07-14 tracey gotweb_free_repo_dir(struct repo_dir *repo_dir)
729 a596b957 2022-07-14 tracey if (repo_dir != NULL) {
730 a596b957 2022-07-14 tracey free(repo_dir->name);
731 a596b957 2022-07-14 tracey free(repo_dir->owner);
732 a596b957 2022-07-14 tracey free(repo_dir->description);
733 a596b957 2022-07-14 tracey free(repo_dir->url);
734 a596b957 2022-07-14 tracey free(repo_dir->path);
736 a596b957 2022-07-14 tracey free(repo_dir);
740 a596b957 2022-07-14 tracey gotweb_free_transport(struct transport *t)
742 df2d3cd2 2023-03-11 op const struct got_error *err;
743 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL, *trc = NULL;
744 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
747 df2d3cd2 2023-03-11 op got_ref_list_free(&t->refs);
748 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
749 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_commits, rc, entry);
750 a596b957 2022-07-14 tracey gotweb_free_repo_commit(rc);
752 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
753 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
754 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
756 a596b957 2022-07-14 tracey gotweb_free_repo_dir(t->repo_dir);
757 a596b957 2022-07-14 tracey gotweb_free_querystring(t->qs);
758 e3662697 2023-02-03 op free(t->more_id);
759 341fa7ca 2022-09-01 op free(t->next_id);
760 341fa7ca 2022-09-01 op free(t->prev_id);
762 df2d3cd2 2023-03-11 op got_object_blob_close(t->blob);
764 df2d3cd2 2023-03-11 op err = got_gotweb_flushfile(t->fp, t->fd);
766 df2d3cd2 2023-03-11 op log_warnx("%s: got_gotweb_flushfile failure: %s",
767 df2d3cd2 2023-03-11 op __func__, err->msg);
770 df2d3cd2 2023-03-11 op if (t->fd != -1)
771 df2d3cd2 2023-03-11 op close(t->fd);
772 df2d3cd2 2023-03-11 op if (t->repos) {
773 df2d3cd2 2023-03-11 op for (i = 0; i < t->nrepos; ++i)
774 df2d3cd2 2023-03-11 op free(t->repos[i]);
775 df2d3cd2 2023-03-11 op free(t->repos);
781 b4c0bd72 2022-12-17 op gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
782 b4c0bd72 2022-12-17 op struct gotweb_url *next, int *have_next)
784 a596b957 2022-07-14 tracey struct transport *t = c->t;
785 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
786 a596b957 2022-07-14 tracey struct server *srv = c->srv;
788 b4c0bd72 2022-12-17 op *have_prev = *have_next = 0;
790 a596b957 2022-07-14 tracey switch(qs->action) {
791 a596b957 2022-07-14 tracey case INDEX:
792 a596b957 2022-07-14 tracey if (qs->index_page > 0) {
793 b4c0bd72 2022-12-17 op *have_prev = 1;
794 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
795 8d02314f 2022-09-07 op .action = -1,
796 8d02314f 2022-09-07 op .index_page = qs->index_page - 1,
800 b4c0bd72 2022-12-17 op if (t->next_disp == srv->max_repos_display &&
801 b4c0bd72 2022-12-17 op t->repos_total != (qs->index_page + 1) *
802 b4c0bd72 2022-12-17 op srv->max_repos_display) {
803 b4c0bd72 2022-12-17 op *have_next = 1;
804 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
805 b4c0bd72 2022-12-17 op .action = -1,
806 b4c0bd72 2022-12-17 op .index_page = qs->index_page + 1,
811 a596b957 2022-07-14 tracey case TAGS:
812 b4c0bd72 2022-12-17 op if (t->prev_id && qs->commit != NULL &&
813 b4c0bd72 2022-12-17 op strcmp(qs->commit, t->prev_id) != 0) {
814 b4c0bd72 2022-12-17 op *have_prev = 1;
815 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
816 b4c0bd72 2022-12-17 op .action = TAGS,
817 b4c0bd72 2022-12-17 op .index_page = -1,
818 b4c0bd72 2022-12-17 op .page = qs->page - 1,
819 b4c0bd72 2022-12-17 op .path = qs->path,
820 b4c0bd72 2022-12-17 op .commit = t->prev_id,
821 b4c0bd72 2022-12-17 op .headref = qs->headref,
824 a596b957 2022-07-14 tracey if (t->next_id) {
825 b4c0bd72 2022-12-17 op *have_next = 1;
826 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
827 8d02314f 2022-09-07 op .action = TAGS,
828 8d02314f 2022-09-07 op .index_page = -1,
829 8d02314f 2022-09-07 op .page = qs->page + 1,
830 8d02314f 2022-09-07 op .path = qs->path,
831 8d02314f 2022-09-07 op .commit = t->next_id,
832 8d02314f 2022-09-07 op .headref = qs->headref,
840 df2d3cd2 2023-03-11 op gotweb_render_index(struct template *tp)
842 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
843 df2d3cd2 2023-03-11 op struct request *c = tp->tp_arg;
844 a596b957 2022-07-14 tracey struct server *srv = c->srv;
845 a596b957 2022-07-14 tracey struct transport *t = c->t;
846 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
847 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
848 df2d3cd2 2023-03-11 op struct dirent **sd_dent = t->repos;
849 df2d3cd2 2023-03-11 op unsigned int d_i, d_disp = 0;
850 525dfdf4 2022-11-22 op unsigned int d_skipped = 0;
853 ed619ca0 2022-12-14 op if (gotweb_render_repo_table_hdr(c->tp) == -1)
856 df2d3cd2 2023-03-11 op for (d_i = 0; d_i < t->nrepos; d_i++) {
857 659fa237 2022-11-22 op if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
860 a596b957 2022-07-14 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
861 525dfdf4 2022-11-22 op strcmp(sd_dent[d_i]->d_name, "..") == 0) {
866 525dfdf4 2022-11-22 op error = got_path_dirent_type(&type, srv->repos_path,
867 525dfdf4 2022-11-22 op sd_dent[d_i]);
870 525dfdf4 2022-11-22 op if (type != DT_DIR) {
872 a596b957 2022-07-14 tracey continue;
875 a596b957 2022-07-14 tracey if (qs->index_page > 0 && (qs->index_page *
876 a596b957 2022-07-14 tracey srv->max_repos_display) > t->prev_disp) {
877 a596b957 2022-07-14 tracey t->prev_disp++;
878 a596b957 2022-07-14 tracey continue;
881 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
882 a596b957 2022-07-14 tracey if (error)
885 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
886 df2d3cd2 2023-03-11 op if (error && error->code == GOT_ERR_LONELY_PACKIDX) {
887 df2d3cd2 2023-03-11 op if (error->code != GOT_ERR_NOT_GIT_REPO)
888 df2d3cd2 2023-03-11 op log_warnx("%s: %s: %s", __func__,
889 df2d3cd2 2023-03-11 op sd_dent[d_i]->d_name, error->msg);
890 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
891 a596b957 2022-07-14 tracey repo_dir = NULL;
893 a596b957 2022-07-14 tracey continue;
896 a596b957 2022-07-14 tracey d_disp++;
897 a596b957 2022-07-14 tracey t->prev_disp++;
899 df2d3cd2 2023-03-11 op r = gotweb_render_repo_fragment(c->tp, repo_dir);
900 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
904 a596b957 2022-07-14 tracey t->next_disp++;
905 a596b957 2022-07-14 tracey if (d_disp == srv->max_repos_display)
908 df2d3cd2 2023-03-11 op t->repos_total = t->nrepos - d_skipped;
910 a596b957 2022-07-14 tracey if (srv->max_repos_display == 0)
912 a596b957 2022-07-14 tracey if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
914 a596b957 2022-07-14 tracey if (t->repos_total <= srv->max_repos ||
915 a596b957 2022-07-14 tracey t->repos_total <= srv->max_repos_display)
918 b4c0bd72 2022-12-17 op if (gotweb_render_navs(c->tp) == -1)
924 8d02314f 2022-09-07 op static inline int
925 8d02314f 2022-09-07 op should_urlencode(int c)
927 8d02314f 2022-09-07 op if (c <= ' ' || c >= 127)
931 8d02314f 2022-09-07 op /* gen-delim */
939 8d02314f 2022-09-07 op /* sub-delims */
951 4a7f5bae 2023-01-05 op /* needed because the URLs are embedded into the HTML */
959 8d02314f 2022-09-07 op static char *
960 8d02314f 2022-09-07 op gotweb_urlencode(const char *str)
962 8d02314f 2022-09-07 op const char *s;
963 8d02314f 2022-09-07 op char *escaped;
964 8d02314f 2022-09-07 op size_t i, len;
968 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
970 8d02314f 2022-09-07 op if (should_urlencode(*s))
974 8d02314f 2022-09-07 op escaped = calloc(1, len + 1);
975 8d02314f 2022-09-07 op if (escaped == NULL)
979 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
980 8d02314f 2022-09-07 op if (should_urlencode(*s)) {
981 8d02314f 2022-09-07 op a = (*s & 0xF0) >> 4;
982 8d02314f 2022-09-07 op b = (*s & 0x0F);
984 8d02314f 2022-09-07 op escaped[i++] = '%';
985 8d02314f 2022-09-07 op escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
986 8d02314f 2022-09-07 op escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
988 8d02314f 2022-09-07 op escaped[i++] = *s;
991 8d02314f 2022-09-07 op return escaped;
995 ed619ca0 2022-12-14 op gotweb_action_name(int action)
997 8d02314f 2022-09-07 op switch (action) {
999 8d02314f 2022-09-07 op return "blame";
1001 8d02314f 2022-09-07 op return "blob";
1002 298f95fb 2023-01-05 op case BLOBRAW:
1003 298f95fb 2023-01-05 op return "blobraw";
1004 8d02314f 2022-09-07 op case BRIEFS:
1005 8d02314f 2022-09-07 op return "briefs";
1006 8d02314f 2022-09-07 op case COMMITS:
1007 8d02314f 2022-09-07 op return "commits";
1009 8d02314f 2022-09-07 op return "diff";
1011 8d02314f 2022-09-07 op return "err";
1013 8d02314f 2022-09-07 op return "index";
1014 8d02314f 2022-09-07 op case SUMMARY:
1015 8d02314f 2022-09-07 op return "summary";
1017 8d02314f 2022-09-07 op return "tag";
1019 8d02314f 2022-09-07 op return "tags";
1021 8d02314f 2022-09-07 op return "tree";
1023 1abb18e1 2022-12-20 op return "rss";
1025 8d02314f 2022-09-07 op return NULL;
1030 ed619ca0 2022-12-14 op gotweb_render_url(struct request *c, struct gotweb_url *url)
1032 8d02314f 2022-09-07 op const char *sep = "?", *action;
1036 ed619ca0 2022-12-14 op action = gotweb_action_name(url->action);
1037 8d02314f 2022-09-07 op if (action != NULL) {
1038 8d02314f 2022-09-07 op if (fcgi_printf(c, "?action=%s", action) == -1)
1043 8d02314f 2022-09-07 op if (url->commit) {
1044 8d02314f 2022-09-07 op if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1049 8d02314f 2022-09-07 op if (url->previd) {
1050 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1055 8d02314f 2022-09-07 op if (url->prevset) {
1056 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1061 8d02314f 2022-09-07 op if (url->file) {
1062 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->file);
1063 8d02314f 2022-09-07 op if (tmp == NULL)
1065 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1067 8d02314f 2022-09-07 op if (r == -1)
1072 8d02314f 2022-09-07 op if (url->folder) {
1073 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->folder);
1074 8d02314f 2022-09-07 op if (tmp == NULL)
1076 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1078 8d02314f 2022-09-07 op if (r == -1)
1083 8d02314f 2022-09-07 op if (url->headref) {
1084 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->headref);
1085 8d02314f 2022-09-07 op if (tmp == NULL)
1087 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1089 8d02314f 2022-09-07 op if (r == -1)
1094 8d02314f 2022-09-07 op if (url->index_page != -1) {
1095 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sindex_page=%d", sep,
1096 8d02314f 2022-09-07 op url->index_page) == -1)
1101 8d02314f 2022-09-07 op if (url->path) {
1102 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->path);
1103 8d02314f 2022-09-07 op if (tmp == NULL)
1105 8d02314f 2022-09-07 op r = fcgi_printf(c, "%spath=%s", sep, tmp);
1107 8d02314f 2022-09-07 op if (r == -1)
1112 8d02314f 2022-09-07 op if (url->page != -1) {
1113 8d02314f 2022-09-07 op if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1122 1abb18e1 2022-12-20 op gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1124 1abb18e1 2022-12-20 op struct template *tp = c->tp;
1125 1abb18e1 2022-12-20 op const char *proto = c->https ? "https" : "http";
1127 1abb18e1 2022-12-20 op if (fcgi_puts(tp, proto) == -1 ||
1128 1abb18e1 2022-12-20 op fcgi_puts(tp, "://") == -1 ||
1129 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->server_name) == -1 ||
1130 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->document_uri) == -1)
1133 1abb18e1 2022-12-20 op return gotweb_render_url(c, url);
1136 b5c757f5 2022-09-01 stsp static struct got_repository *
1137 b5c757f5 2022-09-01 stsp find_cached_repo(struct server *srv, const char *path)
1141 b5c757f5 2022-09-01 stsp for (i = 0; i < srv->ncached_repos; i++) {
1142 b5c757f5 2022-09-01 stsp if (strcmp(srv->cached_repos[i].path, path) == 0)
1143 b5c757f5 2022-09-01 stsp return srv->cached_repos[i].repo;
1146 b5c757f5 2022-09-01 stsp return NULL;
1149 a596b957 2022-07-14 tracey static const struct got_error *
1150 b5c757f5 2022-09-01 stsp cache_repo(struct got_repository **new, struct server *srv,
1151 b5c757f5 2022-09-01 stsp struct repo_dir *repo_dir, struct socket *sock)
1153 b5c757f5 2022-09-01 stsp const struct got_error *error = NULL;
1154 b5c757f5 2022-09-01 stsp struct got_repository *repo;
1155 b5c757f5 2022-09-01 stsp struct cached_repo *cr;
1156 b5c757f5 2022-09-01 stsp int evicted = 0;
1158 7e0ec052 2022-09-06 op if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1159 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos - 1];
1160 b5c757f5 2022-09-01 stsp error = got_repo_close(cr->repo);
1161 b5c757f5 2022-09-01 stsp memset(cr, 0, sizeof(*cr));
1162 b5c757f5 2022-09-01 stsp srv->ncached_repos--;
1163 b5c757f5 2022-09-01 stsp if (error)
1164 b5c757f5 2022-09-01 stsp return error;
1165 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1166 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1167 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[0];
1168 b5c757f5 2022-09-01 stsp evicted = 1;
1170 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos];
1173 b5c757f5 2022-09-01 stsp error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1174 b5c757f5 2022-09-01 stsp if (error) {
1175 b5c757f5 2022-09-01 stsp if (evicted) {
1176 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1177 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1179 b5c757f5 2022-09-01 stsp return error;
1182 b5c757f5 2022-09-01 stsp if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1183 b5c757f5 2022-09-01 stsp >= sizeof(cr->path)) {
1184 b5c757f5 2022-09-01 stsp if (evicted) {
1185 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1186 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1188 b5c757f5 2022-09-01 stsp return got_error(GOT_ERR_NO_SPACE);
1191 b5c757f5 2022-09-01 stsp cr->repo = repo;
1192 b5c757f5 2022-09-01 stsp srv->ncached_repos++;
1193 b5c757f5 2022-09-01 stsp *new = repo;
1194 b5c757f5 2022-09-01 stsp return NULL;
1197 b5c757f5 2022-09-01 stsp static const struct got_error *
1198 a596b957 2022-07-14 tracey gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1200 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1201 a596b957 2022-07-14 tracey struct socket *sock = c->sock;
1202 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1203 a596b957 2022-07-14 tracey struct transport *t = c->t;
1204 b5c757f5 2022-09-01 stsp struct got_repository *repo = NULL;
1205 a596b957 2022-07-14 tracey DIR *dt;
1206 a596b957 2022-07-14 tracey char *dir_test;
1208 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1209 a596b957 2022-07-14 tracey GOTWEB_GIT_DIR) == -1)
1210 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1212 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1213 a596b957 2022-07-14 tracey if (dt == NULL) {
1214 a596b957 2022-07-14 tracey free(dir_test);
1215 a596b957 2022-07-14 tracey } else {
1216 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1217 a596b957 2022-07-14 tracey dir_test = NULL;
1221 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1222 0fad85dd 2022-09-01 op repo_dir->name) == -1)
1223 0fad85dd 2022-09-01 op return got_error_from_errno("asprintf");
1225 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1226 a596b957 2022-07-14 tracey if (dt == NULL) {
1227 a596b957 2022-07-14 tracey error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1228 a596b957 2022-07-14 tracey goto err;
1230 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1231 0fad85dd 2022-09-01 op dir_test = NULL;
1235 d5996b9e 2022-10-31 landry if (srv->respect_exportok &&
1236 d5996b9e 2022-10-31 landry faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1237 d5996b9e 2022-10-31 landry error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1238 d5996b9e 2022-10-31 landry goto err;
1241 b5c757f5 2022-09-01 stsp repo = find_cached_repo(srv, repo_dir->path);
1242 b5c757f5 2022-09-01 stsp if (repo == NULL) {
1243 b5c757f5 2022-09-01 stsp error = cache_repo(&repo, srv, repo_dir, sock);
1244 b5c757f5 2022-09-01 stsp if (error)
1247 b5c757f5 2022-09-01 stsp t->repo = repo;
1248 a596b957 2022-07-14 tracey error = gotweb_get_repo_description(&repo_dir->description, srv,
1249 3b81530f 2022-11-22 op repo_dir->path, dirfd(dt));
1250 a596b957 2022-07-14 tracey if (error)
1251 a596b957 2022-07-14 tracey goto err;
1252 c127fc49 2022-11-22 op error = got_get_repo_owner(&repo_dir->owner, c);
1253 a596b957 2022-07-14 tracey if (error)
1254 a596b957 2022-07-14 tracey goto err;
1255 cb93ab40 2023-01-22 op error = got_get_repo_age(&repo_dir->age, c, NULL);
1256 a596b957 2022-07-14 tracey if (error)
1257 a596b957 2022-07-14 tracey goto err;
1258 3b81530f 2022-11-22 op error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1261 a596b957 2022-07-14 tracey free(dir_test);
1262 0fad85dd 2022-09-01 op if (dt != NULL && closedir(dt) == EOF && error == NULL)
1263 0fad85dd 2022-09-01 op error = got_error_from_errno("closedir");
1264 a596b957 2022-07-14 tracey return error;
1267 a596b957 2022-07-14 tracey static const struct got_error *
1268 a596b957 2022-07-14 tracey gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1270 a596b957 2022-07-14 tracey const struct got_error *error;
1272 a596b957 2022-07-14 tracey *repo_dir = calloc(1, sizeof(**repo_dir));
1273 a596b957 2022-07-14 tracey if (*repo_dir == NULL)
1274 a596b957 2022-07-14 tracey return got_error_from_errno("calloc");
1276 a596b957 2022-07-14 tracey if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1277 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1278 a596b957 2022-07-14 tracey free(*repo_dir);
1279 a596b957 2022-07-14 tracey *repo_dir = NULL;
1280 a596b957 2022-07-14 tracey return error;
1282 a596b957 2022-07-14 tracey (*repo_dir)->owner = NULL;
1283 a596b957 2022-07-14 tracey (*repo_dir)->description = NULL;
1284 a596b957 2022-07-14 tracey (*repo_dir)->url = NULL;
1285 a596b957 2022-07-14 tracey (*repo_dir)->path = NULL;
1287 a596b957 2022-07-14 tracey return NULL;
1290 a596b957 2022-07-14 tracey static const struct got_error *
1291 3b81530f 2022-11-22 op gotweb_get_repo_description(char **description, struct server *srv,
1292 3b81530f 2022-11-22 op const char *dirpath, int dir)
1294 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1295 3b81530f 2022-11-22 op struct stat sb;
1296 3b81530f 2022-11-22 op int fd = -1;
1299 a596b957 2022-07-14 tracey *description = NULL;
1300 a596b957 2022-07-14 tracey if (srv->show_repo_description == 0)
1301 a596b957 2022-07-14 tracey return NULL;
1303 3b81530f 2022-11-22 op fd = openat(dir, "description", O_RDONLY);
1304 3b81530f 2022-11-22 op if (fd == -1) {
1305 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1306 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1307 3b81530f 2022-11-22 op dirpath, "description");
1309 a596b957 2022-07-14 tracey goto done;
1312 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1313 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1314 3b81530f 2022-11-22 op dirpath, "description");
1315 a596b957 2022-07-14 tracey goto done;
1318 3b81530f 2022-11-22 op len = sb.st_size;
1319 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXDESCRSZ - 1)
1320 270c41a2 2022-12-01 op len = GOTWEBD_MAXDESCRSZ - 1;
1322 a596b957 2022-07-14 tracey *description = calloc(len + 1, sizeof(**description));
1323 a596b957 2022-07-14 tracey if (*description == NULL) {
1324 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1325 a596b957 2022-07-14 tracey goto done;
1328 3b81530f 2022-11-22 op if (read(fd, *description, len) == -1)
1329 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1331 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1332 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1333 a596b957 2022-07-14 tracey return error;
1336 a596b957 2022-07-14 tracey static const struct got_error *
1337 3b81530f 2022-11-22 op gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1340 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1341 3b81530f 2022-11-22 op struct stat sb;
1342 3b81530f 2022-11-22 op int fd = -1;
1345 a596b957 2022-07-14 tracey *url = NULL;
1346 a596b957 2022-07-14 tracey if (srv->show_repo_cloneurl == 0)
1347 a596b957 2022-07-14 tracey return NULL;
1349 3b81530f 2022-11-22 op fd = openat(dir, "cloneurl", O_RDONLY);
1350 3b81530f 2022-11-22 op if (fd == -1) {
1351 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1352 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1353 3b81530f 2022-11-22 op dirpath, "cloneurl");
1355 a596b957 2022-07-14 tracey goto done;
1358 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1359 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1360 3b81530f 2022-11-22 op dirpath, "cloneurl");
1361 a596b957 2022-07-14 tracey goto done;
1364 3b81530f 2022-11-22 op len = sb.st_size;
1365 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1366 270c41a2 2022-12-01 op len = GOTWEBD_MAXCLONEURLSZ - 1;
1368 a596b957 2022-07-14 tracey *url = calloc(len + 1, sizeof(**url));
1369 a596b957 2022-07-14 tracey if (*url == NULL) {
1370 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1371 a596b957 2022-07-14 tracey goto done;
1374 3b81530f 2022-11-22 op if (read(fd, *url, len) == -1)
1375 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1377 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1378 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1379 a596b957 2022-07-14 tracey return error;
1383 cb93ab40 2023-01-22 op gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1385 cb93ab40 2023-01-22 op struct request *c = tp->tp_arg;
1386 a596b957 2022-07-14 tracey struct tm tm;
1387 fced5a66 2022-07-20 naddy long long diff_time;
1388 a596b957 2022-07-14 tracey const char *years = "years ago", *months = "months ago";
1389 a596b957 2022-07-14 tracey const char *weeks = "weeks ago", *days = "days ago";
1390 a596b957 2022-07-14 tracey const char *hours = "hours ago", *minutes = "minutes ago";
1391 a596b957 2022-07-14 tracey const char *seconds = "seconds ago", *now = "right now";
1392 a596b957 2022-07-14 tracey char *s;
1393 1abb18e1 2022-12-20 op char datebuf[64];
1396 a596b957 2022-07-14 tracey switch (ref_tm) {
1397 a596b957 2022-07-14 tracey case TM_DIFF:
1398 a596b957 2022-07-14 tracey diff_time = time(NULL) - committer_time;
1399 a596b957 2022-07-14 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
1400 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1401 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 365), years) == -1)
1403 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1404 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1405 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
1406 a596b957 2022-07-14 tracey months) == -1)
1408 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1409 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1410 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1412 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
1413 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1414 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24), days) == -1)
1416 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 2) {
1417 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s",
1418 a596b957 2022-07-14 tracey (diff_time / 60 / 60), hours) == -1)
1420 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 2) {
1421 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1422 a596b957 2022-07-14 tracey minutes) == -1)
1424 cb93ab40 2023-01-22 op } else if (diff_time > 2) {
1425 cb93ab40 2023-01-22 op if (fcgi_printf(c, "%lld %s", diff_time,
1426 a596b957 2022-07-14 tracey seconds) == -1)
1428 a596b957 2022-07-14 tracey } else {
1429 cb93ab40 2023-01-22 op if (fcgi_puts(tp, now) == -1)
1433 a596b957 2022-07-14 tracey case TM_LONG:
1434 a596b957 2022-07-14 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1437 a596b957 2022-07-14 tracey s = asctime_r(&tm, datebuf);
1438 a596b957 2022-07-14 tracey if (s == NULL)
1441 cb93ab40 2023-01-22 op if (fcgi_puts(tp, datebuf) == -1 ||
1442 cb93ab40 2023-01-22 op fcgi_puts(tp, " UTC") == -1)
1445 1abb18e1 2022-12-20 op case TM_RFC822:
1446 1abb18e1 2022-12-20 op if (gmtime_r(&committer_time, &tm) == NULL)
1449 1abb18e1 2022-12-20 op r = strftime(datebuf, sizeof(datebuf),
1450 1abb18e1 2022-12-20 op "%a, %d %b %Y %H:%M:%S GMT", &tm);
1454 cb93ab40 2023-01-22 op if (fcgi_puts(tp, datebuf) == -1)