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 if (TAILQ_EMPTY(&host->aliases))
271 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
273 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
276 only_once(host->cert, "cert");
277 host->cert = ensure_absolute_path($2);
280 only_once(host->key, "key");
281 host->key = ensure_absolute_path($2);
284 only_once(host->ocsp, "ocsp");
285 host->ocsp = ensure_absolute_path($2);
287 | PARAM string '=' string {
293 proxy : PROXY { advance_proxy(); }
294 proxy_matches '{' optnl proxy_opts '}' {
295 if (proxy->host == NULL)
296 yyerror("invalid proxy block: missing `relay-to' option");
298 if ((proxy->cert == NULL && proxy->key != NULL) ||
299 (proxy->cert != NULL && proxy->key == NULL))
300 yyerror("invalid proxy block: missing cert or key");
304 proxy_matches : /* empty */
305 | proxy_matches proxy_match
308 proxy_match : PROTO string {
309 only_once(proxy->match_proto, "proxy proto");
310 free(proxy->match_proto);
311 proxy->match_proto = $2;
314 only_once(proxy->match_host, "proxy for-host");
315 free(proxy->match_host);
316 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
320 proxy_opts : /* empty */
321 | proxy_opts proxy_opt optnl
324 proxy_opt : CERT string {
325 only_once(proxy->cert, "proxy cert");
326 tls_unload_file(proxy->cert, proxy->certlen);
327 ensure_absolute_path($2);
328 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
329 if (proxy->cert == NULL)
330 yyerror("can't load cert %s", $2);
334 only_once(proxy->key, "proxy key");
335 tls_unload_file(proxy->key, proxy->keylen);
336 ensure_absolute_path($2);
337 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
338 if (proxy->key == NULL)
339 yyerror("can't load key %s", $2);
343 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
344 yyerror("invalid protocols string \"%s\"", $2);
348 only_once(proxy->host, "proxy relay-to");
350 parsehp($2, &proxy->host, &proxy->port, "1965");
352 | REQUIRE CLIENT CA string {
353 only_once(proxy->reqca, "require client ca");
354 ensure_absolute_path($4);
355 if ((proxy->reqca = load_ca($4)) == NULL)
356 yyerror("couldn't load ca cert: %s", $4);
360 only_once(proxy->sni, "proxy sni");
368 proxy->noverifyname = !$2;
372 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
373 /* drop the starting '/' if any */
375 memmove($3, $3+1, strlen($3));
381 locopts : /* empty */
382 | locopts locopt optnl
385 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
386 | BLOCK RETURN NUM string {
387 only_once(loc->block_fmt, "block");
388 loc->block_fmt = check_block_fmt($4);
389 loc->block_code = check_block_code($3);
392 only_once(loc->block_fmt, "block");
393 loc->block_fmt = xstrdup("temporary failure");
394 loc->block_code = check_block_code($3);
395 if ($3 >= 30 && $3 < 40)
396 yyerror("missing `meta' for block return %d", $3);
399 only_once(loc->block_fmt, "block");
400 loc->block_fmt = xstrdup("temporary failure");
401 loc->block_code = 40;
403 | DEFAULT TYPE string {
404 only_once(loc->default_mime, "default type");
405 loc->default_mime = $3;
409 only_once(loc->index, "index");
413 only_once(loc->lang, "lang");
416 | LOG bool { loc->disable_log = !$2; }
417 | REQUIRE CLIENT CA string {
418 only_once(loc->reqca, "require client ca");
419 ensure_absolute_path($4);
420 if ((loc->reqca = load_ca($4)) == NULL)
421 yyerror("couldn't load ca cert: %s", $4);
425 only_once(loc->dir, "root");
426 loc->dir = ensure_absolute_path($2);
428 | STRIP NUM { loc->strip = check_strip_no($2); }
431 fastcgi : SPAWN string {
432 only_oncei(loc->fcgi, "fastcgi");
433 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
436 only_oncei(loc->fcgi, "fastcgi");
437 loc->fcgi = fastcgi_conf($1, NULL, NULL);
439 | TCP string PORT NUM {
441 if (asprintf(&c, "%d", $4) == -1)
443 only_oncei(loc->fcgi, "fastcgi");
444 loc->fcgi = fastcgi_conf($2, c, NULL);
447 only_oncei(loc->fcgi, "fastcgi");
448 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
450 | TCP string PORT string {
451 only_oncei(loc->fcgi, "fastcgi");
452 loc->fcgi = fastcgi_conf($2, $4, NULL);
456 types : TYPES '{' optnl mediaopts_l '}' ;
458 mediaopts_l : mediaopts_l mediaoptsl nl
462 mediaoptsl : STRING {
465 } medianames_l optsemicolon
469 medianames_l : medianames_l medianamesl
473 medianamesl : numberstring {
474 if (add_mime(&conf.mime, current_media, $1) == -1)
483 optnl : '\n' optnl /* zero or more newlines */
484 | ';' optnl /* semicolons too */
494 static const struct keyword {
498 /* these MUST be sorted */
506 {"default", DEFAULT},
507 {"fastcgi", FASTCGI},
508 {"for-host", FOR_HOST},
509 {"include", INCLUDE},
514 {"location", LOCATION},
521 {"prefork", PREFORK},
523 {"protocols", PROTOCOLS},
525 {"relay-to", RELAY_TO},
526 {"require", REQUIRE},
537 {"use-tls", USE_TLS},
539 {"verifyname", VERIFYNAME},
543 yyerror(const char *msg, ...)
550 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
551 vfprintf(stderr, msg, ap);
552 fprintf(stderr, "\n");
557 yywarn(const char *msg, ...)
562 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
563 vfprintf(stderr, msg, ap);
564 fprintf(stderr, "\n");
569 kw_cmp(const void *k, const void *e)
571 return strcmp(k, ((struct keyword *)e)->word);
577 const struct keyword *p;
579 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
580 sizeof(keywords[0]), kw_cmp);
588 #define START_EXPAND 1
589 #define DONE_EXPAND 2
591 static int expanding;
599 if (file->ungetpos > 0)
600 c = file->ungetbuf[--file->ungetpos];
602 c = getc(file->stream);
604 if (c == START_EXPAND)
606 else if (c == DONE_EXPAND)
620 if ((c = igetc()) == EOF) {
621 yyerror("reached end of file while parsing "
623 if (file == topfile || popfile() == EOF)
630 while ((c = igetc()) == '\\') {
636 yylval.lineno = file->lineno;
642 * Fake EOL when hit EOF for the first time. This gets line
643 * count right if last line in included file is syntactically
644 * invalid and has no newline.
646 if (file->eof_reached == 0) {
647 file->eof_reached = 1;
651 if (file == topfile || popfile() == EOF)
665 if (file->ungetpos >= file->ungetsize) {
666 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
670 file->ungetsize *= 2;
672 file->ungetbuf[file->ungetpos++] = c;
680 /* Skip to either EOF or the first real EOL. */
703 while ((c = lgetc(0)) == ' ' || c == '\t')
706 yylval.lineno = file->lineno;
708 while ((c = lgetc(0)) != '\n' && c != EOF)
710 if (c == '$' && !expanding) {
712 if ((c = lgetc(0)) == EOF)
714 if (p + 1 >= buf + sizeof(buf) -1) {
715 yyerror("string too long");
718 if (isalnum(c) || c == '_') {
728 yyerror("macro `%s' not defined", buf);
731 yylval.v.string = xstrdup(val);
734 if (c == '@' && !expanding) {
736 if ((c = lgetc(0)) == EOF)
739 if (p + 1 >= buf + sizeof(buf) - 1) {
740 yyerror("string too long");
743 if (isalnum(c) || c == '_') {
753 yyerror("macro '%s' not defined", buf);
756 p = val + strlen(val) - 1;
757 lungetc(DONE_EXPAND);
762 lungetc(START_EXPAND);
771 if ((c = lgetc(quotec)) == EOF)
776 } else if (c == '\\') {
777 if ((next = lgetc(quotec)) == EOF)
779 if (next == quotec || next == ' ' ||
782 else if (next == '\n') {
787 } else if (c == quotec) {
790 } else if (c == '\0') {
791 yyerror("invalid syntax");
794 if (p + 1 >= buf + sizeof(buf) - 1) {
795 yyerror("string too long");
800 yylval.v.string = strdup(buf);
801 if (yylval.v.string == NULL)
802 err(1, "yylex: strdup");
806 #define allowed_to_end_number(x) \
807 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
809 if (c == '-' || isdigit(c)) {
812 if ((size_t)(p-buf) >= sizeof(buf)) {
813 yyerror("string too long");
816 } while ((c = lgetc(0)) != EOF && isdigit(c));
818 if (p == buf + 1 && buf[0] == '-')
820 if (c == EOF || allowed_to_end_number(c)) {
821 const char *errstr = NULL;
824 yylval.v.number = strtonum(buf, LLONG_MIN,
827 yyerror("\"%s\" invalid number: %s",
842 #define allowed_in_string(x) \
843 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
844 x != '{' && x != '}' && \
845 x != '!' && x != '=' && x != '#' && \
846 x != ',' && x != ';'))
848 if (isalnum(c) || c == ':' || c == '_') {
851 if ((size_t)(p-buf) >= sizeof(buf)) {
852 yyerror("string too long");
855 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
858 if ((token = lookup(buf)) == STRING)
859 yylval.v.string = xstrdup(buf);
863 yylval.lineno = file->lineno;
872 pushfile(const char *name, int secret)
876 nfile = xcalloc(1, sizeof(*nfile));
877 nfile->name = xstrdup(name);
878 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
879 log_warn(NULL, "can't open %s: %s", nfile->name,
885 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
886 nfile->ungetsize = 16;
887 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
888 TAILQ_INSERT_TAIL(&files, nfile, entry);
897 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
898 prev->errors += file->errors;
900 TAILQ_REMOVE(&files, file, entry);
901 fclose(file->stream);
903 free(file->ungetbuf);
906 return file ? 0 : EOF;
910 parse_conf(const char *filename)
912 struct sym *sym, *next;
914 file = pushfile(filename, 0);
920 errors = file->errors;
923 /* Free macros and check which have not been used. */
924 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
925 /* TODO: warn if !sym->used */
929 TAILQ_REMOVE(&symhead, sym, entry);
942 /* struct location *l; */
943 /* struct envlist *e; */
944 /* struct alist *a; */
946 if (*conf.chroot != '\0')
947 printf("chroot \"%s\"\n", conf.chroot);
948 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
949 /* XXX: defined mimes? */
950 printf("port %d\n", conf.port);
951 printf("prefork %d\n", conf.prefork);
952 /* XXX: protocols? */
953 if (*conf.user != '\0')
954 printf("user \"%s\"\n", conf.user);
956 TAILQ_FOREACH(h, &hosts, vhosts) {
957 printf("\nserver \"%s\" {\n", h->domain);
958 printf(" cert \"%s\"\n", h->cert);
959 printf(" key \"%s\"\n", h->key);
960 /* TODO: print locations... */
966 symset(const char *name, const char *val, int persist)
970 TAILQ_FOREACH(sym, &symhead, entry) {
971 if (!strcmp(name, sym->name))
981 TAILQ_REMOVE(&symhead, sym, entry);
986 sym = xcalloc(1, sizeof(*sym));
987 sym->name = xstrdup(name);
988 sym->val = xstrdup(val);
990 sym->persist = persist;
992 TAILQ_INSERT_TAIL(&symhead, sym, entry);
997 cmdline_symset(char *s)
1002 if ((val = strrchr(s, '=')) == NULL)
1004 sym = xcalloc(1, val - s + 1);
1005 memcpy(sym, s, val - s);
1006 ret = symset(sym, val + 1, 1);
1012 symget(const char *nam)
1016 TAILQ_FOREACH(sym, &symhead, entry) {
1017 if (strcmp(nam, sym->name) == 0) {
1028 return xcalloc(1, sizeof(struct vhost));
1036 l = xcalloc(1, sizeof(*l));
1047 conf.can_open_sockets = 1;
1049 p = xcalloc(1, sizeof(*p));
1050 p->protocols = TLS_PROTOCOLS_DEFAULT;
1055 ensure_absolute_path(char *path)
1057 if (path == NULL || *path != '/')
1058 yyerror("not an absolute path: %s", path);
1063 check_block_code(int n)
1065 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1066 yyerror("invalid block code %d", n);
1071 check_block_fmt(char *fmt)
1075 for (s = fmt; *s; ++s) {
1086 yyerror("invalid format specifier %%%c", *s);
1094 check_strip_no(int n)
1097 yyerror("invalid strip number %d", n);
1102 check_port_num(int n)
1104 if (n <= 0 || n >= UINT16_MAX)
1105 yyerror("port number is %s: %d",
1106 n <= 0 ? "too small" : "too large",
1112 check_prefork_num(int n)
1114 if (n <= 0 || n >= PROC_MAX)
1115 yyerror("invalid prefork number %d", n);
1122 loc = new_location();
1123 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1129 proxy = new_proxy();
1130 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1134 parsehp(char *str, char **host, const char **port, const char *def)
1141 if ((at = strchr(str, ':')) != NULL) {
1147 strtonum(*port, 1, UINT16_MAX, &errstr);
1149 yyerror("port is %s: %s", errstr, *port);
1153 only_once(const void *ptr, const char *name)
1156 yyerror("`%s' specified more than once", name);
1160 only_oncei(int i, const char *name)
1163 yyerror("`%s' specified more than once", name);
1167 fastcgi_conf(char *path, char *port, char *prog)
1172 conf.can_open_sockets = 1;
1174 for (i = 0; i < FCGI_MAX; ++i) {
1177 if (f->path == NULL) {
1185 /* XXX: what to do with prog? */
1186 if (!strcmp(f->path, path) &&
1187 ((port == NULL && f->port == NULL) ||
1188 !strcmp(f->port, port))) {
1195 yyerror("too much `fastcgi' rules defined.");
1200 add_param(char *name, char *val)
1203 struct envhead *h = &host->params;
1205 e = xcalloc(1, sizeof(*e));
1209 TAILQ_INSERT_HEAD(h, e, envs);
1211 TAILQ_INSERT_TAIL(h, e, envs);