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 c39be742 2021-07-09 op * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
6 c39be742 2021-07-09 op * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
7 c39be742 2021-07-09 op * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
8 c39be742 2021-07-09 op * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
9 c39be742 2021-07-09 op * Copyright (c) 2001 Markus Friedl. All rights reserved.
10 c39be742 2021-07-09 op * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
11 c39be742 2021-07-09 op * Copyright (c) 2001 Theo de Raadt. All rights reserved.
12 15902770 2021-01-15 op *
13 15902770 2021-01-15 op * Permission to use, copy, modify, and distribute this software for any
14 15902770 2021-01-15 op * purpose with or without fee is hereby granted, provided that the above
15 15902770 2021-01-15 op * copyright notice and this permission notice appear in all copies.
16 15902770 2021-01-15 op *
17 15902770 2021-01-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 15902770 2021-01-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 15902770 2021-01-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 15902770 2021-01-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 15902770 2021-01-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 15902770 2021-01-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 15902770 2021-01-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 15902770 2021-01-15 op */
25 15902770 2021-01-15 op
26 74f0778b 2021-06-16 op #include <ctype.h>
27 002a84a1 2021-02-10 op #include <errno.h>
28 6abda252 2021-02-06 op #include <stdarg.h>
29 15902770 2021-01-15 op #include <stdio.h>
30 74f0778b 2021-06-16 op #include <stdlib.h>
31 32693ee6 2021-01-28 op #include <string.h>
32 15902770 2021-01-15 op
33 15902770 2021-01-15 op #include "gmid.h"
34 15902770 2021-01-15 op
35 c39be742 2021-07-09 op TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
36 c39be742 2021-07-09 op static struct file {
37 c39be742 2021-07-09 op TAILQ_ENTRY(file) entry;
38 c39be742 2021-07-09 op FILE *stream;
39 c39be742 2021-07-09 op char *name;
40 c39be742 2021-07-09 op size_t ungetpos;
41 c39be742 2021-07-09 op size_t ungetsize;
42 c39be742 2021-07-09 op u_char *ungetbuf;
43 c39be742 2021-07-09 op int eof_reached;
44 c39be742 2021-07-09 op int lineno;
45 c39be742 2021-07-09 op int errors;
46 c39be742 2021-07-09 op } *file, *topfile;
47 74f0778b 2021-06-16 op
48 c39be742 2021-07-09 op struct file *pushfile(const char *, int);
49 c39be742 2021-07-09 op int popfile(void);
50 c39be742 2021-07-09 op int yyparse(void);
51 c39be742 2021-07-09 op int yylex(void);
52 c39be742 2021-07-09 op void yyerror(const char *, ...)
53 c39be742 2021-07-09 op __attribute__((__format__ (printf, 1, 2)))
54 c39be742 2021-07-09 op __attribute__((__nonnull__ (1)));
55 f3966209 2021-07-13 op void yywarn(const char *, ...)
56 f3966209 2021-07-13 op __attribute__((__format__ (printf, 1, 2)))
57 f3966209 2021-07-13 op __attribute__((__nonnull__ (1)));
58 c39be742 2021-07-09 op int kw_cmp(const void *, const void *);
59 c39be742 2021-07-09 op int lookup(char *);
60 c39be742 2021-07-09 op int igetc(void);
61 c39be742 2021-07-09 op int lgetc(int);
62 c39be742 2021-07-09 op void lungetc(int);
63 c39be742 2021-07-09 op int findeol(void);
64 ef129b08 2021-06-16 op
65 15902770 2021-01-15 op /*
66 15902770 2021-01-15 op * #define YYDEBUG 1
67 15902770 2021-01-15 op * int yydebug = 1;
68 15902770 2021-01-15 op */
69 15902770 2021-01-15 op
70 3b21cca3 2021-06-29 op TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
71 3b21cca3 2021-06-29 op struct sym {
72 3b21cca3 2021-06-29 op TAILQ_ENTRY(sym) entry;
73 3b21cca3 2021-06-29 op int used;
74 3b21cca3 2021-06-29 op int persist;
75 3b21cca3 2021-06-29 op char *name;
76 3b21cca3 2021-06-29 op char *val;
77 3b21cca3 2021-06-29 op };
78 3b21cca3 2021-06-29 op
79 c39be742 2021-07-09 op int symset(const char *, const char *, int);
80 c39be742 2021-07-09 op char *symget(const char *);
81 15902770 2021-01-15 op
82 c39be742 2021-07-09 op struct vhost *new_vhost(void);
83 c39be742 2021-07-09 op struct location *new_location(void);
84 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
85 6abda252 2021-02-06 op int check_block_code(int);
86 6abda252 2021-02-06 op char *check_block_fmt(char*);
87 6abda252 2021-02-06 op int check_strip_no(int);
88 391825e3 2021-07-09 op int check_port_num(int);
89 a709ddf5 2021-02-07 op int check_prefork_num(int);
90 49b73ba1 2021-02-10 op void advance_loc(void);
91 c705ecb1 2021-05-03 op void only_once(const void*, const char*);
92 8ad1c570 2021-05-09 op void only_oncei(int, const char*);
93 8ad1c570 2021-05-09 op int fastcgi_conf(char *, char *, char *);
94 c92b802b 2021-06-11 op void add_param(char *, char *, int);
95 13ed2fb6 2021-01-27 op
96 c39be742 2021-07-09 op static struct vhost *host;
97 c39be742 2021-07-09 op static struct location *loc;
98 c39be742 2021-07-09 op static int errors;
99 c39be742 2021-07-09 op
100 c39be742 2021-07-09 op typedef struct {
101 c39be742 2021-07-09 op union {
102 c39be742 2021-07-09 op char *string;
103 c39be742 2021-07-09 op int number;
104 c39be742 2021-07-09 op } v;
105 c39be742 2021-07-09 op int lineno;
106 c39be742 2021-07-09 op } YYSTYPE;
107 c39be742 2021-07-09 op
108 15902770 2021-01-15 op %}
109 15902770 2021-01-15 op
110 15902770 2021-01-15 op /* for bison: */
111 15902770 2021-01-15 op /* %define parse.error verbose */
112 15902770 2021-01-15 op
113 c74c7030 2021-07-19 op %token ALIAS AUTO
114 c74c7030 2021-07-19 op %token BLOCK
115 c74c7030 2021-07-19 op %token CA CERT CGI CHROOT CLIENT
116 c74c7030 2021-07-19 op %token DEFAULT
117 c74c7030 2021-07-19 op %token ENTRYPOINT ENV
118 abc8801d 2021-07-19 op %token FASTCGI
119 c74c7030 2021-07-19 op %token INCLUDE INDEX IPV6
120 c74c7030 2021-07-19 op %token KEY
121 c74c7030 2021-07-19 op %token LANG LOCATION LOG
122 c74c7030 2021-07-19 op %token MAP MIME
123 ff05125e 2021-10-15 op %token OCSP OFF ON
124 72b033ef 2021-12-29 op %token PARAM PORT PREFORK PROTOCOLS PROXY
125 72b033ef 2021-12-29 op %token RELAY_TO REQUIRE RETURN ROOT
126 c74c7030 2021-07-19 op %token SERVER SPAWN STRIP
127 c74c7030 2021-07-19 op %token TCP TOEXT TYPE USER
128 5128c0b0 2021-01-01 op %token VERIFYNAME
129 d06d6f4b 2021-04-29 op
130 c39be742 2021-07-09 op %token ERROR
131 7252049d 2021-06-29 op
132 c39be742 2021-07-09 op %token <v.string> STRING
133 c39be742 2021-07-09 op %token <v.number> NUM
134 15902770 2021-01-15 op
135 c39be742 2021-07-09 op %type <v.number> bool
136 c39be742 2021-07-09 op %type <v.string> string
137 98f52178 2021-06-29 op
138 15902770 2021-01-15 op %%
139 15902770 2021-01-15 op
140 6b86655a 2021-06-29 op conf : /* empty */
141 c39be742 2021-07-09 op | conf include '\n'
142 c39be742 2021-07-09 op | conf '\n'
143 c39be742 2021-07-09 op | conf varset '\n'
144 c39be742 2021-07-09 op | conf option '\n'
145 c39be742 2021-07-09 op | conf vhost '\n'
146 c39be742 2021-07-09 op | conf error '\n' { file->errors++; }
147 3b21cca3 2021-06-29 op ;
148 3b21cca3 2021-06-29 op
149 c39be742 2021-07-09 op include : INCLUDE STRING {
150 c39be742 2021-07-09 op struct file *nfile;
151 c39be742 2021-07-09 op
152 c39be742 2021-07-09 op if ((nfile = pushfile($2, 0)) == NULL) {
153 c39be742 2021-07-09 op yyerror("failed to include file %s", $2);
154 c39be742 2021-07-09 op free($2);
155 c39be742 2021-07-09 op YYERROR;
156 c39be742 2021-07-09 op }
157 c39be742 2021-07-09 op free($2);
158 c39be742 2021-07-09 op
159 c39be742 2021-07-09 op file = nfile;
160 c39be742 2021-07-09 op lungetc('\n');
161 c39be742 2021-07-09 op }
162 c39be742 2021-07-09 op ;
163 c39be742 2021-07-09 op
164 c74c7030 2021-07-19 op bool : ON { $$ = 1; }
165 c74c7030 2021-07-19 op | OFF { $$ = 0; }
166 c39be742 2021-07-09 op ;
167 c39be742 2021-07-09 op
168 c39be742 2021-07-09 op string : string STRING {
169 98f52178 2021-06-29 op if (asprintf(&$$, "%s%s", $1, $2) == -1) {
170 98f52178 2021-06-29 op free($1);
171 98f52178 2021-06-29 op free($2);
172 98f52178 2021-06-29 op yyerror("string: asprintf: %s", strerror(errno));
173 98f52178 2021-06-29 op YYERROR;
174 98f52178 2021-06-29 op }
175 98f52178 2021-06-29 op free($1);
176 98f52178 2021-06-29 op free($2);
177 98f52178 2021-06-29 op }
178 c39be742 2021-07-09 op | STRING
179 98f52178 2021-06-29 op ;
180 98f52178 2021-06-29 op
181 c39be742 2021-07-09 op varset : STRING '=' string {
182 3b21cca3 2021-06-29 op char *s = $1;
183 3b21cca3 2021-06-29 op while (*s++) {
184 c39be742 2021-07-09 op if (isspace((unsigned char)*s)) {
185 3b21cca3 2021-06-29 op yyerror("macro name cannot contain "
186 3b21cca3 2021-06-29 op "whitespaces");
187 3b21cca3 2021-06-29 op free($1);
188 3b21cca3 2021-06-29 op free($3);
189 3b21cca3 2021-06-29 op YYERROR;
190 3b21cca3 2021-06-29 op }
191 3b21cca3 2021-06-29 op }
192 3b21cca3 2021-06-29 op symset($1, $3, 0);
193 3b21cca3 2021-06-29 op free($1);
194 3b21cca3 2021-06-29 op free($3);
195 3b21cca3 2021-06-29 op }
196 15902770 2021-01-15 op ;
197 15902770 2021-01-15 op
198 c74c7030 2021-07-19 op option : CHROOT string { conf.chroot = $2; }
199 c74c7030 2021-07-19 op | IPV6 bool { conf.ipv6 = $2; }
200 c74c7030 2021-07-19 op | MIME STRING string {
201 f3966209 2021-07-13 op yywarn("`mime MIME EXT' is deprecated and will be "
202 f3966209 2021-07-13 op "removed in a future version, please use the new "
203 f3966209 2021-07-13 op "syntax: `map MIME to-ext EXT'");
204 d19951cf 2021-07-09 op add_mime(&conf.mime, $2, $3);
205 d19951cf 2021-07-09 op }
206 c74c7030 2021-07-19 op | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
207 c74c7030 2021-07-19 op | PORT NUM { conf.port = check_port_num($2); }
208 c74c7030 2021-07-19 op | PREFORK NUM { conf.prefork = check_prefork_num($2); }
209 c74c7030 2021-07-19 op | PROTOCOLS string {
210 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
211 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
212 3c4b712b 2021-01-01 op free($2);
213 5bc3c98e 2021-01-15 op }
214 c74c7030 2021-07-19 op | USER string { conf.user = $2; }
215 15902770 2021-01-15 op ;
216 15902770 2021-01-15 op
217 c74c7030 2021-07-19 op vhost : SERVER string {
218 b8e64ccd 2021-03-31 op host = new_vhost();
219 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
220 b8e64ccd 2021-03-31 op
221 b8e64ccd 2021-03-31 op loc = new_location();
222 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
223 b8e64ccd 2021-03-31 op
224 b8e64ccd 2021-03-31 op loc->match = xstrdup("*");
225 15902770 2021-01-15 op host->domain = $2;
226 15902770 2021-01-15 op
227 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
228 f3966209 2021-07-13 op yywarn("\"%s\" looks like punycode: you "
229 f3966209 2021-07-13 op "should use the decoded hostname", $2);
230 cbeee4ca 2021-01-28 op }
231 c39be742 2021-07-09 op } '{' optnl servopts locations '}' {
232 fdea6aa0 2021-04-30 op if (host->cert == NULL || host->key == NULL)
233 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
234 15902770 2021-01-15 op }
235 f3966209 2021-07-13 op | error '}' { yyerror("bad server directive"); }
236 15902770 2021-01-15 op ;
237 15902770 2021-01-15 op
238 15902770 2021-01-15 op servopts : /* empty */
239 c39be742 2021-07-09 op | servopts servopt optnl
240 15902770 2021-01-15 op ;
241 15902770 2021-01-15 op
242 c74c7030 2021-07-19 op servopt : ALIAS string {
243 cc8c2901 2021-04-29 op struct alist *a;
244 cc8c2901 2021-04-29 op
245 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
246 cc8c2901 2021-04-29 op a->alias = $2;
247 cc8c2901 2021-04-29 op if (TAILQ_EMPTY(&host->aliases))
248 cc8c2901 2021-04-29 op TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
249 cc8c2901 2021-04-29 op else
250 cc8c2901 2021-04-29 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
251 cc8c2901 2021-04-29 op }
252 c74c7030 2021-07-19 op | CERT string {
253 c705ecb1 2021-05-03 op only_once(host->cert, "cert");
254 c705ecb1 2021-05-03 op host->cert = ensure_absolute_path($2);
255 c705ecb1 2021-05-03 op }
256 c74c7030 2021-07-19 op | CGI string {
257 c705ecb1 2021-05-03 op only_once(host->cgi, "cgi");
258 15902770 2021-01-15 op /* drop the starting '/', if any */
259 709f4c94 2021-02-04 op if (*$2 == '/')
260 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
261 709f4c94 2021-02-04 op host->cgi = $2;
262 05c23a54 2021-01-19 op }
263 c74c7030 2021-07-19 op | ENTRYPOINT string {
264 c705ecb1 2021-05-03 op only_once(host->entrypoint, "entrypoint");
265 e3ddf390 2021-02-06 op while (*$2 == '/')
266 e3ddf390 2021-02-06 op memmove($2, $2+1, strlen($2));
267 e3ddf390 2021-02-06 op host->entrypoint = $2;
268 e3ddf390 2021-02-06 op }
269 c74c7030 2021-07-19 op | ENV string '=' string {
270 762b9b99 2021-07-09 op add_param($2, $4, 1);
271 9cc630aa 2021-04-28 op }
272 c74c7030 2021-07-19 op | KEY string {
273 c705ecb1 2021-05-03 op only_once(host->key, "key");
274 c705ecb1 2021-05-03 op host->key = ensure_absolute_path($2);
275 c92b802b 2021-06-11 op }
276 ff05125e 2021-10-15 op | OCSP string {
277 ff05125e 2021-10-15 op only_once(host->ocsp, "ocsp");
278 ff05125e 2021-10-15 op host->ocsp = ensure_absolute_path($2);
279 ff05125e 2021-10-15 op }
280 c74c7030 2021-07-19 op | PARAM string '=' string {
281 762b9b99 2021-07-09 op add_param($2, $4, 0);
282 c705ecb1 2021-05-03 op }
283 7bdcc91e 2021-01-01 op | proxy
284 c8b74339 2021-01-24 op | locopt
285 c8b74339 2021-01-24 op ;
286 7bdcc91e 2021-01-01 op
287 7bdcc91e 2021-01-01 op proxy : PROXY proxy_opt
288 7bdcc91e 2021-01-01 op | PROXY '{' optnl proxy_opts '}'
289 7bdcc91e 2021-01-01 op ;
290 7bdcc91e 2021-01-01 op
291 7bdcc91e 2021-01-01 op proxy_opts : /* empty */
292 7bdcc91e 2021-01-01 op | proxy_opts proxy_opt optnl
293 7bdcc91e 2021-01-01 op ;
294 7bdcc91e 2021-01-01 op
295 7bdcc91e 2021-01-01 op proxy_opt : CERT string {
296 7bdcc91e 2021-01-01 op struct proxy *p = &host->proxy;
297 c8b74339 2021-01-24 op
298 7bdcc91e 2021-01-01 op only_once(p->cert, "proxy cert");
299 7bdcc91e 2021-01-01 op ensure_absolute_path($2);
300 7bdcc91e 2021-01-01 op p->cert = tls_load_file($2, &p->certlen, NULL);
301 7bdcc91e 2021-01-01 op if (p->cert == NULL)
302 7bdcc91e 2021-01-01 op yyerror("can't load cert %s", $2);
303 3c4b712b 2021-01-01 op free($2);
304 7bdcc91e 2021-01-01 op }
305 7bdcc91e 2021-01-01 op | KEY string {
306 7bdcc91e 2021-01-01 op struct proxy *p = &host->proxy;
307 7bdcc91e 2021-01-01 op
308 7bdcc91e 2021-01-01 op only_once(p->key, "proxy key");
309 7bdcc91e 2021-01-01 op ensure_absolute_path($2);
310 7bdcc91e 2021-01-01 op p->key = tls_load_file($2, &p->keylen, NULL);
311 7bdcc91e 2021-01-01 op if (p->key == NULL)
312 7bdcc91e 2021-01-01 op yyerror("can't load key %s", $2);
313 3c4b712b 2021-01-01 op free($2);
314 c7c8ef44 2021-01-01 op }
315 c7c8ef44 2021-01-01 op | PROTOCOLS string {
316 c7c8ef44 2021-01-01 op struct proxy *p = &host->proxy;
317 c7c8ef44 2021-01-01 op
318 c7c8ef44 2021-01-01 op if (tls_config_parse_protocols(&p->protocols, $2) == -1)
319 c7c8ef44 2021-01-01 op yyerror("invalid protocols string \"%s\"", $2);
320 3c4b712b 2021-01-01 op free($2);
321 7bdcc91e 2021-01-01 op }
322 7bdcc91e 2021-01-01 op | RELAY_TO string {
323 7bdcc91e 2021-01-01 op char *at;
324 7bdcc91e 2021-01-01 op const char *errstr;
325 7bdcc91e 2021-01-01 op struct proxy *p = &host->proxy;
326 7bdcc91e 2021-01-01 op
327 7bdcc91e 2021-01-01 op only_once(p->host, "proxy relay-to");
328 7bdcc91e 2021-01-01 op p->host = $2;
329 7bdcc91e 2021-01-01 op
330 7bdcc91e 2021-01-01 op if ((at = strchr($2, ':')) != NULL) {
331 7bdcc91e 2021-01-01 op *at++ = '\0';
332 7bdcc91e 2021-01-01 op p->port = at;
333 7bdcc91e 2021-01-01 op } else
334 7bdcc91e 2021-01-01 op p->port = "1965";
335 7bdcc91e 2021-01-01 op
336 7bdcc91e 2021-01-01 op strtonum(p->port, 1, UINT16_MAX, &errstr);
337 7bdcc91e 2021-01-01 op if (errstr != NULL)
338 7bdcc91e 2021-01-01 op yyerror("proxy port is %s: %s", errstr,
339 7bdcc91e 2021-01-01 op p->port);
340 7bdcc91e 2021-01-01 op }
341 5128c0b0 2021-01-01 op | VERIFYNAME bool {
342 5128c0b0 2021-01-01 op host->proxy.noverifyname = !$2;
343 5128c0b0 2021-01-01 op }
344 7bdcc91e 2021-01-01 op ;
345 7bdcc91e 2021-01-01 op
346 c8b74339 2021-01-24 op locations : /* empty */
347 c39be742 2021-07-09 op | locations location optnl
348 c8b74339 2021-01-24 op ;
349 c8b74339 2021-01-24 op
350 c74c7030 2021-07-19 op location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
351 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
352 49b73ba1 2021-02-10 op if (*$3 == '/')
353 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
354 49b73ba1 2021-02-10 op loc->match = $3;
355 6119e13e 2021-01-19 op }
356 c8b74339 2021-01-24 op | error '}'
357 c8b74339 2021-01-24 op ;
358 c8b74339 2021-01-24 op
359 c8b74339 2021-01-24 op locopts : /* empty */
360 c39be742 2021-07-09 op | locopts locopt optnl
361 c8b74339 2021-01-24 op ;
362 c8b74339 2021-01-24 op
363 c74c7030 2021-07-19 op locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
364 c74c7030 2021-07-19 op | BLOCK RETURN NUM string {
365 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
366 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
367 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
368 6abda252 2021-02-06 op }
369 c74c7030 2021-07-19 op | BLOCK RETURN NUM {
370 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
371 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
372 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
373 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
374 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
375 6abda252 2021-02-06 op }
376 c74c7030 2021-07-19 op | BLOCK {
377 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
378 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
379 6abda252 2021-02-06 op loc->block_code = 40;
380 6abda252 2021-02-06 op }
381 c74c7030 2021-07-19 op | DEFAULT TYPE string {
382 c705ecb1 2021-05-03 op only_once(loc->default_mime, "default type");
383 eb59f87e 2021-02-09 op loc->default_mime = $3;
384 eb59f87e 2021-02-09 op }
385 abc8801d 2021-07-19 op | FASTCGI fastcgi
386 c74c7030 2021-07-19 op | INDEX string {
387 c705ecb1 2021-05-03 op only_once(loc->index, "index");
388 eb59f87e 2021-02-09 op loc->index = $2;
389 eb59f87e 2021-02-09 op }
390 c74c7030 2021-07-19 op | LANG string {
391 c705ecb1 2021-05-03 op only_once(loc->lang, "lang");
392 eb59f87e 2021-02-09 op loc->lang = $2;
393 eb59f87e 2021-02-09 op }
394 c74c7030 2021-07-19 op | LOG bool { loc->disable_log = !$2; }
395 da2185f3 2021-01-01 op | REQUIRE CLIENT CA string {
396 da2185f3 2021-01-01 op only_once(loc->reqca, "require client ca");
397 da2185f3 2021-01-01 op ensure_absolute_path($4);
398 da2185f3 2021-01-01 op if ((loc->reqca = load_ca($4)) == NULL)
399 da2185f3 2021-01-01 op yyerror("couldn't load ca cert: %s", $4);
400 da2185f3 2021-01-01 op free($4);
401 da2185f3 2021-01-01 op }
402 da2185f3 2021-01-01 op | ROOT string {
403 da2185f3 2021-01-01 op only_once(loc->dir, "root");
404 da2185f3 2021-01-01 op loc->dir = ensure_absolute_path($2);
405 da2185f3 2021-01-01 op }
406 da2185f3 2021-01-01 op | STRIP NUM { loc->strip = check_strip_no($2); }
407 da2185f3 2021-01-01 op ;
408 da2185f3 2021-01-01 op
409 c74c7030 2021-07-19 op fastcgi : SPAWN string {
410 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
411 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf(NULL, NULL, $2);
412 0d047efc 2021-05-24 op }
413 98f52178 2021-06-29 op | string {
414 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
415 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($1, NULL, NULL);
416 0d047efc 2021-05-24 op }
417 c74c7030 2021-07-19 op | TCP string PORT NUM {
418 0d047efc 2021-05-24 op char *c;
419 762b9b99 2021-07-09 op if (asprintf(&c, "%d", $4) == -1)
420 0d047efc 2021-05-24 op err(1, "asprintf");
421 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
422 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, c, NULL);
423 0d047efc 2021-05-24 op }
424 c74c7030 2021-07-19 op | TCP string {
425 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
426 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
427 0d047efc 2021-05-24 op }
428 c74c7030 2021-07-19 op | TCP string PORT string {
429 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
430 762b9b99 2021-07-09 op loc->fcgi = fastcgi_conf($2, $4, NULL);
431 0d047efc 2021-05-24 op }
432 0d047efc 2021-05-24 op ;
433 0d047efc 2021-05-24 op
434 c39be742 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
435 67f49405 2021-07-09 op | ';' optnl /* semicolons too */
436 c39be742 2021-07-09 op | /*empty*/
437 c39be742 2021-07-09 op ;
438 fdea6aa0 2021-04-30 op
439 c39be742 2021-07-09 op %%
440 b8e64ccd 2021-03-31 op
441 74f0778b 2021-06-16 op static struct keyword {
442 74f0778b 2021-06-16 op const char *word;
443 74f0778b 2021-06-16 op int token;
444 74f0778b 2021-06-16 op } keywords[] = {
445 d93c8191 2021-07-09 op /* these MUST be sorted */
446 c74c7030 2021-07-19 op {"alias", ALIAS},
447 c74c7030 2021-07-19 op {"auto", AUTO},
448 c74c7030 2021-07-19 op {"block", BLOCK},
449 c74c7030 2021-07-19 op {"ca", CA},
450 c74c7030 2021-07-19 op {"cert", CERT},
451 c74c7030 2021-07-19 op {"cgi", CGI},
452 c74c7030 2021-07-19 op {"chroot", CHROOT},
453 c74c7030 2021-07-19 op {"client", CLIENT},
454 c74c7030 2021-07-19 op {"default", DEFAULT},
455 c74c7030 2021-07-19 op {"entrypoint", ENTRYPOINT},
456 c74c7030 2021-07-19 op {"env", ENV},
457 abc8801d 2021-07-19 op {"fastcgi", FASTCGI},
458 c74c7030 2021-07-19 op {"index", INDEX},
459 c74c7030 2021-07-19 op {"ipv6", IPV6},
460 c74c7030 2021-07-19 op {"key", KEY},
461 c74c7030 2021-07-19 op {"lang", LANG},
462 c74c7030 2021-07-19 op {"location", LOCATION},
463 c74c7030 2021-07-19 op {"log", LOG},
464 c74c7030 2021-07-19 op {"map", MAP},
465 c74c7030 2021-07-19 op {"mime", MIME},
466 ff05125e 2021-10-15 op {"ocsp", OCSP},
467 c74c7030 2021-07-19 op {"off", OFF},
468 c74c7030 2021-07-19 op {"on", ON},
469 c74c7030 2021-07-19 op {"param", PARAM},
470 c74c7030 2021-07-19 op {"port", PORT},
471 c74c7030 2021-07-19 op {"prefork", PREFORK},
472 c74c7030 2021-07-19 op {"protocols", PROTOCOLS},
473 72b033ef 2021-12-29 op {"proxy", PROXY},
474 72b033ef 2021-12-29 op {"relay-to", RELAY_TO},
475 c74c7030 2021-07-19 op {"require", REQUIRE},
476 c74c7030 2021-07-19 op {"return", RETURN},
477 c74c7030 2021-07-19 op {"root", ROOT},
478 c74c7030 2021-07-19 op {"server", SERVER},
479 c74c7030 2021-07-19 op {"spawn", SPAWN},
480 c74c7030 2021-07-19 op {"strip", STRIP},
481 c74c7030 2021-07-19 op {"tcp", TCP},
482 c74c7030 2021-07-19 op {"to-ext", TOEXT},
483 c74c7030 2021-07-19 op {"type", TYPE},
484 c74c7030 2021-07-19 op {"user", USER},
485 5128c0b0 2021-01-01 op {"verifyname", VERIFYNAME},
486 74f0778b 2021-06-16 op };
487 74f0778b 2021-06-16 op
488 c39be742 2021-07-09 op void
489 c39be742 2021-07-09 op yyerror(const char *msg, ...)
490 c39be742 2021-07-09 op {
491 c39be742 2021-07-09 op va_list ap;
492 c39be742 2021-07-09 op
493 c39be742 2021-07-09 op file->errors++;
494 c39be742 2021-07-09 op
495 c39be742 2021-07-09 op va_start(ap, msg);
496 f3966209 2021-07-13 op fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
497 f3966209 2021-07-13 op vfprintf(stderr, msg, ap);
498 f3966209 2021-07-13 op fprintf(stderr, "\n");
499 f3966209 2021-07-13 op va_end(ap);
500 f3966209 2021-07-13 op }
501 f3966209 2021-07-13 op
502 f3966209 2021-07-13 op void
503 f3966209 2021-07-13 op yywarn(const char *msg, ...)
504 f3966209 2021-07-13 op {
505 f3966209 2021-07-13 op va_list ap;
506 f3966209 2021-07-13 op
507 f3966209 2021-07-13 op va_start(ap, msg);
508 f3966209 2021-07-13 op fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
509 c39be742 2021-07-09 op vfprintf(stderr, msg, ap);
510 c39be742 2021-07-09 op fprintf(stderr, "\n");
511 c39be742 2021-07-09 op va_end(ap);
512 c39be742 2021-07-09 op }
513 c39be742 2021-07-09 op
514 d93c8191 2021-07-09 op int
515 d93c8191 2021-07-09 op kw_cmp(const void *k, const void *e)
516 d93c8191 2021-07-09 op {
517 d93c8191 2021-07-09 op return strcmp(k, ((struct keyword *)e)->word);
518 d93c8191 2021-07-09 op }
519 d93c8191 2021-07-09 op
520 c39be742 2021-07-09 op int
521 c39be742 2021-07-09 op lookup(char *s)
522 74f0778b 2021-06-16 op {
523 c39be742 2021-07-09 op const struct keyword *p;
524 74f0778b 2021-06-16 op
525 c39be742 2021-07-09 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
526 c39be742 2021-07-09 op sizeof(keywords[0]), kw_cmp);
527 74f0778b 2021-06-16 op
528 c39be742 2021-07-09 op if (p)
529 c39be742 2021-07-09 op return p->token;
530 c39be742 2021-07-09 op else
531 c39be742 2021-07-09 op return STRING;
532 c39be742 2021-07-09 op }
533 c39be742 2021-07-09 op
534 c39be742 2021-07-09 op #define START_EXPAND 1
535 c39be742 2021-07-09 op #define DONE_EXPAND 2
536 c39be742 2021-07-09 op
537 c39be742 2021-07-09 op static int expanding;
538 c39be742 2021-07-09 op
539 c39be742 2021-07-09 op int
540 c39be742 2021-07-09 op igetc(void)
541 c39be742 2021-07-09 op {
542 c39be742 2021-07-09 op int c;
543 c39be742 2021-07-09 op
544 c39be742 2021-07-09 op while (1) {
545 c39be742 2021-07-09 op if (file->ungetpos > 0)
546 c39be742 2021-07-09 op c = file->ungetbuf[--file->ungetpos];
547 c39be742 2021-07-09 op else
548 c39be742 2021-07-09 op c = getc(file->stream);
549 c39be742 2021-07-09 op
550 c39be742 2021-07-09 op if (c == START_EXPAND)
551 c39be742 2021-07-09 op expanding = 1;
552 c39be742 2021-07-09 op else if (c == DONE_EXPAND)
553 c39be742 2021-07-09 op expanding = 0;
554 c39be742 2021-07-09 op else
555 c39be742 2021-07-09 op break;
556 74f0778b 2021-06-16 op }
557 c39be742 2021-07-09 op return c;
558 c39be742 2021-07-09 op }
559 74f0778b 2021-06-16 op
560 c39be742 2021-07-09 op int
561 c39be742 2021-07-09 op lgetc(int quotec)
562 c39be742 2021-07-09 op {
563 c39be742 2021-07-09 op int c, next;
564 c39be742 2021-07-09 op
565 c39be742 2021-07-09 op if (quotec) {
566 c39be742 2021-07-09 op if ((c = igetc()) == EOF) {
567 c39be742 2021-07-09 op yyerror("reached end of file while parsing "
568 c39be742 2021-07-09 op "quoted string");
569 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
570 c39be742 2021-07-09 op return EOF;
571 c39be742 2021-07-09 op return quotec;
572 c39be742 2021-07-09 op }
573 74f0778b 2021-06-16 op return c;
574 74f0778b 2021-06-16 op }
575 74f0778b 2021-06-16 op
576 c39be742 2021-07-09 op while ((c = igetc()) == '\\') {
577 c39be742 2021-07-09 op next = igetc();
578 c39be742 2021-07-09 op if (next != '\n') {
579 c39be742 2021-07-09 op c = next;
580 74f0778b 2021-06-16 op break;
581 c39be742 2021-07-09 op }
582 c39be742 2021-07-09 op yylval.lineno = file->lineno;
583 c39be742 2021-07-09 op file->lineno++;
584 c39be742 2021-07-09 op }
585 3b21cca3 2021-06-29 op
586 c39be742 2021-07-09 op if (c == EOF) {
587 c39be742 2021-07-09 op /*
588 c39be742 2021-07-09 op * Fake EOL when hit EOF for the first time. This gets line
589 c39be742 2021-07-09 op * count right if last line in included file is syntactically
590 c39be742 2021-07-09 op * invalid and has no newline.
591 c39be742 2021-07-09 op */
592 c39be742 2021-07-09 op if (file->eof_reached == 0) {
593 c39be742 2021-07-09 op file->eof_reached = 1;
594 c39be742 2021-07-09 op return '\n';
595 c39be742 2021-07-09 op }
596 c39be742 2021-07-09 op while (c == EOF) {
597 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
598 c39be742 2021-07-09 op return EOF;
599 c39be742 2021-07-09 op c = igetc();
600 c39be742 2021-07-09 op }
601 c39be742 2021-07-09 op }
602 c39be742 2021-07-09 op return c;
603 c39be742 2021-07-09 op }
604 c39be742 2021-07-09 op
605 c39be742 2021-07-09 op void
606 c39be742 2021-07-09 op lungetc(int c)
607 c39be742 2021-07-09 op {
608 c39be742 2021-07-09 op if (c == EOF)
609 c39be742 2021-07-09 op return;
610 c39be742 2021-07-09 op
611 c39be742 2021-07-09 op if (file->ungetpos >= file->ungetsize) {
612 c39be742 2021-07-09 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
613 c39be742 2021-07-09 op if (p == NULL)
614 c39be742 2021-07-09 op err(1, "lungetc");
615 c39be742 2021-07-09 op file->ungetbuf = p;
616 c39be742 2021-07-09 op file->ungetsize *= 2;
617 c39be742 2021-07-09 op }
618 c39be742 2021-07-09 op file->ungetbuf[file->ungetpos++] = c;
619 c39be742 2021-07-09 op }
620 c39be742 2021-07-09 op
621 c39be742 2021-07-09 op int
622 c39be742 2021-07-09 op findeol(void)
623 c39be742 2021-07-09 op {
624 c39be742 2021-07-09 op int c;
625 c39be742 2021-07-09 op
626 c39be742 2021-07-09 op /* Skip to either EOF or the first real EOL. */
627 c39be742 2021-07-09 op while (1) {
628 c39be742 2021-07-09 op c = lgetc(0);
629 c39be742 2021-07-09 op if (c == '\n') {
630 c39be742 2021-07-09 op file->lineno++;
631 3b21cca3 2021-06-29 op break;
632 c39be742 2021-07-09 op }
633 c39be742 2021-07-09 op if (c == EOF)
634 74f0778b 2021-06-16 op break;
635 c39be742 2021-07-09 op }
636 c39be742 2021-07-09 op return ERROR;
637 c39be742 2021-07-09 op }
638 c39be742 2021-07-09 op
639 c39be742 2021-07-09 op int
640 c39be742 2021-07-09 op yylex(void)
641 c39be742 2021-07-09 op {
642 1bd706dc 2021-07-09 op char buf[8096];
643 1bd706dc 2021-07-09 op char *p, *val;
644 1bd706dc 2021-07-09 op int quotec, next, c;
645 1bd706dc 2021-07-09 op int token;
646 c39be742 2021-07-09 op
647 c39be742 2021-07-09 op top:
648 c39be742 2021-07-09 op p = buf;
649 c39be742 2021-07-09 op while ((c = lgetc(0)) == ' ' || c == '\t')
650 c39be742 2021-07-09 op ; /* nothing */
651 c39be742 2021-07-09 op
652 c39be742 2021-07-09 op yylval.lineno = file->lineno;
653 c39be742 2021-07-09 op if (c == '#')
654 c39be742 2021-07-09 op while ((c = lgetc(0)) != '\n' && c != EOF)
655 c39be742 2021-07-09 op ; /* nothing */
656 c39be742 2021-07-09 op if (c == '$' && !expanding) {
657 c39be742 2021-07-09 op while (1) {
658 c39be742 2021-07-09 op if ((c = lgetc(0)) == EOF)
659 c39be742 2021-07-09 op return 0;
660 67f49405 2021-07-09 op if (p + 1 >= buf + sizeof(buf) -1) {
661 67f49405 2021-07-09 op yyerror("string too long");
662 67f49405 2021-07-09 op return findeol();
663 67f49405 2021-07-09 op }
664 67f49405 2021-07-09 op if (isalnum(c) || c == '_') {
665 67f49405 2021-07-09 op *p++ = c;
666 67f49405 2021-07-09 op continue;
667 67f49405 2021-07-09 op }
668 67f49405 2021-07-09 op *p = '\0';
669 67f49405 2021-07-09 op lungetc(c);
670 67f49405 2021-07-09 op break;
671 67f49405 2021-07-09 op }
672 67f49405 2021-07-09 op val = symget(buf);
673 67f49405 2021-07-09 op if (val == NULL) {
674 67f49405 2021-07-09 op yyerror("macro `%s' not defined", buf);
675 67f49405 2021-07-09 op return findeol();
676 67f49405 2021-07-09 op }
677 67f49405 2021-07-09 op yylval.v.string = xstrdup(val);
678 67f49405 2021-07-09 op return STRING;
679 67f49405 2021-07-09 op }
680 67f49405 2021-07-09 op if (c == '@' && !expanding) {
681 67f49405 2021-07-09 op while (1) {
682 67f49405 2021-07-09 op if ((c = lgetc(0)) == EOF)
683 67f49405 2021-07-09 op return 0;
684 c39be742 2021-07-09 op
685 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
686 c39be742 2021-07-09 op yyerror("string too long");
687 c39be742 2021-07-09 op return findeol();
688 c39be742 2021-07-09 op }
689 c39be742 2021-07-09 op if (isalnum(c) || c == '_') {
690 c39be742 2021-07-09 op *p++ = c;
691 74f0778b 2021-06-16 op continue;
692 74f0778b 2021-06-16 op }
693 c39be742 2021-07-09 op *p = '\0';
694 c39be742 2021-07-09 op lungetc(c);
695 c39be742 2021-07-09 op break;
696 74f0778b 2021-06-16 op }
697 c39be742 2021-07-09 op val = symget(buf);
698 c39be742 2021-07-09 op if (val == NULL) {
699 c39be742 2021-07-09 op yyerror("macro '%s' not defined", buf);
700 c39be742 2021-07-09 op return findeol();
701 74f0778b 2021-06-16 op }
702 c39be742 2021-07-09 op p = val + strlen(val) - 1;
703 c39be742 2021-07-09 op lungetc(DONE_EXPAND);
704 c39be742 2021-07-09 op while (p >= val) {
705 c39be742 2021-07-09 op lungetc(*p);
706 c39be742 2021-07-09 op p--;
707 c39be742 2021-07-09 op }
708 c39be742 2021-07-09 op lungetc(START_EXPAND);
709 c39be742 2021-07-09 op goto top;
710 74f0778b 2021-06-16 op }
711 74f0778b 2021-06-16 op
712 c39be742 2021-07-09 op switch (c) {
713 c39be742 2021-07-09 op case '\'':
714 c39be742 2021-07-09 op case '"':
715 c39be742 2021-07-09 op quotec = c;
716 c39be742 2021-07-09 op while (1) {
717 c39be742 2021-07-09 op if ((c = lgetc(quotec)) == EOF)
718 c39be742 2021-07-09 op return 0;
719 c39be742 2021-07-09 op if (c == '\n') {
720 c39be742 2021-07-09 op file->lineno++;
721 c39be742 2021-07-09 op continue;
722 c39be742 2021-07-09 op } else if (c == '\\') {
723 c39be742 2021-07-09 op if ((next = lgetc(quotec)) == EOF)
724 c39be742 2021-07-09 op return (0);
725 c39be742 2021-07-09 op if (next == quotec || next == ' ' ||
726 c39be742 2021-07-09 op next == '\t')
727 c39be742 2021-07-09 op c = next;
728 c39be742 2021-07-09 op else if (next == '\n') {
729 c39be742 2021-07-09 op file->lineno++;
730 c39be742 2021-07-09 op continue;
731 c39be742 2021-07-09 op } else
732 c39be742 2021-07-09 op lungetc(next);
733 c39be742 2021-07-09 op } else if (c == quotec) {
734 c39be742 2021-07-09 op *p = '\0';
735 c39be742 2021-07-09 op break;
736 c39be742 2021-07-09 op } else if (c == '\0') {
737 f3966209 2021-07-13 op yyerror("invalid syntax");
738 c39be742 2021-07-09 op return findeol();
739 c39be742 2021-07-09 op }
740 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
741 c39be742 2021-07-09 op yyerror("string too long");
742 c39be742 2021-07-09 op return findeol();
743 c39be742 2021-07-09 op }
744 c39be742 2021-07-09 op *p++ = c;
745 c39be742 2021-07-09 op }
746 c39be742 2021-07-09 op yylval.v.string = strdup(buf);
747 c39be742 2021-07-09 op if (yylval.v.string == NULL)
748 c39be742 2021-07-09 op err(1, "yylex: strdup");
749 c39be742 2021-07-09 op return STRING;
750 74f0778b 2021-06-16 op }
751 c39be742 2021-07-09 op
752 c39be742 2021-07-09 op #define allowed_to_end_number(x) \
753 c39be742 2021-07-09 op (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
754 c39be742 2021-07-09 op
755 c39be742 2021-07-09 op if (c == '-' || isdigit(c)) {
756 c39be742 2021-07-09 op do {
757 c39be742 2021-07-09 op *p++ = c;
758 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
759 c39be742 2021-07-09 op yyerror("string too long");
760 c39be742 2021-07-09 op return findeol();
761 c39be742 2021-07-09 op }
762 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && isdigit(c));
763 c39be742 2021-07-09 op lungetc(c);
764 c39be742 2021-07-09 op if (p == buf + 1 && buf[0] == '-')
765 c39be742 2021-07-09 op goto nodigits;
766 c39be742 2021-07-09 op if (c == EOF || allowed_to_end_number(c)) {
767 c39be742 2021-07-09 op const char *errstr = NULL;
768 c39be742 2021-07-09 op
769 c39be742 2021-07-09 op *p = '\0';
770 c39be742 2021-07-09 op yylval.v.number = strtonum(buf, LLONG_MIN,
771 c39be742 2021-07-09 op LLONG_MAX, &errstr);
772 c39be742 2021-07-09 op if (errstr) {
773 c39be742 2021-07-09 op yyerror("\"%s\" invalid number: %s",
774 c39be742 2021-07-09 op buf, errstr);
775 c39be742 2021-07-09 op return findeol();
776 c39be742 2021-07-09 op }
777 c39be742 2021-07-09 op return NUM;
778 c39be742 2021-07-09 op } else {
779 c39be742 2021-07-09 op nodigits:
780 c39be742 2021-07-09 op while (p > buf + 1)
781 c39be742 2021-07-09 op lungetc(*--p);
782 c39be742 2021-07-09 op c = *--p;
783 c39be742 2021-07-09 op if (c == '-')
784 c39be742 2021-07-09 op return c;
785 c39be742 2021-07-09 op }
786 74f0778b 2021-06-16 op }
787 c39be742 2021-07-09 op
788 c39be742 2021-07-09 op #define allowed_in_string(x) \
789 c39be742 2021-07-09 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
790 c39be742 2021-07-09 op x != '{' && x != '}' && \
791 c39be742 2021-07-09 op x != '!' && x != '=' && x != '#' && \
792 67f49405 2021-07-09 op x != ',' && x != ';'))
793 c39be742 2021-07-09 op
794 67f49405 2021-07-09 op if (isalnum(c) || c == ':' || c == '_') {
795 c39be742 2021-07-09 op do {
796 c39be742 2021-07-09 op *p++ = c;
797 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
798 c39be742 2021-07-09 op yyerror("string too long");
799 c39be742 2021-07-09 op return findeol();
800 c39be742 2021-07-09 op }
801 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
802 c39be742 2021-07-09 op lungetc(c);
803 c39be742 2021-07-09 op *p = '\0';
804 c39be742 2021-07-09 op if ((token = lookup(buf)) == STRING)
805 c39be742 2021-07-09 op yylval.v.string = xstrdup(buf);
806 c39be742 2021-07-09 op return token;
807 74f0778b 2021-06-16 op }
808 c39be742 2021-07-09 op if (c == '\n') {
809 c39be742 2021-07-09 op yylval.lineno = file->lineno;
810 c39be742 2021-07-09 op file->lineno++;
811 74f0778b 2021-06-16 op }
812 c39be742 2021-07-09 op if (c == EOF)
813 c39be742 2021-07-09 op return 0;
814 c39be742 2021-07-09 op return c;
815 c39be742 2021-07-09 op }
816 c39be742 2021-07-09 op
817 c39be742 2021-07-09 op struct file *
818 c39be742 2021-07-09 op pushfile(const char *name, int secret)
819 c39be742 2021-07-09 op {
820 c39be742 2021-07-09 op struct file *nfile;
821 c39be742 2021-07-09 op
822 c39be742 2021-07-09 op nfile = xcalloc(1, sizeof(*nfile));
823 c39be742 2021-07-09 op nfile->name = xstrdup(name);
824 c39be742 2021-07-09 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
825 83272dfe 2021-08-23 op log_warn(NULL, "can't open %s: %s", nfile->name,
826 c39be742 2021-07-09 op strerror(errno));
827 c39be742 2021-07-09 op free(nfile->name);
828 c39be742 2021-07-09 op free(nfile);
829 c39be742 2021-07-09 op return NULL;
830 74f0778b 2021-06-16 op }
831 c39be742 2021-07-09 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
832 c39be742 2021-07-09 op nfile->ungetsize = 16;
833 c39be742 2021-07-09 op nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
834 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&files, nfile, entry);
835 c39be742 2021-07-09 op return nfile;
836 c39be742 2021-07-09 op }
837 74f0778b 2021-06-16 op
838 c39be742 2021-07-09 op int
839 c39be742 2021-07-09 op popfile(void)
840 c39be742 2021-07-09 op {
841 c39be742 2021-07-09 op struct file *prev;
842 13ed2fb6 2021-01-27 op
843 c39be742 2021-07-09 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
844 c39be742 2021-07-09 op prev->errors += file->errors;
845 13ed2fb6 2021-01-27 op
846 c39be742 2021-07-09 op TAILQ_REMOVE(&files, file, entry);
847 c39be742 2021-07-09 op fclose(file->stream);
848 c39be742 2021-07-09 op free(file->name);
849 c39be742 2021-07-09 op free(file->ungetbuf);
850 c39be742 2021-07-09 op free(file);
851 c39be742 2021-07-09 op file = prev;
852 c39be742 2021-07-09 op return file ? 0 : EOF;
853 13ed2fb6 2021-01-27 op }
854 13ed2fb6 2021-01-27 op
855 13ed2fb6 2021-01-27 op void
856 c39be742 2021-07-09 op parse_conf(const char *filename)
857 13ed2fb6 2021-01-27 op {
858 c39be742 2021-07-09 op struct sym *sym, *next;
859 3b21cca3 2021-06-29 op
860 c39be742 2021-07-09 op file = pushfile(filename, 0);
861 c39be742 2021-07-09 op if (file == NULL)
862 83272dfe 2021-08-23 op exit(1);
863 c39be742 2021-07-09 op topfile = file;
864 c39be742 2021-07-09 op
865 13ed2fb6 2021-01-27 op yyparse();
866 c39be742 2021-07-09 op errors = file->errors;
867 c39be742 2021-07-09 op popfile();
868 13ed2fb6 2021-01-27 op
869 c39be742 2021-07-09 op /* Free macros and check which have not been used. */
870 3b21cca3 2021-06-29 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
871 3b21cca3 2021-06-29 op /* TODO: warn if !sym->used */
872 3b21cca3 2021-06-29 op if (!sym->persist) {
873 3b21cca3 2021-06-29 op free(sym->name);
874 3b21cca3 2021-06-29 op free(sym->val);
875 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
876 3b21cca3 2021-06-29 op free(sym);
877 3b21cca3 2021-06-29 op }
878 3b21cca3 2021-06-29 op }
879 c39be742 2021-07-09 op
880 c39be742 2021-07-09 op if (errors)
881 c39be742 2021-07-09 op exit(1);
882 13ed2fb6 2021-01-27 op }
883 e17642a7 2021-02-01 op
884 f0a01fc7 2021-10-09 op void
885 f0a01fc7 2021-10-09 op print_conf(void)
886 f0a01fc7 2021-10-09 op {
887 f0a01fc7 2021-10-09 op struct vhost *h;
888 f0a01fc7 2021-10-09 op /* struct location *l; */
889 f0a01fc7 2021-10-09 op /* struct envlist *e; */
890 f0a01fc7 2021-10-09 op /* struct alist *a; */
891 f0a01fc7 2021-10-09 op
892 f0a01fc7 2021-10-09 op if (conf.chroot != NULL)
893 f0a01fc7 2021-10-09 op printf("chroot \"%s\"\n", conf.chroot);
894 f0a01fc7 2021-10-09 op printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
895 f0a01fc7 2021-10-09 op /* XXX: defined mimes? */
896 f0a01fc7 2021-10-09 op printf("port %d\n", conf.port);
897 f0a01fc7 2021-10-09 op printf("prefork %d\n", conf.prefork);
898 f0a01fc7 2021-10-09 op /* XXX: protocols? */
899 f0a01fc7 2021-10-09 op if (conf.user != NULL)
900 f0a01fc7 2021-10-09 op printf("user \"%s\"\n", conf.user);
901 f0a01fc7 2021-10-09 op
902 f0a01fc7 2021-10-09 op TAILQ_FOREACH(h, &hosts, vhosts) {
903 f0a01fc7 2021-10-09 op printf("\nserver \"%s\" {\n", h->domain);
904 f0a01fc7 2021-10-09 op printf(" cert \"%s\"\n", h->cert);
905 f0a01fc7 2021-10-09 op printf(" key \"%s\"\n", h->key);
906 f0a01fc7 2021-10-09 op /* TODO: print locations... */
907 f0a01fc7 2021-10-09 op printf("}\n");
908 f0a01fc7 2021-10-09 op }
909 f0a01fc7 2021-10-09 op }
910 f0a01fc7 2021-10-09 op
911 c39be742 2021-07-09 op int
912 c39be742 2021-07-09 op symset(const char *name, const char *val, int persist)
913 c39be742 2021-07-09 op {
914 c39be742 2021-07-09 op struct sym *sym;
915 c39be742 2021-07-09 op
916 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
917 c39be742 2021-07-09 op if (!strcmp(name, sym->name))
918 c39be742 2021-07-09 op break;
919 c39be742 2021-07-09 op }
920 c39be742 2021-07-09 op
921 c39be742 2021-07-09 op if (sym != NULL) {
922 c39be742 2021-07-09 op if (sym->persist)
923 c39be742 2021-07-09 op return 0;
924 c39be742 2021-07-09 op else {
925 c39be742 2021-07-09 op free(sym->name);
926 c39be742 2021-07-09 op free(sym->val);
927 c39be742 2021-07-09 op TAILQ_REMOVE(&symhead, sym, entry);
928 c39be742 2021-07-09 op free(sym);
929 c39be742 2021-07-09 op }
930 c39be742 2021-07-09 op }
931 c39be742 2021-07-09 op
932 c39be742 2021-07-09 op sym = xcalloc(1, sizeof(*sym));
933 c39be742 2021-07-09 op sym->name = xstrdup(name);
934 c39be742 2021-07-09 op sym->val = xstrdup(val);
935 c39be742 2021-07-09 op sym->used = 0;
936 c39be742 2021-07-09 op sym->persist = persist;
937 c39be742 2021-07-09 op
938 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
939 c39be742 2021-07-09 op return 0;
940 c39be742 2021-07-09 op }
941 c39be742 2021-07-09 op
942 c39be742 2021-07-09 op int
943 c39be742 2021-07-09 op cmdline_symset(char *s)
944 c39be742 2021-07-09 op {
945 c39be742 2021-07-09 op char *sym, *val;
946 c39be742 2021-07-09 op int ret;
947 c39be742 2021-07-09 op
948 c39be742 2021-07-09 op if ((val = strrchr(s, '=')) == NULL)
949 c39be742 2021-07-09 op return -1;
950 c39be742 2021-07-09 op sym = xcalloc(1, val - s + 1);
951 c39be742 2021-07-09 op memcpy(sym, s, val - s);
952 c39be742 2021-07-09 op ret = symset(sym, val + 1, 1);
953 c39be742 2021-07-09 op free(sym);
954 c39be742 2021-07-09 op return ret;
955 c39be742 2021-07-09 op }
956 c39be742 2021-07-09 op
957 e17642a7 2021-02-01 op char *
958 c39be742 2021-07-09 op symget(const char *nam)
959 c39be742 2021-07-09 op {
960 c39be742 2021-07-09 op struct sym *sym;
961 c39be742 2021-07-09 op
962 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
963 c39be742 2021-07-09 op if (strcmp(nam, sym->name) == 0) {
964 c39be742 2021-07-09 op sym->used = 1;
965 c39be742 2021-07-09 op return sym->val;
966 c39be742 2021-07-09 op }
967 c39be742 2021-07-09 op }
968 c39be742 2021-07-09 op return NULL;
969 c39be742 2021-07-09 op }
970 c39be742 2021-07-09 op
971 c39be742 2021-07-09 op struct vhost *
972 c39be742 2021-07-09 op new_vhost(void)
973 c39be742 2021-07-09 op {
974 c7c8ef44 2021-01-01 op struct vhost *v;
975 c7c8ef44 2021-01-01 op
976 c7c8ef44 2021-01-01 op v = xcalloc(1, sizeof(*v));
977 c7c8ef44 2021-01-01 op v->proxy.protocols = TLS_PROTOCOLS_DEFAULT;
978 c7c8ef44 2021-01-01 op return v;
979 c39be742 2021-07-09 op }
980 c39be742 2021-07-09 op
981 c39be742 2021-07-09 op struct location *
982 c39be742 2021-07-09 op new_location(void)
983 c39be742 2021-07-09 op {
984 c39be742 2021-07-09 op struct location *l;
985 c39be742 2021-07-09 op
986 c39be742 2021-07-09 op l = xcalloc(1, sizeof(*l));
987 c39be742 2021-07-09 op l->dirfd = -1;
988 c39be742 2021-07-09 op l->fcgi = -1;
989 c39be742 2021-07-09 op return l;
990 c39be742 2021-07-09 op }
991 c39be742 2021-07-09 op
992 c39be742 2021-07-09 op char *
993 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
994 e17642a7 2021-02-01 op {
995 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
996 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
997 e17642a7 2021-02-01 op return path;
998 e17642a7 2021-02-01 op }
999 6abda252 2021-02-06 op
1000 6abda252 2021-02-06 op int
1001 6abda252 2021-02-06 op check_block_code(int n)
1002 6abda252 2021-02-06 op {
1003 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1004 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
1005 6abda252 2021-02-06 op return n;
1006 6abda252 2021-02-06 op }
1007 6abda252 2021-02-06 op
1008 6abda252 2021-02-06 op char *
1009 6abda252 2021-02-06 op check_block_fmt(char *fmt)
1010 6abda252 2021-02-06 op {
1011 6abda252 2021-02-06 op char *s;
1012 6abda252 2021-02-06 op
1013 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
1014 6abda252 2021-02-06 op if (*s != '%')
1015 6abda252 2021-02-06 op continue;
1016 6abda252 2021-02-06 op switch (*++s) {
1017 6abda252 2021-02-06 op case '%':
1018 6abda252 2021-02-06 op case 'p':
1019 6abda252 2021-02-06 op case 'q':
1020 6abda252 2021-02-06 op case 'P':
1021 6abda252 2021-02-06 op case 'N':
1022 6abda252 2021-02-06 op break;
1023 6abda252 2021-02-06 op default:
1024 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
1025 6abda252 2021-02-06 op }
1026 6abda252 2021-02-06 op }
1027 6abda252 2021-02-06 op
1028 6abda252 2021-02-06 op return fmt;
1029 6abda252 2021-02-06 op }
1030 6abda252 2021-02-06 op
1031 6abda252 2021-02-06 op int
1032 6abda252 2021-02-06 op check_strip_no(int n)
1033 6abda252 2021-02-06 op {
1034 6abda252 2021-02-06 op if (n <= 0)
1035 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
1036 a709ddf5 2021-02-07 op return n;
1037 a709ddf5 2021-02-07 op }
1038 a709ddf5 2021-02-07 op
1039 a709ddf5 2021-02-07 op int
1040 391825e3 2021-07-09 op check_port_num(int n)
1041 391825e3 2021-07-09 op {
1042 391825e3 2021-07-09 op if (n <= 0 || n >= UINT16_MAX)
1043 391825e3 2021-07-09 op yyerror("port number is %s: %d",
1044 391825e3 2021-07-09 op n <= 0 ? "too small" : "too large",
1045 391825e3 2021-07-09 op n);
1046 391825e3 2021-07-09 op return n;
1047 391825e3 2021-07-09 op }
1048 391825e3 2021-07-09 op
1049 391825e3 2021-07-09 op int
1050 a709ddf5 2021-02-07 op check_prefork_num(int n)
1051 a709ddf5 2021-02-07 op {
1052 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
1053 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
1054 6abda252 2021-02-06 op return n;
1055 6abda252 2021-02-06 op }
1056 49b73ba1 2021-02-10 op
1057 49b73ba1 2021-02-10 op void
1058 49b73ba1 2021-02-10 op advance_loc(void)
1059 49b73ba1 2021-02-10 op {
1060 b8e64ccd 2021-03-31 op loc = new_location();
1061 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1062 49b73ba1 2021-02-10 op }
1063 c705ecb1 2021-05-03 op
1064 c705ecb1 2021-05-03 op void
1065 c705ecb1 2021-05-03 op only_once(const void *ptr, const char *name)
1066 c705ecb1 2021-05-03 op {
1067 c705ecb1 2021-05-03 op if (ptr != NULL)
1068 c705ecb1 2021-05-03 op yyerror("`%s' specified more than once", name);
1069 c705ecb1 2021-05-03 op }
1070 8ad1c570 2021-05-09 op
1071 8ad1c570 2021-05-09 op void
1072 8ad1c570 2021-05-09 op only_oncei(int i, const char *name)
1073 8ad1c570 2021-05-09 op {
1074 8ad1c570 2021-05-09 op if (i != -1)
1075 8ad1c570 2021-05-09 op yyerror("`%s' specified more than once", name);
1076 8ad1c570 2021-05-09 op }
1077 8ad1c570 2021-05-09 op
1078 8ad1c570 2021-05-09 op int
1079 8ad1c570 2021-05-09 op fastcgi_conf(char *path, char *port, char *prog)
1080 8ad1c570 2021-05-09 op {
1081 8ad1c570 2021-05-09 op struct fcgi *f;
1082 8ad1c570 2021-05-09 op int i;
1083 8ad1c570 2021-05-09 op
1084 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
1085 8ad1c570 2021-05-09 op f = &fcgi[i];
1086 fafc6849 2021-06-29 op
1087 8ad1c570 2021-05-09 op if (f->path == NULL) {
1088 8ad1c570 2021-05-09 op f->id = i;
1089 8ad1c570 2021-05-09 op f->path = path;
1090 8ad1c570 2021-05-09 op f->port = port;
1091 8ad1c570 2021-05-09 op f->prog = prog;
1092 8ad1c570 2021-05-09 op return i;
1093 8ad1c570 2021-05-09 op }
1094 8ad1c570 2021-05-09 op
1095 8ad1c570 2021-05-09 op /* XXX: what to do with prog? */
1096 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
1097 8ad1c570 2021-05-09 op ((port == NULL && f->port == NULL) ||
1098 8ad1c570 2021-05-09 op !strcmp(f->port, port))) {
1099 8ad1c570 2021-05-09 op free(path);
1100 8ad1c570 2021-05-09 op free(port);
1101 8ad1c570 2021-05-09 op return i;
1102 8ad1c570 2021-05-09 op }
1103 8ad1c570 2021-05-09 op }
1104 8ad1c570 2021-05-09 op
1105 8ad1c570 2021-05-09 op yyerror("too much `fastcgi' rules defined.");
1106 8ad1c570 2021-05-09 op return -1;
1107 c92b802b 2021-06-11 op }
1108 c92b802b 2021-06-11 op
1109 c92b802b 2021-06-11 op void
1110 c92b802b 2021-06-11 op add_param(char *name, char *val, int env)
1111 c92b802b 2021-06-11 op {
1112 c92b802b 2021-06-11 op struct envlist *e;
1113 c92b802b 2021-06-11 op struct envhead *h;
1114 c92b802b 2021-06-11 op
1115 c92b802b 2021-06-11 op if (env)
1116 c92b802b 2021-06-11 op h = &host->env;
1117 c92b802b 2021-06-11 op else
1118 c92b802b 2021-06-11 op h = &host->params;
1119 c92b802b 2021-06-11 op
1120 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
1121 c92b802b 2021-06-11 op e->name = name;
1122 c92b802b 2021-06-11 op e->value = val;
1123 c92b802b 2021-06-11 op if (TAILQ_EMPTY(h))
1124 c92b802b 2021-06-11 op TAILQ_INSERT_HEAD(h, e, envs);
1125 c92b802b 2021-06-11 op else
1126 c92b802b 2021-06-11 op TAILQ_INSERT_TAIL(h, e, envs);
1127 3b21cca3 2021-06-29 op }