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 *, int);
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 CGI CHROOT CLIENT
122 %token ENTRYPOINT ENV
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
139 %token <v.number> NUM
141 %type <v.number> bool
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 { 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 "
221 "syntax: `map MIME to-ext EXT'");
222 add_mime(&conf.mime, $2, $3);
224 | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
225 | PORT NUM { conf.port = check_port_num($2); }
226 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
228 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
229 yyerror("invalid protocols string \"%s\"", $2);
232 | USER string { conf.user = $2; }
235 vhost : SERVER string {
237 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
239 loc = new_location();
240 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
242 TAILQ_INIT(&host->proxies);
244 loc->match = xstrdup("*");
247 if (strstr($2, "xn--") != NULL) {
248 yywarn("\"%s\" looks like punycode: you "
249 "should use the decoded hostname", $2);
251 } '{' optnl servbody '}' {
252 if (host->cert == NULL || host->key == NULL)
253 yyerror("invalid vhost definition: %s", $2);
255 | error '}' { yyerror("bad server directive"); }
258 servbody : /* empty */
259 | servbody servopt optnl
260 | servbody location optnl
261 | servbody proxy optnl
264 servopt : ALIAS string {
267 a = xcalloc(1, sizeof(*a));
269 if (TAILQ_EMPTY(&host->aliases))
270 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
272 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
275 only_once(host->cert, "cert");
276 host->cert = ensure_absolute_path($2);
279 only_once(host->cgi, "cgi");
280 /* drop the starting '/', if any */
282 memmove($2, $2+1, strlen($2));
285 | ENTRYPOINT string {
286 only_once(host->entrypoint, "entrypoint");
288 memmove($2, $2+1, strlen($2));
289 host->entrypoint = $2;
291 | ENV string '=' string {
292 add_param($2, $4, 1);
295 only_once(host->key, "key");
296 host->key = ensure_absolute_path($2);
299 only_once(host->ocsp, "ocsp");
300 host->ocsp = ensure_absolute_path($2);
302 | PARAM string '=' string {
303 add_param($2, $4, 0);
308 proxy : PROXY { advance_proxy(); }
309 proxy_matches '{' optnl proxy_opts '}' {
310 if (proxy->host == NULL)
311 yyerror("invalid proxy block: missing `relay-to' option");
313 if ((proxy->cert == NULL && proxy->key != NULL) ||
314 (proxy->cert != NULL && proxy->key == NULL))
315 yyerror("invalid proxy block: missing cert or key");
319 proxy_matches : /* empty */
320 | proxy_matches proxy_match
323 proxy_match : PROTO string {
324 only_once(proxy->match_proto, "proxy proto");
325 free(proxy->match_proto);
326 proxy->match_proto = $2;
329 only_once(proxy->match_host, "proxy for-host");
330 free(proxy->match_host);
331 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
335 proxy_opts : /* empty */
336 | proxy_opts proxy_opt optnl
339 proxy_opt : CERT string {
340 only_once(proxy->cert, "proxy cert");
341 tls_unload_file(proxy->cert, proxy->certlen);
342 ensure_absolute_path($2);
343 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
344 if (proxy->cert == NULL)
345 yyerror("can't load cert %s", $2);
349 only_once(proxy->key, "proxy key");
350 tls_unload_file(proxy->key, proxy->keylen);
351 ensure_absolute_path($2);
352 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
353 if (proxy->key == NULL)
354 yyerror("can't load key %s", $2);
358 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
359 yyerror("invalid protocols string \"%s\"", $2);
363 only_once(proxy->host, "proxy relay-to");
365 parsehp($2, &proxy->host, &proxy->port, "1965");
367 | REQUIRE CLIENT CA string {
368 only_once(proxy->reqca, "require client ca");
369 ensure_absolute_path($4);
370 if ((proxy->reqca = load_ca($4)) == NULL)
371 yyerror("couldn't load ca cert: %s", $4);
375 only_once(proxy->sni, "proxy sni");
383 proxy->noverifyname = !$2;
387 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
388 /* drop the starting '/' if any */
390 memmove($3, $3+1, strlen($3));
396 locopts : /* empty */
397 | locopts locopt optnl
400 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
401 | BLOCK RETURN NUM string {
402 only_once(loc->block_fmt, "block");
403 loc->block_fmt = check_block_fmt($4);
404 loc->block_code = check_block_code($3);
407 only_once(loc->block_fmt, "block");
408 loc->block_fmt = xstrdup("temporary failure");
409 loc->block_code = check_block_code($3);
410 if ($3 >= 30 && $3 < 40)
411 yyerror("missing `meta' for block return %d", $3);
414 only_once(loc->block_fmt, "block");
415 loc->block_fmt = xstrdup("temporary failure");
416 loc->block_code = 40;
418 | DEFAULT TYPE string {
419 only_once(loc->default_mime, "default type");
420 loc->default_mime = $3;
424 only_once(loc->index, "index");
428 only_once(loc->lang, "lang");
431 | LOG bool { loc->disable_log = !$2; }
432 | REQUIRE CLIENT CA string {
433 only_once(loc->reqca, "require client ca");
434 ensure_absolute_path($4);
435 if ((loc->reqca = load_ca($4)) == NULL)
436 yyerror("couldn't load ca cert: %s", $4);
440 only_once(loc->dir, "root");
441 loc->dir = ensure_absolute_path($2);
443 | STRIP NUM { loc->strip = check_strip_no($2); }
446 fastcgi : SPAWN string {
447 only_oncei(loc->fcgi, "fastcgi");
448 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
451 only_oncei(loc->fcgi, "fastcgi");
452 loc->fcgi = fastcgi_conf($1, NULL, NULL);
454 | TCP string PORT NUM {
456 if (asprintf(&c, "%d", $4) == -1)
458 only_oncei(loc->fcgi, "fastcgi");
459 loc->fcgi = fastcgi_conf($2, c, NULL);
462 only_oncei(loc->fcgi, "fastcgi");
463 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
465 | TCP string PORT string {
466 only_oncei(loc->fcgi, "fastcgi");
467 loc->fcgi = fastcgi_conf($2, $4, NULL);
471 types : TYPES '{' optnl mediaopts_l '}'
474 mediaopts_l : mediaopts_l mediaoptsl nl
478 mediaoptsl : STRING { current_media = $1; } medianames_l optsemicolon
482 medianames_l : medianames_l medianamesl
486 medianamesl : numberstring { add_mime(&conf.mime, current_media, $1); }
492 optnl : '\n' optnl /* zero or more newlines */
493 | ';' optnl /* semicolons too */
503 static struct keyword {
507 /* these MUST be sorted */
516 {"default", DEFAULT},
517 {"entrypoint", ENTRYPOINT},
519 {"fastcgi", FASTCGI},
520 {"for-host", FOR_HOST},
521 {"include", INCLUDE},
526 {"location", LOCATION},
535 {"prefork", PREFORK},
537 {"protocols", PROTOCOLS},
539 {"relay-to", RELAY_TO},
540 {"require", REQUIRE},
551 {"use-tls", USE_TLS},
553 {"verifyname", VERIFYNAME},
557 yyerror(const char *msg, ...)
564 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
565 vfprintf(stderr, msg, ap);
566 fprintf(stderr, "\n");
571 yywarn(const char *msg, ...)
576 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
577 vfprintf(stderr, msg, ap);
578 fprintf(stderr, "\n");
583 kw_cmp(const void *k, const void *e)
585 return strcmp(k, ((struct keyword *)e)->word);
591 const struct keyword *p;
593 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
594 sizeof(keywords[0]), kw_cmp);
602 #define START_EXPAND 1
603 #define DONE_EXPAND 2
605 static int expanding;
613 if (file->ungetpos > 0)
614 c = file->ungetbuf[--file->ungetpos];
616 c = getc(file->stream);
618 if (c == START_EXPAND)
620 else if (c == DONE_EXPAND)
634 if ((c = igetc()) == EOF) {
635 yyerror("reached end of file while parsing "
637 if (file == topfile || popfile() == EOF)
644 while ((c = igetc()) == '\\') {
650 yylval.lineno = file->lineno;
656 * Fake EOL when hit EOF for the first time. This gets line
657 * count right if last line in included file is syntactically
658 * invalid and has no newline.
660 if (file->eof_reached == 0) {
661 file->eof_reached = 1;
665 if (file == topfile || popfile() == EOF)
679 if (file->ungetpos >= file->ungetsize) {
680 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
684 file->ungetsize *= 2;
686 file->ungetbuf[file->ungetpos++] = c;
694 /* Skip to either EOF or the first real EOL. */
717 while ((c = lgetc(0)) == ' ' || c == '\t')
720 yylval.lineno = file->lineno;
722 while ((c = lgetc(0)) != '\n' && c != EOF)
724 if (c == '$' && !expanding) {
726 if ((c = lgetc(0)) == EOF)
728 if (p + 1 >= buf + sizeof(buf) -1) {
729 yyerror("string too long");
732 if (isalnum(c) || c == '_') {
742 yyerror("macro `%s' not defined", buf);
745 yylval.v.string = xstrdup(val);
748 if (c == '@' && !expanding) {
750 if ((c = lgetc(0)) == EOF)
753 if (p + 1 >= buf + sizeof(buf) - 1) {
754 yyerror("string too long");
757 if (isalnum(c) || c == '_') {
767 yyerror("macro '%s' not defined", buf);
770 p = val + strlen(val) - 1;
771 lungetc(DONE_EXPAND);
776 lungetc(START_EXPAND);
785 if ((c = lgetc(quotec)) == EOF)
790 } else if (c == '\\') {
791 if ((next = lgetc(quotec)) == EOF)
793 if (next == quotec || next == ' ' ||
796 else if (next == '\n') {
801 } else if (c == quotec) {
804 } else if (c == '\0') {
805 yyerror("invalid syntax");
808 if (p + 1 >= buf + sizeof(buf) - 1) {
809 yyerror("string too long");
814 yylval.v.string = strdup(buf);
815 if (yylval.v.string == NULL)
816 err(1, "yylex: strdup");
820 #define allowed_to_end_number(x) \
821 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
823 if (c == '-' || isdigit(c)) {
826 if ((size_t)(p-buf) >= sizeof(buf)) {
827 yyerror("string too long");
830 } while ((c = lgetc(0)) != EOF && isdigit(c));
832 if (p == buf + 1 && buf[0] == '-')
834 if (c == EOF || allowed_to_end_number(c)) {
835 const char *errstr = NULL;
838 yylval.v.number = strtonum(buf, LLONG_MIN,
841 yyerror("\"%s\" invalid number: %s",
856 #define allowed_in_string(x) \
857 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
858 x != '{' && x != '}' && \
859 x != '!' && x != '=' && x != '#' && \
860 x != ',' && x != ';'))
862 if (isalnum(c) || c == ':' || c == '_') {
865 if ((size_t)(p-buf) >= sizeof(buf)) {
866 yyerror("string too long");
869 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
872 if ((token = lookup(buf)) == STRING)
873 yylval.v.string = xstrdup(buf);
877 yylval.lineno = file->lineno;
886 pushfile(const char *name, int secret)
890 nfile = xcalloc(1, sizeof(*nfile));
891 nfile->name = xstrdup(name);
892 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
893 log_warn(NULL, "can't open %s: %s", nfile->name,
899 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
900 nfile->ungetsize = 16;
901 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
902 TAILQ_INSERT_TAIL(&files, nfile, entry);
911 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
912 prev->errors += file->errors;
914 TAILQ_REMOVE(&files, file, entry);
915 fclose(file->stream);
917 free(file->ungetbuf);
920 return file ? 0 : EOF;
924 parse_conf(const char *filename)
926 struct sym *sym, *next;
928 file = pushfile(filename, 0);
934 errors = file->errors;
937 /* Free macros and check which have not been used. */
938 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
939 /* TODO: warn if !sym->used */
943 TAILQ_REMOVE(&symhead, sym, entry);
956 /* struct location *l; */
957 /* struct envlist *e; */
958 /* struct alist *a; */
960 if (conf.chroot != NULL)
961 printf("chroot \"%s\"\n", conf.chroot);
962 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
963 /* XXX: defined mimes? */
964 printf("port %d\n", conf.port);
965 printf("prefork %d\n", conf.prefork);
966 /* XXX: protocols? */
967 if (conf.user != NULL)
968 printf("user \"%s\"\n", conf.user);
970 TAILQ_FOREACH(h, &hosts, vhosts) {
971 printf("\nserver \"%s\" {\n", h->domain);
972 printf(" cert \"%s\"\n", h->cert);
973 printf(" key \"%s\"\n", h->key);
974 /* TODO: print locations... */
980 symset(const char *name, const char *val, int persist)
984 TAILQ_FOREACH(sym, &symhead, entry) {
985 if (!strcmp(name, sym->name))
995 TAILQ_REMOVE(&symhead, sym, entry);
1000 sym = xcalloc(1, sizeof(*sym));
1001 sym->name = xstrdup(name);
1002 sym->val = xstrdup(val);
1004 sym->persist = persist;
1006 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1011 cmdline_symset(char *s)
1016 if ((val = strrchr(s, '=')) == NULL)
1018 sym = xcalloc(1, val - s + 1);
1019 memcpy(sym, s, val - s);
1020 ret = symset(sym, val + 1, 1);
1026 symget(const char *nam)
1030 TAILQ_FOREACH(sym, &symhead, entry) {
1031 if (strcmp(nam, sym->name) == 0) {
1042 return xcalloc(1, sizeof(struct vhost));
1050 l = xcalloc(1, sizeof(*l));
1061 p = xcalloc(1, sizeof(*p));
1062 p->protocols = TLS_PROTOCOLS_DEFAULT;
1067 ensure_absolute_path(char *path)
1069 if (path == NULL || *path != '/')
1070 yyerror("not an absolute path: %s", path);
1075 check_block_code(int n)
1077 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1078 yyerror("invalid block code %d", n);
1083 check_block_fmt(char *fmt)
1087 for (s = fmt; *s; ++s) {
1098 yyerror("invalid format specifier %%%c", *s);
1106 check_strip_no(int n)
1109 yyerror("invalid strip number %d", n);
1114 check_port_num(int n)
1116 if (n <= 0 || n >= UINT16_MAX)
1117 yyerror("port number is %s: %d",
1118 n <= 0 ? "too small" : "too large",
1124 check_prefork_num(int n)
1126 if (n <= 0 || n >= PROC_MAX)
1127 yyerror("invalid prefork number %d", n);
1134 loc = new_location();
1135 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1141 proxy = new_proxy();
1142 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1146 parsehp(char *str, char **host, const char **port, const char *def)
1153 if ((at = strchr(str, ':')) != NULL) {
1159 strtonum(*port, 1, UINT16_MAX, &errstr);
1161 yyerror("port is %s: %s", errstr, *port);
1165 only_once(const void *ptr, const char *name)
1168 yyerror("`%s' specified more than once", name);
1172 only_oncei(int i, const char *name)
1175 yyerror("`%s' specified more than once", name);
1179 fastcgi_conf(char *path, char *port, char *prog)
1184 for (i = 0; i < FCGI_MAX; ++i) {
1187 if (f->path == NULL) {
1195 /* XXX: what to do with prog? */
1196 if (!strcmp(f->path, path) &&
1197 ((port == NULL && f->port == NULL) ||
1198 !strcmp(f->port, port))) {
1205 yyerror("too much `fastcgi' rules defined.");
1210 add_param(char *name, char *val, int env)
1220 e = xcalloc(1, sizeof(*e));
1224 TAILQ_INSERT_HEAD(h, e, envs);
1226 TAILQ_INSERT_TAIL(h, e, envs);