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 enum gotd_procid gotd_proc_id;
91 typedef struct {
92 union {
93 long long number;
94 char *string;
95 } v;
96 int lineno;
97 } YYSTYPE;
99 %}
101 %token PATH ERROR ON UNIX_SOCKET UNIX_GROUP USER REPOSITORY
103 %token <v.string> STRING
104 %token <v.number> NUMBER
105 %type <v.number> boolean
107 %%
109 grammar :
110 | grammar '\n'
111 | grammar main '\n'
112 | grammar repository '\n'
115 boolean : STRING {
116 if (strcasecmp($1, "1") == 0 ||
117 strcasecmp($1, "yes") == 0 ||
118 strcasecmp($1, "on") == 0)
119 $$ = 1;
120 else if (strcasecmp($1, "0") == 0 ||
121 strcasecmp($1, "off") == 0 ||
122 strcasecmp($1, "no") == 0)
123 $$ = 0;
124 else {
125 yyerror("invalid boolean value '%s'", $1);
126 free($1);
127 YYERROR;
129 free($1);
131 | ON { $$ = 1; }
132 | NUMBER { $$ = $1; }
135 main : UNIX_SOCKET STRING {
136 if (gotd_proc_id == PROC_GOTD) {
137 if (strlcpy(gotd->unix_socket_path, $2,
138 sizeof(gotd->unix_socket_path)) >=
139 sizeof(gotd->unix_socket_path)) {
140 yyerror("%s: unix socket path too long",
141 __func__);
142 free($2);
143 YYERROR;
146 free($2);
148 | UNIX_GROUP STRING {
149 if (strlcpy(gotd->unix_group_name, $2,
150 sizeof(gotd->unix_group_name)) >=
151 sizeof(gotd->unix_group_name)) {
152 yyerror("%s: unix group name too long",
153 __func__);
154 free($2);
155 YYERROR;
157 free($2);
159 | USER STRING {
160 if (strlcpy(gotd->user_name, $2,
161 sizeof(gotd->user_name)) >=
162 sizeof(gotd->user_name)) {
163 yyerror("%s: user name too long", __func__);
164 free($2);
165 YYERROR;
167 free($2);
171 repository : REPOSITORY STRING {
172 struct gotd_repo *repo;
174 TAILQ_FOREACH(repo, &gotd->repos, entry) {
175 if (strcmp(repo->name, $2) == 0) {
176 yyerror("duplicate repository '%s'", $2);
177 free($2);
178 YYERROR;
182 if (gotd_proc_id == PROC_GOTD) {
183 new_repo = conf_new_repo($2);
185 free($2);
187 | REPOSITORY STRING {
188 struct gotd_repo *repo;
190 TAILQ_FOREACH(repo, &gotd->repos, entry) {
191 if (strcmp(repo->name, $2) == 0) {
192 yyerror("duplicate repository '%s'", $2);
193 free($2);
194 YYERROR;
198 if (gotd_proc_id == PROC_GOTD) {
199 new_repo = conf_new_repo($2);
201 free($2);
202 } '{' optnl repoopts2 '}' {
206 repoopts1 : PATH STRING {
207 if (gotd_proc_id == PROC_GOTD) {
208 if (!got_path_is_absolute($2)) {
209 yyerror("%s: path %s is not absolute",
210 __func__, $2);
211 free($2);
212 YYERROR;
214 if (strlcpy(new_repo->path, $2,
215 sizeof(new_repo->path)) >=
216 sizeof(new_repo->path)) {
217 yyerror("%s: path truncated", __func__);
218 free($2);
219 YYERROR;
222 free($2);
226 repoopts2 : repoopts2 repoopts1 nl
227 | repoopts1 optnl
230 nl : '\n' optnl
233 optnl : '\n' optnl /* zero or more newlines */
234 | /* empty */
237 %%
239 struct keywords {
240 const char *k_name;
241 int k_val;
242 };
244 int
245 yyerror(const char *fmt, ...)
247 va_list ap;
248 char *msg;
250 file->errors++;
251 va_start(ap, fmt);
252 if (vasprintf(&msg, fmt, ap) == -1)
253 fatalx("yyerror vasprintf");
254 va_end(ap);
255 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
256 free(msg);
257 return (0);
260 int
261 kw_cmp(const void *k, const void *e)
263 return (strcmp(k, ((const struct keywords *)e)->k_name));
266 int
267 lookup(char *s)
269 /* This has to be sorted always. */
270 static const struct keywords keywords[] = {
271 { "on", ON },
272 { "path", PATH },
273 { "repository", REPOSITORY },
274 { "unix_group", UNIX_GROUP },
275 { "unix_socket", UNIX_SOCKET },
276 { "user", USER },
277 };
278 const struct keywords *p;
280 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
281 sizeof(keywords[0]), kw_cmp);
283 if (p)
284 return (p->k_val);
285 else
286 return (STRING);
289 #define MAXPUSHBACK 128
291 unsigned char *parsebuf;
292 int parseindex;
293 unsigned char pushback_buffer[MAXPUSHBACK];
294 int pushback_index = 0;
296 int
297 lgetc(int quotec)
299 int c, next;
301 if (parsebuf) {
302 /* Read character from the parsebuffer instead of input. */
303 if (parseindex >= 0) {
304 c = parsebuf[parseindex++];
305 if (c != '\0')
306 return (c);
307 parsebuf = NULL;
308 } else
309 parseindex++;
312 if (pushback_index)
313 return (pushback_buffer[--pushback_index]);
315 if (quotec) {
316 c = getc(file->stream);
317 if (c == EOF)
318 yyerror("reached end of file while parsing "
319 "quoted string");
320 return (c);
323 c = getc(file->stream);
324 while (c == '\\') {
325 next = getc(file->stream);
326 if (next != '\n') {
327 c = next;
328 break;
330 yylval.lineno = file->lineno;
331 file->lineno++;
332 c = getc(file->stream);
335 return (c);
338 int
339 lungetc(int c)
341 if (c == EOF)
342 return (EOF);
343 if (parsebuf) {
344 parseindex--;
345 if (parseindex >= 0)
346 return (c);
348 if (pushback_index < MAXPUSHBACK-1)
349 return (pushback_buffer[pushback_index++] = c);
350 else
351 return (EOF);
354 int
355 findeol(void)
357 int c;
359 parsebuf = NULL;
361 /* Skip to either EOF or the first real EOL. */
362 while (1) {
363 if (pushback_index)
364 c = pushback_buffer[--pushback_index];
365 else
366 c = lgetc(0);
367 if (c == '\n') {
368 file->lineno++;
369 break;
371 if (c == EOF)
372 break;
374 return (ERROR);
377 int
378 yylex(void)
380 unsigned char buf[8096];
381 unsigned char *p, *val;
382 int quotec, next, c;
383 int token;
385 top:
386 p = buf;
387 c = lgetc(0);
388 while (c == ' ' || c == '\t')
389 c = lgetc(0); /* nothing */
391 yylval.lineno = file->lineno;
392 if (c == '#') {
393 c = lgetc(0);
394 while (c != '\n' && c != EOF)
395 c = lgetc(0); /* nothing */
397 if (c == '$' && parsebuf == NULL) {
398 while (1) {
399 c = lgetc(0);
400 if (c == EOF)
401 return (0);
403 if (p + 1 >= buf + sizeof(buf) - 1) {
404 yyerror("string too long");
405 return (findeol());
407 if (isalnum(c) || c == '_') {
408 *p++ = c;
409 continue;
411 *p = '\0';
412 lungetc(c);
413 break;
415 val = symget(buf);
416 if (val == NULL) {
417 yyerror("macro '%s' not defined", buf);
418 return (findeol());
420 parsebuf = val;
421 parseindex = 0;
422 goto top;
425 switch (c) {
426 case '\'':
427 case '"':
428 quotec = c;
429 while (1) {
430 c = lgetc(quotec);
431 if (c == EOF)
432 return (0);
433 if (c == '\n') {
434 file->lineno++;
435 continue;
436 } else if (c == '\\') {
437 next = lgetc(quotec);
438 if (next == EOF)
439 return (0);
440 if (next == quotec || c == ' ' || c == '\t')
441 c = next;
442 else if (next == '\n') {
443 file->lineno++;
444 continue;
445 } else
446 lungetc(next);
447 } else if (c == quotec) {
448 *p = '\0';
449 break;
450 } else if (c == '\0') {
451 yyerror("syntax error");
452 return (findeol());
454 if (p + 1 >= buf + sizeof(buf) - 1) {
455 yyerror("string too long");
456 return (findeol());
458 *p++ = c;
460 yylval.v.string = strdup(buf);
461 if (yylval.v.string == NULL)
462 err(1, "yylex: strdup");
463 return (STRING);
466 #define allowed_to_end_number(x) \
467 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
469 if (c == '-' || isdigit(c)) {
470 do {
471 *p++ = c;
472 if ((unsigned)(p-buf) >= sizeof(buf)) {
473 yyerror("string too long");
474 return (findeol());
476 c = lgetc(0);
477 } while (c != EOF && isdigit(c));
478 lungetc(c);
479 if (p == buf + 1 && buf[0] == '-')
480 goto nodigits;
481 if (c == EOF || allowed_to_end_number(c)) {
482 const char *errstr = NULL;
484 *p = '\0';
485 yylval.v.number = strtonum(buf, LLONG_MIN,
486 LLONG_MAX, &errstr);
487 if (errstr) {
488 yyerror("\"%s\" invalid number: %s",
489 buf, errstr);
490 return (findeol());
492 return (NUMBER);
493 } else {
494 nodigits:
495 while (p > buf + 1)
496 lungetc(*--p);
497 c = *--p;
498 if (c == '-')
499 return (c);
503 #define allowed_in_string(x) \
504 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
505 x != '{' && x != '}' && \
506 x != '!' && x != '=' && x != '#' && \
507 x != ','))
509 if (isalnum(c) || c == ':' || c == '_') {
510 do {
511 *p++ = c;
512 if ((unsigned)(p-buf) >= sizeof(buf)) {
513 yyerror("string too long");
514 return (findeol());
516 c = lgetc(0);
517 } while (c != EOF && (allowed_in_string(c)));
518 lungetc(c);
519 *p = '\0';
520 token = lookup(buf);
521 if (token == STRING) {
522 yylval.v.string = strdup(buf);
523 if (yylval.v.string == NULL)
524 err(1, "yylex: strdup");
526 return (token);
528 if (c == '\n') {
529 yylval.lineno = file->lineno;
530 file->lineno++;
532 if (c == EOF)
533 return (0);
534 return (c);
537 int
538 check_file_secrecy(int fd, const char *fname)
540 struct stat st;
542 if (fstat(fd, &st)) {
543 log_warn("cannot stat %s", fname);
544 return (-1);
546 if (st.st_uid != 0 && st.st_uid != getuid()) {
547 log_warnx("%s: owner not root or current user", fname);
548 return (-1);
550 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
551 log_warnx("%s: group writable or world read/writable", fname);
552 return (-1);
554 return (0);
557 struct file *
558 newfile(const char *name, int secret)
560 struct file *nfile;
562 nfile = calloc(1, sizeof(struct file));
563 if (nfile == NULL) {
564 log_warn("calloc");
565 return (NULL);
567 nfile->name = strdup(name);
568 if (nfile->name == NULL) {
569 log_warn("strdup");
570 free(nfile);
571 return (NULL);
573 nfile->stream = fopen(nfile->name, "r");
574 if (nfile->stream == NULL) {
575 /* no warning, we don't require a conf file */
576 free(nfile->name);
577 free(nfile);
578 return (NULL);
579 } else if (secret &&
580 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
581 fclose(nfile->stream);
582 free(nfile->name);
583 free(nfile);
584 return (NULL);
586 nfile->lineno = 1;
587 return (nfile);
590 static void
591 closefile(struct file *xfile)
593 fclose(xfile->stream);
594 free(xfile->name);
595 free(xfile);
598 int
599 parse_config(const char *filename, enum gotd_procid proc_id,
600 struct gotd *env)
602 struct sym *sym, *next;
604 memset(env, 0, sizeof(*env));
606 gotd = env;
607 gotd_proc_id = proc_id;
608 TAILQ_INIT(&gotd->repos);
610 /* Apply default values. */
611 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
612 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
613 fprintf(stderr, "%s: unix socket path too long", __func__);
614 return -1;
616 if (strlcpy(gotd->unix_group_name, GOTD_UNIX_GROUP,
617 sizeof(gotd->unix_group_name)) >= sizeof(gotd->unix_group_name)) {
618 fprintf(stderr, "%s: unix group name too long", __func__);
619 return -1;
621 if (strlcpy(gotd->user_name, GOTD_USER,
622 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
623 fprintf(stderr, "%s: user name too long", __func__);
624 return -1;
627 file = newfile(filename, 0);
628 if (file == NULL) {
629 /* just return, as we don't require a conf file */
630 return (0);
633 yyparse();
634 errors = file->errors;
635 closefile(file);
637 /* Free macros and check which have not been used. */
638 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
639 if ((gotd->verbosity > 1) && !sym->used)
640 fprintf(stderr, "warning: macro '%s' not used\n",
641 sym->nam);
642 if (!sym->persist) {
643 free(sym->nam);
644 free(sym->val);
645 TAILQ_REMOVE(&symhead, sym, entry);
646 free(sym);
650 if (errors)
651 return (-1);
653 return (0);
656 static struct gotd_repo *
657 conf_new_repo(const char *name)
659 struct gotd_repo *repo;
661 if (strchr(name, '\n') != NULL) {
662 fatalx("%s: repository names must not contain linefeeds: %s",
663 getprogname(), name);
666 repo = calloc(1, sizeof(*repo));
667 if (repo == NULL)
668 fatalx("%s: calloc", __func__);
670 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
671 sizeof(repo->name))
672 fatalx("%s: strlcpy", __func__);
674 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
675 gotd->nrepos++;
677 return repo;
678 };
680 int
681 symset(const char *nam, const char *val, int persist)
683 struct sym *sym;
685 TAILQ_FOREACH(sym, &symhead, entry) {
686 if (strcmp(nam, sym->nam) == 0)
687 break;
690 if (sym != NULL) {
691 if (sym->persist == 1)
692 return (0);
693 else {
694 free(sym->nam);
695 free(sym->val);
696 TAILQ_REMOVE(&symhead, sym, entry);
697 free(sym);
700 sym = calloc(1, sizeof(*sym));
701 if (sym == NULL)
702 return (-1);
704 sym->nam = strdup(nam);
705 if (sym->nam == NULL) {
706 free(sym);
707 return (-1);
709 sym->val = strdup(val);
710 if (sym->val == NULL) {
711 free(sym->nam);
712 free(sym);
713 return (-1);
715 sym->used = 0;
716 sym->persist = persist;
717 TAILQ_INSERT_TAIL(&symhead, sym, entry);
718 return (0);
721 char *
722 symget(const char *nam)
724 struct sym *sym;
726 TAILQ_FOREACH(sym, &symhead, entry) {
727 if (strcmp(nam, sym->nam) == 0) {
728 sym->used = 1;
729 return (sym->val);
732 return (NULL);