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 */
20 #include "got_compat.h"
22 #include <net/if.h>
23 #include <netinet/in.h>
24 #include <sys/queue.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <event.h>
32 #include <fcntl.h>
33 #include <imsg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "gotwebd.h"
52 #include "tmpl.h"
54 static const struct querystring_keys querystring_keys[] = {
55 { "action", ACTION },
56 { "commit", COMMIT },
57 { "file", RFILE },
58 { "folder", FOLDER },
59 { "headref", HEADREF },
60 { "index_page", INDEX_PAGE },
61 { "path", PATH },
62 { "page", PAGE },
63 };
65 static const struct action_keys action_keys[] = {
66 { "blame", BLAME },
67 { "blob", BLOB },
68 { "blobraw", BLOBRAW },
69 { "briefs", BRIEFS },
70 { "commits", COMMITS },
71 { "diff", DIFF },
72 { "error", ERR },
73 { "index", INDEX },
74 { "summary", SUMMARY },
75 { "tag", TAG },
76 { "tags", TAGS },
77 { "tree", TREE },
78 { "rss", RSS },
79 };
81 static const struct got_error *gotweb_init_querystring(struct querystring **);
82 static const struct got_error *gotweb_parse_querystring(struct querystring **,
83 char *);
84 static const struct got_error *gotweb_assign_querystring(struct querystring **,
85 char *, char *);
86 static int gotweb_render_index(struct template *);
87 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
88 const char *);
89 static const struct got_error *gotweb_load_got_path(struct request *c,
90 struct repo_dir *);
91 static const struct got_error *gotweb_get_repo_description(char **,
92 struct server *, const char *, int);
93 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
94 const char *, int);
96 static void gotweb_free_querystring(struct querystring *);
97 static void gotweb_free_repo_dir(struct repo_dir *);
99 struct server *gotweb_get_server(const char *);
101 static int
102 gotweb_reply(struct request *c, int status, const char *ctype,
103 struct gotweb_url *location)
105 const char *csp;
107 if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
108 return -1;
110 if (location) {
111 if (tp_writes(c->tp, "Location: ") == -1 ||
112 gotweb_render_url(c, location) == -1 ||
113 tp_writes(c->tp, "\r\n") == -1)
114 return -1;
117 csp = "Content-Security-Policy: default-src 'self'; "
118 "script-src 'none'; object-src 'none';\r\n";
119 if (tp_writes(c->tp, csp) == -1)
120 return -1;
122 if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
123 return -1;
125 return tp_writes(c->tp, "\r\n");
128 static int
129 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
130 const char *suffix)
132 int r;
134 r = tp_writef(c->tp, "Content-Disposition: attachment; "
135 "filename=%s%s\r\n", file, suffix ? suffix : "");
136 if (r == -1)
137 return -1;
138 return gotweb_reply(c, 200, ctype, NULL);
141 void
142 gotweb_process_request(struct request *c)
144 const struct got_error *error = NULL;
145 struct server *srv = NULL;
146 struct querystring *qs = NULL;
147 struct repo_dir *repo_dir = NULL;
148 const char *rss_ctype = "application/rss+xml;charset=utf-8";
149 const uint8_t *buf;
150 size_t len;
151 int r, binary = 0;
153 /* init the transport */
154 error = gotweb_init_transport(&c->t);
155 if (error) {
156 log_warnx("%s: %s", __func__, error->msg);
157 return;
159 /* don't process any further if client disconnected */
160 if (c->sock->client_status == CLIENT_DISCONNECT)
161 return;
162 /* get the gotwebd server */
163 srv = gotweb_get_server(c->server_name);
164 if (srv == NULL) {
165 log_warnx("%s: error server is NULL", __func__);
166 goto err;
168 c->srv = srv;
169 /* parse our querystring */
170 error = gotweb_init_querystring(&qs);
171 if (error) {
172 log_warnx("%s: %s", __func__, error->msg);
173 goto err;
175 c->t->qs = qs;
176 error = gotweb_parse_querystring(&qs, c->querystring);
177 if (error) {
178 log_warnx("%s: %s", __func__, error->msg);
179 goto err;
182 /*
183 * certain actions require a commit id in the querystring. this stops
184 * bad actors from exploiting this by manually manipulating the
185 * querystring.
186 */
188 if (qs->action == BLAME || qs->action == BLOB ||
189 qs->action == BLOBRAW || qs->action == DIFF) {
190 if (qs->commit == NULL) {
191 error = got_error(GOT_ERR_BAD_QUERYSTRING);
192 goto err;
196 if (qs->action != INDEX) {
197 error = gotweb_init_repo_dir(&repo_dir, qs->path);
198 if (error)
199 goto err;
200 error = gotweb_load_got_path(c, repo_dir);
201 c->t->repo_dir = repo_dir;
202 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
203 goto err;
206 if (qs->action == BLOBRAW || qs->action == BLOB) {
207 error = got_get_repo_commits(c, 1);
208 if (error)
209 goto err;
211 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
212 &binary, c);
213 if (error)
214 goto err;
217 switch (qs->action) {
218 case BLAME:
219 error = got_get_repo_commits(c, 1);
220 if (error) {
221 log_warnx("%s: %s", __func__, error->msg);
222 goto err;
224 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
225 return;
226 gotweb_render_page(c->tp, gotweb_render_blame);
227 return;
228 case BLOB:
229 if (binary) {
230 struct gotweb_url url = {
231 .index_page = -1,
232 .page = -1,
233 .action = BLOBRAW,
234 .path = qs->path,
235 .commit = qs->commit,
236 .folder = qs->folder,
237 .file = qs->file,
238 };
240 gotweb_reply(c, 302, NULL, &url);
241 return;
244 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
245 return;
246 gotweb_render_page(c->tp, gotweb_render_blob);
247 return;
248 case BLOBRAW:
249 if (binary)
250 r = gotweb_reply_file(c, "application/octet-stream",
251 qs->file, NULL);
252 else
253 r = gotweb_reply(c, 200, "text/plain", NULL);
254 if (r == -1)
255 return;
256 if (template_flush(c->tp) == -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_write(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 < nitems(querystring_keys); 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;
648 /* entry found */
649 break;
651 done:
652 return error;
655 void
656 gotweb_free_repo_tag(struct repo_tag *rt)
658 if (rt != NULL) {
659 free(rt->commit_id);
660 free(rt->tag_name);
661 free(rt->tag_commit);
662 free(rt->commit_msg);
663 free(rt->tagger);
665 free(rt);
668 void
669 gotweb_free_repo_commit(struct repo_commit *rc)
671 if (rc != NULL) {
672 free(rc->path);
673 free(rc->refs_str);
674 free(rc->commit_id);
675 free(rc->parent_id);
676 free(rc->tree_id);
677 free(rc->author);
678 free(rc->committer);
679 free(rc->commit_msg);
681 free(rc);
684 static void
685 gotweb_free_querystring(struct querystring *qs)
687 if (qs != NULL) {
688 free(qs->commit);
689 free(qs->file);
690 free(qs->folder);
691 free(qs->headref);
692 free(qs->path);
694 free(qs);
697 static void
698 gotweb_free_repo_dir(struct repo_dir *repo_dir)
700 if (repo_dir != NULL) {
701 free(repo_dir->name);
702 free(repo_dir->owner);
703 free(repo_dir->description);
704 free(repo_dir->url);
705 free(repo_dir->path);
707 free(repo_dir);
710 void
711 gotweb_free_transport(struct transport *t)
713 const struct got_error *err;
714 struct repo_commit *rc = NULL, *trc = NULL;
715 struct repo_tag *rt = NULL, *trt = NULL;
716 int i;
718 got_ref_list_free(&t->refs);
719 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
720 TAILQ_REMOVE(&t->repo_commits, rc, entry);
721 gotweb_free_repo_commit(rc);
723 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
724 TAILQ_REMOVE(&t->repo_tags, rt, entry);
725 gotweb_free_repo_tag(rt);
727 gotweb_free_repo_dir(t->repo_dir);
728 gotweb_free_querystring(t->qs);
729 free(t->more_id);
730 free(t->next_id);
731 free(t->prev_id);
732 if (t->blob)
733 got_object_blob_close(t->blob);
734 if (t->fp) {
735 err = got_gotweb_closefile(t->fp);
736 if (err)
737 log_warnx("%s: got_gotweb_closefile failure: %s",
738 __func__, err->msg);
740 if (t->fd != -1 && close(t->fd) == -1)
741 log_warn("%s: close", __func__);
742 if (t->repos) {
743 for (i = 0; i < t->nrepos; ++i)
744 free(t->repos[i]);
745 free(t->repos);
747 if (t->repo)
748 got_repo_close(t->repo);
749 free(t);
752 void
753 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
754 struct gotweb_url *next, int *have_next)
756 struct transport *t = c->t;
757 struct querystring *qs = t->qs;
758 struct server *srv = c->srv;
760 *have_prev = *have_next = 0;
762 switch(qs->action) {
763 case INDEX:
764 if (qs->index_page > 0) {
765 *have_prev = 1;
766 *prev = (struct gotweb_url){
767 .action = -1,
768 .index_page = qs->index_page - 1,
769 .page = -1,
770 };
772 if (t->next_disp == srv->max_repos_display &&
773 t->repos_total != (qs->index_page + 1) *
774 srv->max_repos_display) {
775 *have_next = 1;
776 *next = (struct gotweb_url){
777 .action = -1,
778 .index_page = qs->index_page + 1,
779 .page = -1,
780 };
782 break;
783 case TAGS:
784 if (t->prev_id && qs->commit != NULL &&
785 strcmp(qs->commit, t->prev_id) != 0) {
786 *have_prev = 1;
787 *prev = (struct gotweb_url){
788 .action = TAGS,
789 .index_page = -1,
790 .page = qs->page - 1,
791 .path = qs->path,
792 .commit = t->prev_id,
793 .headref = qs->headref,
794 };
796 if (t->next_id) {
797 *have_next = 1;
798 *next = (struct gotweb_url){
799 .action = TAGS,
800 .index_page = -1,
801 .page = qs->page + 1,
802 .path = qs->path,
803 .commit = t->next_id,
804 .headref = qs->headref,
805 };
807 break;
811 static int
812 gotweb_render_index(struct template *tp)
814 const struct got_error *error = NULL;
815 struct request *c = tp->tp_arg;
816 struct server *srv = c->srv;
817 struct transport *t = c->t;
818 struct querystring *qs = t->qs;
819 struct repo_dir *repo_dir = NULL;
820 struct dirent **sd_dent = t->repos;
821 unsigned int d_i, d_disp = 0;
822 unsigned int d_skipped = 0;
823 int type, r;
825 if (gotweb_render_repo_table_hdr(c->tp) == -1)
826 return -1;
828 for (d_i = 0; d_i < t->nrepos; d_i++) {
829 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
830 break;
832 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
833 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
834 d_skipped++;
835 continue;
838 error = got_path_dirent_type(&type, srv->repos_path,
839 sd_dent[d_i]);
840 if (error)
841 continue;
842 if (type != DT_DIR) {
843 d_skipped++;
844 continue;
847 if (qs->index_page > 0 && (qs->index_page *
848 srv->max_repos_display) > t->prev_disp) {
849 t->prev_disp++;
850 continue;
853 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
854 if (error)
855 continue;
857 error = gotweb_load_got_path(c, repo_dir);
858 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
859 if (error->code != GOT_ERR_NOT_GIT_REPO)
860 log_warnx("%s: %s: %s", __func__,
861 sd_dent[d_i]->d_name, error->msg);
862 gotweb_free_repo_dir(repo_dir);
863 repo_dir = NULL;
864 d_skipped++;
865 continue;
868 d_disp++;
869 t->prev_disp++;
871 r = gotweb_render_repo_fragment(c->tp, repo_dir);
872 gotweb_free_repo_dir(repo_dir);
873 repo_dir = NULL;
874 got_repo_close(t->repo);
875 t->repo = NULL;
876 if (r == -1)
877 return -1;
879 t->next_disp++;
880 if (d_disp == srv->max_repos_display)
881 break;
883 t->repos_total = t->nrepos - d_skipped;
885 if (srv->max_repos_display == 0)
886 return 0;
887 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
888 return 0;
889 if (t->repos_total <= srv->max_repos ||
890 t->repos_total <= srv->max_repos_display)
891 return 0;
893 if (gotweb_render_navs(c->tp) == -1)
894 return -1;
896 return 0;
899 static inline int
900 should_urlencode(int c)
902 if (c <= ' ' || c >= 127)
903 return 1;
905 switch (c) {
906 /* gen-delim */
907 case ':':
908 case '/':
909 case '?':
910 case '#':
911 case '[':
912 case ']':
913 case '@':
914 /* sub-delims */
915 case '!':
916 case '$':
917 case '&':
918 case '\'':
919 case '(':
920 case ')':
921 case '*':
922 case '+':
923 case ',':
924 case ';':
925 case '=':
926 /* needed because the URLs are embedded into the HTML */
927 case '\"':
928 return 1;
929 default:
930 return 0;
934 static char *
935 gotweb_urlencode(const char *str)
937 const char *s;
938 char *escaped;
939 size_t i, len;
940 int a, b;
942 len = 0;
943 for (s = str; *s; ++s) {
944 len++;
945 if (should_urlencode(*s))
946 len += 2;
949 escaped = calloc(1, len + 1);
950 if (escaped == NULL)
951 return NULL;
953 i = 0;
954 for (s = str; *s; ++s) {
955 if (should_urlencode(*s)) {
956 a = (*s & 0xF0) >> 4;
957 b = (*s & 0x0F);
959 escaped[i++] = '%';
960 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
961 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
962 } else
963 escaped[i++] = *s;
966 return escaped;
969 const char *
970 gotweb_action_name(int action)
972 switch (action) {
973 case BLAME:
974 return "blame";
975 case BLOB:
976 return "blob";
977 case BLOBRAW:
978 return "blobraw";
979 case BRIEFS:
980 return "briefs";
981 case COMMITS:
982 return "commits";
983 case DIFF:
984 return "diff";
985 case ERR:
986 return "err";
987 case INDEX:
988 return "index";
989 case SUMMARY:
990 return "summary";
991 case TAG:
992 return "tag";
993 case TAGS:
994 return "tags";
995 case TREE:
996 return "tree";
997 case RSS:
998 return "rss";
999 default:
1000 return NULL;
1004 int
1005 gotweb_render_url(struct request *c, struct gotweb_url *url)
1007 const char *sep = "?", *action;
1008 char *tmp;
1009 int r;
1011 action = gotweb_action_name(url->action);
1012 if (action != NULL) {
1013 if (tp_writef(c->tp, "?action=%s", action) == -1)
1014 return -1;
1015 sep = "&";
1018 if (url->commit) {
1019 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
1020 return -1;
1021 sep = "&";
1024 if (url->previd) {
1025 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
1026 return -1;
1027 sep = "&";
1030 if (url->prevset) {
1031 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
1032 return -1;
1033 sep = "&";
1036 if (url->file) {
1037 tmp = gotweb_urlencode(url->file);
1038 if (tmp == NULL)
1039 return -1;
1040 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1041 free(tmp);
1042 if (r == -1)
1043 return -1;
1044 sep = "&";
1047 if (url->folder) {
1048 tmp = gotweb_urlencode(url->folder);
1049 if (tmp == NULL)
1050 return -1;
1051 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1052 free(tmp);
1053 if (r == -1)
1054 return -1;
1055 sep = "&";
1058 if (url->headref) {
1059 tmp = gotweb_urlencode(url->headref);
1060 if (tmp == NULL)
1061 return -1;
1062 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1063 free(tmp);
1064 if (r == -1)
1065 return -1;
1066 sep = "&";
1069 if (url->index_page != -1) {
1070 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1071 url->index_page) == -1)
1072 return -1;
1073 sep = "&";
1076 if (url->path) {
1077 tmp = gotweb_urlencode(url->path);
1078 if (tmp == NULL)
1079 return -1;
1080 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1081 free(tmp);
1082 if (r == -1)
1083 return -1;
1084 sep = "&";
1087 if (url->page != -1) {
1088 if (tp_writef(c->tp, "%spage=%d", sep, url->page) == -1)
1089 return -1;
1090 sep = "&";
1093 return 0;
1096 int
1097 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1099 struct template *tp = c->tp;
1100 const char *proto = c->https ? "https" : "http";
1102 if (tp_writes(tp, proto) == -1 ||
1103 tp_writes(tp, "://") == -1 ||
1104 tp_htmlescape(tp, c->server_name) == -1 ||
1105 tp_htmlescape(tp, c->document_uri) == -1)
1106 return -1;
1108 return gotweb_render_url(c, url);
1111 static const struct got_error *
1112 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1114 const struct got_error *error = NULL;
1115 struct socket *sock = c->sock;
1116 struct server *srv = c->srv;
1117 struct transport *t = c->t;
1118 DIR *dt;
1119 char *dir_test;
1121 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1122 GOTWEB_GIT_DIR) == -1)
1123 return got_error_from_errno("asprintf");
1125 dt = opendir(dir_test);
1126 if (dt == NULL) {
1127 free(dir_test);
1128 } else {
1129 repo_dir->path = dir_test;
1130 dir_test = NULL;
1131 goto open_repo;
1134 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1135 repo_dir->name) == -1) {
1136 error = got_error_from_errno("asprintf");
1137 goto err;
1140 dt = opendir(dir_test);
1141 if (dt == NULL) {
1142 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1143 goto err;
1144 } else {
1145 repo_dir->path = dir_test;
1146 dir_test = NULL;
1149 open_repo:
1150 if (srv->respect_exportok &&
1151 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1152 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1153 goto err;
1156 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1157 if (error)
1158 goto err;
1159 error = gotweb_get_repo_description(&repo_dir->description, srv,
1160 repo_dir->path, dirfd(dt));
1161 if (error)
1162 goto err;
1163 error = got_get_repo_owner(&repo_dir->owner, c);
1164 if (error)
1165 goto err;
1166 if (srv->show_repo_age) {
1167 error = got_get_repo_age(&repo_dir->age, c, NULL);
1168 if (error)
1169 goto err;
1171 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1172 dirfd(dt));
1173 err:
1174 free(dir_test);
1175 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1176 error = got_error_from_errno("closedir");
1177 if (error && t->repo) {
1178 got_repo_close(t->repo);
1179 t->repo = NULL;
1181 return error;
1184 static const struct got_error *
1185 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1187 const struct got_error *error;
1189 *repo_dir = calloc(1, sizeof(**repo_dir));
1190 if (*repo_dir == NULL)
1191 return got_error_from_errno("calloc");
1193 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1194 error = got_error_from_errno("asprintf");
1195 free(*repo_dir);
1196 *repo_dir = NULL;
1197 return error;
1199 (*repo_dir)->owner = NULL;
1200 (*repo_dir)->description = NULL;
1201 (*repo_dir)->url = NULL;
1202 (*repo_dir)->path = NULL;
1204 return NULL;
1207 static const struct got_error *
1208 gotweb_get_repo_description(char **description, struct server *srv,
1209 const char *dirpath, int dir)
1211 const struct got_error *error = NULL;
1212 struct stat sb;
1213 int fd = -1;
1214 off_t len;
1216 *description = NULL;
1217 if (srv->show_repo_description == 0)
1218 return NULL;
1220 fd = openat(dir, "description", O_RDONLY);
1221 if (fd == -1) {
1222 if (errno != ENOENT && errno != EACCES) {
1223 error = got_error_from_errno_fmt("openat %s/%s",
1224 dirpath, "description");
1226 goto done;
1229 if (fstat(fd, &sb) == -1) {
1230 error = got_error_from_errno_fmt("fstat %s/%s",
1231 dirpath, "description");
1232 goto done;
1235 len = sb.st_size;
1236 if (len > GOTWEBD_MAXDESCRSZ - 1)
1237 len = GOTWEBD_MAXDESCRSZ - 1;
1239 *description = calloc(len + 1, sizeof(**description));
1240 if (*description == NULL) {
1241 error = got_error_from_errno("calloc");
1242 goto done;
1245 if (read(fd, *description, len) == -1)
1246 error = got_error_from_errno("read");
1247 done:
1248 if (fd != -1 && close(fd) == -1 && error == NULL)
1249 error = got_error_from_errno("close");
1250 return error;
1253 static const struct got_error *
1254 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1255 int dir)
1257 const struct got_error *error = NULL;
1258 struct stat sb;
1259 int fd = -1;
1260 off_t len;
1262 *url = NULL;
1263 if (srv->show_repo_cloneurl == 0)
1264 return NULL;
1266 fd = openat(dir, "cloneurl", O_RDONLY);
1267 if (fd == -1) {
1268 if (errno != ENOENT && errno != EACCES) {
1269 error = got_error_from_errno_fmt("openat %s/%s",
1270 dirpath, "cloneurl");
1272 goto done;
1275 if (fstat(fd, &sb) == -1) {
1276 error = got_error_from_errno_fmt("fstat %s/%s",
1277 dirpath, "cloneurl");
1278 goto done;
1281 len = sb.st_size;
1282 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1283 len = GOTWEBD_MAXCLONEURLSZ - 1;
1285 *url = calloc(len + 1, sizeof(**url));
1286 if (*url == NULL) {
1287 error = got_error_from_errno("calloc");
1288 goto done;
1291 if (read(fd, *url, 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 int
1300 gotweb_render_age(struct template *tp, time_t committer_time)
1302 struct request *c = tp->tp_arg;
1303 long long diff_time;
1304 const char *years = "years ago", *months = "months ago";
1305 const char *weeks = "weeks ago", *days = "days ago";
1306 const char *hours = "hours ago", *minutes = "minutes ago";
1307 const char *seconds = "seconds ago", *now = "right now";
1309 diff_time = time(NULL) - committer_time;
1310 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1311 if (tp_writef(c->tp, "%lld %s",
1312 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1313 return -1;
1314 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1315 if (tp_writef(c->tp, "%lld %s",
1316 (diff_time / 60 / 60 / 24 / (365 / 12)),
1317 months) == -1)
1318 return -1;
1319 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1320 if (tp_writef(c->tp, "%lld %s",
1321 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1322 return -1;
1323 } else if (diff_time > 60 * 60 * 24 * 2) {
1324 if (tp_writef(c->tp, "%lld %s",
1325 (diff_time / 60 / 60 / 24), days) == -1)
1326 return -1;
1327 } else if (diff_time > 60 * 60 * 2) {
1328 if (tp_writef(c->tp, "%lld %s",
1329 (diff_time / 60 / 60), hours) == -1)
1330 return -1;
1331 } else if (diff_time > 60 * 2) {
1332 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1333 minutes) == -1)
1334 return -1;
1335 } else if (diff_time > 2) {
1336 if (tp_writef(c->tp, "%lld %s", diff_time,
1337 seconds) == -1)
1338 return -1;
1339 } else {
1340 if (tp_writes(tp, now) == -1)
1341 return -1;
1343 return 0;