Blame


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