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 */
116 %token TCA TCERT TCGI TCHROOT TCLIENT
118 %token TENTRYPOINT TENV
122 %token TLANG TLOCATION TLOG
125 %token TPARAM TPORT TPREFORK TPROTOCOLS
126 %token TREQUIRE TRETURN TROOT
127 %token TSERVER TSPAWN TSTRIP
128 %token TTCP TTOEXT TTYPE TUSER
132 %token <v.string> STRING
133 %token <v.number> NUM
135 %type <v.number> bool
136 %type <v.string> string
146 | conf error '\n' { file->errors++; }
149 include : INCLUDE STRING {
152 if ((nfile = pushfile($2, 0)) == NULL) {
153 yyerror("failed to include file %s", $2);
164 bool : TON { $$ = 1; }
168 string : string STRING {
169 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
172 yyerror("string: asprintf: %s", strerror(errno));
181 varset : STRING '=' string {
184 if (isspace((unsigned char)*s)) {
185 yyerror("macro name cannot contain "
198 option : TCHROOT string { conf.chroot = $2; }
199 | TIPV6 bool { conf.ipv6 = $2; }
200 | TMIME STRING string {
201 yywarn("`mime MIME EXT' is deprecated and will be "
202 "removed in a future version, please use the new "
203 "syntax: `map MIME to-ext EXT'");
204 add_mime(&conf.mime, $2, $3);
206 | TMAP string TTOEXT string { add_mime(&conf.mime, $2, $4); }
207 | TPORT NUM { conf.port = check_port_num($2); }
208 | TPREFORK NUM { conf.prefork = check_prefork_num($2); }
209 | TPROTOCOLS string {
210 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
211 yyerror("invalid protocols string \"%s\"", $2);
213 | TUSER string { conf.user = $2; }
216 vhost : TSERVER string {
218 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
220 loc = new_location();
221 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
223 loc->match = xstrdup("*");
226 if (strstr($2, "xn--") != NULL) {
227 yywarn("\"%s\" looks like punycode: you "
228 "should use the decoded hostname", $2);
230 } '{' optnl servopts locations '}' {
231 if (host->cert == NULL || host->key == NULL)
232 yyerror("invalid vhost definition: %s", $2);
234 | error '}' { yyerror("bad server directive"); }
237 servopts : /* empty */
238 | servopts servopt optnl
241 servopt : TALIAS string {
244 a = xcalloc(1, sizeof(*a));
246 if (TAILQ_EMPTY(&host->aliases))
247 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
249 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
252 only_once(host->cert, "cert");
253 host->cert = ensure_absolute_path($2);
256 only_once(host->cgi, "cgi");
257 /* drop the starting '/', if any */
259 memmove($2, $2+1, strlen($2));
262 | TENTRYPOINT string {
263 only_once(host->entrypoint, "entrypoint");
265 memmove($2, $2+1, strlen($2));
266 host->entrypoint = $2;
268 | TENV string '=' string {
269 add_param($2, $4, 1);
272 only_once(host->key, "key");
273 host->key = ensure_absolute_path($2);
275 | TPARAM string '=' string {
276 add_param($2, $4, 0);
281 locations : /* empty */
282 | locations location optnl
285 location : TLOCATION { advance_loc(); } string '{' optnl locopts '}' {
286 /* drop the starting '/' if any */
288 memmove($3, $3+1, strlen($3));
294 locopts : /* empty */
295 | locopts locopt optnl
298 locopt : TAUTO TINDEX bool { loc->auto_index = $3 ? 1 : -1; }
299 | TBLOCK TRETURN NUM string {
300 only_once(loc->block_fmt, "block");
301 loc->block_fmt = check_block_fmt($4);
302 loc->block_code = check_block_code($3);
304 | TBLOCK TRETURN NUM {
305 only_once(loc->block_fmt, "block");
306 loc->block_fmt = xstrdup("temporary failure");
307 loc->block_code = check_block_code($3);
308 if ($3 >= 30 && $3 < 40)
309 yyerror("missing `meta' for block return %d", $3);
312 only_once(loc->block_fmt, "block");
313 loc->block_fmt = xstrdup("temporary failure");
314 loc->block_code = 40;
316 | TDEFAULT TTYPE string {
317 only_once(loc->default_mime, "default type");
318 loc->default_mime = $3;
322 only_once(loc->index, "index");
326 only_once(loc->lang, "lang");
329 | TLOG bool { loc->disable_log = !$2; }
330 | TREQUIRE TCLIENT TCA string {
331 only_once(loc->reqca, "require client ca");
332 ensure_absolute_path($4);
333 if ((loc->reqca = load_ca($4)) == NULL)
334 yyerror("couldn't load ca cert: %s", $4);
338 only_once(loc->dir, "root");
339 loc->dir = ensure_absolute_path($2);
341 | TSTRIP NUM { loc->strip = check_strip_no($2); }
344 fastcgi : TSPAWN string {
345 only_oncei(loc->fcgi, "fastcgi");
346 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
349 only_oncei(loc->fcgi, "fastcgi");
350 loc->fcgi = fastcgi_conf($1, NULL, NULL);
352 | TTCP string TPORT NUM {
354 if (asprintf(&c, "%d", $4) == -1)
356 only_oncei(loc->fcgi, "fastcgi");
357 loc->fcgi = fastcgi_conf($2, c, NULL);
360 only_oncei(loc->fcgi, "fastcgi");
361 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
363 | TTCP string TPORT string {
364 only_oncei(loc->fcgi, "fastcgi");
365 loc->fcgi = fastcgi_conf($2, $4, NULL);
369 optnl : '\n' optnl /* zero or more newlines */
370 | ';' optnl /* semicolons too */
376 static struct keyword {
380 /* these MUST be sorted */
389 {"default", TDEFAULT},
390 {"entrypoint", TENTRYPOINT},
392 {"fastcgi", TFASTCGI},
397 {"location", TLOCATION},
405 {"prefork", TPREFORK},
406 {"protocols", TPROTOCOLS},
407 {"require", TREQUIRE},
420 yyerror(const char *msg, ...)
427 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
428 vfprintf(stderr, msg, ap);
429 fprintf(stderr, "\n");
434 yywarn(const char *msg, ...)
439 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
440 vfprintf(stderr, msg, ap);
441 fprintf(stderr, "\n");
446 kw_cmp(const void *k, const void *e)
448 return strcmp(k, ((struct keyword *)e)->word);
454 const struct keyword *p;
456 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
457 sizeof(keywords[0]), kw_cmp);
465 #define START_EXPAND 1
466 #define DONE_EXPAND 2
468 static int expanding;
476 if (file->ungetpos > 0)
477 c = file->ungetbuf[--file->ungetpos];
479 c = getc(file->stream);
481 if (c == START_EXPAND)
483 else if (c == DONE_EXPAND)
497 if ((c = igetc()) == EOF) {
498 yyerror("reached end of file while parsing "
500 if (file == topfile || popfile() == EOF)
507 while ((c = igetc()) == '\\') {
513 yylval.lineno = file->lineno;
519 * Fake EOL when hit EOF for the first time. This gets line
520 * count right if last line in included file is syntactically
521 * invalid and has no newline.
523 if (file->eof_reached == 0) {
524 file->eof_reached = 1;
528 if (file == topfile || popfile() == EOF)
542 if (file->ungetpos >= file->ungetsize) {
543 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
547 file->ungetsize *= 2;
549 file->ungetbuf[file->ungetpos++] = c;
557 /* Skip to either EOF or the first real EOL. */
580 while ((c = lgetc(0)) == ' ' || c == '\t')
583 yylval.lineno = file->lineno;
585 while ((c = lgetc(0)) != '\n' && c != EOF)
587 if (c == '$' && !expanding) {
589 if ((c = lgetc(0)) == EOF)
591 if (p + 1 >= buf + sizeof(buf) -1) {
592 yyerror("string too long");
595 if (isalnum(c) || c == '_') {
605 yyerror("macro `%s' not defined", buf);
608 yylval.v.string = xstrdup(val);
611 if (c == '@' && !expanding) {
613 if ((c = lgetc(0)) == EOF)
616 if (p + 1 >= buf + sizeof(buf) - 1) {
617 yyerror("string too long");
620 if (isalnum(c) || c == '_') {
630 yyerror("macro '%s' not defined", buf);
633 p = val + strlen(val) - 1;
634 lungetc(DONE_EXPAND);
639 lungetc(START_EXPAND);
648 if ((c = lgetc(quotec)) == EOF)
653 } else if (c == '\\') {
654 if ((next = lgetc(quotec)) == EOF)
656 if (next == quotec || next == ' ' ||
659 else if (next == '\n') {
664 } else if (c == quotec) {
667 } else if (c == '\0') {
668 yyerror("invalid syntax");
671 if (p + 1 >= buf + sizeof(buf) - 1) {
672 yyerror("string too long");
677 yylval.v.string = strdup(buf);
678 if (yylval.v.string == NULL)
679 err(1, "yylex: strdup");
683 #define allowed_to_end_number(x) \
684 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
686 if (c == '-' || isdigit(c)) {
689 if ((size_t)(p-buf) >= sizeof(buf)) {
690 yyerror("string too long");
693 } while ((c = lgetc(0)) != EOF && isdigit(c));
695 if (p == buf + 1 && buf[0] == '-')
697 if (c == EOF || allowed_to_end_number(c)) {
698 const char *errstr = NULL;
701 yylval.v.number = strtonum(buf, LLONG_MIN,
704 yyerror("\"%s\" invalid number: %s",
719 #define allowed_in_string(x) \
720 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
721 x != '{' && x != '}' && \
722 x != '!' && x != '=' && x != '#' && \
723 x != ',' && x != ';'))
725 if (isalnum(c) || c == ':' || c == '_') {
728 if ((size_t)(p-buf) >= sizeof(buf)) {
729 yyerror("string too long");
732 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
735 if ((token = lookup(buf)) == STRING)
736 yylval.v.string = xstrdup(buf);
740 yylval.lineno = file->lineno;
749 pushfile(const char *name, int secret)
753 nfile = xcalloc(1, sizeof(*nfile));
754 nfile->name = xstrdup(name);
755 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
756 yyerror("can't open %s: %s", nfile->name,
762 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
763 nfile->ungetsize = 16;
764 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
765 TAILQ_INSERT_TAIL(&files, nfile, entry);
774 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
775 prev->errors += file->errors;
777 TAILQ_REMOVE(&files, file, entry);
778 fclose(file->stream);
780 free(file->ungetbuf);
783 return file ? 0 : EOF;
787 parse_conf(const char *filename)
789 struct sym *sym, *next;
791 file = pushfile(filename, 0);
797 errors = file->errors;
800 /* Free macros and check which have not been used. */
801 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
802 /* TODO: warn if !sym->used */
806 TAILQ_REMOVE(&symhead, sym, entry);
816 symset(const char *name, const char *val, int persist)
820 TAILQ_FOREACH(sym, &symhead, entry) {
821 if (!strcmp(name, sym->name))
831 TAILQ_REMOVE(&symhead, sym, entry);
836 sym = xcalloc(1, sizeof(*sym));
837 sym->name = xstrdup(name);
838 sym->val = xstrdup(val);
840 sym->persist = persist;
842 TAILQ_INSERT_TAIL(&symhead, sym, entry);
847 cmdline_symset(char *s)
852 if ((val = strrchr(s, '=')) == NULL)
854 sym = xcalloc(1, val - s + 1);
855 memcpy(sym, s, val - s);
856 ret = symset(sym, val + 1, 1);
862 symget(const char *nam)
866 TAILQ_FOREACH(sym, &symhead, entry) {
867 if (strcmp(nam, sym->name) == 0) {
878 return xcalloc(1, sizeof(struct vhost));
886 l = xcalloc(1, sizeof(*l));
893 ensure_absolute_path(char *path)
895 if (path == NULL || *path != '/')
896 yyerror("not an absolute path: %s", path);
901 check_block_code(int n)
903 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
904 yyerror("invalid block code %d", n);
909 check_block_fmt(char *fmt)
913 for (s = fmt; *s; ++s) {
924 yyerror("invalid format specifier %%%c", *s);
932 check_strip_no(int n)
935 yyerror("invalid strip number %d", n);
940 check_port_num(int n)
942 if (n <= 0 || n >= UINT16_MAX)
943 yyerror("port number is %s: %d",
944 n <= 0 ? "too small" : "too large",
950 check_prefork_num(int n)
952 if (n <= 0 || n >= PROC_MAX)
953 yyerror("invalid prefork number %d", n);
960 loc = new_location();
961 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
965 only_once(const void *ptr, const char *name)
968 yyerror("`%s' specified more than once", name);
972 only_oncei(int i, const char *name)
975 yyerror("`%s' specified more than once", name);
979 fastcgi_conf(char *path, char *port, char *prog)
984 for (i = 0; i < FCGI_MAX; ++i) {
987 if (f->path == NULL) {
995 /* XXX: what to do with prog? */
996 if (!strcmp(f->path, path) &&
997 ((port == NULL && f->port == NULL) ||
998 !strcmp(f->port, port))) {
1005 yyerror("too much `fastcgi' rules defined.");
1010 add_param(char *name, char *val, int env)
1020 e = xcalloc(1, sizeof(*e));
1024 TAILQ_INSERT_HEAD(h, e, envs);
1026 TAILQ_INSERT_TAIL(h, e, envs);