Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <fcntl.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "proc.h"
52 #include "gotwebd.h"
53 #include "tmpl.h"
55 static const struct querystring_keys querystring_keys[] = {
56 { "action", ACTION },
57 { "commit", COMMIT },
58 { "file", RFILE },
59 { "folder", FOLDER },
60 { "headref", HEADREF },
61 { "index_page", INDEX_PAGE },
62 { "path", PATH },
63 { "page", PAGE },
64 };
66 static const struct action_keys action_keys[] = {
67 { "blame", BLAME },
68 { "blob", BLOB },
69 { "blobraw", BLOBRAW },
70 { "briefs", BRIEFS },
71 { "commits", COMMITS },
72 { "diff", DIFF },
73 { "error", ERR },
74 { "index", INDEX },
75 { "summary", SUMMARY },
76 { "tag", TAG },
77 { "tags", TAGS },
78 { "tree", TREE },
79 { "rss", RSS },
80 };
82 static const struct got_error *gotweb_init_querystring(struct querystring **);
83 static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 char *);
85 static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 char *, char *);
87 static const struct got_error *gotweb_render_index(struct request *);
88 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
89 const char *);
90 static const struct got_error *gotweb_load_got_path(struct request *c,
91 struct repo_dir *);
92 static const struct got_error *gotweb_get_repo_description(char **,
93 struct server *, const char *, int);
94 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
95 const char *, int);
97 static void gotweb_free_querystring(struct querystring *);
98 static void gotweb_free_repo_dir(struct repo_dir *);
100 struct server *gotweb_get_server(uint8_t *, uint8_t *);
102 static int
103 gotweb_reply(struct request *c, int status, const char *ctype,
104 struct gotweb_url *location)
106 const char *csp;
108 if (status != 200 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
109 return -1;
111 if (location) {
112 if (fcgi_puts(c->tp, "Location: ") == -1 ||
113 gotweb_render_url(c, location) == -1 ||
114 fcgi_puts(c->tp, "\r\n") == -1)
115 return -1;
118 csp = "Content-Security-Policy: default-src 'self'; "
119 "script-src 'none'; object-src 'none';\r\n";
120 if (fcgi_puts(c->tp, csp) == -1)
121 return -1;
123 if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
124 return -1;
126 return fcgi_puts(c->tp, "\r\n");
129 static int
130 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
131 const char *suffix)
133 int r;
135 r = fcgi_printf(c, "Content-Disposition: attachment; "
136 "filename=%s%s\r\n", file, suffix ? suffix : "");
137 if (r == -1)
138 return -1;
139 return gotweb_reply(c, 200, ctype, NULL);
142 void
143 gotweb_process_request(struct request *c)
145 const struct got_error *error = NULL, *error2 = NULL;
146 struct got_blob_object *blob = NULL;
147 struct server *srv = NULL;
148 struct querystring *qs = NULL;
149 struct repo_dir *repo_dir = NULL;
150 struct got_reflist_head refs;
151 FILE *fp = NULL;
152 uint8_t err[] = "gotwebd experienced an error: ";
153 int r, html = 0, fd = -1;
155 TAILQ_INIT(&refs);
157 /* init the transport */
158 error = gotweb_init_transport(&c->t);
159 if (error) {
160 log_warnx("%s: %s", __func__, error->msg);
161 return;
163 /* don't process any further if client disconnected */
164 if (c->sock->client_status == CLIENT_DISCONNECT)
165 return;
166 /* get the gotwebd server */
167 srv = gotweb_get_server(c->server_name, c->http_host);
168 if (srv == NULL) {
169 log_warnx("%s: error server is NULL", __func__);
170 goto err;
172 c->srv = srv;
173 /* parse our querystring */
174 error = gotweb_init_querystring(&qs);
175 if (error) {
176 log_warnx("%s: %s", __func__, error->msg);
177 goto err;
179 c->t->qs = qs;
180 error = gotweb_parse_querystring(&qs, c->querystring);
181 if (error) {
182 log_warnx("%s: %s", __func__, error->msg);
183 goto err;
186 /*
187 * certain actions require a commit id in the querystring. this stops
188 * bad actors from exploiting this by manually manipulating the
189 * querystring.
190 */
192 if (qs->action == BLAME || qs->action == BLOB ||
193 qs->action == BLOBRAW || qs->action == DIFF) {
194 if (qs->commit == NULL) {
195 error2 = got_error(GOT_ERR_QUERYSTRING);
196 goto render;
200 if (qs->action != INDEX) {
201 error = gotweb_init_repo_dir(&repo_dir, qs->path);
202 if (error)
203 goto done;
204 error = gotweb_load_got_path(c, repo_dir);
205 c->t->repo_dir = repo_dir;
206 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
207 goto err;
210 if (qs->action == BLOBRAW) {
211 const uint8_t *buf;
212 size_t len;
213 int binary, r;
215 error = got_get_repo_commits(c, 1);
216 if (error)
217 goto done;
219 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
220 if (error2)
221 goto render;
223 if (binary)
224 r = gotweb_reply_file(c, "application/octet-stream",
225 qs->file, NULL);
226 else
227 r = gotweb_reply(c, 200, "text/plain", NULL);
228 if (r == -1)
229 goto done;
231 for (;;) {
232 error = got_object_blob_read_block(&len, blob);
233 if (error)
234 goto done;
235 if (len == 0)
236 break;
237 buf = got_object_blob_get_read_buf(blob);
238 if (fcgi_gen_binary_response(c, buf, len) == -1)
239 goto done;
242 goto done;
245 if (qs->action == BLOB) {
246 int binary;
247 struct gotweb_url url = {
248 .index_page = -1,
249 .page = -1,
250 .action = BLOBRAW,
251 .path = qs->path,
252 .commit = qs->commit,
253 .folder = qs->folder,
254 .file = qs->file,
255 };
257 error = got_get_repo_commits(c, 1);
258 if (error)
259 goto done;
261 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
262 if (error2)
263 goto render;
264 if (binary) {
265 gotweb_reply(c, 302, NULL, &url);
266 goto done;
270 if (qs->action == RSS) {
271 const char *ctype = "application/rss+xml;charset=utf-8";
273 if (gotweb_reply_file(c, ctype, repo_dir->name, ".rss") == -1)
274 goto done;
276 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
277 if (error) {
278 log_warnx("%s: %s", __func__, error->msg);
279 goto err;
281 if (gotweb_render_rss(c->tp) == -1)
282 goto err;
283 goto done;
286 render:
287 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
288 goto done;
289 html = 1;
291 if (gotweb_render_header(c->tp) == -1)
292 goto err;
294 if (error2) {
295 error = error2;
296 goto err;
299 switch(qs->action) {
300 case BLAME:
301 error = got_get_repo_commits(c, 1);
302 if (error) {
303 log_warnx("%s: %s", __func__, error->msg);
304 goto err;
306 if (gotweb_render_blame(c->tp) == -1)
307 goto done;
308 break;
309 case BLOB:
310 if (gotweb_render_blob(c->tp, blob) == -1)
311 goto err;
312 break;
313 case BRIEFS:
314 if (gotweb_render_briefs(c->tp) == -1)
315 goto err;
316 break;
317 case COMMITS:
318 error = got_get_repo_commits(c, srv->max_commits_display);
319 if (error) {
320 log_warnx("%s: %s", __func__, error->msg);
321 goto err;
323 if (gotweb_render_commits(c->tp) == -1)
324 goto err;
325 break;
326 case DIFF:
327 error = got_get_repo_commits(c, 1);
328 if (error) {
329 log_warnx("%s: %s", __func__, error->msg);
330 goto err;
332 error = got_open_diff_for_output(&fp, &fd, c);
333 if (error) {
334 log_warnx("%s: %s", __func__, error->msg);
335 goto err;
337 if (gotweb_render_diff(c->tp, fp) == -1)
338 goto err;
339 break;
340 case INDEX:
341 error = gotweb_render_index(c);
342 if (error) {
343 log_warnx("%s: %s", __func__, error->msg);
344 goto err;
346 break;
347 case SUMMARY:
348 error = got_ref_list(&refs, c->t->repo, "refs/heads",
349 got_ref_cmp_by_name, NULL);
350 if (error) {
351 log_warnx("%s: got_ref_list: %s", __func__,
352 error->msg);
353 goto err;
355 qs->action = TAGS;
356 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
357 if (error) {
358 log_warnx("%s: got_get_repo_tags: %s", __func__,
359 error->msg);
360 goto err;
362 qs->action = SUMMARY;
363 if (gotweb_render_summary(c->tp, &refs) == -1)
364 goto done;
365 break;
366 case TAG:
367 error = got_get_repo_tags(c, 1);
368 if (error) {
369 log_warnx("%s: %s", __func__, error->msg);
370 goto err;
372 if (c->t->tag_count == 0) {
373 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
374 "bad commit id");
375 goto err;
377 if (gotweb_render_tag(c->tp) == -1)
378 goto done;
379 break;
380 case TAGS:
381 error = got_get_repo_tags(c, srv->max_commits_display);
382 if (error) {
383 log_warnx("%s: %s", __func__, error->msg);
384 goto err;
386 if (gotweb_render_tags(c->tp) == -1)
387 goto done;
388 break;
389 case TREE:
390 error = got_get_repo_commits(c, 1);
391 if (error) {
392 log_warnx("%s: %s", __func__, error->msg);
393 goto err;
395 if (gotweb_render_tree(c->tp) == -1)
396 goto err;
397 break;
398 case ERR:
399 default:
400 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
401 "Erorr: Bad Querystring");
402 if (r == -1)
403 goto err;
404 break;
407 goto done;
408 err:
409 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
410 return;
411 if (fcgi_printf(c, "\n%s", err) == -1)
412 return;
413 if (error) {
414 if (fcgi_printf(c, "%s", error->msg) == -1)
415 return;
416 } else {
417 if (fcgi_printf(c, "see daemon logs for details") == -1)
418 return;
420 if (html && fcgi_printf(c, "</div>\n") == -1)
421 return;
422 done:
423 if (blob)
424 got_object_blob_close(blob);
425 if (fp) {
426 error = got_gotweb_flushfile(fp, fd);
427 if (error)
428 log_warnx("%s: got_gotweb_flushfile failure: %s",
429 __func__, error->msg);
430 fd = -1;
432 if (fd != -1)
433 close(fd);
434 if (html && srv != NULL)
435 gotweb_render_footer(c->tp);
437 got_ref_list_free(&refs);
440 struct server *
441 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
443 struct server *srv = NULL;
445 /* check against the server name first */
446 if (strlen(server_name) > 0)
447 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
448 if (strcmp(srv->name, server_name) == 0)
449 goto done;
451 /* check against subdomain second */
452 if (strlen(subdomain) > 0)
453 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
454 if (strcmp(srv->name, subdomain) == 0)
455 goto done;
457 /* if those fail, send first server */
458 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
459 if (srv != NULL)
460 break;
461 done:
462 return srv;
463 };
465 const struct got_error *
466 gotweb_init_transport(struct transport **t)
468 const struct got_error *error = NULL;
470 *t = calloc(1, sizeof(**t));
471 if (*t == NULL)
472 return got_error_from_errno2("%s: calloc", __func__);
474 TAILQ_INIT(&(*t)->repo_commits);
475 TAILQ_INIT(&(*t)->repo_tags);
477 (*t)->repo = NULL;
478 (*t)->repo_dir = NULL;
479 (*t)->qs = NULL;
480 (*t)->next_id = NULL;
481 (*t)->prev_id = NULL;
482 (*t)->next_disp = 0;
483 (*t)->prev_disp = 0;
485 return error;
488 static const struct got_error *
489 gotweb_init_querystring(struct querystring **qs)
491 const struct got_error *error = NULL;
493 *qs = calloc(1, sizeof(**qs));
494 if (*qs == NULL)
495 return got_error_from_errno2("%s: calloc", __func__);
497 (*qs)->headref = strdup("HEAD");
498 if ((*qs)->headref == NULL) {
499 free(*qs);
500 *qs = NULL;
501 return got_error_from_errno2("%s: strdup", __func__);
504 (*qs)->action = INDEX;
505 (*qs)->commit = NULL;
506 (*qs)->file = NULL;
507 (*qs)->folder = NULL;
508 (*qs)->index_page = 0;
509 (*qs)->path = NULL;
511 return error;
514 static const struct got_error *
515 gotweb_parse_querystring(struct querystring **qs, char *qst)
517 const struct got_error *error = NULL;
518 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
519 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
521 if (qst == NULL)
522 return error;
524 tok1 = strdup(qst);
525 if (tok1 == NULL)
526 return got_error_from_errno2("%s: strdup", __func__);
528 tok1_pair = tok1;
529 tok1_end = tok1;
531 while (tok1_pair != NULL) {
532 strsep(&tok1_end, "&");
534 tok2 = strdup(tok1_pair);
535 if (tok2 == NULL) {
536 free(tok1);
537 return got_error_from_errno2("%s: strdup", __func__);
540 tok2_pair = tok2;
541 tok2_end = tok2;
543 while (tok2_pair != NULL) {
544 strsep(&tok2_end, "=");
545 if (tok2_end) {
546 error = gotweb_assign_querystring(qs, tok2_pair,
547 tok2_end);
548 if (error)
549 goto err;
551 tok2_pair = tok2_end;
553 free(tok2);
554 tok1_pair = tok1_end;
556 free(tok1);
557 return error;
558 err:
559 free(tok2);
560 free(tok1);
561 return error;
564 /*
565 * Adapted from usr.sbin/httpd/httpd.c url_decode.
566 */
567 static const struct got_error *
568 gotweb_urldecode(char *url)
570 char *p, *q;
571 char hex[3];
572 unsigned long x;
574 hex[2] = '\0';
575 p = q = url;
577 while (*p != '\0') {
578 switch (*p) {
579 case '%':
580 /* Encoding character is followed by two hex chars */
581 if (!isxdigit((unsigned char)p[1]) ||
582 !isxdigit((unsigned char)p[2]) ||
583 (p[1] == '0' && p[2] == '0'))
584 return got_error(GOT_ERR_BAD_QUERYSTRING);
586 hex[0] = p[1];
587 hex[1] = p[2];
589 /*
590 * We don't have to validate "hex" because it is
591 * guaranteed to include two hex chars followed by nul.
592 */
593 x = strtoul(hex, NULL, 16);
594 *q = (char)x;
595 p += 2;
596 break;
597 default:
598 *q = *p;
599 break;
601 p++;
602 q++;
604 *q = '\0';
606 return NULL;
609 static const struct got_error *
610 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
612 const struct got_error *error = NULL;
613 const char *errstr;
614 int a_cnt, el_cnt;
616 error = gotweb_urldecode(value);
617 if (error)
618 return error;
620 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
621 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
622 continue;
624 switch (querystring_keys[el_cnt].element) {
625 case ACTION:
626 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
627 if (strcmp(value, action_keys[a_cnt].name) != 0)
628 continue;
629 else if (strcmp(value,
630 action_keys[a_cnt].name) == 0){
631 (*qs)->action =
632 action_keys[a_cnt].action;
633 goto qa_found;
636 (*qs)->action = ERR;
637 qa_found:
638 break;
639 case COMMIT:
640 (*qs)->commit = strdup(value);
641 if ((*qs)->commit == NULL) {
642 error = got_error_from_errno2("%s: strdup",
643 __func__);
644 goto done;
646 break;
647 case RFILE:
648 (*qs)->file = strdup(value);
649 if ((*qs)->file == NULL) {
650 error = got_error_from_errno2("%s: strdup",
651 __func__);
652 goto done;
654 break;
655 case FOLDER:
656 (*qs)->folder = strdup(value);
657 if ((*qs)->folder == NULL) {
658 error = got_error_from_errno2("%s: strdup",
659 __func__);
660 goto done;
662 break;
663 case HEADREF:
664 free((*qs)->headref);
665 (*qs)->headref = strdup(value);
666 if ((*qs)->headref == NULL) {
667 error = got_error_from_errno2("%s: strdup",
668 __func__);
669 goto done;
671 break;
672 case INDEX_PAGE:
673 if (strlen(value) == 0)
674 break;
675 (*qs)->index_page = strtonum(value, INT64_MIN,
676 INT64_MAX, &errstr);
677 if (errstr) {
678 error = got_error_from_errno3("%s: strtonum %s",
679 __func__, errstr);
680 goto done;
682 if ((*qs)->index_page < 0)
683 (*qs)->index_page = 0;
684 break;
685 case PATH:
686 (*qs)->path = strdup(value);
687 if ((*qs)->path == NULL) {
688 error = got_error_from_errno2("%s: strdup",
689 __func__);
690 goto done;
692 break;
693 case PAGE:
694 if (strlen(value) == 0)
695 break;
696 (*qs)->page = strtonum(value, INT64_MIN,
697 INT64_MAX, &errstr);
698 if (errstr) {
699 error = got_error_from_errno3("%s: strtonum %s",
700 __func__, errstr);
701 goto done;
703 if ((*qs)->page < 0)
704 (*qs)->page = 0;
705 break;
706 default:
707 break;
710 done:
711 return error;
714 void
715 gotweb_free_repo_tag(struct repo_tag *rt)
717 if (rt != NULL) {
718 free(rt->commit_id);
719 free(rt->tag_name);
720 free(rt->tag_commit);
721 free(rt->commit_msg);
722 free(rt->tagger);
724 free(rt);
727 void
728 gotweb_free_repo_commit(struct repo_commit *rc)
730 if (rc != NULL) {
731 free(rc->path);
732 free(rc->refs_str);
733 free(rc->commit_id);
734 free(rc->parent_id);
735 free(rc->tree_id);
736 free(rc->author);
737 free(rc->committer);
738 free(rc->commit_msg);
740 free(rc);
743 static void
744 gotweb_free_querystring(struct querystring *qs)
746 if (qs != NULL) {
747 free(qs->commit);
748 free(qs->file);
749 free(qs->folder);
750 free(qs->headref);
751 free(qs->path);
753 free(qs);
756 static void
757 gotweb_free_repo_dir(struct repo_dir *repo_dir)
759 if (repo_dir != NULL) {
760 free(repo_dir->name);
761 free(repo_dir->owner);
762 free(repo_dir->description);
763 free(repo_dir->url);
764 free(repo_dir->path);
766 free(repo_dir);
769 void
770 gotweb_free_transport(struct transport *t)
772 struct repo_commit *rc = NULL, *trc = NULL;
773 struct repo_tag *rt = NULL, *trt = NULL;
775 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
776 TAILQ_REMOVE(&t->repo_commits, rc, entry);
777 gotweb_free_repo_commit(rc);
779 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
780 TAILQ_REMOVE(&t->repo_tags, rt, entry);
781 gotweb_free_repo_tag(rt);
783 gotweb_free_repo_dir(t->repo_dir);
784 gotweb_free_querystring(t->qs);
785 free(t->next_id);
786 free(t->prev_id);
787 free(t);
790 void
791 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
792 struct gotweb_url *next, int *have_next)
794 struct transport *t = c->t;
795 struct querystring *qs = t->qs;
796 struct server *srv = c->srv;
798 *have_prev = *have_next = 0;
800 switch(qs->action) {
801 case INDEX:
802 if (qs->index_page > 0) {
803 *have_prev = 1;
804 *prev = (struct gotweb_url){
805 .action = -1,
806 .index_page = qs->index_page - 1,
807 .page = -1,
808 };
810 if (t->next_disp == srv->max_repos_display &&
811 t->repos_total != (qs->index_page + 1) *
812 srv->max_repos_display) {
813 *have_next = 1;
814 *next = (struct gotweb_url){
815 .action = -1,
816 .index_page = qs->index_page + 1,
817 .page = -1,
818 };
820 break;
821 case BRIEFS:
822 if (t->prev_id && qs->commit != NULL &&
823 strcmp(qs->commit, t->prev_id) != 0) {
824 *have_prev = 1;
825 *prev = (struct gotweb_url){
826 .action = BRIEFS,
827 .index_page = -1,
828 .page = qs->page - 1,
829 .path = qs->path,
830 .commit = t->prev_id,
831 .headref = qs->headref,
832 };
834 if (t->next_id) {
835 *have_next = 1;
836 *next = (struct gotweb_url){
837 .action = BRIEFS,
838 .index_page = -1,
839 .page = qs->page + 1,
840 .path = qs->path,
841 .commit = t->next_id,
842 .headref = qs->headref,
843 };
845 break;
846 case COMMITS:
847 if (t->prev_id && qs->commit != NULL &&
848 strcmp(qs->commit, t->prev_id) != 0) {
849 *have_prev = 1;
850 *prev = (struct gotweb_url){
851 .action = COMMITS,
852 .index_page = -1,
853 .page = qs->page - 1,
854 .path = qs->path,
855 .commit = t->prev_id,
856 .headref = qs->headref,
857 .folder = qs->folder,
858 .file = qs->file,
859 };
861 if (t->next_id) {
862 *have_next = 1;
863 *next = (struct gotweb_url){
864 .action = COMMITS,
865 .index_page = -1,
866 .page = qs->page + 1,
867 .path = qs->path,
868 .commit = t->next_id,
869 .headref = qs->headref,
870 .folder = qs->folder,
871 .file = qs->file,
872 };
874 break;
875 case TAGS:
876 if (t->prev_id && qs->commit != NULL &&
877 strcmp(qs->commit, t->prev_id) != 0) {
878 *have_prev = 1;
879 *prev = (struct gotweb_url){
880 .action = TAGS,
881 .index_page = -1,
882 .page = qs->page - 1,
883 .path = qs->path,
884 .commit = t->prev_id,
885 .headref = qs->headref,
886 };
888 if (t->next_id) {
889 *have_next = 1;
890 *next = (struct gotweb_url){
891 .action = TAGS,
892 .index_page = -1,
893 .page = qs->page + 1,
894 .path = qs->path,
895 .commit = t->next_id,
896 .headref = qs->headref,
897 };
899 break;
903 static const struct got_error *
904 gotweb_render_index(struct request *c)
906 const struct got_error *error = NULL;
907 struct server *srv = c->srv;
908 struct transport *t = c->t;
909 struct querystring *qs = t->qs;
910 struct repo_dir *repo_dir = NULL;
911 DIR *d;
912 struct dirent **sd_dent = NULL;
913 unsigned int d_cnt, d_i, d_disp = 0;
914 unsigned int d_skipped = 0;
915 int type;
917 d = opendir(srv->repos_path);
918 if (d == NULL) {
919 error = got_error_from_errno2("opendir", srv->repos_path);
920 return error;
923 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
924 if (d_cnt == -1) {
925 sd_dent = NULL;
926 error = got_error_from_errno2("scandir", srv->repos_path);
927 goto done;
930 if (gotweb_render_repo_table_hdr(c->tp) == -1)
931 goto done;
933 for (d_i = 0; d_i < d_cnt; d_i++) {
934 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
935 break;
937 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
938 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
939 d_skipped++;
940 continue;
943 error = got_path_dirent_type(&type, srv->repos_path,
944 sd_dent[d_i]);
945 if (error)
946 goto done;
947 if (type != DT_DIR) {
948 d_skipped++;
949 continue;
952 if (qs->index_page > 0 && (qs->index_page *
953 srv->max_repos_display) > t->prev_disp) {
954 t->prev_disp++;
955 continue;
958 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
959 if (error)
960 goto done;
962 error = gotweb_load_got_path(c, repo_dir);
963 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
964 error = NULL;
965 gotweb_free_repo_dir(repo_dir);
966 repo_dir = NULL;
967 d_skipped++;
968 continue;
970 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
971 goto done;
973 d_disp++;
974 t->prev_disp++;
976 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
977 goto done;
979 gotweb_free_repo_dir(repo_dir);
980 repo_dir = NULL;
981 t->next_disp++;
982 if (d_disp == srv->max_repos_display)
983 break;
985 t->repos_total = d_cnt - d_skipped;
987 if (srv->max_repos_display == 0)
988 goto done;
989 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
990 goto done;
991 if (t->repos_total <= srv->max_repos ||
992 t->repos_total <= srv->max_repos_display)
993 goto done;
995 if (gotweb_render_navs(c->tp) == -1)
996 goto done;
997 done:
998 if (sd_dent) {
999 for (d_i = 0; d_i < d_cnt; d_i++)
1000 free(sd_dent[d_i]);
1001 free(sd_dent);
1003 if (d != NULL && closedir(d) == EOF && error == NULL)
1004 error = got_error_from_errno("closedir");
1005 return error;
1008 static inline int
1009 should_urlencode(int c)
1011 if (c <= ' ' || c >= 127)
1012 return 1;
1014 switch (c) {
1015 /* gen-delim */
1016 case ':':
1017 case '/':
1018 case '?':
1019 case '#':
1020 case '[':
1021 case ']':
1022 case '@':
1023 /* sub-delims */
1024 case '!':
1025 case '$':
1026 case '&':
1027 case '\'':
1028 case '(':
1029 case ')':
1030 case '*':
1031 case '+':
1032 case ',':
1033 case ';':
1034 case '=':
1035 /* needed because the URLs are embedded into the HTML */
1036 case '\"':
1037 return 1;
1038 default:
1039 return 0;
1043 static char *
1044 gotweb_urlencode(const char *str)
1046 const char *s;
1047 char *escaped;
1048 size_t i, len;
1049 int a, b;
1051 len = 0;
1052 for (s = str; *s; ++s) {
1053 len++;
1054 if (should_urlencode(*s))
1055 len += 2;
1058 escaped = calloc(1, len + 1);
1059 if (escaped == NULL)
1060 return NULL;
1062 i = 0;
1063 for (s = str; *s; ++s) {
1064 if (should_urlencode(*s)) {
1065 a = (*s & 0xF0) >> 4;
1066 b = (*s & 0x0F);
1068 escaped[i++] = '%';
1069 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1070 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1071 } else
1072 escaped[i++] = *s;
1075 return escaped;
1078 const char *
1079 gotweb_action_name(int action)
1081 switch (action) {
1082 case BLAME:
1083 return "blame";
1084 case BLOB:
1085 return "blob";
1086 case BLOBRAW:
1087 return "blobraw";
1088 case BRIEFS:
1089 return "briefs";
1090 case COMMITS:
1091 return "commits";
1092 case DIFF:
1093 return "diff";
1094 case ERR:
1095 return "err";
1096 case INDEX:
1097 return "index";
1098 case SUMMARY:
1099 return "summary";
1100 case TAG:
1101 return "tag";
1102 case TAGS:
1103 return "tags";
1104 case TREE:
1105 return "tree";
1106 case RSS:
1107 return "rss";
1108 default:
1109 return NULL;
1113 int
1114 gotweb_render_url(struct request *c, struct gotweb_url *url)
1116 const char *sep = "?", *action;
1117 char *tmp;
1118 int r;
1120 action = gotweb_action_name(url->action);
1121 if (action != NULL) {
1122 if (fcgi_printf(c, "?action=%s", action) == -1)
1123 return -1;
1124 sep = "&";
1127 if (url->commit) {
1128 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1129 return -1;
1130 sep = "&";
1133 if (url->previd) {
1134 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1135 return -1;
1136 sep = "&";
1139 if (url->prevset) {
1140 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1141 return -1;
1142 sep = "&";
1145 if (url->file) {
1146 tmp = gotweb_urlencode(url->file);
1147 if (tmp == NULL)
1148 return -1;
1149 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1150 free(tmp);
1151 if (r == -1)
1152 return -1;
1153 sep = "&";
1156 if (url->folder) {
1157 tmp = gotweb_urlencode(url->folder);
1158 if (tmp == NULL)
1159 return -1;
1160 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1161 free(tmp);
1162 if (r == -1)
1163 return -1;
1164 sep = "&";
1167 if (url->headref) {
1168 tmp = gotweb_urlencode(url->headref);
1169 if (tmp == NULL)
1170 return -1;
1171 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1172 free(tmp);
1173 if (r == -1)
1174 return -1;
1175 sep = "&";
1178 if (url->index_page != -1) {
1179 if (fcgi_printf(c, "%sindex_page=%d", sep,
1180 url->index_page) == -1)
1181 return -1;
1182 sep = "&";
1185 if (url->path) {
1186 tmp = gotweb_urlencode(url->path);
1187 if (tmp == NULL)
1188 return -1;
1189 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1190 free(tmp);
1191 if (r == -1)
1192 return -1;
1193 sep = "&";
1196 if (url->page != -1) {
1197 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1198 return -1;
1199 sep = "&";
1202 return 0;
1205 int
1206 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1208 struct template *tp = c->tp;
1209 const char *proto = c->https ? "https" : "http";
1211 if (fcgi_puts(tp, proto) == -1 ||
1212 fcgi_puts(tp, "://") == -1 ||
1213 tp_htmlescape(tp, c->server_name) == -1 ||
1214 tp_htmlescape(tp, c->document_uri) == -1)
1215 return -1;
1217 return gotweb_render_url(c, url);
1220 static struct got_repository *
1221 find_cached_repo(struct server *srv, const char *path)
1223 int i;
1225 for (i = 0; i < srv->ncached_repos; i++) {
1226 if (strcmp(srv->cached_repos[i].path, path) == 0)
1227 return srv->cached_repos[i].repo;
1230 return NULL;
1233 static const struct got_error *
1234 cache_repo(struct got_repository **new, struct server *srv,
1235 struct repo_dir *repo_dir, struct socket *sock)
1237 const struct got_error *error = NULL;
1238 struct got_repository *repo;
1239 struct cached_repo *cr;
1240 int evicted = 0;
1242 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1243 cr = &srv->cached_repos[srv->ncached_repos - 1];
1244 error = got_repo_close(cr->repo);
1245 memset(cr, 0, sizeof(*cr));
1246 srv->ncached_repos--;
1247 if (error)
1248 return error;
1249 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1250 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1251 cr = &srv->cached_repos[0];
1252 evicted = 1;
1253 } else {
1254 cr = &srv->cached_repos[srv->ncached_repos];
1257 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1258 if (error) {
1259 if (evicted) {
1260 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1261 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1263 return error;
1266 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1267 >= sizeof(cr->path)) {
1268 if (evicted) {
1269 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1270 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1272 return got_error(GOT_ERR_NO_SPACE);
1275 cr->repo = repo;
1276 srv->ncached_repos++;
1277 *new = repo;
1278 return NULL;
1281 static const struct got_error *
1282 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1284 const struct got_error *error = NULL;
1285 struct socket *sock = c->sock;
1286 struct server *srv = c->srv;
1287 struct transport *t = c->t;
1288 struct got_repository *repo = NULL;
1289 DIR *dt;
1290 char *dir_test;
1292 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1293 GOTWEB_GIT_DIR) == -1)
1294 return got_error_from_errno("asprintf");
1296 dt = opendir(dir_test);
1297 if (dt == NULL) {
1298 free(dir_test);
1299 } else {
1300 repo_dir->path = dir_test;
1301 dir_test = NULL;
1302 goto done;
1305 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1306 repo_dir->name) == -1)
1307 return got_error_from_errno("asprintf");
1309 dt = opendir(dir_test);
1310 if (dt == NULL) {
1311 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1312 goto err;
1313 } else {
1314 repo_dir->path = dir_test;
1315 dir_test = NULL;
1318 done:
1319 if (srv->respect_exportok &&
1320 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1321 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1322 goto err;
1325 repo = find_cached_repo(srv, repo_dir->path);
1326 if (repo == NULL) {
1327 error = cache_repo(&repo, srv, repo_dir, sock);
1328 if (error)
1329 goto err;
1331 t->repo = repo;
1332 error = gotweb_get_repo_description(&repo_dir->description, srv,
1333 repo_dir->path, dirfd(dt));
1334 if (error)
1335 goto err;
1336 error = got_get_repo_owner(&repo_dir->owner, c);
1337 if (error)
1338 goto err;
1339 error = got_get_repo_age(&repo_dir->age, c, NULL);
1340 if (error)
1341 goto err;
1342 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1343 dirfd(dt));
1344 err:
1345 free(dir_test);
1346 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1347 error = got_error_from_errno("closedir");
1348 return error;
1351 static const struct got_error *
1352 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1354 const struct got_error *error;
1356 *repo_dir = calloc(1, sizeof(**repo_dir));
1357 if (*repo_dir == NULL)
1358 return got_error_from_errno("calloc");
1360 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1361 error = got_error_from_errno("asprintf");
1362 free(*repo_dir);
1363 *repo_dir = NULL;
1364 return error;
1366 (*repo_dir)->owner = NULL;
1367 (*repo_dir)->description = NULL;
1368 (*repo_dir)->url = NULL;
1369 (*repo_dir)->path = NULL;
1371 return NULL;
1374 static const struct got_error *
1375 gotweb_get_repo_description(char **description, struct server *srv,
1376 const char *dirpath, int dir)
1378 const struct got_error *error = NULL;
1379 struct stat sb;
1380 int fd = -1;
1381 off_t len;
1383 *description = NULL;
1384 if (srv->show_repo_description == 0)
1385 return NULL;
1387 fd = openat(dir, "description", O_RDONLY);
1388 if (fd == -1) {
1389 if (errno != ENOENT && errno != EACCES) {
1390 error = got_error_from_errno_fmt("openat %s/%s",
1391 dirpath, "description");
1393 goto done;
1396 if (fstat(fd, &sb) == -1) {
1397 error = got_error_from_errno_fmt("fstat %s/%s",
1398 dirpath, "description");
1399 goto done;
1402 len = sb.st_size;
1403 if (len > GOTWEBD_MAXDESCRSZ - 1)
1404 len = GOTWEBD_MAXDESCRSZ - 1;
1406 *description = calloc(len + 1, sizeof(**description));
1407 if (*description == NULL) {
1408 error = got_error_from_errno("calloc");
1409 goto done;
1412 if (read(fd, *description, len) == -1)
1413 error = got_error_from_errno("read");
1414 done:
1415 if (fd != -1 && close(fd) == -1 && error == NULL)
1416 error = got_error_from_errno("close");
1417 return error;
1420 static const struct got_error *
1421 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1422 int dir)
1424 const struct got_error *error = NULL;
1425 struct stat sb;
1426 int fd = -1;
1427 off_t len;
1429 *url = NULL;
1430 if (srv->show_repo_cloneurl == 0)
1431 return NULL;
1433 fd = openat(dir, "cloneurl", O_RDONLY);
1434 if (fd == -1) {
1435 if (errno != ENOENT && errno != EACCES) {
1436 error = got_error_from_errno_fmt("openat %s/%s",
1437 dirpath, "cloneurl");
1439 goto done;
1442 if (fstat(fd, &sb) == -1) {
1443 error = got_error_from_errno_fmt("fstat %s/%s",
1444 dirpath, "cloneurl");
1445 goto done;
1448 len = sb.st_size;
1449 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1450 len = GOTWEBD_MAXCLONEURLSZ - 1;
1452 *url = calloc(len + 1, sizeof(**url));
1453 if (*url == NULL) {
1454 error = got_error_from_errno("calloc");
1455 goto done;
1458 if (read(fd, *url, len) == -1)
1459 error = got_error_from_errno("read");
1460 done:
1461 if (fd != -1 && close(fd) == -1 && error == NULL)
1462 error = got_error_from_errno("close");
1463 return error;
1466 int
1467 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1469 struct request *c = tp->tp_arg;
1470 struct tm tm;
1471 long long diff_time;
1472 const char *years = "years ago", *months = "months ago";
1473 const char *weeks = "weeks ago", *days = "days ago";
1474 const char *hours = "hours ago", *minutes = "minutes ago";
1475 const char *seconds = "seconds ago", *now = "right now";
1476 char *s;
1477 char datebuf[64];
1478 size_t r;
1480 switch (ref_tm) {
1481 case TM_DIFF:
1482 diff_time = time(NULL) - committer_time;
1483 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1484 if (fcgi_printf(c, "%lld %s",
1485 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1486 return -1;
1487 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1488 if (fcgi_printf(c, "%lld %s",
1489 (diff_time / 60 / 60 / 24 / (365 / 12)),
1490 months) == -1)
1491 return -1;
1492 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1493 if (fcgi_printf(c, "%lld %s",
1494 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1495 return -1;
1496 } else if (diff_time > 60 * 60 * 24 * 2) {
1497 if (fcgi_printf(c, "%lld %s",
1498 (diff_time / 60 / 60 / 24), days) == -1)
1499 return -1;
1500 } else if (diff_time > 60 * 60 * 2) {
1501 if (fcgi_printf(c, "%lld %s",
1502 (diff_time / 60 / 60), hours) == -1)
1503 return -1;
1504 } else if (diff_time > 60 * 2) {
1505 if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1506 minutes) == -1)
1507 return -1;
1508 } else if (diff_time > 2) {
1509 if (fcgi_printf(c, "%lld %s", diff_time,
1510 seconds) == -1)
1511 return -1;
1512 } else {
1513 if (fcgi_puts(tp, now) == -1)
1514 return -1;
1516 break;
1517 case TM_LONG:
1518 if (gmtime_r(&committer_time, &tm) == NULL)
1519 return -1;
1521 s = asctime_r(&tm, datebuf);
1522 if (s == NULL)
1523 return -1;
1525 if (fcgi_puts(tp, datebuf) == -1 ||
1526 fcgi_puts(tp, " UTC") == -1)
1527 return -1;
1528 break;
1529 case TM_RFC822:
1530 if (gmtime_r(&committer_time, &tm) == NULL)
1531 return -1;
1533 r = strftime(datebuf, sizeof(datebuf),
1534 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1535 if (r == 0)
1536 return -1;
1538 if (fcgi_puts(tp, datebuf) == -1)
1539 return -1;
1540 break;
1542 return 0;