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.
35 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
37 TAILQ_ENTRY(file) entry;
48 struct file *pushfile(const char *, int);
52 void yyerror(const char *, ...)
53 __attribute__((__format__ (printf, 1, 2)))
54 __attribute__((__nonnull__ (1)));
55 void yywarn(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 int kw_cmp(const void *, const void *);
70 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
72 TAILQ_ENTRY(sym) entry;
79 int symset(const char *, const char *, int);
80 char *symget(const char *);
82 struct vhost *new_vhost(void);
83 struct location *new_location(void);
84 struct proxy *new_proxy(void);
85 char *ensure_absolute_path(char*);
86 int check_block_code(int);
87 char *check_block_fmt(char*);
88 int check_strip_no(int);
89 int check_port_num(int);
90 int check_prefork_num(int);
91 void advance_loc(void);
92 void advance_proxy(void);
93 void parsehp(char *, char **, const char **, const char *);
94 void only_once(const void*, const char*);
95 void only_oncei(int, const char*);
96 int fastcgi_conf(char *, char *, char *);
97 void add_param(char *, char *);
99 static struct vhost *host;
100 static struct location *loc;
101 static struct proxy *proxy;
102 static char *current_media;
116 /* %define parse.error verbose */
120 %token CA CERT CHROOT CLIENT
122 %token FASTCGI FOR_HOST
123 %token INCLUDE INDEX IPV6
125 %token LANG LOCATION LOG
127 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
128 %token RELAY_TO REQUIRE RETURN ROOT
129 %token SERVER SNI SPAWN STRIP
130 %token TCP TOEXT TYPE TYPES
136 %token <v.string> STRING
137 %token <v.number> NUM
139 %type <v.number> bool
140 %type <v.string> string numberstring
151 | conf error '\n' { file->errors++; }
154 include : INCLUDE STRING {
157 if ((nfile = pushfile($2, 0)) == NULL) {
158 yyerror("failed to include file %s", $2);
169 bool : ON { $$ = 1; }
173 string : string STRING {
174 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
177 yyerror("string: asprintf: %s", strerror(errno));
188 if (asprintf(&s, "%d", $1) == -1) {
189 yyerror("asprintf: number");
197 varset : STRING '=' string {
200 if (isspace((unsigned char)*s)) {
201 yyerror("macro name cannot contain "
214 option : CHROOT string {
215 if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >=
217 yyerror("chroot path too long");
220 | IPV6 bool { conf.ipv6 = $2; }
221 | PORT NUM { conf.port = check_port_num($2); }
222 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
224 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
225 yyerror("invalid protocols string \"%s\"", $2);
229 if (strlcpy(conf.user, $2, sizeof(conf.user)) >=
231 yyerror("user name too long");
236 vhost : SERVER string {
238 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
240 loc = new_location();
241 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
243 TAILQ_INIT(&host->proxies);
245 loc->match = xstrdup("*");
248 if (strstr($2, "xn--") != NULL) {
249 yywarn("\"%s\" looks like punycode: you "
250 "should use the decoded hostname", $2);
252 } '{' optnl servbody '}' {
253 if (host->cert == NULL || host->key == NULL)
254 yyerror("invalid vhost definition: %s", $2);
256 | error '}' { yyerror("bad server directive"); }
259 servbody : /* empty */
260 | servbody servopt optnl
261 | servbody location optnl
262 | servbody proxy optnl
265 servopt : ALIAS string {
268 a = xcalloc(1, sizeof(*a));
270 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
273 only_once(host->cert, "cert");
274 host->cert = ensure_absolute_path($2);
277 only_once(host->key, "key");
278 host->key = ensure_absolute_path($2);
281 only_once(host->ocsp, "ocsp");
282 host->ocsp = ensure_absolute_path($2);
284 | PARAM string '=' string {
290 proxy : PROXY { advance_proxy(); }
291 proxy_matches '{' optnl proxy_opts '}' {
292 if (proxy->host == NULL)
293 yyerror("invalid proxy block: missing `relay-to' option");
295 if ((proxy->cert == NULL && proxy->key != NULL) ||
296 (proxy->cert != NULL && proxy->key == NULL))
297 yyerror("invalid proxy block: missing cert or key");
301 proxy_matches : /* empty */
302 | proxy_matches proxy_match
305 proxy_match : PROTO string {
306 only_once(proxy->match_proto, "proxy proto");
307 free(proxy->match_proto);
308 proxy->match_proto = $2;
311 only_once(proxy->match_host, "proxy for-host");
312 free(proxy->match_host);
313 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
317 proxy_opts : /* empty */
318 | proxy_opts proxy_opt optnl
321 proxy_opt : CERT string {
322 only_once(proxy->cert, "proxy cert");
323 tls_unload_file(proxy->cert, proxy->certlen);
324 ensure_absolute_path($2);
325 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
326 if (proxy->cert == NULL)
327 yyerror("can't load cert %s", $2);
331 only_once(proxy->key, "proxy key");
332 tls_unload_file(proxy->key, proxy->keylen);
333 ensure_absolute_path($2);
334 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
335 if (proxy->key == NULL)
336 yyerror("can't load key %s", $2);
340 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
341 yyerror("invalid protocols string \"%s\"", $2);
345 only_once(proxy->host, "proxy relay-to");
347 parsehp($2, &proxy->host, &proxy->port, "1965");
349 | REQUIRE CLIENT CA string {
350 only_once(proxy->reqca, "require client ca");
351 ensure_absolute_path($4);
352 if ((proxy->reqca = load_ca($4)) == NULL)
353 yyerror("couldn't load ca cert: %s", $4);
357 only_once(proxy->sni, "proxy sni");
365 proxy->noverifyname = !$2;
369 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
370 /* drop the starting '/' if any */
372 memmove($3, $3+1, strlen($3));
378 locopts : /* empty */
379 | locopts locopt optnl
382 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
383 | BLOCK RETURN NUM string {
384 only_once(loc->block_fmt, "block");
385 loc->block_fmt = check_block_fmt($4);
386 loc->block_code = check_block_code($3);
389 only_once(loc->block_fmt, "block");
390 loc->block_fmt = xstrdup("temporary failure");
391 loc->block_code = check_block_code($3);
392 if ($3 >= 30 && $3 < 40)
393 yyerror("missing `meta' for block return %d", $3);
396 only_once(loc->block_fmt, "block");
397 loc->block_fmt = xstrdup("temporary failure");
398 loc->block_code = 40;
400 | DEFAULT TYPE string {
401 only_once(loc->default_mime, "default type");
402 loc->default_mime = $3;
406 only_once(loc->index, "index");
410 only_once(loc->lang, "lang");
413 | LOG bool { loc->disable_log = !$2; }
414 | REQUIRE CLIENT CA string {
415 only_once(loc->reqca, "require client ca");
416 ensure_absolute_path($4);
417 if ((loc->reqca = load_ca($4)) == NULL)
418 yyerror("couldn't load ca cert: %s", $4);
422 only_once(loc->dir, "root");
423 loc->dir = ensure_absolute_path($2);
425 | STRIP NUM { loc->strip = check_strip_no($2); }
428 fastcgi : SPAWN string {
429 only_oncei(loc->fcgi, "fastcgi");
430 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
433 only_oncei(loc->fcgi, "fastcgi");
434 loc->fcgi = fastcgi_conf($1, NULL, NULL);
436 | TCP string PORT NUM {
438 if (asprintf(&c, "%d", $4) == -1)
440 only_oncei(loc->fcgi, "fastcgi");
441 loc->fcgi = fastcgi_conf($2, c, NULL);
444 only_oncei(loc->fcgi, "fastcgi");
445 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
447 | TCP string PORT string {
448 only_oncei(loc->fcgi, "fastcgi");
449 loc->fcgi = fastcgi_conf($2, $4, NULL);
453 types : TYPES '{' optnl mediaopts_l '}' ;
455 mediaopts_l : mediaopts_l mediaoptsl nl
459 mediaoptsl : STRING {
462 } medianames_l optsemicolon
466 medianames_l : medianames_l medianamesl
470 medianamesl : numberstring {
471 if (add_mime(&conf.mime, current_media, $1) == -1)
480 optnl : '\n' optnl /* zero or more newlines */
481 | ';' optnl /* semicolons too */
491 static const struct keyword {
495 /* these MUST be sorted */
503 {"default", DEFAULT},
504 {"fastcgi", FASTCGI},
505 {"for-host", FOR_HOST},
506 {"include", INCLUDE},
511 {"location", LOCATION},
518 {"prefork", PREFORK},
520 {"protocols", PROTOCOLS},
522 {"relay-to", RELAY_TO},
523 {"require", REQUIRE},
534 {"use-tls", USE_TLS},
536 {"verifyname", VERIFYNAME},
540 yyerror(const char *msg, ...)
547 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
548 vfprintf(stderr, msg, ap);
549 fprintf(stderr, "\n");
554 yywarn(const char *msg, ...)
559 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
560 vfprintf(stderr, msg, ap);
561 fprintf(stderr, "\n");
566 kw_cmp(const void *k, const void *e)
568 return strcmp(k, ((struct keyword *)e)->word);
574 const struct keyword *p;
576 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
577 sizeof(keywords[0]), kw_cmp);
585 #define START_EXPAND 1
586 #define DONE_EXPAND 2
588 static int expanding;
596 if (file->ungetpos > 0)
597 c = file->ungetbuf[--file->ungetpos];
599 c = getc(file->stream);
601 if (c == START_EXPAND)
603 else if (c == DONE_EXPAND)
617 if ((c = igetc()) == EOF) {
618 yyerror("reached end of file while parsing "
620 if (file == topfile || popfile() == EOF)
627 while ((c = igetc()) == '\\') {
633 yylval.lineno = file->lineno;
639 * Fake EOL when hit EOF for the first time. This gets line
640 * count right if last line in included file is syntactically
641 * invalid and has no newline.
643 if (file->eof_reached == 0) {
644 file->eof_reached = 1;
648 if (file == topfile || popfile() == EOF)
662 if (file->ungetpos >= file->ungetsize) {
663 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
667 file->ungetsize *= 2;
669 file->ungetbuf[file->ungetpos++] = c;
677 /* Skip to either EOF or the first real EOL. */
700 while ((c = lgetc(0)) == ' ' || c == '\t')
703 yylval.lineno = file->lineno;
705 while ((c = lgetc(0)) != '\n' && c != EOF)
707 if (c == '$' && !expanding) {
709 if ((c = lgetc(0)) == EOF)
711 if (p + 1 >= buf + sizeof(buf) -1) {
712 yyerror("string too long");
715 if (isalnum(c) || c == '_') {
725 yyerror("macro `%s' not defined", buf);
728 yylval.v.string = xstrdup(val);
731 if (c == '@' && !expanding) {
733 if ((c = lgetc(0)) == EOF)
736 if (p + 1 >= buf + sizeof(buf) - 1) {
737 yyerror("string too long");
740 if (isalnum(c) || c == '_') {
750 yyerror("macro '%s' not defined", buf);
753 p = val + strlen(val) - 1;
754 lungetc(DONE_EXPAND);
759 lungetc(START_EXPAND);
768 if ((c = lgetc(quotec)) == EOF)
773 } else if (c == '\\') {
774 if ((next = lgetc(quotec)) == EOF)
776 if (next == quotec || next == ' ' ||
779 else if (next == '\n') {
784 } else if (c == quotec) {
787 } else if (c == '\0') {
788 yyerror("invalid syntax");
791 if (p + 1 >= buf + sizeof(buf) - 1) {
792 yyerror("string too long");
797 yylval.v.string = strdup(buf);
798 if (yylval.v.string == NULL)
799 err(1, "yylex: strdup");
803 #define allowed_to_end_number(x) \
804 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
806 if (c == '-' || isdigit(c)) {
809 if ((size_t)(p-buf) >= sizeof(buf)) {
810 yyerror("string too long");
813 } while ((c = lgetc(0)) != EOF && isdigit(c));
815 if (p == buf + 1 && buf[0] == '-')
817 if (c == EOF || allowed_to_end_number(c)) {
818 const char *errstr = NULL;
821 yylval.v.number = strtonum(buf, LLONG_MIN,
824 yyerror("\"%s\" invalid number: %s",
839 #define allowed_in_string(x) \
840 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
841 x != '{' && x != '}' && \
842 x != '!' && x != '=' && x != '#' && \
843 x != ',' && x != ';'))
845 if (isalnum(c) || c == ':' || c == '_') {
848 if ((size_t)(p-buf) >= sizeof(buf)) {
849 yyerror("string too long");
852 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
855 if ((token = lookup(buf)) == STRING)
856 yylval.v.string = xstrdup(buf);
860 yylval.lineno = file->lineno;
869 pushfile(const char *name, int secret)
873 nfile = xcalloc(1, sizeof(*nfile));
874 nfile->name = xstrdup(name);
875 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
876 log_warn(NULL, "can't open %s: %s", nfile->name,
882 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
883 nfile->ungetsize = 16;
884 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
885 TAILQ_INSERT_TAIL(&files, nfile, entry);
894 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
895 prev->errors += file->errors;
897 TAILQ_REMOVE(&files, file, entry);
898 fclose(file->stream);
900 free(file->ungetbuf);
903 return file ? 0 : EOF;
907 parse_conf(const char *filename)
909 struct sym *sym, *next;
911 file = pushfile(filename, 0);
917 errors = file->errors;
920 /* Free macros and check which have not been used. */
921 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
922 /* TODO: warn if !sym->used */
926 TAILQ_REMOVE(&symhead, sym, entry);
939 /* struct location *l; */
940 /* struct envlist *e; */
941 /* struct alist *a; */
943 if (*conf.chroot != '\0')
944 printf("chroot \"%s\"\n", conf.chroot);
945 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
946 /* XXX: defined mimes? */
947 printf("port %d\n", conf.port);
948 printf("prefork %d\n", conf.prefork);
949 /* XXX: protocols? */
950 if (*conf.user != '\0')
951 printf("user \"%s\"\n", conf.user);
953 TAILQ_FOREACH(h, &hosts, vhosts) {
954 printf("\nserver \"%s\" {\n", h->domain);
955 printf(" cert \"%s\"\n", h->cert);
956 printf(" key \"%s\"\n", h->key);
957 /* TODO: print locations... */
963 symset(const char *name, const char *val, int persist)
967 TAILQ_FOREACH(sym, &symhead, entry) {
968 if (!strcmp(name, sym->name))
978 TAILQ_REMOVE(&symhead, sym, entry);
983 sym = xcalloc(1, sizeof(*sym));
984 sym->name = xstrdup(name);
985 sym->val = xstrdup(val);
987 sym->persist = persist;
989 TAILQ_INSERT_TAIL(&symhead, sym, entry);
994 cmdline_symset(char *s)
999 if ((val = strrchr(s, '=')) == NULL)
1001 sym = xcalloc(1, val - s + 1);
1002 memcpy(sym, s, val - s);
1003 ret = symset(sym, val + 1, 1);
1009 symget(const char *nam)
1013 TAILQ_FOREACH(sym, &symhead, entry) {
1014 if (strcmp(nam, sym->name) == 0) {
1027 h = xcalloc(1, sizeof(*h));
1028 TAILQ_INIT(&h->locations);
1029 TAILQ_INIT(&h->params);
1030 TAILQ_INIT(&h->aliases);
1031 TAILQ_INIT(&h->proxies);
1040 l = xcalloc(1, sizeof(*l));
1051 conf.can_open_sockets = 1;
1053 p = xcalloc(1, sizeof(*p));
1054 p->protocols = TLS_PROTOCOLS_DEFAULT;
1059 ensure_absolute_path(char *path)
1061 if (path == NULL || *path != '/')
1062 yyerror("not an absolute path: %s", path);
1067 check_block_code(int n)
1069 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1070 yyerror("invalid block code %d", n);
1075 check_block_fmt(char *fmt)
1079 for (s = fmt; *s; ++s) {
1090 yyerror("invalid format specifier %%%c", *s);
1098 check_strip_no(int n)
1101 yyerror("invalid strip number %d", n);
1106 check_port_num(int n)
1108 if (n <= 0 || n >= UINT16_MAX)
1109 yyerror("port number is %s: %d",
1110 n <= 0 ? "too small" : "too large",
1116 check_prefork_num(int n)
1118 if (n <= 0 || n >= PROC_MAX)
1119 yyerror("invalid prefork number %d", n);
1126 loc = new_location();
1127 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1133 proxy = new_proxy();
1134 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1138 parsehp(char *str, char **host, const char **port, const char *def)
1145 if ((at = strchr(str, ':')) != NULL) {
1151 strtonum(*port, 1, UINT16_MAX, &errstr);
1153 yyerror("port is %s: %s", errstr, *port);
1157 only_once(const void *ptr, const char *name)
1160 yyerror("`%s' specified more than once", name);
1164 only_oncei(int i, const char *name)
1167 yyerror("`%s' specified more than once", name);
1171 fastcgi_conf(char *path, char *port, char *prog)
1176 conf.can_open_sockets = 1;
1178 for (i = 0; i < FCGI_MAX; ++i) {
1181 if (f->path == NULL) {
1189 /* XXX: what to do with prog? */
1190 if (!strcmp(f->path, path) &&
1191 ((port == NULL && f->port == NULL) ||
1192 !strcmp(f->port, port))) {
1199 yyerror("too much `fastcgi' rules defined.");
1204 add_param(char *name, char *val)
1207 struct envhead *h = &host->params;
1209 e = xcalloc(1, sizeof(*e));
1212 TAILQ_INSERT_TAIL(h, e, envs);