Blob

Date:
Fri Apr 8 13:52:35 2022 UTC
Message:
don't load the built-in list when using `types'

1%{
2
3/*
4 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
5 * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
6 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
7 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
8 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
9 * Copyright (c) 2001 Markus Friedl. All rights reserved.
10 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
11 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
12 *
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 */
26#include "gmid.h"
28#include <ctype.h>
29#include <errno.h>
30#include <stdarg.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
35TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
36static struct file {
37 TAILQ_ENTRY(file) entry;
38 FILE *stream;
39 char *name;
40 size_t ungetpos;
41 size_t ungetsize;
42 u_char *ungetbuf;
43 int eof_reached;
44 int lineno;
45 int errors;
46} *file, *topfile;
48struct file *pushfile(const char *, int);
49int popfile(void);
50int yyparse(void);
51int yylex(void);
52void yyerror(const char *, ...)
53 __attribute__((__format__ (printf, 1, 2)))
54 __attribute__((__nonnull__ (1)));
55void yywarn(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58int kw_cmp(const void *, const void *);
59int lookup(char *);
60int igetc(void);
61int lgetc(int);
62void lungetc(int);
63int findeol(void);
65/*
66 * #define YYDEBUG 1
67 * int yydebug = 1;
68 */
70TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
71struct sym {
72 TAILQ_ENTRY(sym) entry;
73 int used;
74 int persist;
75 char *name;
76 char *val;
77};
79int symset(const char *, const char *, int);
80char *symget(const char *);
82struct vhost *new_vhost(void);
83struct location *new_location(void);
84struct proxy *new_proxy(void);
85char *ensure_absolute_path(char*);
86int check_block_code(int);
87char *check_block_fmt(char*);
88int check_strip_no(int);
89int check_port_num(int);
90int check_prefork_num(int);
91void advance_loc(void);
92void advance_proxy(void);
93void parsehp(char *, char **, const char **, const char *);
94void only_once(const void*, const char*);
95void only_oncei(int, const char*);
96int fastcgi_conf(char *, char *, char *);
97void add_param(char *, char *, int);
99static struct vhost *host;
100static struct location *loc;
101static struct proxy *proxy;
102static char *current_media;
103static int errors;
105typedef struct {
106 union {
107 char *string;
108 int number;
109 } v;
110 int lineno;
111} YYSTYPE;
113%}
115/* for bison: */
116/* %define parse.error verbose */
118%token ALIAS AUTO
119%token BLOCK
120%token CA CERT CGI CHROOT CLIENT
121%token DEFAULT
122%token ENTRYPOINT ENV
123%token FASTCGI FOR_HOST
124%token INCLUDE INDEX IPV6
125%token KEY
126%token LANG LOCATION LOG
127%token MAP MIME
128%token OCSP OFF ON
129%token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
130%token RELAY_TO REQUIRE RETURN ROOT
131%token SERVER SNI SPAWN STRIP
132%token TCP TOEXT TYPE TYPES
133%token USE_TLS USER
134%token VERIFYNAME
136%token ERROR
138%token <v.string> STRING
139%token <v.number> NUM
141%type <v.number> bool
142%type <v.string> string numberstring
144%%
146conf : /* empty */
147 | conf include '\n'
148 | conf '\n'
149 | conf varset '\n'
150 | conf option '\n'
151 | conf vhost '\n'
152 | conf types '\n'
153 | conf error '\n' { file->errors++; }
154 ;
156include : INCLUDE STRING {
157 struct file *nfile;
159 if ((nfile = pushfile($2, 0)) == NULL) {
160 yyerror("failed to include file %s", $2);
161 free($2);
162 YYERROR;
163 }
164 free($2);
166 file = nfile;
167 lungetc('\n');
168 }
169 ;
171bool : ON { $$ = 1; }
172 | OFF { $$ = 0; }
173 ;
175string : string STRING {
176 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
177 free($1);
178 free($2);
179 yyerror("string: asprintf: %s", strerror(errno));
180 YYERROR;
181 }
182 free($1);
183 free($2);
184 }
185 | STRING
186 ;
188numberstring : NUM {
189 char *s;
190 if (asprintf(&s, "%d", $1) == -1) {
191 yyerror("asprintf: number");
192 YYERROR;
193 }
194 $$ = s;
195 }
196 | STRING
197 ;
199varset : STRING '=' string {
200 char *s = $1;
201 while (*s++) {
202 if (isspace((unsigned char)*s)) {
203 yyerror("macro name cannot contain "
204 "whitespaces");
205 free($1);
206 free($3);
207 YYERROR;
208 }
209 }
210 symset($1, $3, 0);
211 free($1);
212 free($3);
213 }
214 ;
216option : CHROOT string { conf.chroot = $2; }
217 | IPV6 bool { conf.ipv6 = $2; }
218 | MIME STRING string {
219 yywarn("`mime MIME EXT' is deprecated and will be "
220 "removed in a future version, please use the new "
221 "`types' block.");
222 if (add_mime(&conf.mime, $2, $3) == -1)
223 err(1, "add_mime");
224 }
225 | MAP string TOEXT string {
226 yywarn("`map mime to-ext' is deprecated and will be "
227 "removed in a future version, please use the new "
228 "`types' block.");
229 if (add_mime(&conf.mime, $2, $4) == -1)
230 err(1, "add_mime");
231 }
232 | PORT NUM { conf.port = check_port_num($2); }
233 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
234 | PROTOCOLS string {
235 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
236 yyerror("invalid protocols string \"%s\"", $2);
237 free($2);
238 }
239 | USER string { conf.user = $2; }
240 ;
242vhost : SERVER string {
243 host = new_vhost();
244 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
246 loc = new_location();
247 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
249 TAILQ_INIT(&host->proxies);
251 loc->match = xstrdup("*");
252 host->domain = $2;
254 if (strstr($2, "xn--") != NULL) {
255 yywarn("\"%s\" looks like punycode: you "
256 "should use the decoded hostname", $2);
257 }
258 } '{' optnl servbody '}' {
259 if (host->cert == NULL || host->key == NULL)
260 yyerror("invalid vhost definition: %s", $2);
261 }
262 | error '}' { yyerror("bad server directive"); }
263 ;
265servbody : /* empty */
266 | servbody servopt optnl
267 | servbody location optnl
268 | servbody proxy optnl
269 ;
271servopt : ALIAS string {
272 struct alist *a;
274 a = xcalloc(1, sizeof(*a));
275 a->alias = $2;
276 if (TAILQ_EMPTY(&host->aliases))
277 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
278 else
279 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
280 }
281 | CERT string {
282 only_once(host->cert, "cert");
283 host->cert = ensure_absolute_path($2);
284 }
285 | CGI string {
286 only_once(host->cgi, "cgi");
287 /* drop the starting '/', if any */
288 if (*$2 == '/')
289 memmove($2, $2+1, strlen($2));
290 host->cgi = $2;
291 }
292 | ENTRYPOINT string {
293 only_once(host->entrypoint, "entrypoint");
294 while (*$2 == '/')
295 memmove($2, $2+1, strlen($2));
296 host->entrypoint = $2;
297 }
298 | ENV string '=' string {
299 add_param($2, $4, 1);
300 }
301 | KEY string {
302 only_once(host->key, "key");
303 host->key = ensure_absolute_path($2);
304 }
305 | OCSP string {
306 only_once(host->ocsp, "ocsp");
307 host->ocsp = ensure_absolute_path($2);
308 }
309 | PARAM string '=' string {
310 add_param($2, $4, 0);
311 }
312 | locopt
313 ;
315proxy : PROXY { advance_proxy(); }
316 proxy_matches '{' optnl proxy_opts '}' {
317 if (proxy->host == NULL)
318 yyerror("invalid proxy block: missing `relay-to' option");
320 if ((proxy->cert == NULL && proxy->key != NULL) ||
321 (proxy->cert != NULL && proxy->key == NULL))
322 yyerror("invalid proxy block: missing cert or key");
323 }
324 ;
326proxy_matches : /* empty */
327 | proxy_matches proxy_match
328 ;
330proxy_match : PROTO string {
331 only_once(proxy->match_proto, "proxy proto");
332 free(proxy->match_proto);
333 proxy->match_proto = $2;
334 }
335 | FOR_HOST string {
336 only_once(proxy->match_host, "proxy for-host");
337 free(proxy->match_host);
338 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
339 }
340 ;
342proxy_opts : /* empty */
343 | proxy_opts proxy_opt optnl
344 ;
346proxy_opt : CERT string {
347 only_once(proxy->cert, "proxy cert");
348 tls_unload_file(proxy->cert, proxy->certlen);
349 ensure_absolute_path($2);
350 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
351 if (proxy->cert == NULL)
352 yyerror("can't load cert %s", $2);
353 free($2);
354 }
355 | KEY string {
356 only_once(proxy->key, "proxy key");
357 tls_unload_file(proxy->key, proxy->keylen);
358 ensure_absolute_path($2);
359 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
360 if (proxy->key == NULL)
361 yyerror("can't load key %s", $2);
362 free($2);
363 }
364 | PROTOCOLS string {
365 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
366 yyerror("invalid protocols string \"%s\"", $2);
367 free($2);
368 }
369 | RELAY_TO string {
370 only_once(proxy->host, "proxy relay-to");
371 free(proxy->host);
372 parsehp($2, &proxy->host, &proxy->port, "1965");
373 }
374 | REQUIRE CLIENT CA string {
375 only_once(proxy->reqca, "require client ca");
376 ensure_absolute_path($4);
377 if ((proxy->reqca = load_ca($4)) == NULL)
378 yyerror("couldn't load ca cert: %s", $4);
379 free($4);
380 }
381 | SNI string {
382 only_once(proxy->sni, "proxy sni");
383 free(proxy->sni);
384 proxy->sni = $2;
385 }
386 | USE_TLS bool {
387 proxy->notls = !$2;
388 }
389 | VERIFYNAME bool {
390 proxy->noverifyname = !$2;
391 }
392 ;
394location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
395 /* drop the starting '/' if any */
396 if (*$3 == '/')
397 memmove($3, $3+1, strlen($3));
398 loc->match = $3;
399 }
400 | error '}'
401 ;
403locopts : /* empty */
404 | locopts locopt optnl
405 ;
407locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
408 | BLOCK RETURN NUM string {
409 only_once(loc->block_fmt, "block");
410 loc->block_fmt = check_block_fmt($4);
411 loc->block_code = check_block_code($3);
412 }
413 | BLOCK RETURN NUM {
414 only_once(loc->block_fmt, "block");
415 loc->block_fmt = xstrdup("temporary failure");
416 loc->block_code = check_block_code($3);
417 if ($3 >= 30 && $3 < 40)
418 yyerror("missing `meta' for block return %d", $3);
419 }
420 | BLOCK {
421 only_once(loc->block_fmt, "block");
422 loc->block_fmt = xstrdup("temporary failure");
423 loc->block_code = 40;
424 }
425 | DEFAULT TYPE string {
426 only_once(loc->default_mime, "default type");
427 loc->default_mime = $3;
428 }
429 | FASTCGI fastcgi
430 | INDEX string {
431 only_once(loc->index, "index");
432 loc->index = $2;
433 }
434 | LANG string {
435 only_once(loc->lang, "lang");
436 loc->lang = $2;
437 }
438 | LOG bool { loc->disable_log = !$2; }
439 | REQUIRE CLIENT CA string {
440 only_once(loc->reqca, "require client ca");
441 ensure_absolute_path($4);
442 if ((loc->reqca = load_ca($4)) == NULL)
443 yyerror("couldn't load ca cert: %s", $4);
444 free($4);
445 }
446 | ROOT string {
447 only_once(loc->dir, "root");
448 loc->dir = ensure_absolute_path($2);
449 }
450 | STRIP NUM { loc->strip = check_strip_no($2); }
451 ;
453fastcgi : SPAWN string {
454 only_oncei(loc->fcgi, "fastcgi");
455 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
456 }
457 | string {
458 only_oncei(loc->fcgi, "fastcgi");
459 loc->fcgi = fastcgi_conf($1, NULL, NULL);
460 }
461 | TCP string PORT NUM {
462 char *c;
463 if (asprintf(&c, "%d", $4) == -1)
464 err(1, "asprintf");
465 only_oncei(loc->fcgi, "fastcgi");
466 loc->fcgi = fastcgi_conf($2, c, NULL);
467 }
468 | TCP string {
469 only_oncei(loc->fcgi, "fastcgi");
470 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
471 }
472 | TCP string PORT string {
473 only_oncei(loc->fcgi, "fastcgi");
474 loc->fcgi = fastcgi_conf($2, $4, NULL);
475 }
476 ;
478types : TYPES '{' optnl mediaopts_l '}' {
479 conf.mime.skip_defaults = 1;
480 }
481 ;
483mediaopts_l : mediaopts_l mediaoptsl nl
484 | mediaoptsl nl
485 ;
487mediaoptsl : STRING { current_media = $1; } medianames_l optsemicolon
488 | include
489 ;
491medianames_l : medianames_l medianamesl
492 | medianamesl
493 ;
495medianamesl : numberstring {
496 if (add_mime(&conf.mime, current_media, $1) == -1)
497 err(1, "add_mime");
498 }
499 ;
501nl : '\n' optnl
502 ;
504optnl : '\n' optnl /* zero or more newlines */
505 | ';' optnl /* semicolons too */
506 | /*empty*/
507 ;
509optsemicolon : ';'
510 |
511 ;
513%%
515static const struct keyword {
516 const char *word;
517 int token;
518} keywords[] = {
519 /* these MUST be sorted */
520 {"alias", ALIAS},
521 {"auto", AUTO},
522 {"block", BLOCK},
523 {"ca", CA},
524 {"cert", CERT},
525 {"cgi", CGI},
526 {"chroot", CHROOT},
527 {"client", CLIENT},
528 {"default", DEFAULT},
529 {"entrypoint", ENTRYPOINT},
530 {"env", ENV},
531 {"fastcgi", FASTCGI},
532 {"for-host", FOR_HOST},
533 {"include", INCLUDE},
534 {"index", INDEX},
535 {"ipv6", IPV6},
536 {"key", KEY},
537 {"lang", LANG},
538 {"location", LOCATION},
539 {"log", LOG},
540 {"map", MAP},
541 {"mime", MIME},
542 {"ocsp", OCSP},
543 {"off", OFF},
544 {"on", ON},
545 {"param", PARAM},
546 {"port", PORT},
547 {"prefork", PREFORK},
548 {"proto", PROTO},
549 {"protocols", PROTOCOLS},
550 {"proxy", PROXY},
551 {"relay-to", RELAY_TO},
552 {"require", REQUIRE},
553 {"return", RETURN},
554 {"root", ROOT},
555 {"server", SERVER},
556 {"sni", SNI},
557 {"spawn", SPAWN},
558 {"strip", STRIP},
559 {"tcp", TCP},
560 {"to-ext", TOEXT},
561 {"type", TYPE},
562 {"types", TYPES},
563 {"use-tls", USE_TLS},
564 {"user", USER},
565 {"verifyname", VERIFYNAME},
566};
568void
569yyerror(const char *msg, ...)
570{
571 va_list ap;
573 file->errors++;
575 va_start(ap, msg);
576 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
577 vfprintf(stderr, msg, ap);
578 fprintf(stderr, "\n");
579 va_end(ap);
580}
582void
583yywarn(const char *msg, ...)
584{
585 va_list ap;
587 va_start(ap, msg);
588 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
589 vfprintf(stderr, msg, ap);
590 fprintf(stderr, "\n");
591 va_end(ap);
592}
594int
595kw_cmp(const void *k, const void *e)
596{
597 return strcmp(k, ((struct keyword *)e)->word);
598}
600int
601lookup(char *s)
602{
603 const struct keyword *p;
605 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
606 sizeof(keywords[0]), kw_cmp);
608 if (p)
609 return p->token;
610 else
611 return STRING;
612}
614#define START_EXPAND 1
615#define DONE_EXPAND 2
617static int expanding;
619int
620igetc(void)
621{
622 int c;
624 while (1) {
625 if (file->ungetpos > 0)
626 c = file->ungetbuf[--file->ungetpos];
627 else
628 c = getc(file->stream);
630 if (c == START_EXPAND)
631 expanding = 1;
632 else if (c == DONE_EXPAND)
633 expanding = 0;
634 else
635 break;
636 }
637 return c;
638}
640int
641lgetc(int quotec)
642{
643 int c, next;
645 if (quotec) {
646 if ((c = igetc()) == EOF) {
647 yyerror("reached end of file while parsing "
648 "quoted string");
649 if (file == topfile || popfile() == EOF)
650 return EOF;
651 return quotec;
652 }
653 return c;
654 }
656 while ((c = igetc()) == '\\') {
657 next = igetc();
658 if (next != '\n') {
659 c = next;
660 break;
661 }
662 yylval.lineno = file->lineno;
663 file->lineno++;
664 }
666 if (c == EOF) {
667 /*
668 * Fake EOL when hit EOF for the first time. This gets line
669 * count right if last line in included file is syntactically
670 * invalid and has no newline.
671 */
672 if (file->eof_reached == 0) {
673 file->eof_reached = 1;
674 return '\n';
675 }
676 while (c == EOF) {
677 if (file == topfile || popfile() == EOF)
678 return EOF;
679 c = igetc();
680 }
681 }
682 return c;
683}
685void
686lungetc(int c)
687{
688 if (c == EOF)
689 return;
691 if (file->ungetpos >= file->ungetsize) {
692 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
693 if (p == NULL)
694 err(1, "lungetc");
695 file->ungetbuf = p;
696 file->ungetsize *= 2;
697 }
698 file->ungetbuf[file->ungetpos++] = c;
699}
701int
702findeol(void)
703{
704 int c;
706 /* Skip to either EOF or the first real EOL. */
707 while (1) {
708 c = lgetc(0);
709 if (c == '\n') {
710 file->lineno++;
711 break;
712 }
713 if (c == EOF)
714 break;
715 }
716 return ERROR;
717}
719int
720yylex(void)
721{
722 char buf[8096];
723 char *p, *val;
724 int quotec, next, c;
725 int token;
727top:
728 p = buf;
729 while ((c = lgetc(0)) == ' ' || c == '\t')
730 ; /* nothing */
732 yylval.lineno = file->lineno;
733 if (c == '#')
734 while ((c = lgetc(0)) != '\n' && c != EOF)
735 ; /* nothing */
736 if (c == '$' && !expanding) {
737 while (1) {
738 if ((c = lgetc(0)) == EOF)
739 return 0;
740 if (p + 1 >= buf + sizeof(buf) -1) {
741 yyerror("string too long");
742 return findeol();
743 }
744 if (isalnum(c) || c == '_') {
745 *p++ = c;
746 continue;
747 }
748 *p = '\0';
749 lungetc(c);
750 break;
751 }
752 val = symget(buf);
753 if (val == NULL) {
754 yyerror("macro `%s' not defined", buf);
755 return findeol();
756 }
757 yylval.v.string = xstrdup(val);
758 return STRING;
759 }
760 if (c == '@' && !expanding) {
761 while (1) {
762 if ((c = lgetc(0)) == EOF)
763 return 0;
765 if (p + 1 >= buf + sizeof(buf) - 1) {
766 yyerror("string too long");
767 return findeol();
768 }
769 if (isalnum(c) || c == '_') {
770 *p++ = c;
771 continue;
772 }
773 *p = '\0';
774 lungetc(c);
775 break;
776 }
777 val = symget(buf);
778 if (val == NULL) {
779 yyerror("macro '%s' not defined", buf);
780 return findeol();
781 }
782 p = val + strlen(val) - 1;
783 lungetc(DONE_EXPAND);
784 while (p >= val) {
785 lungetc(*p);
786 p--;
787 }
788 lungetc(START_EXPAND);
789 goto top;
790 }
792 switch (c) {
793 case '\'':
794 case '"':
795 quotec = c;
796 while (1) {
797 if ((c = lgetc(quotec)) == EOF)
798 return 0;
799 if (c == '\n') {
800 file->lineno++;
801 continue;
802 } else if (c == '\\') {
803 if ((next = lgetc(quotec)) == EOF)
804 return (0);
805 if (next == quotec || next == ' ' ||
806 next == '\t')
807 c = next;
808 else if (next == '\n') {
809 file->lineno++;
810 continue;
811 } else
812 lungetc(next);
813 } else if (c == quotec) {
814 *p = '\0';
815 break;
816 } else if (c == '\0') {
817 yyerror("invalid syntax");
818 return findeol();
819 }
820 if (p + 1 >= buf + sizeof(buf) - 1) {
821 yyerror("string too long");
822 return findeol();
823 }
824 *p++ = c;
825 }
826 yylval.v.string = strdup(buf);
827 if (yylval.v.string == NULL)
828 err(1, "yylex: strdup");
829 return STRING;
830 }
832#define allowed_to_end_number(x) \
833 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
835 if (c == '-' || isdigit(c)) {
836 do {
837 *p++ = c;
838 if ((size_t)(p-buf) >= sizeof(buf)) {
839 yyerror("string too long");
840 return findeol();
841 }
842 } while ((c = lgetc(0)) != EOF && isdigit(c));
843 lungetc(c);
844 if (p == buf + 1 && buf[0] == '-')
845 goto nodigits;
846 if (c == EOF || allowed_to_end_number(c)) {
847 const char *errstr = NULL;
849 *p = '\0';
850 yylval.v.number = strtonum(buf, LLONG_MIN,
851 LLONG_MAX, &errstr);
852 if (errstr) {
853 yyerror("\"%s\" invalid number: %s",
854 buf, errstr);
855 return findeol();
856 }
857 return NUM;
858 } else {
859nodigits:
860 while (p > buf + 1)
861 lungetc(*--p);
862 c = *--p;
863 if (c == '-')
864 return c;
865 }
866 }
868#define allowed_in_string(x) \
869 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
870 x != '{' && x != '}' && \
871 x != '!' && x != '=' && x != '#' && \
872 x != ',' && x != ';'))
874 if (isalnum(c) || c == ':' || c == '_') {
875 do {
876 *p++ = c;
877 if ((size_t)(p-buf) >= sizeof(buf)) {
878 yyerror("string too long");
879 return findeol();
880 }
881 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
882 lungetc(c);
883 *p = '\0';
884 if ((token = lookup(buf)) == STRING)
885 yylval.v.string = xstrdup(buf);
886 return token;
887 }
888 if (c == '\n') {
889 yylval.lineno = file->lineno;
890 file->lineno++;
891 }
892 if (c == EOF)
893 return 0;
894 return c;
895}
897struct file *
898pushfile(const char *name, int secret)
899{
900 struct file *nfile;
902 nfile = xcalloc(1, sizeof(*nfile));
903 nfile->name = xstrdup(name);
904 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
905 log_warn(NULL, "can't open %s: %s", nfile->name,
906 strerror(errno));
907 free(nfile->name);
908 free(nfile);
909 return NULL;
910 }
911 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
912 nfile->ungetsize = 16;
913 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
914 TAILQ_INSERT_TAIL(&files, nfile, entry);
915 return nfile;
916}
918int
919popfile(void)
920{
921 struct file *prev;
923 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
924 prev->errors += file->errors;
926 TAILQ_REMOVE(&files, file, entry);
927 fclose(file->stream);
928 free(file->name);
929 free(file->ungetbuf);
930 free(file);
931 file = prev;
932 return file ? 0 : EOF;
933}
935void
936parse_conf(const char *filename)
937{
938 struct sym *sym, *next;
940 file = pushfile(filename, 0);
941 if (file == NULL)
942 exit(1);
943 topfile = file;
945 yyparse();
946 errors = file->errors;
947 popfile();
949 /* Free macros and check which have not been used. */
950 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
951 /* TODO: warn if !sym->used */
952 if (!sym->persist) {
953 free(sym->name);
954 free(sym->val);
955 TAILQ_REMOVE(&symhead, sym, entry);
956 free(sym);
957 }
958 }
960 if (errors)
961 exit(1);
962}
964void
965print_conf(void)
966{
967 struct vhost *h;
968 /* struct location *l; */
969 /* struct envlist *e; */
970 /* struct alist *a; */
972 if (conf.chroot != NULL)
973 printf("chroot \"%s\"\n", conf.chroot);
974 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
975 /* XXX: defined mimes? */
976 printf("port %d\n", conf.port);
977 printf("prefork %d\n", conf.prefork);
978 /* XXX: protocols? */
979 if (conf.user != NULL)
980 printf("user \"%s\"\n", conf.user);
982 TAILQ_FOREACH(h, &hosts, vhosts) {
983 printf("\nserver \"%s\" {\n", h->domain);
984 printf(" cert \"%s\"\n", h->cert);
985 printf(" key \"%s\"\n", h->key);
986 /* TODO: print locations... */
987 printf("}\n");
988 }
989}
991int
992symset(const char *name, const char *val, int persist)
993{
994 struct sym *sym;
996 TAILQ_FOREACH(sym, &symhead, entry) {
997 if (!strcmp(name, sym->name))
998 break;
999 }
1001 if (sym != NULL) {
1002 if (sym->persist)
1003 return 0;
1004 else {
1005 free(sym->name);
1006 free(sym->val);
1007 TAILQ_REMOVE(&symhead, sym, entry);
1008 free(sym);
1009 }
1010 }
1012 sym = xcalloc(1, sizeof(*sym));
1013 sym->name = xstrdup(name);
1014 sym->val = xstrdup(val);
1015 sym->used = 0;
1016 sym->persist = persist;
1018 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1019 return 0;
1022int
1023cmdline_symset(char *s)
1025 char *sym, *val;
1026 int ret;
1028 if ((val = strrchr(s, '=')) == NULL)
1029 return -1;
1030 sym = xcalloc(1, val - s + 1);
1031 memcpy(sym, s, val - s);
1032 ret = symset(sym, val + 1, 1);
1033 free(sym);
1034 return ret;
1037char *
1038symget(const char *nam)
1040 struct sym *sym;
1042 TAILQ_FOREACH(sym, &symhead, entry) {
1043 if (strcmp(nam, sym->name) == 0) {
1044 sym->used = 1;
1045 return sym->val;
1046 }
1047 }
1048 return NULL;
1051struct vhost *
1052new_vhost(void)
1054 return xcalloc(1, sizeof(struct vhost));
1057struct location *
1058new_location(void)
1060 struct location *l;
1062 l = xcalloc(1, sizeof(*l));
1063 l->dirfd = -1;
1064 l->fcgi = -1;
1065 return l;
1068struct proxy *
1069new_proxy(void)
1071 struct proxy *p;
1073 p = xcalloc(1, sizeof(*p));
1074 p->protocols = TLS_PROTOCOLS_DEFAULT;
1075 return p;
1078char *
1079ensure_absolute_path(char *path)
1081 if (path == NULL || *path != '/')
1082 yyerror("not an absolute path: %s", path);
1083 return path;
1086int
1087check_block_code(int n)
1089 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1090 yyerror("invalid block code %d", n);
1091 return n;
1094char *
1095check_block_fmt(char *fmt)
1097 char *s;
1099 for (s = fmt; *s; ++s) {
1100 if (*s != '%')
1101 continue;
1102 switch (*++s) {
1103 case '%':
1104 case 'p':
1105 case 'q':
1106 case 'P':
1107 case 'N':
1108 break;
1109 default:
1110 yyerror("invalid format specifier %%%c", *s);
1111 }
1112 }
1114 return fmt;
1117int
1118check_strip_no(int n)
1120 if (n <= 0)
1121 yyerror("invalid strip number %d", n);
1122 return n;
1125int
1126check_port_num(int n)
1128 if (n <= 0 || n >= UINT16_MAX)
1129 yyerror("port number is %s: %d",
1130 n <= 0 ? "too small" : "too large",
1131 n);
1132 return n;
1135int
1136check_prefork_num(int n)
1138 if (n <= 0 || n >= PROC_MAX)
1139 yyerror("invalid prefork number %d", n);
1140 return n;
1143void
1144advance_loc(void)
1146 loc = new_location();
1147 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1150void
1151advance_proxy(void)
1153 proxy = new_proxy();
1154 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1157void
1158parsehp(char *str, char **host, const char **port, const char *def)
1160 char *at;
1161 const char *errstr;
1163 *host = str;
1165 if ((at = strchr(str, ':')) != NULL) {
1166 *at++ = '\0';
1167 *port = at;
1168 } else
1169 *port = def;
1171 strtonum(*port, 1, UINT16_MAX, &errstr);
1172 if (errstr != NULL)
1173 yyerror("port is %s: %s", errstr, *port);
1176void
1177only_once(const void *ptr, const char *name)
1179 if (ptr != NULL)
1180 yyerror("`%s' specified more than once", name);
1183void
1184only_oncei(int i, const char *name)
1186 if (i != -1)
1187 yyerror("`%s' specified more than once", name);
1190int
1191fastcgi_conf(char *path, char *port, char *prog)
1193 struct fcgi *f;
1194 int i;
1196 for (i = 0; i < FCGI_MAX; ++i) {
1197 f = &fcgi[i];
1199 if (f->path == NULL) {
1200 f->id = i;
1201 f->path = path;
1202 f->port = port;
1203 f->prog = prog;
1204 return i;
1205 }
1207 /* XXX: what to do with prog? */
1208 if (!strcmp(f->path, path) &&
1209 ((port == NULL && f->port == NULL) ||
1210 !strcmp(f->port, port))) {
1211 free(path);
1212 free(port);
1213 return i;
1214 }
1215 }
1217 yyerror("too much `fastcgi' rules defined.");
1218 return -1;
1221void
1222add_param(char *name, char *val, int env)
1224 struct envlist *e;
1225 struct envhead *h;
1227 if (env)
1228 h = &host->env;
1229 else
1230 h = &host->params;
1232 e = xcalloc(1, sizeof(*e));
1233 e->name = name;
1234 e->value = val;
1235 if (TAILQ_EMPTY(h))
1236 TAILQ_INSERT_HEAD(h, e, envs);
1237 else
1238 TAILQ_INSERT_TAIL(h, e, envs);