Blame


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