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 <ctype.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #include "gmid.h"
35 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
36 static 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;
48 struct file *pushfile(const char *, int);
49 int popfile(void);
50 int yyparse(void);
51 int yylex(void);
52 void yyerror(const char *, ...)
53 __attribute__((__format__ (printf, 1, 2)))
54 __attribute__((__nonnull__ (1)));
55 void yywarn(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 int kw_cmp(const void *, const void *);
59 int lookup(char *);
60 int igetc(void);
61 int lgetc(int);
62 void lungetc(int);
63 int findeol(void);
65 /*
66 * #define YYDEBUG 1
67 * int yydebug = 1;
68 */
70 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
71 struct sym {
72 TAILQ_ENTRY(sym) entry;
73 int used;
74 int persist;
75 char *name;
76 char *val;
77 };
79 int symset(const char *, const char *, int);
80 char *symget(const char *);
82 struct vhost *new_vhost(void);
83 struct location *new_location(void);
84 struct proxy *new_proxy(void);
85 char *ensure_absolute_path(char*);
86 int check_block_code(int);
87 char *check_block_fmt(char*);
88 int check_strip_no(int);
89 int check_port_num(int);
90 int check_prefork_num(int);
91 void advance_loc(void);
92 void advance_proxy(void);
93 void parsehp(char *, char **, const char **, const char *);
94 void only_once(const void*, const char*);
95 void only_oncei(int, const char*);
96 int fastcgi_conf(char *, char *, char *);
97 void add_param(char *, char *, int);
99 static struct vhost *host;
100 static struct location *loc;
101 static struct proxy *proxy;
102 static int errors;
104 typedef struct {
105 union {
106 char *string;
107 int number;
108 } v;
109 int lineno;
110 } YYSTYPE;
112 %}
114 /* for bison: */
115 /* %define parse.error verbose */
117 %token ALIAS AUTO
118 %token BLOCK
119 %token CA CERT CGI CHROOT CLIENT
120 %token DEFAULT
121 %token ENTRYPOINT ENV
122 %token FASTCGI FOR_HOST
123 %token INCLUDE INDEX IPV6
124 %token KEY
125 %token LANG LOCATION LOG
126 %token MAP MIME
127 %token OCSP OFF ON
128 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
129 %token RELAY_TO REQUIRE RETURN ROOT
130 %token SERVER SPAWN STRIP
131 %token TCP TOEXT TYPE
132 %token USE_TLS USER
133 %token VERIFYNAME
135 %token ERROR
137 %token <v.string> STRING
138 %token <v.number> NUM
140 %type <v.number> bool
141 %type <v.string> string
143 %%
145 conf : /* empty */
146 | conf include '\n'
147 | conf '\n'
148 | conf varset '\n'
149 | conf option '\n'
150 | conf vhost '\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 varset : STRING '=' string {
187 char *s = $1;
188 while (*s++) {
189 if (isspace((unsigned char)*s)) {
190 yyerror("macro name cannot contain "
191 "whitespaces");
192 free($1);
193 free($3);
194 YYERROR;
197 symset($1, $3, 0);
198 free($1);
199 free($3);
203 option : CHROOT string { conf.chroot = $2; }
204 | IPV6 bool { conf.ipv6 = $2; }
205 | MIME STRING string {
206 yywarn("`mime MIME EXT' is deprecated and will be "
207 "removed in a future version, please use the new "
208 "syntax: `map MIME to-ext EXT'");
209 add_mime(&conf.mime, $2, $3);
211 | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
212 | PORT NUM { conf.port = check_port_num($2); }
213 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
214 | PROTOCOLS string {
215 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
216 yyerror("invalid protocols string \"%s\"", $2);
217 free($2);
219 | USER string { conf.user = $2; }
222 vhost : SERVER string {
223 host = new_vhost();
224 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
226 loc = new_location();
227 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
229 TAILQ_INIT(&host->proxies);
231 loc->match = xstrdup("*");
232 host->domain = $2;
234 if (strstr($2, "xn--") != NULL) {
235 yywarn("\"%s\" looks like punycode: you "
236 "should use the decoded hostname", $2);
238 } '{' optnl servbody '}' {
239 if (host->cert == NULL || host->key == NULL)
240 yyerror("invalid vhost definition: %s", $2);
242 | error '}' { yyerror("bad server directive"); }
245 servbody : /* empty */
246 | servbody servopt optnl
247 | servbody location optnl
248 | servbody proxy optnl
251 servopt : ALIAS string {
252 struct alist *a;
254 a = xcalloc(1, sizeof(*a));
255 a->alias = $2;
256 if (TAILQ_EMPTY(&host->aliases))
257 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
258 else
259 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
261 | CERT string {
262 only_once(host->cert, "cert");
263 host->cert = ensure_absolute_path($2);
265 | CGI string {
266 only_once(host->cgi, "cgi");
267 /* drop the starting '/', if any */
268 if (*$2 == '/')
269 memmove($2, $2+1, strlen($2));
270 host->cgi = $2;
272 | ENTRYPOINT string {
273 only_once(host->entrypoint, "entrypoint");
274 while (*$2 == '/')
275 memmove($2, $2+1, strlen($2));
276 host->entrypoint = $2;
278 | ENV string '=' string {
279 add_param($2, $4, 1);
281 | KEY string {
282 only_once(host->key, "key");
283 host->key = ensure_absolute_path($2);
285 | OCSP string {
286 only_once(host->ocsp, "ocsp");
287 host->ocsp = ensure_absolute_path($2);
289 | PARAM string '=' string {
290 add_param($2, $4, 0);
292 | locopt
295 proxy : PROXY { advance_proxy(); }
296 proxy_matches '{' optnl proxy_opts '}' {
297 if (proxy->host == NULL)
298 yyerror("invalid proxy block: missing `relay-to' option");
300 if ((proxy->cert == NULL && proxy->key != NULL) ||
301 (proxy->cert != NULL && proxy->key == NULL))
302 yyerror("invalid proxy block: missing cert or key");
306 proxy_matches : /* empty */
307 | proxy_matches proxy_match
310 proxy_match : PROTO string {
311 only_once(proxy->match_proto, "proxy proto");
312 free(proxy->match_proto);
313 proxy->match_proto = $2;
315 | FOR_HOST string {
316 only_once(proxy->match_host, "proxy for-host");
317 free(proxy->match_host);
318 parsehp($2, &proxy->match_host, &proxy->match_port, "10965");
322 proxy_opts : /* empty */
323 | proxy_opts proxy_opt optnl
326 proxy_opt : CERT string {
327 only_once(proxy->cert, "proxy cert");
328 tls_unload_file(proxy->cert, proxy->certlen);
329 ensure_absolute_path($2);
330 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
331 if (proxy->cert == NULL)
332 yyerror("can't load cert %s", $2);
333 free($2);
335 | KEY string {
336 only_once(proxy->key, "proxy key");
337 tls_unload_file(proxy->key, proxy->keylen);
338 ensure_absolute_path($2);
339 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
340 if (proxy->key == NULL)
341 yyerror("can't load key %s", $2);
342 free($2);
344 | PROTOCOLS string {
345 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
346 yyerror("invalid protocols string \"%s\"", $2);
347 free($2);
349 | RELAY_TO string {
350 only_once(proxy->host, "proxy relay-to");
351 free(proxy->host);
352 parsehp($2, &proxy->host, &proxy->port, "1965");
354 | USE_TLS bool {
355 proxy->notls = !$2;
357 | VERIFYNAME bool {
358 proxy->noverifyname = !$2;
362 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
363 /* drop the starting '/' if any */
364 if (*$3 == '/')
365 memmove($3, $3+1, strlen($3));
366 loc->match = $3;
368 | error '}'
371 locopts : /* empty */
372 | locopts locopt optnl
375 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
376 | BLOCK RETURN NUM string {
377 only_once(loc->block_fmt, "block");
378 loc->block_fmt = check_block_fmt($4);
379 loc->block_code = check_block_code($3);
381 | BLOCK RETURN NUM {
382 only_once(loc->block_fmt, "block");
383 loc->block_fmt = xstrdup("temporary failure");
384 loc->block_code = check_block_code($3);
385 if ($3 >= 30 && $3 < 40)
386 yyerror("missing `meta' for block return %d", $3);
388 | BLOCK {
389 only_once(loc->block_fmt, "block");
390 loc->block_fmt = xstrdup("temporary failure");
391 loc->block_code = 40;
393 | DEFAULT TYPE string {
394 only_once(loc->default_mime, "default type");
395 loc->default_mime = $3;
397 | FASTCGI fastcgi
398 | INDEX string {
399 only_once(loc->index, "index");
400 loc->index = $2;
402 | LANG string {
403 only_once(loc->lang, "lang");
404 loc->lang = $2;
406 | LOG bool { loc->disable_log = !$2; }
407 | REQUIRE CLIENT CA string {
408 only_once(loc->reqca, "require client ca");
409 ensure_absolute_path($4);
410 if ((loc->reqca = load_ca($4)) == NULL)
411 yyerror("couldn't load ca cert: %s", $4);
412 free($4);
414 | ROOT string {
415 only_once(loc->dir, "root");
416 loc->dir = ensure_absolute_path($2);
418 | STRIP NUM { loc->strip = check_strip_no($2); }
421 fastcgi : SPAWN string {
422 only_oncei(loc->fcgi, "fastcgi");
423 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
425 | string {
426 only_oncei(loc->fcgi, "fastcgi");
427 loc->fcgi = fastcgi_conf($1, NULL, NULL);
429 | TCP string PORT NUM {
430 char *c;
431 if (asprintf(&c, "%d", $4) == -1)
432 err(1, "asprintf");
433 only_oncei(loc->fcgi, "fastcgi");
434 loc->fcgi = fastcgi_conf($2, c, NULL);
436 | TCP string {
437 only_oncei(loc->fcgi, "fastcgi");
438 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
440 | TCP string PORT string {
441 only_oncei(loc->fcgi, "fastcgi");
442 loc->fcgi = fastcgi_conf($2, $4, NULL);
446 optnl : '\n' optnl /* zero or more newlines */
447 | ';' optnl /* semicolons too */
448 | /*empty*/
451 %%
453 static struct keyword {
454 const char *word;
455 int token;
456 } keywords[] = {
457 /* these MUST be sorted */
458 {"alias", ALIAS},
459 {"auto", AUTO},
460 {"block", BLOCK},
461 {"ca", CA},
462 {"cert", CERT},
463 {"cgi", CGI},
464 {"chroot", CHROOT},
465 {"client", CLIENT},
466 {"default", DEFAULT},
467 {"entrypoint", ENTRYPOINT},
468 {"env", ENV},
469 {"fastcgi", FASTCGI},
470 {"for-host", FOR_HOST},
471 {"index", INDEX},
472 {"ipv6", IPV6},
473 {"key", KEY},
474 {"lang", LANG},
475 {"location", LOCATION},
476 {"log", LOG},
477 {"map", MAP},
478 {"mime", MIME},
479 {"ocsp", OCSP},
480 {"off", OFF},
481 {"on", ON},
482 {"param", PARAM},
483 {"port", PORT},
484 {"prefork", PREFORK},
485 {"proto", PROTO},
486 {"protocols", PROTOCOLS},
487 {"proxy", PROXY},
488 {"relay-to", RELAY_TO},
489 {"require", REQUIRE},
490 {"return", RETURN},
491 {"root", ROOT},
492 {"server", SERVER},
493 {"spawn", SPAWN},
494 {"strip", STRIP},
495 {"tcp", TCP},
496 {"to-ext", TOEXT},
497 {"type", TYPE},
498 {"use-tls", USE_TLS},
499 {"user", USER},
500 {"verifyname", VERIFYNAME},
501 };
503 void
504 yyerror(const char *msg, ...)
506 va_list ap;
508 file->errors++;
510 va_start(ap, msg);
511 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
512 vfprintf(stderr, msg, ap);
513 fprintf(stderr, "\n");
514 va_end(ap);
517 void
518 yywarn(const char *msg, ...)
520 va_list ap;
522 va_start(ap, msg);
523 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
524 vfprintf(stderr, msg, ap);
525 fprintf(stderr, "\n");
526 va_end(ap);
529 int
530 kw_cmp(const void *k, const void *e)
532 return strcmp(k, ((struct keyword *)e)->word);
535 int
536 lookup(char *s)
538 const struct keyword *p;
540 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
541 sizeof(keywords[0]), kw_cmp);
543 if (p)
544 return p->token;
545 else
546 return STRING;
549 #define START_EXPAND 1
550 #define DONE_EXPAND 2
552 static int expanding;
554 int
555 igetc(void)
557 int c;
559 while (1) {
560 if (file->ungetpos > 0)
561 c = file->ungetbuf[--file->ungetpos];
562 else
563 c = getc(file->stream);
565 if (c == START_EXPAND)
566 expanding = 1;
567 else if (c == DONE_EXPAND)
568 expanding = 0;
569 else
570 break;
572 return c;
575 int
576 lgetc(int quotec)
578 int c, next;
580 if (quotec) {
581 if ((c = igetc()) == EOF) {
582 yyerror("reached end of file while parsing "
583 "quoted string");
584 if (file == topfile || popfile() == EOF)
585 return EOF;
586 return quotec;
588 return c;
591 while ((c = igetc()) == '\\') {
592 next = igetc();
593 if (next != '\n') {
594 c = next;
595 break;
597 yylval.lineno = file->lineno;
598 file->lineno++;
601 if (c == EOF) {
602 /*
603 * Fake EOL when hit EOF for the first time. This gets line
604 * count right if last line in included file is syntactically
605 * invalid and has no newline.
606 */
607 if (file->eof_reached == 0) {
608 file->eof_reached = 1;
609 return '\n';
611 while (c == EOF) {
612 if (file == topfile || popfile() == EOF)
613 return EOF;
614 c = igetc();
617 return c;
620 void
621 lungetc(int c)
623 if (c == EOF)
624 return;
626 if (file->ungetpos >= file->ungetsize) {
627 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
628 if (p == NULL)
629 err(1, "lungetc");
630 file->ungetbuf = p;
631 file->ungetsize *= 2;
633 file->ungetbuf[file->ungetpos++] = c;
636 int
637 findeol(void)
639 int c;
641 /* Skip to either EOF or the first real EOL. */
642 while (1) {
643 c = lgetc(0);
644 if (c == '\n') {
645 file->lineno++;
646 break;
648 if (c == EOF)
649 break;
651 return ERROR;
654 int
655 yylex(void)
657 char buf[8096];
658 char *p, *val;
659 int quotec, next, c;
660 int token;
662 top:
663 p = buf;
664 while ((c = lgetc(0)) == ' ' || c == '\t')
665 ; /* nothing */
667 yylval.lineno = file->lineno;
668 if (c == '#')
669 while ((c = lgetc(0)) != '\n' && c != EOF)
670 ; /* nothing */
671 if (c == '$' && !expanding) {
672 while (1) {
673 if ((c = lgetc(0)) == EOF)
674 return 0;
675 if (p + 1 >= buf + sizeof(buf) -1) {
676 yyerror("string too long");
677 return findeol();
679 if (isalnum(c) || c == '_') {
680 *p++ = c;
681 continue;
683 *p = '\0';
684 lungetc(c);
685 break;
687 val = symget(buf);
688 if (val == NULL) {
689 yyerror("macro `%s' not defined", buf);
690 return findeol();
692 yylval.v.string = xstrdup(val);
693 return STRING;
695 if (c == '@' && !expanding) {
696 while (1) {
697 if ((c = lgetc(0)) == EOF)
698 return 0;
700 if (p + 1 >= buf + sizeof(buf) - 1) {
701 yyerror("string too long");
702 return findeol();
704 if (isalnum(c) || c == '_') {
705 *p++ = c;
706 continue;
708 *p = '\0';
709 lungetc(c);
710 break;
712 val = symget(buf);
713 if (val == NULL) {
714 yyerror("macro '%s' not defined", buf);
715 return findeol();
717 p = val + strlen(val) - 1;
718 lungetc(DONE_EXPAND);
719 while (p >= val) {
720 lungetc(*p);
721 p--;
723 lungetc(START_EXPAND);
724 goto top;
727 switch (c) {
728 case '\'':
729 case '"':
730 quotec = c;
731 while (1) {
732 if ((c = lgetc(quotec)) == EOF)
733 return 0;
734 if (c == '\n') {
735 file->lineno++;
736 continue;
737 } else if (c == '\\') {
738 if ((next = lgetc(quotec)) == EOF)
739 return (0);
740 if (next == quotec || next == ' ' ||
741 next == '\t')
742 c = next;
743 else if (next == '\n') {
744 file->lineno++;
745 continue;
746 } else
747 lungetc(next);
748 } else if (c == quotec) {
749 *p = '\0';
750 break;
751 } else if (c == '\0') {
752 yyerror("invalid syntax");
753 return findeol();
755 if (p + 1 >= buf + sizeof(buf) - 1) {
756 yyerror("string too long");
757 return findeol();
759 *p++ = c;
761 yylval.v.string = strdup(buf);
762 if (yylval.v.string == NULL)
763 err(1, "yylex: strdup");
764 return STRING;
767 #define allowed_to_end_number(x) \
768 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
770 if (c == '-' || isdigit(c)) {
771 do {
772 *p++ = c;
773 if ((size_t)(p-buf) >= sizeof(buf)) {
774 yyerror("string too long");
775 return findeol();
777 } while ((c = lgetc(0)) != EOF && isdigit(c));
778 lungetc(c);
779 if (p == buf + 1 && buf[0] == '-')
780 goto nodigits;
781 if (c == EOF || allowed_to_end_number(c)) {
782 const char *errstr = NULL;
784 *p = '\0';
785 yylval.v.number = strtonum(buf, LLONG_MIN,
786 LLONG_MAX, &errstr);
787 if (errstr) {
788 yyerror("\"%s\" invalid number: %s",
789 buf, errstr);
790 return findeol();
792 return NUM;
793 } else {
794 nodigits:
795 while (p > buf + 1)
796 lungetc(*--p);
797 c = *--p;
798 if (c == '-')
799 return c;
803 #define allowed_in_string(x) \
804 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
805 x != '{' && x != '}' && \
806 x != '!' && x != '=' && x != '#' && \
807 x != ',' && x != ';'))
809 if (isalnum(c) || c == ':' || c == '_') {
810 do {
811 *p++ = c;
812 if ((size_t)(p-buf) >= sizeof(buf)) {
813 yyerror("string too long");
814 return findeol();
816 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
817 lungetc(c);
818 *p = '\0';
819 if ((token = lookup(buf)) == STRING)
820 yylval.v.string = xstrdup(buf);
821 return token;
823 if (c == '\n') {
824 yylval.lineno = file->lineno;
825 file->lineno++;
827 if (c == EOF)
828 return 0;
829 return c;
832 struct file *
833 pushfile(const char *name, int secret)
835 struct file *nfile;
837 nfile = xcalloc(1, sizeof(*nfile));
838 nfile->name = xstrdup(name);
839 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
840 log_warn(NULL, "can't open %s: %s", nfile->name,
841 strerror(errno));
842 free(nfile->name);
843 free(nfile);
844 return NULL;
846 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
847 nfile->ungetsize = 16;
848 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
849 TAILQ_INSERT_TAIL(&files, nfile, entry);
850 return nfile;
853 int
854 popfile(void)
856 struct file *prev;
858 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
859 prev->errors += file->errors;
861 TAILQ_REMOVE(&files, file, entry);
862 fclose(file->stream);
863 free(file->name);
864 free(file->ungetbuf);
865 free(file);
866 file = prev;
867 return file ? 0 : EOF;
870 void
871 parse_conf(const char *filename)
873 struct sym *sym, *next;
875 file = pushfile(filename, 0);
876 if (file == NULL)
877 exit(1);
878 topfile = file;
880 yyparse();
881 errors = file->errors;
882 popfile();
884 /* Free macros and check which have not been used. */
885 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
886 /* TODO: warn if !sym->used */
887 if (!sym->persist) {
888 free(sym->name);
889 free(sym->val);
890 TAILQ_REMOVE(&symhead, sym, entry);
891 free(sym);
895 if (errors)
896 exit(1);
899 void
900 print_conf(void)
902 struct vhost *h;
903 /* struct location *l; */
904 /* struct envlist *e; */
905 /* struct alist *a; */
907 if (conf.chroot != NULL)
908 printf("chroot \"%s\"\n", conf.chroot);
909 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
910 /* XXX: defined mimes? */
911 printf("port %d\n", conf.port);
912 printf("prefork %d\n", conf.prefork);
913 /* XXX: protocols? */
914 if (conf.user != NULL)
915 printf("user \"%s\"\n", conf.user);
917 TAILQ_FOREACH(h, &hosts, vhosts) {
918 printf("\nserver \"%s\" {\n", h->domain);
919 printf(" cert \"%s\"\n", h->cert);
920 printf(" key \"%s\"\n", h->key);
921 /* TODO: print locations... */
922 printf("}\n");
926 int
927 symset(const char *name, const char *val, int persist)
929 struct sym *sym;
931 TAILQ_FOREACH(sym, &symhead, entry) {
932 if (!strcmp(name, sym->name))
933 break;
936 if (sym != NULL) {
937 if (sym->persist)
938 return 0;
939 else {
940 free(sym->name);
941 free(sym->val);
942 TAILQ_REMOVE(&symhead, sym, entry);
943 free(sym);
947 sym = xcalloc(1, sizeof(*sym));
948 sym->name = xstrdup(name);
949 sym->val = xstrdup(val);
950 sym->used = 0;
951 sym->persist = persist;
953 TAILQ_INSERT_TAIL(&symhead, sym, entry);
954 return 0;
957 int
958 cmdline_symset(char *s)
960 char *sym, *val;
961 int ret;
963 if ((val = strrchr(s, '=')) == NULL)
964 return -1;
965 sym = xcalloc(1, val - s + 1);
966 memcpy(sym, s, val - s);
967 ret = symset(sym, val + 1, 1);
968 free(sym);
969 return ret;
972 char *
973 symget(const char *nam)
975 struct sym *sym;
977 TAILQ_FOREACH(sym, &symhead, entry) {
978 if (strcmp(nam, sym->name) == 0) {
979 sym->used = 1;
980 return sym->val;
983 return NULL;
986 struct vhost *
987 new_vhost(void)
989 return xcalloc(1, sizeof(struct vhost));
992 struct location *
993 new_location(void)
995 struct location *l;
997 l = xcalloc(1, sizeof(*l));
998 l->dirfd = -1;
999 l->fcgi = -1;
1000 return l;
1003 struct proxy *
1004 new_proxy(void)
1006 struct proxy *p;
1008 p = xcalloc(1, sizeof(*p));
1009 p->protocols = TLS_PROTOCOLS_DEFAULT;
1010 return p;
1013 char *
1014 ensure_absolute_path(char *path)
1016 if (path == NULL || *path != '/')
1017 yyerror("not an absolute path: %s", path);
1018 return path;
1021 int
1022 check_block_code(int n)
1024 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1025 yyerror("invalid block code %d", n);
1026 return n;
1029 char *
1030 check_block_fmt(char *fmt)
1032 char *s;
1034 for (s = fmt; *s; ++s) {
1035 if (*s != '%')
1036 continue;
1037 switch (*++s) {
1038 case '%':
1039 case 'p':
1040 case 'q':
1041 case 'P':
1042 case 'N':
1043 break;
1044 default:
1045 yyerror("invalid format specifier %%%c", *s);
1049 return fmt;
1052 int
1053 check_strip_no(int n)
1055 if (n <= 0)
1056 yyerror("invalid strip number %d", n);
1057 return n;
1060 int
1061 check_port_num(int n)
1063 if (n <= 0 || n >= UINT16_MAX)
1064 yyerror("port number is %s: %d",
1065 n <= 0 ? "too small" : "too large",
1066 n);
1067 return n;
1070 int
1071 check_prefork_num(int n)
1073 if (n <= 0 || n >= PROC_MAX)
1074 yyerror("invalid prefork number %d", n);
1075 return n;
1078 void
1079 advance_loc(void)
1081 loc = new_location();
1082 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1085 void
1086 advance_proxy(void)
1088 proxy = new_proxy();
1089 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1092 void
1093 parsehp(char *str, char **host, const char **port, const char *def)
1095 char *at;
1096 const char *errstr;
1098 *host = str;
1100 if ((at = strchr(str, ':')) != NULL) {
1101 *at++ = '\0';
1102 *port = at;
1103 } else
1104 *port = def;
1106 strtonum(*port, 1, UINT16_MAX, &errstr);
1107 if (errstr != NULL)
1108 yyerror("port is %s: %s", errstr, *port);
1111 void
1112 only_once(const void *ptr, const char *name)
1114 if (ptr != NULL)
1115 yyerror("`%s' specified more than once", name);
1118 void
1119 only_oncei(int i, const char *name)
1121 if (i != -1)
1122 yyerror("`%s' specified more than once", name);
1125 int
1126 fastcgi_conf(char *path, char *port, char *prog)
1128 struct fcgi *f;
1129 int i;
1131 for (i = 0; i < FCGI_MAX; ++i) {
1132 f = &fcgi[i];
1134 if (f->path == NULL) {
1135 f->id = i;
1136 f->path = path;
1137 f->port = port;
1138 f->prog = prog;
1139 return i;
1142 /* XXX: what to do with prog? */
1143 if (!strcmp(f->path, path) &&
1144 ((port == NULL && f->port == NULL) ||
1145 !strcmp(f->port, port))) {
1146 free(path);
1147 free(port);
1148 return i;
1152 yyerror("too much `fastcgi' rules defined.");
1153 return -1;
1156 void
1157 add_param(char *name, char *val, int env)
1159 struct envlist *e;
1160 struct envhead *h;
1162 if (env)
1163 h = &host->env;
1164 else
1165 h = &host->params;
1167 e = xcalloc(1, sizeof(*e));
1168 e->name = name;
1169 e->value = val;
1170 if (TAILQ_EMPTY(h))
1171 TAILQ_INSERT_HEAD(h, e, envs);
1172 else
1173 TAILQ_INSERT_TAIL(h, e, envs);