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 c74c7030 2021-07-19 op %token OFF ON
124 c74c7030 2021-07-19 op %token PARAM PORT PREFORK PROTOCOLS
125 c74c7030 2021-07-19 op %token 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 c74c7030 2021-07-19 op | PARAM string '=' string {
275 762b9b99 2021-07-09 op add_param($2, $4, 0);
276 c705ecb1 2021-05-03 op }
277 c8b74339 2021-01-24 op | locopt
278 c8b74339 2021-01-24 op ;
279 c8b74339 2021-01-24 op
280 c8b74339 2021-01-24 op locations : /* empty */
281 c39be742 2021-07-09 op | locations location optnl
282 c8b74339 2021-01-24 op ;
283 c8b74339 2021-01-24 op
284 c74c7030 2021-07-19 op location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
285 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
286 49b73ba1 2021-02-10 op if (*$3 == '/')
287 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
288 49b73ba1 2021-02-10 op loc->match = $3;
289 6119e13e 2021-01-19 op }
290 c8b74339 2021-01-24 op | error '}'
291 c8b74339 2021-01-24 op ;
292 c8b74339 2021-01-24 op
293 c8b74339 2021-01-24 op locopts : /* empty */
294 c39be742 2021-07-09 op | locopts locopt optnl
295 c8b74339 2021-01-24 op ;
296 c8b74339 2021-01-24 op
297 c74c7030 2021-07-19 op locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
298 c74c7030 2021-07-19 op | BLOCK RETURN NUM string {
299 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
300 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
301 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
302 6abda252 2021-02-06 op }
303 c74c7030 2021-07-19 op | BLOCK RETURN NUM {
304 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
305 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
306 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
307 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
308 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
309 6abda252 2021-02-06 op }
310 c74c7030 2021-07-19 op | BLOCK {
311 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
312 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
313 6abda252 2021-02-06 op loc->block_code = 40;
314 6abda252 2021-02-06 op }
315 c74c7030 2021-07-19 op | DEFAULT TYPE string {
316 c705ecb1 2021-05-03 op only_once(loc->default_mime, "default type");
317 eb59f87e 2021-02-09 op loc->default_mime = $3;
318 eb59f87e 2021-02-09 op }
319 abc8801d 2021-07-19 op | FASTCGI fastcgi
320 c74c7030 2021-07-19 op | INDEX string {
321 c705ecb1 2021-05-03 op only_once(loc->index, "index");
322 eb59f87e 2021-02-09 op loc->index = $2;
323 eb59f87e 2021-02-09 op }
324 c74c7030 2021-07-19 op | LANG string {
325 c705ecb1 2021-05-03 op only_once(loc->lang, "lang");
326 eb59f87e 2021-02-09 op loc->lang = $2;
327 eb59f87e 2021-02-09 op }
328 c74c7030 2021-07-19 op | LOG bool { loc->disable_log = !$2; }
329 c74c7030 2021-07-19 op | REQUIRE CLIENT CA string {
330 c705ecb1 2021-05-03 op only_once(loc->reqca, "require client ca");
331 adbe6a64 2021-04-30 op ensure_absolute_path($4);
332 02be96c6 2021-02-09 op if ((loc->reqca = load_ca($4)) == NULL)
333 02be96c6 2021-02-09 op yyerror("couldn't load ca cert: %s", $4);
334 02be96c6 2021-02-09 op free($4);
335 fdea6aa0 2021-04-30 op }
336 c74c7030 2021-07-19 op | ROOT string {
337 c705ecb1 2021-05-03 op only_once(loc->dir, "root");
338 fdea6aa0 2021-04-30 op loc->dir = ensure_absolute_path($2);
339 02be96c6 2021-02-09 op }
340 c74c7030 2021-07-19 op | STRIP NUM { loc->strip = check_strip_no($2); }
341 15902770 2021-01-15 op ;
342 13ed2fb6 2021-01-27 op
343 c74c7030 2021-07-19 op fastcgi : SPAWN string {
344 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
345 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf(NULL, NULL, $2);
346 0d047efc 2021-05-24 op }
347 98f52178 2021-06-29 op | string {
348 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
349 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($1, NULL, NULL);
350 0d047efc 2021-05-24 op }
351 c74c7030 2021-07-19 op | TCP string PORT NUM {
352 0d047efc 2021-05-24 op char *c;
353 762b9b99 2021-07-09 op if (asprintf(&c, "%d", $4) == -1)
354 0d047efc 2021-05-24 op err(1, "asprintf");
355 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
356 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, c, NULL);
357 0d047efc 2021-05-24 op }
358 c74c7030 2021-07-19 op | TCP string {
359 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
360 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
361 0d047efc 2021-05-24 op }
362 c74c7030 2021-07-19 op | TCP string PORT string {
363 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
364 762b9b99 2021-07-09 op loc->fcgi = fastcgi_conf($2, $4, NULL);
365 0d047efc 2021-05-24 op }
366 0d047efc 2021-05-24 op ;
367 0d047efc 2021-05-24 op
368 c39be742 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
369 67f49405 2021-07-09 op | ';' optnl /* semicolons too */
370 c39be742 2021-07-09 op | /*empty*/
371 c39be742 2021-07-09 op ;
372 fdea6aa0 2021-04-30 op
373 c39be742 2021-07-09 op %%
374 b8e64ccd 2021-03-31 op
375 74f0778b 2021-06-16 op static struct keyword {
376 74f0778b 2021-06-16 op const char *word;
377 74f0778b 2021-06-16 op int token;
378 74f0778b 2021-06-16 op } keywords[] = {
379 d93c8191 2021-07-09 op /* these MUST be sorted */
380 c74c7030 2021-07-19 op {"alias", ALIAS},
381 c74c7030 2021-07-19 op {"auto", AUTO},
382 c74c7030 2021-07-19 op {"block", BLOCK},
383 c74c7030 2021-07-19 op {"ca", CA},
384 c74c7030 2021-07-19 op {"cert", CERT},
385 c74c7030 2021-07-19 op {"cgi", CGI},
386 c74c7030 2021-07-19 op {"chroot", CHROOT},
387 c74c7030 2021-07-19 op {"client", CLIENT},
388 c74c7030 2021-07-19 op {"default", DEFAULT},
389 c74c7030 2021-07-19 op {"entrypoint", ENTRYPOINT},
390 c74c7030 2021-07-19 op {"env", ENV},
391 abc8801d 2021-07-19 op {"fastcgi", FASTCGI},
392 c74c7030 2021-07-19 op {"index", INDEX},
393 c74c7030 2021-07-19 op {"ipv6", IPV6},
394 c74c7030 2021-07-19 op {"key", KEY},
395 c74c7030 2021-07-19 op {"lang", LANG},
396 c74c7030 2021-07-19 op {"location", LOCATION},
397 c74c7030 2021-07-19 op {"log", LOG},
398 c74c7030 2021-07-19 op {"map", MAP},
399 c74c7030 2021-07-19 op {"mime", MIME},
400 c74c7030 2021-07-19 op {"off", OFF},
401 c74c7030 2021-07-19 op {"on", ON},
402 c74c7030 2021-07-19 op {"param", PARAM},
403 c74c7030 2021-07-19 op {"port", PORT},
404 c74c7030 2021-07-19 op {"prefork", PREFORK},
405 c74c7030 2021-07-19 op {"protocols", PROTOCOLS},
406 c74c7030 2021-07-19 op {"require", REQUIRE},
407 c74c7030 2021-07-19 op {"return", RETURN},
408 c74c7030 2021-07-19 op {"root", ROOT},
409 c74c7030 2021-07-19 op {"server", SERVER},
410 c74c7030 2021-07-19 op {"spawn", SPAWN},
411 c74c7030 2021-07-19 op {"strip", STRIP},
412 c74c7030 2021-07-19 op {"tcp", TCP},
413 c74c7030 2021-07-19 op {"to-ext", TOEXT},
414 c74c7030 2021-07-19 op {"type", TYPE},
415 c74c7030 2021-07-19 op {"user", USER},
416 74f0778b 2021-06-16 op };
417 74f0778b 2021-06-16 op
418 c39be742 2021-07-09 op void
419 c39be742 2021-07-09 op yyerror(const char *msg, ...)
420 c39be742 2021-07-09 op {
421 c39be742 2021-07-09 op va_list ap;
422 c39be742 2021-07-09 op
423 c39be742 2021-07-09 op file->errors++;
424 c39be742 2021-07-09 op
425 c39be742 2021-07-09 op va_start(ap, msg);
426 f3966209 2021-07-13 op fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
427 f3966209 2021-07-13 op vfprintf(stderr, msg, ap);
428 f3966209 2021-07-13 op fprintf(stderr, "\n");
429 f3966209 2021-07-13 op va_end(ap);
430 f3966209 2021-07-13 op }
431 f3966209 2021-07-13 op
432 f3966209 2021-07-13 op void
433 f3966209 2021-07-13 op yywarn(const char *msg, ...)
434 f3966209 2021-07-13 op {
435 f3966209 2021-07-13 op va_list ap;
436 f3966209 2021-07-13 op
437 f3966209 2021-07-13 op va_start(ap, msg);
438 f3966209 2021-07-13 op fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
439 c39be742 2021-07-09 op vfprintf(stderr, msg, ap);
440 c39be742 2021-07-09 op fprintf(stderr, "\n");
441 c39be742 2021-07-09 op va_end(ap);
442 c39be742 2021-07-09 op }
443 c39be742 2021-07-09 op
444 d93c8191 2021-07-09 op int
445 d93c8191 2021-07-09 op kw_cmp(const void *k, const void *e)
446 d93c8191 2021-07-09 op {
447 d93c8191 2021-07-09 op return strcmp(k, ((struct keyword *)e)->word);
448 d93c8191 2021-07-09 op }
449 d93c8191 2021-07-09 op
450 c39be742 2021-07-09 op int
451 c39be742 2021-07-09 op lookup(char *s)
452 74f0778b 2021-06-16 op {
453 c39be742 2021-07-09 op const struct keyword *p;
454 74f0778b 2021-06-16 op
455 c39be742 2021-07-09 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
456 c39be742 2021-07-09 op sizeof(keywords[0]), kw_cmp);
457 74f0778b 2021-06-16 op
458 c39be742 2021-07-09 op if (p)
459 c39be742 2021-07-09 op return p->token;
460 c39be742 2021-07-09 op else
461 c39be742 2021-07-09 op return STRING;
462 c39be742 2021-07-09 op }
463 c39be742 2021-07-09 op
464 c39be742 2021-07-09 op #define START_EXPAND 1
465 c39be742 2021-07-09 op #define DONE_EXPAND 2
466 c39be742 2021-07-09 op
467 c39be742 2021-07-09 op static int expanding;
468 c39be742 2021-07-09 op
469 c39be742 2021-07-09 op int
470 c39be742 2021-07-09 op igetc(void)
471 c39be742 2021-07-09 op {
472 c39be742 2021-07-09 op int c;
473 c39be742 2021-07-09 op
474 c39be742 2021-07-09 op while (1) {
475 c39be742 2021-07-09 op if (file->ungetpos > 0)
476 c39be742 2021-07-09 op c = file->ungetbuf[--file->ungetpos];
477 c39be742 2021-07-09 op else
478 c39be742 2021-07-09 op c = getc(file->stream);
479 c39be742 2021-07-09 op
480 c39be742 2021-07-09 op if (c == START_EXPAND)
481 c39be742 2021-07-09 op expanding = 1;
482 c39be742 2021-07-09 op else if (c == DONE_EXPAND)
483 c39be742 2021-07-09 op expanding = 0;
484 c39be742 2021-07-09 op else
485 c39be742 2021-07-09 op break;
486 74f0778b 2021-06-16 op }
487 c39be742 2021-07-09 op return c;
488 c39be742 2021-07-09 op }
489 74f0778b 2021-06-16 op
490 c39be742 2021-07-09 op int
491 c39be742 2021-07-09 op lgetc(int quotec)
492 c39be742 2021-07-09 op {
493 c39be742 2021-07-09 op int c, next;
494 c39be742 2021-07-09 op
495 c39be742 2021-07-09 op if (quotec) {
496 c39be742 2021-07-09 op if ((c = igetc()) == EOF) {
497 c39be742 2021-07-09 op yyerror("reached end of file while parsing "
498 c39be742 2021-07-09 op "quoted string");
499 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
500 c39be742 2021-07-09 op return EOF;
501 c39be742 2021-07-09 op return quotec;
502 c39be742 2021-07-09 op }
503 74f0778b 2021-06-16 op return c;
504 74f0778b 2021-06-16 op }
505 74f0778b 2021-06-16 op
506 c39be742 2021-07-09 op while ((c = igetc()) == '\\') {
507 c39be742 2021-07-09 op next = igetc();
508 c39be742 2021-07-09 op if (next != '\n') {
509 c39be742 2021-07-09 op c = next;
510 74f0778b 2021-06-16 op break;
511 c39be742 2021-07-09 op }
512 c39be742 2021-07-09 op yylval.lineno = file->lineno;
513 c39be742 2021-07-09 op file->lineno++;
514 c39be742 2021-07-09 op }
515 3b21cca3 2021-06-29 op
516 c39be742 2021-07-09 op if (c == EOF) {
517 c39be742 2021-07-09 op /*
518 c39be742 2021-07-09 op * Fake EOL when hit EOF for the first time. This gets line
519 c39be742 2021-07-09 op * count right if last line in included file is syntactically
520 c39be742 2021-07-09 op * invalid and has no newline.
521 c39be742 2021-07-09 op */
522 c39be742 2021-07-09 op if (file->eof_reached == 0) {
523 c39be742 2021-07-09 op file->eof_reached = 1;
524 c39be742 2021-07-09 op return '\n';
525 c39be742 2021-07-09 op }
526 c39be742 2021-07-09 op while (c == EOF) {
527 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
528 c39be742 2021-07-09 op return EOF;
529 c39be742 2021-07-09 op c = igetc();
530 c39be742 2021-07-09 op }
531 c39be742 2021-07-09 op }
532 c39be742 2021-07-09 op return c;
533 c39be742 2021-07-09 op }
534 c39be742 2021-07-09 op
535 c39be742 2021-07-09 op void
536 c39be742 2021-07-09 op lungetc(int c)
537 c39be742 2021-07-09 op {
538 c39be742 2021-07-09 op if (c == EOF)
539 c39be742 2021-07-09 op return;
540 c39be742 2021-07-09 op
541 c39be742 2021-07-09 op if (file->ungetpos >= file->ungetsize) {
542 c39be742 2021-07-09 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
543 c39be742 2021-07-09 op if (p == NULL)
544 c39be742 2021-07-09 op err(1, "lungetc");
545 c39be742 2021-07-09 op file->ungetbuf = p;
546 c39be742 2021-07-09 op file->ungetsize *= 2;
547 c39be742 2021-07-09 op }
548 c39be742 2021-07-09 op file->ungetbuf[file->ungetpos++] = c;
549 c39be742 2021-07-09 op }
550 c39be742 2021-07-09 op
551 c39be742 2021-07-09 op int
552 c39be742 2021-07-09 op findeol(void)
553 c39be742 2021-07-09 op {
554 c39be742 2021-07-09 op int c;
555 c39be742 2021-07-09 op
556 c39be742 2021-07-09 op /* Skip to either EOF or the first real EOL. */
557 c39be742 2021-07-09 op while (1) {
558 c39be742 2021-07-09 op c = lgetc(0);
559 c39be742 2021-07-09 op if (c == '\n') {
560 c39be742 2021-07-09 op file->lineno++;
561 3b21cca3 2021-06-29 op break;
562 c39be742 2021-07-09 op }
563 c39be742 2021-07-09 op if (c == EOF)
564 74f0778b 2021-06-16 op break;
565 c39be742 2021-07-09 op }
566 c39be742 2021-07-09 op return ERROR;
567 c39be742 2021-07-09 op }
568 c39be742 2021-07-09 op
569 c39be742 2021-07-09 op int
570 c39be742 2021-07-09 op yylex(void)
571 c39be742 2021-07-09 op {
572 1bd706dc 2021-07-09 op char buf[8096];
573 1bd706dc 2021-07-09 op char *p, *val;
574 1bd706dc 2021-07-09 op int quotec, next, c;
575 1bd706dc 2021-07-09 op int token;
576 c39be742 2021-07-09 op
577 c39be742 2021-07-09 op top:
578 c39be742 2021-07-09 op p = buf;
579 c39be742 2021-07-09 op while ((c = lgetc(0)) == ' ' || c == '\t')
580 c39be742 2021-07-09 op ; /* nothing */
581 c39be742 2021-07-09 op
582 c39be742 2021-07-09 op yylval.lineno = file->lineno;
583 c39be742 2021-07-09 op if (c == '#')
584 c39be742 2021-07-09 op while ((c = lgetc(0)) != '\n' && c != EOF)
585 c39be742 2021-07-09 op ; /* nothing */
586 c39be742 2021-07-09 op if (c == '$' && !expanding) {
587 c39be742 2021-07-09 op while (1) {
588 c39be742 2021-07-09 op if ((c = lgetc(0)) == EOF)
589 c39be742 2021-07-09 op return 0;
590 67f49405 2021-07-09 op if (p + 1 >= buf + sizeof(buf) -1) {
591 67f49405 2021-07-09 op yyerror("string too long");
592 67f49405 2021-07-09 op return findeol();
593 67f49405 2021-07-09 op }
594 67f49405 2021-07-09 op if (isalnum(c) || c == '_') {
595 67f49405 2021-07-09 op *p++ = c;
596 67f49405 2021-07-09 op continue;
597 67f49405 2021-07-09 op }
598 67f49405 2021-07-09 op *p = '\0';
599 67f49405 2021-07-09 op lungetc(c);
600 67f49405 2021-07-09 op break;
601 67f49405 2021-07-09 op }
602 67f49405 2021-07-09 op val = symget(buf);
603 67f49405 2021-07-09 op if (val == NULL) {
604 67f49405 2021-07-09 op yyerror("macro `%s' not defined", buf);
605 67f49405 2021-07-09 op return findeol();
606 67f49405 2021-07-09 op }
607 67f49405 2021-07-09 op yylval.v.string = xstrdup(val);
608 67f49405 2021-07-09 op return STRING;
609 67f49405 2021-07-09 op }
610 67f49405 2021-07-09 op if (c == '@' && !expanding) {
611 67f49405 2021-07-09 op while (1) {
612 67f49405 2021-07-09 op if ((c = lgetc(0)) == EOF)
613 67f49405 2021-07-09 op return 0;
614 c39be742 2021-07-09 op
615 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
616 c39be742 2021-07-09 op yyerror("string too long");
617 c39be742 2021-07-09 op return findeol();
618 c39be742 2021-07-09 op }
619 c39be742 2021-07-09 op if (isalnum(c) || c == '_') {
620 c39be742 2021-07-09 op *p++ = c;
621 74f0778b 2021-06-16 op continue;
622 74f0778b 2021-06-16 op }
623 c39be742 2021-07-09 op *p = '\0';
624 c39be742 2021-07-09 op lungetc(c);
625 c39be742 2021-07-09 op break;
626 74f0778b 2021-06-16 op }
627 c39be742 2021-07-09 op val = symget(buf);
628 c39be742 2021-07-09 op if (val == NULL) {
629 c39be742 2021-07-09 op yyerror("macro '%s' not defined", buf);
630 c39be742 2021-07-09 op return findeol();
631 74f0778b 2021-06-16 op }
632 c39be742 2021-07-09 op p = val + strlen(val) - 1;
633 c39be742 2021-07-09 op lungetc(DONE_EXPAND);
634 c39be742 2021-07-09 op while (p >= val) {
635 c39be742 2021-07-09 op lungetc(*p);
636 c39be742 2021-07-09 op p--;
637 c39be742 2021-07-09 op }
638 c39be742 2021-07-09 op lungetc(START_EXPAND);
639 c39be742 2021-07-09 op goto top;
640 74f0778b 2021-06-16 op }
641 74f0778b 2021-06-16 op
642 c39be742 2021-07-09 op switch (c) {
643 c39be742 2021-07-09 op case '\'':
644 c39be742 2021-07-09 op case '"':
645 c39be742 2021-07-09 op quotec = c;
646 c39be742 2021-07-09 op while (1) {
647 c39be742 2021-07-09 op if ((c = lgetc(quotec)) == EOF)
648 c39be742 2021-07-09 op return 0;
649 c39be742 2021-07-09 op if (c == '\n') {
650 c39be742 2021-07-09 op file->lineno++;
651 c39be742 2021-07-09 op continue;
652 c39be742 2021-07-09 op } else if (c == '\\') {
653 c39be742 2021-07-09 op if ((next = lgetc(quotec)) == EOF)
654 c39be742 2021-07-09 op return (0);
655 c39be742 2021-07-09 op if (next == quotec || next == ' ' ||
656 c39be742 2021-07-09 op next == '\t')
657 c39be742 2021-07-09 op c = next;
658 c39be742 2021-07-09 op else if (next == '\n') {
659 c39be742 2021-07-09 op file->lineno++;
660 c39be742 2021-07-09 op continue;
661 c39be742 2021-07-09 op } else
662 c39be742 2021-07-09 op lungetc(next);
663 c39be742 2021-07-09 op } else if (c == quotec) {
664 c39be742 2021-07-09 op *p = '\0';
665 c39be742 2021-07-09 op break;
666 c39be742 2021-07-09 op } else if (c == '\0') {
667 f3966209 2021-07-13 op yyerror("invalid syntax");
668 c39be742 2021-07-09 op return findeol();
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 *p++ = c;
675 c39be742 2021-07-09 op }
676 c39be742 2021-07-09 op yylval.v.string = strdup(buf);
677 c39be742 2021-07-09 op if (yylval.v.string == NULL)
678 c39be742 2021-07-09 op err(1, "yylex: strdup");
679 c39be742 2021-07-09 op return STRING;
680 74f0778b 2021-06-16 op }
681 c39be742 2021-07-09 op
682 c39be742 2021-07-09 op #define allowed_to_end_number(x) \
683 c39be742 2021-07-09 op (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
684 c39be742 2021-07-09 op
685 c39be742 2021-07-09 op if (c == '-' || isdigit(c)) {
686 c39be742 2021-07-09 op do {
687 c39be742 2021-07-09 op *p++ = c;
688 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
689 c39be742 2021-07-09 op yyerror("string too long");
690 c39be742 2021-07-09 op return findeol();
691 c39be742 2021-07-09 op }
692 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && isdigit(c));
693 c39be742 2021-07-09 op lungetc(c);
694 c39be742 2021-07-09 op if (p == buf + 1 && buf[0] == '-')
695 c39be742 2021-07-09 op goto nodigits;
696 c39be742 2021-07-09 op if (c == EOF || allowed_to_end_number(c)) {
697 c39be742 2021-07-09 op const char *errstr = NULL;
698 c39be742 2021-07-09 op
699 c39be742 2021-07-09 op *p = '\0';
700 c39be742 2021-07-09 op yylval.v.number = strtonum(buf, LLONG_MIN,
701 c39be742 2021-07-09 op LLONG_MAX, &errstr);
702 c39be742 2021-07-09 op if (errstr) {
703 c39be742 2021-07-09 op yyerror("\"%s\" invalid number: %s",
704 c39be742 2021-07-09 op buf, errstr);
705 c39be742 2021-07-09 op return findeol();
706 c39be742 2021-07-09 op }
707 c39be742 2021-07-09 op return NUM;
708 c39be742 2021-07-09 op } else {
709 c39be742 2021-07-09 op nodigits:
710 c39be742 2021-07-09 op while (p > buf + 1)
711 c39be742 2021-07-09 op lungetc(*--p);
712 c39be742 2021-07-09 op c = *--p;
713 c39be742 2021-07-09 op if (c == '-')
714 c39be742 2021-07-09 op return c;
715 c39be742 2021-07-09 op }
716 74f0778b 2021-06-16 op }
717 c39be742 2021-07-09 op
718 c39be742 2021-07-09 op #define allowed_in_string(x) \
719 c39be742 2021-07-09 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
720 c39be742 2021-07-09 op x != '{' && x != '}' && \
721 c39be742 2021-07-09 op x != '!' && x != '=' && x != '#' && \
722 67f49405 2021-07-09 op x != ',' && x != ';'))
723 c39be742 2021-07-09 op
724 67f49405 2021-07-09 op if (isalnum(c) || c == ':' || c == '_') {
725 c39be742 2021-07-09 op do {
726 c39be742 2021-07-09 op *p++ = c;
727 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
728 c39be742 2021-07-09 op yyerror("string too long");
729 c39be742 2021-07-09 op return findeol();
730 c39be742 2021-07-09 op }
731 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
732 c39be742 2021-07-09 op lungetc(c);
733 c39be742 2021-07-09 op *p = '\0';
734 c39be742 2021-07-09 op if ((token = lookup(buf)) == STRING)
735 c39be742 2021-07-09 op yylval.v.string = xstrdup(buf);
736 c39be742 2021-07-09 op return token;
737 74f0778b 2021-06-16 op }
738 c39be742 2021-07-09 op if (c == '\n') {
739 c39be742 2021-07-09 op yylval.lineno = file->lineno;
740 c39be742 2021-07-09 op file->lineno++;
741 74f0778b 2021-06-16 op }
742 c39be742 2021-07-09 op if (c == EOF)
743 c39be742 2021-07-09 op return 0;
744 c39be742 2021-07-09 op return c;
745 c39be742 2021-07-09 op }
746 c39be742 2021-07-09 op
747 c39be742 2021-07-09 op struct file *
748 c39be742 2021-07-09 op pushfile(const char *name, int secret)
749 c39be742 2021-07-09 op {
750 c39be742 2021-07-09 op struct file *nfile;
751 c39be742 2021-07-09 op
752 c39be742 2021-07-09 op nfile = xcalloc(1, sizeof(*nfile));
753 c39be742 2021-07-09 op nfile->name = xstrdup(name);
754 c39be742 2021-07-09 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
755 83272dfe 2021-08-23 op log_warn(NULL, "can't open %s: %s", nfile->name,
756 c39be742 2021-07-09 op strerror(errno));
757 c39be742 2021-07-09 op free(nfile->name);
758 c39be742 2021-07-09 op free(nfile);
759 c39be742 2021-07-09 op return NULL;
760 74f0778b 2021-06-16 op }
761 c39be742 2021-07-09 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
762 c39be742 2021-07-09 op nfile->ungetsize = 16;
763 c39be742 2021-07-09 op nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
764 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&files, nfile, entry);
765 c39be742 2021-07-09 op return nfile;
766 c39be742 2021-07-09 op }
767 74f0778b 2021-06-16 op
768 c39be742 2021-07-09 op int
769 c39be742 2021-07-09 op popfile(void)
770 c39be742 2021-07-09 op {
771 c39be742 2021-07-09 op struct file *prev;
772 13ed2fb6 2021-01-27 op
773 c39be742 2021-07-09 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
774 c39be742 2021-07-09 op prev->errors += file->errors;
775 13ed2fb6 2021-01-27 op
776 c39be742 2021-07-09 op TAILQ_REMOVE(&files, file, entry);
777 c39be742 2021-07-09 op fclose(file->stream);
778 c39be742 2021-07-09 op free(file->name);
779 c39be742 2021-07-09 op free(file->ungetbuf);
780 c39be742 2021-07-09 op free(file);
781 c39be742 2021-07-09 op file = prev;
782 c39be742 2021-07-09 op return file ? 0 : EOF;
783 13ed2fb6 2021-01-27 op }
784 13ed2fb6 2021-01-27 op
785 13ed2fb6 2021-01-27 op void
786 c39be742 2021-07-09 op parse_conf(const char *filename)
787 13ed2fb6 2021-01-27 op {
788 c39be742 2021-07-09 op struct sym *sym, *next;
789 3b21cca3 2021-06-29 op
790 c39be742 2021-07-09 op file = pushfile(filename, 0);
791 c39be742 2021-07-09 op if (file == NULL)
792 83272dfe 2021-08-23 op exit(1);
793 c39be742 2021-07-09 op topfile = file;
794 c39be742 2021-07-09 op
795 13ed2fb6 2021-01-27 op yyparse();
796 c39be742 2021-07-09 op errors = file->errors;
797 c39be742 2021-07-09 op popfile();
798 13ed2fb6 2021-01-27 op
799 c39be742 2021-07-09 op /* Free macros and check which have not been used. */
800 3b21cca3 2021-06-29 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
801 3b21cca3 2021-06-29 op /* TODO: warn if !sym->used */
802 3b21cca3 2021-06-29 op if (!sym->persist) {
803 3b21cca3 2021-06-29 op free(sym->name);
804 3b21cca3 2021-06-29 op free(sym->val);
805 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
806 3b21cca3 2021-06-29 op free(sym);
807 3b21cca3 2021-06-29 op }
808 3b21cca3 2021-06-29 op }
809 c39be742 2021-07-09 op
810 c39be742 2021-07-09 op if (errors)
811 c39be742 2021-07-09 op exit(1);
812 13ed2fb6 2021-01-27 op }
813 e17642a7 2021-02-01 op
814 c39be742 2021-07-09 op int
815 c39be742 2021-07-09 op symset(const char *name, const char *val, int persist)
816 c39be742 2021-07-09 op {
817 c39be742 2021-07-09 op struct sym *sym;
818 c39be742 2021-07-09 op
819 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
820 c39be742 2021-07-09 op if (!strcmp(name, sym->name))
821 c39be742 2021-07-09 op break;
822 c39be742 2021-07-09 op }
823 c39be742 2021-07-09 op
824 c39be742 2021-07-09 op if (sym != NULL) {
825 c39be742 2021-07-09 op if (sym->persist)
826 c39be742 2021-07-09 op return 0;
827 c39be742 2021-07-09 op else {
828 c39be742 2021-07-09 op free(sym->name);
829 c39be742 2021-07-09 op free(sym->val);
830 c39be742 2021-07-09 op TAILQ_REMOVE(&symhead, sym, entry);
831 c39be742 2021-07-09 op free(sym);
832 c39be742 2021-07-09 op }
833 c39be742 2021-07-09 op }
834 c39be742 2021-07-09 op
835 c39be742 2021-07-09 op sym = xcalloc(1, sizeof(*sym));
836 c39be742 2021-07-09 op sym->name = xstrdup(name);
837 c39be742 2021-07-09 op sym->val = xstrdup(val);
838 c39be742 2021-07-09 op sym->used = 0;
839 c39be742 2021-07-09 op sym->persist = persist;
840 c39be742 2021-07-09 op
841 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
842 c39be742 2021-07-09 op return 0;
843 c39be742 2021-07-09 op }
844 c39be742 2021-07-09 op
845 c39be742 2021-07-09 op int
846 c39be742 2021-07-09 op cmdline_symset(char *s)
847 c39be742 2021-07-09 op {
848 c39be742 2021-07-09 op char *sym, *val;
849 c39be742 2021-07-09 op int ret;
850 c39be742 2021-07-09 op
851 c39be742 2021-07-09 op if ((val = strrchr(s, '=')) == NULL)
852 c39be742 2021-07-09 op return -1;
853 c39be742 2021-07-09 op sym = xcalloc(1, val - s + 1);
854 c39be742 2021-07-09 op memcpy(sym, s, val - s);
855 c39be742 2021-07-09 op ret = symset(sym, val + 1, 1);
856 c39be742 2021-07-09 op free(sym);
857 c39be742 2021-07-09 op return ret;
858 c39be742 2021-07-09 op }
859 c39be742 2021-07-09 op
860 e17642a7 2021-02-01 op char *
861 c39be742 2021-07-09 op symget(const char *nam)
862 c39be742 2021-07-09 op {
863 c39be742 2021-07-09 op struct sym *sym;
864 c39be742 2021-07-09 op
865 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
866 c39be742 2021-07-09 op if (strcmp(nam, sym->name) == 0) {
867 c39be742 2021-07-09 op sym->used = 1;
868 c39be742 2021-07-09 op return sym->val;
869 c39be742 2021-07-09 op }
870 c39be742 2021-07-09 op }
871 c39be742 2021-07-09 op return NULL;
872 c39be742 2021-07-09 op }
873 c39be742 2021-07-09 op
874 c39be742 2021-07-09 op struct vhost *
875 c39be742 2021-07-09 op new_vhost(void)
876 c39be742 2021-07-09 op {
877 c39be742 2021-07-09 op return xcalloc(1, sizeof(struct vhost));
878 c39be742 2021-07-09 op }
879 c39be742 2021-07-09 op
880 c39be742 2021-07-09 op struct location *
881 c39be742 2021-07-09 op new_location(void)
882 c39be742 2021-07-09 op {
883 c39be742 2021-07-09 op struct location *l;
884 c39be742 2021-07-09 op
885 c39be742 2021-07-09 op l = xcalloc(1, sizeof(*l));
886 c39be742 2021-07-09 op l->dirfd = -1;
887 c39be742 2021-07-09 op l->fcgi = -1;
888 c39be742 2021-07-09 op return l;
889 c39be742 2021-07-09 op }
890 c39be742 2021-07-09 op
891 c39be742 2021-07-09 op char *
892 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
893 e17642a7 2021-02-01 op {
894 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
895 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
896 e17642a7 2021-02-01 op return path;
897 e17642a7 2021-02-01 op }
898 6abda252 2021-02-06 op
899 6abda252 2021-02-06 op int
900 6abda252 2021-02-06 op check_block_code(int n)
901 6abda252 2021-02-06 op {
902 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
903 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
904 6abda252 2021-02-06 op return n;
905 6abda252 2021-02-06 op }
906 6abda252 2021-02-06 op
907 6abda252 2021-02-06 op char *
908 6abda252 2021-02-06 op check_block_fmt(char *fmt)
909 6abda252 2021-02-06 op {
910 6abda252 2021-02-06 op char *s;
911 6abda252 2021-02-06 op
912 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
913 6abda252 2021-02-06 op if (*s != '%')
914 6abda252 2021-02-06 op continue;
915 6abda252 2021-02-06 op switch (*++s) {
916 6abda252 2021-02-06 op case '%':
917 6abda252 2021-02-06 op case 'p':
918 6abda252 2021-02-06 op case 'q':
919 6abda252 2021-02-06 op case 'P':
920 6abda252 2021-02-06 op case 'N':
921 6abda252 2021-02-06 op break;
922 6abda252 2021-02-06 op default:
923 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
924 6abda252 2021-02-06 op }
925 6abda252 2021-02-06 op }
926 6abda252 2021-02-06 op
927 6abda252 2021-02-06 op return fmt;
928 6abda252 2021-02-06 op }
929 6abda252 2021-02-06 op
930 6abda252 2021-02-06 op int
931 6abda252 2021-02-06 op check_strip_no(int n)
932 6abda252 2021-02-06 op {
933 6abda252 2021-02-06 op if (n <= 0)
934 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
935 a709ddf5 2021-02-07 op return n;
936 a709ddf5 2021-02-07 op }
937 a709ddf5 2021-02-07 op
938 a709ddf5 2021-02-07 op int
939 391825e3 2021-07-09 op check_port_num(int n)
940 391825e3 2021-07-09 op {
941 391825e3 2021-07-09 op if (n <= 0 || n >= UINT16_MAX)
942 391825e3 2021-07-09 op yyerror("port number is %s: %d",
943 391825e3 2021-07-09 op n <= 0 ? "too small" : "too large",
944 391825e3 2021-07-09 op n);
945 391825e3 2021-07-09 op return n;
946 391825e3 2021-07-09 op }
947 391825e3 2021-07-09 op
948 391825e3 2021-07-09 op int
949 a709ddf5 2021-02-07 op check_prefork_num(int n)
950 a709ddf5 2021-02-07 op {
951 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
952 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
953 6abda252 2021-02-06 op return n;
954 6abda252 2021-02-06 op }
955 49b73ba1 2021-02-10 op
956 49b73ba1 2021-02-10 op void
957 49b73ba1 2021-02-10 op advance_loc(void)
958 49b73ba1 2021-02-10 op {
959 b8e64ccd 2021-03-31 op loc = new_location();
960 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
961 49b73ba1 2021-02-10 op }
962 c705ecb1 2021-05-03 op
963 c705ecb1 2021-05-03 op void
964 c705ecb1 2021-05-03 op only_once(const void *ptr, const char *name)
965 c705ecb1 2021-05-03 op {
966 c705ecb1 2021-05-03 op if (ptr != NULL)
967 c705ecb1 2021-05-03 op yyerror("`%s' specified more than once", name);
968 c705ecb1 2021-05-03 op }
969 8ad1c570 2021-05-09 op
970 8ad1c570 2021-05-09 op void
971 8ad1c570 2021-05-09 op only_oncei(int i, const char *name)
972 8ad1c570 2021-05-09 op {
973 8ad1c570 2021-05-09 op if (i != -1)
974 8ad1c570 2021-05-09 op yyerror("`%s' specified more than once", name);
975 8ad1c570 2021-05-09 op }
976 8ad1c570 2021-05-09 op
977 8ad1c570 2021-05-09 op int
978 8ad1c570 2021-05-09 op fastcgi_conf(char *path, char *port, char *prog)
979 8ad1c570 2021-05-09 op {
980 8ad1c570 2021-05-09 op struct fcgi *f;
981 8ad1c570 2021-05-09 op int i;
982 8ad1c570 2021-05-09 op
983 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
984 8ad1c570 2021-05-09 op f = &fcgi[i];
985 fafc6849 2021-06-29 op
986 8ad1c570 2021-05-09 op if (f->path == NULL) {
987 8ad1c570 2021-05-09 op f->id = i;
988 8ad1c570 2021-05-09 op f->path = path;
989 8ad1c570 2021-05-09 op f->port = port;
990 8ad1c570 2021-05-09 op f->prog = prog;
991 8ad1c570 2021-05-09 op return i;
992 8ad1c570 2021-05-09 op }
993 8ad1c570 2021-05-09 op
994 8ad1c570 2021-05-09 op /* XXX: what to do with prog? */
995 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
996 8ad1c570 2021-05-09 op ((port == NULL && f->port == NULL) ||
997 8ad1c570 2021-05-09 op !strcmp(f->port, port))) {
998 8ad1c570 2021-05-09 op free(path);
999 8ad1c570 2021-05-09 op free(port);
1000 8ad1c570 2021-05-09 op return i;
1001 8ad1c570 2021-05-09 op }
1002 8ad1c570 2021-05-09 op }
1003 8ad1c570 2021-05-09 op
1004 8ad1c570 2021-05-09 op yyerror("too much `fastcgi' rules defined.");
1005 8ad1c570 2021-05-09 op return -1;
1006 c92b802b 2021-06-11 op }
1007 c92b802b 2021-06-11 op
1008 c92b802b 2021-06-11 op void
1009 c92b802b 2021-06-11 op add_param(char *name, char *val, int env)
1010 c92b802b 2021-06-11 op {
1011 c92b802b 2021-06-11 op struct envlist *e;
1012 c92b802b 2021-06-11 op struct envhead *h;
1013 c92b802b 2021-06-11 op
1014 c92b802b 2021-06-11 op if (env)
1015 c92b802b 2021-06-11 op h = &host->env;
1016 c92b802b 2021-06-11 op else
1017 c92b802b 2021-06-11 op h = &host->params;
1018 c92b802b 2021-06-11 op
1019 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
1020 c92b802b 2021-06-11 op e->name = name;
1021 c92b802b 2021-06-11 op e->value = val;
1022 c92b802b 2021-06-11 op if (TAILQ_EMPTY(h))
1023 c92b802b 2021-06-11 op TAILQ_INSERT_HEAD(h, e, envs);
1024 c92b802b 2021-06-11 op else
1025 c92b802b 2021-06-11 op TAILQ_INSERT_TAIL(h, e, envs);
1026 3b21cca3 2021-06-29 op }