Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <fcntl.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <sha2.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_diff.h"
48 #include "got_commit_graph.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
52 #include "gotwebd.h"
53 #include "tmpl.h"
55 static const struct querystring_keys querystring_keys[] = {
56 { "action", ACTION },
57 { "commit", COMMIT },
58 { "file", RFILE },
59 { "folder", FOLDER },
60 { "headref", HEADREF },
61 { "index_page", INDEX_PAGE },
62 { "path", PATH },
63 };
65 static const struct action_keys action_keys[] = {
66 { "blame", BLAME },
67 { "blob", BLOB },
68 { "blobraw", BLOBRAW },
69 { "briefs", BRIEFS },
70 { "commits", COMMITS },
71 { "diff", DIFF },
72 { "error", ERR },
73 { "index", INDEX },
74 { "patch", PATCH },
75 { "summary", SUMMARY },
76 { "tag", TAG },
77 { "tags", TAGS },
78 { "tree", TREE },
79 { "rss", RSS },
80 };
82 static const struct got_error *gotweb_init_querystring(struct querystring **);
83 static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 char *);
85 static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 char *, char *);
87 static int gotweb_render_index(struct template *);
88 static const struct got_error *gotweb_load_got_path(struct repo_dir **,
89 const char *, struct request *);
90 static const struct got_error *gotweb_get_repo_description(char **,
91 struct server *, const char *, int);
92 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
93 const char *, int);
95 static void gotweb_free_querystring(struct querystring *);
96 static void gotweb_free_repo_dir(struct repo_dir *);
98 struct server *gotweb_get_server(const char *);
100 static int
101 gotweb_reply(struct request *c, int status, const char *ctype,
102 struct gotweb_url *location)
104 const char *csp;
106 if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
107 return -1;
109 if (location) {
110 if (tp_writes(c->tp, "Location: ") == -1 ||
111 gotweb_render_url(c, location) == -1 ||
112 tp_writes(c->tp, "\r\n") == -1)
113 return -1;
116 csp = "Content-Security-Policy: default-src 'self'; "
117 "script-src 'none'; object-src 'none';\r\n";
118 if (tp_writes(c->tp, csp) == -1)
119 return -1;
121 if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
122 return -1;
124 return tp_writes(c->tp, "\r\n");
127 static int
128 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
129 const char *suffix)
131 int r;
133 r = tp_writef(c->tp, "Content-Disposition: attachment; "
134 "filename=%s%s\r\n", file, suffix ? suffix : "");
135 if (r == -1)
136 return -1;
137 return gotweb_reply(c, 200, ctype, NULL);
140 void
141 gotweb_process_request(struct request *c)
143 const struct got_error *error = NULL;
144 struct server *srv = NULL;
145 struct querystring *qs = NULL;
146 struct repo_dir *repo_dir = NULL;
147 const char *rss_ctype = "application/rss+xml;charset=utf-8";
148 const uint8_t *buf;
149 size_t len;
150 int r, binary = 0;
152 /* init the transport */
153 error = gotweb_init_transport(&c->t);
154 if (error) {
155 log_warnx("%s: %s", __func__, error->msg);
156 return;
158 /* don't process any further if client disconnected */
159 if (c->sock->client_status == CLIENT_DISCONNECT)
160 return;
161 /* get the gotwebd server */
162 srv = gotweb_get_server(c->server_name);
163 if (srv == NULL) {
164 log_warnx("%s: error server is NULL", __func__);
165 goto err;
167 c->srv = srv;
168 /* parse our querystring */
169 error = gotweb_init_querystring(&qs);
170 if (error) {
171 log_warnx("%s: %s", __func__, error->msg);
172 goto err;
174 c->t->qs = qs;
175 error = gotweb_parse_querystring(&qs, c->querystring);
176 if (error) {
177 log_warnx("%s: %s", __func__, error->msg);
178 goto err;
181 /*
182 * certain actions require a commit id in the querystring. this stops
183 * bad actors from exploiting this by manually manipulating the
184 * querystring.
185 */
187 if (qs->action == BLAME || qs->action == BLOB ||
188 qs->action == BLOBRAW || qs->action == DIFF ||
189 qs->action == PATCH) {
190 if (qs->commit == NULL) {
191 error = got_error(GOT_ERR_BAD_QUERYSTRING);
192 goto err;
196 if (qs->action != INDEX) {
197 error = gotweb_load_got_path(&repo_dir, qs->path, c);
198 c->t->repo_dir = repo_dir;
199 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
200 goto err;
203 if (qs->action == BLOBRAW || qs->action == BLOB) {
204 if (qs->folder == NULL || qs->file == NULL) {
205 error = got_error(GOT_ERR_BAD_QUERYSTRING);
206 goto err;
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 if (qs->folder == NULL || qs->file == NULL) {
222 error = got_error(GOT_ERR_BAD_QUERYSTRING);
223 goto err;
225 error = got_get_repo_commits(c, 1);
226 if (error) {
227 log_warnx("%s: %s", __func__, error->msg);
228 goto err;
230 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
231 return;
232 gotweb_render_page(c->tp, gotweb_render_blame);
233 return;
234 case BLOB:
235 if (binary) {
236 struct gotweb_url url = {
237 .index_page = -1,
238 .action = BLOBRAW,
239 .path = qs->path,
240 .commit = qs->commit,
241 .folder = qs->folder,
242 .file = qs->file,
243 };
245 gotweb_reply(c, 302, NULL, &url);
246 return;
249 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
250 return;
251 gotweb_render_page(c->tp, gotweb_render_blob);
252 return;
253 case BLOBRAW:
254 if (binary)
255 r = gotweb_reply_file(c, "application/octet-stream",
256 qs->file, NULL);
257 else
258 r = gotweb_reply(c, 200, "text/plain", NULL);
259 if (r == -1)
260 return;
261 if (template_flush(c->tp) == -1)
262 return;
264 for (;;) {
265 error = got_object_blob_read_block(&len, c->t->blob);
266 if (error)
267 break;
268 if (len == 0)
269 break;
270 buf = got_object_blob_get_read_buf(c->t->blob);
271 if (fcgi_write(c, buf, len) == -1)
272 break;
274 return;
275 case BRIEFS:
276 error = got_get_repo_commits(c, srv->max_commits_display);
277 if (error)
278 goto err;
279 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
280 return;
281 gotweb_render_page(c->tp, gotweb_render_briefs);
282 return;
283 case COMMITS:
284 error = got_get_repo_commits(c, srv->max_commits_display);
285 if (error) {
286 log_warnx("%s: %s", __func__, error->msg);
287 goto err;
289 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
290 return;
291 gotweb_render_page(c->tp, gotweb_render_commits);
292 return;
293 case DIFF:
294 error = got_get_repo_commits(c, 1);
295 if (error) {
296 log_warnx("%s: %s", __func__, error->msg);
297 goto err;
299 error = got_open_diff_for_output(&c->t->fp, c);
300 if (error) {
301 log_warnx("%s: %s", __func__, error->msg);
302 goto err;
304 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
305 return;
306 gotweb_render_page(c->tp, gotweb_render_diff);
307 return;
308 case INDEX:
309 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
310 alphasort);
311 if (c->t->nrepos == -1) {
312 c->t->repos = NULL;
313 error = got_error_from_errno2("scandir",
314 srv->repos_path);
315 goto err;
317 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
318 return;
319 gotweb_render_page(c->tp, gotweb_render_index);
320 return;
321 case PATCH:
322 error = got_get_repo_commits(c, 1);
323 if (error) {
324 log_warnx("%s: %s", __func__, error->msg);
325 goto err;
327 error = got_open_diff_for_output(&c->t->fp, c);
328 if (error) {
329 log_warnx("%s: %s", __func__, error->msg);
330 goto err;
332 if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
333 return;
334 gotweb_render_patch(c->tp);
335 return;
336 case RSS:
337 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
338 if (error)
339 goto err;
340 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
341 == -1)
342 return;
343 gotweb_render_rss(c->tp);
344 return;
345 case SUMMARY:
346 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
347 got_ref_cmp_by_name, NULL);
348 if (error) {
349 log_warnx("%s: got_ref_list: %s", __func__,
350 error->msg);
351 goto err;
353 error = got_get_repo_commits(c, srv->summary_commits_display);
354 if (error)
355 goto err;
356 qs->action = TAGS;
357 error = got_get_repo_tags(c, srv->summary_tags_display);
358 if (error) {
359 log_warnx("%s: got_get_repo_tags: %s", __func__,
360 error->msg);
361 goto err;
363 qs->action = SUMMARY;
364 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
365 return;
366 gotweb_render_page(c->tp, gotweb_render_summary);
367 return;
368 case TAG:
369 error = got_get_repo_tags(c, 1);
370 if (error) {
371 log_warnx("%s: %s", __func__, error->msg);
372 goto err;
374 if (c->t->tag_count == 0) {
375 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
376 "bad commit id");
377 goto err;
379 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
380 return;
381 gotweb_render_page(c->tp, gotweb_render_tag);
382 return;
383 case TAGS:
384 error = got_get_repo_tags(c, srv->max_commits_display);
385 if (error) {
386 log_warnx("%s: %s", __func__, error->msg);
387 goto err;
389 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
390 return;
391 gotweb_render_page(c->tp, gotweb_render_tags);
392 return;
393 case TREE:
394 error = got_get_repo_commits(c, 1);
395 if (error) {
396 log_warnx("%s: %s", __func__, error->msg);
397 goto err;
399 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
400 return;
401 gotweb_render_page(c->tp, gotweb_render_tree);
402 return;
403 case ERR:
404 default:
405 error = got_error(GOT_ERR_BAD_QUERYSTRING);
408 err:
409 c->t->error = error;
410 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
411 return;
412 gotweb_render_page(c->tp, gotweb_render_error);
415 struct server *
416 gotweb_get_server(const char *server_name)
418 struct server *srv;
420 /* check against the server name first */
421 if (*server_name != '\0')
422 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
423 if (strcmp(srv->name, server_name) == 0)
424 return srv;
426 /* otherwise, use the first server */
427 return TAILQ_FIRST(&gotwebd_env->servers);
428 };
430 const struct got_error *
431 gotweb_init_transport(struct transport **t)
433 const struct got_error *error = NULL;
435 *t = calloc(1, sizeof(**t));
436 if (*t == NULL)
437 return got_error_from_errno2(__func__, "calloc");
439 TAILQ_INIT(&(*t)->repo_commits);
440 TAILQ_INIT(&(*t)->repo_tags);
441 TAILQ_INIT(&(*t)->refs);
443 (*t)->fd = -1;
445 return error;
448 static const struct got_error *
449 gotweb_init_querystring(struct querystring **qs)
451 const struct got_error *error = NULL;
453 *qs = calloc(1, sizeof(**qs));
454 if (*qs == NULL)
455 return got_error_from_errno2(__func__, "calloc");
457 (*qs)->headref = strdup("HEAD");
458 if ((*qs)->headref == NULL) {
459 free(*qs);
460 *qs = NULL;
461 return got_error_from_errno2(__func__, "strdup");
464 (*qs)->action = INDEX;
466 return error;
469 static const struct got_error *
470 gotweb_parse_querystring(struct querystring **qs, char *qst)
472 const struct got_error *error = NULL;
473 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
474 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
476 if (qst == NULL)
477 return error;
479 tok1 = strdup(qst);
480 if (tok1 == NULL)
481 return got_error_from_errno2(__func__, "strdup");
483 tok1_pair = tok1;
484 tok1_end = tok1;
486 while (tok1_pair != NULL) {
487 strsep(&tok1_end, "&");
489 tok2 = strdup(tok1_pair);
490 if (tok2 == NULL) {
491 free(tok1);
492 return got_error_from_errno2(__func__, "strdup");
495 tok2_pair = tok2;
496 tok2_end = tok2;
498 while (tok2_pair != NULL) {
499 strsep(&tok2_end, "=");
500 if (tok2_end) {
501 error = gotweb_assign_querystring(qs, tok2_pair,
502 tok2_end);
503 if (error)
504 goto err;
506 tok2_pair = tok2_end;
508 free(tok2);
509 tok1_pair = tok1_end;
511 free(tok1);
512 return error;
513 err:
514 free(tok2);
515 free(tok1);
516 return error;
519 /*
520 * Adapted from usr.sbin/httpd/httpd.c url_decode.
521 */
522 static const struct got_error *
523 gotweb_urldecode(char *url)
525 char *p, *q;
526 char hex[3];
527 unsigned long x;
529 hex[2] = '\0';
530 p = q = url;
532 while (*p != '\0') {
533 switch (*p) {
534 case '%':
535 /* Encoding character is followed by two hex chars */
536 if (!isxdigit((unsigned char)p[1]) ||
537 !isxdigit((unsigned char)p[2]) ||
538 (p[1] == '0' && p[2] == '0'))
539 return got_error(GOT_ERR_BAD_QUERYSTRING);
541 hex[0] = p[1];
542 hex[1] = p[2];
544 /*
545 * We don't have to validate "hex" because it is
546 * guaranteed to include two hex chars followed by nul.
547 */
548 x = strtoul(hex, NULL, 16);
549 *q = (char)x;
550 p += 2;
551 break;
552 default:
553 *q = *p;
554 break;
556 p++;
557 q++;
559 *q = '\0';
561 return NULL;
564 static const struct got_error *
565 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
567 const struct got_error *error = NULL;
568 const char *errstr;
569 int a_cnt, el_cnt;
571 error = gotweb_urldecode(value);
572 if (error)
573 return error;
575 for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
576 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
577 continue;
579 switch (querystring_keys[el_cnt].element) {
580 case ACTION:
581 for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
582 if (strcmp(value, action_keys[a_cnt].name) != 0)
583 continue;
584 else if (strcmp(value,
585 action_keys[a_cnt].name) == 0){
586 (*qs)->action =
587 action_keys[a_cnt].action;
588 goto qa_found;
591 (*qs)->action = ERR;
592 qa_found:
593 break;
594 case COMMIT:
595 (*qs)->commit = strdup(value);
596 if ((*qs)->commit == NULL) {
597 error = got_error_from_errno2(__func__,
598 "strdup");
599 goto done;
601 break;
602 case RFILE:
603 (*qs)->file = strdup(value);
604 if ((*qs)->file == NULL) {
605 error = got_error_from_errno2(__func__,
606 "strdup");
607 goto done;
609 break;
610 case FOLDER:
611 (*qs)->folder = strdup(value);
612 if ((*qs)->folder == NULL) {
613 error = got_error_from_errno2(__func__,
614 "strdup");
615 goto done;
617 break;
618 case HEADREF:
619 free((*qs)->headref);
620 (*qs)->headref = strdup(value);
621 if ((*qs)->headref == NULL) {
622 error = got_error_from_errno2(__func__,
623 "strdup");
624 goto done;
626 break;
627 case INDEX_PAGE:
628 if (*value == '\0')
629 break;
630 (*qs)->index_page = strtonum(value, INT64_MIN,
631 INT64_MAX, &errstr);
632 if (errstr) {
633 error = got_error_from_errno3(__func__,
634 "strtonum", errstr);
635 goto done;
637 if ((*qs)->index_page < 0)
638 (*qs)->index_page = 0;
639 break;
640 case PATH:
641 (*qs)->path = strdup(value);
642 if ((*qs)->path == NULL) {
643 error = got_error_from_errno2(__func__,
644 "strdup");
645 goto done;
647 break;
650 /* entry found */
651 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->tags_more_id);
733 if (t->blob)
734 got_object_blob_close(t->blob);
735 if (t->fp) {
736 err = got_gotweb_closefile(t->fp);
737 if (err)
738 log_warnx("%s: got_gotweb_closefile failure: %s",
739 __func__, err->msg);
741 if (t->fd != -1 && close(t->fd) == -1)
742 log_warn("%s: close", __func__);
743 if (t->repos) {
744 for (i = 0; i < t->nrepos; ++i)
745 free(t->repos[i]);
746 free(t->repos);
748 if (t->repo)
749 got_repo_close(t->repo);
750 free(t);
753 void
754 gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
755 struct gotweb_url *next, int *have_next)
757 struct transport *t = c->t;
758 struct querystring *qs = t->qs;
759 struct server *srv = c->srv;
761 *have_prev = *have_next = 0;
763 if (qs->index_page > 0) {
764 *have_prev = 1;
765 *prev = (struct gotweb_url){
766 .action = -1,
767 .index_page = qs->index_page - 1,
768 };
770 if (t->next_disp == srv->max_repos_display &&
771 t->repos_total != (qs->index_page + 1) *
772 srv->max_repos_display) {
773 *have_next = 1;
774 *next = (struct gotweb_url){
775 .action = -1,
776 .index_page = qs->index_page + 1,
777 };
781 static int
782 gotweb_render_index(struct template *tp)
784 const struct got_error *error = NULL;
785 struct request *c = tp->tp_arg;
786 struct server *srv = c->srv;
787 struct transport *t = c->t;
788 struct querystring *qs = t->qs;
789 struct repo_dir *repo_dir = NULL;
790 struct dirent **sd_dent = t->repos;
791 unsigned int d_i, d_disp = 0;
792 unsigned int d_skipped = 0;
793 int type, r;
795 if (gotweb_render_repo_table_hdr(c->tp) == -1)
796 return -1;
798 for (d_i = 0; d_i < t->nrepos; d_i++) {
799 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
800 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
801 d_skipped++;
802 continue;
805 error = got_path_dirent_type(&type, srv->repos_path,
806 sd_dent[d_i]);
807 if (error)
808 continue;
809 if (type != DT_DIR) {
810 d_skipped++;
811 continue;
814 if (qs->index_page > 0 && (qs->index_page *
815 srv->max_repos_display) > t->prev_disp) {
816 t->prev_disp++;
817 continue;
820 error = gotweb_load_got_path(&repo_dir, sd_dent[d_i]->d_name,
821 c);
822 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
823 if (error->code != GOT_ERR_NOT_GIT_REPO)
824 log_warnx("%s: %s: %s", __func__,
825 sd_dent[d_i]->d_name, error->msg);
826 gotweb_free_repo_dir(repo_dir);
827 repo_dir = NULL;
828 d_skipped++;
829 continue;
832 d_disp++;
833 t->prev_disp++;
835 r = gotweb_render_repo_fragment(c->tp, repo_dir);
836 gotweb_free_repo_dir(repo_dir);
837 repo_dir = NULL;
838 got_repo_close(t->repo);
839 t->repo = NULL;
840 if (r == -1)
841 return -1;
843 t->next_disp++;
844 if (d_disp == srv->max_repos_display)
845 break;
847 t->repos_total = t->nrepos - d_skipped;
849 if (srv->max_repos_display == 0 ||
850 t->repos_total <= srv->max_repos_display)
851 return 0;
853 if (gotweb_render_navs(c->tp) == -1)
854 return -1;
856 return 0;
859 static inline int
860 should_urlencode(int c)
862 if (c <= ' ' || c >= 127)
863 return 1;
865 switch (c) {
866 /* gen-delim */
867 case ':':
868 case '/':
869 case '?':
870 case '#':
871 case '[':
872 case ']':
873 case '@':
874 /* sub-delims */
875 case '!':
876 case '$':
877 case '&':
878 case '\'':
879 case '(':
880 case ')':
881 case '*':
882 case '+':
883 case ',':
884 case ';':
885 case '=':
886 /* needed because the URLs are embedded into the HTML */
887 case '\"':
888 return 1;
889 default:
890 return 0;
894 static char *
895 gotweb_urlencode(const char *str)
897 const char *s;
898 char *escaped;
899 size_t i, len;
900 int a, b;
902 len = 0;
903 for (s = str; *s; ++s) {
904 len++;
905 if (should_urlencode(*s))
906 len += 2;
909 escaped = calloc(1, len + 1);
910 if (escaped == NULL)
911 return NULL;
913 i = 0;
914 for (s = str; *s; ++s) {
915 if (should_urlencode(*s)) {
916 a = (*s & 0xF0) >> 4;
917 b = (*s & 0x0F);
919 escaped[i++] = '%';
920 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
921 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
922 } else
923 escaped[i++] = *s;
926 return escaped;
929 const char *
930 gotweb_action_name(int action)
932 switch (action) {
933 case BLAME:
934 return "blame";
935 case BLOB:
936 return "blob";
937 case BLOBRAW:
938 return "blobraw";
939 case BRIEFS:
940 return "briefs";
941 case COMMITS:
942 return "commits";
943 case DIFF:
944 return "diff";
945 case ERR:
946 return "err";
947 case INDEX:
948 return "index";
949 case PATCH:
950 return "patch";
951 case SUMMARY:
952 return "summary";
953 case TAG:
954 return "tag";
955 case TAGS:
956 return "tags";
957 case TREE:
958 return "tree";
959 case RSS:
960 return "rss";
961 default:
962 return NULL;
966 int
967 gotweb_render_url(struct request *c, struct gotweb_url *url)
969 const char *sep = "?", *action;
970 char *tmp;
971 int r;
973 action = gotweb_action_name(url->action);
974 if (action != NULL) {
975 if (tp_writef(c->tp, "?action=%s", action) == -1)
976 return -1;
977 sep = "&";
980 if (url->commit) {
981 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
982 return -1;
983 sep = "&";
986 if (url->previd) {
987 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
988 return -1;
989 sep = "&";
992 if (url->prevset) {
993 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
994 return -1;
995 sep = "&";
998 if (url->file) {
999 tmp = gotweb_urlencode(url->file);
1000 if (tmp == NULL)
1001 return -1;
1002 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1003 free(tmp);
1004 if (r == -1)
1005 return -1;
1006 sep = "&";
1009 if (url->folder) {
1010 tmp = gotweb_urlencode(url->folder);
1011 if (tmp == NULL)
1012 return -1;
1013 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1014 free(tmp);
1015 if (r == -1)
1016 return -1;
1017 sep = "&";
1020 if (url->headref) {
1021 tmp = gotweb_urlencode(url->headref);
1022 if (tmp == NULL)
1023 return -1;
1024 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1025 free(tmp);
1026 if (r == -1)
1027 return -1;
1028 sep = "&";
1031 if (url->index_page != -1) {
1032 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1033 url->index_page) == -1)
1034 return -1;
1035 sep = "&";
1038 if (url->path) {
1039 tmp = gotweb_urlencode(url->path);
1040 if (tmp == NULL)
1041 return -1;
1042 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1043 free(tmp);
1044 if (r == -1)
1045 return -1;
1046 sep = "&";
1049 return 0;
1052 int
1053 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1055 struct template *tp = c->tp;
1056 const char *proto = c->https ? "https" : "http";
1058 if (tp_writes(tp, proto) == -1 ||
1059 tp_writes(tp, "://") == -1 ||
1060 tp_htmlescape(tp, c->server_name) == -1 ||
1061 tp_htmlescape(tp, c->document_uri) == -1)
1062 return -1;
1064 return gotweb_render_url(c, url);
1067 static const struct got_error *
1068 gotweb_load_got_path(struct repo_dir **rp, const char *dir,
1069 struct request *c)
1071 const struct got_error *error = NULL;
1072 struct socket *sock = c->sock;
1073 struct server *srv = c->srv;
1074 struct transport *t = c->t;
1075 struct repo_dir *repo_dir;
1076 DIR *dt;
1077 char *dir_test;
1079 *rp = calloc(1, sizeof(**rp));
1080 if (*rp == NULL)
1081 return got_error_from_errno("calloc");
1082 repo_dir = *rp;
1084 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, dir,
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, dir) == -1)
1092 return got_error_from_errno("asprintf");
1093 dt = opendir(dir_test);
1094 if (dt == NULL) {
1095 free(dir_test);
1096 if (asprintf(&dir_test, "%s/%s%s", srv->repos_path,
1097 dir, GOTWEB_GIT_DIR) == -1)
1098 return got_error_from_errno("asprintf");
1099 dt = opendir(dir_test);
1100 if (dt == NULL) {
1101 free(dir_test);
1102 return got_error_path(dir,
1103 GOT_ERR_NOT_GIT_REPO);
1108 repo_dir->path = dir_test;
1109 dir_test = NULL;
1111 repo_dir->name = strdup(repo_dir->path + strlen(srv->repos_path) + 1);
1112 if (repo_dir->name == NULL) {
1113 error = got_error_from_errno("strdup");
1114 goto err;
1117 if (srv->respect_exportok &&
1118 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1119 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1120 goto err;
1123 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1124 if (error)
1125 goto err;
1126 error = gotweb_get_repo_description(&repo_dir->description, srv,
1127 repo_dir->path, dirfd(dt));
1128 if (error)
1129 goto err;
1130 error = got_get_repo_owner(&repo_dir->owner, c);
1131 if (error)
1132 goto err;
1133 if (srv->show_repo_age) {
1134 error = got_get_repo_age(&repo_dir->age, c, NULL);
1135 if (error)
1136 goto err;
1138 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1139 dirfd(dt));
1140 err:
1141 free(dir_test);
1142 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1143 error = got_error_from_errno("closedir");
1144 if (error && t->repo) {
1145 got_repo_close(t->repo);
1146 t->repo = NULL;
1148 return error;
1151 static const struct got_error *
1152 gotweb_get_repo_description(char **description, struct server *srv,
1153 const char *dirpath, int dir)
1155 const struct got_error *error = NULL;
1156 struct stat sb;
1157 int fd = -1;
1158 off_t len;
1160 *description = NULL;
1161 if (srv->show_repo_description == 0)
1162 return NULL;
1164 fd = openat(dir, "description", O_RDONLY);
1165 if (fd == -1) {
1166 if (errno != ENOENT && errno != EACCES) {
1167 error = got_error_from_errno_fmt("openat %s/%s",
1168 dirpath, "description");
1170 goto done;
1173 if (fstat(fd, &sb) == -1) {
1174 error = got_error_from_errno_fmt("fstat %s/%s",
1175 dirpath, "description");
1176 goto done;
1179 len = sb.st_size;
1180 if (len > GOTWEBD_MAXDESCRSZ - 1)
1181 len = GOTWEBD_MAXDESCRSZ - 1;
1183 *description = calloc(len + 1, sizeof(**description));
1184 if (*description == NULL) {
1185 error = got_error_from_errno("calloc");
1186 goto done;
1189 if (read(fd, *description, len) == -1)
1190 error = got_error_from_errno("read");
1191 done:
1192 if (fd != -1 && close(fd) == -1 && error == NULL)
1193 error = got_error_from_errno("close");
1194 return error;
1197 static const struct got_error *
1198 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1199 int dir)
1201 const struct got_error *error = NULL;
1202 struct stat sb;
1203 int fd = -1;
1204 off_t len;
1206 *url = NULL;
1207 if (srv->show_repo_cloneurl == 0)
1208 return NULL;
1210 fd = openat(dir, "cloneurl", O_RDONLY);
1211 if (fd == -1) {
1212 if (errno != ENOENT && errno != EACCES) {
1213 error = got_error_from_errno_fmt("openat %s/%s",
1214 dirpath, "cloneurl");
1216 goto done;
1219 if (fstat(fd, &sb) == -1) {
1220 error = got_error_from_errno_fmt("fstat %s/%s",
1221 dirpath, "cloneurl");
1222 goto done;
1225 len = sb.st_size;
1226 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1227 len = GOTWEBD_MAXCLONEURLSZ - 1;
1229 *url = calloc(len + 1, sizeof(**url));
1230 if (*url == NULL) {
1231 error = got_error_from_errno("calloc");
1232 goto done;
1235 if (read(fd, *url, len) == -1)
1236 error = got_error_from_errno("read");
1237 done:
1238 if (fd != -1 && close(fd) == -1 && error == NULL)
1239 error = got_error_from_errno("close");
1240 return error;
1243 int
1244 gotweb_render_age(struct template *tp, time_t committer_time)
1246 struct request *c = tp->tp_arg;
1247 long long diff_time;
1248 const char *years = "years ago", *months = "months ago";
1249 const char *weeks = "weeks ago", *days = "days ago";
1250 const char *hours = "hours ago", *minutes = "minutes ago";
1251 const char *seconds = "seconds ago", *now = "right now";
1253 diff_time = time(NULL) - committer_time;
1254 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1255 if (tp_writef(c->tp, "%lld %s",
1256 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1257 return -1;
1258 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1259 if (tp_writef(c->tp, "%lld %s",
1260 (diff_time / 60 / 60 / 24 / (365 / 12)),
1261 months) == -1)
1262 return -1;
1263 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1264 if (tp_writef(c->tp, "%lld %s",
1265 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1266 return -1;
1267 } else if (diff_time > 60 * 60 * 24 * 2) {
1268 if (tp_writef(c->tp, "%lld %s",
1269 (diff_time / 60 / 60 / 24), days) == -1)
1270 return -1;
1271 } else if (diff_time > 60 * 60 * 2) {
1272 if (tp_writef(c->tp, "%lld %s",
1273 (diff_time / 60 / 60), hours) == -1)
1274 return -1;
1275 } else if (diff_time > 60 * 2) {
1276 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1277 minutes) == -1)
1278 return -1;
1279 } else if (diff_time > 2) {
1280 if (tp_writef(c->tp, "%lld %s", diff_time,
1281 seconds) == -1)
1282 return -1;
1283 } else {
1284 if (tp_writes(tp, now) == -1)
1285 return -1;
1287 return 0;