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 proxy *pr = NULL;
105 %token CHROOT HOSTNAME NO PORT PREFORK PROXY SOURCE STYLESHEET TLS
106 %token <v.number> NUMBER
107 %token <v.string> STRING
108 %type <v.number> port
109 %type <v.string> string
113 grammar : /* empty */
114 | grammar include '\n'
116 | grammar varset '\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 "proxies: %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 proxy : PROXY STRING {
178 if ((p = calloc(1, sizeof(*p))) == NULL)
181 n = strlcpy(p->pr_conf.host, $2,
182 sizeof(p->pr_conf.host));
183 if (n >= sizeof(p->pr_conf.host)) {
184 yyerror("server name too long");
192 } '{' optnl proxyopts_l '}' {
193 /* check if duplicate */
194 if (proxy_match(conf, pr->pr_conf.host) != NULL)
195 yyerror("duplicate proxy `%s'",
198 TAILQ_INSERT_TAIL(&conf->sc_proxies, pr, pr_entry);
200 if (*pr->pr_conf.proxy_addr == '\0')
201 yyerror("missing source in proxy block `%s'",
208 proxyopts_l : proxyopts_l proxyoptsl nl
212 proxyoptsl : SOURCE STRING proxyport {
215 n = strlcpy(pr->pr_conf.proxy_addr, $2,
216 sizeof(pr->pr_conf.proxy_addr));
217 if (n >= sizeof(pr->pr_conf.proxy_addr))
218 yyerror("proxy source too long!");
220 if (*pr->pr_conf.proxy_name == '\0') {
221 n = strlcpy(pr->pr_conf.proxy_name, $2,
222 sizeof(pr->pr_conf.proxy_name));
223 if (n >= sizeof(pr->pr_conf.proxy_name))
224 yyerror("proxy hostname too long!");
232 n = strlcpy(pr->pr_conf.proxy_name, $2,
233 sizeof(pr->pr_conf.proxy_name));
234 if (n >= sizeof(pr->pr_conf.proxy_name))
235 yyerror("proxy hostname too long!");
238 | STYLESHEET string {
241 n = strlcpy(pr->pr_conf.stylesheet, $2,
242 sizeof(pr->pr_conf.stylesheet));
243 if (n >= sizeof(pr->pr_conf.stylesheet))
244 yyerror("stylesheet path too long!");
248 pr->pr_conf.no_tls = 1;
252 proxyport : /* empty */ {
253 strlcpy(pr->pr_conf.proxy_port, "1965",
254 sizeof(pr->pr_conf.proxy_port));
260 len = sizeof(pr->pr_conf.proxy_port);
261 n = snprintf(pr->pr_conf.proxy_port, len, "%lld", $2);
262 if (n < 0 || (size_t)n >= len)
263 fatal("port number too long?");
267 if ($1 <= 0 || $1 > (int)USHRT_MAX) {
268 yyerror("invalid port: %lld", $1);
276 if ((val = getservice($1)) == -1) {
277 yyerror("invalid port: %s", $1);
286 string : STRING string {
287 if (asprintf(&$$, "%s%s", $1, $2) == -1)
288 fatal("asprintf string");
310 yyerror(const char *fmt, ...)
317 if (vasprintf(&msg, fmt, ap) == -1)
318 fatal("yyerror vasprintf");
320 log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
326 kw_cmp(const void *k, const void *e)
328 return (strcmp(k, ((const struct keywords *)e)->k_name));
334 /* this has to be sorted always */
335 static const struct keywords keywords[] = {
336 { "chroot", CHROOT },
337 { "hostname", HOSTNAME },
338 { "include", INCLUDE },
341 { "prefork", PREFORK },
343 { "source", SOURCE },
344 { "stylesheet", STYLESHEET},
347 const struct keywords *p;
349 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
358 #define START_EXPAND 1
359 #define DONE_EXPAND 2
361 static int expanding;
369 if (file->ungetpos > 0)
370 c = file->ungetbuf[--file->ungetpos];
372 c = getc(file->stream);
374 if (c == START_EXPAND)
376 else if (c == DONE_EXPAND)
390 if ((c = igetc()) == EOF) {
391 yyerror("reached end of file while parsing "
393 if (file == topfile || popfile() == EOF)
400 while ((c = igetc()) == '\\') {
406 yylval.lineno = file->lineno;
409 if (c == '\t' || c == ' ') {
410 /* Compress blanks to a single space. */
412 c = getc(file->stream);
413 } while (c == '\t' || c == ' ');
414 ungetc(c, file->stream);
420 * Fake EOL when hit EOF for the first time. This gets line
421 * count right if last line in included file is syntactically
422 * invalid and has no newline.
424 if (file->eof_reached == 0) {
425 file->eof_reached = 1;
429 if (file == topfile || popfile() == EOF)
443 if (file->ungetpos >= file->ungetsize) {
444 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
446 err(1, "%s", __func__);
448 file->ungetsize *= 2;
450 file->ungetbuf[file->ungetpos++] = c;
458 /* skip to either EOF or the first real EOL */
481 while ((c = lgetc(0)) == ' ' || c == '\t')
484 yylval.lineno = file->lineno;
486 while ((c = lgetc(0)) != '\n' && c != EOF)
488 if (c == '$' && !expanding) {
490 if ((c = lgetc(0)) == EOF)
493 if (p + 1 >= buf + sizeof(buf) - 1) {
494 yyerror("string too long");
497 if (isalnum(c) || c == '_') {
507 yyerror("macro '%s' not defined", buf);
510 p = val + strlen(val) - 1;
511 lungetc(DONE_EXPAND);
513 lungetc((unsigned char)*p);
516 lungetc(START_EXPAND);
525 if ((c = lgetc(quotec)) == EOF)
530 } else if (c == '\\') {
531 if ((next = lgetc(quotec)) == EOF)
533 if (next == quotec || next == ' ' ||
536 else if (next == '\n') {
541 } else if (c == quotec) {
544 } else if (c == '\0') {
545 yyerror("syntax error");
548 if (p + 1 >= buf + sizeof(buf) - 1) {
549 yyerror("string too long");
554 yylval.v.string = strdup(buf);
555 if (yylval.v.string == NULL)
556 fatal("yylex: strdup");
560 #define allowed_to_end_number(x) \
561 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
563 if (c == '-' || isdigit(c)) {
566 if ((size_t)(p-buf) >= sizeof(buf)) {
567 yyerror("string too long");
570 } while ((c = lgetc(0)) != EOF && isdigit(c));
572 if (p == buf + 1 && buf[0] == '-')
574 if (c == EOF || allowed_to_end_number(c)) {
575 const char *errstr = NULL;
578 yylval.v.number = strtonum(buf, LLONG_MIN,
581 yyerror("\"%s\" invalid number: %s",
589 lungetc((unsigned char)*--p);
590 c = (unsigned char)*--p;
596 #define allowed_in_string(x) \
597 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
598 x != '{' && x != '}' && \
599 x != '!' && x != '=' && x != '#' && \
602 if (isalnum(c) || c == ':' || c == '_' || c == '/') {
605 if ((size_t)(p-buf) >= sizeof(buf)) {
606 yyerror("string too long");
609 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
612 if ((token = lookup(buf)) == STRING)
613 if ((yylval.v.string = strdup(buf)) == NULL)
614 fatal("yylex: strdup");
618 yylval.lineno = file->lineno;
627 pushfile(const char *name, int secret)
631 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
632 log_warn("%s", __func__);
635 if ((nfile->name = strdup(name)) == NULL) {
636 log_warn("%s", __func__);
640 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
645 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
646 nfile->ungetsize = 16;
647 nfile->ungetbuf = malloc(nfile->ungetsize);
648 if (nfile->ungetbuf == NULL) {
649 log_warn("%s", __func__);
650 fclose(nfile->stream);
655 TAILQ_INSERT_TAIL(&files, nfile, entry);
664 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
665 prev->errors += file->errors;
667 TAILQ_REMOVE(&files, file, entry);
668 fclose(file->stream);
670 free(file->ungetbuf);
673 return (file ? 0 : EOF);
677 parse_config(const char *filename, struct galileo *env)
679 struct sym *sym, *next;
684 n = strlcpy(conf->sc_conffile, filename, sizeof(conf->sc_conffile));
685 if (n >= sizeof(conf->sc_conffile)) {
686 log_warn("path too long: %s", filename);
690 if ((file = pushfile(filename, 0)) == NULL) {
691 log_warn("failed to open %s", filename);
698 if (TAILQ_EMPTY(&conf->sc_proxies))
699 yyerror("no proxies defined");
700 errors = file->errors;
705 /* Free macros and check which have not been used. */
706 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
708 fprintf(stderr, "warning: macro `%s' not used\n",
713 TAILQ_REMOVE(&symhead, sym, entry);
725 symset(const char *nam, const char *val, int persist)
729 TAILQ_FOREACH(sym, &symhead, entry) {
730 if (strcmp(nam, sym->nam) == 0)
735 if (sym->persist == 1)
740 TAILQ_REMOVE(&symhead, sym, entry);
744 if ((sym = calloc(1, sizeof(*sym))) == NULL)
747 sym->nam = strdup(nam);
748 if (sym->nam == NULL) {
752 sym->val = strdup(val);
753 if (sym->val == NULL) {
759 sym->persist = persist;
760 TAILQ_INSERT_TAIL(&symhead, sym, entry);
765 cmdline_symset(char *s)
770 if ((val = strrchr(s, '=')) == NULL)
772 sym = strndup(s, val - s);
774 fatal("%s: strndup", __func__);
775 ret = symset(sym, val + 1, 1);
782 symget(const char *nam)
786 TAILQ_FOREACH(sym, &symhead, entry) {
787 if (strcmp(nam, sym->nam) == 0) {
796 getservice(const char *n)
802 llval = strtonum(n, 0, UINT16_MAX, &errstr);
804 s = getservbyname(n, "tcp");
806 s = getservbyname(n, "udp");
809 return (ntohs(s->s_port));
812 return ((unsigned short)llval);