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 ACCESS 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 SYSLOG
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 | log
236 | PORT NUM {
237 yywarn("option `port' is deprecated,"
238 " please use `listen on'");
239 default_port = $2;
241 | PREFORK NUM { conf->prefork = check_prefork_num($2); }
242 | PROTOCOLS string {
243 if (tls_config_parse_protocols(&conf->protos, $2) == -1)
244 yyerror("invalid protocols string \"%s\"", $2);
245 free($2);
247 | USER string {
248 if (strlcpy(conf->user, $2, sizeof(conf->user)) >=
249 sizeof(conf->user))
250 yyerror("user name too long");
251 free($2);
255 log : LOG '{' optnl logopts '}'
256 | LOG logopt
259 logopts : /* empty */
260 | logopts logopt optnl
263 logopt : SYSLOG {
264 free(conf->log_access);
265 conf->log_access = NULL;
267 | ACCESS string {
268 free(conf->log_access);
269 conf->log_access = $2;
273 vhost : SERVER string {
274 host = new_vhost();
275 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
277 loc = new_location();
278 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
280 TAILQ_INIT(&host->proxies);
282 (void) strlcpy(loc->match, "*", sizeof(loc->match));
283 (void) strlcpy(host->domain, $2, sizeof(host->domain));
285 if (strstr($2, "xn--") != NULL) {
286 yywarn("\"%s\" looks like punycode: you "
287 "should use the decoded hostname", $2);
290 free($2);
291 } '{' optnl servbody '}' {
292 if (host->cert_path == NULL ||
293 host->key_path == NULL)
294 yyerror("invalid vhost definition: %s",
295 host->domain);
296 if (TAILQ_EMPTY(&host->addrs)) {
297 char portno[32];
298 int r;
300 r = snprintf(portno, sizeof(portno), "%d",
301 default_port);
302 if (r < 0 || (size_t)r >= sizeof(portno))
303 fatal("snprintf");
305 yywarn("missing `listen on' in server %s,"
306 " assuming %s port %d", $2, default_host,
307 default_port);
308 listen_on(default_host, portno);
311 | error '}' { yyerror("bad server directive"); }
314 servbody : /* empty */
315 | servbody servopt optnl
316 | servbody location optnl
317 | servbody proxy optnl
320 listen_addr : '*' { $$ = NULL; }
321 | STRING
324 servopt : ALIAS string {
325 struct alist *a;
327 a = xcalloc(1, sizeof(*a));
328 (void) strlcpy(a->alias, $2, sizeof(a->alias));
329 free($2);
330 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
332 | CERT string {
333 ensure_absolute_path($2);
334 free(host->cert_path);
335 host->cert_path = $2;
337 | KEY string {
338 ensure_absolute_path($2);
339 free(host->key_path);
340 host->key_path = $2;
342 | OCSP string {
343 ensure_absolute_path($2);
344 free(host->ocsp_path);
345 host->ocsp_path = $2;
347 | PARAM string '=' string {
348 yywarn("the top-level `param' directive is deprecated."
349 " Please use `fastcgi { param ... }`");
350 add_param($2, $4);
352 | LISTEN ON listen_addr {
353 listen_on($3, "1965");
355 | LISTEN ON listen_addr PORT STRING {
356 listen_on($3, $5);
357 free($3);
358 free($5);
360 | LISTEN ON listen_addr PORT NUM {
361 char portno[32];
362 int r;
364 r = snprintf(portno, sizeof(portno), "%d", $5);
365 if (r < 0 || (size_t)r >= sizeof(portno))
366 fatal("snprintf");
368 listen_on($3, portno);
369 free($3);
371 | locopt
374 proxy : PROXY { advance_proxy(); }
375 proxy_matches '{' optnl proxy_opts '}' {
376 if (*proxy->host == '\0')
377 yyerror("invalid proxy block: missing `relay-to' option");
379 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
380 (proxy->cert_path != NULL && proxy->key_path == NULL))
381 yyerror("invalid proxy block: missing cert or key");
385 proxy_matches : /* empty */
386 | proxy_matches proxy_match
389 proxy_port : /* empty */ { $$ = 1965; }
390 | PORT STRING {
391 if (($$ = getservice($2)) == -1)
392 yyerror("invalid port number %s", $2);
393 free($2);
395 | PORT NUM { $$ = $2; }
398 proxy_match : PROTO string {
399 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
400 free($2);
402 | FOR_HOST string proxy_port {
403 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
404 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
405 "%d", $3);
406 free($2);
410 proxy_opts : /* empty */
411 | proxy_opts proxy_opt optnl
414 proxy_opt : CERT string {
415 free(proxy->cert);
416 ensure_absolute_path($2);
417 proxy->cert_path = $2;
419 | KEY string {
420 free(proxy->key);
421 ensure_absolute_path($2);
422 proxy->key_path = $2;
424 | PROTOCOLS string {
425 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
426 yyerror("invalid protocols string \"%s\"", $2);
427 free($2);
429 | RELAY_TO string proxy_port {
430 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
431 (void) snprintf(proxy->port, sizeof(proxy->port),
432 "%d", $3);
433 free($2);
435 | REQUIRE CLIENT CA string {
436 ensure_absolute_path($4);
437 proxy->reqca_path = $4;
439 | SNI string {
440 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
441 free($2);
443 | USE_TLS bool {
444 proxy->notls = !$2;
446 | VERIFYNAME bool {
447 proxy->noverifyname = !$2;
451 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
452 /* drop the starting '/' if any */
453 if (*$3 == '/')
454 memmove($3, $3+1, strlen($3));
455 (void) strlcpy(loc->match, $3, sizeof(loc->match));
456 free($3);
458 | error '}'
461 locopts : /* empty */
462 | locopts locopt optnl
465 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
466 | BLOCK RETURN NUM string {
467 check_block_fmt($4);
468 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
469 loc->block_code = check_block_code($3);
470 free($4);
472 | BLOCK RETURN NUM {
473 (void) strlcpy(loc->block_fmt, "temporary failure",
474 sizeof(loc->block_fmt));
475 loc->block_code = check_block_code($3);
476 if ($3 >= 30 && $3 < 40)
477 yyerror("missing `meta' for block return %d", $3);
479 | BLOCK {
480 (void) strlcpy(loc->block_fmt, "temporary failure",
481 sizeof(loc->block_fmt));
482 loc->block_code = 40;
484 | DEFAULT TYPE string {
485 (void) strlcpy(loc->default_mime, $3,
486 sizeof(loc->default_mime));
487 free($3);
489 | fastcgi
490 | INDEX string {
491 (void) strlcpy(loc->index, $2, sizeof(loc->index));
492 free($2);
494 | LANG string {
495 (void) strlcpy(loc->lang, $2,
496 sizeof(loc->lang));
497 free($2);
499 | LOG bool { loc->disable_log = !$2; }
500 | REQUIRE CLIENT CA string {
501 ensure_absolute_path($4);
502 loc->reqca_path = $4;
504 | ROOT string {
505 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
506 free($2);
508 | STRIP NUM { loc->strip = check_strip_no($2); }
511 fastcgi : FASTCGI '{' optnl fastcgiopts '}'
512 | FASTCGI fastcgiopt
513 | FASTCGI OFF {
514 loc->fcgi = -1;
515 loc->nofcgi = 1;
517 | FASTCGI string {
518 yywarn("`fastcgi path' is deprecated. "
519 "Please use `fastcgi socket path' instead.");
520 loc->fcgi = fastcgi_conf($2, NULL);
521 free($2);
525 fastcgiopts : /* empty */
526 | fastcgiopts fastcgiopt optnl
529 fastcgiopt : PARAM string '=' string {
530 add_param($2, $4);
532 | SOCKET string {
533 loc->fcgi = fastcgi_conf($2, NULL);
534 free($2);
536 | SOCKET TCP string PORT NUM {
537 char *c;
539 if (asprintf(&c, "%d", $5) == -1)
540 fatal("asprintf");
541 loc->fcgi = fastcgi_conf($3, c);
542 free($3);
543 free(c);
545 | SOCKET TCP string {
546 loc->fcgi = fastcgi_conf($3, "9000");
548 | SOCKET TCP string PORT string {
549 loc->fcgi = fastcgi_conf($3, $5);
550 free($3);
551 free($5);
555 types : TYPES '{' optnl mediaopts_l '}' ;
557 mediaopts_l : mediaopts_l mediaoptsl nl
558 | mediaoptsl nl
561 mediaoptsl : STRING {
562 free(current_media);
563 current_media = $1;
564 } medianames_l optsemicolon
565 | include
568 medianames_l : medianames_l medianamesl
569 | medianamesl
572 medianamesl : numberstring {
573 if (add_mime(&conf->mime, current_media, $1) == -1)
574 fatal("add_mime");
575 free($1);
579 nl : '\n' optnl
582 optnl : '\n' optnl /* zero or more newlines */
583 | ';' optnl /* semicolons too */
584 | /*empty*/
587 optsemicolon : ';'
591 %%
593 static const struct keyword {
594 const char *word;
595 int token;
596 } keywords[] = {
597 /* these MUST be sorted */
598 {"access", ACCESS},
599 {"alias", ALIAS},
600 {"auto", AUTO},
601 {"block", BLOCK},
602 {"ca", CA},
603 {"cert", CERT},
604 {"chroot", CHROOT},
605 {"client", CLIENT},
606 {"default", DEFAULT},
607 {"fastcgi", FASTCGI},
608 {"for-host", FOR_HOST},
609 {"include", INCLUDE},
610 {"index", INDEX},
611 {"ipv6", IPV6},
612 {"key", KEY},
613 {"lang", LANG},
614 {"listen", LISTEN},
615 {"location", LOCATION},
616 {"log", LOG},
617 {"ocsp", OCSP},
618 {"off", OFF},
619 {"on", ON},
620 {"param", PARAM},
621 {"port", PORT},
622 {"prefork", PREFORK},
623 {"proto", PROTO},
624 {"protocols", PROTOCOLS},
625 {"proxy", PROXY},
626 {"relay-to", RELAY_TO},
627 {"require", REQUIRE},
628 {"return", RETURN},
629 {"root", ROOT},
630 {"server", SERVER},
631 {"sni", SNI},
632 {"socket", SOCKET},
633 {"strip", STRIP},
634 {"syslog", SYSLOG},
635 {"tcp", TCP},
636 {"to-ext", TOEXT},
637 {"type", TYPE},
638 {"types", TYPES},
639 {"use-tls", USE_TLS},
640 {"user", USER},
641 {"verifyname", VERIFYNAME},
642 };
644 void
645 yyerror(const char *msg, ...)
647 va_list ap;
649 file->errors++;
651 va_start(ap, msg);
652 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
653 vfprintf(stderr, msg, ap);
654 fprintf(stderr, "\n");
655 va_end(ap);
658 void
659 yywarn(const char *msg, ...)
661 va_list ap;
663 va_start(ap, msg);
664 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
665 vfprintf(stderr, msg, ap);
666 fprintf(stderr, "\n");
667 va_end(ap);
670 int
671 kw_cmp(const void *k, const void *e)
673 return strcmp(k, ((struct keyword *)e)->word);
676 int
677 lookup(char *s)
679 const struct keyword *p;
681 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
682 sizeof(keywords[0]), kw_cmp);
684 if (p)
685 return p->token;
686 else
687 return STRING;
690 #define START_EXPAND 1
691 #define DONE_EXPAND 2
693 static int expanding;
695 int
696 igetc(void)
698 int c;
700 while (1) {
701 if (file->ungetpos > 0)
702 c = file->ungetbuf[--file->ungetpos];
703 else
704 c = getc(file->stream);
706 if (c == START_EXPAND)
707 expanding = 1;
708 else if (c == DONE_EXPAND)
709 expanding = 0;
710 else
711 break;
713 return c;
716 int
717 lgetc(int quotec)
719 int c, next;
721 if (quotec) {
722 if ((c = igetc()) == EOF) {
723 yyerror("reached end of file while parsing "
724 "quoted string");
725 if (file == topfile || popfile() == EOF)
726 return EOF;
727 return quotec;
729 return c;
732 while ((c = igetc()) == '\\') {
733 next = igetc();
734 if (next != '\n') {
735 c = next;
736 break;
738 yylval.lineno = file->lineno;
739 file->lineno++;
742 if (c == EOF) {
743 /*
744 * Fake EOL when hit EOF for the first time. This gets line
745 * count right if last line in included file is syntactically
746 * invalid and has no newline.
747 */
748 if (file->eof_reached == 0) {
749 file->eof_reached = 1;
750 return '\n';
752 while (c == EOF) {
753 if (file == topfile || popfile() == EOF)
754 return EOF;
755 c = igetc();
758 return c;
761 void
762 lungetc(int c)
764 if (c == EOF)
765 return;
767 if (file->ungetpos >= file->ungetsize) {
768 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
769 if (p == NULL)
770 fatal("lungetc");
771 file->ungetbuf = p;
772 file->ungetsize *= 2;
774 file->ungetbuf[file->ungetpos++] = c;
777 int
778 findeol(void)
780 int c;
782 /* Skip to either EOF or the first real EOL. */
783 while (1) {
784 c = lgetc(0);
785 if (c == '\n') {
786 file->lineno++;
787 break;
789 if (c == EOF)
790 break;
792 return ERROR;
795 int
796 yylex(void)
798 char buf[8096];
799 char *p, *val;
800 int quotec, next, c;
801 int token;
803 top:
804 p = buf;
805 while ((c = lgetc(0)) == ' ' || c == '\t')
806 ; /* nothing */
808 yylval.lineno = file->lineno;
809 if (c == '#')
810 while ((c = lgetc(0)) != '\n' && c != EOF)
811 ; /* nothing */
812 if (c == '$' && !expanding) {
813 while (1) {
814 if ((c = lgetc(0)) == EOF)
815 return 0;
816 if (p + 1 >= buf + sizeof(buf) -1) {
817 yyerror("string too long");
818 return findeol();
820 if (isalnum(c) || c == '_') {
821 *p++ = c;
822 continue;
824 *p = '\0';
825 lungetc(c);
826 break;
828 val = symget(buf);
829 if (val == NULL) {
830 yyerror("macro `%s' not defined", buf);
831 return findeol();
833 yylval.v.string = xstrdup(val);
834 return STRING;
836 if (c == '@' && !expanding) {
837 while (1) {
838 if ((c = lgetc(0)) == EOF)
839 return 0;
841 if (p + 1 >= buf + sizeof(buf) - 1) {
842 yyerror("string too long");
843 return findeol();
845 if (isalnum(c) || c == '_') {
846 *p++ = c;
847 continue;
849 *p = '\0';
850 lungetc(c);
851 break;
853 val = symget(buf);
854 if (val == NULL) {
855 yyerror("macro '%s' not defined", buf);
856 return findeol();
858 p = val + strlen(val) - 1;
859 lungetc(DONE_EXPAND);
860 while (p >= val) {
861 lungetc(*p);
862 p--;
864 lungetc(START_EXPAND);
865 goto top;
868 switch (c) {
869 case '\'':
870 case '"':
871 quotec = c;
872 while (1) {
873 if ((c = lgetc(quotec)) == EOF)
874 return 0;
875 if (c == '\n') {
876 file->lineno++;
877 continue;
878 } else if (c == '\\') {
879 if ((next = lgetc(quotec)) == EOF)
880 return (0);
881 if (next == quotec || next == ' ' ||
882 next == '\t')
883 c = next;
884 else if (next == '\n') {
885 file->lineno++;
886 continue;
887 } else
888 lungetc(next);
889 } else if (c == quotec) {
890 *p = '\0';
891 break;
892 } else if (c == '\0') {
893 yyerror("invalid syntax");
894 return findeol();
896 if (p + 1 >= buf + sizeof(buf) - 1) {
897 yyerror("string too long");
898 return findeol();
900 *p++ = c;
902 yylval.v.string = strdup(buf);
903 if (yylval.v.string == NULL)
904 fatal("yylex: strdup");
905 return STRING;
908 #define allowed_to_end_number(x) \
909 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
911 if (c == '-' || isdigit(c)) {
912 do {
913 *p++ = c;
914 if ((size_t)(p-buf) >= sizeof(buf)) {
915 yyerror("string too long");
916 return findeol();
918 } while ((c = lgetc(0)) != EOF && isdigit(c));
919 lungetc(c);
920 if (p == buf + 1 && buf[0] == '-')
921 goto nodigits;
922 if (c == EOF || allowed_to_end_number(c)) {
923 const char *errstr = NULL;
925 *p = '\0';
926 yylval.v.number = strtonum(buf, LLONG_MIN,
927 LLONG_MAX, &errstr);
928 if (errstr) {
929 yyerror("\"%s\" invalid number: %s",
930 buf, errstr);
931 return findeol();
933 return NUM;
934 } else {
935 nodigits:
936 while (p > buf + 1)
937 lungetc(*--p);
938 c = *--p;
939 if (c == '-')
940 return c;
944 #define allowed_in_string(x) \
945 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
946 x != '{' && x != '}' && \
947 x != '!' && x != '=' && x != '#' && \
948 x != ',' && x != ';'))
950 if (isalnum(c) || c == ':' || c == '_') {
951 do {
952 *p++ = c;
953 if ((size_t)(p-buf) >= sizeof(buf)) {
954 yyerror("string too long");
955 return findeol();
957 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
958 lungetc(c);
959 *p = '\0';
960 if ((token = lookup(buf)) == STRING)
961 yylval.v.string = xstrdup(buf);
962 return token;
964 if (c == '\n') {
965 yylval.lineno = file->lineno;
966 file->lineno++;
968 if (c == EOF)
969 return 0;
970 return c;
973 struct file *
974 pushfile(const char *name, int secret)
976 struct file *nfile;
978 nfile = xcalloc(1, sizeof(*nfile));
979 nfile->name = xstrdup(name);
980 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
981 log_warn("can't open %s", nfile->name);
982 free(nfile->name);
983 free(nfile);
984 return NULL;
986 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
987 nfile->ungetsize = 16;
988 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
989 TAILQ_INSERT_TAIL(&files, nfile, entry);
990 return nfile;
993 int
994 popfile(void)
996 struct file *prev;
998 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
999 prev->errors += file->errors;
1001 TAILQ_REMOVE(&files, file, entry);
1002 fclose(file->stream);
1003 free(file->name);
1004 free(file->ungetbuf);
1005 free(file);
1006 file = prev;
1007 return file ? 0 : EOF;
1010 int
1011 parse_conf(struct conf *c, const char *filename)
1013 struct sym *sym, *next;
1015 default_host = "*";
1016 default_port = 1965;
1018 conf = c;
1020 file = pushfile(filename, 0);
1021 if (file == NULL)
1022 return -1;
1023 topfile = file;
1025 yyparse();
1026 errors = file->errors;
1027 popfile();
1029 /* Free macros and check which have not been used. */
1030 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1031 /* TODO: warn if !sym->used */
1032 if (!sym->persist) {
1033 free(sym->name);
1034 free(sym->val);
1035 TAILQ_REMOVE(&symhead, sym, entry);
1036 free(sym);
1040 if (errors)
1041 return -1;
1042 return 0;
1045 int
1046 symset(const char *name, const char *val, int persist)
1048 struct sym *sym;
1050 TAILQ_FOREACH(sym, &symhead, entry) {
1051 if (!strcmp(name, sym->name))
1052 break;
1055 if (sym != NULL) {
1056 if (sym->persist)
1057 return 0;
1058 else {
1059 free(sym->name);
1060 free(sym->val);
1061 TAILQ_REMOVE(&symhead, sym, entry);
1062 free(sym);
1066 sym = xcalloc(1, sizeof(*sym));
1067 sym->name = xstrdup(name);
1068 sym->val = xstrdup(val);
1069 sym->used = 0;
1070 sym->persist = persist;
1072 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1073 return 0;
1076 int
1077 cmdline_symset(char *s)
1079 char *sym, *val;
1080 int ret;
1082 if ((val = strrchr(s, '=')) == NULL)
1083 return -1;
1084 sym = xcalloc(1, val - s + 1);
1085 memcpy(sym, s, val - s);
1086 ret = symset(sym, val + 1, 1);
1087 free(sym);
1088 return ret;
1091 char *
1092 symget(const char *nam)
1094 struct sym *sym;
1096 TAILQ_FOREACH(sym, &symhead, entry) {
1097 if (strcmp(nam, sym->name) == 0) {
1098 sym->used = 1;
1099 return sym->val;
1102 return NULL;
1105 char *
1106 ensure_absolute_path(char *path)
1108 if (path == NULL || *path != '/')
1109 yyerror("not an absolute path: %s", path);
1110 return path;
1113 int
1114 check_block_code(int n)
1116 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1117 yyerror("invalid block code %d", n);
1118 return n;
1121 char *
1122 check_block_fmt(char *fmt)
1124 char *s;
1126 for (s = fmt; *s; ++s) {
1127 if (*s != '%')
1128 continue;
1129 switch (*++s) {
1130 case '%':
1131 case 'p':
1132 case 'q':
1133 case 'P':
1134 case 'N':
1135 break;
1136 default:
1137 yyerror("invalid format specifier %%%c", *s);
1141 return fmt;
1144 int
1145 check_strip_no(int n)
1147 if (n <= 0)
1148 yyerror("invalid strip number %d", n);
1149 return n;
1152 int
1153 check_port_num(int n)
1155 if (n <= 0 || n >= UINT16_MAX)
1156 yyerror("port number is %s: %d",
1157 n <= 0 ? "too small" : "too large",
1158 n);
1159 return n;
1162 int
1163 check_prefork_num(int n)
1165 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1166 yyerror("invalid prefork number %d", n);
1167 return n;
1170 void
1171 advance_loc(void)
1173 loc = new_location();
1174 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1177 void
1178 advance_proxy(void)
1180 proxy = new_proxy();
1181 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1184 void
1185 parsehp(char *str, char **host, const char **port, const char *def)
1187 char *at;
1188 const char *errstr;
1190 *host = str;
1192 if ((at = strchr(str, ':')) != NULL) {
1193 *at++ = '\0';
1194 *port = at;
1195 } else
1196 *port = def;
1198 strtonum(*port, 1, UINT16_MAX, &errstr);
1199 if (errstr != NULL)
1200 yyerror("port is %s: %s", errstr, *port);
1203 int
1204 fastcgi_conf(const char *path, const char *port)
1206 struct fcgi *f;
1207 int i = 0;
1209 TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1210 if (!strcmp(f->path, path) &&
1211 ((port == NULL && *f->port == '\0') ||
1212 !strcmp(f->port, port)))
1213 return i;
1214 ++i;
1217 f = xcalloc(1, sizeof(*f));
1218 f->id = i;
1219 (void)strlcpy(f->path, path, sizeof(f->path));
1220 if (port != NULL)
1221 (void)strlcpy(f->port, port, sizeof(f->port));
1222 TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1224 return f->id;
1227 void
1228 add_param(char *name, char *val)
1230 struct envlist *e;
1231 struct envhead *h = &loc->params;
1233 e = xcalloc(1, sizeof(*e));
1234 (void) strlcpy(e->name, name, sizeof(e->name));
1235 (void) strlcpy(e->value, val, sizeof(e->value));
1236 TAILQ_INSERT_TAIL(h, e, envs);
1239 int
1240 getservice(const char *n)
1242 struct servent *s;
1243 const char *errstr;
1244 long long llval;
1246 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1247 if (errstr) {
1248 s = getservbyname(n, "tcp");
1249 if (s == NULL)
1250 s = getservbyname(n, "udp");
1251 if (s == NULL)
1252 return (-1);
1253 return (ntohs(s->s_port));
1256 return ((unsigned short)llval);
1259 static void
1260 add_to_addr_queue(struct addrhead *a, struct addrinfo *ai)
1262 struct address *addr;
1263 struct sockaddr_in *sin;
1264 struct sockaddr_in6 *sin6;
1266 if (ai->ai_addrlen > sizeof(addr->ss))
1267 fatalx("ai_addrlen larger than a sockaddr_storage");
1269 TAILQ_FOREACH(addr, a, addrs) {
1270 if (addr->ai_flags == ai->ai_flags &&
1271 addr->ai_family == ai->ai_family &&
1272 addr->ai_socktype == ai->ai_socktype &&
1273 addr->ai_protocol == ai->ai_protocol &&
1274 addr->slen == ai->ai_addrlen &&
1275 !memcmp(&addr->ss, ai->ai_addr, addr->slen))
1276 return;
1279 addr = xcalloc(1, sizeof(*addr));
1280 addr->ai_flags = ai->ai_flags;
1281 addr->ai_family = ai->ai_family;
1282 addr->ai_socktype = ai->ai_socktype;
1283 addr->ai_protocol = ai->ai_protocol;
1284 addr->slen = ai->ai_addrlen;
1285 memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
1287 /* for commodity */
1288 switch (addr->ai_family) {
1289 case AF_INET:
1290 sin = (struct sockaddr_in *)&addr->ss;
1291 addr->port = ntohs(sin->sin_port);
1292 break;
1293 case AF_INET6:
1294 sin6 = (struct sockaddr_in6 *)&addr->ss;
1295 addr->port = ntohs(sin6->sin6_port);
1296 break;
1297 default:
1298 fatalx("unknown socket family %d", addr->ai_family);
1301 addr->sock = -1;
1303 TAILQ_INSERT_HEAD(a, addr, addrs);
1306 void
1307 listen_on(const char *hostname, const char *servname)
1309 struct addrinfo hints, *res, *res0;
1310 int error;
1312 memset(&hints, 0, sizeof(hints));
1313 hints.ai_family = AF_UNSPEC;
1314 hints.ai_socktype = SOCK_STREAM;
1315 hints.ai_flags = AI_PASSIVE;
1316 error = getaddrinfo(hostname, servname, &hints, &res0);
1317 if (error) {
1318 yyerror("listen on \"%s\" port %s: %s", hostname, servname,
1319 gai_strerror(errno));
1320 return;
1323 for (res = res0; res; res = res->ai_next) {
1324 add_to_addr_queue(&host->addrs, res);
1325 add_to_addr_queue(&conf->addrs, res);
1328 freeaddrinfo(res0);