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 OFF ON
124 %token PARAM PORT PREFORK PROTOCOLS
125 %token 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 | PARAM string '=' string {
275 add_param($2, $4, 0);
277 | locopt
280 locations : /* empty */
281 | locations location optnl
284 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
285 /* drop the starting '/' if any */
286 if (*$3 == '/')
287 memmove($3, $3+1, strlen($3));
288 loc->match = $3;
290 | error '}'
293 locopts : /* empty */
294 | locopts locopt optnl
297 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
298 | BLOCK RETURN NUM string {
299 only_once(loc->block_fmt, "block");
300 loc->block_fmt = check_block_fmt($4);
301 loc->block_code = check_block_code($3);
303 | BLOCK RETURN NUM {
304 only_once(loc->block_fmt, "block");
305 loc->block_fmt = xstrdup("temporary failure");
306 loc->block_code = check_block_code($3);
307 if ($3 >= 30 && $3 < 40)
308 yyerror("missing `meta' for block return %d", $3);
310 | BLOCK {
311 only_once(loc->block_fmt, "block");
312 loc->block_fmt = xstrdup("temporary failure");
313 loc->block_code = 40;
315 | DEFAULT TYPE string {
316 only_once(loc->default_mime, "default type");
317 loc->default_mime = $3;
319 | FASTCGI fastcgi
320 | INDEX string {
321 only_once(loc->index, "index");
322 loc->index = $2;
324 | LANG string {
325 only_once(loc->lang, "lang");
326 loc->lang = $2;
328 | LOG bool { loc->disable_log = !$2; }
329 | REQUIRE CLIENT CA string {
330 only_once(loc->reqca, "require client ca");
331 ensure_absolute_path($4);
332 if ((loc->reqca = load_ca($4)) == NULL)
333 yyerror("couldn't load ca cert: %s", $4);
334 free($4);
336 | ROOT string {
337 only_once(loc->dir, "root");
338 loc->dir = ensure_absolute_path($2);
340 | STRIP NUM { loc->strip = check_strip_no($2); }
343 fastcgi : SPAWN string {
344 only_oncei(loc->fcgi, "fastcgi");
345 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
347 | string {
348 only_oncei(loc->fcgi, "fastcgi");
349 loc->fcgi = fastcgi_conf($1, NULL, NULL);
351 | TCP string PORT NUM {
352 char *c;
353 if (asprintf(&c, "%d", $4) == -1)
354 err(1, "asprintf");
355 only_oncei(loc->fcgi, "fastcgi");
356 loc->fcgi = fastcgi_conf($2, c, NULL);
358 | TCP string {
359 only_oncei(loc->fcgi, "fastcgi");
360 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
362 | TCP string PORT string {
363 only_oncei(loc->fcgi, "fastcgi");
364 loc->fcgi = fastcgi_conf($2, $4, NULL);
368 optnl : '\n' optnl /* zero or more newlines */
369 | ';' optnl /* semicolons too */
370 | /*empty*/
373 %%
375 static struct keyword {
376 const char *word;
377 int token;
378 } keywords[] = {
379 /* these MUST be sorted */
380 {"alias", ALIAS},
381 {"auto", AUTO},
382 {"block", BLOCK},
383 {"ca", CA},
384 {"cert", CERT},
385 {"cgi", CGI},
386 {"chroot", CHROOT},
387 {"client", CLIENT},
388 {"default", DEFAULT},
389 {"entrypoint", ENTRYPOINT},
390 {"env", ENV},
391 {"fastcgi", FASTCGI},
392 {"index", INDEX},
393 {"ipv6", IPV6},
394 {"key", KEY},
395 {"lang", LANG},
396 {"location", LOCATION},
397 {"log", LOG},
398 {"map", MAP},
399 {"mime", MIME},
400 {"off", OFF},
401 {"on", ON},
402 {"param", PARAM},
403 {"port", PORT},
404 {"prefork", PREFORK},
405 {"protocols", PROTOCOLS},
406 {"require", REQUIRE},
407 {"return", RETURN},
408 {"root", ROOT},
409 {"server", SERVER},
410 {"spawn", SPAWN},
411 {"strip", STRIP},
412 {"tcp", TCP},
413 {"to-ext", TOEXT},
414 {"type", TYPE},
415 {"user", USER},
416 };
418 void
419 yyerror(const char *msg, ...)
421 va_list ap;
423 file->errors++;
425 va_start(ap, msg);
426 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
427 vfprintf(stderr, msg, ap);
428 fprintf(stderr, "\n");
429 va_end(ap);
432 void
433 yywarn(const char *msg, ...)
435 va_list ap;
437 va_start(ap, msg);
438 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
439 vfprintf(stderr, msg, ap);
440 fprintf(stderr, "\n");
441 va_end(ap);
444 int
445 kw_cmp(const void *k, const void *e)
447 return strcmp(k, ((struct keyword *)e)->word);
450 int
451 lookup(char *s)
453 const struct keyword *p;
455 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
456 sizeof(keywords[0]), kw_cmp);
458 if (p)
459 return p->token;
460 else
461 return STRING;
464 #define START_EXPAND 1
465 #define DONE_EXPAND 2
467 static int expanding;
469 int
470 igetc(void)
472 int c;
474 while (1) {
475 if (file->ungetpos > 0)
476 c = file->ungetbuf[--file->ungetpos];
477 else
478 c = getc(file->stream);
480 if (c == START_EXPAND)
481 expanding = 1;
482 else if (c == DONE_EXPAND)
483 expanding = 0;
484 else
485 break;
487 return c;
490 int
491 lgetc(int quotec)
493 int c, next;
495 if (quotec) {
496 if ((c = igetc()) == EOF) {
497 yyerror("reached end of file while parsing "
498 "quoted string");
499 if (file == topfile || popfile() == EOF)
500 return EOF;
501 return quotec;
503 return c;
506 while ((c = igetc()) == '\\') {
507 next = igetc();
508 if (next != '\n') {
509 c = next;
510 break;
512 yylval.lineno = file->lineno;
513 file->lineno++;
516 if (c == EOF) {
517 /*
518 * Fake EOL when hit EOF for the first time. This gets line
519 * count right if last line in included file is syntactically
520 * invalid and has no newline.
521 */
522 if (file->eof_reached == 0) {
523 file->eof_reached = 1;
524 return '\n';
526 while (c == EOF) {
527 if (file == topfile || popfile() == EOF)
528 return EOF;
529 c = igetc();
532 return c;
535 void
536 lungetc(int c)
538 if (c == EOF)
539 return;
541 if (file->ungetpos >= file->ungetsize) {
542 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
543 if (p == NULL)
544 err(1, "lungetc");
545 file->ungetbuf = p;
546 file->ungetsize *= 2;
548 file->ungetbuf[file->ungetpos++] = c;
551 int
552 findeol(void)
554 int c;
556 /* Skip to either EOF or the first real EOL. */
557 while (1) {
558 c = lgetc(0);
559 if (c == '\n') {
560 file->lineno++;
561 break;
563 if (c == EOF)
564 break;
566 return ERROR;
569 int
570 yylex(void)
572 char buf[8096];
573 char *p, *val;
574 int quotec, next, c;
575 int token;
577 top:
578 p = buf;
579 while ((c = lgetc(0)) == ' ' || c == '\t')
580 ; /* nothing */
582 yylval.lineno = file->lineno;
583 if (c == '#')
584 while ((c = lgetc(0)) != '\n' && c != EOF)
585 ; /* nothing */
586 if (c == '$' && !expanding) {
587 while (1) {
588 if ((c = lgetc(0)) == EOF)
589 return 0;
590 if (p + 1 >= buf + sizeof(buf) -1) {
591 yyerror("string too long");
592 return findeol();
594 if (isalnum(c) || c == '_') {
595 *p++ = c;
596 continue;
598 *p = '\0';
599 lungetc(c);
600 break;
602 val = symget(buf);
603 if (val == NULL) {
604 yyerror("macro `%s' not defined", buf);
605 return findeol();
607 yylval.v.string = xstrdup(val);
608 return STRING;
610 if (c == '@' && !expanding) {
611 while (1) {
612 if ((c = lgetc(0)) == EOF)
613 return 0;
615 if (p + 1 >= buf + sizeof(buf) - 1) {
616 yyerror("string too long");
617 return findeol();
619 if (isalnum(c) || c == '_') {
620 *p++ = c;
621 continue;
623 *p = '\0';
624 lungetc(c);
625 break;
627 val = symget(buf);
628 if (val == NULL) {
629 yyerror("macro '%s' not defined", buf);
630 return findeol();
632 p = val + strlen(val) - 1;
633 lungetc(DONE_EXPAND);
634 while (p >= val) {
635 lungetc(*p);
636 p--;
638 lungetc(START_EXPAND);
639 goto top;
642 switch (c) {
643 case '\'':
644 case '"':
645 quotec = c;
646 while (1) {
647 if ((c = lgetc(quotec)) == EOF)
648 return 0;
649 if (c == '\n') {
650 file->lineno++;
651 continue;
652 } else if (c == '\\') {
653 if ((next = lgetc(quotec)) == EOF)
654 return (0);
655 if (next == quotec || next == ' ' ||
656 next == '\t')
657 c = next;
658 else if (next == '\n') {
659 file->lineno++;
660 continue;
661 } else
662 lungetc(next);
663 } else if (c == quotec) {
664 *p = '\0';
665 break;
666 } else if (c == '\0') {
667 yyerror("invalid syntax");
668 return findeol();
670 if (p + 1 >= buf + sizeof(buf) - 1) {
671 yyerror("string too long");
672 return findeol();
674 *p++ = c;
676 yylval.v.string = strdup(buf);
677 if (yylval.v.string == NULL)
678 err(1, "yylex: strdup");
679 return STRING;
682 #define allowed_to_end_number(x) \
683 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
685 if (c == '-' || isdigit(c)) {
686 do {
687 *p++ = c;
688 if ((size_t)(p-buf) >= sizeof(buf)) {
689 yyerror("string too long");
690 return findeol();
692 } while ((c = lgetc(0)) != EOF && isdigit(c));
693 lungetc(c);
694 if (p == buf + 1 && buf[0] == '-')
695 goto nodigits;
696 if (c == EOF || allowed_to_end_number(c)) {
697 const char *errstr = NULL;
699 *p = '\0';
700 yylval.v.number = strtonum(buf, LLONG_MIN,
701 LLONG_MAX, &errstr);
702 if (errstr) {
703 yyerror("\"%s\" invalid number: %s",
704 buf, errstr);
705 return findeol();
707 return NUM;
708 } else {
709 nodigits:
710 while (p > buf + 1)
711 lungetc(*--p);
712 c = *--p;
713 if (c == '-')
714 return c;
718 #define allowed_in_string(x) \
719 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
720 x != '{' && x != '}' && \
721 x != '!' && x != '=' && x != '#' && \
722 x != ',' && x != ';'))
724 if (isalnum(c) || c == ':' || c == '_') {
725 do {
726 *p++ = c;
727 if ((size_t)(p-buf) >= sizeof(buf)) {
728 yyerror("string too long");
729 return findeol();
731 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
732 lungetc(c);
733 *p = '\0';
734 if ((token = lookup(buf)) == STRING)
735 yylval.v.string = xstrdup(buf);
736 return token;
738 if (c == '\n') {
739 yylval.lineno = file->lineno;
740 file->lineno++;
742 if (c == EOF)
743 return 0;
744 return c;
747 struct file *
748 pushfile(const char *name, int secret)
750 struct file *nfile;
752 nfile = xcalloc(1, sizeof(*nfile));
753 nfile->name = xstrdup(name);
754 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
755 log_warn(NULL, "can't open %s: %s", nfile->name,
756 strerror(errno));
757 free(nfile->name);
758 free(nfile);
759 return NULL;
761 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
762 nfile->ungetsize = 16;
763 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
764 TAILQ_INSERT_TAIL(&files, nfile, entry);
765 return nfile;
768 int
769 popfile(void)
771 struct file *prev;
773 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
774 prev->errors += file->errors;
776 TAILQ_REMOVE(&files, file, entry);
777 fclose(file->stream);
778 free(file->name);
779 free(file->ungetbuf);
780 free(file);
781 file = prev;
782 return file ? 0 : EOF;
785 void
786 parse_conf(const char *filename)
788 struct sym *sym, *next;
790 file = pushfile(filename, 0);
791 if (file == NULL)
792 exit(1);
793 topfile = file;
795 yyparse();
796 errors = file->errors;
797 popfile();
799 /* Free macros and check which have not been used. */
800 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
801 /* TODO: warn if !sym->used */
802 if (!sym->persist) {
803 free(sym->name);
804 free(sym->val);
805 TAILQ_REMOVE(&symhead, sym, entry);
806 free(sym);
810 if (errors)
811 exit(1);
814 int
815 symset(const char *name, const char *val, int persist)
817 struct sym *sym;
819 TAILQ_FOREACH(sym, &symhead, entry) {
820 if (!strcmp(name, sym->name))
821 break;
824 if (sym != NULL) {
825 if (sym->persist)
826 return 0;
827 else {
828 free(sym->name);
829 free(sym->val);
830 TAILQ_REMOVE(&symhead, sym, entry);
831 free(sym);
835 sym = xcalloc(1, sizeof(*sym));
836 sym->name = xstrdup(name);
837 sym->val = xstrdup(val);
838 sym->used = 0;
839 sym->persist = persist;
841 TAILQ_INSERT_TAIL(&symhead, sym, entry);
842 return 0;
845 int
846 cmdline_symset(char *s)
848 char *sym, *val;
849 int ret;
851 if ((val = strrchr(s, '=')) == NULL)
852 return -1;
853 sym = xcalloc(1, val - s + 1);
854 memcpy(sym, s, val - s);
855 ret = symset(sym, val + 1, 1);
856 free(sym);
857 return ret;
860 char *
861 symget(const char *nam)
863 struct sym *sym;
865 TAILQ_FOREACH(sym, &symhead, entry) {
866 if (strcmp(nam, sym->name) == 0) {
867 sym->used = 1;
868 return sym->val;
871 return NULL;
874 struct vhost *
875 new_vhost(void)
877 return xcalloc(1, sizeof(struct vhost));
880 struct location *
881 new_location(void)
883 struct location *l;
885 l = xcalloc(1, sizeof(*l));
886 l->dirfd = -1;
887 l->fcgi = -1;
888 return l;
891 char *
892 ensure_absolute_path(char *path)
894 if (path == NULL || *path != '/')
895 yyerror("not an absolute path: %s", path);
896 return path;
899 int
900 check_block_code(int n)
902 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
903 yyerror("invalid block code %d", n);
904 return n;
907 char *
908 check_block_fmt(char *fmt)
910 char *s;
912 for (s = fmt; *s; ++s) {
913 if (*s != '%')
914 continue;
915 switch (*++s) {
916 case '%':
917 case 'p':
918 case 'q':
919 case 'P':
920 case 'N':
921 break;
922 default:
923 yyerror("invalid format specifier %%%c", *s);
927 return fmt;
930 int
931 check_strip_no(int n)
933 if (n <= 0)
934 yyerror("invalid strip number %d", n);
935 return n;
938 int
939 check_port_num(int n)
941 if (n <= 0 || n >= UINT16_MAX)
942 yyerror("port number is %s: %d",
943 n <= 0 ? "too small" : "too large",
944 n);
945 return n;
948 int
949 check_prefork_num(int n)
951 if (n <= 0 || n >= PROC_MAX)
952 yyerror("invalid prefork number %d", n);
953 return n;
956 void
957 advance_loc(void)
959 loc = new_location();
960 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
963 void
964 only_once(const void *ptr, const char *name)
966 if (ptr != NULL)
967 yyerror("`%s' specified more than once", name);
970 void
971 only_oncei(int i, const char *name)
973 if (i != -1)
974 yyerror("`%s' specified more than once", name);
977 int
978 fastcgi_conf(char *path, char *port, char *prog)
980 struct fcgi *f;
981 int i;
983 for (i = 0; i < FCGI_MAX; ++i) {
984 f = &fcgi[i];
986 if (f->path == NULL) {
987 f->id = i;
988 f->path = path;
989 f->port = port;
990 f->prog = prog;
991 return i;
994 /* XXX: what to do with prog? */
995 if (!strcmp(f->path, path) &&
996 ((port == NULL && f->port == NULL) ||
997 !strcmp(f->port, port))) {
998 free(path);
999 free(port);
1000 return i;
1004 yyerror("too much `fastcgi' rules defined.");
1005 return -1;
1008 void
1009 add_param(char *name, char *val, int env)
1011 struct envlist *e;
1012 struct envhead *h;
1014 if (env)
1015 h = &host->env;
1016 else
1017 h = &host->params;
1019 e = xcalloc(1, sizeof(*e));
1020 e->name = name;
1021 e->value = val;
1022 if (TAILQ_EMPTY(h))
1023 TAILQ_INSERT_HEAD(h, e, envs);
1024 else
1025 TAILQ_INSERT_TAIL(h, e, envs);