Blame


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