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 COMBINED COMMON CONDENSED
128 %token DEFAULT
129 %token FASTCGI FOR_HOST
130 %token INCLUDE INDEX IPV6
131 %token KEY
132 %token LANG LEGACY 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 STYLE 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;
271 | STYLE COMMON {
272 conf->log_format = LOG_FORMAT_COMMON;
274 | STYLE COMBINED {
275 conf->log_format = LOG_FORMAT_COMBINED;
277 | STYLE CONDENSED {
278 conf->log_format = LOG_FORMAT_CONDENSED;
280 | STILE LEGACY {
281 conf->log_format = LOG_FORMAT_LEGACY;
285 vhost : SERVER string {
286 host = new_vhost();
287 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
289 loc = new_location();
290 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
292 TAILQ_INIT(&host->proxies);
294 (void) strlcpy(loc->match, "*", sizeof(loc->match));
295 (void) strlcpy(host->domain, $2, sizeof(host->domain));
297 if (strstr($2, "xn--") != NULL) {
298 yywarn("\"%s\" looks like punycode: you "
299 "should use the decoded hostname", $2);
302 free($2);
303 } '{' optnl servbody '}' {
304 if (host->cert_path == NULL ||
305 host->key_path == NULL)
306 yyerror("invalid vhost definition: %s",
307 host->domain);
308 if (TAILQ_EMPTY(&host->addrs)) {
309 char portno[32];
310 int r;
312 r = snprintf(portno, sizeof(portno), "%d",
313 default_port);
314 if (r < 0 || (size_t)r >= sizeof(portno))
315 fatal("snprintf");
317 yywarn("missing `listen on' in server %s,"
318 " assuming %s port %d", $2, default_host,
319 default_port);
320 listen_on(default_host, portno);
323 | error '}' { yyerror("bad server directive"); }
326 servbody : /* empty */
327 | servbody servopt optnl
328 | servbody location optnl
329 | servbody proxy optnl
332 listen_addr : '*' { $$ = NULL; }
333 | STRING
336 servopt : ALIAS string {
337 struct alist *a;
339 a = xcalloc(1, sizeof(*a));
340 (void) strlcpy(a->alias, $2, sizeof(a->alias));
341 free($2);
342 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
344 | CERT string {
345 ensure_absolute_path($2);
346 free(host->cert_path);
347 host->cert_path = $2;
349 | KEY string {
350 ensure_absolute_path($2);
351 free(host->key_path);
352 host->key_path = $2;
354 | OCSP string {
355 ensure_absolute_path($2);
356 free(host->ocsp_path);
357 host->ocsp_path = $2;
359 | PARAM string '=' string {
360 yywarn("the top-level `param' directive is deprecated."
361 " Please use `fastcgi { param ... }`");
362 add_param($2, $4);
364 | LISTEN ON listen_addr {
365 listen_on($3, "1965");
367 | LISTEN ON listen_addr PORT STRING {
368 listen_on($3, $5);
369 free($3);
370 free($5);
372 | LISTEN ON listen_addr PORT NUM {
373 char portno[32];
374 int r;
376 r = snprintf(portno, sizeof(portno), "%d", $5);
377 if (r < 0 || (size_t)r >= sizeof(portno))
378 fatal("snprintf");
380 listen_on($3, portno);
381 free($3);
383 | locopt
386 proxy : PROXY { advance_proxy(); }
387 proxy_matches '{' optnl proxy_opts '}' {
388 if (*proxy->host == '\0')
389 yyerror("invalid proxy block: missing `relay-to' option");
391 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
392 (proxy->cert_path != NULL && proxy->key_path == NULL))
393 yyerror("invalid proxy block: missing cert or key");
397 proxy_matches : /* empty */
398 | proxy_matches proxy_match
401 proxy_port : /* empty */ { $$ = 1965; }
402 | PORT STRING {
403 if (($$ = getservice($2)) == -1)
404 yyerror("invalid port number %s", $2);
405 free($2);
407 | PORT NUM { $$ = $2; }
410 proxy_match : PROTO string {
411 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
412 free($2);
414 | FOR_HOST string proxy_port {
415 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
416 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
417 "%d", $3);
418 free($2);
422 proxy_opts : /* empty */
423 | proxy_opts proxy_opt optnl
426 proxy_opt : CERT string {
427 free(proxy->cert);
428 ensure_absolute_path($2);
429 proxy->cert_path = $2;
431 | KEY string {
432 free(proxy->key);
433 ensure_absolute_path($2);
434 proxy->key_path = $2;
436 | PROTOCOLS string {
437 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
438 yyerror("invalid protocols string \"%s\"", $2);
439 free($2);
441 | RELAY_TO string proxy_port {
442 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
443 (void) snprintf(proxy->port, sizeof(proxy->port),
444 "%d", $3);
445 free($2);
447 | REQUIRE CLIENT CA string {
448 ensure_absolute_path($4);
449 proxy->reqca_path = $4;
451 | SNI string {
452 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
453 free($2);
455 | USE_TLS bool {
456 proxy->notls = !$2;
458 | VERIFYNAME bool {
459 proxy->noverifyname = !$2;
463 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
464 /* drop the starting '/' if any */
465 if (*$3 == '/')
466 memmove($3, $3+1, strlen($3));
467 (void) strlcpy(loc->match, $3, sizeof(loc->match));
468 free($3);
470 | error '}'
473 locopts : /* empty */
474 | locopts locopt optnl
477 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
478 | BLOCK RETURN NUM string {
479 check_block_fmt($4);
480 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
481 loc->block_code = check_block_code($3);
482 free($4);
484 | BLOCK RETURN NUM {
485 (void) strlcpy(loc->block_fmt, "temporary failure",
486 sizeof(loc->block_fmt));
487 loc->block_code = check_block_code($3);
488 if ($3 >= 30 && $3 < 40)
489 yyerror("missing `meta' for block return %d", $3);
491 | BLOCK {
492 (void) strlcpy(loc->block_fmt, "temporary failure",
493 sizeof(loc->block_fmt));
494 loc->block_code = 40;
496 | DEFAULT TYPE string {
497 (void) strlcpy(loc->default_mime, $3,
498 sizeof(loc->default_mime));
499 free($3);
501 | fastcgi
502 | INDEX string {
503 (void) strlcpy(loc->index, $2, sizeof(loc->index));
504 free($2);
506 | LANG string {
507 (void) strlcpy(loc->lang, $2,
508 sizeof(loc->lang));
509 free($2);
511 | LOG bool { loc->disable_log = !$2; }
512 | REQUIRE CLIENT CA string {
513 ensure_absolute_path($4);
514 loc->reqca_path = $4;
516 | ROOT string {
517 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
518 free($2);
520 | STRIP NUM { loc->strip = check_strip_no($2); }
523 fastcgi : FASTCGI '{' optnl fastcgiopts '}'
524 | FASTCGI fastcgiopt
525 | FASTCGI OFF {
526 loc->fcgi = -1;
527 loc->nofcgi = 1;
529 | FASTCGI string {
530 yywarn("`fastcgi path' is deprecated. "
531 "Please use `fastcgi socket path' instead.");
532 loc->fcgi = fastcgi_conf($2, NULL);
533 free($2);
537 fastcgiopts : /* empty */
538 | fastcgiopts fastcgiopt optnl
541 fastcgiopt : PARAM string '=' string {
542 add_param($2, $4);
544 | SOCKET string {
545 loc->fcgi = fastcgi_conf($2, NULL);
546 free($2);
548 | SOCKET TCP string PORT NUM {
549 char *c;
551 if (asprintf(&c, "%d", $5) == -1)
552 fatal("asprintf");
553 loc->fcgi = fastcgi_conf($3, c);
554 free($3);
555 free(c);
557 | SOCKET TCP string {
558 loc->fcgi = fastcgi_conf($3, "9000");
560 | SOCKET TCP string PORT string {
561 loc->fcgi = fastcgi_conf($3, $5);
562 free($3);
563 free($5);
567 types : TYPES '{' optnl mediaopts_l '}' ;
569 mediaopts_l : mediaopts_l mediaoptsl nl
570 | mediaoptsl nl
573 mediaoptsl : STRING {
574 free(current_media);
575 current_media = $1;
576 } medianames_l optsemicolon
577 | include
580 medianames_l : medianames_l medianamesl
581 | medianamesl
584 medianamesl : numberstring {
585 if (add_mime(&conf->mime, current_media, $1) == -1)
586 fatal("add_mime");
587 free($1);
591 nl : '\n' optnl
594 optnl : '\n' optnl /* zero or more newlines */
595 | ';' optnl /* semicolons too */
596 | /*empty*/
599 optsemicolon : ';'
603 %%
605 static const struct keyword {
606 const char *word;
607 int token;
608 } keywords[] = {
609 /* these MUST be sorted */
610 {"access", ACCESS},
611 {"alias", ALIAS},
612 {"auto", AUTO},
613 {"block", BLOCK},
614 {"ca", CA},
615 {"cert", CERT},
616 {"chroot", CHROOT},
617 {"client", CLIENT},
618 {"combined", COMBINED},
619 {"common", COMMON},
620 {"condensed", CONDENSED},
621 {"default", DEFAULT},
622 {"fastcgi", FASTCGI},
623 {"for-host", FOR_HOST},
624 {"include", INCLUDE},
625 {"index", INDEX},
626 {"ipv6", IPV6},
627 {"key", KEY},
628 {"lang", LANG},
629 {"legacy", LEGACY},
630 {"listen", LISTEN},
631 {"location", LOCATION},
632 {"log", LOG},
633 {"ocsp", OCSP},
634 {"off", OFF},
635 {"on", ON},
636 {"param", PARAM},
637 {"port", PORT},
638 {"prefork", PREFORK},
639 {"proto", PROTO},
640 {"protocols", PROTOCOLS},
641 {"proxy", PROXY},
642 {"relay-to", RELAY_TO},
643 {"require", REQUIRE},
644 {"return", RETURN},
645 {"root", ROOT},
646 {"server", SERVER},
647 {"sni", SNI},
648 {"socket", SOCKET},
649 {"strip", STRIP},
650 {"style", STYLE},
651 {"syslog", SYSLOG},
652 {"tcp", TCP},
653 {"to-ext", TOEXT},
654 {"type", TYPE},
655 {"types", TYPES},
656 {"use-tls", USE_TLS},
657 {"user", USER},
658 {"verifyname", VERIFYNAME},
659 };
661 void
662 yyerror(const char *msg, ...)
664 va_list ap;
666 file->errors++;
668 va_start(ap, msg);
669 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
670 vfprintf(stderr, msg, ap);
671 fprintf(stderr, "\n");
672 va_end(ap);
675 void
676 yywarn(const char *msg, ...)
678 va_list ap;
680 va_start(ap, msg);
681 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
682 vfprintf(stderr, msg, ap);
683 fprintf(stderr, "\n");
684 va_end(ap);
687 int
688 kw_cmp(const void *k, const void *e)
690 return strcmp(k, ((struct keyword *)e)->word);
693 int
694 lookup(char *s)
696 const struct keyword *p;
698 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
699 sizeof(keywords[0]), kw_cmp);
701 if (p)
702 return p->token;
703 else
704 return STRING;
707 #define START_EXPAND 1
708 #define DONE_EXPAND 2
710 static int expanding;
712 int
713 igetc(void)
715 int c;
717 while (1) {
718 if (file->ungetpos > 0)
719 c = file->ungetbuf[--file->ungetpos];
720 else
721 c = getc(file->stream);
723 if (c == START_EXPAND)
724 expanding = 1;
725 else if (c == DONE_EXPAND)
726 expanding = 0;
727 else
728 break;
730 return c;
733 int
734 lgetc(int quotec)
736 int c, next;
738 if (quotec) {
739 if ((c = igetc()) == EOF) {
740 yyerror("reached end of file while parsing "
741 "quoted string");
742 if (file == topfile || popfile() == EOF)
743 return EOF;
744 return quotec;
746 return c;
749 while ((c = igetc()) == '\\') {
750 next = igetc();
751 if (next != '\n') {
752 c = next;
753 break;
755 yylval.lineno = file->lineno;
756 file->lineno++;
759 if (c == EOF) {
760 /*
761 * Fake EOL when hit EOF for the first time. This gets line
762 * count right if last line in included file is syntactically
763 * invalid and has no newline.
764 */
765 if (file->eof_reached == 0) {
766 file->eof_reached = 1;
767 return '\n';
769 while (c == EOF) {
770 if (file == topfile || popfile() == EOF)
771 return EOF;
772 c = igetc();
775 return c;
778 void
779 lungetc(int c)
781 if (c == EOF)
782 return;
784 if (file->ungetpos >= file->ungetsize) {
785 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
786 if (p == NULL)
787 fatal("lungetc");
788 file->ungetbuf = p;
789 file->ungetsize *= 2;
791 file->ungetbuf[file->ungetpos++] = c;
794 int
795 findeol(void)
797 int c;
799 /* Skip to either EOF or the first real EOL. */
800 while (1) {
801 c = lgetc(0);
802 if (c == '\n') {
803 file->lineno++;
804 break;
806 if (c == EOF)
807 break;
809 return ERROR;
812 int
813 yylex(void)
815 char buf[8096];
816 char *p, *val;
817 int quotec, next, c;
818 int token;
820 top:
821 p = buf;
822 while ((c = lgetc(0)) == ' ' || c == '\t')
823 ; /* nothing */
825 yylval.lineno = file->lineno;
826 if (c == '#')
827 while ((c = lgetc(0)) != '\n' && c != EOF)
828 ; /* nothing */
829 if (c == '$' && !expanding) {
830 while (1) {
831 if ((c = lgetc(0)) == EOF)
832 return 0;
833 if (p + 1 >= buf + sizeof(buf) -1) {
834 yyerror("string too long");
835 return findeol();
837 if (isalnum(c) || c == '_') {
838 *p++ = c;
839 continue;
841 *p = '\0';
842 lungetc(c);
843 break;
845 val = symget(buf);
846 if (val == NULL) {
847 yyerror("macro `%s' not defined", buf);
848 return findeol();
850 yylval.v.string = xstrdup(val);
851 return STRING;
853 if (c == '@' && !expanding) {
854 while (1) {
855 if ((c = lgetc(0)) == EOF)
856 return 0;
858 if (p + 1 >= buf + sizeof(buf) - 1) {
859 yyerror("string too long");
860 return findeol();
862 if (isalnum(c) || c == '_') {
863 *p++ = c;
864 continue;
866 *p = '\0';
867 lungetc(c);
868 break;
870 val = symget(buf);
871 if (val == NULL) {
872 yyerror("macro '%s' not defined", buf);
873 return findeol();
875 p = val + strlen(val) - 1;
876 lungetc(DONE_EXPAND);
877 while (p >= val) {
878 lungetc(*p);
879 p--;
881 lungetc(START_EXPAND);
882 goto top;
885 switch (c) {
886 case '\'':
887 case '"':
888 quotec = c;
889 while (1) {
890 if ((c = lgetc(quotec)) == EOF)
891 return 0;
892 if (c == '\n') {
893 file->lineno++;
894 continue;
895 } else if (c == '\\') {
896 if ((next = lgetc(quotec)) == EOF)
897 return (0);
898 if (next == quotec || next == ' ' ||
899 next == '\t')
900 c = next;
901 else if (next == '\n') {
902 file->lineno++;
903 continue;
904 } else
905 lungetc(next);
906 } else if (c == quotec) {
907 *p = '\0';
908 break;
909 } else if (c == '\0') {
910 yyerror("invalid syntax");
911 return findeol();
913 if (p + 1 >= buf + sizeof(buf) - 1) {
914 yyerror("string too long");
915 return findeol();
917 *p++ = c;
919 yylval.v.string = strdup(buf);
920 if (yylval.v.string == NULL)
921 fatal("yylex: strdup");
922 return STRING;
925 #define allowed_to_end_number(x) \
926 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
928 if (c == '-' || isdigit(c)) {
929 do {
930 *p++ = c;
931 if ((size_t)(p-buf) >= sizeof(buf)) {
932 yyerror("string too long");
933 return findeol();
935 } while ((c = lgetc(0)) != EOF && isdigit(c));
936 lungetc(c);
937 if (p == buf + 1 && buf[0] == '-')
938 goto nodigits;
939 if (c == EOF || allowed_to_end_number(c)) {
940 const char *errstr = NULL;
942 *p = '\0';
943 yylval.v.number = strtonum(buf, LLONG_MIN,
944 LLONG_MAX, &errstr);
945 if (errstr) {
946 yyerror("\"%s\" invalid number: %s",
947 buf, errstr);
948 return findeol();
950 return NUM;
951 } else {
952 nodigits:
953 while (p > buf + 1)
954 lungetc(*--p);
955 c = *--p;
956 if (c == '-')
957 return c;
961 #define allowed_in_string(x) \
962 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
963 x != '{' && x != '}' && \
964 x != '!' && x != '=' && x != '#' && \
965 x != ',' && x != ';'))
967 if (isalnum(c) || c == ':' || c == '_') {
968 do {
969 *p++ = c;
970 if ((size_t)(p-buf) >= sizeof(buf)) {
971 yyerror("string too long");
972 return findeol();
974 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
975 lungetc(c);
976 *p = '\0';
977 if ((token = lookup(buf)) == STRING)
978 yylval.v.string = xstrdup(buf);
979 return token;
981 if (c == '\n') {
982 yylval.lineno = file->lineno;
983 file->lineno++;
985 if (c == EOF)
986 return 0;
987 return c;
990 struct file *
991 pushfile(const char *name, int secret)
993 struct file *nfile;
995 nfile = xcalloc(1, sizeof(*nfile));
996 nfile->name = xstrdup(name);
997 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
998 log_warn("can't open %s", nfile->name);
999 free(nfile->name);
1000 free(nfile);
1001 return NULL;
1003 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
1004 nfile->ungetsize = 16;
1005 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
1006 TAILQ_INSERT_TAIL(&files, nfile, entry);
1007 return nfile;
1010 int
1011 popfile(void)
1013 struct file *prev;
1015 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
1016 prev->errors += file->errors;
1018 TAILQ_REMOVE(&files, file, entry);
1019 fclose(file->stream);
1020 free(file->name);
1021 free(file->ungetbuf);
1022 free(file);
1023 file = prev;
1024 return file ? 0 : EOF;
1027 int
1028 parse_conf(struct conf *c, const char *filename)
1030 struct sym *sym, *next;
1032 default_host = "*";
1033 default_port = 1965;
1035 conf = c;
1037 file = pushfile(filename, 0);
1038 if (file == NULL)
1039 return -1;
1040 topfile = file;
1042 yyparse();
1043 errors = file->errors;
1044 popfile();
1046 /* Free macros and check which have not been used. */
1047 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1048 /* TODO: warn if !sym->used */
1049 if (!sym->persist) {
1050 free(sym->name);
1051 free(sym->val);
1052 TAILQ_REMOVE(&symhead, sym, entry);
1053 free(sym);
1057 if (errors)
1058 return -1;
1059 return 0;
1062 int
1063 symset(const char *name, const char *val, int persist)
1065 struct sym *sym;
1067 TAILQ_FOREACH(sym, &symhead, entry) {
1068 if (!strcmp(name, sym->name))
1069 break;
1072 if (sym != NULL) {
1073 if (sym->persist)
1074 return 0;
1075 else {
1076 free(sym->name);
1077 free(sym->val);
1078 TAILQ_REMOVE(&symhead, sym, entry);
1079 free(sym);
1083 sym = xcalloc(1, sizeof(*sym));
1084 sym->name = xstrdup(name);
1085 sym->val = xstrdup(val);
1086 sym->used = 0;
1087 sym->persist = persist;
1089 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1090 return 0;
1093 int
1094 cmdline_symset(char *s)
1096 char *sym, *val;
1097 int ret;
1099 if ((val = strrchr(s, '=')) == NULL)
1100 return -1;
1101 sym = xcalloc(1, val - s + 1);
1102 memcpy(sym, s, val - s);
1103 ret = symset(sym, val + 1, 1);
1104 free(sym);
1105 return ret;
1108 char *
1109 symget(const char *nam)
1111 struct sym *sym;
1113 TAILQ_FOREACH(sym, &symhead, entry) {
1114 if (strcmp(nam, sym->name) == 0) {
1115 sym->used = 1;
1116 return sym->val;
1119 return NULL;
1122 char *
1123 ensure_absolute_path(char *path)
1125 if (path == NULL || *path != '/')
1126 yyerror("not an absolute path: %s", path);
1127 return path;
1130 int
1131 check_block_code(int n)
1133 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1134 yyerror("invalid block code %d", n);
1135 return n;
1138 char *
1139 check_block_fmt(char *fmt)
1141 char *s;
1143 for (s = fmt; *s; ++s) {
1144 if (*s != '%')
1145 continue;
1146 switch (*++s) {
1147 case '%':
1148 case 'p':
1149 case 'q':
1150 case 'P':
1151 case 'N':
1152 break;
1153 default:
1154 yyerror("invalid format specifier %%%c", *s);
1158 return fmt;
1161 int
1162 check_strip_no(int n)
1164 if (n <= 0)
1165 yyerror("invalid strip number %d", n);
1166 return n;
1169 int
1170 check_port_num(int n)
1172 if (n <= 0 || n >= UINT16_MAX)
1173 yyerror("port number is %s: %d",
1174 n <= 0 ? "too small" : "too large",
1175 n);
1176 return n;
1179 int
1180 check_prefork_num(int n)
1182 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1183 yyerror("invalid prefork number %d", n);
1184 return n;
1187 void
1188 advance_loc(void)
1190 loc = new_location();
1191 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1194 void
1195 advance_proxy(void)
1197 proxy = new_proxy();
1198 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1201 void
1202 parsehp(char *str, char **host, const char **port, const char *def)
1204 char *at;
1205 const char *errstr;
1207 *host = str;
1209 if ((at = strchr(str, ':')) != NULL) {
1210 *at++ = '\0';
1211 *port = at;
1212 } else
1213 *port = def;
1215 strtonum(*port, 1, UINT16_MAX, &errstr);
1216 if (errstr != NULL)
1217 yyerror("port is %s: %s", errstr, *port);
1220 int
1221 fastcgi_conf(const char *path, const char *port)
1223 struct fcgi *f;
1224 int i = 0;
1226 TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1227 if (!strcmp(f->path, path) &&
1228 ((port == NULL && *f->port == '\0') ||
1229 !strcmp(f->port, port)))
1230 return i;
1231 ++i;
1234 f = xcalloc(1, sizeof(*f));
1235 f->id = i;
1236 (void)strlcpy(f->path, path, sizeof(f->path));
1237 if (port != NULL)
1238 (void)strlcpy(f->port, port, sizeof(f->port));
1239 TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1241 return f->id;
1244 void
1245 add_param(char *name, char *val)
1247 struct envlist *e;
1248 struct envhead *h = &loc->params;
1250 e = xcalloc(1, sizeof(*e));
1251 (void) strlcpy(e->name, name, sizeof(e->name));
1252 (void) strlcpy(e->value, val, sizeof(e->value));
1253 TAILQ_INSERT_TAIL(h, e, envs);
1256 int
1257 getservice(const char *n)
1259 struct servent *s;
1260 const char *errstr;
1261 long long llval;
1263 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1264 if (errstr) {
1265 s = getservbyname(n, "tcp");
1266 if (s == NULL)
1267 s = getservbyname(n, "udp");
1268 if (s == NULL)
1269 return (-1);
1270 return (ntohs(s->s_port));
1273 return ((unsigned short)llval);
1276 static void
1277 add_to_addr_queue(struct addrhead *a, struct addrinfo *ai)
1279 struct address *addr;
1280 struct sockaddr_in *sin;
1281 struct sockaddr_in6 *sin6;
1283 if (ai->ai_addrlen > sizeof(addr->ss))
1284 fatalx("ai_addrlen larger than a sockaddr_storage");
1286 TAILQ_FOREACH(addr, a, addrs) {
1287 if (addr->ai_flags == ai->ai_flags &&
1288 addr->ai_family == ai->ai_family &&
1289 addr->ai_socktype == ai->ai_socktype &&
1290 addr->ai_protocol == ai->ai_protocol &&
1291 addr->slen == ai->ai_addrlen &&
1292 !memcmp(&addr->ss, ai->ai_addr, addr->slen))
1293 return;
1296 addr = xcalloc(1, sizeof(*addr));
1297 addr->ai_flags = ai->ai_flags;
1298 addr->ai_family = ai->ai_family;
1299 addr->ai_socktype = ai->ai_socktype;
1300 addr->ai_protocol = ai->ai_protocol;
1301 addr->slen = ai->ai_addrlen;
1302 memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
1304 /* for commodity */
1305 switch (addr->ai_family) {
1306 case AF_INET:
1307 sin = (struct sockaddr_in *)&addr->ss;
1308 addr->port = ntohs(sin->sin_port);
1309 break;
1310 case AF_INET6:
1311 sin6 = (struct sockaddr_in6 *)&addr->ss;
1312 addr->port = ntohs(sin6->sin6_port);
1313 break;
1314 default:
1315 fatalx("unknown socket family %d", addr->ai_family);
1318 addr->sock = -1;
1320 TAILQ_INSERT_HEAD(a, addr, addrs);
1323 void
1324 listen_on(const char *hostname, const char *servname)
1326 struct addrinfo hints, *res, *res0;
1327 int error;
1329 memset(&hints, 0, sizeof(hints));
1330 hints.ai_family = AF_UNSPEC;
1331 hints.ai_socktype = SOCK_STREAM;
1332 hints.ai_flags = AI_PASSIVE;
1333 error = getaddrinfo(hostname, servname, &hints, &res0);
1334 if (error) {
1335 yyerror("listen on \"%s\" port %s: %s", hostname, servname,
1336 gai_strerror(errno));
1337 return;
1340 for (res = res0; res; res = res->ai_next) {
1341 add_to_addr_queue(&host->addrs, res);
1342 add_to_addr_queue(&conf->addrs, res);
1345 freeaddrinfo(res0);