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 %type <v.number> NUMBER
107 %type <v.number> port
108 %type <v.string> STRING
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 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'",
198 pr->pr_conf.host);
200 pr = NULL;
204 proxyopts_l : proxyopts_l proxyoptsl nl
205 | proxyoptsl optnl
208 proxyoptsl : SOURCE STRING proxyport {
209 size_t n;
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!");
223 free($2);
225 | HOSTNAME STRING {
226 size_t n;
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!");
232 free($2);
234 | STYLESHEET string {
235 size_t n;
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!");
241 free($2);
245 proxyport : /* empty */ {
246 strlcpy(pr->pr_conf.proxy_port, "1965",
247 sizeof(pr->pr_conf.proxy_port));
249 | PORT port {
250 size_t len;
251 int n;
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?");
257 };
259 port : NUMBER {
260 if ($1 <= 0 || $1 > (int)USHRT_MAX) {
261 yyerror("invalid port: %lld", $1);
262 YYERROR;
264 $$ = $1;
266 | STRING {
267 int val;
269 if ((val = getservice($1)) == -1) {
270 yyerror("invalid port: %s", $1);
271 free($1);
272 YYERROR;
274 free($1);
275 $$ = val;
279 string : STRING string {
280 if (asprintf(&$$, "%s%s", $1, $2) == -1)
281 fatal("asprintf string");
282 free($1);
283 free($2);
285 | STRING
288 optnl : '\n' optnl
292 nl : '\n' optnl
295 %%
297 struct keywords {
298 const char *k_name;
299 int k_val;
300 };
302 int
303 yyerror(const char *fmt, ...)
305 va_list ap;
306 char *msg;
308 file->errors++;
309 va_start(ap, fmt);
310 if (vasprintf(&msg, fmt, ap) == -1)
311 fatal("yyerror vasprintf");
312 va_end(ap);
313 log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
314 free(msg);
315 return (0);
318 int
319 kw_cmp(const void *k, const void *e)
321 return (strcmp(k, ((const struct keywords *)e)->k_name));
324 int
325 lookup(char *s)
327 /* this has to be sorted always */
328 static const struct keywords keywords[] = {
329 { "chroot", CHROOT },
330 { "hostname", HOSTNAME },
331 { "include", INCLUDE },
332 { "port", PORT },
333 { "prefork", PREFORK },
334 { "proxy", PROXY },
335 { "source", SOURCE },
336 { "stylesheet", STYLESHEET},
337 };
338 const struct keywords *p;
340 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
341 kw_cmp);
343 if (p)
344 return (p->k_val);
345 else
346 return (STRING);
349 #define START_EXPAND 1
350 #define DONE_EXPAND 2
352 static int expanding;
354 int
355 igetc(void)
357 int c;
359 while (1) {
360 if (file->ungetpos > 0)
361 c = file->ungetbuf[--file->ungetpos];
362 else
363 c = getc(file->stream);
365 if (c == START_EXPAND)
366 expanding = 1;
367 else if (c == DONE_EXPAND)
368 expanding = 0;
369 else
370 break;
372 return (c);
375 int
376 lgetc(int quotec)
378 int c, next;
380 if (quotec) {
381 if ((c = igetc()) == EOF) {
382 yyerror("reached end of file while parsing "
383 "quoted string");
384 if (file == topfile || popfile() == EOF)
385 return (EOF);
386 return (quotec);
388 return (c);
391 while ((c = igetc()) == '\\') {
392 next = igetc();
393 if (next != '\n') {
394 c = next;
395 break;
397 yylval.lineno = file->lineno;
398 file->lineno++;
400 if (c == '\t' || c == ' ') {
401 /* Compress blanks to a single space. */
402 do {
403 c = getc(file->stream);
404 } while (c == '\t' || c == ' ');
405 ungetc(c, file->stream);
406 c = ' ';
409 if (c == EOF) {
410 /*
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.
414 */
415 if (file->eof_reached == 0) {
416 file->eof_reached = 1;
417 return ('\n');
419 while (c == EOF) {
420 if (file == topfile || popfile() == EOF)
421 return (EOF);
422 c = igetc();
425 return (c);
428 void
429 lungetc(int c)
431 if (c == EOF)
432 return;
434 if (file->ungetpos >= file->ungetsize) {
435 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
436 if (p == NULL)
437 err(1, "%s", __func__);
438 file->ungetbuf = p;
439 file->ungetsize *= 2;
441 file->ungetbuf[file->ungetpos++] = c;
444 int
445 findeol(void)
447 int c;
449 /* skip to either EOF or the first real EOL */
450 while (1) {
451 c = lgetc(0);
452 if (c == '\n') {
453 file->lineno++;
454 break;
456 if (c == EOF)
457 break;
459 return (ERROR);
462 int
463 yylex(void)
465 char buf[8096];
466 char *p, *val;
467 int quotec, next, c;
468 int token;
470 top:
471 p = buf;
472 while ((c = lgetc(0)) == ' ' || c == '\t')
473 ; /* nothing */
475 yylval.lineno = file->lineno;
476 if (c == '#')
477 while ((c = lgetc(0)) != '\n' && c != EOF)
478 ; /* nothing */
479 if (c == '$' && !expanding) {
480 while (1) {
481 if ((c = lgetc(0)) == EOF)
482 return (0);
484 if (p + 1 >= buf + sizeof(buf) - 1) {
485 yyerror("string too long");
486 return (findeol());
488 if (isalnum(c) || c == '_') {
489 *p++ = c;
490 continue;
492 *p = '\0';
493 lungetc(c);
494 break;
496 val = symget(buf);
497 if (val == NULL) {
498 yyerror("macro '%s' not defined", buf);
499 return (findeol());
501 p = val + strlen(val) - 1;
502 lungetc(DONE_EXPAND);
503 while (p >= val) {
504 lungetc((unsigned char)*p);
505 p--;
507 lungetc(START_EXPAND);
508 goto top;
511 switch (c) {
512 case '\'':
513 case '"':
514 quotec = c;
515 while (1) {
516 if ((c = lgetc(quotec)) == EOF)
517 return (0);
518 if (c == '\n') {
519 file->lineno++;
520 continue;
521 } else if (c == '\\') {
522 if ((next = lgetc(quotec)) == EOF)
523 return (0);
524 if (next == quotec || next == ' ' ||
525 next == '\t')
526 c = next;
527 else if (next == '\n') {
528 file->lineno++;
529 continue;
530 } else
531 lungetc(next);
532 } else if (c == quotec) {
533 *p = '\0';
534 break;
535 } else if (c == '\0') {
536 yyerror("syntax error");
537 return (findeol());
539 if (p + 1 >= buf + sizeof(buf) - 1) {
540 yyerror("string too long");
541 return (findeol());
543 *p++ = c;
545 yylval.v.string = strdup(buf);
546 if (yylval.v.string == NULL)
547 fatal("yylex: strdup");
548 return (STRING);
551 #define allowed_to_end_number(x) \
552 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
554 if (c == '-' || isdigit(c)) {
555 do {
556 *p++ = c;
557 if ((size_t)(p-buf) >= sizeof(buf)) {
558 yyerror("string too long");
559 return (findeol());
561 } while ((c = lgetc(0)) != EOF && isdigit(c));
562 lungetc(c);
563 if (p == buf + 1 && buf[0] == '-')
564 goto nodigits;
565 if (c == EOF || allowed_to_end_number(c)) {
566 const char *errstr = NULL;
568 *p = '\0';
569 yylval.v.number = strtonum(buf, LLONG_MIN,
570 LLONG_MAX, &errstr);
571 if (errstr) {
572 yyerror("\"%s\" invalid number: %s",
573 buf, errstr);
574 return (findeol());
576 return (NUMBER);
577 } else {
578 nodigits:
579 while (p > buf + 1)
580 lungetc((unsigned char)*--p);
581 c = (unsigned char)*--p;
582 if (c == '-')
583 return (c);
587 #define allowed_in_string(x) \
588 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
589 x != '{' && x != '}' && \
590 x != '!' && x != '=' && x != '#' && \
591 x != ','))
593 if (isalnum(c) || c == ':' || c == '_' || c == '/') {
594 do {
595 *p++ = c;
596 if ((size_t)(p-buf) >= sizeof(buf)) {
597 yyerror("string too long");
598 return (findeol());
600 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
601 lungetc(c);
602 *p = '\0';
603 if ((token = lookup(buf)) == STRING)
604 if ((yylval.v.string = strdup(buf)) == NULL)
605 fatal("yylex: strdup");
606 return (token);
608 if (c == '\n') {
609 yylval.lineno = file->lineno;
610 file->lineno++;
612 if (c == EOF)
613 return (0);
614 return (c);
617 struct file *
618 pushfile(const char *name, int secret)
620 struct file *nfile;
622 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
623 log_warn("%s", __func__);
624 return (NULL);
626 if ((nfile->name = strdup(name)) == NULL) {
627 log_warn("%s", __func__);
628 free(nfile);
629 return (NULL);
631 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
632 free(nfile->name);
633 free(nfile);
634 return (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);
642 free(nfile->name);
643 free(nfile);
644 return (NULL);
646 TAILQ_INSERT_TAIL(&files, nfile, entry);
647 return (nfile);
650 int
651 popfile(void)
653 struct file *prev;
655 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
656 prev->errors += file->errors;
658 TAILQ_REMOVE(&files, file, entry);
659 fclose(file->stream);
660 free(file->name);
661 free(file->ungetbuf);
662 free(file);
663 file = prev;
664 return (file ? 0 : EOF);
667 int
668 parse_config(const char *filename, struct galileo *env)
670 struct sym *sym, *next;
671 size_t n;
673 conf = env;
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);
678 return (-1);
681 if ((file = pushfile(filename, 0)) == NULL) {
682 log_warn("failed to open %s", filename);
683 return (-1);
685 topfile = file;
686 setservent(1);
688 yyparse();
689 errors = file->errors;
690 popfile();
692 endservent();
694 /* Free macros and check which have not been used. */
695 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
696 if (!sym->used)
697 fprintf(stderr, "warning: macro `%s' not used\n",
698 sym->nam);
699 if (!sym->persist) {
700 free(sym->nam);
701 free(sym->val);
702 TAILQ_REMOVE(&symhead, sym, entry);
703 free(sym);
707 if (errors)
708 return (-1);
710 return (0);
713 int
714 symset(const char *nam, const char *val, int persist)
716 struct sym *sym;
718 TAILQ_FOREACH(sym, &symhead, entry) {
719 if (strcmp(nam, sym->nam) == 0)
720 break;
723 if (sym != NULL) {
724 if (sym->persist == 1)
725 return (0);
726 else {
727 free(sym->nam);
728 free(sym->val);
729 TAILQ_REMOVE(&symhead, sym, entry);
730 free(sym);
733 if ((sym = calloc(1, sizeof(*sym))) == NULL)
734 return (-1);
736 sym->nam = strdup(nam);
737 if (sym->nam == NULL) {
738 free(sym);
739 return (-1);
741 sym->val = strdup(val);
742 if (sym->val == NULL) {
743 free(sym->nam);
744 free(sym);
745 return (-1);
747 sym->used = 0;
748 sym->persist = persist;
749 TAILQ_INSERT_TAIL(&symhead, sym, entry);
750 return (0);
753 int
754 cmdline_symset(char *s)
756 char *sym, *val;
757 int ret;
759 if ((val = strrchr(s, '=')) == NULL)
760 return (-1);
761 sym = strndup(s, val - s);
762 if (sym == NULL)
763 fatal("%s: strndup", __func__);
764 ret = symset(sym, val + 1, 1);
765 free(sym);
767 return (ret);
770 char *
771 symget(const char *nam)
773 struct sym *sym;
775 TAILQ_FOREACH(sym, &symhead, entry) {
776 if (strcmp(nam, sym->nam) == 0) {
777 sym->used = 1;
778 return (sym->val);
781 return (NULL);
784 int
785 getservice(const char *n)
787 struct servent *s;
788 const char *errstr;
789 long long llval;
791 llval = strtonum(n, 0, UINT16_MAX, &errstr);
792 if (errstr) {
793 s = getservbyname(n, "tcp");
794 if (s == NULL)
795 s = getservbyname(n, "udp");
796 if (s == NULL)
797 return (-1);
798 return (ntohs(s->s_port));
801 return ((unsigned short)llval);