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 13ed2fb6 2021-01-27 op
50 15902770 2021-01-15 op %}
51 15902770 2021-01-15 op
52 15902770 2021-01-15 op /* for bison: */
53 15902770 2021-01-15 op /* %define parse.error verbose */
54 15902770 2021-01-15 op
55 15902770 2021-01-15 op %union {
56 15902770 2021-01-15 op char *str;
57 15902770 2021-01-15 op int num;
58 15902770 2021-01-15 op }
59 15902770 2021-01-15 op
60 46af8c6c 2021-01-27 op %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE
61 a709ddf5 2021-02-07 op %token TCHROOT TUSER TSERVER TPREFORK
62 793835cb 2021-02-23 op %token TLOCATION TCERT TKEY TROOT TCGI TLANG TLOG TINDEX TAUTO
63 02be96c6 2021-02-09 op %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA
64 15902770 2021-01-15 op %token TERR
65 15902770 2021-01-15 op
66 15902770 2021-01-15 op %token <str> TSTRING
67 15902770 2021-01-15 op %token <num> TNUM
68 15902770 2021-01-15 op %token <num> TBOOL
69 15902770 2021-01-15 op
70 15902770 2021-01-15 op %%
71 15902770 2021-01-15 op
72 15902770 2021-01-15 op conf : options vhosts ;
73 15902770 2021-01-15 op
74 15902770 2021-01-15 op options : /* empty */
75 15902770 2021-01-15 op | options option
76 15902770 2021-01-15 op ;
77 15902770 2021-01-15 op
78 eb59f87e 2021-02-09 op option : TCHROOT TSTRING { conf.chroot = $2; }
79 eb59f87e 2021-02-09 op | TIPV6 TBOOL { conf.ipv6 = $2; }
80 eb59f87e 2021-02-09 op | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
81 15902770 2021-01-15 op | TPORT TNUM { conf.port = $2; }
82 eb59f87e 2021-02-09 op | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
83 5bc3c98e 2021-01-15 op | TPROTOCOLS TSTRING {
84 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
85 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
86 5bc3c98e 2021-01-15 op }
87 ae08ec7d 2021-01-25 op | TUSER TSTRING { conf.user = $2; }
88 15902770 2021-01-15 op ;
89 15902770 2021-01-15 op
90 15902770 2021-01-15 op vhosts : /* empty */
91 15902770 2021-01-15 op | vhosts vhost
92 15902770 2021-01-15 op ;
93 15902770 2021-01-15 op
94 b8e64ccd 2021-03-31 op vhost : TSERVER TSTRING {
95 b8e64ccd 2021-03-31 op host = new_vhost();
96 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
97 b8e64ccd 2021-03-31 op
98 b8e64ccd 2021-03-31 op loc = new_location();
99 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
100 b8e64ccd 2021-03-31 op
101 b8e64ccd 2021-03-31 op loc->match = xstrdup("*");
102 15902770 2021-01-15 op host->domain = $2;
103 15902770 2021-01-15 op
104 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
105 cbeee4ca 2021-01-28 op warnx("%s:%d \"%s\" looks like punycode: "
106 415ac7a2 2021-01-28 op "you should use the decoded hostname.",
107 415ac7a2 2021-01-28 op config_path, yylineno, $2);
108 cbeee4ca 2021-01-28 op }
109 b8e64ccd 2021-03-31 op } '{' servopts locations '}' {
110 cbeee4ca 2021-01-28 op
111 15902770 2021-01-15 op if (host->cert == NULL || host->key == NULL ||
112 15902770 2021-01-15 op host->dir == 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 e17642a7 2021-02-01 op servopt : TCERT TSTRING { host->cert = ensure_absolute_path($2); }
123 15902770 2021-01-15 op | TCGI TSTRING {
124 15902770 2021-01-15 op /* drop the starting '/', if any */
125 709f4c94 2021-02-04 op if (*$2 == '/')
126 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
127 709f4c94 2021-02-04 op host->cgi = $2;
128 05c23a54 2021-01-19 op }
129 e3ddf390 2021-02-06 op | TENTRYPOINT TSTRING {
130 e3ddf390 2021-02-06 op if (host->entrypoint != NULL)
131 e3ddf390 2021-02-06 op yyerror("`entrypoint' specified more than once");
132 e3ddf390 2021-02-06 op while (*$2 == '/')
133 e3ddf390 2021-02-06 op memmove($2, $2+1, strlen($2));
134 e3ddf390 2021-02-06 op host->entrypoint = $2;
135 e3ddf390 2021-02-06 op }
136 eb59f87e 2021-02-09 op | TKEY TSTRING { host->key = ensure_absolute_path($2); }
137 eb59f87e 2021-02-09 op | TROOT TSTRING { host->dir = ensure_absolute_path($2); }
138 c8b74339 2021-01-24 op | locopt
139 c8b74339 2021-01-24 op ;
140 c8b74339 2021-01-24 op
141 c8b74339 2021-01-24 op locations : /* empty */
142 c8b74339 2021-01-24 op | locations location
143 c8b74339 2021-01-24 op ;
144 c8b74339 2021-01-24 op
145 49b73ba1 2021-02-10 op location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
146 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
147 49b73ba1 2021-02-10 op if (*$3 == '/')
148 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
149 49b73ba1 2021-02-10 op loc->match = $3;
150 6119e13e 2021-01-19 op }
151 c8b74339 2021-01-24 op | error '}'
152 c8b74339 2021-01-24 op ;
153 c8b74339 2021-01-24 op
154 c8b74339 2021-01-24 op locopts : /* empty */
155 c8b74339 2021-01-24 op | locopts locopt
156 c8b74339 2021-01-24 op ;
157 c8b74339 2021-01-24 op
158 eb59f87e 2021-02-09 op locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
159 6abda252 2021-02-06 op | TBLOCK TRETURN TNUM TSTRING {
160 6abda252 2021-02-06 op if (loc->block_fmt != NULL)
161 6abda252 2021-02-06 op yyerror("`block' rule specified more than once");
162 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
163 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
164 6abda252 2021-02-06 op }
165 6abda252 2021-02-06 op | TBLOCK TRETURN TNUM {
166 6abda252 2021-02-06 op if (loc->block_fmt != NULL)
167 6abda252 2021-02-06 op yyerror("`block' rule specified more than once");
168 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
169 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
170 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
171 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
172 6abda252 2021-02-06 op }
173 6abda252 2021-02-06 op | TBLOCK {
174 6abda252 2021-02-06 op if (loc->block_fmt != NULL)
175 6abda252 2021-02-06 op yyerror("`block' rule specified more than once");
176 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
177 6abda252 2021-02-06 op loc->block_code = 40;
178 6abda252 2021-02-06 op }
179 eb59f87e 2021-02-09 op | TDEFAULT TTYPE TSTRING {
180 eb59f87e 2021-02-09 op if (loc->default_mime != NULL)
181 eb59f87e 2021-02-09 op yyerror("`default type' specified more than once");
182 eb59f87e 2021-02-09 op loc->default_mime = $3;
183 eb59f87e 2021-02-09 op }
184 eb59f87e 2021-02-09 op | TINDEX TSTRING {
185 eb59f87e 2021-02-09 op if (loc->index != NULL)
186 eb59f87e 2021-02-09 op yyerror("`index' specified more than once");
187 eb59f87e 2021-02-09 op loc->index = $2;
188 eb59f87e 2021-02-09 op }
189 eb59f87e 2021-02-09 op | TLANG TSTRING {
190 eb59f87e 2021-02-09 op if (loc->lang != NULL)
191 eb59f87e 2021-02-09 op yyerror("`lang' specified more than once");
192 eb59f87e 2021-02-09 op loc->lang = $2;
193 eb59f87e 2021-02-09 op }
194 793835cb 2021-02-23 op | TLOG TBOOL { loc->disable_log = !$2; }
195 02be96c6 2021-02-09 op | TREQUIRE TCLIENT TCA TSTRING {
196 02be96c6 2021-02-09 op if (loc->reqca != NULL)
197 02be96c6 2021-02-09 op yyerror("`require client ca' specified more than once");
198 02be96c6 2021-02-09 op if (*$4 != '/')
199 02be96c6 2021-02-09 op yyerror("path to certificate must be absolute: %s", $4);
200 02be96c6 2021-02-09 op if ((loc->reqca = load_ca($4)) == NULL)
201 02be96c6 2021-02-09 op yyerror("couldn't load ca cert: %s", $4);
202 02be96c6 2021-02-09 op free($4);
203 02be96c6 2021-02-09 op }
204 eb59f87e 2021-02-09 op | TSTRIP TNUM { loc->strip = check_strip_no($2); }
205 15902770 2021-01-15 op ;
206 13ed2fb6 2021-01-27 op
207 13ed2fb6 2021-01-27 op %%
208 b8e64ccd 2021-03-31 op
209 b8e64ccd 2021-03-31 op static struct vhost *
210 b8e64ccd 2021-03-31 op new_vhost(void)
211 b8e64ccd 2021-03-31 op {
212 b8e64ccd 2021-03-31 op return xcalloc(1, sizeof(struct vhost));
213 b8e64ccd 2021-03-31 op }
214 13ed2fb6 2021-01-27 op
215 b8e64ccd 2021-03-31 op static struct location *
216 b8e64ccd 2021-03-31 op new_location(void)
217 b8e64ccd 2021-03-31 op {
218 b8e64ccd 2021-03-31 op return xcalloc(1, sizeof(struct location));
219 b8e64ccd 2021-03-31 op }
220 b8e64ccd 2021-03-31 op
221 13ed2fb6 2021-01-27 op void
222 6abda252 2021-02-06 op yyerror(const char *msg, ...)
223 13ed2fb6 2021-01-27 op {
224 6abda252 2021-02-06 op va_list ap;
225 6abda252 2021-02-06 op
226 13ed2fb6 2021-01-27 op goterror = 1;
227 6abda252 2021-02-06 op
228 6abda252 2021-02-06 op va_start(ap, msg);
229 6abda252 2021-02-06 op fprintf(stderr, "%s:%d: ", config_path, yylineno);
230 6abda252 2021-02-06 op vfprintf(stderr, msg, ap);
231 a1373913 2021-02-07 op fprintf(stderr, "\n");
232 6abda252 2021-02-06 op va_end(ap);
233 13ed2fb6 2021-01-27 op }
234 13ed2fb6 2021-01-27 op
235 13ed2fb6 2021-01-27 op int
236 13ed2fb6 2021-01-27 op parse_portno(const char *p)
237 13ed2fb6 2021-01-27 op {
238 13ed2fb6 2021-01-27 op const char *errstr;
239 13ed2fb6 2021-01-27 op int n;
240 13ed2fb6 2021-01-27 op
241 13ed2fb6 2021-01-27 op n = strtonum(p, 0, UINT16_MAX, &errstr);
242 13ed2fb6 2021-01-27 op if (errstr != NULL)
243 2d34f732 2021-02-10 op yyerror("port number is %s: %s", errstr, p);
244 13ed2fb6 2021-01-27 op return n;
245 13ed2fb6 2021-01-27 op }
246 13ed2fb6 2021-01-27 op
247 13ed2fb6 2021-01-27 op void
248 13ed2fb6 2021-01-27 op parse_conf(const char *path)
249 13ed2fb6 2021-01-27 op {
250 13ed2fb6 2021-01-27 op config_path = path;
251 13ed2fb6 2021-01-27 op if ((yyin = fopen(path, "r")) == NULL)
252 002a84a1 2021-02-10 op fatal("cannot open config: %s: %s", path, strerror(errno));
253 13ed2fb6 2021-01-27 op yyparse();
254 13ed2fb6 2021-01-27 op fclose(yyin);
255 13ed2fb6 2021-01-27 op
256 13ed2fb6 2021-01-27 op if (goterror)
257 13ed2fb6 2021-01-27 op exit(1);
258 b8e64ccd 2021-03-31 op
259 b8e64ccd 2021-03-31 op if (TAILQ_FIRST(&hosts)->domain == NULL)
260 b8e64ccd 2021-03-31 op fatal("no vhost defined in %s", path);
261 13ed2fb6 2021-01-27 op }
262 e17642a7 2021-02-01 op
263 e17642a7 2021-02-01 op char *
264 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
265 e17642a7 2021-02-01 op {
266 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
267 e17642a7 2021-02-01 op yyerror("not an absolute path");
268 e17642a7 2021-02-01 op return path;
269 e17642a7 2021-02-01 op }
270 6abda252 2021-02-06 op
271 6abda252 2021-02-06 op int
272 6abda252 2021-02-06 op check_block_code(int n)
273 6abda252 2021-02-06 op {
274 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
275 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
276 6abda252 2021-02-06 op return n;
277 6abda252 2021-02-06 op }
278 6abda252 2021-02-06 op
279 6abda252 2021-02-06 op char *
280 6abda252 2021-02-06 op check_block_fmt(char *fmt)
281 6abda252 2021-02-06 op {
282 6abda252 2021-02-06 op char *s;
283 6abda252 2021-02-06 op
284 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
285 6abda252 2021-02-06 op if (*s != '%')
286 6abda252 2021-02-06 op continue;
287 6abda252 2021-02-06 op switch (*++s) {
288 6abda252 2021-02-06 op case '%':
289 6abda252 2021-02-06 op case 'p':
290 6abda252 2021-02-06 op case 'q':
291 6abda252 2021-02-06 op case 'P':
292 6abda252 2021-02-06 op case 'N':
293 6abda252 2021-02-06 op break;
294 6abda252 2021-02-06 op default:
295 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
296 6abda252 2021-02-06 op }
297 6abda252 2021-02-06 op }
298 6abda252 2021-02-06 op
299 6abda252 2021-02-06 op return fmt;
300 6abda252 2021-02-06 op }
301 6abda252 2021-02-06 op
302 6abda252 2021-02-06 op int
303 6abda252 2021-02-06 op check_strip_no(int n)
304 6abda252 2021-02-06 op {
305 6abda252 2021-02-06 op if (n <= 0)
306 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
307 a709ddf5 2021-02-07 op return n;
308 a709ddf5 2021-02-07 op }
309 a709ddf5 2021-02-07 op
310 a709ddf5 2021-02-07 op int
311 a709ddf5 2021-02-07 op check_prefork_num(int n)
312 a709ddf5 2021-02-07 op {
313 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
314 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
315 6abda252 2021-02-06 op return n;
316 6abda252 2021-02-06 op }
317 49b73ba1 2021-02-10 op
318 49b73ba1 2021-02-10 op void
319 49b73ba1 2021-02-10 op advance_loc(void)
320 49b73ba1 2021-02-10 op {
321 b8e64ccd 2021-03-31 op loc = new_location();
322 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
323 49b73ba1 2021-02-10 op }