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 static inline int
1005 should_urlencode(int c)
1007 if (c <= ' ' || c >= 127)
1008 return 1;
1010 switch (c) {
1011 /* gen-delim */
1012 case ':':
1013 case '/':
1014 case '?':
1015 case '#':
1016 case '[':
1017 case ']':
1018 case '@':
1019 /* sub-delims */
1020 case '!':
1021 case '$':
1022 case '&':
1023 case '\'':
1024 case '(':
1025 case ')':
1026 case '*':
1027 case '+':
1028 case ',':
1029 case ';':
1030 case '=':
1031 /* needed because the URLs are embedded into the HTML */
1032 case '\"':
1033 return 1;
1034 default:
1035 return 0;
1039 static char *
1040 gotweb_urlencode(const char *str)
1042 const char *s;
1043 char *escaped;
1044 size_t i, len;
1045 int a, b;
1047 len = 0;
1048 for (s = str; *s; ++s) {
1049 len++;
1050 if (should_urlencode(*s))
1051 len += 2;
1054 escaped = calloc(1, len + 1);
1055 if (escaped == NULL)
1056 return NULL;
1058 i = 0;
1059 for (s = str; *s; ++s) {
1060 if (should_urlencode(*s)) {
1061 a = (*s & 0xF0) >> 4;
1062 b = (*s & 0x0F);
1064 escaped[i++] = '%';
1065 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1066 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1067 } else
1068 escaped[i++] = *s;
1071 return escaped;
1074 const char *
1075 gotweb_action_name(int action)
1077 switch (action) {
1078 case BLAME:
1079 return "blame";
1080 case BLOB:
1081 return "blob";
1082 case BLOBRAW:
1083 return "blobraw";
1084 case BRIEFS:
1085 return "briefs";
1086 case COMMITS:
1087 return "commits";
1088 case DIFF:
1089 return "diff";
1090 case ERR:
1091 return "err";
1092 case INDEX:
1093 return "index";
1094 case SUMMARY:
1095 return "summary";
1096 case TAG:
1097 return "tag";
1098 case TAGS:
1099 return "tags";
1100 case TREE:
1101 return "tree";
1102 case RSS:
1103 return "rss";
1104 default:
1105 return NULL;
1109 int
1110 gotweb_render_url(struct request *c, struct gotweb_url *url)
1112 const char *sep = "?", *action;
1113 char *tmp;
1114 int r;
1116 action = gotweb_action_name(url->action);
1117 if (action != NULL) {
1118 if (fcgi_printf(c, "?action=%s", action) == -1)
1119 return -1;
1120 sep = "&";
1123 if (url->commit) {
1124 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1125 return -1;
1126 sep = "&";
1129 if (url->previd) {
1130 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1131 return -1;
1132 sep = "&";
1135 if (url->prevset) {
1136 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1137 return -1;
1138 sep = "&";
1141 if (url->file) {
1142 tmp = gotweb_urlencode(url->file);
1143 if (tmp == NULL)
1144 return -1;
1145 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1146 free(tmp);
1147 if (r == -1)
1148 return -1;
1149 sep = "&";
1152 if (url->folder) {
1153 tmp = gotweb_urlencode(url->folder);
1154 if (tmp == NULL)
1155 return -1;
1156 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1157 free(tmp);
1158 if (r == -1)
1159 return -1;
1160 sep = "&";
1163 if (url->headref) {
1164 tmp = gotweb_urlencode(url->headref);
1165 if (tmp == NULL)
1166 return -1;
1167 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1168 free(tmp);
1169 if (r == -1)
1170 return -1;
1171 sep = "&";
1174 if (url->index_page != -1) {
1175 if (fcgi_printf(c, "%sindex_page=%d", sep,
1176 url->index_page) == -1)
1177 return -1;
1178 sep = "&";
1181 if (url->path) {
1182 tmp = gotweb_urlencode(url->path);
1183 if (tmp == NULL)
1184 return -1;
1185 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1186 free(tmp);
1187 if (r == -1)
1188 return -1;
1189 sep = "&";
1192 if (url->page != -1) {
1193 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1194 return -1;
1195 sep = "&";
1198 return 0;
1201 int
1202 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1204 struct template *tp = c->tp;
1205 const char *proto = c->https ? "https" : "http";
1207 if (fcgi_puts(tp, proto) == -1 ||
1208 fcgi_puts(tp, "://") == -1 ||
1209 tp_htmlescape(tp, c->server_name) == -1 ||
1210 tp_htmlescape(tp, c->document_uri) == -1)
1211 return -1;
1213 return gotweb_render_url(c, url);
1216 static struct got_repository *
1217 find_cached_repo(struct server *srv, const char *path)
1219 int i;
1221 for (i = 0; i < srv->ncached_repos; i++) {
1222 if (strcmp(srv->cached_repos[i].path, path) == 0)
1223 return srv->cached_repos[i].repo;
1226 return NULL;
1229 static const struct got_error *
1230 cache_repo(struct got_repository **new, struct server *srv,
1231 struct repo_dir *repo_dir, struct socket *sock)
1233 const struct got_error *error = NULL;
1234 struct got_repository *repo;
1235 struct cached_repo *cr;
1236 int evicted = 0;
1238 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1239 cr = &srv->cached_repos[srv->ncached_repos - 1];
1240 error = got_repo_close(cr->repo);
1241 memset(cr, 0, sizeof(*cr));
1242 srv->ncached_repos--;
1243 if (error)
1244 return error;
1245 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1246 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1247 cr = &srv->cached_repos[0];
1248 evicted = 1;
1249 } else {
1250 cr = &srv->cached_repos[srv->ncached_repos];
1253 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1254 if (error) {
1255 if (evicted) {
1256 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1257 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1259 return error;
1262 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1263 >= sizeof(cr->path)) {
1264 if (evicted) {
1265 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1266 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1268 return got_error(GOT_ERR_NO_SPACE);
1271 cr->repo = repo;
1272 srv->ncached_repos++;
1273 *new = repo;
1274 return NULL;
1277 static const struct got_error *
1278 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1280 const struct got_error *error = NULL;
1281 struct socket *sock = c->sock;
1282 struct server *srv = c->srv;
1283 struct transport *t = c->t;
1284 struct got_repository *repo = NULL;
1285 DIR *dt;
1286 char *dir_test;
1288 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1289 GOTWEB_GIT_DIR) == -1)
1290 return got_error_from_errno("asprintf");
1292 dt = opendir(dir_test);
1293 if (dt == NULL) {
1294 free(dir_test);
1295 } else {
1296 repo_dir->path = dir_test;
1297 dir_test = NULL;
1298 goto done;
1301 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1302 repo_dir->name) == -1)
1303 return got_error_from_errno("asprintf");
1305 dt = opendir(dir_test);
1306 if (dt == NULL) {
1307 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1308 goto err;
1309 } else {
1310 repo_dir->path = dir_test;
1311 dir_test = NULL;
1314 done:
1315 if (srv->respect_exportok &&
1316 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1317 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1318 goto err;
1321 repo = find_cached_repo(srv, repo_dir->path);
1322 if (repo == NULL) {
1323 error = cache_repo(&repo, srv, repo_dir, sock);
1324 if (error)
1325 goto err;
1327 t->repo = repo;
1328 error = gotweb_get_repo_description(&repo_dir->description, srv,
1329 repo_dir->path, dirfd(dt));
1330 if (error)
1331 goto err;
1332 error = got_get_repo_owner(&repo_dir->owner, c);
1333 if (error)
1334 goto err;
1335 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1336 if (error)
1337 goto err;
1338 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1339 dirfd(dt));
1340 err:
1341 free(dir_test);
1342 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1343 error = got_error_from_errno("closedir");
1344 return error;
1347 static const struct got_error *
1348 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1350 const struct got_error *error;
1352 *repo_dir = calloc(1, sizeof(**repo_dir));
1353 if (*repo_dir == NULL)
1354 return got_error_from_errno("calloc");
1356 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1357 error = got_error_from_errno("asprintf");
1358 free(*repo_dir);
1359 *repo_dir = NULL;
1360 return error;
1362 (*repo_dir)->owner = NULL;
1363 (*repo_dir)->description = NULL;
1364 (*repo_dir)->url = NULL;
1365 (*repo_dir)->age = NULL;
1366 (*repo_dir)->path = NULL;
1368 return NULL;
1371 static const struct got_error *
1372 gotweb_get_repo_description(char **description, struct server *srv,
1373 const char *dirpath, int dir)
1375 const struct got_error *error = NULL;
1376 struct stat sb;
1377 int fd = -1;
1378 off_t len;
1380 *description = NULL;
1381 if (srv->show_repo_description == 0)
1382 return NULL;
1384 fd = openat(dir, "description", O_RDONLY);
1385 if (fd == -1) {
1386 if (errno != ENOENT && errno != EACCES) {
1387 error = got_error_from_errno_fmt("openat %s/%s",
1388 dirpath, "description");
1390 goto done;
1393 if (fstat(fd, &sb) == -1) {
1394 error = got_error_from_errno_fmt("fstat %s/%s",
1395 dirpath, "description");
1396 goto done;
1399 len = sb.st_size;
1400 if (len > GOTWEBD_MAXDESCRSZ - 1)
1401 len = GOTWEBD_MAXDESCRSZ - 1;
1403 *description = calloc(len + 1, sizeof(**description));
1404 if (*description == NULL) {
1405 error = got_error_from_errno("calloc");
1406 goto done;
1409 if (read(fd, *description, len) == -1)
1410 error = got_error_from_errno("read");
1411 done:
1412 if (fd != -1 && close(fd) == -1 && error == NULL)
1413 error = got_error_from_errno("close");
1414 return error;
1417 static const struct got_error *
1418 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1419 int dir)
1421 const struct got_error *error = NULL;
1422 struct stat sb;
1423 int fd = -1;
1424 off_t len;
1426 *url = NULL;
1427 if (srv->show_repo_cloneurl == 0)
1428 return NULL;
1430 fd = openat(dir, "cloneurl", O_RDONLY);
1431 if (fd == -1) {
1432 if (errno != ENOENT && errno != EACCES) {
1433 error = got_error_from_errno_fmt("openat %s/%s",
1434 dirpath, "cloneurl");
1436 goto done;
1439 if (fstat(fd, &sb) == -1) {
1440 error = got_error_from_errno_fmt("fstat %s/%s",
1441 dirpath, "cloneurl");
1442 goto done;
1445 len = sb.st_size;
1446 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1447 len = GOTWEBD_MAXCLONEURLSZ - 1;
1449 *url = calloc(len + 1, sizeof(**url));
1450 if (*url == NULL) {
1451 error = got_error_from_errno("calloc");
1452 goto done;
1455 if (read(fd, *url, len) == -1)
1456 error = got_error_from_errno("read");
1457 done:
1458 if (fd != -1 && close(fd) == -1 && error == NULL)
1459 error = got_error_from_errno("close");
1460 return error;
1463 const struct got_error *
1464 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1466 struct tm tm;
1467 long long diff_time;
1468 const char *years = "years ago", *months = "months ago";
1469 const char *weeks = "weeks ago", *days = "days ago";
1470 const char *hours = "hours ago", *minutes = "minutes ago";
1471 const char *seconds = "seconds ago", *now = "right now";
1472 char *s;
1473 char datebuf[64];
1474 size_t r;
1476 *repo_age = NULL;
1478 switch (ref_tm) {
1479 case TM_DIFF:
1480 diff_time = time(NULL) - committer_time;
1481 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1482 if (asprintf(repo_age, "%lld %s",
1483 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1484 return got_error_from_errno("asprintf");
1485 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1486 if (asprintf(repo_age, "%lld %s",
1487 (diff_time / 60 / 60 / 24 / (365 / 12)),
1488 months) == -1)
1489 return got_error_from_errno("asprintf");
1490 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1491 if (asprintf(repo_age, "%lld %s",
1492 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1493 return got_error_from_errno("asprintf");
1494 } else if (diff_time > 60 * 60 * 24 * 2) {
1495 if (asprintf(repo_age, "%lld %s",
1496 (diff_time / 60 / 60 / 24), days) == -1)
1497 return got_error_from_errno("asprintf");
1498 } else if (diff_time > 60 * 60 * 2) {
1499 if (asprintf(repo_age, "%lld %s",
1500 (diff_time / 60 / 60), hours) == -1)
1501 return got_error_from_errno("asprintf");
1502 } else if (diff_time > 60 * 2) {
1503 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1504 minutes) == -1)
1505 return got_error_from_errno("asprintf");
1506 } else if (diff_time > 2) {
1507 if (asprintf(repo_age, "%lld %s", diff_time,
1508 seconds) == -1)
1509 return got_error_from_errno("asprintf");
1510 } else {
1511 if (asprintf(repo_age, "%s", now) == -1)
1512 return got_error_from_errno("asprintf");
1514 break;
1515 case TM_LONG:
1516 if (gmtime_r(&committer_time, &tm) == NULL)
1517 return got_error_from_errno("gmtime_r");
1519 s = asctime_r(&tm, datebuf);
1520 if (s == NULL)
1521 return got_error_from_errno("asctime_r");
1523 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1524 return got_error_from_errno("asprintf");
1525 break;
1526 case TM_RFC822:
1527 if (gmtime_r(&committer_time, &tm) == NULL)
1528 return got_error_from_errno("gmtime_r");
1530 r = strftime(datebuf, sizeof(datebuf),
1531 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1532 if (r == 0)
1533 return got_error(GOT_ERR_NO_SPACE);
1535 *repo_age = strdup(datebuf);
1536 if (*repo_age == NULL)
1537 return got_error_from_errno("asprintf");
1538 break;
1540 return NULL;