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