Blob


1 %{
3 /*
4 * Copyright (c) 2021, 2022 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 "gmid.h"
28 #include <ctype.h>
29 #include <errno.h>
30 #include <netdb.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include "log.h"
38 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
39 static struct file {
40 TAILQ_ENTRY(file) entry;
41 FILE *stream;
42 char *name;
43 size_t ungetpos;
44 size_t ungetsize;
45 u_char *ungetbuf;
46 int eof_reached;
47 int lineno;
48 int errors;
49 } *file, *topfile;
51 struct file *pushfile(const char *, int);
52 int popfile(void);
53 int yyparse(void);
54 int yylex(void);
55 void yyerror(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 void yywarn(const char *, ...)
59 __attribute__((__format__ (printf, 1, 2)))
60 __attribute__((__nonnull__ (1)));
61 int kw_cmp(const void *, const void *);
62 int lookup(char *);
63 int igetc(void);
64 int lgetc(int);
65 void lungetc(int);
66 int findeol(void);
68 /*
69 * #define YYDEBUG 1
70 * int yydebug = 1;
71 */
73 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
74 struct sym {
75 TAILQ_ENTRY(sym) entry;
76 int used;
77 int persist;
78 char *name;
79 char *val;
80 };
82 int symset(const char *, const char *, int);
83 char *symget(const char *);
85 struct vhost *new_vhost(void);
86 struct location *new_location(void);
87 struct proxy *new_proxy(void);
88 char *ensure_absolute_path(char*);
89 int check_block_code(int);
90 char *check_block_fmt(char*);
91 int check_strip_no(int);
92 int check_port_num(int);
93 int check_prefork_num(int);
94 void advance_loc(void);
95 void advance_proxy(void);
96 void parsehp(char *, char **, const char **, const char *);
97 int fastcgi_conf(const char *, const char *);
98 void add_param(char *, char *);
99 int getservice(const char *);
101 static struct vhost *host;
102 static struct location *loc;
103 static struct proxy *proxy;
104 static char *current_media;
105 static int errors;
107 typedef struct {
108 union {
109 char *string;
110 int number;
111 } v;
112 int lineno;
113 } YYSTYPE;
115 %}
117 /* for bison: */
118 /* %define parse.error verbose */
120 %token ALIAS AUTO
121 %token BLOCK
122 %token CA CERT CHROOT CLIENT
123 %token DEFAULT
124 %token FASTCGI FOR_HOST
125 %token INCLUDE INDEX IPV6
126 %token KEY
127 %token LANG LOCATION LOG
128 %token OCSP OFF ON
129 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
130 %token RELAY_TO REQUIRE RETURN ROOT
131 %token SERVER SNI STRIP
132 %token TCP TOEXT TYPE TYPES
133 %token USE_TLS USER
134 %token VERIFYNAME
136 %token ERROR
138 %token <v.string> STRING
139 %token <v.number> NUM
141 %type <v.number> bool proxy_port
142 %type <v.string> string numberstring
144 %%
146 conf : /* empty */
147 | conf include '\n'
148 | conf '\n'
149 | conf varset '\n'
150 | conf option '\n'
151 | conf vhost '\n'
152 | conf types '\n'
153 | conf error '\n' { file->errors++; }
156 include : INCLUDE STRING {
157 struct file *nfile;
159 if ((nfile = pushfile($2, 0)) == NULL) {
160 yyerror("failed to include file %s", $2);
161 free($2);
162 YYERROR;
164 free($2);
166 file = nfile;
167 lungetc('\n');
171 bool : ON { $$ = 1; }
172 | OFF { $$ = 0; }
175 string : string STRING {
176 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
177 free($1);
178 free($2);
179 yyerror("string: asprintf: %s", strerror(errno));
180 YYERROR;
182 free($1);
183 free($2);
185 | STRING
188 numberstring : NUM {
189 char *s;
190 if (asprintf(&s, "%d", $1) == -1) {
191 yyerror("asprintf: number");
192 YYERROR;
194 $$ = s;
196 | STRING
199 varset : STRING '=' string {
200 char *s = $1;
201 while (*s++) {
202 if (isspace((unsigned char)*s)) {
203 yyerror("macro name cannot contain "
204 "whitespaces");
205 free($1);
206 free($3);
207 YYERROR;
210 symset($1, $3, 0);
211 free($1);
212 free($3);
216 option : CHROOT string {
217 if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >=
218 sizeof(conf.chroot))
219 yyerror("chroot path too long");
220 free($2);
222 | IPV6 bool { conf.ipv6 = $2; }
223 | PORT NUM { conf.port = check_port_num($2); }
224 | PREFORK NUM { conf.prefork = check_prefork_num($2); }
225 | PROTOCOLS string {
226 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
227 yyerror("invalid protocols string \"%s\"", $2);
228 free($2);
230 | USER string {
231 if (strlcpy(conf.user, $2, sizeof(conf.user)) >=
232 sizeof(conf.user))
233 yyerror("user name too long");
234 free($2);
238 vhost : SERVER string {
239 host = new_vhost();
240 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
242 loc = new_location();
243 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
245 TAILQ_INIT(&host->proxies);
247 (void) strlcpy(loc->match, "*", sizeof(loc->match));
248 (void) strlcpy(host->domain, $2, sizeof(host->domain));
250 if (strstr($2, "xn--") != NULL) {
251 yywarn("\"%s\" looks like punycode: you "
252 "should use the decoded hostname", $2);
255 free($2);
256 } '{' optnl servbody '}' {
257 if (host->cert_path == NULL ||
258 host->key_path == NULL)
259 yyerror("invalid vhost definition: %s", $2);
261 | error '}' { yyerror("bad server directive"); }
264 servbody : /* empty */
265 | servbody servopt optnl
266 | servbody location optnl
267 | servbody proxy optnl
270 servopt : ALIAS string {
271 struct alist *a;
273 a = xcalloc(1, sizeof(*a));
274 (void) strlcpy(a->alias, $2, sizeof(a->alias));
275 free($2);
276 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
278 | CERT string {
279 ensure_absolute_path($2);
280 free(host->cert_path);
281 host->cert_path = $2;
283 | KEY string {
284 ensure_absolute_path($2);
285 free(host->key_path);
286 host->key_path = $2;
288 | OCSP string {
289 ensure_absolute_path($2);
290 free(host->ocsp_path);
291 host->ocsp_path = $2;
293 | PARAM string '=' string {
294 add_param($2, $4);
296 | locopt
299 proxy : PROXY { advance_proxy(); }
300 proxy_matches '{' optnl proxy_opts '}' {
301 if (*proxy->host == '\0')
302 yyerror("invalid proxy block: missing `relay-to' option");
304 if ((proxy->cert == NULL && proxy->key != NULL) ||
305 (proxy->cert != NULL && proxy->key == NULL))
306 yyerror("invalid proxy block: missing cert or key");
310 proxy_matches : /* empty */
311 | proxy_matches proxy_match
314 proxy_port : /* empty */ { $$ = 1965; }
315 | PORT STRING {
316 if (($$ = getservice($2)) == -1)
317 yyerror("invalid port number %s", $2);
318 free($2);
320 | PORT NUM { $$ = $2; }
323 proxy_match : PROTO string {
324 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
325 free($2);
327 | FOR_HOST string proxy_port {
328 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
329 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
330 "%d", $3);
331 free($2);
335 proxy_opts : /* empty */
336 | proxy_opts proxy_opt optnl
339 proxy_opt : CERT string {
340 tls_unload_file(proxy->cert, proxy->certlen);
341 ensure_absolute_path($2);
342 proxy->cert = tls_load_file($2, &proxy->certlen, NULL);
343 if (proxy->cert == NULL)
344 yyerror("can't load cert %s", $2);
345 free($2);
347 | KEY string {
348 tls_unload_file(proxy->key, proxy->keylen);
349 ensure_absolute_path($2);
350 proxy->key = tls_load_file($2, &proxy->keylen, NULL);
351 if (proxy->key == NULL)
352 yyerror("can't load key %s", $2);
353 free($2);
355 | PROTOCOLS string {
356 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
357 yyerror("invalid protocols string \"%s\"", $2);
358 free($2);
360 | RELAY_TO string proxy_port {
361 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
362 (void) snprintf(proxy->port, sizeof(proxy->port),
363 "%d", $3);
364 free($2);
366 | REQUIRE CLIENT CA string {
367 ensure_absolute_path($4);
368 if ((proxy->reqca = load_ca($4)) == NULL)
369 yyerror("couldn't load ca cert: %s", $4);
370 free($4);
372 | SNI string {
373 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
374 free($2);
376 | USE_TLS bool {
377 proxy->notls = !$2;
379 | VERIFYNAME bool {
380 proxy->noverifyname = !$2;
384 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
385 /* drop the starting '/' if any */
386 if (*$3 == '/')
387 memmove($3, $3+1, strlen($3));
388 (void) strlcpy(loc->match, $3, sizeof(loc->match));
389 free($3);
391 | error '}'
394 locopts : /* empty */
395 | locopts locopt optnl
398 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
399 | BLOCK RETURN NUM string {
400 check_block_fmt($4);
401 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
402 loc->block_code = check_block_code($3);
403 free($4);
405 | BLOCK RETURN NUM {
406 (void) strlcpy(loc->block_fmt, "temporary failure",
407 sizeof(loc->block_fmt));
408 loc->block_code = check_block_code($3);
409 if ($3 >= 30 && $3 < 40)
410 yyerror("missing `meta' for block return %d", $3);
412 | BLOCK {
413 (void) strlcpy(loc->block_fmt, "temporary failure",
414 sizeof(loc->block_fmt));
415 loc->block_code = 40;
417 | DEFAULT TYPE string {
418 (void) strlcpy(loc->default_mime, $3,
419 sizeof(loc->default_mime));
420 free($3);
422 | FASTCGI fastcgi
423 | INDEX string {
424 (void) strlcpy(loc->index, $2, sizeof(loc->index));
425 free($2);
427 | LANG string {
428 (void) strlcpy(loc->lang, $2,
429 sizeof(loc->lang));
430 free($2);
432 | LOG bool { loc->disable_log = !$2; }
433 | REQUIRE CLIENT CA string {
434 ensure_absolute_path($4);
435 if ((loc->reqca = load_ca($4)) == NULL)
436 yyerror("couldn't load ca cert: %s", $4);
437 free($4);
439 | ROOT string {
440 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
441 free($2);
443 | STRIP NUM { loc->strip = check_strip_no($2); }
446 fastcgi : string {
447 loc->fcgi = fastcgi_conf($1, NULL);
448 free($1);
450 | TCP string PORT NUM {
451 char *c;
452 if (asprintf(&c, "%d", $4) == -1)
453 err(1, "asprintf");
454 loc->fcgi = fastcgi_conf($2, c);
455 free($2);
457 | TCP string {
458 loc->fcgi = fastcgi_conf($2, "9000");
459 free($2);
461 | TCP string PORT string {
462 loc->fcgi = fastcgi_conf($2, $4);
463 free($2);
464 free($4);
468 types : TYPES '{' optnl mediaopts_l '}' ;
470 mediaopts_l : mediaopts_l mediaoptsl nl
471 | mediaoptsl nl
474 mediaoptsl : STRING {
475 free(current_media);
476 current_media = $1;
477 } medianames_l optsemicolon
478 | include
481 medianames_l : medianames_l medianamesl
482 | medianamesl
485 medianamesl : numberstring {
486 if (add_mime(&conf.mime, current_media, $1) == -1)
487 err(1, "add_mime");
488 free($1);
492 nl : '\n' optnl
495 optnl : '\n' optnl /* zero or more newlines */
496 | ';' optnl /* semicolons too */
497 | /*empty*/
500 optsemicolon : ';'
504 %%
506 static const struct keyword {
507 const char *word;
508 int token;
509 } keywords[] = {
510 /* these MUST be sorted */
511 {"alias", ALIAS},
512 {"auto", AUTO},
513 {"block", BLOCK},
514 {"ca", CA},
515 {"cert", CERT},
516 {"chroot", CHROOT},
517 {"client", CLIENT},
518 {"default", DEFAULT},
519 {"fastcgi", FASTCGI},
520 {"for-host", FOR_HOST},
521 {"include", INCLUDE},
522 {"index", INDEX},
523 {"ipv6", IPV6},
524 {"key", KEY},
525 {"lang", LANG},
526 {"location", LOCATION},
527 {"log", LOG},
528 {"ocsp", OCSP},
529 {"off", OFF},
530 {"on", ON},
531 {"param", PARAM},
532 {"port", PORT},
533 {"prefork", PREFORK},
534 {"proto", PROTO},
535 {"protocols", PROTOCOLS},
536 {"proxy", PROXY},
537 {"relay-to", RELAY_TO},
538 {"require", REQUIRE},
539 {"return", RETURN},
540 {"root", ROOT},
541 {"server", SERVER},
542 {"sni", SNI},
543 {"strip", STRIP},
544 {"tcp", TCP},
545 {"to-ext", TOEXT},
546 {"type", TYPE},
547 {"types", TYPES},
548 {"use-tls", USE_TLS},
549 {"user", USER},
550 {"verifyname", VERIFYNAME},
551 };
553 void
554 yyerror(const char *msg, ...)
556 va_list ap;
558 file->errors++;
560 va_start(ap, msg);
561 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
562 vfprintf(stderr, msg, ap);
563 fprintf(stderr, "\n");
564 va_end(ap);
567 void
568 yywarn(const char *msg, ...)
570 va_list ap;
572 va_start(ap, msg);
573 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
574 vfprintf(stderr, msg, ap);
575 fprintf(stderr, "\n");
576 va_end(ap);
579 int
580 kw_cmp(const void *k, const void *e)
582 return strcmp(k, ((struct keyword *)e)->word);
585 int
586 lookup(char *s)
588 const struct keyword *p;
590 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
591 sizeof(keywords[0]), kw_cmp);
593 if (p)
594 return p->token;
595 else
596 return STRING;
599 #define START_EXPAND 1
600 #define DONE_EXPAND 2
602 static int expanding;
604 int
605 igetc(void)
607 int c;
609 while (1) {
610 if (file->ungetpos > 0)
611 c = file->ungetbuf[--file->ungetpos];
612 else
613 c = getc(file->stream);
615 if (c == START_EXPAND)
616 expanding = 1;
617 else if (c == DONE_EXPAND)
618 expanding = 0;
619 else
620 break;
622 return c;
625 int
626 lgetc(int quotec)
628 int c, next;
630 if (quotec) {
631 if ((c = igetc()) == EOF) {
632 yyerror("reached end of file while parsing "
633 "quoted string");
634 if (file == topfile || popfile() == EOF)
635 return EOF;
636 return quotec;
638 return c;
641 while ((c = igetc()) == '\\') {
642 next = igetc();
643 if (next != '\n') {
644 c = next;
645 break;
647 yylval.lineno = file->lineno;
648 file->lineno++;
651 if (c == EOF) {
652 /*
653 * Fake EOL when hit EOF for the first time. This gets line
654 * count right if last line in included file is syntactically
655 * invalid and has no newline.
656 */
657 if (file->eof_reached == 0) {
658 file->eof_reached = 1;
659 return '\n';
661 while (c == EOF) {
662 if (file == topfile || popfile() == EOF)
663 return EOF;
664 c = igetc();
667 return c;
670 void
671 lungetc(int c)
673 if (c == EOF)
674 return;
676 if (file->ungetpos >= file->ungetsize) {
677 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
678 if (p == NULL)
679 fatal("lungetc");
680 file->ungetbuf = p;
681 file->ungetsize *= 2;
683 file->ungetbuf[file->ungetpos++] = c;
686 int
687 findeol(void)
689 int c;
691 /* Skip to either EOF or the first real EOL. */
692 while (1) {
693 c = lgetc(0);
694 if (c == '\n') {
695 file->lineno++;
696 break;
698 if (c == EOF)
699 break;
701 return ERROR;
704 int
705 yylex(void)
707 char buf[8096];
708 char *p, *val;
709 int quotec, next, c;
710 int token;
712 top:
713 p = buf;
714 while ((c = lgetc(0)) == ' ' || c == '\t')
715 ; /* nothing */
717 yylval.lineno = file->lineno;
718 if (c == '#')
719 while ((c = lgetc(0)) != '\n' && c != EOF)
720 ; /* nothing */
721 if (c == '$' && !expanding) {
722 while (1) {
723 if ((c = lgetc(0)) == EOF)
724 return 0;
725 if (p + 1 >= buf + sizeof(buf) -1) {
726 yyerror("string too long");
727 return findeol();
729 if (isalnum(c) || c == '_') {
730 *p++ = c;
731 continue;
733 *p = '\0';
734 lungetc(c);
735 break;
737 val = symget(buf);
738 if (val == NULL) {
739 yyerror("macro `%s' not defined", buf);
740 return findeol();
742 yylval.v.string = xstrdup(val);
743 return STRING;
745 if (c == '@' && !expanding) {
746 while (1) {
747 if ((c = lgetc(0)) == EOF)
748 return 0;
750 if (p + 1 >= buf + sizeof(buf) - 1) {
751 yyerror("string too long");
752 return findeol();
754 if (isalnum(c) || c == '_') {
755 *p++ = c;
756 continue;
758 *p = '\0';
759 lungetc(c);
760 break;
762 val = symget(buf);
763 if (val == NULL) {
764 yyerror("macro '%s' not defined", buf);
765 return findeol();
767 p = val + strlen(val) - 1;
768 lungetc(DONE_EXPAND);
769 while (p >= val) {
770 lungetc(*p);
771 p--;
773 lungetc(START_EXPAND);
774 goto top;
777 switch (c) {
778 case '\'':
779 case '"':
780 quotec = c;
781 while (1) {
782 if ((c = lgetc(quotec)) == EOF)
783 return 0;
784 if (c == '\n') {
785 file->lineno++;
786 continue;
787 } else if (c == '\\') {
788 if ((next = lgetc(quotec)) == EOF)
789 return (0);
790 if (next == quotec || next == ' ' ||
791 next == '\t')
792 c = next;
793 else if (next == '\n') {
794 file->lineno++;
795 continue;
796 } else
797 lungetc(next);
798 } else if (c == quotec) {
799 *p = '\0';
800 break;
801 } else if (c == '\0') {
802 yyerror("invalid syntax");
803 return findeol();
805 if (p + 1 >= buf + sizeof(buf) - 1) {
806 yyerror("string too long");
807 return findeol();
809 *p++ = c;
811 yylval.v.string = strdup(buf);
812 if (yylval.v.string == NULL)
813 fatal("yylex: strdup");
814 return STRING;
817 #define allowed_to_end_number(x) \
818 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
820 if (c == '-' || isdigit(c)) {
821 do {
822 *p++ = c;
823 if ((size_t)(p-buf) >= sizeof(buf)) {
824 yyerror("string too long");
825 return findeol();
827 } while ((c = lgetc(0)) != EOF && isdigit(c));
828 lungetc(c);
829 if (p == buf + 1 && buf[0] == '-')
830 goto nodigits;
831 if (c == EOF || allowed_to_end_number(c)) {
832 const char *errstr = NULL;
834 *p = '\0';
835 yylval.v.number = strtonum(buf, LLONG_MIN,
836 LLONG_MAX, &errstr);
837 if (errstr) {
838 yyerror("\"%s\" invalid number: %s",
839 buf, errstr);
840 return findeol();
842 return NUM;
843 } else {
844 nodigits:
845 while (p > buf + 1)
846 lungetc(*--p);
847 c = *--p;
848 if (c == '-')
849 return c;
853 #define allowed_in_string(x) \
854 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
855 x != '{' && x != '}' && \
856 x != '!' && x != '=' && x != '#' && \
857 x != ',' && x != ';'))
859 if (isalnum(c) || c == ':' || c == '_') {
860 do {
861 *p++ = c;
862 if ((size_t)(p-buf) >= sizeof(buf)) {
863 yyerror("string too long");
864 return findeol();
866 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
867 lungetc(c);
868 *p = '\0';
869 if ((token = lookup(buf)) == STRING)
870 yylval.v.string = xstrdup(buf);
871 return token;
873 if (c == '\n') {
874 yylval.lineno = file->lineno;
875 file->lineno++;
877 if (c == EOF)
878 return 0;
879 return c;
882 struct file *
883 pushfile(const char *name, int secret)
885 struct file *nfile;
887 nfile = xcalloc(1, sizeof(*nfile));
888 nfile->name = xstrdup(name);
889 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
890 log_warn("can't open %s", nfile->name);
891 free(nfile->name);
892 free(nfile);
893 return NULL;
895 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
896 nfile->ungetsize = 16;
897 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
898 TAILQ_INSERT_TAIL(&files, nfile, entry);
899 return nfile;
902 int
903 popfile(void)
905 struct file *prev;
907 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
908 prev->errors += file->errors;
910 TAILQ_REMOVE(&files, file, entry);
911 fclose(file->stream);
912 free(file->name);
913 free(file->ungetbuf);
914 free(file);
915 file = prev;
916 return file ? 0 : EOF;
919 void
920 parse_conf(const char *filename)
922 struct sym *sym, *next;
924 file = pushfile(filename, 0);
925 if (file == NULL)
926 exit(1);
927 topfile = file;
929 yyparse();
930 errors = file->errors;
931 popfile();
933 /* Free macros and check which have not been used. */
934 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
935 /* TODO: warn if !sym->used */
936 if (!sym->persist) {
937 free(sym->name);
938 free(sym->val);
939 TAILQ_REMOVE(&symhead, sym, entry);
940 free(sym);
944 if (errors)
945 exit(1);
948 void
949 print_conf(void)
951 struct vhost *h;
952 /* struct location *l; */
953 /* struct envlist *e; */
954 /* struct alist *a; */
956 if (*conf.chroot != '\0')
957 printf("chroot \"%s\"\n", conf.chroot);
958 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
959 /* XXX: defined mimes? */
960 printf("port %d\n", conf.port);
961 printf("prefork %d\n", conf.prefork);
962 /* XXX: protocols? */
963 if (*conf.user != '\0')
964 printf("user \"%s\"\n", conf.user);
966 TAILQ_FOREACH(h, &hosts, vhosts) {
967 printf("\nserver \"%s\" {\n", h->domain);
968 printf(" cert \"%s\"\n", h->cert);
969 printf(" key \"%s\"\n", h->key);
970 /* TODO: print locations... */
971 printf("}\n");
975 int
976 symset(const char *name, const char *val, int persist)
978 struct sym *sym;
980 TAILQ_FOREACH(sym, &symhead, entry) {
981 if (!strcmp(name, sym->name))
982 break;
985 if (sym != NULL) {
986 if (sym->persist)
987 return 0;
988 else {
989 free(sym->name);
990 free(sym->val);
991 TAILQ_REMOVE(&symhead, sym, entry);
992 free(sym);
996 sym = xcalloc(1, sizeof(*sym));
997 sym->name = xstrdup(name);
998 sym->val = xstrdup(val);
999 sym->used = 0;
1000 sym->persist = persist;
1002 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1003 return 0;
1006 int
1007 cmdline_symset(char *s)
1009 char *sym, *val;
1010 int ret;
1012 if ((val = strrchr(s, '=')) == NULL)
1013 return -1;
1014 sym = xcalloc(1, val - s + 1);
1015 memcpy(sym, s, val - s);
1016 ret = symset(sym, val + 1, 1);
1017 free(sym);
1018 return ret;
1021 char *
1022 symget(const char *nam)
1024 struct sym *sym;
1026 TAILQ_FOREACH(sym, &symhead, entry) {
1027 if (strcmp(nam, sym->name) == 0) {
1028 sym->used = 1;
1029 return sym->val;
1032 return NULL;
1035 char *
1036 ensure_absolute_path(char *path)
1038 if (path == NULL || *path != '/')
1039 yyerror("not an absolute path: %s", path);
1040 return path;
1043 int
1044 check_block_code(int n)
1046 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1047 yyerror("invalid block code %d", n);
1048 return n;
1051 char *
1052 check_block_fmt(char *fmt)
1054 char *s;
1056 for (s = fmt; *s; ++s) {
1057 if (*s != '%')
1058 continue;
1059 switch (*++s) {
1060 case '%':
1061 case 'p':
1062 case 'q':
1063 case 'P':
1064 case 'N':
1065 break;
1066 default:
1067 yyerror("invalid format specifier %%%c", *s);
1071 return fmt;
1074 int
1075 check_strip_no(int n)
1077 if (n <= 0)
1078 yyerror("invalid strip number %d", n);
1079 return n;
1082 int
1083 check_port_num(int n)
1085 if (n <= 0 || n >= UINT16_MAX)
1086 yyerror("port number is %s: %d",
1087 n <= 0 ? "too small" : "too large",
1088 n);
1089 return n;
1092 int
1093 check_prefork_num(int n)
1095 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1096 yyerror("invalid prefork number %d", n);
1097 return n;
1100 void
1101 advance_loc(void)
1103 loc = new_location();
1104 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1107 void
1108 advance_proxy(void)
1110 proxy = new_proxy();
1111 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1114 void
1115 parsehp(char *str, char **host, const char **port, const char *def)
1117 char *at;
1118 const char *errstr;
1120 *host = str;
1122 if ((at = strchr(str, ':')) != NULL) {
1123 *at++ = '\0';
1124 *port = at;
1125 } else
1126 *port = def;
1128 strtonum(*port, 1, UINT16_MAX, &errstr);
1129 if (errstr != NULL)
1130 yyerror("port is %s: %s", errstr, *port);
1133 int
1134 fastcgi_conf(const char *path, const char *port)
1136 struct fcgi *f;
1137 int i;
1139 for (i = 0; i < FCGI_MAX; ++i) {
1140 f = &fcgi[i];
1142 if (*f->path == '\0') {
1143 f->id = i;
1144 (void) strlcpy(f->path, path, sizeof(f->path));
1145 if (port != NULL)
1146 (void) strlcpy(f->port, port, sizeof(f->port));
1147 return i;
1150 if (!strcmp(f->path, path) &&
1151 ((port == NULL && *f->port == '\0') ||
1152 !strcmp(f->port, port)))
1153 return i;
1156 yyerror("too much `fastcgi' rules defined.");
1157 return -1;
1160 void
1161 add_param(char *name, char *val)
1163 struct envlist *e;
1164 struct envhead *h = &host->params;
1166 e = xcalloc(1, sizeof(*e));
1167 (void) strlcpy(e->name, name, sizeof(e->name));
1168 (void) strlcpy(e->value, val, sizeof(e->value));
1169 TAILQ_INSERT_TAIL(h, e, envs);
1172 int
1173 getservice(const char *n)
1175 struct servent *s;
1176 const char *errstr;
1177 long long llval;
1179 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1180 if (errstr) {
1181 s = getservbyname(n, "tcp");
1182 if (s == NULL)
1183 s = getservbyname(n, "udp");
1184 if (s == NULL)
1185 return (-1);
1186 return (ntohs(s->s_port));
1189 return ((unsigned short)llval);