Blob


1 /*
2 * Copyright (c) 2019 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2014, 2015, 2017 Kristaps Dzonsons <kristaps@bsd.lv>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include <got_object.h>
34 #include <got_reference.h>
35 #include <got_repository.h>
36 #include <got_path.h>
37 #include <got_cancel.h>
38 #include <got_worktree.h>
39 #include <got_diff.h>
40 #include <got_commit_graph.h>
41 #include <got_blame.h>
42 #include <got_privsep.h>
43 #include <got_opentemp.h>
45 #include <kcgi.h>
46 #include <kcgihtml.h>
48 #include "gotweb.h"
49 #include "gotweb_ui.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 struct trans {
56 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
57 struct gw_dir *gw_dir;
58 struct gotweb_conf *gw_conf;
59 struct ktemplate *gw_tmpl;
60 struct khtmlreq *gw_html_req;
61 struct kreq *gw_req;
62 char *repo_name;
63 char *repo_path;
64 char *commit;
65 char *repo_file;
66 char *action_name;
67 unsigned int action;
68 unsigned int page;
69 unsigned int repos_total;
70 enum kmime mime;
71 };
73 enum gw_key {
74 KEY_PATH,
75 KEY_ACTION,
76 KEY_COMMIT_ID,
77 KEY_FILE,
78 KEY_PAGE,
79 KEY__MAX
80 };
82 struct gw_dir {
83 TAILQ_ENTRY(gw_dir) entry;
84 char *name;
85 char *owner;
86 char *description;
87 char *url;
88 char *age;
89 char *path;
90 };
92 enum tmpl {
93 TEMPL_HEAD,
94 TEMPL_HEADER,
95 TEMPL_SITEPATH,
96 TEMPL_SITEOWNER,
97 TEMPL_TITLE,
98 TEMPL_SEARCH,
99 TEMPL_CONTENT,
100 TEMPL__MAX
101 };
103 enum ref_tm {
104 TM_DIFF,
105 TM_LONG,
106 };
108 static const char *const templs[TEMPL__MAX] = {
109 "head",
110 "header",
111 "sitepath",
112 "siteowner",
113 "title",
114 "search",
115 "content",
116 };
118 static const struct kvalid gw_keys[KEY__MAX] = {
119 { kvalid_stringne, "path" },
120 { kvalid_stringne, "action" },
121 { kvalid_stringne, "commit" },
122 { kvalid_stringne, "file" },
123 { kvalid_int, "page" },
124 };
126 static struct gw_dir *gw_init_gw_dir(char *);
128 static char *gw_get_repo_description(struct trans *,
129 char *);
130 static char *gw_get_repo_owner(struct trans *,
131 char *);
132 static char *gw_get_repo_age(struct trans *,
133 char *, char *, int);
134 static char *gw_get_repo_shortlog(struct trans *);
135 static char *gw_get_repo_tags(struct trans *);
136 static char *gw_get_repo_heads(struct trans *);
137 static char *gw_get_clone_url(struct trans *, char *);
138 static char *gw_get_got_link(struct trans *);
139 static char *gw_get_site_link(struct trans *);
140 static char *gw_html_escape(const char *);
142 static void gw_display_open(struct trans *, enum khttp,
143 enum kmime);
144 static void gw_display_index(struct trans *,
145 const struct got_error *);
147 static int gw_template(size_t, void *);
149 static const struct got_error* apply_unveil(const char *, const char *);
150 static const struct got_error* gw_load_got_paths(struct trans *);
151 static const struct got_error* gw_load_got_path(struct trans *,
152 struct gw_dir *);
153 static const struct got_error* gw_parse_querystring(struct trans *);
155 static const struct got_error* gw_blame(struct trans *);
156 static const struct got_error* gw_blob(struct trans *);
157 static const struct got_error* gw_blob_diff(struct trans *);
158 static const struct got_error* gw_commit(struct trans *);
159 static const struct got_error* gw_commit_diff(struct trans *);
160 static const struct got_error* gw_history(struct trans *);
161 static const struct got_error* gw_index(struct trans *);
162 static const struct got_error* gw_log(struct trans *);
163 static const struct got_error* gw_raw(struct trans *);
164 static const struct got_error* gw_shortlog(struct trans *);
165 static const struct got_error* gw_snapshot(struct trans *);
166 static const struct got_error* gw_summary(struct trans *);
167 static const struct got_error* gw_tree(struct trans *);
169 struct gw_query_action {
170 unsigned int func_id;
171 const char *func_name;
172 const struct got_error *(*func_main)(struct trans *);
173 char *template;
174 };
176 enum gw_query_actions {
177 GW_BLAME,
178 GW_BLOB,
179 GW_BLOBDIFF,
180 GW_COMMIT,
181 GW_COMMITDIFF,
182 GW_ERR,
183 GW_HISTORY,
184 GW_INDEX,
185 GW_LOG,
186 GW_RAW,
187 GW_SHORTLOG,
188 GW_SNAPSHOT,
189 GW_SUMMARY,
190 GW_TREE
191 };
193 static struct gw_query_action gw_query_funcs[] = {
194 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
195 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
196 { GW_BLOBDIFF, "blobdiff", gw_blob_diff, "gw_tmpl/index.tmpl" },
197 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
198 { GW_COMMITDIFF, "commit_diff", gw_commit_diff, "gw_tmpl/index.tmpl" },
199 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
200 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
201 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
202 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
203 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
204 { GW_SHORTLOG, "shortlog", gw_shortlog, "gw_tmpl/index.tmpl" },
205 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
206 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
207 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
208 };
210 static const struct got_error *
211 apply_unveil(const char *repo_path, const char *repo_file)
213 const struct got_error *err;
215 if (repo_path && repo_file) {
216 char *full_path;
217 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
218 return got_error_from_errno("asprintf unveil");
219 if (unveil(full_path, "r") != 0)
220 return got_error_from_errno2("unveil", full_path);
223 if (repo_path && unveil(repo_path, "r") != 0)
224 return got_error_from_errno2("unveil", repo_path);
226 if (unveil("/tmp", "rwc") != 0)
227 return got_error_from_errno2("unveil", "/tmp");
229 err = got_privsep_unveil_exec_helpers();
230 if (err != NULL)
231 return err;
233 if (unveil(NULL, NULL) != 0)
234 return got_error_from_errno("unveil");
236 return NULL;
239 static const struct got_error *
240 gw_blame(struct trans *gw_trans)
242 const struct got_error *error = NULL;
244 return error;
247 static const struct got_error *
248 gw_blob(struct trans *gw_trans)
250 const struct got_error *error = NULL;
252 return error;
255 static const struct got_error *
256 gw_blob_diff(struct trans *gw_trans)
258 const struct got_error *error = NULL;
260 return error;
263 static const struct got_error *
264 gw_commit(struct trans *gw_trans)
266 const struct got_error *error = NULL;
268 return error;
271 static const struct got_error *
272 gw_commit_diff(struct trans *gw_trans)
274 const struct got_error *error = NULL;
276 return error;
279 static const struct got_error *
280 gw_history(struct trans *gw_trans)
282 const struct got_error *error = NULL;
284 return error;
287 static const struct got_error *
288 gw_index(struct trans *gw_trans)
290 const struct got_error *error = NULL;
291 struct gw_dir *gw_dir = NULL;
292 char *html, *navs, *next, *prev;
293 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
295 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
296 if (error)
297 return error;
299 error = gw_load_got_paths(gw_trans);
300 if (error)
301 return error;
303 khttp_puts(gw_trans->gw_req, index_projects_header);
305 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
306 dir_c++;
308 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
309 if (gw_trans->page > 0 && (gw_trans->page *
310 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
311 prev_disp++;
312 continue;
315 prev_disp++;
316 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
317 gw_dir->name, gw_dir->name)) == -1)
318 return got_error_from_errno("asprintf");
320 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
321 gw_dir->description, gw_dir->owner, gw_dir->age,
322 navs)) == -1)
323 return got_error_from_errno("asprintf");
325 khttp_puts(gw_trans->gw_req, html);
327 free(navs);
328 free(html);
330 if (gw_trans->gw_conf->got_max_repos_display == 0)
331 continue;
333 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
334 khttp_puts(gw_trans->gw_req, np_wrapper_start);
335 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
336 (gw_trans->page > 0) &&
337 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
338 prev_disp == gw_trans->repos_total))
339 khttp_puts(gw_trans->gw_req, np_wrapper_start);
341 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
342 (gw_trans->page > 0) &&
343 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
344 prev_disp == gw_trans->repos_total)) {
345 if ((asprintf(&prev, nav_prev,
346 gw_trans->page - 1)) == -1)
347 return got_error_from_errno("asprintf");
348 khttp_puts(gw_trans->gw_req, prev);
349 free(prev);
352 khttp_puts(gw_trans->gw_req, div_end);
354 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
355 next_disp == gw_trans->gw_conf->got_max_repos_display &&
356 dir_c != (gw_trans->page + 1) *
357 gw_trans->gw_conf->got_max_repos_display) {
358 if ((asprintf(&next, nav_next,
359 gw_trans->page + 1)) == -1)
360 return got_error_from_errno("calloc");
361 khttp_puts(gw_trans->gw_req, next);
362 khttp_puts(gw_trans->gw_req, div_end);
363 free(next);
364 next_disp = 0;
365 break;
368 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
369 (gw_trans->page > 0) &&
370 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
371 prev_disp == gw_trans->repos_total))
372 khttp_puts(gw_trans->gw_req, div_end);
374 next_disp++;
376 return error;
379 static const struct got_error *
380 gw_log(struct trans *gw_trans)
382 const struct got_error *error = NULL;
384 return error;
387 static const struct got_error *
388 gw_raw(struct trans *gw_trans)
390 const struct got_error *error = NULL;
392 return error;
395 static const struct got_error *
396 gw_shortlog(struct trans *gw_trans)
398 const struct got_error *error = NULL;
400 return error;
403 static const struct got_error *
404 gw_snapshot(struct trans *gw_trans)
406 const struct got_error *error = NULL;
408 return error;
411 static const struct got_error *
412 gw_summary(struct trans *gw_trans)
414 const struct got_error *error = NULL;
415 char *description_html, *repo_owner_html, *repo_age_html,
416 *cloneurl_html, *shortlog, *tags, *heads, *shortlog_html,
417 *tags_html, *heads_html, *age;
419 error = apply_unveil(gw_trans->gw_dir->path, NULL);
420 if (error)
421 return error;
423 khttp_puts(gw_trans->gw_req, summary_wrapper);
424 if (gw_trans->gw_conf->got_show_repo_description) {
425 if (gw_trans->gw_dir->description != NULL &&
426 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
427 if ((asprintf(&description_html, description,
428 gw_trans->gw_dir->description)) == -1)
429 return got_error_from_errno("asprintf");
431 khttp_puts(gw_trans->gw_req, description_html);
432 free(description_html);
436 if (gw_trans->gw_conf->got_show_repo_owner) {
437 if (gw_trans->gw_dir->owner != NULL &&
438 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
439 if ((asprintf(&repo_owner_html, repo_owner,
440 gw_trans->gw_dir->owner)) == -1)
441 return got_error_from_errno("asprintf");
443 khttp_puts(gw_trans->gw_req, repo_owner_html);
444 free(repo_owner_html);
448 if (gw_trans->gw_conf->got_show_repo_age) {
449 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
450 "refs/heads", TM_LONG);
451 if (age != NULL && (strcmp(age, "") != 0)) {
452 if ((asprintf(&repo_age_html, last_change, age)) == -1)
453 return got_error_from_errno("asprintf");
455 khttp_puts(gw_trans->gw_req, repo_age_html);
456 free(repo_age_html);
457 free(age);
461 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
462 if (gw_trans->gw_dir->url != NULL &&
463 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
464 if ((asprintf(&cloneurl_html, cloneurl,
465 gw_trans->gw_dir->url)) == -1)
466 return got_error_from_errno("asprintf");
468 khttp_puts(gw_trans->gw_req, cloneurl_html);
469 free(cloneurl_html);
472 khttp_puts(gw_trans->gw_req, div_end);
474 shortlog = gw_get_repo_shortlog(gw_trans);
475 tags = gw_get_repo_tags(gw_trans);
476 heads = gw_get_repo_heads(gw_trans);
478 if (shortlog != NULL && strcmp(shortlog, "") != 0) {
479 if ((asprintf(&shortlog_html, summary_shortlog,
480 shortlog)) == -1)
481 return got_error_from_errno("asprintf");
482 khttp_puts(gw_trans->gw_req, shortlog_html);
483 free(shortlog_html);
484 free(shortlog);
487 if (tags != NULL && strcmp(tags, "") != 0) {
488 if ((asprintf(&tags_html, summary_tags,
489 tags)) == -1)
490 return got_error_from_errno("asprintf");
491 khttp_puts(gw_trans->gw_req, tags_html);
492 free(tags_html);
493 free(tags);
496 if (heads != NULL && strcmp(heads, "") != 0) {
497 if ((asprintf(&heads_html, summary_heads,
498 heads)) == -1)
499 return got_error_from_errno("asprintf");
500 khttp_puts(gw_trans->gw_req, heads_html);
501 free(heads_html);
502 free(heads);
505 return error;
508 static const struct got_error *
509 gw_tree(struct trans *gw_trans)
511 const struct got_error *error = NULL;
513 return error;
516 static const struct got_error *
517 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
519 const struct got_error *error = NULL;
520 DIR *dt;
521 char *dir_test;
522 bool opened = false;
524 if ((asprintf(&dir_test, "%s/%s/%s",
525 gw_trans->gw_conf->got_repos_path, gw_dir->name,
526 GOTWEB_GIT_DIR)) == -1)
527 return got_error_from_errno("asprintf");
529 dt = opendir(dir_test);
530 if (dt == NULL) {
531 free(dir_test);
532 } else {
533 gw_dir->path = strdup(dir_test);
534 opened = true;
535 goto done;
538 if ((asprintf(&dir_test, "%s/%s/%s",
539 gw_trans->gw_conf->got_repos_path, gw_dir->name,
540 GOTWEB_GOT_DIR)) == -1)
541 return got_error_from_errno("asprintf");
543 dt = opendir(dir_test);
544 if (dt == NULL)
545 free(dir_test);
546 else {
547 opened = true;
548 error = got_error(GOT_ERR_NOT_GIT_REPO);
549 goto errored;
552 if ((asprintf(&dir_test, "%s/%s",
553 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
554 return got_error_from_errno("asprintf");
556 gw_dir->path = strdup(dir_test);
558 done:
559 gw_dir->description = gw_get_repo_description(gw_trans,
560 gw_dir->path);
561 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
562 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
563 TM_DIFF);
564 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
566 errored:
567 free(dir_test);
568 if (opened)
569 closedir(dt);
570 return error;
573 static const struct got_error *
574 gw_load_got_paths(struct trans *gw_trans)
576 const struct got_error *error = NULL;
577 DIR *d;
578 struct dirent **sd_dent;
579 struct gw_dir *gw_dir;
580 struct stat st;
581 unsigned int d_cnt, d_i;
583 d = opendir(gw_trans->gw_conf->got_repos_path);
584 if (d == NULL) {
585 error = got_error_from_errno2("opendir",
586 gw_trans->gw_conf->got_repos_path);
587 return error;
590 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
591 alphasort);
592 if (d_cnt == -1) {
593 error = got_error_from_errno2("scandir",
594 gw_trans->gw_conf->got_repos_path);
595 return error;
598 for (d_i = 0; d_i < d_cnt; d_i++) {
599 if (gw_trans->gw_conf->got_max_repos > 0 &&
600 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
601 break; /* account for parent and self */
603 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
604 strcmp(sd_dent[d_i]->d_name, "..") == 0)
605 continue;
607 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
608 return got_error_from_errno("gw_dir malloc");
610 error = gw_load_got_path(gw_trans, gw_dir);
611 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
612 continue;
613 else if (error)
614 return error;
616 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
617 !got_path_dir_is_empty(gw_dir->path)) {
618 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
619 entry);
620 gw_trans->repos_total++;
624 closedir(d);
625 return error;
628 static const struct got_error *
629 gw_parse_querystring(struct trans *gw_trans)
631 const struct got_error *error = NULL;
632 struct kpair *p;
633 struct gw_query_action *action = NULL;
634 unsigned int i;
636 if (gw_trans->gw_req->fieldnmap[0]) {
637 error = got_error_from_errno("bad parse");
638 return error;
639 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
640 /* define gw_trans->repo_path */
641 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
642 return got_error_from_errno("asprintf");
644 if ((asprintf(&gw_trans->repo_path, "%s/%s",
645 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
646 return got_error_from_errno("asprintf");
648 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
649 if ((asprintf(&gw_trans->commit, "%s",
650 p->parsed.s)) == -1)
651 return got_error_from_errno("asprintf");
653 /* get action and set function */
654 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
655 for (i = 0; i < nitems(gw_query_funcs); i++) {
656 action = &gw_query_funcs[i];
657 if (action->func_name == NULL)
658 continue;
660 if (strcmp(action->func_name,
661 p->parsed.s) == 0) {
662 gw_trans->action = i;
663 if ((asprintf(&gw_trans->action_name,
664 "%s", action->func_name)) == -1)
665 return
666 got_error_from_errno(
667 "asprintf");
669 break;
672 action = NULL;
675 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
676 if ((asprintf(&gw_trans->repo_file, "%s",
677 p->parsed.s)) == -1)
678 return got_error_from_errno("asprintf");
680 if (action == NULL) {
681 error = got_error_from_errno("invalid action");
682 return error;
684 if ((gw_trans->gw_dir =
685 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
686 return got_error_from_errno("gw_dir malloc");
688 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
689 if (error)
690 return error;
691 } else
692 gw_trans->action = GW_INDEX;
694 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
695 gw_trans->page = p->parsed.i;
697 if (gw_trans->action == GW_RAW)
698 gw_trans->mime = KMIME_TEXT_PLAIN;
700 return error;
703 static struct gw_dir *
704 gw_init_gw_dir(char *dir)
706 struct gw_dir *gw_dir;
708 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
709 return NULL;
711 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
712 return NULL;
714 return gw_dir;
717 static void
718 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
720 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
721 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
722 khttps[code]);
723 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
724 kmimetypes[mime]);
725 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
726 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
727 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
728 khttp_body(gw_trans->gw_req);
731 static void
732 gw_display_index(struct trans *gw_trans, const struct got_error *err)
734 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
735 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
737 if (err)
738 khttp_puts(gw_trans->gw_req, err->msg);
739 else
740 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
741 gw_query_funcs[gw_trans->action].template);
743 khtml_close(gw_trans->gw_html_req);
746 static int
747 gw_template(size_t key, void *arg)
749 const struct got_error *error = NULL;
750 struct trans *gw_trans = arg;
751 char *gw_got_link, *gw_site_link;
752 char *site_owner_name, *site_owner_name_h;
754 switch (key) {
755 case (TEMPL_HEAD):
756 khttp_puts(gw_trans->gw_req, head);
757 break;
758 case(TEMPL_HEADER):
759 gw_got_link = gw_get_got_link(gw_trans);
760 if (gw_got_link != NULL)
761 khttp_puts(gw_trans->gw_req, gw_got_link);
763 free(gw_got_link);
764 break;
765 case (TEMPL_SITEPATH):
766 gw_site_link = gw_get_site_link(gw_trans);
767 if (gw_site_link != NULL)
768 khttp_puts(gw_trans->gw_req, gw_site_link);
770 free(gw_site_link);
771 break;
772 case(TEMPL_TITLE):
773 if (gw_trans->gw_conf->got_site_name != NULL)
774 khtml_puts(gw_trans->gw_html_req,
775 gw_trans->gw_conf->got_site_name);
777 break;
778 case (TEMPL_SEARCH):
779 khttp_puts(gw_trans->gw_req, search);
780 break;
781 case(TEMPL_SITEOWNER):
782 if (gw_trans->gw_conf->got_site_owner != NULL &&
783 gw_trans->gw_conf->got_show_site_owner) {
784 site_owner_name =
785 gw_html_escape(gw_trans->gw_conf->got_site_owner);
786 if ((asprintf(&site_owner_name_h, site_owner,
787 site_owner_name))
788 == -1)
789 return 0;
791 khttp_puts(gw_trans->gw_req, site_owner_name_h);
792 free(site_owner_name);
793 free(site_owner_name_h);
795 break;
796 case(TEMPL_CONTENT):
797 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
798 if (error)
799 khttp_puts(gw_trans->gw_req, error->msg);
801 break;
802 default:
803 return 0;
804 break;
806 return 1;
809 static char *
810 gw_get_repo_description(struct trans *gw_trans, char *dir)
812 FILE *f;
813 char *description = NULL, *d_file = NULL;
814 unsigned int len;
816 if (gw_trans->gw_conf->got_show_repo_description == false)
817 goto err;
819 if ((asprintf(&d_file, "%s/description", dir)) == -1)
820 goto err;
822 if ((f = fopen(d_file, "r")) == NULL)
823 goto err;
825 fseek(f, 0, SEEK_END);
826 len = ftell(f) + 1;
827 fseek(f, 0, SEEK_SET);
828 if ((description = calloc(len, sizeof(char *))) == NULL)
829 goto err;
831 fread(description, 1, len, f);
832 fclose(f);
833 free(d_file);
834 return description;
835 err:
836 if ((asprintf(&description, "%s", "")) == -1)
837 return NULL;
839 return description;
842 static char *
843 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
845 const struct got_error *error = NULL;
846 struct got_object_id *id = NULL;
847 struct got_repository *repo = NULL;
848 struct got_commit_object *commit = NULL;
849 struct got_reflist_head refs;
850 struct got_reflist_entry *re;
851 struct got_reference *head_ref;
852 struct tm tm;
853 time_t committer_time = 0, cmp_time = 0, diff_time;
854 char *repo_age = NULL, *years = "years ago", *months = "months ago";
855 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
856 char *minutes = "minutes ago", *seconds = "seconds ago";
857 char *now = "right now";
858 char *s;
859 char datebuf[BUFFER_SIZE];
861 if (repo_ref == NULL)
862 return NULL;
864 SIMPLEQ_INIT(&refs);
865 if (gw_trans->gw_conf->got_show_repo_age == false) {
866 asprintf(&repo_age, "");
867 return repo_age;
869 error = got_repo_open(&repo, dir, NULL);
870 if (error != NULL)
871 goto err;
873 error = got_ref_list(&refs, repo, repo_ref, got_ref_cmp_by_name,
874 NULL);
875 if (error != NULL)
876 goto err;
878 const char *refname;
879 SIMPLEQ_FOREACH(re, &refs, entry) {
880 refname = got_ref_get_name(re->ref);
881 error = got_ref_open(&head_ref, repo, refname, 0);
882 if (error != NULL)
883 goto err;
885 error = got_ref_resolve(&id, repo, head_ref);
886 got_ref_close(head_ref);
887 if (error != NULL)
888 goto err;
890 /* here is what breaks tags, so adjust */
891 error = got_object_open_as_commit(&commit, repo, id);
892 if (error != NULL)
893 goto err;
895 committer_time =
896 got_object_commit_get_committer_time(commit);
898 if (cmp_time < committer_time)
899 cmp_time = committer_time;
902 if (cmp_time != 0)
903 committer_time = cmp_time;
905 switch (ref_tm) {
906 case TM_DIFF:
907 diff_time = time(NULL) - committer_time;
908 if (diff_time > 60 * 60 * 24 * 365 * 2) {
909 if ((asprintf(&repo_age, "%lld %s",
910 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
911 return NULL;
912 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
913 if ((asprintf(&repo_age, "%lld %s",
914 (diff_time / 60 / 60 / 24 / (365 / 12)),
915 months)) == -1)
916 return NULL;
917 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
918 if ((asprintf(&repo_age, "%lld %s",
919 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
920 return NULL;
921 } else if (diff_time > 60 * 60 * 24 * 2) {
922 if ((asprintf(&repo_age, "%lld %s",
923 (diff_time / 60 / 60 / 24), days)) == -1)
924 return NULL;
925 } else if (diff_time > 60 * 60 * 2) {
926 if ((asprintf(&repo_age, "%lld %s",
927 (diff_time / 60 / 60), hours)) == -1)
928 return NULL;
929 } else if (diff_time > 60 * 2) {
930 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
931 minutes)) == -1)
932 return NULL;
933 } else if (diff_time > 2) {
934 if ((asprintf(&repo_age, "%lld %s", diff_time,
935 seconds)) == -1)
936 return NULL;
937 } else {
938 if ((asprintf(&repo_age, "%s", now)) == -1)
939 return NULL;
941 break;
942 case TM_LONG:
943 if (cmp_time != 0) {
944 if (gmtime_r(&committer_time, &tm) == NULL)
945 return NULL;
947 s = asctime_r(&tm, datebuf);
948 if (s == NULL)
949 return NULL;
951 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
952 return NULL;
953 } else {
954 if ((asprintf(&repo_age, "")) == -1)
955 return NULL;
957 break;
960 /* noref: */
961 got_ref_list_free(&refs);
962 free(id);
963 return repo_age;
964 err:
965 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
966 return NULL;
968 return repo_age;
971 static char *
972 gw_get_repo_owner(struct trans *gw_trans, char *dir)
974 FILE *f;
975 char *owner = NULL, *d_file = NULL;
976 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
977 char *comp, *pos, *buf;
978 unsigned int i;
980 if (gw_trans->gw_conf->got_show_repo_owner == false)
981 goto err;
983 if ((asprintf(&d_file, "%s/config", dir)) == -1)
984 goto err;
986 if ((f = fopen(d_file, "r")) == NULL)
987 goto err;
989 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
990 goto err;
992 while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
993 if ((pos = strstr(buf, gotweb)) != NULL)
994 break;
996 if ((pos = strstr(buf, gitweb)) != NULL)
997 break;
1000 if (pos == NULL)
1001 goto err;
1003 do {
1004 fgets(buf, BUFFER_SIZE, f);
1005 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1007 if (comp == NULL)
1008 goto err;
1010 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1011 goto err;
1013 for (i = 0; i < 2; i++) {
1014 owner = strsep(&buf, "\"");
1017 if (owner == NULL)
1018 goto err;
1020 fclose(f);
1021 free(d_file);
1022 return owner;
1023 err:
1024 if ((asprintf(&owner, "%s", "")) == -1)
1025 return NULL;
1027 return owner;
1030 static char *
1031 gw_get_clone_url(struct trans *gw_trans, char *dir)
1033 FILE *f;
1034 char *url = NULL, *d_file = NULL;
1035 unsigned int len;
1037 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1038 return NULL;
1040 if ((f = fopen(d_file, "r")) == NULL)
1041 return NULL;
1043 fseek(f, 0, SEEK_END);
1044 len = ftell(f) + 1;
1045 fseek(f, 0, SEEK_SET);
1047 if ((url = calloc(len, sizeof(char *))) == NULL)
1048 return NULL;
1050 fread(url, 1, len, f);
1051 fclose(f);
1052 free(d_file);
1053 return url;
1056 static char *
1057 gw_get_repo_shortlog(struct trans *gw_trans)
1059 const struct got_error *error;
1060 struct got_repository *repo = NULL;
1061 struct got_reflist_head refs;
1062 struct got_commit_object *commit = NULL;
1063 struct got_object_id *id = NULL;
1064 char *start_commit = NULL, *head_ref_name = NULL;
1065 char *shortlog = NULL;
1067 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1068 if (error != NULL)
1069 goto done;
1071 if (start_commit == NULL) {
1072 struct got_reference *head_ref;
1073 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1074 if (error != NULL)
1075 return NULL;
1076 error = got_ref_resolve(&id, repo, head_ref);
1077 got_ref_close(head_ref);
1078 if (error != NULL)
1079 return NULL;
1080 error = got_object_open_as_commit(&commit, repo, id);
1081 } else {
1082 struct got_reference *ref;
1083 error = got_ref_open(&ref, repo, start_commit, 0);
1084 if (error == NULL) {
1085 int obj_type;
1086 error = got_ref_resolve(&id, repo, ref);
1087 got_ref_close(ref);
1088 if (error != NULL)
1089 goto done;
1090 error = got_object_get_type(&obj_type, repo, id);
1091 if (error != NULL)
1092 goto done;
1093 if (obj_type == GOT_OBJ_TYPE_TAG) {
1094 struct got_tag_object *tag;
1095 error = got_object_open_as_tag(&tag, repo, id);
1096 if (error != NULL)
1097 goto done;
1098 if (got_object_tag_get_object_type(tag) !=
1099 GOT_OBJ_TYPE_COMMIT) {
1100 got_object_tag_close(tag);
1101 error = got_error(GOT_ERR_OBJ_TYPE);
1102 goto done;
1104 free(id);
1105 id = got_object_id_dup(
1106 got_object_tag_get_object_id(tag));
1107 if (id == NULL)
1108 error = got_error_from_errno(
1109 "got_object_id_dup");
1110 got_object_tag_close(tag);
1111 if (error)
1112 goto done;
1113 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1114 error = got_error(GOT_ERR_OBJ_TYPE);
1115 goto done;
1117 error = got_object_open_as_commit(&commit, repo, id);
1118 if (error != NULL)
1119 goto done;
1121 if (commit == NULL) {
1122 error = got_repo_match_object_id_prefix(&id,
1123 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1124 if (error != NULL)
1125 return NULL;
1129 if (error != NULL)
1130 goto done;
1132 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1133 if (error)
1134 goto done;
1136 /* int diff_context = -1, show_patch = 0; */
1137 /* error = print_commits(id, repo, path, show_patch, search_pattern, */
1138 /* diff_context, limit, first_parent_traversal, &refs); */
1139 /* error = print_commit(commit, id, repo, path, 0, */
1140 /* -1, refs); */
1141 got_object_commit_close(commit);
1143 asprintf(&shortlog, shortlog_row, "30 min ago", "Flan Author", "this is just a fake ass place holder", shortlog_navs);
1144 return shortlog;
1145 done:
1146 free(head_ref_name);
1147 if (repo)
1148 got_repo_close(repo);
1149 got_ref_list_free(&refs);
1150 return NULL;
1153 static char *
1154 gw_get_repo_tags(struct trans *gw_trans)
1156 char *tags = NULL;
1158 asprintf(&tags, tags_row, "30 min ago", "1.0.0", "tag 1.0.0", tags_navs);
1159 return tags;
1162 static char *
1163 gw_get_repo_heads(struct trans *gw_trans)
1165 char *heads = NULL;
1167 asprintf(&heads, heads_row, "30 min ago", "master", heads_navs);
1168 return heads;
1171 static char *
1172 gw_get_got_link(struct trans *gw_trans)
1174 char *link;
1176 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
1177 gw_trans->gw_conf->got_logo)) == -1)
1178 return NULL;
1180 return link;
1183 static char *
1184 gw_get_site_link(struct trans *gw_trans)
1186 char *link, *repo = "", *action = "";
1188 if (gw_trans->repo_name != NULL)
1189 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
1190 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
1191 return NULL;
1193 if (gw_trans->action_name != NULL)
1194 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
1195 return NULL;
1197 if ((asprintf(&link, site_link, GOTWEB,
1198 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
1199 return NULL;
1201 return link;
1204 static char *
1205 gw_html_escape(const char *html)
1207 char *escaped_str = NULL, *buf;
1208 char c[1];
1209 size_t sz, i;
1211 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1212 return NULL;
1214 if (html == NULL)
1215 return NULL;
1216 else
1217 if ((sz = strlen(html)) == 0)
1218 return NULL;
1220 /* only work with BUFFER_SIZE */
1221 if (BUFFER_SIZE < sz)
1222 sz = BUFFER_SIZE;
1224 for (i = 0; i < sz; i++) {
1225 c[0] = html[i];
1226 switch (c[0]) {
1227 case ('>'):
1228 strcat(buf, "&gt;");
1229 break;
1230 case ('&'):
1231 strcat(buf, "&amp;");
1232 break;
1233 case ('<'):
1234 strcat(buf, "&lt;");
1235 break;
1236 case ('"'):
1237 strcat(buf, "&quot;");
1238 break;
1239 case ('\''):
1240 strcat(buf, "&apos;");
1241 break;
1242 case ('\n'):
1243 strcat(buf, "<br />");
1244 default:
1245 strcat(buf, &c[0]);
1246 break;
1249 asprintf(&escaped_str, "%s", buf);
1250 free(buf);
1251 return escaped_str;
1254 int
1255 main()
1257 const struct got_error *error = NULL;
1258 struct trans *gw_trans;
1259 struct gw_dir *dir = NULL, *tdir;
1260 const char *page = "index";
1261 bool gw_malloc = true;
1263 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
1264 errx(1, "malloc");
1266 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
1267 errx(1, "malloc");
1269 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
1270 errx(1, "malloc");
1272 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
1273 errx(1, "malloc");
1275 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
1276 &page, 1, 0))
1277 errx(1, "khttp_parse");
1279 if ((gw_trans->gw_conf =
1280 malloc(sizeof(struct gotweb_conf))) == NULL) {
1281 gw_malloc = false;
1282 error = got_error_from_errno("malloc");
1283 goto err;
1286 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
1287 error = got_error_from_errno("pledge");
1288 goto err;
1291 TAILQ_INIT(&gw_trans->gw_dirs);
1293 gw_trans->page = 0;
1294 gw_trans->repos_total = 0;
1295 gw_trans->repo_path = NULL;
1296 gw_trans->commit = NULL;
1297 gw_trans->mime = KMIME_TEXT_HTML;
1298 gw_trans->gw_tmpl->key = templs;
1299 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
1300 gw_trans->gw_tmpl->arg = gw_trans;
1301 gw_trans->gw_tmpl->cb = gw_template;
1302 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
1304 err:
1305 if (error) {
1306 gw_trans->mime = KMIME_TEXT_PLAIN;
1307 gw_trans->action = GW_ERR;
1308 gw_display_index(gw_trans, error);
1309 goto done;
1312 error = gw_parse_querystring(gw_trans);
1313 if (error)
1314 goto err;
1316 gw_display_index(gw_trans, error);
1318 done:
1319 if (gw_malloc) {
1320 free(gw_trans->gw_conf->got_repos_path);
1321 free(gw_trans->gw_conf->got_www_path);
1322 free(gw_trans->gw_conf->got_site_name);
1323 free(gw_trans->gw_conf->got_site_owner);
1324 free(gw_trans->gw_conf->got_site_link);
1325 free(gw_trans->gw_conf->got_logo);
1326 free(gw_trans->gw_conf->got_logo_url);
1327 free(gw_trans->gw_conf);
1328 free(gw_trans->commit);
1329 free(gw_trans->repo_path);
1330 free(gw_trans->repo_name);
1331 free(gw_trans->repo_file);
1332 free(gw_trans->action_name);
1334 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
1335 free(dir->name);
1336 free(dir->description);
1337 free(dir->age);
1338 free(dir->url);
1339 free(dir->path);
1340 free(dir);
1345 khttp_free(gw_trans->gw_req);
1346 return EXIT_SUCCESS;