2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 13b2bc37 2022-10-23 stsp * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
4 13b2bc37 2022-10-23 stsp * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 13b2bc37 2022-10-23 stsp * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 13b2bc37 2022-10-23 stsp * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 13b2bc37 2022-10-23 stsp * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 13b2bc37 2022-10-23 stsp * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 13b2bc37 2022-10-23 stsp * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 13b2bc37 2022-10-23 stsp * Permission to use, copy, modify, and distribute this software for any
12 13b2bc37 2022-10-23 stsp * purpose with or without fee is hereby granted, provided that the above
13 13b2bc37 2022-10-23 stsp * copyright notice and this permission notice appear in all copies.
15 13b2bc37 2022-10-23 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 13b2bc37 2022-10-23 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 13b2bc37 2022-10-23 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 13b2bc37 2022-10-23 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 13b2bc37 2022-10-23 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 13b2bc37 2022-10-23 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 13b2bc37 2022-10-23 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 13b2bc37 2022-10-23 stsp #include <sys/time.h>
26 13b2bc37 2022-10-23 stsp #include <sys/types.h>
27 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
28 13b2bc37 2022-10-23 stsp #include <sys/stat.h>
30 13b2bc37 2022-10-23 stsp #include <ctype.h>
31 13b2bc37 2022-10-23 stsp #include <err.h>
32 13b2bc37 2022-10-23 stsp #include <errno.h>
33 13b2bc37 2022-10-23 stsp #include <event.h>
34 13b2bc37 2022-10-23 stsp #include <imsg.h>
35 13b2bc37 2022-10-23 stsp #include <limits.h>
36 13b2bc37 2022-10-23 stsp #include <sha1.h>
37 13b2bc37 2022-10-23 stsp #include <stdarg.h>
38 13b2bc37 2022-10-23 stsp #include <stdlib.h>
39 13b2bc37 2022-10-23 stsp #include <stdio.h>
40 13b2bc37 2022-10-23 stsp #include <string.h>
41 13b2bc37 2022-10-23 stsp #include <syslog.h>
42 13b2bc37 2022-10-23 stsp #include <unistd.h>
44 13b2bc37 2022-10-23 stsp #include "got_error.h"
45 13b2bc37 2022-10-23 stsp #include "got_path.h"
47 13b2bc37 2022-10-23 stsp #include "log.h"
48 13b2bc37 2022-10-23 stsp #include "gotd.h"
49 40b85cca 2023-01-03 stsp #include "auth.h"
50 40b85cca 2023-01-03 stsp #include "listen.h"
52 13b2bc37 2022-10-23 stsp TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
53 13b2bc37 2022-10-23 stsp static struct file {
54 13b2bc37 2022-10-23 stsp TAILQ_ENTRY(file) entry;
55 13b2bc37 2022-10-23 stsp FILE *stream;
60 13b2bc37 2022-10-23 stsp struct file *newfile(const char *, int);
61 13b2bc37 2022-10-23 stsp static void closefile(struct file *);
62 13b2bc37 2022-10-23 stsp int check_file_secrecy(int, const char *);
63 13b2bc37 2022-10-23 stsp int yyparse(void);
64 13b2bc37 2022-10-23 stsp int yylex(void);
65 13b2bc37 2022-10-23 stsp int yyerror(const char *, ...)
66 13b2bc37 2022-10-23 stsp __attribute__((__format__ (printf, 1, 2)))
67 13b2bc37 2022-10-23 stsp __attribute__((__nonnull__ (1)));
68 13b2bc37 2022-10-23 stsp int kw_cmp(const void *, const void *);
69 13b2bc37 2022-10-23 stsp int lookup(char *);
70 13b2bc37 2022-10-23 stsp int lgetc(int);
71 13b2bc37 2022-10-23 stsp int lungetc(int);
72 13b2bc37 2022-10-23 stsp int findeol(void);
74 13b2bc37 2022-10-23 stsp TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 13b2bc37 2022-10-23 stsp struct sym {
76 13b2bc37 2022-10-23 stsp TAILQ_ENTRY(sym) entry;
78 13b2bc37 2022-10-23 stsp int persist;
83 13b2bc37 2022-10-23 stsp int symset(const char *, const char *, int);
84 13b2bc37 2022-10-23 stsp char *symget(const char *);
86 13b2bc37 2022-10-23 stsp static int errors;
88 13b2bc37 2022-10-23 stsp static struct gotd *gotd;
89 13b2bc37 2022-10-23 stsp static struct gotd_repo *new_repo;
90 fc89c900 2023-01-03 op static int conf_limit_user_connections(const char *, int);
91 13b2bc37 2022-10-23 stsp static struct gotd_repo *conf_new_repo(const char *);
92 533abaaa 2022-11-18 op static void conf_new_access_rule(struct gotd_repo *,
93 533abaaa 2022-11-18 op enum gotd_access, int, char *);
94 13b2bc37 2022-10-23 stsp static enum gotd_procid gotd_proc_id;
96 13b2bc37 2022-10-23 stsp typedef struct {
98 13b2bc37 2022-10-23 stsp long long number;
99 13b2bc37 2022-10-23 stsp char *string;
100 40b85cca 2023-01-03 stsp struct timeval tv;
102 13b2bc37 2022-10-23 stsp int lineno;
107 6f854dde 2023-01-04 stsp %token PATH ERROR ON UNIX_SOCKET USER REPOSITORY PERMIT DENY
108 40b85cca 2023-01-03 stsp %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
110 13b2bc37 2022-10-23 stsp %token <v.string> STRING
111 13b2bc37 2022-10-23 stsp %token <v.number> NUMBER
112 13b2bc37 2022-10-23 stsp %type <v.number> boolean
113 40b85cca 2023-01-03 stsp %type <v.tv> timeout
118 13b2bc37 2022-10-23 stsp | grammar '\n'
119 13b2bc37 2022-10-23 stsp | grammar main '\n'
120 13b2bc37 2022-10-23 stsp | grammar repository '\n'
123 13b2bc37 2022-10-23 stsp boolean : STRING {
124 13b2bc37 2022-10-23 stsp if (strcasecmp($1, "1") == 0 ||
125 13b2bc37 2022-10-23 stsp strcasecmp($1, "yes") == 0 ||
126 13b2bc37 2022-10-23 stsp strcasecmp($1, "on") == 0)
128 13b2bc37 2022-10-23 stsp else if (strcasecmp($1, "0") == 0 ||
129 13b2bc37 2022-10-23 stsp strcasecmp($1, "off") == 0 ||
130 13b2bc37 2022-10-23 stsp strcasecmp($1, "no") == 0)
133 13b2bc37 2022-10-23 stsp yyerror("invalid boolean value '%s'", $1);
139 13b2bc37 2022-10-23 stsp | ON { $$ = 1; }
140 13b2bc37 2022-10-23 stsp | NUMBER { $$ = $1; }
143 40b85cca 2023-01-03 stsp timeout : NUMBER {
144 40b85cca 2023-01-03 stsp if ($1 < 0) {
145 40b85cca 2023-01-03 stsp yyerror("invalid timeout: %lld", $1);
148 40b85cca 2023-01-03 stsp $$.tv_sec = $1;
149 40b85cca 2023-01-03 stsp $$.tv_usec = 0;
152 2be11cde 2023-01-03 op const char *errstr;
153 2be11cde 2023-01-03 op const char *type = "seconds";
157 2be11cde 2023-01-03 op if (*$1 == '\0') {
158 2be11cde 2023-01-03 op yyerror("invalid number of seconds: %s", $1);
163 2be11cde 2023-01-03 op len = strlen($1);
164 2be11cde 2023-01-03 op switch ($1[len - 1]) {
167 2be11cde 2023-01-03 op $1[len - 1] = '\0';
171 2be11cde 2023-01-03 op type = "minutes";
173 2be11cde 2023-01-03 op $1[len - 1] = '\0';
177 2be11cde 2023-01-03 op type = "hours";
178 2be11cde 2023-01-03 op mul = 60 * 60;
179 2be11cde 2023-01-03 op $1[len - 1] = '\0';
183 2be11cde 2023-01-03 op $$.tv_usec = 0;
184 71cd355c 2023-01-03 op $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
185 2be11cde 2023-01-03 op if (errstr) {
186 2be11cde 2023-01-03 op yyerror("number of %s is %s: %s", type,
192 2be11cde 2023-01-03 op $$.tv_sec *= mul;
197 13b2bc37 2022-10-23 stsp main : UNIX_SOCKET STRING {
198 d93ecf7d 2022-12-14 stsp if (gotd_proc_id == PROC_LISTEN) {
199 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->unix_socket_path, $2,
200 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) >=
201 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) {
202 13b2bc37 2022-10-23 stsp yyerror("%s: unix socket path too long",
210 13b2bc37 2022-10-23 stsp | USER STRING {
211 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->user_name, $2,
212 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) >=
213 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) {
214 13b2bc37 2022-10-23 stsp yyerror("%s: user name too long", __func__);
220 40b85cca 2023-01-03 stsp | connection
223 40b85cca 2023-01-03 stsp connection : CONNECTION '{' optnl conflags_l '}'
224 40b85cca 2023-01-03 stsp | CONNECTION conflags
226 40b85cca 2023-01-03 stsp conflags_l : conflags optnl conflags_l
227 40b85cca 2023-01-03 stsp | conflags optnl
230 40b85cca 2023-01-03 stsp conflags : REQUEST TIMEOUT timeout {
231 46e48ac7 2023-01-03 stsp if ($3.tv_sec <= 0) {
232 46e48ac7 2023-01-03 stsp yyerror("invalid timeout: %lld", $3.tv_sec);
235 40b85cca 2023-01-03 stsp memcpy(&gotd->request_timeout, &$3,
236 40b85cca 2023-01-03 stsp sizeof(gotd->request_timeout));
238 40b85cca 2023-01-03 stsp | LIMIT USER STRING NUMBER {
239 40b85cca 2023-01-03 stsp if (gotd_proc_id == PROC_LISTEN &&
240 40b85cca 2023-01-03 stsp conf_limit_user_connections($3, $4) == -1) {
248 13b2bc37 2022-10-23 stsp repository : REPOSITORY STRING {
249 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
251 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
252 13b2bc37 2022-10-23 stsp if (strcmp(repo->name, $2) == 0) {
253 13b2bc37 2022-10-23 stsp yyerror("duplicate repository '%s'", $2);
259 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_GOTD ||
260 5e25db14 2022-12-29 stsp gotd_proc_id == PROC_AUTH) {
261 13b2bc37 2022-10-23 stsp new_repo = conf_new_repo($2);
264 13b2bc37 2022-10-23 stsp } '{' optnl repoopts2 '}' {
268 13b2bc37 2022-10-23 stsp repoopts1 : PATH STRING {
269 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_GOTD ||
270 5e25db14 2022-12-29 stsp gotd_proc_id == PROC_AUTH) {
271 13b2bc37 2022-10-23 stsp if (!got_path_is_absolute($2)) {
272 13b2bc37 2022-10-23 stsp yyerror("%s: path %s is not absolute",
273 13b2bc37 2022-10-23 stsp __func__, $2);
277 13b2bc37 2022-10-23 stsp if (strlcpy(new_repo->path, $2,
278 13b2bc37 2022-10-23 stsp sizeof(new_repo->path)) >=
279 13b2bc37 2022-10-23 stsp sizeof(new_repo->path)) {
280 13b2bc37 2022-10-23 stsp yyerror("%s: path truncated", __func__);
287 0ccf3acb 2022-11-16 stsp | PERMIT RO STRING {
288 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
289 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
290 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
293 0ccf3acb 2022-11-16 stsp | PERMIT RW STRING {
294 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
295 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
296 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_PERMITTED,
297 0ccf3acb 2022-11-16 stsp GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
300 0ccf3acb 2022-11-16 stsp | DENY STRING {
301 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
302 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
303 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_DENIED, 0, $2);
308 13b2bc37 2022-10-23 stsp repoopts2 : repoopts2 repoopts1 nl
309 13b2bc37 2022-10-23 stsp | repoopts1 optnl
312 13b2bc37 2022-10-23 stsp nl : '\n' optnl
315 13b2bc37 2022-10-23 stsp optnl : '\n' optnl /* zero or more newlines */
316 13b2bc37 2022-10-23 stsp | /* empty */
321 13b2bc37 2022-10-23 stsp struct keywords {
322 13b2bc37 2022-10-23 stsp const char *k_name;
327 13b2bc37 2022-10-23 stsp yyerror(const char *fmt, ...)
329 13b2bc37 2022-10-23 stsp va_list ap;
332 13b2bc37 2022-10-23 stsp file->errors++;
333 13b2bc37 2022-10-23 stsp va_start(ap, fmt);
334 13b2bc37 2022-10-23 stsp if (vasprintf(&msg, fmt, ap) == -1)
335 13b2bc37 2022-10-23 stsp fatalx("yyerror vasprintf");
336 13b2bc37 2022-10-23 stsp va_end(ap);
337 13b2bc37 2022-10-23 stsp logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
339 13b2bc37 2022-10-23 stsp return (0);
343 13b2bc37 2022-10-23 stsp kw_cmp(const void *k, const void *e)
345 13b2bc37 2022-10-23 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
349 13b2bc37 2022-10-23 stsp lookup(char *s)
351 13b2bc37 2022-10-23 stsp /* This has to be sorted always. */
352 13b2bc37 2022-10-23 stsp static const struct keywords keywords[] = {
353 40b85cca 2023-01-03 stsp { "connection", CONNECTION },
354 0ccf3acb 2022-11-16 stsp { "deny", DENY },
355 40b85cca 2023-01-03 stsp { "limit", LIMIT },
356 13b2bc37 2022-10-23 stsp { "on", ON },
357 13b2bc37 2022-10-23 stsp { "path", PATH },
358 0ccf3acb 2022-11-16 stsp { "permit", PERMIT },
359 13b2bc37 2022-10-23 stsp { "repository", REPOSITORY },
360 40b85cca 2023-01-03 stsp { "request", REQUEST },
361 0ccf3acb 2022-11-16 stsp { "ro", RO },
362 0ccf3acb 2022-11-16 stsp { "rw", RW },
363 40b85cca 2023-01-03 stsp { "timeout", TIMEOUT },
364 13b2bc37 2022-10-23 stsp { "unix_socket", UNIX_SOCKET },
365 13b2bc37 2022-10-23 stsp { "user", USER },
367 13b2bc37 2022-10-23 stsp const struct keywords *p;
369 13b2bc37 2022-10-23 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
370 13b2bc37 2022-10-23 stsp sizeof(keywords[0]), kw_cmp);
373 13b2bc37 2022-10-23 stsp return (p->k_val);
375 13b2bc37 2022-10-23 stsp return (STRING);
378 13b2bc37 2022-10-23 stsp #define MAXPUSHBACK 128
380 13b2bc37 2022-10-23 stsp unsigned char *parsebuf;
381 13b2bc37 2022-10-23 stsp int parseindex;
382 13b2bc37 2022-10-23 stsp unsigned char pushback_buffer[MAXPUSHBACK];
383 13b2bc37 2022-10-23 stsp int pushback_index = 0;
386 13b2bc37 2022-10-23 stsp lgetc(int quotec)
388 13b2bc37 2022-10-23 stsp int c, next;
390 13b2bc37 2022-10-23 stsp if (parsebuf) {
391 13b2bc37 2022-10-23 stsp /* Read character from the parsebuffer instead of input. */
392 13b2bc37 2022-10-23 stsp if (parseindex >= 0) {
393 13b2bc37 2022-10-23 stsp c = parsebuf[parseindex++];
394 13b2bc37 2022-10-23 stsp if (c != '\0')
395 13b2bc37 2022-10-23 stsp return (c);
396 13b2bc37 2022-10-23 stsp parsebuf = NULL;
398 13b2bc37 2022-10-23 stsp parseindex++;
401 13b2bc37 2022-10-23 stsp if (pushback_index)
402 13b2bc37 2022-10-23 stsp return (pushback_buffer[--pushback_index]);
404 13b2bc37 2022-10-23 stsp if (quotec) {
405 13b2bc37 2022-10-23 stsp c = getc(file->stream);
406 13b2bc37 2022-10-23 stsp if (c == EOF)
407 13b2bc37 2022-10-23 stsp yyerror("reached end of file while parsing "
408 13b2bc37 2022-10-23 stsp "quoted string");
409 13b2bc37 2022-10-23 stsp return (c);
412 13b2bc37 2022-10-23 stsp c = getc(file->stream);
413 13b2bc37 2022-10-23 stsp while (c == '\\') {
414 13b2bc37 2022-10-23 stsp next = getc(file->stream);
415 13b2bc37 2022-10-23 stsp if (next != '\n') {
419 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
420 13b2bc37 2022-10-23 stsp file->lineno++;
421 13b2bc37 2022-10-23 stsp c = getc(file->stream);
424 13b2bc37 2022-10-23 stsp return (c);
428 13b2bc37 2022-10-23 stsp lungetc(int c)
430 13b2bc37 2022-10-23 stsp if (c == EOF)
431 13b2bc37 2022-10-23 stsp return (EOF);
432 13b2bc37 2022-10-23 stsp if (parsebuf) {
433 13b2bc37 2022-10-23 stsp parseindex--;
434 13b2bc37 2022-10-23 stsp if (parseindex >= 0)
435 13b2bc37 2022-10-23 stsp return (c);
437 13b2bc37 2022-10-23 stsp if (pushback_index < MAXPUSHBACK-1)
438 13b2bc37 2022-10-23 stsp return (pushback_buffer[pushback_index++] = c);
440 13b2bc37 2022-10-23 stsp return (EOF);
444 13b2bc37 2022-10-23 stsp findeol(void)
448 13b2bc37 2022-10-23 stsp parsebuf = NULL;
450 13b2bc37 2022-10-23 stsp /* Skip to either EOF or the first real EOL. */
451 13b2bc37 2022-10-23 stsp while (1) {
452 13b2bc37 2022-10-23 stsp if (pushback_index)
453 13b2bc37 2022-10-23 stsp c = pushback_buffer[--pushback_index];
455 13b2bc37 2022-10-23 stsp c = lgetc(0);
456 13b2bc37 2022-10-23 stsp if (c == '\n') {
457 13b2bc37 2022-10-23 stsp file->lineno++;
460 13b2bc37 2022-10-23 stsp if (c == EOF)
463 13b2bc37 2022-10-23 stsp return (ERROR);
467 13b2bc37 2022-10-23 stsp yylex(void)
469 13b2bc37 2022-10-23 stsp unsigned char buf[8096];
470 13b2bc37 2022-10-23 stsp unsigned char *p, *val;
471 13b2bc37 2022-10-23 stsp int quotec, next, c;
476 13b2bc37 2022-10-23 stsp c = lgetc(0);
477 13b2bc37 2022-10-23 stsp while (c == ' ' || c == '\t')
478 13b2bc37 2022-10-23 stsp c = lgetc(0); /* nothing */
480 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
481 13b2bc37 2022-10-23 stsp if (c == '#') {
482 13b2bc37 2022-10-23 stsp c = lgetc(0);
483 13b2bc37 2022-10-23 stsp while (c != '\n' && c != EOF)
484 13b2bc37 2022-10-23 stsp c = lgetc(0); /* nothing */
486 13b2bc37 2022-10-23 stsp if (c == '$' && parsebuf == NULL) {
487 13b2bc37 2022-10-23 stsp while (1) {
488 13b2bc37 2022-10-23 stsp c = lgetc(0);
489 13b2bc37 2022-10-23 stsp if (c == EOF)
490 13b2bc37 2022-10-23 stsp return (0);
492 13b2bc37 2022-10-23 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
493 13b2bc37 2022-10-23 stsp yyerror("string too long");
494 13b2bc37 2022-10-23 stsp return (findeol());
496 13b2bc37 2022-10-23 stsp if (isalnum(c) || c == '_') {
501 13b2bc37 2022-10-23 stsp lungetc(c);
504 13b2bc37 2022-10-23 stsp val = symget(buf);
505 13b2bc37 2022-10-23 stsp if (val == NULL) {
506 13b2bc37 2022-10-23 stsp yyerror("macro '%s' not defined", buf);
507 13b2bc37 2022-10-23 stsp return (findeol());
509 13b2bc37 2022-10-23 stsp parsebuf = val;
510 13b2bc37 2022-10-23 stsp parseindex = 0;
514 13b2bc37 2022-10-23 stsp switch (c) {
517 13b2bc37 2022-10-23 stsp quotec = c;
518 13b2bc37 2022-10-23 stsp while (1) {
519 13b2bc37 2022-10-23 stsp c = lgetc(quotec);
520 13b2bc37 2022-10-23 stsp if (c == EOF)
521 13b2bc37 2022-10-23 stsp return (0);
522 13b2bc37 2022-10-23 stsp if (c == '\n') {
523 13b2bc37 2022-10-23 stsp file->lineno++;
525 13b2bc37 2022-10-23 stsp } else if (c == '\\') {
526 13b2bc37 2022-10-23 stsp next = lgetc(quotec);
527 13b2bc37 2022-10-23 stsp if (next == EOF)
528 13b2bc37 2022-10-23 stsp return (0);
529 13b2bc37 2022-10-23 stsp if (next == quotec || c == ' ' || c == '\t')
531 13b2bc37 2022-10-23 stsp else if (next == '\n') {
532 13b2bc37 2022-10-23 stsp file->lineno++;
535 13b2bc37 2022-10-23 stsp lungetc(next);
536 13b2bc37 2022-10-23 stsp } else if (c == quotec) {
539 13b2bc37 2022-10-23 stsp } else if (c == '\0') {
540 13b2bc37 2022-10-23 stsp yyerror("syntax error");
541 13b2bc37 2022-10-23 stsp return (findeol());
543 13b2bc37 2022-10-23 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
544 13b2bc37 2022-10-23 stsp yyerror("string too long");
545 13b2bc37 2022-10-23 stsp return (findeol());
549 13b2bc37 2022-10-23 stsp yylval.v.string = strdup(buf);
550 13b2bc37 2022-10-23 stsp if (yylval.v.string == NULL)
551 13b2bc37 2022-10-23 stsp err(1, "yylex: strdup");
552 13b2bc37 2022-10-23 stsp return (STRING);
555 13b2bc37 2022-10-23 stsp #define allowed_to_end_number(x) \
556 13b2bc37 2022-10-23 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
558 13b2bc37 2022-10-23 stsp if (c == '-' || isdigit(c)) {
561 13b2bc37 2022-10-23 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
562 13b2bc37 2022-10-23 stsp yyerror("string too long");
563 13b2bc37 2022-10-23 stsp return (findeol());
565 13b2bc37 2022-10-23 stsp c = lgetc(0);
566 13b2bc37 2022-10-23 stsp } while (c != EOF && isdigit(c));
567 13b2bc37 2022-10-23 stsp lungetc(c);
568 13b2bc37 2022-10-23 stsp if (p == buf + 1 && buf[0] == '-')
569 13b2bc37 2022-10-23 stsp goto nodigits;
570 13b2bc37 2022-10-23 stsp if (c == EOF || allowed_to_end_number(c)) {
571 13b2bc37 2022-10-23 stsp const char *errstr = NULL;
574 13b2bc37 2022-10-23 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
575 13b2bc37 2022-10-23 stsp LLONG_MAX, &errstr);
576 13b2bc37 2022-10-23 stsp if (errstr) {
577 13b2bc37 2022-10-23 stsp yyerror("\"%s\" invalid number: %s",
578 13b2bc37 2022-10-23 stsp buf, errstr);
579 13b2bc37 2022-10-23 stsp return (findeol());
581 13b2bc37 2022-10-23 stsp return (NUMBER);
584 13b2bc37 2022-10-23 stsp while (p > buf + 1)
585 13b2bc37 2022-10-23 stsp lungetc(*--p);
587 13b2bc37 2022-10-23 stsp if (c == '-')
588 13b2bc37 2022-10-23 stsp return (c);
592 13b2bc37 2022-10-23 stsp #define allowed_in_string(x) \
593 13b2bc37 2022-10-23 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
594 13b2bc37 2022-10-23 stsp x != '{' && x != '}' && \
595 13b2bc37 2022-10-23 stsp x != '!' && x != '=' && x != '#' && \
598 13b2bc37 2022-10-23 stsp if (isalnum(c) || c == ':' || c == '_') {
601 13b2bc37 2022-10-23 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
602 13b2bc37 2022-10-23 stsp yyerror("string too long");
603 13b2bc37 2022-10-23 stsp return (findeol());
605 13b2bc37 2022-10-23 stsp c = lgetc(0);
606 13b2bc37 2022-10-23 stsp } while (c != EOF && (allowed_in_string(c)));
607 13b2bc37 2022-10-23 stsp lungetc(c);
609 13b2bc37 2022-10-23 stsp token = lookup(buf);
610 13b2bc37 2022-10-23 stsp if (token == STRING) {
611 13b2bc37 2022-10-23 stsp yylval.v.string = strdup(buf);
612 13b2bc37 2022-10-23 stsp if (yylval.v.string == NULL)
613 13b2bc37 2022-10-23 stsp err(1, "yylex: strdup");
615 13b2bc37 2022-10-23 stsp return (token);
617 13b2bc37 2022-10-23 stsp if (c == '\n') {
618 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
619 13b2bc37 2022-10-23 stsp file->lineno++;
621 13b2bc37 2022-10-23 stsp if (c == EOF)
622 13b2bc37 2022-10-23 stsp return (0);
623 13b2bc37 2022-10-23 stsp return (c);
627 13b2bc37 2022-10-23 stsp check_file_secrecy(int fd, const char *fname)
629 13b2bc37 2022-10-23 stsp struct stat st;
631 13b2bc37 2022-10-23 stsp if (fstat(fd, &st)) {
632 13b2bc37 2022-10-23 stsp log_warn("cannot stat %s", fname);
633 13b2bc37 2022-10-23 stsp return (-1);
635 13b2bc37 2022-10-23 stsp if (st.st_uid != 0 && st.st_uid != getuid()) {
636 13b2bc37 2022-10-23 stsp log_warnx("%s: owner not root or current user", fname);
637 13b2bc37 2022-10-23 stsp return (-1);
639 13b2bc37 2022-10-23 stsp if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
640 13b2bc37 2022-10-23 stsp log_warnx("%s: group writable or world read/writable", fname);
641 13b2bc37 2022-10-23 stsp return (-1);
643 13b2bc37 2022-10-23 stsp return (0);
646 13b2bc37 2022-10-23 stsp struct file *
647 13b2bc37 2022-10-23 stsp newfile(const char *name, int secret)
649 13b2bc37 2022-10-23 stsp struct file *nfile;
651 13b2bc37 2022-10-23 stsp nfile = calloc(1, sizeof(struct file));
652 13b2bc37 2022-10-23 stsp if (nfile == NULL) {
653 13b2bc37 2022-10-23 stsp log_warn("calloc");
654 13b2bc37 2022-10-23 stsp return (NULL);
656 13b2bc37 2022-10-23 stsp nfile->name = strdup(name);
657 13b2bc37 2022-10-23 stsp if (nfile->name == NULL) {
658 13b2bc37 2022-10-23 stsp log_warn("strdup");
659 13b2bc37 2022-10-23 stsp free(nfile);
660 13b2bc37 2022-10-23 stsp return (NULL);
662 13b2bc37 2022-10-23 stsp nfile->stream = fopen(nfile->name, "r");
663 13b2bc37 2022-10-23 stsp if (nfile->stream == NULL) {
664 13b2bc37 2022-10-23 stsp /* no warning, we don't require a conf file */
665 13b2bc37 2022-10-23 stsp free(nfile->name);
666 13b2bc37 2022-10-23 stsp free(nfile);
667 13b2bc37 2022-10-23 stsp return (NULL);
668 13b2bc37 2022-10-23 stsp } else if (secret &&
669 13b2bc37 2022-10-23 stsp check_file_secrecy(fileno(nfile->stream), nfile->name)) {
670 13b2bc37 2022-10-23 stsp fclose(nfile->stream);
671 13b2bc37 2022-10-23 stsp free(nfile->name);
672 13b2bc37 2022-10-23 stsp free(nfile);
673 13b2bc37 2022-10-23 stsp return (NULL);
675 13b2bc37 2022-10-23 stsp nfile->lineno = 1;
676 13b2bc37 2022-10-23 stsp return (nfile);
679 13b2bc37 2022-10-23 stsp static void
680 13b2bc37 2022-10-23 stsp closefile(struct file *xfile)
682 13b2bc37 2022-10-23 stsp fclose(xfile->stream);
683 13b2bc37 2022-10-23 stsp free(xfile->name);
684 13b2bc37 2022-10-23 stsp free(xfile);
688 13b2bc37 2022-10-23 stsp parse_config(const char *filename, enum gotd_procid proc_id,
689 13b2bc37 2022-10-23 stsp struct gotd *env)
691 13b2bc37 2022-10-23 stsp struct sym *sym, *next;
692 3b706203 2023-01-02 stsp struct gotd_repo *repo;
694 13b2bc37 2022-10-23 stsp memset(env, 0, sizeof(*env));
696 13b2bc37 2022-10-23 stsp gotd = env;
697 13b2bc37 2022-10-23 stsp gotd_proc_id = proc_id;
698 13b2bc37 2022-10-23 stsp TAILQ_INIT(&gotd->repos);
700 13b2bc37 2022-10-23 stsp /* Apply default values. */
701 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
702 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
703 13b2bc37 2022-10-23 stsp fprintf(stderr, "%s: unix socket path too long", __func__);
706 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->user_name, GOTD_USER,
707 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
708 13b2bc37 2022-10-23 stsp fprintf(stderr, "%s: user name too long", __func__);
712 40b85cca 2023-01-03 stsp gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
713 40b85cca 2023-01-03 stsp gotd->request_timeout.tv_usec = 0;
715 13b2bc37 2022-10-23 stsp file = newfile(filename, 0);
716 13b2bc37 2022-10-23 stsp if (file == NULL) {
717 13b2bc37 2022-10-23 stsp /* just return, as we don't require a conf file */
718 13b2bc37 2022-10-23 stsp return (0);
722 13b2bc37 2022-10-23 stsp errors = file->errors;
723 13b2bc37 2022-10-23 stsp closefile(file);
725 13b2bc37 2022-10-23 stsp /* Free macros and check which have not been used. */
726 13b2bc37 2022-10-23 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
727 13b2bc37 2022-10-23 stsp if ((gotd->verbosity > 1) && !sym->used)
728 13b2bc37 2022-10-23 stsp fprintf(stderr, "warning: macro '%s' not used\n",
730 13b2bc37 2022-10-23 stsp if (!sym->persist) {
731 13b2bc37 2022-10-23 stsp free(sym->nam);
732 13b2bc37 2022-10-23 stsp free(sym->val);
733 13b2bc37 2022-10-23 stsp TAILQ_REMOVE(&symhead, sym, entry);
738 13b2bc37 2022-10-23 stsp if (errors)
739 13b2bc37 2022-10-23 stsp return (-1);
741 3b706203 2023-01-02 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
742 3b706203 2023-01-02 stsp if (repo->path[0] == '\0') {
743 2507ffb7 2023-01-02 stsp log_warnx("repository \"%s\": no path provided in "
744 2507ffb7 2023-01-02 stsp "configuration file", repo->name);
745 3b706203 2023-01-02 stsp return (-1);
749 13b2bc37 2022-10-23 stsp return (0);
753 40b85cca 2023-01-03 stsp uid_connection_limit_cmp(const void *pa, const void *pb)
755 40b85cca 2023-01-03 stsp const struct gotd_uid_connection_limit *a = pa, *b = pb;
757 40b85cca 2023-01-03 stsp if (a->uid < b->uid)
759 40b85cca 2023-01-03 stsp else if (a->uid > b->uid);
766 40b85cca 2023-01-03 stsp conf_limit_user_connections(const char *user, int maximum)
769 40b85cca 2023-01-03 stsp struct gotd_uid_connection_limit *limit;
770 40b85cca 2023-01-03 stsp size_t nlimits;
772 40b85cca 2023-01-03 stsp if (maximum < 1) {
773 40b85cca 2023-01-03 stsp yyerror("max connections cannot be smaller 1");
776 40b85cca 2023-01-03 stsp if (maximum > GOTD_MAXCLIENTS) {
777 40b85cca 2023-01-03 stsp yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
781 40b85cca 2023-01-03 stsp if (gotd_auth_parseuid(user, &uid) == -1) {
782 40b85cca 2023-01-03 stsp yyerror("%s: no such user", user);
786 40b85cca 2023-01-03 stsp limit = gotd_find_uid_connection_limit(gotd->connection_limits,
787 40b85cca 2023-01-03 stsp gotd->nconnection_limits, uid);
788 40b85cca 2023-01-03 stsp if (limit) {
789 40b85cca 2023-01-03 stsp limit->max_connections = maximum;
793 40b85cca 2023-01-03 stsp limit = gotd->connection_limits;
794 40b85cca 2023-01-03 stsp nlimits = gotd->nconnection_limits + 1;
795 40b85cca 2023-01-03 stsp limit = reallocarray(limit, nlimits, sizeof(*limit));
796 40b85cca 2023-01-03 stsp if (limit == NULL)
797 40b85cca 2023-01-03 stsp fatal("reallocarray");
799 40b85cca 2023-01-03 stsp limit[nlimits - 1].uid = uid;
800 40b85cca 2023-01-03 stsp limit[nlimits - 1].max_connections = maximum;
802 40b85cca 2023-01-03 stsp gotd->connection_limits = limit;
803 40b85cca 2023-01-03 stsp gotd->nconnection_limits = nlimits;
804 40b85cca 2023-01-03 stsp qsort(gotd->connection_limits, gotd->nconnection_limits,
805 40b85cca 2023-01-03 stsp sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
810 13b2bc37 2022-10-23 stsp static struct gotd_repo *
811 13b2bc37 2022-10-23 stsp conf_new_repo(const char *name)
813 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
815 7683f79a 2023-01-02 stsp if (name[0] == '\0') {
816 7683f79a 2023-01-02 stsp fatalx("syntax error: empty repository name found in %s",
817 7683f79a 2023-01-02 stsp file->name);
820 2507ffb7 2023-01-02 stsp if (strchr(name, '\n') != NULL)
821 2507ffb7 2023-01-02 stsp fatalx("repository names must not contain linefeeds: %s", name);
823 13b2bc37 2022-10-23 stsp repo = calloc(1, sizeof(*repo));
824 13b2bc37 2022-10-23 stsp if (repo == NULL)
825 13b2bc37 2022-10-23 stsp fatalx("%s: calloc", __func__);
827 0ccf3acb 2022-11-16 stsp STAILQ_INIT(&repo->rules);
829 13b2bc37 2022-10-23 stsp if (strlcpy(repo->name, name, sizeof(repo->name)) >=
830 13b2bc37 2022-10-23 stsp sizeof(repo->name))
831 13b2bc37 2022-10-23 stsp fatalx("%s: strlcpy", __func__);
833 13b2bc37 2022-10-23 stsp TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
834 13b2bc37 2022-10-23 stsp gotd->nrepos++;
836 13b2bc37 2022-10-23 stsp return repo;
839 0ccf3acb 2022-11-16 stsp static void
840 0ccf3acb 2022-11-16 stsp conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
841 0ccf3acb 2022-11-16 stsp int authorization, char *identifier)
843 0ccf3acb 2022-11-16 stsp struct gotd_access_rule *rule;
845 0ccf3acb 2022-11-16 stsp rule = calloc(1, sizeof(*rule));
846 0ccf3acb 2022-11-16 stsp if (rule == NULL)
847 0ccf3acb 2022-11-16 stsp fatal("calloc");
849 0ccf3acb 2022-11-16 stsp rule->access = access;
850 0ccf3acb 2022-11-16 stsp rule->authorization = authorization;
851 0ccf3acb 2022-11-16 stsp rule->identifier = identifier;
853 0ccf3acb 2022-11-16 stsp STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
857 13b2bc37 2022-10-23 stsp symset(const char *nam, const char *val, int persist)
859 13b2bc37 2022-10-23 stsp struct sym *sym;
861 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(sym, &symhead, entry) {
862 13b2bc37 2022-10-23 stsp if (strcmp(nam, sym->nam) == 0)
866 13b2bc37 2022-10-23 stsp if (sym != NULL) {
867 13b2bc37 2022-10-23 stsp if (sym->persist == 1)
868 13b2bc37 2022-10-23 stsp return (0);
870 13b2bc37 2022-10-23 stsp free(sym->nam);
871 13b2bc37 2022-10-23 stsp free(sym->val);
872 13b2bc37 2022-10-23 stsp TAILQ_REMOVE(&symhead, sym, entry);
876 13b2bc37 2022-10-23 stsp sym = calloc(1, sizeof(*sym));
877 13b2bc37 2022-10-23 stsp if (sym == NULL)
878 13b2bc37 2022-10-23 stsp return (-1);
880 13b2bc37 2022-10-23 stsp sym->nam = strdup(nam);
881 13b2bc37 2022-10-23 stsp if (sym->nam == NULL) {
883 13b2bc37 2022-10-23 stsp return (-1);
885 13b2bc37 2022-10-23 stsp sym->val = strdup(val);
886 13b2bc37 2022-10-23 stsp if (sym->val == NULL) {
887 13b2bc37 2022-10-23 stsp free(sym->nam);
889 13b2bc37 2022-10-23 stsp return (-1);
891 13b2bc37 2022-10-23 stsp sym->used = 0;
892 13b2bc37 2022-10-23 stsp sym->persist = persist;
893 13b2bc37 2022-10-23 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
894 13b2bc37 2022-10-23 stsp return (0);
898 13b2bc37 2022-10-23 stsp symget(const char *nam)
900 13b2bc37 2022-10-23 stsp struct sym *sym;
902 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(sym, &symhead, entry) {
903 13b2bc37 2022-10-23 stsp if (strcmp(nam, sym->nam) == 0) {
904 13b2bc37 2022-10-23 stsp sym->used = 1;
905 13b2bc37 2022-10-23 stsp return (sym->val);
908 13b2bc37 2022-10-23 stsp return (NULL);