Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20 #include "got_compat.h"
22 #include <net/if.h>
23 #include <netinet/in.h>
24 #include <sys/queue.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <event.h>
32 #include <fcntl.h>
33 #include <imsg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "gotwebd.h"
52 #include "tmpl.h"
54 static const struct querystring_keys querystring_keys[] = {
55 { "action", ACTION },
56 { "commit", COMMIT },
57 { "file", RFILE },
58 { "folder", FOLDER },
59 { "headref", HEADREF },
60 { "index_page", INDEX_PAGE },
61 { "path", PATH },
62 };
64 static const struct action_keys action_keys[] = {
65 { "blame", BLAME },
66 { "blob", BLOB },
67 { "blobraw", BLOBRAW },
68 { "briefs", BRIEFS },
69 { "commits", COMMITS },
70 { "diff", DIFF },
71 { "error", ERR },
72 { "index", INDEX },
73 { "patch", PATCH },
74 { "summary", SUMMARY },
75 { "tag", TAG },
76 { "tags", TAGS },
77 { "tree", TREE },
78 { "rss", RSS },
79 };
81 static const struct got_error *gotweb_init_querystring(struct querystring **);
82 static const struct got_error *gotweb_parse_querystring(struct querystring **,
83 char *);
84 static const struct got_error *gotweb_assign_querystring(struct querystring **,
85 char *, char *);
86 static int gotweb_render_index(struct template *);
87 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
88 const char *);
89 static const struct got_error *gotweb_load_got_path(struct request *c,
90 struct repo_dir *);
91 static const struct got_error *gotweb_get_repo_description(char **,
92 struct server *, const char *, int);
93 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
94 const char *, int);
96 static void gotweb_free_querystring(struct querystring *);
97 static void gotweb_free_repo_dir(struct repo_dir *);
99 struct server *gotweb_get_server(const char *);
101 static int
102 gotweb_reply(struct request *c, int status, const char *ctype,
103 struct gotweb_url *location)
105 const char *csp;
107 if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
108 return -1;
110 if (location) {
111 if (tp_writes(c->tp, "Location: ") == -1 ||
112 gotweb_render_url(c, location) == -1 ||
113 tp_writes(c->tp, "\r\n") == -1)
114 return -1;
117 csp = "Content-Security-Policy: default-src 'self'; "
118 "script-src 'none'; object-src 'none';\r\n";
119 if (tp_writes(c->tp, csp) == -1)
120 return -1;
122 if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
123 return -1;
125 return tp_writes(c->tp, "\r\n");
128 static int
129 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
130 const char *suffix)
132 int r;
134 r = tp_writef(c->tp, "Content-Disposition: attachment; "
135 "filename=%s%s\r\n", file, suffix ? suffix : "");
136 if (r == -1)
137 return -1;
138 return gotweb_reply(c, 200, ctype, NULL);
141 void
142 gotweb_process_request(struct request *c)
144 const struct got_error *error = NULL;
145 struct server *srv = NULL;
146 struct querystring *qs = NULL;
147 struct repo_dir *repo_dir = NULL;
148 const char *rss_ctype = "application/rss+xml;charset=utf-8";
149 const uint8_t *buf;
150 size_t len;
151 int r, binary = 0;
153 /* init the transport */
154 error = gotweb_init_transport(&c->t);
155 if (error) {
156 log_warnx("%s: %s", __func__, error->msg);
157 return;
159 /* don't process any further if client disconnected */
160 if (c->sock->client_status == CLIENT_DISCONNECT)
161 return;
162 /* get the gotwebd server */
163 srv = gotweb_get_server(c->server_name);
164 if (srv == NULL) {
165 log_warnx("%s: error server is NULL", __func__);
166 goto err;
168 c->srv = srv;
169 /* parse our querystring */
170 error = gotweb_init_querystring(&qs);
171 if (error) {
172 log_warnx("%s: %s", __func__, error->msg);
173 goto err;
175 c->t->qs = qs;
176 error = gotweb_parse_querystring(&qs, c->querystring);
177 if (error) {
178 log_warnx("%s: %s", __func__, error->msg);
179 goto err;
182 /*
183 * certain actions require a commit id in the querystring. this stops
184 * bad actors from exploiting this by manually manipulating the
185 * querystring.
186 */
188 if (qs->action == BLAME || qs->action == BLOB ||
189 qs->action == BLOBRAW || qs->action == DIFF ||
190 qs->action == PATCH) {
191 if (qs->commit == NULL) {
192 error = got_error(GOT_ERR_BAD_QUERYSTRING);
193 goto err;
197 if (qs->action != INDEX) {
198 error = gotweb_init_repo_dir(&repo_dir, qs->path);
199 if (error)
200 goto err;
201 error = gotweb_load_got_path(c, repo_dir);
202 c->t->repo_dir = repo_dir;
203 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
204 goto err;
207 if (qs->action == BLOBRAW || qs->action == BLOB) {
208 if (qs->folder == NULL || qs->file == NULL) {
209 error = got_error(GOT_ERR_BAD_QUERYSTRING);
210 goto err;
213 error = got_get_repo_commits(c, 1);
214 if (error)
215 goto err;
217 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
218 &binary, c, qs->folder, qs->file, qs->commit);
219 if (error)
220 goto err;
223 switch (qs->action) {
224 case BLAME:
225 if (qs->folder == NULL || qs->file == NULL) {
226 error = got_error(GOT_ERR_BAD_QUERYSTRING);
227 goto err;
229 error = got_get_repo_commits(c, 1);
230 if (error) {
231 log_warnx("%s: %s", __func__, error->msg);
232 goto err;
234 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
235 return;
236 gotweb_render_page(c->tp, gotweb_render_blame);
237 return;
238 case BLOB:
239 if (binary) {
240 struct gotweb_url url = {
241 .index_page = -1,
242 .action = BLOBRAW,
243 .path = qs->path,
244 .commit = qs->commit,
245 .folder = qs->folder,
246 .file = qs->file,
247 };
249 gotweb_reply(c, 302, NULL, &url);
250 return;
253 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
254 return;
255 gotweb_render_page(c->tp, gotweb_render_blob);
256 return;
257 case BLOBRAW:
258 if (binary)
259 r = gotweb_reply_file(c, "application/octet-stream",
260 qs->file, NULL);
261 else
262 r = gotweb_reply(c, 200, "text/plain", NULL);
263 if (r == -1)
264 return;
265 if (template_flush(c->tp) == -1)
266 return;
268 for (;;) {
269 error = got_object_blob_read_block(&len, c->t->blob);
270 if (error)
271 break;
272 if (len == 0)
273 break;
274 buf = got_object_blob_get_read_buf(c->t->blob);
275 if (fcgi_write(c, buf, len) == -1)
276 break;
278 return;
279 case BRIEFS:
280 error = got_get_repo_commits(c, srv->max_commits_display);
281 if (error)
282 goto err;
283 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
284 return;
285 gotweb_render_page(c->tp, gotweb_render_briefs);
286 return;
287 case COMMITS:
288 error = got_get_repo_commits(c, srv->max_commits_display);
289 if (error) {
290 log_warnx("%s: %s", __func__, error->msg);
291 goto err;
293 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
294 return;
295 gotweb_render_page(c->tp, gotweb_render_commits);
296 return;
297 case DIFF:
298 error = got_get_repo_commits(c, 1);
299 if (error) {
300 log_warnx("%s: %s", __func__, error->msg);
301 goto err;
303 error = got_open_diff_for_output(&c->t->fp, c);
304 if (error) {
305 log_warnx("%s: %s", __func__, error->msg);
306 goto err;
308 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
309 return;
310 gotweb_render_page(c->tp, gotweb_render_diff);
311 return;
312 case INDEX:
313 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
314 alphasort);
315 if (c->t->nrepos == -1) {
316 c->t->repos = NULL;
317 error = got_error_from_errno2("scandir",
318 srv->repos_path);
319 goto err;
321 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
322 return;
323 gotweb_render_page(c->tp, gotweb_render_index);
324 return;
325 case PATCH:
326 error = got_get_repo_commits(c, 1);
327 if (error) {
328 log_warnx("%s: %s", __func__, error->msg);
329 goto err;
331 error = got_open_diff_for_output(&c->t->fp, c);
332 if (error) {
333 log_warnx("%s: %s", __func__, error->msg);
334 goto err;
336 if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
337 return;
338 gotweb_render_patch(c->tp);
339 return;
340 case RSS:
341 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
342 if (error)
343 goto err;
344 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
345 == -1)
346 return;
347 gotweb_render_rss(c->tp);
348 return;
349 case SUMMARY:
350 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
351 got_ref_cmp_by_name, NULL);
352 if (error) {
353 log_warnx("%s: got_ref_list: %s", __func__,
354 error->msg);
355 goto err;
357 error = got_get_repo_commits(c, srv->summary_commits_display);
358 if (error)
359 goto err;
360 qs->action = TAGS;
361 error = got_get_repo_tags(c, srv->summary_tags_display);
362 if (error) {
363 log_warnx("%s: got_get_repo_tags: %s", __func__,
364 error->msg);
365 goto err;
367 qs->action = SUMMARY;
368 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
369 return;
370 gotweb_render_page(c->tp, gotweb_render_summary);
371 return;
372 case TAG:
373 error = got_get_repo_tags(c, 1);
374 if (error) {
375 log_warnx("%s: %s", __func__, error->msg);
376 goto err;
378 if (c->t->tag_count == 0) {
379 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
380 "bad commit id");
381 goto err;
383 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
384 return;
385 gotweb_render_page(c->tp, gotweb_render_tag);
386 return;
387 case TAGS:
388 error = got_get_repo_tags(c, srv->max_commits_display);
389 if (error) {
390 log_warnx("%s: %s", __func__, error->msg);
391 goto err;
393 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
394 return;
395 gotweb_render_page(c->tp, gotweb_render_tags);
396 return;
397 case TREE:
398 error = got_get_repo_commits(c, 1);
399 if (error) {
400 log_warnx("%s: %s", __func__, error->msg);
401 goto err;
403 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
404 return;
405 gotweb_render_page(c->tp, gotweb_render_tree);
406 return;
407 case ERR:
408 default:
409 error = got_error(GOT_ERR_BAD_QUERYSTRING);
412 err:
413 c->t->error = error;
414 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
415 return;
416 gotweb_render_page(c->tp, gotweb_render_error);
419 struct server *
420 gotweb_get_server(const char *server_name)
422 struct server *srv;
424 /* check against the server name first */
425 if (*server_name != '\0')
426 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
427 if (strcmp(srv->name, server_name) == 0)
428 return srv;
430 /* otherwise, use the first server */
431 return TAILQ_FIRST(&gotwebd_env->servers);
432 };
434 const struct got_error *
435 gotweb_init_transport(struct transport **t)
437 const struct got_error *error = NULL;
439 *t = calloc(1, sizeof(**t));
440 if (*t == NULL)
441 return got_error_from_errno2(__func__, "calloc");
443 TAILQ_INIT(&(*t)->repo_commits);
444 TAILQ_INIT(&(*t)->repo_tags);
445 TAILQ_INIT(&(*t)->refs);
447 (*t)->fd = -1;
449 return error;
452 static const struct got_error *
453 gotweb_init_querystring(struct querystring **qs)
455 const struct got_error *error = NULL;
457 *qs = calloc(1, sizeof(**qs));
458 if (*qs == NULL)
459 return got_error_from_errno2(__func__, "calloc");
461 (*qs)->headref = strdup("HEAD");
462 if ((*qs)->headref == NULL) {
463 free(*qs);
464 *qs = NULL;
465 return got_error_from_errno2(__func__, "strdup");
468 (*qs)->action = INDEX;
470 return error;
473 static const struct got_error *
474 gotweb_parse_querystring(struct querystring **qs, char *qst)
476 const struct got_error *error = NULL;
477 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
478 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
480 if (qst == NULL)
481 return error;
483 tok1 = strdup(qst);
484 if (tok1 == NULL)
485 return got_error_from_errno2(__func__, "strdup");
487 tok1_pair = tok1;
488 tok1_end = tok1;
490 while (tok1_pair != NULL) {
491 strsep(&tok1_end, "&");
493 tok2 = strdup(tok1_pair);
494 if (tok2 == NULL) {
495 free(tok1);
496 return got_error_from_errno2(__func__, "strdup");
499 tok2_pair = tok2;
500 tok2_end = tok2;
502 while (tok2_pair != NULL) {
503 strsep(&tok2_end, "=");
504 if (tok2_end) {
505 error = gotweb_assign_querystring(qs, tok2_pair,
506 tok2_end);
507 if (error)
508 goto err;
510 tok2_pair = tok2_end;
512 free(tok2);
513 tok1_pair = tok1_end;
515 free(tok1);
516 return error;
517 err:
518 free(tok2);
519 free(tok1);
520 return error;
523 /*
524 * Adapted from usr.sbin/httpd/httpd.c url_decode.
525 */
526 static const struct got_error *
527 gotweb_urldecode(char *url)
529 char *p, *q;
530 char hex[3];
531 unsigned long x;
533 hex[2] = '\0';
534 p = q = url;
536 while (*p != '\0') {
537 switch (*p) {
538 case '%':
539 /* Encoding character is followed by two hex chars */
540 if (!isxdigit((unsigned char)p[1]) ||
541 !isxdigit((unsigned char)p[2]) ||
542 (p[1] == '0' && p[2] == '0'))
543 return got_error(GOT_ERR_BAD_QUERYSTRING);
545 hex[0] = p[1];
546 hex[1] = p[2];
548 /*
549 * We don't have to validate "hex" because it is
550 * guaranteed to include two hex chars followed by nul.
551 */
552 x = strtoul(hex, NULL, 16);
553 *q = (char)x;
554 p += 2;
555 break;
556 default:
557 *q = *p;
558 break;
560 p++;
561 q++;
563 *q = '\0';
565 return NULL;
568 static const struct got_error *
569 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
571 const struct got_error *error = NULL;
572 const char *errstr;
573 int a_cnt, el_cnt;
575 error = gotweb_urldecode(value);
576 if (error)
577 return error;
579 for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
580 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
581 continue;
583 switch (querystring_keys[el_cnt].element) {
584 case ACTION:
585 for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
586 if (strcmp(value, action_keys[a_cnt].name) != 0)
587 continue;
588 else if (strcmp(value,
589 action_keys[a_cnt].name) == 0){
590 (*qs)->action =
591 action_keys[a_cnt].action;
592 goto qa_found;
595 (*qs)->action = ERR;
596 qa_found:
597 break;
598 case COMMIT:
599 (*qs)->commit = strdup(value);
600 if ((*qs)->commit == NULL) {
601 error = got_error_from_errno2(__func__,
602 "strdup");
603 goto done;
605 break;
606 case RFILE:
607 (*qs)->file = strdup(value);
608 if ((*qs)->file == NULL) {
609 error = got_error_from_errno2(__func__,
610 "strdup");
611 goto done;
613 break;
614 case FOLDER:
615 (*qs)->folder = strdup(value);
616 if ((*qs)->folder == NULL) {
617 error = got_error_from_errno2(__func__,
618 "strdup");
619 goto done;
621 break;
622 case HEADREF:
623 free((*qs)->headref);
624 (*qs)->headref = strdup(value);
625 if ((*qs)->headref == NULL) {
626 error = got_error_from_errno2(__func__,
627 "strdup");
628 goto done;
630 break;
631 case INDEX_PAGE:
632 if (*value == '\0')
633 break;
634 (*qs)->index_page = strtonum(value, INT64_MIN,
635 INT64_MAX, &errstr);
636 if (errstr) {
637 error = got_error_from_errno3(__func__,
638 "strtonum", errstr);
639 goto done;
641 if ((*qs)->index_page < 0)
642 (*qs)->index_page = 0;
643 break;
644 case PATH:
645 (*qs)->path = strdup(value);
646 if ((*qs)->path == NULL) {
647 error = got_error_from_errno2(__func__,
648 "strdup");
649 goto done;
651 break;
654 /* entry found */
655 break;
657 done:
658 return error;
661 void
662 gotweb_free_repo_tag(struct repo_tag *rt)
664 if (rt != NULL) {
665 free(rt->commit_id);
666 free(rt->tag_name);
667 free(rt->tag_commit);
668 free(rt->commit_msg);
669 free(rt->tagger);
671 free(rt);
674 void
675 gotweb_free_repo_commit(struct repo_commit *rc)
677 if (rc != NULL) {
678 free(rc->path);
679 free(rc->refs_str);
680 free(rc->commit_id);
681 free(rc->parent_id);
682 free(rc->tree_id);
683 free(rc->author);
684 free(rc->committer);
685 free(rc->commit_msg);
687 free(rc);
690 static void
691 gotweb_free_querystring(struct querystring *qs)
693 if (qs != NULL) {
694 free(qs->commit);
695 free(qs->file);
696 free(qs->folder);
697 free(qs->headref);
698 free(qs->path);
700 free(qs);
703 static void
704 gotweb_free_repo_dir(struct repo_dir *repo_dir)
706 if (repo_dir != NULL) {
707 free(repo_dir->name);
708 free(repo_dir->owner);
709 free(repo_dir->description);
710 free(repo_dir->url);
711 free(repo_dir->path);
713 free(repo_dir);
716 void
717 gotweb_free_transport(struct transport *t)
719 const struct got_error *err;
720 struct repo_commit *rc = NULL, *trc = NULL;
721 struct repo_tag *rt = NULL, *trt = NULL;
722 int i;
724 got_ref_list_free(&t->refs);
725 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
726 TAILQ_REMOVE(&t->repo_commits, rc, entry);
727 gotweb_free_repo_commit(rc);
729 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
730 TAILQ_REMOVE(&t->repo_tags, rt, entry);
731 gotweb_free_repo_tag(rt);
733 gotweb_free_repo_dir(t->repo_dir);
734 gotweb_free_querystring(t->qs);
735 free(t->more_id);
736 free(t->tags_more_id);
737 if (t->blob)
738 got_object_blob_close(t->blob);
739 if (t->fp) {
740 err = got_gotweb_closefile(t->fp);
741 if (err)
742 log_warnx("%s: got_gotweb_closefile failure: %s",
743 __func__, err->msg);
745 if (t->fd != -1 && close(t->fd) == -1)
746 log_warn("%s: close", __func__);
747 if (t->repos) {
748 for (i = 0; i < t->nrepos; ++i)
749 free(t->repos[i]);
750 free(t->repos);
752 if (t->repo)
753 got_repo_close(t->repo);
754 free(t);
757 void
758 gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
759 struct gotweb_url *next, int *have_next)
761 struct transport *t = c->t;
762 struct querystring *qs = t->qs;
763 struct server *srv = c->srv;
765 *have_prev = *have_next = 0;
767 if (qs->index_page > 0) {
768 *have_prev = 1;
769 *prev = (struct gotweb_url){
770 .action = -1,
771 .index_page = qs->index_page - 1,
772 };
774 if (t->next_disp == srv->max_repos_display &&
775 t->repos_total != (qs->index_page + 1) *
776 srv->max_repos_display) {
777 *have_next = 1;
778 *next = (struct gotweb_url){
779 .action = -1,
780 .index_page = qs->index_page + 1,
781 };
785 static int
786 gotweb_render_index(struct template *tp)
788 const struct got_error *error = NULL;
789 struct request *c = tp->tp_arg;
790 struct server *srv = c->srv;
791 struct transport *t = c->t;
792 struct querystring *qs = t->qs;
793 struct repo_dir *repo_dir = NULL;
794 struct dirent **sd_dent = t->repos;
795 unsigned int d_i, d_disp = 0;
796 unsigned int d_skipped = 0;
797 int type, r;
799 if (gotweb_render_repo_table_hdr(c->tp) == -1)
800 return -1;
802 for (d_i = 0; d_i < t->nrepos; d_i++) {
803 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
804 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
805 d_skipped++;
806 continue;
809 error = got_path_dirent_type(&type, srv->repos_path,
810 sd_dent[d_i]);
811 if (error)
812 continue;
813 if (type != DT_DIR) {
814 d_skipped++;
815 continue;
818 if (qs->index_page > 0 && (qs->index_page *
819 srv->max_repos_display) > t->prev_disp) {
820 t->prev_disp++;
821 continue;
824 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
825 if (error)
826 continue;
828 error = gotweb_load_got_path(c, repo_dir);
829 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
830 if (error->code != GOT_ERR_NOT_GIT_REPO)
831 log_warnx("%s: %s: %s", __func__,
832 sd_dent[d_i]->d_name, error->msg);
833 gotweb_free_repo_dir(repo_dir);
834 repo_dir = NULL;
835 d_skipped++;
836 continue;
839 d_disp++;
840 t->prev_disp++;
842 r = gotweb_render_repo_fragment(c->tp, repo_dir);
843 gotweb_free_repo_dir(repo_dir);
844 repo_dir = NULL;
845 got_repo_close(t->repo);
846 t->repo = NULL;
847 if (r == -1)
848 return -1;
850 t->next_disp++;
851 if (d_disp == srv->max_repos_display)
852 break;
854 t->repos_total = t->nrepos - d_skipped;
856 if (srv->max_repos_display == 0 ||
857 t->repos_total <= srv->max_repos_display)
858 return 0;
860 if (gotweb_render_navs(c->tp) == -1)
861 return -1;
863 return 0;
866 static inline int
867 should_urlencode(int c)
869 if (c <= ' ' || c >= 127)
870 return 1;
872 switch (c) {
873 /* gen-delim */
874 case ':':
875 case '/':
876 case '?':
877 case '#':
878 case '[':
879 case ']':
880 case '@':
881 /* sub-delims */
882 case '!':
883 case '$':
884 case '&':
885 case '\'':
886 case '(':
887 case ')':
888 case '*':
889 case '+':
890 case ',':
891 case ';':
892 case '=':
893 /* needed because the URLs are embedded into the HTML */
894 case '\"':
895 return 1;
896 default:
897 return 0;
901 static char *
902 gotweb_urlencode(const char *str)
904 const char *s;
905 char *escaped;
906 size_t i, len;
907 int a, b;
909 len = 0;
910 for (s = str; *s; ++s) {
911 len++;
912 if (should_urlencode(*s))
913 len += 2;
916 escaped = calloc(1, len + 1);
917 if (escaped == NULL)
918 return NULL;
920 i = 0;
921 for (s = str; *s; ++s) {
922 if (should_urlencode(*s)) {
923 a = (*s & 0xF0) >> 4;
924 b = (*s & 0x0F);
926 escaped[i++] = '%';
927 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
928 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
929 } else
930 escaped[i++] = *s;
933 return escaped;
936 const char *
937 gotweb_action_name(int action)
939 switch (action) {
940 case BLAME:
941 return "blame";
942 case BLOB:
943 return "blob";
944 case BLOBRAW:
945 return "blobraw";
946 case BRIEFS:
947 return "briefs";
948 case COMMITS:
949 return "commits";
950 case DIFF:
951 return "diff";
952 case ERR:
953 return "err";
954 case INDEX:
955 return "index";
956 case PATCH:
957 return "patch";
958 case SUMMARY:
959 return "summary";
960 case TAG:
961 return "tag";
962 case TAGS:
963 return "tags";
964 case TREE:
965 return "tree";
966 case RSS:
967 return "rss";
968 default:
969 return NULL;
973 int
974 gotweb_render_url(struct request *c, struct gotweb_url *url)
976 const char *sep = "?", *action;
977 char *tmp;
978 int r;
980 action = gotweb_action_name(url->action);
981 if (action != NULL) {
982 if (tp_writef(c->tp, "?action=%s", action) == -1)
983 return -1;
984 sep = "&";
987 if (url->commit) {
988 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
989 return -1;
990 sep = "&";
993 if (url->previd) {
994 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
995 return -1;
996 sep = "&";
999 if (url->prevset) {
1000 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
1001 return -1;
1002 sep = "&";
1005 if (url->file) {
1006 tmp = gotweb_urlencode(url->file);
1007 if (tmp == NULL)
1008 return -1;
1009 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1010 free(tmp);
1011 if (r == -1)
1012 return -1;
1013 sep = "&";
1016 if (url->folder) {
1017 tmp = gotweb_urlencode(url->folder);
1018 if (tmp == NULL)
1019 return -1;
1020 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1021 free(tmp);
1022 if (r == -1)
1023 return -1;
1024 sep = "&";
1027 if (url->headref) {
1028 tmp = gotweb_urlencode(url->headref);
1029 if (tmp == NULL)
1030 return -1;
1031 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1032 free(tmp);
1033 if (r == -1)
1034 return -1;
1035 sep = "&";
1038 if (url->index_page != -1) {
1039 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1040 url->index_page) == -1)
1041 return -1;
1042 sep = "&";
1045 if (url->path) {
1046 tmp = gotweb_urlencode(url->path);
1047 if (tmp == NULL)
1048 return -1;
1049 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1050 free(tmp);
1051 if (r == -1)
1052 return -1;
1053 sep = "&";
1056 return 0;
1059 int
1060 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1062 struct template *tp = c->tp;
1063 const char *proto = c->https ? "https" : "http";
1065 if (tp_writes(tp, proto) == -1 ||
1066 tp_writes(tp, "://") == -1 ||
1067 tp_htmlescape(tp, c->server_name) == -1 ||
1068 tp_htmlescape(tp, c->document_uri) == -1)
1069 return -1;
1071 return gotweb_render_url(c, url);
1074 static const struct got_error *
1075 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1077 const struct got_error *error = NULL;
1078 struct socket *sock = c->sock;
1079 struct server *srv = c->srv;
1080 struct transport *t = c->t;
1081 DIR *dt;
1082 char *dir_test;
1084 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1085 GOTWEB_GIT_DIR) == -1)
1086 return got_error_from_errno("asprintf");
1088 dt = opendir(dir_test);
1089 if (dt == NULL) {
1090 free(dir_test);
1091 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1092 repo_dir->name) == -1)
1093 return got_error_from_errno("asprintf");
1094 dt = opendir(dir_test);
1095 if (dt == NULL) {
1096 free(dir_test);
1097 return got_error_path(repo_dir->name,
1098 GOT_ERR_NOT_GIT_REPO);
1102 repo_dir->path = dir_test;
1103 dir_test = NULL;
1105 if (srv->respect_exportok &&
1106 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1107 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1108 goto err;
1111 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1112 if (error)
1113 goto err;
1114 error = gotweb_get_repo_description(&repo_dir->description, srv,
1115 repo_dir->path, dirfd(dt));
1116 if (error)
1117 goto err;
1118 error = got_get_repo_owner(&repo_dir->owner, c);
1119 if (error)
1120 goto err;
1121 if (srv->show_repo_age) {
1122 error = got_get_repo_age(&repo_dir->age, c, NULL);
1123 if (error)
1124 goto err;
1126 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1127 dirfd(dt));
1128 err:
1129 free(dir_test);
1130 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1131 error = got_error_from_errno("closedir");
1132 if (error && t->repo) {
1133 got_repo_close(t->repo);
1134 t->repo = NULL;
1136 return error;
1139 static const struct got_error *
1140 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1142 const struct got_error *error;
1144 *repo_dir = calloc(1, sizeof(**repo_dir));
1145 if (*repo_dir == NULL)
1146 return got_error_from_errno("calloc");
1148 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1149 error = got_error_from_errno("asprintf");
1150 free(*repo_dir);
1151 *repo_dir = NULL;
1152 return error;
1154 (*repo_dir)->owner = NULL;
1155 (*repo_dir)->description = NULL;
1156 (*repo_dir)->url = NULL;
1157 (*repo_dir)->path = NULL;
1159 return NULL;
1162 static const struct got_error *
1163 gotweb_get_repo_description(char **description, struct server *srv,
1164 const char *dirpath, int dir)
1166 const struct got_error *error = NULL;
1167 struct stat sb;
1168 int fd = -1;
1169 off_t len;
1171 *description = NULL;
1172 if (srv->show_repo_description == 0)
1173 return NULL;
1175 fd = openat(dir, "description", O_RDONLY);
1176 if (fd == -1) {
1177 if (errno != ENOENT && errno != EACCES) {
1178 error = got_error_from_errno_fmt("openat %s/%s",
1179 dirpath, "description");
1181 goto done;
1184 if (fstat(fd, &sb) == -1) {
1185 error = got_error_from_errno_fmt("fstat %s/%s",
1186 dirpath, "description");
1187 goto done;
1190 len = sb.st_size;
1191 if (len > GOTWEBD_MAXDESCRSZ - 1)
1192 len = GOTWEBD_MAXDESCRSZ - 1;
1194 *description = calloc(len + 1, sizeof(**description));
1195 if (*description == NULL) {
1196 error = got_error_from_errno("calloc");
1197 goto done;
1200 if (read(fd, *description, len) == -1)
1201 error = got_error_from_errno("read");
1202 done:
1203 if (fd != -1 && close(fd) == -1 && error == NULL)
1204 error = got_error_from_errno("close");
1205 return error;
1208 static const struct got_error *
1209 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1210 int dir)
1212 const struct got_error *error = NULL;
1213 struct stat sb;
1214 int fd = -1;
1215 off_t len;
1217 *url = NULL;
1218 if (srv->show_repo_cloneurl == 0)
1219 return NULL;
1221 fd = openat(dir, "cloneurl", O_RDONLY);
1222 if (fd == -1) {
1223 if (errno != ENOENT && errno != EACCES) {
1224 error = got_error_from_errno_fmt("openat %s/%s",
1225 dirpath, "cloneurl");
1227 goto done;
1230 if (fstat(fd, &sb) == -1) {
1231 error = got_error_from_errno_fmt("fstat %s/%s",
1232 dirpath, "cloneurl");
1233 goto done;
1236 len = sb.st_size;
1237 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1238 len = GOTWEBD_MAXCLONEURLSZ - 1;
1240 *url = calloc(len + 1, sizeof(**url));
1241 if (*url == NULL) {
1242 error = got_error_from_errno("calloc");
1243 goto done;
1246 if (read(fd, *url, len) == -1)
1247 error = got_error_from_errno("read");
1248 done:
1249 if (fd != -1 && close(fd) == -1 && error == NULL)
1250 error = got_error_from_errno("close");
1251 return error;
1254 int
1255 gotweb_render_age(struct template *tp, time_t committer_time)
1257 struct request *c = tp->tp_arg;
1258 long long diff_time;
1259 const char *years = "years ago", *months = "months ago";
1260 const char *weeks = "weeks ago", *days = "days ago";
1261 const char *hours = "hours ago", *minutes = "minutes ago";
1262 const char *seconds = "seconds ago", *now = "right now";
1264 diff_time = time(NULL) - committer_time;
1265 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1266 if (tp_writef(c->tp, "%lld %s",
1267 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1268 return -1;
1269 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1270 if (tp_writef(c->tp, "%lld %s",
1271 (diff_time / 60 / 60 / 24 / (365 / 12)),
1272 months) == -1)
1273 return -1;
1274 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1275 if (tp_writef(c->tp, "%lld %s",
1276 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1277 return -1;
1278 } else if (diff_time > 60 * 60 * 24 * 2) {
1279 if (tp_writef(c->tp, "%lld %s",
1280 (diff_time / 60 / 60 / 24), days) == -1)
1281 return -1;
1282 } else if (diff_time > 60 * 60 * 2) {
1283 if (tp_writef(c->tp, "%lld %s",
1284 (diff_time / 60 / 60), hours) == -1)
1285 return -1;
1286 } else if (diff_time > 60 * 2) {
1287 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1288 minutes) == -1)
1289 return -1;
1290 } else if (diff_time > 2) {
1291 if (tp_writef(c->tp, "%lld %s", diff_time,
1292 seconds) == -1)
1293 return -1;
1294 } else {
1295 if (tp_writes(tp, now) == -1)
1296 return -1;
1298 return 0;