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