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
129 %token ERROR
131 %token <v.string> STRING
132 %token <v.number> NUM
134 %type <v.number> bool
135 %type <v.string> string
137 %%
139 conf : /* empty */
140 | conf include '\n'
141 | conf '\n'
142 | conf varset '\n'
143 | conf option '\n'
144 | conf vhost '\n'
145 | conf error '\n' { file->errors++; }
148 include : INCLUDE STRING {
149 struct file *nfile;
151 if ((nfile = pushfile($2, 0)) == NULL) {
152 yyerror("failed to include file %s", $2);
153 free($2);
154 YYERROR;
156 free($2);
158 file = nfile;
159 lungetc('\n');
163 bool : ON { $$ = 1; }
164 | OFF { $$ = 0; }
167 string : string STRING {
168 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
169 free($1);
170 free($2);
171 yyerror("string: asprintf: %s", strerror(errno));
172 YYERROR;
174 free($1);
175 free($2);
177 | STRING
180 varset : STRING '=' string {
181 char *s = $1;
182 while (*s++) {
183 if (isspace((unsigned char)*s)) {
184 yyerror("macro name cannot contain "
185 "whitespaces");
186 free($1);
187 free($3);
188 YYERROR;
191 symset($1, $3, 0);
192 free($1);
193 free($3);
197 option : CHROOT string { conf.chroot = $2; }
198 | IPV6 bool { conf.ipv6 = $2; }
199 | MIME STRING string {
200 yywarn("`mime MIME EXT' is deprecated and will be "
201 "removed in a future version, please use the new "
202 "syntax: `map MIME to-ext EXT'");
203 add_mime(&conf.mime, $2, $3);
205 | MAP string TOEXT string { add_mime(&conf.mime, $2, $4); }
206 | PORT NUM { conf.port = check_port_num($2); }
207 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
208 | PROTOCOLS string {
209 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
210 yyerror("invalid protocols string \"%s\"", $2);
212 | USER string { conf.user = $2; }
215 vhost : SERVER string {
216 host = new_vhost();
217 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
219 loc = new_location();
220 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
222 loc->match = xstrdup("*");
223 host->domain = $2;
225 if (strstr($2, "xn--") != NULL) {
226 yywarn("\"%s\" looks like punycode: you "
227 "should use the decoded hostname", $2);
229 } '{' optnl servopts locations '}' {
230 if (host->cert == NULL || host->key == NULL)
231 yyerror("invalid vhost definition: %s", $2);
233 | error '}' { yyerror("bad server directive"); }
236 servopts : /* empty */
237 | servopts servopt optnl
240 servopt : ALIAS string {
241 struct alist *a;
243 a = xcalloc(1, sizeof(*a));
244 a->alias = $2;
245 if (TAILQ_EMPTY(&host->aliases))
246 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
247 else
248 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
250 | CERT string {
251 only_once(host->cert, "cert");
252 host->cert = ensure_absolute_path($2);
254 | CGI string {
255 only_once(host->cgi, "cgi");
256 /* drop the starting '/', if any */
257 if (*$2 == '/')
258 memmove($2, $2+1, strlen($2));
259 host->cgi = $2;
261 | ENTRYPOINT string {
262 only_once(host->entrypoint, "entrypoint");
263 while (*$2 == '/')
264 memmove($2, $2+1, strlen($2));
265 host->entrypoint = $2;
267 | ENV string '=' string {
268 add_param($2, $4, 1);
270 | KEY string {
271 only_once(host->key, "key");
272 host->key = ensure_absolute_path($2);
274 | OCSP string {
275 only_once(host->ocsp, "ocsp");
276 host->ocsp = ensure_absolute_path($2);
278 | PARAM string '=' string {
279 add_param($2, $4, 0);
281 | proxy
282 | locopt
285 proxy : PROXY proxy_opt
286 | PROXY '{' optnl proxy_opts '}'
289 proxy_opts : /* empty */
290 | proxy_opts proxy_opt optnl
293 proxy_opt : CERT string {
294 struct proxy *p = &host->proxy;
296 only_once(p->cert, "proxy cert");
297 ensure_absolute_path($2);
298 p->cert = tls_load_file($2, &p->certlen, NULL);
299 if (p->cert == NULL)
300 yyerror("can't load cert %s", $2);
302 | KEY string {
303 struct proxy *p = &host->proxy;
305 only_once(p->key, "proxy key");
306 ensure_absolute_path($2);
307 p->key = tls_load_file($2, &p->keylen, NULL);
308 if (p->key == NULL)
309 yyerror("can't load key %s", $2);
311 | RELAY_TO string {
312 char *at;
313 const char *errstr;
314 struct proxy *p = &host->proxy;
316 only_once(p->host, "proxy relay-to");
317 p->host = $2;
319 if ((at = strchr($2, ':')) != NULL) {
320 *at++ = '\0';
321 p->port = at;
322 } else
323 p->port = "1965";
325 strtonum(p->port, 1, UINT16_MAX, &errstr);
326 if (errstr != NULL)
327 yyerror("proxy port is %s: %s", errstr,
328 p->port);
332 locations : /* empty */
333 | locations location optnl
336 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
337 /* drop the starting '/' if any */
338 if (*$3 == '/')
339 memmove($3, $3+1, strlen($3));
340 loc->match = $3;
342 | error '}'
345 locopts : /* empty */
346 | locopts locopt optnl
349 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
350 | BLOCK RETURN NUM string {
351 only_once(loc->block_fmt, "block");
352 loc->block_fmt = check_block_fmt($4);
353 loc->block_code = check_block_code($3);
355 | BLOCK RETURN NUM {
356 only_once(loc->block_fmt, "block");
357 loc->block_fmt = xstrdup("temporary failure");
358 loc->block_code = check_block_code($3);
359 if ($3 >= 30 && $3 < 40)
360 yyerror("missing `meta' for block return %d", $3);
362 | BLOCK {
363 only_once(loc->block_fmt, "block");
364 loc->block_fmt = xstrdup("temporary failure");
365 loc->block_code = 40;
367 | DEFAULT TYPE string {
368 only_once(loc->default_mime, "default type");
369 loc->default_mime = $3;
371 | FASTCGI fastcgi
372 | INDEX string {
373 only_once(loc->index, "index");
374 loc->index = $2;
376 | LANG string {
377 only_once(loc->lang, "lang");
378 loc->lang = $2;
380 | LOG bool { loc->disable_log = !$2; }
381 | REQUIRE CLIENT CA string {
382 only_once(loc->reqca, "require client ca");
383 ensure_absolute_path($4);
384 if ((loc->reqca = load_ca($4)) == NULL)
385 yyerror("couldn't load ca cert: %s", $4);
386 free($4);
388 | ROOT string {
389 only_once(loc->dir, "root");
390 loc->dir = ensure_absolute_path($2);
392 | STRIP NUM { loc->strip = check_strip_no($2); }
395 fastcgi : SPAWN string {
396 only_oncei(loc->fcgi, "fastcgi");
397 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
399 | string {
400 only_oncei(loc->fcgi, "fastcgi");
401 loc->fcgi = fastcgi_conf($1, NULL, NULL);
403 | TCP string PORT NUM {
404 char *c;
405 if (asprintf(&c, "%d", $4) == -1)
406 err(1, "asprintf");
407 only_oncei(loc->fcgi, "fastcgi");
408 loc->fcgi = fastcgi_conf($2, c, NULL);
410 | TCP string {
411 only_oncei(loc->fcgi, "fastcgi");
412 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
414 | TCP string PORT string {
415 only_oncei(loc->fcgi, "fastcgi");
416 loc->fcgi = fastcgi_conf($2, $4, NULL);
420 optnl : '\n' optnl /* zero or more newlines */
421 | ';' optnl /* semicolons too */
422 | /*empty*/
425 %%
427 static struct keyword {
428 const char *word;
429 int token;
430 } keywords[] = {
431 /* these MUST be sorted */
432 {"alias", ALIAS},
433 {"auto", AUTO},
434 {"block", BLOCK},
435 {"ca", CA},
436 {"cert", CERT},
437 {"cgi", CGI},
438 {"chroot", CHROOT},
439 {"client", CLIENT},
440 {"default", DEFAULT},
441 {"entrypoint", ENTRYPOINT},
442 {"env", ENV},
443 {"fastcgi", FASTCGI},
444 {"index", INDEX},
445 {"ipv6", IPV6},
446 {"key", KEY},
447 {"lang", LANG},
448 {"location", LOCATION},
449 {"log", LOG},
450 {"map", MAP},
451 {"mime", MIME},
452 {"ocsp", OCSP},
453 {"off", OFF},
454 {"on", ON},
455 {"param", PARAM},
456 {"port", PORT},
457 {"prefork", PREFORK},
458 {"protocols", PROTOCOLS},
459 {"proxy", PROXY},
460 {"relay-to", RELAY_TO},
461 {"require", REQUIRE},
462 {"return", RETURN},
463 {"root", ROOT},
464 {"server", SERVER},
465 {"spawn", SPAWN},
466 {"strip", STRIP},
467 {"tcp", TCP},
468 {"to-ext", TOEXT},
469 {"type", TYPE},
470 {"user", USER},
471 };
473 void
474 yyerror(const char *msg, ...)
476 va_list ap;
478 file->errors++;
480 va_start(ap, msg);
481 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
482 vfprintf(stderr, msg, ap);
483 fprintf(stderr, "\n");
484 va_end(ap);
487 void
488 yywarn(const char *msg, ...)
490 va_list ap;
492 va_start(ap, msg);
493 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
494 vfprintf(stderr, msg, ap);
495 fprintf(stderr, "\n");
496 va_end(ap);
499 int
500 kw_cmp(const void *k, const void *e)
502 return strcmp(k, ((struct keyword *)e)->word);
505 int
506 lookup(char *s)
508 const struct keyword *p;
510 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
511 sizeof(keywords[0]), kw_cmp);
513 if (p)
514 return p->token;
515 else
516 return STRING;
519 #define START_EXPAND 1
520 #define DONE_EXPAND 2
522 static int expanding;
524 int
525 igetc(void)
527 int c;
529 while (1) {
530 if (file->ungetpos > 0)
531 c = file->ungetbuf[--file->ungetpos];
532 else
533 c = getc(file->stream);
535 if (c == START_EXPAND)
536 expanding = 1;
537 else if (c == DONE_EXPAND)
538 expanding = 0;
539 else
540 break;
542 return c;
545 int
546 lgetc(int quotec)
548 int c, next;
550 if (quotec) {
551 if ((c = igetc()) == EOF) {
552 yyerror("reached end of file while parsing "
553 "quoted string");
554 if (file == topfile || popfile() == EOF)
555 return EOF;
556 return quotec;
558 return c;
561 while ((c = igetc()) == '\\') {
562 next = igetc();
563 if (next != '\n') {
564 c = next;
565 break;
567 yylval.lineno = file->lineno;
568 file->lineno++;
571 if (c == EOF) {
572 /*
573 * Fake EOL when hit EOF for the first time. This gets line
574 * count right if last line in included file is syntactically
575 * invalid and has no newline.
576 */
577 if (file->eof_reached == 0) {
578 file->eof_reached = 1;
579 return '\n';
581 while (c == EOF) {
582 if (file == topfile || popfile() == EOF)
583 return EOF;
584 c = igetc();
587 return c;
590 void
591 lungetc(int c)
593 if (c == EOF)
594 return;
596 if (file->ungetpos >= file->ungetsize) {
597 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
598 if (p == NULL)
599 err(1, "lungetc");
600 file->ungetbuf = p;
601 file->ungetsize *= 2;
603 file->ungetbuf[file->ungetpos++] = c;
606 int
607 findeol(void)
609 int c;
611 /* Skip to either EOF or the first real EOL. */
612 while (1) {
613 c = lgetc(0);
614 if (c == '\n') {
615 file->lineno++;
616 break;
618 if (c == EOF)
619 break;
621 return ERROR;
624 int
625 yylex(void)
627 char buf[8096];
628 char *p, *val;
629 int quotec, next, c;
630 int token;
632 top:
633 p = buf;
634 while ((c = lgetc(0)) == ' ' || c == '\t')
635 ; /* nothing */
637 yylval.lineno = file->lineno;
638 if (c == '#')
639 while ((c = lgetc(0)) != '\n' && c != EOF)
640 ; /* nothing */
641 if (c == '$' && !expanding) {
642 while (1) {
643 if ((c = lgetc(0)) == EOF)
644 return 0;
645 if (p + 1 >= buf + sizeof(buf) -1) {
646 yyerror("string too long");
647 return findeol();
649 if (isalnum(c) || c == '_') {
650 *p++ = c;
651 continue;
653 *p = '\0';
654 lungetc(c);
655 break;
657 val = symget(buf);
658 if (val == NULL) {
659 yyerror("macro `%s' not defined", buf);
660 return findeol();
662 yylval.v.string = xstrdup(val);
663 return STRING;
665 if (c == '@' && !expanding) {
666 while (1) {
667 if ((c = lgetc(0)) == EOF)
668 return 0;
670 if (p + 1 >= buf + sizeof(buf) - 1) {
671 yyerror("string too long");
672 return findeol();
674 if (isalnum(c) || c == '_') {
675 *p++ = c;
676 continue;
678 *p = '\0';
679 lungetc(c);
680 break;
682 val = symget(buf);
683 if (val == NULL) {
684 yyerror("macro '%s' not defined", buf);
685 return findeol();
687 p = val + strlen(val) - 1;
688 lungetc(DONE_EXPAND);
689 while (p >= val) {
690 lungetc(*p);
691 p--;
693 lungetc(START_EXPAND);
694 goto top;
697 switch (c) {
698 case '\'':
699 case '"':
700 quotec = c;
701 while (1) {
702 if ((c = lgetc(quotec)) == EOF)
703 return 0;
704 if (c == '\n') {
705 file->lineno++;
706 continue;
707 } else if (c == '\\') {
708 if ((next = lgetc(quotec)) == EOF)
709 return (0);
710 if (next == quotec || next == ' ' ||
711 next == '\t')
712 c = next;
713 else if (next == '\n') {
714 file->lineno++;
715 continue;
716 } else
717 lungetc(next);
718 } else if (c == quotec) {
719 *p = '\0';
720 break;
721 } else if (c == '\0') {
722 yyerror("invalid syntax");
723 return findeol();
725 if (p + 1 >= buf + sizeof(buf) - 1) {
726 yyerror("string too long");
727 return findeol();
729 *p++ = c;
731 yylval.v.string = strdup(buf);
732 if (yylval.v.string == NULL)
733 err(1, "yylex: strdup");
734 return STRING;
737 #define allowed_to_end_number(x) \
738 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
740 if (c == '-' || isdigit(c)) {
741 do {
742 *p++ = c;
743 if ((size_t)(p-buf) >= sizeof(buf)) {
744 yyerror("string too long");
745 return findeol();
747 } while ((c = lgetc(0)) != EOF && isdigit(c));
748 lungetc(c);
749 if (p == buf + 1 && buf[0] == '-')
750 goto nodigits;
751 if (c == EOF || allowed_to_end_number(c)) {
752 const char *errstr = NULL;
754 *p = '\0';
755 yylval.v.number = strtonum(buf, LLONG_MIN,
756 LLONG_MAX, &errstr);
757 if (errstr) {
758 yyerror("\"%s\" invalid number: %s",
759 buf, errstr);
760 return findeol();
762 return NUM;
763 } else {
764 nodigits:
765 while (p > buf + 1)
766 lungetc(*--p);
767 c = *--p;
768 if (c == '-')
769 return c;
773 #define allowed_in_string(x) \
774 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
775 x != '{' && x != '}' && \
776 x != '!' && x != '=' && x != '#' && \
777 x != ',' && x != ';'))
779 if (isalnum(c) || c == ':' || c == '_') {
780 do {
781 *p++ = c;
782 if ((size_t)(p-buf) >= sizeof(buf)) {
783 yyerror("string too long");
784 return findeol();
786 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
787 lungetc(c);
788 *p = '\0';
789 if ((token = lookup(buf)) == STRING)
790 yylval.v.string = xstrdup(buf);
791 return token;
793 if (c == '\n') {
794 yylval.lineno = file->lineno;
795 file->lineno++;
797 if (c == EOF)
798 return 0;
799 return c;
802 struct file *
803 pushfile(const char *name, int secret)
805 struct file *nfile;
807 nfile = xcalloc(1, sizeof(*nfile));
808 nfile->name = xstrdup(name);
809 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
810 log_warn(NULL, "can't open %s: %s", nfile->name,
811 strerror(errno));
812 free(nfile->name);
813 free(nfile);
814 return NULL;
816 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
817 nfile->ungetsize = 16;
818 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
819 TAILQ_INSERT_TAIL(&files, nfile, entry);
820 return nfile;
823 int
824 popfile(void)
826 struct file *prev;
828 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
829 prev->errors += file->errors;
831 TAILQ_REMOVE(&files, file, entry);
832 fclose(file->stream);
833 free(file->name);
834 free(file->ungetbuf);
835 free(file);
836 file = prev;
837 return file ? 0 : EOF;
840 void
841 parse_conf(const char *filename)
843 struct sym *sym, *next;
845 file = pushfile(filename, 0);
846 if (file == NULL)
847 exit(1);
848 topfile = file;
850 yyparse();
851 errors = file->errors;
852 popfile();
854 /* Free macros and check which have not been used. */
855 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
856 /* TODO: warn if !sym->used */
857 if (!sym->persist) {
858 free(sym->name);
859 free(sym->val);
860 TAILQ_REMOVE(&symhead, sym, entry);
861 free(sym);
865 if (errors)
866 exit(1);
869 void
870 print_conf(void)
872 struct vhost *h;
873 /* struct location *l; */
874 /* struct envlist *e; */
875 /* struct alist *a; */
877 if (conf.chroot != NULL)
878 printf("chroot \"%s\"\n", conf.chroot);
879 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
880 /* XXX: defined mimes? */
881 printf("port %d\n", conf.port);
882 printf("prefork %d\n", conf.prefork);
883 /* XXX: protocols? */
884 if (conf.user != NULL)
885 printf("user \"%s\"\n", conf.user);
887 TAILQ_FOREACH(h, &hosts, vhosts) {
888 printf("\nserver \"%s\" {\n", h->domain);
889 printf(" cert \"%s\"\n", h->cert);
890 printf(" key \"%s\"\n", h->key);
891 /* TODO: print locations... */
892 printf("}\n");
896 int
897 symset(const char *name, const char *val, int persist)
899 struct sym *sym;
901 TAILQ_FOREACH(sym, &symhead, entry) {
902 if (!strcmp(name, sym->name))
903 break;
906 if (sym != NULL) {
907 if (sym->persist)
908 return 0;
909 else {
910 free(sym->name);
911 free(sym->val);
912 TAILQ_REMOVE(&symhead, sym, entry);
913 free(sym);
917 sym = xcalloc(1, sizeof(*sym));
918 sym->name = xstrdup(name);
919 sym->val = xstrdup(val);
920 sym->used = 0;
921 sym->persist = persist;
923 TAILQ_INSERT_TAIL(&symhead, sym, entry);
924 return 0;
927 int
928 cmdline_symset(char *s)
930 char *sym, *val;
931 int ret;
933 if ((val = strrchr(s, '=')) == NULL)
934 return -1;
935 sym = xcalloc(1, val - s + 1);
936 memcpy(sym, s, val - s);
937 ret = symset(sym, val + 1, 1);
938 free(sym);
939 return ret;
942 char *
943 symget(const char *nam)
945 struct sym *sym;
947 TAILQ_FOREACH(sym, &symhead, entry) {
948 if (strcmp(nam, sym->name) == 0) {
949 sym->used = 1;
950 return sym->val;
953 return NULL;
956 struct vhost *
957 new_vhost(void)
959 return xcalloc(1, sizeof(struct vhost));
962 struct location *
963 new_location(void)
965 struct location *l;
967 l = xcalloc(1, sizeof(*l));
968 l->dirfd = -1;
969 l->fcgi = -1;
970 return l;
973 char *
974 ensure_absolute_path(char *path)
976 if (path == NULL || *path != '/')
977 yyerror("not an absolute path: %s", path);
978 return path;
981 int
982 check_block_code(int n)
984 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
985 yyerror("invalid block code %d", n);
986 return n;
989 char *
990 check_block_fmt(char *fmt)
992 char *s;
994 for (s = fmt; *s; ++s) {
995 if (*s != '%')
996 continue;
997 switch (*++s) {
998 case '%':
999 case 'p':
1000 case 'q':
1001 case 'P':
1002 case 'N':
1003 break;
1004 default:
1005 yyerror("invalid format specifier %%%c", *s);
1009 return fmt;
1012 int
1013 check_strip_no(int n)
1015 if (n <= 0)
1016 yyerror("invalid strip number %d", n);
1017 return n;
1020 int
1021 check_port_num(int n)
1023 if (n <= 0 || n >= UINT16_MAX)
1024 yyerror("port number is %s: %d",
1025 n <= 0 ? "too small" : "too large",
1026 n);
1027 return n;
1030 int
1031 check_prefork_num(int n)
1033 if (n <= 0 || n >= PROC_MAX)
1034 yyerror("invalid prefork number %d", n);
1035 return n;
1038 void
1039 advance_loc(void)
1041 loc = new_location();
1042 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1045 void
1046 only_once(const void *ptr, const char *name)
1048 if (ptr != NULL)
1049 yyerror("`%s' specified more than once", name);
1052 void
1053 only_oncei(int i, const char *name)
1055 if (i != -1)
1056 yyerror("`%s' specified more than once", name);
1059 int
1060 fastcgi_conf(char *path, char *port, char *prog)
1062 struct fcgi *f;
1063 int i;
1065 for (i = 0; i < FCGI_MAX; ++i) {
1066 f = &fcgi[i];
1068 if (f->path == NULL) {
1069 f->id = i;
1070 f->path = path;
1071 f->port = port;
1072 f->prog = prog;
1073 return i;
1076 /* XXX: what to do with prog? */
1077 if (!strcmp(f->path, path) &&
1078 ((port == NULL && f->port == NULL) ||
1079 !strcmp(f->port, port))) {
1080 free(path);
1081 free(port);
1082 return i;
1086 yyerror("too much `fastcgi' rules defined.");
1087 return -1;
1090 void
1091 add_param(char *name, char *val, int env)
1093 struct envlist *e;
1094 struct envhead *h;
1096 if (env)
1097 h = &host->env;
1098 else
1099 h = &host->params;
1101 e = xcalloc(1, sizeof(*e));
1102 e->name = name;
1103 e->value = val;
1104 if (TAILQ_EMPTY(h))
1105 TAILQ_INSERT_HEAD(h, e, envs);
1106 else
1107 TAILQ_INSERT_TAIL(h, e, envs);