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 CHROOT CLIENT
121 %token DEFAULT
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 SNI SPAWN STRIP
131 %token TCP TOEXT TYPE TYPES
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 numberstring
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 types '\n'
152 | conf error '\n' { file->errors++; }
155 include : INCLUDE STRING {
156 struct file *nfile;
158 if ((nfile = pushfile($2, 0)) == NULL) {
159 yyerror("failed to include file %s", $2);
160 free($2);
161 YYERROR;
163 free($2);
165 file = nfile;
166 lungetc('\n');
170 bool : ON { $$ = 1; }
171 | OFF { $$ = 0; }
174 string : string STRING {
175 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
176 free($1);
177 free($2);
178 yyerror("string: asprintf: %s", strerror(errno));
179 YYERROR;
181 free($1);
182 free($2);
184 | STRING
187 numberstring : NUM {
188 char *s;
189 if (asprintf(&s, "%d", $1) == -1) {
190 yyerror("asprintf: number");
191 YYERROR;
193 $$ = s;
195 | STRING
198 varset : STRING '=' string {
199 char *s = $1;
200 while (*s++) {
201 if (isspace((unsigned char)*s)) {
202 yyerror("macro name cannot contain "
203 "whitespaces");
204 free($1);
205 free($3);
206 YYERROR;
209 symset($1, $3, 0);
210 free($1);
211 free($3);
215 option : CHROOT string {
216 if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >=
217 sizeof(conf.chroot))
218 yyerror("chroot path too long");
219 free($2);
221 | IPV6 bool { conf.ipv6 = $2; }
222 | MIME STRING string {
223 yywarn("`mime MIME EXT' is deprecated and will be "
224 "removed in a future version, please use the new "
225 "`types' block.");
226 if (add_mime(&conf.mime, $2, $3) == -1)
227 err(1, "add_mime");
229 | MAP string TOEXT string {
230 yywarn("`map mime to-ext' is deprecated and will be "
231 "removed in a future version, please use the new "
232 "`types' block.");
233 if (add_mime(&conf.mime, $2, $4) == -1)
234 err(1, "add_mime");
236 | PORT NUM { conf.port = check_port_num($2); }
237 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
238 | PROTOCOLS string {
239 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
240 yyerror("invalid protocols string \"%s\"", $2);
241 free($2);
243 | USER string {
244 if (strlcpy(conf.user, $2, sizeof(conf.user)) >=
245 sizeof(conf.user))
246 yyerror("user name too long");
247 free($2);
251 vhost : SERVER string {
252 host = new_vhost();
253 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
255 loc = new_location();
256 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
258 TAILQ_INIT(&host->proxies);
260 loc->match = xstrdup("*");
261 host->domain = $2;
263 if (strstr($2, "xn--") != NULL) {
264 yywarn("\"%s\" looks like punycode: you "
265 "should use the decoded hostname", $2);
267 } '{' optnl servbody '}' {
268 if (host->cert == NULL || host->key == NULL)
269 yyerror("invalid vhost definition: %s", $2);
271 | error '}' { yyerror("bad server directive"); }
274 servbody : /* empty */
275 | servbody servopt optnl
276 | servbody location optnl
277 | servbody proxy optnl
280 servopt : ALIAS string {
281 struct alist *a;
283 a = xcalloc(1, sizeof(*a));
284 a->alias = $2;
285 if (TAILQ_EMPTY(&host->aliases))
286 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
287 else
288 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
290 | CERT string {
291 only_once(host->cert, "cert");
292 host->cert = ensure_absolute_path($2);
294 | KEY string {
295 only_once(host->key, "key");
296 host->key = ensure_absolute_path($2);
298 | OCSP string {
299 only_once(host->ocsp, "ocsp");
300 host->ocsp = ensure_absolute_path($2);
302 | PARAM string '=' string {
303 add_param($2, $4, 0);
305 | locopt
308 proxy : PROXY { advance_proxy(); }
309 proxy_matches '{' optnl proxy_opts '}' {
310 if (proxy->host == NULL)
311 yyerror("invalid proxy block: missing `relay-to' option");
313 if ((proxy->cert == NULL && proxy->key != NULL) ||
314 (proxy->cert != NULL && proxy->key == NULL))
315 yyerror("invalid proxy block: missing cert or key");
319 proxy_matches : /* empty */
320 | proxy_matches proxy_match
323 proxy_match : PROTO string {
324 only_once(proxy->match_proto, "proxy proto");
325 free(proxy->match_proto);
326 proxy->match_proto = $2;
328 | FOR_HOST string {
329 only_once(proxy->match_host, "proxy for-host");
330 free(proxy->match_host);
331 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
335 proxy_opts : /* empty */
336 | proxy_opts proxy_opt optnl
339 proxy_opt : CERT string {
340 only_once(proxy->cert, "proxy cert");
341 tls_unload_file(proxy->cert, proxy->certlen);
342 ensure_absolute_path($2);
343 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
344 if (proxy->cert == NULL)
345 yyerror("can't load cert %s", $2);
346 free($2);
348 | KEY string {
349 only_once(proxy->key, "proxy key");
350 tls_unload_file(proxy->key, proxy->keylen);
351 ensure_absolute_path($2);
352 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
353 if (proxy->key == NULL)
354 yyerror("can't load key %s", $2);
355 free($2);
357 | PROTOCOLS string {
358 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
359 yyerror("invalid protocols string \"%s\"", $2);
360 free($2);
362 | RELAY_TO string {
363 only_once(proxy->host, "proxy relay-to");
364 free(proxy->host);
365 parsehp($2, &proxy->host, &proxy->port, "1965");
367 | REQUIRE CLIENT CA string {
368 only_once(proxy->reqca, "require client ca");
369 ensure_absolute_path($4);
370 if ((proxy->reqca = load_ca($4)) == NULL)
371 yyerror("couldn't load ca cert: %s", $4);
372 free($4);
374 | SNI string {
375 only_once(proxy->sni, "proxy sni");
376 free(proxy->sni);
377 proxy->sni = $2;
379 | USE_TLS bool {
380 proxy->notls = !$2;
382 | VERIFYNAME bool {
383 proxy->noverifyname = !$2;
387 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
388 /* drop the starting '/' if any */
389 if (*$3 == '/')
390 memmove($3, $3+1, strlen($3));
391 loc->match = $3;
393 | error '}'
396 locopts : /* empty */
397 | locopts locopt optnl
400 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
401 | BLOCK RETURN NUM string {
402 only_once(loc->block_fmt, "block");
403 loc->block_fmt = check_block_fmt($4);
404 loc->block_code = check_block_code($3);
406 | BLOCK RETURN NUM {
407 only_once(loc->block_fmt, "block");
408 loc->block_fmt = xstrdup("temporary failure");
409 loc->block_code = check_block_code($3);
410 if ($3 >= 30 && $3 < 40)
411 yyerror("missing `meta' for block return %d", $3);
413 | BLOCK {
414 only_once(loc->block_fmt, "block");
415 loc->block_fmt = xstrdup("temporary failure");
416 loc->block_code = 40;
418 | DEFAULT TYPE string {
419 only_once(loc->default_mime, "default type");
420 loc->default_mime = $3;
422 | FASTCGI fastcgi
423 | INDEX string {
424 only_once(loc->index, "index");
425 loc->index = $2;
427 | LANG string {
428 only_once(loc->lang, "lang");
429 loc->lang = $2;
431 | LOG bool { loc->disable_log = !$2; }
432 | REQUIRE CLIENT CA string {
433 only_once(loc->reqca, "require client ca");
434 ensure_absolute_path($4);
435 if ((loc->reqca = load_ca($4)) == NULL)
436 yyerror("couldn't load ca cert: %s", $4);
437 free($4);
439 | ROOT string {
440 only_once(loc->dir, "root");
441 loc->dir = ensure_absolute_path($2);
443 | STRIP NUM { loc->strip = check_strip_no($2); }
446 fastcgi : SPAWN string {
447 only_oncei(loc->fcgi, "fastcgi");
448 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
450 | string {
451 only_oncei(loc->fcgi, "fastcgi");
452 loc->fcgi = fastcgi_conf($1, NULL, NULL);
454 | TCP string PORT NUM {
455 char *c;
456 if (asprintf(&c, "%d", $4) == -1)
457 err(1, "asprintf");
458 only_oncei(loc->fcgi, "fastcgi");
459 loc->fcgi = fastcgi_conf($2, c, NULL);
461 | TCP string {
462 only_oncei(loc->fcgi, "fastcgi");
463 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
465 | TCP string PORT string {
466 only_oncei(loc->fcgi, "fastcgi");
467 loc->fcgi = fastcgi_conf($2, $4, NULL);
471 types : TYPES '{' optnl mediaopts_l '}' {
472 conf.mime.skip_defaults = 1;
476 mediaopts_l : mediaopts_l mediaoptsl nl
477 | mediaoptsl nl
480 mediaoptsl : STRING { current_media = $1; } medianames_l optsemicolon
481 | include
484 medianames_l : medianames_l medianamesl
485 | medianamesl
488 medianamesl : numberstring {
489 if (add_mime(&conf.mime, current_media, $1) == -1)
490 err(1, "add_mime");
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 {"chroot", CHROOT},
519 {"client", CLIENT},
520 {"default", DEFAULT},
521 {"fastcgi", FASTCGI},
522 {"for-host", FOR_HOST},
523 {"include", INCLUDE},
524 {"index", INDEX},
525 {"ipv6", IPV6},
526 {"key", KEY},
527 {"lang", LANG},
528 {"location", LOCATION},
529 {"log", LOG},
530 {"map", MAP},
531 {"mime", MIME},
532 {"ocsp", OCSP},
533 {"off", OFF},
534 {"on", ON},
535 {"param", PARAM},
536 {"port", PORT},
537 {"prefork", PREFORK},
538 {"proto", PROTO},
539 {"protocols", PROTOCOLS},
540 {"proxy", PROXY},
541 {"relay-to", RELAY_TO},
542 {"require", REQUIRE},
543 {"return", RETURN},
544 {"root", ROOT},
545 {"server", SERVER},
546 {"sni", SNI},
547 {"spawn", SPAWN},
548 {"strip", STRIP},
549 {"tcp", TCP},
550 {"to-ext", TOEXT},
551 {"type", TYPE},
552 {"types", TYPES},
553 {"use-tls", USE_TLS},
554 {"user", USER},
555 {"verifyname", VERIFYNAME},
556 };
558 void
559 yyerror(const char *msg, ...)
561 va_list ap;
563 file->errors++;
565 va_start(ap, msg);
566 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
567 vfprintf(stderr, msg, ap);
568 fprintf(stderr, "\n");
569 va_end(ap);
572 void
573 yywarn(const char *msg, ...)
575 va_list ap;
577 va_start(ap, msg);
578 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
579 vfprintf(stderr, msg, ap);
580 fprintf(stderr, "\n");
581 va_end(ap);
584 int
585 kw_cmp(const void *k, const void *e)
587 return strcmp(k, ((struct keyword *)e)->word);
590 int
591 lookup(char *s)
593 const struct keyword *p;
595 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
596 sizeof(keywords[0]), kw_cmp);
598 if (p)
599 return p->token;
600 else
601 return STRING;
604 #define START_EXPAND 1
605 #define DONE_EXPAND 2
607 static int expanding;
609 int
610 igetc(void)
612 int c;
614 while (1) {
615 if (file->ungetpos > 0)
616 c = file->ungetbuf[--file->ungetpos];
617 else
618 c = getc(file->stream);
620 if (c == START_EXPAND)
621 expanding = 1;
622 else if (c == DONE_EXPAND)
623 expanding = 0;
624 else
625 break;
627 return c;
630 int
631 lgetc(int quotec)
633 int c, next;
635 if (quotec) {
636 if ((c = igetc()) == EOF) {
637 yyerror("reached end of file while parsing "
638 "quoted string");
639 if (file == topfile || popfile() == EOF)
640 return EOF;
641 return quotec;
643 return c;
646 while ((c = igetc()) == '\\') {
647 next = igetc();
648 if (next != '\n') {
649 c = next;
650 break;
652 yylval.lineno = file->lineno;
653 file->lineno++;
656 if (c == EOF) {
657 /*
658 * Fake EOL when hit EOF for the first time. This gets line
659 * count right if last line in included file is syntactically
660 * invalid and has no newline.
661 */
662 if (file->eof_reached == 0) {
663 file->eof_reached = 1;
664 return '\n';
666 while (c == EOF) {
667 if (file == topfile || popfile() == EOF)
668 return EOF;
669 c = igetc();
672 return c;
675 void
676 lungetc(int c)
678 if (c == EOF)
679 return;
681 if (file->ungetpos >= file->ungetsize) {
682 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
683 if (p == NULL)
684 err(1, "lungetc");
685 file->ungetbuf = p;
686 file->ungetsize *= 2;
688 file->ungetbuf[file->ungetpos++] = c;
691 int
692 findeol(void)
694 int c;
696 /* Skip to either EOF or the first real EOL. */
697 while (1) {
698 c = lgetc(0);
699 if (c == '\n') {
700 file->lineno++;
701 break;
703 if (c == EOF)
704 break;
706 return ERROR;
709 int
710 yylex(void)
712 char buf[8096];
713 char *p, *val;
714 int quotec, next, c;
715 int token;
717 top:
718 p = buf;
719 while ((c = lgetc(0)) == ' ' || c == '\t')
720 ; /* nothing */
722 yylval.lineno = file->lineno;
723 if (c == '#')
724 while ((c = lgetc(0)) != '\n' && c != EOF)
725 ; /* nothing */
726 if (c == '$' && !expanding) {
727 while (1) {
728 if ((c = lgetc(0)) == EOF)
729 return 0;
730 if (p + 1 >= buf + sizeof(buf) -1) {
731 yyerror("string too long");
732 return findeol();
734 if (isalnum(c) || c == '_') {
735 *p++ = c;
736 continue;
738 *p = '\0';
739 lungetc(c);
740 break;
742 val = symget(buf);
743 if (val == NULL) {
744 yyerror("macro `%s' not defined", buf);
745 return findeol();
747 yylval.v.string = xstrdup(val);
748 return STRING;
750 if (c == '@' && !expanding) {
751 while (1) {
752 if ((c = lgetc(0)) == EOF)
753 return 0;
755 if (p + 1 >= buf + sizeof(buf) - 1) {
756 yyerror("string too long");
757 return findeol();
759 if (isalnum(c) || c == '_') {
760 *p++ = c;
761 continue;
763 *p = '\0';
764 lungetc(c);
765 break;
767 val = symget(buf);
768 if (val == NULL) {
769 yyerror("macro '%s' not defined", buf);
770 return findeol();
772 p = val + strlen(val) - 1;
773 lungetc(DONE_EXPAND);
774 while (p >= val) {
775 lungetc(*p);
776 p--;
778 lungetc(START_EXPAND);
779 goto top;
782 switch (c) {
783 case '\'':
784 case '"':
785 quotec = c;
786 while (1) {
787 if ((c = lgetc(quotec)) == EOF)
788 return 0;
789 if (c == '\n') {
790 file->lineno++;
791 continue;
792 } else if (c == '\\') {
793 if ((next = lgetc(quotec)) == EOF)
794 return (0);
795 if (next == quotec || next == ' ' ||
796 next == '\t')
797 c = next;
798 else if (next == '\n') {
799 file->lineno++;
800 continue;
801 } else
802 lungetc(next);
803 } else if (c == quotec) {
804 *p = '\0';
805 break;
806 } else if (c == '\0') {
807 yyerror("invalid syntax");
808 return findeol();
810 if (p + 1 >= buf + sizeof(buf) - 1) {
811 yyerror("string too long");
812 return findeol();
814 *p++ = c;
816 yylval.v.string = strdup(buf);
817 if (yylval.v.string == NULL)
818 err(1, "yylex: strdup");
819 return STRING;
822 #define allowed_to_end_number(x) \
823 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
825 if (c == '-' || isdigit(c)) {
826 do {
827 *p++ = c;
828 if ((size_t)(p-buf) >= sizeof(buf)) {
829 yyerror("string too long");
830 return findeol();
832 } while ((c = lgetc(0)) != EOF && isdigit(c));
833 lungetc(c);
834 if (p == buf + 1 && buf[0] == '-')
835 goto nodigits;
836 if (c == EOF || allowed_to_end_number(c)) {
837 const char *errstr = NULL;
839 *p = '\0';
840 yylval.v.number = strtonum(buf, LLONG_MIN,
841 LLONG_MAX, &errstr);
842 if (errstr) {
843 yyerror("\"%s\" invalid number: %s",
844 buf, errstr);
845 return findeol();
847 return NUM;
848 } else {
849 nodigits:
850 while (p > buf + 1)
851 lungetc(*--p);
852 c = *--p;
853 if (c == '-')
854 return c;
858 #define allowed_in_string(x) \
859 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
860 x != '{' && x != '}' && \
861 x != '!' && x != '=' && x != '#' && \
862 x != ',' && x != ';'))
864 if (isalnum(c) || c == ':' || c == '_') {
865 do {
866 *p++ = c;
867 if ((size_t)(p-buf) >= sizeof(buf)) {
868 yyerror("string too long");
869 return findeol();
871 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
872 lungetc(c);
873 *p = '\0';
874 if ((token = lookup(buf)) == STRING)
875 yylval.v.string = xstrdup(buf);
876 return token;
878 if (c == '\n') {
879 yylval.lineno = file->lineno;
880 file->lineno++;
882 if (c == EOF)
883 return 0;
884 return c;
887 struct file *
888 pushfile(const char *name, int secret)
890 struct file *nfile;
892 nfile = xcalloc(1, sizeof(*nfile));
893 nfile->name = xstrdup(name);
894 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
895 log_warn(NULL, "can't open %s: %s", nfile->name,
896 strerror(errno));
897 free(nfile->name);
898 free(nfile);
899 return NULL;
901 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
902 nfile->ungetsize = 16;
903 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
904 TAILQ_INSERT_TAIL(&files, nfile, entry);
905 return nfile;
908 int
909 popfile(void)
911 struct file *prev;
913 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
914 prev->errors += file->errors;
916 TAILQ_REMOVE(&files, file, entry);
917 fclose(file->stream);
918 free(file->name);
919 free(file->ungetbuf);
920 free(file);
921 file = prev;
922 return file ? 0 : EOF;
925 void
926 parse_conf(const char *filename)
928 struct sym *sym, *next;
930 file = pushfile(filename, 0);
931 if (file == NULL)
932 exit(1);
933 topfile = file;
935 yyparse();
936 errors = file->errors;
937 popfile();
939 /* Free macros and check which have not been used. */
940 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
941 /* TODO: warn if !sym->used */
942 if (!sym->persist) {
943 free(sym->name);
944 free(sym->val);
945 TAILQ_REMOVE(&symhead, sym, entry);
946 free(sym);
950 if (errors)
951 exit(1);
954 void
955 print_conf(void)
957 struct vhost *h;
958 /* struct location *l; */
959 /* struct envlist *e; */
960 /* struct alist *a; */
962 if (*conf.chroot != '\0')
963 printf("chroot \"%s\"\n", conf.chroot);
964 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
965 /* XXX: defined mimes? */
966 printf("port %d\n", conf.port);
967 printf("prefork %d\n", conf.prefork);
968 /* XXX: protocols? */
969 if (*conf.user != '\0')
970 printf("user \"%s\"\n", conf.user);
972 TAILQ_FOREACH(h, &hosts, vhosts) {
973 printf("\nserver \"%s\" {\n", h->domain);
974 printf(" cert \"%s\"\n", h->cert);
975 printf(" key \"%s\"\n", h->key);
976 /* TODO: print locations... */
977 printf("}\n");
981 int
982 symset(const char *name, const char *val, int persist)
984 struct sym *sym;
986 TAILQ_FOREACH(sym, &symhead, entry) {
987 if (!strcmp(name, sym->name))
988 break;
991 if (sym != NULL) {
992 if (sym->persist)
993 return 0;
994 else {
995 free(sym->name);
996 free(sym->val);
997 TAILQ_REMOVE(&symhead, sym, entry);
998 free(sym);
1002 sym = xcalloc(1, sizeof(*sym));
1003 sym->name = xstrdup(name);
1004 sym->val = xstrdup(val);
1005 sym->used = 0;
1006 sym->persist = persist;
1008 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1009 return 0;
1012 int
1013 cmdline_symset(char *s)
1015 char *sym, *val;
1016 int ret;
1018 if ((val = strrchr(s, '=')) == NULL)
1019 return -1;
1020 sym = xcalloc(1, val - s + 1);
1021 memcpy(sym, s, val - s);
1022 ret = symset(sym, val + 1, 1);
1023 free(sym);
1024 return ret;
1027 char *
1028 symget(const char *nam)
1030 struct sym *sym;
1032 TAILQ_FOREACH(sym, &symhead, entry) {
1033 if (strcmp(nam, sym->name) == 0) {
1034 sym->used = 1;
1035 return sym->val;
1038 return NULL;
1041 struct vhost *
1042 new_vhost(void)
1044 return xcalloc(1, sizeof(struct vhost));
1047 struct location *
1048 new_location(void)
1050 struct location *l;
1052 l = xcalloc(1, sizeof(*l));
1053 l->dirfd = -1;
1054 l->fcgi = -1;
1055 return l;
1058 struct proxy *
1059 new_proxy(void)
1061 struct proxy *p;
1063 conf.can_open_sockets = 1;
1065 p = xcalloc(1, sizeof(*p));
1066 p->protocols = TLS_PROTOCOLS_DEFAULT;
1067 return p;
1070 char *
1071 ensure_absolute_path(char *path)
1073 if (path == NULL || *path != '/')
1074 yyerror("not an absolute path: %s", path);
1075 return path;
1078 int
1079 check_block_code(int n)
1081 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1082 yyerror("invalid block code %d", n);
1083 return n;
1086 char *
1087 check_block_fmt(char *fmt)
1089 char *s;
1091 for (s = fmt; *s; ++s) {
1092 if (*s != '%')
1093 continue;
1094 switch (*++s) {
1095 case '%':
1096 case 'p':
1097 case 'q':
1098 case 'P':
1099 case 'N':
1100 break;
1101 default:
1102 yyerror("invalid format specifier %%%c", *s);
1106 return fmt;
1109 int
1110 check_strip_no(int n)
1112 if (n <= 0)
1113 yyerror("invalid strip number %d", n);
1114 return n;
1117 int
1118 check_port_num(int n)
1120 if (n <= 0 || n >= UINT16_MAX)
1121 yyerror("port number is %s: %d",
1122 n <= 0 ? "too small" : "too large",
1123 n);
1124 return n;
1127 int
1128 check_prefork_num(int n)
1130 if (n <= 0 || n >= PROC_MAX)
1131 yyerror("invalid prefork number %d", n);
1132 return n;
1135 void
1136 advance_loc(void)
1138 loc = new_location();
1139 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1142 void
1143 advance_proxy(void)
1145 proxy = new_proxy();
1146 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1149 void
1150 parsehp(char *str, char **host, const char **port, const char *def)
1152 char *at;
1153 const char *errstr;
1155 *host = str;
1157 if ((at = strchr(str, ':')) != NULL) {
1158 *at++ = '\0';
1159 *port = at;
1160 } else
1161 *port = def;
1163 strtonum(*port, 1, UINT16_MAX, &errstr);
1164 if (errstr != NULL)
1165 yyerror("port is %s: %s", errstr, *port);
1168 void
1169 only_once(const void *ptr, const char *name)
1171 if (ptr != NULL)
1172 yyerror("`%s' specified more than once", name);
1175 void
1176 only_oncei(int i, const char *name)
1178 if (i != -1)
1179 yyerror("`%s' specified more than once", name);
1182 int
1183 fastcgi_conf(char *path, char *port, char *prog)
1185 struct fcgi *f;
1186 int i;
1188 conf.can_open_sockets = 1;
1190 for (i = 0; i < FCGI_MAX; ++i) {
1191 f = &fcgi[i];
1193 if (f->path == NULL) {
1194 f->id = i;
1195 f->path = path;
1196 f->port = port;
1197 f->prog = prog;
1198 return i;
1201 /* XXX: what to do with prog? */
1202 if (!strcmp(f->path, path) &&
1203 ((port == NULL && f->port == NULL) ||
1204 !strcmp(f->port, port))) {
1205 free(path);
1206 free(port);
1207 return i;
1211 yyerror("too much `fastcgi' rules defined.");
1212 return -1;
1215 void
1216 add_param(char *name, char *val, int env)
1218 struct envlist *e;
1219 struct envhead *h;
1221 if (env)
1222 h = &host->env;
1223 else
1224 h = &host->params;
1226 e = xcalloc(1, sizeof(*e));
1227 e->name = name;
1228 e->value = val;
1229 if (TAILQ_EMPTY(h))
1230 TAILQ_INSERT_HEAD(h, e, envs);
1231 else
1232 TAILQ_INSERT_TAIL(h, e, envs);