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);
96 static const struct got_error *gotweb_render_blame(struct request *);
98 static void gotweb_free_querystring(struct querystring *);
99 static void gotweb_free_repo_dir(struct repo_dir *);
101 struct server *gotweb_get_server(uint8_t *, uint8_t *);
103 void
104 gotweb_process_request(struct request *c)
106 const struct got_error *error = NULL, *error2 = NULL;
107 struct got_blob_object *blob = NULL;
108 struct server *srv = NULL;
109 struct querystring *qs = NULL;
110 struct repo_dir *repo_dir = NULL;
111 struct got_reflist_head refs;
112 FILE *fp = NULL;
113 uint8_t err[] = "gotwebd experienced an error: ";
114 int r, html = 0, fd = -1;
116 TAILQ_INIT(&refs);
118 /* init the transport */
119 error = gotweb_init_transport(&c->t);
120 if (error) {
121 log_warnx("%s: %s", __func__, error->msg);
122 return;
124 /* don't process any further if client disconnected */
125 if (c->sock->client_status == CLIENT_DISCONNECT)
126 return;
127 /* get the gotwebd server */
128 srv = gotweb_get_server(c->server_name, c->http_host);
129 if (srv == NULL) {
130 log_warnx("%s: error server is NULL", __func__);
131 goto err;
133 c->srv = srv;
134 /* parse our querystring */
135 error = gotweb_init_querystring(&qs);
136 if (error) {
137 log_warnx("%s: %s", __func__, error->msg);
138 goto err;
140 c->t->qs = qs;
141 error = gotweb_parse_querystring(&qs, c->querystring);
142 if (error) {
143 log_warnx("%s: %s", __func__, error->msg);
144 goto err;
147 /*
148 * certain actions require a commit id in the querystring. this stops
149 * bad actors from exploiting this by manually manipulating the
150 * querystring.
151 */
153 if (qs->action == BLAME || qs->action == BLOB ||
154 qs->action == BLOBRAW || qs->action == DIFF) {
155 if (qs->commit == NULL) {
156 error2 = got_error(GOT_ERR_QUERYSTRING);
157 goto render;
161 if (qs->action != INDEX) {
162 error = gotweb_init_repo_dir(&repo_dir, qs->path);
163 if (error)
164 goto done;
165 error = gotweb_load_got_path(c, repo_dir);
166 c->t->repo_dir = repo_dir;
167 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
168 goto err;
171 if (qs->action == BLOBRAW) {
172 error = got_get_repo_commits(c, 1);
173 if (error)
174 goto done;
175 error = got_output_file_blob(c);
176 if (error) {
177 log_warnx("%s: %s", __func__, error->msg);
178 goto err;
180 goto done;
183 if (qs->action == BLOB) {
184 int binary;
185 struct gotweb_url url = {
186 .index_page = -1,
187 .page = -1,
188 .action = BLOBRAW,
189 .path = qs->path,
190 .commit = qs->commit,
191 .folder = qs->folder,
192 .file = qs->file,
193 };
195 error = got_get_repo_commits(c, 1);
196 if (error)
197 goto done;
199 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
200 if (error2)
201 goto render;
202 if (binary) {
203 fcgi_puts(c->tp, "Status: 302\r\n");
204 fcgi_puts(c->tp, "Location: ");
205 gotweb_render_url(c, &url);
206 fcgi_puts(c->tp, "\r\n\r\n");
207 goto done;
211 if (qs->action == RSS) {
212 error = gotweb_render_content_type_file(c,
213 "application/rss+xml;charset=utf-8",
214 repo_dir->name, ".rss");
215 if (error) {
216 log_warnx("%s: %s", __func__, error->msg);
217 goto err;
220 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
221 if (error) {
222 log_warnx("%s: %s", __func__, error->msg);
223 goto err;
225 if (gotweb_render_rss(c->tp) == -1)
226 goto err;
227 goto done;
230 render:
231 error = gotweb_render_content_type(c, "text/html");
232 if (error) {
233 log_warnx("%s: %s", __func__, error->msg);
234 goto err;
236 html = 1;
238 if (gotweb_render_header(c->tp) == -1)
239 goto err;
241 if (error2) {
242 error = error2;
243 goto err;
246 switch(qs->action) {
247 case BLAME:
248 error = gotweb_render_blame(c);
249 if (error) {
250 log_warnx("%s: %s", __func__, error->msg);
251 goto err;
253 break;
254 case BLOB:
255 if (gotweb_render_blob(c->tp, blob) == -1)
256 goto err;
257 break;
258 case BRIEFS:
259 if (gotweb_render_briefs(c->tp) == -1)
260 goto err;
261 break;
262 case COMMITS:
263 error = got_get_repo_commits(c, srv->max_commits_display);
264 if (error) {
265 log_warnx("%s: %s", __func__, error->msg);
266 goto err;
268 if (gotweb_render_commits(c->tp) == -1)
269 goto err;
270 break;
271 case DIFF:
272 error = got_get_repo_commits(c, 1);
273 if (error) {
274 log_warnx("%s: %s", __func__, error->msg);
275 goto err;
277 error = got_open_diff_for_output(&fp, &fd, c);
278 if (error) {
279 log_warnx("%s: %s", __func__, error->msg);
280 goto err;
282 if (gotweb_render_diff(c->tp, fp) == -1)
283 goto err;
284 break;
285 case INDEX:
286 error = gotweb_render_index(c);
287 if (error) {
288 log_warnx("%s: %s", __func__, error->msg);
289 goto err;
291 break;
292 case SUMMARY:
293 error = got_ref_list(&refs, c->t->repo, "refs/heads",
294 got_ref_cmp_by_name, NULL);
295 if (error) {
296 log_warnx("%s: got_ref_list: %s", __func__,
297 error->msg);
298 goto err;
300 qs->action = TAGS;
301 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
302 if (error) {
303 log_warnx("%s: got_get_repo_tags: %s", __func__,
304 error->msg);
305 goto err;
307 qs->action = SUMMARY;
308 if (gotweb_render_summary(c->tp, &refs) == -1)
309 goto done;
310 break;
311 case TAG:
312 error = got_get_repo_tags(c, 1);
313 if (error) {
314 log_warnx("%s: %s", __func__, error->msg);
315 goto err;
317 if (c->t->tag_count == 0) {
318 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
319 "bad commit id");
320 goto err;
322 if (gotweb_render_tag(c->tp) == -1)
323 goto done;
324 break;
325 case TAGS:
326 error = got_get_repo_tags(c, srv->max_commits_display);
327 if (error) {
328 log_warnx("%s: %s", __func__, error->msg);
329 goto err;
331 if (gotweb_render_tags(c->tp) == -1)
332 goto done;
333 break;
334 case TREE:
335 error = got_get_repo_commits(c, 1);
336 if (error) {
337 log_warnx("%s: %s", __func__, error->msg);
338 goto err;
340 if (gotweb_render_tree(c->tp) == -1)
341 goto err;
342 break;
343 case ERR:
344 default:
345 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
346 "Erorr: Bad Querystring");
347 if (r == -1)
348 goto err;
349 break;
352 goto done;
353 err:
354 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
355 return;
356 if (fcgi_printf(c, "\n%s", err) == -1)
357 return;
358 if (error) {
359 if (fcgi_printf(c, "%s", error->msg) == -1)
360 return;
361 } else {
362 if (fcgi_printf(c, "see daemon logs for details") == -1)
363 return;
365 if (html && fcgi_printf(c, "</div>\n") == -1)
366 return;
367 done:
368 if (blob)
369 got_object_blob_close(blob);
370 if (fp) {
371 error = got_gotweb_flushfile(fp, fd);
372 if (error)
373 log_warnx("%s: got_gotweb_flushfile failure: %s",
374 __func__, error->msg);
375 fd = -1;
377 if (fd != -1)
378 close(fd);
379 if (html && srv != NULL)
380 gotweb_render_footer(c->tp);
382 got_ref_list_free(&refs);
385 struct server *
386 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
388 struct server *srv = NULL;
390 /* check against the server name first */
391 if (strlen(server_name) > 0)
392 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
393 if (strcmp(srv->name, server_name) == 0)
394 goto done;
396 /* check against subdomain second */
397 if (strlen(subdomain) > 0)
398 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
399 if (strcmp(srv->name, subdomain) == 0)
400 goto done;
402 /* if those fail, send first server */
403 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
404 if (srv != NULL)
405 break;
406 done:
407 return srv;
408 };
410 const struct got_error *
411 gotweb_init_transport(struct transport **t)
413 const struct got_error *error = NULL;
415 *t = calloc(1, sizeof(**t));
416 if (*t == NULL)
417 return got_error_from_errno2("%s: calloc", __func__);
419 TAILQ_INIT(&(*t)->repo_commits);
420 TAILQ_INIT(&(*t)->repo_tags);
422 (*t)->repo = NULL;
423 (*t)->repo_dir = NULL;
424 (*t)->qs = NULL;
425 (*t)->next_id = NULL;
426 (*t)->prev_id = NULL;
427 (*t)->next_disp = 0;
428 (*t)->prev_disp = 0;
430 return error;
433 static const struct got_error *
434 gotweb_init_querystring(struct querystring **qs)
436 const struct got_error *error = NULL;
438 *qs = calloc(1, sizeof(**qs));
439 if (*qs == NULL)
440 return got_error_from_errno2("%s: calloc", __func__);
442 (*qs)->headref = strdup("HEAD");
443 if ((*qs)->headref == NULL) {
444 free(*qs);
445 *qs = NULL;
446 return got_error_from_errno2("%s: strdup", __func__);
449 (*qs)->action = INDEX;
450 (*qs)->commit = NULL;
451 (*qs)->file = NULL;
452 (*qs)->folder = NULL;
453 (*qs)->index_page = 0;
454 (*qs)->path = NULL;
456 return error;
459 static const struct got_error *
460 gotweb_parse_querystring(struct querystring **qs, char *qst)
462 const struct got_error *error = NULL;
463 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
464 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
466 if (qst == NULL)
467 return error;
469 tok1 = strdup(qst);
470 if (tok1 == NULL)
471 return got_error_from_errno2("%s: strdup", __func__);
473 tok1_pair = tok1;
474 tok1_end = tok1;
476 while (tok1_pair != NULL) {
477 strsep(&tok1_end, "&");
479 tok2 = strdup(tok1_pair);
480 if (tok2 == NULL) {
481 free(tok1);
482 return got_error_from_errno2("%s: strdup", __func__);
485 tok2_pair = tok2;
486 tok2_end = tok2;
488 while (tok2_pair != NULL) {
489 strsep(&tok2_end, "=");
490 if (tok2_end) {
491 error = gotweb_assign_querystring(qs, tok2_pair,
492 tok2_end);
493 if (error)
494 goto err;
496 tok2_pair = tok2_end;
498 free(tok2);
499 tok1_pair = tok1_end;
501 free(tok1);
502 return error;
503 err:
504 free(tok2);
505 free(tok1);
506 return error;
509 /*
510 * Adapted from usr.sbin/httpd/httpd.c url_decode.
511 */
512 static const struct got_error *
513 gotweb_urldecode(char *url)
515 char *p, *q;
516 char hex[3];
517 unsigned long x;
519 hex[2] = '\0';
520 p = q = url;
522 while (*p != '\0') {
523 switch (*p) {
524 case '%':
525 /* Encoding character is followed by two hex chars */
526 if (!isxdigit((unsigned char)p[1]) ||
527 !isxdigit((unsigned char)p[2]) ||
528 (p[1] == '0' && p[2] == '0'))
529 return got_error(GOT_ERR_BAD_QUERYSTRING);
531 hex[0] = p[1];
532 hex[1] = p[2];
534 /*
535 * We don't have to validate "hex" because it is
536 * guaranteed to include two hex chars followed by nul.
537 */
538 x = strtoul(hex, NULL, 16);
539 *q = (char)x;
540 p += 2;
541 break;
542 default:
543 *q = *p;
544 break;
546 p++;
547 q++;
549 *q = '\0';
551 return NULL;
554 static const struct got_error *
555 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
557 const struct got_error *error = NULL;
558 const char *errstr;
559 int a_cnt, el_cnt;
561 error = gotweb_urldecode(value);
562 if (error)
563 return error;
565 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
566 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
567 continue;
569 switch (querystring_keys[el_cnt].element) {
570 case ACTION:
571 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
572 if (strcmp(value, action_keys[a_cnt].name) != 0)
573 continue;
574 else if (strcmp(value,
575 action_keys[a_cnt].name) == 0){
576 (*qs)->action =
577 action_keys[a_cnt].action;
578 goto qa_found;
581 (*qs)->action = ERR;
582 qa_found:
583 break;
584 case COMMIT:
585 (*qs)->commit = strdup(value);
586 if ((*qs)->commit == NULL) {
587 error = got_error_from_errno2("%s: strdup",
588 __func__);
589 goto done;
591 break;
592 case RFILE:
593 (*qs)->file = strdup(value);
594 if ((*qs)->file == NULL) {
595 error = got_error_from_errno2("%s: strdup",
596 __func__);
597 goto done;
599 break;
600 case FOLDER:
601 (*qs)->folder = strdup(value);
602 if ((*qs)->folder == NULL) {
603 error = got_error_from_errno2("%s: strdup",
604 __func__);
605 goto done;
607 break;
608 case HEADREF:
609 free((*qs)->headref);
610 (*qs)->headref = strdup(value);
611 if ((*qs)->headref == NULL) {
612 error = got_error_from_errno2("%s: strdup",
613 __func__);
614 goto done;
616 break;
617 case INDEX_PAGE:
618 if (strlen(value) == 0)
619 break;
620 (*qs)->index_page = strtonum(value, INT64_MIN,
621 INT64_MAX, &errstr);
622 if (errstr) {
623 error = got_error_from_errno3("%s: strtonum %s",
624 __func__, errstr);
625 goto done;
627 if ((*qs)->index_page < 0)
628 (*qs)->index_page = 0;
629 break;
630 case PATH:
631 (*qs)->path = strdup(value);
632 if ((*qs)->path == NULL) {
633 error = got_error_from_errno2("%s: strdup",
634 __func__);
635 goto done;
637 break;
638 case PAGE:
639 if (strlen(value) == 0)
640 break;
641 (*qs)->page = strtonum(value, INT64_MIN,
642 INT64_MAX, &errstr);
643 if (errstr) {
644 error = got_error_from_errno3("%s: strtonum %s",
645 __func__, errstr);
646 goto done;
648 if ((*qs)->page < 0)
649 (*qs)->page = 0;
650 break;
651 default:
652 break;
655 done:
656 return error;
659 void
660 gotweb_free_repo_tag(struct repo_tag *rt)
662 if (rt != NULL) {
663 free(rt->commit_id);
664 free(rt->tag_name);
665 free(rt->tag_commit);
666 free(rt->commit_msg);
667 free(rt->tagger);
669 free(rt);
672 void
673 gotweb_free_repo_commit(struct repo_commit *rc)
675 if (rc != NULL) {
676 free(rc->path);
677 free(rc->refs_str);
678 free(rc->commit_id);
679 free(rc->parent_id);
680 free(rc->tree_id);
681 free(rc->author);
682 free(rc->committer);
683 free(rc->commit_msg);
685 free(rc);
688 static void
689 gotweb_free_querystring(struct querystring *qs)
691 if (qs != NULL) {
692 free(qs->commit);
693 free(qs->file);
694 free(qs->folder);
695 free(qs->headref);
696 free(qs->path);
698 free(qs);
701 static void
702 gotweb_free_repo_dir(struct repo_dir *repo_dir)
704 if (repo_dir != NULL) {
705 free(repo_dir->name);
706 free(repo_dir->owner);
707 free(repo_dir->description);
708 free(repo_dir->url);
709 free(repo_dir->age);
710 free(repo_dir->path);
712 free(repo_dir);
715 void
716 gotweb_free_transport(struct transport *t)
718 struct repo_commit *rc = NULL, *trc = NULL;
719 struct repo_tag *rt = NULL, *trt = NULL;
721 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
722 TAILQ_REMOVE(&t->repo_commits, rc, entry);
723 gotweb_free_repo_commit(rc);
725 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
726 TAILQ_REMOVE(&t->repo_tags, rt, entry);
727 gotweb_free_repo_tag(rt);
729 gotweb_free_repo_dir(t->repo_dir);
730 gotweb_free_querystring(t->qs);
731 free(t->next_id);
732 free(t->prev_id);
733 free(t);
736 const struct got_error *
737 gotweb_render_content_type(struct request *c, const char *type)
739 const char *csp = "default-src 'self'; script-src 'none'; "
740 "object-src 'none';";
742 fcgi_printf(c,
743 "Content-Security-Policy: %s\r\n"
744 "Content-Type: %s\r\n\r\n",
745 csp, type);
746 return NULL;
749 const struct got_error *
750 gotweb_render_content_type_file(struct request *c, const char *type,
751 const char *file, const char *suffix)
753 fcgi_printf(c, "Content-type: %s\r\n"
754 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
755 type, file, suffix ? suffix : "");
756 return NULL;
759 void
760 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
761 struct gotweb_url *next, int *have_next)
763 struct transport *t = c->t;
764 struct querystring *qs = t->qs;
765 struct server *srv = c->srv;
767 *have_prev = *have_next = 0;
769 switch(qs->action) {
770 case INDEX:
771 if (qs->index_page > 0) {
772 *have_prev = 1;
773 *prev = (struct gotweb_url){
774 .action = -1,
775 .index_page = qs->index_page - 1,
776 .page = -1,
777 };
779 if (t->next_disp == srv->max_repos_display &&
780 t->repos_total != (qs->index_page + 1) *
781 srv->max_repos_display) {
782 *have_next = 1;
783 *next = (struct gotweb_url){
784 .action = -1,
785 .index_page = qs->index_page + 1,
786 .page = -1,
787 };
789 break;
790 case BRIEFS:
791 if (t->prev_id && qs->commit != NULL &&
792 strcmp(qs->commit, t->prev_id) != 0) {
793 *have_prev = 1;
794 *prev = (struct gotweb_url){
795 .action = BRIEFS,
796 .index_page = -1,
797 .page = qs->page - 1,
798 .path = qs->path,
799 .commit = t->prev_id,
800 .headref = qs->headref,
801 };
803 if (t->next_id) {
804 *have_next = 1;
805 *next = (struct gotweb_url){
806 .action = BRIEFS,
807 .index_page = -1,
808 .page = qs->page + 1,
809 .path = qs->path,
810 .commit = t->next_id,
811 .headref = qs->headref,
812 };
814 break;
815 case COMMITS:
816 if (t->prev_id && qs->commit != NULL &&
817 strcmp(qs->commit, t->prev_id) != 0) {
818 *have_prev = 1;
819 *prev = (struct gotweb_url){
820 .action = COMMITS,
821 .index_page = -1,
822 .page = qs->page - 1,
823 .path = qs->path,
824 .commit = t->prev_id,
825 .headref = qs->headref,
826 .folder = qs->folder,
827 .file = qs->file,
828 };
830 if (t->next_id) {
831 *have_next = 1;
832 *next = (struct gotweb_url){
833 .action = COMMITS,
834 .index_page = -1,
835 .page = qs->page + 1,
836 .path = qs->path,
837 .commit = t->next_id,
838 .headref = qs->headref,
839 .folder = qs->folder,
840 .file = qs->file,
841 };
843 break;
844 case TAGS:
845 if (t->prev_id && qs->commit != NULL &&
846 strcmp(qs->commit, t->prev_id) != 0) {
847 *have_prev = 1;
848 *prev = (struct gotweb_url){
849 .action = TAGS,
850 .index_page = -1,
851 .page = qs->page - 1,
852 .path = qs->path,
853 .commit = t->prev_id,
854 .headref = qs->headref,
855 };
857 if (t->next_id) {
858 *have_next = 1;
859 *next = (struct gotweb_url){
860 .action = TAGS,
861 .index_page = -1,
862 .page = qs->page + 1,
863 .path = qs->path,
864 .commit = t->next_id,
865 .headref = qs->headref,
866 };
868 break;
872 static const struct got_error *
873 gotweb_render_index(struct request *c)
875 const struct got_error *error = NULL;
876 struct server *srv = c->srv;
877 struct transport *t = c->t;
878 struct querystring *qs = t->qs;
879 struct repo_dir *repo_dir = NULL;
880 DIR *d;
881 struct dirent **sd_dent = NULL;
882 unsigned int d_cnt, d_i, d_disp = 0;
883 unsigned int d_skipped = 0;
884 int type;
886 d = opendir(srv->repos_path);
887 if (d == NULL) {
888 error = got_error_from_errno2("opendir", srv->repos_path);
889 return error;
892 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
893 if (d_cnt == -1) {
894 sd_dent = NULL;
895 error = got_error_from_errno2("scandir", srv->repos_path);
896 goto done;
899 if (gotweb_render_repo_table_hdr(c->tp) == -1)
900 goto done;
902 for (d_i = 0; d_i < d_cnt; d_i++) {
903 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
904 break;
906 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
907 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
908 d_skipped++;
909 continue;
912 error = got_path_dirent_type(&type, srv->repos_path,
913 sd_dent[d_i]);
914 if (error)
915 goto done;
916 if (type != DT_DIR) {
917 d_skipped++;
918 continue;
921 if (qs->index_page > 0 && (qs->index_page *
922 srv->max_repos_display) > t->prev_disp) {
923 t->prev_disp++;
924 continue;
927 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
928 if (error)
929 goto done;
931 error = gotweb_load_got_path(c, repo_dir);
932 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
933 error = NULL;
934 gotweb_free_repo_dir(repo_dir);
935 repo_dir = NULL;
936 d_skipped++;
937 continue;
939 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
940 goto done;
942 d_disp++;
943 t->prev_disp++;
945 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
946 goto done;
948 gotweb_free_repo_dir(repo_dir);
949 repo_dir = NULL;
950 t->next_disp++;
951 if (d_disp == srv->max_repos_display)
952 break;
954 t->repos_total = d_cnt - d_skipped;
956 if (srv->max_repos_display == 0)
957 goto done;
958 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
959 goto done;
960 if (t->repos_total <= srv->max_repos ||
961 t->repos_total <= srv->max_repos_display)
962 goto done;
964 if (gotweb_render_navs(c->tp) == -1)
965 goto done;
966 done:
967 if (sd_dent) {
968 for (d_i = 0; d_i < d_cnt; d_i++)
969 free(sd_dent[d_i]);
970 free(sd_dent);
972 if (d != NULL && closedir(d) == EOF && error == NULL)
973 error = got_error_from_errno("closedir");
974 return error;
977 static const struct got_error *
978 gotweb_render_blame(struct request *c)
980 const struct got_error *error = NULL;
981 struct transport *t = c->t;
982 struct repo_commit *rc = NULL;
983 char *age = NULL, *msg = NULL;
984 int r;
986 error = got_get_repo_commits(c, 1);
987 if (error)
988 return error;
990 rc = TAILQ_FIRST(&t->repo_commits);
992 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
993 if (error)
994 goto done;
995 error = gotweb_escape_html(&msg, rc->commit_msg);
996 if (error)
997 goto done;
999 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1000 "<div id='blame_title'>Blame</div>\n"
1001 "</div>\n" /* #blame_title_wrapper */
1002 "<div id='blame_content'>\n"
1003 "<div id='blame_header_wrapper'>\n"
1004 "<div id='blame_header'>\n"
1005 "<div class='header_age_title'>Date:</div>\n"
1006 "<div class='header_age'>%s</div>\n"
1007 "<div id='header_commit_msg_title'>Message:</div>\n"
1008 "<div id='header_commit_msg'>%s</div>\n"
1009 "</div>\n" /* #blame_header */
1010 "</div>\n" /* #blame_header_wrapper */
1011 "<div class='dotted_line'></div>\n"
1012 "<div id='blame'>\n",
1013 age,
1014 msg);
1015 if (r == -1)
1016 goto done;
1018 error = got_output_file_blame(c);
1019 if (error)
1020 goto done;
1022 fcgi_printf(c, "</div>\n" /* #blame */
1023 "</div>\n"); /* #blame_content */
1024 done:
1025 free(age);
1026 free(msg);
1027 return error;
1030 const struct got_error *
1031 gotweb_escape_html(char **escaped_html, const char *orig_html)
1033 const struct got_error *error = NULL;
1034 struct escape_pair {
1035 char c;
1036 const char *s;
1037 } esc[] = {
1038 { '>', "&gt;" },
1039 { '<', "&lt;" },
1040 { '&', "&amp;" },
1041 { '"', "&quot;" },
1042 { '\'', "&apos;" },
1043 { '\n', "<br />" },
1045 size_t orig_len, len;
1046 int i, j, x;
1048 orig_len = strlen(orig_html);
1049 len = orig_len;
1050 for (i = 0; i < orig_len; i++) {
1051 for (j = 0; j < nitems(esc); j++) {
1052 if (orig_html[i] != esc[j].c)
1053 continue;
1054 len += strlen(esc[j].s) - 1 /* escaped char */;
1058 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1059 if (*escaped_html == NULL)
1060 return got_error_from_errno("calloc");
1062 x = 0;
1063 for (i = 0; i < orig_len; i++) {
1064 int escaped = 0;
1065 for (j = 0; j < nitems(esc); j++) {
1066 if (orig_html[i] != esc[j].c)
1067 continue;
1069 if (strlcat(*escaped_html, esc[j].s, len + 1)
1070 >= len + 1) {
1071 error = got_error(GOT_ERR_NO_SPACE);
1072 goto done;
1074 x += strlen(esc[j].s);
1075 escaped = 1;
1076 break;
1078 if (!escaped) {
1079 (*escaped_html)[x] = orig_html[i];
1080 x++;
1083 done:
1084 if (error) {
1085 free(*escaped_html);
1086 *escaped_html = NULL;
1087 } else {
1088 (*escaped_html)[x] = '\0';
1091 return error;
1094 static inline int
1095 should_urlencode(int c)
1097 if (c <= ' ' || c >= 127)
1098 return 1;
1100 switch (c) {
1101 /* gen-delim */
1102 case ':':
1103 case '/':
1104 case '?':
1105 case '#':
1106 case '[':
1107 case ']':
1108 case '@':
1109 /* sub-delims */
1110 case '!':
1111 case '$':
1112 case '&':
1113 case '\'':
1114 case '(':
1115 case ')':
1116 case '*':
1117 case '+':
1118 case ',':
1119 case ';':
1120 case '=':
1121 /* needed because the URLs are embedded into the HTML */
1122 case '\"':
1123 return 1;
1124 default:
1125 return 0;
1129 static char *
1130 gotweb_urlencode(const char *str)
1132 const char *s;
1133 char *escaped;
1134 size_t i, len;
1135 int a, b;
1137 len = 0;
1138 for (s = str; *s; ++s) {
1139 len++;
1140 if (should_urlencode(*s))
1141 len += 2;
1144 escaped = calloc(1, len + 1);
1145 if (escaped == NULL)
1146 return NULL;
1148 i = 0;
1149 for (s = str; *s; ++s) {
1150 if (should_urlencode(*s)) {
1151 a = (*s & 0xF0) >> 4;
1152 b = (*s & 0x0F);
1154 escaped[i++] = '%';
1155 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1156 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1157 } else
1158 escaped[i++] = *s;
1161 return escaped;
1164 const char *
1165 gotweb_action_name(int action)
1167 switch (action) {
1168 case BLAME:
1169 return "blame";
1170 case BLOB:
1171 return "blob";
1172 case BLOBRAW:
1173 return "blobraw";
1174 case BRIEFS:
1175 return "briefs";
1176 case COMMITS:
1177 return "commits";
1178 case DIFF:
1179 return "diff";
1180 case ERR:
1181 return "err";
1182 case INDEX:
1183 return "index";
1184 case SUMMARY:
1185 return "summary";
1186 case TAG:
1187 return "tag";
1188 case TAGS:
1189 return "tags";
1190 case TREE:
1191 return "tree";
1192 case RSS:
1193 return "rss";
1194 default:
1195 return NULL;
1199 int
1200 gotweb_render_url(struct request *c, struct gotweb_url *url)
1202 const char *sep = "?", *action;
1203 char *tmp;
1204 int r;
1206 action = gotweb_action_name(url->action);
1207 if (action != NULL) {
1208 if (fcgi_printf(c, "?action=%s", action) == -1)
1209 return -1;
1210 sep = "&";
1213 if (url->commit) {
1214 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1215 return -1;
1216 sep = "&";
1219 if (url->previd) {
1220 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1221 return -1;
1222 sep = "&";
1225 if (url->prevset) {
1226 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1227 return -1;
1228 sep = "&";
1231 if (url->file) {
1232 tmp = gotweb_urlencode(url->file);
1233 if (tmp == NULL)
1234 return -1;
1235 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1236 free(tmp);
1237 if (r == -1)
1238 return -1;
1239 sep = "&";
1242 if (url->folder) {
1243 tmp = gotweb_urlencode(url->folder);
1244 if (tmp == NULL)
1245 return -1;
1246 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1247 free(tmp);
1248 if (r == -1)
1249 return -1;
1250 sep = "&";
1253 if (url->headref) {
1254 tmp = gotweb_urlencode(url->headref);
1255 if (tmp == NULL)
1256 return -1;
1257 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1258 free(tmp);
1259 if (r == -1)
1260 return -1;
1261 sep = "&";
1264 if (url->index_page != -1) {
1265 if (fcgi_printf(c, "%sindex_page=%d", sep,
1266 url->index_page) == -1)
1267 return -1;
1268 sep = "&";
1271 if (url->path) {
1272 tmp = gotweb_urlencode(url->path);
1273 if (tmp == NULL)
1274 return -1;
1275 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1276 free(tmp);
1277 if (r == -1)
1278 return -1;
1279 sep = "&";
1282 if (url->page != -1) {
1283 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1284 return -1;
1285 sep = "&";
1288 return 0;
1291 int
1292 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1294 struct template *tp = c->tp;
1295 const char *proto = c->https ? "https" : "http";
1297 if (fcgi_puts(tp, proto) == -1 ||
1298 fcgi_puts(tp, "://") == -1 ||
1299 tp_htmlescape(tp, c->server_name) == -1 ||
1300 tp_htmlescape(tp, c->document_uri) == -1)
1301 return -1;
1303 return gotweb_render_url(c, url);
1306 int
1307 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1309 va_list ap;
1310 int r;
1312 if (fcgi_printf(c, "<a href='") == -1)
1313 return -1;
1315 if (gotweb_render_url(c, url) == -1)
1316 return -1;
1318 if (fcgi_printf(c, "'>") == -1)
1319 return -1;
1321 va_start(ap, fmt);
1322 r = fcgi_vprintf(c, fmt, ap);
1323 va_end(ap);
1324 if (r == -1)
1325 return -1;
1327 if (fcgi_printf(c, "</a>"))
1328 return -1;
1329 return 0;
1332 static struct got_repository *
1333 find_cached_repo(struct server *srv, const char *path)
1335 int i;
1337 for (i = 0; i < srv->ncached_repos; i++) {
1338 if (strcmp(srv->cached_repos[i].path, path) == 0)
1339 return srv->cached_repos[i].repo;
1342 return NULL;
1345 static const struct got_error *
1346 cache_repo(struct got_repository **new, struct server *srv,
1347 struct repo_dir *repo_dir, struct socket *sock)
1349 const struct got_error *error = NULL;
1350 struct got_repository *repo;
1351 struct cached_repo *cr;
1352 int evicted = 0;
1354 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1355 cr = &srv->cached_repos[srv->ncached_repos - 1];
1356 error = got_repo_close(cr->repo);
1357 memset(cr, 0, sizeof(*cr));
1358 srv->ncached_repos--;
1359 if (error)
1360 return error;
1361 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1362 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1363 cr = &srv->cached_repos[0];
1364 evicted = 1;
1365 } else {
1366 cr = &srv->cached_repos[srv->ncached_repos];
1369 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1370 if (error) {
1371 if (evicted) {
1372 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1373 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1375 return error;
1378 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1379 >= sizeof(cr->path)) {
1380 if (evicted) {
1381 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1382 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1384 return got_error(GOT_ERR_NO_SPACE);
1387 cr->repo = repo;
1388 srv->ncached_repos++;
1389 *new = repo;
1390 return NULL;
1393 static const struct got_error *
1394 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1396 const struct got_error *error = NULL;
1397 struct socket *sock = c->sock;
1398 struct server *srv = c->srv;
1399 struct transport *t = c->t;
1400 struct got_repository *repo = NULL;
1401 DIR *dt;
1402 char *dir_test;
1404 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1405 GOTWEB_GIT_DIR) == -1)
1406 return got_error_from_errno("asprintf");
1408 dt = opendir(dir_test);
1409 if (dt == NULL) {
1410 free(dir_test);
1411 } else {
1412 repo_dir->path = dir_test;
1413 dir_test = NULL;
1414 goto done;
1417 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1418 repo_dir->name) == -1)
1419 return got_error_from_errno("asprintf");
1421 dt = opendir(dir_test);
1422 if (dt == NULL) {
1423 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1424 goto err;
1425 } else {
1426 repo_dir->path = dir_test;
1427 dir_test = NULL;
1430 done:
1431 if (srv->respect_exportok &&
1432 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1433 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1434 goto err;
1437 repo = find_cached_repo(srv, repo_dir->path);
1438 if (repo == NULL) {
1439 error = cache_repo(&repo, srv, repo_dir, sock);
1440 if (error)
1441 goto err;
1443 t->repo = repo;
1444 error = gotweb_get_repo_description(&repo_dir->description, srv,
1445 repo_dir->path, dirfd(dt));
1446 if (error)
1447 goto err;
1448 error = got_get_repo_owner(&repo_dir->owner, c);
1449 if (error)
1450 goto err;
1451 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1452 if (error)
1453 goto err;
1454 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1455 dirfd(dt));
1456 err:
1457 free(dir_test);
1458 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1459 error = got_error_from_errno("closedir");
1460 return error;
1463 static const struct got_error *
1464 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1466 const struct got_error *error;
1468 *repo_dir = calloc(1, sizeof(**repo_dir));
1469 if (*repo_dir == NULL)
1470 return got_error_from_errno("calloc");
1472 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1473 error = got_error_from_errno("asprintf");
1474 free(*repo_dir);
1475 *repo_dir = NULL;
1476 return error;
1478 (*repo_dir)->owner = NULL;
1479 (*repo_dir)->description = NULL;
1480 (*repo_dir)->url = NULL;
1481 (*repo_dir)->age = NULL;
1482 (*repo_dir)->path = NULL;
1484 return NULL;
1487 static const struct got_error *
1488 gotweb_get_repo_description(char **description, struct server *srv,
1489 const char *dirpath, int dir)
1491 const struct got_error *error = NULL;
1492 struct stat sb;
1493 int fd = -1;
1494 off_t len;
1496 *description = NULL;
1497 if (srv->show_repo_description == 0)
1498 return NULL;
1500 fd = openat(dir, "description", O_RDONLY);
1501 if (fd == -1) {
1502 if (errno != ENOENT && errno != EACCES) {
1503 error = got_error_from_errno_fmt("openat %s/%s",
1504 dirpath, "description");
1506 goto done;
1509 if (fstat(fd, &sb) == -1) {
1510 error = got_error_from_errno_fmt("fstat %s/%s",
1511 dirpath, "description");
1512 goto done;
1515 len = sb.st_size;
1516 if (len > GOTWEBD_MAXDESCRSZ - 1)
1517 len = GOTWEBD_MAXDESCRSZ - 1;
1519 *description = calloc(len + 1, sizeof(**description));
1520 if (*description == NULL) {
1521 error = got_error_from_errno("calloc");
1522 goto done;
1525 if (read(fd, *description, len) == -1)
1526 error = got_error_from_errno("read");
1527 done:
1528 if (fd != -1 && close(fd) == -1 && error == NULL)
1529 error = got_error_from_errno("close");
1530 return error;
1533 static const struct got_error *
1534 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1535 int dir)
1537 const struct got_error *error = NULL;
1538 struct stat sb;
1539 int fd = -1;
1540 off_t len;
1542 *url = NULL;
1543 if (srv->show_repo_cloneurl == 0)
1544 return NULL;
1546 fd = openat(dir, "cloneurl", O_RDONLY);
1547 if (fd == -1) {
1548 if (errno != ENOENT && errno != EACCES) {
1549 error = got_error_from_errno_fmt("openat %s/%s",
1550 dirpath, "cloneurl");
1552 goto done;
1555 if (fstat(fd, &sb) == -1) {
1556 error = got_error_from_errno_fmt("fstat %s/%s",
1557 dirpath, "cloneurl");
1558 goto done;
1561 len = sb.st_size;
1562 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1563 len = GOTWEBD_MAXCLONEURLSZ - 1;
1565 *url = calloc(len + 1, sizeof(**url));
1566 if (*url == NULL) {
1567 error = got_error_from_errno("calloc");
1568 goto done;
1571 if (read(fd, *url, len) == -1)
1572 error = got_error_from_errno("read");
1573 done:
1574 if (fd != -1 && close(fd) == -1 && error == NULL)
1575 error = got_error_from_errno("close");
1576 return error;
1579 const struct got_error *
1580 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1582 struct tm tm;
1583 long long diff_time;
1584 const char *years = "years ago", *months = "months ago";
1585 const char *weeks = "weeks ago", *days = "days ago";
1586 const char *hours = "hours ago", *minutes = "minutes ago";
1587 const char *seconds = "seconds ago", *now = "right now";
1588 char *s;
1589 char datebuf[64];
1590 size_t r;
1592 *repo_age = NULL;
1594 switch (ref_tm) {
1595 case TM_DIFF:
1596 diff_time = time(NULL) - committer_time;
1597 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1598 if (asprintf(repo_age, "%lld %s",
1599 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1600 return got_error_from_errno("asprintf");
1601 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1602 if (asprintf(repo_age, "%lld %s",
1603 (diff_time / 60 / 60 / 24 / (365 / 12)),
1604 months) == -1)
1605 return got_error_from_errno("asprintf");
1606 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1607 if (asprintf(repo_age, "%lld %s",
1608 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1609 return got_error_from_errno("asprintf");
1610 } else if (diff_time > 60 * 60 * 24 * 2) {
1611 if (asprintf(repo_age, "%lld %s",
1612 (diff_time / 60 / 60 / 24), days) == -1)
1613 return got_error_from_errno("asprintf");
1614 } else if (diff_time > 60 * 60 * 2) {
1615 if (asprintf(repo_age, "%lld %s",
1616 (diff_time / 60 / 60), hours) == -1)
1617 return got_error_from_errno("asprintf");
1618 } else if (diff_time > 60 * 2) {
1619 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1620 minutes) == -1)
1621 return got_error_from_errno("asprintf");
1622 } else if (diff_time > 2) {
1623 if (asprintf(repo_age, "%lld %s", diff_time,
1624 seconds) == -1)
1625 return got_error_from_errno("asprintf");
1626 } else {
1627 if (asprintf(repo_age, "%s", now) == -1)
1628 return got_error_from_errno("asprintf");
1630 break;
1631 case TM_LONG:
1632 if (gmtime_r(&committer_time, &tm) == NULL)
1633 return got_error_from_errno("gmtime_r");
1635 s = asctime_r(&tm, datebuf);
1636 if (s == NULL)
1637 return got_error_from_errno("asctime_r");
1639 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1640 return got_error_from_errno("asprintf");
1641 break;
1642 case TM_RFC822:
1643 if (gmtime_r(&committer_time, &tm) == NULL)
1644 return got_error_from_errno("gmtime_r");
1646 r = strftime(datebuf, sizeof(datebuf),
1647 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1648 if (r == 0)
1649 return got_error(GOT_ERR_NO_SPACE);
1651 *repo_age = strdup(datebuf);
1652 if (*repo_age == NULL)
1653 return got_error_from_errno("asprintf");
1654 break;
1656 return NULL;