4 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
39 static int lineno, colno;
41 static struct vhost *new_vhost(void);
42 static struct location *new_location(void);
44 void yyerror(const char*, ...);
45 static int yylex(void);
46 int parse_portno(const char*);
47 void parse_conf(const char*);
48 char *ensure_absolute_path(char*);
49 int check_block_code(int);
50 char *check_block_fmt(char*);
51 int check_strip_no(int);
52 int check_prefork_num(int);
53 void advance_loc(void);
54 void only_once(const void*, const char*);
55 void only_oncei(int, const char*);
56 int fastcgi_conf(char *, char *, char *);
57 void add_param(char *, char *, int);
62 /* %define parse.error verbose */
69 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
70 %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
71 %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
72 %token TFASTCGI TSPAWN TPARAM
82 conf : options vhosts ;
88 option : TCHROOT TSTRING { conf.chroot = $2; }
89 | TIPV6 TBOOL { conf.ipv6 = $2; }
90 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
91 | TPORT TNUM { conf.port = $2; }
92 | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
93 | TPROTOCOLS TSTRING {
94 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
95 yyerror("invalid protocols string \"%s\"", $2);
97 | TUSER TSTRING { conf.user = $2; }
104 vhost : TSERVER TSTRING {
106 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
108 loc = new_location();
109 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
111 loc->match = xstrdup("*");
114 if (strstr($2, "xn--") != NULL) {
115 warnx("%s:%d \"%s\" looks like punycode: "
116 "you should use the decoded hostname.",
117 config_path, lineno, $2);
119 } '{' servopts locations '}' {
121 if (host->cert == NULL || host->key == NULL)
122 yyerror("invalid vhost definition: %s", $2);
124 | error '}' { yyerror("error in server directive"); }
127 servopts : /* empty */
131 servopt : TALIAS TSTRING {
134 a = xcalloc(1, sizeof(*a));
136 if (TAILQ_EMPTY(&host->aliases))
137 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
139 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
142 only_once(host->cert, "cert");
143 host->cert = ensure_absolute_path($2);
146 only_once(host->cgi, "cgi");
147 /* drop the starting '/', if any */
149 memmove($2, $2+1, strlen($2));
152 | TENTRYPOINT TSTRING {
153 only_once(host->entrypoint, "entrypoint");
155 memmove($2, $2+1, strlen($2));
156 host->entrypoint = $2;
158 | TENV TSTRING TSTRING {
159 add_param($2, $3, 1);
162 only_once(host->key, "key");
163 host->key = ensure_absolute_path($2);
165 | TPARAM TSTRING TSTRING {
166 add_param($2, $3, 0);
171 locations : /* empty */
175 location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
176 /* drop the starting '/' if any */
178 memmove($3, $3+1, strlen($3));
184 locopts : /* empty */
188 locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
189 | TBLOCK TRETURN TNUM TSTRING {
190 only_once(loc->block_fmt, "block");
191 loc->block_fmt = check_block_fmt($4);
192 loc->block_code = check_block_code($3);
194 | TBLOCK TRETURN TNUM {
195 only_once(loc->block_fmt, "block");
196 loc->block_fmt = xstrdup("temporary failure");
197 loc->block_code = check_block_code($3);
198 if ($3 >= 30 && $3 < 40)
199 yyerror("missing `meta' for block return %d", $3);
202 only_once(loc->block_fmt, "block");
203 loc->block_fmt = xstrdup("temporary failure");
204 loc->block_code = 40;
206 | TDEFAULT TTYPE TSTRING {
207 only_once(loc->default_mime, "default type");
208 loc->default_mime = $3;
212 only_once(loc->index, "index");
216 only_once(loc->lang, "lang");
219 | TLOG TBOOL { loc->disable_log = !$2; }
220 | TREQUIRE TCLIENT TCA TSTRING {
221 only_once(loc->reqca, "require client ca");
222 ensure_absolute_path($4);
223 if ((loc->reqca = load_ca($4)) == NULL)
224 yyerror("couldn't load ca cert: %s", $4);
228 only_once(loc->dir, "root");
229 loc->dir = ensure_absolute_path($2);
231 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
234 fastcgi : TSPAWN TSTRING {
235 only_oncei(loc->fcgi, "fastcgi");
236 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
239 only_oncei(loc->fcgi, "fastcgi");
240 loc->fcgi = fastcgi_conf($1, NULL, NULL);
242 | TTCP TSTRING TNUM {
244 if (asprintf(&c, "%d", $3) == -1)
246 only_oncei(loc->fcgi, "fastcgi");
247 loc->fcgi = fastcgi_conf($2, c, NULL);
250 only_oncei(loc->fcgi, "fastcgi");
251 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
253 | TTCP TSTRING TSTRING {
254 only_oncei(loc->fcgi, "fastcgi");
255 loc->fcgi = fastcgi_conf($2, $3, NULL);
261 static struct vhost *
264 return xcalloc(1, sizeof(struct vhost));
267 static struct location *
272 l = xcalloc(1, sizeof(*l));
279 yyerror(const char *msg, ...)
286 fprintf(stderr, "%s:%d: ", config_path, lineno);
287 vfprintf(stderr, msg, ap);
288 fprintf(stderr, "\n");
292 static struct keyword {
304 {"default", TDEFAULT},
305 {"entrypoint", TENTRYPOINT},
307 {"fastcgi", TFASTCGI},
312 {"location", TLOCATION},
317 {"prefork", TPREFORK},
318 {"protocols", TPROTOCOLS},
319 {"require", TREQUIRE},
331 * Taken an adapted from doas' parse.y
336 char buf[1024], *ebuf, *p, *str;
337 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
341 ebuf = buf + sizeof(buf);
344 /* skip whitespace first */
345 for (c = getc(yyfp); isspace(c); c = getc(yyfp)) {
353 /* check for special one-character constructions */
359 /* skip comments; NUL is allowed; no continuation */
360 while ((c = getc(yyfp)) != '\n')
370 /* parsing next word */
371 for (;; c = getc(yyfp), colno++) {
374 yyerror("unallowed character NULL in column %d",
385 yyerror("unterminated quotes in column %d",
396 yyerror("unterminated escape in column %d",
399 yyerror("unterminated quotes in column %d",
407 if (!escape && !quotes)
422 yyerror("line too long");
434 * There could be a number of reason for empty buffer,
435 * and we handle all of them here, to avoid cluttering
440 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
444 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); ++i) {
445 if (!strcmp(buf, keywords[i].word))
446 return keywords[i].token;
450 if (!nonkw && (c == '-' || isdigit(c))) {
451 yylval.num = parse_portno(buf);
454 if (!nonkw && !strcmp(buf, "on")) {
458 if (!nonkw && !strcmp(buf, "off")) {
462 if ((str = strdup(buf)) == NULL)
463 err(1, "%s", __func__);
469 yyerror("input error reading config");
474 parse_portno(const char *p)
479 n = strtonum(p, 0, UINT16_MAX, &errstr);
481 yyerror("port number is %s: %s", errstr, p);
486 parse_conf(const char *path)
489 if ((yyfp = fopen(path, "r")) == NULL)
490 err(1, "cannot open config: %s", path);
497 if (TAILQ_FIRST(&hosts)->domain == NULL)
498 errx(1, "no vhost defined in %s", path);
502 ensure_absolute_path(char *path)
504 if (path == NULL || *path != '/')
505 yyerror("not an absolute path: %s", path);
510 check_block_code(int n)
512 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
513 yyerror("invalid block code %d", n);
518 check_block_fmt(char *fmt)
522 for (s = fmt; *s; ++s) {
533 yyerror("invalid format specifier %%%c", *s);
541 check_strip_no(int n)
544 yyerror("invalid strip number %d", n);
549 check_prefork_num(int n)
551 if (n <= 0 || n >= PROC_MAX)
552 yyerror("invalid prefork number %d", n);
559 loc = new_location();
560 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
564 only_once(const void *ptr, const char *name)
567 yyerror("`%s' specified more than once", name);
571 only_oncei(int i, const char *name)
574 yyerror("`%s' specified more than once", name);
578 fastcgi_conf(char *path, char *port, char *prog)
583 for (i = 0; i < FCGI_MAX; ++i) {
586 if (f->path == NULL) {
594 /* XXX: what to do with prog? */
595 if (!strcmp(f->path, path) &&
596 ((port == NULL && f->port == NULL) ||
597 !strcmp(f->port, port))) {
604 yyerror("too much `fastcgi' rules defined.");
609 add_param(char *name, char *val, int env)
619 e = xcalloc(1, sizeof(*e));
623 TAILQ_INSERT_HEAD(h, e, envs);
625 TAILQ_INSERT_TAIL(h, e, envs);