Blob


1 %{
3 /*
4 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
5 * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
6 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
7 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
8 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
9 * Copyright (c) 2001 Markus Friedl. All rights reserved.
10 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
11 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
12 *
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 */
26 #include "gmid.h"
28 #include <ctype.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
36 static struct file {
37 TAILQ_ENTRY(file) entry;
38 FILE *stream;
39 char *name;
40 size_t ungetpos;
41 size_t ungetsize;
42 u_char *ungetbuf;
43 int eof_reached;
44 int lineno;
45 int errors;
46 } *file, *topfile;
48 struct file *pushfile(const char *, int);
49 int popfile(void);
50 int yyparse(void);
51 int yylex(void);
52 void yyerror(const char *, ...)
53 __attribute__((__format__ (printf, 1, 2)))
54 __attribute__((__nonnull__ (1)));
55 void yywarn(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 int kw_cmp(const void *, const void *);
59 int lookup(char *);
60 int igetc(void);
61 int lgetc(int);
62 void lungetc(int);
63 int findeol(void);
65 /*
66 * #define YYDEBUG 1
67 * int yydebug = 1;
68 */
70 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
71 struct sym {
72 TAILQ_ENTRY(sym) entry;
73 int used;
74 int persist;
75 char *name;
76 char *val;
77 };
79 int symset(const char *, const char *, int);
80 char *symget(const char *);
82 struct vhost *new_vhost(void);
83 struct location *new_location(void);
84 struct proxy *new_proxy(void);
85 char *ensure_absolute_path(char*);
86 int check_block_code(int);
87 char *check_block_fmt(char*);
88 int check_strip_no(int);
89 int check_port_num(int);
90 int check_prefork_num(int);
91 void advance_loc(void);
92 void advance_proxy(void);
93 void parsehp(char *, char **, const char **, const char *);
94 void only_once(const void*, const char*);
95 void only_oncei(int, const char*);
96 int fastcgi_conf(char *, char *, char *);
97 void add_param(char *, char *, int);
99 static struct vhost *host;
100 static struct location *loc;
101 static struct proxy *proxy;
102 static char *current_media;
103 static int errors;
105 typedef struct {
106 union {
107 char *string;
108 int number;
109 } v;
110 int lineno;
111 } YYSTYPE;
113 %}
115 /* for bison: */
116 /* %define parse.error verbose */
118 %token ALIAS AUTO
119 %token BLOCK
120 %token CA CERT CGI CHROOT CLIENT
121 %token DEFAULT
122 %token ENTRYPOINT ENV
123 %token FASTCGI FOR_HOST
124 %token INCLUDE INDEX IPV6
125 %token KEY
126 %token LANG LOCATION LOG
127 %token MAP MIME
128 %token OCSP OFF ON
129 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
130 %token RELAY_TO REQUIRE RETURN ROOT
131 %token SERVER SNI SPAWN STRIP
132 %token TCP TOEXT TYPE TYPES
133 %token USE_TLS USER
134 %token VERIFYNAME
136 %token ERROR
138 %token <v.string> STRING
139 %token <v.number> NUM
141 %type <v.number> bool
142 %type <v.string> string numberstring
144 %%
146 conf : /* empty */
147 | conf include '\n'
148 | conf '\n'
149 | conf varset '\n'
150 | conf option '\n'
151 | conf vhost '\n'
152 | conf types '\n'
153 | conf error '\n' { file->errors++; }
156 include : INCLUDE STRING {
157 struct file *nfile;
159 if ((nfile = pushfile($2, 0)) == NULL) {
160 yyerror("failed to include file %s", $2);
161 free($2);
162 YYERROR;
164 free($2);
166 file = nfile;
167 lungetc('\n');
171 bool : ON { $$ = 1; }
172 | OFF { $$ = 0; }
175 string : string STRING {
176 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
177 free($1);
178 free($2);
179 yyerror("string: asprintf: %s", strerror(errno));
180 YYERROR;
182 free($1);
183 free($2);
185 | STRING
188 numberstring : NUM {
189 char *s;
190 if (asprintf(&s, "%d", $1) == -1) {
191 yyerror("asprintf: number");
192 YYERROR;
194 $$ = s;
196 | STRING
199 varset : STRING '=' string {
200 char *s = $1;
201 while (*s++) {
202 if (isspace((unsigned char)*s)) {
203 yyerror("macro name cannot contain "
204 "whitespaces");
205 free($1);
206 free($3);
207 YYERROR;
210 symset($1, $3, 0);
211 free($1);
212 free($3);
216 option : CHROOT string { conf.chroot = $2; }
217 | IPV6 bool { conf.ipv6 = $2; }
218 | MIME STRING string {
219 yywarn("`mime MIME EXT' is deprecated and will be "
220 "removed in a future version, please use the new "
221 "`types' block.");
222 if (add_mime(&conf.mime, $2, $3) == -1)
223 err(1, "add_mime");
225 | MAP string TOEXT string {
226 yywarn("`map mime to-ext' is deprecated and will be "
227 "removed in a future version, please use the new "
228 "`types' block.");
229 if (add_mime(&conf.mime, $2, $4) == -1)
230 err(1, "add_mime");
232 | PORT NUM { conf.port = check_port_num($2); }
233 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
234 | PROTOCOLS string {
235 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
236 yyerror("invalid protocols string \"%s\"", $2);
237 free($2);
239 | USER string { conf.user = $2; }
242 vhost : SERVER string {
243 host = new_vhost();
244 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
246 loc = new_location();
247 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
249 TAILQ_INIT(&host->proxies);
251 loc->match = xstrdup("*");
252 host->domain = $2;
254 if (strstr($2, "xn--") != NULL) {
255 yywarn("\"%s\" looks like punycode: you "
256 "should use the decoded hostname", $2);
258 } '{' optnl servbody '}' {
259 if (host->cert == NULL || host->key == NULL)
260 yyerror("invalid vhost definition: %s", $2);
262 | error '}' { yyerror("bad server directive"); }
265 servbody : /* empty */
266 | servbody servopt optnl
267 | servbody location optnl
268 | servbody proxy optnl
271 servopt : ALIAS string {
272 struct alist *a;
274 a = xcalloc(1, sizeof(*a));
275 a->alias = $2;
276 if (TAILQ_EMPTY(&host->aliases))
277 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
278 else
279 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
281 | CERT string {
282 only_once(host->cert, "cert");
283 host->cert = ensure_absolute_path($2);
285 | CGI string {
286 only_once(host->cgi, "cgi");
287 /* drop the starting '/', if any */
288 if (*$2 == '/')
289 memmove($2, $2+1, strlen($2));
290 host->cgi = $2;
292 | ENTRYPOINT string {
293 only_once(host->entrypoint, "entrypoint");
294 while (*$2 == '/')
295 memmove($2, $2+1, strlen($2));
296 host->entrypoint = $2;
298 | ENV string '=' string {
299 add_param($2, $4, 1);
301 | KEY string {
302 only_once(host->key, "key");
303 host->key = ensure_absolute_path($2);
305 | OCSP string {
306 only_once(host->ocsp, "ocsp");
307 host->ocsp = ensure_absolute_path($2);
309 | PARAM string '=' string {
310 add_param($2, $4, 0);
312 | locopt
315 proxy : PROXY { advance_proxy(); }
316 proxy_matches '{' optnl proxy_opts '}' {
317 if (proxy->host == NULL)
318 yyerror("invalid proxy block: missing `relay-to' option");
320 if ((proxy->cert == NULL && proxy->key != NULL) ||
321 (proxy->cert != NULL && proxy->key == NULL))
322 yyerror("invalid proxy block: missing cert or key");
326 proxy_matches : /* empty */
327 | proxy_matches proxy_match
330 proxy_match : PROTO string {
331 only_once(proxy->match_proto, "proxy proto");
332 free(proxy->match_proto);
333 proxy->match_proto = $2;
335 | FOR_HOST string {
336 only_once(proxy->match_host, "proxy for-host");
337 free(proxy->match_host);
338 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
342 proxy_opts : /* empty */
343 | proxy_opts proxy_opt optnl
346 proxy_opt : CERT string {
347 only_once(proxy->cert, "proxy cert");
348 tls_unload_file(proxy->cert, proxy->certlen);
349 ensure_absolute_path($2);
350 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
351 if (proxy->cert == NULL)
352 yyerror("can't load cert %s", $2);
353 free($2);
355 | KEY string {
356 only_once(proxy->key, "proxy key");
357 tls_unload_file(proxy->key, proxy->keylen);
358 ensure_absolute_path($2);
359 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
360 if (proxy->key == NULL)
361 yyerror("can't load key %s", $2);
362 free($2);
364 | PROTOCOLS string {
365 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
366 yyerror("invalid protocols string \"%s\"", $2);
367 free($2);
369 | RELAY_TO string {
370 only_once(proxy->host, "proxy relay-to");
371 free(proxy->host);
372 parsehp($2, &proxy->host, &proxy->port, "1965");
374 | REQUIRE CLIENT CA string {
375 only_once(proxy->reqca, "require client ca");
376 ensure_absolute_path($4);
377 if ((proxy->reqca = load_ca($4)) == NULL)
378 yyerror("couldn't load ca cert: %s", $4);
379 free($4);
381 | SNI string {
382 only_once(proxy->sni, "proxy sni");
383 free(proxy->sni);
384 proxy->sni = $2;
386 | USE_TLS bool {
387 proxy->notls = !$2;
389 | VERIFYNAME bool {
390 proxy->noverifyname = !$2;
394 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
395 /* drop the starting '/' if any */
396 if (*$3 == '/')
397 memmove($3, $3+1, strlen($3));
398 loc->match = $3;
400 | error '}'
403 locopts : /* empty */
404 | locopts locopt optnl
407 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
408 | BLOCK RETURN NUM string {
409 only_once(loc->block_fmt, "block");
410 loc->block_fmt = check_block_fmt($4);
411 loc->block_code = check_block_code($3);
413 | BLOCK RETURN NUM {
414 only_once(loc->block_fmt, "block");
415 loc->block_fmt = xstrdup("temporary failure");
416 loc->block_code = check_block_code($3);
417 if ($3 >= 30 && $3 < 40)
418 yyerror("missing `meta' for block return %d", $3);
420 | BLOCK {
421 only_once(loc->block_fmt, "block");
422 loc->block_fmt = xstrdup("temporary failure");
423 loc->block_code = 40;
425 | DEFAULT TYPE string {
426 only_once(loc->default_mime, "default type");
427 loc->default_mime = $3;
429 | FASTCGI fastcgi
430 | INDEX string {
431 only_once(loc->index, "index");
432 loc->index = $2;
434 | LANG string {
435 only_once(loc->lang, "lang");
436 loc->lang = $2;
438 | LOG bool { loc->disable_log = !$2; }
439 | REQUIRE CLIENT CA string {
440 only_once(loc->reqca, "require client ca");
441 ensure_absolute_path($4);
442 if ((loc->reqca = load_ca($4)) == NULL)
443 yyerror("couldn't load ca cert: %s", $4);
444 free($4);
446 | ROOT string {
447 only_once(loc->dir, "root");
448 loc->dir = ensure_absolute_path($2);
450 | STRIP NUM { loc->strip = check_strip_no($2); }
453 fastcgi : SPAWN string {
454 only_oncei(loc->fcgi, "fastcgi");
455 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
457 | string {
458 only_oncei(loc->fcgi, "fastcgi");
459 loc->fcgi = fastcgi_conf($1, NULL, NULL);
461 | TCP string PORT NUM {
462 char *c;
463 if (asprintf(&c, "%d", $4) == -1)
464 err(1, "asprintf");
465 only_oncei(loc->fcgi, "fastcgi");
466 loc->fcgi = fastcgi_conf($2, c, NULL);
468 | TCP string {
469 only_oncei(loc->fcgi, "fastcgi");
470 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
472 | TCP string PORT string {
473 only_oncei(loc->fcgi, "fastcgi");
474 loc->fcgi = fastcgi_conf($2, $4, NULL);
478 types : TYPES '{' optnl mediaopts_l '}'
481 mediaopts_l : mediaopts_l mediaoptsl nl
482 | mediaoptsl nl
485 mediaoptsl : STRING { current_media = $1; } medianames_l optsemicolon
486 | include
489 medianames_l : medianames_l medianamesl
490 | medianamesl
493 medianamesl : numberstring {
494 if (add_mime(&conf.mime, current_media, $1) == -1)
495 err(1, "add_mime");
499 nl : '\n' optnl
502 optnl : '\n' optnl /* zero or more newlines */
503 | ';' optnl /* semicolons too */
504 | /*empty*/
507 optsemicolon : ';'
511 %%
513 static const struct keyword {
514 const char *word;
515 int token;
516 } keywords[] = {
517 /* these MUST be sorted */
518 {"alias", ALIAS},
519 {"auto", AUTO},
520 {"block", BLOCK},
521 {"ca", CA},
522 {"cert", CERT},
523 {"cgi", CGI},
524 {"chroot", CHROOT},
525 {"client", CLIENT},
526 {"default", DEFAULT},
527 {"entrypoint", ENTRYPOINT},
528 {"env", ENV},
529 {"fastcgi", FASTCGI},
530 {"for-host", FOR_HOST},
531 {"include", INCLUDE},
532 {"index", INDEX},
533 {"ipv6", IPV6},
534 {"key", KEY},
535 {"lang", LANG},
536 {"location", LOCATION},
537 {"log", LOG},
538 {"map", MAP},
539 {"mime", MIME},
540 {"ocsp", OCSP},
541 {"off", OFF},
542 {"on", ON},
543 {"param", PARAM},
544 {"port", PORT},
545 {"prefork", PREFORK},
546 {"proto", PROTO},
547 {"protocols", PROTOCOLS},
548 {"proxy", PROXY},
549 {"relay-to", RELAY_TO},
550 {"require", REQUIRE},
551 {"return", RETURN},
552 {"root", ROOT},
553 {"server", SERVER},
554 {"sni", SNI},
555 {"spawn", SPAWN},
556 {"strip", STRIP},
557 {"tcp", TCP},
558 {"to-ext", TOEXT},
559 {"type", TYPE},
560 {"types", TYPES},
561 {"use-tls", USE_TLS},
562 {"user", USER},
563 {"verifyname", VERIFYNAME},
564 };
566 void
567 yyerror(const char *msg, ...)
569 va_list ap;
571 file->errors++;
573 va_start(ap, msg);
574 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
575 vfprintf(stderr, msg, ap);
576 fprintf(stderr, "\n");
577 va_end(ap);
580 void
581 yywarn(const char *msg, ...)
583 va_list ap;
585 va_start(ap, msg);
586 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
587 vfprintf(stderr, msg, ap);
588 fprintf(stderr, "\n");
589 va_end(ap);
592 int
593 kw_cmp(const void *k, const void *e)
595 return strcmp(k, ((struct keyword *)e)->word);
598 int
599 lookup(char *s)
601 const struct keyword *p;
603 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
604 sizeof(keywords[0]), kw_cmp);
606 if (p)
607 return p->token;
608 else
609 return STRING;
612 #define START_EXPAND 1
613 #define DONE_EXPAND 2
615 static int expanding;
617 int
618 igetc(void)
620 int c;
622 while (1) {
623 if (file->ungetpos > 0)
624 c = file->ungetbuf[--file->ungetpos];
625 else
626 c = getc(file->stream);
628 if (c == START_EXPAND)
629 expanding = 1;
630 else if (c == DONE_EXPAND)
631 expanding = 0;
632 else
633 break;
635 return c;
638 int
639 lgetc(int quotec)
641 int c, next;
643 if (quotec) {
644 if ((c = igetc()) == EOF) {
645 yyerror("reached end of file while parsing "
646 "quoted string");
647 if (file == topfile || popfile() == EOF)
648 return EOF;
649 return quotec;
651 return c;
654 while ((c = igetc()) == '\\') {
655 next = igetc();
656 if (next != '\n') {
657 c = next;
658 break;
660 yylval.lineno = file->lineno;
661 file->lineno++;
664 if (c == EOF) {
665 /*
666 * Fake EOL when hit EOF for the first time. This gets line
667 * count right if last line in included file is syntactically
668 * invalid and has no newline.
669 */
670 if (file->eof_reached == 0) {
671 file->eof_reached = 1;
672 return '\n';
674 while (c == EOF) {
675 if (file == topfile || popfile() == EOF)
676 return EOF;
677 c = igetc();
680 return c;
683 void
684 lungetc(int c)
686 if (c == EOF)
687 return;
689 if (file->ungetpos >= file->ungetsize) {
690 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
691 if (p == NULL)
692 err(1, "lungetc");
693 file->ungetbuf = p;
694 file->ungetsize *= 2;
696 file->ungetbuf[file->ungetpos++] = c;
699 int
700 findeol(void)
702 int c;
704 /* Skip to either EOF or the first real EOL. */
705 while (1) {
706 c = lgetc(0);
707 if (c == '\n') {
708 file->lineno++;
709 break;
711 if (c == EOF)
712 break;
714 return ERROR;
717 int
718 yylex(void)
720 char buf[8096];
721 char *p, *val;
722 int quotec, next, c;
723 int token;
725 top:
726 p = buf;
727 while ((c = lgetc(0)) == ' ' || c == '\t')
728 ; /* nothing */
730 yylval.lineno = file->lineno;
731 if (c == '#')
732 while ((c = lgetc(0)) != '\n' && c != EOF)
733 ; /* nothing */
734 if (c == '$' && !expanding) {
735 while (1) {
736 if ((c = lgetc(0)) == EOF)
737 return 0;
738 if (p + 1 >= buf + sizeof(buf) -1) {
739 yyerror("string too long");
740 return findeol();
742 if (isalnum(c) || c == '_') {
743 *p++ = c;
744 continue;
746 *p = '\0';
747 lungetc(c);
748 break;
750 val = symget(buf);
751 if (val == NULL) {
752 yyerror("macro `%s' not defined", buf);
753 return findeol();
755 yylval.v.string = xstrdup(val);
756 return STRING;
758 if (c == '@' && !expanding) {
759 while (1) {
760 if ((c = lgetc(0)) == EOF)
761 return 0;
763 if (p + 1 >= buf + sizeof(buf) - 1) {
764 yyerror("string too long");
765 return findeol();
767 if (isalnum(c) || c == '_') {
768 *p++ = c;
769 continue;
771 *p = '\0';
772 lungetc(c);
773 break;
775 val = symget(buf);
776 if (val == NULL) {
777 yyerror("macro '%s' not defined", buf);
778 return findeol();
780 p = val + strlen(val) - 1;
781 lungetc(DONE_EXPAND);
782 while (p >= val) {
783 lungetc(*p);
784 p--;
786 lungetc(START_EXPAND);
787 goto top;
790 switch (c) {
791 case '\'':
792 case '"':
793 quotec = c;
794 while (1) {
795 if ((c = lgetc(quotec)) == EOF)
796 return 0;
797 if (c == '\n') {
798 file->lineno++;
799 continue;
800 } else if (c == '\\') {
801 if ((next = lgetc(quotec)) == EOF)
802 return (0);
803 if (next == quotec || next == ' ' ||
804 next == '\t')
805 c = next;
806 else if (next == '\n') {
807 file->lineno++;
808 continue;
809 } else
810 lungetc(next);
811 } else if (c == quotec) {
812 *p = '\0';
813 break;
814 } else if (c == '\0') {
815 yyerror("invalid syntax");
816 return findeol();
818 if (p + 1 >= buf + sizeof(buf) - 1) {
819 yyerror("string too long");
820 return findeol();
822 *p++ = c;
824 yylval.v.string = strdup(buf);
825 if (yylval.v.string == NULL)
826 err(1, "yylex: strdup");
827 return STRING;
830 #define allowed_to_end_number(x) \
831 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
833 if (c == '-' || isdigit(c)) {
834 do {
835 *p++ = c;
836 if ((size_t)(p-buf) >= sizeof(buf)) {
837 yyerror("string too long");
838 return findeol();
840 } while ((c = lgetc(0)) != EOF && isdigit(c));
841 lungetc(c);
842 if (p == buf + 1 && buf[0] == '-')
843 goto nodigits;
844 if (c == EOF || allowed_to_end_number(c)) {
845 const char *errstr = NULL;
847 *p = '\0';
848 yylval.v.number = strtonum(buf, LLONG_MIN,
849 LLONG_MAX, &errstr);
850 if (errstr) {
851 yyerror("\"%s\" invalid number: %s",
852 buf, errstr);
853 return findeol();
855 return NUM;
856 } else {
857 nodigits:
858 while (p > buf + 1)
859 lungetc(*--p);
860 c = *--p;
861 if (c == '-')
862 return c;
866 #define allowed_in_string(x) \
867 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
868 x != '{' && x != '}' && \
869 x != '!' && x != '=' && x != '#' && \
870 x != ',' && x != ';'))
872 if (isalnum(c) || c == ':' || c == '_') {
873 do {
874 *p++ = c;
875 if ((size_t)(p-buf) >= sizeof(buf)) {
876 yyerror("string too long");
877 return findeol();
879 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
880 lungetc(c);
881 *p = '\0';
882 if ((token = lookup(buf)) == STRING)
883 yylval.v.string = xstrdup(buf);
884 return token;
886 if (c == '\n') {
887 yylval.lineno = file->lineno;
888 file->lineno++;
890 if (c == EOF)
891 return 0;
892 return c;
895 struct file *
896 pushfile(const char *name, int secret)
898 struct file *nfile;
900 nfile = xcalloc(1, sizeof(*nfile));
901 nfile->name = xstrdup(name);
902 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
903 log_warn(NULL, "can't open %s: %s", nfile->name,
904 strerror(errno));
905 free(nfile->name);
906 free(nfile);
907 return NULL;
909 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
910 nfile->ungetsize = 16;
911 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
912 TAILQ_INSERT_TAIL(&files, nfile, entry);
913 return nfile;
916 int
917 popfile(void)
919 struct file *prev;
921 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
922 prev->errors += file->errors;
924 TAILQ_REMOVE(&files, file, entry);
925 fclose(file->stream);
926 free(file->name);
927 free(file->ungetbuf);
928 free(file);
929 file = prev;
930 return file ? 0 : EOF;
933 void
934 parse_conf(const char *filename)
936 struct sym *sym, *next;
938 file = pushfile(filename, 0);
939 if (file == NULL)
940 exit(1);
941 topfile = file;
943 yyparse();
944 errors = file->errors;
945 popfile();
947 /* Free macros and check which have not been used. */
948 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
949 /* TODO: warn if !sym->used */
950 if (!sym->persist) {
951 free(sym->name);
952 free(sym->val);
953 TAILQ_REMOVE(&symhead, sym, entry);
954 free(sym);
958 if (errors)
959 exit(1);
962 void
963 print_conf(void)
965 struct vhost *h;
966 /* struct location *l; */
967 /* struct envlist *e; */
968 /* struct alist *a; */
970 if (conf.chroot != NULL)
971 printf("chroot \"%s\"\n", conf.chroot);
972 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
973 /* XXX: defined mimes? */
974 printf("port %d\n", conf.port);
975 printf("prefork %d\n", conf.prefork);
976 /* XXX: protocols? */
977 if (conf.user != NULL)
978 printf("user \"%s\"\n", conf.user);
980 TAILQ_FOREACH(h, &hosts, vhosts) {
981 printf("\nserver \"%s\" {\n", h->domain);
982 printf(" cert \"%s\"\n", h->cert);
983 printf(" key \"%s\"\n", h->key);
984 /* TODO: print locations... */
985 printf("}\n");
989 int
990 symset(const char *name, const char *val, int persist)
992 struct sym *sym;
994 TAILQ_FOREACH(sym, &symhead, entry) {
995 if (!strcmp(name, sym->name))
996 break;
999 if (sym != NULL) {
1000 if (sym->persist)
1001 return 0;
1002 else {
1003 free(sym->name);
1004 free(sym->val);
1005 TAILQ_REMOVE(&symhead, sym, entry);
1006 free(sym);
1010 sym = xcalloc(1, sizeof(*sym));
1011 sym->name = xstrdup(name);
1012 sym->val = xstrdup(val);
1013 sym->used = 0;
1014 sym->persist = persist;
1016 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1017 return 0;
1020 int
1021 cmdline_symset(char *s)
1023 char *sym, *val;
1024 int ret;
1026 if ((val = strrchr(s, '=')) == NULL)
1027 return -1;
1028 sym = xcalloc(1, val - s + 1);
1029 memcpy(sym, s, val - s);
1030 ret = symset(sym, val + 1, 1);
1031 free(sym);
1032 return ret;
1035 char *
1036 symget(const char *nam)
1038 struct sym *sym;
1040 TAILQ_FOREACH(sym, &symhead, entry) {
1041 if (strcmp(nam, sym->name) == 0) {
1042 sym->used = 1;
1043 return sym->val;
1046 return NULL;
1049 struct vhost *
1050 new_vhost(void)
1052 return xcalloc(1, sizeof(struct vhost));
1055 struct location *
1056 new_location(void)
1058 struct location *l;
1060 l = xcalloc(1, sizeof(*l));
1061 l->dirfd = -1;
1062 l->fcgi = -1;
1063 return l;
1066 struct proxy *
1067 new_proxy(void)
1069 struct proxy *p;
1071 p = xcalloc(1, sizeof(*p));
1072 p->protocols = TLS_PROTOCOLS_DEFAULT;
1073 return p;
1076 char *
1077 ensure_absolute_path(char *path)
1079 if (path == NULL || *path != '/')
1080 yyerror("not an absolute path: %s", path);
1081 return path;
1084 int
1085 check_block_code(int n)
1087 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1088 yyerror("invalid block code %d", n);
1089 return n;
1092 char *
1093 check_block_fmt(char *fmt)
1095 char *s;
1097 for (s = fmt; *s; ++s) {
1098 if (*s != '%')
1099 continue;
1100 switch (*++s) {
1101 case '%':
1102 case 'p':
1103 case 'q':
1104 case 'P':
1105 case 'N':
1106 break;
1107 default:
1108 yyerror("invalid format specifier %%%c", *s);
1112 return fmt;
1115 int
1116 check_strip_no(int n)
1118 if (n <= 0)
1119 yyerror("invalid strip number %d", n);
1120 return n;
1123 int
1124 check_port_num(int n)
1126 if (n <= 0 || n >= UINT16_MAX)
1127 yyerror("port number is %s: %d",
1128 n <= 0 ? "too small" : "too large",
1129 n);
1130 return n;
1133 int
1134 check_prefork_num(int n)
1136 if (n <= 0 || n >= PROC_MAX)
1137 yyerror("invalid prefork number %d", n);
1138 return n;
1141 void
1142 advance_loc(void)
1144 loc = new_location();
1145 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1148 void
1149 advance_proxy(void)
1151 proxy = new_proxy();
1152 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1155 void
1156 parsehp(char *str, char **host, const char **port, const char *def)
1158 char *at;
1159 const char *errstr;
1161 *host = str;
1163 if ((at = strchr(str, ':')) != NULL) {
1164 *at++ = '\0';
1165 *port = at;
1166 } else
1167 *port = def;
1169 strtonum(*port, 1, UINT16_MAX, &errstr);
1170 if (errstr != NULL)
1171 yyerror("port is %s: %s", errstr, *port);
1174 void
1175 only_once(const void *ptr, const char *name)
1177 if (ptr != NULL)
1178 yyerror("`%s' specified more than once", name);
1181 void
1182 only_oncei(int i, const char *name)
1184 if (i != -1)
1185 yyerror("`%s' specified more than once", name);
1188 int
1189 fastcgi_conf(char *path, char *port, char *prog)
1191 struct fcgi *f;
1192 int i;
1194 for (i = 0; i < FCGI_MAX; ++i) {
1195 f = &fcgi[i];
1197 if (f->path == NULL) {
1198 f->id = i;
1199 f->path = path;
1200 f->port = port;
1201 f->prog = prog;
1202 return i;
1205 /* XXX: what to do with prog? */
1206 if (!strcmp(f->path, path) &&
1207 ((port == NULL && f->port == NULL) ||
1208 !strcmp(f->port, port))) {
1209 free(path);
1210 free(port);
1211 return i;
1215 yyerror("too much `fastcgi' rules defined.");
1216 return -1;
1219 void
1220 add_param(char *name, char *val, int env)
1222 struct envlist *e;
1223 struct envhead *h;
1225 if (env)
1226 h = &host->env;
1227 else
1228 h = &host->params;
1230 e = xcalloc(1, sizeof(*e));
1231 e->name = name;
1232 e->value = val;
1233 if (TAILQ_EMPTY(h))
1234 TAILQ_INSERT_HEAD(h, e, envs);
1235 else
1236 TAILQ_INSERT_TAIL(h, e, envs);