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