Blob


1 %{
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 <netdb.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include "log.h"
38 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
39 static struct file {
40 TAILQ_ENTRY(file) entry;
41 FILE *stream;
42 char *name;
43 size_t ungetpos;
44 size_t ungetsize;
45 u_char *ungetbuf;
46 int eof_reached;
47 int lineno;
48 int errors;
49 } *file, *topfile;
51 struct file *pushfile(const char *, int);
52 int popfile(void);
53 int yyparse(void);
54 int yylex(void);
55 void yyerror(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 void yywarn(const char *, ...)
59 __attribute__((__format__ (printf, 1, 2)))
60 __attribute__((__nonnull__ (1)));
61 int kw_cmp(const void *, const void *);
62 int lookup(char *);
63 int igetc(void);
64 int lgetc(int);
65 void lungetc(int);
66 int findeol(void);
68 /*
69 * #define YYDEBUG 1
70 * int yydebug = 1;
71 */
73 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
74 struct sym {
75 TAILQ_ENTRY(sym) entry;
76 int used;
77 int persist;
78 char *name;
79 char *val;
80 };
82 int symset(const char *, const char *, int);
83 char *symget(const char *);
85 struct vhost *new_vhost(void);
86 struct location *new_location(void);
87 struct proxy *new_proxy(void);
88 char *ensure_absolute_path(char*);
89 int check_block_code(int);
90 char *check_block_fmt(char*);
91 int check_strip_no(int);
92 int check_port_num(int);
93 int check_prefork_num(int);
94 void advance_loc(void);
95 void advance_proxy(void);
96 void parsehp(char *, char **, const char **, const char *);
97 int fastcgi_conf(const char *, const char *);
98 void add_param(char *, char *);
99 int getservice(const char *);
101 static struct vhost *host;
102 static struct location *loc;
103 static struct proxy *proxy;
104 static char *current_media;
105 static int errors;
107 typedef struct {
108 union {
109 char *string;
110 int number;
111 } v;
112 int lineno;
113 } YYSTYPE;
115 %}
117 /* for bison: */
118 /* %define parse.error verbose */
120 %token ALIAS AUTO
121 %token BLOCK
122 %token CA CERT CHROOT CLIENT
123 %token DEFAULT
124 %token FASTCGI FOR_HOST
125 %token INCLUDE INDEX IPV6
126 %token KEY
127 %token LANG LOCATION LOG
128 %token OCSP OFF ON
129 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
130 %token RELAY_TO REQUIRE RETURN ROOT
131 %token SERVER SNI 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 proxy_port
142 %type <v.string> string numberstring
144 %%
146 conf : /* 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++; }
156 include : 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;
164 free($2);
166 file = nfile;
167 lungetc('\n');
171 bool : ON { $$ = 1; }
172 | OFF { $$ = 0; }
175 string : 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;
182 free($1);
183 free($2);
185 | STRING
188 numberstring : NUM {
189 char *s;
190 if (asprintf(&s, "%d", $1) == -1) {
191 yyerror("asprintf: number");
192 YYERROR;
194 $$ = s;
196 | STRING
199 varset : 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;
210 symset($1, $3, 0);
211 free($1);
212 free($3);
216 option : CHROOT string {
217 if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >=
218 sizeof(conf.chroot))
219 yyerror("chroot path too long");
220 free($2);
222 | IPV6 bool { conf.ipv6 = $2; }
223 | PORT NUM { conf.port = check_port_num($2); }
224 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
225 | PROTOCOLS string {
226 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
227 yyerror("invalid protocols string \"%s\"", $2);
228 free($2);
230 | USER string {
231 if (strlcpy(conf.user, $2, sizeof(conf.user)) >=
232 sizeof(conf.user))
233 yyerror("user name too long");
234 free($2);
238 vhost : SERVER string {
239 host = new_vhost();
240 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
242 loc = new_location();
243 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
245 TAILQ_INIT(&host->proxies);
247 (void) strlcpy(loc->match, "*", sizeof(loc->match));
248 (void) strlcpy(host->domain, $2, sizeof(host->domain));
250 if (strstr($2, "xn--") != NULL) {
251 yywarn("\"%s\" looks like punycode: you "
252 "should use the decoded hostname", $2);
255 free($2);
256 } '{' optnl servbody '}' {
257 if (*host->cert == '\0' || *host->key == '\0')
258 yyerror("invalid vhost definition: %s", $2);
260 | error '}' { yyerror("bad server directive"); }
263 servbody : /* empty */
264 | servbody servopt optnl
265 | servbody location optnl
266 | servbody proxy optnl
269 servopt : ALIAS string {
270 struct alist *a;
272 a = xcalloc(1, sizeof(*a));
273 (void) strlcpy(a->alias, $2, sizeof(a->alias));
274 free($2);
275 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
277 | CERT string {
278 ensure_absolute_path($2);
279 (void) strlcpy(host->cert, $2, sizeof(host->cert));
280 free($2);
282 | KEY string {
283 ensure_absolute_path($2);
284 (void) strlcpy(host->key, $2, sizeof(host->key));
285 free($2);
287 | OCSP string {
288 ensure_absolute_path($2);
289 (void) strlcpy(host->ocsp, $2, sizeof(host->ocsp));
290 free($2);
292 | PARAM string '=' string {
293 add_param($2, $4);
295 | locopt
298 proxy : PROXY { advance_proxy(); }
299 proxy_matches '{' optnl proxy_opts '}' {
300 if (*proxy->host == '\0')
301 yyerror("invalid proxy block: missing `relay-to' option");
303 if ((proxy->cert == NULL && proxy->key != NULL) ||
304 (proxy->cert != NULL && proxy->key == NULL))
305 yyerror("invalid proxy block: missing cert or key");
309 proxy_matches : /* empty */
310 | proxy_matches proxy_match
313 proxy_port : /* empty */ { $$ = 1965; }
314 | PORT STRING {
315 if (($$ = getservice($2)) == -1)
316 yyerror("invalid port number %s", $2);
317 free($2);
319 | PORT NUM { $$ = $2; }
322 proxy_match : PROTO string {
323 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
324 free($2);
326 | FOR_HOST string proxy_port {
327 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
328 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
329 "%d", $3);
330 free($2);
334 proxy_opts : /* empty */
335 | proxy_opts proxy_opt optnl
338 proxy_opt : CERT string {
339 tls_unload_file(proxy->cert, proxy->certlen);
340 ensure_absolute_path($2);
341 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
342 if (proxy->cert == NULL)
343 yyerror("can't load cert %s", $2);
344 free($2);
346 | KEY string {
347 tls_unload_file(proxy->key, proxy->keylen);
348 ensure_absolute_path($2);
349 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
350 if (proxy->key == NULL)
351 yyerror("can't load key %s", $2);
352 free($2);
354 | PROTOCOLS string {
355 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
356 yyerror("invalid protocols string \"%s\"", $2);
357 free($2);
359 | RELAY_TO string proxy_port {
360 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
361 (void) snprintf(proxy->port, sizeof(proxy->port),
362 "%d", $3);
363 free($2);
365 | REQUIRE CLIENT CA string {
366 ensure_absolute_path($4);
367 if ((proxy->reqca = load_ca($4)) == NULL)
368 yyerror("couldn't load ca cert: %s", $4);
369 free($4);
371 | SNI string {
372 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
373 free($2);
375 | USE_TLS bool {
376 proxy->notls = !$2;
378 | VERIFYNAME bool {
379 proxy->noverifyname = !$2;
383 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
384 /* drop the starting '/' if any */
385 if (*$3 == '/')
386 memmove($3, $3+1, strlen($3));
387 (void) strlcpy(loc->match, $3, sizeof(loc->match));
388 free($3);
390 | error '}'
393 locopts : /* empty */
394 | locopts locopt optnl
397 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
398 | BLOCK RETURN NUM string {
399 check_block_fmt($4);
400 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
401 loc->block_code = check_block_code($3);
402 free($4);
404 | BLOCK RETURN NUM {
405 (void) strlcpy(loc->block_fmt, "temporary failure",
406 sizeof(loc->block_fmt));
407 loc->block_code = check_block_code($3);
408 if ($3 >= 30 && $3 < 40)
409 yyerror("missing `meta' for block return %d", $3);
411 | BLOCK {
412 (void) strlcpy(loc->block_fmt, "temporary failure",
413 sizeof(loc->block_fmt));
414 loc->block_code = 40;
416 | DEFAULT TYPE string {
417 (void) strlcpy(loc->default_mime, $3,
418 sizeof(loc->default_mime));
419 free($3);
421 | FASTCGI fastcgi
422 | INDEX string {
423 (void) strlcpy(loc->index, $2, sizeof(loc->index));
424 free($2);
426 | LANG string {
427 (void) strlcpy(loc->lang, $2,
428 sizeof(loc->lang));
429 free($2);
431 | LOG bool { loc->disable_log = !$2; }
432 | REQUIRE CLIENT CA string {
433 ensure_absolute_path($4);
434 if ((loc->reqca = load_ca($4)) == NULL)
435 yyerror("couldn't load ca cert: %s", $4);
436 free($4);
438 | ROOT string {
439 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
440 free($2);
442 | STRIP NUM { loc->strip = check_strip_no($2); }
445 fastcgi : string {
446 loc->fcgi = fastcgi_conf($1, NULL);
447 free($1);
449 | TCP string PORT NUM {
450 char *c;
451 if (asprintf(&c, "%d", $4) == -1)
452 err(1, "asprintf");
453 loc->fcgi = fastcgi_conf($2, c);
454 free($2);
456 | TCP string {
457 loc->fcgi = fastcgi_conf($2, "9000");
458 free($2);
460 | TCP string PORT string {
461 loc->fcgi = fastcgi_conf($2, $4);
462 free($2);
463 free($4);
467 types : TYPES '{' optnl mediaopts_l '}' ;
469 mediaopts_l : mediaopts_l mediaoptsl nl
470 | mediaoptsl nl
473 mediaoptsl : STRING {
474 free(current_media);
475 current_media = $1;
476 } medianames_l optsemicolon
477 | include
480 medianames_l : medianames_l medianamesl
481 | medianamesl
484 medianamesl : numberstring {
485 if (add_mime(&conf.mime, current_media, $1) == -1)
486 err(1, "add_mime");
487 free($1);
491 nl : '\n' optnl
494 optnl : '\n' optnl /* zero or more newlines */
495 | ';' optnl /* semicolons too */
496 | /*empty*/
499 optsemicolon : ';'
503 %%
505 static const struct keyword {
506 const char *word;
507 int token;
508 } keywords[] = {
509 /* these MUST be sorted */
510 {"alias", ALIAS},
511 {"auto", AUTO},
512 {"block", BLOCK},
513 {"ca", CA},
514 {"cert", CERT},
515 {"chroot", CHROOT},
516 {"client", CLIENT},
517 {"default", DEFAULT},
518 {"fastcgi", FASTCGI},
519 {"for-host", FOR_HOST},
520 {"include", INCLUDE},
521 {"index", INDEX},
522 {"ipv6", IPV6},
523 {"key", KEY},
524 {"lang", LANG},
525 {"location", LOCATION},
526 {"log", LOG},
527 {"ocsp", OCSP},
528 {"off", OFF},
529 {"on", ON},
530 {"param", PARAM},
531 {"port", PORT},
532 {"prefork", PREFORK},
533 {"proto", PROTO},
534 {"protocols", PROTOCOLS},
535 {"proxy", PROXY},
536 {"relay-to", RELAY_TO},
537 {"require", REQUIRE},
538 {"return", RETURN},
539 {"root", ROOT},
540 {"server", SERVER},
541 {"sni", SNI},
542 {"strip", STRIP},
543 {"tcp", TCP},
544 {"to-ext", TOEXT},
545 {"type", TYPE},
546 {"types", TYPES},
547 {"use-tls", USE_TLS},
548 {"user", USER},
549 {"verifyname", VERIFYNAME},
550 };
552 void
553 yyerror(const char *msg, ...)
555 va_list ap;
557 file->errors++;
559 va_start(ap, msg);
560 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
561 vfprintf(stderr, msg, ap);
562 fprintf(stderr, "\n");
563 va_end(ap);
566 void
567 yywarn(const char *msg, ...)
569 va_list ap;
571 va_start(ap, msg);
572 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
573 vfprintf(stderr, msg, ap);
574 fprintf(stderr, "\n");
575 va_end(ap);
578 int
579 kw_cmp(const void *k, const void *e)
581 return strcmp(k, ((struct keyword *)e)->word);
584 int
585 lookup(char *s)
587 const struct keyword *p;
589 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
590 sizeof(keywords[0]), kw_cmp);
592 if (p)
593 return p->token;
594 else
595 return STRING;
598 #define START_EXPAND 1
599 #define DONE_EXPAND 2
601 static int expanding;
603 int
604 igetc(void)
606 int c;
608 while (1) {
609 if (file->ungetpos > 0)
610 c = file->ungetbuf[--file->ungetpos];
611 else
612 c = getc(file->stream);
614 if (c == START_EXPAND)
615 expanding = 1;
616 else if (c == DONE_EXPAND)
617 expanding = 0;
618 else
619 break;
621 return c;
624 int
625 lgetc(int quotec)
627 int c, next;
629 if (quotec) {
630 if ((c = igetc()) == EOF) {
631 yyerror("reached end of file while parsing "
632 "quoted string");
633 if (file == topfile || popfile() == EOF)
634 return EOF;
635 return quotec;
637 return c;
640 while ((c = igetc()) == '\\') {
641 next = igetc();
642 if (next != '\n') {
643 c = next;
644 break;
646 yylval.lineno = file->lineno;
647 file->lineno++;
650 if (c == EOF) {
651 /*
652 * Fake EOL when hit EOF for the first time. This gets line
653 * count right if last line in included file is syntactically
654 * invalid and has no newline.
655 */
656 if (file->eof_reached == 0) {
657 file->eof_reached = 1;
658 return '\n';
660 while (c == EOF) {
661 if (file == topfile || popfile() == EOF)
662 return EOF;
663 c = igetc();
666 return c;
669 void
670 lungetc(int c)
672 if (c == EOF)
673 return;
675 if (file->ungetpos >= file->ungetsize) {
676 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
677 if (p == NULL)
678 fatal("lungetc");
679 file->ungetbuf = p;
680 file->ungetsize *= 2;
682 file->ungetbuf[file->ungetpos++] = c;
685 int
686 findeol(void)
688 int c;
690 /* Skip to either EOF or the first real EOL. */
691 while (1) {
692 c = lgetc(0);
693 if (c == '\n') {
694 file->lineno++;
695 break;
697 if (c == EOF)
698 break;
700 return ERROR;
703 int
704 yylex(void)
706 char buf[8096];
707 char *p, *val;
708 int quotec, next, c;
709 int token;
711 top:
712 p = buf;
713 while ((c = lgetc(0)) == ' ' || c == '\t')
714 ; /* nothing */
716 yylval.lineno = file->lineno;
717 if (c == '#')
718 while ((c = lgetc(0)) != '\n' && c != EOF)
719 ; /* nothing */
720 if (c == '$' && !expanding) {
721 while (1) {
722 if ((c = lgetc(0)) == EOF)
723 return 0;
724 if (p + 1 >= buf + sizeof(buf) -1) {
725 yyerror("string too long");
726 return findeol();
728 if (isalnum(c) || c == '_') {
729 *p++ = c;
730 continue;
732 *p = '\0';
733 lungetc(c);
734 break;
736 val = symget(buf);
737 if (val == NULL) {
738 yyerror("macro `%s' not defined", buf);
739 return findeol();
741 yylval.v.string = xstrdup(val);
742 return STRING;
744 if (c == '@' && !expanding) {
745 while (1) {
746 if ((c = lgetc(0)) == EOF)
747 return 0;
749 if (p + 1 >= buf + sizeof(buf) - 1) {
750 yyerror("string too long");
751 return findeol();
753 if (isalnum(c) || c == '_') {
754 *p++ = c;
755 continue;
757 *p = '\0';
758 lungetc(c);
759 break;
761 val = symget(buf);
762 if (val == NULL) {
763 yyerror("macro '%s' not defined", buf);
764 return findeol();
766 p = val + strlen(val) - 1;
767 lungetc(DONE_EXPAND);
768 while (p >= val) {
769 lungetc(*p);
770 p--;
772 lungetc(START_EXPAND);
773 goto top;
776 switch (c) {
777 case '\'':
778 case '"':
779 quotec = c;
780 while (1) {
781 if ((c = lgetc(quotec)) == EOF)
782 return 0;
783 if (c == '\n') {
784 file->lineno++;
785 continue;
786 } else if (c == '\\') {
787 if ((next = lgetc(quotec)) == EOF)
788 return (0);
789 if (next == quotec || next == ' ' ||
790 next == '\t')
791 c = next;
792 else if (next == '\n') {
793 file->lineno++;
794 continue;
795 } else
796 lungetc(next);
797 } else if (c == quotec) {
798 *p = '\0';
799 break;
800 } else if (c == '\0') {
801 yyerror("invalid syntax");
802 return findeol();
804 if (p + 1 >= buf + sizeof(buf) - 1) {
805 yyerror("string too long");
806 return findeol();
808 *p++ = c;
810 yylval.v.string = strdup(buf);
811 if (yylval.v.string == NULL)
812 fatal("yylex: strdup");
813 return STRING;
816 #define allowed_to_end_number(x) \
817 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
819 if (c == '-' || isdigit(c)) {
820 do {
821 *p++ = c;
822 if ((size_t)(p-buf) >= sizeof(buf)) {
823 yyerror("string too long");
824 return findeol();
826 } while ((c = lgetc(0)) != EOF && isdigit(c));
827 lungetc(c);
828 if (p == buf + 1 && buf[0] == '-')
829 goto nodigits;
830 if (c == EOF || allowed_to_end_number(c)) {
831 const char *errstr = NULL;
833 *p = '\0';
834 yylval.v.number = strtonum(buf, LLONG_MIN,
835 LLONG_MAX, &errstr);
836 if (errstr) {
837 yyerror("\"%s\" invalid number: %s",
838 buf, errstr);
839 return findeol();
841 return NUM;
842 } else {
843 nodigits:
844 while (p > buf + 1)
845 lungetc(*--p);
846 c = *--p;
847 if (c == '-')
848 return c;
852 #define allowed_in_string(x) \
853 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
854 x != '{' && x != '}' && \
855 x != '!' && x != '=' && x != '#' && \
856 x != ',' && x != ';'))
858 if (isalnum(c) || c == ':' || c == '_') {
859 do {
860 *p++ = c;
861 if ((size_t)(p-buf) >= sizeof(buf)) {
862 yyerror("string too long");
863 return findeol();
865 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
866 lungetc(c);
867 *p = '\0';
868 if ((token = lookup(buf)) == STRING)
869 yylval.v.string = xstrdup(buf);
870 return token;
872 if (c == '\n') {
873 yylval.lineno = file->lineno;
874 file->lineno++;
876 if (c == EOF)
877 return 0;
878 return c;
881 struct file *
882 pushfile(const char *name, int secret)
884 struct file *nfile;
886 nfile = xcalloc(1, sizeof(*nfile));
887 nfile->name = xstrdup(name);
888 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
889 log_warn("can't open %s", nfile->name);
890 free(nfile->name);
891 free(nfile);
892 return NULL;
894 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
895 nfile->ungetsize = 16;
896 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
897 TAILQ_INSERT_TAIL(&files, nfile, entry);
898 return nfile;
901 int
902 popfile(void)
904 struct file *prev;
906 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
907 prev->errors += file->errors;
909 TAILQ_REMOVE(&files, file, entry);
910 fclose(file->stream);
911 free(file->name);
912 free(file->ungetbuf);
913 free(file);
914 file = prev;
915 return file ? 0 : EOF;
918 void
919 parse_conf(const char *filename)
921 struct sym *sym, *next;
923 file = pushfile(filename, 0);
924 if (file == NULL)
925 exit(1);
926 topfile = file;
928 yyparse();
929 errors = file->errors;
930 popfile();
932 /* Free macros and check which have not been used. */
933 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
934 /* TODO: warn if !sym->used */
935 if (!sym->persist) {
936 free(sym->name);
937 free(sym->val);
938 TAILQ_REMOVE(&symhead, sym, entry);
939 free(sym);
943 if (errors)
944 exit(1);
947 void
948 print_conf(void)
950 struct vhost *h;
951 /* struct location *l; */
952 /* struct envlist *e; */
953 /* struct alist *a; */
955 if (*conf.chroot != '\0')
956 printf("chroot \"%s\"\n", conf.chroot);
957 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
958 /* XXX: defined mimes? */
959 printf("port %d\n", conf.port);
960 printf("prefork %d\n", conf.prefork);
961 /* XXX: protocols? */
962 if (*conf.user != '\0')
963 printf("user \"%s\"\n", conf.user);
965 TAILQ_FOREACH(h, &hosts, vhosts) {
966 printf("\nserver \"%s\" {\n", h->domain);
967 printf(" cert \"%s\"\n", h->cert);
968 printf(" key \"%s\"\n", h->key);
969 /* TODO: print locations... */
970 printf("}\n");
974 int
975 symset(const char *name, const char *val, int persist)
977 struct sym *sym;
979 TAILQ_FOREACH(sym, &symhead, entry) {
980 if (!strcmp(name, sym->name))
981 break;
984 if (sym != NULL) {
985 if (sym->persist)
986 return 0;
987 else {
988 free(sym->name);
989 free(sym->val);
990 TAILQ_REMOVE(&symhead, sym, entry);
991 free(sym);
995 sym = xcalloc(1, sizeof(*sym));
996 sym->name = xstrdup(name);
997 sym->val = xstrdup(val);
998 sym->used = 0;
999 sym->persist = persist;
1001 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1002 return 0;
1005 int
1006 cmdline_symset(char *s)
1008 char *sym, *val;
1009 int ret;
1011 if ((val = strrchr(s, '=')) == NULL)
1012 return -1;
1013 sym = xcalloc(1, val - s + 1);
1014 memcpy(sym, s, val - s);
1015 ret = symset(sym, val + 1, 1);
1016 free(sym);
1017 return ret;
1020 char *
1021 symget(const char *nam)
1023 struct sym *sym;
1025 TAILQ_FOREACH(sym, &symhead, entry) {
1026 if (strcmp(nam, sym->name) == 0) {
1027 sym->used = 1;
1028 return sym->val;
1031 return NULL;
1034 struct vhost *
1035 new_vhost(void)
1037 struct vhost *h;
1039 h = xcalloc(1, sizeof(*h));
1040 TAILQ_INIT(&h->locations);
1041 TAILQ_INIT(&h->params);
1042 TAILQ_INIT(&h->aliases);
1043 TAILQ_INIT(&h->proxies);
1044 return h;
1047 struct location *
1048 new_location(void)
1050 struct location *l;
1052 l = xcalloc(1, sizeof(*l));
1053 l->dirfd = -1;
1054 l->fcgi = -1;
1055 return l;
1058 struct proxy *
1059 new_proxy(void)
1061 struct proxy *p;
1063 p = xcalloc(1, sizeof(*p));
1064 p->protocols = TLS_PROTOCOLS_DEFAULT;
1065 return p;
1068 char *
1069 ensure_absolute_path(char *path)
1071 if (path == NULL || *path != '/')
1072 yyerror("not an absolute path: %s", path);
1073 return path;
1076 int
1077 check_block_code(int n)
1079 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1080 yyerror("invalid block code %d", n);
1081 return n;
1084 char *
1085 check_block_fmt(char *fmt)
1087 char *s;
1089 for (s = fmt; *s; ++s) {
1090 if (*s != '%')
1091 continue;
1092 switch (*++s) {
1093 case '%':
1094 case 'p':
1095 case 'q':
1096 case 'P':
1097 case 'N':
1098 break;
1099 default:
1100 yyerror("invalid format specifier %%%c", *s);
1104 return fmt;
1107 int
1108 check_strip_no(int n)
1110 if (n <= 0)
1111 yyerror("invalid strip number %d", n);
1112 return n;
1115 int
1116 check_port_num(int n)
1118 if (n <= 0 || n >= UINT16_MAX)
1119 yyerror("port number is %s: %d",
1120 n <= 0 ? "too small" : "too large",
1121 n);
1122 return n;
1125 int
1126 check_prefork_num(int n)
1128 if (n <= 0 || n >= PREFORK_MAX)
1129 yyerror("invalid prefork number %d", n);
1130 return n;
1133 void
1134 advance_loc(void)
1136 loc = new_location();
1137 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1140 void
1141 advance_proxy(void)
1143 proxy = new_proxy();
1144 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1147 void
1148 parsehp(char *str, char **host, const char **port, const char *def)
1150 char *at;
1151 const char *errstr;
1153 *host = str;
1155 if ((at = strchr(str, ':')) != NULL) {
1156 *at++ = '\0';
1157 *port = at;
1158 } else
1159 *port = def;
1161 strtonum(*port, 1, UINT16_MAX, &errstr);
1162 if (errstr != NULL)
1163 yyerror("port is %s: %s", errstr, *port);
1166 int
1167 fastcgi_conf(const char *path, const char *port)
1169 struct fcgi *f;
1170 int i;
1172 for (i = 0; i < FCGI_MAX; ++i) {
1173 f = &fcgi[i];
1175 if (*f->path == '\0') {
1176 f->id = i;
1177 (void) strlcpy(f->path, path, sizeof(f->path));
1178 if (port != NULL)
1179 (void) strlcpy(f->port, port, sizeof(f->port));
1180 return i;
1183 if (!strcmp(f->path, path) &&
1184 ((port == NULL && *f->port == '\0') ||
1185 !strcmp(f->port, port)))
1186 return i;
1189 yyerror("too much `fastcgi' rules defined.");
1190 return -1;
1193 void
1194 add_param(char *name, char *val)
1196 struct envlist *e;
1197 struct envhead *h = &host->params;
1199 e = xcalloc(1, sizeof(*e));
1200 (void) strlcpy(e->name, name, sizeof(e->name));
1201 (void) strlcpy(e->value, val, sizeof(e->value));
1202 TAILQ_INSERT_TAIL(h, e, envs);
1205 int
1206 getservice(const char *n)
1208 struct servent *s;
1209 const char *errstr;
1210 long long llval;
1212 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1213 if (errstr) {
1214 s = getservbyname(n, "tcp");
1215 if (s == NULL)
1216 s = getservbyname(n, "udp");
1217 if (s == NULL)
1218 return (-1);
1219 return (ntohs(s->s_port));
1222 return ((unsigned short)llval);