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.
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.
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.
40 static const char *default_host = "*";
41 static uint16_t default_port = 1965;
43 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
45 TAILQ_ENTRY(file) entry;
56 struct file *pushfile(const char *, int);
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 *);
78 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
80 TAILQ_ENTRY(sym) entry;
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;
118 #define YYSTYPE YYSTYPE
123 /* %define parse.error verbose */
127 %token CA CERT CHROOT CLIENT
129 %token FASTCGI FOR_HOST
130 %token INCLUDE INDEX IPV6
132 %token LANG LISTEN LOCATION LOG
134 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
135 %token RELAY_TO REQUIRE RETURN ROOT
136 %token SERVER SNI STRIP
137 %token TCP TOEXT TYPE TYPES
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
158 | conf error '\n' { file->errors++; }
161 include : INCLUDE STRING {
164 if ((nfile = pushfile($2, 0)) == NULL) {
165 yyerror("failed to include file %s", $2);
176 bool : ON { $$ = 1; }
180 string : string STRING {
181 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
184 yyerror("string: asprintf: %s", strerror(errno));
195 if (asprintf(&s, "%d", $1) == -1) {
196 yyerror("asprintf: number");
204 varset : STRING '=' string {
207 if (isspace((unsigned char)*s)) {
208 yyerror("macro name cannot contain "
221 option : CHROOT string {
222 if (strlcpy(conf->chroot, $2, sizeof(conf->chroot)) >=
223 sizeof(conf->chroot))
224 yyerror("chroot path too long");
228 yywarn("option `ipv6' is deprecated,"
229 " please use `listen on'");
233 default_host = "0.0.0.0";
236 yywarn("option `port' is deprecated,"
237 " please use `listen on'");
240 | PREFORK NUM { conf->prefork = check_prefork_num($2); }
242 if (tls_config_parse_protocols(&conf->protos, $2) == -1)
243 yyerror("invalid protocols string \"%s\"", $2);
247 if (strlcpy(conf->user, $2, sizeof(conf->user)) >=
249 yyerror("user name too long");
254 vhost : SERVER string {
256 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
258 loc = new_location();
259 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
261 TAILQ_INIT(&host->proxies);
263 (void) strlcpy(loc->match, "*", sizeof(loc->match));
264 (void) strlcpy(host->domain, $2, sizeof(host->domain));
266 if (strstr($2, "xn--") != NULL) {
267 yywarn("\"%s\" looks like punycode: you "
268 "should use the decoded hostname", $2);
272 } '{' optnl servbody '}' {
273 if (host->cert_path == NULL ||
274 host->key_path == NULL)
275 yyerror("invalid vhost definition: %s",
277 if (TAILQ_EMPTY(&host->addrs)) {
281 r = snprintf(portno, sizeof(portno), "%d",
283 if (r < 0 || (size_t)r >= sizeof(portno))
286 yywarn("missing `listen on' in server %s,"
287 " assuming %s port %d", $2, default_host,
289 listen_on(default_host, portno);
292 | error '}' { yyerror("bad server directive"); }
295 servbody : /* empty */
296 | servbody servopt optnl
297 | servbody location optnl
298 | servbody proxy optnl
301 listen_addr : '*' { $$ = NULL; }
305 servopt : ALIAS string {
308 a = xcalloc(1, sizeof(*a));
309 (void) strlcpy(a->alias, $2, sizeof(a->alias));
311 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
314 ensure_absolute_path($2);
315 free(host->cert_path);
316 host->cert_path = $2;
319 ensure_absolute_path($2);
320 free(host->key_path);
324 ensure_absolute_path($2);
325 free(host->ocsp_path);
326 host->ocsp_path = $2;
328 | PARAM string '=' string {
331 | LISTEN ON listen_addr {
332 listen_on($3, "1965");
334 | LISTEN ON listen_addr PORT STRING {
339 | LISTEN ON listen_addr PORT NUM {
343 r = snprintf(portno, sizeof(portno), "%d", $5);
344 if (r < 0 || (size_t)r >= sizeof(portno))
347 listen_on($3, portno);
353 proxy : PROXY { advance_proxy(); }
354 proxy_matches '{' optnl proxy_opts '}' {
355 if (*proxy->host == '\0')
356 yyerror("invalid proxy block: missing `relay-to' option");
358 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
359 (proxy->cert_path != NULL && proxy->key_path == NULL))
360 yyerror("invalid proxy block: missing cert or key");
364 proxy_matches : /* empty */
365 | proxy_matches proxy_match
368 proxy_port : /* empty */ { $$ = 1965; }
370 if (($$ = getservice($2)) == -1)
371 yyerror("invalid port number %s", $2);
374 | PORT NUM { $$ = $2; }
377 proxy_match : PROTO string {
378 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
381 | FOR_HOST string proxy_port {
382 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
383 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
389 proxy_opts : /* empty */
390 | proxy_opts proxy_opt optnl
393 proxy_opt : CERT string {
395 ensure_absolute_path($2);
396 proxy->cert_path = $2;
400 ensure_absolute_path($2);
401 proxy->key_path = $2;
404 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
405 yyerror("invalid protocols string \"%s\"", $2);
408 | RELAY_TO string proxy_port {
409 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
410 (void) snprintf(proxy->port, sizeof(proxy->port),
414 | REQUIRE CLIENT CA string {
415 ensure_absolute_path($4);
416 proxy->reqca_path = $4;
419 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
426 proxy->noverifyname = !$2;
430 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
431 /* drop the starting '/' if any */
433 memmove($3, $3+1, strlen($3));
434 (void) strlcpy(loc->match, $3, sizeof(loc->match));
440 locopts : /* empty */
441 | locopts locopt optnl
444 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
445 | BLOCK RETURN NUM string {
447 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
448 loc->block_code = check_block_code($3);
452 (void) strlcpy(loc->block_fmt, "temporary failure",
453 sizeof(loc->block_fmt));
454 loc->block_code = check_block_code($3);
455 if ($3 >= 30 && $3 < 40)
456 yyerror("missing `meta' for block return %d", $3);
459 (void) strlcpy(loc->block_fmt, "temporary failure",
460 sizeof(loc->block_fmt));
461 loc->block_code = 40;
463 | DEFAULT TYPE string {
464 (void) strlcpy(loc->default_mime, $3,
465 sizeof(loc->default_mime));
470 (void) strlcpy(loc->index, $2, sizeof(loc->index));
474 (void) strlcpy(loc->lang, $2,
478 | LOG bool { loc->disable_log = !$2; }
479 | REQUIRE CLIENT CA string {
480 ensure_absolute_path($4);
481 loc->reqca_path = $4;
484 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
487 | STRIP NUM { loc->strip = check_strip_no($2); }
491 loc->fcgi = fastcgi_conf($1, NULL);
494 | TCP string PORT NUM {
496 if (asprintf(&c, "%d", $4) == -1)
498 loc->fcgi = fastcgi_conf($2, c);
502 loc->fcgi = fastcgi_conf($2, "9000");
505 | TCP string PORT string {
506 loc->fcgi = fastcgi_conf($2, $4);
512 types : TYPES '{' optnl mediaopts_l '}' ;
514 mediaopts_l : mediaopts_l mediaoptsl nl
518 mediaoptsl : STRING {
521 } medianames_l optsemicolon
525 medianames_l : medianames_l medianamesl
529 medianamesl : numberstring {
530 if (add_mime(&conf->mime, current_media, $1) == -1)
539 optnl : '\n' optnl /* zero or more newlines */
540 | ';' optnl /* semicolons too */
550 static const struct keyword {
554 /* these MUST be sorted */
562 {"default", DEFAULT},
563 {"fastcgi", FASTCGI},
564 {"for-host", FOR_HOST},
565 {"include", INCLUDE},
571 {"location", LOCATION},
578 {"prefork", PREFORK},
580 {"protocols", PROTOCOLS},
582 {"relay-to", RELAY_TO},
583 {"require", REQUIRE},
593 {"use-tls", USE_TLS},
595 {"verifyname", VERIFYNAME},
599 yyerror(const char *msg, ...)
606 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
607 vfprintf(stderr, msg, ap);
608 fprintf(stderr, "\n");
613 yywarn(const char *msg, ...)
618 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
619 vfprintf(stderr, msg, ap);
620 fprintf(stderr, "\n");
625 kw_cmp(const void *k, const void *e)
627 return strcmp(k, ((struct keyword *)e)->word);
633 const struct keyword *p;
635 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
636 sizeof(keywords[0]), kw_cmp);
644 #define START_EXPAND 1
645 #define DONE_EXPAND 2
647 static int expanding;
655 if (file->ungetpos > 0)
656 c = file->ungetbuf[--file->ungetpos];
658 c = getc(file->stream);
660 if (c == START_EXPAND)
662 else if (c == DONE_EXPAND)
676 if ((c = igetc()) == EOF) {
677 yyerror("reached end of file while parsing "
679 if (file == topfile || popfile() == EOF)
686 while ((c = igetc()) == '\\') {
692 yylval.lineno = file->lineno;
698 * Fake EOL when hit EOF for the first time. This gets line
699 * count right if last line in included file is syntactically
700 * invalid and has no newline.
702 if (file->eof_reached == 0) {
703 file->eof_reached = 1;
707 if (file == topfile || popfile() == EOF)
721 if (file->ungetpos >= file->ungetsize) {
722 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
726 file->ungetsize *= 2;
728 file->ungetbuf[file->ungetpos++] = c;
736 /* Skip to either EOF or the first real EOL. */
759 while ((c = lgetc(0)) == ' ' || c == '\t')
762 yylval.lineno = file->lineno;
764 while ((c = lgetc(0)) != '\n' && c != EOF)
766 if (c == '$' && !expanding) {
768 if ((c = lgetc(0)) == EOF)
770 if (p + 1 >= buf + sizeof(buf) -1) {
771 yyerror("string too long");
774 if (isalnum(c) || c == '_') {
784 yyerror("macro `%s' not defined", buf);
787 yylval.v.string = xstrdup(val);
790 if (c == '@' && !expanding) {
792 if ((c = lgetc(0)) == EOF)
795 if (p + 1 >= buf + sizeof(buf) - 1) {
796 yyerror("string too long");
799 if (isalnum(c) || c == '_') {
809 yyerror("macro '%s' not defined", buf);
812 p = val + strlen(val) - 1;
813 lungetc(DONE_EXPAND);
818 lungetc(START_EXPAND);
827 if ((c = lgetc(quotec)) == EOF)
832 } else if (c == '\\') {
833 if ((next = lgetc(quotec)) == EOF)
835 if (next == quotec || next == ' ' ||
838 else if (next == '\n') {
843 } else if (c == quotec) {
846 } else if (c == '\0') {
847 yyerror("invalid syntax");
850 if (p + 1 >= buf + sizeof(buf) - 1) {
851 yyerror("string too long");
856 yylval.v.string = strdup(buf);
857 if (yylval.v.string == NULL)
858 fatal("yylex: strdup");
862 #define allowed_to_end_number(x) \
863 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
865 if (c == '-' || isdigit(c)) {
868 if ((size_t)(p-buf) >= sizeof(buf)) {
869 yyerror("string too long");
872 } while ((c = lgetc(0)) != EOF && isdigit(c));
874 if (p == buf + 1 && buf[0] == '-')
876 if (c == EOF || allowed_to_end_number(c)) {
877 const char *errstr = NULL;
880 yylval.v.number = strtonum(buf, LLONG_MIN,
883 yyerror("\"%s\" invalid number: %s",
898 #define allowed_in_string(x) \
899 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
900 x != '{' && x != '}' && \
901 x != '!' && x != '=' && x != '#' && \
902 x != ',' && x != ';'))
904 if (isalnum(c) || c == ':' || c == '_') {
907 if ((size_t)(p-buf) >= sizeof(buf)) {
908 yyerror("string too long");
911 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
914 if ((token = lookup(buf)) == STRING)
915 yylval.v.string = xstrdup(buf);
919 yylval.lineno = file->lineno;
928 pushfile(const char *name, int secret)
932 nfile = xcalloc(1, sizeof(*nfile));
933 nfile->name = xstrdup(name);
934 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
935 log_warn("can't open %s", nfile->name);
940 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
941 nfile->ungetsize = 16;
942 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
943 TAILQ_INSERT_TAIL(&files, nfile, entry);
952 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
953 prev->errors += file->errors;
955 TAILQ_REMOVE(&files, file, entry);
956 fclose(file->stream);
958 free(file->ungetbuf);
961 return file ? 0 : EOF;
965 parse_conf(struct conf *c, const char *filename)
967 struct sym *sym, *next;
974 file = pushfile(filename, 0);
980 errors = file->errors;
983 /* Free macros and check which have not been used. */
984 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
985 /* TODO: warn if !sym->used */
989 TAILQ_REMOVE(&symhead, sym, entry);
1000 symset(const char *name, const char *val, int persist)
1004 TAILQ_FOREACH(sym, &symhead, entry) {
1005 if (!strcmp(name, sym->name))
1015 TAILQ_REMOVE(&symhead, sym, entry);
1020 sym = xcalloc(1, sizeof(*sym));
1021 sym->name = xstrdup(name);
1022 sym->val = xstrdup(val);
1024 sym->persist = persist;
1026 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1031 cmdline_symset(char *s)
1036 if ((val = strrchr(s, '=')) == NULL)
1038 sym = xcalloc(1, val - s + 1);
1039 memcpy(sym, s, val - s);
1040 ret = symset(sym, val + 1, 1);
1046 symget(const char *nam)
1050 TAILQ_FOREACH(sym, &symhead, entry) {
1051 if (strcmp(nam, sym->name) == 0) {
1060 ensure_absolute_path(char *path)
1062 if (path == NULL || *path != '/')
1063 yyerror("not an absolute path: %s", path);
1068 check_block_code(int n)
1070 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1071 yyerror("invalid block code %d", n);
1076 check_block_fmt(char *fmt)
1080 for (s = fmt; *s; ++s) {
1091 yyerror("invalid format specifier %%%c", *s);
1099 check_strip_no(int n)
1102 yyerror("invalid strip number %d", n);
1107 check_port_num(int n)
1109 if (n <= 0 || n >= UINT16_MAX)
1110 yyerror("port number is %s: %d",
1111 n <= 0 ? "too small" : "too large",
1117 check_prefork_num(int n)
1119 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1120 yyerror("invalid prefork number %d", n);
1127 loc = new_location();
1128 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1134 proxy = new_proxy();
1135 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1139 parsehp(char *str, char **host, const char **port, const char *def)
1146 if ((at = strchr(str, ':')) != NULL) {
1152 strtonum(*port, 1, UINT16_MAX, &errstr);
1154 yyerror("port is %s: %s", errstr, *port);
1158 fastcgi_conf(const char *path, const char *port)
1163 TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1164 if (!strcmp(f->path, path) &&
1165 ((port == NULL && *f->port == '\0') ||
1166 !strcmp(f->port, port)))
1171 f = xcalloc(1, sizeof(*f));
1173 (void)strlcpy(f->path, path, sizeof(f->path));
1175 (void)strlcpy(f->port, port, sizeof(f->port));
1176 TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1182 add_param(char *name, char *val)
1185 struct envhead *h = &host->params;
1187 e = xcalloc(1, sizeof(*e));
1188 (void) strlcpy(e->name, name, sizeof(e->name));
1189 (void) strlcpy(e->value, val, sizeof(e->value));
1190 TAILQ_INSERT_TAIL(h, e, envs);
1194 getservice(const char *n)
1200 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1202 s = getservbyname(n, "tcp");
1204 s = getservbyname(n, "udp");
1207 return (ntohs(s->s_port));
1210 return ((unsigned short)llval);
1214 add_to_addr_queue(struct addrhead *a, struct addrinfo *ai)
1216 struct address *addr;
1217 struct sockaddr_in *sin;
1218 struct sockaddr_in6 *sin6;
1220 if (ai->ai_addrlen > sizeof(addr->ss))
1221 fatalx("ai_addrlen larger than a sockaddr_storage");
1223 TAILQ_FOREACH(addr, a, addrs) {
1224 if (addr->ai_flags == ai->ai_flags &&
1225 addr->ai_family == ai->ai_family &&
1226 addr->ai_socktype == ai->ai_socktype &&
1227 addr->ai_protocol == ai->ai_protocol &&
1228 addr->slen == ai->ai_addrlen &&
1229 !memcmp(&addr->ss, ai->ai_addr, addr->slen))
1233 addr = xcalloc(1, sizeof(*addr));
1234 addr->ai_flags = ai->ai_flags;
1235 addr->ai_family = ai->ai_family;
1236 addr->ai_socktype = ai->ai_socktype;
1237 addr->ai_protocol = ai->ai_protocol;
1238 addr->slen = ai->ai_addrlen;
1239 memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
1242 switch (addr->ai_family) {
1244 sin = (struct sockaddr_in *)&addr->ss;
1245 addr->port = ntohs(sin->sin_port);
1248 sin6 = (struct sockaddr_in6 *)&addr->ss;
1249 addr->port = ntohs(sin6->sin6_port);
1252 fatalx("unknown socket family %d", addr->ai_family);
1257 TAILQ_INSERT_HEAD(a, addr, addrs);
1261 listen_on(const char *hostname, const char *servname)
1263 struct addrinfo hints, *res, *res0;
1266 memset(&hints, 0, sizeof(hints));
1267 hints.ai_family = AF_UNSPEC;
1268 hints.ai_socktype = SOCK_STREAM;
1269 hints.ai_flags = AI_PASSIVE;
1270 error = getaddrinfo(hostname, servname, &hints, &res0);
1272 yyerror("listen on \"%s\" port %s: %s", hostname, servname,
1273 gai_strerror(errno));
1277 for (res = res0; res; res = res->ai_next) {
1278 add_to_addr_queue(&host->addrs, res);
1279 add_to_addr_queue(&conf->addrs, res);