4 * Copyright (c) 2021 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 char *ensure_absolute_path(char*);
85 int check_block_code(int);
86 char *check_block_fmt(char*);
87 int check_strip_no(int);
88 int check_port_num(int);
89 int check_prefork_num(int);
90 void advance_loc(void);
91 void only_once(const void*, const char*);
92 void only_oncei(int, const char*);
93 int fastcgi_conf(char *, char *, char *);
94 void add_param(char *, char *, int);
96 static struct vhost *host;
97 static struct location *loc;
111 /* %define parse.error verbose */
115 %token CA CERT CGI CHROOT CLIENT
117 %token ENTRYPOINT ENV
119 %token INCLUDE INDEX IPV6
121 %token LANG LOCATION LOG
124 %token PARAM PORT PREFORK PROTOCOLS PROXY
125 %token RELAY_TO REQUIRE RETURN ROOT
126 %token SERVER SPAWN STRIP
127 %token TCP TOEXT TYPE USER
131 %token <v.string> STRING
132 %token <v.number> NUM
134 %type <v.number> bool
135 %type <v.string> string
145 | conf error '\n' { file->errors++; }
148 include : INCLUDE STRING {
151 if ((nfile = pushfile($2, 0)) == NULL) {
152 yyerror("failed to include file %s", $2);
163 bool : ON { $$ = 1; }
167 string : string STRING {
168 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
171 yyerror("string: asprintf: %s", strerror(errno));
180 varset : STRING '=' string {
183 if (isspace((unsigned char)*s)) {
184 yyerror("macro name cannot contain "
197 option : CHROOT string { conf.chroot = $2; }
198 | IPV6 bool { conf.ipv6 = $2; }
199 | MIME STRING string {
200 yywarn("`mime MIME EXT' is deprecated and will be "
201 "removed in a future version, please use the new "
202 "syntax: `map MIME to-ext EXT'");
203 add_mime(&conf.mime, $2, $3);
205 | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
206 | PORT NUM { conf.port = check_port_num($2); }
207 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
209 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
210 yyerror("invalid protocols string \"%s\"", $2);
212 | USER string { conf.user = $2; }
215 vhost : SERVER string {
217 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
219 loc = new_location();
220 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
222 loc->match = xstrdup("*");
225 if (strstr($2, "xn--") != NULL) {
226 yywarn("\"%s\" looks like punycode: you "
227 "should use the decoded hostname", $2);
229 } '{' optnl servopts locations '}' {
230 if (host->cert == NULL || host->key == NULL)
231 yyerror("invalid vhost definition: %s", $2);
233 | error '}' { yyerror("bad server directive"); }
236 servopts : /* empty */
237 | servopts servopt optnl
240 servopt : ALIAS string {
243 a = xcalloc(1, sizeof(*a));
245 if (TAILQ_EMPTY(&host->aliases))
246 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
248 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
251 only_once(host->cert, "cert");
252 host->cert = ensure_absolute_path($2);
255 only_once(host->cgi, "cgi");
256 /* drop the starting '/', if any */
258 memmove($2, $2+1, strlen($2));
261 | ENTRYPOINT string {
262 only_once(host->entrypoint, "entrypoint");
264 memmove($2, $2+1, strlen($2));
265 host->entrypoint = $2;
267 | ENV string '=' string {
268 add_param($2, $4, 1);
271 only_once(host->key, "key");
272 host->key = ensure_absolute_path($2);
275 only_once(host->ocsp, "ocsp");
276 host->ocsp = ensure_absolute_path($2);
278 | PARAM string '=' string {
279 add_param($2, $4, 0);
284 locations : /* empty */
285 | locations location optnl
288 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
289 /* drop the starting '/' if any */
291 memmove($3, $3+1, strlen($3));
297 locopts : /* empty */
298 | locopts locopt optnl
301 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
302 | BLOCK RETURN NUM string {
303 only_once(loc->block_fmt, "block");
304 loc->block_fmt = check_block_fmt($4);
305 loc->block_code = check_block_code($3);
308 only_once(loc->block_fmt, "block");
309 loc->block_fmt = xstrdup("temporary failure");
310 loc->block_code = check_block_code($3);
311 if ($3 >= 30 && $3 < 40)
312 yyerror("missing `meta' for block return %d", $3);
315 only_once(loc->block_fmt, "block");
316 loc->block_fmt = xstrdup("temporary failure");
317 loc->block_code = 40;
319 | DEFAULT TYPE string {
320 only_once(loc->default_mime, "default type");
321 loc->default_mime = $3;
325 only_once(loc->index, "index");
329 only_once(loc->lang, "lang");
332 | LOG bool { loc->disable_log = !$2; }
334 | REQUIRE CLIENT CA string {
335 only_once(loc->reqca, "require client ca");
336 ensure_absolute_path($4);
337 if ((loc->reqca = load_ca($4)) == NULL)
338 yyerror("couldn't load ca cert: %s", $4);
342 only_once(loc->dir, "root");
343 loc->dir = ensure_absolute_path($2);
345 | STRIP NUM { loc->strip = check_strip_no($2); }
348 proxy : PROXY proxy_opt
349 | PROXY '{' optnl proxy_opts '}'
352 proxy_opts : /* empty */
353 | proxy_opts proxy_opt optnl
356 proxy_opt : RELAY_TO string {
360 only_once(loc->proxy_host, "proxy relay-to");
361 loc->proxy_host = $2;
363 if ((at = strchr($2, ':')) != NULL) {
365 loc->proxy_port = at;
367 loc->proxy_port = "1965";
369 strtonum(loc->proxy_port, 1, UINT16_MAX, &errstr);
371 yyerror("proxy port is %s: %s", errstr,
376 fastcgi : SPAWN string {
377 only_oncei(loc->fcgi, "fastcgi");
378 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
381 only_oncei(loc->fcgi, "fastcgi");
382 loc->fcgi = fastcgi_conf($1, NULL, NULL);
384 | TCP string PORT NUM {
386 if (asprintf(&c, "%d", $4) == -1)
388 only_oncei(loc->fcgi, "fastcgi");
389 loc->fcgi = fastcgi_conf($2, c, NULL);
392 only_oncei(loc->fcgi, "fastcgi");
393 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
395 | TCP string PORT string {
396 only_oncei(loc->fcgi, "fastcgi");
397 loc->fcgi = fastcgi_conf($2, $4, NULL);
401 optnl : '\n' optnl /* zero or more newlines */
402 | ';' optnl /* semicolons too */
408 static struct keyword {
412 /* these MUST be sorted */
421 {"default", DEFAULT},
422 {"entrypoint", ENTRYPOINT},
424 {"fastcgi", FASTCGI},
429 {"location", LOCATION},
438 {"prefork", PREFORK},
439 {"protocols", PROTOCOLS},
441 {"relay-to", RELAY_TO},
442 {"require", REQUIRE},
455 yyerror(const char *msg, ...)
462 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
463 vfprintf(stderr, msg, ap);
464 fprintf(stderr, "\n");
469 yywarn(const char *msg, ...)
474 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
475 vfprintf(stderr, msg, ap);
476 fprintf(stderr, "\n");
481 kw_cmp(const void *k, const void *e)
483 return strcmp(k, ((struct keyword *)e)->word);
489 const struct keyword *p;
491 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
492 sizeof(keywords[0]), kw_cmp);
500 #define START_EXPAND 1
501 #define DONE_EXPAND 2
503 static int expanding;
511 if (file->ungetpos > 0)
512 c = file->ungetbuf[--file->ungetpos];
514 c = getc(file->stream);
516 if (c == START_EXPAND)
518 else if (c == DONE_EXPAND)
532 if ((c = igetc()) == EOF) {
533 yyerror("reached end of file while parsing "
535 if (file == topfile || popfile() == EOF)
542 while ((c = igetc()) == '\\') {
548 yylval.lineno = file->lineno;
554 * Fake EOL when hit EOF for the first time. This gets line
555 * count right if last line in included file is syntactically
556 * invalid and has no newline.
558 if (file->eof_reached == 0) {
559 file->eof_reached = 1;
563 if (file == topfile || popfile() == EOF)
577 if (file->ungetpos >= file->ungetsize) {
578 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
582 file->ungetsize *= 2;
584 file->ungetbuf[file->ungetpos++] = c;
592 /* Skip to either EOF or the first real EOL. */
615 while ((c = lgetc(0)) == ' ' || c == '\t')
618 yylval.lineno = file->lineno;
620 while ((c = lgetc(0)) != '\n' && c != EOF)
622 if (c == '$' && !expanding) {
624 if ((c = lgetc(0)) == EOF)
626 if (p + 1 >= buf + sizeof(buf) -1) {
627 yyerror("string too long");
630 if (isalnum(c) || c == '_') {
640 yyerror("macro `%s' not defined", buf);
643 yylval.v.string = xstrdup(val);
646 if (c == '@' && !expanding) {
648 if ((c = lgetc(0)) == EOF)
651 if (p + 1 >= buf + sizeof(buf) - 1) {
652 yyerror("string too long");
655 if (isalnum(c) || c == '_') {
665 yyerror("macro '%s' not defined", buf);
668 p = val + strlen(val) - 1;
669 lungetc(DONE_EXPAND);
674 lungetc(START_EXPAND);
683 if ((c = lgetc(quotec)) == EOF)
688 } else if (c == '\\') {
689 if ((next = lgetc(quotec)) == EOF)
691 if (next == quotec || next == ' ' ||
694 else if (next == '\n') {
699 } else if (c == quotec) {
702 } else if (c == '\0') {
703 yyerror("invalid syntax");
706 if (p + 1 >= buf + sizeof(buf) - 1) {
707 yyerror("string too long");
712 yylval.v.string = strdup(buf);
713 if (yylval.v.string == NULL)
714 err(1, "yylex: strdup");
718 #define allowed_to_end_number(x) \
719 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
721 if (c == '-' || isdigit(c)) {
724 if ((size_t)(p-buf) >= sizeof(buf)) {
725 yyerror("string too long");
728 } while ((c = lgetc(0)) != EOF && isdigit(c));
730 if (p == buf + 1 && buf[0] == '-')
732 if (c == EOF || allowed_to_end_number(c)) {
733 const char *errstr = NULL;
736 yylval.v.number = strtonum(buf, LLONG_MIN,
739 yyerror("\"%s\" invalid number: %s",
754 #define allowed_in_string(x) \
755 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
756 x != '{' && x != '}' && \
757 x != '!' && x != '=' && x != '#' && \
758 x != ',' && x != ';'))
760 if (isalnum(c) || c == ':' || c == '_') {
763 if ((size_t)(p-buf) >= sizeof(buf)) {
764 yyerror("string too long");
767 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
770 if ((token = lookup(buf)) == STRING)
771 yylval.v.string = xstrdup(buf);
775 yylval.lineno = file->lineno;
784 pushfile(const char *name, int secret)
788 nfile = xcalloc(1, sizeof(*nfile));
789 nfile->name = xstrdup(name);
790 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
791 log_warn(NULL, "can't open %s: %s", nfile->name,
797 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
798 nfile->ungetsize = 16;
799 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
800 TAILQ_INSERT_TAIL(&files, nfile, entry);
809 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
810 prev->errors += file->errors;
812 TAILQ_REMOVE(&files, file, entry);
813 fclose(file->stream);
815 free(file->ungetbuf);
818 return file ? 0 : EOF;
822 parse_conf(const char *filename)
824 struct sym *sym, *next;
826 file = pushfile(filename, 0);
832 errors = file->errors;
835 /* Free macros and check which have not been used. */
836 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
837 /* TODO: warn if !sym->used */
841 TAILQ_REMOVE(&symhead, sym, entry);
854 /* struct location *l; */
855 /* struct envlist *e; */
856 /* struct alist *a; */
858 if (conf.chroot != NULL)
859 printf("chroot \"%s\"\n", conf.chroot);
860 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
861 /* XXX: defined mimes? */
862 printf("port %d\n", conf.port);
863 printf("prefork %d\n", conf.prefork);
864 /* XXX: protocols? */
865 if (conf.user != NULL)
866 printf("user \"%s\"\n", conf.user);
868 TAILQ_FOREACH(h, &hosts, vhosts) {
869 printf("\nserver \"%s\" {\n", h->domain);
870 printf(" cert \"%s\"\n", h->cert);
871 printf(" key \"%s\"\n", h->key);
872 /* TODO: print locations... */
878 symset(const char *name, const char *val, int persist)
882 TAILQ_FOREACH(sym, &symhead, entry) {
883 if (!strcmp(name, sym->name))
893 TAILQ_REMOVE(&symhead, sym, entry);
898 sym = xcalloc(1, sizeof(*sym));
899 sym->name = xstrdup(name);
900 sym->val = xstrdup(val);
902 sym->persist = persist;
904 TAILQ_INSERT_TAIL(&symhead, sym, entry);
909 cmdline_symset(char *s)
914 if ((val = strrchr(s, '=')) == NULL)
916 sym = xcalloc(1, val - s + 1);
917 memcpy(sym, s, val - s);
918 ret = symset(sym, val + 1, 1);
924 symget(const char *nam)
928 TAILQ_FOREACH(sym, &symhead, entry) {
929 if (strcmp(nam, sym->name) == 0) {
940 return xcalloc(1, sizeof(struct vhost));
948 l = xcalloc(1, sizeof(*l));
955 ensure_absolute_path(char *path)
957 if (path == NULL || *path != '/')
958 yyerror("not an absolute path: %s", path);
963 check_block_code(int n)
965 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
966 yyerror("invalid block code %d", n);
971 check_block_fmt(char *fmt)
975 for (s = fmt; *s; ++s) {
986 yyerror("invalid format specifier %%%c", *s);
994 check_strip_no(int n)
997 yyerror("invalid strip number %d", n);
1002 check_port_num(int n)
1004 if (n <= 0 || n >= UINT16_MAX)
1005 yyerror("port number is %s: %d",
1006 n <= 0 ? "too small" : "too large",
1012 check_prefork_num(int n)
1014 if (n <= 0 || n >= PROC_MAX)
1015 yyerror("invalid prefork number %d", n);
1022 loc = new_location();
1023 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1027 only_once(const void *ptr, const char *name)
1030 yyerror("`%s' specified more than once", name);
1034 only_oncei(int i, const char *name)
1037 yyerror("`%s' specified more than once", name);
1041 fastcgi_conf(char *path, char *port, char *prog)
1046 for (i = 0; i < FCGI_MAX; ++i) {
1049 if (f->path == NULL) {
1057 /* XXX: what to do with prog? */
1058 if (!strcmp(f->path, path) &&
1059 ((port == NULL && f->port == NULL) ||
1060 !strcmp(f->port, port))) {
1067 yyerror("too much `fastcgi' rules defined.");
1072 add_param(char *name, char *val, int env)
1082 e = xcalloc(1, sizeof(*e));
1086 TAILQ_INSERT_HEAD(h, e, envs);
1088 TAILQ_INSERT_TAIL(h, e, envs);