Blob


1 %{
3 /*
4 * Copyright (c) 2021-2024 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>
35 #include <syslog.h>
37 #include "log.h"
39 struct conf *conf;
41 static const char *default_host = NULL;
42 static uint16_t default_port = 1965;
44 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
45 static struct file {
46 TAILQ_ENTRY(file) entry;
47 FILE *stream;
48 char *name;
49 size_t ungetpos;
50 size_t ungetsize;
51 u_char *ungetbuf;
52 int eof_reached;
53 int lineno;
54 int errors;
55 } *file, *topfile;
57 struct file *pushfile(const char *, int);
58 int popfile(void);
59 int yyparse(void);
60 int yylex(void);
61 void yyerror(const char *, ...)
62 __attribute__((__format__ (printf, 1, 2)))
63 __attribute__((__nonnull__ (1)));
64 void yywarn(const char *, ...)
65 __attribute__((__format__ (printf, 1, 2)))
66 __attribute__((__nonnull__ (1)));
67 int kw_cmp(const void *, const void *);
68 int lookup(char *);
69 int igetc(void);
70 int lgetc(int);
71 void lungetc(int);
72 int findeol(void);
74 /*
75 * #define YYDEBUG 1
76 * int yydebug = 1;
77 */
79 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
80 struct sym {
81 TAILQ_ENTRY(sym) entry;
82 int used;
83 int persist;
84 char *name;
85 char *val;
86 };
88 int symset(const char *, const char *, int);
89 char *symget(const char *);
91 char *ensure_absolute_path(char*);
92 int check_block_code(int);
93 char *check_block_fmt(char*);
94 int check_strip_no(int);
95 int check_port_num(int);
96 int check_prefork_num(int);
97 void advance_loc(void);
98 void advance_proxy(void);
99 void parsehp(char *, char **, const char **, const char *);
100 int fastcgi_conf(const char *, const char *);
101 void add_param(char *, char *);
102 int getservice(const char *);
103 void listen_on(const char *, const char *);
105 static struct vhost *host;
106 static struct location *loc;
107 static struct proxy *proxy;
108 static char *current_media;
109 static int errors;
111 typedef struct {
112 union {
113 char *string;
114 int number;
115 } v;
116 int lineno;
117 } YYSTYPE;
119 #define YYSTYPE YYSTYPE
121 %}
123 /* for bison: */
124 /* %define parse.error verbose */
126 %token ACCESS ALIAS AUTO
127 %token BLOCK
128 %token CA CERT CHROOT CLIENT
129 %token DEFAULT
130 %token FACILITY FASTCGI FOR_HOST
131 %token INCLUDE INDEX IPV6
132 %token KEY
133 %token LANG LISTEN LOCATION LOG
134 %token OCSP OFF ON
135 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
136 %token RELAY_TO REQUIRE RETURN ROOT
137 %token SERVER SNI SOCKET STRIP STYLE SYSLOG
138 %token TCP TOEXT TYPE TYPES
139 %token USE_TLS USER
140 %token VERIFYNAME
142 %token ERROR
144 %token <v.string> STRING
145 %token <v.number> NUM
147 %type <v.number> bool proxy_port
148 %type <v.string> string numberstring listen_addr
150 %%
152 conf : /* empty */
153 | conf include nl
154 | conf varset nl
155 | conf option nl
156 | conf vhost nl
157 | conf types nl
158 | conf error nl { 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 = NULL;
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 : ACCESS string {
264 free(conf->log_access);
265 conf->log_access = $2;
267 | STYLE string {
268 if (!strcmp("combined", $2))
269 conf->log_format = LOG_FORMAT_COMBINED;
270 else if (!strcmp("common", $2))
271 conf->log_format = LOG_FORMAT_COMMON;
272 else if (!strcmp("condensed", $2))
273 conf->log_format = LOG_FORMAT_CONDENSED;
274 else if (!strcmp("legacy", $2))
275 conf->log_format = LOG_FORMAT_LEGACY;
276 else
277 yyerror("unknown log style: %s", $2);
278 free($2);
280 | SYSLOG FACILITY string {
281 const char *str = $3;
283 conf->log_syslog = 1;
285 if (!strncasecmp(str, "LOG_", 4))
286 str += 4;
288 if (!strcasecmp(str, "daemon"))
289 conf->log_facility = LOG_DAEMON;
290 #ifdef LOG_FTP
291 else if (!strcasecmp(str, "ftp"))
292 conf->log_facility = LOG_FTP;
293 #endif
294 else if (!strcasecmp(str, "local1"))
295 conf->log_facility = LOG_LOCAL1;
296 else if (!strcasecmp(str, "local2"))
297 conf->log_facility = LOG_LOCAL2;
298 else if (!strcasecmp(str, "local3"))
299 conf->log_facility = LOG_LOCAL3;
300 else if (!strcasecmp(str, "local4"))
301 conf->log_facility = LOG_LOCAL4;
302 else if (!strcasecmp(str, "local5"))
303 conf->log_facility = LOG_LOCAL5;
304 else if (!strcasecmp(str, "local6"))
305 conf->log_facility = LOG_LOCAL6;
306 else if (!strcasecmp(str, "local7"))
307 conf->log_facility = LOG_LOCAL7;
308 else if (!strcasecmp(str, "user"))
309 conf->log_facility = LOG_USER;
310 else
311 yywarn("unknown syslog facility `%s'",
312 $3);
314 free($3);
316 | SYSLOG OFF {
317 conf->log_syslog = 0;
319 | SYSLOG {
320 conf->log_syslog = 1;
324 vhost : SERVER string {
325 host = new_vhost();
326 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
328 loc = new_location();
329 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
331 TAILQ_INIT(&host->proxies);
333 (void) strlcpy(loc->match, "*", sizeof(loc->match));
334 (void) strlcpy(host->domain, $2, sizeof(host->domain));
336 if (strstr($2, "xn--") != NULL) {
337 yywarn("\"%s\" looks like punycode: you "
338 "should use the decoded hostname", $2);
341 free($2);
342 } '{' optnl servbody '}' {
343 if (host->cert_path == NULL ||
344 host->key_path == NULL)
345 yyerror("invalid vhost definition: %s",
346 host->domain);
347 if (TAILQ_EMPTY(&host->addrs)) {
348 char portno[32];
349 int r;
351 r = snprintf(portno, sizeof(portno), "%d",
352 default_port);
353 if (r < 0 || (size_t)r >= sizeof(portno))
354 fatal("snprintf");
356 yywarn("missing `listen on' in server %s,"
357 " assuming %s port %d", host->domain,
358 default_host ? default_host : "*",
359 default_port);
360 listen_on(default_host, portno);
363 | error '}' { yyerror("bad server directive"); }
366 servbody : /* empty */
367 | servbody servopt optnl
368 | servbody location optnl
369 | servbody proxy optnl
372 listen_addr : '*' { $$ = NULL; }
373 | STRING
376 servopt : ALIAS string {
377 struct alist *a;
379 a = xcalloc(1, sizeof(*a));
380 (void) strlcpy(a->alias, $2, sizeof(a->alias));
381 free($2);
382 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
384 | CERT string {
385 ensure_absolute_path($2);
386 free(host->cert_path);
387 host->cert_path = $2;
389 | KEY string {
390 ensure_absolute_path($2);
391 free(host->key_path);
392 host->key_path = $2;
394 | OCSP string {
395 ensure_absolute_path($2);
396 free(host->ocsp_path);
397 host->ocsp_path = $2;
399 | PARAM string '=' string {
400 yywarn("the top-level `param' directive is deprecated."
401 " Please use `fastcgi { param ... }`");
402 add_param($2, $4);
404 | LISTEN ON listen_addr {
405 listen_on($3, "1965");
406 free($3);
408 | LISTEN ON listen_addr PORT STRING {
409 listen_on($3, $5);
410 free($3);
411 free($5);
413 | LISTEN ON listen_addr PORT NUM {
414 char portno[32];
415 int r;
417 r = snprintf(portno, sizeof(portno), "%d", $5);
418 if (r < 0 || (size_t)r >= sizeof(portno))
419 fatal("snprintf");
421 listen_on($3, portno);
422 free($3);
424 | locopt
427 proxy : PROXY { advance_proxy(); }
428 proxy_matches '{' optnl proxy_opts '}' {
429 if (*proxy->host == '\0')
430 yyerror("invalid proxy block: missing `relay-to' option");
432 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
433 (proxy->cert_path != NULL && proxy->key_path == NULL))
434 yyerror("invalid proxy block: missing cert or key");
438 proxy_matches : /* empty */
439 | proxy_matches proxy_match
442 proxy_port : /* empty */ { $$ = 1965; }
443 | PORT STRING {
444 if (($$ = getservice($2)) == -1)
445 yyerror("invalid port number %s", $2);
446 free($2);
448 | PORT NUM { $$ = $2; }
451 proxy_match : PROTO string {
452 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
453 free($2);
455 | FOR_HOST string proxy_port {
456 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
457 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
458 "%d", $3);
459 free($2);
463 proxy_opts : /* empty */
464 | proxy_opts proxy_opt optnl
467 proxy_opt : CERT string {
468 free(proxy->cert);
469 ensure_absolute_path($2);
470 proxy->cert_path = $2;
472 | KEY string {
473 free(proxy->key);
474 ensure_absolute_path($2);
475 proxy->key_path = $2;
477 | PROTOCOLS string {
478 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
479 yyerror("invalid protocols string \"%s\"", $2);
480 free($2);
482 | RELAY_TO string proxy_port {
483 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
484 (void) snprintf(proxy->port, sizeof(proxy->port),
485 "%d", $3);
486 free($2);
488 | REQUIRE CLIENT CA string {
489 ensure_absolute_path($4);
490 proxy->reqca_path = $4;
492 | SNI string {
493 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
494 free($2);
496 | USE_TLS bool {
497 proxy->notls = !$2;
499 | VERIFYNAME bool {
500 proxy->noverifyname = !$2;
504 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
505 /* drop the starting '/' if any */
506 if (*$3 == '/')
507 memmove($3, $3+1, strlen($3));
508 (void) strlcpy(loc->match, $3, sizeof(loc->match));
509 free($3);
511 | error '}'
514 locopts : /* empty */
515 | locopts locopt optnl
518 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
519 | BLOCK RETURN NUM string {
520 check_block_fmt($4);
521 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
522 loc->block_code = check_block_code($3);
523 free($4);
525 | BLOCK RETURN NUM {
526 (void) strlcpy(loc->block_fmt, "temporary failure",
527 sizeof(loc->block_fmt));
528 loc->block_code = check_block_code($3);
529 if ($3 >= 30 && $3 < 40)
530 yyerror("missing `meta' for block return %d", $3);
532 | BLOCK {
533 (void) strlcpy(loc->block_fmt, "temporary failure",
534 sizeof(loc->block_fmt));
535 loc->block_code = 40;
537 | DEFAULT TYPE string {
538 (void) strlcpy(loc->default_mime, $3,
539 sizeof(loc->default_mime));
540 free($3);
542 | fastcgi
543 | INDEX string {
544 (void) strlcpy(loc->index, $2, sizeof(loc->index));
545 free($2);
547 | LANG string {
548 (void) strlcpy(loc->lang, $2,
549 sizeof(loc->lang));
550 free($2);
552 | LOG bool { loc->disable_log = !$2; }
553 | REQUIRE CLIENT CA string {
554 ensure_absolute_path($4);
555 loc->reqca_path = $4;
557 | ROOT string {
558 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
559 free($2);
561 | STRIP NUM { loc->strip = check_strip_no($2); }
564 fastcgi : FASTCGI '{' optnl fastcgiopts '}'
565 | FASTCGI fastcgiopt
566 | FASTCGI OFF {
567 loc->fcgi = -1;
568 loc->nofcgi = 1;
570 | FASTCGI string {
571 yywarn("`fastcgi path' is deprecated. "
572 "Please use `fastcgi socket path' instead.");
573 loc->fcgi = fastcgi_conf($2, NULL);
574 free($2);
578 fastcgiopts : /* empty */
579 | fastcgiopts fastcgiopt optnl
582 fastcgiopt : PARAM string '=' string {
583 add_param($2, $4);
585 | SOCKET string {
586 loc->fcgi = fastcgi_conf($2, NULL);
587 free($2);
589 | SOCKET TCP string PORT NUM {
590 char *c;
592 if (asprintf(&c, "%d", $5) == -1)
593 fatal("asprintf");
594 loc->fcgi = fastcgi_conf($3, c);
595 free($3);
596 free(c);
598 | SOCKET TCP string {
599 loc->fcgi = fastcgi_conf($3, "9000");
601 | SOCKET TCP string PORT string {
602 loc->fcgi = fastcgi_conf($3, $5);
603 free($3);
604 free($5);
606 | STRIP NUM {
607 loc->fcgi_strip = $2;
611 types : TYPES '{' optnl mediaopts_l '}' ;
613 mediaopts_l : mediaopts_l mediaoptsl nl
614 | mediaoptsl nl
617 mediaoptsl : STRING {
618 free(current_media);
619 current_media = $1;
620 } medianames_l
621 | include
624 medianames_l : medianames_l medianamesl
625 | medianamesl
628 medianamesl : numberstring {
629 if (add_mime(&conf->mime, current_media, $1) == -1)
630 fatal("add_mime");
631 free($1);
635 nl : '\n' optnl
636 | ';' optnl
639 optnl : nl
640 | /*empty*/
643 %%
645 static const struct keyword {
646 const char *word;
647 int token;
648 } keywords[] = {
649 /* these MUST be sorted */
650 {"access", ACCESS},
651 {"alias", ALIAS},
652 {"auto", AUTO},
653 {"block", BLOCK},
654 {"ca", CA},
655 {"cert", CERT},
656 {"chroot", CHROOT},
657 {"client", CLIENT},
658 {"default", DEFAULT},
659 {"facility", FACILITY},
660 {"fastcgi", FASTCGI},
661 {"for-host", FOR_HOST},
662 {"include", INCLUDE},
663 {"index", INDEX},
664 {"ipv6", IPV6},
665 {"key", KEY},
666 {"lang", LANG},
667 {"listen", LISTEN},
668 {"location", LOCATION},
669 {"log", LOG},
670 {"ocsp", OCSP},
671 {"off", OFF},
672 {"on", ON},
673 {"param", PARAM},
674 {"port", PORT},
675 {"prefork", PREFORK},
676 {"proto", PROTO},
677 {"protocols", PROTOCOLS},
678 {"proxy", PROXY},
679 {"relay-to", RELAY_TO},
680 {"require", REQUIRE},
681 {"return", RETURN},
682 {"root", ROOT},
683 {"server", SERVER},
684 {"sni", SNI},
685 {"socket", SOCKET},
686 {"strip", STRIP},
687 {"style", STYLE},
688 {"syslog", SYSLOG},
689 {"tcp", TCP},
690 {"to-ext", TOEXT},
691 {"type", TYPE},
692 {"types", TYPES},
693 {"use-tls", USE_TLS},
694 {"user", USER},
695 {"verifyname", VERIFYNAME},
696 };
698 void
699 yyerror(const char *msg, ...)
701 va_list ap;
703 file->errors++;
705 va_start(ap, msg);
706 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
707 vfprintf(stderr, msg, ap);
708 fprintf(stderr, "\n");
709 va_end(ap);
712 void
713 yywarn(const char *msg, ...)
715 va_list ap;
717 va_start(ap, msg);
718 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
719 vfprintf(stderr, msg, ap);
720 fprintf(stderr, "\n");
721 va_end(ap);
724 int
725 kw_cmp(const void *k, const void *e)
727 return strcmp(k, ((struct keyword *)e)->word);
730 int
731 lookup(char *s)
733 const struct keyword *p;
735 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
736 sizeof(keywords[0]), kw_cmp);
738 if (p)
739 return p->token;
740 else
741 return STRING;
744 #define START_EXPAND 1
745 #define DONE_EXPAND 2
747 static int expanding;
749 int
750 igetc(void)
752 int c;
754 while (1) {
755 if (file->ungetpos > 0)
756 c = file->ungetbuf[--file->ungetpos];
757 else
758 c = getc(file->stream);
760 if (c == START_EXPAND)
761 expanding = 1;
762 else if (c == DONE_EXPAND)
763 expanding = 0;
764 else
765 break;
767 return c;
770 int
771 lgetc(int quotec)
773 int c, next;
775 if (quotec) {
776 if ((c = igetc()) == EOF) {
777 yyerror("reached end of file while parsing "
778 "quoted string");
779 if (file == topfile || popfile() == EOF)
780 return EOF;
781 return quotec;
783 return c;
786 while ((c = igetc()) == '\\') {
787 next = igetc();
788 if (next != '\n') {
789 c = next;
790 break;
792 yylval.lineno = file->lineno;
793 file->lineno++;
796 if (c == EOF) {
797 /*
798 * Fake EOL when hit EOF for the first time. This gets line
799 * count right if last line in included file is syntactically
800 * invalid and has no newline.
801 */
802 if (file->eof_reached == 0) {
803 file->eof_reached = 1;
804 return '\n';
806 while (c == EOF) {
807 if (file == topfile || popfile() == EOF)
808 return EOF;
809 c = igetc();
812 return c;
815 void
816 lungetc(int c)
818 if (c == EOF)
819 return;
821 if (file->ungetpos >= file->ungetsize) {
822 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
823 if (p == NULL)
824 fatal("lungetc");
825 file->ungetbuf = p;
826 file->ungetsize *= 2;
828 file->ungetbuf[file->ungetpos++] = c;
831 int
832 findeol(void)
834 int c;
836 /* Skip to either EOF or the first real EOL. */
837 while (1) {
838 c = lgetc(0);
839 if (c == '\n') {
840 file->lineno++;
841 break;
843 if (c == EOF)
844 break;
846 return ERROR;
849 int
850 yylex(void)
852 char buf[8096];
853 char *p, *val;
854 int quotec, next, c;
855 int token;
857 top:
858 p = buf;
859 while ((c = lgetc(0)) == ' ' || c == '\t')
860 ; /* nothing */
862 yylval.lineno = file->lineno;
863 if (c == '#')
864 while ((c = lgetc(0)) != '\n' && c != EOF)
865 ; /* nothing */
866 if (c == '$' && !expanding) {
867 while (1) {
868 if ((c = lgetc(0)) == EOF)
869 return 0;
870 if (p + 1 >= buf + sizeof(buf) -1) {
871 yyerror("string too long");
872 return findeol();
874 if (isalnum(c) || c == '_') {
875 *p++ = c;
876 continue;
878 *p = '\0';
879 lungetc(c);
880 break;
882 val = symget(buf);
883 if (val == NULL) {
884 yyerror("macro `%s' not defined", buf);
885 return findeol();
887 yylval.v.string = xstrdup(val);
888 return STRING;
890 if (c == '@' && !expanding) {
891 while (1) {
892 if ((c = lgetc(0)) == EOF)
893 return 0;
895 if (p + 1 >= buf + sizeof(buf) - 1) {
896 yyerror("string too long");
897 return findeol();
899 if (isalnum(c) || c == '_') {
900 *p++ = c;
901 continue;
903 *p = '\0';
904 lungetc(c);
905 break;
907 val = symget(buf);
908 if (val == NULL) {
909 yyerror("macro '%s' not defined", buf);
910 return findeol();
912 p = val + strlen(val) - 1;
913 lungetc(DONE_EXPAND);
914 while (p >= val) {
915 lungetc(*p);
916 p--;
918 lungetc(START_EXPAND);
919 goto top;
922 switch (c) {
923 case '\'':
924 case '"':
925 quotec = c;
926 while (1) {
927 if ((c = lgetc(quotec)) == EOF)
928 return 0;
929 if (c == '\n') {
930 file->lineno++;
931 continue;
932 } else if (c == '\\') {
933 if ((next = lgetc(quotec)) == EOF)
934 return (0);
935 if (next == quotec || next == ' ' ||
936 next == '\t')
937 c = next;
938 else if (next == '\n') {
939 file->lineno++;
940 continue;
941 } else
942 lungetc(next);
943 } else if (c == quotec) {
944 *p = '\0';
945 break;
946 } else if (c == '\0') {
947 yyerror("invalid syntax");
948 return findeol();
950 if (p + 1 >= buf + sizeof(buf) - 1) {
951 yyerror("string too long");
952 return findeol();
954 *p++ = c;
956 yylval.v.string = strdup(buf);
957 if (yylval.v.string == NULL)
958 fatal("yylex: strdup");
959 return STRING;
962 #define allowed_to_end_number(x) \
963 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
965 if (c == '-' || isdigit(c)) {
966 do {
967 *p++ = c;
968 if ((size_t)(p-buf) >= sizeof(buf)) {
969 yyerror("string too long");
970 return findeol();
972 } while ((c = lgetc(0)) != EOF && isdigit(c));
973 lungetc(c);
974 if (p == buf + 1 && buf[0] == '-')
975 goto nodigits;
976 if (c == EOF || allowed_to_end_number(c)) {
977 const char *errstr = NULL;
979 *p = '\0';
980 yylval.v.number = strtonum(buf, LLONG_MIN,
981 LLONG_MAX, &errstr);
982 if (errstr) {
983 yyerror("\"%s\" invalid number: %s",
984 buf, errstr);
985 return findeol();
987 return NUM;
988 } else {
989 nodigits:
990 while (p > buf + 1)
991 lungetc(*--p);
992 c = *--p;
993 if (c == '-')
994 return c;
998 #define allowed_in_string(x) \
999 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1000 x != '{' && x != '}' && \
1001 x != '!' && x != '=' && x != '#' && \
1002 x != ',' && x != ';'))
1004 if (isalnum(c) || c == ':' || c == '_') {
1005 do {
1006 *p++ = c;
1007 if ((size_t)(p-buf) >= sizeof(buf)) {
1008 yyerror("string too long");
1009 return findeol();
1011 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
1012 lungetc(c);
1013 *p = '\0';
1014 if ((token = lookup(buf)) == STRING)
1015 yylval.v.string = xstrdup(buf);
1016 return token;
1018 if (c == '\n') {
1019 yylval.lineno = file->lineno;
1020 file->lineno++;
1022 if (c == EOF)
1023 return 0;
1024 return c;
1027 struct file *
1028 pushfile(const char *name, int secret)
1030 struct file *nfile;
1032 nfile = xcalloc(1, sizeof(*nfile));
1033 nfile->name = xstrdup(name);
1034 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
1035 log_warn("can't open %s", nfile->name);
1036 free(nfile->name);
1037 free(nfile);
1038 return NULL;
1040 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
1041 nfile->ungetsize = 16;
1042 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
1043 TAILQ_INSERT_TAIL(&files, nfile, entry);
1044 return nfile;
1047 int
1048 popfile(void)
1050 struct file *prev;
1052 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
1053 prev->errors += file->errors;
1055 TAILQ_REMOVE(&files, file, entry);
1056 fclose(file->stream);
1057 free(file->name);
1058 free(file->ungetbuf);
1059 free(file);
1060 file = prev;
1061 return file ? 0 : EOF;
1064 int
1065 parse_conf(struct conf *c, const char *filename)
1067 struct sym *sym, *next;
1069 default_host = NULL;
1070 default_port = 1965;
1072 conf = c;
1074 file = pushfile(filename, 0);
1075 if (file == NULL)
1076 return -1;
1077 topfile = file;
1079 yyparse();
1080 errors = file->errors;
1081 popfile();
1083 /* Free macros and check which have not been used. */
1084 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1085 /* TODO: warn if !sym->used */
1086 if (!sym->persist) {
1087 free(sym->name);
1088 free(sym->val);
1089 TAILQ_REMOVE(&symhead, sym, entry);
1090 free(sym);
1094 if (errors)
1095 return -1;
1096 return 0;
1099 int
1100 symset(const char *name, const char *val, int persist)
1102 struct sym *sym;
1104 TAILQ_FOREACH(sym, &symhead, entry) {
1105 if (!strcmp(name, sym->name))
1106 break;
1109 if (sym != NULL) {
1110 if (sym->persist)
1111 return 0;
1112 else {
1113 free(sym->name);
1114 free(sym->val);
1115 TAILQ_REMOVE(&symhead, sym, entry);
1116 free(sym);
1120 sym = xcalloc(1, sizeof(*sym));
1121 sym->name = xstrdup(name);
1122 sym->val = xstrdup(val);
1123 sym->used = 0;
1124 sym->persist = persist;
1126 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1127 return 0;
1130 int
1131 cmdline_symset(char *s)
1133 char *sym, *val;
1134 int ret;
1136 if ((val = strrchr(s, '=')) == NULL)
1137 return -1;
1138 sym = xcalloc(1, val - s + 1);
1139 memcpy(sym, s, val - s);
1140 ret = symset(sym, val + 1, 1);
1141 free(sym);
1142 return ret;
1145 char *
1146 symget(const char *nam)
1148 struct sym *sym;
1150 TAILQ_FOREACH(sym, &symhead, entry) {
1151 if (strcmp(nam, sym->name) == 0) {
1152 sym->used = 1;
1153 return sym->val;
1156 return NULL;
1159 char *
1160 ensure_absolute_path(char *path)
1162 if (path == NULL || *path != '/')
1163 yyerror("not an absolute path: %s", path);
1164 return path;
1167 int
1168 check_block_code(int n)
1170 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1171 yyerror("invalid block code %d", n);
1172 return n;
1175 char *
1176 check_block_fmt(char *fmt)
1178 char *s;
1180 for (s = fmt; *s; ++s) {
1181 if (*s != '%')
1182 continue;
1183 switch (*++s) {
1184 case '%':
1185 case 'p':
1186 case 'q':
1187 case 'P':
1188 case 'N':
1189 break;
1190 default:
1191 yyerror("invalid format specifier %%%c", *s);
1195 return fmt;
1198 int
1199 check_strip_no(int n)
1201 if (n <= 0)
1202 yyerror("invalid strip number %d", n);
1203 return n;
1206 int
1207 check_port_num(int n)
1209 if (n <= 0 || n >= UINT16_MAX)
1210 yyerror("port number is %s: %d",
1211 n <= 0 ? "too small" : "too large",
1212 n);
1213 return n;
1216 int
1217 check_prefork_num(int n)
1219 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1220 yyerror("invalid prefork number %d", n);
1221 return n;
1224 void
1225 advance_loc(void)
1227 loc = new_location();
1228 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1231 void
1232 advance_proxy(void)
1234 proxy = new_proxy();
1235 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1238 void
1239 parsehp(char *str, char **host, const char **port, const char *def)
1241 char *at;
1242 const char *errstr;
1244 *host = str;
1246 if ((at = strchr(str, ':')) != NULL) {
1247 *at++ = '\0';
1248 *port = at;
1249 } else
1250 *port = def;
1252 strtonum(*port, 1, UINT16_MAX, &errstr);
1253 if (errstr != NULL)
1254 yyerror("port is %s: %s", errstr, *port);
1257 int
1258 fastcgi_conf(const char *path, const char *port)
1260 struct fcgi *f;
1261 int i = 0;
1263 TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1264 if (!strcmp(f->path, path) &&
1265 ((port == NULL && *f->port == '\0') ||
1266 !strcmp(f->port, port)))
1267 return i;
1268 ++i;
1271 f = xcalloc(1, sizeof(*f));
1272 f->id = i;
1273 (void)strlcpy(f->path, path, sizeof(f->path));
1274 if (port != NULL)
1275 (void)strlcpy(f->port, port, sizeof(f->port));
1276 TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1278 return f->id;
1281 void
1282 add_param(char *name, char *val)
1284 struct envlist *e;
1285 struct envhead *h = &loc->params;
1287 e = xcalloc(1, sizeof(*e));
1288 (void) strlcpy(e->name, name, sizeof(e->name));
1289 (void) strlcpy(e->value, val, sizeof(e->value));
1290 TAILQ_INSERT_TAIL(h, e, envs);
1293 int
1294 getservice(const char *n)
1296 struct servent *s;
1297 const char *errstr;
1298 long long llval;
1300 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1301 if (errstr) {
1302 s = getservbyname(n, "tcp");
1303 if (s == NULL)
1304 s = getservbyname(n, "udp");
1305 if (s == NULL)
1306 return (-1);
1307 return (ntohs(s->s_port));
1310 return ((unsigned short)llval);
1313 static void
1314 add_to_addr_queue(struct addrhead *a, struct addrinfo *ai)
1316 struct address *addr;
1317 struct sockaddr_in *sin;
1318 struct sockaddr_in6 *sin6;
1320 if (ai->ai_addrlen > sizeof(addr->ss))
1321 fatalx("ai_addrlen larger than a sockaddr_storage");
1323 TAILQ_FOREACH(addr, a, addrs) {
1324 if (addr->ai_flags == ai->ai_flags &&
1325 addr->ai_family == ai->ai_family &&
1326 addr->ai_socktype == ai->ai_socktype &&
1327 addr->ai_protocol == ai->ai_protocol &&
1328 addr->slen == ai->ai_addrlen &&
1329 !memcmp(&addr->ss, ai->ai_addr, addr->slen))
1330 return;
1333 addr = xcalloc(1, sizeof(*addr));
1334 addr->ai_flags = ai->ai_flags;
1335 addr->ai_family = ai->ai_family;
1336 addr->ai_socktype = ai->ai_socktype;
1337 addr->ai_protocol = ai->ai_protocol;
1338 addr->slen = ai->ai_addrlen;
1339 memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
1341 /* for commodity */
1342 switch (addr->ai_family) {
1343 case AF_INET:
1344 sin = (struct sockaddr_in *)&addr->ss;
1345 addr->port = ntohs(sin->sin_port);
1346 break;
1347 case AF_INET6:
1348 sin6 = (struct sockaddr_in6 *)&addr->ss;
1349 addr->port = ntohs(sin6->sin6_port);
1350 break;
1351 default:
1352 fatalx("unknown socket family %d", addr->ai_family);
1355 addr->sock = -1;
1357 TAILQ_INSERT_HEAD(a, addr, addrs);
1360 void
1361 listen_on(const char *hostname, const char *servname)
1363 struct addrinfo hints, *res, *res0;
1364 int error;
1366 memset(&hints, 0, sizeof(hints));
1367 hints.ai_family = AF_UNSPEC;
1368 hints.ai_socktype = SOCK_STREAM;
1369 hints.ai_flags = AI_PASSIVE;
1370 error = getaddrinfo(hostname, servname, &hints, &res0);
1371 if (error) {
1372 yyerror("listen on \"%s\" port %s: %s", hostname, servname,
1373 gai_strerror(errno));
1374 return;
1377 for (res = res0; res; res = res->ai_next) {
1378 add_to_addr_queue(&host->addrs, res);
1379 add_to_addr_queue(&conf->addrs, res);
1382 freeaddrinfo(res0);