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;
115 /* %define parse.error verbose */
119 %token CA CERT CGI CHROOT CLIENT
121 %token ENTRYPOINT ENV
122 %token FASTCGI FOR_HOST
123 %token INCLUDE INDEX IPV6
125 %token LANG LOCATION LOG
128 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
129 %token RELAY_TO REQUIRE RETURN ROOT
130 %token SERVER SNI SPAWN STRIP
131 %token TCP TOEXT TYPE
137 %token <v.string> STRING
138 %token <v.number> NUM
140 %type <v.number> bool
141 %type <v.string> string
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));
186 varset : STRING '=' string {
189 if (isspace((unsigned char)*s)) {
190 yyerror("macro name cannot contain "
203 option : CHROOT string { conf.chroot = $2; }
204 | IPV6 bool { conf.ipv6 = $2; }
205 | MIME STRING string {
206 yywarn("`mime MIME EXT' is deprecated and will be "
207 "removed in a future version, please use the new "
208 "syntax: `map MIME to-ext EXT'");
209 add_mime(&conf.mime, $2, $3);
211 | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
212 | PORT NUM { conf.port = check_port_num($2); }
213 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
215 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
216 yyerror("invalid protocols string \"%s\"", $2);
219 | USER string { conf.user = $2; }
222 vhost : SERVER string {
224 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
226 loc = new_location();
227 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
229 TAILQ_INIT(&host->proxies);
231 loc->match = xstrdup("*");
234 if (strstr($2, "xn--") != NULL) {
235 yywarn("\"%s\" looks like punycode: you "
236 "should use the decoded hostname", $2);
238 } '{' optnl servbody '}' {
239 if (host->cert == NULL || host->key == NULL)
240 yyerror("invalid vhost definition: %s", $2);
242 | error '}' { yyerror("bad server directive"); }
245 servbody : /* empty */
246 | servbody servopt optnl
247 | servbody location optnl
248 | servbody proxy optnl
251 servopt : ALIAS string {
254 a = xcalloc(1, sizeof(*a));
256 if (TAILQ_EMPTY(&host->aliases))
257 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
259 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
262 only_once(host->cert, "cert");
263 host->cert = ensure_absolute_path($2);
266 only_once(host->cgi, "cgi");
267 /* drop the starting '/', if any */
269 memmove($2, $2+1, strlen($2));
272 | ENTRYPOINT string {
273 only_once(host->entrypoint, "entrypoint");
275 memmove($2, $2+1, strlen($2));
276 host->entrypoint = $2;
278 | ENV string '=' string {
279 add_param($2, $4, 1);
282 only_once(host->key, "key");
283 host->key = ensure_absolute_path($2);
286 only_once(host->ocsp, "ocsp");
287 host->ocsp = ensure_absolute_path($2);
289 | PARAM string '=' string {
290 add_param($2, $4, 0);
295 proxy : PROXY { advance_proxy(); }
296 proxy_matches '{' optnl proxy_opts '}' {
297 if (proxy->host == NULL)
298 yyerror("invalid proxy block: missing `relay-to' option");
300 if ((proxy->cert == NULL && proxy->key != NULL) ||
301 (proxy->cert != NULL && proxy->key == NULL))
302 yyerror("invalid proxy block: missing cert or key");
306 proxy_matches : /* empty */
307 | proxy_matches proxy_match
310 proxy_match : PROTO string {
311 only_once(proxy->match_proto, "proxy proto");
312 free(proxy->match_proto);
313 proxy->match_proto = $2;
316 only_once(proxy->match_host, "proxy for-host");
317 free(proxy->match_host);
318 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
322 proxy_opts : /* empty */
323 | proxy_opts proxy_opt optnl
326 proxy_opt : CERT string {
327 only_once(proxy->cert, "proxy cert");
328 tls_unload_file(proxy->cert, proxy->certlen);
329 ensure_absolute_path($2);
330 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
331 if (proxy->cert == NULL)
332 yyerror("can't load cert %s", $2);
336 only_once(proxy->key, "proxy key");
337 tls_unload_file(proxy->key, proxy->keylen);
338 ensure_absolute_path($2);
339 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
340 if (proxy->key == NULL)
341 yyerror("can't load key %s", $2);
345 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
346 yyerror("invalid protocols string \"%s\"", $2);
350 only_once(proxy->host, "proxy relay-to");
352 parsehp($2, &proxy->host, &proxy->port, "1965");
354 | REQUIRE CLIENT CA string {
355 only_once(proxy->reqca, "require client ca");
356 ensure_absolute_path($4);
357 if ((proxy->reqca = load_ca($4)) == NULL)
358 yyerror("couldn't load ca cert: %s", $4);
362 only_once(proxy->sni, "proxy sni");
370 proxy->noverifyname = !$2;
374 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
375 /* drop the starting '/' if any */
377 memmove($3, $3+1, strlen($3));
383 locopts : /* empty */
384 | locopts locopt optnl
387 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
388 | BLOCK RETURN NUM string {
389 only_once(loc->block_fmt, "block");
390 loc->block_fmt = check_block_fmt($4);
391 loc->block_code = check_block_code($3);
394 only_once(loc->block_fmt, "block");
395 loc->block_fmt = xstrdup("temporary failure");
396 loc->block_code = check_block_code($3);
397 if ($3 >= 30 && $3 < 40)
398 yyerror("missing `meta' for block return %d", $3);
401 only_once(loc->block_fmt, "block");
402 loc->block_fmt = xstrdup("temporary failure");
403 loc->block_code = 40;
405 | DEFAULT TYPE string {
406 only_once(loc->default_mime, "default type");
407 loc->default_mime = $3;
411 only_once(loc->index, "index");
415 only_once(loc->lang, "lang");
418 | LOG bool { loc->disable_log = !$2; }
419 | REQUIRE CLIENT CA string {
420 only_once(loc->reqca, "require client ca");
421 ensure_absolute_path($4);
422 if ((loc->reqca = load_ca($4)) == NULL)
423 yyerror("couldn't load ca cert: %s", $4);
427 only_once(loc->dir, "root");
428 loc->dir = ensure_absolute_path($2);
430 | STRIP NUM { loc->strip = check_strip_no($2); }
433 fastcgi : SPAWN string {
434 only_oncei(loc->fcgi, "fastcgi");
435 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
438 only_oncei(loc->fcgi, "fastcgi");
439 loc->fcgi = fastcgi_conf($1, NULL, NULL);
441 | TCP string PORT NUM {
443 if (asprintf(&c, "%d", $4) == -1)
445 only_oncei(loc->fcgi, "fastcgi");
446 loc->fcgi = fastcgi_conf($2, c, NULL);
449 only_oncei(loc->fcgi, "fastcgi");
450 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
452 | TCP string PORT string {
453 only_oncei(loc->fcgi, "fastcgi");
454 loc->fcgi = fastcgi_conf($2, $4, NULL);
458 optnl : '\n' optnl /* zero or more newlines */
459 | ';' optnl /* semicolons too */
465 static struct keyword {
469 /* these MUST be sorted */
478 {"default", DEFAULT},
479 {"entrypoint", ENTRYPOINT},
481 {"fastcgi", FASTCGI},
482 {"for-host", FOR_HOST},
487 {"location", LOCATION},
496 {"prefork", PREFORK},
498 {"protocols", PROTOCOLS},
500 {"relay-to", RELAY_TO},
501 {"require", REQUIRE},
511 {"use-tls", USE_TLS},
513 {"verifyname", VERIFYNAME},
517 yyerror(const char *msg, ...)
524 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
525 vfprintf(stderr, msg, ap);
526 fprintf(stderr, "\n");
531 yywarn(const char *msg, ...)
536 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
537 vfprintf(stderr, msg, ap);
538 fprintf(stderr, "\n");
543 kw_cmp(const void *k, const void *e)
545 return strcmp(k, ((struct keyword *)e)->word);
551 const struct keyword *p;
553 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
554 sizeof(keywords[0]), kw_cmp);
562 #define START_EXPAND 1
563 #define DONE_EXPAND 2
565 static int expanding;
573 if (file->ungetpos > 0)
574 c = file->ungetbuf[--file->ungetpos];
576 c = getc(file->stream);
578 if (c == START_EXPAND)
580 else if (c == DONE_EXPAND)
594 if ((c = igetc()) == EOF) {
595 yyerror("reached end of file while parsing "
597 if (file == topfile || popfile() == EOF)
604 while ((c = igetc()) == '\\') {
610 yylval.lineno = file->lineno;
616 * Fake EOL when hit EOF for the first time. This gets line
617 * count right if last line in included file is syntactically
618 * invalid and has no newline.
620 if (file->eof_reached == 0) {
621 file->eof_reached = 1;
625 if (file == topfile || popfile() == EOF)
639 if (file->ungetpos >= file->ungetsize) {
640 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
644 file->ungetsize *= 2;
646 file->ungetbuf[file->ungetpos++] = c;
654 /* Skip to either EOF or the first real EOL. */
677 while ((c = lgetc(0)) == ' ' || c == '\t')
680 yylval.lineno = file->lineno;
682 while ((c = lgetc(0)) != '\n' && c != EOF)
684 if (c == '$' && !expanding) {
686 if ((c = lgetc(0)) == EOF)
688 if (p + 1 >= buf + sizeof(buf) -1) {
689 yyerror("string too long");
692 if (isalnum(c) || c == '_') {
702 yyerror("macro `%s' not defined", buf);
705 yylval.v.string = xstrdup(val);
708 if (c == '@' && !expanding) {
710 if ((c = lgetc(0)) == EOF)
713 if (p + 1 >= buf + sizeof(buf) - 1) {
714 yyerror("string too long");
717 if (isalnum(c) || c == '_') {
727 yyerror("macro '%s' not defined", buf);
730 p = val + strlen(val) - 1;
731 lungetc(DONE_EXPAND);
736 lungetc(START_EXPAND);
745 if ((c = lgetc(quotec)) == EOF)
750 } else if (c == '\\') {
751 if ((next = lgetc(quotec)) == EOF)
753 if (next == quotec || next == ' ' ||
756 else if (next == '\n') {
761 } else if (c == quotec) {
764 } else if (c == '\0') {
765 yyerror("invalid syntax");
768 if (p + 1 >= buf + sizeof(buf) - 1) {
769 yyerror("string too long");
774 yylval.v.string = strdup(buf);
775 if (yylval.v.string == NULL)
776 err(1, "yylex: strdup");
780 #define allowed_to_end_number(x) \
781 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
783 if (c == '-' || isdigit(c)) {
786 if ((size_t)(p-buf) >= sizeof(buf)) {
787 yyerror("string too long");
790 } while ((c = lgetc(0)) != EOF && isdigit(c));
792 if (p == buf + 1 && buf[0] == '-')
794 if (c == EOF || allowed_to_end_number(c)) {
795 const char *errstr = NULL;
798 yylval.v.number = strtonum(buf, LLONG_MIN,
801 yyerror("\"%s\" invalid number: %s",
816 #define allowed_in_string(x) \
817 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
818 x != '{' && x != '}' && \
819 x != '!' && x != '=' && x != '#' && \
820 x != ',' && x != ';'))
822 if (isalnum(c) || c == ':' || c == '_') {
825 if ((size_t)(p-buf) >= sizeof(buf)) {
826 yyerror("string too long");
829 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
832 if ((token = lookup(buf)) == STRING)
833 yylval.v.string = xstrdup(buf);
837 yylval.lineno = file->lineno;
846 pushfile(const char *name, int secret)
850 nfile = xcalloc(1, sizeof(*nfile));
851 nfile->name = xstrdup(name);
852 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
853 log_warn(NULL, "can't open %s: %s", nfile->name,
859 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
860 nfile->ungetsize = 16;
861 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
862 TAILQ_INSERT_TAIL(&files, nfile, entry);
871 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
872 prev->errors += file->errors;
874 TAILQ_REMOVE(&files, file, entry);
875 fclose(file->stream);
877 free(file->ungetbuf);
880 return file ? 0 : EOF;
884 parse_conf(const char *filename)
886 struct sym *sym, *next;
888 file = pushfile(filename, 0);
894 errors = file->errors;
897 /* Free macros and check which have not been used. */
898 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
899 /* TODO: warn if !sym->used */
903 TAILQ_REMOVE(&symhead, sym, entry);
916 /* struct location *l; */
917 /* struct envlist *e; */
918 /* struct alist *a; */
920 if (conf.chroot != NULL)
921 printf("chroot \"%s\"\n", conf.chroot);
922 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
923 /* XXX: defined mimes? */
924 printf("port %d\n", conf.port);
925 printf("prefork %d\n", conf.prefork);
926 /* XXX: protocols? */
927 if (conf.user != NULL)
928 printf("user \"%s\"\n", conf.user);
930 TAILQ_FOREACH(h, &hosts, vhosts) {
931 printf("\nserver \"%s\" {\n", h->domain);
932 printf(" cert \"%s\"\n", h->cert);
933 printf(" key \"%s\"\n", h->key);
934 /* TODO: print locations... */
940 symset(const char *name, const char *val, int persist)
944 TAILQ_FOREACH(sym, &symhead, entry) {
945 if (!strcmp(name, sym->name))
955 TAILQ_REMOVE(&symhead, sym, entry);
960 sym = xcalloc(1, sizeof(*sym));
961 sym->name = xstrdup(name);
962 sym->val = xstrdup(val);
964 sym->persist = persist;
966 TAILQ_INSERT_TAIL(&symhead, sym, entry);
971 cmdline_symset(char *s)
976 if ((val = strrchr(s, '=')) == NULL)
978 sym = xcalloc(1, val - s + 1);
979 memcpy(sym, s, val - s);
980 ret = symset(sym, val + 1, 1);
986 symget(const char *nam)
990 TAILQ_FOREACH(sym, &symhead, entry) {
991 if (strcmp(nam, sym->name) == 0) {
1002 return xcalloc(1, sizeof(struct vhost));
1010 l = xcalloc(1, sizeof(*l));
1021 p = xcalloc(1, sizeof(*p));
1022 p->protocols = TLS_PROTOCOLS_DEFAULT;
1027 ensure_absolute_path(char *path)
1029 if (path == NULL || *path != '/')
1030 yyerror("not an absolute path: %s", path);
1035 check_block_code(int n)
1037 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1038 yyerror("invalid block code %d", n);
1043 check_block_fmt(char *fmt)
1047 for (s = fmt; *s; ++s) {
1058 yyerror("invalid format specifier %%%c", *s);
1066 check_strip_no(int n)
1069 yyerror("invalid strip number %d", n);
1074 check_port_num(int n)
1076 if (n <= 0 || n >= UINT16_MAX)
1077 yyerror("port number is %s: %d",
1078 n <= 0 ? "too small" : "too large",
1084 check_prefork_num(int n)
1086 if (n <= 0 || n >= PROC_MAX)
1087 yyerror("invalid prefork number %d", n);
1094 loc = new_location();
1095 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1101 proxy = new_proxy();
1102 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1106 parsehp(char *str, char **host, const char **port, const char *def)
1113 if ((at = strchr(str, ':')) != NULL) {
1119 strtonum(*port, 1, UINT16_MAX, &errstr);
1121 yyerror("port is %s: %s", errstr, *port);
1125 only_once(const void *ptr, const char *name)
1128 yyerror("`%s' specified more than once", name);
1132 only_oncei(int i, const char *name)
1135 yyerror("`%s' specified more than once", name);
1139 fastcgi_conf(char *path, char *port, char *prog)
1144 for (i = 0; i < FCGI_MAX; ++i) {
1147 if (f->path == NULL) {
1155 /* XXX: what to do with prog? */
1156 if (!strcmp(f->path, path) &&
1157 ((port == NULL && f->port == NULL) ||
1158 !strcmp(f->port, port))) {
1165 yyerror("too much `fastcgi' rules defined.");
1170 add_param(char *name, char *val, int env)
1180 e = xcalloc(1, sizeof(*e));
1184 TAILQ_INSERT_HEAD(h, e, envs);
1186 TAILQ_INSERT_TAIL(h, e, envs);