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