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
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 '=' string {
214 add_param($2, $4, 1);
217 only_once(host->key, "key");
218 host->key = ensure_absolute_path($2);
220 | TPARAM string '=' 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')
437 /* parsing next word */
438 for (;; c = getc(yyfp), yylval.colno++) {
441 yyerror("unallowed character NULL in column %d",
451 /* expand macros in-place */
453 if (!escape && !quotes) {
456 if ((c = getc(yyfp)) == EOF) {
457 yyerror("EOF during macro expansion");
460 if (p + 1 >= ebuf - 1) {
461 yyerror("string too long");
464 if (isalnum(c) || c == '_') {
472 if ((val = symget(p)) == NULL) {
473 yyerror("macro '%s' not defined", v);
477 if (p + len >= ebuf - 1) {
478 yyerror("after macro-expansion, "
483 strlcat(p, val, ebuf - p);
491 yyerror("unterminated quotes in column %d",
502 yyerror("unterminated escape in column %d",
505 yyerror("unterminated quotes in column %d",
513 if (!escape && !quotes)
528 yyerror("line too long");
540 * There could be a number of reason for empty buffer,
541 * and we handle all of them here, to avoid cluttering
546 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
550 kw = bsearch(buf, keywords, sizeof(keywords)/sizeof(keywords[0]),
551 sizeof(keywords[0]), kw_cmp);
556 if (!nonkw && (c == '-' || isdigit(c))) {
557 yylval.v.num = parse_portno(buf);
560 if (!nonkw && !strcmp(buf, "on")) {
564 if (!nonkw && !strcmp(buf, "off")) {
568 if ((str = strdup(buf)) == NULL)
569 err(1, "%s", __func__);
575 yyerror("input error reading config");
580 parse_portno(const char *p)
585 n = strtonum(p, 0, UINT16_MAX, &errstr);
587 yyerror("port number is %s: %s", errstr, p);
592 parse_conf(const char *path)
594 struct sym *sym, *next;
597 if ((yyfp = fopen(path, "r")) == NULL)
598 err(1, "cannot open config: %s", path);
605 if (TAILQ_FIRST(&hosts)->domain == NULL)
606 errx(1, "no vhost defined in %s", path);
608 /* free unused macros */
609 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
610 /* TODO: warn if !sym->used */
614 TAILQ_REMOVE(&symhead, sym, entry);
621 ensure_absolute_path(char *path)
623 if (path == NULL || *path != '/')
624 yyerror("not an absolute path: %s", path);
629 check_block_code(int n)
631 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
632 yyerror("invalid block code %d", n);
637 check_block_fmt(char *fmt)
641 for (s = fmt; *s; ++s) {
652 yyerror("invalid format specifier %%%c", *s);
660 check_strip_no(int n)
663 yyerror("invalid strip number %d", n);
668 check_prefork_num(int n)
670 if (n <= 0 || n >= PROC_MAX)
671 yyerror("invalid prefork number %d", n);
678 loc = new_location();
679 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
683 only_once(const void *ptr, const char *name)
686 yyerror("`%s' specified more than once", name);
690 only_oncei(int i, const char *name)
693 yyerror("`%s' specified more than once", name);
697 fastcgi_conf(char *path, char *port, char *prog)
702 for (i = 0; i < FCGI_MAX; ++i) {
705 if (f->path == NULL) {
713 /* XXX: what to do with prog? */
714 if (!strcmp(f->path, path) &&
715 ((port == NULL && f->port == NULL) ||
716 !strcmp(f->port, port))) {
723 yyerror("too much `fastcgi' rules defined.");
728 add_param(char *name, char *val, int env)
738 e = xcalloc(1, sizeof(*e));
742 TAILQ_INSERT_HEAD(h, e, envs);
744 TAILQ_INSERT_TAIL(h, e, envs);
748 symset(const char *name, const char *val, int persist)
752 TAILQ_FOREACH(sym, &symhead, entry) {
753 if (!strcmp(name, sym->name))
763 TAILQ_REMOVE(&symhead, sym, entry);
768 sym = xcalloc(1, sizeof(*sym));
769 sym->name = xstrdup(name);
770 sym->val = xstrdup(val);
772 sym->persist = persist;
774 TAILQ_INSERT_TAIL(&symhead, sym, entry);
779 cmdline_symset(char *s)
784 if ((val = strrchr(s, '=')) == NULL)
786 sym = xcalloc(1, val - s + 1);
787 memcpy(sym, s, val - s);
788 ret = symset(sym, val + 1, 1);
794 symget(const char *name)
798 TAILQ_FOREACH(sym, &symhead, entry) {
799 if (!strcmp(name, sym->name)) {