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
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 | 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 | REQUIRE CLIENT CA string {
334 only_once(loc->reqca, "require client ca");
335 ensure_absolute_path($4);
336 if ((loc->reqca = load_ca($4)) == NULL)
337 yyerror("couldn't load ca cert: %s", $4);
338 free($4);
340 | ROOT string {
341 only_once(loc->dir, "root");
342 loc->dir = ensure_absolute_path($2);
344 | STRIP NUM { loc->strip = check_strip_no($2); }
347 fastcgi : SPAWN string {
348 only_oncei(loc->fcgi, "fastcgi");
349 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
351 | string {
352 only_oncei(loc->fcgi, "fastcgi");
353 loc->fcgi = fastcgi_conf($1, NULL, NULL);
355 | TCP string PORT NUM {
356 char *c;
357 if (asprintf(&c, "%d", $4) == -1)
358 err(1, "asprintf");
359 only_oncei(loc->fcgi, "fastcgi");
360 loc->fcgi = fastcgi_conf($2, c, NULL);
362 | TCP string {
363 only_oncei(loc->fcgi, "fastcgi");
364 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
366 | TCP string PORT string {
367 only_oncei(loc->fcgi, "fastcgi");
368 loc->fcgi = fastcgi_conf($2, $4, NULL);
372 optnl : '\n' optnl /* zero or more newlines */
373 | ';' optnl /* semicolons too */
374 | /*empty*/
377 %%
379 static struct keyword {
380 const char *word;
381 int token;
382 } keywords[] = {
383 /* these MUST be sorted */
384 {"alias", ALIAS},
385 {"auto", AUTO},
386 {"block", BLOCK},
387 {"ca", CA},
388 {"cert", CERT},
389 {"cgi", CGI},
390 {"chroot", CHROOT},
391 {"client", CLIENT},
392 {"default", DEFAULT},
393 {"entrypoint", ENTRYPOINT},
394 {"env", ENV},
395 {"fastcgi", FASTCGI},
396 {"index", INDEX},
397 {"ipv6", IPV6},
398 {"key", KEY},
399 {"lang", LANG},
400 {"location", LOCATION},
401 {"log", LOG},
402 {"map", MAP},
403 {"mime", MIME},
404 {"ocsp", OCSP},
405 {"off", OFF},
406 {"on", ON},
407 {"param", PARAM},
408 {"port", PORT},
409 {"prefork", PREFORK},
410 {"protocols", PROTOCOLS},
411 {"require", REQUIRE},
412 {"return", RETURN},
413 {"root", ROOT},
414 {"server", SERVER},
415 {"spawn", SPAWN},
416 {"strip", STRIP},
417 {"tcp", TCP},
418 {"to-ext", TOEXT},
419 {"type", TYPE},
420 {"user", USER},
421 };
423 void
424 yyerror(const char *msg, ...)
426 va_list ap;
428 file->errors++;
430 va_start(ap, msg);
431 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
432 vfprintf(stderr, msg, ap);
433 fprintf(stderr, "\n");
434 va_end(ap);
437 void
438 yywarn(const char *msg, ...)
440 va_list ap;
442 va_start(ap, msg);
443 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
444 vfprintf(stderr, msg, ap);
445 fprintf(stderr, "\n");
446 va_end(ap);
449 int
450 kw_cmp(const void *k, const void *e)
452 return strcmp(k, ((struct keyword *)e)->word);
455 int
456 lookup(char *s)
458 const struct keyword *p;
460 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
461 sizeof(keywords[0]), kw_cmp);
463 if (p)
464 return p->token;
465 else
466 return STRING;
469 #define START_EXPAND 1
470 #define DONE_EXPAND 2
472 static int expanding;
474 int
475 igetc(void)
477 int c;
479 while (1) {
480 if (file->ungetpos > 0)
481 c = file->ungetbuf[--file->ungetpos];
482 else
483 c = getc(file->stream);
485 if (c == START_EXPAND)
486 expanding = 1;
487 else if (c == DONE_EXPAND)
488 expanding = 0;
489 else
490 break;
492 return c;
495 int
496 lgetc(int quotec)
498 int c, next;
500 if (quotec) {
501 if ((c = igetc()) == EOF) {
502 yyerror("reached end of file while parsing "
503 "quoted string");
504 if (file == topfile || popfile() == EOF)
505 return EOF;
506 return quotec;
508 return c;
511 while ((c = igetc()) == '\\') {
512 next = igetc();
513 if (next != '\n') {
514 c = next;
515 break;
517 yylval.lineno = file->lineno;
518 file->lineno++;
521 if (c == EOF) {
522 /*
523 * Fake EOL when hit EOF for the first time. This gets line
524 * count right if last line in included file is syntactically
525 * invalid and has no newline.
526 */
527 if (file->eof_reached == 0) {
528 file->eof_reached = 1;
529 return '\n';
531 while (c == EOF) {
532 if (file == topfile || popfile() == EOF)
533 return EOF;
534 c = igetc();
537 return c;
540 void
541 lungetc(int c)
543 if (c == EOF)
544 return;
546 if (file->ungetpos >= file->ungetsize) {
547 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
548 if (p == NULL)
549 err(1, "lungetc");
550 file->ungetbuf = p;
551 file->ungetsize *= 2;
553 file->ungetbuf[file->ungetpos++] = c;
556 int
557 findeol(void)
559 int c;
561 /* Skip to either EOF or the first real EOL. */
562 while (1) {
563 c = lgetc(0);
564 if (c == '\n') {
565 file->lineno++;
566 break;
568 if (c == EOF)
569 break;
571 return ERROR;
574 int
575 yylex(void)
577 char buf[8096];
578 char *p, *val;
579 int quotec, next, c;
580 int token;
582 top:
583 p = buf;
584 while ((c = lgetc(0)) == ' ' || c == '\t')
585 ; /* nothing */
587 yylval.lineno = file->lineno;
588 if (c == '#')
589 while ((c = lgetc(0)) != '\n' && c != EOF)
590 ; /* nothing */
591 if (c == '$' && !expanding) {
592 while (1) {
593 if ((c = lgetc(0)) == EOF)
594 return 0;
595 if (p + 1 >= buf + sizeof(buf) -1) {
596 yyerror("string too long");
597 return findeol();
599 if (isalnum(c) || c == '_') {
600 *p++ = c;
601 continue;
603 *p = '\0';
604 lungetc(c);
605 break;
607 val = symget(buf);
608 if (val == NULL) {
609 yyerror("macro `%s' not defined", buf);
610 return findeol();
612 yylval.v.string = xstrdup(val);
613 return STRING;
615 if (c == '@' && !expanding) {
616 while (1) {
617 if ((c = lgetc(0)) == EOF)
618 return 0;
620 if (p + 1 >= buf + sizeof(buf) - 1) {
621 yyerror("string too long");
622 return findeol();
624 if (isalnum(c) || c == '_') {
625 *p++ = c;
626 continue;
628 *p = '\0';
629 lungetc(c);
630 break;
632 val = symget(buf);
633 if (val == NULL) {
634 yyerror("macro '%s' not defined", buf);
635 return findeol();
637 p = val + strlen(val) - 1;
638 lungetc(DONE_EXPAND);
639 while (p >= val) {
640 lungetc(*p);
641 p--;
643 lungetc(START_EXPAND);
644 goto top;
647 switch (c) {
648 case '\'':
649 case '"':
650 quotec = c;
651 while (1) {
652 if ((c = lgetc(quotec)) == EOF)
653 return 0;
654 if (c == '\n') {
655 file->lineno++;
656 continue;
657 } else if (c == '\\') {
658 if ((next = lgetc(quotec)) == EOF)
659 return (0);
660 if (next == quotec || next == ' ' ||
661 next == '\t')
662 c = next;
663 else if (next == '\n') {
664 file->lineno++;
665 continue;
666 } else
667 lungetc(next);
668 } else if (c == quotec) {
669 *p = '\0';
670 break;
671 } else if (c == '\0') {
672 yyerror("invalid syntax");
673 return findeol();
675 if (p + 1 >= buf + sizeof(buf) - 1) {
676 yyerror("string too long");
677 return findeol();
679 *p++ = c;
681 yylval.v.string = strdup(buf);
682 if (yylval.v.string == NULL)
683 err(1, "yylex: strdup");
684 return STRING;
687 #define allowed_to_end_number(x) \
688 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
690 if (c == '-' || isdigit(c)) {
691 do {
692 *p++ = c;
693 if ((size_t)(p-buf) >= sizeof(buf)) {
694 yyerror("string too long");
695 return findeol();
697 } while ((c = lgetc(0)) != EOF && isdigit(c));
698 lungetc(c);
699 if (p == buf + 1 && buf[0] == '-')
700 goto nodigits;
701 if (c == EOF || allowed_to_end_number(c)) {
702 const char *errstr = NULL;
704 *p = '\0';
705 yylval.v.number = strtonum(buf, LLONG_MIN,
706 LLONG_MAX, &errstr);
707 if (errstr) {
708 yyerror("\"%s\" invalid number: %s",
709 buf, errstr);
710 return findeol();
712 return NUM;
713 } else {
714 nodigits:
715 while (p > buf + 1)
716 lungetc(*--p);
717 c = *--p;
718 if (c == '-')
719 return c;
723 #define allowed_in_string(x) \
724 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
725 x != '{' && x != '}' && \
726 x != '!' && x != '=' && x != '#' && \
727 x != ',' && x != ';'))
729 if (isalnum(c) || c == ':' || c == '_') {
730 do {
731 *p++ = c;
732 if ((size_t)(p-buf) >= sizeof(buf)) {
733 yyerror("string too long");
734 return findeol();
736 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
737 lungetc(c);
738 *p = '\0';
739 if ((token = lookup(buf)) == STRING)
740 yylval.v.string = xstrdup(buf);
741 return token;
743 if (c == '\n') {
744 yylval.lineno = file->lineno;
745 file->lineno++;
747 if (c == EOF)
748 return 0;
749 return c;
752 struct file *
753 pushfile(const char *name, int secret)
755 struct file *nfile;
757 nfile = xcalloc(1, sizeof(*nfile));
758 nfile->name = xstrdup(name);
759 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
760 log_warn(NULL, "can't open %s: %s", nfile->name,
761 strerror(errno));
762 free(nfile->name);
763 free(nfile);
764 return NULL;
766 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
767 nfile->ungetsize = 16;
768 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
769 TAILQ_INSERT_TAIL(&files, nfile, entry);
770 return nfile;
773 int
774 popfile(void)
776 struct file *prev;
778 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
779 prev->errors += file->errors;
781 TAILQ_REMOVE(&files, file, entry);
782 fclose(file->stream);
783 free(file->name);
784 free(file->ungetbuf);
785 free(file);
786 file = prev;
787 return file ? 0 : EOF;
790 void
791 parse_conf(const char *filename)
793 struct sym *sym, *next;
795 file = pushfile(filename, 0);
796 if (file == NULL)
797 exit(1);
798 topfile = file;
800 yyparse();
801 errors = file->errors;
802 popfile();
804 /* Free macros and check which have not been used. */
805 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
806 /* TODO: warn if !sym->used */
807 if (!sym->persist) {
808 free(sym->name);
809 free(sym->val);
810 TAILQ_REMOVE(&symhead, sym, entry);
811 free(sym);
815 if (errors)
816 exit(1);
819 void
820 print_conf(void)
822 struct vhost *h;
823 /* struct location *l; */
824 /* struct envlist *e; */
825 /* struct alist *a; */
827 if (conf.chroot != NULL)
828 printf("chroot \"%s\"\n", conf.chroot);
829 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
830 /* XXX: defined mimes? */
831 printf("port %d\n", conf.port);
832 printf("prefork %d\n", conf.prefork);
833 /* XXX: protocols? */
834 if (conf.user != NULL)
835 printf("user \"%s\"\n", conf.user);
837 TAILQ_FOREACH(h, &hosts, vhosts) {
838 printf("\nserver \"%s\" {\n", h->domain);
839 printf(" cert \"%s\"\n", h->cert);
840 printf(" key \"%s\"\n", h->key);
841 /* TODO: print locations... */
842 printf("}\n");
846 int
847 symset(const char *name, const char *val, int persist)
849 struct sym *sym;
851 TAILQ_FOREACH(sym, &symhead, entry) {
852 if (!strcmp(name, sym->name))
853 break;
856 if (sym != NULL) {
857 if (sym->persist)
858 return 0;
859 else {
860 free(sym->name);
861 free(sym->val);
862 TAILQ_REMOVE(&symhead, sym, entry);
863 free(sym);
867 sym = xcalloc(1, sizeof(*sym));
868 sym->name = xstrdup(name);
869 sym->val = xstrdup(val);
870 sym->used = 0;
871 sym->persist = persist;
873 TAILQ_INSERT_TAIL(&symhead, sym, entry);
874 return 0;
877 int
878 cmdline_symset(char *s)
880 char *sym, *val;
881 int ret;
883 if ((val = strrchr(s, '=')) == NULL)
884 return -1;
885 sym = xcalloc(1, val - s + 1);
886 memcpy(sym, s, val - s);
887 ret = symset(sym, val + 1, 1);
888 free(sym);
889 return ret;
892 char *
893 symget(const char *nam)
895 struct sym *sym;
897 TAILQ_FOREACH(sym, &symhead, entry) {
898 if (strcmp(nam, sym->name) == 0) {
899 sym->used = 1;
900 return sym->val;
903 return NULL;
906 struct vhost *
907 new_vhost(void)
909 return xcalloc(1, sizeof(struct vhost));
912 struct location *
913 new_location(void)
915 struct location *l;
917 l = xcalloc(1, sizeof(*l));
918 l->dirfd = -1;
919 l->fcgi = -1;
920 return l;
923 char *
924 ensure_absolute_path(char *path)
926 if (path == NULL || *path != '/')
927 yyerror("not an absolute path: %s", path);
928 return path;
931 int
932 check_block_code(int n)
934 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
935 yyerror("invalid block code %d", n);
936 return n;
939 char *
940 check_block_fmt(char *fmt)
942 char *s;
944 for (s = fmt; *s; ++s) {
945 if (*s != '%')
946 continue;
947 switch (*++s) {
948 case '%':
949 case 'p':
950 case 'q':
951 case 'P':
952 case 'N':
953 break;
954 default:
955 yyerror("invalid format specifier %%%c", *s);
959 return fmt;
962 int
963 check_strip_no(int n)
965 if (n <= 0)
966 yyerror("invalid strip number %d", n);
967 return n;
970 int
971 check_port_num(int n)
973 if (n <= 0 || n >= UINT16_MAX)
974 yyerror("port number is %s: %d",
975 n <= 0 ? "too small" : "too large",
976 n);
977 return n;
980 int
981 check_prefork_num(int n)
983 if (n <= 0 || n >= PROC_MAX)
984 yyerror("invalid prefork number %d", n);
985 return n;
988 void
989 advance_loc(void)
991 loc = new_location();
992 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
995 void
996 only_once(const void *ptr, const char *name)
998 if (ptr != NULL)
999 yyerror("`%s' specified more than once", name);
1002 void
1003 only_oncei(int i, const char *name)
1005 if (i != -1)
1006 yyerror("`%s' specified more than once", name);
1009 int
1010 fastcgi_conf(char *path, char *port, char *prog)
1012 struct fcgi *f;
1013 int i;
1015 for (i = 0; i < FCGI_MAX; ++i) {
1016 f = &fcgi[i];
1018 if (f->path == NULL) {
1019 f->id = i;
1020 f->path = path;
1021 f->port = port;
1022 f->prog = prog;
1023 return i;
1026 /* XXX: what to do with prog? */
1027 if (!strcmp(f->path, path) &&
1028 ((port == NULL && f->port == NULL) ||
1029 !strcmp(f->port, port))) {
1030 free(path);
1031 free(port);
1032 return i;
1036 yyerror("too much `fastcgi' rules defined.");
1037 return -1;
1040 void
1041 add_param(char *name, char *val, int env)
1043 struct envlist *e;
1044 struct envhead *h;
1046 if (env)
1047 h = &host->env;
1048 else
1049 h = &host->params;
1051 e = xcalloc(1, sizeof(*e));
1052 e->name = name;
1053 e->value = val;
1054 if (TAILQ_EMPTY(h))
1055 TAILQ_INSERT_HEAD(h, e, envs);
1056 else
1057 TAILQ_INSERT_TAIL(h, e, envs);