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 "gotwebd.h"
53 #include "tmpl.h"
55 static const struct querystring_keys querystring_keys[] = {
56 { "action", ACTION },
57 { "commit", COMMIT },
58 { "file", RFILE },
59 { "folder", FOLDER },
60 { "headref", HEADREF },
61 { "index_page", INDEX_PAGE },
62 { "path", PATH },
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 { "patch", PATCH },
75 { "summary", SUMMARY },
76 { "tag", TAG },
77 { "tags", TAGS },
78 { "tree", TREE },
79 { "rss", RSS },
80 };
82 static const struct got_error *gotweb_init_querystring(struct querystring **);
83 static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 char *);
85 static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 char *, char *);
87 static int gotweb_render_index(struct template *);
88 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
89 const char *);
90 static const struct got_error *gotweb_load_got_path(struct request *c,
91 struct repo_dir *);
92 static const struct got_error *gotweb_get_repo_description(char **,
93 struct server *, const char *, int);
94 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
95 const char *, int);
97 static void gotweb_free_querystring(struct querystring *);
98 static void gotweb_free_repo_dir(struct repo_dir *);
100 struct server *gotweb_get_server(const char *);
102 static int
103 gotweb_reply(struct request *c, int status, const char *ctype,
104 struct gotweb_url *location)
106 const char *csp;
108 if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
109 return -1;
111 if (location) {
112 if (tp_writes(c->tp, "Location: ") == -1 ||
113 gotweb_render_url(c, location) == -1 ||
114 tp_writes(c->tp, "\r\n") == -1)
115 return -1;
118 csp = "Content-Security-Policy: default-src 'self'; "
119 "script-src 'none'; object-src 'none';\r\n";
120 if (tp_writes(c->tp, csp) == -1)
121 return -1;
123 if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
124 return -1;
126 return tp_writes(c->tp, "\r\n");
129 static int
130 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
131 const char *suffix)
133 int r;
135 r = tp_writef(c->tp, "Content-Disposition: attachment; "
136 "filename=%s%s\r\n", file, suffix ? suffix : "");
137 if (r == -1)
138 return -1;
139 return gotweb_reply(c, 200, ctype, NULL);
142 void
143 gotweb_process_request(struct request *c)
145 const struct got_error *error = NULL;
146 struct server *srv = NULL;
147 struct querystring *qs = NULL;
148 struct repo_dir *repo_dir = NULL;
149 const char *rss_ctype = "application/rss+xml;charset=utf-8";
150 const uint8_t *buf;
151 size_t len;
152 int r, binary = 0;
154 /* init the transport */
155 error = gotweb_init_transport(&c->t);
156 if (error) {
157 log_warnx("%s: %s", __func__, error->msg);
158 return;
160 /* don't process any further if client disconnected */
161 if (c->sock->client_status == CLIENT_DISCONNECT)
162 return;
163 /* get the gotwebd server */
164 srv = gotweb_get_server(c->server_name);
165 if (srv == NULL) {
166 log_warnx("%s: error server is NULL", __func__);
167 goto err;
169 c->srv = srv;
170 /* parse our querystring */
171 error = gotweb_init_querystring(&qs);
172 if (error) {
173 log_warnx("%s: %s", __func__, error->msg);
174 goto err;
176 c->t->qs = qs;
177 error = gotweb_parse_querystring(&qs, c->querystring);
178 if (error) {
179 log_warnx("%s: %s", __func__, error->msg);
180 goto err;
183 /*
184 * certain actions require a commit id in the querystring. this stops
185 * bad actors from exploiting this by manually manipulating the
186 * querystring.
187 */
189 if (qs->action == BLAME || qs->action == BLOB ||
190 qs->action == BLOBRAW || qs->action == DIFF ||
191 qs->action == PATCH) {
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, qs->folder, qs->file, qs->commit);
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 .action = BLOBRAW,
235 .path = qs->path,
236 .commit = qs->commit,
237 .folder = qs->folder,
238 .file = qs->file,
239 };
241 gotweb_reply(c, 302, NULL, &url);
242 return;
245 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
246 return;
247 gotweb_render_page(c->tp, gotweb_render_blob);
248 return;
249 case BLOBRAW:
250 if (binary)
251 r = gotweb_reply_file(c, "application/octet-stream",
252 qs->file, NULL);
253 else
254 r = gotweb_reply(c, 200, "text/plain", NULL);
255 if (r == -1)
256 return;
257 if (template_flush(c->tp) == -1)
258 return;
260 for (;;) {
261 error = got_object_blob_read_block(&len, c->t->blob);
262 if (error)
263 break;
264 if (len == 0)
265 break;
266 buf = got_object_blob_get_read_buf(c->t->blob);
267 if (fcgi_write(c, buf, len) == -1)
268 break;
270 return;
271 case BRIEFS:
272 error = got_get_repo_commits(c, srv->max_commits_display);
273 if (error)
274 goto err;
275 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
276 return;
277 gotweb_render_page(c->tp, gotweb_render_briefs);
278 return;
279 case COMMITS:
280 error = got_get_repo_commits(c, srv->max_commits_display);
281 if (error) {
282 log_warnx("%s: %s", __func__, error->msg);
283 goto err;
285 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
286 return;
287 gotweb_render_page(c->tp, gotweb_render_commits);
288 return;
289 case DIFF:
290 error = got_get_repo_commits(c, 1);
291 if (error) {
292 log_warnx("%s: %s", __func__, error->msg);
293 goto err;
295 error = got_open_diff_for_output(&c->t->fp, c);
296 if (error) {
297 log_warnx("%s: %s", __func__, error->msg);
298 goto err;
300 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
301 return;
302 gotweb_render_page(c->tp, gotweb_render_diff);
303 return;
304 case INDEX:
305 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
306 alphasort);
307 if (c->t->nrepos == -1) {
308 c->t->repos = NULL;
309 error = got_error_from_errno2("scandir",
310 srv->repos_path);
311 goto err;
313 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
314 return;
315 gotweb_render_page(c->tp, gotweb_render_index);
316 return;
317 case PATCH:
318 error = got_get_repo_commits(c, 1);
319 if (error) {
320 log_warnx("%s: %s", __func__, error->msg);
321 goto err;
323 error = got_open_diff_for_output(&c->t->fp, c);
324 if (error) {
325 log_warnx("%s: %s", __func__, error->msg);
326 goto err;
328 if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
329 return;
330 gotweb_render_patch(c->tp);
331 return;
332 case RSS:
333 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
334 if (error)
335 goto err;
336 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
337 == -1)
338 return;
339 gotweb_render_rss(c->tp);
340 return;
341 case SUMMARY:
342 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
343 got_ref_cmp_by_name, NULL);
344 if (error) {
345 log_warnx("%s: got_ref_list: %s", __func__,
346 error->msg);
347 goto err;
349 error = got_get_repo_commits(c, srv->summary_commits_display);
350 if (error)
351 goto err;
352 qs->action = TAGS;
353 error = got_get_repo_tags(c, srv->summary_tags_display);
354 if (error) {
355 log_warnx("%s: got_get_repo_tags: %s", __func__,
356 error->msg);
357 goto err;
359 qs->action = SUMMARY;
360 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
361 return;
362 gotweb_render_page(c->tp, gotweb_render_summary);
363 return;
364 case TAG:
365 error = got_get_repo_tags(c, 1);
366 if (error) {
367 log_warnx("%s: %s", __func__, error->msg);
368 goto err;
370 if (c->t->tag_count == 0) {
371 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
372 "bad commit id");
373 goto err;
375 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
376 return;
377 gotweb_render_page(c->tp, gotweb_render_tag);
378 return;
379 case TAGS:
380 error = got_get_repo_tags(c, srv->max_commits_display);
381 if (error) {
382 log_warnx("%s: %s", __func__, error->msg);
383 goto err;
385 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
386 return;
387 gotweb_render_page(c->tp, gotweb_render_tags);
388 return;
389 case TREE:
390 error = got_get_repo_commits(c, 1);
391 if (error) {
392 log_warnx("%s: %s", __func__, error->msg);
393 goto err;
395 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
396 return;
397 gotweb_render_page(c->tp, gotweb_render_tree);
398 return;
399 case ERR:
400 default:
401 error = got_error(GOT_ERR_BAD_QUERYSTRING);
404 err:
405 c->t->error = error;
406 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
407 return;
408 gotweb_render_page(c->tp, gotweb_render_error);
411 struct server *
412 gotweb_get_server(const char *server_name)
414 struct server *srv;
416 /* check against the server name first */
417 if (*server_name != '\0')
418 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
419 if (strcmp(srv->name, server_name) == 0)
420 return srv;
422 /* otherwise, use the first server */
423 return TAILQ_FIRST(&gotwebd_env->servers);
424 };
426 const struct got_error *
427 gotweb_init_transport(struct transport **t)
429 const struct got_error *error = NULL;
431 *t = calloc(1, sizeof(**t));
432 if (*t == NULL)
433 return got_error_from_errno2(__func__, "calloc");
435 TAILQ_INIT(&(*t)->repo_commits);
436 TAILQ_INIT(&(*t)->repo_tags);
437 TAILQ_INIT(&(*t)->refs);
439 (*t)->fd = -1;
441 return error;
444 static const struct got_error *
445 gotweb_init_querystring(struct querystring **qs)
447 const struct got_error *error = NULL;
449 *qs = calloc(1, sizeof(**qs));
450 if (*qs == NULL)
451 return got_error_from_errno2(__func__, "calloc");
453 (*qs)->headref = strdup("HEAD");
454 if ((*qs)->headref == NULL) {
455 free(*qs);
456 *qs = NULL;
457 return got_error_from_errno2(__func__, "strdup");
460 (*qs)->action = INDEX;
462 return error;
465 static const struct got_error *
466 gotweb_parse_querystring(struct querystring **qs, char *qst)
468 const struct got_error *error = NULL;
469 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
470 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
472 if (qst == NULL)
473 return error;
475 tok1 = strdup(qst);
476 if (tok1 == NULL)
477 return got_error_from_errno2(__func__, "strdup");
479 tok1_pair = tok1;
480 tok1_end = tok1;
482 while (tok1_pair != NULL) {
483 strsep(&tok1_end, "&");
485 tok2 = strdup(tok1_pair);
486 if (tok2 == NULL) {
487 free(tok1);
488 return got_error_from_errno2(__func__, "strdup");
491 tok2_pair = tok2;
492 tok2_end = tok2;
494 while (tok2_pair != NULL) {
495 strsep(&tok2_end, "=");
496 if (tok2_end) {
497 error = gotweb_assign_querystring(qs, tok2_pair,
498 tok2_end);
499 if (error)
500 goto err;
502 tok2_pair = tok2_end;
504 free(tok2);
505 tok1_pair = tok1_end;
507 free(tok1);
508 return error;
509 err:
510 free(tok2);
511 free(tok1);
512 return error;
515 /*
516 * Adapted from usr.sbin/httpd/httpd.c url_decode.
517 */
518 static const struct got_error *
519 gotweb_urldecode(char *url)
521 char *p, *q;
522 char hex[3];
523 unsigned long x;
525 hex[2] = '\0';
526 p = q = url;
528 while (*p != '\0') {
529 switch (*p) {
530 case '%':
531 /* Encoding character is followed by two hex chars */
532 if (!isxdigit((unsigned char)p[1]) ||
533 !isxdigit((unsigned char)p[2]) ||
534 (p[1] == '0' && p[2] == '0'))
535 return got_error(GOT_ERR_BAD_QUERYSTRING);
537 hex[0] = p[1];
538 hex[1] = p[2];
540 /*
541 * We don't have to validate "hex" because it is
542 * guaranteed to include two hex chars followed by nul.
543 */
544 x = strtoul(hex, NULL, 16);
545 *q = (char)x;
546 p += 2;
547 break;
548 default:
549 *q = *p;
550 break;
552 p++;
553 q++;
555 *q = '\0';
557 return NULL;
560 static const struct got_error *
561 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
563 const struct got_error *error = NULL;
564 const char *errstr;
565 int a_cnt, el_cnt;
567 error = gotweb_urldecode(value);
568 if (error)
569 return error;
571 for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
572 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
573 continue;
575 switch (querystring_keys[el_cnt].element) {
576 case ACTION:
577 for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
578 if (strcmp(value, action_keys[a_cnt].name) != 0)
579 continue;
580 else if (strcmp(value,
581 action_keys[a_cnt].name) == 0){
582 (*qs)->action =
583 action_keys[a_cnt].action;
584 goto qa_found;
587 (*qs)->action = ERR;
588 qa_found:
589 break;
590 case COMMIT:
591 (*qs)->commit = strdup(value);
592 if ((*qs)->commit == NULL) {
593 error = got_error_from_errno2(__func__,
594 "strdup");
595 goto done;
597 break;
598 case RFILE:
599 (*qs)->file = strdup(value);
600 if ((*qs)->file == NULL) {
601 error = got_error_from_errno2(__func__,
602 "strdup");
603 goto done;
605 break;
606 case FOLDER:
607 (*qs)->folder = strdup(value);
608 if ((*qs)->folder == NULL) {
609 error = got_error_from_errno2(__func__,
610 "strdup");
611 goto done;
613 break;
614 case HEADREF:
615 free((*qs)->headref);
616 (*qs)->headref = strdup(value);
617 if ((*qs)->headref == NULL) {
618 error = got_error_from_errno2(__func__,
619 "strdup");
620 goto done;
622 break;
623 case INDEX_PAGE:
624 if (*value == '\0')
625 break;
626 (*qs)->index_page = strtonum(value, INT64_MIN,
627 INT64_MAX, &errstr);
628 if (errstr) {
629 error = got_error_from_errno3(__func__,
630 "strtonum", errstr);
631 goto done;
633 if ((*qs)->index_page < 0)
634 (*qs)->index_page = 0;
635 break;
636 case PATH:
637 (*qs)->path = strdup(value);
638 if ((*qs)->path == NULL) {
639 error = got_error_from_errno2(__func__,
640 "strdup");
641 goto done;
643 break;
646 /* entry found */
647 break;
649 done:
650 return error;
653 void
654 gotweb_free_repo_tag(struct repo_tag *rt)
656 if (rt != NULL) {
657 free(rt->commit_id);
658 free(rt->tag_name);
659 free(rt->tag_commit);
660 free(rt->commit_msg);
661 free(rt->tagger);
663 free(rt);
666 void
667 gotweb_free_repo_commit(struct repo_commit *rc)
669 if (rc != NULL) {
670 free(rc->path);
671 free(rc->refs_str);
672 free(rc->commit_id);
673 free(rc->parent_id);
674 free(rc->tree_id);
675 free(rc->author);
676 free(rc->committer);
677 free(rc->commit_msg);
679 free(rc);
682 static void
683 gotweb_free_querystring(struct querystring *qs)
685 if (qs != NULL) {
686 free(qs->commit);
687 free(qs->file);
688 free(qs->folder);
689 free(qs->headref);
690 free(qs->path);
692 free(qs);
695 static void
696 gotweb_free_repo_dir(struct repo_dir *repo_dir)
698 if (repo_dir != NULL) {
699 free(repo_dir->name);
700 free(repo_dir->owner);
701 free(repo_dir->description);
702 free(repo_dir->url);
703 free(repo_dir->path);
705 free(repo_dir);
708 void
709 gotweb_free_transport(struct transport *t)
711 const struct got_error *err;
712 struct repo_commit *rc = NULL, *trc = NULL;
713 struct repo_tag *rt = NULL, *trt = NULL;
714 int i;
716 got_ref_list_free(&t->refs);
717 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
718 TAILQ_REMOVE(&t->repo_commits, rc, entry);
719 gotweb_free_repo_commit(rc);
721 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
722 TAILQ_REMOVE(&t->repo_tags, rt, entry);
723 gotweb_free_repo_tag(rt);
725 gotweb_free_repo_dir(t->repo_dir);
726 gotweb_free_querystring(t->qs);
727 free(t->more_id);
728 free(t->tags_more_id);
729 if (t->blob)
730 got_object_blob_close(t->blob);
731 if (t->fp) {
732 err = got_gotweb_closefile(t->fp);
733 if (err)
734 log_warnx("%s: got_gotweb_closefile failure: %s",
735 __func__, err->msg);
737 if (t->fd != -1 && close(t->fd) == -1)
738 log_warn("%s: close", __func__);
739 if (t->repos) {
740 for (i = 0; i < t->nrepos; ++i)
741 free(t->repos[i]);
742 free(t->repos);
744 if (t->repo)
745 got_repo_close(t->repo);
746 free(t);
749 void
750 gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
751 struct gotweb_url *next, int *have_next)
753 struct transport *t = c->t;
754 struct querystring *qs = t->qs;
755 struct server *srv = c->srv;
757 *have_prev = *have_next = 0;
759 if (qs->index_page > 0) {
760 *have_prev = 1;
761 *prev = (struct gotweb_url){
762 .action = -1,
763 .index_page = qs->index_page - 1,
764 };
766 if (t->next_disp == srv->max_repos_display &&
767 t->repos_total != (qs->index_page + 1) *
768 srv->max_repos_display) {
769 *have_next = 1;
770 *next = (struct gotweb_url){
771 .action = -1,
772 .index_page = qs->index_page + 1,
773 };
777 static int
778 gotweb_render_index(struct template *tp)
780 const struct got_error *error = NULL;
781 struct request *c = tp->tp_arg;
782 struct server *srv = c->srv;
783 struct transport *t = c->t;
784 struct querystring *qs = t->qs;
785 struct repo_dir *repo_dir = NULL;
786 struct dirent **sd_dent = t->repos;
787 unsigned int d_i, d_disp = 0;
788 unsigned int d_skipped = 0;
789 int type, r;
791 if (gotweb_render_repo_table_hdr(c->tp) == -1)
792 return -1;
794 for (d_i = 0; d_i < t->nrepos; d_i++) {
795 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
796 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
797 d_skipped++;
798 continue;
801 error = got_path_dirent_type(&type, srv->repos_path,
802 sd_dent[d_i]);
803 if (error)
804 continue;
805 if (type != DT_DIR) {
806 d_skipped++;
807 continue;
810 if (qs->index_page > 0 && (qs->index_page *
811 srv->max_repos_display) > t->prev_disp) {
812 t->prev_disp++;
813 continue;
816 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
817 if (error)
818 continue;
820 error = gotweb_load_got_path(c, repo_dir);
821 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
822 if (error->code != GOT_ERR_NOT_GIT_REPO)
823 log_warnx("%s: %s: %s", __func__,
824 sd_dent[d_i]->d_name, error->msg);
825 gotweb_free_repo_dir(repo_dir);
826 repo_dir = NULL;
827 d_skipped++;
828 continue;
831 d_disp++;
832 t->prev_disp++;
834 r = gotweb_render_repo_fragment(c->tp, repo_dir);
835 gotweb_free_repo_dir(repo_dir);
836 repo_dir = NULL;
837 got_repo_close(t->repo);
838 t->repo = NULL;
839 if (r == -1)
840 return -1;
842 t->next_disp++;
843 if (d_disp == srv->max_repos_display)
844 break;
846 t->repos_total = t->nrepos - d_skipped;
848 if (srv->max_repos_display == 0 ||
849 t->repos_total <= srv->max_repos_display)
850 return 0;
852 if (gotweb_render_navs(c->tp) == -1)
853 return -1;
855 return 0;
858 static inline int
859 should_urlencode(int c)
861 if (c <= ' ' || c >= 127)
862 return 1;
864 switch (c) {
865 /* gen-delim */
866 case ':':
867 case '/':
868 case '?':
869 case '#':
870 case '[':
871 case ']':
872 case '@':
873 /* sub-delims */
874 case '!':
875 case '$':
876 case '&':
877 case '\'':
878 case '(':
879 case ')':
880 case '*':
881 case '+':
882 case ',':
883 case ';':
884 case '=':
885 /* needed because the URLs are embedded into the HTML */
886 case '\"':
887 return 1;
888 default:
889 return 0;
893 static char *
894 gotweb_urlencode(const char *str)
896 const char *s;
897 char *escaped;
898 size_t i, len;
899 int a, b;
901 len = 0;
902 for (s = str; *s; ++s) {
903 len++;
904 if (should_urlencode(*s))
905 len += 2;
908 escaped = calloc(1, len + 1);
909 if (escaped == NULL)
910 return NULL;
912 i = 0;
913 for (s = str; *s; ++s) {
914 if (should_urlencode(*s)) {
915 a = (*s & 0xF0) >> 4;
916 b = (*s & 0x0F);
918 escaped[i++] = '%';
919 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
920 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
921 } else
922 escaped[i++] = *s;
925 return escaped;
928 const char *
929 gotweb_action_name(int action)
931 switch (action) {
932 case BLAME:
933 return "blame";
934 case BLOB:
935 return "blob";
936 case BLOBRAW:
937 return "blobraw";
938 case BRIEFS:
939 return "briefs";
940 case COMMITS:
941 return "commits";
942 case DIFF:
943 return "diff";
944 case ERR:
945 return "err";
946 case INDEX:
947 return "index";
948 case PATCH:
949 return "patch";
950 case SUMMARY:
951 return "summary";
952 case TAG:
953 return "tag";
954 case TAGS:
955 return "tags";
956 case TREE:
957 return "tree";
958 case RSS:
959 return "rss";
960 default:
961 return NULL;
965 int
966 gotweb_render_url(struct request *c, struct gotweb_url *url)
968 const char *sep = "?", *action;
969 char *tmp;
970 int r;
972 action = gotweb_action_name(url->action);
973 if (action != NULL) {
974 if (tp_writef(c->tp, "?action=%s", action) == -1)
975 return -1;
976 sep = "&";
979 if (url->commit) {
980 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
981 return -1;
982 sep = "&";
985 if (url->previd) {
986 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
987 return -1;
988 sep = "&";
991 if (url->prevset) {
992 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
993 return -1;
994 sep = "&";
997 if (url->file) {
998 tmp = gotweb_urlencode(url->file);
999 if (tmp == NULL)
1000 return -1;
1001 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1002 free(tmp);
1003 if (r == -1)
1004 return -1;
1005 sep = "&";
1008 if (url->folder) {
1009 tmp = gotweb_urlencode(url->folder);
1010 if (tmp == NULL)
1011 return -1;
1012 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1013 free(tmp);
1014 if (r == -1)
1015 return -1;
1016 sep = "&";
1019 if (url->headref) {
1020 tmp = gotweb_urlencode(url->headref);
1021 if (tmp == NULL)
1022 return -1;
1023 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1024 free(tmp);
1025 if (r == -1)
1026 return -1;
1027 sep = "&";
1030 if (url->index_page != -1) {
1031 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1032 url->index_page) == -1)
1033 return -1;
1034 sep = "&";
1037 if (url->path) {
1038 tmp = gotweb_urlencode(url->path);
1039 if (tmp == NULL)
1040 return -1;
1041 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1042 free(tmp);
1043 if (r == -1)
1044 return -1;
1045 sep = "&";
1048 return 0;
1051 int
1052 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1054 struct template *tp = c->tp;
1055 const char *proto = c->https ? "https" : "http";
1057 if (tp_writes(tp, proto) == -1 ||
1058 tp_writes(tp, "://") == -1 ||
1059 tp_htmlescape(tp, c->server_name) == -1 ||
1060 tp_htmlescape(tp, c->document_uri) == -1)
1061 return -1;
1063 return gotweb_render_url(c, url);
1066 static const struct got_error *
1067 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1069 const struct got_error *error = NULL;
1070 struct socket *sock = c->sock;
1071 struct server *srv = c->srv;
1072 struct transport *t = c->t;
1073 DIR *dt;
1074 char *dir_test;
1076 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1077 GOTWEB_GIT_DIR) == -1)
1078 return got_error_from_errno("asprintf");
1080 dt = opendir(dir_test);
1081 if (dt == NULL) {
1082 free(dir_test);
1083 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1084 repo_dir->name) == -1)
1085 return got_error_from_errno("asprintf");
1086 dt = opendir(dir_test);
1087 if (dt == NULL) {
1088 free(dir_test);
1089 return got_error_path(repo_dir->name,
1090 GOT_ERR_NOT_GIT_REPO);
1094 repo_dir->path = dir_test;
1095 dir_test = NULL;
1097 if (srv->respect_exportok &&
1098 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1099 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1100 goto err;
1103 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1104 if (error)
1105 goto err;
1106 error = gotweb_get_repo_description(&repo_dir->description, srv,
1107 repo_dir->path, dirfd(dt));
1108 if (error)
1109 goto err;
1110 error = got_get_repo_owner(&repo_dir->owner, c);
1111 if (error)
1112 goto err;
1113 if (srv->show_repo_age) {
1114 error = got_get_repo_age(&repo_dir->age, c, NULL);
1115 if (error)
1116 goto err;
1118 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1119 dirfd(dt));
1120 err:
1121 free(dir_test);
1122 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1123 error = got_error_from_errno("closedir");
1124 if (error && t->repo) {
1125 got_repo_close(t->repo);
1126 t->repo = NULL;
1128 return error;
1131 static const struct got_error *
1132 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1134 const struct got_error *error;
1136 *repo_dir = calloc(1, sizeof(**repo_dir));
1137 if (*repo_dir == NULL)
1138 return got_error_from_errno("calloc");
1140 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1141 error = got_error_from_errno("asprintf");
1142 free(*repo_dir);
1143 *repo_dir = NULL;
1144 return error;
1146 (*repo_dir)->owner = NULL;
1147 (*repo_dir)->description = NULL;
1148 (*repo_dir)->url = NULL;
1149 (*repo_dir)->path = NULL;
1151 return NULL;
1154 static const struct got_error *
1155 gotweb_get_repo_description(char **description, struct server *srv,
1156 const char *dirpath, int dir)
1158 const struct got_error *error = NULL;
1159 struct stat sb;
1160 int fd = -1;
1161 off_t len;
1163 *description = NULL;
1164 if (srv->show_repo_description == 0)
1165 return NULL;
1167 fd = openat(dir, "description", O_RDONLY);
1168 if (fd == -1) {
1169 if (errno != ENOENT && errno != EACCES) {
1170 error = got_error_from_errno_fmt("openat %s/%s",
1171 dirpath, "description");
1173 goto done;
1176 if (fstat(fd, &sb) == -1) {
1177 error = got_error_from_errno_fmt("fstat %s/%s",
1178 dirpath, "description");
1179 goto done;
1182 len = sb.st_size;
1183 if (len > GOTWEBD_MAXDESCRSZ - 1)
1184 len = GOTWEBD_MAXDESCRSZ - 1;
1186 *description = calloc(len + 1, sizeof(**description));
1187 if (*description == NULL) {
1188 error = got_error_from_errno("calloc");
1189 goto done;
1192 if (read(fd, *description, len) == -1)
1193 error = got_error_from_errno("read");
1194 done:
1195 if (fd != -1 && close(fd) == -1 && error == NULL)
1196 error = got_error_from_errno("close");
1197 return error;
1200 static const struct got_error *
1201 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1202 int dir)
1204 const struct got_error *error = NULL;
1205 struct stat sb;
1206 int fd = -1;
1207 off_t len;
1209 *url = NULL;
1210 if (srv->show_repo_cloneurl == 0)
1211 return NULL;
1213 fd = openat(dir, "cloneurl", O_RDONLY);
1214 if (fd == -1) {
1215 if (errno != ENOENT && errno != EACCES) {
1216 error = got_error_from_errno_fmt("openat %s/%s",
1217 dirpath, "cloneurl");
1219 goto done;
1222 if (fstat(fd, &sb) == -1) {
1223 error = got_error_from_errno_fmt("fstat %s/%s",
1224 dirpath, "cloneurl");
1225 goto done;
1228 len = sb.st_size;
1229 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1230 len = GOTWEBD_MAXCLONEURLSZ - 1;
1232 *url = calloc(len + 1, sizeof(**url));
1233 if (*url == NULL) {
1234 error = got_error_from_errno("calloc");
1235 goto done;
1238 if (read(fd, *url, len) == -1)
1239 error = got_error_from_errno("read");
1240 done:
1241 if (fd != -1 && close(fd) == -1 && error == NULL)
1242 error = got_error_from_errno("close");
1243 return error;
1246 int
1247 gotweb_render_age(struct template *tp, time_t committer_time)
1249 struct request *c = tp->tp_arg;
1250 long long diff_time;
1251 const char *years = "years ago", *months = "months ago";
1252 const char *weeks = "weeks ago", *days = "days ago";
1253 const char *hours = "hours ago", *minutes = "minutes ago";
1254 const char *seconds = "seconds ago", *now = "right now";
1256 diff_time = time(NULL) - committer_time;
1257 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1258 if (tp_writef(c->tp, "%lld %s",
1259 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1260 return -1;
1261 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1262 if (tp_writef(c->tp, "%lld %s",
1263 (diff_time / 60 / 60 / 24 / (365 / 12)),
1264 months) == -1)
1265 return -1;
1266 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1267 if (tp_writef(c->tp, "%lld %s",
1268 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1269 return -1;
1270 } else if (diff_time > 60 * 60 * 24 * 2) {
1271 if (tp_writef(c->tp, "%lld %s",
1272 (diff_time / 60 / 60 / 24), days) == -1)
1273 return -1;
1274 } else if (diff_time > 60 * 60 * 2) {
1275 if (tp_writef(c->tp, "%lld %s",
1276 (diff_time / 60 / 60), hours) == -1)
1277 return -1;
1278 } else if (diff_time > 60 * 2) {
1279 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1280 minutes) == -1)
1281 return -1;
1282 } else if (diff_time > 2) {
1283 if (tp_writef(c->tp, "%lld %s", diff_time,
1284 seconds) == -1)
1285 return -1;
1286 } else {
1287 if (tp_writes(tp, now) == -1)
1288 return -1;
1290 return 0;