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 gotweb_conf *gw_conf;
58 struct ktemplate *gw_tmpl;
59 struct khtmlreq *gw_html_req;
60 struct kreq *gw_req;
61 char *repo_name;
62 char *repo_path;
63 char *commit;
64 char *repo_file;
65 char *action_name;
66 unsigned int action;
67 unsigned int page;
68 unsigned int repos_total;
69 enum kmime mime;
70 };
72 enum gw_key {
73 KEY_PATH,
74 KEY_ACTION,
75 KEY_COMMIT_ID,
76 KEY_FILE,
77 KEY_PAGE,
78 KEY__MAX
79 };
81 struct gw_dir {
82 TAILQ_ENTRY(gw_dir) entry;
83 char *name;
84 char *owner;
85 char *description;
86 char *url;
87 char *age;
88 char *path;
89 };
91 enum tmpl {
92 TEMPL_HEAD,
93 TEMPL_HEADER,
94 TEMPL_SITEPATH,
95 TEMPL_SITEOWNER,
96 TEMPL_TITLE,
97 TEMPL_SEARCH,
98 TEMPL_DESCRIPTION,
99 TEMPL_CONTENT,
100 TEMPL_REPO_OWNER,
101 TEMPL_REPO_AGE,
102 TEMPL_CLONEURL,
103 TEMPL__MAX
104 };
106 enum ref_tm {
107 TM_DIFF,
108 TM_LONG,
109 };
111 static const char *const templs[TEMPL__MAX] = {
112 "head",
113 "header",
114 "sitepath",
115 "siteowner",
116 "title",
117 "search",
118 "description",
119 "content",
120 "repo_owner",
121 "repo_age",
122 "cloneurl",
123 };
125 static const struct kvalid gw_keys[KEY__MAX] = {
126 { kvalid_stringne, "path" },
127 { kvalid_stringne, "action" },
128 { kvalid_stringne, "commit" },
129 { kvalid_stringne, "file" },
130 { kvalid_int, "page" },
131 };
133 static struct gw_dir *gw_init_gw_dir(char *);
135 static char *gw_get_repo_description(struct trans *,
136 char *);
137 static char *gw_get_repo_owner(struct trans *,
138 char *);
139 static char *gw_get_repo_age(struct trans *,
140 char *, char *, int);
141 static char *gw_get_clone_url(struct trans *, char *);
142 static char *gw_get_got_link(struct trans *);
143 static char *gw_get_site_link(struct trans *);
144 static char *gw_html_escape(const char *);
146 static void gw_display_open(struct trans *, enum khttp,
147 enum kmime);
148 static void gw_display_index(struct trans *,
149 const struct got_error *);
151 static int gw_template(size_t, void *);
153 static const struct got_error* apply_unveil(const char *, const char *);
154 static const struct got_error* gw_load_got_paths(struct trans *);
155 static const struct got_error* gw_load_got_path(struct trans *,
156 struct gw_dir *);
157 static const struct got_error* gw_parse_querystring(struct trans *);
159 static const struct got_error* gw_blame(struct trans *);
160 static const struct got_error* gw_blob(struct trans *);
161 static const struct got_error* gw_blob_diff(struct trans *);
162 static const struct got_error* gw_commit(struct trans *);
163 static const struct got_error* gw_commit_diff(struct trans *);
164 static const struct got_error* gw_heads(struct trans *);
165 static const struct got_error* gw_history(struct trans *);
166 static const struct got_error* gw_index(struct trans *);
167 static const struct got_error* gw_log(struct trans *);
168 static const struct got_error* gw_raw(struct trans *);
169 static const struct got_error* gw_shortlog(struct trans *);
170 static const struct got_error* gw_snapshot(struct trans *);
171 static const struct got_error* gw_summary(struct trans *);
172 static const struct got_error* gw_tags(struct trans *);
173 static const struct got_error* gw_tree(struct trans *);
175 struct gw_query_action {
176 unsigned int func_id;
177 const char *func_name;
178 const struct got_error *(*func_main)(struct trans *);
179 char *template;
180 };
182 enum gw_query_actions {
183 GW_BLAME,
184 GW_BLOB,
185 GW_BLOBDIFF,
186 GW_COMMIT,
187 GW_COMMITDIFF,
188 GW_ERR,
189 GW_HISTORY,
190 GW_INDEX,
191 GW_LOG,
192 GW_RAW,
193 GW_SHORTLOG,
194 GW_SNAPSHOT,
195 GW_SUMMARY,
196 GW_TREE
197 };
199 static struct gw_query_action gw_query_funcs[] = {
200 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
201 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
202 { GW_BLOBDIFF, "blobdiff", gw_blob_diff, "gw_tmpl/index.tmpl" },
203 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
204 { GW_COMMITDIFF, "commit_diff", gw_commit_diff, "gw_tmpl/index.tmpl" },
205 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
206 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
207 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
208 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
209 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
210 { GW_SHORTLOG, "shortlog", gw_shortlog, "gw_tmpl/index.tmpl" },
211 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
212 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summary.tmpl" },
213 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
214 };
216 static const struct got_error *
217 apply_unveil(const char *repo_path, const char *repo_file)
219 const struct got_error *err;
221 if (repo_path && repo_file) {
222 char *full_path;
223 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
224 return got_error_from_errno("asprintf unveil");
225 if (unveil(full_path, "r") != 0)
226 return got_error_from_errno2("unveil", full_path);
229 if (repo_path && unveil(repo_path, "r") != 0)
230 return got_error_from_errno2("unveil", repo_path);
232 if (unveil("/tmp", "rwc") != 0)
233 return got_error_from_errno2("unveil", "/tmp");
235 err = got_privsep_unveil_exec_helpers();
236 if (err != NULL)
237 return err;
239 if (unveil(NULL, NULL) != 0)
240 return got_error_from_errno("unveil");
242 return NULL;
245 static const struct got_error *
246 gw_blame(struct trans *gw_trans)
248 const struct got_error *error = NULL;
250 return error;
253 static const struct got_error *
254 gw_blob(struct trans *gw_trans)
256 const struct got_error *error = NULL;
258 return error;
261 static const struct got_error *
262 gw_blob_diff(struct trans *gw_trans)
264 const struct got_error *error = NULL;
266 return error;
269 static const struct got_error *
270 gw_commit(struct trans *gw_trans)
272 const struct got_error *error = NULL;
274 return error;
277 static const struct got_error *
278 gw_commit_diff(struct trans *gw_trans)
280 const struct got_error *error = NULL;
282 return error;
285 static const struct got_error *
286 gw_heads(struct trans *gw_trans)
288 const struct got_error *error = NULL;
290 return error;
293 static const struct got_error *
294 gw_history(struct trans *gw_trans)
296 const struct got_error *error = NULL;
298 return error;
301 static const struct got_error *
302 gw_index(struct trans *gw_trans)
304 const struct got_error *error = NULL;
305 struct gw_dir *dir = NULL;
306 char *html, *navs, *next, *prev;
307 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
309 error = gw_load_got_paths(gw_trans);
310 if (error && error->code != GOT_ERR_OK)
311 return error;
313 khttp_puts(gw_trans->gw_req, index_projects_header);
315 TAILQ_FOREACH(dir, &gw_trans->gw_dirs, entry)
316 dir_c++;
318 TAILQ_FOREACH(dir, &gw_trans->gw_dirs, entry) {
319 if (gw_trans->page > 0 && (gw_trans->page *
320 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
321 prev_disp++;
322 continue;
325 prev_disp++;
326 if((asprintf(&navs, index_navs, dir->name, dir->name, dir->name,
327 dir->name)) == -1)
328 return got_error_from_errno("asprintf");
330 if ((asprintf(&html, index_projects, dir->name, dir->name,
331 dir->description, dir->owner, dir->age, navs)) == -1)
332 return got_error_from_errno("asprintf");
334 khttp_puts(gw_trans->gw_req, html);
336 free(navs);
337 free(html);
339 if (gw_trans->gw_conf->got_max_repos_display == 0)
340 continue;
342 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
343 khttp_puts(gw_trans->gw_req, np_wrapper_start);
344 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
345 (gw_trans->page > 0) &&
346 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
347 prev_disp == gw_trans->repos_total))
348 khttp_puts(gw_trans->gw_req, np_wrapper_start);
350 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
351 (gw_trans->page > 0) &&
352 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
353 prev_disp == gw_trans->repos_total)) {
354 if ((asprintf(&prev, nav_prev,
355 gw_trans->page - 1)) == -1)
356 return got_error_from_errno("asprintf");
357 khttp_puts(gw_trans->gw_req, prev);
358 free(prev);
361 khttp_puts(gw_trans->gw_req, div_end);
363 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
364 next_disp == gw_trans->gw_conf->got_max_repos_display &&
365 dir_c != (gw_trans->page + 1) *
366 gw_trans->gw_conf->got_max_repos_display) {
367 if ((asprintf(&next, nav_next,
368 gw_trans->page + 1)) == -1)
369 return got_error_from_errno("calloc");
370 khttp_puts(gw_trans->gw_req, next);
371 khttp_puts(gw_trans->gw_req, div_end);
372 free(next);
373 next_disp = 0;
374 break;
377 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
378 (gw_trans->page > 0) &&
379 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
380 prev_disp == gw_trans->repos_total))
381 khttp_puts(gw_trans->gw_req, div_end);
383 next_disp++;
385 return error;
388 static const struct got_error *
389 gw_log(struct trans *gw_trans)
391 const struct got_error *error = NULL;
393 return error;
396 static const struct got_error *
397 gw_raw(struct trans *gw_trans)
399 const struct got_error *error = NULL;
401 return error;
404 static const struct got_error *
405 gw_shortlog(struct trans *gw_trans)
407 const struct got_error *error = NULL;
409 return error;
412 static const struct got_error *
413 gw_snapshot(struct trans *gw_trans)
415 const struct got_error *error = NULL;
417 return error;
420 static const struct got_error *
421 gw_summary(struct trans *gw_trans)
423 const struct got_error *error = NULL;
425 error = gw_shortlog(gw_trans);
426 error = gw_tags(gw_trans);
427 error = gw_heads(gw_trans);
428 khttp_puts(gw_trans->gw_req, summary_shortlog);
429 khttp_puts(gw_trans->gw_req, summary_tags);
430 khttp_puts(gw_trans->gw_req, summary_heads);
431 return error;
434 static const struct got_error *
435 gw_tags(struct trans *gw_trans)
437 const struct got_error *error = NULL;
439 return error;
442 static const struct got_error *
443 gw_tree(struct trans *gw_trans)
445 const struct got_error *error = NULL;
447 return error;
450 static const struct got_error *
451 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
453 const struct got_error *error = NULL;
454 DIR *dt;
455 char *dir_test;
456 bool opened = false;
458 if ((asprintf(&dir_test, "%s/%s/%s",
459 gw_trans->gw_conf->got_repos_path, gw_dir->name,
460 GOTWEB_GIT_DIR)) == -1)
461 return got_error_from_errno("asprintf");
463 dt = opendir(dir_test);
464 if (dt == NULL) {
465 free(dir_test);
466 } else {
467 gw_dir->path = strdup(dir_test);
468 opened = true;
469 goto done;
472 if ((asprintf(&dir_test, "%s/%s/%s",
473 gw_trans->gw_conf->got_repos_path, gw_dir->name,
474 GOTWEB_GOT_DIR)) == -1)
475 return got_error_from_errno("asprintf");
477 dt = opendir(dir_test);
478 if (dt == NULL)
479 free(dir_test);
480 else {
481 opened = true;
482 error = got_error(GOT_ERR_NOT_GIT_REPO);
483 goto errored;
486 if ((asprintf(&dir_test, "%s/%s",
487 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
488 return got_error_from_errno("asprintf");
490 gw_dir->path = strdup(dir_test);
492 done:
493 gw_dir->description = gw_get_repo_description(gw_trans,
494 gw_dir->path);
495 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
496 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
497 TM_DIFF);
498 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
500 errored:
501 free(dir_test);
502 if (opened)
503 closedir(dt);
504 return error;
507 static const struct got_error *
508 gw_load_got_paths(struct trans *gw_trans)
510 const struct got_error *error = NULL;
511 DIR *d;
512 struct dirent **sd_dent;
513 struct gw_dir *gw_dir;
514 struct stat st;
515 unsigned int d_cnt, d_i;
517 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
518 error = got_error_from_errno("pledge");
519 return error;
522 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
523 if (error)
524 return error;
526 d = opendir(gw_trans->gw_conf->got_repos_path);
527 if (d == NULL) {
528 error = got_error_from_errno2("opendir",
529 gw_trans->gw_conf->got_repos_path);
530 return error;
533 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
534 alphasort);
535 if (d_cnt == -1) {
536 error = got_error_from_errno2("scandir",
537 gw_trans->gw_conf->got_repos_path);
538 return error;
541 for (d_i = 0; d_i < d_cnt; d_i++) {
542 if (gw_trans->gw_conf->got_max_repos > 0 &&
543 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
544 break; /* account for parent and self */
546 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
547 strcmp(sd_dent[d_i]->d_name, "..") == 0)
548 continue;
550 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
551 return got_error_from_errno("gw_dir malloc");
553 error = gw_load_got_path(gw_trans, gw_dir);
554 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
555 continue;
556 else if (error)
557 return error;
559 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
560 !got_path_dir_is_empty(gw_dir->path)) {
561 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
562 entry);
563 gw_trans->repos_total++;
567 closedir(d);
568 return error;
571 static const struct got_error *
572 gw_parse_querystring(struct trans *gw_trans)
574 const struct got_error *error = NULL;
575 struct kpair *p;
576 struct gw_query_action *action = NULL;
577 unsigned int i;
579 if (gw_trans->gw_req->fieldnmap[0]) {
580 error = got_error_from_errno("bad parse");
581 return error;
582 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
583 /* define gw_trans->repo_path */
584 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
585 return got_error_from_errno("asprintf");
587 if ((asprintf(&gw_trans->repo_path, "%s/%s",
588 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
589 return got_error_from_errno("asprintf");
591 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
592 if ((asprintf(&gw_trans->commit, "%s",
593 p->parsed.s)) == -1)
594 return got_error_from_errno("asprintf");
596 /* get action and set function */
597 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
598 for (i = 0; i < nitems(gw_query_funcs); i++) {
599 action = &gw_query_funcs[i];
600 if (action->func_name == NULL)
601 continue;
603 if (strcmp(action->func_name,
604 p->parsed.s) == 0) {
605 gw_trans->action = i;
606 if ((asprintf(&gw_trans->action_name,
607 "%s", action->func_name)) == -1)
608 return
609 got_error_from_errno(
610 "asprintf");
612 break;
615 action = NULL;
618 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
619 if ((asprintf(&gw_trans->repo_file, "%s",
620 p->parsed.s)) == -1)
621 return got_error_from_errno("asprintf");
623 if (action == NULL) {
624 error = got_error_from_errno("invalid action");
625 return error;
627 } else
628 gw_trans->action = GW_INDEX;
630 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
631 gw_trans->page = p->parsed.i;
633 if (gw_trans->action == GW_RAW)
634 gw_trans->mime = KMIME_TEXT_PLAIN;
636 return error;
639 static struct gw_dir *
640 gw_init_gw_dir(char *dir)
642 struct gw_dir *gw_dir;
644 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
645 return NULL;
647 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
648 return NULL;
650 return gw_dir;
653 static void
654 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
656 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
657 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
658 khttps[code]);
659 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
660 kmimetypes[mime]);
661 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
662 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
663 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
664 khttp_body(gw_trans->gw_req);
667 static void
668 gw_display_index(struct trans *gw_trans, const struct got_error *err)
670 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
671 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
673 if (err)
674 khttp_puts(gw_trans->gw_req, err->msg);
675 else
676 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
677 gw_query_funcs[gw_trans->action].template);
679 khtml_close(gw_trans->gw_html_req);
682 static int
683 gw_template(size_t key, void *arg)
685 const struct got_error *error = NULL;
686 struct trans *gw_trans = arg;
687 char *gw_got_link, *gw_site_link;
688 char *site_owner_name, *site_owner_name_h;
689 char *description, *description_h;
690 char *repo_owner, *repo_owner_h;
691 char *repo_age, *repo_age_h;
692 char *cloneurl, *cloneurl_h;
694 switch (key) {
695 case (TEMPL_HEAD):
696 khttp_puts(gw_trans->gw_req, head);
697 break;
698 case(TEMPL_HEADER):
699 gw_got_link = gw_get_got_link(gw_trans);
700 if (gw_got_link != NULL)
701 khttp_puts(gw_trans->gw_req, gw_got_link);
703 free(gw_got_link);
704 break;
705 case (TEMPL_SITEPATH):
706 gw_site_link = gw_get_site_link(gw_trans);
707 if (gw_site_link != NULL)
708 khttp_puts(gw_trans->gw_req, gw_site_link);
710 free(gw_site_link);
711 break;
712 case(TEMPL_TITLE):
713 if (gw_trans->gw_conf->got_site_name != NULL)
714 khtml_puts(gw_trans->gw_html_req,
715 gw_trans->gw_conf->got_site_name);
717 break;
718 case (TEMPL_SEARCH):
719 khttp_puts(gw_trans->gw_req, search);
720 break;
721 case(TEMPL_DESCRIPTION):
722 if (gw_trans->gw_conf->got_show_repo_description) {
723 description = gw_html_escape(
724 gw_get_repo_description(gw_trans,
725 gw_trans->repo_path));
726 if (description != NULL &&
727 (strcmp(description, "") != 0)) {
728 if ((asprintf(&description_h,
729 summary_description, description)) == -1)
730 return 0;
732 khttp_puts(gw_trans->gw_req, description_h);
733 free(description);
734 free(description_h);
737 break;
738 case(TEMPL_SITEOWNER):
739 if (gw_trans->gw_conf->got_site_owner != NULL &&
740 gw_trans->gw_conf->got_show_site_owner) {
741 site_owner_name =
742 gw_html_escape(gw_trans->gw_conf->got_site_owner);
743 if ((asprintf(&site_owner_name_h, site_owner,
744 site_owner_name))
745 == -1)
746 return 0;
748 khttp_puts(gw_trans->gw_req, site_owner_name_h);
749 free(site_owner_name);
750 free(site_owner_name_h);
752 break;
753 case(TEMPL_CONTENT):
754 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
755 if (error)
756 khttp_puts(gw_trans->gw_req, error->msg);
758 break;
759 case(TEMPL_REPO_OWNER):
760 if (gw_trans->gw_conf->got_show_repo_owner) {
761 repo_owner = gw_html_escape(gw_get_repo_owner(gw_trans,
762 gw_trans->repo_path));
763 if ((asprintf(&repo_owner_h, summary_repo_owner,
764 repo_owner)) == -1)
765 return 0;
767 if (repo_owner != NULL &&
768 (strcmp(repo_owner, "") != 0)) {
769 khttp_puts(gw_trans->gw_req, repo_owner_h);
772 free(repo_owner_h);
774 break;
775 case(TEMPL_REPO_AGE):
776 if (gw_trans->gw_conf->got_show_repo_age) {
777 repo_age = gw_get_repo_age(gw_trans,
778 gw_trans->repo_path, "refs/heads", TM_LONG);
779 if (repo_age != NULL) {
780 if ((asprintf(&repo_age_h, summary_last_change,
781 repo_age)) == -1)
782 return 0;
783 khttp_puts(gw_trans->gw_req, repo_age_h);
784 free(repo_age);
785 free(repo_age_h);
788 break;
789 case(TEMPL_CLONEURL):
790 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
791 cloneurl = gw_html_escape(gw_get_clone_url(gw_trans,
792 gw_trans->repo_path));
793 if (cloneurl != NULL) {
794 if ((asprintf(&cloneurl_h,
795 summary_cloneurl, cloneurl)) == -1)
796 return 0;
798 khttp_puts(gw_trans->gw_req, cloneurl_h);
799 free(cloneurl);
800 free(cloneurl_h);
804 break;
805 default:
806 return 0;
807 break;
809 return 1;
812 static char *
813 gw_get_repo_description(struct trans *gw_trans, char *dir)
815 FILE *f;
816 char *description = NULL, *d_file = NULL;
817 unsigned int len;
819 if (gw_trans->gw_conf->got_show_repo_description == false)
820 goto err;
822 if ((asprintf(&d_file, "%s/description", dir)) == -1)
823 goto err;
825 if ((f = fopen(d_file, "r")) == NULL)
826 goto err;
828 fseek(f, 0, SEEK_END);
829 len = ftell(f) + 1;
830 fseek(f, 0, SEEK_SET);
831 if ((description = calloc(len, sizeof(char *))) == NULL)
832 goto err;
834 fread(description, 1, len, f);
835 fclose(f);
836 free(d_file);
837 return description;
838 err:
839 if ((asprintf(&description, "%s", "")) == -1)
840 return NULL;
842 return description;
845 static char *
846 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
848 const struct got_error *error = NULL;
849 struct got_object_id *id = NULL;
850 struct got_repository *repo = NULL;
851 struct got_commit_object *commit = NULL;
852 struct got_reflist_head refs;
853 struct got_reflist_entry *re;
854 struct got_reference *head_ref;
855 struct tm tm;
856 time_t committer_time = 0, cmp_time = 0, diff_time;
857 char *repo_age = NULL, *years = "years ago", *months = "months ago";
858 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
859 char *minutes = "minutes ago", *seconds = "seconds ago";
860 char *now = "right now";
861 char *s;
862 char datebuf[BUFFER_SIZE];
864 if (repo_ref == NULL)
865 return NULL;
867 SIMPLEQ_INIT(&refs);
868 if (gw_trans->gw_conf->got_show_repo_age == false) {
869 asprintf(&repo_age, "");
870 return repo_age;
872 error = got_repo_open(&repo, dir, NULL);
873 if (error != NULL)
874 goto err;
876 error = got_ref_list(&refs, repo, repo_ref, got_ref_cmp_by_name,
877 NULL);
878 if (error != NULL)
879 goto err;
881 const char *refname;
882 SIMPLEQ_FOREACH(re, &refs, entry) {
883 refname = got_ref_get_name(re->ref);
884 error = got_ref_open(&head_ref, repo, refname, 0);
885 if (error != NULL)
886 goto err;
888 error = got_ref_resolve(&id, repo, head_ref);
889 got_ref_close(head_ref);
890 if (error != NULL)
891 goto err;
893 /* here is what breaks tags, so adjust */
894 error = got_object_open_as_commit(&commit, repo, id);
895 if (error != NULL)
896 goto err;
898 committer_time =
899 got_object_commit_get_committer_time(commit);
901 if (cmp_time < committer_time)
902 cmp_time = committer_time;
905 if (cmp_time != 0)
906 committer_time = cmp_time;
908 switch (ref_tm) {
909 case TM_DIFF:
910 diff_time = time(NULL) - committer_time;
911 if (diff_time > 60 * 60 * 24 * 365 * 2) {
912 if ((asprintf(&repo_age, "%lld %s",
913 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
914 return NULL;
915 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
916 if ((asprintf(&repo_age, "%lld %s",
917 (diff_time / 60 / 60 / 24 / (365 / 12)),
918 months)) == -1)
919 return NULL;
920 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
921 if ((asprintf(&repo_age, "%lld %s",
922 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
923 return NULL;
924 } else if (diff_time > 60 * 60 * 24 * 2) {
925 if ((asprintf(&repo_age, "%lld %s",
926 (diff_time / 60 / 60 / 24), days)) == -1)
927 return NULL;
928 } else if (diff_time > 60 * 60 * 2) {
929 if ((asprintf(&repo_age, "%lld %s",
930 (diff_time / 60 / 60), hours)) == -1)
931 return NULL;
932 } else if (diff_time > 60 * 2) {
933 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
934 minutes)) == -1)
935 return NULL;
936 } else if (diff_time > 2) {
937 if ((asprintf(&repo_age, "%lld %s", diff_time,
938 seconds)) == -1)
939 return NULL;
940 } else {
941 if ((asprintf(&repo_age, "%s", now)) == -1)
942 return NULL;
944 break;
945 case TM_LONG:
946 if (cmp_time != 0) {
947 if (gmtime_r(&committer_time, &tm) == NULL)
948 return NULL;
950 s = asctime_r(&tm, datebuf);
951 if (s == NULL)
952 return NULL;
954 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
955 return NULL;
956 } else {
957 if ((asprintf(&repo_age, "")) == -1)
958 return NULL;
960 break;
963 noref:
964 got_ref_list_free(&refs);
965 free(id);
966 return repo_age;
967 err:
968 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
969 return NULL;
971 return repo_age;
974 static char *
975 gw_get_repo_owner(struct trans *gw_trans, char *dir)
977 FILE *f;
978 char *owner = NULL, *d_file = NULL;
979 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
980 char *comp, *pos, *buf;
981 unsigned int i;
983 if (gw_trans->gw_conf->got_show_repo_owner == false)
984 goto err;
986 if ((asprintf(&d_file, "%s/config", dir)) == -1)
987 goto err;
989 if ((f = fopen(d_file, "r")) == NULL)
990 goto err;
992 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
993 goto err;
995 while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
996 if ((pos = strstr(buf, gotweb)) != NULL)
997 break;
999 if ((pos = strstr(buf, gitweb)) != NULL)
1000 break;
1003 if (pos == NULL)
1004 goto err;
1006 do {
1007 fgets(buf, BUFFER_SIZE, f);
1008 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1010 if (comp == NULL)
1011 goto err;
1013 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1014 goto err;
1016 for (i = 0; i < 2; i++) {
1017 owner = strsep(&buf, "\"");
1020 if (owner == NULL)
1021 goto err;
1023 fclose(f);
1024 free(d_file);
1025 return owner;
1026 err:
1027 if ((asprintf(&owner, "%s", "")) == -1)
1028 return NULL;
1030 return owner;
1033 static char *
1034 gw_get_clone_url(struct trans *gw_trans, char *dir)
1036 FILE *f;
1037 char *url = NULL, *d_file = NULL;
1038 unsigned int len;
1040 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1041 return NULL;
1043 if ((f = fopen(d_file, "r")) == NULL)
1044 return NULL;
1046 fseek(f, 0, SEEK_END);
1047 len = ftell(f) + 1;
1048 fseek(f, 0, SEEK_SET);
1050 if ((url = calloc(len, sizeof(char *))) == NULL)
1051 return NULL;
1053 fread(url, 1, len, f);
1054 fclose(f);
1055 free(d_file);
1056 return url;
1059 static char *
1060 gw_get_got_link(struct trans *gw_trans)
1062 char *link;
1064 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
1065 gw_trans->gw_conf->got_logo)) == -1)
1066 return NULL;
1068 return link;
1071 static char *
1072 gw_get_site_link(struct trans *gw_trans)
1074 char *link, *repo = "", *action = "";
1076 if (gw_trans->repo_name != NULL)
1077 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
1078 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
1079 return NULL;
1081 if (gw_trans->action_name != NULL)
1082 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
1083 return NULL;
1085 if ((asprintf(&link, site_link, GOTWEB,
1086 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
1087 return NULL;
1089 return link;
1092 static char *
1093 gw_html_escape(const char *html)
1095 char *escaped_str = NULL, *buf;
1096 char c[1];
1097 size_t sz, i;
1099 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1100 return NULL;
1102 if (html == NULL)
1103 return NULL;
1104 else
1105 if ((sz = strlen(html)) == 0)
1106 return NULL;
1108 /* only work with BUFFER_SIZE */
1109 if (BUFFER_SIZE < sz)
1110 sz = BUFFER_SIZE;
1112 for (i = 0; i < sz; i++) {
1113 c[0] = html[i];
1114 switch (c[0]) {
1115 case ('>'):
1116 strcat(buf, "&gt;");
1117 break;
1118 case ('&'):
1119 strcat(buf, "&amp;");
1120 break;
1121 case ('<'):
1122 strcat(buf, "&lt;");
1123 break;
1124 case ('"'):
1125 strcat(buf, "&quot;");
1126 break;
1127 case ('\''):
1128 strcat(buf, "&apos;");
1129 break;
1130 case ('\n'):
1131 strcat(buf, "<br />");
1132 default:
1133 strcat(buf, &c[0]);
1134 break;
1137 asprintf(&escaped_str, "%s", buf);
1138 free(buf);
1139 return escaped_str;
1142 int
1143 main()
1145 const struct got_error *error = NULL;
1146 struct trans *gw_trans;
1147 struct gw_dir *dir = NULL, *tdir;
1148 const char *page = "index";
1149 bool gw_malloc = true;
1151 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
1152 errx(1, "malloc");
1154 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
1155 errx(1, "malloc");
1157 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
1158 errx(1, "malloc");
1160 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
1161 errx(1, "malloc");
1163 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
1164 &page, 1, 0))
1165 errx(1, "khttp_parse");
1167 if ((gw_trans->gw_conf =
1168 malloc(sizeof(struct gotweb_conf))) == NULL) {
1169 gw_malloc = false;
1170 error = got_error_from_errno("malloc");
1171 goto err;
1174 TAILQ_INIT(&gw_trans->gw_dirs);
1176 gw_trans->page = 0;
1177 gw_trans->repos_total = 0;
1178 gw_trans->repo_path = NULL;
1179 gw_trans->commit = NULL;
1180 gw_trans->mime = KMIME_TEXT_HTML;
1181 gw_trans->gw_tmpl->key = templs;
1182 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
1183 gw_trans->gw_tmpl->arg = gw_trans;
1184 gw_trans->gw_tmpl->cb = gw_template;
1185 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
1187 err:
1188 if (error) {
1189 gw_trans->mime = KMIME_TEXT_PLAIN;
1190 gw_trans->action = GW_ERR;
1191 gw_display_index(gw_trans, error);
1192 goto done;
1195 error = gw_parse_querystring(gw_trans);
1196 if (error)
1197 goto err;
1199 gw_display_index(gw_trans, error);
1201 done:
1202 if (gw_malloc) {
1203 free(gw_trans->gw_conf->got_repos_path);
1204 free(gw_trans->gw_conf->got_www_path);
1205 free(gw_trans->gw_conf->got_site_name);
1206 free(gw_trans->gw_conf->got_site_owner);
1207 free(gw_trans->gw_conf->got_site_link);
1208 free(gw_trans->gw_conf->got_logo);
1209 free(gw_trans->gw_conf->got_logo_url);
1210 free(gw_trans->gw_conf);
1211 free(gw_trans->commit);
1212 free(gw_trans->repo_path);
1213 free(gw_trans->repo_name);
1214 free(gw_trans->repo_file);
1215 free(gw_trans->action_name);
1217 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
1218 free(dir->name);
1219 free(dir->description);
1220 free(dir->age);
1221 free(dir->url);
1222 free(dir->path);
1223 free(dir);
1228 khttp_free(gw_trans->gw_req);
1229 return EXIT_SUCCESS;