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 BAR CHROOT FOOTER HOSTNAME IMAGE NAVIGATION NO PORT
107 %token PREFORK PREVIEW PROXY SOURCE STYLESHEET TLS
108 %token <v.number> NUMBER
109 %token <v.string> STRING
110 %type <v.number> port
111 %type <v.string> string
115 grammar : /* empty */
116 | grammar include '\n'
118 | grammar varset '\n'
121 | grammar error '\n' { file->errors++; }
124 include : INCLUDE string {
127 if ((nfile = pushfile($2, 0)) == NULL) {
128 yyerror("failed to include file %s", $2);
139 varset : STRING '=' STRING {
142 if (isspace((unsigned char)*s)) {
143 yyerror("macro name cannot contain "
150 if (symset($1, $3, 0) == -1)
151 fatalx("cannot store variable");
157 main : PREFORK NUMBER {
158 if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
159 yyerror("invalid number of preforked "
160 "proxies: %"PRId64, $2);
163 conf->sc_prefork = $2;
168 n = strlcpy(conf->sc_chroot, $2,
169 sizeof(conf->sc_chroot));
170 if (n >= sizeof(conf->sc_chroot))
171 yyerror("chroot path too long!");
176 proxy : PROXY STRING {
180 if ((p = calloc(1, sizeof(*p))) == NULL)
183 n = strlcpy(p->pr_conf.host, $2,
184 sizeof(p->pr_conf.host));
185 if (n >= sizeof(p->pr_conf.host)) {
186 yyerror("server name too long");
194 } '{' optnl proxyopts_l '}' {
195 /* check if duplicate */
196 if (proxy_match(conf, pr->pr_conf.host) != NULL)
197 yyerror("duplicate proxy `%s'",
200 TAILQ_INSERT_TAIL(&conf->sc_proxies, pr, pr_entry);
202 if (*pr->pr_conf.proxy_addr == '\0')
203 yyerror("missing source in proxy block `%s'",
210 proxyopts_l : proxyopts_l proxyoptsl nl
214 proxyoptsl : SOURCE STRING proxyport {
217 n = strlcpy(pr->pr_conf.proxy_addr, $2,
218 sizeof(pr->pr_conf.proxy_addr));
219 if (n >= sizeof(pr->pr_conf.proxy_addr))
220 yyerror("proxy source too long!");
222 if (*pr->pr_conf.proxy_name == '\0') {
223 n = strlcpy(pr->pr_conf.proxy_name, $2,
224 sizeof(pr->pr_conf.proxy_name));
225 if (n >= sizeof(pr->pr_conf.proxy_name))
226 yyerror("proxy hostname too long!");
234 n = strlcpy(pr->pr_conf.proxy_name, $2,
235 sizeof(pr->pr_conf.proxy_name));
236 if (n >= sizeof(pr->pr_conf.proxy_name))
237 yyerror("proxy hostname too long!");
240 | STYLESHEET string {
243 n = strlcpy(pr->pr_conf.stylesheet, $2,
244 sizeof(pr->pr_conf.stylesheet));
245 if (n >= sizeof(pr->pr_conf.stylesheet))
246 yyerror("stylesheet path too long!");
250 pr->pr_conf.flags |= PROXY_NO_FOOTER;
253 pr->pr_conf.flags |= PROXY_NO_IMGPRV;
255 | NO NAVIGATION BAR {
256 pr->pr_conf.flags |= PROXY_NO_NAVBAR;
259 pr->pr_conf.flags |= PROXY_NO_TLS;
263 proxyport : /* empty */ {
264 strlcpy(pr->pr_conf.proxy_port, "1965",
265 sizeof(pr->pr_conf.proxy_port));
271 len = sizeof(pr->pr_conf.proxy_port);
272 n = snprintf(pr->pr_conf.proxy_port, len,
274 if (n < 0 || (size_t)n >= len)
275 fatal("port number too long?");
279 if ($1 <= 0 || $1 > (int)USHRT_MAX) {
280 yyerror("invalid port: %"PRId64, $1);
288 if ((val = getservice($1)) == -1) {
289 yyerror("invalid port: %s", $1);
298 string : STRING string {
299 if (asprintf(&$$, "%s%s", $1, $2) == -1)
300 fatal("asprintf string");
322 yyerror(const char *fmt, ...)
329 if (vasprintf(&msg, fmt, ap) == -1)
330 fatal("yyerror vasprintf");
332 log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
338 kw_cmp(const void *k, const void *e)
340 return (strcmp(k, ((const struct keywords *)e)->k_name));
346 /* this has to be sorted always */
347 static const struct keywords keywords[] = {
349 { "chroot", CHROOT },
350 { "footer", FOOTER },
351 { "hostname", HOSTNAME },
353 { "include", INCLUDE },
354 { "navigation", NAVIGATION },
357 { "prefork", PREFORK },
358 { "preview", PREVIEW },
360 { "source", SOURCE },
361 { "stylesheet", STYLESHEET},
364 const struct keywords *p;
366 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
375 #define START_EXPAND 1
376 #define DONE_EXPAND 2
378 static int expanding;
386 if (file->ungetpos > 0)
387 c = file->ungetbuf[--file->ungetpos];
389 c = getc(file->stream);
391 if (c == START_EXPAND)
393 else if (c == DONE_EXPAND)
407 if ((c = igetc()) == EOF) {
408 yyerror("reached end of file while parsing "
410 if (file == topfile || popfile() == EOF)
417 while ((c = igetc()) == '\\') {
423 yylval.lineno = file->lineno;
426 if (c == '\t' || c == ' ') {
427 /* Compress blanks to a single space. */
429 c = getc(file->stream);
430 } while (c == '\t' || c == ' ');
431 ungetc(c, file->stream);
437 * Fake EOL when hit EOF for the first time. This gets line
438 * count right if last line in included file is syntactically
439 * invalid and has no newline.
441 if (file->eof_reached == 0) {
442 file->eof_reached = 1;
446 if (file == topfile || popfile() == EOF)
460 if (file->ungetpos >= file->ungetsize) {
461 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
463 err(1, "%s", __func__);
465 file->ungetsize *= 2;
467 file->ungetbuf[file->ungetpos++] = c;
475 /* skip to either EOF or the first real EOL */
498 while ((c = lgetc(0)) == ' ' || c == '\t')
501 yylval.lineno = file->lineno;
503 while ((c = lgetc(0)) != '\n' && c != EOF)
505 if (c == '$' && !expanding) {
507 if ((c = lgetc(0)) == EOF)
510 if (p + 1 >= buf + sizeof(buf) - 1) {
511 yyerror("string too long");
514 if (isalnum(c) || c == '_') {
524 yyerror("macro '%s' not defined", buf);
527 p = val + strlen(val) - 1;
528 lungetc(DONE_EXPAND);
530 lungetc((unsigned char)*p);
533 lungetc(START_EXPAND);
542 if ((c = lgetc(quotec)) == EOF)
547 } else if (c == '\\') {
548 if ((next = lgetc(quotec)) == EOF)
550 if (next == quotec || next == ' ' ||
553 else if (next == '\n') {
558 } else if (c == quotec) {
561 } else if (c == '\0') {
562 yyerror("syntax error");
565 if (p + 1 >= buf + sizeof(buf) - 1) {
566 yyerror("string too long");
571 yylval.v.string = strdup(buf);
572 if (yylval.v.string == NULL)
573 fatal("yylex: strdup");
577 #define allowed_to_end_number(x) \
578 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
580 if (c == '-' || isdigit(c)) {
583 if ((size_t)(p-buf) >= sizeof(buf)) {
584 yyerror("string too long");
587 } while ((c = lgetc(0)) != EOF && isdigit(c));
589 if (p == buf + 1 && buf[0] == '-')
591 if (c == EOF || allowed_to_end_number(c)) {
592 const char *errstr = NULL;
595 yylval.v.number = strtonum(buf, LLONG_MIN,
598 yyerror("\"%s\" invalid number: %s",
606 lungetc((unsigned char)*--p);
607 c = (unsigned char)*--p;
613 #define allowed_in_string(x) \
614 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
615 x != '{' && x != '}' && \
616 x != '!' && x != '=' && x != '#' && \
619 if (isalnum(c) || c == ':' || c == '_' || c == '/') {
622 if ((size_t)(p-buf) >= sizeof(buf)) {
623 yyerror("string too long");
626 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
629 if ((token = lookup(buf)) == STRING)
630 if ((yylval.v.string = strdup(buf)) == NULL)
631 fatal("yylex: strdup");
635 yylval.lineno = file->lineno;
644 pushfile(const char *name, int secret)
648 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
649 log_warn("%s", __func__);
652 if ((nfile->name = strdup(name)) == NULL) {
653 log_warn("%s", __func__);
657 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
662 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
663 nfile->ungetsize = 16;
664 nfile->ungetbuf = malloc(nfile->ungetsize);
665 if (nfile->ungetbuf == NULL) {
666 log_warn("%s", __func__);
667 fclose(nfile->stream);
672 TAILQ_INSERT_TAIL(&files, nfile, entry);
681 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
682 prev->errors += file->errors;
684 TAILQ_REMOVE(&files, file, entry);
685 fclose(file->stream);
687 free(file->ungetbuf);
690 return (file ? 0 : EOF);
694 parse_config(const char *filename, struct galileo *env)
696 struct sym *sym, *next;
701 n = strlcpy(conf->sc_conffile, filename, sizeof(conf->sc_conffile));
702 if (n >= sizeof(conf->sc_conffile)) {
703 log_warn("path too long: %s", filename);
707 if ((file = pushfile(filename, 0)) == NULL) {
708 log_warn("failed to open %s", filename);
715 if (TAILQ_EMPTY(&conf->sc_proxies))
716 yyerror("no proxies defined");
717 errors = file->errors;
722 /* Free macros and check which have not been used. */
723 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
725 fprintf(stderr, "warning: macro `%s' not used\n",
730 TAILQ_REMOVE(&symhead, sym, entry);
742 symset(const char *nam, const char *val, int persist)
746 TAILQ_FOREACH(sym, &symhead, entry) {
747 if (strcmp(nam, sym->nam) == 0)
752 if (sym->persist == 1)
757 TAILQ_REMOVE(&symhead, sym, entry);
761 if ((sym = calloc(1, sizeof(*sym))) == NULL)
764 sym->nam = strdup(nam);
765 if (sym->nam == NULL) {
769 sym->val = strdup(val);
770 if (sym->val == NULL) {
776 sym->persist = persist;
777 TAILQ_INSERT_TAIL(&symhead, sym, entry);
782 cmdline_symset(char *s)
787 if ((val = strrchr(s, '=')) == NULL)
789 sym = strndup(s, val - s);
791 fatal("%s: strndup", __func__);
792 ret = symset(sym, val + 1, 1);
799 symget(const char *nam)
803 TAILQ_FOREACH(sym, &symhead, entry) {
804 if (strcmp(nam, sym->nam) == 0) {
813 getservice(const char *n)
819 llval = strtonum(n, 0, UINT16_MAX, &errstr);
821 s = getservbyname(n, "tcp");
823 s = getservbyname(n, "udp");
826 return (ntohs(s->s_port));
829 return ((unsigned short)llval);