4 15902770 2021-01-15 op * Copyright (c) 2021 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 74f0778b 2021-06-16 op #include <ctype.h>
27 002a84a1 2021-02-10 op #include <errno.h>
28 6abda252 2021-02-06 op #include <stdarg.h>
29 15902770 2021-01-15 op #include <stdio.h>
30 74f0778b 2021-06-16 op #include <stdlib.h>
31 32693ee6 2021-01-28 op #include <string.h>
33 15902770 2021-01-15 op #include "gmid.h"
35 c39be742 2021-07-09 op TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
36 c39be742 2021-07-09 op static struct file {
37 c39be742 2021-07-09 op TAILQ_ENTRY(file) entry;
40 c39be742 2021-07-09 op size_t ungetpos;
41 c39be742 2021-07-09 op size_t ungetsize;
42 c39be742 2021-07-09 op u_char *ungetbuf;
43 c39be742 2021-07-09 op int eof_reached;
46 c39be742 2021-07-09 op } *file, *topfile;
48 c39be742 2021-07-09 op struct file *pushfile(const char *, int);
49 c39be742 2021-07-09 op int popfile(void);
50 c39be742 2021-07-09 op int yyparse(void);
51 c39be742 2021-07-09 op int yylex(void);
52 c39be742 2021-07-09 op void yyerror(const char *, ...)
53 c39be742 2021-07-09 op __attribute__((__format__ (printf, 1, 2)))
54 c39be742 2021-07-09 op __attribute__((__nonnull__ (1)));
55 c39be742 2021-07-09 op int kw_cmp(const void *, const void *);
56 c39be742 2021-07-09 op int lookup(char *);
57 c39be742 2021-07-09 op int igetc(void);
58 c39be742 2021-07-09 op int lgetc(int);
59 c39be742 2021-07-09 op void lungetc(int);
60 c39be742 2021-07-09 op int findeol(void);
63 15902770 2021-01-15 op * #define YYDEBUG 1
64 15902770 2021-01-15 op * int yydebug = 1;
67 3b21cca3 2021-06-29 op TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
69 3b21cca3 2021-06-29 op TAILQ_ENTRY(sym) entry;
76 c39be742 2021-07-09 op int symset(const char *, const char *, int);
77 c39be742 2021-07-09 op char *symget(const char *);
79 c39be742 2021-07-09 op struct vhost *new_vhost(void);
80 c39be742 2021-07-09 op struct location *new_location(void);
81 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
82 6abda252 2021-02-06 op int check_block_code(int);
83 6abda252 2021-02-06 op char *check_block_fmt(char*);
84 6abda252 2021-02-06 op int check_strip_no(int);
85 391825e3 2021-07-09 op int check_port_num(int);
86 a709ddf5 2021-02-07 op int check_prefork_num(int);
87 49b73ba1 2021-02-10 op void advance_loc(void);
88 c705ecb1 2021-05-03 op void only_once(const void*, const char*);
89 8ad1c570 2021-05-09 op void only_oncei(int, const char*);
90 8ad1c570 2021-05-09 op int fastcgi_conf(char *, char *, char *);
91 c92b802b 2021-06-11 op void add_param(char *, char *, int);
93 c39be742 2021-07-09 op static struct vhost *host;
94 c39be742 2021-07-09 op static struct location *loc;
95 c39be742 2021-07-09 op static int errors;
97 c39be742 2021-07-09 op typedef struct {
107 15902770 2021-01-15 op /* for bison: */
108 15902770 2021-01-15 op /* %define parse.error verbose */
110 7252049d 2021-06-29 op %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
111 7252049d 2021-06-29 op %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
112 7252049d 2021-06-29 op %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
113 c39be742 2021-07-09 op %token TFASTCGI TSPAWN TPARAM TMAP TTOEXT INCLUDE TON TOFF
117 c39be742 2021-07-09 op %token <v.string> STRING
118 c39be742 2021-07-09 op %token <v.number> NUM
120 c39be742 2021-07-09 op %type <v.number> bool
121 c39be742 2021-07-09 op %type <v.string> string
125 6b86655a 2021-06-29 op conf : /* empty */
126 c39be742 2021-07-09 op | conf include '\n'
128 c39be742 2021-07-09 op | conf varset '\n'
129 c39be742 2021-07-09 op | conf option '\n'
130 c39be742 2021-07-09 op | conf vhost '\n'
131 c39be742 2021-07-09 op | conf error '\n' { file->errors++; }
134 c39be742 2021-07-09 op include : INCLUDE STRING {
135 c39be742 2021-07-09 op struct file *nfile;
137 c39be742 2021-07-09 op if ((nfile = pushfile($2, 0)) == NULL) {
138 c39be742 2021-07-09 op yyerror("failed to include file %s", $2);
144 c39be742 2021-07-09 op file = nfile;
145 c39be742 2021-07-09 op lungetc('\n');
149 c39be742 2021-07-09 op bool : TON { $$ = 1; }
150 c39be742 2021-07-09 op | TOFF { $$ = 0; }
153 c39be742 2021-07-09 op string : string STRING {
154 98f52178 2021-06-29 op if (asprintf(&$$, "%s%s", $1, $2) == -1) {
157 98f52178 2021-06-29 op yyerror("string: asprintf: %s", strerror(errno));
166 c39be742 2021-07-09 op varset : STRING '=' string {
167 3b21cca3 2021-06-29 op char *s = $1;
168 3b21cca3 2021-06-29 op while (*s++) {
169 c39be742 2021-07-09 op if (isspace((unsigned char)*s)) {
170 3b21cca3 2021-06-29 op yyerror("macro name cannot contain "
171 3b21cca3 2021-06-29 op "whitespaces");
177 3b21cca3 2021-06-29 op symset($1, $3, 0);
183 98f52178 2021-06-29 op option : TCHROOT string { conf.chroot = $2; }
184 c39be742 2021-07-09 op | TIPV6 bool { conf.ipv6 = $2; }
185 c39be742 2021-07-09 op | TMIME STRING string {
186 d19951cf 2021-07-09 op fprintf(stderr, "%s:%d: `mime MIME EXT' is deprecated and "
187 d19951cf 2021-07-09 op "will be removed in a future version, "
188 e3b2a0f8 2021-07-09 op "please use the new syntax: `map MIME to-ext EXT'\n",
189 d19951cf 2021-07-09 op config_path, yylval.lineno+1);
190 d19951cf 2021-07-09 op add_mime(&conf.mime, $2, $3);
192 ff954a3e 2021-07-09 op | TMAP string TTOEXT string { add_mime(&conf.mime, $2, $4); }
193 391825e3 2021-07-09 op | TPORT NUM { conf.port = check_port_num($2); }
194 c39be742 2021-07-09 op | TPREFORK NUM { conf.prefork = check_prefork_num($2); }
195 98f52178 2021-06-29 op | TPROTOCOLS string {
196 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
197 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
199 98f52178 2021-06-29 op | TUSER string { conf.user = $2; }
202 98f52178 2021-06-29 op vhost : TSERVER string {
203 b8e64ccd 2021-03-31 op host = new_vhost();
204 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
206 b8e64ccd 2021-03-31 op loc = new_location();
207 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
209 b8e64ccd 2021-03-31 op loc->match = xstrdup("*");
210 15902770 2021-01-15 op host->domain = $2;
212 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
213 c39be742 2021-07-09 op warnx("%s:%d \"%s\" looks like punycode: "
214 415ac7a2 2021-01-28 op "you should use the decoded hostname.",
215 c39be742 2021-07-09 op config_path, yylval.lineno+1, $2);
217 c39be742 2021-07-09 op } '{' optnl servopts locations '}' {
218 fdea6aa0 2021-04-30 op if (host->cert == NULL || host->key == NULL)
219 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
221 15902770 2021-01-15 op | error '}' { yyerror("error in server directive"); }
224 15902770 2021-01-15 op servopts : /* empty */
225 c39be742 2021-07-09 op | servopts servopt optnl
228 98f52178 2021-06-29 op servopt : TALIAS string {
229 cc8c2901 2021-04-29 op struct alist *a;
231 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
232 cc8c2901 2021-04-29 op a->alias = $2;
233 cc8c2901 2021-04-29 op if (TAILQ_EMPTY(&host->aliases))
234 cc8c2901 2021-04-29 op TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
236 cc8c2901 2021-04-29 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
238 98f52178 2021-06-29 op | TCERT string {
239 c705ecb1 2021-05-03 op only_once(host->cert, "cert");
240 c705ecb1 2021-05-03 op host->cert = ensure_absolute_path($2);
242 98f52178 2021-06-29 op | TCGI string {
243 c705ecb1 2021-05-03 op only_once(host->cgi, "cgi");
244 15902770 2021-01-15 op /* drop the starting '/', if any */
245 709f4c94 2021-02-04 op if (*$2 == '/')
246 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
247 709f4c94 2021-02-04 op host->cgi = $2;
249 98f52178 2021-06-29 op | TENTRYPOINT string {
250 c705ecb1 2021-05-03 op only_once(host->entrypoint, "entrypoint");
251 e3ddf390 2021-02-06 op while (*$2 == '/')
252 e3ddf390 2021-02-06 op memmove($2, $2+1, strlen($2));
253 e3ddf390 2021-02-06 op host->entrypoint = $2;
255 efacb859 2021-07-09 op | TENV string '=' string {
256 762b9b99 2021-07-09 op add_param($2, $4, 1);
258 98f52178 2021-06-29 op | TKEY string {
259 c705ecb1 2021-05-03 op only_once(host->key, "key");
260 c705ecb1 2021-05-03 op host->key = ensure_absolute_path($2);
262 efacb859 2021-07-09 op | TPARAM string '=' string {
263 762b9b99 2021-07-09 op add_param($2, $4, 0);
268 c8b74339 2021-01-24 op locations : /* empty */
269 c39be742 2021-07-09 op | locations location optnl
272 c39be742 2021-07-09 op location : TLOCATION { advance_loc(); } string '{' optnl locopts '}' {
273 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
274 49b73ba1 2021-02-10 op if (*$3 == '/')
275 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
276 49b73ba1 2021-02-10 op loc->match = $3;
281 c8b74339 2021-01-24 op locopts : /* empty */
282 c39be742 2021-07-09 op | locopts locopt optnl
285 c39be742 2021-07-09 op locopt : TAUTO TINDEX bool { loc->auto_index = $3 ? 1 : -1; }
286 c39be742 2021-07-09 op | TBLOCK TRETURN NUM string {
287 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
288 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
289 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
291 c39be742 2021-07-09 op | TBLOCK TRETURN NUM {
292 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
293 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
294 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
295 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
296 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
299 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
300 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
301 6abda252 2021-02-06 op loc->block_code = 40;
303 98f52178 2021-06-29 op | TDEFAULT TTYPE string {
304 c705ecb1 2021-05-03 op only_once(loc->default_mime, "default type");
305 eb59f87e 2021-02-09 op loc->default_mime = $3;
307 0d047efc 2021-05-24 op | TFASTCGI fastcgi
308 98f52178 2021-06-29 op | TINDEX string {
309 c705ecb1 2021-05-03 op only_once(loc->index, "index");
310 eb59f87e 2021-02-09 op loc->index = $2;
312 98f52178 2021-06-29 op | TLANG string {
313 c705ecb1 2021-05-03 op only_once(loc->lang, "lang");
314 eb59f87e 2021-02-09 op loc->lang = $2;
316 c39be742 2021-07-09 op | TLOG bool { loc->disable_log = !$2; }
317 98f52178 2021-06-29 op | TREQUIRE TCLIENT TCA string {
318 c705ecb1 2021-05-03 op only_once(loc->reqca, "require client ca");
319 adbe6a64 2021-04-30 op ensure_absolute_path($4);
320 02be96c6 2021-02-09 op if ((loc->reqca = load_ca($4)) == NULL)
321 02be96c6 2021-02-09 op yyerror("couldn't load ca cert: %s", $4);
324 98f52178 2021-06-29 op | TROOT string {
325 c705ecb1 2021-05-03 op only_once(loc->dir, "root");
326 fdea6aa0 2021-04-30 op loc->dir = ensure_absolute_path($2);
328 c39be742 2021-07-09 op | TSTRIP NUM { loc->strip = check_strip_no($2); }
331 98f52178 2021-06-29 op fastcgi : TSPAWN string {
332 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
333 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf(NULL, NULL, $2);
336 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
337 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($1, NULL, NULL);
339 c39be742 2021-07-09 op | TTCP string TPORT NUM {
341 762b9b99 2021-07-09 op if (asprintf(&c, "%d", $4) == -1)
342 0d047efc 2021-05-24 op err(1, "asprintf");
343 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
344 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, c, NULL);
346 98f52178 2021-06-29 op | TTCP string {
347 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
348 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
350 762b9b99 2021-07-09 op | TTCP string TPORT string {
351 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
352 762b9b99 2021-07-09 op loc->fcgi = fastcgi_conf($2, $4, NULL);
356 c39be742 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
357 67f49405 2021-07-09 op | ';' optnl /* semicolons too */
363 74f0778b 2021-06-16 op static struct keyword {
364 74f0778b 2021-06-16 op const char *word;
366 74f0778b 2021-06-16 op } keywords[] = {
367 d93c8191 2021-07-09 op /* these MUST be sorted */
368 74f0778b 2021-06-16 op {"alias", TALIAS},
369 74f0778b 2021-06-16 op {"auto", TAUTO},
370 74f0778b 2021-06-16 op {"block", TBLOCK},
372 74f0778b 2021-06-16 op {"cert", TCERT},
373 74f0778b 2021-06-16 op {"cgi", TCGI},
374 74f0778b 2021-06-16 op {"chroot", TCHROOT},
375 74f0778b 2021-06-16 op {"client", TCLIENT},
376 74f0778b 2021-06-16 op {"default", TDEFAULT},
377 74f0778b 2021-06-16 op {"entrypoint", TENTRYPOINT},
378 74f0778b 2021-06-16 op {"env", TENV},
379 74f0778b 2021-06-16 op {"fastcgi", TFASTCGI},
380 74f0778b 2021-06-16 op {"index", TINDEX},
381 74f0778b 2021-06-16 op {"ipv6", TIPV6},
382 74f0778b 2021-06-16 op {"key", TKEY},
383 74f0778b 2021-06-16 op {"lang", TLANG},
384 74f0778b 2021-06-16 op {"location", TLOCATION},
385 74f0778b 2021-06-16 op {"log", TLOG},
386 d19951cf 2021-07-09 op {"map", TMAP},
387 74f0778b 2021-06-16 op {"mime", TMIME},
388 c39be742 2021-07-09 op {"off", TOFF},
390 74f0778b 2021-06-16 op {"param", TPARAM},
391 74f0778b 2021-06-16 op {"port", TPORT},
392 74f0778b 2021-06-16 op {"prefork", TPREFORK},
393 74f0778b 2021-06-16 op {"protocols", TPROTOCOLS},
394 74f0778b 2021-06-16 op {"require", TREQUIRE},
395 74f0778b 2021-06-16 op {"return", TRETURN},
396 74f0778b 2021-06-16 op {"root", TROOT},
397 74f0778b 2021-06-16 op {"server", TSERVER},
398 74f0778b 2021-06-16 op {"spawn", TSPAWN},
399 74f0778b 2021-06-16 op {"strip", TSTRIP},
400 74f0778b 2021-06-16 op {"tcp", TTCP},
401 d19951cf 2021-07-09 op {"to-ext", TTOEXT},
402 74f0778b 2021-06-16 op {"type", TTYPE},
403 74f0778b 2021-06-16 op {"user", TUSER},
407 c39be742 2021-07-09 op yyerror(const char *msg, ...)
411 c39be742 2021-07-09 op file->errors++;
413 c39be742 2021-07-09 op va_start(ap, msg);
414 c39be742 2021-07-09 op fprintf(stderr, "%s:%d: ", config_path, yylval.lineno);
415 c39be742 2021-07-09 op vfprintf(stderr, msg, ap);
416 c39be742 2021-07-09 op fprintf(stderr, "\n");
421 d93c8191 2021-07-09 op kw_cmp(const void *k, const void *e)
423 d93c8191 2021-07-09 op return strcmp(k, ((struct keyword *)e)->word);
427 c39be742 2021-07-09 op lookup(char *s)
429 c39be742 2021-07-09 op const struct keyword *p;
431 c39be742 2021-07-09 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
432 c39be742 2021-07-09 op sizeof(keywords[0]), kw_cmp);
435 c39be742 2021-07-09 op return p->token;
437 c39be742 2021-07-09 op return STRING;
440 c39be742 2021-07-09 op #define START_EXPAND 1
441 c39be742 2021-07-09 op #define DONE_EXPAND 2
443 c39be742 2021-07-09 op static int expanding;
451 c39be742 2021-07-09 op if (file->ungetpos > 0)
452 c39be742 2021-07-09 op c = file->ungetbuf[--file->ungetpos];
454 c39be742 2021-07-09 op c = getc(file->stream);
456 c39be742 2021-07-09 op if (c == START_EXPAND)
457 c39be742 2021-07-09 op expanding = 1;
458 c39be742 2021-07-09 op else if (c == DONE_EXPAND)
459 c39be742 2021-07-09 op expanding = 0;
467 c39be742 2021-07-09 op lgetc(int quotec)
471 c39be742 2021-07-09 op if (quotec) {
472 c39be742 2021-07-09 op if ((c = igetc()) == EOF) {
473 c39be742 2021-07-09 op yyerror("reached end of file while parsing "
474 c39be742 2021-07-09 op "quoted string");
475 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
477 c39be742 2021-07-09 op return quotec;
482 c39be742 2021-07-09 op while ((c = igetc()) == '\\') {
483 c39be742 2021-07-09 op next = igetc();
484 c39be742 2021-07-09 op if (next != '\n') {
488 c39be742 2021-07-09 op yylval.lineno = file->lineno;
489 c39be742 2021-07-09 op file->lineno++;
492 c39be742 2021-07-09 op if (c == EOF) {
494 c39be742 2021-07-09 op * Fake EOL when hit EOF for the first time. This gets line
495 c39be742 2021-07-09 op * count right if last line in included file is syntactically
496 c39be742 2021-07-09 op * invalid and has no newline.
498 c39be742 2021-07-09 op if (file->eof_reached == 0) {
499 c39be742 2021-07-09 op file->eof_reached = 1;
502 c39be742 2021-07-09 op while (c == EOF) {
503 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
512 c39be742 2021-07-09 op lungetc(int c)
514 c39be742 2021-07-09 op if (c == EOF)
517 c39be742 2021-07-09 op if (file->ungetpos >= file->ungetsize) {
518 c39be742 2021-07-09 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
519 c39be742 2021-07-09 op if (p == NULL)
520 c39be742 2021-07-09 op err(1, "lungetc");
521 c39be742 2021-07-09 op file->ungetbuf = p;
522 c39be742 2021-07-09 op file->ungetsize *= 2;
524 c39be742 2021-07-09 op file->ungetbuf[file->ungetpos++] = c;
528 c39be742 2021-07-09 op findeol(void)
532 c39be742 2021-07-09 op /* Skip to either EOF or the first real EOL. */
534 c39be742 2021-07-09 op c = lgetc(0);
535 c39be742 2021-07-09 op if (c == '\n') {
536 c39be742 2021-07-09 op file->lineno++;
539 c39be742 2021-07-09 op if (c == EOF)
542 c39be742 2021-07-09 op return ERROR;
548 1bd706dc 2021-07-09 op char buf[8096];
549 1bd706dc 2021-07-09 op char *p, *val;
550 1bd706dc 2021-07-09 op int quotec, next, c;
555 c39be742 2021-07-09 op while ((c = lgetc(0)) == ' ' || c == '\t')
556 c39be742 2021-07-09 op ; /* nothing */
558 c39be742 2021-07-09 op yylval.lineno = file->lineno;
559 c39be742 2021-07-09 op if (c == '#')
560 c39be742 2021-07-09 op while ((c = lgetc(0)) != '\n' && c != EOF)
561 c39be742 2021-07-09 op ; /* nothing */
562 c39be742 2021-07-09 op if (c == '$' && !expanding) {
564 c39be742 2021-07-09 op if ((c = lgetc(0)) == EOF)
566 67f49405 2021-07-09 op if (p + 1 >= buf + sizeof(buf) -1) {
567 67f49405 2021-07-09 op yyerror("string too long");
568 67f49405 2021-07-09 op return findeol();
570 67f49405 2021-07-09 op if (isalnum(c) || c == '_') {
578 67f49405 2021-07-09 op val = symget(buf);
579 67f49405 2021-07-09 op if (val == NULL) {
580 67f49405 2021-07-09 op yyerror("macro `%s' not defined", buf);
581 67f49405 2021-07-09 op return findeol();
583 67f49405 2021-07-09 op yylval.v.string = xstrdup(val);
584 67f49405 2021-07-09 op return STRING;
586 67f49405 2021-07-09 op if (c == '@' && !expanding) {
588 67f49405 2021-07-09 op if ((c = lgetc(0)) == EOF)
591 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
592 c39be742 2021-07-09 op yyerror("string too long");
593 c39be742 2021-07-09 op return findeol();
595 c39be742 2021-07-09 op if (isalnum(c) || c == '_') {
603 c39be742 2021-07-09 op val = symget(buf);
604 c39be742 2021-07-09 op if (val == NULL) {
605 c39be742 2021-07-09 op yyerror("macro '%s' not defined", buf);
606 c39be742 2021-07-09 op return findeol();
608 c39be742 2021-07-09 op p = val + strlen(val) - 1;
609 c39be742 2021-07-09 op lungetc(DONE_EXPAND);
610 c39be742 2021-07-09 op while (p >= val) {
614 c39be742 2021-07-09 op lungetc(START_EXPAND);
623 c39be742 2021-07-09 op if ((c = lgetc(quotec)) == EOF)
625 c39be742 2021-07-09 op if (c == '\n') {
626 c39be742 2021-07-09 op file->lineno++;
628 c39be742 2021-07-09 op } else if (c == '\\') {
629 c39be742 2021-07-09 op if ((next = lgetc(quotec)) == EOF)
631 c39be742 2021-07-09 op if (next == quotec || next == ' ' ||
632 c39be742 2021-07-09 op next == '\t')
634 c39be742 2021-07-09 op else if (next == '\n') {
635 c39be742 2021-07-09 op file->lineno++;
638 c39be742 2021-07-09 op lungetc(next);
639 c39be742 2021-07-09 op } else if (c == quotec) {
642 c39be742 2021-07-09 op } else if (c == '\0') {
643 c39be742 2021-07-09 op yyerror("syntax error");
644 c39be742 2021-07-09 op return findeol();
646 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
647 c39be742 2021-07-09 op yyerror("string too long");
648 c39be742 2021-07-09 op return findeol();
652 c39be742 2021-07-09 op yylval.v.string = strdup(buf);
653 c39be742 2021-07-09 op if (yylval.v.string == NULL)
654 c39be742 2021-07-09 op err(1, "yylex: strdup");
655 c39be742 2021-07-09 op return STRING;
658 c39be742 2021-07-09 op #define allowed_to_end_number(x) \
659 c39be742 2021-07-09 op (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
661 c39be742 2021-07-09 op if (c == '-' || isdigit(c)) {
664 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
665 c39be742 2021-07-09 op yyerror("string too long");
666 c39be742 2021-07-09 op return findeol();
668 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && isdigit(c));
670 c39be742 2021-07-09 op if (p == buf + 1 && buf[0] == '-')
671 c39be742 2021-07-09 op goto nodigits;
672 c39be742 2021-07-09 op if (c == EOF || allowed_to_end_number(c)) {
673 c39be742 2021-07-09 op const char *errstr = NULL;
676 c39be742 2021-07-09 op yylval.v.number = strtonum(buf, LLONG_MIN,
677 c39be742 2021-07-09 op LLONG_MAX, &errstr);
678 c39be742 2021-07-09 op if (errstr) {
679 c39be742 2021-07-09 op yyerror("\"%s\" invalid number: %s",
680 c39be742 2021-07-09 op buf, errstr);
681 c39be742 2021-07-09 op return findeol();
686 c39be742 2021-07-09 op while (p > buf + 1)
687 c39be742 2021-07-09 op lungetc(*--p);
689 c39be742 2021-07-09 op if (c == '-')
694 c39be742 2021-07-09 op #define allowed_in_string(x) \
695 c39be742 2021-07-09 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
696 c39be742 2021-07-09 op x != '{' && x != '}' && \
697 c39be742 2021-07-09 op x != '!' && x != '=' && x != '#' && \
698 67f49405 2021-07-09 op x != ',' && x != ';'))
700 67f49405 2021-07-09 op if (isalnum(c) || c == ':' || c == '_') {
703 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
704 c39be742 2021-07-09 op yyerror("string too long");
705 c39be742 2021-07-09 op return findeol();
707 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
710 c39be742 2021-07-09 op if ((token = lookup(buf)) == STRING)
711 c39be742 2021-07-09 op yylval.v.string = xstrdup(buf);
712 c39be742 2021-07-09 op return token;
714 c39be742 2021-07-09 op if (c == '\n') {
715 c39be742 2021-07-09 op yylval.lineno = file->lineno;
716 c39be742 2021-07-09 op file->lineno++;
718 c39be742 2021-07-09 op if (c == EOF)
723 c39be742 2021-07-09 op struct file *
724 c39be742 2021-07-09 op pushfile(const char *name, int secret)
726 c39be742 2021-07-09 op struct file *nfile;
728 c39be742 2021-07-09 op nfile = xcalloc(1, sizeof(*nfile));
729 c39be742 2021-07-09 op nfile->name = xstrdup(name);
730 c39be742 2021-07-09 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
731 c39be742 2021-07-09 op yyerror("can't open %s: %s", nfile->name,
732 c39be742 2021-07-09 op strerror(errno));
733 c39be742 2021-07-09 op free(nfile->name);
737 c39be742 2021-07-09 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
738 c39be742 2021-07-09 op nfile->ungetsize = 16;
739 c39be742 2021-07-09 op nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
740 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&files, nfile, entry);
741 c39be742 2021-07-09 op return nfile;
745 c39be742 2021-07-09 op popfile(void)
747 c39be742 2021-07-09 op struct file *prev;
749 c39be742 2021-07-09 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
750 c39be742 2021-07-09 op prev->errors += file->errors;
752 c39be742 2021-07-09 op TAILQ_REMOVE(&files, file, entry);
753 c39be742 2021-07-09 op fclose(file->stream);
754 c39be742 2021-07-09 op free(file->name);
755 c39be742 2021-07-09 op free(file->ungetbuf);
758 c39be742 2021-07-09 op return file ? 0 : EOF;
762 c39be742 2021-07-09 op parse_conf(const char *filename)
764 c39be742 2021-07-09 op struct sym *sym, *next;
766 c39be742 2021-07-09 op file = pushfile(filename, 0);
767 c39be742 2021-07-09 op if (file == NULL)
769 c39be742 2021-07-09 op topfile = file;
772 c39be742 2021-07-09 op errors = file->errors;
775 c39be742 2021-07-09 op /* Free macros and check which have not been used. */
776 3b21cca3 2021-06-29 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
777 3b21cca3 2021-06-29 op /* TODO: warn if !sym->used */
778 3b21cca3 2021-06-29 op if (!sym->persist) {
779 3b21cca3 2021-06-29 op free(sym->name);
780 3b21cca3 2021-06-29 op free(sym->val);
781 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
791 c39be742 2021-07-09 op symset(const char *name, const char *val, int persist)
793 c39be742 2021-07-09 op struct sym *sym;
795 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
796 c39be742 2021-07-09 op if (!strcmp(name, sym->name))
800 c39be742 2021-07-09 op if (sym != NULL) {
801 c39be742 2021-07-09 op if (sym->persist)
804 c39be742 2021-07-09 op free(sym->name);
805 c39be742 2021-07-09 op free(sym->val);
806 c39be742 2021-07-09 op TAILQ_REMOVE(&symhead, sym, entry);
811 c39be742 2021-07-09 op sym = xcalloc(1, sizeof(*sym));
812 c39be742 2021-07-09 op sym->name = xstrdup(name);
813 c39be742 2021-07-09 op sym->val = xstrdup(val);
814 c39be742 2021-07-09 op sym->used = 0;
815 c39be742 2021-07-09 op sym->persist = persist;
817 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
822 c39be742 2021-07-09 op cmdline_symset(char *s)
824 c39be742 2021-07-09 op char *sym, *val;
827 c39be742 2021-07-09 op if ((val = strrchr(s, '=')) == NULL)
829 c39be742 2021-07-09 op sym = xcalloc(1, val - s + 1);
830 c39be742 2021-07-09 op memcpy(sym, s, val - s);
831 c39be742 2021-07-09 op ret = symset(sym, val + 1, 1);
837 c39be742 2021-07-09 op symget(const char *nam)
839 c39be742 2021-07-09 op struct sym *sym;
841 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
842 c39be742 2021-07-09 op if (strcmp(nam, sym->name) == 0) {
843 c39be742 2021-07-09 op sym->used = 1;
844 c39be742 2021-07-09 op return sym->val;
850 c39be742 2021-07-09 op struct vhost *
851 c39be742 2021-07-09 op new_vhost(void)
853 c39be742 2021-07-09 op return xcalloc(1, sizeof(struct vhost));
856 c39be742 2021-07-09 op struct location *
857 c39be742 2021-07-09 op new_location(void)
859 c39be742 2021-07-09 op struct location *l;
861 c39be742 2021-07-09 op l = xcalloc(1, sizeof(*l));
862 c39be742 2021-07-09 op l->dirfd = -1;
863 c39be742 2021-07-09 op l->fcgi = -1;
868 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
870 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
871 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
876 6abda252 2021-02-06 op check_block_code(int n)
878 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
879 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
884 6abda252 2021-02-06 op check_block_fmt(char *fmt)
888 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
889 6abda252 2021-02-06 op if (*s != '%')
891 6abda252 2021-02-06 op switch (*++s) {
899 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
907 6abda252 2021-02-06 op check_strip_no(int n)
910 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
915 391825e3 2021-07-09 op check_port_num(int n)
917 391825e3 2021-07-09 op if (n <= 0 || n >= UINT16_MAX)
918 391825e3 2021-07-09 op yyerror("port number is %s: %d",
919 391825e3 2021-07-09 op n <= 0 ? "too small" : "too large",
925 a709ddf5 2021-02-07 op check_prefork_num(int n)
927 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
928 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
933 49b73ba1 2021-02-10 op advance_loc(void)
935 b8e64ccd 2021-03-31 op loc = new_location();
936 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
940 c705ecb1 2021-05-03 op only_once(const void *ptr, const char *name)
942 c705ecb1 2021-05-03 op if (ptr != NULL)
943 c705ecb1 2021-05-03 op yyerror("`%s' specified more than once", name);
947 8ad1c570 2021-05-09 op only_oncei(int i, const char *name)
950 8ad1c570 2021-05-09 op yyerror("`%s' specified more than once", name);
954 8ad1c570 2021-05-09 op fastcgi_conf(char *path, char *port, char *prog)
956 8ad1c570 2021-05-09 op struct fcgi *f;
959 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
960 8ad1c570 2021-05-09 op f = &fcgi[i];
962 8ad1c570 2021-05-09 op if (f->path == NULL) {
964 8ad1c570 2021-05-09 op f->path = path;
965 8ad1c570 2021-05-09 op f->port = port;
966 8ad1c570 2021-05-09 op f->prog = prog;
970 8ad1c570 2021-05-09 op /* XXX: what to do with prog? */
971 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
972 8ad1c570 2021-05-09 op ((port == NULL && f->port == NULL) ||
973 8ad1c570 2021-05-09 op !strcmp(f->port, port))) {
980 8ad1c570 2021-05-09 op yyerror("too much `fastcgi' rules defined.");
985 c92b802b 2021-06-11 op add_param(char *name, char *val, int env)
987 c92b802b 2021-06-11 op struct envlist *e;
988 c92b802b 2021-06-11 op struct envhead *h;
991 c92b802b 2021-06-11 op h = &host->env;
993 c92b802b 2021-06-11 op h = &host->params;
995 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
996 c92b802b 2021-06-11 op e->name = name;
997 c92b802b 2021-06-11 op e->value = val;
998 c92b802b 2021-06-11 op if (TAILQ_EMPTY(h))
999 c92b802b 2021-06-11 op TAILQ_INSERT_HEAD(h, e, envs);
1001 c92b802b 2021-06-11 op TAILQ_INSERT_TAIL(h, e, envs);