Blame


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