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 <regex.h>
26 #include <stdarg.h>
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
37 #include <got_path.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
40 #include <got_diff.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
46 #include <kcgi.h>
47 #include <kcgihtml.h>
49 #include "buf.h"
50 #include "gotweb.h"
51 #include "gotweb_ui.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 struct trans {
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
63 struct kreq *gw_req;
64 char *repo_name;
65 char *repo_path;
66 char *commit;
67 char *repo_file;
68 char *action_name;
69 unsigned int action;
70 unsigned int page;
71 unsigned int repos_total;
72 enum kmime mime;
73 };
75 enum gw_key {
76 KEY_PATH,
77 KEY_ACTION,
78 KEY_COMMIT_ID,
79 KEY_FILE,
80 KEY_PAGE,
81 KEY__MAX
82 };
84 struct gw_dir {
85 TAILQ_ENTRY(gw_dir) entry;
86 char *name;
87 char *owner;
88 char *description;
89 char *url;
90 char *age;
91 char *path;
92 };
94 enum tmpl {
95 TEMPL_HEAD,
96 TEMPL_HEADER,
97 TEMPL_SITEPATH,
98 TEMPL_SITEOWNER,
99 TEMPL_TITLE,
100 TEMPL_SEARCH,
101 TEMPL_CONTENT,
102 TEMPL__MAX
103 };
105 enum ref_tm {
106 TM_DIFF,
107 TM_LONG,
108 };
110 struct buf {
111 /* buffer handle, buffer size, and data length */
112 u_char *cb_buf;
113 size_t cb_size;
114 size_t cb_len;
115 };
117 static const char *const templs[TEMPL__MAX] = {
118 "head",
119 "header",
120 "sitepath",
121 "siteowner",
122 "title",
123 "search",
124 "content",
125 };
127 static const struct kvalid gw_keys[KEY__MAX] = {
128 { kvalid_stringne, "path" },
129 { kvalid_stringne, "action" },
130 { kvalid_stringne, "commit" },
131 { kvalid_stringne, "file" },
132 { kvalid_int, "page" },
133 };
135 static struct gw_dir *gw_init_gw_dir(char *);
137 static char *gw_get_repo_description(struct trans *,
138 char *);
139 static char *gw_get_repo_owner(struct trans *,
140 char *);
141 static char *gw_get_time_str(time_t, int);
142 static char *gw_get_repo_age(struct trans *,
143 char *, char *, int);
144 static char *gw_get_repo_log(struct trans *, const char *,
145 char *, int, int);
146 static char *gw_get_repo_tags(struct trans *);
147 static char *gw_get_repo_heads(struct trans *);
148 static char *gw_get_clone_url(struct trans *, char *);
149 static char *gw_get_got_link(struct trans *);
150 static char *gw_get_site_link(struct trans *);
151 static char *gw_html_escape(const char *);
153 static void gw_display_open(struct trans *, enum khttp,
154 enum kmime);
155 static void gw_display_index(struct trans *,
156 const struct got_error *);
158 static int gw_template(size_t, void *);
160 static const struct got_error* apply_unveil(const char *, const char *);
161 static const struct got_error* gw_load_got_paths(struct trans *);
162 static const struct got_error* gw_load_got_path(struct trans *,
163 struct gw_dir *);
164 static const struct got_error* gw_parse_querystring(struct trans *);
165 static const struct got_error* match_logmsg(int *, struct got_object_id *,
166 struct got_commit_object *, regex_t *);
168 static const struct got_error* gw_blame(struct trans *);
169 static const struct got_error* gw_blob(struct trans *);
170 static const struct got_error* gw_blob_diff(struct trans *);
171 static const struct got_error* gw_commit(struct trans *);
172 static const struct got_error* gw_commit_diff(struct trans *);
173 static const struct got_error* gw_history(struct trans *);
174 static const struct got_error* gw_index(struct trans *);
175 static const struct got_error* gw_log(struct trans *);
176 static const struct got_error* gw_raw(struct trans *);
177 static const struct got_error* gw_logbriefs(struct trans *);
178 static const struct got_error* gw_snapshot(struct trans *);
179 static const struct got_error* gw_summary(struct trans *);
180 static const struct got_error* gw_tree(struct trans *);
182 struct gw_query_action {
183 unsigned int func_id;
184 const char *func_name;
185 const struct got_error *(*func_main)(struct trans *);
186 char *template;
187 };
189 enum gw_query_actions {
190 GW_BLAME,
191 GW_BLOB,
192 GW_BLOBDIFF,
193 GW_COMMIT,
194 GW_COMMITDIFF,
195 GW_ERR,
196 GW_HISTORY,
197 GW_INDEX,
198 GW_LOG,
199 GW_RAW,
200 GW_LOGBRIEFS,
201 GW_SNAPSHOT,
202 GW_SUMMARY,
203 GW_TREE
204 };
206 static struct gw_query_action gw_query_funcs[] = {
207 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
208 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
209 { GW_BLOBDIFF, "blobdiff", gw_blob_diff, "gw_tmpl/index.tmpl" },
210 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
211 { GW_COMMITDIFF, "commit_diff", gw_commit_diff, "gw_tmpl/index.tmpl" },
212 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
213 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
214 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
215 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
216 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
217 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
218 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
219 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
220 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
221 };
223 static const struct got_error *
224 apply_unveil(const char *repo_path, const char *repo_file)
226 const struct got_error *err;
228 if (repo_path && repo_file) {
229 char *full_path;
230 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
231 return got_error_from_errno("asprintf unveil");
232 if (unveil(full_path, "r") != 0)
233 return got_error_from_errno2("unveil", full_path);
236 if (repo_path && unveil(repo_path, "r") != 0)
237 return got_error_from_errno2("unveil", repo_path);
239 if (unveil("/tmp", "rwc") != 0)
240 return got_error_from_errno2("unveil", "/tmp");
242 err = got_privsep_unveil_exec_helpers();
243 if (err != NULL)
244 return err;
246 if (unveil(NULL, NULL) != 0)
247 return got_error_from_errno("unveil");
249 return NULL;
252 static const struct got_error *
253 gw_blame(struct trans *gw_trans)
255 const struct got_error *error = NULL;
257 return error;
260 static const struct got_error *
261 gw_blob(struct trans *gw_trans)
263 const struct got_error *error = NULL;
265 return error;
268 static const struct got_error *
269 gw_blob_diff(struct trans *gw_trans)
271 const struct got_error *error = NULL;
273 return error;
276 static const struct got_error *
277 gw_commit(struct trans *gw_trans)
279 const struct got_error *error = NULL;
281 return error;
284 static const struct got_error *
285 gw_commit_diff(struct trans *gw_trans)
287 const struct got_error *error = NULL;
289 return error;
292 static const struct got_error *
293 gw_history(struct trans *gw_trans)
295 const struct got_error *error = NULL;
297 return error;
300 static const struct got_error *
301 gw_index(struct trans *gw_trans)
303 const struct got_error *error = NULL;
304 struct gw_dir *gw_dir = NULL;
305 char *html, *navs, *next, *prev;
306 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
308 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
309 if (error)
310 return error;
312 error = gw_load_got_paths(gw_trans);
313 if (error)
314 return error;
316 khttp_puts(gw_trans->gw_req, index_projects_header);
318 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
319 dir_c++;
321 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
322 if (gw_trans->page > 0 && (gw_trans->page *
323 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
324 prev_disp++;
325 continue;
328 prev_disp++;
329 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
330 gw_dir->name, gw_dir->name)) == -1)
331 return got_error_from_errno("asprintf");
333 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
334 gw_dir->description, gw_dir->owner, gw_dir->age,
335 navs)) == -1)
336 return got_error_from_errno("asprintf");
338 khttp_puts(gw_trans->gw_req, html);
340 free(navs);
341 free(html);
343 if (gw_trans->gw_conf->got_max_repos_display == 0)
344 continue;
346 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
347 khttp_puts(gw_trans->gw_req, np_wrapper_start);
348 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
349 (gw_trans->page > 0) &&
350 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
351 prev_disp == gw_trans->repos_total))
352 khttp_puts(gw_trans->gw_req, np_wrapper_start);
354 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
355 (gw_trans->page > 0) &&
356 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
357 prev_disp == gw_trans->repos_total)) {
358 if ((asprintf(&prev, nav_prev,
359 gw_trans->page - 1)) == -1)
360 return got_error_from_errno("asprintf");
361 khttp_puts(gw_trans->gw_req, prev);
362 free(prev);
365 khttp_puts(gw_trans->gw_req, div_end);
367 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
368 next_disp == gw_trans->gw_conf->got_max_repos_display &&
369 dir_c != (gw_trans->page + 1) *
370 gw_trans->gw_conf->got_max_repos_display) {
371 if ((asprintf(&next, nav_next,
372 gw_trans->page + 1)) == -1)
373 return got_error_from_errno("calloc");
374 khttp_puts(gw_trans->gw_req, next);
375 khttp_puts(gw_trans->gw_req, div_end);
376 free(next);
377 next_disp = 0;
378 break;
381 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
382 (gw_trans->page > 0) &&
383 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
384 prev_disp == gw_trans->repos_total))
385 khttp_puts(gw_trans->gw_req, div_end);
387 next_disp++;
389 return error;
392 static const struct got_error *
393 gw_log(struct trans *gw_trans)
395 const struct got_error *error = NULL;
396 char *log, *log_html;
398 log = gw_get_repo_log(gw_trans, NULL, NULL,
399 gw_trans->gw_conf->got_max_commits_display, 1);
401 if (log != NULL && strcmp(log, "") != 0) {
402 if ((asprintf(&log_html, logs, log)) == -1)
403 return got_error_from_errno("asprintf");
404 khttp_puts(gw_trans->gw_req, log_html);
405 free(log_html);
406 free(log);
408 return error;
411 static const struct got_error *
412 gw_raw(struct trans *gw_trans)
414 const struct got_error *error = NULL;
416 return error;
419 static const struct got_error *
420 gw_logbriefs(struct trans *gw_trans)
422 const struct got_error *error = NULL;
423 char *logbriefs, *logbriefs_html;
425 logbriefs = gw_get_repo_log(gw_trans, NULL, NULL,
426 gw_trans->gw_conf->got_max_commits_display, 0);
428 if (logbriefs != NULL && strcmp(logbriefs, "") != 0) {
429 if ((asprintf(&logbriefs_html, summary_logbriefs,
430 logbriefs)) == -1)
431 return got_error_from_errno("asprintf");
432 khttp_puts(gw_trans->gw_req, logbriefs_html);
433 free(logbriefs_html);
434 free(logbriefs);
436 return error;
439 static const struct got_error *
440 gw_snapshot(struct trans *gw_trans)
442 const struct got_error *error = NULL;
444 return error;
447 static const struct got_error *
448 gw_summary(struct trans *gw_trans)
450 const struct got_error *error = NULL;
451 char *description_html, *repo_owner_html, *repo_age_html,
452 *cloneurl_html, *logbriefs, *tags, *heads, *logbriefs_html,
453 *tags_html, *heads_html, *age;
455 error = apply_unveil(gw_trans->gw_dir->path, NULL);
456 if (error)
457 return error;
459 khttp_puts(gw_trans->gw_req, summary_wrapper);
460 if (gw_trans->gw_conf->got_show_repo_description) {
461 if (gw_trans->gw_dir->description != NULL &&
462 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
463 if ((asprintf(&description_html, description,
464 gw_trans->gw_dir->description)) == -1)
465 return got_error_from_errno("asprintf");
467 khttp_puts(gw_trans->gw_req, description_html);
468 free(description_html);
472 if (gw_trans->gw_conf->got_show_repo_owner) {
473 if (gw_trans->gw_dir->owner != NULL &&
474 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
475 if ((asprintf(&repo_owner_html, repo_owner,
476 gw_trans->gw_dir->owner)) == -1)
477 return got_error_from_errno("asprintf");
479 khttp_puts(gw_trans->gw_req, repo_owner_html);
480 free(repo_owner_html);
484 if (gw_trans->gw_conf->got_show_repo_age) {
485 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
486 "refs/heads", TM_LONG);
487 if (age != NULL && (strcmp(age, "") != 0)) {
488 if ((asprintf(&repo_age_html, last_change, age)) == -1)
489 return got_error_from_errno("asprintf");
491 khttp_puts(gw_trans->gw_req, repo_age_html);
492 free(repo_age_html);
493 free(age);
497 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
498 if (gw_trans->gw_dir->url != NULL &&
499 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
500 if ((asprintf(&cloneurl_html, cloneurl,
501 gw_trans->gw_dir->url)) == -1)
502 return got_error_from_errno("asprintf");
504 khttp_puts(gw_trans->gw_req, cloneurl_html);
505 free(cloneurl_html);
508 khttp_puts(gw_trans->gw_req, div_end);
510 logbriefs = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
511 tags = gw_get_repo_tags(gw_trans);
512 heads = gw_get_repo_heads(gw_trans);
514 if (logbriefs != NULL && strcmp(logbriefs, "") != 0) {
515 if ((asprintf(&logbriefs_html, summary_logbriefs,
516 logbriefs)) == -1)
517 return got_error_from_errno("asprintf");
518 khttp_puts(gw_trans->gw_req, logbriefs_html);
519 free(logbriefs_html);
520 free(logbriefs);
523 if (tags != NULL && strcmp(tags, "") != 0) {
524 if ((asprintf(&tags_html, summary_tags,
525 tags)) == -1)
526 return got_error_from_errno("asprintf");
527 khttp_puts(gw_trans->gw_req, tags_html);
528 free(tags_html);
529 free(tags);
532 if (heads != NULL && strcmp(heads, "") != 0) {
533 if ((asprintf(&heads_html, summary_heads,
534 heads)) == -1)
535 return got_error_from_errno("asprintf");
536 khttp_puts(gw_trans->gw_req, heads_html);
537 free(heads_html);
538 free(heads);
541 return error;
544 static const struct got_error *
545 gw_tree(struct trans *gw_trans)
547 const struct got_error *error = NULL;
549 return error;
552 static const struct got_error *
553 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
555 const struct got_error *error = NULL;
556 DIR *dt;
557 char *dir_test;
558 int opened = 0;
560 if ((asprintf(&dir_test, "%s/%s/%s",
561 gw_trans->gw_conf->got_repos_path, gw_dir->name,
562 GOTWEB_GIT_DIR)) == -1)
563 return got_error_from_errno("asprintf");
565 dt = opendir(dir_test);
566 if (dt == NULL) {
567 free(dir_test);
568 } else {
569 gw_dir->path = strdup(dir_test);
570 opened = 1;
571 goto done;
574 if ((asprintf(&dir_test, "%s/%s/%s",
575 gw_trans->gw_conf->got_repos_path, gw_dir->name,
576 GOTWEB_GOT_DIR)) == -1)
577 return got_error_from_errno("asprintf");
579 dt = opendir(dir_test);
580 if (dt == NULL)
581 free(dir_test);
582 else {
583 opened = 1;
584 error = got_error(GOT_ERR_NOT_GIT_REPO);
585 goto errored;
588 if ((asprintf(&dir_test, "%s/%s",
589 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
590 return got_error_from_errno("asprintf");
592 gw_dir->path = strdup(dir_test);
594 done:
595 gw_dir->description = gw_get_repo_description(gw_trans,
596 gw_dir->path);
597 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
598 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
599 TM_DIFF);
600 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
602 errored:
603 free(dir_test);
604 if (opened)
605 closedir(dt);
606 return error;
609 static const struct got_error *
610 gw_load_got_paths(struct trans *gw_trans)
612 const struct got_error *error = NULL;
613 DIR *d;
614 struct dirent **sd_dent;
615 struct gw_dir *gw_dir;
616 struct stat st;
617 unsigned int d_cnt, d_i;
619 d = opendir(gw_trans->gw_conf->got_repos_path);
620 if (d == NULL) {
621 error = got_error_from_errno2("opendir",
622 gw_trans->gw_conf->got_repos_path);
623 return error;
626 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
627 alphasort);
628 if (d_cnt == -1) {
629 error = got_error_from_errno2("scandir",
630 gw_trans->gw_conf->got_repos_path);
631 return error;
634 for (d_i = 0; d_i < d_cnt; d_i++) {
635 if (gw_trans->gw_conf->got_max_repos > 0 &&
636 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
637 break; /* account for parent and self */
639 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
640 strcmp(sd_dent[d_i]->d_name, "..") == 0)
641 continue;
643 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
644 return got_error_from_errno("gw_dir malloc");
646 error = gw_load_got_path(gw_trans, gw_dir);
647 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
648 continue;
649 else if (error)
650 return error;
652 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
653 !got_path_dir_is_empty(gw_dir->path)) {
654 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
655 entry);
656 gw_trans->repos_total++;
660 closedir(d);
661 return error;
664 static const struct got_error *
665 gw_parse_querystring(struct trans *gw_trans)
667 const struct got_error *error = NULL;
668 struct kpair *p;
669 struct gw_query_action *action = NULL;
670 unsigned int i;
672 if (gw_trans->gw_req->fieldnmap[0]) {
673 error = got_error_from_errno("bad parse");
674 return error;
675 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
676 /* define gw_trans->repo_path */
677 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
678 return got_error_from_errno("asprintf");
680 if ((asprintf(&gw_trans->repo_path, "%s/%s",
681 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
682 return got_error_from_errno("asprintf");
684 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
685 if ((asprintf(&gw_trans->commit, "%s",
686 p->parsed.s)) == -1)
687 return got_error_from_errno("asprintf");
689 /* get action and set function */
690 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
691 for (i = 0; i < nitems(gw_query_funcs); i++) {
692 action = &gw_query_funcs[i];
693 if (action->func_name == NULL)
694 continue;
696 if (strcmp(action->func_name,
697 p->parsed.s) == 0) {
698 gw_trans->action = i;
699 if ((asprintf(&gw_trans->action_name,
700 "%s", action->func_name)) == -1)
701 return
702 got_error_from_errno(
703 "asprintf");
705 break;
708 action = NULL;
711 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
712 if ((asprintf(&gw_trans->repo_file, "%s",
713 p->parsed.s)) == -1)
714 return got_error_from_errno("asprintf");
716 if (action == NULL) {
717 error = got_error_from_errno("invalid action");
718 return error;
720 if ((gw_trans->gw_dir =
721 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
722 return got_error_from_errno("gw_dir malloc");
724 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
725 if (error)
726 return error;
727 } else
728 gw_trans->action = GW_INDEX;
730 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
731 gw_trans->page = p->parsed.i;
733 if (gw_trans->action == GW_RAW)
734 gw_trans->mime = KMIME_TEXT_PLAIN;
736 return error;
739 static struct gw_dir *
740 gw_init_gw_dir(char *dir)
742 struct gw_dir *gw_dir;
744 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
745 return NULL;
747 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
748 return NULL;
750 return gw_dir;
753 static const struct got_error*
754 match_logmsg(int *have_match, struct got_object_id *id,
755 struct got_commit_object *commit, regex_t *regex)
757 const struct got_error *err = NULL;
758 regmatch_t regmatch;
759 char *id_str = NULL, *logmsg = NULL;
761 *have_match = 0;
763 err = got_object_id_str(&id_str, id);
764 if (err)
765 return err;
767 err = got_object_commit_get_logmsg(&logmsg, commit);
768 if (err)
769 goto done;
771 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
772 *have_match = 1;
773 done:
774 free(id_str);
775 free(logmsg);
776 return err;
779 static void
780 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
782 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
783 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
784 khttps[code]);
785 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
786 kmimetypes[mime]);
787 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
788 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
789 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
790 khttp_body(gw_trans->gw_req);
793 static void
794 gw_display_index(struct trans *gw_trans, const struct got_error *err)
796 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
797 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
799 if (err)
800 khttp_puts(gw_trans->gw_req, err->msg);
801 else
802 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
803 gw_query_funcs[gw_trans->action].template);
805 khtml_close(gw_trans->gw_html_req);
808 static int
809 gw_template(size_t key, void *arg)
811 const struct got_error *error = NULL;
812 struct trans *gw_trans = arg;
813 char *gw_got_link, *gw_site_link;
814 char *site_owner_name, *site_owner_name_h;
816 switch (key) {
817 case (TEMPL_HEAD):
818 khttp_puts(gw_trans->gw_req, head);
819 break;
820 case(TEMPL_HEADER):
821 gw_got_link = gw_get_got_link(gw_trans);
822 if (gw_got_link != NULL)
823 khttp_puts(gw_trans->gw_req, gw_got_link);
825 free(gw_got_link);
826 break;
827 case (TEMPL_SITEPATH):
828 gw_site_link = gw_get_site_link(gw_trans);
829 if (gw_site_link != NULL)
830 khttp_puts(gw_trans->gw_req, gw_site_link);
832 free(gw_site_link);
833 break;
834 case(TEMPL_TITLE):
835 if (gw_trans->gw_conf->got_site_name != NULL)
836 khtml_puts(gw_trans->gw_html_req,
837 gw_trans->gw_conf->got_site_name);
839 break;
840 case (TEMPL_SEARCH):
841 khttp_puts(gw_trans->gw_req, search);
842 break;
843 case(TEMPL_SITEOWNER):
844 if (gw_trans->gw_conf->got_site_owner != NULL &&
845 gw_trans->gw_conf->got_show_site_owner) {
846 site_owner_name =
847 gw_html_escape(gw_trans->gw_conf->got_site_owner);
848 if ((asprintf(&site_owner_name_h, site_owner,
849 site_owner_name))
850 == -1)
851 return 0;
853 khttp_puts(gw_trans->gw_req, site_owner_name_h);
854 free(site_owner_name);
855 free(site_owner_name_h);
857 break;
858 case(TEMPL_CONTENT):
859 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
860 if (error)
861 khttp_puts(gw_trans->gw_req, error->msg);
863 break;
864 default:
865 return 0;
866 break;
868 return 1;
871 static char *
872 gw_get_repo_description(struct trans *gw_trans, char *dir)
874 FILE *f;
875 char *description = NULL, *d_file = NULL;
876 unsigned int len;
878 if (gw_trans->gw_conf->got_show_repo_description == false)
879 goto err;
881 if ((asprintf(&d_file, "%s/description", dir)) == -1)
882 goto err;
884 if ((f = fopen(d_file, "r")) == NULL)
885 goto err;
887 fseek(f, 0, SEEK_END);
888 len = ftell(f) + 1;
889 fseek(f, 0, SEEK_SET);
890 if ((description = calloc(len, sizeof(char *))) == NULL)
891 goto err;
893 fread(description, 1, len, f);
894 fclose(f);
895 free(d_file);
896 return description;
897 err:
898 if ((asprintf(&description, "%s", "")) == -1)
899 return NULL;
901 return description;
904 static char *
905 gw_get_time_str(time_t committer_time, int ref_tm)
907 struct tm tm;
908 time_t diff_time;
909 char *years = "years ago", *months = "months ago";
910 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
911 char *minutes = "minutes ago", *seconds = "seconds ago";
912 char *now = "right now";
913 char *repo_age, *s;
914 char datebuf[BUFFER_SIZE];
916 switch (ref_tm) {
917 case TM_DIFF:
918 diff_time = time(NULL) - committer_time;
919 if (diff_time > 60 * 60 * 24 * 365 * 2) {
920 if ((asprintf(&repo_age, "%lld %s",
921 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
922 return NULL;
923 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
924 if ((asprintf(&repo_age, "%lld %s",
925 (diff_time / 60 / 60 / 24 / (365 / 12)),
926 months)) == -1)
927 return NULL;
928 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
929 if ((asprintf(&repo_age, "%lld %s",
930 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
931 return NULL;
932 } else if (diff_time > 60 * 60 * 24 * 2) {
933 if ((asprintf(&repo_age, "%lld %s",
934 (diff_time / 60 / 60 / 24), days)) == -1)
935 return NULL;
936 } else if (diff_time > 60 * 60 * 2) {
937 if ((asprintf(&repo_age, "%lld %s",
938 (diff_time / 60 / 60), hours)) == -1)
939 return NULL;
940 } else if (diff_time > 60 * 2) {
941 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
942 minutes)) == -1)
943 return NULL;
944 } else if (diff_time > 2) {
945 if ((asprintf(&repo_age, "%lld %s", diff_time,
946 seconds)) == -1)
947 return NULL;
948 } else {
949 if ((asprintf(&repo_age, "%s", now)) == -1)
950 return NULL;
952 break;
953 case TM_LONG:
954 if (gmtime_r(&committer_time, &tm) == NULL)
955 return NULL;
957 s = asctime_r(&tm, datebuf);
958 if (s == NULL)
959 return NULL;
961 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
962 return NULL;
963 break;
965 return repo_age;
968 static char *
969 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
971 const struct got_error *error = NULL;
972 struct got_object_id *id = NULL;
973 struct got_repository *repo = NULL;
974 struct got_commit_object *commit = NULL;
975 struct got_reflist_head refs;
976 struct got_reflist_entry *re;
977 struct got_reference *head_ref;
978 time_t committer_time = 0, cmp_time = 0;
979 char *repo_age = NULL;
981 if (repo_ref == NULL)
982 return NULL;
984 SIMPLEQ_INIT(&refs);
985 if (gw_trans->gw_conf->got_show_repo_age == false) {
986 asprintf(&repo_age, "");
987 return repo_age;
989 error = got_repo_open(&repo, dir, NULL);
990 if (error != NULL)
991 goto err;
993 error = got_ref_list(&refs, repo, repo_ref, got_ref_cmp_by_name,
994 NULL);
995 if (error != NULL)
996 goto err;
998 const char *refname;
999 SIMPLEQ_FOREACH(re, &refs, entry) {
1000 refname = got_ref_get_name(re->ref);
1001 error = got_ref_open(&head_ref, repo, refname, 0);
1002 if (error != NULL)
1003 goto err;
1005 error = got_ref_resolve(&id, repo, head_ref);
1006 got_ref_close(head_ref);
1007 if (error != NULL)
1008 goto err;
1010 /* here is what breaks tags, so adjust */
1011 error = got_object_open_as_commit(&commit, repo, id);
1012 if (error != NULL)
1013 goto err;
1015 committer_time =
1016 got_object_commit_get_committer_time(commit);
1018 if (cmp_time < committer_time)
1019 cmp_time = committer_time;
1022 if (cmp_time != 0) {
1023 committer_time = cmp_time;
1024 repo_age = gw_get_time_str(committer_time, ref_tm);
1025 } else
1026 if ((asprintf(&repo_age, "")) == -1)
1027 return NULL;
1028 got_ref_list_free(&refs);
1029 free(id);
1030 return repo_age;
1031 err:
1032 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1033 return NULL;
1035 return repo_age;
1038 static char *
1039 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1041 FILE *f;
1042 char *owner = NULL, *d_file = NULL;
1043 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1044 char *comp, *pos, *buf;
1045 unsigned int i;
1047 if (gw_trans->gw_conf->got_show_repo_owner == false)
1048 goto err;
1050 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1051 goto err;
1053 if ((f = fopen(d_file, "r")) == NULL)
1054 goto err;
1056 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1057 goto err;
1059 while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
1060 if ((pos = strstr(buf, gotweb)) != NULL)
1061 break;
1063 if ((pos = strstr(buf, gitweb)) != NULL)
1064 break;
1067 if (pos == NULL)
1068 goto err;
1070 do {
1071 fgets(buf, BUFFER_SIZE, f);
1072 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1074 if (comp == NULL)
1075 goto err;
1077 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1078 goto err;
1080 for (i = 0; i < 2; i++) {
1081 owner = strsep(&buf, "\"");
1084 if (owner == NULL)
1085 goto err;
1087 fclose(f);
1088 free(d_file);
1089 return owner;
1090 err:
1091 if ((asprintf(&owner, "%s", "")) == -1)
1092 return NULL;
1094 return owner;
1097 static char *
1098 gw_get_clone_url(struct trans *gw_trans, char *dir)
1100 FILE *f;
1101 char *url = NULL, *d_file = NULL;
1102 unsigned int len;
1104 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1105 return NULL;
1107 if ((f = fopen(d_file, "r")) == NULL)
1108 return NULL;
1110 fseek(f, 0, SEEK_END);
1111 len = ftell(f) + 1;
1112 fseek(f, 0, SEEK_SET);
1114 if ((url = calloc(len, sizeof(char *))) == NULL)
1115 return NULL;
1117 fread(url, 1, len, f);
1118 fclose(f);
1119 free(d_file);
1120 return url;
1123 static char *
1124 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1125 char *start_commit, int limit, int full_log)
1127 const struct got_error *error;
1128 struct got_repository *repo = NULL;
1129 struct got_reflist_head refs;
1130 struct got_reflist_entry *re;
1131 struct got_commit_object *commit = NULL;
1132 struct got_object_id *id = NULL;
1133 struct got_commit_graph *graph = NULL;
1134 char *logbriefs = NULL, *id_str = NULL, *path = NULL,
1135 *in_repo_path = NULL, *commit_row = NULL, *commit_age = NULL,
1136 *commit_author = NULL, *commit_log = NULL, *commit_log0, *newline,
1137 *logbriefs_navs_html = NULL;
1138 regex_t regex;
1139 int have_match;
1140 size_t newsize;
1141 struct buf *diffbuf;
1142 time_t committer_time;
1144 if (search_pattern &&
1145 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1146 REG_NEWLINE))
1147 return NULL;
1149 SIMPLEQ_INIT(&refs);
1151 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1152 if (error != NULL)
1153 goto done;
1155 error = buf_alloc(&diffbuf, BUFFER_SIZE);
1156 if (error != NULL)
1157 goto done;
1159 if (start_commit == NULL) {
1160 struct got_reference *head_ref;
1161 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1162 if (error != NULL)
1163 return NULL;
1164 error = got_ref_resolve(&id, repo, head_ref);
1165 got_ref_close(head_ref);
1166 if (error != NULL)
1167 return NULL;
1168 error = got_object_open_as_commit(&commit, repo, id);
1169 } else {
1170 struct got_reference *ref;
1171 error = got_ref_open(&ref, repo, start_commit, 0);
1172 if (error == NULL) {
1173 int obj_type;
1174 error = got_ref_resolve(&id, repo, ref);
1175 got_ref_close(ref);
1176 if (error != NULL)
1177 goto done;
1178 error = got_object_get_type(&obj_type, repo, id);
1179 if (error != NULL)
1180 goto done;
1181 if (obj_type == GOT_OBJ_TYPE_TAG) {
1182 struct got_tag_object *tag;
1183 error = got_object_open_as_tag(&tag, repo, id);
1184 if (error != NULL)
1185 goto done;
1186 if (got_object_tag_get_object_type(tag) !=
1187 GOT_OBJ_TYPE_COMMIT) {
1188 got_object_tag_close(tag);
1189 error = got_error(GOT_ERR_OBJ_TYPE);
1190 goto done;
1192 free(id);
1193 id = got_object_id_dup(
1194 got_object_tag_get_object_id(tag));
1195 if (id == NULL)
1196 error = got_error_from_errno(
1197 "got_object_id_dup");
1198 got_object_tag_close(tag);
1199 if (error)
1200 goto done;
1201 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1202 error = got_error(GOT_ERR_OBJ_TYPE);
1203 goto done;
1205 error = got_object_open_as_commit(&commit, repo, id);
1206 if (error != NULL)
1207 goto done;
1209 if (commit == NULL) {
1210 error = got_repo_match_object_id_prefix(&id,
1211 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1212 if (error != NULL)
1213 return NULL;
1217 if (error != NULL)
1218 goto done;
1220 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1221 if (error != NULL)
1222 goto done;
1224 if (in_repo_path) {
1225 free(path);
1226 path = in_repo_path;
1229 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1230 if (error)
1231 goto done;
1233 error = got_commit_graph_open(&graph, id, path, 0, repo);
1234 if (error)
1235 goto done;
1237 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
1238 if (error)
1239 goto done;
1241 for (;;) {
1242 struct got_commit_object *commit_disp;
1244 error = got_commit_graph_iter_next(&id, graph);
1245 if (error) {
1246 if (error->code == GOT_ERR_ITER_COMPLETED) {
1247 error = NULL;
1248 break;
1250 if (error->code != GOT_ERR_ITER_NEED_MORE)
1251 break;
1252 error = got_commit_graph_fetch_commits(graph, 1, repo,
1253 NULL, NULL);
1254 if (error)
1255 break;
1256 else
1257 continue;
1259 if (id == NULL)
1260 break;
1262 error = got_object_open_as_commit(&commit_disp, repo, id);
1263 if (error)
1264 break;
1266 if (search_pattern) {
1267 error = match_logmsg(&have_match, id, commit_disp,
1268 &regex);
1269 if (error) {
1270 got_object_commit_close(commit_disp);
1271 break;
1273 if (have_match == 0) {
1274 got_object_commit_close(commit_disp);
1275 continue;
1279 SIMPLEQ_FOREACH(re, &refs, entry) {
1280 const char *name;
1281 struct got_tag_object *tag = NULL;
1282 int cmp;
1284 name = got_ref_get_name(re->ref);
1285 if (strcmp(name, GOT_REF_HEAD) == 0)
1286 continue;
1287 if (strncmp(name, "refs/", 5) == 0)
1288 name += 5;
1289 if (strncmp(name, "got/", 4) == 0)
1290 continue;
1291 if (strncmp(name, "heads/", 6) == 0)
1292 name += 6;
1293 if (strncmp(name, "remotes/", 8) == 0)
1294 name += 8;
1295 if (strncmp(name, "tags/", 5) == 0) {
1296 error = got_object_open_as_tag(&tag, repo,
1297 re->id);
1298 if (error) {
1299 if (error->code != GOT_ERR_OBJ_TYPE)
1300 continue;
1302 * Ref points at something other
1303 * than a tag.
1305 error = NULL;
1306 tag = NULL;
1309 cmp = got_object_id_cmp(tag ?
1310 got_object_tag_get_object_id(tag) : re->id, id);
1311 if (tag)
1312 got_object_tag_close(tag);
1313 if (cmp != 0)
1314 continue;
1317 got_ref_list_free(&refs);
1319 /* commit id */
1320 error = got_object_id_str(&id_str, id);
1321 if (error)
1322 break;
1324 committer_time =
1325 got_object_commit_get_committer_time(commit_disp);
1327 if (full_log) {
1329 asprintf(&commit_age, "%s",
1330 gw_get_time_str(committer_time, TM_LONG));
1331 asprintf(&commit_author, "%s",
1332 got_object_commit_get_author(commit_disp));
1333 error = got_object_commit_get_logmsg(&commit_log0,
1334 commit_disp);
1335 if (error)
1336 commit_log = strdup("");
1337 else
1338 commit_log = gw_html_escape(commit_log0);
1339 asprintf(&logbriefs_navs_html, logbriefs_navs,
1340 gw_trans->repo_name, id_str, gw_trans->repo_name,
1341 id_str, gw_trans->repo_name, id_str,
1342 gw_trans->repo_name, id_str);
1343 asprintf(&commit_row, logs_row, id_str,
1344 gw_html_escape(commit_author), commit_age,
1345 commit_log, logbriefs_navs_html);
1346 error = buf_append(&newsize, diffbuf, commit_row,
1347 strlen(commit_row));
1348 } else {
1349 asprintf(&commit_age, "%s",
1350 gw_get_time_str(committer_time, TM_DIFF));
1351 asprintf(&commit_author, "%s",
1352 got_object_commit_get_author(commit_disp));
1353 error = got_object_commit_get_logmsg(&commit_log0,
1354 commit_disp);
1355 if (error)
1356 commit_log = strdup("");
1357 else {
1358 commit_log = commit_log0;
1359 while (*commit_log == '\n')
1360 commit_log++;
1361 newline = strchr(commit_log, '\n');
1362 if (newline)
1363 *newline = '\0';
1365 asprintf(&logbriefs_navs_html, logbriefs_navs,
1366 gw_trans->repo_name, id_str, gw_trans->repo_name,
1367 id_str, gw_trans->repo_name, id_str,
1368 gw_trans->repo_name, id_str);
1369 asprintf(&commit_row, logbriefs_row, commit_age,
1370 commit_author, commit_log, logbriefs_navs_html);
1371 error = buf_append(&newsize, diffbuf, commit_row,
1372 strlen(commit_row));
1375 free(commit_age);
1376 free(commit_author);
1377 free(commit_log0);
1378 free(logbriefs_navs_html);
1379 free(commit_row);
1380 free(id_str);
1381 commit_age = NULL;
1382 commit_author = NULL;
1383 commit_log = NULL;
1384 logbriefs_navs_html = NULL;
1385 commit_row = NULL;
1386 id_str = NULL;
1388 got_object_commit_close(commit_disp);
1389 if (error || (limit && --limit == 0))
1390 break;
1392 logbriefs = strdup(diffbuf->cb_buf);
1393 got_object_commit_close(commit);
1395 free(path);
1396 free(id);
1397 buf_free(diffbuf);
1399 if (repo) {
1400 error = got_repo_close(repo);
1401 if (error != NULL)
1402 return NULL;
1405 if (search_pattern)
1406 regfree(&regex);
1407 return logbriefs;
1408 done:
1409 if (repo)
1410 got_repo_close(repo);
1411 got_ref_list_free(&refs);
1413 if (search_pattern)
1414 regfree(&regex);
1415 got_commit_graph_close(graph);
1416 return NULL;
1419 static char *
1420 gw_get_repo_tags(struct trans *gw_trans)
1422 char *tags = NULL;
1424 asprintf(&tags, tags_row, "30 min ago", "1.0.0", "tag 1.0.0",
1425 tags_navs);
1426 return tags;
1429 static char *
1430 gw_get_repo_heads(struct trans *gw_trans)
1432 char *heads = NULL;
1434 asprintf(&heads, heads_row, "30 min ago", "master", heads_navs);
1435 return heads;
1438 static char *
1439 gw_get_got_link(struct trans *gw_trans)
1441 char *link;
1443 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
1444 gw_trans->gw_conf->got_logo)) == -1)
1445 return NULL;
1447 return link;
1450 static char *
1451 gw_get_site_link(struct trans *gw_trans)
1453 char *link, *repo = "", *action = "";
1455 if (gw_trans->repo_name != NULL)
1456 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
1457 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
1458 return NULL;
1460 if (gw_trans->action_name != NULL)
1461 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
1462 return NULL;
1464 if ((asprintf(&link, site_link, GOTWEB,
1465 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
1466 return NULL;
1468 return link;
1471 static char *
1472 gw_html_escape(const char *html)
1474 char *escaped_str = NULL, *buf;
1475 char c[1];
1476 size_t sz, i;
1478 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1479 return NULL;
1481 if (html == NULL)
1482 return NULL;
1483 else
1484 if ((sz = strlen(html)) == 0)
1485 return NULL;
1487 /* only work with BUFFER_SIZE */
1488 if (BUFFER_SIZE < sz)
1489 sz = BUFFER_SIZE;
1491 for (i = 0; i < sz; i++) {
1492 c[0] = html[i];
1493 switch (c[0]) {
1494 case ('>'):
1495 strcat(buf, "&gt;");
1496 break;
1497 case ('&'):
1498 strcat(buf, "&amp;");
1499 break;
1500 case ('<'):
1501 strcat(buf, "&lt;");
1502 break;
1503 case ('"'):
1504 strcat(buf, "&quot;");
1505 break;
1506 case ('\''):
1507 strcat(buf, "&apos;");
1508 break;
1509 case ('\n'):
1510 strcat(buf, "<br />");
1511 default:
1512 strcat(buf, &c[0]);
1513 break;
1516 asprintf(&escaped_str, "%s", buf);
1517 free(buf);
1518 return escaped_str;
1521 int
1522 main()
1524 const struct got_error *error = NULL;
1525 struct trans *gw_trans;
1526 struct gw_dir *dir = NULL, *tdir;
1527 const char *page = "index";
1528 int gw_malloc = 1;
1530 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
1531 errx(1, "malloc");
1533 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
1534 errx(1, "malloc");
1536 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
1537 errx(1, "malloc");
1539 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
1540 errx(1, "malloc");
1542 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
1543 &page, 1, 0))
1544 errx(1, "khttp_parse");
1546 if ((gw_trans->gw_conf =
1547 malloc(sizeof(struct gotweb_conf))) == NULL) {
1548 gw_malloc = 0;
1549 error = got_error_from_errno("malloc");
1550 goto err;
1553 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
1554 error = got_error_from_errno("pledge");
1555 goto err;
1558 TAILQ_INIT(&gw_trans->gw_dirs);
1560 gw_trans->page = 0;
1561 gw_trans->repos_total = 0;
1562 gw_trans->repo_path = NULL;
1563 gw_trans->commit = NULL;
1564 gw_trans->mime = KMIME_TEXT_HTML;
1565 gw_trans->gw_tmpl->key = templs;
1566 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
1567 gw_trans->gw_tmpl->arg = gw_trans;
1568 gw_trans->gw_tmpl->cb = gw_template;
1569 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
1571 err:
1572 if (error) {
1573 gw_trans->mime = KMIME_TEXT_PLAIN;
1574 gw_trans->action = GW_ERR;
1575 gw_display_index(gw_trans, error);
1576 goto done;
1579 error = gw_parse_querystring(gw_trans);
1580 if (error)
1581 goto err;
1583 gw_display_index(gw_trans, error);
1585 done:
1586 if (gw_malloc) {
1587 free(gw_trans->gw_conf->got_repos_path);
1588 free(gw_trans->gw_conf->got_www_path);
1589 free(gw_trans->gw_conf->got_site_name);
1590 free(gw_trans->gw_conf->got_site_owner);
1591 free(gw_trans->gw_conf->got_site_link);
1592 free(gw_trans->gw_conf->got_logo);
1593 free(gw_trans->gw_conf->got_logo_url);
1594 free(gw_trans->gw_conf);
1595 free(gw_trans->commit);
1596 free(gw_trans->repo_path);
1597 free(gw_trans->repo_name);
1598 free(gw_trans->repo_file);
1599 free(gw_trans->action_name);
1601 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
1602 free(dir->name);
1603 free(dir->description);
1604 free(dir->age);
1605 free(dir->url);
1606 free(dir->path);
1607 free(dir);
1612 khttp_free(gw_trans->gw_req);
1613 return EXIT_SUCCESS;