Blob


1 %{
3 /*
4 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
5 * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
6 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
7 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
8 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
9 * Copyright (c) 2001 Markus Friedl. All rights reserved.
10 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
11 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
12 *
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 */
26 #include "gmid.h"
28 #include <ctype.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
36 static struct file {
37 TAILQ_ENTRY(file) entry;
38 FILE *stream;
39 char *name;
40 size_t ungetpos;
41 size_t ungetsize;
42 u_char *ungetbuf;
43 int eof_reached;
44 int lineno;
45 int errors;
46 } *file, *topfile;
48 struct file *pushfile(const char *, int);
49 int popfile(void);
50 int yyparse(void);
51 int yylex(void);
52 void yyerror(const char *, ...)
53 __attribute__((__format__ (printf, 1, 2)))
54 __attribute__((__nonnull__ (1)));
55 void yywarn(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 int kw_cmp(const void *, const void *);
59 int lookup(char *);
60 int igetc(void);
61 int lgetc(int);
62 void lungetc(int);
63 int findeol(void);
65 /*
66 * #define YYDEBUG 1
67 * int yydebug = 1;
68 */
70 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
71 struct sym {
72 TAILQ_ENTRY(sym) entry;
73 int used;
74 int persist;
75 char *name;
76 char *val;
77 };
79 int symset(const char *, const char *, int);
80 char *symget(const char *);
82 struct vhost *new_vhost(void);
83 struct location *new_location(void);
84 struct proxy *new_proxy(void);
85 char *ensure_absolute_path(char*);
86 int check_block_code(int);
87 char *check_block_fmt(char*);
88 int check_strip_no(int);
89 int check_port_num(int);
90 int check_prefork_num(int);
91 void advance_loc(void);
92 void advance_proxy(void);
93 void parsehp(char *, char **, const char **, const char *);
94 void only_once(const void*, const char*);
95 void only_oncei(int, const char*);
96 int fastcgi_conf(char *, char *, char *);
97 void add_param(char *, char *);
99 static struct vhost *host;
100 static struct location *loc;
101 static struct proxy *proxy;
102 static char *current_media;
103 static int errors;
105 typedef struct {
106 union {
107 char *string;
108 int number;
109 } v;
110 int lineno;
111 } YYSTYPE;
113 %}
115 /* for bison: */
116 /* %define parse.error verbose */
118 %token ALIAS AUTO
119 %token BLOCK
120 %token CA CERT CHROOT CLIENT
121 %token DEFAULT
122 %token FASTCGI FOR_HOST
123 %token INCLUDE INDEX IPV6
124 %token KEY
125 %token LANG LOCATION LOG
126 %token OCSP OFF ON
127 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
128 %token RELAY_TO REQUIRE RETURN ROOT
129 %token SERVER SNI SPAWN STRIP
130 %token TCP TOEXT TYPE TYPES
131 %token USE_TLS USER
132 %token VERIFYNAME
134 %token ERROR
136 %token <v.string> STRING
137 %token <v.number> NUM
139 %type <v.number> bool
140 %type <v.string> string numberstring
142 %%
144 conf : /* empty */
145 | conf include '\n'
146 | conf '\n'
147 | conf varset '\n'
148 | conf option '\n'
149 | conf vhost '\n'
150 | conf types '\n'
151 | conf error '\n' { file->errors++; }
154 include : INCLUDE STRING {
155 struct file *nfile;
157 if ((nfile = pushfile($2, 0)) == NULL) {
158 yyerror("failed to include file %s", $2);
159 free($2);
160 YYERROR;
162 free($2);
164 file = nfile;
165 lungetc('\n');
169 bool : ON { $$ = 1; }
170 | OFF { $$ = 0; }
173 string : string STRING {
174 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
175 free($1);
176 free($2);
177 yyerror("string: asprintf: %s", strerror(errno));
178 YYERROR;
180 free($1);
181 free($2);
183 | STRING
186 numberstring : NUM {
187 char *s;
188 if (asprintf(&s, "%d", $1) == -1) {
189 yyerror("asprintf: number");
190 YYERROR;
192 $$ = s;
194 | STRING
197 varset : STRING '=' string {
198 char *s = $1;
199 while (*s++) {
200 if (isspace((unsigned char)*s)) {
201 yyerror("macro name cannot contain "
202 "whitespaces");
203 free($1);
204 free($3);
205 YYERROR;
208 symset($1, $3, 0);
209 free($1);
210 free($3);
214 option : CHROOT string {
215 if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >=
216 sizeof(conf.chroot))
217 yyerror("chroot path too long");
218 free($2);
220 | IPV6 bool { conf.ipv6 = $2; }
221 | PORT NUM { conf.port = check_port_num($2); }
222 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
223 | PROTOCOLS string {
224 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
225 yyerror("invalid protocols string \"%s\"", $2);
226 free($2);
228 | USER string {
229 if (strlcpy(conf.user, $2, sizeof(conf.user)) >=
230 sizeof(conf.user))
231 yyerror("user name too long");
232 free($2);
236 vhost : SERVER string {
237 host = new_vhost();
238 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
240 loc = new_location();
241 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
243 TAILQ_INIT(&host->proxies);
245 loc->match = xstrdup("*");
246 host->domain = $2;
248 if (strstr($2, "xn--") != NULL) {
249 yywarn("\"%s\" looks like punycode: you "
250 "should use the decoded hostname", $2);
252 } '{' optnl servbody '}' {
253 if (host->cert == NULL || host->key == NULL)
254 yyerror("invalid vhost definition: %s", $2);
256 | error '}' { yyerror("bad server directive"); }
259 servbody : /* empty */
260 | servbody servopt optnl
261 | servbody location optnl
262 | servbody proxy optnl
265 servopt : ALIAS string {
266 struct alist *a;
268 a = xcalloc(1, sizeof(*a));
269 a->alias = $2;
270 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
272 | CERT string {
273 only_once(host->cert, "cert");
274 host->cert = ensure_absolute_path($2);
276 | KEY string {
277 only_once(host->key, "key");
278 host->key = ensure_absolute_path($2);
280 | OCSP string {
281 only_once(host->ocsp, "ocsp");
282 host->ocsp = ensure_absolute_path($2);
284 | PARAM string '=' string {
285 add_param($2, $4);
287 | locopt
290 proxy : PROXY { advance_proxy(); }
291 proxy_matches '{' optnl proxy_opts '}' {
292 if (proxy->host == NULL)
293 yyerror("invalid proxy block: missing `relay-to' option");
295 if ((proxy->cert == NULL && proxy->key != NULL) ||
296 (proxy->cert != NULL && proxy->key == NULL))
297 yyerror("invalid proxy block: missing cert or key");
301 proxy_matches : /* empty */
302 | proxy_matches proxy_match
305 proxy_match : PROTO string {
306 only_once(proxy->match_proto, "proxy proto");
307 free(proxy->match_proto);
308 proxy->match_proto = $2;
310 | FOR_HOST string {
311 only_once(proxy->match_host, "proxy for-host");
312 free(proxy->match_host);
313 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
317 proxy_opts : /* empty */
318 | proxy_opts proxy_opt optnl
321 proxy_opt : CERT string {
322 only_once(proxy->cert, "proxy cert");
323 tls_unload_file(proxy->cert, proxy->certlen);
324 ensure_absolute_path($2);
325 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
326 if (proxy->cert == NULL)
327 yyerror("can't load cert %s", $2);
328 free($2);
330 | KEY string {
331 only_once(proxy->key, "proxy key");
332 tls_unload_file(proxy->key, proxy->keylen);
333 ensure_absolute_path($2);
334 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
335 if (proxy->key == NULL)
336 yyerror("can't load key %s", $2);
337 free($2);
339 | PROTOCOLS string {
340 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
341 yyerror("invalid protocols string \"%s\"", $2);
342 free($2);
344 | RELAY_TO string {
345 only_once(proxy->host, "proxy relay-to");
346 free(proxy->host);
347 parsehp($2, &proxy->host, &proxy->port, "1965");
349 | REQUIRE CLIENT CA string {
350 only_once(proxy->reqca, "require client ca");
351 ensure_absolute_path($4);
352 if ((proxy->reqca = load_ca($4)) == NULL)
353 yyerror("couldn't load ca cert: %s", $4);
354 free($4);
356 | SNI string {
357 only_once(proxy->sni, "proxy sni");
358 free(proxy->sni);
359 proxy->sni = $2;
361 | USE_TLS bool {
362 proxy->notls = !$2;
364 | VERIFYNAME bool {
365 proxy->noverifyname = !$2;
369 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
370 /* drop the starting '/' if any */
371 if (*$3 == '/')
372 memmove($3, $3+1, strlen($3));
373 loc->match = $3;
375 | error '}'
378 locopts : /* empty */
379 | locopts locopt optnl
382 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
383 | BLOCK RETURN NUM string {
384 only_once(loc->block_fmt, "block");
385 loc->block_fmt = check_block_fmt($4);
386 loc->block_code = check_block_code($3);
388 | BLOCK RETURN NUM {
389 only_once(loc->block_fmt, "block");
390 loc->block_fmt = xstrdup("temporary failure");
391 loc->block_code = check_block_code($3);
392 if ($3 >= 30 && $3 < 40)
393 yyerror("missing `meta' for block return %d", $3);
395 | BLOCK {
396 only_once(loc->block_fmt, "block");
397 loc->block_fmt = xstrdup("temporary failure");
398 loc->block_code = 40;
400 | DEFAULT TYPE string {
401 only_once(loc->default_mime, "default type");
402 loc->default_mime = $3;
404 | FASTCGI fastcgi
405 | INDEX string {
406 only_once(loc->index, "index");
407 loc->index = $2;
409 | LANG string {
410 only_once(loc->lang, "lang");
411 loc->lang = $2;
413 | LOG bool { loc->disable_log = !$2; }
414 | REQUIRE CLIENT CA string {
415 only_once(loc->reqca, "require client ca");
416 ensure_absolute_path($4);
417 if ((loc->reqca = load_ca($4)) == NULL)
418 yyerror("couldn't load ca cert: %s", $4);
419 free($4);
421 | ROOT string {
422 only_once(loc->dir, "root");
423 loc->dir = ensure_absolute_path($2);
425 | STRIP NUM { loc->strip = check_strip_no($2); }
428 fastcgi : SPAWN string {
429 only_oncei(loc->fcgi, "fastcgi");
430 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
432 | string {
433 only_oncei(loc->fcgi, "fastcgi");
434 loc->fcgi = fastcgi_conf($1, NULL, NULL);
436 | TCP string PORT NUM {
437 char *c;
438 if (asprintf(&c, "%d", $4) == -1)
439 err(1, "asprintf");
440 only_oncei(loc->fcgi, "fastcgi");
441 loc->fcgi = fastcgi_conf($2, c, NULL);
443 | TCP string {
444 only_oncei(loc->fcgi, "fastcgi");
445 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
447 | TCP string PORT string {
448 only_oncei(loc->fcgi, "fastcgi");
449 loc->fcgi = fastcgi_conf($2, $4, NULL);
453 types : TYPES '{' optnl mediaopts_l '}' ;
455 mediaopts_l : mediaopts_l mediaoptsl nl
456 | mediaoptsl nl
459 mediaoptsl : STRING {
460 free(current_media);
461 current_media = $1;
462 } medianames_l optsemicolon
463 | include
466 medianames_l : medianames_l medianamesl
467 | medianamesl
470 medianamesl : numberstring {
471 if (add_mime(&conf.mime, current_media, $1) == -1)
472 err(1, "add_mime");
473 free($1);
477 nl : '\n' optnl
480 optnl : '\n' optnl /* zero or more newlines */
481 | ';' optnl /* semicolons too */
482 | /*empty*/
485 optsemicolon : ';'
489 %%
491 static const struct keyword {
492 const char *word;
493 int token;
494 } keywords[] = {
495 /* these MUST be sorted */
496 {"alias", ALIAS},
497 {"auto", AUTO},
498 {"block", BLOCK},
499 {"ca", CA},
500 {"cert", CERT},
501 {"chroot", CHROOT},
502 {"client", CLIENT},
503 {"default", DEFAULT},
504 {"fastcgi", FASTCGI},
505 {"for-host", FOR_HOST},
506 {"include", INCLUDE},
507 {"index", INDEX},
508 {"ipv6", IPV6},
509 {"key", KEY},
510 {"lang", LANG},
511 {"location", LOCATION},
512 {"log", LOG},
513 {"ocsp", OCSP},
514 {"off", OFF},
515 {"on", ON},
516 {"param", PARAM},
517 {"port", PORT},
518 {"prefork", PREFORK},
519 {"proto", PROTO},
520 {"protocols", PROTOCOLS},
521 {"proxy", PROXY},
522 {"relay-to", RELAY_TO},
523 {"require", REQUIRE},
524 {"return", RETURN},
525 {"root", ROOT},
526 {"server", SERVER},
527 {"sni", SNI},
528 {"spawn", SPAWN},
529 {"strip", STRIP},
530 {"tcp", TCP},
531 {"to-ext", TOEXT},
532 {"type", TYPE},
533 {"types", TYPES},
534 {"use-tls", USE_TLS},
535 {"user", USER},
536 {"verifyname", VERIFYNAME},
537 };
539 void
540 yyerror(const char *msg, ...)
542 va_list ap;
544 file->errors++;
546 va_start(ap, msg);
547 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
548 vfprintf(stderr, msg, ap);
549 fprintf(stderr, "\n");
550 va_end(ap);
553 void
554 yywarn(const char *msg, ...)
556 va_list ap;
558 va_start(ap, msg);
559 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
560 vfprintf(stderr, msg, ap);
561 fprintf(stderr, "\n");
562 va_end(ap);
565 int
566 kw_cmp(const void *k, const void *e)
568 return strcmp(k, ((struct keyword *)e)->word);
571 int
572 lookup(char *s)
574 const struct keyword *p;
576 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
577 sizeof(keywords[0]), kw_cmp);
579 if (p)
580 return p->token;
581 else
582 return STRING;
585 #define START_EXPAND 1
586 #define DONE_EXPAND 2
588 static int expanding;
590 int
591 igetc(void)
593 int c;
595 while (1) {
596 if (file->ungetpos > 0)
597 c = file->ungetbuf[--file->ungetpos];
598 else
599 c = getc(file->stream);
601 if (c == START_EXPAND)
602 expanding = 1;
603 else if (c == DONE_EXPAND)
604 expanding = 0;
605 else
606 break;
608 return c;
611 int
612 lgetc(int quotec)
614 int c, next;
616 if (quotec) {
617 if ((c = igetc()) == EOF) {
618 yyerror("reached end of file while parsing "
619 "quoted string");
620 if (file == topfile || popfile() == EOF)
621 return EOF;
622 return quotec;
624 return c;
627 while ((c = igetc()) == '\\') {
628 next = igetc();
629 if (next != '\n') {
630 c = next;
631 break;
633 yylval.lineno = file->lineno;
634 file->lineno++;
637 if (c == EOF) {
638 /*
639 * Fake EOL when hit EOF for the first time. This gets line
640 * count right if last line in included file is syntactically
641 * invalid and has no newline.
642 */
643 if (file->eof_reached == 0) {
644 file->eof_reached = 1;
645 return '\n';
647 while (c == EOF) {
648 if (file == topfile || popfile() == EOF)
649 return EOF;
650 c = igetc();
653 return c;
656 void
657 lungetc(int c)
659 if (c == EOF)
660 return;
662 if (file->ungetpos >= file->ungetsize) {
663 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
664 if (p == NULL)
665 err(1, "lungetc");
666 file->ungetbuf = p;
667 file->ungetsize *= 2;
669 file->ungetbuf[file->ungetpos++] = c;
672 int
673 findeol(void)
675 int c;
677 /* Skip to either EOF or the first real EOL. */
678 while (1) {
679 c = lgetc(0);
680 if (c == '\n') {
681 file->lineno++;
682 break;
684 if (c == EOF)
685 break;
687 return ERROR;
690 int
691 yylex(void)
693 char buf[8096];
694 char *p, *val;
695 int quotec, next, c;
696 int token;
698 top:
699 p = buf;
700 while ((c = lgetc(0)) == ' ' || c == '\t')
701 ; /* nothing */
703 yylval.lineno = file->lineno;
704 if (c == '#')
705 while ((c = lgetc(0)) != '\n' && c != EOF)
706 ; /* nothing */
707 if (c == '$' && !expanding) {
708 while (1) {
709 if ((c = lgetc(0)) == EOF)
710 return 0;
711 if (p + 1 >= buf + sizeof(buf) -1) {
712 yyerror("string too long");
713 return findeol();
715 if (isalnum(c) || c == '_') {
716 *p++ = c;
717 continue;
719 *p = '\0';
720 lungetc(c);
721 break;
723 val = symget(buf);
724 if (val == NULL) {
725 yyerror("macro `%s' not defined", buf);
726 return findeol();
728 yylval.v.string = xstrdup(val);
729 return STRING;
731 if (c == '@' && !expanding) {
732 while (1) {
733 if ((c = lgetc(0)) == EOF)
734 return 0;
736 if (p + 1 >= buf + sizeof(buf) - 1) {
737 yyerror("string too long");
738 return findeol();
740 if (isalnum(c) || c == '_') {
741 *p++ = c;
742 continue;
744 *p = '\0';
745 lungetc(c);
746 break;
748 val = symget(buf);
749 if (val == NULL) {
750 yyerror("macro '%s' not defined", buf);
751 return findeol();
753 p = val + strlen(val) - 1;
754 lungetc(DONE_EXPAND);
755 while (p >= val) {
756 lungetc(*p);
757 p--;
759 lungetc(START_EXPAND);
760 goto top;
763 switch (c) {
764 case '\'':
765 case '"':
766 quotec = c;
767 while (1) {
768 if ((c = lgetc(quotec)) == EOF)
769 return 0;
770 if (c == '\n') {
771 file->lineno++;
772 continue;
773 } else if (c == '\\') {
774 if ((next = lgetc(quotec)) == EOF)
775 return (0);
776 if (next == quotec || next == ' ' ||
777 next == '\t')
778 c = next;
779 else if (next == '\n') {
780 file->lineno++;
781 continue;
782 } else
783 lungetc(next);
784 } else if (c == quotec) {
785 *p = '\0';
786 break;
787 } else if (c == '\0') {
788 yyerror("invalid syntax");
789 return findeol();
791 if (p + 1 >= buf + sizeof(buf) - 1) {
792 yyerror("string too long");
793 return findeol();
795 *p++ = c;
797 yylval.v.string = strdup(buf);
798 if (yylval.v.string == NULL)
799 err(1, "yylex: strdup");
800 return STRING;
803 #define allowed_to_end_number(x) \
804 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
806 if (c == '-' || isdigit(c)) {
807 do {
808 *p++ = c;
809 if ((size_t)(p-buf) >= sizeof(buf)) {
810 yyerror("string too long");
811 return findeol();
813 } while ((c = lgetc(0)) != EOF && isdigit(c));
814 lungetc(c);
815 if (p == buf + 1 && buf[0] == '-')
816 goto nodigits;
817 if (c == EOF || allowed_to_end_number(c)) {
818 const char *errstr = NULL;
820 *p = '\0';
821 yylval.v.number = strtonum(buf, LLONG_MIN,
822 LLONG_MAX, &errstr);
823 if (errstr) {
824 yyerror("\"%s\" invalid number: %s",
825 buf, errstr);
826 return findeol();
828 return NUM;
829 } else {
830 nodigits:
831 while (p > buf + 1)
832 lungetc(*--p);
833 c = *--p;
834 if (c == '-')
835 return c;
839 #define allowed_in_string(x) \
840 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
841 x != '{' && x != '}' && \
842 x != '!' && x != '=' && x != '#' && \
843 x != ',' && x != ';'))
845 if (isalnum(c) || c == ':' || c == '_') {
846 do {
847 *p++ = c;
848 if ((size_t)(p-buf) >= sizeof(buf)) {
849 yyerror("string too long");
850 return findeol();
852 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
853 lungetc(c);
854 *p = '\0';
855 if ((token = lookup(buf)) == STRING)
856 yylval.v.string = xstrdup(buf);
857 return token;
859 if (c == '\n') {
860 yylval.lineno = file->lineno;
861 file->lineno++;
863 if (c == EOF)
864 return 0;
865 return c;
868 struct file *
869 pushfile(const char *name, int secret)
871 struct file *nfile;
873 nfile = xcalloc(1, sizeof(*nfile));
874 nfile->name = xstrdup(name);
875 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
876 log_warn(NULL, "can't open %s: %s", nfile->name,
877 strerror(errno));
878 free(nfile->name);
879 free(nfile);
880 return NULL;
882 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
883 nfile->ungetsize = 16;
884 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
885 TAILQ_INSERT_TAIL(&files, nfile, entry);
886 return nfile;
889 int
890 popfile(void)
892 struct file *prev;
894 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
895 prev->errors += file->errors;
897 TAILQ_REMOVE(&files, file, entry);
898 fclose(file->stream);
899 free(file->name);
900 free(file->ungetbuf);
901 free(file);
902 file = prev;
903 return file ? 0 : EOF;
906 void
907 parse_conf(const char *filename)
909 struct sym *sym, *next;
911 file = pushfile(filename, 0);
912 if (file == NULL)
913 exit(1);
914 topfile = file;
916 yyparse();
917 errors = file->errors;
918 popfile();
920 /* Free macros and check which have not been used. */
921 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
922 /* TODO: warn if !sym->used */
923 if (!sym->persist) {
924 free(sym->name);
925 free(sym->val);
926 TAILQ_REMOVE(&symhead, sym, entry);
927 free(sym);
931 if (errors)
932 exit(1);
935 void
936 print_conf(void)
938 struct vhost *h;
939 /* struct location *l; */
940 /* struct envlist *e; */
941 /* struct alist *a; */
943 if (*conf.chroot != '\0')
944 printf("chroot \"%s\"\n", conf.chroot);
945 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
946 /* XXX: defined mimes? */
947 printf("port %d\n", conf.port);
948 printf("prefork %d\n", conf.prefork);
949 /* XXX: protocols? */
950 if (*conf.user != '\0')
951 printf("user \"%s\"\n", conf.user);
953 TAILQ_FOREACH(h, &hosts, vhosts) {
954 printf("\nserver \"%s\" {\n", h->domain);
955 printf(" cert \"%s\"\n", h->cert);
956 printf(" key \"%s\"\n", h->key);
957 /* TODO: print locations... */
958 printf("}\n");
962 int
963 symset(const char *name, const char *val, int persist)
965 struct sym *sym;
967 TAILQ_FOREACH(sym, &symhead, entry) {
968 if (!strcmp(name, sym->name))
969 break;
972 if (sym != NULL) {
973 if (sym->persist)
974 return 0;
975 else {
976 free(sym->name);
977 free(sym->val);
978 TAILQ_REMOVE(&symhead, sym, entry);
979 free(sym);
983 sym = xcalloc(1, sizeof(*sym));
984 sym->name = xstrdup(name);
985 sym->val = xstrdup(val);
986 sym->used = 0;
987 sym->persist = persist;
989 TAILQ_INSERT_TAIL(&symhead, sym, entry);
990 return 0;
993 int
994 cmdline_symset(char *s)
996 char *sym, *val;
997 int ret;
999 if ((val = strrchr(s, '=')) == NULL)
1000 return -1;
1001 sym = xcalloc(1, val - s + 1);
1002 memcpy(sym, s, val - s);
1003 ret = symset(sym, val + 1, 1);
1004 free(sym);
1005 return ret;
1008 char *
1009 symget(const char *nam)
1011 struct sym *sym;
1013 TAILQ_FOREACH(sym, &symhead, entry) {
1014 if (strcmp(nam, sym->name) == 0) {
1015 sym->used = 1;
1016 return sym->val;
1019 return NULL;
1022 struct vhost *
1023 new_vhost(void)
1025 struct vhost *h;
1027 h = xcalloc(1, sizeof(*h));
1028 TAILQ_INIT(&h->locations);
1029 TAILQ_INIT(&h->params);
1030 TAILQ_INIT(&h->aliases);
1031 TAILQ_INIT(&h->proxies);
1032 return h;
1035 struct location *
1036 new_location(void)
1038 struct location *l;
1040 l = xcalloc(1, sizeof(*l));
1041 l->dirfd = -1;
1042 l->fcgi = -1;
1043 return l;
1046 struct proxy *
1047 new_proxy(void)
1049 struct proxy *p;
1051 conf.can_open_sockets = 1;
1053 p = xcalloc(1, sizeof(*p));
1054 p->protocols = TLS_PROTOCOLS_DEFAULT;
1055 return p;
1058 char *
1059 ensure_absolute_path(char *path)
1061 if (path == NULL || *path != '/')
1062 yyerror("not an absolute path: %s", path);
1063 return path;
1066 int
1067 check_block_code(int n)
1069 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1070 yyerror("invalid block code %d", n);
1071 return n;
1074 char *
1075 check_block_fmt(char *fmt)
1077 char *s;
1079 for (s = fmt; *s; ++s) {
1080 if (*s != '%')
1081 continue;
1082 switch (*++s) {
1083 case '%':
1084 case 'p':
1085 case 'q':
1086 case 'P':
1087 case 'N':
1088 break;
1089 default:
1090 yyerror("invalid format specifier %%%c", *s);
1094 return fmt;
1097 int
1098 check_strip_no(int n)
1100 if (n <= 0)
1101 yyerror("invalid strip number %d", n);
1102 return n;
1105 int
1106 check_port_num(int n)
1108 if (n <= 0 || n >= UINT16_MAX)
1109 yyerror("port number is %s: %d",
1110 n <= 0 ? "too small" : "too large",
1111 n);
1112 return n;
1115 int
1116 check_prefork_num(int n)
1118 if (n <= 0 || n >= PROC_MAX)
1119 yyerror("invalid prefork number %d", n);
1120 return n;
1123 void
1124 advance_loc(void)
1126 loc = new_location();
1127 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1130 void
1131 advance_proxy(void)
1133 proxy = new_proxy();
1134 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1137 void
1138 parsehp(char *str, char **host, const char **port, const char *def)
1140 char *at;
1141 const char *errstr;
1143 *host = str;
1145 if ((at = strchr(str, ':')) != NULL) {
1146 *at++ = '\0';
1147 *port = at;
1148 } else
1149 *port = def;
1151 strtonum(*port, 1, UINT16_MAX, &errstr);
1152 if (errstr != NULL)
1153 yyerror("port is %s: %s", errstr, *port);
1156 void
1157 only_once(const void *ptr, const char *name)
1159 if (ptr != NULL)
1160 yyerror("`%s' specified more than once", name);
1163 void
1164 only_oncei(int i, const char *name)
1166 if (i != -1)
1167 yyerror("`%s' specified more than once", name);
1170 int
1171 fastcgi_conf(char *path, char *port, char *prog)
1173 struct fcgi *f;
1174 int i;
1176 conf.can_open_sockets = 1;
1178 for (i = 0; i < FCGI_MAX; ++i) {
1179 f = &fcgi[i];
1181 if (f->path == NULL) {
1182 f->id = i;
1183 f->path = path;
1184 f->port = port;
1185 return i;
1188 /* XXX: what to do with prog? */
1189 if (!strcmp(f->path, path) &&
1190 ((port == NULL && f->port == NULL) ||
1191 !strcmp(f->port, port))) {
1192 free(path);
1193 free(port);
1194 return i;
1198 yyerror("too much `fastcgi' rules defined.");
1199 return -1;
1202 void
1203 add_param(char *name, char *val)
1205 struct envlist *e;
1206 struct envhead *h = &host->params;
1208 e = xcalloc(1, sizeof(*e));
1209 e->name = name;
1210 e->value = val;
1211 TAILQ_INSERT_TAIL(h, e, envs);