Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20 #include "got_compat.h"
22 #include <net/if.h>
23 #include <netinet/in.h>
24 #include <sys/queue.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <event.h>
32 #include <fcntl.h>
33 #include <imsg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "gotwebd.h"
52 #include "tmpl.h"
54 static const struct querystring_keys querystring_keys[] = {
55 { "action", ACTION },
56 { "commit", COMMIT },
57 { "file", RFILE },
58 { "folder", FOLDER },
59 { "headref", HEADREF },
60 { "index_page", INDEX_PAGE },
61 { "path", PATH },
62 { "page", PAGE },
63 };
65 static const struct action_keys action_keys[] = {
66 { "blame", BLAME },
67 { "blob", BLOB },
68 { "blobraw", BLOBRAW },
69 { "briefs", BRIEFS },
70 { "commits", COMMITS },
71 { "diff", DIFF },
72 { "error", ERR },
73 { "index", INDEX },
74 { "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 .page = -1,
235 .action = BLOBRAW,
236 .path = qs->path,
237 .commit = qs->commit,
238 .folder = qs->folder,
239 .file = qs->file,
240 };
242 gotweb_reply(c, 302, NULL, &url);
243 return;
246 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
247 return;
248 gotweb_render_page(c->tp, gotweb_render_blob);
249 return;
250 case BLOBRAW:
251 if (binary)
252 r = gotweb_reply_file(c, "application/octet-stream",
253 qs->file, NULL);
254 else
255 r = gotweb_reply(c, 200, "text/plain", NULL);
256 if (r == -1)
257 return;
258 if (template_flush(c->tp) == -1)
259 return;
261 for (;;) {
262 error = got_object_blob_read_block(&len, c->t->blob);
263 if (error)
264 break;
265 if (len == 0)
266 break;
267 buf = got_object_blob_get_read_buf(c->t->blob);
268 if (fcgi_write(c, buf, len) == -1)
269 break;
271 return;
272 case BRIEFS:
273 error = got_get_repo_commits(c, srv->max_commits_display);
274 if (error)
275 goto err;
276 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
277 return;
278 gotweb_render_page(c->tp, gotweb_render_briefs);
279 return;
280 case COMMITS:
281 error = got_get_repo_commits(c, srv->max_commits_display);
282 if (error) {
283 log_warnx("%s: %s", __func__, error->msg);
284 goto err;
286 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
287 return;
288 gotweb_render_page(c->tp, gotweb_render_commits);
289 return;
290 case DIFF:
291 error = got_get_repo_commits(c, 1);
292 if (error) {
293 log_warnx("%s: %s", __func__, error->msg);
294 goto err;
296 error = got_open_diff_for_output(&c->t->fp, c);
297 if (error) {
298 log_warnx("%s: %s", __func__, error->msg);
299 goto err;
301 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
302 return;
303 gotweb_render_page(c->tp, gotweb_render_diff);
304 return;
305 case INDEX:
306 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
307 alphasort);
308 if (c->t->nrepos == -1) {
309 c->t->repos = NULL;
310 error = got_error_from_errno2("scandir",
311 srv->repos_path);
312 goto err;
314 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
315 return;
316 gotweb_render_page(c->tp, gotweb_render_index);
317 return;
318 case PATCH:
319 error = got_get_repo_commits(c, 1);
320 if (error) {
321 log_warnx("%s: %s", __func__, error->msg);
322 goto err;
324 error = got_open_diff_for_output(&c->t->fp, c);
325 if (error) {
326 log_warnx("%s: %s", __func__, error->msg);
327 goto err;
329 if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
330 return;
331 gotweb_render_patch(c->tp);
332 return;
333 case RSS:
334 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
335 if (error)
336 goto err;
337 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
338 == -1)
339 return;
340 gotweb_render_rss(c->tp);
341 return;
342 case SUMMARY:
343 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
344 got_ref_cmp_by_name, NULL);
345 if (error) {
346 log_warnx("%s: got_ref_list: %s", __func__,
347 error->msg);
348 goto err;
350 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
351 if (error)
352 goto err;
353 qs->action = TAGS;
354 error = got_get_repo_tags(c, D_MAXSLTAGDISP);
355 if (error) {
356 log_warnx("%s: got_get_repo_tags: %s", __func__,
357 error->msg);
358 goto err;
360 qs->action = SUMMARY;
361 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
362 return;
363 gotweb_render_page(c->tp, gotweb_render_summary);
364 return;
365 case TAG:
366 error = got_get_repo_tags(c, 1);
367 if (error) {
368 log_warnx("%s: %s", __func__, error->msg);
369 goto err;
371 if (c->t->tag_count == 0) {
372 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
373 "bad commit id");
374 goto err;
376 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
377 return;
378 gotweb_render_page(c->tp, gotweb_render_tag);
379 return;
380 case TAGS:
381 error = got_get_repo_tags(c, srv->max_commits_display);
382 if (error) {
383 log_warnx("%s: %s", __func__, error->msg);
384 goto err;
386 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
387 return;
388 gotweb_render_page(c->tp, gotweb_render_tags);
389 return;
390 case TREE:
391 error = got_get_repo_commits(c, 1);
392 if (error) {
393 log_warnx("%s: %s", __func__, error->msg);
394 goto err;
396 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
397 return;
398 gotweb_render_page(c->tp, gotweb_render_tree);
399 return;
400 case ERR:
401 default:
402 error = got_error(GOT_ERR_BAD_QUERYSTRING);
405 err:
406 c->t->error = error;
407 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
408 return;
409 gotweb_render_page(c->tp, gotweb_render_error);
412 struct server *
413 gotweb_get_server(const char *server_name)
415 struct server *srv;
417 /* check against the server name first */
418 if (*server_name != '\0')
419 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
420 if (strcmp(srv->name, server_name) == 0)
421 return srv;
423 /* otherwise, use the first server */
424 return TAILQ_FIRST(&gotwebd_env->servers);
425 };
427 const struct got_error *
428 gotweb_init_transport(struct transport **t)
430 const struct got_error *error = NULL;
432 *t = calloc(1, sizeof(**t));
433 if (*t == NULL)
434 return got_error_from_errno2(__func__, "calloc");
436 TAILQ_INIT(&(*t)->repo_commits);
437 TAILQ_INIT(&(*t)->repo_tags);
438 TAILQ_INIT(&(*t)->refs);
440 (*t)->fd = -1;
442 return error;
445 static const struct got_error *
446 gotweb_init_querystring(struct querystring **qs)
448 const struct got_error *error = NULL;
450 *qs = calloc(1, sizeof(**qs));
451 if (*qs == NULL)
452 return got_error_from_errno2(__func__, "calloc");
454 (*qs)->headref = strdup("HEAD");
455 if ((*qs)->headref == NULL) {
456 free(*qs);
457 *qs = NULL;
458 return got_error_from_errno2(__func__, "strdup");
461 (*qs)->action = INDEX;
463 return error;
466 static const struct got_error *
467 gotweb_parse_querystring(struct querystring **qs, char *qst)
469 const struct got_error *error = NULL;
470 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
471 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
473 if (qst == NULL)
474 return error;
476 tok1 = strdup(qst);
477 if (tok1 == NULL)
478 return got_error_from_errno2(__func__, "strdup");
480 tok1_pair = tok1;
481 tok1_end = tok1;
483 while (tok1_pair != NULL) {
484 strsep(&tok1_end, "&");
486 tok2 = strdup(tok1_pair);
487 if (tok2 == NULL) {
488 free(tok1);
489 return got_error_from_errno2(__func__, "strdup");
492 tok2_pair = tok2;
493 tok2_end = tok2;
495 while (tok2_pair != NULL) {
496 strsep(&tok2_end, "=");
497 if (tok2_end) {
498 error = gotweb_assign_querystring(qs, tok2_pair,
499 tok2_end);
500 if (error)
501 goto err;
503 tok2_pair = tok2_end;
505 free(tok2);
506 tok1_pair = tok1_end;
508 free(tok1);
509 return error;
510 err:
511 free(tok2);
512 free(tok1);
513 return error;
516 /*
517 * Adapted from usr.sbin/httpd/httpd.c url_decode.
518 */
519 static const struct got_error *
520 gotweb_urldecode(char *url)
522 char *p, *q;
523 char hex[3];
524 unsigned long x;
526 hex[2] = '\0';
527 p = q = url;
529 while (*p != '\0') {
530 switch (*p) {
531 case '%':
532 /* Encoding character is followed by two hex chars */
533 if (!isxdigit((unsigned char)p[1]) ||
534 !isxdigit((unsigned char)p[2]) ||
535 (p[1] == '0' && p[2] == '0'))
536 return got_error(GOT_ERR_BAD_QUERYSTRING);
538 hex[0] = p[1];
539 hex[1] = p[2];
541 /*
542 * We don't have to validate "hex" because it is
543 * guaranteed to include two hex chars followed by nul.
544 */
545 x = strtoul(hex, NULL, 16);
546 *q = (char)x;
547 p += 2;
548 break;
549 default:
550 *q = *p;
551 break;
553 p++;
554 q++;
556 *q = '\0';
558 return NULL;
561 static const struct got_error *
562 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
564 const struct got_error *error = NULL;
565 const char *errstr;
566 int a_cnt, el_cnt;
568 error = gotweb_urldecode(value);
569 if (error)
570 return error;
572 for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
573 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
574 continue;
576 switch (querystring_keys[el_cnt].element) {
577 case ACTION:
578 for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
579 if (strcmp(value, action_keys[a_cnt].name) != 0)
580 continue;
581 else if (strcmp(value,
582 action_keys[a_cnt].name) == 0){
583 (*qs)->action =
584 action_keys[a_cnt].action;
585 goto qa_found;
588 (*qs)->action = ERR;
589 qa_found:
590 break;
591 case COMMIT:
592 (*qs)->commit = strdup(value);
593 if ((*qs)->commit == NULL) {
594 error = got_error_from_errno2(__func__,
595 "strdup");
596 goto done;
598 break;
599 case RFILE:
600 (*qs)->file = strdup(value);
601 if ((*qs)->file == NULL) {
602 error = got_error_from_errno2(__func__,
603 "strdup");
604 goto done;
606 break;
607 case FOLDER:
608 (*qs)->folder = strdup(value);
609 if ((*qs)->folder == NULL) {
610 error = got_error_from_errno2(__func__,
611 "strdup");
612 goto done;
614 break;
615 case HEADREF:
616 free((*qs)->headref);
617 (*qs)->headref = strdup(value);
618 if ((*qs)->headref == NULL) {
619 error = got_error_from_errno2(__func__,
620 "strdup");
621 goto done;
623 break;
624 case INDEX_PAGE:
625 if (*value == '\0')
626 break;
627 (*qs)->index_page = strtonum(value, INT64_MIN,
628 INT64_MAX, &errstr);
629 if (errstr) {
630 error = got_error_from_errno3(__func__,
631 "strtonum", errstr);
632 goto done;
634 if ((*qs)->index_page < 0)
635 (*qs)->index_page = 0;
636 break;
637 case PATH:
638 (*qs)->path = strdup(value);
639 if ((*qs)->path == NULL) {
640 error = got_error_from_errno2(__func__,
641 "strdup");
642 goto done;
644 break;
645 case PAGE:
646 if (*value == '\0')
647 break;
648 (*qs)->page = strtonum(value, INT64_MIN,
649 INT64_MAX, &errstr);
650 if (errstr) {
651 error = got_error_from_errno3(__func__,
652 "strtonum", errstr);
653 goto done;
655 if ((*qs)->page < 0)
656 (*qs)->page = 0;
657 break;
660 /* entry found */
661 break;
663 done:
664 return error;
667 void
668 gotweb_free_repo_tag(struct repo_tag *rt)
670 if (rt != NULL) {
671 free(rt->commit_id);
672 free(rt->tag_name);
673 free(rt->tag_commit);
674 free(rt->commit_msg);
675 free(rt->tagger);
677 free(rt);
680 void
681 gotweb_free_repo_commit(struct repo_commit *rc)
683 if (rc != NULL) {
684 free(rc->path);
685 free(rc->refs_str);
686 free(rc->commit_id);
687 free(rc->parent_id);
688 free(rc->tree_id);
689 free(rc->author);
690 free(rc->committer);
691 free(rc->commit_msg);
693 free(rc);
696 static void
697 gotweb_free_querystring(struct querystring *qs)
699 if (qs != NULL) {
700 free(qs->commit);
701 free(qs->file);
702 free(qs->folder);
703 free(qs->headref);
704 free(qs->path);
706 free(qs);
709 static void
710 gotweb_free_repo_dir(struct repo_dir *repo_dir)
712 if (repo_dir != NULL) {
713 free(repo_dir->name);
714 free(repo_dir->owner);
715 free(repo_dir->description);
716 free(repo_dir->url);
717 free(repo_dir->path);
719 free(repo_dir);
722 void
723 gotweb_free_transport(struct transport *t)
725 const struct got_error *err;
726 struct repo_commit *rc = NULL, *trc = NULL;
727 struct repo_tag *rt = NULL, *trt = NULL;
728 int i;
730 got_ref_list_free(&t->refs);
731 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
732 TAILQ_REMOVE(&t->repo_commits, rc, entry);
733 gotweb_free_repo_commit(rc);
735 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
736 TAILQ_REMOVE(&t->repo_tags, rt, entry);
737 gotweb_free_repo_tag(rt);
739 gotweb_free_repo_dir(t->repo_dir);
740 gotweb_free_querystring(t->qs);
741 free(t->more_id);
742 free(t->tags_more_id);
743 if (t->blob)
744 got_object_blob_close(t->blob);
745 if (t->fp) {
746 err = got_gotweb_closefile(t->fp);
747 if (err)
748 log_warnx("%s: got_gotweb_closefile failure: %s",
749 __func__, err->msg);
751 if (t->fd != -1 && close(t->fd) == -1)
752 log_warn("%s: close", __func__);
753 if (t->repos) {
754 for (i = 0; i < t->nrepos; ++i)
755 free(t->repos[i]);
756 free(t->repos);
758 if (t->repo)
759 got_repo_close(t->repo);
760 free(t);
763 void
764 gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
765 struct gotweb_url *next, int *have_next)
767 struct transport *t = c->t;
768 struct querystring *qs = t->qs;
769 struct server *srv = c->srv;
771 *have_prev = *have_next = 0;
773 if (qs->index_page > 0) {
774 *have_prev = 1;
775 *prev = (struct gotweb_url){
776 .action = -1,
777 .index_page = qs->index_page - 1,
778 .page = -1,
779 };
781 if (t->next_disp == srv->max_repos_display &&
782 t->repos_total != (qs->index_page + 1) *
783 srv->max_repos_display) {
784 *have_next = 1;
785 *next = (struct gotweb_url){
786 .action = -1,
787 .index_page = qs->index_page + 1,
788 .page = -1,
789 };
793 static int
794 gotweb_render_index(struct template *tp)
796 const struct got_error *error = NULL;
797 struct request *c = tp->tp_arg;
798 struct server *srv = c->srv;
799 struct transport *t = c->t;
800 struct querystring *qs = t->qs;
801 struct repo_dir *repo_dir = NULL;
802 struct dirent **sd_dent = t->repos;
803 unsigned int d_i, d_disp = 0;
804 unsigned int d_skipped = 0;
805 int type, r;
807 if (gotweb_render_repo_table_hdr(c->tp) == -1)
808 return -1;
810 for (d_i = 0; d_i < t->nrepos; d_i++) {
811 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
812 break;
814 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
815 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
816 d_skipped++;
817 continue;
820 error = got_path_dirent_type(&type, srv->repos_path,
821 sd_dent[d_i]);
822 if (error)
823 continue;
824 if (type != DT_DIR) {
825 d_skipped++;
826 continue;
829 if (qs->index_page > 0 && (qs->index_page *
830 srv->max_repos_display) > t->prev_disp) {
831 t->prev_disp++;
832 continue;
835 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
836 if (error)
837 continue;
839 error = gotweb_load_got_path(c, repo_dir);
840 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
841 if (error->code != GOT_ERR_NOT_GIT_REPO)
842 log_warnx("%s: %s: %s", __func__,
843 sd_dent[d_i]->d_name, error->msg);
844 gotweb_free_repo_dir(repo_dir);
845 repo_dir = NULL;
846 d_skipped++;
847 continue;
850 d_disp++;
851 t->prev_disp++;
853 r = gotweb_render_repo_fragment(c->tp, repo_dir);
854 gotweb_free_repo_dir(repo_dir);
855 repo_dir = NULL;
856 got_repo_close(t->repo);
857 t->repo = NULL;
858 if (r == -1)
859 return -1;
861 t->next_disp++;
862 if (d_disp == srv->max_repos_display)
863 break;
865 t->repos_total = t->nrepos - d_skipped;
867 if (srv->max_repos_display == 0)
868 return 0;
869 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
870 return 0;
871 if (t->repos_total <= srv->max_repos ||
872 t->repos_total <= srv->max_repos_display)
873 return 0;
875 if (gotweb_render_navs(c->tp) == -1)
876 return -1;
878 return 0;
881 static inline int
882 should_urlencode(int c)
884 if (c <= ' ' || c >= 127)
885 return 1;
887 switch (c) {
888 /* gen-delim */
889 case ':':
890 case '/':
891 case '?':
892 case '#':
893 case '[':
894 case ']':
895 case '@':
896 /* sub-delims */
897 case '!':
898 case '$':
899 case '&':
900 case '\'':
901 case '(':
902 case ')':
903 case '*':
904 case '+':
905 case ',':
906 case ';':
907 case '=':
908 /* needed because the URLs are embedded into the HTML */
909 case '\"':
910 return 1;
911 default:
912 return 0;
916 static char *
917 gotweb_urlencode(const char *str)
919 const char *s;
920 char *escaped;
921 size_t i, len;
922 int a, b;
924 len = 0;
925 for (s = str; *s; ++s) {
926 len++;
927 if (should_urlencode(*s))
928 len += 2;
931 escaped = calloc(1, len + 1);
932 if (escaped == NULL)
933 return NULL;
935 i = 0;
936 for (s = str; *s; ++s) {
937 if (should_urlencode(*s)) {
938 a = (*s & 0xF0) >> 4;
939 b = (*s & 0x0F);
941 escaped[i++] = '%';
942 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
943 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
944 } else
945 escaped[i++] = *s;
948 return escaped;
951 const char *
952 gotweb_action_name(int action)
954 switch (action) {
955 case BLAME:
956 return "blame";
957 case BLOB:
958 return "blob";
959 case BLOBRAW:
960 return "blobraw";
961 case BRIEFS:
962 return "briefs";
963 case COMMITS:
964 return "commits";
965 case DIFF:
966 return "diff";
967 case ERR:
968 return "err";
969 case INDEX:
970 return "index";
971 case PATCH:
972 return "patch";
973 case SUMMARY:
974 return "summary";
975 case TAG:
976 return "tag";
977 case TAGS:
978 return "tags";
979 case TREE:
980 return "tree";
981 case RSS:
982 return "rss";
983 default:
984 return NULL;
988 int
989 gotweb_render_url(struct request *c, struct gotweb_url *url)
991 const char *sep = "?", *action;
992 char *tmp;
993 int r;
995 action = gotweb_action_name(url->action);
996 if (action != NULL) {
997 if (tp_writef(c->tp, "?action=%s", action) == -1)
998 return -1;
999 sep = "&";
1002 if (url->commit) {
1003 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
1004 return -1;
1005 sep = "&";
1008 if (url->previd) {
1009 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
1010 return -1;
1011 sep = "&";
1014 if (url->prevset) {
1015 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
1016 return -1;
1017 sep = "&";
1020 if (url->file) {
1021 tmp = gotweb_urlencode(url->file);
1022 if (tmp == NULL)
1023 return -1;
1024 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1025 free(tmp);
1026 if (r == -1)
1027 return -1;
1028 sep = "&";
1031 if (url->folder) {
1032 tmp = gotweb_urlencode(url->folder);
1033 if (tmp == NULL)
1034 return -1;
1035 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1036 free(tmp);
1037 if (r == -1)
1038 return -1;
1039 sep = "&";
1042 if (url->headref) {
1043 tmp = gotweb_urlencode(url->headref);
1044 if (tmp == NULL)
1045 return -1;
1046 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1047 free(tmp);
1048 if (r == -1)
1049 return -1;
1050 sep = "&";
1053 if (url->index_page != -1) {
1054 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1055 url->index_page) == -1)
1056 return -1;
1057 sep = "&";
1060 if (url->path) {
1061 tmp = gotweb_urlencode(url->path);
1062 if (tmp == NULL)
1063 return -1;
1064 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1065 free(tmp);
1066 if (r == -1)
1067 return -1;
1068 sep = "&";
1071 if (url->page != -1) {
1072 if (tp_writef(c->tp, "%spage=%d", sep, url->page) == -1)
1073 return -1;
1074 sep = "&";
1077 return 0;
1080 int
1081 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1083 struct template *tp = c->tp;
1084 const char *proto = c->https ? "https" : "http";
1086 if (tp_writes(tp, proto) == -1 ||
1087 tp_writes(tp, "://") == -1 ||
1088 tp_htmlescape(tp, c->server_name) == -1 ||
1089 tp_htmlescape(tp, c->document_uri) == -1)
1090 return -1;
1092 return gotweb_render_url(c, url);
1095 static const struct got_error *
1096 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1098 const struct got_error *error = NULL;
1099 struct socket *sock = c->sock;
1100 struct server *srv = c->srv;
1101 struct transport *t = c->t;
1102 DIR *dt;
1103 char *dir_test;
1105 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1106 GOTWEB_GIT_DIR) == -1)
1107 return got_error_from_errno("asprintf");
1109 dt = opendir(dir_test);
1110 if (dt == NULL) {
1111 free(dir_test);
1112 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1113 repo_dir->name) == -1)
1114 return got_error_from_errno("asprintf");
1115 dt = opendir(dir_test);
1116 if (dt == NULL) {
1117 free(dir_test);
1118 return got_error_path(repo_dir->name,
1119 GOT_ERR_NOT_GIT_REPO);
1123 repo_dir->path = dir_test;
1124 dir_test = NULL;
1126 if (srv->respect_exportok &&
1127 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1128 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1129 goto err;
1132 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1133 if (error)
1134 goto err;
1135 error = gotweb_get_repo_description(&repo_dir->description, srv,
1136 repo_dir->path, dirfd(dt));
1137 if (error)
1138 goto err;
1139 error = got_get_repo_owner(&repo_dir->owner, c);
1140 if (error)
1141 goto err;
1142 if (srv->show_repo_age) {
1143 error = got_get_repo_age(&repo_dir->age, c, NULL);
1144 if (error)
1145 goto err;
1147 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1148 dirfd(dt));
1149 err:
1150 free(dir_test);
1151 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1152 error = got_error_from_errno("closedir");
1153 if (error && t->repo) {
1154 got_repo_close(t->repo);
1155 t->repo = NULL;
1157 return error;
1160 static const struct got_error *
1161 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1163 const struct got_error *error;
1165 *repo_dir = calloc(1, sizeof(**repo_dir));
1166 if (*repo_dir == NULL)
1167 return got_error_from_errno("calloc");
1169 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1170 error = got_error_from_errno("asprintf");
1171 free(*repo_dir);
1172 *repo_dir = NULL;
1173 return error;
1175 (*repo_dir)->owner = NULL;
1176 (*repo_dir)->description = NULL;
1177 (*repo_dir)->url = NULL;
1178 (*repo_dir)->path = NULL;
1180 return NULL;
1183 static const struct got_error *
1184 gotweb_get_repo_description(char **description, struct server *srv,
1185 const char *dirpath, int dir)
1187 const struct got_error *error = NULL;
1188 struct stat sb;
1189 int fd = -1;
1190 off_t len;
1192 *description = NULL;
1193 if (srv->show_repo_description == 0)
1194 return NULL;
1196 fd = openat(dir, "description", O_RDONLY);
1197 if (fd == -1) {
1198 if (errno != ENOENT && errno != EACCES) {
1199 error = got_error_from_errno_fmt("openat %s/%s",
1200 dirpath, "description");
1202 goto done;
1205 if (fstat(fd, &sb) == -1) {
1206 error = got_error_from_errno_fmt("fstat %s/%s",
1207 dirpath, "description");
1208 goto done;
1211 len = sb.st_size;
1212 if (len > GOTWEBD_MAXDESCRSZ - 1)
1213 len = GOTWEBD_MAXDESCRSZ - 1;
1215 *description = calloc(len + 1, sizeof(**description));
1216 if (*description == NULL) {
1217 error = got_error_from_errno("calloc");
1218 goto done;
1221 if (read(fd, *description, len) == -1)
1222 error = got_error_from_errno("read");
1223 done:
1224 if (fd != -1 && close(fd) == -1 && error == NULL)
1225 error = got_error_from_errno("close");
1226 return error;
1229 static const struct got_error *
1230 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1231 int dir)
1233 const struct got_error *error = NULL;
1234 struct stat sb;
1235 int fd = -1;
1236 off_t len;
1238 *url = NULL;
1239 if (srv->show_repo_cloneurl == 0)
1240 return NULL;
1242 fd = openat(dir, "cloneurl", O_RDONLY);
1243 if (fd == -1) {
1244 if (errno != ENOENT && errno != EACCES) {
1245 error = got_error_from_errno_fmt("openat %s/%s",
1246 dirpath, "cloneurl");
1248 goto done;
1251 if (fstat(fd, &sb) == -1) {
1252 error = got_error_from_errno_fmt("fstat %s/%s",
1253 dirpath, "cloneurl");
1254 goto done;
1257 len = sb.st_size;
1258 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1259 len = GOTWEBD_MAXCLONEURLSZ - 1;
1261 *url = calloc(len + 1, sizeof(**url));
1262 if (*url == NULL) {
1263 error = got_error_from_errno("calloc");
1264 goto done;
1267 if (read(fd, *url, len) == -1)
1268 error = got_error_from_errno("read");
1269 done:
1270 if (fd != -1 && close(fd) == -1 && error == NULL)
1271 error = got_error_from_errno("close");
1272 return error;
1275 int
1276 gotweb_render_age(struct template *tp, time_t committer_time)
1278 struct request *c = tp->tp_arg;
1279 long long diff_time;
1280 const char *years = "years ago", *months = "months ago";
1281 const char *weeks = "weeks ago", *days = "days ago";
1282 const char *hours = "hours ago", *minutes = "minutes ago";
1283 const char *seconds = "seconds ago", *now = "right now";
1285 diff_time = time(NULL) - committer_time;
1286 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1287 if (tp_writef(c->tp, "%lld %s",
1288 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1289 return -1;
1290 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1291 if (tp_writef(c->tp, "%lld %s",
1292 (diff_time / 60 / 60 / 24 / (365 / 12)),
1293 months) == -1)
1294 return -1;
1295 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1296 if (tp_writef(c->tp, "%lld %s",
1297 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1298 return -1;
1299 } else if (diff_time > 60 * 60 * 24 * 2) {
1300 if (tp_writef(c->tp, "%lld %s",
1301 (diff_time / 60 / 60 / 24), days) == -1)
1302 return -1;
1303 } else if (diff_time > 60 * 60 * 2) {
1304 if (tp_writef(c->tp, "%lld %s",
1305 (diff_time / 60 / 60), hours) == -1)
1306 return -1;
1307 } else if (diff_time > 60 * 2) {
1308 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1309 minutes) == -1)
1310 return -1;
1311 } else if (diff_time > 2) {
1312 if (tp_writef(c->tp, "%lld %s", diff_time,
1313 seconds) == -1)
1314 return -1;
1315 } else {
1316 if (tp_writes(tp, now) == -1)
1317 return -1;
1319 return 0;