2 8a35f56c 2022-07-16 thomas * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 2ac684a4 2022-09-03 thomas * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 8a35f56c 2022-07-16 thomas * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
9 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
10 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
12 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 8a35f56c 2022-07-16 thomas #include <net/if.h>
22 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
23 3e12c168 2022-07-16 thomas #include <sys/queue.h>
24 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
25 8a35f56c 2022-07-16 thomas #include <sys/types.h>
27 2ac684a4 2022-09-03 thomas #include <ctype.h>
28 8a35f56c 2022-07-16 thomas #include <dirent.h>
29 8a35f56c 2022-07-16 thomas #include <errno.h>
30 8a35f56c 2022-07-16 thomas #include <event.h>
31 4606e6d4 2022-11-23 thomas #include <fcntl.h>
32 4606e6d4 2022-11-23 thomas #include <imsg.h>
33 588a8092 2023-02-23 thomas #include <sha1.h>
34 588a8092 2023-02-23 thomas #include <sha2.h>
35 8a35f56c 2022-07-16 thomas #include <stdio.h>
36 8a35f56c 2022-07-16 thomas #include <stdlib.h>
37 8a35f56c 2022-07-16 thomas #include <string.h>
38 8a35f56c 2022-07-16 thomas #include <unistd.h>
40 8a35f56c 2022-07-16 thomas #include "got_error.h"
41 8a35f56c 2022-07-16 thomas #include "got_object.h"
42 8a35f56c 2022-07-16 thomas #include "got_reference.h"
43 8a35f56c 2022-07-16 thomas #include "got_repository.h"
44 8a35f56c 2022-07-16 thomas #include "got_path.h"
45 8a35f56c 2022-07-16 thomas #include "got_cancel.h"
46 8a35f56c 2022-07-16 thomas #include "got_worktree.h"
47 8a35f56c 2022-07-16 thomas #include "got_diff.h"
48 8a35f56c 2022-07-16 thomas #include "got_commit_graph.h"
49 8a35f56c 2022-07-16 thomas #include "got_blame.h"
50 8a35f56c 2022-07-16 thomas #include "got_privsep.h"
52 cb11302c 2022-12-30 thomas #include "got_compat.h"
54 8a35f56c 2022-07-16 thomas #include "proc.h"
55 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
56 d6795e9f 2022-12-30 thomas #include "tmpl.h"
58 8a35f56c 2022-07-16 thomas static const struct querystring_keys querystring_keys[] = {
59 8a35f56c 2022-07-16 thomas { "action", ACTION },
60 8a35f56c 2022-07-16 thomas { "commit", COMMIT },
61 8a35f56c 2022-07-16 thomas { "file", RFILE },
62 8a35f56c 2022-07-16 thomas { "folder", FOLDER },
63 8a35f56c 2022-07-16 thomas { "headref", HEADREF },
64 8a35f56c 2022-07-16 thomas { "index_page", INDEX_PAGE },
65 8a35f56c 2022-07-16 thomas { "path", PATH },
66 8a35f56c 2022-07-16 thomas { "page", PAGE },
69 8a35f56c 2022-07-16 thomas static const struct action_keys action_keys[] = {
70 8a35f56c 2022-07-16 thomas { "blame", BLAME },
71 8a35f56c 2022-07-16 thomas { "blob", BLOB },
72 b82440e1 2023-01-06 thomas { "blobraw", BLOBRAW },
73 8a35f56c 2022-07-16 thomas { "briefs", BRIEFS },
74 8a35f56c 2022-07-16 thomas { "commits", COMMITS },
75 8a35f56c 2022-07-16 thomas { "diff", DIFF },
76 8a35f56c 2022-07-16 thomas { "error", ERR },
77 8a35f56c 2022-07-16 thomas { "index", INDEX },
78 8a35f56c 2022-07-16 thomas { "summary", SUMMARY },
79 8a35f56c 2022-07-16 thomas { "tag", TAG },
80 8a35f56c 2022-07-16 thomas { "tags", TAGS },
81 8a35f56c 2022-07-16 thomas { "tree", TREE },
82 d6795e9f 2022-12-30 thomas { "rss", RSS },
85 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_querystring(struct querystring **);
86 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_parse_querystring(struct querystring **,
88 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_assign_querystring(struct querystring **,
89 8a35f56c 2022-07-16 thomas char *, char *);
90 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_index(struct request *);
91 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
92 8a35f56c 2022-07-16 thomas const char *);
93 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_load_got_path(struct request *c,
94 8a35f56c 2022-07-16 thomas struct repo_dir *);
95 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_repo_description(char **,
96 4606e6d4 2022-11-23 thomas struct server *, const char *, int);
97 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_clone_url(char **, struct server *,
98 4606e6d4 2022-11-23 thomas const char *, int);
100 8a35f56c 2022-07-16 thomas static void gotweb_free_querystring(struct querystring *);
101 8a35f56c 2022-07-16 thomas static void gotweb_free_repo_dir(struct repo_dir *);
103 3f0158b4 2022-08-30 thomas struct server *gotweb_get_server(uint8_t *, uint8_t *);
105 50237485 2023-01-23 thomas static int
106 50237485 2023-01-23 thomas gotweb_reply(struct request *c, int status, const char *ctype,
107 50237485 2023-01-23 thomas struct gotweb_url *location)
109 50237485 2023-01-23 thomas const char *csp;
111 50237485 2023-01-23 thomas if (status != 200 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
112 50237485 2023-01-23 thomas return -1;
114 50237485 2023-01-23 thomas if (location) {
115 50237485 2023-01-23 thomas if (fcgi_puts(c->tp, "Location: ") == -1 ||
116 50237485 2023-01-23 thomas gotweb_render_url(c, location) == -1 ||
117 50237485 2023-01-23 thomas fcgi_puts(c->tp, "\r\n") == -1)
118 50237485 2023-01-23 thomas return -1;
121 50237485 2023-01-23 thomas csp = "Content-Security-Policy: default-src 'self'; "
122 50237485 2023-01-23 thomas "script-src 'none'; object-src 'none';\r\n";
123 50237485 2023-01-23 thomas if (fcgi_puts(c->tp, csp) == -1)
124 50237485 2023-01-23 thomas return -1;
126 50237485 2023-01-23 thomas if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
127 50237485 2023-01-23 thomas return -1;
129 50237485 2023-01-23 thomas return fcgi_puts(c->tp, "\r\n");
132 50237485 2023-01-23 thomas static int
133 50237485 2023-01-23 thomas gotweb_reply_file(struct request *c, const char *ctype, const char *file,
134 50237485 2023-01-23 thomas const char *suffix)
138 50237485 2023-01-23 thomas r = fcgi_printf(c, "Content-Disposition: attachment; "
139 50237485 2023-01-23 thomas "filename=%s%s\r\n", file, suffix ? suffix : "");
140 50237485 2023-01-23 thomas if (r == -1)
141 50237485 2023-01-23 thomas return -1;
142 50237485 2023-01-23 thomas return gotweb_reply(c, 200, ctype, NULL);
146 8a35f56c 2022-07-16 thomas gotweb_process_request(struct request *c)
148 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL, *error2 = NULL;
149 b82440e1 2023-01-06 thomas struct got_blob_object *blob = NULL;
150 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
151 8a35f56c 2022-07-16 thomas struct querystring *qs = NULL;
152 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
153 18e466eb 2023-01-14 thomas struct got_reflist_head refs;
154 dccd05b4 2023-01-10 thomas FILE *fp = NULL;
155 8a35f56c 2022-07-16 thomas uint8_t err[] = "gotwebd experienced an error: ";
156 b82440e1 2023-01-06 thomas int r, html = 0, fd = -1;
158 18e466eb 2023-01-14 thomas TAILQ_INIT(&refs);
160 8a35f56c 2022-07-16 thomas /* init the transport */
161 8a35f56c 2022-07-16 thomas error = gotweb_init_transport(&c->t);
162 8a35f56c 2022-07-16 thomas if (error) {
163 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
166 8a35f56c 2022-07-16 thomas /* don't process any further if client disconnected */
167 8a35f56c 2022-07-16 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
169 8a35f56c 2022-07-16 thomas /* get the gotwebd server */
170 3f0158b4 2022-08-30 thomas srv = gotweb_get_server(c->server_name, c->http_host);
171 8a35f56c 2022-07-16 thomas if (srv == NULL) {
172 8a35f56c 2022-07-16 thomas log_warnx("%s: error server is NULL", __func__);
173 8a35f56c 2022-07-16 thomas goto err;
175 8a35f56c 2022-07-16 thomas c->srv = srv;
176 8a35f56c 2022-07-16 thomas /* parse our querystring */
177 8a35f56c 2022-07-16 thomas error = gotweb_init_querystring(&qs);
178 8a35f56c 2022-07-16 thomas if (error) {
179 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
180 8a35f56c 2022-07-16 thomas goto err;
182 8a35f56c 2022-07-16 thomas c->t->qs = qs;
183 8a35f56c 2022-07-16 thomas error = gotweb_parse_querystring(&qs, c->querystring);
184 8a35f56c 2022-07-16 thomas if (error) {
185 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
186 8a35f56c 2022-07-16 thomas goto err;
190 8a35f56c 2022-07-16 thomas * certain actions require a commit id in the querystring. this stops
191 8a35f56c 2022-07-16 thomas * bad actors from exploiting this by manually manipulating the
192 8a35f56c 2022-07-16 thomas * querystring.
195 b82440e1 2023-01-06 thomas if (qs->action == BLAME || qs->action == BLOB ||
196 b82440e1 2023-01-06 thomas qs->action == BLOBRAW || qs->action == DIFF) {
197 b82440e1 2023-01-06 thomas if (qs->commit == NULL) {
198 b82440e1 2023-01-06 thomas error2 = got_error(GOT_ERR_QUERYSTRING);
199 b82440e1 2023-01-06 thomas goto render;
203 8a35f56c 2022-07-16 thomas if (qs->action != INDEX) {
204 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, qs->path);
205 8a35f56c 2022-07-16 thomas if (error)
206 8a35f56c 2022-07-16 thomas goto done;
207 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
208 8a35f56c 2022-07-16 thomas c->t->repo_dir = repo_dir;
209 8a35f56c 2022-07-16 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
210 8a35f56c 2022-07-16 thomas goto err;
213 b82440e1 2023-01-06 thomas if (qs->action == BLOBRAW) {
214 ea1b5cf5 2023-01-15 thomas const uint8_t *buf;
215 ea1b5cf5 2023-01-15 thomas size_t len;
216 50237485 2023-01-23 thomas int binary, r;
218 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
219 8a35f56c 2022-07-16 thomas if (error)
220 8a35f56c 2022-07-16 thomas goto done;
222 ea1b5cf5 2023-01-15 thomas error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
223 ea1b5cf5 2023-01-15 thomas if (error2)
224 ea1b5cf5 2023-01-15 thomas goto render;
226 ea1b5cf5 2023-01-15 thomas if (binary)
227 50237485 2023-01-23 thomas r = gotweb_reply_file(c, "application/octet-stream",
228 50237485 2023-01-23 thomas qs->file, NULL);
230 50237485 2023-01-23 thomas r = gotweb_reply(c, 200, "text/plain", NULL);
231 50237485 2023-01-23 thomas if (r == -1)
232 ea1b5cf5 2023-01-15 thomas goto done;
234 ea1b5cf5 2023-01-15 thomas for (;;) {
235 ea1b5cf5 2023-01-15 thomas error = got_object_blob_read_block(&len, blob);
236 ea1b5cf5 2023-01-15 thomas if (error)
237 ea1b5cf5 2023-01-15 thomas goto done;
238 ea1b5cf5 2023-01-15 thomas if (len == 0)
240 ea1b5cf5 2023-01-15 thomas buf = got_object_blob_get_read_buf(blob);
241 ea1b5cf5 2023-01-15 thomas if (fcgi_gen_binary_response(c, buf, len) == -1)
242 ea1b5cf5 2023-01-15 thomas goto done;
245 d6795e9f 2022-12-30 thomas goto done;
248 b82440e1 2023-01-06 thomas if (qs->action == BLOB) {
249 b82440e1 2023-01-06 thomas int binary;
250 b82440e1 2023-01-06 thomas struct gotweb_url url = {
251 b82440e1 2023-01-06 thomas .index_page = -1,
252 b82440e1 2023-01-06 thomas .page = -1,
253 b82440e1 2023-01-06 thomas .action = BLOBRAW,
254 b82440e1 2023-01-06 thomas .path = qs->path,
255 b82440e1 2023-01-06 thomas .commit = qs->commit,
256 b82440e1 2023-01-06 thomas .folder = qs->folder,
257 b82440e1 2023-01-06 thomas .file = qs->file,
260 b82440e1 2023-01-06 thomas error = got_get_repo_commits(c, 1);
261 b82440e1 2023-01-06 thomas if (error)
262 b82440e1 2023-01-06 thomas goto done;
264 b82440e1 2023-01-06 thomas error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
265 b82440e1 2023-01-06 thomas if (error2)
266 b82440e1 2023-01-06 thomas goto render;
267 b82440e1 2023-01-06 thomas if (binary) {
268 50237485 2023-01-23 thomas gotweb_reply(c, 302, NULL, &url);
269 b82440e1 2023-01-06 thomas goto done;
273 d6795e9f 2022-12-30 thomas if (qs->action == RSS) {
274 50237485 2023-01-23 thomas const char *ctype = "application/rss+xml;charset=utf-8";
276 50237485 2023-01-23 thomas if (gotweb_reply_file(c, ctype, repo_dir->name, ".rss") == -1)
277 50237485 2023-01-23 thomas goto done;
279 d6795e9f 2022-12-30 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
280 8a35f56c 2022-07-16 thomas if (error) {
281 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
282 8a35f56c 2022-07-16 thomas goto err;
284 d6795e9f 2022-12-30 thomas if (gotweb_render_rss(c->tp) == -1)
285 d6795e9f 2022-12-30 thomas goto err;
286 8a35f56c 2022-07-16 thomas goto done;
290 50237485 2023-01-23 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
291 50237485 2023-01-23 thomas goto done;
292 43a44bce 2022-12-04 thomas html = 1;
294 e7e5fa49 2022-12-30 thomas if (gotweb_render_header(c->tp) == -1)
295 8a35f56c 2022-07-16 thomas goto err;
297 8a35f56c 2022-07-16 thomas if (error2) {
298 8a35f56c 2022-07-16 thomas error = error2;
299 8a35f56c 2022-07-16 thomas goto err;
302 8a35f56c 2022-07-16 thomas switch(qs->action) {
303 8a35f56c 2022-07-16 thomas case BLAME:
304 1cd5d437 2023-01-15 thomas error = got_get_repo_commits(c, 1);
305 8a35f56c 2022-07-16 thomas if (error) {
306 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
307 8a35f56c 2022-07-16 thomas goto err;
309 1cd5d437 2023-01-15 thomas if (gotweb_render_blame(c->tp) == -1)
310 1cd5d437 2023-01-15 thomas goto done;
312 b82440e1 2023-01-06 thomas case BLOB:
313 b82440e1 2023-01-06 thomas if (gotweb_render_blob(c->tp, blob) == -1)
314 b82440e1 2023-01-06 thomas goto err;
316 8a35f56c 2022-07-16 thomas case BRIEFS:
317 e7e5fa49 2022-12-30 thomas if (gotweb_render_briefs(c->tp) == -1)
318 8a35f56c 2022-07-16 thomas goto err;
320 8a35f56c 2022-07-16 thomas case COMMITS:
321 7ade8b27 2022-12-30 thomas error = got_get_repo_commits(c, srv->max_commits_display);
322 8a35f56c 2022-07-16 thomas if (error) {
323 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
324 8a35f56c 2022-07-16 thomas goto err;
326 7ade8b27 2022-12-30 thomas if (gotweb_render_commits(c->tp) == -1)
327 7ade8b27 2022-12-30 thomas goto err;
329 8a35f56c 2022-07-16 thomas case DIFF:
330 dccd05b4 2023-01-10 thomas error = got_get_repo_commits(c, 1);
331 5d860bce 2023-01-07 thomas if (error) {
332 5d860bce 2023-01-07 thomas log_warnx("%s: %s", __func__, error->msg);
333 5d860bce 2023-01-07 thomas goto err;
335 dccd05b4 2023-01-10 thomas error = got_open_diff_for_output(&fp, &fd, c);
336 dccd05b4 2023-01-10 thomas if (error) {
337 dccd05b4 2023-01-10 thomas log_warnx("%s: %s", __func__, error->msg);
338 dccd05b4 2023-01-10 thomas goto err;
340 dccd05b4 2023-01-10 thomas if (gotweb_render_diff(c->tp, fp) == -1)
341 dccd05b4 2023-01-10 thomas goto err;
343 8a35f56c 2022-07-16 thomas case INDEX:
344 8a35f56c 2022-07-16 thomas error = gotweb_render_index(c);
345 8a35f56c 2022-07-16 thomas if (error) {
346 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
347 8a35f56c 2022-07-16 thomas goto err;
350 8a35f56c 2022-07-16 thomas case SUMMARY:
351 18e466eb 2023-01-14 thomas error = got_ref_list(&refs, c->t->repo, "refs/heads",
352 18e466eb 2023-01-14 thomas got_ref_cmp_by_name, NULL);
353 8a35f56c 2022-07-16 thomas if (error) {
354 18e466eb 2023-01-14 thomas log_warnx("%s: got_ref_list: %s", __func__,
355 18e466eb 2023-01-14 thomas error->msg);
356 8a35f56c 2022-07-16 thomas goto err;
358 18e466eb 2023-01-14 thomas qs->action = TAGS;
359 18e466eb 2023-01-14 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
360 18e466eb 2023-01-14 thomas if (error) {
361 18e466eb 2023-01-14 thomas log_warnx("%s: got_get_repo_tags: %s", __func__,
362 18e466eb 2023-01-14 thomas error->msg);
363 18e466eb 2023-01-14 thomas goto err;
365 18e466eb 2023-01-14 thomas qs->action = SUMMARY;
366 18e466eb 2023-01-14 thomas if (gotweb_render_summary(c->tp, &refs) == -1)
367 18e466eb 2023-01-14 thomas goto done;
369 8a35f56c 2022-07-16 thomas case TAG:
370 145ca42a 2023-01-09 thomas error = got_get_repo_tags(c, 1);
371 8a35f56c 2022-07-16 thomas if (error) {
372 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
373 145ca42a 2023-01-09 thomas goto err;
375 145ca42a 2023-01-09 thomas if (c->t->tag_count == 0) {
376 145ca42a 2023-01-09 thomas error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
377 145ca42a 2023-01-09 thomas "bad commit id");
378 8a35f56c 2022-07-16 thomas goto err;
380 145ca42a 2023-01-09 thomas if (gotweb_render_tag(c->tp) == -1)
381 145ca42a 2023-01-09 thomas goto done;
383 8a35f56c 2022-07-16 thomas case TAGS:
384 b3ba36c3 2023-01-14 thomas error = got_get_repo_tags(c, srv->max_commits_display);
385 8a35f56c 2022-07-16 thomas if (error) {
386 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
387 8a35f56c 2022-07-16 thomas goto err;
389 b3ba36c3 2023-01-14 thomas if (gotweb_render_tags(c->tp) == -1)
390 b3ba36c3 2023-01-14 thomas goto done;
392 8a35f56c 2022-07-16 thomas case TREE:
393 3c14c1f2 2023-01-06 thomas error = got_get_repo_commits(c, 1);
394 8a35f56c 2022-07-16 thomas if (error) {
395 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
396 8a35f56c 2022-07-16 thomas goto err;
398 3c14c1f2 2023-01-06 thomas if (gotweb_render_tree(c->tp) == -1)
399 3c14c1f2 2023-01-06 thomas goto err;
401 8a35f56c 2022-07-16 thomas case ERR:
403 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
404 79393471 2022-08-27 thomas "Erorr: Bad Querystring");
405 79393471 2022-08-27 thomas if (r == -1)
406 8a35f56c 2022-07-16 thomas goto err;
410 8a35f56c 2022-07-16 thomas goto done;
412 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
414 a5f25a12 2022-10-31 thomas if (fcgi_printf(c, "\n%s", err) == -1)
416 8a35f56c 2022-07-16 thomas if (error) {
417 79393471 2022-08-27 thomas if (fcgi_printf(c, "%s", error->msg) == -1)
420 79393471 2022-08-27 thomas if (fcgi_printf(c, "see daemon logs for details") == -1)
423 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "</div>\n") == -1)
426 b82440e1 2023-01-06 thomas if (blob)
427 b82440e1 2023-01-06 thomas got_object_blob_close(blob);
428 dccd05b4 2023-01-10 thomas if (fp) {
429 dccd05b4 2023-01-10 thomas error = got_gotweb_flushfile(fp, fd);
430 dccd05b4 2023-01-10 thomas if (error)
431 dccd05b4 2023-01-10 thomas log_warnx("%s: got_gotweb_flushfile failure: %s",
432 dccd05b4 2023-01-10 thomas __func__, error->msg);
435 b82440e1 2023-01-06 thomas if (fd != -1)
436 b82440e1 2023-01-06 thomas close(fd);
437 8a35f56c 2022-07-16 thomas if (html && srv != NULL)
438 e7e5fa49 2022-12-30 thomas gotweb_render_footer(c->tp);
440 18e466eb 2023-01-14 thomas got_ref_list_free(&refs);
443 8a35f56c 2022-07-16 thomas struct server *
444 3f0158b4 2022-08-30 thomas gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
446 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
448 3f0158b4 2022-08-30 thomas /* check against the server name first */
449 8a35f56c 2022-07-16 thomas if (strlen(server_name) > 0)
450 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
451 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, server_name) == 0)
452 8a35f56c 2022-07-16 thomas goto done;
454 3f0158b4 2022-08-30 thomas /* check against subdomain second */
455 8a35f56c 2022-07-16 thomas if (strlen(subdomain) > 0)
456 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
457 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, subdomain) == 0)
458 8a35f56c 2022-07-16 thomas goto done;
460 8a35f56c 2022-07-16 thomas /* if those fail, send first server */
461 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
462 8a35f56c 2022-07-16 thomas if (srv != NULL)
465 8a35f56c 2022-07-16 thomas return srv;
468 8a35f56c 2022-07-16 thomas const struct got_error *
469 8a35f56c 2022-07-16 thomas gotweb_init_transport(struct transport **t)
471 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
473 8a35f56c 2022-07-16 thomas *t = calloc(1, sizeof(**t));
474 8a35f56c 2022-07-16 thomas if (*t == NULL)
475 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
477 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_commits);
478 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_tags);
480 8a35f56c 2022-07-16 thomas (*t)->repo = NULL;
481 8a35f56c 2022-07-16 thomas (*t)->repo_dir = NULL;
482 8a35f56c 2022-07-16 thomas (*t)->qs = NULL;
483 8a35f56c 2022-07-16 thomas (*t)->next_id = NULL;
484 8a35f56c 2022-07-16 thomas (*t)->prev_id = NULL;
485 8a35f56c 2022-07-16 thomas (*t)->next_disp = 0;
486 8a35f56c 2022-07-16 thomas (*t)->prev_disp = 0;
488 8a35f56c 2022-07-16 thomas return error;
491 8a35f56c 2022-07-16 thomas static const struct got_error *
492 8a35f56c 2022-07-16 thomas gotweb_init_querystring(struct querystring **qs)
494 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
496 8a35f56c 2022-07-16 thomas *qs = calloc(1, sizeof(**qs));
497 8a35f56c 2022-07-16 thomas if (*qs == NULL)
498 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
500 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup("HEAD");
501 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
502 84630254 2022-09-02 thomas free(*qs);
503 84630254 2022-09-02 thomas *qs = NULL;
504 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
507 84630254 2022-09-02 thomas (*qs)->action = INDEX;
508 84630254 2022-09-02 thomas (*qs)->commit = NULL;
509 84630254 2022-09-02 thomas (*qs)->file = NULL;
510 84630254 2022-09-02 thomas (*qs)->folder = NULL;
511 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
512 8a35f56c 2022-07-16 thomas (*qs)->path = NULL;
514 8a35f56c 2022-07-16 thomas return error;
517 8a35f56c 2022-07-16 thomas static const struct got_error *
518 8a35f56c 2022-07-16 thomas gotweb_parse_querystring(struct querystring **qs, char *qst)
520 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
521 8a35f56c 2022-07-16 thomas char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
522 8a35f56c 2022-07-16 thomas char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
524 8a35f56c 2022-07-16 thomas if (qst == NULL)
525 8a35f56c 2022-07-16 thomas return error;
527 8a35f56c 2022-07-16 thomas tok1 = strdup(qst);
528 8a35f56c 2022-07-16 thomas if (tok1 == NULL)
529 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
531 8a35f56c 2022-07-16 thomas tok1_pair = tok1;
532 8a35f56c 2022-07-16 thomas tok1_end = tok1;
534 8a35f56c 2022-07-16 thomas while (tok1_pair != NULL) {
535 8a35f56c 2022-07-16 thomas strsep(&tok1_end, "&");
537 8a35f56c 2022-07-16 thomas tok2 = strdup(tok1_pair);
538 8a35f56c 2022-07-16 thomas if (tok2 == NULL) {
539 8a35f56c 2022-07-16 thomas free(tok1);
540 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
543 8a35f56c 2022-07-16 thomas tok2_pair = tok2;
544 8a35f56c 2022-07-16 thomas tok2_end = tok2;
546 8a35f56c 2022-07-16 thomas while (tok2_pair != NULL) {
547 8a35f56c 2022-07-16 thomas strsep(&tok2_end, "=");
548 8a35f56c 2022-07-16 thomas if (tok2_end) {
549 8a35f56c 2022-07-16 thomas error = gotweb_assign_querystring(qs, tok2_pair,
550 8a35f56c 2022-07-16 thomas tok2_end);
551 8a35f56c 2022-07-16 thomas if (error)
552 8a35f56c 2022-07-16 thomas goto err;
554 8a35f56c 2022-07-16 thomas tok2_pair = tok2_end;
556 8a35f56c 2022-07-16 thomas free(tok2);
557 8a35f56c 2022-07-16 thomas tok1_pair = tok1_end;
559 8a35f56c 2022-07-16 thomas free(tok1);
560 8a35f56c 2022-07-16 thomas return error;
562 8a35f56c 2022-07-16 thomas free(tok2);
563 8a35f56c 2022-07-16 thomas free(tok1);
564 8a35f56c 2022-07-16 thomas return error;
568 2ac684a4 2022-09-03 thomas * Adapted from usr.sbin/httpd/httpd.c url_decode.
570 8a35f56c 2022-07-16 thomas static const struct got_error *
571 2ac684a4 2022-09-03 thomas gotweb_urldecode(char *url)
573 2ac684a4 2022-09-03 thomas char *p, *q;
574 2ac684a4 2022-09-03 thomas char hex[3];
575 2ac684a4 2022-09-03 thomas unsigned long x;
577 2ac684a4 2022-09-03 thomas hex[2] = '\0';
578 2ac684a4 2022-09-03 thomas p = q = url;
580 2ac684a4 2022-09-03 thomas while (*p != '\0') {
581 2ac684a4 2022-09-03 thomas switch (*p) {
582 2ac684a4 2022-09-03 thomas case '%':
583 2ac684a4 2022-09-03 thomas /* Encoding character is followed by two hex chars */
584 2ac684a4 2022-09-03 thomas if (!isxdigit((unsigned char)p[1]) ||
585 2ac684a4 2022-09-03 thomas !isxdigit((unsigned char)p[2]) ||
586 2ac684a4 2022-09-03 thomas (p[1] == '0' && p[2] == '0'))
587 2ac684a4 2022-09-03 thomas return got_error(GOT_ERR_BAD_QUERYSTRING);
589 2ac684a4 2022-09-03 thomas hex[0] = p[1];
590 2ac684a4 2022-09-03 thomas hex[1] = p[2];
593 2ac684a4 2022-09-03 thomas * We don't have to validate "hex" because it is
594 2ac684a4 2022-09-03 thomas * guaranteed to include two hex chars followed by nul.
596 2ac684a4 2022-09-03 thomas x = strtoul(hex, NULL, 16);
597 2ac684a4 2022-09-03 thomas *q = (char)x;
607 2ac684a4 2022-09-03 thomas *q = '\0';
609 2ac684a4 2022-09-03 thomas return NULL;
612 2ac684a4 2022-09-03 thomas static const struct got_error *
613 8a35f56c 2022-07-16 thomas gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
615 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
616 8a35f56c 2022-07-16 thomas const char *errstr;
617 8a35f56c 2022-07-16 thomas int a_cnt, el_cnt;
619 2ac684a4 2022-09-03 thomas error = gotweb_urldecode(value);
620 2ac684a4 2022-09-03 thomas if (error)
621 2ac684a4 2022-09-03 thomas return error;
623 8a35f56c 2022-07-16 thomas for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
624 8a35f56c 2022-07-16 thomas if (strcmp(key, querystring_keys[el_cnt].name) != 0)
625 8a35f56c 2022-07-16 thomas continue;
627 8a35f56c 2022-07-16 thomas switch (querystring_keys[el_cnt].element) {
628 8a35f56c 2022-07-16 thomas case ACTION:
629 8a35f56c 2022-07-16 thomas for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
630 8a35f56c 2022-07-16 thomas if (strcmp(value, action_keys[a_cnt].name) != 0)
631 8a35f56c 2022-07-16 thomas continue;
632 8a35f56c 2022-07-16 thomas else if (strcmp(value,
633 8a35f56c 2022-07-16 thomas action_keys[a_cnt].name) == 0){
634 8a35f56c 2022-07-16 thomas (*qs)->action =
635 8a35f56c 2022-07-16 thomas action_keys[a_cnt].action;
636 8a35f56c 2022-07-16 thomas goto qa_found;
639 8a35f56c 2022-07-16 thomas (*qs)->action = ERR;
640 8a35f56c 2022-07-16 thomas qa_found:
642 8a35f56c 2022-07-16 thomas case COMMIT:
643 8a35f56c 2022-07-16 thomas (*qs)->commit = strdup(value);
644 8a35f56c 2022-07-16 thomas if ((*qs)->commit == NULL) {
645 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
646 8a35f56c 2022-07-16 thomas __func__);
647 8a35f56c 2022-07-16 thomas goto done;
650 8a35f56c 2022-07-16 thomas case RFILE:
651 8a35f56c 2022-07-16 thomas (*qs)->file = strdup(value);
652 8a35f56c 2022-07-16 thomas if ((*qs)->file == NULL) {
653 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
654 8a35f56c 2022-07-16 thomas __func__);
655 8a35f56c 2022-07-16 thomas goto done;
658 8a35f56c 2022-07-16 thomas case FOLDER:
659 8a35f56c 2022-07-16 thomas (*qs)->folder = strdup(value);
660 8a35f56c 2022-07-16 thomas if ((*qs)->folder == NULL) {
661 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
662 8a35f56c 2022-07-16 thomas __func__);
663 8a35f56c 2022-07-16 thomas goto done;
666 8a35f56c 2022-07-16 thomas case HEADREF:
667 b0764984 2022-09-02 thomas free((*qs)->headref);
668 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup(value);
669 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
670 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
671 8a35f56c 2022-07-16 thomas __func__);
672 8a35f56c 2022-07-16 thomas goto done;
675 8a35f56c 2022-07-16 thomas case INDEX_PAGE:
676 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
678 8a35f56c 2022-07-16 thomas (*qs)->index_page = strtonum(value, INT64_MIN,
679 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
680 8a35f56c 2022-07-16 thomas if (errstr) {
681 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
682 8a35f56c 2022-07-16 thomas __func__, errstr);
683 8a35f56c 2022-07-16 thomas goto done;
685 3d6d1fb0 2022-12-30 thomas if ((*qs)->index_page < 0)
686 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
688 8a35f56c 2022-07-16 thomas case PATH:
689 8a35f56c 2022-07-16 thomas (*qs)->path = strdup(value);
690 8a35f56c 2022-07-16 thomas if ((*qs)->path == NULL) {
691 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
692 8a35f56c 2022-07-16 thomas __func__);
693 8a35f56c 2022-07-16 thomas goto done;
696 8a35f56c 2022-07-16 thomas case PAGE:
697 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
699 8a35f56c 2022-07-16 thomas (*qs)->page = strtonum(value, INT64_MIN,
700 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
701 8a35f56c 2022-07-16 thomas if (errstr) {
702 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
703 8a35f56c 2022-07-16 thomas __func__, errstr);
704 8a35f56c 2022-07-16 thomas goto done;
706 3d6d1fb0 2022-12-30 thomas if ((*qs)->page < 0)
707 8a35f56c 2022-07-16 thomas (*qs)->page = 0;
714 8a35f56c 2022-07-16 thomas return error;
718 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(struct repo_tag *rt)
720 8a35f56c 2022-07-16 thomas if (rt != NULL) {
721 8a35f56c 2022-07-16 thomas free(rt->commit_id);
722 217813df 2022-09-02 thomas free(rt->tag_name);
723 217813df 2022-09-02 thomas free(rt->tag_commit);
724 217813df 2022-09-02 thomas free(rt->commit_msg);
725 8a35f56c 2022-07-16 thomas free(rt->tagger);
727 8a35f56c 2022-07-16 thomas free(rt);
731 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(struct repo_commit *rc)
733 8a35f56c 2022-07-16 thomas if (rc != NULL) {
734 8a35f56c 2022-07-16 thomas free(rc->path);
735 8a35f56c 2022-07-16 thomas free(rc->refs_str);
736 8a35f56c 2022-07-16 thomas free(rc->commit_id);
737 8a35f56c 2022-07-16 thomas free(rc->parent_id);
738 8a35f56c 2022-07-16 thomas free(rc->tree_id);
739 8a35f56c 2022-07-16 thomas free(rc->author);
740 8a35f56c 2022-07-16 thomas free(rc->committer);
741 8a35f56c 2022-07-16 thomas free(rc->commit_msg);
743 8a35f56c 2022-07-16 thomas free(rc);
746 8a35f56c 2022-07-16 thomas static void
747 8a35f56c 2022-07-16 thomas gotweb_free_querystring(struct querystring *qs)
749 8a35f56c 2022-07-16 thomas if (qs != NULL) {
750 8a35f56c 2022-07-16 thomas free(qs->commit);
751 8a35f56c 2022-07-16 thomas free(qs->file);
752 8a35f56c 2022-07-16 thomas free(qs->folder);
753 8a35f56c 2022-07-16 thomas free(qs->headref);
754 8a35f56c 2022-07-16 thomas free(qs->path);
756 8a35f56c 2022-07-16 thomas free(qs);
759 8a35f56c 2022-07-16 thomas static void
760 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(struct repo_dir *repo_dir)
762 8a35f56c 2022-07-16 thomas if (repo_dir != NULL) {
763 8a35f56c 2022-07-16 thomas free(repo_dir->name);
764 8a35f56c 2022-07-16 thomas free(repo_dir->owner);
765 8a35f56c 2022-07-16 thomas free(repo_dir->description);
766 8a35f56c 2022-07-16 thomas free(repo_dir->url);
767 8a35f56c 2022-07-16 thomas free(repo_dir->path);
769 8a35f56c 2022-07-16 thomas free(repo_dir);
773 8a35f56c 2022-07-16 thomas gotweb_free_transport(struct transport *t)
775 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL, *trc = NULL;
776 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
778 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
779 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_commits, rc, entry);
780 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(rc);
782 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
783 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
784 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
786 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(t->repo_dir);
787 8a35f56c 2022-07-16 thomas gotweb_free_querystring(t->qs);
788 20bab626 2023-02-03 thomas free(t->more_id);
789 6b42af1e 2022-09-02 thomas free(t->next_id);
790 6b42af1e 2022-09-02 thomas free(t->prev_id);
795 2f4f0731 2022-12-30 thomas gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
796 2f4f0731 2022-12-30 thomas struct gotweb_url *next, int *have_next)
798 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
799 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
800 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
802 2f4f0731 2022-12-30 thomas *have_prev = *have_next = 0;
804 8a35f56c 2022-07-16 thomas switch(qs->action) {
805 8a35f56c 2022-07-16 thomas case INDEX:
806 8a35f56c 2022-07-16 thomas if (qs->index_page > 0) {
807 2f4f0731 2022-12-30 thomas *have_prev = 1;
808 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
809 55144267 2022-09-07 thomas .action = -1,
810 55144267 2022-09-07 thomas .index_page = qs->index_page - 1,
811 55144267 2022-09-07 thomas .page = -1,
814 2f4f0731 2022-12-30 thomas if (t->next_disp == srv->max_repos_display &&
815 2f4f0731 2022-12-30 thomas t->repos_total != (qs->index_page + 1) *
816 2f4f0731 2022-12-30 thomas srv->max_repos_display) {
817 2f4f0731 2022-12-30 thomas *have_next = 1;
818 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
819 2f4f0731 2022-12-30 thomas .action = -1,
820 2f4f0731 2022-12-30 thomas .index_page = qs->index_page + 1,
821 2f4f0731 2022-12-30 thomas .page = -1,
825 8a35f56c 2022-07-16 thomas case TAGS:
826 2f4f0731 2022-12-30 thomas if (t->prev_id && qs->commit != NULL &&
827 2f4f0731 2022-12-30 thomas strcmp(qs->commit, t->prev_id) != 0) {
828 2f4f0731 2022-12-30 thomas *have_prev = 1;
829 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
830 2f4f0731 2022-12-30 thomas .action = TAGS,
831 2f4f0731 2022-12-30 thomas .index_page = -1,
832 2f4f0731 2022-12-30 thomas .page = qs->page - 1,
833 2f4f0731 2022-12-30 thomas .path = qs->path,
834 2f4f0731 2022-12-30 thomas .commit = t->prev_id,
835 2f4f0731 2022-12-30 thomas .headref = qs->headref,
838 8a35f56c 2022-07-16 thomas if (t->next_id) {
839 2f4f0731 2022-12-30 thomas *have_next = 1;
840 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
841 55144267 2022-09-07 thomas .action = TAGS,
842 55144267 2022-09-07 thomas .index_page = -1,
843 55144267 2022-09-07 thomas .page = qs->page + 1,
844 55144267 2022-09-07 thomas .path = qs->path,
845 55144267 2022-09-07 thomas .commit = t->next_id,
846 55144267 2022-09-07 thomas .headref = qs->headref,
853 8a35f56c 2022-07-16 thomas static const struct got_error *
854 8a35f56c 2022-07-16 thomas gotweb_render_index(struct request *c)
856 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
857 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
858 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
859 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
860 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
861 f530ab0e 2022-09-02 thomas struct dirent **sd_dent = NULL;
862 8a35f56c 2022-07-16 thomas unsigned int d_cnt, d_i, d_disp = 0;
863 24240f6a 2022-11-23 thomas unsigned int d_skipped = 0;
864 e7e5fa49 2022-12-30 thomas int type;
866 8a35f56c 2022-07-16 thomas d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
867 8a35f56c 2022-07-16 thomas if (d_cnt == -1) {
868 f530ab0e 2022-09-02 thomas sd_dent = NULL;
869 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("scandir", srv->repos_path);
870 8a35f56c 2022-07-16 thomas goto done;
873 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_table_hdr(c->tp) == -1)
874 8a35f56c 2022-07-16 thomas goto done;
876 8a35f56c 2022-07-16 thomas for (d_i = 0; d_i < d_cnt; d_i++) {
877 57e88d7c 2022-11-23 thomas if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
880 8a35f56c 2022-07-16 thomas if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
881 24240f6a 2022-11-23 thomas strcmp(sd_dent[d_i]->d_name, "..") == 0) {
882 24240f6a 2022-11-23 thomas d_skipped++;
883 24240f6a 2022-11-23 thomas continue;
886 24240f6a 2022-11-23 thomas error = got_path_dirent_type(&type, srv->repos_path,
887 24240f6a 2022-11-23 thomas sd_dent[d_i]);
888 24240f6a 2022-11-23 thomas if (error)
889 24240f6a 2022-11-23 thomas goto done;
890 24240f6a 2022-11-23 thomas if (type != DT_DIR) {
891 24240f6a 2022-11-23 thomas d_skipped++;
892 8a35f56c 2022-07-16 thomas continue;
895 8a35f56c 2022-07-16 thomas if (qs->index_page > 0 && (qs->index_page *
896 8a35f56c 2022-07-16 thomas srv->max_repos_display) > t->prev_disp) {
897 8a35f56c 2022-07-16 thomas t->prev_disp++;
898 8a35f56c 2022-07-16 thomas continue;
901 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
902 8a35f56c 2022-07-16 thomas if (error)
903 8a35f56c 2022-07-16 thomas goto done;
905 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
906 8a35f56c 2022-07-16 thomas if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
907 8a35f56c 2022-07-16 thomas error = NULL;
908 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
909 8a35f56c 2022-07-16 thomas repo_dir = NULL;
910 24240f6a 2022-11-23 thomas d_skipped++;
911 8a35f56c 2022-07-16 thomas continue;
913 24240f6a 2022-11-23 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
914 24240f6a 2022-11-23 thomas goto done;
916 8a35f56c 2022-07-16 thomas d_disp++;
917 8a35f56c 2022-07-16 thomas t->prev_disp++;
919 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
920 55144267 2022-09-07 thomas goto done;
922 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
923 8a35f56c 2022-07-16 thomas repo_dir = NULL;
924 8a35f56c 2022-07-16 thomas t->next_disp++;
925 8a35f56c 2022-07-16 thomas if (d_disp == srv->max_repos_display)
928 24240f6a 2022-11-23 thomas t->repos_total = d_cnt - d_skipped;
930 8a35f56c 2022-07-16 thomas if (srv->max_repos_display == 0)
931 79393471 2022-08-27 thomas goto done;
932 8a35f56c 2022-07-16 thomas if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
933 79393471 2022-08-27 thomas goto done;
934 8a35f56c 2022-07-16 thomas if (t->repos_total <= srv->max_repos ||
935 8a35f56c 2022-07-16 thomas t->repos_total <= srv->max_repos_display)
936 79393471 2022-08-27 thomas goto done;
938 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
939 8a35f56c 2022-07-16 thomas goto done;
941 f530ab0e 2022-09-02 thomas if (sd_dent) {
942 f530ab0e 2022-09-02 thomas for (d_i = 0; d_i < d_cnt; d_i++)
943 f530ab0e 2022-09-02 thomas free(sd_dent[d_i]);
944 f530ab0e 2022-09-02 thomas free(sd_dent);
946 8a35f56c 2022-07-16 thomas return error;
949 55144267 2022-09-07 thomas static inline int
950 55144267 2022-09-07 thomas should_urlencode(int c)
952 55144267 2022-09-07 thomas if (c <= ' ' || c >= 127)
953 55144267 2022-09-07 thomas return 1;
955 55144267 2022-09-07 thomas switch (c) {
956 55144267 2022-09-07 thomas /* gen-delim */
957 55144267 2022-09-07 thomas case ':':
958 55144267 2022-09-07 thomas case '/':
959 55144267 2022-09-07 thomas case '?':
960 55144267 2022-09-07 thomas case '#':
961 55144267 2022-09-07 thomas case '[':
962 55144267 2022-09-07 thomas case ']':
963 55144267 2022-09-07 thomas case '@':
964 55144267 2022-09-07 thomas /* sub-delims */
965 55144267 2022-09-07 thomas case '!':
966 55144267 2022-09-07 thomas case '$':
967 55144267 2022-09-07 thomas case '&':
968 55144267 2022-09-07 thomas case '\'':
969 55144267 2022-09-07 thomas case '(':
970 55144267 2022-09-07 thomas case ')':
971 55144267 2022-09-07 thomas case '*':
972 55144267 2022-09-07 thomas case '+':
973 55144267 2022-09-07 thomas case ',':
974 55144267 2022-09-07 thomas case ';':
975 55144267 2022-09-07 thomas case '=':
976 e1a9403a 2023-01-06 thomas /* needed because the URLs are embedded into the HTML */
977 e1a9403a 2023-01-06 thomas case '\"':
978 55144267 2022-09-07 thomas return 1;
980 55144267 2022-09-07 thomas return 0;
984 55144267 2022-09-07 thomas static char *
985 55144267 2022-09-07 thomas gotweb_urlencode(const char *str)
987 55144267 2022-09-07 thomas const char *s;
988 55144267 2022-09-07 thomas char *escaped;
989 55144267 2022-09-07 thomas size_t i, len;
990 55144267 2022-09-07 thomas int a, b;
993 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
995 55144267 2022-09-07 thomas if (should_urlencode(*s))
996 55144267 2022-09-07 thomas len += 2;
999 55144267 2022-09-07 thomas escaped = calloc(1, len + 1);
1000 55144267 2022-09-07 thomas if (escaped == NULL)
1001 55144267 2022-09-07 thomas return NULL;
1004 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
1005 55144267 2022-09-07 thomas if (should_urlencode(*s)) {
1006 55144267 2022-09-07 thomas a = (*s & 0xF0) >> 4;
1007 55144267 2022-09-07 thomas b = (*s & 0x0F);
1009 55144267 2022-09-07 thomas escaped[i++] = '%';
1010 55144267 2022-09-07 thomas escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1011 55144267 2022-09-07 thomas escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1013 55144267 2022-09-07 thomas escaped[i++] = *s;
1016 55144267 2022-09-07 thomas return escaped;
1019 e7e5fa49 2022-12-30 thomas const char *
1020 e7e5fa49 2022-12-30 thomas gotweb_action_name(int action)
1022 55144267 2022-09-07 thomas switch (action) {
1023 55144267 2022-09-07 thomas case BLAME:
1024 55144267 2022-09-07 thomas return "blame";
1025 55144267 2022-09-07 thomas case BLOB:
1026 55144267 2022-09-07 thomas return "blob";
1027 b82440e1 2023-01-06 thomas case BLOBRAW:
1028 b82440e1 2023-01-06 thomas return "blobraw";
1029 55144267 2022-09-07 thomas case BRIEFS:
1030 55144267 2022-09-07 thomas return "briefs";
1031 55144267 2022-09-07 thomas case COMMITS:
1032 55144267 2022-09-07 thomas return "commits";
1033 55144267 2022-09-07 thomas case DIFF:
1034 55144267 2022-09-07 thomas return "diff";
1035 55144267 2022-09-07 thomas case ERR:
1036 55144267 2022-09-07 thomas return "err";
1037 55144267 2022-09-07 thomas case INDEX:
1038 55144267 2022-09-07 thomas return "index";
1039 55144267 2022-09-07 thomas case SUMMARY:
1040 55144267 2022-09-07 thomas return "summary";
1041 55144267 2022-09-07 thomas case TAG:
1042 55144267 2022-09-07 thomas return "tag";
1043 55144267 2022-09-07 thomas case TAGS:
1044 55144267 2022-09-07 thomas return "tags";
1045 55144267 2022-09-07 thomas case TREE:
1046 55144267 2022-09-07 thomas return "tree";
1047 d6795e9f 2022-12-30 thomas case RSS:
1048 d6795e9f 2022-12-30 thomas return "rss";
1049 55144267 2022-09-07 thomas default:
1050 55144267 2022-09-07 thomas return NULL;
1055 e7e5fa49 2022-12-30 thomas gotweb_render_url(struct request *c, struct gotweb_url *url)
1057 55144267 2022-09-07 thomas const char *sep = "?", *action;
1058 55144267 2022-09-07 thomas char *tmp;
1061 e7e5fa49 2022-12-30 thomas action = gotweb_action_name(url->action);
1062 55144267 2022-09-07 thomas if (action != NULL) {
1063 55144267 2022-09-07 thomas if (fcgi_printf(c, "?action=%s", action) == -1)
1064 55144267 2022-09-07 thomas return -1;
1065 55144267 2022-09-07 thomas sep = "&";
1068 55144267 2022-09-07 thomas if (url->commit) {
1069 55144267 2022-09-07 thomas if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1070 55144267 2022-09-07 thomas return -1;
1071 55144267 2022-09-07 thomas sep = "&";
1074 55144267 2022-09-07 thomas if (url->previd) {
1075 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1076 55144267 2022-09-07 thomas return -1;
1077 55144267 2022-09-07 thomas sep = "&";
1080 55144267 2022-09-07 thomas if (url->prevset) {
1081 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1082 55144267 2022-09-07 thomas return -1;
1083 55144267 2022-09-07 thomas sep = "&";
1086 55144267 2022-09-07 thomas if (url->file) {
1087 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->file);
1088 55144267 2022-09-07 thomas if (tmp == NULL)
1089 55144267 2022-09-07 thomas return -1;
1090 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1091 55144267 2022-09-07 thomas free(tmp);
1092 55144267 2022-09-07 thomas if (r == -1)
1093 55144267 2022-09-07 thomas return -1;
1094 55144267 2022-09-07 thomas sep = "&";
1097 55144267 2022-09-07 thomas if (url->folder) {
1098 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->folder);
1099 55144267 2022-09-07 thomas if (tmp == NULL)
1100 55144267 2022-09-07 thomas return -1;
1101 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1102 55144267 2022-09-07 thomas free(tmp);
1103 55144267 2022-09-07 thomas if (r == -1)
1104 55144267 2022-09-07 thomas return -1;
1105 55144267 2022-09-07 thomas sep = "&";
1108 55144267 2022-09-07 thomas if (url->headref) {
1109 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->headref);
1110 55144267 2022-09-07 thomas if (tmp == NULL)
1111 55144267 2022-09-07 thomas return -1;
1112 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1113 55144267 2022-09-07 thomas free(tmp);
1114 55144267 2022-09-07 thomas if (r == -1)
1115 55144267 2022-09-07 thomas return -1;
1116 55144267 2022-09-07 thomas sep = "&";
1119 55144267 2022-09-07 thomas if (url->index_page != -1) {
1120 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sindex_page=%d", sep,
1121 55144267 2022-09-07 thomas url->index_page) == -1)
1122 55144267 2022-09-07 thomas return -1;
1123 55144267 2022-09-07 thomas sep = "&";
1126 55144267 2022-09-07 thomas if (url->path) {
1127 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->path);
1128 55144267 2022-09-07 thomas if (tmp == NULL)
1129 55144267 2022-09-07 thomas return -1;
1130 55144267 2022-09-07 thomas r = fcgi_printf(c, "%spath=%s", sep, tmp);
1131 55144267 2022-09-07 thomas free(tmp);
1132 55144267 2022-09-07 thomas if (r == -1)
1133 55144267 2022-09-07 thomas return -1;
1134 55144267 2022-09-07 thomas sep = "&";
1137 55144267 2022-09-07 thomas if (url->page != -1) {
1138 55144267 2022-09-07 thomas if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1139 55144267 2022-09-07 thomas return -1;
1140 55144267 2022-09-07 thomas sep = "&";
1143 55144267 2022-09-07 thomas return 0;
1147 d6795e9f 2022-12-30 thomas gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1149 d6795e9f 2022-12-30 thomas struct template *tp = c->tp;
1150 d6795e9f 2022-12-30 thomas const char *proto = c->https ? "https" : "http";
1152 d6795e9f 2022-12-30 thomas if (fcgi_puts(tp, proto) == -1 ||
1153 d6795e9f 2022-12-30 thomas fcgi_puts(tp, "://") == -1 ||
1154 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->server_name) == -1 ||
1155 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->document_uri) == -1)
1156 d6795e9f 2022-12-30 thomas return -1;
1158 d6795e9f 2022-12-30 thomas return gotweb_render_url(c, url);
1161 55e6cffd 2022-09-01 thomas static struct got_repository *
1162 55e6cffd 2022-09-01 thomas find_cached_repo(struct server *srv, const char *path)
1166 55e6cffd 2022-09-01 thomas for (i = 0; i < srv->ncached_repos; i++) {
1167 55e6cffd 2022-09-01 thomas if (strcmp(srv->cached_repos[i].path, path) == 0)
1168 55e6cffd 2022-09-01 thomas return srv->cached_repos[i].repo;
1171 55e6cffd 2022-09-01 thomas return NULL;
1174 8a35f56c 2022-07-16 thomas static const struct got_error *
1175 55e6cffd 2022-09-01 thomas cache_repo(struct got_repository **new, struct server *srv,
1176 55e6cffd 2022-09-01 thomas struct repo_dir *repo_dir, struct socket *sock)
1178 55e6cffd 2022-09-01 thomas const struct got_error *error = NULL;
1179 55e6cffd 2022-09-01 thomas struct got_repository *repo;
1180 55e6cffd 2022-09-01 thomas struct cached_repo *cr;
1181 55e6cffd 2022-09-01 thomas int evicted = 0;
1183 a004b24a 2022-09-06 thomas if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1184 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos - 1];
1185 55e6cffd 2022-09-01 thomas error = got_repo_close(cr->repo);
1186 55e6cffd 2022-09-01 thomas memset(cr, 0, sizeof(*cr));
1187 55e6cffd 2022-09-01 thomas srv->ncached_repos--;
1188 55e6cffd 2022-09-01 thomas if (error)
1189 55e6cffd 2022-09-01 thomas return error;
1190 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1191 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1192 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[0];
1193 55e6cffd 2022-09-01 thomas evicted = 1;
1194 55e6cffd 2022-09-01 thomas } else {
1195 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos];
1198 55e6cffd 2022-09-01 thomas error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1199 55e6cffd 2022-09-01 thomas if (error) {
1200 55e6cffd 2022-09-01 thomas if (evicted) {
1201 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1202 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1204 55e6cffd 2022-09-01 thomas return error;
1207 55e6cffd 2022-09-01 thomas if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1208 55e6cffd 2022-09-01 thomas >= sizeof(cr->path)) {
1209 55e6cffd 2022-09-01 thomas if (evicted) {
1210 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1211 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1213 55e6cffd 2022-09-01 thomas return got_error(GOT_ERR_NO_SPACE);
1216 55e6cffd 2022-09-01 thomas cr->repo = repo;
1217 55e6cffd 2022-09-01 thomas srv->ncached_repos++;
1218 55e6cffd 2022-09-01 thomas *new = repo;
1219 55e6cffd 2022-09-01 thomas return NULL;
1222 55e6cffd 2022-09-01 thomas static const struct got_error *
1223 8a35f56c 2022-07-16 thomas gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1225 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1226 8a35f56c 2022-07-16 thomas struct socket *sock = c->sock;
1227 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1228 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1229 55e6cffd 2022-09-01 thomas struct got_repository *repo = NULL;
1230 8a35f56c 2022-07-16 thomas DIR *dt;
1231 8a35f56c 2022-07-16 thomas char *dir_test;
1233 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1234 8a35f56c 2022-07-16 thomas GOTWEB_GIT_DIR) == -1)
1235 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1237 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1238 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1239 8a35f56c 2022-07-16 thomas free(dir_test);
1240 8a35f56c 2022-07-16 thomas } else {
1241 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
1242 8a35f56c 2022-07-16 thomas dir_test = NULL;
1243 c2d3d9a0 2022-09-01 thomas goto done;
1246 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1247 c2d3d9a0 2022-09-01 thomas repo_dir->name) == -1)
1248 c2d3d9a0 2022-09-01 thomas return got_error_from_errno("asprintf");
1250 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1251 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1252 8a35f56c 2022-07-16 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1253 8a35f56c 2022-07-16 thomas goto err;
1254 c2d3d9a0 2022-09-01 thomas } else {
1255 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
1256 c2d3d9a0 2022-09-01 thomas dir_test = NULL;
1260 3991b2a5 2022-10-31 thomas if (srv->respect_exportok &&
1261 3991b2a5 2022-10-31 thomas faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1262 3991b2a5 2022-10-31 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1263 3991b2a5 2022-10-31 thomas goto err;
1266 55e6cffd 2022-09-01 thomas repo = find_cached_repo(srv, repo_dir->path);
1267 55e6cffd 2022-09-01 thomas if (repo == NULL) {
1268 55e6cffd 2022-09-01 thomas error = cache_repo(&repo, srv, repo_dir, sock);
1269 55e6cffd 2022-09-01 thomas if (error)
1270 55e6cffd 2022-09-01 thomas goto err;
1272 55e6cffd 2022-09-01 thomas t->repo = repo;
1273 8a35f56c 2022-07-16 thomas error = gotweb_get_repo_description(&repo_dir->description, srv,
1274 4606e6d4 2022-11-23 thomas repo_dir->path, dirfd(dt));
1275 8a35f56c 2022-07-16 thomas if (error)
1276 8a35f56c 2022-07-16 thomas goto err;
1277 6c7f10f7 2022-11-23 thomas error = got_get_repo_owner(&repo_dir->owner, c);
1278 8a35f56c 2022-07-16 thomas if (error)
1279 8a35f56c 2022-07-16 thomas goto err;
1280 53bf32b8 2023-01-23 thomas error = got_get_repo_age(&repo_dir->age, c, NULL);
1281 8a35f56c 2022-07-16 thomas if (error)
1282 8a35f56c 2022-07-16 thomas goto err;
1283 4606e6d4 2022-11-23 thomas error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1284 4606e6d4 2022-11-23 thomas dirfd(dt));
1286 8a35f56c 2022-07-16 thomas free(dir_test);
1287 c2d3d9a0 2022-09-01 thomas if (dt != NULL && closedir(dt) == EOF && error == NULL)
1288 c2d3d9a0 2022-09-01 thomas error = got_error_from_errno("closedir");
1289 8a35f56c 2022-07-16 thomas return error;
1292 8a35f56c 2022-07-16 thomas static const struct got_error *
1293 8a35f56c 2022-07-16 thomas gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1295 8a35f56c 2022-07-16 thomas const struct got_error *error;
1297 8a35f56c 2022-07-16 thomas *repo_dir = calloc(1, sizeof(**repo_dir));
1298 8a35f56c 2022-07-16 thomas if (*repo_dir == NULL)
1299 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
1301 8a35f56c 2022-07-16 thomas if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1302 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1303 8a35f56c 2022-07-16 thomas free(*repo_dir);
1304 8a35f56c 2022-07-16 thomas *repo_dir = NULL;
1305 8a35f56c 2022-07-16 thomas return error;
1307 8a35f56c 2022-07-16 thomas (*repo_dir)->owner = NULL;
1308 8a35f56c 2022-07-16 thomas (*repo_dir)->description = NULL;
1309 8a35f56c 2022-07-16 thomas (*repo_dir)->url = NULL;
1310 8a35f56c 2022-07-16 thomas (*repo_dir)->path = NULL;
1312 8a35f56c 2022-07-16 thomas return NULL;
1315 8a35f56c 2022-07-16 thomas static const struct got_error *
1316 4606e6d4 2022-11-23 thomas gotweb_get_repo_description(char **description, struct server *srv,
1317 4606e6d4 2022-11-23 thomas const char *dirpath, int dir)
1319 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1320 4606e6d4 2022-11-23 thomas struct stat sb;
1321 4606e6d4 2022-11-23 thomas int fd = -1;
1322 4606e6d4 2022-11-23 thomas off_t len;
1324 8a35f56c 2022-07-16 thomas *description = NULL;
1325 8a35f56c 2022-07-16 thomas if (srv->show_repo_description == 0)
1326 8a35f56c 2022-07-16 thomas return NULL;
1328 4606e6d4 2022-11-23 thomas fd = openat(dir, "description", O_RDONLY);
1329 4606e6d4 2022-11-23 thomas if (fd == -1) {
1330 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1331 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1332 4606e6d4 2022-11-23 thomas dirpath, "description");
1334 8a35f56c 2022-07-16 thomas goto done;
1337 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1338 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1339 4606e6d4 2022-11-23 thomas dirpath, "description");
1340 8a35f56c 2022-07-16 thomas goto done;
1343 4606e6d4 2022-11-23 thomas len = sb.st_size;
1344 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXDESCRSZ - 1)
1345 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXDESCRSZ - 1;
1347 8a35f56c 2022-07-16 thomas *description = calloc(len + 1, sizeof(**description));
1348 8a35f56c 2022-07-16 thomas if (*description == NULL) {
1349 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1350 8a35f56c 2022-07-16 thomas goto done;
1353 4606e6d4 2022-11-23 thomas if (read(fd, *description, len) == -1)
1354 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1356 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1357 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1358 8a35f56c 2022-07-16 thomas return error;
1361 8a35f56c 2022-07-16 thomas static const struct got_error *
1362 4606e6d4 2022-11-23 thomas gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1363 4606e6d4 2022-11-23 thomas int dir)
1365 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1366 4606e6d4 2022-11-23 thomas struct stat sb;
1367 4606e6d4 2022-11-23 thomas int fd = -1;
1368 4606e6d4 2022-11-23 thomas off_t len;
1370 8a35f56c 2022-07-16 thomas *url = NULL;
1371 8a35f56c 2022-07-16 thomas if (srv->show_repo_cloneurl == 0)
1372 8a35f56c 2022-07-16 thomas return NULL;
1374 4606e6d4 2022-11-23 thomas fd = openat(dir, "cloneurl", O_RDONLY);
1375 4606e6d4 2022-11-23 thomas if (fd == -1) {
1376 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1377 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1378 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1380 8a35f56c 2022-07-16 thomas goto done;
1383 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1384 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1385 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1386 8a35f56c 2022-07-16 thomas goto done;
1389 4606e6d4 2022-11-23 thomas len = sb.st_size;
1390 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1391 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXCLONEURLSZ - 1;
1393 8a35f56c 2022-07-16 thomas *url = calloc(len + 1, sizeof(**url));
1394 8a35f56c 2022-07-16 thomas if (*url == NULL) {
1395 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1396 8a35f56c 2022-07-16 thomas goto done;
1399 4606e6d4 2022-11-23 thomas if (read(fd, *url, len) == -1)
1400 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1402 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1403 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1404 8a35f56c 2022-07-16 thomas return error;
1408 53bf32b8 2023-01-23 thomas gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1410 53bf32b8 2023-01-23 thomas struct request *c = tp->tp_arg;
1411 8a35f56c 2022-07-16 thomas struct tm tm;
1412 399ea8e4 2022-07-20 thomas long long diff_time;
1413 8a35f56c 2022-07-16 thomas const char *years = "years ago", *months = "months ago";
1414 8a35f56c 2022-07-16 thomas const char *weeks = "weeks ago", *days = "days ago";
1415 8a35f56c 2022-07-16 thomas const char *hours = "hours ago", *minutes = "minutes ago";
1416 8a35f56c 2022-07-16 thomas const char *seconds = "seconds ago", *now = "right now";
1417 8a35f56c 2022-07-16 thomas char *s;
1418 d6795e9f 2022-12-30 thomas char datebuf[64];
1419 d6795e9f 2022-12-30 thomas size_t r;
1421 8a35f56c 2022-07-16 thomas switch (ref_tm) {
1422 8a35f56c 2022-07-16 thomas case TM_DIFF:
1423 8a35f56c 2022-07-16 thomas diff_time = time(NULL) - committer_time;
1424 8a35f56c 2022-07-16 thomas if (diff_time > 60 * 60 * 24 * 365 * 2) {
1425 53bf32b8 2023-01-23 thomas if (fcgi_printf(c, "%lld %s",
1426 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 365), years) == -1)
1427 53bf32b8 2023-01-23 thomas return -1;
1428 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1429 53bf32b8 2023-01-23 thomas if (fcgi_printf(c, "%lld %s",
1430 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / (365 / 12)),
1431 8a35f56c 2022-07-16 thomas months) == -1)
1432 53bf32b8 2023-01-23 thomas return -1;
1433 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1434 53bf32b8 2023-01-23 thomas if (fcgi_printf(c, "%lld %s",
1435 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1436 53bf32b8 2023-01-23 thomas return -1;
1437 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 2) {
1438 53bf32b8 2023-01-23 thomas if (fcgi_printf(c, "%lld %s",
1439 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24), days) == -1)
1440 53bf32b8 2023-01-23 thomas return -1;
1441 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 2) {
1442 53bf32b8 2023-01-23 thomas if (fcgi_printf(c, "%lld %s",
1443 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60), hours) == -1)
1444 53bf32b8 2023-01-23 thomas return -1;
1445 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 2) {
1446 53bf32b8 2023-01-23 thomas if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1447 8a35f56c 2022-07-16 thomas minutes) == -1)
1448 53bf32b8 2023-01-23 thomas return -1;
1449 53bf32b8 2023-01-23 thomas } else if (diff_time > 2) {
1450 53bf32b8 2023-01-23 thomas if (fcgi_printf(c, "%lld %s", diff_time,
1451 8a35f56c 2022-07-16 thomas seconds) == -1)
1452 53bf32b8 2023-01-23 thomas return -1;
1453 8a35f56c 2022-07-16 thomas } else {
1454 53bf32b8 2023-01-23 thomas if (fcgi_puts(tp, now) == -1)
1455 53bf32b8 2023-01-23 thomas return -1;
1458 8a35f56c 2022-07-16 thomas case TM_LONG:
1459 8a35f56c 2022-07-16 thomas if (gmtime_r(&committer_time, &tm) == NULL)
1460 53bf32b8 2023-01-23 thomas return -1;
1462 8a35f56c 2022-07-16 thomas s = asctime_r(&tm, datebuf);
1463 8a35f56c 2022-07-16 thomas if (s == NULL)
1464 53bf32b8 2023-01-23 thomas return -1;
1466 53bf32b8 2023-01-23 thomas if (fcgi_puts(tp, datebuf) == -1 ||
1467 53bf32b8 2023-01-23 thomas fcgi_puts(tp, " UTC") == -1)
1468 53bf32b8 2023-01-23 thomas return -1;
1470 d6795e9f 2022-12-30 thomas case TM_RFC822:
1471 d6795e9f 2022-12-30 thomas if (gmtime_r(&committer_time, &tm) == NULL)
1472 53bf32b8 2023-01-23 thomas return -1;
1474 d6795e9f 2022-12-30 thomas r = strftime(datebuf, sizeof(datebuf),
1475 d6795e9f 2022-12-30 thomas "%a, %d %b %Y %H:%M:%S GMT", &tm);
1476 d6795e9f 2022-12-30 thomas if (r == 0)
1477 53bf32b8 2023-01-23 thomas return -1;
1479 53bf32b8 2023-01-23 thomas if (fcgi_puts(tp, datebuf) == -1)
1480 53bf32b8 2023-01-23 thomas return -1;
1483 53bf32b8 2023-01-23 thomas return 0;