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.
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.
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.
26 1f1f3810 2022-02-03 op #include "gmid.h"
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>
36 df5058c9 2023-06-05 op #include "log.h"
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;
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;
49 c39be742 2021-07-09 op } *file, *topfile;
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);
69 15902770 2021-01-15 op * #define YYDEBUG 1
70 15902770 2021-01-15 op * int yydebug = 1;
73 3b21cca3 2021-06-29 op TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 3b21cca3 2021-06-29 op TAILQ_ENTRY(sym) entry;
82 c39be742 2021-07-09 op int symset(const char *, const char *, int);
83 c39be742 2021-07-09 op char *symget(const char *);
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 *);
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;
107 c39be742 2021-07-09 op typedef struct {
109 c39be742 2021-07-09 op char *string;
117 15902770 2021-01-15 op /* for bison: */
118 15902770 2021-01-15 op /* %define parse.error verbose */
120 c74c7030 2021-07-19 op %token ALIAS AUTO
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
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
138 c39be742 2021-07-09 op %token <v.string> STRING
139 c39be742 2021-07-09 op %token <v.number> NUM
141 534afd0d 2022-10-05 op %type <v.number> bool proxy_port
142 ee219d70 2022-02-26 op %type <v.string> string numberstring
146 6b86655a 2021-06-29 op conf : /* empty */
147 c39be742 2021-07-09 op | conf include '\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++; }
156 c39be742 2021-07-09 op include : INCLUDE STRING {
157 c39be742 2021-07-09 op struct file *nfile;
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);
166 c39be742 2021-07-09 op file = nfile;
167 c39be742 2021-07-09 op lungetc('\n');
171 c74c7030 2021-07-19 op bool : ON { $$ = 1; }
172 c74c7030 2021-07-19 op | OFF { $$ = 0; }
175 c39be742 2021-07-09 op string : string STRING {
176 98f52178 2021-06-29 op if (asprintf(&$$, "%s%s", $1, $2) == -1) {
179 98f52178 2021-06-29 op yyerror("string: asprintf: %s", strerror(errno));
188 ee219d70 2022-02-26 op numberstring : NUM {
190 ee219d70 2022-02-26 op if (asprintf(&s, "%d", $1) == -1) {
191 ee219d70 2022-02-26 op yyerror("asprintf: number");
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");
210 3b21cca3 2021-06-29 op symset($1, $3, 0);
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");
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);
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");
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);
242 b8e64ccd 2021-03-31 op loc = new_location();
243 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
245 b7967bc1 2021-01-02 op TAILQ_INIT(&host->proxies);
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));
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);
256 b7967bc1 2021-01-02 op } '{' optnl servbody '}' {
257 1c6967b3 2023-06-08 op if (host->cert_path == NULL ||
258 1c6967b3 2023-06-08 op host->key_path == NULL)
259 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
261 f3966209 2021-07-13 op | error '}' { yyerror("bad server directive"); }
264 b7967bc1 2021-01-02 op servbody : /* empty */
265 b7967bc1 2021-01-02 op | servbody servopt optnl
266 b7967bc1 2021-01-02 op | servbody location optnl
267 b7967bc1 2021-01-02 op | servbody proxy optnl
270 c74c7030 2021-07-19 op servopt : ALIAS string {
271 cc8c2901 2021-04-29 op struct alist *a;
273 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
274 534afd0d 2022-10-05 op (void) strlcpy(a->alias, $2, sizeof(a->alias));
276 edc5ca66 2022-09-10 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
278 c74c7030 2021-07-19 op | CERT string {
279 534afd0d 2022-10-05 op ensure_absolute_path($2);
280 1c6967b3 2023-06-08 op free(host->cert_path);
281 1c6967b3 2023-06-08 op host->cert_path = $2;
283 c74c7030 2021-07-19 op | KEY string {
284 534afd0d 2022-10-05 op ensure_absolute_path($2);
285 1c6967b3 2023-06-08 op free(host->key_path);
286 1c6967b3 2023-06-08 op host->key_path = $2;
288 ff05125e 2021-10-15 op | OCSP string {
289 534afd0d 2022-10-05 op ensure_absolute_path($2);
290 1c6967b3 2023-06-08 op free(host->ocsp_path);
291 1c6967b3 2023-06-08 op host->ocsp_path = $2;
293 c74c7030 2021-07-19 op | PARAM string '=' string {
294 2025e96d 2022-09-10 op add_param($2, $4);
299 b7967bc1 2021-01-02 op proxy : PROXY { advance_proxy(); }
300 b7967bc1 2021-01-02 op proxy_matches '{' optnl proxy_opts '}' {
301 534afd0d 2022-10-05 op if (*proxy->host == '\0')
302 b7967bc1 2021-01-02 op yyerror("invalid proxy block: missing `relay-to' option");
304 b7967bc1 2021-01-02 op if ((proxy->cert == NULL && proxy->key != NULL) ||
305 b7967bc1 2021-01-02 op (proxy->cert != NULL && proxy->key == NULL))
306 b7967bc1 2021-01-02 op yyerror("invalid proxy block: missing cert or key");
310 b7967bc1 2021-01-02 op proxy_matches : /* empty */
311 b7967bc1 2021-01-02 op | proxy_matches proxy_match
314 534afd0d 2022-10-05 op proxy_port : /* empty */ { $$ = 1965; }
315 534afd0d 2022-10-05 op | PORT STRING {
316 534afd0d 2022-10-05 op if (($$ = getservice($2)) == -1)
317 534afd0d 2022-10-05 op yyerror("invalid port number %s", $2);
320 534afd0d 2022-10-05 op | PORT NUM { $$ = $2; }
323 b7967bc1 2021-01-02 op proxy_match : PROTO string {
324 534afd0d 2022-10-05 op (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
327 534afd0d 2022-10-05 op | FOR_HOST string proxy_port {
328 534afd0d 2022-10-05 op (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
329 534afd0d 2022-10-05 op (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
335 7bdcc91e 2021-01-01 op proxy_opts : /* empty */
336 7bdcc91e 2021-01-01 op | proxy_opts proxy_opt optnl
339 7bdcc91e 2021-01-01 op proxy_opt : CERT string {
340 b7967bc1 2021-01-02 op tls_unload_file(proxy->cert, proxy->certlen);
341 7bdcc91e 2021-01-01 op ensure_absolute_path($2);
342 b7967bc1 2021-01-02 op proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
343 b7967bc1 2021-01-02 op if (proxy->cert == NULL)
344 7bdcc91e 2021-01-01 op yyerror("can't load cert %s", $2);
347 7bdcc91e 2021-01-01 op | KEY string {
348 b7967bc1 2021-01-02 op tls_unload_file(proxy->key, proxy->keylen);
349 7bdcc91e 2021-01-01 op ensure_absolute_path($2);
350 b7967bc1 2021-01-02 op proxy->key = tls_load_file($2, &proxy->keylen, NULL);
351 b7967bc1 2021-01-02 op if (proxy->key == NULL)
352 7bdcc91e 2021-01-01 op yyerror("can't load key %s", $2);
355 c7c8ef44 2021-01-01 op | PROTOCOLS string {
356 b7967bc1 2021-01-02 op if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
357 c7c8ef44 2021-01-01 op yyerror("invalid protocols string \"%s\"", $2);
360 534afd0d 2022-10-05 op | RELAY_TO string proxy_port {
361 534afd0d 2022-10-05 op (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
362 534afd0d 2022-10-05 op (void) snprintf(proxy->port, sizeof(proxy->port),
366 ba94a608 2022-01-04 op | REQUIRE CLIENT CA string {
367 ba94a608 2022-01-04 op ensure_absolute_path($4);
368 ba94a608 2022-01-04 op if ((proxy->reqca = load_ca($4)) == NULL)
369 ba94a608 2022-01-04 op yyerror("couldn't load ca cert: %s", $4);
372 1cdea97b 2022-01-30 op | SNI string {
373 534afd0d 2022-10-05 op (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
376 593e412b 2021-01-01 op | USE_TLS bool {
377 b7967bc1 2021-01-02 op proxy->notls = !$2;
379 5128c0b0 2021-01-01 op | VERIFYNAME bool {
380 b7967bc1 2021-01-02 op proxy->noverifyname = !$2;
384 c74c7030 2021-07-19 op location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
385 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
386 49b73ba1 2021-02-10 op if (*$3 == '/')
387 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
388 534afd0d 2022-10-05 op (void) strlcpy(loc->match, $3, sizeof(loc->match));
394 c8b74339 2021-01-24 op locopts : /* empty */
395 c39be742 2021-07-09 op | locopts locopt optnl
398 c74c7030 2021-07-19 op locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
399 c74c7030 2021-07-19 op | BLOCK RETURN NUM string {
400 534afd0d 2022-10-05 op check_block_fmt($4);
401 534afd0d 2022-10-05 op (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
402 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
405 c74c7030 2021-07-19 op | BLOCK RETURN NUM {
406 534afd0d 2022-10-05 op (void) strlcpy(loc->block_fmt, "temporary failure",
407 534afd0d 2022-10-05 op sizeof(loc->block_fmt));
408 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
409 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
410 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
413 534afd0d 2022-10-05 op (void) strlcpy(loc->block_fmt, "temporary failure",
414 534afd0d 2022-10-05 op sizeof(loc->block_fmt));
415 6abda252 2021-02-06 op loc->block_code = 40;
417 c74c7030 2021-07-19 op | DEFAULT TYPE string {
418 534afd0d 2022-10-05 op (void) strlcpy(loc->default_mime, $3,
419 534afd0d 2022-10-05 op sizeof(loc->default_mime));
422 abc8801d 2021-07-19 op | FASTCGI fastcgi
423 c74c7030 2021-07-19 op | INDEX string {
424 534afd0d 2022-10-05 op (void) strlcpy(loc->index, $2, sizeof(loc->index));
427 c74c7030 2021-07-19 op | LANG string {
428 534afd0d 2022-10-05 op (void) strlcpy(loc->lang, $2,
429 534afd0d 2022-10-05 op sizeof(loc->lang));
432 c74c7030 2021-07-19 op | LOG bool { loc->disable_log = !$2; }
433 da2185f3 2021-01-01 op | REQUIRE CLIENT CA string {
434 da2185f3 2021-01-01 op ensure_absolute_path($4);
435 da2185f3 2021-01-01 op if ((loc->reqca = load_ca($4)) == NULL)
436 da2185f3 2021-01-01 op yyerror("couldn't load ca cert: %s", $4);
439 da2185f3 2021-01-01 op | ROOT string {
440 534afd0d 2022-10-05 op (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
443 da2185f3 2021-01-01 op | STRIP NUM { loc->strip = check_strip_no($2); }
446 7b00c890 2022-10-05 op fastcgi : string {
447 7b00c890 2022-10-05 op loc->fcgi = fastcgi_conf($1, NULL);
450 c74c7030 2021-07-19 op | TCP string PORT NUM {
452 762b9b99 2021-07-09 op if (asprintf(&c, "%d", $4) == -1)
453 0d047efc 2021-05-24 op err(1, "asprintf");
454 7b00c890 2022-10-05 op loc->fcgi = fastcgi_conf($2, c);
457 c74c7030 2021-07-19 op | TCP string {
458 7b00c890 2022-10-05 op loc->fcgi = fastcgi_conf($2, "9000");
461 c74c7030 2021-07-19 op | TCP string PORT string {
462 7b00c890 2022-10-05 op loc->fcgi = fastcgi_conf($2, $4);
468 cd5826b8 2022-09-10 op types : TYPES '{' optnl mediaopts_l '}' ;
470 ee219d70 2022-02-26 op mediaopts_l : mediaopts_l mediaoptsl nl
471 ee219d70 2022-02-26 op | mediaoptsl nl
474 aa9543b9 2022-09-10 op mediaoptsl : STRING {
475 aa9543b9 2022-09-10 op free(current_media);
476 aa9543b9 2022-09-10 op current_media = $1;
477 aa9543b9 2022-09-10 op } medianames_l optsemicolon
481 ee219d70 2022-02-26 op medianames_l : medianames_l medianamesl
482 ee219d70 2022-02-26 op | medianamesl
485 d8d170aa 2022-04-08 op medianamesl : numberstring {
486 d8d170aa 2022-04-08 op if (add_mime(&conf.mime, current_media, $1) == -1)
487 d8d170aa 2022-04-08 op err(1, "add_mime");
492 ee219d70 2022-02-26 op nl : '\n' optnl
495 c39be742 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
496 67f49405 2021-07-09 op | ';' optnl /* semicolons too */
500 ee219d70 2022-02-26 op optsemicolon : ';'
506 e5d82d94 2022-03-19 op static const struct keyword {
507 74f0778b 2021-06-16 op const char *word;
509 74f0778b 2021-06-16 op } keywords[] = {
510 d93c8191 2021-07-09 op /* these MUST be sorted */
511 c74c7030 2021-07-19 op {"alias", ALIAS},
512 c74c7030 2021-07-19 op {"auto", AUTO},
513 c74c7030 2021-07-19 op {"block", BLOCK},
515 c74c7030 2021-07-19 op {"cert", CERT},
516 c74c7030 2021-07-19 op {"chroot", CHROOT},
517 c74c7030 2021-07-19 op {"client", CLIENT},
518 c74c7030 2021-07-19 op {"default", DEFAULT},
519 abc8801d 2021-07-19 op {"fastcgi", FASTCGI},
520 b7967bc1 2021-01-02 op {"for-host", FOR_HOST},
521 88971f9a 2022-02-26 op {"include", INCLUDE},
522 c74c7030 2021-07-19 op {"index", INDEX},
523 c74c7030 2021-07-19 op {"ipv6", IPV6},
524 c74c7030 2021-07-19 op {"key", KEY},
525 c74c7030 2021-07-19 op {"lang", LANG},
526 c74c7030 2021-07-19 op {"location", LOCATION},
527 c74c7030 2021-07-19 op {"log", LOG},
528 ff05125e 2021-10-15 op {"ocsp", OCSP},
529 c74c7030 2021-07-19 op {"off", OFF},
531 c74c7030 2021-07-19 op {"param", PARAM},
532 c74c7030 2021-07-19 op {"port", PORT},
533 c74c7030 2021-07-19 op {"prefork", PREFORK},
534 b7967bc1 2021-01-02 op {"proto", PROTO},
535 c74c7030 2021-07-19 op {"protocols", PROTOCOLS},
536 72b033ef 2021-12-29 op {"proxy", PROXY},
537 72b033ef 2021-12-29 op {"relay-to", RELAY_TO},
538 c74c7030 2021-07-19 op {"require", REQUIRE},
539 c74c7030 2021-07-19 op {"return", RETURN},
540 c74c7030 2021-07-19 op {"root", ROOT},
541 c74c7030 2021-07-19 op {"server", SERVER},
542 1cdea97b 2022-01-30 op {"sni", SNI},
543 c74c7030 2021-07-19 op {"strip", STRIP},
544 c74c7030 2021-07-19 op {"tcp", TCP},
545 c74c7030 2021-07-19 op {"to-ext", TOEXT},
546 c74c7030 2021-07-19 op {"type", TYPE},
547 ee219d70 2022-02-26 op {"types", TYPES},
548 593e412b 2021-01-01 op {"use-tls", USE_TLS},
549 c74c7030 2021-07-19 op {"user", USER},
550 5128c0b0 2021-01-01 op {"verifyname", VERIFYNAME},
554 c39be742 2021-07-09 op yyerror(const char *msg, ...)
558 c39be742 2021-07-09 op file->errors++;
560 c39be742 2021-07-09 op va_start(ap, msg);
561 f3966209 2021-07-13 op fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
562 f3966209 2021-07-13 op vfprintf(stderr, msg, ap);
563 f3966209 2021-07-13 op fprintf(stderr, "\n");
568 f3966209 2021-07-13 op yywarn(const char *msg, ...)
572 f3966209 2021-07-13 op va_start(ap, msg);
573 f3966209 2021-07-13 op fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
574 c39be742 2021-07-09 op vfprintf(stderr, msg, ap);
575 c39be742 2021-07-09 op fprintf(stderr, "\n");
580 d93c8191 2021-07-09 op kw_cmp(const void *k, const void *e)
582 d93c8191 2021-07-09 op return strcmp(k, ((struct keyword *)e)->word);
586 c39be742 2021-07-09 op lookup(char *s)
588 c39be742 2021-07-09 op const struct keyword *p;
590 c39be742 2021-07-09 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
591 c39be742 2021-07-09 op sizeof(keywords[0]), kw_cmp);
594 c39be742 2021-07-09 op return p->token;
596 c39be742 2021-07-09 op return STRING;
599 c39be742 2021-07-09 op #define START_EXPAND 1
600 c39be742 2021-07-09 op #define DONE_EXPAND 2
602 c39be742 2021-07-09 op static int expanding;
610 c39be742 2021-07-09 op if (file->ungetpos > 0)
611 c39be742 2021-07-09 op c = file->ungetbuf[--file->ungetpos];
613 c39be742 2021-07-09 op c = getc(file->stream);
615 c39be742 2021-07-09 op if (c == START_EXPAND)
616 c39be742 2021-07-09 op expanding = 1;
617 c39be742 2021-07-09 op else if (c == DONE_EXPAND)
618 c39be742 2021-07-09 op expanding = 0;
626 c39be742 2021-07-09 op lgetc(int quotec)
630 c39be742 2021-07-09 op if (quotec) {
631 c39be742 2021-07-09 op if ((c = igetc()) == EOF) {
632 c39be742 2021-07-09 op yyerror("reached end of file while parsing "
633 c39be742 2021-07-09 op "quoted string");
634 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
636 c39be742 2021-07-09 op return quotec;
641 c39be742 2021-07-09 op while ((c = igetc()) == '\\') {
642 c39be742 2021-07-09 op next = igetc();
643 c39be742 2021-07-09 op if (next != '\n') {
647 c39be742 2021-07-09 op yylval.lineno = file->lineno;
648 c39be742 2021-07-09 op file->lineno++;
651 c39be742 2021-07-09 op if (c == EOF) {
653 c39be742 2021-07-09 op * Fake EOL when hit EOF for the first time. This gets line
654 c39be742 2021-07-09 op * count right if last line in included file is syntactically
655 c39be742 2021-07-09 op * invalid and has no newline.
657 c39be742 2021-07-09 op if (file->eof_reached == 0) {
658 c39be742 2021-07-09 op file->eof_reached = 1;
661 c39be742 2021-07-09 op while (c == EOF) {
662 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
671 c39be742 2021-07-09 op lungetc(int c)
673 c39be742 2021-07-09 op if (c == EOF)
676 c39be742 2021-07-09 op if (file->ungetpos >= file->ungetsize) {
677 c39be742 2021-07-09 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
678 c39be742 2021-07-09 op if (p == NULL)
679 2dd5994a 2023-06-06 op fatal("lungetc");
680 c39be742 2021-07-09 op file->ungetbuf = p;
681 c39be742 2021-07-09 op file->ungetsize *= 2;
683 c39be742 2021-07-09 op file->ungetbuf[file->ungetpos++] = c;
687 c39be742 2021-07-09 op findeol(void)
691 c39be742 2021-07-09 op /* Skip to either EOF or the first real EOL. */
693 c39be742 2021-07-09 op c = lgetc(0);
694 c39be742 2021-07-09 op if (c == '\n') {
695 c39be742 2021-07-09 op file->lineno++;
698 c39be742 2021-07-09 op if (c == EOF)
701 c39be742 2021-07-09 op return ERROR;
707 1bd706dc 2021-07-09 op char buf[8096];
708 1bd706dc 2021-07-09 op char *p, *val;
709 1bd706dc 2021-07-09 op int quotec, next, c;
714 c39be742 2021-07-09 op while ((c = lgetc(0)) == ' ' || c == '\t')
715 c39be742 2021-07-09 op ; /* nothing */
717 c39be742 2021-07-09 op yylval.lineno = file->lineno;
718 c39be742 2021-07-09 op if (c == '#')
719 c39be742 2021-07-09 op while ((c = lgetc(0)) != '\n' && c != EOF)
720 c39be742 2021-07-09 op ; /* nothing */
721 c39be742 2021-07-09 op if (c == '$' && !expanding) {
723 c39be742 2021-07-09 op if ((c = lgetc(0)) == EOF)
725 67f49405 2021-07-09 op if (p + 1 >= buf + sizeof(buf) -1) {
726 67f49405 2021-07-09 op yyerror("string too long");
727 67f49405 2021-07-09 op return findeol();
729 67f49405 2021-07-09 op if (isalnum(c) || c == '_') {
737 67f49405 2021-07-09 op val = symget(buf);
738 67f49405 2021-07-09 op if (val == NULL) {
739 67f49405 2021-07-09 op yyerror("macro `%s' not defined", buf);
740 67f49405 2021-07-09 op return findeol();
742 67f49405 2021-07-09 op yylval.v.string = xstrdup(val);
743 67f49405 2021-07-09 op return STRING;
745 67f49405 2021-07-09 op if (c == '@' && !expanding) {
747 67f49405 2021-07-09 op if ((c = lgetc(0)) == EOF)
750 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
751 c39be742 2021-07-09 op yyerror("string too long");
752 c39be742 2021-07-09 op return findeol();
754 c39be742 2021-07-09 op if (isalnum(c) || c == '_') {
762 c39be742 2021-07-09 op val = symget(buf);
763 c39be742 2021-07-09 op if (val == NULL) {
764 c39be742 2021-07-09 op yyerror("macro '%s' not defined", buf);
765 c39be742 2021-07-09 op return findeol();
767 c39be742 2021-07-09 op p = val + strlen(val) - 1;
768 c39be742 2021-07-09 op lungetc(DONE_EXPAND);
769 c39be742 2021-07-09 op while (p >= val) {
773 c39be742 2021-07-09 op lungetc(START_EXPAND);
782 c39be742 2021-07-09 op if ((c = lgetc(quotec)) == EOF)
784 c39be742 2021-07-09 op if (c == '\n') {
785 c39be742 2021-07-09 op file->lineno++;
787 c39be742 2021-07-09 op } else if (c == '\\') {
788 c39be742 2021-07-09 op if ((next = lgetc(quotec)) == EOF)
790 c39be742 2021-07-09 op if (next == quotec || next == ' ' ||
791 c39be742 2021-07-09 op next == '\t')
793 c39be742 2021-07-09 op else if (next == '\n') {
794 c39be742 2021-07-09 op file->lineno++;
797 c39be742 2021-07-09 op lungetc(next);
798 c39be742 2021-07-09 op } else if (c == quotec) {
801 c39be742 2021-07-09 op } else if (c == '\0') {
802 f3966209 2021-07-13 op yyerror("invalid syntax");
803 c39be742 2021-07-09 op return findeol();
805 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
806 c39be742 2021-07-09 op yyerror("string too long");
807 c39be742 2021-07-09 op return findeol();
811 c39be742 2021-07-09 op yylval.v.string = strdup(buf);
812 c39be742 2021-07-09 op if (yylval.v.string == NULL)
813 2dd5994a 2023-06-06 op fatal("yylex: strdup");
814 c39be742 2021-07-09 op return STRING;
817 c39be742 2021-07-09 op #define allowed_to_end_number(x) \
818 c39be742 2021-07-09 op (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
820 c39be742 2021-07-09 op if (c == '-' || isdigit(c)) {
823 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
824 c39be742 2021-07-09 op yyerror("string too long");
825 c39be742 2021-07-09 op return findeol();
827 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && isdigit(c));
829 c39be742 2021-07-09 op if (p == buf + 1 && buf[0] == '-')
830 c39be742 2021-07-09 op goto nodigits;
831 c39be742 2021-07-09 op if (c == EOF || allowed_to_end_number(c)) {
832 c39be742 2021-07-09 op const char *errstr = NULL;
835 c39be742 2021-07-09 op yylval.v.number = strtonum(buf, LLONG_MIN,
836 c39be742 2021-07-09 op LLONG_MAX, &errstr);
837 c39be742 2021-07-09 op if (errstr) {
838 c39be742 2021-07-09 op yyerror("\"%s\" invalid number: %s",
839 c39be742 2021-07-09 op buf, errstr);
840 c39be742 2021-07-09 op return findeol();
845 c39be742 2021-07-09 op while (p > buf + 1)
846 c39be742 2021-07-09 op lungetc(*--p);
848 c39be742 2021-07-09 op if (c == '-')
853 c39be742 2021-07-09 op #define allowed_in_string(x) \
854 c39be742 2021-07-09 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
855 c39be742 2021-07-09 op x != '{' && x != '}' && \
856 c39be742 2021-07-09 op x != '!' && x != '=' && x != '#' && \
857 67f49405 2021-07-09 op x != ',' && x != ';'))
859 67f49405 2021-07-09 op if (isalnum(c) || c == ':' || c == '_') {
862 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
863 c39be742 2021-07-09 op yyerror("string too long");
864 c39be742 2021-07-09 op return findeol();
866 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
869 c39be742 2021-07-09 op if ((token = lookup(buf)) == STRING)
870 c39be742 2021-07-09 op yylval.v.string = xstrdup(buf);
871 c39be742 2021-07-09 op return token;
873 c39be742 2021-07-09 op if (c == '\n') {
874 c39be742 2021-07-09 op yylval.lineno = file->lineno;
875 c39be742 2021-07-09 op file->lineno++;
877 c39be742 2021-07-09 op if (c == EOF)
882 c39be742 2021-07-09 op struct file *
883 c39be742 2021-07-09 op pushfile(const char *name, int secret)
885 c39be742 2021-07-09 op struct file *nfile;
887 c39be742 2021-07-09 op nfile = xcalloc(1, sizeof(*nfile));
888 c39be742 2021-07-09 op nfile->name = xstrdup(name);
889 c39be742 2021-07-09 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
890 eae52ad4 2023-06-06 op log_warn("can't open %s", nfile->name);
891 c39be742 2021-07-09 op free(nfile->name);
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;
903 c39be742 2021-07-09 op popfile(void)
905 c39be742 2021-07-09 op struct file *prev;
907 c39be742 2021-07-09 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
908 c39be742 2021-07-09 op prev->errors += file->errors;
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);
916 c39be742 2021-07-09 op return file ? 0 : EOF;
920 c39be742 2021-07-09 op parse_conf(const char *filename)
922 c39be742 2021-07-09 op struct sym *sym, *next;
924 c39be742 2021-07-09 op file = pushfile(filename, 0);
925 c39be742 2021-07-09 op if (file == NULL)
927 c39be742 2021-07-09 op topfile = file;
930 c39be742 2021-07-09 op errors = file->errors;
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);
949 f0a01fc7 2021-10-09 op print_conf(void)
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; */
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);
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");
976 c39be742 2021-07-09 op symset(const char *name, const char *val, int persist)
978 c39be742 2021-07-09 op struct sym *sym;
980 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
981 c39be742 2021-07-09 op if (!strcmp(name, sym->name))
985 c39be742 2021-07-09 op if (sym != NULL) {
986 c39be742 2021-07-09 op if (sym->persist)
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);
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;
1002 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
1007 c39be742 2021-07-09 op cmdline_symset(char *s)
1009 c39be742 2021-07-09 op char *sym, *val;
1012 c39be742 2021-07-09 op if ((val = strrchr(s, '=')) == NULL)
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);
1022 c39be742 2021-07-09 op symget(const char *nam)
1024 c39be742 2021-07-09 op struct sym *sym;
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;
1032 c39be742 2021-07-09 op return NULL;
1036 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
1038 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
1039 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
1040 e17642a7 2021-02-01 op return path;
1044 6abda252 2021-02-06 op check_block_code(int n)
1046 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1047 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
1052 6abda252 2021-02-06 op check_block_fmt(char *fmt)
1056 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
1057 6abda252 2021-02-06 op if (*s != '%')
1059 6abda252 2021-02-06 op switch (*++s) {
1067 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
1075 6abda252 2021-02-06 op check_strip_no(int n)
1078 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
1083 391825e3 2021-07-09 op check_port_num(int n)
1085 391825e3 2021-07-09 op if (n <= 0 || n >= UINT16_MAX)
1086 391825e3 2021-07-09 op yyerror("port number is %s: %d",
1087 391825e3 2021-07-09 op n <= 0 ? "too small" : "too large",
1093 a709ddf5 2021-02-07 op check_prefork_num(int n)
1095 c26f2460 2023-06-08 op if (n <= 0 || n >= PROC_MAX_INSTANCES)
1096 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
1101 49b73ba1 2021-02-10 op advance_loc(void)
1103 b8e64ccd 2021-03-31 op loc = new_location();
1104 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1108 b7967bc1 2021-01-02 op advance_proxy(void)
1110 b7967bc1 2021-01-02 op proxy = new_proxy();
1111 b7967bc1 2021-01-02 op TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1115 b7967bc1 2021-01-02 op parsehp(char *str, char **host, const char **port, const char *def)
1118 b7967bc1 2021-01-02 op const char *errstr;
1120 b7967bc1 2021-01-02 op *host = str;
1122 b7967bc1 2021-01-02 op if ((at = strchr(str, ':')) != NULL) {
1123 b7967bc1 2021-01-02 op *at++ = '\0';
1126 b7967bc1 2021-01-02 op *port = def;
1128 b7967bc1 2021-01-02 op strtonum(*port, 1, UINT16_MAX, &errstr);
1129 b7967bc1 2021-01-02 op if (errstr != NULL)
1130 b7967bc1 2021-01-02 op yyerror("port is %s: %s", errstr, *port);
1134 7b00c890 2022-10-05 op fastcgi_conf(const char *path, const char *port)
1136 8ad1c570 2021-05-09 op struct fcgi *f;
1139 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
1140 8ad1c570 2021-05-09 op f = &fcgi[i];
1142 534afd0d 2022-10-05 op if (*f->path == '\0') {
1144 534afd0d 2022-10-05 op (void) strlcpy(f->path, path, sizeof(f->path));
1145 e92efb0d 2022-11-27 op if (port != NULL)
1146 e92efb0d 2022-11-27 op (void) strlcpy(f->port, port, sizeof(f->port));
1150 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
1151 534afd0d 2022-10-05 op ((port == NULL && *f->port == '\0') ||
1152 534afd0d 2022-10-05 op !strcmp(f->port, port)))
1156 8ad1c570 2021-05-09 op yyerror("too much `fastcgi' rules defined.");
1161 2025e96d 2022-09-10 op add_param(char *name, char *val)
1163 c92b802b 2021-06-11 op struct envlist *e;
1164 2025e96d 2022-09-10 op struct envhead *h = &host->params;
1166 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
1167 534afd0d 2022-10-05 op (void) strlcpy(e->name, name, sizeof(e->name));
1168 534afd0d 2022-10-05 op (void) strlcpy(e->value, val, sizeof(e->value));
1169 edc5ca66 2022-09-10 op TAILQ_INSERT_TAIL(h, e, envs);
1173 534afd0d 2022-10-05 op getservice(const char *n)
1175 534afd0d 2022-10-05 op struct servent *s;
1176 534afd0d 2022-10-05 op const char *errstr;
1177 534afd0d 2022-10-05 op long long llval;
1179 534afd0d 2022-10-05 op llval = strtonum(n, 0, UINT16_MAX, &errstr);
1180 534afd0d 2022-10-05 op if (errstr) {
1181 534afd0d 2022-10-05 op s = getservbyname(n, "tcp");
1182 534afd0d 2022-10-05 op if (s == NULL)
1183 534afd0d 2022-10-05 op s = getservbyname(n, "udp");
1184 534afd0d 2022-10-05 op if (s == NULL)
1185 534afd0d 2022-10-05 op return (-1);
1186 534afd0d 2022-10-05 op return (ntohs(s->s_port));
1189 534afd0d 2022-10-05 op return ((unsigned short)llval);