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