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 <sha2.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_diff.h"
48 #include "got_commit_graph.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
52 #include "got_compat.h"
54 #include "proc.h"
55 #include "gotwebd.h"
56 #include "tmpl.h"
58 static const struct querystring_keys querystring_keys[] = {
59 { "action", ACTION },
60 { "commit", COMMIT },
61 { "file", RFILE },
62 { "folder", FOLDER },
63 { "headref", HEADREF },
64 { "index_page", INDEX_PAGE },
65 { "path", PATH },
66 { "page", PAGE },
67 };
69 static const struct action_keys action_keys[] = {
70 { "blame", BLAME },
71 { "blob", BLOB },
72 { "blobraw", BLOBRAW },
73 { "briefs", BRIEFS },
74 { "commits", COMMITS },
75 { "diff", DIFF },
76 { "error", ERR },
77 { "index", INDEX },
78 { "summary", SUMMARY },
79 { "tag", TAG },
80 { "tags", TAGS },
81 { "tree", TREE },
82 { "rss", RSS },
83 };
85 static const struct got_error *gotweb_init_querystring(struct querystring **);
86 static const struct got_error *gotweb_parse_querystring(struct querystring **,
87 char *);
88 static const struct got_error *gotweb_assign_querystring(struct querystring **,
89 char *, char *);
90 static const struct got_error *gotweb_render_index(struct request *);
91 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
92 const char *);
93 static const struct got_error *gotweb_load_got_path(struct request *c,
94 struct repo_dir *);
95 static const struct got_error *gotweb_get_repo_description(char **,
96 struct server *, const char *, int);
97 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
98 const char *, int);
100 static void gotweb_free_querystring(struct querystring *);
101 static void gotweb_free_repo_dir(struct repo_dir *);
103 struct server *gotweb_get_server(uint8_t *, uint8_t *);
105 static int
106 gotweb_reply(struct request *c, int status, const char *ctype,
107 struct gotweb_url *location)
109 const char *csp;
111 if (status != 200 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
112 return -1;
114 if (location) {
115 if (fcgi_puts(c->tp, "Location: ") == -1 ||
116 gotweb_render_url(c, location) == -1 ||
117 fcgi_puts(c->tp, "\r\n") == -1)
118 return -1;
121 csp = "Content-Security-Policy: default-src 'self'; "
122 "script-src 'none'; object-src 'none';\r\n";
123 if (fcgi_puts(c->tp, csp) == -1)
124 return -1;
126 if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
127 return -1;
129 return fcgi_puts(c->tp, "\r\n");
132 static int
133 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
134 const char *suffix)
136 int r;
138 r = fcgi_printf(c, "Content-Disposition: attachment; "
139 "filename=%s%s\r\n", file, suffix ? suffix : "");
140 if (r == -1)
141 return -1;
142 return gotweb_reply(c, 200, ctype, NULL);
145 void
146 gotweb_process_request(struct request *c)
148 const struct got_error *error = NULL, *error2 = NULL;
149 struct got_blob_object *blob = NULL;
150 struct server *srv = NULL;
151 struct querystring *qs = NULL;
152 struct repo_dir *repo_dir = NULL;
153 struct got_reflist_head refs;
154 FILE *fp = NULL;
155 uint8_t err[] = "gotwebd experienced an error: ";
156 int r, html = 0, fd = -1;
158 TAILQ_INIT(&refs);
160 /* init the transport */
161 error = gotweb_init_transport(&c->t);
162 if (error) {
163 log_warnx("%s: %s", __func__, error->msg);
164 return;
166 /* don't process any further if client disconnected */
167 if (c->sock->client_status == CLIENT_DISCONNECT)
168 return;
169 /* get the gotwebd server */
170 srv = gotweb_get_server(c->server_name, c->http_host);
171 if (srv == NULL) {
172 log_warnx("%s: error server is NULL", __func__);
173 goto err;
175 c->srv = srv;
176 /* parse our querystring */
177 error = gotweb_init_querystring(&qs);
178 if (error) {
179 log_warnx("%s: %s", __func__, error->msg);
180 goto err;
182 c->t->qs = qs;
183 error = gotweb_parse_querystring(&qs, c->querystring);
184 if (error) {
185 log_warnx("%s: %s", __func__, error->msg);
186 goto err;
189 /*
190 * certain actions require a commit id in the querystring. this stops
191 * bad actors from exploiting this by manually manipulating the
192 * querystring.
193 */
195 if (qs->action == BLAME || qs->action == BLOB ||
196 qs->action == BLOBRAW || qs->action == DIFF) {
197 if (qs->commit == NULL) {
198 error2 = got_error(GOT_ERR_QUERYSTRING);
199 goto render;
203 if (qs->action != INDEX) {
204 error = gotweb_init_repo_dir(&repo_dir, qs->path);
205 if (error)
206 goto done;
207 error = gotweb_load_got_path(c, repo_dir);
208 c->t->repo_dir = repo_dir;
209 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
210 goto err;
213 if (qs->action == BLOBRAW) {
214 const uint8_t *buf;
215 size_t len;
216 int binary, r;
218 error = got_get_repo_commits(c, 1);
219 if (error)
220 goto done;
222 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
223 if (error2)
224 goto render;
226 if (binary)
227 r = gotweb_reply_file(c, "application/octet-stream",
228 qs->file, NULL);
229 else
230 r = gotweb_reply(c, 200, "text/plain", NULL);
231 if (r == -1)
232 goto done;
234 for (;;) {
235 error = got_object_blob_read_block(&len, blob);
236 if (error)
237 goto done;
238 if (len == 0)
239 break;
240 buf = got_object_blob_get_read_buf(blob);
241 if (fcgi_gen_binary_response(c, buf, len) == -1)
242 goto done;
245 goto done;
248 if (qs->action == BLOB) {
249 int binary;
250 struct gotweb_url url = {
251 .index_page = -1,
252 .page = -1,
253 .action = BLOBRAW,
254 .path = qs->path,
255 .commit = qs->commit,
256 .folder = qs->folder,
257 .file = qs->file,
258 };
260 error = got_get_repo_commits(c, 1);
261 if (error)
262 goto done;
264 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
265 if (error2)
266 goto render;
267 if (binary) {
268 gotweb_reply(c, 302, NULL, &url);
269 goto done;
273 if (qs->action == RSS) {
274 const char *ctype = "application/rss+xml;charset=utf-8";
276 if (gotweb_reply_file(c, ctype, repo_dir->name, ".rss") == -1)
277 goto done;
279 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
280 if (error) {
281 log_warnx("%s: %s", __func__, error->msg);
282 goto err;
284 if (gotweb_render_rss(c->tp) == -1)
285 goto err;
286 goto done;
289 render:
290 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
291 goto done;
292 html = 1;
294 if (gotweb_render_header(c->tp) == -1)
295 goto err;
297 if (error2) {
298 error = error2;
299 goto err;
302 switch(qs->action) {
303 case BLAME:
304 error = got_get_repo_commits(c, 1);
305 if (error) {
306 log_warnx("%s: %s", __func__, error->msg);
307 goto err;
309 if (gotweb_render_blame(c->tp) == -1)
310 goto done;
311 break;
312 case BLOB:
313 if (gotweb_render_blob(c->tp, blob) == -1)
314 goto err;
315 break;
316 case BRIEFS:
317 if (gotweb_render_briefs(c->tp) == -1)
318 goto err;
319 break;
320 case COMMITS:
321 error = got_get_repo_commits(c, srv->max_commits_display);
322 if (error) {
323 log_warnx("%s: %s", __func__, error->msg);
324 goto err;
326 if (gotweb_render_commits(c->tp) == -1)
327 goto err;
328 break;
329 case DIFF:
330 error = got_get_repo_commits(c, 1);
331 if (error) {
332 log_warnx("%s: %s", __func__, error->msg);
333 goto err;
335 error = got_open_diff_for_output(&fp, &fd, c);
336 if (error) {
337 log_warnx("%s: %s", __func__, error->msg);
338 goto err;
340 if (gotweb_render_diff(c->tp, fp) == -1)
341 goto err;
342 break;
343 case INDEX:
344 error = gotweb_render_index(c);
345 if (error) {
346 log_warnx("%s: %s", __func__, error->msg);
347 goto err;
349 break;
350 case SUMMARY:
351 error = got_ref_list(&refs, c->t->repo, "refs/heads",
352 got_ref_cmp_by_name, NULL);
353 if (error) {
354 log_warnx("%s: got_ref_list: %s", __func__,
355 error->msg);
356 goto err;
358 qs->action = TAGS;
359 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
360 if (error) {
361 log_warnx("%s: got_get_repo_tags: %s", __func__,
362 error->msg);
363 goto err;
365 qs->action = SUMMARY;
366 if (gotweb_render_summary(c->tp, &refs) == -1)
367 goto done;
368 break;
369 case TAG:
370 error = got_get_repo_tags(c, 1);
371 if (error) {
372 log_warnx("%s: %s", __func__, error->msg);
373 goto err;
375 if (c->t->tag_count == 0) {
376 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
377 "bad commit id");
378 goto err;
380 if (gotweb_render_tag(c->tp) == -1)
381 goto done;
382 break;
383 case TAGS:
384 error = got_get_repo_tags(c, srv->max_commits_display);
385 if (error) {
386 log_warnx("%s: %s", __func__, error->msg);
387 goto err;
389 if (gotweb_render_tags(c->tp) == -1)
390 goto done;
391 break;
392 case TREE:
393 error = got_get_repo_commits(c, 1);
394 if (error) {
395 log_warnx("%s: %s", __func__, error->msg);
396 goto err;
398 if (gotweb_render_tree(c->tp) == -1)
399 goto err;
400 break;
401 case ERR:
402 default:
403 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
404 "Erorr: Bad Querystring");
405 if (r == -1)
406 goto err;
407 break;
410 goto done;
411 err:
412 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
413 return;
414 if (fcgi_printf(c, "\n%s", err) == -1)
415 return;
416 if (error) {
417 if (fcgi_printf(c, "%s", error->msg) == -1)
418 return;
419 } else {
420 if (fcgi_printf(c, "see daemon logs for details") == -1)
421 return;
423 if (html && fcgi_printf(c, "</div>\n") == -1)
424 return;
425 done:
426 if (blob)
427 got_object_blob_close(blob);
428 if (fp) {
429 error = got_gotweb_flushfile(fp, fd);
430 if (error)
431 log_warnx("%s: got_gotweb_flushfile failure: %s",
432 __func__, error->msg);
433 fd = -1;
435 if (fd != -1)
436 close(fd);
437 if (html && srv != NULL)
438 gotweb_render_footer(c->tp);
440 got_ref_list_free(&refs);
443 struct server *
444 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
446 struct server *srv = NULL;
448 /* check against the server name first */
449 if (strlen(server_name) > 0)
450 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
451 if (strcmp(srv->name, server_name) == 0)
452 goto done;
454 /* check against subdomain second */
455 if (strlen(subdomain) > 0)
456 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
457 if (strcmp(srv->name, subdomain) == 0)
458 goto done;
460 /* if those fail, send first server */
461 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
462 if (srv != NULL)
463 break;
464 done:
465 return srv;
466 };
468 const struct got_error *
469 gotweb_init_transport(struct transport **t)
471 const struct got_error *error = NULL;
473 *t = calloc(1, sizeof(**t));
474 if (*t == NULL)
475 return got_error_from_errno2("%s: calloc", __func__);
477 TAILQ_INIT(&(*t)->repo_commits);
478 TAILQ_INIT(&(*t)->repo_tags);
480 (*t)->repo = NULL;
481 (*t)->repo_dir = NULL;
482 (*t)->qs = NULL;
483 (*t)->next_id = NULL;
484 (*t)->prev_id = NULL;
485 (*t)->next_disp = 0;
486 (*t)->prev_disp = 0;
488 return error;
491 static const struct got_error *
492 gotweb_init_querystring(struct querystring **qs)
494 const struct got_error *error = NULL;
496 *qs = calloc(1, sizeof(**qs));
497 if (*qs == NULL)
498 return got_error_from_errno2("%s: calloc", __func__);
500 (*qs)->headref = strdup("HEAD");
501 if ((*qs)->headref == NULL) {
502 free(*qs);
503 *qs = NULL;
504 return got_error_from_errno2("%s: strdup", __func__);
507 (*qs)->action = INDEX;
508 (*qs)->commit = NULL;
509 (*qs)->file = NULL;
510 (*qs)->folder = NULL;
511 (*qs)->index_page = 0;
512 (*qs)->path = NULL;
514 return error;
517 static const struct got_error *
518 gotweb_parse_querystring(struct querystring **qs, char *qst)
520 const struct got_error *error = NULL;
521 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
522 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
524 if (qst == NULL)
525 return error;
527 tok1 = strdup(qst);
528 if (tok1 == NULL)
529 return got_error_from_errno2("%s: strdup", __func__);
531 tok1_pair = tok1;
532 tok1_end = tok1;
534 while (tok1_pair != NULL) {
535 strsep(&tok1_end, "&");
537 tok2 = strdup(tok1_pair);
538 if (tok2 == NULL) {
539 free(tok1);
540 return got_error_from_errno2("%s: strdup", __func__);
543 tok2_pair = tok2;
544 tok2_end = tok2;
546 while (tok2_pair != NULL) {
547 strsep(&tok2_end, "=");
548 if (tok2_end) {
549 error = gotweb_assign_querystring(qs, tok2_pair,
550 tok2_end);
551 if (error)
552 goto err;
554 tok2_pair = tok2_end;
556 free(tok2);
557 tok1_pair = tok1_end;
559 free(tok1);
560 return error;
561 err:
562 free(tok2);
563 free(tok1);
564 return error;
567 /*
568 * Adapted from usr.sbin/httpd/httpd.c url_decode.
569 */
570 static const struct got_error *
571 gotweb_urldecode(char *url)
573 char *p, *q;
574 char hex[3];
575 unsigned long x;
577 hex[2] = '\0';
578 p = q = url;
580 while (*p != '\0') {
581 switch (*p) {
582 case '%':
583 /* Encoding character is followed by two hex chars */
584 if (!isxdigit((unsigned char)p[1]) ||
585 !isxdigit((unsigned char)p[2]) ||
586 (p[1] == '0' && p[2] == '0'))
587 return got_error(GOT_ERR_BAD_QUERYSTRING);
589 hex[0] = p[1];
590 hex[1] = p[2];
592 /*
593 * We don't have to validate "hex" because it is
594 * guaranteed to include two hex chars followed by nul.
595 */
596 x = strtoul(hex, NULL, 16);
597 *q = (char)x;
598 p += 2;
599 break;
600 default:
601 *q = *p;
602 break;
604 p++;
605 q++;
607 *q = '\0';
609 return NULL;
612 static const struct got_error *
613 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
615 const struct got_error *error = NULL;
616 const char *errstr;
617 int a_cnt, el_cnt;
619 error = gotweb_urldecode(value);
620 if (error)
621 return error;
623 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
624 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
625 continue;
627 switch (querystring_keys[el_cnt].element) {
628 case ACTION:
629 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
630 if (strcmp(value, action_keys[a_cnt].name) != 0)
631 continue;
632 else if (strcmp(value,
633 action_keys[a_cnt].name) == 0){
634 (*qs)->action =
635 action_keys[a_cnt].action;
636 goto qa_found;
639 (*qs)->action = ERR;
640 qa_found:
641 break;
642 case COMMIT:
643 (*qs)->commit = strdup(value);
644 if ((*qs)->commit == NULL) {
645 error = got_error_from_errno2("%s: strdup",
646 __func__);
647 goto done;
649 break;
650 case RFILE:
651 (*qs)->file = strdup(value);
652 if ((*qs)->file == NULL) {
653 error = got_error_from_errno2("%s: strdup",
654 __func__);
655 goto done;
657 break;
658 case FOLDER:
659 (*qs)->folder = strdup(value);
660 if ((*qs)->folder == NULL) {
661 error = got_error_from_errno2("%s: strdup",
662 __func__);
663 goto done;
665 break;
666 case HEADREF:
667 free((*qs)->headref);
668 (*qs)->headref = strdup(value);
669 if ((*qs)->headref == NULL) {
670 error = got_error_from_errno2("%s: strdup",
671 __func__);
672 goto done;
674 break;
675 case INDEX_PAGE:
676 if (strlen(value) == 0)
677 break;
678 (*qs)->index_page = strtonum(value, INT64_MIN,
679 INT64_MAX, &errstr);
680 if (errstr) {
681 error = got_error_from_errno3("%s: strtonum %s",
682 __func__, errstr);
683 goto done;
685 if ((*qs)->index_page < 0)
686 (*qs)->index_page = 0;
687 break;
688 case PATH:
689 (*qs)->path = strdup(value);
690 if ((*qs)->path == NULL) {
691 error = got_error_from_errno2("%s: strdup",
692 __func__);
693 goto done;
695 break;
696 case PAGE:
697 if (strlen(value) == 0)
698 break;
699 (*qs)->page = strtonum(value, INT64_MIN,
700 INT64_MAX, &errstr);
701 if (errstr) {
702 error = got_error_from_errno3("%s: strtonum %s",
703 __func__, errstr);
704 goto done;
706 if ((*qs)->page < 0)
707 (*qs)->page = 0;
708 break;
709 default:
710 break;
713 done:
714 return error;
717 void
718 gotweb_free_repo_tag(struct repo_tag *rt)
720 if (rt != NULL) {
721 free(rt->commit_id);
722 free(rt->tag_name);
723 free(rt->tag_commit);
724 free(rt->commit_msg);
725 free(rt->tagger);
727 free(rt);
730 void
731 gotweb_free_repo_commit(struct repo_commit *rc)
733 if (rc != NULL) {
734 free(rc->path);
735 free(rc->refs_str);
736 free(rc->commit_id);
737 free(rc->parent_id);
738 free(rc->tree_id);
739 free(rc->author);
740 free(rc->committer);
741 free(rc->commit_msg);
743 free(rc);
746 static void
747 gotweb_free_querystring(struct querystring *qs)
749 if (qs != NULL) {
750 free(qs->commit);
751 free(qs->file);
752 free(qs->folder);
753 free(qs->headref);
754 free(qs->path);
756 free(qs);
759 static void
760 gotweb_free_repo_dir(struct repo_dir *repo_dir)
762 if (repo_dir != NULL) {
763 free(repo_dir->name);
764 free(repo_dir->owner);
765 free(repo_dir->description);
766 free(repo_dir->url);
767 free(repo_dir->path);
769 free(repo_dir);
772 void
773 gotweb_free_transport(struct transport *t)
775 struct repo_commit *rc = NULL, *trc = NULL;
776 struct repo_tag *rt = NULL, *trt = NULL;
778 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
779 TAILQ_REMOVE(&t->repo_commits, rc, entry);
780 gotweb_free_repo_commit(rc);
782 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
783 TAILQ_REMOVE(&t->repo_tags, rt, entry);
784 gotweb_free_repo_tag(rt);
786 gotweb_free_repo_dir(t->repo_dir);
787 gotweb_free_querystring(t->qs);
788 free(t->more_id);
789 free(t->next_id);
790 free(t->prev_id);
791 free(t);
794 void
795 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
796 struct gotweb_url *next, int *have_next)
798 struct transport *t = c->t;
799 struct querystring *qs = t->qs;
800 struct server *srv = c->srv;
802 *have_prev = *have_next = 0;
804 switch(qs->action) {
805 case INDEX:
806 if (qs->index_page > 0) {
807 *have_prev = 1;
808 *prev = (struct gotweb_url){
809 .action = -1,
810 .index_page = qs->index_page - 1,
811 .page = -1,
812 };
814 if (t->next_disp == srv->max_repos_display &&
815 t->repos_total != (qs->index_page + 1) *
816 srv->max_repos_display) {
817 *have_next = 1;
818 *next = (struct gotweb_url){
819 .action = -1,
820 .index_page = qs->index_page + 1,
821 .page = -1,
822 };
824 break;
825 case TAGS:
826 if (t->prev_id && qs->commit != NULL &&
827 strcmp(qs->commit, t->prev_id) != 0) {
828 *have_prev = 1;
829 *prev = (struct gotweb_url){
830 .action = TAGS,
831 .index_page = -1,
832 .page = qs->page - 1,
833 .path = qs->path,
834 .commit = t->prev_id,
835 .headref = qs->headref,
836 };
838 if (t->next_id) {
839 *have_next = 1;
840 *next = (struct gotweb_url){
841 .action = TAGS,
842 .index_page = -1,
843 .page = qs->page + 1,
844 .path = qs->path,
845 .commit = t->next_id,
846 .headref = qs->headref,
847 };
849 break;
853 static const struct got_error *
854 gotweb_render_index(struct request *c)
856 const struct got_error *error = NULL;
857 struct server *srv = c->srv;
858 struct transport *t = c->t;
859 struct querystring *qs = t->qs;
860 struct repo_dir *repo_dir = NULL;
861 struct dirent **sd_dent = NULL;
862 unsigned int d_cnt, d_i, d_disp = 0;
863 unsigned int d_skipped = 0;
864 int type;
866 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
867 if (d_cnt == -1) {
868 sd_dent = NULL;
869 error = got_error_from_errno2("scandir", srv->repos_path);
870 goto done;
873 if (gotweb_render_repo_table_hdr(c->tp) == -1)
874 goto done;
876 for (d_i = 0; d_i < d_cnt; d_i++) {
877 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
878 break;
880 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
881 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
882 d_skipped++;
883 continue;
886 error = got_path_dirent_type(&type, srv->repos_path,
887 sd_dent[d_i]);
888 if (error)
889 goto done;
890 if (type != DT_DIR) {
891 d_skipped++;
892 continue;
895 if (qs->index_page > 0 && (qs->index_page *
896 srv->max_repos_display) > t->prev_disp) {
897 t->prev_disp++;
898 continue;
901 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
902 if (error)
903 goto done;
905 error = gotweb_load_got_path(c, repo_dir);
906 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
907 error = NULL;
908 gotweb_free_repo_dir(repo_dir);
909 repo_dir = NULL;
910 d_skipped++;
911 continue;
913 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
914 goto done;
916 d_disp++;
917 t->prev_disp++;
919 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
920 goto done;
922 gotweb_free_repo_dir(repo_dir);
923 repo_dir = NULL;
924 t->next_disp++;
925 if (d_disp == srv->max_repos_display)
926 break;
928 t->repos_total = d_cnt - d_skipped;
930 if (srv->max_repos_display == 0)
931 goto done;
932 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
933 goto done;
934 if (t->repos_total <= srv->max_repos ||
935 t->repos_total <= srv->max_repos_display)
936 goto done;
938 if (gotweb_render_navs(c->tp) == -1)
939 goto done;
940 done:
941 if (sd_dent) {
942 for (d_i = 0; d_i < d_cnt; d_i++)
943 free(sd_dent[d_i]);
944 free(sd_dent);
946 return error;
949 static inline int
950 should_urlencode(int c)
952 if (c <= ' ' || c >= 127)
953 return 1;
955 switch (c) {
956 /* gen-delim */
957 case ':':
958 case '/':
959 case '?':
960 case '#':
961 case '[':
962 case ']':
963 case '@':
964 /* sub-delims */
965 case '!':
966 case '$':
967 case '&':
968 case '\'':
969 case '(':
970 case ')':
971 case '*':
972 case '+':
973 case ',':
974 case ';':
975 case '=':
976 /* needed because the URLs are embedded into the HTML */
977 case '\"':
978 return 1;
979 default:
980 return 0;
984 static char *
985 gotweb_urlencode(const char *str)
987 const char *s;
988 char *escaped;
989 size_t i, len;
990 int a, b;
992 len = 0;
993 for (s = str; *s; ++s) {
994 len++;
995 if (should_urlencode(*s))
996 len += 2;
999 escaped = calloc(1, len + 1);
1000 if (escaped == NULL)
1001 return NULL;
1003 i = 0;
1004 for (s = str; *s; ++s) {
1005 if (should_urlencode(*s)) {
1006 a = (*s & 0xF0) >> 4;
1007 b = (*s & 0x0F);
1009 escaped[i++] = '%';
1010 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1011 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1012 } else
1013 escaped[i++] = *s;
1016 return escaped;
1019 const char *
1020 gotweb_action_name(int action)
1022 switch (action) {
1023 case BLAME:
1024 return "blame";
1025 case BLOB:
1026 return "blob";
1027 case BLOBRAW:
1028 return "blobraw";
1029 case BRIEFS:
1030 return "briefs";
1031 case COMMITS:
1032 return "commits";
1033 case DIFF:
1034 return "diff";
1035 case ERR:
1036 return "err";
1037 case INDEX:
1038 return "index";
1039 case SUMMARY:
1040 return "summary";
1041 case TAG:
1042 return "tag";
1043 case TAGS:
1044 return "tags";
1045 case TREE:
1046 return "tree";
1047 case RSS:
1048 return "rss";
1049 default:
1050 return NULL;
1054 int
1055 gotweb_render_url(struct request *c, struct gotweb_url *url)
1057 const char *sep = "?", *action;
1058 char *tmp;
1059 int r;
1061 action = gotweb_action_name(url->action);
1062 if (action != NULL) {
1063 if (fcgi_printf(c, "?action=%s", action) == -1)
1064 return -1;
1065 sep = "&";
1068 if (url->commit) {
1069 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1070 return -1;
1071 sep = "&";
1074 if (url->previd) {
1075 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1076 return -1;
1077 sep = "&";
1080 if (url->prevset) {
1081 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1082 return -1;
1083 sep = "&";
1086 if (url->file) {
1087 tmp = gotweb_urlencode(url->file);
1088 if (tmp == NULL)
1089 return -1;
1090 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1091 free(tmp);
1092 if (r == -1)
1093 return -1;
1094 sep = "&";
1097 if (url->folder) {
1098 tmp = gotweb_urlencode(url->folder);
1099 if (tmp == NULL)
1100 return -1;
1101 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1102 free(tmp);
1103 if (r == -1)
1104 return -1;
1105 sep = "&";
1108 if (url->headref) {
1109 tmp = gotweb_urlencode(url->headref);
1110 if (tmp == NULL)
1111 return -1;
1112 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1113 free(tmp);
1114 if (r == -1)
1115 return -1;
1116 sep = "&";
1119 if (url->index_page != -1) {
1120 if (fcgi_printf(c, "%sindex_page=%d", sep,
1121 url->index_page) == -1)
1122 return -1;
1123 sep = "&";
1126 if (url->path) {
1127 tmp = gotweb_urlencode(url->path);
1128 if (tmp == NULL)
1129 return -1;
1130 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1131 free(tmp);
1132 if (r == -1)
1133 return -1;
1134 sep = "&";
1137 if (url->page != -1) {
1138 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1139 return -1;
1140 sep = "&";
1143 return 0;
1146 int
1147 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1149 struct template *tp = c->tp;
1150 const char *proto = c->https ? "https" : "http";
1152 if (fcgi_puts(tp, proto) == -1 ||
1153 fcgi_puts(tp, "://") == -1 ||
1154 tp_htmlescape(tp, c->server_name) == -1 ||
1155 tp_htmlescape(tp, c->document_uri) == -1)
1156 return -1;
1158 return gotweb_render_url(c, url);
1161 static struct got_repository *
1162 find_cached_repo(struct server *srv, const char *path)
1164 int i;
1166 for (i = 0; i < srv->ncached_repos; i++) {
1167 if (strcmp(srv->cached_repos[i].path, path) == 0)
1168 return srv->cached_repos[i].repo;
1171 return NULL;
1174 static const struct got_error *
1175 cache_repo(struct got_repository **new, struct server *srv,
1176 struct repo_dir *repo_dir, struct socket *sock)
1178 const struct got_error *error = NULL;
1179 struct got_repository *repo;
1180 struct cached_repo *cr;
1181 int evicted = 0;
1183 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1184 cr = &srv->cached_repos[srv->ncached_repos - 1];
1185 error = got_repo_close(cr->repo);
1186 memset(cr, 0, sizeof(*cr));
1187 srv->ncached_repos--;
1188 if (error)
1189 return error;
1190 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1191 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1192 cr = &srv->cached_repos[0];
1193 evicted = 1;
1194 } else {
1195 cr = &srv->cached_repos[srv->ncached_repos];
1198 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1199 if (error) {
1200 if (evicted) {
1201 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1202 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1204 return error;
1207 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1208 >= sizeof(cr->path)) {
1209 if (evicted) {
1210 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1211 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1213 return got_error(GOT_ERR_NO_SPACE);
1216 cr->repo = repo;
1217 srv->ncached_repos++;
1218 *new = repo;
1219 return NULL;
1222 static const struct got_error *
1223 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1225 const struct got_error *error = NULL;
1226 struct socket *sock = c->sock;
1227 struct server *srv = c->srv;
1228 struct transport *t = c->t;
1229 struct got_repository *repo = NULL;
1230 DIR *dt;
1231 char *dir_test;
1233 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1234 GOTWEB_GIT_DIR) == -1)
1235 return got_error_from_errno("asprintf");
1237 dt = opendir(dir_test);
1238 if (dt == NULL) {
1239 free(dir_test);
1240 } else {
1241 repo_dir->path = dir_test;
1242 dir_test = NULL;
1243 goto done;
1246 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1247 repo_dir->name) == -1)
1248 return got_error_from_errno("asprintf");
1250 dt = opendir(dir_test);
1251 if (dt == NULL) {
1252 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1253 goto err;
1254 } else {
1255 repo_dir->path = dir_test;
1256 dir_test = NULL;
1259 done:
1260 if (srv->respect_exportok &&
1261 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1262 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1263 goto err;
1266 repo = find_cached_repo(srv, repo_dir->path);
1267 if (repo == NULL) {
1268 error = cache_repo(&repo, srv, repo_dir, sock);
1269 if (error)
1270 goto err;
1272 t->repo = repo;
1273 error = gotweb_get_repo_description(&repo_dir->description, srv,
1274 repo_dir->path, dirfd(dt));
1275 if (error)
1276 goto err;
1277 error = got_get_repo_owner(&repo_dir->owner, c);
1278 if (error)
1279 goto err;
1280 error = got_get_repo_age(&repo_dir->age, c, NULL);
1281 if (error)
1282 goto err;
1283 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1284 dirfd(dt));
1285 err:
1286 free(dir_test);
1287 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1288 error = got_error_from_errno("closedir");
1289 return error;
1292 static const struct got_error *
1293 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1295 const struct got_error *error;
1297 *repo_dir = calloc(1, sizeof(**repo_dir));
1298 if (*repo_dir == NULL)
1299 return got_error_from_errno("calloc");
1301 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1302 error = got_error_from_errno("asprintf");
1303 free(*repo_dir);
1304 *repo_dir = NULL;
1305 return error;
1307 (*repo_dir)->owner = NULL;
1308 (*repo_dir)->description = NULL;
1309 (*repo_dir)->url = NULL;
1310 (*repo_dir)->path = NULL;
1312 return NULL;
1315 static const struct got_error *
1316 gotweb_get_repo_description(char **description, struct server *srv,
1317 const char *dirpath, int dir)
1319 const struct got_error *error = NULL;
1320 struct stat sb;
1321 int fd = -1;
1322 off_t len;
1324 *description = NULL;
1325 if (srv->show_repo_description == 0)
1326 return NULL;
1328 fd = openat(dir, "description", O_RDONLY);
1329 if (fd == -1) {
1330 if (errno != ENOENT && errno != EACCES) {
1331 error = got_error_from_errno_fmt("openat %s/%s",
1332 dirpath, "description");
1334 goto done;
1337 if (fstat(fd, &sb) == -1) {
1338 error = got_error_from_errno_fmt("fstat %s/%s",
1339 dirpath, "description");
1340 goto done;
1343 len = sb.st_size;
1344 if (len > GOTWEBD_MAXDESCRSZ - 1)
1345 len = GOTWEBD_MAXDESCRSZ - 1;
1347 *description = calloc(len + 1, sizeof(**description));
1348 if (*description == NULL) {
1349 error = got_error_from_errno("calloc");
1350 goto done;
1353 if (read(fd, *description, len) == -1)
1354 error = got_error_from_errno("read");
1355 done:
1356 if (fd != -1 && close(fd) == -1 && error == NULL)
1357 error = got_error_from_errno("close");
1358 return error;
1361 static const struct got_error *
1362 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1363 int dir)
1365 const struct got_error *error = NULL;
1366 struct stat sb;
1367 int fd = -1;
1368 off_t len;
1370 *url = NULL;
1371 if (srv->show_repo_cloneurl == 0)
1372 return NULL;
1374 fd = openat(dir, "cloneurl", O_RDONLY);
1375 if (fd == -1) {
1376 if (errno != ENOENT && errno != EACCES) {
1377 error = got_error_from_errno_fmt("openat %s/%s",
1378 dirpath, "cloneurl");
1380 goto done;
1383 if (fstat(fd, &sb) == -1) {
1384 error = got_error_from_errno_fmt("fstat %s/%s",
1385 dirpath, "cloneurl");
1386 goto done;
1389 len = sb.st_size;
1390 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1391 len = GOTWEBD_MAXCLONEURLSZ - 1;
1393 *url = calloc(len + 1, sizeof(**url));
1394 if (*url == NULL) {
1395 error = got_error_from_errno("calloc");
1396 goto done;
1399 if (read(fd, *url, len) == -1)
1400 error = got_error_from_errno("read");
1401 done:
1402 if (fd != -1 && close(fd) == -1 && error == NULL)
1403 error = got_error_from_errno("close");
1404 return error;
1407 int
1408 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1410 struct request *c = tp->tp_arg;
1411 struct tm tm;
1412 long long diff_time;
1413 const char *years = "years ago", *months = "months ago";
1414 const char *weeks = "weeks ago", *days = "days ago";
1415 const char *hours = "hours ago", *minutes = "minutes ago";
1416 const char *seconds = "seconds ago", *now = "right now";
1417 char *s;
1418 char datebuf[64];
1419 size_t r;
1421 switch (ref_tm) {
1422 case TM_DIFF:
1423 diff_time = time(NULL) - committer_time;
1424 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1425 if (fcgi_printf(c, "%lld %s",
1426 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1427 return -1;
1428 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1429 if (fcgi_printf(c, "%lld %s",
1430 (diff_time / 60 / 60 / 24 / (365 / 12)),
1431 months) == -1)
1432 return -1;
1433 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1434 if (fcgi_printf(c, "%lld %s",
1435 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1436 return -1;
1437 } else if (diff_time > 60 * 60 * 24 * 2) {
1438 if (fcgi_printf(c, "%lld %s",
1439 (diff_time / 60 / 60 / 24), days) == -1)
1440 return -1;
1441 } else if (diff_time > 60 * 60 * 2) {
1442 if (fcgi_printf(c, "%lld %s",
1443 (diff_time / 60 / 60), hours) == -1)
1444 return -1;
1445 } else if (diff_time > 60 * 2) {
1446 if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1447 minutes) == -1)
1448 return -1;
1449 } else if (diff_time > 2) {
1450 if (fcgi_printf(c, "%lld %s", diff_time,
1451 seconds) == -1)
1452 return -1;
1453 } else {
1454 if (fcgi_puts(tp, now) == -1)
1455 return -1;
1457 break;
1458 case TM_LONG:
1459 if (gmtime_r(&committer_time, &tm) == NULL)
1460 return -1;
1462 s = asctime_r(&tm, datebuf);
1463 if (s == NULL)
1464 return -1;
1466 if (fcgi_puts(tp, datebuf) == -1 ||
1467 fcgi_puts(tp, " UTC") == -1)
1468 return -1;
1469 break;
1470 case TM_RFC822:
1471 if (gmtime_r(&committer_time, &tm) == NULL)
1472 return -1;
1474 r = strftime(datebuf, sizeof(datebuf),
1475 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1476 if (r == 0)
1477 return -1;
1479 if (fcgi_puts(tp, datebuf) == -1)
1480 return -1;
1481 break;
1483 return 0;