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 "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 int gotweb_render_index(struct template *);
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(const char *);
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;
147 struct server *srv = NULL;
148 struct querystring *qs = NULL;
149 struct repo_dir *repo_dir = NULL;
150 const char *rss_ctype = "application/rss+xml;charset=utf-8";
151 const uint8_t *buf;
152 size_t len;
153 int r, binary = 0;
155 /* init the transport */
156 error = gotweb_init_transport(&c->t);
157 if (error) {
158 log_warnx("%s: %s", __func__, error->msg);
159 return;
161 /* don't process any further if client disconnected */
162 if (c->sock->client_status == CLIENT_DISCONNECT)
163 return;
164 /* get the gotwebd server */
165 srv = gotweb_get_server(c->server_name);
166 if (srv == NULL) {
167 log_warnx("%s: error server is NULL", __func__);
168 goto err;
170 c->srv = srv;
171 /* parse our querystring */
172 error = gotweb_init_querystring(&qs);
173 if (error) {
174 log_warnx("%s: %s", __func__, error->msg);
175 goto err;
177 c->t->qs = qs;
178 error = gotweb_parse_querystring(&qs, c->querystring);
179 if (error) {
180 log_warnx("%s: %s", __func__, error->msg);
181 goto err;
184 /*
185 * certain actions require a commit id in the querystring. this stops
186 * bad actors from exploiting this by manually manipulating the
187 * querystring.
188 */
190 if (qs->action == BLAME || qs->action == BLOB ||
191 qs->action == BLOBRAW || qs->action == DIFF) {
192 if (qs->commit == NULL) {
193 error = got_error(GOT_ERR_BAD_QUERYSTRING);
194 goto err;
198 if (qs->action != INDEX) {
199 error = gotweb_init_repo_dir(&repo_dir, qs->path);
200 if (error)
201 goto err;
202 error = gotweb_load_got_path(c, repo_dir);
203 c->t->repo_dir = repo_dir;
204 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
205 goto err;
208 if (qs->action == BLOBRAW || qs->action == BLOB) {
209 error = got_get_repo_commits(c, 1);
210 if (error)
211 goto err;
213 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
214 &binary, c);
215 if (error)
216 goto err;
219 switch(qs->action) {
220 case BLAME:
221 error = got_get_repo_commits(c, 1);
222 if (error) {
223 log_warnx("%s: %s", __func__, error->msg);
224 goto err;
226 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
227 return;
228 gotweb_render_page(c->tp, gotweb_render_blame);
229 return;
230 case BLOB:
231 if (binary) {
232 struct gotweb_url url = {
233 .index_page = -1,
234 .page = -1,
235 .action = BLOBRAW,
236 .path = qs->path,
237 .commit = qs->commit,
238 .folder = qs->folder,
239 .file = qs->file,
240 };
242 gotweb_reply(c, 302, NULL, &url);
243 return;
246 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
247 return;
248 gotweb_render_page(c->tp, gotweb_render_blob);
249 return;
250 case BLOBRAW:
251 if (binary)
252 r = gotweb_reply_file(c, "application/octet-stream",
253 qs->file, NULL);
254 else
255 r = gotweb_reply(c, 200, "text/plain", NULL);
256 if (r == -1)
257 return;
259 for (;;) {
260 error = got_object_blob_read_block(&len, c->t->blob);
261 if (error)
262 break;
263 if (len == 0)
264 break;
265 buf = got_object_blob_get_read_buf(c->t->blob);
266 if (fcgi_gen_binary_response(c, buf, len) == -1)
267 break;
269 return;
270 case BRIEFS:
271 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
272 return;
273 gotweb_render_page(c->tp, gotweb_render_briefs);
274 return;
275 case COMMITS:
276 error = got_get_repo_commits(c, srv->max_commits_display);
277 if (error) {
278 log_warnx("%s: %s", __func__, error->msg);
279 goto err;
281 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
282 return;
283 gotweb_render_page(c->tp, gotweb_render_commits);
284 return;
285 case DIFF:
286 error = got_get_repo_commits(c, 1);
287 if (error) {
288 log_warnx("%s: %s", __func__, error->msg);
289 goto err;
291 error = got_open_diff_for_output(&c->t->fp, c);
292 if (error) {
293 log_warnx("%s: %s", __func__, error->msg);
294 goto err;
296 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
297 return;
298 gotweb_render_page(c->tp, gotweb_render_diff);
299 return;
300 case INDEX:
301 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
302 alphasort);
303 if (c->t->nrepos == -1) {
304 c->t->repos = NULL;
305 error = got_error_from_errno2("scandir",
306 srv->repos_path);
307 goto err;
309 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
310 return;
311 gotweb_render_page(c->tp, gotweb_render_index);
312 return;
313 case RSS:
314 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
315 if (error)
316 goto err;
317 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
318 == -1)
319 return;
320 gotweb_render_rss(c->tp);
321 return;
322 case SUMMARY:
323 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
324 got_ref_cmp_by_name, NULL);
325 if (error) {
326 log_warnx("%s: got_ref_list: %s", __func__,
327 error->msg);
328 goto err;
330 qs->action = TAGS;
331 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
332 if (error) {
333 log_warnx("%s: got_get_repo_tags: %s", __func__,
334 error->msg);
335 goto err;
337 qs->action = SUMMARY;
338 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
339 return;
340 gotweb_render_page(c->tp, gotweb_render_summary);
341 return;
342 case TAG:
343 error = got_get_repo_tags(c, 1);
344 if (error) {
345 log_warnx("%s: %s", __func__, error->msg);
346 goto err;
348 if (c->t->tag_count == 0) {
349 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
350 "bad commit id");
351 goto err;
353 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
354 return;
355 gotweb_render_page(c->tp, gotweb_render_tag);
356 return;
357 case TAGS:
358 error = got_get_repo_tags(c, srv->max_commits_display);
359 if (error) {
360 log_warnx("%s: %s", __func__, error->msg);
361 goto err;
363 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
364 return;
365 gotweb_render_page(c->tp, gotweb_render_tags);
366 return;
367 case TREE:
368 error = got_get_repo_commits(c, 1);
369 if (error) {
370 log_warnx("%s: %s", __func__, error->msg);
371 goto err;
373 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
374 return;
375 gotweb_render_page(c->tp, gotweb_render_tree);
376 return;
377 case ERR:
378 default:
379 error = got_error(GOT_ERR_BAD_QUERYSTRING);
382 err:
383 c->t->error = error;
384 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
385 return;
386 gotweb_render_page(c->tp, gotweb_render_error);
389 struct server *
390 gotweb_get_server(const char *server_name)
392 struct server *srv;
394 /* check against the server name first */
395 if (*server_name != '\0')
396 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
397 if (strcmp(srv->name, server_name) == 0)
398 return srv;
400 /* otherwise, use the first server */
401 return TAILQ_FIRST(&gotwebd_env->servers);
402 };
404 const struct got_error *
405 gotweb_init_transport(struct transport **t)
407 const struct got_error *error = NULL;
409 *t = calloc(1, sizeof(**t));
410 if (*t == NULL)
411 return got_error_from_errno2(__func__, "calloc");
413 TAILQ_INIT(&(*t)->repo_commits);
414 TAILQ_INIT(&(*t)->repo_tags);
415 TAILQ_INIT(&(*t)->refs);
417 (*t)->fd = -1;
419 return error;
422 static const struct got_error *
423 gotweb_init_querystring(struct querystring **qs)
425 const struct got_error *error = NULL;
427 *qs = calloc(1, sizeof(**qs));
428 if (*qs == NULL)
429 return got_error_from_errno2(__func__, "calloc");
431 (*qs)->headref = strdup("HEAD");
432 if ((*qs)->headref == NULL) {
433 free(*qs);
434 *qs = NULL;
435 return got_error_from_errno2(__func__, "strdup");
438 (*qs)->action = INDEX;
439 (*qs)->commit = NULL;
440 (*qs)->file = NULL;
441 (*qs)->folder = NULL;
442 (*qs)->index_page = 0;
443 (*qs)->path = NULL;
445 return error;
448 static const struct got_error *
449 gotweb_parse_querystring(struct querystring **qs, char *qst)
451 const struct got_error *error = NULL;
452 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
453 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
455 if (qst == NULL)
456 return error;
458 tok1 = strdup(qst);
459 if (tok1 == NULL)
460 return got_error_from_errno2(__func__, "strdup");
462 tok1_pair = tok1;
463 tok1_end = tok1;
465 while (tok1_pair != NULL) {
466 strsep(&tok1_end, "&");
468 tok2 = strdup(tok1_pair);
469 if (tok2 == NULL) {
470 free(tok1);
471 return got_error_from_errno2(__func__, "strdup");
474 tok2_pair = tok2;
475 tok2_end = tok2;
477 while (tok2_pair != NULL) {
478 strsep(&tok2_end, "=");
479 if (tok2_end) {
480 error = gotweb_assign_querystring(qs, tok2_pair,
481 tok2_end);
482 if (error)
483 goto err;
485 tok2_pair = tok2_end;
487 free(tok2);
488 tok1_pair = tok1_end;
490 free(tok1);
491 return error;
492 err:
493 free(tok2);
494 free(tok1);
495 return error;
498 /*
499 * Adapted from usr.sbin/httpd/httpd.c url_decode.
500 */
501 static const struct got_error *
502 gotweb_urldecode(char *url)
504 char *p, *q;
505 char hex[3];
506 unsigned long x;
508 hex[2] = '\0';
509 p = q = url;
511 while (*p != '\0') {
512 switch (*p) {
513 case '%':
514 /* Encoding character is followed by two hex chars */
515 if (!isxdigit((unsigned char)p[1]) ||
516 !isxdigit((unsigned char)p[2]) ||
517 (p[1] == '0' && p[2] == '0'))
518 return got_error(GOT_ERR_BAD_QUERYSTRING);
520 hex[0] = p[1];
521 hex[1] = p[2];
523 /*
524 * We don't have to validate "hex" because it is
525 * guaranteed to include two hex chars followed by nul.
526 */
527 x = strtoul(hex, NULL, 16);
528 *q = (char)x;
529 p += 2;
530 break;
531 default:
532 *q = *p;
533 break;
535 p++;
536 q++;
538 *q = '\0';
540 return NULL;
543 static const struct got_error *
544 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
546 const struct got_error *error = NULL;
547 const char *errstr;
548 int a_cnt, el_cnt;
550 error = gotweb_urldecode(value);
551 if (error)
552 return error;
554 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
555 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
556 continue;
558 switch (querystring_keys[el_cnt].element) {
559 case ACTION:
560 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
561 if (strcmp(value, action_keys[a_cnt].name) != 0)
562 continue;
563 else if (strcmp(value,
564 action_keys[a_cnt].name) == 0){
565 (*qs)->action =
566 action_keys[a_cnt].action;
567 goto qa_found;
570 (*qs)->action = ERR;
571 qa_found:
572 break;
573 case COMMIT:
574 (*qs)->commit = strdup(value);
575 if ((*qs)->commit == NULL) {
576 error = got_error_from_errno2(__func__,
577 "strdup");
578 goto done;
580 break;
581 case RFILE:
582 (*qs)->file = strdup(value);
583 if ((*qs)->file == NULL) {
584 error = got_error_from_errno2(__func__,
585 "strdup");
586 goto done;
588 break;
589 case FOLDER:
590 (*qs)->folder = strdup(value);
591 if ((*qs)->folder == NULL) {
592 error = got_error_from_errno2(__func__,
593 "strdup");
594 goto done;
596 break;
597 case HEADREF:
598 free((*qs)->headref);
599 (*qs)->headref = strdup(value);
600 if ((*qs)->headref == NULL) {
601 error = got_error_from_errno2(__func__,
602 "strdup");
603 goto done;
605 break;
606 case INDEX_PAGE:
607 if (*value == '\0')
608 break;
609 (*qs)->index_page = strtonum(value, INT64_MIN,
610 INT64_MAX, &errstr);
611 if (errstr) {
612 error = got_error_from_errno3(__func__,
613 "strtonum", errstr);
614 goto done;
616 if ((*qs)->index_page < 0)
617 (*qs)->index_page = 0;
618 break;
619 case PATH:
620 (*qs)->path = strdup(value);
621 if ((*qs)->path == NULL) {
622 error = got_error_from_errno2(__func__,
623 "strdup");
624 goto done;
626 break;
627 case PAGE:
628 if (*value == '\0')
629 break;
630 (*qs)->page = strtonum(value, INT64_MIN,
631 INT64_MAX, &errstr);
632 if (errstr) {
633 error = got_error_from_errno3(__func__,
634 "strtonum", errstr);
635 goto done;
637 if ((*qs)->page < 0)
638 (*qs)->page = 0;
639 break;
640 default:
641 break;
644 done:
645 return error;
648 void
649 gotweb_free_repo_tag(struct repo_tag *rt)
651 if (rt != NULL) {
652 free(rt->commit_id);
653 free(rt->tag_name);
654 free(rt->tag_commit);
655 free(rt->commit_msg);
656 free(rt->tagger);
658 free(rt);
661 void
662 gotweb_free_repo_commit(struct repo_commit *rc)
664 if (rc != NULL) {
665 free(rc->path);
666 free(rc->refs_str);
667 free(rc->commit_id);
668 free(rc->parent_id);
669 free(rc->tree_id);
670 free(rc->author);
671 free(rc->committer);
672 free(rc->commit_msg);
674 free(rc);
677 static void
678 gotweb_free_querystring(struct querystring *qs)
680 if (qs != NULL) {
681 free(qs->commit);
682 free(qs->file);
683 free(qs->folder);
684 free(qs->headref);
685 free(qs->path);
687 free(qs);
690 static void
691 gotweb_free_repo_dir(struct repo_dir *repo_dir)
693 if (repo_dir != NULL) {
694 free(repo_dir->name);
695 free(repo_dir->owner);
696 free(repo_dir->description);
697 free(repo_dir->url);
698 free(repo_dir->path);
700 free(repo_dir);
703 void
704 gotweb_free_transport(struct transport *t)
706 const struct got_error *err;
707 struct repo_commit *rc = NULL, *trc = NULL;
708 struct repo_tag *rt = NULL, *trt = NULL;
709 int i;
711 got_ref_list_free(&t->refs);
712 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
713 TAILQ_REMOVE(&t->repo_commits, rc, entry);
714 gotweb_free_repo_commit(rc);
716 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
717 TAILQ_REMOVE(&t->repo_tags, rt, entry);
718 gotweb_free_repo_tag(rt);
720 gotweb_free_repo_dir(t->repo_dir);
721 gotweb_free_querystring(t->qs);
722 free(t->more_id);
723 free(t->next_id);
724 free(t->prev_id);
725 if (t->blob)
726 got_object_blob_close(t->blob);
727 if (t->fp) {
728 err = got_gotweb_closefile(t->fp);
729 if (err)
730 log_warnx("%s: got_gotweb_closefile failure: %s",
731 __func__, err->msg);
733 if (t->fd != -1 && close(t->fd) == -1)
734 log_warn("%s: close", __func__);
735 if (t->repos) {
736 for (i = 0; i < t->nrepos; ++i)
737 free(t->repos[i]);
738 free(t->repos);
740 free(t);
743 void
744 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
745 struct gotweb_url *next, int *have_next)
747 struct transport *t = c->t;
748 struct querystring *qs = t->qs;
749 struct server *srv = c->srv;
751 *have_prev = *have_next = 0;
753 switch(qs->action) {
754 case INDEX:
755 if (qs->index_page > 0) {
756 *have_prev = 1;
757 *prev = (struct gotweb_url){
758 .action = -1,
759 .index_page = qs->index_page - 1,
760 .page = -1,
761 };
763 if (t->next_disp == srv->max_repos_display &&
764 t->repos_total != (qs->index_page + 1) *
765 srv->max_repos_display) {
766 *have_next = 1;
767 *next = (struct gotweb_url){
768 .action = -1,
769 .index_page = qs->index_page + 1,
770 .page = -1,
771 };
773 break;
774 case TAGS:
775 if (t->prev_id && qs->commit != NULL &&
776 strcmp(qs->commit, t->prev_id) != 0) {
777 *have_prev = 1;
778 *prev = (struct gotweb_url){
779 .action = TAGS,
780 .index_page = -1,
781 .page = qs->page - 1,
782 .path = qs->path,
783 .commit = t->prev_id,
784 .headref = qs->headref,
785 };
787 if (t->next_id) {
788 *have_next = 1;
789 *next = (struct gotweb_url){
790 .action = TAGS,
791 .index_page = -1,
792 .page = qs->page + 1,
793 .path = qs->path,
794 .commit = t->next_id,
795 .headref = qs->headref,
796 };
798 break;
802 static int
803 gotweb_render_index(struct template *tp)
805 const struct got_error *error = NULL;
806 struct request *c = tp->tp_arg;
807 struct server *srv = c->srv;
808 struct transport *t = c->t;
809 struct querystring *qs = t->qs;
810 struct repo_dir *repo_dir = NULL;
811 struct dirent **sd_dent = t->repos;
812 unsigned int d_i, d_disp = 0;
813 unsigned int d_skipped = 0;
814 int type, r;
816 if (gotweb_render_repo_table_hdr(c->tp) == -1)
817 return -1;
819 for (d_i = 0; d_i < t->nrepos; d_i++) {
820 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
821 break;
823 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
824 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
825 d_skipped++;
826 continue;
829 error = got_path_dirent_type(&type, srv->repos_path,
830 sd_dent[d_i]);
831 if (error)
832 continue;
833 if (type != DT_DIR) {
834 d_skipped++;
835 continue;
838 if (qs->index_page > 0 && (qs->index_page *
839 srv->max_repos_display) > t->prev_disp) {
840 t->prev_disp++;
841 continue;
844 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
845 if (error)
846 continue;
848 error = gotweb_load_got_path(c, repo_dir);
849 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
850 if (error->code != GOT_ERR_NOT_GIT_REPO)
851 log_warnx("%s: %s: %s", __func__,
852 sd_dent[d_i]->d_name, error->msg);
853 gotweb_free_repo_dir(repo_dir);
854 repo_dir = NULL;
855 d_skipped++;
856 continue;
859 d_disp++;
860 t->prev_disp++;
862 r = gotweb_render_repo_fragment(c->tp, repo_dir);
863 gotweb_free_repo_dir(repo_dir);
864 if (r == -1)
865 return -1;
867 t->next_disp++;
868 if (d_disp == srv->max_repos_display)
869 break;
871 t->repos_total = t->nrepos - d_skipped;
873 if (srv->max_repos_display == 0)
874 return 0;
875 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
876 return 0;
877 if (t->repos_total <= srv->max_repos ||
878 t->repos_total <= srv->max_repos_display)
879 return 0;
881 if (gotweb_render_navs(c->tp) == -1)
882 return -1;
884 return 0;
887 static inline int
888 should_urlencode(int c)
890 if (c <= ' ' || c >= 127)
891 return 1;
893 switch (c) {
894 /* gen-delim */
895 case ':':
896 case '/':
897 case '?':
898 case '#':
899 case '[':
900 case ']':
901 case '@':
902 /* sub-delims */
903 case '!':
904 case '$':
905 case '&':
906 case '\'':
907 case '(':
908 case ')':
909 case '*':
910 case '+':
911 case ',':
912 case ';':
913 case '=':
914 /* needed because the URLs are embedded into the HTML */
915 case '\"':
916 return 1;
917 default:
918 return 0;
922 static char *
923 gotweb_urlencode(const char *str)
925 const char *s;
926 char *escaped;
927 size_t i, len;
928 int a, b;
930 len = 0;
931 for (s = str; *s; ++s) {
932 len++;
933 if (should_urlencode(*s))
934 len += 2;
937 escaped = calloc(1, len + 1);
938 if (escaped == NULL)
939 return NULL;
941 i = 0;
942 for (s = str; *s; ++s) {
943 if (should_urlencode(*s)) {
944 a = (*s & 0xF0) >> 4;
945 b = (*s & 0x0F);
947 escaped[i++] = '%';
948 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
949 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
950 } else
951 escaped[i++] = *s;
954 return escaped;
957 const char *
958 gotweb_action_name(int action)
960 switch (action) {
961 case BLAME:
962 return "blame";
963 case BLOB:
964 return "blob";
965 case BLOBRAW:
966 return "blobraw";
967 case BRIEFS:
968 return "briefs";
969 case COMMITS:
970 return "commits";
971 case DIFF:
972 return "diff";
973 case ERR:
974 return "err";
975 case INDEX:
976 return "index";
977 case SUMMARY:
978 return "summary";
979 case TAG:
980 return "tag";
981 case TAGS:
982 return "tags";
983 case TREE:
984 return "tree";
985 case RSS:
986 return "rss";
987 default:
988 return NULL;
992 int
993 gotweb_render_url(struct request *c, struct gotweb_url *url)
995 const char *sep = "?", *action;
996 char *tmp;
997 int r;
999 action = gotweb_action_name(url->action);
1000 if (action != NULL) {
1001 if (fcgi_printf(c, "?action=%s", action) == -1)
1002 return -1;
1003 sep = "&";
1006 if (url->commit) {
1007 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1008 return -1;
1009 sep = "&";
1012 if (url->previd) {
1013 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1014 return -1;
1015 sep = "&";
1018 if (url->prevset) {
1019 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1020 return -1;
1021 sep = "&";
1024 if (url->file) {
1025 tmp = gotweb_urlencode(url->file);
1026 if (tmp == NULL)
1027 return -1;
1028 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1029 free(tmp);
1030 if (r == -1)
1031 return -1;
1032 sep = "&";
1035 if (url->folder) {
1036 tmp = gotweb_urlencode(url->folder);
1037 if (tmp == NULL)
1038 return -1;
1039 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1040 free(tmp);
1041 if (r == -1)
1042 return -1;
1043 sep = "&";
1046 if (url->headref) {
1047 tmp = gotweb_urlencode(url->headref);
1048 if (tmp == NULL)
1049 return -1;
1050 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1051 free(tmp);
1052 if (r == -1)
1053 return -1;
1054 sep = "&";
1057 if (url->index_page != -1) {
1058 if (fcgi_printf(c, "%sindex_page=%d", sep,
1059 url->index_page) == -1)
1060 return -1;
1061 sep = "&";
1064 if (url->path) {
1065 tmp = gotweb_urlencode(url->path);
1066 if (tmp == NULL)
1067 return -1;
1068 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1069 free(tmp);
1070 if (r == -1)
1071 return -1;
1072 sep = "&";
1075 if (url->page != -1) {
1076 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1077 return -1;
1078 sep = "&";
1081 return 0;
1084 int
1085 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1087 struct template *tp = c->tp;
1088 const char *proto = c->https ? "https" : "http";
1090 if (fcgi_puts(tp, proto) == -1 ||
1091 fcgi_puts(tp, "://") == -1 ||
1092 tp_htmlescape(tp, c->server_name) == -1 ||
1093 tp_htmlescape(tp, c->document_uri) == -1)
1094 return -1;
1096 return gotweb_render_url(c, url);
1099 static struct got_repository *
1100 find_cached_repo(struct server *srv, const char *path)
1102 int i;
1104 for (i = 0; i < srv->ncached_repos; i++) {
1105 if (strcmp(srv->cached_repos[i].path, path) == 0)
1106 return srv->cached_repos[i].repo;
1109 return NULL;
1112 static const struct got_error *
1113 cache_repo(struct got_repository **new, struct server *srv,
1114 struct repo_dir *repo_dir, struct socket *sock)
1116 const struct got_error *error = NULL;
1117 struct got_repository *repo;
1118 struct cached_repo *cr;
1119 int evicted = 0;
1121 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1122 cr = &srv->cached_repos[srv->ncached_repos - 1];
1123 error = got_repo_close(cr->repo);
1124 memset(cr, 0, sizeof(*cr));
1125 srv->ncached_repos--;
1126 if (error)
1127 return error;
1128 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1129 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1130 cr = &srv->cached_repos[0];
1131 evicted = 1;
1132 } else {
1133 cr = &srv->cached_repos[srv->ncached_repos];
1136 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1137 if (error) {
1138 if (evicted) {
1139 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1140 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1142 return error;
1145 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1146 >= sizeof(cr->path)) {
1147 if (evicted) {
1148 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1149 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1151 return got_error(GOT_ERR_NO_SPACE);
1154 cr->repo = repo;
1155 srv->ncached_repos++;
1156 *new = repo;
1157 return NULL;
1160 static const struct got_error *
1161 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1163 const struct got_error *error = NULL;
1164 struct socket *sock = c->sock;
1165 struct server *srv = c->srv;
1166 struct transport *t = c->t;
1167 struct got_repository *repo = NULL;
1168 DIR *dt;
1169 char *dir_test;
1171 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1172 GOTWEB_GIT_DIR) == -1)
1173 return got_error_from_errno("asprintf");
1175 dt = opendir(dir_test);
1176 if (dt == NULL) {
1177 free(dir_test);
1178 } else {
1179 repo_dir->path = dir_test;
1180 dir_test = NULL;
1181 goto done;
1184 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1185 repo_dir->name) == -1)
1186 return got_error_from_errno("asprintf");
1188 dt = opendir(dir_test);
1189 if (dt == NULL) {
1190 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1191 goto err;
1192 } else {
1193 repo_dir->path = dir_test;
1194 dir_test = NULL;
1197 done:
1198 if (srv->respect_exportok &&
1199 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1200 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1201 goto err;
1204 repo = find_cached_repo(srv, repo_dir->path);
1205 if (repo == NULL) {
1206 error = cache_repo(&repo, srv, repo_dir, sock);
1207 if (error)
1208 goto err;
1210 t->repo = repo;
1211 error = gotweb_get_repo_description(&repo_dir->description, srv,
1212 repo_dir->path, dirfd(dt));
1213 if (error)
1214 goto err;
1215 error = got_get_repo_owner(&repo_dir->owner, c);
1216 if (error)
1217 goto err;
1218 error = got_get_repo_age(&repo_dir->age, c, NULL);
1219 if (error)
1220 goto err;
1221 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1222 dirfd(dt));
1223 err:
1224 free(dir_test);
1225 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1226 error = got_error_from_errno("closedir");
1227 return error;
1230 static const struct got_error *
1231 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1233 const struct got_error *error;
1235 *repo_dir = calloc(1, sizeof(**repo_dir));
1236 if (*repo_dir == NULL)
1237 return got_error_from_errno("calloc");
1239 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1240 error = got_error_from_errno("asprintf");
1241 free(*repo_dir);
1242 *repo_dir = NULL;
1243 return error;
1245 (*repo_dir)->owner = NULL;
1246 (*repo_dir)->description = NULL;
1247 (*repo_dir)->url = NULL;
1248 (*repo_dir)->path = NULL;
1250 return NULL;
1253 static const struct got_error *
1254 gotweb_get_repo_description(char **description, struct server *srv,
1255 const char *dirpath, int dir)
1257 const struct got_error *error = NULL;
1258 struct stat sb;
1259 int fd = -1;
1260 off_t len;
1262 *description = NULL;
1263 if (srv->show_repo_description == 0)
1264 return NULL;
1266 fd = openat(dir, "description", O_RDONLY);
1267 if (fd == -1) {
1268 if (errno != ENOENT && errno != EACCES) {
1269 error = got_error_from_errno_fmt("openat %s/%s",
1270 dirpath, "description");
1272 goto done;
1275 if (fstat(fd, &sb) == -1) {
1276 error = got_error_from_errno_fmt("fstat %s/%s",
1277 dirpath, "description");
1278 goto done;
1281 len = sb.st_size;
1282 if (len > GOTWEBD_MAXDESCRSZ - 1)
1283 len = GOTWEBD_MAXDESCRSZ - 1;
1285 *description = calloc(len + 1, sizeof(**description));
1286 if (*description == NULL) {
1287 error = got_error_from_errno("calloc");
1288 goto done;
1291 if (read(fd, *description, len) == -1)
1292 error = got_error_from_errno("read");
1293 done:
1294 if (fd != -1 && close(fd) == -1 && error == NULL)
1295 error = got_error_from_errno("close");
1296 return error;
1299 static const struct got_error *
1300 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1301 int dir)
1303 const struct got_error *error = NULL;
1304 struct stat sb;
1305 int fd = -1;
1306 off_t len;
1308 *url = NULL;
1309 if (srv->show_repo_cloneurl == 0)
1310 return NULL;
1312 fd = openat(dir, "cloneurl", O_RDONLY);
1313 if (fd == -1) {
1314 if (errno != ENOENT && errno != EACCES) {
1315 error = got_error_from_errno_fmt("openat %s/%s",
1316 dirpath, "cloneurl");
1318 goto done;
1321 if (fstat(fd, &sb) == -1) {
1322 error = got_error_from_errno_fmt("fstat %s/%s",
1323 dirpath, "cloneurl");
1324 goto done;
1327 len = sb.st_size;
1328 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1329 len = GOTWEBD_MAXCLONEURLSZ - 1;
1331 *url = calloc(len + 1, sizeof(**url));
1332 if (*url == NULL) {
1333 error = got_error_from_errno("calloc");
1334 goto done;
1337 if (read(fd, *url, len) == -1)
1338 error = got_error_from_errno("read");
1339 done:
1340 if (fd != -1 && close(fd) == -1 && error == NULL)
1341 error = got_error_from_errno("close");
1342 return error;
1345 int
1346 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1348 struct request *c = tp->tp_arg;
1349 struct tm tm;
1350 long long diff_time;
1351 const char *years = "years ago", *months = "months ago";
1352 const char *weeks = "weeks ago", *days = "days ago";
1353 const char *hours = "hours ago", *minutes = "minutes ago";
1354 const char *seconds = "seconds ago", *now = "right now";
1355 char *s;
1356 char datebuf[64];
1357 size_t r;
1359 switch (ref_tm) {
1360 case TM_DIFF:
1361 diff_time = time(NULL) - committer_time;
1362 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1363 if (fcgi_printf(c, "%lld %s",
1364 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1365 return -1;
1366 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1367 if (fcgi_printf(c, "%lld %s",
1368 (diff_time / 60 / 60 / 24 / (365 / 12)),
1369 months) == -1)
1370 return -1;
1371 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1372 if (fcgi_printf(c, "%lld %s",
1373 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1374 return -1;
1375 } else if (diff_time > 60 * 60 * 24 * 2) {
1376 if (fcgi_printf(c, "%lld %s",
1377 (diff_time / 60 / 60 / 24), days) == -1)
1378 return -1;
1379 } else if (diff_time > 60 * 60 * 2) {
1380 if (fcgi_printf(c, "%lld %s",
1381 (diff_time / 60 / 60), hours) == -1)
1382 return -1;
1383 } else if (diff_time > 60 * 2) {
1384 if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1385 minutes) == -1)
1386 return -1;
1387 } else if (diff_time > 2) {
1388 if (fcgi_printf(c, "%lld %s", diff_time,
1389 seconds) == -1)
1390 return -1;
1391 } else {
1392 if (fcgi_puts(tp, now) == -1)
1393 return -1;
1395 break;
1396 case TM_LONG:
1397 if (gmtime_r(&committer_time, &tm) == NULL)
1398 return -1;
1400 s = asctime_r(&tm, datebuf);
1401 if (s == NULL)
1402 return -1;
1404 if (fcgi_puts(tp, datebuf) == -1 ||
1405 fcgi_puts(tp, " UTC") == -1)
1406 return -1;
1407 break;
1408 case TM_RFC822:
1409 if (gmtime_r(&committer_time, &tm) == NULL)
1410 return -1;
1412 r = strftime(datebuf, sizeof(datebuf),
1413 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1414 if (r == 0)
1415 return -1;
1417 if (fcgi_puts(tp, datebuf) == -1)
1418 return -1;
1419 break;
1421 return 0;