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 : RELAY_TO string {
357 char *at;
358 const char *errstr;
360 only_once(loc->proxy_host, "proxy relay-to");
361 loc->proxy_host = $2;
363 if ((at = strchr($2, ':')) != NULL) {
364 *at++ = '\0';
365 loc->proxy_port = at;
366 } else
367 loc->proxy_port = "1965";
369 strtonum(loc->proxy_port, 1, UINT16_MAX, &errstr);
370 if (errstr != NULL)
371 yyerror("proxy port is %s: %s", errstr,
372 loc->proxy_port);
376 fastcgi : SPAWN string {
377 only_oncei(loc->fcgi, "fastcgi");
378 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
380 | string {
381 only_oncei(loc->fcgi, "fastcgi");
382 loc->fcgi = fastcgi_conf($1, NULL, NULL);
384 | TCP string PORT NUM {
385 char *c;
386 if (asprintf(&c, "%d", $4) == -1)
387 err(1, "asprintf");
388 only_oncei(loc->fcgi, "fastcgi");
389 loc->fcgi = fastcgi_conf($2, c, NULL);
391 | TCP string {
392 only_oncei(loc->fcgi, "fastcgi");
393 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
395 | TCP string PORT string {
396 only_oncei(loc->fcgi, "fastcgi");
397 loc->fcgi = fastcgi_conf($2, $4, NULL);
401 optnl : '\n' optnl /* zero or more newlines */
402 | ';' optnl /* semicolons too */
403 | /*empty*/
406 %%
408 static struct keyword {
409 const char *word;
410 int token;
411 } keywords[] = {
412 /* these MUST be sorted */
413 {"alias", ALIAS},
414 {"auto", AUTO},
415 {"block", BLOCK},
416 {"ca", CA},
417 {"cert", CERT},
418 {"cgi", CGI},
419 {"chroot", CHROOT},
420 {"client", CLIENT},
421 {"default", DEFAULT},
422 {"entrypoint", ENTRYPOINT},
423 {"env", ENV},
424 {"fastcgi", FASTCGI},
425 {"index", INDEX},
426 {"ipv6", IPV6},
427 {"key", KEY},
428 {"lang", LANG},
429 {"location", LOCATION},
430 {"log", LOG},
431 {"map", MAP},
432 {"mime", MIME},
433 {"ocsp", OCSP},
434 {"off", OFF},
435 {"on", ON},
436 {"param", PARAM},
437 {"port", PORT},
438 {"prefork", PREFORK},
439 {"protocols", PROTOCOLS},
440 {"proxy", PROXY},
441 {"relay-to", RELAY_TO},
442 {"require", REQUIRE},
443 {"return", RETURN},
444 {"root", ROOT},
445 {"server", SERVER},
446 {"spawn", SPAWN},
447 {"strip", STRIP},
448 {"tcp", TCP},
449 {"to-ext", TOEXT},
450 {"type", TYPE},
451 {"user", USER},
452 };
454 void
455 yyerror(const char *msg, ...)
457 va_list ap;
459 file->errors++;
461 va_start(ap, msg);
462 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
463 vfprintf(stderr, msg, ap);
464 fprintf(stderr, "\n");
465 va_end(ap);
468 void
469 yywarn(const char *msg, ...)
471 va_list ap;
473 va_start(ap, msg);
474 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
475 vfprintf(stderr, msg, ap);
476 fprintf(stderr, "\n");
477 va_end(ap);
480 int
481 kw_cmp(const void *k, const void *e)
483 return strcmp(k, ((struct keyword *)e)->word);
486 int
487 lookup(char *s)
489 const struct keyword *p;
491 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
492 sizeof(keywords[0]), kw_cmp);
494 if (p)
495 return p->token;
496 else
497 return STRING;
500 #define START_EXPAND 1
501 #define DONE_EXPAND 2
503 static int expanding;
505 int
506 igetc(void)
508 int c;
510 while (1) {
511 if (file->ungetpos > 0)
512 c = file->ungetbuf[--file->ungetpos];
513 else
514 c = getc(file->stream);
516 if (c == START_EXPAND)
517 expanding = 1;
518 else if (c == DONE_EXPAND)
519 expanding = 0;
520 else
521 break;
523 return c;
526 int
527 lgetc(int quotec)
529 int c, next;
531 if (quotec) {
532 if ((c = igetc()) == EOF) {
533 yyerror("reached end of file while parsing "
534 "quoted string");
535 if (file == topfile || popfile() == EOF)
536 return EOF;
537 return quotec;
539 return c;
542 while ((c = igetc()) == '\\') {
543 next = igetc();
544 if (next != '\n') {
545 c = next;
546 break;
548 yylval.lineno = file->lineno;
549 file->lineno++;
552 if (c == EOF) {
553 /*
554 * Fake EOL when hit EOF for the first time. This gets line
555 * count right if last line in included file is syntactically
556 * invalid and has no newline.
557 */
558 if (file->eof_reached == 0) {
559 file->eof_reached = 1;
560 return '\n';
562 while (c == EOF) {
563 if (file == topfile || popfile() == EOF)
564 return EOF;
565 c = igetc();
568 return c;
571 void
572 lungetc(int c)
574 if (c == EOF)
575 return;
577 if (file->ungetpos >= file->ungetsize) {
578 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
579 if (p == NULL)
580 err(1, "lungetc");
581 file->ungetbuf = p;
582 file->ungetsize *= 2;
584 file->ungetbuf[file->ungetpos++] = c;
587 int
588 findeol(void)
590 int c;
592 /* Skip to either EOF or the first real EOL. */
593 while (1) {
594 c = lgetc(0);
595 if (c == '\n') {
596 file->lineno++;
597 break;
599 if (c == EOF)
600 break;
602 return ERROR;
605 int
606 yylex(void)
608 char buf[8096];
609 char *p, *val;
610 int quotec, next, c;
611 int token;
613 top:
614 p = buf;
615 while ((c = lgetc(0)) == ' ' || c == '\t')
616 ; /* nothing */
618 yylval.lineno = file->lineno;
619 if (c == '#')
620 while ((c = lgetc(0)) != '\n' && c != EOF)
621 ; /* nothing */
622 if (c == '$' && !expanding) {
623 while (1) {
624 if ((c = lgetc(0)) == EOF)
625 return 0;
626 if (p + 1 >= buf + sizeof(buf) -1) {
627 yyerror("string too long");
628 return findeol();
630 if (isalnum(c) || c == '_') {
631 *p++ = c;
632 continue;
634 *p = '\0';
635 lungetc(c);
636 break;
638 val = symget(buf);
639 if (val == NULL) {
640 yyerror("macro `%s' not defined", buf);
641 return findeol();
643 yylval.v.string = xstrdup(val);
644 return STRING;
646 if (c == '@' && !expanding) {
647 while (1) {
648 if ((c = lgetc(0)) == EOF)
649 return 0;
651 if (p + 1 >= buf + sizeof(buf) - 1) {
652 yyerror("string too long");
653 return findeol();
655 if (isalnum(c) || c == '_') {
656 *p++ = c;
657 continue;
659 *p = '\0';
660 lungetc(c);
661 break;
663 val = symget(buf);
664 if (val == NULL) {
665 yyerror("macro '%s' not defined", buf);
666 return findeol();
668 p = val + strlen(val) - 1;
669 lungetc(DONE_EXPAND);
670 while (p >= val) {
671 lungetc(*p);
672 p--;
674 lungetc(START_EXPAND);
675 goto top;
678 switch (c) {
679 case '\'':
680 case '"':
681 quotec = c;
682 while (1) {
683 if ((c = lgetc(quotec)) == EOF)
684 return 0;
685 if (c == '\n') {
686 file->lineno++;
687 continue;
688 } else if (c == '\\') {
689 if ((next = lgetc(quotec)) == EOF)
690 return (0);
691 if (next == quotec || next == ' ' ||
692 next == '\t')
693 c = next;
694 else if (next == '\n') {
695 file->lineno++;
696 continue;
697 } else
698 lungetc(next);
699 } else if (c == quotec) {
700 *p = '\0';
701 break;
702 } else if (c == '\0') {
703 yyerror("invalid syntax");
704 return findeol();
706 if (p + 1 >= buf + sizeof(buf) - 1) {
707 yyerror("string too long");
708 return findeol();
710 *p++ = c;
712 yylval.v.string = strdup(buf);
713 if (yylval.v.string == NULL)
714 err(1, "yylex: strdup");
715 return STRING;
718 #define allowed_to_end_number(x) \
719 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
721 if (c == '-' || isdigit(c)) {
722 do {
723 *p++ = c;
724 if ((size_t)(p-buf) >= sizeof(buf)) {
725 yyerror("string too long");
726 return findeol();
728 } while ((c = lgetc(0)) != EOF && isdigit(c));
729 lungetc(c);
730 if (p == buf + 1 && buf[0] == '-')
731 goto nodigits;
732 if (c == EOF || allowed_to_end_number(c)) {
733 const char *errstr = NULL;
735 *p = '\0';
736 yylval.v.number = strtonum(buf, LLONG_MIN,
737 LLONG_MAX, &errstr);
738 if (errstr) {
739 yyerror("\"%s\" invalid number: %s",
740 buf, errstr);
741 return findeol();
743 return NUM;
744 } else {
745 nodigits:
746 while (p > buf + 1)
747 lungetc(*--p);
748 c = *--p;
749 if (c == '-')
750 return c;
754 #define allowed_in_string(x) \
755 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
756 x != '{' && x != '}' && \
757 x != '!' && x != '=' && x != '#' && \
758 x != ',' && x != ';'))
760 if (isalnum(c) || c == ':' || c == '_') {
761 do {
762 *p++ = c;
763 if ((size_t)(p-buf) >= sizeof(buf)) {
764 yyerror("string too long");
765 return findeol();
767 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
768 lungetc(c);
769 *p = '\0';
770 if ((token = lookup(buf)) == STRING)
771 yylval.v.string = xstrdup(buf);
772 return token;
774 if (c == '\n') {
775 yylval.lineno = file->lineno;
776 file->lineno++;
778 if (c == EOF)
779 return 0;
780 return c;
783 struct file *
784 pushfile(const char *name, int secret)
786 struct file *nfile;
788 nfile = xcalloc(1, sizeof(*nfile));
789 nfile->name = xstrdup(name);
790 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
791 log_warn(NULL, "can't open %s: %s", nfile->name,
792 strerror(errno));
793 free(nfile->name);
794 free(nfile);
795 return NULL;
797 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
798 nfile->ungetsize = 16;
799 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
800 TAILQ_INSERT_TAIL(&files, nfile, entry);
801 return nfile;
804 int
805 popfile(void)
807 struct file *prev;
809 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
810 prev->errors += file->errors;
812 TAILQ_REMOVE(&files, file, entry);
813 fclose(file->stream);
814 free(file->name);
815 free(file->ungetbuf);
816 free(file);
817 file = prev;
818 return file ? 0 : EOF;
821 void
822 parse_conf(const char *filename)
824 struct sym *sym, *next;
826 file = pushfile(filename, 0);
827 if (file == NULL)
828 exit(1);
829 topfile = file;
831 yyparse();
832 errors = file->errors;
833 popfile();
835 /* Free macros and check which have not been used. */
836 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
837 /* TODO: warn if !sym->used */
838 if (!sym->persist) {
839 free(sym->name);
840 free(sym->val);
841 TAILQ_REMOVE(&symhead, sym, entry);
842 free(sym);
846 if (errors)
847 exit(1);
850 void
851 print_conf(void)
853 struct vhost *h;
854 /* struct location *l; */
855 /* struct envlist *e; */
856 /* struct alist *a; */
858 if (conf.chroot != NULL)
859 printf("chroot \"%s\"\n", conf.chroot);
860 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
861 /* XXX: defined mimes? */
862 printf("port %d\n", conf.port);
863 printf("prefork %d\n", conf.prefork);
864 /* XXX: protocols? */
865 if (conf.user != NULL)
866 printf("user \"%s\"\n", conf.user);
868 TAILQ_FOREACH(h, &hosts, vhosts) {
869 printf("\nserver \"%s\" {\n", h->domain);
870 printf(" cert \"%s\"\n", h->cert);
871 printf(" key \"%s\"\n", h->key);
872 /* TODO: print locations... */
873 printf("}\n");
877 int
878 symset(const char *name, const char *val, int persist)
880 struct sym *sym;
882 TAILQ_FOREACH(sym, &symhead, entry) {
883 if (!strcmp(name, sym->name))
884 break;
887 if (sym != NULL) {
888 if (sym->persist)
889 return 0;
890 else {
891 free(sym->name);
892 free(sym->val);
893 TAILQ_REMOVE(&symhead, sym, entry);
894 free(sym);
898 sym = xcalloc(1, sizeof(*sym));
899 sym->name = xstrdup(name);
900 sym->val = xstrdup(val);
901 sym->used = 0;
902 sym->persist = persist;
904 TAILQ_INSERT_TAIL(&symhead, sym, entry);
905 return 0;
908 int
909 cmdline_symset(char *s)
911 char *sym, *val;
912 int ret;
914 if ((val = strrchr(s, '=')) == NULL)
915 return -1;
916 sym = xcalloc(1, val - s + 1);
917 memcpy(sym, s, val - s);
918 ret = symset(sym, val + 1, 1);
919 free(sym);
920 return ret;
923 char *
924 symget(const char *nam)
926 struct sym *sym;
928 TAILQ_FOREACH(sym, &symhead, entry) {
929 if (strcmp(nam, sym->name) == 0) {
930 sym->used = 1;
931 return sym->val;
934 return NULL;
937 struct vhost *
938 new_vhost(void)
940 return xcalloc(1, sizeof(struct vhost));
943 struct location *
944 new_location(void)
946 struct location *l;
948 l = xcalloc(1, sizeof(*l));
949 l->dirfd = -1;
950 l->fcgi = -1;
951 return l;
954 char *
955 ensure_absolute_path(char *path)
957 if (path == NULL || *path != '/')
958 yyerror("not an absolute path: %s", path);
959 return path;
962 int
963 check_block_code(int n)
965 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
966 yyerror("invalid block code %d", n);
967 return n;
970 char *
971 check_block_fmt(char *fmt)
973 char *s;
975 for (s = fmt; *s; ++s) {
976 if (*s != '%')
977 continue;
978 switch (*++s) {
979 case '%':
980 case 'p':
981 case 'q':
982 case 'P':
983 case 'N':
984 break;
985 default:
986 yyerror("invalid format specifier %%%c", *s);
990 return fmt;
993 int
994 check_strip_no(int n)
996 if (n <= 0)
997 yyerror("invalid strip number %d", n);
998 return n;
1001 int
1002 check_port_num(int n)
1004 if (n <= 0 || n >= UINT16_MAX)
1005 yyerror("port number is %s: %d",
1006 n <= 0 ? "too small" : "too large",
1007 n);
1008 return n;
1011 int
1012 check_prefork_num(int n)
1014 if (n <= 0 || n >= PROC_MAX)
1015 yyerror("invalid prefork number %d", n);
1016 return n;
1019 void
1020 advance_loc(void)
1022 loc = new_location();
1023 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1026 void
1027 only_once(const void *ptr, const char *name)
1029 if (ptr != NULL)
1030 yyerror("`%s' specified more than once", name);
1033 void
1034 only_oncei(int i, const char *name)
1036 if (i != -1)
1037 yyerror("`%s' specified more than once", name);
1040 int
1041 fastcgi_conf(char *path, char *port, char *prog)
1043 struct fcgi *f;
1044 int i;
1046 for (i = 0; i < FCGI_MAX; ++i) {
1047 f = &fcgi[i];
1049 if (f->path == NULL) {
1050 f->id = i;
1051 f->path = path;
1052 f->port = port;
1053 f->prog = prog;
1054 return i;
1057 /* XXX: what to do with prog? */
1058 if (!strcmp(f->path, path) &&
1059 ((port == NULL && f->port == NULL) ||
1060 !strcmp(f->port, port))) {
1061 free(path);
1062 free(port);
1063 return i;
1067 yyerror("too much `fastcgi' rules defined.");
1068 return -1;
1071 void
1072 add_param(char *name, char *val, int env)
1074 struct envlist *e;
1075 struct envhead *h;
1077 if (env)
1078 h = &host->env;
1079 else
1080 h = &host->params;
1082 e = xcalloc(1, sizeof(*e));
1083 e->name = name;
1084 e->value = val;
1085 if (TAILQ_EMPTY(h))
1086 TAILQ_INSERT_HEAD(h, e, envs);
1087 else
1088 TAILQ_INSERT_TAIL(h, e, envs);