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