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 | RELAY_TO string {
313 char *at;
314 const char *errstr;
315 struct proxy *p = &host->proxy;
317 only_once(p->host, "proxy relay-to");
318 p->host = $2;
320 if ((at = strchr($2, ':')) != NULL) {
321 *at++ = '\0';
322 p->port = at;
323 } else
324 p->port = "1965";
326 strtonum(p->port, 1, UINT16_MAX, &errstr);
327 if (errstr != NULL)
328 yyerror("proxy port is %s: %s", errstr,
329 p->port);
331 | VERIFYNAME bool {
332 host->proxy.noverifyname = !$2;
336 locations : /* empty */
337 | locations location optnl
340 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
341 /* drop the starting '/' if any */
342 if (*$3 == '/')
343 memmove($3, $3+1, strlen($3));
344 loc->match = $3;
346 | error '}'
349 locopts : /* empty */
350 | locopts locopt optnl
353 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
354 | BLOCK RETURN NUM string {
355 only_once(loc->block_fmt, "block");
356 loc->block_fmt = check_block_fmt($4);
357 loc->block_code = check_block_code($3);
359 | BLOCK RETURN NUM {
360 only_once(loc->block_fmt, "block");
361 loc->block_fmt = xstrdup("temporary failure");
362 loc->block_code = check_block_code($3);
363 if ($3 >= 30 && $3 < 40)
364 yyerror("missing `meta' for block return %d", $3);
366 | BLOCK {
367 only_once(loc->block_fmt, "block");
368 loc->block_fmt = xstrdup("temporary failure");
369 loc->block_code = 40;
371 | DEFAULT TYPE string {
372 only_once(loc->default_mime, "default type");
373 loc->default_mime = $3;
375 | FASTCGI fastcgi
376 | INDEX string {
377 only_once(loc->index, "index");
378 loc->index = $2;
380 | LANG string {
381 only_once(loc->lang, "lang");
382 loc->lang = $2;
384 | LOG bool { loc->disable_log = !$2; }
385 | REQUIRE CLIENT CA string {
386 only_once(loc->reqca, "require client ca");
387 ensure_absolute_path($4);
388 if ((loc->reqca = load_ca($4)) == NULL)
389 yyerror("couldn't load ca cert: %s", $4);
390 free($4);
392 | ROOT string {
393 only_once(loc->dir, "root");
394 loc->dir = ensure_absolute_path($2);
396 | STRIP NUM { loc->strip = check_strip_no($2); }
399 fastcgi : SPAWN string {
400 only_oncei(loc->fcgi, "fastcgi");
401 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
403 | string {
404 only_oncei(loc->fcgi, "fastcgi");
405 loc->fcgi = fastcgi_conf($1, NULL, NULL);
407 | TCP string PORT NUM {
408 char *c;
409 if (asprintf(&c, "%d", $4) == -1)
410 err(1, "asprintf");
411 only_oncei(loc->fcgi, "fastcgi");
412 loc->fcgi = fastcgi_conf($2, c, NULL);
414 | TCP string {
415 only_oncei(loc->fcgi, "fastcgi");
416 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
418 | TCP string PORT string {
419 only_oncei(loc->fcgi, "fastcgi");
420 loc->fcgi = fastcgi_conf($2, $4, NULL);
424 optnl : '\n' optnl /* zero or more newlines */
425 | ';' optnl /* semicolons too */
426 | /*empty*/
429 %%
431 static struct keyword {
432 const char *word;
433 int token;
434 } keywords[] = {
435 /* these MUST be sorted */
436 {"alias", ALIAS},
437 {"auto", AUTO},
438 {"block", BLOCK},
439 {"ca", CA},
440 {"cert", CERT},
441 {"cgi", CGI},
442 {"chroot", CHROOT},
443 {"client", CLIENT},
444 {"default", DEFAULT},
445 {"entrypoint", ENTRYPOINT},
446 {"env", ENV},
447 {"fastcgi", FASTCGI},
448 {"index", INDEX},
449 {"ipv6", IPV6},
450 {"key", KEY},
451 {"lang", LANG},
452 {"location", LOCATION},
453 {"log", LOG},
454 {"map", MAP},
455 {"mime", MIME},
456 {"ocsp", OCSP},
457 {"off", OFF},
458 {"on", ON},
459 {"param", PARAM},
460 {"port", PORT},
461 {"prefork", PREFORK},
462 {"protocols", PROTOCOLS},
463 {"proxy", PROXY},
464 {"relay-to", RELAY_TO},
465 {"require", REQUIRE},
466 {"return", RETURN},
467 {"root", ROOT},
468 {"server", SERVER},
469 {"spawn", SPAWN},
470 {"strip", STRIP},
471 {"tcp", TCP},
472 {"to-ext", TOEXT},
473 {"type", TYPE},
474 {"user", USER},
475 {"verifyname", VERIFYNAME},
476 };
478 void
479 yyerror(const char *msg, ...)
481 va_list ap;
483 file->errors++;
485 va_start(ap, msg);
486 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
487 vfprintf(stderr, msg, ap);
488 fprintf(stderr, "\n");
489 va_end(ap);
492 void
493 yywarn(const char *msg, ...)
495 va_list ap;
497 va_start(ap, msg);
498 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
499 vfprintf(stderr, msg, ap);
500 fprintf(stderr, "\n");
501 va_end(ap);
504 int
505 kw_cmp(const void *k, const void *e)
507 return strcmp(k, ((struct keyword *)e)->word);
510 int
511 lookup(char *s)
513 const struct keyword *p;
515 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
516 sizeof(keywords[0]), kw_cmp);
518 if (p)
519 return p->token;
520 else
521 return STRING;
524 #define START_EXPAND 1
525 #define DONE_EXPAND 2
527 static int expanding;
529 int
530 igetc(void)
532 int c;
534 while (1) {
535 if (file->ungetpos > 0)
536 c = file->ungetbuf[--file->ungetpos];
537 else
538 c = getc(file->stream);
540 if (c == START_EXPAND)
541 expanding = 1;
542 else if (c == DONE_EXPAND)
543 expanding = 0;
544 else
545 break;
547 return c;
550 int
551 lgetc(int quotec)
553 int c, next;
555 if (quotec) {
556 if ((c = igetc()) == EOF) {
557 yyerror("reached end of file while parsing "
558 "quoted string");
559 if (file == topfile || popfile() == EOF)
560 return EOF;
561 return quotec;
563 return c;
566 while ((c = igetc()) == '\\') {
567 next = igetc();
568 if (next != '\n') {
569 c = next;
570 break;
572 yylval.lineno = file->lineno;
573 file->lineno++;
576 if (c == EOF) {
577 /*
578 * Fake EOL when hit EOF for the first time. This gets line
579 * count right if last line in included file is syntactically
580 * invalid and has no newline.
581 */
582 if (file->eof_reached == 0) {
583 file->eof_reached = 1;
584 return '\n';
586 while (c == EOF) {
587 if (file == topfile || popfile() == EOF)
588 return EOF;
589 c = igetc();
592 return c;
595 void
596 lungetc(int c)
598 if (c == EOF)
599 return;
601 if (file->ungetpos >= file->ungetsize) {
602 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
603 if (p == NULL)
604 err(1, "lungetc");
605 file->ungetbuf = p;
606 file->ungetsize *= 2;
608 file->ungetbuf[file->ungetpos++] = c;
611 int
612 findeol(void)
614 int c;
616 /* Skip to either EOF or the first real EOL. */
617 while (1) {
618 c = lgetc(0);
619 if (c == '\n') {
620 file->lineno++;
621 break;
623 if (c == EOF)
624 break;
626 return ERROR;
629 int
630 yylex(void)
632 char buf[8096];
633 char *p, *val;
634 int quotec, next, c;
635 int token;
637 top:
638 p = buf;
639 while ((c = lgetc(0)) == ' ' || c == '\t')
640 ; /* nothing */
642 yylval.lineno = file->lineno;
643 if (c == '#')
644 while ((c = lgetc(0)) != '\n' && c != EOF)
645 ; /* nothing */
646 if (c == '$' && !expanding) {
647 while (1) {
648 if ((c = lgetc(0)) == EOF)
649 return 0;
650 if (p + 1 >= buf + sizeof(buf) -1) {
651 yyerror("string too long");
652 return findeol();
654 if (isalnum(c) || c == '_') {
655 *p++ = c;
656 continue;
658 *p = '\0';
659 lungetc(c);
660 break;
662 val = symget(buf);
663 if (val == NULL) {
664 yyerror("macro `%s' not defined", buf);
665 return findeol();
667 yylval.v.string = xstrdup(val);
668 return STRING;
670 if (c == '@' && !expanding) {
671 while (1) {
672 if ((c = lgetc(0)) == EOF)
673 return 0;
675 if (p + 1 >= buf + sizeof(buf) - 1) {
676 yyerror("string too long");
677 return findeol();
679 if (isalnum(c) || c == '_') {
680 *p++ = c;
681 continue;
683 *p = '\0';
684 lungetc(c);
685 break;
687 val = symget(buf);
688 if (val == NULL) {
689 yyerror("macro '%s' not defined", buf);
690 return findeol();
692 p = val + strlen(val) - 1;
693 lungetc(DONE_EXPAND);
694 while (p >= val) {
695 lungetc(*p);
696 p--;
698 lungetc(START_EXPAND);
699 goto top;
702 switch (c) {
703 case '\'':
704 case '"':
705 quotec = c;
706 while (1) {
707 if ((c = lgetc(quotec)) == EOF)
708 return 0;
709 if (c == '\n') {
710 file->lineno++;
711 continue;
712 } else if (c == '\\') {
713 if ((next = lgetc(quotec)) == EOF)
714 return (0);
715 if (next == quotec || next == ' ' ||
716 next == '\t')
717 c = next;
718 else if (next == '\n') {
719 file->lineno++;
720 continue;
721 } else
722 lungetc(next);
723 } else if (c == quotec) {
724 *p = '\0';
725 break;
726 } else if (c == '\0') {
727 yyerror("invalid syntax");
728 return findeol();
730 if (p + 1 >= buf + sizeof(buf) - 1) {
731 yyerror("string too long");
732 return findeol();
734 *p++ = c;
736 yylval.v.string = strdup(buf);
737 if (yylval.v.string == NULL)
738 err(1, "yylex: strdup");
739 return STRING;
742 #define allowed_to_end_number(x) \
743 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
745 if (c == '-' || isdigit(c)) {
746 do {
747 *p++ = c;
748 if ((size_t)(p-buf) >= sizeof(buf)) {
749 yyerror("string too long");
750 return findeol();
752 } while ((c = lgetc(0)) != EOF && isdigit(c));
753 lungetc(c);
754 if (p == buf + 1 && buf[0] == '-')
755 goto nodigits;
756 if (c == EOF || allowed_to_end_number(c)) {
757 const char *errstr = NULL;
759 *p = '\0';
760 yylval.v.number = strtonum(buf, LLONG_MIN,
761 LLONG_MAX, &errstr);
762 if (errstr) {
763 yyerror("\"%s\" invalid number: %s",
764 buf, errstr);
765 return findeol();
767 return NUM;
768 } else {
769 nodigits:
770 while (p > buf + 1)
771 lungetc(*--p);
772 c = *--p;
773 if (c == '-')
774 return c;
778 #define allowed_in_string(x) \
779 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
780 x != '{' && x != '}' && \
781 x != '!' && x != '=' && x != '#' && \
782 x != ',' && x != ';'))
784 if (isalnum(c) || c == ':' || c == '_') {
785 do {
786 *p++ = c;
787 if ((size_t)(p-buf) >= sizeof(buf)) {
788 yyerror("string too long");
789 return findeol();
791 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
792 lungetc(c);
793 *p = '\0';
794 if ((token = lookup(buf)) == STRING)
795 yylval.v.string = xstrdup(buf);
796 return token;
798 if (c == '\n') {
799 yylval.lineno = file->lineno;
800 file->lineno++;
802 if (c == EOF)
803 return 0;
804 return c;
807 struct file *
808 pushfile(const char *name, int secret)
810 struct file *nfile;
812 nfile = xcalloc(1, sizeof(*nfile));
813 nfile->name = xstrdup(name);
814 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
815 log_warn(NULL, "can't open %s: %s", nfile->name,
816 strerror(errno));
817 free(nfile->name);
818 free(nfile);
819 return NULL;
821 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
822 nfile->ungetsize = 16;
823 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
824 TAILQ_INSERT_TAIL(&files, nfile, entry);
825 return nfile;
828 int
829 popfile(void)
831 struct file *prev;
833 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
834 prev->errors += file->errors;
836 TAILQ_REMOVE(&files, file, entry);
837 fclose(file->stream);
838 free(file->name);
839 free(file->ungetbuf);
840 free(file);
841 file = prev;
842 return file ? 0 : EOF;
845 void
846 parse_conf(const char *filename)
848 struct sym *sym, *next;
850 file = pushfile(filename, 0);
851 if (file == NULL)
852 exit(1);
853 topfile = file;
855 yyparse();
856 errors = file->errors;
857 popfile();
859 /* Free macros and check which have not been used. */
860 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
861 /* TODO: warn if !sym->used */
862 if (!sym->persist) {
863 free(sym->name);
864 free(sym->val);
865 TAILQ_REMOVE(&symhead, sym, entry);
866 free(sym);
870 if (errors)
871 exit(1);
874 void
875 print_conf(void)
877 struct vhost *h;
878 /* struct location *l; */
879 /* struct envlist *e; */
880 /* struct alist *a; */
882 if (conf.chroot != NULL)
883 printf("chroot \"%s\"\n", conf.chroot);
884 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
885 /* XXX: defined mimes? */
886 printf("port %d\n", conf.port);
887 printf("prefork %d\n", conf.prefork);
888 /* XXX: protocols? */
889 if (conf.user != NULL)
890 printf("user \"%s\"\n", conf.user);
892 TAILQ_FOREACH(h, &hosts, vhosts) {
893 printf("\nserver \"%s\" {\n", h->domain);
894 printf(" cert \"%s\"\n", h->cert);
895 printf(" key \"%s\"\n", h->key);
896 /* TODO: print locations... */
897 printf("}\n");
901 int
902 symset(const char *name, const char *val, int persist)
904 struct sym *sym;
906 TAILQ_FOREACH(sym, &symhead, entry) {
907 if (!strcmp(name, sym->name))
908 break;
911 if (sym != NULL) {
912 if (sym->persist)
913 return 0;
914 else {
915 free(sym->name);
916 free(sym->val);
917 TAILQ_REMOVE(&symhead, sym, entry);
918 free(sym);
922 sym = xcalloc(1, sizeof(*sym));
923 sym->name = xstrdup(name);
924 sym->val = xstrdup(val);
925 sym->used = 0;
926 sym->persist = persist;
928 TAILQ_INSERT_TAIL(&symhead, sym, entry);
929 return 0;
932 int
933 cmdline_symset(char *s)
935 char *sym, *val;
936 int ret;
938 if ((val = strrchr(s, '=')) == NULL)
939 return -1;
940 sym = xcalloc(1, val - s + 1);
941 memcpy(sym, s, val - s);
942 ret = symset(sym, val + 1, 1);
943 free(sym);
944 return ret;
947 char *
948 symget(const char *nam)
950 struct sym *sym;
952 TAILQ_FOREACH(sym, &symhead, entry) {
953 if (strcmp(nam, sym->name) == 0) {
954 sym->used = 1;
955 return sym->val;
958 return NULL;
961 struct vhost *
962 new_vhost(void)
964 return xcalloc(1, sizeof(struct vhost));
967 struct location *
968 new_location(void)
970 struct location *l;
972 l = xcalloc(1, sizeof(*l));
973 l->dirfd = -1;
974 l->fcgi = -1;
975 return l;
978 char *
979 ensure_absolute_path(char *path)
981 if (path == NULL || *path != '/')
982 yyerror("not an absolute path: %s", path);
983 return path;
986 int
987 check_block_code(int n)
989 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
990 yyerror("invalid block code %d", n);
991 return n;
994 char *
995 check_block_fmt(char *fmt)
997 char *s;
999 for (s = fmt; *s; ++s) {
1000 if (*s != '%')
1001 continue;
1002 switch (*++s) {
1003 case '%':
1004 case 'p':
1005 case 'q':
1006 case 'P':
1007 case 'N':
1008 break;
1009 default:
1010 yyerror("invalid format specifier %%%c", *s);
1014 return fmt;
1017 int
1018 check_strip_no(int n)
1020 if (n <= 0)
1021 yyerror("invalid strip number %d", n);
1022 return n;
1025 int
1026 check_port_num(int n)
1028 if (n <= 0 || n >= UINT16_MAX)
1029 yyerror("port number is %s: %d",
1030 n <= 0 ? "too small" : "too large",
1031 n);
1032 return n;
1035 int
1036 check_prefork_num(int n)
1038 if (n <= 0 || n >= PROC_MAX)
1039 yyerror("invalid prefork number %d", n);
1040 return n;
1043 void
1044 advance_loc(void)
1046 loc = new_location();
1047 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1050 void
1051 only_once(const void *ptr, const char *name)
1053 if (ptr != NULL)
1054 yyerror("`%s' specified more than once", name);
1057 void
1058 only_oncei(int i, const char *name)
1060 if (i != -1)
1061 yyerror("`%s' specified more than once", name);
1064 int
1065 fastcgi_conf(char *path, char *port, char *prog)
1067 struct fcgi *f;
1068 int i;
1070 for (i = 0; i < FCGI_MAX; ++i) {
1071 f = &fcgi[i];
1073 if (f->path == NULL) {
1074 f->id = i;
1075 f->path = path;
1076 f->port = port;
1077 f->prog = prog;
1078 return i;
1081 /* XXX: what to do with prog? */
1082 if (!strcmp(f->path, path) &&
1083 ((port == NULL && f->port == NULL) ||
1084 !strcmp(f->port, port))) {
1085 free(path);
1086 free(port);
1087 return i;
1091 yyerror("too much `fastcgi' rules defined.");
1092 return -1;
1095 void
1096 add_param(char *name, char *val, int env)
1098 struct envlist *e;
1099 struct envhead *h;
1101 if (env)
1102 h = &host->env;
1103 else
1104 h = &host->params;
1106 e = xcalloc(1, sizeof(*e));
1107 e->name = name;
1108 e->value = val;
1109 if (TAILQ_EMPTY(h))
1110 TAILQ_INSERT_HEAD(h, e, envs);
1111 else
1112 TAILQ_INSERT_TAIL(h, e, envs);