Blame


1 15902770 2021-01-15 op /* -*- mode: fundamental; indent-tabs-mode: t; -*- */
2 15902770 2021-01-15 op %{
3 15902770 2021-01-15 op
4 15902770 2021-01-15 op /*
5 15902770 2021-01-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
6 15902770 2021-01-15 op *
7 15902770 2021-01-15 op * Permission to use, copy, modify, and distribute this software for any
8 15902770 2021-01-15 op * purpose with or without fee is hereby granted, provided that the above
9 15902770 2021-01-15 op * copyright notice and this permission notice appear in all copies.
10 15902770 2021-01-15 op *
11 15902770 2021-01-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 15902770 2021-01-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 15902770 2021-01-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 15902770 2021-01-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 15902770 2021-01-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 15902770 2021-01-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 15902770 2021-01-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 15902770 2021-01-15 op */
19 15902770 2021-01-15 op
20 002a84a1 2021-02-10 op #include <errno.h>
21 6abda252 2021-02-06 op #include <stdarg.h>
22 15902770 2021-01-15 op #include <stdio.h>
23 32693ee6 2021-01-28 op #include <string.h>
24 15902770 2021-01-15 op
25 15902770 2021-01-15 op #include "gmid.h"
26 15902770 2021-01-15 op
27 15902770 2021-01-15 op /*
28 15902770 2021-01-15 op * #define YYDEBUG 1
29 15902770 2021-01-15 op * int yydebug = 1;
30 15902770 2021-01-15 op */
31 15902770 2021-01-15 op
32 ca21e100 2021-02-04 op struct vhost *host;
33 ca21e100 2021-02-04 op struct location *loc;
34 15902770 2021-01-15 op
35 13ed2fb6 2021-01-27 op int goterror = 0;
36 15902770 2021-01-15 op
37 b8e64ccd 2021-03-31 op static struct vhost *new_vhost(void);
38 b8e64ccd 2021-03-31 op static struct location *new_location(void);
39 b8e64ccd 2021-03-31 op
40 6abda252 2021-02-06 op void yyerror(const char*, ...);
41 13ed2fb6 2021-01-27 op int parse_portno(const char*);
42 13ed2fb6 2021-01-27 op void parse_conf(const char*);
43 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
44 6abda252 2021-02-06 op int check_block_code(int);
45 6abda252 2021-02-06 op char *check_block_fmt(char*);
46 6abda252 2021-02-06 op int check_strip_no(int);
47 a709ddf5 2021-02-07 op int check_prefork_num(int);
48 49b73ba1 2021-02-10 op void advance_loc(void);
49 c705ecb1 2021-05-03 op void only_once(const void*, const char*);
50 13ed2fb6 2021-01-27 op
51 15902770 2021-01-15 op %}
52 15902770 2021-01-15 op
53 15902770 2021-01-15 op /* for bison: */
54 15902770 2021-01-15 op /* %define parse.error verbose */
55 15902770 2021-01-15 op
56 15902770 2021-01-15 op %union {
57 15902770 2021-01-15 op char *str;
58 15902770 2021-01-15 op int num;
59 15902770 2021-01-15 op }
60 15902770 2021-01-15 op
61 d06d6f4b 2021-04-29 op %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
62 d06d6f4b 2021-04-29 op %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
63 cc8c2901 2021-04-29 op %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS
64 d06d6f4b 2021-04-29 op
65 15902770 2021-01-15 op %token TERR
66 15902770 2021-01-15 op
67 15902770 2021-01-15 op %token <str> TSTRING
68 15902770 2021-01-15 op %token <num> TNUM
69 15902770 2021-01-15 op %token <num> TBOOL
70 15902770 2021-01-15 op
71 15902770 2021-01-15 op %%
72 15902770 2021-01-15 op
73 15902770 2021-01-15 op conf : options vhosts ;
74 15902770 2021-01-15 op
75 15902770 2021-01-15 op options : /* empty */
76 15902770 2021-01-15 op | options option
77 15902770 2021-01-15 op ;
78 15902770 2021-01-15 op
79 eb59f87e 2021-02-09 op option : TCHROOT TSTRING { conf.chroot = $2; }
80 eb59f87e 2021-02-09 op | TIPV6 TBOOL { conf.ipv6 = $2; }
81 eb59f87e 2021-02-09 op | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
82 15902770 2021-01-15 op | TPORT TNUM { conf.port = $2; }
83 eb59f87e 2021-02-09 op | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
84 5bc3c98e 2021-01-15 op | TPROTOCOLS TSTRING {
85 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
86 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
87 5bc3c98e 2021-01-15 op }
88 ae08ec7d 2021-01-25 op | TUSER TSTRING { conf.user = $2; }
89 15902770 2021-01-15 op ;
90 15902770 2021-01-15 op
91 15902770 2021-01-15 op vhosts : /* empty */
92 15902770 2021-01-15 op | vhosts vhost
93 15902770 2021-01-15 op ;
94 15902770 2021-01-15 op
95 b8e64ccd 2021-03-31 op vhost : TSERVER TSTRING {
96 b8e64ccd 2021-03-31 op host = new_vhost();
97 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
98 b8e64ccd 2021-03-31 op
99 b8e64ccd 2021-03-31 op loc = new_location();
100 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
101 b8e64ccd 2021-03-31 op
102 b8e64ccd 2021-03-31 op loc->match = xstrdup("*");
103 15902770 2021-01-15 op host->domain = $2;
104 15902770 2021-01-15 op
105 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
106 cbeee4ca 2021-01-28 op warnx("%s:%d \"%s\" looks like punycode: "
107 415ac7a2 2021-01-28 op "you should use the decoded hostname.",
108 415ac7a2 2021-01-28 op config_path, yylineno, $2);
109 cbeee4ca 2021-01-28 op }
110 b8e64ccd 2021-03-31 op } '{' servopts locations '}' {
111 cbeee4ca 2021-01-28 op
112 fdea6aa0 2021-04-30 op if (host->cert == NULL || host->key == NULL)
113 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
114 15902770 2021-01-15 op }
115 15902770 2021-01-15 op | error '}' { yyerror("error in server directive"); }
116 15902770 2021-01-15 op ;
117 15902770 2021-01-15 op
118 15902770 2021-01-15 op servopts : /* empty */
119 15902770 2021-01-15 op | servopts servopt
120 15902770 2021-01-15 op ;
121 15902770 2021-01-15 op
122 cc8c2901 2021-04-29 op servopt : TALIAS TSTRING {
123 cc8c2901 2021-04-29 op struct alist *a;
124 cc8c2901 2021-04-29 op
125 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
126 cc8c2901 2021-04-29 op a->alias = $2;
127 cc8c2901 2021-04-29 op if (TAILQ_EMPTY(&host->aliases))
128 cc8c2901 2021-04-29 op TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
129 cc8c2901 2021-04-29 op else
130 cc8c2901 2021-04-29 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
131 cc8c2901 2021-04-29 op }
132 c705ecb1 2021-05-03 op | TCERT TSTRING {
133 c705ecb1 2021-05-03 op only_once(host->cert, "cert");
134 c705ecb1 2021-05-03 op host->cert = ensure_absolute_path($2);
135 c705ecb1 2021-05-03 op }
136 15902770 2021-01-15 op | TCGI TSTRING {
137 c705ecb1 2021-05-03 op only_once(host->cgi, "cgi");
138 15902770 2021-01-15 op /* drop the starting '/', if any */
139 709f4c94 2021-02-04 op if (*$2 == '/')
140 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
141 709f4c94 2021-02-04 op host->cgi = $2;
142 05c23a54 2021-01-19 op }
143 e3ddf390 2021-02-06 op | TENTRYPOINT TSTRING {
144 c705ecb1 2021-05-03 op only_once(host->entrypoint, "entrypoint");
145 e3ddf390 2021-02-06 op while (*$2 == '/')
146 e3ddf390 2021-02-06 op memmove($2, $2+1, strlen($2));
147 e3ddf390 2021-02-06 op host->entrypoint = $2;
148 e3ddf390 2021-02-06 op }
149 9cc630aa 2021-04-28 op | TENV TSTRING TSTRING {
150 9cc630aa 2021-04-28 op struct envlist *e;
151 9cc630aa 2021-04-28 op
152 9cc630aa 2021-04-28 op e = xcalloc(1, sizeof(*e));
153 9cc630aa 2021-04-28 op e->name = $2;
154 9cc630aa 2021-04-28 op e->value = $3;
155 9cc630aa 2021-04-28 op if (TAILQ_EMPTY(&host->env))
156 9cc630aa 2021-04-28 op TAILQ_INSERT_HEAD(&host->env, e, envs);
157 9cc630aa 2021-04-28 op else
158 9cc630aa 2021-04-28 op TAILQ_INSERT_TAIL(&host->env, e, envs);
159 9cc630aa 2021-04-28 op }
160 c705ecb1 2021-05-03 op | TKEY TSTRING {
161 c705ecb1 2021-05-03 op only_once(host->key, "key");
162 c705ecb1 2021-05-03 op host->key = ensure_absolute_path($2);
163 c705ecb1 2021-05-03 op }
164 c8b74339 2021-01-24 op | locopt
165 c8b74339 2021-01-24 op ;
166 c8b74339 2021-01-24 op
167 c8b74339 2021-01-24 op locations : /* empty */
168 c8b74339 2021-01-24 op | locations location
169 c8b74339 2021-01-24 op ;
170 c8b74339 2021-01-24 op
171 49b73ba1 2021-02-10 op location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
172 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
173 49b73ba1 2021-02-10 op if (*$3 == '/')
174 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
175 49b73ba1 2021-02-10 op loc->match = $3;
176 6119e13e 2021-01-19 op }
177 c8b74339 2021-01-24 op | error '}'
178 c8b74339 2021-01-24 op ;
179 c8b74339 2021-01-24 op
180 c8b74339 2021-01-24 op locopts : /* empty */
181 c8b74339 2021-01-24 op | locopts locopt
182 c8b74339 2021-01-24 op ;
183 c8b74339 2021-01-24 op
184 eb59f87e 2021-02-09 op locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
185 6abda252 2021-02-06 op | TBLOCK TRETURN TNUM TSTRING {
186 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
187 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
188 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
189 6abda252 2021-02-06 op }
190 6abda252 2021-02-06 op | TBLOCK TRETURN TNUM {
191 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
192 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
193 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
194 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
195 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
196 6abda252 2021-02-06 op }
197 6abda252 2021-02-06 op | TBLOCK {
198 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
199 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
200 6abda252 2021-02-06 op loc->block_code = 40;
201 6abda252 2021-02-06 op }
202 eb59f87e 2021-02-09 op | TDEFAULT TTYPE TSTRING {
203 c705ecb1 2021-05-03 op only_once(loc->default_mime, "default type");
204 eb59f87e 2021-02-09 op loc->default_mime = $3;
205 eb59f87e 2021-02-09 op }
206 eb59f87e 2021-02-09 op | TINDEX TSTRING {
207 c705ecb1 2021-05-03 op only_once(loc->index, "index");
208 eb59f87e 2021-02-09 op loc->index = $2;
209 eb59f87e 2021-02-09 op }
210 eb59f87e 2021-02-09 op | TLANG TSTRING {
211 c705ecb1 2021-05-03 op only_once(loc->lang, "lang");
212 eb59f87e 2021-02-09 op loc->lang = $2;
213 eb59f87e 2021-02-09 op }
214 793835cb 2021-02-23 op | TLOG TBOOL { loc->disable_log = !$2; }
215 02be96c6 2021-02-09 op | TREQUIRE TCLIENT TCA TSTRING {
216 c705ecb1 2021-05-03 op only_once(loc->reqca, "require client ca");
217 adbe6a64 2021-04-30 op ensure_absolute_path($4);
218 02be96c6 2021-02-09 op if ((loc->reqca = load_ca($4)) == NULL)
219 02be96c6 2021-02-09 op yyerror("couldn't load ca cert: %s", $4);
220 02be96c6 2021-02-09 op free($4);
221 fdea6aa0 2021-04-30 op }
222 fdea6aa0 2021-04-30 op | TROOT TSTRING {
223 c705ecb1 2021-05-03 op only_once(loc->dir, "root");
224 fdea6aa0 2021-04-30 op loc->dir = ensure_absolute_path($2);
225 02be96c6 2021-02-09 op }
226 eb59f87e 2021-02-09 op | TSTRIP TNUM { loc->strip = check_strip_no($2); }
227 15902770 2021-01-15 op ;
228 13ed2fb6 2021-01-27 op
229 13ed2fb6 2021-01-27 op %%
230 b8e64ccd 2021-03-31 op
231 b8e64ccd 2021-03-31 op static struct vhost *
232 b8e64ccd 2021-03-31 op new_vhost(void)
233 b8e64ccd 2021-03-31 op {
234 b8e64ccd 2021-03-31 op return xcalloc(1, sizeof(struct vhost));
235 b8e64ccd 2021-03-31 op }
236 13ed2fb6 2021-01-27 op
237 b8e64ccd 2021-03-31 op static struct location *
238 b8e64ccd 2021-03-31 op new_location(void)
239 b8e64ccd 2021-03-31 op {
240 fdea6aa0 2021-04-30 op struct location *l;
241 fdea6aa0 2021-04-30 op
242 fdea6aa0 2021-04-30 op l = xcalloc(1, sizeof(*l));
243 fdea6aa0 2021-04-30 op l->dirfd = -1;
244 fdea6aa0 2021-04-30 op return l;
245 b8e64ccd 2021-03-31 op }
246 b8e64ccd 2021-03-31 op
247 13ed2fb6 2021-01-27 op void
248 6abda252 2021-02-06 op yyerror(const char *msg, ...)
249 13ed2fb6 2021-01-27 op {
250 6abda252 2021-02-06 op va_list ap;
251 6abda252 2021-02-06 op
252 13ed2fb6 2021-01-27 op goterror = 1;
253 6abda252 2021-02-06 op
254 6abda252 2021-02-06 op va_start(ap, msg);
255 6abda252 2021-02-06 op fprintf(stderr, "%s:%d: ", config_path, yylineno);
256 6abda252 2021-02-06 op vfprintf(stderr, msg, ap);
257 a1373913 2021-02-07 op fprintf(stderr, "\n");
258 6abda252 2021-02-06 op va_end(ap);
259 13ed2fb6 2021-01-27 op }
260 13ed2fb6 2021-01-27 op
261 13ed2fb6 2021-01-27 op int
262 13ed2fb6 2021-01-27 op parse_portno(const char *p)
263 13ed2fb6 2021-01-27 op {
264 13ed2fb6 2021-01-27 op const char *errstr;
265 13ed2fb6 2021-01-27 op int n;
266 13ed2fb6 2021-01-27 op
267 13ed2fb6 2021-01-27 op n = strtonum(p, 0, UINT16_MAX, &errstr);
268 13ed2fb6 2021-01-27 op if (errstr != NULL)
269 2d34f732 2021-02-10 op yyerror("port number is %s: %s", errstr, p);
270 13ed2fb6 2021-01-27 op return n;
271 13ed2fb6 2021-01-27 op }
272 13ed2fb6 2021-01-27 op
273 13ed2fb6 2021-01-27 op void
274 13ed2fb6 2021-01-27 op parse_conf(const char *path)
275 13ed2fb6 2021-01-27 op {
276 13ed2fb6 2021-01-27 op config_path = path;
277 13ed2fb6 2021-01-27 op if ((yyin = fopen(path, "r")) == NULL)
278 48b69cb2 2021-04-28 op err(1, "cannot open config: %s", path);
279 13ed2fb6 2021-01-27 op yyparse();
280 13ed2fb6 2021-01-27 op fclose(yyin);
281 13ed2fb6 2021-01-27 op
282 13ed2fb6 2021-01-27 op if (goterror)
283 13ed2fb6 2021-01-27 op exit(1);
284 b8e64ccd 2021-03-31 op
285 b8e64ccd 2021-03-31 op if (TAILQ_FIRST(&hosts)->domain == NULL)
286 48b69cb2 2021-04-28 op errx(1, "no vhost defined in %s", path);
287 13ed2fb6 2021-01-27 op }
288 e17642a7 2021-02-01 op
289 e17642a7 2021-02-01 op char *
290 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
291 e17642a7 2021-02-01 op {
292 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
293 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
294 e17642a7 2021-02-01 op return path;
295 e17642a7 2021-02-01 op }
296 6abda252 2021-02-06 op
297 6abda252 2021-02-06 op int
298 6abda252 2021-02-06 op check_block_code(int n)
299 6abda252 2021-02-06 op {
300 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
301 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
302 6abda252 2021-02-06 op return n;
303 6abda252 2021-02-06 op }
304 6abda252 2021-02-06 op
305 6abda252 2021-02-06 op char *
306 6abda252 2021-02-06 op check_block_fmt(char *fmt)
307 6abda252 2021-02-06 op {
308 6abda252 2021-02-06 op char *s;
309 6abda252 2021-02-06 op
310 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
311 6abda252 2021-02-06 op if (*s != '%')
312 6abda252 2021-02-06 op continue;
313 6abda252 2021-02-06 op switch (*++s) {
314 6abda252 2021-02-06 op case '%':
315 6abda252 2021-02-06 op case 'p':
316 6abda252 2021-02-06 op case 'q':
317 6abda252 2021-02-06 op case 'P':
318 6abda252 2021-02-06 op case 'N':
319 6abda252 2021-02-06 op break;
320 6abda252 2021-02-06 op default:
321 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
322 6abda252 2021-02-06 op }
323 6abda252 2021-02-06 op }
324 6abda252 2021-02-06 op
325 6abda252 2021-02-06 op return fmt;
326 6abda252 2021-02-06 op }
327 6abda252 2021-02-06 op
328 6abda252 2021-02-06 op int
329 6abda252 2021-02-06 op check_strip_no(int n)
330 6abda252 2021-02-06 op {
331 6abda252 2021-02-06 op if (n <= 0)
332 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
333 a709ddf5 2021-02-07 op return n;
334 a709ddf5 2021-02-07 op }
335 a709ddf5 2021-02-07 op
336 a709ddf5 2021-02-07 op int
337 a709ddf5 2021-02-07 op check_prefork_num(int n)
338 a709ddf5 2021-02-07 op {
339 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
340 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
341 6abda252 2021-02-06 op return n;
342 6abda252 2021-02-06 op }
343 49b73ba1 2021-02-10 op
344 49b73ba1 2021-02-10 op void
345 49b73ba1 2021-02-10 op advance_loc(void)
346 49b73ba1 2021-02-10 op {
347 b8e64ccd 2021-03-31 op loc = new_location();
348 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
349 49b73ba1 2021-02-10 op }
350 c705ecb1 2021-05-03 op
351 c705ecb1 2021-05-03 op void
352 c705ecb1 2021-05-03 op only_once(const void *ptr, const char *name)
353 c705ecb1 2021-05-03 op {
354 c705ecb1 2021-05-03 op if (ptr != NULL)
355 c705ecb1 2021-05-03 op yyerror("`%s' specified more than once", name);
356 c705ecb1 2021-05-03 op }