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);
50 %}
52 /* for bison: */
53 /* %define parse.error verbose */
55 %union {
56 char *str;
57 int num;
58 }
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
64 %token TERR
66 %token <str> TSTRING
67 %token <num> TNUM
68 %token <num> TBOOL
70 %%
72 conf : options vhosts ;
74 options : /* empty */
75 | options option
76 ;
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);
86 }
87 | TUSER TSTRING { conf.user = $2; }
88 ;
90 vhosts : /* empty */
91 | vhosts vhost
92 ;
94 vhost : TSERVER TSTRING {
95 host = new_vhost();
96 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
98 loc = new_location();
99 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
101 loc->match = xstrdup("*");
102 host->domain = $2;
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)
112 yyerror("invalid vhost definition: %s", $2);
114 | error '}' { yyerror("error in server directive"); }
117 servopts : /* empty */
118 | servopts servopt
121 servopt : TALIAS TSTRING {
122 struct alist *a;
124 a = xcalloc(1, sizeof(*a));
125 a->alias = $2;
126 if (TAILQ_EMPTY(&host->aliases))
127 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
128 else
129 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
131 | TCERT TSTRING { host->cert = ensure_absolute_path($2); }
132 | TCGI TSTRING {
133 /* drop the starting '/', if any */
134 if (*$2 == '/')
135 memmove($2, $2+1, strlen($2));
136 host->cgi = $2;
138 | TENTRYPOINT TSTRING {
139 if (host->entrypoint != NULL)
140 yyerror("`entrypoint' specified more than once");
141 while (*$2 == '/')
142 memmove($2, $2+1, strlen($2));
143 host->entrypoint = $2;
145 | TENV TSTRING TSTRING {
146 struct envlist *e;
148 e = xcalloc(1, sizeof(*e));
149 e->name = $2;
150 e->value = $3;
151 if (TAILQ_EMPTY(&host->env))
152 TAILQ_INSERT_HEAD(&host->env, e, envs);
153 else
154 TAILQ_INSERT_TAIL(&host->env, e, envs);
156 | TKEY TSTRING { host->key = ensure_absolute_path($2); }
157 | locopt
160 locations : /* empty */
161 | locations location
164 location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
165 /* drop the starting '/' if any */
166 if (*$3 == '/')
167 memmove($3, $3+1, strlen($3));
168 loc->match = $3;
170 | error '}'
173 locopts : /* empty */
174 | locopts locopt
177 locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
178 | TBLOCK TRETURN TNUM TSTRING {
179 if (loc->block_fmt != NULL)
180 yyerror("`block' rule specified more than once");
181 loc->block_fmt = check_block_fmt($4);
182 loc->block_code = check_block_code($3);
184 | TBLOCK TRETURN TNUM {
185 if (loc->block_fmt != NULL)
186 yyerror("`block' rule specified more than once");
187 loc->block_fmt = xstrdup("temporary failure");
188 loc->block_code = check_block_code($3);
189 if ($3 >= 30 && $3 < 40)
190 yyerror("missing `meta' for block return %d", $3);
192 | TBLOCK {
193 if (loc->block_fmt != NULL)
194 yyerror("`block' rule specified more than once");
195 loc->block_fmt = xstrdup("temporary failure");
196 loc->block_code = 40;
198 | TDEFAULT TTYPE TSTRING {
199 if (loc->default_mime != NULL)
200 yyerror("`default type' specified more than once");
201 loc->default_mime = $3;
203 | TINDEX TSTRING {
204 if (loc->index != NULL)
205 yyerror("`index' specified more than once");
206 loc->index = $2;
208 | TLANG TSTRING {
209 if (loc->lang != NULL)
210 yyerror("`lang' specified more than once");
211 loc->lang = $2;
213 | TLOG TBOOL { loc->disable_log = !$2; }
214 | TREQUIRE TCLIENT TCA TSTRING {
215 if (loc->reqca != NULL)
216 yyerror("`require client ca' specified more than once");
218 ensure_absolute_path($4);
219 if ((loc->reqca = load_ca($4)) == NULL)
220 yyerror("couldn't load ca cert: %s", $4);
221 free($4);
223 | TROOT TSTRING {
224 if (loc->dir != NULL)
225 yyerror("`root' specified more than once");
227 loc->dir = ensure_absolute_path($2);
229 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
232 %%
234 static struct vhost *
235 new_vhost(void)
237 return xcalloc(1, sizeof(struct vhost));
240 static struct location *
241 new_location(void)
243 struct location *l;
245 l = xcalloc(1, sizeof(*l));
246 l->dirfd = -1;
247 return l;
250 void
251 yyerror(const char *msg, ...)
253 va_list ap;
255 goterror = 1;
257 va_start(ap, msg);
258 fprintf(stderr, "%s:%d: ", config_path, yylineno);
259 vfprintf(stderr, msg, ap);
260 fprintf(stderr, "\n");
261 va_end(ap);
264 int
265 parse_portno(const char *p)
267 const char *errstr;
268 int n;
270 n = strtonum(p, 0, UINT16_MAX, &errstr);
271 if (errstr != NULL)
272 yyerror("port number is %s: %s", errstr, p);
273 return n;
276 void
277 parse_conf(const char *path)
279 config_path = path;
280 if ((yyin = fopen(path, "r")) == NULL)
281 err(1, "cannot open config: %s", path);
282 yyparse();
283 fclose(yyin);
285 if (goterror)
286 exit(1);
288 if (TAILQ_FIRST(&hosts)->domain == NULL)
289 errx(1, "no vhost defined in %s", path);
292 char *
293 ensure_absolute_path(char *path)
295 if (path == NULL || *path != '/')
296 yyerror("not an absolute path: %s", path);
297 return path;
300 int
301 check_block_code(int n)
303 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
304 yyerror("invalid block code %d", n);
305 return n;
308 char *
309 check_block_fmt(char *fmt)
311 char *s;
313 for (s = fmt; *s; ++s) {
314 if (*s != '%')
315 continue;
316 switch (*++s) {
317 case '%':
318 case 'p':
319 case 'q':
320 case 'P':
321 case 'N':
322 break;
323 default:
324 yyerror("invalid format specifier %%%c", *s);
328 return fmt;
331 int
332 check_strip_no(int n)
334 if (n <= 0)
335 yyerror("invalid strip number %d", n);
336 return n;
339 int
340 check_prefork_num(int n)
342 if (n <= 0 || n >= PROC_MAX)
343 yyerror("invalid prefork number %d", n);
344 return n;
347 void
348 advance_loc(void)
350 loc = new_location();
351 TAILQ_INSERT_TAIL(&host->locations, loc, locations);