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);
213 | USER string { conf.user = $2; }
216 vhost : SERVER string {
217 host = new_vhost();
218 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
220 loc = new_location();
221 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
223 loc->match = xstrdup("*");
224 host->domain = $2;
226 if (strstr($2, "xn--") != NULL) {
227 yywarn("\"%s\" looks like punycode: you "
228 "should use the decoded hostname", $2);
230 } '{' optnl servopts locations '}' {
231 if (host->cert == NULL || host->key == NULL)
232 yyerror("invalid vhost definition: %s", $2);
234 | error '}' { yyerror("bad server directive"); }
237 servopts : /* empty */
238 | servopts servopt optnl
241 servopt : ALIAS string {
242 struct alist *a;
244 a = xcalloc(1, sizeof(*a));
245 a->alias = $2;
246 if (TAILQ_EMPTY(&host->aliases))
247 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
248 else
249 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
251 | CERT string {
252 only_once(host->cert, "cert");
253 host->cert = ensure_absolute_path($2);
255 | CGI string {
256 only_once(host->cgi, "cgi");
257 /* drop the starting '/', if any */
258 if (*$2 == '/')
259 memmove($2, $2+1, strlen($2));
260 host->cgi = $2;
262 | ENTRYPOINT string {
263 only_once(host->entrypoint, "entrypoint");
264 while (*$2 == '/')
265 memmove($2, $2+1, strlen($2));
266 host->entrypoint = $2;
268 | ENV string '=' string {
269 add_param($2, $4, 1);
271 | KEY string {
272 only_once(host->key, "key");
273 host->key = ensure_absolute_path($2);
275 | OCSP string {
276 only_once(host->ocsp, "ocsp");
277 host->ocsp = ensure_absolute_path($2);
279 | PARAM string '=' string {
280 add_param($2, $4, 0);
282 | proxy
283 | locopt
286 proxy : PROXY proxy_opt
287 | PROXY '{' optnl proxy_opts '}'
290 proxy_opts : /* empty */
291 | proxy_opts proxy_opt optnl
294 proxy_opt : CERT string {
295 struct proxy *p = &host->proxy;
297 only_once(p->cert, "proxy cert");
298 ensure_absolute_path($2);
299 p->cert = tls_load_file($2, &p->certlen, NULL);
300 if (p->cert == NULL)
301 yyerror("can't load cert %s", $2);
303 | KEY string {
304 struct proxy *p = &host->proxy;
306 only_once(p->key, "proxy key");
307 ensure_absolute_path($2);
308 p->key = tls_load_file($2, &p->keylen, NULL);
309 if (p->key == NULL)
310 yyerror("can't load key %s", $2);
312 | PROTOCOLS string {
313 struct proxy *p = &host->proxy;
315 if (tls_config_parse_protocols(&p->protocols, $2) == -1)
316 yyerror("invalid protocols string \"%s\"", $2);
318 | RELAY_TO string {
319 char *at;
320 const char *errstr;
321 struct proxy *p = &host->proxy;
323 only_once(p->host, "proxy relay-to");
324 p->host = $2;
326 if ((at = strchr($2, ':')) != NULL) {
327 *at++ = '\0';
328 p->port = at;
329 } else
330 p->port = "1965";
332 strtonum(p->port, 1, UINT16_MAX, &errstr);
333 if (errstr != NULL)
334 yyerror("proxy port is %s: %s", errstr,
335 p->port);
337 | VERIFYNAME bool {
338 host->proxy.noverifyname = !$2;
342 locations : /* empty */
343 | locations location optnl
346 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
347 /* drop the starting '/' if any */
348 if (*$3 == '/')
349 memmove($3, $3+1, strlen($3));
350 loc->match = $3;
352 | error '}'
355 locopts : /* empty */
356 | locopts locopt optnl
359 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
360 | BLOCK RETURN NUM string {
361 only_once(loc->block_fmt, "block");
362 loc->block_fmt = check_block_fmt($4);
363 loc->block_code = check_block_code($3);
365 | BLOCK RETURN NUM {
366 only_once(loc->block_fmt, "block");
367 loc->block_fmt = xstrdup("temporary failure");
368 loc->block_code = check_block_code($3);
369 if ($3 >= 30 && $3 < 40)
370 yyerror("missing `meta' for block return %d", $3);
372 | BLOCK {
373 only_once(loc->block_fmt, "block");
374 loc->block_fmt = xstrdup("temporary failure");
375 loc->block_code = 40;
377 | DEFAULT TYPE string {
378 only_once(loc->default_mime, "default type");
379 loc->default_mime = $3;
381 | FASTCGI fastcgi
382 | INDEX string {
383 only_once(loc->index, "index");
384 loc->index = $2;
386 | LANG string {
387 only_once(loc->lang, "lang");
388 loc->lang = $2;
390 | LOG bool { loc->disable_log = !$2; }
391 | REQUIRE CLIENT CA string {
392 only_once(loc->reqca, "require client ca");
393 ensure_absolute_path($4);
394 if ((loc->reqca = load_ca($4)) == NULL)
395 yyerror("couldn't load ca cert: %s", $4);
396 free($4);
398 | ROOT string {
399 only_once(loc->dir, "root");
400 loc->dir = ensure_absolute_path($2);
402 | STRIP NUM { loc->strip = check_strip_no($2); }
405 fastcgi : SPAWN string {
406 only_oncei(loc->fcgi, "fastcgi");
407 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
409 | string {
410 only_oncei(loc->fcgi, "fastcgi");
411 loc->fcgi = fastcgi_conf($1, NULL, NULL);
413 | TCP string PORT NUM {
414 char *c;
415 if (asprintf(&c, "%d", $4) == -1)
416 err(1, "asprintf");
417 only_oncei(loc->fcgi, "fastcgi");
418 loc->fcgi = fastcgi_conf($2, c, NULL);
420 | TCP string {
421 only_oncei(loc->fcgi, "fastcgi");
422 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
424 | TCP string PORT string {
425 only_oncei(loc->fcgi, "fastcgi");
426 loc->fcgi = fastcgi_conf($2, $4, NULL);
430 optnl : '\n' optnl /* zero or more newlines */
431 | ';' optnl /* semicolons too */
432 | /*empty*/
435 %%
437 static struct keyword {
438 const char *word;
439 int token;
440 } keywords[] = {
441 /* these MUST be sorted */
442 {"alias", ALIAS},
443 {"auto", AUTO},
444 {"block", BLOCK},
445 {"ca", CA},
446 {"cert", CERT},
447 {"cgi", CGI},
448 {"chroot", CHROOT},
449 {"client", CLIENT},
450 {"default", DEFAULT},
451 {"entrypoint", ENTRYPOINT},
452 {"env", ENV},
453 {"fastcgi", FASTCGI},
454 {"index", INDEX},
455 {"ipv6", IPV6},
456 {"key", KEY},
457 {"lang", LANG},
458 {"location", LOCATION},
459 {"log", LOG},
460 {"map", MAP},
461 {"mime", MIME},
462 {"ocsp", OCSP},
463 {"off", OFF},
464 {"on", ON},
465 {"param", PARAM},
466 {"port", PORT},
467 {"prefork", PREFORK},
468 {"protocols", PROTOCOLS},
469 {"proxy", PROXY},
470 {"relay-to", RELAY_TO},
471 {"require", REQUIRE},
472 {"return", RETURN},
473 {"root", ROOT},
474 {"server", SERVER},
475 {"spawn", SPAWN},
476 {"strip", STRIP},
477 {"tcp", TCP},
478 {"to-ext", TOEXT},
479 {"type", TYPE},
480 {"user", USER},
481 {"verifyname", VERIFYNAME},
482 };
484 void
485 yyerror(const char *msg, ...)
487 va_list ap;
489 file->errors++;
491 va_start(ap, msg);
492 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
493 vfprintf(stderr, msg, ap);
494 fprintf(stderr, "\n");
495 va_end(ap);
498 void
499 yywarn(const char *msg, ...)
501 va_list ap;
503 va_start(ap, msg);
504 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
505 vfprintf(stderr, msg, ap);
506 fprintf(stderr, "\n");
507 va_end(ap);
510 int
511 kw_cmp(const void *k, const void *e)
513 return strcmp(k, ((struct keyword *)e)->word);
516 int
517 lookup(char *s)
519 const struct keyword *p;
521 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
522 sizeof(keywords[0]), kw_cmp);
524 if (p)
525 return p->token;
526 else
527 return STRING;
530 #define START_EXPAND 1
531 #define DONE_EXPAND 2
533 static int expanding;
535 int
536 igetc(void)
538 int c;
540 while (1) {
541 if (file->ungetpos > 0)
542 c = file->ungetbuf[--file->ungetpos];
543 else
544 c = getc(file->stream);
546 if (c == START_EXPAND)
547 expanding = 1;
548 else if (c == DONE_EXPAND)
549 expanding = 0;
550 else
551 break;
553 return c;
556 int
557 lgetc(int quotec)
559 int c, next;
561 if (quotec) {
562 if ((c = igetc()) == EOF) {
563 yyerror("reached end of file while parsing "
564 "quoted string");
565 if (file == topfile || popfile() == EOF)
566 return EOF;
567 return quotec;
569 return c;
572 while ((c = igetc()) == '\\') {
573 next = igetc();
574 if (next != '\n') {
575 c = next;
576 break;
578 yylval.lineno = file->lineno;
579 file->lineno++;
582 if (c == EOF) {
583 /*
584 * Fake EOL when hit EOF for the first time. This gets line
585 * count right if last line in included file is syntactically
586 * invalid and has no newline.
587 */
588 if (file->eof_reached == 0) {
589 file->eof_reached = 1;
590 return '\n';
592 while (c == EOF) {
593 if (file == topfile || popfile() == EOF)
594 return EOF;
595 c = igetc();
598 return c;
601 void
602 lungetc(int c)
604 if (c == EOF)
605 return;
607 if (file->ungetpos >= file->ungetsize) {
608 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
609 if (p == NULL)
610 err(1, "lungetc");
611 file->ungetbuf = p;
612 file->ungetsize *= 2;
614 file->ungetbuf[file->ungetpos++] = c;
617 int
618 findeol(void)
620 int c;
622 /* Skip to either EOF or the first real EOL. */
623 while (1) {
624 c = lgetc(0);
625 if (c == '\n') {
626 file->lineno++;
627 break;
629 if (c == EOF)
630 break;
632 return ERROR;
635 int
636 yylex(void)
638 char buf[8096];
639 char *p, *val;
640 int quotec, next, c;
641 int token;
643 top:
644 p = buf;
645 while ((c = lgetc(0)) == ' ' || c == '\t')
646 ; /* nothing */
648 yylval.lineno = file->lineno;
649 if (c == '#')
650 while ((c = lgetc(0)) != '\n' && c != EOF)
651 ; /* nothing */
652 if (c == '$' && !expanding) {
653 while (1) {
654 if ((c = lgetc(0)) == EOF)
655 return 0;
656 if (p + 1 >= buf + sizeof(buf) -1) {
657 yyerror("string too long");
658 return findeol();
660 if (isalnum(c) || c == '_') {
661 *p++ = c;
662 continue;
664 *p = '\0';
665 lungetc(c);
666 break;
668 val = symget(buf);
669 if (val == NULL) {
670 yyerror("macro `%s' not defined", buf);
671 return findeol();
673 yylval.v.string = xstrdup(val);
674 return STRING;
676 if (c == '@' && !expanding) {
677 while (1) {
678 if ((c = lgetc(0)) == EOF)
679 return 0;
681 if (p + 1 >= buf + sizeof(buf) - 1) {
682 yyerror("string too long");
683 return findeol();
685 if (isalnum(c) || c == '_') {
686 *p++ = c;
687 continue;
689 *p = '\0';
690 lungetc(c);
691 break;
693 val = symget(buf);
694 if (val == NULL) {
695 yyerror("macro '%s' not defined", buf);
696 return findeol();
698 p = val + strlen(val) - 1;
699 lungetc(DONE_EXPAND);
700 while (p >= val) {
701 lungetc(*p);
702 p--;
704 lungetc(START_EXPAND);
705 goto top;
708 switch (c) {
709 case '\'':
710 case '"':
711 quotec = c;
712 while (1) {
713 if ((c = lgetc(quotec)) == EOF)
714 return 0;
715 if (c == '\n') {
716 file->lineno++;
717 continue;
718 } else if (c == '\\') {
719 if ((next = lgetc(quotec)) == EOF)
720 return (0);
721 if (next == quotec || next == ' ' ||
722 next == '\t')
723 c = next;
724 else if (next == '\n') {
725 file->lineno++;
726 continue;
727 } else
728 lungetc(next);
729 } else if (c == quotec) {
730 *p = '\0';
731 break;
732 } else if (c == '\0') {
733 yyerror("invalid syntax");
734 return findeol();
736 if (p + 1 >= buf + sizeof(buf) - 1) {
737 yyerror("string too long");
738 return findeol();
740 *p++ = c;
742 yylval.v.string = strdup(buf);
743 if (yylval.v.string == NULL)
744 err(1, "yylex: strdup");
745 return STRING;
748 #define allowed_to_end_number(x) \
749 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
751 if (c == '-' || isdigit(c)) {
752 do {
753 *p++ = c;
754 if ((size_t)(p-buf) >= sizeof(buf)) {
755 yyerror("string too long");
756 return findeol();
758 } while ((c = lgetc(0)) != EOF && isdigit(c));
759 lungetc(c);
760 if (p == buf + 1 && buf[0] == '-')
761 goto nodigits;
762 if (c == EOF || allowed_to_end_number(c)) {
763 const char *errstr = NULL;
765 *p = '\0';
766 yylval.v.number = strtonum(buf, LLONG_MIN,
767 LLONG_MAX, &errstr);
768 if (errstr) {
769 yyerror("\"%s\" invalid number: %s",
770 buf, errstr);
771 return findeol();
773 return NUM;
774 } else {
775 nodigits:
776 while (p > buf + 1)
777 lungetc(*--p);
778 c = *--p;
779 if (c == '-')
780 return c;
784 #define allowed_in_string(x) \
785 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
786 x != '{' && x != '}' && \
787 x != '!' && x != '=' && x != '#' && \
788 x != ',' && x != ';'))
790 if (isalnum(c) || c == ':' || c == '_') {
791 do {
792 *p++ = c;
793 if ((size_t)(p-buf) >= sizeof(buf)) {
794 yyerror("string too long");
795 return findeol();
797 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
798 lungetc(c);
799 *p = '\0';
800 if ((token = lookup(buf)) == STRING)
801 yylval.v.string = xstrdup(buf);
802 return token;
804 if (c == '\n') {
805 yylval.lineno = file->lineno;
806 file->lineno++;
808 if (c == EOF)
809 return 0;
810 return c;
813 struct file *
814 pushfile(const char *name, int secret)
816 struct file *nfile;
818 nfile = xcalloc(1, sizeof(*nfile));
819 nfile->name = xstrdup(name);
820 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
821 log_warn(NULL, "can't open %s: %s", nfile->name,
822 strerror(errno));
823 free(nfile->name);
824 free(nfile);
825 return NULL;
827 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
828 nfile->ungetsize = 16;
829 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
830 TAILQ_INSERT_TAIL(&files, nfile, entry);
831 return nfile;
834 int
835 popfile(void)
837 struct file *prev;
839 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
840 prev->errors += file->errors;
842 TAILQ_REMOVE(&files, file, entry);
843 fclose(file->stream);
844 free(file->name);
845 free(file->ungetbuf);
846 free(file);
847 file = prev;
848 return file ? 0 : EOF;
851 void
852 parse_conf(const char *filename)
854 struct sym *sym, *next;
856 file = pushfile(filename, 0);
857 if (file == NULL)
858 exit(1);
859 topfile = file;
861 yyparse();
862 errors = file->errors;
863 popfile();
865 /* Free macros and check which have not been used. */
866 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
867 /* TODO: warn if !sym->used */
868 if (!sym->persist) {
869 free(sym->name);
870 free(sym->val);
871 TAILQ_REMOVE(&symhead, sym, entry);
872 free(sym);
876 if (errors)
877 exit(1);
880 void
881 print_conf(void)
883 struct vhost *h;
884 /* struct location *l; */
885 /* struct envlist *e; */
886 /* struct alist *a; */
888 if (conf.chroot != NULL)
889 printf("chroot \"%s\"\n", conf.chroot);
890 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
891 /* XXX: defined mimes? */
892 printf("port %d\n", conf.port);
893 printf("prefork %d\n", conf.prefork);
894 /* XXX: protocols? */
895 if (conf.user != NULL)
896 printf("user \"%s\"\n", conf.user);
898 TAILQ_FOREACH(h, &hosts, vhosts) {
899 printf("\nserver \"%s\" {\n", h->domain);
900 printf(" cert \"%s\"\n", h->cert);
901 printf(" key \"%s\"\n", h->key);
902 /* TODO: print locations... */
903 printf("}\n");
907 int
908 symset(const char *name, const char *val, int persist)
910 struct sym *sym;
912 TAILQ_FOREACH(sym, &symhead, entry) {
913 if (!strcmp(name, sym->name))
914 break;
917 if (sym != NULL) {
918 if (sym->persist)
919 return 0;
920 else {
921 free(sym->name);
922 free(sym->val);
923 TAILQ_REMOVE(&symhead, sym, entry);
924 free(sym);
928 sym = xcalloc(1, sizeof(*sym));
929 sym->name = xstrdup(name);
930 sym->val = xstrdup(val);
931 sym->used = 0;
932 sym->persist = persist;
934 TAILQ_INSERT_TAIL(&symhead, sym, entry);
935 return 0;
938 int
939 cmdline_symset(char *s)
941 char *sym, *val;
942 int ret;
944 if ((val = strrchr(s, '=')) == NULL)
945 return -1;
946 sym = xcalloc(1, val - s + 1);
947 memcpy(sym, s, val - s);
948 ret = symset(sym, val + 1, 1);
949 free(sym);
950 return ret;
953 char *
954 symget(const char *nam)
956 struct sym *sym;
958 TAILQ_FOREACH(sym, &symhead, entry) {
959 if (strcmp(nam, sym->name) == 0) {
960 sym->used = 1;
961 return sym->val;
964 return NULL;
967 struct vhost *
968 new_vhost(void)
970 struct vhost *v;
972 v = xcalloc(1, sizeof(*v));
973 v->proxy.protocols = TLS_PROTOCOLS_DEFAULT;
974 return v;
977 struct location *
978 new_location(void)
980 struct location *l;
982 l = xcalloc(1, sizeof(*l));
983 l->dirfd = -1;
984 l->fcgi = -1;
985 return l;
988 char *
989 ensure_absolute_path(char *path)
991 if (path == NULL || *path != '/')
992 yyerror("not an absolute path: %s", path);
993 return path;
996 int
997 check_block_code(int n)
999 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1000 yyerror("invalid block code %d", n);
1001 return n;
1004 char *
1005 check_block_fmt(char *fmt)
1007 char *s;
1009 for (s = fmt; *s; ++s) {
1010 if (*s != '%')
1011 continue;
1012 switch (*++s) {
1013 case '%':
1014 case 'p':
1015 case 'q':
1016 case 'P':
1017 case 'N':
1018 break;
1019 default:
1020 yyerror("invalid format specifier %%%c", *s);
1024 return fmt;
1027 int
1028 check_strip_no(int n)
1030 if (n <= 0)
1031 yyerror("invalid strip number %d", n);
1032 return n;
1035 int
1036 check_port_num(int n)
1038 if (n <= 0 || n >= UINT16_MAX)
1039 yyerror("port number is %s: %d",
1040 n <= 0 ? "too small" : "too large",
1041 n);
1042 return n;
1045 int
1046 check_prefork_num(int n)
1048 if (n <= 0 || n >= PROC_MAX)
1049 yyerror("invalid prefork number %d", n);
1050 return n;
1053 void
1054 advance_loc(void)
1056 loc = new_location();
1057 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1060 void
1061 only_once(const void *ptr, const char *name)
1063 if (ptr != NULL)
1064 yyerror("`%s' specified more than once", name);
1067 void
1068 only_oncei(int i, const char *name)
1070 if (i != -1)
1071 yyerror("`%s' specified more than once", name);
1074 int
1075 fastcgi_conf(char *path, char *port, char *prog)
1077 struct fcgi *f;
1078 int i;
1080 for (i = 0; i < FCGI_MAX; ++i) {
1081 f = &fcgi[i];
1083 if (f->path == NULL) {
1084 f->id = i;
1085 f->path = path;
1086 f->port = port;
1087 f->prog = prog;
1088 return i;
1091 /* XXX: what to do with prog? */
1092 if (!strcmp(f->path, path) &&
1093 ((port == NULL && f->port == NULL) ||
1094 !strcmp(f->port, port))) {
1095 free(path);
1096 free(port);
1097 return i;
1101 yyerror("too much `fastcgi' rules defined.");
1102 return -1;
1105 void
1106 add_param(char *name, char *val, int env)
1108 struct envlist *e;
1109 struct envhead *h;
1111 if (env)
1112 h = &host->env;
1113 else
1114 h = &host->params;
1116 e = xcalloc(1, sizeof(*e));
1117 e->name = name;
1118 e->value = val;
1119 if (TAILQ_EMPTY(h))
1120 TAILQ_INSERT_HEAD(h, e, envs);
1121 else
1122 TAILQ_INSERT_TAIL(h, e, envs);