2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2007-2016 Reyk Floeter <reyk@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.
25 #include <sys/types.h>
26 #include <sys/queue.h>
51 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
53 TAILQ_ENTRY(file) entry;
63 struct file *pushfile(const char *, int);
67 int yyerror(const char *, ...)
68 __attribute__((__format__ (printf, 1, 2)))
69 __attribute__((__nonnull__ (1)));
70 int kw_cmp(const void *, const void *);
77 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
79 TAILQ_ENTRY(sym) entry;
85 int symset(const char *, const char *, int);
86 char *symget(const char *);
88 int getservice(const char *);
90 static struct galileo *conf = NULL;
91 static struct server *srv = NULL;
105 %token CHROOT HOSTNAME PORT PREFORK PROXY SERVER SOURCE STYLESHEET
106 %type <v.number> NUMBER
107 %type <v.number> port
108 %type <v.string> STRING
109 %type <v.string> string
113 grammar : /* empty */
114 | grammar include '\n'
116 | grammar varset '\n'
118 | grammar server '\n'
119 | grammar error '\n' { file->errors++; }
122 include : INCLUDE string {
125 if ((nfile = pushfile($2, 0)) == NULL) {
126 yyerror("failed to include file %s", $2);
137 varset : STRING '=' STRING {
140 if (isspace((unsigned char)*s)) {
141 yyerror("macro name cannot contain "
148 if (symset($1, $3, 0) == -1)
149 fatalx("cannot store variable");
155 main : PREFORK NUMBER {
156 if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
157 yyerror("invalid number of preforked "
158 "servers: %lld", $2);
161 conf->sc_prefork = $2;
166 n = strlcpy(conf->sc_chroot, $2,
167 sizeof(conf->sc_chroot));
168 if (n >= sizeof(conf->sc_chroot))
169 yyerror("chroot path too long!");
174 server : SERVER STRING {
178 if ((s = calloc(1, sizeof(*s))) == NULL)
181 n = strlcpy(s->srv_conf.host, $2,
182 sizeof(s->srv_conf.host));
183 if (n >= sizeof(s->srv_conf.host)) {
184 yyerror("server name too long");
192 TAILQ_INSERT_TAIL(&conf->sc_servers, srv, srv_entry);
193 } '{' optnl serveropts_l '}' {
194 /* check if duplicate */
195 /* eventually load the tls certs */
201 serveropts_l : serveropts_l serveroptsl nl
205 serveroptsl : PROXY STRING {
208 | PROXY '{' optnl proxyopts_l '}'
209 | STYLESHEET string {
212 n = strlcpy(srv->srv_conf.stylesheet, $2,
213 sizeof(srv->srv_conf.stylesheet));
214 if (n >= sizeof(srv->srv_conf.stylesheet))
215 yyerror("stylesheet path too long!");
220 proxyopts_l : proxyopts_l proxyoptsl nl
224 proxyoptsl : SOURCE STRING PORT port {
227 n = strlcpy(srv->srv_conf.proxy_addr, $2,
228 sizeof(srv->srv_conf.proxy_addr));
229 if (n >= sizeof(srv->srv_conf.proxy_addr))
230 yyerror("proxy source too long!");
231 srv->srv_conf.proxy_port = $4;
238 n = strlcpy(srv->srv_conf.proxy_name, $2,
239 sizeof(srv->srv_conf.proxy_name));
240 if (n >= sizeof(srv->srv_conf.proxy_name))
241 yyerror("proxy hostname too long!");
247 if ($1 <= 0 || $1 > (int)USHRT_MAX) {
248 yyerror("invalid port: %lld", $1);
256 if ((val = getservice($1)) == -1) {
257 yyerror("invalid port: %s", $1);
266 string : STRING string {
267 if (asprintf(&$$, "%s%s", $1, $2) == -1)
268 fatal("asprintf string");
290 yyerror(const char *fmt, ...)
297 if (vasprintf(&msg, fmt, ap) == -1)
298 fatal("yyerror vasprintf");
300 log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
306 kw_cmp(const void *k, const void *e)
308 return (strcmp(k, ((const struct keywords *)e)->k_name));
314 /* this has to be sorted always */
315 static const struct keywords keywords[] = {
316 { "chroot", CHROOT },
317 { "hostname", HOSTNAME },
318 { "include", INCLUDE },
320 { "prefork", PREFORK },
322 { "server", SERVER },
323 { "source", SOURCE },
324 { "stylesheet", STYLESHEET},
326 const struct keywords *p;
328 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
337 #define START_EXPAND 1
338 #define DONE_EXPAND 2
340 static int expanding;
348 if (file->ungetpos > 0)
349 c = file->ungetbuf[--file->ungetpos];
351 c = getc(file->stream);
353 if (c == START_EXPAND)
355 else if (c == DONE_EXPAND)
369 if ((c = igetc()) == EOF) {
370 yyerror("reached end of file while parsing "
372 if (file == topfile || popfile() == EOF)
379 while ((c = igetc()) == '\\') {
385 yylval.lineno = file->lineno;
388 if (c == '\t' || c == ' ') {
389 /* Compress blanks to a single space. */
391 c = getc(file->stream);
392 } while (c == '\t' || c == ' ');
393 ungetc(c, file->stream);
399 * Fake EOL when hit EOF for the first time. This gets line
400 * count right if last line in included file is syntactically
401 * invalid and has no newline.
403 if (file->eof_reached == 0) {
404 file->eof_reached = 1;
408 if (file == topfile || popfile() == EOF)
422 if (file->ungetpos >= file->ungetsize) {
423 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
425 err(1, "%s", __func__);
427 file->ungetsize *= 2;
429 file->ungetbuf[file->ungetpos++] = c;
437 /* skip to either EOF or the first real EOL */
460 while ((c = lgetc(0)) == ' ' || c == '\t')
463 yylval.lineno = file->lineno;
465 while ((c = lgetc(0)) != '\n' && c != EOF)
467 if (c == '$' && !expanding) {
469 if ((c = lgetc(0)) == EOF)
472 if (p + 1 >= buf + sizeof(buf) - 1) {
473 yyerror("string too long");
476 if (isalnum(c) || c == '_') {
486 yyerror("macro '%s' not defined", buf);
489 p = val + strlen(val) - 1;
490 lungetc(DONE_EXPAND);
492 lungetc((unsigned char)*p);
495 lungetc(START_EXPAND);
504 if ((c = lgetc(quotec)) == EOF)
509 } else if (c == '\\') {
510 if ((next = lgetc(quotec)) == EOF)
512 if (next == quotec || next == ' ' ||
515 else if (next == '\n') {
520 } else if (c == quotec) {
523 } else if (c == '\0') {
524 yyerror("syntax error");
527 if (p + 1 >= buf + sizeof(buf) - 1) {
528 yyerror("string too long");
533 yylval.v.string = strdup(buf);
534 if (yylval.v.string == NULL)
535 fatal("yylex: strdup");
539 #define allowed_to_end_number(x) \
540 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
542 if (c == '-' || isdigit(c)) {
545 if ((size_t)(p-buf) >= sizeof(buf)) {
546 yyerror("string too long");
549 } while ((c = lgetc(0)) != EOF && isdigit(c));
551 if (p == buf + 1 && buf[0] == '-')
553 if (c == EOF || allowed_to_end_number(c)) {
554 const char *errstr = NULL;
557 yylval.v.number = strtonum(buf, LLONG_MIN,
560 yyerror("\"%s\" invalid number: %s",
568 lungetc((unsigned char)*--p);
569 c = (unsigned char)*--p;
575 #define allowed_in_string(x) \
576 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
577 x != '{' && x != '}' && \
578 x != '!' && x != '=' && x != '#' && \
581 if (isalnum(c) || c == ':' || c == '_' || c == '/') {
584 if ((size_t)(p-buf) >= sizeof(buf)) {
585 yyerror("string too long");
588 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
591 if ((token = lookup(buf)) == STRING)
592 if ((yylval.v.string = strdup(buf)) == NULL)
593 fatal("yylex: strdup");
597 yylval.lineno = file->lineno;
606 pushfile(const char *name, int secret)
610 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
611 log_warn("%s", __func__);
614 if ((nfile->name = strdup(name)) == NULL) {
615 log_warn("%s", __func__);
619 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
624 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
625 nfile->ungetsize = 16;
626 nfile->ungetbuf = malloc(nfile->ungetsize);
627 if (nfile->ungetbuf == NULL) {
628 log_warn("%s", __func__);
629 fclose(nfile->stream);
634 TAILQ_INSERT_TAIL(&files, nfile, entry);
643 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
644 prev->errors += file->errors;
646 TAILQ_REMOVE(&files, file, entry);
647 fclose(file->stream);
649 free(file->ungetbuf);
652 return (file ? 0 : EOF);
656 parse_config(const char *filename, struct galileo *env)
658 struct sym *sym, *next;
663 n = strlcpy(conf->sc_conffile, filename, sizeof(conf->sc_conffile));
664 if (n >= sizeof(conf->sc_conffile)) {
665 log_warn("path too long: %s", filename);
669 if ((file = pushfile(filename, 0)) == NULL) {
670 log_warn("failed to open %s", filename);
677 errors = file->errors;
682 /* Free macros and check which have not been used. */
683 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
685 fprintf(stderr, "warning: macro `%s' not used\n",
690 TAILQ_REMOVE(&symhead, sym, entry);
702 symset(const char *nam, const char *val, int persist)
706 TAILQ_FOREACH(sym, &symhead, entry) {
707 if (strcmp(nam, sym->nam) == 0)
712 if (sym->persist == 1)
717 TAILQ_REMOVE(&symhead, sym, entry);
721 if ((sym = calloc(1, sizeof(*sym))) == NULL)
724 sym->nam = strdup(nam);
725 if (sym->nam == NULL) {
729 sym->val = strdup(val);
730 if (sym->val == NULL) {
736 sym->persist = persist;
737 TAILQ_INSERT_TAIL(&symhead, sym, entry);
742 cmdline_symset(char *s)
747 if ((val = strrchr(s, '=')) == NULL)
749 sym = strndup(s, val - s);
751 fatal("%s: strndup", __func__);
752 ret = symset(sym, val + 1, 1);
759 symget(const char *nam)
763 TAILQ_FOREACH(sym, &symhead, entry) {
764 if (strcmp(nam, sym->nam) == 0) {
773 getservice(const char *n)
779 llval = strtonum(n, 0, UINT16_MAX, &errstr);
781 s = getservbyname(n, "tcp");
783 s = getservbyname(n, "udp");
786 return (ntohs(s->s_port));
789 return ((unsigned short)llval);