Blame


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