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 (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
796 break;
798 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
799 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
800 d_skipped++;
801 continue;
804 error = got_path_dirent_type(&type, srv->repos_path,
805 sd_dent[d_i]);
806 if (error)
807 continue;
808 if (type != DT_DIR) {
809 d_skipped++;
810 continue;
813 if (qs->index_page > 0 && (qs->index_page *
814 srv->max_repos_display) > t->prev_disp) {
815 t->prev_disp++;
816 continue;
819 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
820 if (error)
821 continue;
823 error = gotweb_load_got_path(c, repo_dir);
824 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
825 if (error->code != GOT_ERR_NOT_GIT_REPO)
826 log_warnx("%s: %s: %s", __func__,
827 sd_dent[d_i]->d_name, error->msg);
828 gotweb_free_repo_dir(repo_dir);
829 repo_dir = NULL;
830 d_skipped++;
831 continue;
834 d_disp++;
835 t->prev_disp++;
837 r = gotweb_render_repo_fragment(c->tp, repo_dir);
838 gotweb_free_repo_dir(repo_dir);
839 repo_dir = NULL;
840 got_repo_close(t->repo);
841 t->repo = NULL;
842 if (r == -1)
843 return -1;
845 t->next_disp++;
846 if (d_disp == srv->max_repos_display)
847 break;
849 t->repos_total = t->nrepos - d_skipped;
851 if (srv->max_repos_display == 0)
852 return 0;
853 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
854 return 0;
855 if (t->repos_total <= srv->max_repos ||
856 t->repos_total <= srv->max_repos_display)
857 return 0;
859 if (gotweb_render_navs(c->tp) == -1)
860 return -1;
862 return 0;
865 static inline int
866 should_urlencode(int c)
868 if (c <= ' ' || c >= 127)
869 return 1;
871 switch (c) {
872 /* gen-delim */
873 case ':':
874 case '/':
875 case '?':
876 case '#':
877 case '[':
878 case ']':
879 case '@':
880 /* sub-delims */
881 case '!':
882 case '$':
883 case '&':
884 case '\'':
885 case '(':
886 case ')':
887 case '*':
888 case '+':
889 case ',':
890 case ';':
891 case '=':
892 /* needed because the URLs are embedded into the HTML */
893 case '\"':
894 return 1;
895 default:
896 return 0;
900 static char *
901 gotweb_urlencode(const char *str)
903 const char *s;
904 char *escaped;
905 size_t i, len;
906 int a, b;
908 len = 0;
909 for (s = str; *s; ++s) {
910 len++;
911 if (should_urlencode(*s))
912 len += 2;
915 escaped = calloc(1, len + 1);
916 if (escaped == NULL)
917 return NULL;
919 i = 0;
920 for (s = str; *s; ++s) {
921 if (should_urlencode(*s)) {
922 a = (*s & 0xF0) >> 4;
923 b = (*s & 0x0F);
925 escaped[i++] = '%';
926 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
927 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
928 } else
929 escaped[i++] = *s;
932 return escaped;
935 const char *
936 gotweb_action_name(int action)
938 switch (action) {
939 case BLAME:
940 return "blame";
941 case BLOB:
942 return "blob";
943 case BLOBRAW:
944 return "blobraw";
945 case BRIEFS:
946 return "briefs";
947 case COMMITS:
948 return "commits";
949 case DIFF:
950 return "diff";
951 case ERR:
952 return "err";
953 case INDEX:
954 return "index";
955 case PATCH:
956 return "patch";
957 case SUMMARY:
958 return "summary";
959 case TAG:
960 return "tag";
961 case TAGS:
962 return "tags";
963 case TREE:
964 return "tree";
965 case RSS:
966 return "rss";
967 default:
968 return NULL;
972 int
973 gotweb_render_url(struct request *c, struct gotweb_url *url)
975 const char *sep = "?", *action;
976 char *tmp;
977 int r;
979 action = gotweb_action_name(url->action);
980 if (action != NULL) {
981 if (tp_writef(c->tp, "?action=%s", action) == -1)
982 return -1;
983 sep = "&";
986 if (url->commit) {
987 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
988 return -1;
989 sep = "&";
992 if (url->previd) {
993 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
994 return -1;
995 sep = "&";
998 if (url->prevset) {
999 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
1000 return -1;
1001 sep = "&";
1004 if (url->file) {
1005 tmp = gotweb_urlencode(url->file);
1006 if (tmp == NULL)
1007 return -1;
1008 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1009 free(tmp);
1010 if (r == -1)
1011 return -1;
1012 sep = "&";
1015 if (url->folder) {
1016 tmp = gotweb_urlencode(url->folder);
1017 if (tmp == NULL)
1018 return -1;
1019 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1020 free(tmp);
1021 if (r == -1)
1022 return -1;
1023 sep = "&";
1026 if (url->headref) {
1027 tmp = gotweb_urlencode(url->headref);
1028 if (tmp == NULL)
1029 return -1;
1030 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1031 free(tmp);
1032 if (r == -1)
1033 return -1;
1034 sep = "&";
1037 if (url->index_page != -1) {
1038 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1039 url->index_page) == -1)
1040 return -1;
1041 sep = "&";
1044 if (url->path) {
1045 tmp = gotweb_urlencode(url->path);
1046 if (tmp == NULL)
1047 return -1;
1048 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1049 free(tmp);
1050 if (r == -1)
1051 return -1;
1052 sep = "&";
1055 return 0;
1058 int
1059 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1061 struct template *tp = c->tp;
1062 const char *proto = c->https ? "https" : "http";
1064 if (tp_writes(tp, proto) == -1 ||
1065 tp_writes(tp, "://") == -1 ||
1066 tp_htmlescape(tp, c->server_name) == -1 ||
1067 tp_htmlescape(tp, c->document_uri) == -1)
1068 return -1;
1070 return gotweb_render_url(c, url);
1073 static const struct got_error *
1074 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1076 const struct got_error *error = NULL;
1077 struct socket *sock = c->sock;
1078 struct server *srv = c->srv;
1079 struct transport *t = c->t;
1080 DIR *dt;
1081 char *dir_test;
1083 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1084 GOTWEB_GIT_DIR) == -1)
1085 return got_error_from_errno("asprintf");
1087 dt = opendir(dir_test);
1088 if (dt == NULL) {
1089 free(dir_test);
1090 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1091 repo_dir->name) == -1)
1092 return got_error_from_errno("asprintf");
1093 dt = opendir(dir_test);
1094 if (dt == NULL) {
1095 free(dir_test);
1096 return got_error_path(repo_dir->name,
1097 GOT_ERR_NOT_GIT_REPO);
1101 repo_dir->path = dir_test;
1102 dir_test = NULL;
1104 if (srv->respect_exportok &&
1105 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1106 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1107 goto err;
1110 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1111 if (error)
1112 goto err;
1113 error = gotweb_get_repo_description(&repo_dir->description, srv,
1114 repo_dir->path, dirfd(dt));
1115 if (error)
1116 goto err;
1117 error = got_get_repo_owner(&repo_dir->owner, c);
1118 if (error)
1119 goto err;
1120 if (srv->show_repo_age) {
1121 error = got_get_repo_age(&repo_dir->age, c, NULL);
1122 if (error)
1123 goto err;
1125 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1126 dirfd(dt));
1127 err:
1128 free(dir_test);
1129 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1130 error = got_error_from_errno("closedir");
1131 if (error && t->repo) {
1132 got_repo_close(t->repo);
1133 t->repo = NULL;
1135 return error;
1138 static const struct got_error *
1139 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1141 const struct got_error *error;
1143 *repo_dir = calloc(1, sizeof(**repo_dir));
1144 if (*repo_dir == NULL)
1145 return got_error_from_errno("calloc");
1147 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1148 error = got_error_from_errno("asprintf");
1149 free(*repo_dir);
1150 *repo_dir = NULL;
1151 return error;
1153 (*repo_dir)->owner = NULL;
1154 (*repo_dir)->description = NULL;
1155 (*repo_dir)->url = NULL;
1156 (*repo_dir)->path = NULL;
1158 return NULL;
1161 static const struct got_error *
1162 gotweb_get_repo_description(char **description, struct server *srv,
1163 const char *dirpath, int dir)
1165 const struct got_error *error = NULL;
1166 struct stat sb;
1167 int fd = -1;
1168 off_t len;
1170 *description = NULL;
1171 if (srv->show_repo_description == 0)
1172 return NULL;
1174 fd = openat(dir, "description", O_RDONLY);
1175 if (fd == -1) {
1176 if (errno != ENOENT && errno != EACCES) {
1177 error = got_error_from_errno_fmt("openat %s/%s",
1178 dirpath, "description");
1180 goto done;
1183 if (fstat(fd, &sb) == -1) {
1184 error = got_error_from_errno_fmt("fstat %s/%s",
1185 dirpath, "description");
1186 goto done;
1189 len = sb.st_size;
1190 if (len > GOTWEBD_MAXDESCRSZ - 1)
1191 len = GOTWEBD_MAXDESCRSZ - 1;
1193 *description = calloc(len + 1, sizeof(**description));
1194 if (*description == NULL) {
1195 error = got_error_from_errno("calloc");
1196 goto done;
1199 if (read(fd, *description, len) == -1)
1200 error = got_error_from_errno("read");
1201 done:
1202 if (fd != -1 && close(fd) == -1 && error == NULL)
1203 error = got_error_from_errno("close");
1204 return error;
1207 static const struct got_error *
1208 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1209 int dir)
1211 const struct got_error *error = NULL;
1212 struct stat sb;
1213 int fd = -1;
1214 off_t len;
1216 *url = NULL;
1217 if (srv->show_repo_cloneurl == 0)
1218 return NULL;
1220 fd = openat(dir, "cloneurl", O_RDONLY);
1221 if (fd == -1) {
1222 if (errno != ENOENT && errno != EACCES) {
1223 error = got_error_from_errno_fmt("openat %s/%s",
1224 dirpath, "cloneurl");
1226 goto done;
1229 if (fstat(fd, &sb) == -1) {
1230 error = got_error_from_errno_fmt("fstat %s/%s",
1231 dirpath, "cloneurl");
1232 goto done;
1235 len = sb.st_size;
1236 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1237 len = GOTWEBD_MAXCLONEURLSZ - 1;
1239 *url = calloc(len + 1, sizeof(**url));
1240 if (*url == NULL) {
1241 error = got_error_from_errno("calloc");
1242 goto done;
1245 if (read(fd, *url, 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 int
1254 gotweb_render_age(struct template *tp, time_t committer_time)
1256 struct request *c = tp->tp_arg;
1257 long long diff_time;
1258 const char *years = "years ago", *months = "months ago";
1259 const char *weeks = "weeks ago", *days = "days ago";
1260 const char *hours = "hours ago", *minutes = "minutes ago";
1261 const char *seconds = "seconds ago", *now = "right now";
1263 diff_time = time(NULL) - committer_time;
1264 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1265 if (tp_writef(c->tp, "%lld %s",
1266 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1267 return -1;
1268 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1269 if (tp_writef(c->tp, "%lld %s",
1270 (diff_time / 60 / 60 / 24 / (365 / 12)),
1271 months) == -1)
1272 return -1;
1273 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1274 if (tp_writef(c->tp, "%lld %s",
1275 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1276 return -1;
1277 } else if (diff_time > 60 * 60 * 24 * 2) {
1278 if (tp_writef(c->tp, "%lld %s",
1279 (diff_time / 60 / 60 / 24), days) == -1)
1280 return -1;
1281 } else if (diff_time > 60 * 60 * 2) {
1282 if (tp_writef(c->tp, "%lld %s",
1283 (diff_time / 60 / 60), hours) == -1)
1284 return -1;
1285 } else if (diff_time > 60 * 2) {
1286 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1287 minutes) == -1)
1288 return -1;
1289 } else if (diff_time > 2) {
1290 if (tp_writef(c->tp, "%lld %s", diff_time,
1291 seconds) == -1)
1292 return -1;
1293 } else {
1294 if (tp_writes(tp, now) == -1)
1295 return -1;
1297 return 0;