Blob


1 /* -*- mode: fundamental; indent-tabs-mode: t; -*- */
2 %{
4 /*
5 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
6 *
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.
10 *
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.
18 */
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include "gmid.h"
27 /*
28 * #define YYDEBUG 1
29 * int yydebug = 1;
30 */
32 struct vhost *host;
33 struct location *loc;
35 int goterror = 0;
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);
49 void only_once(const void*, const char*);
51 %}
53 /* for bison: */
54 /* %define parse.error verbose */
56 %union {
57 char *str;
58 int num;
59 }
61 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
62 %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
63 %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS
65 %token TERR
67 %token <str> TSTRING
68 %token <num> TNUM
69 %token <num> TBOOL
71 %%
73 conf : options vhosts ;
75 options : /* empty */
76 | options option
77 ;
79 option : TCHROOT TSTRING { conf.chroot = $2; }
80 | TIPV6 TBOOL { conf.ipv6 = $2; }
81 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
82 | TPORT TNUM { conf.port = $2; }
83 | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
84 | TPROTOCOLS TSTRING {
85 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
86 yyerror("invalid protocols string \"%s\"", $2);
87 }
88 | TUSER TSTRING { conf.user = $2; }
89 ;
91 vhosts : /* empty */
92 | vhosts vhost
93 ;
95 vhost : TSERVER TSTRING {
96 host = new_vhost();
97 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
99 loc = new_location();
100 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
102 loc->match = xstrdup("*");
103 host->domain = $2;
105 if (strstr($2, "xn--") != NULL) {
106 warnx("%s:%d \"%s\" looks like punycode: "
107 "you should use the decoded hostname.",
108 config_path, yylineno, $2);
110 } '{' servopts locations '}' {
112 if (host->cert == NULL || host->key == NULL)
113 yyerror("invalid vhost definition: %s", $2);
115 | error '}' { yyerror("error in server directive"); }
118 servopts : /* empty */
119 | servopts servopt
122 servopt : TALIAS TSTRING {
123 struct alist *a;
125 a = xcalloc(1, sizeof(*a));
126 a->alias = $2;
127 if (TAILQ_EMPTY(&host->aliases))
128 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
129 else
130 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
132 | TCERT TSTRING {
133 only_once(host->cert, "cert");
134 host->cert = ensure_absolute_path($2);
136 | TCGI TSTRING {
137 only_once(host->cgi, "cgi");
138 /* drop the starting '/', if any */
139 if (*$2 == '/')
140 memmove($2, $2+1, strlen($2));
141 host->cgi = $2;
143 | TENTRYPOINT TSTRING {
144 only_once(host->entrypoint, "entrypoint");
145 while (*$2 == '/')
146 memmove($2, $2+1, strlen($2));
147 host->entrypoint = $2;
149 | TENV TSTRING TSTRING {
150 struct envlist *e;
152 e = xcalloc(1, sizeof(*e));
153 e->name = $2;
154 e->value = $3;
155 if (TAILQ_EMPTY(&host->env))
156 TAILQ_INSERT_HEAD(&host->env, e, envs);
157 else
158 TAILQ_INSERT_TAIL(&host->env, e, envs);
160 | TKEY TSTRING {
161 only_once(host->key, "key");
162 host->key = ensure_absolute_path($2);
164 | locopt
167 locations : /* empty */
168 | locations location
171 location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
172 /* drop the starting '/' if any */
173 if (*$3 == '/')
174 memmove($3, $3+1, strlen($3));
175 loc->match = $3;
177 | error '}'
180 locopts : /* empty */
181 | locopts locopt
184 locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
185 | TBLOCK TRETURN TNUM TSTRING {
186 only_once(loc->block_fmt, "block");
187 loc->block_fmt = check_block_fmt($4);
188 loc->block_code = check_block_code($3);
190 | TBLOCK TRETURN TNUM {
191 only_once(loc->block_fmt, "block");
192 loc->block_fmt = xstrdup("temporary failure");
193 loc->block_code = check_block_code($3);
194 if ($3 >= 30 && $3 < 40)
195 yyerror("missing `meta' for block return %d", $3);
197 | TBLOCK {
198 only_once(loc->block_fmt, "block");
199 loc->block_fmt = xstrdup("temporary failure");
200 loc->block_code = 40;
202 | TDEFAULT TTYPE TSTRING {
203 only_once(loc->default_mime, "default type");
204 loc->default_mime = $3;
206 | TINDEX TSTRING {
207 only_once(loc->index, "index");
208 loc->index = $2;
210 | TLANG TSTRING {
211 only_once(loc->lang, "lang");
212 loc->lang = $2;
214 | TLOG TBOOL { loc->disable_log = !$2; }
215 | TREQUIRE TCLIENT TCA TSTRING {
216 only_once(loc->reqca, "require client ca");
217 ensure_absolute_path($4);
218 if ((loc->reqca = load_ca($4)) == NULL)
219 yyerror("couldn't load ca cert: %s", $4);
220 free($4);
222 | TROOT TSTRING {
223 only_once(loc->dir, "root");
224 loc->dir = ensure_absolute_path($2);
226 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
229 %%
231 static struct vhost *
232 new_vhost(void)
234 return xcalloc(1, sizeof(struct vhost));
237 static struct location *
238 new_location(void)
240 struct location *l;
242 l = xcalloc(1, sizeof(*l));
243 l->dirfd = -1;
244 return l;
247 void
248 yyerror(const char *msg, ...)
250 va_list ap;
252 goterror = 1;
254 va_start(ap, msg);
255 fprintf(stderr, "%s:%d: ", config_path, yylineno);
256 vfprintf(stderr, msg, ap);
257 fprintf(stderr, "\n");
258 va_end(ap);
261 int
262 parse_portno(const char *p)
264 const char *errstr;
265 int n;
267 n = strtonum(p, 0, UINT16_MAX, &errstr);
268 if (errstr != NULL)
269 yyerror("port number is %s: %s", errstr, p);
270 return n;
273 void
274 parse_conf(const char *path)
276 config_path = path;
277 if ((yyin = fopen(path, "r")) == NULL)
278 err(1, "cannot open config: %s", path);
279 yyparse();
280 fclose(yyin);
282 if (goterror)
283 exit(1);
285 if (TAILQ_FIRST(&hosts)->domain == NULL)
286 errx(1, "no vhost defined in %s", path);
289 char *
290 ensure_absolute_path(char *path)
292 if (path == NULL || *path != '/')
293 yyerror("not an absolute path: %s", path);
294 return path;
297 int
298 check_block_code(int n)
300 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
301 yyerror("invalid block code %d", n);
302 return n;
305 char *
306 check_block_fmt(char *fmt)
308 char *s;
310 for (s = fmt; *s; ++s) {
311 if (*s != '%')
312 continue;
313 switch (*++s) {
314 case '%':
315 case 'p':
316 case 'q':
317 case 'P':
318 case 'N':
319 break;
320 default:
321 yyerror("invalid format specifier %%%c", *s);
325 return fmt;
328 int
329 check_strip_no(int n)
331 if (n <= 0)
332 yyerror("invalid strip number %d", n);
333 return n;
336 int
337 check_prefork_num(int n)
339 if (n <= 0 || n >= PROC_MAX)
340 yyerror("invalid prefork number %d", n);
341 return n;
344 void
345 advance_loc(void)
347 loc = new_location();
348 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
351 void
352 only_once(const void *ptr, const char *name)
354 if (ptr != NULL)
355 yyerror("`%s' specified more than once", name);