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 TAILQ_INSERT_TAIL(&conf->sc_proxies, p, pr_entry);
193 } '{' optnl proxyopts_l '}' {
194 /* check if duplicate */
196 if (*pr->pr_conf.proxy_addr == '\0')
197 yyerror("missing source in proxy block `%s'",
204 proxyopts_l : proxyopts_l proxyoptsl nl
208 proxyoptsl : SOURCE STRING proxyport {
211 n = strlcpy(pr->pr_conf.proxy_addr, $2,
212 sizeof(pr->pr_conf.proxy_addr));
213 if (n >= sizeof(pr->pr_conf.proxy_addr))
214 yyerror("proxy source too long!");
216 if (*pr->pr_conf.proxy_name == '\0') {
217 n = strlcpy(pr->pr_conf.proxy_name, $2,
218 sizeof(pr->pr_conf.proxy_name));
219 if (n >= sizeof(pr->pr_conf.proxy_name))
220 yyerror("proxy hostname too long!");
228 n = strlcpy(pr->pr_conf.proxy_name, $2,
229 sizeof(pr->pr_conf.proxy_name));
230 if (n >= sizeof(pr->pr_conf.proxy_name))
231 yyerror("proxy hostname too long!");
234 | STYLESHEET string {
237 n = strlcpy(pr->pr_conf.stylesheet, $2,
238 sizeof(pr->pr_conf.stylesheet));
239 if (n >= sizeof(pr->pr_conf.stylesheet))
240 yyerror("stylesheet path too long!");
245 proxyport : /* empty */ {
246 strlcpy(pr->pr_conf.proxy_port, "1965",
247 sizeof(pr->pr_conf.proxy_port));
253 len = sizeof(pr->pr_conf.proxy_port);
254 n = snprintf(pr->pr_conf.proxy_port, len, "%lld", $2);
255 if (n < 0 || (size_t)n >= len)
256 fatal("port number too long?");
260 if ($1 <= 0 || $1 > (int)USHRT_MAX) {
261 yyerror("invalid port: %lld", $1);
269 if ((val = getservice($1)) == -1) {
270 yyerror("invalid port: %s", $1);
279 string : STRING string {
280 if (asprintf(&$$, "%s%s", $1, $2) == -1)
281 fatal("asprintf string");
303 yyerror(const char *fmt, ...)
310 if (vasprintf(&msg, fmt, ap) == -1)
311 fatal("yyerror vasprintf");
313 log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
319 kw_cmp(const void *k, const void *e)
321 return (strcmp(k, ((const struct keywords *)e)->k_name));
327 /* this has to be sorted always */
328 static const struct keywords keywords[] = {
329 { "chroot", CHROOT },
330 { "hostname", HOSTNAME },
331 { "include", INCLUDE },
333 { "prefork", PREFORK },
335 { "source", SOURCE },
336 { "stylesheet", STYLESHEET},
338 const struct keywords *p;
340 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
349 #define START_EXPAND 1
350 #define DONE_EXPAND 2
352 static int expanding;
360 if (file->ungetpos > 0)
361 c = file->ungetbuf[--file->ungetpos];
363 c = getc(file->stream);
365 if (c == START_EXPAND)
367 else if (c == DONE_EXPAND)
381 if ((c = igetc()) == EOF) {
382 yyerror("reached end of file while parsing "
384 if (file == topfile || popfile() == EOF)
391 while ((c = igetc()) == '\\') {
397 yylval.lineno = file->lineno;
400 if (c == '\t' || c == ' ') {
401 /* Compress blanks to a single space. */
403 c = getc(file->stream);
404 } while (c == '\t' || c == ' ');
405 ungetc(c, file->stream);
411 * Fake EOL when hit EOF for the first time. This gets line
412 * count right if last line in included file is syntactically
413 * invalid and has no newline.
415 if (file->eof_reached == 0) {
416 file->eof_reached = 1;
420 if (file == topfile || popfile() == EOF)
434 if (file->ungetpos >= file->ungetsize) {
435 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
437 err(1, "%s", __func__);
439 file->ungetsize *= 2;
441 file->ungetbuf[file->ungetpos++] = c;
449 /* skip to either EOF or the first real EOL */
472 while ((c = lgetc(0)) == ' ' || c == '\t')
475 yylval.lineno = file->lineno;
477 while ((c = lgetc(0)) != '\n' && c != EOF)
479 if (c == '$' && !expanding) {
481 if ((c = lgetc(0)) == EOF)
484 if (p + 1 >= buf + sizeof(buf) - 1) {
485 yyerror("string too long");
488 if (isalnum(c) || c == '_') {
498 yyerror("macro '%s' not defined", buf);
501 p = val + strlen(val) - 1;
502 lungetc(DONE_EXPAND);
504 lungetc((unsigned char)*p);
507 lungetc(START_EXPAND);
516 if ((c = lgetc(quotec)) == EOF)
521 } else if (c == '\\') {
522 if ((next = lgetc(quotec)) == EOF)
524 if (next == quotec || next == ' ' ||
527 else if (next == '\n') {
532 } else if (c == quotec) {
535 } else if (c == '\0') {
536 yyerror("syntax error");
539 if (p + 1 >= buf + sizeof(buf) - 1) {
540 yyerror("string too long");
545 yylval.v.string = strdup(buf);
546 if (yylval.v.string == NULL)
547 fatal("yylex: strdup");
551 #define allowed_to_end_number(x) \
552 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
554 if (c == '-' || isdigit(c)) {
557 if ((size_t)(p-buf) >= sizeof(buf)) {
558 yyerror("string too long");
561 } while ((c = lgetc(0)) != EOF && isdigit(c));
563 if (p == buf + 1 && buf[0] == '-')
565 if (c == EOF || allowed_to_end_number(c)) {
566 const char *errstr = NULL;
569 yylval.v.number = strtonum(buf, LLONG_MIN,
572 yyerror("\"%s\" invalid number: %s",
580 lungetc((unsigned char)*--p);
581 c = (unsigned char)*--p;
587 #define allowed_in_string(x) \
588 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
589 x != '{' && x != '}' && \
590 x != '!' && x != '=' && x != '#' && \
593 if (isalnum(c) || c == ':' || c == '_' || c == '/') {
596 if ((size_t)(p-buf) >= sizeof(buf)) {
597 yyerror("string too long");
600 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
603 if ((token = lookup(buf)) == STRING)
604 if ((yylval.v.string = strdup(buf)) == NULL)
605 fatal("yylex: strdup");
609 yylval.lineno = file->lineno;
618 pushfile(const char *name, int secret)
622 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
623 log_warn("%s", __func__);
626 if ((nfile->name = strdup(name)) == NULL) {
627 log_warn("%s", __func__);
631 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
636 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
637 nfile->ungetsize = 16;
638 nfile->ungetbuf = malloc(nfile->ungetsize);
639 if (nfile->ungetbuf == NULL) {
640 log_warn("%s", __func__);
641 fclose(nfile->stream);
646 TAILQ_INSERT_TAIL(&files, nfile, entry);
655 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
656 prev->errors += file->errors;
658 TAILQ_REMOVE(&files, file, entry);
659 fclose(file->stream);
661 free(file->ungetbuf);
664 return (file ? 0 : EOF);
668 parse_config(const char *filename, struct galileo *env)
670 struct sym *sym, *next;
675 n = strlcpy(conf->sc_conffile, filename, sizeof(conf->sc_conffile));
676 if (n >= sizeof(conf->sc_conffile)) {
677 log_warn("path too long: %s", filename);
681 if ((file = pushfile(filename, 0)) == NULL) {
682 log_warn("failed to open %s", filename);
689 errors = file->errors;
694 /* Free macros and check which have not been used. */
695 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
697 fprintf(stderr, "warning: macro `%s' not used\n",
702 TAILQ_REMOVE(&symhead, sym, entry);
714 symset(const char *nam, const char *val, int persist)
718 TAILQ_FOREACH(sym, &symhead, entry) {
719 if (strcmp(nam, sym->nam) == 0)
724 if (sym->persist == 1)
729 TAILQ_REMOVE(&symhead, sym, entry);
733 if ((sym = calloc(1, sizeof(*sym))) == NULL)
736 sym->nam = strdup(nam);
737 if (sym->nam == NULL) {
741 sym->val = strdup(val);
742 if (sym->val == NULL) {
748 sym->persist = persist;
749 TAILQ_INSERT_TAIL(&symhead, sym, entry);
754 cmdline_symset(char *s)
759 if ((val = strrchr(s, '=')) == NULL)
761 sym = strndup(s, val - s);
763 fatal("%s: strndup", __func__);
764 ret = symset(sym, val + 1, 1);
771 symget(const char *nam)
775 TAILQ_FOREACH(sym, &symhead, entry) {
776 if (strcmp(nam, sym->nam) == 0) {
785 getservice(const char *n)
791 llval = strtonum(n, 0, UINT16_MAX, &errstr);
793 s = getservbyname(n, "tcp");
795 s = getservbyname(n, "udp");
798 return (ntohs(s->s_port));
801 return ((unsigned short)llval);