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 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 {"include", INCLUDE},
484 {"index", INDEX},
485 {"ipv6", IPV6},
486 {"key", KEY},
487 {"lang", LANG},
488 {"location", LOCATION},
489 {"log", LOG},
490 {"map", MAP},
491 {"mime", MIME},
492 {"ocsp", OCSP},
493 {"off", OFF},
494 {"on", ON},
495 {"param", PARAM},
496 {"port", PORT},
497 {"prefork", PREFORK},
498 {"proto", PROTO},
499 {"protocols", PROTOCOLS},
500 {"proxy", PROXY},
501 {"relay-to", RELAY_TO},
502 {"require", REQUIRE},
503 {"return", RETURN},
504 {"root", ROOT},
505 {"server", SERVER},
506 {"sni", SNI},
507 {"spawn", SPAWN},
508 {"strip", STRIP},
509 {"tcp", TCP},
510 {"to-ext", TOEXT},
511 {"type", TYPE},
512 {"use-tls", USE_TLS},
513 {"user", USER},
514 {"verifyname", VERIFYNAME},
515 };
517 void
518 yyerror(const char *msg, ...)
520 va_list ap;
522 file->errors++;
524 va_start(ap, msg);
525 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
526 vfprintf(stderr, msg, ap);
527 fprintf(stderr, "\n");
528 va_end(ap);
531 void
532 yywarn(const char *msg, ...)
534 va_list ap;
536 va_start(ap, msg);
537 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
538 vfprintf(stderr, msg, ap);
539 fprintf(stderr, "\n");
540 va_end(ap);
543 int
544 kw_cmp(const void *k, const void *e)
546 return strcmp(k, ((struct keyword *)e)->word);
549 int
550 lookup(char *s)
552 const struct keyword *p;
554 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
555 sizeof(keywords[0]), kw_cmp);
557 if (p)
558 return p->token;
559 else
560 return STRING;
563 #define START_EXPAND 1
564 #define DONE_EXPAND 2
566 static int expanding;
568 int
569 igetc(void)
571 int c;
573 while (1) {
574 if (file->ungetpos > 0)
575 c = file->ungetbuf[--file->ungetpos];
576 else
577 c = getc(file->stream);
579 if (c == START_EXPAND)
580 expanding = 1;
581 else if (c == DONE_EXPAND)
582 expanding = 0;
583 else
584 break;
586 return c;
589 int
590 lgetc(int quotec)
592 int c, next;
594 if (quotec) {
595 if ((c = igetc()) == EOF) {
596 yyerror("reached end of file while parsing "
597 "quoted string");
598 if (file == topfile || popfile() == EOF)
599 return EOF;
600 return quotec;
602 return c;
605 while ((c = igetc()) == '\\') {
606 next = igetc();
607 if (next != '\n') {
608 c = next;
609 break;
611 yylval.lineno = file->lineno;
612 file->lineno++;
615 if (c == EOF) {
616 /*
617 * Fake EOL when hit EOF for the first time. This gets line
618 * count right if last line in included file is syntactically
619 * invalid and has no newline.
620 */
621 if (file->eof_reached == 0) {
622 file->eof_reached = 1;
623 return '\n';
625 while (c == EOF) {
626 if (file == topfile || popfile() == EOF)
627 return EOF;
628 c = igetc();
631 return c;
634 void
635 lungetc(int c)
637 if (c == EOF)
638 return;
640 if (file->ungetpos >= file->ungetsize) {
641 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
642 if (p == NULL)
643 err(1, "lungetc");
644 file->ungetbuf = p;
645 file->ungetsize *= 2;
647 file->ungetbuf[file->ungetpos++] = c;
650 int
651 findeol(void)
653 int c;
655 /* Skip to either EOF or the first real EOL. */
656 while (1) {
657 c = lgetc(0);
658 if (c == '\n') {
659 file->lineno++;
660 break;
662 if (c == EOF)
663 break;
665 return ERROR;
668 int
669 yylex(void)
671 char buf[8096];
672 char *p, *val;
673 int quotec, next, c;
674 int token;
676 top:
677 p = buf;
678 while ((c = lgetc(0)) == ' ' || c == '\t')
679 ; /* nothing */
681 yylval.lineno = file->lineno;
682 if (c == '#')
683 while ((c = lgetc(0)) != '\n' && c != EOF)
684 ; /* nothing */
685 if (c == '$' && !expanding) {
686 while (1) {
687 if ((c = lgetc(0)) == EOF)
688 return 0;
689 if (p + 1 >= buf + sizeof(buf) -1) {
690 yyerror("string too long");
691 return findeol();
693 if (isalnum(c) || c == '_') {
694 *p++ = c;
695 continue;
697 *p = '\0';
698 lungetc(c);
699 break;
701 val = symget(buf);
702 if (val == NULL) {
703 yyerror("macro `%s' not defined", buf);
704 return findeol();
706 yylval.v.string = xstrdup(val);
707 return STRING;
709 if (c == '@' && !expanding) {
710 while (1) {
711 if ((c = lgetc(0)) == EOF)
712 return 0;
714 if (p + 1 >= buf + sizeof(buf) - 1) {
715 yyerror("string too long");
716 return findeol();
718 if (isalnum(c) || c == '_') {
719 *p++ = c;
720 continue;
722 *p = '\0';
723 lungetc(c);
724 break;
726 val = symget(buf);
727 if (val == NULL) {
728 yyerror("macro '%s' not defined", buf);
729 return findeol();
731 p = val + strlen(val) - 1;
732 lungetc(DONE_EXPAND);
733 while (p >= val) {
734 lungetc(*p);
735 p--;
737 lungetc(START_EXPAND);
738 goto top;
741 switch (c) {
742 case '\'':
743 case '"':
744 quotec = c;
745 while (1) {
746 if ((c = lgetc(quotec)) == EOF)
747 return 0;
748 if (c == '\n') {
749 file->lineno++;
750 continue;
751 } else if (c == '\\') {
752 if ((next = lgetc(quotec)) == EOF)
753 return (0);
754 if (next == quotec || next == ' ' ||
755 next == '\t')
756 c = next;
757 else if (next == '\n') {
758 file->lineno++;
759 continue;
760 } else
761 lungetc(next);
762 } else if (c == quotec) {
763 *p = '\0';
764 break;
765 } else if (c == '\0') {
766 yyerror("invalid syntax");
767 return findeol();
769 if (p + 1 >= buf + sizeof(buf) - 1) {
770 yyerror("string too long");
771 return findeol();
773 *p++ = c;
775 yylval.v.string = strdup(buf);
776 if (yylval.v.string == NULL)
777 err(1, "yylex: strdup");
778 return STRING;
781 #define allowed_to_end_number(x) \
782 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
784 if (c == '-' || isdigit(c)) {
785 do {
786 *p++ = c;
787 if ((size_t)(p-buf) >= sizeof(buf)) {
788 yyerror("string too long");
789 return findeol();
791 } while ((c = lgetc(0)) != EOF && isdigit(c));
792 lungetc(c);
793 if (p == buf + 1 && buf[0] == '-')
794 goto nodigits;
795 if (c == EOF || allowed_to_end_number(c)) {
796 const char *errstr = NULL;
798 *p = '\0';
799 yylval.v.number = strtonum(buf, LLONG_MIN,
800 LLONG_MAX, &errstr);
801 if (errstr) {
802 yyerror("\"%s\" invalid number: %s",
803 buf, errstr);
804 return findeol();
806 return NUM;
807 } else {
808 nodigits:
809 while (p > buf + 1)
810 lungetc(*--p);
811 c = *--p;
812 if (c == '-')
813 return c;
817 #define allowed_in_string(x) \
818 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
819 x != '{' && x != '}' && \
820 x != '!' && x != '=' && x != '#' && \
821 x != ',' && x != ';'))
823 if (isalnum(c) || c == ':' || 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 && (allowed_in_string(c)));
831 lungetc(c);
832 *p = '\0';
833 if ((token = lookup(buf)) == STRING)
834 yylval.v.string = xstrdup(buf);
835 return token;
837 if (c == '\n') {
838 yylval.lineno = file->lineno;
839 file->lineno++;
841 if (c == EOF)
842 return 0;
843 return c;
846 struct file *
847 pushfile(const char *name, int secret)
849 struct file *nfile;
851 nfile = xcalloc(1, sizeof(*nfile));
852 nfile->name = xstrdup(name);
853 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
854 log_warn(NULL, "can't open %s: %s", nfile->name,
855 strerror(errno));
856 free(nfile->name);
857 free(nfile);
858 return NULL;
860 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
861 nfile->ungetsize = 16;
862 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
863 TAILQ_INSERT_TAIL(&files, nfile, entry);
864 return nfile;
867 int
868 popfile(void)
870 struct file *prev;
872 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
873 prev->errors += file->errors;
875 TAILQ_REMOVE(&files, file, entry);
876 fclose(file->stream);
877 free(file->name);
878 free(file->ungetbuf);
879 free(file);
880 file = prev;
881 return file ? 0 : EOF;
884 void
885 parse_conf(const char *filename)
887 struct sym *sym, *next;
889 file = pushfile(filename, 0);
890 if (file == NULL)
891 exit(1);
892 topfile = file;
894 yyparse();
895 errors = file->errors;
896 popfile();
898 /* Free macros and check which have not been used. */
899 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
900 /* TODO: warn if !sym->used */
901 if (!sym->persist) {
902 free(sym->name);
903 free(sym->val);
904 TAILQ_REMOVE(&symhead, sym, entry);
905 free(sym);
909 if (errors)
910 exit(1);
913 void
914 print_conf(void)
916 struct vhost *h;
917 /* struct location *l; */
918 /* struct envlist *e; */
919 /* struct alist *a; */
921 if (conf.chroot != NULL)
922 printf("chroot \"%s\"\n", conf.chroot);
923 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
924 /* XXX: defined mimes? */
925 printf("port %d\n", conf.port);
926 printf("prefork %d\n", conf.prefork);
927 /* XXX: protocols? */
928 if (conf.user != NULL)
929 printf("user \"%s\"\n", conf.user);
931 TAILQ_FOREACH(h, &hosts, vhosts) {
932 printf("\nserver \"%s\" {\n", h->domain);
933 printf(" cert \"%s\"\n", h->cert);
934 printf(" key \"%s\"\n", h->key);
935 /* TODO: print locations... */
936 printf("}\n");
940 int
941 symset(const char *name, const char *val, int persist)
943 struct sym *sym;
945 TAILQ_FOREACH(sym, &symhead, entry) {
946 if (!strcmp(name, sym->name))
947 break;
950 if (sym != NULL) {
951 if (sym->persist)
952 return 0;
953 else {
954 free(sym->name);
955 free(sym->val);
956 TAILQ_REMOVE(&symhead, sym, entry);
957 free(sym);
961 sym = xcalloc(1, sizeof(*sym));
962 sym->name = xstrdup(name);
963 sym->val = xstrdup(val);
964 sym->used = 0;
965 sym->persist = persist;
967 TAILQ_INSERT_TAIL(&symhead, sym, entry);
968 return 0;
971 int
972 cmdline_symset(char *s)
974 char *sym, *val;
975 int ret;
977 if ((val = strrchr(s, '=')) == NULL)
978 return -1;
979 sym = xcalloc(1, val - s + 1);
980 memcpy(sym, s, val - s);
981 ret = symset(sym, val + 1, 1);
982 free(sym);
983 return ret;
986 char *
987 symget(const char *nam)
989 struct sym *sym;
991 TAILQ_FOREACH(sym, &symhead, entry) {
992 if (strcmp(nam, sym->name) == 0) {
993 sym->used = 1;
994 return sym->val;
997 return NULL;
1000 struct vhost *
1001 new_vhost(void)
1003 return xcalloc(1, sizeof(struct vhost));
1006 struct location *
1007 new_location(void)
1009 struct location *l;
1011 l = xcalloc(1, sizeof(*l));
1012 l->dirfd = -1;
1013 l->fcgi = -1;
1014 return l;
1017 struct proxy *
1018 new_proxy(void)
1020 struct proxy *p;
1022 p = xcalloc(1, sizeof(*p));
1023 p->protocols = TLS_PROTOCOLS_DEFAULT;
1024 return p;
1027 char *
1028 ensure_absolute_path(char *path)
1030 if (path == NULL || *path != '/')
1031 yyerror("not an absolute path: %s", path);
1032 return path;
1035 int
1036 check_block_code(int n)
1038 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1039 yyerror("invalid block code %d", n);
1040 return n;
1043 char *
1044 check_block_fmt(char *fmt)
1046 char *s;
1048 for (s = fmt; *s; ++s) {
1049 if (*s != '%')
1050 continue;
1051 switch (*++s) {
1052 case '%':
1053 case 'p':
1054 case 'q':
1055 case 'P':
1056 case 'N':
1057 break;
1058 default:
1059 yyerror("invalid format specifier %%%c", *s);
1063 return fmt;
1066 int
1067 check_strip_no(int n)
1069 if (n <= 0)
1070 yyerror("invalid strip number %d", n);
1071 return n;
1074 int
1075 check_port_num(int n)
1077 if (n <= 0 || n >= UINT16_MAX)
1078 yyerror("port number is %s: %d",
1079 n <= 0 ? "too small" : "too large",
1080 n);
1081 return n;
1084 int
1085 check_prefork_num(int n)
1087 if (n <= 0 || n >= PROC_MAX)
1088 yyerror("invalid prefork number %d", n);
1089 return n;
1092 void
1093 advance_loc(void)
1095 loc = new_location();
1096 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1099 void
1100 advance_proxy(void)
1102 proxy = new_proxy();
1103 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1106 void
1107 parsehp(char *str, char **host, const char **port, const char *def)
1109 char *at;
1110 const char *errstr;
1112 *host = str;
1114 if ((at = strchr(str, ':')) != NULL) {
1115 *at++ = '\0';
1116 *port = at;
1117 } else
1118 *port = def;
1120 strtonum(*port, 1, UINT16_MAX, &errstr);
1121 if (errstr != NULL)
1122 yyerror("port is %s: %s", errstr, *port);
1125 void
1126 only_once(const void *ptr, const char *name)
1128 if (ptr != NULL)
1129 yyerror("`%s' specified more than once", name);
1132 void
1133 only_oncei(int i, const char *name)
1135 if (i != -1)
1136 yyerror("`%s' specified more than once", name);
1139 int
1140 fastcgi_conf(char *path, char *port, char *prog)
1142 struct fcgi *f;
1143 int i;
1145 for (i = 0; i < FCGI_MAX; ++i) {
1146 f = &fcgi[i];
1148 if (f->path == NULL) {
1149 f->id = i;
1150 f->path = path;
1151 f->port = port;
1152 f->prog = prog;
1153 return i;
1156 /* XXX: what to do with prog? */
1157 if (!strcmp(f->path, path) &&
1158 ((port == NULL && f->port == NULL) ||
1159 !strcmp(f->port, port))) {
1160 free(path);
1161 free(port);
1162 return i;
1166 yyerror("too much `fastcgi' rules defined.");
1167 return -1;
1170 void
1171 add_param(char *name, char *val, int env)
1173 struct envlist *e;
1174 struct envhead *h;
1176 if (env)
1177 h = &host->env;
1178 else
1179 h = &host->params;
1181 e = xcalloc(1, sizeof(*e));
1182 e->name = name;
1183 e->value = val;
1184 if (TAILQ_EMPTY(h))
1185 TAILQ_INSERT_HEAD(h, e, envs);
1186 else
1187 TAILQ_INSERT_TAIL(h, e, envs);