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 { conf.chroot = $2; }
216 | IPV6 bool { conf.ipv6 = $2; }
217 | MIME STRING string {
218 yywarn("`mime MIME EXT' is deprecated and will be "
219 "removed in a future version, please use the new "
220 "`types' block.");
221 if (add_mime(&conf.mime, $2, $3) == -1)
222 err(1, "add_mime");
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 if (add_mime(&conf.mime, $2, $4) == -1)
229 err(1, "add_mime");
231 | PORT NUM { conf.port = check_port_num($2); }
232 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
233 | PROTOCOLS string {
234 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
235 yyerror("invalid protocols string \"%s\"", $2);
236 free($2);
238 | USER string { conf.user = $2; }
241 vhost : SERVER string {
242 host = new_vhost();
243 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
245 loc = new_location();
246 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
248 TAILQ_INIT(&host->proxies);
250 loc->match = xstrdup("*");
251 host->domain = $2;
253 if (strstr($2, "xn--") != NULL) {
254 yywarn("\"%s\" looks like punycode: you "
255 "should use the decoded hostname", $2);
257 } '{' optnl servbody '}' {
258 if (host->cert == NULL || host->key == NULL)
259 yyerror("invalid vhost definition: %s", $2);
261 | error '}' { yyerror("bad server directive"); }
264 servbody : /* empty */
265 | servbody servopt optnl
266 | servbody location optnl
267 | servbody proxy optnl
270 servopt : ALIAS string {
271 struct alist *a;
273 a = xcalloc(1, sizeof(*a));
274 a->alias = $2;
275 if (TAILQ_EMPTY(&host->aliases))
276 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
277 else
278 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
280 | CERT string {
281 only_once(host->cert, "cert");
282 host->cert = ensure_absolute_path($2);
284 | KEY string {
285 only_once(host->key, "key");
286 host->key = ensure_absolute_path($2);
288 | OCSP string {
289 only_once(host->ocsp, "ocsp");
290 host->ocsp = ensure_absolute_path($2);
292 | PARAM string '=' string {
293 add_param($2, $4, 0);
295 | locopt
298 proxy : PROXY { advance_proxy(); }
299 proxy_matches '{' optnl proxy_opts '}' {
300 if (proxy->host == NULL)
301 yyerror("invalid proxy block: missing `relay-to' option");
303 if ((proxy->cert == NULL && proxy->key != NULL) ||
304 (proxy->cert != NULL && proxy->key == NULL))
305 yyerror("invalid proxy block: missing cert or key");
309 proxy_matches : /* empty */
310 | proxy_matches proxy_match
313 proxy_match : PROTO string {
314 only_once(proxy->match_proto, "proxy proto");
315 free(proxy->match_proto);
316 proxy->match_proto = $2;
318 | FOR_HOST string {
319 only_once(proxy->match_host, "proxy for-host");
320 free(proxy->match_host);
321 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
325 proxy_opts : /* empty */
326 | proxy_opts proxy_opt optnl
329 proxy_opt : CERT string {
330 only_once(proxy->cert, "proxy cert");
331 tls_unload_file(proxy->cert, proxy->certlen);
332 ensure_absolute_path($2);
333 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
334 if (proxy->cert == NULL)
335 yyerror("can't load cert %s", $2);
336 free($2);
338 | KEY string {
339 only_once(proxy->key, "proxy key");
340 tls_unload_file(proxy->key, proxy->keylen);
341 ensure_absolute_path($2);
342 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
343 if (proxy->key == NULL)
344 yyerror("can't load key %s", $2);
345 free($2);
347 | PROTOCOLS string {
348 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
349 yyerror("invalid protocols string \"%s\"", $2);
350 free($2);
352 | RELAY_TO string {
353 only_once(proxy->host, "proxy relay-to");
354 free(proxy->host);
355 parsehp($2, &proxy->host, &proxy->port, "1965");
357 | REQUIRE CLIENT CA string {
358 only_once(proxy->reqca, "require client ca");
359 ensure_absolute_path($4);
360 if ((proxy->reqca = load_ca($4)) == NULL)
361 yyerror("couldn't load ca cert: %s", $4);
362 free($4);
364 | SNI string {
365 only_once(proxy->sni, "proxy sni");
366 free(proxy->sni);
367 proxy->sni = $2;
369 | USE_TLS bool {
370 proxy->notls = !$2;
372 | VERIFYNAME bool {
373 proxy->noverifyname = !$2;
377 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
378 /* drop the starting '/' if any */
379 if (*$3 == '/')
380 memmove($3, $3+1, strlen($3));
381 loc->match = $3;
383 | error '}'
386 locopts : /* empty */
387 | locopts locopt optnl
390 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
391 | BLOCK RETURN NUM string {
392 only_once(loc->block_fmt, "block");
393 loc->block_fmt = check_block_fmt($4);
394 loc->block_code = check_block_code($3);
396 | BLOCK RETURN NUM {
397 only_once(loc->block_fmt, "block");
398 loc->block_fmt = xstrdup("temporary failure");
399 loc->block_code = check_block_code($3);
400 if ($3 >= 30 && $3 < 40)
401 yyerror("missing `meta' for block return %d", $3);
403 | BLOCK {
404 only_once(loc->block_fmt, "block");
405 loc->block_fmt = xstrdup("temporary failure");
406 loc->block_code = 40;
408 | DEFAULT TYPE string {
409 only_once(loc->default_mime, "default type");
410 loc->default_mime = $3;
412 | FASTCGI fastcgi
413 | INDEX string {
414 only_once(loc->index, "index");
415 loc->index = $2;
417 | LANG string {
418 only_once(loc->lang, "lang");
419 loc->lang = $2;
421 | LOG bool { loc->disable_log = !$2; }
422 | REQUIRE CLIENT CA string {
423 only_once(loc->reqca, "require client ca");
424 ensure_absolute_path($4);
425 if ((loc->reqca = load_ca($4)) == NULL)
426 yyerror("couldn't load ca cert: %s", $4);
427 free($4);
429 | ROOT string {
430 only_once(loc->dir, "root");
431 loc->dir = ensure_absolute_path($2);
433 | STRIP NUM { loc->strip = check_strip_no($2); }
436 fastcgi : SPAWN string {
437 only_oncei(loc->fcgi, "fastcgi");
438 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
440 | string {
441 only_oncei(loc->fcgi, "fastcgi");
442 loc->fcgi = fastcgi_conf($1, NULL, NULL);
444 | TCP string PORT NUM {
445 char *c;
446 if (asprintf(&c, "%d", $4) == -1)
447 err(1, "asprintf");
448 only_oncei(loc->fcgi, "fastcgi");
449 loc->fcgi = fastcgi_conf($2, c, NULL);
451 | TCP string {
452 only_oncei(loc->fcgi, "fastcgi");
453 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
455 | TCP string PORT string {
456 only_oncei(loc->fcgi, "fastcgi");
457 loc->fcgi = fastcgi_conf($2, $4, NULL);
461 types : TYPES '{' optnl mediaopts_l '}' {
462 conf.mime.skip_defaults = 1;
466 mediaopts_l : mediaopts_l mediaoptsl nl
467 | mediaoptsl nl
470 mediaoptsl : STRING { current_media = $1; } medianames_l optsemicolon
471 | include
474 medianames_l : medianames_l medianamesl
475 | medianamesl
478 medianamesl : numberstring {
479 if (add_mime(&conf.mime, current_media, $1) == -1)
480 err(1, "add_mime");
484 nl : '\n' optnl
487 optnl : '\n' optnl /* zero or more newlines */
488 | ';' optnl /* semicolons too */
489 | /*empty*/
492 optsemicolon : ';'
496 %%
498 static const struct keyword {
499 const char *word;
500 int token;
501 } keywords[] = {
502 /* these MUST be sorted */
503 {"alias", ALIAS},
504 {"auto", AUTO},
505 {"block", BLOCK},
506 {"ca", CA},
507 {"cert", CERT},
508 {"chroot", CHROOT},
509 {"client", CLIENT},
510 {"default", DEFAULT},
511 {"fastcgi", FASTCGI},
512 {"for-host", FOR_HOST},
513 {"include", INCLUDE},
514 {"index", INDEX},
515 {"ipv6", IPV6},
516 {"key", KEY},
517 {"lang", LANG},
518 {"location", LOCATION},
519 {"log", LOG},
520 {"map", MAP},
521 {"mime", MIME},
522 {"ocsp", OCSP},
523 {"off", OFF},
524 {"on", ON},
525 {"param", PARAM},
526 {"port", PORT},
527 {"prefork", PREFORK},
528 {"proto", PROTO},
529 {"protocols", PROTOCOLS},
530 {"proxy", PROXY},
531 {"relay-to", RELAY_TO},
532 {"require", REQUIRE},
533 {"return", RETURN},
534 {"root", ROOT},
535 {"server", SERVER},
536 {"sni", SNI},
537 {"spawn", SPAWN},
538 {"strip", STRIP},
539 {"tcp", TCP},
540 {"to-ext", TOEXT},
541 {"type", TYPE},
542 {"types", TYPES},
543 {"use-tls", USE_TLS},
544 {"user", USER},
545 {"verifyname", VERIFYNAME},
546 };
548 void
549 yyerror(const char *msg, ...)
551 va_list ap;
553 file->errors++;
555 va_start(ap, msg);
556 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
557 vfprintf(stderr, msg, ap);
558 fprintf(stderr, "\n");
559 va_end(ap);
562 void
563 yywarn(const char *msg, ...)
565 va_list ap;
567 va_start(ap, msg);
568 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
569 vfprintf(stderr, msg, ap);
570 fprintf(stderr, "\n");
571 va_end(ap);
574 int
575 kw_cmp(const void *k, const void *e)
577 return strcmp(k, ((struct keyword *)e)->word);
580 int
581 lookup(char *s)
583 const struct keyword *p;
585 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
586 sizeof(keywords[0]), kw_cmp);
588 if (p)
589 return p->token;
590 else
591 return STRING;
594 #define START_EXPAND 1
595 #define DONE_EXPAND 2
597 static int expanding;
599 int
600 igetc(void)
602 int c;
604 while (1) {
605 if (file->ungetpos > 0)
606 c = file->ungetbuf[--file->ungetpos];
607 else
608 c = getc(file->stream);
610 if (c == START_EXPAND)
611 expanding = 1;
612 else if (c == DONE_EXPAND)
613 expanding = 0;
614 else
615 break;
617 return c;
620 int
621 lgetc(int quotec)
623 int c, next;
625 if (quotec) {
626 if ((c = igetc()) == EOF) {
627 yyerror("reached end of file while parsing "
628 "quoted string");
629 if (file == topfile || popfile() == EOF)
630 return EOF;
631 return quotec;
633 return c;
636 while ((c = igetc()) == '\\') {
637 next = igetc();
638 if (next != '\n') {
639 c = next;
640 break;
642 yylval.lineno = file->lineno;
643 file->lineno++;
646 if (c == EOF) {
647 /*
648 * Fake EOL when hit EOF for the first time. This gets line
649 * count right if last line in included file is syntactically
650 * invalid and has no newline.
651 */
652 if (file->eof_reached == 0) {
653 file->eof_reached = 1;
654 return '\n';
656 while (c == EOF) {
657 if (file == topfile || popfile() == EOF)
658 return EOF;
659 c = igetc();
662 return c;
665 void
666 lungetc(int c)
668 if (c == EOF)
669 return;
671 if (file->ungetpos >= file->ungetsize) {
672 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
673 if (p == NULL)
674 err(1, "lungetc");
675 file->ungetbuf = p;
676 file->ungetsize *= 2;
678 file->ungetbuf[file->ungetpos++] = c;
681 int
682 findeol(void)
684 int c;
686 /* Skip to either EOF or the first real EOL. */
687 while (1) {
688 c = lgetc(0);
689 if (c == '\n') {
690 file->lineno++;
691 break;
693 if (c == EOF)
694 break;
696 return ERROR;
699 int
700 yylex(void)
702 char buf[8096];
703 char *p, *val;
704 int quotec, next, c;
705 int token;
707 top:
708 p = buf;
709 while ((c = lgetc(0)) == ' ' || c == '\t')
710 ; /* nothing */
712 yylval.lineno = file->lineno;
713 if (c == '#')
714 while ((c = lgetc(0)) != '\n' && c != EOF)
715 ; /* nothing */
716 if (c == '$' && !expanding) {
717 while (1) {
718 if ((c = lgetc(0)) == EOF)
719 return 0;
720 if (p + 1 >= buf + sizeof(buf) -1) {
721 yyerror("string too long");
722 return findeol();
724 if (isalnum(c) || c == '_') {
725 *p++ = c;
726 continue;
728 *p = '\0';
729 lungetc(c);
730 break;
732 val = symget(buf);
733 if (val == NULL) {
734 yyerror("macro `%s' not defined", buf);
735 return findeol();
737 yylval.v.string = xstrdup(val);
738 return STRING;
740 if (c == '@' && !expanding) {
741 while (1) {
742 if ((c = lgetc(0)) == EOF)
743 return 0;
745 if (p + 1 >= buf + sizeof(buf) - 1) {
746 yyerror("string too long");
747 return findeol();
749 if (isalnum(c) || c == '_') {
750 *p++ = c;
751 continue;
753 *p = '\0';
754 lungetc(c);
755 break;
757 val = symget(buf);
758 if (val == NULL) {
759 yyerror("macro '%s' not defined", buf);
760 return findeol();
762 p = val + strlen(val) - 1;
763 lungetc(DONE_EXPAND);
764 while (p >= val) {
765 lungetc(*p);
766 p--;
768 lungetc(START_EXPAND);
769 goto top;
772 switch (c) {
773 case '\'':
774 case '"':
775 quotec = c;
776 while (1) {
777 if ((c = lgetc(quotec)) == EOF)
778 return 0;
779 if (c == '\n') {
780 file->lineno++;
781 continue;
782 } else if (c == '\\') {
783 if ((next = lgetc(quotec)) == EOF)
784 return (0);
785 if (next == quotec || next == ' ' ||
786 next == '\t')
787 c = next;
788 else if (next == '\n') {
789 file->lineno++;
790 continue;
791 } else
792 lungetc(next);
793 } else if (c == quotec) {
794 *p = '\0';
795 break;
796 } else if (c == '\0') {
797 yyerror("invalid syntax");
798 return findeol();
800 if (p + 1 >= buf + sizeof(buf) - 1) {
801 yyerror("string too long");
802 return findeol();
804 *p++ = c;
806 yylval.v.string = strdup(buf);
807 if (yylval.v.string == NULL)
808 err(1, "yylex: strdup");
809 return STRING;
812 #define allowed_to_end_number(x) \
813 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
815 if (c == '-' || isdigit(c)) {
816 do {
817 *p++ = c;
818 if ((size_t)(p-buf) >= sizeof(buf)) {
819 yyerror("string too long");
820 return findeol();
822 } while ((c = lgetc(0)) != EOF && isdigit(c));
823 lungetc(c);
824 if (p == buf + 1 && buf[0] == '-')
825 goto nodigits;
826 if (c == EOF || allowed_to_end_number(c)) {
827 const char *errstr = NULL;
829 *p = '\0';
830 yylval.v.number = strtonum(buf, LLONG_MIN,
831 LLONG_MAX, &errstr);
832 if (errstr) {
833 yyerror("\"%s\" invalid number: %s",
834 buf, errstr);
835 return findeol();
837 return NUM;
838 } else {
839 nodigits:
840 while (p > buf + 1)
841 lungetc(*--p);
842 c = *--p;
843 if (c == '-')
844 return c;
848 #define allowed_in_string(x) \
849 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
850 x != '{' && x != '}' && \
851 x != '!' && x != '=' && x != '#' && \
852 x != ',' && x != ';'))
854 if (isalnum(c) || c == ':' || c == '_') {
855 do {
856 *p++ = c;
857 if ((size_t)(p-buf) >= sizeof(buf)) {
858 yyerror("string too long");
859 return findeol();
861 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
862 lungetc(c);
863 *p = '\0';
864 if ((token = lookup(buf)) == STRING)
865 yylval.v.string = xstrdup(buf);
866 return token;
868 if (c == '\n') {
869 yylval.lineno = file->lineno;
870 file->lineno++;
872 if (c == EOF)
873 return 0;
874 return c;
877 struct file *
878 pushfile(const char *name, int secret)
880 struct file *nfile;
882 nfile = xcalloc(1, sizeof(*nfile));
883 nfile->name = xstrdup(name);
884 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
885 log_warn(NULL, "can't open %s: %s", nfile->name,
886 strerror(errno));
887 free(nfile->name);
888 free(nfile);
889 return NULL;
891 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
892 nfile->ungetsize = 16;
893 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
894 TAILQ_INSERT_TAIL(&files, nfile, entry);
895 return nfile;
898 int
899 popfile(void)
901 struct file *prev;
903 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
904 prev->errors += file->errors;
906 TAILQ_REMOVE(&files, file, entry);
907 fclose(file->stream);
908 free(file->name);
909 free(file->ungetbuf);
910 free(file);
911 file = prev;
912 return file ? 0 : EOF;
915 void
916 parse_conf(const char *filename)
918 struct sym *sym, *next;
920 file = pushfile(filename, 0);
921 if (file == NULL)
922 exit(1);
923 topfile = file;
925 yyparse();
926 errors = file->errors;
927 popfile();
929 /* Free macros and check which have not been used. */
930 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
931 /* TODO: warn if !sym->used */
932 if (!sym->persist) {
933 free(sym->name);
934 free(sym->val);
935 TAILQ_REMOVE(&symhead, sym, entry);
936 free(sym);
940 if (errors)
941 exit(1);
944 void
945 print_conf(void)
947 struct vhost *h;
948 /* struct location *l; */
949 /* struct envlist *e; */
950 /* struct alist *a; */
952 if (conf.chroot != NULL)
953 printf("chroot \"%s\"\n", conf.chroot);
954 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
955 /* XXX: defined mimes? */
956 printf("port %d\n", conf.port);
957 printf("prefork %d\n", conf.prefork);
958 /* XXX: protocols? */
959 if (conf.user != NULL)
960 printf("user \"%s\"\n", conf.user);
962 TAILQ_FOREACH(h, &hosts, vhosts) {
963 printf("\nserver \"%s\" {\n", h->domain);
964 printf(" cert \"%s\"\n", h->cert);
965 printf(" key \"%s\"\n", h->key);
966 /* TODO: print locations... */
967 printf("}\n");
971 int
972 symset(const char *name, const char *val, int persist)
974 struct sym *sym;
976 TAILQ_FOREACH(sym, &symhead, entry) {
977 if (!strcmp(name, sym->name))
978 break;
981 if (sym != NULL) {
982 if (sym->persist)
983 return 0;
984 else {
985 free(sym->name);
986 free(sym->val);
987 TAILQ_REMOVE(&symhead, sym, entry);
988 free(sym);
992 sym = xcalloc(1, sizeof(*sym));
993 sym->name = xstrdup(name);
994 sym->val = xstrdup(val);
995 sym->used = 0;
996 sym->persist = persist;
998 TAILQ_INSERT_TAIL(&symhead, sym, entry);
999 return 0;
1002 int
1003 cmdline_symset(char *s)
1005 char *sym, *val;
1006 int ret;
1008 if ((val = strrchr(s, '=')) == NULL)
1009 return -1;
1010 sym = xcalloc(1, val - s + 1);
1011 memcpy(sym, s, val - s);
1012 ret = symset(sym, val + 1, 1);
1013 free(sym);
1014 return ret;
1017 char *
1018 symget(const char *nam)
1020 struct sym *sym;
1022 TAILQ_FOREACH(sym, &symhead, entry) {
1023 if (strcmp(nam, sym->name) == 0) {
1024 sym->used = 1;
1025 return sym->val;
1028 return NULL;
1031 struct vhost *
1032 new_vhost(void)
1034 return xcalloc(1, sizeof(struct vhost));
1037 struct location *
1038 new_location(void)
1040 struct location *l;
1042 l = xcalloc(1, sizeof(*l));
1043 l->dirfd = -1;
1044 l->fcgi = -1;
1045 return l;
1048 struct proxy *
1049 new_proxy(void)
1051 struct proxy *p;
1053 conf.can_open_sockets = 1;
1055 p = xcalloc(1, sizeof(*p));
1056 p->protocols = TLS_PROTOCOLS_DEFAULT;
1057 return p;
1060 char *
1061 ensure_absolute_path(char *path)
1063 if (path == NULL || *path != '/')
1064 yyerror("not an absolute path: %s", path);
1065 return path;
1068 int
1069 check_block_code(int n)
1071 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1072 yyerror("invalid block code %d", n);
1073 return n;
1076 char *
1077 check_block_fmt(char *fmt)
1079 char *s;
1081 for (s = fmt; *s; ++s) {
1082 if (*s != '%')
1083 continue;
1084 switch (*++s) {
1085 case '%':
1086 case 'p':
1087 case 'q':
1088 case 'P':
1089 case 'N':
1090 break;
1091 default:
1092 yyerror("invalid format specifier %%%c", *s);
1096 return fmt;
1099 int
1100 check_strip_no(int n)
1102 if (n <= 0)
1103 yyerror("invalid strip number %d", n);
1104 return n;
1107 int
1108 check_port_num(int n)
1110 if (n <= 0 || n >= UINT16_MAX)
1111 yyerror("port number is %s: %d",
1112 n <= 0 ? "too small" : "too large",
1113 n);
1114 return n;
1117 int
1118 check_prefork_num(int n)
1120 if (n <= 0 || n >= PROC_MAX)
1121 yyerror("invalid prefork number %d", n);
1122 return n;
1125 void
1126 advance_loc(void)
1128 loc = new_location();
1129 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1132 void
1133 advance_proxy(void)
1135 proxy = new_proxy();
1136 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1139 void
1140 parsehp(char *str, char **host, const char **port, const char *def)
1142 char *at;
1143 const char *errstr;
1145 *host = str;
1147 if ((at = strchr(str, ':')) != NULL) {
1148 *at++ = '\0';
1149 *port = at;
1150 } else
1151 *port = def;
1153 strtonum(*port, 1, UINT16_MAX, &errstr);
1154 if (errstr != NULL)
1155 yyerror("port is %s: %s", errstr, *port);
1158 void
1159 only_once(const void *ptr, const char *name)
1161 if (ptr != NULL)
1162 yyerror("`%s' specified more than once", name);
1165 void
1166 only_oncei(int i, const char *name)
1168 if (i != -1)
1169 yyerror("`%s' specified more than once", name);
1172 int
1173 fastcgi_conf(char *path, char *port, char *prog)
1175 struct fcgi *f;
1176 int i;
1178 conf.can_open_sockets = 1;
1180 for (i = 0; i < FCGI_MAX; ++i) {
1181 f = &fcgi[i];
1183 if (f->path == NULL) {
1184 f->id = i;
1185 f->path = path;
1186 f->port = port;
1187 f->prog = prog;
1188 return i;
1191 /* XXX: what to do with prog? */
1192 if (!strcmp(f->path, path) &&
1193 ((port == NULL && f->port == NULL) ||
1194 !strcmp(f->port, port))) {
1195 free(path);
1196 free(port);
1197 return i;
1201 yyerror("too much `fastcgi' rules defined.");
1202 return -1;
1205 void
1206 add_param(char *name, char *val, int env)
1208 struct envlist *e;
1209 struct envhead *h;
1211 if (env)
1212 h = &host->env;
1213 else
1214 h = &host->params;
1216 e = xcalloc(1, sizeof(*e));
1217 e->name = name;
1218 e->value = val;
1219 if (TAILQ_EMPTY(h))
1220 TAILQ_INSERT_HEAD(h, e, envs);
1221 else
1222 TAILQ_INSERT_TAIL(h, e, envs);