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.
38 #define YYSTYPE yystype
50 static struct vhost *new_vhost(void);
51 static struct location *new_location(void);
53 void yyerror(const char*, ...);
54 static int yylex(void);
55 int parse_portno(const char*);
56 void parse_conf(const char*);
57 char *ensure_absolute_path(char*);
58 int check_block_code(int);
59 char *check_block_fmt(char*);
60 int check_strip_no(int);
61 int check_prefork_num(int);
62 void advance_loc(void);
63 void only_once(const void*, const char*);
64 void only_oncei(int, const char*);
65 int fastcgi_conf(char *, char *, char *);
66 void add_param(char *, char *, int);
71 /* %define parse.error verbose */
73 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
74 %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
75 %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
76 %token TFASTCGI TSPAWN TPARAM
86 conf : options vhosts ;
92 option : TCHROOT TSTRING { conf.chroot = $2; }
93 | TIPV6 TBOOL { conf.ipv6 = $2; }
94 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
95 | TPORT TNUM { conf.port = $2; }
96 | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
97 | TPROTOCOLS TSTRING {
98 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
99 yyerror("invalid protocols string \"%s\"", $2);
101 | TUSER TSTRING { conf.user = $2; }
108 vhost : TSERVER TSTRING {
110 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
112 loc = new_location();
113 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
115 loc->match = xstrdup("*");
118 if (strstr($2, "xn--") != NULL) {
119 warnx("%s:%d:%d \"%s\" looks like punycode: "
120 "you should use the decoded hostname.",
121 config_path, yylval.lineno+1, yylval.colno,
124 } '{' servopts locations '}' {
126 if (host->cert == NULL || host->key == NULL)
127 yyerror("invalid vhost definition: %s", $2);
129 | error '}' { yyerror("error in server directive"); }
132 servopts : /* empty */
136 servopt : TALIAS TSTRING {
139 a = xcalloc(1, sizeof(*a));
141 if (TAILQ_EMPTY(&host->aliases))
142 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
144 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
147 only_once(host->cert, "cert");
148 host->cert = ensure_absolute_path($2);
151 only_once(host->cgi, "cgi");
152 /* drop the starting '/', if any */
154 memmove($2, $2+1, strlen($2));
157 | TENTRYPOINT TSTRING {
158 only_once(host->entrypoint, "entrypoint");
160 memmove($2, $2+1, strlen($2));
161 host->entrypoint = $2;
163 | TENV TSTRING TSTRING {
164 add_param($2, $3, 1);
167 only_once(host->key, "key");
168 host->key = ensure_absolute_path($2);
170 | TPARAM TSTRING TSTRING {
171 add_param($2, $3, 0);
176 locations : /* empty */
180 location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
181 /* drop the starting '/' if any */
183 memmove($3, $3+1, strlen($3));
189 locopts : /* empty */
193 locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
194 | TBLOCK TRETURN TNUM TSTRING {
195 only_once(loc->block_fmt, "block");
196 loc->block_fmt = check_block_fmt($4);
197 loc->block_code = check_block_code($3);
199 | TBLOCK TRETURN TNUM {
200 only_once(loc->block_fmt, "block");
201 loc->block_fmt = xstrdup("temporary failure");
202 loc->block_code = check_block_code($3);
203 if ($3 >= 30 && $3 < 40)
204 yyerror("missing `meta' for block return %d", $3);
207 only_once(loc->block_fmt, "block");
208 loc->block_fmt = xstrdup("temporary failure");
209 loc->block_code = 40;
211 | TDEFAULT TTYPE TSTRING {
212 only_once(loc->default_mime, "default type");
213 loc->default_mime = $3;
217 only_once(loc->index, "index");
221 only_once(loc->lang, "lang");
224 | TLOG TBOOL { loc->disable_log = !$2; }
225 | TREQUIRE TCLIENT TCA TSTRING {
226 only_once(loc->reqca, "require client ca");
227 ensure_absolute_path($4);
228 if ((loc->reqca = load_ca($4)) == NULL)
229 yyerror("couldn't load ca cert: %s", $4);
233 only_once(loc->dir, "root");
234 loc->dir = ensure_absolute_path($2);
236 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
239 fastcgi : TSPAWN TSTRING {
240 only_oncei(loc->fcgi, "fastcgi");
241 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
244 only_oncei(loc->fcgi, "fastcgi");
245 loc->fcgi = fastcgi_conf($1, NULL, NULL);
247 | TTCP TSTRING TNUM {
249 if (asprintf(&c, "%d", $3) == -1)
251 only_oncei(loc->fcgi, "fastcgi");
252 loc->fcgi = fastcgi_conf($2, c, NULL);
255 only_oncei(loc->fcgi, "fastcgi");
256 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
258 | TTCP TSTRING TSTRING {
259 only_oncei(loc->fcgi, "fastcgi");
260 loc->fcgi = fastcgi_conf($2, $3, NULL);
266 static struct vhost *
269 return xcalloc(1, sizeof(struct vhost));
272 static struct location *
277 l = xcalloc(1, sizeof(*l));
284 yyerror(const char *msg, ...)
291 fprintf(stderr, "%s:%d: ", config_path, yylval.lineno);
292 vfprintf(stderr, msg, ap);
293 fprintf(stderr, "\n");
297 static struct keyword {
309 {"default", TDEFAULT},
310 {"entrypoint", TENTRYPOINT},
312 {"fastcgi", TFASTCGI},
317 {"location", TLOCATION},
322 {"prefork", TPREFORK},
323 {"protocols", TPROTOCOLS},
324 {"require", TREQUIRE},
336 * Taken an adapted from doas' parse.y
341 char buf[1024], *ebuf, *p, *str;
342 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
346 ebuf = buf + sizeof(buf);
349 /* skip whitespace first */
350 for (c = getc(yyfp); isspace(c); c = getc(yyfp)) {
358 /* check for special one-character constructions */
364 /* skip comments; NUL is allowed; no continuation */
365 while ((c = getc(yyfp)) != '\n')
375 /* parsing next word */
376 for (;; c = getc(yyfp), yylval.colno++) {
379 yyerror("unallowed character NULL in column %d",
390 yyerror("unterminated quotes in column %d",
401 yyerror("unterminated escape in column %d",
404 yyerror("unterminated quotes in column %d",
412 if (!escape && !quotes)
427 yyerror("line too long");
439 * There could be a number of reason for empty buffer,
440 * and we handle all of them here, to avoid cluttering
445 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
449 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); ++i) {
450 if (!strcmp(buf, keywords[i].word))
451 return keywords[i].token;
455 if (!nonkw && (c == '-' || isdigit(c))) {
456 yylval.num = parse_portno(buf);
459 if (!nonkw && !strcmp(buf, "on")) {
463 if (!nonkw && !strcmp(buf, "off")) {
467 if ((str = strdup(buf)) == NULL)
468 err(1, "%s", __func__);
474 yyerror("input error reading config");
479 parse_portno(const char *p)
484 n = strtonum(p, 0, UINT16_MAX, &errstr);
486 yyerror("port number is %s: %s", errstr, p);
491 parse_conf(const char *path)
494 if ((yyfp = fopen(path, "r")) == NULL)
495 err(1, "cannot open config: %s", path);
502 if (TAILQ_FIRST(&hosts)->domain == NULL)
503 errx(1, "no vhost defined in %s", path);
507 ensure_absolute_path(char *path)
509 if (path == NULL || *path != '/')
510 yyerror("not an absolute path: %s", path);
515 check_block_code(int n)
517 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
518 yyerror("invalid block code %d", n);
523 check_block_fmt(char *fmt)
527 for (s = fmt; *s; ++s) {
538 yyerror("invalid format specifier %%%c", *s);
546 check_strip_no(int n)
549 yyerror("invalid strip number %d", n);
554 check_prefork_num(int n)
556 if (n <= 0 || n >= PROC_MAX)
557 yyerror("invalid prefork number %d", n);
564 loc = new_location();
565 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
569 only_once(const void *ptr, const char *name)
572 yyerror("`%s' specified more than once", name);
576 only_oncei(int i, const char *name)
579 yyerror("`%s' specified more than once", name);
583 fastcgi_conf(char *path, char *port, char *prog)
588 for (i = 0; i < FCGI_MAX; ++i) {
591 if (f->path == NULL) {
599 /* XXX: what to do with prog? */
600 if (!strcmp(f->path, path) &&
601 ((port == NULL && f->port == NULL) ||
602 !strcmp(f->port, port))) {
609 yyerror("too much `fastcgi' rules defined.");
614 add_param(char *name, char *val, int env)
624 e = xcalloc(1, sizeof(*e));
628 TAILQ_INSERT_HEAD(h, e, envs);
630 TAILQ_INSERT_TAIL(h, e, envs);