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