Blame


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