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 | locopt
284 locations : /* empty */
285 | locations location optnl
288 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
289 /* drop the starting '/' if any */
290 if (*$3 == '/')
291 memmove($3, $3+1, strlen($3));
292 loc->match = $3;
294 | error '}'
297 locopts : /* empty */
298 | locopts locopt optnl
301 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
302 | BLOCK RETURN NUM string {
303 only_once(loc->block_fmt, "block");
304 loc->block_fmt = check_block_fmt($4);
305 loc->block_code = check_block_code($3);
307 | BLOCK RETURN NUM {
308 only_once(loc->block_fmt, "block");
309 loc->block_fmt = xstrdup("temporary failure");
310 loc->block_code = check_block_code($3);
311 if ($3 >= 30 && $3 < 40)
312 yyerror("missing `meta' for block return %d", $3);
314 | BLOCK {
315 only_once(loc->block_fmt, "block");
316 loc->block_fmt = xstrdup("temporary failure");
317 loc->block_code = 40;
319 | DEFAULT TYPE string {
320 only_once(loc->default_mime, "default type");
321 loc->default_mime = $3;
323 | FASTCGI fastcgi
324 | INDEX string {
325 only_once(loc->index, "index");
326 loc->index = $2;
328 | LANG string {
329 only_once(loc->lang, "lang");
330 loc->lang = $2;
332 | LOG bool { loc->disable_log = !$2; }
333 | proxy
334 | REQUIRE CLIENT CA string {
335 only_once(loc->reqca, "require client ca");
336 ensure_absolute_path($4);
337 if ((loc->reqca = load_ca($4)) == NULL)
338 yyerror("couldn't load ca cert: %s", $4);
339 free($4);
341 | ROOT string {
342 only_once(loc->dir, "root");
343 loc->dir = ensure_absolute_path($2);
345 | STRIP NUM { loc->strip = check_strip_no($2); }
348 proxy : PROXY proxy_opt
349 | PROXY '{' optnl proxy_opts '}'
352 proxy_opts : /* empty */
353 | proxy_opts proxy_opt optnl
356 proxy_opt : CERT string {
357 only_once(loc->proxy_cert, "proxy cert");
358 ensure_absolute_path($2);
359 loc->proxy_cert = tls_load_file($2, &loc->proxy_cert_len, NULL);
360 if (loc->proxy_cert == NULL)
361 yyerror("can't load cert %s", $2);
363 | KEY string {
364 only_once(loc->proxy_key, "proxy key");
365 ensure_absolute_path($2);
366 loc->proxy_key = tls_load_file($2, &loc->proxy_key_len, NULL);
367 if (loc->proxy_key == NULL)
368 yyerror("can't load key %s", $2);
370 | RELAY_TO string {
371 char *at;
372 const char *errstr;
374 only_once(loc->proxy_host, "proxy relay-to");
375 loc->proxy_host = $2;
377 if ((at = strchr($2, ':')) != NULL) {
378 *at++ = '\0';
379 loc->proxy_port = at;
380 } else
381 loc->proxy_port = "1965";
383 strtonum(loc->proxy_port, 1, UINT16_MAX, &errstr);
384 if (errstr != NULL)
385 yyerror("proxy port is %s: %s", errstr,
386 loc->proxy_port);
390 fastcgi : SPAWN string {
391 only_oncei(loc->fcgi, "fastcgi");
392 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
394 | string {
395 only_oncei(loc->fcgi, "fastcgi");
396 loc->fcgi = fastcgi_conf($1, NULL, NULL);
398 | TCP string PORT NUM {
399 char *c;
400 if (asprintf(&c, "%d", $4) == -1)
401 err(1, "asprintf");
402 only_oncei(loc->fcgi, "fastcgi");
403 loc->fcgi = fastcgi_conf($2, c, NULL);
405 | TCP string {
406 only_oncei(loc->fcgi, "fastcgi");
407 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
409 | TCP string PORT string {
410 only_oncei(loc->fcgi, "fastcgi");
411 loc->fcgi = fastcgi_conf($2, $4, NULL);
415 optnl : '\n' optnl /* zero or more newlines */
416 | ';' optnl /* semicolons too */
417 | /*empty*/
420 %%
422 static struct keyword {
423 const char *word;
424 int token;
425 } keywords[] = {
426 /* these MUST be sorted */
427 {"alias", ALIAS},
428 {"auto", AUTO},
429 {"block", BLOCK},
430 {"ca", CA},
431 {"cert", CERT},
432 {"cgi", CGI},
433 {"chroot", CHROOT},
434 {"client", CLIENT},
435 {"default", DEFAULT},
436 {"entrypoint", ENTRYPOINT},
437 {"env", ENV},
438 {"fastcgi", FASTCGI},
439 {"index", INDEX},
440 {"ipv6", IPV6},
441 {"key", KEY},
442 {"lang", LANG},
443 {"location", LOCATION},
444 {"log", LOG},
445 {"map", MAP},
446 {"mime", MIME},
447 {"ocsp", OCSP},
448 {"off", OFF},
449 {"on", ON},
450 {"param", PARAM},
451 {"port", PORT},
452 {"prefork", PREFORK},
453 {"protocols", PROTOCOLS},
454 {"proxy", PROXY},
455 {"relay-to", RELAY_TO},
456 {"require", REQUIRE},
457 {"return", RETURN},
458 {"root", ROOT},
459 {"server", SERVER},
460 {"spawn", SPAWN},
461 {"strip", STRIP},
462 {"tcp", TCP},
463 {"to-ext", TOEXT},
464 {"type", TYPE},
465 {"user", USER},
466 };
468 void
469 yyerror(const char *msg, ...)
471 va_list ap;
473 file->errors++;
475 va_start(ap, msg);
476 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
477 vfprintf(stderr, msg, ap);
478 fprintf(stderr, "\n");
479 va_end(ap);
482 void
483 yywarn(const char *msg, ...)
485 va_list ap;
487 va_start(ap, msg);
488 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
489 vfprintf(stderr, msg, ap);
490 fprintf(stderr, "\n");
491 va_end(ap);
494 int
495 kw_cmp(const void *k, const void *e)
497 return strcmp(k, ((struct keyword *)e)->word);
500 int
501 lookup(char *s)
503 const struct keyword *p;
505 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
506 sizeof(keywords[0]), kw_cmp);
508 if (p)
509 return p->token;
510 else
511 return STRING;
514 #define START_EXPAND 1
515 #define DONE_EXPAND 2
517 static int expanding;
519 int
520 igetc(void)
522 int c;
524 while (1) {
525 if (file->ungetpos > 0)
526 c = file->ungetbuf[--file->ungetpos];
527 else
528 c = getc(file->stream);
530 if (c == START_EXPAND)
531 expanding = 1;
532 else if (c == DONE_EXPAND)
533 expanding = 0;
534 else
535 break;
537 return c;
540 int
541 lgetc(int quotec)
543 int c, next;
545 if (quotec) {
546 if ((c = igetc()) == EOF) {
547 yyerror("reached end of file while parsing "
548 "quoted string");
549 if (file == topfile || popfile() == EOF)
550 return EOF;
551 return quotec;
553 return c;
556 while ((c = igetc()) == '\\') {
557 next = igetc();
558 if (next != '\n') {
559 c = next;
560 break;
562 yylval.lineno = file->lineno;
563 file->lineno++;
566 if (c == EOF) {
567 /*
568 * Fake EOL when hit EOF for the first time. This gets line
569 * count right if last line in included file is syntactically
570 * invalid and has no newline.
571 */
572 if (file->eof_reached == 0) {
573 file->eof_reached = 1;
574 return '\n';
576 while (c == EOF) {
577 if (file == topfile || popfile() == EOF)
578 return EOF;
579 c = igetc();
582 return c;
585 void
586 lungetc(int c)
588 if (c == EOF)
589 return;
591 if (file->ungetpos >= file->ungetsize) {
592 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
593 if (p == NULL)
594 err(1, "lungetc");
595 file->ungetbuf = p;
596 file->ungetsize *= 2;
598 file->ungetbuf[file->ungetpos++] = c;
601 int
602 findeol(void)
604 int c;
606 /* Skip to either EOF or the first real EOL. */
607 while (1) {
608 c = lgetc(0);
609 if (c == '\n') {
610 file->lineno++;
611 break;
613 if (c == EOF)
614 break;
616 return ERROR;
619 int
620 yylex(void)
622 char buf[8096];
623 char *p, *val;
624 int quotec, next, c;
625 int token;
627 top:
628 p = buf;
629 while ((c = lgetc(0)) == ' ' || c == '\t')
630 ; /* nothing */
632 yylval.lineno = file->lineno;
633 if (c == '#')
634 while ((c = lgetc(0)) != '\n' && c != EOF)
635 ; /* nothing */
636 if (c == '$' && !expanding) {
637 while (1) {
638 if ((c = lgetc(0)) == EOF)
639 return 0;
640 if (p + 1 >= buf + sizeof(buf) -1) {
641 yyerror("string too long");
642 return findeol();
644 if (isalnum(c) || c == '_') {
645 *p++ = c;
646 continue;
648 *p = '\0';
649 lungetc(c);
650 break;
652 val = symget(buf);
653 if (val == NULL) {
654 yyerror("macro `%s' not defined", buf);
655 return findeol();
657 yylval.v.string = xstrdup(val);
658 return STRING;
660 if (c == '@' && !expanding) {
661 while (1) {
662 if ((c = lgetc(0)) == EOF)
663 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 p = val + strlen(val) - 1;
683 lungetc(DONE_EXPAND);
684 while (p >= val) {
685 lungetc(*p);
686 p--;
688 lungetc(START_EXPAND);
689 goto top;
692 switch (c) {
693 case '\'':
694 case '"':
695 quotec = c;
696 while (1) {
697 if ((c = lgetc(quotec)) == EOF)
698 return 0;
699 if (c == '\n') {
700 file->lineno++;
701 continue;
702 } else if (c == '\\') {
703 if ((next = lgetc(quotec)) == EOF)
704 return (0);
705 if (next == quotec || next == ' ' ||
706 next == '\t')
707 c = next;
708 else if (next == '\n') {
709 file->lineno++;
710 continue;
711 } else
712 lungetc(next);
713 } else if (c == quotec) {
714 *p = '\0';
715 break;
716 } else if (c == '\0') {
717 yyerror("invalid syntax");
718 return findeol();
720 if (p + 1 >= buf + sizeof(buf) - 1) {
721 yyerror("string too long");
722 return findeol();
724 *p++ = c;
726 yylval.v.string = strdup(buf);
727 if (yylval.v.string == NULL)
728 err(1, "yylex: strdup");
729 return STRING;
732 #define allowed_to_end_number(x) \
733 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
735 if (c == '-' || isdigit(c)) {
736 do {
737 *p++ = c;
738 if ((size_t)(p-buf) >= sizeof(buf)) {
739 yyerror("string too long");
740 return findeol();
742 } while ((c = lgetc(0)) != EOF && isdigit(c));
743 lungetc(c);
744 if (p == buf + 1 && buf[0] == '-')
745 goto nodigits;
746 if (c == EOF || allowed_to_end_number(c)) {
747 const char *errstr = NULL;
749 *p = '\0';
750 yylval.v.number = strtonum(buf, LLONG_MIN,
751 LLONG_MAX, &errstr);
752 if (errstr) {
753 yyerror("\"%s\" invalid number: %s",
754 buf, errstr);
755 return findeol();
757 return NUM;
758 } else {
759 nodigits:
760 while (p > buf + 1)
761 lungetc(*--p);
762 c = *--p;
763 if (c == '-')
764 return c;
768 #define allowed_in_string(x) \
769 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
770 x != '{' && x != '}' && \
771 x != '!' && x != '=' && x != '#' && \
772 x != ',' && x != ';'))
774 if (isalnum(c) || c == ':' || c == '_') {
775 do {
776 *p++ = c;
777 if ((size_t)(p-buf) >= sizeof(buf)) {
778 yyerror("string too long");
779 return findeol();
781 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
782 lungetc(c);
783 *p = '\0';
784 if ((token = lookup(buf)) == STRING)
785 yylval.v.string = xstrdup(buf);
786 return token;
788 if (c == '\n') {
789 yylval.lineno = file->lineno;
790 file->lineno++;
792 if (c == EOF)
793 return 0;
794 return c;
797 struct file *
798 pushfile(const char *name, int secret)
800 struct file *nfile;
802 nfile = xcalloc(1, sizeof(*nfile));
803 nfile->name = xstrdup(name);
804 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
805 log_warn(NULL, "can't open %s: %s", nfile->name,
806 strerror(errno));
807 free(nfile->name);
808 free(nfile);
809 return NULL;
811 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
812 nfile->ungetsize = 16;
813 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
814 TAILQ_INSERT_TAIL(&files, nfile, entry);
815 return nfile;
818 int
819 popfile(void)
821 struct file *prev;
823 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
824 prev->errors += file->errors;
826 TAILQ_REMOVE(&files, file, entry);
827 fclose(file->stream);
828 free(file->name);
829 free(file->ungetbuf);
830 free(file);
831 file = prev;
832 return file ? 0 : EOF;
835 void
836 parse_conf(const char *filename)
838 struct sym *sym, *next;
840 file = pushfile(filename, 0);
841 if (file == NULL)
842 exit(1);
843 topfile = file;
845 yyparse();
846 errors = file->errors;
847 popfile();
849 /* Free macros and check which have not been used. */
850 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
851 /* TODO: warn if !sym->used */
852 if (!sym->persist) {
853 free(sym->name);
854 free(sym->val);
855 TAILQ_REMOVE(&symhead, sym, entry);
856 free(sym);
860 if (errors)
861 exit(1);
864 void
865 print_conf(void)
867 struct vhost *h;
868 /* struct location *l; */
869 /* struct envlist *e; */
870 /* struct alist *a; */
872 if (conf.chroot != NULL)
873 printf("chroot \"%s\"\n", conf.chroot);
874 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
875 /* XXX: defined mimes? */
876 printf("port %d\n", conf.port);
877 printf("prefork %d\n", conf.prefork);
878 /* XXX: protocols? */
879 if (conf.user != NULL)
880 printf("user \"%s\"\n", conf.user);
882 TAILQ_FOREACH(h, &hosts, vhosts) {
883 printf("\nserver \"%s\" {\n", h->domain);
884 printf(" cert \"%s\"\n", h->cert);
885 printf(" key \"%s\"\n", h->key);
886 /* TODO: print locations... */
887 printf("}\n");
891 int
892 symset(const char *name, const char *val, int persist)
894 struct sym *sym;
896 TAILQ_FOREACH(sym, &symhead, entry) {
897 if (!strcmp(name, sym->name))
898 break;
901 if (sym != NULL) {
902 if (sym->persist)
903 return 0;
904 else {
905 free(sym->name);
906 free(sym->val);
907 TAILQ_REMOVE(&symhead, sym, entry);
908 free(sym);
912 sym = xcalloc(1, sizeof(*sym));
913 sym->name = xstrdup(name);
914 sym->val = xstrdup(val);
915 sym->used = 0;
916 sym->persist = persist;
918 TAILQ_INSERT_TAIL(&symhead, sym, entry);
919 return 0;
922 int
923 cmdline_symset(char *s)
925 char *sym, *val;
926 int ret;
928 if ((val = strrchr(s, '=')) == NULL)
929 return -1;
930 sym = xcalloc(1, val - s + 1);
931 memcpy(sym, s, val - s);
932 ret = symset(sym, val + 1, 1);
933 free(sym);
934 return ret;
937 char *
938 symget(const char *nam)
940 struct sym *sym;
942 TAILQ_FOREACH(sym, &symhead, entry) {
943 if (strcmp(nam, sym->name) == 0) {
944 sym->used = 1;
945 return sym->val;
948 return NULL;
951 struct vhost *
952 new_vhost(void)
954 return xcalloc(1, sizeof(struct vhost));
957 struct location *
958 new_location(void)
960 struct location *l;
962 l = xcalloc(1, sizeof(*l));
963 l->dirfd = -1;
964 l->fcgi = -1;
965 return l;
968 char *
969 ensure_absolute_path(char *path)
971 if (path == NULL || *path != '/')
972 yyerror("not an absolute path: %s", path);
973 return path;
976 int
977 check_block_code(int n)
979 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
980 yyerror("invalid block code %d", n);
981 return n;
984 char *
985 check_block_fmt(char *fmt)
987 char *s;
989 for (s = fmt; *s; ++s) {
990 if (*s != '%')
991 continue;
992 switch (*++s) {
993 case '%':
994 case 'p':
995 case 'q':
996 case 'P':
997 case 'N':
998 break;
999 default:
1000 yyerror("invalid format specifier %%%c", *s);
1004 return fmt;
1007 int
1008 check_strip_no(int n)
1010 if (n <= 0)
1011 yyerror("invalid strip number %d", n);
1012 return n;
1015 int
1016 check_port_num(int n)
1018 if (n <= 0 || n >= UINT16_MAX)
1019 yyerror("port number is %s: %d",
1020 n <= 0 ? "too small" : "too large",
1021 n);
1022 return n;
1025 int
1026 check_prefork_num(int n)
1028 if (n <= 0 || n >= PROC_MAX)
1029 yyerror("invalid prefork number %d", n);
1030 return n;
1033 void
1034 advance_loc(void)
1036 loc = new_location();
1037 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1040 void
1041 only_once(const void *ptr, const char *name)
1043 if (ptr != NULL)
1044 yyerror("`%s' specified more than once", name);
1047 void
1048 only_oncei(int i, const char *name)
1050 if (i != -1)
1051 yyerror("`%s' specified more than once", name);
1054 int
1055 fastcgi_conf(char *path, char *port, char *prog)
1057 struct fcgi *f;
1058 int i;
1060 for (i = 0; i < FCGI_MAX; ++i) {
1061 f = &fcgi[i];
1063 if (f->path == NULL) {
1064 f->id = i;
1065 f->path = path;
1066 f->port = port;
1067 f->prog = prog;
1068 return i;
1071 /* XXX: what to do with prog? */
1072 if (!strcmp(f->path, path) &&
1073 ((port == NULL && f->port == NULL) ||
1074 !strcmp(f->port, port))) {
1075 free(path);
1076 free(port);
1077 return i;
1081 yyerror("too much `fastcgi' rules defined.");
1082 return -1;
1085 void
1086 add_param(char *name, char *val, int env)
1088 struct envlist *e;
1089 struct envhead *h;
1091 if (env)
1092 h = &host->env;
1093 else
1094 h = &host->params;
1096 e = xcalloc(1, sizeof(*e));
1097 e->name = name;
1098 e->value = val;
1099 if (TAILQ_EMPTY(h))
1100 TAILQ_INSERT_HEAD(h, e, envs);
1101 else
1102 TAILQ_INSERT_TAIL(h, e, envs);