Blob


1 /*
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.
10 *
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.
14 *
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.
22 */
24 %{
25 #include <sys/types.h>
26 #include <sys/queue.h>
27 #include <sys/tree.h>
28 #include <sys/uio.h>
30 #include <err.h>
31 #include <event.h>
32 #include <netdb.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <limits.h>
36 #include <stdarg.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <pwd.h>
44 #include <imsg.h>
46 #include "log.h"
47 #include "proc.h"
49 #include "galileo.h"
51 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
52 static struct file {
53 TAILQ_ENTRY(file) entry;
54 FILE *stream;
55 char *name;
56 size_t ungetpos;
57 size_t ungetsize;
58 u_char *ungetbuf;
59 int eof_reached;
60 int lineno;
61 int errors;
62 } *file, *topfile;
63 struct file *pushfile(const char *, int);
64 int popfile(void);
65 int yyparse(void);
66 int yylex(void);
67 int yyerror(const char *, ...)
68 __attribute__((__format__ (printf, 1, 2)))
69 __attribute__((__nonnull__ (1)));
70 int kw_cmp(const void *, const void *);
71 int lookup(char *);
72 int igetc(void);
73 int lgetc(int);
74 void lungetc(int);
75 int findeol(void);
77 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
78 struct sym {
79 TAILQ_ENTRY(sym) entry;
80 int used;
81 int persist;
82 char *nam;
83 char *val;
84 };
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;
92 static int errors;
94 typedef struct {
95 union {
96 int64_t number;
97 char *string;
98 } v;
99 int lineno;
100 } YYSTYPE;
102 %}
104 %token INCLUDE ERROR
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
111 %%
113 grammar : /* empty */
114 | grammar include '\n'
115 | grammar '\n'
116 | grammar varset '\n'
117 | grammar main '\n'
118 | grammar proxy '\n'
119 | grammar error '\n' { file->errors++; }
122 include : INCLUDE string {
123 struct file *nfile;
125 if ((nfile = pushfile($2, 0)) == NULL) {
126 yyerror("failed to include file %s", $2);
127 free($2);
128 YYERROR;
130 free($2);
132 file = nfile;
133 lungetc('\n');
137 varset : STRING '=' STRING {
138 char *s = $1;
139 while (*s++) {
140 if (isspace((unsigned char)*s)) {
141 yyerror("macro name cannot contain "
142 "whitespace");
143 free($1);
144 free($3);
145 YYERROR;
148 if (symset($1, $3, 0) == -1)
149 fatalx("cannot store variable");
150 free($1);
151 free($3);
155 main : PREFORK NUMBER {
156 if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
157 yyerror("invalid number of preforked "
158 "proxies: %lld", $2);
159 YYERROR;
161 conf->sc_prefork = $2;
163 | CHROOT STRING {
164 size_t n;
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!");
170 free($2);
174 proxy : PROXY STRING {
175 struct proxy *p;
176 size_t n;
178 if ((p = calloc(1, sizeof(*p))) == NULL)
179 fatal("calloc");
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");
185 free($2);
186 free(p);
187 YYERROR;
189 free($2);
191 pr = p;
192 } '{' optnl proxyopts_l '}' {
193 /* check if duplicate */
194 if (proxy_match(conf, pr->pr_conf.host) != NULL)
195 yyerror("duplicate proxy `%s'",
196 pr->pr_conf.host);
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'",
202 pr->pr_conf.host);
204 pr = NULL;
208 proxyopts_l : proxyopts_l proxyoptsl nl
209 | proxyoptsl optnl
212 proxyoptsl : SOURCE STRING proxyport {
213 size_t n;
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!");
227 free($2);
229 | HOSTNAME STRING {
230 size_t n;
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!");
236 free($2);
238 | STYLESHEET string {
239 size_t n;
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!");
245 free($2);
249 proxyport : /* empty */ {
250 strlcpy(pr->pr_conf.proxy_port, "1965",
251 sizeof(pr->pr_conf.proxy_port));
253 | PORT port {
254 size_t len;
255 int n;
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?");
261 };
263 port : NUMBER {
264 if ($1 <= 0 || $1 > (int)USHRT_MAX) {
265 yyerror("invalid port: %lld", $1);
266 YYERROR;
268 $$ = $1;
270 | STRING {
271 int val;
273 if ((val = getservice($1)) == -1) {
274 yyerror("invalid port: %s", $1);
275 free($1);
276 YYERROR;
278 free($1);
279 $$ = val;
283 string : STRING string {
284 if (asprintf(&$$, "%s%s", $1, $2) == -1)
285 fatal("asprintf string");
286 free($1);
287 free($2);
289 | STRING
292 optnl : '\n' optnl
296 nl : '\n' optnl
299 %%
301 struct keywords {
302 const char *k_name;
303 int k_val;
304 };
306 int
307 yyerror(const char *fmt, ...)
309 va_list ap;
310 char *msg;
312 file->errors++;
313 va_start(ap, fmt);
314 if (vasprintf(&msg, fmt, ap) == -1)
315 fatal("yyerror vasprintf");
316 va_end(ap);
317 log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
318 free(msg);
319 return (0);
322 int
323 kw_cmp(const void *k, const void *e)
325 return (strcmp(k, ((const struct keywords *)e)->k_name));
328 int
329 lookup(char *s)
331 /* this has to be sorted always */
332 static const struct keywords keywords[] = {
333 { "chroot", CHROOT },
334 { "hostname", HOSTNAME },
335 { "include", INCLUDE },
336 { "port", PORT },
337 { "prefork", PREFORK },
338 { "proxy", PROXY },
339 { "source", SOURCE },
340 { "stylesheet", STYLESHEET},
341 };
342 const struct keywords *p;
344 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
345 kw_cmp);
347 if (p)
348 return (p->k_val);
349 else
350 return (STRING);
353 #define START_EXPAND 1
354 #define DONE_EXPAND 2
356 static int expanding;
358 int
359 igetc(void)
361 int c;
363 while (1) {
364 if (file->ungetpos > 0)
365 c = file->ungetbuf[--file->ungetpos];
366 else
367 c = getc(file->stream);
369 if (c == START_EXPAND)
370 expanding = 1;
371 else if (c == DONE_EXPAND)
372 expanding = 0;
373 else
374 break;
376 return (c);
379 int
380 lgetc(int quotec)
382 int c, next;
384 if (quotec) {
385 if ((c = igetc()) == EOF) {
386 yyerror("reached end of file while parsing "
387 "quoted string");
388 if (file == topfile || popfile() == EOF)
389 return (EOF);
390 return (quotec);
392 return (c);
395 while ((c = igetc()) == '\\') {
396 next = igetc();
397 if (next != '\n') {
398 c = next;
399 break;
401 yylval.lineno = file->lineno;
402 file->lineno++;
404 if (c == '\t' || c == ' ') {
405 /* Compress blanks to a single space. */
406 do {
407 c = getc(file->stream);
408 } while (c == '\t' || c == ' ');
409 ungetc(c, file->stream);
410 c = ' ';
413 if (c == EOF) {
414 /*
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.
418 */
419 if (file->eof_reached == 0) {
420 file->eof_reached = 1;
421 return ('\n');
423 while (c == EOF) {
424 if (file == topfile || popfile() == EOF)
425 return (EOF);
426 c = igetc();
429 return (c);
432 void
433 lungetc(int c)
435 if (c == EOF)
436 return;
438 if (file->ungetpos >= file->ungetsize) {
439 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
440 if (p == NULL)
441 err(1, "%s", __func__);
442 file->ungetbuf = p;
443 file->ungetsize *= 2;
445 file->ungetbuf[file->ungetpos++] = c;
448 int
449 findeol(void)
451 int c;
453 /* skip to either EOF or the first real EOL */
454 while (1) {
455 c = lgetc(0);
456 if (c == '\n') {
457 file->lineno++;
458 break;
460 if (c == EOF)
461 break;
463 return (ERROR);
466 int
467 yylex(void)
469 char buf[8096];
470 char *p, *val;
471 int quotec, next, c;
472 int token;
474 top:
475 p = buf;
476 while ((c = lgetc(0)) == ' ' || c == '\t')
477 ; /* nothing */
479 yylval.lineno = file->lineno;
480 if (c == '#')
481 while ((c = lgetc(0)) != '\n' && c != EOF)
482 ; /* nothing */
483 if (c == '$' && !expanding) {
484 while (1) {
485 if ((c = lgetc(0)) == EOF)
486 return (0);
488 if (p + 1 >= buf + sizeof(buf) - 1) {
489 yyerror("string too long");
490 return (findeol());
492 if (isalnum(c) || c == '_') {
493 *p++ = c;
494 continue;
496 *p = '\0';
497 lungetc(c);
498 break;
500 val = symget(buf);
501 if (val == NULL) {
502 yyerror("macro '%s' not defined", buf);
503 return (findeol());
505 p = val + strlen(val) - 1;
506 lungetc(DONE_EXPAND);
507 while (p >= val) {
508 lungetc((unsigned char)*p);
509 p--;
511 lungetc(START_EXPAND);
512 goto top;
515 switch (c) {
516 case '\'':
517 case '"':
518 quotec = c;
519 while (1) {
520 if ((c = lgetc(quotec)) == EOF)
521 return (0);
522 if (c == '\n') {
523 file->lineno++;
524 continue;
525 } else if (c == '\\') {
526 if ((next = lgetc(quotec)) == EOF)
527 return (0);
528 if (next == quotec || next == ' ' ||
529 next == '\t')
530 c = next;
531 else if (next == '\n') {
532 file->lineno++;
533 continue;
534 } else
535 lungetc(next);
536 } else if (c == quotec) {
537 *p = '\0';
538 break;
539 } else if (c == '\0') {
540 yyerror("syntax error");
541 return (findeol());
543 if (p + 1 >= buf + sizeof(buf) - 1) {
544 yyerror("string too long");
545 return (findeol());
547 *p++ = c;
549 yylval.v.string = strdup(buf);
550 if (yylval.v.string == NULL)
551 fatal("yylex: strdup");
552 return (STRING);
555 #define allowed_to_end_number(x) \
556 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
558 if (c == '-' || isdigit(c)) {
559 do {
560 *p++ = c;
561 if ((size_t)(p-buf) >= sizeof(buf)) {
562 yyerror("string too long");
563 return (findeol());
565 } while ((c = lgetc(0)) != EOF && isdigit(c));
566 lungetc(c);
567 if (p == buf + 1 && buf[0] == '-')
568 goto nodigits;
569 if (c == EOF || allowed_to_end_number(c)) {
570 const char *errstr = NULL;
572 *p = '\0';
573 yylval.v.number = strtonum(buf, LLONG_MIN,
574 LLONG_MAX, &errstr);
575 if (errstr) {
576 yyerror("\"%s\" invalid number: %s",
577 buf, errstr);
578 return (findeol());
580 return (NUMBER);
581 } else {
582 nodigits:
583 while (p > buf + 1)
584 lungetc((unsigned char)*--p);
585 c = (unsigned char)*--p;
586 if (c == '-')
587 return (c);
591 #define allowed_in_string(x) \
592 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
593 x != '{' && x != '}' && \
594 x != '!' && x != '=' && x != '#' && \
595 x != ','))
597 if (isalnum(c) || c == ':' || c == '_' || c == '/') {
598 do {
599 *p++ = c;
600 if ((size_t)(p-buf) >= sizeof(buf)) {
601 yyerror("string too long");
602 return (findeol());
604 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
605 lungetc(c);
606 *p = '\0';
607 if ((token = lookup(buf)) == STRING)
608 if ((yylval.v.string = strdup(buf)) == NULL)
609 fatal("yylex: strdup");
610 return (token);
612 if (c == '\n') {
613 yylval.lineno = file->lineno;
614 file->lineno++;
616 if (c == EOF)
617 return (0);
618 return (c);
621 struct file *
622 pushfile(const char *name, int secret)
624 struct file *nfile;
626 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
627 log_warn("%s", __func__);
628 return (NULL);
630 if ((nfile->name = strdup(name)) == NULL) {
631 log_warn("%s", __func__);
632 free(nfile);
633 return (NULL);
635 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
636 free(nfile->name);
637 free(nfile);
638 return (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);
646 free(nfile->name);
647 free(nfile);
648 return (NULL);
650 TAILQ_INSERT_TAIL(&files, nfile, entry);
651 return (nfile);
654 int
655 popfile(void)
657 struct file *prev;
659 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
660 prev->errors += file->errors;
662 TAILQ_REMOVE(&files, file, entry);
663 fclose(file->stream);
664 free(file->name);
665 free(file->ungetbuf);
666 free(file);
667 file = prev;
668 return (file ? 0 : EOF);
671 int
672 parse_config(const char *filename, struct galileo *env)
674 struct sym *sym, *next;
675 size_t n;
677 conf = env;
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);
682 return (-1);
685 if ((file = pushfile(filename, 0)) == NULL) {
686 log_warn("failed to open %s", filename);
687 return (-1);
689 topfile = file;
690 setservent(1);
692 yyparse();
693 if (TAILQ_EMPTY(&conf->sc_proxies))
694 yyerror("no proxies defined");
695 errors = file->errors;
696 popfile();
698 endservent();
700 /* Free macros and check which have not been used. */
701 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
702 if (!sym->used)
703 fprintf(stderr, "warning: macro `%s' not used\n",
704 sym->nam);
705 if (!sym->persist) {
706 free(sym->nam);
707 free(sym->val);
708 TAILQ_REMOVE(&symhead, sym, entry);
709 free(sym);
713 if (errors)
714 return (-1);
716 return (0);
719 int
720 symset(const char *nam, const char *val, int persist)
722 struct sym *sym;
724 TAILQ_FOREACH(sym, &symhead, entry) {
725 if (strcmp(nam, sym->nam) == 0)
726 break;
729 if (sym != NULL) {
730 if (sym->persist == 1)
731 return (0);
732 else {
733 free(sym->nam);
734 free(sym->val);
735 TAILQ_REMOVE(&symhead, sym, entry);
736 free(sym);
739 if ((sym = calloc(1, sizeof(*sym))) == NULL)
740 return (-1);
742 sym->nam = strdup(nam);
743 if (sym->nam == NULL) {
744 free(sym);
745 return (-1);
747 sym->val = strdup(val);
748 if (sym->val == NULL) {
749 free(sym->nam);
750 free(sym);
751 return (-1);
753 sym->used = 0;
754 sym->persist = persist;
755 TAILQ_INSERT_TAIL(&symhead, sym, entry);
756 return (0);
759 int
760 cmdline_symset(char *s)
762 char *sym, *val;
763 int ret;
765 if ((val = strrchr(s, '=')) == NULL)
766 return (-1);
767 sym = strndup(s, val - s);
768 if (sym == NULL)
769 fatal("%s: strndup", __func__);
770 ret = symset(sym, val + 1, 1);
771 free(sym);
773 return (ret);
776 char *
777 symget(const char *nam)
779 struct sym *sym;
781 TAILQ_FOREACH(sym, &symhead, entry) {
782 if (strcmp(nam, sym->nam) == 0) {
783 sym->used = 1;
784 return (sym->val);
787 return (NULL);
790 int
791 getservice(const char *n)
793 struct servent *s;
794 const char *errstr;
795 long long llval;
797 llval = strtonum(n, 0, UINT16_MAX, &errstr);
798 if (errstr) {
799 s = getservbyname(n, "tcp");
800 if (s == NULL)
801 s = getservbyname(n, "udp");
802 if (s == NULL)
803 return (-1);
804 return (ntohs(s->s_port));
807 return ((unsigned short)llval);