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 <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_diff.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
50 #include "got_compat.h"
52 #include "proc.h"
53 #include "gotwebd.h"
54 #include "tmpl.h"
56 static const struct querystring_keys querystring_keys[] = {
57 { "action", ACTION },
58 { "commit", COMMIT },
59 { "file", RFILE },
60 { "folder", FOLDER },
61 { "headref", HEADREF },
62 { "index_page", INDEX_PAGE },
63 { "path", PATH },
64 { "page", PAGE },
65 };
67 static const struct action_keys action_keys[] = {
68 { "blame", BLAME },
69 { "blob", BLOB },
70 { "blobraw", BLOBRAW },
71 { "briefs", BRIEFS },
72 { "commits", COMMITS },
73 { "diff", DIFF },
74 { "error", ERR },
75 { "index", INDEX },
76 { "summary", SUMMARY },
77 { "tag", TAG },
78 { "tags", TAGS },
79 { "tree", TREE },
80 { "rss", RSS },
81 };
83 static const struct got_error *gotweb_init_querystring(struct querystring **);
84 static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 char *);
86 static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 char *, char *);
88 static const struct got_error *gotweb_render_index(struct request *);
89 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 const char *);
91 static const struct got_error *gotweb_load_got_path(struct request *c,
92 struct repo_dir *);
93 static const struct got_error *gotweb_get_repo_description(char **,
94 struct server *, const char *, int);
95 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 const char *, int);
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 static int
104 gotweb_reply(struct request *c, int status, const char *ctype,
105 struct gotweb_url *location)
107 const char *csp;
109 if (status != 200 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
110 return -1;
112 if (location) {
113 if (fcgi_puts(c->tp, "Location: ") == -1 ||
114 gotweb_render_url(c, location) == -1 ||
115 fcgi_puts(c->tp, "\r\n") == -1)
116 return -1;
119 csp = "Content-Security-Policy: default-src 'self'; "
120 "script-src 'none'; object-src 'none';\r\n";
121 if (fcgi_puts(c->tp, csp) == -1)
122 return -1;
124 if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
125 return -1;
127 return fcgi_puts(c->tp, "\r\n");
130 static int
131 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
132 const char *suffix)
134 int r;
136 r = fcgi_printf(c, "Content-Disposition: attachment; "
137 "filename=%s%s\r\n", file, suffix ? suffix : "");
138 if (r == -1)
139 return -1;
140 return gotweb_reply(c, 200, ctype, NULL);
143 void
144 gotweb_process_request(struct request *c)
146 const struct got_error *error = NULL, *error2 = NULL;
147 struct got_blob_object *blob = NULL;
148 struct server *srv = NULL;
149 struct querystring *qs = NULL;
150 struct repo_dir *repo_dir = NULL;
151 struct got_reflist_head refs;
152 FILE *fp = NULL;
153 uint8_t err[] = "gotwebd experienced an error: ";
154 int r, html = 0, fd = -1;
156 TAILQ_INIT(&refs);
158 /* init the transport */
159 error = gotweb_init_transport(&c->t);
160 if (error) {
161 log_warnx("%s: %s", __func__, error->msg);
162 return;
164 /* don't process any further if client disconnected */
165 if (c->sock->client_status == CLIENT_DISCONNECT)
166 return;
167 /* get the gotwebd server */
168 srv = gotweb_get_server(c->server_name, c->http_host);
169 if (srv == NULL) {
170 log_warnx("%s: error server is NULL", __func__);
171 goto err;
173 c->srv = srv;
174 /* parse our querystring */
175 error = gotweb_init_querystring(&qs);
176 if (error) {
177 log_warnx("%s: %s", __func__, error->msg);
178 goto err;
180 c->t->qs = qs;
181 error = gotweb_parse_querystring(&qs, c->querystring);
182 if (error) {
183 log_warnx("%s: %s", __func__, error->msg);
184 goto err;
187 /*
188 * certain actions require a commit id in the querystring. this stops
189 * bad actors from exploiting this by manually manipulating the
190 * querystring.
191 */
193 if (qs->action == BLAME || qs->action == BLOB ||
194 qs->action == BLOBRAW || qs->action == DIFF) {
195 if (qs->commit == NULL) {
196 error2 = got_error(GOT_ERR_QUERYSTRING);
197 goto render;
201 if (qs->action != INDEX) {
202 error = gotweb_init_repo_dir(&repo_dir, qs->path);
203 if (error)
204 goto done;
205 error = gotweb_load_got_path(c, repo_dir);
206 c->t->repo_dir = repo_dir;
207 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
208 goto err;
211 if (qs->action == BLOBRAW) {
212 const uint8_t *buf;
213 size_t len;
214 int binary, r;
216 error = got_get_repo_commits(c, 1);
217 if (error)
218 goto done;
220 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
221 if (error2)
222 goto render;
224 if (binary)
225 r = gotweb_reply_file(c, "application/octet-stream",
226 qs->file, NULL);
227 else
228 r = gotweb_reply(c, 200, "text/plain", NULL);
229 if (r == -1)
230 goto done;
232 for (;;) {
233 error = got_object_blob_read_block(&len, blob);
234 if (error)
235 goto done;
236 if (len == 0)
237 break;
238 buf = got_object_blob_get_read_buf(blob);
239 if (fcgi_gen_binary_response(c, buf, len) == -1)
240 goto done;
243 goto done;
246 if (qs->action == BLOB) {
247 int binary;
248 struct gotweb_url url = {
249 .index_page = -1,
250 .page = -1,
251 .action = BLOBRAW,
252 .path = qs->path,
253 .commit = qs->commit,
254 .folder = qs->folder,
255 .file = qs->file,
256 };
258 error = got_get_repo_commits(c, 1);
259 if (error)
260 goto done;
262 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
263 if (error2)
264 goto render;
265 if (binary) {
266 gotweb_reply(c, 302, NULL, &url);
267 goto done;
271 if (qs->action == RSS) {
272 const char *ctype = "application/rss+xml;charset=utf-8";
274 if (gotweb_reply_file(c, ctype, repo_dir->name, ".rss") == -1)
275 goto done;
277 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
278 if (error) {
279 log_warnx("%s: %s", __func__, error->msg);
280 goto err;
282 if (gotweb_render_rss(c->tp) == -1)
283 goto err;
284 goto done;
287 render:
288 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
289 goto done;
290 html = 1;
292 if (gotweb_render_header(c->tp) == -1)
293 goto err;
295 if (error2) {
296 error = error2;
297 goto err;
300 switch(qs->action) {
301 case BLAME:
302 error = got_get_repo_commits(c, 1);
303 if (error) {
304 log_warnx("%s: %s", __func__, error->msg);
305 goto err;
307 if (gotweb_render_blame(c->tp) == -1)
308 goto done;
309 break;
310 case BLOB:
311 if (gotweb_render_blob(c->tp, blob) == -1)
312 goto err;
313 break;
314 case BRIEFS:
315 if (gotweb_render_briefs(c->tp) == -1)
316 goto err;
317 break;
318 case COMMITS:
319 error = got_get_repo_commits(c, srv->max_commits_display);
320 if (error) {
321 log_warnx("%s: %s", __func__, error->msg);
322 goto err;
324 if (gotweb_render_commits(c->tp) == -1)
325 goto err;
326 break;
327 case DIFF:
328 error = got_get_repo_commits(c, 1);
329 if (error) {
330 log_warnx("%s: %s", __func__, error->msg);
331 goto err;
333 error = got_open_diff_for_output(&fp, &fd, c);
334 if (error) {
335 log_warnx("%s: %s", __func__, error->msg);
336 goto err;
338 if (gotweb_render_diff(c->tp, fp) == -1)
339 goto err;
340 break;
341 case INDEX:
342 error = gotweb_render_index(c);
343 if (error) {
344 log_warnx("%s: %s", __func__, error->msg);
345 goto err;
347 break;
348 case SUMMARY:
349 error = got_ref_list(&refs, c->t->repo, "refs/heads",
350 got_ref_cmp_by_name, NULL);
351 if (error) {
352 log_warnx("%s: got_ref_list: %s", __func__,
353 error->msg);
354 goto err;
356 qs->action = TAGS;
357 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
358 if (error) {
359 log_warnx("%s: got_get_repo_tags: %s", __func__,
360 error->msg);
361 goto err;
363 qs->action = SUMMARY;
364 if (gotweb_render_summary(c->tp, &refs) == -1)
365 goto done;
366 break;
367 case TAG:
368 error = got_get_repo_tags(c, 1);
369 if (error) {
370 log_warnx("%s: %s", __func__, error->msg);
371 goto err;
373 if (c->t->tag_count == 0) {
374 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
375 "bad commit id");
376 goto err;
378 if (gotweb_render_tag(c->tp) == -1)
379 goto done;
380 break;
381 case TAGS:
382 error = got_get_repo_tags(c, srv->max_commits_display);
383 if (error) {
384 log_warnx("%s: %s", __func__, error->msg);
385 goto err;
387 if (gotweb_render_tags(c->tp) == -1)
388 goto done;
389 break;
390 case TREE:
391 error = got_get_repo_commits(c, 1);
392 if (error) {
393 log_warnx("%s: %s", __func__, error->msg);
394 goto err;
396 if (gotweb_render_tree(c->tp) == -1)
397 goto err;
398 break;
399 case ERR:
400 default:
401 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
402 "Erorr: Bad Querystring");
403 if (r == -1)
404 goto err;
405 break;
408 goto done;
409 err:
410 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
411 return;
412 if (fcgi_printf(c, "\n%s", err) == -1)
413 return;
414 if (error) {
415 if (fcgi_printf(c, "%s", error->msg) == -1)
416 return;
417 } else {
418 if (fcgi_printf(c, "see daemon logs for details") == -1)
419 return;
421 if (html && fcgi_printf(c, "</div>\n") == -1)
422 return;
423 done:
424 if (blob)
425 got_object_blob_close(blob);
426 if (fp) {
427 error = got_gotweb_flushfile(fp, fd);
428 if (error)
429 log_warnx("%s: got_gotweb_flushfile failure: %s",
430 __func__, error->msg);
431 fd = -1;
433 if (fd != -1)
434 close(fd);
435 if (html && srv != NULL)
436 gotweb_render_footer(c->tp);
438 got_ref_list_free(&refs);
441 struct server *
442 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
444 struct server *srv = NULL;
446 /* check against the server name first */
447 if (strlen(server_name) > 0)
448 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
449 if (strcmp(srv->name, server_name) == 0)
450 goto done;
452 /* check against subdomain second */
453 if (strlen(subdomain) > 0)
454 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
455 if (strcmp(srv->name, subdomain) == 0)
456 goto done;
458 /* if those fail, send first server */
459 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
460 if (srv != NULL)
461 break;
462 done:
463 return srv;
464 };
466 const struct got_error *
467 gotweb_init_transport(struct transport **t)
469 const struct got_error *error = NULL;
471 *t = calloc(1, sizeof(**t));
472 if (*t == NULL)
473 return got_error_from_errno2("%s: calloc", __func__);
475 TAILQ_INIT(&(*t)->repo_commits);
476 TAILQ_INIT(&(*t)->repo_tags);
478 (*t)->repo = NULL;
479 (*t)->repo_dir = NULL;
480 (*t)->qs = NULL;
481 (*t)->next_id = NULL;
482 (*t)->prev_id = NULL;
483 (*t)->next_disp = 0;
484 (*t)->prev_disp = 0;
486 return error;
489 static const struct got_error *
490 gotweb_init_querystring(struct querystring **qs)
492 const struct got_error *error = NULL;
494 *qs = calloc(1, sizeof(**qs));
495 if (*qs == NULL)
496 return got_error_from_errno2("%s: calloc", __func__);
498 (*qs)->headref = strdup("HEAD");
499 if ((*qs)->headref == NULL) {
500 free(*qs);
501 *qs = NULL;
502 return got_error_from_errno2("%s: strdup", __func__);
505 (*qs)->action = INDEX;
506 (*qs)->commit = NULL;
507 (*qs)->file = NULL;
508 (*qs)->folder = NULL;
509 (*qs)->index_page = 0;
510 (*qs)->path = NULL;
512 return error;
515 static const struct got_error *
516 gotweb_parse_querystring(struct querystring **qs, char *qst)
518 const struct got_error *error = NULL;
519 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
520 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
522 if (qst == NULL)
523 return error;
525 tok1 = strdup(qst);
526 if (tok1 == NULL)
527 return got_error_from_errno2("%s: strdup", __func__);
529 tok1_pair = tok1;
530 tok1_end = tok1;
532 while (tok1_pair != NULL) {
533 strsep(&tok1_end, "&");
535 tok2 = strdup(tok1_pair);
536 if (tok2 == NULL) {
537 free(tok1);
538 return got_error_from_errno2("%s: strdup", __func__);
541 tok2_pair = tok2;
542 tok2_end = tok2;
544 while (tok2_pair != NULL) {
545 strsep(&tok2_end, "=");
546 if (tok2_end) {
547 error = gotweb_assign_querystring(qs, tok2_pair,
548 tok2_end);
549 if (error)
550 goto err;
552 tok2_pair = tok2_end;
554 free(tok2);
555 tok1_pair = tok1_end;
557 free(tok1);
558 return error;
559 err:
560 free(tok2);
561 free(tok1);
562 return error;
565 /*
566 * Adapted from usr.sbin/httpd/httpd.c url_decode.
567 */
568 static const struct got_error *
569 gotweb_urldecode(char *url)
571 char *p, *q;
572 char hex[3];
573 unsigned long x;
575 hex[2] = '\0';
576 p = q = url;
578 while (*p != '\0') {
579 switch (*p) {
580 case '%':
581 /* Encoding character is followed by two hex chars */
582 if (!isxdigit((unsigned char)p[1]) ||
583 !isxdigit((unsigned char)p[2]) ||
584 (p[1] == '0' && p[2] == '0'))
585 return got_error(GOT_ERR_BAD_QUERYSTRING);
587 hex[0] = p[1];
588 hex[1] = p[2];
590 /*
591 * We don't have to validate "hex" because it is
592 * guaranteed to include two hex chars followed by nul.
593 */
594 x = strtoul(hex, NULL, 16);
595 *q = (char)x;
596 p += 2;
597 break;
598 default:
599 *q = *p;
600 break;
602 p++;
603 q++;
605 *q = '\0';
607 return NULL;
610 static const struct got_error *
611 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
613 const struct got_error *error = NULL;
614 const char *errstr;
615 int a_cnt, el_cnt;
617 error = gotweb_urldecode(value);
618 if (error)
619 return error;
621 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
622 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
623 continue;
625 switch (querystring_keys[el_cnt].element) {
626 case ACTION:
627 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
628 if (strcmp(value, action_keys[a_cnt].name) != 0)
629 continue;
630 else if (strcmp(value,
631 action_keys[a_cnt].name) == 0){
632 (*qs)->action =
633 action_keys[a_cnt].action;
634 goto qa_found;
637 (*qs)->action = ERR;
638 qa_found:
639 break;
640 case COMMIT:
641 (*qs)->commit = strdup(value);
642 if ((*qs)->commit == NULL) {
643 error = got_error_from_errno2("%s: strdup",
644 __func__);
645 goto done;
647 break;
648 case RFILE:
649 (*qs)->file = strdup(value);
650 if ((*qs)->file == NULL) {
651 error = got_error_from_errno2("%s: strdup",
652 __func__);
653 goto done;
655 break;
656 case FOLDER:
657 (*qs)->folder = strdup(value);
658 if ((*qs)->folder == NULL) {
659 error = got_error_from_errno2("%s: strdup",
660 __func__);
661 goto done;
663 break;
664 case HEADREF:
665 free((*qs)->headref);
666 (*qs)->headref = strdup(value);
667 if ((*qs)->headref == NULL) {
668 error = got_error_from_errno2("%s: strdup",
669 __func__);
670 goto done;
672 break;
673 case INDEX_PAGE:
674 if (strlen(value) == 0)
675 break;
676 (*qs)->index_page = strtonum(value, INT64_MIN,
677 INT64_MAX, &errstr);
678 if (errstr) {
679 error = got_error_from_errno3("%s: strtonum %s",
680 __func__, errstr);
681 goto done;
683 if ((*qs)->index_page < 0)
684 (*qs)->index_page = 0;
685 break;
686 case PATH:
687 (*qs)->path = strdup(value);
688 if ((*qs)->path == NULL) {
689 error = got_error_from_errno2("%s: strdup",
690 __func__);
691 goto done;
693 break;
694 case PAGE:
695 if (strlen(value) == 0)
696 break;
697 (*qs)->page = strtonum(value, INT64_MIN,
698 INT64_MAX, &errstr);
699 if (errstr) {
700 error = got_error_from_errno3("%s: strtonum %s",
701 __func__, errstr);
702 goto done;
704 if ((*qs)->page < 0)
705 (*qs)->page = 0;
706 break;
707 default:
708 break;
711 done:
712 return error;
715 void
716 gotweb_free_repo_tag(struct repo_tag *rt)
718 if (rt != NULL) {
719 free(rt->commit_id);
720 free(rt->tag_name);
721 free(rt->tag_commit);
722 free(rt->commit_msg);
723 free(rt->tagger);
725 free(rt);
728 void
729 gotweb_free_repo_commit(struct repo_commit *rc)
731 if (rc != NULL) {
732 free(rc->path);
733 free(rc->refs_str);
734 free(rc->commit_id);
735 free(rc->parent_id);
736 free(rc->tree_id);
737 free(rc->author);
738 free(rc->committer);
739 free(rc->commit_msg);
741 free(rc);
744 static void
745 gotweb_free_querystring(struct querystring *qs)
747 if (qs != NULL) {
748 free(qs->commit);
749 free(qs->file);
750 free(qs->folder);
751 free(qs->headref);
752 free(qs->path);
754 free(qs);
757 static void
758 gotweb_free_repo_dir(struct repo_dir *repo_dir)
760 if (repo_dir != NULL) {
761 free(repo_dir->name);
762 free(repo_dir->owner);
763 free(repo_dir->description);
764 free(repo_dir->url);
765 free(repo_dir->path);
767 free(repo_dir);
770 void
771 gotweb_free_transport(struct transport *t)
773 struct repo_commit *rc = NULL, *trc = NULL;
774 struct repo_tag *rt = NULL, *trt = NULL;
776 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
777 TAILQ_REMOVE(&t->repo_commits, rc, entry);
778 gotweb_free_repo_commit(rc);
780 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
781 TAILQ_REMOVE(&t->repo_tags, rt, entry);
782 gotweb_free_repo_tag(rt);
784 gotweb_free_repo_dir(t->repo_dir);
785 gotweb_free_querystring(t->qs);
786 free(t->next_id);
787 free(t->prev_id);
788 free(t);
791 void
792 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
793 struct gotweb_url *next, int *have_next)
795 struct transport *t = c->t;
796 struct querystring *qs = t->qs;
797 struct server *srv = c->srv;
799 *have_prev = *have_next = 0;
801 switch(qs->action) {
802 case INDEX:
803 if (qs->index_page > 0) {
804 *have_prev = 1;
805 *prev = (struct gotweb_url){
806 .action = -1,
807 .index_page = qs->index_page - 1,
808 .page = -1,
809 };
811 if (t->next_disp == srv->max_repos_display &&
812 t->repos_total != (qs->index_page + 1) *
813 srv->max_repos_display) {
814 *have_next = 1;
815 *next = (struct gotweb_url){
816 .action = -1,
817 .index_page = qs->index_page + 1,
818 .page = -1,
819 };
821 break;
822 case BRIEFS:
823 if (t->prev_id && qs->commit != NULL &&
824 strcmp(qs->commit, t->prev_id) != 0) {
825 *have_prev = 1;
826 *prev = (struct gotweb_url){
827 .action = BRIEFS,
828 .index_page = -1,
829 .page = qs->page - 1,
830 .path = qs->path,
831 .commit = t->prev_id,
832 .headref = qs->headref,
833 };
835 if (t->next_id) {
836 *have_next = 1;
837 *next = (struct gotweb_url){
838 .action = BRIEFS,
839 .index_page = -1,
840 .page = qs->page + 1,
841 .path = qs->path,
842 .commit = t->next_id,
843 .headref = qs->headref,
844 };
846 break;
847 case COMMITS:
848 if (t->prev_id && qs->commit != NULL &&
849 strcmp(qs->commit, t->prev_id) != 0) {
850 *have_prev = 1;
851 *prev = (struct gotweb_url){
852 .action = COMMITS,
853 .index_page = -1,
854 .page = qs->page - 1,
855 .path = qs->path,
856 .commit = t->prev_id,
857 .headref = qs->headref,
858 .folder = qs->folder,
859 .file = qs->file,
860 };
862 if (t->next_id) {
863 *have_next = 1;
864 *next = (struct gotweb_url){
865 .action = COMMITS,
866 .index_page = -1,
867 .page = qs->page + 1,
868 .path = qs->path,
869 .commit = t->next_id,
870 .headref = qs->headref,
871 .folder = qs->folder,
872 .file = qs->file,
873 };
875 break;
876 case TAGS:
877 if (t->prev_id && qs->commit != NULL &&
878 strcmp(qs->commit, t->prev_id) != 0) {
879 *have_prev = 1;
880 *prev = (struct gotweb_url){
881 .action = TAGS,
882 .index_page = -1,
883 .page = qs->page - 1,
884 .path = qs->path,
885 .commit = t->prev_id,
886 .headref = qs->headref,
887 };
889 if (t->next_id) {
890 *have_next = 1;
891 *next = (struct gotweb_url){
892 .action = TAGS,
893 .index_page = -1,
894 .page = qs->page + 1,
895 .path = qs->path,
896 .commit = t->next_id,
897 .headref = qs->headref,
898 };
900 break;
904 static const struct got_error *
905 gotweb_render_index(struct request *c)
907 const struct got_error *error = NULL;
908 struct server *srv = c->srv;
909 struct transport *t = c->t;
910 struct querystring *qs = t->qs;
911 struct repo_dir *repo_dir = NULL;
912 DIR *d;
913 struct dirent **sd_dent = NULL;
914 unsigned int d_cnt, d_i, d_disp = 0;
915 unsigned int d_skipped = 0;
916 int type;
918 d = opendir(srv->repos_path);
919 if (d == NULL) {
920 error = got_error_from_errno2("opendir", srv->repos_path);
921 return error;
924 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
925 if (d_cnt == -1) {
926 sd_dent = NULL;
927 error = got_error_from_errno2("scandir", srv->repos_path);
928 goto done;
931 if (gotweb_render_repo_table_hdr(c->tp) == -1)
932 goto done;
934 for (d_i = 0; d_i < d_cnt; d_i++) {
935 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
936 break;
938 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
939 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
940 d_skipped++;
941 continue;
944 error = got_path_dirent_type(&type, srv->repos_path,
945 sd_dent[d_i]);
946 if (error)
947 goto done;
948 if (type != DT_DIR) {
949 d_skipped++;
950 continue;
953 if (qs->index_page > 0 && (qs->index_page *
954 srv->max_repos_display) > t->prev_disp) {
955 t->prev_disp++;
956 continue;
959 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
960 if (error)
961 goto done;
963 error = gotweb_load_got_path(c, repo_dir);
964 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
965 error = NULL;
966 gotweb_free_repo_dir(repo_dir);
967 repo_dir = NULL;
968 d_skipped++;
969 continue;
971 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
972 goto done;
974 d_disp++;
975 t->prev_disp++;
977 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
978 goto done;
980 gotweb_free_repo_dir(repo_dir);
981 repo_dir = NULL;
982 t->next_disp++;
983 if (d_disp == srv->max_repos_display)
984 break;
986 t->repos_total = d_cnt - d_skipped;
988 if (srv->max_repos_display == 0)
989 goto done;
990 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
991 goto done;
992 if (t->repos_total <= srv->max_repos ||
993 t->repos_total <= srv->max_repos_display)
994 goto done;
996 if (gotweb_render_navs(c->tp) == -1)
997 goto done;
998 done:
999 if (sd_dent) {
1000 for (d_i = 0; d_i < d_cnt; d_i++)
1001 free(sd_dent[d_i]);
1002 free(sd_dent);
1004 if (d != NULL && closedir(d) == EOF && error == NULL)
1005 error = got_error_from_errno("closedir");
1006 return error;
1009 static inline int
1010 should_urlencode(int c)
1012 if (c <= ' ' || c >= 127)
1013 return 1;
1015 switch (c) {
1016 /* gen-delim */
1017 case ':':
1018 case '/':
1019 case '?':
1020 case '#':
1021 case '[':
1022 case ']':
1023 case '@':
1024 /* sub-delims */
1025 case '!':
1026 case '$':
1027 case '&':
1028 case '\'':
1029 case '(':
1030 case ')':
1031 case '*':
1032 case '+':
1033 case ',':
1034 case ';':
1035 case '=':
1036 /* needed because the URLs are embedded into the HTML */
1037 case '\"':
1038 return 1;
1039 default:
1040 return 0;
1044 static char *
1045 gotweb_urlencode(const char *str)
1047 const char *s;
1048 char *escaped;
1049 size_t i, len;
1050 int a, b;
1052 len = 0;
1053 for (s = str; *s; ++s) {
1054 len++;
1055 if (should_urlencode(*s))
1056 len += 2;
1059 escaped = calloc(1, len + 1);
1060 if (escaped == NULL)
1061 return NULL;
1063 i = 0;
1064 for (s = str; *s; ++s) {
1065 if (should_urlencode(*s)) {
1066 a = (*s & 0xF0) >> 4;
1067 b = (*s & 0x0F);
1069 escaped[i++] = '%';
1070 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1071 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1072 } else
1073 escaped[i++] = *s;
1076 return escaped;
1079 const char *
1080 gotweb_action_name(int action)
1082 switch (action) {
1083 case BLAME:
1084 return "blame";
1085 case BLOB:
1086 return "blob";
1087 case BLOBRAW:
1088 return "blobraw";
1089 case BRIEFS:
1090 return "briefs";
1091 case COMMITS:
1092 return "commits";
1093 case DIFF:
1094 return "diff";
1095 case ERR:
1096 return "err";
1097 case INDEX:
1098 return "index";
1099 case SUMMARY:
1100 return "summary";
1101 case TAG:
1102 return "tag";
1103 case TAGS:
1104 return "tags";
1105 case TREE:
1106 return "tree";
1107 case RSS:
1108 return "rss";
1109 default:
1110 return NULL;
1114 int
1115 gotweb_render_url(struct request *c, struct gotweb_url *url)
1117 const char *sep = "?", *action;
1118 char *tmp;
1119 int r;
1121 action = gotweb_action_name(url->action);
1122 if (action != NULL) {
1123 if (fcgi_printf(c, "?action=%s", action) == -1)
1124 return -1;
1125 sep = "&";
1128 if (url->commit) {
1129 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1130 return -1;
1131 sep = "&";
1134 if (url->previd) {
1135 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1136 return -1;
1137 sep = "&";
1140 if (url->prevset) {
1141 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1142 return -1;
1143 sep = "&";
1146 if (url->file) {
1147 tmp = gotweb_urlencode(url->file);
1148 if (tmp == NULL)
1149 return -1;
1150 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1151 free(tmp);
1152 if (r == -1)
1153 return -1;
1154 sep = "&";
1157 if (url->folder) {
1158 tmp = gotweb_urlencode(url->folder);
1159 if (tmp == NULL)
1160 return -1;
1161 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1162 free(tmp);
1163 if (r == -1)
1164 return -1;
1165 sep = "&";
1168 if (url->headref) {
1169 tmp = gotweb_urlencode(url->headref);
1170 if (tmp == NULL)
1171 return -1;
1172 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1173 free(tmp);
1174 if (r == -1)
1175 return -1;
1176 sep = "&";
1179 if (url->index_page != -1) {
1180 if (fcgi_printf(c, "%sindex_page=%d", sep,
1181 url->index_page) == -1)
1182 return -1;
1183 sep = "&";
1186 if (url->path) {
1187 tmp = gotweb_urlencode(url->path);
1188 if (tmp == NULL)
1189 return -1;
1190 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1191 free(tmp);
1192 if (r == -1)
1193 return -1;
1194 sep = "&";
1197 if (url->page != -1) {
1198 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1199 return -1;
1200 sep = "&";
1203 return 0;
1206 int
1207 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1209 struct template *tp = c->tp;
1210 const char *proto = c->https ? "https" : "http";
1212 if (fcgi_puts(tp, proto) == -1 ||
1213 fcgi_puts(tp, "://") == -1 ||
1214 tp_htmlescape(tp, c->server_name) == -1 ||
1215 tp_htmlescape(tp, c->document_uri) == -1)
1216 return -1;
1218 return gotweb_render_url(c, url);
1221 static struct got_repository *
1222 find_cached_repo(struct server *srv, const char *path)
1224 int i;
1226 for (i = 0; i < srv->ncached_repos; i++) {
1227 if (strcmp(srv->cached_repos[i].path, path) == 0)
1228 return srv->cached_repos[i].repo;
1231 return NULL;
1234 static const struct got_error *
1235 cache_repo(struct got_repository **new, struct server *srv,
1236 struct repo_dir *repo_dir, struct socket *sock)
1238 const struct got_error *error = NULL;
1239 struct got_repository *repo;
1240 struct cached_repo *cr;
1241 int evicted = 0;
1243 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1244 cr = &srv->cached_repos[srv->ncached_repos - 1];
1245 error = got_repo_close(cr->repo);
1246 memset(cr, 0, sizeof(*cr));
1247 srv->ncached_repos--;
1248 if (error)
1249 return error;
1250 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1251 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1252 cr = &srv->cached_repos[0];
1253 evicted = 1;
1254 } else {
1255 cr = &srv->cached_repos[srv->ncached_repos];
1258 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1259 if (error) {
1260 if (evicted) {
1261 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1262 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1264 return error;
1267 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1268 >= sizeof(cr->path)) {
1269 if (evicted) {
1270 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1271 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1273 return got_error(GOT_ERR_NO_SPACE);
1276 cr->repo = repo;
1277 srv->ncached_repos++;
1278 *new = repo;
1279 return NULL;
1282 static const struct got_error *
1283 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1285 const struct got_error *error = NULL;
1286 struct socket *sock = c->sock;
1287 struct server *srv = c->srv;
1288 struct transport *t = c->t;
1289 struct got_repository *repo = NULL;
1290 DIR *dt;
1291 char *dir_test;
1293 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1294 GOTWEB_GIT_DIR) == -1)
1295 return got_error_from_errno("asprintf");
1297 dt = opendir(dir_test);
1298 if (dt == NULL) {
1299 free(dir_test);
1300 } else {
1301 repo_dir->path = dir_test;
1302 dir_test = NULL;
1303 goto done;
1306 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1307 repo_dir->name) == -1)
1308 return got_error_from_errno("asprintf");
1310 dt = opendir(dir_test);
1311 if (dt == NULL) {
1312 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1313 goto err;
1314 } else {
1315 repo_dir->path = dir_test;
1316 dir_test = NULL;
1319 done:
1320 if (srv->respect_exportok &&
1321 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1322 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1323 goto err;
1326 repo = find_cached_repo(srv, repo_dir->path);
1327 if (repo == NULL) {
1328 error = cache_repo(&repo, srv, repo_dir, sock);
1329 if (error)
1330 goto err;
1332 t->repo = repo;
1333 error = gotweb_get_repo_description(&repo_dir->description, srv,
1334 repo_dir->path, dirfd(dt));
1335 if (error)
1336 goto err;
1337 error = got_get_repo_owner(&repo_dir->owner, c);
1338 if (error)
1339 goto err;
1340 error = got_get_repo_age(&repo_dir->age, c, NULL);
1341 if (error)
1342 goto err;
1343 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1344 dirfd(dt));
1345 err:
1346 free(dir_test);
1347 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1348 error = got_error_from_errno("closedir");
1349 return error;
1352 static const struct got_error *
1353 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1355 const struct got_error *error;
1357 *repo_dir = calloc(1, sizeof(**repo_dir));
1358 if (*repo_dir == NULL)
1359 return got_error_from_errno("calloc");
1361 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1362 error = got_error_from_errno("asprintf");
1363 free(*repo_dir);
1364 *repo_dir = NULL;
1365 return error;
1367 (*repo_dir)->owner = NULL;
1368 (*repo_dir)->description = NULL;
1369 (*repo_dir)->url = NULL;
1370 (*repo_dir)->path = NULL;
1372 return NULL;
1375 static const struct got_error *
1376 gotweb_get_repo_description(char **description, struct server *srv,
1377 const char *dirpath, int dir)
1379 const struct got_error *error = NULL;
1380 struct stat sb;
1381 int fd = -1;
1382 off_t len;
1384 *description = NULL;
1385 if (srv->show_repo_description == 0)
1386 return NULL;
1388 fd = openat(dir, "description", O_RDONLY);
1389 if (fd == -1) {
1390 if (errno != ENOENT && errno != EACCES) {
1391 error = got_error_from_errno_fmt("openat %s/%s",
1392 dirpath, "description");
1394 goto done;
1397 if (fstat(fd, &sb) == -1) {
1398 error = got_error_from_errno_fmt("fstat %s/%s",
1399 dirpath, "description");
1400 goto done;
1403 len = sb.st_size;
1404 if (len > GOTWEBD_MAXDESCRSZ - 1)
1405 len = GOTWEBD_MAXDESCRSZ - 1;
1407 *description = calloc(len + 1, sizeof(**description));
1408 if (*description == NULL) {
1409 error = got_error_from_errno("calloc");
1410 goto done;
1413 if (read(fd, *description, len) == -1)
1414 error = got_error_from_errno("read");
1415 done:
1416 if (fd != -1 && close(fd) == -1 && error == NULL)
1417 error = got_error_from_errno("close");
1418 return error;
1421 static const struct got_error *
1422 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1423 int dir)
1425 const struct got_error *error = NULL;
1426 struct stat sb;
1427 int fd = -1;
1428 off_t len;
1430 *url = NULL;
1431 if (srv->show_repo_cloneurl == 0)
1432 return NULL;
1434 fd = openat(dir, "cloneurl", O_RDONLY);
1435 if (fd == -1) {
1436 if (errno != ENOENT && errno != EACCES) {
1437 error = got_error_from_errno_fmt("openat %s/%s",
1438 dirpath, "cloneurl");
1440 goto done;
1443 if (fstat(fd, &sb) == -1) {
1444 error = got_error_from_errno_fmt("fstat %s/%s",
1445 dirpath, "cloneurl");
1446 goto done;
1449 len = sb.st_size;
1450 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1451 len = GOTWEBD_MAXCLONEURLSZ - 1;
1453 *url = calloc(len + 1, sizeof(**url));
1454 if (*url == NULL) {
1455 error = got_error_from_errno("calloc");
1456 goto done;
1459 if (read(fd, *url, len) == -1)
1460 error = got_error_from_errno("read");
1461 done:
1462 if (fd != -1 && close(fd) == -1 && error == NULL)
1463 error = got_error_from_errno("close");
1464 return error;
1467 int
1468 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1470 struct request *c = tp->tp_arg;
1471 struct tm tm;
1472 long long diff_time;
1473 const char *years = "years ago", *months = "months ago";
1474 const char *weeks = "weeks ago", *days = "days ago";
1475 const char *hours = "hours ago", *minutes = "minutes ago";
1476 const char *seconds = "seconds ago", *now = "right now";
1477 char *s;
1478 char datebuf[64];
1479 size_t r;
1481 switch (ref_tm) {
1482 case TM_DIFF:
1483 diff_time = time(NULL) - committer_time;
1484 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1485 if (fcgi_printf(c, "%lld %s",
1486 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1487 return -1;
1488 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1489 if (fcgi_printf(c, "%lld %s",
1490 (diff_time / 60 / 60 / 24 / (365 / 12)),
1491 months) == -1)
1492 return -1;
1493 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1494 if (fcgi_printf(c, "%lld %s",
1495 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1496 return -1;
1497 } else if (diff_time > 60 * 60 * 24 * 2) {
1498 if (fcgi_printf(c, "%lld %s",
1499 (diff_time / 60 / 60 / 24), days) == -1)
1500 return -1;
1501 } else if (diff_time > 60 * 60 * 2) {
1502 if (fcgi_printf(c, "%lld %s",
1503 (diff_time / 60 / 60), hours) == -1)
1504 return -1;
1505 } else if (diff_time > 60 * 2) {
1506 if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1507 minutes) == -1)
1508 return -1;
1509 } else if (diff_time > 2) {
1510 if (fcgi_printf(c, "%lld %s", diff_time,
1511 seconds) == -1)
1512 return -1;
1513 } else {
1514 if (fcgi_puts(tp, now) == -1)
1515 return -1;
1517 break;
1518 case TM_LONG:
1519 if (gmtime_r(&committer_time, &tm) == NULL)
1520 return -1;
1522 s = asctime_r(&tm, datebuf);
1523 if (s == NULL)
1524 return -1;
1526 if (fcgi_puts(tp, datebuf) == -1 ||
1527 fcgi_puts(tp, " UTC") == -1)
1528 return -1;
1529 break;
1530 case TM_RFC822:
1531 if (gmtime_r(&committer_time, &tm) == NULL)
1532 return -1;
1534 r = strftime(datebuf, sizeof(datebuf),
1535 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1536 if (r == 0)
1537 return -1;
1539 if (fcgi_puts(tp, datebuf) == -1)
1540 return -1;
1541 break;
1543 return 0;