Blame


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