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 int kw_cmp(const void *, const void *);
67 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
69 TAILQ_ENTRY(sym) entry;
76 int symset(const char *, const char *, int);
77 char *symget(const char *);
79 struct vhost *new_vhost(void);
80 struct location *new_location(void);
81 char *ensure_absolute_path(char*);
82 int check_block_code(int);
83 char *check_block_fmt(char*);
84 int check_strip_no(int);
85 int check_port_num(int);
86 int check_prefork_num(int);
87 void advance_loc(void);
88 void only_once(const void*, const char*);
89 void only_oncei(int, const char*);
90 int fastcgi_conf(char *, char *, char *);
91 void add_param(char *, char *, int);
93 static struct vhost *host;
94 static struct location *loc;
108 /* %define parse.error verbose */
110 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
111 %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
112 %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
113 %token TFASTCGI TSPAWN TPARAM TMAP TTOEXT INCLUDE TON TOFF
117 %token <v.string> STRING
118 %token <v.number> NUM
120 %type <v.number> bool
121 %type <v.string> string
131 | conf error '\n' { file->errors++; }
134 include : INCLUDE STRING {
137 if ((nfile = pushfile($2, 0)) == NULL) {
138 yyerror("failed to include file %s", $2);
149 bool : TON { $$ = 1; }
153 string : string STRING {
154 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
157 yyerror("string: asprintf: %s", strerror(errno));
166 varset : STRING '=' string {
169 if (isspace((unsigned char)*s)) {
170 yyerror("macro name cannot contain "
183 option : TCHROOT string { conf.chroot = $2; }
184 | TIPV6 bool { conf.ipv6 = $2; }
185 | TMIME STRING string {
186 fprintf(stderr, "%s:%d: `mime MIME EXT' is deprecated and "
187 "will be removed in a future version, "
188 "please use the new syntax: `map MIME to-ext EXT'\n",
189 config_path, yylval.lineno+1);
190 add_mime(&conf.mime, $2, $3);
192 | TMAP string TTOEXT string { add_mime(&conf.mime, $2, $4); }
193 | TPORT NUM { conf.port = check_port_num($2); }
194 | TPREFORK NUM { conf.prefork = check_prefork_num($2); }
195 | TPROTOCOLS string {
196 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
197 yyerror("invalid protocols string \"%s\"", $2);
199 | TUSER string { conf.user = $2; }
202 vhost : TSERVER string {
204 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
206 loc = new_location();
207 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
209 loc->match = xstrdup("*");
212 if (strstr($2, "xn--") != NULL) {
213 warnx("%s:%d \"%s\" looks like punycode: "
214 "you should use the decoded hostname.",
215 config_path, yylval.lineno+1, $2);
217 } '{' optnl servopts locations '}' {
218 if (host->cert == NULL || host->key == NULL)
219 yyerror("invalid vhost definition: %s", $2);
221 | error '}' { yyerror("error in server directive"); }
224 servopts : /* empty */
225 | servopts servopt optnl
228 servopt : TALIAS string {
231 a = xcalloc(1, sizeof(*a));
233 if (TAILQ_EMPTY(&host->aliases))
234 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
236 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
239 only_once(host->cert, "cert");
240 host->cert = ensure_absolute_path($2);
243 only_once(host->cgi, "cgi");
244 /* drop the starting '/', if any */
246 memmove($2, $2+1, strlen($2));
249 | TENTRYPOINT string {
250 only_once(host->entrypoint, "entrypoint");
252 memmove($2, $2+1, strlen($2));
253 host->entrypoint = $2;
255 | TENV string '=' string {
256 add_param($2, $4, 1);
259 only_once(host->key, "key");
260 host->key = ensure_absolute_path($2);
262 | TPARAM string '=' string {
263 add_param($2, $4, 0);
268 locations : /* empty */
269 | locations location optnl
272 location : TLOCATION { advance_loc(); } string '{' optnl locopts '}' {
273 /* drop the starting '/' if any */
275 memmove($3, $3+1, strlen($3));
281 locopts : /* empty */
282 | locopts locopt optnl
285 locopt : TAUTO TINDEX bool { loc->auto_index = $3 ? 1 : -1; }
286 | TBLOCK TRETURN NUM string {
287 only_once(loc->block_fmt, "block");
288 loc->block_fmt = check_block_fmt($4);
289 loc->block_code = check_block_code($3);
291 | TBLOCK TRETURN NUM {
292 only_once(loc->block_fmt, "block");
293 loc->block_fmt = xstrdup("temporary failure");
294 loc->block_code = check_block_code($3);
295 if ($3 >= 30 && $3 < 40)
296 yyerror("missing `meta' for block return %d", $3);
299 only_once(loc->block_fmt, "block");
300 loc->block_fmt = xstrdup("temporary failure");
301 loc->block_code = 40;
303 | TDEFAULT TTYPE string {
304 only_once(loc->default_mime, "default type");
305 loc->default_mime = $3;
309 only_once(loc->index, "index");
313 only_once(loc->lang, "lang");
316 | TLOG bool { loc->disable_log = !$2; }
317 | TREQUIRE TCLIENT TCA string {
318 only_once(loc->reqca, "require client ca");
319 ensure_absolute_path($4);
320 if ((loc->reqca = load_ca($4)) == NULL)
321 yyerror("couldn't load ca cert: %s", $4);
325 only_once(loc->dir, "root");
326 loc->dir = ensure_absolute_path($2);
328 | TSTRIP NUM { loc->strip = check_strip_no($2); }
331 fastcgi : TSPAWN string {
332 only_oncei(loc->fcgi, "fastcgi");
333 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
336 only_oncei(loc->fcgi, "fastcgi");
337 loc->fcgi = fastcgi_conf($1, NULL, NULL);
339 | TTCP string TPORT NUM {
341 if (asprintf(&c, "%d", $4) == -1)
343 only_oncei(loc->fcgi, "fastcgi");
344 loc->fcgi = fastcgi_conf($2, c, NULL);
347 only_oncei(loc->fcgi, "fastcgi");
348 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
350 | TTCP string TPORT string {
351 only_oncei(loc->fcgi, "fastcgi");
352 loc->fcgi = fastcgi_conf($2, $4, NULL);
356 optnl : '\n' optnl /* zero or more newlines */
357 | ';' optnl /* semicolons too */
363 static struct keyword {
367 /* these MUST be sorted */
376 {"default", TDEFAULT},
377 {"entrypoint", TENTRYPOINT},
379 {"fastcgi", TFASTCGI},
384 {"location", TLOCATION},
392 {"prefork", TPREFORK},
393 {"protocols", TPROTOCOLS},
394 {"require", TREQUIRE},
407 yyerror(const char *msg, ...)
414 fprintf(stderr, "%s:%d: ", config_path, yylval.lineno);
415 vfprintf(stderr, msg, ap);
416 fprintf(stderr, "\n");
421 kw_cmp(const void *k, const void *e)
423 return strcmp(k, ((struct keyword *)e)->word);
429 const struct keyword *p;
431 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
432 sizeof(keywords[0]), kw_cmp);
440 #define START_EXPAND 1
441 #define DONE_EXPAND 2
443 static int expanding;
451 if (file->ungetpos > 0)
452 c = file->ungetbuf[--file->ungetpos];
454 c = getc(file->stream);
456 if (c == START_EXPAND)
458 else if (c == DONE_EXPAND)
472 if ((c = igetc()) == EOF) {
473 yyerror("reached end of file while parsing "
475 if (file == topfile || popfile() == EOF)
482 while ((c = igetc()) == '\\') {
488 yylval.lineno = file->lineno;
494 * Fake EOL when hit EOF for the first time. This gets line
495 * count right if last line in included file is syntactically
496 * invalid and has no newline.
498 if (file->eof_reached == 0) {
499 file->eof_reached = 1;
503 if (file == topfile || popfile() == EOF)
517 if (file->ungetpos >= file->ungetsize) {
518 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
522 file->ungetsize *= 2;
524 file->ungetbuf[file->ungetpos++] = c;
532 /* Skip to either EOF or the first real EOL. */
555 while ((c = lgetc(0)) == ' ' || c == '\t')
558 yylval.lineno = file->lineno;
560 while ((c = lgetc(0)) != '\n' && c != EOF)
562 if (c == '$' && !expanding) {
564 if ((c = lgetc(0)) == EOF)
566 if (p + 1 >= buf + sizeof(buf) -1) {
567 yyerror("string too long");
570 if (isalnum(c) || c == '_') {
580 yyerror("macro `%s' not defined", buf);
583 yylval.v.string = xstrdup(val);
586 if (c == '@' && !expanding) {
588 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 p = val + strlen(val) - 1;
609 lungetc(DONE_EXPAND);
614 lungetc(START_EXPAND);
623 if ((c = lgetc(quotec)) == EOF)
628 } else if (c == '\\') {
629 if ((next = lgetc(quotec)) == EOF)
631 if (next == quotec || next == ' ' ||
634 else if (next == '\n') {
639 } else if (c == quotec) {
642 } else if (c == '\0') {
643 yyerror("syntax error");
646 if (p + 1 >= buf + sizeof(buf) - 1) {
647 yyerror("string too long");
652 yylval.v.string = strdup(buf);
653 if (yylval.v.string == NULL)
654 err(1, "yylex: strdup");
658 #define allowed_to_end_number(x) \
659 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
661 if (c == '-' || isdigit(c)) {
664 if ((size_t)(p-buf) >= sizeof(buf)) {
665 yyerror("string too long");
668 } while ((c = lgetc(0)) != EOF && isdigit(c));
670 if (p == buf + 1 && buf[0] == '-')
672 if (c == EOF || allowed_to_end_number(c)) {
673 const char *errstr = NULL;
676 yylval.v.number = strtonum(buf, LLONG_MIN,
679 yyerror("\"%s\" invalid number: %s",
694 #define allowed_in_string(x) \
695 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
696 x != '{' && x != '}' && \
697 x != '!' && x != '=' && x != '#' && \
698 x != ',' && x != ';'))
700 if (isalnum(c) || c == ':' || c == '_') {
703 if ((size_t)(p-buf) >= sizeof(buf)) {
704 yyerror("string too long");
707 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
710 if ((token = lookup(buf)) == STRING)
711 yylval.v.string = xstrdup(buf);
715 yylval.lineno = file->lineno;
724 pushfile(const char *name, int secret)
728 nfile = xcalloc(1, sizeof(*nfile));
729 nfile->name = xstrdup(name);
730 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
731 yyerror("can't open %s: %s", nfile->name,
737 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
738 nfile->ungetsize = 16;
739 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
740 TAILQ_INSERT_TAIL(&files, nfile, entry);
749 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
750 prev->errors += file->errors;
752 TAILQ_REMOVE(&files, file, entry);
753 fclose(file->stream);
755 free(file->ungetbuf);
758 return file ? 0 : EOF;
762 parse_conf(const char *filename)
764 struct sym *sym, *next;
766 file = pushfile(filename, 0);
772 errors = file->errors;
775 /* Free macros and check which have not been used. */
776 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
777 /* TODO: warn if !sym->used */
781 TAILQ_REMOVE(&symhead, sym, entry);
791 symset(const char *name, const char *val, int persist)
795 TAILQ_FOREACH(sym, &symhead, entry) {
796 if (!strcmp(name, sym->name))
806 TAILQ_REMOVE(&symhead, sym, entry);
811 sym = xcalloc(1, sizeof(*sym));
812 sym->name = xstrdup(name);
813 sym->val = xstrdup(val);
815 sym->persist = persist;
817 TAILQ_INSERT_TAIL(&symhead, sym, entry);
822 cmdline_symset(char *s)
827 if ((val = strrchr(s, '=')) == NULL)
829 sym = xcalloc(1, val - s + 1);
830 memcpy(sym, s, val - s);
831 ret = symset(sym, val + 1, 1);
837 symget(const char *nam)
841 TAILQ_FOREACH(sym, &symhead, entry) {
842 if (strcmp(nam, sym->name) == 0) {
853 return xcalloc(1, sizeof(struct vhost));
861 l = xcalloc(1, sizeof(*l));
868 ensure_absolute_path(char *path)
870 if (path == NULL || *path != '/')
871 yyerror("not an absolute path: %s", path);
876 check_block_code(int n)
878 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
879 yyerror("invalid block code %d", n);
884 check_block_fmt(char *fmt)
888 for (s = fmt; *s; ++s) {
899 yyerror("invalid format specifier %%%c", *s);
907 check_strip_no(int n)
910 yyerror("invalid strip number %d", n);
915 check_port_num(int n)
917 if (n <= 0 || n >= UINT16_MAX)
918 yyerror("port number is %s: %d",
919 n <= 0 ? "too small" : "too large",
925 check_prefork_num(int n)
927 if (n <= 0 || n >= PROC_MAX)
928 yyerror("invalid prefork number %d", n);
935 loc = new_location();
936 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
940 only_once(const void *ptr, const char *name)
943 yyerror("`%s' specified more than once", name);
947 only_oncei(int i, const char *name)
950 yyerror("`%s' specified more than once", name);
954 fastcgi_conf(char *path, char *port, char *prog)
959 for (i = 0; i < FCGI_MAX; ++i) {
962 if (f->path == NULL) {
970 /* XXX: what to do with prog? */
971 if (!strcmp(f->path, path) &&
972 ((port == NULL && f->port == NULL) ||
973 !strcmp(f->port, port))) {
980 yyerror("too much `fastcgi' rules defined.");
985 add_param(char *name, char *val, int env)
995 e = xcalloc(1, sizeof(*e));
999 TAILQ_INSERT_HEAD(h, e, envs);
1001 TAILQ_INSERT_TAIL(h, e, envs);