Blame


1 15902770 2021-01-15 op %{
2 15902770 2021-01-15 op
3 15902770 2021-01-15 op /*
4 e2f167af 2021-01-02 op * Copyright (c) 2021, 2022 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 1f1f3810 2022-02-03 op
26 1f1f3810 2022-02-03 op #include "gmid.h"
27 15902770 2021-01-15 op
28 74f0778b 2021-06-16 op #include <ctype.h>
29 002a84a1 2021-02-10 op #include <errno.h>
30 534afd0d 2022-10-05 op #include <netdb.h>
31 6abda252 2021-02-06 op #include <stdarg.h>
32 15902770 2021-01-15 op #include <stdio.h>
33 74f0778b 2021-06-16 op #include <stdlib.h>
34 32693ee6 2021-01-28 op #include <string.h>
35 df5058c9 2023-06-05 op
36 df5058c9 2023-06-05 op #include "log.h"
37 15902770 2021-01-15 op
38 c39be742 2021-07-09 op TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
39 c39be742 2021-07-09 op static struct file {
40 c39be742 2021-07-09 op TAILQ_ENTRY(file) entry;
41 c39be742 2021-07-09 op FILE *stream;
42 c39be742 2021-07-09 op char *name;
43 c39be742 2021-07-09 op size_t ungetpos;
44 c39be742 2021-07-09 op size_t ungetsize;
45 c39be742 2021-07-09 op u_char *ungetbuf;
46 c39be742 2021-07-09 op int eof_reached;
47 c39be742 2021-07-09 op int lineno;
48 c39be742 2021-07-09 op int errors;
49 c39be742 2021-07-09 op } *file, *topfile;
50 74f0778b 2021-06-16 op
51 c39be742 2021-07-09 op struct file *pushfile(const char *, int);
52 c39be742 2021-07-09 op int popfile(void);
53 c39be742 2021-07-09 op int yyparse(void);
54 c39be742 2021-07-09 op int yylex(void);
55 c39be742 2021-07-09 op void yyerror(const char *, ...)
56 c39be742 2021-07-09 op __attribute__((__format__ (printf, 1, 2)))
57 c39be742 2021-07-09 op __attribute__((__nonnull__ (1)));
58 f3966209 2021-07-13 op void yywarn(const char *, ...)
59 f3966209 2021-07-13 op __attribute__((__format__ (printf, 1, 2)))
60 f3966209 2021-07-13 op __attribute__((__nonnull__ (1)));
61 c39be742 2021-07-09 op int kw_cmp(const void *, const void *);
62 c39be742 2021-07-09 op int lookup(char *);
63 c39be742 2021-07-09 op int igetc(void);
64 c39be742 2021-07-09 op int lgetc(int);
65 c39be742 2021-07-09 op void lungetc(int);
66 c39be742 2021-07-09 op int findeol(void);
67 ef129b08 2021-06-16 op
68 15902770 2021-01-15 op /*
69 15902770 2021-01-15 op * #define YYDEBUG 1
70 15902770 2021-01-15 op * int yydebug = 1;
71 15902770 2021-01-15 op */
72 15902770 2021-01-15 op
73 3b21cca3 2021-06-29 op TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
74 3b21cca3 2021-06-29 op struct sym {
75 3b21cca3 2021-06-29 op TAILQ_ENTRY(sym) entry;
76 3b21cca3 2021-06-29 op int used;
77 3b21cca3 2021-06-29 op int persist;
78 3b21cca3 2021-06-29 op char *name;
79 3b21cca3 2021-06-29 op char *val;
80 3b21cca3 2021-06-29 op };
81 3b21cca3 2021-06-29 op
82 c39be742 2021-07-09 op int symset(const char *, const char *, int);
83 c39be742 2021-07-09 op char *symget(const char *);
84 15902770 2021-01-15 op
85 c39be742 2021-07-09 op struct vhost *new_vhost(void);
86 c39be742 2021-07-09 op struct location *new_location(void);
87 b7967bc1 2021-01-02 op struct proxy *new_proxy(void);
88 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
89 6abda252 2021-02-06 op int check_block_code(int);
90 6abda252 2021-02-06 op char *check_block_fmt(char*);
91 6abda252 2021-02-06 op int check_strip_no(int);
92 391825e3 2021-07-09 op int check_port_num(int);
93 a709ddf5 2021-02-07 op int check_prefork_num(int);
94 49b73ba1 2021-02-10 op void advance_loc(void);
95 b7967bc1 2021-01-02 op void advance_proxy(void);
96 b7967bc1 2021-01-02 op void parsehp(char *, char **, const char **, const char *);
97 7b00c890 2022-10-05 op int fastcgi_conf(const char *, const char *);
98 2025e96d 2022-09-10 op void add_param(char *, char *);
99 534afd0d 2022-10-05 op int getservice(const char *);
100 13ed2fb6 2021-01-27 op
101 c39be742 2021-07-09 op static struct vhost *host;
102 c39be742 2021-07-09 op static struct location *loc;
103 b7967bc1 2021-01-02 op static struct proxy *proxy;
104 ee219d70 2022-02-26 op static char *current_media;
105 c39be742 2021-07-09 op static int errors;
106 c39be742 2021-07-09 op
107 c39be742 2021-07-09 op typedef struct {
108 c39be742 2021-07-09 op union {
109 c39be742 2021-07-09 op char *string;
110 c39be742 2021-07-09 op int number;
111 c39be742 2021-07-09 op } v;
112 c39be742 2021-07-09 op int lineno;
113 c39be742 2021-07-09 op } YYSTYPE;
114 c39be742 2021-07-09 op
115 15902770 2021-01-15 op %}
116 15902770 2021-01-15 op
117 15902770 2021-01-15 op /* for bison: */
118 15902770 2021-01-15 op /* %define parse.error verbose */
119 15902770 2021-01-15 op
120 c74c7030 2021-07-19 op %token ALIAS AUTO
121 c74c7030 2021-07-19 op %token BLOCK
122 d29a2ee2 2022-09-06 op %token CA CERT CHROOT CLIENT
123 c74c7030 2021-07-19 op %token DEFAULT
124 b7967bc1 2021-01-02 op %token FASTCGI FOR_HOST
125 c74c7030 2021-07-19 op %token INCLUDE INDEX IPV6
126 c74c7030 2021-07-19 op %token KEY
127 c74c7030 2021-07-19 op %token LANG LOCATION LOG
128 ff05125e 2021-10-15 op %token OCSP OFF ON
129 b7967bc1 2021-01-02 op %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
130 72b033ef 2021-12-29 op %token RELAY_TO REQUIRE RETURN ROOT
131 7b00c890 2022-10-05 op %token SERVER SNI STRIP
132 ee219d70 2022-02-26 op %token TCP TOEXT TYPE TYPES
133 593e412b 2021-01-01 op %token USE_TLS USER
134 5128c0b0 2021-01-01 op %token VERIFYNAME
135 d06d6f4b 2021-04-29 op
136 c39be742 2021-07-09 op %token ERROR
137 7252049d 2021-06-29 op
138 c39be742 2021-07-09 op %token <v.string> STRING
139 c39be742 2021-07-09 op %token <v.number> NUM
140 15902770 2021-01-15 op
141 534afd0d 2022-10-05 op %type <v.number> bool proxy_port
142 ee219d70 2022-02-26 op %type <v.string> string numberstring
143 98f52178 2021-06-29 op
144 15902770 2021-01-15 op %%
145 15902770 2021-01-15 op
146 6b86655a 2021-06-29 op conf : /* empty */
147 c39be742 2021-07-09 op | conf include '\n'
148 c39be742 2021-07-09 op | conf '\n'
149 c39be742 2021-07-09 op | conf varset '\n'
150 c39be742 2021-07-09 op | conf option '\n'
151 c39be742 2021-07-09 op | conf vhost '\n'
152 ee219d70 2022-02-26 op | conf types '\n'
153 c39be742 2021-07-09 op | conf error '\n' { file->errors++; }
154 3b21cca3 2021-06-29 op ;
155 3b21cca3 2021-06-29 op
156 c39be742 2021-07-09 op include : INCLUDE STRING {
157 c39be742 2021-07-09 op struct file *nfile;
158 c39be742 2021-07-09 op
159 c39be742 2021-07-09 op if ((nfile = pushfile($2, 0)) == NULL) {
160 c39be742 2021-07-09 op yyerror("failed to include file %s", $2);
161 c39be742 2021-07-09 op free($2);
162 c39be742 2021-07-09 op YYERROR;
163 c39be742 2021-07-09 op }
164 c39be742 2021-07-09 op free($2);
165 c39be742 2021-07-09 op
166 c39be742 2021-07-09 op file = nfile;
167 c39be742 2021-07-09 op lungetc('\n');
168 c39be742 2021-07-09 op }
169 c39be742 2021-07-09 op ;
170 c39be742 2021-07-09 op
171 c74c7030 2021-07-19 op bool : ON { $$ = 1; }
172 c74c7030 2021-07-19 op | OFF { $$ = 0; }
173 c39be742 2021-07-09 op ;
174 c39be742 2021-07-09 op
175 c39be742 2021-07-09 op string : string STRING {
176 98f52178 2021-06-29 op if (asprintf(&$$, "%s%s", $1, $2) == -1) {
177 98f52178 2021-06-29 op free($1);
178 98f52178 2021-06-29 op free($2);
179 98f52178 2021-06-29 op yyerror("string: asprintf: %s", strerror(errno));
180 98f52178 2021-06-29 op YYERROR;
181 98f52178 2021-06-29 op }
182 98f52178 2021-06-29 op free($1);
183 98f52178 2021-06-29 op free($2);
184 98f52178 2021-06-29 op }
185 c39be742 2021-07-09 op | STRING
186 98f52178 2021-06-29 op ;
187 98f52178 2021-06-29 op
188 ee219d70 2022-02-26 op numberstring : NUM {
189 ee219d70 2022-02-26 op char *s;
190 ee219d70 2022-02-26 op if (asprintf(&s, "%d", $1) == -1) {
191 ee219d70 2022-02-26 op yyerror("asprintf: number");
192 ee219d70 2022-02-26 op YYERROR;
193 ee219d70 2022-02-26 op }
194 ee219d70 2022-02-26 op $$ = s;
195 ee219d70 2022-02-26 op }
196 ee219d70 2022-02-26 op | STRING
197 ee219d70 2022-02-26 op ;
198 ee219d70 2022-02-26 op
199 c39be742 2021-07-09 op varset : STRING '=' string {
200 3b21cca3 2021-06-29 op char *s = $1;
201 3b21cca3 2021-06-29 op while (*s++) {
202 c39be742 2021-07-09 op if (isspace((unsigned char)*s)) {
203 3b21cca3 2021-06-29 op yyerror("macro name cannot contain "
204 3b21cca3 2021-06-29 op "whitespaces");
205 3b21cca3 2021-06-29 op free($1);
206 3b21cca3 2021-06-29 op free($3);
207 3b21cca3 2021-06-29 op YYERROR;
208 3b21cca3 2021-06-29 op }
209 3b21cca3 2021-06-29 op }
210 3b21cca3 2021-06-29 op symset($1, $3, 0);
211 3b21cca3 2021-06-29 op free($1);
212 3b21cca3 2021-06-29 op free($3);
213 3b21cca3 2021-06-29 op }
214 15902770 2021-01-15 op ;
215 15902770 2021-01-15 op
216 7277bb7d 2022-09-10 op option : CHROOT string {
217 7277bb7d 2022-09-10 op if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >=
218 7277bb7d 2022-09-10 op sizeof(conf.chroot))
219 7277bb7d 2022-09-10 op yyerror("chroot path too long");
220 7277bb7d 2022-09-10 op free($2);
221 7277bb7d 2022-09-10 op }
222 c74c7030 2021-07-19 op | IPV6 bool { conf.ipv6 = $2; }
223 c74c7030 2021-07-19 op | PORT NUM { conf.port = check_port_num($2); }
224 c74c7030 2021-07-19 op | PREFORK NUM { conf.prefork = check_prefork_num($2); }
225 c74c7030 2021-07-19 op | PROTOCOLS string {
226 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
227 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
228 3c4b712b 2021-01-01 op free($2);
229 5bc3c98e 2021-01-15 op }
230 7277bb7d 2022-09-10 op | USER string {
231 7277bb7d 2022-09-10 op if (strlcpy(conf.user, $2, sizeof(conf.user)) >=
232 7277bb7d 2022-09-10 op sizeof(conf.user))
233 7277bb7d 2022-09-10 op yyerror("user name too long");
234 7277bb7d 2022-09-10 op free($2);
235 7277bb7d 2022-09-10 op }
236 15902770 2021-01-15 op ;
237 15902770 2021-01-15 op
238 c74c7030 2021-07-19 op vhost : SERVER string {
239 b8e64ccd 2021-03-31 op host = new_vhost();
240 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
241 b8e64ccd 2021-03-31 op
242 b8e64ccd 2021-03-31 op loc = new_location();
243 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
244 b8e64ccd 2021-03-31 op
245 b7967bc1 2021-01-02 op TAILQ_INIT(&host->proxies);
246 b7967bc1 2021-01-02 op
247 534afd0d 2022-10-05 op (void) strlcpy(loc->match, "*", sizeof(loc->match));
248 534afd0d 2022-10-05 op (void) strlcpy(host->domain, $2, sizeof(host->domain));
249 15902770 2021-01-15 op
250 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
251 f3966209 2021-07-13 op yywarn("\"%s\" looks like punycode: you "
252 f3966209 2021-07-13 op "should use the decoded hostname", $2);
253 cbeee4ca 2021-01-28 op }
254 534afd0d 2022-10-05 op
255 534afd0d 2022-10-05 op free($2);
256 b7967bc1 2021-01-02 op } '{' optnl servbody '}' {
257 1c6967b3 2023-06-08 op if (host->cert_path == NULL ||
258 1c6967b3 2023-06-08 op host->key_path == NULL)
259 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
260 15902770 2021-01-15 op }
261 f3966209 2021-07-13 op | error '}' { yyerror("bad server directive"); }
262 15902770 2021-01-15 op ;
263 15902770 2021-01-15 op
264 b7967bc1 2021-01-02 op servbody : /* empty */
265 b7967bc1 2021-01-02 op | servbody servopt optnl
266 b7967bc1 2021-01-02 op | servbody location optnl
267 b7967bc1 2021-01-02 op | servbody proxy optnl
268 15902770 2021-01-15 op ;
269 15902770 2021-01-15 op
270 c74c7030 2021-07-19 op servopt : ALIAS string {
271 cc8c2901 2021-04-29 op struct alist *a;
272 cc8c2901 2021-04-29 op
273 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
274 534afd0d 2022-10-05 op (void) strlcpy(a->alias, $2, sizeof(a->alias));
275 534afd0d 2022-10-05 op free($2);
276 edc5ca66 2022-09-10 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
277 cc8c2901 2021-04-29 op }
278 c74c7030 2021-07-19 op | CERT string {
279 534afd0d 2022-10-05 op ensure_absolute_path($2);
280 1c6967b3 2023-06-08 op free(host->cert_path);
281 1c6967b3 2023-06-08 op host->cert_path = $2;
282 9cc630aa 2021-04-28 op }
283 c74c7030 2021-07-19 op | KEY string {
284 534afd0d 2022-10-05 op ensure_absolute_path($2);
285 1c6967b3 2023-06-08 op free(host->key_path);
286 1c6967b3 2023-06-08 op host->key_path = $2;
287 c92b802b 2021-06-11 op }
288 ff05125e 2021-10-15 op | OCSP string {
289 534afd0d 2022-10-05 op ensure_absolute_path($2);
290 1c6967b3 2023-06-08 op free(host->ocsp_path);
291 1c6967b3 2023-06-08 op host->ocsp_path = $2;
292 ff05125e 2021-10-15 op }
293 c74c7030 2021-07-19 op | PARAM string '=' string {
294 2025e96d 2022-09-10 op add_param($2, $4);
295 c705ecb1 2021-05-03 op }
296 c8b74339 2021-01-24 op | locopt
297 c8b74339 2021-01-24 op ;
298 7bdcc91e 2021-01-01 op
299 b7967bc1 2021-01-02 op proxy : PROXY { advance_proxy(); }
300 b7967bc1 2021-01-02 op proxy_matches '{' optnl proxy_opts '}' {
301 534afd0d 2022-10-05 op if (*proxy->host == '\0')
302 b7967bc1 2021-01-02 op yyerror("invalid proxy block: missing `relay-to' option");
303 b7967bc1 2021-01-02 op
304 deadd9e1 2023-06-09 op if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
305 deadd9e1 2023-06-09 op (proxy->cert_path != NULL && proxy->key_path == NULL))
306 b7967bc1 2021-01-02 op yyerror("invalid proxy block: missing cert or key");
307 b7967bc1 2021-01-02 op }
308 b7967bc1 2021-01-02 op ;
309 b7967bc1 2021-01-02 op
310 b7967bc1 2021-01-02 op proxy_matches : /* empty */
311 b7967bc1 2021-01-02 op | proxy_matches proxy_match
312 b7967bc1 2021-01-02 op ;
313 b7967bc1 2021-01-02 op
314 534afd0d 2022-10-05 op proxy_port : /* empty */ { $$ = 1965; }
315 534afd0d 2022-10-05 op | PORT STRING {
316 534afd0d 2022-10-05 op if (($$ = getservice($2)) == -1)
317 534afd0d 2022-10-05 op yyerror("invalid port number %s", $2);
318 534afd0d 2022-10-05 op free($2);
319 534afd0d 2022-10-05 op }
320 534afd0d 2022-10-05 op | PORT NUM { $$ = $2; }
321 534afd0d 2022-10-05 op ;
322 534afd0d 2022-10-05 op
323 b7967bc1 2021-01-02 op proxy_match : PROTO string {
324 534afd0d 2022-10-05 op (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
325 534afd0d 2022-10-05 op free($2);
326 b7967bc1 2021-01-02 op }
327 534afd0d 2022-10-05 op | FOR_HOST string proxy_port {
328 534afd0d 2022-10-05 op (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
329 534afd0d 2022-10-05 op (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
330 534afd0d 2022-10-05 op "%d", $3);
331 534afd0d 2022-10-05 op free($2);
332 b7967bc1 2021-01-02 op }
333 7bdcc91e 2021-01-01 op ;
334 7bdcc91e 2021-01-01 op
335 7bdcc91e 2021-01-01 op proxy_opts : /* empty */
336 7bdcc91e 2021-01-01 op | proxy_opts proxy_opt optnl
337 7bdcc91e 2021-01-01 op ;
338 7bdcc91e 2021-01-01 op
339 7bdcc91e 2021-01-01 op proxy_opt : CERT string {
340 deadd9e1 2023-06-09 op free(proxy->cert);
341 7bdcc91e 2021-01-01 op ensure_absolute_path($2);
342 deadd9e1 2023-06-09 op proxy->cert_path = $2;
343 7bdcc91e 2021-01-01 op }
344 7bdcc91e 2021-01-01 op | KEY string {
345 deadd9e1 2023-06-09 op free(proxy->key);
346 7bdcc91e 2021-01-01 op ensure_absolute_path($2);
347 deadd9e1 2023-06-09 op proxy->key_path = $2;
348 c7c8ef44 2021-01-01 op }
349 c7c8ef44 2021-01-01 op | PROTOCOLS string {
350 b7967bc1 2021-01-02 op if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
351 c7c8ef44 2021-01-01 op yyerror("invalid protocols string \"%s\"", $2);
352 3c4b712b 2021-01-01 op free($2);
353 7bdcc91e 2021-01-01 op }
354 534afd0d 2022-10-05 op | RELAY_TO string proxy_port {
355 534afd0d 2022-10-05 op (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
356 534afd0d 2022-10-05 op (void) snprintf(proxy->port, sizeof(proxy->port),
357 534afd0d 2022-10-05 op "%d", $3);
358 534afd0d 2022-10-05 op free($2);
359 593e412b 2021-01-01 op }
360 ba94a608 2022-01-04 op | REQUIRE CLIENT CA string {
361 ba94a608 2022-01-04 op ensure_absolute_path($4);
362 deadd9e1 2023-06-09 op proxy->reqca_path = $4;
363 1cdea97b 2022-01-30 op }
364 1cdea97b 2022-01-30 op | SNI string {
365 534afd0d 2022-10-05 op (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
366 534afd0d 2022-10-05 op free($2);
367 ba94a608 2022-01-04 op }
368 593e412b 2021-01-01 op | USE_TLS bool {
369 b7967bc1 2021-01-02 op proxy->notls = !$2;
370 7bdcc91e 2021-01-01 op }
371 5128c0b0 2021-01-01 op | VERIFYNAME bool {
372 b7967bc1 2021-01-02 op proxy->noverifyname = !$2;
373 5128c0b0 2021-01-01 op }
374 7bdcc91e 2021-01-01 op ;
375 7bdcc91e 2021-01-01 op
376 c74c7030 2021-07-19 op location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
377 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
378 49b73ba1 2021-02-10 op if (*$3 == '/')
379 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
380 534afd0d 2022-10-05 op (void) strlcpy(loc->match, $3, sizeof(loc->match));
381 534afd0d 2022-10-05 op free($3);
382 6119e13e 2021-01-19 op }
383 c8b74339 2021-01-24 op | error '}'
384 c8b74339 2021-01-24 op ;
385 c8b74339 2021-01-24 op
386 c8b74339 2021-01-24 op locopts : /* empty */
387 c39be742 2021-07-09 op | locopts locopt optnl
388 c8b74339 2021-01-24 op ;
389 c8b74339 2021-01-24 op
390 c74c7030 2021-07-19 op locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
391 c74c7030 2021-07-19 op | BLOCK RETURN NUM string {
392 534afd0d 2022-10-05 op check_block_fmt($4);
393 534afd0d 2022-10-05 op (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
394 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
395 534afd0d 2022-10-05 op free($4);
396 6abda252 2021-02-06 op }
397 c74c7030 2021-07-19 op | BLOCK RETURN NUM {
398 534afd0d 2022-10-05 op (void) strlcpy(loc->block_fmt, "temporary failure",
399 534afd0d 2022-10-05 op sizeof(loc->block_fmt));
400 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
401 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
402 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
403 6abda252 2021-02-06 op }
404 c74c7030 2021-07-19 op | BLOCK {
405 534afd0d 2022-10-05 op (void) strlcpy(loc->block_fmt, "temporary failure",
406 534afd0d 2022-10-05 op sizeof(loc->block_fmt));
407 6abda252 2021-02-06 op loc->block_code = 40;
408 6abda252 2021-02-06 op }
409 c74c7030 2021-07-19 op | DEFAULT TYPE string {
410 534afd0d 2022-10-05 op (void) strlcpy(loc->default_mime, $3,
411 534afd0d 2022-10-05 op sizeof(loc->default_mime));
412 534afd0d 2022-10-05 op free($3);
413 eb59f87e 2021-02-09 op }
414 abc8801d 2021-07-19 op | FASTCGI fastcgi
415 c74c7030 2021-07-19 op | INDEX string {
416 534afd0d 2022-10-05 op (void) strlcpy(loc->index, $2, sizeof(loc->index));
417 534afd0d 2022-10-05 op free($2);
418 eb59f87e 2021-02-09 op }
419 c74c7030 2021-07-19 op | LANG string {
420 534afd0d 2022-10-05 op (void) strlcpy(loc->lang, $2,
421 534afd0d 2022-10-05 op sizeof(loc->lang));
422 534afd0d 2022-10-05 op free($2);
423 eb59f87e 2021-02-09 op }
424 c74c7030 2021-07-19 op | LOG bool { loc->disable_log = !$2; }
425 da2185f3 2021-01-01 op | REQUIRE CLIENT CA string {
426 da2185f3 2021-01-01 op ensure_absolute_path($4);
427 deadd9e1 2023-06-09 op loc->reqca_path = $4;
428 da2185f3 2021-01-01 op }
429 da2185f3 2021-01-01 op | ROOT string {
430 534afd0d 2022-10-05 op (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
431 534afd0d 2022-10-05 op free($2);
432 da2185f3 2021-01-01 op }
433 da2185f3 2021-01-01 op | STRIP NUM { loc->strip = check_strip_no($2); }
434 da2185f3 2021-01-01 op ;
435 da2185f3 2021-01-01 op
436 7b00c890 2022-10-05 op fastcgi : string {
437 7b00c890 2022-10-05 op loc->fcgi = fastcgi_conf($1, NULL);
438 534afd0d 2022-10-05 op free($1);
439 0d047efc 2021-05-24 op }
440 c74c7030 2021-07-19 op | TCP string PORT NUM {
441 0d047efc 2021-05-24 op char *c;
442 762b9b99 2021-07-09 op if (asprintf(&c, "%d", $4) == -1)
443 0d047efc 2021-05-24 op err(1, "asprintf");
444 7b00c890 2022-10-05 op loc->fcgi = fastcgi_conf($2, c);
445 534afd0d 2022-10-05 op free($2);
446 0d047efc 2021-05-24 op }
447 c74c7030 2021-07-19 op | TCP string {
448 7b00c890 2022-10-05 op loc->fcgi = fastcgi_conf($2, "9000");
449 534afd0d 2022-10-05 op free($2);
450 0d047efc 2021-05-24 op }
451 c74c7030 2021-07-19 op | TCP string PORT string {
452 7b00c890 2022-10-05 op loc->fcgi = fastcgi_conf($2, $4);
453 534afd0d 2022-10-05 op free($2);
454 534afd0d 2022-10-05 op free($4);
455 0d047efc 2021-05-24 op }
456 ee219d70 2022-02-26 op ;
457 ee219d70 2022-02-26 op
458 cd5826b8 2022-09-10 op types : TYPES '{' optnl mediaopts_l '}' ;
459 0d047efc 2021-05-24 op
460 ee219d70 2022-02-26 op mediaopts_l : mediaopts_l mediaoptsl nl
461 ee219d70 2022-02-26 op | mediaoptsl nl
462 ee219d70 2022-02-26 op ;
463 ee219d70 2022-02-26 op
464 aa9543b9 2022-09-10 op mediaoptsl : STRING {
465 aa9543b9 2022-09-10 op free(current_media);
466 aa9543b9 2022-09-10 op current_media = $1;
467 aa9543b9 2022-09-10 op } medianames_l optsemicolon
468 ee219d70 2022-02-26 op | include
469 ee219d70 2022-02-26 op ;
470 ee219d70 2022-02-26 op
471 ee219d70 2022-02-26 op medianames_l : medianames_l medianamesl
472 ee219d70 2022-02-26 op | medianamesl
473 ee219d70 2022-02-26 op ;
474 ee219d70 2022-02-26 op
475 d8d170aa 2022-04-08 op medianamesl : numberstring {
476 d8d170aa 2022-04-08 op if (add_mime(&conf.mime, current_media, $1) == -1)
477 d8d170aa 2022-04-08 op err(1, "add_mime");
478 aa9543b9 2022-09-10 op free($1);
479 d8d170aa 2022-04-08 op }
480 ee219d70 2022-02-26 op ;
481 ee219d70 2022-02-26 op
482 ee219d70 2022-02-26 op nl : '\n' optnl
483 ee219d70 2022-02-26 op ;
484 ee219d70 2022-02-26 op
485 c39be742 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
486 67f49405 2021-07-09 op | ';' optnl /* semicolons too */
487 c39be742 2021-07-09 op | /*empty*/
488 c39be742 2021-07-09 op ;
489 fdea6aa0 2021-04-30 op
490 ee219d70 2022-02-26 op optsemicolon : ';'
491 ee219d70 2022-02-26 op |
492 ee219d70 2022-02-26 op ;
493 ee219d70 2022-02-26 op
494 c39be742 2021-07-09 op %%
495 b8e64ccd 2021-03-31 op
496 e5d82d94 2022-03-19 op static const struct keyword {
497 74f0778b 2021-06-16 op const char *word;
498 74f0778b 2021-06-16 op int token;
499 74f0778b 2021-06-16 op } keywords[] = {
500 d93c8191 2021-07-09 op /* these MUST be sorted */
501 c74c7030 2021-07-19 op {"alias", ALIAS},
502 c74c7030 2021-07-19 op {"auto", AUTO},
503 c74c7030 2021-07-19 op {"block", BLOCK},
504 c74c7030 2021-07-19 op {"ca", CA},
505 c74c7030 2021-07-19 op {"cert", CERT},
506 c74c7030 2021-07-19 op {"chroot", CHROOT},
507 c74c7030 2021-07-19 op {"client", CLIENT},
508 c74c7030 2021-07-19 op {"default", DEFAULT},
509 abc8801d 2021-07-19 op {"fastcgi", FASTCGI},
510 b7967bc1 2021-01-02 op {"for-host", FOR_HOST},
511 88971f9a 2022-02-26 op {"include", INCLUDE},
512 c74c7030 2021-07-19 op {"index", INDEX},
513 c74c7030 2021-07-19 op {"ipv6", IPV6},
514 c74c7030 2021-07-19 op {"key", KEY},
515 c74c7030 2021-07-19 op {"lang", LANG},
516 c74c7030 2021-07-19 op {"location", LOCATION},
517 c74c7030 2021-07-19 op {"log", LOG},
518 ff05125e 2021-10-15 op {"ocsp", OCSP},
519 c74c7030 2021-07-19 op {"off", OFF},
520 c74c7030 2021-07-19 op {"on", ON},
521 c74c7030 2021-07-19 op {"param", PARAM},
522 c74c7030 2021-07-19 op {"port", PORT},
523 c74c7030 2021-07-19 op {"prefork", PREFORK},
524 b7967bc1 2021-01-02 op {"proto", PROTO},
525 c74c7030 2021-07-19 op {"protocols", PROTOCOLS},
526 72b033ef 2021-12-29 op {"proxy", PROXY},
527 72b033ef 2021-12-29 op {"relay-to", RELAY_TO},
528 c74c7030 2021-07-19 op {"require", REQUIRE},
529 c74c7030 2021-07-19 op {"return", RETURN},
530 c74c7030 2021-07-19 op {"root", ROOT},
531 c74c7030 2021-07-19 op {"server", SERVER},
532 1cdea97b 2022-01-30 op {"sni", SNI},
533 c74c7030 2021-07-19 op {"strip", STRIP},
534 c74c7030 2021-07-19 op {"tcp", TCP},
535 c74c7030 2021-07-19 op {"to-ext", TOEXT},
536 c74c7030 2021-07-19 op {"type", TYPE},
537 ee219d70 2022-02-26 op {"types", TYPES},
538 593e412b 2021-01-01 op {"use-tls", USE_TLS},
539 c74c7030 2021-07-19 op {"user", USER},
540 5128c0b0 2021-01-01 op {"verifyname", VERIFYNAME},
541 74f0778b 2021-06-16 op };
542 74f0778b 2021-06-16 op
543 c39be742 2021-07-09 op void
544 c39be742 2021-07-09 op yyerror(const char *msg, ...)
545 c39be742 2021-07-09 op {
546 c39be742 2021-07-09 op va_list ap;
547 c39be742 2021-07-09 op
548 c39be742 2021-07-09 op file->errors++;
549 c39be742 2021-07-09 op
550 c39be742 2021-07-09 op va_start(ap, msg);
551 f3966209 2021-07-13 op fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
552 f3966209 2021-07-13 op vfprintf(stderr, msg, ap);
553 f3966209 2021-07-13 op fprintf(stderr, "\n");
554 f3966209 2021-07-13 op va_end(ap);
555 f3966209 2021-07-13 op }
556 f3966209 2021-07-13 op
557 f3966209 2021-07-13 op void
558 f3966209 2021-07-13 op yywarn(const char *msg, ...)
559 f3966209 2021-07-13 op {
560 f3966209 2021-07-13 op va_list ap;
561 f3966209 2021-07-13 op
562 f3966209 2021-07-13 op va_start(ap, msg);
563 f3966209 2021-07-13 op fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
564 c39be742 2021-07-09 op vfprintf(stderr, msg, ap);
565 c39be742 2021-07-09 op fprintf(stderr, "\n");
566 c39be742 2021-07-09 op va_end(ap);
567 c39be742 2021-07-09 op }
568 c39be742 2021-07-09 op
569 d93c8191 2021-07-09 op int
570 d93c8191 2021-07-09 op kw_cmp(const void *k, const void *e)
571 d93c8191 2021-07-09 op {
572 d93c8191 2021-07-09 op return strcmp(k, ((struct keyword *)e)->word);
573 d93c8191 2021-07-09 op }
574 d93c8191 2021-07-09 op
575 c39be742 2021-07-09 op int
576 c39be742 2021-07-09 op lookup(char *s)
577 74f0778b 2021-06-16 op {
578 c39be742 2021-07-09 op const struct keyword *p;
579 74f0778b 2021-06-16 op
580 c39be742 2021-07-09 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
581 c39be742 2021-07-09 op sizeof(keywords[0]), kw_cmp);
582 74f0778b 2021-06-16 op
583 c39be742 2021-07-09 op if (p)
584 c39be742 2021-07-09 op return p->token;
585 c39be742 2021-07-09 op else
586 c39be742 2021-07-09 op return STRING;
587 c39be742 2021-07-09 op }
588 c39be742 2021-07-09 op
589 c39be742 2021-07-09 op #define START_EXPAND 1
590 c39be742 2021-07-09 op #define DONE_EXPAND 2
591 c39be742 2021-07-09 op
592 c39be742 2021-07-09 op static int expanding;
593 c39be742 2021-07-09 op
594 c39be742 2021-07-09 op int
595 c39be742 2021-07-09 op igetc(void)
596 c39be742 2021-07-09 op {
597 c39be742 2021-07-09 op int c;
598 c39be742 2021-07-09 op
599 c39be742 2021-07-09 op while (1) {
600 c39be742 2021-07-09 op if (file->ungetpos > 0)
601 c39be742 2021-07-09 op c = file->ungetbuf[--file->ungetpos];
602 c39be742 2021-07-09 op else
603 c39be742 2021-07-09 op c = getc(file->stream);
604 c39be742 2021-07-09 op
605 c39be742 2021-07-09 op if (c == START_EXPAND)
606 c39be742 2021-07-09 op expanding = 1;
607 c39be742 2021-07-09 op else if (c == DONE_EXPAND)
608 c39be742 2021-07-09 op expanding = 0;
609 c39be742 2021-07-09 op else
610 c39be742 2021-07-09 op break;
611 74f0778b 2021-06-16 op }
612 c39be742 2021-07-09 op return c;
613 c39be742 2021-07-09 op }
614 74f0778b 2021-06-16 op
615 c39be742 2021-07-09 op int
616 c39be742 2021-07-09 op lgetc(int quotec)
617 c39be742 2021-07-09 op {
618 c39be742 2021-07-09 op int c, next;
619 c39be742 2021-07-09 op
620 c39be742 2021-07-09 op if (quotec) {
621 c39be742 2021-07-09 op if ((c = igetc()) == EOF) {
622 c39be742 2021-07-09 op yyerror("reached end of file while parsing "
623 c39be742 2021-07-09 op "quoted string");
624 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
625 c39be742 2021-07-09 op return EOF;
626 c39be742 2021-07-09 op return quotec;
627 c39be742 2021-07-09 op }
628 74f0778b 2021-06-16 op return c;
629 74f0778b 2021-06-16 op }
630 74f0778b 2021-06-16 op
631 c39be742 2021-07-09 op while ((c = igetc()) == '\\') {
632 c39be742 2021-07-09 op next = igetc();
633 c39be742 2021-07-09 op if (next != '\n') {
634 c39be742 2021-07-09 op c = next;
635 74f0778b 2021-06-16 op break;
636 c39be742 2021-07-09 op }
637 c39be742 2021-07-09 op yylval.lineno = file->lineno;
638 c39be742 2021-07-09 op file->lineno++;
639 c39be742 2021-07-09 op }
640 3b21cca3 2021-06-29 op
641 c39be742 2021-07-09 op if (c == EOF) {
642 c39be742 2021-07-09 op /*
643 c39be742 2021-07-09 op * Fake EOL when hit EOF for the first time. This gets line
644 c39be742 2021-07-09 op * count right if last line in included file is syntactically
645 c39be742 2021-07-09 op * invalid and has no newline.
646 c39be742 2021-07-09 op */
647 c39be742 2021-07-09 op if (file->eof_reached == 0) {
648 c39be742 2021-07-09 op file->eof_reached = 1;
649 c39be742 2021-07-09 op return '\n';
650 c39be742 2021-07-09 op }
651 c39be742 2021-07-09 op while (c == EOF) {
652 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
653 c39be742 2021-07-09 op return EOF;
654 c39be742 2021-07-09 op c = igetc();
655 c39be742 2021-07-09 op }
656 c39be742 2021-07-09 op }
657 c39be742 2021-07-09 op return c;
658 c39be742 2021-07-09 op }
659 c39be742 2021-07-09 op
660 c39be742 2021-07-09 op void
661 c39be742 2021-07-09 op lungetc(int c)
662 c39be742 2021-07-09 op {
663 c39be742 2021-07-09 op if (c == EOF)
664 c39be742 2021-07-09 op return;
665 c39be742 2021-07-09 op
666 c39be742 2021-07-09 op if (file->ungetpos >= file->ungetsize) {
667 c39be742 2021-07-09 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
668 c39be742 2021-07-09 op if (p == NULL)
669 2dd5994a 2023-06-06 op fatal("lungetc");
670 c39be742 2021-07-09 op file->ungetbuf = p;
671 c39be742 2021-07-09 op file->ungetsize *= 2;
672 c39be742 2021-07-09 op }
673 c39be742 2021-07-09 op file->ungetbuf[file->ungetpos++] = c;
674 c39be742 2021-07-09 op }
675 c39be742 2021-07-09 op
676 c39be742 2021-07-09 op int
677 c39be742 2021-07-09 op findeol(void)
678 c39be742 2021-07-09 op {
679 c39be742 2021-07-09 op int c;
680 c39be742 2021-07-09 op
681 c39be742 2021-07-09 op /* Skip to either EOF or the first real EOL. */
682 c39be742 2021-07-09 op while (1) {
683 c39be742 2021-07-09 op c = lgetc(0);
684 c39be742 2021-07-09 op if (c == '\n') {
685 c39be742 2021-07-09 op file->lineno++;
686 3b21cca3 2021-06-29 op break;
687 c39be742 2021-07-09 op }
688 c39be742 2021-07-09 op if (c == EOF)
689 74f0778b 2021-06-16 op break;
690 c39be742 2021-07-09 op }
691 c39be742 2021-07-09 op return ERROR;
692 c39be742 2021-07-09 op }
693 c39be742 2021-07-09 op
694 c39be742 2021-07-09 op int
695 c39be742 2021-07-09 op yylex(void)
696 c39be742 2021-07-09 op {
697 1bd706dc 2021-07-09 op char buf[8096];
698 1bd706dc 2021-07-09 op char *p, *val;
699 1bd706dc 2021-07-09 op int quotec, next, c;
700 1bd706dc 2021-07-09 op int token;
701 c39be742 2021-07-09 op
702 c39be742 2021-07-09 op top:
703 c39be742 2021-07-09 op p = buf;
704 c39be742 2021-07-09 op while ((c = lgetc(0)) == ' ' || c == '\t')
705 c39be742 2021-07-09 op ; /* nothing */
706 c39be742 2021-07-09 op
707 c39be742 2021-07-09 op yylval.lineno = file->lineno;
708 c39be742 2021-07-09 op if (c == '#')
709 c39be742 2021-07-09 op while ((c = lgetc(0)) != '\n' && c != EOF)
710 c39be742 2021-07-09 op ; /* nothing */
711 c39be742 2021-07-09 op if (c == '$' && !expanding) {
712 c39be742 2021-07-09 op while (1) {
713 c39be742 2021-07-09 op if ((c = lgetc(0)) == EOF)
714 c39be742 2021-07-09 op return 0;
715 67f49405 2021-07-09 op if (p + 1 >= buf + sizeof(buf) -1) {
716 67f49405 2021-07-09 op yyerror("string too long");
717 67f49405 2021-07-09 op return findeol();
718 67f49405 2021-07-09 op }
719 67f49405 2021-07-09 op if (isalnum(c) || c == '_') {
720 67f49405 2021-07-09 op *p++ = c;
721 67f49405 2021-07-09 op continue;
722 67f49405 2021-07-09 op }
723 67f49405 2021-07-09 op *p = '\0';
724 67f49405 2021-07-09 op lungetc(c);
725 67f49405 2021-07-09 op break;
726 67f49405 2021-07-09 op }
727 67f49405 2021-07-09 op val = symget(buf);
728 67f49405 2021-07-09 op if (val == NULL) {
729 67f49405 2021-07-09 op yyerror("macro `%s' not defined", buf);
730 67f49405 2021-07-09 op return findeol();
731 67f49405 2021-07-09 op }
732 67f49405 2021-07-09 op yylval.v.string = xstrdup(val);
733 67f49405 2021-07-09 op return STRING;
734 67f49405 2021-07-09 op }
735 67f49405 2021-07-09 op if (c == '@' && !expanding) {
736 67f49405 2021-07-09 op while (1) {
737 67f49405 2021-07-09 op if ((c = lgetc(0)) == EOF)
738 67f49405 2021-07-09 op return 0;
739 c39be742 2021-07-09 op
740 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
741 c39be742 2021-07-09 op yyerror("string too long");
742 c39be742 2021-07-09 op return findeol();
743 c39be742 2021-07-09 op }
744 c39be742 2021-07-09 op if (isalnum(c) || c == '_') {
745 c39be742 2021-07-09 op *p++ = c;
746 74f0778b 2021-06-16 op continue;
747 74f0778b 2021-06-16 op }
748 c39be742 2021-07-09 op *p = '\0';
749 c39be742 2021-07-09 op lungetc(c);
750 c39be742 2021-07-09 op break;
751 74f0778b 2021-06-16 op }
752 c39be742 2021-07-09 op val = symget(buf);
753 c39be742 2021-07-09 op if (val == NULL) {
754 c39be742 2021-07-09 op yyerror("macro '%s' not defined", buf);
755 c39be742 2021-07-09 op return findeol();
756 74f0778b 2021-06-16 op }
757 c39be742 2021-07-09 op p = val + strlen(val) - 1;
758 c39be742 2021-07-09 op lungetc(DONE_EXPAND);
759 c39be742 2021-07-09 op while (p >= val) {
760 c39be742 2021-07-09 op lungetc(*p);
761 c39be742 2021-07-09 op p--;
762 c39be742 2021-07-09 op }
763 c39be742 2021-07-09 op lungetc(START_EXPAND);
764 c39be742 2021-07-09 op goto top;
765 74f0778b 2021-06-16 op }
766 74f0778b 2021-06-16 op
767 c39be742 2021-07-09 op switch (c) {
768 c39be742 2021-07-09 op case '\'':
769 c39be742 2021-07-09 op case '"':
770 c39be742 2021-07-09 op quotec = c;
771 c39be742 2021-07-09 op while (1) {
772 c39be742 2021-07-09 op if ((c = lgetc(quotec)) == EOF)
773 c39be742 2021-07-09 op return 0;
774 c39be742 2021-07-09 op if (c == '\n') {
775 c39be742 2021-07-09 op file->lineno++;
776 c39be742 2021-07-09 op continue;
777 c39be742 2021-07-09 op } else if (c == '\\') {
778 c39be742 2021-07-09 op if ((next = lgetc(quotec)) == EOF)
779 c39be742 2021-07-09 op return (0);
780 c39be742 2021-07-09 op if (next == quotec || next == ' ' ||
781 c39be742 2021-07-09 op next == '\t')
782 c39be742 2021-07-09 op c = next;
783 c39be742 2021-07-09 op else if (next == '\n') {
784 c39be742 2021-07-09 op file->lineno++;
785 c39be742 2021-07-09 op continue;
786 c39be742 2021-07-09 op } else
787 c39be742 2021-07-09 op lungetc(next);
788 c39be742 2021-07-09 op } else if (c == quotec) {
789 c39be742 2021-07-09 op *p = '\0';
790 c39be742 2021-07-09 op break;
791 c39be742 2021-07-09 op } else if (c == '\0') {
792 f3966209 2021-07-13 op yyerror("invalid syntax");
793 c39be742 2021-07-09 op return findeol();
794 c39be742 2021-07-09 op }
795 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
796 c39be742 2021-07-09 op yyerror("string too long");
797 c39be742 2021-07-09 op return findeol();
798 c39be742 2021-07-09 op }
799 c39be742 2021-07-09 op *p++ = c;
800 c39be742 2021-07-09 op }
801 c39be742 2021-07-09 op yylval.v.string = strdup(buf);
802 c39be742 2021-07-09 op if (yylval.v.string == NULL)
803 2dd5994a 2023-06-06 op fatal("yylex: strdup");
804 c39be742 2021-07-09 op return STRING;
805 74f0778b 2021-06-16 op }
806 c39be742 2021-07-09 op
807 c39be742 2021-07-09 op #define allowed_to_end_number(x) \
808 c39be742 2021-07-09 op (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
809 c39be742 2021-07-09 op
810 c39be742 2021-07-09 op if (c == '-' || isdigit(c)) {
811 c39be742 2021-07-09 op do {
812 c39be742 2021-07-09 op *p++ = c;
813 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
814 c39be742 2021-07-09 op yyerror("string too long");
815 c39be742 2021-07-09 op return findeol();
816 c39be742 2021-07-09 op }
817 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && isdigit(c));
818 c39be742 2021-07-09 op lungetc(c);
819 c39be742 2021-07-09 op if (p == buf + 1 && buf[0] == '-')
820 c39be742 2021-07-09 op goto nodigits;
821 c39be742 2021-07-09 op if (c == EOF || allowed_to_end_number(c)) {
822 c39be742 2021-07-09 op const char *errstr = NULL;
823 c39be742 2021-07-09 op
824 c39be742 2021-07-09 op *p = '\0';
825 c39be742 2021-07-09 op yylval.v.number = strtonum(buf, LLONG_MIN,
826 c39be742 2021-07-09 op LLONG_MAX, &errstr);
827 c39be742 2021-07-09 op if (errstr) {
828 c39be742 2021-07-09 op yyerror("\"%s\" invalid number: %s",
829 c39be742 2021-07-09 op buf, errstr);
830 c39be742 2021-07-09 op return findeol();
831 c39be742 2021-07-09 op }
832 c39be742 2021-07-09 op return NUM;
833 c39be742 2021-07-09 op } else {
834 c39be742 2021-07-09 op nodigits:
835 c39be742 2021-07-09 op while (p > buf + 1)
836 c39be742 2021-07-09 op lungetc(*--p);
837 c39be742 2021-07-09 op c = *--p;
838 c39be742 2021-07-09 op if (c == '-')
839 c39be742 2021-07-09 op return c;
840 c39be742 2021-07-09 op }
841 74f0778b 2021-06-16 op }
842 c39be742 2021-07-09 op
843 c39be742 2021-07-09 op #define allowed_in_string(x) \
844 c39be742 2021-07-09 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
845 c39be742 2021-07-09 op x != '{' && x != '}' && \
846 c39be742 2021-07-09 op x != '!' && x != '=' && x != '#' && \
847 67f49405 2021-07-09 op x != ',' && x != ';'))
848 c39be742 2021-07-09 op
849 67f49405 2021-07-09 op if (isalnum(c) || c == ':' || c == '_') {
850 c39be742 2021-07-09 op do {
851 c39be742 2021-07-09 op *p++ = c;
852 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
853 c39be742 2021-07-09 op yyerror("string too long");
854 c39be742 2021-07-09 op return findeol();
855 c39be742 2021-07-09 op }
856 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
857 c39be742 2021-07-09 op lungetc(c);
858 c39be742 2021-07-09 op *p = '\0';
859 c39be742 2021-07-09 op if ((token = lookup(buf)) == STRING)
860 c39be742 2021-07-09 op yylval.v.string = xstrdup(buf);
861 c39be742 2021-07-09 op return token;
862 74f0778b 2021-06-16 op }
863 c39be742 2021-07-09 op if (c == '\n') {
864 c39be742 2021-07-09 op yylval.lineno = file->lineno;
865 c39be742 2021-07-09 op file->lineno++;
866 74f0778b 2021-06-16 op }
867 c39be742 2021-07-09 op if (c == EOF)
868 c39be742 2021-07-09 op return 0;
869 c39be742 2021-07-09 op return c;
870 c39be742 2021-07-09 op }
871 c39be742 2021-07-09 op
872 c39be742 2021-07-09 op struct file *
873 c39be742 2021-07-09 op pushfile(const char *name, int secret)
874 c39be742 2021-07-09 op {
875 c39be742 2021-07-09 op struct file *nfile;
876 c39be742 2021-07-09 op
877 c39be742 2021-07-09 op nfile = xcalloc(1, sizeof(*nfile));
878 c39be742 2021-07-09 op nfile->name = xstrdup(name);
879 c39be742 2021-07-09 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
880 eae52ad4 2023-06-06 op log_warn("can't open %s", nfile->name);
881 c39be742 2021-07-09 op free(nfile->name);
882 c39be742 2021-07-09 op free(nfile);
883 c39be742 2021-07-09 op return NULL;
884 74f0778b 2021-06-16 op }
885 c39be742 2021-07-09 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
886 c39be742 2021-07-09 op nfile->ungetsize = 16;
887 c39be742 2021-07-09 op nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
888 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&files, nfile, entry);
889 c39be742 2021-07-09 op return nfile;
890 c39be742 2021-07-09 op }
891 74f0778b 2021-06-16 op
892 c39be742 2021-07-09 op int
893 c39be742 2021-07-09 op popfile(void)
894 c39be742 2021-07-09 op {
895 c39be742 2021-07-09 op struct file *prev;
896 13ed2fb6 2021-01-27 op
897 c39be742 2021-07-09 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
898 c39be742 2021-07-09 op prev->errors += file->errors;
899 13ed2fb6 2021-01-27 op
900 c39be742 2021-07-09 op TAILQ_REMOVE(&files, file, entry);
901 c39be742 2021-07-09 op fclose(file->stream);
902 c39be742 2021-07-09 op free(file->name);
903 c39be742 2021-07-09 op free(file->ungetbuf);
904 c39be742 2021-07-09 op free(file);
905 c39be742 2021-07-09 op file = prev;
906 c39be742 2021-07-09 op return file ? 0 : EOF;
907 13ed2fb6 2021-01-27 op }
908 13ed2fb6 2021-01-27 op
909 13ed2fb6 2021-01-27 op void
910 c39be742 2021-07-09 op parse_conf(const char *filename)
911 13ed2fb6 2021-01-27 op {
912 c39be742 2021-07-09 op struct sym *sym, *next;
913 3b21cca3 2021-06-29 op
914 c39be742 2021-07-09 op file = pushfile(filename, 0);
915 c39be742 2021-07-09 op if (file == NULL)
916 83272dfe 2021-08-23 op exit(1);
917 c39be742 2021-07-09 op topfile = file;
918 c39be742 2021-07-09 op
919 13ed2fb6 2021-01-27 op yyparse();
920 c39be742 2021-07-09 op errors = file->errors;
921 c39be742 2021-07-09 op popfile();
922 13ed2fb6 2021-01-27 op
923 c39be742 2021-07-09 op /* Free macros and check which have not been used. */
924 3b21cca3 2021-06-29 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
925 3b21cca3 2021-06-29 op /* TODO: warn if !sym->used */
926 3b21cca3 2021-06-29 op if (!sym->persist) {
927 3b21cca3 2021-06-29 op free(sym->name);
928 3b21cca3 2021-06-29 op free(sym->val);
929 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
930 3b21cca3 2021-06-29 op free(sym);
931 3b21cca3 2021-06-29 op }
932 3b21cca3 2021-06-29 op }
933 c39be742 2021-07-09 op
934 c39be742 2021-07-09 op if (errors)
935 c39be742 2021-07-09 op exit(1);
936 13ed2fb6 2021-01-27 op }
937 e17642a7 2021-02-01 op
938 f0a01fc7 2021-10-09 op void
939 f0a01fc7 2021-10-09 op print_conf(void)
940 f0a01fc7 2021-10-09 op {
941 f0a01fc7 2021-10-09 op struct vhost *h;
942 f0a01fc7 2021-10-09 op /* struct location *l; */
943 f0a01fc7 2021-10-09 op /* struct envlist *e; */
944 f0a01fc7 2021-10-09 op /* struct alist *a; */
945 f0a01fc7 2021-10-09 op
946 7277bb7d 2022-09-10 op if (*conf.chroot != '\0')
947 f0a01fc7 2021-10-09 op printf("chroot \"%s\"\n", conf.chroot);
948 f0a01fc7 2021-10-09 op printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
949 f0a01fc7 2021-10-09 op /* XXX: defined mimes? */
950 f0a01fc7 2021-10-09 op printf("port %d\n", conf.port);
951 f0a01fc7 2021-10-09 op printf("prefork %d\n", conf.prefork);
952 f0a01fc7 2021-10-09 op /* XXX: protocols? */
953 7277bb7d 2022-09-10 op if (*conf.user != '\0')
954 f0a01fc7 2021-10-09 op printf("user \"%s\"\n", conf.user);
955 f0a01fc7 2021-10-09 op
956 f0a01fc7 2021-10-09 op TAILQ_FOREACH(h, &hosts, vhosts) {
957 f0a01fc7 2021-10-09 op printf("\nserver \"%s\" {\n", h->domain);
958 f0a01fc7 2021-10-09 op printf(" cert \"%s\"\n", h->cert);
959 f0a01fc7 2021-10-09 op printf(" key \"%s\"\n", h->key);
960 f0a01fc7 2021-10-09 op /* TODO: print locations... */
961 f0a01fc7 2021-10-09 op printf("}\n");
962 f0a01fc7 2021-10-09 op }
963 f0a01fc7 2021-10-09 op }
964 f0a01fc7 2021-10-09 op
965 c39be742 2021-07-09 op int
966 c39be742 2021-07-09 op symset(const char *name, const char *val, int persist)
967 c39be742 2021-07-09 op {
968 c39be742 2021-07-09 op struct sym *sym;
969 c39be742 2021-07-09 op
970 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
971 c39be742 2021-07-09 op if (!strcmp(name, sym->name))
972 c39be742 2021-07-09 op break;
973 c39be742 2021-07-09 op }
974 c39be742 2021-07-09 op
975 c39be742 2021-07-09 op if (sym != NULL) {
976 c39be742 2021-07-09 op if (sym->persist)
977 c39be742 2021-07-09 op return 0;
978 c39be742 2021-07-09 op else {
979 c39be742 2021-07-09 op free(sym->name);
980 c39be742 2021-07-09 op free(sym->val);
981 c39be742 2021-07-09 op TAILQ_REMOVE(&symhead, sym, entry);
982 c39be742 2021-07-09 op free(sym);
983 c39be742 2021-07-09 op }
984 c39be742 2021-07-09 op }
985 c39be742 2021-07-09 op
986 c39be742 2021-07-09 op sym = xcalloc(1, sizeof(*sym));
987 c39be742 2021-07-09 op sym->name = xstrdup(name);
988 c39be742 2021-07-09 op sym->val = xstrdup(val);
989 c39be742 2021-07-09 op sym->used = 0;
990 c39be742 2021-07-09 op sym->persist = persist;
991 c39be742 2021-07-09 op
992 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
993 c39be742 2021-07-09 op return 0;
994 c39be742 2021-07-09 op }
995 c39be742 2021-07-09 op
996 c39be742 2021-07-09 op int
997 c39be742 2021-07-09 op cmdline_symset(char *s)
998 c39be742 2021-07-09 op {
999 c39be742 2021-07-09 op char *sym, *val;
1000 c39be742 2021-07-09 op int ret;
1001 c39be742 2021-07-09 op
1002 c39be742 2021-07-09 op if ((val = strrchr(s, '=')) == NULL)
1003 c39be742 2021-07-09 op return -1;
1004 c39be742 2021-07-09 op sym = xcalloc(1, val - s + 1);
1005 c39be742 2021-07-09 op memcpy(sym, s, val - s);
1006 c39be742 2021-07-09 op ret = symset(sym, val + 1, 1);
1007 c39be742 2021-07-09 op free(sym);
1008 c39be742 2021-07-09 op return ret;
1009 c39be742 2021-07-09 op }
1010 c39be742 2021-07-09 op
1011 e17642a7 2021-02-01 op char *
1012 c39be742 2021-07-09 op symget(const char *nam)
1013 c39be742 2021-07-09 op {
1014 c39be742 2021-07-09 op struct sym *sym;
1015 c39be742 2021-07-09 op
1016 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
1017 c39be742 2021-07-09 op if (strcmp(nam, sym->name) == 0) {
1018 c39be742 2021-07-09 op sym->used = 1;
1019 c39be742 2021-07-09 op return sym->val;
1020 c39be742 2021-07-09 op }
1021 c39be742 2021-07-09 op }
1022 c39be742 2021-07-09 op return NULL;
1023 c39be742 2021-07-09 op }
1024 c39be742 2021-07-09 op
1025 c39be742 2021-07-09 op char *
1026 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
1027 e17642a7 2021-02-01 op {
1028 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
1029 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
1030 e17642a7 2021-02-01 op return path;
1031 e17642a7 2021-02-01 op }
1032 6abda252 2021-02-06 op
1033 6abda252 2021-02-06 op int
1034 6abda252 2021-02-06 op check_block_code(int n)
1035 6abda252 2021-02-06 op {
1036 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1037 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
1038 6abda252 2021-02-06 op return n;
1039 6abda252 2021-02-06 op }
1040 6abda252 2021-02-06 op
1041 6abda252 2021-02-06 op char *
1042 6abda252 2021-02-06 op check_block_fmt(char *fmt)
1043 6abda252 2021-02-06 op {
1044 6abda252 2021-02-06 op char *s;
1045 6abda252 2021-02-06 op
1046 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
1047 6abda252 2021-02-06 op if (*s != '%')
1048 6abda252 2021-02-06 op continue;
1049 6abda252 2021-02-06 op switch (*++s) {
1050 6abda252 2021-02-06 op case '%':
1051 6abda252 2021-02-06 op case 'p':
1052 6abda252 2021-02-06 op case 'q':
1053 6abda252 2021-02-06 op case 'P':
1054 6abda252 2021-02-06 op case 'N':
1055 6abda252 2021-02-06 op break;
1056 6abda252 2021-02-06 op default:
1057 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
1058 6abda252 2021-02-06 op }
1059 6abda252 2021-02-06 op }
1060 6abda252 2021-02-06 op
1061 6abda252 2021-02-06 op return fmt;
1062 6abda252 2021-02-06 op }
1063 6abda252 2021-02-06 op
1064 6abda252 2021-02-06 op int
1065 6abda252 2021-02-06 op check_strip_no(int n)
1066 6abda252 2021-02-06 op {
1067 6abda252 2021-02-06 op if (n <= 0)
1068 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
1069 a709ddf5 2021-02-07 op return n;
1070 a709ddf5 2021-02-07 op }
1071 a709ddf5 2021-02-07 op
1072 a709ddf5 2021-02-07 op int
1073 391825e3 2021-07-09 op check_port_num(int n)
1074 391825e3 2021-07-09 op {
1075 391825e3 2021-07-09 op if (n <= 0 || n >= UINT16_MAX)
1076 391825e3 2021-07-09 op yyerror("port number is %s: %d",
1077 391825e3 2021-07-09 op n <= 0 ? "too small" : "too large",
1078 391825e3 2021-07-09 op n);
1079 391825e3 2021-07-09 op return n;
1080 391825e3 2021-07-09 op }
1081 391825e3 2021-07-09 op
1082 391825e3 2021-07-09 op int
1083 a709ddf5 2021-02-07 op check_prefork_num(int n)
1084 a709ddf5 2021-02-07 op {
1085 c26f2460 2023-06-08 op if (n <= 0 || n >= PROC_MAX_INSTANCES)
1086 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
1087 6abda252 2021-02-06 op return n;
1088 6abda252 2021-02-06 op }
1089 49b73ba1 2021-02-10 op
1090 49b73ba1 2021-02-10 op void
1091 49b73ba1 2021-02-10 op advance_loc(void)
1092 49b73ba1 2021-02-10 op {
1093 b8e64ccd 2021-03-31 op loc = new_location();
1094 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1095 49b73ba1 2021-02-10 op }
1096 c705ecb1 2021-05-03 op
1097 c705ecb1 2021-05-03 op void
1098 b7967bc1 2021-01-02 op advance_proxy(void)
1099 b7967bc1 2021-01-02 op {
1100 b7967bc1 2021-01-02 op proxy = new_proxy();
1101 b7967bc1 2021-01-02 op TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1102 b7967bc1 2021-01-02 op }
1103 b7967bc1 2021-01-02 op
1104 b7967bc1 2021-01-02 op void
1105 b7967bc1 2021-01-02 op parsehp(char *str, char **host, const char **port, const char *def)
1106 b7967bc1 2021-01-02 op {
1107 b7967bc1 2021-01-02 op char *at;
1108 b7967bc1 2021-01-02 op const char *errstr;
1109 b7967bc1 2021-01-02 op
1110 b7967bc1 2021-01-02 op *host = str;
1111 b7967bc1 2021-01-02 op
1112 b7967bc1 2021-01-02 op if ((at = strchr(str, ':')) != NULL) {
1113 b7967bc1 2021-01-02 op *at++ = '\0';
1114 b7967bc1 2021-01-02 op *port = at;
1115 b7967bc1 2021-01-02 op } else
1116 b7967bc1 2021-01-02 op *port = def;
1117 b7967bc1 2021-01-02 op
1118 b7967bc1 2021-01-02 op strtonum(*port, 1, UINT16_MAX, &errstr);
1119 b7967bc1 2021-01-02 op if (errstr != NULL)
1120 b7967bc1 2021-01-02 op yyerror("port is %s: %s", errstr, *port);
1121 8ad1c570 2021-05-09 op }
1122 8ad1c570 2021-05-09 op
1123 8ad1c570 2021-05-09 op int
1124 7b00c890 2022-10-05 op fastcgi_conf(const char *path, const char *port)
1125 8ad1c570 2021-05-09 op {
1126 8ad1c570 2021-05-09 op struct fcgi *f;
1127 5d22294a 2023-06-09 op int i = 0;
1128 fafc6849 2021-06-29 op
1129 5d22294a 2023-06-09 op TAILQ_FOREACH(f, &conf.fcgi, fcgi) {
1130 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
1131 534afd0d 2022-10-05 op ((port == NULL && *f->port == '\0') ||
1132 534afd0d 2022-10-05 op !strcmp(f->port, port)))
1133 8ad1c570 2021-05-09 op return i;
1134 5d22294a 2023-06-09 op ++i;
1135 8ad1c570 2021-05-09 op }
1136 8ad1c570 2021-05-09 op
1137 5d22294a 2023-06-09 op f = xcalloc(1, sizeof(*f));
1138 5d22294a 2023-06-09 op f->id = i;
1139 5d22294a 2023-06-09 op (void)strlcpy(f->path, path, sizeof(f->path));
1140 5d22294a 2023-06-09 op if (port != NULL)
1141 5d22294a 2023-06-09 op (void)strlcpy(f->port, port, sizeof(f->port));
1142 5d22294a 2023-06-09 op TAILQ_INSERT_TAIL(&conf.fcgi, f, fcgi);
1143 5d22294a 2023-06-09 op
1144 5d22294a 2023-06-09 op return f->id;
1145 c92b802b 2021-06-11 op }
1146 c92b802b 2021-06-11 op
1147 c92b802b 2021-06-11 op void
1148 2025e96d 2022-09-10 op add_param(char *name, char *val)
1149 c92b802b 2021-06-11 op {
1150 c92b802b 2021-06-11 op struct envlist *e;
1151 2025e96d 2022-09-10 op struct envhead *h = &host->params;
1152 c92b802b 2021-06-11 op
1153 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
1154 534afd0d 2022-10-05 op (void) strlcpy(e->name, name, sizeof(e->name));
1155 534afd0d 2022-10-05 op (void) strlcpy(e->value, val, sizeof(e->value));
1156 edc5ca66 2022-09-10 op TAILQ_INSERT_TAIL(h, e, envs);
1157 3b21cca3 2021-06-29 op }
1158 534afd0d 2022-10-05 op
1159 534afd0d 2022-10-05 op int
1160 534afd0d 2022-10-05 op getservice(const char *n)
1161 534afd0d 2022-10-05 op {
1162 534afd0d 2022-10-05 op struct servent *s;
1163 534afd0d 2022-10-05 op const char *errstr;
1164 534afd0d 2022-10-05 op long long llval;
1165 534afd0d 2022-10-05 op
1166 534afd0d 2022-10-05 op llval = strtonum(n, 0, UINT16_MAX, &errstr);
1167 534afd0d 2022-10-05 op if (errstr) {
1168 534afd0d 2022-10-05 op s = getservbyname(n, "tcp");
1169 534afd0d 2022-10-05 op if (s == NULL)
1170 534afd0d 2022-10-05 op s = getservbyname(n, "udp");
1171 534afd0d 2022-10-05 op if (s == NULL)
1172 534afd0d 2022-10-05 op return (-1);
1173 534afd0d 2022-10-05 op return (ntohs(s->s_port));
1174 534afd0d 2022-10-05 op }
1175 534afd0d 2022-10-05 op
1176 534afd0d 2022-10-05 op return ((unsigned short)llval);
1177 534afd0d 2022-10-05 op }