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 USER
128 %token VERIFYNAME
130 %token ERROR
132 %token <v.string> STRING
133 %token <v.number> NUM
135 %type <v.number> bool
136 %type <v.string> string
138 %%
140 conf : /* empty */
141 | conf include '\n'
142 | conf '\n'
143 | conf varset '\n'
144 | conf option '\n'
145 | conf vhost '\n'
146 | conf error '\n' { file->errors++; }
149 include : INCLUDE STRING {
150 struct file *nfile;
152 if ((nfile = pushfile($2, 0)) == NULL) {
153 yyerror("failed to include file %s", $2);
154 free($2);
155 YYERROR;
157 free($2);
159 file = nfile;
160 lungetc('\n');
164 bool : ON { $$ = 1; }
165 | OFF { $$ = 0; }
168 string : string STRING {
169 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
170 free($1);
171 free($2);
172 yyerror("string: asprintf: %s", strerror(errno));
173 YYERROR;
175 free($1);
176 free($2);
178 | STRING
181 varset : STRING '=' string {
182 char *s = $1;
183 while (*s++) {
184 if (isspace((unsigned char)*s)) {
185 yyerror("macro name cannot contain "
186 "whitespaces");
187 free($1);
188 free($3);
189 YYERROR;
192 symset($1, $3, 0);
193 free($1);
194 free($3);
198 option : CHROOT string { conf.chroot = $2; }
199 | IPV6 bool { conf.ipv6 = $2; }
200 | MIME STRING string {
201 yywarn("`mime MIME EXT' is deprecated and will be "
202 "removed in a future version, please use the new "
203 "syntax: `map MIME to-ext EXT'");
204 add_mime(&conf.mime, $2, $3);
206 | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
207 | PORT NUM { conf.port = check_port_num($2); }
208 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
209 | PROTOCOLS string {
210 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
211 yyerror("invalid protocols string \"%s\"", $2);
212 free($2);
214 | USER string { conf.user = $2; }
217 vhost : SERVER string {
218 host = new_vhost();
219 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
221 loc = new_location();
222 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
224 loc->match = xstrdup("*");
225 host->domain = $2;
227 if (strstr($2, "xn--") != NULL) {
228 yywarn("\"%s\" looks like punycode: you "
229 "should use the decoded hostname", $2);
231 } '{' optnl servopts locations '}' {
232 if (host->cert == NULL || host->key == NULL)
233 yyerror("invalid vhost definition: %s", $2);
235 | error '}' { yyerror("bad server directive"); }
238 servopts : /* empty */
239 | servopts servopt optnl
242 servopt : ALIAS string {
243 struct alist *a;
245 a = xcalloc(1, sizeof(*a));
246 a->alias = $2;
247 if (TAILQ_EMPTY(&host->aliases))
248 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
249 else
250 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
252 | CERT string {
253 only_once(host->cert, "cert");
254 host->cert = ensure_absolute_path($2);
256 | CGI string {
257 only_once(host->cgi, "cgi");
258 /* drop the starting '/', if any */
259 if (*$2 == '/')
260 memmove($2, $2+1, strlen($2));
261 host->cgi = $2;
263 | ENTRYPOINT string {
264 only_once(host->entrypoint, "entrypoint");
265 while (*$2 == '/')
266 memmove($2, $2+1, strlen($2));
267 host->entrypoint = $2;
269 | ENV string '=' string {
270 add_param($2, $4, 1);
272 | KEY string {
273 only_once(host->key, "key");
274 host->key = ensure_absolute_path($2);
276 | OCSP string {
277 only_once(host->ocsp, "ocsp");
278 host->ocsp = ensure_absolute_path($2);
280 | PARAM string '=' string {
281 add_param($2, $4, 0);
283 | proxy
284 | locopt
287 proxy : PROXY proxy_opt
288 | PROXY '{' optnl proxy_opts '}'
291 proxy_opts : /* empty */
292 | proxy_opts proxy_opt optnl
295 proxy_opt : CERT string {
296 struct proxy *p = &host->proxy;
298 only_once(p->cert, "proxy cert");
299 ensure_absolute_path($2);
300 p->cert = tls_load_file($2, &p->certlen, NULL);
301 if (p->cert == NULL)
302 yyerror("can't load cert %s", $2);
303 free($2);
305 | KEY string {
306 struct proxy *p = &host->proxy;
308 only_once(p->key, "proxy key");
309 ensure_absolute_path($2);
310 p->key = tls_load_file($2, &p->keylen, NULL);
311 if (p->key == NULL)
312 yyerror("can't load key %s", $2);
313 free($2);
315 | PROTOCOLS string {
316 struct proxy *p = &host->proxy;
318 if (tls_config_parse_protocols(&p->protocols, $2) == -1)
319 yyerror("invalid protocols string \"%s\"", $2);
320 free($2);
322 | RELAY_TO string {
323 char *at;
324 const char *errstr;
325 struct proxy *p = &host->proxy;
327 only_once(p->host, "proxy relay-to");
328 p->host = $2;
330 if ((at = strchr($2, ':')) != NULL) {
331 *at++ = '\0';
332 p->port = at;
333 } else
334 p->port = "1965";
336 strtonum(p->port, 1, UINT16_MAX, &errstr);
337 if (errstr != NULL)
338 yyerror("proxy port is %s: %s", errstr,
339 p->port);
341 | VERIFYNAME bool {
342 host->proxy.noverifyname = !$2;
346 locations : /* empty */
347 | locations location optnl
350 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
351 /* drop the starting '/' if any */
352 if (*$3 == '/')
353 memmove($3, $3+1, strlen($3));
354 loc->match = $3;
356 | error '}'
359 locopts : /* empty */
360 | locopts locopt optnl
363 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
364 | BLOCK RETURN NUM string {
365 only_once(loc->block_fmt, "block");
366 loc->block_fmt = check_block_fmt($4);
367 loc->block_code = check_block_code($3);
369 | BLOCK RETURN NUM {
370 only_once(loc->block_fmt, "block");
371 loc->block_fmt = xstrdup("temporary failure");
372 loc->block_code = check_block_code($3);
373 if ($3 >= 30 && $3 < 40)
374 yyerror("missing `meta' for block return %d", $3);
376 | BLOCK {
377 only_once(loc->block_fmt, "block");
378 loc->block_fmt = xstrdup("temporary failure");
379 loc->block_code = 40;
381 | DEFAULT TYPE string {
382 only_once(loc->default_mime, "default type");
383 loc->default_mime = $3;
385 | FASTCGI fastcgi
386 | INDEX string {
387 only_once(loc->index, "index");
388 loc->index = $2;
390 | LANG string {
391 only_once(loc->lang, "lang");
392 loc->lang = $2;
394 | LOG bool { loc->disable_log = !$2; }
395 | REQUIRE CLIENT CA string {
396 only_once(loc->reqca, "require client ca");
397 ensure_absolute_path($4);
398 if ((loc->reqca = load_ca($4)) == NULL)
399 yyerror("couldn't load ca cert: %s", $4);
400 free($4);
402 | ROOT string {
403 only_once(loc->dir, "root");
404 loc->dir = ensure_absolute_path($2);
406 | STRIP NUM { loc->strip = check_strip_no($2); }
409 fastcgi : SPAWN string {
410 only_oncei(loc->fcgi, "fastcgi");
411 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
413 | string {
414 only_oncei(loc->fcgi, "fastcgi");
415 loc->fcgi = fastcgi_conf($1, NULL, NULL);
417 | TCP string PORT NUM {
418 char *c;
419 if (asprintf(&c, "%d", $4) == -1)
420 err(1, "asprintf");
421 only_oncei(loc->fcgi, "fastcgi");
422 loc->fcgi = fastcgi_conf($2, c, NULL);
424 | TCP string {
425 only_oncei(loc->fcgi, "fastcgi");
426 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
428 | TCP string PORT string {
429 only_oncei(loc->fcgi, "fastcgi");
430 loc->fcgi = fastcgi_conf($2, $4, NULL);
434 optnl : '\n' optnl /* zero or more newlines */
435 | ';' optnl /* semicolons too */
436 | /*empty*/
439 %%
441 static struct keyword {
442 const char *word;
443 int token;
444 } keywords[] = {
445 /* these MUST be sorted */
446 {"alias", ALIAS},
447 {"auto", AUTO},
448 {"block", BLOCK},
449 {"ca", CA},
450 {"cert", CERT},
451 {"cgi", CGI},
452 {"chroot", CHROOT},
453 {"client", CLIENT},
454 {"default", DEFAULT},
455 {"entrypoint", ENTRYPOINT},
456 {"env", ENV},
457 {"fastcgi", FASTCGI},
458 {"index", INDEX},
459 {"ipv6", IPV6},
460 {"key", KEY},
461 {"lang", LANG},
462 {"location", LOCATION},
463 {"log", LOG},
464 {"map", MAP},
465 {"mime", MIME},
466 {"ocsp", OCSP},
467 {"off", OFF},
468 {"on", ON},
469 {"param", PARAM},
470 {"port", PORT},
471 {"prefork", PREFORK},
472 {"protocols", PROTOCOLS},
473 {"proxy", PROXY},
474 {"relay-to", RELAY_TO},
475 {"require", REQUIRE},
476 {"return", RETURN},
477 {"root", ROOT},
478 {"server", SERVER},
479 {"spawn", SPAWN},
480 {"strip", STRIP},
481 {"tcp", TCP},
482 {"to-ext", TOEXT},
483 {"type", TYPE},
484 {"user", USER},
485 {"verifyname", VERIFYNAME},
486 };
488 void
489 yyerror(const char *msg, ...)
491 va_list ap;
493 file->errors++;
495 va_start(ap, msg);
496 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
497 vfprintf(stderr, msg, ap);
498 fprintf(stderr, "\n");
499 va_end(ap);
502 void
503 yywarn(const char *msg, ...)
505 va_list ap;
507 va_start(ap, msg);
508 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
509 vfprintf(stderr, msg, ap);
510 fprintf(stderr, "\n");
511 va_end(ap);
514 int
515 kw_cmp(const void *k, const void *e)
517 return strcmp(k, ((struct keyword *)e)->word);
520 int
521 lookup(char *s)
523 const struct keyword *p;
525 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
526 sizeof(keywords[0]), kw_cmp);
528 if (p)
529 return p->token;
530 else
531 return STRING;
534 #define START_EXPAND 1
535 #define DONE_EXPAND 2
537 static int expanding;
539 int
540 igetc(void)
542 int c;
544 while (1) {
545 if (file->ungetpos > 0)
546 c = file->ungetbuf[--file->ungetpos];
547 else
548 c = getc(file->stream);
550 if (c == START_EXPAND)
551 expanding = 1;
552 else if (c == DONE_EXPAND)
553 expanding = 0;
554 else
555 break;
557 return c;
560 int
561 lgetc(int quotec)
563 int c, next;
565 if (quotec) {
566 if ((c = igetc()) == EOF) {
567 yyerror("reached end of file while parsing "
568 "quoted string");
569 if (file == topfile || popfile() == EOF)
570 return EOF;
571 return quotec;
573 return c;
576 while ((c = igetc()) == '\\') {
577 next = igetc();
578 if (next != '\n') {
579 c = next;
580 break;
582 yylval.lineno = file->lineno;
583 file->lineno++;
586 if (c == EOF) {
587 /*
588 * Fake EOL when hit EOF for the first time. This gets line
589 * count right if last line in included file is syntactically
590 * invalid and has no newline.
591 */
592 if (file->eof_reached == 0) {
593 file->eof_reached = 1;
594 return '\n';
596 while (c == EOF) {
597 if (file == topfile || popfile() == EOF)
598 return EOF;
599 c = igetc();
602 return c;
605 void
606 lungetc(int c)
608 if (c == EOF)
609 return;
611 if (file->ungetpos >= file->ungetsize) {
612 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
613 if (p == NULL)
614 err(1, "lungetc");
615 file->ungetbuf = p;
616 file->ungetsize *= 2;
618 file->ungetbuf[file->ungetpos++] = c;
621 int
622 findeol(void)
624 int c;
626 /* Skip to either EOF or the first real EOL. */
627 while (1) {
628 c = lgetc(0);
629 if (c == '\n') {
630 file->lineno++;
631 break;
633 if (c == EOF)
634 break;
636 return ERROR;
639 int
640 yylex(void)
642 char buf[8096];
643 char *p, *val;
644 int quotec, next, c;
645 int token;
647 top:
648 p = buf;
649 while ((c = lgetc(0)) == ' ' || c == '\t')
650 ; /* nothing */
652 yylval.lineno = file->lineno;
653 if (c == '#')
654 while ((c = lgetc(0)) != '\n' && c != EOF)
655 ; /* nothing */
656 if (c == '$' && !expanding) {
657 while (1) {
658 if ((c = lgetc(0)) == EOF)
659 return 0;
660 if (p + 1 >= buf + sizeof(buf) -1) {
661 yyerror("string too long");
662 return findeol();
664 if (isalnum(c) || c == '_') {
665 *p++ = c;
666 continue;
668 *p = '\0';
669 lungetc(c);
670 break;
672 val = symget(buf);
673 if (val == NULL) {
674 yyerror("macro `%s' not defined", buf);
675 return findeol();
677 yylval.v.string = xstrdup(val);
678 return STRING;
680 if (c == '@' && !expanding) {
681 while (1) {
682 if ((c = lgetc(0)) == EOF)
683 return 0;
685 if (p + 1 >= buf + sizeof(buf) - 1) {
686 yyerror("string too long");
687 return findeol();
689 if (isalnum(c) || c == '_') {
690 *p++ = c;
691 continue;
693 *p = '\0';
694 lungetc(c);
695 break;
697 val = symget(buf);
698 if (val == NULL) {
699 yyerror("macro '%s' not defined", buf);
700 return findeol();
702 p = val + strlen(val) - 1;
703 lungetc(DONE_EXPAND);
704 while (p >= val) {
705 lungetc(*p);
706 p--;
708 lungetc(START_EXPAND);
709 goto top;
712 switch (c) {
713 case '\'':
714 case '"':
715 quotec = c;
716 while (1) {
717 if ((c = lgetc(quotec)) == EOF)
718 return 0;
719 if (c == '\n') {
720 file->lineno++;
721 continue;
722 } else if (c == '\\') {
723 if ((next = lgetc(quotec)) == EOF)
724 return (0);
725 if (next == quotec || next == ' ' ||
726 next == '\t')
727 c = next;
728 else if (next == '\n') {
729 file->lineno++;
730 continue;
731 } else
732 lungetc(next);
733 } else if (c == quotec) {
734 *p = '\0';
735 break;
736 } else if (c == '\0') {
737 yyerror("invalid syntax");
738 return findeol();
740 if (p + 1 >= buf + sizeof(buf) - 1) {
741 yyerror("string too long");
742 return findeol();
744 *p++ = c;
746 yylval.v.string = strdup(buf);
747 if (yylval.v.string == NULL)
748 err(1, "yylex: strdup");
749 return STRING;
752 #define allowed_to_end_number(x) \
753 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
755 if (c == '-' || isdigit(c)) {
756 do {
757 *p++ = c;
758 if ((size_t)(p-buf) >= sizeof(buf)) {
759 yyerror("string too long");
760 return findeol();
762 } while ((c = lgetc(0)) != EOF && isdigit(c));
763 lungetc(c);
764 if (p == buf + 1 && buf[0] == '-')
765 goto nodigits;
766 if (c == EOF || allowed_to_end_number(c)) {
767 const char *errstr = NULL;
769 *p = '\0';
770 yylval.v.number = strtonum(buf, LLONG_MIN,
771 LLONG_MAX, &errstr);
772 if (errstr) {
773 yyerror("\"%s\" invalid number: %s",
774 buf, errstr);
775 return findeol();
777 return NUM;
778 } else {
779 nodigits:
780 while (p > buf + 1)
781 lungetc(*--p);
782 c = *--p;
783 if (c == '-')
784 return c;
788 #define allowed_in_string(x) \
789 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
790 x != '{' && x != '}' && \
791 x != '!' && x != '=' && x != '#' && \
792 x != ',' && x != ';'))
794 if (isalnum(c) || c == ':' || c == '_') {
795 do {
796 *p++ = c;
797 if ((size_t)(p-buf) >= sizeof(buf)) {
798 yyerror("string too long");
799 return findeol();
801 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
802 lungetc(c);
803 *p = '\0';
804 if ((token = lookup(buf)) == STRING)
805 yylval.v.string = xstrdup(buf);
806 return token;
808 if (c == '\n') {
809 yylval.lineno = file->lineno;
810 file->lineno++;
812 if (c == EOF)
813 return 0;
814 return c;
817 struct file *
818 pushfile(const char *name, int secret)
820 struct file *nfile;
822 nfile = xcalloc(1, sizeof(*nfile));
823 nfile->name = xstrdup(name);
824 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
825 log_warn(NULL, "can't open %s: %s", nfile->name,
826 strerror(errno));
827 free(nfile->name);
828 free(nfile);
829 return NULL;
831 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
832 nfile->ungetsize = 16;
833 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
834 TAILQ_INSERT_TAIL(&files, nfile, entry);
835 return nfile;
838 int
839 popfile(void)
841 struct file *prev;
843 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
844 prev->errors += file->errors;
846 TAILQ_REMOVE(&files, file, entry);
847 fclose(file->stream);
848 free(file->name);
849 free(file->ungetbuf);
850 free(file);
851 file = prev;
852 return file ? 0 : EOF;
855 void
856 parse_conf(const char *filename)
858 struct sym *sym, *next;
860 file = pushfile(filename, 0);
861 if (file == NULL)
862 exit(1);
863 topfile = file;
865 yyparse();
866 errors = file->errors;
867 popfile();
869 /* Free macros and check which have not been used. */
870 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
871 /* TODO: warn if !sym->used */
872 if (!sym->persist) {
873 free(sym->name);
874 free(sym->val);
875 TAILQ_REMOVE(&symhead, sym, entry);
876 free(sym);
880 if (errors)
881 exit(1);
884 void
885 print_conf(void)
887 struct vhost *h;
888 /* struct location *l; */
889 /* struct envlist *e; */
890 /* struct alist *a; */
892 if (conf.chroot != NULL)
893 printf("chroot \"%s\"\n", conf.chroot);
894 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
895 /* XXX: defined mimes? */
896 printf("port %d\n", conf.port);
897 printf("prefork %d\n", conf.prefork);
898 /* XXX: protocols? */
899 if (conf.user != NULL)
900 printf("user \"%s\"\n", conf.user);
902 TAILQ_FOREACH(h, &hosts, vhosts) {
903 printf("\nserver \"%s\" {\n", h->domain);
904 printf(" cert \"%s\"\n", h->cert);
905 printf(" key \"%s\"\n", h->key);
906 /* TODO: print locations... */
907 printf("}\n");
911 int
912 symset(const char *name, const char *val, int persist)
914 struct sym *sym;
916 TAILQ_FOREACH(sym, &symhead, entry) {
917 if (!strcmp(name, sym->name))
918 break;
921 if (sym != NULL) {
922 if (sym->persist)
923 return 0;
924 else {
925 free(sym->name);
926 free(sym->val);
927 TAILQ_REMOVE(&symhead, sym, entry);
928 free(sym);
932 sym = xcalloc(1, sizeof(*sym));
933 sym->name = xstrdup(name);
934 sym->val = xstrdup(val);
935 sym->used = 0;
936 sym->persist = persist;
938 TAILQ_INSERT_TAIL(&symhead, sym, entry);
939 return 0;
942 int
943 cmdline_symset(char *s)
945 char *sym, *val;
946 int ret;
948 if ((val = strrchr(s, '=')) == NULL)
949 return -1;
950 sym = xcalloc(1, val - s + 1);
951 memcpy(sym, s, val - s);
952 ret = symset(sym, val + 1, 1);
953 free(sym);
954 return ret;
957 char *
958 symget(const char *nam)
960 struct sym *sym;
962 TAILQ_FOREACH(sym, &symhead, entry) {
963 if (strcmp(nam, sym->name) == 0) {
964 sym->used = 1;
965 return sym->val;
968 return NULL;
971 struct vhost *
972 new_vhost(void)
974 struct vhost *v;
976 v = xcalloc(1, sizeof(*v));
977 v->proxy.protocols = TLS_PROTOCOLS_DEFAULT;
978 return v;
981 struct location *
982 new_location(void)
984 struct location *l;
986 l = xcalloc(1, sizeof(*l));
987 l->dirfd = -1;
988 l->fcgi = -1;
989 return l;
992 char *
993 ensure_absolute_path(char *path)
995 if (path == NULL || *path != '/')
996 yyerror("not an absolute path: %s", path);
997 return path;
1000 int
1001 check_block_code(int n)
1003 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1004 yyerror("invalid block code %d", n);
1005 return n;
1008 char *
1009 check_block_fmt(char *fmt)
1011 char *s;
1013 for (s = fmt; *s; ++s) {
1014 if (*s != '%')
1015 continue;
1016 switch (*++s) {
1017 case '%':
1018 case 'p':
1019 case 'q':
1020 case 'P':
1021 case 'N':
1022 break;
1023 default:
1024 yyerror("invalid format specifier %%%c", *s);
1028 return fmt;
1031 int
1032 check_strip_no(int n)
1034 if (n <= 0)
1035 yyerror("invalid strip number %d", n);
1036 return n;
1039 int
1040 check_port_num(int n)
1042 if (n <= 0 || n >= UINT16_MAX)
1043 yyerror("port number is %s: %d",
1044 n <= 0 ? "too small" : "too large",
1045 n);
1046 return n;
1049 int
1050 check_prefork_num(int n)
1052 if (n <= 0 || n >= PROC_MAX)
1053 yyerror("invalid prefork number %d", n);
1054 return n;
1057 void
1058 advance_loc(void)
1060 loc = new_location();
1061 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1064 void
1065 only_once(const void *ptr, const char *name)
1067 if (ptr != NULL)
1068 yyerror("`%s' specified more than once", name);
1071 void
1072 only_oncei(int i, const char *name)
1074 if (i != -1)
1075 yyerror("`%s' specified more than once", name);
1078 int
1079 fastcgi_conf(char *path, char *port, char *prog)
1081 struct fcgi *f;
1082 int i;
1084 for (i = 0; i < FCGI_MAX; ++i) {
1085 f = &fcgi[i];
1087 if (f->path == NULL) {
1088 f->id = i;
1089 f->path = path;
1090 f->port = port;
1091 f->prog = prog;
1092 return i;
1095 /* XXX: what to do with prog? */
1096 if (!strcmp(f->path, path) &&
1097 ((port == NULL && f->port == NULL) ||
1098 !strcmp(f->port, port))) {
1099 free(path);
1100 free(port);
1101 return i;
1105 yyerror("too much `fastcgi' rules defined.");
1106 return -1;
1109 void
1110 add_param(char *name, char *val, int env)
1112 struct envlist *e;
1113 struct envhead *h;
1115 if (env)
1116 h = &host->env;
1117 else
1118 h = &host->params;
1120 e = xcalloc(1, sizeof(*e));
1121 e->name = name;
1122 e->value = val;
1123 if (TAILQ_EMPTY(h))
1124 TAILQ_INSERT_HEAD(h, e, envs);
1125 else
1126 TAILQ_INSERT_TAIL(h, e, envs);