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.
20 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
22 8a35f56c 2022-07-16 thomas #include <net/if.h>
23 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
24 3e12c168 2022-07-16 thomas #include <sys/queue.h>
25 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
26 8a35f56c 2022-07-16 thomas #include <sys/types.h>
28 2ac684a4 2022-09-03 thomas #include <ctype.h>
29 8a35f56c 2022-07-16 thomas #include <dirent.h>
30 8a35f56c 2022-07-16 thomas #include <errno.h>
31 8a35f56c 2022-07-16 thomas #include <event.h>
32 4606e6d4 2022-11-23 thomas #include <fcntl.h>
33 4606e6d4 2022-11-23 thomas #include <imsg.h>
34 8a35f56c 2022-07-16 thomas #include <stdio.h>
35 8a35f56c 2022-07-16 thomas #include <stdlib.h>
36 8a35f56c 2022-07-16 thomas #include <string.h>
37 8a35f56c 2022-07-16 thomas #include <unistd.h>
39 8a35f56c 2022-07-16 thomas #include "got_error.h"
40 8a35f56c 2022-07-16 thomas #include "got_object.h"
41 8a35f56c 2022-07-16 thomas #include "got_reference.h"
42 8a35f56c 2022-07-16 thomas #include "got_repository.h"
43 8a35f56c 2022-07-16 thomas #include "got_path.h"
44 8a35f56c 2022-07-16 thomas #include "got_cancel.h"
45 8a35f56c 2022-07-16 thomas #include "got_worktree.h"
46 8a35f56c 2022-07-16 thomas #include "got_diff.h"
47 8a35f56c 2022-07-16 thomas #include "got_commit_graph.h"
48 8a35f56c 2022-07-16 thomas #include "got_blame.h"
49 8a35f56c 2022-07-16 thomas #include "got_privsep.h"
51 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
52 d6795e9f 2022-12-30 thomas #include "tmpl.h"
54 8a35f56c 2022-07-16 thomas static const struct querystring_keys querystring_keys[] = {
55 8a35f56c 2022-07-16 thomas { "action", ACTION },
56 8a35f56c 2022-07-16 thomas { "commit", COMMIT },
57 8a35f56c 2022-07-16 thomas { "file", RFILE },
58 8a35f56c 2022-07-16 thomas { "folder", FOLDER },
59 8a35f56c 2022-07-16 thomas { "headref", HEADREF },
60 8a35f56c 2022-07-16 thomas { "index_page", INDEX_PAGE },
61 8a35f56c 2022-07-16 thomas { "path", PATH },
64 8a35f56c 2022-07-16 thomas static const struct action_keys action_keys[] = {
65 8a35f56c 2022-07-16 thomas { "blame", BLAME },
66 8a35f56c 2022-07-16 thomas { "blob", BLOB },
67 b82440e1 2023-01-06 thomas { "blobraw", BLOBRAW },
68 8a35f56c 2022-07-16 thomas { "briefs", BRIEFS },
69 8a35f56c 2022-07-16 thomas { "commits", COMMITS },
70 8a35f56c 2022-07-16 thomas { "diff", DIFF },
71 8a35f56c 2022-07-16 thomas { "error", ERR },
72 8a35f56c 2022-07-16 thomas { "index", INDEX },
73 feaddee6 2023-12-03 thomas { "patch", PATCH },
74 8a35f56c 2022-07-16 thomas { "summary", SUMMARY },
75 8a35f56c 2022-07-16 thomas { "tag", TAG },
76 8a35f56c 2022-07-16 thomas { "tags", TAGS },
77 8a35f56c 2022-07-16 thomas { "tree", TREE },
78 d6795e9f 2022-12-30 thomas { "rss", RSS },
81 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_querystring(struct querystring **);
82 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_assign_querystring(struct querystring **,
85 8a35f56c 2022-07-16 thomas char *, char *);
86 161663e7 2023-03-11 thomas static int gotweb_render_index(struct template *);
87 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
88 8a35f56c 2022-07-16 thomas const char *);
89 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_load_got_path(struct request *c,
90 8a35f56c 2022-07-16 thomas struct repo_dir *);
91 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_repo_description(char **,
92 4606e6d4 2022-11-23 thomas struct server *, const char *, int);
93 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_clone_url(char **, struct server *,
94 4606e6d4 2022-11-23 thomas const char *, int);
96 8a35f56c 2022-07-16 thomas static void gotweb_free_querystring(struct querystring *);
97 8a35f56c 2022-07-16 thomas static void gotweb_free_repo_dir(struct repo_dir *);
99 b95d1cf6 2023-06-25 thomas struct server *gotweb_get_server(const char *);
101 50237485 2023-01-23 thomas static int
102 50237485 2023-01-23 thomas gotweb_reply(struct request *c, int status, const char *ctype,
103 50237485 2023-01-23 thomas struct gotweb_url *location)
105 50237485 2023-01-23 thomas const char *csp;
107 d8bf4f25 2023-09-14 thomas if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
108 50237485 2023-01-23 thomas return -1;
110 50237485 2023-01-23 thomas if (location) {
111 d8bf4f25 2023-09-14 thomas if (tp_writes(c->tp, "Location: ") == -1 ||
112 50237485 2023-01-23 thomas gotweb_render_url(c, location) == -1 ||
113 d8bf4f25 2023-09-14 thomas tp_writes(c->tp, "\r\n") == -1)
114 50237485 2023-01-23 thomas return -1;
117 50237485 2023-01-23 thomas csp = "Content-Security-Policy: default-src 'self'; "
118 50237485 2023-01-23 thomas "script-src 'none'; object-src 'none';\r\n";
119 d8bf4f25 2023-09-14 thomas if (tp_writes(c->tp, csp) == -1)
120 50237485 2023-01-23 thomas return -1;
122 d8bf4f25 2023-09-14 thomas if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
123 50237485 2023-01-23 thomas return -1;
125 d8bf4f25 2023-09-14 thomas return tp_writes(c->tp, "\r\n");
128 50237485 2023-01-23 thomas static int
129 50237485 2023-01-23 thomas gotweb_reply_file(struct request *c, const char *ctype, const char *file,
130 50237485 2023-01-23 thomas const char *suffix)
134 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "Content-Disposition: attachment; "
135 50237485 2023-01-23 thomas "filename=%s%s\r\n", file, suffix ? suffix : "");
136 50237485 2023-01-23 thomas if (r == -1)
137 50237485 2023-01-23 thomas return -1;
138 50237485 2023-01-23 thomas return gotweb_reply(c, 200, ctype, NULL);
142 8a35f56c 2022-07-16 thomas gotweb_process_request(struct request *c)
144 164b5ddc 2023-03-11 thomas const struct got_error *error = NULL;
145 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
146 8a35f56c 2022-07-16 thomas struct querystring *qs = NULL;
147 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
148 44f2135a 2023-04-01 thomas const char *rss_ctype = "application/rss+xml;charset=utf-8";
149 44f2135a 2023-04-01 thomas const uint8_t *buf;
150 44f2135a 2023-04-01 thomas size_t len;
151 44f2135a 2023-04-01 thomas int r, binary = 0;
153 8a35f56c 2022-07-16 thomas /* init the transport */
154 8a35f56c 2022-07-16 thomas error = gotweb_init_transport(&c->t);
155 8a35f56c 2022-07-16 thomas if (error) {
156 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
159 8a35f56c 2022-07-16 thomas /* don't process any further if client disconnected */
160 8a35f56c 2022-07-16 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
162 8a35f56c 2022-07-16 thomas /* get the gotwebd server */
163 b95d1cf6 2023-06-25 thomas srv = gotweb_get_server(c->server_name);
164 8a35f56c 2022-07-16 thomas if (srv == NULL) {
165 8a35f56c 2022-07-16 thomas log_warnx("%s: error server is NULL", __func__);
166 8a35f56c 2022-07-16 thomas goto err;
168 8a35f56c 2022-07-16 thomas c->srv = srv;
169 8a35f56c 2022-07-16 thomas /* parse our querystring */
170 8a35f56c 2022-07-16 thomas error = gotweb_init_querystring(&qs);
171 8a35f56c 2022-07-16 thomas if (error) {
172 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
173 8a35f56c 2022-07-16 thomas goto err;
175 8a35f56c 2022-07-16 thomas c->t->qs = qs;
176 8a35f56c 2022-07-16 thomas error = gotweb_parse_querystring(&qs, c->querystring);
177 8a35f56c 2022-07-16 thomas if (error) {
178 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
179 8a35f56c 2022-07-16 thomas goto err;
183 8a35f56c 2022-07-16 thomas * certain actions require a commit id in the querystring. this stops
184 8a35f56c 2022-07-16 thomas * bad actors from exploiting this by manually manipulating the
185 8a35f56c 2022-07-16 thomas * querystring.
188 b82440e1 2023-01-06 thomas if (qs->action == BLAME || qs->action == BLOB ||
189 feaddee6 2023-12-03 thomas qs->action == BLOBRAW || qs->action == DIFF ||
190 feaddee6 2023-12-03 thomas qs->action == PATCH) {
191 b82440e1 2023-01-06 thomas if (qs->commit == NULL) {
192 b2b363b2 2023-06-22 thomas error = got_error(GOT_ERR_BAD_QUERYSTRING);
193 164b5ddc 2023-03-11 thomas goto err;
197 8a35f56c 2022-07-16 thomas if (qs->action != INDEX) {
198 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, qs->path);
199 8a35f56c 2022-07-16 thomas if (error)
200 164b5ddc 2023-03-11 thomas goto err;
201 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
202 8a35f56c 2022-07-16 thomas c->t->repo_dir = repo_dir;
203 8a35f56c 2022-07-16 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
204 8a35f56c 2022-07-16 thomas goto err;
207 44f2135a 2023-04-01 thomas if (qs->action == BLOBRAW || qs->action == BLOB) {
208 f03e50d6 2024-01-31 thomas if (qs->folder == NULL || qs->file == NULL) {
209 f03e50d6 2024-01-31 thomas error = got_error(GOT_ERR_BAD_QUERYSTRING);
210 f03e50d6 2024-01-31 thomas goto err;
213 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
214 8a35f56c 2022-07-16 thomas if (error)
215 164b5ddc 2023-03-11 thomas goto err;
217 164b5ddc 2023-03-11 thomas error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
218 34875d49 2023-12-08 thomas &binary, c, qs->folder, qs->file, qs->commit);
219 164b5ddc 2023-03-11 thomas if (error)
220 164b5ddc 2023-03-11 thomas goto err;
223 aea518b5 2023-11-22 thomas switch (qs->action) {
224 44f2135a 2023-04-01 thomas case BLAME:
225 f03e50d6 2024-01-31 thomas if (qs->folder == NULL || qs->file == NULL) {
226 f03e50d6 2024-01-31 thomas error = got_error(GOT_ERR_BAD_QUERYSTRING);
227 f03e50d6 2024-01-31 thomas goto err;
229 44f2135a 2023-04-01 thomas error = got_get_repo_commits(c, 1);
230 44f2135a 2023-04-01 thomas if (error) {
231 44f2135a 2023-04-01 thomas log_warnx("%s: %s", __func__, error->msg);
232 44f2135a 2023-04-01 thomas goto err;
234 44f2135a 2023-04-01 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
236 44f2135a 2023-04-01 thomas gotweb_render_page(c->tp, gotweb_render_blame);
238 44f2135a 2023-04-01 thomas case BLOB:
239 44f2135a 2023-04-01 thomas if (binary) {
240 44f2135a 2023-04-01 thomas struct gotweb_url url = {
241 44f2135a 2023-04-01 thomas .index_page = -1,
242 44f2135a 2023-04-01 thomas .action = BLOBRAW,
243 44f2135a 2023-04-01 thomas .path = qs->path,
244 44f2135a 2023-04-01 thomas .commit = qs->commit,
245 44f2135a 2023-04-01 thomas .folder = qs->folder,
246 44f2135a 2023-04-01 thomas .file = qs->file,
249 44f2135a 2023-04-01 thomas gotweb_reply(c, 302, NULL, &url);
253 44f2135a 2023-04-01 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
255 44f2135a 2023-04-01 thomas gotweb_render_page(c->tp, gotweb_render_blob);
257 44f2135a 2023-04-01 thomas case BLOBRAW:
258 ea1b5cf5 2023-01-15 thomas if (binary)
259 50237485 2023-01-23 thomas r = gotweb_reply_file(c, "application/octet-stream",
260 50237485 2023-01-23 thomas qs->file, NULL);
262 50237485 2023-01-23 thomas r = gotweb_reply(c, 200, "text/plain", NULL);
263 50237485 2023-01-23 thomas if (r == -1)
265 d8bf4f25 2023-09-14 thomas if (template_flush(c->tp) == -1)
268 ea1b5cf5 2023-01-15 thomas for (;;) {
269 161663e7 2023-03-11 thomas error = got_object_blob_read_block(&len, c->t->blob);
270 ea1b5cf5 2023-01-15 thomas if (error)
272 ea1b5cf5 2023-01-15 thomas if (len == 0)
274 161663e7 2023-03-11 thomas buf = got_object_blob_get_read_buf(c->t->blob);
275 d8bf4f25 2023-09-14 thomas if (fcgi_write(c, buf, len) == -1)
279 8a35f56c 2022-07-16 thomas case BRIEFS:
280 b2251e6e 2023-06-26 thomas error = got_get_repo_commits(c, srv->max_commits_display);
281 b2251e6e 2023-06-26 thomas if (error)
282 b2251e6e 2023-06-26 thomas goto err;
283 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
285 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_briefs);
287 8a35f56c 2022-07-16 thomas case COMMITS:
288 7ade8b27 2022-12-30 thomas error = got_get_repo_commits(c, srv->max_commits_display);
289 8a35f56c 2022-07-16 thomas if (error) {
290 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
291 8a35f56c 2022-07-16 thomas goto err;
293 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
295 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_commits);
297 8a35f56c 2022-07-16 thomas case DIFF:
298 dccd05b4 2023-01-10 thomas error = got_get_repo_commits(c, 1);
299 5d860bce 2023-01-07 thomas if (error) {
300 5d860bce 2023-01-07 thomas log_warnx("%s: %s", __func__, error->msg);
301 5d860bce 2023-01-07 thomas goto err;
303 04b2c111 2023-05-16 thomas error = got_open_diff_for_output(&c->t->fp, c);
304 dccd05b4 2023-01-10 thomas if (error) {
305 dccd05b4 2023-01-10 thomas log_warnx("%s: %s", __func__, error->msg);
306 dccd05b4 2023-01-10 thomas goto err;
308 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
310 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_diff);
312 8a35f56c 2022-07-16 thomas case INDEX:
313 161663e7 2023-03-11 thomas c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
314 161663e7 2023-03-11 thomas alphasort);
315 161663e7 2023-03-11 thomas if (c->t->nrepos == -1) {
316 161663e7 2023-03-11 thomas c->t->repos = NULL;
317 161663e7 2023-03-11 thomas error = got_error_from_errno2("scandir",
318 161663e7 2023-03-11 thomas srv->repos_path);
319 8a35f56c 2022-07-16 thomas goto err;
321 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
323 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_index);
325 feaddee6 2023-12-03 thomas case PATCH:
326 feaddee6 2023-12-03 thomas error = got_get_repo_commits(c, 1);
327 feaddee6 2023-12-03 thomas if (error) {
328 feaddee6 2023-12-03 thomas log_warnx("%s: %s", __func__, error->msg);
329 feaddee6 2023-12-03 thomas goto err;
331 feaddee6 2023-12-03 thomas error = got_open_diff_for_output(&c->t->fp, c);
332 feaddee6 2023-12-03 thomas if (error) {
333 feaddee6 2023-12-03 thomas log_warnx("%s: %s", __func__, error->msg);
334 feaddee6 2023-12-03 thomas goto err;
336 feaddee6 2023-12-03 thomas if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
338 feaddee6 2023-12-03 thomas gotweb_render_patch(c->tp);
340 44f2135a 2023-04-01 thomas case RSS:
341 44f2135a 2023-04-01 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
342 44f2135a 2023-04-01 thomas if (error)
343 44f2135a 2023-04-01 thomas goto err;
344 44f2135a 2023-04-01 thomas if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
347 44f2135a 2023-04-01 thomas gotweb_render_rss(c->tp);
349 8a35f56c 2022-07-16 thomas case SUMMARY:
350 161663e7 2023-03-11 thomas error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
351 18e466eb 2023-01-14 thomas got_ref_cmp_by_name, NULL);
352 8a35f56c 2022-07-16 thomas if (error) {
353 18e466eb 2023-01-14 thomas log_warnx("%s: got_ref_list: %s", __func__,
354 18e466eb 2023-01-14 thomas error->msg);
355 8a35f56c 2022-07-16 thomas goto err;
357 e85a14fa 2023-12-30 thomas error = got_get_repo_commits(c, srv->summary_commits_display);
358 b2251e6e 2023-06-26 thomas if (error)
359 b2251e6e 2023-06-26 thomas goto err;
360 18e466eb 2023-01-14 thomas qs->action = TAGS;
361 e85a14fa 2023-12-30 thomas error = got_get_repo_tags(c, srv->summary_tags_display);
362 18e466eb 2023-01-14 thomas if (error) {
363 18e466eb 2023-01-14 thomas log_warnx("%s: got_get_repo_tags: %s", __func__,
364 18e466eb 2023-01-14 thomas error->msg);
365 18e466eb 2023-01-14 thomas goto err;
367 18e466eb 2023-01-14 thomas qs->action = SUMMARY;
368 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
370 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_summary);
372 8a35f56c 2022-07-16 thomas case TAG:
373 145ca42a 2023-01-09 thomas error = got_get_repo_tags(c, 1);
374 8a35f56c 2022-07-16 thomas if (error) {
375 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
376 145ca42a 2023-01-09 thomas goto err;
378 145ca42a 2023-01-09 thomas if (c->t->tag_count == 0) {
379 145ca42a 2023-01-09 thomas error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
380 145ca42a 2023-01-09 thomas "bad commit id");
381 8a35f56c 2022-07-16 thomas goto err;
383 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
385 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_tag);
387 8a35f56c 2022-07-16 thomas case TAGS:
388 b3ba36c3 2023-01-14 thomas error = got_get_repo_tags(c, srv->max_commits_display);
389 8a35f56c 2022-07-16 thomas if (error) {
390 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
391 8a35f56c 2022-07-16 thomas goto err;
393 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
395 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_tags);
397 8a35f56c 2022-07-16 thomas case TREE:
398 3c14c1f2 2023-01-06 thomas error = got_get_repo_commits(c, 1);
399 8a35f56c 2022-07-16 thomas if (error) {
400 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
401 8a35f56c 2022-07-16 thomas goto err;
403 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
405 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_tree);
407 8a35f56c 2022-07-16 thomas case ERR:
409 164b5ddc 2023-03-11 thomas error = got_error(GOT_ERR_BAD_QUERYSTRING);
413 164b5ddc 2023-03-11 thomas c->t->error = error;
414 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 400, "text/html", NULL) == -1)
416 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_error);
419 8a35f56c 2022-07-16 thomas struct server *
420 b95d1cf6 2023-06-25 thomas gotweb_get_server(const char *server_name)
422 b95d1cf6 2023-06-25 thomas struct server *srv;
424 3f0158b4 2022-08-30 thomas /* check against the server name first */
425 102d840d 2023-06-22 thomas if (*server_name != '\0')
426 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
427 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, server_name) == 0)
428 b95d1cf6 2023-06-25 thomas return srv;
430 b95d1cf6 2023-06-25 thomas /* otherwise, use the first server */
431 b95d1cf6 2023-06-25 thomas return TAILQ_FIRST(&gotwebd_env->servers);
434 8a35f56c 2022-07-16 thomas const struct got_error *
435 8a35f56c 2022-07-16 thomas gotweb_init_transport(struct transport **t)
437 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
439 8a35f56c 2022-07-16 thomas *t = calloc(1, sizeof(**t));
440 8a35f56c 2022-07-16 thomas if (*t == NULL)
441 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "calloc");
443 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_commits);
444 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_tags);
445 161663e7 2023-03-11 thomas TAILQ_INIT(&(*t)->refs);
447 161663e7 2023-03-11 thomas (*t)->fd = -1;
449 8a35f56c 2022-07-16 thomas return error;
452 8a35f56c 2022-07-16 thomas static const struct got_error *
453 8a35f56c 2022-07-16 thomas gotweb_init_querystring(struct querystring **qs)
455 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
457 8a35f56c 2022-07-16 thomas *qs = calloc(1, sizeof(**qs));
458 8a35f56c 2022-07-16 thomas if (*qs == NULL)
459 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "calloc");
461 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup("HEAD");
462 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
463 84630254 2022-09-02 thomas free(*qs);
464 84630254 2022-09-02 thomas *qs = NULL;
465 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "strdup");
468 84630254 2022-09-02 thomas (*qs)->action = INDEX;
470 8a35f56c 2022-07-16 thomas return error;
473 8a35f56c 2022-07-16 thomas static const struct got_error *
474 8a35f56c 2022-07-16 thomas gotweb_parse_querystring(struct querystring **qs, char *qst)
476 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
477 8a35f56c 2022-07-16 thomas char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
478 8a35f56c 2022-07-16 thomas char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
480 8a35f56c 2022-07-16 thomas if (qst == NULL)
481 8a35f56c 2022-07-16 thomas return error;
483 8a35f56c 2022-07-16 thomas tok1 = strdup(qst);
484 8a35f56c 2022-07-16 thomas if (tok1 == NULL)
485 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "strdup");
487 8a35f56c 2022-07-16 thomas tok1_pair = tok1;
488 8a35f56c 2022-07-16 thomas tok1_end = tok1;
490 8a35f56c 2022-07-16 thomas while (tok1_pair != NULL) {
491 8a35f56c 2022-07-16 thomas strsep(&tok1_end, "&");
493 8a35f56c 2022-07-16 thomas tok2 = strdup(tok1_pair);
494 8a35f56c 2022-07-16 thomas if (tok2 == NULL) {
495 8a35f56c 2022-07-16 thomas free(tok1);
496 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "strdup");
499 8a35f56c 2022-07-16 thomas tok2_pair = tok2;
500 8a35f56c 2022-07-16 thomas tok2_end = tok2;
502 8a35f56c 2022-07-16 thomas while (tok2_pair != NULL) {
503 8a35f56c 2022-07-16 thomas strsep(&tok2_end, "=");
504 8a35f56c 2022-07-16 thomas if (tok2_end) {
505 8a35f56c 2022-07-16 thomas error = gotweb_assign_querystring(qs, tok2_pair,
506 8a35f56c 2022-07-16 thomas tok2_end);
507 8a35f56c 2022-07-16 thomas if (error)
508 8a35f56c 2022-07-16 thomas goto err;
510 8a35f56c 2022-07-16 thomas tok2_pair = tok2_end;
512 8a35f56c 2022-07-16 thomas free(tok2);
513 8a35f56c 2022-07-16 thomas tok1_pair = tok1_end;
515 8a35f56c 2022-07-16 thomas free(tok1);
516 8a35f56c 2022-07-16 thomas return error;
518 8a35f56c 2022-07-16 thomas free(tok2);
519 8a35f56c 2022-07-16 thomas free(tok1);
520 8a35f56c 2022-07-16 thomas return error;
524 2ac684a4 2022-09-03 thomas * Adapted from usr.sbin/httpd/httpd.c url_decode.
526 8a35f56c 2022-07-16 thomas static const struct got_error *
527 2ac684a4 2022-09-03 thomas gotweb_urldecode(char *url)
529 2ac684a4 2022-09-03 thomas char *p, *q;
530 2ac684a4 2022-09-03 thomas char hex[3];
531 2ac684a4 2022-09-03 thomas unsigned long x;
533 2ac684a4 2022-09-03 thomas hex[2] = '\0';
534 2ac684a4 2022-09-03 thomas p = q = url;
536 2ac684a4 2022-09-03 thomas while (*p != '\0') {
537 2ac684a4 2022-09-03 thomas switch (*p) {
538 2ac684a4 2022-09-03 thomas case '%':
539 2ac684a4 2022-09-03 thomas /* Encoding character is followed by two hex chars */
540 2ac684a4 2022-09-03 thomas if (!isxdigit((unsigned char)p[1]) ||
541 2ac684a4 2022-09-03 thomas !isxdigit((unsigned char)p[2]) ||
542 2ac684a4 2022-09-03 thomas (p[1] == '0' && p[2] == '0'))
543 2ac684a4 2022-09-03 thomas return got_error(GOT_ERR_BAD_QUERYSTRING);
545 2ac684a4 2022-09-03 thomas hex[0] = p[1];
546 2ac684a4 2022-09-03 thomas hex[1] = p[2];
549 2ac684a4 2022-09-03 thomas * We don't have to validate "hex" because it is
550 2ac684a4 2022-09-03 thomas * guaranteed to include two hex chars followed by nul.
552 2ac684a4 2022-09-03 thomas x = strtoul(hex, NULL, 16);
553 2ac684a4 2022-09-03 thomas *q = (char)x;
563 2ac684a4 2022-09-03 thomas *q = '\0';
565 2ac684a4 2022-09-03 thomas return NULL;
568 2ac684a4 2022-09-03 thomas static const struct got_error *
569 8a35f56c 2022-07-16 thomas gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
571 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
572 8a35f56c 2022-07-16 thomas const char *errstr;
573 8a35f56c 2022-07-16 thomas int a_cnt, el_cnt;
575 2ac684a4 2022-09-03 thomas error = gotweb_urldecode(value);
576 2ac684a4 2022-09-03 thomas if (error)
577 2ac684a4 2022-09-03 thomas return error;
579 b38e9a2a 2023-11-22 thomas for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
580 8a35f56c 2022-07-16 thomas if (strcmp(key, querystring_keys[el_cnt].name) != 0)
581 8a35f56c 2022-07-16 thomas continue;
583 8a35f56c 2022-07-16 thomas switch (querystring_keys[el_cnt].element) {
584 8a35f56c 2022-07-16 thomas case ACTION:
585 cd292c73 2023-12-18 thomas for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
586 8a35f56c 2022-07-16 thomas if (strcmp(value, action_keys[a_cnt].name) != 0)
587 8a35f56c 2022-07-16 thomas continue;
588 8a35f56c 2022-07-16 thomas else if (strcmp(value,
589 8a35f56c 2022-07-16 thomas action_keys[a_cnt].name) == 0){
590 8a35f56c 2022-07-16 thomas (*qs)->action =
591 8a35f56c 2022-07-16 thomas action_keys[a_cnt].action;
592 8a35f56c 2022-07-16 thomas goto qa_found;
595 8a35f56c 2022-07-16 thomas (*qs)->action = ERR;
596 8a35f56c 2022-07-16 thomas qa_found:
598 8a35f56c 2022-07-16 thomas case COMMIT:
599 8a35f56c 2022-07-16 thomas (*qs)->commit = strdup(value);
600 8a35f56c 2022-07-16 thomas if ((*qs)->commit == NULL) {
601 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
602 d8edcc94 2023-06-01 thomas "strdup");
603 8a35f56c 2022-07-16 thomas goto done;
606 8a35f56c 2022-07-16 thomas case RFILE:
607 8a35f56c 2022-07-16 thomas (*qs)->file = strdup(value);
608 8a35f56c 2022-07-16 thomas if ((*qs)->file == NULL) {
609 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
610 d8edcc94 2023-06-01 thomas "strdup");
611 8a35f56c 2022-07-16 thomas goto done;
614 8a35f56c 2022-07-16 thomas case FOLDER:
615 8a35f56c 2022-07-16 thomas (*qs)->folder = strdup(value);
616 8a35f56c 2022-07-16 thomas if ((*qs)->folder == NULL) {
617 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
618 d8edcc94 2023-06-01 thomas "strdup");
619 8a35f56c 2022-07-16 thomas goto done;
622 8a35f56c 2022-07-16 thomas case HEADREF:
623 b0764984 2022-09-02 thomas free((*qs)->headref);
624 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup(value);
625 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
626 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
627 d8edcc94 2023-06-01 thomas "strdup");
628 8a35f56c 2022-07-16 thomas goto done;
631 8a35f56c 2022-07-16 thomas case INDEX_PAGE:
632 102d840d 2023-06-22 thomas if (*value == '\0')
634 8a35f56c 2022-07-16 thomas (*qs)->index_page = strtonum(value, INT64_MIN,
635 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
636 8a35f56c 2022-07-16 thomas if (errstr) {
637 d8edcc94 2023-06-01 thomas error = got_error_from_errno3(__func__,
638 d8edcc94 2023-06-01 thomas "strtonum", errstr);
639 8a35f56c 2022-07-16 thomas goto done;
641 3d6d1fb0 2022-12-30 thomas if ((*qs)->index_page < 0)
642 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
644 8a35f56c 2022-07-16 thomas case PATH:
645 8a35f56c 2022-07-16 thomas (*qs)->path = strdup(value);
646 8a35f56c 2022-07-16 thomas if ((*qs)->path == NULL) {
647 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
648 d8edcc94 2023-06-01 thomas "strdup");
649 8a35f56c 2022-07-16 thomas goto done;
654 b38e9a2a 2023-11-22 thomas /* entry found */
658 8a35f56c 2022-07-16 thomas return error;
662 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(struct repo_tag *rt)
664 8a35f56c 2022-07-16 thomas if (rt != NULL) {
665 8a35f56c 2022-07-16 thomas free(rt->commit_id);
666 217813df 2022-09-02 thomas free(rt->tag_name);
667 217813df 2022-09-02 thomas free(rt->tag_commit);
668 217813df 2022-09-02 thomas free(rt->commit_msg);
669 8a35f56c 2022-07-16 thomas free(rt->tagger);
671 8a35f56c 2022-07-16 thomas free(rt);
675 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(struct repo_commit *rc)
677 8a35f56c 2022-07-16 thomas if (rc != NULL) {
678 8a35f56c 2022-07-16 thomas free(rc->path);
679 8a35f56c 2022-07-16 thomas free(rc->refs_str);
680 8a35f56c 2022-07-16 thomas free(rc->commit_id);
681 8a35f56c 2022-07-16 thomas free(rc->parent_id);
682 8a35f56c 2022-07-16 thomas free(rc->tree_id);
683 8a35f56c 2022-07-16 thomas free(rc->author);
684 8a35f56c 2022-07-16 thomas free(rc->committer);
685 8a35f56c 2022-07-16 thomas free(rc->commit_msg);
687 8a35f56c 2022-07-16 thomas free(rc);
690 8a35f56c 2022-07-16 thomas static void
691 8a35f56c 2022-07-16 thomas gotweb_free_querystring(struct querystring *qs)
693 8a35f56c 2022-07-16 thomas if (qs != NULL) {
694 8a35f56c 2022-07-16 thomas free(qs->commit);
695 8a35f56c 2022-07-16 thomas free(qs->file);
696 8a35f56c 2022-07-16 thomas free(qs->folder);
697 8a35f56c 2022-07-16 thomas free(qs->headref);
698 8a35f56c 2022-07-16 thomas free(qs->path);
700 8a35f56c 2022-07-16 thomas free(qs);
703 8a35f56c 2022-07-16 thomas static void
704 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(struct repo_dir *repo_dir)
706 8a35f56c 2022-07-16 thomas if (repo_dir != NULL) {
707 8a35f56c 2022-07-16 thomas free(repo_dir->name);
708 8a35f56c 2022-07-16 thomas free(repo_dir->owner);
709 8a35f56c 2022-07-16 thomas free(repo_dir->description);
710 8a35f56c 2022-07-16 thomas free(repo_dir->url);
711 8a35f56c 2022-07-16 thomas free(repo_dir->path);
713 8a35f56c 2022-07-16 thomas free(repo_dir);
717 8a35f56c 2022-07-16 thomas gotweb_free_transport(struct transport *t)
719 161663e7 2023-03-11 thomas const struct got_error *err;
720 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL, *trc = NULL;
721 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
724 161663e7 2023-03-11 thomas got_ref_list_free(&t->refs);
725 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
726 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_commits, rc, entry);
727 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(rc);
729 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
730 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
731 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
733 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(t->repo_dir);
734 8a35f56c 2022-07-16 thomas gotweb_free_querystring(t->qs);
735 20bab626 2023-02-03 thomas free(t->more_id);
736 978394e7 2023-12-08 thomas free(t->tags_more_id);
737 161663e7 2023-03-11 thomas if (t->blob)
738 161663e7 2023-03-11 thomas got_object_blob_close(t->blob);
739 161663e7 2023-03-11 thomas if (t->fp) {
740 d00235d8 2023-05-17 thomas err = got_gotweb_closefile(t->fp);
742 d00235d8 2023-05-17 thomas log_warnx("%s: got_gotweb_closefile failure: %s",
743 161663e7 2023-03-11 thomas __func__, err->msg);
745 54ca6e2b 2023-05-16 thomas if (t->fd != -1 && close(t->fd) == -1)
746 54ca6e2b 2023-05-16 thomas log_warn("%s: close", __func__);
747 161663e7 2023-03-11 thomas if (t->repos) {
748 161663e7 2023-03-11 thomas for (i = 0; i < t->nrepos; ++i)
749 161663e7 2023-03-11 thomas free(t->repos[i]);
750 161663e7 2023-03-11 thomas free(t->repos);
752 ab5bda7e 2023-11-22 thomas if (t->repo)
753 ab5bda7e 2023-11-22 thomas got_repo_close(t->repo);
758 978394e7 2023-12-08 thomas gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
759 2f4f0731 2022-12-30 thomas struct gotweb_url *next, int *have_next)
761 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
762 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
763 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
765 2f4f0731 2022-12-30 thomas *have_prev = *have_next = 0;
767 978394e7 2023-12-08 thomas if (qs->index_page > 0) {
768 978394e7 2023-12-08 thomas *have_prev = 1;
769 978394e7 2023-12-08 thomas *prev = (struct gotweb_url){
770 978394e7 2023-12-08 thomas .action = -1,
771 978394e7 2023-12-08 thomas .index_page = qs->index_page - 1,
774 978394e7 2023-12-08 thomas if (t->next_disp == srv->max_repos_display &&
775 978394e7 2023-12-08 thomas t->repos_total != (qs->index_page + 1) *
776 978394e7 2023-12-08 thomas srv->max_repos_display) {
777 978394e7 2023-12-08 thomas *have_next = 1;
778 978394e7 2023-12-08 thomas *next = (struct gotweb_url){
779 978394e7 2023-12-08 thomas .action = -1,
780 978394e7 2023-12-08 thomas .index_page = qs->index_page + 1,
785 161663e7 2023-03-11 thomas static int
786 161663e7 2023-03-11 thomas gotweb_render_index(struct template *tp)
788 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
789 161663e7 2023-03-11 thomas struct request *c = tp->tp_arg;
790 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
791 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
792 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
793 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
794 161663e7 2023-03-11 thomas struct dirent **sd_dent = t->repos;
795 161663e7 2023-03-11 thomas unsigned int d_i, d_disp = 0;
796 24240f6a 2022-11-23 thomas unsigned int d_skipped = 0;
797 161663e7 2023-03-11 thomas int type, r;
799 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_table_hdr(c->tp) == -1)
800 161663e7 2023-03-11 thomas return -1;
802 161663e7 2023-03-11 thomas for (d_i = 0; d_i < t->nrepos; d_i++) {
803 8a35f56c 2022-07-16 thomas if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
804 24240f6a 2022-11-23 thomas strcmp(sd_dent[d_i]->d_name, "..") == 0) {
805 24240f6a 2022-11-23 thomas d_skipped++;
806 24240f6a 2022-11-23 thomas continue;
809 24240f6a 2022-11-23 thomas error = got_path_dirent_type(&type, srv->repos_path,
810 24240f6a 2022-11-23 thomas sd_dent[d_i]);
811 24240f6a 2022-11-23 thomas if (error)
812 161663e7 2023-03-11 thomas continue;
813 24240f6a 2022-11-23 thomas if (type != DT_DIR) {
814 24240f6a 2022-11-23 thomas d_skipped++;
815 8a35f56c 2022-07-16 thomas continue;
818 8a35f56c 2022-07-16 thomas if (qs->index_page > 0 && (qs->index_page *
819 8a35f56c 2022-07-16 thomas srv->max_repos_display) > t->prev_disp) {
820 8a35f56c 2022-07-16 thomas t->prev_disp++;
821 8a35f56c 2022-07-16 thomas continue;
824 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
825 8a35f56c 2022-07-16 thomas if (error)
826 161663e7 2023-03-11 thomas continue;
828 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
829 f5af6dbd 2023-04-29 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
830 161663e7 2023-03-11 thomas if (error->code != GOT_ERR_NOT_GIT_REPO)
831 161663e7 2023-03-11 thomas log_warnx("%s: %s: %s", __func__,
832 161663e7 2023-03-11 thomas sd_dent[d_i]->d_name, error->msg);
833 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
834 8a35f56c 2022-07-16 thomas repo_dir = NULL;
835 24240f6a 2022-11-23 thomas d_skipped++;
836 8a35f56c 2022-07-16 thomas continue;
839 8a35f56c 2022-07-16 thomas d_disp++;
840 8a35f56c 2022-07-16 thomas t->prev_disp++;
842 161663e7 2023-03-11 thomas r = gotweb_render_repo_fragment(c->tp, repo_dir);
843 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
844 ab5bda7e 2023-11-22 thomas repo_dir = NULL;
845 ab5bda7e 2023-11-22 thomas got_repo_close(t->repo);
846 ab5bda7e 2023-11-22 thomas t->repo = NULL;
847 161663e7 2023-03-11 thomas if (r == -1)
848 161663e7 2023-03-11 thomas return -1;
850 8a35f56c 2022-07-16 thomas t->next_disp++;
851 8a35f56c 2022-07-16 thomas if (d_disp == srv->max_repos_display)
854 161663e7 2023-03-11 thomas t->repos_total = t->nrepos - d_skipped;
856 c6c5c5fd 2023-12-30 thomas if (srv->max_repos_display == 0 ||
857 8a35f56c 2022-07-16 thomas t->repos_total <= srv->max_repos_display)
858 161663e7 2023-03-11 thomas return 0;
860 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
861 161663e7 2023-03-11 thomas return -1;
863 161663e7 2023-03-11 thomas return 0;
866 55144267 2022-09-07 thomas static inline int
867 55144267 2022-09-07 thomas should_urlencode(int c)
869 55144267 2022-09-07 thomas if (c <= ' ' || c >= 127)
870 55144267 2022-09-07 thomas return 1;
872 55144267 2022-09-07 thomas switch (c) {
873 55144267 2022-09-07 thomas /* gen-delim */
874 55144267 2022-09-07 thomas case ':':
875 55144267 2022-09-07 thomas case '/':
876 55144267 2022-09-07 thomas case '?':
877 55144267 2022-09-07 thomas case '#':
878 55144267 2022-09-07 thomas case '[':
879 55144267 2022-09-07 thomas case ']':
880 55144267 2022-09-07 thomas case '@':
881 55144267 2022-09-07 thomas /* sub-delims */
882 55144267 2022-09-07 thomas case '!':
883 55144267 2022-09-07 thomas case '$':
884 55144267 2022-09-07 thomas case '&':
885 55144267 2022-09-07 thomas case '\'':
886 55144267 2022-09-07 thomas case '(':
887 55144267 2022-09-07 thomas case ')':
888 55144267 2022-09-07 thomas case '*':
889 55144267 2022-09-07 thomas case '+':
890 55144267 2022-09-07 thomas case ',':
891 55144267 2022-09-07 thomas case ';':
892 55144267 2022-09-07 thomas case '=':
893 e1a9403a 2023-01-06 thomas /* needed because the URLs are embedded into the HTML */
894 e1a9403a 2023-01-06 thomas case '\"':
895 55144267 2022-09-07 thomas return 1;
897 55144267 2022-09-07 thomas return 0;
901 55144267 2022-09-07 thomas static char *
902 55144267 2022-09-07 thomas gotweb_urlencode(const char *str)
904 55144267 2022-09-07 thomas const char *s;
905 55144267 2022-09-07 thomas char *escaped;
906 55144267 2022-09-07 thomas size_t i, len;
907 55144267 2022-09-07 thomas int a, b;
910 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
912 55144267 2022-09-07 thomas if (should_urlencode(*s))
913 55144267 2022-09-07 thomas len += 2;
916 55144267 2022-09-07 thomas escaped = calloc(1, len + 1);
917 55144267 2022-09-07 thomas if (escaped == NULL)
918 55144267 2022-09-07 thomas return NULL;
921 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
922 55144267 2022-09-07 thomas if (should_urlencode(*s)) {
923 55144267 2022-09-07 thomas a = (*s & 0xF0) >> 4;
924 55144267 2022-09-07 thomas b = (*s & 0x0F);
926 55144267 2022-09-07 thomas escaped[i++] = '%';
927 55144267 2022-09-07 thomas escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
928 55144267 2022-09-07 thomas escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
930 55144267 2022-09-07 thomas escaped[i++] = *s;
933 55144267 2022-09-07 thomas return escaped;
936 e7e5fa49 2022-12-30 thomas const char *
937 e7e5fa49 2022-12-30 thomas gotweb_action_name(int action)
939 55144267 2022-09-07 thomas switch (action) {
940 55144267 2022-09-07 thomas case BLAME:
941 55144267 2022-09-07 thomas return "blame";
942 55144267 2022-09-07 thomas case BLOB:
943 55144267 2022-09-07 thomas return "blob";
944 b82440e1 2023-01-06 thomas case BLOBRAW:
945 b82440e1 2023-01-06 thomas return "blobraw";
946 55144267 2022-09-07 thomas case BRIEFS:
947 55144267 2022-09-07 thomas return "briefs";
948 55144267 2022-09-07 thomas case COMMITS:
949 55144267 2022-09-07 thomas return "commits";
950 55144267 2022-09-07 thomas case DIFF:
951 55144267 2022-09-07 thomas return "diff";
952 55144267 2022-09-07 thomas case ERR:
953 55144267 2022-09-07 thomas return "err";
954 55144267 2022-09-07 thomas case INDEX:
955 55144267 2022-09-07 thomas return "index";
956 feaddee6 2023-12-03 thomas case PATCH:
957 feaddee6 2023-12-03 thomas return "patch";
958 55144267 2022-09-07 thomas case SUMMARY:
959 55144267 2022-09-07 thomas return "summary";
960 55144267 2022-09-07 thomas case TAG:
961 55144267 2022-09-07 thomas return "tag";
962 55144267 2022-09-07 thomas case TAGS:
963 55144267 2022-09-07 thomas return "tags";
964 55144267 2022-09-07 thomas case TREE:
965 55144267 2022-09-07 thomas return "tree";
966 d6795e9f 2022-12-30 thomas case RSS:
967 d6795e9f 2022-12-30 thomas return "rss";
969 55144267 2022-09-07 thomas return NULL;
974 e7e5fa49 2022-12-30 thomas gotweb_render_url(struct request *c, struct gotweb_url *url)
976 55144267 2022-09-07 thomas const char *sep = "?", *action;
977 55144267 2022-09-07 thomas char *tmp;
980 e7e5fa49 2022-12-30 thomas action = gotweb_action_name(url->action);
981 55144267 2022-09-07 thomas if (action != NULL) {
982 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "?action=%s", action) == -1)
983 55144267 2022-09-07 thomas return -1;
984 55144267 2022-09-07 thomas sep = "&";
987 55144267 2022-09-07 thomas if (url->commit) {
988 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
989 55144267 2022-09-07 thomas return -1;
990 55144267 2022-09-07 thomas sep = "&";
993 55144267 2022-09-07 thomas if (url->previd) {
994 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
995 55144267 2022-09-07 thomas return -1;
996 55144267 2022-09-07 thomas sep = "&";
999 55144267 2022-09-07 thomas if (url->prevset) {
1000 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
1001 55144267 2022-09-07 thomas return -1;
1002 55144267 2022-09-07 thomas sep = "&";
1005 55144267 2022-09-07 thomas if (url->file) {
1006 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->file);
1007 55144267 2022-09-07 thomas if (tmp == NULL)
1008 55144267 2022-09-07 thomas return -1;
1009 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1010 55144267 2022-09-07 thomas free(tmp);
1011 55144267 2022-09-07 thomas if (r == -1)
1012 55144267 2022-09-07 thomas return -1;
1013 55144267 2022-09-07 thomas sep = "&";
1016 55144267 2022-09-07 thomas if (url->folder) {
1017 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->folder);
1018 55144267 2022-09-07 thomas if (tmp == NULL)
1019 55144267 2022-09-07 thomas return -1;
1020 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1021 55144267 2022-09-07 thomas free(tmp);
1022 55144267 2022-09-07 thomas if (r == -1)
1023 55144267 2022-09-07 thomas return -1;
1024 55144267 2022-09-07 thomas sep = "&";
1027 55144267 2022-09-07 thomas if (url->headref) {
1028 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->headref);
1029 55144267 2022-09-07 thomas if (tmp == NULL)
1030 55144267 2022-09-07 thomas return -1;
1031 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1032 55144267 2022-09-07 thomas free(tmp);
1033 55144267 2022-09-07 thomas if (r == -1)
1034 55144267 2022-09-07 thomas return -1;
1035 55144267 2022-09-07 thomas sep = "&";
1038 55144267 2022-09-07 thomas if (url->index_page != -1) {
1039 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%sindex_page=%d", sep,
1040 55144267 2022-09-07 thomas url->index_page) == -1)
1041 55144267 2022-09-07 thomas return -1;
1042 55144267 2022-09-07 thomas sep = "&";
1045 55144267 2022-09-07 thomas if (url->path) {
1046 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->path);
1047 55144267 2022-09-07 thomas if (tmp == NULL)
1048 55144267 2022-09-07 thomas return -1;
1049 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1050 55144267 2022-09-07 thomas free(tmp);
1051 55144267 2022-09-07 thomas if (r == -1)
1052 55144267 2022-09-07 thomas return -1;
1053 55144267 2022-09-07 thomas sep = "&";
1056 55144267 2022-09-07 thomas return 0;
1060 d6795e9f 2022-12-30 thomas gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1062 d6795e9f 2022-12-30 thomas struct template *tp = c->tp;
1063 d6795e9f 2022-12-30 thomas const char *proto = c->https ? "https" : "http";
1065 d8bf4f25 2023-09-14 thomas if (tp_writes(tp, proto) == -1 ||
1066 d8bf4f25 2023-09-14 thomas tp_writes(tp, "://") == -1 ||
1067 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->server_name) == -1 ||
1068 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->document_uri) == -1)
1069 d6795e9f 2022-12-30 thomas return -1;
1071 d6795e9f 2022-12-30 thomas return gotweb_render_url(c, url);
1074 8a35f56c 2022-07-16 thomas static const struct got_error *
1075 8a35f56c 2022-07-16 thomas gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1077 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1078 8a35f56c 2022-07-16 thomas struct socket *sock = c->sock;
1079 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1080 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1081 8a35f56c 2022-07-16 thomas DIR *dt;
1082 8a35f56c 2022-07-16 thomas char *dir_test;
1084 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1085 8a35f56c 2022-07-16 thomas GOTWEB_GIT_DIR) == -1)
1086 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1088 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1089 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1090 8a35f56c 2022-07-16 thomas free(dir_test);
1091 c13168e4 2023-12-01 thomas if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1092 c13168e4 2023-12-01 thomas repo_dir->name) == -1)
1093 c13168e4 2023-12-01 thomas return got_error_from_errno("asprintf");
1094 c13168e4 2023-12-01 thomas dt = opendir(dir_test);
1095 c13168e4 2023-12-01 thomas if (dt == NULL) {
1096 c13168e4 2023-12-01 thomas free(dir_test);
1097 c13168e4 2023-12-01 thomas return got_error_path(repo_dir->name,
1098 c13168e4 2023-12-01 thomas GOT_ERR_NOT_GIT_REPO);
1102 c13168e4 2023-12-01 thomas repo_dir->path = dir_test;
1103 c13168e4 2023-12-01 thomas dir_test = NULL;
1105 3991b2a5 2022-10-31 thomas if (srv->respect_exportok &&
1106 3991b2a5 2022-10-31 thomas faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1107 3991b2a5 2022-10-31 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1108 3991b2a5 2022-10-31 thomas goto err;
1111 ab5bda7e 2023-11-22 thomas error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1112 ab5bda7e 2023-11-22 thomas if (error)
1113 ab5bda7e 2023-11-22 thomas goto err;
1114 8a35f56c 2022-07-16 thomas error = gotweb_get_repo_description(&repo_dir->description, srv,
1115 4606e6d4 2022-11-23 thomas repo_dir->path, dirfd(dt));
1116 8a35f56c 2022-07-16 thomas if (error)
1117 8a35f56c 2022-07-16 thomas goto err;
1118 6c7f10f7 2022-11-23 thomas error = got_get_repo_owner(&repo_dir->owner, c);
1119 8a35f56c 2022-07-16 thomas if (error)
1120 8a35f56c 2022-07-16 thomas goto err;
1121 b38bef13 2023-08-12 thomas if (srv->show_repo_age) {
1122 b38bef13 2023-08-12 thomas error = got_get_repo_age(&repo_dir->age, c, NULL);
1123 b38bef13 2023-08-12 thomas if (error)
1124 b38bef13 2023-08-12 thomas goto err;
1126 4606e6d4 2022-11-23 thomas error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1127 4606e6d4 2022-11-23 thomas dirfd(dt));
1129 8a35f56c 2022-07-16 thomas free(dir_test);
1130 c2d3d9a0 2022-09-01 thomas if (dt != NULL && closedir(dt) == EOF && error == NULL)
1131 c2d3d9a0 2022-09-01 thomas error = got_error_from_errno("closedir");
1132 ab5bda7e 2023-11-22 thomas if (error && t->repo) {
1133 ab5bda7e 2023-11-22 thomas got_repo_close(t->repo);
1134 ab5bda7e 2023-11-22 thomas t->repo = NULL;
1136 8a35f56c 2022-07-16 thomas return error;
1139 8a35f56c 2022-07-16 thomas static const struct got_error *
1140 8a35f56c 2022-07-16 thomas gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1142 8a35f56c 2022-07-16 thomas const struct got_error *error;
1144 8a35f56c 2022-07-16 thomas *repo_dir = calloc(1, sizeof(**repo_dir));
1145 8a35f56c 2022-07-16 thomas if (*repo_dir == NULL)
1146 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
1148 8a35f56c 2022-07-16 thomas if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1149 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1150 8a35f56c 2022-07-16 thomas free(*repo_dir);
1151 8a35f56c 2022-07-16 thomas *repo_dir = NULL;
1152 8a35f56c 2022-07-16 thomas return error;
1154 8a35f56c 2022-07-16 thomas (*repo_dir)->owner = NULL;
1155 8a35f56c 2022-07-16 thomas (*repo_dir)->description = NULL;
1156 8a35f56c 2022-07-16 thomas (*repo_dir)->url = NULL;
1157 8a35f56c 2022-07-16 thomas (*repo_dir)->path = NULL;
1159 8a35f56c 2022-07-16 thomas return NULL;
1162 8a35f56c 2022-07-16 thomas static const struct got_error *
1163 4606e6d4 2022-11-23 thomas gotweb_get_repo_description(char **description, struct server *srv,
1164 4606e6d4 2022-11-23 thomas const char *dirpath, int dir)
1166 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1167 4606e6d4 2022-11-23 thomas struct stat sb;
1168 4606e6d4 2022-11-23 thomas int fd = -1;
1169 4606e6d4 2022-11-23 thomas off_t len;
1171 8a35f56c 2022-07-16 thomas *description = NULL;
1172 8a35f56c 2022-07-16 thomas if (srv->show_repo_description == 0)
1173 8a35f56c 2022-07-16 thomas return NULL;
1175 4606e6d4 2022-11-23 thomas fd = openat(dir, "description", O_RDONLY);
1176 4606e6d4 2022-11-23 thomas if (fd == -1) {
1177 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1178 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1179 4606e6d4 2022-11-23 thomas dirpath, "description");
1181 8a35f56c 2022-07-16 thomas goto done;
1184 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1185 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1186 4606e6d4 2022-11-23 thomas dirpath, "description");
1187 8a35f56c 2022-07-16 thomas goto done;
1190 4606e6d4 2022-11-23 thomas len = sb.st_size;
1191 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXDESCRSZ - 1)
1192 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXDESCRSZ - 1;
1194 8a35f56c 2022-07-16 thomas *description = calloc(len + 1, sizeof(**description));
1195 8a35f56c 2022-07-16 thomas if (*description == NULL) {
1196 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1197 8a35f56c 2022-07-16 thomas goto done;
1200 4606e6d4 2022-11-23 thomas if (read(fd, *description, len) == -1)
1201 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1203 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1204 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1205 8a35f56c 2022-07-16 thomas return error;
1208 8a35f56c 2022-07-16 thomas static const struct got_error *
1209 4606e6d4 2022-11-23 thomas gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1210 4606e6d4 2022-11-23 thomas int dir)
1212 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1213 4606e6d4 2022-11-23 thomas struct stat sb;
1214 4606e6d4 2022-11-23 thomas int fd = -1;
1215 4606e6d4 2022-11-23 thomas off_t len;
1217 8a35f56c 2022-07-16 thomas *url = NULL;
1218 8a35f56c 2022-07-16 thomas if (srv->show_repo_cloneurl == 0)
1219 8a35f56c 2022-07-16 thomas return NULL;
1221 4606e6d4 2022-11-23 thomas fd = openat(dir, "cloneurl", O_RDONLY);
1222 4606e6d4 2022-11-23 thomas if (fd == -1) {
1223 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1224 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1225 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1227 8a35f56c 2022-07-16 thomas goto done;
1230 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1231 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1232 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1233 8a35f56c 2022-07-16 thomas goto done;
1236 4606e6d4 2022-11-23 thomas len = sb.st_size;
1237 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1238 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXCLONEURLSZ - 1;
1240 8a35f56c 2022-07-16 thomas *url = calloc(len + 1, sizeof(**url));
1241 8a35f56c 2022-07-16 thomas if (*url == NULL) {
1242 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1243 8a35f56c 2022-07-16 thomas goto done;
1246 4606e6d4 2022-11-23 thomas if (read(fd, *url, len) == -1)
1247 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1249 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1250 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1251 8a35f56c 2022-07-16 thomas return error;
1255 4cc0851e 2023-10-08 thomas gotweb_render_age(struct template *tp, time_t committer_time)
1257 53bf32b8 2023-01-23 thomas struct request *c = tp->tp_arg;
1258 399ea8e4 2022-07-20 thomas long long diff_time;
1259 8a35f56c 2022-07-16 thomas const char *years = "years ago", *months = "months ago";
1260 8a35f56c 2022-07-16 thomas const char *weeks = "weeks ago", *days = "days ago";
1261 8a35f56c 2022-07-16 thomas const char *hours = "hours ago", *minutes = "minutes ago";
1262 8a35f56c 2022-07-16 thomas const char *seconds = "seconds ago", *now = "right now";
1264 4cc0851e 2023-10-08 thomas diff_time = time(NULL) - committer_time;
1265 4cc0851e 2023-10-08 thomas if (diff_time > 60 * 60 * 24 * 365 * 2) {
1266 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1267 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60 / 24 / 365), years) == -1)
1268 53bf32b8 2023-01-23 thomas return -1;
1269 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1270 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1271 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60 / 24 / (365 / 12)),
1272 4cc0851e 2023-10-08 thomas months) == -1)
1273 53bf32b8 2023-01-23 thomas return -1;
1274 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1275 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1276 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1277 53bf32b8 2023-01-23 thomas return -1;
1278 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 60 * 24 * 2) {
1279 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1280 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60 / 24), days) == -1)
1281 4cc0851e 2023-10-08 thomas return -1;
1282 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 60 * 2) {
1283 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1284 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60), hours) == -1)
1285 4cc0851e 2023-10-08 thomas return -1;
1286 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 2) {
1287 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1288 4cc0851e 2023-10-08 thomas minutes) == -1)
1289 4cc0851e 2023-10-08 thomas return -1;
1290 4cc0851e 2023-10-08 thomas } else if (diff_time > 2) {
1291 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s", diff_time,
1292 4cc0851e 2023-10-08 thomas seconds) == -1)
1293 4cc0851e 2023-10-08 thomas return -1;
1294 4cc0851e 2023-10-08 thomas } else {
1295 4cc0851e 2023-10-08 thomas if (tp_writes(tp, now) == -1)
1296 4cc0851e 2023-10-08 thomas return -1;
1298 53bf32b8 2023-01-23 thomas return 0;