Blob


1 %{
3 /*
4 * Copyright (c) 2021, 2022, 2023 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 #include "log.h"
38 struct conf *conf;
40 static const char *default_host = "*";
41 static uint16_t default_port = 1965;
43 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
44 static struct file {
45 TAILQ_ENTRY(file) entry;
46 FILE *stream;
47 char *name;
48 size_t ungetpos;
49 size_t ungetsize;
50 u_char *ungetbuf;
51 int eof_reached;
52 int lineno;
53 int errors;
54 } *file, *topfile;
56 struct file *pushfile(const char *, int);
57 int popfile(void);
58 int yyparse(void);
59 int yylex(void);
60 void yyerror(const char *, ...)
61 __attribute__((__format__ (printf, 1, 2)))
62 __attribute__((__nonnull__ (1)));
63 void yywarn(const char *, ...)
64 __attribute__((__format__ (printf, 1, 2)))
65 __attribute__((__nonnull__ (1)));
66 int kw_cmp(const void *, const void *);
67 int lookup(char *);
68 int igetc(void);
69 int lgetc(int);
70 void lungetc(int);
71 int findeol(void);
73 /*
74 * #define YYDEBUG 1
75 * int yydebug = 1;
76 */
78 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
79 struct sym {
80 TAILQ_ENTRY(sym) entry;
81 int used;
82 int persist;
83 char *name;
84 char *val;
85 };
87 int symset(const char *, const char *, int);
88 char *symget(const char *);
90 char *ensure_absolute_path(char*);
91 int check_block_code(int);
92 char *check_block_fmt(char*);
93 int check_strip_no(int);
94 int check_port_num(int);
95 int check_prefork_num(int);
96 void advance_loc(void);
97 void advance_proxy(void);
98 void parsehp(char *, char **, const char **, const char *);
99 int fastcgi_conf(const char *, const char *);
100 void add_param(char *, char *);
101 int getservice(const char *);
102 void listen_on(const char *, const char *);
104 static struct vhost *host;
105 static struct location *loc;
106 static struct proxy *proxy;
107 static char *current_media;
108 static int errors;
110 typedef struct {
111 union {
112 char *string;
113 int number;
114 } v;
115 int lineno;
116 } YYSTYPE;
118 #define YYSTYPE YYSTYPE
120 %}
122 /* for bison: */
123 /* %define parse.error verbose */
125 %token ALIAS AUTO
126 %token BLOCK
127 %token CA CERT CHROOT CLIENT
128 %token DEFAULT
129 %token FASTCGI FOR_HOST
130 %token INCLUDE INDEX IPV6
131 %token KEY
132 %token LANG LISTEN LOCATION LOG
133 %token OCSP OFF ON
134 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
135 %token RELAY_TO REQUIRE RETURN ROOT
136 %token SERVER SNI STRIP
137 %token TCP TOEXT TYPE TYPES
138 %token USE_TLS USER
139 %token VERIFYNAME
141 %token ERROR
143 %token <v.string> STRING
144 %token <v.number> NUM
146 %type <v.number> bool proxy_port
147 %type <v.string> string numberstring listen_addr
149 %%
151 conf : /* empty */
152 | conf include '\n'
153 | conf '\n'
154 | conf varset '\n'
155 | conf option '\n'
156 | conf vhost '\n'
157 | conf types '\n'
158 | conf error '\n' { file->errors++; }
161 include : INCLUDE STRING {
162 struct file *nfile;
164 if ((nfile = pushfile($2, 0)) == NULL) {
165 yyerror("failed to include file %s", $2);
166 free($2);
167 YYERROR;
169 free($2);
171 file = nfile;
172 lungetc('\n');
176 bool : ON { $$ = 1; }
177 | OFF { $$ = 0; }
180 string : string STRING {
181 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
182 free($1);
183 free($2);
184 yyerror("string: asprintf: %s", strerror(errno));
185 YYERROR;
187 free($1);
188 free($2);
190 | STRING
193 numberstring : NUM {
194 char *s;
195 if (asprintf(&s, "%d", $1) == -1) {
196 yyerror("asprintf: number");
197 YYERROR;
199 $$ = s;
201 | STRING
204 varset : STRING '=' string {
205 char *s = $1;
206 while (*s++) {
207 if (isspace((unsigned char)*s)) {
208 yyerror("macro name cannot contain "
209 "whitespaces");
210 free($1);
211 free($3);
212 YYERROR;
215 symset($1, $3, 0);
216 free($1);
217 free($3);
221 option : CHROOT string {
222 if (strlcpy(conf->chroot, $2, sizeof(conf->chroot)) >=
223 sizeof(conf->chroot))
224 yyerror("chroot path too long");
225 free($2);
227 | IPV6 bool {
228 yywarn("option `ipv6' is deprecated,"
229 " please use `listen on'");
230 if ($2)
231 default_host = "*";
232 else
233 default_host = "0.0.0.0";
235 | PORT NUM {
236 yywarn("option `port' is deprecated,"
237 " please use `listen on'");
238 default_port = $2;
240 | PREFORK NUM { conf->prefork = check_prefork_num($2); }
241 | PROTOCOLS string {
242 if (tls_config_parse_protocols(&conf->protos, $2) == -1)
243 yyerror("invalid protocols string \"%s\"", $2);
244 free($2);
246 | USER string {
247 if (strlcpy(conf->user, $2, sizeof(conf->user)) >=
248 sizeof(conf->user))
249 yyerror("user name too long");
250 free($2);
254 vhost : SERVER string {
255 host = new_vhost();
256 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
258 loc = new_location();
259 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
261 TAILQ_INIT(&host->proxies);
263 (void) strlcpy(loc->match, "*", sizeof(loc->match));
264 (void) strlcpy(host->domain, $2, sizeof(host->domain));
266 if (strstr($2, "xn--") != NULL) {
267 yywarn("\"%s\" looks like punycode: you "
268 "should use the decoded hostname", $2);
271 free($2);
272 } '{' optnl servbody '}' {
273 if (host->cert_path == NULL ||
274 host->key_path == NULL)
275 yyerror("invalid vhost definition: %s",
276 host->domain);
277 if (TAILQ_EMPTY(&host->addrs)) {
278 char portno[32];
279 int r;
281 r = snprintf(portno, sizeof(portno), "%d",
282 default_port);
283 if (r < 0 || (size_t)r >= sizeof(portno))
284 fatal("snprintf");
286 yywarn("missing `listen on' in server %s,"
287 " assuming %s port %d", $2, default_host,
288 default_port);
289 listen_on(default_host, portno);
292 | error '}' { yyerror("bad server directive"); }
295 servbody : /* empty */
296 | servbody servopt optnl
297 | servbody location optnl
298 | servbody proxy optnl
301 listen_addr : '*' { $$ = NULL; }
302 | STRING
305 servopt : ALIAS string {
306 struct alist *a;
308 a = xcalloc(1, sizeof(*a));
309 (void) strlcpy(a->alias, $2, sizeof(a->alias));
310 free($2);
311 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
313 | CERT string {
314 ensure_absolute_path($2);
315 free(host->cert_path);
316 host->cert_path = $2;
318 | KEY string {
319 ensure_absolute_path($2);
320 free(host->key_path);
321 host->key_path = $2;
323 | OCSP string {
324 ensure_absolute_path($2);
325 free(host->ocsp_path);
326 host->ocsp_path = $2;
328 | PARAM string '=' string {
329 add_param($2, $4);
331 | LISTEN ON listen_addr PORT STRING {
332 listen_on($3, $5);
333 free($3);
334 free($5);
336 | LISTEN ON listen_addr PORT NUM {
337 char portno[32];
338 int r;
340 r = snprintf(portno, sizeof(portno), "%d", $5);
341 if (r < 0 || (size_t)r >= sizeof(portno))
342 fatal("snprintf");
344 listen_on($3, portno);
345 free($3);
347 | locopt
350 proxy : PROXY { advance_proxy(); }
351 proxy_matches '{' optnl proxy_opts '}' {
352 if (*proxy->host == '\0')
353 yyerror("invalid proxy block: missing `relay-to' option");
355 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
356 (proxy->cert_path != NULL && proxy->key_path == NULL))
357 yyerror("invalid proxy block: missing cert or key");
361 proxy_matches : /* empty */
362 | proxy_matches proxy_match
365 proxy_port : /* empty */ { $$ = 1965; }
366 | PORT STRING {
367 if (($$ = getservice($2)) == -1)
368 yyerror("invalid port number %s", $2);
369 free($2);
371 | PORT NUM { $$ = $2; }
374 proxy_match : PROTO string {
375 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
376 free($2);
378 | FOR_HOST string proxy_port {
379 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
380 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
381 "%d", $3);
382 free($2);
386 proxy_opts : /* empty */
387 | proxy_opts proxy_opt optnl
390 proxy_opt : CERT string {
391 free(proxy->cert);
392 ensure_absolute_path($2);
393 proxy->cert_path = $2;
395 | KEY string {
396 free(proxy->key);
397 ensure_absolute_path($2);
398 proxy->key_path = $2;
400 | PROTOCOLS string {
401 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
402 yyerror("invalid protocols string \"%s\"", $2);
403 free($2);
405 | RELAY_TO string proxy_port {
406 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
407 (void) snprintf(proxy->port, sizeof(proxy->port),
408 "%d", $3);
409 free($2);
411 | REQUIRE CLIENT CA string {
412 ensure_absolute_path($4);
413 proxy->reqca_path = $4;
415 | SNI string {
416 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
417 free($2);
419 | USE_TLS bool {
420 proxy->notls = !$2;
422 | VERIFYNAME bool {
423 proxy->noverifyname = !$2;
427 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
428 /* drop the starting '/' if any */
429 if (*$3 == '/')
430 memmove($3, $3+1, strlen($3));
431 (void) strlcpy(loc->match, $3, sizeof(loc->match));
432 free($3);
434 | error '}'
437 locopts : /* empty */
438 | locopts locopt optnl
441 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
442 | BLOCK RETURN NUM string {
443 check_block_fmt($4);
444 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
445 loc->block_code = check_block_code($3);
446 free($4);
448 | BLOCK RETURN NUM {
449 (void) strlcpy(loc->block_fmt, "temporary failure",
450 sizeof(loc->block_fmt));
451 loc->block_code = check_block_code($3);
452 if ($3 >= 30 && $3 < 40)
453 yyerror("missing `meta' for block return %d", $3);
455 | BLOCK {
456 (void) strlcpy(loc->block_fmt, "temporary failure",
457 sizeof(loc->block_fmt));
458 loc->block_code = 40;
460 | DEFAULT TYPE string {
461 (void) strlcpy(loc->default_mime, $3,
462 sizeof(loc->default_mime));
463 free($3);
465 | FASTCGI fastcgi
466 | INDEX string {
467 (void) strlcpy(loc->index, $2, sizeof(loc->index));
468 free($2);
470 | LANG string {
471 (void) strlcpy(loc->lang, $2,
472 sizeof(loc->lang));
473 free($2);
475 | LOG bool { loc->disable_log = !$2; }
476 | REQUIRE CLIENT CA string {
477 ensure_absolute_path($4);
478 loc->reqca_path = $4;
480 | ROOT string {
481 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
482 free($2);
484 | STRIP NUM { loc->strip = check_strip_no($2); }
487 fastcgi : string {
488 loc->fcgi = fastcgi_conf($1, NULL);
489 free($1);
491 | TCP string PORT NUM {
492 char *c;
493 if (asprintf(&c, "%d", $4) == -1)
494 fatal("asprintf");
495 loc->fcgi = fastcgi_conf($2, c);
496 free($2);
498 | TCP string {
499 loc->fcgi = fastcgi_conf($2, "9000");
500 free($2);
502 | TCP string PORT string {
503 loc->fcgi = fastcgi_conf($2, $4);
504 free($2);
505 free($4);
509 types : TYPES '{' optnl mediaopts_l '}' ;
511 mediaopts_l : mediaopts_l mediaoptsl nl
512 | mediaoptsl nl
515 mediaoptsl : STRING {
516 free(current_media);
517 current_media = $1;
518 } medianames_l optsemicolon
519 | include
522 medianames_l : medianames_l medianamesl
523 | medianamesl
526 medianamesl : numberstring {
527 if (add_mime(&conf->mime, current_media, $1) == -1)
528 fatal("add_mime");
529 free($1);
533 nl : '\n' optnl
536 optnl : '\n' optnl /* zero or more newlines */
537 | ';' optnl /* semicolons too */
538 | /*empty*/
541 optsemicolon : ';'
545 %%
547 static const struct keyword {
548 const char *word;
549 int token;
550 } keywords[] = {
551 /* these MUST be sorted */
552 {"alias", ALIAS},
553 {"auto", AUTO},
554 {"block", BLOCK},
555 {"ca", CA},
556 {"cert", CERT},
557 {"chroot", CHROOT},
558 {"client", CLIENT},
559 {"default", DEFAULT},
560 {"fastcgi", FASTCGI},
561 {"for-host", FOR_HOST},
562 {"include", INCLUDE},
563 {"index", INDEX},
564 {"ipv6", IPV6},
565 {"key", KEY},
566 {"lang", LANG},
567 {"listen", LISTEN},
568 {"location", LOCATION},
569 {"log", LOG},
570 {"ocsp", OCSP},
571 {"off", OFF},
572 {"on", ON},
573 {"param", PARAM},
574 {"port", PORT},
575 {"prefork", PREFORK},
576 {"proto", PROTO},
577 {"protocols", PROTOCOLS},
578 {"proxy", PROXY},
579 {"relay-to", RELAY_TO},
580 {"require", REQUIRE},
581 {"return", RETURN},
582 {"root", ROOT},
583 {"server", SERVER},
584 {"sni", SNI},
585 {"strip", STRIP},
586 {"tcp", TCP},
587 {"to-ext", TOEXT},
588 {"type", TYPE},
589 {"types", TYPES},
590 {"use-tls", USE_TLS},
591 {"user", USER},
592 {"verifyname", VERIFYNAME},
593 };
595 void
596 yyerror(const char *msg, ...)
598 va_list ap;
600 file->errors++;
602 va_start(ap, msg);
603 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
604 vfprintf(stderr, msg, ap);
605 fprintf(stderr, "\n");
606 va_end(ap);
609 void
610 yywarn(const char *msg, ...)
612 va_list ap;
614 va_start(ap, msg);
615 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
616 vfprintf(stderr, msg, ap);
617 fprintf(stderr, "\n");
618 va_end(ap);
621 int
622 kw_cmp(const void *k, const void *e)
624 return strcmp(k, ((struct keyword *)e)->word);
627 int
628 lookup(char *s)
630 const struct keyword *p;
632 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
633 sizeof(keywords[0]), kw_cmp);
635 if (p)
636 return p->token;
637 else
638 return STRING;
641 #define START_EXPAND 1
642 #define DONE_EXPAND 2
644 static int expanding;
646 int
647 igetc(void)
649 int c;
651 while (1) {
652 if (file->ungetpos > 0)
653 c = file->ungetbuf[--file->ungetpos];
654 else
655 c = getc(file->stream);
657 if (c == START_EXPAND)
658 expanding = 1;
659 else if (c == DONE_EXPAND)
660 expanding = 0;
661 else
662 break;
664 return c;
667 int
668 lgetc(int quotec)
670 int c, next;
672 if (quotec) {
673 if ((c = igetc()) == EOF) {
674 yyerror("reached end of file while parsing "
675 "quoted string");
676 if (file == topfile || popfile() == EOF)
677 return EOF;
678 return quotec;
680 return c;
683 while ((c = igetc()) == '\\') {
684 next = igetc();
685 if (next != '\n') {
686 c = next;
687 break;
689 yylval.lineno = file->lineno;
690 file->lineno++;
693 if (c == EOF) {
694 /*
695 * Fake EOL when hit EOF for the first time. This gets line
696 * count right if last line in included file is syntactically
697 * invalid and has no newline.
698 */
699 if (file->eof_reached == 0) {
700 file->eof_reached = 1;
701 return '\n';
703 while (c == EOF) {
704 if (file == topfile || popfile() == EOF)
705 return EOF;
706 c = igetc();
709 return c;
712 void
713 lungetc(int c)
715 if (c == EOF)
716 return;
718 if (file->ungetpos >= file->ungetsize) {
719 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
720 if (p == NULL)
721 fatal("lungetc");
722 file->ungetbuf = p;
723 file->ungetsize *= 2;
725 file->ungetbuf[file->ungetpos++] = c;
728 int
729 findeol(void)
731 int c;
733 /* Skip to either EOF or the first real EOL. */
734 while (1) {
735 c = lgetc(0);
736 if (c == '\n') {
737 file->lineno++;
738 break;
740 if (c == EOF)
741 break;
743 return ERROR;
746 int
747 yylex(void)
749 char buf[8096];
750 char *p, *val;
751 int quotec, next, c;
752 int token;
754 top:
755 p = buf;
756 while ((c = lgetc(0)) == ' ' || c == '\t')
757 ; /* nothing */
759 yylval.lineno = file->lineno;
760 if (c == '#')
761 while ((c = lgetc(0)) != '\n' && c != EOF)
762 ; /* nothing */
763 if (c == '$' && !expanding) {
764 while (1) {
765 if ((c = lgetc(0)) == EOF)
766 return 0;
767 if (p + 1 >= buf + sizeof(buf) -1) {
768 yyerror("string too long");
769 return findeol();
771 if (isalnum(c) || c == '_') {
772 *p++ = c;
773 continue;
775 *p = '\0';
776 lungetc(c);
777 break;
779 val = symget(buf);
780 if (val == NULL) {
781 yyerror("macro `%s' not defined", buf);
782 return findeol();
784 yylval.v.string = xstrdup(val);
785 return STRING;
787 if (c == '@' && !expanding) {
788 while (1) {
789 if ((c = lgetc(0)) == EOF)
790 return 0;
792 if (p + 1 >= buf + sizeof(buf) - 1) {
793 yyerror("string too long");
794 return findeol();
796 if (isalnum(c) || c == '_') {
797 *p++ = c;
798 continue;
800 *p = '\0';
801 lungetc(c);
802 break;
804 val = symget(buf);
805 if (val == NULL) {
806 yyerror("macro '%s' not defined", buf);
807 return findeol();
809 p = val + strlen(val) - 1;
810 lungetc(DONE_EXPAND);
811 while (p >= val) {
812 lungetc(*p);
813 p--;
815 lungetc(START_EXPAND);
816 goto top;
819 switch (c) {
820 case '\'':
821 case '"':
822 quotec = c;
823 while (1) {
824 if ((c = lgetc(quotec)) == EOF)
825 return 0;
826 if (c == '\n') {
827 file->lineno++;
828 continue;
829 } else if (c == '\\') {
830 if ((next = lgetc(quotec)) == EOF)
831 return (0);
832 if (next == quotec || next == ' ' ||
833 next == '\t')
834 c = next;
835 else if (next == '\n') {
836 file->lineno++;
837 continue;
838 } else
839 lungetc(next);
840 } else if (c == quotec) {
841 *p = '\0';
842 break;
843 } else if (c == '\0') {
844 yyerror("invalid syntax");
845 return findeol();
847 if (p + 1 >= buf + sizeof(buf) - 1) {
848 yyerror("string too long");
849 return findeol();
851 *p++ = c;
853 yylval.v.string = strdup(buf);
854 if (yylval.v.string == NULL)
855 fatal("yylex: strdup");
856 return STRING;
859 #define allowed_to_end_number(x) \
860 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
862 if (c == '-' || isdigit(c)) {
863 do {
864 *p++ = c;
865 if ((size_t)(p-buf) >= sizeof(buf)) {
866 yyerror("string too long");
867 return findeol();
869 } while ((c = lgetc(0)) != EOF && isdigit(c));
870 lungetc(c);
871 if (p == buf + 1 && buf[0] == '-')
872 goto nodigits;
873 if (c == EOF || allowed_to_end_number(c)) {
874 const char *errstr = NULL;
876 *p = '\0';
877 yylval.v.number = strtonum(buf, LLONG_MIN,
878 LLONG_MAX, &errstr);
879 if (errstr) {
880 yyerror("\"%s\" invalid number: %s",
881 buf, errstr);
882 return findeol();
884 return NUM;
885 } else {
886 nodigits:
887 while (p > buf + 1)
888 lungetc(*--p);
889 c = *--p;
890 if (c == '-')
891 return c;
895 #define allowed_in_string(x) \
896 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
897 x != '{' && x != '}' && \
898 x != '!' && x != '=' && x != '#' && \
899 x != ',' && x != ';'))
901 if (isalnum(c) || c == ':' || c == '_') {
902 do {
903 *p++ = c;
904 if ((size_t)(p-buf) >= sizeof(buf)) {
905 yyerror("string too long");
906 return findeol();
908 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
909 lungetc(c);
910 *p = '\0';
911 if ((token = lookup(buf)) == STRING)
912 yylval.v.string = xstrdup(buf);
913 return token;
915 if (c == '\n') {
916 yylval.lineno = file->lineno;
917 file->lineno++;
919 if (c == EOF)
920 return 0;
921 return c;
924 struct file *
925 pushfile(const char *name, int secret)
927 struct file *nfile;
929 nfile = xcalloc(1, sizeof(*nfile));
930 nfile->name = xstrdup(name);
931 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
932 log_warn("can't open %s", nfile->name);
933 free(nfile->name);
934 free(nfile);
935 return NULL;
937 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
938 nfile->ungetsize = 16;
939 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
940 TAILQ_INSERT_TAIL(&files, nfile, entry);
941 return nfile;
944 int
945 popfile(void)
947 struct file *prev;
949 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
950 prev->errors += file->errors;
952 TAILQ_REMOVE(&files, file, entry);
953 fclose(file->stream);
954 free(file->name);
955 free(file->ungetbuf);
956 free(file);
957 file = prev;
958 return file ? 0 : EOF;
961 int
962 parse_conf(struct conf *c, const char *filename)
964 struct sym *sym, *next;
966 default_host = "*";
967 default_port = 1965;
969 conf = c;
971 file = pushfile(filename, 0);
972 if (file == NULL)
973 return -1;
974 topfile = file;
976 yyparse();
977 errors = file->errors;
978 popfile();
980 /* Free macros and check which have not been used. */
981 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
982 /* TODO: warn if !sym->used */
983 if (!sym->persist) {
984 free(sym->name);
985 free(sym->val);
986 TAILQ_REMOVE(&symhead, sym, entry);
987 free(sym);
991 if (errors)
992 return -1;
993 return 0;
996 int
997 symset(const char *name, const char *val, int persist)
999 struct sym *sym;
1001 TAILQ_FOREACH(sym, &symhead, entry) {
1002 if (!strcmp(name, sym->name))
1003 break;
1006 if (sym != NULL) {
1007 if (sym->persist)
1008 return 0;
1009 else {
1010 free(sym->name);
1011 free(sym->val);
1012 TAILQ_REMOVE(&symhead, sym, entry);
1013 free(sym);
1017 sym = xcalloc(1, sizeof(*sym));
1018 sym->name = xstrdup(name);
1019 sym->val = xstrdup(val);
1020 sym->used = 0;
1021 sym->persist = persist;
1023 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1024 return 0;
1027 int
1028 cmdline_symset(char *s)
1030 char *sym, *val;
1031 int ret;
1033 if ((val = strrchr(s, '=')) == NULL)
1034 return -1;
1035 sym = xcalloc(1, val - s + 1);
1036 memcpy(sym, s, val - s);
1037 ret = symset(sym, val + 1, 1);
1038 free(sym);
1039 return ret;
1042 char *
1043 symget(const char *nam)
1045 struct sym *sym;
1047 TAILQ_FOREACH(sym, &symhead, entry) {
1048 if (strcmp(nam, sym->name) == 0) {
1049 sym->used = 1;
1050 return sym->val;
1053 return NULL;
1056 char *
1057 ensure_absolute_path(char *path)
1059 if (path == NULL || *path != '/')
1060 yyerror("not an absolute path: %s", path);
1061 return path;
1064 int
1065 check_block_code(int n)
1067 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1068 yyerror("invalid block code %d", n);
1069 return n;
1072 char *
1073 check_block_fmt(char *fmt)
1075 char *s;
1077 for (s = fmt; *s; ++s) {
1078 if (*s != '%')
1079 continue;
1080 switch (*++s) {
1081 case '%':
1082 case 'p':
1083 case 'q':
1084 case 'P':
1085 case 'N':
1086 break;
1087 default:
1088 yyerror("invalid format specifier %%%c", *s);
1092 return fmt;
1095 int
1096 check_strip_no(int n)
1098 if (n <= 0)
1099 yyerror("invalid strip number %d", n);
1100 return n;
1103 int
1104 check_port_num(int n)
1106 if (n <= 0 || n >= UINT16_MAX)
1107 yyerror("port number is %s: %d",
1108 n <= 0 ? "too small" : "too large",
1109 n);
1110 return n;
1113 int
1114 check_prefork_num(int n)
1116 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1117 yyerror("invalid prefork number %d", n);
1118 return n;
1121 void
1122 advance_loc(void)
1124 loc = new_location();
1125 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1128 void
1129 advance_proxy(void)
1131 proxy = new_proxy();
1132 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1135 void
1136 parsehp(char *str, char **host, const char **port, const char *def)
1138 char *at;
1139 const char *errstr;
1141 *host = str;
1143 if ((at = strchr(str, ':')) != NULL) {
1144 *at++ = '\0';
1145 *port = at;
1146 } else
1147 *port = def;
1149 strtonum(*port, 1, UINT16_MAX, &errstr);
1150 if (errstr != NULL)
1151 yyerror("port is %s: %s", errstr, *port);
1154 int
1155 fastcgi_conf(const char *path, const char *port)
1157 struct fcgi *f;
1158 int i = 0;
1160 TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1161 if (!strcmp(f->path, path) &&
1162 ((port == NULL && *f->port == '\0') ||
1163 !strcmp(f->port, port)))
1164 return i;
1165 ++i;
1168 f = xcalloc(1, sizeof(*f));
1169 f->id = i;
1170 (void)strlcpy(f->path, path, sizeof(f->path));
1171 if (port != NULL)
1172 (void)strlcpy(f->port, port, sizeof(f->port));
1173 TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1175 return f->id;
1178 void
1179 add_param(char *name, char *val)
1181 struct envlist *e;
1182 struct envhead *h = &host->params;
1184 e = xcalloc(1, sizeof(*e));
1185 (void) strlcpy(e->name, name, sizeof(e->name));
1186 (void) strlcpy(e->value, val, sizeof(e->value));
1187 TAILQ_INSERT_TAIL(h, e, envs);
1190 int
1191 getservice(const char *n)
1193 struct servent *s;
1194 const char *errstr;
1195 long long llval;
1197 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1198 if (errstr) {
1199 s = getservbyname(n, "tcp");
1200 if (s == NULL)
1201 s = getservbyname(n, "udp");
1202 if (s == NULL)
1203 return (-1);
1204 return (ntohs(s->s_port));
1207 return ((unsigned short)llval);
1210 static void
1211 add_to_addr_queue(struct addrhead *a, struct addrinfo *ai)
1213 struct address *addr;
1214 struct sockaddr_in *sin;
1215 struct sockaddr_in6 *sin6;
1217 if (ai->ai_addrlen > sizeof(addr->ss))
1218 fatalx("ai_addrlen larger than a sockaddr_storage");
1220 TAILQ_FOREACH(addr, a, addrs) {
1221 if (addr->ai_flags == ai->ai_flags &&
1222 addr->ai_family == ai->ai_family &&
1223 addr->ai_socktype == ai->ai_socktype &&
1224 addr->ai_protocol == ai->ai_protocol &&
1225 addr->slen == ai->ai_addrlen &&
1226 !memcmp(&addr->ss, ai->ai_addr, addr->slen))
1227 return;
1230 addr = xcalloc(1, sizeof(*addr));
1231 addr->ai_flags = ai->ai_flags;
1232 addr->ai_family = ai->ai_family;
1233 addr->ai_socktype = ai->ai_socktype;
1234 addr->ai_protocol = ai->ai_protocol;
1235 addr->slen = ai->ai_addrlen;
1236 memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
1238 /* for commodity */
1239 switch (addr->ai_family) {
1240 case AF_INET:
1241 sin = (struct sockaddr_in *)&addr->ss;
1242 addr->port = ntohs(sin->sin_port);
1243 break;
1244 case AF_INET6:
1245 sin6 = (struct sockaddr_in6 *)&addr->ss;
1246 addr->port = ntohs(sin6->sin6_port);
1247 break;
1248 default:
1249 fatalx("unknown socket family %d", addr->ai_family);
1252 addr->sock = -1;
1254 TAILQ_INSERT_HEAD(a, addr, addrs);
1257 void
1258 listen_on(const char *hostname, const char *servname)
1260 struct addrinfo hints, *res, *res0;
1261 int error;
1263 memset(&hints, 0, sizeof(hints));
1264 hints.ai_family = AF_UNSPEC;
1265 hints.ai_socktype = SOCK_STREAM;
1266 hints.ai_flags = AI_PASSIVE;
1267 error = getaddrinfo(hostname, servname, &hints, &res0);
1268 if (error) {
1269 yyerror("listen on \"%s\" port %s: %s", hostname, servname,
1270 gai_strerror(errno));
1271 return;
1274 for (res = res0; res; res = res->ai_next) {
1275 add_to_addr_queue(&host->addrs, res);
1276 add_to_addr_queue(&conf->addrs, res);
1279 freeaddrinfo(res0);