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_prefork_num(int);
96 void advance_loc(void);
97 void advance_proxy(void);
98 int fastcgi_conf(const char *, const char *);
99 void add_param(char *, char *);
100 int getservice(const char *);
101 void listen_on(const char *, const char *);
103 static struct vhost *host;
104 static struct location *loc;
105 static struct proxy *proxy;
106 static char *current_media;
107 static int errors;
109 typedef struct {
110 union {
111 char *string;
112 int number;
113 } v;
114 int lineno;
115 } YYSTYPE;
117 #define YYSTYPE YYSTYPE
119 %}
121 /* for bison: */
122 /* %define parse.error verbose */
124 %token ACCESS ALIAS AUTO
125 %token BLOCK
126 %token CA CERT CHROOT CLIENT
127 %token DEFAULT
128 %token FACILITY FASTCGI FOR_HOST
129 %token INCLUDE INDEX IPV6
130 %token KEY
131 %token LANG LISTEN LOCATION LOG
132 %token OCSP OFF ON
133 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
134 %token RELAY_TO REQUIRE RETURN ROOT
135 %token SERVER SNI SOCKET STRIP STYLE SYSLOG
136 %token TCP TOEXT TYPE TYPES
137 %token USE_TLS USER
138 %token VERIFYNAME
140 %token ERROR
142 %token <v.string> STRING
143 %token <v.number> NUM
145 %type <v.number> bool proxy_port
146 %type <v.string> string numberstring listen_addr
148 %%
150 conf : /* empty */
151 | conf include nl
152 | conf varset nl
153 | conf option nl
154 | conf vhost nl
155 | conf types nl
156 | conf error nl { file->errors++; }
159 include : INCLUDE STRING {
160 struct file *nfile;
162 if ((nfile = pushfile($2, 0)) == NULL) {
163 yyerror("failed to include file %s", $2);
164 free($2);
165 YYERROR;
167 free($2);
169 file = nfile;
170 lungetc('\n');
174 bool : ON { $$ = 1; }
175 | OFF { $$ = 0; }
178 string : string STRING {
179 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
180 free($1);
181 free($2);
182 yyerror("string: asprintf: %s", strerror(errno));
183 YYERROR;
185 free($1);
186 free($2);
188 | STRING
191 numberstring : NUM {
192 char *s;
193 if (asprintf(&s, "%d", $1) == -1) {
194 yyerror("asprintf: number");
195 YYERROR;
197 $$ = s;
199 | STRING
202 varset : STRING '=' string {
203 char *s = $1;
204 while (*s++) {
205 if (isspace((unsigned char)*s)) {
206 yyerror("macro name cannot contain "
207 "whitespaces");
208 free($1);
209 free($3);
210 YYERROR;
213 symset($1, $3, 0);
214 free($1);
215 free($3);
219 option : CHROOT string {
220 if (strlcpy(conf->chroot, $2, sizeof(conf->chroot)) >=
221 sizeof(conf->chroot))
222 yyerror("chroot path too long");
223 free($2);
225 | IPV6 bool {
226 yywarn("option `ipv6' is deprecated,"
227 " please use `listen on'");
228 if ($2)
229 default_host = NULL;
230 else
231 default_host = "0.0.0.0";
233 | log
234 | PORT NUM {
235 yywarn("option `port' is deprecated,"
236 " please use `listen on'");
237 default_port = $2;
239 | PREFORK NUM { conf->prefork = check_prefork_num($2); }
240 | PROTOCOLS string {
241 if (tls_config_parse_protocols(&conf->protos, $2) == -1)
242 yyerror("invalid protocols string \"%s\"", $2);
243 free($2);
245 | USER string {
246 if (strlcpy(conf->user, $2, sizeof(conf->user)) >=
247 sizeof(conf->user))
248 yyerror("user name too long");
249 free($2);
253 log : LOG '{' optnl logopts '}'
254 | LOG logopt
257 logopts : /* empty */
258 | logopts logopt optnl
261 logopt : ACCESS string {
262 free(conf->log_access);
263 conf->log_access = $2;
265 | STYLE string {
266 if (!strcmp("combined", $2))
267 conf->log_format = LOG_FORMAT_COMBINED;
268 else if (!strcmp("common", $2))
269 conf->log_format = LOG_FORMAT_COMMON;
270 else if (!strcmp("condensed", $2))
271 conf->log_format = LOG_FORMAT_CONDENSED;
272 else if (!strcmp("legacy", $2))
273 conf->log_format = LOG_FORMAT_LEGACY;
274 else
275 yyerror("unknown log style: %s", $2);
276 free($2);
278 | SYSLOG FACILITY string {
279 const char *str = $3;
281 conf->log_syslog = 1;
283 if (!strncasecmp(str, "LOG_", 4))
284 str += 4;
286 if (!strcasecmp(str, "daemon"))
287 conf->log_facility = LOG_DAEMON;
288 #ifdef LOG_FTP
289 else if (!strcasecmp(str, "ftp"))
290 conf->log_facility = LOG_FTP;
291 #endif
292 else if (!strcasecmp(str, "local1"))
293 conf->log_facility = LOG_LOCAL1;
294 else if (!strcasecmp(str, "local2"))
295 conf->log_facility = LOG_LOCAL2;
296 else if (!strcasecmp(str, "local3"))
297 conf->log_facility = LOG_LOCAL3;
298 else if (!strcasecmp(str, "local4"))
299 conf->log_facility = LOG_LOCAL4;
300 else if (!strcasecmp(str, "local5"))
301 conf->log_facility = LOG_LOCAL5;
302 else if (!strcasecmp(str, "local6"))
303 conf->log_facility = LOG_LOCAL6;
304 else if (!strcasecmp(str, "local7"))
305 conf->log_facility = LOG_LOCAL7;
306 else if (!strcasecmp(str, "user"))
307 conf->log_facility = LOG_USER;
308 else
309 yywarn("unknown syslog facility `%s'",
310 $3);
312 free($3);
314 | SYSLOG OFF {
315 conf->log_syslog = 0;
317 | SYSLOG {
318 conf->log_syslog = 1;
322 vhost : SERVER string {
323 host = new_vhost();
324 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
326 loc = new_location();
327 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
329 TAILQ_INIT(&host->proxies);
331 (void) strlcpy(loc->match, "*", sizeof(loc->match));
332 (void) strlcpy(host->domain, $2, sizeof(host->domain));
334 if (strstr($2, "xn--") != NULL) {
335 yywarn("\"%s\" looks like punycode: you "
336 "should use the decoded hostname", $2);
339 free($2);
340 } '{' optnl servbody '}' {
341 if (host->cert_path == NULL ||
342 host->key_path == NULL)
343 yyerror("invalid vhost definition: %s",
344 host->domain);
345 if (TAILQ_EMPTY(&host->addrs)) {
346 char portno[32];
347 int r;
349 r = snprintf(portno, sizeof(portno), "%d",
350 default_port);
351 if (r < 0 || (size_t)r >= sizeof(portno))
352 fatal("snprintf");
354 yywarn("missing `listen on' in server %s,"
355 " assuming %s port %d", host->domain,
356 default_host ? default_host : "*",
357 default_port);
358 listen_on(default_host, portno);
361 | error '}' { yyerror("bad server directive"); }
364 servbody : /* empty */
365 | servbody servopt optnl
366 | servbody location optnl
367 | servbody proxy optnl
370 listen_addr : '*' { $$ = NULL; }
371 | STRING
374 servopt : ALIAS string {
375 struct alist *a;
377 a = xcalloc(1, sizeof(*a));
378 (void) strlcpy(a->alias, $2, sizeof(a->alias));
379 free($2);
380 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
382 | CERT string {
383 ensure_absolute_path($2);
384 free(host->cert_path);
385 host->cert_path = $2;
387 | KEY string {
388 ensure_absolute_path($2);
389 free(host->key_path);
390 host->key_path = $2;
392 | OCSP string {
393 ensure_absolute_path($2);
394 free(host->ocsp_path);
395 host->ocsp_path = $2;
397 | PARAM string '=' string {
398 yywarn("the top-level `param' directive is deprecated."
399 " Please use `fastcgi { param ... }`");
400 add_param($2, $4);
402 | LISTEN ON listen_addr {
403 listen_on($3, "1965");
404 free($3);
406 | LISTEN ON listen_addr PORT STRING {
407 listen_on($3, $5);
408 free($3);
409 free($5);
411 | LISTEN ON listen_addr PORT NUM {
412 char portno[32];
413 int r;
415 r = snprintf(portno, sizeof(portno), "%d", $5);
416 if (r < 0 || (size_t)r >= sizeof(portno))
417 fatal("snprintf");
419 listen_on($3, portno);
420 free($3);
422 | locopt
425 proxy : PROXY { advance_proxy(); }
426 proxy_matches '{' optnl proxy_opts '}' {
427 if (*proxy->host == '\0')
428 yyerror("invalid proxy block: missing `relay-to' option");
430 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
431 (proxy->cert_path != NULL && proxy->key_path == NULL))
432 yyerror("invalid proxy block: missing cert or key");
436 proxy_matches : /* empty */
437 | proxy_matches proxy_match
440 proxy_port : /* empty */ { $$ = 1965; }
441 | PORT STRING {
442 if (($$ = getservice($2)) == -1)
443 yyerror("invalid port number %s", $2);
444 free($2);
446 | PORT NUM { $$ = $2; }
449 proxy_match : PROTO string {
450 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
451 free($2);
453 | FOR_HOST string proxy_port {
454 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
455 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
456 "%d", $3);
457 free($2);
461 proxy_opts : /* empty */
462 | proxy_opts proxy_opt optnl
465 proxy_opt : CERT string {
466 free(proxy->cert);
467 ensure_absolute_path($2);
468 proxy->cert_path = $2;
470 | KEY string {
471 free(proxy->key);
472 ensure_absolute_path($2);
473 proxy->key_path = $2;
475 | PROTOCOLS string {
476 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
477 yyerror("invalid protocols string \"%s\"", $2);
478 free($2);
480 | RELAY_TO string proxy_port {
481 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
482 (void) snprintf(proxy->port, sizeof(proxy->port),
483 "%d", $3);
484 free($2);
486 | REQUIRE CLIENT CA string {
487 ensure_absolute_path($4);
488 proxy->reqca_path = $4;
490 | SNI string {
491 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
492 free($2);
494 | USE_TLS bool {
495 proxy->notls = !$2;
497 | VERIFYNAME bool {
498 proxy->noverifyname = !$2;
502 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
503 /* drop the starting '/' if any */
504 if (*$3 == '/')
505 memmove($3, $3+1, strlen($3));
506 (void) strlcpy(loc->match, $3, sizeof(loc->match));
507 free($3);
509 | error '}'
512 locopts : /* empty */
513 | locopts locopt optnl
516 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
517 | BLOCK RETURN NUM string {
518 check_block_fmt($4);
519 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
520 loc->block_code = check_block_code($3);
521 free($4);
523 | BLOCK RETURN NUM {
524 (void) strlcpy(loc->block_fmt, "temporary failure",
525 sizeof(loc->block_fmt));
526 loc->block_code = check_block_code($3);
527 if ($3 >= 30 && $3 < 40)
528 yyerror("missing `meta' for block return %d", $3);
530 | BLOCK {
531 (void) strlcpy(loc->block_fmt, "temporary failure",
532 sizeof(loc->block_fmt));
533 loc->block_code = 40;
535 | DEFAULT TYPE string {
536 (void) strlcpy(loc->default_mime, $3,
537 sizeof(loc->default_mime));
538 free($3);
540 | fastcgi
541 | INDEX string {
542 (void) strlcpy(loc->index, $2, sizeof(loc->index));
543 free($2);
545 | LANG string {
546 (void) strlcpy(loc->lang, $2,
547 sizeof(loc->lang));
548 free($2);
550 | LOG bool { loc->disable_log = !$2; }
551 | REQUIRE CLIENT CA string {
552 ensure_absolute_path($4);
553 loc->reqca_path = $4;
555 | ROOT string {
556 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
557 free($2);
559 | STRIP NUM { loc->strip = check_strip_no($2); }
562 fastcgi : FASTCGI '{' optnl fastcgiopts '}'
563 | FASTCGI fastcgiopt
564 | FASTCGI OFF {
565 loc->fcgi = -1;
566 loc->nofcgi = 1;
568 | FASTCGI string {
569 yywarn("`fastcgi path' is deprecated. "
570 "Please use `fastcgi socket path' instead.");
571 loc->fcgi = fastcgi_conf($2, NULL);
572 free($2);
576 fastcgiopts : /* empty */
577 | fastcgiopts fastcgiopt optnl
580 fastcgiopt : PARAM string '=' string {
581 add_param($2, $4);
583 | SOCKET string {
584 loc->fcgi = fastcgi_conf($2, NULL);
585 free($2);
587 | SOCKET TCP string PORT NUM {
588 char *c;
590 if (asprintf(&c, "%d", $5) == -1)
591 fatal("asprintf");
592 loc->fcgi = fastcgi_conf($3, c);
593 free($3);
594 free(c);
596 | SOCKET TCP string {
597 loc->fcgi = fastcgi_conf($3, "9000");
599 | SOCKET TCP string PORT string {
600 loc->fcgi = fastcgi_conf($3, $5);
601 free($3);
602 free($5);
604 | STRIP NUM {
605 loc->fcgi_strip = $2;
609 types : TYPES '{' optnl mediaopts_l '}' ;
611 mediaopts_l : mediaopts_l mediaoptsl nl
612 | mediaoptsl nl
615 mediaoptsl : STRING {
616 free(current_media);
617 current_media = $1;
618 } medianames_l
619 | include
622 medianames_l : medianames_l medianamesl
623 | medianamesl
626 medianamesl : numberstring {
627 if (add_mime(&conf->mime, current_media, $1) == -1)
628 fatal("add_mime");
629 free($1);
633 nl : '\n' optnl
634 | ';' optnl
637 optnl : nl
638 | /*empty*/
641 %%
643 static const struct keyword {
644 const char *word;
645 int token;
646 } keywords[] = {
647 /* these MUST be sorted */
648 {"access", ACCESS},
649 {"alias", ALIAS},
650 {"auto", AUTO},
651 {"block", BLOCK},
652 {"ca", CA},
653 {"cert", CERT},
654 {"chroot", CHROOT},
655 {"client", CLIENT},
656 {"default", DEFAULT},
657 {"facility", FACILITY},
658 {"fastcgi", FASTCGI},
659 {"for-host", FOR_HOST},
660 {"include", INCLUDE},
661 {"index", INDEX},
662 {"ipv6", IPV6},
663 {"key", KEY},
664 {"lang", LANG},
665 {"listen", LISTEN},
666 {"location", LOCATION},
667 {"log", LOG},
668 {"ocsp", OCSP},
669 {"off", OFF},
670 {"on", ON},
671 {"param", PARAM},
672 {"port", PORT},
673 {"prefork", PREFORK},
674 {"proto", PROTO},
675 {"protocols", PROTOCOLS},
676 {"proxy", PROXY},
677 {"relay-to", RELAY_TO},
678 {"require", REQUIRE},
679 {"return", RETURN},
680 {"root", ROOT},
681 {"server", SERVER},
682 {"sni", SNI},
683 {"socket", SOCKET},
684 {"strip", STRIP},
685 {"style", STYLE},
686 {"syslog", SYSLOG},
687 {"tcp", TCP},
688 {"to-ext", TOEXT},
689 {"type", TYPE},
690 {"types", TYPES},
691 {"use-tls", USE_TLS},
692 {"user", USER},
693 {"verifyname", VERIFYNAME},
694 };
696 void
697 yyerror(const char *msg, ...)
699 va_list ap;
701 file->errors++;
703 va_start(ap, msg);
704 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
705 vfprintf(stderr, msg, ap);
706 fprintf(stderr, "\n");
707 va_end(ap);
710 void
711 yywarn(const char *msg, ...)
713 va_list ap;
715 va_start(ap, msg);
716 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
717 vfprintf(stderr, msg, ap);
718 fprintf(stderr, "\n");
719 va_end(ap);
722 int
723 kw_cmp(const void *k, const void *e)
725 return strcmp(k, ((struct keyword *)e)->word);
728 int
729 lookup(char *s)
731 const struct keyword *p;
733 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
734 sizeof(keywords[0]), kw_cmp);
736 if (p)
737 return p->token;
738 else
739 return STRING;
742 #define START_EXPAND 1
743 #define DONE_EXPAND 2
745 static int expanding;
747 int
748 igetc(void)
750 int c;
752 while (1) {
753 if (file->ungetpos > 0)
754 c = file->ungetbuf[--file->ungetpos];
755 else
756 c = getc(file->stream);
758 if (c == START_EXPAND)
759 expanding = 1;
760 else if (c == DONE_EXPAND)
761 expanding = 0;
762 else
763 break;
765 return c;
768 int
769 lgetc(int quotec)
771 int c, next;
773 if (quotec) {
774 if ((c = igetc()) == EOF) {
775 yyerror("reached end of file while parsing "
776 "quoted string");
777 if (file == topfile || popfile() == EOF)
778 return EOF;
779 return quotec;
781 return c;
784 while ((c = igetc()) == '\\') {
785 next = igetc();
786 if (next != '\n') {
787 c = next;
788 break;
790 yylval.lineno = file->lineno;
791 file->lineno++;
794 if (c == EOF) {
795 /*
796 * Fake EOL when hit EOF for the first time. This gets line
797 * count right if last line in included file is syntactically
798 * invalid and has no newline.
799 */
800 if (file->eof_reached == 0) {
801 file->eof_reached = 1;
802 return '\n';
804 while (c == EOF) {
805 if (file == topfile || popfile() == EOF)
806 return EOF;
807 c = igetc();
810 return c;
813 void
814 lungetc(int c)
816 if (c == EOF)
817 return;
819 if (file->ungetpos >= file->ungetsize) {
820 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
821 if (p == NULL)
822 fatal("lungetc");
823 file->ungetbuf = p;
824 file->ungetsize *= 2;
826 file->ungetbuf[file->ungetpos++] = c;
829 int
830 findeol(void)
832 int c;
834 /* Skip to either EOF or the first real EOL. */
835 while (1) {
836 c = lgetc(0);
837 if (c == '\n') {
838 file->lineno++;
839 break;
841 if (c == EOF)
842 break;
844 return ERROR;
847 int
848 yylex(void)
850 char buf[8096];
851 char *p, *val;
852 int quotec, next, c;
853 int token;
855 top:
856 p = buf;
857 while ((c = lgetc(0)) == ' ' || c == '\t')
858 ; /* nothing */
860 yylval.lineno = file->lineno;
861 if (c == '#')
862 while ((c = lgetc(0)) != '\n' && c != EOF)
863 ; /* nothing */
864 if (c == '$' && !expanding) {
865 while (1) {
866 if ((c = lgetc(0)) == EOF)
867 return 0;
868 if (p + 1 >= buf + sizeof(buf) -1) {
869 yyerror("string too long");
870 return findeol();
872 if (isalnum(c) || c == '_') {
873 *p++ = c;
874 continue;
876 *p = '\0';
877 lungetc(c);
878 break;
880 val = symget(buf);
881 if (val == NULL) {
882 yyerror("macro `%s' not defined", buf);
883 return findeol();
885 yylval.v.string = xstrdup(val);
886 return STRING;
888 if (c == '@' && !expanding) {
889 while (1) {
890 if ((c = lgetc(0)) == EOF)
891 return 0;
893 if (p + 1 >= buf + sizeof(buf) - 1) {
894 yyerror("string too long");
895 return findeol();
897 if (isalnum(c) || c == '_') {
898 *p++ = c;
899 continue;
901 *p = '\0';
902 lungetc(c);
903 break;
905 val = symget(buf);
906 if (val == NULL) {
907 yyerror("macro '%s' not defined", buf);
908 return findeol();
910 p = val + strlen(val) - 1;
911 lungetc(DONE_EXPAND);
912 while (p >= val) {
913 lungetc(*p);
914 p--;
916 lungetc(START_EXPAND);
917 goto top;
920 switch (c) {
921 case '\'':
922 case '"':
923 quotec = c;
924 while (1) {
925 if ((c = lgetc(quotec)) == EOF)
926 return 0;
927 if (c == '\n') {
928 file->lineno++;
929 continue;
930 } else if (c == '\\') {
931 if ((next = lgetc(quotec)) == EOF)
932 return (0);
933 if (next == quotec || next == ' ' ||
934 next == '\t')
935 c = next;
936 else if (next == '\n') {
937 file->lineno++;
938 continue;
939 } else
940 lungetc(next);
941 } else if (c == quotec) {
942 *p = '\0';
943 break;
944 } else if (c == '\0') {
945 yyerror("invalid syntax");
946 return findeol();
948 if (p + 1 >= buf + sizeof(buf) - 1) {
949 yyerror("string too long");
950 return findeol();
952 *p++ = c;
954 yylval.v.string = strdup(buf);
955 if (yylval.v.string == NULL)
956 fatal("yylex: strdup");
957 return STRING;
960 #define allowed_to_end_number(x) \
961 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
963 if (c == '-' || isdigit(c)) {
964 do {
965 *p++ = c;
966 if ((size_t)(p-buf) >= sizeof(buf)) {
967 yyerror("string too long");
968 return findeol();
970 } while ((c = lgetc(0)) != EOF && isdigit(c));
971 lungetc(c);
972 if (p == buf + 1 && buf[0] == '-')
973 goto nodigits;
974 if (c == EOF || allowed_to_end_number(c)) {
975 const char *errstr = NULL;
977 *p = '\0';
978 yylval.v.number = strtonum(buf, LLONG_MIN,
979 LLONG_MAX, &errstr);
980 if (errstr) {
981 yyerror("\"%s\" invalid number: %s",
982 buf, errstr);
983 return findeol();
985 return NUM;
986 } else {
987 nodigits:
988 while (p > buf + 1)
989 lungetc(*--p);
990 c = *--p;
991 if (c == '-')
992 return c;
996 #define allowed_in_string(x) \
997 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
998 x != '{' && x != '}' && \
999 x != '!' && x != '=' && x != '#' && \
1000 x != ',' && x != ';'))
1002 if (isalnum(c) || c == ':' || c == '_') {
1003 do {
1004 *p++ = c;
1005 if ((size_t)(p-buf) >= sizeof(buf)) {
1006 yyerror("string too long");
1007 return findeol();
1009 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
1010 lungetc(c);
1011 *p = '\0';
1012 if ((token = lookup(buf)) == STRING)
1013 yylval.v.string = xstrdup(buf);
1014 return token;
1016 if (c == '\n') {
1017 yylval.lineno = file->lineno;
1018 file->lineno++;
1020 if (c == EOF)
1021 return 0;
1022 return c;
1025 struct file *
1026 pushfile(const char *name, int secret)
1028 struct file *nfile;
1030 nfile = xcalloc(1, sizeof(*nfile));
1031 nfile->name = xstrdup(name);
1032 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
1033 log_warn("can't open %s", nfile->name);
1034 free(nfile->name);
1035 free(nfile);
1036 return NULL;
1038 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
1039 nfile->ungetsize = 16;
1040 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
1041 TAILQ_INSERT_TAIL(&files, nfile, entry);
1042 return nfile;
1045 int
1046 popfile(void)
1048 struct file *prev;
1050 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
1051 prev->errors += file->errors;
1053 TAILQ_REMOVE(&files, file, entry);
1054 fclose(file->stream);
1055 free(file->name);
1056 free(file->ungetbuf);
1057 free(file);
1058 file = prev;
1059 return file ? 0 : EOF;
1062 int
1063 parse_conf(struct conf *c, const char *filename)
1065 struct sym *sym, *next;
1067 default_host = NULL;
1068 default_port = 1965;
1070 conf = c;
1072 file = pushfile(filename, 0);
1073 if (file == NULL)
1074 return -1;
1075 topfile = file;
1077 yyparse();
1078 errors = file->errors;
1079 popfile();
1081 /* Free macros and check which have not been used. */
1082 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1083 /* TODO: warn if !sym->used */
1084 if (!sym->persist) {
1085 free(sym->name);
1086 free(sym->val);
1087 TAILQ_REMOVE(&symhead, sym, entry);
1088 free(sym);
1092 if (errors)
1093 return -1;
1094 return 0;
1097 int
1098 symset(const char *name, const char *val, int persist)
1100 struct sym *sym;
1102 TAILQ_FOREACH(sym, &symhead, entry) {
1103 if (!strcmp(name, sym->name))
1104 break;
1107 if (sym != NULL) {
1108 if (sym->persist)
1109 return 0;
1110 else {
1111 free(sym->name);
1112 free(sym->val);
1113 TAILQ_REMOVE(&symhead, sym, entry);
1114 free(sym);
1118 sym = xcalloc(1, sizeof(*sym));
1119 sym->name = xstrdup(name);
1120 sym->val = xstrdup(val);
1121 sym->used = 0;
1122 sym->persist = persist;
1124 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1125 return 0;
1128 int
1129 cmdline_symset(char *s)
1131 char *sym, *val;
1132 int ret;
1134 if ((val = strrchr(s, '=')) == NULL)
1135 return -1;
1136 sym = xcalloc(1, val - s + 1);
1137 memcpy(sym, s, val - s);
1138 ret = symset(sym, val + 1, 1);
1139 free(sym);
1140 return ret;
1143 char *
1144 symget(const char *nam)
1146 struct sym *sym;
1148 TAILQ_FOREACH(sym, &symhead, entry) {
1149 if (strcmp(nam, sym->name) == 0) {
1150 sym->used = 1;
1151 return sym->val;
1154 return NULL;
1157 char *
1158 ensure_absolute_path(char *path)
1160 if (path == NULL || *path != '/')
1161 yyerror("not an absolute path: %s", path);
1162 return path;
1165 int
1166 check_block_code(int n)
1168 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1169 yyerror("invalid block code %d", n);
1170 return n;
1173 char *
1174 check_block_fmt(char *fmt)
1176 char *s;
1178 for (s = fmt; *s; ++s) {
1179 if (*s != '%')
1180 continue;
1181 switch (*++s) {
1182 case '%':
1183 case 'p':
1184 case 'q':
1185 case 'P':
1186 case 'N':
1187 break;
1188 default:
1189 yyerror("invalid format specifier %%%c", *s);
1193 return fmt;
1196 int
1197 check_strip_no(int n)
1199 if (n <= 0)
1200 yyerror("invalid strip number %d", n);
1201 return n;
1204 int
1205 check_prefork_num(int n)
1207 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1208 yyerror("invalid prefork number %d", n);
1209 return n;
1212 void
1213 advance_loc(void)
1215 loc = new_location();
1216 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1219 void
1220 advance_proxy(void)
1222 proxy = new_proxy();
1223 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1226 int
1227 fastcgi_conf(const char *path, const char *port)
1229 struct fcgi *f;
1230 int i = 0;
1232 TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1233 if (!strcmp(f->path, path) &&
1234 ((port == NULL && *f->port == '\0') ||
1235 !strcmp(f->port, port)))
1236 return i;
1237 ++i;
1240 f = xcalloc(1, sizeof(*f));
1241 f->id = i;
1242 (void)strlcpy(f->path, path, sizeof(f->path));
1243 if (port != NULL)
1244 (void)strlcpy(f->port, port, sizeof(f->port));
1245 TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1247 return f->id;
1250 void
1251 add_param(char *name, char *val)
1253 struct envlist *e;
1254 struct envhead *h = &loc->params;
1256 e = xcalloc(1, sizeof(*e));
1257 (void) strlcpy(e->name, name, sizeof(e->name));
1258 (void) strlcpy(e->value, val, sizeof(e->value));
1259 TAILQ_INSERT_TAIL(h, e, envs);
1262 int
1263 getservice(const char *n)
1265 struct servent *s;
1266 const char *errstr;
1267 long long llval;
1269 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1270 if (errstr) {
1271 s = getservbyname(n, "tcp");
1272 if (s == NULL)
1273 s = getservbyname(n, "udp");
1274 if (s == NULL)
1275 return (-1);
1276 return (ntohs(s->s_port));
1279 return ((unsigned short)llval);
1282 static void
1283 add_to_addr_queue(struct addrhead *a, struct addrinfo *ai)
1285 struct address *addr;
1286 struct sockaddr_in *sin;
1287 struct sockaddr_in6 *sin6;
1289 if (ai->ai_addrlen > sizeof(addr->ss))
1290 fatalx("ai_addrlen larger than a sockaddr_storage");
1292 TAILQ_FOREACH(addr, a, addrs) {
1293 if (addr->ai_flags == ai->ai_flags &&
1294 addr->ai_family == ai->ai_family &&
1295 addr->ai_socktype == ai->ai_socktype &&
1296 addr->ai_protocol == ai->ai_protocol &&
1297 addr->slen == ai->ai_addrlen &&
1298 !memcmp(&addr->ss, ai->ai_addr, addr->slen))
1299 return;
1302 addr = xcalloc(1, sizeof(*addr));
1303 addr->ai_flags = ai->ai_flags;
1304 addr->ai_family = ai->ai_family;
1305 addr->ai_socktype = ai->ai_socktype;
1306 addr->ai_protocol = ai->ai_protocol;
1307 addr->slen = ai->ai_addrlen;
1308 memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
1310 /* for commodity */
1311 switch (addr->ai_family) {
1312 case AF_INET:
1313 sin = (struct sockaddr_in *)&addr->ss;
1314 addr->port = ntohs(sin->sin_port);
1315 break;
1316 case AF_INET6:
1317 sin6 = (struct sockaddr_in6 *)&addr->ss;
1318 addr->port = ntohs(sin6->sin6_port);
1319 break;
1320 default:
1321 fatalx("unknown socket family %d", addr->ai_family);
1324 addr->sock = -1;
1326 TAILQ_INSERT_HEAD(a, addr, addrs);
1329 void
1330 listen_on(const char *hostname, const char *servname)
1332 struct addrinfo hints, *res, *res0;
1333 int error;
1335 memset(&hints, 0, sizeof(hints));
1336 hints.ai_family = AF_UNSPEC;
1337 hints.ai_socktype = SOCK_STREAM;
1338 hints.ai_flags = AI_PASSIVE;
1339 error = getaddrinfo(hostname, servname, &hints, &res0);
1340 if (error) {
1341 yyerror("listen on \"%s\" port %s: %s", hostname, servname,
1342 gai_strerror(errno));
1343 return;
1346 for (res = res0; res; res = res->ai_next) {
1347 add_to_addr_queue(&host->addrs, res);
1348 add_to_addr_queue(&conf->addrs, res);
1351 freeaddrinfo(res0);