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 void
103 gotweb_process_request(struct request *c)
105 const struct got_error *error = NULL, *error2 = NULL;
106 struct got_blob_object *blob = NULL;
107 struct server *srv = NULL;
108 struct querystring *qs = NULL;
109 struct repo_dir *repo_dir = NULL;
110 struct got_reflist_head refs;
111 FILE *fp = NULL;
112 uint8_t err[] = "gotwebd experienced an error: ";
113 int r, html = 0, fd = -1;
115 TAILQ_INIT(&refs);
117 /* init the transport */
118 error = gotweb_init_transport(&c->t);
119 if (error) {
120 log_warnx("%s: %s", __func__, error->msg);
121 return;
123 /* don't process any further if client disconnected */
124 if (c->sock->client_status == CLIENT_DISCONNECT)
125 return;
126 /* get the gotwebd server */
127 srv = gotweb_get_server(c->server_name, c->http_host);
128 if (srv == NULL) {
129 log_warnx("%s: error server is NULL", __func__);
130 goto err;
132 c->srv = srv;
133 /* parse our querystring */
134 error = gotweb_init_querystring(&qs);
135 if (error) {
136 log_warnx("%s: %s", __func__, error->msg);
137 goto err;
139 c->t->qs = qs;
140 error = gotweb_parse_querystring(&qs, c->querystring);
141 if (error) {
142 log_warnx("%s: %s", __func__, error->msg);
143 goto err;
146 /*
147 * certain actions require a commit id in the querystring. this stops
148 * bad actors from exploiting this by manually manipulating the
149 * querystring.
150 */
152 if (qs->action == BLAME || qs->action == BLOB ||
153 qs->action == BLOBRAW || qs->action == DIFF) {
154 if (qs->commit == NULL) {
155 error2 = got_error(GOT_ERR_QUERYSTRING);
156 goto render;
160 if (qs->action != INDEX) {
161 error = gotweb_init_repo_dir(&repo_dir, qs->path);
162 if (error)
163 goto done;
164 error = gotweb_load_got_path(c, repo_dir);
165 c->t->repo_dir = repo_dir;
166 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
167 goto err;
170 if (qs->action == BLOBRAW) {
171 const uint8_t *buf;
172 size_t len;
173 int binary;
175 error = got_get_repo_commits(c, 1);
176 if (error)
177 goto done;
179 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
180 if (error2)
181 goto render;
183 if (binary)
184 error = gotweb_render_content_type_file(c,
185 "application/octet-stream", qs->file, NULL);
186 else
187 error = gotweb_render_content_type(c, "text/plain");
189 if (error) {
190 log_warnx("%s: %s", __func__, error->msg);
191 goto done;
194 for (;;) {
195 error = got_object_blob_read_block(&len, blob);
196 if (error)
197 goto done;
198 if (len == 0)
199 break;
200 buf = got_object_blob_get_read_buf(blob);
201 if (fcgi_gen_binary_response(c, buf, len) == -1)
202 goto done;
205 goto done;
208 if (qs->action == BLOB) {
209 int binary;
210 struct gotweb_url url = {
211 .index_page = -1,
212 .page = -1,
213 .action = BLOBRAW,
214 .path = qs->path,
215 .commit = qs->commit,
216 .folder = qs->folder,
217 .file = qs->file,
218 };
220 error = got_get_repo_commits(c, 1);
221 if (error)
222 goto done;
224 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
225 if (error2)
226 goto render;
227 if (binary) {
228 fcgi_puts(c->tp, "Status: 302\r\n");
229 fcgi_puts(c->tp, "Location: ");
230 gotweb_render_url(c, &url);
231 fcgi_puts(c->tp, "\r\n\r\n");
232 goto done;
236 if (qs->action == RSS) {
237 error = gotweb_render_content_type_file(c,
238 "application/rss+xml;charset=utf-8",
239 repo_dir->name, ".rss");
240 if (error) {
241 log_warnx("%s: %s", __func__, error->msg);
242 goto err;
245 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
246 if (error) {
247 log_warnx("%s: %s", __func__, error->msg);
248 goto err;
250 if (gotweb_render_rss(c->tp) == -1)
251 goto err;
252 goto done;
255 render:
256 error = gotweb_render_content_type(c, "text/html");
257 if (error) {
258 log_warnx("%s: %s", __func__, error->msg);
259 goto err;
261 html = 1;
263 if (gotweb_render_header(c->tp) == -1)
264 goto err;
266 if (error2) {
267 error = error2;
268 goto err;
271 switch(qs->action) {
272 case BLAME:
273 error = got_get_repo_commits(c, 1);
274 if (error) {
275 log_warnx("%s: %s", __func__, error->msg);
276 goto err;
278 if (gotweb_render_blame(c->tp) == -1)
279 goto done;
280 break;
281 case BLOB:
282 if (gotweb_render_blob(c->tp, blob) == -1)
283 goto err;
284 break;
285 case BRIEFS:
286 if (gotweb_render_briefs(c->tp) == -1)
287 goto err;
288 break;
289 case COMMITS:
290 error = got_get_repo_commits(c, srv->max_commits_display);
291 if (error) {
292 log_warnx("%s: %s", __func__, error->msg);
293 goto err;
295 if (gotweb_render_commits(c->tp) == -1)
296 goto err;
297 break;
298 case DIFF:
299 error = got_get_repo_commits(c, 1);
300 if (error) {
301 log_warnx("%s: %s", __func__, error->msg);
302 goto err;
304 error = got_open_diff_for_output(&fp, &fd, c);
305 if (error) {
306 log_warnx("%s: %s", __func__, error->msg);
307 goto err;
309 if (gotweb_render_diff(c->tp, fp) == -1)
310 goto err;
311 break;
312 case INDEX:
313 error = gotweb_render_index(c);
314 if (error) {
315 log_warnx("%s: %s", __func__, error->msg);
316 goto err;
318 break;
319 case SUMMARY:
320 error = got_ref_list(&refs, c->t->repo, "refs/heads",
321 got_ref_cmp_by_name, NULL);
322 if (error) {
323 log_warnx("%s: got_ref_list: %s", __func__,
324 error->msg);
325 goto err;
327 qs->action = TAGS;
328 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
329 if (error) {
330 log_warnx("%s: got_get_repo_tags: %s", __func__,
331 error->msg);
332 goto err;
334 qs->action = SUMMARY;
335 if (gotweb_render_summary(c->tp, &refs) == -1)
336 goto done;
337 break;
338 case TAG:
339 error = got_get_repo_tags(c, 1);
340 if (error) {
341 log_warnx("%s: %s", __func__, error->msg);
342 goto err;
344 if (c->t->tag_count == 0) {
345 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
346 "bad commit id");
347 goto err;
349 if (gotweb_render_tag(c->tp) == -1)
350 goto done;
351 break;
352 case TAGS:
353 error = got_get_repo_tags(c, srv->max_commits_display);
354 if (error) {
355 log_warnx("%s: %s", __func__, error->msg);
356 goto err;
358 if (gotweb_render_tags(c->tp) == -1)
359 goto done;
360 break;
361 case TREE:
362 error = got_get_repo_commits(c, 1);
363 if (error) {
364 log_warnx("%s: %s", __func__, error->msg);
365 goto err;
367 if (gotweb_render_tree(c->tp) == -1)
368 goto err;
369 break;
370 case ERR:
371 default:
372 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
373 "Erorr: Bad Querystring");
374 if (r == -1)
375 goto err;
376 break;
379 goto done;
380 err:
381 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
382 return;
383 if (fcgi_printf(c, "\n%s", err) == -1)
384 return;
385 if (error) {
386 if (fcgi_printf(c, "%s", error->msg) == -1)
387 return;
388 } else {
389 if (fcgi_printf(c, "see daemon logs for details") == -1)
390 return;
392 if (html && fcgi_printf(c, "</div>\n") == -1)
393 return;
394 done:
395 if (blob)
396 got_object_blob_close(blob);
397 if (fp) {
398 error = got_gotweb_flushfile(fp, fd);
399 if (error)
400 log_warnx("%s: got_gotweb_flushfile failure: %s",
401 __func__, error->msg);
402 fd = -1;
404 if (fd != -1)
405 close(fd);
406 if (html && srv != NULL)
407 gotweb_render_footer(c->tp);
409 got_ref_list_free(&refs);
412 struct server *
413 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
415 struct server *srv = NULL;
417 /* check against the server name first */
418 if (strlen(server_name) > 0)
419 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
420 if (strcmp(srv->name, server_name) == 0)
421 goto done;
423 /* check against subdomain second */
424 if (strlen(subdomain) > 0)
425 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
426 if (strcmp(srv->name, subdomain) == 0)
427 goto done;
429 /* if those fail, send first server */
430 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
431 if (srv != NULL)
432 break;
433 done:
434 return srv;
435 };
437 const struct got_error *
438 gotweb_init_transport(struct transport **t)
440 const struct got_error *error = NULL;
442 *t = calloc(1, sizeof(**t));
443 if (*t == NULL)
444 return got_error_from_errno2("%s: calloc", __func__);
446 TAILQ_INIT(&(*t)->repo_commits);
447 TAILQ_INIT(&(*t)->repo_tags);
449 (*t)->repo = NULL;
450 (*t)->repo_dir = NULL;
451 (*t)->qs = NULL;
452 (*t)->next_id = NULL;
453 (*t)->prev_id = NULL;
454 (*t)->next_disp = 0;
455 (*t)->prev_disp = 0;
457 return error;
460 static const struct got_error *
461 gotweb_init_querystring(struct querystring **qs)
463 const struct got_error *error = NULL;
465 *qs = calloc(1, sizeof(**qs));
466 if (*qs == NULL)
467 return got_error_from_errno2("%s: calloc", __func__);
469 (*qs)->headref = strdup("HEAD");
470 if ((*qs)->headref == NULL) {
471 free(*qs);
472 *qs = NULL;
473 return got_error_from_errno2("%s: strdup", __func__);
476 (*qs)->action = INDEX;
477 (*qs)->commit = NULL;
478 (*qs)->file = NULL;
479 (*qs)->folder = NULL;
480 (*qs)->index_page = 0;
481 (*qs)->path = NULL;
483 return error;
486 static const struct got_error *
487 gotweb_parse_querystring(struct querystring **qs, char *qst)
489 const struct got_error *error = NULL;
490 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
491 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
493 if (qst == NULL)
494 return error;
496 tok1 = strdup(qst);
497 if (tok1 == NULL)
498 return got_error_from_errno2("%s: strdup", __func__);
500 tok1_pair = tok1;
501 tok1_end = tok1;
503 while (tok1_pair != NULL) {
504 strsep(&tok1_end, "&");
506 tok2 = strdup(tok1_pair);
507 if (tok2 == NULL) {
508 free(tok1);
509 return got_error_from_errno2("%s: strdup", __func__);
512 tok2_pair = tok2;
513 tok2_end = tok2;
515 while (tok2_pair != NULL) {
516 strsep(&tok2_end, "=");
517 if (tok2_end) {
518 error = gotweb_assign_querystring(qs, tok2_pair,
519 tok2_end);
520 if (error)
521 goto err;
523 tok2_pair = tok2_end;
525 free(tok2);
526 tok1_pair = tok1_end;
528 free(tok1);
529 return error;
530 err:
531 free(tok2);
532 free(tok1);
533 return error;
536 /*
537 * Adapted from usr.sbin/httpd/httpd.c url_decode.
538 */
539 static const struct got_error *
540 gotweb_urldecode(char *url)
542 char *p, *q;
543 char hex[3];
544 unsigned long x;
546 hex[2] = '\0';
547 p = q = url;
549 while (*p != '\0') {
550 switch (*p) {
551 case '%':
552 /* Encoding character is followed by two hex chars */
553 if (!isxdigit((unsigned char)p[1]) ||
554 !isxdigit((unsigned char)p[2]) ||
555 (p[1] == '0' && p[2] == '0'))
556 return got_error(GOT_ERR_BAD_QUERYSTRING);
558 hex[0] = p[1];
559 hex[1] = p[2];
561 /*
562 * We don't have to validate "hex" because it is
563 * guaranteed to include two hex chars followed by nul.
564 */
565 x = strtoul(hex, NULL, 16);
566 *q = (char)x;
567 p += 2;
568 break;
569 default:
570 *q = *p;
571 break;
573 p++;
574 q++;
576 *q = '\0';
578 return NULL;
581 static const struct got_error *
582 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
584 const struct got_error *error = NULL;
585 const char *errstr;
586 int a_cnt, el_cnt;
588 error = gotweb_urldecode(value);
589 if (error)
590 return error;
592 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
593 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
594 continue;
596 switch (querystring_keys[el_cnt].element) {
597 case ACTION:
598 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
599 if (strcmp(value, action_keys[a_cnt].name) != 0)
600 continue;
601 else if (strcmp(value,
602 action_keys[a_cnt].name) == 0){
603 (*qs)->action =
604 action_keys[a_cnt].action;
605 goto qa_found;
608 (*qs)->action = ERR;
609 qa_found:
610 break;
611 case COMMIT:
612 (*qs)->commit = strdup(value);
613 if ((*qs)->commit == NULL) {
614 error = got_error_from_errno2("%s: strdup",
615 __func__);
616 goto done;
618 break;
619 case RFILE:
620 (*qs)->file = strdup(value);
621 if ((*qs)->file == NULL) {
622 error = got_error_from_errno2("%s: strdup",
623 __func__);
624 goto done;
626 break;
627 case FOLDER:
628 (*qs)->folder = strdup(value);
629 if ((*qs)->folder == NULL) {
630 error = got_error_from_errno2("%s: strdup",
631 __func__);
632 goto done;
634 break;
635 case HEADREF:
636 free((*qs)->headref);
637 (*qs)->headref = strdup(value);
638 if ((*qs)->headref == NULL) {
639 error = got_error_from_errno2("%s: strdup",
640 __func__);
641 goto done;
643 break;
644 case INDEX_PAGE:
645 if (strlen(value) == 0)
646 break;
647 (*qs)->index_page = strtonum(value, INT64_MIN,
648 INT64_MAX, &errstr);
649 if (errstr) {
650 error = got_error_from_errno3("%s: strtonum %s",
651 __func__, errstr);
652 goto done;
654 if ((*qs)->index_page < 0)
655 (*qs)->index_page = 0;
656 break;
657 case PATH:
658 (*qs)->path = strdup(value);
659 if ((*qs)->path == NULL) {
660 error = got_error_from_errno2("%s: strdup",
661 __func__);
662 goto done;
664 break;
665 case PAGE:
666 if (strlen(value) == 0)
667 break;
668 (*qs)->page = strtonum(value, INT64_MIN,
669 INT64_MAX, &errstr);
670 if (errstr) {
671 error = got_error_from_errno3("%s: strtonum %s",
672 __func__, errstr);
673 goto done;
675 if ((*qs)->page < 0)
676 (*qs)->page = 0;
677 break;
678 default:
679 break;
682 done:
683 return error;
686 void
687 gotweb_free_repo_tag(struct repo_tag *rt)
689 if (rt != NULL) {
690 free(rt->commit_id);
691 free(rt->tag_name);
692 free(rt->tag_commit);
693 free(rt->commit_msg);
694 free(rt->tagger);
696 free(rt);
699 void
700 gotweb_free_repo_commit(struct repo_commit *rc)
702 if (rc != NULL) {
703 free(rc->path);
704 free(rc->refs_str);
705 free(rc->commit_id);
706 free(rc->parent_id);
707 free(rc->tree_id);
708 free(rc->author);
709 free(rc->committer);
710 free(rc->commit_msg);
712 free(rc);
715 static void
716 gotweb_free_querystring(struct querystring *qs)
718 if (qs != NULL) {
719 free(qs->commit);
720 free(qs->file);
721 free(qs->folder);
722 free(qs->headref);
723 free(qs->path);
725 free(qs);
728 static void
729 gotweb_free_repo_dir(struct repo_dir *repo_dir)
731 if (repo_dir != NULL) {
732 free(repo_dir->name);
733 free(repo_dir->owner);
734 free(repo_dir->description);
735 free(repo_dir->url);
736 free(repo_dir->age);
737 free(repo_dir->path);
739 free(repo_dir);
742 void
743 gotweb_free_transport(struct transport *t)
745 struct repo_commit *rc = NULL, *trc = NULL;
746 struct repo_tag *rt = NULL, *trt = NULL;
748 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
749 TAILQ_REMOVE(&t->repo_commits, rc, entry);
750 gotweb_free_repo_commit(rc);
752 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
753 TAILQ_REMOVE(&t->repo_tags, rt, entry);
754 gotweb_free_repo_tag(rt);
756 gotweb_free_repo_dir(t->repo_dir);
757 gotweb_free_querystring(t->qs);
758 free(t->next_id);
759 free(t->prev_id);
760 free(t);
763 const struct got_error *
764 gotweb_render_content_type(struct request *c, const char *type)
766 const char *csp = "default-src 'self'; script-src 'none'; "
767 "object-src 'none';";
769 fcgi_printf(c,
770 "Content-Security-Policy: %s\r\n"
771 "Content-Type: %s\r\n\r\n",
772 csp, type);
773 return NULL;
776 const struct got_error *
777 gotweb_render_content_type_file(struct request *c, const char *type,
778 const char *file, const char *suffix)
780 fcgi_printf(c, "Content-type: %s\r\n"
781 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
782 type, file, suffix ? suffix : "");
783 return NULL;
786 void
787 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
788 struct gotweb_url *next, int *have_next)
790 struct transport *t = c->t;
791 struct querystring *qs = t->qs;
792 struct server *srv = c->srv;
794 *have_prev = *have_next = 0;
796 switch(qs->action) {
797 case INDEX:
798 if (qs->index_page > 0) {
799 *have_prev = 1;
800 *prev = (struct gotweb_url){
801 .action = -1,
802 .index_page = qs->index_page - 1,
803 .page = -1,
804 };
806 if (t->next_disp == srv->max_repos_display &&
807 t->repos_total != (qs->index_page + 1) *
808 srv->max_repos_display) {
809 *have_next = 1;
810 *next = (struct gotweb_url){
811 .action = -1,
812 .index_page = qs->index_page + 1,
813 .page = -1,
814 };
816 break;
817 case BRIEFS:
818 if (t->prev_id && qs->commit != NULL &&
819 strcmp(qs->commit, t->prev_id) != 0) {
820 *have_prev = 1;
821 *prev = (struct gotweb_url){
822 .action = BRIEFS,
823 .index_page = -1,
824 .page = qs->page - 1,
825 .path = qs->path,
826 .commit = t->prev_id,
827 .headref = qs->headref,
828 };
830 if (t->next_id) {
831 *have_next = 1;
832 *next = (struct gotweb_url){
833 .action = BRIEFS,
834 .index_page = -1,
835 .page = qs->page + 1,
836 .path = qs->path,
837 .commit = t->next_id,
838 .headref = qs->headref,
839 };
841 break;
842 case COMMITS:
843 if (t->prev_id && qs->commit != NULL &&
844 strcmp(qs->commit, t->prev_id) != 0) {
845 *have_prev = 1;
846 *prev = (struct gotweb_url){
847 .action = COMMITS,
848 .index_page = -1,
849 .page = qs->page - 1,
850 .path = qs->path,
851 .commit = t->prev_id,
852 .headref = qs->headref,
853 .folder = qs->folder,
854 .file = qs->file,
855 };
857 if (t->next_id) {
858 *have_next = 1;
859 *next = (struct gotweb_url){
860 .action = COMMITS,
861 .index_page = -1,
862 .page = qs->page + 1,
863 .path = qs->path,
864 .commit = t->next_id,
865 .headref = qs->headref,
866 .folder = qs->folder,
867 .file = qs->file,
868 };
870 break;
871 case TAGS:
872 if (t->prev_id && qs->commit != NULL &&
873 strcmp(qs->commit, t->prev_id) != 0) {
874 *have_prev = 1;
875 *prev = (struct gotweb_url){
876 .action = TAGS,
877 .index_page = -1,
878 .page = qs->page - 1,
879 .path = qs->path,
880 .commit = t->prev_id,
881 .headref = qs->headref,
882 };
884 if (t->next_id) {
885 *have_next = 1;
886 *next = (struct gotweb_url){
887 .action = TAGS,
888 .index_page = -1,
889 .page = qs->page + 1,
890 .path = qs->path,
891 .commit = t->next_id,
892 .headref = qs->headref,
893 };
895 break;
899 static const struct got_error *
900 gotweb_render_index(struct request *c)
902 const struct got_error *error = NULL;
903 struct server *srv = c->srv;
904 struct transport *t = c->t;
905 struct querystring *qs = t->qs;
906 struct repo_dir *repo_dir = NULL;
907 DIR *d;
908 struct dirent **sd_dent = NULL;
909 unsigned int d_cnt, d_i, d_disp = 0;
910 unsigned int d_skipped = 0;
911 int type;
913 d = opendir(srv->repos_path);
914 if (d == NULL) {
915 error = got_error_from_errno2("opendir", srv->repos_path);
916 return error;
919 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
920 if (d_cnt == -1) {
921 sd_dent = NULL;
922 error = got_error_from_errno2("scandir", srv->repos_path);
923 goto done;
926 if (gotweb_render_repo_table_hdr(c->tp) == -1)
927 goto done;
929 for (d_i = 0; d_i < d_cnt; d_i++) {
930 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
931 break;
933 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
934 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
935 d_skipped++;
936 continue;
939 error = got_path_dirent_type(&type, srv->repos_path,
940 sd_dent[d_i]);
941 if (error)
942 goto done;
943 if (type != DT_DIR) {
944 d_skipped++;
945 continue;
948 if (qs->index_page > 0 && (qs->index_page *
949 srv->max_repos_display) > t->prev_disp) {
950 t->prev_disp++;
951 continue;
954 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
955 if (error)
956 goto done;
958 error = gotweb_load_got_path(c, repo_dir);
959 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
960 error = NULL;
961 gotweb_free_repo_dir(repo_dir);
962 repo_dir = NULL;
963 d_skipped++;
964 continue;
966 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
967 goto done;
969 d_disp++;
970 t->prev_disp++;
972 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
973 goto done;
975 gotweb_free_repo_dir(repo_dir);
976 repo_dir = NULL;
977 t->next_disp++;
978 if (d_disp == srv->max_repos_display)
979 break;
981 t->repos_total = d_cnt - d_skipped;
983 if (srv->max_repos_display == 0)
984 goto done;
985 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
986 goto done;
987 if (t->repos_total <= srv->max_repos ||
988 t->repos_total <= srv->max_repos_display)
989 goto done;
991 if (gotweb_render_navs(c->tp) == -1)
992 goto done;
993 done:
994 if (sd_dent) {
995 for (d_i = 0; d_i < d_cnt; d_i++)
996 free(sd_dent[d_i]);
997 free(sd_dent);
999 if (d != NULL && closedir(d) == EOF && error == NULL)
1000 error = got_error_from_errno("closedir");
1001 return error;
1004 const struct got_error *
1005 gotweb_escape_html(char **escaped_html, const char *orig_html)
1007 const struct got_error *error = NULL;
1008 struct escape_pair {
1009 char c;
1010 const char *s;
1011 } esc[] = {
1012 { '>', "&gt;" },
1013 { '<', "&lt;" },
1014 { '&', "&amp;" },
1015 { '"', "&quot;" },
1016 { '\'', "&apos;" },
1017 { '\n', "<br />" },
1019 size_t orig_len, len;
1020 int i, j, x;
1022 orig_len = strlen(orig_html);
1023 len = orig_len;
1024 for (i = 0; i < orig_len; i++) {
1025 for (j = 0; j < nitems(esc); j++) {
1026 if (orig_html[i] != esc[j].c)
1027 continue;
1028 len += strlen(esc[j].s) - 1 /* escaped char */;
1032 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1033 if (*escaped_html == NULL)
1034 return got_error_from_errno("calloc");
1036 x = 0;
1037 for (i = 0; i < orig_len; i++) {
1038 int escaped = 0;
1039 for (j = 0; j < nitems(esc); j++) {
1040 if (orig_html[i] != esc[j].c)
1041 continue;
1043 if (strlcat(*escaped_html, esc[j].s, len + 1)
1044 >= len + 1) {
1045 error = got_error(GOT_ERR_NO_SPACE);
1046 goto done;
1048 x += strlen(esc[j].s);
1049 escaped = 1;
1050 break;
1052 if (!escaped) {
1053 (*escaped_html)[x] = orig_html[i];
1054 x++;
1057 done:
1058 if (error) {
1059 free(*escaped_html);
1060 *escaped_html = NULL;
1061 } else {
1062 (*escaped_html)[x] = '\0';
1065 return error;
1068 static inline int
1069 should_urlencode(int c)
1071 if (c <= ' ' || c >= 127)
1072 return 1;
1074 switch (c) {
1075 /* gen-delim */
1076 case ':':
1077 case '/':
1078 case '?':
1079 case '#':
1080 case '[':
1081 case ']':
1082 case '@':
1083 /* sub-delims */
1084 case '!':
1085 case '$':
1086 case '&':
1087 case '\'':
1088 case '(':
1089 case ')':
1090 case '*':
1091 case '+':
1092 case ',':
1093 case ';':
1094 case '=':
1095 /* needed because the URLs are embedded into the HTML */
1096 case '\"':
1097 return 1;
1098 default:
1099 return 0;
1103 static char *
1104 gotweb_urlencode(const char *str)
1106 const char *s;
1107 char *escaped;
1108 size_t i, len;
1109 int a, b;
1111 len = 0;
1112 for (s = str; *s; ++s) {
1113 len++;
1114 if (should_urlencode(*s))
1115 len += 2;
1118 escaped = calloc(1, len + 1);
1119 if (escaped == NULL)
1120 return NULL;
1122 i = 0;
1123 for (s = str; *s; ++s) {
1124 if (should_urlencode(*s)) {
1125 a = (*s & 0xF0) >> 4;
1126 b = (*s & 0x0F);
1128 escaped[i++] = '%';
1129 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1130 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1131 } else
1132 escaped[i++] = *s;
1135 return escaped;
1138 const char *
1139 gotweb_action_name(int action)
1141 switch (action) {
1142 case BLAME:
1143 return "blame";
1144 case BLOB:
1145 return "blob";
1146 case BLOBRAW:
1147 return "blobraw";
1148 case BRIEFS:
1149 return "briefs";
1150 case COMMITS:
1151 return "commits";
1152 case DIFF:
1153 return "diff";
1154 case ERR:
1155 return "err";
1156 case INDEX:
1157 return "index";
1158 case SUMMARY:
1159 return "summary";
1160 case TAG:
1161 return "tag";
1162 case TAGS:
1163 return "tags";
1164 case TREE:
1165 return "tree";
1166 case RSS:
1167 return "rss";
1168 default:
1169 return NULL;
1173 int
1174 gotweb_render_url(struct request *c, struct gotweb_url *url)
1176 const char *sep = "?", *action;
1177 char *tmp;
1178 int r;
1180 action = gotweb_action_name(url->action);
1181 if (action != NULL) {
1182 if (fcgi_printf(c, "?action=%s", action) == -1)
1183 return -1;
1184 sep = "&";
1187 if (url->commit) {
1188 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1189 return -1;
1190 sep = "&";
1193 if (url->previd) {
1194 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1195 return -1;
1196 sep = "&";
1199 if (url->prevset) {
1200 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1201 return -1;
1202 sep = "&";
1205 if (url->file) {
1206 tmp = gotweb_urlencode(url->file);
1207 if (tmp == NULL)
1208 return -1;
1209 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1210 free(tmp);
1211 if (r == -1)
1212 return -1;
1213 sep = "&";
1216 if (url->folder) {
1217 tmp = gotweb_urlencode(url->folder);
1218 if (tmp == NULL)
1219 return -1;
1220 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1221 free(tmp);
1222 if (r == -1)
1223 return -1;
1224 sep = "&";
1227 if (url->headref) {
1228 tmp = gotweb_urlencode(url->headref);
1229 if (tmp == NULL)
1230 return -1;
1231 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1232 free(tmp);
1233 if (r == -1)
1234 return -1;
1235 sep = "&";
1238 if (url->index_page != -1) {
1239 if (fcgi_printf(c, "%sindex_page=%d", sep,
1240 url->index_page) == -1)
1241 return -1;
1242 sep = "&";
1245 if (url->path) {
1246 tmp = gotweb_urlencode(url->path);
1247 if (tmp == NULL)
1248 return -1;
1249 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1250 free(tmp);
1251 if (r == -1)
1252 return -1;
1253 sep = "&";
1256 if (url->page != -1) {
1257 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1258 return -1;
1259 sep = "&";
1262 return 0;
1265 int
1266 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1268 struct template *tp = c->tp;
1269 const char *proto = c->https ? "https" : "http";
1271 if (fcgi_puts(tp, proto) == -1 ||
1272 fcgi_puts(tp, "://") == -1 ||
1273 tp_htmlescape(tp, c->server_name) == -1 ||
1274 tp_htmlescape(tp, c->document_uri) == -1)
1275 return -1;
1277 return gotweb_render_url(c, url);
1280 int
1281 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1283 va_list ap;
1284 int r;
1286 if (fcgi_printf(c, "<a href='") == -1)
1287 return -1;
1289 if (gotweb_render_url(c, url) == -1)
1290 return -1;
1292 if (fcgi_printf(c, "'>") == -1)
1293 return -1;
1295 va_start(ap, fmt);
1296 r = fcgi_vprintf(c, fmt, ap);
1297 va_end(ap);
1298 if (r == -1)
1299 return -1;
1301 if (fcgi_printf(c, "</a>"))
1302 return -1;
1303 return 0;
1306 static struct got_repository *
1307 find_cached_repo(struct server *srv, const char *path)
1309 int i;
1311 for (i = 0; i < srv->ncached_repos; i++) {
1312 if (strcmp(srv->cached_repos[i].path, path) == 0)
1313 return srv->cached_repos[i].repo;
1316 return NULL;
1319 static const struct got_error *
1320 cache_repo(struct got_repository **new, struct server *srv,
1321 struct repo_dir *repo_dir, struct socket *sock)
1323 const struct got_error *error = NULL;
1324 struct got_repository *repo;
1325 struct cached_repo *cr;
1326 int evicted = 0;
1328 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1329 cr = &srv->cached_repos[srv->ncached_repos - 1];
1330 error = got_repo_close(cr->repo);
1331 memset(cr, 0, sizeof(*cr));
1332 srv->ncached_repos--;
1333 if (error)
1334 return error;
1335 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1336 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1337 cr = &srv->cached_repos[0];
1338 evicted = 1;
1339 } else {
1340 cr = &srv->cached_repos[srv->ncached_repos];
1343 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1344 if (error) {
1345 if (evicted) {
1346 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1347 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1349 return error;
1352 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1353 >= sizeof(cr->path)) {
1354 if (evicted) {
1355 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1356 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1358 return got_error(GOT_ERR_NO_SPACE);
1361 cr->repo = repo;
1362 srv->ncached_repos++;
1363 *new = repo;
1364 return NULL;
1367 static const struct got_error *
1368 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1370 const struct got_error *error = NULL;
1371 struct socket *sock = c->sock;
1372 struct server *srv = c->srv;
1373 struct transport *t = c->t;
1374 struct got_repository *repo = NULL;
1375 DIR *dt;
1376 char *dir_test;
1378 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1379 GOTWEB_GIT_DIR) == -1)
1380 return got_error_from_errno("asprintf");
1382 dt = opendir(dir_test);
1383 if (dt == NULL) {
1384 free(dir_test);
1385 } else {
1386 repo_dir->path = dir_test;
1387 dir_test = NULL;
1388 goto done;
1391 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1392 repo_dir->name) == -1)
1393 return got_error_from_errno("asprintf");
1395 dt = opendir(dir_test);
1396 if (dt == NULL) {
1397 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1398 goto err;
1399 } else {
1400 repo_dir->path = dir_test;
1401 dir_test = NULL;
1404 done:
1405 if (srv->respect_exportok &&
1406 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1407 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1408 goto err;
1411 repo = find_cached_repo(srv, repo_dir->path);
1412 if (repo == NULL) {
1413 error = cache_repo(&repo, srv, repo_dir, sock);
1414 if (error)
1415 goto err;
1417 t->repo = repo;
1418 error = gotweb_get_repo_description(&repo_dir->description, srv,
1419 repo_dir->path, dirfd(dt));
1420 if (error)
1421 goto err;
1422 error = got_get_repo_owner(&repo_dir->owner, c);
1423 if (error)
1424 goto err;
1425 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1426 if (error)
1427 goto err;
1428 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1429 dirfd(dt));
1430 err:
1431 free(dir_test);
1432 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1433 error = got_error_from_errno("closedir");
1434 return error;
1437 static const struct got_error *
1438 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1440 const struct got_error *error;
1442 *repo_dir = calloc(1, sizeof(**repo_dir));
1443 if (*repo_dir == NULL)
1444 return got_error_from_errno("calloc");
1446 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1447 error = got_error_from_errno("asprintf");
1448 free(*repo_dir);
1449 *repo_dir = NULL;
1450 return error;
1452 (*repo_dir)->owner = NULL;
1453 (*repo_dir)->description = NULL;
1454 (*repo_dir)->url = NULL;
1455 (*repo_dir)->age = NULL;
1456 (*repo_dir)->path = NULL;
1458 return NULL;
1461 static const struct got_error *
1462 gotweb_get_repo_description(char **description, struct server *srv,
1463 const char *dirpath, int dir)
1465 const struct got_error *error = NULL;
1466 struct stat sb;
1467 int fd = -1;
1468 off_t len;
1470 *description = NULL;
1471 if (srv->show_repo_description == 0)
1472 return NULL;
1474 fd = openat(dir, "description", O_RDONLY);
1475 if (fd == -1) {
1476 if (errno != ENOENT && errno != EACCES) {
1477 error = got_error_from_errno_fmt("openat %s/%s",
1478 dirpath, "description");
1480 goto done;
1483 if (fstat(fd, &sb) == -1) {
1484 error = got_error_from_errno_fmt("fstat %s/%s",
1485 dirpath, "description");
1486 goto done;
1489 len = sb.st_size;
1490 if (len > GOTWEBD_MAXDESCRSZ - 1)
1491 len = GOTWEBD_MAXDESCRSZ - 1;
1493 *description = calloc(len + 1, sizeof(**description));
1494 if (*description == NULL) {
1495 error = got_error_from_errno("calloc");
1496 goto done;
1499 if (read(fd, *description, len) == -1)
1500 error = got_error_from_errno("read");
1501 done:
1502 if (fd != -1 && close(fd) == -1 && error == NULL)
1503 error = got_error_from_errno("close");
1504 return error;
1507 static const struct got_error *
1508 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1509 int dir)
1511 const struct got_error *error = NULL;
1512 struct stat sb;
1513 int fd = -1;
1514 off_t len;
1516 *url = NULL;
1517 if (srv->show_repo_cloneurl == 0)
1518 return NULL;
1520 fd = openat(dir, "cloneurl", O_RDONLY);
1521 if (fd == -1) {
1522 if (errno != ENOENT && errno != EACCES) {
1523 error = got_error_from_errno_fmt("openat %s/%s",
1524 dirpath, "cloneurl");
1526 goto done;
1529 if (fstat(fd, &sb) == -1) {
1530 error = got_error_from_errno_fmt("fstat %s/%s",
1531 dirpath, "cloneurl");
1532 goto done;
1535 len = sb.st_size;
1536 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1537 len = GOTWEBD_MAXCLONEURLSZ - 1;
1539 *url = calloc(len + 1, sizeof(**url));
1540 if (*url == NULL) {
1541 error = got_error_from_errno("calloc");
1542 goto done;
1545 if (read(fd, *url, len) == -1)
1546 error = got_error_from_errno("read");
1547 done:
1548 if (fd != -1 && close(fd) == -1 && error == NULL)
1549 error = got_error_from_errno("close");
1550 return error;
1553 const struct got_error *
1554 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1556 struct tm tm;
1557 long long diff_time;
1558 const char *years = "years ago", *months = "months ago";
1559 const char *weeks = "weeks ago", *days = "days ago";
1560 const char *hours = "hours ago", *minutes = "minutes ago";
1561 const char *seconds = "seconds ago", *now = "right now";
1562 char *s;
1563 char datebuf[64];
1564 size_t r;
1566 *repo_age = NULL;
1568 switch (ref_tm) {
1569 case TM_DIFF:
1570 diff_time = time(NULL) - committer_time;
1571 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1572 if (asprintf(repo_age, "%lld %s",
1573 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1574 return got_error_from_errno("asprintf");
1575 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1576 if (asprintf(repo_age, "%lld %s",
1577 (diff_time / 60 / 60 / 24 / (365 / 12)),
1578 months) == -1)
1579 return got_error_from_errno("asprintf");
1580 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1581 if (asprintf(repo_age, "%lld %s",
1582 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1583 return got_error_from_errno("asprintf");
1584 } else if (diff_time > 60 * 60 * 24 * 2) {
1585 if (asprintf(repo_age, "%lld %s",
1586 (diff_time / 60 / 60 / 24), days) == -1)
1587 return got_error_from_errno("asprintf");
1588 } else if (diff_time > 60 * 60 * 2) {
1589 if (asprintf(repo_age, "%lld %s",
1590 (diff_time / 60 / 60), hours) == -1)
1591 return got_error_from_errno("asprintf");
1592 } else if (diff_time > 60 * 2) {
1593 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1594 minutes) == -1)
1595 return got_error_from_errno("asprintf");
1596 } else if (diff_time > 2) {
1597 if (asprintf(repo_age, "%lld %s", diff_time,
1598 seconds) == -1)
1599 return got_error_from_errno("asprintf");
1600 } else {
1601 if (asprintf(repo_age, "%s", now) == -1)
1602 return got_error_from_errno("asprintf");
1604 break;
1605 case TM_LONG:
1606 if (gmtime_r(&committer_time, &tm) == NULL)
1607 return got_error_from_errno("gmtime_r");
1609 s = asctime_r(&tm, datebuf);
1610 if (s == NULL)
1611 return got_error_from_errno("asctime_r");
1613 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1614 return got_error_from_errno("asprintf");
1615 break;
1616 case TM_RFC822:
1617 if (gmtime_r(&committer_time, &tm) == NULL)
1618 return got_error_from_errno("gmtime_r");
1620 r = strftime(datebuf, sizeof(datebuf),
1621 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1622 if (r == 0)
1623 return got_error(GOT_ERR_NO_SPACE);
1625 *repo_age = strdup(datebuf);
1626 if (*repo_age == NULL)
1627 return got_error_from_errno("asprintf");
1628 break;
1630 return NULL;