Blob


1 %{
3 /*
4 * Copyright (c) 2021 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 char *ensure_absolute_path(char*);
85 int check_block_code(int);
86 char *check_block_fmt(char*);
87 int check_strip_no(int);
88 int check_port_num(int);
89 int check_prefork_num(int);
90 void advance_loc(void);
91 void only_once(const void*, const char*);
92 void only_oncei(int, const char*);
93 int fastcgi_conf(char *, char *, char *);
94 void add_param(char *, char *, int);
96 static struct vhost *host;
97 static struct location *loc;
98 static int errors;
100 typedef struct {
101 union {
102 char *string;
103 int number;
104 } v;
105 int lineno;
106 } YYSTYPE;
108 %}
110 /* for bison: */
111 /* %define parse.error verbose */
113 %token ALIAS AUTO
114 %token BLOCK
115 %token CA CERT CGI CHROOT CLIENT
116 %token DEFAULT
117 %token ENTRYPOINT ENV
118 %token FASTCGI
119 %token INCLUDE INDEX IPV6
120 %token KEY
121 %token LANG LOCATION LOG
122 %token MAP MIME
123 %token OCSP OFF ON
124 %token PARAM PORT PREFORK PROTOCOLS PROXY
125 %token RELAY_TO REQUIRE RETURN ROOT
126 %token SERVER SPAWN STRIP
127 %token TCP TOEXT TYPE
128 %token USER
129 %token VERIFYNAME
131 %token ERROR
133 %token <v.string> STRING
134 %token <v.number> NUM
136 %type <v.number> bool
137 %type <v.string> string
139 %%
141 conf : /* empty */
142 | conf include '\n'
143 | conf '\n'
144 | conf varset '\n'
145 | conf option '\n'
146 | conf vhost '\n'
147 | conf error '\n' { file->errors++; }
150 include : INCLUDE STRING {
151 struct file *nfile;
153 if ((nfile = pushfile($2, 0)) == NULL) {
154 yyerror("failed to include file %s", $2);
155 free($2);
156 YYERROR;
158 free($2);
160 file = nfile;
161 lungetc('\n');
165 bool : ON { $$ = 1; }
166 | OFF { $$ = 0; }
169 string : string STRING {
170 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
171 free($1);
172 free($2);
173 yyerror("string: asprintf: %s", strerror(errno));
174 YYERROR;
176 free($1);
177 free($2);
179 | STRING
182 varset : STRING '=' string {
183 char *s = $1;
184 while (*s++) {
185 if (isspace((unsigned char)*s)) {
186 yyerror("macro name cannot contain "
187 "whitespaces");
188 free($1);
189 free($3);
190 YYERROR;
193 symset($1, $3, 0);
194 free($1);
195 free($3);
199 option : CHROOT string { conf.chroot = $2; }
200 | IPV6 bool { conf.ipv6 = $2; }
201 | MIME STRING string {
202 yywarn("`mime MIME EXT' is deprecated and will be "
203 "removed in a future version, please use the new "
204 "syntax: `map MIME to-ext EXT'");
205 add_mime(&conf.mime, $2, $3);
207 | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
208 | PORT NUM { conf.port = check_port_num($2); }
209 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
210 | PROTOCOLS string {
211 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
212 yyerror("invalid protocols string \"%s\"", $2);
213 free($2);
215 | USER string { conf.user = $2; }
218 vhost : SERVER string {
219 host = new_vhost();
220 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
222 loc = new_location();
223 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
225 loc->match = xstrdup("*");
226 host->domain = $2;
228 if (strstr($2, "xn--") != NULL) {
229 yywarn("\"%s\" looks like punycode: you "
230 "should use the decoded hostname", $2);
232 } '{' optnl servopts locations '}' {
233 if (host->cert == NULL || host->key == NULL)
234 yyerror("invalid vhost definition: %s", $2);
236 | error '}' { yyerror("bad server directive"); }
239 servopts : /* empty */
240 | servopts servopt optnl
243 servopt : ALIAS string {
244 struct alist *a;
246 a = xcalloc(1, sizeof(*a));
247 a->alias = $2;
248 if (TAILQ_EMPTY(&host->aliases))
249 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
250 else
251 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
253 | CERT string {
254 only_once(host->cert, "cert");
255 host->cert = ensure_absolute_path($2);
257 | CGI string {
258 only_once(host->cgi, "cgi");
259 /* drop the starting '/', if any */
260 if (*$2 == '/')
261 memmove($2, $2+1, strlen($2));
262 host->cgi = $2;
264 | ENTRYPOINT string {
265 only_once(host->entrypoint, "entrypoint");
266 while (*$2 == '/')
267 memmove($2, $2+1, strlen($2));
268 host->entrypoint = $2;
270 | ENV string '=' string {
271 add_param($2, $4, 1);
273 | KEY string {
274 only_once(host->key, "key");
275 host->key = ensure_absolute_path($2);
277 | OCSP string {
278 only_once(host->ocsp, "ocsp");
279 host->ocsp = ensure_absolute_path($2);
281 | PARAM string '=' string {
282 add_param($2, $4, 0);
284 | proxy
285 | locopt
288 proxy : PROXY proxy_opt
289 | PROXY '{' optnl proxy_opts '}'
292 proxy_opts : /* empty */
293 | proxy_opts proxy_opt optnl
296 proxy_opt : CERT string {
297 struct proxy *p = &host->proxy;
299 only_once(p->cert, "proxy cert");
300 ensure_absolute_path($2);
301 p->cert = tls_load_file($2, &p->certlen, NULL);
302 if (p->cert == NULL)
303 yyerror("can't load cert %s", $2);
304 free($2);
306 | KEY string {
307 struct proxy *p = &host->proxy;
309 only_once(p->key, "proxy key");
310 ensure_absolute_path($2);
311 p->key = tls_load_file($2, &p->keylen, NULL);
312 if (p->key == NULL)
313 yyerror("can't load key %s", $2);
314 free($2);
316 | PROTOCOLS string {
317 struct proxy *p = &host->proxy;
319 if (tls_config_parse_protocols(&p->protocols, $2) == -1)
320 yyerror("invalid protocols string \"%s\"", $2);
321 free($2);
323 | RELAY_TO string {
324 char *at;
325 const char *errstr;
326 struct proxy *p = &host->proxy;
328 only_once(p->host, "proxy relay-to");
329 p->host = $2;
331 if ((at = strchr($2, ':')) != NULL) {
332 *at++ = '\0';
333 p->port = at;
334 } else
335 p->port = "1965";
337 strtonum(p->port, 1, UINT16_MAX, &errstr);
338 if (errstr != NULL)
339 yyerror("proxy port is %s: %s", errstr,
340 p->port);
342 | VERIFYNAME bool {
343 host->proxy.noverifyname = !$2;
347 locations : /* empty */
348 | locations location optnl
351 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
352 /* drop the starting '/' if any */
353 if (*$3 == '/')
354 memmove($3, $3+1, strlen($3));
355 loc->match = $3;
357 | error '}'
360 locopts : /* empty */
361 | locopts locopt optnl
364 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
365 | BLOCK RETURN NUM string {
366 only_once(loc->block_fmt, "block");
367 loc->block_fmt = check_block_fmt($4);
368 loc->block_code = check_block_code($3);
370 | BLOCK RETURN NUM {
371 only_once(loc->block_fmt, "block");
372 loc->block_fmt = xstrdup("temporary failure");
373 loc->block_code = check_block_code($3);
374 if ($3 >= 30 && $3 < 40)
375 yyerror("missing `meta' for block return %d", $3);
377 | BLOCK {
378 only_once(loc->block_fmt, "block");
379 loc->block_fmt = xstrdup("temporary failure");
380 loc->block_code = 40;
382 | DEFAULT TYPE string {
383 only_once(loc->default_mime, "default type");
384 loc->default_mime = $3;
386 | FASTCGI fastcgi
387 | INDEX string {
388 only_once(loc->index, "index");
389 loc->index = $2;
391 | LANG string {
392 only_once(loc->lang, "lang");
393 loc->lang = $2;
395 | LOG bool { loc->disable_log = !$2; }
396 | REQUIRE CLIENT CA string {
397 only_once(loc->reqca, "require client ca");
398 ensure_absolute_path($4);
399 if ((loc->reqca = load_ca($4)) == NULL)
400 yyerror("couldn't load ca cert: %s", $4);
401 free($4);
403 | ROOT string {
404 only_once(loc->dir, "root");
405 loc->dir = ensure_absolute_path($2);
407 | STRIP NUM { loc->strip = check_strip_no($2); }
410 fastcgi : SPAWN string {
411 only_oncei(loc->fcgi, "fastcgi");
412 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
414 | string {
415 only_oncei(loc->fcgi, "fastcgi");
416 loc->fcgi = fastcgi_conf($1, NULL, NULL);
418 | TCP string PORT NUM {
419 char *c;
420 if (asprintf(&c, "%d", $4) == -1)
421 err(1, "asprintf");
422 only_oncei(loc->fcgi, "fastcgi");
423 loc->fcgi = fastcgi_conf($2, c, NULL);
425 | TCP string {
426 only_oncei(loc->fcgi, "fastcgi");
427 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
429 | TCP string PORT string {
430 only_oncei(loc->fcgi, "fastcgi");
431 loc->fcgi = fastcgi_conf($2, $4, NULL);
435 optnl : '\n' optnl /* zero or more newlines */
436 | ';' optnl /* semicolons too */
437 | /*empty*/
440 %%
442 static struct keyword {
443 const char *word;
444 int token;
445 } keywords[] = {
446 /* these MUST be sorted */
447 {"alias", ALIAS},
448 {"auto", AUTO},
449 {"block", BLOCK},
450 {"ca", CA},
451 {"cert", CERT},
452 {"cgi", CGI},
453 {"chroot", CHROOT},
454 {"client", CLIENT},
455 {"default", DEFAULT},
456 {"entrypoint", ENTRYPOINT},
457 {"env", ENV},
458 {"fastcgi", FASTCGI},
459 {"index", INDEX},
460 {"ipv6", IPV6},
461 {"key", KEY},
462 {"lang", LANG},
463 {"location", LOCATION},
464 {"log", LOG},
465 {"map", MAP},
466 {"mime", MIME},
467 {"ocsp", OCSP},
468 {"off", OFF},
469 {"on", ON},
470 {"param", PARAM},
471 {"port", PORT},
472 {"prefork", PREFORK},
473 {"protocols", PROTOCOLS},
474 {"proxy", PROXY},
475 {"relay-to", RELAY_TO},
476 {"require", REQUIRE},
477 {"return", RETURN},
478 {"root", ROOT},
479 {"server", SERVER},
480 {"spawn", SPAWN},
481 {"strip", STRIP},
482 {"tcp", TCP},
483 {"to-ext", TOEXT},
484 {"type", TYPE},
485 {"user", USER},
486 {"verifyname", VERIFYNAME},
487 };
489 void
490 yyerror(const char *msg, ...)
492 va_list ap;
494 file->errors++;
496 va_start(ap, msg);
497 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
498 vfprintf(stderr, msg, ap);
499 fprintf(stderr, "\n");
500 va_end(ap);
503 void
504 yywarn(const char *msg, ...)
506 va_list ap;
508 va_start(ap, msg);
509 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
510 vfprintf(stderr, msg, ap);
511 fprintf(stderr, "\n");
512 va_end(ap);
515 int
516 kw_cmp(const void *k, const void *e)
518 return strcmp(k, ((struct keyword *)e)->word);
521 int
522 lookup(char *s)
524 const struct keyword *p;
526 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
527 sizeof(keywords[0]), kw_cmp);
529 if (p)
530 return p->token;
531 else
532 return STRING;
535 #define START_EXPAND 1
536 #define DONE_EXPAND 2
538 static int expanding;
540 int
541 igetc(void)
543 int c;
545 while (1) {
546 if (file->ungetpos > 0)
547 c = file->ungetbuf[--file->ungetpos];
548 else
549 c = getc(file->stream);
551 if (c == START_EXPAND)
552 expanding = 1;
553 else if (c == DONE_EXPAND)
554 expanding = 0;
555 else
556 break;
558 return c;
561 int
562 lgetc(int quotec)
564 int c, next;
566 if (quotec) {
567 if ((c = igetc()) == EOF) {
568 yyerror("reached end of file while parsing "
569 "quoted string");
570 if (file == topfile || popfile() == EOF)
571 return EOF;
572 return quotec;
574 return c;
577 while ((c = igetc()) == '\\') {
578 next = igetc();
579 if (next != '\n') {
580 c = next;
581 break;
583 yylval.lineno = file->lineno;
584 file->lineno++;
587 if (c == EOF) {
588 /*
589 * Fake EOL when hit EOF for the first time. This gets line
590 * count right if last line in included file is syntactically
591 * invalid and has no newline.
592 */
593 if (file->eof_reached == 0) {
594 file->eof_reached = 1;
595 return '\n';
597 while (c == EOF) {
598 if (file == topfile || popfile() == EOF)
599 return EOF;
600 c = igetc();
603 return c;
606 void
607 lungetc(int c)
609 if (c == EOF)
610 return;
612 if (file->ungetpos >= file->ungetsize) {
613 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
614 if (p == NULL)
615 err(1, "lungetc");
616 file->ungetbuf = p;
617 file->ungetsize *= 2;
619 file->ungetbuf[file->ungetpos++] = c;
622 int
623 findeol(void)
625 int c;
627 /* Skip to either EOF or the first real EOL. */
628 while (1) {
629 c = lgetc(0);
630 if (c == '\n') {
631 file->lineno++;
632 break;
634 if (c == EOF)
635 break;
637 return ERROR;
640 int
641 yylex(void)
643 char buf[8096];
644 char *p, *val;
645 int quotec, next, c;
646 int token;
648 top:
649 p = buf;
650 while ((c = lgetc(0)) == ' ' || c == '\t')
651 ; /* nothing */
653 yylval.lineno = file->lineno;
654 if (c == '#')
655 while ((c = lgetc(0)) != '\n' && c != EOF)
656 ; /* nothing */
657 if (c == '$' && !expanding) {
658 while (1) {
659 if ((c = lgetc(0)) == EOF)
660 return 0;
661 if (p + 1 >= buf + sizeof(buf) -1) {
662 yyerror("string too long");
663 return findeol();
665 if (isalnum(c) || c == '_') {
666 *p++ = c;
667 continue;
669 *p = '\0';
670 lungetc(c);
671 break;
673 val = symget(buf);
674 if (val == NULL) {
675 yyerror("macro `%s' not defined", buf);
676 return findeol();
678 yylval.v.string = xstrdup(val);
679 return STRING;
681 if (c == '@' && !expanding) {
682 while (1) {
683 if ((c = lgetc(0)) == EOF)
684 return 0;
686 if (p + 1 >= buf + sizeof(buf) - 1) {
687 yyerror("string too long");
688 return findeol();
690 if (isalnum(c) || c == '_') {
691 *p++ = c;
692 continue;
694 *p = '\0';
695 lungetc(c);
696 break;
698 val = symget(buf);
699 if (val == NULL) {
700 yyerror("macro '%s' not defined", buf);
701 return findeol();
703 p = val + strlen(val) - 1;
704 lungetc(DONE_EXPAND);
705 while (p >= val) {
706 lungetc(*p);
707 p--;
709 lungetc(START_EXPAND);
710 goto top;
713 switch (c) {
714 case '\'':
715 case '"':
716 quotec = c;
717 while (1) {
718 if ((c = lgetc(quotec)) == EOF)
719 return 0;
720 if (c == '\n') {
721 file->lineno++;
722 continue;
723 } else if (c == '\\') {
724 if ((next = lgetc(quotec)) == EOF)
725 return (0);
726 if (next == quotec || next == ' ' ||
727 next == '\t')
728 c = next;
729 else if (next == '\n') {
730 file->lineno++;
731 continue;
732 } else
733 lungetc(next);
734 } else if (c == quotec) {
735 *p = '\0';
736 break;
737 } else if (c == '\0') {
738 yyerror("invalid syntax");
739 return findeol();
741 if (p + 1 >= buf + sizeof(buf) - 1) {
742 yyerror("string too long");
743 return findeol();
745 *p++ = c;
747 yylval.v.string = strdup(buf);
748 if (yylval.v.string == NULL)
749 err(1, "yylex: strdup");
750 return STRING;
753 #define allowed_to_end_number(x) \
754 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
756 if (c == '-' || isdigit(c)) {
757 do {
758 *p++ = c;
759 if ((size_t)(p-buf) >= sizeof(buf)) {
760 yyerror("string too long");
761 return findeol();
763 } while ((c = lgetc(0)) != EOF && isdigit(c));
764 lungetc(c);
765 if (p == buf + 1 && buf[0] == '-')
766 goto nodigits;
767 if (c == EOF || allowed_to_end_number(c)) {
768 const char *errstr = NULL;
770 *p = '\0';
771 yylval.v.number = strtonum(buf, LLONG_MIN,
772 LLONG_MAX, &errstr);
773 if (errstr) {
774 yyerror("\"%s\" invalid number: %s",
775 buf, errstr);
776 return findeol();
778 return NUM;
779 } else {
780 nodigits:
781 while (p > buf + 1)
782 lungetc(*--p);
783 c = *--p;
784 if (c == '-')
785 return c;
789 #define allowed_in_string(x) \
790 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
791 x != '{' && x != '}' && \
792 x != '!' && x != '=' && x != '#' && \
793 x != ',' && x != ';'))
795 if (isalnum(c) || c == ':' || c == '_') {
796 do {
797 *p++ = c;
798 if ((size_t)(p-buf) >= sizeof(buf)) {
799 yyerror("string too long");
800 return findeol();
802 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
803 lungetc(c);
804 *p = '\0';
805 if ((token = lookup(buf)) == STRING)
806 yylval.v.string = xstrdup(buf);
807 return token;
809 if (c == '\n') {
810 yylval.lineno = file->lineno;
811 file->lineno++;
813 if (c == EOF)
814 return 0;
815 return c;
818 struct file *
819 pushfile(const char *name, int secret)
821 struct file *nfile;
823 nfile = xcalloc(1, sizeof(*nfile));
824 nfile->name = xstrdup(name);
825 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
826 log_warn(NULL, "can't open %s: %s", nfile->name,
827 strerror(errno));
828 free(nfile->name);
829 free(nfile);
830 return NULL;
832 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
833 nfile->ungetsize = 16;
834 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
835 TAILQ_INSERT_TAIL(&files, nfile, entry);
836 return nfile;
839 int
840 popfile(void)
842 struct file *prev;
844 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
845 prev->errors += file->errors;
847 TAILQ_REMOVE(&files, file, entry);
848 fclose(file->stream);
849 free(file->name);
850 free(file->ungetbuf);
851 free(file);
852 file = prev;
853 return file ? 0 : EOF;
856 void
857 parse_conf(const char *filename)
859 struct sym *sym, *next;
861 file = pushfile(filename, 0);
862 if (file == NULL)
863 exit(1);
864 topfile = file;
866 yyparse();
867 errors = file->errors;
868 popfile();
870 /* Free macros and check which have not been used. */
871 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
872 /* TODO: warn if !sym->used */
873 if (!sym->persist) {
874 free(sym->name);
875 free(sym->val);
876 TAILQ_REMOVE(&symhead, sym, entry);
877 free(sym);
881 if (errors)
882 exit(1);
885 void
886 print_conf(void)
888 struct vhost *h;
889 /* struct location *l; */
890 /* struct envlist *e; */
891 /* struct alist *a; */
893 if (conf.chroot != NULL)
894 printf("chroot \"%s\"\n", conf.chroot);
895 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
896 /* XXX: defined mimes? */
897 printf("port %d\n", conf.port);
898 printf("prefork %d\n", conf.prefork);
899 /* XXX: protocols? */
900 if (conf.user != NULL)
901 printf("user \"%s\"\n", conf.user);
903 TAILQ_FOREACH(h, &hosts, vhosts) {
904 printf("\nserver \"%s\" {\n", h->domain);
905 printf(" cert \"%s\"\n", h->cert);
906 printf(" key \"%s\"\n", h->key);
907 /* TODO: print locations... */
908 printf("}\n");
912 int
913 symset(const char *name, const char *val, int persist)
915 struct sym *sym;
917 TAILQ_FOREACH(sym, &symhead, entry) {
918 if (!strcmp(name, sym->name))
919 break;
922 if (sym != NULL) {
923 if (sym->persist)
924 return 0;
925 else {
926 free(sym->name);
927 free(sym->val);
928 TAILQ_REMOVE(&symhead, sym, entry);
929 free(sym);
933 sym = xcalloc(1, sizeof(*sym));
934 sym->name = xstrdup(name);
935 sym->val = xstrdup(val);
936 sym->used = 0;
937 sym->persist = persist;
939 TAILQ_INSERT_TAIL(&symhead, sym, entry);
940 return 0;
943 int
944 cmdline_symset(char *s)
946 char *sym, *val;
947 int ret;
949 if ((val = strrchr(s, '=')) == NULL)
950 return -1;
951 sym = xcalloc(1, val - s + 1);
952 memcpy(sym, s, val - s);
953 ret = symset(sym, val + 1, 1);
954 free(sym);
955 return ret;
958 char *
959 symget(const char *nam)
961 struct sym *sym;
963 TAILQ_FOREACH(sym, &symhead, entry) {
964 if (strcmp(nam, sym->name) == 0) {
965 sym->used = 1;
966 return sym->val;
969 return NULL;
972 struct vhost *
973 new_vhost(void)
975 struct vhost *v;
977 v = xcalloc(1, sizeof(*v));
978 v->proxy.protocols = TLS_PROTOCOLS_DEFAULT;
979 return v;
982 struct location *
983 new_location(void)
985 struct location *l;
987 l = xcalloc(1, sizeof(*l));
988 l->dirfd = -1;
989 l->fcgi = -1;
990 return l;
993 char *
994 ensure_absolute_path(char *path)
996 if (path == NULL || *path != '/')
997 yyerror("not an absolute path: %s", path);
998 return path;
1001 int
1002 check_block_code(int n)
1004 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1005 yyerror("invalid block code %d", n);
1006 return n;
1009 char *
1010 check_block_fmt(char *fmt)
1012 char *s;
1014 for (s = fmt; *s; ++s) {
1015 if (*s != '%')
1016 continue;
1017 switch (*++s) {
1018 case '%':
1019 case 'p':
1020 case 'q':
1021 case 'P':
1022 case 'N':
1023 break;
1024 default:
1025 yyerror("invalid format specifier %%%c", *s);
1029 return fmt;
1032 int
1033 check_strip_no(int n)
1035 if (n <= 0)
1036 yyerror("invalid strip number %d", n);
1037 return n;
1040 int
1041 check_port_num(int n)
1043 if (n <= 0 || n >= UINT16_MAX)
1044 yyerror("port number is %s: %d",
1045 n <= 0 ? "too small" : "too large",
1046 n);
1047 return n;
1050 int
1051 check_prefork_num(int n)
1053 if (n <= 0 || n >= PROC_MAX)
1054 yyerror("invalid prefork number %d", n);
1055 return n;
1058 void
1059 advance_loc(void)
1061 loc = new_location();
1062 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1065 void
1066 only_once(const void *ptr, const char *name)
1068 if (ptr != NULL)
1069 yyerror("`%s' specified more than once", name);
1072 void
1073 only_oncei(int i, const char *name)
1075 if (i != -1)
1076 yyerror("`%s' specified more than once", name);
1079 int
1080 fastcgi_conf(char *path, char *port, char *prog)
1082 struct fcgi *f;
1083 int i;
1085 for (i = 0; i < FCGI_MAX; ++i) {
1086 f = &fcgi[i];
1088 if (f->path == NULL) {
1089 f->id = i;
1090 f->path = path;
1091 f->port = port;
1092 f->prog = prog;
1093 return i;
1096 /* XXX: what to do with prog? */
1097 if (!strcmp(f->path, path) &&
1098 ((port == NULL && f->port == NULL) ||
1099 !strcmp(f->port, port))) {
1100 free(path);
1101 free(port);
1102 return i;
1106 yyerror("too much `fastcgi' rules defined.");
1107 return -1;
1110 void
1111 add_param(char *name, char *val, int env)
1113 struct envlist *e;
1114 struct envhead *h;
1116 if (env)
1117 h = &host->env;
1118 else
1119 h = &host->params;
1121 e = xcalloc(1, sizeof(*e));
1122 e->name = name;
1123 e->value = val;
1124 if (TAILQ_EMPTY(h))
1125 TAILQ_INSERT_HEAD(h, e, envs);
1126 else
1127 TAILQ_INSERT_TAIL(h, e, envs);