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 };
64 static const struct action_keys action_keys[] = {
65 { "blame", BLAME },
66 { "blob", BLOB },
67 { "blobraw", BLOBRAW },
68 { "briefs", BRIEFS },
69 { "commits", COMMITS },
70 { "diff", DIFF },
71 { "error", ERR },
72 { "index", INDEX },
73 { "patch", PATCH },
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 qs->action == PATCH) {
191 if (qs->commit == NULL) {
192 error = got_error(GOT_ERR_BAD_QUERYSTRING);
193 goto err;
197 if (qs->action != INDEX) {
198 error = gotweb_init_repo_dir(&repo_dir, qs->path);
199 if (error)
200 goto err;
201 error = gotweb_load_got_path(c, repo_dir);
202 c->t->repo_dir = repo_dir;
203 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
204 goto err;
207 if (qs->action == BLOBRAW || qs->action == BLOB) {
208 error = got_get_repo_commits(c, 1);
209 if (error)
210 goto err;
212 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
213 &binary, c, qs->folder, qs->file, qs->commit);
214 if (error)
215 goto err;
218 switch (qs->action) {
219 case BLAME:
220 error = got_get_repo_commits(c, 1);
221 if (error) {
222 log_warnx("%s: %s", __func__, error->msg);
223 goto err;
225 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
226 return;
227 gotweb_render_page(c->tp, gotweb_render_blame);
228 return;
229 case BLOB:
230 if (binary) {
231 struct gotweb_url url = {
232 .index_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 PATCH:
317 error = got_get_repo_commits(c, 1);
318 if (error) {
319 log_warnx("%s: %s", __func__, error->msg);
320 goto err;
322 error = got_open_diff_for_output(&c->t->fp, c);
323 if (error) {
324 log_warnx("%s: %s", __func__, error->msg);
325 goto err;
327 if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
328 return;
329 gotweb_render_patch(c->tp);
330 return;
331 case RSS:
332 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
333 if (error)
334 goto err;
335 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
336 == -1)
337 return;
338 gotweb_render_rss(c->tp);
339 return;
340 case SUMMARY:
341 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
342 got_ref_cmp_by_name, NULL);
343 if (error) {
344 log_warnx("%s: got_ref_list: %s", __func__,
345 error->msg);
346 goto err;
348 error = got_get_repo_commits(c, srv->summary_commits_display);
349 if (error)
350 goto err;
351 qs->action = TAGS;
352 error = got_get_repo_tags(c, srv->summary_tags_display);
353 if (error) {
354 log_warnx("%s: got_get_repo_tags: %s", __func__,
355 error->msg);
356 goto err;
358 qs->action = SUMMARY;
359 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
360 return;
361 gotweb_render_page(c->tp, gotweb_render_summary);
362 return;
363 case TAG:
364 error = got_get_repo_tags(c, 1);
365 if (error) {
366 log_warnx("%s: %s", __func__, error->msg);
367 goto err;
369 if (c->t->tag_count == 0) {
370 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
371 "bad commit id");
372 goto err;
374 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
375 return;
376 gotweb_render_page(c->tp, gotweb_render_tag);
377 return;
378 case TAGS:
379 error = got_get_repo_tags(c, srv->max_commits_display);
380 if (error) {
381 log_warnx("%s: %s", __func__, error->msg);
382 goto err;
384 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
385 return;
386 gotweb_render_page(c->tp, gotweb_render_tags);
387 return;
388 case TREE:
389 error = got_get_repo_commits(c, 1);
390 if (error) {
391 log_warnx("%s: %s", __func__, error->msg);
392 goto err;
394 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
395 return;
396 gotweb_render_page(c->tp, gotweb_render_tree);
397 return;
398 case ERR:
399 default:
400 error = got_error(GOT_ERR_BAD_QUERYSTRING);
403 err:
404 c->t->error = error;
405 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
406 return;
407 gotweb_render_page(c->tp, gotweb_render_error);
410 struct server *
411 gotweb_get_server(const char *server_name)
413 struct server *srv;
415 /* check against the server name first */
416 if (*server_name != '\0')
417 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
418 if (strcmp(srv->name, server_name) == 0)
419 return srv;
421 /* otherwise, use the first server */
422 return TAILQ_FIRST(&gotwebd_env->servers);
423 };
425 const struct got_error *
426 gotweb_init_transport(struct transport **t)
428 const struct got_error *error = NULL;
430 *t = calloc(1, sizeof(**t));
431 if (*t == NULL)
432 return got_error_from_errno2(__func__, "calloc");
434 TAILQ_INIT(&(*t)->repo_commits);
435 TAILQ_INIT(&(*t)->repo_tags);
436 TAILQ_INIT(&(*t)->refs);
438 (*t)->fd = -1;
440 return error;
443 static const struct got_error *
444 gotweb_init_querystring(struct querystring **qs)
446 const struct got_error *error = NULL;
448 *qs = calloc(1, sizeof(**qs));
449 if (*qs == NULL)
450 return got_error_from_errno2(__func__, "calloc");
452 (*qs)->headref = strdup("HEAD");
453 if ((*qs)->headref == NULL) {
454 free(*qs);
455 *qs = NULL;
456 return got_error_from_errno2(__func__, "strdup");
459 (*qs)->action = INDEX;
461 return error;
464 static const struct got_error *
465 gotweb_parse_querystring(struct querystring **qs, char *qst)
467 const struct got_error *error = NULL;
468 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
469 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
471 if (qst == NULL)
472 return error;
474 tok1 = strdup(qst);
475 if (tok1 == NULL)
476 return got_error_from_errno2(__func__, "strdup");
478 tok1_pair = tok1;
479 tok1_end = tok1;
481 while (tok1_pair != NULL) {
482 strsep(&tok1_end, "&");
484 tok2 = strdup(tok1_pair);
485 if (tok2 == NULL) {
486 free(tok1);
487 return got_error_from_errno2(__func__, "strdup");
490 tok2_pair = tok2;
491 tok2_end = tok2;
493 while (tok2_pair != NULL) {
494 strsep(&tok2_end, "=");
495 if (tok2_end) {
496 error = gotweb_assign_querystring(qs, tok2_pair,
497 tok2_end);
498 if (error)
499 goto err;
501 tok2_pair = tok2_end;
503 free(tok2);
504 tok1_pair = tok1_end;
506 free(tok1);
507 return error;
508 err:
509 free(tok2);
510 free(tok1);
511 return error;
514 /*
515 * Adapted from usr.sbin/httpd/httpd.c url_decode.
516 */
517 static const struct got_error *
518 gotweb_urldecode(char *url)
520 char *p, *q;
521 char hex[3];
522 unsigned long x;
524 hex[2] = '\0';
525 p = q = url;
527 while (*p != '\0') {
528 switch (*p) {
529 case '%':
530 /* Encoding character is followed by two hex chars */
531 if (!isxdigit((unsigned char)p[1]) ||
532 !isxdigit((unsigned char)p[2]) ||
533 (p[1] == '0' && p[2] == '0'))
534 return got_error(GOT_ERR_BAD_QUERYSTRING);
536 hex[0] = p[1];
537 hex[1] = p[2];
539 /*
540 * We don't have to validate "hex" because it is
541 * guaranteed to include two hex chars followed by nul.
542 */
543 x = strtoul(hex, NULL, 16);
544 *q = (char)x;
545 p += 2;
546 break;
547 default:
548 *q = *p;
549 break;
551 p++;
552 q++;
554 *q = '\0';
556 return NULL;
559 static const struct got_error *
560 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
562 const struct got_error *error = NULL;
563 const char *errstr;
564 int a_cnt, el_cnt;
566 error = gotweb_urldecode(value);
567 if (error)
568 return error;
570 for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
571 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
572 continue;
574 switch (querystring_keys[el_cnt].element) {
575 case ACTION:
576 for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
577 if (strcmp(value, action_keys[a_cnt].name) != 0)
578 continue;
579 else if (strcmp(value,
580 action_keys[a_cnt].name) == 0){
581 (*qs)->action =
582 action_keys[a_cnt].action;
583 goto qa_found;
586 (*qs)->action = ERR;
587 qa_found:
588 break;
589 case COMMIT:
590 (*qs)->commit = strdup(value);
591 if ((*qs)->commit == NULL) {
592 error = got_error_from_errno2(__func__,
593 "strdup");
594 goto done;
596 break;
597 case RFILE:
598 (*qs)->file = strdup(value);
599 if ((*qs)->file == NULL) {
600 error = got_error_from_errno2(__func__,
601 "strdup");
602 goto done;
604 break;
605 case FOLDER:
606 (*qs)->folder = strdup(value);
607 if ((*qs)->folder == NULL) {
608 error = got_error_from_errno2(__func__,
609 "strdup");
610 goto done;
612 break;
613 case HEADREF:
614 free((*qs)->headref);
615 (*qs)->headref = strdup(value);
616 if ((*qs)->headref == NULL) {
617 error = got_error_from_errno2(__func__,
618 "strdup");
619 goto done;
621 break;
622 case INDEX_PAGE:
623 if (*value == '\0')
624 break;
625 (*qs)->index_page = strtonum(value, INT64_MIN,
626 INT64_MAX, &errstr);
627 if (errstr) {
628 error = got_error_from_errno3(__func__,
629 "strtonum", errstr);
630 goto done;
632 if ((*qs)->index_page < 0)
633 (*qs)->index_page = 0;
634 break;
635 case PATH:
636 (*qs)->path = strdup(value);
637 if ((*qs)->path == NULL) {
638 error = got_error_from_errno2(__func__,
639 "strdup");
640 goto done;
642 break;
645 /* entry found */
646 break;
648 done:
649 return error;
652 void
653 gotweb_free_repo_tag(struct repo_tag *rt)
655 if (rt != NULL) {
656 free(rt->commit_id);
657 free(rt->tag_name);
658 free(rt->tag_commit);
659 free(rt->commit_msg);
660 free(rt->tagger);
662 free(rt);
665 void
666 gotweb_free_repo_commit(struct repo_commit *rc)
668 if (rc != NULL) {
669 free(rc->path);
670 free(rc->refs_str);
671 free(rc->commit_id);
672 free(rc->parent_id);
673 free(rc->tree_id);
674 free(rc->author);
675 free(rc->committer);
676 free(rc->commit_msg);
678 free(rc);
681 static void
682 gotweb_free_querystring(struct querystring *qs)
684 if (qs != NULL) {
685 free(qs->commit);
686 free(qs->file);
687 free(qs->folder);
688 free(qs->headref);
689 free(qs->path);
691 free(qs);
694 static void
695 gotweb_free_repo_dir(struct repo_dir *repo_dir)
697 if (repo_dir != NULL) {
698 free(repo_dir->name);
699 free(repo_dir->owner);
700 free(repo_dir->description);
701 free(repo_dir->url);
702 free(repo_dir->path);
704 free(repo_dir);
707 void
708 gotweb_free_transport(struct transport *t)
710 const struct got_error *err;
711 struct repo_commit *rc = NULL, *trc = NULL;
712 struct repo_tag *rt = NULL, *trt = NULL;
713 int i;
715 got_ref_list_free(&t->refs);
716 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
717 TAILQ_REMOVE(&t->repo_commits, rc, entry);
718 gotweb_free_repo_commit(rc);
720 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
721 TAILQ_REMOVE(&t->repo_tags, rt, entry);
722 gotweb_free_repo_tag(rt);
724 gotweb_free_repo_dir(t->repo_dir);
725 gotweb_free_querystring(t->qs);
726 free(t->more_id);
727 free(t->tags_more_id);
728 if (t->blob)
729 got_object_blob_close(t->blob);
730 if (t->fp) {
731 err = got_gotweb_closefile(t->fp);
732 if (err)
733 log_warnx("%s: got_gotweb_closefile failure: %s",
734 __func__, err->msg);
736 if (t->fd != -1 && close(t->fd) == -1)
737 log_warn("%s: close", __func__);
738 if (t->repos) {
739 for (i = 0; i < t->nrepos; ++i)
740 free(t->repos[i]);
741 free(t->repos);
743 if (t->repo)
744 got_repo_close(t->repo);
745 free(t);
748 void
749 gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
750 struct gotweb_url *next, int *have_next)
752 struct transport *t = c->t;
753 struct querystring *qs = t->qs;
754 struct server *srv = c->srv;
756 *have_prev = *have_next = 0;
758 if (qs->index_page > 0) {
759 *have_prev = 1;
760 *prev = (struct gotweb_url){
761 .action = -1,
762 .index_page = qs->index_page - 1,
763 };
765 if (t->next_disp == srv->max_repos_display &&
766 t->repos_total != (qs->index_page + 1) *
767 srv->max_repos_display) {
768 *have_next = 1;
769 *next = (struct gotweb_url){
770 .action = -1,
771 .index_page = qs->index_page + 1,
772 };
776 static int
777 gotweb_render_index(struct template *tp)
779 const struct got_error *error = NULL;
780 struct request *c = tp->tp_arg;
781 struct server *srv = c->srv;
782 struct transport *t = c->t;
783 struct querystring *qs = t->qs;
784 struct repo_dir *repo_dir = NULL;
785 struct dirent **sd_dent = t->repos;
786 unsigned int d_i, d_disp = 0;
787 unsigned int d_skipped = 0;
788 int type, r;
790 if (gotweb_render_repo_table_hdr(c->tp) == -1)
791 return -1;
793 for (d_i = 0; d_i < t->nrepos; d_i++) {
794 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
795 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
796 d_skipped++;
797 continue;
800 error = got_path_dirent_type(&type, srv->repos_path,
801 sd_dent[d_i]);
802 if (error)
803 continue;
804 if (type != DT_DIR) {
805 d_skipped++;
806 continue;
809 if (qs->index_page > 0 && (qs->index_page *
810 srv->max_repos_display) > t->prev_disp) {
811 t->prev_disp++;
812 continue;
815 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
816 if (error)
817 continue;
819 error = gotweb_load_got_path(c, repo_dir);
820 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
821 if (error->code != GOT_ERR_NOT_GIT_REPO)
822 log_warnx("%s: %s: %s", __func__,
823 sd_dent[d_i]->d_name, error->msg);
824 gotweb_free_repo_dir(repo_dir);
825 repo_dir = NULL;
826 d_skipped++;
827 continue;
830 d_disp++;
831 t->prev_disp++;
833 r = gotweb_render_repo_fragment(c->tp, repo_dir);
834 gotweb_free_repo_dir(repo_dir);
835 repo_dir = NULL;
836 got_repo_close(t->repo);
837 t->repo = NULL;
838 if (r == -1)
839 return -1;
841 t->next_disp++;
842 if (d_disp == srv->max_repos_display)
843 break;
845 t->repos_total = t->nrepos - d_skipped;
847 if (srv->max_repos_display == 0 ||
848 t->repos_total <= srv->max_repos_display)
849 return 0;
851 if (gotweb_render_navs(c->tp) == -1)
852 return -1;
854 return 0;
857 static inline int
858 should_urlencode(int c)
860 if (c <= ' ' || c >= 127)
861 return 1;
863 switch (c) {
864 /* gen-delim */
865 case ':':
866 case '/':
867 case '?':
868 case '#':
869 case '[':
870 case ']':
871 case '@':
872 /* sub-delims */
873 case '!':
874 case '$':
875 case '&':
876 case '\'':
877 case '(':
878 case ')':
879 case '*':
880 case '+':
881 case ',':
882 case ';':
883 case '=':
884 /* needed because the URLs are embedded into the HTML */
885 case '\"':
886 return 1;
887 default:
888 return 0;
892 static char *
893 gotweb_urlencode(const char *str)
895 const char *s;
896 char *escaped;
897 size_t i, len;
898 int a, b;
900 len = 0;
901 for (s = str; *s; ++s) {
902 len++;
903 if (should_urlencode(*s))
904 len += 2;
907 escaped = calloc(1, len + 1);
908 if (escaped == NULL)
909 return NULL;
911 i = 0;
912 for (s = str; *s; ++s) {
913 if (should_urlencode(*s)) {
914 a = (*s & 0xF0) >> 4;
915 b = (*s & 0x0F);
917 escaped[i++] = '%';
918 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
919 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
920 } else
921 escaped[i++] = *s;
924 return escaped;
927 const char *
928 gotweb_action_name(int action)
930 switch (action) {
931 case BLAME:
932 return "blame";
933 case BLOB:
934 return "blob";
935 case BLOBRAW:
936 return "blobraw";
937 case BRIEFS:
938 return "briefs";
939 case COMMITS:
940 return "commits";
941 case DIFF:
942 return "diff";
943 case ERR:
944 return "err";
945 case INDEX:
946 return "index";
947 case PATCH:
948 return "patch";
949 case SUMMARY:
950 return "summary";
951 case TAG:
952 return "tag";
953 case TAGS:
954 return "tags";
955 case TREE:
956 return "tree";
957 case RSS:
958 return "rss";
959 default:
960 return NULL;
964 int
965 gotweb_render_url(struct request *c, struct gotweb_url *url)
967 const char *sep = "?", *action;
968 char *tmp;
969 int r;
971 action = gotweb_action_name(url->action);
972 if (action != NULL) {
973 if (tp_writef(c->tp, "?action=%s", action) == -1)
974 return -1;
975 sep = "&";
978 if (url->commit) {
979 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
980 return -1;
981 sep = "&";
984 if (url->previd) {
985 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
986 return -1;
987 sep = "&";
990 if (url->prevset) {
991 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
992 return -1;
993 sep = "&";
996 if (url->file) {
997 tmp = gotweb_urlencode(url->file);
998 if (tmp == NULL)
999 return -1;
1000 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1001 free(tmp);
1002 if (r == -1)
1003 return -1;
1004 sep = "&";
1007 if (url->folder) {
1008 tmp = gotweb_urlencode(url->folder);
1009 if (tmp == NULL)
1010 return -1;
1011 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1012 free(tmp);
1013 if (r == -1)
1014 return -1;
1015 sep = "&";
1018 if (url->headref) {
1019 tmp = gotweb_urlencode(url->headref);
1020 if (tmp == NULL)
1021 return -1;
1022 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1023 free(tmp);
1024 if (r == -1)
1025 return -1;
1026 sep = "&";
1029 if (url->index_page != -1) {
1030 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1031 url->index_page) == -1)
1032 return -1;
1033 sep = "&";
1036 if (url->path) {
1037 tmp = gotweb_urlencode(url->path);
1038 if (tmp == NULL)
1039 return -1;
1040 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1041 free(tmp);
1042 if (r == -1)
1043 return -1;
1044 sep = "&";
1047 return 0;
1050 int
1051 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1053 struct template *tp = c->tp;
1054 const char *proto = c->https ? "https" : "http";
1056 if (tp_writes(tp, proto) == -1 ||
1057 tp_writes(tp, "://") == -1 ||
1058 tp_htmlescape(tp, c->server_name) == -1 ||
1059 tp_htmlescape(tp, c->document_uri) == -1)
1060 return -1;
1062 return gotweb_render_url(c, url);
1065 static const struct got_error *
1066 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1068 const struct got_error *error = NULL;
1069 struct socket *sock = c->sock;
1070 struct server *srv = c->srv;
1071 struct transport *t = c->t;
1072 DIR *dt;
1073 char *dir_test;
1075 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1076 GOTWEB_GIT_DIR) == -1)
1077 return got_error_from_errno("asprintf");
1079 dt = opendir(dir_test);
1080 if (dt == NULL) {
1081 free(dir_test);
1082 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1083 repo_dir->name) == -1)
1084 return got_error_from_errno("asprintf");
1085 dt = opendir(dir_test);
1086 if (dt == NULL) {
1087 free(dir_test);
1088 return got_error_path(repo_dir->name,
1089 GOT_ERR_NOT_GIT_REPO);
1093 repo_dir->path = dir_test;
1094 dir_test = NULL;
1096 if (srv->respect_exportok &&
1097 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1098 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1099 goto err;
1102 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1103 if (error)
1104 goto err;
1105 error = gotweb_get_repo_description(&repo_dir->description, srv,
1106 repo_dir->path, dirfd(dt));
1107 if (error)
1108 goto err;
1109 error = got_get_repo_owner(&repo_dir->owner, c);
1110 if (error)
1111 goto err;
1112 if (srv->show_repo_age) {
1113 error = got_get_repo_age(&repo_dir->age, c, NULL);
1114 if (error)
1115 goto err;
1117 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1118 dirfd(dt));
1119 err:
1120 free(dir_test);
1121 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1122 error = got_error_from_errno("closedir");
1123 if (error && t->repo) {
1124 got_repo_close(t->repo);
1125 t->repo = NULL;
1127 return error;
1130 static const struct got_error *
1131 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1133 const struct got_error *error;
1135 *repo_dir = calloc(1, sizeof(**repo_dir));
1136 if (*repo_dir == NULL)
1137 return got_error_from_errno("calloc");
1139 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1140 error = got_error_from_errno("asprintf");
1141 free(*repo_dir);
1142 *repo_dir = NULL;
1143 return error;
1145 (*repo_dir)->owner = NULL;
1146 (*repo_dir)->description = NULL;
1147 (*repo_dir)->url = NULL;
1148 (*repo_dir)->path = NULL;
1150 return NULL;
1153 static const struct got_error *
1154 gotweb_get_repo_description(char **description, struct server *srv,
1155 const char *dirpath, int dir)
1157 const struct got_error *error = NULL;
1158 struct stat sb;
1159 int fd = -1;
1160 off_t len;
1162 *description = NULL;
1163 if (srv->show_repo_description == 0)
1164 return NULL;
1166 fd = openat(dir, "description", O_RDONLY);
1167 if (fd == -1) {
1168 if (errno != ENOENT && errno != EACCES) {
1169 error = got_error_from_errno_fmt("openat %s/%s",
1170 dirpath, "description");
1172 goto done;
1175 if (fstat(fd, &sb) == -1) {
1176 error = got_error_from_errno_fmt("fstat %s/%s",
1177 dirpath, "description");
1178 goto done;
1181 len = sb.st_size;
1182 if (len > GOTWEBD_MAXDESCRSZ - 1)
1183 len = GOTWEBD_MAXDESCRSZ - 1;
1185 *description = calloc(len + 1, sizeof(**description));
1186 if (*description == NULL) {
1187 error = got_error_from_errno("calloc");
1188 goto done;
1191 if (read(fd, *description, len) == -1)
1192 error = got_error_from_errno("read");
1193 done:
1194 if (fd != -1 && close(fd) == -1 && error == NULL)
1195 error = got_error_from_errno("close");
1196 return error;
1199 static const struct got_error *
1200 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1201 int dir)
1203 const struct got_error *error = NULL;
1204 struct stat sb;
1205 int fd = -1;
1206 off_t len;
1208 *url = NULL;
1209 if (srv->show_repo_cloneurl == 0)
1210 return NULL;
1212 fd = openat(dir, "cloneurl", O_RDONLY);
1213 if (fd == -1) {
1214 if (errno != ENOENT && errno != EACCES) {
1215 error = got_error_from_errno_fmt("openat %s/%s",
1216 dirpath, "cloneurl");
1218 goto done;
1221 if (fstat(fd, &sb) == -1) {
1222 error = got_error_from_errno_fmt("fstat %s/%s",
1223 dirpath, "cloneurl");
1224 goto done;
1227 len = sb.st_size;
1228 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1229 len = GOTWEBD_MAXCLONEURLSZ - 1;
1231 *url = calloc(len + 1, sizeof(**url));
1232 if (*url == NULL) {
1233 error = got_error_from_errno("calloc");
1234 goto done;
1237 if (read(fd, *url, len) == -1)
1238 error = got_error_from_errno("read");
1239 done:
1240 if (fd != -1 && close(fd) == -1 && error == NULL)
1241 error = got_error_from_errno("close");
1242 return error;
1245 int
1246 gotweb_render_age(struct template *tp, time_t committer_time)
1248 struct request *c = tp->tp_arg;
1249 long long diff_time;
1250 const char *years = "years ago", *months = "months ago";
1251 const char *weeks = "weeks ago", *days = "days ago";
1252 const char *hours = "hours ago", *minutes = "minutes ago";
1253 const char *seconds = "seconds ago", *now = "right now";
1255 diff_time = time(NULL) - committer_time;
1256 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1257 if (tp_writef(c->tp, "%lld %s",
1258 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1259 return -1;
1260 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1261 if (tp_writef(c->tp, "%lld %s",
1262 (diff_time / 60 / 60 / 24 / (365 / 12)),
1263 months) == -1)
1264 return -1;
1265 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1266 if (tp_writef(c->tp, "%lld %s",
1267 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1268 return -1;
1269 } else if (diff_time > 60 * 60 * 24 * 2) {
1270 if (tp_writef(c->tp, "%lld %s",
1271 (diff_time / 60 / 60 / 24), days) == -1)
1272 return -1;
1273 } else if (diff_time > 60 * 60 * 2) {
1274 if (tp_writef(c->tp, "%lld %s",
1275 (diff_time / 60 / 60), hours) == -1)
1276 return -1;
1277 } else if (diff_time > 60 * 2) {
1278 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1279 minutes) == -1)
1280 return -1;
1281 } else if (diff_time > 2) {
1282 if (tp_writef(c->tp, "%lld %s", diff_time,
1283 seconds) == -1)
1284 return -1;
1285 } else {
1286 if (tp_writes(tp, now) == -1)
1287 return -1;
1289 return 0;