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 c74c7030 2021-07-19 op %token ALIAS AUTO
115 c74c7030 2021-07-19 op %token CA CERT CGI CHROOT CLIENT
116 c74c7030 2021-07-19 op %token DEFAULT
117 c74c7030 2021-07-19 op %token ENTRYPOINT ENV
118 abc8801d 2021-07-19 op %token FASTCGI
119 c74c7030 2021-07-19 op %token INCLUDE INDEX IPV6
121 c74c7030 2021-07-19 op %token LANG LOCATION LOG
122 c74c7030 2021-07-19 op %token MAP MIME
123 ff05125e 2021-10-15 op %token OCSP OFF ON
124 72b033ef 2021-12-29 op %token PARAM PORT PREFORK PROTOCOLS PROXY
125 72b033ef 2021-12-29 op %token RELAY_TO REQUIRE RETURN ROOT
126 c74c7030 2021-07-19 op %token SERVER SPAWN STRIP
127 c74c7030 2021-07-19 op %token TCP TOEXT TYPE USER
131 c39be742 2021-07-09 op %token <v.string> STRING
132 c39be742 2021-07-09 op %token <v.number> NUM
134 c39be742 2021-07-09 op %type <v.number> bool
135 c39be742 2021-07-09 op %type <v.string> string
139 6b86655a 2021-06-29 op conf : /* empty */
140 c39be742 2021-07-09 op | conf include '\n'
142 c39be742 2021-07-09 op | conf varset '\n'
143 c39be742 2021-07-09 op | conf option '\n'
144 c39be742 2021-07-09 op | conf vhost '\n'
145 c39be742 2021-07-09 op | conf error '\n' { file->errors++; }
148 c39be742 2021-07-09 op include : INCLUDE STRING {
149 c39be742 2021-07-09 op struct file *nfile;
151 c39be742 2021-07-09 op if ((nfile = pushfile($2, 0)) == NULL) {
152 c39be742 2021-07-09 op yyerror("failed to include file %s", $2);
158 c39be742 2021-07-09 op file = nfile;
159 c39be742 2021-07-09 op lungetc('\n');
163 c74c7030 2021-07-19 op bool : ON { $$ = 1; }
164 c74c7030 2021-07-19 op | OFF { $$ = 0; }
167 c39be742 2021-07-09 op string : string STRING {
168 98f52178 2021-06-29 op if (asprintf(&$$, "%s%s", $1, $2) == -1) {
171 98f52178 2021-06-29 op yyerror("string: asprintf: %s", strerror(errno));
180 c39be742 2021-07-09 op varset : STRING '=' string {
181 3b21cca3 2021-06-29 op char *s = $1;
182 3b21cca3 2021-06-29 op while (*s++) {
183 c39be742 2021-07-09 op if (isspace((unsigned char)*s)) {
184 3b21cca3 2021-06-29 op yyerror("macro name cannot contain "
185 3b21cca3 2021-06-29 op "whitespaces");
191 3b21cca3 2021-06-29 op symset($1, $3, 0);
197 c74c7030 2021-07-19 op option : CHROOT string { conf.chroot = $2; }
198 c74c7030 2021-07-19 op | IPV6 bool { conf.ipv6 = $2; }
199 c74c7030 2021-07-19 op | MIME STRING string {
200 f3966209 2021-07-13 op yywarn("`mime MIME EXT' is deprecated and will be "
201 f3966209 2021-07-13 op "removed in a future version, please use the new "
202 f3966209 2021-07-13 op "syntax: `map MIME to-ext EXT'");
203 d19951cf 2021-07-09 op add_mime(&conf.mime, $2, $3);
205 c74c7030 2021-07-19 op | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
206 c74c7030 2021-07-19 op | PORT NUM { conf.port = check_port_num($2); }
207 c74c7030 2021-07-19 op | PREFORK NUM { conf.prefork = check_prefork_num($2); }
208 c74c7030 2021-07-19 op | PROTOCOLS string {
209 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
210 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
212 c74c7030 2021-07-19 op | USER string { conf.user = $2; }
215 c74c7030 2021-07-19 op vhost : SERVER string {
216 b8e64ccd 2021-03-31 op host = new_vhost();
217 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
219 b8e64ccd 2021-03-31 op loc = new_location();
220 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
222 b8e64ccd 2021-03-31 op loc->match = xstrdup("*");
223 15902770 2021-01-15 op host->domain = $2;
225 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
226 f3966209 2021-07-13 op yywarn("\"%s\" looks like punycode: you "
227 f3966209 2021-07-13 op "should use the decoded hostname", $2);
229 c39be742 2021-07-09 op } '{' optnl servopts locations '}' {
230 fdea6aa0 2021-04-30 op if (host->cert == NULL || host->key == NULL)
231 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
233 f3966209 2021-07-13 op | error '}' { yyerror("bad server directive"); }
236 15902770 2021-01-15 op servopts : /* empty */
237 c39be742 2021-07-09 op | servopts servopt optnl
240 c74c7030 2021-07-19 op servopt : ALIAS string {
241 cc8c2901 2021-04-29 op struct alist *a;
243 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
244 cc8c2901 2021-04-29 op a->alias = $2;
245 cc8c2901 2021-04-29 op if (TAILQ_EMPTY(&host->aliases))
246 cc8c2901 2021-04-29 op TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
248 cc8c2901 2021-04-29 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
250 c74c7030 2021-07-19 op | CERT string {
251 c705ecb1 2021-05-03 op only_once(host->cert, "cert");
252 c705ecb1 2021-05-03 op host->cert = ensure_absolute_path($2);
254 c74c7030 2021-07-19 op | CGI string {
255 c705ecb1 2021-05-03 op only_once(host->cgi, "cgi");
256 15902770 2021-01-15 op /* drop the starting '/', if any */
257 709f4c94 2021-02-04 op if (*$2 == '/')
258 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
259 709f4c94 2021-02-04 op host->cgi = $2;
261 c74c7030 2021-07-19 op | ENTRYPOINT string {
262 c705ecb1 2021-05-03 op only_once(host->entrypoint, "entrypoint");
263 e3ddf390 2021-02-06 op while (*$2 == '/')
264 e3ddf390 2021-02-06 op memmove($2, $2+1, strlen($2));
265 e3ddf390 2021-02-06 op host->entrypoint = $2;
267 c74c7030 2021-07-19 op | ENV string '=' string {
268 762b9b99 2021-07-09 op add_param($2, $4, 1);
270 c74c7030 2021-07-19 op | KEY string {
271 c705ecb1 2021-05-03 op only_once(host->key, "key");
272 c705ecb1 2021-05-03 op host->key = ensure_absolute_path($2);
274 ff05125e 2021-10-15 op | OCSP string {
275 ff05125e 2021-10-15 op only_once(host->ocsp, "ocsp");
276 ff05125e 2021-10-15 op host->ocsp = ensure_absolute_path($2);
278 c74c7030 2021-07-19 op | PARAM string '=' string {
279 762b9b99 2021-07-09 op add_param($2, $4, 0);
284 c8b74339 2021-01-24 op locations : /* empty */
285 c39be742 2021-07-09 op | locations location optnl
288 c74c7030 2021-07-19 op location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
289 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
290 49b73ba1 2021-02-10 op if (*$3 == '/')
291 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
292 49b73ba1 2021-02-10 op loc->match = $3;
297 c8b74339 2021-01-24 op locopts : /* empty */
298 c39be742 2021-07-09 op | locopts locopt optnl
301 c74c7030 2021-07-19 op locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
302 c74c7030 2021-07-19 op | BLOCK RETURN NUM string {
303 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
304 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
305 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
307 c74c7030 2021-07-19 op | BLOCK RETURN NUM {
308 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
309 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
310 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
311 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
312 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
315 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
316 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
317 6abda252 2021-02-06 op loc->block_code = 40;
319 c74c7030 2021-07-19 op | DEFAULT TYPE string {
320 c705ecb1 2021-05-03 op only_once(loc->default_mime, "default type");
321 eb59f87e 2021-02-09 op loc->default_mime = $3;
323 abc8801d 2021-07-19 op | FASTCGI fastcgi
324 c74c7030 2021-07-19 op | INDEX string {
325 c705ecb1 2021-05-03 op only_once(loc->index, "index");
326 eb59f87e 2021-02-09 op loc->index = $2;
328 c74c7030 2021-07-19 op | LANG string {
329 c705ecb1 2021-05-03 op only_once(loc->lang, "lang");
330 eb59f87e 2021-02-09 op loc->lang = $2;
332 c74c7030 2021-07-19 op | LOG bool { loc->disable_log = !$2; }
334 da2185f3 2021-01-01 op | REQUIRE CLIENT CA string {
335 da2185f3 2021-01-01 op only_once(loc->reqca, "require client ca");
336 da2185f3 2021-01-01 op ensure_absolute_path($4);
337 da2185f3 2021-01-01 op if ((loc->reqca = load_ca($4)) == NULL)
338 da2185f3 2021-01-01 op yyerror("couldn't load ca cert: %s", $4);
341 da2185f3 2021-01-01 op | ROOT string {
342 da2185f3 2021-01-01 op only_once(loc->dir, "root");
343 da2185f3 2021-01-01 op loc->dir = ensure_absolute_path($2);
345 da2185f3 2021-01-01 op | STRIP NUM { loc->strip = check_strip_no($2); }
348 da2185f3 2021-01-01 op proxy : PROXY proxy_opt
349 da2185f3 2021-01-01 op | PROXY '{' optnl proxy_opts '}'
352 da2185f3 2021-01-01 op proxy_opts : /* empty */
353 da2185f3 2021-01-01 op | proxy_opts proxy_opt optnl
356 da2185f3 2021-01-01 op proxy_opt : RELAY_TO string {
358 72b033ef 2021-12-29 op const char *errstr;
360 72b033ef 2021-12-29 op only_once(loc->proxy_host, "proxy relay-to");
361 da2185f3 2021-01-01 op loc->proxy_host = $2;
363 da2185f3 2021-01-01 op if ((at = strchr($2, ':')) != NULL) {
364 72b033ef 2021-12-29 op *at++ = '\0';
365 72b033ef 2021-12-29 op loc->proxy_port = at;
367 72b033ef 2021-12-29 op loc->proxy_port = "1965";
369 72b033ef 2021-12-29 op strtonum(loc->proxy_port, 1, UINT16_MAX, &errstr);
370 72b033ef 2021-12-29 op if (errstr != NULL)
371 72b033ef 2021-12-29 op yyerror("proxy port is %s: %s", errstr,
372 72b033ef 2021-12-29 op loc->proxy_port);
376 c74c7030 2021-07-19 op fastcgi : SPAWN string {
377 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
378 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf(NULL, NULL, $2);
381 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
382 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($1, NULL, NULL);
384 c74c7030 2021-07-19 op | TCP string PORT NUM {
386 762b9b99 2021-07-09 op if (asprintf(&c, "%d", $4) == -1)
387 0d047efc 2021-05-24 op err(1, "asprintf");
388 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
389 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, c, NULL);
391 c74c7030 2021-07-19 op | TCP string {
392 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
393 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
395 c74c7030 2021-07-19 op | TCP string PORT string {
396 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
397 762b9b99 2021-07-09 op loc->fcgi = fastcgi_conf($2, $4, NULL);
401 c39be742 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
402 67f49405 2021-07-09 op | ';' optnl /* semicolons too */
408 74f0778b 2021-06-16 op static struct keyword {
409 74f0778b 2021-06-16 op const char *word;
411 74f0778b 2021-06-16 op } keywords[] = {
412 d93c8191 2021-07-09 op /* these MUST be sorted */
413 c74c7030 2021-07-19 op {"alias", ALIAS},
414 c74c7030 2021-07-19 op {"auto", AUTO},
415 c74c7030 2021-07-19 op {"block", BLOCK},
417 c74c7030 2021-07-19 op {"cert", CERT},
418 c74c7030 2021-07-19 op {"cgi", CGI},
419 c74c7030 2021-07-19 op {"chroot", CHROOT},
420 c74c7030 2021-07-19 op {"client", CLIENT},
421 c74c7030 2021-07-19 op {"default", DEFAULT},
422 c74c7030 2021-07-19 op {"entrypoint", ENTRYPOINT},
423 c74c7030 2021-07-19 op {"env", ENV},
424 abc8801d 2021-07-19 op {"fastcgi", FASTCGI},
425 c74c7030 2021-07-19 op {"index", INDEX},
426 c74c7030 2021-07-19 op {"ipv6", IPV6},
427 c74c7030 2021-07-19 op {"key", KEY},
428 c74c7030 2021-07-19 op {"lang", LANG},
429 c74c7030 2021-07-19 op {"location", LOCATION},
430 c74c7030 2021-07-19 op {"log", LOG},
431 c74c7030 2021-07-19 op {"map", MAP},
432 c74c7030 2021-07-19 op {"mime", MIME},
433 ff05125e 2021-10-15 op {"ocsp", OCSP},
434 c74c7030 2021-07-19 op {"off", OFF},
436 c74c7030 2021-07-19 op {"param", PARAM},
437 c74c7030 2021-07-19 op {"port", PORT},
438 c74c7030 2021-07-19 op {"prefork", PREFORK},
439 c74c7030 2021-07-19 op {"protocols", PROTOCOLS},
440 72b033ef 2021-12-29 op {"proxy", PROXY},
441 72b033ef 2021-12-29 op {"relay-to", RELAY_TO},
442 c74c7030 2021-07-19 op {"require", REQUIRE},
443 c74c7030 2021-07-19 op {"return", RETURN},
444 c74c7030 2021-07-19 op {"root", ROOT},
445 c74c7030 2021-07-19 op {"server", SERVER},
446 c74c7030 2021-07-19 op {"spawn", SPAWN},
447 c74c7030 2021-07-19 op {"strip", STRIP},
448 c74c7030 2021-07-19 op {"tcp", TCP},
449 c74c7030 2021-07-19 op {"to-ext", TOEXT},
450 c74c7030 2021-07-19 op {"type", TYPE},
451 c74c7030 2021-07-19 op {"user", USER},
455 c39be742 2021-07-09 op yyerror(const char *msg, ...)
459 c39be742 2021-07-09 op file->errors++;
461 c39be742 2021-07-09 op va_start(ap, msg);
462 f3966209 2021-07-13 op fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
463 f3966209 2021-07-13 op vfprintf(stderr, msg, ap);
464 f3966209 2021-07-13 op fprintf(stderr, "\n");
469 f3966209 2021-07-13 op yywarn(const char *msg, ...)
473 f3966209 2021-07-13 op va_start(ap, msg);
474 f3966209 2021-07-13 op fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
475 c39be742 2021-07-09 op vfprintf(stderr, msg, ap);
476 c39be742 2021-07-09 op fprintf(stderr, "\n");
481 d93c8191 2021-07-09 op kw_cmp(const void *k, const void *e)
483 d93c8191 2021-07-09 op return strcmp(k, ((struct keyword *)e)->word);
487 c39be742 2021-07-09 op lookup(char *s)
489 c39be742 2021-07-09 op const struct keyword *p;
491 c39be742 2021-07-09 op p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
492 c39be742 2021-07-09 op sizeof(keywords[0]), kw_cmp);
495 c39be742 2021-07-09 op return p->token;
497 c39be742 2021-07-09 op return STRING;
500 c39be742 2021-07-09 op #define START_EXPAND 1
501 c39be742 2021-07-09 op #define DONE_EXPAND 2
503 c39be742 2021-07-09 op static int expanding;
511 c39be742 2021-07-09 op if (file->ungetpos > 0)
512 c39be742 2021-07-09 op c = file->ungetbuf[--file->ungetpos];
514 c39be742 2021-07-09 op c = getc(file->stream);
516 c39be742 2021-07-09 op if (c == START_EXPAND)
517 c39be742 2021-07-09 op expanding = 1;
518 c39be742 2021-07-09 op else if (c == DONE_EXPAND)
519 c39be742 2021-07-09 op expanding = 0;
527 c39be742 2021-07-09 op lgetc(int quotec)
531 c39be742 2021-07-09 op if (quotec) {
532 c39be742 2021-07-09 op if ((c = igetc()) == EOF) {
533 c39be742 2021-07-09 op yyerror("reached end of file while parsing "
534 c39be742 2021-07-09 op "quoted string");
535 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
537 c39be742 2021-07-09 op return quotec;
542 c39be742 2021-07-09 op while ((c = igetc()) == '\\') {
543 c39be742 2021-07-09 op next = igetc();
544 c39be742 2021-07-09 op if (next != '\n') {
548 c39be742 2021-07-09 op yylval.lineno = file->lineno;
549 c39be742 2021-07-09 op file->lineno++;
552 c39be742 2021-07-09 op if (c == EOF) {
554 c39be742 2021-07-09 op * Fake EOL when hit EOF for the first time. This gets line
555 c39be742 2021-07-09 op * count right if last line in included file is syntactically
556 c39be742 2021-07-09 op * invalid and has no newline.
558 c39be742 2021-07-09 op if (file->eof_reached == 0) {
559 c39be742 2021-07-09 op file->eof_reached = 1;
562 c39be742 2021-07-09 op while (c == EOF) {
563 c39be742 2021-07-09 op if (file == topfile || popfile() == EOF)
572 c39be742 2021-07-09 op lungetc(int c)
574 c39be742 2021-07-09 op if (c == EOF)
577 c39be742 2021-07-09 op if (file->ungetpos >= file->ungetsize) {
578 c39be742 2021-07-09 op void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
579 c39be742 2021-07-09 op if (p == NULL)
580 c39be742 2021-07-09 op err(1, "lungetc");
581 c39be742 2021-07-09 op file->ungetbuf = p;
582 c39be742 2021-07-09 op file->ungetsize *= 2;
584 c39be742 2021-07-09 op file->ungetbuf[file->ungetpos++] = c;
588 c39be742 2021-07-09 op findeol(void)
592 c39be742 2021-07-09 op /* Skip to either EOF or the first real EOL. */
594 c39be742 2021-07-09 op c = lgetc(0);
595 c39be742 2021-07-09 op if (c == '\n') {
596 c39be742 2021-07-09 op file->lineno++;
599 c39be742 2021-07-09 op if (c == EOF)
602 c39be742 2021-07-09 op return ERROR;
608 1bd706dc 2021-07-09 op char buf[8096];
609 1bd706dc 2021-07-09 op char *p, *val;
610 1bd706dc 2021-07-09 op int quotec, next, c;
615 c39be742 2021-07-09 op while ((c = lgetc(0)) == ' ' || c == '\t')
616 c39be742 2021-07-09 op ; /* nothing */
618 c39be742 2021-07-09 op yylval.lineno = file->lineno;
619 c39be742 2021-07-09 op if (c == '#')
620 c39be742 2021-07-09 op while ((c = lgetc(0)) != '\n' && c != EOF)
621 c39be742 2021-07-09 op ; /* nothing */
622 c39be742 2021-07-09 op if (c == '$' && !expanding) {
624 c39be742 2021-07-09 op if ((c = lgetc(0)) == EOF)
626 67f49405 2021-07-09 op if (p + 1 >= buf + sizeof(buf) -1) {
627 67f49405 2021-07-09 op yyerror("string too long");
628 67f49405 2021-07-09 op return findeol();
630 67f49405 2021-07-09 op if (isalnum(c) || c == '_') {
638 67f49405 2021-07-09 op val = symget(buf);
639 67f49405 2021-07-09 op if (val == NULL) {
640 67f49405 2021-07-09 op yyerror("macro `%s' not defined", buf);
641 67f49405 2021-07-09 op return findeol();
643 67f49405 2021-07-09 op yylval.v.string = xstrdup(val);
644 67f49405 2021-07-09 op return STRING;
646 67f49405 2021-07-09 op if (c == '@' && !expanding) {
648 67f49405 2021-07-09 op if ((c = lgetc(0)) == EOF)
651 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
652 c39be742 2021-07-09 op yyerror("string too long");
653 c39be742 2021-07-09 op return findeol();
655 c39be742 2021-07-09 op if (isalnum(c) || c == '_') {
663 c39be742 2021-07-09 op val = symget(buf);
664 c39be742 2021-07-09 op if (val == NULL) {
665 c39be742 2021-07-09 op yyerror("macro '%s' not defined", buf);
666 c39be742 2021-07-09 op return findeol();
668 c39be742 2021-07-09 op p = val + strlen(val) - 1;
669 c39be742 2021-07-09 op lungetc(DONE_EXPAND);
670 c39be742 2021-07-09 op while (p >= val) {
674 c39be742 2021-07-09 op lungetc(START_EXPAND);
683 c39be742 2021-07-09 op if ((c = lgetc(quotec)) == EOF)
685 c39be742 2021-07-09 op if (c == '\n') {
686 c39be742 2021-07-09 op file->lineno++;
688 c39be742 2021-07-09 op } else if (c == '\\') {
689 c39be742 2021-07-09 op if ((next = lgetc(quotec)) == EOF)
691 c39be742 2021-07-09 op if (next == quotec || next == ' ' ||
692 c39be742 2021-07-09 op next == '\t')
694 c39be742 2021-07-09 op else if (next == '\n') {
695 c39be742 2021-07-09 op file->lineno++;
698 c39be742 2021-07-09 op lungetc(next);
699 c39be742 2021-07-09 op } else if (c == quotec) {
702 c39be742 2021-07-09 op } else if (c == '\0') {
703 f3966209 2021-07-13 op yyerror("invalid syntax");
704 c39be742 2021-07-09 op return findeol();
706 c39be742 2021-07-09 op if (p + 1 >= buf + sizeof(buf) - 1) {
707 c39be742 2021-07-09 op yyerror("string too long");
708 c39be742 2021-07-09 op return findeol();
712 c39be742 2021-07-09 op yylval.v.string = strdup(buf);
713 c39be742 2021-07-09 op if (yylval.v.string == NULL)
714 c39be742 2021-07-09 op err(1, "yylex: strdup");
715 c39be742 2021-07-09 op return STRING;
718 c39be742 2021-07-09 op #define allowed_to_end_number(x) \
719 c39be742 2021-07-09 op (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
721 c39be742 2021-07-09 op if (c == '-' || isdigit(c)) {
724 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
725 c39be742 2021-07-09 op yyerror("string too long");
726 c39be742 2021-07-09 op return findeol();
728 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && isdigit(c));
730 c39be742 2021-07-09 op if (p == buf + 1 && buf[0] == '-')
731 c39be742 2021-07-09 op goto nodigits;
732 c39be742 2021-07-09 op if (c == EOF || allowed_to_end_number(c)) {
733 c39be742 2021-07-09 op const char *errstr = NULL;
736 c39be742 2021-07-09 op yylval.v.number = strtonum(buf, LLONG_MIN,
737 c39be742 2021-07-09 op LLONG_MAX, &errstr);
738 c39be742 2021-07-09 op if (errstr) {
739 c39be742 2021-07-09 op yyerror("\"%s\" invalid number: %s",
740 c39be742 2021-07-09 op buf, errstr);
741 c39be742 2021-07-09 op return findeol();
746 c39be742 2021-07-09 op while (p > buf + 1)
747 c39be742 2021-07-09 op lungetc(*--p);
749 c39be742 2021-07-09 op if (c == '-')
754 c39be742 2021-07-09 op #define allowed_in_string(x) \
755 c39be742 2021-07-09 op (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
756 c39be742 2021-07-09 op x != '{' && x != '}' && \
757 c39be742 2021-07-09 op x != '!' && x != '=' && x != '#' && \
758 67f49405 2021-07-09 op x != ',' && x != ';'))
760 67f49405 2021-07-09 op if (isalnum(c) || c == ':' || c == '_') {
763 c39be742 2021-07-09 op if ((size_t)(p-buf) >= sizeof(buf)) {
764 c39be742 2021-07-09 op yyerror("string too long");
765 c39be742 2021-07-09 op return findeol();
767 c39be742 2021-07-09 op } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
770 c39be742 2021-07-09 op if ((token = lookup(buf)) == STRING)
771 c39be742 2021-07-09 op yylval.v.string = xstrdup(buf);
772 c39be742 2021-07-09 op return token;
774 c39be742 2021-07-09 op if (c == '\n') {
775 c39be742 2021-07-09 op yylval.lineno = file->lineno;
776 c39be742 2021-07-09 op file->lineno++;
778 c39be742 2021-07-09 op if (c == EOF)
783 c39be742 2021-07-09 op struct file *
784 c39be742 2021-07-09 op pushfile(const char *name, int secret)
786 c39be742 2021-07-09 op struct file *nfile;
788 c39be742 2021-07-09 op nfile = xcalloc(1, sizeof(*nfile));
789 c39be742 2021-07-09 op nfile->name = xstrdup(name);
790 c39be742 2021-07-09 op if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
791 83272dfe 2021-08-23 op log_warn(NULL, "can't open %s: %s", nfile->name,
792 c39be742 2021-07-09 op strerror(errno));
793 c39be742 2021-07-09 op free(nfile->name);
797 c39be742 2021-07-09 op nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
798 c39be742 2021-07-09 op nfile->ungetsize = 16;
799 c39be742 2021-07-09 op nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
800 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&files, nfile, entry);
801 c39be742 2021-07-09 op return nfile;
805 c39be742 2021-07-09 op popfile(void)
807 c39be742 2021-07-09 op struct file *prev;
809 c39be742 2021-07-09 op if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
810 c39be742 2021-07-09 op prev->errors += file->errors;
812 c39be742 2021-07-09 op TAILQ_REMOVE(&files, file, entry);
813 c39be742 2021-07-09 op fclose(file->stream);
814 c39be742 2021-07-09 op free(file->name);
815 c39be742 2021-07-09 op free(file->ungetbuf);
818 c39be742 2021-07-09 op return file ? 0 : EOF;
822 c39be742 2021-07-09 op parse_conf(const char *filename)
824 c39be742 2021-07-09 op struct sym *sym, *next;
826 c39be742 2021-07-09 op file = pushfile(filename, 0);
827 c39be742 2021-07-09 op if (file == NULL)
829 c39be742 2021-07-09 op topfile = file;
832 c39be742 2021-07-09 op errors = file->errors;
835 c39be742 2021-07-09 op /* Free macros and check which have not been used. */
836 3b21cca3 2021-06-29 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
837 3b21cca3 2021-06-29 op /* TODO: warn if !sym->used */
838 3b21cca3 2021-06-29 op if (!sym->persist) {
839 3b21cca3 2021-06-29 op free(sym->name);
840 3b21cca3 2021-06-29 op free(sym->val);
841 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
851 f0a01fc7 2021-10-09 op print_conf(void)
853 f0a01fc7 2021-10-09 op struct vhost *h;
854 f0a01fc7 2021-10-09 op /* struct location *l; */
855 f0a01fc7 2021-10-09 op /* struct envlist *e; */
856 f0a01fc7 2021-10-09 op /* struct alist *a; */
858 f0a01fc7 2021-10-09 op if (conf.chroot != NULL)
859 f0a01fc7 2021-10-09 op printf("chroot \"%s\"\n", conf.chroot);
860 f0a01fc7 2021-10-09 op printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
861 f0a01fc7 2021-10-09 op /* XXX: defined mimes? */
862 f0a01fc7 2021-10-09 op printf("port %d\n", conf.port);
863 f0a01fc7 2021-10-09 op printf("prefork %d\n", conf.prefork);
864 f0a01fc7 2021-10-09 op /* XXX: protocols? */
865 f0a01fc7 2021-10-09 op if (conf.user != NULL)
866 f0a01fc7 2021-10-09 op printf("user \"%s\"\n", conf.user);
868 f0a01fc7 2021-10-09 op TAILQ_FOREACH(h, &hosts, vhosts) {
869 f0a01fc7 2021-10-09 op printf("\nserver \"%s\" {\n", h->domain);
870 f0a01fc7 2021-10-09 op printf(" cert \"%s\"\n", h->cert);
871 f0a01fc7 2021-10-09 op printf(" key \"%s\"\n", h->key);
872 f0a01fc7 2021-10-09 op /* TODO: print locations... */
873 f0a01fc7 2021-10-09 op printf("}\n");
878 c39be742 2021-07-09 op symset(const char *name, const char *val, int persist)
880 c39be742 2021-07-09 op struct sym *sym;
882 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
883 c39be742 2021-07-09 op if (!strcmp(name, sym->name))
887 c39be742 2021-07-09 op if (sym != NULL) {
888 c39be742 2021-07-09 op if (sym->persist)
891 c39be742 2021-07-09 op free(sym->name);
892 c39be742 2021-07-09 op free(sym->val);
893 c39be742 2021-07-09 op TAILQ_REMOVE(&symhead, sym, entry);
898 c39be742 2021-07-09 op sym = xcalloc(1, sizeof(*sym));
899 c39be742 2021-07-09 op sym->name = xstrdup(name);
900 c39be742 2021-07-09 op sym->val = xstrdup(val);
901 c39be742 2021-07-09 op sym->used = 0;
902 c39be742 2021-07-09 op sym->persist = persist;
904 c39be742 2021-07-09 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
909 c39be742 2021-07-09 op cmdline_symset(char *s)
911 c39be742 2021-07-09 op char *sym, *val;
914 c39be742 2021-07-09 op if ((val = strrchr(s, '=')) == NULL)
916 c39be742 2021-07-09 op sym = xcalloc(1, val - s + 1);
917 c39be742 2021-07-09 op memcpy(sym, s, val - s);
918 c39be742 2021-07-09 op ret = symset(sym, val + 1, 1);
924 c39be742 2021-07-09 op symget(const char *nam)
926 c39be742 2021-07-09 op struct sym *sym;
928 c39be742 2021-07-09 op TAILQ_FOREACH(sym, &symhead, entry) {
929 c39be742 2021-07-09 op if (strcmp(nam, sym->name) == 0) {
930 c39be742 2021-07-09 op sym->used = 1;
931 c39be742 2021-07-09 op return sym->val;
937 c39be742 2021-07-09 op struct vhost *
938 c39be742 2021-07-09 op new_vhost(void)
940 c39be742 2021-07-09 op return xcalloc(1, sizeof(struct vhost));
943 c39be742 2021-07-09 op struct location *
944 c39be742 2021-07-09 op new_location(void)
946 c39be742 2021-07-09 op struct location *l;
948 c39be742 2021-07-09 op l = xcalloc(1, sizeof(*l));
949 c39be742 2021-07-09 op l->dirfd = -1;
950 c39be742 2021-07-09 op l->fcgi = -1;
955 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
957 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
958 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
963 6abda252 2021-02-06 op check_block_code(int n)
965 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
966 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
971 6abda252 2021-02-06 op check_block_fmt(char *fmt)
975 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
976 6abda252 2021-02-06 op if (*s != '%')
978 6abda252 2021-02-06 op switch (*++s) {
986 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
994 6abda252 2021-02-06 op check_strip_no(int n)
997 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
1002 391825e3 2021-07-09 op check_port_num(int n)
1004 391825e3 2021-07-09 op if (n <= 0 || n >= UINT16_MAX)
1005 391825e3 2021-07-09 op yyerror("port number is %s: %d",
1006 391825e3 2021-07-09 op n <= 0 ? "too small" : "too large",
1012 a709ddf5 2021-02-07 op check_prefork_num(int n)
1014 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
1015 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
1020 49b73ba1 2021-02-10 op advance_loc(void)
1022 b8e64ccd 2021-03-31 op loc = new_location();
1023 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1027 c705ecb1 2021-05-03 op only_once(const void *ptr, const char *name)
1029 c705ecb1 2021-05-03 op if (ptr != NULL)
1030 c705ecb1 2021-05-03 op yyerror("`%s' specified more than once", name);
1034 8ad1c570 2021-05-09 op only_oncei(int i, const char *name)
1036 8ad1c570 2021-05-09 op if (i != -1)
1037 8ad1c570 2021-05-09 op yyerror("`%s' specified more than once", name);
1041 8ad1c570 2021-05-09 op fastcgi_conf(char *path, char *port, char *prog)
1043 8ad1c570 2021-05-09 op struct fcgi *f;
1046 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
1047 8ad1c570 2021-05-09 op f = &fcgi[i];
1049 8ad1c570 2021-05-09 op if (f->path == NULL) {
1051 8ad1c570 2021-05-09 op f->path = path;
1052 8ad1c570 2021-05-09 op f->port = port;
1053 8ad1c570 2021-05-09 op f->prog = prog;
1057 8ad1c570 2021-05-09 op /* XXX: what to do with prog? */
1058 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
1059 8ad1c570 2021-05-09 op ((port == NULL && f->port == NULL) ||
1060 8ad1c570 2021-05-09 op !strcmp(f->port, port))) {
1067 8ad1c570 2021-05-09 op yyerror("too much `fastcgi' rules defined.");
1072 c92b802b 2021-06-11 op add_param(char *name, char *val, int env)
1074 c92b802b 2021-06-11 op struct envlist *e;
1075 c92b802b 2021-06-11 op struct envhead *h;
1078 c92b802b 2021-06-11 op h = &host->env;
1080 c92b802b 2021-06-11 op h = &host->params;
1082 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
1083 c92b802b 2021-06-11 op e->name = name;
1084 c92b802b 2021-06-11 op e->value = val;
1085 c92b802b 2021-06-11 op if (TAILQ_EMPTY(h))
1086 c92b802b 2021-06-11 op TAILQ_INSERT_HEAD(h, e, envs);
1088 c92b802b 2021-06-11 op TAILQ_INSERT_TAIL(h, e, envs);