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 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
110 return -1;
112 if (location) {
113 if (tp_writes(c->tp, "Location: ") == -1 ||
114 gotweb_render_url(c, location) == -1 ||
115 tp_writes(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 (tp_writes(c->tp, csp) == -1)
122 return -1;
124 if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
125 return -1;
127 return tp_writes(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 = tp_writef(c->tp, "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;
258 if (template_flush(c->tp) == -1)
259 return;
261 for (;;) {
262 error = got_object_blob_read_block(&len, c->t->blob);
263 if (error)
264 break;
265 if (len == 0)
266 break;
267 buf = got_object_blob_get_read_buf(c->t->blob);
268 if (fcgi_write(c, buf, len) == -1)
269 break;
271 return;
272 case BRIEFS:
273 error = got_get_repo_commits(c, srv->max_commits_display);
274 if (error)
275 goto err;
276 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
277 return;
278 gotweb_render_page(c->tp, gotweb_render_briefs);
279 return;
280 case COMMITS:
281 error = got_get_repo_commits(c, srv->max_commits_display);
282 if (error) {
283 log_warnx("%s: %s", __func__, error->msg);
284 goto err;
286 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
287 return;
288 gotweb_render_page(c->tp, gotweb_render_commits);
289 return;
290 case DIFF:
291 error = got_get_repo_commits(c, 1);
292 if (error) {
293 log_warnx("%s: %s", __func__, error->msg);
294 goto err;
296 error = got_open_diff_for_output(&c->t->fp, c);
297 if (error) {
298 log_warnx("%s: %s", __func__, error->msg);
299 goto err;
301 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
302 return;
303 gotweb_render_page(c->tp, gotweb_render_diff);
304 return;
305 case INDEX:
306 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
307 alphasort);
308 if (c->t->nrepos == -1) {
309 c->t->repos = NULL;
310 error = got_error_from_errno2("scandir",
311 srv->repos_path);
312 goto err;
314 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
315 return;
316 gotweb_render_page(c->tp, gotweb_render_index);
317 return;
318 case RSS:
319 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
320 if (error)
321 goto err;
322 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
323 == -1)
324 return;
325 gotweb_render_rss(c->tp);
326 return;
327 case SUMMARY:
328 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
329 got_ref_cmp_by_name, NULL);
330 if (error) {
331 log_warnx("%s: got_ref_list: %s", __func__,
332 error->msg);
333 goto err;
335 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
336 if (error)
337 goto err;
338 qs->action = TAGS;
339 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
340 if (error) {
341 log_warnx("%s: got_get_repo_tags: %s", __func__,
342 error->msg);
343 goto err;
345 qs->action = SUMMARY;
346 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
347 return;
348 gotweb_render_page(c->tp, gotweb_render_summary);
349 return;
350 case TAG:
351 error = got_get_repo_tags(c, 1);
352 if (error) {
353 log_warnx("%s: %s", __func__, error->msg);
354 goto err;
356 if (c->t->tag_count == 0) {
357 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
358 "bad commit id");
359 goto err;
361 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
362 return;
363 gotweb_render_page(c->tp, gotweb_render_tag);
364 return;
365 case TAGS:
366 error = got_get_repo_tags(c, srv->max_commits_display);
367 if (error) {
368 log_warnx("%s: %s", __func__, error->msg);
369 goto err;
371 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
372 return;
373 gotweb_render_page(c->tp, gotweb_render_tags);
374 return;
375 case TREE:
376 error = got_get_repo_commits(c, 1);
377 if (error) {
378 log_warnx("%s: %s", __func__, error->msg);
379 goto err;
381 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
382 return;
383 gotweb_render_page(c->tp, gotweb_render_tree);
384 return;
385 case ERR:
386 default:
387 error = got_error(GOT_ERR_BAD_QUERYSTRING);
390 err:
391 c->t->error = error;
392 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
393 return;
394 gotweb_render_page(c->tp, gotweb_render_error);
397 struct server *
398 gotweb_get_server(const char *server_name)
400 struct server *srv;
402 /* check against the server name first */
403 if (*server_name != '\0')
404 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
405 if (strcmp(srv->name, server_name) == 0)
406 return srv;
408 /* otherwise, use the first server */
409 return TAILQ_FIRST(&gotwebd_env->servers);
410 };
412 const struct got_error *
413 gotweb_init_transport(struct transport **t)
415 const struct got_error *error = NULL;
417 *t = calloc(1, sizeof(**t));
418 if (*t == NULL)
419 return got_error_from_errno2(__func__, "calloc");
421 TAILQ_INIT(&(*t)->repo_commits);
422 TAILQ_INIT(&(*t)->repo_tags);
423 TAILQ_INIT(&(*t)->refs);
425 (*t)->fd = -1;
427 return error;
430 static const struct got_error *
431 gotweb_init_querystring(struct querystring **qs)
433 const struct got_error *error = NULL;
435 *qs = calloc(1, sizeof(**qs));
436 if (*qs == NULL)
437 return got_error_from_errno2(__func__, "calloc");
439 (*qs)->headref = strdup("HEAD");
440 if ((*qs)->headref == NULL) {
441 free(*qs);
442 *qs = NULL;
443 return got_error_from_errno2(__func__, "strdup");
446 (*qs)->action = INDEX;
447 (*qs)->commit = NULL;
448 (*qs)->file = NULL;
449 (*qs)->folder = NULL;
450 (*qs)->index_page = 0;
451 (*qs)->path = NULL;
453 return error;
456 static const struct got_error *
457 gotweb_parse_querystring(struct querystring **qs, char *qst)
459 const struct got_error *error = NULL;
460 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
461 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
463 if (qst == NULL)
464 return error;
466 tok1 = strdup(qst);
467 if (tok1 == NULL)
468 return got_error_from_errno2(__func__, "strdup");
470 tok1_pair = tok1;
471 tok1_end = tok1;
473 while (tok1_pair != NULL) {
474 strsep(&tok1_end, "&");
476 tok2 = strdup(tok1_pair);
477 if (tok2 == NULL) {
478 free(tok1);
479 return got_error_from_errno2(__func__, "strdup");
482 tok2_pair = tok2;
483 tok2_end = tok2;
485 while (tok2_pair != NULL) {
486 strsep(&tok2_end, "=");
487 if (tok2_end) {
488 error = gotweb_assign_querystring(qs, tok2_pair,
489 tok2_end);
490 if (error)
491 goto err;
493 tok2_pair = tok2_end;
495 free(tok2);
496 tok1_pair = tok1_end;
498 free(tok1);
499 return error;
500 err:
501 free(tok2);
502 free(tok1);
503 return error;
506 /*
507 * Adapted from usr.sbin/httpd/httpd.c url_decode.
508 */
509 static const struct got_error *
510 gotweb_urldecode(char *url)
512 char *p, *q;
513 char hex[3];
514 unsigned long x;
516 hex[2] = '\0';
517 p = q = url;
519 while (*p != '\0') {
520 switch (*p) {
521 case '%':
522 /* Encoding character is followed by two hex chars */
523 if (!isxdigit((unsigned char)p[1]) ||
524 !isxdigit((unsigned char)p[2]) ||
525 (p[1] == '0' && p[2] == '0'))
526 return got_error(GOT_ERR_BAD_QUERYSTRING);
528 hex[0] = p[1];
529 hex[1] = p[2];
531 /*
532 * We don't have to validate "hex" because it is
533 * guaranteed to include two hex chars followed by nul.
534 */
535 x = strtoul(hex, NULL, 16);
536 *q = (char)x;
537 p += 2;
538 break;
539 default:
540 *q = *p;
541 break;
543 p++;
544 q++;
546 *q = '\0';
548 return NULL;
551 static const struct got_error *
552 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
554 const struct got_error *error = NULL;
555 const char *errstr;
556 int a_cnt, el_cnt;
558 error = gotweb_urldecode(value);
559 if (error)
560 return error;
562 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
563 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
564 continue;
566 switch (querystring_keys[el_cnt].element) {
567 case ACTION:
568 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
569 if (strcmp(value, action_keys[a_cnt].name) != 0)
570 continue;
571 else if (strcmp(value,
572 action_keys[a_cnt].name) == 0){
573 (*qs)->action =
574 action_keys[a_cnt].action;
575 goto qa_found;
578 (*qs)->action = ERR;
579 qa_found:
580 break;
581 case COMMIT:
582 (*qs)->commit = strdup(value);
583 if ((*qs)->commit == NULL) {
584 error = got_error_from_errno2(__func__,
585 "strdup");
586 goto done;
588 break;
589 case RFILE:
590 (*qs)->file = strdup(value);
591 if ((*qs)->file == NULL) {
592 error = got_error_from_errno2(__func__,
593 "strdup");
594 goto done;
596 break;
597 case FOLDER:
598 (*qs)->folder = strdup(value);
599 if ((*qs)->folder == NULL) {
600 error = got_error_from_errno2(__func__,
601 "strdup");
602 goto done;
604 break;
605 case HEADREF:
606 free((*qs)->headref);
607 (*qs)->headref = strdup(value);
608 if ((*qs)->headref == NULL) {
609 error = got_error_from_errno2(__func__,
610 "strdup");
611 goto done;
613 break;
614 case INDEX_PAGE:
615 if (*value == '\0')
616 break;
617 (*qs)->index_page = strtonum(value, INT64_MIN,
618 INT64_MAX, &errstr);
619 if (errstr) {
620 error = got_error_from_errno3(__func__,
621 "strtonum", errstr);
622 goto done;
624 if ((*qs)->index_page < 0)
625 (*qs)->index_page = 0;
626 break;
627 case PATH:
628 (*qs)->path = strdup(value);
629 if ((*qs)->path == NULL) {
630 error = got_error_from_errno2(__func__,
631 "strdup");
632 goto done;
634 break;
635 case PAGE:
636 if (*value == '\0')
637 break;
638 (*qs)->page = strtonum(value, INT64_MIN,
639 INT64_MAX, &errstr);
640 if (errstr) {
641 error = got_error_from_errno3(__func__,
642 "strtonum", errstr);
643 goto done;
645 if ((*qs)->page < 0)
646 (*qs)->page = 0;
647 break;
648 default:
649 break;
652 done:
653 return error;
656 void
657 gotweb_free_repo_tag(struct repo_tag *rt)
659 if (rt != NULL) {
660 free(rt->commit_id);
661 free(rt->tag_name);
662 free(rt->tag_commit);
663 free(rt->commit_msg);
664 free(rt->tagger);
666 free(rt);
669 void
670 gotweb_free_repo_commit(struct repo_commit *rc)
672 if (rc != NULL) {
673 free(rc->path);
674 free(rc->refs_str);
675 free(rc->commit_id);
676 free(rc->parent_id);
677 free(rc->tree_id);
678 free(rc->author);
679 free(rc->committer);
680 free(rc->commit_msg);
682 free(rc);
685 static void
686 gotweb_free_querystring(struct querystring *qs)
688 if (qs != NULL) {
689 free(qs->commit);
690 free(qs->file);
691 free(qs->folder);
692 free(qs->headref);
693 free(qs->path);
695 free(qs);
698 static void
699 gotweb_free_repo_dir(struct repo_dir *repo_dir)
701 if (repo_dir != NULL) {
702 free(repo_dir->name);
703 free(repo_dir->owner);
704 free(repo_dir->description);
705 free(repo_dir->url);
706 free(repo_dir->path);
708 free(repo_dir);
711 void
712 gotweb_free_transport(struct transport *t)
714 const struct got_error *err;
715 struct repo_commit *rc = NULL, *trc = NULL;
716 struct repo_tag *rt = NULL, *trt = NULL;
717 int i;
719 got_ref_list_free(&t->refs);
720 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
721 TAILQ_REMOVE(&t->repo_commits, rc, entry);
722 gotweb_free_repo_commit(rc);
724 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
725 TAILQ_REMOVE(&t->repo_tags, rt, entry);
726 gotweb_free_repo_tag(rt);
728 gotweb_free_repo_dir(t->repo_dir);
729 gotweb_free_querystring(t->qs);
730 free(t->more_id);
731 free(t->next_id);
732 free(t->prev_id);
733 if (t->blob)
734 got_object_blob_close(t->blob);
735 if (t->fp) {
736 err = got_gotweb_closefile(t->fp);
737 if (err)
738 log_warnx("%s: got_gotweb_closefile failure: %s",
739 __func__, err->msg);
741 if (t->fd != -1 && close(t->fd) == -1)
742 log_warn("%s: close", __func__);
743 if (t->repos) {
744 for (i = 0; i < t->nrepos; ++i)
745 free(t->repos[i]);
746 free(t->repos);
748 free(t);
751 void
752 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
753 struct gotweb_url *next, int *have_next)
755 struct transport *t = c->t;
756 struct querystring *qs = t->qs;
757 struct server *srv = c->srv;
759 *have_prev = *have_next = 0;
761 switch(qs->action) {
762 case INDEX:
763 if (qs->index_page > 0) {
764 *have_prev = 1;
765 *prev = (struct gotweb_url){
766 .action = -1,
767 .index_page = qs->index_page - 1,
768 .page = -1,
769 };
771 if (t->next_disp == srv->max_repos_display &&
772 t->repos_total != (qs->index_page + 1) *
773 srv->max_repos_display) {
774 *have_next = 1;
775 *next = (struct gotweb_url){
776 .action = -1,
777 .index_page = qs->index_page + 1,
778 .page = -1,
779 };
781 break;
782 case TAGS:
783 if (t->prev_id && qs->commit != NULL &&
784 strcmp(qs->commit, t->prev_id) != 0) {
785 *have_prev = 1;
786 *prev = (struct gotweb_url){
787 .action = TAGS,
788 .index_page = -1,
789 .page = qs->page - 1,
790 .path = qs->path,
791 .commit = t->prev_id,
792 .headref = qs->headref,
793 };
795 if (t->next_id) {
796 *have_next = 1;
797 *next = (struct gotweb_url){
798 .action = TAGS,
799 .index_page = -1,
800 .page = qs->page + 1,
801 .path = qs->path,
802 .commit = t->next_id,
803 .headref = qs->headref,
804 };
806 break;
810 static int
811 gotweb_render_index(struct template *tp)
813 const struct got_error *error = NULL;
814 struct request *c = tp->tp_arg;
815 struct server *srv = c->srv;
816 struct transport *t = c->t;
817 struct querystring *qs = t->qs;
818 struct repo_dir *repo_dir = NULL;
819 struct dirent **sd_dent = t->repos;
820 unsigned int d_i, d_disp = 0;
821 unsigned int d_skipped = 0;
822 int type, r;
824 if (gotweb_render_repo_table_hdr(c->tp) == -1)
825 return -1;
827 for (d_i = 0; d_i < t->nrepos; d_i++) {
828 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
829 break;
831 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
832 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
833 d_skipped++;
834 continue;
837 error = got_path_dirent_type(&type, srv->repos_path,
838 sd_dent[d_i]);
839 if (error)
840 continue;
841 if (type != DT_DIR) {
842 d_skipped++;
843 continue;
846 if (qs->index_page > 0 && (qs->index_page *
847 srv->max_repos_display) > t->prev_disp) {
848 t->prev_disp++;
849 continue;
852 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
853 if (error)
854 continue;
856 error = gotweb_load_got_path(c, repo_dir);
857 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
858 if (error->code != GOT_ERR_NOT_GIT_REPO)
859 log_warnx("%s: %s: %s", __func__,
860 sd_dent[d_i]->d_name, error->msg);
861 gotweb_free_repo_dir(repo_dir);
862 repo_dir = NULL;
863 d_skipped++;
864 continue;
867 d_disp++;
868 t->prev_disp++;
870 r = gotweb_render_repo_fragment(c->tp, repo_dir);
871 gotweb_free_repo_dir(repo_dir);
872 if (r == -1)
873 return -1;
875 t->next_disp++;
876 if (d_disp == srv->max_repos_display)
877 break;
879 t->repos_total = t->nrepos - d_skipped;
881 if (srv->max_repos_display == 0)
882 return 0;
883 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
884 return 0;
885 if (t->repos_total <= srv->max_repos ||
886 t->repos_total <= srv->max_repos_display)
887 return 0;
889 if (gotweb_render_navs(c->tp) == -1)
890 return -1;
892 return 0;
895 static inline int
896 should_urlencode(int c)
898 if (c <= ' ' || c >= 127)
899 return 1;
901 switch (c) {
902 /* gen-delim */
903 case ':':
904 case '/':
905 case '?':
906 case '#':
907 case '[':
908 case ']':
909 case '@':
910 /* sub-delims */
911 case '!':
912 case '$':
913 case '&':
914 case '\'':
915 case '(':
916 case ')':
917 case '*':
918 case '+':
919 case ',':
920 case ';':
921 case '=':
922 /* needed because the URLs are embedded into the HTML */
923 case '\"':
924 return 1;
925 default:
926 return 0;
930 static char *
931 gotweb_urlencode(const char *str)
933 const char *s;
934 char *escaped;
935 size_t i, len;
936 int a, b;
938 len = 0;
939 for (s = str; *s; ++s) {
940 len++;
941 if (should_urlencode(*s))
942 len += 2;
945 escaped = calloc(1, len + 1);
946 if (escaped == NULL)
947 return NULL;
949 i = 0;
950 for (s = str; *s; ++s) {
951 if (should_urlencode(*s)) {
952 a = (*s & 0xF0) >> 4;
953 b = (*s & 0x0F);
955 escaped[i++] = '%';
956 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
957 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
958 } else
959 escaped[i++] = *s;
962 return escaped;
965 const char *
966 gotweb_action_name(int action)
968 switch (action) {
969 case BLAME:
970 return "blame";
971 case BLOB:
972 return "blob";
973 case BLOBRAW:
974 return "blobraw";
975 case BRIEFS:
976 return "briefs";
977 case COMMITS:
978 return "commits";
979 case DIFF:
980 return "diff";
981 case ERR:
982 return "err";
983 case INDEX:
984 return "index";
985 case SUMMARY:
986 return "summary";
987 case TAG:
988 return "tag";
989 case TAGS:
990 return "tags";
991 case TREE:
992 return "tree";
993 case RSS:
994 return "rss";
995 default:
996 return NULL;
1000 int
1001 gotweb_render_url(struct request *c, struct gotweb_url *url)
1003 const char *sep = "?", *action;
1004 char *tmp;
1005 int r;
1007 action = gotweb_action_name(url->action);
1008 if (action != NULL) {
1009 if (tp_writef(c->tp, "?action=%s", action) == -1)
1010 return -1;
1011 sep = "&";
1014 if (url->commit) {
1015 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
1016 return -1;
1017 sep = "&";
1020 if (url->previd) {
1021 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
1022 return -1;
1023 sep = "&";
1026 if (url->prevset) {
1027 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
1028 return -1;
1029 sep = "&";
1032 if (url->file) {
1033 tmp = gotweb_urlencode(url->file);
1034 if (tmp == NULL)
1035 return -1;
1036 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1037 free(tmp);
1038 if (r == -1)
1039 return -1;
1040 sep = "&";
1043 if (url->folder) {
1044 tmp = gotweb_urlencode(url->folder);
1045 if (tmp == NULL)
1046 return -1;
1047 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1048 free(tmp);
1049 if (r == -1)
1050 return -1;
1051 sep = "&";
1054 if (url->headref) {
1055 tmp = gotweb_urlencode(url->headref);
1056 if (tmp == NULL)
1057 return -1;
1058 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1059 free(tmp);
1060 if (r == -1)
1061 return -1;
1062 sep = "&";
1065 if (url->index_page != -1) {
1066 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1067 url->index_page) == -1)
1068 return -1;
1069 sep = "&";
1072 if (url->path) {
1073 tmp = gotweb_urlencode(url->path);
1074 if (tmp == NULL)
1075 return -1;
1076 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1077 free(tmp);
1078 if (r == -1)
1079 return -1;
1080 sep = "&";
1083 if (url->page != -1) {
1084 if (tp_writef(c->tp, "%spage=%d", sep, url->page) == -1)
1085 return -1;
1086 sep = "&";
1089 return 0;
1092 int
1093 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1095 struct template *tp = c->tp;
1096 const char *proto = c->https ? "https" : "http";
1098 if (tp_writes(tp, proto) == -1 ||
1099 tp_writes(tp, "://") == -1 ||
1100 tp_htmlescape(tp, c->server_name) == -1 ||
1101 tp_htmlescape(tp, c->document_uri) == -1)
1102 return -1;
1104 return gotweb_render_url(c, url);
1107 static struct got_repository *
1108 find_cached_repo(struct server *srv, const char *path)
1110 int i;
1112 for (i = 0; i < srv->ncached_repos; i++) {
1113 if (strcmp(srv->cached_repos[i].path, path) == 0)
1114 return srv->cached_repos[i].repo;
1117 return NULL;
1120 static const struct got_error *
1121 cache_repo(struct got_repository **new, struct server *srv,
1122 struct repo_dir *repo_dir, struct socket *sock)
1124 const struct got_error *error = NULL;
1125 struct got_repository *repo;
1126 struct cached_repo *cr;
1127 int evicted = 0;
1129 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1130 cr = &srv->cached_repos[srv->ncached_repos - 1];
1131 error = got_repo_close(cr->repo);
1132 memset(cr, 0, sizeof(*cr));
1133 srv->ncached_repos--;
1134 if (error)
1135 return error;
1136 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1137 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1138 cr = &srv->cached_repos[0];
1139 evicted = 1;
1140 } else {
1141 cr = &srv->cached_repos[srv->ncached_repos];
1144 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1145 if (error) {
1146 if (evicted) {
1147 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1148 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1150 return error;
1153 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1154 >= sizeof(cr->path)) {
1155 if (evicted) {
1156 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1157 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1159 return got_error(GOT_ERR_NO_SPACE);
1162 cr->repo = repo;
1163 srv->ncached_repos++;
1164 *new = repo;
1165 return NULL;
1168 static const struct got_error *
1169 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1171 const struct got_error *error = NULL;
1172 struct socket *sock = c->sock;
1173 struct server *srv = c->srv;
1174 struct transport *t = c->t;
1175 struct got_repository *repo = NULL;
1176 DIR *dt;
1177 char *dir_test;
1179 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1180 GOTWEB_GIT_DIR) == -1)
1181 return got_error_from_errno("asprintf");
1183 dt = opendir(dir_test);
1184 if (dt == NULL) {
1185 free(dir_test);
1186 } else {
1187 repo_dir->path = dir_test;
1188 dir_test = NULL;
1189 goto done;
1192 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1193 repo_dir->name) == -1)
1194 return got_error_from_errno("asprintf");
1196 dt = opendir(dir_test);
1197 if (dt == NULL) {
1198 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1199 goto err;
1200 } else {
1201 repo_dir->path = dir_test;
1202 dir_test = NULL;
1205 done:
1206 if (srv->respect_exportok &&
1207 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1208 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1209 goto err;
1212 repo = find_cached_repo(srv, repo_dir->path);
1213 if (repo == NULL) {
1214 error = cache_repo(&repo, srv, repo_dir, sock);
1215 if (error)
1216 goto err;
1218 t->repo = repo;
1219 error = gotweb_get_repo_description(&repo_dir->description, srv,
1220 repo_dir->path, dirfd(dt));
1221 if (error)
1222 goto err;
1223 error = got_get_repo_owner(&repo_dir->owner, c);
1224 if (error)
1225 goto err;
1226 if (srv->show_repo_age) {
1227 error = got_get_repo_age(&repo_dir->age, c, NULL);
1228 if (error)
1229 goto err;
1231 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1232 dirfd(dt));
1233 err:
1234 free(dir_test);
1235 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1236 error = got_error_from_errno("closedir");
1237 return error;
1240 static const struct got_error *
1241 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1243 const struct got_error *error;
1245 *repo_dir = calloc(1, sizeof(**repo_dir));
1246 if (*repo_dir == NULL)
1247 return got_error_from_errno("calloc");
1249 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1250 error = got_error_from_errno("asprintf");
1251 free(*repo_dir);
1252 *repo_dir = NULL;
1253 return error;
1255 (*repo_dir)->owner = NULL;
1256 (*repo_dir)->description = NULL;
1257 (*repo_dir)->url = NULL;
1258 (*repo_dir)->path = NULL;
1260 return NULL;
1263 static const struct got_error *
1264 gotweb_get_repo_description(char **description, struct server *srv,
1265 const char *dirpath, int dir)
1267 const struct got_error *error = NULL;
1268 struct stat sb;
1269 int fd = -1;
1270 off_t len;
1272 *description = NULL;
1273 if (srv->show_repo_description == 0)
1274 return NULL;
1276 fd = openat(dir, "description", O_RDONLY);
1277 if (fd == -1) {
1278 if (errno != ENOENT && errno != EACCES) {
1279 error = got_error_from_errno_fmt("openat %s/%s",
1280 dirpath, "description");
1282 goto done;
1285 if (fstat(fd, &sb) == -1) {
1286 error = got_error_from_errno_fmt("fstat %s/%s",
1287 dirpath, "description");
1288 goto done;
1291 len = sb.st_size;
1292 if (len > GOTWEBD_MAXDESCRSZ - 1)
1293 len = GOTWEBD_MAXDESCRSZ - 1;
1295 *description = calloc(len + 1, sizeof(**description));
1296 if (*description == NULL) {
1297 error = got_error_from_errno("calloc");
1298 goto done;
1301 if (read(fd, *description, len) == -1)
1302 error = got_error_from_errno("read");
1303 done:
1304 if (fd != -1 && close(fd) == -1 && error == NULL)
1305 error = got_error_from_errno("close");
1306 return error;
1309 static const struct got_error *
1310 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1311 int dir)
1313 const struct got_error *error = NULL;
1314 struct stat sb;
1315 int fd = -1;
1316 off_t len;
1318 *url = NULL;
1319 if (srv->show_repo_cloneurl == 0)
1320 return NULL;
1322 fd = openat(dir, "cloneurl", O_RDONLY);
1323 if (fd == -1) {
1324 if (errno != ENOENT && errno != EACCES) {
1325 error = got_error_from_errno_fmt("openat %s/%s",
1326 dirpath, "cloneurl");
1328 goto done;
1331 if (fstat(fd, &sb) == -1) {
1332 error = got_error_from_errno_fmt("fstat %s/%s",
1333 dirpath, "cloneurl");
1334 goto done;
1337 len = sb.st_size;
1338 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1339 len = GOTWEBD_MAXCLONEURLSZ - 1;
1341 *url = calloc(len + 1, sizeof(**url));
1342 if (*url == NULL) {
1343 error = got_error_from_errno("calloc");
1344 goto done;
1347 if (read(fd, *url, len) == -1)
1348 error = got_error_from_errno("read");
1349 done:
1350 if (fd != -1 && close(fd) == -1 && error == NULL)
1351 error = got_error_from_errno("close");
1352 return error;
1355 int
1356 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1358 struct request *c = tp->tp_arg;
1359 struct tm tm;
1360 long long diff_time;
1361 const char *years = "years ago", *months = "months ago";
1362 const char *weeks = "weeks ago", *days = "days ago";
1363 const char *hours = "hours ago", *minutes = "minutes ago";
1364 const char *seconds = "seconds ago", *now = "right now";
1365 char *s;
1366 char datebuf[64];
1367 size_t r;
1369 switch (ref_tm) {
1370 case TM_DIFF:
1371 diff_time = time(NULL) - committer_time;
1372 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1373 if (tp_writef(c->tp, "%lld %s",
1374 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1375 return -1;
1376 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1377 if (tp_writef(c->tp, "%lld %s",
1378 (diff_time / 60 / 60 / 24 / (365 / 12)),
1379 months) == -1)
1380 return -1;
1381 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1382 if (tp_writef(c->tp, "%lld %s",
1383 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1384 return -1;
1385 } else if (diff_time > 60 * 60 * 24 * 2) {
1386 if (tp_writef(c->tp, "%lld %s",
1387 (diff_time / 60 / 60 / 24), days) == -1)
1388 return -1;
1389 } else if (diff_time > 60 * 60 * 2) {
1390 if (tp_writef(c->tp, "%lld %s",
1391 (diff_time / 60 / 60), hours) == -1)
1392 return -1;
1393 } else if (diff_time > 60 * 2) {
1394 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1395 minutes) == -1)
1396 return -1;
1397 } else if (diff_time > 2) {
1398 if (tp_writef(c->tp, "%lld %s", diff_time,
1399 seconds) == -1)
1400 return -1;
1401 } else {
1402 if (tp_writes(tp, now) == -1)
1403 return -1;
1405 break;
1406 case TM_LONG:
1407 if (gmtime_r(&committer_time, &tm) == NULL)
1408 return -1;
1410 s = asctime_r(&tm, datebuf);
1411 if (s == NULL)
1412 return -1;
1414 if (tp_writes(tp, datebuf) == -1 ||
1415 tp_writes(tp, " UTC") == -1)
1416 return -1;
1417 break;
1418 case TM_RFC822:
1419 if (gmtime_r(&committer_time, &tm) == NULL)
1420 return -1;
1422 r = strftime(datebuf, sizeof(datebuf),
1423 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1424 if (r == 0)
1425 return -1;
1427 if (tp_writes(tp, datebuf) == -1)
1428 return -1;
1429 break;
1431 return 0;