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 <ctype.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #include "gmid.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 int errors;
104 typedef struct {
105 union {
106 char *string;
107 int number;
108 } v;
109 int lineno;
110 } YYSTYPE;
112 %}
114 /* for bison: */
115 /* %define parse.error verbose */
117 %token ALIAS AUTO
118 %token BLOCK
119 %token CA CERT CGI CHROOT CLIENT
120 %token DEFAULT
121 %token ENTRYPOINT ENV
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
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
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 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 varset : STRING '=' string {
187 char *s = $1;
188 while (*s++) {
189 if (isspace((unsigned char)*s)) {
190 yyerror("macro name cannot contain "
191 "whitespaces");
192 free($1);
193 free($3);
194 YYERROR;
197 symset($1, $3, 0);
198 free($1);
199 free($3);
203 option : CHROOT string { conf.chroot = $2; }
204 | IPV6 bool { conf.ipv6 = $2; }
205 | MIME STRING string {
206 yywarn("`mime MIME EXT' is deprecated and will be "
207 "removed in a future version, please use the new "
208 "syntax: `map MIME to-ext EXT'");
209 add_mime(&conf.mime, $2, $3);
211 | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
212 | PORT NUM { conf.port = check_port_num($2); }
213 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
214 | PROTOCOLS string {
215 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
216 yyerror("invalid protocols string \"%s\"", $2);
217 free($2);
219 | USER string { conf.user = $2; }
222 vhost : SERVER string {
223 host = new_vhost();
224 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
226 loc = new_location();
227 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
229 TAILQ_INIT(&host->proxies);
231 loc->match = xstrdup("*");
232 host->domain = $2;
234 if (strstr($2, "xn--") != NULL) {
235 yywarn("\"%s\" looks like punycode: you "
236 "should use the decoded hostname", $2);
238 } '{' optnl servbody '}' {
239 if (host->cert == NULL || host->key == NULL)
240 yyerror("invalid vhost definition: %s", $2);
242 | error '}' { yyerror("bad server directive"); }
245 servbody : /* empty */
246 | servbody servopt optnl
247 | servbody location optnl
248 | servbody proxy optnl
251 servopt : ALIAS string {
252 struct alist *a;
254 a = xcalloc(1, sizeof(*a));
255 a->alias = $2;
256 if (TAILQ_EMPTY(&host->aliases))
257 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
258 else
259 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
261 | CERT string {
262 only_once(host->cert, "cert");
263 host->cert = ensure_absolute_path($2);
265 | CGI string {
266 only_once(host->cgi, "cgi");
267 /* drop the starting '/', if any */
268 if (*$2 == '/')
269 memmove($2, $2+1, strlen($2));
270 host->cgi = $2;
272 | ENTRYPOINT string {
273 only_once(host->entrypoint, "entrypoint");
274 while (*$2 == '/')
275 memmove($2, $2+1, strlen($2));
276 host->entrypoint = $2;
278 | ENV string '=' string {
279 add_param($2, $4, 1);
281 | KEY string {
282 only_once(host->key, "key");
283 host->key = ensure_absolute_path($2);
285 | OCSP string {
286 only_once(host->ocsp, "ocsp");
287 host->ocsp = ensure_absolute_path($2);
289 | PARAM string '=' string {
290 add_param($2, $4, 0);
292 | locopt
295 proxy : PROXY { advance_proxy(); }
296 proxy_matches '{' optnl proxy_opts '}' {
297 if (proxy->host == NULL)
298 yyerror("invalid proxy block: missing `relay-to' option");
300 if ((proxy->cert == NULL && proxy->key != NULL) ||
301 (proxy->cert != NULL && proxy->key == NULL))
302 yyerror("invalid proxy block: missing cert or key");
306 proxy_matches : /* empty */
307 | proxy_matches proxy_match
310 proxy_match : PROTO string {
311 only_once(proxy->match_proto, "proxy proto");
312 free(proxy->match_proto);
313 proxy->match_proto = $2;
315 | FOR_HOST string {
316 only_once(proxy->match_host, "proxy for-host");
317 free(proxy->match_host);
318 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
322 proxy_opts : /* empty */
323 | proxy_opts proxy_opt optnl
326 proxy_opt : CERT string {
327 only_once(proxy->cert, "proxy cert");
328 tls_unload_file(proxy->cert, proxy->certlen);
329 ensure_absolute_path($2);
330 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
331 if (proxy->cert == NULL)
332 yyerror("can't load cert %s", $2);
333 free($2);
335 | KEY string {
336 only_once(proxy->key, "proxy key");
337 tls_unload_file(proxy->key, proxy->keylen);
338 ensure_absolute_path($2);
339 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
340 if (proxy->key == NULL)
341 yyerror("can't load key %s", $2);
342 free($2);
344 | PROTOCOLS string {
345 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
346 yyerror("invalid protocols string \"%s\"", $2);
347 free($2);
349 | RELAY_TO string {
350 only_once(proxy->host, "proxy relay-to");
351 free(proxy->host);
352 parsehp($2, &proxy->host, &proxy->port, "1965");
354 | REQUIRE CLIENT CA string {
355 only_once(proxy->reqca, "require client ca");
356 ensure_absolute_path($4);
357 if ((proxy->reqca = load_ca($4)) == NULL)
358 yyerror("couldn't load ca cert: %s", $4);
359 free($4);
361 | SNI string {
362 only_once(proxy->sni, "proxy sni");
363 free(proxy->sni);
364 proxy->sni = $2;
366 | USE_TLS bool {
367 proxy->notls = !$2;
369 | VERIFYNAME bool {
370 proxy->noverifyname = !$2;
374 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
375 /* drop the starting '/' if any */
376 if (*$3 == '/')
377 memmove($3, $3+1, strlen($3));
378 loc->match = $3;
380 | error '}'
383 locopts : /* empty */
384 | locopts locopt optnl
387 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
388 | BLOCK RETURN NUM string {
389 only_once(loc->block_fmt, "block");
390 loc->block_fmt = check_block_fmt($4);
391 loc->block_code = check_block_code($3);
393 | BLOCK RETURN NUM {
394 only_once(loc->block_fmt, "block");
395 loc->block_fmt = xstrdup("temporary failure");
396 loc->block_code = check_block_code($3);
397 if ($3 >= 30 && $3 < 40)
398 yyerror("missing `meta' for block return %d", $3);
400 | BLOCK {
401 only_once(loc->block_fmt, "block");
402 loc->block_fmt = xstrdup("temporary failure");
403 loc->block_code = 40;
405 | DEFAULT TYPE string {
406 only_once(loc->default_mime, "default type");
407 loc->default_mime = $3;
409 | FASTCGI fastcgi
410 | INDEX string {
411 only_once(loc->index, "index");
412 loc->index = $2;
414 | LANG string {
415 only_once(loc->lang, "lang");
416 loc->lang = $2;
418 | LOG bool { loc->disable_log = !$2; }
419 | REQUIRE CLIENT CA string {
420 only_once(loc->reqca, "require client ca");
421 ensure_absolute_path($4);
422 if ((loc->reqca = load_ca($4)) == NULL)
423 yyerror("couldn't load ca cert: %s", $4);
424 free($4);
426 | ROOT string {
427 only_once(loc->dir, "root");
428 loc->dir = ensure_absolute_path($2);
430 | STRIP NUM { loc->strip = check_strip_no($2); }
433 fastcgi : SPAWN string {
434 only_oncei(loc->fcgi, "fastcgi");
435 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
437 | string {
438 only_oncei(loc->fcgi, "fastcgi");
439 loc->fcgi = fastcgi_conf($1, NULL, NULL);
441 | TCP string PORT NUM {
442 char *c;
443 if (asprintf(&c, "%d", $4) == -1)
444 err(1, "asprintf");
445 only_oncei(loc->fcgi, "fastcgi");
446 loc->fcgi = fastcgi_conf($2, c, NULL);
448 | TCP string {
449 only_oncei(loc->fcgi, "fastcgi");
450 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
452 | TCP string PORT string {
453 only_oncei(loc->fcgi, "fastcgi");
454 loc->fcgi = fastcgi_conf($2, $4, NULL);
458 optnl : '\n' optnl /* zero or more newlines */
459 | ';' optnl /* semicolons too */
460 | /*empty*/
463 %%
465 static struct keyword {
466 const char *word;
467 int token;
468 } keywords[] = {
469 /* these MUST be sorted */
470 {"alias", ALIAS},
471 {"auto", AUTO},
472 {"block", BLOCK},
473 {"ca", CA},
474 {"cert", CERT},
475 {"cgi", CGI},
476 {"chroot", CHROOT},
477 {"client", CLIENT},
478 {"default", DEFAULT},
479 {"entrypoint", ENTRYPOINT},
480 {"env", ENV},
481 {"fastcgi", FASTCGI},
482 {"for-host", FOR_HOST},
483 {"index", INDEX},
484 {"ipv6", IPV6},
485 {"key", KEY},
486 {"lang", LANG},
487 {"location", LOCATION},
488 {"log", LOG},
489 {"map", MAP},
490 {"mime", MIME},
491 {"ocsp", OCSP},
492 {"off", OFF},
493 {"on", ON},
494 {"param", PARAM},
495 {"port", PORT},
496 {"prefork", PREFORK},
497 {"proto", PROTO},
498 {"protocols", PROTOCOLS},
499 {"proxy", PROXY},
500 {"relay-to", RELAY_TO},
501 {"require", REQUIRE},
502 {"return", RETURN},
503 {"root", ROOT},
504 {"server", SERVER},
505 {"sni", SNI},
506 {"spawn", SPAWN},
507 {"strip", STRIP},
508 {"tcp", TCP},
509 {"to-ext", TOEXT},
510 {"type", TYPE},
511 {"use-tls", USE_TLS},
512 {"user", USER},
513 {"verifyname", VERIFYNAME},
514 };
516 void
517 yyerror(const char *msg, ...)
519 va_list ap;
521 file->errors++;
523 va_start(ap, msg);
524 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
525 vfprintf(stderr, msg, ap);
526 fprintf(stderr, "\n");
527 va_end(ap);
530 void
531 yywarn(const char *msg, ...)
533 va_list ap;
535 va_start(ap, msg);
536 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
537 vfprintf(stderr, msg, ap);
538 fprintf(stderr, "\n");
539 va_end(ap);
542 int
543 kw_cmp(const void *k, const void *e)
545 return strcmp(k, ((struct keyword *)e)->word);
548 int
549 lookup(char *s)
551 const struct keyword *p;
553 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
554 sizeof(keywords[0]), kw_cmp);
556 if (p)
557 return p->token;
558 else
559 return STRING;
562 #define START_EXPAND 1
563 #define DONE_EXPAND 2
565 static int expanding;
567 int
568 igetc(void)
570 int c;
572 while (1) {
573 if (file->ungetpos > 0)
574 c = file->ungetbuf[--file->ungetpos];
575 else
576 c = getc(file->stream);
578 if (c == START_EXPAND)
579 expanding = 1;
580 else if (c == DONE_EXPAND)
581 expanding = 0;
582 else
583 break;
585 return c;
588 int
589 lgetc(int quotec)
591 int c, next;
593 if (quotec) {
594 if ((c = igetc()) == EOF) {
595 yyerror("reached end of file while parsing "
596 "quoted string");
597 if (file == topfile || popfile() == EOF)
598 return EOF;
599 return quotec;
601 return c;
604 while ((c = igetc()) == '\\') {
605 next = igetc();
606 if (next != '\n') {
607 c = next;
608 break;
610 yylval.lineno = file->lineno;
611 file->lineno++;
614 if (c == EOF) {
615 /*
616 * Fake EOL when hit EOF for the first time. This gets line
617 * count right if last line in included file is syntactically
618 * invalid and has no newline.
619 */
620 if (file->eof_reached == 0) {
621 file->eof_reached = 1;
622 return '\n';
624 while (c == EOF) {
625 if (file == topfile || popfile() == EOF)
626 return EOF;
627 c = igetc();
630 return c;
633 void
634 lungetc(int c)
636 if (c == EOF)
637 return;
639 if (file->ungetpos >= file->ungetsize) {
640 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
641 if (p == NULL)
642 err(1, "lungetc");
643 file->ungetbuf = p;
644 file->ungetsize *= 2;
646 file->ungetbuf[file->ungetpos++] = c;
649 int
650 findeol(void)
652 int c;
654 /* Skip to either EOF or the first real EOL. */
655 while (1) {
656 c = lgetc(0);
657 if (c == '\n') {
658 file->lineno++;
659 break;
661 if (c == EOF)
662 break;
664 return ERROR;
667 int
668 yylex(void)
670 char buf[8096];
671 char *p, *val;
672 int quotec, next, c;
673 int token;
675 top:
676 p = buf;
677 while ((c = lgetc(0)) == ' ' || c == '\t')
678 ; /* nothing */
680 yylval.lineno = file->lineno;
681 if (c == '#')
682 while ((c = lgetc(0)) != '\n' && c != EOF)
683 ; /* nothing */
684 if (c == '$' && !expanding) {
685 while (1) {
686 if ((c = lgetc(0)) == EOF)
687 return 0;
688 if (p + 1 >= buf + sizeof(buf) -1) {
689 yyerror("string too long");
690 return findeol();
692 if (isalnum(c) || c == '_') {
693 *p++ = c;
694 continue;
696 *p = '\0';
697 lungetc(c);
698 break;
700 val = symget(buf);
701 if (val == NULL) {
702 yyerror("macro `%s' not defined", buf);
703 return findeol();
705 yylval.v.string = xstrdup(val);
706 return STRING;
708 if (c == '@' && !expanding) {
709 while (1) {
710 if ((c = lgetc(0)) == EOF)
711 return 0;
713 if (p + 1 >= buf + sizeof(buf) - 1) {
714 yyerror("string too long");
715 return findeol();
717 if (isalnum(c) || c == '_') {
718 *p++ = c;
719 continue;
721 *p = '\0';
722 lungetc(c);
723 break;
725 val = symget(buf);
726 if (val == NULL) {
727 yyerror("macro '%s' not defined", buf);
728 return findeol();
730 p = val + strlen(val) - 1;
731 lungetc(DONE_EXPAND);
732 while (p >= val) {
733 lungetc(*p);
734 p--;
736 lungetc(START_EXPAND);
737 goto top;
740 switch (c) {
741 case '\'':
742 case '"':
743 quotec = c;
744 while (1) {
745 if ((c = lgetc(quotec)) == EOF)
746 return 0;
747 if (c == '\n') {
748 file->lineno++;
749 continue;
750 } else if (c == '\\') {
751 if ((next = lgetc(quotec)) == EOF)
752 return (0);
753 if (next == quotec || next == ' ' ||
754 next == '\t')
755 c = next;
756 else if (next == '\n') {
757 file->lineno++;
758 continue;
759 } else
760 lungetc(next);
761 } else if (c == quotec) {
762 *p = '\0';
763 break;
764 } else if (c == '\0') {
765 yyerror("invalid syntax");
766 return findeol();
768 if (p + 1 >= buf + sizeof(buf) - 1) {
769 yyerror("string too long");
770 return findeol();
772 *p++ = c;
774 yylval.v.string = strdup(buf);
775 if (yylval.v.string == NULL)
776 err(1, "yylex: strdup");
777 return STRING;
780 #define allowed_to_end_number(x) \
781 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
783 if (c == '-' || isdigit(c)) {
784 do {
785 *p++ = c;
786 if ((size_t)(p-buf) >= sizeof(buf)) {
787 yyerror("string too long");
788 return findeol();
790 } while ((c = lgetc(0)) != EOF && isdigit(c));
791 lungetc(c);
792 if (p == buf + 1 && buf[0] == '-')
793 goto nodigits;
794 if (c == EOF || allowed_to_end_number(c)) {
795 const char *errstr = NULL;
797 *p = '\0';
798 yylval.v.number = strtonum(buf, LLONG_MIN,
799 LLONG_MAX, &errstr);
800 if (errstr) {
801 yyerror("\"%s\" invalid number: %s",
802 buf, errstr);
803 return findeol();
805 return NUM;
806 } else {
807 nodigits:
808 while (p > buf + 1)
809 lungetc(*--p);
810 c = *--p;
811 if (c == '-')
812 return c;
816 #define allowed_in_string(x) \
817 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
818 x != '{' && x != '}' && \
819 x != '!' && x != '=' && x != '#' && \
820 x != ',' && x != ';'))
822 if (isalnum(c) || c == ':' || c == '_') {
823 do {
824 *p++ = c;
825 if ((size_t)(p-buf) >= sizeof(buf)) {
826 yyerror("string too long");
827 return findeol();
829 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
830 lungetc(c);
831 *p = '\0';
832 if ((token = lookup(buf)) == STRING)
833 yylval.v.string = xstrdup(buf);
834 return token;
836 if (c == '\n') {
837 yylval.lineno = file->lineno;
838 file->lineno++;
840 if (c == EOF)
841 return 0;
842 return c;
845 struct file *
846 pushfile(const char *name, int secret)
848 struct file *nfile;
850 nfile = xcalloc(1, sizeof(*nfile));
851 nfile->name = xstrdup(name);
852 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
853 log_warn(NULL, "can't open %s: %s", nfile->name,
854 strerror(errno));
855 free(nfile->name);
856 free(nfile);
857 return NULL;
859 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
860 nfile->ungetsize = 16;
861 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
862 TAILQ_INSERT_TAIL(&files, nfile, entry);
863 return nfile;
866 int
867 popfile(void)
869 struct file *prev;
871 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
872 prev->errors += file->errors;
874 TAILQ_REMOVE(&files, file, entry);
875 fclose(file->stream);
876 free(file->name);
877 free(file->ungetbuf);
878 free(file);
879 file = prev;
880 return file ? 0 : EOF;
883 void
884 parse_conf(const char *filename)
886 struct sym *sym, *next;
888 file = pushfile(filename, 0);
889 if (file == NULL)
890 exit(1);
891 topfile = file;
893 yyparse();
894 errors = file->errors;
895 popfile();
897 /* Free macros and check which have not been used. */
898 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
899 /* TODO: warn if !sym->used */
900 if (!sym->persist) {
901 free(sym->name);
902 free(sym->val);
903 TAILQ_REMOVE(&symhead, sym, entry);
904 free(sym);
908 if (errors)
909 exit(1);
912 void
913 print_conf(void)
915 struct vhost *h;
916 /* struct location *l; */
917 /* struct envlist *e; */
918 /* struct alist *a; */
920 if (conf.chroot != NULL)
921 printf("chroot \"%s\"\n", conf.chroot);
922 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
923 /* XXX: defined mimes? */
924 printf("port %d\n", conf.port);
925 printf("prefork %d\n", conf.prefork);
926 /* XXX: protocols? */
927 if (conf.user != NULL)
928 printf("user \"%s\"\n", conf.user);
930 TAILQ_FOREACH(h, &hosts, vhosts) {
931 printf("\nserver \"%s\" {\n", h->domain);
932 printf(" cert \"%s\"\n", h->cert);
933 printf(" key \"%s\"\n", h->key);
934 /* TODO: print locations... */
935 printf("}\n");
939 int
940 symset(const char *name, const char *val, int persist)
942 struct sym *sym;
944 TAILQ_FOREACH(sym, &symhead, entry) {
945 if (!strcmp(name, sym->name))
946 break;
949 if (sym != NULL) {
950 if (sym->persist)
951 return 0;
952 else {
953 free(sym->name);
954 free(sym->val);
955 TAILQ_REMOVE(&symhead, sym, entry);
956 free(sym);
960 sym = xcalloc(1, sizeof(*sym));
961 sym->name = xstrdup(name);
962 sym->val = xstrdup(val);
963 sym->used = 0;
964 sym->persist = persist;
966 TAILQ_INSERT_TAIL(&symhead, sym, entry);
967 return 0;
970 int
971 cmdline_symset(char *s)
973 char *sym, *val;
974 int ret;
976 if ((val = strrchr(s, '=')) == NULL)
977 return -1;
978 sym = xcalloc(1, val - s + 1);
979 memcpy(sym, s, val - s);
980 ret = symset(sym, val + 1, 1);
981 free(sym);
982 return ret;
985 char *
986 symget(const char *nam)
988 struct sym *sym;
990 TAILQ_FOREACH(sym, &symhead, entry) {
991 if (strcmp(nam, sym->name) == 0) {
992 sym->used = 1;
993 return sym->val;
996 return NULL;
999 struct vhost *
1000 new_vhost(void)
1002 return xcalloc(1, sizeof(struct vhost));
1005 struct location *
1006 new_location(void)
1008 struct location *l;
1010 l = xcalloc(1, sizeof(*l));
1011 l->dirfd = -1;
1012 l->fcgi = -1;
1013 return l;
1016 struct proxy *
1017 new_proxy(void)
1019 struct proxy *p;
1021 p = xcalloc(1, sizeof(*p));
1022 p->protocols = TLS_PROTOCOLS_DEFAULT;
1023 return p;
1026 char *
1027 ensure_absolute_path(char *path)
1029 if (path == NULL || *path != '/')
1030 yyerror("not an absolute path: %s", path);
1031 return path;
1034 int
1035 check_block_code(int n)
1037 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1038 yyerror("invalid block code %d", n);
1039 return n;
1042 char *
1043 check_block_fmt(char *fmt)
1045 char *s;
1047 for (s = fmt; *s; ++s) {
1048 if (*s != '%')
1049 continue;
1050 switch (*++s) {
1051 case '%':
1052 case 'p':
1053 case 'q':
1054 case 'P':
1055 case 'N':
1056 break;
1057 default:
1058 yyerror("invalid format specifier %%%c", *s);
1062 return fmt;
1065 int
1066 check_strip_no(int n)
1068 if (n <= 0)
1069 yyerror("invalid strip number %d", n);
1070 return n;
1073 int
1074 check_port_num(int n)
1076 if (n <= 0 || n >= UINT16_MAX)
1077 yyerror("port number is %s: %d",
1078 n <= 0 ? "too small" : "too large",
1079 n);
1080 return n;
1083 int
1084 check_prefork_num(int n)
1086 if (n <= 0 || n >= PROC_MAX)
1087 yyerror("invalid prefork number %d", n);
1088 return n;
1091 void
1092 advance_loc(void)
1094 loc = new_location();
1095 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1098 void
1099 advance_proxy(void)
1101 proxy = new_proxy();
1102 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1105 void
1106 parsehp(char *str, char **host, const char **port, const char *def)
1108 char *at;
1109 const char *errstr;
1111 *host = str;
1113 if ((at = strchr(str, ':')) != NULL) {
1114 *at++ = '\0';
1115 *port = at;
1116 } else
1117 *port = def;
1119 strtonum(*port, 1, UINT16_MAX, &errstr);
1120 if (errstr != NULL)
1121 yyerror("port is %s: %s", errstr, *port);
1124 void
1125 only_once(const void *ptr, const char *name)
1127 if (ptr != NULL)
1128 yyerror("`%s' specified more than once", name);
1131 void
1132 only_oncei(int i, const char *name)
1134 if (i != -1)
1135 yyerror("`%s' specified more than once", name);
1138 int
1139 fastcgi_conf(char *path, char *port, char *prog)
1141 struct fcgi *f;
1142 int i;
1144 for (i = 0; i < FCGI_MAX; ++i) {
1145 f = &fcgi[i];
1147 if (f->path == NULL) {
1148 f->id = i;
1149 f->path = path;
1150 f->port = port;
1151 f->prog = prog;
1152 return i;
1155 /* XXX: what to do with prog? */
1156 if (!strcmp(f->path, path) &&
1157 ((port == NULL && f->port == NULL) ||
1158 !strcmp(f->port, port))) {
1159 free(path);
1160 free(port);
1161 return i;
1165 yyerror("too much `fastcgi' rules defined.");
1166 return -1;
1169 void
1170 add_param(char *name, char *val, int env)
1172 struct envlist *e;
1173 struct envhead *h;
1175 if (env)
1176 h = &host->env;
1177 else
1178 h = &host->params;
1180 e = xcalloc(1, sizeof(*e));
1181 e->name = name;
1182 e->value = val;
1183 if (TAILQ_EMPTY(h))
1184 TAILQ_INSERT_HEAD(h, e, envs);
1185 else
1186 TAILQ_INSERT_TAIL(h, e, envs);