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 7252049d 2021-06-29 op %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
114 7252049d 2021-06-29 op %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
115 7252049d 2021-06-29 op %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
116 c39be742 2021-07-09 op %token TFASTCGI TSPAWN TPARAM TMAP TTOEXT INCLUDE TON TOFF
117 d06d6f4b 2021-04-29 op
118 c39be742 2021-07-09 op %token ERROR
119 7252049d 2021-06-29 op
120 c39be742 2021-07-09 op %token <v.string> STRING
121 c39be742 2021-07-09 op %token <v.number> NUM
122 15902770 2021-01-15 op
123 c39be742 2021-07-09 op %type <v.number> bool
124 c39be742 2021-07-09 op %type <v.string> string
125 98f52178 2021-06-29 op
126 15902770 2021-01-15 op %%
127 15902770 2021-01-15 op
128 6b86655a 2021-06-29 op conf : /* empty */
129 c39be742 2021-07-09 op | conf include '\n'
130 c39be742 2021-07-09 op | conf '\n'
131 c39be742 2021-07-09 op | conf varset '\n'
132 c39be742 2021-07-09 op | conf option '\n'
133 c39be742 2021-07-09 op | conf vhost '\n'
134 c39be742 2021-07-09 op | conf error '\n' { file->errors++; }
135 3b21cca3 2021-06-29 op ;
136 3b21cca3 2021-06-29 op
137 c39be742 2021-07-09 op include : INCLUDE STRING {
138 c39be742 2021-07-09 op struct file *nfile;
139 c39be742 2021-07-09 op
140 c39be742 2021-07-09 op if ((nfile = pushfile($2, 0)) == NULL) {
141 c39be742 2021-07-09 op yyerror("failed to include file %s", $2);
142 c39be742 2021-07-09 op free($2);
143 c39be742 2021-07-09 op YYERROR;
144 c39be742 2021-07-09 op }
145 c39be742 2021-07-09 op free($2);
146 c39be742 2021-07-09 op
147 c39be742 2021-07-09 op file = nfile;
148 c39be742 2021-07-09 op lungetc('\n');
149 c39be742 2021-07-09 op }
150 c39be742 2021-07-09 op ;
151 c39be742 2021-07-09 op
152 c39be742 2021-07-09 op bool : TON { $$ = 1; }
153 c39be742 2021-07-09 op | TOFF { $$ = 0; }
154 c39be742 2021-07-09 op ;
155 c39be742 2021-07-09 op
156 c39be742 2021-07-09 op string : string STRING {
157 98f52178 2021-06-29 op if (asprintf(&$$, "%s%s", $1, $2) == -1) {
158 98f52178 2021-06-29 op free($1);
159 98f52178 2021-06-29 op free($2);
160 98f52178 2021-06-29 op yyerror("string: asprintf: %s", strerror(errno));
161 98f52178 2021-06-29 op YYERROR;
162 98f52178 2021-06-29 op }
163 98f52178 2021-06-29 op free($1);
164 98f52178 2021-06-29 op free($2);
165 98f52178 2021-06-29 op }
166 c39be742 2021-07-09 op | STRING
167 98f52178 2021-06-29 op ;
168 98f52178 2021-06-29 op
169 c39be742 2021-07-09 op varset : STRING '=' string {
170 3b21cca3 2021-06-29 op char *s = $1;
171 3b21cca3 2021-06-29 op while (*s++) {
172 c39be742 2021-07-09 op if (isspace((unsigned char)*s)) {
173 3b21cca3 2021-06-29 op yyerror("macro name cannot contain "
174 3b21cca3 2021-06-29 op "whitespaces");
175 3b21cca3 2021-06-29 op free($1);
176 3b21cca3 2021-06-29 op free($3);
177 3b21cca3 2021-06-29 op YYERROR;
178 3b21cca3 2021-06-29 op }
179 3b21cca3 2021-06-29 op }
180 3b21cca3 2021-06-29 op symset($1, $3, 0);
181 3b21cca3 2021-06-29 op free($1);
182 3b21cca3 2021-06-29 op free($3);
183 3b21cca3 2021-06-29 op }
184 15902770 2021-01-15 op ;
185 15902770 2021-01-15 op
186 98f52178 2021-06-29 op option : TCHROOT string { conf.chroot = $2; }
187 c39be742 2021-07-09 op | TIPV6 bool { conf.ipv6 = $2; }
188 c39be742 2021-07-09 op | TMIME STRING string {
189 f3966209 2021-07-13 op yywarn("`mime MIME EXT' is deprecated and will be "
190 f3966209 2021-07-13 op "removed in a future version, please use the new "
191 f3966209 2021-07-13 op "syntax: `map MIME to-ext EXT'");
192 d19951cf 2021-07-09 op add_mime(&conf.mime, $2, $3);
193 d19951cf 2021-07-09 op }
194 ff954a3e 2021-07-09 op | TMAP string TTOEXT string { add_mime(&conf.mime, $2, $4); }
195 391825e3 2021-07-09 op | TPORT NUM { conf.port = check_port_num($2); }
196 c39be742 2021-07-09 op | TPREFORK NUM { conf.prefork = check_prefork_num($2); }
197 98f52178 2021-06-29 op | TPROTOCOLS string {
198 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
199 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
200 5bc3c98e 2021-01-15 op }
201 98f52178 2021-06-29 op | TUSER string { conf.user = $2; }
202 15902770 2021-01-15 op ;
203 15902770 2021-01-15 op
204 98f52178 2021-06-29 op vhost : TSERVER string {
205 b8e64ccd 2021-03-31 op host = new_vhost();
206 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
207 b8e64ccd 2021-03-31 op
208 b8e64ccd 2021-03-31 op loc = new_location();
209 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
210 b8e64ccd 2021-03-31 op
211 b8e64ccd 2021-03-31 op loc->match = xstrdup("*");
212 15902770 2021-01-15 op host->domain = $2;
213 15902770 2021-01-15 op
214 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
215 f3966209 2021-07-13 op yywarn("\"%s\" looks like punycode: you "
216 f3966209 2021-07-13 op "should use the decoded hostname", $2);
217 cbeee4ca 2021-01-28 op }
218 c39be742 2021-07-09 op } '{' optnl servopts locations '}' {
219 fdea6aa0 2021-04-30 op if (host->cert == NULL || host->key == NULL)
220 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
221 15902770 2021-01-15 op }
222 f3966209 2021-07-13 op | error '}' { yyerror("bad server directive"); }
223 15902770 2021-01-15 op ;
224 15902770 2021-01-15 op
225 15902770 2021-01-15 op servopts : /* empty */
226 c39be742 2021-07-09 op | servopts servopt optnl
227 15902770 2021-01-15 op ;
228 15902770 2021-01-15 op
229 98f52178 2021-06-29 op servopt : TALIAS string {
230 cc8c2901 2021-04-29 op struct alist *a;
231 cc8c2901 2021-04-29 op
232 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
233 cc8c2901 2021-04-29 op a->alias = $2;
234 cc8c2901 2021-04-29 op if (TAILQ_EMPTY(&host->aliases))
235 cc8c2901 2021-04-29 op TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
236 cc8c2901 2021-04-29 op else
237 cc8c2901 2021-04-29 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
238 cc8c2901 2021-04-29 op }
239 98f52178 2021-06-29 op | TCERT string {
240 c705ecb1 2021-05-03 op only_once(host->cert, "cert");
241 c705ecb1 2021-05-03 op host->cert = ensure_absolute_path($2);
242 c705ecb1 2021-05-03 op }
243 98f52178 2021-06-29 op | TCGI string {
244 c705ecb1 2021-05-03 op only_once(host->cgi, "cgi");
245 15902770 2021-01-15 op /* drop the starting '/', if any */
246 709f4c94 2021-02-04 op if (*$2 == '/')
247 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
248 709f4c94 2021-02-04 op host->cgi = $2;
249 05c23a54 2021-01-19 op }
250 98f52178 2021-06-29 op | TENTRYPOINT string {
251 c705ecb1 2021-05-03 op only_once(host->entrypoint, "entrypoint");
252 e3ddf390 2021-02-06 op while (*$2 == '/')
253 e3ddf390 2021-02-06 op memmove($2, $2+1, strlen($2));
254 e3ddf390 2021-02-06 op host->entrypoint = $2;
255 e3ddf390 2021-02-06 op }
256 efacb859 2021-07-09 op | TENV string '=' string {
257 762b9b99 2021-07-09 op add_param($2, $4, 1);
258 9cc630aa 2021-04-28 op }
259 98f52178 2021-06-29 op | TKEY string {
260 c705ecb1 2021-05-03 op only_once(host->key, "key");
261 c705ecb1 2021-05-03 op host->key = ensure_absolute_path($2);
262 c92b802b 2021-06-11 op }
263 efacb859 2021-07-09 op | TPARAM string '=' string {
264 762b9b99 2021-07-09 op add_param($2, $4, 0);
265 c705ecb1 2021-05-03 op }
266 c8b74339 2021-01-24 op | locopt
267 c8b74339 2021-01-24 op ;
268 c8b74339 2021-01-24 op
269 c8b74339 2021-01-24 op locations : /* empty */
270 c39be742 2021-07-09 op | locations location optnl
271 c8b74339 2021-01-24 op ;
272 c8b74339 2021-01-24 op
273 c39be742 2021-07-09 op location : TLOCATION { advance_loc(); } string '{' optnl locopts '}' {
274 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
275 49b73ba1 2021-02-10 op if (*$3 == '/')
276 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
277 49b73ba1 2021-02-10 op loc->match = $3;
278 6119e13e 2021-01-19 op }
279 c8b74339 2021-01-24 op | error '}'
280 c8b74339 2021-01-24 op ;
281 c8b74339 2021-01-24 op
282 c8b74339 2021-01-24 op locopts : /* empty */
283 c39be742 2021-07-09 op | locopts locopt optnl
284 c8b74339 2021-01-24 op ;
285 c8b74339 2021-01-24 op
286 c39be742 2021-07-09 op locopt : TAUTO TINDEX bool { loc->auto_index = $3 ? 1 : -1; }
287 c39be742 2021-07-09 op | TBLOCK TRETURN NUM string {
288 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
289 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
290 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
291 6abda252 2021-02-06 op }
292 c39be742 2021-07-09 op | TBLOCK TRETURN NUM {
293 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
294 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
295 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
296 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
297 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
298 6abda252 2021-02-06 op }
299 6abda252 2021-02-06 op | TBLOCK {
300 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
301 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
302 6abda252 2021-02-06 op loc->block_code = 40;
303 6abda252 2021-02-06 op }
304 98f52178 2021-06-29 op | TDEFAULT TTYPE string {
305 c705ecb1 2021-05-03 op only_once(loc->default_mime, "default type");
306 eb59f87e 2021-02-09 op loc->default_mime = $3;
307 eb59f87e 2021-02-09 op }
308 0d047efc 2021-05-24 op | TFASTCGI fastcgi
309 98f52178 2021-06-29 op | TINDEX string {
310 c705ecb1 2021-05-03 op only_once(loc->index, "index");
311 eb59f87e 2021-02-09 op loc->index = $2;
312 eb59f87e 2021-02-09 op }
313 98f52178 2021-06-29 op | TLANG string {
314 c705ecb1 2021-05-03 op only_once(loc->lang, "lang");
315 eb59f87e 2021-02-09 op loc->lang = $2;
316 eb59f87e 2021-02-09 op }
317 c39be742 2021-07-09 op | TLOG bool { loc->disable_log = !$2; }
318 98f52178 2021-06-29 op | TREQUIRE TCLIENT TCA string {
319 c705ecb1 2021-05-03 op only_once(loc->reqca, "require client ca");
320 adbe6a64 2021-04-30 op ensure_absolute_path($4);
321 02be96c6 2021-02-09 op if ((loc->reqca = load_ca($4)) == NULL)
322 02be96c6 2021-02-09 op yyerror("couldn't load ca cert: %s", $4);
323 02be96c6 2021-02-09 op free($4);
324 fdea6aa0 2021-04-30 op }
325 98f52178 2021-06-29 op | TROOT string {
326 c705ecb1 2021-05-03 op only_once(loc->dir, "root");
327 fdea6aa0 2021-04-30 op loc->dir = ensure_absolute_path($2);
328 02be96c6 2021-02-09 op }
329 c39be742 2021-07-09 op | TSTRIP NUM { loc->strip = check_strip_no($2); }
330 15902770 2021-01-15 op ;
331 13ed2fb6 2021-01-27 op
332 98f52178 2021-06-29 op fastcgi : TSPAWN string {
333 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
334 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf(NULL, NULL, $2);
335 0d047efc 2021-05-24 op }
336 98f52178 2021-06-29 op | string {
337 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
338 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($1, NULL, NULL);
339 0d047efc 2021-05-24 op }
340 c39be742 2021-07-09 op | TTCP string TPORT NUM {
341 0d047efc 2021-05-24 op char *c;
342 762b9b99 2021-07-09 op if (asprintf(&c, "%d", $4) == -1)
343 0d047efc 2021-05-24 op err(1, "asprintf");
344 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
345 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, c, NULL);
346 0d047efc 2021-05-24 op }
347 98f52178 2021-06-29 op | TTCP string {
348 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
349 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
350 0d047efc 2021-05-24 op }
351 762b9b99 2021-07-09 op | TTCP string TPORT string {
352 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
353 762b9b99 2021-07-09 op loc->fcgi = fastcgi_conf($2, $4, NULL);
354 0d047efc 2021-05-24 op }
355 0d047efc 2021-05-24 op ;
356 0d047efc 2021-05-24 op
357 c39be742 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
358 67f49405 2021-07-09 op | ';' optnl /* semicolons too */
359 c39be742 2021-07-09 op | /*empty*/
360 c39be742 2021-07-09 op ;
361 fdea6aa0 2021-04-30 op
362 c39be742 2021-07-09 op %%
363 b8e64ccd 2021-03-31 op
364 74f0778b 2021-06-16 op static struct keyword {
365 74f0778b 2021-06-16 op const char *word;
366 74f0778b 2021-06-16 op int token;
367 74f0778b 2021-06-16 op } keywords[] = {
368 d93c8191 2021-07-09 op /* these MUST be sorted */
369 74f0778b 2021-06-16 op {"alias", TALIAS},
370 74f0778b 2021-06-16 op {"auto", TAUTO},
371 74f0778b 2021-06-16 op {"block", TBLOCK},
372 74f0778b 2021-06-16 op {"ca", TCA},
373 74f0778b 2021-06-16 op {"cert", TCERT},
374 74f0778b 2021-06-16 op {"cgi", TCGI},
375 74f0778b 2021-06-16 op {"chroot", TCHROOT},
376 74f0778b 2021-06-16 op {"client", TCLIENT},
377 74f0778b 2021-06-16 op {"default", TDEFAULT},
378 74f0778b 2021-06-16 op {"entrypoint", TENTRYPOINT},
379 74f0778b 2021-06-16 op {"env", TENV},
380 74f0778b 2021-06-16 op {"fastcgi", TFASTCGI},
381 74f0778b 2021-06-16 op {"index", TINDEX},
382 74f0778b 2021-06-16 op {"ipv6", TIPV6},
383 74f0778b 2021-06-16 op {"key", TKEY},
384 74f0778b 2021-06-16 op {"lang", TLANG},
385 74f0778b 2021-06-16 op {"location", TLOCATION},
386 74f0778b 2021-06-16 op {"log", TLOG},
387 d19951cf 2021-07-09 op {"map", TMAP},
388 74f0778b 2021-06-16 op {"mime", TMIME},
389 c39be742 2021-07-09 op {"off", TOFF},
390 c39be742 2021-07-09 op {"on", TON},
391 74f0778b 2021-06-16 op {"param", TPARAM},
392 74f0778b 2021-06-16 op {"port", TPORT},
393 74f0778b 2021-06-16 op {"prefork", TPREFORK},
394 74f0778b 2021-06-16 op {"protocols", TPROTOCOLS},
395 74f0778b 2021-06-16 op {"require", TREQUIRE},
396 74f0778b 2021-06-16 op {"return", TRETURN},
397 74f0778b 2021-06-16 op {"root", TROOT},
398 74f0778b 2021-06-16 op {"server", TSERVER},
399 74f0778b 2021-06-16 op {"spawn", TSPAWN},
400 74f0778b 2021-06-16 op {"strip", TSTRIP},
401 74f0778b 2021-06-16 op {"tcp", TTCP},
402 d19951cf 2021-07-09 op {"to-ext", TTOEXT},
403 74f0778b 2021-06-16 op {"type", TTYPE},
404 74f0778b 2021-06-16 op {"user", TUSER},
405 74f0778b 2021-06-16 op };
406 74f0778b 2021-06-16 op
407 c39be742 2021-07-09 op void
408 c39be742 2021-07-09 op yyerror(const char *msg, ...)
409 c39be742 2021-07-09 op {
410 c39be742 2021-07-09 op va_list ap;
411 c39be742 2021-07-09 op
412 c39be742 2021-07-09 op file->errors++;
413 c39be742 2021-07-09 op
414 c39be742 2021-07-09 op va_start(ap, msg);
415 f3966209 2021-07-13 op fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
416 f3966209 2021-07-13 op vfprintf(stderr, msg, ap);
417 f3966209 2021-07-13 op fprintf(stderr, "\n");
418 f3966209 2021-07-13 op va_end(ap);
419 f3966209 2021-07-13 op }
420 f3966209 2021-07-13 op
421 f3966209 2021-07-13 op void
422 f3966209 2021-07-13 op yywarn(const char *msg, ...)
423 f3966209 2021-07-13 op {
424 f3966209 2021-07-13 op va_list ap;
425 f3966209 2021-07-13 op
426 f3966209 2021-07-13 op va_start(ap, msg);
427 f3966209 2021-07-13 op fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
428 c39be742 2021-07-09 op vfprintf(stderr, msg, ap);
429 c39be742 2021-07-09 op fprintf(stderr, "\n");
430 c39be742 2021-07-09 op va_end(ap);
431 c39be742 2021-07-09 op }
432 c39be742 2021-07-09 op
433 d93c8191 2021-07-09 op int
434 d93c8191 2021-07-09 op kw_cmp(const void *k, const void *e)
435 d93c8191 2021-07-09 op {
436 d93c8191 2021-07-09 op return strcmp(k, ((struct keyword *)e)->word);
437 d93c8191 2021-07-09 op }
438 d93c8191 2021-07-09 op
439 c39be742 2021-07-09 op int
440 c39be742 2021-07-09 op lookup(char *s)
441 74f0778b 2021-06-16 op {
442 c39be742 2021-07-09 op const struct keyword *p;
443 74f0778b 2021-06-16 op
444 c39be742 2021-07-09 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
445 c39be742 2021-07-09 op sizeof(keywords[0]), kw_cmp);
446 74f0778b 2021-06-16 op
447 c39be742 2021-07-09 op if (p)
448 c39be742 2021-07-09 op return p->token;
449 c39be742 2021-07-09 op else
450 c39be742 2021-07-09 op return STRING;
451 c39be742 2021-07-09 op }
452 c39be742 2021-07-09 op
453 c39be742 2021-07-09 op #define START_EXPAND 1
454 c39be742 2021-07-09 op #define DONE_EXPAND 2
455 c39be742 2021-07-09 op
456 c39be742 2021-07-09 op static int expanding;
457 c39be742 2021-07-09 op
458 c39be742 2021-07-09 op int
459 c39be742 2021-07-09 op igetc(void)
460 c39be742 2021-07-09 op {
461 c39be742 2021-07-09 op int c;
462 c39be742 2021-07-09 op
463 c39be742 2021-07-09 op while (1) {
464 c39be742 2021-07-09 op if (file->ungetpos > 0)
465 c39be742 2021-07-09 op c = file->ungetbuf[--file->ungetpos];
466 c39be742 2021-07-09 op else
467 c39be742 2021-07-09 op c = getc(file->stream);
468 c39be742 2021-07-09 op
469 c39be742 2021-07-09 op if (c == START_EXPAND)
470 c39be742 2021-07-09 op expanding = 1;
471 c39be742 2021-07-09 op else if (c == DONE_EXPAND)
472 c39be742 2021-07-09 op expanding = 0;
473 c39be742 2021-07-09 op else
474 c39be742 2021-07-09 op break;
475 74f0778b 2021-06-16 op }
476 c39be742 2021-07-09 op return c;
477 c39be742 2021-07-09 op }
478 74f0778b 2021-06-16 op
479 c39be742 2021-07-09 op int
480 c39be742 2021-07-09 op lgetc(int quotec)
481 c39be742 2021-07-09 op {
482 c39be742 2021-07-09 op int c, next;
483 c39be742 2021-07-09 op
484 c39be742 2021-07-09 op if (quotec) {
485 c39be742 2021-07-09 op if ((c = igetc()) == EOF) {
486 c39be742 2021-07-09 op yyerror("reached end of file while parsing "
487 c39be742 2021-07-09 op "quoted string");
488 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
489 c39be742 2021-07-09 op return EOF;
490 c39be742 2021-07-09 op return quotec;
491 c39be742 2021-07-09 op }
492 74f0778b 2021-06-16 op return c;
493 74f0778b 2021-06-16 op }
494 74f0778b 2021-06-16 op
495 c39be742 2021-07-09 op while ((c = igetc()) == '\\') {
496 c39be742 2021-07-09 op next = igetc();
497 c39be742 2021-07-09 op if (next != '\n') {
498 c39be742 2021-07-09 op c = next;
499 74f0778b 2021-06-16 op break;
500 c39be742 2021-07-09 op }
501 c39be742 2021-07-09 op yylval.lineno = file->lineno;
502 c39be742 2021-07-09 op file->lineno++;
503 c39be742 2021-07-09 op }
504 3b21cca3 2021-06-29 op
505 c39be742 2021-07-09 op if (c == EOF) {
506 c39be742 2021-07-09 op /*
507 c39be742 2021-07-09 op * Fake EOL when hit EOF for the first time. This gets line
508 c39be742 2021-07-09 op * count right if last line in included file is syntactically
509 c39be742 2021-07-09 op * invalid and has no newline.
510 c39be742 2021-07-09 op */
511 c39be742 2021-07-09 op if (file->eof_reached == 0) {
512 c39be742 2021-07-09 op file->eof_reached = 1;
513 c39be742 2021-07-09 op return '\n';
514 c39be742 2021-07-09 op }
515 c39be742 2021-07-09 op while (c == EOF) {
516 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
517 c39be742 2021-07-09 op return EOF;
518 c39be742 2021-07-09 op c = igetc();
519 c39be742 2021-07-09 op }
520 c39be742 2021-07-09 op }
521 c39be742 2021-07-09 op return c;
522 c39be742 2021-07-09 op }
523 c39be742 2021-07-09 op
524 c39be742 2021-07-09 op void
525 c39be742 2021-07-09 op lungetc(int c)
526 c39be742 2021-07-09 op {
527 c39be742 2021-07-09 op if (c == EOF)
528 c39be742 2021-07-09 op return;
529 c39be742 2021-07-09 op
530 c39be742 2021-07-09 op if (file->ungetpos >= file->ungetsize) {
531 c39be742 2021-07-09 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
532 c39be742 2021-07-09 op if (p == NULL)
533 c39be742 2021-07-09 op err(1, "lungetc");
534 c39be742 2021-07-09 op file->ungetbuf = p;
535 c39be742 2021-07-09 op file->ungetsize *= 2;
536 c39be742 2021-07-09 op }
537 c39be742 2021-07-09 op file->ungetbuf[file->ungetpos++] = c;
538 c39be742 2021-07-09 op }
539 c39be742 2021-07-09 op
540 c39be742 2021-07-09 op int
541 c39be742 2021-07-09 op findeol(void)
542 c39be742 2021-07-09 op {
543 c39be742 2021-07-09 op int c;
544 c39be742 2021-07-09 op
545 c39be742 2021-07-09 op /* Skip to either EOF or the first real EOL. */
546 c39be742 2021-07-09 op while (1) {
547 c39be742 2021-07-09 op c = lgetc(0);
548 c39be742 2021-07-09 op if (c == '\n') {
549 c39be742 2021-07-09 op file->lineno++;
550 3b21cca3 2021-06-29 op break;
551 c39be742 2021-07-09 op }
552 c39be742 2021-07-09 op if (c == EOF)
553 74f0778b 2021-06-16 op break;
554 c39be742 2021-07-09 op }
555 c39be742 2021-07-09 op return ERROR;
556 c39be742 2021-07-09 op }
557 c39be742 2021-07-09 op
558 c39be742 2021-07-09 op int
559 c39be742 2021-07-09 op yylex(void)
560 c39be742 2021-07-09 op {
561 1bd706dc 2021-07-09 op char buf[8096];
562 1bd706dc 2021-07-09 op char *p, *val;
563 1bd706dc 2021-07-09 op int quotec, next, c;
564 1bd706dc 2021-07-09 op int token;
565 c39be742 2021-07-09 op
566 c39be742 2021-07-09 op top:
567 c39be742 2021-07-09 op p = buf;
568 c39be742 2021-07-09 op while ((c = lgetc(0)) == ' ' || c == '\t')
569 c39be742 2021-07-09 op ; /* nothing */
570 c39be742 2021-07-09 op
571 c39be742 2021-07-09 op yylval.lineno = file->lineno;
572 c39be742 2021-07-09 op if (c == '#')
573 c39be742 2021-07-09 op while ((c = lgetc(0)) != '\n' && c != EOF)
574 c39be742 2021-07-09 op ; /* nothing */
575 c39be742 2021-07-09 op if (c == '$' && !expanding) {
576 c39be742 2021-07-09 op while (1) {
577 c39be742 2021-07-09 op if ((c = lgetc(0)) == EOF)
578 c39be742 2021-07-09 op return 0;
579 67f49405 2021-07-09 op if (p + 1 >= buf + sizeof(buf) -1) {
580 67f49405 2021-07-09 op yyerror("string too long");
581 67f49405 2021-07-09 op return findeol();
582 67f49405 2021-07-09 op }
583 67f49405 2021-07-09 op if (isalnum(c) || c == '_') {
584 67f49405 2021-07-09 op *p++ = c;
585 67f49405 2021-07-09 op continue;
586 67f49405 2021-07-09 op }
587 67f49405 2021-07-09 op *p = '\0';
588 67f49405 2021-07-09 op lungetc(c);
589 67f49405 2021-07-09 op break;
590 67f49405 2021-07-09 op }
591 67f49405 2021-07-09 op val = symget(buf);
592 67f49405 2021-07-09 op if (val == NULL) {
593 67f49405 2021-07-09 op yyerror("macro `%s' not defined", buf);
594 67f49405 2021-07-09 op return findeol();
595 67f49405 2021-07-09 op }
596 67f49405 2021-07-09 op yylval.v.string = xstrdup(val);
597 67f49405 2021-07-09 op return STRING;
598 67f49405 2021-07-09 op }
599 67f49405 2021-07-09 op if (c == '@' && !expanding) {
600 67f49405 2021-07-09 op while (1) {
601 67f49405 2021-07-09 op if ((c = lgetc(0)) == EOF)
602 67f49405 2021-07-09 op return 0;
603 c39be742 2021-07-09 op
604 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
605 c39be742 2021-07-09 op yyerror("string too long");
606 c39be742 2021-07-09 op return findeol();
607 c39be742 2021-07-09 op }
608 c39be742 2021-07-09 op if (isalnum(c) || c == '_') {
609 c39be742 2021-07-09 op *p++ = c;
610 74f0778b 2021-06-16 op continue;
611 74f0778b 2021-06-16 op }
612 c39be742 2021-07-09 op *p = '\0';
613 c39be742 2021-07-09 op lungetc(c);
614 c39be742 2021-07-09 op break;
615 74f0778b 2021-06-16 op }
616 c39be742 2021-07-09 op val = symget(buf);
617 c39be742 2021-07-09 op if (val == NULL) {
618 c39be742 2021-07-09 op yyerror("macro '%s' not defined", buf);
619 c39be742 2021-07-09 op return findeol();
620 74f0778b 2021-06-16 op }
621 c39be742 2021-07-09 op p = val + strlen(val) - 1;
622 c39be742 2021-07-09 op lungetc(DONE_EXPAND);
623 c39be742 2021-07-09 op while (p >= val) {
624 c39be742 2021-07-09 op lungetc(*p);
625 c39be742 2021-07-09 op p--;
626 c39be742 2021-07-09 op }
627 c39be742 2021-07-09 op lungetc(START_EXPAND);
628 c39be742 2021-07-09 op goto top;
629 74f0778b 2021-06-16 op }
630 74f0778b 2021-06-16 op
631 c39be742 2021-07-09 op switch (c) {
632 c39be742 2021-07-09 op case '\'':
633 c39be742 2021-07-09 op case '"':
634 c39be742 2021-07-09 op quotec = c;
635 c39be742 2021-07-09 op while (1) {
636 c39be742 2021-07-09 op if ((c = lgetc(quotec)) == EOF)
637 c39be742 2021-07-09 op return 0;
638 c39be742 2021-07-09 op if (c == '\n') {
639 c39be742 2021-07-09 op file->lineno++;
640 c39be742 2021-07-09 op continue;
641 c39be742 2021-07-09 op } else if (c == '\\') {
642 c39be742 2021-07-09 op if ((next = lgetc(quotec)) == EOF)
643 c39be742 2021-07-09 op return (0);
644 c39be742 2021-07-09 op if (next == quotec || next == ' ' ||
645 c39be742 2021-07-09 op next == '\t')
646 c39be742 2021-07-09 op c = next;
647 c39be742 2021-07-09 op else if (next == '\n') {
648 c39be742 2021-07-09 op file->lineno++;
649 c39be742 2021-07-09 op continue;
650 c39be742 2021-07-09 op } else
651 c39be742 2021-07-09 op lungetc(next);
652 c39be742 2021-07-09 op } else if (c == quotec) {
653 c39be742 2021-07-09 op *p = '\0';
654 c39be742 2021-07-09 op break;
655 c39be742 2021-07-09 op } else if (c == '\0') {
656 f3966209 2021-07-13 op yyerror("invalid syntax");
657 c39be742 2021-07-09 op return findeol();
658 c39be742 2021-07-09 op }
659 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
660 c39be742 2021-07-09 op yyerror("string too long");
661 c39be742 2021-07-09 op return findeol();
662 c39be742 2021-07-09 op }
663 c39be742 2021-07-09 op *p++ = c;
664 c39be742 2021-07-09 op }
665 c39be742 2021-07-09 op yylval.v.string = strdup(buf);
666 c39be742 2021-07-09 op if (yylval.v.string == NULL)
667 c39be742 2021-07-09 op err(1, "yylex: strdup");
668 c39be742 2021-07-09 op return STRING;
669 74f0778b 2021-06-16 op }
670 c39be742 2021-07-09 op
671 c39be742 2021-07-09 op #define allowed_to_end_number(x) \
672 c39be742 2021-07-09 op (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
673 c39be742 2021-07-09 op
674 c39be742 2021-07-09 op if (c == '-' || isdigit(c)) {
675 c39be742 2021-07-09 op do {
676 c39be742 2021-07-09 op *p++ = c;
677 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
678 c39be742 2021-07-09 op yyerror("string too long");
679 c39be742 2021-07-09 op return findeol();
680 c39be742 2021-07-09 op }
681 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && isdigit(c));
682 c39be742 2021-07-09 op lungetc(c);
683 c39be742 2021-07-09 op if (p == buf + 1 && buf[0] == '-')
684 c39be742 2021-07-09 op goto nodigits;
685 c39be742 2021-07-09 op if (c == EOF || allowed_to_end_number(c)) {
686 c39be742 2021-07-09 op const char *errstr = NULL;
687 c39be742 2021-07-09 op
688 c39be742 2021-07-09 op *p = '\0';
689 c39be742 2021-07-09 op yylval.v.number = strtonum(buf, LLONG_MIN,
690 c39be742 2021-07-09 op LLONG_MAX, &errstr);
691 c39be742 2021-07-09 op if (errstr) {
692 c39be742 2021-07-09 op yyerror("\"%s\" invalid number: %s",
693 c39be742 2021-07-09 op buf, errstr);
694 c39be742 2021-07-09 op return findeol();
695 c39be742 2021-07-09 op }
696 c39be742 2021-07-09 op return NUM;
697 c39be742 2021-07-09 op } else {
698 c39be742 2021-07-09 op nodigits:
699 c39be742 2021-07-09 op while (p > buf + 1)
700 c39be742 2021-07-09 op lungetc(*--p);
701 c39be742 2021-07-09 op c = *--p;
702 c39be742 2021-07-09 op if (c == '-')
703 c39be742 2021-07-09 op return c;
704 c39be742 2021-07-09 op }
705 74f0778b 2021-06-16 op }
706 c39be742 2021-07-09 op
707 c39be742 2021-07-09 op #define allowed_in_string(x) \
708 c39be742 2021-07-09 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
709 c39be742 2021-07-09 op x != '{' && x != '}' && \
710 c39be742 2021-07-09 op x != '!' && x != '=' && x != '#' && \
711 67f49405 2021-07-09 op x != ',' && x != ';'))
712 c39be742 2021-07-09 op
713 67f49405 2021-07-09 op if (isalnum(c) || c == ':' || c == '_') {
714 c39be742 2021-07-09 op do {
715 c39be742 2021-07-09 op *p++ = c;
716 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
717 c39be742 2021-07-09 op yyerror("string too long");
718 c39be742 2021-07-09 op return findeol();
719 c39be742 2021-07-09 op }
720 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
721 c39be742 2021-07-09 op lungetc(c);
722 c39be742 2021-07-09 op *p = '\0';
723 c39be742 2021-07-09 op if ((token = lookup(buf)) == STRING)
724 c39be742 2021-07-09 op yylval.v.string = xstrdup(buf);
725 c39be742 2021-07-09 op return token;
726 74f0778b 2021-06-16 op }
727 c39be742 2021-07-09 op if (c == '\n') {
728 c39be742 2021-07-09 op yylval.lineno = file->lineno;
729 c39be742 2021-07-09 op file->lineno++;
730 74f0778b 2021-06-16 op }
731 c39be742 2021-07-09 op if (c == EOF)
732 c39be742 2021-07-09 op return 0;
733 c39be742 2021-07-09 op return c;
734 c39be742 2021-07-09 op }
735 c39be742 2021-07-09 op
736 c39be742 2021-07-09 op struct file *
737 c39be742 2021-07-09 op pushfile(const char *name, int secret)
738 c39be742 2021-07-09 op {
739 c39be742 2021-07-09 op struct file *nfile;
740 c39be742 2021-07-09 op
741 c39be742 2021-07-09 op nfile = xcalloc(1, sizeof(*nfile));
742 c39be742 2021-07-09 op nfile->name = xstrdup(name);
743 c39be742 2021-07-09 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
744 c39be742 2021-07-09 op yyerror("can't open %s: %s", nfile->name,
745 c39be742 2021-07-09 op strerror(errno));
746 c39be742 2021-07-09 op free(nfile->name);
747 c39be742 2021-07-09 op free(nfile);
748 c39be742 2021-07-09 op return NULL;
749 74f0778b 2021-06-16 op }
750 c39be742 2021-07-09 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
751 c39be742 2021-07-09 op nfile->ungetsize = 16;
752 c39be742 2021-07-09 op nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
753 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&files, nfile, entry);
754 c39be742 2021-07-09 op return nfile;
755 c39be742 2021-07-09 op }
756 74f0778b 2021-06-16 op
757 c39be742 2021-07-09 op int
758 c39be742 2021-07-09 op popfile(void)
759 c39be742 2021-07-09 op {
760 c39be742 2021-07-09 op struct file *prev;
761 13ed2fb6 2021-01-27 op
762 c39be742 2021-07-09 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
763 c39be742 2021-07-09 op prev->errors += file->errors;
764 13ed2fb6 2021-01-27 op
765 c39be742 2021-07-09 op TAILQ_REMOVE(&files, file, entry);
766 c39be742 2021-07-09 op fclose(file->stream);
767 c39be742 2021-07-09 op free(file->name);
768 c39be742 2021-07-09 op free(file->ungetbuf);
769 c39be742 2021-07-09 op free(file);
770 c39be742 2021-07-09 op file = prev;
771 c39be742 2021-07-09 op return file ? 0 : EOF;
772 13ed2fb6 2021-01-27 op }
773 13ed2fb6 2021-01-27 op
774 13ed2fb6 2021-01-27 op void
775 c39be742 2021-07-09 op parse_conf(const char *filename)
776 13ed2fb6 2021-01-27 op {
777 c39be742 2021-07-09 op struct sym *sym, *next;
778 3b21cca3 2021-06-29 op
779 c39be742 2021-07-09 op file = pushfile(filename, 0);
780 c39be742 2021-07-09 op if (file == NULL)
781 c39be742 2021-07-09 op return;
782 c39be742 2021-07-09 op topfile = file;
783 c39be742 2021-07-09 op
784 13ed2fb6 2021-01-27 op yyparse();
785 c39be742 2021-07-09 op errors = file->errors;
786 c39be742 2021-07-09 op popfile();
787 13ed2fb6 2021-01-27 op
788 c39be742 2021-07-09 op /* Free macros and check which have not been used. */
789 3b21cca3 2021-06-29 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
790 3b21cca3 2021-06-29 op /* TODO: warn if !sym->used */
791 3b21cca3 2021-06-29 op if (!sym->persist) {
792 3b21cca3 2021-06-29 op free(sym->name);
793 3b21cca3 2021-06-29 op free(sym->val);
794 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
795 3b21cca3 2021-06-29 op free(sym);
796 3b21cca3 2021-06-29 op }
797 3b21cca3 2021-06-29 op }
798 c39be742 2021-07-09 op
799 c39be742 2021-07-09 op if (errors)
800 c39be742 2021-07-09 op exit(1);
801 13ed2fb6 2021-01-27 op }
802 e17642a7 2021-02-01 op
803 c39be742 2021-07-09 op int
804 c39be742 2021-07-09 op symset(const char *name, const char *val, int persist)
805 c39be742 2021-07-09 op {
806 c39be742 2021-07-09 op struct sym *sym;
807 c39be742 2021-07-09 op
808 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
809 c39be742 2021-07-09 op if (!strcmp(name, sym->name))
810 c39be742 2021-07-09 op break;
811 c39be742 2021-07-09 op }
812 c39be742 2021-07-09 op
813 c39be742 2021-07-09 op if (sym != NULL) {
814 c39be742 2021-07-09 op if (sym->persist)
815 c39be742 2021-07-09 op return 0;
816 c39be742 2021-07-09 op else {
817 c39be742 2021-07-09 op free(sym->name);
818 c39be742 2021-07-09 op free(sym->val);
819 c39be742 2021-07-09 op TAILQ_REMOVE(&symhead, sym, entry);
820 c39be742 2021-07-09 op free(sym);
821 c39be742 2021-07-09 op }
822 c39be742 2021-07-09 op }
823 c39be742 2021-07-09 op
824 c39be742 2021-07-09 op sym = xcalloc(1, sizeof(*sym));
825 c39be742 2021-07-09 op sym->name = xstrdup(name);
826 c39be742 2021-07-09 op sym->val = xstrdup(val);
827 c39be742 2021-07-09 op sym->used = 0;
828 c39be742 2021-07-09 op sym->persist = persist;
829 c39be742 2021-07-09 op
830 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
831 c39be742 2021-07-09 op return 0;
832 c39be742 2021-07-09 op }
833 c39be742 2021-07-09 op
834 c39be742 2021-07-09 op int
835 c39be742 2021-07-09 op cmdline_symset(char *s)
836 c39be742 2021-07-09 op {
837 c39be742 2021-07-09 op char *sym, *val;
838 c39be742 2021-07-09 op int ret;
839 c39be742 2021-07-09 op
840 c39be742 2021-07-09 op if ((val = strrchr(s, '=')) == NULL)
841 c39be742 2021-07-09 op return -1;
842 c39be742 2021-07-09 op sym = xcalloc(1, val - s + 1);
843 c39be742 2021-07-09 op memcpy(sym, s, val - s);
844 c39be742 2021-07-09 op ret = symset(sym, val + 1, 1);
845 c39be742 2021-07-09 op free(sym);
846 c39be742 2021-07-09 op return ret;
847 c39be742 2021-07-09 op }
848 c39be742 2021-07-09 op
849 e17642a7 2021-02-01 op char *
850 c39be742 2021-07-09 op symget(const char *nam)
851 c39be742 2021-07-09 op {
852 c39be742 2021-07-09 op struct sym *sym;
853 c39be742 2021-07-09 op
854 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
855 c39be742 2021-07-09 op if (strcmp(nam, sym->name) == 0) {
856 c39be742 2021-07-09 op sym->used = 1;
857 c39be742 2021-07-09 op return sym->val;
858 c39be742 2021-07-09 op }
859 c39be742 2021-07-09 op }
860 c39be742 2021-07-09 op return NULL;
861 c39be742 2021-07-09 op }
862 c39be742 2021-07-09 op
863 c39be742 2021-07-09 op struct vhost *
864 c39be742 2021-07-09 op new_vhost(void)
865 c39be742 2021-07-09 op {
866 c39be742 2021-07-09 op return xcalloc(1, sizeof(struct vhost));
867 c39be742 2021-07-09 op }
868 c39be742 2021-07-09 op
869 c39be742 2021-07-09 op struct location *
870 c39be742 2021-07-09 op new_location(void)
871 c39be742 2021-07-09 op {
872 c39be742 2021-07-09 op struct location *l;
873 c39be742 2021-07-09 op
874 c39be742 2021-07-09 op l = xcalloc(1, sizeof(*l));
875 c39be742 2021-07-09 op l->dirfd = -1;
876 c39be742 2021-07-09 op l->fcgi = -1;
877 c39be742 2021-07-09 op return l;
878 c39be742 2021-07-09 op }
879 c39be742 2021-07-09 op
880 c39be742 2021-07-09 op char *
881 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
882 e17642a7 2021-02-01 op {
883 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
884 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
885 e17642a7 2021-02-01 op return path;
886 e17642a7 2021-02-01 op }
887 6abda252 2021-02-06 op
888 6abda252 2021-02-06 op int
889 6abda252 2021-02-06 op check_block_code(int n)
890 6abda252 2021-02-06 op {
891 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
892 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
893 6abda252 2021-02-06 op return n;
894 6abda252 2021-02-06 op }
895 6abda252 2021-02-06 op
896 6abda252 2021-02-06 op char *
897 6abda252 2021-02-06 op check_block_fmt(char *fmt)
898 6abda252 2021-02-06 op {
899 6abda252 2021-02-06 op char *s;
900 6abda252 2021-02-06 op
901 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
902 6abda252 2021-02-06 op if (*s != '%')
903 6abda252 2021-02-06 op continue;
904 6abda252 2021-02-06 op switch (*++s) {
905 6abda252 2021-02-06 op case '%':
906 6abda252 2021-02-06 op case 'p':
907 6abda252 2021-02-06 op case 'q':
908 6abda252 2021-02-06 op case 'P':
909 6abda252 2021-02-06 op case 'N':
910 6abda252 2021-02-06 op break;
911 6abda252 2021-02-06 op default:
912 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
913 6abda252 2021-02-06 op }
914 6abda252 2021-02-06 op }
915 6abda252 2021-02-06 op
916 6abda252 2021-02-06 op return fmt;
917 6abda252 2021-02-06 op }
918 6abda252 2021-02-06 op
919 6abda252 2021-02-06 op int
920 6abda252 2021-02-06 op check_strip_no(int n)
921 6abda252 2021-02-06 op {
922 6abda252 2021-02-06 op if (n <= 0)
923 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
924 a709ddf5 2021-02-07 op return n;
925 a709ddf5 2021-02-07 op }
926 a709ddf5 2021-02-07 op
927 a709ddf5 2021-02-07 op int
928 391825e3 2021-07-09 op check_port_num(int n)
929 391825e3 2021-07-09 op {
930 391825e3 2021-07-09 op if (n <= 0 || n >= UINT16_MAX)
931 391825e3 2021-07-09 op yyerror("port number is %s: %d",
932 391825e3 2021-07-09 op n <= 0 ? "too small" : "too large",
933 391825e3 2021-07-09 op n);
934 391825e3 2021-07-09 op return n;
935 391825e3 2021-07-09 op }
936 391825e3 2021-07-09 op
937 391825e3 2021-07-09 op int
938 a709ddf5 2021-02-07 op check_prefork_num(int n)
939 a709ddf5 2021-02-07 op {
940 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
941 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
942 6abda252 2021-02-06 op return n;
943 6abda252 2021-02-06 op }
944 49b73ba1 2021-02-10 op
945 49b73ba1 2021-02-10 op void
946 49b73ba1 2021-02-10 op advance_loc(void)
947 49b73ba1 2021-02-10 op {
948 b8e64ccd 2021-03-31 op loc = new_location();
949 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
950 49b73ba1 2021-02-10 op }
951 c705ecb1 2021-05-03 op
952 c705ecb1 2021-05-03 op void
953 c705ecb1 2021-05-03 op only_once(const void *ptr, const char *name)
954 c705ecb1 2021-05-03 op {
955 c705ecb1 2021-05-03 op if (ptr != NULL)
956 c705ecb1 2021-05-03 op yyerror("`%s' specified more than once", name);
957 c705ecb1 2021-05-03 op }
958 8ad1c570 2021-05-09 op
959 8ad1c570 2021-05-09 op void
960 8ad1c570 2021-05-09 op only_oncei(int i, const char *name)
961 8ad1c570 2021-05-09 op {
962 8ad1c570 2021-05-09 op if (i != -1)
963 8ad1c570 2021-05-09 op yyerror("`%s' specified more than once", name);
964 8ad1c570 2021-05-09 op }
965 8ad1c570 2021-05-09 op
966 8ad1c570 2021-05-09 op int
967 8ad1c570 2021-05-09 op fastcgi_conf(char *path, char *port, char *prog)
968 8ad1c570 2021-05-09 op {
969 8ad1c570 2021-05-09 op struct fcgi *f;
970 8ad1c570 2021-05-09 op int i;
971 8ad1c570 2021-05-09 op
972 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
973 8ad1c570 2021-05-09 op f = &fcgi[i];
974 fafc6849 2021-06-29 op
975 8ad1c570 2021-05-09 op if (f->path == NULL) {
976 8ad1c570 2021-05-09 op f->id = i;
977 8ad1c570 2021-05-09 op f->path = path;
978 8ad1c570 2021-05-09 op f->port = port;
979 8ad1c570 2021-05-09 op f->prog = prog;
980 8ad1c570 2021-05-09 op return i;
981 8ad1c570 2021-05-09 op }
982 8ad1c570 2021-05-09 op
983 8ad1c570 2021-05-09 op /* XXX: what to do with prog? */
984 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
985 8ad1c570 2021-05-09 op ((port == NULL && f->port == NULL) ||
986 8ad1c570 2021-05-09 op !strcmp(f->port, port))) {
987 8ad1c570 2021-05-09 op free(path);
988 8ad1c570 2021-05-09 op free(port);
989 8ad1c570 2021-05-09 op return i;
990 8ad1c570 2021-05-09 op }
991 8ad1c570 2021-05-09 op }
992 8ad1c570 2021-05-09 op
993 8ad1c570 2021-05-09 op yyerror("too much `fastcgi' rules defined.");
994 8ad1c570 2021-05-09 op return -1;
995 c92b802b 2021-06-11 op }
996 c92b802b 2021-06-11 op
997 c92b802b 2021-06-11 op void
998 c92b802b 2021-06-11 op add_param(char *name, char *val, int env)
999 c92b802b 2021-06-11 op {
1000 c92b802b 2021-06-11 op struct envlist *e;
1001 c92b802b 2021-06-11 op struct envhead *h;
1002 c92b802b 2021-06-11 op
1003 c92b802b 2021-06-11 op if (env)
1004 c92b802b 2021-06-11 op h = &host->env;
1005 c92b802b 2021-06-11 op else
1006 c92b802b 2021-06-11 op h = &host->params;
1007 c92b802b 2021-06-11 op
1008 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
1009 c92b802b 2021-06-11 op e->name = name;
1010 c92b802b 2021-06-11 op e->value = val;
1011 c92b802b 2021-06-11 op if (TAILQ_EMPTY(h))
1012 c92b802b 2021-06-11 op TAILQ_INSERT_HEAD(h, e, envs);
1013 c92b802b 2021-06-11 op else
1014 c92b802b 2021-06-11 op TAILQ_INSERT_TAIL(h, e, envs);
1015 3b21cca3 2021-06-29 op }