Blame


1 8a35f56c 2022-07-16 thomas /*
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>
7 8a35f56c 2022-07-16 thomas *
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.
11 8a35f56c 2022-07-16 thomas *
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.
19 8a35f56c 2022-07-16 thomas */
20 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
21 8a35f56c 2022-07-16 thomas
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>
27 8a35f56c 2022-07-16 thomas
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>
38 8a35f56c 2022-07-16 thomas
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"
50 cb11302c 2022-12-30 thomas
51 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
52 d6795e9f 2022-12-30 thomas #include "tmpl.h"
53 ff36aeea 2022-07-16 thomas
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 },
62 8a35f56c 2022-07-16 thomas };
63 8a35f56c 2022-07-16 thomas
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 },
79 8a35f56c 2022-07-16 thomas };
80 8a35f56c 2022-07-16 thomas
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 **,
83 8a35f56c 2022-07-16 thomas char *);
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 f56b35c7 2024-04-29 thomas static const struct got_error *gotweb_load_got_path(struct repo_dir **,
88 f56b35c7 2024-04-29 thomas const char *, struct request *);
89 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_repo_description(char **,
90 4606e6d4 2022-11-23 thomas struct server *, const char *, int);
91 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_clone_url(char **, struct server *,
92 4606e6d4 2022-11-23 thomas const char *, int);
93 e7e5fa49 2022-12-30 thomas
94 8a35f56c 2022-07-16 thomas static void gotweb_free_querystring(struct querystring *);
95 8a35f56c 2022-07-16 thomas static void gotweb_free_repo_dir(struct repo_dir *);
96 8a35f56c 2022-07-16 thomas
97 b95d1cf6 2023-06-25 thomas struct server *gotweb_get_server(const char *);
98 8a35f56c 2022-07-16 thomas
99 50237485 2023-01-23 thomas static int
100 50237485 2023-01-23 thomas gotweb_reply(struct request *c, int status, const char *ctype,
101 50237485 2023-01-23 thomas struct gotweb_url *location)
102 50237485 2023-01-23 thomas {
103 50237485 2023-01-23 thomas const char *csp;
104 50237485 2023-01-23 thomas
105 d8bf4f25 2023-09-14 thomas if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
106 50237485 2023-01-23 thomas return -1;
107 50237485 2023-01-23 thomas
108 50237485 2023-01-23 thomas if (location) {
109 d8bf4f25 2023-09-14 thomas if (tp_writes(c->tp, "Location: ") == -1 ||
110 50237485 2023-01-23 thomas gotweb_render_url(c, location) == -1 ||
111 d8bf4f25 2023-09-14 thomas tp_writes(c->tp, "\r\n") == -1)
112 50237485 2023-01-23 thomas return -1;
113 50237485 2023-01-23 thomas }
114 50237485 2023-01-23 thomas
115 50237485 2023-01-23 thomas csp = "Content-Security-Policy: default-src 'self'; "
116 50237485 2023-01-23 thomas "script-src 'none'; object-src 'none';\r\n";
117 d8bf4f25 2023-09-14 thomas if (tp_writes(c->tp, csp) == -1)
118 50237485 2023-01-23 thomas return -1;
119 50237485 2023-01-23 thomas
120 d8bf4f25 2023-09-14 thomas if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
121 50237485 2023-01-23 thomas return -1;
122 50237485 2023-01-23 thomas
123 d8bf4f25 2023-09-14 thomas return tp_writes(c->tp, "\r\n");
124 50237485 2023-01-23 thomas }
125 50237485 2023-01-23 thomas
126 50237485 2023-01-23 thomas static int
127 50237485 2023-01-23 thomas gotweb_reply_file(struct request *c, const char *ctype, const char *file,
128 50237485 2023-01-23 thomas const char *suffix)
129 50237485 2023-01-23 thomas {
130 50237485 2023-01-23 thomas int r;
131 50237485 2023-01-23 thomas
132 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "Content-Disposition: attachment; "
133 50237485 2023-01-23 thomas "filename=%s%s\r\n", file, suffix ? suffix : "");
134 50237485 2023-01-23 thomas if (r == -1)
135 50237485 2023-01-23 thomas return -1;
136 50237485 2023-01-23 thomas return gotweb_reply(c, 200, ctype, NULL);
137 50237485 2023-01-23 thomas }
138 50237485 2023-01-23 thomas
139 8a35f56c 2022-07-16 thomas void
140 8a35f56c 2022-07-16 thomas gotweb_process_request(struct request *c)
141 8a35f56c 2022-07-16 thomas {
142 164b5ddc 2023-03-11 thomas const struct got_error *error = NULL;
143 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
144 8a35f56c 2022-07-16 thomas struct querystring *qs = NULL;
145 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
146 44f2135a 2023-04-01 thomas const char *rss_ctype = "application/rss+xml;charset=utf-8";
147 44f2135a 2023-04-01 thomas const uint8_t *buf;
148 44f2135a 2023-04-01 thomas size_t len;
149 44f2135a 2023-04-01 thomas int r, binary = 0;
150 18e466eb 2023-01-14 thomas
151 8a35f56c 2022-07-16 thomas /* init the transport */
152 8a35f56c 2022-07-16 thomas error = gotweb_init_transport(&c->t);
153 8a35f56c 2022-07-16 thomas if (error) {
154 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
155 46aeda9a 2022-08-27 thomas return;
156 8a35f56c 2022-07-16 thomas }
157 8a35f56c 2022-07-16 thomas /* don't process any further if client disconnected */
158 8a35f56c 2022-07-16 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
159 8a35f56c 2022-07-16 thomas return;
160 8a35f56c 2022-07-16 thomas /* get the gotwebd server */
161 b95d1cf6 2023-06-25 thomas srv = gotweb_get_server(c->server_name);
162 8a35f56c 2022-07-16 thomas if (srv == NULL) {
163 8a35f56c 2022-07-16 thomas log_warnx("%s: error server is NULL", __func__);
164 8a35f56c 2022-07-16 thomas goto err;
165 8a35f56c 2022-07-16 thomas }
166 8a35f56c 2022-07-16 thomas c->srv = srv;
167 8a35f56c 2022-07-16 thomas /* parse our querystring */
168 8a35f56c 2022-07-16 thomas error = gotweb_init_querystring(&qs);
169 8a35f56c 2022-07-16 thomas if (error) {
170 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
171 8a35f56c 2022-07-16 thomas goto err;
172 8a35f56c 2022-07-16 thomas }
173 8a35f56c 2022-07-16 thomas c->t->qs = qs;
174 8a35f56c 2022-07-16 thomas error = gotweb_parse_querystring(&qs, c->querystring);
175 8a35f56c 2022-07-16 thomas if (error) {
176 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
177 8a35f56c 2022-07-16 thomas goto err;
178 8a35f56c 2022-07-16 thomas }
179 8a35f56c 2022-07-16 thomas
180 8a35f56c 2022-07-16 thomas /*
181 8a35f56c 2022-07-16 thomas * certain actions require a commit id in the querystring. this stops
182 8a35f56c 2022-07-16 thomas * bad actors from exploiting this by manually manipulating the
183 8a35f56c 2022-07-16 thomas * querystring.
184 8a35f56c 2022-07-16 thomas */
185 8a35f56c 2022-07-16 thomas
186 b82440e1 2023-01-06 thomas if (qs->action == BLAME || qs->action == BLOB ||
187 feaddee6 2023-12-03 thomas qs->action == BLOBRAW || qs->action == DIFF ||
188 feaddee6 2023-12-03 thomas qs->action == PATCH) {
189 b82440e1 2023-01-06 thomas if (qs->commit == NULL) {
190 b2b363b2 2023-06-22 thomas error = got_error(GOT_ERR_BAD_QUERYSTRING);
191 164b5ddc 2023-03-11 thomas goto err;
192 b82440e1 2023-01-06 thomas }
193 8a35f56c 2022-07-16 thomas }
194 8a35f56c 2022-07-16 thomas
195 8a35f56c 2022-07-16 thomas if (qs->action != INDEX) {
196 f56b35c7 2024-04-29 thomas error = gotweb_load_got_path(&repo_dir, qs->path, c);
197 8a35f56c 2022-07-16 thomas c->t->repo_dir = repo_dir;
198 8a35f56c 2022-07-16 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
199 8a35f56c 2022-07-16 thomas goto err;
200 8a35f56c 2022-07-16 thomas }
201 8a35f56c 2022-07-16 thomas
202 44f2135a 2023-04-01 thomas if (qs->action == BLOBRAW || qs->action == BLOB) {
203 f03e50d6 2024-01-31 thomas if (qs->folder == NULL || qs->file == NULL) {
204 f03e50d6 2024-01-31 thomas error = got_error(GOT_ERR_BAD_QUERYSTRING);
205 f03e50d6 2024-01-31 thomas goto err;
206 f03e50d6 2024-01-31 thomas }
207 f03e50d6 2024-01-31 thomas
208 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
209 8a35f56c 2022-07-16 thomas if (error)
210 164b5ddc 2023-03-11 thomas goto err;
211 ea1b5cf5 2023-01-15 thomas
212 164b5ddc 2023-03-11 thomas error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
213 34875d49 2023-12-08 thomas &binary, c, qs->folder, qs->file, qs->commit);
214 164b5ddc 2023-03-11 thomas if (error)
215 164b5ddc 2023-03-11 thomas goto err;
216 44f2135a 2023-04-01 thomas }
217 44f2135a 2023-04-01 thomas
218 aea518b5 2023-11-22 thomas switch (qs->action) {
219 44f2135a 2023-04-01 thomas case BLAME:
220 f03e50d6 2024-01-31 thomas if (qs->folder == NULL || qs->file == NULL) {
221 f03e50d6 2024-01-31 thomas error = got_error(GOT_ERR_BAD_QUERYSTRING);
222 f03e50d6 2024-01-31 thomas goto err;
223 f03e50d6 2024-01-31 thomas }
224 44f2135a 2023-04-01 thomas error = got_get_repo_commits(c, 1);
225 44f2135a 2023-04-01 thomas if (error) {
226 44f2135a 2023-04-01 thomas log_warnx("%s: %s", __func__, error->msg);
227 44f2135a 2023-04-01 thomas goto err;
228 44f2135a 2023-04-01 thomas }
229 44f2135a 2023-04-01 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
230 44f2135a 2023-04-01 thomas return;
231 44f2135a 2023-04-01 thomas gotweb_render_page(c->tp, gotweb_render_blame);
232 44f2135a 2023-04-01 thomas return;
233 44f2135a 2023-04-01 thomas case BLOB:
234 44f2135a 2023-04-01 thomas if (binary) {
235 44f2135a 2023-04-01 thomas struct gotweb_url url = {
236 44f2135a 2023-04-01 thomas .index_page = -1,
237 44f2135a 2023-04-01 thomas .action = BLOBRAW,
238 44f2135a 2023-04-01 thomas .path = qs->path,
239 44f2135a 2023-04-01 thomas .commit = qs->commit,
240 44f2135a 2023-04-01 thomas .folder = qs->folder,
241 44f2135a 2023-04-01 thomas .file = qs->file,
242 44f2135a 2023-04-01 thomas };
243 ea1b5cf5 2023-01-15 thomas
244 44f2135a 2023-04-01 thomas gotweb_reply(c, 302, NULL, &url);
245 44f2135a 2023-04-01 thomas return;
246 44f2135a 2023-04-01 thomas }
247 44f2135a 2023-04-01 thomas
248 44f2135a 2023-04-01 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
249 44f2135a 2023-04-01 thomas return;
250 44f2135a 2023-04-01 thomas gotweb_render_page(c->tp, gotweb_render_blob);
251 44f2135a 2023-04-01 thomas return;
252 44f2135a 2023-04-01 thomas case BLOBRAW:
253 ea1b5cf5 2023-01-15 thomas if (binary)
254 50237485 2023-01-23 thomas r = gotweb_reply_file(c, "application/octet-stream",
255 50237485 2023-01-23 thomas qs->file, NULL);
256 ea1b5cf5 2023-01-15 thomas else
257 50237485 2023-01-23 thomas r = gotweb_reply(c, 200, "text/plain", NULL);
258 50237485 2023-01-23 thomas if (r == -1)
259 d8bf4f25 2023-09-14 thomas return;
260 d8bf4f25 2023-09-14 thomas if (template_flush(c->tp) == -1)
261 164b5ddc 2023-03-11 thomas return;
262 ea1b5cf5 2023-01-15 thomas
263 ea1b5cf5 2023-01-15 thomas for (;;) {
264 161663e7 2023-03-11 thomas error = got_object_blob_read_block(&len, c->t->blob);
265 ea1b5cf5 2023-01-15 thomas if (error)
266 164b5ddc 2023-03-11 thomas break;
267 ea1b5cf5 2023-01-15 thomas if (len == 0)
268 ea1b5cf5 2023-01-15 thomas break;
269 161663e7 2023-03-11 thomas buf = got_object_blob_get_read_buf(c->t->blob);
270 d8bf4f25 2023-09-14 thomas if (fcgi_write(c, buf, len) == -1)
271 164b5ddc 2023-03-11 thomas break;
272 b82440e1 2023-01-06 thomas }
273 164b5ddc 2023-03-11 thomas return;
274 8a35f56c 2022-07-16 thomas case BRIEFS:
275 b2251e6e 2023-06-26 thomas error = got_get_repo_commits(c, srv->max_commits_display);
276 b2251e6e 2023-06-26 thomas if (error)
277 b2251e6e 2023-06-26 thomas goto err;
278 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
279 164b5ddc 2023-03-11 thomas return;
280 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_briefs);
281 164b5ddc 2023-03-11 thomas return;
282 8a35f56c 2022-07-16 thomas case COMMITS:
283 7ade8b27 2022-12-30 thomas error = got_get_repo_commits(c, srv->max_commits_display);
284 8a35f56c 2022-07-16 thomas if (error) {
285 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
286 8a35f56c 2022-07-16 thomas goto err;
287 8a35f56c 2022-07-16 thomas }
288 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
289 164b5ddc 2023-03-11 thomas return;
290 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_commits);
291 164b5ddc 2023-03-11 thomas return;
292 8a35f56c 2022-07-16 thomas case DIFF:
293 dccd05b4 2023-01-10 thomas error = got_get_repo_commits(c, 1);
294 5d860bce 2023-01-07 thomas if (error) {
295 5d860bce 2023-01-07 thomas log_warnx("%s: %s", __func__, error->msg);
296 5d860bce 2023-01-07 thomas goto err;
297 5d860bce 2023-01-07 thomas }
298 04b2c111 2023-05-16 thomas error = got_open_diff_for_output(&c->t->fp, c);
299 dccd05b4 2023-01-10 thomas if (error) {
300 dccd05b4 2023-01-10 thomas log_warnx("%s: %s", __func__, error->msg);
301 dccd05b4 2023-01-10 thomas goto err;
302 dccd05b4 2023-01-10 thomas }
303 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
304 164b5ddc 2023-03-11 thomas return;
305 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_diff);
306 164b5ddc 2023-03-11 thomas return;
307 8a35f56c 2022-07-16 thomas case INDEX:
308 161663e7 2023-03-11 thomas c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
309 161663e7 2023-03-11 thomas alphasort);
310 161663e7 2023-03-11 thomas if (c->t->nrepos == -1) {
311 161663e7 2023-03-11 thomas c->t->repos = NULL;
312 161663e7 2023-03-11 thomas error = got_error_from_errno2("scandir",
313 161663e7 2023-03-11 thomas srv->repos_path);
314 8a35f56c 2022-07-16 thomas goto err;
315 8a35f56c 2022-07-16 thomas }
316 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
317 164b5ddc 2023-03-11 thomas return;
318 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_index);
319 164b5ddc 2023-03-11 thomas return;
320 feaddee6 2023-12-03 thomas case PATCH:
321 feaddee6 2023-12-03 thomas error = got_get_repo_commits(c, 1);
322 feaddee6 2023-12-03 thomas if (error) {
323 feaddee6 2023-12-03 thomas log_warnx("%s: %s", __func__, error->msg);
324 feaddee6 2023-12-03 thomas goto err;
325 feaddee6 2023-12-03 thomas }
326 feaddee6 2023-12-03 thomas error = got_open_diff_for_output(&c->t->fp, c);
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;
330 feaddee6 2023-12-03 thomas }
331 feaddee6 2023-12-03 thomas if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
332 feaddee6 2023-12-03 thomas return;
333 feaddee6 2023-12-03 thomas gotweb_render_patch(c->tp);
334 feaddee6 2023-12-03 thomas return;
335 44f2135a 2023-04-01 thomas case RSS:
336 44f2135a 2023-04-01 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
337 44f2135a 2023-04-01 thomas if (error)
338 44f2135a 2023-04-01 thomas goto err;
339 44f2135a 2023-04-01 thomas if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
340 44f2135a 2023-04-01 thomas == -1)
341 44f2135a 2023-04-01 thomas return;
342 44f2135a 2023-04-01 thomas gotweb_render_rss(c->tp);
343 44f2135a 2023-04-01 thomas return;
344 8a35f56c 2022-07-16 thomas case SUMMARY:
345 161663e7 2023-03-11 thomas error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
346 18e466eb 2023-01-14 thomas got_ref_cmp_by_name, NULL);
347 8a35f56c 2022-07-16 thomas if (error) {
348 18e466eb 2023-01-14 thomas log_warnx("%s: got_ref_list: %s", __func__,
349 18e466eb 2023-01-14 thomas error->msg);
350 8a35f56c 2022-07-16 thomas goto err;
351 8a35f56c 2022-07-16 thomas }
352 e85a14fa 2023-12-30 thomas error = got_get_repo_commits(c, srv->summary_commits_display);
353 b2251e6e 2023-06-26 thomas if (error)
354 b2251e6e 2023-06-26 thomas goto err;
355 18e466eb 2023-01-14 thomas qs->action = TAGS;
356 e85a14fa 2023-12-30 thomas error = got_get_repo_tags(c, srv->summary_tags_display);
357 18e466eb 2023-01-14 thomas if (error) {
358 18e466eb 2023-01-14 thomas log_warnx("%s: got_get_repo_tags: %s", __func__,
359 18e466eb 2023-01-14 thomas error->msg);
360 18e466eb 2023-01-14 thomas goto err;
361 18e466eb 2023-01-14 thomas }
362 18e466eb 2023-01-14 thomas qs->action = SUMMARY;
363 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
364 164b5ddc 2023-03-11 thomas return;
365 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_summary);
366 164b5ddc 2023-03-11 thomas return;
367 8a35f56c 2022-07-16 thomas case TAG:
368 145ca42a 2023-01-09 thomas error = got_get_repo_tags(c, 1);
369 8a35f56c 2022-07-16 thomas if (error) {
370 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
371 145ca42a 2023-01-09 thomas goto err;
372 145ca42a 2023-01-09 thomas }
373 145ca42a 2023-01-09 thomas if (c->t->tag_count == 0) {
374 145ca42a 2023-01-09 thomas error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
375 145ca42a 2023-01-09 thomas "bad commit id");
376 8a35f56c 2022-07-16 thomas goto err;
377 8a35f56c 2022-07-16 thomas }
378 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
379 164b5ddc 2023-03-11 thomas return;
380 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_tag);
381 164b5ddc 2023-03-11 thomas return;
382 8a35f56c 2022-07-16 thomas case TAGS:
383 b3ba36c3 2023-01-14 thomas error = got_get_repo_tags(c, srv->max_commits_display);
384 8a35f56c 2022-07-16 thomas if (error) {
385 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
386 8a35f56c 2022-07-16 thomas goto err;
387 8a35f56c 2022-07-16 thomas }
388 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
389 164b5ddc 2023-03-11 thomas return;
390 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_tags);
391 164b5ddc 2023-03-11 thomas return;
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;
397 8a35f56c 2022-07-16 thomas }
398 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 200, "text/html", NULL) == -1)
399 164b5ddc 2023-03-11 thomas return;
400 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_tree);
401 164b5ddc 2023-03-11 thomas return;
402 8a35f56c 2022-07-16 thomas case ERR:
403 8a35f56c 2022-07-16 thomas default:
404 164b5ddc 2023-03-11 thomas error = got_error(GOT_ERR_BAD_QUERYSTRING);
405 8a35f56c 2022-07-16 thomas }
406 8a35f56c 2022-07-16 thomas
407 8a35f56c 2022-07-16 thomas err:
408 164b5ddc 2023-03-11 thomas c->t->error = error;
409 164b5ddc 2023-03-11 thomas if (gotweb_reply(c, 400, "text/html", NULL) == -1)
410 8a35f56c 2022-07-16 thomas return;
411 164b5ddc 2023-03-11 thomas gotweb_render_page(c->tp, gotweb_render_error);
412 8a35f56c 2022-07-16 thomas }
413 8a35f56c 2022-07-16 thomas
414 8a35f56c 2022-07-16 thomas struct server *
415 b95d1cf6 2023-06-25 thomas gotweb_get_server(const char *server_name)
416 8a35f56c 2022-07-16 thomas {
417 b95d1cf6 2023-06-25 thomas struct server *srv;
418 8a35f56c 2022-07-16 thomas
419 3f0158b4 2022-08-30 thomas /* check against the server name first */
420 102d840d 2023-06-22 thomas if (*server_name != '\0')
421 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
422 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, server_name) == 0)
423 b95d1cf6 2023-06-25 thomas return srv;
424 8a35f56c 2022-07-16 thomas
425 b95d1cf6 2023-06-25 thomas /* otherwise, use the first server */
426 b95d1cf6 2023-06-25 thomas return TAILQ_FIRST(&gotwebd_env->servers);
427 8a35f56c 2022-07-16 thomas };
428 8a35f56c 2022-07-16 thomas
429 8a35f56c 2022-07-16 thomas const struct got_error *
430 8a35f56c 2022-07-16 thomas gotweb_init_transport(struct transport **t)
431 8a35f56c 2022-07-16 thomas {
432 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
433 8a35f56c 2022-07-16 thomas
434 8a35f56c 2022-07-16 thomas *t = calloc(1, sizeof(**t));
435 8a35f56c 2022-07-16 thomas if (*t == NULL)
436 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "calloc");
437 8a35f56c 2022-07-16 thomas
438 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_commits);
439 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_tags);
440 161663e7 2023-03-11 thomas TAILQ_INIT(&(*t)->refs);
441 161663e7 2023-03-11 thomas
442 161663e7 2023-03-11 thomas (*t)->fd = -1;
443 8a35f56c 2022-07-16 thomas
444 8a35f56c 2022-07-16 thomas return error;
445 8a35f56c 2022-07-16 thomas }
446 8a35f56c 2022-07-16 thomas
447 8a35f56c 2022-07-16 thomas static const struct got_error *
448 8a35f56c 2022-07-16 thomas gotweb_init_querystring(struct querystring **qs)
449 8a35f56c 2022-07-16 thomas {
450 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
451 8a35f56c 2022-07-16 thomas
452 8a35f56c 2022-07-16 thomas *qs = calloc(1, sizeof(**qs));
453 8a35f56c 2022-07-16 thomas if (*qs == NULL)
454 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "calloc");
455 8a35f56c 2022-07-16 thomas
456 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup("HEAD");
457 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
458 84630254 2022-09-02 thomas free(*qs);
459 84630254 2022-09-02 thomas *qs = NULL;
460 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "strdup");
461 8a35f56c 2022-07-16 thomas }
462 84630254 2022-09-02 thomas
463 84630254 2022-09-02 thomas (*qs)->action = INDEX;
464 8a35f56c 2022-07-16 thomas
465 8a35f56c 2022-07-16 thomas return error;
466 8a35f56c 2022-07-16 thomas }
467 8a35f56c 2022-07-16 thomas
468 8a35f56c 2022-07-16 thomas static const struct got_error *
469 8a35f56c 2022-07-16 thomas gotweb_parse_querystring(struct querystring **qs, char *qst)
470 8a35f56c 2022-07-16 thomas {
471 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
472 8a35f56c 2022-07-16 thomas char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
473 8a35f56c 2022-07-16 thomas char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
474 8a35f56c 2022-07-16 thomas
475 8a35f56c 2022-07-16 thomas if (qst == NULL)
476 8a35f56c 2022-07-16 thomas return error;
477 8a35f56c 2022-07-16 thomas
478 8a35f56c 2022-07-16 thomas tok1 = strdup(qst);
479 8a35f56c 2022-07-16 thomas if (tok1 == NULL)
480 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "strdup");
481 8a35f56c 2022-07-16 thomas
482 8a35f56c 2022-07-16 thomas tok1_pair = tok1;
483 8a35f56c 2022-07-16 thomas tok1_end = tok1;
484 8a35f56c 2022-07-16 thomas
485 8a35f56c 2022-07-16 thomas while (tok1_pair != NULL) {
486 8a35f56c 2022-07-16 thomas strsep(&tok1_end, "&");
487 8a35f56c 2022-07-16 thomas
488 8a35f56c 2022-07-16 thomas tok2 = strdup(tok1_pair);
489 8a35f56c 2022-07-16 thomas if (tok2 == NULL) {
490 8a35f56c 2022-07-16 thomas free(tok1);
491 d8edcc94 2023-06-01 thomas return got_error_from_errno2(__func__, "strdup");
492 8a35f56c 2022-07-16 thomas }
493 8a35f56c 2022-07-16 thomas
494 8a35f56c 2022-07-16 thomas tok2_pair = tok2;
495 8a35f56c 2022-07-16 thomas tok2_end = tok2;
496 8a35f56c 2022-07-16 thomas
497 8a35f56c 2022-07-16 thomas while (tok2_pair != NULL) {
498 8a35f56c 2022-07-16 thomas strsep(&tok2_end, "=");
499 8a35f56c 2022-07-16 thomas if (tok2_end) {
500 8a35f56c 2022-07-16 thomas error = gotweb_assign_querystring(qs, tok2_pair,
501 8a35f56c 2022-07-16 thomas tok2_end);
502 8a35f56c 2022-07-16 thomas if (error)
503 8a35f56c 2022-07-16 thomas goto err;
504 8a35f56c 2022-07-16 thomas }
505 8a35f56c 2022-07-16 thomas tok2_pair = tok2_end;
506 8a35f56c 2022-07-16 thomas }
507 8a35f56c 2022-07-16 thomas free(tok2);
508 8a35f56c 2022-07-16 thomas tok1_pair = tok1_end;
509 8a35f56c 2022-07-16 thomas }
510 8a35f56c 2022-07-16 thomas free(tok1);
511 8a35f56c 2022-07-16 thomas return error;
512 8a35f56c 2022-07-16 thomas err:
513 8a35f56c 2022-07-16 thomas free(tok2);
514 8a35f56c 2022-07-16 thomas free(tok1);
515 8a35f56c 2022-07-16 thomas return error;
516 8a35f56c 2022-07-16 thomas }
517 8a35f56c 2022-07-16 thomas
518 2ac684a4 2022-09-03 thomas /*
519 2ac684a4 2022-09-03 thomas * Adapted from usr.sbin/httpd/httpd.c url_decode.
520 2ac684a4 2022-09-03 thomas */
521 8a35f56c 2022-07-16 thomas static const struct got_error *
522 2ac684a4 2022-09-03 thomas gotweb_urldecode(char *url)
523 2ac684a4 2022-09-03 thomas {
524 2ac684a4 2022-09-03 thomas char *p, *q;
525 2ac684a4 2022-09-03 thomas char hex[3];
526 2ac684a4 2022-09-03 thomas unsigned long x;
527 2ac684a4 2022-09-03 thomas
528 2ac684a4 2022-09-03 thomas hex[2] = '\0';
529 2ac684a4 2022-09-03 thomas p = q = url;
530 2ac684a4 2022-09-03 thomas
531 2ac684a4 2022-09-03 thomas while (*p != '\0') {
532 2ac684a4 2022-09-03 thomas switch (*p) {
533 2ac684a4 2022-09-03 thomas case '%':
534 2ac684a4 2022-09-03 thomas /* Encoding character is followed by two hex chars */
535 2ac684a4 2022-09-03 thomas if (!isxdigit((unsigned char)p[1]) ||
536 2ac684a4 2022-09-03 thomas !isxdigit((unsigned char)p[2]) ||
537 2ac684a4 2022-09-03 thomas (p[1] == '0' && p[2] == '0'))
538 2ac684a4 2022-09-03 thomas return got_error(GOT_ERR_BAD_QUERYSTRING);
539 2ac684a4 2022-09-03 thomas
540 2ac684a4 2022-09-03 thomas hex[0] = p[1];
541 2ac684a4 2022-09-03 thomas hex[1] = p[2];
542 2ac684a4 2022-09-03 thomas
543 2ac684a4 2022-09-03 thomas /*
544 2ac684a4 2022-09-03 thomas * We don't have to validate "hex" because it is
545 2ac684a4 2022-09-03 thomas * guaranteed to include two hex chars followed by nul.
546 2ac684a4 2022-09-03 thomas */
547 2ac684a4 2022-09-03 thomas x = strtoul(hex, NULL, 16);
548 2ac684a4 2022-09-03 thomas *q = (char)x;
549 2ac684a4 2022-09-03 thomas p += 2;
550 2ac684a4 2022-09-03 thomas break;
551 2ac684a4 2022-09-03 thomas default:
552 2ac684a4 2022-09-03 thomas *q = *p;
553 2ac684a4 2022-09-03 thomas break;
554 2ac684a4 2022-09-03 thomas }
555 2ac684a4 2022-09-03 thomas p++;
556 2ac684a4 2022-09-03 thomas q++;
557 2ac684a4 2022-09-03 thomas }
558 2ac684a4 2022-09-03 thomas *q = '\0';
559 2ac684a4 2022-09-03 thomas
560 2ac684a4 2022-09-03 thomas return NULL;
561 2ac684a4 2022-09-03 thomas }
562 2ac684a4 2022-09-03 thomas
563 2ac684a4 2022-09-03 thomas static const struct got_error *
564 8a35f56c 2022-07-16 thomas gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
565 8a35f56c 2022-07-16 thomas {
566 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
567 8a35f56c 2022-07-16 thomas const char *errstr;
568 8a35f56c 2022-07-16 thomas int a_cnt, el_cnt;
569 8a35f56c 2022-07-16 thomas
570 2ac684a4 2022-09-03 thomas error = gotweb_urldecode(value);
571 2ac684a4 2022-09-03 thomas if (error)
572 2ac684a4 2022-09-03 thomas return error;
573 2ac684a4 2022-09-03 thomas
574 b38e9a2a 2023-11-22 thomas for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
575 8a35f56c 2022-07-16 thomas if (strcmp(key, querystring_keys[el_cnt].name) != 0)
576 8a35f56c 2022-07-16 thomas continue;
577 8a35f56c 2022-07-16 thomas
578 8a35f56c 2022-07-16 thomas switch (querystring_keys[el_cnt].element) {
579 8a35f56c 2022-07-16 thomas case ACTION:
580 cd292c73 2023-12-18 thomas for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
581 8a35f56c 2022-07-16 thomas if (strcmp(value, action_keys[a_cnt].name) != 0)
582 8a35f56c 2022-07-16 thomas continue;
583 8a35f56c 2022-07-16 thomas else if (strcmp(value,
584 8a35f56c 2022-07-16 thomas action_keys[a_cnt].name) == 0){
585 8a35f56c 2022-07-16 thomas (*qs)->action =
586 8a35f56c 2022-07-16 thomas action_keys[a_cnt].action;
587 8a35f56c 2022-07-16 thomas goto qa_found;
588 8a35f56c 2022-07-16 thomas }
589 8a35f56c 2022-07-16 thomas }
590 8a35f56c 2022-07-16 thomas (*qs)->action = ERR;
591 8a35f56c 2022-07-16 thomas qa_found:
592 8a35f56c 2022-07-16 thomas break;
593 8a35f56c 2022-07-16 thomas case COMMIT:
594 8a35f56c 2022-07-16 thomas (*qs)->commit = strdup(value);
595 8a35f56c 2022-07-16 thomas if ((*qs)->commit == NULL) {
596 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
597 d8edcc94 2023-06-01 thomas "strdup");
598 8a35f56c 2022-07-16 thomas goto done;
599 8a35f56c 2022-07-16 thomas }
600 8a35f56c 2022-07-16 thomas break;
601 8a35f56c 2022-07-16 thomas case RFILE:
602 8a35f56c 2022-07-16 thomas (*qs)->file = strdup(value);
603 8a35f56c 2022-07-16 thomas if ((*qs)->file == NULL) {
604 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
605 d8edcc94 2023-06-01 thomas "strdup");
606 8a35f56c 2022-07-16 thomas goto done;
607 8a35f56c 2022-07-16 thomas }
608 8a35f56c 2022-07-16 thomas break;
609 8a35f56c 2022-07-16 thomas case FOLDER:
610 8a35f56c 2022-07-16 thomas (*qs)->folder = strdup(value);
611 8a35f56c 2022-07-16 thomas if ((*qs)->folder == NULL) {
612 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
613 d8edcc94 2023-06-01 thomas "strdup");
614 8a35f56c 2022-07-16 thomas goto done;
615 8a35f56c 2022-07-16 thomas }
616 8a35f56c 2022-07-16 thomas break;
617 8a35f56c 2022-07-16 thomas case HEADREF:
618 b0764984 2022-09-02 thomas free((*qs)->headref);
619 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup(value);
620 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
621 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
622 d8edcc94 2023-06-01 thomas "strdup");
623 8a35f56c 2022-07-16 thomas goto done;
624 8a35f56c 2022-07-16 thomas }
625 8a35f56c 2022-07-16 thomas break;
626 8a35f56c 2022-07-16 thomas case INDEX_PAGE:
627 102d840d 2023-06-22 thomas if (*value == '\0')
628 8a35f56c 2022-07-16 thomas break;
629 8a35f56c 2022-07-16 thomas (*qs)->index_page = strtonum(value, INT64_MIN,
630 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
631 8a35f56c 2022-07-16 thomas if (errstr) {
632 d8edcc94 2023-06-01 thomas error = got_error_from_errno3(__func__,
633 d8edcc94 2023-06-01 thomas "strtonum", errstr);
634 8a35f56c 2022-07-16 thomas goto done;
635 8a35f56c 2022-07-16 thomas }
636 3d6d1fb0 2022-12-30 thomas if ((*qs)->index_page < 0)
637 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
638 8a35f56c 2022-07-16 thomas break;
639 8a35f56c 2022-07-16 thomas case PATH:
640 8a35f56c 2022-07-16 thomas (*qs)->path = strdup(value);
641 8a35f56c 2022-07-16 thomas if ((*qs)->path == NULL) {
642 d8edcc94 2023-06-01 thomas error = got_error_from_errno2(__func__,
643 d8edcc94 2023-06-01 thomas "strdup");
644 8a35f56c 2022-07-16 thomas goto done;
645 8a35f56c 2022-07-16 thomas }
646 8a35f56c 2022-07-16 thomas break;
647 8a35f56c 2022-07-16 thomas }
648 b38e9a2a 2023-11-22 thomas
649 b38e9a2a 2023-11-22 thomas /* entry found */
650 b38e9a2a 2023-11-22 thomas break;
651 8a35f56c 2022-07-16 thomas }
652 8a35f56c 2022-07-16 thomas done:
653 8a35f56c 2022-07-16 thomas return error;
654 8a35f56c 2022-07-16 thomas }
655 8a35f56c 2022-07-16 thomas
656 8a35f56c 2022-07-16 thomas void
657 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(struct repo_tag *rt)
658 8a35f56c 2022-07-16 thomas {
659 8a35f56c 2022-07-16 thomas if (rt != NULL) {
660 8a35f56c 2022-07-16 thomas free(rt->commit_id);
661 217813df 2022-09-02 thomas free(rt->tag_name);
662 217813df 2022-09-02 thomas free(rt->tag_commit);
663 217813df 2022-09-02 thomas free(rt->commit_msg);
664 8a35f56c 2022-07-16 thomas free(rt->tagger);
665 8a35f56c 2022-07-16 thomas }
666 8a35f56c 2022-07-16 thomas free(rt);
667 8a35f56c 2022-07-16 thomas }
668 8a35f56c 2022-07-16 thomas
669 8a35f56c 2022-07-16 thomas void
670 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(struct repo_commit *rc)
671 8a35f56c 2022-07-16 thomas {
672 8a35f56c 2022-07-16 thomas if (rc != NULL) {
673 8a35f56c 2022-07-16 thomas free(rc->path);
674 8a35f56c 2022-07-16 thomas free(rc->refs_str);
675 8a35f56c 2022-07-16 thomas free(rc->commit_id);
676 8a35f56c 2022-07-16 thomas free(rc->parent_id);
677 8a35f56c 2022-07-16 thomas free(rc->tree_id);
678 8a35f56c 2022-07-16 thomas free(rc->author);
679 8a35f56c 2022-07-16 thomas free(rc->committer);
680 8a35f56c 2022-07-16 thomas free(rc->commit_msg);
681 8a35f56c 2022-07-16 thomas }
682 8a35f56c 2022-07-16 thomas free(rc);
683 8a35f56c 2022-07-16 thomas }
684 8a35f56c 2022-07-16 thomas
685 8a35f56c 2022-07-16 thomas static void
686 8a35f56c 2022-07-16 thomas gotweb_free_querystring(struct querystring *qs)
687 8a35f56c 2022-07-16 thomas {
688 8a35f56c 2022-07-16 thomas if (qs != NULL) {
689 8a35f56c 2022-07-16 thomas free(qs->commit);
690 8a35f56c 2022-07-16 thomas free(qs->file);
691 8a35f56c 2022-07-16 thomas free(qs->folder);
692 8a35f56c 2022-07-16 thomas free(qs->headref);
693 8a35f56c 2022-07-16 thomas free(qs->path);
694 8a35f56c 2022-07-16 thomas }
695 8a35f56c 2022-07-16 thomas free(qs);
696 8a35f56c 2022-07-16 thomas }
697 8a35f56c 2022-07-16 thomas
698 8a35f56c 2022-07-16 thomas static void
699 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(struct repo_dir *repo_dir)
700 8a35f56c 2022-07-16 thomas {
701 8a35f56c 2022-07-16 thomas if (repo_dir != NULL) {
702 8a35f56c 2022-07-16 thomas free(repo_dir->name);
703 8a35f56c 2022-07-16 thomas free(repo_dir->owner);
704 8a35f56c 2022-07-16 thomas free(repo_dir->description);
705 8a35f56c 2022-07-16 thomas free(repo_dir->url);
706 8a35f56c 2022-07-16 thomas free(repo_dir->path);
707 8a35f56c 2022-07-16 thomas }
708 8a35f56c 2022-07-16 thomas free(repo_dir);
709 8a35f56c 2022-07-16 thomas }
710 8a35f56c 2022-07-16 thomas
711 8a35f56c 2022-07-16 thomas void
712 8a35f56c 2022-07-16 thomas gotweb_free_transport(struct transport *t)
713 8a35f56c 2022-07-16 thomas {
714 161663e7 2023-03-11 thomas const struct got_error *err;
715 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL, *trc = NULL;
716 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
717 161663e7 2023-03-11 thomas int i;
718 8a35f56c 2022-07-16 thomas
719 161663e7 2023-03-11 thomas got_ref_list_free(&t->refs);
720 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
721 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_commits, rc, entry);
722 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(rc);
723 8a35f56c 2022-07-16 thomas }
724 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
725 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
726 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
727 8a35f56c 2022-07-16 thomas }
728 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(t->repo_dir);
729 8a35f56c 2022-07-16 thomas gotweb_free_querystring(t->qs);
730 20bab626 2023-02-03 thomas free(t->more_id);
731 978394e7 2023-12-08 thomas free(t->tags_more_id);
732 161663e7 2023-03-11 thomas if (t->blob)
733 161663e7 2023-03-11 thomas got_object_blob_close(t->blob);
734 161663e7 2023-03-11 thomas if (t->fp) {
735 d00235d8 2023-05-17 thomas err = got_gotweb_closefile(t->fp);
736 161663e7 2023-03-11 thomas if (err)
737 d00235d8 2023-05-17 thomas log_warnx("%s: got_gotweb_closefile failure: %s",
738 161663e7 2023-03-11 thomas __func__, err->msg);
739 161663e7 2023-03-11 thomas }
740 54ca6e2b 2023-05-16 thomas if (t->fd != -1 && close(t->fd) == -1)
741 54ca6e2b 2023-05-16 thomas log_warn("%s: close", __func__);
742 161663e7 2023-03-11 thomas if (t->repos) {
743 161663e7 2023-03-11 thomas for (i = 0; i < t->nrepos; ++i)
744 161663e7 2023-03-11 thomas free(t->repos[i]);
745 161663e7 2023-03-11 thomas free(t->repos);
746 161663e7 2023-03-11 thomas }
747 ab5bda7e 2023-11-22 thomas if (t->repo)
748 ab5bda7e 2023-11-22 thomas got_repo_close(t->repo);
749 8a35f56c 2022-07-16 thomas free(t);
750 8a35f56c 2022-07-16 thomas }
751 8a35f56c 2022-07-16 thomas
752 2f4f0731 2022-12-30 thomas void
753 978394e7 2023-12-08 thomas gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
754 2f4f0731 2022-12-30 thomas struct gotweb_url *next, int *have_next)
755 8a35f56c 2022-07-16 thomas {
756 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
757 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
758 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
759 8a35f56c 2022-07-16 thomas
760 2f4f0731 2022-12-30 thomas *have_prev = *have_next = 0;
761 8a35f56c 2022-07-16 thomas
762 978394e7 2023-12-08 thomas if (qs->index_page > 0) {
763 978394e7 2023-12-08 thomas *have_prev = 1;
764 978394e7 2023-12-08 thomas *prev = (struct gotweb_url){
765 978394e7 2023-12-08 thomas .action = -1,
766 978394e7 2023-12-08 thomas .index_page = qs->index_page - 1,
767 978394e7 2023-12-08 thomas };
768 978394e7 2023-12-08 thomas }
769 978394e7 2023-12-08 thomas if (t->next_disp == srv->max_repos_display &&
770 978394e7 2023-12-08 thomas t->repos_total != (qs->index_page + 1) *
771 978394e7 2023-12-08 thomas srv->max_repos_display) {
772 978394e7 2023-12-08 thomas *have_next = 1;
773 978394e7 2023-12-08 thomas *next = (struct gotweb_url){
774 978394e7 2023-12-08 thomas .action = -1,
775 978394e7 2023-12-08 thomas .index_page = qs->index_page + 1,
776 978394e7 2023-12-08 thomas };
777 8a35f56c 2022-07-16 thomas }
778 8a35f56c 2022-07-16 thomas }
779 8a35f56c 2022-07-16 thomas
780 161663e7 2023-03-11 thomas static int
781 161663e7 2023-03-11 thomas gotweb_render_index(struct template *tp)
782 8a35f56c 2022-07-16 thomas {
783 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
784 161663e7 2023-03-11 thomas struct request *c = tp->tp_arg;
785 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
786 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
787 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
788 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
789 161663e7 2023-03-11 thomas struct dirent **sd_dent = t->repos;
790 161663e7 2023-03-11 thomas unsigned int d_i, d_disp = 0;
791 24240f6a 2022-11-23 thomas unsigned int d_skipped = 0;
792 161663e7 2023-03-11 thomas int type, r;
793 8a35f56c 2022-07-16 thomas
794 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_table_hdr(c->tp) == -1)
795 161663e7 2023-03-11 thomas return -1;
796 79393471 2022-08-27 thomas
797 161663e7 2023-03-11 thomas for (d_i = 0; d_i < t->nrepos; d_i++) {
798 8a35f56c 2022-07-16 thomas if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
799 24240f6a 2022-11-23 thomas strcmp(sd_dent[d_i]->d_name, "..") == 0) {
800 24240f6a 2022-11-23 thomas d_skipped++;
801 24240f6a 2022-11-23 thomas continue;
802 24240f6a 2022-11-23 thomas }
803 24240f6a 2022-11-23 thomas
804 24240f6a 2022-11-23 thomas error = got_path_dirent_type(&type, srv->repos_path,
805 24240f6a 2022-11-23 thomas sd_dent[d_i]);
806 24240f6a 2022-11-23 thomas if (error)
807 161663e7 2023-03-11 thomas continue;
808 24240f6a 2022-11-23 thomas if (type != DT_DIR) {
809 24240f6a 2022-11-23 thomas d_skipped++;
810 8a35f56c 2022-07-16 thomas continue;
811 24240f6a 2022-11-23 thomas }
812 8a35f56c 2022-07-16 thomas
813 8a35f56c 2022-07-16 thomas if (qs->index_page > 0 && (qs->index_page *
814 8a35f56c 2022-07-16 thomas srv->max_repos_display) > t->prev_disp) {
815 8a35f56c 2022-07-16 thomas t->prev_disp++;
816 8a35f56c 2022-07-16 thomas continue;
817 8a35f56c 2022-07-16 thomas }
818 8a35f56c 2022-07-16 thomas
819 f56b35c7 2024-04-29 thomas error = gotweb_load_got_path(&repo_dir, sd_dent[d_i]->d_name,
820 f56b35c7 2024-04-29 thomas c);
821 f5af6dbd 2023-04-29 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
822 161663e7 2023-03-11 thomas if (error->code != GOT_ERR_NOT_GIT_REPO)
823 161663e7 2023-03-11 thomas log_warnx("%s: %s: %s", __func__,
824 161663e7 2023-03-11 thomas sd_dent[d_i]->d_name, error->msg);
825 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
826 8a35f56c 2022-07-16 thomas repo_dir = NULL;
827 24240f6a 2022-11-23 thomas d_skipped++;
828 8a35f56c 2022-07-16 thomas continue;
829 8a35f56c 2022-07-16 thomas }
830 24240f6a 2022-11-23 thomas
831 8a35f56c 2022-07-16 thomas d_disp++;
832 8a35f56c 2022-07-16 thomas t->prev_disp++;
833 8a35f56c 2022-07-16 thomas
834 161663e7 2023-03-11 thomas r = gotweb_render_repo_fragment(c->tp, repo_dir);
835 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
836 ab5bda7e 2023-11-22 thomas repo_dir = NULL;
837 ab5bda7e 2023-11-22 thomas got_repo_close(t->repo);
838 ab5bda7e 2023-11-22 thomas t->repo = NULL;
839 161663e7 2023-03-11 thomas if (r == -1)
840 161663e7 2023-03-11 thomas return -1;
841 161663e7 2023-03-11 thomas
842 8a35f56c 2022-07-16 thomas t->next_disp++;
843 8a35f56c 2022-07-16 thomas if (d_disp == srv->max_repos_display)
844 8a35f56c 2022-07-16 thomas break;
845 8a35f56c 2022-07-16 thomas }
846 161663e7 2023-03-11 thomas t->repos_total = t->nrepos - d_skipped;
847 24240f6a 2022-11-23 thomas
848 c6c5c5fd 2023-12-30 thomas if (srv->max_repos_display == 0 ||
849 8a35f56c 2022-07-16 thomas t->repos_total <= srv->max_repos_display)
850 161663e7 2023-03-11 thomas return 0;
851 8a35f56c 2022-07-16 thomas
852 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
853 161663e7 2023-03-11 thomas return -1;
854 161663e7 2023-03-11 thomas
855 161663e7 2023-03-11 thomas return 0;
856 8a35f56c 2022-07-16 thomas }
857 8a35f56c 2022-07-16 thomas
858 55144267 2022-09-07 thomas static inline int
859 55144267 2022-09-07 thomas should_urlencode(int c)
860 55144267 2022-09-07 thomas {
861 55144267 2022-09-07 thomas if (c <= ' ' || c >= 127)
862 55144267 2022-09-07 thomas return 1;
863 55144267 2022-09-07 thomas
864 55144267 2022-09-07 thomas switch (c) {
865 55144267 2022-09-07 thomas /* gen-delim */
866 55144267 2022-09-07 thomas case ':':
867 55144267 2022-09-07 thomas case '/':
868 55144267 2022-09-07 thomas case '?':
869 55144267 2022-09-07 thomas case '#':
870 55144267 2022-09-07 thomas case '[':
871 55144267 2022-09-07 thomas case ']':
872 55144267 2022-09-07 thomas case '@':
873 55144267 2022-09-07 thomas /* sub-delims */
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 case '+':
882 55144267 2022-09-07 thomas case ',':
883 55144267 2022-09-07 thomas case ';':
884 55144267 2022-09-07 thomas case '=':
885 e1a9403a 2023-01-06 thomas /* needed because the URLs are embedded into the HTML */
886 e1a9403a 2023-01-06 thomas case '\"':
887 55144267 2022-09-07 thomas return 1;
888 55144267 2022-09-07 thomas default:
889 55144267 2022-09-07 thomas return 0;
890 55144267 2022-09-07 thomas }
891 55144267 2022-09-07 thomas }
892 55144267 2022-09-07 thomas
893 55144267 2022-09-07 thomas static char *
894 55144267 2022-09-07 thomas gotweb_urlencode(const char *str)
895 55144267 2022-09-07 thomas {
896 55144267 2022-09-07 thomas const char *s;
897 55144267 2022-09-07 thomas char *escaped;
898 55144267 2022-09-07 thomas size_t i, len;
899 55144267 2022-09-07 thomas int a, b;
900 55144267 2022-09-07 thomas
901 55144267 2022-09-07 thomas len = 0;
902 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
903 55144267 2022-09-07 thomas len++;
904 55144267 2022-09-07 thomas if (should_urlencode(*s))
905 55144267 2022-09-07 thomas len += 2;
906 55144267 2022-09-07 thomas }
907 55144267 2022-09-07 thomas
908 55144267 2022-09-07 thomas escaped = calloc(1, len + 1);
909 55144267 2022-09-07 thomas if (escaped == NULL)
910 55144267 2022-09-07 thomas return NULL;
911 55144267 2022-09-07 thomas
912 55144267 2022-09-07 thomas i = 0;
913 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
914 55144267 2022-09-07 thomas if (should_urlencode(*s)) {
915 55144267 2022-09-07 thomas a = (*s & 0xF0) >> 4;
916 55144267 2022-09-07 thomas b = (*s & 0x0F);
917 55144267 2022-09-07 thomas
918 55144267 2022-09-07 thomas escaped[i++] = '%';
919 55144267 2022-09-07 thomas escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
920 55144267 2022-09-07 thomas escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
921 55144267 2022-09-07 thomas } else
922 55144267 2022-09-07 thomas escaped[i++] = *s;
923 55144267 2022-09-07 thomas }
924 55144267 2022-09-07 thomas
925 55144267 2022-09-07 thomas return escaped;
926 55144267 2022-09-07 thomas }
927 55144267 2022-09-07 thomas
928 e7e5fa49 2022-12-30 thomas const char *
929 e7e5fa49 2022-12-30 thomas gotweb_action_name(int action)
930 55144267 2022-09-07 thomas {
931 55144267 2022-09-07 thomas switch (action) {
932 55144267 2022-09-07 thomas case BLAME:
933 55144267 2022-09-07 thomas return "blame";
934 55144267 2022-09-07 thomas case BLOB:
935 55144267 2022-09-07 thomas return "blob";
936 b82440e1 2023-01-06 thomas case BLOBRAW:
937 b82440e1 2023-01-06 thomas return "blobraw";
938 55144267 2022-09-07 thomas case BRIEFS:
939 55144267 2022-09-07 thomas return "briefs";
940 55144267 2022-09-07 thomas case COMMITS:
941 55144267 2022-09-07 thomas return "commits";
942 55144267 2022-09-07 thomas case DIFF:
943 55144267 2022-09-07 thomas return "diff";
944 55144267 2022-09-07 thomas case ERR:
945 55144267 2022-09-07 thomas return "err";
946 55144267 2022-09-07 thomas case INDEX:
947 55144267 2022-09-07 thomas return "index";
948 feaddee6 2023-12-03 thomas case PATCH:
949 feaddee6 2023-12-03 thomas return "patch";
950 55144267 2022-09-07 thomas case SUMMARY:
951 55144267 2022-09-07 thomas return "summary";
952 55144267 2022-09-07 thomas case TAG:
953 55144267 2022-09-07 thomas return "tag";
954 55144267 2022-09-07 thomas case TAGS:
955 55144267 2022-09-07 thomas return "tags";
956 55144267 2022-09-07 thomas case TREE:
957 55144267 2022-09-07 thomas return "tree";
958 d6795e9f 2022-12-30 thomas case RSS:
959 d6795e9f 2022-12-30 thomas return "rss";
960 55144267 2022-09-07 thomas default:
961 55144267 2022-09-07 thomas return NULL;
962 55144267 2022-09-07 thomas }
963 55144267 2022-09-07 thomas }
964 55144267 2022-09-07 thomas
965 e7e5fa49 2022-12-30 thomas int
966 e7e5fa49 2022-12-30 thomas gotweb_render_url(struct request *c, struct gotweb_url *url)
967 55144267 2022-09-07 thomas {
968 55144267 2022-09-07 thomas const char *sep = "?", *action;
969 55144267 2022-09-07 thomas char *tmp;
970 55144267 2022-09-07 thomas int r;
971 55144267 2022-09-07 thomas
972 e7e5fa49 2022-12-30 thomas action = gotweb_action_name(url->action);
973 55144267 2022-09-07 thomas if (action != NULL) {
974 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "?action=%s", action) == -1)
975 55144267 2022-09-07 thomas return -1;
976 55144267 2022-09-07 thomas sep = "&";
977 55144267 2022-09-07 thomas }
978 55144267 2022-09-07 thomas
979 55144267 2022-09-07 thomas if (url->commit) {
980 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
981 55144267 2022-09-07 thomas return -1;
982 55144267 2022-09-07 thomas sep = "&";
983 55144267 2022-09-07 thomas }
984 55144267 2022-09-07 thomas
985 55144267 2022-09-07 thomas if (url->previd) {
986 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
987 55144267 2022-09-07 thomas return -1;
988 55144267 2022-09-07 thomas sep = "&";
989 55144267 2022-09-07 thomas }
990 55144267 2022-09-07 thomas
991 55144267 2022-09-07 thomas if (url->prevset) {
992 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
993 55144267 2022-09-07 thomas return -1;
994 55144267 2022-09-07 thomas sep = "&";
995 55144267 2022-09-07 thomas }
996 55144267 2022-09-07 thomas
997 55144267 2022-09-07 thomas if (url->file) {
998 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->file);
999 55144267 2022-09-07 thomas if (tmp == NULL)
1000 55144267 2022-09-07 thomas return -1;
1001 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1002 55144267 2022-09-07 thomas free(tmp);
1003 55144267 2022-09-07 thomas if (r == -1)
1004 55144267 2022-09-07 thomas return -1;
1005 55144267 2022-09-07 thomas sep = "&";
1006 55144267 2022-09-07 thomas }
1007 55144267 2022-09-07 thomas
1008 55144267 2022-09-07 thomas if (url->folder) {
1009 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->folder);
1010 55144267 2022-09-07 thomas if (tmp == NULL)
1011 55144267 2022-09-07 thomas return -1;
1012 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1013 55144267 2022-09-07 thomas free(tmp);
1014 55144267 2022-09-07 thomas if (r == -1)
1015 55144267 2022-09-07 thomas return -1;
1016 55144267 2022-09-07 thomas sep = "&";
1017 55144267 2022-09-07 thomas }
1018 55144267 2022-09-07 thomas
1019 55144267 2022-09-07 thomas if (url->headref) {
1020 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->headref);
1021 55144267 2022-09-07 thomas if (tmp == NULL)
1022 55144267 2022-09-07 thomas return -1;
1023 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1024 55144267 2022-09-07 thomas free(tmp);
1025 55144267 2022-09-07 thomas if (r == -1)
1026 55144267 2022-09-07 thomas return -1;
1027 55144267 2022-09-07 thomas sep = "&";
1028 55144267 2022-09-07 thomas }
1029 55144267 2022-09-07 thomas
1030 55144267 2022-09-07 thomas if (url->index_page != -1) {
1031 d8bf4f25 2023-09-14 thomas if (tp_writef(c->tp, "%sindex_page=%d", sep,
1032 55144267 2022-09-07 thomas url->index_page) == -1)
1033 55144267 2022-09-07 thomas return -1;
1034 55144267 2022-09-07 thomas sep = "&";
1035 55144267 2022-09-07 thomas }
1036 55144267 2022-09-07 thomas
1037 55144267 2022-09-07 thomas if (url->path) {
1038 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->path);
1039 55144267 2022-09-07 thomas if (tmp == NULL)
1040 55144267 2022-09-07 thomas return -1;
1041 d8bf4f25 2023-09-14 thomas r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1042 55144267 2022-09-07 thomas free(tmp);
1043 55144267 2022-09-07 thomas if (r == -1)
1044 55144267 2022-09-07 thomas return -1;
1045 55144267 2022-09-07 thomas sep = "&";
1046 55144267 2022-09-07 thomas }
1047 55144267 2022-09-07 thomas
1048 55144267 2022-09-07 thomas return 0;
1049 55144267 2022-09-07 thomas }
1050 55144267 2022-09-07 thomas
1051 55144267 2022-09-07 thomas int
1052 d6795e9f 2022-12-30 thomas gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1053 d6795e9f 2022-12-30 thomas {
1054 d6795e9f 2022-12-30 thomas struct template *tp = c->tp;
1055 d6795e9f 2022-12-30 thomas const char *proto = c->https ? "https" : "http";
1056 d6795e9f 2022-12-30 thomas
1057 d8bf4f25 2023-09-14 thomas if (tp_writes(tp, proto) == -1 ||
1058 d8bf4f25 2023-09-14 thomas tp_writes(tp, "://") == -1 ||
1059 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->server_name) == -1 ||
1060 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->document_uri) == -1)
1061 d6795e9f 2022-12-30 thomas return -1;
1062 d6795e9f 2022-12-30 thomas
1063 d6795e9f 2022-12-30 thomas return gotweb_render_url(c, url);
1064 55e6cffd 2022-09-01 thomas }
1065 55e6cffd 2022-09-01 thomas
1066 8a35f56c 2022-07-16 thomas static const struct got_error *
1067 f56b35c7 2024-04-29 thomas gotweb_load_got_path(struct repo_dir **rp, const char *dir,
1068 f56b35c7 2024-04-29 thomas struct request *c)
1069 8a35f56c 2022-07-16 thomas {
1070 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1071 8a35f56c 2022-07-16 thomas struct socket *sock = c->sock;
1072 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1073 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1074 f56b35c7 2024-04-29 thomas struct repo_dir *repo_dir;
1075 8a35f56c 2022-07-16 thomas DIR *dt;
1076 8a35f56c 2022-07-16 thomas char *dir_test;
1077 8a35f56c 2022-07-16 thomas
1078 f56b35c7 2024-04-29 thomas *rp = calloc(1, sizeof(**rp));
1079 f56b35c7 2024-04-29 thomas if (*rp == NULL)
1080 f56b35c7 2024-04-29 thomas return got_error_from_errno("calloc");
1081 f56b35c7 2024-04-29 thomas repo_dir = *rp;
1082 f56b35c7 2024-04-29 thomas
1083 f56b35c7 2024-04-29 thomas if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, dir,
1084 8a35f56c 2022-07-16 thomas GOTWEB_GIT_DIR) == -1)
1085 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1086 8a35f56c 2022-07-16 thomas
1087 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1088 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1089 8a35f56c 2022-07-16 thomas free(dir_test);
1090 f56b35c7 2024-04-29 thomas if (asprintf(&dir_test, "%s/%s", srv->repos_path, dir) == -1)
1091 c13168e4 2023-12-01 thomas return got_error_from_errno("asprintf");
1092 c13168e4 2023-12-01 thomas dt = opendir(dir_test);
1093 c13168e4 2023-12-01 thomas if (dt == NULL) {
1094 c13168e4 2023-12-01 thomas free(dir_test);
1095 f56b35c7 2024-04-29 thomas if (asprintf(&dir_test, "%s/%s%s", srv->repos_path,
1096 f56b35c7 2024-04-29 thomas dir, GOTWEB_GIT_DIR) == -1)
1097 f56b35c7 2024-04-29 thomas return got_error_from_errno("asprintf");
1098 f56b35c7 2024-04-29 thomas dt = opendir(dir_test);
1099 f56b35c7 2024-04-29 thomas if (dt == NULL) {
1100 f56b35c7 2024-04-29 thomas free(dir_test);
1101 f56b35c7 2024-04-29 thomas return got_error_path(dir,
1102 f56b35c7 2024-04-29 thomas GOT_ERR_NOT_GIT_REPO);
1103 f56b35c7 2024-04-29 thomas }
1104 c13168e4 2023-12-01 thomas }
1105 ab5bda7e 2023-11-22 thomas }
1106 8a35f56c 2022-07-16 thomas
1107 c13168e4 2023-12-01 thomas repo_dir->path = dir_test;
1108 c13168e4 2023-12-01 thomas dir_test = NULL;
1109 c2d3d9a0 2022-09-01 thomas
1110 f56b35c7 2024-04-29 thomas repo_dir->name = strdup(repo_dir->path + strlen(srv->repos_path) + 1);
1111 f56b35c7 2024-04-29 thomas if (repo_dir->name == NULL) {
1112 f56b35c7 2024-04-29 thomas error = got_error_from_errno("strdup");
1113 f56b35c7 2024-04-29 thomas goto err;
1114 f56b35c7 2024-04-29 thomas }
1115 f56b35c7 2024-04-29 thomas
1116 3991b2a5 2022-10-31 thomas if (srv->respect_exportok &&
1117 3991b2a5 2022-10-31 thomas faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1118 3991b2a5 2022-10-31 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1119 3991b2a5 2022-10-31 thomas goto err;
1120 3991b2a5 2022-10-31 thomas }
1121 3991b2a5 2022-10-31 thomas
1122 ab5bda7e 2023-11-22 thomas error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1123 ab5bda7e 2023-11-22 thomas if (error)
1124 ab5bda7e 2023-11-22 thomas goto err;
1125 8a35f56c 2022-07-16 thomas error = gotweb_get_repo_description(&repo_dir->description, srv,
1126 4606e6d4 2022-11-23 thomas repo_dir->path, dirfd(dt));
1127 8a35f56c 2022-07-16 thomas if (error)
1128 8a35f56c 2022-07-16 thomas goto err;
1129 6c7f10f7 2022-11-23 thomas error = got_get_repo_owner(&repo_dir->owner, c);
1130 8a35f56c 2022-07-16 thomas if (error)
1131 8a35f56c 2022-07-16 thomas goto err;
1132 b38bef13 2023-08-12 thomas if (srv->show_repo_age) {
1133 b38bef13 2023-08-12 thomas error = got_get_repo_age(&repo_dir->age, c, NULL);
1134 b38bef13 2023-08-12 thomas if (error)
1135 b38bef13 2023-08-12 thomas goto err;
1136 b38bef13 2023-08-12 thomas }
1137 4606e6d4 2022-11-23 thomas error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1138 4606e6d4 2022-11-23 thomas dirfd(dt));
1139 8a35f56c 2022-07-16 thomas err:
1140 8a35f56c 2022-07-16 thomas free(dir_test);
1141 c2d3d9a0 2022-09-01 thomas if (dt != NULL && closedir(dt) == EOF && error == NULL)
1142 c2d3d9a0 2022-09-01 thomas error = got_error_from_errno("closedir");
1143 ab5bda7e 2023-11-22 thomas if (error && t->repo) {
1144 ab5bda7e 2023-11-22 thomas got_repo_close(t->repo);
1145 ab5bda7e 2023-11-22 thomas t->repo = NULL;
1146 ab5bda7e 2023-11-22 thomas }
1147 8a35f56c 2022-07-16 thomas return error;
1148 8a35f56c 2022-07-16 thomas }
1149 8a35f56c 2022-07-16 thomas
1150 8a35f56c 2022-07-16 thomas static const struct got_error *
1151 4606e6d4 2022-11-23 thomas gotweb_get_repo_description(char **description, struct server *srv,
1152 4606e6d4 2022-11-23 thomas const char *dirpath, int dir)
1153 8a35f56c 2022-07-16 thomas {
1154 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1155 4606e6d4 2022-11-23 thomas struct stat sb;
1156 4606e6d4 2022-11-23 thomas int fd = -1;
1157 4606e6d4 2022-11-23 thomas off_t len;
1158 8a35f56c 2022-07-16 thomas
1159 8a35f56c 2022-07-16 thomas *description = NULL;
1160 8a35f56c 2022-07-16 thomas if (srv->show_repo_description == 0)
1161 8a35f56c 2022-07-16 thomas return NULL;
1162 8a35f56c 2022-07-16 thomas
1163 4606e6d4 2022-11-23 thomas fd = openat(dir, "description", O_RDONLY);
1164 4606e6d4 2022-11-23 thomas if (fd == -1) {
1165 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1166 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1167 4606e6d4 2022-11-23 thomas dirpath, "description");
1168 4606e6d4 2022-11-23 thomas }
1169 8a35f56c 2022-07-16 thomas goto done;
1170 8a35f56c 2022-07-16 thomas }
1171 8a35f56c 2022-07-16 thomas
1172 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1173 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1174 4606e6d4 2022-11-23 thomas dirpath, "description");
1175 8a35f56c 2022-07-16 thomas goto done;
1176 8a35f56c 2022-07-16 thomas }
1177 8a35f56c 2022-07-16 thomas
1178 4606e6d4 2022-11-23 thomas len = sb.st_size;
1179 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXDESCRSZ - 1)
1180 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXDESCRSZ - 1;
1181 8a35f56c 2022-07-16 thomas
1182 8a35f56c 2022-07-16 thomas *description = calloc(len + 1, sizeof(**description));
1183 8a35f56c 2022-07-16 thomas if (*description == NULL) {
1184 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1185 8a35f56c 2022-07-16 thomas goto done;
1186 8a35f56c 2022-07-16 thomas }
1187 8a35f56c 2022-07-16 thomas
1188 4606e6d4 2022-11-23 thomas if (read(fd, *description, len) == -1)
1189 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1190 8a35f56c 2022-07-16 thomas done:
1191 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1192 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1193 8a35f56c 2022-07-16 thomas return error;
1194 8a35f56c 2022-07-16 thomas }
1195 8a35f56c 2022-07-16 thomas
1196 8a35f56c 2022-07-16 thomas static const struct got_error *
1197 4606e6d4 2022-11-23 thomas gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1198 4606e6d4 2022-11-23 thomas int dir)
1199 8a35f56c 2022-07-16 thomas {
1200 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1201 4606e6d4 2022-11-23 thomas struct stat sb;
1202 4606e6d4 2022-11-23 thomas int fd = -1;
1203 4606e6d4 2022-11-23 thomas off_t len;
1204 8a35f56c 2022-07-16 thomas
1205 8a35f56c 2022-07-16 thomas *url = NULL;
1206 8a35f56c 2022-07-16 thomas if (srv->show_repo_cloneurl == 0)
1207 8a35f56c 2022-07-16 thomas return NULL;
1208 8a35f56c 2022-07-16 thomas
1209 4606e6d4 2022-11-23 thomas fd = openat(dir, "cloneurl", O_RDONLY);
1210 4606e6d4 2022-11-23 thomas if (fd == -1) {
1211 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1212 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1213 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1214 4606e6d4 2022-11-23 thomas }
1215 8a35f56c 2022-07-16 thomas goto done;
1216 8a35f56c 2022-07-16 thomas }
1217 8a35f56c 2022-07-16 thomas
1218 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1219 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1220 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1221 8a35f56c 2022-07-16 thomas goto done;
1222 8a35f56c 2022-07-16 thomas }
1223 8a35f56c 2022-07-16 thomas
1224 4606e6d4 2022-11-23 thomas len = sb.st_size;
1225 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1226 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXCLONEURLSZ - 1;
1227 8a35f56c 2022-07-16 thomas
1228 8a35f56c 2022-07-16 thomas *url = calloc(len + 1, sizeof(**url));
1229 8a35f56c 2022-07-16 thomas if (*url == NULL) {
1230 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1231 8a35f56c 2022-07-16 thomas goto done;
1232 8a35f56c 2022-07-16 thomas }
1233 8a35f56c 2022-07-16 thomas
1234 4606e6d4 2022-11-23 thomas if (read(fd, *url, len) == -1)
1235 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1236 8a35f56c 2022-07-16 thomas done:
1237 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1238 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1239 8a35f56c 2022-07-16 thomas return error;
1240 8a35f56c 2022-07-16 thomas }
1241 8a35f56c 2022-07-16 thomas
1242 53bf32b8 2023-01-23 thomas int
1243 4cc0851e 2023-10-08 thomas gotweb_render_age(struct template *tp, time_t committer_time)
1244 8a35f56c 2022-07-16 thomas {
1245 53bf32b8 2023-01-23 thomas struct request *c = tp->tp_arg;
1246 399ea8e4 2022-07-20 thomas long long diff_time;
1247 8a35f56c 2022-07-16 thomas const char *years = "years ago", *months = "months ago";
1248 8a35f56c 2022-07-16 thomas const char *weeks = "weeks ago", *days = "days ago";
1249 8a35f56c 2022-07-16 thomas const char *hours = "hours ago", *minutes = "minutes ago";
1250 8a35f56c 2022-07-16 thomas const char *seconds = "seconds ago", *now = "right now";
1251 8a35f56c 2022-07-16 thomas
1252 4cc0851e 2023-10-08 thomas diff_time = time(NULL) - committer_time;
1253 4cc0851e 2023-10-08 thomas if (diff_time > 60 * 60 * 24 * 365 * 2) {
1254 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1255 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60 / 24 / 365), years) == -1)
1256 53bf32b8 2023-01-23 thomas return -1;
1257 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1258 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1259 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60 / 24 / (365 / 12)),
1260 4cc0851e 2023-10-08 thomas months) == -1)
1261 53bf32b8 2023-01-23 thomas return -1;
1262 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1263 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1264 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1265 53bf32b8 2023-01-23 thomas return -1;
1266 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 60 * 24 * 2) {
1267 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1268 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60 / 24), days) == -1)
1269 4cc0851e 2023-10-08 thomas return -1;
1270 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 60 * 2) {
1271 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s",
1272 4cc0851e 2023-10-08 thomas (diff_time / 60 / 60), hours) == -1)
1273 4cc0851e 2023-10-08 thomas return -1;
1274 4cc0851e 2023-10-08 thomas } else if (diff_time > 60 * 2) {
1275 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1276 4cc0851e 2023-10-08 thomas minutes) == -1)
1277 4cc0851e 2023-10-08 thomas return -1;
1278 4cc0851e 2023-10-08 thomas } else if (diff_time > 2) {
1279 4cc0851e 2023-10-08 thomas if (tp_writef(c->tp, "%lld %s", diff_time,
1280 4cc0851e 2023-10-08 thomas seconds) == -1)
1281 4cc0851e 2023-10-08 thomas return -1;
1282 4cc0851e 2023-10-08 thomas } else {
1283 4cc0851e 2023-10-08 thomas if (tp_writes(tp, now) == -1)
1284 4cc0851e 2023-10-08 thomas return -1;
1285 8a35f56c 2022-07-16 thomas }
1286 53bf32b8 2023-01-23 thomas return 0;
1287 3e12c168 2022-07-16 thomas }