4 * Copyright (c) 2021, 2022 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.
38 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
40 TAILQ_ENTRY(file) entry;
51 struct file *pushfile(const char *, int);
55 void yyerror(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 void yywarn(const char *, ...)
59 __attribute__((__format__ (printf, 1, 2)))
60 __attribute__((__nonnull__ (1)));
61 int kw_cmp(const void *, const void *);
73 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 TAILQ_ENTRY(sym) entry;
82 int symset(const char *, const char *, int);
83 char *symget(const char *);
85 struct vhost *new_vhost(void);
86 struct location *new_location(void);
87 struct proxy *new_proxy(void);
88 char *ensure_absolute_path(char*);
89 int check_block_code(int);
90 char *check_block_fmt(char*);
91 int check_strip_no(int);
92 int check_port_num(int);
93 int check_prefork_num(int);
94 void advance_loc(void);
95 void advance_proxy(void);
96 void parsehp(char *, char **, const char **, const char *);
97 int fastcgi_conf(const char *, const char *);
98 void add_param(char *, char *);
99 int getservice(const char *);
101 static struct vhost *host;
102 static struct location *loc;
103 static struct proxy *proxy;
104 static char *current_media;
118 /* %define parse.error verbose */
122 %token CA CERT CHROOT CLIENT
124 %token FASTCGI FOR_HOST
125 %token INCLUDE INDEX IPV6
127 %token LANG LOCATION LOG
129 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
130 %token RELAY_TO REQUIRE RETURN ROOT
131 %token SERVER SNI STRIP
132 %token TCP TOEXT TYPE TYPES
138 %token <v.string> STRING
139 %token <v.number> NUM
141 %type <v.number> bool proxy_port
142 %type <v.string> string numberstring
153 | conf error '\n' { file->errors++; }
156 include : INCLUDE STRING {
159 if ((nfile = pushfile($2, 0)) == NULL) {
160 yyerror("failed to include file %s", $2);
171 bool : ON { $$ = 1; }
175 string : string STRING {
176 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
179 yyerror("string: asprintf: %s", strerror(errno));
190 if (asprintf(&s, "%d", $1) == -1) {
191 yyerror("asprintf: number");
199 varset : STRING '=' string {
202 if (isspace((unsigned char)*s)) {
203 yyerror("macro name cannot contain "
216 option : CHROOT string {
217 if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >=
219 yyerror("chroot path too long");
222 | IPV6 bool { conf.ipv6 = $2; }
223 | PORT NUM { conf.port = check_port_num($2); }
224 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
226 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
227 yyerror("invalid protocols string \"%s\"", $2);
231 if (strlcpy(conf.user, $2, sizeof(conf.user)) >=
233 yyerror("user name too long");
238 vhost : SERVER string {
240 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
242 loc = new_location();
243 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
245 TAILQ_INIT(&host->proxies);
247 (void) strlcpy(loc->match, "*", sizeof(loc->match));
248 (void) strlcpy(host->domain, $2, sizeof(host->domain));
250 if (strstr($2, "xn--") != NULL) {
251 yywarn("\"%s\" looks like punycode: you "
252 "should use the decoded hostname", $2);
256 } '{' optnl servbody '}' {
257 if (host->cert_path == NULL ||
258 host->key_path == NULL)
259 yyerror("invalid vhost definition: %s", $2);
261 | error '}' { yyerror("bad server directive"); }
264 servbody : /* empty */
265 | servbody servopt optnl
266 | servbody location optnl
267 | servbody proxy optnl
270 servopt : ALIAS string {
273 a = xcalloc(1, sizeof(*a));
274 (void) strlcpy(a->alias, $2, sizeof(a->alias));
276 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
279 ensure_absolute_path($2);
280 free(host->cert_path);
281 host->cert_path = $2;
284 ensure_absolute_path($2);
285 free(host->key_path);
289 ensure_absolute_path($2);
290 free(host->ocsp_path);
291 host->ocsp_path = $2;
293 | PARAM string '=' string {
299 proxy : PROXY { advance_proxy(); }
300 proxy_matches '{' optnl proxy_opts '}' {
301 if (*proxy->host == '\0')
302 yyerror("invalid proxy block: missing `relay-to' option");
304 if ((proxy->cert == NULL && proxy->key != NULL) ||
305 (proxy->cert != NULL && proxy->key == NULL))
306 yyerror("invalid proxy block: missing cert or key");
310 proxy_matches : /* empty */
311 | proxy_matches proxy_match
314 proxy_port : /* empty */ { $$ = 1965; }
316 if (($$ = getservice($2)) == -1)
317 yyerror("invalid port number %s", $2);
320 | PORT NUM { $$ = $2; }
323 proxy_match : PROTO string {
324 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
327 | FOR_HOST string proxy_port {
328 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
329 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
335 proxy_opts : /* empty */
336 | proxy_opts proxy_opt optnl
339 proxy_opt : CERT string {
340 tls_unload_file(proxy->cert, proxy->certlen);
341 ensure_absolute_path($2);
342 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
343 if (proxy->cert == NULL)
344 yyerror("can't load cert %s", $2);
348 tls_unload_file(proxy->key, proxy->keylen);
349 ensure_absolute_path($2);
350 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
351 if (proxy->key == NULL)
352 yyerror("can't load key %s", $2);
356 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
357 yyerror("invalid protocols string \"%s\"", $2);
360 | RELAY_TO string proxy_port {
361 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
362 (void) snprintf(proxy->port, sizeof(proxy->port),
366 | REQUIRE CLIENT CA string {
367 ensure_absolute_path($4);
368 if ((proxy->reqca = load_ca($4)) == NULL)
369 yyerror("couldn't load ca cert: %s", $4);
373 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
380 proxy->noverifyname = !$2;
384 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
385 /* drop the starting '/' if any */
387 memmove($3, $3+1, strlen($3));
388 (void) strlcpy(loc->match, $3, sizeof(loc->match));
394 locopts : /* empty */
395 | locopts locopt optnl
398 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
399 | BLOCK RETURN NUM string {
401 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
402 loc->block_code = check_block_code($3);
406 (void) strlcpy(loc->block_fmt, "temporary failure",
407 sizeof(loc->block_fmt));
408 loc->block_code = check_block_code($3);
409 if ($3 >= 30 && $3 < 40)
410 yyerror("missing `meta' for block return %d", $3);
413 (void) strlcpy(loc->block_fmt, "temporary failure",
414 sizeof(loc->block_fmt));
415 loc->block_code = 40;
417 | DEFAULT TYPE string {
418 (void) strlcpy(loc->default_mime, $3,
419 sizeof(loc->default_mime));
424 (void) strlcpy(loc->index, $2, sizeof(loc->index));
428 (void) strlcpy(loc->lang, $2,
432 | LOG bool { loc->disable_log = !$2; }
433 | REQUIRE CLIENT CA string {
434 ensure_absolute_path($4);
435 if ((loc->reqca = load_ca($4)) == NULL)
436 yyerror("couldn't load ca cert: %s", $4);
440 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
443 | STRIP NUM { loc->strip = check_strip_no($2); }
447 loc->fcgi = fastcgi_conf($1, NULL);
450 | TCP string PORT NUM {
452 if (asprintf(&c, "%d", $4) == -1)
454 loc->fcgi = fastcgi_conf($2, c);
458 loc->fcgi = fastcgi_conf($2, "9000");
461 | TCP string PORT string {
462 loc->fcgi = fastcgi_conf($2, $4);
468 types : TYPES '{' optnl mediaopts_l '}' ;
470 mediaopts_l : mediaopts_l mediaoptsl nl
474 mediaoptsl : STRING {
477 } medianames_l optsemicolon
481 medianames_l : medianames_l medianamesl
485 medianamesl : numberstring {
486 if (add_mime(&conf.mime, current_media, $1) == -1)
495 optnl : '\n' optnl /* zero or more newlines */
496 | ';' optnl /* semicolons too */
506 static const struct keyword {
510 /* these MUST be sorted */
518 {"default", DEFAULT},
519 {"fastcgi", FASTCGI},
520 {"for-host", FOR_HOST},
521 {"include", INCLUDE},
526 {"location", LOCATION},
533 {"prefork", PREFORK},
535 {"protocols", PROTOCOLS},
537 {"relay-to", RELAY_TO},
538 {"require", REQUIRE},
548 {"use-tls", USE_TLS},
550 {"verifyname", VERIFYNAME},
554 yyerror(const char *msg, ...)
561 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
562 vfprintf(stderr, msg, ap);
563 fprintf(stderr, "\n");
568 yywarn(const char *msg, ...)
573 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
574 vfprintf(stderr, msg, ap);
575 fprintf(stderr, "\n");
580 kw_cmp(const void *k, const void *e)
582 return strcmp(k, ((struct keyword *)e)->word);
588 const struct keyword *p;
590 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
591 sizeof(keywords[0]), kw_cmp);
599 #define START_EXPAND 1
600 #define DONE_EXPAND 2
602 static int expanding;
610 if (file->ungetpos > 0)
611 c = file->ungetbuf[--file->ungetpos];
613 c = getc(file->stream);
615 if (c == START_EXPAND)
617 else if (c == DONE_EXPAND)
631 if ((c = igetc()) == EOF) {
632 yyerror("reached end of file while parsing "
634 if (file == topfile || popfile() == EOF)
641 while ((c = igetc()) == '\\') {
647 yylval.lineno = file->lineno;
653 * Fake EOL when hit EOF for the first time. This gets line
654 * count right if last line in included file is syntactically
655 * invalid and has no newline.
657 if (file->eof_reached == 0) {
658 file->eof_reached = 1;
662 if (file == topfile || popfile() == EOF)
676 if (file->ungetpos >= file->ungetsize) {
677 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
681 file->ungetsize *= 2;
683 file->ungetbuf[file->ungetpos++] = c;
691 /* Skip to either EOF or the first real EOL. */
714 while ((c = lgetc(0)) == ' ' || c == '\t')
717 yylval.lineno = file->lineno;
719 while ((c = lgetc(0)) != '\n' && c != EOF)
721 if (c == '$' && !expanding) {
723 if ((c = lgetc(0)) == EOF)
725 if (p + 1 >= buf + sizeof(buf) -1) {
726 yyerror("string too long");
729 if (isalnum(c) || c == '_') {
739 yyerror("macro `%s' not defined", buf);
742 yylval.v.string = xstrdup(val);
745 if (c == '@' && !expanding) {
747 if ((c = lgetc(0)) == EOF)
750 if (p + 1 >= buf + sizeof(buf) - 1) {
751 yyerror("string too long");
754 if (isalnum(c) || c == '_') {
764 yyerror("macro '%s' not defined", buf);
767 p = val + strlen(val) - 1;
768 lungetc(DONE_EXPAND);
773 lungetc(START_EXPAND);
782 if ((c = lgetc(quotec)) == EOF)
787 } else if (c == '\\') {
788 if ((next = lgetc(quotec)) == EOF)
790 if (next == quotec || next == ' ' ||
793 else if (next == '\n') {
798 } else if (c == quotec) {
801 } else if (c == '\0') {
802 yyerror("invalid syntax");
805 if (p + 1 >= buf + sizeof(buf) - 1) {
806 yyerror("string too long");
811 yylval.v.string = strdup(buf);
812 if (yylval.v.string == NULL)
813 fatal("yylex: strdup");
817 #define allowed_to_end_number(x) \
818 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
820 if (c == '-' || isdigit(c)) {
823 if ((size_t)(p-buf) >= sizeof(buf)) {
824 yyerror("string too long");
827 } while ((c = lgetc(0)) != EOF && isdigit(c));
829 if (p == buf + 1 && buf[0] == '-')
831 if (c == EOF || allowed_to_end_number(c)) {
832 const char *errstr = NULL;
835 yylval.v.number = strtonum(buf, LLONG_MIN,
838 yyerror("\"%s\" invalid number: %s",
853 #define allowed_in_string(x) \
854 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
855 x != '{' && x != '}' && \
856 x != '!' && x != '=' && x != '#' && \
857 x != ',' && x != ';'))
859 if (isalnum(c) || c == ':' || c == '_') {
862 if ((size_t)(p-buf) >= sizeof(buf)) {
863 yyerror("string too long");
866 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
869 if ((token = lookup(buf)) == STRING)
870 yylval.v.string = xstrdup(buf);
874 yylval.lineno = file->lineno;
883 pushfile(const char *name, int secret)
887 nfile = xcalloc(1, sizeof(*nfile));
888 nfile->name = xstrdup(name);
889 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
890 log_warn("can't open %s", nfile->name);
895 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
896 nfile->ungetsize = 16;
897 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
898 TAILQ_INSERT_TAIL(&files, nfile, entry);
907 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
908 prev->errors += file->errors;
910 TAILQ_REMOVE(&files, file, entry);
911 fclose(file->stream);
913 free(file->ungetbuf);
916 return file ? 0 : EOF;
920 parse_conf(const char *filename)
922 struct sym *sym, *next;
924 file = pushfile(filename, 0);
930 errors = file->errors;
933 /* Free macros and check which have not been used. */
934 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
935 /* TODO: warn if !sym->used */
939 TAILQ_REMOVE(&symhead, sym, entry);
952 /* struct location *l; */
953 /* struct envlist *e; */
954 /* struct alist *a; */
956 if (*conf.chroot != '\0')
957 printf("chroot \"%s\"\n", conf.chroot);
958 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
959 /* XXX: defined mimes? */
960 printf("port %d\n", conf.port);
961 printf("prefork %d\n", conf.prefork);
962 /* XXX: protocols? */
963 if (*conf.user != '\0')
964 printf("user \"%s\"\n", conf.user);
966 TAILQ_FOREACH(h, &hosts, vhosts) {
967 printf("\nserver \"%s\" {\n", h->domain);
968 printf(" cert \"%s\"\n", h->cert);
969 printf(" key \"%s\"\n", h->key);
970 /* TODO: print locations... */
976 symset(const char *name, const char *val, int persist)
980 TAILQ_FOREACH(sym, &symhead, entry) {
981 if (!strcmp(name, sym->name))
991 TAILQ_REMOVE(&symhead, sym, entry);
996 sym = xcalloc(1, sizeof(*sym));
997 sym->name = xstrdup(name);
998 sym->val = xstrdup(val);
1000 sym->persist = persist;
1002 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1007 cmdline_symset(char *s)
1012 if ((val = strrchr(s, '=')) == NULL)
1014 sym = xcalloc(1, val - s + 1);
1015 memcpy(sym, s, val - s);
1016 ret = symset(sym, val + 1, 1);
1022 symget(const char *nam)
1026 TAILQ_FOREACH(sym, &symhead, entry) {
1027 if (strcmp(nam, sym->name) == 0) {
1036 ensure_absolute_path(char *path)
1038 if (path == NULL || *path != '/')
1039 yyerror("not an absolute path: %s", path);
1044 check_block_code(int n)
1046 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1047 yyerror("invalid block code %d", n);
1052 check_block_fmt(char *fmt)
1056 for (s = fmt; *s; ++s) {
1067 yyerror("invalid format specifier %%%c", *s);
1075 check_strip_no(int n)
1078 yyerror("invalid strip number %d", n);
1083 check_port_num(int n)
1085 if (n <= 0 || n >= UINT16_MAX)
1086 yyerror("port number is %s: %d",
1087 n <= 0 ? "too small" : "too large",
1093 check_prefork_num(int n)
1095 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1096 yyerror("invalid prefork number %d", n);
1103 loc = new_location();
1104 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1110 proxy = new_proxy();
1111 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1115 parsehp(char *str, char **host, const char **port, const char *def)
1122 if ((at = strchr(str, ':')) != NULL) {
1128 strtonum(*port, 1, UINT16_MAX, &errstr);
1130 yyerror("port is %s: %s", errstr, *port);
1134 fastcgi_conf(const char *path, const char *port)
1139 for (i = 0; i < FCGI_MAX; ++i) {
1142 if (*f->path == '\0') {
1144 (void) strlcpy(f->path, path, sizeof(f->path));
1146 (void) strlcpy(f->port, port, sizeof(f->port));
1150 if (!strcmp(f->path, path) &&
1151 ((port == NULL && *f->port == '\0') ||
1152 !strcmp(f->port, port)))
1156 yyerror("too much `fastcgi' rules defined.");
1161 add_param(char *name, char *val)
1164 struct envhead *h = &host->params;
1166 e = xcalloc(1, sizeof(*e));
1167 (void) strlcpy(e->name, name, sizeof(e->name));
1168 (void) strlcpy(e->value, val, sizeof(e->value));
1169 TAILQ_INSERT_TAIL(h, e, envs);
1173 getservice(const char *n)
1179 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1181 s = getservbyname(n, "tcp");
1183 s = getservbyname(n, "udp");
1186 return (ntohs(s->s_port));
1189 return ((unsigned short)llval);