1 /* -*- mode: fundamental; indent-tabs-mode: t; -*- */
5 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 static struct vhost *new_vhost(void);
38 static struct location *new_location(void);
40 void yyerror(const char*, ...);
41 int parse_portno(const char*);
42 void parse_conf(const char*);
43 char *ensure_absolute_path(char*);
44 int check_block_code(int);
45 char *check_block_fmt(char*);
46 int check_strip_no(int);
47 int check_prefork_num(int);
48 void advance_loc(void);
53 /* %define parse.error verbose */
60 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
61 %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
62 %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS
72 conf : options vhosts ;
78 option : TCHROOT TSTRING { conf.chroot = $2; }
79 | TIPV6 TBOOL { conf.ipv6 = $2; }
80 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
81 | TPORT TNUM { conf.port = $2; }
82 | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
83 | TPROTOCOLS TSTRING {
84 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
85 yyerror("invalid protocols string \"%s\"", $2);
87 | TUSER TSTRING { conf.user = $2; }
94 vhost : TSERVER TSTRING {
96 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
99 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
101 loc->match = xstrdup("*");
104 if (strstr($2, "xn--") != NULL) {
105 warnx("%s:%d \"%s\" looks like punycode: "
106 "you should use the decoded hostname.",
107 config_path, yylineno, $2);
109 } '{' servopts locations '}' {
111 if (host->cert == NULL || host->key == NULL ||
113 yyerror("invalid vhost definition: %s", $2);
115 | error '}' { yyerror("error in server directive"); }
118 servopts : /* empty */
122 servopt : TALIAS TSTRING {
125 a = xcalloc(1, sizeof(*a));
127 if (TAILQ_EMPTY(&host->aliases))
128 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
130 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
132 | TCERT TSTRING { host->cert = ensure_absolute_path($2); }
134 /* drop the starting '/', if any */
136 memmove($2, $2+1, strlen($2));
139 | TENTRYPOINT TSTRING {
140 if (host->entrypoint != NULL)
141 yyerror("`entrypoint' specified more than once");
143 memmove($2, $2+1, strlen($2));
144 host->entrypoint = $2;
146 | TENV TSTRING TSTRING {
149 e = xcalloc(1, sizeof(*e));
152 if (TAILQ_EMPTY(&host->env))
153 TAILQ_INSERT_HEAD(&host->env, e, envs);
155 TAILQ_INSERT_TAIL(&host->env, e, envs);
157 | TKEY TSTRING { host->key = ensure_absolute_path($2); }
158 | TROOT TSTRING { host->dir = ensure_absolute_path($2); }
162 locations : /* empty */
166 location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
167 /* drop the starting '/' if any */
169 memmove($3, $3+1, strlen($3));
175 locopts : /* empty */
179 locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
180 | TBLOCK TRETURN TNUM TSTRING {
181 if (loc->block_fmt != NULL)
182 yyerror("`block' rule specified more than once");
183 loc->block_fmt = check_block_fmt($4);
184 loc->block_code = check_block_code($3);
186 | TBLOCK TRETURN TNUM {
187 if (loc->block_fmt != NULL)
188 yyerror("`block' rule specified more than once");
189 loc->block_fmt = xstrdup("temporary failure");
190 loc->block_code = check_block_code($3);
191 if ($3 >= 30 && $3 < 40)
192 yyerror("missing `meta' for block return %d", $3);
195 if (loc->block_fmt != NULL)
196 yyerror("`block' rule specified more than once");
197 loc->block_fmt = xstrdup("temporary failure");
198 loc->block_code = 40;
200 | TDEFAULT TTYPE TSTRING {
201 if (loc->default_mime != NULL)
202 yyerror("`default type' specified more than once");
203 loc->default_mime = $3;
206 if (loc->index != NULL)
207 yyerror("`index' specified more than once");
211 if (loc->lang != NULL)
212 yyerror("`lang' specified more than once");
215 | TLOG TBOOL { loc->disable_log = !$2; }
216 | TREQUIRE TCLIENT TCA TSTRING {
217 if (loc->reqca != NULL)
218 yyerror("`require client ca' specified more than once");
220 ensure_absolute_path($4);
221 if ((loc->reqca = load_ca($4)) == NULL)
222 yyerror("couldn't load ca cert: %s", $4);
225 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
230 static struct vhost *
233 return xcalloc(1, sizeof(struct vhost));
236 static struct location *
239 return xcalloc(1, sizeof(struct location));
243 yyerror(const char *msg, ...)
250 fprintf(stderr, "%s:%d: ", config_path, yylineno);
251 vfprintf(stderr, msg, ap);
252 fprintf(stderr, "\n");
257 parse_portno(const char *p)
262 n = strtonum(p, 0, UINT16_MAX, &errstr);
264 yyerror("port number is %s: %s", errstr, p);
269 parse_conf(const char *path)
272 if ((yyin = fopen(path, "r")) == NULL)
273 err(1, "cannot open config: %s", path);
280 if (TAILQ_FIRST(&hosts)->domain == NULL)
281 errx(1, "no vhost defined in %s", path);
285 ensure_absolute_path(char *path)
287 if (path == NULL || *path != '/')
288 yyerror("not an absolute path: %s", path);
293 check_block_code(int n)
295 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
296 yyerror("invalid block code %d", n);
301 check_block_fmt(char *fmt)
305 for (s = fmt; *s; ++s) {
316 yyerror("invalid format specifier %%%c", *s);
324 check_strip_no(int n)
327 yyerror("invalid strip number %d", n);
332 check_prefork_num(int n)
334 if (n <= 0 || n >= PROC_MAX)
335 yyerror("invalid prefork number %d", n);
342 loc = new_location();
343 TAILQ_INSERT_TAIL(&host->locations, loc, locations);