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 RELAY_TO string {
334 char *at;
335 const char *errstr;
337 only_once(loc->proxy_host, "proxy relay-to");
338 loc->proxy_host = $3;
340 if ((at = strchr($3, ':')) != NULL) {
341 *at++ = '\0';
342 loc->proxy_port = at;
343 } else
344 loc->proxy_port = "1965";
346 strtonum(loc->proxy_port, 1, UINT16_MAX, &errstr);
347 if (errstr != NULL)
348 yyerror("proxy port is %s: %s", errstr,
349 loc->proxy_port);
351 | REQUIRE CLIENT CA string {
352 only_once(loc->reqca, "require client ca");
353 ensure_absolute_path($4);
354 if ((loc->reqca = load_ca($4)) == NULL)
355 yyerror("couldn't load ca cert: %s", $4);
356 free($4);
358 | ROOT string {
359 only_once(loc->dir, "root");
360 loc->dir = ensure_absolute_path($2);
362 | STRIP NUM { loc->strip = check_strip_no($2); }
365 fastcgi : SPAWN string {
366 only_oncei(loc->fcgi, "fastcgi");
367 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
369 | string {
370 only_oncei(loc->fcgi, "fastcgi");
371 loc->fcgi = fastcgi_conf($1, NULL, NULL);
373 | TCP string PORT NUM {
374 char *c;
375 if (asprintf(&c, "%d", $4) == -1)
376 err(1, "asprintf");
377 only_oncei(loc->fcgi, "fastcgi");
378 loc->fcgi = fastcgi_conf($2, c, NULL);
380 | TCP string {
381 only_oncei(loc->fcgi, "fastcgi");
382 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
384 | TCP string PORT string {
385 only_oncei(loc->fcgi, "fastcgi");
386 loc->fcgi = fastcgi_conf($2, $4, NULL);
390 optnl : '\n' optnl /* zero or more newlines */
391 | ';' optnl /* semicolons too */
392 | /*empty*/
395 %%
397 static struct keyword {
398 const char *word;
399 int token;
400 } keywords[] = {
401 /* these MUST be sorted */
402 {"alias", ALIAS},
403 {"auto", AUTO},
404 {"block", BLOCK},
405 {"ca", CA},
406 {"cert", CERT},
407 {"cgi", CGI},
408 {"chroot", CHROOT},
409 {"client", CLIENT},
410 {"default", DEFAULT},
411 {"entrypoint", ENTRYPOINT},
412 {"env", ENV},
413 {"fastcgi", FASTCGI},
414 {"index", INDEX},
415 {"ipv6", IPV6},
416 {"key", KEY},
417 {"lang", LANG},
418 {"location", LOCATION},
419 {"log", LOG},
420 {"map", MAP},
421 {"mime", MIME},
422 {"ocsp", OCSP},
423 {"off", OFF},
424 {"on", ON},
425 {"param", PARAM},
426 {"port", PORT},
427 {"prefork", PREFORK},
428 {"protocols", PROTOCOLS},
429 {"proxy", PROXY},
430 {"relay-to", RELAY_TO},
431 {"require", REQUIRE},
432 {"return", RETURN},
433 {"root", ROOT},
434 {"server", SERVER},
435 {"spawn", SPAWN},
436 {"strip", STRIP},
437 {"tcp", TCP},
438 {"to-ext", TOEXT},
439 {"type", TYPE},
440 {"user", USER},
441 };
443 void
444 yyerror(const char *msg, ...)
446 va_list ap;
448 file->errors++;
450 va_start(ap, msg);
451 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
452 vfprintf(stderr, msg, ap);
453 fprintf(stderr, "\n");
454 va_end(ap);
457 void
458 yywarn(const char *msg, ...)
460 va_list ap;
462 va_start(ap, msg);
463 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
464 vfprintf(stderr, msg, ap);
465 fprintf(stderr, "\n");
466 va_end(ap);
469 int
470 kw_cmp(const void *k, const void *e)
472 return strcmp(k, ((struct keyword *)e)->word);
475 int
476 lookup(char *s)
478 const struct keyword *p;
480 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
481 sizeof(keywords[0]), kw_cmp);
483 if (p)
484 return p->token;
485 else
486 return STRING;
489 #define START_EXPAND 1
490 #define DONE_EXPAND 2
492 static int expanding;
494 int
495 igetc(void)
497 int c;
499 while (1) {
500 if (file->ungetpos > 0)
501 c = file->ungetbuf[--file->ungetpos];
502 else
503 c = getc(file->stream);
505 if (c == START_EXPAND)
506 expanding = 1;
507 else if (c == DONE_EXPAND)
508 expanding = 0;
509 else
510 break;
512 return c;
515 int
516 lgetc(int quotec)
518 int c, next;
520 if (quotec) {
521 if ((c = igetc()) == EOF) {
522 yyerror("reached end of file while parsing "
523 "quoted string");
524 if (file == topfile || popfile() == EOF)
525 return EOF;
526 return quotec;
528 return c;
531 while ((c = igetc()) == '\\') {
532 next = igetc();
533 if (next != '\n') {
534 c = next;
535 break;
537 yylval.lineno = file->lineno;
538 file->lineno++;
541 if (c == EOF) {
542 /*
543 * Fake EOL when hit EOF for the first time. This gets line
544 * count right if last line in included file is syntactically
545 * invalid and has no newline.
546 */
547 if (file->eof_reached == 0) {
548 file->eof_reached = 1;
549 return '\n';
551 while (c == EOF) {
552 if (file == topfile || popfile() == EOF)
553 return EOF;
554 c = igetc();
557 return c;
560 void
561 lungetc(int c)
563 if (c == EOF)
564 return;
566 if (file->ungetpos >= file->ungetsize) {
567 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
568 if (p == NULL)
569 err(1, "lungetc");
570 file->ungetbuf = p;
571 file->ungetsize *= 2;
573 file->ungetbuf[file->ungetpos++] = c;
576 int
577 findeol(void)
579 int c;
581 /* Skip to either EOF or the first real EOL. */
582 while (1) {
583 c = lgetc(0);
584 if (c == '\n') {
585 file->lineno++;
586 break;
588 if (c == EOF)
589 break;
591 return ERROR;
594 int
595 yylex(void)
597 char buf[8096];
598 char *p, *val;
599 int quotec, next, c;
600 int token;
602 top:
603 p = buf;
604 while ((c = lgetc(0)) == ' ' || c == '\t')
605 ; /* nothing */
607 yylval.lineno = file->lineno;
608 if (c == '#')
609 while ((c = lgetc(0)) != '\n' && c != EOF)
610 ; /* nothing */
611 if (c == '$' && !expanding) {
612 while (1) {
613 if ((c = lgetc(0)) == EOF)
614 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 yylval.v.string = xstrdup(val);
633 return STRING;
635 if (c == '@' && !expanding) {
636 while (1) {
637 if ((c = lgetc(0)) == EOF)
638 return 0;
640 if (p + 1 >= buf + sizeof(buf) - 1) {
641 yyerror("string too long");
642 return findeol();
644 if (isalnum(c) || c == '_') {
645 *p++ = c;
646 continue;
648 *p = '\0';
649 lungetc(c);
650 break;
652 val = symget(buf);
653 if (val == NULL) {
654 yyerror("macro '%s' not defined", buf);
655 return findeol();
657 p = val + strlen(val) - 1;
658 lungetc(DONE_EXPAND);
659 while (p >= val) {
660 lungetc(*p);
661 p--;
663 lungetc(START_EXPAND);
664 goto top;
667 switch (c) {
668 case '\'':
669 case '"':
670 quotec = c;
671 while (1) {
672 if ((c = lgetc(quotec)) == EOF)
673 return 0;
674 if (c == '\n') {
675 file->lineno++;
676 continue;
677 } else if (c == '\\') {
678 if ((next = lgetc(quotec)) == EOF)
679 return (0);
680 if (next == quotec || next == ' ' ||
681 next == '\t')
682 c = next;
683 else if (next == '\n') {
684 file->lineno++;
685 continue;
686 } else
687 lungetc(next);
688 } else if (c == quotec) {
689 *p = '\0';
690 break;
691 } else if (c == '\0') {
692 yyerror("invalid syntax");
693 return findeol();
695 if (p + 1 >= buf + sizeof(buf) - 1) {
696 yyerror("string too long");
697 return findeol();
699 *p++ = c;
701 yylval.v.string = strdup(buf);
702 if (yylval.v.string == NULL)
703 err(1, "yylex: strdup");
704 return STRING;
707 #define allowed_to_end_number(x) \
708 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
710 if (c == '-' || isdigit(c)) {
711 do {
712 *p++ = c;
713 if ((size_t)(p-buf) >= sizeof(buf)) {
714 yyerror("string too long");
715 return findeol();
717 } while ((c = lgetc(0)) != EOF && isdigit(c));
718 lungetc(c);
719 if (p == buf + 1 && buf[0] == '-')
720 goto nodigits;
721 if (c == EOF || allowed_to_end_number(c)) {
722 const char *errstr = NULL;
724 *p = '\0';
725 yylval.v.number = strtonum(buf, LLONG_MIN,
726 LLONG_MAX, &errstr);
727 if (errstr) {
728 yyerror("\"%s\" invalid number: %s",
729 buf, errstr);
730 return findeol();
732 return NUM;
733 } else {
734 nodigits:
735 while (p > buf + 1)
736 lungetc(*--p);
737 c = *--p;
738 if (c == '-')
739 return c;
743 #define allowed_in_string(x) \
744 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
745 x != '{' && x != '}' && \
746 x != '!' && x != '=' && x != '#' && \
747 x != ',' && x != ';'))
749 if (isalnum(c) || c == ':' || c == '_') {
750 do {
751 *p++ = c;
752 if ((size_t)(p-buf) >= sizeof(buf)) {
753 yyerror("string too long");
754 return findeol();
756 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
757 lungetc(c);
758 *p = '\0';
759 if ((token = lookup(buf)) == STRING)
760 yylval.v.string = xstrdup(buf);
761 return token;
763 if (c == '\n') {
764 yylval.lineno = file->lineno;
765 file->lineno++;
767 if (c == EOF)
768 return 0;
769 return c;
772 struct file *
773 pushfile(const char *name, int secret)
775 struct file *nfile;
777 nfile = xcalloc(1, sizeof(*nfile));
778 nfile->name = xstrdup(name);
779 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
780 log_warn(NULL, "can't open %s: %s", nfile->name,
781 strerror(errno));
782 free(nfile->name);
783 free(nfile);
784 return NULL;
786 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
787 nfile->ungetsize = 16;
788 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
789 TAILQ_INSERT_TAIL(&files, nfile, entry);
790 return nfile;
793 int
794 popfile(void)
796 struct file *prev;
798 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
799 prev->errors += file->errors;
801 TAILQ_REMOVE(&files, file, entry);
802 fclose(file->stream);
803 free(file->name);
804 free(file->ungetbuf);
805 free(file);
806 file = prev;
807 return file ? 0 : EOF;
810 void
811 parse_conf(const char *filename)
813 struct sym *sym, *next;
815 file = pushfile(filename, 0);
816 if (file == NULL)
817 exit(1);
818 topfile = file;
820 yyparse();
821 errors = file->errors;
822 popfile();
824 /* Free macros and check which have not been used. */
825 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
826 /* TODO: warn if !sym->used */
827 if (!sym->persist) {
828 free(sym->name);
829 free(sym->val);
830 TAILQ_REMOVE(&symhead, sym, entry);
831 free(sym);
835 if (errors)
836 exit(1);
839 void
840 print_conf(void)
842 struct vhost *h;
843 /* struct location *l; */
844 /* struct envlist *e; */
845 /* struct alist *a; */
847 if (conf.chroot != NULL)
848 printf("chroot \"%s\"\n", conf.chroot);
849 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
850 /* XXX: defined mimes? */
851 printf("port %d\n", conf.port);
852 printf("prefork %d\n", conf.prefork);
853 /* XXX: protocols? */
854 if (conf.user != NULL)
855 printf("user \"%s\"\n", conf.user);
857 TAILQ_FOREACH(h, &hosts, vhosts) {
858 printf("\nserver \"%s\" {\n", h->domain);
859 printf(" cert \"%s\"\n", h->cert);
860 printf(" key \"%s\"\n", h->key);
861 /* TODO: print locations... */
862 printf("}\n");
866 int
867 symset(const char *name, const char *val, int persist)
869 struct sym *sym;
871 TAILQ_FOREACH(sym, &symhead, entry) {
872 if (!strcmp(name, sym->name))
873 break;
876 if (sym != NULL) {
877 if (sym->persist)
878 return 0;
879 else {
880 free(sym->name);
881 free(sym->val);
882 TAILQ_REMOVE(&symhead, sym, entry);
883 free(sym);
887 sym = xcalloc(1, sizeof(*sym));
888 sym->name = xstrdup(name);
889 sym->val = xstrdup(val);
890 sym->used = 0;
891 sym->persist = persist;
893 TAILQ_INSERT_TAIL(&symhead, sym, entry);
894 return 0;
897 int
898 cmdline_symset(char *s)
900 char *sym, *val;
901 int ret;
903 if ((val = strrchr(s, '=')) == NULL)
904 return -1;
905 sym = xcalloc(1, val - s + 1);
906 memcpy(sym, s, val - s);
907 ret = symset(sym, val + 1, 1);
908 free(sym);
909 return ret;
912 char *
913 symget(const char *nam)
915 struct sym *sym;
917 TAILQ_FOREACH(sym, &symhead, entry) {
918 if (strcmp(nam, sym->name) == 0) {
919 sym->used = 1;
920 return sym->val;
923 return NULL;
926 struct vhost *
927 new_vhost(void)
929 return xcalloc(1, sizeof(struct vhost));
932 struct location *
933 new_location(void)
935 struct location *l;
937 l = xcalloc(1, sizeof(*l));
938 l->dirfd = -1;
939 l->fcgi = -1;
940 return l;
943 char *
944 ensure_absolute_path(char *path)
946 if (path == NULL || *path != '/')
947 yyerror("not an absolute path: %s", path);
948 return path;
951 int
952 check_block_code(int n)
954 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
955 yyerror("invalid block code %d", n);
956 return n;
959 char *
960 check_block_fmt(char *fmt)
962 char *s;
964 for (s = fmt; *s; ++s) {
965 if (*s != '%')
966 continue;
967 switch (*++s) {
968 case '%':
969 case 'p':
970 case 'q':
971 case 'P':
972 case 'N':
973 break;
974 default:
975 yyerror("invalid format specifier %%%c", *s);
979 return fmt;
982 int
983 check_strip_no(int n)
985 if (n <= 0)
986 yyerror("invalid strip number %d", n);
987 return n;
990 int
991 check_port_num(int n)
993 if (n <= 0 || n >= UINT16_MAX)
994 yyerror("port number is %s: %d",
995 n <= 0 ? "too small" : "too large",
996 n);
997 return n;
1000 int
1001 check_prefork_num(int n)
1003 if (n <= 0 || n >= PROC_MAX)
1004 yyerror("invalid prefork number %d", n);
1005 return n;
1008 void
1009 advance_loc(void)
1011 loc = new_location();
1012 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1015 void
1016 only_once(const void *ptr, const char *name)
1018 if (ptr != NULL)
1019 yyerror("`%s' specified more than once", name);
1022 void
1023 only_oncei(int i, const char *name)
1025 if (i != -1)
1026 yyerror("`%s' specified more than once", name);
1029 int
1030 fastcgi_conf(char *path, char *port, char *prog)
1032 struct fcgi *f;
1033 int i;
1035 for (i = 0; i < FCGI_MAX; ++i) {
1036 f = &fcgi[i];
1038 if (f->path == NULL) {
1039 f->id = i;
1040 f->path = path;
1041 f->port = port;
1042 f->prog = prog;
1043 return i;
1046 /* XXX: what to do with prog? */
1047 if (!strcmp(f->path, path) &&
1048 ((port == NULL && f->port == NULL) ||
1049 !strcmp(f->port, port))) {
1050 free(path);
1051 free(port);
1052 return i;
1056 yyerror("too much `fastcgi' rules defined.");
1057 return -1;
1060 void
1061 add_param(char *name, char *val, int env)
1063 struct envlist *e;
1064 struct envhead *h;
1066 if (env)
1067 h = &host->env;
1068 else
1069 h = &host->params;
1071 e = xcalloc(1, sizeof(*e));
1072 e->name = name;
1073 e->value = val;
1074 if (TAILQ_EMPTY(h))
1075 TAILQ_INSERT_HEAD(h, e, envs);
1076 else
1077 TAILQ_INSERT_TAIL(h, e, envs);