2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
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.
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.
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.
47 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
49 TAILQ_ENTRY(file) entry;
59 struct file *pushfile(const char *, int);
61 int check_file_secrecy(int, const char *);
64 int yyerror(const char *, ...)
65 __attribute__((__format__ (printf, 1, 2)))
66 __attribute__((__nonnull__ (1)));
67 int kw_cmp(const void *, const void *);
74 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
76 TAILQ_ENTRY(sym) entry;
83 int symset(const char *, const char *, int);
84 char *symget(const char *);
86 void clear_config(struct kd_conf *xconf);
88 static void add_table(const char *, const char *, const char *);
89 static struct table *findtable(const char *name);
90 static void add_cert(const char *, const char *);
91 static void add_key(const char *, const char *);
92 static struct kd_listen_conf *listen_new(void);
94 static uint32_t counter;
95 static struct table *table;
96 static struct kd_listen_conf *listen;
97 static struct kd_conf *conf;
123 %token <v.string> STRING
124 %token <v.number> NUMBER
125 %type <v.number> yesno
126 %type <v.string> string
127 %type <v.table> tableref
131 grammar : /* empty */
132 | grammar include '\n'
136 | grammar listen '\n'
137 | grammar varset '\n'
138 | grammar error '\n' { file->errors++; }
141 include : INCLUDE STRING {
144 if ((nfile = pushfile($2, 0)) == NULL) {
145 yyerror("failed to include file %s", $2);
156 string : string STRING {
157 if (asprintf(&$$, "%s %s", $1, $2) == -1) {
160 yyerror("string: asprintf");
169 yesno : YES { $$ = 1; }
173 optnl : '\n' optnl /* zero or more newlines */
177 nl : '\n' optnl /* one or more newlines */
186 varset : STRING '=' string {
189 printf("%s = \"%s\"\n", $1, $3);
191 if (isspace((unsigned char)*s)) {
192 yyerror("macro name cannot contain "
199 if (symset($1, $3, 0) == -1)
200 fatal("cannot store variable");
206 pki : PKI STRING CERT STRING { add_cert($2, $4); }
207 | PKI STRING KEY STRING { add_key($2, $4); }
210 table_kp : string arrow string {
211 if (table_add(table, $1, $3) == -1)
212 yyerror("can't add to table %s",
220 | table_kp comma table_kps
224 if (table_add(table, $1, NULL) == -1)
225 yyerror("can't add to table %s",
231 string_list : stringel
232 | stringel comma string_list
235 table_vals : table_kps
239 table : TABLE STRING STRING {
242 if ((p = strchr($3, ':')) == NULL) {
243 yyerror("invalid table %s", $2);
248 add_table($2, $3, p+1);
253 add_table($2, "static", NULL);
254 } '{' table_vals '}' {
259 tableref : '<' STRING '>' {
270 listen : LISTEN { listen = listen_new(); }
271 listen_opts { listen = NULL; };
273 listen_opts : listen_opt
274 | listen_opt listen_opts
277 listen_opt : ON STRING PORT NUMBER {
278 if (*listen->iface != '\0')
279 yyerror("listen address and port already"
281 strlcpy(listen->iface, $2, sizeof(listen->iface));
285 if (*listen->pki != '\0')
286 yyerror("listen tls pki already defined");
287 strlcpy(listen->pki, $3, sizeof(listen->pki));
290 if (listen->auth_table != NULL)
291 yyerror("listen auth already defined");
292 listen->auth_table = $2;
304 yyerror(const char *fmt, ...)
311 if (vasprintf(&msg, fmt, ap) == -1)
312 fatalx("yyerror vasprintf");
314 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
320 kw_cmp(const void *k, const void *e)
322 return strcmp(k, ((const struct keywords *)e)->k_name);
328 /* This has to be sorted always. */
329 static const struct keywords keywords[] = {
332 {"include", INCLUDE},
343 const struct keywords *p;
345 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
346 sizeof(keywords[0]), kw_cmp);
354 #define START_EXPAND 1
355 #define DONE_EXPAND 2
357 static int expanding;
365 if (file->ungetpos > 0)
366 c = file->ungetbuf[--file->ungetpos];
368 c = getc(file->stream);
370 if (c == START_EXPAND)
372 else if (c == DONE_EXPAND)
386 if ((c = igetc()) == EOF) {
387 yyerror("reached end of file while parsing "
389 if (file == topfile || popfile() == EOF)
396 while ((c = igetc()) == '\\') {
402 yylval.lineno = file->lineno;
408 * Fake EOL when hit EOF for the first time. This gets line
409 * count right if last line in included file is syntactically
410 * invalid and has no newline.
412 if (file->eof_reached == 0) {
413 file->eof_reached = 1;
417 if (file == topfile || popfile() == EOF)
431 if (file->ungetpos >= file->ungetsize) {
432 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
436 file->ungetsize *= 2;
438 file->ungetbuf[file->ungetpos++] = c;
446 /* Skip to either EOF or the first real EOL. */
462 unsigned char buf[8096];
463 unsigned char *p, *val;
469 while ((c = lgetc(0)) == ' ' || c == '\t')
472 yylval.lineno = file->lineno;
474 while ((c = lgetc(0)) != '\n' && c != EOF)
476 if (c == '$' && !expanding) {
478 if ((c = lgetc(0)) == EOF)
481 if (p + 1 >= buf + sizeof(buf) - 1) {
482 yyerror("string too long");
485 if (isalnum(c) || c == '_') {
495 yyerror("macro '%s' not defined", buf);
498 p = val + strlen(val) - 1;
499 lungetc(DONE_EXPAND);
504 lungetc(START_EXPAND);
513 if ((c = lgetc(quotec)) == EOF)
518 } else if (c == '\\') {
519 if ((next = lgetc(quotec)) == EOF)
521 if (next == quotec || next == ' ' ||
524 else if (next == '\n') {
529 } else if (c == quotec) {
532 } else if (c == '\0') {
533 yyerror("syntax error");
536 if (p + 1 >= buf + sizeof(buf) - 1) {
537 yyerror("string too long");
542 yylval.v.string = strdup(buf);
543 if (yylval.v.string == NULL)
544 err(1, "yylex: strdup");
548 #define allowed_to_end_number(x) \
549 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
551 if (c == '-' || isdigit(c)) {
554 if ((size_t)(p-buf) >= sizeof(buf)) {
555 yyerror("string too long");
558 } while ((c = lgetc(0)) != EOF && isdigit(c));
560 if (p == buf + 1 && buf[0] == '-')
562 if (c == EOF || allowed_to_end_number(c)) {
563 const char *errstr = NULL;
566 yylval.v.number = strtonum(buf, LLONG_MIN,
569 yyerror("\"%s\" invalid number: %s",
584 #define allowed_in_string(x) \
585 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
586 x != '{' && x != '}' && \
587 x != '!' && x != '=' && x != '#' && \
590 if (isalnum(c) || c == ':' || c == '_') {
593 if ((size_t)(p-buf) >= sizeof(buf)) {
594 yyerror("string too long");
597 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
600 if ((token = lookup(buf)) == STRING)
601 if ((yylval.v.string = strdup(buf)) == NULL)
602 err(1, "yylex: strdup");
606 yylval.lineno = file->lineno;
615 check_file_secrecy(int fd, const char *fname)
619 if (fstat(fd, &st)) {
620 log_warn("cannot stat %s", fname);
623 if (st.st_uid != 0 && st.st_uid != getuid()) {
624 log_warnx("%s: owner not root or current user", fname);
627 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
628 log_warnx("%s: group writable or world read/writable", fname);
635 pushfile(const char *name, int secret)
639 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
643 if ((nfile->name = strdup(name)) == NULL) {
648 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
649 log_warn("%s", nfile->name);
654 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
655 fclose(nfile->stream);
660 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
661 nfile->ungetsize = 16;
662 nfile->ungetbuf = malloc(nfile->ungetsize);
663 if (nfile->ungetbuf == NULL) {
665 fclose(nfile->stream);
670 TAILQ_INSERT_TAIL(&files, nfile, entry);
679 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
680 prev->errors += file->errors;
682 TAILQ_REMOVE(&files, file, entry);
683 fclose(file->stream);
685 free(file->ungetbuf);
688 return file ? 0 : EOF;
692 parse_config(const char *filename)
694 struct sym *sym, *next;
697 conf = config_new_empty();
699 file = pushfile(filename, 0);
707 errors = file->errors;
710 /* Free macros and check which have not been used. */
711 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
712 if (verbose && !sym->used)
713 fprintf(stderr, "warning: macro '%s' not used\n",
718 TAILQ_REMOVE(&symhead, sym, entry);
732 symset(const char *nam, const char *val, int persist)
736 TAILQ_FOREACH(sym, &symhead, entry) {
737 if (strcmp(nam, sym->nam) == 0)
742 if (sym->persist == 1)
747 TAILQ_REMOVE(&symhead, sym, entry);
751 if ((sym = calloc(1, sizeof(*sym))) == NULL)
754 sym->nam = strdup(nam);
755 if (sym->nam == NULL) {
759 sym->val = strdup(val);
760 if (sym->val == NULL) {
766 sym->persist = persist;
767 TAILQ_INSERT_TAIL(&symhead, sym, entry);
772 cmdline_symset(char *s)
777 if ((val = strrchr(s, '=')) == NULL)
779 sym = strndup(s, val - s);
781 errx(1, "%s: strndup", __func__);
782 ret = symset(sym, val + 1, 1);
789 symget(const char *nam)
793 TAILQ_FOREACH(sym, &symhead, entry) {
794 if (strcmp(nam, sym->nam) == 0) {
803 clear_config(struct kd_conf *xconf)
811 add_table(const char *name, const char *type, const char *path)
813 if (table_open(conf, name, type, path) == -1)
814 yyerror("can't initialize table %s", name);
815 table = SIMPLEQ_FIRST(&conf->table_head)->table;
818 static struct table *
819 findtable(const char *name)
821 struct kd_tables_conf *i;
823 SIMPLEQ_FOREACH(i, &conf->table_head, entry) {
824 if (!strcmp(i->table->t_name, name))
828 yyerror("unknown table %s", name);
833 add_cert(const char *name, const char *path)
835 struct kd_pki_conf *pki;
837 SIMPLEQ_FOREACH(pki, &conf->pki_head, entry) {
838 if (strcmp(name, pki->name) != 0)
841 if (pki->cert != NULL) {
842 yyerror("duplicate `pki %s cert'", name);
849 pki = xcalloc(1, sizeof(*pki));
850 strlcpy(pki->name, name, sizeof(pki->name));
851 SIMPLEQ_INSERT_HEAD(&conf->pki_head, pki, entry);
854 if ((pki->cert = tls_load_file(path, &pki->certlen, NULL)) == NULL)
859 add_key(const char *name, const char *path)
861 struct kd_pki_conf *pki;
863 SIMPLEQ_FOREACH(pki, &conf->pki_head, entry) {
864 if (strcmp(name, pki->name) != 0)
867 if (pki->key != NULL) {
868 yyerror("duplicate `pki %s key'", name);
875 pki = xcalloc(1, sizeof(*pki));
876 strlcpy(pki->name, name, sizeof(pki->name));
877 SIMPLEQ_INSERT_HEAD(&conf->pki_head, pki, entry);
880 if ((pki->key = tls_load_file(path, &pki->keylen, NULL)) == NULL)
884 static struct kd_listen_conf *
887 struct kd_listen_conf *l;
889 l = xcalloc(1, sizeof(*l));
893 SIMPLEQ_INSERT_HEAD(&conf->listen_head, l, entry);