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 *listener;
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 { listener = listen_new(); }
272 if (listener->auth_table == NULL)
273 yyerror("missing auth table");
274 if (!(listener->flags & L_TLS))
275 yyerror("can't define a non-tls listener");
280 listen_opts : listen_opt
281 | listen_opt listen_opts
284 listen_opt : ON STRING PORT NUMBER {
285 if (*listener->iface != '\0')
286 yyerror("listen address and port already"
288 strlcpy(listener->iface, $2, sizeof(listener->iface));
292 if (*listener->pki != '\0')
293 yyerror("listen tls pki already defined");
294 listener->flags |= L_TLS;
295 strlcpy(listener->pki, $3, sizeof(listener->pki));
298 if (listener->auth_table != NULL)
299 yyerror("listen auth already defined");
300 listener->auth_table = $2;
312 yyerror(const char *fmt, ...)
319 if (vasprintf(&msg, fmt, ap) == -1)
320 fatalx("yyerror vasprintf");
322 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
328 kw_cmp(const void *k, const void *e)
330 return strcmp(k, ((const struct keywords *)e)->k_name);
336 /* This has to be sorted always. */
337 static const struct keywords keywords[] = {
340 {"include", INCLUDE},
351 const struct keywords *p;
353 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
354 sizeof(keywords[0]), kw_cmp);
362 #define START_EXPAND 1
363 #define DONE_EXPAND 2
365 static int expanding;
373 if (file->ungetpos > 0)
374 c = file->ungetbuf[--file->ungetpos];
376 c = getc(file->stream);
378 if (c == START_EXPAND)
380 else if (c == DONE_EXPAND)
394 if ((c = igetc()) == EOF) {
395 yyerror("reached end of file while parsing "
397 if (file == topfile || popfile() == EOF)
404 while ((c = igetc()) == '\\') {
410 yylval.lineno = file->lineno;
416 * Fake EOL when hit EOF for the first time. This gets line
417 * count right if last line in included file is syntactically
418 * invalid and has no newline.
420 if (file->eof_reached == 0) {
421 file->eof_reached = 1;
425 if (file == topfile || popfile() == EOF)
439 if (file->ungetpos >= file->ungetsize) {
440 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
444 file->ungetsize *= 2;
446 file->ungetbuf[file->ungetpos++] = c;
454 /* Skip to either EOF or the first real EOL. */
470 unsigned char buf[8096];
471 unsigned char *p, *val;
477 while ((c = lgetc(0)) == ' ' || c == '\t')
480 yylval.lineno = file->lineno;
482 while ((c = lgetc(0)) != '\n' && c != EOF)
484 if (c == '$' && !expanding) {
486 if ((c = lgetc(0)) == EOF)
489 if (p + 1 >= buf + sizeof(buf) - 1) {
490 yyerror("string too long");
493 if (isalnum(c) || c == '_') {
503 yyerror("macro '%s' not defined", buf);
506 p = val + strlen(val) - 1;
507 lungetc(DONE_EXPAND);
512 lungetc(START_EXPAND);
521 if ((c = lgetc(quotec)) == EOF)
526 } else if (c == '\\') {
527 if ((next = lgetc(quotec)) == EOF)
529 if (next == quotec || next == ' ' ||
532 else if (next == '\n') {
537 } else if (c == quotec) {
540 } else if (c == '\0') {
541 yyerror("syntax error");
544 if (p + 1 >= buf + sizeof(buf) - 1) {
545 yyerror("string too long");
550 yylval.v.string = strdup(buf);
551 if (yylval.v.string == NULL)
552 err(1, "yylex: strdup");
556 #define allowed_to_end_number(x) \
557 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
559 if (c == '-' || isdigit(c)) {
562 if ((size_t)(p-buf) >= sizeof(buf)) {
563 yyerror("string too long");
566 } while ((c = lgetc(0)) != EOF && isdigit(c));
568 if (p == buf + 1 && buf[0] == '-')
570 if (c == EOF || allowed_to_end_number(c)) {
571 const char *errstr = NULL;
574 yylval.v.number = strtonum(buf, LLONG_MIN,
577 yyerror("\"%s\" invalid number: %s",
592 #define allowed_in_string(x) \
593 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
594 x != '{' && x != '}' && \
595 x != '!' && x != '=' && x != '#' && \
598 if (isalnum(c) || c == ':' || c == '_') {
601 if ((size_t)(p-buf) >= sizeof(buf)) {
602 yyerror("string too long");
605 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
608 if ((token = lookup(buf)) == STRING)
609 if ((yylval.v.string = strdup(buf)) == NULL)
610 err(1, "yylex: strdup");
614 yylval.lineno = file->lineno;
623 check_file_secrecy(int fd, const char *fname)
627 if (fstat(fd, &st)) {
628 log_warn("cannot stat %s", fname);
631 if (st.st_uid != 0 && st.st_uid != getuid()) {
632 log_warnx("%s: owner not root or current user", fname);
635 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
636 log_warnx("%s: group writable or world read/writable", fname);
643 pushfile(const char *name, int secret)
647 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
651 if ((nfile->name = strdup(name)) == NULL) {
656 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
657 log_warn("%s", nfile->name);
662 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
663 fclose(nfile->stream);
668 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
669 nfile->ungetsize = 16;
670 nfile->ungetbuf = malloc(nfile->ungetsize);
671 if (nfile->ungetbuf == NULL) {
673 fclose(nfile->stream);
678 TAILQ_INSERT_TAIL(&files, nfile, entry);
687 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
688 prev->errors += file->errors;
690 TAILQ_REMOVE(&files, file, entry);
691 fclose(file->stream);
693 free(file->ungetbuf);
696 return file ? 0 : EOF;
700 parse_config(const char *filename)
702 struct sym *sym, *next;
705 conf = config_new_empty();
707 file = pushfile(filename, 0);
715 errors = file->errors;
718 /* Free macros and check which have not been used. */
719 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
720 if (verbose && !sym->used)
721 fprintf(stderr, "warning: macro '%s' not used\n",
726 TAILQ_REMOVE(&symhead, sym, entry);
740 symset(const char *nam, const char *val, int persist)
744 TAILQ_FOREACH(sym, &symhead, entry) {
745 if (strcmp(nam, sym->nam) == 0)
750 if (sym->persist == 1)
755 TAILQ_REMOVE(&symhead, sym, entry);
759 if ((sym = calloc(1, sizeof(*sym))) == NULL)
762 sym->nam = strdup(nam);
763 if (sym->nam == NULL) {
767 sym->val = strdup(val);
768 if (sym->val == NULL) {
774 sym->persist = persist;
775 TAILQ_INSERT_TAIL(&symhead, sym, entry);
780 cmdline_symset(char *s)
785 if ((val = strrchr(s, '=')) == NULL)
787 sym = strndup(s, val - s);
789 errx(1, "%s: strndup", __func__);
790 ret = symset(sym, val + 1, 1);
797 symget(const char *nam)
801 TAILQ_FOREACH(sym, &symhead, entry) {
802 if (strcmp(nam, sym->nam) == 0) {
811 clear_config(struct kd_conf *xconf)
819 add_table(const char *name, const char *type, const char *path)
821 if (table_open(conf, name, type, path) == -1)
822 yyerror("can't initialize table %s", name);
823 table = STAILQ_FIRST(&conf->table_head)->table;
826 static struct table *
827 findtable(const char *name)
829 struct kd_tables_conf *i;
831 STAILQ_FOREACH(i, &conf->table_head, entry) {
832 if (!strcmp(i->table->t_name, name))
836 yyerror("unknown table %s", name);
841 add_cert(const char *name, const char *path)
843 struct kd_pki_conf *pki;
845 STAILQ_FOREACH(pki, &conf->pki_head, entry) {
846 if (strcmp(name, pki->name) != 0)
849 if (pki->cert != NULL) {
850 yyerror("duplicate `pki %s cert'", name);
857 pki = xcalloc(1, sizeof(*pki));
858 strlcpy(pki->name, name, sizeof(pki->name));
859 STAILQ_INSERT_HEAD(&conf->pki_head, pki, entry);
862 if ((pki->cert = tls_load_file(path, &pki->certlen, NULL)) == NULL)
867 add_key(const char *name, const char *path)
869 struct kd_pki_conf *pki;
871 STAILQ_FOREACH(pki, &conf->pki_head, entry) {
872 if (strcmp(name, pki->name) != 0)
875 if (pki->key != NULL) {
876 yyerror("duplicate `pki %s key'", name);
883 pki = xcalloc(1, sizeof(*pki));
884 strlcpy(pki->name, name, sizeof(pki->name));
885 STAILQ_INSERT_HEAD(&conf->pki_head, pki, entry);
888 if ((pki->key = tls_load_file(path, &pki->keylen, NULL)) == NULL)
892 static struct kd_listen_conf *
895 struct kd_listen_conf *l;
897 l = xcalloc(1, sizeof(*l));
901 STAILQ_INSERT_HEAD(&conf->listen_head, l, entry);