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