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