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.
35TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
37 TAILQ_ENTRY(file) entry;
48struct file *pushfile(const char *, int);
52void yyerror(const char *, ...)
53 __attribute__((__format__ (printf, 1, 2)))
54 __attribute__((__nonnull__ (1)));
55void yywarn(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58int kw_cmp(const void *, const void *);
70TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
72 TAILQ_ENTRY(sym) entry;
79int symset(const char *, const char *, int);
80char *symget(const char *);
82struct vhost *new_vhost(void);
83struct location *new_location(void);
84struct proxy *new_proxy(void);
85char *ensure_absolute_path(char*);
86int check_block_code(int);
87char *check_block_fmt(char*);
88int check_strip_no(int);
89int check_port_num(int);
90int check_prefork_num(int);
91void advance_loc(void);
92void advance_proxy(void);
93void parsehp(char *, char **, const char **, const char *);
94void only_once(const void*, const char*);
95void only_oncei(int, const char*);
96int fastcgi_conf(char *, char *, char *);
97void add_param(char *, char *, int);
99static struct vhost *host;
100static struct location *loc;
101static struct proxy *proxy;
102static char *current_media;
116/* %define parse.error verbose */
120%token CA CERT CGI CHROOT CLIENT
123%token FASTCGI FOR_HOST
124%token INCLUDE INDEX IPV6
126%token LANG LOCATION LOG
129%token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
130%token RELAY_TO REQUIRE RETURN ROOT
131%token SERVER SNI SPAWN STRIP
132%token TCP TOEXT TYPE TYPES
138%token <v.string> STRING
142%type <v.string> string numberstring
153 | conf error '\n' { file->errors++; }
156include : INCLUDE STRING {
159 if ((nfile = pushfile($2, 0)) == NULL) {
160 yyerror("failed to include file %s", $2);
175string : 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");
199varset : STRING '=' string {
202 if (isspace((unsigned char)*s)) {
203 yyerror("macro name cannot contain "
216option : CHROOT string { conf.chroot = $2; }
217 | IPV6 bool { conf.ipv6 = $2; }
218 | MIME STRING string {
219 yywarn("`mime MIME EXT' is deprecated and will be "
220 "removed in a future version, please use the new "
222 if (add_mime(&conf.mime, $2, $3) == -1)
225 | MAP string TOEXT string {
226 yywarn("`map mime to-ext' is deprecated and will be "
227 "removed in a future version, please use the new "
229 if (add_mime(&conf.mime, $2, $4) == -1)
232 | PORT NUM { conf.port = check_port_num($2); }
233 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
235 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
236 yyerror("invalid protocols string \"%s\"", $2);
239 | USER string { conf.user = $2; }
242vhost : SERVER string {
244 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
246 loc = new_location();
247 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
249 TAILQ_INIT(&host->proxies);
251 loc->match = xstrdup("*");
254 if (strstr($2, "xn--") != NULL) {
255 yywarn("\"%s\" looks like punycode: you "
256 "should use the decoded hostname", $2);
258 } '{' optnl servbody '}' {
259 if (host->cert == NULL || host->key == NULL)
260 yyerror("invalid vhost definition: %s", $2);
262 | error '}' { yyerror("bad server directive"); }
265servbody : /* empty */
266 | servbody servopt optnl
267 | servbody location optnl
268 | servbody proxy optnl
271servopt : ALIAS string {
274 a = xcalloc(1, sizeof(*a));
276 if (TAILQ_EMPTY(&host->aliases))
277 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
279 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
282 only_once(host->cert, "cert");
283 host->cert = ensure_absolute_path($2);
286 only_once(host->cgi, "cgi");
287 /* drop the starting '/', if any */
289 memmove($2, $2+1, strlen($2));
292 | ENTRYPOINT string {
293 only_once(host->entrypoint, "entrypoint");
295 memmove($2, $2+1, strlen($2));
296 host->entrypoint = $2;
298 | ENV string '=' string {
299 add_param($2, $4, 1);
302 only_once(host->key, "key");
303 host->key = ensure_absolute_path($2);
306 only_once(host->ocsp, "ocsp");
307 host->ocsp = ensure_absolute_path($2);
309 | PARAM string '=' string {
310 add_param($2, $4, 0);
315proxy : PROXY { advance_proxy(); }
316 proxy_matches '{' optnl proxy_opts '}' {
317 if (proxy->host == NULL)
318 yyerror("invalid proxy block: missing `relay-to' option");
320 if ((proxy->cert == NULL && proxy->key != NULL) ||
321 (proxy->cert != NULL && proxy->key == NULL))
322 yyerror("invalid proxy block: missing cert or key");
326proxy_matches : /* empty */
327 | proxy_matches proxy_match
330proxy_match : PROTO string {
331 only_once(proxy->match_proto, "proxy proto");
332 free(proxy->match_proto);
333 proxy->match_proto = $2;
336 only_once(proxy->match_host, "proxy for-host");
337 free(proxy->match_host);
338 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
342proxy_opts : /* empty */
343 | proxy_opts proxy_opt optnl
346proxy_opt : CERT string {
347 only_once(proxy->cert, "proxy cert");
348 tls_unload_file(proxy->cert, proxy->certlen);
349 ensure_absolute_path($2);
350 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
351 if (proxy->cert == NULL)
352 yyerror("can't load cert %s", $2);
356 only_once(proxy->key, "proxy key");
357 tls_unload_file(proxy->key, proxy->keylen);
358 ensure_absolute_path($2);
359 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
360 if (proxy->key == NULL)
361 yyerror("can't load key %s", $2);
365 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
366 yyerror("invalid protocols string \"%s\"", $2);
370 only_once(proxy->host, "proxy relay-to");
372 parsehp($2, &proxy->host, &proxy->port, "1965");
374 | REQUIRE CLIENT CA string {
375 only_once(proxy->reqca, "require client ca");
376 ensure_absolute_path($4);
377 if ((proxy->reqca = load_ca($4)) == NULL)
378 yyerror("couldn't load ca cert: %s", $4);
382 only_once(proxy->sni, "proxy sni");
390 proxy->noverifyname = !$2;
394location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
395 /* drop the starting '/' if any */
397 memmove($3, $3+1, strlen($3));
404 | locopts locopt optnl
407locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
408 | BLOCK RETURN NUM string {
409 only_once(loc->block_fmt, "block");
410 loc->block_fmt = check_block_fmt($4);
411 loc->block_code = check_block_code($3);
414 only_once(loc->block_fmt, "block");
415 loc->block_fmt = xstrdup("temporary failure");
416 loc->block_code = check_block_code($3);
417 if ($3 >= 30 && $3 < 40)
418 yyerror("missing `meta' for block return %d", $3);
421 only_once(loc->block_fmt, "block");
422 loc->block_fmt = xstrdup("temporary failure");
423 loc->block_code = 40;
425 | DEFAULT TYPE string {
426 only_once(loc->default_mime, "default type");
427 loc->default_mime = $3;
431 only_once(loc->index, "index");
435 only_once(loc->lang, "lang");
438 | LOG bool { loc->disable_log = !$2; }
439 | REQUIRE CLIENT CA string {
440 only_once(loc->reqca, "require client ca");
441 ensure_absolute_path($4);
442 if ((loc->reqca = load_ca($4)) == NULL)
443 yyerror("couldn't load ca cert: %s", $4);
447 only_once(loc->dir, "root");
448 loc->dir = ensure_absolute_path($2);
450 | STRIP NUM { loc->strip = check_strip_no($2); }
453fastcgi : SPAWN string {
454 only_oncei(loc->fcgi, "fastcgi");
455 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
458 only_oncei(loc->fcgi, "fastcgi");
459 loc->fcgi = fastcgi_conf($1, NULL, NULL);
461 | TCP string PORT NUM {
463 if (asprintf(&c, "%d", $4) == -1)
465 only_oncei(loc->fcgi, "fastcgi");
466 loc->fcgi = fastcgi_conf($2, c, NULL);
469 only_oncei(loc->fcgi, "fastcgi");
470 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
472 | TCP string PORT string {
473 only_oncei(loc->fcgi, "fastcgi");
474 loc->fcgi = fastcgi_conf($2, $4, NULL);
478types : TYPES '{' optnl mediaopts_l '}' {
479 conf.mime.skip_defaults = 1;
483mediaopts_l : mediaopts_l mediaoptsl nl
487mediaoptsl : STRING { current_media = $1; } medianames_l optsemicolon
491medianames_l : medianames_l medianamesl
495medianamesl : numberstring {
496 if (add_mime(&conf.mime, current_media, $1) == -1)
504optnl : '\n' optnl /* zero or more newlines */
505 | ';' optnl /* semicolons too */
515static const struct keyword {
519 /* these MUST be sorted */
528 {"default", DEFAULT},
529 {"entrypoint", ENTRYPOINT},
531 {"fastcgi", FASTCGI},
532 {"for-host", FOR_HOST},
533 {"include", INCLUDE},
538 {"location", LOCATION},
547 {"prefork", PREFORK},
549 {"protocols", PROTOCOLS},
551 {"relay-to", RELAY_TO},
552 {"require", REQUIRE},
563 {"use-tls", USE_TLS},
565 {"verifyname", VERIFYNAME},
569yyerror(const char *msg, ...)
576 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
577 vfprintf(stderr, msg, ap);
578 fprintf(stderr, "\n");
583yywarn(const char *msg, ...)
588 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
589 vfprintf(stderr, msg, ap);
590 fprintf(stderr, "\n");
595kw_cmp(const void *k, const void *e)
597 return strcmp(k, ((struct keyword *)e)->word);
603 const struct keyword *p;
605 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
606 sizeof(keywords[0]), kw_cmp);
614#define START_EXPAND 1
625 if (file->ungetpos > 0)
626 c = file->ungetbuf[--file->ungetpos];
628 c = getc(file->stream);
630 if (c == START_EXPAND)
632 else if (c == DONE_EXPAND)
646 if ((c = igetc()) == EOF) {
647 yyerror("reached end of file while parsing "
649 if (file == topfile || popfile() == EOF)
656 while ((c = igetc()) == '\\') {
662 yylval.lineno = file->lineno;
668 * Fake EOL when hit EOF for the first time. This gets line
669 * count right if last line in included file is syntactically
670 * invalid and has no newline.
672 if (file->eof_reached == 0) {
673 file->eof_reached = 1;
677 if (file == topfile || popfile() == EOF)
691 if (file->ungetpos >= file->ungetsize) {
692 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
696 file->ungetsize *= 2;
698 file->ungetbuf[file->ungetpos++] = c;
706 /* Skip to either EOF or the first real EOL. */
729 while ((c = lgetc(0)) == ' ' || c == '\t')
732 yylval.lineno = file->lineno;
734 while ((c = lgetc(0)) != '\n' && c != EOF)
736 if (c == '$' && !expanding) {
738 if ((c = lgetc(0)) == EOF)
740 if (p + 1 >= buf + sizeof(buf) -1) {
741 yyerror("string too long");
744 if (isalnum(c) || c == '_') {
754 yyerror("macro `%s' not defined", buf);
757 yylval.v.string = xstrdup(val);
760 if (c == '@' && !expanding) {
762 if ((c = lgetc(0)) == EOF)
765 if (p + 1 >= buf + sizeof(buf) - 1) {
766 yyerror("string too long");
769 if (isalnum(c) || c == '_') {
779 yyerror("macro '%s' not defined", buf);
782 p = val + strlen(val) - 1;
783 lungetc(DONE_EXPAND);
788 lungetc(START_EXPAND);
797 if ((c = lgetc(quotec)) == EOF)
802 } else if (c == '\\') {
803 if ((next = lgetc(quotec)) == EOF)
805 if (next == quotec || next == ' ' ||
808 else if (next == '\n') {
813 } else if (c == quotec) {
816 } else if (c == '\0') {
817 yyerror("invalid syntax");
820 if (p + 1 >= buf + sizeof(buf) - 1) {
821 yyerror("string too long");
826 yylval.v.string = strdup(buf);
827 if (yylval.v.string == NULL)
828 err(1, "yylex: strdup");
832#define allowed_to_end_number(x) \
833 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
835 if (c == '-' || isdigit(c)) {
838 if ((size_t)(p-buf) >= sizeof(buf)) {
839 yyerror("string too long");
842 } while ((c = lgetc(0)) != EOF && isdigit(c));
844 if (p == buf + 1 && buf[0] == '-')
846 if (c == EOF || allowed_to_end_number(c)) {
847 const char *errstr = NULL;
850 yylval.v.number = strtonum(buf, LLONG_MIN,
853 yyerror("\"%s\" invalid number: %s",
868#define allowed_in_string(x) \
869 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
870 x != '{' && x != '}' && \
871 x != '!' && x != '=' && x != '#' && \
872 x != ',' && x != ';'))
874 if (isalnum(c) || c == ':' || c == '_') {
877 if ((size_t)(p-buf) >= sizeof(buf)) {
878 yyerror("string too long");
881 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
884 if ((token = lookup(buf)) == STRING)
885 yylval.v.string = xstrdup(buf);
889 yylval.lineno = file->lineno;
898pushfile(const char *name, int secret)
902 nfile = xcalloc(1, sizeof(*nfile));
903 nfile->name = xstrdup(name);
904 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
905 log_warn(NULL, "can't open %s: %s", nfile->name,
911 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
912 nfile->ungetsize = 16;
913 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
914 TAILQ_INSERT_TAIL(&files, nfile, entry);
923 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
924 prev->errors += file->errors;
926 TAILQ_REMOVE(&files, file, entry);
927 fclose(file->stream);
929 free(file->ungetbuf);
932 return file ? 0 : EOF;
936parse_conf(const char *filename)
938 struct sym *sym, *next;
940 file = pushfile(filename, 0);
946 errors = file->errors;
949 /* Free macros and check which have not been used. */
950 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
951 /* TODO: warn if !sym->used */
955 TAILQ_REMOVE(&symhead, sym, entry);
968 /* struct location *l; */
969 /* struct envlist *e; */
970 /* struct alist *a; */
972 if (conf.chroot != NULL)
973 printf("chroot \"%s\"\n", conf.chroot);
974 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
975 /* XXX: defined mimes? */
976 printf("port %d\n", conf.port);
977 printf("prefork %d\n", conf.prefork);
978 /* XXX: protocols? */
979 if (conf.user != NULL)
980 printf("user \"%s\"\n", conf.user);
982 TAILQ_FOREACH(h, &hosts, vhosts) {
983 printf("\nserver \"%s\" {\n", h->domain);
984 printf(" cert \"%s\"\n", h->cert);
985 printf(" key \"%s\"\n", h->key);
986 /* TODO: print locations... */
992symset(const char *name, const char *val, int persist)
996 TAILQ_FOREACH(sym, &symhead, entry) {
997 if (!strcmp(name, sym->name))
1007 TAILQ_REMOVE(&symhead, sym, entry);
1012 sym = xcalloc(1, sizeof(*sym));
1013 sym->name = xstrdup(name);
1014 sym->val = xstrdup(val);
1016 sym->persist = persist;
1018 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1023cmdline_symset(char *s)
1028 if ((val = strrchr(s, '=')) == NULL)
1030 sym = xcalloc(1, val - s + 1);
1031 memcpy(sym, s, val - s);
1032 ret = symset(sym, val + 1, 1);
1038symget(const char *nam)
1042 TAILQ_FOREACH(sym, &symhead, entry) {
1043 if (strcmp(nam, sym->name) == 0) {
1054 return xcalloc(1, sizeof(struct vhost));
1062 l = xcalloc(1, sizeof(*l));
1073 p = xcalloc(1, sizeof(*p));
1074 p->protocols = TLS_PROTOCOLS_DEFAULT;
1079ensure_absolute_path(char *path)
1081 if (path == NULL || *path != '/')
1082 yyerror("not an absolute path: %s", path);
1087check_block_code(int n)
1089 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1090 yyerror("invalid block code %d", n);
1095check_block_fmt(char *fmt)
1099 for (s = fmt; *s; ++s) {
1110 yyerror("invalid format specifier %%%c", *s);
1118check_strip_no(int n)
1121 yyerror("invalid strip number %d", n);
1126check_port_num(int n)
1128 if (n <= 0 || n >= UINT16_MAX)
1129 yyerror("port number is %s: %d",
1130 n <= 0 ? "too small" : "too large",
1136check_prefork_num(int n)
1138 if (n <= 0 || n >= PROC_MAX)
1139 yyerror("invalid prefork number %d", n);
1146 loc = new_location();
1147 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1153 proxy = new_proxy();
1154 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1158parsehp(char *str, char **host, const char **port, const char *def)
1165 if ((at = strchr(str, ':')) != NULL) {
1171 strtonum(*port, 1, UINT16_MAX, &errstr);
1173 yyerror("port is %s: %s", errstr, *port);
1177only_once(const void *ptr, const char *name)
1180 yyerror("`%s' specified more than once", name);
1184only_oncei(int i, const char *name)
1187 yyerror("`%s' specified more than once", name);
1191fastcgi_conf(char *path, char *port, char *prog)
1196 for (i = 0; i < FCGI_MAX; ++i) {
1199 if (f->path == NULL) {
1207 /* XXX: what to do with prog? */
1208 if (!strcmp(f->path, path) &&
1209 ((port == NULL && f->port == NULL) ||
1210 !strcmp(f->port, port))) {
1217 yyerror("too much `fastcgi' rules defined.");
1222add_param(char *name, char *val, int env)
1232 e = xcalloc(1, sizeof(*e));
1236 TAILQ_INSERT_HEAD(h, e, envs);
1238 TAILQ_INSERT_TAIL(h, e, envs);