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 {
332 listen_on($3, "1965");
334 | LISTEN ON listen_addr PORT STRING {
335 listen_on($3, $5);
336 free($3);
337 free($5);
339 | LISTEN ON listen_addr PORT NUM {
340 char portno[32];
341 int r;
343 r = snprintf(portno, sizeof(portno), "%d", $5);
344 if (r < 0 || (size_t)r >= sizeof(portno))
345 fatal("snprintf");
347 listen_on($3, portno);
348 free($3);
350 | locopt
353 proxy : PROXY { advance_proxy(); }
354 proxy_matches '{' optnl proxy_opts '}' {
355 if (*proxy->host == '\0')
356 yyerror("invalid proxy block: missing `relay-to' option");
358 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
359 (proxy->cert_path != NULL && proxy->key_path == NULL))
360 yyerror("invalid proxy block: missing cert or key");
364 proxy_matches : /* empty */
365 | proxy_matches proxy_match
368 proxy_port : /* empty */ { $$ = 1965; }
369 | PORT STRING {
370 if (($$ = getservice($2)) == -1)
371 yyerror("invalid port number %s", $2);
372 free($2);
374 | PORT NUM { $$ = $2; }
377 proxy_match : PROTO string {
378 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
379 free($2);
381 | FOR_HOST string proxy_port {
382 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
383 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
384 "%d", $3);
385 free($2);
389 proxy_opts : /* empty */
390 | proxy_opts proxy_opt optnl
393 proxy_opt : CERT string {
394 free(proxy->cert);
395 ensure_absolute_path($2);
396 proxy->cert_path = $2;
398 | KEY string {
399 free(proxy->key);
400 ensure_absolute_path($2);
401 proxy->key_path = $2;
403 | PROTOCOLS string {
404 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
405 yyerror("invalid protocols string \"%s\"", $2);
406 free($2);
408 | RELAY_TO string proxy_port {
409 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
410 (void) snprintf(proxy->port, sizeof(proxy->port),
411 "%d", $3);
412 free($2);
414 | REQUIRE CLIENT CA string {
415 ensure_absolute_path($4);
416 proxy->reqca_path = $4;
418 | SNI string {
419 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
420 free($2);
422 | USE_TLS bool {
423 proxy->notls = !$2;
425 | VERIFYNAME bool {
426 proxy->noverifyname = !$2;
430 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
431 /* drop the starting '/' if any */
432 if (*$3 == '/')
433 memmove($3, $3+1, strlen($3));
434 (void) strlcpy(loc->match, $3, sizeof(loc->match));
435 free($3);
437 | error '}'
440 locopts : /* empty */
441 | locopts locopt optnl
444 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
445 | BLOCK RETURN NUM string {
446 check_block_fmt($4);
447 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
448 loc->block_code = check_block_code($3);
449 free($4);
451 | BLOCK RETURN NUM {
452 (void) strlcpy(loc->block_fmt, "temporary failure",
453 sizeof(loc->block_fmt));
454 loc->block_code = check_block_code($3);
455 if ($3 >= 30 && $3 < 40)
456 yyerror("missing `meta' for block return %d", $3);
458 | BLOCK {
459 (void) strlcpy(loc->block_fmt, "temporary failure",
460 sizeof(loc->block_fmt));
461 loc->block_code = 40;
463 | DEFAULT TYPE string {
464 (void) strlcpy(loc->default_mime, $3,
465 sizeof(loc->default_mime));
466 free($3);
468 | FASTCGI fastcgi
469 | INDEX string {
470 (void) strlcpy(loc->index, $2, sizeof(loc->index));
471 free($2);
473 | LANG string {
474 (void) strlcpy(loc->lang, $2,
475 sizeof(loc->lang));
476 free($2);
478 | LOG bool { loc->disable_log = !$2; }
479 | REQUIRE CLIENT CA string {
480 ensure_absolute_path($4);
481 loc->reqca_path = $4;
483 | ROOT string {
484 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
485 free($2);
487 | STRIP NUM { loc->strip = check_strip_no($2); }
490 fastcgi : string {
491 loc->fcgi = fastcgi_conf($1, NULL);
492 free($1);
494 | TCP string PORT NUM {
495 char *c;
496 if (asprintf(&c, "%d", $4) == -1)
497 fatal("asprintf");
498 loc->fcgi = fastcgi_conf($2, c);
499 free($2);
501 | TCP string {
502 loc->fcgi = fastcgi_conf($2, "9000");
503 free($2);
505 | TCP string PORT string {
506 loc->fcgi = fastcgi_conf($2, $4);
507 free($2);
508 free($4);
512 types : TYPES '{' optnl mediaopts_l '}' ;
514 mediaopts_l : mediaopts_l mediaoptsl nl
515 | mediaoptsl nl
518 mediaoptsl : STRING {
519 free(current_media);
520 current_media = $1;
521 } medianames_l optsemicolon
522 | include
525 medianames_l : medianames_l medianamesl
526 | medianamesl
529 medianamesl : numberstring {
530 if (add_mime(&conf->mime, current_media, $1) == -1)
531 fatal("add_mime");
532 free($1);
536 nl : '\n' optnl
539 optnl : '\n' optnl /* zero or more newlines */
540 | ';' optnl /* semicolons too */
541 | /*empty*/
544 optsemicolon : ';'
548 %%
550 static const struct keyword {
551 const char *word;
552 int token;
553 } keywords[] = {
554 /* these MUST be sorted */
555 {"alias", ALIAS},
556 {"auto", AUTO},
557 {"block", BLOCK},
558 {"ca", CA},
559 {"cert", CERT},
560 {"chroot", CHROOT},
561 {"client", CLIENT},
562 {"default", DEFAULT},
563 {"fastcgi", FASTCGI},
564 {"for-host", FOR_HOST},
565 {"include", INCLUDE},
566 {"index", INDEX},
567 {"ipv6", IPV6},
568 {"key", KEY},
569 {"lang", LANG},
570 {"listen", LISTEN},
571 {"location", LOCATION},
572 {"log", LOG},
573 {"ocsp", OCSP},
574 {"off", OFF},
575 {"on", ON},
576 {"param", PARAM},
577 {"port", PORT},
578 {"prefork", PREFORK},
579 {"proto", PROTO},
580 {"protocols", PROTOCOLS},
581 {"proxy", PROXY},
582 {"relay-to", RELAY_TO},
583 {"require", REQUIRE},
584 {"return", RETURN},
585 {"root", ROOT},
586 {"server", SERVER},
587 {"sni", SNI},
588 {"strip", STRIP},
589 {"tcp", TCP},
590 {"to-ext", TOEXT},
591 {"type", TYPE},
592 {"types", TYPES},
593 {"use-tls", USE_TLS},
594 {"user", USER},
595 {"verifyname", VERIFYNAME},
596 };
598 void
599 yyerror(const char *msg, ...)
601 va_list ap;
603 file->errors++;
605 va_start(ap, msg);
606 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
607 vfprintf(stderr, msg, ap);
608 fprintf(stderr, "\n");
609 va_end(ap);
612 void
613 yywarn(const char *msg, ...)
615 va_list ap;
617 va_start(ap, msg);
618 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
619 vfprintf(stderr, msg, ap);
620 fprintf(stderr, "\n");
621 va_end(ap);
624 int
625 kw_cmp(const void *k, const void *e)
627 return strcmp(k, ((struct keyword *)e)->word);
630 int
631 lookup(char *s)
633 const struct keyword *p;
635 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
636 sizeof(keywords[0]), kw_cmp);
638 if (p)
639 return p->token;
640 else
641 return STRING;
644 #define START_EXPAND 1
645 #define DONE_EXPAND 2
647 static int expanding;
649 int
650 igetc(void)
652 int c;
654 while (1) {
655 if (file->ungetpos > 0)
656 c = file->ungetbuf[--file->ungetpos];
657 else
658 c = getc(file->stream);
660 if (c == START_EXPAND)
661 expanding = 1;
662 else if (c == DONE_EXPAND)
663 expanding = 0;
664 else
665 break;
667 return c;
670 int
671 lgetc(int quotec)
673 int c, next;
675 if (quotec) {
676 if ((c = igetc()) == EOF) {
677 yyerror("reached end of file while parsing "
678 "quoted string");
679 if (file == topfile || popfile() == EOF)
680 return EOF;
681 return quotec;
683 return c;
686 while ((c = igetc()) == '\\') {
687 next = igetc();
688 if (next != '\n') {
689 c = next;
690 break;
692 yylval.lineno = file->lineno;
693 file->lineno++;
696 if (c == EOF) {
697 /*
698 * Fake EOL when hit EOF for the first time. This gets line
699 * count right if last line in included file is syntactically
700 * invalid and has no newline.
701 */
702 if (file->eof_reached == 0) {
703 file->eof_reached = 1;
704 return '\n';
706 while (c == EOF) {
707 if (file == topfile || popfile() == EOF)
708 return EOF;
709 c = igetc();
712 return c;
715 void
716 lungetc(int c)
718 if (c == EOF)
719 return;
721 if (file->ungetpos >= file->ungetsize) {
722 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
723 if (p == NULL)
724 fatal("lungetc");
725 file->ungetbuf = p;
726 file->ungetsize *= 2;
728 file->ungetbuf[file->ungetpos++] = c;
731 int
732 findeol(void)
734 int c;
736 /* Skip to either EOF or the first real EOL. */
737 while (1) {
738 c = lgetc(0);
739 if (c == '\n') {
740 file->lineno++;
741 break;
743 if (c == EOF)
744 break;
746 return ERROR;
749 int
750 yylex(void)
752 char buf[8096];
753 char *p, *val;
754 int quotec, next, c;
755 int token;
757 top:
758 p = buf;
759 while ((c = lgetc(0)) == ' ' || c == '\t')
760 ; /* nothing */
762 yylval.lineno = file->lineno;
763 if (c == '#')
764 while ((c = lgetc(0)) != '\n' && c != EOF)
765 ; /* nothing */
766 if (c == '$' && !expanding) {
767 while (1) {
768 if ((c = lgetc(0)) == EOF)
769 return 0;
770 if (p + 1 >= buf + sizeof(buf) -1) {
771 yyerror("string too long");
772 return findeol();
774 if (isalnum(c) || c == '_') {
775 *p++ = c;
776 continue;
778 *p = '\0';
779 lungetc(c);
780 break;
782 val = symget(buf);
783 if (val == NULL) {
784 yyerror("macro `%s' not defined", buf);
785 return findeol();
787 yylval.v.string = xstrdup(val);
788 return STRING;
790 if (c == '@' && !expanding) {
791 while (1) {
792 if ((c = lgetc(0)) == EOF)
793 return 0;
795 if (p + 1 >= buf + sizeof(buf) - 1) {
796 yyerror("string too long");
797 return findeol();
799 if (isalnum(c) || c == '_') {
800 *p++ = c;
801 continue;
803 *p = '\0';
804 lungetc(c);
805 break;
807 val = symget(buf);
808 if (val == NULL) {
809 yyerror("macro '%s' not defined", buf);
810 return findeol();
812 p = val + strlen(val) - 1;
813 lungetc(DONE_EXPAND);
814 while (p >= val) {
815 lungetc(*p);
816 p--;
818 lungetc(START_EXPAND);
819 goto top;
822 switch (c) {
823 case '\'':
824 case '"':
825 quotec = c;
826 while (1) {
827 if ((c = lgetc(quotec)) == EOF)
828 return 0;
829 if (c == '\n') {
830 file->lineno++;
831 continue;
832 } else if (c == '\\') {
833 if ((next = lgetc(quotec)) == EOF)
834 return (0);
835 if (next == quotec || next == ' ' ||
836 next == '\t')
837 c = next;
838 else if (next == '\n') {
839 file->lineno++;
840 continue;
841 } else
842 lungetc(next);
843 } else if (c == quotec) {
844 *p = '\0';
845 break;
846 } else if (c == '\0') {
847 yyerror("invalid syntax");
848 return findeol();
850 if (p + 1 >= buf + sizeof(buf) - 1) {
851 yyerror("string too long");
852 return findeol();
854 *p++ = c;
856 yylval.v.string = strdup(buf);
857 if (yylval.v.string == NULL)
858 fatal("yylex: strdup");
859 return STRING;
862 #define allowed_to_end_number(x) \
863 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
865 if (c == '-' || isdigit(c)) {
866 do {
867 *p++ = c;
868 if ((size_t)(p-buf) >= sizeof(buf)) {
869 yyerror("string too long");
870 return findeol();
872 } while ((c = lgetc(0)) != EOF && isdigit(c));
873 lungetc(c);
874 if (p == buf + 1 && buf[0] == '-')
875 goto nodigits;
876 if (c == EOF || allowed_to_end_number(c)) {
877 const char *errstr = NULL;
879 *p = '\0';
880 yylval.v.number = strtonum(buf, LLONG_MIN,
881 LLONG_MAX, &errstr);
882 if (errstr) {
883 yyerror("\"%s\" invalid number: %s",
884 buf, errstr);
885 return findeol();
887 return NUM;
888 } else {
889 nodigits:
890 while (p > buf + 1)
891 lungetc(*--p);
892 c = *--p;
893 if (c == '-')
894 return c;
898 #define allowed_in_string(x) \
899 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
900 x != '{' && x != '}' && \
901 x != '!' && x != '=' && x != '#' && \
902 x != ',' && x != ';'))
904 if (isalnum(c) || c == ':' || c == '_') {
905 do {
906 *p++ = c;
907 if ((size_t)(p-buf) >= sizeof(buf)) {
908 yyerror("string too long");
909 return findeol();
911 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
912 lungetc(c);
913 *p = '\0';
914 if ((token = lookup(buf)) == STRING)
915 yylval.v.string = xstrdup(buf);
916 return token;
918 if (c == '\n') {
919 yylval.lineno = file->lineno;
920 file->lineno++;
922 if (c == EOF)
923 return 0;
924 return c;
927 struct file *
928 pushfile(const char *name, int secret)
930 struct file *nfile;
932 nfile = xcalloc(1, sizeof(*nfile));
933 nfile->name = xstrdup(name);
934 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
935 log_warn("can't open %s", nfile->name);
936 free(nfile->name);
937 free(nfile);
938 return NULL;
940 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
941 nfile->ungetsize = 16;
942 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
943 TAILQ_INSERT_TAIL(&files, nfile, entry);
944 return nfile;
947 int
948 popfile(void)
950 struct file *prev;
952 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
953 prev->errors += file->errors;
955 TAILQ_REMOVE(&files, file, entry);
956 fclose(file->stream);
957 free(file->name);
958 free(file->ungetbuf);
959 free(file);
960 file = prev;
961 return file ? 0 : EOF;
964 int
965 parse_conf(struct conf *c, const char *filename)
967 struct sym *sym, *next;
969 default_host = "*";
970 default_port = 1965;
972 conf = c;
974 file = pushfile(filename, 0);
975 if (file == NULL)
976 return -1;
977 topfile = file;
979 yyparse();
980 errors = file->errors;
981 popfile();
983 /* Free macros and check which have not been used. */
984 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
985 /* TODO: warn if !sym->used */
986 if (!sym->persist) {
987 free(sym->name);
988 free(sym->val);
989 TAILQ_REMOVE(&symhead, sym, entry);
990 free(sym);
994 if (errors)
995 return -1;
996 return 0;
999 int
1000 symset(const char *name, const char *val, int persist)
1002 struct sym *sym;
1004 TAILQ_FOREACH(sym, &symhead, entry) {
1005 if (!strcmp(name, sym->name))
1006 break;
1009 if (sym != NULL) {
1010 if (sym->persist)
1011 return 0;
1012 else {
1013 free(sym->name);
1014 free(sym->val);
1015 TAILQ_REMOVE(&symhead, sym, entry);
1016 free(sym);
1020 sym = xcalloc(1, sizeof(*sym));
1021 sym->name = xstrdup(name);
1022 sym->val = xstrdup(val);
1023 sym->used = 0;
1024 sym->persist = persist;
1026 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1027 return 0;
1030 int
1031 cmdline_symset(char *s)
1033 char *sym, *val;
1034 int ret;
1036 if ((val = strrchr(s, '=')) == NULL)
1037 return -1;
1038 sym = xcalloc(1, val - s + 1);
1039 memcpy(sym, s, val - s);
1040 ret = symset(sym, val + 1, 1);
1041 free(sym);
1042 return ret;
1045 char *
1046 symget(const char *nam)
1048 struct sym *sym;
1050 TAILQ_FOREACH(sym, &symhead, entry) {
1051 if (strcmp(nam, sym->name) == 0) {
1052 sym->used = 1;
1053 return sym->val;
1056 return NULL;
1059 char *
1060 ensure_absolute_path(char *path)
1062 if (path == NULL || *path != '/')
1063 yyerror("not an absolute path: %s", path);
1064 return path;
1067 int
1068 check_block_code(int n)
1070 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1071 yyerror("invalid block code %d", n);
1072 return n;
1075 char *
1076 check_block_fmt(char *fmt)
1078 char *s;
1080 for (s = fmt; *s; ++s) {
1081 if (*s != '%')
1082 continue;
1083 switch (*++s) {
1084 case '%':
1085 case 'p':
1086 case 'q':
1087 case 'P':
1088 case 'N':
1089 break;
1090 default:
1091 yyerror("invalid format specifier %%%c", *s);
1095 return fmt;
1098 int
1099 check_strip_no(int n)
1101 if (n <= 0)
1102 yyerror("invalid strip number %d", n);
1103 return n;
1106 int
1107 check_port_num(int n)
1109 if (n <= 0 || n >= UINT16_MAX)
1110 yyerror("port number is %s: %d",
1111 n <= 0 ? "too small" : "too large",
1112 n);
1113 return n;
1116 int
1117 check_prefork_num(int n)
1119 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1120 yyerror("invalid prefork number %d", n);
1121 return n;
1124 void
1125 advance_loc(void)
1127 loc = new_location();
1128 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1131 void
1132 advance_proxy(void)
1134 proxy = new_proxy();
1135 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1138 void
1139 parsehp(char *str, char **host, const char **port, const char *def)
1141 char *at;
1142 const char *errstr;
1144 *host = str;
1146 if ((at = strchr(str, ':')) != NULL) {
1147 *at++ = '\0';
1148 *port = at;
1149 } else
1150 *port = def;
1152 strtonum(*port, 1, UINT16_MAX, &errstr);
1153 if (errstr != NULL)
1154 yyerror("port is %s: %s", errstr, *port);
1157 int
1158 fastcgi_conf(const char *path, const char *port)
1160 struct fcgi *f;
1161 int i = 0;
1163 TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1164 if (!strcmp(f->path, path) &&
1165 ((port == NULL && *f->port == '\0') ||
1166 !strcmp(f->port, port)))
1167 return i;
1168 ++i;
1171 f = xcalloc(1, sizeof(*f));
1172 f->id = i;
1173 (void)strlcpy(f->path, path, sizeof(f->path));
1174 if (port != NULL)
1175 (void)strlcpy(f->port, port, sizeof(f->port));
1176 TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1178 return f->id;
1181 void
1182 add_param(char *name, char *val)
1184 struct envlist *e;
1185 struct envhead *h = &host->params;
1187 e = xcalloc(1, sizeof(*e));
1188 (void) strlcpy(e->name, name, sizeof(e->name));
1189 (void) strlcpy(e->value, val, sizeof(e->value));
1190 TAILQ_INSERT_TAIL(h, e, envs);
1193 int
1194 getservice(const char *n)
1196 struct servent *s;
1197 const char *errstr;
1198 long long llval;
1200 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1201 if (errstr) {
1202 s = getservbyname(n, "tcp");
1203 if (s == NULL)
1204 s = getservbyname(n, "udp");
1205 if (s == NULL)
1206 return (-1);
1207 return (ntohs(s->s_port));
1210 return ((unsigned short)llval);
1213 static void
1214 add_to_addr_queue(struct addrhead *a, struct addrinfo *ai)
1216 struct address *addr;
1217 struct sockaddr_in *sin;
1218 struct sockaddr_in6 *sin6;
1220 if (ai->ai_addrlen > sizeof(addr->ss))
1221 fatalx("ai_addrlen larger than a sockaddr_storage");
1223 TAILQ_FOREACH(addr, a, addrs) {
1224 if (addr->ai_flags == ai->ai_flags &&
1225 addr->ai_family == ai->ai_family &&
1226 addr->ai_socktype == ai->ai_socktype &&
1227 addr->ai_protocol == ai->ai_protocol &&
1228 addr->slen == ai->ai_addrlen &&
1229 !memcmp(&addr->ss, ai->ai_addr, addr->slen))
1230 return;
1233 addr = xcalloc(1, sizeof(*addr));
1234 addr->ai_flags = ai->ai_flags;
1235 addr->ai_family = ai->ai_family;
1236 addr->ai_socktype = ai->ai_socktype;
1237 addr->ai_protocol = ai->ai_protocol;
1238 addr->slen = ai->ai_addrlen;
1239 memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
1241 /* for commodity */
1242 switch (addr->ai_family) {
1243 case AF_INET:
1244 sin = (struct sockaddr_in *)&addr->ss;
1245 addr->port = ntohs(sin->sin_port);
1246 break;
1247 case AF_INET6:
1248 sin6 = (struct sockaddr_in6 *)&addr->ss;
1249 addr->port = ntohs(sin6->sin6_port);
1250 break;
1251 default:
1252 fatalx("unknown socket family %d", addr->ai_family);
1255 addr->sock = -1;
1257 TAILQ_INSERT_HEAD(a, addr, addrs);
1260 void
1261 listen_on(const char *hostname, const char *servname)
1263 struct addrinfo hints, *res, *res0;
1264 int error;
1266 memset(&hints, 0, sizeof(hints));
1267 hints.ai_family = AF_UNSPEC;
1268 hints.ai_socktype = SOCK_STREAM;
1269 hints.ai_flags = AI_PASSIVE;
1270 error = getaddrinfo(hostname, servname, &hints, &res0);
1271 if (error) {
1272 yyerror("listen on \"%s\" port %s: %s", hostname, servname,
1273 gai_strerror(errno));
1274 return;
1277 for (res = res0; res; res = res->ai_next) {
1278 add_to_addr_queue(&host->addrs, res);
1279 add_to_addr_queue(&conf->addrs, res);
1282 freeaddrinfo(res0);