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 8a35f56c 2022-07-16 thomas
21 8a35f56c 2022-07-16 thomas #include <net/if.h>
22 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
23 3e12c168 2022-07-16 thomas #include <sys/queue.h>
24 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
25 8a35f56c 2022-07-16 thomas #include <sys/types.h>
26 8a35f56c 2022-07-16 thomas
27 2ac684a4 2022-09-03 thomas #include <ctype.h>
28 8a35f56c 2022-07-16 thomas #include <dirent.h>
29 8a35f56c 2022-07-16 thomas #include <errno.h>
30 8a35f56c 2022-07-16 thomas #include <event.h>
31 4606e6d4 2022-11-23 thomas #include <fcntl.h>
32 4606e6d4 2022-11-23 thomas #include <imsg.h>
33 8a35f56c 2022-07-16 thomas #include <stdio.h>
34 8a35f56c 2022-07-16 thomas #include <stdlib.h>
35 8a35f56c 2022-07-16 thomas #include <string.h>
36 8a35f56c 2022-07-16 thomas #include <unistd.h>
37 8a35f56c 2022-07-16 thomas
38 8a35f56c 2022-07-16 thomas #include "got_error.h"
39 8a35f56c 2022-07-16 thomas #include "got_object.h"
40 8a35f56c 2022-07-16 thomas #include "got_reference.h"
41 8a35f56c 2022-07-16 thomas #include "got_repository.h"
42 8a35f56c 2022-07-16 thomas #include "got_path.h"
43 8a35f56c 2022-07-16 thomas #include "got_cancel.h"
44 8a35f56c 2022-07-16 thomas #include "got_worktree.h"
45 8a35f56c 2022-07-16 thomas #include "got_diff.h"
46 8a35f56c 2022-07-16 thomas #include "got_commit_graph.h"
47 8a35f56c 2022-07-16 thomas #include "got_blame.h"
48 8a35f56c 2022-07-16 thomas #include "got_privsep.h"
49 cb11302c 2022-12-30 thomas
50 cb11302c 2022-12-30 thomas #include "got_compat.h"
51 8a35f56c 2022-07-16 thomas
52 8a35f56c 2022-07-16 thomas #include "proc.h"
53 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
54 d6795e9f 2022-12-30 thomas #include "tmpl.h"
55 ff36aeea 2022-07-16 thomas
56 8a35f56c 2022-07-16 thomas static const struct querystring_keys querystring_keys[] = {
57 8a35f56c 2022-07-16 thomas { "action", ACTION },
58 8a35f56c 2022-07-16 thomas { "commit", COMMIT },
59 8a35f56c 2022-07-16 thomas { "file", RFILE },
60 8a35f56c 2022-07-16 thomas { "folder", FOLDER },
61 8a35f56c 2022-07-16 thomas { "headref", HEADREF },
62 8a35f56c 2022-07-16 thomas { "index_page", INDEX_PAGE },
63 8a35f56c 2022-07-16 thomas { "path", PATH },
64 8a35f56c 2022-07-16 thomas { "page", PAGE },
65 8a35f56c 2022-07-16 thomas };
66 8a35f56c 2022-07-16 thomas
67 8a35f56c 2022-07-16 thomas static const struct action_keys action_keys[] = {
68 8a35f56c 2022-07-16 thomas { "blame", BLAME },
69 8a35f56c 2022-07-16 thomas { "blob", BLOB },
70 b82440e1 2023-01-06 thomas { "blobraw", BLOBRAW },
71 8a35f56c 2022-07-16 thomas { "briefs", BRIEFS },
72 8a35f56c 2022-07-16 thomas { "commits", COMMITS },
73 8a35f56c 2022-07-16 thomas { "diff", DIFF },
74 8a35f56c 2022-07-16 thomas { "error", ERR },
75 8a35f56c 2022-07-16 thomas { "index", INDEX },
76 8a35f56c 2022-07-16 thomas { "summary", SUMMARY },
77 8a35f56c 2022-07-16 thomas { "tag", TAG },
78 8a35f56c 2022-07-16 thomas { "tags", TAGS },
79 8a35f56c 2022-07-16 thomas { "tree", TREE },
80 d6795e9f 2022-12-30 thomas { "rss", RSS },
81 8a35f56c 2022-07-16 thomas };
82 8a35f56c 2022-07-16 thomas
83 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_querystring(struct querystring **);
84 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 8a35f56c 2022-07-16 thomas char *);
86 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 8a35f56c 2022-07-16 thomas char *, char *);
88 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_index(struct request *);
89 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 8a35f56c 2022-07-16 thomas const char *);
91 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_load_got_path(struct request *c,
92 8a35f56c 2022-07-16 thomas struct repo_dir *);
93 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_repo_description(char **,
94 4606e6d4 2022-11-23 thomas struct server *, const char *, int);
95 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 4606e6d4 2022-11-23 thomas const char *, int);
97 e7e5fa49 2022-12-30 thomas
98 8a35f56c 2022-07-16 thomas static void gotweb_free_querystring(struct querystring *);
99 8a35f56c 2022-07-16 thomas static void gotweb_free_repo_dir(struct repo_dir *);
100 8a35f56c 2022-07-16 thomas
101 3f0158b4 2022-08-30 thomas struct server *gotweb_get_server(uint8_t *, uint8_t *);
102 8a35f56c 2022-07-16 thomas
103 8a35f56c 2022-07-16 thomas void
104 8a35f56c 2022-07-16 thomas gotweb_process_request(struct request *c)
105 8a35f56c 2022-07-16 thomas {
106 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL, *error2 = NULL;
107 b82440e1 2023-01-06 thomas struct got_blob_object *blob = NULL;
108 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
109 8a35f56c 2022-07-16 thomas struct querystring *qs = NULL;
110 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
111 18e466eb 2023-01-14 thomas struct got_reflist_head refs;
112 dccd05b4 2023-01-10 thomas FILE *fp = NULL;
113 8a35f56c 2022-07-16 thomas uint8_t err[] = "gotwebd experienced an error: ";
114 b82440e1 2023-01-06 thomas int r, html = 0, fd = -1;
115 18e466eb 2023-01-14 thomas
116 18e466eb 2023-01-14 thomas TAILQ_INIT(&refs);
117 8a35f56c 2022-07-16 thomas
118 8a35f56c 2022-07-16 thomas /* init the transport */
119 8a35f56c 2022-07-16 thomas error = gotweb_init_transport(&c->t);
120 8a35f56c 2022-07-16 thomas if (error) {
121 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
122 46aeda9a 2022-08-27 thomas return;
123 8a35f56c 2022-07-16 thomas }
124 8a35f56c 2022-07-16 thomas /* don't process any further if client disconnected */
125 8a35f56c 2022-07-16 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
126 8a35f56c 2022-07-16 thomas return;
127 8a35f56c 2022-07-16 thomas /* get the gotwebd server */
128 3f0158b4 2022-08-30 thomas srv = gotweb_get_server(c->server_name, c->http_host);
129 8a35f56c 2022-07-16 thomas if (srv == NULL) {
130 8a35f56c 2022-07-16 thomas log_warnx("%s: error server is NULL", __func__);
131 8a35f56c 2022-07-16 thomas goto err;
132 8a35f56c 2022-07-16 thomas }
133 8a35f56c 2022-07-16 thomas c->srv = srv;
134 8a35f56c 2022-07-16 thomas /* parse our querystring */
135 8a35f56c 2022-07-16 thomas error = gotweb_init_querystring(&qs);
136 8a35f56c 2022-07-16 thomas if (error) {
137 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
138 8a35f56c 2022-07-16 thomas goto err;
139 8a35f56c 2022-07-16 thomas }
140 8a35f56c 2022-07-16 thomas c->t->qs = qs;
141 8a35f56c 2022-07-16 thomas error = gotweb_parse_querystring(&qs, c->querystring);
142 8a35f56c 2022-07-16 thomas if (error) {
143 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
144 8a35f56c 2022-07-16 thomas goto err;
145 8a35f56c 2022-07-16 thomas }
146 8a35f56c 2022-07-16 thomas
147 8a35f56c 2022-07-16 thomas /*
148 8a35f56c 2022-07-16 thomas * certain actions require a commit id in the querystring. this stops
149 8a35f56c 2022-07-16 thomas * bad actors from exploiting this by manually manipulating the
150 8a35f56c 2022-07-16 thomas * querystring.
151 8a35f56c 2022-07-16 thomas */
152 8a35f56c 2022-07-16 thomas
153 b82440e1 2023-01-06 thomas if (qs->action == BLAME || qs->action == BLOB ||
154 b82440e1 2023-01-06 thomas qs->action == BLOBRAW || qs->action == DIFF) {
155 b82440e1 2023-01-06 thomas if (qs->commit == NULL) {
156 b82440e1 2023-01-06 thomas error2 = got_error(GOT_ERR_QUERYSTRING);
157 b82440e1 2023-01-06 thomas goto render;
158 b82440e1 2023-01-06 thomas }
159 8a35f56c 2022-07-16 thomas }
160 8a35f56c 2022-07-16 thomas
161 8a35f56c 2022-07-16 thomas if (qs->action != INDEX) {
162 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, qs->path);
163 8a35f56c 2022-07-16 thomas if (error)
164 8a35f56c 2022-07-16 thomas goto done;
165 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
166 8a35f56c 2022-07-16 thomas c->t->repo_dir = repo_dir;
167 8a35f56c 2022-07-16 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
168 8a35f56c 2022-07-16 thomas goto err;
169 8a35f56c 2022-07-16 thomas }
170 8a35f56c 2022-07-16 thomas
171 b82440e1 2023-01-06 thomas if (qs->action == BLOBRAW) {
172 ea1b5cf5 2023-01-15 thomas const uint8_t *buf;
173 ea1b5cf5 2023-01-15 thomas size_t len;
174 ea1b5cf5 2023-01-15 thomas int binary;
175 ea1b5cf5 2023-01-15 thomas
176 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
177 8a35f56c 2022-07-16 thomas if (error)
178 8a35f56c 2022-07-16 thomas goto done;
179 ea1b5cf5 2023-01-15 thomas
180 ea1b5cf5 2023-01-15 thomas error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
181 ea1b5cf5 2023-01-15 thomas if (error2)
182 ea1b5cf5 2023-01-15 thomas goto render;
183 ea1b5cf5 2023-01-15 thomas
184 ea1b5cf5 2023-01-15 thomas if (binary)
185 ea1b5cf5 2023-01-15 thomas error = gotweb_render_content_type_file(c,
186 ea1b5cf5 2023-01-15 thomas "application/octet-stream", qs->file, NULL);
187 ea1b5cf5 2023-01-15 thomas else
188 ea1b5cf5 2023-01-15 thomas error = gotweb_render_content_type(c, "text/plain");
189 ea1b5cf5 2023-01-15 thomas
190 d6795e9f 2022-12-30 thomas if (error) {
191 d6795e9f 2022-12-30 thomas log_warnx("%s: %s", __func__, error->msg);
192 ea1b5cf5 2023-01-15 thomas goto done;
193 ea1b5cf5 2023-01-15 thomas }
194 ea1b5cf5 2023-01-15 thomas
195 ea1b5cf5 2023-01-15 thomas for (;;) {
196 ea1b5cf5 2023-01-15 thomas error = got_object_blob_read_block(&len, blob);
197 ea1b5cf5 2023-01-15 thomas if (error)
198 ea1b5cf5 2023-01-15 thomas goto done;
199 ea1b5cf5 2023-01-15 thomas if (len == 0)
200 ea1b5cf5 2023-01-15 thomas break;
201 ea1b5cf5 2023-01-15 thomas buf = got_object_blob_get_read_buf(blob);
202 ea1b5cf5 2023-01-15 thomas if (fcgi_gen_binary_response(c, buf, len) == -1)
203 ea1b5cf5 2023-01-15 thomas goto done;
204 d6795e9f 2022-12-30 thomas }
205 ea1b5cf5 2023-01-15 thomas
206 d6795e9f 2022-12-30 thomas goto done;
207 b82440e1 2023-01-06 thomas }
208 b82440e1 2023-01-06 thomas
209 b82440e1 2023-01-06 thomas if (qs->action == BLOB) {
210 b82440e1 2023-01-06 thomas int binary;
211 b82440e1 2023-01-06 thomas struct gotweb_url url = {
212 b82440e1 2023-01-06 thomas .index_page = -1,
213 b82440e1 2023-01-06 thomas .page = -1,
214 b82440e1 2023-01-06 thomas .action = BLOBRAW,
215 b82440e1 2023-01-06 thomas .path = qs->path,
216 b82440e1 2023-01-06 thomas .commit = qs->commit,
217 b82440e1 2023-01-06 thomas .folder = qs->folder,
218 b82440e1 2023-01-06 thomas .file = qs->file,
219 b82440e1 2023-01-06 thomas };
220 b82440e1 2023-01-06 thomas
221 b82440e1 2023-01-06 thomas error = got_get_repo_commits(c, 1);
222 b82440e1 2023-01-06 thomas if (error)
223 b82440e1 2023-01-06 thomas goto done;
224 b82440e1 2023-01-06 thomas
225 b82440e1 2023-01-06 thomas error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
226 b82440e1 2023-01-06 thomas if (error2)
227 b82440e1 2023-01-06 thomas goto render;
228 b82440e1 2023-01-06 thomas if (binary) {
229 b82440e1 2023-01-06 thomas fcgi_puts(c->tp, "Status: 302\r\n");
230 b82440e1 2023-01-06 thomas fcgi_puts(c->tp, "Location: ");
231 b82440e1 2023-01-06 thomas gotweb_render_url(c, &url);
232 b82440e1 2023-01-06 thomas fcgi_puts(c->tp, "\r\n\r\n");
233 b82440e1 2023-01-06 thomas goto done;
234 b82440e1 2023-01-06 thomas }
235 d6795e9f 2022-12-30 thomas }
236 d6795e9f 2022-12-30 thomas
237 d6795e9f 2022-12-30 thomas if (qs->action == RSS) {
238 d7034a4e 2023-01-06 thomas error = gotweb_render_content_type_file(c,
239 d7034a4e 2023-01-06 thomas "application/rss+xml;charset=utf-8",
240 d7034a4e 2023-01-06 thomas repo_dir->name, ".rss");
241 d6795e9f 2022-12-30 thomas if (error) {
242 d6795e9f 2022-12-30 thomas log_warnx("%s: %s", __func__, error->msg);
243 d6795e9f 2022-12-30 thomas goto err;
244 d6795e9f 2022-12-30 thomas }
245 d6795e9f 2022-12-30 thomas
246 d6795e9f 2022-12-30 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
247 8a35f56c 2022-07-16 thomas if (error) {
248 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
249 8a35f56c 2022-07-16 thomas goto err;
250 8a35f56c 2022-07-16 thomas }
251 d6795e9f 2022-12-30 thomas if (gotweb_render_rss(c->tp) == -1)
252 d6795e9f 2022-12-30 thomas goto err;
253 8a35f56c 2022-07-16 thomas goto done;
254 43a44bce 2022-12-04 thomas }
255 43a44bce 2022-12-04 thomas
256 43a44bce 2022-12-04 thomas render:
257 43a44bce 2022-12-04 thomas error = gotweb_render_content_type(c, "text/html");
258 43a44bce 2022-12-04 thomas if (error) {
259 43a44bce 2022-12-04 thomas log_warnx("%s: %s", __func__, error->msg);
260 43a44bce 2022-12-04 thomas goto err;
261 8a35f56c 2022-07-16 thomas }
262 43a44bce 2022-12-04 thomas html = 1;
263 8a35f56c 2022-07-16 thomas
264 e7e5fa49 2022-12-30 thomas if (gotweb_render_header(c->tp) == -1)
265 8a35f56c 2022-07-16 thomas goto err;
266 8a35f56c 2022-07-16 thomas
267 8a35f56c 2022-07-16 thomas if (error2) {
268 8a35f56c 2022-07-16 thomas error = error2;
269 8a35f56c 2022-07-16 thomas goto err;
270 8a35f56c 2022-07-16 thomas }
271 8a35f56c 2022-07-16 thomas
272 8a35f56c 2022-07-16 thomas switch(qs->action) {
273 8a35f56c 2022-07-16 thomas case BLAME:
274 1cd5d437 2023-01-15 thomas error = got_get_repo_commits(c, 1);
275 8a35f56c 2022-07-16 thomas if (error) {
276 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
277 8a35f56c 2022-07-16 thomas goto err;
278 8a35f56c 2022-07-16 thomas }
279 1cd5d437 2023-01-15 thomas if (gotweb_render_blame(c->tp) == -1)
280 1cd5d437 2023-01-15 thomas goto done;
281 b82440e1 2023-01-06 thomas break;
282 b82440e1 2023-01-06 thomas case BLOB:
283 b82440e1 2023-01-06 thomas if (gotweb_render_blob(c->tp, blob) == -1)
284 b82440e1 2023-01-06 thomas goto err;
285 8a35f56c 2022-07-16 thomas break;
286 8a35f56c 2022-07-16 thomas case BRIEFS:
287 e7e5fa49 2022-12-30 thomas if (gotweb_render_briefs(c->tp) == -1)
288 8a35f56c 2022-07-16 thomas goto err;
289 8a35f56c 2022-07-16 thomas break;
290 8a35f56c 2022-07-16 thomas case COMMITS:
291 7ade8b27 2022-12-30 thomas error = got_get_repo_commits(c, srv->max_commits_display);
292 8a35f56c 2022-07-16 thomas if (error) {
293 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
294 8a35f56c 2022-07-16 thomas goto err;
295 8a35f56c 2022-07-16 thomas }
296 7ade8b27 2022-12-30 thomas if (gotweb_render_commits(c->tp) == -1)
297 7ade8b27 2022-12-30 thomas goto err;
298 8a35f56c 2022-07-16 thomas break;
299 8a35f56c 2022-07-16 thomas case DIFF:
300 dccd05b4 2023-01-10 thomas error = got_get_repo_commits(c, 1);
301 5d860bce 2023-01-07 thomas if (error) {
302 5d860bce 2023-01-07 thomas log_warnx("%s: %s", __func__, error->msg);
303 5d860bce 2023-01-07 thomas goto err;
304 5d860bce 2023-01-07 thomas }
305 dccd05b4 2023-01-10 thomas error = got_open_diff_for_output(&fp, &fd, c);
306 dccd05b4 2023-01-10 thomas if (error) {
307 dccd05b4 2023-01-10 thomas log_warnx("%s: %s", __func__, error->msg);
308 dccd05b4 2023-01-10 thomas goto err;
309 dccd05b4 2023-01-10 thomas }
310 dccd05b4 2023-01-10 thomas if (gotweb_render_diff(c->tp, fp) == -1)
311 dccd05b4 2023-01-10 thomas goto err;
312 8a35f56c 2022-07-16 thomas break;
313 8a35f56c 2022-07-16 thomas case INDEX:
314 8a35f56c 2022-07-16 thomas error = gotweb_render_index(c);
315 8a35f56c 2022-07-16 thomas if (error) {
316 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
317 8a35f56c 2022-07-16 thomas goto err;
318 8a35f56c 2022-07-16 thomas }
319 8a35f56c 2022-07-16 thomas break;
320 8a35f56c 2022-07-16 thomas case SUMMARY:
321 18e466eb 2023-01-14 thomas error = got_ref_list(&refs, c->t->repo, "refs/heads",
322 18e466eb 2023-01-14 thomas got_ref_cmp_by_name, NULL);
323 8a35f56c 2022-07-16 thomas if (error) {
324 18e466eb 2023-01-14 thomas log_warnx("%s: got_ref_list: %s", __func__,
325 18e466eb 2023-01-14 thomas error->msg);
326 8a35f56c 2022-07-16 thomas goto err;
327 8a35f56c 2022-07-16 thomas }
328 18e466eb 2023-01-14 thomas qs->action = TAGS;
329 18e466eb 2023-01-14 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
330 18e466eb 2023-01-14 thomas if (error) {
331 18e466eb 2023-01-14 thomas log_warnx("%s: got_get_repo_tags: %s", __func__,
332 18e466eb 2023-01-14 thomas error->msg);
333 18e466eb 2023-01-14 thomas goto err;
334 18e466eb 2023-01-14 thomas }
335 18e466eb 2023-01-14 thomas qs->action = SUMMARY;
336 18e466eb 2023-01-14 thomas if (gotweb_render_summary(c->tp, &refs) == -1)
337 18e466eb 2023-01-14 thomas goto done;
338 8a35f56c 2022-07-16 thomas break;
339 8a35f56c 2022-07-16 thomas case TAG:
340 145ca42a 2023-01-09 thomas error = got_get_repo_tags(c, 1);
341 8a35f56c 2022-07-16 thomas if (error) {
342 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
343 145ca42a 2023-01-09 thomas goto err;
344 145ca42a 2023-01-09 thomas }
345 145ca42a 2023-01-09 thomas if (c->t->tag_count == 0) {
346 145ca42a 2023-01-09 thomas error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
347 145ca42a 2023-01-09 thomas "bad commit id");
348 8a35f56c 2022-07-16 thomas goto err;
349 8a35f56c 2022-07-16 thomas }
350 145ca42a 2023-01-09 thomas if (gotweb_render_tag(c->tp) == -1)
351 145ca42a 2023-01-09 thomas goto done;
352 8a35f56c 2022-07-16 thomas break;
353 8a35f56c 2022-07-16 thomas case TAGS:
354 b3ba36c3 2023-01-14 thomas error = got_get_repo_tags(c, srv->max_commits_display);
355 8a35f56c 2022-07-16 thomas if (error) {
356 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
357 8a35f56c 2022-07-16 thomas goto err;
358 8a35f56c 2022-07-16 thomas }
359 b3ba36c3 2023-01-14 thomas if (gotweb_render_tags(c->tp) == -1)
360 b3ba36c3 2023-01-14 thomas goto done;
361 8a35f56c 2022-07-16 thomas break;
362 8a35f56c 2022-07-16 thomas case TREE:
363 3c14c1f2 2023-01-06 thomas error = got_get_repo_commits(c, 1);
364 8a35f56c 2022-07-16 thomas if (error) {
365 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
366 8a35f56c 2022-07-16 thomas goto err;
367 8a35f56c 2022-07-16 thomas }
368 3c14c1f2 2023-01-06 thomas if (gotweb_render_tree(c->tp) == -1)
369 3c14c1f2 2023-01-06 thomas goto err;
370 8a35f56c 2022-07-16 thomas break;
371 8a35f56c 2022-07-16 thomas case ERR:
372 8a35f56c 2022-07-16 thomas default:
373 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
374 79393471 2022-08-27 thomas "Erorr: Bad Querystring");
375 79393471 2022-08-27 thomas if (r == -1)
376 8a35f56c 2022-07-16 thomas goto err;
377 8a35f56c 2022-07-16 thomas break;
378 8a35f56c 2022-07-16 thomas }
379 8a35f56c 2022-07-16 thomas
380 8a35f56c 2022-07-16 thomas goto done;
381 8a35f56c 2022-07-16 thomas err:
382 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
383 8a35f56c 2022-07-16 thomas return;
384 a5f25a12 2022-10-31 thomas if (fcgi_printf(c, "\n%s", err) == -1)
385 8a35f56c 2022-07-16 thomas return;
386 8a35f56c 2022-07-16 thomas if (error) {
387 79393471 2022-08-27 thomas if (fcgi_printf(c, "%s", error->msg) == -1)
388 8a35f56c 2022-07-16 thomas return;
389 8a35f56c 2022-07-16 thomas } else {
390 79393471 2022-08-27 thomas if (fcgi_printf(c, "see daemon logs for details") == -1)
391 8a35f56c 2022-07-16 thomas return;
392 8a35f56c 2022-07-16 thomas }
393 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "</div>\n") == -1)
394 8a35f56c 2022-07-16 thomas return;
395 8a35f56c 2022-07-16 thomas done:
396 b82440e1 2023-01-06 thomas if (blob)
397 b82440e1 2023-01-06 thomas got_object_blob_close(blob);
398 dccd05b4 2023-01-10 thomas if (fp) {
399 dccd05b4 2023-01-10 thomas error = got_gotweb_flushfile(fp, fd);
400 dccd05b4 2023-01-10 thomas if (error)
401 dccd05b4 2023-01-10 thomas log_warnx("%s: got_gotweb_flushfile failure: %s",
402 dccd05b4 2023-01-10 thomas __func__, error->msg);
403 dccd05b4 2023-01-10 thomas fd = -1;
404 dccd05b4 2023-01-10 thomas }
405 b82440e1 2023-01-06 thomas if (fd != -1)
406 b82440e1 2023-01-06 thomas close(fd);
407 8a35f56c 2022-07-16 thomas if (html && srv != NULL)
408 e7e5fa49 2022-12-30 thomas gotweb_render_footer(c->tp);
409 18e466eb 2023-01-14 thomas
410 18e466eb 2023-01-14 thomas got_ref_list_free(&refs);
411 8a35f56c 2022-07-16 thomas }
412 8a35f56c 2022-07-16 thomas
413 8a35f56c 2022-07-16 thomas struct server *
414 3f0158b4 2022-08-30 thomas gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
415 8a35f56c 2022-07-16 thomas {
416 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
417 8a35f56c 2022-07-16 thomas
418 3f0158b4 2022-08-30 thomas /* check against the server name first */
419 8a35f56c 2022-07-16 thomas if (strlen(server_name) > 0)
420 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
421 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, server_name) == 0)
422 8a35f56c 2022-07-16 thomas goto done;
423 8a35f56c 2022-07-16 thomas
424 3f0158b4 2022-08-30 thomas /* check against subdomain second */
425 8a35f56c 2022-07-16 thomas if (strlen(subdomain) > 0)
426 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
427 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, subdomain) == 0)
428 8a35f56c 2022-07-16 thomas goto done;
429 8a35f56c 2022-07-16 thomas
430 8a35f56c 2022-07-16 thomas /* if those fail, send first server */
431 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
432 8a35f56c 2022-07-16 thomas if (srv != NULL)
433 8a35f56c 2022-07-16 thomas break;
434 8a35f56c 2022-07-16 thomas done:
435 8a35f56c 2022-07-16 thomas return srv;
436 8a35f56c 2022-07-16 thomas };
437 8a35f56c 2022-07-16 thomas
438 8a35f56c 2022-07-16 thomas const struct got_error *
439 8a35f56c 2022-07-16 thomas gotweb_init_transport(struct transport **t)
440 8a35f56c 2022-07-16 thomas {
441 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
442 8a35f56c 2022-07-16 thomas
443 8a35f56c 2022-07-16 thomas *t = calloc(1, sizeof(**t));
444 8a35f56c 2022-07-16 thomas if (*t == NULL)
445 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
446 8a35f56c 2022-07-16 thomas
447 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_commits);
448 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_tags);
449 8a35f56c 2022-07-16 thomas
450 8a35f56c 2022-07-16 thomas (*t)->repo = NULL;
451 8a35f56c 2022-07-16 thomas (*t)->repo_dir = NULL;
452 8a35f56c 2022-07-16 thomas (*t)->qs = NULL;
453 8a35f56c 2022-07-16 thomas (*t)->next_id = NULL;
454 8a35f56c 2022-07-16 thomas (*t)->prev_id = NULL;
455 8a35f56c 2022-07-16 thomas (*t)->next_disp = 0;
456 8a35f56c 2022-07-16 thomas (*t)->prev_disp = 0;
457 8a35f56c 2022-07-16 thomas
458 8a35f56c 2022-07-16 thomas return error;
459 8a35f56c 2022-07-16 thomas }
460 8a35f56c 2022-07-16 thomas
461 8a35f56c 2022-07-16 thomas static const struct got_error *
462 8a35f56c 2022-07-16 thomas gotweb_init_querystring(struct querystring **qs)
463 8a35f56c 2022-07-16 thomas {
464 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
465 8a35f56c 2022-07-16 thomas
466 8a35f56c 2022-07-16 thomas *qs = calloc(1, sizeof(**qs));
467 8a35f56c 2022-07-16 thomas if (*qs == NULL)
468 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
469 8a35f56c 2022-07-16 thomas
470 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup("HEAD");
471 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
472 84630254 2022-09-02 thomas free(*qs);
473 84630254 2022-09-02 thomas *qs = NULL;
474 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
475 8a35f56c 2022-07-16 thomas }
476 84630254 2022-09-02 thomas
477 84630254 2022-09-02 thomas (*qs)->action = INDEX;
478 84630254 2022-09-02 thomas (*qs)->commit = NULL;
479 84630254 2022-09-02 thomas (*qs)->file = NULL;
480 84630254 2022-09-02 thomas (*qs)->folder = NULL;
481 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
482 8a35f56c 2022-07-16 thomas (*qs)->path = NULL;
483 8a35f56c 2022-07-16 thomas
484 8a35f56c 2022-07-16 thomas return error;
485 8a35f56c 2022-07-16 thomas }
486 8a35f56c 2022-07-16 thomas
487 8a35f56c 2022-07-16 thomas static const struct got_error *
488 8a35f56c 2022-07-16 thomas gotweb_parse_querystring(struct querystring **qs, char *qst)
489 8a35f56c 2022-07-16 thomas {
490 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
491 8a35f56c 2022-07-16 thomas char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
492 8a35f56c 2022-07-16 thomas char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
493 8a35f56c 2022-07-16 thomas
494 8a35f56c 2022-07-16 thomas if (qst == NULL)
495 8a35f56c 2022-07-16 thomas return error;
496 8a35f56c 2022-07-16 thomas
497 8a35f56c 2022-07-16 thomas tok1 = strdup(qst);
498 8a35f56c 2022-07-16 thomas if (tok1 == NULL)
499 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
500 8a35f56c 2022-07-16 thomas
501 8a35f56c 2022-07-16 thomas tok1_pair = tok1;
502 8a35f56c 2022-07-16 thomas tok1_end = tok1;
503 8a35f56c 2022-07-16 thomas
504 8a35f56c 2022-07-16 thomas while (tok1_pair != NULL) {
505 8a35f56c 2022-07-16 thomas strsep(&tok1_end, "&");
506 8a35f56c 2022-07-16 thomas
507 8a35f56c 2022-07-16 thomas tok2 = strdup(tok1_pair);
508 8a35f56c 2022-07-16 thomas if (tok2 == NULL) {
509 8a35f56c 2022-07-16 thomas free(tok1);
510 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
511 8a35f56c 2022-07-16 thomas }
512 8a35f56c 2022-07-16 thomas
513 8a35f56c 2022-07-16 thomas tok2_pair = tok2;
514 8a35f56c 2022-07-16 thomas tok2_end = tok2;
515 8a35f56c 2022-07-16 thomas
516 8a35f56c 2022-07-16 thomas while (tok2_pair != NULL) {
517 8a35f56c 2022-07-16 thomas strsep(&tok2_end, "=");
518 8a35f56c 2022-07-16 thomas if (tok2_end) {
519 8a35f56c 2022-07-16 thomas error = gotweb_assign_querystring(qs, tok2_pair,
520 8a35f56c 2022-07-16 thomas tok2_end);
521 8a35f56c 2022-07-16 thomas if (error)
522 8a35f56c 2022-07-16 thomas goto err;
523 8a35f56c 2022-07-16 thomas }
524 8a35f56c 2022-07-16 thomas tok2_pair = tok2_end;
525 8a35f56c 2022-07-16 thomas }
526 8a35f56c 2022-07-16 thomas free(tok2);
527 8a35f56c 2022-07-16 thomas tok1_pair = tok1_end;
528 8a35f56c 2022-07-16 thomas }
529 8a35f56c 2022-07-16 thomas free(tok1);
530 8a35f56c 2022-07-16 thomas return error;
531 8a35f56c 2022-07-16 thomas err:
532 8a35f56c 2022-07-16 thomas free(tok2);
533 8a35f56c 2022-07-16 thomas free(tok1);
534 8a35f56c 2022-07-16 thomas return error;
535 8a35f56c 2022-07-16 thomas }
536 8a35f56c 2022-07-16 thomas
537 2ac684a4 2022-09-03 thomas /*
538 2ac684a4 2022-09-03 thomas * Adapted from usr.sbin/httpd/httpd.c url_decode.
539 2ac684a4 2022-09-03 thomas */
540 8a35f56c 2022-07-16 thomas static const struct got_error *
541 2ac684a4 2022-09-03 thomas gotweb_urldecode(char *url)
542 2ac684a4 2022-09-03 thomas {
543 2ac684a4 2022-09-03 thomas char *p, *q;
544 2ac684a4 2022-09-03 thomas char hex[3];
545 2ac684a4 2022-09-03 thomas unsigned long x;
546 2ac684a4 2022-09-03 thomas
547 2ac684a4 2022-09-03 thomas hex[2] = '\0';
548 2ac684a4 2022-09-03 thomas p = q = url;
549 2ac684a4 2022-09-03 thomas
550 2ac684a4 2022-09-03 thomas while (*p != '\0') {
551 2ac684a4 2022-09-03 thomas switch (*p) {
552 2ac684a4 2022-09-03 thomas case '%':
553 2ac684a4 2022-09-03 thomas /* Encoding character is followed by two hex chars */
554 2ac684a4 2022-09-03 thomas if (!isxdigit((unsigned char)p[1]) ||
555 2ac684a4 2022-09-03 thomas !isxdigit((unsigned char)p[2]) ||
556 2ac684a4 2022-09-03 thomas (p[1] == '0' && p[2] == '0'))
557 2ac684a4 2022-09-03 thomas return got_error(GOT_ERR_BAD_QUERYSTRING);
558 2ac684a4 2022-09-03 thomas
559 2ac684a4 2022-09-03 thomas hex[0] = p[1];
560 2ac684a4 2022-09-03 thomas hex[1] = p[2];
561 2ac684a4 2022-09-03 thomas
562 2ac684a4 2022-09-03 thomas /*
563 2ac684a4 2022-09-03 thomas * We don't have to validate "hex" because it is
564 2ac684a4 2022-09-03 thomas * guaranteed to include two hex chars followed by nul.
565 2ac684a4 2022-09-03 thomas */
566 2ac684a4 2022-09-03 thomas x = strtoul(hex, NULL, 16);
567 2ac684a4 2022-09-03 thomas *q = (char)x;
568 2ac684a4 2022-09-03 thomas p += 2;
569 2ac684a4 2022-09-03 thomas break;
570 2ac684a4 2022-09-03 thomas default:
571 2ac684a4 2022-09-03 thomas *q = *p;
572 2ac684a4 2022-09-03 thomas break;
573 2ac684a4 2022-09-03 thomas }
574 2ac684a4 2022-09-03 thomas p++;
575 2ac684a4 2022-09-03 thomas q++;
576 2ac684a4 2022-09-03 thomas }
577 2ac684a4 2022-09-03 thomas *q = '\0';
578 2ac684a4 2022-09-03 thomas
579 2ac684a4 2022-09-03 thomas return NULL;
580 2ac684a4 2022-09-03 thomas }
581 2ac684a4 2022-09-03 thomas
582 2ac684a4 2022-09-03 thomas static const struct got_error *
583 8a35f56c 2022-07-16 thomas gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
584 8a35f56c 2022-07-16 thomas {
585 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
586 8a35f56c 2022-07-16 thomas const char *errstr;
587 8a35f56c 2022-07-16 thomas int a_cnt, el_cnt;
588 8a35f56c 2022-07-16 thomas
589 2ac684a4 2022-09-03 thomas error = gotweb_urldecode(value);
590 2ac684a4 2022-09-03 thomas if (error)
591 2ac684a4 2022-09-03 thomas return error;
592 2ac684a4 2022-09-03 thomas
593 8a35f56c 2022-07-16 thomas for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
594 8a35f56c 2022-07-16 thomas if (strcmp(key, querystring_keys[el_cnt].name) != 0)
595 8a35f56c 2022-07-16 thomas continue;
596 8a35f56c 2022-07-16 thomas
597 8a35f56c 2022-07-16 thomas switch (querystring_keys[el_cnt].element) {
598 8a35f56c 2022-07-16 thomas case ACTION:
599 8a35f56c 2022-07-16 thomas for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
600 8a35f56c 2022-07-16 thomas if (strcmp(value, action_keys[a_cnt].name) != 0)
601 8a35f56c 2022-07-16 thomas continue;
602 8a35f56c 2022-07-16 thomas else if (strcmp(value,
603 8a35f56c 2022-07-16 thomas action_keys[a_cnt].name) == 0){
604 8a35f56c 2022-07-16 thomas (*qs)->action =
605 8a35f56c 2022-07-16 thomas action_keys[a_cnt].action;
606 8a35f56c 2022-07-16 thomas goto qa_found;
607 8a35f56c 2022-07-16 thomas }
608 8a35f56c 2022-07-16 thomas }
609 8a35f56c 2022-07-16 thomas (*qs)->action = ERR;
610 8a35f56c 2022-07-16 thomas qa_found:
611 8a35f56c 2022-07-16 thomas break;
612 8a35f56c 2022-07-16 thomas case COMMIT:
613 8a35f56c 2022-07-16 thomas (*qs)->commit = strdup(value);
614 8a35f56c 2022-07-16 thomas if ((*qs)->commit == NULL) {
615 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
616 8a35f56c 2022-07-16 thomas __func__);
617 8a35f56c 2022-07-16 thomas goto done;
618 8a35f56c 2022-07-16 thomas }
619 8a35f56c 2022-07-16 thomas break;
620 8a35f56c 2022-07-16 thomas case RFILE:
621 8a35f56c 2022-07-16 thomas (*qs)->file = strdup(value);
622 8a35f56c 2022-07-16 thomas if ((*qs)->file == NULL) {
623 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
624 8a35f56c 2022-07-16 thomas __func__);
625 8a35f56c 2022-07-16 thomas goto done;
626 8a35f56c 2022-07-16 thomas }
627 8a35f56c 2022-07-16 thomas break;
628 8a35f56c 2022-07-16 thomas case FOLDER:
629 8a35f56c 2022-07-16 thomas (*qs)->folder = strdup(value);
630 8a35f56c 2022-07-16 thomas if ((*qs)->folder == NULL) {
631 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
632 8a35f56c 2022-07-16 thomas __func__);
633 8a35f56c 2022-07-16 thomas goto done;
634 8a35f56c 2022-07-16 thomas }
635 8a35f56c 2022-07-16 thomas break;
636 8a35f56c 2022-07-16 thomas case HEADREF:
637 b0764984 2022-09-02 thomas free((*qs)->headref);
638 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup(value);
639 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
640 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
641 8a35f56c 2022-07-16 thomas __func__);
642 8a35f56c 2022-07-16 thomas goto done;
643 8a35f56c 2022-07-16 thomas }
644 8a35f56c 2022-07-16 thomas break;
645 8a35f56c 2022-07-16 thomas case INDEX_PAGE:
646 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
647 8a35f56c 2022-07-16 thomas break;
648 8a35f56c 2022-07-16 thomas (*qs)->index_page = strtonum(value, INT64_MIN,
649 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
650 8a35f56c 2022-07-16 thomas if (errstr) {
651 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
652 8a35f56c 2022-07-16 thomas __func__, errstr);
653 8a35f56c 2022-07-16 thomas goto done;
654 8a35f56c 2022-07-16 thomas }
655 3d6d1fb0 2022-12-30 thomas if ((*qs)->index_page < 0)
656 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
657 8a35f56c 2022-07-16 thomas break;
658 8a35f56c 2022-07-16 thomas case PATH:
659 8a35f56c 2022-07-16 thomas (*qs)->path = strdup(value);
660 8a35f56c 2022-07-16 thomas if ((*qs)->path == NULL) {
661 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
662 8a35f56c 2022-07-16 thomas __func__);
663 8a35f56c 2022-07-16 thomas goto done;
664 8a35f56c 2022-07-16 thomas }
665 8a35f56c 2022-07-16 thomas break;
666 8a35f56c 2022-07-16 thomas case PAGE:
667 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
668 8a35f56c 2022-07-16 thomas break;
669 8a35f56c 2022-07-16 thomas (*qs)->page = strtonum(value, INT64_MIN,
670 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
671 8a35f56c 2022-07-16 thomas if (errstr) {
672 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
673 8a35f56c 2022-07-16 thomas __func__, errstr);
674 8a35f56c 2022-07-16 thomas goto done;
675 8a35f56c 2022-07-16 thomas }
676 3d6d1fb0 2022-12-30 thomas if ((*qs)->page < 0)
677 8a35f56c 2022-07-16 thomas (*qs)->page = 0;
678 8a35f56c 2022-07-16 thomas break;
679 8a35f56c 2022-07-16 thomas default:
680 8a35f56c 2022-07-16 thomas break;
681 8a35f56c 2022-07-16 thomas }
682 8a35f56c 2022-07-16 thomas }
683 8a35f56c 2022-07-16 thomas done:
684 8a35f56c 2022-07-16 thomas return error;
685 8a35f56c 2022-07-16 thomas }
686 8a35f56c 2022-07-16 thomas
687 8a35f56c 2022-07-16 thomas void
688 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(struct repo_tag *rt)
689 8a35f56c 2022-07-16 thomas {
690 8a35f56c 2022-07-16 thomas if (rt != NULL) {
691 8a35f56c 2022-07-16 thomas free(rt->commit_id);
692 217813df 2022-09-02 thomas free(rt->tag_name);
693 217813df 2022-09-02 thomas free(rt->tag_commit);
694 217813df 2022-09-02 thomas free(rt->commit_msg);
695 8a35f56c 2022-07-16 thomas free(rt->tagger);
696 8a35f56c 2022-07-16 thomas }
697 8a35f56c 2022-07-16 thomas free(rt);
698 8a35f56c 2022-07-16 thomas }
699 8a35f56c 2022-07-16 thomas
700 8a35f56c 2022-07-16 thomas void
701 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(struct repo_commit *rc)
702 8a35f56c 2022-07-16 thomas {
703 8a35f56c 2022-07-16 thomas if (rc != NULL) {
704 8a35f56c 2022-07-16 thomas free(rc->path);
705 8a35f56c 2022-07-16 thomas free(rc->refs_str);
706 8a35f56c 2022-07-16 thomas free(rc->commit_id);
707 8a35f56c 2022-07-16 thomas free(rc->parent_id);
708 8a35f56c 2022-07-16 thomas free(rc->tree_id);
709 8a35f56c 2022-07-16 thomas free(rc->author);
710 8a35f56c 2022-07-16 thomas free(rc->committer);
711 8a35f56c 2022-07-16 thomas free(rc->commit_msg);
712 8a35f56c 2022-07-16 thomas }
713 8a35f56c 2022-07-16 thomas free(rc);
714 8a35f56c 2022-07-16 thomas }
715 8a35f56c 2022-07-16 thomas
716 8a35f56c 2022-07-16 thomas static void
717 8a35f56c 2022-07-16 thomas gotweb_free_querystring(struct querystring *qs)
718 8a35f56c 2022-07-16 thomas {
719 8a35f56c 2022-07-16 thomas if (qs != NULL) {
720 8a35f56c 2022-07-16 thomas free(qs->commit);
721 8a35f56c 2022-07-16 thomas free(qs->file);
722 8a35f56c 2022-07-16 thomas free(qs->folder);
723 8a35f56c 2022-07-16 thomas free(qs->headref);
724 8a35f56c 2022-07-16 thomas free(qs->path);
725 8a35f56c 2022-07-16 thomas }
726 8a35f56c 2022-07-16 thomas free(qs);
727 8a35f56c 2022-07-16 thomas }
728 8a35f56c 2022-07-16 thomas
729 8a35f56c 2022-07-16 thomas static void
730 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(struct repo_dir *repo_dir)
731 8a35f56c 2022-07-16 thomas {
732 8a35f56c 2022-07-16 thomas if (repo_dir != NULL) {
733 8a35f56c 2022-07-16 thomas free(repo_dir->name);
734 8a35f56c 2022-07-16 thomas free(repo_dir->owner);
735 8a35f56c 2022-07-16 thomas free(repo_dir->description);
736 8a35f56c 2022-07-16 thomas free(repo_dir->url);
737 8a35f56c 2022-07-16 thomas free(repo_dir->age);
738 8a35f56c 2022-07-16 thomas free(repo_dir->path);
739 8a35f56c 2022-07-16 thomas }
740 8a35f56c 2022-07-16 thomas free(repo_dir);
741 8a35f56c 2022-07-16 thomas }
742 8a35f56c 2022-07-16 thomas
743 8a35f56c 2022-07-16 thomas void
744 8a35f56c 2022-07-16 thomas gotweb_free_transport(struct transport *t)
745 8a35f56c 2022-07-16 thomas {
746 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL, *trc = NULL;
747 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
748 8a35f56c 2022-07-16 thomas
749 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
750 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_commits, rc, entry);
751 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(rc);
752 8a35f56c 2022-07-16 thomas }
753 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
754 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
755 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
756 8a35f56c 2022-07-16 thomas }
757 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(t->repo_dir);
758 8a35f56c 2022-07-16 thomas gotweb_free_querystring(t->qs);
759 6b42af1e 2022-09-02 thomas free(t->next_id);
760 6b42af1e 2022-09-02 thomas free(t->prev_id);
761 8a35f56c 2022-07-16 thomas free(t);
762 8a35f56c 2022-07-16 thomas }
763 8a35f56c 2022-07-16 thomas
764 8a35f56c 2022-07-16 thomas const struct got_error *
765 0cef9478 2023-01-06 thomas gotweb_render_content_type(struct request *c, const char *type)
766 8a35f56c 2022-07-16 thomas {
767 0b75e088 2022-08-27 thomas const char *csp = "default-src 'self'; script-src 'none'; "
768 0b75e088 2022-08-27 thomas "object-src 'none';";
769 0b75e088 2022-08-27 thomas
770 0b75e088 2022-08-27 thomas fcgi_printf(c,
771 0b75e088 2022-08-27 thomas "Content-Security-Policy: %s\r\n"
772 0b75e088 2022-08-27 thomas "Content-Type: %s\r\n\r\n",
773 0b75e088 2022-08-27 thomas csp, type);
774 79393471 2022-08-27 thomas return NULL;
775 8a35f56c 2022-07-16 thomas }
776 8a35f56c 2022-07-16 thomas
777 8a35f56c 2022-07-16 thomas const struct got_error *
778 c7c38295 2023-01-06 thomas gotweb_render_content_type_file(struct request *c, const char *type,
779 d7034a4e 2023-01-06 thomas const char *file, const char *suffix)
780 8a35f56c 2022-07-16 thomas {
781 79393471 2022-08-27 thomas fcgi_printf(c, "Content-type: %s\r\n"
782 d7034a4e 2023-01-06 thomas "Content-disposition: attachment; filename=%s%s\r\n\r\n",
783 d7034a4e 2023-01-06 thomas type, file, suffix ? suffix : "");
784 79393471 2022-08-27 thomas return NULL;
785 8a35f56c 2022-07-16 thomas }
786 8a35f56c 2022-07-16 thomas
787 2f4f0731 2022-12-30 thomas void
788 2f4f0731 2022-12-30 thomas gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
789 2f4f0731 2022-12-30 thomas struct gotweb_url *next, int *have_next)
790 8a35f56c 2022-07-16 thomas {
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 server *srv = c->srv;
794 8a35f56c 2022-07-16 thomas
795 2f4f0731 2022-12-30 thomas *have_prev = *have_next = 0;
796 8a35f56c 2022-07-16 thomas
797 8a35f56c 2022-07-16 thomas switch(qs->action) {
798 8a35f56c 2022-07-16 thomas case INDEX:
799 8a35f56c 2022-07-16 thomas if (qs->index_page > 0) {
800 2f4f0731 2022-12-30 thomas *have_prev = 1;
801 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
802 55144267 2022-09-07 thomas .action = -1,
803 55144267 2022-09-07 thomas .index_page = qs->index_page - 1,
804 55144267 2022-09-07 thomas .page = -1,
805 55144267 2022-09-07 thomas };
806 8a35f56c 2022-07-16 thomas }
807 2f4f0731 2022-12-30 thomas if (t->next_disp == srv->max_repos_display &&
808 2f4f0731 2022-12-30 thomas t->repos_total != (qs->index_page + 1) *
809 2f4f0731 2022-12-30 thomas srv->max_repos_display) {
810 2f4f0731 2022-12-30 thomas *have_next = 1;
811 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
812 2f4f0731 2022-12-30 thomas .action = -1,
813 2f4f0731 2022-12-30 thomas .index_page = qs->index_page + 1,
814 2f4f0731 2022-12-30 thomas .page = -1,
815 2f4f0731 2022-12-30 thomas };
816 2f4f0731 2022-12-30 thomas }
817 8a35f56c 2022-07-16 thomas break;
818 8a35f56c 2022-07-16 thomas case BRIEFS:
819 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
820 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
821 2f4f0731 2022-12-30 thomas *have_prev = 1;
822 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
823 55144267 2022-09-07 thomas .action = BRIEFS,
824 55144267 2022-09-07 thomas .index_page = -1,
825 55144267 2022-09-07 thomas .page = qs->page - 1,
826 55144267 2022-09-07 thomas .path = qs->path,
827 55144267 2022-09-07 thomas .commit = t->prev_id,
828 55144267 2022-09-07 thomas .headref = qs->headref,
829 55144267 2022-09-07 thomas };
830 8a35f56c 2022-07-16 thomas }
831 2f4f0731 2022-12-30 thomas if (t->next_id) {
832 2f4f0731 2022-12-30 thomas *have_next = 1;
833 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
834 2f4f0731 2022-12-30 thomas .action = BRIEFS,
835 2f4f0731 2022-12-30 thomas .index_page = -1,
836 2f4f0731 2022-12-30 thomas .page = qs->page + 1,
837 2f4f0731 2022-12-30 thomas .path = qs->path,
838 2f4f0731 2022-12-30 thomas .commit = t->next_id,
839 2f4f0731 2022-12-30 thomas .headref = qs->headref,
840 2f4f0731 2022-12-30 thomas };
841 2f4f0731 2022-12-30 thomas }
842 8a35f56c 2022-07-16 thomas break;
843 8a35f56c 2022-07-16 thomas case COMMITS:
844 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
845 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
846 2f4f0731 2022-12-30 thomas *have_prev = 1;
847 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
848 8ea2e76e 2022-12-30 thomas .action = COMMITS,
849 55144267 2022-09-07 thomas .index_page = -1,
850 55144267 2022-09-07 thomas .page = qs->page - 1,
851 55144267 2022-09-07 thomas .path = qs->path,
852 55144267 2022-09-07 thomas .commit = t->prev_id,
853 55144267 2022-09-07 thomas .headref = qs->headref,
854 55144267 2022-09-07 thomas .folder = qs->folder,
855 55144267 2022-09-07 thomas .file = qs->file,
856 55144267 2022-09-07 thomas };
857 8a35f56c 2022-07-16 thomas }
858 2f4f0731 2022-12-30 thomas if (t->next_id) {
859 2f4f0731 2022-12-30 thomas *have_next = 1;
860 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
861 8ea2e76e 2022-12-30 thomas .action = COMMITS,
862 55144267 2022-09-07 thomas .index_page = -1,
863 55144267 2022-09-07 thomas .page = qs->page + 1,
864 55144267 2022-09-07 thomas .path = qs->path,
865 55144267 2022-09-07 thomas .commit = t->next_id,
866 55144267 2022-09-07 thomas .headref = qs->headref,
867 55144267 2022-09-07 thomas .folder = qs->folder,
868 55144267 2022-09-07 thomas .file = qs->file,
869 55144267 2022-09-07 thomas };
870 8a35f56c 2022-07-16 thomas }
871 8a35f56c 2022-07-16 thomas break;
872 8a35f56c 2022-07-16 thomas case TAGS:
873 2f4f0731 2022-12-30 thomas if (t->prev_id && qs->commit != NULL &&
874 2f4f0731 2022-12-30 thomas strcmp(qs->commit, t->prev_id) != 0) {
875 2f4f0731 2022-12-30 thomas *have_prev = 1;
876 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
877 2f4f0731 2022-12-30 thomas .action = TAGS,
878 2f4f0731 2022-12-30 thomas .index_page = -1,
879 2f4f0731 2022-12-30 thomas .page = qs->page - 1,
880 2f4f0731 2022-12-30 thomas .path = qs->path,
881 2f4f0731 2022-12-30 thomas .commit = t->prev_id,
882 2f4f0731 2022-12-30 thomas .headref = qs->headref,
883 2f4f0731 2022-12-30 thomas };
884 2f4f0731 2022-12-30 thomas }
885 8a35f56c 2022-07-16 thomas if (t->next_id) {
886 2f4f0731 2022-12-30 thomas *have_next = 1;
887 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
888 55144267 2022-09-07 thomas .action = TAGS,
889 55144267 2022-09-07 thomas .index_page = -1,
890 55144267 2022-09-07 thomas .page = qs->page + 1,
891 55144267 2022-09-07 thomas .path = qs->path,
892 55144267 2022-09-07 thomas .commit = t->next_id,
893 55144267 2022-09-07 thomas .headref = qs->headref,
894 55144267 2022-09-07 thomas };
895 8a35f56c 2022-07-16 thomas }
896 8a35f56c 2022-07-16 thomas break;
897 8a35f56c 2022-07-16 thomas }
898 8a35f56c 2022-07-16 thomas }
899 8a35f56c 2022-07-16 thomas
900 8a35f56c 2022-07-16 thomas static const struct got_error *
901 8a35f56c 2022-07-16 thomas gotweb_render_index(struct request *c)
902 8a35f56c 2022-07-16 thomas {
903 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
904 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
905 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
906 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
907 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
908 8a35f56c 2022-07-16 thomas DIR *d;
909 f530ab0e 2022-09-02 thomas struct dirent **sd_dent = NULL;
910 8a35f56c 2022-07-16 thomas unsigned int d_cnt, d_i, d_disp = 0;
911 24240f6a 2022-11-23 thomas unsigned int d_skipped = 0;
912 e7e5fa49 2022-12-30 thomas int type;
913 8a35f56c 2022-07-16 thomas
914 8a35f56c 2022-07-16 thomas d = opendir(srv->repos_path);
915 8a35f56c 2022-07-16 thomas if (d == NULL) {
916 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("opendir", srv->repos_path);
917 8a35f56c 2022-07-16 thomas return error;
918 8a35f56c 2022-07-16 thomas }
919 8a35f56c 2022-07-16 thomas
920 8a35f56c 2022-07-16 thomas d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
921 8a35f56c 2022-07-16 thomas if (d_cnt == -1) {
922 f530ab0e 2022-09-02 thomas sd_dent = NULL;
923 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("scandir", srv->repos_path);
924 8a35f56c 2022-07-16 thomas goto done;
925 8a35f56c 2022-07-16 thomas }
926 8a35f56c 2022-07-16 thomas
927 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_table_hdr(c->tp) == -1)
928 8a35f56c 2022-07-16 thomas goto done;
929 79393471 2022-08-27 thomas
930 8a35f56c 2022-07-16 thomas for (d_i = 0; d_i < d_cnt; d_i++) {
931 57e88d7c 2022-11-23 thomas if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
932 57e88d7c 2022-11-23 thomas break;
933 8a35f56c 2022-07-16 thomas
934 8a35f56c 2022-07-16 thomas if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
935 24240f6a 2022-11-23 thomas strcmp(sd_dent[d_i]->d_name, "..") == 0) {
936 24240f6a 2022-11-23 thomas d_skipped++;
937 24240f6a 2022-11-23 thomas continue;
938 24240f6a 2022-11-23 thomas }
939 24240f6a 2022-11-23 thomas
940 24240f6a 2022-11-23 thomas error = got_path_dirent_type(&type, srv->repos_path,
941 24240f6a 2022-11-23 thomas sd_dent[d_i]);
942 24240f6a 2022-11-23 thomas if (error)
943 24240f6a 2022-11-23 thomas goto done;
944 24240f6a 2022-11-23 thomas if (type != DT_DIR) {
945 24240f6a 2022-11-23 thomas d_skipped++;
946 8a35f56c 2022-07-16 thomas continue;
947 24240f6a 2022-11-23 thomas }
948 8a35f56c 2022-07-16 thomas
949 8a35f56c 2022-07-16 thomas if (qs->index_page > 0 && (qs->index_page *
950 8a35f56c 2022-07-16 thomas srv->max_repos_display) > t->prev_disp) {
951 8a35f56c 2022-07-16 thomas t->prev_disp++;
952 8a35f56c 2022-07-16 thomas continue;
953 8a35f56c 2022-07-16 thomas }
954 8a35f56c 2022-07-16 thomas
955 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
956 8a35f56c 2022-07-16 thomas if (error)
957 8a35f56c 2022-07-16 thomas goto done;
958 8a35f56c 2022-07-16 thomas
959 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
960 8a35f56c 2022-07-16 thomas if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
961 8a35f56c 2022-07-16 thomas error = NULL;
962 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
963 8a35f56c 2022-07-16 thomas repo_dir = NULL;
964 24240f6a 2022-11-23 thomas d_skipped++;
965 8a35f56c 2022-07-16 thomas continue;
966 8a35f56c 2022-07-16 thomas }
967 24240f6a 2022-11-23 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
968 24240f6a 2022-11-23 thomas goto done;
969 24240f6a 2022-11-23 thomas
970 8a35f56c 2022-07-16 thomas d_disp++;
971 8a35f56c 2022-07-16 thomas t->prev_disp++;
972 8a35f56c 2022-07-16 thomas
973 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
974 55144267 2022-09-07 thomas goto done;
975 55144267 2022-09-07 thomas
976 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
977 8a35f56c 2022-07-16 thomas repo_dir = NULL;
978 8a35f56c 2022-07-16 thomas t->next_disp++;
979 8a35f56c 2022-07-16 thomas if (d_disp == srv->max_repos_display)
980 8a35f56c 2022-07-16 thomas break;
981 8a35f56c 2022-07-16 thomas }
982 24240f6a 2022-11-23 thomas t->repos_total = d_cnt - d_skipped;
983 24240f6a 2022-11-23 thomas
984 8a35f56c 2022-07-16 thomas if (srv->max_repos_display == 0)
985 79393471 2022-08-27 thomas goto done;
986 8a35f56c 2022-07-16 thomas if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
987 79393471 2022-08-27 thomas goto done;
988 8a35f56c 2022-07-16 thomas if (t->repos_total <= srv->max_repos ||
989 8a35f56c 2022-07-16 thomas t->repos_total <= srv->max_repos_display)
990 79393471 2022-08-27 thomas goto done;
991 8a35f56c 2022-07-16 thomas
992 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
993 8a35f56c 2022-07-16 thomas goto done;
994 8a35f56c 2022-07-16 thomas done:
995 f530ab0e 2022-09-02 thomas if (sd_dent) {
996 f530ab0e 2022-09-02 thomas for (d_i = 0; d_i < d_cnt; d_i++)
997 f530ab0e 2022-09-02 thomas free(sd_dent[d_i]);
998 f530ab0e 2022-09-02 thomas free(sd_dent);
999 f530ab0e 2022-09-02 thomas }
1000 8a35f56c 2022-07-16 thomas if (d != NULL && closedir(d) == EOF && error == NULL)
1001 8a35f56c 2022-07-16 thomas error = got_error_from_errno("closedir");
1002 8a35f56c 2022-07-16 thomas return error;
1003 8a35f56c 2022-07-16 thomas }
1004 8a35f56c 2022-07-16 thomas
1005 55144267 2022-09-07 thomas static inline int
1006 55144267 2022-09-07 thomas should_urlencode(int c)
1007 55144267 2022-09-07 thomas {
1008 55144267 2022-09-07 thomas if (c <= ' ' || c >= 127)
1009 55144267 2022-09-07 thomas return 1;
1010 55144267 2022-09-07 thomas
1011 55144267 2022-09-07 thomas switch (c) {
1012 55144267 2022-09-07 thomas /* gen-delim */
1013 55144267 2022-09-07 thomas case ':':
1014 55144267 2022-09-07 thomas case '/':
1015 55144267 2022-09-07 thomas case '?':
1016 55144267 2022-09-07 thomas case '#':
1017 55144267 2022-09-07 thomas case '[':
1018 55144267 2022-09-07 thomas case ']':
1019 55144267 2022-09-07 thomas case '@':
1020 55144267 2022-09-07 thomas /* sub-delims */
1021 55144267 2022-09-07 thomas case '!':
1022 55144267 2022-09-07 thomas case '$':
1023 55144267 2022-09-07 thomas case '&':
1024 55144267 2022-09-07 thomas case '\'':
1025 55144267 2022-09-07 thomas case '(':
1026 55144267 2022-09-07 thomas case ')':
1027 55144267 2022-09-07 thomas case '*':
1028 55144267 2022-09-07 thomas case '+':
1029 55144267 2022-09-07 thomas case ',':
1030 55144267 2022-09-07 thomas case ';':
1031 55144267 2022-09-07 thomas case '=':
1032 e1a9403a 2023-01-06 thomas /* needed because the URLs are embedded into the HTML */
1033 e1a9403a 2023-01-06 thomas case '\"':
1034 55144267 2022-09-07 thomas return 1;
1035 55144267 2022-09-07 thomas default:
1036 55144267 2022-09-07 thomas return 0;
1037 55144267 2022-09-07 thomas }
1038 55144267 2022-09-07 thomas }
1039 55144267 2022-09-07 thomas
1040 55144267 2022-09-07 thomas static char *
1041 55144267 2022-09-07 thomas gotweb_urlencode(const char *str)
1042 55144267 2022-09-07 thomas {
1043 55144267 2022-09-07 thomas const char *s;
1044 55144267 2022-09-07 thomas char *escaped;
1045 55144267 2022-09-07 thomas size_t i, len;
1046 55144267 2022-09-07 thomas int a, b;
1047 55144267 2022-09-07 thomas
1048 55144267 2022-09-07 thomas len = 0;
1049 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
1050 55144267 2022-09-07 thomas len++;
1051 55144267 2022-09-07 thomas if (should_urlencode(*s))
1052 55144267 2022-09-07 thomas len += 2;
1053 55144267 2022-09-07 thomas }
1054 55144267 2022-09-07 thomas
1055 55144267 2022-09-07 thomas escaped = calloc(1, len + 1);
1056 55144267 2022-09-07 thomas if (escaped == NULL)
1057 55144267 2022-09-07 thomas return NULL;
1058 55144267 2022-09-07 thomas
1059 55144267 2022-09-07 thomas i = 0;
1060 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
1061 55144267 2022-09-07 thomas if (should_urlencode(*s)) {
1062 55144267 2022-09-07 thomas a = (*s & 0xF0) >> 4;
1063 55144267 2022-09-07 thomas b = (*s & 0x0F);
1064 55144267 2022-09-07 thomas
1065 55144267 2022-09-07 thomas escaped[i++] = '%';
1066 55144267 2022-09-07 thomas escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1067 55144267 2022-09-07 thomas escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1068 55144267 2022-09-07 thomas } else
1069 55144267 2022-09-07 thomas escaped[i++] = *s;
1070 55144267 2022-09-07 thomas }
1071 55144267 2022-09-07 thomas
1072 55144267 2022-09-07 thomas return escaped;
1073 55144267 2022-09-07 thomas }
1074 55144267 2022-09-07 thomas
1075 e7e5fa49 2022-12-30 thomas const char *
1076 e7e5fa49 2022-12-30 thomas gotweb_action_name(int action)
1077 55144267 2022-09-07 thomas {
1078 55144267 2022-09-07 thomas switch (action) {
1079 55144267 2022-09-07 thomas case BLAME:
1080 55144267 2022-09-07 thomas return "blame";
1081 55144267 2022-09-07 thomas case BLOB:
1082 55144267 2022-09-07 thomas return "blob";
1083 b82440e1 2023-01-06 thomas case BLOBRAW:
1084 b82440e1 2023-01-06 thomas return "blobraw";
1085 55144267 2022-09-07 thomas case BRIEFS:
1086 55144267 2022-09-07 thomas return "briefs";
1087 55144267 2022-09-07 thomas case COMMITS:
1088 55144267 2022-09-07 thomas return "commits";
1089 55144267 2022-09-07 thomas case DIFF:
1090 55144267 2022-09-07 thomas return "diff";
1091 55144267 2022-09-07 thomas case ERR:
1092 55144267 2022-09-07 thomas return "err";
1093 55144267 2022-09-07 thomas case INDEX:
1094 55144267 2022-09-07 thomas return "index";
1095 55144267 2022-09-07 thomas case SUMMARY:
1096 55144267 2022-09-07 thomas return "summary";
1097 55144267 2022-09-07 thomas case TAG:
1098 55144267 2022-09-07 thomas return "tag";
1099 55144267 2022-09-07 thomas case TAGS:
1100 55144267 2022-09-07 thomas return "tags";
1101 55144267 2022-09-07 thomas case TREE:
1102 55144267 2022-09-07 thomas return "tree";
1103 d6795e9f 2022-12-30 thomas case RSS:
1104 d6795e9f 2022-12-30 thomas return "rss";
1105 55144267 2022-09-07 thomas default:
1106 55144267 2022-09-07 thomas return NULL;
1107 55144267 2022-09-07 thomas }
1108 55144267 2022-09-07 thomas }
1109 55144267 2022-09-07 thomas
1110 e7e5fa49 2022-12-30 thomas int
1111 e7e5fa49 2022-12-30 thomas gotweb_render_url(struct request *c, struct gotweb_url *url)
1112 55144267 2022-09-07 thomas {
1113 55144267 2022-09-07 thomas const char *sep = "?", *action;
1114 55144267 2022-09-07 thomas char *tmp;
1115 55144267 2022-09-07 thomas int r;
1116 55144267 2022-09-07 thomas
1117 e7e5fa49 2022-12-30 thomas action = gotweb_action_name(url->action);
1118 55144267 2022-09-07 thomas if (action != NULL) {
1119 55144267 2022-09-07 thomas if (fcgi_printf(c, "?action=%s", action) == -1)
1120 55144267 2022-09-07 thomas return -1;
1121 55144267 2022-09-07 thomas sep = "&";
1122 55144267 2022-09-07 thomas }
1123 55144267 2022-09-07 thomas
1124 55144267 2022-09-07 thomas if (url->commit) {
1125 55144267 2022-09-07 thomas if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1126 55144267 2022-09-07 thomas return -1;
1127 55144267 2022-09-07 thomas sep = "&";
1128 55144267 2022-09-07 thomas }
1129 55144267 2022-09-07 thomas
1130 55144267 2022-09-07 thomas if (url->previd) {
1131 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1132 55144267 2022-09-07 thomas return -1;
1133 55144267 2022-09-07 thomas sep = "&";
1134 55144267 2022-09-07 thomas }
1135 55144267 2022-09-07 thomas
1136 55144267 2022-09-07 thomas if (url->prevset) {
1137 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1138 55144267 2022-09-07 thomas return -1;
1139 55144267 2022-09-07 thomas sep = "&";
1140 55144267 2022-09-07 thomas }
1141 55144267 2022-09-07 thomas
1142 55144267 2022-09-07 thomas if (url->file) {
1143 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->file);
1144 55144267 2022-09-07 thomas if (tmp == NULL)
1145 55144267 2022-09-07 thomas return -1;
1146 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1147 55144267 2022-09-07 thomas free(tmp);
1148 55144267 2022-09-07 thomas if (r == -1)
1149 55144267 2022-09-07 thomas return -1;
1150 55144267 2022-09-07 thomas sep = "&";
1151 55144267 2022-09-07 thomas }
1152 55144267 2022-09-07 thomas
1153 55144267 2022-09-07 thomas if (url->folder) {
1154 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->folder);
1155 55144267 2022-09-07 thomas if (tmp == NULL)
1156 55144267 2022-09-07 thomas return -1;
1157 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1158 55144267 2022-09-07 thomas free(tmp);
1159 55144267 2022-09-07 thomas if (r == -1)
1160 55144267 2022-09-07 thomas return -1;
1161 55144267 2022-09-07 thomas sep = "&";
1162 55144267 2022-09-07 thomas }
1163 55144267 2022-09-07 thomas
1164 55144267 2022-09-07 thomas if (url->headref) {
1165 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->headref);
1166 55144267 2022-09-07 thomas if (tmp == NULL)
1167 55144267 2022-09-07 thomas return -1;
1168 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1169 55144267 2022-09-07 thomas free(tmp);
1170 55144267 2022-09-07 thomas if (r == -1)
1171 55144267 2022-09-07 thomas return -1;
1172 55144267 2022-09-07 thomas sep = "&";
1173 55144267 2022-09-07 thomas }
1174 55144267 2022-09-07 thomas
1175 55144267 2022-09-07 thomas if (url->index_page != -1) {
1176 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sindex_page=%d", sep,
1177 55144267 2022-09-07 thomas url->index_page) == -1)
1178 55144267 2022-09-07 thomas return -1;
1179 55144267 2022-09-07 thomas sep = "&";
1180 55144267 2022-09-07 thomas }
1181 55144267 2022-09-07 thomas
1182 55144267 2022-09-07 thomas if (url->path) {
1183 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->path);
1184 55144267 2022-09-07 thomas if (tmp == NULL)
1185 55144267 2022-09-07 thomas return -1;
1186 55144267 2022-09-07 thomas r = fcgi_printf(c, "%spath=%s", sep, tmp);
1187 55144267 2022-09-07 thomas free(tmp);
1188 55144267 2022-09-07 thomas if (r == -1)
1189 55144267 2022-09-07 thomas return -1;
1190 55144267 2022-09-07 thomas sep = "&";
1191 55144267 2022-09-07 thomas }
1192 55144267 2022-09-07 thomas
1193 55144267 2022-09-07 thomas if (url->page != -1) {
1194 55144267 2022-09-07 thomas if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1195 55144267 2022-09-07 thomas return -1;
1196 55144267 2022-09-07 thomas sep = "&";
1197 55144267 2022-09-07 thomas }
1198 55144267 2022-09-07 thomas
1199 55144267 2022-09-07 thomas return 0;
1200 55144267 2022-09-07 thomas }
1201 55144267 2022-09-07 thomas
1202 55144267 2022-09-07 thomas int
1203 d6795e9f 2022-12-30 thomas gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1204 d6795e9f 2022-12-30 thomas {
1205 d6795e9f 2022-12-30 thomas struct template *tp = c->tp;
1206 d6795e9f 2022-12-30 thomas const char *proto = c->https ? "https" : "http";
1207 d6795e9f 2022-12-30 thomas
1208 d6795e9f 2022-12-30 thomas if (fcgi_puts(tp, proto) == -1 ||
1209 d6795e9f 2022-12-30 thomas fcgi_puts(tp, "://") == -1 ||
1210 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->server_name) == -1 ||
1211 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->document_uri) == -1)
1212 d6795e9f 2022-12-30 thomas return -1;
1213 d6795e9f 2022-12-30 thomas
1214 d6795e9f 2022-12-30 thomas return gotweb_render_url(c, url);
1215 55144267 2022-09-07 thomas }
1216 55144267 2022-09-07 thomas
1217 55e6cffd 2022-09-01 thomas static struct got_repository *
1218 55e6cffd 2022-09-01 thomas find_cached_repo(struct server *srv, const char *path)
1219 55e6cffd 2022-09-01 thomas {
1220 55e6cffd 2022-09-01 thomas int i;
1221 55e6cffd 2022-09-01 thomas
1222 55e6cffd 2022-09-01 thomas for (i = 0; i < srv->ncached_repos; i++) {
1223 55e6cffd 2022-09-01 thomas if (strcmp(srv->cached_repos[i].path, path) == 0)
1224 55e6cffd 2022-09-01 thomas return srv->cached_repos[i].repo;
1225 55e6cffd 2022-09-01 thomas }
1226 55e6cffd 2022-09-01 thomas
1227 55e6cffd 2022-09-01 thomas return NULL;
1228 55e6cffd 2022-09-01 thomas }
1229 55e6cffd 2022-09-01 thomas
1230 8a35f56c 2022-07-16 thomas static const struct got_error *
1231 55e6cffd 2022-09-01 thomas cache_repo(struct got_repository **new, struct server *srv,
1232 55e6cffd 2022-09-01 thomas struct repo_dir *repo_dir, struct socket *sock)
1233 55e6cffd 2022-09-01 thomas {
1234 55e6cffd 2022-09-01 thomas const struct got_error *error = NULL;
1235 55e6cffd 2022-09-01 thomas struct got_repository *repo;
1236 55e6cffd 2022-09-01 thomas struct cached_repo *cr;
1237 55e6cffd 2022-09-01 thomas int evicted = 0;
1238 55e6cffd 2022-09-01 thomas
1239 a004b24a 2022-09-06 thomas if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1240 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos - 1];
1241 55e6cffd 2022-09-01 thomas error = got_repo_close(cr->repo);
1242 55e6cffd 2022-09-01 thomas memset(cr, 0, sizeof(*cr));
1243 55e6cffd 2022-09-01 thomas srv->ncached_repos--;
1244 55e6cffd 2022-09-01 thomas if (error)
1245 55e6cffd 2022-09-01 thomas return error;
1246 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1247 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1248 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[0];
1249 55e6cffd 2022-09-01 thomas evicted = 1;
1250 55e6cffd 2022-09-01 thomas } else {
1251 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos];
1252 55e6cffd 2022-09-01 thomas }
1253 55e6cffd 2022-09-01 thomas
1254 55e6cffd 2022-09-01 thomas error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1255 55e6cffd 2022-09-01 thomas if (error) {
1256 55e6cffd 2022-09-01 thomas if (evicted) {
1257 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1258 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1259 55e6cffd 2022-09-01 thomas }
1260 55e6cffd 2022-09-01 thomas return error;
1261 55e6cffd 2022-09-01 thomas }
1262 55e6cffd 2022-09-01 thomas
1263 55e6cffd 2022-09-01 thomas if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1264 55e6cffd 2022-09-01 thomas >= sizeof(cr->path)) {
1265 55e6cffd 2022-09-01 thomas if (evicted) {
1266 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1267 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1268 55e6cffd 2022-09-01 thomas }
1269 55e6cffd 2022-09-01 thomas return got_error(GOT_ERR_NO_SPACE);
1270 55e6cffd 2022-09-01 thomas }
1271 55e6cffd 2022-09-01 thomas
1272 55e6cffd 2022-09-01 thomas cr->repo = repo;
1273 55e6cffd 2022-09-01 thomas srv->ncached_repos++;
1274 55e6cffd 2022-09-01 thomas *new = repo;
1275 55e6cffd 2022-09-01 thomas return NULL;
1276 55e6cffd 2022-09-01 thomas }
1277 55e6cffd 2022-09-01 thomas
1278 55e6cffd 2022-09-01 thomas static const struct got_error *
1279 8a35f56c 2022-07-16 thomas gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1280 8a35f56c 2022-07-16 thomas {
1281 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1282 8a35f56c 2022-07-16 thomas struct socket *sock = c->sock;
1283 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1284 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1285 55e6cffd 2022-09-01 thomas struct got_repository *repo = NULL;
1286 8a35f56c 2022-07-16 thomas DIR *dt;
1287 8a35f56c 2022-07-16 thomas char *dir_test;
1288 8a35f56c 2022-07-16 thomas
1289 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1290 8a35f56c 2022-07-16 thomas GOTWEB_GIT_DIR) == -1)
1291 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1292 8a35f56c 2022-07-16 thomas
1293 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1294 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1295 8a35f56c 2022-07-16 thomas free(dir_test);
1296 8a35f56c 2022-07-16 thomas } else {
1297 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
1298 8a35f56c 2022-07-16 thomas dir_test = NULL;
1299 c2d3d9a0 2022-09-01 thomas goto done;
1300 8a35f56c 2022-07-16 thomas }
1301 8a35f56c 2022-07-16 thomas
1302 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1303 c2d3d9a0 2022-09-01 thomas repo_dir->name) == -1)
1304 c2d3d9a0 2022-09-01 thomas return got_error_from_errno("asprintf");
1305 8a35f56c 2022-07-16 thomas
1306 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1307 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1308 8a35f56c 2022-07-16 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1309 8a35f56c 2022-07-16 thomas goto err;
1310 c2d3d9a0 2022-09-01 thomas } else {
1311 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
1312 c2d3d9a0 2022-09-01 thomas dir_test = NULL;
1313 c2d3d9a0 2022-09-01 thomas }
1314 c2d3d9a0 2022-09-01 thomas
1315 8a35f56c 2022-07-16 thomas done:
1316 3991b2a5 2022-10-31 thomas if (srv->respect_exportok &&
1317 3991b2a5 2022-10-31 thomas faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1318 3991b2a5 2022-10-31 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1319 3991b2a5 2022-10-31 thomas goto err;
1320 3991b2a5 2022-10-31 thomas }
1321 3991b2a5 2022-10-31 thomas
1322 55e6cffd 2022-09-01 thomas repo = find_cached_repo(srv, repo_dir->path);
1323 55e6cffd 2022-09-01 thomas if (repo == NULL) {
1324 55e6cffd 2022-09-01 thomas error = cache_repo(&repo, srv, repo_dir, sock);
1325 55e6cffd 2022-09-01 thomas if (error)
1326 55e6cffd 2022-09-01 thomas goto err;
1327 55e6cffd 2022-09-01 thomas }
1328 55e6cffd 2022-09-01 thomas t->repo = repo;
1329 8a35f56c 2022-07-16 thomas error = gotweb_get_repo_description(&repo_dir->description, srv,
1330 4606e6d4 2022-11-23 thomas repo_dir->path, dirfd(dt));
1331 8a35f56c 2022-07-16 thomas if (error)
1332 8a35f56c 2022-07-16 thomas goto err;
1333 6c7f10f7 2022-11-23 thomas error = got_get_repo_owner(&repo_dir->owner, c);
1334 8a35f56c 2022-07-16 thomas if (error)
1335 8a35f56c 2022-07-16 thomas goto err;
1336 6c7f10f7 2022-11-23 thomas error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1337 8a35f56c 2022-07-16 thomas if (error)
1338 8a35f56c 2022-07-16 thomas goto err;
1339 4606e6d4 2022-11-23 thomas error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1340 4606e6d4 2022-11-23 thomas dirfd(dt));
1341 8a35f56c 2022-07-16 thomas err:
1342 8a35f56c 2022-07-16 thomas free(dir_test);
1343 c2d3d9a0 2022-09-01 thomas if (dt != NULL && closedir(dt) == EOF && error == NULL)
1344 c2d3d9a0 2022-09-01 thomas error = got_error_from_errno("closedir");
1345 8a35f56c 2022-07-16 thomas return error;
1346 8a35f56c 2022-07-16 thomas }
1347 8a35f56c 2022-07-16 thomas
1348 8a35f56c 2022-07-16 thomas static const struct got_error *
1349 8a35f56c 2022-07-16 thomas gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1350 8a35f56c 2022-07-16 thomas {
1351 8a35f56c 2022-07-16 thomas const struct got_error *error;
1352 8a35f56c 2022-07-16 thomas
1353 8a35f56c 2022-07-16 thomas *repo_dir = calloc(1, sizeof(**repo_dir));
1354 8a35f56c 2022-07-16 thomas if (*repo_dir == NULL)
1355 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
1356 8a35f56c 2022-07-16 thomas
1357 8a35f56c 2022-07-16 thomas if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1358 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1359 8a35f56c 2022-07-16 thomas free(*repo_dir);
1360 8a35f56c 2022-07-16 thomas *repo_dir = NULL;
1361 8a35f56c 2022-07-16 thomas return error;
1362 8a35f56c 2022-07-16 thomas }
1363 8a35f56c 2022-07-16 thomas (*repo_dir)->owner = NULL;
1364 8a35f56c 2022-07-16 thomas (*repo_dir)->description = NULL;
1365 8a35f56c 2022-07-16 thomas (*repo_dir)->url = NULL;
1366 8a35f56c 2022-07-16 thomas (*repo_dir)->age = NULL;
1367 8a35f56c 2022-07-16 thomas (*repo_dir)->path = NULL;
1368 8a35f56c 2022-07-16 thomas
1369 8a35f56c 2022-07-16 thomas return NULL;
1370 8a35f56c 2022-07-16 thomas }
1371 8a35f56c 2022-07-16 thomas
1372 8a35f56c 2022-07-16 thomas static const struct got_error *
1373 4606e6d4 2022-11-23 thomas gotweb_get_repo_description(char **description, struct server *srv,
1374 4606e6d4 2022-11-23 thomas const char *dirpath, int dir)
1375 8a35f56c 2022-07-16 thomas {
1376 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1377 4606e6d4 2022-11-23 thomas struct stat sb;
1378 4606e6d4 2022-11-23 thomas int fd = -1;
1379 4606e6d4 2022-11-23 thomas off_t len;
1380 8a35f56c 2022-07-16 thomas
1381 8a35f56c 2022-07-16 thomas *description = NULL;
1382 8a35f56c 2022-07-16 thomas if (srv->show_repo_description == 0)
1383 8a35f56c 2022-07-16 thomas return NULL;
1384 8a35f56c 2022-07-16 thomas
1385 4606e6d4 2022-11-23 thomas fd = openat(dir, "description", O_RDONLY);
1386 4606e6d4 2022-11-23 thomas if (fd == -1) {
1387 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1388 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1389 4606e6d4 2022-11-23 thomas dirpath, "description");
1390 4606e6d4 2022-11-23 thomas }
1391 8a35f56c 2022-07-16 thomas goto done;
1392 8a35f56c 2022-07-16 thomas }
1393 8a35f56c 2022-07-16 thomas
1394 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1395 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1396 4606e6d4 2022-11-23 thomas dirpath, "description");
1397 8a35f56c 2022-07-16 thomas goto done;
1398 8a35f56c 2022-07-16 thomas }
1399 8a35f56c 2022-07-16 thomas
1400 4606e6d4 2022-11-23 thomas len = sb.st_size;
1401 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXDESCRSZ - 1)
1402 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXDESCRSZ - 1;
1403 8a35f56c 2022-07-16 thomas
1404 8a35f56c 2022-07-16 thomas *description = calloc(len + 1, sizeof(**description));
1405 8a35f56c 2022-07-16 thomas if (*description == NULL) {
1406 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1407 8a35f56c 2022-07-16 thomas goto done;
1408 8a35f56c 2022-07-16 thomas }
1409 8a35f56c 2022-07-16 thomas
1410 4606e6d4 2022-11-23 thomas if (read(fd, *description, len) == -1)
1411 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1412 8a35f56c 2022-07-16 thomas done:
1413 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1414 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1415 8a35f56c 2022-07-16 thomas return error;
1416 8a35f56c 2022-07-16 thomas }
1417 8a35f56c 2022-07-16 thomas
1418 8a35f56c 2022-07-16 thomas static const struct got_error *
1419 4606e6d4 2022-11-23 thomas gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1420 4606e6d4 2022-11-23 thomas int dir)
1421 8a35f56c 2022-07-16 thomas {
1422 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1423 4606e6d4 2022-11-23 thomas struct stat sb;
1424 4606e6d4 2022-11-23 thomas int fd = -1;
1425 4606e6d4 2022-11-23 thomas off_t len;
1426 8a35f56c 2022-07-16 thomas
1427 8a35f56c 2022-07-16 thomas *url = NULL;
1428 8a35f56c 2022-07-16 thomas if (srv->show_repo_cloneurl == 0)
1429 8a35f56c 2022-07-16 thomas return NULL;
1430 8a35f56c 2022-07-16 thomas
1431 4606e6d4 2022-11-23 thomas fd = openat(dir, "cloneurl", O_RDONLY);
1432 4606e6d4 2022-11-23 thomas if (fd == -1) {
1433 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1434 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1435 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1436 4606e6d4 2022-11-23 thomas }
1437 8a35f56c 2022-07-16 thomas goto done;
1438 8a35f56c 2022-07-16 thomas }
1439 8a35f56c 2022-07-16 thomas
1440 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1441 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1442 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1443 8a35f56c 2022-07-16 thomas goto done;
1444 8a35f56c 2022-07-16 thomas }
1445 8a35f56c 2022-07-16 thomas
1446 4606e6d4 2022-11-23 thomas len = sb.st_size;
1447 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1448 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXCLONEURLSZ - 1;
1449 8a35f56c 2022-07-16 thomas
1450 8a35f56c 2022-07-16 thomas *url = calloc(len + 1, sizeof(**url));
1451 8a35f56c 2022-07-16 thomas if (*url == NULL) {
1452 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1453 8a35f56c 2022-07-16 thomas goto done;
1454 8a35f56c 2022-07-16 thomas }
1455 8a35f56c 2022-07-16 thomas
1456 4606e6d4 2022-11-23 thomas if (read(fd, *url, len) == -1)
1457 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1458 8a35f56c 2022-07-16 thomas done:
1459 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1460 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1461 8a35f56c 2022-07-16 thomas return error;
1462 8a35f56c 2022-07-16 thomas }
1463 8a35f56c 2022-07-16 thomas
1464 8a35f56c 2022-07-16 thomas const struct got_error *
1465 8a35f56c 2022-07-16 thomas gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1466 8a35f56c 2022-07-16 thomas {
1467 8a35f56c 2022-07-16 thomas struct tm tm;
1468 399ea8e4 2022-07-20 thomas long long diff_time;
1469 8a35f56c 2022-07-16 thomas const char *years = "years ago", *months = "months ago";
1470 8a35f56c 2022-07-16 thomas const char *weeks = "weeks ago", *days = "days ago";
1471 8a35f56c 2022-07-16 thomas const char *hours = "hours ago", *minutes = "minutes ago";
1472 8a35f56c 2022-07-16 thomas const char *seconds = "seconds ago", *now = "right now";
1473 8a35f56c 2022-07-16 thomas char *s;
1474 d6795e9f 2022-12-30 thomas char datebuf[64];
1475 d6795e9f 2022-12-30 thomas size_t r;
1476 8a35f56c 2022-07-16 thomas
1477 8a35f56c 2022-07-16 thomas *repo_age = NULL;
1478 8a35f56c 2022-07-16 thomas
1479 8a35f56c 2022-07-16 thomas switch (ref_tm) {
1480 8a35f56c 2022-07-16 thomas case TM_DIFF:
1481 8a35f56c 2022-07-16 thomas diff_time = time(NULL) - committer_time;
1482 8a35f56c 2022-07-16 thomas if (diff_time > 60 * 60 * 24 * 365 * 2) {
1483 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
1484 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 365), years) == -1)
1485 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1486 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1487 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
1488 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / (365 / 12)),
1489 8a35f56c 2022-07-16 thomas months) == -1)
1490 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1491 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1492 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
1493 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1494 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1495 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 2) {
1496 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
1497 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24), days) == -1)
1498 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1499 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 2) {
1500 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
1501 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60), hours) == -1)
1502 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1503 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 2) {
1504 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1505 8a35f56c 2022-07-16 thomas minutes) == -1)
1506 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1507 8a35f56c 2022-07-16 thomas } else if (diff_time > 2) {
1508 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", diff_time,
1509 8a35f56c 2022-07-16 thomas seconds) == -1)
1510 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1511 8a35f56c 2022-07-16 thomas } else {
1512 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s", now) == -1)
1513 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1514 8a35f56c 2022-07-16 thomas }
1515 8a35f56c 2022-07-16 thomas break;
1516 8a35f56c 2022-07-16 thomas case TM_LONG:
1517 8a35f56c 2022-07-16 thomas if (gmtime_r(&committer_time, &tm) == NULL)
1518 8a35f56c 2022-07-16 thomas return got_error_from_errno("gmtime_r");
1519 8a35f56c 2022-07-16 thomas
1520 8a35f56c 2022-07-16 thomas s = asctime_r(&tm, datebuf);
1521 8a35f56c 2022-07-16 thomas if (s == NULL)
1522 8a35f56c 2022-07-16 thomas return got_error_from_errno("asctime_r");
1523 8a35f56c 2022-07-16 thomas
1524 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1525 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1526 8a35f56c 2022-07-16 thomas break;
1527 d6795e9f 2022-12-30 thomas case TM_RFC822:
1528 d6795e9f 2022-12-30 thomas if (gmtime_r(&committer_time, &tm) == NULL)
1529 d6795e9f 2022-12-30 thomas return got_error_from_errno("gmtime_r");
1530 d6795e9f 2022-12-30 thomas
1531 d6795e9f 2022-12-30 thomas r = strftime(datebuf, sizeof(datebuf),
1532 d6795e9f 2022-12-30 thomas "%a, %d %b %Y %H:%M:%S GMT", &tm);
1533 d6795e9f 2022-12-30 thomas if (r == 0)
1534 d6795e9f 2022-12-30 thomas return got_error(GOT_ERR_NO_SPACE);
1535 d6795e9f 2022-12-30 thomas
1536 d6795e9f 2022-12-30 thomas *repo_age = strdup(datebuf);
1537 d6795e9f 2022-12-30 thomas if (*repo_age == NULL)
1538 d6795e9f 2022-12-30 thomas return got_error_from_errno("asprintf");
1539 d6795e9f 2022-12-30 thomas break;
1540 8a35f56c 2022-07-16 thomas }
1541 8a35f56c 2022-07-16 thomas return NULL;
1542 3e12c168 2022-07-16 thomas }