Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
4 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 *
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
24 %{
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <sys/queue.h>
28 #include <sys/stat.h>
30 #include <ctype.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <event.h>
34 #include <imsg.h>
35 #include <limits.h>
36 #include <sha1.h>
37 #include <stdarg.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include <unistd.h>
44 #include "got_error.h"
45 #include "got_path.h"
47 #include "log.h"
48 #include "gotd.h"
50 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
51 static struct file {
52 TAILQ_ENTRY(file) entry;
53 FILE *stream;
54 char *name;
55 int lineno;
56 int errors;
57 } *file;
58 struct file *newfile(const char *, int);
59 static void closefile(struct file *);
60 int check_file_secrecy(int, const char *);
61 int yyparse(void);
62 int yylex(void);
63 int yyerror(const char *, ...)
64 __attribute__((__format__ (printf, 1, 2)))
65 __attribute__((__nonnull__ (1)));
66 int kw_cmp(const void *, const void *);
67 int lookup(char *);
68 int lgetc(int);
69 int lungetc(int);
70 int findeol(void);
72 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
73 struct sym {
74 TAILQ_ENTRY(sym) entry;
75 int used;
76 int persist;
77 char *nam;
78 char *val;
79 };
81 int symset(const char *, const char *, int);
82 char *symget(const char *);
84 static int errors;
86 static struct gotd *gotd;
87 static struct gotd_repo *new_repo;
88 static struct gotd_repo *conf_new_repo(const char *);
89 static void conf_new_access_rule(struct gotd_repo *,
90 enum gotd_access, int, char *);
91 static enum gotd_procid gotd_proc_id;
93 typedef struct {
94 union {
95 long long number;
96 char *string;
97 } v;
98 int lineno;
99 } YYSTYPE;
101 %}
103 %token PATH ERROR ON UNIX_SOCKET UNIX_GROUP USER REPOSITORY PERMIT DENY
104 %token RO RW
106 %token <v.string> STRING
107 %token <v.number> NUMBER
108 %type <v.number> boolean
110 %%
112 grammar :
113 | grammar '\n'
114 | grammar main '\n'
115 | grammar repository '\n'
118 boolean : STRING {
119 if (strcasecmp($1, "1") == 0 ||
120 strcasecmp($1, "yes") == 0 ||
121 strcasecmp($1, "on") == 0)
122 $$ = 1;
123 else if (strcasecmp($1, "0") == 0 ||
124 strcasecmp($1, "off") == 0 ||
125 strcasecmp($1, "no") == 0)
126 $$ = 0;
127 else {
128 yyerror("invalid boolean value '%s'", $1);
129 free($1);
130 YYERROR;
132 free($1);
134 | ON { $$ = 1; }
135 | NUMBER { $$ = $1; }
138 main : UNIX_SOCKET STRING {
139 if (gotd_proc_id == PROC_LISTEN) {
140 if (strlcpy(gotd->unix_socket_path, $2,
141 sizeof(gotd->unix_socket_path)) >=
142 sizeof(gotd->unix_socket_path)) {
143 yyerror("%s: unix socket path too long",
144 __func__);
145 free($2);
146 YYERROR;
149 free($2);
151 | UNIX_GROUP STRING {
152 if (strlcpy(gotd->unix_group_name, $2,
153 sizeof(gotd->unix_group_name)) >=
154 sizeof(gotd->unix_group_name)) {
155 yyerror("%s: unix group name too long",
156 __func__);
157 free($2);
158 YYERROR;
160 free($2);
162 | USER STRING {
163 if (strlcpy(gotd->user_name, $2,
164 sizeof(gotd->user_name)) >=
165 sizeof(gotd->user_name)) {
166 yyerror("%s: user name too long", __func__);
167 free($2);
168 YYERROR;
170 free($2);
174 repository : REPOSITORY STRING {
175 struct gotd_repo *repo;
177 TAILQ_FOREACH(repo, &gotd->repos, entry) {
178 if (strcmp(repo->name, $2) == 0) {
179 yyerror("duplicate repository '%s'", $2);
180 free($2);
181 YYERROR;
185 if (gotd_proc_id == PROC_GOTD ||
186 gotd_proc_id == PROC_AUTH) {
187 new_repo = conf_new_repo($2);
189 free($2);
191 | REPOSITORY STRING {
192 struct gotd_repo *repo;
194 TAILQ_FOREACH(repo, &gotd->repos, entry) {
195 if (strcmp(repo->name, $2) == 0) {
196 yyerror("duplicate repository '%s'", $2);
197 free($2);
198 YYERROR;
202 if (gotd_proc_id == PROC_GOTD ||
203 gotd_proc_id == PROC_AUTH) {
204 new_repo = conf_new_repo($2);
206 free($2);
207 } '{' optnl repoopts2 '}' {
211 repoopts1 : PATH STRING {
212 if (gotd_proc_id == PROC_GOTD ||
213 gotd_proc_id == PROC_AUTH) {
214 if (!got_path_is_absolute($2)) {
215 yyerror("%s: path %s is not absolute",
216 __func__, $2);
217 free($2);
218 YYERROR;
220 if (strlcpy(new_repo->path, $2,
221 sizeof(new_repo->path)) >=
222 sizeof(new_repo->path)) {
223 yyerror("%s: path truncated", __func__);
224 free($2);
225 YYERROR;
228 free($2);
230 | PERMIT RO STRING {
231 if (gotd_proc_id == PROC_AUTH) {
232 conf_new_access_rule(new_repo,
233 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
236 | PERMIT RW STRING {
237 if (gotd_proc_id == PROC_AUTH) {
238 conf_new_access_rule(new_repo,
239 GOTD_ACCESS_PERMITTED,
240 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
243 | DENY STRING {
244 if (gotd_proc_id == PROC_AUTH) {
245 conf_new_access_rule(new_repo,
246 GOTD_ACCESS_DENIED, 0, $2);
251 repoopts2 : repoopts2 repoopts1 nl
252 | repoopts1 optnl
255 nl : '\n' optnl
258 optnl : '\n' optnl /* zero or more newlines */
259 | /* empty */
262 %%
264 struct keywords {
265 const char *k_name;
266 int k_val;
267 };
269 int
270 yyerror(const char *fmt, ...)
272 va_list ap;
273 char *msg;
275 file->errors++;
276 va_start(ap, fmt);
277 if (vasprintf(&msg, fmt, ap) == -1)
278 fatalx("yyerror vasprintf");
279 va_end(ap);
280 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
281 free(msg);
282 return (0);
285 int
286 kw_cmp(const void *k, const void *e)
288 return (strcmp(k, ((const struct keywords *)e)->k_name));
291 int
292 lookup(char *s)
294 /* This has to be sorted always. */
295 static const struct keywords keywords[] = {
296 { "deny", DENY },
297 { "on", ON },
298 { "path", PATH },
299 { "permit", PERMIT },
300 { "repository", REPOSITORY },
301 { "ro", RO },
302 { "rw", RW },
303 { "unix_group", UNIX_GROUP },
304 { "unix_socket", UNIX_SOCKET },
305 { "user", USER },
306 };
307 const struct keywords *p;
309 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
310 sizeof(keywords[0]), kw_cmp);
312 if (p)
313 return (p->k_val);
314 else
315 return (STRING);
318 #define MAXPUSHBACK 128
320 unsigned char *parsebuf;
321 int parseindex;
322 unsigned char pushback_buffer[MAXPUSHBACK];
323 int pushback_index = 0;
325 int
326 lgetc(int quotec)
328 int c, next;
330 if (parsebuf) {
331 /* Read character from the parsebuffer instead of input. */
332 if (parseindex >= 0) {
333 c = parsebuf[parseindex++];
334 if (c != '\0')
335 return (c);
336 parsebuf = NULL;
337 } else
338 parseindex++;
341 if (pushback_index)
342 return (pushback_buffer[--pushback_index]);
344 if (quotec) {
345 c = getc(file->stream);
346 if (c == EOF)
347 yyerror("reached end of file while parsing "
348 "quoted string");
349 return (c);
352 c = getc(file->stream);
353 while (c == '\\') {
354 next = getc(file->stream);
355 if (next != '\n') {
356 c = next;
357 break;
359 yylval.lineno = file->lineno;
360 file->lineno++;
361 c = getc(file->stream);
364 return (c);
367 int
368 lungetc(int c)
370 if (c == EOF)
371 return (EOF);
372 if (parsebuf) {
373 parseindex--;
374 if (parseindex >= 0)
375 return (c);
377 if (pushback_index < MAXPUSHBACK-1)
378 return (pushback_buffer[pushback_index++] = c);
379 else
380 return (EOF);
383 int
384 findeol(void)
386 int c;
388 parsebuf = NULL;
390 /* Skip to either EOF or the first real EOL. */
391 while (1) {
392 if (pushback_index)
393 c = pushback_buffer[--pushback_index];
394 else
395 c = lgetc(0);
396 if (c == '\n') {
397 file->lineno++;
398 break;
400 if (c == EOF)
401 break;
403 return (ERROR);
406 int
407 yylex(void)
409 unsigned char buf[8096];
410 unsigned char *p, *val;
411 int quotec, next, c;
412 int token;
414 top:
415 p = buf;
416 c = lgetc(0);
417 while (c == ' ' || c == '\t')
418 c = lgetc(0); /* nothing */
420 yylval.lineno = file->lineno;
421 if (c == '#') {
422 c = lgetc(0);
423 while (c != '\n' && c != EOF)
424 c = lgetc(0); /* nothing */
426 if (c == '$' && parsebuf == NULL) {
427 while (1) {
428 c = lgetc(0);
429 if (c == EOF)
430 return (0);
432 if (p + 1 >= buf + sizeof(buf) - 1) {
433 yyerror("string too long");
434 return (findeol());
436 if (isalnum(c) || c == '_') {
437 *p++ = c;
438 continue;
440 *p = '\0';
441 lungetc(c);
442 break;
444 val = symget(buf);
445 if (val == NULL) {
446 yyerror("macro '%s' not defined", buf);
447 return (findeol());
449 parsebuf = val;
450 parseindex = 0;
451 goto top;
454 switch (c) {
455 case '\'':
456 case '"':
457 quotec = c;
458 while (1) {
459 c = lgetc(quotec);
460 if (c == EOF)
461 return (0);
462 if (c == '\n') {
463 file->lineno++;
464 continue;
465 } else if (c == '\\') {
466 next = lgetc(quotec);
467 if (next == EOF)
468 return (0);
469 if (next == quotec || c == ' ' || c == '\t')
470 c = next;
471 else if (next == '\n') {
472 file->lineno++;
473 continue;
474 } else
475 lungetc(next);
476 } else if (c == quotec) {
477 *p = '\0';
478 break;
479 } else if (c == '\0') {
480 yyerror("syntax error");
481 return (findeol());
483 if (p + 1 >= buf + sizeof(buf) - 1) {
484 yyerror("string too long");
485 return (findeol());
487 *p++ = c;
489 yylval.v.string = strdup(buf);
490 if (yylval.v.string == NULL)
491 err(1, "yylex: strdup");
492 return (STRING);
495 #define allowed_to_end_number(x) \
496 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
498 if (c == '-' || isdigit(c)) {
499 do {
500 *p++ = c;
501 if ((unsigned)(p-buf) >= sizeof(buf)) {
502 yyerror("string too long");
503 return (findeol());
505 c = lgetc(0);
506 } while (c != EOF && isdigit(c));
507 lungetc(c);
508 if (p == buf + 1 && buf[0] == '-')
509 goto nodigits;
510 if (c == EOF || allowed_to_end_number(c)) {
511 const char *errstr = NULL;
513 *p = '\0';
514 yylval.v.number = strtonum(buf, LLONG_MIN,
515 LLONG_MAX, &errstr);
516 if (errstr) {
517 yyerror("\"%s\" invalid number: %s",
518 buf, errstr);
519 return (findeol());
521 return (NUMBER);
522 } else {
523 nodigits:
524 while (p > buf + 1)
525 lungetc(*--p);
526 c = *--p;
527 if (c == '-')
528 return (c);
532 #define allowed_in_string(x) \
533 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
534 x != '{' && x != '}' && \
535 x != '!' && x != '=' && x != '#' && \
536 x != ','))
538 if (isalnum(c) || c == ':' || c == '_') {
539 do {
540 *p++ = c;
541 if ((unsigned)(p-buf) >= sizeof(buf)) {
542 yyerror("string too long");
543 return (findeol());
545 c = lgetc(0);
546 } while (c != EOF && (allowed_in_string(c)));
547 lungetc(c);
548 *p = '\0';
549 token = lookup(buf);
550 if (token == STRING) {
551 yylval.v.string = strdup(buf);
552 if (yylval.v.string == NULL)
553 err(1, "yylex: strdup");
555 return (token);
557 if (c == '\n') {
558 yylval.lineno = file->lineno;
559 file->lineno++;
561 if (c == EOF)
562 return (0);
563 return (c);
566 int
567 check_file_secrecy(int fd, const char *fname)
569 struct stat st;
571 if (fstat(fd, &st)) {
572 log_warn("cannot stat %s", fname);
573 return (-1);
575 if (st.st_uid != 0 && st.st_uid != getuid()) {
576 log_warnx("%s: owner not root or current user", fname);
577 return (-1);
579 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
580 log_warnx("%s: group writable or world read/writable", fname);
581 return (-1);
583 return (0);
586 struct file *
587 newfile(const char *name, int secret)
589 struct file *nfile;
591 nfile = calloc(1, sizeof(struct file));
592 if (nfile == NULL) {
593 log_warn("calloc");
594 return (NULL);
596 nfile->name = strdup(name);
597 if (nfile->name == NULL) {
598 log_warn("strdup");
599 free(nfile);
600 return (NULL);
602 nfile->stream = fopen(nfile->name, "r");
603 if (nfile->stream == NULL) {
604 /* no warning, we don't require a conf file */
605 free(nfile->name);
606 free(nfile);
607 return (NULL);
608 } else if (secret &&
609 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
610 fclose(nfile->stream);
611 free(nfile->name);
612 free(nfile);
613 return (NULL);
615 nfile->lineno = 1;
616 return (nfile);
619 static void
620 closefile(struct file *xfile)
622 fclose(xfile->stream);
623 free(xfile->name);
624 free(xfile);
627 int
628 parse_config(const char *filename, enum gotd_procid proc_id,
629 struct gotd *env)
631 struct sym *sym, *next;
633 memset(env, 0, sizeof(*env));
635 gotd = env;
636 gotd_proc_id = proc_id;
637 TAILQ_INIT(&gotd->repos);
639 /* Apply default values. */
640 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
641 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
642 fprintf(stderr, "%s: unix socket path too long", __func__);
643 return -1;
645 if (strlcpy(gotd->unix_group_name, GOTD_UNIX_GROUP,
646 sizeof(gotd->unix_group_name)) >= sizeof(gotd->unix_group_name)) {
647 fprintf(stderr, "%s: unix group name too long", __func__);
648 return -1;
650 if (strlcpy(gotd->user_name, GOTD_USER,
651 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
652 fprintf(stderr, "%s: user name too long", __func__);
653 return -1;
656 file = newfile(filename, 0);
657 if (file == NULL) {
658 /* just return, as we don't require a conf file */
659 return (0);
662 yyparse();
663 errors = file->errors;
664 closefile(file);
666 /* Free macros and check which have not been used. */
667 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
668 if ((gotd->verbosity > 1) && !sym->used)
669 fprintf(stderr, "warning: macro '%s' not used\n",
670 sym->nam);
671 if (!sym->persist) {
672 free(sym->nam);
673 free(sym->val);
674 TAILQ_REMOVE(&symhead, sym, entry);
675 free(sym);
679 if (errors)
680 return (-1);
682 return (0);
685 static struct gotd_repo *
686 conf_new_repo(const char *name)
688 struct gotd_repo *repo;
690 if (strchr(name, '\n') != NULL) {
691 fatalx("%s: repository names must not contain linefeeds: %s",
692 getprogname(), name);
695 repo = calloc(1, sizeof(*repo));
696 if (repo == NULL)
697 fatalx("%s: calloc", __func__);
699 STAILQ_INIT(&repo->rules);
701 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
702 sizeof(repo->name))
703 fatalx("%s: strlcpy", __func__);
705 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
706 gotd->nrepos++;
708 return repo;
709 };
711 static void
712 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
713 int authorization, char *identifier)
715 struct gotd_access_rule *rule;
717 rule = calloc(1, sizeof(*rule));
718 if (rule == NULL)
719 fatal("calloc");
721 rule->access = access;
722 rule->authorization = authorization;
723 rule->identifier = identifier;
725 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
728 int
729 symset(const char *nam, const char *val, int persist)
731 struct sym *sym;
733 TAILQ_FOREACH(sym, &symhead, entry) {
734 if (strcmp(nam, sym->nam) == 0)
735 break;
738 if (sym != NULL) {
739 if (sym->persist == 1)
740 return (0);
741 else {
742 free(sym->nam);
743 free(sym->val);
744 TAILQ_REMOVE(&symhead, sym, entry);
745 free(sym);
748 sym = calloc(1, sizeof(*sym));
749 if (sym == NULL)
750 return (-1);
752 sym->nam = strdup(nam);
753 if (sym->nam == NULL) {
754 free(sym);
755 return (-1);
757 sym->val = strdup(val);
758 if (sym->val == NULL) {
759 free(sym->nam);
760 free(sym);
761 return (-1);
763 sym->used = 0;
764 sym->persist = persist;
765 TAILQ_INSERT_TAIL(&symhead, sym, entry);
766 return (0);
769 char *
770 symget(const char *nam)
772 struct sym *sym;
774 TAILQ_FOREACH(sym, &symhead, entry) {
775 if (strcmp(nam, sym->nam) == 0) {
776 sym->used = 1;
777 return (sym->val);
780 return (NULL);