4 f862d389 2024-01-30 op * Copyright (c) 2021-2024 Omar Polo <op@omarpolo.com>
5 c39be742 2021-07-09 op * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
6 c39be742 2021-07-09 op * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
7 c39be742 2021-07-09 op * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
8 c39be742 2021-07-09 op * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
9 c39be742 2021-07-09 op * Copyright (c) 2001 Markus Friedl. All rights reserved.
10 c39be742 2021-07-09 op * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
11 c39be742 2021-07-09 op * Copyright (c) 2001 Theo de Raadt. All rights reserved.
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>
35 9abba172 2023-08-07 op #include <syslog.h>
37 df5058c9 2023-06-05 op #include "log.h"
39 af1dab18 2023-06-09 op struct conf *conf;
41 c2c051f2 2023-08-25 op static const char *default_host = NULL;
42 509d0509 2023-06-23 op static uint16_t default_port = 1965;
44 c39be742 2021-07-09 op TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
45 c39be742 2021-07-09 op static struct file {
46 c39be742 2021-07-09 op TAILQ_ENTRY(file) entry;
49 574f71f7 2024-01-30 op size_t ungetpos;
50 c39be742 2021-07-09 op size_t ungetsize;
51 c39be742 2021-07-09 op u_char *ungetbuf;
52 c39be742 2021-07-09 op int eof_reached;
55 c39be742 2021-07-09 op } *file, *topfile;
57 c39be742 2021-07-09 op struct file *pushfile(const char *, int);
58 c39be742 2021-07-09 op int popfile(void);
59 c39be742 2021-07-09 op int yyparse(void);
60 c39be742 2021-07-09 op int yylex(void);
61 c39be742 2021-07-09 op void yyerror(const char *, ...)
62 c39be742 2021-07-09 op __attribute__((__format__ (printf, 1, 2)))
63 c39be742 2021-07-09 op __attribute__((__nonnull__ (1)));
64 f3966209 2021-07-13 op void yywarn(const char *, ...)
65 f3966209 2021-07-13 op __attribute__((__format__ (printf, 1, 2)))
66 f3966209 2021-07-13 op __attribute__((__nonnull__ (1)));
67 c39be742 2021-07-09 op int kw_cmp(const void *, const void *);
68 c39be742 2021-07-09 op int lookup(char *);
69 c39be742 2021-07-09 op int igetc(void);
70 c39be742 2021-07-09 op int lgetc(int);
71 c39be742 2021-07-09 op void lungetc(int);
72 c39be742 2021-07-09 op int findeol(void);
75 15902770 2021-01-15 op * #define YYDEBUG 1
76 15902770 2021-01-15 op * int yydebug = 1;
79 3b21cca3 2021-06-29 op TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
81 3b21cca3 2021-06-29 op TAILQ_ENTRY(sym) entry;
88 c39be742 2021-07-09 op int symset(const char *, const char *, int);
89 c39be742 2021-07-09 op char *symget(const char *);
91 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
92 6abda252 2021-02-06 op int check_block_code(int);
93 6abda252 2021-02-06 op char *check_block_fmt(char*);
94 6abda252 2021-02-06 op int check_strip_no(int);
95 391825e3 2021-07-09 op int check_port_num(int);
96 a709ddf5 2021-02-07 op int check_prefork_num(int);
97 49b73ba1 2021-02-10 op void advance_loc(void);
98 b7967bc1 2021-01-02 op void advance_proxy(void);
99 b7967bc1 2021-01-02 op void parsehp(char *, char **, const char **, const char *);
100 7b00c890 2022-10-05 op int fastcgi_conf(const char *, const char *);
101 2025e96d 2022-09-10 op void add_param(char *, char *);
102 534afd0d 2022-10-05 op int getservice(const char *);
103 509d0509 2023-06-23 op void listen_on(const char *, const char *);
105 c39be742 2021-07-09 op static struct vhost *host;
106 c39be742 2021-07-09 op static struct location *loc;
107 b7967bc1 2021-01-02 op static struct proxy *proxy;
108 ee219d70 2022-02-26 op static char *current_media;
109 c39be742 2021-07-09 op static int errors;
111 c39be742 2021-07-09 op typedef struct {
113 c39be742 2021-07-09 op char *string;
119 8af9da98 2023-06-13 op #define YYSTYPE YYSTYPE
123 15902770 2021-01-15 op /* for bison: */
124 15902770 2021-01-15 op /* %define parse.error verbose */
126 226f13ec 2023-07-24 op %token ACCESS ALIAS AUTO
128 f862d389 2024-01-30 op %token CA CERT CHROOT CLIENT
129 c74c7030 2021-07-19 op %token DEFAULT
130 9abba172 2023-08-07 op %token FACILITY FASTCGI FOR_HOST
131 c74c7030 2021-07-19 op %token INCLUDE INDEX IPV6
133 f862d389 2024-01-30 op %token LANG LISTEN LOCATION LOG
134 ff05125e 2021-10-15 op %token OCSP OFF ON
135 b7967bc1 2021-01-02 op %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
136 72b033ef 2021-12-29 op %token RELAY_TO REQUIRE RETURN ROOT
137 abd261d2 2023-07-25 op %token SERVER SNI SOCKET STRIP STYLE SYSLOG
138 ee219d70 2022-02-26 op %token TCP TOEXT TYPE TYPES
139 593e412b 2021-01-01 op %token USE_TLS USER
140 5128c0b0 2021-01-01 op %token VERIFYNAME
144 c39be742 2021-07-09 op %token <v.string> STRING
145 c39be742 2021-07-09 op %token <v.number> NUM
147 534afd0d 2022-10-05 op %type <v.number> bool proxy_port
148 a7a998ac 2023-06-23 op %type <v.string> string numberstring listen_addr
152 6b86655a 2021-06-29 op conf : /* empty */
153 ddb089c1 2024-01-26 op | conf include nl
154 ddb089c1 2024-01-26 op | conf varset nl
155 ddb089c1 2024-01-26 op | conf option nl
156 ddb089c1 2024-01-26 op | conf vhost nl
157 ddb089c1 2024-01-26 op | conf types nl
158 ddb089c1 2024-01-26 op | conf error nl { file->errors++; }
161 c39be742 2021-07-09 op include : INCLUDE STRING {
162 c39be742 2021-07-09 op struct file *nfile;
164 c39be742 2021-07-09 op if ((nfile = pushfile($2, 0)) == NULL) {
165 c39be742 2021-07-09 op yyerror("failed to include file %s", $2);
171 c39be742 2021-07-09 op file = nfile;
172 c39be742 2021-07-09 op lungetc('\n');
176 c74c7030 2021-07-19 op bool : ON { $$ = 1; }
177 c74c7030 2021-07-19 op | OFF { $$ = 0; }
180 c39be742 2021-07-09 op string : string STRING {
181 98f52178 2021-06-29 op if (asprintf(&$$, "%s%s", $1, $2) == -1) {
184 98f52178 2021-06-29 op yyerror("string: asprintf: %s", strerror(errno));
193 ee219d70 2022-02-26 op numberstring : NUM {
195 ee219d70 2022-02-26 op if (asprintf(&s, "%d", $1) == -1) {
196 ee219d70 2022-02-26 op yyerror("asprintf: number");
204 c39be742 2021-07-09 op varset : STRING '=' string {
205 3b21cca3 2021-06-29 op char *s = $1;
206 3b21cca3 2021-06-29 op while (*s++) {
207 c39be742 2021-07-09 op if (isspace((unsigned char)*s)) {
208 3b21cca3 2021-06-29 op yyerror("macro name cannot contain "
209 3b21cca3 2021-06-29 op "whitespaces");
215 3b21cca3 2021-06-29 op symset($1, $3, 0);
221 7277bb7d 2022-09-10 op option : CHROOT string {
222 af1dab18 2023-06-09 op if (strlcpy(conf->chroot, $2, sizeof(conf->chroot)) >=
223 af1dab18 2023-06-09 op sizeof(conf->chroot))
224 7277bb7d 2022-09-10 op yyerror("chroot path too long");
227 509d0509 2023-06-23 op | IPV6 bool {
228 509d0509 2023-06-23 op yywarn("option `ipv6' is deprecated,"
229 509d0509 2023-06-23 op " please use `listen on'");
231 c2c051f2 2023-08-25 op default_host = NULL;
233 509d0509 2023-06-23 op default_host = "0.0.0.0";
237 509d0509 2023-06-23 op yywarn("option `port' is deprecated,"
238 509d0509 2023-06-23 op " please use `listen on'");
239 509d0509 2023-06-23 op default_port = $2;
241 af1dab18 2023-06-09 op | PREFORK NUM { conf->prefork = check_prefork_num($2); }
242 c74c7030 2021-07-19 op | PROTOCOLS string {
243 af1dab18 2023-06-09 op if (tls_config_parse_protocols(&conf->protos, $2) == -1)
244 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
247 7277bb7d 2022-09-10 op | USER string {
248 af1dab18 2023-06-09 op if (strlcpy(conf->user, $2, sizeof(conf->user)) >=
249 af1dab18 2023-06-09 op sizeof(conf->user))
250 7277bb7d 2022-09-10 op yyerror("user name too long");
255 226f13ec 2023-07-24 op log : LOG '{' optnl logopts '}'
259 226f13ec 2023-07-24 op logopts : /* empty */
260 226f13ec 2023-07-24 op | logopts logopt optnl
263 3a93c904 2023-08-07 op logopt : ACCESS string {
264 226f13ec 2023-07-24 op free(conf->log_access);
265 226f13ec 2023-07-24 op conf->log_access = $2;
267 f862d389 2024-01-30 op | STYLE string {
268 f862d389 2024-01-30 op if (!strcmp("combined", $2))
269 f862d389 2024-01-30 op conf->log_format = LOG_FORMAT_COMBINED;
270 f862d389 2024-01-30 op else if (!strcmp("common", $2))
271 f862d389 2024-01-30 op conf->log_format = LOG_FORMAT_COMMON;
272 f862d389 2024-01-30 op else if (!strcmp("condensed", $2))
273 f862d389 2024-01-30 op conf->log_format = LOG_FORMAT_CONDENSED;
274 f862d389 2024-01-30 op else if (!strcmp("legacy", $2))
275 f862d389 2024-01-30 op conf->log_format = LOG_FORMAT_LEGACY;
277 f862d389 2024-01-30 op yyerror("unknown log style: %s", $2);
280 9abba172 2023-08-07 op | SYSLOG FACILITY string {
281 9abba172 2023-08-07 op const char *str = $3;
283 9abba172 2023-08-07 op conf->log_syslog = 1;
285 9abba172 2023-08-07 op if (!strncasecmp(str, "LOG_", 4))
288 9abba172 2023-08-07 op if (!strcasecmp(str, "daemon"))
289 9abba172 2023-08-07 op conf->log_facility = LOG_DAEMON;
290 9abba172 2023-08-07 op #ifdef LOG_FTP
291 9abba172 2023-08-07 op else if (!strcasecmp(str, "ftp"))
292 9abba172 2023-08-07 op conf->log_facility = LOG_FTP;
294 9abba172 2023-08-07 op else if (!strcasecmp(str, "local1"))
295 9abba172 2023-08-07 op conf->log_facility = LOG_LOCAL1;
296 9abba172 2023-08-07 op else if (!strcasecmp(str, "local2"))
297 9abba172 2023-08-07 op conf->log_facility = LOG_LOCAL2;
298 9abba172 2023-08-07 op else if (!strcasecmp(str, "local3"))
299 9abba172 2023-08-07 op conf->log_facility = LOG_LOCAL3;
300 9abba172 2023-08-07 op else if (!strcasecmp(str, "local4"))
301 9abba172 2023-08-07 op conf->log_facility = LOG_LOCAL4;
302 9abba172 2023-08-07 op else if (!strcasecmp(str, "local5"))
303 9abba172 2023-08-07 op conf->log_facility = LOG_LOCAL5;
304 9abba172 2023-08-07 op else if (!strcasecmp(str, "local6"))
305 9abba172 2023-08-07 op conf->log_facility = LOG_LOCAL6;
306 9abba172 2023-08-07 op else if (!strcasecmp(str, "local7"))
307 9abba172 2023-08-07 op conf->log_facility = LOG_LOCAL7;
308 9abba172 2023-08-07 op else if (!strcasecmp(str, "user"))
309 9abba172 2023-08-07 op conf->log_facility = LOG_USER;
311 9abba172 2023-08-07 op yywarn("unknown syslog facility `%s'",
316 3a93c904 2023-08-07 op | SYSLOG OFF {
317 3a93c904 2023-08-07 op conf->log_syslog = 0;
320 3a93c904 2023-08-07 op conf->log_syslog = 1;
324 c74c7030 2021-07-19 op vhost : SERVER string {
325 b8e64ccd 2021-03-31 op host = new_vhost();
326 af1dab18 2023-06-09 op TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
328 b8e64ccd 2021-03-31 op loc = new_location();
329 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
331 b7967bc1 2021-01-02 op TAILQ_INIT(&host->proxies);
333 534afd0d 2022-10-05 op (void) strlcpy(loc->match, "*", sizeof(loc->match));
334 534afd0d 2022-10-05 op (void) strlcpy(host->domain, $2, sizeof(host->domain));
336 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
337 f3966209 2021-07-13 op yywarn("\"%s\" looks like punycode: you "
338 f3966209 2021-07-13 op "should use the decoded hostname", $2);
342 b7967bc1 2021-01-02 op } '{' optnl servbody '}' {
343 1c6967b3 2023-06-08 op if (host->cert_path == NULL ||
344 1c6967b3 2023-06-08 op host->key_path == NULL)
345 fc2d207c 2023-06-23 op yyerror("invalid vhost definition: %s",
346 fc2d207c 2023-06-23 op host->domain);
347 509d0509 2023-06-23 op if (TAILQ_EMPTY(&host->addrs)) {
348 509d0509 2023-06-23 op char portno[32];
351 509d0509 2023-06-23 op r = snprintf(portno, sizeof(portno), "%d",
352 509d0509 2023-06-23 op default_port);
353 509d0509 2023-06-23 op if (r < 0 || (size_t)r >= sizeof(portno))
354 509d0509 2023-06-23 op fatal("snprintf");
356 509d0509 2023-06-23 op yywarn("missing `listen on' in server %s,"
357 d8df6756 2024-01-11 op " assuming %s port %d", host->domain,
358 c2c051f2 2023-08-25 op default_host ? default_host : "*",
359 509d0509 2023-06-23 op default_port);
360 509d0509 2023-06-23 op listen_on(default_host, portno);
363 f3966209 2021-07-13 op | error '}' { yyerror("bad server directive"); }
366 b7967bc1 2021-01-02 op servbody : /* empty */
367 b7967bc1 2021-01-02 op | servbody servopt optnl
368 b7967bc1 2021-01-02 op | servbody location optnl
369 b7967bc1 2021-01-02 op | servbody proxy optnl
372 a7a998ac 2023-06-23 op listen_addr : '*' { $$ = NULL; }
376 c74c7030 2021-07-19 op servopt : ALIAS string {
377 cc8c2901 2021-04-29 op struct alist *a;
379 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
380 534afd0d 2022-10-05 op (void) strlcpy(a->alias, $2, sizeof(a->alias));
382 edc5ca66 2022-09-10 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
384 c74c7030 2021-07-19 op | CERT string {
385 534afd0d 2022-10-05 op ensure_absolute_path($2);
386 1c6967b3 2023-06-08 op free(host->cert_path);
387 1c6967b3 2023-06-08 op host->cert_path = $2;
389 c74c7030 2021-07-19 op | KEY string {
390 534afd0d 2022-10-05 op ensure_absolute_path($2);
391 1c6967b3 2023-06-08 op free(host->key_path);
392 1c6967b3 2023-06-08 op host->key_path = $2;
394 ff05125e 2021-10-15 op | OCSP string {
395 534afd0d 2022-10-05 op ensure_absolute_path($2);
396 1c6967b3 2023-06-08 op free(host->ocsp_path);
397 1c6967b3 2023-06-08 op host->ocsp_path = $2;
399 c74c7030 2021-07-19 op | PARAM string '=' string {
400 a1ba9650 2023-07-23 op yywarn("the top-level `param' directive is deprecated."
401 a1ba9650 2023-07-23 op " Please use `fastcgi { param ... }`");
402 2025e96d 2022-09-10 op add_param($2, $4);
404 911156fb 2023-06-29 op | LISTEN ON listen_addr {
405 911156fb 2023-06-29 op listen_on($3, "1965");
408 a7a998ac 2023-06-23 op | LISTEN ON listen_addr PORT STRING {
409 509d0509 2023-06-23 op listen_on($3, $5);
413 a7a998ac 2023-06-23 op | LISTEN ON listen_addr PORT NUM {
414 509d0509 2023-06-23 op char portno[32];
417 509d0509 2023-06-23 op r = snprintf(portno, sizeof(portno), "%d", $5);
418 509d0509 2023-06-23 op if (r < 0 || (size_t)r >= sizeof(portno))
419 509d0509 2023-06-23 op fatal("snprintf");
421 509d0509 2023-06-23 op listen_on($3, portno);
427 b7967bc1 2021-01-02 op proxy : PROXY { advance_proxy(); }
428 b7967bc1 2021-01-02 op proxy_matches '{' optnl proxy_opts '}' {
429 534afd0d 2022-10-05 op if (*proxy->host == '\0')
430 b7967bc1 2021-01-02 op yyerror("invalid proxy block: missing `relay-to' option");
432 deadd9e1 2023-06-09 op if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
433 deadd9e1 2023-06-09 op (proxy->cert_path != NULL && proxy->key_path == NULL))
434 b7967bc1 2021-01-02 op yyerror("invalid proxy block: missing cert or key");
438 b7967bc1 2021-01-02 op proxy_matches : /* empty */
439 b7967bc1 2021-01-02 op | proxy_matches proxy_match
442 534afd0d 2022-10-05 op proxy_port : /* empty */ { $$ = 1965; }
443 534afd0d 2022-10-05 op | PORT STRING {
444 534afd0d 2022-10-05 op if (($$ = getservice($2)) == -1)
445 534afd0d 2022-10-05 op yyerror("invalid port number %s", $2);
448 534afd0d 2022-10-05 op | PORT NUM { $$ = $2; }
451 b7967bc1 2021-01-02 op proxy_match : PROTO string {
452 534afd0d 2022-10-05 op (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
455 534afd0d 2022-10-05 op | FOR_HOST string proxy_port {
456 534afd0d 2022-10-05 op (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
457 534afd0d 2022-10-05 op (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
463 7bdcc91e 2021-01-01 op proxy_opts : /* empty */
464 7bdcc91e 2021-01-01 op | proxy_opts proxy_opt optnl
467 7bdcc91e 2021-01-01 op proxy_opt : CERT string {
468 deadd9e1 2023-06-09 op free(proxy->cert);
469 7bdcc91e 2021-01-01 op ensure_absolute_path($2);
470 deadd9e1 2023-06-09 op proxy->cert_path = $2;
472 7bdcc91e 2021-01-01 op | KEY string {
473 deadd9e1 2023-06-09 op free(proxy->key);
474 7bdcc91e 2021-01-01 op ensure_absolute_path($2);
475 deadd9e1 2023-06-09 op proxy->key_path = $2;
477 c7c8ef44 2021-01-01 op | PROTOCOLS string {
478 b7967bc1 2021-01-02 op if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
479 c7c8ef44 2021-01-01 op yyerror("invalid protocols string \"%s\"", $2);
482 534afd0d 2022-10-05 op | RELAY_TO string proxy_port {
483 534afd0d 2022-10-05 op (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
484 534afd0d 2022-10-05 op (void) snprintf(proxy->port, sizeof(proxy->port),
488 ba94a608 2022-01-04 op | REQUIRE CLIENT CA string {
489 ba94a608 2022-01-04 op ensure_absolute_path($4);
490 deadd9e1 2023-06-09 op proxy->reqca_path = $4;
492 1cdea97b 2022-01-30 op | SNI string {
493 534afd0d 2022-10-05 op (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
496 593e412b 2021-01-01 op | USE_TLS bool {
497 b7967bc1 2021-01-02 op proxy->notls = !$2;
499 5128c0b0 2021-01-01 op | VERIFYNAME bool {
500 b7967bc1 2021-01-02 op proxy->noverifyname = !$2;
504 c74c7030 2021-07-19 op location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
505 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
506 49b73ba1 2021-02-10 op if (*$3 == '/')
507 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
508 534afd0d 2022-10-05 op (void) strlcpy(loc->match, $3, sizeof(loc->match));
514 c8b74339 2021-01-24 op locopts : /* empty */
515 c39be742 2021-07-09 op | locopts locopt optnl
518 c74c7030 2021-07-19 op locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
519 c74c7030 2021-07-19 op | BLOCK RETURN NUM string {
520 534afd0d 2022-10-05 op check_block_fmt($4);
521 534afd0d 2022-10-05 op (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
522 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
525 c74c7030 2021-07-19 op | BLOCK RETURN NUM {
526 534afd0d 2022-10-05 op (void) strlcpy(loc->block_fmt, "temporary failure",
527 534afd0d 2022-10-05 op sizeof(loc->block_fmt));
528 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
529 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
530 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
533 534afd0d 2022-10-05 op (void) strlcpy(loc->block_fmt, "temporary failure",
534 534afd0d 2022-10-05 op sizeof(loc->block_fmt));
535 6abda252 2021-02-06 op loc->block_code = 40;
537 c74c7030 2021-07-19 op | DEFAULT TYPE string {
538 534afd0d 2022-10-05 op (void) strlcpy(loc->default_mime, $3,
539 534afd0d 2022-10-05 op sizeof(loc->default_mime));
543 c74c7030 2021-07-19 op | INDEX string {
544 534afd0d 2022-10-05 op (void) strlcpy(loc->index, $2, sizeof(loc->index));
547 c74c7030 2021-07-19 op | LANG string {
548 534afd0d 2022-10-05 op (void) strlcpy(loc->lang, $2,
549 534afd0d 2022-10-05 op sizeof(loc->lang));
552 c74c7030 2021-07-19 op | LOG bool { loc->disable_log = !$2; }
553 da2185f3 2021-01-01 op | REQUIRE CLIENT CA string {
554 da2185f3 2021-01-01 op ensure_absolute_path($4);
555 deadd9e1 2023-06-09 op loc->reqca_path = $4;
557 da2185f3 2021-01-01 op | ROOT string {
558 534afd0d 2022-10-05 op (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
561 da2185f3 2021-01-01 op | STRIP NUM { loc->strip = check_strip_no($2); }
564 a1ba9650 2023-07-23 op fastcgi : FASTCGI '{' optnl fastcgiopts '}'
565 a1ba9650 2023-07-23 op | FASTCGI fastcgiopt
566 6a8387e5 2023-07-23 op | FASTCGI OFF {
567 6a8387e5 2023-07-23 op loc->fcgi = -1;
568 6a8387e5 2023-07-23 op loc->nofcgi = 1;
570 a1ba9650 2023-07-23 op | FASTCGI string {
571 a1ba9650 2023-07-23 op yywarn("`fastcgi path' is deprecated. "
572 a1ba9650 2023-07-23 op "Please use `fastcgi socket path' instead.");
573 a1ba9650 2023-07-23 op loc->fcgi = fastcgi_conf($2, NULL);
578 a1ba9650 2023-07-23 op fastcgiopts : /* empty */
579 a1ba9650 2023-07-23 op | fastcgiopts fastcgiopt optnl
582 a1ba9650 2023-07-23 op fastcgiopt : PARAM string '=' string {
583 a1ba9650 2023-07-23 op add_param($2, $4);
585 a1ba9650 2023-07-23 op | SOCKET string {
586 a1ba9650 2023-07-23 op loc->fcgi = fastcgi_conf($2, NULL);
589 a1ba9650 2023-07-23 op | SOCKET TCP string PORT NUM {
592 a1ba9650 2023-07-23 op if (asprintf(&c, "%d", $5) == -1)
593 a1ba9650 2023-07-23 op fatal("asprintf");
594 a1ba9650 2023-07-23 op loc->fcgi = fastcgi_conf($3, c);
598 a1ba9650 2023-07-23 op | SOCKET TCP string {
599 a1ba9650 2023-07-23 op loc->fcgi = fastcgi_conf($3, "9000");
601 a1ba9650 2023-07-23 op | SOCKET TCP string PORT string {
602 a1ba9650 2023-07-23 op loc->fcgi = fastcgi_conf($3, $5);
606 03d671e2 2023-08-08 op | STRIP NUM {
607 03d671e2 2023-08-08 op loc->fcgi_strip = $2;
611 cd5826b8 2022-09-10 op types : TYPES '{' optnl mediaopts_l '}' ;
613 ee219d70 2022-02-26 op mediaopts_l : mediaopts_l mediaoptsl nl
614 ee219d70 2022-02-26 op | mediaoptsl nl
617 aa9543b9 2022-09-10 op mediaoptsl : STRING {
618 aa9543b9 2022-09-10 op free(current_media);
619 aa9543b9 2022-09-10 op current_media = $1;
620 ddb089c1 2024-01-26 op } medianames_l
624 ee219d70 2022-02-26 op medianames_l : medianames_l medianamesl
625 ee219d70 2022-02-26 op | medianamesl
628 d8d170aa 2022-04-08 op medianamesl : numberstring {
629 af1dab18 2023-06-09 op if (add_mime(&conf->mime, current_media, $1) == -1)
630 792f302a 2023-06-09 op fatal("add_mime");
635 ee219d70 2022-02-26 op nl : '\n' optnl
645 e5d82d94 2022-03-19 op static const struct keyword {
646 74f0778b 2021-06-16 op const char *word;
648 74f0778b 2021-06-16 op } keywords[] = {
649 d93c8191 2021-07-09 op /* these MUST be sorted */
650 226f13ec 2023-07-24 op {"access", ACCESS},
651 c74c7030 2021-07-19 op {"alias", ALIAS},
652 c74c7030 2021-07-19 op {"auto", AUTO},
653 c74c7030 2021-07-19 op {"block", BLOCK},
655 c74c7030 2021-07-19 op {"cert", CERT},
656 c74c7030 2021-07-19 op {"chroot", CHROOT},
657 c74c7030 2021-07-19 op {"client", CLIENT},
658 c74c7030 2021-07-19 op {"default", DEFAULT},
659 9abba172 2023-08-07 op {"facility", FACILITY},
660 abc8801d 2021-07-19 op {"fastcgi", FASTCGI},
661 b7967bc1 2021-01-02 op {"for-host", FOR_HOST},
662 88971f9a 2022-02-26 op {"include", INCLUDE},
663 c74c7030 2021-07-19 op {"index", INDEX},
664 c74c7030 2021-07-19 op {"ipv6", IPV6},
665 c74c7030 2021-07-19 op {"key", KEY},
666 c74c7030 2021-07-19 op {"lang", LANG},
667 509d0509 2023-06-23 op {"listen", LISTEN},
668 c74c7030 2021-07-19 op {"location", LOCATION},
669 c74c7030 2021-07-19 op {"log", LOG},
670 ff05125e 2021-10-15 op {"ocsp", OCSP},
671 c74c7030 2021-07-19 op {"off", OFF},
673 c74c7030 2021-07-19 op {"param", PARAM},
674 c74c7030 2021-07-19 op {"port", PORT},
675 c74c7030 2021-07-19 op {"prefork", PREFORK},
676 b7967bc1 2021-01-02 op {"proto", PROTO},
677 c74c7030 2021-07-19 op {"protocols", PROTOCOLS},
678 72b033ef 2021-12-29 op {"proxy", PROXY},
679 72b033ef 2021-12-29 op {"relay-to", RELAY_TO},
680 c74c7030 2021-07-19 op {"require", REQUIRE},
681 c74c7030 2021-07-19 op {"return", RETURN},
682 c74c7030 2021-07-19 op {"root", ROOT},
683 c74c7030 2021-07-19 op {"server", SERVER},
684 1cdea97b 2022-01-30 op {"sni", SNI},
685 a1ba9650 2023-07-23 op {"socket", SOCKET},
686 c74c7030 2021-07-19 op {"strip", STRIP},
687 abd261d2 2023-07-25 op {"style", STYLE},
688 226f13ec 2023-07-24 op {"syslog", SYSLOG},
689 c74c7030 2021-07-19 op {"tcp", TCP},
690 c74c7030 2021-07-19 op {"to-ext", TOEXT},
691 c74c7030 2021-07-19 op {"type", TYPE},
692 ee219d70 2022-02-26 op {"types", TYPES},
693 593e412b 2021-01-01 op {"use-tls", USE_TLS},
694 c74c7030 2021-07-19 op {"user", USER},
695 5128c0b0 2021-01-01 op {"verifyname", VERIFYNAME},
699 c39be742 2021-07-09 op yyerror(const char *msg, ...)
703 c39be742 2021-07-09 op file->errors++;
705 c39be742 2021-07-09 op va_start(ap, msg);
706 f3966209 2021-07-13 op fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
707 f3966209 2021-07-13 op vfprintf(stderr, msg, ap);
708 f3966209 2021-07-13 op fprintf(stderr, "\n");
713 f3966209 2021-07-13 op yywarn(const char *msg, ...)
717 f3966209 2021-07-13 op va_start(ap, msg);
718 f3966209 2021-07-13 op fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
719 c39be742 2021-07-09 op vfprintf(stderr, msg, ap);
720 c39be742 2021-07-09 op fprintf(stderr, "\n");
725 d93c8191 2021-07-09 op kw_cmp(const void *k, const void *e)
727 d93c8191 2021-07-09 op return strcmp(k, ((struct keyword *)e)->word);
731 c39be742 2021-07-09 op lookup(char *s)
733 c39be742 2021-07-09 op const struct keyword *p;
735 c39be742 2021-07-09 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
736 c39be742 2021-07-09 op sizeof(keywords[0]), kw_cmp);
739 c39be742 2021-07-09 op return p->token;
741 c39be742 2021-07-09 op return STRING;
744 c39be742 2021-07-09 op #define START_EXPAND 1
745 c39be742 2021-07-09 op #define DONE_EXPAND 2
747 c39be742 2021-07-09 op static int expanding;
755 c39be742 2021-07-09 op if (file->ungetpos > 0)
756 c39be742 2021-07-09 op c = file->ungetbuf[--file->ungetpos];
758 c39be742 2021-07-09 op c = getc(file->stream);
760 c39be742 2021-07-09 op if (c == START_EXPAND)
761 c39be742 2021-07-09 op expanding = 1;
762 c39be742 2021-07-09 op else if (c == DONE_EXPAND)
763 c39be742 2021-07-09 op expanding = 0;
771 c39be742 2021-07-09 op lgetc(int quotec)
775 c39be742 2021-07-09 op if (quotec) {
776 c39be742 2021-07-09 op if ((c = igetc()) == EOF) {
777 c39be742 2021-07-09 op yyerror("reached end of file while parsing "
778 c39be742 2021-07-09 op "quoted string");
779 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
781 c39be742 2021-07-09 op return quotec;
786 c39be742 2021-07-09 op while ((c = igetc()) == '\\') {
787 c39be742 2021-07-09 op next = igetc();
788 c39be742 2021-07-09 op if (next != '\n') {
792 c39be742 2021-07-09 op yylval.lineno = file->lineno;
793 c39be742 2021-07-09 op file->lineno++;
796 c39be742 2021-07-09 op if (c == EOF) {
798 c39be742 2021-07-09 op * Fake EOL when hit EOF for the first time. This gets line
799 c39be742 2021-07-09 op * count right if last line in included file is syntactically
800 c39be742 2021-07-09 op * invalid and has no newline.
802 c39be742 2021-07-09 op if (file->eof_reached == 0) {
803 c39be742 2021-07-09 op file->eof_reached = 1;
806 c39be742 2021-07-09 op while (c == EOF) {
807 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
816 c39be742 2021-07-09 op lungetc(int c)
818 c39be742 2021-07-09 op if (c == EOF)
821 c39be742 2021-07-09 op if (file->ungetpos >= file->ungetsize) {
822 c39be742 2021-07-09 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
823 c39be742 2021-07-09 op if (p == NULL)
824 2dd5994a 2023-06-06 op fatal("lungetc");
825 c39be742 2021-07-09 op file->ungetbuf = p;
826 c39be742 2021-07-09 op file->ungetsize *= 2;
828 c39be742 2021-07-09 op file->ungetbuf[file->ungetpos++] = c;
832 c39be742 2021-07-09 op findeol(void)
836 c39be742 2021-07-09 op /* Skip to either EOF or the first real EOL. */
838 c39be742 2021-07-09 op c = lgetc(0);
839 c39be742 2021-07-09 op if (c == '\n') {
840 c39be742 2021-07-09 op file->lineno++;
843 c39be742 2021-07-09 op if (c == EOF)
846 c39be742 2021-07-09 op return ERROR;
852 1bd706dc 2021-07-09 op char buf[8096];
853 1bd706dc 2021-07-09 op char *p, *val;
854 1bd706dc 2021-07-09 op int quotec, next, c;
859 c39be742 2021-07-09 op while ((c = lgetc(0)) == ' ' || c == '\t')
860 c39be742 2021-07-09 op ; /* nothing */
862 c39be742 2021-07-09 op yylval.lineno = file->lineno;
863 c39be742 2021-07-09 op if (c == '#')
864 c39be742 2021-07-09 op while ((c = lgetc(0)) != '\n' && c != EOF)
865 c39be742 2021-07-09 op ; /* nothing */
866 c39be742 2021-07-09 op if (c == '$' && !expanding) {
868 c39be742 2021-07-09 op if ((c = lgetc(0)) == EOF)
870 67f49405 2021-07-09 op if (p + 1 >= buf + sizeof(buf) -1) {
871 67f49405 2021-07-09 op yyerror("string too long");
872 67f49405 2021-07-09 op return findeol();
874 67f49405 2021-07-09 op if (isalnum(c) || c == '_') {
882 67f49405 2021-07-09 op val = symget(buf);
883 67f49405 2021-07-09 op if (val == NULL) {
884 67f49405 2021-07-09 op yyerror("macro `%s' not defined", buf);
885 67f49405 2021-07-09 op return findeol();
887 67f49405 2021-07-09 op yylval.v.string = xstrdup(val);
888 67f49405 2021-07-09 op return STRING;
890 67f49405 2021-07-09 op if (c == '@' && !expanding) {
892 67f49405 2021-07-09 op if ((c = lgetc(0)) == EOF)
895 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
896 c39be742 2021-07-09 op yyerror("string too long");
897 c39be742 2021-07-09 op return findeol();
899 c39be742 2021-07-09 op if (isalnum(c) || c == '_') {
907 c39be742 2021-07-09 op val = symget(buf);
908 c39be742 2021-07-09 op if (val == NULL) {
909 c39be742 2021-07-09 op yyerror("macro '%s' not defined", buf);
910 c39be742 2021-07-09 op return findeol();
912 c39be742 2021-07-09 op p = val + strlen(val) - 1;
913 c39be742 2021-07-09 op lungetc(DONE_EXPAND);
914 c39be742 2021-07-09 op while (p >= val) {
918 c39be742 2021-07-09 op lungetc(START_EXPAND);
927 c39be742 2021-07-09 op if ((c = lgetc(quotec)) == EOF)
929 c39be742 2021-07-09 op if (c == '\n') {
930 c39be742 2021-07-09 op file->lineno++;
932 c39be742 2021-07-09 op } else if (c == '\\') {
933 c39be742 2021-07-09 op if ((next = lgetc(quotec)) == EOF)
935 c39be742 2021-07-09 op if (next == quotec || next == ' ' ||
936 c39be742 2021-07-09 op next == '\t')
938 c39be742 2021-07-09 op else if (next == '\n') {
939 c39be742 2021-07-09 op file->lineno++;
942 c39be742 2021-07-09 op lungetc(next);
943 c39be742 2021-07-09 op } else if (c == quotec) {
946 c39be742 2021-07-09 op } else if (c == '\0') {
947 f3966209 2021-07-13 op yyerror("invalid syntax");
948 c39be742 2021-07-09 op return findeol();
950 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
951 c39be742 2021-07-09 op yyerror("string too long");
952 c39be742 2021-07-09 op return findeol();
956 c39be742 2021-07-09 op yylval.v.string = strdup(buf);
957 c39be742 2021-07-09 op if (yylval.v.string == NULL)
958 2dd5994a 2023-06-06 op fatal("yylex: strdup");
959 c39be742 2021-07-09 op return STRING;
962 c39be742 2021-07-09 op #define allowed_to_end_number(x) \
963 c39be742 2021-07-09 op (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
965 c39be742 2021-07-09 op if (c == '-' || isdigit(c)) {
968 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
969 c39be742 2021-07-09 op yyerror("string too long");
970 c39be742 2021-07-09 op return findeol();
972 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && isdigit(c));
974 c39be742 2021-07-09 op if (p == buf + 1 && buf[0] == '-')
975 c39be742 2021-07-09 op goto nodigits;
976 c39be742 2021-07-09 op if (c == EOF || allowed_to_end_number(c)) {
977 c39be742 2021-07-09 op const char *errstr = NULL;
980 c39be742 2021-07-09 op yylval.v.number = strtonum(buf, LLONG_MIN,
981 c39be742 2021-07-09 op LLONG_MAX, &errstr);
982 c39be742 2021-07-09 op if (errstr) {
983 c39be742 2021-07-09 op yyerror("\"%s\" invalid number: %s",
984 c39be742 2021-07-09 op buf, errstr);
985 c39be742 2021-07-09 op return findeol();
990 c39be742 2021-07-09 op while (p > buf + 1)
991 c39be742 2021-07-09 op lungetc(*--p);
993 c39be742 2021-07-09 op if (c == '-')
998 c39be742 2021-07-09 op #define allowed_in_string(x) \
999 c39be742 2021-07-09 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1000 c39be742 2021-07-09 op x != '{' && x != '}' && \
1001 c39be742 2021-07-09 op x != '!' && x != '=' && x != '#' && \
1002 67f49405 2021-07-09 op x != ',' && x != ';'))
1004 67f49405 2021-07-09 op if (isalnum(c) || c == ':' || c == '_') {
1007 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
1008 c39be742 2021-07-09 op yyerror("string too long");
1009 c39be742 2021-07-09 op return findeol();
1011 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
1014 c39be742 2021-07-09 op if ((token = lookup(buf)) == STRING)
1015 c39be742 2021-07-09 op yylval.v.string = xstrdup(buf);
1016 c39be742 2021-07-09 op return token;
1018 c39be742 2021-07-09 op if (c == '\n') {
1019 c39be742 2021-07-09 op yylval.lineno = file->lineno;
1020 c39be742 2021-07-09 op file->lineno++;
1022 c39be742 2021-07-09 op if (c == EOF)
1027 c39be742 2021-07-09 op struct file *
1028 c39be742 2021-07-09 op pushfile(const char *name, int secret)
1030 c39be742 2021-07-09 op struct file *nfile;
1032 c39be742 2021-07-09 op nfile = xcalloc(1, sizeof(*nfile));
1033 c39be742 2021-07-09 op nfile->name = xstrdup(name);
1034 c39be742 2021-07-09 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
1035 eae52ad4 2023-06-06 op log_warn("can't open %s", nfile->name);
1036 c39be742 2021-07-09 op free(nfile->name);
1037 c39be742 2021-07-09 op free(nfile);
1038 c39be742 2021-07-09 op return NULL;
1040 c39be742 2021-07-09 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
1041 c39be742 2021-07-09 op nfile->ungetsize = 16;
1042 c39be742 2021-07-09 op nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
1043 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&files, nfile, entry);
1044 c39be742 2021-07-09 op return nfile;
1048 c39be742 2021-07-09 op popfile(void)
1050 c39be742 2021-07-09 op struct file *prev;
1052 c39be742 2021-07-09 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
1053 c39be742 2021-07-09 op prev->errors += file->errors;
1055 c39be742 2021-07-09 op TAILQ_REMOVE(&files, file, entry);
1056 c39be742 2021-07-09 op fclose(file->stream);
1057 c39be742 2021-07-09 op free(file->name);
1058 c39be742 2021-07-09 op free(file->ungetbuf);
1060 c39be742 2021-07-09 op file = prev;
1061 c39be742 2021-07-09 op return file ? 0 : EOF;
1065 af1dab18 2023-06-09 op parse_conf(struct conf *c, const char *filename)
1067 c39be742 2021-07-09 op struct sym *sym, *next;
1069 c2c051f2 2023-08-25 op default_host = NULL;
1070 509d0509 2023-06-23 op default_port = 1965;
1074 c39be742 2021-07-09 op file = pushfile(filename, 0);
1075 c39be742 2021-07-09 op if (file == NULL)
1077 c39be742 2021-07-09 op topfile = file;
1080 c39be742 2021-07-09 op errors = file->errors;
1083 c39be742 2021-07-09 op /* Free macros and check which have not been used. */
1084 3b21cca3 2021-06-29 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1085 3b21cca3 2021-06-29 op /* TODO: warn if !sym->used */
1086 3b21cca3 2021-06-29 op if (!sym->persist) {
1087 3b21cca3 2021-06-29 op free(sym->name);
1088 3b21cca3 2021-06-29 op free(sym->val);
1089 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
1100 c39be742 2021-07-09 op symset(const char *name, const char *val, int persist)
1102 c39be742 2021-07-09 op struct sym *sym;
1104 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
1105 c39be742 2021-07-09 op if (!strcmp(name, sym->name))
1109 c39be742 2021-07-09 op if (sym != NULL) {
1110 c39be742 2021-07-09 op if (sym->persist)
1113 c39be742 2021-07-09 op free(sym->name);
1114 c39be742 2021-07-09 op free(sym->val);
1115 c39be742 2021-07-09 op TAILQ_REMOVE(&symhead, sym, entry);
1120 c39be742 2021-07-09 op sym = xcalloc(1, sizeof(*sym));
1121 c39be742 2021-07-09 op sym->name = xstrdup(name);
1122 c39be742 2021-07-09 op sym->val = xstrdup(val);
1123 c39be742 2021-07-09 op sym->used = 0;
1124 c39be742 2021-07-09 op sym->persist = persist;
1126 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
1131 c39be742 2021-07-09 op cmdline_symset(char *s)
1133 c39be742 2021-07-09 op char *sym, *val;
1136 c39be742 2021-07-09 op if ((val = strrchr(s, '=')) == NULL)
1138 c39be742 2021-07-09 op sym = xcalloc(1, val - s + 1);
1139 c39be742 2021-07-09 op memcpy(sym, s, val - s);
1140 c39be742 2021-07-09 op ret = symset(sym, val + 1, 1);
1146 c39be742 2021-07-09 op symget(const char *nam)
1148 c39be742 2021-07-09 op struct sym *sym;
1150 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
1151 c39be742 2021-07-09 op if (strcmp(nam, sym->name) == 0) {
1152 c39be742 2021-07-09 op sym->used = 1;
1153 c39be742 2021-07-09 op return sym->val;
1156 c39be742 2021-07-09 op return NULL;
1160 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
1162 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
1163 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
1164 e17642a7 2021-02-01 op return path;
1168 6abda252 2021-02-06 op check_block_code(int n)
1170 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1171 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
1176 6abda252 2021-02-06 op check_block_fmt(char *fmt)
1180 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
1181 6abda252 2021-02-06 op if (*s != '%')
1183 6abda252 2021-02-06 op switch (*++s) {
1191 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
1199 6abda252 2021-02-06 op check_strip_no(int n)
1202 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
1207 391825e3 2021-07-09 op check_port_num(int n)
1209 391825e3 2021-07-09 op if (n <= 0 || n >= UINT16_MAX)
1210 391825e3 2021-07-09 op yyerror("port number is %s: %d",
1211 391825e3 2021-07-09 op n <= 0 ? "too small" : "too large",
1217 a709ddf5 2021-02-07 op check_prefork_num(int n)
1219 c26f2460 2023-06-08 op if (n <= 0 || n >= PROC_MAX_INSTANCES)
1220 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
1225 49b73ba1 2021-02-10 op advance_loc(void)
1227 b8e64ccd 2021-03-31 op loc = new_location();
1228 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1232 b7967bc1 2021-01-02 op advance_proxy(void)
1234 b7967bc1 2021-01-02 op proxy = new_proxy();
1235 b7967bc1 2021-01-02 op TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1239 b7967bc1 2021-01-02 op parsehp(char *str, char **host, const char **port, const char *def)
1242 b7967bc1 2021-01-02 op const char *errstr;
1244 b7967bc1 2021-01-02 op *host = str;
1246 b7967bc1 2021-01-02 op if ((at = strchr(str, ':')) != NULL) {
1247 b7967bc1 2021-01-02 op *at++ = '\0';
1250 b7967bc1 2021-01-02 op *port = def;
1252 b7967bc1 2021-01-02 op strtonum(*port, 1, UINT16_MAX, &errstr);
1253 b7967bc1 2021-01-02 op if (errstr != NULL)
1254 b7967bc1 2021-01-02 op yyerror("port is %s: %s", errstr, *port);
1258 7b00c890 2022-10-05 op fastcgi_conf(const char *path, const char *port)
1260 8ad1c570 2021-05-09 op struct fcgi *f;
1263 af1dab18 2023-06-09 op TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1264 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
1265 534afd0d 2022-10-05 op ((port == NULL && *f->port == '\0') ||
1266 534afd0d 2022-10-05 op !strcmp(f->port, port)))
1271 5d22294a 2023-06-09 op f = xcalloc(1, sizeof(*f));
1273 5d22294a 2023-06-09 op (void)strlcpy(f->path, path, sizeof(f->path));
1274 5d22294a 2023-06-09 op if (port != NULL)
1275 5d22294a 2023-06-09 op (void)strlcpy(f->port, port, sizeof(f->port));
1276 af1dab18 2023-06-09 op TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1278 5d22294a 2023-06-09 op return f->id;
1282 2025e96d 2022-09-10 op add_param(char *name, char *val)
1284 c92b802b 2021-06-11 op struct envlist *e;
1285 a1ba9650 2023-07-23 op struct envhead *h = &loc->params;
1287 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
1288 534afd0d 2022-10-05 op (void) strlcpy(e->name, name, sizeof(e->name));
1289 534afd0d 2022-10-05 op (void) strlcpy(e->value, val, sizeof(e->value));
1290 edc5ca66 2022-09-10 op TAILQ_INSERT_TAIL(h, e, envs);
1294 534afd0d 2022-10-05 op getservice(const char *n)
1296 534afd0d 2022-10-05 op struct servent *s;
1297 534afd0d 2022-10-05 op const char *errstr;
1298 534afd0d 2022-10-05 op long long llval;
1300 534afd0d 2022-10-05 op llval = strtonum(n, 0, UINT16_MAX, &errstr);
1301 534afd0d 2022-10-05 op if (errstr) {
1302 534afd0d 2022-10-05 op s = getservbyname(n, "tcp");
1303 534afd0d 2022-10-05 op if (s == NULL)
1304 534afd0d 2022-10-05 op s = getservbyname(n, "udp");
1305 534afd0d 2022-10-05 op if (s == NULL)
1306 534afd0d 2022-10-05 op return (-1);
1307 534afd0d 2022-10-05 op return (ntohs(s->s_port));
1310 534afd0d 2022-10-05 op return ((unsigned short)llval);
1314 509d0509 2023-06-23 op add_to_addr_queue(struct addrhead *a, struct addrinfo *ai)
1316 509d0509 2023-06-23 op struct address *addr;
1317 509d0509 2023-06-23 op struct sockaddr_in *sin;
1318 509d0509 2023-06-23 op struct sockaddr_in6 *sin6;
1320 509d0509 2023-06-23 op if (ai->ai_addrlen > sizeof(addr->ss))
1321 509d0509 2023-06-23 op fatalx("ai_addrlen larger than a sockaddr_storage");
1323 509d0509 2023-06-23 op TAILQ_FOREACH(addr, a, addrs) {
1324 509d0509 2023-06-23 op if (addr->ai_flags == ai->ai_flags &&
1325 509d0509 2023-06-23 op addr->ai_family == ai->ai_family &&
1326 509d0509 2023-06-23 op addr->ai_socktype == ai->ai_socktype &&
1327 509d0509 2023-06-23 op addr->ai_protocol == ai->ai_protocol &&
1328 509d0509 2023-06-23 op addr->slen == ai->ai_addrlen &&
1329 509d0509 2023-06-23 op !memcmp(&addr->ss, ai->ai_addr, addr->slen))
1333 509d0509 2023-06-23 op addr = xcalloc(1, sizeof(*addr));
1334 509d0509 2023-06-23 op addr->ai_flags = ai->ai_flags;
1335 509d0509 2023-06-23 op addr->ai_family = ai->ai_family;
1336 509d0509 2023-06-23 op addr->ai_socktype = ai->ai_socktype;
1337 509d0509 2023-06-23 op addr->ai_protocol = ai->ai_protocol;
1338 509d0509 2023-06-23 op addr->slen = ai->ai_addrlen;
1339 509d0509 2023-06-23 op memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
1341 509d0509 2023-06-23 op /* for commodity */
1342 509d0509 2023-06-23 op switch (addr->ai_family) {
1343 509d0509 2023-06-23 op case AF_INET:
1344 509d0509 2023-06-23 op sin = (struct sockaddr_in *)&addr->ss;
1345 509d0509 2023-06-23 op addr->port = ntohs(sin->sin_port);
1347 509d0509 2023-06-23 op case AF_INET6:
1348 509d0509 2023-06-23 op sin6 = (struct sockaddr_in6 *)&addr->ss;
1349 509d0509 2023-06-23 op addr->port = ntohs(sin6->sin6_port);
1352 509d0509 2023-06-23 op fatalx("unknown socket family %d", addr->ai_family);
1355 509d0509 2023-06-23 op addr->sock = -1;
1357 509d0509 2023-06-23 op TAILQ_INSERT_HEAD(a, addr, addrs);
1361 509d0509 2023-06-23 op listen_on(const char *hostname, const char *servname)
1363 509d0509 2023-06-23 op struct addrinfo hints, *res, *res0;
1366 509d0509 2023-06-23 op memset(&hints, 0, sizeof(hints));
1367 509d0509 2023-06-23 op hints.ai_family = AF_UNSPEC;
1368 509d0509 2023-06-23 op hints.ai_socktype = SOCK_STREAM;
1369 509d0509 2023-06-23 op hints.ai_flags = AI_PASSIVE;
1370 509d0509 2023-06-23 op error = getaddrinfo(hostname, servname, &hints, &res0);
1371 509d0509 2023-06-23 op if (error) {
1372 509d0509 2023-06-23 op yyerror("listen on \"%s\" port %s: %s", hostname, servname,
1373 509d0509 2023-06-23 op gai_strerror(errno));
1377 509d0509 2023-06-23 op for (res = res0; res; res = res->ai_next) {
1378 509d0509 2023-06-23 op add_to_addr_queue(&host->addrs, res);
1379 509d0509 2023-06-23 op add_to_addr_queue(&conf->addrs, res);
1382 509d0509 2023-06-23 op freeaddrinfo(res0);