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 PORT PREFORK PROXY 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'
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 TAILQ_INSERT_TAIL(&conf->sc_proxies, p, pr_entry);
193 } '{' optnl proxyopts_l '}' {
194 /* check if duplicate */
195 /* eventually load the tls certs */
201 proxyopts_l : proxyopts_l proxyoptsl nl
205 proxyoptsl : SOURCE STRING PORT port {
208 n = strlcpy(pr->pr_conf.proxy_addr, $2,
209 sizeof(pr->pr_conf.proxy_addr));
210 if (n >= sizeof(pr->pr_conf.proxy_addr))
211 yyerror("proxy source too long!");
212 pr->pr_conf.proxy_port = $4;
219 n = strlcpy(pr->pr_conf.proxy_name, $2,
220 sizeof(pr->pr_conf.proxy_name));
221 if (n >= sizeof(pr->pr_conf.proxy_name))
222 yyerror("proxy hostname too long!");
225 | STYLESHEET string {
228 n = strlcpy(pr->pr_conf.stylesheet, $2,
229 sizeof(pr->pr_conf.stylesheet));
230 if (n >= sizeof(pr->pr_conf.stylesheet))
231 yyerror("stylesheet path too long!");
237 if ($1 <= 0 || $1 > (int)USHRT_MAX) {
238 yyerror("invalid port: %lld", $1);
246 if ((val = getservice($1)) == -1) {
247 yyerror("invalid port: %s", $1);
256 string : STRING string {
257 if (asprintf(&$$, "%s%s", $1, $2) == -1)
258 fatal("asprintf string");
280 yyerror(const char *fmt, ...)
287 if (vasprintf(&msg, fmt, ap) == -1)
288 fatal("yyerror vasprintf");
290 log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
296 kw_cmp(const void *k, const void *e)
298 return (strcmp(k, ((const struct keywords *)e)->k_name));
304 /* this has to be sorted always */
305 static const struct keywords keywords[] = {
306 { "chroot", CHROOT },
307 { "hostname", HOSTNAME },
308 { "include", INCLUDE },
310 { "prefork", PREFORK },
312 { "source", SOURCE },
313 { "stylesheet", STYLESHEET},
315 const struct keywords *p;
317 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
326 #define START_EXPAND 1
327 #define DONE_EXPAND 2
329 static int expanding;
337 if (file->ungetpos > 0)
338 c = file->ungetbuf[--file->ungetpos];
340 c = getc(file->stream);
342 if (c == START_EXPAND)
344 else if (c == DONE_EXPAND)
358 if ((c = igetc()) == EOF) {
359 yyerror("reached end of file while parsing "
361 if (file == topfile || popfile() == EOF)
368 while ((c = igetc()) == '\\') {
374 yylval.lineno = file->lineno;
377 if (c == '\t' || c == ' ') {
378 /* Compress blanks to a single space. */
380 c = getc(file->stream);
381 } while (c == '\t' || c == ' ');
382 ungetc(c, file->stream);
388 * Fake EOL when hit EOF for the first time. This gets line
389 * count right if last line in included file is syntactically
390 * invalid and has no newline.
392 if (file->eof_reached == 0) {
393 file->eof_reached = 1;
397 if (file == topfile || popfile() == EOF)
411 if (file->ungetpos >= file->ungetsize) {
412 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
414 err(1, "%s", __func__);
416 file->ungetsize *= 2;
418 file->ungetbuf[file->ungetpos++] = c;
426 /* skip to either EOF or the first real EOL */
449 while ((c = lgetc(0)) == ' ' || c == '\t')
452 yylval.lineno = file->lineno;
454 while ((c = lgetc(0)) != '\n' && c != EOF)
456 if (c == '$' && !expanding) {
458 if ((c = lgetc(0)) == EOF)
461 if (p + 1 >= buf + sizeof(buf) - 1) {
462 yyerror("string too long");
465 if (isalnum(c) || c == '_') {
475 yyerror("macro '%s' not defined", buf);
478 p = val + strlen(val) - 1;
479 lungetc(DONE_EXPAND);
481 lungetc((unsigned char)*p);
484 lungetc(START_EXPAND);
493 if ((c = lgetc(quotec)) == EOF)
498 } else if (c == '\\') {
499 if ((next = lgetc(quotec)) == EOF)
501 if (next == quotec || next == ' ' ||
504 else if (next == '\n') {
509 } else if (c == quotec) {
512 } else if (c == '\0') {
513 yyerror("syntax error");
516 if (p + 1 >= buf + sizeof(buf) - 1) {
517 yyerror("string too long");
522 yylval.v.string = strdup(buf);
523 if (yylval.v.string == NULL)
524 fatal("yylex: strdup");
528 #define allowed_to_end_number(x) \
529 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
531 if (c == '-' || isdigit(c)) {
534 if ((size_t)(p-buf) >= sizeof(buf)) {
535 yyerror("string too long");
538 } while ((c = lgetc(0)) != EOF && isdigit(c));
540 if (p == buf + 1 && buf[0] == '-')
542 if (c == EOF || allowed_to_end_number(c)) {
543 const char *errstr = NULL;
546 yylval.v.number = strtonum(buf, LLONG_MIN,
549 yyerror("\"%s\" invalid number: %s",
557 lungetc((unsigned char)*--p);
558 c = (unsigned char)*--p;
564 #define allowed_in_string(x) \
565 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
566 x != '{' && x != '}' && \
567 x != '!' && x != '=' && x != '#' && \
570 if (isalnum(c) || c == ':' || c == '_' || c == '/') {
573 if ((size_t)(p-buf) >= sizeof(buf)) {
574 yyerror("string too long");
577 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
580 if ((token = lookup(buf)) == STRING)
581 if ((yylval.v.string = strdup(buf)) == NULL)
582 fatal("yylex: strdup");
586 yylval.lineno = file->lineno;
595 pushfile(const char *name, int secret)
599 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
600 log_warn("%s", __func__);
603 if ((nfile->name = strdup(name)) == NULL) {
604 log_warn("%s", __func__);
608 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
613 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
614 nfile->ungetsize = 16;
615 nfile->ungetbuf = malloc(nfile->ungetsize);
616 if (nfile->ungetbuf == NULL) {
617 log_warn("%s", __func__);
618 fclose(nfile->stream);
623 TAILQ_INSERT_TAIL(&files, nfile, entry);
632 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
633 prev->errors += file->errors;
635 TAILQ_REMOVE(&files, file, entry);
636 fclose(file->stream);
638 free(file->ungetbuf);
641 return (file ? 0 : EOF);
645 parse_config(const char *filename, struct galileo *env)
647 struct sym *sym, *next;
652 n = strlcpy(conf->sc_conffile, filename, sizeof(conf->sc_conffile));
653 if (n >= sizeof(conf->sc_conffile)) {
654 log_warn("path too long: %s", filename);
658 if ((file = pushfile(filename, 0)) == NULL) {
659 log_warn("failed to open %s", filename);
666 errors = file->errors;
671 /* Free macros and check which have not been used. */
672 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
674 fprintf(stderr, "warning: macro `%s' not used\n",
679 TAILQ_REMOVE(&symhead, sym, entry);
691 symset(const char *nam, const char *val, int persist)
695 TAILQ_FOREACH(sym, &symhead, entry) {
696 if (strcmp(nam, sym->nam) == 0)
701 if (sym->persist == 1)
706 TAILQ_REMOVE(&symhead, sym, entry);
710 if ((sym = calloc(1, sizeof(*sym))) == NULL)
713 sym->nam = strdup(nam);
714 if (sym->nam == NULL) {
718 sym->val = strdup(val);
719 if (sym->val == NULL) {
725 sym->persist = persist;
726 TAILQ_INSERT_TAIL(&symhead, sym, entry);
731 cmdline_symset(char *s)
736 if ((val = strrchr(s, '=')) == NULL)
738 sym = strndup(s, val - s);
740 fatal("%s: strndup", __func__);
741 ret = symset(sym, val + 1, 1);
748 symget(const char *nam)
752 TAILQ_FOREACH(sym, &symhead, entry) {
753 if (strcmp(nam, sym->nam) == 0) {
762 getservice(const char *n)
768 llval = strtonum(n, 0, UINT16_MAX, &errstr);
770 s = getservbyname(n, "tcp");
772 s = getservbyname(n, "udp");
775 return (ntohs(s->s_port));
778 return ((unsigned short)llval);