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 */
113 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
114 %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
115 %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
116 %token TFASTCGI TSPAWN TPARAM TMAP TTOEXT INCLUDE TON TOFF
120 %token <v.string> STRING
121 %token <v.number> NUM
123 %type <v.number> bool
124 %type <v.string> string
134 | conf error '\n' { file->errors++; }
137 include : INCLUDE STRING {
140 if ((nfile = pushfile($2, 0)) == NULL) {
141 yyerror("failed to include file %s", $2);
152 bool : TON { $$ = 1; }
156 string : string STRING {
157 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
160 yyerror("string: asprintf: %s", strerror(errno));
169 varset : STRING '=' string {
172 if (isspace((unsigned char)*s)) {
173 yyerror("macro name cannot contain "
186 option : TCHROOT string { conf.chroot = $2; }
187 | TIPV6 bool { conf.ipv6 = $2; }
188 | TMIME STRING string {
189 yywarn("`mime MIME EXT' is deprecated and will be "
190 "removed in a future version, please use the new "
191 "syntax: `map MIME to-ext EXT'");
192 add_mime(&conf.mime, $2, $3);
194 | TMAP string TTOEXT string { add_mime(&conf.mime, $2, $4); }
195 | TPORT NUM { conf.port = check_port_num($2); }
196 | TPREFORK NUM { conf.prefork = check_prefork_num($2); }
197 | TPROTOCOLS string {
198 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
199 yyerror("invalid protocols string \"%s\"", $2);
201 | TUSER string { conf.user = $2; }
204 vhost : TSERVER string {
206 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
208 loc = new_location();
209 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
211 loc->match = xstrdup("*");
214 if (strstr($2, "xn--") != NULL) {
215 yywarn("\"%s\" looks like punycode: you "
216 "should use the decoded hostname", $2);
218 } '{' optnl servopts locations '}' {
219 if (host->cert == NULL || host->key == NULL)
220 yyerror("invalid vhost definition: %s", $2);
222 | error '}' { yyerror("bad server directive"); }
225 servopts : /* empty */
226 | servopts servopt optnl
229 servopt : TALIAS string {
232 a = xcalloc(1, sizeof(*a));
234 if (TAILQ_EMPTY(&host->aliases))
235 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
237 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
240 only_once(host->cert, "cert");
241 host->cert = ensure_absolute_path($2);
244 only_once(host->cgi, "cgi");
245 /* drop the starting '/', if any */
247 memmove($2, $2+1, strlen($2));
250 | TENTRYPOINT string {
251 only_once(host->entrypoint, "entrypoint");
253 memmove($2, $2+1, strlen($2));
254 host->entrypoint = $2;
256 | TENV string '=' string {
257 add_param($2, $4, 1);
260 only_once(host->key, "key");
261 host->key = ensure_absolute_path($2);
263 | TPARAM string '=' string {
264 add_param($2, $4, 0);
269 locations : /* empty */
270 | locations location optnl
273 location : TLOCATION { advance_loc(); } string '{' optnl locopts '}' {
274 /* drop the starting '/' if any */
276 memmove($3, $3+1, strlen($3));
282 locopts : /* empty */
283 | locopts locopt optnl
286 locopt : TAUTO TINDEX bool { loc->auto_index = $3 ? 1 : -1; }
287 | TBLOCK TRETURN NUM string {
288 only_once(loc->block_fmt, "block");
289 loc->block_fmt = check_block_fmt($4);
290 loc->block_code = check_block_code($3);
292 | TBLOCK TRETURN NUM {
293 only_once(loc->block_fmt, "block");
294 loc->block_fmt = xstrdup("temporary failure");
295 loc->block_code = check_block_code($3);
296 if ($3 >= 30 && $3 < 40)
297 yyerror("missing `meta' for block return %d", $3);
300 only_once(loc->block_fmt, "block");
301 loc->block_fmt = xstrdup("temporary failure");
302 loc->block_code = 40;
304 | TDEFAULT TTYPE string {
305 only_once(loc->default_mime, "default type");
306 loc->default_mime = $3;
310 only_once(loc->index, "index");
314 only_once(loc->lang, "lang");
317 | TLOG bool { loc->disable_log = !$2; }
318 | TREQUIRE TCLIENT TCA string {
319 only_once(loc->reqca, "require client ca");
320 ensure_absolute_path($4);
321 if ((loc->reqca = load_ca($4)) == NULL)
322 yyerror("couldn't load ca cert: %s", $4);
326 only_once(loc->dir, "root");
327 loc->dir = ensure_absolute_path($2);
329 | TSTRIP NUM { loc->strip = check_strip_no($2); }
332 fastcgi : TSPAWN string {
333 only_oncei(loc->fcgi, "fastcgi");
334 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
337 only_oncei(loc->fcgi, "fastcgi");
338 loc->fcgi = fastcgi_conf($1, NULL, NULL);
340 | TTCP string TPORT NUM {
342 if (asprintf(&c, "%d", $4) == -1)
344 only_oncei(loc->fcgi, "fastcgi");
345 loc->fcgi = fastcgi_conf($2, c, NULL);
348 only_oncei(loc->fcgi, "fastcgi");
349 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
351 | TTCP string TPORT string {
352 only_oncei(loc->fcgi, "fastcgi");
353 loc->fcgi = fastcgi_conf($2, $4, NULL);
357 optnl : '\n' optnl /* zero or more newlines */
358 | ';' optnl /* semicolons too */
364 static struct keyword {
368 /* these MUST be sorted */
377 {"default", TDEFAULT},
378 {"entrypoint", TENTRYPOINT},
380 {"fastcgi", TFASTCGI},
385 {"location", TLOCATION},
393 {"prefork", TPREFORK},
394 {"protocols", TPROTOCOLS},
395 {"require", TREQUIRE},
408 yyerror(const char *msg, ...)
415 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
416 vfprintf(stderr, msg, ap);
417 fprintf(stderr, "\n");
422 yywarn(const char *msg, ...)
427 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
428 vfprintf(stderr, msg, ap);
429 fprintf(stderr, "\n");
434 kw_cmp(const void *k, const void *e)
436 return strcmp(k, ((struct keyword *)e)->word);
442 const struct keyword *p;
444 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
445 sizeof(keywords[0]), kw_cmp);
453 #define START_EXPAND 1
454 #define DONE_EXPAND 2
456 static int expanding;
464 if (file->ungetpos > 0)
465 c = file->ungetbuf[--file->ungetpos];
467 c = getc(file->stream);
469 if (c == START_EXPAND)
471 else if (c == DONE_EXPAND)
485 if ((c = igetc()) == EOF) {
486 yyerror("reached end of file while parsing "
488 if (file == topfile || popfile() == EOF)
495 while ((c = igetc()) == '\\') {
501 yylval.lineno = file->lineno;
507 * Fake EOL when hit EOF for the first time. This gets line
508 * count right if last line in included file is syntactically
509 * invalid and has no newline.
511 if (file->eof_reached == 0) {
512 file->eof_reached = 1;
516 if (file == topfile || popfile() == EOF)
530 if (file->ungetpos >= file->ungetsize) {
531 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
535 file->ungetsize *= 2;
537 file->ungetbuf[file->ungetpos++] = c;
545 /* Skip to either EOF or the first real EOL. */
568 while ((c = lgetc(0)) == ' ' || c == '\t')
571 yylval.lineno = file->lineno;
573 while ((c = lgetc(0)) != '\n' && c != EOF)
575 if (c == '$' && !expanding) {
577 if ((c = lgetc(0)) == EOF)
579 if (p + 1 >= buf + sizeof(buf) -1) {
580 yyerror("string too long");
583 if (isalnum(c) || c == '_') {
593 yyerror("macro `%s' not defined", buf);
596 yylval.v.string = xstrdup(val);
599 if (c == '@' && !expanding) {
601 if ((c = lgetc(0)) == EOF)
604 if (p + 1 >= buf + sizeof(buf) - 1) {
605 yyerror("string too long");
608 if (isalnum(c) || c == '_') {
618 yyerror("macro '%s' not defined", buf);
621 p = val + strlen(val) - 1;
622 lungetc(DONE_EXPAND);
627 lungetc(START_EXPAND);
636 if ((c = lgetc(quotec)) == EOF)
641 } else if (c == '\\') {
642 if ((next = lgetc(quotec)) == EOF)
644 if (next == quotec || next == ' ' ||
647 else if (next == '\n') {
652 } else if (c == quotec) {
655 } else if (c == '\0') {
656 yyerror("invalid syntax");
659 if (p + 1 >= buf + sizeof(buf) - 1) {
660 yyerror("string too long");
665 yylval.v.string = strdup(buf);
666 if (yylval.v.string == NULL)
667 err(1, "yylex: strdup");
671 #define allowed_to_end_number(x) \
672 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
674 if (c == '-' || isdigit(c)) {
677 if ((size_t)(p-buf) >= sizeof(buf)) {
678 yyerror("string too long");
681 } while ((c = lgetc(0)) != EOF && isdigit(c));
683 if (p == buf + 1 && buf[0] == '-')
685 if (c == EOF || allowed_to_end_number(c)) {
686 const char *errstr = NULL;
689 yylval.v.number = strtonum(buf, LLONG_MIN,
692 yyerror("\"%s\" invalid number: %s",
707 #define allowed_in_string(x) \
708 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
709 x != '{' && x != '}' && \
710 x != '!' && x != '=' && x != '#' && \
711 x != ',' && x != ';'))
713 if (isalnum(c) || c == ':' || c == '_') {
716 if ((size_t)(p-buf) >= sizeof(buf)) {
717 yyerror("string too long");
720 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
723 if ((token = lookup(buf)) == STRING)
724 yylval.v.string = xstrdup(buf);
728 yylval.lineno = file->lineno;
737 pushfile(const char *name, int secret)
741 nfile = xcalloc(1, sizeof(*nfile));
742 nfile->name = xstrdup(name);
743 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
744 yyerror("can't open %s: %s", nfile->name,
750 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
751 nfile->ungetsize = 16;
752 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
753 TAILQ_INSERT_TAIL(&files, nfile, entry);
762 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
763 prev->errors += file->errors;
765 TAILQ_REMOVE(&files, file, entry);
766 fclose(file->stream);
768 free(file->ungetbuf);
771 return file ? 0 : EOF;
775 parse_conf(const char *filename)
777 struct sym *sym, *next;
779 file = pushfile(filename, 0);
785 errors = file->errors;
788 /* Free macros and check which have not been used. */
789 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
790 /* TODO: warn if !sym->used */
794 TAILQ_REMOVE(&symhead, sym, entry);
804 symset(const char *name, const char *val, int persist)
808 TAILQ_FOREACH(sym, &symhead, entry) {
809 if (!strcmp(name, sym->name))
819 TAILQ_REMOVE(&symhead, sym, entry);
824 sym = xcalloc(1, sizeof(*sym));
825 sym->name = xstrdup(name);
826 sym->val = xstrdup(val);
828 sym->persist = persist;
830 TAILQ_INSERT_TAIL(&symhead, sym, entry);
835 cmdline_symset(char *s)
840 if ((val = strrchr(s, '=')) == NULL)
842 sym = xcalloc(1, val - s + 1);
843 memcpy(sym, s, val - s);
844 ret = symset(sym, val + 1, 1);
850 symget(const char *nam)
854 TAILQ_FOREACH(sym, &symhead, entry) {
855 if (strcmp(nam, sym->name) == 0) {
866 return xcalloc(1, sizeof(struct vhost));
874 l = xcalloc(1, sizeof(*l));
881 ensure_absolute_path(char *path)
883 if (path == NULL || *path != '/')
884 yyerror("not an absolute path: %s", path);
889 check_block_code(int n)
891 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
892 yyerror("invalid block code %d", n);
897 check_block_fmt(char *fmt)
901 for (s = fmt; *s; ++s) {
912 yyerror("invalid format specifier %%%c", *s);
920 check_strip_no(int n)
923 yyerror("invalid strip number %d", n);
928 check_port_num(int n)
930 if (n <= 0 || n >= UINT16_MAX)
931 yyerror("port number is %s: %d",
932 n <= 0 ? "too small" : "too large",
938 check_prefork_num(int n)
940 if (n <= 0 || n >= PROC_MAX)
941 yyerror("invalid prefork number %d", n);
948 loc = new_location();
949 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
953 only_once(const void *ptr, const char *name)
956 yyerror("`%s' specified more than once", name);
960 only_oncei(int i, const char *name)
963 yyerror("`%s' specified more than once", name);
967 fastcgi_conf(char *path, char *port, char *prog)
972 for (i = 0; i < FCGI_MAX; ++i) {
975 if (f->path == NULL) {
983 /* XXX: what to do with prog? */
984 if (!strcmp(f->path, path) &&
985 ((port == NULL && f->port == NULL) ||
986 !strcmp(f->port, port))) {
993 yyerror("too much `fastcgi' rules defined.");
998 add_param(char *name, char *val, int env)
1008 e = xcalloc(1, sizeof(*e));
1012 TAILQ_INSERT_HEAD(h, e, envs);
1014 TAILQ_INSERT_TAIL(h, e, envs);