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
46 * The idea behind this implementation of macros is from rad/parse.y
48 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
50 TAILQ_ENTRY(sym) entry;
62 static struct vhost *new_vhost(void);
63 static struct location *new_location(void);
65 void yyerror(const char*, ...);
66 int kw_cmp(const void *, const void *);
67 static int yylex(void);
68 int parse_portno(const char*);
69 void parse_conf(const char*);
70 char *ensure_absolute_path(char*);
71 int check_block_code(int);
72 char *check_block_fmt(char*);
73 int check_strip_no(int);
74 int check_prefork_num(int);
75 void advance_loc(void);
76 void only_once(const void*, const char*);
77 void only_oncei(int, const char*);
78 int fastcgi_conf(char *, char *, char *);
79 void add_param(char *, char *, int);
80 int symset(const char *, const char *, int);
81 char *symget(const char *);
86 /* %define parse.error verbose */
88 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
89 %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
90 %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
91 %token TFASTCGI TSPAWN TPARAM TMAP TTOEXT TARROW
95 %token <v.str> TSTRING
109 string : string TSTRING {
110 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
113 yyerror("string: asprintf: %s", strerror(errno));
122 var : TSTRING '=' string {
126 yyerror("macro name cannot contain "
139 option : TCHROOT string { conf.chroot = $2; }
140 | TIPV6 TBOOL { conf.ipv6 = $2; }
141 | TMIME TSTRING string {
142 fprintf(stderr, "%s:%d: `mime MIME EXT' is deprecated and "
143 "will be removed in a future version, "
144 "please use the new syntax: `map MIME to-ext EXT'",
145 config_path, yylval.lineno+1);
146 add_mime(&conf.mime, $2, $3);
148 | TMAP string TTOEXT string { add_mime(&conf.mime, $2, $4); }
149 | TPORT TNUM { conf.port = $2; }
150 | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
151 | TPROTOCOLS string {
152 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
153 yyerror("invalid protocols string \"%s\"", $2);
155 | TUSER string { conf.user = $2; }
158 vhost : TSERVER string {
160 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
162 loc = new_location();
163 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
165 loc->match = xstrdup("*");
168 if (strstr($2, "xn--") != NULL) {
169 warnx("%s:%d:%d \"%s\" looks like punycode: "
170 "you should use the decoded hostname.",
171 config_path, yylval.lineno+1, yylval.colno,
174 } '{' servopts locations '}' {
176 if (host->cert == NULL || host->key == NULL)
177 yyerror("invalid vhost definition: %s", $2);
179 | error '}' { yyerror("error in server directive"); }
182 servopts : /* empty */
186 servopt : TALIAS string {
189 a = xcalloc(1, sizeof(*a));
191 if (TAILQ_EMPTY(&host->aliases))
192 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
194 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
197 only_once(host->cert, "cert");
198 host->cert = ensure_absolute_path($2);
201 only_once(host->cgi, "cgi");
202 /* drop the starting '/', if any */
204 memmove($2, $2+1, strlen($2));
207 | TENTRYPOINT string {
208 only_once(host->entrypoint, "entrypoint");
210 memmove($2, $2+1, strlen($2));
211 host->entrypoint = $2;
213 | TENV string TARROW string {
214 add_param($2, $4, 1);
217 only_once(host->key, "key");
218 host->key = ensure_absolute_path($2);
220 | TPARAM string TARROW string {
221 add_param($2, $4, 0);
226 locations : /* empty */
230 location : TLOCATION { advance_loc(); } string '{' locopts '}' {
231 /* drop the starting '/' if any */
233 memmove($3, $3+1, strlen($3));
239 locopts : /* empty */
243 locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
244 | TBLOCK TRETURN TNUM string {
245 only_once(loc->block_fmt, "block");
246 loc->block_fmt = check_block_fmt($4);
247 loc->block_code = check_block_code($3);
249 | TBLOCK TRETURN TNUM {
250 only_once(loc->block_fmt, "block");
251 loc->block_fmt = xstrdup("temporary failure");
252 loc->block_code = check_block_code($3);
253 if ($3 >= 30 && $3 < 40)
254 yyerror("missing `meta' for block return %d", $3);
257 only_once(loc->block_fmt, "block");
258 loc->block_fmt = xstrdup("temporary failure");
259 loc->block_code = 40;
261 | TDEFAULT TTYPE string {
262 only_once(loc->default_mime, "default type");
263 loc->default_mime = $3;
267 only_once(loc->index, "index");
271 only_once(loc->lang, "lang");
274 | TLOG TBOOL { loc->disable_log = !$2; }
275 | TREQUIRE TCLIENT TCA string {
276 only_once(loc->reqca, "require client ca");
277 ensure_absolute_path($4);
278 if ((loc->reqca = load_ca($4)) == NULL)
279 yyerror("couldn't load ca cert: %s", $4);
283 only_once(loc->dir, "root");
284 loc->dir = ensure_absolute_path($2);
286 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
289 fastcgi : TSPAWN string {
290 only_oncei(loc->fcgi, "fastcgi");
291 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
294 only_oncei(loc->fcgi, "fastcgi");
295 loc->fcgi = fastcgi_conf($1, NULL, NULL);
297 | TTCP string TPORT TNUM {
299 if (asprintf(&c, "%d", $4) == -1)
301 only_oncei(loc->fcgi, "fastcgi");
302 loc->fcgi = fastcgi_conf($2, c, NULL);
305 only_oncei(loc->fcgi, "fastcgi");
306 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
308 | TTCP string TPORT string {
309 only_oncei(loc->fcgi, "fastcgi");
310 loc->fcgi = fastcgi_conf($2, $4, NULL);
316 static struct vhost *
319 return xcalloc(1, sizeof(struct vhost));
322 static struct location *
327 l = xcalloc(1, sizeof(*l));
334 yyerror(const char *msg, ...)
341 fprintf(stderr, "%s:%d: ", config_path, yylval.lineno);
342 vfprintf(stderr, msg, ap);
343 fprintf(stderr, "\n");
347 static struct keyword {
351 /* these MUST be sorted */
360 {"default", TDEFAULT},
361 {"entrypoint", TENTRYPOINT},
363 {"fastcgi", TFASTCGI},
368 {"location", TLOCATION},
374 {"prefork", TPREFORK},
375 {"protocols", TPROTOCOLS},
376 {"require", TREQUIRE},
389 kw_cmp(const void *k, const void *e)
391 return strcmp(k, ((struct keyword *)e)->word);
395 * Taken an adapted from doas' parse.y
401 char buf[8096], *ebuf, *p, *str, *v, *val;
402 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
406 ebuf = buf + sizeof(buf);
409 /* skip whitespace first */
410 for (c = getc(yyfp); isspace(c); c = getc(yyfp)) {
418 /* check for special one-character constructions */
424 /* skip comments; NUL is allowed; no continuation */
425 while ((c = getc(yyfp)) != '\n')
432 if ((c = getc(yyfp)) == '>')
440 /* parsing next word */
441 for (;; c = getc(yyfp), yylval.colno++) {
444 yyerror("unallowed character NULL in column %d",
454 /* expand macros in-place */
456 if (!escape && !quotes) {
459 if ((c = getc(yyfp)) == EOF) {
460 yyerror("EOF during macro expansion");
463 if (p + 1 >= ebuf - 1) {
464 yyerror("string too long");
467 if (isalnum(c) || c == '_') {
475 if ((val = symget(p)) == NULL) {
476 yyerror("macro '%s' not defined", v);
480 if (p + len >= ebuf - 1) {
481 yyerror("after macro-expansion, "
486 strlcat(p, val, ebuf - p);
494 yyerror("unterminated quotes in column %d",
505 yyerror("unterminated escape in column %d",
508 yyerror("unterminated quotes in column %d",
516 if (!escape && !quotes)
531 yyerror("line too long");
543 * There could be a number of reason for empty buffer,
544 * and we handle all of them here, to avoid cluttering
549 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
553 kw = bsearch(buf, keywords, sizeof(keywords)/sizeof(keywords[0]),
554 sizeof(keywords[0]), kw_cmp);
559 if (!nonkw && (c == '-' || isdigit(c))) {
560 yylval.v.num = parse_portno(buf);
563 if (!nonkw && !strcmp(buf, "on")) {
567 if (!nonkw && !strcmp(buf, "off")) {
571 if ((str = strdup(buf)) == NULL)
572 err(1, "%s", __func__);
578 yyerror("input error reading config");
583 parse_portno(const char *p)
588 n = strtonum(p, 0, UINT16_MAX, &errstr);
590 yyerror("port number is %s: %s", errstr, p);
595 parse_conf(const char *path)
597 struct sym *sym, *next;
600 if ((yyfp = fopen(path, "r")) == NULL)
601 err(1, "cannot open config: %s", path);
608 if (TAILQ_FIRST(&hosts)->domain == NULL)
609 errx(1, "no vhost defined in %s", path);
611 /* free unused macros */
612 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
613 /* TODO: warn if !sym->used */
617 TAILQ_REMOVE(&symhead, sym, entry);
624 ensure_absolute_path(char *path)
626 if (path == NULL || *path != '/')
627 yyerror("not an absolute path: %s", path);
632 check_block_code(int n)
634 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
635 yyerror("invalid block code %d", n);
640 check_block_fmt(char *fmt)
644 for (s = fmt; *s; ++s) {
655 yyerror("invalid format specifier %%%c", *s);
663 check_strip_no(int n)
666 yyerror("invalid strip number %d", n);
671 check_prefork_num(int n)
673 if (n <= 0 || n >= PROC_MAX)
674 yyerror("invalid prefork number %d", n);
681 loc = new_location();
682 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
686 only_once(const void *ptr, const char *name)
689 yyerror("`%s' specified more than once", name);
693 only_oncei(int i, const char *name)
696 yyerror("`%s' specified more than once", name);
700 fastcgi_conf(char *path, char *port, char *prog)
705 for (i = 0; i < FCGI_MAX; ++i) {
708 if (f->path == NULL) {
716 /* XXX: what to do with prog? */
717 if (!strcmp(f->path, path) &&
718 ((port == NULL && f->port == NULL) ||
719 !strcmp(f->port, port))) {
726 yyerror("too much `fastcgi' rules defined.");
731 add_param(char *name, char *val, int env)
741 e = xcalloc(1, sizeof(*e));
745 TAILQ_INSERT_HEAD(h, e, envs);
747 TAILQ_INSERT_TAIL(h, e, envs);
751 symset(const char *name, const char *val, int persist)
755 TAILQ_FOREACH(sym, &symhead, entry) {
756 if (!strcmp(name, sym->name))
766 TAILQ_REMOVE(&symhead, sym, entry);
771 sym = xcalloc(1, sizeof(*sym));
772 sym->name = xstrdup(name);
773 sym->val = xstrdup(val);
775 sym->persist = persist;
777 TAILQ_INSERT_TAIL(&symhead, sym, entry);
782 cmdline_symset(char *s)
787 if ((val = strrchr(s, '=')) == NULL)
789 sym = xcalloc(1, val - s + 1);
790 memcpy(sym, s, val - s);
791 ret = symset(sym, val + 1, 1);
797 symget(const char *name)
801 TAILQ_FOREACH(sym, &symhead, entry) {
802 if (!strcmp(name, sym->name)) {