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 SOCKET 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 yywarn("the top-level `param' directive is deprecated."
330 " Please use `fastcgi { param ... }`");
331 add_param($2, $4);
333 | LISTEN ON listen_addr {
334 listen_on($3, "1965");
336 | LISTEN ON listen_addr PORT STRING {
337 listen_on($3, $5);
338 free($3);
339 free($5);
341 | LISTEN ON listen_addr PORT NUM {
342 char portno[32];
343 int r;
345 r = snprintf(portno, sizeof(portno), "%d", $5);
346 if (r < 0 || (size_t)r >= sizeof(portno))
347 fatal("snprintf");
349 listen_on($3, portno);
350 free($3);
352 | locopt
355 proxy : PROXY { advance_proxy(); }
356 proxy_matches '{' optnl proxy_opts '}' {
357 if (*proxy->host == '\0')
358 yyerror("invalid proxy block: missing `relay-to' option");
360 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
361 (proxy->cert_path != NULL && proxy->key_path == NULL))
362 yyerror("invalid proxy block: missing cert or key");
366 proxy_matches : /* empty */
367 | proxy_matches proxy_match
370 proxy_port : /* empty */ { $$ = 1965; }
371 | PORT STRING {
372 if (($$ = getservice($2)) == -1)
373 yyerror("invalid port number %s", $2);
374 free($2);
376 | PORT NUM { $$ = $2; }
379 proxy_match : PROTO string {
380 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
381 free($2);
383 | FOR_HOST string proxy_port {
384 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
385 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
386 "%d", $3);
387 free($2);
391 proxy_opts : /* empty */
392 | proxy_opts proxy_opt optnl
395 proxy_opt : CERT string {
396 free(proxy->cert);
397 ensure_absolute_path($2);
398 proxy->cert_path = $2;
400 | KEY string {
401 free(proxy->key);
402 ensure_absolute_path($2);
403 proxy->key_path = $2;
405 | PROTOCOLS string {
406 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
407 yyerror("invalid protocols string \"%s\"", $2);
408 free($2);
410 | RELAY_TO string proxy_port {
411 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
412 (void) snprintf(proxy->port, sizeof(proxy->port),
413 "%d", $3);
414 free($2);
416 | REQUIRE CLIENT CA string {
417 ensure_absolute_path($4);
418 proxy->reqca_path = $4;
420 | SNI string {
421 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
422 free($2);
424 | USE_TLS bool {
425 proxy->notls = !$2;
427 | VERIFYNAME bool {
428 proxy->noverifyname = !$2;
432 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
433 /* drop the starting '/' if any */
434 if (*$3 == '/')
435 memmove($3, $3+1, strlen($3));
436 (void) strlcpy(loc->match, $3, sizeof(loc->match));
437 free($3);
439 | error '}'
442 locopts : /* empty */
443 | locopts locopt optnl
446 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
447 | BLOCK RETURN NUM string {
448 check_block_fmt($4);
449 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
450 loc->block_code = check_block_code($3);
451 free($4);
453 | BLOCK RETURN NUM {
454 (void) strlcpy(loc->block_fmt, "temporary failure",
455 sizeof(loc->block_fmt));
456 loc->block_code = check_block_code($3);
457 if ($3 >= 30 && $3 < 40)
458 yyerror("missing `meta' for block return %d", $3);
460 | BLOCK {
461 (void) strlcpy(loc->block_fmt, "temporary failure",
462 sizeof(loc->block_fmt));
463 loc->block_code = 40;
465 | DEFAULT TYPE string {
466 (void) strlcpy(loc->default_mime, $3,
467 sizeof(loc->default_mime));
468 free($3);
470 | fastcgi
471 | INDEX string {
472 (void) strlcpy(loc->index, $2, sizeof(loc->index));
473 free($2);
475 | LANG string {
476 (void) strlcpy(loc->lang, $2,
477 sizeof(loc->lang));
478 free($2);
480 | LOG bool { loc->disable_log = !$2; }
481 | REQUIRE CLIENT CA string {
482 ensure_absolute_path($4);
483 loc->reqca_path = $4;
485 | ROOT string {
486 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
487 free($2);
489 | STRIP NUM { loc->strip = check_strip_no($2); }
492 fastcgi : FASTCGI '{' optnl fastcgiopts '}'
493 | FASTCGI fastcgiopt
494 | FASTCGI string {
495 yywarn("`fastcgi path' is deprecated. "
496 "Please use `fastcgi socket path' instead.");
497 loc->fcgi = fastcgi_conf($2, NULL);
498 free($2);
500 | error '}'
503 fastcgiopts : /* empty */
504 | fastcgiopts fastcgiopt optnl
507 fastcgiopt : PARAM string '=' string {
508 add_param($2, $4);
510 | SOCKET string {
511 loc->fcgi = fastcgi_conf($2, NULL);
512 free($2);
514 | SOCKET TCP string PORT NUM {
515 char *c;
517 if (asprintf(&c, "%d", $5) == -1)
518 fatal("asprintf");
519 loc->fcgi = fastcgi_conf($3, c);
520 free($3);
521 free(c);
523 | SOCKET TCP string {
524 loc->fcgi = fastcgi_conf($3, "9000");
526 | SOCKET TCP string PORT string {
527 loc->fcgi = fastcgi_conf($3, $5);
528 free($3);
529 free($5);
533 types : TYPES '{' optnl mediaopts_l '}' ;
535 mediaopts_l : mediaopts_l mediaoptsl nl
536 | mediaoptsl nl
539 mediaoptsl : STRING {
540 free(current_media);
541 current_media = $1;
542 } medianames_l optsemicolon
543 | include
546 medianames_l : medianames_l medianamesl
547 | medianamesl
550 medianamesl : numberstring {
551 if (add_mime(&conf->mime, current_media, $1) == -1)
552 fatal("add_mime");
553 free($1);
557 nl : '\n' optnl
560 optnl : '\n' optnl /* zero or more newlines */
561 | ';' optnl /* semicolons too */
562 | /*empty*/
565 optsemicolon : ';'
569 %%
571 static const struct keyword {
572 const char *word;
573 int token;
574 } keywords[] = {
575 /* these MUST be sorted */
576 {"alias", ALIAS},
577 {"auto", AUTO},
578 {"block", BLOCK},
579 {"ca", CA},
580 {"cert", CERT},
581 {"chroot", CHROOT},
582 {"client", CLIENT},
583 {"default", DEFAULT},
584 {"fastcgi", FASTCGI},
585 {"for-host", FOR_HOST},
586 {"include", INCLUDE},
587 {"index", INDEX},
588 {"ipv6", IPV6},
589 {"key", KEY},
590 {"lang", LANG},
591 {"listen", LISTEN},
592 {"location", LOCATION},
593 {"log", LOG},
594 {"ocsp", OCSP},
595 {"off", OFF},
596 {"on", ON},
597 {"param", PARAM},
598 {"port", PORT},
599 {"prefork", PREFORK},
600 {"proto", PROTO},
601 {"protocols", PROTOCOLS},
602 {"proxy", PROXY},
603 {"relay-to", RELAY_TO},
604 {"require", REQUIRE},
605 {"return", RETURN},
606 {"root", ROOT},
607 {"server", SERVER},
608 {"sni", SNI},
609 {"socket", SOCKET},
610 {"strip", STRIP},
611 {"tcp", TCP},
612 {"to-ext", TOEXT},
613 {"type", TYPE},
614 {"types", TYPES},
615 {"use-tls", USE_TLS},
616 {"user", USER},
617 {"verifyname", VERIFYNAME},
618 };
620 void
621 yyerror(const char *msg, ...)
623 va_list ap;
625 file->errors++;
627 va_start(ap, msg);
628 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
629 vfprintf(stderr, msg, ap);
630 fprintf(stderr, "\n");
631 va_end(ap);
634 void
635 yywarn(const char *msg, ...)
637 va_list ap;
639 va_start(ap, msg);
640 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
641 vfprintf(stderr, msg, ap);
642 fprintf(stderr, "\n");
643 va_end(ap);
646 int
647 kw_cmp(const void *k, const void *e)
649 return strcmp(k, ((struct keyword *)e)->word);
652 int
653 lookup(char *s)
655 const struct keyword *p;
657 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
658 sizeof(keywords[0]), kw_cmp);
660 if (p)
661 return p->token;
662 else
663 return STRING;
666 #define START_EXPAND 1
667 #define DONE_EXPAND 2
669 static int expanding;
671 int
672 igetc(void)
674 int c;
676 while (1) {
677 if (file->ungetpos > 0)
678 c = file->ungetbuf[--file->ungetpos];
679 else
680 c = getc(file->stream);
682 if (c == START_EXPAND)
683 expanding = 1;
684 else if (c == DONE_EXPAND)
685 expanding = 0;
686 else
687 break;
689 return c;
692 int
693 lgetc(int quotec)
695 int c, next;
697 if (quotec) {
698 if ((c = igetc()) == EOF) {
699 yyerror("reached end of file while parsing "
700 "quoted string");
701 if (file == topfile || popfile() == EOF)
702 return EOF;
703 return quotec;
705 return c;
708 while ((c = igetc()) == '\\') {
709 next = igetc();
710 if (next != '\n') {
711 c = next;
712 break;
714 yylval.lineno = file->lineno;
715 file->lineno++;
718 if (c == EOF) {
719 /*
720 * Fake EOL when hit EOF for the first time. This gets line
721 * count right if last line in included file is syntactically
722 * invalid and has no newline.
723 */
724 if (file->eof_reached == 0) {
725 file->eof_reached = 1;
726 return '\n';
728 while (c == EOF) {
729 if (file == topfile || popfile() == EOF)
730 return EOF;
731 c = igetc();
734 return c;
737 void
738 lungetc(int c)
740 if (c == EOF)
741 return;
743 if (file->ungetpos >= file->ungetsize) {
744 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
745 if (p == NULL)
746 fatal("lungetc");
747 file->ungetbuf = p;
748 file->ungetsize *= 2;
750 file->ungetbuf[file->ungetpos++] = c;
753 int
754 findeol(void)
756 int c;
758 /* Skip to either EOF or the first real EOL. */
759 while (1) {
760 c = lgetc(0);
761 if (c == '\n') {
762 file->lineno++;
763 break;
765 if (c == EOF)
766 break;
768 return ERROR;
771 int
772 yylex(void)
774 char buf[8096];
775 char *p, *val;
776 int quotec, next, c;
777 int token;
779 top:
780 p = buf;
781 while ((c = lgetc(0)) == ' ' || c == '\t')
782 ; /* nothing */
784 yylval.lineno = file->lineno;
785 if (c == '#')
786 while ((c = lgetc(0)) != '\n' && c != EOF)
787 ; /* nothing */
788 if (c == '$' && !expanding) {
789 while (1) {
790 if ((c = lgetc(0)) == EOF)
791 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 yylval.v.string = xstrdup(val);
810 return STRING;
812 if (c == '@' && !expanding) {
813 while (1) {
814 if ((c = lgetc(0)) == EOF)
815 return 0;
817 if (p + 1 >= buf + sizeof(buf) - 1) {
818 yyerror("string too long");
819 return findeol();
821 if (isalnum(c) || c == '_') {
822 *p++ = c;
823 continue;
825 *p = '\0';
826 lungetc(c);
827 break;
829 val = symget(buf);
830 if (val == NULL) {
831 yyerror("macro '%s' not defined", buf);
832 return findeol();
834 p = val + strlen(val) - 1;
835 lungetc(DONE_EXPAND);
836 while (p >= val) {
837 lungetc(*p);
838 p--;
840 lungetc(START_EXPAND);
841 goto top;
844 switch (c) {
845 case '\'':
846 case '"':
847 quotec = c;
848 while (1) {
849 if ((c = lgetc(quotec)) == EOF)
850 return 0;
851 if (c == '\n') {
852 file->lineno++;
853 continue;
854 } else if (c == '\\') {
855 if ((next = lgetc(quotec)) == EOF)
856 return (0);
857 if (next == quotec || next == ' ' ||
858 next == '\t')
859 c = next;
860 else if (next == '\n') {
861 file->lineno++;
862 continue;
863 } else
864 lungetc(next);
865 } else if (c == quotec) {
866 *p = '\0';
867 break;
868 } else if (c == '\0') {
869 yyerror("invalid syntax");
870 return findeol();
872 if (p + 1 >= buf + sizeof(buf) - 1) {
873 yyerror("string too long");
874 return findeol();
876 *p++ = c;
878 yylval.v.string = strdup(buf);
879 if (yylval.v.string == NULL)
880 fatal("yylex: strdup");
881 return STRING;
884 #define allowed_to_end_number(x) \
885 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
887 if (c == '-' || isdigit(c)) {
888 do {
889 *p++ = c;
890 if ((size_t)(p-buf) >= sizeof(buf)) {
891 yyerror("string too long");
892 return findeol();
894 } while ((c = lgetc(0)) != EOF && isdigit(c));
895 lungetc(c);
896 if (p == buf + 1 && buf[0] == '-')
897 goto nodigits;
898 if (c == EOF || allowed_to_end_number(c)) {
899 const char *errstr = NULL;
901 *p = '\0';
902 yylval.v.number = strtonum(buf, LLONG_MIN,
903 LLONG_MAX, &errstr);
904 if (errstr) {
905 yyerror("\"%s\" invalid number: %s",
906 buf, errstr);
907 return findeol();
909 return NUM;
910 } else {
911 nodigits:
912 while (p > buf + 1)
913 lungetc(*--p);
914 c = *--p;
915 if (c == '-')
916 return c;
920 #define allowed_in_string(x) \
921 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
922 x != '{' && x != '}' && \
923 x != '!' && x != '=' && x != '#' && \
924 x != ',' && x != ';'))
926 if (isalnum(c) || c == ':' || c == '_') {
927 do {
928 *p++ = c;
929 if ((size_t)(p-buf) >= sizeof(buf)) {
930 yyerror("string too long");
931 return findeol();
933 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
934 lungetc(c);
935 *p = '\0';
936 if ((token = lookup(buf)) == STRING)
937 yylval.v.string = xstrdup(buf);
938 return token;
940 if (c == '\n') {
941 yylval.lineno = file->lineno;
942 file->lineno++;
944 if (c == EOF)
945 return 0;
946 return c;
949 struct file *
950 pushfile(const char *name, int secret)
952 struct file *nfile;
954 nfile = xcalloc(1, sizeof(*nfile));
955 nfile->name = xstrdup(name);
956 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
957 log_warn("can't open %s", nfile->name);
958 free(nfile->name);
959 free(nfile);
960 return NULL;
962 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
963 nfile->ungetsize = 16;
964 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
965 TAILQ_INSERT_TAIL(&files, nfile, entry);
966 return nfile;
969 int
970 popfile(void)
972 struct file *prev;
974 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
975 prev->errors += file->errors;
977 TAILQ_REMOVE(&files, file, entry);
978 fclose(file->stream);
979 free(file->name);
980 free(file->ungetbuf);
981 free(file);
982 file = prev;
983 return file ? 0 : EOF;
986 int
987 parse_conf(struct conf *c, const char *filename)
989 struct sym *sym, *next;
991 default_host = "*";
992 default_port = 1965;
994 conf = c;
996 file = pushfile(filename, 0);
997 if (file == NULL)
998 return -1;
999 topfile = file;
1001 yyparse();
1002 errors = file->errors;
1003 popfile();
1005 /* Free macros and check which have not been used. */
1006 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1007 /* TODO: warn if !sym->used */
1008 if (!sym->persist) {
1009 free(sym->name);
1010 free(sym->val);
1011 TAILQ_REMOVE(&symhead, sym, entry);
1012 free(sym);
1016 if (errors)
1017 return -1;
1018 return 0;
1021 int
1022 symset(const char *name, const char *val, int persist)
1024 struct sym *sym;
1026 TAILQ_FOREACH(sym, &symhead, entry) {
1027 if (!strcmp(name, sym->name))
1028 break;
1031 if (sym != NULL) {
1032 if (sym->persist)
1033 return 0;
1034 else {
1035 free(sym->name);
1036 free(sym->val);
1037 TAILQ_REMOVE(&symhead, sym, entry);
1038 free(sym);
1042 sym = xcalloc(1, sizeof(*sym));
1043 sym->name = xstrdup(name);
1044 sym->val = xstrdup(val);
1045 sym->used = 0;
1046 sym->persist = persist;
1048 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1049 return 0;
1052 int
1053 cmdline_symset(char *s)
1055 char *sym, *val;
1056 int ret;
1058 if ((val = strrchr(s, '=')) == NULL)
1059 return -1;
1060 sym = xcalloc(1, val - s + 1);
1061 memcpy(sym, s, val - s);
1062 ret = symset(sym, val + 1, 1);
1063 free(sym);
1064 return ret;
1067 char *
1068 symget(const char *nam)
1070 struct sym *sym;
1072 TAILQ_FOREACH(sym, &symhead, entry) {
1073 if (strcmp(nam, sym->name) == 0) {
1074 sym->used = 1;
1075 return sym->val;
1078 return NULL;
1081 char *
1082 ensure_absolute_path(char *path)
1084 if (path == NULL || *path != '/')
1085 yyerror("not an absolute path: %s", path);
1086 return path;
1089 int
1090 check_block_code(int n)
1092 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1093 yyerror("invalid block code %d", n);
1094 return n;
1097 char *
1098 check_block_fmt(char *fmt)
1100 char *s;
1102 for (s = fmt; *s; ++s) {
1103 if (*s != '%')
1104 continue;
1105 switch (*++s) {
1106 case '%':
1107 case 'p':
1108 case 'q':
1109 case 'P':
1110 case 'N':
1111 break;
1112 default:
1113 yyerror("invalid format specifier %%%c", *s);
1117 return fmt;
1120 int
1121 check_strip_no(int n)
1123 if (n <= 0)
1124 yyerror("invalid strip number %d", n);
1125 return n;
1128 int
1129 check_port_num(int n)
1131 if (n <= 0 || n >= UINT16_MAX)
1132 yyerror("port number is %s: %d",
1133 n <= 0 ? "too small" : "too large",
1134 n);
1135 return n;
1138 int
1139 check_prefork_num(int n)
1141 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1142 yyerror("invalid prefork number %d", n);
1143 return n;
1146 void
1147 advance_loc(void)
1149 loc = new_location();
1150 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1153 void
1154 advance_proxy(void)
1156 proxy = new_proxy();
1157 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1160 void
1161 parsehp(char *str, char **host, const char **port, const char *def)
1163 char *at;
1164 const char *errstr;
1166 *host = str;
1168 if ((at = strchr(str, ':')) != NULL) {
1169 *at++ = '\0';
1170 *port = at;
1171 } else
1172 *port = def;
1174 strtonum(*port, 1, UINT16_MAX, &errstr);
1175 if (errstr != NULL)
1176 yyerror("port is %s: %s", errstr, *port);
1179 int
1180 fastcgi_conf(const char *path, const char *port)
1182 struct fcgi *f;
1183 int i = 0;
1185 TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1186 if (!strcmp(f->path, path) &&
1187 ((port == NULL && *f->port == '\0') ||
1188 !strcmp(f->port, port)))
1189 return i;
1190 ++i;
1193 f = xcalloc(1, sizeof(*f));
1194 f->id = i;
1195 (void)strlcpy(f->path, path, sizeof(f->path));
1196 if (port != NULL)
1197 (void)strlcpy(f->port, port, sizeof(f->port));
1198 TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1200 return f->id;
1203 void
1204 add_param(char *name, char *val)
1206 struct envlist *e;
1207 struct envhead *h = &loc->params;
1209 e = xcalloc(1, sizeof(*e));
1210 (void) strlcpy(e->name, name, sizeof(e->name));
1211 (void) strlcpy(e->value, val, sizeof(e->value));
1212 TAILQ_INSERT_TAIL(h, e, envs);
1215 int
1216 getservice(const char *n)
1218 struct servent *s;
1219 const char *errstr;
1220 long long llval;
1222 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1223 if (errstr) {
1224 s = getservbyname(n, "tcp");
1225 if (s == NULL)
1226 s = getservbyname(n, "udp");
1227 if (s == NULL)
1228 return (-1);
1229 return (ntohs(s->s_port));
1232 return ((unsigned short)llval);
1235 static void
1236 add_to_addr_queue(struct addrhead *a, struct addrinfo *ai)
1238 struct address *addr;
1239 struct sockaddr_in *sin;
1240 struct sockaddr_in6 *sin6;
1242 if (ai->ai_addrlen > sizeof(addr->ss))
1243 fatalx("ai_addrlen larger than a sockaddr_storage");
1245 TAILQ_FOREACH(addr, a, addrs) {
1246 if (addr->ai_flags == ai->ai_flags &&
1247 addr->ai_family == ai->ai_family &&
1248 addr->ai_socktype == ai->ai_socktype &&
1249 addr->ai_protocol == ai->ai_protocol &&
1250 addr->slen == ai->ai_addrlen &&
1251 !memcmp(&addr->ss, ai->ai_addr, addr->slen))
1252 return;
1255 addr = xcalloc(1, sizeof(*addr));
1256 addr->ai_flags = ai->ai_flags;
1257 addr->ai_family = ai->ai_family;
1258 addr->ai_socktype = ai->ai_socktype;
1259 addr->ai_protocol = ai->ai_protocol;
1260 addr->slen = ai->ai_addrlen;
1261 memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
1263 /* for commodity */
1264 switch (addr->ai_family) {
1265 case AF_INET:
1266 sin = (struct sockaddr_in *)&addr->ss;
1267 addr->port = ntohs(sin->sin_port);
1268 break;
1269 case AF_INET6:
1270 sin6 = (struct sockaddr_in6 *)&addr->ss;
1271 addr->port = ntohs(sin6->sin6_port);
1272 break;
1273 default:
1274 fatalx("unknown socket family %d", addr->ai_family);
1277 addr->sock = -1;
1279 TAILQ_INSERT_HEAD(a, addr, addrs);
1282 void
1283 listen_on(const char *hostname, const char *servname)
1285 struct addrinfo hints, *res, *res0;
1286 int error;
1288 memset(&hints, 0, sizeof(hints));
1289 hints.ai_family = AF_UNSPEC;
1290 hints.ai_socktype = SOCK_STREAM;
1291 hints.ai_flags = AI_PASSIVE;
1292 error = getaddrinfo(hostname, servname, &hints, &res0);
1293 if (error) {
1294 yyerror("listen on \"%s\" port %s: %s", hostname, servname,
1295 gai_strerror(errno));
1296 return;
1299 for (res = res0; res; res = res->ai_next) {
1300 add_to_addr_queue(&host->addrs, res);
1301 add_to_addr_queue(&conf->addrs, res);
1304 freeaddrinfo(res0);