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