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 13ed2fb6 2021-01-27 op int parse_portno(const char*);
82 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
83 6abda252 2021-02-06 op int check_block_code(int);
84 6abda252 2021-02-06 op char *check_block_fmt(char*);
85 6abda252 2021-02-06 op int check_strip_no(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 d19951cf 2021-07-09 op "please use the new syntax: `map MIME to-ext EXT'",
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 c39be742 2021-07-09 op | TPORT NUM { conf.port = $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 c39be742 2021-07-09 op | /*empty*/
358 c39be742 2021-07-09 op ;
359 fdea6aa0 2021-04-30 op
360 c39be742 2021-07-09 op %%
361 b8e64ccd 2021-03-31 op
362 74f0778b 2021-06-16 op static struct keyword {
363 74f0778b 2021-06-16 op const char *word;
364 74f0778b 2021-06-16 op int token;
365 74f0778b 2021-06-16 op } keywords[] = {
366 d93c8191 2021-07-09 op /* these MUST be sorted */
367 74f0778b 2021-06-16 op {"alias", TALIAS},
368 74f0778b 2021-06-16 op {"auto", TAUTO},
369 74f0778b 2021-06-16 op {"block", TBLOCK},
370 74f0778b 2021-06-16 op {"ca", TCA},
371 74f0778b 2021-06-16 op {"cert", TCERT},
372 74f0778b 2021-06-16 op {"cgi", TCGI},
373 74f0778b 2021-06-16 op {"chroot", TCHROOT},
374 74f0778b 2021-06-16 op {"client", TCLIENT},
375 74f0778b 2021-06-16 op {"default", TDEFAULT},
376 74f0778b 2021-06-16 op {"entrypoint", TENTRYPOINT},
377 74f0778b 2021-06-16 op {"env", TENV},
378 74f0778b 2021-06-16 op {"fastcgi", TFASTCGI},
379 74f0778b 2021-06-16 op {"index", TINDEX},
380 74f0778b 2021-06-16 op {"ipv6", TIPV6},
381 74f0778b 2021-06-16 op {"key", TKEY},
382 74f0778b 2021-06-16 op {"lang", TLANG},
383 74f0778b 2021-06-16 op {"location", TLOCATION},
384 74f0778b 2021-06-16 op {"log", TLOG},
385 d19951cf 2021-07-09 op {"map", TMAP},
386 74f0778b 2021-06-16 op {"mime", TMIME},
387 c39be742 2021-07-09 op {"off", TOFF},
388 c39be742 2021-07-09 op {"on", TON},
389 74f0778b 2021-06-16 op {"param", TPARAM},
390 74f0778b 2021-06-16 op {"port", TPORT},
391 74f0778b 2021-06-16 op {"prefork", TPREFORK},
392 74f0778b 2021-06-16 op {"protocols", TPROTOCOLS},
393 74f0778b 2021-06-16 op {"require", TREQUIRE},
394 74f0778b 2021-06-16 op {"return", TRETURN},
395 74f0778b 2021-06-16 op {"root", TROOT},
396 74f0778b 2021-06-16 op {"server", TSERVER},
397 74f0778b 2021-06-16 op {"spawn", TSPAWN},
398 74f0778b 2021-06-16 op {"strip", TSTRIP},
399 74f0778b 2021-06-16 op {"tcp", TTCP},
400 d19951cf 2021-07-09 op {"to-ext", TTOEXT},
401 74f0778b 2021-06-16 op {"type", TTYPE},
402 74f0778b 2021-06-16 op {"user", TUSER},
403 74f0778b 2021-06-16 op };
404 74f0778b 2021-06-16 op
405 c39be742 2021-07-09 op void
406 c39be742 2021-07-09 op yyerror(const char *msg, ...)
407 c39be742 2021-07-09 op {
408 c39be742 2021-07-09 op va_list ap;
409 c39be742 2021-07-09 op
410 c39be742 2021-07-09 op file->errors++;
411 c39be742 2021-07-09 op
412 c39be742 2021-07-09 op va_start(ap, msg);
413 c39be742 2021-07-09 op fprintf(stderr, "%s:%d: ", config_path, yylval.lineno);
414 c39be742 2021-07-09 op vfprintf(stderr, msg, ap);
415 c39be742 2021-07-09 op fprintf(stderr, "\n");
416 c39be742 2021-07-09 op va_end(ap);
417 c39be742 2021-07-09 op }
418 c39be742 2021-07-09 op
419 d93c8191 2021-07-09 op int
420 d93c8191 2021-07-09 op kw_cmp(const void *k, const void *e)
421 d93c8191 2021-07-09 op {
422 d93c8191 2021-07-09 op return strcmp(k, ((struct keyword *)e)->word);
423 d93c8191 2021-07-09 op }
424 d93c8191 2021-07-09 op
425 c39be742 2021-07-09 op int
426 c39be742 2021-07-09 op lookup(char *s)
427 74f0778b 2021-06-16 op {
428 c39be742 2021-07-09 op const struct keyword *p;
429 74f0778b 2021-06-16 op
430 c39be742 2021-07-09 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
431 c39be742 2021-07-09 op sizeof(keywords[0]), kw_cmp);
432 74f0778b 2021-06-16 op
433 c39be742 2021-07-09 op if (p)
434 c39be742 2021-07-09 op return p->token;
435 c39be742 2021-07-09 op else
436 c39be742 2021-07-09 op return STRING;
437 c39be742 2021-07-09 op }
438 c39be742 2021-07-09 op
439 c39be742 2021-07-09 op #define START_EXPAND 1
440 c39be742 2021-07-09 op #define DONE_EXPAND 2
441 c39be742 2021-07-09 op
442 c39be742 2021-07-09 op static int expanding;
443 c39be742 2021-07-09 op
444 c39be742 2021-07-09 op int
445 c39be742 2021-07-09 op igetc(void)
446 c39be742 2021-07-09 op {
447 c39be742 2021-07-09 op int c;
448 c39be742 2021-07-09 op
449 c39be742 2021-07-09 op while (1) {
450 c39be742 2021-07-09 op if (file->ungetpos > 0)
451 c39be742 2021-07-09 op c = file->ungetbuf[--file->ungetpos];
452 c39be742 2021-07-09 op else
453 c39be742 2021-07-09 op c = getc(file->stream);
454 c39be742 2021-07-09 op
455 c39be742 2021-07-09 op if (c == START_EXPAND)
456 c39be742 2021-07-09 op expanding = 1;
457 c39be742 2021-07-09 op else if (c == DONE_EXPAND)
458 c39be742 2021-07-09 op expanding = 0;
459 c39be742 2021-07-09 op else
460 c39be742 2021-07-09 op break;
461 74f0778b 2021-06-16 op }
462 c39be742 2021-07-09 op return c;
463 c39be742 2021-07-09 op }
464 74f0778b 2021-06-16 op
465 c39be742 2021-07-09 op int
466 c39be742 2021-07-09 op lgetc(int quotec)
467 c39be742 2021-07-09 op {
468 c39be742 2021-07-09 op int c, next;
469 c39be742 2021-07-09 op
470 c39be742 2021-07-09 op if (quotec) {
471 c39be742 2021-07-09 op if ((c = igetc()) == EOF) {
472 c39be742 2021-07-09 op yyerror("reached end of file while parsing "
473 c39be742 2021-07-09 op "quoted string");
474 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
475 c39be742 2021-07-09 op return EOF;
476 c39be742 2021-07-09 op return quotec;
477 c39be742 2021-07-09 op }
478 74f0778b 2021-06-16 op return c;
479 74f0778b 2021-06-16 op }
480 74f0778b 2021-06-16 op
481 c39be742 2021-07-09 op while ((c = igetc()) == '\\') {
482 c39be742 2021-07-09 op next = igetc();
483 c39be742 2021-07-09 op if (next != '\n') {
484 c39be742 2021-07-09 op c = next;
485 74f0778b 2021-06-16 op break;
486 c39be742 2021-07-09 op }
487 c39be742 2021-07-09 op yylval.lineno = file->lineno;
488 c39be742 2021-07-09 op file->lineno++;
489 c39be742 2021-07-09 op }
490 3b21cca3 2021-06-29 op
491 c39be742 2021-07-09 op if (c == EOF) {
492 c39be742 2021-07-09 op /*
493 c39be742 2021-07-09 op * Fake EOL when hit EOF for the first time. This gets line
494 c39be742 2021-07-09 op * count right if last line in included file is syntactically
495 c39be742 2021-07-09 op * invalid and has no newline.
496 c39be742 2021-07-09 op */
497 c39be742 2021-07-09 op if (file->eof_reached == 0) {
498 c39be742 2021-07-09 op file->eof_reached = 1;
499 c39be742 2021-07-09 op return '\n';
500 c39be742 2021-07-09 op }
501 c39be742 2021-07-09 op while (c == EOF) {
502 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
503 c39be742 2021-07-09 op return EOF;
504 c39be742 2021-07-09 op c = igetc();
505 c39be742 2021-07-09 op }
506 c39be742 2021-07-09 op }
507 c39be742 2021-07-09 op return c;
508 c39be742 2021-07-09 op }
509 c39be742 2021-07-09 op
510 c39be742 2021-07-09 op void
511 c39be742 2021-07-09 op lungetc(int c)
512 c39be742 2021-07-09 op {
513 c39be742 2021-07-09 op if (c == EOF)
514 c39be742 2021-07-09 op return;
515 c39be742 2021-07-09 op
516 c39be742 2021-07-09 op if (file->ungetpos >= file->ungetsize) {
517 c39be742 2021-07-09 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
518 c39be742 2021-07-09 op if (p == NULL)
519 c39be742 2021-07-09 op err(1, "lungetc");
520 c39be742 2021-07-09 op file->ungetbuf = p;
521 c39be742 2021-07-09 op file->ungetsize *= 2;
522 c39be742 2021-07-09 op }
523 c39be742 2021-07-09 op file->ungetbuf[file->ungetpos++] = c;
524 c39be742 2021-07-09 op }
525 c39be742 2021-07-09 op
526 c39be742 2021-07-09 op int
527 c39be742 2021-07-09 op findeol(void)
528 c39be742 2021-07-09 op {
529 c39be742 2021-07-09 op int c;
530 c39be742 2021-07-09 op
531 c39be742 2021-07-09 op /* Skip to either EOF or the first real EOL. */
532 c39be742 2021-07-09 op while (1) {
533 c39be742 2021-07-09 op c = lgetc(0);
534 c39be742 2021-07-09 op if (c == '\n') {
535 c39be742 2021-07-09 op file->lineno++;
536 3b21cca3 2021-06-29 op break;
537 c39be742 2021-07-09 op }
538 c39be742 2021-07-09 op if (c == EOF)
539 74f0778b 2021-06-16 op break;
540 c39be742 2021-07-09 op }
541 c39be742 2021-07-09 op return ERROR;
542 c39be742 2021-07-09 op }
543 c39be742 2021-07-09 op
544 c39be742 2021-07-09 op int
545 c39be742 2021-07-09 op yylex(void)
546 c39be742 2021-07-09 op {
547 c39be742 2021-07-09 op unsigned char buf[8096];
548 c39be742 2021-07-09 op unsigned char *p, *val;
549 c39be742 2021-07-09 op int quotec, next, c;
550 c39be742 2021-07-09 op int token;
551 c39be742 2021-07-09 op
552 c39be742 2021-07-09 op top:
553 c39be742 2021-07-09 op p = buf;
554 c39be742 2021-07-09 op while ((c = lgetc(0)) == ' ' || c == '\t')
555 c39be742 2021-07-09 op ; /* nothing */
556 c39be742 2021-07-09 op
557 c39be742 2021-07-09 op yylval.lineno = file->lineno;
558 c39be742 2021-07-09 op if (c == '#')
559 c39be742 2021-07-09 op while ((c = lgetc(0)) != '\n' && c != EOF)
560 c39be742 2021-07-09 op ; /* nothing */
561 c39be742 2021-07-09 op if (c == '$' && !expanding) {
562 c39be742 2021-07-09 op while (1) {
563 c39be742 2021-07-09 op if ((c = lgetc(0)) == EOF)
564 c39be742 2021-07-09 op return 0;
565 c39be742 2021-07-09 op
566 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
567 c39be742 2021-07-09 op yyerror("string too long");
568 c39be742 2021-07-09 op return findeol();
569 c39be742 2021-07-09 op }
570 c39be742 2021-07-09 op if (isalnum(c) || c == '_') {
571 c39be742 2021-07-09 op *p++ = c;
572 74f0778b 2021-06-16 op continue;
573 74f0778b 2021-06-16 op }
574 c39be742 2021-07-09 op *p = '\0';
575 c39be742 2021-07-09 op lungetc(c);
576 c39be742 2021-07-09 op break;
577 74f0778b 2021-06-16 op }
578 c39be742 2021-07-09 op val = symget(buf);
579 c39be742 2021-07-09 op if (val == NULL) {
580 c39be742 2021-07-09 op yyerror("macro '%s' not defined", buf);
581 c39be742 2021-07-09 op return findeol();
582 74f0778b 2021-06-16 op }
583 c39be742 2021-07-09 op p = val + strlen(val) - 1;
584 c39be742 2021-07-09 op lungetc(DONE_EXPAND);
585 c39be742 2021-07-09 op while (p >= val) {
586 c39be742 2021-07-09 op lungetc(*p);
587 c39be742 2021-07-09 op p--;
588 c39be742 2021-07-09 op }
589 c39be742 2021-07-09 op lungetc(START_EXPAND);
590 c39be742 2021-07-09 op goto top;
591 74f0778b 2021-06-16 op }
592 74f0778b 2021-06-16 op
593 c39be742 2021-07-09 op switch (c) {
594 c39be742 2021-07-09 op case '\'':
595 c39be742 2021-07-09 op case '"':
596 c39be742 2021-07-09 op quotec = c;
597 c39be742 2021-07-09 op while (1) {
598 c39be742 2021-07-09 op if ((c = lgetc(quotec)) == EOF)
599 c39be742 2021-07-09 op return 0;
600 c39be742 2021-07-09 op if (c == '\n') {
601 c39be742 2021-07-09 op file->lineno++;
602 c39be742 2021-07-09 op continue;
603 c39be742 2021-07-09 op } else if (c == '\\') {
604 c39be742 2021-07-09 op if ((next = lgetc(quotec)) == EOF)
605 c39be742 2021-07-09 op return (0);
606 c39be742 2021-07-09 op if (next == quotec || next == ' ' ||
607 c39be742 2021-07-09 op next == '\t')
608 c39be742 2021-07-09 op c = next;
609 c39be742 2021-07-09 op else if (next == '\n') {
610 c39be742 2021-07-09 op file->lineno++;
611 c39be742 2021-07-09 op continue;
612 c39be742 2021-07-09 op } else
613 c39be742 2021-07-09 op lungetc(next);
614 c39be742 2021-07-09 op } else if (c == quotec) {
615 c39be742 2021-07-09 op *p = '\0';
616 c39be742 2021-07-09 op break;
617 c39be742 2021-07-09 op } else if (c == '\0') {
618 c39be742 2021-07-09 op yyerror("syntax error");
619 c39be742 2021-07-09 op return findeol();
620 c39be742 2021-07-09 op }
621 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
622 c39be742 2021-07-09 op yyerror("string too long");
623 c39be742 2021-07-09 op return findeol();
624 c39be742 2021-07-09 op }
625 c39be742 2021-07-09 op *p++ = c;
626 c39be742 2021-07-09 op }
627 c39be742 2021-07-09 op yylval.v.string = strdup(buf);
628 c39be742 2021-07-09 op if (yylval.v.string == NULL)
629 c39be742 2021-07-09 op err(1, "yylex: strdup");
630 c39be742 2021-07-09 op return STRING;
631 74f0778b 2021-06-16 op }
632 c39be742 2021-07-09 op
633 c39be742 2021-07-09 op #define allowed_to_end_number(x) \
634 c39be742 2021-07-09 op (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
635 c39be742 2021-07-09 op
636 c39be742 2021-07-09 op if (c == '-' || isdigit(c)) {
637 c39be742 2021-07-09 op do {
638 c39be742 2021-07-09 op *p++ = c;
639 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
640 c39be742 2021-07-09 op yyerror("string too long");
641 c39be742 2021-07-09 op return findeol();
642 c39be742 2021-07-09 op }
643 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && isdigit(c));
644 c39be742 2021-07-09 op lungetc(c);
645 c39be742 2021-07-09 op if (p == buf + 1 && buf[0] == '-')
646 c39be742 2021-07-09 op goto nodigits;
647 c39be742 2021-07-09 op if (c == EOF || allowed_to_end_number(c)) {
648 c39be742 2021-07-09 op const char *errstr = NULL;
649 c39be742 2021-07-09 op
650 c39be742 2021-07-09 op *p = '\0';
651 c39be742 2021-07-09 op yylval.v.number = strtonum(buf, LLONG_MIN,
652 c39be742 2021-07-09 op LLONG_MAX, &errstr);
653 c39be742 2021-07-09 op if (errstr) {
654 c39be742 2021-07-09 op yyerror("\"%s\" invalid number: %s",
655 c39be742 2021-07-09 op buf, errstr);
656 c39be742 2021-07-09 op return findeol();
657 c39be742 2021-07-09 op }
658 c39be742 2021-07-09 op return NUM;
659 c39be742 2021-07-09 op } else {
660 c39be742 2021-07-09 op nodigits:
661 c39be742 2021-07-09 op while (p > buf + 1)
662 c39be742 2021-07-09 op lungetc(*--p);
663 c39be742 2021-07-09 op c = *--p;
664 c39be742 2021-07-09 op if (c == '-')
665 c39be742 2021-07-09 op return c;
666 c39be742 2021-07-09 op }
667 74f0778b 2021-06-16 op }
668 c39be742 2021-07-09 op
669 c39be742 2021-07-09 op #define allowed_in_string(x) \
670 c39be742 2021-07-09 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
671 c39be742 2021-07-09 op x != '{' && x != '}' && \
672 c39be742 2021-07-09 op x != '!' && x != '=' && x != '#' && \
673 c39be742 2021-07-09 op x != ','))
674 c39be742 2021-07-09 op
675 c39be742 2021-07-09 op if (isalnum(c) || c == ':' || c == '_' || c == '/' || c == '.') {
676 c39be742 2021-07-09 op do {
677 c39be742 2021-07-09 op *p++ = c;
678 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
679 c39be742 2021-07-09 op yyerror("string too long");
680 c39be742 2021-07-09 op return findeol();
681 c39be742 2021-07-09 op }
682 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
683 c39be742 2021-07-09 op lungetc(c);
684 c39be742 2021-07-09 op *p = '\0';
685 c39be742 2021-07-09 op if ((token = lookup(buf)) == STRING)
686 c39be742 2021-07-09 op yylval.v.string = xstrdup(buf);
687 c39be742 2021-07-09 op return token;
688 74f0778b 2021-06-16 op }
689 c39be742 2021-07-09 op if (c == '\n') {
690 c39be742 2021-07-09 op yylval.lineno = file->lineno;
691 c39be742 2021-07-09 op file->lineno++;
692 74f0778b 2021-06-16 op }
693 c39be742 2021-07-09 op if (c == EOF)
694 c39be742 2021-07-09 op return 0;
695 c39be742 2021-07-09 op return c;
696 c39be742 2021-07-09 op }
697 c39be742 2021-07-09 op
698 c39be742 2021-07-09 op struct file *
699 c39be742 2021-07-09 op pushfile(const char *name, int secret)
700 c39be742 2021-07-09 op {
701 c39be742 2021-07-09 op struct file *nfile;
702 c39be742 2021-07-09 op
703 c39be742 2021-07-09 op nfile = xcalloc(1, sizeof(*nfile));
704 c39be742 2021-07-09 op nfile->name = xstrdup(name);
705 c39be742 2021-07-09 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
706 c39be742 2021-07-09 op yyerror("can't open %s: %s", nfile->name,
707 c39be742 2021-07-09 op strerror(errno));
708 c39be742 2021-07-09 op free(nfile->name);
709 c39be742 2021-07-09 op free(nfile);
710 c39be742 2021-07-09 op return NULL;
711 74f0778b 2021-06-16 op }
712 c39be742 2021-07-09 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
713 c39be742 2021-07-09 op nfile->ungetsize = 16;
714 c39be742 2021-07-09 op nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
715 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&files, nfile, entry);
716 c39be742 2021-07-09 op return nfile;
717 c39be742 2021-07-09 op }
718 74f0778b 2021-06-16 op
719 c39be742 2021-07-09 op int
720 c39be742 2021-07-09 op popfile(void)
721 c39be742 2021-07-09 op {
722 c39be742 2021-07-09 op struct file *prev;
723 13ed2fb6 2021-01-27 op
724 c39be742 2021-07-09 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
725 c39be742 2021-07-09 op prev->errors += file->errors;
726 13ed2fb6 2021-01-27 op
727 c39be742 2021-07-09 op TAILQ_REMOVE(&files, file, entry);
728 c39be742 2021-07-09 op fclose(file->stream);
729 c39be742 2021-07-09 op free(file->name);
730 c39be742 2021-07-09 op free(file->ungetbuf);
731 c39be742 2021-07-09 op free(file);
732 c39be742 2021-07-09 op file = prev;
733 c39be742 2021-07-09 op return file ? 0 : EOF;
734 13ed2fb6 2021-01-27 op }
735 13ed2fb6 2021-01-27 op
736 13ed2fb6 2021-01-27 op void
737 c39be742 2021-07-09 op parse_conf(const char *filename)
738 13ed2fb6 2021-01-27 op {
739 c39be742 2021-07-09 op struct sym *sym, *next;
740 3b21cca3 2021-06-29 op
741 c39be742 2021-07-09 op file = pushfile(filename, 0);
742 c39be742 2021-07-09 op if (file == NULL)
743 c39be742 2021-07-09 op return;
744 c39be742 2021-07-09 op topfile = file;
745 c39be742 2021-07-09 op
746 13ed2fb6 2021-01-27 op yyparse();
747 c39be742 2021-07-09 op errors = file->errors;
748 c39be742 2021-07-09 op popfile();
749 13ed2fb6 2021-01-27 op
750 c39be742 2021-07-09 op /* Free macros and check which have not been used. */
751 3b21cca3 2021-06-29 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
752 3b21cca3 2021-06-29 op /* TODO: warn if !sym->used */
753 3b21cca3 2021-06-29 op if (!sym->persist) {
754 3b21cca3 2021-06-29 op free(sym->name);
755 3b21cca3 2021-06-29 op free(sym->val);
756 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
757 3b21cca3 2021-06-29 op free(sym);
758 3b21cca3 2021-06-29 op }
759 3b21cca3 2021-06-29 op }
760 c39be742 2021-07-09 op
761 c39be742 2021-07-09 op if (errors)
762 c39be742 2021-07-09 op exit(1);
763 13ed2fb6 2021-01-27 op }
764 e17642a7 2021-02-01 op
765 c39be742 2021-07-09 op int
766 c39be742 2021-07-09 op symset(const char *name, const char *val, int persist)
767 c39be742 2021-07-09 op {
768 c39be742 2021-07-09 op struct sym *sym;
769 c39be742 2021-07-09 op
770 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
771 c39be742 2021-07-09 op if (!strcmp(name, sym->name))
772 c39be742 2021-07-09 op break;
773 c39be742 2021-07-09 op }
774 c39be742 2021-07-09 op
775 c39be742 2021-07-09 op if (sym != NULL) {
776 c39be742 2021-07-09 op if (sym->persist)
777 c39be742 2021-07-09 op return 0;
778 c39be742 2021-07-09 op else {
779 c39be742 2021-07-09 op free(sym->name);
780 c39be742 2021-07-09 op free(sym->val);
781 c39be742 2021-07-09 op TAILQ_REMOVE(&symhead, sym, entry);
782 c39be742 2021-07-09 op free(sym);
783 c39be742 2021-07-09 op }
784 c39be742 2021-07-09 op }
785 c39be742 2021-07-09 op
786 c39be742 2021-07-09 op sym = xcalloc(1, sizeof(*sym));
787 c39be742 2021-07-09 op sym->name = xstrdup(name);
788 c39be742 2021-07-09 op sym->val = xstrdup(val);
789 c39be742 2021-07-09 op sym->used = 0;
790 c39be742 2021-07-09 op sym->persist = persist;
791 c39be742 2021-07-09 op
792 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
793 c39be742 2021-07-09 op return 0;
794 c39be742 2021-07-09 op }
795 c39be742 2021-07-09 op
796 c39be742 2021-07-09 op int
797 c39be742 2021-07-09 op cmdline_symset(char *s)
798 c39be742 2021-07-09 op {
799 c39be742 2021-07-09 op char *sym, *val;
800 c39be742 2021-07-09 op int ret;
801 c39be742 2021-07-09 op
802 c39be742 2021-07-09 op if ((val = strrchr(s, '=')) == NULL)
803 c39be742 2021-07-09 op return -1;
804 c39be742 2021-07-09 op sym = xcalloc(1, val - s + 1);
805 c39be742 2021-07-09 op memcpy(sym, s, val - s);
806 c39be742 2021-07-09 op ret = symset(sym, val + 1, 1);
807 c39be742 2021-07-09 op free(sym);
808 c39be742 2021-07-09 op return ret;
809 c39be742 2021-07-09 op }
810 c39be742 2021-07-09 op
811 e17642a7 2021-02-01 op char *
812 c39be742 2021-07-09 op symget(const char *nam)
813 c39be742 2021-07-09 op {
814 c39be742 2021-07-09 op struct sym *sym;
815 c39be742 2021-07-09 op
816 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
817 c39be742 2021-07-09 op if (strcmp(nam, sym->name) == 0) {
818 c39be742 2021-07-09 op sym->used = 1;
819 c39be742 2021-07-09 op return sym->val;
820 c39be742 2021-07-09 op }
821 c39be742 2021-07-09 op }
822 c39be742 2021-07-09 op return NULL;
823 c39be742 2021-07-09 op }
824 c39be742 2021-07-09 op
825 c39be742 2021-07-09 op struct vhost *
826 c39be742 2021-07-09 op new_vhost(void)
827 c39be742 2021-07-09 op {
828 c39be742 2021-07-09 op return xcalloc(1, sizeof(struct vhost));
829 c39be742 2021-07-09 op }
830 c39be742 2021-07-09 op
831 c39be742 2021-07-09 op struct location *
832 c39be742 2021-07-09 op new_location(void)
833 c39be742 2021-07-09 op {
834 c39be742 2021-07-09 op struct location *l;
835 c39be742 2021-07-09 op
836 c39be742 2021-07-09 op l = xcalloc(1, sizeof(*l));
837 c39be742 2021-07-09 op l->dirfd = -1;
838 c39be742 2021-07-09 op l->fcgi = -1;
839 c39be742 2021-07-09 op return l;
840 c39be742 2021-07-09 op }
841 c39be742 2021-07-09 op
842 c39be742 2021-07-09 op int
843 c39be742 2021-07-09 op parse_portno(const char *p)
844 c39be742 2021-07-09 op {
845 c39be742 2021-07-09 op const char *errstr;
846 c39be742 2021-07-09 op int n;
847 c39be742 2021-07-09 op
848 c39be742 2021-07-09 op n = strtonum(p, 0, UINT16_MAX, &errstr);
849 c39be742 2021-07-09 op if (errstr != NULL)
850 c39be742 2021-07-09 op yyerror("port number is %s: %s", errstr, p);
851 c39be742 2021-07-09 op return n;
852 c39be742 2021-07-09 op }
853 c39be742 2021-07-09 op
854 c39be742 2021-07-09 op char *
855 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
856 e17642a7 2021-02-01 op {
857 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
858 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
859 e17642a7 2021-02-01 op return path;
860 e17642a7 2021-02-01 op }
861 6abda252 2021-02-06 op
862 6abda252 2021-02-06 op int
863 6abda252 2021-02-06 op check_block_code(int n)
864 6abda252 2021-02-06 op {
865 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
866 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
867 6abda252 2021-02-06 op return n;
868 6abda252 2021-02-06 op }
869 6abda252 2021-02-06 op
870 6abda252 2021-02-06 op char *
871 6abda252 2021-02-06 op check_block_fmt(char *fmt)
872 6abda252 2021-02-06 op {
873 6abda252 2021-02-06 op char *s;
874 6abda252 2021-02-06 op
875 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
876 6abda252 2021-02-06 op if (*s != '%')
877 6abda252 2021-02-06 op continue;
878 6abda252 2021-02-06 op switch (*++s) {
879 6abda252 2021-02-06 op case '%':
880 6abda252 2021-02-06 op case 'p':
881 6abda252 2021-02-06 op case 'q':
882 6abda252 2021-02-06 op case 'P':
883 6abda252 2021-02-06 op case 'N':
884 6abda252 2021-02-06 op break;
885 6abda252 2021-02-06 op default:
886 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
887 6abda252 2021-02-06 op }
888 6abda252 2021-02-06 op }
889 6abda252 2021-02-06 op
890 6abda252 2021-02-06 op return fmt;
891 6abda252 2021-02-06 op }
892 6abda252 2021-02-06 op
893 6abda252 2021-02-06 op int
894 6abda252 2021-02-06 op check_strip_no(int n)
895 6abda252 2021-02-06 op {
896 6abda252 2021-02-06 op if (n <= 0)
897 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
898 a709ddf5 2021-02-07 op return n;
899 a709ddf5 2021-02-07 op }
900 a709ddf5 2021-02-07 op
901 a709ddf5 2021-02-07 op int
902 a709ddf5 2021-02-07 op check_prefork_num(int n)
903 a709ddf5 2021-02-07 op {
904 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
905 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
906 6abda252 2021-02-06 op return n;
907 6abda252 2021-02-06 op }
908 49b73ba1 2021-02-10 op
909 49b73ba1 2021-02-10 op void
910 49b73ba1 2021-02-10 op advance_loc(void)
911 49b73ba1 2021-02-10 op {
912 b8e64ccd 2021-03-31 op loc = new_location();
913 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
914 49b73ba1 2021-02-10 op }
915 c705ecb1 2021-05-03 op
916 c705ecb1 2021-05-03 op void
917 c705ecb1 2021-05-03 op only_once(const void *ptr, const char *name)
918 c705ecb1 2021-05-03 op {
919 c705ecb1 2021-05-03 op if (ptr != NULL)
920 c705ecb1 2021-05-03 op yyerror("`%s' specified more than once", name);
921 c705ecb1 2021-05-03 op }
922 8ad1c570 2021-05-09 op
923 8ad1c570 2021-05-09 op void
924 8ad1c570 2021-05-09 op only_oncei(int i, const char *name)
925 8ad1c570 2021-05-09 op {
926 8ad1c570 2021-05-09 op if (i != -1)
927 8ad1c570 2021-05-09 op yyerror("`%s' specified more than once", name);
928 8ad1c570 2021-05-09 op }
929 8ad1c570 2021-05-09 op
930 8ad1c570 2021-05-09 op int
931 8ad1c570 2021-05-09 op fastcgi_conf(char *path, char *port, char *prog)
932 8ad1c570 2021-05-09 op {
933 8ad1c570 2021-05-09 op struct fcgi *f;
934 8ad1c570 2021-05-09 op int i;
935 8ad1c570 2021-05-09 op
936 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
937 8ad1c570 2021-05-09 op f = &fcgi[i];
938 fafc6849 2021-06-29 op
939 8ad1c570 2021-05-09 op if (f->path == NULL) {
940 8ad1c570 2021-05-09 op f->id = i;
941 8ad1c570 2021-05-09 op f->path = path;
942 8ad1c570 2021-05-09 op f->port = port;
943 8ad1c570 2021-05-09 op f->prog = prog;
944 8ad1c570 2021-05-09 op return i;
945 8ad1c570 2021-05-09 op }
946 8ad1c570 2021-05-09 op
947 8ad1c570 2021-05-09 op /* XXX: what to do with prog? */
948 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
949 8ad1c570 2021-05-09 op ((port == NULL && f->port == NULL) ||
950 8ad1c570 2021-05-09 op !strcmp(f->port, port))) {
951 8ad1c570 2021-05-09 op free(path);
952 8ad1c570 2021-05-09 op free(port);
953 8ad1c570 2021-05-09 op return i;
954 8ad1c570 2021-05-09 op }
955 8ad1c570 2021-05-09 op }
956 8ad1c570 2021-05-09 op
957 8ad1c570 2021-05-09 op yyerror("too much `fastcgi' rules defined.");
958 8ad1c570 2021-05-09 op return -1;
959 c92b802b 2021-06-11 op }
960 c92b802b 2021-06-11 op
961 c92b802b 2021-06-11 op void
962 c92b802b 2021-06-11 op add_param(char *name, char *val, int env)
963 c92b802b 2021-06-11 op {
964 c92b802b 2021-06-11 op struct envlist *e;
965 c92b802b 2021-06-11 op struct envhead *h;
966 c92b802b 2021-06-11 op
967 c92b802b 2021-06-11 op if (env)
968 c92b802b 2021-06-11 op h = &host->env;
969 c92b802b 2021-06-11 op else
970 c92b802b 2021-06-11 op h = &host->params;
971 c92b802b 2021-06-11 op
972 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
973 c92b802b 2021-06-11 op e->name = name;
974 c92b802b 2021-06-11 op e->value = val;
975 c92b802b 2021-06-11 op if (TAILQ_EMPTY(h))
976 c92b802b 2021-06-11 op TAILQ_INSERT_HEAD(h, e, envs);
977 c92b802b 2021-06-11 op else
978 c92b802b 2021-06-11 op TAILQ_INSERT_TAIL(h, e, envs);
979 3b21cca3 2021-06-29 op }