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 */
195 /* eventually load the tls certs */
197 pr = NULL;
201 proxyopts_l : proxyopts_l proxyoptsl nl
202 | proxyoptsl optnl
205 proxyoptsl : SOURCE STRING PORT port {
206 size_t n;
208 n = strlcpy(pr->pr_conf.proxy_addr, $2,
209 sizeof(pr->pr_conf.proxy_addr));
210 if (n >= sizeof(pr->pr_conf.proxy_addr))
211 yyerror("proxy source too long!");
212 pr->pr_conf.proxy_port = $4;
214 free($2);
216 | HOSTNAME STRING {
217 size_t n;
219 n = strlcpy(pr->pr_conf.proxy_name, $2,
220 sizeof(pr->pr_conf.proxy_name));
221 if (n >= sizeof(pr->pr_conf.proxy_name))
222 yyerror("proxy hostname too long!");
223 free($2);
225 | STYLESHEET string {
226 size_t n;
228 n = strlcpy(pr->pr_conf.stylesheet, $2,
229 sizeof(pr->pr_conf.stylesheet));
230 if (n >= sizeof(pr->pr_conf.stylesheet))
231 yyerror("stylesheet path too long!");
232 free($2);
236 port : NUMBER {
237 if ($1 <= 0 || $1 > (int)USHRT_MAX) {
238 yyerror("invalid port: %lld", $1);
239 YYERROR;
241 $$ = $1;
243 | STRING {
244 int val;
246 if ((val = getservice($1)) == -1) {
247 yyerror("invalid port: %s", $1);
248 free($1);
249 YYERROR;
251 free($1);
252 $$ = val;
256 string : STRING string {
257 if (asprintf(&$$, "%s%s", $1, $2) == -1)
258 fatal("asprintf string");
259 free($1);
260 free($2);
262 | STRING
265 optnl : '\n' optnl
269 nl : '\n' optnl
272 %%
274 struct keywords {
275 const char *k_name;
276 int k_val;
277 };
279 int
280 yyerror(const char *fmt, ...)
282 va_list ap;
283 char *msg;
285 file->errors++;
286 va_start(ap, fmt);
287 if (vasprintf(&msg, fmt, ap) == -1)
288 fatal("yyerror vasprintf");
289 va_end(ap);
290 log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
291 free(msg);
292 return (0);
295 int
296 kw_cmp(const void *k, const void *e)
298 return (strcmp(k, ((const struct keywords *)e)->k_name));
301 int
302 lookup(char *s)
304 /* this has to be sorted always */
305 static const struct keywords keywords[] = {
306 { "chroot", CHROOT },
307 { "hostname", HOSTNAME },
308 { "include", INCLUDE },
309 { "port", PORT },
310 { "prefork", PREFORK },
311 { "proxy", PROXY },
312 { "source", SOURCE },
313 { "stylesheet", STYLESHEET},
314 };
315 const struct keywords *p;
317 p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
318 kw_cmp);
320 if (p)
321 return (p->k_val);
322 else
323 return (STRING);
326 #define START_EXPAND 1
327 #define DONE_EXPAND 2
329 static int expanding;
331 int
332 igetc(void)
334 int c;
336 while (1) {
337 if (file->ungetpos > 0)
338 c = file->ungetbuf[--file->ungetpos];
339 else
340 c = getc(file->stream);
342 if (c == START_EXPAND)
343 expanding = 1;
344 else if (c == DONE_EXPAND)
345 expanding = 0;
346 else
347 break;
349 return (c);
352 int
353 lgetc(int quotec)
355 int c, next;
357 if (quotec) {
358 if ((c = igetc()) == EOF) {
359 yyerror("reached end of file while parsing "
360 "quoted string");
361 if (file == topfile || popfile() == EOF)
362 return (EOF);
363 return (quotec);
365 return (c);
368 while ((c = igetc()) == '\\') {
369 next = igetc();
370 if (next != '\n') {
371 c = next;
372 break;
374 yylval.lineno = file->lineno;
375 file->lineno++;
377 if (c == '\t' || c == ' ') {
378 /* Compress blanks to a single space. */
379 do {
380 c = getc(file->stream);
381 } while (c == '\t' || c == ' ');
382 ungetc(c, file->stream);
383 c = ' ';
386 if (c == EOF) {
387 /*
388 * Fake EOL when hit EOF for the first time. This gets line
389 * count right if last line in included file is syntactically
390 * invalid and has no newline.
391 */
392 if (file->eof_reached == 0) {
393 file->eof_reached = 1;
394 return ('\n');
396 while (c == EOF) {
397 if (file == topfile || popfile() == EOF)
398 return (EOF);
399 c = igetc();
402 return (c);
405 void
406 lungetc(int c)
408 if (c == EOF)
409 return;
411 if (file->ungetpos >= file->ungetsize) {
412 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
413 if (p == NULL)
414 err(1, "%s", __func__);
415 file->ungetbuf = p;
416 file->ungetsize *= 2;
418 file->ungetbuf[file->ungetpos++] = c;
421 int
422 findeol(void)
424 int c;
426 /* skip to either EOF or the first real EOL */
427 while (1) {
428 c = lgetc(0);
429 if (c == '\n') {
430 file->lineno++;
431 break;
433 if (c == EOF)
434 break;
436 return (ERROR);
439 int
440 yylex(void)
442 char buf[8096];
443 char *p, *val;
444 int quotec, next, c;
445 int token;
447 top:
448 p = buf;
449 while ((c = lgetc(0)) == ' ' || c == '\t')
450 ; /* nothing */
452 yylval.lineno = file->lineno;
453 if (c == '#')
454 while ((c = lgetc(0)) != '\n' && c != EOF)
455 ; /* nothing */
456 if (c == '$' && !expanding) {
457 while (1) {
458 if ((c = lgetc(0)) == EOF)
459 return (0);
461 if (p + 1 >= buf + sizeof(buf) - 1) {
462 yyerror("string too long");
463 return (findeol());
465 if (isalnum(c) || c == '_') {
466 *p++ = c;
467 continue;
469 *p = '\0';
470 lungetc(c);
471 break;
473 val = symget(buf);
474 if (val == NULL) {
475 yyerror("macro '%s' not defined", buf);
476 return (findeol());
478 p = val + strlen(val) - 1;
479 lungetc(DONE_EXPAND);
480 while (p >= val) {
481 lungetc((unsigned char)*p);
482 p--;
484 lungetc(START_EXPAND);
485 goto top;
488 switch (c) {
489 case '\'':
490 case '"':
491 quotec = c;
492 while (1) {
493 if ((c = lgetc(quotec)) == EOF)
494 return (0);
495 if (c == '\n') {
496 file->lineno++;
497 continue;
498 } else if (c == '\\') {
499 if ((next = lgetc(quotec)) == EOF)
500 return (0);
501 if (next == quotec || next == ' ' ||
502 next == '\t')
503 c = next;
504 else if (next == '\n') {
505 file->lineno++;
506 continue;
507 } else
508 lungetc(next);
509 } else if (c == quotec) {
510 *p = '\0';
511 break;
512 } else if (c == '\0') {
513 yyerror("syntax error");
514 return (findeol());
516 if (p + 1 >= buf + sizeof(buf) - 1) {
517 yyerror("string too long");
518 return (findeol());
520 *p++ = c;
522 yylval.v.string = strdup(buf);
523 if (yylval.v.string == NULL)
524 fatal("yylex: strdup");
525 return (STRING);
528 #define allowed_to_end_number(x) \
529 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
531 if (c == '-' || isdigit(c)) {
532 do {
533 *p++ = c;
534 if ((size_t)(p-buf) >= sizeof(buf)) {
535 yyerror("string too long");
536 return (findeol());
538 } while ((c = lgetc(0)) != EOF && isdigit(c));
539 lungetc(c);
540 if (p == buf + 1 && buf[0] == '-')
541 goto nodigits;
542 if (c == EOF || allowed_to_end_number(c)) {
543 const char *errstr = NULL;
545 *p = '\0';
546 yylval.v.number = strtonum(buf, LLONG_MIN,
547 LLONG_MAX, &errstr);
548 if (errstr) {
549 yyerror("\"%s\" invalid number: %s",
550 buf, errstr);
551 return (findeol());
553 return (NUMBER);
554 } else {
555 nodigits:
556 while (p > buf + 1)
557 lungetc((unsigned char)*--p);
558 c = (unsigned char)*--p;
559 if (c == '-')
560 return (c);
564 #define allowed_in_string(x) \
565 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
566 x != '{' && x != '}' && \
567 x != '!' && x != '=' && x != '#' && \
568 x != ','))
570 if (isalnum(c) || c == ':' || c == '_' || c == '/') {
571 do {
572 *p++ = c;
573 if ((size_t)(p-buf) >= sizeof(buf)) {
574 yyerror("string too long");
575 return (findeol());
577 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
578 lungetc(c);
579 *p = '\0';
580 if ((token = lookup(buf)) == STRING)
581 if ((yylval.v.string = strdup(buf)) == NULL)
582 fatal("yylex: strdup");
583 return (token);
585 if (c == '\n') {
586 yylval.lineno = file->lineno;
587 file->lineno++;
589 if (c == EOF)
590 return (0);
591 return (c);
594 struct file *
595 pushfile(const char *name, int secret)
597 struct file *nfile;
599 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
600 log_warn("%s", __func__);
601 return (NULL);
603 if ((nfile->name = strdup(name)) == NULL) {
604 log_warn("%s", __func__);
605 free(nfile);
606 return (NULL);
608 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
609 free(nfile->name);
610 free(nfile);
611 return (NULL);
613 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
614 nfile->ungetsize = 16;
615 nfile->ungetbuf = malloc(nfile->ungetsize);
616 if (nfile->ungetbuf == NULL) {
617 log_warn("%s", __func__);
618 fclose(nfile->stream);
619 free(nfile->name);
620 free(nfile);
621 return (NULL);
623 TAILQ_INSERT_TAIL(&files, nfile, entry);
624 return (nfile);
627 int
628 popfile(void)
630 struct file *prev;
632 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
633 prev->errors += file->errors;
635 TAILQ_REMOVE(&files, file, entry);
636 fclose(file->stream);
637 free(file->name);
638 free(file->ungetbuf);
639 free(file);
640 file = prev;
641 return (file ? 0 : EOF);
644 int
645 parse_config(const char *filename, struct galileo *env)
647 struct sym *sym, *next;
648 size_t n;
650 conf = env;
652 n = strlcpy(conf->sc_conffile, filename, sizeof(conf->sc_conffile));
653 if (n >= sizeof(conf->sc_conffile)) {
654 log_warn("path too long: %s", filename);
655 return (-1);
658 if ((file = pushfile(filename, 0)) == NULL) {
659 log_warn("failed to open %s", filename);
660 return (-1);
662 topfile = file;
663 setservent(1);
665 yyparse();
666 errors = file->errors;
667 popfile();
669 endservent();
671 /* Free macros and check which have not been used. */
672 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
673 if (!sym->used)
674 fprintf(stderr, "warning: macro `%s' not used\n",
675 sym->nam);
676 if (!sym->persist) {
677 free(sym->nam);
678 free(sym->val);
679 TAILQ_REMOVE(&symhead, sym, entry);
680 free(sym);
684 if (errors)
685 return (-1);
687 return (0);
690 int
691 symset(const char *nam, const char *val, int persist)
693 struct sym *sym;
695 TAILQ_FOREACH(sym, &symhead, entry) {
696 if (strcmp(nam, sym->nam) == 0)
697 break;
700 if (sym != NULL) {
701 if (sym->persist == 1)
702 return (0);
703 else {
704 free(sym->nam);
705 free(sym->val);
706 TAILQ_REMOVE(&symhead, sym, entry);
707 free(sym);
710 if ((sym = calloc(1, sizeof(*sym))) == NULL)
711 return (-1);
713 sym->nam = strdup(nam);
714 if (sym->nam == NULL) {
715 free(sym);
716 return (-1);
718 sym->val = strdup(val);
719 if (sym->val == NULL) {
720 free(sym->nam);
721 free(sym);
722 return (-1);
724 sym->used = 0;
725 sym->persist = persist;
726 TAILQ_INSERT_TAIL(&symhead, sym, entry);
727 return (0);
730 int
731 cmdline_symset(char *s)
733 char *sym, *val;
734 int ret;
736 if ((val = strrchr(s, '=')) == NULL)
737 return (-1);
738 sym = strndup(s, val - s);
739 if (sym == NULL)
740 fatal("%s: strndup", __func__);
741 ret = symset(sym, val + 1, 1);
742 free(sym);
744 return (ret);
747 char *
748 symget(const char *nam)
750 struct sym *sym;
752 TAILQ_FOREACH(sym, &symhead, entry) {
753 if (strcmp(nam, sym->nam) == 0) {
754 sym->used = 1;
755 return (sym->val);
758 return (NULL);
761 int
762 getservice(const char *n)
764 struct servent *s;
765 const char *errstr;
766 long long llval;
768 llval = strtonum(n, 0, UINT16_MAX, &errstr);
769 if (errstr) {
770 s = getservbyname(n, "tcp");
771 if (s == NULL)
772 s = getservbyname(n, "udp");
773 if (s == NULL)
774 return (-1);
775 return (ntohs(s->s_port));
778 return ((unsigned short)llval);