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 <netdb.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
37 static struct file {
38 TAILQ_ENTRY(file) entry;
39 FILE *stream;
40 char *name;
41 size_t ungetpos;
42 size_t ungetsize;
43 u_char *ungetbuf;
44 int eof_reached;
45 int lineno;
46 int errors;
47 } *file, *topfile;
49 struct file *pushfile(const char *, int);
50 int popfile(void);
51 int yyparse(void);
52 int yylex(void);
53 void yyerror(const char *, ...)
54 __attribute__((__format__ (printf, 1, 2)))
55 __attribute__((__nonnull__ (1)));
56 void yywarn(const char *, ...)
57 __attribute__((__format__ (printf, 1, 2)))
58 __attribute__((__nonnull__ (1)));
59 int kw_cmp(const void *, const void *);
60 int lookup(char *);
61 int igetc(void);
62 int lgetc(int);
63 void lungetc(int);
64 int findeol(void);
66 /*
67 * #define YYDEBUG 1
68 * int yydebug = 1;
69 */
71 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
72 struct sym {
73 TAILQ_ENTRY(sym) entry;
74 int used;
75 int persist;
76 char *name;
77 char *val;
78 };
80 int symset(const char *, const char *, int);
81 char *symget(const char *);
83 struct vhost *new_vhost(void);
84 struct location *new_location(void);
85 struct proxy *new_proxy(void);
86 char *ensure_absolute_path(char*);
87 int check_block_code(int);
88 char *check_block_fmt(char*);
89 int check_strip_no(int);
90 int check_port_num(int);
91 int check_prefork_num(int);
92 void advance_loc(void);
93 void advance_proxy(void);
94 void parsehp(char *, char **, const char **, const char *);
95 int fastcgi_conf(const char *, const char *);
96 void add_param(char *, char *);
97 int getservice(const 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 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 proxy_port
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 (void) strlcpy(loc->match, "*", sizeof(loc->match));
246 (void) strlcpy(host->domain, $2, sizeof(host->domain));
248 if (strstr($2, "xn--") != NULL) {
249 yywarn("\"%s\" looks like punycode: you "
250 "should use the decoded hostname", $2);
253 free($2);
254 } '{' optnl servbody '}' {
255 if (*host->cert == '\0' || *host->key == '\0')
256 yyerror("invalid vhost definition: %s", $2);
258 | error '}' { yyerror("bad server directive"); }
261 servbody : /* empty */
262 | servbody servopt optnl
263 | servbody location optnl
264 | servbody proxy optnl
267 servopt : ALIAS string {
268 struct alist *a;
270 a = xcalloc(1, sizeof(*a));
271 (void) strlcpy(a->alias, $2, sizeof(a->alias));
272 free($2);
273 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
275 | CERT string {
276 ensure_absolute_path($2);
277 (void) strlcpy(host->cert, $2, sizeof(host->cert));
278 free($2);
280 | KEY string {
281 ensure_absolute_path($2);
282 (void) strlcpy(host->key, $2, sizeof(host->key));
283 free($2);
285 | OCSP string {
286 ensure_absolute_path($2);
287 (void) strlcpy(host->ocsp, $2, sizeof(host->ocsp));
288 free($2);
290 | PARAM string '=' string {
291 add_param($2, $4);
293 | locopt
296 proxy : PROXY { advance_proxy(); }
297 proxy_matches '{' optnl proxy_opts '}' {
298 if (*proxy->host == '\0')
299 yyerror("invalid proxy block: missing `relay-to' option");
301 if ((proxy->cert == NULL && proxy->key != NULL) ||
302 (proxy->cert != NULL && proxy->key == NULL))
303 yyerror("invalid proxy block: missing cert or key");
307 proxy_matches : /* empty */
308 | proxy_matches proxy_match
311 proxy_port : /* empty */ { $$ = 1965; }
312 | PORT STRING {
313 if (($$ = getservice($2)) == -1)
314 yyerror("invalid port number %s", $2);
315 free($2);
317 | PORT NUM { $$ = $2; }
320 proxy_match : PROTO string {
321 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
322 free($2);
324 | FOR_HOST string proxy_port {
325 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
326 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
327 "%d", $3);
328 free($2);
332 proxy_opts : /* empty */
333 | proxy_opts proxy_opt optnl
336 proxy_opt : CERT string {
337 tls_unload_file(proxy->cert, proxy->certlen);
338 ensure_absolute_path($2);
339 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
340 if (proxy->cert == NULL)
341 yyerror("can't load cert %s", $2);
342 free($2);
344 | KEY string {
345 tls_unload_file(proxy->key, proxy->keylen);
346 ensure_absolute_path($2);
347 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
348 if (proxy->key == NULL)
349 yyerror("can't load key %s", $2);
350 free($2);
352 | PROTOCOLS string {
353 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
354 yyerror("invalid protocols string \"%s\"", $2);
355 free($2);
357 | RELAY_TO string proxy_port {
358 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
359 (void) snprintf(proxy->port, sizeof(proxy->port),
360 "%d", $3);
361 free($2);
363 | REQUIRE CLIENT CA string {
364 ensure_absolute_path($4);
365 if ((proxy->reqca = load_ca($4)) == NULL)
366 yyerror("couldn't load ca cert: %s", $4);
367 free($4);
369 | SNI string {
370 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
371 free($2);
373 | USE_TLS bool {
374 proxy->notls = !$2;
376 | VERIFYNAME bool {
377 proxy->noverifyname = !$2;
381 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
382 /* drop the starting '/' if any */
383 if (*$3 == '/')
384 memmove($3, $3+1, strlen($3));
385 (void) strlcpy(loc->match, $3, sizeof(loc->match));
386 free($3);
388 | error '}'
391 locopts : /* empty */
392 | locopts locopt optnl
395 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
396 | BLOCK RETURN NUM string {
397 check_block_fmt($4);
398 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
399 loc->block_code = check_block_code($3);
400 free($4);
402 | BLOCK RETURN NUM {
403 (void) strlcpy(loc->block_fmt, "temporary failure",
404 sizeof(loc->block_fmt));
405 loc->block_code = check_block_code($3);
406 if ($3 >= 30 && $3 < 40)
407 yyerror("missing `meta' for block return %d", $3);
409 | BLOCK {
410 (void) strlcpy(loc->block_fmt, "temporary failure",
411 sizeof(loc->block_fmt));
412 loc->block_code = 40;
414 | DEFAULT TYPE string {
415 (void) strlcpy(loc->default_mime, $3,
416 sizeof(loc->default_mime));
417 free($3);
419 | FASTCGI fastcgi
420 | INDEX string {
421 (void) strlcpy(loc->index, $2, sizeof(loc->index));
422 free($2);
424 | LANG string {
425 (void) strlcpy(loc->lang, $2,
426 sizeof(loc->lang));
427 free($2);
429 | LOG bool { loc->disable_log = !$2; }
430 | REQUIRE CLIENT CA string {
431 ensure_absolute_path($4);
432 if ((loc->reqca = load_ca($4)) == NULL)
433 yyerror("couldn't load ca cert: %s", $4);
434 free($4);
436 | ROOT string {
437 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
438 free($2);
440 | STRIP NUM { loc->strip = check_strip_no($2); }
443 fastcgi : string {
444 loc->fcgi = fastcgi_conf($1, NULL);
445 free($1);
447 | TCP string PORT NUM {
448 char *c;
449 if (asprintf(&c, "%d", $4) == -1)
450 err(1, "asprintf");
451 loc->fcgi = fastcgi_conf($2, c);
452 free($2);
454 | TCP string {
455 loc->fcgi = fastcgi_conf($2, "9000");
456 free($2);
458 | TCP string PORT string {
459 loc->fcgi = fastcgi_conf($2, $4);
460 free($2);
461 free($4);
465 types : TYPES '{' optnl mediaopts_l '}' ;
467 mediaopts_l : mediaopts_l mediaoptsl nl
468 | mediaoptsl nl
471 mediaoptsl : STRING {
472 free(current_media);
473 current_media = $1;
474 } medianames_l optsemicolon
475 | include
478 medianames_l : medianames_l medianamesl
479 | medianamesl
482 medianamesl : numberstring {
483 if (add_mime(&conf.mime, current_media, $1) == -1)
484 err(1, "add_mime");
485 free($1);
489 nl : '\n' optnl
492 optnl : '\n' optnl /* zero or more newlines */
493 | ';' optnl /* semicolons too */
494 | /*empty*/
497 optsemicolon : ';'
501 %%
503 static const struct keyword {
504 const char *word;
505 int token;
506 } keywords[] = {
507 /* these MUST be sorted */
508 {"alias", ALIAS},
509 {"auto", AUTO},
510 {"block", BLOCK},
511 {"ca", CA},
512 {"cert", CERT},
513 {"chroot", CHROOT},
514 {"client", CLIENT},
515 {"default", DEFAULT},
516 {"fastcgi", FASTCGI},
517 {"for-host", FOR_HOST},
518 {"include", INCLUDE},
519 {"index", INDEX},
520 {"ipv6", IPV6},
521 {"key", KEY},
522 {"lang", LANG},
523 {"location", LOCATION},
524 {"log", LOG},
525 {"ocsp", OCSP},
526 {"off", OFF},
527 {"on", ON},
528 {"param", PARAM},
529 {"port", PORT},
530 {"prefork", PREFORK},
531 {"proto", PROTO},
532 {"protocols", PROTOCOLS},
533 {"proxy", PROXY},
534 {"relay-to", RELAY_TO},
535 {"require", REQUIRE},
536 {"return", RETURN},
537 {"root", ROOT},
538 {"server", SERVER},
539 {"sni", SNI},
540 {"strip", STRIP},
541 {"tcp", TCP},
542 {"to-ext", TOEXT},
543 {"type", TYPE},
544 {"types", TYPES},
545 {"use-tls", USE_TLS},
546 {"user", USER},
547 {"verifyname", VERIFYNAME},
548 };
550 void
551 yyerror(const char *msg, ...)
553 va_list ap;
555 file->errors++;
557 va_start(ap, msg);
558 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
559 vfprintf(stderr, msg, ap);
560 fprintf(stderr, "\n");
561 va_end(ap);
564 void
565 yywarn(const char *msg, ...)
567 va_list ap;
569 va_start(ap, msg);
570 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
571 vfprintf(stderr, msg, ap);
572 fprintf(stderr, "\n");
573 va_end(ap);
576 int
577 kw_cmp(const void *k, const void *e)
579 return strcmp(k, ((struct keyword *)e)->word);
582 int
583 lookup(char *s)
585 const struct keyword *p;
587 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
588 sizeof(keywords[0]), kw_cmp);
590 if (p)
591 return p->token;
592 else
593 return STRING;
596 #define START_EXPAND 1
597 #define DONE_EXPAND 2
599 static int expanding;
601 int
602 igetc(void)
604 int c;
606 while (1) {
607 if (file->ungetpos > 0)
608 c = file->ungetbuf[--file->ungetpos];
609 else
610 c = getc(file->stream);
612 if (c == START_EXPAND)
613 expanding = 1;
614 else if (c == DONE_EXPAND)
615 expanding = 0;
616 else
617 break;
619 return c;
622 int
623 lgetc(int quotec)
625 int c, next;
627 if (quotec) {
628 if ((c = igetc()) == EOF) {
629 yyerror("reached end of file while parsing "
630 "quoted string");
631 if (file == topfile || popfile() == EOF)
632 return EOF;
633 return quotec;
635 return c;
638 while ((c = igetc()) == '\\') {
639 next = igetc();
640 if (next != '\n') {
641 c = next;
642 break;
644 yylval.lineno = file->lineno;
645 file->lineno++;
648 if (c == EOF) {
649 /*
650 * Fake EOL when hit EOF for the first time. This gets line
651 * count right if last line in included file is syntactically
652 * invalid and has no newline.
653 */
654 if (file->eof_reached == 0) {
655 file->eof_reached = 1;
656 return '\n';
658 while (c == EOF) {
659 if (file == topfile || popfile() == EOF)
660 return EOF;
661 c = igetc();
664 return c;
667 void
668 lungetc(int c)
670 if (c == EOF)
671 return;
673 if (file->ungetpos >= file->ungetsize) {
674 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
675 if (p == NULL)
676 err(1, "lungetc");
677 file->ungetbuf = p;
678 file->ungetsize *= 2;
680 file->ungetbuf[file->ungetpos++] = c;
683 int
684 findeol(void)
686 int c;
688 /* Skip to either EOF or the first real EOL. */
689 while (1) {
690 c = lgetc(0);
691 if (c == '\n') {
692 file->lineno++;
693 break;
695 if (c == EOF)
696 break;
698 return ERROR;
701 int
702 yylex(void)
704 char buf[8096];
705 char *p, *val;
706 int quotec, next, c;
707 int token;
709 top:
710 p = buf;
711 while ((c = lgetc(0)) == ' ' || c == '\t')
712 ; /* nothing */
714 yylval.lineno = file->lineno;
715 if (c == '#')
716 while ((c = lgetc(0)) != '\n' && c != EOF)
717 ; /* nothing */
718 if (c == '$' && !expanding) {
719 while (1) {
720 if ((c = lgetc(0)) == EOF)
721 return 0;
722 if (p + 1 >= buf + sizeof(buf) -1) {
723 yyerror("string too long");
724 return findeol();
726 if (isalnum(c) || c == '_') {
727 *p++ = c;
728 continue;
730 *p = '\0';
731 lungetc(c);
732 break;
734 val = symget(buf);
735 if (val == NULL) {
736 yyerror("macro `%s' not defined", buf);
737 return findeol();
739 yylval.v.string = xstrdup(val);
740 return STRING;
742 if (c == '@' && !expanding) {
743 while (1) {
744 if ((c = lgetc(0)) == EOF)
745 return 0;
747 if (p + 1 >= buf + sizeof(buf) - 1) {
748 yyerror("string too long");
749 return findeol();
751 if (isalnum(c) || c == '_') {
752 *p++ = c;
753 continue;
755 *p = '\0';
756 lungetc(c);
757 break;
759 val = symget(buf);
760 if (val == NULL) {
761 yyerror("macro '%s' not defined", buf);
762 return findeol();
764 p = val + strlen(val) - 1;
765 lungetc(DONE_EXPAND);
766 while (p >= val) {
767 lungetc(*p);
768 p--;
770 lungetc(START_EXPAND);
771 goto top;
774 switch (c) {
775 case '\'':
776 case '"':
777 quotec = c;
778 while (1) {
779 if ((c = lgetc(quotec)) == EOF)
780 return 0;
781 if (c == '\n') {
782 file->lineno++;
783 continue;
784 } else if (c == '\\') {
785 if ((next = lgetc(quotec)) == EOF)
786 return (0);
787 if (next == quotec || next == ' ' ||
788 next == '\t')
789 c = next;
790 else if (next == '\n') {
791 file->lineno++;
792 continue;
793 } else
794 lungetc(next);
795 } else if (c == quotec) {
796 *p = '\0';
797 break;
798 } else if (c == '\0') {
799 yyerror("invalid syntax");
800 return findeol();
802 if (p + 1 >= buf + sizeof(buf) - 1) {
803 yyerror("string too long");
804 return findeol();
806 *p++ = c;
808 yylval.v.string = strdup(buf);
809 if (yylval.v.string == NULL)
810 err(1, "yylex: strdup");
811 return STRING;
814 #define allowed_to_end_number(x) \
815 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
817 if (c == '-' || isdigit(c)) {
818 do {
819 *p++ = c;
820 if ((size_t)(p-buf) >= sizeof(buf)) {
821 yyerror("string too long");
822 return findeol();
824 } while ((c = lgetc(0)) != EOF && isdigit(c));
825 lungetc(c);
826 if (p == buf + 1 && buf[0] == '-')
827 goto nodigits;
828 if (c == EOF || allowed_to_end_number(c)) {
829 const char *errstr = NULL;
831 *p = '\0';
832 yylval.v.number = strtonum(buf, LLONG_MIN,
833 LLONG_MAX, &errstr);
834 if (errstr) {
835 yyerror("\"%s\" invalid number: %s",
836 buf, errstr);
837 return findeol();
839 return NUM;
840 } else {
841 nodigits:
842 while (p > buf + 1)
843 lungetc(*--p);
844 c = *--p;
845 if (c == '-')
846 return c;
850 #define allowed_in_string(x) \
851 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
852 x != '{' && x != '}' && \
853 x != '!' && x != '=' && x != '#' && \
854 x != ',' && x != ';'))
856 if (isalnum(c) || c == ':' || c == '_') {
857 do {
858 *p++ = c;
859 if ((size_t)(p-buf) >= sizeof(buf)) {
860 yyerror("string too long");
861 return findeol();
863 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
864 lungetc(c);
865 *p = '\0';
866 if ((token = lookup(buf)) == STRING)
867 yylval.v.string = xstrdup(buf);
868 return token;
870 if (c == '\n') {
871 yylval.lineno = file->lineno;
872 file->lineno++;
874 if (c == EOF)
875 return 0;
876 return c;
879 struct file *
880 pushfile(const char *name, int secret)
882 struct file *nfile;
884 nfile = xcalloc(1, sizeof(*nfile));
885 nfile->name = xstrdup(name);
886 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
887 log_warn(NULL, "can't open %s: %s", nfile->name,
888 strerror(errno));
889 free(nfile->name);
890 free(nfile);
891 return NULL;
893 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
894 nfile->ungetsize = 16;
895 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
896 TAILQ_INSERT_TAIL(&files, nfile, entry);
897 return nfile;
900 int
901 popfile(void)
903 struct file *prev;
905 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
906 prev->errors += file->errors;
908 TAILQ_REMOVE(&files, file, entry);
909 fclose(file->stream);
910 free(file->name);
911 free(file->ungetbuf);
912 free(file);
913 file = prev;
914 return file ? 0 : EOF;
917 void
918 parse_conf(const char *filename)
920 struct sym *sym, *next;
922 file = pushfile(filename, 0);
923 if (file == NULL)
924 exit(1);
925 topfile = file;
927 yyparse();
928 errors = file->errors;
929 popfile();
931 /* Free macros and check which have not been used. */
932 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
933 /* TODO: warn if !sym->used */
934 if (!sym->persist) {
935 free(sym->name);
936 free(sym->val);
937 TAILQ_REMOVE(&symhead, sym, entry);
938 free(sym);
942 if (errors)
943 exit(1);
946 void
947 print_conf(void)
949 struct vhost *h;
950 /* struct location *l; */
951 /* struct envlist *e; */
952 /* struct alist *a; */
954 if (*conf.chroot != '\0')
955 printf("chroot \"%s\"\n", conf.chroot);
956 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
957 /* XXX: defined mimes? */
958 printf("port %d\n", conf.port);
959 printf("prefork %d\n", conf.prefork);
960 /* XXX: protocols? */
961 if (*conf.user != '\0')
962 printf("user \"%s\"\n", conf.user);
964 TAILQ_FOREACH(h, &hosts, vhosts) {
965 printf("\nserver \"%s\" {\n", h->domain);
966 printf(" cert \"%s\"\n", h->cert);
967 printf(" key \"%s\"\n", h->key);
968 /* TODO: print locations... */
969 printf("}\n");
973 int
974 symset(const char *name, const char *val, int persist)
976 struct sym *sym;
978 TAILQ_FOREACH(sym, &symhead, entry) {
979 if (!strcmp(name, sym->name))
980 break;
983 if (sym != NULL) {
984 if (sym->persist)
985 return 0;
986 else {
987 free(sym->name);
988 free(sym->val);
989 TAILQ_REMOVE(&symhead, sym, entry);
990 free(sym);
994 sym = xcalloc(1, sizeof(*sym));
995 sym->name = xstrdup(name);
996 sym->val = xstrdup(val);
997 sym->used = 0;
998 sym->persist = persist;
1000 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1001 return 0;
1004 int
1005 cmdline_symset(char *s)
1007 char *sym, *val;
1008 int ret;
1010 if ((val = strrchr(s, '=')) == NULL)
1011 return -1;
1012 sym = xcalloc(1, val - s + 1);
1013 memcpy(sym, s, val - s);
1014 ret = symset(sym, val + 1, 1);
1015 free(sym);
1016 return ret;
1019 char *
1020 symget(const char *nam)
1022 struct sym *sym;
1024 TAILQ_FOREACH(sym, &symhead, entry) {
1025 if (strcmp(nam, sym->name) == 0) {
1026 sym->used = 1;
1027 return sym->val;
1030 return NULL;
1033 struct vhost *
1034 new_vhost(void)
1036 struct vhost *h;
1038 h = xcalloc(1, sizeof(*h));
1039 TAILQ_INIT(&h->locations);
1040 TAILQ_INIT(&h->params);
1041 TAILQ_INIT(&h->aliases);
1042 TAILQ_INIT(&h->proxies);
1043 return h;
1046 struct location *
1047 new_location(void)
1049 struct location *l;
1051 l = xcalloc(1, sizeof(*l));
1052 l->dirfd = -1;
1053 l->fcgi = -1;
1054 return l;
1057 struct proxy *
1058 new_proxy(void)
1060 struct proxy *p;
1062 conf.can_open_sockets = 1;
1064 p = xcalloc(1, sizeof(*p));
1065 p->protocols = TLS_PROTOCOLS_DEFAULT;
1066 return p;
1069 char *
1070 ensure_absolute_path(char *path)
1072 if (path == NULL || *path != '/')
1073 yyerror("not an absolute path: %s", path);
1074 return path;
1077 int
1078 check_block_code(int n)
1080 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1081 yyerror("invalid block code %d", n);
1082 return n;
1085 char *
1086 check_block_fmt(char *fmt)
1088 char *s;
1090 for (s = fmt; *s; ++s) {
1091 if (*s != '%')
1092 continue;
1093 switch (*++s) {
1094 case '%':
1095 case 'p':
1096 case 'q':
1097 case 'P':
1098 case 'N':
1099 break;
1100 default:
1101 yyerror("invalid format specifier %%%c", *s);
1105 return fmt;
1108 int
1109 check_strip_no(int n)
1111 if (n <= 0)
1112 yyerror("invalid strip number %d", n);
1113 return n;
1116 int
1117 check_port_num(int n)
1119 if (n <= 0 || n >= UINT16_MAX)
1120 yyerror("port number is %s: %d",
1121 n <= 0 ? "too small" : "too large",
1122 n);
1123 return n;
1126 int
1127 check_prefork_num(int n)
1129 if (n <= 0 || n >= PROC_MAX)
1130 yyerror("invalid prefork number %d", n);
1131 return n;
1134 void
1135 advance_loc(void)
1137 loc = new_location();
1138 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1141 void
1142 advance_proxy(void)
1144 proxy = new_proxy();
1145 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1148 void
1149 parsehp(char *str, char **host, const char **port, const char *def)
1151 char *at;
1152 const char *errstr;
1154 *host = str;
1156 if ((at = strchr(str, ':')) != NULL) {
1157 *at++ = '\0';
1158 *port = at;
1159 } else
1160 *port = def;
1162 strtonum(*port, 1, UINT16_MAX, &errstr);
1163 if (errstr != NULL)
1164 yyerror("port is %s: %s", errstr, *port);
1167 int
1168 fastcgi_conf(const char *path, const char *port)
1170 struct fcgi *f;
1171 int i;
1173 conf.can_open_sockets = 1;
1175 for (i = 0; i < FCGI_MAX; ++i) {
1176 f = &fcgi[i];
1178 if (*f->path == '\0') {
1179 f->id = i;
1180 (void) strlcpy(f->path, path, sizeof(f->path));
1181 (void) strlcpy(f->port, port, sizeof(f->port));
1182 return i;
1185 if (!strcmp(f->path, path) &&
1186 ((port == NULL && *f->port == '\0') ||
1187 !strcmp(f->port, port)))
1188 return i;
1191 yyerror("too much `fastcgi' rules defined.");
1192 return -1;
1195 void
1196 add_param(char *name, char *val)
1198 struct envlist *e;
1199 struct envhead *h = &host->params;
1201 e = xcalloc(1, sizeof(*e));
1202 (void) strlcpy(e->name, name, sizeof(e->name));
1203 (void) strlcpy(e->value, val, sizeof(e->value));
1204 TAILQ_INSERT_TAIL(h, e, envs);
1207 int
1208 getservice(const char *n)
1210 struct servent *s;
1211 const char *errstr;
1212 long long llval;
1214 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1215 if (errstr) {
1216 s = getservbyname(n, "tcp");
1217 if (s == NULL)
1218 s = getservbyname(n, "udp");
1219 if (s == NULL)
1220 return (-1);
1221 return (ntohs(s->s_port));
1224 return ((unsigned short)llval);