Blame


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