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 <inttypes.h>
33 #include <netdb.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <limits.h>
37 #include <stdarg.h>
38 #include <stdint.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <pwd.h>
45 #include <imsg.h>
47 #include "log.h"
48 #include "proc.h"
50 #include "galileo.h"
52 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
53 static struct file {
54 TAILQ_ENTRY(file) entry;
55 FILE *stream;
56 char *name;
57 size_t ungetpos;
58 size_t ungetsize;
59 u_char *ungetbuf;
60 int eof_reached;
61 int lineno;
62 int errors;
63 } *file, *topfile;
64 struct file *pushfile(const char *, int);
65 int popfile(void);
66 int yyparse(void);
67 int yylex(void);
68 int yyerror(const char *, ...)
69 __attribute__((__format__ (printf, 1, 2)))
70 __attribute__((__nonnull__ (1)));
71 int kw_cmp(const void *, const void *);
72 int lookup(char *);
73 int igetc(void);
74 int lgetc(int);
75 void lungetc(int);
76 int findeol(void);
78 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
79 struct sym {
80 TAILQ_ENTRY(sym) entry;
81 int used;
82 int persist;
83 char *nam;
84 char *val;
85 };
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;
93 static int errors;
95 typedef struct {
96 union {
97 int64_t number;
98 char *string;
99 } v;
100 int lineno;
101 } YYSTYPE;
103 %}
105 %token INCLUDE ERROR
106 %token CHROOT HOSTNAME NO PORT PREFORK PROXY SOURCE STYLESHEET TLS
107 %token <v.number> NUMBER
108 %token <v.string> STRING
109 %type <v.number> port
110 %type <v.string> string
112 %%
114 grammar : /* empty */
115 | grammar include '\n'
116 | grammar '\n'
117 | grammar varset '\n'
118 | grammar main '\n'
119 | grammar proxy '\n'
120 | grammar error '\n' { file->errors++; }
123 include : INCLUDE string {
124 struct file *nfile;
126 if ((nfile = pushfile($2, 0)) == NULL) {
127 yyerror("failed to include file %s", $2);
128 free($2);
129 YYERROR;
131 free($2);
133 file = nfile;
134 lungetc('\n');
138 varset : STRING '=' STRING {
139 char *s = $1;
140 while (*s++) {
141 if (isspace((unsigned char)*s)) {
142 yyerror("macro name cannot contain "
143 "whitespace");
144 free($1);
145 free($3);
146 YYERROR;
149 if (symset($1, $3, 0) == -1)
150 fatalx("cannot store variable");
151 free($1);
152 free($3);
156 main : PREFORK NUMBER {
157 if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
158 yyerror("invalid number of preforked "
159 "proxies: %"PRId64, $2);
160 YYERROR;
162 conf->sc_prefork = $2;
164 | CHROOT STRING {
165 size_t n;
167 n = strlcpy(conf->sc_chroot, $2,
168 sizeof(conf->sc_chroot));
169 if (n >= sizeof(conf->sc_chroot))
170 yyerror("chroot path too long!");
171 free($2);
175 proxy : PROXY STRING {
176 struct proxy *p;
177 size_t n;
179 if ((p = calloc(1, sizeof(*p))) == NULL)
180 fatal("calloc");
182 n = strlcpy(p->pr_conf.host, $2,
183 sizeof(p->pr_conf.host));
184 if (n >= sizeof(p->pr_conf.host)) {
185 yyerror("server name too long");
186 free($2);
187 free(p);
188 YYERROR;
190 free($2);
192 pr = p;
193 } '{' optnl proxyopts_l '}' {
194 /* check if duplicate */
195 if (proxy_match(conf, pr->pr_conf.host) != NULL)
196 yyerror("duplicate proxy `%s'",
197 pr->pr_conf.host);
199 TAILQ_INSERT_TAIL(&conf->sc_proxies, pr, pr_entry);
201 if (*pr->pr_conf.proxy_addr == '\0')
202 yyerror("missing source in proxy block `%s'",
203 pr->pr_conf.host);
205 pr = NULL;
209 proxyopts_l : proxyopts_l proxyoptsl nl
210 | proxyoptsl optnl
213 proxyoptsl : SOURCE STRING proxyport {
214 size_t n;
216 n = strlcpy(pr->pr_conf.proxy_addr, $2,
217 sizeof(pr->pr_conf.proxy_addr));
218 if (n >= sizeof(pr->pr_conf.proxy_addr))
219 yyerror("proxy source too long!");
221 if (*pr->pr_conf.proxy_name == '\0') {
222 n = strlcpy(pr->pr_conf.proxy_name, $2,
223 sizeof(pr->pr_conf.proxy_name));
224 if (n >= sizeof(pr->pr_conf.proxy_name))
225 yyerror("proxy hostname too long!");
228 free($2);
230 | HOSTNAME STRING {
231 size_t n;
233 n = strlcpy(pr->pr_conf.proxy_name, $2,
234 sizeof(pr->pr_conf.proxy_name));
235 if (n >= sizeof(pr->pr_conf.proxy_name))
236 yyerror("proxy hostname too long!");
237 free($2);
239 | STYLESHEET string {
240 size_t n;
242 n = strlcpy(pr->pr_conf.stylesheet, $2,
243 sizeof(pr->pr_conf.stylesheet));
244 if (n >= sizeof(pr->pr_conf.stylesheet))
245 yyerror("stylesheet path too long!");
246 free($2);
248 | NO TLS {
249 pr->pr_conf.no_tls = 1;
253 proxyport : /* empty */ {
254 strlcpy(pr->pr_conf.proxy_port, "1965",
255 sizeof(pr->pr_conf.proxy_port));
257 | PORT port {
258 size_t len;
259 int n;
261 len = sizeof(pr->pr_conf.proxy_port);
262 n = snprintf(pr->pr_conf.proxy_port, len,
263 "%"PRId64, $2);
264 if (n < 0 || (size_t)n >= len)
265 fatal("port number too long?");
266 };
268 port : NUMBER {
269 if ($1 <= 0 || $1 > (int)USHRT_MAX) {
270 yyerror("invalid port: %"PRId64, $1);
271 YYERROR;
273 $$ = $1;
275 | STRING {
276 int val;
278 if ((val = getservice($1)) == -1) {
279 yyerror("invalid port: %s", $1);
280 free($1);
281 YYERROR;
283 free($1);
284 $$ = val;
288 string : STRING string {
289 if (asprintf(&$$, "%s%s", $1, $2) == -1)
290 fatal("asprintf string");
291 free($1);
292 free($2);
294 | STRING
297 optnl : '\n' optnl
301 nl : '\n' optnl
304 %%
306 struct keywords {
307 const char *k_name;
308 int k_val;
309 };
311 int
312 yyerror(const char *fmt, ...)
314 va_list ap;
315 char *msg;
317 file->errors++;
318 va_start(ap, fmt);
319 if (vasprintf(&msg, fmt, ap) == -1)
320 fatal("yyerror vasprintf");
321 va_end(ap);
322 log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
323 free(msg);
324 return (0);
327 int
328 kw_cmp(const void *k, const void *e)
330 return (strcmp(k, ((const struct keywords *)e)->k_name));
333 int
334 lookup(char *s)
336 /* this has to be sorted always */
337 static const struct keywords keywords[] = {
338 { "chroot", CHROOT },
339 { "hostname", HOSTNAME },
340 { "include", INCLUDE },
341 { "no", NO },
342 { "port", PORT },
343 { "prefork", PREFORK },
344 { "proxy", PROXY },
345 { "source", SOURCE },
346 { "stylesheet", STYLESHEET},
347 { "tls", TLS },
348 };
349 const struct keywords *p;
351 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
352 kw_cmp);
354 if (p)
355 return (p->k_val);
356 else
357 return (STRING);
360 #define START_EXPAND 1
361 #define DONE_EXPAND 2
363 static int expanding;
365 int
366 igetc(void)
368 int c;
370 while (1) {
371 if (file->ungetpos > 0)
372 c = file->ungetbuf[--file->ungetpos];
373 else
374 c = getc(file->stream);
376 if (c == START_EXPAND)
377 expanding = 1;
378 else if (c == DONE_EXPAND)
379 expanding = 0;
380 else
381 break;
383 return (c);
386 int
387 lgetc(int quotec)
389 int c, next;
391 if (quotec) {
392 if ((c = igetc()) == EOF) {
393 yyerror("reached end of file while parsing "
394 "quoted string");
395 if (file == topfile || popfile() == EOF)
396 return (EOF);
397 return (quotec);
399 return (c);
402 while ((c = igetc()) == '\\') {
403 next = igetc();
404 if (next != '\n') {
405 c = next;
406 break;
408 yylval.lineno = file->lineno;
409 file->lineno++;
411 if (c == '\t' || c == ' ') {
412 /* Compress blanks to a single space. */
413 do {
414 c = getc(file->stream);
415 } while (c == '\t' || c == ' ');
416 ungetc(c, file->stream);
417 c = ' ';
420 if (c == EOF) {
421 /*
422 * Fake EOL when hit EOF for the first time. This gets line
423 * count right if last line in included file is syntactically
424 * invalid and has no newline.
425 */
426 if (file->eof_reached == 0) {
427 file->eof_reached = 1;
428 return ('\n');
430 while (c == EOF) {
431 if (file == topfile || popfile() == EOF)
432 return (EOF);
433 c = igetc();
436 return (c);
439 void
440 lungetc(int c)
442 if (c == EOF)
443 return;
445 if (file->ungetpos >= file->ungetsize) {
446 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
447 if (p == NULL)
448 err(1, "%s", __func__);
449 file->ungetbuf = p;
450 file->ungetsize *= 2;
452 file->ungetbuf[file->ungetpos++] = c;
455 int
456 findeol(void)
458 int c;
460 /* skip to either EOF or the first real EOL */
461 while (1) {
462 c = lgetc(0);
463 if (c == '\n') {
464 file->lineno++;
465 break;
467 if (c == EOF)
468 break;
470 return (ERROR);
473 int
474 yylex(void)
476 char buf[8096];
477 char *p, *val;
478 int quotec, next, c;
479 int token;
481 top:
482 p = buf;
483 while ((c = lgetc(0)) == ' ' || c == '\t')
484 ; /* nothing */
486 yylval.lineno = file->lineno;
487 if (c == '#')
488 while ((c = lgetc(0)) != '\n' && c != EOF)
489 ; /* nothing */
490 if (c == '$' && !expanding) {
491 while (1) {
492 if ((c = lgetc(0)) == EOF)
493 return (0);
495 if (p + 1 >= buf + sizeof(buf) - 1) {
496 yyerror("string too long");
497 return (findeol());
499 if (isalnum(c) || c == '_') {
500 *p++ = c;
501 continue;
503 *p = '\0';
504 lungetc(c);
505 break;
507 val = symget(buf);
508 if (val == NULL) {
509 yyerror("macro '%s' not defined", buf);
510 return (findeol());
512 p = val + strlen(val) - 1;
513 lungetc(DONE_EXPAND);
514 while (p >= val) {
515 lungetc((unsigned char)*p);
516 p--;
518 lungetc(START_EXPAND);
519 goto top;
522 switch (c) {
523 case '\'':
524 case '"':
525 quotec = c;
526 while (1) {
527 if ((c = lgetc(quotec)) == EOF)
528 return (0);
529 if (c == '\n') {
530 file->lineno++;
531 continue;
532 } else if (c == '\\') {
533 if ((next = lgetc(quotec)) == EOF)
534 return (0);
535 if (next == quotec || next == ' ' ||
536 next == '\t')
537 c = next;
538 else if (next == '\n') {
539 file->lineno++;
540 continue;
541 } else
542 lungetc(next);
543 } else if (c == quotec) {
544 *p = '\0';
545 break;
546 } else if (c == '\0') {
547 yyerror("syntax error");
548 return (findeol());
550 if (p + 1 >= buf + sizeof(buf) - 1) {
551 yyerror("string too long");
552 return (findeol());
554 *p++ = c;
556 yylval.v.string = strdup(buf);
557 if (yylval.v.string == NULL)
558 fatal("yylex: strdup");
559 return (STRING);
562 #define allowed_to_end_number(x) \
563 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
565 if (c == '-' || isdigit(c)) {
566 do {
567 *p++ = c;
568 if ((size_t)(p-buf) >= sizeof(buf)) {
569 yyerror("string too long");
570 return (findeol());
572 } while ((c = lgetc(0)) != EOF && isdigit(c));
573 lungetc(c);
574 if (p == buf + 1 && buf[0] == '-')
575 goto nodigits;
576 if (c == EOF || allowed_to_end_number(c)) {
577 const char *errstr = NULL;
579 *p = '\0';
580 yylval.v.number = strtonum(buf, LLONG_MIN,
581 LLONG_MAX, &errstr);
582 if (errstr) {
583 yyerror("\"%s\" invalid number: %s",
584 buf, errstr);
585 return (findeol());
587 return (NUMBER);
588 } else {
589 nodigits:
590 while (p > buf + 1)
591 lungetc((unsigned char)*--p);
592 c = (unsigned char)*--p;
593 if (c == '-')
594 return (c);
598 #define allowed_in_string(x) \
599 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
600 x != '{' && x != '}' && \
601 x != '!' && x != '=' && x != '#' && \
602 x != ','))
604 if (isalnum(c) || c == ':' || c == '_' || c == '/') {
605 do {
606 *p++ = c;
607 if ((size_t)(p-buf) >= sizeof(buf)) {
608 yyerror("string too long");
609 return (findeol());
611 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
612 lungetc(c);
613 *p = '\0';
614 if ((token = lookup(buf)) == STRING)
615 if ((yylval.v.string = strdup(buf)) == NULL)
616 fatal("yylex: strdup");
617 return (token);
619 if (c == '\n') {
620 yylval.lineno = file->lineno;
621 file->lineno++;
623 if (c == EOF)
624 return (0);
625 return (c);
628 struct file *
629 pushfile(const char *name, int secret)
631 struct file *nfile;
633 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
634 log_warn("%s", __func__);
635 return (NULL);
637 if ((nfile->name = strdup(name)) == NULL) {
638 log_warn("%s", __func__);
639 free(nfile);
640 return (NULL);
642 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
643 free(nfile->name);
644 free(nfile);
645 return (NULL);
647 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
648 nfile->ungetsize = 16;
649 nfile->ungetbuf = malloc(nfile->ungetsize);
650 if (nfile->ungetbuf == NULL) {
651 log_warn("%s", __func__);
652 fclose(nfile->stream);
653 free(nfile->name);
654 free(nfile);
655 return (NULL);
657 TAILQ_INSERT_TAIL(&files, nfile, entry);
658 return (nfile);
661 int
662 popfile(void)
664 struct file *prev;
666 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
667 prev->errors += file->errors;
669 TAILQ_REMOVE(&files, file, entry);
670 fclose(file->stream);
671 free(file->name);
672 free(file->ungetbuf);
673 free(file);
674 file = prev;
675 return (file ? 0 : EOF);
678 int
679 parse_config(const char *filename, struct galileo *env)
681 struct sym *sym, *next;
682 size_t n;
684 conf = env;
686 n = strlcpy(conf->sc_conffile, filename, sizeof(conf->sc_conffile));
687 if (n >= sizeof(conf->sc_conffile)) {
688 log_warn("path too long: %s", filename);
689 return (-1);
692 if ((file = pushfile(filename, 0)) == NULL) {
693 log_warn("failed to open %s", filename);
694 return (-1);
696 topfile = file;
697 setservent(1);
699 yyparse();
700 if (TAILQ_EMPTY(&conf->sc_proxies))
701 yyerror("no proxies defined");
702 errors = file->errors;
703 popfile();
705 endservent();
707 /* Free macros and check which have not been used. */
708 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
709 if (!sym->used)
710 fprintf(stderr, "warning: macro `%s' not used\n",
711 sym->nam);
712 if (!sym->persist) {
713 free(sym->nam);
714 free(sym->val);
715 TAILQ_REMOVE(&symhead, sym, entry);
716 free(sym);
720 if (errors)
721 return (-1);
723 return (0);
726 int
727 symset(const char *nam, const char *val, int persist)
729 struct sym *sym;
731 TAILQ_FOREACH(sym, &symhead, entry) {
732 if (strcmp(nam, sym->nam) == 0)
733 break;
736 if (sym != NULL) {
737 if (sym->persist == 1)
738 return (0);
739 else {
740 free(sym->nam);
741 free(sym->val);
742 TAILQ_REMOVE(&symhead, sym, entry);
743 free(sym);
746 if ((sym = calloc(1, sizeof(*sym))) == NULL)
747 return (-1);
749 sym->nam = strdup(nam);
750 if (sym->nam == NULL) {
751 free(sym);
752 return (-1);
754 sym->val = strdup(val);
755 if (sym->val == NULL) {
756 free(sym->nam);
757 free(sym);
758 return (-1);
760 sym->used = 0;
761 sym->persist = persist;
762 TAILQ_INSERT_TAIL(&symhead, sym, entry);
763 return (0);
766 int
767 cmdline_symset(char *s)
769 char *sym, *val;
770 int ret;
772 if ((val = strrchr(s, '=')) == NULL)
773 return (-1);
774 sym = strndup(s, val - s);
775 if (sym == NULL)
776 fatal("%s: strndup", __func__);
777 ret = symset(sym, val + 1, 1);
778 free(sym);
780 return (ret);
783 char *
784 symget(const char *nam)
786 struct sym *sym;
788 TAILQ_FOREACH(sym, &symhead, entry) {
789 if (strcmp(nam, sym->nam) == 0) {
790 sym->used = 1;
791 return (sym->val);
794 return (NULL);
797 int
798 getservice(const char *n)
800 struct servent *s;
801 const char *errstr;
802 long long llval;
804 llval = strtonum(n, 0, UINT16_MAX, &errstr);
805 if (errstr) {
806 s = getservbyname(n, "tcp");
807 if (s == NULL)
808 s = getservbyname(n, "udp");
809 if (s == NULL)
810 return (-1);
811 return (ntohs(s->s_port));
814 return ((unsigned short)llval);