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