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 USE_TLS 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 | USE_TLS bool {
343 host->proxy.notls = !$2;
345 | VERIFYNAME bool {
346 host->proxy.noverifyname = !$2;
350 locations : /* empty */
351 | locations location optnl
354 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
355 /* drop the starting '/' if any */
356 if (*$3 == '/')
357 memmove($3, $3+1, strlen($3));
358 loc->match = $3;
360 | error '}'
363 locopts : /* empty */
364 | locopts locopt optnl
367 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
368 | BLOCK RETURN NUM string {
369 only_once(loc->block_fmt, "block");
370 loc->block_fmt = check_block_fmt($4);
371 loc->block_code = check_block_code($3);
373 | BLOCK RETURN NUM {
374 only_once(loc->block_fmt, "block");
375 loc->block_fmt = xstrdup("temporary failure");
376 loc->block_code = check_block_code($3);
377 if ($3 >= 30 && $3 < 40)
378 yyerror("missing `meta' for block return %d", $3);
380 | BLOCK {
381 only_once(loc->block_fmt, "block");
382 loc->block_fmt = xstrdup("temporary failure");
383 loc->block_code = 40;
385 | DEFAULT TYPE string {
386 only_once(loc->default_mime, "default type");
387 loc->default_mime = $3;
389 | FASTCGI fastcgi
390 | INDEX string {
391 only_once(loc->index, "index");
392 loc->index = $2;
394 | LANG string {
395 only_once(loc->lang, "lang");
396 loc->lang = $2;
398 | LOG bool { loc->disable_log = !$2; }
399 | REQUIRE CLIENT CA string {
400 only_once(loc->reqca, "require client ca");
401 ensure_absolute_path($4);
402 if ((loc->reqca = load_ca($4)) == NULL)
403 yyerror("couldn't load ca cert: %s", $4);
404 free($4);
406 | ROOT string {
407 only_once(loc->dir, "root");
408 loc->dir = ensure_absolute_path($2);
410 | STRIP NUM { loc->strip = check_strip_no($2); }
413 fastcgi : SPAWN string {
414 only_oncei(loc->fcgi, "fastcgi");
415 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
417 | string {
418 only_oncei(loc->fcgi, "fastcgi");
419 loc->fcgi = fastcgi_conf($1, NULL, NULL);
421 | TCP string PORT NUM {
422 char *c;
423 if (asprintf(&c, "%d", $4) == -1)
424 err(1, "asprintf");
425 only_oncei(loc->fcgi, "fastcgi");
426 loc->fcgi = fastcgi_conf($2, c, NULL);
428 | TCP string {
429 only_oncei(loc->fcgi, "fastcgi");
430 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
432 | TCP string PORT string {
433 only_oncei(loc->fcgi, "fastcgi");
434 loc->fcgi = fastcgi_conf($2, $4, NULL);
438 optnl : '\n' optnl /* zero or more newlines */
439 | ';' optnl /* semicolons too */
440 | /*empty*/
443 %%
445 static struct keyword {
446 const char *word;
447 int token;
448 } keywords[] = {
449 /* these MUST be sorted */
450 {"alias", ALIAS},
451 {"auto", AUTO},
452 {"block", BLOCK},
453 {"ca", CA},
454 {"cert", CERT},
455 {"cgi", CGI},
456 {"chroot", CHROOT},
457 {"client", CLIENT},
458 {"default", DEFAULT},
459 {"entrypoint", ENTRYPOINT},
460 {"env", ENV},
461 {"fastcgi", FASTCGI},
462 {"index", INDEX},
463 {"ipv6", IPV6},
464 {"key", KEY},
465 {"lang", LANG},
466 {"location", LOCATION},
467 {"log", LOG},
468 {"map", MAP},
469 {"mime", MIME},
470 {"ocsp", OCSP},
471 {"off", OFF},
472 {"on", ON},
473 {"param", PARAM},
474 {"port", PORT},
475 {"prefork", PREFORK},
476 {"protocols", PROTOCOLS},
477 {"proxy", PROXY},
478 {"relay-to", RELAY_TO},
479 {"require", REQUIRE},
480 {"return", RETURN},
481 {"root", ROOT},
482 {"server", SERVER},
483 {"spawn", SPAWN},
484 {"strip", STRIP},
485 {"tcp", TCP},
486 {"to-ext", TOEXT},
487 {"type", TYPE},
488 {"use-tls", USE_TLS},
489 {"user", USER},
490 {"verifyname", VERIFYNAME},
491 };
493 void
494 yyerror(const char *msg, ...)
496 va_list ap;
498 file->errors++;
500 va_start(ap, msg);
501 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
502 vfprintf(stderr, msg, ap);
503 fprintf(stderr, "\n");
504 va_end(ap);
507 void
508 yywarn(const char *msg, ...)
510 va_list ap;
512 va_start(ap, msg);
513 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
514 vfprintf(stderr, msg, ap);
515 fprintf(stderr, "\n");
516 va_end(ap);
519 int
520 kw_cmp(const void *k, const void *e)
522 return strcmp(k, ((struct keyword *)e)->word);
525 int
526 lookup(char *s)
528 const struct keyword *p;
530 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
531 sizeof(keywords[0]), kw_cmp);
533 if (p)
534 return p->token;
535 else
536 return STRING;
539 #define START_EXPAND 1
540 #define DONE_EXPAND 2
542 static int expanding;
544 int
545 igetc(void)
547 int c;
549 while (1) {
550 if (file->ungetpos > 0)
551 c = file->ungetbuf[--file->ungetpos];
552 else
553 c = getc(file->stream);
555 if (c == START_EXPAND)
556 expanding = 1;
557 else if (c == DONE_EXPAND)
558 expanding = 0;
559 else
560 break;
562 return c;
565 int
566 lgetc(int quotec)
568 int c, next;
570 if (quotec) {
571 if ((c = igetc()) == EOF) {
572 yyerror("reached end of file while parsing "
573 "quoted string");
574 if (file == topfile || popfile() == EOF)
575 return EOF;
576 return quotec;
578 return c;
581 while ((c = igetc()) == '\\') {
582 next = igetc();
583 if (next != '\n') {
584 c = next;
585 break;
587 yylval.lineno = file->lineno;
588 file->lineno++;
591 if (c == EOF) {
592 /*
593 * Fake EOL when hit EOF for the first time. This gets line
594 * count right if last line in included file is syntactically
595 * invalid and has no newline.
596 */
597 if (file->eof_reached == 0) {
598 file->eof_reached = 1;
599 return '\n';
601 while (c == EOF) {
602 if (file == topfile || popfile() == EOF)
603 return EOF;
604 c = igetc();
607 return c;
610 void
611 lungetc(int c)
613 if (c == EOF)
614 return;
616 if (file->ungetpos >= file->ungetsize) {
617 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
618 if (p == NULL)
619 err(1, "lungetc");
620 file->ungetbuf = p;
621 file->ungetsize *= 2;
623 file->ungetbuf[file->ungetpos++] = c;
626 int
627 findeol(void)
629 int c;
631 /* Skip to either EOF or the first real EOL. */
632 while (1) {
633 c = lgetc(0);
634 if (c == '\n') {
635 file->lineno++;
636 break;
638 if (c == EOF)
639 break;
641 return ERROR;
644 int
645 yylex(void)
647 char buf[8096];
648 char *p, *val;
649 int quotec, next, c;
650 int token;
652 top:
653 p = buf;
654 while ((c = lgetc(0)) == ' ' || c == '\t')
655 ; /* nothing */
657 yylval.lineno = file->lineno;
658 if (c == '#')
659 while ((c = lgetc(0)) != '\n' && c != EOF)
660 ; /* nothing */
661 if (c == '$' && !expanding) {
662 while (1) {
663 if ((c = lgetc(0)) == EOF)
664 return 0;
665 if (p + 1 >= buf + sizeof(buf) -1) {
666 yyerror("string too long");
667 return findeol();
669 if (isalnum(c) || c == '_') {
670 *p++ = c;
671 continue;
673 *p = '\0';
674 lungetc(c);
675 break;
677 val = symget(buf);
678 if (val == NULL) {
679 yyerror("macro `%s' not defined", buf);
680 return findeol();
682 yylval.v.string = xstrdup(val);
683 return STRING;
685 if (c == '@' && !expanding) {
686 while (1) {
687 if ((c = lgetc(0)) == EOF)
688 return 0;
690 if (p + 1 >= buf + sizeof(buf) - 1) {
691 yyerror("string too long");
692 return findeol();
694 if (isalnum(c) || c == '_') {
695 *p++ = c;
696 continue;
698 *p = '\0';
699 lungetc(c);
700 break;
702 val = symget(buf);
703 if (val == NULL) {
704 yyerror("macro '%s' not defined", buf);
705 return findeol();
707 p = val + strlen(val) - 1;
708 lungetc(DONE_EXPAND);
709 while (p >= val) {
710 lungetc(*p);
711 p--;
713 lungetc(START_EXPAND);
714 goto top;
717 switch (c) {
718 case '\'':
719 case '"':
720 quotec = c;
721 while (1) {
722 if ((c = lgetc(quotec)) == EOF)
723 return 0;
724 if (c == '\n') {
725 file->lineno++;
726 continue;
727 } else if (c == '\\') {
728 if ((next = lgetc(quotec)) == EOF)
729 return (0);
730 if (next == quotec || next == ' ' ||
731 next == '\t')
732 c = next;
733 else if (next == '\n') {
734 file->lineno++;
735 continue;
736 } else
737 lungetc(next);
738 } else if (c == quotec) {
739 *p = '\0';
740 break;
741 } else if (c == '\0') {
742 yyerror("invalid syntax");
743 return findeol();
745 if (p + 1 >= buf + sizeof(buf) - 1) {
746 yyerror("string too long");
747 return findeol();
749 *p++ = c;
751 yylval.v.string = strdup(buf);
752 if (yylval.v.string == NULL)
753 err(1, "yylex: strdup");
754 return STRING;
757 #define allowed_to_end_number(x) \
758 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
760 if (c == '-' || isdigit(c)) {
761 do {
762 *p++ = c;
763 if ((size_t)(p-buf) >= sizeof(buf)) {
764 yyerror("string too long");
765 return findeol();
767 } while ((c = lgetc(0)) != EOF && isdigit(c));
768 lungetc(c);
769 if (p == buf + 1 && buf[0] == '-')
770 goto nodigits;
771 if (c == EOF || allowed_to_end_number(c)) {
772 const char *errstr = NULL;
774 *p = '\0';
775 yylval.v.number = strtonum(buf, LLONG_MIN,
776 LLONG_MAX, &errstr);
777 if (errstr) {
778 yyerror("\"%s\" invalid number: %s",
779 buf, errstr);
780 return findeol();
782 return NUM;
783 } else {
784 nodigits:
785 while (p > buf + 1)
786 lungetc(*--p);
787 c = *--p;
788 if (c == '-')
789 return c;
793 #define allowed_in_string(x) \
794 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
795 x != '{' && x != '}' && \
796 x != '!' && x != '=' && x != '#' && \
797 x != ',' && x != ';'))
799 if (isalnum(c) || c == ':' || c == '_') {
800 do {
801 *p++ = c;
802 if ((size_t)(p-buf) >= sizeof(buf)) {
803 yyerror("string too long");
804 return findeol();
806 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
807 lungetc(c);
808 *p = '\0';
809 if ((token = lookup(buf)) == STRING)
810 yylval.v.string = xstrdup(buf);
811 return token;
813 if (c == '\n') {
814 yylval.lineno = file->lineno;
815 file->lineno++;
817 if (c == EOF)
818 return 0;
819 return c;
822 struct file *
823 pushfile(const char *name, int secret)
825 struct file *nfile;
827 nfile = xcalloc(1, sizeof(*nfile));
828 nfile->name = xstrdup(name);
829 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
830 log_warn(NULL, "can't open %s: %s", nfile->name,
831 strerror(errno));
832 free(nfile->name);
833 free(nfile);
834 return NULL;
836 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
837 nfile->ungetsize = 16;
838 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
839 TAILQ_INSERT_TAIL(&files, nfile, entry);
840 return nfile;
843 int
844 popfile(void)
846 struct file *prev;
848 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
849 prev->errors += file->errors;
851 TAILQ_REMOVE(&files, file, entry);
852 fclose(file->stream);
853 free(file->name);
854 free(file->ungetbuf);
855 free(file);
856 file = prev;
857 return file ? 0 : EOF;
860 void
861 parse_conf(const char *filename)
863 struct sym *sym, *next;
865 file = pushfile(filename, 0);
866 if (file == NULL)
867 exit(1);
868 topfile = file;
870 yyparse();
871 errors = file->errors;
872 popfile();
874 /* Free macros and check which have not been used. */
875 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
876 /* TODO: warn if !sym->used */
877 if (!sym->persist) {
878 free(sym->name);
879 free(sym->val);
880 TAILQ_REMOVE(&symhead, sym, entry);
881 free(sym);
885 if (errors)
886 exit(1);
889 void
890 print_conf(void)
892 struct vhost *h;
893 /* struct location *l; */
894 /* struct envlist *e; */
895 /* struct alist *a; */
897 if (conf.chroot != NULL)
898 printf("chroot \"%s\"\n", conf.chroot);
899 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
900 /* XXX: defined mimes? */
901 printf("port %d\n", conf.port);
902 printf("prefork %d\n", conf.prefork);
903 /* XXX: protocols? */
904 if (conf.user != NULL)
905 printf("user \"%s\"\n", conf.user);
907 TAILQ_FOREACH(h, &hosts, vhosts) {
908 printf("\nserver \"%s\" {\n", h->domain);
909 printf(" cert \"%s\"\n", h->cert);
910 printf(" key \"%s\"\n", h->key);
911 /* TODO: print locations... */
912 printf("}\n");
916 int
917 symset(const char *name, const char *val, int persist)
919 struct sym *sym;
921 TAILQ_FOREACH(sym, &symhead, entry) {
922 if (!strcmp(name, sym->name))
923 break;
926 if (sym != NULL) {
927 if (sym->persist)
928 return 0;
929 else {
930 free(sym->name);
931 free(sym->val);
932 TAILQ_REMOVE(&symhead, sym, entry);
933 free(sym);
937 sym = xcalloc(1, sizeof(*sym));
938 sym->name = xstrdup(name);
939 sym->val = xstrdup(val);
940 sym->used = 0;
941 sym->persist = persist;
943 TAILQ_INSERT_TAIL(&symhead, sym, entry);
944 return 0;
947 int
948 cmdline_symset(char *s)
950 char *sym, *val;
951 int ret;
953 if ((val = strrchr(s, '=')) == NULL)
954 return -1;
955 sym = xcalloc(1, val - s + 1);
956 memcpy(sym, s, val - s);
957 ret = symset(sym, val + 1, 1);
958 free(sym);
959 return ret;
962 char *
963 symget(const char *nam)
965 struct sym *sym;
967 TAILQ_FOREACH(sym, &symhead, entry) {
968 if (strcmp(nam, sym->name) == 0) {
969 sym->used = 1;
970 return sym->val;
973 return NULL;
976 struct vhost *
977 new_vhost(void)
979 struct vhost *v;
981 v = xcalloc(1, sizeof(*v));
982 v->proxy.protocols = TLS_PROTOCOLS_DEFAULT;
983 return v;
986 struct location *
987 new_location(void)
989 struct location *l;
991 l = xcalloc(1, sizeof(*l));
992 l->dirfd = -1;
993 l->fcgi = -1;
994 return l;
997 char *
998 ensure_absolute_path(char *path)
1000 if (path == NULL || *path != '/')
1001 yyerror("not an absolute path: %s", path);
1002 return path;
1005 int
1006 check_block_code(int n)
1008 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1009 yyerror("invalid block code %d", n);
1010 return n;
1013 char *
1014 check_block_fmt(char *fmt)
1016 char *s;
1018 for (s = fmt; *s; ++s) {
1019 if (*s != '%')
1020 continue;
1021 switch (*++s) {
1022 case '%':
1023 case 'p':
1024 case 'q':
1025 case 'P':
1026 case 'N':
1027 break;
1028 default:
1029 yyerror("invalid format specifier %%%c", *s);
1033 return fmt;
1036 int
1037 check_strip_no(int n)
1039 if (n <= 0)
1040 yyerror("invalid strip number %d", n);
1041 return n;
1044 int
1045 check_port_num(int n)
1047 if (n <= 0 || n >= UINT16_MAX)
1048 yyerror("port number is %s: %d",
1049 n <= 0 ? "too small" : "too large",
1050 n);
1051 return n;
1054 int
1055 check_prefork_num(int n)
1057 if (n <= 0 || n >= PROC_MAX)
1058 yyerror("invalid prefork number %d", n);
1059 return n;
1062 void
1063 advance_loc(void)
1065 loc = new_location();
1066 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1069 void
1070 only_once(const void *ptr, const char *name)
1072 if (ptr != NULL)
1073 yyerror("`%s' specified more than once", name);
1076 void
1077 only_oncei(int i, const char *name)
1079 if (i != -1)
1080 yyerror("`%s' specified more than once", name);
1083 int
1084 fastcgi_conf(char *path, char *port, char *prog)
1086 struct fcgi *f;
1087 int i;
1089 for (i = 0; i < FCGI_MAX; ++i) {
1090 f = &fcgi[i];
1092 if (f->path == NULL) {
1093 f->id = i;
1094 f->path = path;
1095 f->port = port;
1096 f->prog = prog;
1097 return i;
1100 /* XXX: what to do with prog? */
1101 if (!strcmp(f->path, path) &&
1102 ((port == NULL && f->port == NULL) ||
1103 !strcmp(f->port, port))) {
1104 free(path);
1105 free(port);
1106 return i;
1110 yyerror("too much `fastcgi' rules defined.");
1111 return -1;
1114 void
1115 add_param(char *name, char *val, int env)
1117 struct envlist *e;
1118 struct envhead *h;
1120 if (env)
1121 h = &host->env;
1122 else
1123 h = &host->params;
1125 e = xcalloc(1, sizeof(*e));
1126 e->name = name;
1127 e->value = val;
1128 if (TAILQ_EMPTY(h))
1129 TAILQ_INSERT_HEAD(h, e, envs);
1130 else
1131 TAILQ_INSERT_TAIL(h, e, envs);