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 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
37 static struct file {
38 TAILQ_ENTRY(file) entry;
39 FILE *stream;
40 char *name;
41 size_t ungetpos;
42 size_t ungetsize;
43 u_char *ungetbuf;
44 int eof_reached;
45 int lineno;
46 int errors;
47 } *file, *topfile;
49 struct file *pushfile(const char *, int);
50 int popfile(void);
51 int yyparse(void);
52 int yylex(void);
53 void yyerror(const char *, ...)
54 __attribute__((__format__ (printf, 1, 2)))
55 __attribute__((__nonnull__ (1)));
56 void yywarn(const char *, ...)
57 __attribute__((__format__ (printf, 1, 2)))
58 __attribute__((__nonnull__ (1)));
59 int kw_cmp(const void *, const void *);
60 int lookup(char *);
61 int igetc(void);
62 int lgetc(int);
63 void lungetc(int);
64 int findeol(void);
66 /*
67 * #define YYDEBUG 1
68 * int yydebug = 1;
69 */
71 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
72 struct sym {
73 TAILQ_ENTRY(sym) entry;
74 int used;
75 int persist;
76 char *name;
77 char *val;
78 };
80 int symset(const char *, const char *, int);
81 char *symget(const char *);
83 struct vhost *new_vhost(void);
84 struct location *new_location(void);
85 struct proxy *new_proxy(void);
86 char *ensure_absolute_path(char*);
87 int check_block_code(int);
88 char *check_block_fmt(char*);
89 int check_strip_no(int);
90 int check_port_num(int);
91 int check_prefork_num(int);
92 void advance_loc(void);
93 void advance_proxy(void);
94 void parsehp(char *, char **, const char **, const char *);
95 int fastcgi_conf(const char *, const char *, char *);
96 void add_param(char *, char *);
97 int getservice(const char *);
99 static struct vhost *host;
100 static struct location *loc;
101 static struct proxy *proxy;
102 static char *current_media;
103 static int errors;
105 typedef 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 CHROOT CLIENT
121 %token DEFAULT
122 %token FASTCGI FOR_HOST
123 %token INCLUDE INDEX IPV6
124 %token KEY
125 %token LANG LOCATION LOG
126 %token OCSP OFF ON
127 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
128 %token RELAY_TO REQUIRE RETURN ROOT
129 %token SERVER SNI SPAWN STRIP
130 %token TCP TOEXT TYPE TYPES
131 %token USE_TLS USER
132 %token VERIFYNAME
134 %token ERROR
136 %token <v.string> STRING
137 %token <v.number> NUM
139 %type <v.number> bool proxy_port
140 %type <v.string> string numberstring
142 %%
144 conf : /* empty */
145 | conf include '\n'
146 | conf '\n'
147 | conf varset '\n'
148 | conf option '\n'
149 | conf vhost '\n'
150 | conf types '\n'
151 | conf error '\n' { file->errors++; }
154 include : INCLUDE STRING {
155 struct file *nfile;
157 if ((nfile = pushfile($2, 0)) == NULL) {
158 yyerror("failed to include file %s", $2);
159 free($2);
160 YYERROR;
162 free($2);
164 file = nfile;
165 lungetc('\n');
169 bool : ON { $$ = 1; }
170 | OFF { $$ = 0; }
173 string : string STRING {
174 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
175 free($1);
176 free($2);
177 yyerror("string: asprintf: %s", strerror(errno));
178 YYERROR;
180 free($1);
181 free($2);
183 | STRING
186 numberstring : NUM {
187 char *s;
188 if (asprintf(&s, "%d", $1) == -1) {
189 yyerror("asprintf: number");
190 YYERROR;
192 $$ = s;
194 | STRING
197 varset : STRING '=' string {
198 char *s = $1;
199 while (*s++) {
200 if (isspace((unsigned char)*s)) {
201 yyerror("macro name cannot contain "
202 "whitespaces");
203 free($1);
204 free($3);
205 YYERROR;
208 symset($1, $3, 0);
209 free($1);
210 free($3);
214 option : CHROOT string {
215 if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >=
216 sizeof(conf.chroot))
217 yyerror("chroot path too long");
218 free($2);
220 | IPV6 bool { conf.ipv6 = $2; }
221 | PORT NUM { conf.port = check_port_num($2); }
222 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
223 | PROTOCOLS string {
224 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
225 yyerror("invalid protocols string \"%s\"", $2);
226 free($2);
228 | USER string {
229 if (strlcpy(conf.user, $2, sizeof(conf.user)) >=
230 sizeof(conf.user))
231 yyerror("user name too long");
232 free($2);
236 vhost : SERVER string {
237 host = new_vhost();
238 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
240 loc = new_location();
241 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
243 TAILQ_INIT(&host->proxies);
245 (void) strlcpy(loc->match, "*", sizeof(loc->match));
246 (void) strlcpy(host->domain, $2, sizeof(host->domain));
248 if (strstr($2, "xn--") != NULL) {
249 yywarn("\"%s\" looks like punycode: you "
250 "should use the decoded hostname", $2);
253 free($2);
254 } '{' optnl servbody '}' {
255 if (*host->cert == '\0' || *host->key == '\0')
256 yyerror("invalid vhost definition: %s", $2);
258 | error '}' { yyerror("bad server directive"); }
261 servbody : /* empty */
262 | servbody servopt optnl
263 | servbody location optnl
264 | servbody proxy optnl
267 servopt : ALIAS string {
268 struct alist *a;
270 a = xcalloc(1, sizeof(*a));
271 (void) strlcpy(a->alias, $2, sizeof(a->alias));
272 free($2);
273 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
275 | CERT string {
276 ensure_absolute_path($2);
277 (void) strlcpy(host->cert, $2, sizeof(host->cert));
278 free($2);
280 | KEY string {
281 ensure_absolute_path($2);
282 (void) strlcpy(host->key, $2, sizeof(host->key));
283 free($2);
285 | OCSP string {
286 ensure_absolute_path($2);
287 (void) strlcpy(host->ocsp, $2, sizeof(host->ocsp));
288 free($2);
290 | PARAM string '=' string {
291 add_param($2, $4);
293 | locopt
296 proxy : PROXY { advance_proxy(); }
297 proxy_matches '{' optnl proxy_opts '}' {
298 if (*proxy->host == '\0')
299 yyerror("invalid proxy block: missing `relay-to' option");
301 if ((proxy->cert == NULL && proxy->key != NULL) ||
302 (proxy->cert != NULL && proxy->key == NULL))
303 yyerror("invalid proxy block: missing cert or key");
307 proxy_matches : /* empty */
308 | proxy_matches proxy_match
311 proxy_port : /* empty */ { $$ = 1965; }
312 | PORT STRING {
313 if (($$ = getservice($2)) == -1)
314 yyerror("invalid port number %s", $2);
315 free($2);
317 | PORT NUM { $$ = $2; }
320 proxy_match : PROTO string {
321 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
322 free($2);
324 | FOR_HOST string proxy_port {
325 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
326 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
327 "%d", $3);
328 free($2);
332 proxy_opts : /* empty */
333 | proxy_opts proxy_opt optnl
336 proxy_opt : CERT string {
337 tls_unload_file(proxy->cert, proxy->certlen);
338 ensure_absolute_path($2);
339 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
340 if (proxy->cert == NULL)
341 yyerror("can't load cert %s", $2);
342 free($2);
344 | KEY string {
345 tls_unload_file(proxy->key, proxy->keylen);
346 ensure_absolute_path($2);
347 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
348 if (proxy->key == NULL)
349 yyerror("can't load key %s", $2);
350 free($2);
352 | PROTOCOLS string {
353 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
354 yyerror("invalid protocols string \"%s\"", $2);
355 free($2);
357 | RELAY_TO string proxy_port {
358 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
359 (void) snprintf(proxy->port, sizeof(proxy->port),
360 "%d", $3);
361 free($2);
363 | REQUIRE CLIENT CA string {
364 ensure_absolute_path($4);
365 if ((proxy->reqca = load_ca($4)) == NULL)
366 yyerror("couldn't load ca cert: %s", $4);
367 free($4);
369 | SNI string {
370 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
371 free($2);
373 | USE_TLS bool {
374 proxy->notls = !$2;
376 | VERIFYNAME bool {
377 proxy->noverifyname = !$2;
381 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
382 /* drop the starting '/' if any */
383 if (*$3 == '/')
384 memmove($3, $3+1, strlen($3));
385 (void) strlcpy(loc->match, $3, sizeof(loc->match));
386 free($3);
388 | error '}'
391 locopts : /* empty */
392 | locopts locopt optnl
395 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
396 | BLOCK RETURN NUM string {
397 check_block_fmt($4);
398 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
399 loc->block_code = check_block_code($3);
400 free($4);
402 | BLOCK RETURN NUM {
403 (void) strlcpy(loc->block_fmt, "temporary failure",
404 sizeof(loc->block_fmt));
405 loc->block_code = check_block_code($3);
406 if ($3 >= 30 && $3 < 40)
407 yyerror("missing `meta' for block return %d", $3);
409 | BLOCK {
410 (void) strlcpy(loc->block_fmt, "temporary failure",
411 sizeof(loc->block_fmt));
412 loc->block_code = 40;
414 | DEFAULT TYPE string {
415 (void) strlcpy(loc->default_mime, $3,
416 sizeof(loc->default_mime));
417 free($3);
419 | FASTCGI fastcgi
420 | INDEX string {
421 (void) strlcpy(loc->index, $2, sizeof(loc->index));
422 free($2);
424 | LANG string {
425 (void) strlcpy(loc->lang, $2,
426 sizeof(loc->lang));
427 free($2);
429 | LOG bool { loc->disable_log = !$2; }
430 | REQUIRE CLIENT CA string {
431 ensure_absolute_path($4);
432 if ((loc->reqca = load_ca($4)) == NULL)
433 yyerror("couldn't load ca cert: %s", $4);
434 free($4);
436 | ROOT string {
437 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
438 free($2);
440 | STRIP NUM { loc->strip = check_strip_no($2); }
443 fastcgi : SPAWN string {
444 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
445 free($2);
447 | string {
448 loc->fcgi = fastcgi_conf($1, NULL, NULL);
449 free($1);
451 | TCP string PORT NUM {
452 char *c;
453 if (asprintf(&c, "%d", $4) == -1)
454 err(1, "asprintf");
455 loc->fcgi = fastcgi_conf($2, c, NULL);
456 free($2);
458 | TCP string {
459 loc->fcgi = fastcgi_conf($2, "9000", NULL);
460 free($2);
462 | TCP string PORT string {
463 loc->fcgi = fastcgi_conf($2, $4, NULL);
464 free($2);
465 free($4);
469 types : TYPES '{' optnl mediaopts_l '}' ;
471 mediaopts_l : mediaopts_l mediaoptsl nl
472 | mediaoptsl nl
475 mediaoptsl : STRING {
476 free(current_media);
477 current_media = $1;
478 } medianames_l optsemicolon
479 | include
482 medianames_l : medianames_l medianamesl
483 | medianamesl
486 medianamesl : numberstring {
487 if (add_mime(&conf.mime, current_media, $1) == -1)
488 err(1, "add_mime");
489 free($1);
493 nl : '\n' optnl
496 optnl : '\n' optnl /* zero or more newlines */
497 | ';' optnl /* semicolons too */
498 | /*empty*/
501 optsemicolon : ';'
505 %%
507 static const struct keyword {
508 const char *word;
509 int token;
510 } keywords[] = {
511 /* these MUST be sorted */
512 {"alias", ALIAS},
513 {"auto", AUTO},
514 {"block", BLOCK},
515 {"ca", CA},
516 {"cert", CERT},
517 {"chroot", CHROOT},
518 {"client", CLIENT},
519 {"default", DEFAULT},
520 {"fastcgi", FASTCGI},
521 {"for-host", FOR_HOST},
522 {"include", INCLUDE},
523 {"index", INDEX},
524 {"ipv6", IPV6},
525 {"key", KEY},
526 {"lang", LANG},
527 {"location", LOCATION},
528 {"log", LOG},
529 {"ocsp", OCSP},
530 {"off", OFF},
531 {"on", ON},
532 {"param", PARAM},
533 {"port", PORT},
534 {"prefork", PREFORK},
535 {"proto", PROTO},
536 {"protocols", PROTOCOLS},
537 {"proxy", PROXY},
538 {"relay-to", RELAY_TO},
539 {"require", REQUIRE},
540 {"return", RETURN},
541 {"root", ROOT},
542 {"server", SERVER},
543 {"sni", SNI},
544 {"spawn", SPAWN},
545 {"strip", STRIP},
546 {"tcp", TCP},
547 {"to-ext", TOEXT},
548 {"type", TYPE},
549 {"types", TYPES},
550 {"use-tls", USE_TLS},
551 {"user", USER},
552 {"verifyname", VERIFYNAME},
553 };
555 void
556 yyerror(const char *msg, ...)
558 va_list ap;
560 file->errors++;
562 va_start(ap, msg);
563 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
564 vfprintf(stderr, msg, ap);
565 fprintf(stderr, "\n");
566 va_end(ap);
569 void
570 yywarn(const char *msg, ...)
572 va_list ap;
574 va_start(ap, msg);
575 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
576 vfprintf(stderr, msg, ap);
577 fprintf(stderr, "\n");
578 va_end(ap);
581 int
582 kw_cmp(const void *k, const void *e)
584 return strcmp(k, ((struct keyword *)e)->word);
587 int
588 lookup(char *s)
590 const struct keyword *p;
592 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
593 sizeof(keywords[0]), kw_cmp);
595 if (p)
596 return p->token;
597 else
598 return STRING;
601 #define START_EXPAND 1
602 #define DONE_EXPAND 2
604 static int expanding;
606 int
607 igetc(void)
609 int c;
611 while (1) {
612 if (file->ungetpos > 0)
613 c = file->ungetbuf[--file->ungetpos];
614 else
615 c = getc(file->stream);
617 if (c == START_EXPAND)
618 expanding = 1;
619 else if (c == DONE_EXPAND)
620 expanding = 0;
621 else
622 break;
624 return c;
627 int
628 lgetc(int quotec)
630 int c, next;
632 if (quotec) {
633 if ((c = igetc()) == EOF) {
634 yyerror("reached end of file while parsing "
635 "quoted string");
636 if (file == topfile || popfile() == EOF)
637 return EOF;
638 return quotec;
640 return c;
643 while ((c = igetc()) == '\\') {
644 next = igetc();
645 if (next != '\n') {
646 c = next;
647 break;
649 yylval.lineno = file->lineno;
650 file->lineno++;
653 if (c == EOF) {
654 /*
655 * Fake EOL when hit EOF for the first time. This gets line
656 * count right if last line in included file is syntactically
657 * invalid and has no newline.
658 */
659 if (file->eof_reached == 0) {
660 file->eof_reached = 1;
661 return '\n';
663 while (c == EOF) {
664 if (file == topfile || popfile() == EOF)
665 return EOF;
666 c = igetc();
669 return c;
672 void
673 lungetc(int c)
675 if (c == EOF)
676 return;
678 if (file->ungetpos >= file->ungetsize) {
679 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
680 if (p == NULL)
681 err(1, "lungetc");
682 file->ungetbuf = p;
683 file->ungetsize *= 2;
685 file->ungetbuf[file->ungetpos++] = c;
688 int
689 findeol(void)
691 int c;
693 /* Skip to either EOF or the first real EOL. */
694 while (1) {
695 c = lgetc(0);
696 if (c == '\n') {
697 file->lineno++;
698 break;
700 if (c == EOF)
701 break;
703 return ERROR;
706 int
707 yylex(void)
709 char buf[8096];
710 char *p, *val;
711 int quotec, next, c;
712 int token;
714 top:
715 p = buf;
716 while ((c = lgetc(0)) == ' ' || c == '\t')
717 ; /* nothing */
719 yylval.lineno = file->lineno;
720 if (c == '#')
721 while ((c = lgetc(0)) != '\n' && c != EOF)
722 ; /* nothing */
723 if (c == '$' && !expanding) {
724 while (1) {
725 if ((c = lgetc(0)) == EOF)
726 return 0;
727 if (p + 1 >= buf + sizeof(buf) -1) {
728 yyerror("string too long");
729 return findeol();
731 if (isalnum(c) || c == '_') {
732 *p++ = c;
733 continue;
735 *p = '\0';
736 lungetc(c);
737 break;
739 val = symget(buf);
740 if (val == NULL) {
741 yyerror("macro `%s' not defined", buf);
742 return findeol();
744 yylval.v.string = xstrdup(val);
745 return STRING;
747 if (c == '@' && !expanding) {
748 while (1) {
749 if ((c = lgetc(0)) == EOF)
750 return 0;
752 if (p + 1 >= buf + sizeof(buf) - 1) {
753 yyerror("string too long");
754 return findeol();
756 if (isalnum(c) || c == '_') {
757 *p++ = c;
758 continue;
760 *p = '\0';
761 lungetc(c);
762 break;
764 val = symget(buf);
765 if (val == NULL) {
766 yyerror("macro '%s' not defined", buf);
767 return findeol();
769 p = val + strlen(val) - 1;
770 lungetc(DONE_EXPAND);
771 while (p >= val) {
772 lungetc(*p);
773 p--;
775 lungetc(START_EXPAND);
776 goto top;
779 switch (c) {
780 case '\'':
781 case '"':
782 quotec = c;
783 while (1) {
784 if ((c = lgetc(quotec)) == EOF)
785 return 0;
786 if (c == '\n') {
787 file->lineno++;
788 continue;
789 } else if (c == '\\') {
790 if ((next = lgetc(quotec)) == EOF)
791 return (0);
792 if (next == quotec || next == ' ' ||
793 next == '\t')
794 c = next;
795 else if (next == '\n') {
796 file->lineno++;
797 continue;
798 } else
799 lungetc(next);
800 } else if (c == quotec) {
801 *p = '\0';
802 break;
803 } else if (c == '\0') {
804 yyerror("invalid syntax");
805 return findeol();
807 if (p + 1 >= buf + sizeof(buf) - 1) {
808 yyerror("string too long");
809 return findeol();
811 *p++ = c;
813 yylval.v.string = strdup(buf);
814 if (yylval.v.string == NULL)
815 err(1, "yylex: strdup");
816 return STRING;
819 #define allowed_to_end_number(x) \
820 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
822 if (c == '-' || isdigit(c)) {
823 do {
824 *p++ = c;
825 if ((size_t)(p-buf) >= sizeof(buf)) {
826 yyerror("string too long");
827 return findeol();
829 } while ((c = lgetc(0)) != EOF && isdigit(c));
830 lungetc(c);
831 if (p == buf + 1 && buf[0] == '-')
832 goto nodigits;
833 if (c == EOF || allowed_to_end_number(c)) {
834 const char *errstr = NULL;
836 *p = '\0';
837 yylval.v.number = strtonum(buf, LLONG_MIN,
838 LLONG_MAX, &errstr);
839 if (errstr) {
840 yyerror("\"%s\" invalid number: %s",
841 buf, errstr);
842 return findeol();
844 return NUM;
845 } else {
846 nodigits:
847 while (p > buf + 1)
848 lungetc(*--p);
849 c = *--p;
850 if (c == '-')
851 return c;
855 #define allowed_in_string(x) \
856 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
857 x != '{' && x != '}' && \
858 x != '!' && x != '=' && x != '#' && \
859 x != ',' && x != ';'))
861 if (isalnum(c) || c == ':' || c == '_') {
862 do {
863 *p++ = c;
864 if ((size_t)(p-buf) >= sizeof(buf)) {
865 yyerror("string too long");
866 return findeol();
868 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
869 lungetc(c);
870 *p = '\0';
871 if ((token = lookup(buf)) == STRING)
872 yylval.v.string = xstrdup(buf);
873 return token;
875 if (c == '\n') {
876 yylval.lineno = file->lineno;
877 file->lineno++;
879 if (c == EOF)
880 return 0;
881 return c;
884 struct file *
885 pushfile(const char *name, int secret)
887 struct file *nfile;
889 nfile = xcalloc(1, sizeof(*nfile));
890 nfile->name = xstrdup(name);
891 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
892 log_warn(NULL, "can't open %s: %s", nfile->name,
893 strerror(errno));
894 free(nfile->name);
895 free(nfile);
896 return NULL;
898 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
899 nfile->ungetsize = 16;
900 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
901 TAILQ_INSERT_TAIL(&files, nfile, entry);
902 return nfile;
905 int
906 popfile(void)
908 struct file *prev;
910 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
911 prev->errors += file->errors;
913 TAILQ_REMOVE(&files, file, entry);
914 fclose(file->stream);
915 free(file->name);
916 free(file->ungetbuf);
917 free(file);
918 file = prev;
919 return file ? 0 : EOF;
922 void
923 parse_conf(const char *filename)
925 struct sym *sym, *next;
927 file = pushfile(filename, 0);
928 if (file == NULL)
929 exit(1);
930 topfile = file;
932 yyparse();
933 errors = file->errors;
934 popfile();
936 /* Free macros and check which have not been used. */
937 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
938 /* TODO: warn if !sym->used */
939 if (!sym->persist) {
940 free(sym->name);
941 free(sym->val);
942 TAILQ_REMOVE(&symhead, sym, entry);
943 free(sym);
947 if (errors)
948 exit(1);
951 void
952 print_conf(void)
954 struct vhost *h;
955 /* struct location *l; */
956 /* struct envlist *e; */
957 /* struct alist *a; */
959 if (*conf.chroot != '\0')
960 printf("chroot \"%s\"\n", conf.chroot);
961 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
962 /* XXX: defined mimes? */
963 printf("port %d\n", conf.port);
964 printf("prefork %d\n", conf.prefork);
965 /* XXX: protocols? */
966 if (*conf.user != '\0')
967 printf("user \"%s\"\n", conf.user);
969 TAILQ_FOREACH(h, &hosts, vhosts) {
970 printf("\nserver \"%s\" {\n", h->domain);
971 printf(" cert \"%s\"\n", h->cert);
972 printf(" key \"%s\"\n", h->key);
973 /* TODO: print locations... */
974 printf("}\n");
978 int
979 symset(const char *name, const char *val, int persist)
981 struct sym *sym;
983 TAILQ_FOREACH(sym, &symhead, entry) {
984 if (!strcmp(name, sym->name))
985 break;
988 if (sym != NULL) {
989 if (sym->persist)
990 return 0;
991 else {
992 free(sym->name);
993 free(sym->val);
994 TAILQ_REMOVE(&symhead, sym, entry);
995 free(sym);
999 sym = xcalloc(1, sizeof(*sym));
1000 sym->name = xstrdup(name);
1001 sym->val = xstrdup(val);
1002 sym->used = 0;
1003 sym->persist = persist;
1005 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1006 return 0;
1009 int
1010 cmdline_symset(char *s)
1012 char *sym, *val;
1013 int ret;
1015 if ((val = strrchr(s, '=')) == NULL)
1016 return -1;
1017 sym = xcalloc(1, val - s + 1);
1018 memcpy(sym, s, val - s);
1019 ret = symset(sym, val + 1, 1);
1020 free(sym);
1021 return ret;
1024 char *
1025 symget(const char *nam)
1027 struct sym *sym;
1029 TAILQ_FOREACH(sym, &symhead, entry) {
1030 if (strcmp(nam, sym->name) == 0) {
1031 sym->used = 1;
1032 return sym->val;
1035 return NULL;
1038 struct vhost *
1039 new_vhost(void)
1041 struct vhost *h;
1043 h = xcalloc(1, sizeof(*h));
1044 TAILQ_INIT(&h->locations);
1045 TAILQ_INIT(&h->params);
1046 TAILQ_INIT(&h->aliases);
1047 TAILQ_INIT(&h->proxies);
1048 return h;
1051 struct location *
1052 new_location(void)
1054 struct location *l;
1056 l = xcalloc(1, sizeof(*l));
1057 l->dirfd = -1;
1058 l->fcgi = -1;
1059 return l;
1062 struct proxy *
1063 new_proxy(void)
1065 struct proxy *p;
1067 conf.can_open_sockets = 1;
1069 p = xcalloc(1, sizeof(*p));
1070 p->protocols = TLS_PROTOCOLS_DEFAULT;
1071 return p;
1074 char *
1075 ensure_absolute_path(char *path)
1077 if (path == NULL || *path != '/')
1078 yyerror("not an absolute path: %s", path);
1079 return path;
1082 int
1083 check_block_code(int n)
1085 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1086 yyerror("invalid block code %d", n);
1087 return n;
1090 char *
1091 check_block_fmt(char *fmt)
1093 char *s;
1095 for (s = fmt; *s; ++s) {
1096 if (*s != '%')
1097 continue;
1098 switch (*++s) {
1099 case '%':
1100 case 'p':
1101 case 'q':
1102 case 'P':
1103 case 'N':
1104 break;
1105 default:
1106 yyerror("invalid format specifier %%%c", *s);
1110 return fmt;
1113 int
1114 check_strip_no(int n)
1116 if (n <= 0)
1117 yyerror("invalid strip number %d", n);
1118 return n;
1121 int
1122 check_port_num(int n)
1124 if (n <= 0 || n >= UINT16_MAX)
1125 yyerror("port number is %s: %d",
1126 n <= 0 ? "too small" : "too large",
1127 n);
1128 return n;
1131 int
1132 check_prefork_num(int n)
1134 if (n <= 0 || n >= PROC_MAX)
1135 yyerror("invalid prefork number %d", n);
1136 return n;
1139 void
1140 advance_loc(void)
1142 loc = new_location();
1143 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1146 void
1147 advance_proxy(void)
1149 proxy = new_proxy();
1150 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1153 void
1154 parsehp(char *str, char **host, const char **port, const char *def)
1156 char *at;
1157 const char *errstr;
1159 *host = str;
1161 if ((at = strchr(str, ':')) != NULL) {
1162 *at++ = '\0';
1163 *port = at;
1164 } else
1165 *port = def;
1167 strtonum(*port, 1, UINT16_MAX, &errstr);
1168 if (errstr != NULL)
1169 yyerror("port is %s: %s", errstr, *port);
1172 int
1173 fastcgi_conf(const char *path, const char *port, char *prog)
1175 struct fcgi *f;
1176 int i;
1178 conf.can_open_sockets = 1;
1180 for (i = 0; i < FCGI_MAX; ++i) {
1181 f = &fcgi[i];
1183 if (*f->path == '\0') {
1184 f->id = i;
1185 (void) strlcpy(f->path, path, sizeof(f->path));
1186 (void) strlcpy(f->port, port, sizeof(f->port));
1187 return i;
1190 if (!strcmp(f->path, path) &&
1191 ((port == NULL && *f->port == '\0') ||
1192 !strcmp(f->port, port)))
1193 return i;
1196 yyerror("too much `fastcgi' rules defined.");
1197 return -1;
1200 void
1201 add_param(char *name, char *val)
1203 struct envlist *e;
1204 struct envhead *h = &host->params;
1206 e = xcalloc(1, sizeof(*e));
1207 (void) strlcpy(e->name, name, sizeof(e->name));
1208 (void) strlcpy(e->value, val, sizeof(e->value));
1209 TAILQ_INSERT_TAIL(h, e, envs);
1212 int
1213 getservice(const char *n)
1215 struct servent *s;
1216 const char *errstr;
1217 long long llval;
1219 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1220 if (errstr) {
1221 s = getservbyname(n, "tcp");
1222 if (s == NULL)
1223 s = getservbyname(n, "udp");
1224 if (s == NULL)
1225 return (-1);
1226 return (ntohs(s->s_port));
1229 return ((unsigned short)llval);