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 INCLUDE
114 %token TALIAS TAUTO
115 %token TBLOCK
116 %token TCA TCERT TCGI TCHROOT TCLIENT
117 %token TDEFAULT
118 %token TENTRYPOINT TENV
119 %token TFASTCGI
120 %token TINDEX TIPV6
121 %token TKEY
122 %token TLANG TLOCATION TLOG
123 %token TMAP TMIME
124 %token TOFF TON
125 %token TPARAM TPORT TPREFORK TPROTOCOLS
126 %token TREQUIRE TRETURN TROOT
127 %token TSERVER TSPAWN TSTRIP
128 %token TTCP TTOEXT TTYPE TUSER
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 : TON { $$ = 1; }
165 | TOFF { $$ = 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 : TCHROOT string { conf.chroot = $2; }
199 | TIPV6 bool { conf.ipv6 = $2; }
200 | TMIME 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 | TMAP string TTOEXT string { add_mime(&conf.mime, $2, $4); }
207 | TPORT NUM { conf.port = check_port_num($2); }
208 | TPREFORK NUM { conf.prefork = check_prefork_num($2); }
209 | TPROTOCOLS string {
210 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
211 yyerror("invalid protocols string \"%s\"", $2);
213 | TUSER string { conf.user = $2; }
216 vhost : TSERVER 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 : TALIAS 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 | TCERT string {
252 only_once(host->cert, "cert");
253 host->cert = ensure_absolute_path($2);
255 | TCGI 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 | TENTRYPOINT string {
263 only_once(host->entrypoint, "entrypoint");
264 while (*$2 == '/')
265 memmove($2, $2+1, strlen($2));
266 host->entrypoint = $2;
268 | TENV string '=' string {
269 add_param($2, $4, 1);
271 | TKEY string {
272 only_once(host->key, "key");
273 host->key = ensure_absolute_path($2);
275 | TPARAM string '=' string {
276 add_param($2, $4, 0);
278 | locopt
281 locations : /* empty */
282 | locations location optnl
285 location : TLOCATION { advance_loc(); } string '{' optnl locopts '}' {
286 /* drop the starting '/' if any */
287 if (*$3 == '/')
288 memmove($3, $3+1, strlen($3));
289 loc->match = $3;
291 | error '}'
294 locopts : /* empty */
295 | locopts locopt optnl
298 locopt : TAUTO TINDEX bool { loc->auto_index = $3 ? 1 : -1; }
299 | TBLOCK TRETURN NUM string {
300 only_once(loc->block_fmt, "block");
301 loc->block_fmt = check_block_fmt($4);
302 loc->block_code = check_block_code($3);
304 | TBLOCK TRETURN NUM {
305 only_once(loc->block_fmt, "block");
306 loc->block_fmt = xstrdup("temporary failure");
307 loc->block_code = check_block_code($3);
308 if ($3 >= 30 && $3 < 40)
309 yyerror("missing `meta' for block return %d", $3);
311 | TBLOCK {
312 only_once(loc->block_fmt, "block");
313 loc->block_fmt = xstrdup("temporary failure");
314 loc->block_code = 40;
316 | TDEFAULT TTYPE string {
317 only_once(loc->default_mime, "default type");
318 loc->default_mime = $3;
320 | TFASTCGI fastcgi
321 | TINDEX string {
322 only_once(loc->index, "index");
323 loc->index = $2;
325 | TLANG string {
326 only_once(loc->lang, "lang");
327 loc->lang = $2;
329 | TLOG bool { loc->disable_log = !$2; }
330 | TREQUIRE TCLIENT TCA string {
331 only_once(loc->reqca, "require client ca");
332 ensure_absolute_path($4);
333 if ((loc->reqca = load_ca($4)) == NULL)
334 yyerror("couldn't load ca cert: %s", $4);
335 free($4);
337 | TROOT string {
338 only_once(loc->dir, "root");
339 loc->dir = ensure_absolute_path($2);
341 | TSTRIP NUM { loc->strip = check_strip_no($2); }
344 fastcgi : TSPAWN string {
345 only_oncei(loc->fcgi, "fastcgi");
346 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
348 | string {
349 only_oncei(loc->fcgi, "fastcgi");
350 loc->fcgi = fastcgi_conf($1, NULL, NULL);
352 | TTCP string TPORT NUM {
353 char *c;
354 if (asprintf(&c, "%d", $4) == -1)
355 err(1, "asprintf");
356 only_oncei(loc->fcgi, "fastcgi");
357 loc->fcgi = fastcgi_conf($2, c, NULL);
359 | TTCP string {
360 only_oncei(loc->fcgi, "fastcgi");
361 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
363 | TTCP string TPORT string {
364 only_oncei(loc->fcgi, "fastcgi");
365 loc->fcgi = fastcgi_conf($2, $4, NULL);
369 optnl : '\n' optnl /* zero or more newlines */
370 | ';' optnl /* semicolons too */
371 | /*empty*/
374 %%
376 static struct keyword {
377 const char *word;
378 int token;
379 } keywords[] = {
380 /* these MUST be sorted */
381 {"alias", TALIAS},
382 {"auto", TAUTO},
383 {"block", TBLOCK},
384 {"ca", TCA},
385 {"cert", TCERT},
386 {"cgi", TCGI},
387 {"chroot", TCHROOT},
388 {"client", TCLIENT},
389 {"default", TDEFAULT},
390 {"entrypoint", TENTRYPOINT},
391 {"env", TENV},
392 {"fastcgi", TFASTCGI},
393 {"index", TINDEX},
394 {"ipv6", TIPV6},
395 {"key", TKEY},
396 {"lang", TLANG},
397 {"location", TLOCATION},
398 {"log", TLOG},
399 {"map", TMAP},
400 {"mime", TMIME},
401 {"off", TOFF},
402 {"on", TON},
403 {"param", TPARAM},
404 {"port", TPORT},
405 {"prefork", TPREFORK},
406 {"protocols", TPROTOCOLS},
407 {"require", TREQUIRE},
408 {"return", TRETURN},
409 {"root", TROOT},
410 {"server", TSERVER},
411 {"spawn", TSPAWN},
412 {"strip", TSTRIP},
413 {"tcp", TTCP},
414 {"to-ext", TTOEXT},
415 {"type", TTYPE},
416 {"user", TUSER},
417 };
419 void
420 yyerror(const char *msg, ...)
422 va_list ap;
424 file->errors++;
426 va_start(ap, msg);
427 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
428 vfprintf(stderr, msg, ap);
429 fprintf(stderr, "\n");
430 va_end(ap);
433 void
434 yywarn(const char *msg, ...)
436 va_list ap;
438 va_start(ap, msg);
439 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
440 vfprintf(stderr, msg, ap);
441 fprintf(stderr, "\n");
442 va_end(ap);
445 int
446 kw_cmp(const void *k, const void *e)
448 return strcmp(k, ((struct keyword *)e)->word);
451 int
452 lookup(char *s)
454 const struct keyword *p;
456 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
457 sizeof(keywords[0]), kw_cmp);
459 if (p)
460 return p->token;
461 else
462 return STRING;
465 #define START_EXPAND 1
466 #define DONE_EXPAND 2
468 static int expanding;
470 int
471 igetc(void)
473 int c;
475 while (1) {
476 if (file->ungetpos > 0)
477 c = file->ungetbuf[--file->ungetpos];
478 else
479 c = getc(file->stream);
481 if (c == START_EXPAND)
482 expanding = 1;
483 else if (c == DONE_EXPAND)
484 expanding = 0;
485 else
486 break;
488 return c;
491 int
492 lgetc(int quotec)
494 int c, next;
496 if (quotec) {
497 if ((c = igetc()) == EOF) {
498 yyerror("reached end of file while parsing "
499 "quoted string");
500 if (file == topfile || popfile() == EOF)
501 return EOF;
502 return quotec;
504 return c;
507 while ((c = igetc()) == '\\') {
508 next = igetc();
509 if (next != '\n') {
510 c = next;
511 break;
513 yylval.lineno = file->lineno;
514 file->lineno++;
517 if (c == EOF) {
518 /*
519 * Fake EOL when hit EOF for the first time. This gets line
520 * count right if last line in included file is syntactically
521 * invalid and has no newline.
522 */
523 if (file->eof_reached == 0) {
524 file->eof_reached = 1;
525 return '\n';
527 while (c == EOF) {
528 if (file == topfile || popfile() == EOF)
529 return EOF;
530 c = igetc();
533 return c;
536 void
537 lungetc(int c)
539 if (c == EOF)
540 return;
542 if (file->ungetpos >= file->ungetsize) {
543 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
544 if (p == NULL)
545 err(1, "lungetc");
546 file->ungetbuf = p;
547 file->ungetsize *= 2;
549 file->ungetbuf[file->ungetpos++] = c;
552 int
553 findeol(void)
555 int c;
557 /* Skip to either EOF or the first real EOL. */
558 while (1) {
559 c = lgetc(0);
560 if (c == '\n') {
561 file->lineno++;
562 break;
564 if (c == EOF)
565 break;
567 return ERROR;
570 int
571 yylex(void)
573 char buf[8096];
574 char *p, *val;
575 int quotec, next, c;
576 int token;
578 top:
579 p = buf;
580 while ((c = lgetc(0)) == ' ' || c == '\t')
581 ; /* nothing */
583 yylval.lineno = file->lineno;
584 if (c == '#')
585 while ((c = lgetc(0)) != '\n' && c != EOF)
586 ; /* nothing */
587 if (c == '$' && !expanding) {
588 while (1) {
589 if ((c = lgetc(0)) == EOF)
590 return 0;
591 if (p + 1 >= buf + sizeof(buf) -1) {
592 yyerror("string too long");
593 return findeol();
595 if (isalnum(c) || c == '_') {
596 *p++ = c;
597 continue;
599 *p = '\0';
600 lungetc(c);
601 break;
603 val = symget(buf);
604 if (val == NULL) {
605 yyerror("macro `%s' not defined", buf);
606 return findeol();
608 yylval.v.string = xstrdup(val);
609 return STRING;
611 if (c == '@' && !expanding) {
612 while (1) {
613 if ((c = lgetc(0)) == EOF)
614 return 0;
616 if (p + 1 >= buf + sizeof(buf) - 1) {
617 yyerror("string too long");
618 return findeol();
620 if (isalnum(c) || c == '_') {
621 *p++ = c;
622 continue;
624 *p = '\0';
625 lungetc(c);
626 break;
628 val = symget(buf);
629 if (val == NULL) {
630 yyerror("macro '%s' not defined", buf);
631 return findeol();
633 p = val + strlen(val) - 1;
634 lungetc(DONE_EXPAND);
635 while (p >= val) {
636 lungetc(*p);
637 p--;
639 lungetc(START_EXPAND);
640 goto top;
643 switch (c) {
644 case '\'':
645 case '"':
646 quotec = c;
647 while (1) {
648 if ((c = lgetc(quotec)) == EOF)
649 return 0;
650 if (c == '\n') {
651 file->lineno++;
652 continue;
653 } else if (c == '\\') {
654 if ((next = lgetc(quotec)) == EOF)
655 return (0);
656 if (next == quotec || next == ' ' ||
657 next == '\t')
658 c = next;
659 else if (next == '\n') {
660 file->lineno++;
661 continue;
662 } else
663 lungetc(next);
664 } else if (c == quotec) {
665 *p = '\0';
666 break;
667 } else if (c == '\0') {
668 yyerror("invalid syntax");
669 return findeol();
671 if (p + 1 >= buf + sizeof(buf) - 1) {
672 yyerror("string too long");
673 return findeol();
675 *p++ = c;
677 yylval.v.string = strdup(buf);
678 if (yylval.v.string == NULL)
679 err(1, "yylex: strdup");
680 return STRING;
683 #define allowed_to_end_number(x) \
684 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
686 if (c == '-' || isdigit(c)) {
687 do {
688 *p++ = c;
689 if ((size_t)(p-buf) >= sizeof(buf)) {
690 yyerror("string too long");
691 return findeol();
693 } while ((c = lgetc(0)) != EOF && isdigit(c));
694 lungetc(c);
695 if (p == buf + 1 && buf[0] == '-')
696 goto nodigits;
697 if (c == EOF || allowed_to_end_number(c)) {
698 const char *errstr = NULL;
700 *p = '\0';
701 yylval.v.number = strtonum(buf, LLONG_MIN,
702 LLONG_MAX, &errstr);
703 if (errstr) {
704 yyerror("\"%s\" invalid number: %s",
705 buf, errstr);
706 return findeol();
708 return NUM;
709 } else {
710 nodigits:
711 while (p > buf + 1)
712 lungetc(*--p);
713 c = *--p;
714 if (c == '-')
715 return c;
719 #define allowed_in_string(x) \
720 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
721 x != '{' && x != '}' && \
722 x != '!' && x != '=' && x != '#' && \
723 x != ',' && x != ';'))
725 if (isalnum(c) || c == ':' || c == '_') {
726 do {
727 *p++ = c;
728 if ((size_t)(p-buf) >= sizeof(buf)) {
729 yyerror("string too long");
730 return findeol();
732 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
733 lungetc(c);
734 *p = '\0';
735 if ((token = lookup(buf)) == STRING)
736 yylval.v.string = xstrdup(buf);
737 return token;
739 if (c == '\n') {
740 yylval.lineno = file->lineno;
741 file->lineno++;
743 if (c == EOF)
744 return 0;
745 return c;
748 struct file *
749 pushfile(const char *name, int secret)
751 struct file *nfile;
753 nfile = xcalloc(1, sizeof(*nfile));
754 nfile->name = xstrdup(name);
755 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
756 yyerror("can't open %s: %s", nfile->name,
757 strerror(errno));
758 free(nfile->name);
759 free(nfile);
760 return NULL;
762 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
763 nfile->ungetsize = 16;
764 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
765 TAILQ_INSERT_TAIL(&files, nfile, entry);
766 return nfile;
769 int
770 popfile(void)
772 struct file *prev;
774 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
775 prev->errors += file->errors;
777 TAILQ_REMOVE(&files, file, entry);
778 fclose(file->stream);
779 free(file->name);
780 free(file->ungetbuf);
781 free(file);
782 file = prev;
783 return file ? 0 : EOF;
786 void
787 parse_conf(const char *filename)
789 struct sym *sym, *next;
791 file = pushfile(filename, 0);
792 if (file == NULL)
793 return;
794 topfile = file;
796 yyparse();
797 errors = file->errors;
798 popfile();
800 /* Free macros and check which have not been used. */
801 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
802 /* TODO: warn if !sym->used */
803 if (!sym->persist) {
804 free(sym->name);
805 free(sym->val);
806 TAILQ_REMOVE(&symhead, sym, entry);
807 free(sym);
811 if (errors)
812 exit(1);
815 int
816 symset(const char *name, const char *val, int persist)
818 struct sym *sym;
820 TAILQ_FOREACH(sym, &symhead, entry) {
821 if (!strcmp(name, sym->name))
822 break;
825 if (sym != NULL) {
826 if (sym->persist)
827 return 0;
828 else {
829 free(sym->name);
830 free(sym->val);
831 TAILQ_REMOVE(&symhead, sym, entry);
832 free(sym);
836 sym = xcalloc(1, sizeof(*sym));
837 sym->name = xstrdup(name);
838 sym->val = xstrdup(val);
839 sym->used = 0;
840 sym->persist = persist;
842 TAILQ_INSERT_TAIL(&symhead, sym, entry);
843 return 0;
846 int
847 cmdline_symset(char *s)
849 char *sym, *val;
850 int ret;
852 if ((val = strrchr(s, '=')) == NULL)
853 return -1;
854 sym = xcalloc(1, val - s + 1);
855 memcpy(sym, s, val - s);
856 ret = symset(sym, val + 1, 1);
857 free(sym);
858 return ret;
861 char *
862 symget(const char *nam)
864 struct sym *sym;
866 TAILQ_FOREACH(sym, &symhead, entry) {
867 if (strcmp(nam, sym->name) == 0) {
868 sym->used = 1;
869 return sym->val;
872 return NULL;
875 struct vhost *
876 new_vhost(void)
878 return xcalloc(1, sizeof(struct vhost));
881 struct location *
882 new_location(void)
884 struct location *l;
886 l = xcalloc(1, sizeof(*l));
887 l->dirfd = -1;
888 l->fcgi = -1;
889 return l;
892 char *
893 ensure_absolute_path(char *path)
895 if (path == NULL || *path != '/')
896 yyerror("not an absolute path: %s", path);
897 return path;
900 int
901 check_block_code(int n)
903 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
904 yyerror("invalid block code %d", n);
905 return n;
908 char *
909 check_block_fmt(char *fmt)
911 char *s;
913 for (s = fmt; *s; ++s) {
914 if (*s != '%')
915 continue;
916 switch (*++s) {
917 case '%':
918 case 'p':
919 case 'q':
920 case 'P':
921 case 'N':
922 break;
923 default:
924 yyerror("invalid format specifier %%%c", *s);
928 return fmt;
931 int
932 check_strip_no(int n)
934 if (n <= 0)
935 yyerror("invalid strip number %d", n);
936 return n;
939 int
940 check_port_num(int n)
942 if (n <= 0 || n >= UINT16_MAX)
943 yyerror("port number is %s: %d",
944 n <= 0 ? "too small" : "too large",
945 n);
946 return n;
949 int
950 check_prefork_num(int n)
952 if (n <= 0 || n >= PROC_MAX)
953 yyerror("invalid prefork number %d", n);
954 return n;
957 void
958 advance_loc(void)
960 loc = new_location();
961 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
964 void
965 only_once(const void *ptr, const char *name)
967 if (ptr != NULL)
968 yyerror("`%s' specified more than once", name);
971 void
972 only_oncei(int i, const char *name)
974 if (i != -1)
975 yyerror("`%s' specified more than once", name);
978 int
979 fastcgi_conf(char *path, char *port, char *prog)
981 struct fcgi *f;
982 int i;
984 for (i = 0; i < FCGI_MAX; ++i) {
985 f = &fcgi[i];
987 if (f->path == NULL) {
988 f->id = i;
989 f->path = path;
990 f->port = port;
991 f->prog = prog;
992 return i;
995 /* XXX: what to do with prog? */
996 if (!strcmp(f->path, path) &&
997 ((port == NULL && f->port == NULL) ||
998 !strcmp(f->port, port))) {
999 free(path);
1000 free(port);
1001 return i;
1005 yyerror("too much `fastcgi' rules defined.");
1006 return -1;
1009 void
1010 add_param(char *name, char *val, int env)
1012 struct envlist *e;
1013 struct envhead *h;
1015 if (env)
1016 h = &host->env;
1017 else
1018 h = &host->params;
1020 e = xcalloc(1, sizeof(*e));
1021 e->name = name;
1022 e->value = val;
1023 if (TAILQ_EMPTY(h))
1024 TAILQ_INSERT_HEAD(h, e, envs);
1025 else
1026 TAILQ_INSERT_TAIL(h, e, envs);