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