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>
52 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
54 TAILQ_ENTRY(file) entry;
64 struct file *pushfile(const char *, int);
68 int yyerror(const char *, ...)
69 __attribute__((__format__ (printf, 1, 2)))
70 __attribute__((__nonnull__ (1)));
71 int kw_cmp(const void *, const void *);
78 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
80 TAILQ_ENTRY(sym) entry;
86 int symset(const char *, const char *, int);
87 char *symget(const char *);
89 int getservice(const char *);
91 static struct galileo *conf = NULL;
92 static struct proxy *pr = NULL;
106 %token CHROOT HOSTNAME NO PORT PREFORK PROXY SOURCE STYLESHEET TLS
107 %token <v.number> NUMBER
108 %token <v.string> STRING
109 %type <v.number> port
110 %type <v.string> string
114 grammar : /* empty */
115 | grammar include '\n'
117 | grammar varset '\n'
120 | grammar error '\n' { file->errors++; }
123 include : INCLUDE string {
126 if ((nfile = pushfile($2, 0)) == NULL) {
127 yyerror("failed to include file %s", $2);
138 varset : STRING '=' STRING {
141 if (isspace((unsigned char)*s)) {
142 yyerror("macro name cannot contain "
149 if (symset($1, $3, 0) == -1)
150 fatalx("cannot store variable");
156 main : PREFORK NUMBER {
157 if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
158 yyerror("invalid number of preforked "
159 "proxies: %"PRId64, $2);
162 conf->sc_prefork = $2;
167 n = strlcpy(conf->sc_chroot, $2,
168 sizeof(conf->sc_chroot));
169 if (n >= sizeof(conf->sc_chroot))
170 yyerror("chroot path too long!");
175 proxy : PROXY STRING {
179 if ((p = calloc(1, sizeof(*p))) == NULL)
182 n = strlcpy(p->pr_conf.host, $2,
183 sizeof(p->pr_conf.host));
184 if (n >= sizeof(p->pr_conf.host)) {
185 yyerror("server name too long");
193 } '{' optnl proxyopts_l '}' {
194 /* check if duplicate */
195 if (proxy_match(conf, pr->pr_conf.host) != NULL)
196 yyerror("duplicate proxy `%s'",
199 TAILQ_INSERT_TAIL(&conf->sc_proxies, pr, pr_entry);
201 if (*pr->pr_conf.proxy_addr == '\0')
202 yyerror("missing source in proxy block `%s'",
209 proxyopts_l : proxyopts_l proxyoptsl nl
213 proxyoptsl : SOURCE STRING proxyport {
216 n = strlcpy(pr->pr_conf.proxy_addr, $2,
217 sizeof(pr->pr_conf.proxy_addr));
218 if (n >= sizeof(pr->pr_conf.proxy_addr))
219 yyerror("proxy source too long!");
221 if (*pr->pr_conf.proxy_name == '\0') {
222 n = strlcpy(pr->pr_conf.proxy_name, $2,
223 sizeof(pr->pr_conf.proxy_name));
224 if (n >= sizeof(pr->pr_conf.proxy_name))
225 yyerror("proxy hostname too long!");
233 n = strlcpy(pr->pr_conf.proxy_name, $2,
234 sizeof(pr->pr_conf.proxy_name));
235 if (n >= sizeof(pr->pr_conf.proxy_name))
236 yyerror("proxy hostname too long!");
239 | STYLESHEET string {
242 n = strlcpy(pr->pr_conf.stylesheet, $2,
243 sizeof(pr->pr_conf.stylesheet));
244 if (n >= sizeof(pr->pr_conf.stylesheet))
245 yyerror("stylesheet path too long!");
249 pr->pr_conf.no_tls = 1;
253 proxyport : /* empty */ {
254 strlcpy(pr->pr_conf.proxy_port, "1965",
255 sizeof(pr->pr_conf.proxy_port));
261 len = sizeof(pr->pr_conf.proxy_port);
262 n = snprintf(pr->pr_conf.proxy_port, len,
264 if (n < 0 || (size_t)n >= len)
265 fatal("port number too long?");
269 if ($1 <= 0 || $1 > (int)USHRT_MAX) {
270 yyerror("invalid port: %"PRId64, $1);
278 if ((val = getservice($1)) == -1) {
279 yyerror("invalid port: %s", $1);
288 string : STRING string {
289 if (asprintf(&$$, "%s%s", $1, $2) == -1)
290 fatal("asprintf string");
312 yyerror(const char *fmt, ...)
319 if (vasprintf(&msg, fmt, ap) == -1)
320 fatal("yyerror vasprintf");
322 log_warnx("%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[] = {
338 { "chroot", CHROOT },
339 { "hostname", HOSTNAME },
340 { "include", INCLUDE },
343 { "prefork", PREFORK },
345 { "source", SOURCE },
346 { "stylesheet", STYLESHEET},
349 const struct keywords *p;
351 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
360 #define START_EXPAND 1
361 #define DONE_EXPAND 2
363 static int expanding;
371 if (file->ungetpos > 0)
372 c = file->ungetbuf[--file->ungetpos];
374 c = getc(file->stream);
376 if (c == START_EXPAND)
378 else if (c == DONE_EXPAND)
392 if ((c = igetc()) == EOF) {
393 yyerror("reached end of file while parsing "
395 if (file == topfile || popfile() == EOF)
402 while ((c = igetc()) == '\\') {
408 yylval.lineno = file->lineno;
411 if (c == '\t' || c == ' ') {
412 /* Compress blanks to a single space. */
414 c = getc(file->stream);
415 } while (c == '\t' || c == ' ');
416 ungetc(c, file->stream);
422 * Fake EOL when hit EOF for the first time. This gets line
423 * count right if last line in included file is syntactically
424 * invalid and has no newline.
426 if (file->eof_reached == 0) {
427 file->eof_reached = 1;
431 if (file == topfile || popfile() == EOF)
445 if (file->ungetpos >= file->ungetsize) {
446 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
448 err(1, "%s", __func__);
450 file->ungetsize *= 2;
452 file->ungetbuf[file->ungetpos++] = c;
460 /* skip to either EOF or the first real EOL */
483 while ((c = lgetc(0)) == ' ' || c == '\t')
486 yylval.lineno = file->lineno;
488 while ((c = lgetc(0)) != '\n' && c != EOF)
490 if (c == '$' && !expanding) {
492 if ((c = lgetc(0)) == EOF)
495 if (p + 1 >= buf + sizeof(buf) - 1) {
496 yyerror("string too long");
499 if (isalnum(c) || c == '_') {
509 yyerror("macro '%s' not defined", buf);
512 p = val + strlen(val) - 1;
513 lungetc(DONE_EXPAND);
515 lungetc((unsigned char)*p);
518 lungetc(START_EXPAND);
527 if ((c = lgetc(quotec)) == EOF)
532 } else if (c == '\\') {
533 if ((next = lgetc(quotec)) == EOF)
535 if (next == quotec || next == ' ' ||
538 else if (next == '\n') {
543 } else if (c == quotec) {
546 } else if (c == '\0') {
547 yyerror("syntax error");
550 if (p + 1 >= buf + sizeof(buf) - 1) {
551 yyerror("string too long");
556 yylval.v.string = strdup(buf);
557 if (yylval.v.string == NULL)
558 fatal("yylex: strdup");
562 #define allowed_to_end_number(x) \
563 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
565 if (c == '-' || isdigit(c)) {
568 if ((size_t)(p-buf) >= sizeof(buf)) {
569 yyerror("string too long");
572 } while ((c = lgetc(0)) != EOF && isdigit(c));
574 if (p == buf + 1 && buf[0] == '-')
576 if (c == EOF || allowed_to_end_number(c)) {
577 const char *errstr = NULL;
580 yylval.v.number = strtonum(buf, LLONG_MIN,
583 yyerror("\"%s\" invalid number: %s",
591 lungetc((unsigned char)*--p);
592 c = (unsigned char)*--p;
598 #define allowed_in_string(x) \
599 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
600 x != '{' && x != '}' && \
601 x != '!' && x != '=' && x != '#' && \
604 if (isalnum(c) || c == ':' || c == '_' || c == '/') {
607 if ((size_t)(p-buf) >= sizeof(buf)) {
608 yyerror("string too long");
611 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
614 if ((token = lookup(buf)) == STRING)
615 if ((yylval.v.string = strdup(buf)) == NULL)
616 fatal("yylex: strdup");
620 yylval.lineno = file->lineno;
629 pushfile(const char *name, int secret)
633 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
634 log_warn("%s", __func__);
637 if ((nfile->name = strdup(name)) == NULL) {
638 log_warn("%s", __func__);
642 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
647 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
648 nfile->ungetsize = 16;
649 nfile->ungetbuf = malloc(nfile->ungetsize);
650 if (nfile->ungetbuf == NULL) {
651 log_warn("%s", __func__);
652 fclose(nfile->stream);
657 TAILQ_INSERT_TAIL(&files, nfile, entry);
666 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
667 prev->errors += file->errors;
669 TAILQ_REMOVE(&files, file, entry);
670 fclose(file->stream);
672 free(file->ungetbuf);
675 return (file ? 0 : EOF);
679 parse_config(const char *filename, struct galileo *env)
681 struct sym *sym, *next;
686 n = strlcpy(conf->sc_conffile, filename, sizeof(conf->sc_conffile));
687 if (n >= sizeof(conf->sc_conffile)) {
688 log_warn("path too long: %s", filename);
692 if ((file = pushfile(filename, 0)) == NULL) {
693 log_warn("failed to open %s", filename);
700 if (TAILQ_EMPTY(&conf->sc_proxies))
701 yyerror("no proxies defined");
702 errors = file->errors;
707 /* Free macros and check which have not been used. */
708 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
710 fprintf(stderr, "warning: macro `%s' not used\n",
715 TAILQ_REMOVE(&symhead, sym, entry);
727 symset(const char *nam, const char *val, int persist)
731 TAILQ_FOREACH(sym, &symhead, entry) {
732 if (strcmp(nam, sym->nam) == 0)
737 if (sym->persist == 1)
742 TAILQ_REMOVE(&symhead, sym, entry);
746 if ((sym = calloc(1, sizeof(*sym))) == NULL)
749 sym->nam = strdup(nam);
750 if (sym->nam == NULL) {
754 sym->val = strdup(val);
755 if (sym->val == NULL) {
761 sym->persist = persist;
762 TAILQ_INSERT_TAIL(&symhead, sym, entry);
767 cmdline_symset(char *s)
772 if ((val = strrchr(s, '=')) == NULL)
774 sym = strndup(s, val - s);
776 fatal("%s: strndup", __func__);
777 ret = symset(sym, val + 1, 1);
784 symget(const char *nam)
788 TAILQ_FOREACH(sym, &symhead, entry) {
789 if (strcmp(nam, sym->nam) == 0) {
798 getservice(const char *n)
804 llval = strtonum(n, 0, UINT16_MAX, &errstr);
806 s = getservbyname(n, "tcp");
808 s = getservbyname(n, "udp");
811 return (ntohs(s->s_port));
814 return ((unsigned short)llval);