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 "syntax: `map MIME to-ext EXT'");
222 add_mime(&conf.mime, $2, $3);
224 | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
225 | PORT NUM { conf.port = check_port_num($2); }
226 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
227 | PROTOCOLS string {
228 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
229 yyerror("invalid protocols string \"%s\"", $2);
230 free($2);
232 | USER string { conf.user = $2; }
235 vhost : SERVER string {
236 host = new_vhost();
237 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
239 loc = new_location();
240 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
242 TAILQ_INIT(&host->proxies);
244 loc->match = xstrdup("*");
245 host->domain = $2;
247 if (strstr($2, "xn--") != NULL) {
248 yywarn("\"%s\" looks like punycode: you "
249 "should use the decoded hostname", $2);
251 } '{' optnl servbody '}' {
252 if (host->cert == NULL || host->key == NULL)
253 yyerror("invalid vhost definition: %s", $2);
255 | error '}' { yyerror("bad server directive"); }
258 servbody : /* empty */
259 | servbody servopt optnl
260 | servbody location optnl
261 | servbody proxy optnl
264 servopt : ALIAS string {
265 struct alist *a;
267 a = xcalloc(1, sizeof(*a));
268 a->alias = $2;
269 if (TAILQ_EMPTY(&host->aliases))
270 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
271 else
272 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
274 | CERT string {
275 only_once(host->cert, "cert");
276 host->cert = ensure_absolute_path($2);
278 | CGI string {
279 only_once(host->cgi, "cgi");
280 /* drop the starting '/', if any */
281 if (*$2 == '/')
282 memmove($2, $2+1, strlen($2));
283 host->cgi = $2;
285 | ENTRYPOINT string {
286 only_once(host->entrypoint, "entrypoint");
287 while (*$2 == '/')
288 memmove($2, $2+1, strlen($2));
289 host->entrypoint = $2;
291 | ENV string '=' string {
292 add_param($2, $4, 1);
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 '}'
474 mediaopts_l : mediaopts_l mediaoptsl nl
475 | mediaoptsl nl
478 mediaoptsl : STRING { current_media = $1; } medianames_l optsemicolon
479 | include
482 medianames_l : medianames_l medianamesl
483 | medianamesl
486 medianamesl : numberstring { add_mime(&conf.mime, current_media, $1); }
489 nl : '\n' optnl
492 optnl : '\n' optnl /* zero or more newlines */
493 | ';' optnl /* semicolons too */
494 | /*empty*/
497 optsemicolon : ';'
501 %%
503 static const struct keyword {
504 const char *word;
505 int token;
506 } keywords[] = {
507 /* these MUST be sorted */
508 {"alias", ALIAS},
509 {"auto", AUTO},
510 {"block", BLOCK},
511 {"ca", CA},
512 {"cert", CERT},
513 {"cgi", CGI},
514 {"chroot", CHROOT},
515 {"client", CLIENT},
516 {"default", DEFAULT},
517 {"entrypoint", ENTRYPOINT},
518 {"env", ENV},
519 {"fastcgi", FASTCGI},
520 {"for-host", FOR_HOST},
521 {"include", INCLUDE},
522 {"index", INDEX},
523 {"ipv6", IPV6},
524 {"key", KEY},
525 {"lang", LANG},
526 {"location", LOCATION},
527 {"log", LOG},
528 {"map", MAP},
529 {"mime", MIME},
530 {"ocsp", OCSP},
531 {"off", OFF},
532 {"on", ON},
533 {"param", PARAM},
534 {"port", PORT},
535 {"prefork", PREFORK},
536 {"proto", PROTO},
537 {"protocols", PROTOCOLS},
538 {"proxy", PROXY},
539 {"relay-to", RELAY_TO},
540 {"require", REQUIRE},
541 {"return", RETURN},
542 {"root", ROOT},
543 {"server", SERVER},
544 {"sni", SNI},
545 {"spawn", SPAWN},
546 {"strip", STRIP},
547 {"tcp", TCP},
548 {"to-ext", TOEXT},
549 {"type", TYPE},
550 {"types", TYPES},
551 {"use-tls", USE_TLS},
552 {"user", USER},
553 {"verifyname", VERIFYNAME},
554 };
556 void
557 yyerror(const char *msg, ...)
559 va_list ap;
561 file->errors++;
563 va_start(ap, msg);
564 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
565 vfprintf(stderr, msg, ap);
566 fprintf(stderr, "\n");
567 va_end(ap);
570 void
571 yywarn(const char *msg, ...)
573 va_list ap;
575 va_start(ap, msg);
576 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
577 vfprintf(stderr, msg, ap);
578 fprintf(stderr, "\n");
579 va_end(ap);
582 int
583 kw_cmp(const void *k, const void *e)
585 return strcmp(k, ((struct keyword *)e)->word);
588 int
589 lookup(char *s)
591 const struct keyword *p;
593 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
594 sizeof(keywords[0]), kw_cmp);
596 if (p)
597 return p->token;
598 else
599 return STRING;
602 #define START_EXPAND 1
603 #define DONE_EXPAND 2
605 static int expanding;
607 int
608 igetc(void)
610 int c;
612 while (1) {
613 if (file->ungetpos > 0)
614 c = file->ungetbuf[--file->ungetpos];
615 else
616 c = getc(file->stream);
618 if (c == START_EXPAND)
619 expanding = 1;
620 else if (c == DONE_EXPAND)
621 expanding = 0;
622 else
623 break;
625 return c;
628 int
629 lgetc(int quotec)
631 int c, next;
633 if (quotec) {
634 if ((c = igetc()) == EOF) {
635 yyerror("reached end of file while parsing "
636 "quoted string");
637 if (file == topfile || popfile() == EOF)
638 return EOF;
639 return quotec;
641 return c;
644 while ((c = igetc()) == '\\') {
645 next = igetc();
646 if (next != '\n') {
647 c = next;
648 break;
650 yylval.lineno = file->lineno;
651 file->lineno++;
654 if (c == EOF) {
655 /*
656 * Fake EOL when hit EOF for the first time. This gets line
657 * count right if last line in included file is syntactically
658 * invalid and has no newline.
659 */
660 if (file->eof_reached == 0) {
661 file->eof_reached = 1;
662 return '\n';
664 while (c == EOF) {
665 if (file == topfile || popfile() == EOF)
666 return EOF;
667 c = igetc();
670 return c;
673 void
674 lungetc(int c)
676 if (c == EOF)
677 return;
679 if (file->ungetpos >= file->ungetsize) {
680 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
681 if (p == NULL)
682 err(1, "lungetc");
683 file->ungetbuf = p;
684 file->ungetsize *= 2;
686 file->ungetbuf[file->ungetpos++] = c;
689 int
690 findeol(void)
692 int c;
694 /* Skip to either EOF or the first real EOL. */
695 while (1) {
696 c = lgetc(0);
697 if (c == '\n') {
698 file->lineno++;
699 break;
701 if (c == EOF)
702 break;
704 return ERROR;
707 int
708 yylex(void)
710 char buf[8096];
711 char *p, *val;
712 int quotec, next, c;
713 int token;
715 top:
716 p = buf;
717 while ((c = lgetc(0)) == ' ' || c == '\t')
718 ; /* nothing */
720 yylval.lineno = file->lineno;
721 if (c == '#')
722 while ((c = lgetc(0)) != '\n' && c != EOF)
723 ; /* nothing */
724 if (c == '$' && !expanding) {
725 while (1) {
726 if ((c = lgetc(0)) == EOF)
727 return 0;
728 if (p + 1 >= buf + sizeof(buf) -1) {
729 yyerror("string too long");
730 return findeol();
732 if (isalnum(c) || c == '_') {
733 *p++ = c;
734 continue;
736 *p = '\0';
737 lungetc(c);
738 break;
740 val = symget(buf);
741 if (val == NULL) {
742 yyerror("macro `%s' not defined", buf);
743 return findeol();
745 yylval.v.string = xstrdup(val);
746 return STRING;
748 if (c == '@' && !expanding) {
749 while (1) {
750 if ((c = lgetc(0)) == EOF)
751 return 0;
753 if (p + 1 >= buf + sizeof(buf) - 1) {
754 yyerror("string too long");
755 return findeol();
757 if (isalnum(c) || c == '_') {
758 *p++ = c;
759 continue;
761 *p = '\0';
762 lungetc(c);
763 break;
765 val = symget(buf);
766 if (val == NULL) {
767 yyerror("macro '%s' not defined", buf);
768 return findeol();
770 p = val + strlen(val) - 1;
771 lungetc(DONE_EXPAND);
772 while (p >= val) {
773 lungetc(*p);
774 p--;
776 lungetc(START_EXPAND);
777 goto top;
780 switch (c) {
781 case '\'':
782 case '"':
783 quotec = c;
784 while (1) {
785 if ((c = lgetc(quotec)) == EOF)
786 return 0;
787 if (c == '\n') {
788 file->lineno++;
789 continue;
790 } else if (c == '\\') {
791 if ((next = lgetc(quotec)) == EOF)
792 return (0);
793 if (next == quotec || next == ' ' ||
794 next == '\t')
795 c = next;
796 else if (next == '\n') {
797 file->lineno++;
798 continue;
799 } else
800 lungetc(next);
801 } else if (c == quotec) {
802 *p = '\0';
803 break;
804 } else if (c == '\0') {
805 yyerror("invalid syntax");
806 return findeol();
808 if (p + 1 >= buf + sizeof(buf) - 1) {
809 yyerror("string too long");
810 return findeol();
812 *p++ = c;
814 yylval.v.string = strdup(buf);
815 if (yylval.v.string == NULL)
816 err(1, "yylex: strdup");
817 return STRING;
820 #define allowed_to_end_number(x) \
821 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
823 if (c == '-' || isdigit(c)) {
824 do {
825 *p++ = c;
826 if ((size_t)(p-buf) >= sizeof(buf)) {
827 yyerror("string too long");
828 return findeol();
830 } while ((c = lgetc(0)) != EOF && isdigit(c));
831 lungetc(c);
832 if (p == buf + 1 && buf[0] == '-')
833 goto nodigits;
834 if (c == EOF || allowed_to_end_number(c)) {
835 const char *errstr = NULL;
837 *p = '\0';
838 yylval.v.number = strtonum(buf, LLONG_MIN,
839 LLONG_MAX, &errstr);
840 if (errstr) {
841 yyerror("\"%s\" invalid number: %s",
842 buf, errstr);
843 return findeol();
845 return NUM;
846 } else {
847 nodigits:
848 while (p > buf + 1)
849 lungetc(*--p);
850 c = *--p;
851 if (c == '-')
852 return c;
856 #define allowed_in_string(x) \
857 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
858 x != '{' && x != '}' && \
859 x != '!' && x != '=' && x != '#' && \
860 x != ',' && x != ';'))
862 if (isalnum(c) || c == ':' || c == '_') {
863 do {
864 *p++ = c;
865 if ((size_t)(p-buf) >= sizeof(buf)) {
866 yyerror("string too long");
867 return findeol();
869 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
870 lungetc(c);
871 *p = '\0';
872 if ((token = lookup(buf)) == STRING)
873 yylval.v.string = xstrdup(buf);
874 return token;
876 if (c == '\n') {
877 yylval.lineno = file->lineno;
878 file->lineno++;
880 if (c == EOF)
881 return 0;
882 return c;
885 struct file *
886 pushfile(const char *name, int secret)
888 struct file *nfile;
890 nfile = xcalloc(1, sizeof(*nfile));
891 nfile->name = xstrdup(name);
892 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
893 log_warn(NULL, "can't open %s: %s", nfile->name,
894 strerror(errno));
895 free(nfile->name);
896 free(nfile);
897 return NULL;
899 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
900 nfile->ungetsize = 16;
901 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
902 TAILQ_INSERT_TAIL(&files, nfile, entry);
903 return nfile;
906 int
907 popfile(void)
909 struct file *prev;
911 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
912 prev->errors += file->errors;
914 TAILQ_REMOVE(&files, file, entry);
915 fclose(file->stream);
916 free(file->name);
917 free(file->ungetbuf);
918 free(file);
919 file = prev;
920 return file ? 0 : EOF;
923 void
924 parse_conf(const char *filename)
926 struct sym *sym, *next;
928 file = pushfile(filename, 0);
929 if (file == NULL)
930 exit(1);
931 topfile = file;
933 yyparse();
934 errors = file->errors;
935 popfile();
937 /* Free macros and check which have not been used. */
938 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
939 /* TODO: warn if !sym->used */
940 if (!sym->persist) {
941 free(sym->name);
942 free(sym->val);
943 TAILQ_REMOVE(&symhead, sym, entry);
944 free(sym);
948 if (errors)
949 exit(1);
952 void
953 print_conf(void)
955 struct vhost *h;
956 /* struct location *l; */
957 /* struct envlist *e; */
958 /* struct alist *a; */
960 if (conf.chroot != NULL)
961 printf("chroot \"%s\"\n", conf.chroot);
962 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
963 /* XXX: defined mimes? */
964 printf("port %d\n", conf.port);
965 printf("prefork %d\n", conf.prefork);
966 /* XXX: protocols? */
967 if (conf.user != NULL)
968 printf("user \"%s\"\n", conf.user);
970 TAILQ_FOREACH(h, &hosts, vhosts) {
971 printf("\nserver \"%s\" {\n", h->domain);
972 printf(" cert \"%s\"\n", h->cert);
973 printf(" key \"%s\"\n", h->key);
974 /* TODO: print locations... */
975 printf("}\n");
979 int
980 symset(const char *name, const char *val, int persist)
982 struct sym *sym;
984 TAILQ_FOREACH(sym, &symhead, entry) {
985 if (!strcmp(name, sym->name))
986 break;
989 if (sym != NULL) {
990 if (sym->persist)
991 return 0;
992 else {
993 free(sym->name);
994 free(sym->val);
995 TAILQ_REMOVE(&symhead, sym, entry);
996 free(sym);
1000 sym = xcalloc(1, sizeof(*sym));
1001 sym->name = xstrdup(name);
1002 sym->val = xstrdup(val);
1003 sym->used = 0;
1004 sym->persist = persist;
1006 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1007 return 0;
1010 int
1011 cmdline_symset(char *s)
1013 char *sym, *val;
1014 int ret;
1016 if ((val = strrchr(s, '=')) == NULL)
1017 return -1;
1018 sym = xcalloc(1, val - s + 1);
1019 memcpy(sym, s, val - s);
1020 ret = symset(sym, val + 1, 1);
1021 free(sym);
1022 return ret;
1025 char *
1026 symget(const char *nam)
1028 struct sym *sym;
1030 TAILQ_FOREACH(sym, &symhead, entry) {
1031 if (strcmp(nam, sym->name) == 0) {
1032 sym->used = 1;
1033 return sym->val;
1036 return NULL;
1039 struct vhost *
1040 new_vhost(void)
1042 return xcalloc(1, sizeof(struct vhost));
1045 struct location *
1046 new_location(void)
1048 struct location *l;
1050 l = xcalloc(1, sizeof(*l));
1051 l->dirfd = -1;
1052 l->fcgi = -1;
1053 return l;
1056 struct proxy *
1057 new_proxy(void)
1059 struct proxy *p;
1061 p = xcalloc(1, sizeof(*p));
1062 p->protocols = TLS_PROTOCOLS_DEFAULT;
1063 return p;
1066 char *
1067 ensure_absolute_path(char *path)
1069 if (path == NULL || *path != '/')
1070 yyerror("not an absolute path: %s", path);
1071 return path;
1074 int
1075 check_block_code(int n)
1077 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1078 yyerror("invalid block code %d", n);
1079 return n;
1082 char *
1083 check_block_fmt(char *fmt)
1085 char *s;
1087 for (s = fmt; *s; ++s) {
1088 if (*s != '%')
1089 continue;
1090 switch (*++s) {
1091 case '%':
1092 case 'p':
1093 case 'q':
1094 case 'P':
1095 case 'N':
1096 break;
1097 default:
1098 yyerror("invalid format specifier %%%c", *s);
1102 return fmt;
1105 int
1106 check_strip_no(int n)
1108 if (n <= 0)
1109 yyerror("invalid strip number %d", n);
1110 return n;
1113 int
1114 check_port_num(int n)
1116 if (n <= 0 || n >= UINT16_MAX)
1117 yyerror("port number is %s: %d",
1118 n <= 0 ? "too small" : "too large",
1119 n);
1120 return n;
1123 int
1124 check_prefork_num(int n)
1126 if (n <= 0 || n >= PROC_MAX)
1127 yyerror("invalid prefork number %d", n);
1128 return n;
1131 void
1132 advance_loc(void)
1134 loc = new_location();
1135 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1138 void
1139 advance_proxy(void)
1141 proxy = new_proxy();
1142 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1145 void
1146 parsehp(char *str, char **host, const char **port, const char *def)
1148 char *at;
1149 const char *errstr;
1151 *host = str;
1153 if ((at = strchr(str, ':')) != NULL) {
1154 *at++ = '\0';
1155 *port = at;
1156 } else
1157 *port = def;
1159 strtonum(*port, 1, UINT16_MAX, &errstr);
1160 if (errstr != NULL)
1161 yyerror("port is %s: %s", errstr, *port);
1164 void
1165 only_once(const void *ptr, const char *name)
1167 if (ptr != NULL)
1168 yyerror("`%s' specified more than once", name);
1171 void
1172 only_oncei(int i, const char *name)
1174 if (i != -1)
1175 yyerror("`%s' specified more than once", name);
1178 int
1179 fastcgi_conf(char *path, char *port, char *prog)
1181 struct fcgi *f;
1182 int i;
1184 for (i = 0; i < FCGI_MAX; ++i) {
1185 f = &fcgi[i];
1187 if (f->path == NULL) {
1188 f->id = i;
1189 f->path = path;
1190 f->port = port;
1191 f->prog = prog;
1192 return i;
1195 /* XXX: what to do with prog? */
1196 if (!strcmp(f->path, path) &&
1197 ((port == NULL && f->port == NULL) ||
1198 !strcmp(f->port, port))) {
1199 free(path);
1200 free(port);
1201 return i;
1205 yyerror("too much `fastcgi' rules defined.");
1206 return -1;
1209 void
1210 add_param(char *name, char *val, int env)
1212 struct envlist *e;
1213 struct envhead *h;
1215 if (env)
1216 h = &host->env;
1217 else
1218 h = &host->params;
1220 e = xcalloc(1, sizeof(*e));
1221 e->name = name;
1222 e->value = val;
1223 if (TAILQ_EMPTY(h))
1224 TAILQ_INSERT_HEAD(h, e, envs);
1225 else
1226 TAILQ_INSERT_TAIL(h, e, envs);