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>
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 COMBINED COMMON CONDENSED
129 %token DEFAULT
130 %token FACILITY FASTCGI FOR_HOST
131 %token INCLUDE INDEX IPV6
132 %token KEY
133 %token LANG LEGACY 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 COMMON {
268 conf->log_format = LOG_FORMAT_COMMON;
270 | STYLE COMBINED {
271 conf->log_format = LOG_FORMAT_COMBINED;
273 | STYLE CONDENSED {
274 conf->log_format = LOG_FORMAT_CONDENSED;
276 | STYLE LEGACY {
277 conf->log_format = LOG_FORMAT_LEGACY;
279 | SYSLOG FACILITY string {
280 const char *str = $3;
282 conf->log_syslog = 1;
284 if (!strncasecmp(str, "LOG_", 4))
285 str += 4;
287 if (!strcasecmp(str, "daemon"))
288 conf->log_facility = LOG_DAEMON;
289 #ifdef LOG_FTP
290 else if (!strcasecmp(str, "ftp"))
291 conf->log_facility = LOG_FTP;
292 #endif
293 else if (!strcasecmp(str, "local1"))
294 conf->log_facility = LOG_LOCAL1;
295 else if (!strcasecmp(str, "local2"))
296 conf->log_facility = LOG_LOCAL2;
297 else if (!strcasecmp(str, "local3"))
298 conf->log_facility = LOG_LOCAL3;
299 else if (!strcasecmp(str, "local4"))
300 conf->log_facility = LOG_LOCAL4;
301 else if (!strcasecmp(str, "local5"))
302 conf->log_facility = LOG_LOCAL5;
303 else if (!strcasecmp(str, "local6"))
304 conf->log_facility = LOG_LOCAL6;
305 else if (!strcasecmp(str, "local7"))
306 conf->log_facility = LOG_LOCAL7;
307 else if (!strcasecmp(str, "user"))
308 conf->log_facility = LOG_USER;
309 else
310 yywarn("unknown syslog facility `%s'",
311 $3);
313 free($3);
315 | SYSLOG OFF {
316 conf->log_syslog = 0;
318 | SYSLOG {
319 conf->log_syslog = 1;
323 vhost : SERVER string {
324 host = new_vhost();
325 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
327 loc = new_location();
328 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
330 TAILQ_INIT(&host->proxies);
332 (void) strlcpy(loc->match, "*", sizeof(loc->match));
333 (void) strlcpy(host->domain, $2, sizeof(host->domain));
335 if (strstr($2, "xn--") != NULL) {
336 yywarn("\"%s\" looks like punycode: you "
337 "should use the decoded hostname", $2);
340 free($2);
341 } '{' optnl servbody '}' {
342 if (host->cert_path == NULL ||
343 host->key_path == NULL)
344 yyerror("invalid vhost definition: %s",
345 host->domain);
346 if (TAILQ_EMPTY(&host->addrs)) {
347 char portno[32];
348 int r;
350 r = snprintf(portno, sizeof(portno), "%d",
351 default_port);
352 if (r < 0 || (size_t)r >= sizeof(portno))
353 fatal("snprintf");
355 yywarn("missing `listen on' in server %s,"
356 " assuming %s port %d", host->domain,
357 default_host ? default_host : "*",
358 default_port);
359 listen_on(default_host, portno);
362 | error '}' { yyerror("bad server directive"); }
365 servbody : /* empty */
366 | servbody servopt optnl
367 | servbody location optnl
368 | servbody proxy optnl
371 listen_addr : '*' { $$ = NULL; }
372 | STRING
375 servopt : ALIAS string {
376 struct alist *a;
378 a = xcalloc(1, sizeof(*a));
379 (void) strlcpy(a->alias, $2, sizeof(a->alias));
380 free($2);
381 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
383 | CERT string {
384 ensure_absolute_path($2);
385 free(host->cert_path);
386 host->cert_path = $2;
388 | KEY string {
389 ensure_absolute_path($2);
390 free(host->key_path);
391 host->key_path = $2;
393 | OCSP string {
394 ensure_absolute_path($2);
395 free(host->ocsp_path);
396 host->ocsp_path = $2;
398 | PARAM string '=' string {
399 yywarn("the top-level `param' directive is deprecated."
400 " Please use `fastcgi { param ... }`");
401 add_param($2, $4);
403 | LISTEN ON listen_addr {
404 listen_on($3, "1965");
405 free($3);
407 | LISTEN ON listen_addr PORT STRING {
408 listen_on($3, $5);
409 free($3);
410 free($5);
412 | LISTEN ON listen_addr PORT NUM {
413 char portno[32];
414 int r;
416 r = snprintf(portno, sizeof(portno), "%d", $5);
417 if (r < 0 || (size_t)r >= sizeof(portno))
418 fatal("snprintf");
420 listen_on($3, portno);
421 free($3);
423 | locopt
426 proxy : PROXY { advance_proxy(); }
427 proxy_matches '{' optnl proxy_opts '}' {
428 if (*proxy->host == '\0')
429 yyerror("invalid proxy block: missing `relay-to' option");
431 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
432 (proxy->cert_path != NULL && proxy->key_path == NULL))
433 yyerror("invalid proxy block: missing cert or key");
437 proxy_matches : /* empty */
438 | proxy_matches proxy_match
441 proxy_port : /* empty */ { $$ = 1965; }
442 | PORT STRING {
443 if (($$ = getservice($2)) == -1)
444 yyerror("invalid port number %s", $2);
445 free($2);
447 | PORT NUM { $$ = $2; }
450 proxy_match : PROTO string {
451 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
452 free($2);
454 | FOR_HOST string proxy_port {
455 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
456 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
457 "%d", $3);
458 free($2);
462 proxy_opts : /* empty */
463 | proxy_opts proxy_opt optnl
466 proxy_opt : CERT string {
467 free(proxy->cert);
468 ensure_absolute_path($2);
469 proxy->cert_path = $2;
471 | KEY string {
472 free(proxy->key);
473 ensure_absolute_path($2);
474 proxy->key_path = $2;
476 | PROTOCOLS string {
477 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
478 yyerror("invalid protocols string \"%s\"", $2);
479 free($2);
481 | RELAY_TO string proxy_port {
482 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
483 (void) snprintf(proxy->port, sizeof(proxy->port),
484 "%d", $3);
485 free($2);
487 | REQUIRE CLIENT CA string {
488 ensure_absolute_path($4);
489 proxy->reqca_path = $4;
491 | SNI string {
492 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
493 free($2);
495 | USE_TLS bool {
496 proxy->notls = !$2;
498 | VERIFYNAME bool {
499 proxy->noverifyname = !$2;
503 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
504 /* drop the starting '/' if any */
505 if (*$3 == '/')
506 memmove($3, $3+1, strlen($3));
507 (void) strlcpy(loc->match, $3, sizeof(loc->match));
508 free($3);
510 | error '}'
513 locopts : /* empty */
514 | locopts locopt optnl
517 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
518 | BLOCK RETURN NUM string {
519 check_block_fmt($4);
520 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
521 loc->block_code = check_block_code($3);
522 free($4);
524 | BLOCK RETURN NUM {
525 (void) strlcpy(loc->block_fmt, "temporary failure",
526 sizeof(loc->block_fmt));
527 loc->block_code = check_block_code($3);
528 if ($3 >= 30 && $3 < 40)
529 yyerror("missing `meta' for block return %d", $3);
531 | BLOCK {
532 (void) strlcpy(loc->block_fmt, "temporary failure",
533 sizeof(loc->block_fmt));
534 loc->block_code = 40;
536 | DEFAULT TYPE string {
537 (void) strlcpy(loc->default_mime, $3,
538 sizeof(loc->default_mime));
539 free($3);
541 | fastcgi
542 | INDEX string {
543 (void) strlcpy(loc->index, $2, sizeof(loc->index));
544 free($2);
546 | LANG string {
547 (void) strlcpy(loc->lang, $2,
548 sizeof(loc->lang));
549 free($2);
551 | LOG bool { loc->disable_log = !$2; }
552 | REQUIRE CLIENT CA string {
553 ensure_absolute_path($4);
554 loc->reqca_path = $4;
556 | ROOT string {
557 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
558 free($2);
560 | STRIP NUM { loc->strip = check_strip_no($2); }
563 fastcgi : FASTCGI '{' optnl fastcgiopts '}'
564 | FASTCGI fastcgiopt
565 | FASTCGI OFF {
566 loc->fcgi = -1;
567 loc->nofcgi = 1;
569 | FASTCGI string {
570 yywarn("`fastcgi path' is deprecated. "
571 "Please use `fastcgi socket path' instead.");
572 loc->fcgi = fastcgi_conf($2, NULL);
573 free($2);
577 fastcgiopts : /* empty */
578 | fastcgiopts fastcgiopt optnl
581 fastcgiopt : PARAM string '=' string {
582 add_param($2, $4);
584 | SOCKET string {
585 loc->fcgi = fastcgi_conf($2, NULL);
586 free($2);
588 | SOCKET TCP string PORT NUM {
589 char *c;
591 if (asprintf(&c, "%d", $5) == -1)
592 fatal("asprintf");
593 loc->fcgi = fastcgi_conf($3, c);
594 free($3);
595 free(c);
597 | SOCKET TCP string {
598 loc->fcgi = fastcgi_conf($3, "9000");
600 | SOCKET TCP string PORT string {
601 loc->fcgi = fastcgi_conf($3, $5);
602 free($3);
603 free($5);
605 | STRIP NUM {
606 loc->fcgi_strip = $2;
610 types : TYPES '{' optnl mediaopts_l '}' ;
612 mediaopts_l : mediaopts_l mediaoptsl nl
613 | mediaoptsl nl
616 mediaoptsl : STRING {
617 free(current_media);
618 current_media = $1;
619 } medianames_l
620 | include
623 medianames_l : medianames_l medianamesl
624 | medianamesl
627 medianamesl : numberstring {
628 if (add_mime(&conf->mime, current_media, $1) == -1)
629 fatal("add_mime");
630 free($1);
634 nl : '\n' optnl
635 | ';' optnl
638 optnl : nl
639 | /*empty*/
642 %%
644 static const struct keyword {
645 const char *word;
646 int token;
647 } keywords[] = {
648 /* these MUST be sorted */
649 {"access", ACCESS},
650 {"alias", ALIAS},
651 {"auto", AUTO},
652 {"block", BLOCK},
653 {"ca", CA},
654 {"cert", CERT},
655 {"chroot", CHROOT},
656 {"client", CLIENT},
657 {"combined", COMBINED},
658 {"common", COMMON},
659 {"condensed", CONDENSED},
660 {"default", DEFAULT},
661 {"facility", FACILITY},
662 {"fastcgi", FASTCGI},
663 {"for-host", FOR_HOST},
664 {"include", INCLUDE},
665 {"index", INDEX},
666 {"ipv6", IPV6},
667 {"key", KEY},
668 {"lang", LANG},
669 {"legacy", LEGACY},
670 {"listen", LISTEN},
671 {"location", LOCATION},
672 {"log", LOG},
673 {"ocsp", OCSP},
674 {"off", OFF},
675 {"on", ON},
676 {"param", PARAM},
677 {"port", PORT},
678 {"prefork", PREFORK},
679 {"proto", PROTO},
680 {"protocols", PROTOCOLS},
681 {"proxy", PROXY},
682 {"relay-to", RELAY_TO},
683 {"require", REQUIRE},
684 {"return", RETURN},
685 {"root", ROOT},
686 {"server", SERVER},
687 {"sni", SNI},
688 {"socket", SOCKET},
689 {"strip", STRIP},
690 {"style", STYLE},
691 {"syslog", SYSLOG},
692 {"tcp", TCP},
693 {"to-ext", TOEXT},
694 {"type", TYPE},
695 {"types", TYPES},
696 {"use-tls", USE_TLS},
697 {"user", USER},
698 {"verifyname", VERIFYNAME},
699 };
701 void
702 yyerror(const char *msg, ...)
704 va_list ap;
706 file->errors++;
708 va_start(ap, msg);
709 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
710 vfprintf(stderr, msg, ap);
711 fprintf(stderr, "\n");
712 va_end(ap);
715 void
716 yywarn(const char *msg, ...)
718 va_list ap;
720 va_start(ap, msg);
721 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
722 vfprintf(stderr, msg, ap);
723 fprintf(stderr, "\n");
724 va_end(ap);
727 int
728 kw_cmp(const void *k, const void *e)
730 return strcmp(k, ((struct keyword *)e)->word);
733 int
734 lookup(char *s)
736 const struct keyword *p;
738 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
739 sizeof(keywords[0]), kw_cmp);
741 if (p)
742 return p->token;
743 else
744 return STRING;
747 #define START_EXPAND 1
748 #define DONE_EXPAND 2
750 static int expanding;
752 int
753 igetc(void)
755 int c;
757 while (1) {
758 if (file->ungetpos > 0)
759 c = file->ungetbuf[--file->ungetpos];
760 else
761 c = getc(file->stream);
763 if (c == START_EXPAND)
764 expanding = 1;
765 else if (c == DONE_EXPAND)
766 expanding = 0;
767 else
768 break;
770 return c;
773 int
774 lgetc(int quotec)
776 int c, next;
778 if (quotec) {
779 if ((c = igetc()) == EOF) {
780 yyerror("reached end of file while parsing "
781 "quoted string");
782 if (file == topfile || popfile() == EOF)
783 return EOF;
784 return quotec;
786 return c;
789 while ((c = igetc()) == '\\') {
790 next = igetc();
791 if (next != '\n') {
792 c = next;
793 break;
795 yylval.lineno = file->lineno;
796 file->lineno++;
799 if (c == EOF) {
800 /*
801 * Fake EOL when hit EOF for the first time. This gets line
802 * count right if last line in included file is syntactically
803 * invalid and has no newline.
804 */
805 if (file->eof_reached == 0) {
806 file->eof_reached = 1;
807 return '\n';
809 while (c == EOF) {
810 if (file == topfile || popfile() == EOF)
811 return EOF;
812 c = igetc();
815 return c;
818 void
819 lungetc(int c)
821 if (c == EOF)
822 return;
824 if (file->ungetpos >= file->ungetsize) {
825 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
826 if (p == NULL)
827 fatal("lungetc");
828 file->ungetbuf = p;
829 file->ungetsize *= 2;
831 file->ungetbuf[file->ungetpos++] = c;
834 int
835 findeol(void)
837 int c;
839 /* Skip to either EOF or the first real EOL. */
840 while (1) {
841 c = lgetc(0);
842 if (c == '\n') {
843 file->lineno++;
844 break;
846 if (c == EOF)
847 break;
849 return ERROR;
852 int
853 yylex(void)
855 char buf[8096];
856 char *p, *val;
857 int quotec, next, c;
858 int token;
860 top:
861 p = buf;
862 while ((c = lgetc(0)) == ' ' || c == '\t')
863 ; /* nothing */
865 yylval.lineno = file->lineno;
866 if (c == '#')
867 while ((c = lgetc(0)) != '\n' && c != EOF)
868 ; /* nothing */
869 if (c == '$' && !expanding) {
870 while (1) {
871 if ((c = lgetc(0)) == EOF)
872 return 0;
873 if (p + 1 >= buf + sizeof(buf) -1) {
874 yyerror("string too long");
875 return findeol();
877 if (isalnum(c) || c == '_') {
878 *p++ = c;
879 continue;
881 *p = '\0';
882 lungetc(c);
883 break;
885 val = symget(buf);
886 if (val == NULL) {
887 yyerror("macro `%s' not defined", buf);
888 return findeol();
890 yylval.v.string = xstrdup(val);
891 return STRING;
893 if (c == '@' && !expanding) {
894 while (1) {
895 if ((c = lgetc(0)) == EOF)
896 return 0;
898 if (p + 1 >= buf + sizeof(buf) - 1) {
899 yyerror("string too long");
900 return findeol();
902 if (isalnum(c) || c == '_') {
903 *p++ = c;
904 continue;
906 *p = '\0';
907 lungetc(c);
908 break;
910 val = symget(buf);
911 if (val == NULL) {
912 yyerror("macro '%s' not defined", buf);
913 return findeol();
915 p = val + strlen(val) - 1;
916 lungetc(DONE_EXPAND);
917 while (p >= val) {
918 lungetc(*p);
919 p--;
921 lungetc(START_EXPAND);
922 goto top;
925 switch (c) {
926 case '\'':
927 case '"':
928 quotec = c;
929 while (1) {
930 if ((c = lgetc(quotec)) == EOF)
931 return 0;
932 if (c == '\n') {
933 file->lineno++;
934 continue;
935 } else if (c == '\\') {
936 if ((next = lgetc(quotec)) == EOF)
937 return (0);
938 if (next == quotec || next == ' ' ||
939 next == '\t')
940 c = next;
941 else if (next == '\n') {
942 file->lineno++;
943 continue;
944 } else
945 lungetc(next);
946 } else if (c == quotec) {
947 *p = '\0';
948 break;
949 } else if (c == '\0') {
950 yyerror("invalid syntax");
951 return findeol();
953 if (p + 1 >= buf + sizeof(buf) - 1) {
954 yyerror("string too long");
955 return findeol();
957 *p++ = c;
959 yylval.v.string = strdup(buf);
960 if (yylval.v.string == NULL)
961 fatal("yylex: strdup");
962 return STRING;
965 #define allowed_to_end_number(x) \
966 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
968 if (c == '-' || isdigit(c)) {
969 do {
970 *p++ = c;
971 if ((size_t)(p-buf) >= sizeof(buf)) {
972 yyerror("string too long");
973 return findeol();
975 } while ((c = lgetc(0)) != EOF && isdigit(c));
976 lungetc(c);
977 if (p == buf + 1 && buf[0] == '-')
978 goto nodigits;
979 if (c == EOF || allowed_to_end_number(c)) {
980 const char *errstr = NULL;
982 *p = '\0';
983 yylval.v.number = strtonum(buf, LLONG_MIN,
984 LLONG_MAX, &errstr);
985 if (errstr) {
986 yyerror("\"%s\" invalid number: %s",
987 buf, errstr);
988 return findeol();
990 return NUM;
991 } else {
992 nodigits:
993 while (p > buf + 1)
994 lungetc(*--p);
995 c = *--p;
996 if (c == '-')
997 return c;
1001 #define allowed_in_string(x) \
1002 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1003 x != '{' && x != '}' && \
1004 x != '!' && x != '=' && x != '#' && \
1005 x != ',' && x != ';'))
1007 if (isalnum(c) || c == ':' || c == '_') {
1008 do {
1009 *p++ = c;
1010 if ((size_t)(p-buf) >= sizeof(buf)) {
1011 yyerror("string too long");
1012 return findeol();
1014 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
1015 lungetc(c);
1016 *p = '\0';
1017 if ((token = lookup(buf)) == STRING)
1018 yylval.v.string = xstrdup(buf);
1019 return token;
1021 if (c == '\n') {
1022 yylval.lineno = file->lineno;
1023 file->lineno++;
1025 if (c == EOF)
1026 return 0;
1027 return c;
1030 struct file *
1031 pushfile(const char *name, int secret)
1033 struct file *nfile;
1035 nfile = xcalloc(1, sizeof(*nfile));
1036 nfile->name = xstrdup(name);
1037 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
1038 log_warn("can't open %s", nfile->name);
1039 free(nfile->name);
1040 free(nfile);
1041 return NULL;
1043 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
1044 nfile->ungetsize = 16;
1045 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
1046 TAILQ_INSERT_TAIL(&files, nfile, entry);
1047 return nfile;
1050 int
1051 popfile(void)
1053 struct file *prev;
1055 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
1056 prev->errors += file->errors;
1058 TAILQ_REMOVE(&files, file, entry);
1059 fclose(file->stream);
1060 free(file->name);
1061 free(file->ungetbuf);
1062 free(file);
1063 file = prev;
1064 return file ? 0 : EOF;
1067 int
1068 parse_conf(struct conf *c, const char *filename)
1070 struct sym *sym, *next;
1072 default_host = NULL;
1073 default_port = 1965;
1075 conf = c;
1077 file = pushfile(filename, 0);
1078 if (file == NULL)
1079 return -1;
1080 topfile = file;
1082 yyparse();
1083 errors = file->errors;
1084 popfile();
1086 /* Free macros and check which have not been used. */
1087 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1088 /* TODO: warn if !sym->used */
1089 if (!sym->persist) {
1090 free(sym->name);
1091 free(sym->val);
1092 TAILQ_REMOVE(&symhead, sym, entry);
1093 free(sym);
1097 if (errors)
1098 return -1;
1099 return 0;
1102 int
1103 symset(const char *name, const char *val, int persist)
1105 struct sym *sym;
1107 TAILQ_FOREACH(sym, &symhead, entry) {
1108 if (!strcmp(name, sym->name))
1109 break;
1112 if (sym != NULL) {
1113 if (sym->persist)
1114 return 0;
1115 else {
1116 free(sym->name);
1117 free(sym->val);
1118 TAILQ_REMOVE(&symhead, sym, entry);
1119 free(sym);
1123 sym = xcalloc(1, sizeof(*sym));
1124 sym->name = xstrdup(name);
1125 sym->val = xstrdup(val);
1126 sym->used = 0;
1127 sym->persist = persist;
1129 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1130 return 0;
1133 int
1134 cmdline_symset(char *s)
1136 char *sym, *val;
1137 int ret;
1139 if ((val = strrchr(s, '=')) == NULL)
1140 return -1;
1141 sym = xcalloc(1, val - s + 1);
1142 memcpy(sym, s, val - s);
1143 ret = symset(sym, val + 1, 1);
1144 free(sym);
1145 return ret;
1148 char *
1149 symget(const char *nam)
1151 struct sym *sym;
1153 TAILQ_FOREACH(sym, &symhead, entry) {
1154 if (strcmp(nam, sym->name) == 0) {
1155 sym->used = 1;
1156 return sym->val;
1159 return NULL;
1162 char *
1163 ensure_absolute_path(char *path)
1165 if (path == NULL || *path != '/')
1166 yyerror("not an absolute path: %s", path);
1167 return path;
1170 int
1171 check_block_code(int n)
1173 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1174 yyerror("invalid block code %d", n);
1175 return n;
1178 char *
1179 check_block_fmt(char *fmt)
1181 char *s;
1183 for (s = fmt; *s; ++s) {
1184 if (*s != '%')
1185 continue;
1186 switch (*++s) {
1187 case '%':
1188 case 'p':
1189 case 'q':
1190 case 'P':
1191 case 'N':
1192 break;
1193 default:
1194 yyerror("invalid format specifier %%%c", *s);
1198 return fmt;
1201 int
1202 check_strip_no(int n)
1204 if (n <= 0)
1205 yyerror("invalid strip number %d", n);
1206 return n;
1209 int
1210 check_port_num(int n)
1212 if (n <= 0 || n >= UINT16_MAX)
1213 yyerror("port number is %s: %d",
1214 n <= 0 ? "too small" : "too large",
1215 n);
1216 return n;
1219 int
1220 check_prefork_num(int n)
1222 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1223 yyerror("invalid prefork number %d", n);
1224 return n;
1227 void
1228 advance_loc(void)
1230 loc = new_location();
1231 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1234 void
1235 advance_proxy(void)
1237 proxy = new_proxy();
1238 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1241 void
1242 parsehp(char *str, char **host, const char **port, const char *def)
1244 char *at;
1245 const char *errstr;
1247 *host = str;
1249 if ((at = strchr(str, ':')) != NULL) {
1250 *at++ = '\0';
1251 *port = at;
1252 } else
1253 *port = def;
1255 strtonum(*port, 1, UINT16_MAX, &errstr);
1256 if (errstr != NULL)
1257 yyerror("port is %s: %s", errstr, *port);
1260 int
1261 fastcgi_conf(const char *path, const char *port)
1263 struct fcgi *f;
1264 int i = 0;
1266 TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1267 if (!strcmp(f->path, path) &&
1268 ((port == NULL && *f->port == '\0') ||
1269 !strcmp(f->port, port)))
1270 return i;
1271 ++i;
1274 f = xcalloc(1, sizeof(*f));
1275 f->id = i;
1276 (void)strlcpy(f->path, path, sizeof(f->path));
1277 if (port != NULL)
1278 (void)strlcpy(f->port, port, sizeof(f->port));
1279 TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1281 return f->id;
1284 void
1285 add_param(char *name, char *val)
1287 struct envlist *e;
1288 struct envhead *h = &loc->params;
1290 e = xcalloc(1, sizeof(*e));
1291 (void) strlcpy(e->name, name, sizeof(e->name));
1292 (void) strlcpy(e->value, val, sizeof(e->value));
1293 TAILQ_INSERT_TAIL(h, e, envs);
1296 int
1297 getservice(const char *n)
1299 struct servent *s;
1300 const char *errstr;
1301 long long llval;
1303 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1304 if (errstr) {
1305 s = getservbyname(n, "tcp");
1306 if (s == NULL)
1307 s = getservbyname(n, "udp");
1308 if (s == NULL)
1309 return (-1);
1310 return (ntohs(s->s_port));
1313 return ((unsigned short)llval);
1316 static void
1317 add_to_addr_queue(struct addrhead *a, struct addrinfo *ai)
1319 struct address *addr;
1320 struct sockaddr_in *sin;
1321 struct sockaddr_in6 *sin6;
1323 if (ai->ai_addrlen > sizeof(addr->ss))
1324 fatalx("ai_addrlen larger than a sockaddr_storage");
1326 TAILQ_FOREACH(addr, a, addrs) {
1327 if (addr->ai_flags == ai->ai_flags &&
1328 addr->ai_family == ai->ai_family &&
1329 addr->ai_socktype == ai->ai_socktype &&
1330 addr->ai_protocol == ai->ai_protocol &&
1331 addr->slen == ai->ai_addrlen &&
1332 !memcmp(&addr->ss, ai->ai_addr, addr->slen))
1333 return;
1336 addr = xcalloc(1, sizeof(*addr));
1337 addr->ai_flags = ai->ai_flags;
1338 addr->ai_family = ai->ai_family;
1339 addr->ai_socktype = ai->ai_socktype;
1340 addr->ai_protocol = ai->ai_protocol;
1341 addr->slen = ai->ai_addrlen;
1342 memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
1344 /* for commodity */
1345 switch (addr->ai_family) {
1346 case AF_INET:
1347 sin = (struct sockaddr_in *)&addr->ss;
1348 addr->port = ntohs(sin->sin_port);
1349 break;
1350 case AF_INET6:
1351 sin6 = (struct sockaddr_in6 *)&addr->ss;
1352 addr->port = ntohs(sin6->sin6_port);
1353 break;
1354 default:
1355 fatalx("unknown socket family %d", addr->ai_family);
1358 addr->sock = -1;
1360 TAILQ_INSERT_HEAD(a, addr, addrs);
1363 void
1364 listen_on(const char *hostname, const char *servname)
1366 struct addrinfo hints, *res, *res0;
1367 int error;
1369 memset(&hints, 0, sizeof(hints));
1370 hints.ai_family = AF_UNSPEC;
1371 hints.ai_socktype = SOCK_STREAM;
1372 hints.ai_flags = AI_PASSIVE;
1373 error = getaddrinfo(hostname, servname, &hints, &res0);
1374 if (error) {
1375 yyerror("listen on \"%s\" port %s: %s", hostname, servname,
1376 gai_strerror(errno));
1377 return;
1380 for (res = res0; res; res = res->ai_next) {
1381 add_to_addr_queue(&host->addrs, res);
1382 add_to_addr_queue(&conf->addrs, res);
1385 freeaddrinfo(res0);