Blob


1 %{
3 /*
4 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
5 * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
6 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
7 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
8 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
9 * Copyright (c) 2001 Markus Friedl. All rights reserved.
10 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
11 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
12 *
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 */
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #include "gmid.h"
35 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
36 static struct file {
37 TAILQ_ENTRY(file) entry;
38 FILE *stream;
39 char *name;
40 size_t ungetpos;
41 size_t ungetsize;
42 u_char *ungetbuf;
43 int eof_reached;
44 int lineno;
45 int errors;
46 } *file, *topfile;
48 struct file *pushfile(const char *, int);
49 int popfile(void);
50 int yyparse(void);
51 int yylex(void);
52 void yyerror(const char *, ...)
53 __attribute__((__format__ (printf, 1, 2)))
54 __attribute__((__nonnull__ (1)));
55 void yywarn(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 int kw_cmp(const void *, const void *);
59 int lookup(char *);
60 int igetc(void);
61 int lgetc(int);
62 void lungetc(int);
63 int findeol(void);
65 /*
66 * #define YYDEBUG 1
67 * int yydebug = 1;
68 */
70 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
71 struct sym {
72 TAILQ_ENTRY(sym) entry;
73 int used;
74 int persist;
75 char *name;
76 char *val;
77 };
79 int symset(const char *, const char *, int);
80 char *symget(const char *);
82 struct vhost *new_vhost(void);
83 struct location *new_location(void);
84 struct proxy *new_proxy(void);
85 char *ensure_absolute_path(char*);
86 int check_block_code(int);
87 char *check_block_fmt(char*);
88 int check_strip_no(int);
89 int check_port_num(int);
90 int check_prefork_num(int);
91 void advance_loc(void);
92 void advance_proxy(void);
93 void parsehp(char *, char **, const char **, const char *);
94 void only_once(const void*, const char*);
95 void only_oncei(int, const char*);
96 int fastcgi_conf(char *, char *, char *);
97 void add_param(char *, char *, int);
99 static struct vhost *host;
100 static struct location *loc;
101 static struct proxy *proxy;
102 static int errors;
104 typedef struct {
105 union {
106 char *string;
107 int number;
108 } v;
109 int lineno;
110 } YYSTYPE;
112 %}
114 /* for bison: */
115 /* %define parse.error verbose */
117 %token ALIAS AUTO
118 %token BLOCK
119 %token CA CERT CGI CHROOT CLIENT
120 %token DEFAULT
121 %token ENTRYPOINT ENV
122 %token FASTCGI FOR_HOST
123 %token INCLUDE INDEX IPV6
124 %token KEY
125 %token LANG LOCATION LOG
126 %token MAP MIME
127 %token OCSP OFF ON
128 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
129 %token RELAY_TO REQUIRE RETURN ROOT
130 %token SERVER SPAWN STRIP
131 %token TCP TOEXT TYPE
132 %token USE_TLS USER
133 %token VERIFYNAME
135 %token ERROR
137 %token <v.string> STRING
138 %token <v.number> NUM
140 %type <v.number> bool
141 %type <v.string> string
143 %%
145 conf : /* empty */
146 | conf include '\n'
147 | conf '\n'
148 | conf varset '\n'
149 | conf option '\n'
150 | conf vhost '\n'
151 | conf error '\n' { file->errors++; }
154 include : INCLUDE STRING {
155 struct file *nfile;
157 if ((nfile = pushfile($2, 0)) == NULL) {
158 yyerror("failed to include file %s", $2);
159 free($2);
160 YYERROR;
162 free($2);
164 file = nfile;
165 lungetc('\n');
169 bool : ON { $$ = 1; }
170 | OFF { $$ = 0; }
173 string : string STRING {
174 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
175 free($1);
176 free($2);
177 yyerror("string: asprintf: %s", strerror(errno));
178 YYERROR;
180 free($1);
181 free($2);
183 | STRING
186 varset : STRING '=' string {
187 char *s = $1;
188 while (*s++) {
189 if (isspace((unsigned char)*s)) {
190 yyerror("macro name cannot contain "
191 "whitespaces");
192 free($1);
193 free($3);
194 YYERROR;
197 symset($1, $3, 0);
198 free($1);
199 free($3);
203 option : CHROOT string { conf.chroot = $2; }
204 | IPV6 bool { conf.ipv6 = $2; }
205 | MIME STRING string {
206 yywarn("`mime MIME EXT' is deprecated and will be "
207 "removed in a future version, please use the new "
208 "syntax: `map MIME to-ext EXT'");
209 add_mime(&conf.mime, $2, $3);
211 | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
212 | PORT NUM { conf.port = check_port_num($2); }
213 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
214 | PROTOCOLS string {
215 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
216 yyerror("invalid protocols string \"%s\"", $2);
217 free($2);
219 | USER string { conf.user = $2; }
222 vhost : SERVER string {
223 host = new_vhost();
224 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
226 loc = new_location();
227 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
229 TAILQ_INIT(&host->proxies);
231 loc->match = xstrdup("*");
232 host->domain = $2;
234 if (strstr($2, "xn--") != NULL) {
235 yywarn("\"%s\" looks like punycode: you "
236 "should use the decoded hostname", $2);
238 } '{' optnl servbody '}' {
239 if (host->cert == NULL || host->key == NULL)
240 yyerror("invalid vhost definition: %s", $2);
242 | error '}' { yyerror("bad server directive"); }
245 servbody : /* empty */
246 | servbody servopt optnl
247 | servbody location optnl
248 | servbody proxy optnl
251 servopt : ALIAS string {
252 struct alist *a;
254 a = xcalloc(1, sizeof(*a));
255 a->alias = $2;
256 if (TAILQ_EMPTY(&host->aliases))
257 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
258 else
259 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
261 | CERT string {
262 only_once(host->cert, "cert");
263 host->cert = ensure_absolute_path($2);
265 | CGI string {
266 only_once(host->cgi, "cgi");
267 /* drop the starting '/', if any */
268 if (*$2 == '/')
269 memmove($2, $2+1, strlen($2));
270 host->cgi = $2;
272 | ENTRYPOINT string {
273 only_once(host->entrypoint, "entrypoint");
274 while (*$2 == '/')
275 memmove($2, $2+1, strlen($2));
276 host->entrypoint = $2;
278 | ENV string '=' string {
279 add_param($2, $4, 1);
281 | KEY string {
282 only_once(host->key, "key");
283 host->key = ensure_absolute_path($2);
285 | OCSP string {
286 only_once(host->ocsp, "ocsp");
287 host->ocsp = ensure_absolute_path($2);
289 | PARAM string '=' string {
290 add_param($2, $4, 0);
292 | locopt
295 proxy : PROXY { advance_proxy(); }
296 proxy_matches '{' optnl proxy_opts '}' {
297 if (proxy->host == NULL)
298 yyerror("invalid proxy block: missing `relay-to' option");
300 if ((proxy->cert == NULL && proxy->key != NULL) ||
301 (proxy->cert != NULL && proxy->key == NULL))
302 yyerror("invalid proxy block: missing cert or key");
306 proxy_matches : /* empty */
307 | proxy_matches proxy_match
310 proxy_match : PROTO string {
311 only_once(proxy->match_proto, "proxy proto");
312 free(proxy->match_proto);
313 proxy->match_proto = $2;
315 | FOR_HOST string {
316 only_once(proxy->match_host, "proxy for-host");
317 free(proxy->match_host);
318 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
322 proxy_opts : /* empty */
323 | proxy_opts proxy_opt optnl
326 proxy_opt : CERT string {
327 only_once(proxy->cert, "proxy cert");
328 tls_unload_file(proxy->cert, proxy->certlen);
329 ensure_absolute_path($2);
330 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
331 if (proxy->cert == NULL)
332 yyerror("can't load cert %s", $2);
333 free($2);
335 | KEY string {
336 only_once(proxy->key, "proxy key");
337 tls_unload_file(proxy->key, proxy->keylen);
338 ensure_absolute_path($2);
339 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
340 if (proxy->key == NULL)
341 yyerror("can't load key %s", $2);
342 free($2);
344 | PROTOCOLS string {
345 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
346 yyerror("invalid protocols string \"%s\"", $2);
347 free($2);
349 | RELAY_TO string {
350 only_once(proxy->host, "proxy relay-to");
351 free(proxy->host);
352 parsehp($2, &proxy->host, &proxy->port, "1965");
354 | REQUIRE CLIENT CA string {
355 only_once(proxy->reqca, "require client ca");
356 ensure_absolute_path($4);
357 if ((proxy->reqca = load_ca($4)) == NULL)
358 yyerror("couldn't load ca cert: %s", $4);
359 free($4);
361 | USE_TLS bool {
362 proxy->notls = !$2;
364 | VERIFYNAME bool {
365 proxy->noverifyname = !$2;
369 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
370 /* drop the starting '/' if any */
371 if (*$3 == '/')
372 memmove($3, $3+1, strlen($3));
373 loc->match = $3;
375 | error '}'
378 locopts : /* empty */
379 | locopts locopt optnl
382 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
383 | BLOCK RETURN NUM string {
384 only_once(loc->block_fmt, "block");
385 loc->block_fmt = check_block_fmt($4);
386 loc->block_code = check_block_code($3);
388 | BLOCK RETURN NUM {
389 only_once(loc->block_fmt, "block");
390 loc->block_fmt = xstrdup("temporary failure");
391 loc->block_code = check_block_code($3);
392 if ($3 >= 30 && $3 < 40)
393 yyerror("missing `meta' for block return %d", $3);
395 | BLOCK {
396 only_once(loc->block_fmt, "block");
397 loc->block_fmt = xstrdup("temporary failure");
398 loc->block_code = 40;
400 | DEFAULT TYPE string {
401 only_once(loc->default_mime, "default type");
402 loc->default_mime = $3;
404 | FASTCGI fastcgi
405 | INDEX string {
406 only_once(loc->index, "index");
407 loc->index = $2;
409 | LANG string {
410 only_once(loc->lang, "lang");
411 loc->lang = $2;
413 | LOG bool { loc->disable_log = !$2; }
414 | REQUIRE CLIENT CA string {
415 only_once(loc->reqca, "require client ca");
416 ensure_absolute_path($4);
417 if ((loc->reqca = load_ca($4)) == NULL)
418 yyerror("couldn't load ca cert: %s", $4);
419 free($4);
421 | ROOT string {
422 only_once(loc->dir, "root");
423 loc->dir = ensure_absolute_path($2);
425 | STRIP NUM { loc->strip = check_strip_no($2); }
428 fastcgi : SPAWN string {
429 only_oncei(loc->fcgi, "fastcgi");
430 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
432 | string {
433 only_oncei(loc->fcgi, "fastcgi");
434 loc->fcgi = fastcgi_conf($1, NULL, NULL);
436 | TCP string PORT NUM {
437 char *c;
438 if (asprintf(&c, "%d", $4) == -1)
439 err(1, "asprintf");
440 only_oncei(loc->fcgi, "fastcgi");
441 loc->fcgi = fastcgi_conf($2, c, NULL);
443 | TCP string {
444 only_oncei(loc->fcgi, "fastcgi");
445 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
447 | TCP string PORT string {
448 only_oncei(loc->fcgi, "fastcgi");
449 loc->fcgi = fastcgi_conf($2, $4, NULL);
453 optnl : '\n' optnl /* zero or more newlines */
454 | ';' optnl /* semicolons too */
455 | /*empty*/
458 %%
460 static struct keyword {
461 const char *word;
462 int token;
463 } keywords[] = {
464 /* these MUST be sorted */
465 {"alias", ALIAS},
466 {"auto", AUTO},
467 {"block", BLOCK},
468 {"ca", CA},
469 {"cert", CERT},
470 {"cgi", CGI},
471 {"chroot", CHROOT},
472 {"client", CLIENT},
473 {"default", DEFAULT},
474 {"entrypoint", ENTRYPOINT},
475 {"env", ENV},
476 {"fastcgi", FASTCGI},
477 {"for-host", FOR_HOST},
478 {"index", INDEX},
479 {"ipv6", IPV6},
480 {"key", KEY},
481 {"lang", LANG},
482 {"location", LOCATION},
483 {"log", LOG},
484 {"map", MAP},
485 {"mime", MIME},
486 {"ocsp", OCSP},
487 {"off", OFF},
488 {"on", ON},
489 {"param", PARAM},
490 {"port", PORT},
491 {"prefork", PREFORK},
492 {"proto", PROTO},
493 {"protocols", PROTOCOLS},
494 {"proxy", PROXY},
495 {"relay-to", RELAY_TO},
496 {"require", REQUIRE},
497 {"return", RETURN},
498 {"root", ROOT},
499 {"server", SERVER},
500 {"spawn", SPAWN},
501 {"strip", STRIP},
502 {"tcp", TCP},
503 {"to-ext", TOEXT},
504 {"type", TYPE},
505 {"use-tls", USE_TLS},
506 {"user", USER},
507 {"verifyname", VERIFYNAME},
508 };
510 void
511 yyerror(const char *msg, ...)
513 va_list ap;
515 file->errors++;
517 va_start(ap, msg);
518 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
519 vfprintf(stderr, msg, ap);
520 fprintf(stderr, "\n");
521 va_end(ap);
524 void
525 yywarn(const char *msg, ...)
527 va_list ap;
529 va_start(ap, msg);
530 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
531 vfprintf(stderr, msg, ap);
532 fprintf(stderr, "\n");
533 va_end(ap);
536 int
537 kw_cmp(const void *k, const void *e)
539 return strcmp(k, ((struct keyword *)e)->word);
542 int
543 lookup(char *s)
545 const struct keyword *p;
547 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
548 sizeof(keywords[0]), kw_cmp);
550 if (p)
551 return p->token;
552 else
553 return STRING;
556 #define START_EXPAND 1
557 #define DONE_EXPAND 2
559 static int expanding;
561 int
562 igetc(void)
564 int c;
566 while (1) {
567 if (file->ungetpos > 0)
568 c = file->ungetbuf[--file->ungetpos];
569 else
570 c = getc(file->stream);
572 if (c == START_EXPAND)
573 expanding = 1;
574 else if (c == DONE_EXPAND)
575 expanding = 0;
576 else
577 break;
579 return c;
582 int
583 lgetc(int quotec)
585 int c, next;
587 if (quotec) {
588 if ((c = igetc()) == EOF) {
589 yyerror("reached end of file while parsing "
590 "quoted string");
591 if (file == topfile || popfile() == EOF)
592 return EOF;
593 return quotec;
595 return c;
598 while ((c = igetc()) == '\\') {
599 next = igetc();
600 if (next != '\n') {
601 c = next;
602 break;
604 yylval.lineno = file->lineno;
605 file->lineno++;
608 if (c == EOF) {
609 /*
610 * Fake EOL when hit EOF for the first time. This gets line
611 * count right if last line in included file is syntactically
612 * invalid and has no newline.
613 */
614 if (file->eof_reached == 0) {
615 file->eof_reached = 1;
616 return '\n';
618 while (c == EOF) {
619 if (file == topfile || popfile() == EOF)
620 return EOF;
621 c = igetc();
624 return c;
627 void
628 lungetc(int c)
630 if (c == EOF)
631 return;
633 if (file->ungetpos >= file->ungetsize) {
634 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
635 if (p == NULL)
636 err(1, "lungetc");
637 file->ungetbuf = p;
638 file->ungetsize *= 2;
640 file->ungetbuf[file->ungetpos++] = c;
643 int
644 findeol(void)
646 int c;
648 /* Skip to either EOF or the first real EOL. */
649 while (1) {
650 c = lgetc(0);
651 if (c == '\n') {
652 file->lineno++;
653 break;
655 if (c == EOF)
656 break;
658 return ERROR;
661 int
662 yylex(void)
664 char buf[8096];
665 char *p, *val;
666 int quotec, next, c;
667 int token;
669 top:
670 p = buf;
671 while ((c = lgetc(0)) == ' ' || c == '\t')
672 ; /* nothing */
674 yylval.lineno = file->lineno;
675 if (c == '#')
676 while ((c = lgetc(0)) != '\n' && c != EOF)
677 ; /* nothing */
678 if (c == '$' && !expanding) {
679 while (1) {
680 if ((c = lgetc(0)) == EOF)
681 return 0;
682 if (p + 1 >= buf + sizeof(buf) -1) {
683 yyerror("string too long");
684 return findeol();
686 if (isalnum(c) || c == '_') {
687 *p++ = c;
688 continue;
690 *p = '\0';
691 lungetc(c);
692 break;
694 val = symget(buf);
695 if (val == NULL) {
696 yyerror("macro `%s' not defined", buf);
697 return findeol();
699 yylval.v.string = xstrdup(val);
700 return STRING;
702 if (c == '@' && !expanding) {
703 while (1) {
704 if ((c = lgetc(0)) == EOF)
705 return 0;
707 if (p + 1 >= buf + sizeof(buf) - 1) {
708 yyerror("string too long");
709 return findeol();
711 if (isalnum(c) || c == '_') {
712 *p++ = c;
713 continue;
715 *p = '\0';
716 lungetc(c);
717 break;
719 val = symget(buf);
720 if (val == NULL) {
721 yyerror("macro '%s' not defined", buf);
722 return findeol();
724 p = val + strlen(val) - 1;
725 lungetc(DONE_EXPAND);
726 while (p >= val) {
727 lungetc(*p);
728 p--;
730 lungetc(START_EXPAND);
731 goto top;
734 switch (c) {
735 case '\'':
736 case '"':
737 quotec = c;
738 while (1) {
739 if ((c = lgetc(quotec)) == EOF)
740 return 0;
741 if (c == '\n') {
742 file->lineno++;
743 continue;
744 } else if (c == '\\') {
745 if ((next = lgetc(quotec)) == EOF)
746 return (0);
747 if (next == quotec || next == ' ' ||
748 next == '\t')
749 c = next;
750 else if (next == '\n') {
751 file->lineno++;
752 continue;
753 } else
754 lungetc(next);
755 } else if (c == quotec) {
756 *p = '\0';
757 break;
758 } else if (c == '\0') {
759 yyerror("invalid syntax");
760 return findeol();
762 if (p + 1 >= buf + sizeof(buf) - 1) {
763 yyerror("string too long");
764 return findeol();
766 *p++ = c;
768 yylval.v.string = strdup(buf);
769 if (yylval.v.string == NULL)
770 err(1, "yylex: strdup");
771 return STRING;
774 #define allowed_to_end_number(x) \
775 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
777 if (c == '-' || isdigit(c)) {
778 do {
779 *p++ = c;
780 if ((size_t)(p-buf) >= sizeof(buf)) {
781 yyerror("string too long");
782 return findeol();
784 } while ((c = lgetc(0)) != EOF && isdigit(c));
785 lungetc(c);
786 if (p == buf + 1 && buf[0] == '-')
787 goto nodigits;
788 if (c == EOF || allowed_to_end_number(c)) {
789 const char *errstr = NULL;
791 *p = '\0';
792 yylval.v.number = strtonum(buf, LLONG_MIN,
793 LLONG_MAX, &errstr);
794 if (errstr) {
795 yyerror("\"%s\" invalid number: %s",
796 buf, errstr);
797 return findeol();
799 return NUM;
800 } else {
801 nodigits:
802 while (p > buf + 1)
803 lungetc(*--p);
804 c = *--p;
805 if (c == '-')
806 return c;
810 #define allowed_in_string(x) \
811 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
812 x != '{' && x != '}' && \
813 x != '!' && x != '=' && x != '#' && \
814 x != ',' && x != ';'))
816 if (isalnum(c) || c == ':' || c == '_') {
817 do {
818 *p++ = c;
819 if ((size_t)(p-buf) >= sizeof(buf)) {
820 yyerror("string too long");
821 return findeol();
823 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
824 lungetc(c);
825 *p = '\0';
826 if ((token = lookup(buf)) == STRING)
827 yylval.v.string = xstrdup(buf);
828 return token;
830 if (c == '\n') {
831 yylval.lineno = file->lineno;
832 file->lineno++;
834 if (c == EOF)
835 return 0;
836 return c;
839 struct file *
840 pushfile(const char *name, int secret)
842 struct file *nfile;
844 nfile = xcalloc(1, sizeof(*nfile));
845 nfile->name = xstrdup(name);
846 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
847 log_warn(NULL, "can't open %s: %s", nfile->name,
848 strerror(errno));
849 free(nfile->name);
850 free(nfile);
851 return NULL;
853 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
854 nfile->ungetsize = 16;
855 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
856 TAILQ_INSERT_TAIL(&files, nfile, entry);
857 return nfile;
860 int
861 popfile(void)
863 struct file *prev;
865 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
866 prev->errors += file->errors;
868 TAILQ_REMOVE(&files, file, entry);
869 fclose(file->stream);
870 free(file->name);
871 free(file->ungetbuf);
872 free(file);
873 file = prev;
874 return file ? 0 : EOF;
877 void
878 parse_conf(const char *filename)
880 struct sym *sym, *next;
882 file = pushfile(filename, 0);
883 if (file == NULL)
884 exit(1);
885 topfile = file;
887 yyparse();
888 errors = file->errors;
889 popfile();
891 /* Free macros and check which have not been used. */
892 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
893 /* TODO: warn if !sym->used */
894 if (!sym->persist) {
895 free(sym->name);
896 free(sym->val);
897 TAILQ_REMOVE(&symhead, sym, entry);
898 free(sym);
902 if (errors)
903 exit(1);
906 void
907 print_conf(void)
909 struct vhost *h;
910 /* struct location *l; */
911 /* struct envlist *e; */
912 /* struct alist *a; */
914 if (conf.chroot != NULL)
915 printf("chroot \"%s\"\n", conf.chroot);
916 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
917 /* XXX: defined mimes? */
918 printf("port %d\n", conf.port);
919 printf("prefork %d\n", conf.prefork);
920 /* XXX: protocols? */
921 if (conf.user != NULL)
922 printf("user \"%s\"\n", conf.user);
924 TAILQ_FOREACH(h, &hosts, vhosts) {
925 printf("\nserver \"%s\" {\n", h->domain);
926 printf(" cert \"%s\"\n", h->cert);
927 printf(" key \"%s\"\n", h->key);
928 /* TODO: print locations... */
929 printf("}\n");
933 int
934 symset(const char *name, const char *val, int persist)
936 struct sym *sym;
938 TAILQ_FOREACH(sym, &symhead, entry) {
939 if (!strcmp(name, sym->name))
940 break;
943 if (sym != NULL) {
944 if (sym->persist)
945 return 0;
946 else {
947 free(sym->name);
948 free(sym->val);
949 TAILQ_REMOVE(&symhead, sym, entry);
950 free(sym);
954 sym = xcalloc(1, sizeof(*sym));
955 sym->name = xstrdup(name);
956 sym->val = xstrdup(val);
957 sym->used = 0;
958 sym->persist = persist;
960 TAILQ_INSERT_TAIL(&symhead, sym, entry);
961 return 0;
964 int
965 cmdline_symset(char *s)
967 char *sym, *val;
968 int ret;
970 if ((val = strrchr(s, '=')) == NULL)
971 return -1;
972 sym = xcalloc(1, val - s + 1);
973 memcpy(sym, s, val - s);
974 ret = symset(sym, val + 1, 1);
975 free(sym);
976 return ret;
979 char *
980 symget(const char *nam)
982 struct sym *sym;
984 TAILQ_FOREACH(sym, &symhead, entry) {
985 if (strcmp(nam, sym->name) == 0) {
986 sym->used = 1;
987 return sym->val;
990 return NULL;
993 struct vhost *
994 new_vhost(void)
996 return xcalloc(1, sizeof(struct vhost));
999 struct location *
1000 new_location(void)
1002 struct location *l;
1004 l = xcalloc(1, sizeof(*l));
1005 l->dirfd = -1;
1006 l->fcgi = -1;
1007 return l;
1010 struct proxy *
1011 new_proxy(void)
1013 struct proxy *p;
1015 p = xcalloc(1, sizeof(*p));
1016 p->protocols = TLS_PROTOCOLS_DEFAULT;
1017 return p;
1020 char *
1021 ensure_absolute_path(char *path)
1023 if (path == NULL || *path != '/')
1024 yyerror("not an absolute path: %s", path);
1025 return path;
1028 int
1029 check_block_code(int n)
1031 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1032 yyerror("invalid block code %d", n);
1033 return n;
1036 char *
1037 check_block_fmt(char *fmt)
1039 char *s;
1041 for (s = fmt; *s; ++s) {
1042 if (*s != '%')
1043 continue;
1044 switch (*++s) {
1045 case '%':
1046 case 'p':
1047 case 'q':
1048 case 'P':
1049 case 'N':
1050 break;
1051 default:
1052 yyerror("invalid format specifier %%%c", *s);
1056 return fmt;
1059 int
1060 check_strip_no(int n)
1062 if (n <= 0)
1063 yyerror("invalid strip number %d", n);
1064 return n;
1067 int
1068 check_port_num(int n)
1070 if (n <= 0 || n >= UINT16_MAX)
1071 yyerror("port number is %s: %d",
1072 n <= 0 ? "too small" : "too large",
1073 n);
1074 return n;
1077 int
1078 check_prefork_num(int n)
1080 if (n <= 0 || n >= PROC_MAX)
1081 yyerror("invalid prefork number %d", n);
1082 return n;
1085 void
1086 advance_loc(void)
1088 loc = new_location();
1089 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1092 void
1093 advance_proxy(void)
1095 proxy = new_proxy();
1096 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1099 void
1100 parsehp(char *str, char **host, const char **port, const char *def)
1102 char *at;
1103 const char *errstr;
1105 *host = str;
1107 if ((at = strchr(str, ':')) != NULL) {
1108 *at++ = '\0';
1109 *port = at;
1110 } else
1111 *port = def;
1113 strtonum(*port, 1, UINT16_MAX, &errstr);
1114 if (errstr != NULL)
1115 yyerror("port is %s: %s", errstr, *port);
1118 void
1119 only_once(const void *ptr, const char *name)
1121 if (ptr != NULL)
1122 yyerror("`%s' specified more than once", name);
1125 void
1126 only_oncei(int i, const char *name)
1128 if (i != -1)
1129 yyerror("`%s' specified more than once", name);
1132 int
1133 fastcgi_conf(char *path, char *port, char *prog)
1135 struct fcgi *f;
1136 int i;
1138 for (i = 0; i < FCGI_MAX; ++i) {
1139 f = &fcgi[i];
1141 if (f->path == NULL) {
1142 f->id = i;
1143 f->path = path;
1144 f->port = port;
1145 f->prog = prog;
1146 return i;
1149 /* XXX: what to do with prog? */
1150 if (!strcmp(f->path, path) &&
1151 ((port == NULL && f->port == NULL) ||
1152 !strcmp(f->port, port))) {
1153 free(path);
1154 free(port);
1155 return i;
1159 yyerror("too much `fastcgi' rules defined.");
1160 return -1;
1163 void
1164 add_param(char *name, char *val, int env)
1166 struct envlist *e;
1167 struct envhead *h;
1169 if (env)
1170 h = &host->env;
1171 else
1172 h = &host->params;
1174 e = xcalloc(1, sizeof(*e));
1175 e->name = name;
1176 e->value = val;
1177 if (TAILQ_EMPTY(h))
1178 TAILQ_INSERT_HEAD(h, e, envs);
1179 else
1180 TAILQ_INSERT_TAIL(h, e, envs);