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