Blame


1 3efd8e31 2022-10-23 thomas /*
2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
4 3efd8e31 2022-10-23 thomas * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 3efd8e31 2022-10-23 thomas * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 3efd8e31 2022-10-23 thomas * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 3efd8e31 2022-10-23 thomas * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 3efd8e31 2022-10-23 thomas * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 3efd8e31 2022-10-23 thomas * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 3efd8e31 2022-10-23 thomas *
11 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
12 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
13 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
14 3efd8e31 2022-10-23 thomas *
15 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 3efd8e31 2022-10-23 thomas */
23 3efd8e31 2022-10-23 thomas
24 3efd8e31 2022-10-23 thomas %{
25 3efd8e31 2022-10-23 thomas #include <sys/time.h>
26 3efd8e31 2022-10-23 thomas #include <sys/types.h>
27 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
28 3efd8e31 2022-10-23 thomas #include <sys/stat.h>
29 3efd8e31 2022-10-23 thomas
30 3efd8e31 2022-10-23 thomas #include <ctype.h>
31 3efd8e31 2022-10-23 thomas #include <err.h>
32 3efd8e31 2022-10-23 thomas #include <errno.h>
33 3efd8e31 2022-10-23 thomas #include <event.h>
34 3efd8e31 2022-10-23 thomas #include <imsg.h>
35 3efd8e31 2022-10-23 thomas #include <limits.h>
36 3efd8e31 2022-10-23 thomas #include <stdarg.h>
37 3efd8e31 2022-10-23 thomas #include <stdlib.h>
38 3efd8e31 2022-10-23 thomas #include <stdio.h>
39 3efd8e31 2022-10-23 thomas #include <string.h>
40 3efd8e31 2022-10-23 thomas #include <syslog.h>
41 3efd8e31 2022-10-23 thomas #include <unistd.h>
42 3efd8e31 2022-10-23 thomas
43 3efd8e31 2022-10-23 thomas #include "got_error.h"
44 3efd8e31 2022-10-23 thomas #include "got_path.h"
45 6d7eb4f7 2023-04-04 thomas #include "got_reference.h"
46 3efd8e31 2022-10-23 thomas
47 3efd8e31 2022-10-23 thomas #include "log.h"
48 3efd8e31 2022-10-23 thomas #include "gotd.h"
49 0781db0e 2023-01-06 thomas #include "auth.h"
50 0781db0e 2023-01-06 thomas #include "listen.h"
51 3efd8e31 2022-10-23 thomas
52 3efd8e31 2022-10-23 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
53 3efd8e31 2022-10-23 thomas static struct file {
54 3efd8e31 2022-10-23 thomas TAILQ_ENTRY(file) entry;
55 3efd8e31 2022-10-23 thomas FILE *stream;
56 3efd8e31 2022-10-23 thomas char *name;
57 3efd8e31 2022-10-23 thomas int lineno;
58 3efd8e31 2022-10-23 thomas int errors;
59 3efd8e31 2022-10-23 thomas } *file;
60 7554713a 2023-04-01 thomas struct file *newfile(const char *, int, int);
61 3efd8e31 2022-10-23 thomas static void closefile(struct file *);
62 3efd8e31 2022-10-23 thomas int check_file_secrecy(int, const char *);
63 3efd8e31 2022-10-23 thomas int yyparse(void);
64 3efd8e31 2022-10-23 thomas int yylex(void);
65 3efd8e31 2022-10-23 thomas int yyerror(const char *, ...)
66 3efd8e31 2022-10-23 thomas __attribute__((__format__ (printf, 1, 2)))
67 3efd8e31 2022-10-23 thomas __attribute__((__nonnull__ (1)));
68 3efd8e31 2022-10-23 thomas int kw_cmp(const void *, const void *);
69 3efd8e31 2022-10-23 thomas int lookup(char *);
70 3efd8e31 2022-10-23 thomas int lgetc(int);
71 3efd8e31 2022-10-23 thomas int lungetc(int);
72 3efd8e31 2022-10-23 thomas int findeol(void);
73 3efd8e31 2022-10-23 thomas
74 3efd8e31 2022-10-23 thomas TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 3efd8e31 2022-10-23 thomas struct sym {
76 3efd8e31 2022-10-23 thomas TAILQ_ENTRY(sym) entry;
77 3efd8e31 2022-10-23 thomas int used;
78 3efd8e31 2022-10-23 thomas int persist;
79 3efd8e31 2022-10-23 thomas char *nam;
80 3efd8e31 2022-10-23 thomas char *val;
81 3efd8e31 2022-10-23 thomas };
82 3efd8e31 2022-10-23 thomas
83 3efd8e31 2022-10-23 thomas int symset(const char *, const char *, int);
84 3efd8e31 2022-10-23 thomas char *symget(const char *);
85 3efd8e31 2022-10-23 thomas
86 3efd8e31 2022-10-23 thomas static int errors;
87 3efd8e31 2022-10-23 thomas
88 3efd8e31 2022-10-23 thomas static struct gotd *gotd;
89 3efd8e31 2022-10-23 thomas static struct gotd_repo *new_repo;
90 67f822ee 2023-01-06 thomas static int conf_limit_user_connections(const char *, int);
91 3efd8e31 2022-10-23 thomas static struct gotd_repo *conf_new_repo(const char *);
92 c3841c67 2022-11-18 thomas static void conf_new_access_rule(struct gotd_repo *,
93 c3841c67 2022-11-18 thomas enum gotd_access, int, char *);
94 6d7eb4f7 2023-04-04 thomas static int conf_protect_ref_namespace(
95 6d7eb4f7 2023-04-04 thomas struct got_pathlist_head *, char *);
96 6d7eb4f7 2023-04-04 thomas static int conf_protect_tag_namespace(struct gotd_repo *,
97 6d7eb4f7 2023-04-04 thomas char *);
98 6d7eb4f7 2023-04-04 thomas static int conf_protect_branch_namespace(
99 6d7eb4f7 2023-04-04 thomas struct gotd_repo *, char *);
100 6d7eb4f7 2023-04-04 thomas static int conf_protect_branch(struct gotd_repo *,
101 6d7eb4f7 2023-04-04 thomas char *);
102 3efd8e31 2022-10-23 thomas static enum gotd_procid gotd_proc_id;
103 3efd8e31 2022-10-23 thomas
104 3efd8e31 2022-10-23 thomas typedef struct {
105 3efd8e31 2022-10-23 thomas union {
106 3efd8e31 2022-10-23 thomas long long number;
107 3efd8e31 2022-10-23 thomas char *string;
108 0781db0e 2023-01-06 thomas struct timeval tv;
109 3efd8e31 2022-10-23 thomas } v;
110 3efd8e31 2022-10-23 thomas int lineno;
111 3efd8e31 2022-10-23 thomas } YYSTYPE;
112 3efd8e31 2022-10-23 thomas
113 3efd8e31 2022-10-23 thomas %}
114 3efd8e31 2022-10-23 thomas
115 f9a4feb6 2023-01-06 thomas %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
116 0781db0e 2023-01-06 thomas %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
117 6d7eb4f7 2023-04-04 thomas %token PROTECT NAMESPACE BRANCH TAG
118 3efd8e31 2022-10-23 thomas
119 3efd8e31 2022-10-23 thomas %token <v.string> STRING
120 3efd8e31 2022-10-23 thomas %token <v.number> NUMBER
121 0781db0e 2023-01-06 thomas %type <v.tv> timeout
122 3efd8e31 2022-10-23 thomas
123 3efd8e31 2022-10-23 thomas %%
124 3efd8e31 2022-10-23 thomas
125 3efd8e31 2022-10-23 thomas grammar :
126 3efd8e31 2022-10-23 thomas | grammar '\n'
127 3efd8e31 2022-10-23 thomas | grammar main '\n'
128 3efd8e31 2022-10-23 thomas | grammar repository '\n'
129 3efd8e31 2022-10-23 thomas ;
130 3efd8e31 2022-10-23 thomas
131 0781db0e 2023-01-06 thomas timeout : NUMBER {
132 0781db0e 2023-01-06 thomas if ($1 < 0) {
133 0781db0e 2023-01-06 thomas yyerror("invalid timeout: %lld", $1);
134 0781db0e 2023-01-06 thomas YYERROR;
135 0781db0e 2023-01-06 thomas }
136 0781db0e 2023-01-06 thomas $$.tv_sec = $1;
137 0781db0e 2023-01-06 thomas $$.tv_usec = 0;
138 0781db0e 2023-01-06 thomas }
139 77d755e8 2023-01-06 thomas | STRING {
140 77d755e8 2023-01-06 thomas const char *errstr;
141 77d755e8 2023-01-06 thomas const char *type = "seconds";
142 77d755e8 2023-01-06 thomas size_t len;
143 77d755e8 2023-01-06 thomas int mul = 1;
144 77d755e8 2023-01-06 thomas
145 77d755e8 2023-01-06 thomas if (*$1 == '\0') {
146 77d755e8 2023-01-06 thomas yyerror("invalid number of seconds: %s", $1);
147 77d755e8 2023-01-06 thomas free($1);
148 77d755e8 2023-01-06 thomas YYERROR;
149 77d755e8 2023-01-06 thomas }
150 77d755e8 2023-01-06 thomas
151 77d755e8 2023-01-06 thomas len = strlen($1);
152 77d755e8 2023-01-06 thomas switch ($1[len - 1]) {
153 77d755e8 2023-01-06 thomas case 'S':
154 77d755e8 2023-01-06 thomas case 's':
155 77d755e8 2023-01-06 thomas $1[len - 1] = '\0';
156 77d755e8 2023-01-06 thomas break;
157 77d755e8 2023-01-06 thomas case 'M':
158 77d755e8 2023-01-06 thomas case 'm':
159 77d755e8 2023-01-06 thomas type = "minutes";
160 77d755e8 2023-01-06 thomas mul = 60;
161 77d755e8 2023-01-06 thomas $1[len - 1] = '\0';
162 77d755e8 2023-01-06 thomas break;
163 77d755e8 2023-01-06 thomas case 'H':
164 77d755e8 2023-01-06 thomas case 'h':
165 77d755e8 2023-01-06 thomas type = "hours";
166 77d755e8 2023-01-06 thomas mul = 60 * 60;
167 77d755e8 2023-01-06 thomas $1[len - 1] = '\0';
168 77d755e8 2023-01-06 thomas break;
169 77d755e8 2023-01-06 thomas }
170 77d755e8 2023-01-06 thomas
171 77d755e8 2023-01-06 thomas $$.tv_usec = 0;
172 85bccbf7 2023-01-06 thomas $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
173 77d755e8 2023-01-06 thomas if (errstr) {
174 77d755e8 2023-01-06 thomas yyerror("number of %s is %s: %s", type,
175 77d755e8 2023-01-06 thomas errstr, $1);
176 77d755e8 2023-01-06 thomas free($1);
177 77d755e8 2023-01-06 thomas YYERROR;
178 77d755e8 2023-01-06 thomas }
179 77d755e8 2023-01-06 thomas
180 77d755e8 2023-01-06 thomas $$.tv_sec *= mul;
181 77d755e8 2023-01-06 thomas free($1);
182 77d755e8 2023-01-06 thomas }
183 0781db0e 2023-01-06 thomas ;
184 0781db0e 2023-01-06 thomas
185 f9a4feb6 2023-01-06 thomas main : LISTEN ON STRING {
186 7348ded8 2023-01-19 thomas if (!got_path_is_absolute($3))
187 7348ded8 2023-01-19 thomas yyerror("bad unix socket path \"%s\": "
188 7348ded8 2023-01-19 thomas "must be an absolute path", $3);
189 7348ded8 2023-01-19 thomas
190 2b3d32a1 2022-12-30 thomas if (gotd_proc_id == PROC_LISTEN) {
191 f9a4feb6 2023-01-06 thomas if (strlcpy(gotd->unix_socket_path, $3,
192 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) >=
193 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) {
194 3efd8e31 2022-10-23 thomas yyerror("%s: unix socket path too long",
195 3efd8e31 2022-10-23 thomas __func__);
196 f9a4feb6 2023-01-06 thomas free($3);
197 3efd8e31 2022-10-23 thomas YYERROR;
198 3efd8e31 2022-10-23 thomas }
199 3efd8e31 2022-10-23 thomas }
200 f9a4feb6 2023-01-06 thomas free($3);
201 3efd8e31 2022-10-23 thomas }
202 3efd8e31 2022-10-23 thomas | USER STRING {
203 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->user_name, $2,
204 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) >=
205 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) {
206 3efd8e31 2022-10-23 thomas yyerror("%s: user name too long", __func__);
207 3efd8e31 2022-10-23 thomas free($2);
208 3efd8e31 2022-10-23 thomas YYERROR;
209 3efd8e31 2022-10-23 thomas }
210 3efd8e31 2022-10-23 thomas free($2);
211 3efd8e31 2022-10-23 thomas }
212 0781db0e 2023-01-06 thomas | connection
213 3efd8e31 2022-10-23 thomas ;
214 3efd8e31 2022-10-23 thomas
215 0781db0e 2023-01-06 thomas connection : CONNECTION '{' optnl conflags_l '}'
216 0781db0e 2023-01-06 thomas | CONNECTION conflags
217 0781db0e 2023-01-06 thomas
218 0781db0e 2023-01-06 thomas conflags_l : conflags optnl conflags_l
219 0781db0e 2023-01-06 thomas | conflags optnl
220 0781db0e 2023-01-06 thomas ;
221 0781db0e 2023-01-06 thomas
222 0781db0e 2023-01-06 thomas conflags : REQUEST TIMEOUT timeout {
223 912db690 2023-01-06 thomas if ($3.tv_sec <= 0) {
224 912db690 2023-01-06 thomas yyerror("invalid timeout: %lld", $3.tv_sec);
225 912db690 2023-01-06 thomas YYERROR;
226 912db690 2023-01-06 thomas }
227 0781db0e 2023-01-06 thomas memcpy(&gotd->request_timeout, &$3,
228 0781db0e 2023-01-06 thomas sizeof(gotd->request_timeout));
229 0781db0e 2023-01-06 thomas }
230 0781db0e 2023-01-06 thomas | LIMIT USER STRING NUMBER {
231 0781db0e 2023-01-06 thomas if (gotd_proc_id == PROC_LISTEN &&
232 0781db0e 2023-01-06 thomas conf_limit_user_connections($3, $4) == -1) {
233 0781db0e 2023-01-06 thomas free($3);
234 0781db0e 2023-01-06 thomas YYERROR;
235 0781db0e 2023-01-06 thomas }
236 0781db0e 2023-01-06 thomas free($3);
237 0781db0e 2023-01-06 thomas }
238 0781db0e 2023-01-06 thomas ;
239 0781db0e 2023-01-06 thomas
240 6d7eb4f7 2023-04-04 thomas protect : PROTECT '{' optnl protectflags_l '}'
241 6d7eb4f7 2023-04-04 thomas | PROTECT protectflags
242 6d7eb4f7 2023-04-04 thomas
243 6d7eb4f7 2023-04-04 thomas protectflags_l : protectflags optnl protectflags_l
244 6d7eb4f7 2023-04-04 thomas | protectflags optnl
245 6d7eb4f7 2023-04-04 thomas ;
246 6d7eb4f7 2023-04-04 thomas
247 6d7eb4f7 2023-04-04 thomas protectflags : TAG NAMESPACE STRING {
248 6d7eb4f7 2023-04-04 thomas if (gotd_proc_id == PROC_GOTD ||
249 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
250 6d7eb4f7 2023-04-04 thomas if (conf_protect_tag_namespace(new_repo, $3)) {
251 6d7eb4f7 2023-04-04 thomas free($3);
252 6d7eb4f7 2023-04-04 thomas YYERROR;
253 6d7eb4f7 2023-04-04 thomas }
254 6d7eb4f7 2023-04-04 thomas }
255 6d7eb4f7 2023-04-04 thomas }
256 6d7eb4f7 2023-04-04 thomas | BRANCH NAMESPACE STRING {
257 6d7eb4f7 2023-04-04 thomas if (gotd_proc_id == PROC_GOTD ||
258 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
259 6d7eb4f7 2023-04-04 thomas if (conf_protect_branch_namespace(new_repo,
260 6d7eb4f7 2023-04-04 thomas $3)) {
261 6d7eb4f7 2023-04-04 thomas free($3);
262 6d7eb4f7 2023-04-04 thomas YYERROR;
263 6d7eb4f7 2023-04-04 thomas }
264 6d7eb4f7 2023-04-04 thomas free($3);
265 6d7eb4f7 2023-04-04 thomas }
266 6d7eb4f7 2023-04-04 thomas }
267 6d7eb4f7 2023-04-04 thomas | BRANCH STRING {
268 6d7eb4f7 2023-04-04 thomas if (gotd_proc_id == PROC_GOTD ||
269 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
270 6d7eb4f7 2023-04-04 thomas if (conf_protect_branch(new_repo, $2)) {
271 6d7eb4f7 2023-04-04 thomas free($2);
272 6d7eb4f7 2023-04-04 thomas YYERROR;
273 6d7eb4f7 2023-04-04 thomas }
274 6d7eb4f7 2023-04-04 thomas }
275 6d7eb4f7 2023-04-04 thomas }
276 6d7eb4f7 2023-04-04 thomas ;
277 6d7eb4f7 2023-04-04 thomas
278 3efd8e31 2022-10-23 thomas repository : REPOSITORY STRING {
279 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
280 3efd8e31 2022-10-23 thomas
281 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
282 3efd8e31 2022-10-23 thomas if (strcmp(repo->name, $2) == 0) {
283 3efd8e31 2022-10-23 thomas yyerror("duplicate repository '%s'", $2);
284 3efd8e31 2022-10-23 thomas free($2);
285 3efd8e31 2022-10-23 thomas YYERROR;
286 3efd8e31 2022-10-23 thomas }
287 3efd8e31 2022-10-23 thomas }
288 3efd8e31 2022-10-23 thomas
289 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_GOTD ||
290 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_AUTH ||
291 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
292 3efd8e31 2022-10-23 thomas new_repo = conf_new_repo($2);
293 3efd8e31 2022-10-23 thomas }
294 3efd8e31 2022-10-23 thomas free($2);
295 3efd8e31 2022-10-23 thomas } '{' optnl repoopts2 '}' {
296 3efd8e31 2022-10-23 thomas }
297 3efd8e31 2022-10-23 thomas ;
298 3efd8e31 2022-10-23 thomas
299 3efd8e31 2022-10-23 thomas repoopts1 : PATH STRING {
300 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_GOTD ||
301 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_AUTH ||
302 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
303 3efd8e31 2022-10-23 thomas if (!got_path_is_absolute($2)) {
304 3efd8e31 2022-10-23 thomas yyerror("%s: path %s is not absolute",
305 3efd8e31 2022-10-23 thomas __func__, $2);
306 3efd8e31 2022-10-23 thomas free($2);
307 3efd8e31 2022-10-23 thomas YYERROR;
308 3efd8e31 2022-10-23 thomas }
309 fe6a8988 2023-01-08 thomas if (realpath($2, new_repo->path) == NULL) {
310 fe6a8988 2023-01-08 thomas yyerror("realpath %s: %s", $2, strerror(errno));
311 3efd8e31 2022-10-23 thomas free($2);
312 3efd8e31 2022-10-23 thomas YYERROR;
313 3efd8e31 2022-10-23 thomas }
314 3efd8e31 2022-10-23 thomas }
315 3efd8e31 2022-10-23 thomas free($2);
316 729a7e24 2022-11-17 thomas }
317 729a7e24 2022-11-17 thomas | PERMIT RO STRING {
318 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
319 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
320 729a7e24 2022-11-17 thomas GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
321 729a7e24 2022-11-17 thomas }
322 729a7e24 2022-11-17 thomas }
323 729a7e24 2022-11-17 thomas | PERMIT RW STRING {
324 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
325 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
326 729a7e24 2022-11-17 thomas GOTD_ACCESS_PERMITTED,
327 729a7e24 2022-11-17 thomas GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
328 729a7e24 2022-11-17 thomas }
329 3efd8e31 2022-10-23 thomas }
330 729a7e24 2022-11-17 thomas | DENY STRING {
331 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
332 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
333 729a7e24 2022-11-17 thomas GOTD_ACCESS_DENIED, 0, $2);
334 729a7e24 2022-11-17 thomas }
335 729a7e24 2022-11-17 thomas }
336 6d7eb4f7 2023-04-04 thomas | protect
337 3efd8e31 2022-10-23 thomas ;
338 3efd8e31 2022-10-23 thomas
339 3efd8e31 2022-10-23 thomas repoopts2 : repoopts2 repoopts1 nl
340 3efd8e31 2022-10-23 thomas | repoopts1 optnl
341 3efd8e31 2022-10-23 thomas ;
342 3efd8e31 2022-10-23 thomas
343 3efd8e31 2022-10-23 thomas nl : '\n' optnl
344 3efd8e31 2022-10-23 thomas ;
345 3efd8e31 2022-10-23 thomas
346 3efd8e31 2022-10-23 thomas optnl : '\n' optnl /* zero or more newlines */
347 3efd8e31 2022-10-23 thomas | /* empty */
348 3efd8e31 2022-10-23 thomas ;
349 3efd8e31 2022-10-23 thomas
350 3efd8e31 2022-10-23 thomas %%
351 3efd8e31 2022-10-23 thomas
352 3efd8e31 2022-10-23 thomas struct keywords {
353 3efd8e31 2022-10-23 thomas const char *k_name;
354 3efd8e31 2022-10-23 thomas int k_val;
355 3efd8e31 2022-10-23 thomas };
356 3efd8e31 2022-10-23 thomas
357 3efd8e31 2022-10-23 thomas int
358 3efd8e31 2022-10-23 thomas yyerror(const char *fmt, ...)
359 3efd8e31 2022-10-23 thomas {
360 3efd8e31 2022-10-23 thomas va_list ap;
361 3efd8e31 2022-10-23 thomas char *msg;
362 3efd8e31 2022-10-23 thomas
363 3efd8e31 2022-10-23 thomas file->errors++;
364 3efd8e31 2022-10-23 thomas va_start(ap, fmt);
365 3efd8e31 2022-10-23 thomas if (vasprintf(&msg, fmt, ap) == -1)
366 3efd8e31 2022-10-23 thomas fatalx("yyerror vasprintf");
367 3efd8e31 2022-10-23 thomas va_end(ap);
368 3efd8e31 2022-10-23 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
369 3efd8e31 2022-10-23 thomas free(msg);
370 3efd8e31 2022-10-23 thomas return (0);
371 3efd8e31 2022-10-23 thomas }
372 3efd8e31 2022-10-23 thomas
373 3efd8e31 2022-10-23 thomas int
374 3efd8e31 2022-10-23 thomas kw_cmp(const void *k, const void *e)
375 3efd8e31 2022-10-23 thomas {
376 3efd8e31 2022-10-23 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
377 3efd8e31 2022-10-23 thomas }
378 3efd8e31 2022-10-23 thomas
379 3efd8e31 2022-10-23 thomas int
380 3efd8e31 2022-10-23 thomas lookup(char *s)
381 3efd8e31 2022-10-23 thomas {
382 3efd8e31 2022-10-23 thomas /* This has to be sorted always. */
383 3efd8e31 2022-10-23 thomas static const struct keywords keywords[] = {
384 6d7eb4f7 2023-04-04 thomas { "branch", BRANCH },
385 0781db0e 2023-01-06 thomas { "connection", CONNECTION },
386 729a7e24 2022-11-17 thomas { "deny", DENY },
387 0781db0e 2023-01-06 thomas { "limit", LIMIT },
388 f9a4feb6 2023-01-06 thomas { "listen", LISTEN },
389 6d7eb4f7 2023-04-04 thomas { "namespace", NAMESPACE },
390 3efd8e31 2022-10-23 thomas { "on", ON },
391 3efd8e31 2022-10-23 thomas { "path", PATH },
392 729a7e24 2022-11-17 thomas { "permit", PERMIT },
393 6d7eb4f7 2023-04-04 thomas { "protect", PROTECT },
394 3efd8e31 2022-10-23 thomas { "repository", REPOSITORY },
395 0781db0e 2023-01-06 thomas { "request", REQUEST },
396 729a7e24 2022-11-17 thomas { "ro", RO },
397 729a7e24 2022-11-17 thomas { "rw", RW },
398 6d7eb4f7 2023-04-04 thomas { "tag", TAG },
399 0781db0e 2023-01-06 thomas { "timeout", TIMEOUT },
400 3efd8e31 2022-10-23 thomas { "user", USER },
401 3efd8e31 2022-10-23 thomas };
402 3efd8e31 2022-10-23 thomas const struct keywords *p;
403 3efd8e31 2022-10-23 thomas
404 3efd8e31 2022-10-23 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
405 3efd8e31 2022-10-23 thomas sizeof(keywords[0]), kw_cmp);
406 3efd8e31 2022-10-23 thomas
407 3efd8e31 2022-10-23 thomas if (p)
408 3efd8e31 2022-10-23 thomas return (p->k_val);
409 3efd8e31 2022-10-23 thomas else
410 3efd8e31 2022-10-23 thomas return (STRING);
411 3efd8e31 2022-10-23 thomas }
412 3efd8e31 2022-10-23 thomas
413 3efd8e31 2022-10-23 thomas #define MAXPUSHBACK 128
414 3efd8e31 2022-10-23 thomas
415 3efd8e31 2022-10-23 thomas unsigned char *parsebuf;
416 3efd8e31 2022-10-23 thomas int parseindex;
417 3efd8e31 2022-10-23 thomas unsigned char pushback_buffer[MAXPUSHBACK];
418 3efd8e31 2022-10-23 thomas int pushback_index = 0;
419 3efd8e31 2022-10-23 thomas
420 3efd8e31 2022-10-23 thomas int
421 3efd8e31 2022-10-23 thomas lgetc(int quotec)
422 3efd8e31 2022-10-23 thomas {
423 3efd8e31 2022-10-23 thomas int c, next;
424 3efd8e31 2022-10-23 thomas
425 3efd8e31 2022-10-23 thomas if (parsebuf) {
426 3efd8e31 2022-10-23 thomas /* Read character from the parsebuffer instead of input. */
427 3efd8e31 2022-10-23 thomas if (parseindex >= 0) {
428 3efd8e31 2022-10-23 thomas c = parsebuf[parseindex++];
429 3efd8e31 2022-10-23 thomas if (c != '\0')
430 3efd8e31 2022-10-23 thomas return (c);
431 3efd8e31 2022-10-23 thomas parsebuf = NULL;
432 3efd8e31 2022-10-23 thomas } else
433 3efd8e31 2022-10-23 thomas parseindex++;
434 3efd8e31 2022-10-23 thomas }
435 3efd8e31 2022-10-23 thomas
436 3efd8e31 2022-10-23 thomas if (pushback_index)
437 3efd8e31 2022-10-23 thomas return (pushback_buffer[--pushback_index]);
438 3efd8e31 2022-10-23 thomas
439 3efd8e31 2022-10-23 thomas if (quotec) {
440 3efd8e31 2022-10-23 thomas c = getc(file->stream);
441 3efd8e31 2022-10-23 thomas if (c == EOF)
442 3efd8e31 2022-10-23 thomas yyerror("reached end of file while parsing "
443 3efd8e31 2022-10-23 thomas "quoted string");
444 3efd8e31 2022-10-23 thomas return (c);
445 3efd8e31 2022-10-23 thomas }
446 3efd8e31 2022-10-23 thomas
447 3efd8e31 2022-10-23 thomas c = getc(file->stream);
448 3efd8e31 2022-10-23 thomas while (c == '\\') {
449 3efd8e31 2022-10-23 thomas next = getc(file->stream);
450 3efd8e31 2022-10-23 thomas if (next != '\n') {
451 3efd8e31 2022-10-23 thomas c = next;
452 3efd8e31 2022-10-23 thomas break;
453 3efd8e31 2022-10-23 thomas }
454 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
455 3efd8e31 2022-10-23 thomas file->lineno++;
456 3efd8e31 2022-10-23 thomas c = getc(file->stream);
457 3efd8e31 2022-10-23 thomas }
458 3efd8e31 2022-10-23 thomas
459 3efd8e31 2022-10-23 thomas return (c);
460 3efd8e31 2022-10-23 thomas }
461 3efd8e31 2022-10-23 thomas
462 3efd8e31 2022-10-23 thomas int
463 3efd8e31 2022-10-23 thomas lungetc(int c)
464 3efd8e31 2022-10-23 thomas {
465 3efd8e31 2022-10-23 thomas if (c == EOF)
466 3efd8e31 2022-10-23 thomas return (EOF);
467 3efd8e31 2022-10-23 thomas if (parsebuf) {
468 3efd8e31 2022-10-23 thomas parseindex--;
469 3efd8e31 2022-10-23 thomas if (parseindex >= 0)
470 3efd8e31 2022-10-23 thomas return (c);
471 3efd8e31 2022-10-23 thomas }
472 3efd8e31 2022-10-23 thomas if (pushback_index < MAXPUSHBACK-1)
473 3efd8e31 2022-10-23 thomas return (pushback_buffer[pushback_index++] = c);
474 3efd8e31 2022-10-23 thomas else
475 3efd8e31 2022-10-23 thomas return (EOF);
476 3efd8e31 2022-10-23 thomas }
477 3efd8e31 2022-10-23 thomas
478 3efd8e31 2022-10-23 thomas int
479 3efd8e31 2022-10-23 thomas findeol(void)
480 3efd8e31 2022-10-23 thomas {
481 3efd8e31 2022-10-23 thomas int c;
482 3efd8e31 2022-10-23 thomas
483 3efd8e31 2022-10-23 thomas parsebuf = NULL;
484 3efd8e31 2022-10-23 thomas
485 3efd8e31 2022-10-23 thomas /* Skip to either EOF or the first real EOL. */
486 3efd8e31 2022-10-23 thomas while (1) {
487 3efd8e31 2022-10-23 thomas if (pushback_index)
488 3efd8e31 2022-10-23 thomas c = pushback_buffer[--pushback_index];
489 3efd8e31 2022-10-23 thomas else
490 3efd8e31 2022-10-23 thomas c = lgetc(0);
491 3efd8e31 2022-10-23 thomas if (c == '\n') {
492 3efd8e31 2022-10-23 thomas file->lineno++;
493 3efd8e31 2022-10-23 thomas break;
494 3efd8e31 2022-10-23 thomas }
495 3efd8e31 2022-10-23 thomas if (c == EOF)
496 3efd8e31 2022-10-23 thomas break;
497 3efd8e31 2022-10-23 thomas }
498 3efd8e31 2022-10-23 thomas return (ERROR);
499 3efd8e31 2022-10-23 thomas }
500 3efd8e31 2022-10-23 thomas
501 3efd8e31 2022-10-23 thomas int
502 3efd8e31 2022-10-23 thomas yylex(void)
503 3efd8e31 2022-10-23 thomas {
504 3efd8e31 2022-10-23 thomas unsigned char buf[8096];
505 3efd8e31 2022-10-23 thomas unsigned char *p, *val;
506 3efd8e31 2022-10-23 thomas int quotec, next, c;
507 3efd8e31 2022-10-23 thomas int token;
508 3efd8e31 2022-10-23 thomas
509 3efd8e31 2022-10-23 thomas top:
510 3efd8e31 2022-10-23 thomas p = buf;
511 3efd8e31 2022-10-23 thomas c = lgetc(0);
512 3efd8e31 2022-10-23 thomas while (c == ' ' || c == '\t')
513 3efd8e31 2022-10-23 thomas c = lgetc(0); /* nothing */
514 3efd8e31 2022-10-23 thomas
515 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
516 3efd8e31 2022-10-23 thomas if (c == '#') {
517 3efd8e31 2022-10-23 thomas c = lgetc(0);
518 3efd8e31 2022-10-23 thomas while (c != '\n' && c != EOF)
519 3efd8e31 2022-10-23 thomas c = lgetc(0); /* nothing */
520 3efd8e31 2022-10-23 thomas }
521 3efd8e31 2022-10-23 thomas if (c == '$' && parsebuf == NULL) {
522 3efd8e31 2022-10-23 thomas while (1) {
523 3efd8e31 2022-10-23 thomas c = lgetc(0);
524 3efd8e31 2022-10-23 thomas if (c == EOF)
525 3efd8e31 2022-10-23 thomas return (0);
526 3efd8e31 2022-10-23 thomas
527 3efd8e31 2022-10-23 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
528 3efd8e31 2022-10-23 thomas yyerror("string too long");
529 3efd8e31 2022-10-23 thomas return (findeol());
530 3efd8e31 2022-10-23 thomas }
531 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == '_') {
532 3efd8e31 2022-10-23 thomas *p++ = c;
533 3efd8e31 2022-10-23 thomas continue;
534 3efd8e31 2022-10-23 thomas }
535 3efd8e31 2022-10-23 thomas *p = '\0';
536 3efd8e31 2022-10-23 thomas lungetc(c);
537 3efd8e31 2022-10-23 thomas break;
538 3efd8e31 2022-10-23 thomas }
539 3efd8e31 2022-10-23 thomas val = symget(buf);
540 3efd8e31 2022-10-23 thomas if (val == NULL) {
541 3efd8e31 2022-10-23 thomas yyerror("macro '%s' not defined", buf);
542 3efd8e31 2022-10-23 thomas return (findeol());
543 3efd8e31 2022-10-23 thomas }
544 3efd8e31 2022-10-23 thomas parsebuf = val;
545 3efd8e31 2022-10-23 thomas parseindex = 0;
546 3efd8e31 2022-10-23 thomas goto top;
547 3efd8e31 2022-10-23 thomas }
548 3efd8e31 2022-10-23 thomas
549 3efd8e31 2022-10-23 thomas switch (c) {
550 3efd8e31 2022-10-23 thomas case '\'':
551 3efd8e31 2022-10-23 thomas case '"':
552 3efd8e31 2022-10-23 thomas quotec = c;
553 3efd8e31 2022-10-23 thomas while (1) {
554 3efd8e31 2022-10-23 thomas c = lgetc(quotec);
555 3efd8e31 2022-10-23 thomas if (c == EOF)
556 3efd8e31 2022-10-23 thomas return (0);
557 3efd8e31 2022-10-23 thomas if (c == '\n') {
558 3efd8e31 2022-10-23 thomas file->lineno++;
559 3efd8e31 2022-10-23 thomas continue;
560 3efd8e31 2022-10-23 thomas } else if (c == '\\') {
561 3efd8e31 2022-10-23 thomas next = lgetc(quotec);
562 3efd8e31 2022-10-23 thomas if (next == EOF)
563 3efd8e31 2022-10-23 thomas return (0);
564 3efd8e31 2022-10-23 thomas if (next == quotec || c == ' ' || c == '\t')
565 3efd8e31 2022-10-23 thomas c = next;
566 3efd8e31 2022-10-23 thomas else if (next == '\n') {
567 3efd8e31 2022-10-23 thomas file->lineno++;
568 3efd8e31 2022-10-23 thomas continue;
569 3efd8e31 2022-10-23 thomas } else
570 3efd8e31 2022-10-23 thomas lungetc(next);
571 3efd8e31 2022-10-23 thomas } else if (c == quotec) {
572 3efd8e31 2022-10-23 thomas *p = '\0';
573 3efd8e31 2022-10-23 thomas break;
574 3efd8e31 2022-10-23 thomas } else if (c == '\0') {
575 3efd8e31 2022-10-23 thomas yyerror("syntax error");
576 3efd8e31 2022-10-23 thomas return (findeol());
577 3efd8e31 2022-10-23 thomas }
578 3efd8e31 2022-10-23 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
579 3efd8e31 2022-10-23 thomas yyerror("string too long");
580 3efd8e31 2022-10-23 thomas return (findeol());
581 3efd8e31 2022-10-23 thomas }
582 3efd8e31 2022-10-23 thomas *p++ = c;
583 3efd8e31 2022-10-23 thomas }
584 3efd8e31 2022-10-23 thomas yylval.v.string = strdup(buf);
585 3efd8e31 2022-10-23 thomas if (yylval.v.string == NULL)
586 3efd8e31 2022-10-23 thomas err(1, "yylex: strdup");
587 3efd8e31 2022-10-23 thomas return (STRING);
588 3efd8e31 2022-10-23 thomas }
589 3efd8e31 2022-10-23 thomas
590 3efd8e31 2022-10-23 thomas #define allowed_to_end_number(x) \
591 3efd8e31 2022-10-23 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
592 3efd8e31 2022-10-23 thomas
593 3efd8e31 2022-10-23 thomas if (c == '-' || isdigit(c)) {
594 3efd8e31 2022-10-23 thomas do {
595 3efd8e31 2022-10-23 thomas *p++ = c;
596 3efd8e31 2022-10-23 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
597 3efd8e31 2022-10-23 thomas yyerror("string too long");
598 3efd8e31 2022-10-23 thomas return (findeol());
599 3efd8e31 2022-10-23 thomas }
600 3efd8e31 2022-10-23 thomas c = lgetc(0);
601 3efd8e31 2022-10-23 thomas } while (c != EOF && isdigit(c));
602 3efd8e31 2022-10-23 thomas lungetc(c);
603 3efd8e31 2022-10-23 thomas if (p == buf + 1 && buf[0] == '-')
604 3efd8e31 2022-10-23 thomas goto nodigits;
605 3efd8e31 2022-10-23 thomas if (c == EOF || allowed_to_end_number(c)) {
606 3efd8e31 2022-10-23 thomas const char *errstr = NULL;
607 3efd8e31 2022-10-23 thomas
608 3efd8e31 2022-10-23 thomas *p = '\0';
609 3efd8e31 2022-10-23 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
610 3efd8e31 2022-10-23 thomas LLONG_MAX, &errstr);
611 3efd8e31 2022-10-23 thomas if (errstr) {
612 3efd8e31 2022-10-23 thomas yyerror("\"%s\" invalid number: %s",
613 3efd8e31 2022-10-23 thomas buf, errstr);
614 3efd8e31 2022-10-23 thomas return (findeol());
615 3efd8e31 2022-10-23 thomas }
616 3efd8e31 2022-10-23 thomas return (NUMBER);
617 3efd8e31 2022-10-23 thomas } else {
618 3efd8e31 2022-10-23 thomas nodigits:
619 3efd8e31 2022-10-23 thomas while (p > buf + 1)
620 3efd8e31 2022-10-23 thomas lungetc(*--p);
621 3efd8e31 2022-10-23 thomas c = *--p;
622 3efd8e31 2022-10-23 thomas if (c == '-')
623 3efd8e31 2022-10-23 thomas return (c);
624 3efd8e31 2022-10-23 thomas }
625 3efd8e31 2022-10-23 thomas }
626 3efd8e31 2022-10-23 thomas
627 3efd8e31 2022-10-23 thomas #define allowed_in_string(x) \
628 3efd8e31 2022-10-23 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
629 3efd8e31 2022-10-23 thomas x != '{' && x != '}' && \
630 3efd8e31 2022-10-23 thomas x != '!' && x != '=' && x != '#' && \
631 3efd8e31 2022-10-23 thomas x != ','))
632 3efd8e31 2022-10-23 thomas
633 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == ':' || c == '_') {
634 3efd8e31 2022-10-23 thomas do {
635 3efd8e31 2022-10-23 thomas *p++ = c;
636 3efd8e31 2022-10-23 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
637 3efd8e31 2022-10-23 thomas yyerror("string too long");
638 3efd8e31 2022-10-23 thomas return (findeol());
639 3efd8e31 2022-10-23 thomas }
640 3efd8e31 2022-10-23 thomas c = lgetc(0);
641 3efd8e31 2022-10-23 thomas } while (c != EOF && (allowed_in_string(c)));
642 3efd8e31 2022-10-23 thomas lungetc(c);
643 3efd8e31 2022-10-23 thomas *p = '\0';
644 3efd8e31 2022-10-23 thomas token = lookup(buf);
645 3efd8e31 2022-10-23 thomas if (token == STRING) {
646 3efd8e31 2022-10-23 thomas yylval.v.string = strdup(buf);
647 3efd8e31 2022-10-23 thomas if (yylval.v.string == NULL)
648 3efd8e31 2022-10-23 thomas err(1, "yylex: strdup");
649 3efd8e31 2022-10-23 thomas }
650 3efd8e31 2022-10-23 thomas return (token);
651 3efd8e31 2022-10-23 thomas }
652 3efd8e31 2022-10-23 thomas if (c == '\n') {
653 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
654 3efd8e31 2022-10-23 thomas file->lineno++;
655 3efd8e31 2022-10-23 thomas }
656 3efd8e31 2022-10-23 thomas if (c == EOF)
657 3efd8e31 2022-10-23 thomas return (0);
658 3efd8e31 2022-10-23 thomas return (c);
659 3efd8e31 2022-10-23 thomas }
660 3efd8e31 2022-10-23 thomas
661 3efd8e31 2022-10-23 thomas int
662 3efd8e31 2022-10-23 thomas check_file_secrecy(int fd, const char *fname)
663 3efd8e31 2022-10-23 thomas {
664 3efd8e31 2022-10-23 thomas struct stat st;
665 3efd8e31 2022-10-23 thomas
666 3efd8e31 2022-10-23 thomas if (fstat(fd, &st)) {
667 3efd8e31 2022-10-23 thomas log_warn("cannot stat %s", fname);
668 3efd8e31 2022-10-23 thomas return (-1);
669 3efd8e31 2022-10-23 thomas }
670 3efd8e31 2022-10-23 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
671 3efd8e31 2022-10-23 thomas log_warnx("%s: owner not root or current user", fname);
672 3efd8e31 2022-10-23 thomas return (-1);
673 3efd8e31 2022-10-23 thomas }
674 3efd8e31 2022-10-23 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
675 3efd8e31 2022-10-23 thomas log_warnx("%s: group writable or world read/writable", fname);
676 3efd8e31 2022-10-23 thomas return (-1);
677 3efd8e31 2022-10-23 thomas }
678 3efd8e31 2022-10-23 thomas return (0);
679 3efd8e31 2022-10-23 thomas }
680 3efd8e31 2022-10-23 thomas
681 3efd8e31 2022-10-23 thomas struct file *
682 7554713a 2023-04-01 thomas newfile(const char *name, int secret, int required)
683 3efd8e31 2022-10-23 thomas {
684 3efd8e31 2022-10-23 thomas struct file *nfile;
685 3efd8e31 2022-10-23 thomas
686 3efd8e31 2022-10-23 thomas nfile = calloc(1, sizeof(struct file));
687 3efd8e31 2022-10-23 thomas if (nfile == NULL) {
688 3efd8e31 2022-10-23 thomas log_warn("calloc");
689 3efd8e31 2022-10-23 thomas return (NULL);
690 3efd8e31 2022-10-23 thomas }
691 3efd8e31 2022-10-23 thomas nfile->name = strdup(name);
692 3efd8e31 2022-10-23 thomas if (nfile->name == NULL) {
693 3efd8e31 2022-10-23 thomas log_warn("strdup");
694 3efd8e31 2022-10-23 thomas free(nfile);
695 3efd8e31 2022-10-23 thomas return (NULL);
696 3efd8e31 2022-10-23 thomas }
697 3efd8e31 2022-10-23 thomas nfile->stream = fopen(nfile->name, "r");
698 3efd8e31 2022-10-23 thomas if (nfile->stream == NULL) {
699 7554713a 2023-04-01 thomas if (required)
700 7554713a 2023-04-01 thomas log_warn("open %s", nfile->name);
701 3efd8e31 2022-10-23 thomas free(nfile->name);
702 3efd8e31 2022-10-23 thomas free(nfile);
703 3efd8e31 2022-10-23 thomas return (NULL);
704 3efd8e31 2022-10-23 thomas } else if (secret &&
705 3efd8e31 2022-10-23 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
706 3efd8e31 2022-10-23 thomas fclose(nfile->stream);
707 3efd8e31 2022-10-23 thomas free(nfile->name);
708 3efd8e31 2022-10-23 thomas free(nfile);
709 3efd8e31 2022-10-23 thomas return (NULL);
710 3efd8e31 2022-10-23 thomas }
711 3efd8e31 2022-10-23 thomas nfile->lineno = 1;
712 3efd8e31 2022-10-23 thomas return (nfile);
713 3efd8e31 2022-10-23 thomas }
714 3efd8e31 2022-10-23 thomas
715 3efd8e31 2022-10-23 thomas static void
716 3efd8e31 2022-10-23 thomas closefile(struct file *xfile)
717 3efd8e31 2022-10-23 thomas {
718 3efd8e31 2022-10-23 thomas fclose(xfile->stream);
719 3efd8e31 2022-10-23 thomas free(xfile->name);
720 3efd8e31 2022-10-23 thomas free(xfile);
721 3efd8e31 2022-10-23 thomas }
722 3efd8e31 2022-10-23 thomas
723 3efd8e31 2022-10-23 thomas int
724 3efd8e31 2022-10-23 thomas parse_config(const char *filename, enum gotd_procid proc_id,
725 7554713a 2023-04-01 thomas struct gotd *env, int require_config_file)
726 3efd8e31 2022-10-23 thomas {
727 3efd8e31 2022-10-23 thomas struct sym *sym, *next;
728 88f1bb6d 2023-01-02 thomas struct gotd_repo *repo;
729 3efd8e31 2022-10-23 thomas
730 3efd8e31 2022-10-23 thomas memset(env, 0, sizeof(*env));
731 3efd8e31 2022-10-23 thomas
732 3efd8e31 2022-10-23 thomas gotd = env;
733 3efd8e31 2022-10-23 thomas gotd_proc_id = proc_id;
734 3efd8e31 2022-10-23 thomas TAILQ_INIT(&gotd->repos);
735 3efd8e31 2022-10-23 thomas
736 3efd8e31 2022-10-23 thomas /* Apply default values. */
737 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
738 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
739 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: unix socket path too long", __func__);
740 3efd8e31 2022-10-23 thomas return -1;
741 3efd8e31 2022-10-23 thomas }
742 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->user_name, GOTD_USER,
743 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
744 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: user name too long", __func__);
745 3efd8e31 2022-10-23 thomas return -1;
746 3efd8e31 2022-10-23 thomas }
747 0781db0e 2023-01-06 thomas
748 0781db0e 2023-01-06 thomas gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
749 0781db0e 2023-01-06 thomas gotd->request_timeout.tv_usec = 0;
750 3efd8e31 2022-10-23 thomas
751 7554713a 2023-04-01 thomas file = newfile(filename, 0, require_config_file);
752 f3296add 2023-03-01 thomas if (file == NULL)
753 7554713a 2023-04-01 thomas return require_config_file ? -1 : 0;
754 3efd8e31 2022-10-23 thomas
755 3efd8e31 2022-10-23 thomas yyparse();
756 3efd8e31 2022-10-23 thomas errors = file->errors;
757 3efd8e31 2022-10-23 thomas closefile(file);
758 3efd8e31 2022-10-23 thomas
759 3efd8e31 2022-10-23 thomas /* Free macros and check which have not been used. */
760 3efd8e31 2022-10-23 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
761 3efd8e31 2022-10-23 thomas if ((gotd->verbosity > 1) && !sym->used)
762 3efd8e31 2022-10-23 thomas fprintf(stderr, "warning: macro '%s' not used\n",
763 3efd8e31 2022-10-23 thomas sym->nam);
764 3efd8e31 2022-10-23 thomas if (!sym->persist) {
765 3efd8e31 2022-10-23 thomas free(sym->nam);
766 3efd8e31 2022-10-23 thomas free(sym->val);
767 3efd8e31 2022-10-23 thomas TAILQ_REMOVE(&symhead, sym, entry);
768 3efd8e31 2022-10-23 thomas free(sym);
769 3efd8e31 2022-10-23 thomas }
770 3efd8e31 2022-10-23 thomas }
771 3efd8e31 2022-10-23 thomas
772 3efd8e31 2022-10-23 thomas if (errors)
773 3efd8e31 2022-10-23 thomas return (-1);
774 3efd8e31 2022-10-23 thomas
775 88f1bb6d 2023-01-02 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
776 88f1bb6d 2023-01-02 thomas if (repo->path[0] == '\0') {
777 0cbf8de7 2023-01-02 thomas log_warnx("repository \"%s\": no path provided in "
778 0cbf8de7 2023-01-02 thomas "configuration file", repo->name);
779 88f1bb6d 2023-01-02 thomas return (-1);
780 88f1bb6d 2023-01-02 thomas }
781 88f1bb6d 2023-01-02 thomas }
782 88f1bb6d 2023-01-02 thomas
783 4d24b1fd 2023-01-19 thomas if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
784 4d24b1fd 2023-01-19 thomas log_warnx("no repository defined in configuration file");
785 4d24b1fd 2023-01-19 thomas return (-1);
786 4d24b1fd 2023-01-19 thomas }
787 4d24b1fd 2023-01-19 thomas
788 3efd8e31 2022-10-23 thomas return (0);
789 3efd8e31 2022-10-23 thomas }
790 3efd8e31 2022-10-23 thomas
791 0781db0e 2023-01-06 thomas static int
792 0781db0e 2023-01-06 thomas uid_connection_limit_cmp(const void *pa, const void *pb)
793 0781db0e 2023-01-06 thomas {
794 0781db0e 2023-01-06 thomas const struct gotd_uid_connection_limit *a = pa, *b = pb;
795 0781db0e 2023-01-06 thomas
796 0781db0e 2023-01-06 thomas if (a->uid < b->uid)
797 0781db0e 2023-01-06 thomas return -1;
798 0781db0e 2023-01-06 thomas else if (a->uid > b->uid);
799 0781db0e 2023-01-06 thomas return 1;
800 0781db0e 2023-01-06 thomas
801 0781db0e 2023-01-06 thomas return 0;
802 0781db0e 2023-01-06 thomas }
803 0781db0e 2023-01-06 thomas
804 0781db0e 2023-01-06 thomas static int
805 0781db0e 2023-01-06 thomas conf_limit_user_connections(const char *user, int maximum)
806 0781db0e 2023-01-06 thomas {
807 0781db0e 2023-01-06 thomas uid_t uid;
808 0781db0e 2023-01-06 thomas struct gotd_uid_connection_limit *limit;
809 0781db0e 2023-01-06 thomas size_t nlimits;
810 0781db0e 2023-01-06 thomas
811 0781db0e 2023-01-06 thomas if (maximum < 1) {
812 0781db0e 2023-01-06 thomas yyerror("max connections cannot be smaller 1");
813 0781db0e 2023-01-06 thomas return -1;
814 0781db0e 2023-01-06 thomas }
815 0781db0e 2023-01-06 thomas if (maximum > GOTD_MAXCLIENTS) {
816 0781db0e 2023-01-06 thomas yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
817 0781db0e 2023-01-06 thomas return -1;
818 0781db0e 2023-01-06 thomas }
819 0781db0e 2023-01-06 thomas
820 0781db0e 2023-01-06 thomas if (gotd_auth_parseuid(user, &uid) == -1) {
821 0781db0e 2023-01-06 thomas yyerror("%s: no such user", user);
822 0781db0e 2023-01-06 thomas return -1;
823 0781db0e 2023-01-06 thomas }
824 0781db0e 2023-01-06 thomas
825 0781db0e 2023-01-06 thomas limit = gotd_find_uid_connection_limit(gotd->connection_limits,
826 0781db0e 2023-01-06 thomas gotd->nconnection_limits, uid);
827 0781db0e 2023-01-06 thomas if (limit) {
828 0781db0e 2023-01-06 thomas limit->max_connections = maximum;
829 0781db0e 2023-01-06 thomas return 0;
830 0781db0e 2023-01-06 thomas }
831 0781db0e 2023-01-06 thomas
832 0781db0e 2023-01-06 thomas limit = gotd->connection_limits;
833 0781db0e 2023-01-06 thomas nlimits = gotd->nconnection_limits + 1;
834 0781db0e 2023-01-06 thomas limit = reallocarray(limit, nlimits, sizeof(*limit));
835 0781db0e 2023-01-06 thomas if (limit == NULL)
836 0781db0e 2023-01-06 thomas fatal("reallocarray");
837 0781db0e 2023-01-06 thomas
838 0781db0e 2023-01-06 thomas limit[nlimits - 1].uid = uid;
839 0781db0e 2023-01-06 thomas limit[nlimits - 1].max_connections = maximum;
840 0781db0e 2023-01-06 thomas
841 0781db0e 2023-01-06 thomas gotd->connection_limits = limit;
842 0781db0e 2023-01-06 thomas gotd->nconnection_limits = nlimits;
843 0781db0e 2023-01-06 thomas qsort(gotd->connection_limits, gotd->nconnection_limits,
844 0781db0e 2023-01-06 thomas sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
845 0781db0e 2023-01-06 thomas
846 0781db0e 2023-01-06 thomas return 0;
847 0781db0e 2023-01-06 thomas }
848 0781db0e 2023-01-06 thomas
849 3efd8e31 2022-10-23 thomas static struct gotd_repo *
850 3efd8e31 2022-10-23 thomas conf_new_repo(const char *name)
851 3efd8e31 2022-10-23 thomas {
852 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
853 3efd8e31 2022-10-23 thomas
854 a42b418b 2023-01-02 thomas if (name[0] == '\0') {
855 a42b418b 2023-01-02 thomas fatalx("syntax error: empty repository name found in %s",
856 a42b418b 2023-01-02 thomas file->name);
857 a42b418b 2023-01-02 thomas }
858 a42b418b 2023-01-02 thomas
859 0cbf8de7 2023-01-02 thomas if (strchr(name, '\n') != NULL)
860 0cbf8de7 2023-01-02 thomas fatalx("repository names must not contain linefeeds: %s", name);
861 3efd8e31 2022-10-23 thomas
862 3efd8e31 2022-10-23 thomas repo = calloc(1, sizeof(*repo));
863 3efd8e31 2022-10-23 thomas if (repo == NULL)
864 3efd8e31 2022-10-23 thomas fatalx("%s: calloc", __func__);
865 3efd8e31 2022-10-23 thomas
866 729a7e24 2022-11-17 thomas STAILQ_INIT(&repo->rules);
867 6d7eb4f7 2023-04-04 thomas TAILQ_INIT(&repo->protected_tag_namespaces);
868 6d7eb4f7 2023-04-04 thomas TAILQ_INIT(&repo->protected_branch_namespaces);
869 6d7eb4f7 2023-04-04 thomas TAILQ_INIT(&repo->protected_branches);
870 729a7e24 2022-11-17 thomas
871 3efd8e31 2022-10-23 thomas if (strlcpy(repo->name, name, sizeof(repo->name)) >=
872 3efd8e31 2022-10-23 thomas sizeof(repo->name))
873 3efd8e31 2022-10-23 thomas fatalx("%s: strlcpy", __func__);
874 3efd8e31 2022-10-23 thomas
875 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
876 3efd8e31 2022-10-23 thomas gotd->nrepos++;
877 3efd8e31 2022-10-23 thomas
878 3efd8e31 2022-10-23 thomas return repo;
879 3efd8e31 2022-10-23 thomas };
880 729a7e24 2022-11-17 thomas
881 729a7e24 2022-11-17 thomas static void
882 729a7e24 2022-11-17 thomas conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
883 729a7e24 2022-11-17 thomas int authorization, char *identifier)
884 729a7e24 2022-11-17 thomas {
885 729a7e24 2022-11-17 thomas struct gotd_access_rule *rule;
886 729a7e24 2022-11-17 thomas
887 729a7e24 2022-11-17 thomas rule = calloc(1, sizeof(*rule));
888 729a7e24 2022-11-17 thomas if (rule == NULL)
889 729a7e24 2022-11-17 thomas fatal("calloc");
890 3efd8e31 2022-10-23 thomas
891 729a7e24 2022-11-17 thomas rule->access = access;
892 729a7e24 2022-11-17 thomas rule->authorization = authorization;
893 729a7e24 2022-11-17 thomas rule->identifier = identifier;
894 729a7e24 2022-11-17 thomas
895 729a7e24 2022-11-17 thomas STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
896 729a7e24 2022-11-17 thomas }
897 729a7e24 2022-11-17 thomas
898 6d7eb4f7 2023-04-04 thomas static int
899 6d7eb4f7 2023-04-04 thomas refname_is_valid(char *refname)
900 6d7eb4f7 2023-04-04 thomas {
901 6d7eb4f7 2023-04-04 thomas if (strlen(refname) < 5 || strncmp(refname, "refs/", 5) != 0) {
902 6d7eb4f7 2023-04-04 thomas yyerror("reference name must begin with \"refs/\": %s",
903 6d7eb4f7 2023-04-04 thomas refname);
904 6d7eb4f7 2023-04-04 thomas return 0;
905 6d7eb4f7 2023-04-04 thomas }
906 6d7eb4f7 2023-04-04 thomas
907 6d7eb4f7 2023-04-04 thomas if (!got_ref_name_is_valid(refname)) {
908 6d7eb4f7 2023-04-04 thomas yyerror("invalid reference name: %s", refname);
909 6d7eb4f7 2023-04-04 thomas return 0;
910 6d7eb4f7 2023-04-04 thomas }
911 6d7eb4f7 2023-04-04 thomas
912 6d7eb4f7 2023-04-04 thomas return 1;
913 6d7eb4f7 2023-04-04 thomas }
914 6d7eb4f7 2023-04-04 thomas
915 6d7eb4f7 2023-04-04 thomas static int
916 6d7eb4f7 2023-04-04 thomas conf_protect_ref_namespace(struct got_pathlist_head *refs, char *namespace)
917 6d7eb4f7 2023-04-04 thomas {
918 6d7eb4f7 2023-04-04 thomas const struct got_error *error;
919 a4435ef0 2023-04-05 thomas struct got_pathlist_entry *new;
920 6d7eb4f7 2023-04-04 thomas char *s;
921 6d7eb4f7 2023-04-04 thomas
922 6d7eb4f7 2023-04-04 thomas got_path_strip_trailing_slashes(namespace);
923 6d7eb4f7 2023-04-04 thomas if (!refname_is_valid(namespace))
924 6d7eb4f7 2023-04-04 thomas return -1;
925 6d7eb4f7 2023-04-04 thomas if (asprintf(&s, "%s/", namespace) == -1) {
926 6d7eb4f7 2023-04-04 thomas yyerror("asprintf: %s", strerror(errno));
927 6d7eb4f7 2023-04-04 thomas return -1;
928 6d7eb4f7 2023-04-04 thomas }
929 6d7eb4f7 2023-04-04 thomas
930 a4435ef0 2023-04-05 thomas error = got_pathlist_insert(&new, refs, s, NULL);
931 a4435ef0 2023-04-05 thomas if (error || new == NULL) {
932 a4435ef0 2023-04-05 thomas free(s);
933 a4435ef0 2023-04-05 thomas if (error)
934 a4435ef0 2023-04-05 thomas yyerror("got_pathlist_insert: %s", error->msg);
935 a4435ef0 2023-04-05 thomas else
936 a4435ef0 2023-04-05 thomas yyerror("duplicate protect namespace %s", namespace);
937 6d7eb4f7 2023-04-04 thomas return -1;
938 6d7eb4f7 2023-04-04 thomas }
939 6d7eb4f7 2023-04-04 thomas
940 6d7eb4f7 2023-04-04 thomas return 0;
941 6d7eb4f7 2023-04-04 thomas }
942 6d7eb4f7 2023-04-04 thomas
943 6d7eb4f7 2023-04-04 thomas static int
944 6d7eb4f7 2023-04-04 thomas conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
945 6d7eb4f7 2023-04-04 thomas {
946 6d7eb4f7 2023-04-04 thomas return conf_protect_ref_namespace(&repo->protected_tag_namespaces,
947 6d7eb4f7 2023-04-04 thomas namespace);
948 6d7eb4f7 2023-04-04 thomas }
949 6d7eb4f7 2023-04-04 thomas
950 6d7eb4f7 2023-04-04 thomas static int
951 6d7eb4f7 2023-04-04 thomas conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
952 6d7eb4f7 2023-04-04 thomas {
953 6d7eb4f7 2023-04-04 thomas return conf_protect_ref_namespace(&repo->protected_branch_namespaces,
954 6d7eb4f7 2023-04-04 thomas namespace);
955 6d7eb4f7 2023-04-04 thomas }
956 6d7eb4f7 2023-04-04 thomas
957 6d7eb4f7 2023-04-04 thomas static int
958 6d7eb4f7 2023-04-04 thomas conf_protect_branch(struct gotd_repo *repo, char *branchname)
959 6d7eb4f7 2023-04-04 thomas {
960 6d7eb4f7 2023-04-04 thomas const struct got_error *error;
961 a4435ef0 2023-04-05 thomas struct got_pathlist_entry *new;
962 6d7eb4f7 2023-04-04 thomas char *refname;
963 6d7eb4f7 2023-04-04 thomas
964 6d7eb4f7 2023-04-04 thomas if (strncmp(branchname, "refs/heads/", 11) != 0) {
965 6d7eb4f7 2023-04-04 thomas if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
966 6d7eb4f7 2023-04-04 thomas yyerror("asprintf: %s", strerror(errno));
967 6d7eb4f7 2023-04-04 thomas return -1;
968 6d7eb4f7 2023-04-04 thomas }
969 6d7eb4f7 2023-04-04 thomas } else {
970 6d7eb4f7 2023-04-04 thomas refname = strdup(branchname);
971 6d7eb4f7 2023-04-04 thomas if (refname == NULL) {
972 6d7eb4f7 2023-04-04 thomas yyerror("strdup: %s", strerror(errno));
973 6d7eb4f7 2023-04-04 thomas return -1;
974 6d7eb4f7 2023-04-04 thomas }
975 6d7eb4f7 2023-04-04 thomas }
976 6d7eb4f7 2023-04-04 thomas
977 6d7eb4f7 2023-04-04 thomas if (!refname_is_valid(refname)) {
978 6d7eb4f7 2023-04-04 thomas free(refname);
979 6d7eb4f7 2023-04-04 thomas return -1;
980 6d7eb4f7 2023-04-04 thomas }
981 6d7eb4f7 2023-04-04 thomas
982 a4435ef0 2023-04-05 thomas error = got_pathlist_insert(&new, &repo->protected_branches,
983 6d7eb4f7 2023-04-04 thomas refname, NULL);
984 a4435ef0 2023-04-05 thomas if (error || new == NULL) {
985 a4435ef0 2023-04-05 thomas free(refname);
986 a4435ef0 2023-04-05 thomas if (error)
987 a4435ef0 2023-04-05 thomas yyerror("got_pathlist_insert: %s", error->msg);
988 a4435ef0 2023-04-05 thomas else
989 a4435ef0 2023-04-05 thomas yyerror("duplicate protect branch %s", branchname);
990 6d7eb4f7 2023-04-04 thomas return -1;
991 6d7eb4f7 2023-04-04 thomas }
992 6d7eb4f7 2023-04-04 thomas
993 6d7eb4f7 2023-04-04 thomas return 0;
994 6d7eb4f7 2023-04-04 thomas }
995 6d7eb4f7 2023-04-04 thomas
996 3efd8e31 2022-10-23 thomas int
997 3efd8e31 2022-10-23 thomas symset(const char *nam, const char *val, int persist)
998 3efd8e31 2022-10-23 thomas {
999 3efd8e31 2022-10-23 thomas struct sym *sym;
1000 3efd8e31 2022-10-23 thomas
1001 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1002 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0)
1003 3efd8e31 2022-10-23 thomas break;
1004 3efd8e31 2022-10-23 thomas }
1005 3efd8e31 2022-10-23 thomas
1006 3efd8e31 2022-10-23 thomas if (sym != NULL) {
1007 3efd8e31 2022-10-23 thomas if (sym->persist == 1)
1008 3efd8e31 2022-10-23 thomas return (0);
1009 3efd8e31 2022-10-23 thomas else {
1010 3efd8e31 2022-10-23 thomas free(sym->nam);
1011 3efd8e31 2022-10-23 thomas free(sym->val);
1012 3efd8e31 2022-10-23 thomas TAILQ_REMOVE(&symhead, sym, entry);
1013 3efd8e31 2022-10-23 thomas free(sym);
1014 3efd8e31 2022-10-23 thomas }
1015 3efd8e31 2022-10-23 thomas }
1016 3efd8e31 2022-10-23 thomas sym = calloc(1, sizeof(*sym));
1017 3efd8e31 2022-10-23 thomas if (sym == NULL)
1018 3efd8e31 2022-10-23 thomas return (-1);
1019 3efd8e31 2022-10-23 thomas
1020 3efd8e31 2022-10-23 thomas sym->nam = strdup(nam);
1021 3efd8e31 2022-10-23 thomas if (sym->nam == NULL) {
1022 3efd8e31 2022-10-23 thomas free(sym);
1023 3efd8e31 2022-10-23 thomas return (-1);
1024 3efd8e31 2022-10-23 thomas }
1025 3efd8e31 2022-10-23 thomas sym->val = strdup(val);
1026 3efd8e31 2022-10-23 thomas if (sym->val == NULL) {
1027 3efd8e31 2022-10-23 thomas free(sym->nam);
1028 3efd8e31 2022-10-23 thomas free(sym);
1029 3efd8e31 2022-10-23 thomas return (-1);
1030 3efd8e31 2022-10-23 thomas }
1031 3efd8e31 2022-10-23 thomas sym->used = 0;
1032 3efd8e31 2022-10-23 thomas sym->persist = persist;
1033 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
1034 3efd8e31 2022-10-23 thomas return (0);
1035 3efd8e31 2022-10-23 thomas }
1036 3efd8e31 2022-10-23 thomas
1037 3efd8e31 2022-10-23 thomas char *
1038 3efd8e31 2022-10-23 thomas symget(const char *nam)
1039 3efd8e31 2022-10-23 thomas {
1040 3efd8e31 2022-10-23 thomas struct sym *sym;
1041 3efd8e31 2022-10-23 thomas
1042 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1043 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0) {
1044 3efd8e31 2022-10-23 thomas sym->used = 1;
1045 3efd8e31 2022-10-23 thomas return (sym->val);
1046 3efd8e31 2022-10-23 thomas }
1047 3efd8e31 2022-10-23 thomas }
1048 3efd8e31 2022-10-23 thomas return (NULL);
1049 5dcb3a43 2023-04-01 thomas }
1050 5dcb3a43 2023-04-01 thomas
1051 5dcb3a43 2023-04-01 thomas struct gotd_repo *
1052 5dcb3a43 2023-04-01 thomas gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1053 5dcb3a43 2023-04-01 thomas {
1054 5dcb3a43 2023-04-01 thomas struct gotd_repo *repo;
1055 5dcb3a43 2023-04-01 thomas size_t namelen;
1056 5dcb3a43 2023-04-01 thomas
1057 5dcb3a43 2023-04-01 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
1058 5dcb3a43 2023-04-01 thomas namelen = strlen(repo->name);
1059 5dcb3a43 2023-04-01 thomas if (strncmp(repo->name, repo_name, namelen) != 0)
1060 5dcb3a43 2023-04-01 thomas continue;
1061 5dcb3a43 2023-04-01 thomas if (repo_name[namelen] == '\0' ||
1062 5dcb3a43 2023-04-01 thomas strcmp(&repo_name[namelen], ".git") == 0)
1063 5dcb3a43 2023-04-01 thomas return repo;
1064 5dcb3a43 2023-04-01 thomas }
1065 5dcb3a43 2023-04-01 thomas
1066 5dcb3a43 2023-04-01 thomas return NULL;
1067 3efd8e31 2022-10-23 thomas }
1068 6d7eb4f7 2023-04-04 thomas
1069 6d7eb4f7 2023-04-04 thomas struct gotd_repo *
1070 6d7eb4f7 2023-04-04 thomas gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1071 6d7eb4f7 2023-04-04 thomas {
1072 6d7eb4f7 2023-04-04 thomas struct gotd_repo *repo;
1073 6d7eb4f7 2023-04-04 thomas
1074 6d7eb4f7 2023-04-04 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
1075 6d7eb4f7 2023-04-04 thomas if (strcmp(repo->path, repo_path) == 0)
1076 6d7eb4f7 2023-04-04 thomas return repo;
1077 6d7eb4f7 2023-04-04 thomas }
1078 6d7eb4f7 2023-04-04 thomas
1079 6d7eb4f7 2023-04-04 thomas return NULL;
1080 6d7eb4f7 2023-04-04 thomas }