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 "gmid.h"
28 #include <ctype.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.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 char *current_media;
103 static int errors;
105 typedef struct {
106 union {
107 char *string;
108 int number;
109 } v;
110 int lineno;
111 } YYSTYPE;
113 %}
115 /* for bison: */
116 /* %define parse.error verbose */
118 %token ALIAS AUTO
119 %token BLOCK
120 %token CA CERT CGI CHROOT CLIENT
121 %token DEFAULT
122 %token ENTRYPOINT ENV
123 %token FASTCGI FOR_HOST
124 %token INCLUDE INDEX IPV6
125 %token KEY
126 %token LANG LOCATION LOG
127 %token MAP MIME
128 %token OCSP OFF ON
129 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
130 %token RELAY_TO REQUIRE RETURN ROOT
131 %token SERVER SNI SPAWN STRIP
132 %token TCP TOEXT TYPE TYPES
133 %token USE_TLS USER
134 %token VERIFYNAME
136 %token ERROR
138 %token <v.string> STRING
139 %token <v.number> NUM
141 %type <v.number> bool
142 %type <v.string> string numberstring
144 %%
146 conf : /* empty */
147 | conf include '\n'
148 | conf '\n'
149 | conf varset '\n'
150 | conf option '\n'
151 | conf vhost '\n'
152 | conf types '\n'
153 | conf error '\n' { file->errors++; }
156 include : INCLUDE STRING {
157 struct file *nfile;
159 if ((nfile = pushfile($2, 0)) == NULL) {
160 yyerror("failed to include file %s", $2);
161 free($2);
162 YYERROR;
164 free($2);
166 file = nfile;
167 lungetc('\n');
171 bool : ON { $$ = 1; }
172 | OFF { $$ = 0; }
175 string : string STRING {
176 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
177 free($1);
178 free($2);
179 yyerror("string: asprintf: %s", strerror(errno));
180 YYERROR;
182 free($1);
183 free($2);
185 | STRING
188 numberstring : NUM {
189 char *s;
190 if (asprintf(&s, "%d", $1) == -1) {
191 yyerror("asprintf: number");
192 YYERROR;
194 $$ = s;
196 | STRING
199 varset : STRING '=' string {
200 char *s = $1;
201 while (*s++) {
202 if (isspace((unsigned char)*s)) {
203 yyerror("macro name cannot contain "
204 "whitespaces");
205 free($1);
206 free($3);
207 YYERROR;
210 symset($1, $3, 0);
211 free($1);
212 free($3);
216 option : CHROOT string { conf.chroot = $2; }
217 | IPV6 bool { conf.ipv6 = $2; }
218 | MIME STRING string {
219 yywarn("`mime MIME EXT' is deprecated and will be "
220 "removed in a future version, please use the new "
221 "`types' block.");
222 add_mime(&conf.mime, $2, $3);
224 | MAP string TOEXT string {
225 yywarn("`map mime to-ext' is deprecated and will be "
226 "removed in a future version, please use the new "
227 "`types' block.");
228 add_mime(&conf.mime, $2, $4);
230 | PORT NUM { conf.port = check_port_num($2); }
231 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
232 | PROTOCOLS string {
233 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
234 yyerror("invalid protocols string \"%s\"", $2);
235 free($2);
237 | USER string { conf.user = $2; }
240 vhost : SERVER string {
241 host = new_vhost();
242 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
244 loc = new_location();
245 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
247 TAILQ_INIT(&host->proxies);
249 loc->match = xstrdup("*");
250 host->domain = $2;
252 if (strstr($2, "xn--") != NULL) {
253 yywarn("\"%s\" looks like punycode: you "
254 "should use the decoded hostname", $2);
256 } '{' optnl servbody '}' {
257 if (host->cert == NULL || host->key == NULL)
258 yyerror("invalid vhost definition: %s", $2);
260 | error '}' { yyerror("bad server directive"); }
263 servbody : /* empty */
264 | servbody servopt optnl
265 | servbody location optnl
266 | servbody proxy optnl
269 servopt : ALIAS string {
270 struct alist *a;
272 a = xcalloc(1, sizeof(*a));
273 a->alias = $2;
274 if (TAILQ_EMPTY(&host->aliases))
275 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
276 else
277 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
279 | CERT string {
280 only_once(host->cert, "cert");
281 host->cert = ensure_absolute_path($2);
283 | CGI string {
284 only_once(host->cgi, "cgi");
285 /* drop the starting '/', if any */
286 if (*$2 == '/')
287 memmove($2, $2+1, strlen($2));
288 host->cgi = $2;
290 | ENTRYPOINT string {
291 only_once(host->entrypoint, "entrypoint");
292 while (*$2 == '/')
293 memmove($2, $2+1, strlen($2));
294 host->entrypoint = $2;
296 | ENV string '=' string {
297 add_param($2, $4, 1);
299 | KEY string {
300 only_once(host->key, "key");
301 host->key = ensure_absolute_path($2);
303 | OCSP string {
304 only_once(host->ocsp, "ocsp");
305 host->ocsp = ensure_absolute_path($2);
307 | PARAM string '=' string {
308 add_param($2, $4, 0);
310 | locopt
313 proxy : PROXY { advance_proxy(); }
314 proxy_matches '{' optnl proxy_opts '}' {
315 if (proxy->host == NULL)
316 yyerror("invalid proxy block: missing `relay-to' option");
318 if ((proxy->cert == NULL && proxy->key != NULL) ||
319 (proxy->cert != NULL && proxy->key == NULL))
320 yyerror("invalid proxy block: missing cert or key");
324 proxy_matches : /* empty */
325 | proxy_matches proxy_match
328 proxy_match : PROTO string {
329 only_once(proxy->match_proto, "proxy proto");
330 free(proxy->match_proto);
331 proxy->match_proto = $2;
333 | FOR_HOST string {
334 only_once(proxy->match_host, "proxy for-host");
335 free(proxy->match_host);
336 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
340 proxy_opts : /* empty */
341 | proxy_opts proxy_opt optnl
344 proxy_opt : CERT string {
345 only_once(proxy->cert, "proxy cert");
346 tls_unload_file(proxy->cert, proxy->certlen);
347 ensure_absolute_path($2);
348 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
349 if (proxy->cert == NULL)
350 yyerror("can't load cert %s", $2);
351 free($2);
353 | KEY string {
354 only_once(proxy->key, "proxy key");
355 tls_unload_file(proxy->key, proxy->keylen);
356 ensure_absolute_path($2);
357 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
358 if (proxy->key == NULL)
359 yyerror("can't load key %s", $2);
360 free($2);
362 | PROTOCOLS string {
363 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
364 yyerror("invalid protocols string \"%s\"", $2);
365 free($2);
367 | RELAY_TO string {
368 only_once(proxy->host, "proxy relay-to");
369 free(proxy->host);
370 parsehp($2, &proxy->host, &proxy->port, "1965");
372 | REQUIRE CLIENT CA string {
373 only_once(proxy->reqca, "require client ca");
374 ensure_absolute_path($4);
375 if ((proxy->reqca = load_ca($4)) == NULL)
376 yyerror("couldn't load ca cert: %s", $4);
377 free($4);
379 | SNI string {
380 only_once(proxy->sni, "proxy sni");
381 free(proxy->sni);
382 proxy->sni = $2;
384 | USE_TLS bool {
385 proxy->notls = !$2;
387 | VERIFYNAME bool {
388 proxy->noverifyname = !$2;
392 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
393 /* drop the starting '/' if any */
394 if (*$3 == '/')
395 memmove($3, $3+1, strlen($3));
396 loc->match = $3;
398 | error '}'
401 locopts : /* empty */
402 | locopts locopt optnl
405 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
406 | BLOCK RETURN NUM string {
407 only_once(loc->block_fmt, "block");
408 loc->block_fmt = check_block_fmt($4);
409 loc->block_code = check_block_code($3);
411 | BLOCK RETURN NUM {
412 only_once(loc->block_fmt, "block");
413 loc->block_fmt = xstrdup("temporary failure");
414 loc->block_code = check_block_code($3);
415 if ($3 >= 30 && $3 < 40)
416 yyerror("missing `meta' for block return %d", $3);
418 | BLOCK {
419 only_once(loc->block_fmt, "block");
420 loc->block_fmt = xstrdup("temporary failure");
421 loc->block_code = 40;
423 | DEFAULT TYPE string {
424 only_once(loc->default_mime, "default type");
425 loc->default_mime = $3;
427 | FASTCGI fastcgi
428 | INDEX string {
429 only_once(loc->index, "index");
430 loc->index = $2;
432 | LANG string {
433 only_once(loc->lang, "lang");
434 loc->lang = $2;
436 | LOG bool { loc->disable_log = !$2; }
437 | REQUIRE CLIENT CA string {
438 only_once(loc->reqca, "require client ca");
439 ensure_absolute_path($4);
440 if ((loc->reqca = load_ca($4)) == NULL)
441 yyerror("couldn't load ca cert: %s", $4);
442 free($4);
444 | ROOT string {
445 only_once(loc->dir, "root");
446 loc->dir = ensure_absolute_path($2);
448 | STRIP NUM { loc->strip = check_strip_no($2); }
451 fastcgi : SPAWN string {
452 only_oncei(loc->fcgi, "fastcgi");
453 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
455 | string {
456 only_oncei(loc->fcgi, "fastcgi");
457 loc->fcgi = fastcgi_conf($1, NULL, NULL);
459 | TCP string PORT NUM {
460 char *c;
461 if (asprintf(&c, "%d", $4) == -1)
462 err(1, "asprintf");
463 only_oncei(loc->fcgi, "fastcgi");
464 loc->fcgi = fastcgi_conf($2, c, NULL);
466 | TCP string {
467 only_oncei(loc->fcgi, "fastcgi");
468 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
470 | TCP string PORT string {
471 only_oncei(loc->fcgi, "fastcgi");
472 loc->fcgi = fastcgi_conf($2, $4, NULL);
476 types : TYPES '{' optnl mediaopts_l '}'
479 mediaopts_l : mediaopts_l mediaoptsl nl
480 | mediaoptsl nl
483 mediaoptsl : STRING { current_media = $1; } medianames_l optsemicolon
484 | include
487 medianames_l : medianames_l medianamesl
488 | medianamesl
491 medianamesl : numberstring { add_mime(&conf.mime, current_media, $1); }
494 nl : '\n' optnl
497 optnl : '\n' optnl /* zero or more newlines */
498 | ';' optnl /* semicolons too */
499 | /*empty*/
502 optsemicolon : ';'
506 %%
508 static const struct keyword {
509 const char *word;
510 int token;
511 } keywords[] = {
512 /* these MUST be sorted */
513 {"alias", ALIAS},
514 {"auto", AUTO},
515 {"block", BLOCK},
516 {"ca", CA},
517 {"cert", CERT},
518 {"cgi", CGI},
519 {"chroot", CHROOT},
520 {"client", CLIENT},
521 {"default", DEFAULT},
522 {"entrypoint", ENTRYPOINT},
523 {"env", ENV},
524 {"fastcgi", FASTCGI},
525 {"for-host", FOR_HOST},
526 {"include", INCLUDE},
527 {"index", INDEX},
528 {"ipv6", IPV6},
529 {"key", KEY},
530 {"lang", LANG},
531 {"location", LOCATION},
532 {"log", LOG},
533 {"map", MAP},
534 {"mime", MIME},
535 {"ocsp", OCSP},
536 {"off", OFF},
537 {"on", ON},
538 {"param", PARAM},
539 {"port", PORT},
540 {"prefork", PREFORK},
541 {"proto", PROTO},
542 {"protocols", PROTOCOLS},
543 {"proxy", PROXY},
544 {"relay-to", RELAY_TO},
545 {"require", REQUIRE},
546 {"return", RETURN},
547 {"root", ROOT},
548 {"server", SERVER},
549 {"sni", SNI},
550 {"spawn", SPAWN},
551 {"strip", STRIP},
552 {"tcp", TCP},
553 {"to-ext", TOEXT},
554 {"type", TYPE},
555 {"types", TYPES},
556 {"use-tls", USE_TLS},
557 {"user", USER},
558 {"verifyname", VERIFYNAME},
559 };
561 void
562 yyerror(const char *msg, ...)
564 va_list ap;
566 file->errors++;
568 va_start(ap, msg);
569 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
570 vfprintf(stderr, msg, ap);
571 fprintf(stderr, "\n");
572 va_end(ap);
575 void
576 yywarn(const char *msg, ...)
578 va_list ap;
580 va_start(ap, msg);
581 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
582 vfprintf(stderr, msg, ap);
583 fprintf(stderr, "\n");
584 va_end(ap);
587 int
588 kw_cmp(const void *k, const void *e)
590 return strcmp(k, ((struct keyword *)e)->word);
593 int
594 lookup(char *s)
596 const struct keyword *p;
598 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
599 sizeof(keywords[0]), kw_cmp);
601 if (p)
602 return p->token;
603 else
604 return STRING;
607 #define START_EXPAND 1
608 #define DONE_EXPAND 2
610 static int expanding;
612 int
613 igetc(void)
615 int c;
617 while (1) {
618 if (file->ungetpos > 0)
619 c = file->ungetbuf[--file->ungetpos];
620 else
621 c = getc(file->stream);
623 if (c == START_EXPAND)
624 expanding = 1;
625 else if (c == DONE_EXPAND)
626 expanding = 0;
627 else
628 break;
630 return c;
633 int
634 lgetc(int quotec)
636 int c, next;
638 if (quotec) {
639 if ((c = igetc()) == EOF) {
640 yyerror("reached end of file while parsing "
641 "quoted string");
642 if (file == topfile || popfile() == EOF)
643 return EOF;
644 return quotec;
646 return c;
649 while ((c = igetc()) == '\\') {
650 next = igetc();
651 if (next != '\n') {
652 c = next;
653 break;
655 yylval.lineno = file->lineno;
656 file->lineno++;
659 if (c == EOF) {
660 /*
661 * Fake EOL when hit EOF for the first time. This gets line
662 * count right if last line in included file is syntactically
663 * invalid and has no newline.
664 */
665 if (file->eof_reached == 0) {
666 file->eof_reached = 1;
667 return '\n';
669 while (c == EOF) {
670 if (file == topfile || popfile() == EOF)
671 return EOF;
672 c = igetc();
675 return c;
678 void
679 lungetc(int c)
681 if (c == EOF)
682 return;
684 if (file->ungetpos >= file->ungetsize) {
685 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
686 if (p == NULL)
687 err(1, "lungetc");
688 file->ungetbuf = p;
689 file->ungetsize *= 2;
691 file->ungetbuf[file->ungetpos++] = c;
694 int
695 findeol(void)
697 int c;
699 /* Skip to either EOF or the first real EOL. */
700 while (1) {
701 c = lgetc(0);
702 if (c == '\n') {
703 file->lineno++;
704 break;
706 if (c == EOF)
707 break;
709 return ERROR;
712 int
713 yylex(void)
715 char buf[8096];
716 char *p, *val;
717 int quotec, next, c;
718 int token;
720 top:
721 p = buf;
722 while ((c = lgetc(0)) == ' ' || c == '\t')
723 ; /* nothing */
725 yylval.lineno = file->lineno;
726 if (c == '#')
727 while ((c = lgetc(0)) != '\n' && c != EOF)
728 ; /* nothing */
729 if (c == '$' && !expanding) {
730 while (1) {
731 if ((c = lgetc(0)) == EOF)
732 return 0;
733 if (p + 1 >= buf + sizeof(buf) -1) {
734 yyerror("string too long");
735 return findeol();
737 if (isalnum(c) || c == '_') {
738 *p++ = c;
739 continue;
741 *p = '\0';
742 lungetc(c);
743 break;
745 val = symget(buf);
746 if (val == NULL) {
747 yyerror("macro `%s' not defined", buf);
748 return findeol();
750 yylval.v.string = xstrdup(val);
751 return STRING;
753 if (c == '@' && !expanding) {
754 while (1) {
755 if ((c = lgetc(0)) == EOF)
756 return 0;
758 if (p + 1 >= buf + sizeof(buf) - 1) {
759 yyerror("string too long");
760 return findeol();
762 if (isalnum(c) || c == '_') {
763 *p++ = c;
764 continue;
766 *p = '\0';
767 lungetc(c);
768 break;
770 val = symget(buf);
771 if (val == NULL) {
772 yyerror("macro '%s' not defined", buf);
773 return findeol();
775 p = val + strlen(val) - 1;
776 lungetc(DONE_EXPAND);
777 while (p >= val) {
778 lungetc(*p);
779 p--;
781 lungetc(START_EXPAND);
782 goto top;
785 switch (c) {
786 case '\'':
787 case '"':
788 quotec = c;
789 while (1) {
790 if ((c = lgetc(quotec)) == EOF)
791 return 0;
792 if (c == '\n') {
793 file->lineno++;
794 continue;
795 } else if (c == '\\') {
796 if ((next = lgetc(quotec)) == EOF)
797 return (0);
798 if (next == quotec || next == ' ' ||
799 next == '\t')
800 c = next;
801 else if (next == '\n') {
802 file->lineno++;
803 continue;
804 } else
805 lungetc(next);
806 } else if (c == quotec) {
807 *p = '\0';
808 break;
809 } else if (c == '\0') {
810 yyerror("invalid syntax");
811 return findeol();
813 if (p + 1 >= buf + sizeof(buf) - 1) {
814 yyerror("string too long");
815 return findeol();
817 *p++ = c;
819 yylval.v.string = strdup(buf);
820 if (yylval.v.string == NULL)
821 err(1, "yylex: strdup");
822 return STRING;
825 #define allowed_to_end_number(x) \
826 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
828 if (c == '-' || isdigit(c)) {
829 do {
830 *p++ = c;
831 if ((size_t)(p-buf) >= sizeof(buf)) {
832 yyerror("string too long");
833 return findeol();
835 } while ((c = lgetc(0)) != EOF && isdigit(c));
836 lungetc(c);
837 if (p == buf + 1 && buf[0] == '-')
838 goto nodigits;
839 if (c == EOF || allowed_to_end_number(c)) {
840 const char *errstr = NULL;
842 *p = '\0';
843 yylval.v.number = strtonum(buf, LLONG_MIN,
844 LLONG_MAX, &errstr);
845 if (errstr) {
846 yyerror("\"%s\" invalid number: %s",
847 buf, errstr);
848 return findeol();
850 return NUM;
851 } else {
852 nodigits:
853 while (p > buf + 1)
854 lungetc(*--p);
855 c = *--p;
856 if (c == '-')
857 return c;
861 #define allowed_in_string(x) \
862 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
863 x != '{' && x != '}' && \
864 x != '!' && x != '=' && x != '#' && \
865 x != ',' && x != ';'))
867 if (isalnum(c) || c == ':' || c == '_') {
868 do {
869 *p++ = c;
870 if ((size_t)(p-buf) >= sizeof(buf)) {
871 yyerror("string too long");
872 return findeol();
874 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
875 lungetc(c);
876 *p = '\0';
877 if ((token = lookup(buf)) == STRING)
878 yylval.v.string = xstrdup(buf);
879 return token;
881 if (c == '\n') {
882 yylval.lineno = file->lineno;
883 file->lineno++;
885 if (c == EOF)
886 return 0;
887 return c;
890 struct file *
891 pushfile(const char *name, int secret)
893 struct file *nfile;
895 nfile = xcalloc(1, sizeof(*nfile));
896 nfile->name = xstrdup(name);
897 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
898 log_warn(NULL, "can't open %s: %s", nfile->name,
899 strerror(errno));
900 free(nfile->name);
901 free(nfile);
902 return NULL;
904 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
905 nfile->ungetsize = 16;
906 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
907 TAILQ_INSERT_TAIL(&files, nfile, entry);
908 return nfile;
911 int
912 popfile(void)
914 struct file *prev;
916 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
917 prev->errors += file->errors;
919 TAILQ_REMOVE(&files, file, entry);
920 fclose(file->stream);
921 free(file->name);
922 free(file->ungetbuf);
923 free(file);
924 file = prev;
925 return file ? 0 : EOF;
928 void
929 parse_conf(const char *filename)
931 struct sym *sym, *next;
933 file = pushfile(filename, 0);
934 if (file == NULL)
935 exit(1);
936 topfile = file;
938 yyparse();
939 errors = file->errors;
940 popfile();
942 /* Free macros and check which have not been used. */
943 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
944 /* TODO: warn if !sym->used */
945 if (!sym->persist) {
946 free(sym->name);
947 free(sym->val);
948 TAILQ_REMOVE(&symhead, sym, entry);
949 free(sym);
953 if (errors)
954 exit(1);
957 void
958 print_conf(void)
960 struct vhost *h;
961 /* struct location *l; */
962 /* struct envlist *e; */
963 /* struct alist *a; */
965 if (conf.chroot != NULL)
966 printf("chroot \"%s\"\n", conf.chroot);
967 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
968 /* XXX: defined mimes? */
969 printf("port %d\n", conf.port);
970 printf("prefork %d\n", conf.prefork);
971 /* XXX: protocols? */
972 if (conf.user != NULL)
973 printf("user \"%s\"\n", conf.user);
975 TAILQ_FOREACH(h, &hosts, vhosts) {
976 printf("\nserver \"%s\" {\n", h->domain);
977 printf(" cert \"%s\"\n", h->cert);
978 printf(" key \"%s\"\n", h->key);
979 /* TODO: print locations... */
980 printf("}\n");
984 int
985 symset(const char *name, const char *val, int persist)
987 struct sym *sym;
989 TAILQ_FOREACH(sym, &symhead, entry) {
990 if (!strcmp(name, sym->name))
991 break;
994 if (sym != NULL) {
995 if (sym->persist)
996 return 0;
997 else {
998 free(sym->name);
999 free(sym->val);
1000 TAILQ_REMOVE(&symhead, sym, entry);
1001 free(sym);
1005 sym = xcalloc(1, sizeof(*sym));
1006 sym->name = xstrdup(name);
1007 sym->val = xstrdup(val);
1008 sym->used = 0;
1009 sym->persist = persist;
1011 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1012 return 0;
1015 int
1016 cmdline_symset(char *s)
1018 char *sym, *val;
1019 int ret;
1021 if ((val = strrchr(s, '=')) == NULL)
1022 return -1;
1023 sym = xcalloc(1, val - s + 1);
1024 memcpy(sym, s, val - s);
1025 ret = symset(sym, val + 1, 1);
1026 free(sym);
1027 return ret;
1030 char *
1031 symget(const char *nam)
1033 struct sym *sym;
1035 TAILQ_FOREACH(sym, &symhead, entry) {
1036 if (strcmp(nam, sym->name) == 0) {
1037 sym->used = 1;
1038 return sym->val;
1041 return NULL;
1044 struct vhost *
1045 new_vhost(void)
1047 return xcalloc(1, sizeof(struct vhost));
1050 struct location *
1051 new_location(void)
1053 struct location *l;
1055 l = xcalloc(1, sizeof(*l));
1056 l->dirfd = -1;
1057 l->fcgi = -1;
1058 return l;
1061 struct proxy *
1062 new_proxy(void)
1064 struct proxy *p;
1066 p = xcalloc(1, sizeof(*p));
1067 p->protocols = TLS_PROTOCOLS_DEFAULT;
1068 return p;
1071 char *
1072 ensure_absolute_path(char *path)
1074 if (path == NULL || *path != '/')
1075 yyerror("not an absolute path: %s", path);
1076 return path;
1079 int
1080 check_block_code(int n)
1082 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1083 yyerror("invalid block code %d", n);
1084 return n;
1087 char *
1088 check_block_fmt(char *fmt)
1090 char *s;
1092 for (s = fmt; *s; ++s) {
1093 if (*s != '%')
1094 continue;
1095 switch (*++s) {
1096 case '%':
1097 case 'p':
1098 case 'q':
1099 case 'P':
1100 case 'N':
1101 break;
1102 default:
1103 yyerror("invalid format specifier %%%c", *s);
1107 return fmt;
1110 int
1111 check_strip_no(int n)
1113 if (n <= 0)
1114 yyerror("invalid strip number %d", n);
1115 return n;
1118 int
1119 check_port_num(int n)
1121 if (n <= 0 || n >= UINT16_MAX)
1122 yyerror("port number is %s: %d",
1123 n <= 0 ? "too small" : "too large",
1124 n);
1125 return n;
1128 int
1129 check_prefork_num(int n)
1131 if (n <= 0 || n >= PROC_MAX)
1132 yyerror("invalid prefork number %d", n);
1133 return n;
1136 void
1137 advance_loc(void)
1139 loc = new_location();
1140 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1143 void
1144 advance_proxy(void)
1146 proxy = new_proxy();
1147 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1150 void
1151 parsehp(char *str, char **host, const char **port, const char *def)
1153 char *at;
1154 const char *errstr;
1156 *host = str;
1158 if ((at = strchr(str, ':')) != NULL) {
1159 *at++ = '\0';
1160 *port = at;
1161 } else
1162 *port = def;
1164 strtonum(*port, 1, UINT16_MAX, &errstr);
1165 if (errstr != NULL)
1166 yyerror("port is %s: %s", errstr, *port);
1169 void
1170 only_once(const void *ptr, const char *name)
1172 if (ptr != NULL)
1173 yyerror("`%s' specified more than once", name);
1176 void
1177 only_oncei(int i, const char *name)
1179 if (i != -1)
1180 yyerror("`%s' specified more than once", name);
1183 int
1184 fastcgi_conf(char *path, char *port, char *prog)
1186 struct fcgi *f;
1187 int i;
1189 for (i = 0; i < FCGI_MAX; ++i) {
1190 f = &fcgi[i];
1192 if (f->path == NULL) {
1193 f->id = i;
1194 f->path = path;
1195 f->port = port;
1196 f->prog = prog;
1197 return i;
1200 /* XXX: what to do with prog? */
1201 if (!strcmp(f->path, path) &&
1202 ((port == NULL && f->port == NULL) ||
1203 !strcmp(f->port, port))) {
1204 free(path);
1205 free(port);
1206 return i;
1210 yyerror("too much `fastcgi' rules defined.");
1211 return -1;
1214 void
1215 add_param(char *name, char *val, int env)
1217 struct envlist *e;
1218 struct envhead *h;
1220 if (env)
1221 h = &host->env;
1222 else
1223 h = &host->params;
1225 e = xcalloc(1, sizeof(*e));
1226 e->name = name;
1227 e->value = val;
1228 if (TAILQ_EMPTY(h))
1229 TAILQ_INSERT_HEAD(h, e, envs);
1230 else
1231 TAILQ_INSERT_TAIL(h, e, envs);