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 error = got_get_repo_commits(c, srv->max_commits_display);
272 if (error)
273 goto err;
274 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
275 return;
276 gotweb_render_page(c->tp, gotweb_render_briefs);
277 return;
278 case COMMITS:
279 error = got_get_repo_commits(c, srv->max_commits_display);
280 if (error) {
281 log_warnx("%s: %s", __func__, error->msg);
282 goto err;
284 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
285 return;
286 gotweb_render_page(c->tp, gotweb_render_commits);
287 return;
288 case DIFF:
289 error = got_get_repo_commits(c, 1);
290 if (error) {
291 log_warnx("%s: %s", __func__, error->msg);
292 goto err;
294 error = got_open_diff_for_output(&c->t->fp, c);
295 if (error) {
296 log_warnx("%s: %s", __func__, error->msg);
297 goto err;
299 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
300 return;
301 gotweb_render_page(c->tp, gotweb_render_diff);
302 return;
303 case INDEX:
304 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
305 alphasort);
306 if (c->t->nrepos == -1) {
307 c->t->repos = NULL;
308 error = got_error_from_errno2("scandir",
309 srv->repos_path);
310 goto err;
312 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
313 return;
314 gotweb_render_page(c->tp, gotweb_render_index);
315 return;
316 case RSS:
317 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
318 if (error)
319 goto err;
320 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
321 == -1)
322 return;
323 gotweb_render_rss(c->tp);
324 return;
325 case SUMMARY:
326 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
327 got_ref_cmp_by_name, NULL);
328 if (error) {
329 log_warnx("%s: got_ref_list: %s", __func__,
330 error->msg);
331 goto err;
333 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
334 if (error)
335 goto err;
336 qs->action = TAGS;
337 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
338 if (error) {
339 log_warnx("%s: got_get_repo_tags: %s", __func__,
340 error->msg);
341 goto err;
343 qs->action = SUMMARY;
344 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
345 return;
346 gotweb_render_page(c->tp, gotweb_render_summary);
347 return;
348 case TAG:
349 error = got_get_repo_tags(c, 1);
350 if (error) {
351 log_warnx("%s: %s", __func__, error->msg);
352 goto err;
354 if (c->t->tag_count == 0) {
355 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
356 "bad commit id");
357 goto err;
359 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
360 return;
361 gotweb_render_page(c->tp, gotweb_render_tag);
362 return;
363 case TAGS:
364 error = got_get_repo_tags(c, srv->max_commits_display);
365 if (error) {
366 log_warnx("%s: %s", __func__, error->msg);
367 goto err;
369 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
370 return;
371 gotweb_render_page(c->tp, gotweb_render_tags);
372 return;
373 case TREE:
374 error = got_get_repo_commits(c, 1);
375 if (error) {
376 log_warnx("%s: %s", __func__, error->msg);
377 goto err;
379 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
380 return;
381 gotweb_render_page(c->tp, gotweb_render_tree);
382 return;
383 case ERR:
384 default:
385 error = got_error(GOT_ERR_BAD_QUERYSTRING);
388 err:
389 c->t->error = error;
390 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
391 return;
392 gotweb_render_page(c->tp, gotweb_render_error);
395 struct server *
396 gotweb_get_server(const char *server_name)
398 struct server *srv;
400 /* check against the server name first */
401 if (*server_name != '\0')
402 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
403 if (strcmp(srv->name, server_name) == 0)
404 return srv;
406 /* otherwise, use the first server */
407 return TAILQ_FIRST(&gotwebd_env->servers);
408 };
410 const struct got_error *
411 gotweb_init_transport(struct transport **t)
413 const struct got_error *error = NULL;
415 *t = calloc(1, sizeof(**t));
416 if (*t == NULL)
417 return got_error_from_errno2(__func__, "calloc");
419 TAILQ_INIT(&(*t)->repo_commits);
420 TAILQ_INIT(&(*t)->repo_tags);
421 TAILQ_INIT(&(*t)->refs);
423 (*t)->fd = -1;
425 return error;
428 static const struct got_error *
429 gotweb_init_querystring(struct querystring **qs)
431 const struct got_error *error = NULL;
433 *qs = calloc(1, sizeof(**qs));
434 if (*qs == NULL)
435 return got_error_from_errno2(__func__, "calloc");
437 (*qs)->headref = strdup("HEAD");
438 if ((*qs)->headref == NULL) {
439 free(*qs);
440 *qs = NULL;
441 return got_error_from_errno2(__func__, "strdup");
444 (*qs)->action = INDEX;
445 (*qs)->commit = NULL;
446 (*qs)->file = NULL;
447 (*qs)->folder = NULL;
448 (*qs)->index_page = 0;
449 (*qs)->path = NULL;
451 return error;
454 static const struct got_error *
455 gotweb_parse_querystring(struct querystring **qs, char *qst)
457 const struct got_error *error = NULL;
458 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
459 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
461 if (qst == NULL)
462 return error;
464 tok1 = strdup(qst);
465 if (tok1 == NULL)
466 return got_error_from_errno2(__func__, "strdup");
468 tok1_pair = tok1;
469 tok1_end = tok1;
471 while (tok1_pair != NULL) {
472 strsep(&tok1_end, "&");
474 tok2 = strdup(tok1_pair);
475 if (tok2 == NULL) {
476 free(tok1);
477 return got_error_from_errno2(__func__, "strdup");
480 tok2_pair = tok2;
481 tok2_end = tok2;
483 while (tok2_pair != NULL) {
484 strsep(&tok2_end, "=");
485 if (tok2_end) {
486 error = gotweb_assign_querystring(qs, tok2_pair,
487 tok2_end);
488 if (error)
489 goto err;
491 tok2_pair = tok2_end;
493 free(tok2);
494 tok1_pair = tok1_end;
496 free(tok1);
497 return error;
498 err:
499 free(tok2);
500 free(tok1);
501 return error;
504 /*
505 * Adapted from usr.sbin/httpd/httpd.c url_decode.
506 */
507 static const struct got_error *
508 gotweb_urldecode(char *url)
510 char *p, *q;
511 char hex[3];
512 unsigned long x;
514 hex[2] = '\0';
515 p = q = url;
517 while (*p != '\0') {
518 switch (*p) {
519 case '%':
520 /* Encoding character is followed by two hex chars */
521 if (!isxdigit((unsigned char)p[1]) ||
522 !isxdigit((unsigned char)p[2]) ||
523 (p[1] == '0' && p[2] == '0'))
524 return got_error(GOT_ERR_BAD_QUERYSTRING);
526 hex[0] = p[1];
527 hex[1] = p[2];
529 /*
530 * We don't have to validate "hex" because it is
531 * guaranteed to include two hex chars followed by nul.
532 */
533 x = strtoul(hex, NULL, 16);
534 *q = (char)x;
535 p += 2;
536 break;
537 default:
538 *q = *p;
539 break;
541 p++;
542 q++;
544 *q = '\0';
546 return NULL;
549 static const struct got_error *
550 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
552 const struct got_error *error = NULL;
553 const char *errstr;
554 int a_cnt, el_cnt;
556 error = gotweb_urldecode(value);
557 if (error)
558 return error;
560 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
561 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
562 continue;
564 switch (querystring_keys[el_cnt].element) {
565 case ACTION:
566 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
567 if (strcmp(value, action_keys[a_cnt].name) != 0)
568 continue;
569 else if (strcmp(value,
570 action_keys[a_cnt].name) == 0){
571 (*qs)->action =
572 action_keys[a_cnt].action;
573 goto qa_found;
576 (*qs)->action = ERR;
577 qa_found:
578 break;
579 case COMMIT:
580 (*qs)->commit = strdup(value);
581 if ((*qs)->commit == NULL) {
582 error = got_error_from_errno2(__func__,
583 "strdup");
584 goto done;
586 break;
587 case RFILE:
588 (*qs)->file = strdup(value);
589 if ((*qs)->file == NULL) {
590 error = got_error_from_errno2(__func__,
591 "strdup");
592 goto done;
594 break;
595 case FOLDER:
596 (*qs)->folder = strdup(value);
597 if ((*qs)->folder == NULL) {
598 error = got_error_from_errno2(__func__,
599 "strdup");
600 goto done;
602 break;
603 case HEADREF:
604 free((*qs)->headref);
605 (*qs)->headref = strdup(value);
606 if ((*qs)->headref == NULL) {
607 error = got_error_from_errno2(__func__,
608 "strdup");
609 goto done;
611 break;
612 case INDEX_PAGE:
613 if (*value == '\0')
614 break;
615 (*qs)->index_page = strtonum(value, INT64_MIN,
616 INT64_MAX, &errstr);
617 if (errstr) {
618 error = got_error_from_errno3(__func__,
619 "strtonum", errstr);
620 goto done;
622 if ((*qs)->index_page < 0)
623 (*qs)->index_page = 0;
624 break;
625 case PATH:
626 (*qs)->path = strdup(value);
627 if ((*qs)->path == NULL) {
628 error = got_error_from_errno2(__func__,
629 "strdup");
630 goto done;
632 break;
633 case PAGE:
634 if (*value == '\0')
635 break;
636 (*qs)->page = strtonum(value, INT64_MIN,
637 INT64_MAX, &errstr);
638 if (errstr) {
639 error = got_error_from_errno3(__func__,
640 "strtonum", errstr);
641 goto done;
643 if ((*qs)->page < 0)
644 (*qs)->page = 0;
645 break;
646 default:
647 break;
650 done:
651 return error;
654 void
655 gotweb_free_repo_tag(struct repo_tag *rt)
657 if (rt != NULL) {
658 free(rt->commit_id);
659 free(rt->tag_name);
660 free(rt->tag_commit);
661 free(rt->commit_msg);
662 free(rt->tagger);
664 free(rt);
667 void
668 gotweb_free_repo_commit(struct repo_commit *rc)
670 if (rc != NULL) {
671 free(rc->path);
672 free(rc->refs_str);
673 free(rc->commit_id);
674 free(rc->parent_id);
675 free(rc->tree_id);
676 free(rc->author);
677 free(rc->committer);
678 free(rc->commit_msg);
680 free(rc);
683 static void
684 gotweb_free_querystring(struct querystring *qs)
686 if (qs != NULL) {
687 free(qs->commit);
688 free(qs->file);
689 free(qs->folder);
690 free(qs->headref);
691 free(qs->path);
693 free(qs);
696 static void
697 gotweb_free_repo_dir(struct repo_dir *repo_dir)
699 if (repo_dir != NULL) {
700 free(repo_dir->name);
701 free(repo_dir->owner);
702 free(repo_dir->description);
703 free(repo_dir->url);
704 free(repo_dir->path);
706 free(repo_dir);
709 void
710 gotweb_free_transport(struct transport *t)
712 const struct got_error *err;
713 struct repo_commit *rc = NULL, *trc = NULL;
714 struct repo_tag *rt = NULL, *trt = NULL;
715 int i;
717 got_ref_list_free(&t->refs);
718 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
719 TAILQ_REMOVE(&t->repo_commits, rc, entry);
720 gotweb_free_repo_commit(rc);
722 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
723 TAILQ_REMOVE(&t->repo_tags, rt, entry);
724 gotweb_free_repo_tag(rt);
726 gotweb_free_repo_dir(t->repo_dir);
727 gotweb_free_querystring(t->qs);
728 free(t->more_id);
729 free(t->next_id);
730 free(t->prev_id);
731 if (t->blob)
732 got_object_blob_close(t->blob);
733 if (t->fp) {
734 err = got_gotweb_closefile(t->fp);
735 if (err)
736 log_warnx("%s: got_gotweb_closefile failure: %s",
737 __func__, err->msg);
739 if (t->fd != -1 && close(t->fd) == -1)
740 log_warn("%s: close", __func__);
741 if (t->repos) {
742 for (i = 0; i < t->nrepos; ++i)
743 free(t->repos[i]);
744 free(t->repos);
746 free(t);
749 void
750 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
751 struct gotweb_url *next, int *have_next)
753 struct transport *t = c->t;
754 struct querystring *qs = t->qs;
755 struct server *srv = c->srv;
757 *have_prev = *have_next = 0;
759 switch(qs->action) {
760 case INDEX:
761 if (qs->index_page > 0) {
762 *have_prev = 1;
763 *prev = (struct gotweb_url){
764 .action = -1,
765 .index_page = qs->index_page - 1,
766 .page = -1,
767 };
769 if (t->next_disp == srv->max_repos_display &&
770 t->repos_total != (qs->index_page + 1) *
771 srv->max_repos_display) {
772 *have_next = 1;
773 *next = (struct gotweb_url){
774 .action = -1,
775 .index_page = qs->index_page + 1,
776 .page = -1,
777 };
779 break;
780 case TAGS:
781 if (t->prev_id && qs->commit != NULL &&
782 strcmp(qs->commit, t->prev_id) != 0) {
783 *have_prev = 1;
784 *prev = (struct gotweb_url){
785 .action = TAGS,
786 .index_page = -1,
787 .page = qs->page - 1,
788 .path = qs->path,
789 .commit = t->prev_id,
790 .headref = qs->headref,
791 };
793 if (t->next_id) {
794 *have_next = 1;
795 *next = (struct gotweb_url){
796 .action = TAGS,
797 .index_page = -1,
798 .page = qs->page + 1,
799 .path = qs->path,
800 .commit = t->next_id,
801 .headref = qs->headref,
802 };
804 break;
808 static int
809 gotweb_render_index(struct template *tp)
811 const struct got_error *error = NULL;
812 struct request *c = tp->tp_arg;
813 struct server *srv = c->srv;
814 struct transport *t = c->t;
815 struct querystring *qs = t->qs;
816 struct repo_dir *repo_dir = NULL;
817 struct dirent **sd_dent = t->repos;
818 unsigned int d_i, d_disp = 0;
819 unsigned int d_skipped = 0;
820 int type, r;
822 if (gotweb_render_repo_table_hdr(c->tp) == -1)
823 return -1;
825 for (d_i = 0; d_i < t->nrepos; d_i++) {
826 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
827 break;
829 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
830 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
831 d_skipped++;
832 continue;
835 error = got_path_dirent_type(&type, srv->repos_path,
836 sd_dent[d_i]);
837 if (error)
838 continue;
839 if (type != DT_DIR) {
840 d_skipped++;
841 continue;
844 if (qs->index_page > 0 && (qs->index_page *
845 srv->max_repos_display) > t->prev_disp) {
846 t->prev_disp++;
847 continue;
850 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
851 if (error)
852 continue;
854 error = gotweb_load_got_path(c, repo_dir);
855 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
856 if (error->code != GOT_ERR_NOT_GIT_REPO)
857 log_warnx("%s: %s: %s", __func__,
858 sd_dent[d_i]->d_name, error->msg);
859 gotweb_free_repo_dir(repo_dir);
860 repo_dir = NULL;
861 d_skipped++;
862 continue;
865 d_disp++;
866 t->prev_disp++;
868 r = gotweb_render_repo_fragment(c->tp, repo_dir);
869 gotweb_free_repo_dir(repo_dir);
870 if (r == -1)
871 return -1;
873 t->next_disp++;
874 if (d_disp == srv->max_repos_display)
875 break;
877 t->repos_total = t->nrepos - d_skipped;
879 if (srv->max_repos_display == 0)
880 return 0;
881 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
882 return 0;
883 if (t->repos_total <= srv->max_repos ||
884 t->repos_total <= srv->max_repos_display)
885 return 0;
887 if (gotweb_render_navs(c->tp) == -1)
888 return -1;
890 return 0;
893 static inline int
894 should_urlencode(int c)
896 if (c <= ' ' || c >= 127)
897 return 1;
899 switch (c) {
900 /* gen-delim */
901 case ':':
902 case '/':
903 case '?':
904 case '#':
905 case '[':
906 case ']':
907 case '@':
908 /* sub-delims */
909 case '!':
910 case '$':
911 case '&':
912 case '\'':
913 case '(':
914 case ')':
915 case '*':
916 case '+':
917 case ',':
918 case ';':
919 case '=':
920 /* needed because the URLs are embedded into the HTML */
921 case '\"':
922 return 1;
923 default:
924 return 0;
928 static char *
929 gotweb_urlencode(const char *str)
931 const char *s;
932 char *escaped;
933 size_t i, len;
934 int a, b;
936 len = 0;
937 for (s = str; *s; ++s) {
938 len++;
939 if (should_urlencode(*s))
940 len += 2;
943 escaped = calloc(1, len + 1);
944 if (escaped == NULL)
945 return NULL;
947 i = 0;
948 for (s = str; *s; ++s) {
949 if (should_urlencode(*s)) {
950 a = (*s & 0xF0) >> 4;
951 b = (*s & 0x0F);
953 escaped[i++] = '%';
954 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
955 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
956 } else
957 escaped[i++] = *s;
960 return escaped;
963 const char *
964 gotweb_action_name(int action)
966 switch (action) {
967 case BLAME:
968 return "blame";
969 case BLOB:
970 return "blob";
971 case BLOBRAW:
972 return "blobraw";
973 case BRIEFS:
974 return "briefs";
975 case COMMITS:
976 return "commits";
977 case DIFF:
978 return "diff";
979 case ERR:
980 return "err";
981 case INDEX:
982 return "index";
983 case SUMMARY:
984 return "summary";
985 case TAG:
986 return "tag";
987 case TAGS:
988 return "tags";
989 case TREE:
990 return "tree";
991 case RSS:
992 return "rss";
993 default:
994 return NULL;
998 int
999 gotweb_render_url(struct request *c, struct gotweb_url *url)
1001 const char *sep = "?", *action;
1002 char *tmp;
1003 int r;
1005 action = gotweb_action_name(url->action);
1006 if (action != NULL) {
1007 if (fcgi_printf(c, "?action=%s", action) == -1)
1008 return -1;
1009 sep = "&";
1012 if (url->commit) {
1013 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1014 return -1;
1015 sep = "&";
1018 if (url->previd) {
1019 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1020 return -1;
1021 sep = "&";
1024 if (url->prevset) {
1025 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1026 return -1;
1027 sep = "&";
1030 if (url->file) {
1031 tmp = gotweb_urlencode(url->file);
1032 if (tmp == NULL)
1033 return -1;
1034 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1035 free(tmp);
1036 if (r == -1)
1037 return -1;
1038 sep = "&";
1041 if (url->folder) {
1042 tmp = gotweb_urlencode(url->folder);
1043 if (tmp == NULL)
1044 return -1;
1045 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1046 free(tmp);
1047 if (r == -1)
1048 return -1;
1049 sep = "&";
1052 if (url->headref) {
1053 tmp = gotweb_urlencode(url->headref);
1054 if (tmp == NULL)
1055 return -1;
1056 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1057 free(tmp);
1058 if (r == -1)
1059 return -1;
1060 sep = "&";
1063 if (url->index_page != -1) {
1064 if (fcgi_printf(c, "%sindex_page=%d", sep,
1065 url->index_page) == -1)
1066 return -1;
1067 sep = "&";
1070 if (url->path) {
1071 tmp = gotweb_urlencode(url->path);
1072 if (tmp == NULL)
1073 return -1;
1074 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1075 free(tmp);
1076 if (r == -1)
1077 return -1;
1078 sep = "&";
1081 if (url->page != -1) {
1082 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1083 return -1;
1084 sep = "&";
1087 return 0;
1090 int
1091 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1093 struct template *tp = c->tp;
1094 const char *proto = c->https ? "https" : "http";
1096 if (fcgi_puts(tp, proto) == -1 ||
1097 fcgi_puts(tp, "://") == -1 ||
1098 tp_htmlescape(tp, c->server_name) == -1 ||
1099 tp_htmlescape(tp, c->document_uri) == -1)
1100 return -1;
1102 return gotweb_render_url(c, url);
1105 static struct got_repository *
1106 find_cached_repo(struct server *srv, const char *path)
1108 int i;
1110 for (i = 0; i < srv->ncached_repos; i++) {
1111 if (strcmp(srv->cached_repos[i].path, path) == 0)
1112 return srv->cached_repos[i].repo;
1115 return NULL;
1118 static const struct got_error *
1119 cache_repo(struct got_repository **new, struct server *srv,
1120 struct repo_dir *repo_dir, struct socket *sock)
1122 const struct got_error *error = NULL;
1123 struct got_repository *repo;
1124 struct cached_repo *cr;
1125 int evicted = 0;
1127 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1128 cr = &srv->cached_repos[srv->ncached_repos - 1];
1129 error = got_repo_close(cr->repo);
1130 memset(cr, 0, sizeof(*cr));
1131 srv->ncached_repos--;
1132 if (error)
1133 return error;
1134 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1135 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1136 cr = &srv->cached_repos[0];
1137 evicted = 1;
1138 } else {
1139 cr = &srv->cached_repos[srv->ncached_repos];
1142 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1143 if (error) {
1144 if (evicted) {
1145 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1146 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1148 return error;
1151 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1152 >= sizeof(cr->path)) {
1153 if (evicted) {
1154 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1155 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1157 return got_error(GOT_ERR_NO_SPACE);
1160 cr->repo = repo;
1161 srv->ncached_repos++;
1162 *new = repo;
1163 return NULL;
1166 static const struct got_error *
1167 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1169 const struct got_error *error = NULL;
1170 struct socket *sock = c->sock;
1171 struct server *srv = c->srv;
1172 struct transport *t = c->t;
1173 struct got_repository *repo = NULL;
1174 DIR *dt;
1175 char *dir_test;
1177 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1178 GOTWEB_GIT_DIR) == -1)
1179 return got_error_from_errno("asprintf");
1181 dt = opendir(dir_test);
1182 if (dt == NULL) {
1183 free(dir_test);
1184 } else {
1185 repo_dir->path = dir_test;
1186 dir_test = NULL;
1187 goto done;
1190 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1191 repo_dir->name) == -1)
1192 return got_error_from_errno("asprintf");
1194 dt = opendir(dir_test);
1195 if (dt == NULL) {
1196 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1197 goto err;
1198 } else {
1199 repo_dir->path = dir_test;
1200 dir_test = NULL;
1203 done:
1204 if (srv->respect_exportok &&
1205 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1206 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1207 goto err;
1210 repo = find_cached_repo(srv, repo_dir->path);
1211 if (repo == NULL) {
1212 error = cache_repo(&repo, srv, repo_dir, sock);
1213 if (error)
1214 goto err;
1216 t->repo = repo;
1217 error = gotweb_get_repo_description(&repo_dir->description, srv,
1218 repo_dir->path, dirfd(dt));
1219 if (error)
1220 goto err;
1221 error = got_get_repo_owner(&repo_dir->owner, c);
1222 if (error)
1223 goto err;
1224 error = got_get_repo_age(&repo_dir->age, c, NULL);
1225 if (error)
1226 goto err;
1227 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1228 dirfd(dt));
1229 err:
1230 free(dir_test);
1231 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1232 error = got_error_from_errno("closedir");
1233 return error;
1236 static const struct got_error *
1237 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1239 const struct got_error *error;
1241 *repo_dir = calloc(1, sizeof(**repo_dir));
1242 if (*repo_dir == NULL)
1243 return got_error_from_errno("calloc");
1245 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1246 error = got_error_from_errno("asprintf");
1247 free(*repo_dir);
1248 *repo_dir = NULL;
1249 return error;
1251 (*repo_dir)->owner = NULL;
1252 (*repo_dir)->description = NULL;
1253 (*repo_dir)->url = NULL;
1254 (*repo_dir)->path = NULL;
1256 return NULL;
1259 static const struct got_error *
1260 gotweb_get_repo_description(char **description, struct server *srv,
1261 const char *dirpath, int dir)
1263 const struct got_error *error = NULL;
1264 struct stat sb;
1265 int fd = -1;
1266 off_t len;
1268 *description = NULL;
1269 if (srv->show_repo_description == 0)
1270 return NULL;
1272 fd = openat(dir, "description", O_RDONLY);
1273 if (fd == -1) {
1274 if (errno != ENOENT && errno != EACCES) {
1275 error = got_error_from_errno_fmt("openat %s/%s",
1276 dirpath, "description");
1278 goto done;
1281 if (fstat(fd, &sb) == -1) {
1282 error = got_error_from_errno_fmt("fstat %s/%s",
1283 dirpath, "description");
1284 goto done;
1287 len = sb.st_size;
1288 if (len > GOTWEBD_MAXDESCRSZ - 1)
1289 len = GOTWEBD_MAXDESCRSZ - 1;
1291 *description = calloc(len + 1, sizeof(**description));
1292 if (*description == NULL) {
1293 error = got_error_from_errno("calloc");
1294 goto done;
1297 if (read(fd, *description, len) == -1)
1298 error = got_error_from_errno("read");
1299 done:
1300 if (fd != -1 && close(fd) == -1 && error == NULL)
1301 error = got_error_from_errno("close");
1302 return error;
1305 static const struct got_error *
1306 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1307 int dir)
1309 const struct got_error *error = NULL;
1310 struct stat sb;
1311 int fd = -1;
1312 off_t len;
1314 *url = NULL;
1315 if (srv->show_repo_cloneurl == 0)
1316 return NULL;
1318 fd = openat(dir, "cloneurl", O_RDONLY);
1319 if (fd == -1) {
1320 if (errno != ENOENT && errno != EACCES) {
1321 error = got_error_from_errno_fmt("openat %s/%s",
1322 dirpath, "cloneurl");
1324 goto done;
1327 if (fstat(fd, &sb) == -1) {
1328 error = got_error_from_errno_fmt("fstat %s/%s",
1329 dirpath, "cloneurl");
1330 goto done;
1333 len = sb.st_size;
1334 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1335 len = GOTWEBD_MAXCLONEURLSZ - 1;
1337 *url = calloc(len + 1, sizeof(**url));
1338 if (*url == NULL) {
1339 error = got_error_from_errno("calloc");
1340 goto done;
1343 if (read(fd, *url, len) == -1)
1344 error = got_error_from_errno("read");
1345 done:
1346 if (fd != -1 && close(fd) == -1 && error == NULL)
1347 error = got_error_from_errno("close");
1348 return error;
1351 int
1352 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1354 struct request *c = tp->tp_arg;
1355 struct tm tm;
1356 long long diff_time;
1357 const char *years = "years ago", *months = "months ago";
1358 const char *weeks = "weeks ago", *days = "days ago";
1359 const char *hours = "hours ago", *minutes = "minutes ago";
1360 const char *seconds = "seconds ago", *now = "right now";
1361 char *s;
1362 char datebuf[64];
1363 size_t r;
1365 switch (ref_tm) {
1366 case TM_DIFF:
1367 diff_time = time(NULL) - committer_time;
1368 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1369 if (fcgi_printf(c, "%lld %s",
1370 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1371 return -1;
1372 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1373 if (fcgi_printf(c, "%lld %s",
1374 (diff_time / 60 / 60 / 24 / (365 / 12)),
1375 months) == -1)
1376 return -1;
1377 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1378 if (fcgi_printf(c, "%lld %s",
1379 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1380 return -1;
1381 } else if (diff_time > 60 * 60 * 24 * 2) {
1382 if (fcgi_printf(c, "%lld %s",
1383 (diff_time / 60 / 60 / 24), days) == -1)
1384 return -1;
1385 } else if (diff_time > 60 * 60 * 2) {
1386 if (fcgi_printf(c, "%lld %s",
1387 (diff_time / 60 / 60), hours) == -1)
1388 return -1;
1389 } else if (diff_time > 60 * 2) {
1390 if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1391 minutes) == -1)
1392 return -1;
1393 } else if (diff_time > 2) {
1394 if (fcgi_printf(c, "%lld %s", diff_time,
1395 seconds) == -1)
1396 return -1;
1397 } else {
1398 if (fcgi_puts(tp, now) == -1)
1399 return -1;
1401 break;
1402 case TM_LONG:
1403 if (gmtime_r(&committer_time, &tm) == NULL)
1404 return -1;
1406 s = asctime_r(&tm, datebuf);
1407 if (s == NULL)
1408 return -1;
1410 if (fcgi_puts(tp, datebuf) == -1 ||
1411 fcgi_puts(tp, " UTC") == -1)
1412 return -1;
1413 break;
1414 case TM_RFC822:
1415 if (gmtime_r(&committer_time, &tm) == NULL)
1416 return -1;
1418 r = strftime(datebuf, sizeof(datebuf),
1419 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1420 if (r == 0)
1421 return -1;
1423 if (fcgi_puts(tp, datebuf) == -1)
1424 return -1;
1425 break;
1427 return 0;