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);
190 } '{' optnl repoopts2 '}' {
194 repoopts1 : PATH STRING {
195 if (gotd_proc_id == PROC_GOTD ||
196 gotd_proc_id == PROC_AUTH) {
197 if (!got_path_is_absolute($2)) {
198 yyerror("%s: path %s is not absolute",
199 __func__, $2);
200 free($2);
201 YYERROR;
203 if (strlcpy(new_repo->path, $2,
204 sizeof(new_repo->path)) >=
205 sizeof(new_repo->path)) {
206 yyerror("%s: path truncated", __func__);
207 free($2);
208 YYERROR;
211 free($2);
213 | PERMIT RO STRING {
214 if (gotd_proc_id == PROC_AUTH) {
215 conf_new_access_rule(new_repo,
216 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
219 | PERMIT RW STRING {
220 if (gotd_proc_id == PROC_AUTH) {
221 conf_new_access_rule(new_repo,
222 GOTD_ACCESS_PERMITTED,
223 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
226 | DENY STRING {
227 if (gotd_proc_id == PROC_AUTH) {
228 conf_new_access_rule(new_repo,
229 GOTD_ACCESS_DENIED, 0, $2);
234 repoopts2 : repoopts2 repoopts1 nl
235 | repoopts1 optnl
238 nl : '\n' optnl
241 optnl : '\n' optnl /* zero or more newlines */
242 | /* empty */
245 %%
247 struct keywords {
248 const char *k_name;
249 int k_val;
250 };
252 int
253 yyerror(const char *fmt, ...)
255 va_list ap;
256 char *msg;
258 file->errors++;
259 va_start(ap, fmt);
260 if (vasprintf(&msg, fmt, ap) == -1)
261 fatalx("yyerror vasprintf");
262 va_end(ap);
263 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
264 free(msg);
265 return (0);
268 int
269 kw_cmp(const void *k, const void *e)
271 return (strcmp(k, ((const struct keywords *)e)->k_name));
274 int
275 lookup(char *s)
277 /* This has to be sorted always. */
278 static const struct keywords keywords[] = {
279 { "deny", DENY },
280 { "on", ON },
281 { "path", PATH },
282 { "permit", PERMIT },
283 { "repository", REPOSITORY },
284 { "ro", RO },
285 { "rw", RW },
286 { "unix_group", UNIX_GROUP },
287 { "unix_socket", UNIX_SOCKET },
288 { "user", USER },
289 };
290 const struct keywords *p;
292 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
293 sizeof(keywords[0]), kw_cmp);
295 if (p)
296 return (p->k_val);
297 else
298 return (STRING);
301 #define MAXPUSHBACK 128
303 unsigned char *parsebuf;
304 int parseindex;
305 unsigned char pushback_buffer[MAXPUSHBACK];
306 int pushback_index = 0;
308 int
309 lgetc(int quotec)
311 int c, next;
313 if (parsebuf) {
314 /* Read character from the parsebuffer instead of input. */
315 if (parseindex >= 0) {
316 c = parsebuf[parseindex++];
317 if (c != '\0')
318 return (c);
319 parsebuf = NULL;
320 } else
321 parseindex++;
324 if (pushback_index)
325 return (pushback_buffer[--pushback_index]);
327 if (quotec) {
328 c = getc(file->stream);
329 if (c == EOF)
330 yyerror("reached end of file while parsing "
331 "quoted string");
332 return (c);
335 c = getc(file->stream);
336 while (c == '\\') {
337 next = getc(file->stream);
338 if (next != '\n') {
339 c = next;
340 break;
342 yylval.lineno = file->lineno;
343 file->lineno++;
344 c = getc(file->stream);
347 return (c);
350 int
351 lungetc(int c)
353 if (c == EOF)
354 return (EOF);
355 if (parsebuf) {
356 parseindex--;
357 if (parseindex >= 0)
358 return (c);
360 if (pushback_index < MAXPUSHBACK-1)
361 return (pushback_buffer[pushback_index++] = c);
362 else
363 return (EOF);
366 int
367 findeol(void)
369 int c;
371 parsebuf = NULL;
373 /* Skip to either EOF or the first real EOL. */
374 while (1) {
375 if (pushback_index)
376 c = pushback_buffer[--pushback_index];
377 else
378 c = lgetc(0);
379 if (c == '\n') {
380 file->lineno++;
381 break;
383 if (c == EOF)
384 break;
386 return (ERROR);
389 int
390 yylex(void)
392 unsigned char buf[8096];
393 unsigned char *p, *val;
394 int quotec, next, c;
395 int token;
397 top:
398 p = buf;
399 c = lgetc(0);
400 while (c == ' ' || c == '\t')
401 c = lgetc(0); /* nothing */
403 yylval.lineno = file->lineno;
404 if (c == '#') {
405 c = lgetc(0);
406 while (c != '\n' && c != EOF)
407 c = lgetc(0); /* nothing */
409 if (c == '$' && parsebuf == NULL) {
410 while (1) {
411 c = lgetc(0);
412 if (c == EOF)
413 return (0);
415 if (p + 1 >= buf + sizeof(buf) - 1) {
416 yyerror("string too long");
417 return (findeol());
419 if (isalnum(c) || c == '_') {
420 *p++ = c;
421 continue;
423 *p = '\0';
424 lungetc(c);
425 break;
427 val = symget(buf);
428 if (val == NULL) {
429 yyerror("macro '%s' not defined", buf);
430 return (findeol());
432 parsebuf = val;
433 parseindex = 0;
434 goto top;
437 switch (c) {
438 case '\'':
439 case '"':
440 quotec = c;
441 while (1) {
442 c = lgetc(quotec);
443 if (c == EOF)
444 return (0);
445 if (c == '\n') {
446 file->lineno++;
447 continue;
448 } else if (c == '\\') {
449 next = lgetc(quotec);
450 if (next == EOF)
451 return (0);
452 if (next == quotec || c == ' ' || c == '\t')
453 c = next;
454 else if (next == '\n') {
455 file->lineno++;
456 continue;
457 } else
458 lungetc(next);
459 } else if (c == quotec) {
460 *p = '\0';
461 break;
462 } else if (c == '\0') {
463 yyerror("syntax error");
464 return (findeol());
466 if (p + 1 >= buf + sizeof(buf) - 1) {
467 yyerror("string too long");
468 return (findeol());
470 *p++ = c;
472 yylval.v.string = strdup(buf);
473 if (yylval.v.string == NULL)
474 err(1, "yylex: strdup");
475 return (STRING);
478 #define allowed_to_end_number(x) \
479 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
481 if (c == '-' || isdigit(c)) {
482 do {
483 *p++ = c;
484 if ((unsigned)(p-buf) >= sizeof(buf)) {
485 yyerror("string too long");
486 return (findeol());
488 c = lgetc(0);
489 } while (c != EOF && isdigit(c));
490 lungetc(c);
491 if (p == buf + 1 && buf[0] == '-')
492 goto nodigits;
493 if (c == EOF || allowed_to_end_number(c)) {
494 const char *errstr = NULL;
496 *p = '\0';
497 yylval.v.number = strtonum(buf, LLONG_MIN,
498 LLONG_MAX, &errstr);
499 if (errstr) {
500 yyerror("\"%s\" invalid number: %s",
501 buf, errstr);
502 return (findeol());
504 return (NUMBER);
505 } else {
506 nodigits:
507 while (p > buf + 1)
508 lungetc(*--p);
509 c = *--p;
510 if (c == '-')
511 return (c);
515 #define allowed_in_string(x) \
516 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
517 x != '{' && x != '}' && \
518 x != '!' && x != '=' && x != '#' && \
519 x != ','))
521 if (isalnum(c) || c == ':' || c == '_') {
522 do {
523 *p++ = c;
524 if ((unsigned)(p-buf) >= sizeof(buf)) {
525 yyerror("string too long");
526 return (findeol());
528 c = lgetc(0);
529 } while (c != EOF && (allowed_in_string(c)));
530 lungetc(c);
531 *p = '\0';
532 token = lookup(buf);
533 if (token == STRING) {
534 yylval.v.string = strdup(buf);
535 if (yylval.v.string == NULL)
536 err(1, "yylex: strdup");
538 return (token);
540 if (c == '\n') {
541 yylval.lineno = file->lineno;
542 file->lineno++;
544 if (c == EOF)
545 return (0);
546 return (c);
549 int
550 check_file_secrecy(int fd, const char *fname)
552 struct stat st;
554 if (fstat(fd, &st)) {
555 log_warn("cannot stat %s", fname);
556 return (-1);
558 if (st.st_uid != 0 && st.st_uid != getuid()) {
559 log_warnx("%s: owner not root or current user", fname);
560 return (-1);
562 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
563 log_warnx("%s: group writable or world read/writable", fname);
564 return (-1);
566 return (0);
569 struct file *
570 newfile(const char *name, int secret)
572 struct file *nfile;
574 nfile = calloc(1, sizeof(struct file));
575 if (nfile == NULL) {
576 log_warn("calloc");
577 return (NULL);
579 nfile->name = strdup(name);
580 if (nfile->name == NULL) {
581 log_warn("strdup");
582 free(nfile);
583 return (NULL);
585 nfile->stream = fopen(nfile->name, "r");
586 if (nfile->stream == NULL) {
587 /* no warning, we don't require a conf file */
588 free(nfile->name);
589 free(nfile);
590 return (NULL);
591 } else if (secret &&
592 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
593 fclose(nfile->stream);
594 free(nfile->name);
595 free(nfile);
596 return (NULL);
598 nfile->lineno = 1;
599 return (nfile);
602 static void
603 closefile(struct file *xfile)
605 fclose(xfile->stream);
606 free(xfile->name);
607 free(xfile);
610 int
611 parse_config(const char *filename, enum gotd_procid proc_id,
612 struct gotd *env)
614 struct sym *sym, *next;
615 struct gotd_repo *repo;
617 memset(env, 0, sizeof(*env));
619 gotd = env;
620 gotd_proc_id = proc_id;
621 TAILQ_INIT(&gotd->repos);
623 /* Apply default values. */
624 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
625 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
626 fprintf(stderr, "%s: unix socket path too long", __func__);
627 return -1;
629 if (strlcpy(gotd->unix_group_name, GOTD_UNIX_GROUP,
630 sizeof(gotd->unix_group_name)) >= sizeof(gotd->unix_group_name)) {
631 fprintf(stderr, "%s: unix group name too long", __func__);
632 return -1;
634 if (strlcpy(gotd->user_name, GOTD_USER,
635 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
636 fprintf(stderr, "%s: user name too long", __func__);
637 return -1;
640 file = newfile(filename, 0);
641 if (file == NULL) {
642 /* just return, as we don't require a conf file */
643 return (0);
646 yyparse();
647 errors = file->errors;
648 closefile(file);
650 /* Free macros and check which have not been used. */
651 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
652 if ((gotd->verbosity > 1) && !sym->used)
653 fprintf(stderr, "warning: macro '%s' not used\n",
654 sym->nam);
655 if (!sym->persist) {
656 free(sym->nam);
657 free(sym->val);
658 TAILQ_REMOVE(&symhead, sym, entry);
659 free(sym);
663 if (errors)
664 return (-1);
666 TAILQ_FOREACH(repo, &gotd->repos, entry) {
667 if (repo->path[0] == '\0') {
668 log_warnx("repository \"%s\": no path provided in "
669 "configuration file", repo->name);
670 return (-1);
674 return (0);
677 static struct gotd_repo *
678 conf_new_repo(const char *name)
680 struct gotd_repo *repo;
682 if (name[0] == '\0') {
683 fatalx("syntax error: empty repository name found in %s",
684 file->name);
687 if (strchr(name, '\n') != NULL)
688 fatalx("repository names must not contain linefeeds: %s", name);
690 repo = calloc(1, sizeof(*repo));
691 if (repo == NULL)
692 fatalx("%s: calloc", __func__);
694 STAILQ_INIT(&repo->rules);
696 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
697 sizeof(repo->name))
698 fatalx("%s: strlcpy", __func__);
700 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
701 gotd->nrepos++;
703 return repo;
704 };
706 static void
707 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
708 int authorization, char *identifier)
710 struct gotd_access_rule *rule;
712 rule = calloc(1, sizeof(*rule));
713 if (rule == NULL)
714 fatal("calloc");
716 rule->access = access;
717 rule->authorization = authorization;
718 rule->identifier = identifier;
720 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
723 int
724 symset(const char *nam, const char *val, int persist)
726 struct sym *sym;
728 TAILQ_FOREACH(sym, &symhead, entry) {
729 if (strcmp(nam, sym->nam) == 0)
730 break;
733 if (sym != NULL) {
734 if (sym->persist == 1)
735 return (0);
736 else {
737 free(sym->nam);
738 free(sym->val);
739 TAILQ_REMOVE(&symhead, sym, entry);
740 free(sym);
743 sym = calloc(1, sizeof(*sym));
744 if (sym == NULL)
745 return (-1);
747 sym->nam = strdup(nam);
748 if (sym->nam == NULL) {
749 free(sym);
750 return (-1);
752 sym->val = strdup(val);
753 if (sym->val == NULL) {
754 free(sym->nam);
755 free(sym);
756 return (-1);
758 sym->used = 0;
759 sym->persist = persist;
760 TAILQ_INSERT_TAIL(&symhead, sym, entry);
761 return (0);
764 char *
765 symget(const char *nam)
767 struct sym *sym;
769 TAILQ_FOREACH(sym, &symhead, entry) {
770 if (strcmp(nam, sym->nam) == 0) {
771 sym->used = 1;
772 return (sym->val);
775 return (NULL);