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 NO PORT PREFORK PROXY SOURCE STYLESHEET TLS
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);
247 | NO TLS {
248 pr->pr_conf.no_tls = 1;
252 proxyport : /* empty */ {
253 strlcpy(pr->pr_conf.proxy_port, "1965",
254 sizeof(pr->pr_conf.proxy_port));
256 | PORT port {
257 size_t len;
258 int n;
260 len = sizeof(pr->pr_conf.proxy_port);
261 n = snprintf(pr->pr_conf.proxy_port, len, "%lld", $2);
262 if (n < 0 || (size_t)n >= len)
263 fatal("port number too long?");
264 };
266 port : NUMBER {
267 if ($1 <= 0 || $1 > (int)USHRT_MAX) {
268 yyerror("invalid port: %lld", $1);
269 YYERROR;
271 $$ = $1;
273 | STRING {
274 int val;
276 if ((val = getservice($1)) == -1) {
277 yyerror("invalid port: %s", $1);
278 free($1);
279 YYERROR;
281 free($1);
282 $$ = val;
286 string : STRING string {
287 if (asprintf(&$$, "%s%s", $1, $2) == -1)
288 fatal("asprintf string");
289 free($1);
290 free($2);
292 | STRING
295 optnl : '\n' optnl
299 nl : '\n' optnl
302 %%
304 struct keywords {
305 const char *k_name;
306 int k_val;
307 };
309 int
310 yyerror(const char *fmt, ...)
312 va_list ap;
313 char *msg;
315 file->errors++;
316 va_start(ap, fmt);
317 if (vasprintf(&msg, fmt, ap) == -1)
318 fatal("yyerror vasprintf");
319 va_end(ap);
320 log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
321 free(msg);
322 return (0);
325 int
326 kw_cmp(const void *k, const void *e)
328 return (strcmp(k, ((const struct keywords *)e)->k_name));
331 int
332 lookup(char *s)
334 /* this has to be sorted always */
335 static const struct keywords keywords[] = {
336 { "chroot", CHROOT },
337 { "hostname", HOSTNAME },
338 { "include", INCLUDE },
339 { "no", NO },
340 { "port", PORT },
341 { "prefork", PREFORK },
342 { "proxy", PROXY },
343 { "source", SOURCE },
344 { "stylesheet", STYLESHEET},
345 { "tls", TLS },
346 };
347 const struct keywords *p;
349 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
350 kw_cmp);
352 if (p)
353 return (p->k_val);
354 else
355 return (STRING);
358 #define START_EXPAND 1
359 #define DONE_EXPAND 2
361 static int expanding;
363 int
364 igetc(void)
366 int c;
368 while (1) {
369 if (file->ungetpos > 0)
370 c = file->ungetbuf[--file->ungetpos];
371 else
372 c = getc(file->stream);
374 if (c == START_EXPAND)
375 expanding = 1;
376 else if (c == DONE_EXPAND)
377 expanding = 0;
378 else
379 break;
381 return (c);
384 int
385 lgetc(int quotec)
387 int c, next;
389 if (quotec) {
390 if ((c = igetc()) == EOF) {
391 yyerror("reached end of file while parsing "
392 "quoted string");
393 if (file == topfile || popfile() == EOF)
394 return (EOF);
395 return (quotec);
397 return (c);
400 while ((c = igetc()) == '\\') {
401 next = igetc();
402 if (next != '\n') {
403 c = next;
404 break;
406 yylval.lineno = file->lineno;
407 file->lineno++;
409 if (c == '\t' || c == ' ') {
410 /* Compress blanks to a single space. */
411 do {
412 c = getc(file->stream);
413 } while (c == '\t' || c == ' ');
414 ungetc(c, file->stream);
415 c = ' ';
418 if (c == EOF) {
419 /*
420 * Fake EOL when hit EOF for the first time. This gets line
421 * count right if last line in included file is syntactically
422 * invalid and has no newline.
423 */
424 if (file->eof_reached == 0) {
425 file->eof_reached = 1;
426 return ('\n');
428 while (c == EOF) {
429 if (file == topfile || popfile() == EOF)
430 return (EOF);
431 c = igetc();
434 return (c);
437 void
438 lungetc(int c)
440 if (c == EOF)
441 return;
443 if (file->ungetpos >= file->ungetsize) {
444 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
445 if (p == NULL)
446 err(1, "%s", __func__);
447 file->ungetbuf = p;
448 file->ungetsize *= 2;
450 file->ungetbuf[file->ungetpos++] = c;
453 int
454 findeol(void)
456 int c;
458 /* skip to either EOF or the first real EOL */
459 while (1) {
460 c = lgetc(0);
461 if (c == '\n') {
462 file->lineno++;
463 break;
465 if (c == EOF)
466 break;
468 return (ERROR);
471 int
472 yylex(void)
474 char buf[8096];
475 char *p, *val;
476 int quotec, next, c;
477 int token;
479 top:
480 p = buf;
481 while ((c = lgetc(0)) == ' ' || c == '\t')
482 ; /* nothing */
484 yylval.lineno = file->lineno;
485 if (c == '#')
486 while ((c = lgetc(0)) != '\n' && c != EOF)
487 ; /* nothing */
488 if (c == '$' && !expanding) {
489 while (1) {
490 if ((c = lgetc(0)) == EOF)
491 return (0);
493 if (p + 1 >= buf + sizeof(buf) - 1) {
494 yyerror("string too long");
495 return (findeol());
497 if (isalnum(c) || c == '_') {
498 *p++ = c;
499 continue;
501 *p = '\0';
502 lungetc(c);
503 break;
505 val = symget(buf);
506 if (val == NULL) {
507 yyerror("macro '%s' not defined", buf);
508 return (findeol());
510 p = val + strlen(val) - 1;
511 lungetc(DONE_EXPAND);
512 while (p >= val) {
513 lungetc((unsigned char)*p);
514 p--;
516 lungetc(START_EXPAND);
517 goto top;
520 switch (c) {
521 case '\'':
522 case '"':
523 quotec = c;
524 while (1) {
525 if ((c = lgetc(quotec)) == EOF)
526 return (0);
527 if (c == '\n') {
528 file->lineno++;
529 continue;
530 } else if (c == '\\') {
531 if ((next = lgetc(quotec)) == EOF)
532 return (0);
533 if (next == quotec || next == ' ' ||
534 next == '\t')
535 c = next;
536 else if (next == '\n') {
537 file->lineno++;
538 continue;
539 } else
540 lungetc(next);
541 } else if (c == quotec) {
542 *p = '\0';
543 break;
544 } else if (c == '\0') {
545 yyerror("syntax error");
546 return (findeol());
548 if (p + 1 >= buf + sizeof(buf) - 1) {
549 yyerror("string too long");
550 return (findeol());
552 *p++ = c;
554 yylval.v.string = strdup(buf);
555 if (yylval.v.string == NULL)
556 fatal("yylex: strdup");
557 return (STRING);
560 #define allowed_to_end_number(x) \
561 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
563 if (c == '-' || isdigit(c)) {
564 do {
565 *p++ = c;
566 if ((size_t)(p-buf) >= sizeof(buf)) {
567 yyerror("string too long");
568 return (findeol());
570 } while ((c = lgetc(0)) != EOF && isdigit(c));
571 lungetc(c);
572 if (p == buf + 1 && buf[0] == '-')
573 goto nodigits;
574 if (c == EOF || allowed_to_end_number(c)) {
575 const char *errstr = NULL;
577 *p = '\0';
578 yylval.v.number = strtonum(buf, LLONG_MIN,
579 LLONG_MAX, &errstr);
580 if (errstr) {
581 yyerror("\"%s\" invalid number: %s",
582 buf, errstr);
583 return (findeol());
585 return (NUMBER);
586 } else {
587 nodigits:
588 while (p > buf + 1)
589 lungetc((unsigned char)*--p);
590 c = (unsigned char)*--p;
591 if (c == '-')
592 return (c);
596 #define allowed_in_string(x) \
597 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
598 x != '{' && x != '}' && \
599 x != '!' && x != '=' && x != '#' && \
600 x != ','))
602 if (isalnum(c) || c == ':' || c == '_' || c == '/') {
603 do {
604 *p++ = c;
605 if ((size_t)(p-buf) >= sizeof(buf)) {
606 yyerror("string too long");
607 return (findeol());
609 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
610 lungetc(c);
611 *p = '\0';
612 if ((token = lookup(buf)) == STRING)
613 if ((yylval.v.string = strdup(buf)) == NULL)
614 fatal("yylex: strdup");
615 return (token);
617 if (c == '\n') {
618 yylval.lineno = file->lineno;
619 file->lineno++;
621 if (c == EOF)
622 return (0);
623 return (c);
626 struct file *
627 pushfile(const char *name, int secret)
629 struct file *nfile;
631 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
632 log_warn("%s", __func__);
633 return (NULL);
635 if ((nfile->name = strdup(name)) == NULL) {
636 log_warn("%s", __func__);
637 free(nfile);
638 return (NULL);
640 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
641 free(nfile->name);
642 free(nfile);
643 return (NULL);
645 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
646 nfile->ungetsize = 16;
647 nfile->ungetbuf = malloc(nfile->ungetsize);
648 if (nfile->ungetbuf == NULL) {
649 log_warn("%s", __func__);
650 fclose(nfile->stream);
651 free(nfile->name);
652 free(nfile);
653 return (NULL);
655 TAILQ_INSERT_TAIL(&files, nfile, entry);
656 return (nfile);
659 int
660 popfile(void)
662 struct file *prev;
664 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
665 prev->errors += file->errors;
667 TAILQ_REMOVE(&files, file, entry);
668 fclose(file->stream);
669 free(file->name);
670 free(file->ungetbuf);
671 free(file);
672 file = prev;
673 return (file ? 0 : EOF);
676 int
677 parse_config(const char *filename, struct galileo *env)
679 struct sym *sym, *next;
680 size_t n;
682 conf = env;
684 n = strlcpy(conf->sc_conffile, filename, sizeof(conf->sc_conffile));
685 if (n >= sizeof(conf->sc_conffile)) {
686 log_warn("path too long: %s", filename);
687 return (-1);
690 if ((file = pushfile(filename, 0)) == NULL) {
691 log_warn("failed to open %s", filename);
692 return (-1);
694 topfile = file;
695 setservent(1);
697 yyparse();
698 if (TAILQ_EMPTY(&conf->sc_proxies))
699 yyerror("no proxies defined");
700 errors = file->errors;
701 popfile();
703 endservent();
705 /* Free macros and check which have not been used. */
706 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
707 if (!sym->used)
708 fprintf(stderr, "warning: macro `%s' not used\n",
709 sym->nam);
710 if (!sym->persist) {
711 free(sym->nam);
712 free(sym->val);
713 TAILQ_REMOVE(&symhead, sym, entry);
714 free(sym);
718 if (errors)
719 return (-1);
721 return (0);
724 int
725 symset(const char *nam, const char *val, int persist)
727 struct sym *sym;
729 TAILQ_FOREACH(sym, &symhead, entry) {
730 if (strcmp(nam, sym->nam) == 0)
731 break;
734 if (sym != NULL) {
735 if (sym->persist == 1)
736 return (0);
737 else {
738 free(sym->nam);
739 free(sym->val);
740 TAILQ_REMOVE(&symhead, sym, entry);
741 free(sym);
744 if ((sym = calloc(1, sizeof(*sym))) == NULL)
745 return (-1);
747 sym->nam = strdup(nam);
748 if (sym->nam == NULL) {
749 free(sym);
750 return (-1);
752 sym->val = strdup(val);
753 if (sym->val == NULL) {
754 free(sym->nam);
755 free(sym);
756 return (-1);
758 sym->used = 0;
759 sym->persist = persist;
760 TAILQ_INSERT_TAIL(&symhead, sym, entry);
761 return (0);
764 int
765 cmdline_symset(char *s)
767 char *sym, *val;
768 int ret;
770 if ((val = strrchr(s, '=')) == NULL)
771 return (-1);
772 sym = strndup(s, val - s);
773 if (sym == NULL)
774 fatal("%s: strndup", __func__);
775 ret = symset(sym, val + 1, 1);
776 free(sym);
778 return (ret);
781 char *
782 symget(const char *nam)
784 struct sym *sym;
786 TAILQ_FOREACH(sym, &symhead, entry) {
787 if (strcmp(nam, sym->nam) == 0) {
788 sym->used = 1;
789 return (sym->val);
792 return (NULL);
795 int
796 getservice(const char *n)
798 struct servent *s;
799 const char *errstr;
800 long long llval;
802 llval = strtonum(n, 0, UINT16_MAX, &errstr);
803 if (errstr) {
804 s = getservbyname(n, "tcp");
805 if (s == NULL)
806 s = getservbyname(n, "udp");
807 if (s == NULL)
808 return (-1);
809 return (ntohs(s->s_port));
812 return ((unsigned short)llval);