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 TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
114 %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
115 %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
116 %token TFASTCGI TSPAWN TPARAM TMAP TTOEXT INCLUDE TON TOFF
118 %token ERROR
120 %token <v.string> STRING
121 %token <v.number> NUM
123 %type <v.number> bool
124 %type <v.string> string
126 %%
128 conf : /* empty */
129 | conf include '\n'
130 | conf '\n'
131 | conf varset '\n'
132 | conf option '\n'
133 | conf vhost '\n'
134 | conf error '\n' { file->errors++; }
137 include : INCLUDE STRING {
138 struct file *nfile;
140 if ((nfile = pushfile($2, 0)) == NULL) {
141 yyerror("failed to include file %s", $2);
142 free($2);
143 YYERROR;
145 free($2);
147 file = nfile;
148 lungetc('\n');
152 bool : TON { $$ = 1; }
153 | TOFF { $$ = 0; }
156 string : string STRING {
157 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
158 free($1);
159 free($2);
160 yyerror("string: asprintf: %s", strerror(errno));
161 YYERROR;
163 free($1);
164 free($2);
166 | STRING
169 varset : STRING '=' string {
170 char *s = $1;
171 while (*s++) {
172 if (isspace((unsigned char)*s)) {
173 yyerror("macro name cannot contain "
174 "whitespaces");
175 free($1);
176 free($3);
177 YYERROR;
180 symset($1, $3, 0);
181 free($1);
182 free($3);
186 option : TCHROOT string { conf.chroot = $2; }
187 | TIPV6 bool { conf.ipv6 = $2; }
188 | TMIME STRING string {
189 yywarn("`mime MIME EXT' is deprecated and will be "
190 "removed in a future version, please use the new "
191 "syntax: `map MIME to-ext EXT'");
192 add_mime(&conf.mime, $2, $3);
194 | TMAP string TTOEXT string { add_mime(&conf.mime, $2, $4); }
195 | TPORT NUM { conf.port = check_port_num($2); }
196 | TPREFORK NUM { conf.prefork = check_prefork_num($2); }
197 | TPROTOCOLS string {
198 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
199 yyerror("invalid protocols string \"%s\"", $2);
201 | TUSER string { conf.user = $2; }
204 vhost : TSERVER string {
205 host = new_vhost();
206 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
208 loc = new_location();
209 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
211 loc->match = xstrdup("*");
212 host->domain = $2;
214 if (strstr($2, "xn--") != NULL) {
215 yywarn("\"%s\" looks like punycode: you "
216 "should use the decoded hostname", $2);
218 } '{' optnl servopts locations '}' {
219 if (host->cert == NULL || host->key == NULL)
220 yyerror("invalid vhost definition: %s", $2);
222 | error '}' { yyerror("bad server directive"); }
225 servopts : /* empty */
226 | servopts servopt optnl
229 servopt : TALIAS string {
230 struct alist *a;
232 a = xcalloc(1, sizeof(*a));
233 a->alias = $2;
234 if (TAILQ_EMPTY(&host->aliases))
235 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
236 else
237 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
239 | TCERT string {
240 only_once(host->cert, "cert");
241 host->cert = ensure_absolute_path($2);
243 | TCGI string {
244 only_once(host->cgi, "cgi");
245 /* drop the starting '/', if any */
246 if (*$2 == '/')
247 memmove($2, $2+1, strlen($2));
248 host->cgi = $2;
250 | TENTRYPOINT string {
251 only_once(host->entrypoint, "entrypoint");
252 while (*$2 == '/')
253 memmove($2, $2+1, strlen($2));
254 host->entrypoint = $2;
256 | TENV string '=' string {
257 add_param($2, $4, 1);
259 | TKEY string {
260 only_once(host->key, "key");
261 host->key = ensure_absolute_path($2);
263 | TPARAM string '=' string {
264 add_param($2, $4, 0);
266 | locopt
269 locations : /* empty */
270 | locations location optnl
273 location : TLOCATION { advance_loc(); } string '{' optnl locopts '}' {
274 /* drop the starting '/' if any */
275 if (*$3 == '/')
276 memmove($3, $3+1, strlen($3));
277 loc->match = $3;
279 | error '}'
282 locopts : /* empty */
283 | locopts locopt optnl
286 locopt : TAUTO TINDEX bool { loc->auto_index = $3 ? 1 : -1; }
287 | TBLOCK TRETURN NUM string {
288 only_once(loc->block_fmt, "block");
289 loc->block_fmt = check_block_fmt($4);
290 loc->block_code = check_block_code($3);
292 | TBLOCK TRETURN NUM {
293 only_once(loc->block_fmt, "block");
294 loc->block_fmt = xstrdup("temporary failure");
295 loc->block_code = check_block_code($3);
296 if ($3 >= 30 && $3 < 40)
297 yyerror("missing `meta' for block return %d", $3);
299 | TBLOCK {
300 only_once(loc->block_fmt, "block");
301 loc->block_fmt = xstrdup("temporary failure");
302 loc->block_code = 40;
304 | TDEFAULT TTYPE string {
305 only_once(loc->default_mime, "default type");
306 loc->default_mime = $3;
308 | TFASTCGI fastcgi
309 | TINDEX string {
310 only_once(loc->index, "index");
311 loc->index = $2;
313 | TLANG string {
314 only_once(loc->lang, "lang");
315 loc->lang = $2;
317 | TLOG bool { loc->disable_log = !$2; }
318 | TREQUIRE TCLIENT TCA string {
319 only_once(loc->reqca, "require client ca");
320 ensure_absolute_path($4);
321 if ((loc->reqca = load_ca($4)) == NULL)
322 yyerror("couldn't load ca cert: %s", $4);
323 free($4);
325 | TROOT string {
326 only_once(loc->dir, "root");
327 loc->dir = ensure_absolute_path($2);
329 | TSTRIP NUM { loc->strip = check_strip_no($2); }
332 fastcgi : TSPAWN string {
333 only_oncei(loc->fcgi, "fastcgi");
334 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
336 | string {
337 only_oncei(loc->fcgi, "fastcgi");
338 loc->fcgi = fastcgi_conf($1, NULL, NULL);
340 | TTCP string TPORT NUM {
341 char *c;
342 if (asprintf(&c, "%d", $4) == -1)
343 err(1, "asprintf");
344 only_oncei(loc->fcgi, "fastcgi");
345 loc->fcgi = fastcgi_conf($2, c, NULL);
347 | TTCP string {
348 only_oncei(loc->fcgi, "fastcgi");
349 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
351 | TTCP string TPORT string {
352 only_oncei(loc->fcgi, "fastcgi");
353 loc->fcgi = fastcgi_conf($2, $4, NULL);
357 optnl : '\n' optnl /* zero or more newlines */
358 | ';' optnl /* semicolons too */
359 | /*empty*/
362 %%
364 static struct keyword {
365 const char *word;
366 int token;
367 } keywords[] = {
368 /* these MUST be sorted */
369 {"alias", TALIAS},
370 {"auto", TAUTO},
371 {"block", TBLOCK},
372 {"ca", TCA},
373 {"cert", TCERT},
374 {"cgi", TCGI},
375 {"chroot", TCHROOT},
376 {"client", TCLIENT},
377 {"default", TDEFAULT},
378 {"entrypoint", TENTRYPOINT},
379 {"env", TENV},
380 {"fastcgi", TFASTCGI},
381 {"index", TINDEX},
382 {"ipv6", TIPV6},
383 {"key", TKEY},
384 {"lang", TLANG},
385 {"location", TLOCATION},
386 {"log", TLOG},
387 {"map", TMAP},
388 {"mime", TMIME},
389 {"off", TOFF},
390 {"on", TON},
391 {"param", TPARAM},
392 {"port", TPORT},
393 {"prefork", TPREFORK},
394 {"protocols", TPROTOCOLS},
395 {"require", TREQUIRE},
396 {"return", TRETURN},
397 {"root", TROOT},
398 {"server", TSERVER},
399 {"spawn", TSPAWN},
400 {"strip", TSTRIP},
401 {"tcp", TTCP},
402 {"to-ext", TTOEXT},
403 {"type", TTYPE},
404 {"user", TUSER},
405 };
407 void
408 yyerror(const char *msg, ...)
410 va_list ap;
412 file->errors++;
414 va_start(ap, msg);
415 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
416 vfprintf(stderr, msg, ap);
417 fprintf(stderr, "\n");
418 va_end(ap);
421 void
422 yywarn(const char *msg, ...)
424 va_list ap;
426 va_start(ap, msg);
427 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
428 vfprintf(stderr, msg, ap);
429 fprintf(stderr, "\n");
430 va_end(ap);
433 int
434 kw_cmp(const void *k, const void *e)
436 return strcmp(k, ((struct keyword *)e)->word);
439 int
440 lookup(char *s)
442 const struct keyword *p;
444 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
445 sizeof(keywords[0]), kw_cmp);
447 if (p)
448 return p->token;
449 else
450 return STRING;
453 #define START_EXPAND 1
454 #define DONE_EXPAND 2
456 static int expanding;
458 int
459 igetc(void)
461 int c;
463 while (1) {
464 if (file->ungetpos > 0)
465 c = file->ungetbuf[--file->ungetpos];
466 else
467 c = getc(file->stream);
469 if (c == START_EXPAND)
470 expanding = 1;
471 else if (c == DONE_EXPAND)
472 expanding = 0;
473 else
474 break;
476 return c;
479 int
480 lgetc(int quotec)
482 int c, next;
484 if (quotec) {
485 if ((c = igetc()) == EOF) {
486 yyerror("reached end of file while parsing "
487 "quoted string");
488 if (file == topfile || popfile() == EOF)
489 return EOF;
490 return quotec;
492 return c;
495 while ((c = igetc()) == '\\') {
496 next = igetc();
497 if (next != '\n') {
498 c = next;
499 break;
501 yylval.lineno = file->lineno;
502 file->lineno++;
505 if (c == EOF) {
506 /*
507 * Fake EOL when hit EOF for the first time. This gets line
508 * count right if last line in included file is syntactically
509 * invalid and has no newline.
510 */
511 if (file->eof_reached == 0) {
512 file->eof_reached = 1;
513 return '\n';
515 while (c == EOF) {
516 if (file == topfile || popfile() == EOF)
517 return EOF;
518 c = igetc();
521 return c;
524 void
525 lungetc(int c)
527 if (c == EOF)
528 return;
530 if (file->ungetpos >= file->ungetsize) {
531 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
532 if (p == NULL)
533 err(1, "lungetc");
534 file->ungetbuf = p;
535 file->ungetsize *= 2;
537 file->ungetbuf[file->ungetpos++] = c;
540 int
541 findeol(void)
543 int c;
545 /* Skip to either EOF or the first real EOL. */
546 while (1) {
547 c = lgetc(0);
548 if (c == '\n') {
549 file->lineno++;
550 break;
552 if (c == EOF)
553 break;
555 return ERROR;
558 int
559 yylex(void)
561 char buf[8096];
562 char *p, *val;
563 int quotec, next, c;
564 int token;
566 top:
567 p = buf;
568 while ((c = lgetc(0)) == ' ' || c == '\t')
569 ; /* nothing */
571 yylval.lineno = file->lineno;
572 if (c == '#')
573 while ((c = lgetc(0)) != '\n' && c != EOF)
574 ; /* nothing */
575 if (c == '$' && !expanding) {
576 while (1) {
577 if ((c = lgetc(0)) == EOF)
578 return 0;
579 if (p + 1 >= buf + sizeof(buf) -1) {
580 yyerror("string too long");
581 return findeol();
583 if (isalnum(c) || c == '_') {
584 *p++ = c;
585 continue;
587 *p = '\0';
588 lungetc(c);
589 break;
591 val = symget(buf);
592 if (val == NULL) {
593 yyerror("macro `%s' not defined", buf);
594 return findeol();
596 yylval.v.string = xstrdup(val);
597 return STRING;
599 if (c == '@' && !expanding) {
600 while (1) {
601 if ((c = lgetc(0)) == EOF)
602 return 0;
604 if (p + 1 >= buf + sizeof(buf) - 1) {
605 yyerror("string too long");
606 return findeol();
608 if (isalnum(c) || c == '_') {
609 *p++ = c;
610 continue;
612 *p = '\0';
613 lungetc(c);
614 break;
616 val = symget(buf);
617 if (val == NULL) {
618 yyerror("macro '%s' not defined", buf);
619 return findeol();
621 p = val + strlen(val) - 1;
622 lungetc(DONE_EXPAND);
623 while (p >= val) {
624 lungetc(*p);
625 p--;
627 lungetc(START_EXPAND);
628 goto top;
631 switch (c) {
632 case '\'':
633 case '"':
634 quotec = c;
635 while (1) {
636 if ((c = lgetc(quotec)) == EOF)
637 return 0;
638 if (c == '\n') {
639 file->lineno++;
640 continue;
641 } else if (c == '\\') {
642 if ((next = lgetc(quotec)) == EOF)
643 return (0);
644 if (next == quotec || next == ' ' ||
645 next == '\t')
646 c = next;
647 else if (next == '\n') {
648 file->lineno++;
649 continue;
650 } else
651 lungetc(next);
652 } else if (c == quotec) {
653 *p = '\0';
654 break;
655 } else if (c == '\0') {
656 yyerror("invalid syntax");
657 return findeol();
659 if (p + 1 >= buf + sizeof(buf) - 1) {
660 yyerror("string too long");
661 return findeol();
663 *p++ = c;
665 yylval.v.string = strdup(buf);
666 if (yylval.v.string == NULL)
667 err(1, "yylex: strdup");
668 return STRING;
671 #define allowed_to_end_number(x) \
672 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
674 if (c == '-' || isdigit(c)) {
675 do {
676 *p++ = c;
677 if ((size_t)(p-buf) >= sizeof(buf)) {
678 yyerror("string too long");
679 return findeol();
681 } while ((c = lgetc(0)) != EOF && isdigit(c));
682 lungetc(c);
683 if (p == buf + 1 && buf[0] == '-')
684 goto nodigits;
685 if (c == EOF || allowed_to_end_number(c)) {
686 const char *errstr = NULL;
688 *p = '\0';
689 yylval.v.number = strtonum(buf, LLONG_MIN,
690 LLONG_MAX, &errstr);
691 if (errstr) {
692 yyerror("\"%s\" invalid number: %s",
693 buf, errstr);
694 return findeol();
696 return NUM;
697 } else {
698 nodigits:
699 while (p > buf + 1)
700 lungetc(*--p);
701 c = *--p;
702 if (c == '-')
703 return c;
707 #define allowed_in_string(x) \
708 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
709 x != '{' && x != '}' && \
710 x != '!' && x != '=' && x != '#' && \
711 x != ',' && x != ';'))
713 if (isalnum(c) || c == ':' || c == '_') {
714 do {
715 *p++ = c;
716 if ((size_t)(p-buf) >= sizeof(buf)) {
717 yyerror("string too long");
718 return findeol();
720 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
721 lungetc(c);
722 *p = '\0';
723 if ((token = lookup(buf)) == STRING)
724 yylval.v.string = xstrdup(buf);
725 return token;
727 if (c == '\n') {
728 yylval.lineno = file->lineno;
729 file->lineno++;
731 if (c == EOF)
732 return 0;
733 return c;
736 struct file *
737 pushfile(const char *name, int secret)
739 struct file *nfile;
741 nfile = xcalloc(1, sizeof(*nfile));
742 nfile->name = xstrdup(name);
743 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
744 yyerror("can't open %s: %s", nfile->name,
745 strerror(errno));
746 free(nfile->name);
747 free(nfile);
748 return NULL;
750 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
751 nfile->ungetsize = 16;
752 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
753 TAILQ_INSERT_TAIL(&files, nfile, entry);
754 return nfile;
757 int
758 popfile(void)
760 struct file *prev;
762 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
763 prev->errors += file->errors;
765 TAILQ_REMOVE(&files, file, entry);
766 fclose(file->stream);
767 free(file->name);
768 free(file->ungetbuf);
769 free(file);
770 file = prev;
771 return file ? 0 : EOF;
774 void
775 parse_conf(const char *filename)
777 struct sym *sym, *next;
779 file = pushfile(filename, 0);
780 if (file == NULL)
781 return;
782 topfile = file;
784 yyparse();
785 errors = file->errors;
786 popfile();
788 /* Free macros and check which have not been used. */
789 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
790 /* TODO: warn if !sym->used */
791 if (!sym->persist) {
792 free(sym->name);
793 free(sym->val);
794 TAILQ_REMOVE(&symhead, sym, entry);
795 free(sym);
799 if (errors)
800 exit(1);
803 int
804 symset(const char *name, const char *val, int persist)
806 struct sym *sym;
808 TAILQ_FOREACH(sym, &symhead, entry) {
809 if (!strcmp(name, sym->name))
810 break;
813 if (sym != NULL) {
814 if (sym->persist)
815 return 0;
816 else {
817 free(sym->name);
818 free(sym->val);
819 TAILQ_REMOVE(&symhead, sym, entry);
820 free(sym);
824 sym = xcalloc(1, sizeof(*sym));
825 sym->name = xstrdup(name);
826 sym->val = xstrdup(val);
827 sym->used = 0;
828 sym->persist = persist;
830 TAILQ_INSERT_TAIL(&symhead, sym, entry);
831 return 0;
834 int
835 cmdline_symset(char *s)
837 char *sym, *val;
838 int ret;
840 if ((val = strrchr(s, '=')) == NULL)
841 return -1;
842 sym = xcalloc(1, val - s + 1);
843 memcpy(sym, s, val - s);
844 ret = symset(sym, val + 1, 1);
845 free(sym);
846 return ret;
849 char *
850 symget(const char *nam)
852 struct sym *sym;
854 TAILQ_FOREACH(sym, &symhead, entry) {
855 if (strcmp(nam, sym->name) == 0) {
856 sym->used = 1;
857 return sym->val;
860 return NULL;
863 struct vhost *
864 new_vhost(void)
866 return xcalloc(1, sizeof(struct vhost));
869 struct location *
870 new_location(void)
872 struct location *l;
874 l = xcalloc(1, sizeof(*l));
875 l->dirfd = -1;
876 l->fcgi = -1;
877 return l;
880 char *
881 ensure_absolute_path(char *path)
883 if (path == NULL || *path != '/')
884 yyerror("not an absolute path: %s", path);
885 return path;
888 int
889 check_block_code(int n)
891 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
892 yyerror("invalid block code %d", n);
893 return n;
896 char *
897 check_block_fmt(char *fmt)
899 char *s;
901 for (s = fmt; *s; ++s) {
902 if (*s != '%')
903 continue;
904 switch (*++s) {
905 case '%':
906 case 'p':
907 case 'q':
908 case 'P':
909 case 'N':
910 break;
911 default:
912 yyerror("invalid format specifier %%%c", *s);
916 return fmt;
919 int
920 check_strip_no(int n)
922 if (n <= 0)
923 yyerror("invalid strip number %d", n);
924 return n;
927 int
928 check_port_num(int n)
930 if (n <= 0 || n >= UINT16_MAX)
931 yyerror("port number is %s: %d",
932 n <= 0 ? "too small" : "too large",
933 n);
934 return n;
937 int
938 check_prefork_num(int n)
940 if (n <= 0 || n >= PROC_MAX)
941 yyerror("invalid prefork number %d", n);
942 return n;
945 void
946 advance_loc(void)
948 loc = new_location();
949 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
952 void
953 only_once(const void *ptr, const char *name)
955 if (ptr != NULL)
956 yyerror("`%s' specified more than once", name);
959 void
960 only_oncei(int i, const char *name)
962 if (i != -1)
963 yyerror("`%s' specified more than once", name);
966 int
967 fastcgi_conf(char *path, char *port, char *prog)
969 struct fcgi *f;
970 int i;
972 for (i = 0; i < FCGI_MAX; ++i) {
973 f = &fcgi[i];
975 if (f->path == NULL) {
976 f->id = i;
977 f->path = path;
978 f->port = port;
979 f->prog = prog;
980 return i;
983 /* XXX: what to do with prog? */
984 if (!strcmp(f->path, path) &&
985 ((port == NULL && f->port == NULL) ||
986 !strcmp(f->port, port))) {
987 free(path);
988 free(port);
989 return i;
993 yyerror("too much `fastcgi' rules defined.");
994 return -1;
997 void
998 add_param(char *name, char *val, int env)
1000 struct envlist *e;
1001 struct envhead *h;
1003 if (env)
1004 h = &host->env;
1005 else
1006 h = &host->params;
1008 e = xcalloc(1, sizeof(*e));
1009 e->name = name;
1010 e->value = val;
1011 if (TAILQ_EMPTY(h))
1012 TAILQ_INSERT_HEAD(h, e, envs);
1013 else
1014 TAILQ_INSERT_TAIL(h, e, envs);