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 %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!");
249 proxyport : /* empty */ {
250 strlcpy(pr->pr_conf.proxy_port, "1965",
251 sizeof(pr->pr_conf.proxy_port));
257 len = sizeof(pr->pr_conf.proxy_port);
258 n = snprintf(pr->pr_conf.proxy_port, len, "%lld", $2);
259 if (n < 0 || (size_t)n >= len)
260 fatal("port number too long?");
264 if ($1 <= 0 || $1 > (int)USHRT_MAX) {
265 yyerror("invalid port: %lld", $1);
273 if ((val = getservice($1)) == -1) {
274 yyerror("invalid port: %s", $1);
283 string : STRING string {
284 if (asprintf(&$$, "%s%s", $1, $2) == -1)
285 fatal("asprintf string");
307 yyerror(const char *fmt, ...)
314 if (vasprintf(&msg, fmt, ap) == -1)
315 fatal("yyerror vasprintf");
317 log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
323 kw_cmp(const void *k, const void *e)
325 return (strcmp(k, ((const struct keywords *)e)->k_name));
331 /* this has to be sorted always */
332 static const struct keywords keywords[] = {
333 { "chroot", CHROOT },
334 { "hostname", HOSTNAME },
335 { "include", INCLUDE },
337 { "prefork", PREFORK },
339 { "source", SOURCE },
340 { "stylesheet", STYLESHEET},
342 const struct keywords *p;
344 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
353 #define START_EXPAND 1
354 #define DONE_EXPAND 2
356 static int expanding;
364 if (file->ungetpos > 0)
365 c = file->ungetbuf[--file->ungetpos];
367 c = getc(file->stream);
369 if (c == START_EXPAND)
371 else if (c == DONE_EXPAND)
385 if ((c = igetc()) == EOF) {
386 yyerror("reached end of file while parsing "
388 if (file == topfile || popfile() == EOF)
395 while ((c = igetc()) == '\\') {
401 yylval.lineno = file->lineno;
404 if (c == '\t' || c == ' ') {
405 /* Compress blanks to a single space. */
407 c = getc(file->stream);
408 } while (c == '\t' || c == ' ');
409 ungetc(c, file->stream);
415 * Fake EOL when hit EOF for the first time. This gets line
416 * count right if last line in included file is syntactically
417 * invalid and has no newline.
419 if (file->eof_reached == 0) {
420 file->eof_reached = 1;
424 if (file == topfile || popfile() == EOF)
438 if (file->ungetpos >= file->ungetsize) {
439 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
441 err(1, "%s", __func__);
443 file->ungetsize *= 2;
445 file->ungetbuf[file->ungetpos++] = c;
453 /* skip to either EOF or the first real EOL */
476 while ((c = lgetc(0)) == ' ' || c == '\t')
479 yylval.lineno = file->lineno;
481 while ((c = lgetc(0)) != '\n' && c != EOF)
483 if (c == '$' && !expanding) {
485 if ((c = lgetc(0)) == EOF)
488 if (p + 1 >= buf + sizeof(buf) - 1) {
489 yyerror("string too long");
492 if (isalnum(c) || c == '_') {
502 yyerror("macro '%s' not defined", buf);
505 p = val + strlen(val) - 1;
506 lungetc(DONE_EXPAND);
508 lungetc((unsigned char)*p);
511 lungetc(START_EXPAND);
520 if ((c = lgetc(quotec)) == EOF)
525 } else if (c == '\\') {
526 if ((next = lgetc(quotec)) == EOF)
528 if (next == quotec || next == ' ' ||
531 else if (next == '\n') {
536 } else if (c == quotec) {
539 } else if (c == '\0') {
540 yyerror("syntax error");
543 if (p + 1 >= buf + sizeof(buf) - 1) {
544 yyerror("string too long");
549 yylval.v.string = strdup(buf);
550 if (yylval.v.string == NULL)
551 fatal("yylex: strdup");
555 #define allowed_to_end_number(x) \
556 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
558 if (c == '-' || isdigit(c)) {
561 if ((size_t)(p-buf) >= sizeof(buf)) {
562 yyerror("string too long");
565 } while ((c = lgetc(0)) != EOF && isdigit(c));
567 if (p == buf + 1 && buf[0] == '-')
569 if (c == EOF || allowed_to_end_number(c)) {
570 const char *errstr = NULL;
573 yylval.v.number = strtonum(buf, LLONG_MIN,
576 yyerror("\"%s\" invalid number: %s",
584 lungetc((unsigned char)*--p);
585 c = (unsigned char)*--p;
591 #define allowed_in_string(x) \
592 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
593 x != '{' && x != '}' && \
594 x != '!' && x != '=' && x != '#' && \
597 if (isalnum(c) || c == ':' || c == '_' || c == '/') {
600 if ((size_t)(p-buf) >= sizeof(buf)) {
601 yyerror("string too long");
604 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
607 if ((token = lookup(buf)) == STRING)
608 if ((yylval.v.string = strdup(buf)) == NULL)
609 fatal("yylex: strdup");
613 yylval.lineno = file->lineno;
622 pushfile(const char *name, int secret)
626 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
627 log_warn("%s", __func__);
630 if ((nfile->name = strdup(name)) == NULL) {
631 log_warn("%s", __func__);
635 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
640 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
641 nfile->ungetsize = 16;
642 nfile->ungetbuf = malloc(nfile->ungetsize);
643 if (nfile->ungetbuf == NULL) {
644 log_warn("%s", __func__);
645 fclose(nfile->stream);
650 TAILQ_INSERT_TAIL(&files, nfile, entry);
659 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
660 prev->errors += file->errors;
662 TAILQ_REMOVE(&files, file, entry);
663 fclose(file->stream);
665 free(file->ungetbuf);
668 return (file ? 0 : EOF);
672 parse_config(const char *filename, struct galileo *env)
674 struct sym *sym, *next;
679 n = strlcpy(conf->sc_conffile, filename, sizeof(conf->sc_conffile));
680 if (n >= sizeof(conf->sc_conffile)) {
681 log_warn("path too long: %s", filename);
685 if ((file = pushfile(filename, 0)) == NULL) {
686 log_warn("failed to open %s", filename);
693 if (TAILQ_EMPTY(&conf->sc_proxies))
694 yyerror("no proxies defined");
695 errors = file->errors;
700 /* Free macros and check which have not been used. */
701 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
703 fprintf(stderr, "warning: macro `%s' not used\n",
708 TAILQ_REMOVE(&symhead, sym, entry);
720 symset(const char *nam, const char *val, int persist)
724 TAILQ_FOREACH(sym, &symhead, entry) {
725 if (strcmp(nam, sym->nam) == 0)
730 if (sym->persist == 1)
735 TAILQ_REMOVE(&symhead, sym, entry);
739 if ((sym = calloc(1, sizeof(*sym))) == NULL)
742 sym->nam = strdup(nam);
743 if (sym->nam == NULL) {
747 sym->val = strdup(val);
748 if (sym->val == NULL) {
754 sym->persist = persist;
755 TAILQ_INSERT_TAIL(&symhead, sym, entry);
760 cmdline_symset(char *s)
765 if ((val = strrchr(s, '=')) == NULL)
767 sym = strndup(s, val - s);
769 fatal("%s: strndup", __func__);
770 ret = symset(sym, val + 1, 1);
777 symget(const char *nam)
781 TAILQ_FOREACH(sym, &symhead, entry) {
782 if (strcmp(nam, sym->nam) == 0) {
791 getservice(const char *n)
797 llval = strtonum(n, 0, UINT16_MAX, &errstr);
799 s = getservbyname(n, "tcp");
801 s = getservbyname(n, "udp");
804 return (ntohs(s->s_port));
807 return ((unsigned short)llval);