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.
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.
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.
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>
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 <sha1.h>
37 3efd8e31 2022-10-23 thomas #include <stdarg.h>
38 3efd8e31 2022-10-23 thomas #include <stdlib.h>
39 3efd8e31 2022-10-23 thomas #include <stdio.h>
40 3efd8e31 2022-10-23 thomas #include <string.h>
41 3efd8e31 2022-10-23 thomas #include <syslog.h>
42 3efd8e31 2022-10-23 thomas #include <unistd.h>
44 3efd8e31 2022-10-23 thomas #include "got_error.h"
45 3efd8e31 2022-10-23 thomas #include "got_path.h"
47 3efd8e31 2022-10-23 thomas #include "log.h"
48 3efd8e31 2022-10-23 thomas #include "gotd.h"
49 0781db0e 2023-01-06 thomas #include "auth.h"
50 0781db0e 2023-01-06 thomas #include "listen.h"
52 3efd8e31 2022-10-23 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
53 3efd8e31 2022-10-23 thomas static struct file {
54 3efd8e31 2022-10-23 thomas TAILQ_ENTRY(file) entry;
55 3efd8e31 2022-10-23 thomas FILE *stream;
56 3efd8e31 2022-10-23 thomas char *name;
57 3efd8e31 2022-10-23 thomas int lineno;
58 3efd8e31 2022-10-23 thomas int errors;
60 3efd8e31 2022-10-23 thomas struct file *newfile(const char *, int);
61 3efd8e31 2022-10-23 thomas static void closefile(struct file *);
62 3efd8e31 2022-10-23 thomas int check_file_secrecy(int, const char *);
63 3efd8e31 2022-10-23 thomas int yyparse(void);
64 3efd8e31 2022-10-23 thomas int yylex(void);
65 3efd8e31 2022-10-23 thomas int yyerror(const char *, ...)
66 3efd8e31 2022-10-23 thomas __attribute__((__format__ (printf, 1, 2)))
67 3efd8e31 2022-10-23 thomas __attribute__((__nonnull__ (1)));
68 3efd8e31 2022-10-23 thomas int kw_cmp(const void *, const void *);
69 3efd8e31 2022-10-23 thomas int lookup(char *);
70 3efd8e31 2022-10-23 thomas int lgetc(int);
71 3efd8e31 2022-10-23 thomas int lungetc(int);
72 3efd8e31 2022-10-23 thomas int findeol(void);
74 3efd8e31 2022-10-23 thomas TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 3efd8e31 2022-10-23 thomas struct sym {
76 3efd8e31 2022-10-23 thomas TAILQ_ENTRY(sym) entry;
78 3efd8e31 2022-10-23 thomas int persist;
79 3efd8e31 2022-10-23 thomas char *nam;
80 3efd8e31 2022-10-23 thomas char *val;
83 3efd8e31 2022-10-23 thomas int symset(const char *, const char *, int);
84 3efd8e31 2022-10-23 thomas char *symget(const char *);
86 3efd8e31 2022-10-23 thomas static int errors;
88 3efd8e31 2022-10-23 thomas static struct gotd *gotd;
89 3efd8e31 2022-10-23 thomas static struct gotd_repo *new_repo;
90 67f822ee 2023-01-06 thomas static int conf_limit_user_connections(const char *, int);
91 3efd8e31 2022-10-23 thomas static struct gotd_repo *conf_new_repo(const char *);
92 c3841c67 2022-11-18 thomas static void conf_new_access_rule(struct gotd_repo *,
93 c3841c67 2022-11-18 thomas enum gotd_access, int, char *);
94 3efd8e31 2022-10-23 thomas static enum gotd_procid gotd_proc_id;
96 3efd8e31 2022-10-23 thomas typedef struct {
98 3efd8e31 2022-10-23 thomas long long number;
99 3efd8e31 2022-10-23 thomas char *string;
100 0781db0e 2023-01-06 thomas struct timeval tv;
102 3efd8e31 2022-10-23 thomas int lineno;
103 3efd8e31 2022-10-23 thomas } YYSTYPE;
107 729a7e24 2022-11-17 thomas %token PATH ERROR ON UNIX_SOCKET UNIX_GROUP USER REPOSITORY PERMIT DENY
108 0781db0e 2023-01-06 thomas %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
110 3efd8e31 2022-10-23 thomas %token <v.string> STRING
111 3efd8e31 2022-10-23 thomas %token <v.number> NUMBER
112 3efd8e31 2022-10-23 thomas %type <v.number> boolean
113 0781db0e 2023-01-06 thomas %type <v.tv> timeout
117 3efd8e31 2022-10-23 thomas grammar :
118 3efd8e31 2022-10-23 thomas | grammar '\n'
119 3efd8e31 2022-10-23 thomas | grammar main '\n'
120 3efd8e31 2022-10-23 thomas | grammar repository '\n'
123 3efd8e31 2022-10-23 thomas boolean : STRING {
124 3efd8e31 2022-10-23 thomas if (strcasecmp($1, "1") == 0 ||
125 3efd8e31 2022-10-23 thomas strcasecmp($1, "yes") == 0 ||
126 3efd8e31 2022-10-23 thomas strcasecmp($1, "on") == 0)
128 3efd8e31 2022-10-23 thomas else if (strcasecmp($1, "0") == 0 ||
129 3efd8e31 2022-10-23 thomas strcasecmp($1, "off") == 0 ||
130 3efd8e31 2022-10-23 thomas strcasecmp($1, "no") == 0)
133 3efd8e31 2022-10-23 thomas yyerror("invalid boolean value '%s'", $1);
134 3efd8e31 2022-10-23 thomas free($1);
137 3efd8e31 2022-10-23 thomas free($1);
139 3efd8e31 2022-10-23 thomas | ON { $$ = 1; }
140 3efd8e31 2022-10-23 thomas | NUMBER { $$ = $1; }
143 0781db0e 2023-01-06 thomas timeout : NUMBER {
144 0781db0e 2023-01-06 thomas if ($1 < 0) {
145 0781db0e 2023-01-06 thomas yyerror("invalid timeout: %lld", $1);
148 0781db0e 2023-01-06 thomas $$.tv_sec = $1;
149 0781db0e 2023-01-06 thomas $$.tv_usec = 0;
153 3efd8e31 2022-10-23 thomas main : UNIX_SOCKET STRING {
154 2b3d32a1 2022-12-30 thomas if (gotd_proc_id == PROC_LISTEN) {
155 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->unix_socket_path, $2,
156 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) >=
157 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) {
158 3efd8e31 2022-10-23 thomas yyerror("%s: unix socket path too long",
159 3efd8e31 2022-10-23 thomas __func__);
160 3efd8e31 2022-10-23 thomas free($2);
164 3efd8e31 2022-10-23 thomas free($2);
166 3efd8e31 2022-10-23 thomas | UNIX_GROUP STRING {
167 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->unix_group_name, $2,
168 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_group_name)) >=
169 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_group_name)) {
170 3efd8e31 2022-10-23 thomas yyerror("%s: unix group name too long",
171 3efd8e31 2022-10-23 thomas __func__);
172 3efd8e31 2022-10-23 thomas free($2);
175 3efd8e31 2022-10-23 thomas free($2);
177 3efd8e31 2022-10-23 thomas | USER STRING {
178 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->user_name, $2,
179 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) >=
180 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) {
181 3efd8e31 2022-10-23 thomas yyerror("%s: user name too long", __func__);
182 3efd8e31 2022-10-23 thomas free($2);
185 3efd8e31 2022-10-23 thomas free($2);
187 0781db0e 2023-01-06 thomas | connection
190 0781db0e 2023-01-06 thomas connection : CONNECTION '{' optnl conflags_l '}'
191 0781db0e 2023-01-06 thomas | CONNECTION conflags
193 0781db0e 2023-01-06 thomas conflags_l : conflags optnl conflags_l
194 0781db0e 2023-01-06 thomas | conflags optnl
197 0781db0e 2023-01-06 thomas conflags : REQUEST TIMEOUT timeout {
198 0781db0e 2023-01-06 thomas memcpy(&gotd->request_timeout, &$3,
199 0781db0e 2023-01-06 thomas sizeof(gotd->request_timeout));
201 0781db0e 2023-01-06 thomas | LIMIT USER STRING NUMBER {
202 0781db0e 2023-01-06 thomas if (gotd_proc_id == PROC_LISTEN &&
203 0781db0e 2023-01-06 thomas conf_limit_user_connections($3, $4) == -1) {
204 0781db0e 2023-01-06 thomas free($3);
207 0781db0e 2023-01-06 thomas free($3);
211 3efd8e31 2022-10-23 thomas repository : REPOSITORY STRING {
212 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
214 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
215 3efd8e31 2022-10-23 thomas if (strcmp(repo->name, $2) == 0) {
216 3efd8e31 2022-10-23 thomas yyerror("duplicate repository '%s'", $2);
217 3efd8e31 2022-10-23 thomas free($2);
222 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_GOTD ||
223 c669c489 2022-12-30 thomas gotd_proc_id == PROC_AUTH) {
224 3efd8e31 2022-10-23 thomas new_repo = conf_new_repo($2);
226 3efd8e31 2022-10-23 thomas free($2);
227 3efd8e31 2022-10-23 thomas } '{' optnl repoopts2 '}' {
231 3efd8e31 2022-10-23 thomas repoopts1 : PATH STRING {
232 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_GOTD ||
233 c669c489 2022-12-30 thomas gotd_proc_id == PROC_AUTH) {
234 3efd8e31 2022-10-23 thomas if (!got_path_is_absolute($2)) {
235 3efd8e31 2022-10-23 thomas yyerror("%s: path %s is not absolute",
236 3efd8e31 2022-10-23 thomas __func__, $2);
237 3efd8e31 2022-10-23 thomas free($2);
240 3efd8e31 2022-10-23 thomas if (strlcpy(new_repo->path, $2,
241 3efd8e31 2022-10-23 thomas sizeof(new_repo->path)) >=
242 3efd8e31 2022-10-23 thomas sizeof(new_repo->path)) {
243 3efd8e31 2022-10-23 thomas yyerror("%s: path truncated", __func__);
244 3efd8e31 2022-10-23 thomas free($2);
248 3efd8e31 2022-10-23 thomas free($2);
250 729a7e24 2022-11-17 thomas | PERMIT RO STRING {
251 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
252 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
253 729a7e24 2022-11-17 thomas GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
256 729a7e24 2022-11-17 thomas | PERMIT RW STRING {
257 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
258 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
259 729a7e24 2022-11-17 thomas GOTD_ACCESS_PERMITTED,
260 729a7e24 2022-11-17 thomas GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
263 729a7e24 2022-11-17 thomas | DENY STRING {
264 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
265 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
266 729a7e24 2022-11-17 thomas GOTD_ACCESS_DENIED, 0, $2);
271 3efd8e31 2022-10-23 thomas repoopts2 : repoopts2 repoopts1 nl
272 3efd8e31 2022-10-23 thomas | repoopts1 optnl
275 3efd8e31 2022-10-23 thomas nl : '\n' optnl
278 3efd8e31 2022-10-23 thomas optnl : '\n' optnl /* zero or more newlines */
279 3efd8e31 2022-10-23 thomas | /* empty */
284 3efd8e31 2022-10-23 thomas struct keywords {
285 3efd8e31 2022-10-23 thomas const char *k_name;
286 3efd8e31 2022-10-23 thomas int k_val;
290 3efd8e31 2022-10-23 thomas yyerror(const char *fmt, ...)
292 3efd8e31 2022-10-23 thomas va_list ap;
293 3efd8e31 2022-10-23 thomas char *msg;
295 3efd8e31 2022-10-23 thomas file->errors++;
296 3efd8e31 2022-10-23 thomas va_start(ap, fmt);
297 3efd8e31 2022-10-23 thomas if (vasprintf(&msg, fmt, ap) == -1)
298 3efd8e31 2022-10-23 thomas fatalx("yyerror vasprintf");
299 3efd8e31 2022-10-23 thomas va_end(ap);
300 3efd8e31 2022-10-23 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
301 3efd8e31 2022-10-23 thomas free(msg);
302 3efd8e31 2022-10-23 thomas return (0);
306 3efd8e31 2022-10-23 thomas kw_cmp(const void *k, const void *e)
308 3efd8e31 2022-10-23 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
312 3efd8e31 2022-10-23 thomas lookup(char *s)
314 3efd8e31 2022-10-23 thomas /* This has to be sorted always. */
315 3efd8e31 2022-10-23 thomas static const struct keywords keywords[] = {
316 0781db0e 2023-01-06 thomas { "connection", CONNECTION },
317 729a7e24 2022-11-17 thomas { "deny", DENY },
318 0781db0e 2023-01-06 thomas { "limit", LIMIT },
319 3efd8e31 2022-10-23 thomas { "on", ON },
320 3efd8e31 2022-10-23 thomas { "path", PATH },
321 729a7e24 2022-11-17 thomas { "permit", PERMIT },
322 3efd8e31 2022-10-23 thomas { "repository", REPOSITORY },
323 0781db0e 2023-01-06 thomas { "request", REQUEST },
324 729a7e24 2022-11-17 thomas { "ro", RO },
325 729a7e24 2022-11-17 thomas { "rw", RW },
326 0781db0e 2023-01-06 thomas { "timeout", TIMEOUT },
327 3efd8e31 2022-10-23 thomas { "unix_group", UNIX_GROUP },
328 3efd8e31 2022-10-23 thomas { "unix_socket", UNIX_SOCKET },
329 3efd8e31 2022-10-23 thomas { "user", USER },
331 3efd8e31 2022-10-23 thomas const struct keywords *p;
333 3efd8e31 2022-10-23 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
334 3efd8e31 2022-10-23 thomas sizeof(keywords[0]), kw_cmp);
337 3efd8e31 2022-10-23 thomas return (p->k_val);
339 3efd8e31 2022-10-23 thomas return (STRING);
342 3efd8e31 2022-10-23 thomas #define MAXPUSHBACK 128
344 3efd8e31 2022-10-23 thomas unsigned char *parsebuf;
345 3efd8e31 2022-10-23 thomas int parseindex;
346 3efd8e31 2022-10-23 thomas unsigned char pushback_buffer[MAXPUSHBACK];
347 3efd8e31 2022-10-23 thomas int pushback_index = 0;
350 3efd8e31 2022-10-23 thomas lgetc(int quotec)
352 3efd8e31 2022-10-23 thomas int c, next;
354 3efd8e31 2022-10-23 thomas if (parsebuf) {
355 3efd8e31 2022-10-23 thomas /* Read character from the parsebuffer instead of input. */
356 3efd8e31 2022-10-23 thomas if (parseindex >= 0) {
357 3efd8e31 2022-10-23 thomas c = parsebuf[parseindex++];
358 3efd8e31 2022-10-23 thomas if (c != '\0')
359 3efd8e31 2022-10-23 thomas return (c);
360 3efd8e31 2022-10-23 thomas parsebuf = NULL;
362 3efd8e31 2022-10-23 thomas parseindex++;
365 3efd8e31 2022-10-23 thomas if (pushback_index)
366 3efd8e31 2022-10-23 thomas return (pushback_buffer[--pushback_index]);
368 3efd8e31 2022-10-23 thomas if (quotec) {
369 3efd8e31 2022-10-23 thomas c = getc(file->stream);
370 3efd8e31 2022-10-23 thomas if (c == EOF)
371 3efd8e31 2022-10-23 thomas yyerror("reached end of file while parsing "
372 3efd8e31 2022-10-23 thomas "quoted string");
373 3efd8e31 2022-10-23 thomas return (c);
376 3efd8e31 2022-10-23 thomas c = getc(file->stream);
377 3efd8e31 2022-10-23 thomas while (c == '\\') {
378 3efd8e31 2022-10-23 thomas next = getc(file->stream);
379 3efd8e31 2022-10-23 thomas if (next != '\n') {
380 3efd8e31 2022-10-23 thomas c = next;
383 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
384 3efd8e31 2022-10-23 thomas file->lineno++;
385 3efd8e31 2022-10-23 thomas c = getc(file->stream);
388 3efd8e31 2022-10-23 thomas return (c);
392 3efd8e31 2022-10-23 thomas lungetc(int c)
394 3efd8e31 2022-10-23 thomas if (c == EOF)
395 3efd8e31 2022-10-23 thomas return (EOF);
396 3efd8e31 2022-10-23 thomas if (parsebuf) {
397 3efd8e31 2022-10-23 thomas parseindex--;
398 3efd8e31 2022-10-23 thomas if (parseindex >= 0)
399 3efd8e31 2022-10-23 thomas return (c);
401 3efd8e31 2022-10-23 thomas if (pushback_index < MAXPUSHBACK-1)
402 3efd8e31 2022-10-23 thomas return (pushback_buffer[pushback_index++] = c);
404 3efd8e31 2022-10-23 thomas return (EOF);
408 3efd8e31 2022-10-23 thomas findeol(void)
412 3efd8e31 2022-10-23 thomas parsebuf = NULL;
414 3efd8e31 2022-10-23 thomas /* Skip to either EOF or the first real EOL. */
415 3efd8e31 2022-10-23 thomas while (1) {
416 3efd8e31 2022-10-23 thomas if (pushback_index)
417 3efd8e31 2022-10-23 thomas c = pushback_buffer[--pushback_index];
419 3efd8e31 2022-10-23 thomas c = lgetc(0);
420 3efd8e31 2022-10-23 thomas if (c == '\n') {
421 3efd8e31 2022-10-23 thomas file->lineno++;
424 3efd8e31 2022-10-23 thomas if (c == EOF)
427 3efd8e31 2022-10-23 thomas return (ERROR);
431 3efd8e31 2022-10-23 thomas yylex(void)
433 3efd8e31 2022-10-23 thomas unsigned char buf[8096];
434 3efd8e31 2022-10-23 thomas unsigned char *p, *val;
435 3efd8e31 2022-10-23 thomas int quotec, next, c;
436 3efd8e31 2022-10-23 thomas int token;
440 3efd8e31 2022-10-23 thomas c = lgetc(0);
441 3efd8e31 2022-10-23 thomas while (c == ' ' || c == '\t')
442 3efd8e31 2022-10-23 thomas c = lgetc(0); /* nothing */
444 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
445 3efd8e31 2022-10-23 thomas if (c == '#') {
446 3efd8e31 2022-10-23 thomas c = lgetc(0);
447 3efd8e31 2022-10-23 thomas while (c != '\n' && c != EOF)
448 3efd8e31 2022-10-23 thomas c = lgetc(0); /* nothing */
450 3efd8e31 2022-10-23 thomas if (c == '$' && parsebuf == NULL) {
451 3efd8e31 2022-10-23 thomas while (1) {
452 3efd8e31 2022-10-23 thomas c = lgetc(0);
453 3efd8e31 2022-10-23 thomas if (c == EOF)
454 3efd8e31 2022-10-23 thomas return (0);
456 3efd8e31 2022-10-23 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
457 3efd8e31 2022-10-23 thomas yyerror("string too long");
458 3efd8e31 2022-10-23 thomas return (findeol());
460 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == '_') {
461 3efd8e31 2022-10-23 thomas *p++ = c;
462 3efd8e31 2022-10-23 thomas continue;
464 3efd8e31 2022-10-23 thomas *p = '\0';
465 3efd8e31 2022-10-23 thomas lungetc(c);
468 3efd8e31 2022-10-23 thomas val = symget(buf);
469 3efd8e31 2022-10-23 thomas if (val == NULL) {
470 3efd8e31 2022-10-23 thomas yyerror("macro '%s' not defined", buf);
471 3efd8e31 2022-10-23 thomas return (findeol());
473 3efd8e31 2022-10-23 thomas parsebuf = val;
474 3efd8e31 2022-10-23 thomas parseindex = 0;
475 3efd8e31 2022-10-23 thomas goto top;
478 3efd8e31 2022-10-23 thomas switch (c) {
479 3efd8e31 2022-10-23 thomas case '\'':
480 3efd8e31 2022-10-23 thomas case '"':
481 3efd8e31 2022-10-23 thomas quotec = c;
482 3efd8e31 2022-10-23 thomas while (1) {
483 3efd8e31 2022-10-23 thomas c = lgetc(quotec);
484 3efd8e31 2022-10-23 thomas if (c == EOF)
485 3efd8e31 2022-10-23 thomas return (0);
486 3efd8e31 2022-10-23 thomas if (c == '\n') {
487 3efd8e31 2022-10-23 thomas file->lineno++;
488 3efd8e31 2022-10-23 thomas continue;
489 3efd8e31 2022-10-23 thomas } else if (c == '\\') {
490 3efd8e31 2022-10-23 thomas next = lgetc(quotec);
491 3efd8e31 2022-10-23 thomas if (next == EOF)
492 3efd8e31 2022-10-23 thomas return (0);
493 3efd8e31 2022-10-23 thomas if (next == quotec || c == ' ' || c == '\t')
494 3efd8e31 2022-10-23 thomas c = next;
495 3efd8e31 2022-10-23 thomas else if (next == '\n') {
496 3efd8e31 2022-10-23 thomas file->lineno++;
497 3efd8e31 2022-10-23 thomas continue;
499 3efd8e31 2022-10-23 thomas lungetc(next);
500 3efd8e31 2022-10-23 thomas } else if (c == quotec) {
501 3efd8e31 2022-10-23 thomas *p = '\0';
503 3efd8e31 2022-10-23 thomas } else if (c == '\0') {
504 3efd8e31 2022-10-23 thomas yyerror("syntax error");
505 3efd8e31 2022-10-23 thomas return (findeol());
507 3efd8e31 2022-10-23 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
508 3efd8e31 2022-10-23 thomas yyerror("string too long");
509 3efd8e31 2022-10-23 thomas return (findeol());
511 3efd8e31 2022-10-23 thomas *p++ = c;
513 3efd8e31 2022-10-23 thomas yylval.v.string = strdup(buf);
514 3efd8e31 2022-10-23 thomas if (yylval.v.string == NULL)
515 3efd8e31 2022-10-23 thomas err(1, "yylex: strdup");
516 3efd8e31 2022-10-23 thomas return (STRING);
519 3efd8e31 2022-10-23 thomas #define allowed_to_end_number(x) \
520 3efd8e31 2022-10-23 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
522 3efd8e31 2022-10-23 thomas if (c == '-' || isdigit(c)) {
524 3efd8e31 2022-10-23 thomas *p++ = c;
525 3efd8e31 2022-10-23 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
526 3efd8e31 2022-10-23 thomas yyerror("string too long");
527 3efd8e31 2022-10-23 thomas return (findeol());
529 3efd8e31 2022-10-23 thomas c = lgetc(0);
530 3efd8e31 2022-10-23 thomas } while (c != EOF && isdigit(c));
531 3efd8e31 2022-10-23 thomas lungetc(c);
532 3efd8e31 2022-10-23 thomas if (p == buf + 1 && buf[0] == '-')
533 3efd8e31 2022-10-23 thomas goto nodigits;
534 3efd8e31 2022-10-23 thomas if (c == EOF || allowed_to_end_number(c)) {
535 3efd8e31 2022-10-23 thomas const char *errstr = NULL;
537 3efd8e31 2022-10-23 thomas *p = '\0';
538 3efd8e31 2022-10-23 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
539 3efd8e31 2022-10-23 thomas LLONG_MAX, &errstr);
540 3efd8e31 2022-10-23 thomas if (errstr) {
541 3efd8e31 2022-10-23 thomas yyerror("\"%s\" invalid number: %s",
542 3efd8e31 2022-10-23 thomas buf, errstr);
543 3efd8e31 2022-10-23 thomas return (findeol());
545 3efd8e31 2022-10-23 thomas return (NUMBER);
547 3efd8e31 2022-10-23 thomas nodigits:
548 3efd8e31 2022-10-23 thomas while (p > buf + 1)
549 3efd8e31 2022-10-23 thomas lungetc(*--p);
550 3efd8e31 2022-10-23 thomas c = *--p;
551 3efd8e31 2022-10-23 thomas if (c == '-')
552 3efd8e31 2022-10-23 thomas return (c);
556 3efd8e31 2022-10-23 thomas #define allowed_in_string(x) \
557 3efd8e31 2022-10-23 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
558 3efd8e31 2022-10-23 thomas x != '{' && x != '}' && \
559 3efd8e31 2022-10-23 thomas x != '!' && x != '=' && x != '#' && \
560 3efd8e31 2022-10-23 thomas x != ','))
562 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == ':' || c == '_') {
564 3efd8e31 2022-10-23 thomas *p++ = c;
565 3efd8e31 2022-10-23 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
566 3efd8e31 2022-10-23 thomas yyerror("string too long");
567 3efd8e31 2022-10-23 thomas return (findeol());
569 3efd8e31 2022-10-23 thomas c = lgetc(0);
570 3efd8e31 2022-10-23 thomas } while (c != EOF && (allowed_in_string(c)));
571 3efd8e31 2022-10-23 thomas lungetc(c);
572 3efd8e31 2022-10-23 thomas *p = '\0';
573 3efd8e31 2022-10-23 thomas token = lookup(buf);
574 3efd8e31 2022-10-23 thomas if (token == STRING) {
575 3efd8e31 2022-10-23 thomas yylval.v.string = strdup(buf);
576 3efd8e31 2022-10-23 thomas if (yylval.v.string == NULL)
577 3efd8e31 2022-10-23 thomas err(1, "yylex: strdup");
579 3efd8e31 2022-10-23 thomas return (token);
581 3efd8e31 2022-10-23 thomas if (c == '\n') {
582 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
583 3efd8e31 2022-10-23 thomas file->lineno++;
585 3efd8e31 2022-10-23 thomas if (c == EOF)
586 3efd8e31 2022-10-23 thomas return (0);
587 3efd8e31 2022-10-23 thomas return (c);
591 3efd8e31 2022-10-23 thomas check_file_secrecy(int fd, const char *fname)
593 3efd8e31 2022-10-23 thomas struct stat st;
595 3efd8e31 2022-10-23 thomas if (fstat(fd, &st)) {
596 3efd8e31 2022-10-23 thomas log_warn("cannot stat %s", fname);
597 3efd8e31 2022-10-23 thomas return (-1);
599 3efd8e31 2022-10-23 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
600 3efd8e31 2022-10-23 thomas log_warnx("%s: owner not root or current user", fname);
601 3efd8e31 2022-10-23 thomas return (-1);
603 3efd8e31 2022-10-23 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
604 3efd8e31 2022-10-23 thomas log_warnx("%s: group writable or world read/writable", fname);
605 3efd8e31 2022-10-23 thomas return (-1);
607 3efd8e31 2022-10-23 thomas return (0);
610 3efd8e31 2022-10-23 thomas struct file *
611 3efd8e31 2022-10-23 thomas newfile(const char *name, int secret)
613 3efd8e31 2022-10-23 thomas struct file *nfile;
615 3efd8e31 2022-10-23 thomas nfile = calloc(1, sizeof(struct file));
616 3efd8e31 2022-10-23 thomas if (nfile == NULL) {
617 3efd8e31 2022-10-23 thomas log_warn("calloc");
618 3efd8e31 2022-10-23 thomas return (NULL);
620 3efd8e31 2022-10-23 thomas nfile->name = strdup(name);
621 3efd8e31 2022-10-23 thomas if (nfile->name == NULL) {
622 3efd8e31 2022-10-23 thomas log_warn("strdup");
623 3efd8e31 2022-10-23 thomas free(nfile);
624 3efd8e31 2022-10-23 thomas return (NULL);
626 3efd8e31 2022-10-23 thomas nfile->stream = fopen(nfile->name, "r");
627 3efd8e31 2022-10-23 thomas if (nfile->stream == NULL) {
628 3efd8e31 2022-10-23 thomas /* no warning, we don't require a conf file */
629 3efd8e31 2022-10-23 thomas free(nfile->name);
630 3efd8e31 2022-10-23 thomas free(nfile);
631 3efd8e31 2022-10-23 thomas return (NULL);
632 3efd8e31 2022-10-23 thomas } else if (secret &&
633 3efd8e31 2022-10-23 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
634 3efd8e31 2022-10-23 thomas fclose(nfile->stream);
635 3efd8e31 2022-10-23 thomas free(nfile->name);
636 3efd8e31 2022-10-23 thomas free(nfile);
637 3efd8e31 2022-10-23 thomas return (NULL);
639 3efd8e31 2022-10-23 thomas nfile->lineno = 1;
640 3efd8e31 2022-10-23 thomas return (nfile);
643 3efd8e31 2022-10-23 thomas static void
644 3efd8e31 2022-10-23 thomas closefile(struct file *xfile)
646 3efd8e31 2022-10-23 thomas fclose(xfile->stream);
647 3efd8e31 2022-10-23 thomas free(xfile->name);
648 3efd8e31 2022-10-23 thomas free(xfile);
652 3efd8e31 2022-10-23 thomas parse_config(const char *filename, enum gotd_procid proc_id,
653 3efd8e31 2022-10-23 thomas struct gotd *env)
655 3efd8e31 2022-10-23 thomas struct sym *sym, *next;
656 88f1bb6d 2023-01-02 thomas struct gotd_repo *repo;
658 3efd8e31 2022-10-23 thomas memset(env, 0, sizeof(*env));
660 3efd8e31 2022-10-23 thomas gotd = env;
661 3efd8e31 2022-10-23 thomas gotd_proc_id = proc_id;
662 3efd8e31 2022-10-23 thomas TAILQ_INIT(&gotd->repos);
664 3efd8e31 2022-10-23 thomas /* Apply default values. */
665 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
666 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
667 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: unix socket path too long", __func__);
668 3efd8e31 2022-10-23 thomas return -1;
670 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->unix_group_name, GOTD_UNIX_GROUP,
671 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_group_name)) >= sizeof(gotd->unix_group_name)) {
672 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: unix group name too long", __func__);
673 3efd8e31 2022-10-23 thomas return -1;
675 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->user_name, GOTD_USER,
676 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
677 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: user name too long", __func__);
678 3efd8e31 2022-10-23 thomas return -1;
681 0781db0e 2023-01-06 thomas gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
682 0781db0e 2023-01-06 thomas gotd->request_timeout.tv_usec = 0;
684 3efd8e31 2022-10-23 thomas file = newfile(filename, 0);
685 3efd8e31 2022-10-23 thomas if (file == NULL) {
686 3efd8e31 2022-10-23 thomas /* just return, as we don't require a conf file */
687 3efd8e31 2022-10-23 thomas return (0);
690 3efd8e31 2022-10-23 thomas yyparse();
691 3efd8e31 2022-10-23 thomas errors = file->errors;
692 3efd8e31 2022-10-23 thomas closefile(file);
694 3efd8e31 2022-10-23 thomas /* Free macros and check which have not been used. */
695 3efd8e31 2022-10-23 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
696 3efd8e31 2022-10-23 thomas if ((gotd->verbosity > 1) && !sym->used)
697 3efd8e31 2022-10-23 thomas fprintf(stderr, "warning: macro '%s' not used\n",
698 3efd8e31 2022-10-23 thomas sym->nam);
699 3efd8e31 2022-10-23 thomas if (!sym->persist) {
700 3efd8e31 2022-10-23 thomas free(sym->nam);
701 3efd8e31 2022-10-23 thomas free(sym->val);
702 3efd8e31 2022-10-23 thomas TAILQ_REMOVE(&symhead, sym, entry);
703 3efd8e31 2022-10-23 thomas free(sym);
707 3efd8e31 2022-10-23 thomas if (errors)
708 3efd8e31 2022-10-23 thomas return (-1);
710 88f1bb6d 2023-01-02 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
711 88f1bb6d 2023-01-02 thomas if (repo->path[0] == '\0') {
712 0cbf8de7 2023-01-02 thomas log_warnx("repository \"%s\": no path provided in "
713 0cbf8de7 2023-01-02 thomas "configuration file", repo->name);
714 88f1bb6d 2023-01-02 thomas return (-1);
718 3efd8e31 2022-10-23 thomas return (0);
721 0781db0e 2023-01-06 thomas static int
722 0781db0e 2023-01-06 thomas uid_connection_limit_cmp(const void *pa, const void *pb)
724 0781db0e 2023-01-06 thomas const struct gotd_uid_connection_limit *a = pa, *b = pb;
726 0781db0e 2023-01-06 thomas if (a->uid < b->uid)
727 0781db0e 2023-01-06 thomas return -1;
728 0781db0e 2023-01-06 thomas else if (a->uid > b->uid);
729 0781db0e 2023-01-06 thomas return 1;
731 0781db0e 2023-01-06 thomas return 0;
734 0781db0e 2023-01-06 thomas static int
735 0781db0e 2023-01-06 thomas conf_limit_user_connections(const char *user, int maximum)
737 0781db0e 2023-01-06 thomas uid_t uid;
738 0781db0e 2023-01-06 thomas struct gotd_uid_connection_limit *limit;
739 0781db0e 2023-01-06 thomas size_t nlimits;
741 0781db0e 2023-01-06 thomas if (maximum < 1) {
742 0781db0e 2023-01-06 thomas yyerror("max connections cannot be smaller 1");
743 0781db0e 2023-01-06 thomas return -1;
745 0781db0e 2023-01-06 thomas if (maximum > GOTD_MAXCLIENTS) {
746 0781db0e 2023-01-06 thomas yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
747 0781db0e 2023-01-06 thomas return -1;
750 0781db0e 2023-01-06 thomas if (gotd_auth_parseuid(user, &uid) == -1) {
751 0781db0e 2023-01-06 thomas yyerror("%s: no such user", user);
752 0781db0e 2023-01-06 thomas return -1;
755 0781db0e 2023-01-06 thomas limit = gotd_find_uid_connection_limit(gotd->connection_limits,
756 0781db0e 2023-01-06 thomas gotd->nconnection_limits, uid);
757 0781db0e 2023-01-06 thomas if (limit) {
758 0781db0e 2023-01-06 thomas limit->max_connections = maximum;
759 0781db0e 2023-01-06 thomas return 0;
762 0781db0e 2023-01-06 thomas limit = gotd->connection_limits;
763 0781db0e 2023-01-06 thomas nlimits = gotd->nconnection_limits + 1;
764 0781db0e 2023-01-06 thomas limit = reallocarray(limit, nlimits, sizeof(*limit));
765 0781db0e 2023-01-06 thomas if (limit == NULL)
766 0781db0e 2023-01-06 thomas fatal("reallocarray");
768 0781db0e 2023-01-06 thomas limit[nlimits - 1].uid = uid;
769 0781db0e 2023-01-06 thomas limit[nlimits - 1].max_connections = maximum;
771 0781db0e 2023-01-06 thomas gotd->connection_limits = limit;
772 0781db0e 2023-01-06 thomas gotd->nconnection_limits = nlimits;
773 0781db0e 2023-01-06 thomas qsort(gotd->connection_limits, gotd->nconnection_limits,
774 0781db0e 2023-01-06 thomas sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
776 0781db0e 2023-01-06 thomas return 0;
779 3efd8e31 2022-10-23 thomas static struct gotd_repo *
780 3efd8e31 2022-10-23 thomas conf_new_repo(const char *name)
782 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
784 a42b418b 2023-01-02 thomas if (name[0] == '\0') {
785 a42b418b 2023-01-02 thomas fatalx("syntax error: empty repository name found in %s",
786 a42b418b 2023-01-02 thomas file->name);
789 0cbf8de7 2023-01-02 thomas if (strchr(name, '\n') != NULL)
790 0cbf8de7 2023-01-02 thomas fatalx("repository names must not contain linefeeds: %s", name);
792 3efd8e31 2022-10-23 thomas repo = calloc(1, sizeof(*repo));
793 3efd8e31 2022-10-23 thomas if (repo == NULL)
794 3efd8e31 2022-10-23 thomas fatalx("%s: calloc", __func__);
796 729a7e24 2022-11-17 thomas STAILQ_INIT(&repo->rules);
798 3efd8e31 2022-10-23 thomas if (strlcpy(repo->name, name, sizeof(repo->name)) >=
799 3efd8e31 2022-10-23 thomas sizeof(repo->name))
800 3efd8e31 2022-10-23 thomas fatalx("%s: strlcpy", __func__);
802 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
803 3efd8e31 2022-10-23 thomas gotd->nrepos++;
805 3efd8e31 2022-10-23 thomas return repo;
808 729a7e24 2022-11-17 thomas static void
809 729a7e24 2022-11-17 thomas conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
810 729a7e24 2022-11-17 thomas int authorization, char *identifier)
812 729a7e24 2022-11-17 thomas struct gotd_access_rule *rule;
814 729a7e24 2022-11-17 thomas rule = calloc(1, sizeof(*rule));
815 729a7e24 2022-11-17 thomas if (rule == NULL)
816 729a7e24 2022-11-17 thomas fatal("calloc");
818 729a7e24 2022-11-17 thomas rule->access = access;
819 729a7e24 2022-11-17 thomas rule->authorization = authorization;
820 729a7e24 2022-11-17 thomas rule->identifier = identifier;
822 729a7e24 2022-11-17 thomas STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
826 3efd8e31 2022-10-23 thomas symset(const char *nam, const char *val, int persist)
828 3efd8e31 2022-10-23 thomas struct sym *sym;
830 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
831 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0)
835 3efd8e31 2022-10-23 thomas if (sym != NULL) {
836 3efd8e31 2022-10-23 thomas if (sym->persist == 1)
837 3efd8e31 2022-10-23 thomas return (0);
839 3efd8e31 2022-10-23 thomas free(sym->nam);
840 3efd8e31 2022-10-23 thomas free(sym->val);
841 3efd8e31 2022-10-23 thomas TAILQ_REMOVE(&symhead, sym, entry);
842 3efd8e31 2022-10-23 thomas free(sym);
845 3efd8e31 2022-10-23 thomas sym = calloc(1, sizeof(*sym));
846 3efd8e31 2022-10-23 thomas if (sym == NULL)
847 3efd8e31 2022-10-23 thomas return (-1);
849 3efd8e31 2022-10-23 thomas sym->nam = strdup(nam);
850 3efd8e31 2022-10-23 thomas if (sym->nam == NULL) {
851 3efd8e31 2022-10-23 thomas free(sym);
852 3efd8e31 2022-10-23 thomas return (-1);
854 3efd8e31 2022-10-23 thomas sym->val = strdup(val);
855 3efd8e31 2022-10-23 thomas if (sym->val == NULL) {
856 3efd8e31 2022-10-23 thomas free(sym->nam);
857 3efd8e31 2022-10-23 thomas free(sym);
858 3efd8e31 2022-10-23 thomas return (-1);
860 3efd8e31 2022-10-23 thomas sym->used = 0;
861 3efd8e31 2022-10-23 thomas sym->persist = persist;
862 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
863 3efd8e31 2022-10-23 thomas return (0);
867 3efd8e31 2022-10-23 thomas symget(const char *nam)
869 3efd8e31 2022-10-23 thomas struct sym *sym;
871 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
872 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0) {
873 3efd8e31 2022-10-23 thomas sym->used = 1;
874 3efd8e31 2022-10-23 thomas return (sym->val);
877 3efd8e31 2022-10-23 thomas return (NULL);