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 f3966209 2021-07-13 op void yywarn(const char *, ...)
56 f3966209 2021-07-13 op __attribute__((__format__ (printf, 1, 2)))
57 f3966209 2021-07-13 op __attribute__((__nonnull__ (1)));
58 c39be742 2021-07-09 op int kw_cmp(const void *, const void *);
59 c39be742 2021-07-09 op int lookup(char *);
60 c39be742 2021-07-09 op int igetc(void);
61 c39be742 2021-07-09 op int lgetc(int);
62 c39be742 2021-07-09 op void lungetc(int);
63 c39be742 2021-07-09 op int findeol(void);
66 15902770 2021-01-15 op * #define YYDEBUG 1
67 15902770 2021-01-15 op * int yydebug = 1;
70 3b21cca3 2021-06-29 op TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
72 3b21cca3 2021-06-29 op TAILQ_ENTRY(sym) entry;
79 c39be742 2021-07-09 op int symset(const char *, const char *, int);
80 c39be742 2021-07-09 op char *symget(const char *);
82 c39be742 2021-07-09 op struct vhost *new_vhost(void);
83 c39be742 2021-07-09 op struct location *new_location(void);
84 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
85 6abda252 2021-02-06 op int check_block_code(int);
86 6abda252 2021-02-06 op char *check_block_fmt(char*);
87 6abda252 2021-02-06 op int check_strip_no(int);
88 391825e3 2021-07-09 op int check_port_num(int);
89 a709ddf5 2021-02-07 op int check_prefork_num(int);
90 49b73ba1 2021-02-10 op void advance_loc(void);
91 c705ecb1 2021-05-03 op void only_once(const void*, const char*);
92 8ad1c570 2021-05-09 op void only_oncei(int, const char*);
93 8ad1c570 2021-05-09 op int fastcgi_conf(char *, char *, char *);
94 c92b802b 2021-06-11 op void add_param(char *, char *, int);
96 c39be742 2021-07-09 op static struct vhost *host;
97 c39be742 2021-07-09 op static struct location *loc;
98 c39be742 2021-07-09 op static int errors;
100 c39be742 2021-07-09 op typedef struct {
102 c39be742 2021-07-09 op char *string;
110 15902770 2021-01-15 op /* for bison: */
111 15902770 2021-01-15 op /* %define parse.error verbose */
113 7252049d 2021-06-29 op %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
114 7252049d 2021-06-29 op %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
115 7252049d 2021-06-29 op %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
116 c39be742 2021-07-09 op %token TFASTCGI TSPAWN TPARAM TMAP TTOEXT INCLUDE TON TOFF
120 c39be742 2021-07-09 op %token <v.string> STRING
121 c39be742 2021-07-09 op %token <v.number> NUM
123 c39be742 2021-07-09 op %type <v.number> bool
124 c39be742 2021-07-09 op %type <v.string> string
128 6b86655a 2021-06-29 op conf : /* empty */
129 c39be742 2021-07-09 op | conf include '\n'
131 c39be742 2021-07-09 op | conf varset '\n'
132 c39be742 2021-07-09 op | conf option '\n'
133 c39be742 2021-07-09 op | conf vhost '\n'
134 c39be742 2021-07-09 op | conf error '\n' { file->errors++; }
137 c39be742 2021-07-09 op include : INCLUDE STRING {
138 c39be742 2021-07-09 op struct file *nfile;
140 c39be742 2021-07-09 op if ((nfile = pushfile($2, 0)) == NULL) {
141 c39be742 2021-07-09 op yyerror("failed to include file %s", $2);
147 c39be742 2021-07-09 op file = nfile;
148 c39be742 2021-07-09 op lungetc('\n');
152 c39be742 2021-07-09 op bool : TON { $$ = 1; }
153 c39be742 2021-07-09 op | TOFF { $$ = 0; }
156 c39be742 2021-07-09 op string : string STRING {
157 98f52178 2021-06-29 op if (asprintf(&$$, "%s%s", $1, $2) == -1) {
160 98f52178 2021-06-29 op yyerror("string: asprintf: %s", strerror(errno));
169 c39be742 2021-07-09 op varset : STRING '=' string {
170 3b21cca3 2021-06-29 op char *s = $1;
171 3b21cca3 2021-06-29 op while (*s++) {
172 c39be742 2021-07-09 op if (isspace((unsigned char)*s)) {
173 3b21cca3 2021-06-29 op yyerror("macro name cannot contain "
174 3b21cca3 2021-06-29 op "whitespaces");
180 3b21cca3 2021-06-29 op symset($1, $3, 0);
186 98f52178 2021-06-29 op option : TCHROOT string { conf.chroot = $2; }
187 c39be742 2021-07-09 op | TIPV6 bool { conf.ipv6 = $2; }
188 c39be742 2021-07-09 op | TMIME STRING string {
189 f3966209 2021-07-13 op yywarn("`mime MIME EXT' is deprecated and will be "
190 f3966209 2021-07-13 op "removed in a future version, please use the new "
191 f3966209 2021-07-13 op "syntax: `map MIME to-ext EXT'");
192 d19951cf 2021-07-09 op add_mime(&conf.mime, $2, $3);
194 ff954a3e 2021-07-09 op | TMAP string TTOEXT string { add_mime(&conf.mime, $2, $4); }
195 391825e3 2021-07-09 op | TPORT NUM { conf.port = check_port_num($2); }
196 c39be742 2021-07-09 op | TPREFORK NUM { conf.prefork = check_prefork_num($2); }
197 98f52178 2021-06-29 op | TPROTOCOLS string {
198 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
199 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
201 98f52178 2021-06-29 op | TUSER string { conf.user = $2; }
204 98f52178 2021-06-29 op vhost : TSERVER string {
205 b8e64ccd 2021-03-31 op host = new_vhost();
206 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
208 b8e64ccd 2021-03-31 op loc = new_location();
209 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
211 b8e64ccd 2021-03-31 op loc->match = xstrdup("*");
212 15902770 2021-01-15 op host->domain = $2;
214 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
215 f3966209 2021-07-13 op yywarn("\"%s\" looks like punycode: you "
216 f3966209 2021-07-13 op "should use the decoded hostname", $2);
218 c39be742 2021-07-09 op } '{' optnl servopts locations '}' {
219 fdea6aa0 2021-04-30 op if (host->cert == NULL || host->key == NULL)
220 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
222 f3966209 2021-07-13 op | error '}' { yyerror("bad server directive"); }
225 15902770 2021-01-15 op servopts : /* empty */
226 c39be742 2021-07-09 op | servopts servopt optnl
229 98f52178 2021-06-29 op servopt : TALIAS string {
230 cc8c2901 2021-04-29 op struct alist *a;
232 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
233 cc8c2901 2021-04-29 op a->alias = $2;
234 cc8c2901 2021-04-29 op if (TAILQ_EMPTY(&host->aliases))
235 cc8c2901 2021-04-29 op TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
237 cc8c2901 2021-04-29 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
239 98f52178 2021-06-29 op | TCERT string {
240 c705ecb1 2021-05-03 op only_once(host->cert, "cert");
241 c705ecb1 2021-05-03 op host->cert = ensure_absolute_path($2);
243 98f52178 2021-06-29 op | TCGI string {
244 c705ecb1 2021-05-03 op only_once(host->cgi, "cgi");
245 15902770 2021-01-15 op /* drop the starting '/', if any */
246 709f4c94 2021-02-04 op if (*$2 == '/')
247 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
248 709f4c94 2021-02-04 op host->cgi = $2;
250 98f52178 2021-06-29 op | TENTRYPOINT string {
251 c705ecb1 2021-05-03 op only_once(host->entrypoint, "entrypoint");
252 e3ddf390 2021-02-06 op while (*$2 == '/')
253 e3ddf390 2021-02-06 op memmove($2, $2+1, strlen($2));
254 e3ddf390 2021-02-06 op host->entrypoint = $2;
256 efacb859 2021-07-09 op | TENV string '=' string {
257 762b9b99 2021-07-09 op add_param($2, $4, 1);
259 98f52178 2021-06-29 op | TKEY string {
260 c705ecb1 2021-05-03 op only_once(host->key, "key");
261 c705ecb1 2021-05-03 op host->key = ensure_absolute_path($2);
263 efacb859 2021-07-09 op | TPARAM string '=' string {
264 762b9b99 2021-07-09 op add_param($2, $4, 0);
269 c8b74339 2021-01-24 op locations : /* empty */
270 c39be742 2021-07-09 op | locations location optnl
273 c39be742 2021-07-09 op location : TLOCATION { advance_loc(); } string '{' optnl locopts '}' {
274 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
275 49b73ba1 2021-02-10 op if (*$3 == '/')
276 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
277 49b73ba1 2021-02-10 op loc->match = $3;
282 c8b74339 2021-01-24 op locopts : /* empty */
283 c39be742 2021-07-09 op | locopts locopt optnl
286 c39be742 2021-07-09 op locopt : TAUTO TINDEX bool { loc->auto_index = $3 ? 1 : -1; }
287 c39be742 2021-07-09 op | TBLOCK TRETURN NUM string {
288 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
289 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
290 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
292 c39be742 2021-07-09 op | TBLOCK TRETURN NUM {
293 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
294 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
295 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
296 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
297 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
300 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
301 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
302 6abda252 2021-02-06 op loc->block_code = 40;
304 98f52178 2021-06-29 op | TDEFAULT TTYPE string {
305 c705ecb1 2021-05-03 op only_once(loc->default_mime, "default type");
306 eb59f87e 2021-02-09 op loc->default_mime = $3;
308 0d047efc 2021-05-24 op | TFASTCGI fastcgi
309 98f52178 2021-06-29 op | TINDEX string {
310 c705ecb1 2021-05-03 op only_once(loc->index, "index");
311 eb59f87e 2021-02-09 op loc->index = $2;
313 98f52178 2021-06-29 op | TLANG string {
314 c705ecb1 2021-05-03 op only_once(loc->lang, "lang");
315 eb59f87e 2021-02-09 op loc->lang = $2;
317 c39be742 2021-07-09 op | TLOG bool { loc->disable_log = !$2; }
318 98f52178 2021-06-29 op | TREQUIRE TCLIENT TCA string {
319 c705ecb1 2021-05-03 op only_once(loc->reqca, "require client ca");
320 adbe6a64 2021-04-30 op ensure_absolute_path($4);
321 02be96c6 2021-02-09 op if ((loc->reqca = load_ca($4)) == NULL)
322 02be96c6 2021-02-09 op yyerror("couldn't load ca cert: %s", $4);
325 98f52178 2021-06-29 op | TROOT string {
326 c705ecb1 2021-05-03 op only_once(loc->dir, "root");
327 fdea6aa0 2021-04-30 op loc->dir = ensure_absolute_path($2);
329 c39be742 2021-07-09 op | TSTRIP NUM { loc->strip = check_strip_no($2); }
332 98f52178 2021-06-29 op fastcgi : TSPAWN string {
333 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
334 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf(NULL, NULL, $2);
337 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
338 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($1, NULL, NULL);
340 c39be742 2021-07-09 op | TTCP string TPORT NUM {
342 762b9b99 2021-07-09 op if (asprintf(&c, "%d", $4) == -1)
343 0d047efc 2021-05-24 op err(1, "asprintf");
344 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
345 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, c, NULL);
347 98f52178 2021-06-29 op | TTCP string {
348 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
349 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
351 762b9b99 2021-07-09 op | TTCP string TPORT string {
352 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
353 762b9b99 2021-07-09 op loc->fcgi = fastcgi_conf($2, $4, NULL);
357 c39be742 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
358 67f49405 2021-07-09 op | ';' optnl /* semicolons too */
364 74f0778b 2021-06-16 op static struct keyword {
365 74f0778b 2021-06-16 op const char *word;
367 74f0778b 2021-06-16 op } keywords[] = {
368 d93c8191 2021-07-09 op /* these MUST be sorted */
369 74f0778b 2021-06-16 op {"alias", TALIAS},
370 74f0778b 2021-06-16 op {"auto", TAUTO},
371 74f0778b 2021-06-16 op {"block", TBLOCK},
373 74f0778b 2021-06-16 op {"cert", TCERT},
374 74f0778b 2021-06-16 op {"cgi", TCGI},
375 74f0778b 2021-06-16 op {"chroot", TCHROOT},
376 74f0778b 2021-06-16 op {"client", TCLIENT},
377 74f0778b 2021-06-16 op {"default", TDEFAULT},
378 74f0778b 2021-06-16 op {"entrypoint", TENTRYPOINT},
379 74f0778b 2021-06-16 op {"env", TENV},
380 74f0778b 2021-06-16 op {"fastcgi", TFASTCGI},
381 74f0778b 2021-06-16 op {"index", TINDEX},
382 74f0778b 2021-06-16 op {"ipv6", TIPV6},
383 74f0778b 2021-06-16 op {"key", TKEY},
384 74f0778b 2021-06-16 op {"lang", TLANG},
385 74f0778b 2021-06-16 op {"location", TLOCATION},
386 74f0778b 2021-06-16 op {"log", TLOG},
387 d19951cf 2021-07-09 op {"map", TMAP},
388 74f0778b 2021-06-16 op {"mime", TMIME},
389 c39be742 2021-07-09 op {"off", TOFF},
391 74f0778b 2021-06-16 op {"param", TPARAM},
392 74f0778b 2021-06-16 op {"port", TPORT},
393 74f0778b 2021-06-16 op {"prefork", TPREFORK},
394 74f0778b 2021-06-16 op {"protocols", TPROTOCOLS},
395 74f0778b 2021-06-16 op {"require", TREQUIRE},
396 74f0778b 2021-06-16 op {"return", TRETURN},
397 74f0778b 2021-06-16 op {"root", TROOT},
398 74f0778b 2021-06-16 op {"server", TSERVER},
399 74f0778b 2021-06-16 op {"spawn", TSPAWN},
400 74f0778b 2021-06-16 op {"strip", TSTRIP},
401 74f0778b 2021-06-16 op {"tcp", TTCP},
402 d19951cf 2021-07-09 op {"to-ext", TTOEXT},
403 74f0778b 2021-06-16 op {"type", TTYPE},
404 74f0778b 2021-06-16 op {"user", TUSER},
408 c39be742 2021-07-09 op yyerror(const char *msg, ...)
412 c39be742 2021-07-09 op file->errors++;
414 c39be742 2021-07-09 op va_start(ap, msg);
415 f3966209 2021-07-13 op fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
416 f3966209 2021-07-13 op vfprintf(stderr, msg, ap);
417 f3966209 2021-07-13 op fprintf(stderr, "\n");
422 f3966209 2021-07-13 op yywarn(const char *msg, ...)
426 f3966209 2021-07-13 op va_start(ap, msg);
427 f3966209 2021-07-13 op fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
428 c39be742 2021-07-09 op vfprintf(stderr, msg, ap);
429 c39be742 2021-07-09 op fprintf(stderr, "\n");
434 d93c8191 2021-07-09 op kw_cmp(const void *k, const void *e)
436 d93c8191 2021-07-09 op return strcmp(k, ((struct keyword *)e)->word);
440 c39be742 2021-07-09 op lookup(char *s)
442 c39be742 2021-07-09 op const struct keyword *p;
444 c39be742 2021-07-09 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
445 c39be742 2021-07-09 op sizeof(keywords[0]), kw_cmp);
448 c39be742 2021-07-09 op return p->token;
450 c39be742 2021-07-09 op return STRING;
453 c39be742 2021-07-09 op #define START_EXPAND 1
454 c39be742 2021-07-09 op #define DONE_EXPAND 2
456 c39be742 2021-07-09 op static int expanding;
464 c39be742 2021-07-09 op if (file->ungetpos > 0)
465 c39be742 2021-07-09 op c = file->ungetbuf[--file->ungetpos];
467 c39be742 2021-07-09 op c = getc(file->stream);
469 c39be742 2021-07-09 op if (c == START_EXPAND)
470 c39be742 2021-07-09 op expanding = 1;
471 c39be742 2021-07-09 op else if (c == DONE_EXPAND)
472 c39be742 2021-07-09 op expanding = 0;
480 c39be742 2021-07-09 op lgetc(int quotec)
484 c39be742 2021-07-09 op if (quotec) {
485 c39be742 2021-07-09 op if ((c = igetc()) == EOF) {
486 c39be742 2021-07-09 op yyerror("reached end of file while parsing "
487 c39be742 2021-07-09 op "quoted string");
488 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
490 c39be742 2021-07-09 op return quotec;
495 c39be742 2021-07-09 op while ((c = igetc()) == '\\') {
496 c39be742 2021-07-09 op next = igetc();
497 c39be742 2021-07-09 op if (next != '\n') {
501 c39be742 2021-07-09 op yylval.lineno = file->lineno;
502 c39be742 2021-07-09 op file->lineno++;
505 c39be742 2021-07-09 op if (c == EOF) {
507 c39be742 2021-07-09 op * Fake EOL when hit EOF for the first time. This gets line
508 c39be742 2021-07-09 op * count right if last line in included file is syntactically
509 c39be742 2021-07-09 op * invalid and has no newline.
511 c39be742 2021-07-09 op if (file->eof_reached == 0) {
512 c39be742 2021-07-09 op file->eof_reached = 1;
515 c39be742 2021-07-09 op while (c == EOF) {
516 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
525 c39be742 2021-07-09 op lungetc(int c)
527 c39be742 2021-07-09 op if (c == EOF)
530 c39be742 2021-07-09 op if (file->ungetpos >= file->ungetsize) {
531 c39be742 2021-07-09 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
532 c39be742 2021-07-09 op if (p == NULL)
533 c39be742 2021-07-09 op err(1, "lungetc");
534 c39be742 2021-07-09 op file->ungetbuf = p;
535 c39be742 2021-07-09 op file->ungetsize *= 2;
537 c39be742 2021-07-09 op file->ungetbuf[file->ungetpos++] = c;
541 c39be742 2021-07-09 op findeol(void)
545 c39be742 2021-07-09 op /* Skip to either EOF or the first real EOL. */
547 c39be742 2021-07-09 op c = lgetc(0);
548 c39be742 2021-07-09 op if (c == '\n') {
549 c39be742 2021-07-09 op file->lineno++;
552 c39be742 2021-07-09 op if (c == EOF)
555 c39be742 2021-07-09 op return ERROR;
561 1bd706dc 2021-07-09 op char buf[8096];
562 1bd706dc 2021-07-09 op char *p, *val;
563 1bd706dc 2021-07-09 op int quotec, next, c;
568 c39be742 2021-07-09 op while ((c = lgetc(0)) == ' ' || c == '\t')
569 c39be742 2021-07-09 op ; /* nothing */
571 c39be742 2021-07-09 op yylval.lineno = file->lineno;
572 c39be742 2021-07-09 op if (c == '#')
573 c39be742 2021-07-09 op while ((c = lgetc(0)) != '\n' && c != EOF)
574 c39be742 2021-07-09 op ; /* nothing */
575 c39be742 2021-07-09 op if (c == '$' && !expanding) {
577 c39be742 2021-07-09 op if ((c = lgetc(0)) == EOF)
579 67f49405 2021-07-09 op if (p + 1 >= buf + sizeof(buf) -1) {
580 67f49405 2021-07-09 op yyerror("string too long");
581 67f49405 2021-07-09 op return findeol();
583 67f49405 2021-07-09 op if (isalnum(c) || c == '_') {
591 67f49405 2021-07-09 op val = symget(buf);
592 67f49405 2021-07-09 op if (val == NULL) {
593 67f49405 2021-07-09 op yyerror("macro `%s' not defined", buf);
594 67f49405 2021-07-09 op return findeol();
596 67f49405 2021-07-09 op yylval.v.string = xstrdup(val);
597 67f49405 2021-07-09 op return STRING;
599 67f49405 2021-07-09 op if (c == '@' && !expanding) {
601 67f49405 2021-07-09 op if ((c = lgetc(0)) == EOF)
604 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
605 c39be742 2021-07-09 op yyerror("string too long");
606 c39be742 2021-07-09 op return findeol();
608 c39be742 2021-07-09 op if (isalnum(c) || c == '_') {
616 c39be742 2021-07-09 op val = symget(buf);
617 c39be742 2021-07-09 op if (val == NULL) {
618 c39be742 2021-07-09 op yyerror("macro '%s' not defined", buf);
619 c39be742 2021-07-09 op return findeol();
621 c39be742 2021-07-09 op p = val + strlen(val) - 1;
622 c39be742 2021-07-09 op lungetc(DONE_EXPAND);
623 c39be742 2021-07-09 op while (p >= val) {
627 c39be742 2021-07-09 op lungetc(START_EXPAND);
636 c39be742 2021-07-09 op if ((c = lgetc(quotec)) == EOF)
638 c39be742 2021-07-09 op if (c == '\n') {
639 c39be742 2021-07-09 op file->lineno++;
641 c39be742 2021-07-09 op } else if (c == '\\') {
642 c39be742 2021-07-09 op if ((next = lgetc(quotec)) == EOF)
644 c39be742 2021-07-09 op if (next == quotec || next == ' ' ||
645 c39be742 2021-07-09 op next == '\t')
647 c39be742 2021-07-09 op else if (next == '\n') {
648 c39be742 2021-07-09 op file->lineno++;
651 c39be742 2021-07-09 op lungetc(next);
652 c39be742 2021-07-09 op } else if (c == quotec) {
655 c39be742 2021-07-09 op } else if (c == '\0') {
656 f3966209 2021-07-13 op yyerror("invalid syntax");
657 c39be742 2021-07-09 op return findeol();
659 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
660 c39be742 2021-07-09 op yyerror("string too long");
661 c39be742 2021-07-09 op return findeol();
665 c39be742 2021-07-09 op yylval.v.string = strdup(buf);
666 c39be742 2021-07-09 op if (yylval.v.string == NULL)
667 c39be742 2021-07-09 op err(1, "yylex: strdup");
668 c39be742 2021-07-09 op return STRING;
671 c39be742 2021-07-09 op #define allowed_to_end_number(x) \
672 c39be742 2021-07-09 op (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
674 c39be742 2021-07-09 op if (c == '-' || isdigit(c)) {
677 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
678 c39be742 2021-07-09 op yyerror("string too long");
679 c39be742 2021-07-09 op return findeol();
681 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && isdigit(c));
683 c39be742 2021-07-09 op if (p == buf + 1 && buf[0] == '-')
684 c39be742 2021-07-09 op goto nodigits;
685 c39be742 2021-07-09 op if (c == EOF || allowed_to_end_number(c)) {
686 c39be742 2021-07-09 op const char *errstr = NULL;
689 c39be742 2021-07-09 op yylval.v.number = strtonum(buf, LLONG_MIN,
690 c39be742 2021-07-09 op LLONG_MAX, &errstr);
691 c39be742 2021-07-09 op if (errstr) {
692 c39be742 2021-07-09 op yyerror("\"%s\" invalid number: %s",
693 c39be742 2021-07-09 op buf, errstr);
694 c39be742 2021-07-09 op return findeol();
699 c39be742 2021-07-09 op while (p > buf + 1)
700 c39be742 2021-07-09 op lungetc(*--p);
702 c39be742 2021-07-09 op if (c == '-')
707 c39be742 2021-07-09 op #define allowed_in_string(x) \
708 c39be742 2021-07-09 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
709 c39be742 2021-07-09 op x != '{' && x != '}' && \
710 c39be742 2021-07-09 op x != '!' && x != '=' && x != '#' && \
711 67f49405 2021-07-09 op x != ',' && x != ';'))
713 67f49405 2021-07-09 op if (isalnum(c) || c == ':' || c == '_') {
716 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
717 c39be742 2021-07-09 op yyerror("string too long");
718 c39be742 2021-07-09 op return findeol();
720 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
723 c39be742 2021-07-09 op if ((token = lookup(buf)) == STRING)
724 c39be742 2021-07-09 op yylval.v.string = xstrdup(buf);
725 c39be742 2021-07-09 op return token;
727 c39be742 2021-07-09 op if (c == '\n') {
728 c39be742 2021-07-09 op yylval.lineno = file->lineno;
729 c39be742 2021-07-09 op file->lineno++;
731 c39be742 2021-07-09 op if (c == EOF)
736 c39be742 2021-07-09 op struct file *
737 c39be742 2021-07-09 op pushfile(const char *name, int secret)
739 c39be742 2021-07-09 op struct file *nfile;
741 c39be742 2021-07-09 op nfile = xcalloc(1, sizeof(*nfile));
742 c39be742 2021-07-09 op nfile->name = xstrdup(name);
743 c39be742 2021-07-09 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
744 c39be742 2021-07-09 op yyerror("can't open %s: %s", nfile->name,
745 c39be742 2021-07-09 op strerror(errno));
746 c39be742 2021-07-09 op free(nfile->name);
750 c39be742 2021-07-09 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
751 c39be742 2021-07-09 op nfile->ungetsize = 16;
752 c39be742 2021-07-09 op nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
753 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&files, nfile, entry);
754 c39be742 2021-07-09 op return nfile;
758 c39be742 2021-07-09 op popfile(void)
760 c39be742 2021-07-09 op struct file *prev;
762 c39be742 2021-07-09 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
763 c39be742 2021-07-09 op prev->errors += file->errors;
765 c39be742 2021-07-09 op TAILQ_REMOVE(&files, file, entry);
766 c39be742 2021-07-09 op fclose(file->stream);
767 c39be742 2021-07-09 op free(file->name);
768 c39be742 2021-07-09 op free(file->ungetbuf);
771 c39be742 2021-07-09 op return file ? 0 : EOF;
775 c39be742 2021-07-09 op parse_conf(const char *filename)
777 c39be742 2021-07-09 op struct sym *sym, *next;
779 c39be742 2021-07-09 op file = pushfile(filename, 0);
780 c39be742 2021-07-09 op if (file == NULL)
782 c39be742 2021-07-09 op topfile = file;
785 c39be742 2021-07-09 op errors = file->errors;
788 c39be742 2021-07-09 op /* Free macros and check which have not been used. */
789 3b21cca3 2021-06-29 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
790 3b21cca3 2021-06-29 op /* TODO: warn if !sym->used */
791 3b21cca3 2021-06-29 op if (!sym->persist) {
792 3b21cca3 2021-06-29 op free(sym->name);
793 3b21cca3 2021-06-29 op free(sym->val);
794 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
804 c39be742 2021-07-09 op symset(const char *name, const char *val, int persist)
806 c39be742 2021-07-09 op struct sym *sym;
808 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
809 c39be742 2021-07-09 op if (!strcmp(name, sym->name))
813 c39be742 2021-07-09 op if (sym != NULL) {
814 c39be742 2021-07-09 op if (sym->persist)
817 c39be742 2021-07-09 op free(sym->name);
818 c39be742 2021-07-09 op free(sym->val);
819 c39be742 2021-07-09 op TAILQ_REMOVE(&symhead, sym, entry);
824 c39be742 2021-07-09 op sym = xcalloc(1, sizeof(*sym));
825 c39be742 2021-07-09 op sym->name = xstrdup(name);
826 c39be742 2021-07-09 op sym->val = xstrdup(val);
827 c39be742 2021-07-09 op sym->used = 0;
828 c39be742 2021-07-09 op sym->persist = persist;
830 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
835 c39be742 2021-07-09 op cmdline_symset(char *s)
837 c39be742 2021-07-09 op char *sym, *val;
840 c39be742 2021-07-09 op if ((val = strrchr(s, '=')) == NULL)
842 c39be742 2021-07-09 op sym = xcalloc(1, val - s + 1);
843 c39be742 2021-07-09 op memcpy(sym, s, val - s);
844 c39be742 2021-07-09 op ret = symset(sym, val + 1, 1);
850 c39be742 2021-07-09 op symget(const char *nam)
852 c39be742 2021-07-09 op struct sym *sym;
854 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
855 c39be742 2021-07-09 op if (strcmp(nam, sym->name) == 0) {
856 c39be742 2021-07-09 op sym->used = 1;
857 c39be742 2021-07-09 op return sym->val;
863 c39be742 2021-07-09 op struct vhost *
864 c39be742 2021-07-09 op new_vhost(void)
866 c39be742 2021-07-09 op return xcalloc(1, sizeof(struct vhost));
869 c39be742 2021-07-09 op struct location *
870 c39be742 2021-07-09 op new_location(void)
872 c39be742 2021-07-09 op struct location *l;
874 c39be742 2021-07-09 op l = xcalloc(1, sizeof(*l));
875 c39be742 2021-07-09 op l->dirfd = -1;
876 c39be742 2021-07-09 op l->fcgi = -1;
881 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
883 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
884 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
889 6abda252 2021-02-06 op check_block_code(int n)
891 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
892 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
897 6abda252 2021-02-06 op check_block_fmt(char *fmt)
901 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
902 6abda252 2021-02-06 op if (*s != '%')
904 6abda252 2021-02-06 op switch (*++s) {
912 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
920 6abda252 2021-02-06 op check_strip_no(int n)
923 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
928 391825e3 2021-07-09 op check_port_num(int n)
930 391825e3 2021-07-09 op if (n <= 0 || n >= UINT16_MAX)
931 391825e3 2021-07-09 op yyerror("port number is %s: %d",
932 391825e3 2021-07-09 op n <= 0 ? "too small" : "too large",
938 a709ddf5 2021-02-07 op check_prefork_num(int n)
940 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
941 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
946 49b73ba1 2021-02-10 op advance_loc(void)
948 b8e64ccd 2021-03-31 op loc = new_location();
949 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
953 c705ecb1 2021-05-03 op only_once(const void *ptr, const char *name)
955 c705ecb1 2021-05-03 op if (ptr != NULL)
956 c705ecb1 2021-05-03 op yyerror("`%s' specified more than once", name);
960 8ad1c570 2021-05-09 op only_oncei(int i, const char *name)
963 8ad1c570 2021-05-09 op yyerror("`%s' specified more than once", name);
967 8ad1c570 2021-05-09 op fastcgi_conf(char *path, char *port, char *prog)
969 8ad1c570 2021-05-09 op struct fcgi *f;
972 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
973 8ad1c570 2021-05-09 op f = &fcgi[i];
975 8ad1c570 2021-05-09 op if (f->path == NULL) {
977 8ad1c570 2021-05-09 op f->path = path;
978 8ad1c570 2021-05-09 op f->port = port;
979 8ad1c570 2021-05-09 op f->prog = prog;
983 8ad1c570 2021-05-09 op /* XXX: what to do with prog? */
984 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
985 8ad1c570 2021-05-09 op ((port == NULL && f->port == NULL) ||
986 8ad1c570 2021-05-09 op !strcmp(f->port, port))) {
993 8ad1c570 2021-05-09 op yyerror("too much `fastcgi' rules defined.");
998 c92b802b 2021-06-11 op add_param(char *name, char *val, int env)
1000 c92b802b 2021-06-11 op struct envlist *e;
1001 c92b802b 2021-06-11 op struct envhead *h;
1004 c92b802b 2021-06-11 op h = &host->env;
1006 c92b802b 2021-06-11 op h = &host->params;
1008 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
1009 c92b802b 2021-06-11 op e->name = name;
1010 c92b802b 2021-06-11 op e->value = val;
1011 c92b802b 2021-06-11 op if (TAILQ_EMPTY(h))
1012 c92b802b 2021-06-11 op TAILQ_INSERT_HEAD(h, e, envs);
1014 c92b802b 2021-06-11 op TAILQ_INSERT_TAIL(h, e, envs);