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 *);
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 OCSP OFF ON
127 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
128 %token RELAY_TO REQUIRE RETURN ROOT
129 %token SERVER SNI SPAWN STRIP
130 %token TCP TOEXT TYPE TYPES
131 %token USE_TLS USER
132 %token VERIFYNAME
134 %token ERROR
136 %token <v.string> STRING
137 %token <v.number> NUM
139 %type <v.number> bool
140 %type <v.string> string numberstring
142 %%
144 conf : /* empty */
145 | conf include '\n'
146 | conf '\n'
147 | conf varset '\n'
148 | conf option '\n'
149 | conf vhost '\n'
150 | conf types '\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 numberstring : NUM {
187 char *s;
188 if (asprintf(&s, "%d", $1) == -1) {
189 yyerror("asprintf: number");
190 YYERROR;
192 $$ = s;
194 | STRING
197 varset : STRING '=' string {
198 char *s = $1;
199 while (*s++) {
200 if (isspace((unsigned char)*s)) {
201 yyerror("macro name cannot contain "
202 "whitespaces");
203 free($1);
204 free($3);
205 YYERROR;
208 symset($1, $3, 0);
209 free($1);
210 free($3);
214 option : CHROOT string {
215 if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >=
216 sizeof(conf.chroot))
217 yyerror("chroot path too long");
218 free($2);
220 | IPV6 bool { conf.ipv6 = $2; }
221 | PORT NUM { conf.port = check_port_num($2); }
222 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
223 | PROTOCOLS string {
224 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
225 yyerror("invalid protocols string \"%s\"", $2);
226 free($2);
228 | USER string {
229 if (strlcpy(conf.user, $2, sizeof(conf.user)) >=
230 sizeof(conf.user))
231 yyerror("user name too long");
232 free($2);
236 vhost : SERVER string {
237 host = new_vhost();
238 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
240 loc = new_location();
241 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
243 TAILQ_INIT(&host->proxies);
245 loc->match = xstrdup("*");
246 host->domain = $2;
248 if (strstr($2, "xn--") != NULL) {
249 yywarn("\"%s\" looks like punycode: you "
250 "should use the decoded hostname", $2);
252 } '{' optnl servbody '}' {
253 if (host->cert == NULL || host->key == NULL)
254 yyerror("invalid vhost definition: %s", $2);
256 | error '}' { yyerror("bad server directive"); }
259 servbody : /* empty */
260 | servbody servopt optnl
261 | servbody location optnl
262 | servbody proxy optnl
265 servopt : ALIAS string {
266 struct alist *a;
268 a = xcalloc(1, sizeof(*a));
269 a->alias = $2;
270 if (TAILQ_EMPTY(&host->aliases))
271 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
272 else
273 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
275 | CERT string {
276 only_once(host->cert, "cert");
277 host->cert = ensure_absolute_path($2);
279 | KEY string {
280 only_once(host->key, "key");
281 host->key = ensure_absolute_path($2);
283 | OCSP string {
284 only_once(host->ocsp, "ocsp");
285 host->ocsp = ensure_absolute_path($2);
287 | PARAM string '=' string {
288 add_param($2, $4);
290 | locopt
293 proxy : PROXY { advance_proxy(); }
294 proxy_matches '{' optnl proxy_opts '}' {
295 if (proxy->host == NULL)
296 yyerror("invalid proxy block: missing `relay-to' option");
298 if ((proxy->cert == NULL && proxy->key != NULL) ||
299 (proxy->cert != NULL && proxy->key == NULL))
300 yyerror("invalid proxy block: missing cert or key");
304 proxy_matches : /* empty */
305 | proxy_matches proxy_match
308 proxy_match : PROTO string {
309 only_once(proxy->match_proto, "proxy proto");
310 free(proxy->match_proto);
311 proxy->match_proto = $2;
313 | FOR_HOST string {
314 only_once(proxy->match_host, "proxy for-host");
315 free(proxy->match_host);
316 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
320 proxy_opts : /* empty */
321 | proxy_opts proxy_opt optnl
324 proxy_opt : CERT string {
325 only_once(proxy->cert, "proxy cert");
326 tls_unload_file(proxy->cert, proxy->certlen);
327 ensure_absolute_path($2);
328 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
329 if (proxy->cert == NULL)
330 yyerror("can't load cert %s", $2);
331 free($2);
333 | KEY string {
334 only_once(proxy->key, "proxy key");
335 tls_unload_file(proxy->key, proxy->keylen);
336 ensure_absolute_path($2);
337 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
338 if (proxy->key == NULL)
339 yyerror("can't load key %s", $2);
340 free($2);
342 | PROTOCOLS string {
343 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
344 yyerror("invalid protocols string \"%s\"", $2);
345 free($2);
347 | RELAY_TO string {
348 only_once(proxy->host, "proxy relay-to");
349 free(proxy->host);
350 parsehp($2, &proxy->host, &proxy->port, "1965");
352 | REQUIRE CLIENT CA string {
353 only_once(proxy->reqca, "require client ca");
354 ensure_absolute_path($4);
355 if ((proxy->reqca = load_ca($4)) == NULL)
356 yyerror("couldn't load ca cert: %s", $4);
357 free($4);
359 | SNI string {
360 only_once(proxy->sni, "proxy sni");
361 free(proxy->sni);
362 proxy->sni = $2;
364 | USE_TLS bool {
365 proxy->notls = !$2;
367 | VERIFYNAME bool {
368 proxy->noverifyname = !$2;
372 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
373 /* drop the starting '/' if any */
374 if (*$3 == '/')
375 memmove($3, $3+1, strlen($3));
376 loc->match = $3;
378 | error '}'
381 locopts : /* empty */
382 | locopts locopt optnl
385 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
386 | BLOCK RETURN NUM string {
387 only_once(loc->block_fmt, "block");
388 loc->block_fmt = check_block_fmt($4);
389 loc->block_code = check_block_code($3);
391 | BLOCK RETURN NUM {
392 only_once(loc->block_fmt, "block");
393 loc->block_fmt = xstrdup("temporary failure");
394 loc->block_code = check_block_code($3);
395 if ($3 >= 30 && $3 < 40)
396 yyerror("missing `meta' for block return %d", $3);
398 | BLOCK {
399 only_once(loc->block_fmt, "block");
400 loc->block_fmt = xstrdup("temporary failure");
401 loc->block_code = 40;
403 | DEFAULT TYPE string {
404 only_once(loc->default_mime, "default type");
405 loc->default_mime = $3;
407 | FASTCGI fastcgi
408 | INDEX string {
409 only_once(loc->index, "index");
410 loc->index = $2;
412 | LANG string {
413 only_once(loc->lang, "lang");
414 loc->lang = $2;
416 | LOG bool { loc->disable_log = !$2; }
417 | REQUIRE CLIENT CA string {
418 only_once(loc->reqca, "require client ca");
419 ensure_absolute_path($4);
420 if ((loc->reqca = load_ca($4)) == NULL)
421 yyerror("couldn't load ca cert: %s", $4);
422 free($4);
424 | ROOT string {
425 only_once(loc->dir, "root");
426 loc->dir = ensure_absolute_path($2);
428 | STRIP NUM { loc->strip = check_strip_no($2); }
431 fastcgi : SPAWN string {
432 only_oncei(loc->fcgi, "fastcgi");
433 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
435 | string {
436 only_oncei(loc->fcgi, "fastcgi");
437 loc->fcgi = fastcgi_conf($1, NULL, NULL);
439 | TCP string PORT NUM {
440 char *c;
441 if (asprintf(&c, "%d", $4) == -1)
442 err(1, "asprintf");
443 only_oncei(loc->fcgi, "fastcgi");
444 loc->fcgi = fastcgi_conf($2, c, NULL);
446 | TCP string {
447 only_oncei(loc->fcgi, "fastcgi");
448 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
450 | TCP string PORT string {
451 only_oncei(loc->fcgi, "fastcgi");
452 loc->fcgi = fastcgi_conf($2, $4, NULL);
456 types : TYPES '{' optnl mediaopts_l '}' ;
458 mediaopts_l : mediaopts_l mediaoptsl nl
459 | mediaoptsl nl
462 mediaoptsl : STRING {
463 free(current_media);
464 current_media = $1;
465 } medianames_l optsemicolon
466 | include
469 medianames_l : medianames_l medianamesl
470 | medianamesl
473 medianamesl : numberstring {
474 if (add_mime(&conf.mime, current_media, $1) == -1)
475 err(1, "add_mime");
476 free($1);
480 nl : '\n' optnl
483 optnl : '\n' optnl /* zero or more newlines */
484 | ';' optnl /* semicolons too */
485 | /*empty*/
488 optsemicolon : ';'
492 %%
494 static const struct keyword {
495 const char *word;
496 int token;
497 } keywords[] = {
498 /* these MUST be sorted */
499 {"alias", ALIAS},
500 {"auto", AUTO},
501 {"block", BLOCK},
502 {"ca", CA},
503 {"cert", CERT},
504 {"chroot", CHROOT},
505 {"client", CLIENT},
506 {"default", DEFAULT},
507 {"fastcgi", FASTCGI},
508 {"for-host", FOR_HOST},
509 {"include", INCLUDE},
510 {"index", INDEX},
511 {"ipv6", IPV6},
512 {"key", KEY},
513 {"lang", LANG},
514 {"location", LOCATION},
515 {"log", LOG},
516 {"ocsp", OCSP},
517 {"off", OFF},
518 {"on", ON},
519 {"param", PARAM},
520 {"port", PORT},
521 {"prefork", PREFORK},
522 {"proto", PROTO},
523 {"protocols", PROTOCOLS},
524 {"proxy", PROXY},
525 {"relay-to", RELAY_TO},
526 {"require", REQUIRE},
527 {"return", RETURN},
528 {"root", ROOT},
529 {"server", SERVER},
530 {"sni", SNI},
531 {"spawn", SPAWN},
532 {"strip", STRIP},
533 {"tcp", TCP},
534 {"to-ext", TOEXT},
535 {"type", TYPE},
536 {"types", TYPES},
537 {"use-tls", USE_TLS},
538 {"user", USER},
539 {"verifyname", VERIFYNAME},
540 };
542 void
543 yyerror(const char *msg, ...)
545 va_list ap;
547 file->errors++;
549 va_start(ap, msg);
550 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
551 vfprintf(stderr, msg, ap);
552 fprintf(stderr, "\n");
553 va_end(ap);
556 void
557 yywarn(const char *msg, ...)
559 va_list ap;
561 va_start(ap, msg);
562 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
563 vfprintf(stderr, msg, ap);
564 fprintf(stderr, "\n");
565 va_end(ap);
568 int
569 kw_cmp(const void *k, const void *e)
571 return strcmp(k, ((struct keyword *)e)->word);
574 int
575 lookup(char *s)
577 const struct keyword *p;
579 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
580 sizeof(keywords[0]), kw_cmp);
582 if (p)
583 return p->token;
584 else
585 return STRING;
588 #define START_EXPAND 1
589 #define DONE_EXPAND 2
591 static int expanding;
593 int
594 igetc(void)
596 int c;
598 while (1) {
599 if (file->ungetpos > 0)
600 c = file->ungetbuf[--file->ungetpos];
601 else
602 c = getc(file->stream);
604 if (c == START_EXPAND)
605 expanding = 1;
606 else if (c == DONE_EXPAND)
607 expanding = 0;
608 else
609 break;
611 return c;
614 int
615 lgetc(int quotec)
617 int c, next;
619 if (quotec) {
620 if ((c = igetc()) == EOF) {
621 yyerror("reached end of file while parsing "
622 "quoted string");
623 if (file == topfile || popfile() == EOF)
624 return EOF;
625 return quotec;
627 return c;
630 while ((c = igetc()) == '\\') {
631 next = igetc();
632 if (next != '\n') {
633 c = next;
634 break;
636 yylval.lineno = file->lineno;
637 file->lineno++;
640 if (c == EOF) {
641 /*
642 * Fake EOL when hit EOF for the first time. This gets line
643 * count right if last line in included file is syntactically
644 * invalid and has no newline.
645 */
646 if (file->eof_reached == 0) {
647 file->eof_reached = 1;
648 return '\n';
650 while (c == EOF) {
651 if (file == topfile || popfile() == EOF)
652 return EOF;
653 c = igetc();
656 return c;
659 void
660 lungetc(int c)
662 if (c == EOF)
663 return;
665 if (file->ungetpos >= file->ungetsize) {
666 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
667 if (p == NULL)
668 err(1, "lungetc");
669 file->ungetbuf = p;
670 file->ungetsize *= 2;
672 file->ungetbuf[file->ungetpos++] = c;
675 int
676 findeol(void)
678 int c;
680 /* Skip to either EOF or the first real EOL. */
681 while (1) {
682 c = lgetc(0);
683 if (c == '\n') {
684 file->lineno++;
685 break;
687 if (c == EOF)
688 break;
690 return ERROR;
693 int
694 yylex(void)
696 char buf[8096];
697 char *p, *val;
698 int quotec, next, c;
699 int token;
701 top:
702 p = buf;
703 while ((c = lgetc(0)) == ' ' || c == '\t')
704 ; /* nothing */
706 yylval.lineno = file->lineno;
707 if (c == '#')
708 while ((c = lgetc(0)) != '\n' && c != EOF)
709 ; /* nothing */
710 if (c == '$' && !expanding) {
711 while (1) {
712 if ((c = lgetc(0)) == EOF)
713 return 0;
714 if (p + 1 >= buf + sizeof(buf) -1) {
715 yyerror("string too long");
716 return findeol();
718 if (isalnum(c) || c == '_') {
719 *p++ = c;
720 continue;
722 *p = '\0';
723 lungetc(c);
724 break;
726 val = symget(buf);
727 if (val == NULL) {
728 yyerror("macro `%s' not defined", buf);
729 return findeol();
731 yylval.v.string = xstrdup(val);
732 return STRING;
734 if (c == '@' && !expanding) {
735 while (1) {
736 if ((c = lgetc(0)) == EOF)
737 return 0;
739 if (p + 1 >= buf + sizeof(buf) - 1) {
740 yyerror("string too long");
741 return findeol();
743 if (isalnum(c) || c == '_') {
744 *p++ = c;
745 continue;
747 *p = '\0';
748 lungetc(c);
749 break;
751 val = symget(buf);
752 if (val == NULL) {
753 yyerror("macro '%s' not defined", buf);
754 return findeol();
756 p = val + strlen(val) - 1;
757 lungetc(DONE_EXPAND);
758 while (p >= val) {
759 lungetc(*p);
760 p--;
762 lungetc(START_EXPAND);
763 goto top;
766 switch (c) {
767 case '\'':
768 case '"':
769 quotec = c;
770 while (1) {
771 if ((c = lgetc(quotec)) == EOF)
772 return 0;
773 if (c == '\n') {
774 file->lineno++;
775 continue;
776 } else if (c == '\\') {
777 if ((next = lgetc(quotec)) == EOF)
778 return (0);
779 if (next == quotec || next == ' ' ||
780 next == '\t')
781 c = next;
782 else if (next == '\n') {
783 file->lineno++;
784 continue;
785 } else
786 lungetc(next);
787 } else if (c == quotec) {
788 *p = '\0';
789 break;
790 } else if (c == '\0') {
791 yyerror("invalid syntax");
792 return findeol();
794 if (p + 1 >= buf + sizeof(buf) - 1) {
795 yyerror("string too long");
796 return findeol();
798 *p++ = c;
800 yylval.v.string = strdup(buf);
801 if (yylval.v.string == NULL)
802 err(1, "yylex: strdup");
803 return STRING;
806 #define allowed_to_end_number(x) \
807 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
809 if (c == '-' || isdigit(c)) {
810 do {
811 *p++ = c;
812 if ((size_t)(p-buf) >= sizeof(buf)) {
813 yyerror("string too long");
814 return findeol();
816 } while ((c = lgetc(0)) != EOF && isdigit(c));
817 lungetc(c);
818 if (p == buf + 1 && buf[0] == '-')
819 goto nodigits;
820 if (c == EOF || allowed_to_end_number(c)) {
821 const char *errstr = NULL;
823 *p = '\0';
824 yylval.v.number = strtonum(buf, LLONG_MIN,
825 LLONG_MAX, &errstr);
826 if (errstr) {
827 yyerror("\"%s\" invalid number: %s",
828 buf, errstr);
829 return findeol();
831 return NUM;
832 } else {
833 nodigits:
834 while (p > buf + 1)
835 lungetc(*--p);
836 c = *--p;
837 if (c == '-')
838 return c;
842 #define allowed_in_string(x) \
843 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
844 x != '{' && x != '}' && \
845 x != '!' && x != '=' && x != '#' && \
846 x != ',' && x != ';'))
848 if (isalnum(c) || c == ':' || c == '_') {
849 do {
850 *p++ = c;
851 if ((size_t)(p-buf) >= sizeof(buf)) {
852 yyerror("string too long");
853 return findeol();
855 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
856 lungetc(c);
857 *p = '\0';
858 if ((token = lookup(buf)) == STRING)
859 yylval.v.string = xstrdup(buf);
860 return token;
862 if (c == '\n') {
863 yylval.lineno = file->lineno;
864 file->lineno++;
866 if (c == EOF)
867 return 0;
868 return c;
871 struct file *
872 pushfile(const char *name, int secret)
874 struct file *nfile;
876 nfile = xcalloc(1, sizeof(*nfile));
877 nfile->name = xstrdup(name);
878 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
879 log_warn(NULL, "can't open %s: %s", nfile->name,
880 strerror(errno));
881 free(nfile->name);
882 free(nfile);
883 return NULL;
885 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
886 nfile->ungetsize = 16;
887 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
888 TAILQ_INSERT_TAIL(&files, nfile, entry);
889 return nfile;
892 int
893 popfile(void)
895 struct file *prev;
897 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
898 prev->errors += file->errors;
900 TAILQ_REMOVE(&files, file, entry);
901 fclose(file->stream);
902 free(file->name);
903 free(file->ungetbuf);
904 free(file);
905 file = prev;
906 return file ? 0 : EOF;
909 void
910 parse_conf(const char *filename)
912 struct sym *sym, *next;
914 file = pushfile(filename, 0);
915 if (file == NULL)
916 exit(1);
917 topfile = file;
919 yyparse();
920 errors = file->errors;
921 popfile();
923 /* Free macros and check which have not been used. */
924 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
925 /* TODO: warn if !sym->used */
926 if (!sym->persist) {
927 free(sym->name);
928 free(sym->val);
929 TAILQ_REMOVE(&symhead, sym, entry);
930 free(sym);
934 if (errors)
935 exit(1);
938 void
939 print_conf(void)
941 struct vhost *h;
942 /* struct location *l; */
943 /* struct envlist *e; */
944 /* struct alist *a; */
946 if (*conf.chroot != '\0')
947 printf("chroot \"%s\"\n", conf.chroot);
948 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
949 /* XXX: defined mimes? */
950 printf("port %d\n", conf.port);
951 printf("prefork %d\n", conf.prefork);
952 /* XXX: protocols? */
953 if (*conf.user != '\0')
954 printf("user \"%s\"\n", conf.user);
956 TAILQ_FOREACH(h, &hosts, vhosts) {
957 printf("\nserver \"%s\" {\n", h->domain);
958 printf(" cert \"%s\"\n", h->cert);
959 printf(" key \"%s\"\n", h->key);
960 /* TODO: print locations... */
961 printf("}\n");
965 int
966 symset(const char *name, const char *val, int persist)
968 struct sym *sym;
970 TAILQ_FOREACH(sym, &symhead, entry) {
971 if (!strcmp(name, sym->name))
972 break;
975 if (sym != NULL) {
976 if (sym->persist)
977 return 0;
978 else {
979 free(sym->name);
980 free(sym->val);
981 TAILQ_REMOVE(&symhead, sym, entry);
982 free(sym);
986 sym = xcalloc(1, sizeof(*sym));
987 sym->name = xstrdup(name);
988 sym->val = xstrdup(val);
989 sym->used = 0;
990 sym->persist = persist;
992 TAILQ_INSERT_TAIL(&symhead, sym, entry);
993 return 0;
996 int
997 cmdline_symset(char *s)
999 char *sym, *val;
1000 int ret;
1002 if ((val = strrchr(s, '=')) == NULL)
1003 return -1;
1004 sym = xcalloc(1, val - s + 1);
1005 memcpy(sym, s, val - s);
1006 ret = symset(sym, val + 1, 1);
1007 free(sym);
1008 return ret;
1011 char *
1012 symget(const char *nam)
1014 struct sym *sym;
1016 TAILQ_FOREACH(sym, &symhead, entry) {
1017 if (strcmp(nam, sym->name) == 0) {
1018 sym->used = 1;
1019 return sym->val;
1022 return NULL;
1025 struct vhost *
1026 new_vhost(void)
1028 return xcalloc(1, sizeof(struct vhost));
1031 struct location *
1032 new_location(void)
1034 struct location *l;
1036 l = xcalloc(1, sizeof(*l));
1037 l->dirfd = -1;
1038 l->fcgi = -1;
1039 return l;
1042 struct proxy *
1043 new_proxy(void)
1045 struct proxy *p;
1047 conf.can_open_sockets = 1;
1049 p = xcalloc(1, sizeof(*p));
1050 p->protocols = TLS_PROTOCOLS_DEFAULT;
1051 return p;
1054 char *
1055 ensure_absolute_path(char *path)
1057 if (path == NULL || *path != '/')
1058 yyerror("not an absolute path: %s", path);
1059 return path;
1062 int
1063 check_block_code(int n)
1065 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1066 yyerror("invalid block code %d", n);
1067 return n;
1070 char *
1071 check_block_fmt(char *fmt)
1073 char *s;
1075 for (s = fmt; *s; ++s) {
1076 if (*s != '%')
1077 continue;
1078 switch (*++s) {
1079 case '%':
1080 case 'p':
1081 case 'q':
1082 case 'P':
1083 case 'N':
1084 break;
1085 default:
1086 yyerror("invalid format specifier %%%c", *s);
1090 return fmt;
1093 int
1094 check_strip_no(int n)
1096 if (n <= 0)
1097 yyerror("invalid strip number %d", n);
1098 return n;
1101 int
1102 check_port_num(int n)
1104 if (n <= 0 || n >= UINT16_MAX)
1105 yyerror("port number is %s: %d",
1106 n <= 0 ? "too small" : "too large",
1107 n);
1108 return n;
1111 int
1112 check_prefork_num(int n)
1114 if (n <= 0 || n >= PROC_MAX)
1115 yyerror("invalid prefork number %d", n);
1116 return n;
1119 void
1120 advance_loc(void)
1122 loc = new_location();
1123 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1126 void
1127 advance_proxy(void)
1129 proxy = new_proxy();
1130 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1133 void
1134 parsehp(char *str, char **host, const char **port, const char *def)
1136 char *at;
1137 const char *errstr;
1139 *host = str;
1141 if ((at = strchr(str, ':')) != NULL) {
1142 *at++ = '\0';
1143 *port = at;
1144 } else
1145 *port = def;
1147 strtonum(*port, 1, UINT16_MAX, &errstr);
1148 if (errstr != NULL)
1149 yyerror("port is %s: %s", errstr, *port);
1152 void
1153 only_once(const void *ptr, const char *name)
1155 if (ptr != NULL)
1156 yyerror("`%s' specified more than once", name);
1159 void
1160 only_oncei(int i, const char *name)
1162 if (i != -1)
1163 yyerror("`%s' specified more than once", name);
1166 int
1167 fastcgi_conf(char *path, char *port, char *prog)
1169 struct fcgi *f;
1170 int i;
1172 conf.can_open_sockets = 1;
1174 for (i = 0; i < FCGI_MAX; ++i) {
1175 f = &fcgi[i];
1177 if (f->path == NULL) {
1178 f->id = i;
1179 f->path = path;
1180 f->port = port;
1181 f->prog = prog;
1182 return i;
1185 /* XXX: what to do with prog? */
1186 if (!strcmp(f->path, path) &&
1187 ((port == NULL && f->port == NULL) ||
1188 !strcmp(f->port, port))) {
1189 free(path);
1190 free(port);
1191 return i;
1195 yyerror("too much `fastcgi' rules defined.");
1196 return -1;
1199 void
1200 add_param(char *name, char *val)
1202 struct envlist *e;
1203 struct envhead *h = &host->params;
1205 e = xcalloc(1, sizeof(*e));
1206 e->name = name;
1207 e->value = val;
1208 if (TAILQ_EMPTY(h))
1209 TAILQ_INSERT_HEAD(h, e, envs);
1210 else
1211 TAILQ_INSERT_TAIL(h, e, envs);