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