Blame


1 15902770 2021-01-15 op %{
2 15902770 2021-01-15 op
3 15902770 2021-01-15 op /*
4 15902770 2021-01-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
5 15902770 2021-01-15 op *
6 15902770 2021-01-15 op * Permission to use, copy, modify, and distribute this software for any
7 15902770 2021-01-15 op * purpose with or without fee is hereby granted, provided that the above
8 15902770 2021-01-15 op * copyright notice and this permission notice appear in all copies.
9 15902770 2021-01-15 op *
10 15902770 2021-01-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 15902770 2021-01-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 15902770 2021-01-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 15902770 2021-01-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 15902770 2021-01-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 15902770 2021-01-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 15902770 2021-01-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 15902770 2021-01-15 op */
18 15902770 2021-01-15 op
19 74f0778b 2021-06-16 op #include <ctype.h>
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 74f0778b 2021-06-16 op #include <stdlib.h>
24 32693ee6 2021-01-28 op #include <string.h>
25 15902770 2021-01-15 op
26 15902770 2021-01-15 op #include "gmid.h"
27 15902770 2021-01-15 op
28 74f0778b 2021-06-16 op FILE *yyfp;
29 74f0778b 2021-06-16 op
30 15902770 2021-01-15 op /*
31 15902770 2021-01-15 op * #define YYDEBUG 1
32 15902770 2021-01-15 op * int yydebug = 1;
33 15902770 2021-01-15 op */
34 15902770 2021-01-15 op
35 ca21e100 2021-02-04 op struct vhost *host;
36 ca21e100 2021-02-04 op struct location *loc;
37 15902770 2021-01-15 op
38 74f0778b 2021-06-16 op static int goterror;
39 74f0778b 2021-06-16 op static int lineno, colno;
40 15902770 2021-01-15 op
41 b8e64ccd 2021-03-31 op static struct vhost *new_vhost(void);
42 b8e64ccd 2021-03-31 op static struct location *new_location(void);
43 b8e64ccd 2021-03-31 op
44 6abda252 2021-02-06 op void yyerror(const char*, ...);
45 74f0778b 2021-06-16 op static int yylex(void);
46 13ed2fb6 2021-01-27 op int parse_portno(const char*);
47 13ed2fb6 2021-01-27 op void parse_conf(const char*);
48 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
49 6abda252 2021-02-06 op int check_block_code(int);
50 6abda252 2021-02-06 op char *check_block_fmt(char*);
51 6abda252 2021-02-06 op int check_strip_no(int);
52 a709ddf5 2021-02-07 op int check_prefork_num(int);
53 49b73ba1 2021-02-10 op void advance_loc(void);
54 c705ecb1 2021-05-03 op void only_once(const void*, const char*);
55 8ad1c570 2021-05-09 op void only_oncei(int, const char*);
56 8ad1c570 2021-05-09 op int fastcgi_conf(char *, char *, char *);
57 c92b802b 2021-06-11 op void add_param(char *, char *, int);
58 13ed2fb6 2021-01-27 op
59 15902770 2021-01-15 op %}
60 15902770 2021-01-15 op
61 15902770 2021-01-15 op /* for bison: */
62 15902770 2021-01-15 op /* %define parse.error verbose */
63 15902770 2021-01-15 op
64 15902770 2021-01-15 op %union {
65 15902770 2021-01-15 op char *str;
66 15902770 2021-01-15 op int num;
67 15902770 2021-01-15 op }
68 15902770 2021-01-15 op
69 d06d6f4b 2021-04-29 op %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
70 d06d6f4b 2021-04-29 op %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
71 8ad1c570 2021-05-09 op %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
72 c92b802b 2021-06-11 op %token TFASTCGI TSPAWN TPARAM
73 d06d6f4b 2021-04-29 op
74 15902770 2021-01-15 op %token TERR
75 15902770 2021-01-15 op
76 15902770 2021-01-15 op %token <str> TSTRING
77 15902770 2021-01-15 op %token <num> TNUM
78 15902770 2021-01-15 op %token <num> TBOOL
79 15902770 2021-01-15 op
80 15902770 2021-01-15 op %%
81 15902770 2021-01-15 op
82 15902770 2021-01-15 op conf : options vhosts ;
83 15902770 2021-01-15 op
84 15902770 2021-01-15 op options : /* empty */
85 15902770 2021-01-15 op | options option
86 15902770 2021-01-15 op ;
87 15902770 2021-01-15 op
88 eb59f87e 2021-02-09 op option : TCHROOT TSTRING { conf.chroot = $2; }
89 eb59f87e 2021-02-09 op | TIPV6 TBOOL { conf.ipv6 = $2; }
90 eb59f87e 2021-02-09 op | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
91 15902770 2021-01-15 op | TPORT TNUM { conf.port = $2; }
92 eb59f87e 2021-02-09 op | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
93 5bc3c98e 2021-01-15 op | TPROTOCOLS TSTRING {
94 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
95 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
96 5bc3c98e 2021-01-15 op }
97 ae08ec7d 2021-01-25 op | TUSER TSTRING { conf.user = $2; }
98 15902770 2021-01-15 op ;
99 15902770 2021-01-15 op
100 15902770 2021-01-15 op vhosts : /* empty */
101 15902770 2021-01-15 op | vhosts vhost
102 15902770 2021-01-15 op ;
103 15902770 2021-01-15 op
104 b8e64ccd 2021-03-31 op vhost : TSERVER TSTRING {
105 b8e64ccd 2021-03-31 op host = new_vhost();
106 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
107 b8e64ccd 2021-03-31 op
108 b8e64ccd 2021-03-31 op loc = new_location();
109 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
110 b8e64ccd 2021-03-31 op
111 b8e64ccd 2021-03-31 op loc->match = xstrdup("*");
112 15902770 2021-01-15 op host->domain = $2;
113 15902770 2021-01-15 op
114 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
115 cbeee4ca 2021-01-28 op warnx("%s:%d \"%s\" looks like punycode: "
116 415ac7a2 2021-01-28 op "you should use the decoded hostname.",
117 74f0778b 2021-06-16 op config_path, lineno, $2);
118 cbeee4ca 2021-01-28 op }
119 b8e64ccd 2021-03-31 op } '{' servopts locations '}' {
120 cbeee4ca 2021-01-28 op
121 fdea6aa0 2021-04-30 op if (host->cert == NULL || host->key == NULL)
122 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
123 15902770 2021-01-15 op }
124 15902770 2021-01-15 op | error '}' { yyerror("error in server directive"); }
125 15902770 2021-01-15 op ;
126 15902770 2021-01-15 op
127 15902770 2021-01-15 op servopts : /* empty */
128 15902770 2021-01-15 op | servopts servopt
129 15902770 2021-01-15 op ;
130 15902770 2021-01-15 op
131 cc8c2901 2021-04-29 op servopt : TALIAS TSTRING {
132 cc8c2901 2021-04-29 op struct alist *a;
133 cc8c2901 2021-04-29 op
134 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
135 cc8c2901 2021-04-29 op a->alias = $2;
136 cc8c2901 2021-04-29 op if (TAILQ_EMPTY(&host->aliases))
137 cc8c2901 2021-04-29 op TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
138 cc8c2901 2021-04-29 op else
139 cc8c2901 2021-04-29 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
140 cc8c2901 2021-04-29 op }
141 c705ecb1 2021-05-03 op | TCERT TSTRING {
142 c705ecb1 2021-05-03 op only_once(host->cert, "cert");
143 c705ecb1 2021-05-03 op host->cert = ensure_absolute_path($2);
144 c705ecb1 2021-05-03 op }
145 15902770 2021-01-15 op | TCGI TSTRING {
146 c705ecb1 2021-05-03 op only_once(host->cgi, "cgi");
147 15902770 2021-01-15 op /* drop the starting '/', if any */
148 709f4c94 2021-02-04 op if (*$2 == '/')
149 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
150 709f4c94 2021-02-04 op host->cgi = $2;
151 05c23a54 2021-01-19 op }
152 e3ddf390 2021-02-06 op | TENTRYPOINT TSTRING {
153 c705ecb1 2021-05-03 op only_once(host->entrypoint, "entrypoint");
154 e3ddf390 2021-02-06 op while (*$2 == '/')
155 e3ddf390 2021-02-06 op memmove($2, $2+1, strlen($2));
156 e3ddf390 2021-02-06 op host->entrypoint = $2;
157 e3ddf390 2021-02-06 op }
158 9cc630aa 2021-04-28 op | TENV TSTRING TSTRING {
159 c92b802b 2021-06-11 op add_param($2, $3, 1);
160 9cc630aa 2021-04-28 op }
161 c705ecb1 2021-05-03 op | TKEY TSTRING {
162 c705ecb1 2021-05-03 op only_once(host->key, "key");
163 c705ecb1 2021-05-03 op host->key = ensure_absolute_path($2);
164 c92b802b 2021-06-11 op }
165 c92b802b 2021-06-11 op | TPARAM TSTRING TSTRING {
166 c92b802b 2021-06-11 op add_param($2, $3, 0);
167 c705ecb1 2021-05-03 op }
168 c8b74339 2021-01-24 op | locopt
169 c8b74339 2021-01-24 op ;
170 c8b74339 2021-01-24 op
171 c8b74339 2021-01-24 op locations : /* empty */
172 c8b74339 2021-01-24 op | locations location
173 c8b74339 2021-01-24 op ;
174 c8b74339 2021-01-24 op
175 49b73ba1 2021-02-10 op location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
176 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
177 49b73ba1 2021-02-10 op if (*$3 == '/')
178 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
179 49b73ba1 2021-02-10 op loc->match = $3;
180 6119e13e 2021-01-19 op }
181 c8b74339 2021-01-24 op | error '}'
182 c8b74339 2021-01-24 op ;
183 c8b74339 2021-01-24 op
184 c8b74339 2021-01-24 op locopts : /* empty */
185 c8b74339 2021-01-24 op | locopts locopt
186 c8b74339 2021-01-24 op ;
187 c8b74339 2021-01-24 op
188 eb59f87e 2021-02-09 op locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
189 6abda252 2021-02-06 op | TBLOCK TRETURN TNUM TSTRING {
190 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
191 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
192 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
193 6abda252 2021-02-06 op }
194 6abda252 2021-02-06 op | TBLOCK TRETURN TNUM {
195 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
196 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
197 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
198 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
199 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
200 6abda252 2021-02-06 op }
201 6abda252 2021-02-06 op | TBLOCK {
202 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
203 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
204 6abda252 2021-02-06 op loc->block_code = 40;
205 6abda252 2021-02-06 op }
206 eb59f87e 2021-02-09 op | TDEFAULT TTYPE TSTRING {
207 c705ecb1 2021-05-03 op only_once(loc->default_mime, "default type");
208 eb59f87e 2021-02-09 op loc->default_mime = $3;
209 eb59f87e 2021-02-09 op }
210 0d047efc 2021-05-24 op | TFASTCGI fastcgi
211 eb59f87e 2021-02-09 op | TINDEX TSTRING {
212 c705ecb1 2021-05-03 op only_once(loc->index, "index");
213 eb59f87e 2021-02-09 op loc->index = $2;
214 eb59f87e 2021-02-09 op }
215 eb59f87e 2021-02-09 op | TLANG TSTRING {
216 c705ecb1 2021-05-03 op only_once(loc->lang, "lang");
217 eb59f87e 2021-02-09 op loc->lang = $2;
218 eb59f87e 2021-02-09 op }
219 793835cb 2021-02-23 op | TLOG TBOOL { loc->disable_log = !$2; }
220 02be96c6 2021-02-09 op | TREQUIRE TCLIENT TCA TSTRING {
221 c705ecb1 2021-05-03 op only_once(loc->reqca, "require client ca");
222 adbe6a64 2021-04-30 op ensure_absolute_path($4);
223 02be96c6 2021-02-09 op if ((loc->reqca = load_ca($4)) == NULL)
224 02be96c6 2021-02-09 op yyerror("couldn't load ca cert: %s", $4);
225 02be96c6 2021-02-09 op free($4);
226 fdea6aa0 2021-04-30 op }
227 fdea6aa0 2021-04-30 op | TROOT TSTRING {
228 c705ecb1 2021-05-03 op only_once(loc->dir, "root");
229 fdea6aa0 2021-04-30 op loc->dir = ensure_absolute_path($2);
230 02be96c6 2021-02-09 op }
231 eb59f87e 2021-02-09 op | TSTRIP TNUM { loc->strip = check_strip_no($2); }
232 15902770 2021-01-15 op ;
233 13ed2fb6 2021-01-27 op
234 0d047efc 2021-05-24 op fastcgi : TSPAWN TSTRING {
235 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
236 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf(NULL, NULL, $2);
237 0d047efc 2021-05-24 op }
238 0d047efc 2021-05-24 op | TSTRING {
239 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
240 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($1, NULL, NULL);
241 0d047efc 2021-05-24 op }
242 0d047efc 2021-05-24 op | TTCP TSTRING TNUM {
243 0d047efc 2021-05-24 op char *c;
244 0d047efc 2021-05-24 op if (asprintf(&c, "%d", $3) == -1)
245 0d047efc 2021-05-24 op err(1, "asprintf");
246 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
247 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, c, NULL);
248 0d047efc 2021-05-24 op }
249 0d047efc 2021-05-24 op | TTCP TSTRING {
250 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
251 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
252 0d047efc 2021-05-24 op }
253 0d047efc 2021-05-24 op | TTCP TSTRING TSTRING {
254 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
255 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, $3, NULL);
256 0d047efc 2021-05-24 op }
257 0d047efc 2021-05-24 op ;
258 0d047efc 2021-05-24 op
259 13ed2fb6 2021-01-27 op %%
260 b8e64ccd 2021-03-31 op
261 b8e64ccd 2021-03-31 op static struct vhost *
262 b8e64ccd 2021-03-31 op new_vhost(void)
263 b8e64ccd 2021-03-31 op {
264 b8e64ccd 2021-03-31 op return xcalloc(1, sizeof(struct vhost));
265 b8e64ccd 2021-03-31 op }
266 13ed2fb6 2021-01-27 op
267 b8e64ccd 2021-03-31 op static struct location *
268 b8e64ccd 2021-03-31 op new_location(void)
269 b8e64ccd 2021-03-31 op {
270 fdea6aa0 2021-04-30 op struct location *l;
271 fdea6aa0 2021-04-30 op
272 fdea6aa0 2021-04-30 op l = xcalloc(1, sizeof(*l));
273 fdea6aa0 2021-04-30 op l->dirfd = -1;
274 8ad1c570 2021-05-09 op l->fcgi = -1;
275 fdea6aa0 2021-04-30 op return l;
276 b8e64ccd 2021-03-31 op }
277 b8e64ccd 2021-03-31 op
278 13ed2fb6 2021-01-27 op void
279 6abda252 2021-02-06 op yyerror(const char *msg, ...)
280 13ed2fb6 2021-01-27 op {
281 6abda252 2021-02-06 op va_list ap;
282 6abda252 2021-02-06 op
283 13ed2fb6 2021-01-27 op goterror = 1;
284 6abda252 2021-02-06 op
285 6abda252 2021-02-06 op va_start(ap, msg);
286 74f0778b 2021-06-16 op fprintf(stderr, "%s:%d: ", config_path, lineno);
287 6abda252 2021-02-06 op vfprintf(stderr, msg, ap);
288 a1373913 2021-02-07 op fprintf(stderr, "\n");
289 6abda252 2021-02-06 op va_end(ap);
290 74f0778b 2021-06-16 op }
291 74f0778b 2021-06-16 op
292 74f0778b 2021-06-16 op static struct keyword {
293 74f0778b 2021-06-16 op const char *word;
294 74f0778b 2021-06-16 op int token;
295 74f0778b 2021-06-16 op } keywords[] = {
296 74f0778b 2021-06-16 op {"alias", TALIAS},
297 74f0778b 2021-06-16 op {"auto", TAUTO},
298 74f0778b 2021-06-16 op {"block", TBLOCK},
299 74f0778b 2021-06-16 op {"ca", TCA},
300 74f0778b 2021-06-16 op {"cert", TCERT},
301 74f0778b 2021-06-16 op {"cgi", TCGI},
302 74f0778b 2021-06-16 op {"chroot", TCHROOT},
303 74f0778b 2021-06-16 op {"client", TCLIENT},
304 74f0778b 2021-06-16 op {"default", TDEFAULT},
305 74f0778b 2021-06-16 op {"entrypoint", TENTRYPOINT},
306 74f0778b 2021-06-16 op {"env", TENV},
307 74f0778b 2021-06-16 op {"fastcgi", TFASTCGI},
308 74f0778b 2021-06-16 op {"index", TINDEX},
309 74f0778b 2021-06-16 op {"ipv6", TIPV6},
310 74f0778b 2021-06-16 op {"key", TKEY},
311 74f0778b 2021-06-16 op {"lang", TLANG},
312 74f0778b 2021-06-16 op {"location", TLOCATION},
313 74f0778b 2021-06-16 op {"log", TLOG},
314 74f0778b 2021-06-16 op {"mime", TMIME},
315 74f0778b 2021-06-16 op {"param", TPARAM},
316 74f0778b 2021-06-16 op {"port", TPORT},
317 74f0778b 2021-06-16 op {"prefork", TPREFORK},
318 74f0778b 2021-06-16 op {"protocols", TPROTOCOLS},
319 74f0778b 2021-06-16 op {"require", TREQUIRE},
320 74f0778b 2021-06-16 op {"return", TRETURN},
321 74f0778b 2021-06-16 op {"root", TROOT},
322 74f0778b 2021-06-16 op {"server", TSERVER},
323 74f0778b 2021-06-16 op {"spawn", TSPAWN},
324 74f0778b 2021-06-16 op {"strip", TSTRIP},
325 74f0778b 2021-06-16 op {"tcp", TTCP},
326 74f0778b 2021-06-16 op {"type", TTYPE},
327 74f0778b 2021-06-16 op {"user", TUSER},
328 74f0778b 2021-06-16 op };
329 74f0778b 2021-06-16 op
330 74f0778b 2021-06-16 op /*
331 74f0778b 2021-06-16 op * Taken an adapted from doas' parse.y
332 74f0778b 2021-06-16 op */
333 74f0778b 2021-06-16 op static int
334 74f0778b 2021-06-16 op yylex(void)
335 74f0778b 2021-06-16 op {
336 74f0778b 2021-06-16 op char buf[1024], *ebuf, *p, *str;
337 74f0778b 2021-06-16 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
338 74f0778b 2021-06-16 op size_t i;
339 74f0778b 2021-06-16 op
340 74f0778b 2021-06-16 op p = buf;
341 74f0778b 2021-06-16 op ebuf = buf + sizeof(buf);
342 74f0778b 2021-06-16 op
343 74f0778b 2021-06-16 op repeat:
344 74f0778b 2021-06-16 op /* skip whitespace first */
345 74f0778b 2021-06-16 op for (c = getc(yyfp); isspace(c); c = getc(yyfp)) {
346 74f0778b 2021-06-16 op colno++;
347 74f0778b 2021-06-16 op if (c == '\n') {
348 74f0778b 2021-06-16 op lineno++;
349 74f0778b 2021-06-16 op colno = 0;
350 74f0778b 2021-06-16 op }
351 74f0778b 2021-06-16 op }
352 74f0778b 2021-06-16 op
353 74f0778b 2021-06-16 op /* check for special one-character constructions */
354 74f0778b 2021-06-16 op switch (c) {
355 74f0778b 2021-06-16 op case '{':
356 74f0778b 2021-06-16 op case '}':
357 74f0778b 2021-06-16 op return c;
358 74f0778b 2021-06-16 op case '#':
359 74f0778b 2021-06-16 op /* skip comments; NUL is allowed; no continuation */
360 74f0778b 2021-06-16 op while ((c = getc(yyfp)) != '\n')
361 74f0778b 2021-06-16 op if (c == EOF)
362 74f0778b 2021-06-16 op goto eof;
363 74f0778b 2021-06-16 op colno = 0;
364 74f0778b 2021-06-16 op lineno++;
365 74f0778b 2021-06-16 op goto repeat;
366 74f0778b 2021-06-16 op case EOF:
367 74f0778b 2021-06-16 op goto eof;
368 74f0778b 2021-06-16 op }
369 74f0778b 2021-06-16 op
370 74f0778b 2021-06-16 op /* parsing next word */
371 74f0778b 2021-06-16 op for (;; c = getc(yyfp), colno++) {
372 74f0778b 2021-06-16 op switch (c) {
373 74f0778b 2021-06-16 op case '\0':
374 74f0778b 2021-06-16 op yyerror("unallowed character NULL in column %d",
375 74f0778b 2021-06-16 op colno+1);
376 74f0778b 2021-06-16 op escape = 0;
377 74f0778b 2021-06-16 op continue;
378 74f0778b 2021-06-16 op case '\\':
379 74f0778b 2021-06-16 op escape = !escape;
380 74f0778b 2021-06-16 op if (escape)
381 74f0778b 2021-06-16 op continue;
382 74f0778b 2021-06-16 op break;
383 74f0778b 2021-06-16 op case '\n':
384 74f0778b 2021-06-16 op if (quotes)
385 74f0778b 2021-06-16 op yyerror("unterminated quotes in column %d",
386 74f0778b 2021-06-16 op colno+1);
387 74f0778b 2021-06-16 op if (escape) {
388 74f0778b 2021-06-16 op nonkw = 1;
389 74f0778b 2021-06-16 op escape = 0;
390 74f0778b 2021-06-16 op colno = 0;
391 74f0778b 2021-06-16 op lineno++;
392 74f0778b 2021-06-16 op }
393 74f0778b 2021-06-16 op goto eow;
394 74f0778b 2021-06-16 op case EOF:
395 74f0778b 2021-06-16 op if (escape)
396 74f0778b 2021-06-16 op yyerror("unterminated escape in column %d",
397 74f0778b 2021-06-16 op colno);
398 74f0778b 2021-06-16 op if (quotes)
399 74f0778b 2021-06-16 op yyerror("unterminated quotes in column %d",
400 74f0778b 2021-06-16 op qpos+1);
401 74f0778b 2021-06-16 op goto eow;
402 74f0778b 2021-06-16 op case '{':
403 74f0778b 2021-06-16 op case '}':
404 74f0778b 2021-06-16 op case '#':
405 74f0778b 2021-06-16 op case ' ':
406 74f0778b 2021-06-16 op case '\t':
407 74f0778b 2021-06-16 op if (!escape && !quotes)
408 74f0778b 2021-06-16 op goto eow;
409 74f0778b 2021-06-16 op break;
410 74f0778b 2021-06-16 op case '"':
411 74f0778b 2021-06-16 op if (!escape) {
412 74f0778b 2021-06-16 op quotes = !quotes;
413 74f0778b 2021-06-16 op if (quotes) {
414 74f0778b 2021-06-16 op nonkw = 1;
415 74f0778b 2021-06-16 op qpos = colno;
416 74f0778b 2021-06-16 op }
417 74f0778b 2021-06-16 op continue;
418 74f0778b 2021-06-16 op }
419 74f0778b 2021-06-16 op }
420 74f0778b 2021-06-16 op *p++ = c;
421 74f0778b 2021-06-16 op if (p == ebuf) {
422 74f0778b 2021-06-16 op yyerror("line too long");
423 74f0778b 2021-06-16 op p = buf;
424 74f0778b 2021-06-16 op }
425 74f0778b 2021-06-16 op escape = 0;
426 74f0778b 2021-06-16 op }
427 74f0778b 2021-06-16 op
428 74f0778b 2021-06-16 op eow:
429 74f0778b 2021-06-16 op *p = 0;
430 74f0778b 2021-06-16 op if (c != EOF)
431 74f0778b 2021-06-16 op ungetc(c, yyfp);
432 74f0778b 2021-06-16 op if (p == buf) {
433 74f0778b 2021-06-16 op /*
434 74f0778b 2021-06-16 op * There could be a number of reason for empty buffer,
435 74f0778b 2021-06-16 op * and we handle all of them here, to avoid cluttering
436 74f0778b 2021-06-16 op * the main loop.
437 74f0778b 2021-06-16 op */
438 74f0778b 2021-06-16 op if (c == EOF)
439 74f0778b 2021-06-16 op goto eof;
440 74f0778b 2021-06-16 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
441 74f0778b 2021-06-16 op goto repeat;
442 74f0778b 2021-06-16 op }
443 74f0778b 2021-06-16 op if (!nonkw) {
444 74f0778b 2021-06-16 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); ++i) {
445 74f0778b 2021-06-16 op if (!strcmp(buf, keywords[i].word))
446 74f0778b 2021-06-16 op return keywords[i].token;
447 74f0778b 2021-06-16 op }
448 74f0778b 2021-06-16 op }
449 74f0778b 2021-06-16 op c = *buf;
450 74f0778b 2021-06-16 op if (!nonkw && (c == '-' || isdigit(c))) {
451 74f0778b 2021-06-16 op yylval.num = parse_portno(buf);
452 74f0778b 2021-06-16 op return TNUM;
453 74f0778b 2021-06-16 op }
454 74f0778b 2021-06-16 op if (!nonkw && !strcmp(buf, "on")) {
455 74f0778b 2021-06-16 op yylval.num = 1;
456 74f0778b 2021-06-16 op return TBOOL;
457 74f0778b 2021-06-16 op }
458 74f0778b 2021-06-16 op if (!nonkw && !strcmp(buf, "off")) {
459 74f0778b 2021-06-16 op yylval.num = 0;
460 74f0778b 2021-06-16 op return TBOOL;
461 74f0778b 2021-06-16 op }
462 74f0778b 2021-06-16 op if ((str = strdup(buf)) == NULL)
463 74f0778b 2021-06-16 op err(1, "%s", __func__);
464 74f0778b 2021-06-16 op yylval.str = str;
465 74f0778b 2021-06-16 op return TSTRING;
466 74f0778b 2021-06-16 op
467 74f0778b 2021-06-16 op eof:
468 74f0778b 2021-06-16 op if (ferror(yyfp))
469 74f0778b 2021-06-16 op yyerror("input error reading config");
470 74f0778b 2021-06-16 op return 0;
471 13ed2fb6 2021-01-27 op }
472 13ed2fb6 2021-01-27 op
473 13ed2fb6 2021-01-27 op int
474 13ed2fb6 2021-01-27 op parse_portno(const char *p)
475 13ed2fb6 2021-01-27 op {
476 13ed2fb6 2021-01-27 op const char *errstr;
477 13ed2fb6 2021-01-27 op int n;
478 13ed2fb6 2021-01-27 op
479 13ed2fb6 2021-01-27 op n = strtonum(p, 0, UINT16_MAX, &errstr);
480 13ed2fb6 2021-01-27 op if (errstr != NULL)
481 2d34f732 2021-02-10 op yyerror("port number is %s: %s", errstr, p);
482 13ed2fb6 2021-01-27 op return n;
483 13ed2fb6 2021-01-27 op }
484 13ed2fb6 2021-01-27 op
485 13ed2fb6 2021-01-27 op void
486 13ed2fb6 2021-01-27 op parse_conf(const char *path)
487 13ed2fb6 2021-01-27 op {
488 13ed2fb6 2021-01-27 op config_path = path;
489 74f0778b 2021-06-16 op if ((yyfp = fopen(path, "r")) == NULL)
490 48b69cb2 2021-04-28 op err(1, "cannot open config: %s", path);
491 13ed2fb6 2021-01-27 op yyparse();
492 74f0778b 2021-06-16 op fclose(yyfp);
493 13ed2fb6 2021-01-27 op
494 13ed2fb6 2021-01-27 op if (goterror)
495 13ed2fb6 2021-01-27 op exit(1);
496 b8e64ccd 2021-03-31 op
497 b8e64ccd 2021-03-31 op if (TAILQ_FIRST(&hosts)->domain == NULL)
498 48b69cb2 2021-04-28 op errx(1, "no vhost defined in %s", path);
499 13ed2fb6 2021-01-27 op }
500 e17642a7 2021-02-01 op
501 e17642a7 2021-02-01 op char *
502 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
503 e17642a7 2021-02-01 op {
504 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
505 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
506 e17642a7 2021-02-01 op return path;
507 e17642a7 2021-02-01 op }
508 6abda252 2021-02-06 op
509 6abda252 2021-02-06 op int
510 6abda252 2021-02-06 op check_block_code(int n)
511 6abda252 2021-02-06 op {
512 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
513 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
514 6abda252 2021-02-06 op return n;
515 6abda252 2021-02-06 op }
516 6abda252 2021-02-06 op
517 6abda252 2021-02-06 op char *
518 6abda252 2021-02-06 op check_block_fmt(char *fmt)
519 6abda252 2021-02-06 op {
520 6abda252 2021-02-06 op char *s;
521 6abda252 2021-02-06 op
522 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
523 6abda252 2021-02-06 op if (*s != '%')
524 6abda252 2021-02-06 op continue;
525 6abda252 2021-02-06 op switch (*++s) {
526 6abda252 2021-02-06 op case '%':
527 6abda252 2021-02-06 op case 'p':
528 6abda252 2021-02-06 op case 'q':
529 6abda252 2021-02-06 op case 'P':
530 6abda252 2021-02-06 op case 'N':
531 6abda252 2021-02-06 op break;
532 6abda252 2021-02-06 op default:
533 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
534 6abda252 2021-02-06 op }
535 6abda252 2021-02-06 op }
536 6abda252 2021-02-06 op
537 6abda252 2021-02-06 op return fmt;
538 6abda252 2021-02-06 op }
539 6abda252 2021-02-06 op
540 6abda252 2021-02-06 op int
541 6abda252 2021-02-06 op check_strip_no(int n)
542 6abda252 2021-02-06 op {
543 6abda252 2021-02-06 op if (n <= 0)
544 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
545 a709ddf5 2021-02-07 op return n;
546 a709ddf5 2021-02-07 op }
547 a709ddf5 2021-02-07 op
548 a709ddf5 2021-02-07 op int
549 a709ddf5 2021-02-07 op check_prefork_num(int n)
550 a709ddf5 2021-02-07 op {
551 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
552 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
553 6abda252 2021-02-06 op return n;
554 6abda252 2021-02-06 op }
555 49b73ba1 2021-02-10 op
556 49b73ba1 2021-02-10 op void
557 49b73ba1 2021-02-10 op advance_loc(void)
558 49b73ba1 2021-02-10 op {
559 b8e64ccd 2021-03-31 op loc = new_location();
560 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
561 49b73ba1 2021-02-10 op }
562 c705ecb1 2021-05-03 op
563 c705ecb1 2021-05-03 op void
564 c705ecb1 2021-05-03 op only_once(const void *ptr, const char *name)
565 c705ecb1 2021-05-03 op {
566 c705ecb1 2021-05-03 op if (ptr != NULL)
567 c705ecb1 2021-05-03 op yyerror("`%s' specified more than once", name);
568 c705ecb1 2021-05-03 op }
569 8ad1c570 2021-05-09 op
570 8ad1c570 2021-05-09 op void
571 8ad1c570 2021-05-09 op only_oncei(int i, const char *name)
572 8ad1c570 2021-05-09 op {
573 8ad1c570 2021-05-09 op if (i != -1)
574 8ad1c570 2021-05-09 op yyerror("`%s' specified more than once", name);
575 8ad1c570 2021-05-09 op }
576 8ad1c570 2021-05-09 op
577 8ad1c570 2021-05-09 op int
578 8ad1c570 2021-05-09 op fastcgi_conf(char *path, char *port, char *prog)
579 8ad1c570 2021-05-09 op {
580 8ad1c570 2021-05-09 op struct fcgi *f;
581 8ad1c570 2021-05-09 op int i;
582 8ad1c570 2021-05-09 op
583 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
584 8ad1c570 2021-05-09 op f = &fcgi[i];
585 8ad1c570 2021-05-09 op
586 8ad1c570 2021-05-09 op if (f->path == NULL) {
587 8ad1c570 2021-05-09 op f->id = i;
588 8ad1c570 2021-05-09 op f->path = path;
589 8ad1c570 2021-05-09 op f->port = port;
590 8ad1c570 2021-05-09 op f->prog = prog;
591 8ad1c570 2021-05-09 op return i;
592 8ad1c570 2021-05-09 op }
593 8ad1c570 2021-05-09 op
594 8ad1c570 2021-05-09 op /* XXX: what to do with prog? */
595 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
596 8ad1c570 2021-05-09 op ((port == NULL && f->port == NULL) ||
597 8ad1c570 2021-05-09 op !strcmp(f->port, port))) {
598 8ad1c570 2021-05-09 op free(path);
599 8ad1c570 2021-05-09 op free(port);
600 8ad1c570 2021-05-09 op return i;
601 8ad1c570 2021-05-09 op }
602 8ad1c570 2021-05-09 op }
603 8ad1c570 2021-05-09 op
604 8ad1c570 2021-05-09 op yyerror("too much `fastcgi' rules defined.");
605 8ad1c570 2021-05-09 op return -1;
606 c92b802b 2021-06-11 op }
607 c92b802b 2021-06-11 op
608 c92b802b 2021-06-11 op void
609 c92b802b 2021-06-11 op add_param(char *name, char *val, int env)
610 c92b802b 2021-06-11 op {
611 c92b802b 2021-06-11 op struct envlist *e;
612 c92b802b 2021-06-11 op struct envhead *h;
613 c92b802b 2021-06-11 op
614 c92b802b 2021-06-11 op if (env)
615 c92b802b 2021-06-11 op h = &host->env;
616 c92b802b 2021-06-11 op else
617 c92b802b 2021-06-11 op h = &host->params;
618 c92b802b 2021-06-11 op
619 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
620 c92b802b 2021-06-11 op e->name = name;
621 c92b802b 2021-06-11 op e->value = val;
622 c92b802b 2021-06-11 op if (TAILQ_EMPTY(h))
623 c92b802b 2021-06-11 op TAILQ_INSERT_HEAD(h, e, envs);
624 c92b802b 2021-06-11 op else
625 c92b802b 2021-06-11 op TAILQ_INSERT_TAIL(h, e, envs);
626 8ad1c570 2021-05-09 op }