4 15902770 2021-01-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
6 15902770 2021-01-15 op * Permission to use, copy, modify, and distribute this software for any
7 15902770 2021-01-15 op * purpose with or without fee is hereby granted, provided that the above
8 15902770 2021-01-15 op * copyright notice and this permission notice appear in all copies.
10 15902770 2021-01-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 15902770 2021-01-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 15902770 2021-01-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 15902770 2021-01-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 15902770 2021-01-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 15902770 2021-01-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 15902770 2021-01-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 74f0778b 2021-06-16 op #include <ctype.h>
20 002a84a1 2021-02-10 op #include <errno.h>
21 6abda252 2021-02-06 op #include <stdarg.h>
22 15902770 2021-01-15 op #include <stdio.h>
23 74f0778b 2021-06-16 op #include <stdlib.h>
24 32693ee6 2021-01-28 op #include <string.h>
26 15902770 2021-01-15 op #include "gmid.h"
30 ef129b08 2021-06-16 op typedef struct {
38 ef129b08 2021-06-16 op #define YYSTYPE yystype
41 15902770 2021-01-15 op * #define YYDEBUG 1
42 15902770 2021-01-15 op * int yydebug = 1;
46 3b21cca3 2021-06-29 op * The idea behind this implementation of macros is from rad/parse.y
48 3b21cca3 2021-06-29 op TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
50 3b21cca3 2021-06-29 op TAILQ_ENTRY(sym) entry;
57 ca21e100 2021-02-04 op struct vhost *host;
58 ca21e100 2021-02-04 op struct location *loc;
60 74f0778b 2021-06-16 op static int goterror;
62 b8e64ccd 2021-03-31 op static struct vhost *new_vhost(void);
63 b8e64ccd 2021-03-31 op static struct location *new_location(void);
65 6abda252 2021-02-06 op void yyerror(const char*, ...);
66 74f0778b 2021-06-16 op static int yylex(void);
67 13ed2fb6 2021-01-27 op int parse_portno(const char*);
68 13ed2fb6 2021-01-27 op void parse_conf(const char*);
69 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
70 6abda252 2021-02-06 op int check_block_code(int);
71 6abda252 2021-02-06 op char *check_block_fmt(char*);
72 6abda252 2021-02-06 op int check_strip_no(int);
73 a709ddf5 2021-02-07 op int check_prefork_num(int);
74 49b73ba1 2021-02-10 op void advance_loc(void);
75 c705ecb1 2021-05-03 op void only_once(const void*, const char*);
76 8ad1c570 2021-05-09 op void only_oncei(int, const char*);
77 8ad1c570 2021-05-09 op int fastcgi_conf(char *, char *, char *);
78 c92b802b 2021-06-11 op void add_param(char *, char *, int);
79 3b21cca3 2021-06-29 op int symset(const char *, const char *, int);
80 3b21cca3 2021-06-29 op char *symget(const char *);
84 15902770 2021-01-15 op /* for bison: */
85 15902770 2021-01-15 op /* %define parse.error verbose */
87 d06d6f4b 2021-04-29 op %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
88 d06d6f4b 2021-04-29 op %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
89 8ad1c570 2021-05-09 op %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
90 c92b802b 2021-06-11 op %token TFASTCGI TSPAWN TPARAM
94 8235a81c 2021-06-29 op %token <v.str> TSTRING
95 8235a81c 2021-06-29 op %token <v.num> TNUM
96 8235a81c 2021-06-29 op %token <v.num> TBOOL
100 3b21cca3 2021-06-29 op conf : vars options vhosts ;
102 3b21cca3 2021-06-29 op vars : /* empty */
106 3b21cca3 2021-06-29 op var : TSTRING '=' TSTRING {
107 3b21cca3 2021-06-29 op char *s = $1;
108 3b21cca3 2021-06-29 op while (*s++) {
109 3b21cca3 2021-06-29 op if (isspace(*s)) {
110 3b21cca3 2021-06-29 op yyerror("macro name cannot contain "
111 3b21cca3 2021-06-29 op "whitespaces");
117 3b21cca3 2021-06-29 op symset($1, $3, 0);
123 15902770 2021-01-15 op options : /* empty */
124 15902770 2021-01-15 op | options option
127 eb59f87e 2021-02-09 op option : TCHROOT TSTRING { conf.chroot = $2; }
128 eb59f87e 2021-02-09 op | TIPV6 TBOOL { conf.ipv6 = $2; }
129 eb59f87e 2021-02-09 op | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
130 15902770 2021-01-15 op | TPORT TNUM { conf.port = $2; }
131 eb59f87e 2021-02-09 op | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
132 5bc3c98e 2021-01-15 op | TPROTOCOLS TSTRING {
133 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
134 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
136 ae08ec7d 2021-01-25 op | TUSER TSTRING { conf.user = $2; }
139 15902770 2021-01-15 op vhosts : /* empty */
140 15902770 2021-01-15 op | vhosts vhost
143 b8e64ccd 2021-03-31 op vhost : TSERVER TSTRING {
144 b8e64ccd 2021-03-31 op host = new_vhost();
145 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
147 b8e64ccd 2021-03-31 op loc = new_location();
148 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
150 b8e64ccd 2021-03-31 op loc->match = xstrdup("*");
151 15902770 2021-01-15 op host->domain = $2;
153 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
154 ef129b08 2021-06-16 op warnx("%s:%d:%d \"%s\" looks like punycode: "
155 415ac7a2 2021-01-28 op "you should use the decoded hostname.",
156 ef129b08 2021-06-16 op config_path, yylval.lineno+1, yylval.colno,
159 b8e64ccd 2021-03-31 op } '{' servopts locations '}' {
161 fdea6aa0 2021-04-30 op if (host->cert == NULL || host->key == NULL)
162 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
164 15902770 2021-01-15 op | error '}' { yyerror("error in server directive"); }
167 15902770 2021-01-15 op servopts : /* empty */
168 15902770 2021-01-15 op | servopts servopt
171 cc8c2901 2021-04-29 op servopt : TALIAS TSTRING {
172 cc8c2901 2021-04-29 op struct alist *a;
174 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
175 cc8c2901 2021-04-29 op a->alias = $2;
176 cc8c2901 2021-04-29 op if (TAILQ_EMPTY(&host->aliases))
177 cc8c2901 2021-04-29 op TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
179 cc8c2901 2021-04-29 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
181 c705ecb1 2021-05-03 op | TCERT TSTRING {
182 c705ecb1 2021-05-03 op only_once(host->cert, "cert");
183 c705ecb1 2021-05-03 op host->cert = ensure_absolute_path($2);
185 15902770 2021-01-15 op | TCGI TSTRING {
186 c705ecb1 2021-05-03 op only_once(host->cgi, "cgi");
187 15902770 2021-01-15 op /* drop the starting '/', if any */
188 709f4c94 2021-02-04 op if (*$2 == '/')
189 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
190 709f4c94 2021-02-04 op host->cgi = $2;
192 e3ddf390 2021-02-06 op | TENTRYPOINT TSTRING {
193 c705ecb1 2021-05-03 op only_once(host->entrypoint, "entrypoint");
194 e3ddf390 2021-02-06 op while (*$2 == '/')
195 e3ddf390 2021-02-06 op memmove($2, $2+1, strlen($2));
196 e3ddf390 2021-02-06 op host->entrypoint = $2;
198 9cc630aa 2021-04-28 op | TENV TSTRING TSTRING {
199 c92b802b 2021-06-11 op add_param($2, $3, 1);
201 c705ecb1 2021-05-03 op | TKEY TSTRING {
202 c705ecb1 2021-05-03 op only_once(host->key, "key");
203 c705ecb1 2021-05-03 op host->key = ensure_absolute_path($2);
205 c92b802b 2021-06-11 op | TPARAM TSTRING TSTRING {
206 c92b802b 2021-06-11 op add_param($2, $3, 0);
211 c8b74339 2021-01-24 op locations : /* empty */
212 c8b74339 2021-01-24 op | locations location
215 49b73ba1 2021-02-10 op location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
216 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
217 49b73ba1 2021-02-10 op if (*$3 == '/')
218 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
219 49b73ba1 2021-02-10 op loc->match = $3;
224 c8b74339 2021-01-24 op locopts : /* empty */
225 c8b74339 2021-01-24 op | locopts locopt
228 eb59f87e 2021-02-09 op locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
229 6abda252 2021-02-06 op | TBLOCK TRETURN TNUM TSTRING {
230 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
231 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
232 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
234 6abda252 2021-02-06 op | TBLOCK TRETURN TNUM {
235 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
236 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
237 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
238 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
239 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
242 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
243 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
244 6abda252 2021-02-06 op loc->block_code = 40;
246 eb59f87e 2021-02-09 op | TDEFAULT TTYPE TSTRING {
247 c705ecb1 2021-05-03 op only_once(loc->default_mime, "default type");
248 eb59f87e 2021-02-09 op loc->default_mime = $3;
250 0d047efc 2021-05-24 op | TFASTCGI fastcgi
251 eb59f87e 2021-02-09 op | TINDEX TSTRING {
252 c705ecb1 2021-05-03 op only_once(loc->index, "index");
253 eb59f87e 2021-02-09 op loc->index = $2;
255 eb59f87e 2021-02-09 op | TLANG TSTRING {
256 c705ecb1 2021-05-03 op only_once(loc->lang, "lang");
257 eb59f87e 2021-02-09 op loc->lang = $2;
259 793835cb 2021-02-23 op | TLOG TBOOL { loc->disable_log = !$2; }
260 02be96c6 2021-02-09 op | TREQUIRE TCLIENT TCA TSTRING {
261 c705ecb1 2021-05-03 op only_once(loc->reqca, "require client ca");
262 adbe6a64 2021-04-30 op ensure_absolute_path($4);
263 02be96c6 2021-02-09 op if ((loc->reqca = load_ca($4)) == NULL)
264 02be96c6 2021-02-09 op yyerror("couldn't load ca cert: %s", $4);
267 fdea6aa0 2021-04-30 op | TROOT TSTRING {
268 c705ecb1 2021-05-03 op only_once(loc->dir, "root");
269 fdea6aa0 2021-04-30 op loc->dir = ensure_absolute_path($2);
271 eb59f87e 2021-02-09 op | TSTRIP TNUM { loc->strip = check_strip_no($2); }
274 0d047efc 2021-05-24 op fastcgi : TSPAWN TSTRING {
275 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
276 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf(NULL, NULL, $2);
279 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
280 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($1, NULL, NULL);
282 0d047efc 2021-05-24 op | TTCP TSTRING TNUM {
284 0d047efc 2021-05-24 op if (asprintf(&c, "%d", $3) == -1)
285 0d047efc 2021-05-24 op err(1, "asprintf");
286 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
287 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, c, NULL);
289 0d047efc 2021-05-24 op | TTCP TSTRING {
290 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
291 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
293 0d047efc 2021-05-24 op | TTCP TSTRING TSTRING {
294 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
295 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, $3, NULL);
301 b8e64ccd 2021-03-31 op static struct vhost *
302 b8e64ccd 2021-03-31 op new_vhost(void)
304 b8e64ccd 2021-03-31 op return xcalloc(1, sizeof(struct vhost));
307 b8e64ccd 2021-03-31 op static struct location *
308 b8e64ccd 2021-03-31 op new_location(void)
310 fdea6aa0 2021-04-30 op struct location *l;
312 fdea6aa0 2021-04-30 op l = xcalloc(1, sizeof(*l));
313 fdea6aa0 2021-04-30 op l->dirfd = -1;
314 8ad1c570 2021-05-09 op l->fcgi = -1;
319 6abda252 2021-02-06 op yyerror(const char *msg, ...)
323 13ed2fb6 2021-01-27 op goterror = 1;
325 6abda252 2021-02-06 op va_start(ap, msg);
326 ef129b08 2021-06-16 op fprintf(stderr, "%s:%d: ", config_path, yylval.lineno);
327 6abda252 2021-02-06 op vfprintf(stderr, msg, ap);
328 a1373913 2021-02-07 op fprintf(stderr, "\n");
332 74f0778b 2021-06-16 op static struct keyword {
333 74f0778b 2021-06-16 op const char *word;
335 74f0778b 2021-06-16 op } keywords[] = {
336 74f0778b 2021-06-16 op {"alias", TALIAS},
337 74f0778b 2021-06-16 op {"auto", TAUTO},
338 74f0778b 2021-06-16 op {"block", TBLOCK},
340 74f0778b 2021-06-16 op {"cert", TCERT},
341 74f0778b 2021-06-16 op {"cgi", TCGI},
342 74f0778b 2021-06-16 op {"chroot", TCHROOT},
343 74f0778b 2021-06-16 op {"client", TCLIENT},
344 74f0778b 2021-06-16 op {"default", TDEFAULT},
345 74f0778b 2021-06-16 op {"entrypoint", TENTRYPOINT},
346 74f0778b 2021-06-16 op {"env", TENV},
347 74f0778b 2021-06-16 op {"fastcgi", TFASTCGI},
348 74f0778b 2021-06-16 op {"index", TINDEX},
349 74f0778b 2021-06-16 op {"ipv6", TIPV6},
350 74f0778b 2021-06-16 op {"key", TKEY},
351 74f0778b 2021-06-16 op {"lang", TLANG},
352 74f0778b 2021-06-16 op {"location", TLOCATION},
353 74f0778b 2021-06-16 op {"log", TLOG},
354 74f0778b 2021-06-16 op {"mime", TMIME},
355 74f0778b 2021-06-16 op {"param", TPARAM},
356 74f0778b 2021-06-16 op {"port", TPORT},
357 74f0778b 2021-06-16 op {"prefork", TPREFORK},
358 74f0778b 2021-06-16 op {"protocols", TPROTOCOLS},
359 74f0778b 2021-06-16 op {"require", TREQUIRE},
360 74f0778b 2021-06-16 op {"return", TRETURN},
361 74f0778b 2021-06-16 op {"root", TROOT},
362 74f0778b 2021-06-16 op {"server", TSERVER},
363 74f0778b 2021-06-16 op {"spawn", TSPAWN},
364 74f0778b 2021-06-16 op {"strip", TSTRIP},
365 74f0778b 2021-06-16 op {"tcp", TTCP},
366 74f0778b 2021-06-16 op {"type", TTYPE},
367 74f0778b 2021-06-16 op {"user", TUSER},
371 74f0778b 2021-06-16 op * Taken an adapted from doas' parse.y
376 3b21cca3 2021-06-29 op char buf[8096], *ebuf, *p, *str, *v, *val;
377 74f0778b 2021-06-16 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
378 3b21cca3 2021-06-29 op size_t i, len;
381 74f0778b 2021-06-16 op ebuf = buf + sizeof(buf);
384 74f0778b 2021-06-16 op /* skip whitespace first */
385 74f0778b 2021-06-16 op for (c = getc(yyfp); isspace(c); c = getc(yyfp)) {
386 ef129b08 2021-06-16 op yylval.colno++;
387 74f0778b 2021-06-16 op if (c == '\n') {
388 ef129b08 2021-06-16 op yylval.lineno++;
389 ef129b08 2021-06-16 op yylval.colno = 0;
393 74f0778b 2021-06-16 op /* check for special one-character constructions */
399 74f0778b 2021-06-16 op /* skip comments; NUL is allowed; no continuation */
400 74f0778b 2021-06-16 op while ((c = getc(yyfp)) != '\n')
401 74f0778b 2021-06-16 op if (c == EOF)
403 ef129b08 2021-06-16 op yylval.colno = 0;
404 ef129b08 2021-06-16 op yylval.lineno++;
413 74f0778b 2021-06-16 op /* parsing next word */
414 ef129b08 2021-06-16 op for (;; c = getc(yyfp), yylval.colno++) {
417 74f0778b 2021-06-16 op yyerror("unallowed character NULL in column %d",
418 ef129b08 2021-06-16 op yylval.colno+1);
422 74f0778b 2021-06-16 op escape = !escape;
427 3b21cca3 2021-06-29 op /* expand macros in-place */
429 3b21cca3 2021-06-29 op if (!escape) {
432 3b21cca3 2021-06-29 op if ((c = getc(yyfp)) == EOF) {
433 3b21cca3 2021-06-29 op yyerror("EOF during macro expansion");
436 3b21cca3 2021-06-29 op if (p + 1 >= ebuf - 1) {
437 3b21cca3 2021-06-29 op yyerror("string too long");
440 3b21cca3 2021-06-29 op if (isalnum(c) || c == '_') {
445 3b21cca3 2021-06-29 op ungetc(c, yyfp);
449 3b21cca3 2021-06-29 op if ((val = symget(p)) == NULL) {
450 3b21cca3 2021-06-29 op yyerror("macro '%s' not defined", v);
453 3b21cca3 2021-06-29 op len = strlen(val);
454 3b21cca3 2021-06-29 op if (p + len >= ebuf - 1) {
455 3b21cca3 2021-06-29 op yyerror("after macro-expansion, "
456 3b21cca3 2021-06-29 op "string too long");
460 3b21cca3 2021-06-29 op strlcat(p, val, ebuf - p);
467 74f0778b 2021-06-16 op yyerror("unterminated quotes in column %d",
468 ef129b08 2021-06-16 op yylval.colno+1);
469 74f0778b 2021-06-16 op if (escape) {
472 ef129b08 2021-06-16 op yylval.colno = 0;
473 ef129b08 2021-06-16 op yylval.lineno++;
478 74f0778b 2021-06-16 op yyerror("unterminated escape in column %d",
479 ef129b08 2021-06-16 op yylval.colno);
481 74f0778b 2021-06-16 op yyerror("unterminated quotes in column %d",
489 74f0778b 2021-06-16 op if (!escape && !quotes)
493 74f0778b 2021-06-16 op if (!escape) {
494 74f0778b 2021-06-16 op quotes = !quotes;
495 74f0778b 2021-06-16 op if (quotes) {
497 ef129b08 2021-06-16 op qpos = yylval.colno;
503 74f0778b 2021-06-16 op if (p == ebuf) {
504 74f0778b 2021-06-16 op yyerror("line too long");
512 74f0778b 2021-06-16 op if (c != EOF)
513 74f0778b 2021-06-16 op ungetc(c, yyfp);
514 74f0778b 2021-06-16 op if (p == buf) {
516 74f0778b 2021-06-16 op * There could be a number of reason for empty buffer,
517 74f0778b 2021-06-16 op * and we handle all of them here, to avoid cluttering
518 74f0778b 2021-06-16 op * the main loop.
520 74f0778b 2021-06-16 op if (c == EOF)
522 74f0778b 2021-06-16 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
525 74f0778b 2021-06-16 op if (!nonkw) {
526 74f0778b 2021-06-16 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); ++i) {
527 74f0778b 2021-06-16 op if (!strcmp(buf, keywords[i].word))
528 74f0778b 2021-06-16 op return keywords[i].token;
532 74f0778b 2021-06-16 op if (!nonkw && (c == '-' || isdigit(c))) {
533 8235a81c 2021-06-29 op yylval.v.num = parse_portno(buf);
536 74f0778b 2021-06-16 op if (!nonkw && !strcmp(buf, "on")) {
537 8235a81c 2021-06-29 op yylval.v.num = 1;
538 74f0778b 2021-06-16 op return TBOOL;
540 74f0778b 2021-06-16 op if (!nonkw && !strcmp(buf, "off")) {
541 8235a81c 2021-06-29 op yylval.v.num = 0;
542 74f0778b 2021-06-16 op return TBOOL;
544 74f0778b 2021-06-16 op if ((str = strdup(buf)) == NULL)
545 74f0778b 2021-06-16 op err(1, "%s", __func__);
546 8235a81c 2021-06-29 op yylval.v.str = str;
547 74f0778b 2021-06-16 op return TSTRING;
550 74f0778b 2021-06-16 op if (ferror(yyfp))
551 74f0778b 2021-06-16 op yyerror("input error reading config");
556 13ed2fb6 2021-01-27 op parse_portno(const char *p)
558 13ed2fb6 2021-01-27 op const char *errstr;
561 13ed2fb6 2021-01-27 op n = strtonum(p, 0, UINT16_MAX, &errstr);
562 13ed2fb6 2021-01-27 op if (errstr != NULL)
563 2d34f732 2021-02-10 op yyerror("port number is %s: %s", errstr, p);
568 13ed2fb6 2021-01-27 op parse_conf(const char *path)
570 3b21cca3 2021-06-29 op struct sym *sym, *next;
572 13ed2fb6 2021-01-27 op config_path = path;
573 74f0778b 2021-06-16 op if ((yyfp = fopen(path, "r")) == NULL)
574 48b69cb2 2021-04-28 op err(1, "cannot open config: %s", path);
576 74f0778b 2021-06-16 op fclose(yyfp);
578 13ed2fb6 2021-01-27 op if (goterror)
581 b8e64ccd 2021-03-31 op if (TAILQ_FIRST(&hosts)->domain == NULL)
582 48b69cb2 2021-04-28 op errx(1, "no vhost defined in %s", path);
584 3b21cca3 2021-06-29 op /* free unused macros */
585 3b21cca3 2021-06-29 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
586 3b21cca3 2021-06-29 op /* TODO: warn if !sym->used */
587 3b21cca3 2021-06-29 op if (!sym->persist) {
588 3b21cca3 2021-06-29 op free(sym->name);
589 3b21cca3 2021-06-29 op free(sym->val);
590 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
597 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
599 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
600 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
605 6abda252 2021-02-06 op check_block_code(int n)
607 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
608 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
613 6abda252 2021-02-06 op check_block_fmt(char *fmt)
617 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
618 6abda252 2021-02-06 op if (*s != '%')
620 6abda252 2021-02-06 op switch (*++s) {
628 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
636 6abda252 2021-02-06 op check_strip_no(int n)
639 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
644 a709ddf5 2021-02-07 op check_prefork_num(int n)
646 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
647 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
652 49b73ba1 2021-02-10 op advance_loc(void)
654 b8e64ccd 2021-03-31 op loc = new_location();
655 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
659 c705ecb1 2021-05-03 op only_once(const void *ptr, const char *name)
661 c705ecb1 2021-05-03 op if (ptr != NULL)
662 c705ecb1 2021-05-03 op yyerror("`%s' specified more than once", name);
666 8ad1c570 2021-05-09 op only_oncei(int i, const char *name)
669 8ad1c570 2021-05-09 op yyerror("`%s' specified more than once", name);
673 8ad1c570 2021-05-09 op fastcgi_conf(char *path, char *port, char *prog)
675 8ad1c570 2021-05-09 op struct fcgi *f;
678 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
679 8ad1c570 2021-05-09 op f = &fcgi[i];
681 8ad1c570 2021-05-09 op if (f->path == NULL) {
683 8ad1c570 2021-05-09 op f->path = path;
684 8ad1c570 2021-05-09 op f->port = port;
685 8ad1c570 2021-05-09 op f->prog = prog;
689 8ad1c570 2021-05-09 op /* XXX: what to do with prog? */
690 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
691 8ad1c570 2021-05-09 op ((port == NULL && f->port == NULL) ||
692 8ad1c570 2021-05-09 op !strcmp(f->port, port))) {
699 8ad1c570 2021-05-09 op yyerror("too much `fastcgi' rules defined.");
704 c92b802b 2021-06-11 op add_param(char *name, char *val, int env)
706 c92b802b 2021-06-11 op struct envlist *e;
707 c92b802b 2021-06-11 op struct envhead *h;
710 c92b802b 2021-06-11 op h = &host->env;
712 c92b802b 2021-06-11 op h = &host->params;
714 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
715 c92b802b 2021-06-11 op e->name = name;
716 c92b802b 2021-06-11 op e->value = val;
717 c92b802b 2021-06-11 op if (TAILQ_EMPTY(h))
718 c92b802b 2021-06-11 op TAILQ_INSERT_HEAD(h, e, envs);
720 c92b802b 2021-06-11 op TAILQ_INSERT_TAIL(h, e, envs);
724 3b21cca3 2021-06-29 op symset(const char *name, const char *val, int persist)
726 3b21cca3 2021-06-29 op struct sym *sym;
728 3b21cca3 2021-06-29 op TAILQ_FOREACH(sym, &symhead, entry) {
729 3b21cca3 2021-06-29 op if (!strcmp(name, sym->name))
733 3b21cca3 2021-06-29 op if (sym != NULL) {
734 3b21cca3 2021-06-29 op if (sym->persist)
737 3b21cca3 2021-06-29 op free(sym->name);
738 3b21cca3 2021-06-29 op free(sym->val);
739 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
744 3b21cca3 2021-06-29 op sym = xcalloc(1, sizeof(*sym));
745 3b21cca3 2021-06-29 op sym->name = xstrdup(name);
746 3b21cca3 2021-06-29 op sym->val = xstrdup(val);
747 3b21cca3 2021-06-29 op sym->used = 0;
748 3b21cca3 2021-06-29 op sym->persist = persist;
750 3b21cca3 2021-06-29 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
755 3b21cca3 2021-06-29 op cmdline_symset(char *s)
757 3b21cca3 2021-06-29 op char *sym, *val;
760 3b21cca3 2021-06-29 op if ((val = strrchr(s, '=')) == NULL)
762 3b21cca3 2021-06-29 op sym = xcalloc(1, val - s + 1);
763 3b21cca3 2021-06-29 op memcpy(sym, s, val - s);
764 3b21cca3 2021-06-29 op ret = symset(sym, val + 1, 1);
770 3b21cca3 2021-06-29 op symget(const char *name)
772 3b21cca3 2021-06-29 op struct sym *sym;
774 3b21cca3 2021-06-29 op TAILQ_FOREACH(sym, &symhead, entry) {
775 3b21cca3 2021-06-29 op if (!strcmp(name, sym->name)) {
776 3b21cca3 2021-06-29 op sym->used = 1;
777 3b21cca3 2021-06-29 op return sym->val;