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