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 15902770 2021-01-15 op *
6 15902770 2021-01-15 op * Permission to use, copy, modify, and distribute this software for any
7 15902770 2021-01-15 op * purpose with or without fee is hereby granted, provided that the above
8 15902770 2021-01-15 op * copyright notice and this permission notice appear in all copies.
9 15902770 2021-01-15 op *
10 15902770 2021-01-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 15902770 2021-01-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 15902770 2021-01-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 15902770 2021-01-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 15902770 2021-01-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 15902770 2021-01-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 15902770 2021-01-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 15902770 2021-01-15 op */
18 15902770 2021-01-15 op
19 74f0778b 2021-06-16 op #include <ctype.h>
20 002a84a1 2021-02-10 op #include <errno.h>
21 6abda252 2021-02-06 op #include <stdarg.h>
22 15902770 2021-01-15 op #include <stdio.h>
23 74f0778b 2021-06-16 op #include <stdlib.h>
24 32693ee6 2021-01-28 op #include <string.h>
25 15902770 2021-01-15 op
26 15902770 2021-01-15 op #include "gmid.h"
27 15902770 2021-01-15 op
28 74f0778b 2021-06-16 op FILE *yyfp;
29 74f0778b 2021-06-16 op
30 ef129b08 2021-06-16 op typedef struct {
31 ef129b08 2021-06-16 op union {
32 ef129b08 2021-06-16 op char *str;
33 ef129b08 2021-06-16 op int num;
34 8235a81c 2021-06-29 op } v;
35 ef129b08 2021-06-16 op int lineno;
36 ef129b08 2021-06-16 op int colno;
37 ef129b08 2021-06-16 op } yystype;
38 ef129b08 2021-06-16 op #define YYSTYPE yystype
39 ef129b08 2021-06-16 op
40 15902770 2021-01-15 op /*
41 15902770 2021-01-15 op * #define YYDEBUG 1
42 15902770 2021-01-15 op * int yydebug = 1;
43 15902770 2021-01-15 op */
44 15902770 2021-01-15 op
45 3b21cca3 2021-06-29 op /*
46 3b21cca3 2021-06-29 op * The idea behind this implementation of macros is from rad/parse.y
47 3b21cca3 2021-06-29 op */
48 3b21cca3 2021-06-29 op TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
49 3b21cca3 2021-06-29 op struct sym {
50 3b21cca3 2021-06-29 op TAILQ_ENTRY(sym) entry;
51 3b21cca3 2021-06-29 op int used;
52 3b21cca3 2021-06-29 op int persist;
53 3b21cca3 2021-06-29 op char *name;
54 3b21cca3 2021-06-29 op char *val;
55 3b21cca3 2021-06-29 op };
56 3b21cca3 2021-06-29 op
57 ca21e100 2021-02-04 op struct vhost *host;
58 ca21e100 2021-02-04 op struct location *loc;
59 15902770 2021-01-15 op
60 74f0778b 2021-06-16 op static int goterror;
61 15902770 2021-01-15 op
62 b8e64ccd 2021-03-31 op static struct vhost *new_vhost(void);
63 b8e64ccd 2021-03-31 op static struct location *new_location(void);
64 b8e64ccd 2021-03-31 op
65 6abda252 2021-02-06 op void yyerror(const char*, ...);
66 74f0778b 2021-06-16 op static int yylex(void);
67 13ed2fb6 2021-01-27 op int parse_portno(const char*);
68 13ed2fb6 2021-01-27 op void parse_conf(const char*);
69 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
70 6abda252 2021-02-06 op int check_block_code(int);
71 6abda252 2021-02-06 op char *check_block_fmt(char*);
72 6abda252 2021-02-06 op int check_strip_no(int);
73 a709ddf5 2021-02-07 op int check_prefork_num(int);
74 49b73ba1 2021-02-10 op void advance_loc(void);
75 c705ecb1 2021-05-03 op void only_once(const void*, const char*);
76 8ad1c570 2021-05-09 op void only_oncei(int, const char*);
77 8ad1c570 2021-05-09 op int fastcgi_conf(char *, char *, char *);
78 c92b802b 2021-06-11 op void add_param(char *, char *, int);
79 3b21cca3 2021-06-29 op int symset(const char *, const char *, int);
80 3b21cca3 2021-06-29 op char *symget(const char *);
81 13ed2fb6 2021-01-27 op
82 15902770 2021-01-15 op %}
83 15902770 2021-01-15 op
84 15902770 2021-01-15 op /* for bison: */
85 15902770 2021-01-15 op /* %define parse.error verbose */
86 15902770 2021-01-15 op
87 7252049d 2021-06-29 op %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
88 7252049d 2021-06-29 op %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
89 7252049d 2021-06-29 op %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
90 7252049d 2021-06-29 op %token TFASTCGI TSPAWN TPARAM
91 d06d6f4b 2021-04-29 op
92 7252049d 2021-06-29 op %token TERR
93 7252049d 2021-06-29 op
94 7252049d 2021-06-29 op %token <v.str> TSTRING
95 7252049d 2021-06-29 op %token <v.num> TNUM
96 7252049d 2021-06-29 op %token <v.num> TBOOL
97 15902770 2021-01-15 op
98 7252049d 2021-06-29 op %type <v.str> string
99 98f52178 2021-06-29 op
100 15902770 2021-01-15 op %%
101 15902770 2021-01-15 op
102 6b86655a 2021-06-29 op conf : /* empty */
103 6b86655a 2021-06-29 op | conf var
104 6b86655a 2021-06-29 op | conf option
105 6b86655a 2021-06-29 op | conf vhost
106 3b21cca3 2021-06-29 op ;
107 3b21cca3 2021-06-29 op
108 98f52178 2021-06-29 op string : string TSTRING {
109 98f52178 2021-06-29 op if (asprintf(&$$, "%s%s", $1, $2) == -1) {
110 98f52178 2021-06-29 op free($1);
111 98f52178 2021-06-29 op free($2);
112 98f52178 2021-06-29 op yyerror("string: asprintf: %s", strerror(errno));
113 98f52178 2021-06-29 op YYERROR;
114 98f52178 2021-06-29 op }
115 98f52178 2021-06-29 op free($1);
116 98f52178 2021-06-29 op free($2);
117 98f52178 2021-06-29 op }
118 98f52178 2021-06-29 op | TSTRING
119 98f52178 2021-06-29 op ;
120 98f52178 2021-06-29 op
121 98f52178 2021-06-29 op var : TSTRING '=' string {
122 3b21cca3 2021-06-29 op char *s = $1;
123 3b21cca3 2021-06-29 op while (*s++) {
124 3b21cca3 2021-06-29 op if (isspace(*s)) {
125 3b21cca3 2021-06-29 op yyerror("macro name cannot contain "
126 3b21cca3 2021-06-29 op "whitespaces");
127 3b21cca3 2021-06-29 op free($1);
128 3b21cca3 2021-06-29 op free($3);
129 3b21cca3 2021-06-29 op YYERROR;
130 3b21cca3 2021-06-29 op }
131 3b21cca3 2021-06-29 op }
132 3b21cca3 2021-06-29 op symset($1, $3, 0);
133 3b21cca3 2021-06-29 op free($1);
134 3b21cca3 2021-06-29 op free($3);
135 3b21cca3 2021-06-29 op }
136 15902770 2021-01-15 op ;
137 15902770 2021-01-15 op
138 98f52178 2021-06-29 op option : TCHROOT string { conf.chroot = $2; }
139 eb59f87e 2021-02-09 op | TIPV6 TBOOL { conf.ipv6 = $2; }
140 98f52178 2021-06-29 op | TMIME TSTRING string { add_mime(&conf.mime, $2, $3); }
141 15902770 2021-01-15 op | TPORT TNUM { conf.port = $2; }
142 eb59f87e 2021-02-09 op | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
143 98f52178 2021-06-29 op | TPROTOCOLS string {
144 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
145 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
146 5bc3c98e 2021-01-15 op }
147 98f52178 2021-06-29 op | TUSER string { conf.user = $2; }
148 15902770 2021-01-15 op ;
149 15902770 2021-01-15 op
150 98f52178 2021-06-29 op vhost : TSERVER string {
151 b8e64ccd 2021-03-31 op host = new_vhost();
152 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
153 b8e64ccd 2021-03-31 op
154 b8e64ccd 2021-03-31 op loc = new_location();
155 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
156 b8e64ccd 2021-03-31 op
157 b8e64ccd 2021-03-31 op loc->match = xstrdup("*");
158 15902770 2021-01-15 op host->domain = $2;
159 15902770 2021-01-15 op
160 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
161 ef129b08 2021-06-16 op warnx("%s:%d:%d \"%s\" looks like punycode: "
162 415ac7a2 2021-01-28 op "you should use the decoded hostname.",
163 ef129b08 2021-06-16 op config_path, yylval.lineno+1, yylval.colno,
164 ef129b08 2021-06-16 op $2);
165 cbeee4ca 2021-01-28 op }
166 b8e64ccd 2021-03-31 op } '{' servopts locations '}' {
167 cbeee4ca 2021-01-28 op
168 fdea6aa0 2021-04-30 op if (host->cert == NULL || host->key == NULL)
169 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
170 15902770 2021-01-15 op }
171 15902770 2021-01-15 op | error '}' { yyerror("error in server directive"); }
172 15902770 2021-01-15 op ;
173 15902770 2021-01-15 op
174 15902770 2021-01-15 op servopts : /* empty */
175 15902770 2021-01-15 op | servopts servopt
176 15902770 2021-01-15 op ;
177 15902770 2021-01-15 op
178 98f52178 2021-06-29 op servopt : TALIAS string {
179 cc8c2901 2021-04-29 op struct alist *a;
180 cc8c2901 2021-04-29 op
181 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
182 cc8c2901 2021-04-29 op a->alias = $2;
183 cc8c2901 2021-04-29 op if (TAILQ_EMPTY(&host->aliases))
184 cc8c2901 2021-04-29 op TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
185 cc8c2901 2021-04-29 op else
186 cc8c2901 2021-04-29 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
187 cc8c2901 2021-04-29 op }
188 98f52178 2021-06-29 op | TCERT string {
189 c705ecb1 2021-05-03 op only_once(host->cert, "cert");
190 c705ecb1 2021-05-03 op host->cert = ensure_absolute_path($2);
191 c705ecb1 2021-05-03 op }
192 98f52178 2021-06-29 op | TCGI string {
193 c705ecb1 2021-05-03 op only_once(host->cgi, "cgi");
194 15902770 2021-01-15 op /* drop the starting '/', if any */
195 709f4c94 2021-02-04 op if (*$2 == '/')
196 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
197 709f4c94 2021-02-04 op host->cgi = $2;
198 05c23a54 2021-01-19 op }
199 98f52178 2021-06-29 op | TENTRYPOINT string {
200 c705ecb1 2021-05-03 op only_once(host->entrypoint, "entrypoint");
201 e3ddf390 2021-02-06 op while (*$2 == '/')
202 e3ddf390 2021-02-06 op memmove($2, $2+1, strlen($2));
203 e3ddf390 2021-02-06 op host->entrypoint = $2;
204 e3ddf390 2021-02-06 op }
205 98f52178 2021-06-29 op | TENV string string {
206 c92b802b 2021-06-11 op add_param($2, $3, 1);
207 9cc630aa 2021-04-28 op }
208 98f52178 2021-06-29 op | TKEY string {
209 c705ecb1 2021-05-03 op only_once(host->key, "key");
210 c705ecb1 2021-05-03 op host->key = ensure_absolute_path($2);
211 c92b802b 2021-06-11 op }
212 98f52178 2021-06-29 op | TPARAM string string {
213 c92b802b 2021-06-11 op add_param($2, $3, 0);
214 c705ecb1 2021-05-03 op }
215 c8b74339 2021-01-24 op | locopt
216 c8b74339 2021-01-24 op ;
217 c8b74339 2021-01-24 op
218 c8b74339 2021-01-24 op locations : /* empty */
219 c8b74339 2021-01-24 op | locations location
220 c8b74339 2021-01-24 op ;
221 c8b74339 2021-01-24 op
222 98f52178 2021-06-29 op location : TLOCATION { advance_loc(); } string '{' locopts '}' {
223 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
224 49b73ba1 2021-02-10 op if (*$3 == '/')
225 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
226 49b73ba1 2021-02-10 op loc->match = $3;
227 6119e13e 2021-01-19 op }
228 c8b74339 2021-01-24 op | error '}'
229 c8b74339 2021-01-24 op ;
230 c8b74339 2021-01-24 op
231 c8b74339 2021-01-24 op locopts : /* empty */
232 c8b74339 2021-01-24 op | locopts locopt
233 c8b74339 2021-01-24 op ;
234 c8b74339 2021-01-24 op
235 eb59f87e 2021-02-09 op locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
236 98f52178 2021-06-29 op | TBLOCK TRETURN TNUM string {
237 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
238 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
239 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
240 6abda252 2021-02-06 op }
241 6abda252 2021-02-06 op | TBLOCK TRETURN TNUM {
242 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
243 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
244 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
245 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
246 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
247 6abda252 2021-02-06 op }
248 6abda252 2021-02-06 op | TBLOCK {
249 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
250 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
251 6abda252 2021-02-06 op loc->block_code = 40;
252 6abda252 2021-02-06 op }
253 98f52178 2021-06-29 op | TDEFAULT TTYPE string {
254 c705ecb1 2021-05-03 op only_once(loc->default_mime, "default type");
255 eb59f87e 2021-02-09 op loc->default_mime = $3;
256 eb59f87e 2021-02-09 op }
257 0d047efc 2021-05-24 op | TFASTCGI fastcgi
258 98f52178 2021-06-29 op | TINDEX string {
259 c705ecb1 2021-05-03 op only_once(loc->index, "index");
260 eb59f87e 2021-02-09 op loc->index = $2;
261 eb59f87e 2021-02-09 op }
262 98f52178 2021-06-29 op | TLANG string {
263 c705ecb1 2021-05-03 op only_once(loc->lang, "lang");
264 eb59f87e 2021-02-09 op loc->lang = $2;
265 eb59f87e 2021-02-09 op }
266 793835cb 2021-02-23 op | TLOG TBOOL { loc->disable_log = !$2; }
267 98f52178 2021-06-29 op | TREQUIRE TCLIENT TCA string {
268 c705ecb1 2021-05-03 op only_once(loc->reqca, "require client ca");
269 adbe6a64 2021-04-30 op ensure_absolute_path($4);
270 02be96c6 2021-02-09 op if ((loc->reqca = load_ca($4)) == NULL)
271 02be96c6 2021-02-09 op yyerror("couldn't load ca cert: %s", $4);
272 02be96c6 2021-02-09 op free($4);
273 fdea6aa0 2021-04-30 op }
274 98f52178 2021-06-29 op | TROOT string {
275 c705ecb1 2021-05-03 op only_once(loc->dir, "root");
276 fdea6aa0 2021-04-30 op loc->dir = ensure_absolute_path($2);
277 02be96c6 2021-02-09 op }
278 eb59f87e 2021-02-09 op | TSTRIP TNUM { loc->strip = check_strip_no($2); }
279 15902770 2021-01-15 op ;
280 13ed2fb6 2021-01-27 op
281 98f52178 2021-06-29 op fastcgi : TSPAWN string {
282 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
283 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf(NULL, NULL, $2);
284 0d047efc 2021-05-24 op }
285 98f52178 2021-06-29 op | string {
286 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
287 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($1, NULL, NULL);
288 0d047efc 2021-05-24 op }
289 98f52178 2021-06-29 op | TTCP string TNUM {
290 0d047efc 2021-05-24 op char *c;
291 0d047efc 2021-05-24 op if (asprintf(&c, "%d", $3) == -1)
292 0d047efc 2021-05-24 op err(1, "asprintf");
293 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
294 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, c, NULL);
295 0d047efc 2021-05-24 op }
296 98f52178 2021-06-29 op | TTCP string {
297 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
298 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
299 0d047efc 2021-05-24 op }
300 98f52178 2021-06-29 op | TTCP string string {
301 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
302 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, $3, NULL);
303 0d047efc 2021-05-24 op }
304 0d047efc 2021-05-24 op ;
305 0d047efc 2021-05-24 op
306 13ed2fb6 2021-01-27 op %%
307 b8e64ccd 2021-03-31 op
308 b8e64ccd 2021-03-31 op static struct vhost *
309 b8e64ccd 2021-03-31 op new_vhost(void)
310 b8e64ccd 2021-03-31 op {
311 b8e64ccd 2021-03-31 op return xcalloc(1, sizeof(struct vhost));
312 b8e64ccd 2021-03-31 op }
313 13ed2fb6 2021-01-27 op
314 b8e64ccd 2021-03-31 op static struct location *
315 b8e64ccd 2021-03-31 op new_location(void)
316 b8e64ccd 2021-03-31 op {
317 fdea6aa0 2021-04-30 op struct location *l;
318 fdea6aa0 2021-04-30 op
319 fdea6aa0 2021-04-30 op l = xcalloc(1, sizeof(*l));
320 fdea6aa0 2021-04-30 op l->dirfd = -1;
321 8ad1c570 2021-05-09 op l->fcgi = -1;
322 fdea6aa0 2021-04-30 op return l;
323 b8e64ccd 2021-03-31 op }
324 b8e64ccd 2021-03-31 op
325 13ed2fb6 2021-01-27 op void
326 6abda252 2021-02-06 op yyerror(const char *msg, ...)
327 13ed2fb6 2021-01-27 op {
328 6abda252 2021-02-06 op va_list ap;
329 6abda252 2021-02-06 op
330 13ed2fb6 2021-01-27 op goterror = 1;
331 6abda252 2021-02-06 op
332 6abda252 2021-02-06 op va_start(ap, msg);
333 ef129b08 2021-06-16 op fprintf(stderr, "%s:%d: ", config_path, yylval.lineno);
334 6abda252 2021-02-06 op vfprintf(stderr, msg, ap);
335 a1373913 2021-02-07 op fprintf(stderr, "\n");
336 6abda252 2021-02-06 op va_end(ap);
337 74f0778b 2021-06-16 op }
338 74f0778b 2021-06-16 op
339 74f0778b 2021-06-16 op static struct keyword {
340 74f0778b 2021-06-16 op const char *word;
341 74f0778b 2021-06-16 op int token;
342 74f0778b 2021-06-16 op } keywords[] = {
343 74f0778b 2021-06-16 op {"alias", TALIAS},
344 74f0778b 2021-06-16 op {"auto", TAUTO},
345 74f0778b 2021-06-16 op {"block", TBLOCK},
346 74f0778b 2021-06-16 op {"ca", TCA},
347 74f0778b 2021-06-16 op {"cert", TCERT},
348 74f0778b 2021-06-16 op {"cgi", TCGI},
349 74f0778b 2021-06-16 op {"chroot", TCHROOT},
350 74f0778b 2021-06-16 op {"client", TCLIENT},
351 74f0778b 2021-06-16 op {"default", TDEFAULT},
352 74f0778b 2021-06-16 op {"entrypoint", TENTRYPOINT},
353 74f0778b 2021-06-16 op {"env", TENV},
354 74f0778b 2021-06-16 op {"fastcgi", TFASTCGI},
355 74f0778b 2021-06-16 op {"index", TINDEX},
356 74f0778b 2021-06-16 op {"ipv6", TIPV6},
357 74f0778b 2021-06-16 op {"key", TKEY},
358 74f0778b 2021-06-16 op {"lang", TLANG},
359 74f0778b 2021-06-16 op {"location", TLOCATION},
360 74f0778b 2021-06-16 op {"log", TLOG},
361 74f0778b 2021-06-16 op {"mime", TMIME},
362 74f0778b 2021-06-16 op {"param", TPARAM},
363 74f0778b 2021-06-16 op {"port", TPORT},
364 74f0778b 2021-06-16 op {"prefork", TPREFORK},
365 74f0778b 2021-06-16 op {"protocols", TPROTOCOLS},
366 74f0778b 2021-06-16 op {"require", TREQUIRE},
367 74f0778b 2021-06-16 op {"return", TRETURN},
368 74f0778b 2021-06-16 op {"root", TROOT},
369 74f0778b 2021-06-16 op {"server", TSERVER},
370 74f0778b 2021-06-16 op {"spawn", TSPAWN},
371 74f0778b 2021-06-16 op {"strip", TSTRIP},
372 74f0778b 2021-06-16 op {"tcp", TTCP},
373 74f0778b 2021-06-16 op {"type", TTYPE},
374 74f0778b 2021-06-16 op {"user", TUSER},
375 74f0778b 2021-06-16 op };
376 74f0778b 2021-06-16 op
377 74f0778b 2021-06-16 op /*
378 74f0778b 2021-06-16 op * Taken an adapted from doas' parse.y
379 74f0778b 2021-06-16 op */
380 74f0778b 2021-06-16 op static int
381 74f0778b 2021-06-16 op yylex(void)
382 74f0778b 2021-06-16 op {
383 3b21cca3 2021-06-29 op char buf[8096], *ebuf, *p, *str, *v, *val;
384 74f0778b 2021-06-16 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
385 3b21cca3 2021-06-29 op size_t i, len;
386 74f0778b 2021-06-16 op
387 74f0778b 2021-06-16 op p = buf;
388 74f0778b 2021-06-16 op ebuf = buf + sizeof(buf);
389 74f0778b 2021-06-16 op
390 74f0778b 2021-06-16 op repeat:
391 74f0778b 2021-06-16 op /* skip whitespace first */
392 74f0778b 2021-06-16 op for (c = getc(yyfp); isspace(c); c = getc(yyfp)) {
393 ef129b08 2021-06-16 op yylval.colno++;
394 74f0778b 2021-06-16 op if (c == '\n') {
395 ef129b08 2021-06-16 op yylval.lineno++;
396 ef129b08 2021-06-16 op yylval.colno = 0;
397 74f0778b 2021-06-16 op }
398 74f0778b 2021-06-16 op }
399 74f0778b 2021-06-16 op
400 74f0778b 2021-06-16 op /* check for special one-character constructions */
401 74f0778b 2021-06-16 op switch (c) {
402 74f0778b 2021-06-16 op case '{':
403 74f0778b 2021-06-16 op case '}':
404 74f0778b 2021-06-16 op return c;
405 74f0778b 2021-06-16 op case '#':
406 74f0778b 2021-06-16 op /* skip comments; NUL is allowed; no continuation */
407 74f0778b 2021-06-16 op while ((c = getc(yyfp)) != '\n')
408 74f0778b 2021-06-16 op if (c == EOF)
409 74f0778b 2021-06-16 op goto eof;
410 ef129b08 2021-06-16 op yylval.colno = 0;
411 ef129b08 2021-06-16 op yylval.lineno++;
412 74f0778b 2021-06-16 op goto repeat;
413 3b21cca3 2021-06-29 op case '=':
414 3b21cca3 2021-06-29 op return c;
415 74f0778b 2021-06-16 op case EOF:
416 74f0778b 2021-06-16 op goto eof;
417 74f0778b 2021-06-16 op }
418 74f0778b 2021-06-16 op
419 74f0778b 2021-06-16 op /* parsing next word */
420 ef129b08 2021-06-16 op for (;; c = getc(yyfp), yylval.colno++) {
421 74f0778b 2021-06-16 op switch (c) {
422 74f0778b 2021-06-16 op case '\0':
423 74f0778b 2021-06-16 op yyerror("unallowed character NULL in column %d",
424 ef129b08 2021-06-16 op yylval.colno+1);
425 74f0778b 2021-06-16 op escape = 0;
426 74f0778b 2021-06-16 op continue;
427 74f0778b 2021-06-16 op case '\\':
428 74f0778b 2021-06-16 op escape = !escape;
429 74f0778b 2021-06-16 op if (escape)
430 74f0778b 2021-06-16 op continue;
431 74f0778b 2021-06-16 op break;
432 3b21cca3 2021-06-29 op
433 3b21cca3 2021-06-29 op /* expand macros in-place */
434 3b21cca3 2021-06-29 op case '$':
435 bfb076ed 2021-06-29 op if (!escape && !quotes) {
436 3b21cca3 2021-06-29 op v = p;
437 3b21cca3 2021-06-29 op while (1) {
438 3b21cca3 2021-06-29 op if ((c = getc(yyfp)) == EOF) {
439 3b21cca3 2021-06-29 op yyerror("EOF during macro expansion");
440 3b21cca3 2021-06-29 op return 0;
441 3b21cca3 2021-06-29 op }
442 3b21cca3 2021-06-29 op if (p + 1 >= ebuf - 1) {
443 3b21cca3 2021-06-29 op yyerror("string too long");
444 3b21cca3 2021-06-29 op return 0;
445 3b21cca3 2021-06-29 op }
446 3b21cca3 2021-06-29 op if (isalnum(c) || c == '_') {
447 3b21cca3 2021-06-29 op *p++ = c;
448 3b21cca3 2021-06-29 op continue;
449 3b21cca3 2021-06-29 op }
450 3b21cca3 2021-06-29 op *p = 0;
451 3b21cca3 2021-06-29 op break;
452 3b21cca3 2021-06-29 op }
453 3b21cca3 2021-06-29 op p = v;
454 3b21cca3 2021-06-29 op if ((val = symget(p)) == NULL) {
455 3b21cca3 2021-06-29 op yyerror("macro '%s' not defined", v);
456 bfb076ed 2021-06-29 op return TERR;
457 3b21cca3 2021-06-29 op }
458 3b21cca3 2021-06-29 op len = strlen(val);
459 3b21cca3 2021-06-29 op if (p + len >= ebuf - 1) {
460 3b21cca3 2021-06-29 op yyerror("after macro-expansion, "
461 3b21cca3 2021-06-29 op "string too long");
462 bfb076ed 2021-06-29 op return TERR;
463 3b21cca3 2021-06-29 op }
464 3b21cca3 2021-06-29 op *p = '\0';
465 3b21cca3 2021-06-29 op strlcat(p, val, ebuf - p);
466 3b21cca3 2021-06-29 op p += len;
467 bfb076ed 2021-06-29 op nonkw = 1;
468 bfb076ed 2021-06-29 op goto eow;
469 3b21cca3 2021-06-29 op }
470 3b21cca3 2021-06-29 op break;
471 74f0778b 2021-06-16 op case '\n':
472 74f0778b 2021-06-16 op if (quotes)
473 74f0778b 2021-06-16 op yyerror("unterminated quotes in column %d",
474 ef129b08 2021-06-16 op yylval.colno+1);
475 74f0778b 2021-06-16 op if (escape) {
476 74f0778b 2021-06-16 op nonkw = 1;
477 74f0778b 2021-06-16 op escape = 0;
478 ef129b08 2021-06-16 op yylval.colno = 0;
479 ef129b08 2021-06-16 op yylval.lineno++;
480 74f0778b 2021-06-16 op }
481 74f0778b 2021-06-16 op goto eow;
482 74f0778b 2021-06-16 op case EOF:
483 74f0778b 2021-06-16 op if (escape)
484 74f0778b 2021-06-16 op yyerror("unterminated escape in column %d",
485 ef129b08 2021-06-16 op yylval.colno);
486 74f0778b 2021-06-16 op if (quotes)
487 74f0778b 2021-06-16 op yyerror("unterminated quotes in column %d",
488 74f0778b 2021-06-16 op qpos+1);
489 74f0778b 2021-06-16 op goto eow;
490 74f0778b 2021-06-16 op case '{':
491 74f0778b 2021-06-16 op case '}':
492 74f0778b 2021-06-16 op case '#':
493 74f0778b 2021-06-16 op case ' ':
494 74f0778b 2021-06-16 op case '\t':
495 74f0778b 2021-06-16 op if (!escape && !quotes)
496 74f0778b 2021-06-16 op goto eow;
497 74f0778b 2021-06-16 op break;
498 74f0778b 2021-06-16 op case '"':
499 74f0778b 2021-06-16 op if (!escape) {
500 74f0778b 2021-06-16 op quotes = !quotes;
501 74f0778b 2021-06-16 op if (quotes) {
502 74f0778b 2021-06-16 op nonkw = 1;
503 ef129b08 2021-06-16 op qpos = yylval.colno;
504 74f0778b 2021-06-16 op }
505 74f0778b 2021-06-16 op continue;
506 74f0778b 2021-06-16 op }
507 74f0778b 2021-06-16 op }
508 74f0778b 2021-06-16 op *p++ = c;
509 74f0778b 2021-06-16 op if (p == ebuf) {
510 74f0778b 2021-06-16 op yyerror("line too long");
511 74f0778b 2021-06-16 op p = buf;
512 74f0778b 2021-06-16 op }
513 74f0778b 2021-06-16 op escape = 0;
514 74f0778b 2021-06-16 op }
515 74f0778b 2021-06-16 op
516 74f0778b 2021-06-16 op eow:
517 74f0778b 2021-06-16 op *p = 0;
518 74f0778b 2021-06-16 op if (c != EOF)
519 74f0778b 2021-06-16 op ungetc(c, yyfp);
520 74f0778b 2021-06-16 op if (p == buf) {
521 74f0778b 2021-06-16 op /*
522 74f0778b 2021-06-16 op * There could be a number of reason for empty buffer,
523 74f0778b 2021-06-16 op * and we handle all of them here, to avoid cluttering
524 74f0778b 2021-06-16 op * the main loop.
525 74f0778b 2021-06-16 op */
526 74f0778b 2021-06-16 op if (c == EOF)
527 74f0778b 2021-06-16 op goto eof;
528 74f0778b 2021-06-16 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
529 74f0778b 2021-06-16 op goto repeat;
530 74f0778b 2021-06-16 op }
531 74f0778b 2021-06-16 op if (!nonkw) {
532 74f0778b 2021-06-16 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); ++i) {
533 74f0778b 2021-06-16 op if (!strcmp(buf, keywords[i].word))
534 74f0778b 2021-06-16 op return keywords[i].token;
535 74f0778b 2021-06-16 op }
536 74f0778b 2021-06-16 op }
537 74f0778b 2021-06-16 op c = *buf;
538 74f0778b 2021-06-16 op if (!nonkw && (c == '-' || isdigit(c))) {
539 8235a81c 2021-06-29 op yylval.v.num = parse_portno(buf);
540 74f0778b 2021-06-16 op return TNUM;
541 74f0778b 2021-06-16 op }
542 74f0778b 2021-06-16 op if (!nonkw && !strcmp(buf, "on")) {
543 8235a81c 2021-06-29 op yylval.v.num = 1;
544 74f0778b 2021-06-16 op return TBOOL;
545 74f0778b 2021-06-16 op }
546 74f0778b 2021-06-16 op if (!nonkw && !strcmp(buf, "off")) {
547 8235a81c 2021-06-29 op yylval.v.num = 0;
548 74f0778b 2021-06-16 op return TBOOL;
549 74f0778b 2021-06-16 op }
550 74f0778b 2021-06-16 op if ((str = strdup(buf)) == NULL)
551 74f0778b 2021-06-16 op err(1, "%s", __func__);
552 8235a81c 2021-06-29 op yylval.v.str = str;
553 74f0778b 2021-06-16 op return TSTRING;
554 74f0778b 2021-06-16 op
555 74f0778b 2021-06-16 op eof:
556 74f0778b 2021-06-16 op if (ferror(yyfp))
557 74f0778b 2021-06-16 op yyerror("input error reading config");
558 74f0778b 2021-06-16 op return 0;
559 13ed2fb6 2021-01-27 op }
560 13ed2fb6 2021-01-27 op
561 13ed2fb6 2021-01-27 op int
562 13ed2fb6 2021-01-27 op parse_portno(const char *p)
563 13ed2fb6 2021-01-27 op {
564 13ed2fb6 2021-01-27 op const char *errstr;
565 13ed2fb6 2021-01-27 op int n;
566 13ed2fb6 2021-01-27 op
567 13ed2fb6 2021-01-27 op n = strtonum(p, 0, UINT16_MAX, &errstr);
568 13ed2fb6 2021-01-27 op if (errstr != NULL)
569 2d34f732 2021-02-10 op yyerror("port number is %s: %s", errstr, p);
570 13ed2fb6 2021-01-27 op return n;
571 13ed2fb6 2021-01-27 op }
572 13ed2fb6 2021-01-27 op
573 13ed2fb6 2021-01-27 op void
574 13ed2fb6 2021-01-27 op parse_conf(const char *path)
575 13ed2fb6 2021-01-27 op {
576 3b21cca3 2021-06-29 op struct sym *sym, *next;
577 3b21cca3 2021-06-29 op
578 13ed2fb6 2021-01-27 op config_path = path;
579 74f0778b 2021-06-16 op if ((yyfp = fopen(path, "r")) == NULL)
580 48b69cb2 2021-04-28 op err(1, "cannot open config: %s", path);
581 13ed2fb6 2021-01-27 op yyparse();
582 74f0778b 2021-06-16 op fclose(yyfp);
583 13ed2fb6 2021-01-27 op
584 13ed2fb6 2021-01-27 op if (goterror)
585 13ed2fb6 2021-01-27 op exit(1);
586 b8e64ccd 2021-03-31 op
587 b8e64ccd 2021-03-31 op if (TAILQ_FIRST(&hosts)->domain == NULL)
588 48b69cb2 2021-04-28 op errx(1, "no vhost defined in %s", path);
589 3b21cca3 2021-06-29 op
590 3b21cca3 2021-06-29 op /* free unused macros */
591 3b21cca3 2021-06-29 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
592 3b21cca3 2021-06-29 op /* TODO: warn if !sym->used */
593 3b21cca3 2021-06-29 op if (!sym->persist) {
594 3b21cca3 2021-06-29 op free(sym->name);
595 3b21cca3 2021-06-29 op free(sym->val);
596 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
597 3b21cca3 2021-06-29 op free(sym);
598 3b21cca3 2021-06-29 op }
599 3b21cca3 2021-06-29 op }
600 13ed2fb6 2021-01-27 op }
601 e17642a7 2021-02-01 op
602 e17642a7 2021-02-01 op char *
603 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
604 e17642a7 2021-02-01 op {
605 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
606 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
607 e17642a7 2021-02-01 op return path;
608 e17642a7 2021-02-01 op }
609 6abda252 2021-02-06 op
610 6abda252 2021-02-06 op int
611 6abda252 2021-02-06 op check_block_code(int n)
612 6abda252 2021-02-06 op {
613 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
614 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
615 6abda252 2021-02-06 op return n;
616 6abda252 2021-02-06 op }
617 6abda252 2021-02-06 op
618 6abda252 2021-02-06 op char *
619 6abda252 2021-02-06 op check_block_fmt(char *fmt)
620 6abda252 2021-02-06 op {
621 6abda252 2021-02-06 op char *s;
622 6abda252 2021-02-06 op
623 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
624 6abda252 2021-02-06 op if (*s != '%')
625 6abda252 2021-02-06 op continue;
626 6abda252 2021-02-06 op switch (*++s) {
627 6abda252 2021-02-06 op case '%':
628 6abda252 2021-02-06 op case 'p':
629 6abda252 2021-02-06 op case 'q':
630 6abda252 2021-02-06 op case 'P':
631 6abda252 2021-02-06 op case 'N':
632 6abda252 2021-02-06 op break;
633 6abda252 2021-02-06 op default:
634 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
635 6abda252 2021-02-06 op }
636 6abda252 2021-02-06 op }
637 6abda252 2021-02-06 op
638 6abda252 2021-02-06 op return fmt;
639 6abda252 2021-02-06 op }
640 6abda252 2021-02-06 op
641 6abda252 2021-02-06 op int
642 6abda252 2021-02-06 op check_strip_no(int n)
643 6abda252 2021-02-06 op {
644 6abda252 2021-02-06 op if (n <= 0)
645 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
646 a709ddf5 2021-02-07 op return n;
647 a709ddf5 2021-02-07 op }
648 a709ddf5 2021-02-07 op
649 a709ddf5 2021-02-07 op int
650 a709ddf5 2021-02-07 op check_prefork_num(int n)
651 a709ddf5 2021-02-07 op {
652 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
653 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
654 6abda252 2021-02-06 op return n;
655 6abda252 2021-02-06 op }
656 49b73ba1 2021-02-10 op
657 49b73ba1 2021-02-10 op void
658 49b73ba1 2021-02-10 op advance_loc(void)
659 49b73ba1 2021-02-10 op {
660 b8e64ccd 2021-03-31 op loc = new_location();
661 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
662 49b73ba1 2021-02-10 op }
663 c705ecb1 2021-05-03 op
664 c705ecb1 2021-05-03 op void
665 c705ecb1 2021-05-03 op only_once(const void *ptr, const char *name)
666 c705ecb1 2021-05-03 op {
667 c705ecb1 2021-05-03 op if (ptr != NULL)
668 c705ecb1 2021-05-03 op yyerror("`%s' specified more than once", name);
669 c705ecb1 2021-05-03 op }
670 8ad1c570 2021-05-09 op
671 8ad1c570 2021-05-09 op void
672 8ad1c570 2021-05-09 op only_oncei(int i, const char *name)
673 8ad1c570 2021-05-09 op {
674 8ad1c570 2021-05-09 op if (i != -1)
675 8ad1c570 2021-05-09 op yyerror("`%s' specified more than once", name);
676 8ad1c570 2021-05-09 op }
677 8ad1c570 2021-05-09 op
678 8ad1c570 2021-05-09 op int
679 8ad1c570 2021-05-09 op fastcgi_conf(char *path, char *port, char *prog)
680 8ad1c570 2021-05-09 op {
681 8ad1c570 2021-05-09 op struct fcgi *f;
682 8ad1c570 2021-05-09 op int i;
683 8ad1c570 2021-05-09 op
684 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
685 8ad1c570 2021-05-09 op f = &fcgi[i];
686 fafc6849 2021-06-29 op
687 8ad1c570 2021-05-09 op if (f->path == NULL) {
688 8ad1c570 2021-05-09 op f->id = i;
689 8ad1c570 2021-05-09 op f->path = path;
690 8ad1c570 2021-05-09 op f->port = port;
691 8ad1c570 2021-05-09 op f->prog = prog;
692 8ad1c570 2021-05-09 op return i;
693 8ad1c570 2021-05-09 op }
694 8ad1c570 2021-05-09 op
695 8ad1c570 2021-05-09 op /* XXX: what to do with prog? */
696 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
697 8ad1c570 2021-05-09 op ((port == NULL && f->port == NULL) ||
698 8ad1c570 2021-05-09 op !strcmp(f->port, port))) {
699 8ad1c570 2021-05-09 op free(path);
700 8ad1c570 2021-05-09 op free(port);
701 8ad1c570 2021-05-09 op return i;
702 8ad1c570 2021-05-09 op }
703 8ad1c570 2021-05-09 op }
704 8ad1c570 2021-05-09 op
705 8ad1c570 2021-05-09 op yyerror("too much `fastcgi' rules defined.");
706 8ad1c570 2021-05-09 op return -1;
707 c92b802b 2021-06-11 op }
708 c92b802b 2021-06-11 op
709 c92b802b 2021-06-11 op void
710 c92b802b 2021-06-11 op add_param(char *name, char *val, int env)
711 c92b802b 2021-06-11 op {
712 c92b802b 2021-06-11 op struct envlist *e;
713 c92b802b 2021-06-11 op struct envhead *h;
714 c92b802b 2021-06-11 op
715 c92b802b 2021-06-11 op if (env)
716 c92b802b 2021-06-11 op h = &host->env;
717 c92b802b 2021-06-11 op else
718 c92b802b 2021-06-11 op h = &host->params;
719 c92b802b 2021-06-11 op
720 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
721 c92b802b 2021-06-11 op e->name = name;
722 c92b802b 2021-06-11 op e->value = val;
723 c92b802b 2021-06-11 op if (TAILQ_EMPTY(h))
724 c92b802b 2021-06-11 op TAILQ_INSERT_HEAD(h, e, envs);
725 c92b802b 2021-06-11 op else
726 c92b802b 2021-06-11 op TAILQ_INSERT_TAIL(h, e, envs);
727 3b21cca3 2021-06-29 op }
728 3b21cca3 2021-06-29 op
729 3b21cca3 2021-06-29 op int
730 3b21cca3 2021-06-29 op symset(const char *name, const char *val, int persist)
731 3b21cca3 2021-06-29 op {
732 3b21cca3 2021-06-29 op struct sym *sym;
733 3b21cca3 2021-06-29 op
734 3b21cca3 2021-06-29 op TAILQ_FOREACH(sym, &symhead, entry) {
735 3b21cca3 2021-06-29 op if (!strcmp(name, sym->name))
736 3b21cca3 2021-06-29 op break;
737 3b21cca3 2021-06-29 op }
738 3b21cca3 2021-06-29 op
739 3b21cca3 2021-06-29 op if (sym != NULL) {
740 3b21cca3 2021-06-29 op if (sym->persist)
741 3b21cca3 2021-06-29 op return 0;
742 3b21cca3 2021-06-29 op else {
743 3b21cca3 2021-06-29 op free(sym->name);
744 3b21cca3 2021-06-29 op free(sym->val);
745 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
746 3b21cca3 2021-06-29 op free(sym);
747 3b21cca3 2021-06-29 op }
748 3b21cca3 2021-06-29 op }
749 3b21cca3 2021-06-29 op
750 3b21cca3 2021-06-29 op sym = xcalloc(1, sizeof(*sym));
751 3b21cca3 2021-06-29 op sym->name = xstrdup(name);
752 3b21cca3 2021-06-29 op sym->val = xstrdup(val);
753 3b21cca3 2021-06-29 op sym->used = 0;
754 3b21cca3 2021-06-29 op sym->persist = persist;
755 3b21cca3 2021-06-29 op
756 3b21cca3 2021-06-29 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
757 3b21cca3 2021-06-29 op return 0;
758 8ad1c570 2021-05-09 op }
759 3b21cca3 2021-06-29 op
760 3b21cca3 2021-06-29 op int
761 3b21cca3 2021-06-29 op cmdline_symset(char *s)
762 3b21cca3 2021-06-29 op {
763 3b21cca3 2021-06-29 op char *sym, *val;
764 3b21cca3 2021-06-29 op int ret;
765 3b21cca3 2021-06-29 op
766 3b21cca3 2021-06-29 op if ((val = strrchr(s, '=')) == NULL)
767 3b21cca3 2021-06-29 op return -1;
768 3b21cca3 2021-06-29 op sym = xcalloc(1, val - s + 1);
769 3b21cca3 2021-06-29 op memcpy(sym, s, val - s);
770 3b21cca3 2021-06-29 op ret = symset(sym, val + 1, 1);
771 3b21cca3 2021-06-29 op free(sym);
772 3b21cca3 2021-06-29 op return ret;
773 3b21cca3 2021-06-29 op }
774 3b21cca3 2021-06-29 op
775 3b21cca3 2021-06-29 op char *
776 3b21cca3 2021-06-29 op symget(const char *name)
777 3b21cca3 2021-06-29 op {
778 3b21cca3 2021-06-29 op struct sym *sym;
779 3b21cca3 2021-06-29 op
780 3b21cca3 2021-06-29 op TAILQ_FOREACH(sym, &symhead, entry) {
781 3b21cca3 2021-06-29 op if (!strcmp(name, sym->name)) {
782 3b21cca3 2021-06-29 op sym->used = 1;
783 3b21cca3 2021-06-29 op return sym->val;
784 3b21cca3 2021-06-29 op }
785 3b21cca3 2021-06-29 op }
786 3b21cca3 2021-06-29 op
787 3b21cca3 2021-06-29 op return NULL;
788 3b21cca3 2021-06-29 op }