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 "proc.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 { "page", PAGE },
64 };
66 static const struct action_keys action_keys[] = {
67 { "blame", BLAME },
68 { "blob", BLOB },
69 { "blobraw", BLOBRAW },
70 { "briefs", BRIEFS },
71 { "commits", COMMITS },
72 { "diff", DIFF },
73 { "error", ERR },
74 { "index", INDEX },
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(uint8_t *, uint8_t *);
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 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
109 return -1;
111 if (location) {
112 if (fcgi_puts(c->tp, "Location: ") == -1 ||
113 gotweb_render_url(c, location) == -1 ||
114 fcgi_puts(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 (fcgi_puts(c->tp, csp) == -1)
121 return -1;
123 if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
124 return -1;
126 return fcgi_puts(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 = fcgi_printf(c, "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, c->http_host);
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 if (qs->commit == NULL) {
192 error = got_error(GOT_ERR_QUERYSTRING);
193 goto err;
197 if (qs->action != INDEX) {
198 error = gotweb_init_repo_dir(&repo_dir, qs->path);
199 if (error)
200 goto err;
201 error = gotweb_load_got_path(c, repo_dir);
202 c->t->repo_dir = repo_dir;
203 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
204 goto err;
207 if (qs->action == BLOBRAW || qs->action == BLOB) {
208 error = got_get_repo_commits(c, 1);
209 if (error)
210 goto err;
212 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
213 &binary, c);
214 if (error)
215 goto err;
218 switch(qs->action) {
219 case BLAME:
220 error = got_get_repo_commits(c, 1);
221 if (error) {
222 log_warnx("%s: %s", __func__, error->msg);
223 goto err;
225 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
226 return;
227 gotweb_render_page(c->tp, gotweb_render_blame);
228 return;
229 case BLOB:
230 if (binary) {
231 struct gotweb_url url = {
232 .index_page = -1,
233 .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;
258 for (;;) {
259 error = got_object_blob_read_block(&len, c->t->blob);
260 if (error)
261 break;
262 if (len == 0)
263 break;
264 buf = got_object_blob_get_read_buf(c->t->blob);
265 if (fcgi_gen_binary_response(c, buf, len) == -1)
266 break;
268 return;
269 case BRIEFS:
270 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
271 return;
272 gotweb_render_page(c->tp, gotweb_render_briefs);
273 return;
274 case COMMITS:
275 error = got_get_repo_commits(c, srv->max_commits_display);
276 if (error) {
277 log_warnx("%s: %s", __func__, error->msg);
278 goto err;
280 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
281 return;
282 gotweb_render_page(c->tp, gotweb_render_commits);
283 return;
284 case DIFF:
285 error = got_get_repo_commits(c, 1);
286 if (error) {
287 log_warnx("%s: %s", __func__, error->msg);
288 goto err;
290 error = got_open_diff_for_output(&c->t->fp, c);
291 if (error) {
292 log_warnx("%s: %s", __func__, error->msg);
293 goto err;
295 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
296 return;
297 gotweb_render_page(c->tp, gotweb_render_diff);
298 return;
299 case INDEX:
300 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
301 alphasort);
302 if (c->t->nrepos == -1) {
303 c->t->repos = NULL;
304 error = got_error_from_errno2("scandir",
305 srv->repos_path);
306 goto err;
308 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
309 return;
310 gotweb_render_page(c->tp, gotweb_render_index);
311 return;
312 case RSS:
313 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
314 if (error)
315 goto err;
316 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
317 == -1)
318 return;
319 gotweb_render_rss(c->tp);
320 return;
321 case SUMMARY:
322 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
323 got_ref_cmp_by_name, NULL);
324 if (error) {
325 log_warnx("%s: got_ref_list: %s", __func__,
326 error->msg);
327 goto err;
329 qs->action = TAGS;
330 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
331 if (error) {
332 log_warnx("%s: got_get_repo_tags: %s", __func__,
333 error->msg);
334 goto err;
336 qs->action = SUMMARY;
337 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
338 return;
339 gotweb_render_page(c->tp, gotweb_render_summary);
340 return;
341 case TAG:
342 error = got_get_repo_tags(c, 1);
343 if (error) {
344 log_warnx("%s: %s", __func__, error->msg);
345 goto err;
347 if (c->t->tag_count == 0) {
348 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
349 "bad commit id");
350 goto err;
352 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
353 return;
354 gotweb_render_page(c->tp, gotweb_render_tag);
355 return;
356 case TAGS:
357 error = got_get_repo_tags(c, srv->max_commits_display);
358 if (error) {
359 log_warnx("%s: %s", __func__, error->msg);
360 goto err;
362 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
363 return;
364 gotweb_render_page(c->tp, gotweb_render_tags);
365 return;
366 case TREE:
367 error = got_get_repo_commits(c, 1);
368 if (error) {
369 log_warnx("%s: %s", __func__, error->msg);
370 goto err;
372 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
373 return;
374 gotweb_render_page(c->tp, gotweb_render_tree);
375 return;
376 case ERR:
377 default:
378 error = got_error(GOT_ERR_BAD_QUERYSTRING);
381 err:
382 c->t->error = error;
383 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
384 return;
385 gotweb_render_page(c->tp, gotweb_render_error);
388 struct server *
389 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
391 struct server *srv = NULL;
393 /* check against the server name first */
394 if (strlen(server_name) > 0)
395 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
396 if (strcmp(srv->name, server_name) == 0)
397 goto done;
399 /* check against subdomain second */
400 if (strlen(subdomain) > 0)
401 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
402 if (strcmp(srv->name, subdomain) == 0)
403 goto done;
405 /* if those fail, send first server */
406 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
407 if (srv != NULL)
408 break;
409 done:
410 return srv;
411 };
413 const struct got_error *
414 gotweb_init_transport(struct transport **t)
416 const struct got_error *error = NULL;
418 *t = calloc(1, sizeof(**t));
419 if (*t == NULL)
420 return got_error_from_errno2("%s: calloc", __func__);
422 TAILQ_INIT(&(*t)->repo_commits);
423 TAILQ_INIT(&(*t)->repo_tags);
424 TAILQ_INIT(&(*t)->refs);
426 (*t)->fd = -1;
428 return error;
431 static const struct got_error *
432 gotweb_init_querystring(struct querystring **qs)
434 const struct got_error *error = NULL;
436 *qs = calloc(1, sizeof(**qs));
437 if (*qs == NULL)
438 return got_error_from_errno2("%s: calloc", __func__);
440 (*qs)->headref = strdup("HEAD");
441 if ((*qs)->headref == NULL) {
442 free(*qs);
443 *qs = NULL;
444 return got_error_from_errno2("%s: strdup", __func__);
447 (*qs)->action = INDEX;
448 (*qs)->commit = NULL;
449 (*qs)->file = NULL;
450 (*qs)->folder = NULL;
451 (*qs)->index_page = 0;
452 (*qs)->path = NULL;
454 return error;
457 static const struct got_error *
458 gotweb_parse_querystring(struct querystring **qs, char *qst)
460 const struct got_error *error = NULL;
461 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
462 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
464 if (qst == NULL)
465 return error;
467 tok1 = strdup(qst);
468 if (tok1 == NULL)
469 return got_error_from_errno2("%s: strdup", __func__);
471 tok1_pair = tok1;
472 tok1_end = tok1;
474 while (tok1_pair != NULL) {
475 strsep(&tok1_end, "&");
477 tok2 = strdup(tok1_pair);
478 if (tok2 == NULL) {
479 free(tok1);
480 return got_error_from_errno2("%s: strdup", __func__);
483 tok2_pair = tok2;
484 tok2_end = tok2;
486 while (tok2_pair != NULL) {
487 strsep(&tok2_end, "=");
488 if (tok2_end) {
489 error = gotweb_assign_querystring(qs, tok2_pair,
490 tok2_end);
491 if (error)
492 goto err;
494 tok2_pair = tok2_end;
496 free(tok2);
497 tok1_pair = tok1_end;
499 free(tok1);
500 return error;
501 err:
502 free(tok2);
503 free(tok1);
504 return error;
507 /*
508 * Adapted from usr.sbin/httpd/httpd.c url_decode.
509 */
510 static const struct got_error *
511 gotweb_urldecode(char *url)
513 char *p, *q;
514 char hex[3];
515 unsigned long x;
517 hex[2] = '\0';
518 p = q = url;
520 while (*p != '\0') {
521 switch (*p) {
522 case '%':
523 /* Encoding character is followed by two hex chars */
524 if (!isxdigit((unsigned char)p[1]) ||
525 !isxdigit((unsigned char)p[2]) ||
526 (p[1] == '0' && p[2] == '0'))
527 return got_error(GOT_ERR_BAD_QUERYSTRING);
529 hex[0] = p[1];
530 hex[1] = p[2];
532 /*
533 * We don't have to validate "hex" because it is
534 * guaranteed to include two hex chars followed by nul.
535 */
536 x = strtoul(hex, NULL, 16);
537 *q = (char)x;
538 p += 2;
539 break;
540 default:
541 *q = *p;
542 break;
544 p++;
545 q++;
547 *q = '\0';
549 return NULL;
552 static const struct got_error *
553 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
555 const struct got_error *error = NULL;
556 const char *errstr;
557 int a_cnt, el_cnt;
559 error = gotweb_urldecode(value);
560 if (error)
561 return error;
563 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
564 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
565 continue;
567 switch (querystring_keys[el_cnt].element) {
568 case ACTION:
569 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
570 if (strcmp(value, action_keys[a_cnt].name) != 0)
571 continue;
572 else if (strcmp(value,
573 action_keys[a_cnt].name) == 0){
574 (*qs)->action =
575 action_keys[a_cnt].action;
576 goto qa_found;
579 (*qs)->action = ERR;
580 qa_found:
581 break;
582 case COMMIT:
583 (*qs)->commit = strdup(value);
584 if ((*qs)->commit == NULL) {
585 error = got_error_from_errno2("%s: strdup",
586 __func__);
587 goto done;
589 break;
590 case RFILE:
591 (*qs)->file = strdup(value);
592 if ((*qs)->file == NULL) {
593 error = got_error_from_errno2("%s: strdup",
594 __func__);
595 goto done;
597 break;
598 case FOLDER:
599 (*qs)->folder = strdup(value);
600 if ((*qs)->folder == NULL) {
601 error = got_error_from_errno2("%s: strdup",
602 __func__);
603 goto done;
605 break;
606 case HEADREF:
607 free((*qs)->headref);
608 (*qs)->headref = strdup(value);
609 if ((*qs)->headref == NULL) {
610 error = got_error_from_errno2("%s: strdup",
611 __func__);
612 goto done;
614 break;
615 case INDEX_PAGE:
616 if (strlen(value) == 0)
617 break;
618 (*qs)->index_page = strtonum(value, INT64_MIN,
619 INT64_MAX, &errstr);
620 if (errstr) {
621 error = got_error_from_errno3("%s: strtonum %s",
622 __func__, errstr);
623 goto done;
625 if ((*qs)->index_page < 0)
626 (*qs)->index_page = 0;
627 break;
628 case PATH:
629 (*qs)->path = strdup(value);
630 if ((*qs)->path == NULL) {
631 error = got_error_from_errno2("%s: strdup",
632 __func__);
633 goto done;
635 break;
636 case PAGE:
637 if (strlen(value) == 0)
638 break;
639 (*qs)->page = strtonum(value, INT64_MIN,
640 INT64_MAX, &errstr);
641 if (errstr) {
642 error = got_error_from_errno3("%s: strtonum %s",
643 __func__, errstr);
644 goto done;
646 if ((*qs)->page < 0)
647 (*qs)->page = 0;
648 break;
649 default:
650 break;
653 done:
654 return error;
657 void
658 gotweb_free_repo_tag(struct repo_tag *rt)
660 if (rt != NULL) {
661 free(rt->commit_id);
662 free(rt->tag_name);
663 free(rt->tag_commit);
664 free(rt->commit_msg);
665 free(rt->tagger);
667 free(rt);
670 void
671 gotweb_free_repo_commit(struct repo_commit *rc)
673 if (rc != NULL) {
674 free(rc->path);
675 free(rc->refs_str);
676 free(rc->commit_id);
677 free(rc->parent_id);
678 free(rc->tree_id);
679 free(rc->author);
680 free(rc->committer);
681 free(rc->commit_msg);
683 free(rc);
686 static void
687 gotweb_free_querystring(struct querystring *qs)
689 if (qs != NULL) {
690 free(qs->commit);
691 free(qs->file);
692 free(qs->folder);
693 free(qs->headref);
694 free(qs->path);
696 free(qs);
699 static void
700 gotweb_free_repo_dir(struct repo_dir *repo_dir)
702 if (repo_dir != NULL) {
703 free(repo_dir->name);
704 free(repo_dir->owner);
705 free(repo_dir->description);
706 free(repo_dir->url);
707 free(repo_dir->path);
709 free(repo_dir);
712 void
713 gotweb_free_transport(struct transport *t)
715 const struct got_error *err;
716 struct repo_commit *rc = NULL, *trc = NULL;
717 struct repo_tag *rt = NULL, *trt = NULL;
718 int i;
720 got_ref_list_free(&t->refs);
721 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
722 TAILQ_REMOVE(&t->repo_commits, rc, entry);
723 gotweb_free_repo_commit(rc);
725 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
726 TAILQ_REMOVE(&t->repo_tags, rt, entry);
727 gotweb_free_repo_tag(rt);
729 gotweb_free_repo_dir(t->repo_dir);
730 gotweb_free_querystring(t->qs);
731 free(t->more_id);
732 free(t->next_id);
733 free(t->prev_id);
734 if (t->blob)
735 got_object_blob_close(t->blob);
736 if (t->fp) {
737 err = got_gotweb_closefile(t->fp);
738 if (err)
739 log_warnx("%s: got_gotweb_closefile failure: %s",
740 __func__, err->msg);
742 if (t->fd != -1 && close(t->fd) == -1)
743 log_warn("%s: close", __func__);
744 if (t->repos) {
745 for (i = 0; i < t->nrepos; ++i)
746 free(t->repos[i]);
747 free(t->repos);
749 free(t);
752 void
753 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
754 struct gotweb_url *next, int *have_next)
756 struct transport *t = c->t;
757 struct querystring *qs = t->qs;
758 struct server *srv = c->srv;
760 *have_prev = *have_next = 0;
762 switch(qs->action) {
763 case INDEX:
764 if (qs->index_page > 0) {
765 *have_prev = 1;
766 *prev = (struct gotweb_url){
767 .action = -1,
768 .index_page = qs->index_page - 1,
769 .page = -1,
770 };
772 if (t->next_disp == srv->max_repos_display &&
773 t->repos_total != (qs->index_page + 1) *
774 srv->max_repos_display) {
775 *have_next = 1;
776 *next = (struct gotweb_url){
777 .action = -1,
778 .index_page = qs->index_page + 1,
779 .page = -1,
780 };
782 break;
783 case TAGS:
784 if (t->prev_id && qs->commit != NULL &&
785 strcmp(qs->commit, t->prev_id) != 0) {
786 *have_prev = 1;
787 *prev = (struct gotweb_url){
788 .action = TAGS,
789 .index_page = -1,
790 .page = qs->page - 1,
791 .path = qs->path,
792 .commit = t->prev_id,
793 .headref = qs->headref,
794 };
796 if (t->next_id) {
797 *have_next = 1;
798 *next = (struct gotweb_url){
799 .action = TAGS,
800 .index_page = -1,
801 .page = qs->page + 1,
802 .path = qs->path,
803 .commit = t->next_id,
804 .headref = qs->headref,
805 };
807 break;
811 static int
812 gotweb_render_index(struct template *tp)
814 const struct got_error *error = NULL;
815 struct request *c = tp->tp_arg;
816 struct server *srv = c->srv;
817 struct transport *t = c->t;
818 struct querystring *qs = t->qs;
819 struct repo_dir *repo_dir = NULL;
820 struct dirent **sd_dent = t->repos;
821 unsigned int d_i, d_disp = 0;
822 unsigned int d_skipped = 0;
823 int type, r;
825 if (gotweb_render_repo_table_hdr(c->tp) == -1)
826 return -1;
828 for (d_i = 0; d_i < t->nrepos; d_i++) {
829 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
830 break;
832 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
833 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
834 d_skipped++;
835 continue;
838 error = got_path_dirent_type(&type, srv->repos_path,
839 sd_dent[d_i]);
840 if (error)
841 continue;
842 if (type != DT_DIR) {
843 d_skipped++;
844 continue;
847 if (qs->index_page > 0 && (qs->index_page *
848 srv->max_repos_display) > t->prev_disp) {
849 t->prev_disp++;
850 continue;
853 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
854 if (error)
855 continue;
857 error = gotweb_load_got_path(c, repo_dir);
858 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
859 if (error->code != GOT_ERR_NOT_GIT_REPO)
860 log_warnx("%s: %s: %s", __func__,
861 sd_dent[d_i]->d_name, error->msg);
862 gotweb_free_repo_dir(repo_dir);
863 repo_dir = NULL;
864 d_skipped++;
865 continue;
868 d_disp++;
869 t->prev_disp++;
871 r = gotweb_render_repo_fragment(c->tp, repo_dir);
872 gotweb_free_repo_dir(repo_dir);
873 if (r == -1)
874 return -1;
876 t->next_disp++;
877 if (d_disp == srv->max_repos_display)
878 break;
880 t->repos_total = t->nrepos - d_skipped;
882 if (srv->max_repos_display == 0)
883 return 0;
884 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
885 return 0;
886 if (t->repos_total <= srv->max_repos ||
887 t->repos_total <= srv->max_repos_display)
888 return 0;
890 if (gotweb_render_navs(c->tp) == -1)
891 return -1;
893 return 0;
896 static inline int
897 should_urlencode(int c)
899 if (c <= ' ' || c >= 127)
900 return 1;
902 switch (c) {
903 /* gen-delim */
904 case ':':
905 case '/':
906 case '?':
907 case '#':
908 case '[':
909 case ']':
910 case '@':
911 /* sub-delims */
912 case '!':
913 case '$':
914 case '&':
915 case '\'':
916 case '(':
917 case ')':
918 case '*':
919 case '+':
920 case ',':
921 case ';':
922 case '=':
923 /* needed because the URLs are embedded into the HTML */
924 case '\"':
925 return 1;
926 default:
927 return 0;
931 static char *
932 gotweb_urlencode(const char *str)
934 const char *s;
935 char *escaped;
936 size_t i, len;
937 int a, b;
939 len = 0;
940 for (s = str; *s; ++s) {
941 len++;
942 if (should_urlencode(*s))
943 len += 2;
946 escaped = calloc(1, len + 1);
947 if (escaped == NULL)
948 return NULL;
950 i = 0;
951 for (s = str; *s; ++s) {
952 if (should_urlencode(*s)) {
953 a = (*s & 0xF0) >> 4;
954 b = (*s & 0x0F);
956 escaped[i++] = '%';
957 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
958 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
959 } else
960 escaped[i++] = *s;
963 return escaped;
966 const char *
967 gotweb_action_name(int action)
969 switch (action) {
970 case BLAME:
971 return "blame";
972 case BLOB:
973 return "blob";
974 case BLOBRAW:
975 return "blobraw";
976 case BRIEFS:
977 return "briefs";
978 case COMMITS:
979 return "commits";
980 case DIFF:
981 return "diff";
982 case ERR:
983 return "err";
984 case INDEX:
985 return "index";
986 case SUMMARY:
987 return "summary";
988 case TAG:
989 return "tag";
990 case TAGS:
991 return "tags";
992 case TREE:
993 return "tree";
994 case RSS:
995 return "rss";
996 default:
997 return NULL;
1001 int
1002 gotweb_render_url(struct request *c, struct gotweb_url *url)
1004 const char *sep = "?", *action;
1005 char *tmp;
1006 int r;
1008 action = gotweb_action_name(url->action);
1009 if (action != NULL) {
1010 if (fcgi_printf(c, "?action=%s", action) == -1)
1011 return -1;
1012 sep = "&";
1015 if (url->commit) {
1016 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1017 return -1;
1018 sep = "&";
1021 if (url->previd) {
1022 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1023 return -1;
1024 sep = "&";
1027 if (url->prevset) {
1028 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1029 return -1;
1030 sep = "&";
1033 if (url->file) {
1034 tmp = gotweb_urlencode(url->file);
1035 if (tmp == NULL)
1036 return -1;
1037 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1038 free(tmp);
1039 if (r == -1)
1040 return -1;
1041 sep = "&";
1044 if (url->folder) {
1045 tmp = gotweb_urlencode(url->folder);
1046 if (tmp == NULL)
1047 return -1;
1048 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1049 free(tmp);
1050 if (r == -1)
1051 return -1;
1052 sep = "&";
1055 if (url->headref) {
1056 tmp = gotweb_urlencode(url->headref);
1057 if (tmp == NULL)
1058 return -1;
1059 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1060 free(tmp);
1061 if (r == -1)
1062 return -1;
1063 sep = "&";
1066 if (url->index_page != -1) {
1067 if (fcgi_printf(c, "%sindex_page=%d", sep,
1068 url->index_page) == -1)
1069 return -1;
1070 sep = "&";
1073 if (url->path) {
1074 tmp = gotweb_urlencode(url->path);
1075 if (tmp == NULL)
1076 return -1;
1077 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1078 free(tmp);
1079 if (r == -1)
1080 return -1;
1081 sep = "&";
1084 if (url->page != -1) {
1085 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1086 return -1;
1087 sep = "&";
1090 return 0;
1093 int
1094 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1096 struct template *tp = c->tp;
1097 const char *proto = c->https ? "https" : "http";
1099 if (fcgi_puts(tp, proto) == -1 ||
1100 fcgi_puts(tp, "://") == -1 ||
1101 tp_htmlescape(tp, c->server_name) == -1 ||
1102 tp_htmlescape(tp, c->document_uri) == -1)
1103 return -1;
1105 return gotweb_render_url(c, url);
1108 static struct got_repository *
1109 find_cached_repo(struct server *srv, const char *path)
1111 int i;
1113 for (i = 0; i < srv->ncached_repos; i++) {
1114 if (strcmp(srv->cached_repos[i].path, path) == 0)
1115 return srv->cached_repos[i].repo;
1118 return NULL;
1121 static const struct got_error *
1122 cache_repo(struct got_repository **new, struct server *srv,
1123 struct repo_dir *repo_dir, struct socket *sock)
1125 const struct got_error *error = NULL;
1126 struct got_repository *repo;
1127 struct cached_repo *cr;
1128 int evicted = 0;
1130 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1131 cr = &srv->cached_repos[srv->ncached_repos - 1];
1132 error = got_repo_close(cr->repo);
1133 memset(cr, 0, sizeof(*cr));
1134 srv->ncached_repos--;
1135 if (error)
1136 return error;
1137 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1138 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1139 cr = &srv->cached_repos[0];
1140 evicted = 1;
1141 } else {
1142 cr = &srv->cached_repos[srv->ncached_repos];
1145 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1146 if (error) {
1147 if (evicted) {
1148 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1149 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1151 return error;
1154 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1155 >= sizeof(cr->path)) {
1156 if (evicted) {
1157 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1158 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1160 return got_error(GOT_ERR_NO_SPACE);
1163 cr->repo = repo;
1164 srv->ncached_repos++;
1165 *new = repo;
1166 return NULL;
1169 static const struct got_error *
1170 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1172 const struct got_error *error = NULL;
1173 struct socket *sock = c->sock;
1174 struct server *srv = c->srv;
1175 struct transport *t = c->t;
1176 struct got_repository *repo = NULL;
1177 DIR *dt;
1178 char *dir_test;
1180 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1181 GOTWEB_GIT_DIR) == -1)
1182 return got_error_from_errno("asprintf");
1184 dt = opendir(dir_test);
1185 if (dt == NULL) {
1186 free(dir_test);
1187 } else {
1188 repo_dir->path = dir_test;
1189 dir_test = NULL;
1190 goto done;
1193 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1194 repo_dir->name) == -1)
1195 return got_error_from_errno("asprintf");
1197 dt = opendir(dir_test);
1198 if (dt == NULL) {
1199 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1200 goto err;
1201 } else {
1202 repo_dir->path = dir_test;
1203 dir_test = NULL;
1206 done:
1207 if (srv->respect_exportok &&
1208 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1209 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1210 goto err;
1213 repo = find_cached_repo(srv, repo_dir->path);
1214 if (repo == NULL) {
1215 error = cache_repo(&repo, srv, repo_dir, sock);
1216 if (error)
1217 goto err;
1219 t->repo = repo;
1220 error = gotweb_get_repo_description(&repo_dir->description, srv,
1221 repo_dir->path, dirfd(dt));
1222 if (error)
1223 goto err;
1224 error = got_get_repo_owner(&repo_dir->owner, c);
1225 if (error)
1226 goto err;
1227 error = got_get_repo_age(&repo_dir->age, c, NULL);
1228 if (error)
1229 goto err;
1230 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1231 dirfd(dt));
1232 err:
1233 free(dir_test);
1234 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1235 error = got_error_from_errno("closedir");
1236 return error;
1239 static const struct got_error *
1240 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1242 const struct got_error *error;
1244 *repo_dir = calloc(1, sizeof(**repo_dir));
1245 if (*repo_dir == NULL)
1246 return got_error_from_errno("calloc");
1248 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1249 error = got_error_from_errno("asprintf");
1250 free(*repo_dir);
1251 *repo_dir = NULL;
1252 return error;
1254 (*repo_dir)->owner = NULL;
1255 (*repo_dir)->description = NULL;
1256 (*repo_dir)->url = NULL;
1257 (*repo_dir)->path = NULL;
1259 return NULL;
1262 static const struct got_error *
1263 gotweb_get_repo_description(char **description, struct server *srv,
1264 const char *dirpath, int dir)
1266 const struct got_error *error = NULL;
1267 struct stat sb;
1268 int fd = -1;
1269 off_t len;
1271 *description = NULL;
1272 if (srv->show_repo_description == 0)
1273 return NULL;
1275 fd = openat(dir, "description", O_RDONLY);
1276 if (fd == -1) {
1277 if (errno != ENOENT && errno != EACCES) {
1278 error = got_error_from_errno_fmt("openat %s/%s",
1279 dirpath, "description");
1281 goto done;
1284 if (fstat(fd, &sb) == -1) {
1285 error = got_error_from_errno_fmt("fstat %s/%s",
1286 dirpath, "description");
1287 goto done;
1290 len = sb.st_size;
1291 if (len > GOTWEBD_MAXDESCRSZ - 1)
1292 len = GOTWEBD_MAXDESCRSZ - 1;
1294 *description = calloc(len + 1, sizeof(**description));
1295 if (*description == NULL) {
1296 error = got_error_from_errno("calloc");
1297 goto done;
1300 if (read(fd, *description, len) == -1)
1301 error = got_error_from_errno("read");
1302 done:
1303 if (fd != -1 && close(fd) == -1 && error == NULL)
1304 error = got_error_from_errno("close");
1305 return error;
1308 static const struct got_error *
1309 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1310 int dir)
1312 const struct got_error *error = NULL;
1313 struct stat sb;
1314 int fd = -1;
1315 off_t len;
1317 *url = NULL;
1318 if (srv->show_repo_cloneurl == 0)
1319 return NULL;
1321 fd = openat(dir, "cloneurl", O_RDONLY);
1322 if (fd == -1) {
1323 if (errno != ENOENT && errno != EACCES) {
1324 error = got_error_from_errno_fmt("openat %s/%s",
1325 dirpath, "cloneurl");
1327 goto done;
1330 if (fstat(fd, &sb) == -1) {
1331 error = got_error_from_errno_fmt("fstat %s/%s",
1332 dirpath, "cloneurl");
1333 goto done;
1336 len = sb.st_size;
1337 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1338 len = GOTWEBD_MAXCLONEURLSZ - 1;
1340 *url = calloc(len + 1, sizeof(**url));
1341 if (*url == NULL) {
1342 error = got_error_from_errno("calloc");
1343 goto done;
1346 if (read(fd, *url, len) == -1)
1347 error = got_error_from_errno("read");
1348 done:
1349 if (fd != -1 && close(fd) == -1 && error == NULL)
1350 error = got_error_from_errno("close");
1351 return error;
1354 int
1355 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1357 struct request *c = tp->tp_arg;
1358 struct tm tm;
1359 long long diff_time;
1360 const char *years = "years ago", *months = "months ago";
1361 const char *weeks = "weeks ago", *days = "days ago";
1362 const char *hours = "hours ago", *minutes = "minutes ago";
1363 const char *seconds = "seconds ago", *now = "right now";
1364 char *s;
1365 char datebuf[64];
1366 size_t r;
1368 switch (ref_tm) {
1369 case TM_DIFF:
1370 diff_time = time(NULL) - committer_time;
1371 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1372 if (fcgi_printf(c, "%lld %s",
1373 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1374 return -1;
1375 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1376 if (fcgi_printf(c, "%lld %s",
1377 (diff_time / 60 / 60 / 24 / (365 / 12)),
1378 months) == -1)
1379 return -1;
1380 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1381 if (fcgi_printf(c, "%lld %s",
1382 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1383 return -1;
1384 } else if (diff_time > 60 * 60 * 24 * 2) {
1385 if (fcgi_printf(c, "%lld %s",
1386 (diff_time / 60 / 60 / 24), days) == -1)
1387 return -1;
1388 } else if (diff_time > 60 * 60 * 2) {
1389 if (fcgi_printf(c, "%lld %s",
1390 (diff_time / 60 / 60), hours) == -1)
1391 return -1;
1392 } else if (diff_time > 60 * 2) {
1393 if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1394 minutes) == -1)
1395 return -1;
1396 } else if (diff_time > 2) {
1397 if (fcgi_printf(c, "%lld %s", diff_time,
1398 seconds) == -1)
1399 return -1;
1400 } else {
1401 if (fcgi_puts(tp, now) == -1)
1402 return -1;
1404 break;
1405 case TM_LONG:
1406 if (gmtime_r(&committer_time, &tm) == NULL)
1407 return -1;
1409 s = asctime_r(&tm, datebuf);
1410 if (s == NULL)
1411 return -1;
1413 if (fcgi_puts(tp, datebuf) == -1 ||
1414 fcgi_puts(tp, " UTC") == -1)
1415 return -1;
1416 break;
1417 case TM_RFC822:
1418 if (gmtime_r(&committer_time, &tm) == NULL)
1419 return -1;
1421 r = strftime(datebuf, sizeof(datebuf),
1422 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1423 if (r == 0)
1424 return -1;
1426 if (fcgi_puts(tp, datebuf) == -1)
1427 return -1;
1428 break;
1430 return 0;