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_path == NULL && proxy->key_path != NULL) ||
305 (proxy->cert_path != NULL && proxy->key_path == 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 free(proxy->cert);
341 ensure_absolute_path($2);
342 proxy->cert_path = $2;
344 | KEY string {
345 free(proxy->key);
346 ensure_absolute_path($2);
347 proxy->key_path = $2;
349 | PROTOCOLS string {
350 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
351 yyerror("invalid protocols string \"%s\"", $2);
352 free($2);
354 | RELAY_TO string proxy_port {
355 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
356 (void) snprintf(proxy->port, sizeof(proxy->port),
357 "%d", $3);
358 free($2);
360 | REQUIRE CLIENT CA string {
361 ensure_absolute_path($4);
362 proxy->reqca_path = $4;
364 | SNI string {
365 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
366 free($2);
368 | USE_TLS bool {
369 proxy->notls = !$2;
371 | VERIFYNAME bool {
372 proxy->noverifyname = !$2;
376 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
377 /* drop the starting '/' if any */
378 if (*$3 == '/')
379 memmove($3, $3+1, strlen($3));
380 (void) strlcpy(loc->match, $3, sizeof(loc->match));
381 free($3);
383 | error '}'
386 locopts : /* empty */
387 | locopts locopt optnl
390 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
391 | BLOCK RETURN NUM string {
392 check_block_fmt($4);
393 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
394 loc->block_code = check_block_code($3);
395 free($4);
397 | BLOCK RETURN NUM {
398 (void) strlcpy(loc->block_fmt, "temporary failure",
399 sizeof(loc->block_fmt));
400 loc->block_code = check_block_code($3);
401 if ($3 >= 30 && $3 < 40)
402 yyerror("missing `meta' for block return %d", $3);
404 | BLOCK {
405 (void) strlcpy(loc->block_fmt, "temporary failure",
406 sizeof(loc->block_fmt));
407 loc->block_code = 40;
409 | DEFAULT TYPE string {
410 (void) strlcpy(loc->default_mime, $3,
411 sizeof(loc->default_mime));
412 free($3);
414 | FASTCGI fastcgi
415 | INDEX string {
416 (void) strlcpy(loc->index, $2, sizeof(loc->index));
417 free($2);
419 | LANG string {
420 (void) strlcpy(loc->lang, $2,
421 sizeof(loc->lang));
422 free($2);
424 | LOG bool { loc->disable_log = !$2; }
425 | REQUIRE CLIENT CA string {
426 ensure_absolute_path($4);
427 loc->reqca_path = $4;
429 | ROOT string {
430 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
431 free($2);
433 | STRIP NUM { loc->strip = check_strip_no($2); }
436 fastcgi : string {
437 loc->fcgi = fastcgi_conf($1, NULL);
438 free($1);
440 | TCP string PORT NUM {
441 char *c;
442 if (asprintf(&c, "%d", $4) == -1)
443 err(1, "asprintf");
444 loc->fcgi = fastcgi_conf($2, c);
445 free($2);
447 | TCP string {
448 loc->fcgi = fastcgi_conf($2, "9000");
449 free($2);
451 | TCP string PORT string {
452 loc->fcgi = fastcgi_conf($2, $4);
453 free($2);
454 free($4);
458 types : TYPES '{' optnl mediaopts_l '}' ;
460 mediaopts_l : mediaopts_l mediaoptsl nl
461 | mediaoptsl nl
464 mediaoptsl : STRING {
465 free(current_media);
466 current_media = $1;
467 } medianames_l optsemicolon
468 | include
471 medianames_l : medianames_l medianamesl
472 | medianamesl
475 medianamesl : numberstring {
476 if (add_mime(&conf.mime, current_media, $1) == -1)
477 err(1, "add_mime");
478 free($1);
482 nl : '\n' optnl
485 optnl : '\n' optnl /* zero or more newlines */
486 | ';' optnl /* semicolons too */
487 | /*empty*/
490 optsemicolon : ';'
494 %%
496 static const struct keyword {
497 const char *word;
498 int token;
499 } keywords[] = {
500 /* these MUST be sorted */
501 {"alias", ALIAS},
502 {"auto", AUTO},
503 {"block", BLOCK},
504 {"ca", CA},
505 {"cert", CERT},
506 {"chroot", CHROOT},
507 {"client", CLIENT},
508 {"default", DEFAULT},
509 {"fastcgi", FASTCGI},
510 {"for-host", FOR_HOST},
511 {"include", INCLUDE},
512 {"index", INDEX},
513 {"ipv6", IPV6},
514 {"key", KEY},
515 {"lang", LANG},
516 {"location", LOCATION},
517 {"log", LOG},
518 {"ocsp", OCSP},
519 {"off", OFF},
520 {"on", ON},
521 {"param", PARAM},
522 {"port", PORT},
523 {"prefork", PREFORK},
524 {"proto", PROTO},
525 {"protocols", PROTOCOLS},
526 {"proxy", PROXY},
527 {"relay-to", RELAY_TO},
528 {"require", REQUIRE},
529 {"return", RETURN},
530 {"root", ROOT},
531 {"server", SERVER},
532 {"sni", SNI},
533 {"strip", STRIP},
534 {"tcp", TCP},
535 {"to-ext", TOEXT},
536 {"type", TYPE},
537 {"types", TYPES},
538 {"use-tls", USE_TLS},
539 {"user", USER},
540 {"verifyname", VERIFYNAME},
541 };
543 void
544 yyerror(const char *msg, ...)
546 va_list ap;
548 file->errors++;
550 va_start(ap, msg);
551 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
552 vfprintf(stderr, msg, ap);
553 fprintf(stderr, "\n");
554 va_end(ap);
557 void
558 yywarn(const char *msg, ...)
560 va_list ap;
562 va_start(ap, msg);
563 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
564 vfprintf(stderr, msg, ap);
565 fprintf(stderr, "\n");
566 va_end(ap);
569 int
570 kw_cmp(const void *k, const void *e)
572 return strcmp(k, ((struct keyword *)e)->word);
575 int
576 lookup(char *s)
578 const struct keyword *p;
580 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
581 sizeof(keywords[0]), kw_cmp);
583 if (p)
584 return p->token;
585 else
586 return STRING;
589 #define START_EXPAND 1
590 #define DONE_EXPAND 2
592 static int expanding;
594 int
595 igetc(void)
597 int c;
599 while (1) {
600 if (file->ungetpos > 0)
601 c = file->ungetbuf[--file->ungetpos];
602 else
603 c = getc(file->stream);
605 if (c == START_EXPAND)
606 expanding = 1;
607 else if (c == DONE_EXPAND)
608 expanding = 0;
609 else
610 break;
612 return c;
615 int
616 lgetc(int quotec)
618 int c, next;
620 if (quotec) {
621 if ((c = igetc()) == EOF) {
622 yyerror("reached end of file while parsing "
623 "quoted string");
624 if (file == topfile || popfile() == EOF)
625 return EOF;
626 return quotec;
628 return c;
631 while ((c = igetc()) == '\\') {
632 next = igetc();
633 if (next != '\n') {
634 c = next;
635 break;
637 yylval.lineno = file->lineno;
638 file->lineno++;
641 if (c == EOF) {
642 /*
643 * Fake EOL when hit EOF for the first time. This gets line
644 * count right if last line in included file is syntactically
645 * invalid and has no newline.
646 */
647 if (file->eof_reached == 0) {
648 file->eof_reached = 1;
649 return '\n';
651 while (c == EOF) {
652 if (file == topfile || popfile() == EOF)
653 return EOF;
654 c = igetc();
657 return c;
660 void
661 lungetc(int c)
663 if (c == EOF)
664 return;
666 if (file->ungetpos >= file->ungetsize) {
667 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
668 if (p == NULL)
669 fatal("lungetc");
670 file->ungetbuf = p;
671 file->ungetsize *= 2;
673 file->ungetbuf[file->ungetpos++] = c;
676 int
677 findeol(void)
679 int c;
681 /* Skip to either EOF or the first real EOL. */
682 while (1) {
683 c = lgetc(0);
684 if (c == '\n') {
685 file->lineno++;
686 break;
688 if (c == EOF)
689 break;
691 return ERROR;
694 int
695 yylex(void)
697 char buf[8096];
698 char *p, *val;
699 int quotec, next, c;
700 int token;
702 top:
703 p = buf;
704 while ((c = lgetc(0)) == ' ' || c == '\t')
705 ; /* nothing */
707 yylval.lineno = file->lineno;
708 if (c == '#')
709 while ((c = lgetc(0)) != '\n' && c != EOF)
710 ; /* nothing */
711 if (c == '$' && !expanding) {
712 while (1) {
713 if ((c = lgetc(0)) == EOF)
714 return 0;
715 if (p + 1 >= buf + sizeof(buf) -1) {
716 yyerror("string too long");
717 return findeol();
719 if (isalnum(c) || c == '_') {
720 *p++ = c;
721 continue;
723 *p = '\0';
724 lungetc(c);
725 break;
727 val = symget(buf);
728 if (val == NULL) {
729 yyerror("macro `%s' not defined", buf);
730 return findeol();
732 yylval.v.string = xstrdup(val);
733 return STRING;
735 if (c == '@' && !expanding) {
736 while (1) {
737 if ((c = lgetc(0)) == EOF)
738 return 0;
740 if (p + 1 >= buf + sizeof(buf) - 1) {
741 yyerror("string too long");
742 return findeol();
744 if (isalnum(c) || c == '_') {
745 *p++ = c;
746 continue;
748 *p = '\0';
749 lungetc(c);
750 break;
752 val = symget(buf);
753 if (val == NULL) {
754 yyerror("macro '%s' not defined", buf);
755 return findeol();
757 p = val + strlen(val) - 1;
758 lungetc(DONE_EXPAND);
759 while (p >= val) {
760 lungetc(*p);
761 p--;
763 lungetc(START_EXPAND);
764 goto top;
767 switch (c) {
768 case '\'':
769 case '"':
770 quotec = c;
771 while (1) {
772 if ((c = lgetc(quotec)) == EOF)
773 return 0;
774 if (c == '\n') {
775 file->lineno++;
776 continue;
777 } else if (c == '\\') {
778 if ((next = lgetc(quotec)) == EOF)
779 return (0);
780 if (next == quotec || next == ' ' ||
781 next == '\t')
782 c = next;
783 else if (next == '\n') {
784 file->lineno++;
785 continue;
786 } else
787 lungetc(next);
788 } else if (c == quotec) {
789 *p = '\0';
790 break;
791 } else if (c == '\0') {
792 yyerror("invalid syntax");
793 return findeol();
795 if (p + 1 >= buf + sizeof(buf) - 1) {
796 yyerror("string too long");
797 return findeol();
799 *p++ = c;
801 yylval.v.string = strdup(buf);
802 if (yylval.v.string == NULL)
803 fatal("yylex: strdup");
804 return STRING;
807 #define allowed_to_end_number(x) \
808 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
810 if (c == '-' || isdigit(c)) {
811 do {
812 *p++ = c;
813 if ((size_t)(p-buf) >= sizeof(buf)) {
814 yyerror("string too long");
815 return findeol();
817 } while ((c = lgetc(0)) != EOF && isdigit(c));
818 lungetc(c);
819 if (p == buf + 1 && buf[0] == '-')
820 goto nodigits;
821 if (c == EOF || allowed_to_end_number(c)) {
822 const char *errstr = NULL;
824 *p = '\0';
825 yylval.v.number = strtonum(buf, LLONG_MIN,
826 LLONG_MAX, &errstr);
827 if (errstr) {
828 yyerror("\"%s\" invalid number: %s",
829 buf, errstr);
830 return findeol();
832 return NUM;
833 } else {
834 nodigits:
835 while (p > buf + 1)
836 lungetc(*--p);
837 c = *--p;
838 if (c == '-')
839 return c;
843 #define allowed_in_string(x) \
844 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
845 x != '{' && x != '}' && \
846 x != '!' && x != '=' && x != '#' && \
847 x != ',' && x != ';'))
849 if (isalnum(c) || c == ':' || c == '_') {
850 do {
851 *p++ = c;
852 if ((size_t)(p-buf) >= sizeof(buf)) {
853 yyerror("string too long");
854 return findeol();
856 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
857 lungetc(c);
858 *p = '\0';
859 if ((token = lookup(buf)) == STRING)
860 yylval.v.string = xstrdup(buf);
861 return token;
863 if (c == '\n') {
864 yylval.lineno = file->lineno;
865 file->lineno++;
867 if (c == EOF)
868 return 0;
869 return c;
872 struct file *
873 pushfile(const char *name, int secret)
875 struct file *nfile;
877 nfile = xcalloc(1, sizeof(*nfile));
878 nfile->name = xstrdup(name);
879 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
880 log_warn("can't open %s", nfile->name);
881 free(nfile->name);
882 free(nfile);
883 return NULL;
885 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
886 nfile->ungetsize = 16;
887 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
888 TAILQ_INSERT_TAIL(&files, nfile, entry);
889 return nfile;
892 int
893 popfile(void)
895 struct file *prev;
897 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
898 prev->errors += file->errors;
900 TAILQ_REMOVE(&files, file, entry);
901 fclose(file->stream);
902 free(file->name);
903 free(file->ungetbuf);
904 free(file);
905 file = prev;
906 return file ? 0 : EOF;
909 void
910 parse_conf(const char *filename)
912 struct sym *sym, *next;
914 file = pushfile(filename, 0);
915 if (file == NULL)
916 exit(1);
917 topfile = file;
919 yyparse();
920 errors = file->errors;
921 popfile();
923 /* Free macros and check which have not been used. */
924 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
925 /* TODO: warn if !sym->used */
926 if (!sym->persist) {
927 free(sym->name);
928 free(sym->val);
929 TAILQ_REMOVE(&symhead, sym, entry);
930 free(sym);
934 if (errors)
935 exit(1);
938 void
939 print_conf(void)
941 struct vhost *h;
942 /* struct location *l; */
943 /* struct envlist *e; */
944 /* struct alist *a; */
946 if (*conf.chroot != '\0')
947 printf("chroot \"%s\"\n", conf.chroot);
948 printf("ipv6 %s\n", conf.ipv6 ? "on" : "off");
949 /* XXX: defined mimes? */
950 printf("port %d\n", conf.port);
951 printf("prefork %d\n", conf.prefork);
952 /* XXX: protocols? */
953 if (*conf.user != '\0')
954 printf("user \"%s\"\n", conf.user);
956 TAILQ_FOREACH(h, &hosts, vhosts) {
957 printf("\nserver \"%s\" {\n", h->domain);
958 printf(" cert \"%s\"\n", h->cert);
959 printf(" key \"%s\"\n", h->key);
960 /* TODO: print locations... */
961 printf("}\n");
965 int
966 symset(const char *name, const char *val, int persist)
968 struct sym *sym;
970 TAILQ_FOREACH(sym, &symhead, entry) {
971 if (!strcmp(name, sym->name))
972 break;
975 if (sym != NULL) {
976 if (sym->persist)
977 return 0;
978 else {
979 free(sym->name);
980 free(sym->val);
981 TAILQ_REMOVE(&symhead, sym, entry);
982 free(sym);
986 sym = xcalloc(1, sizeof(*sym));
987 sym->name = xstrdup(name);
988 sym->val = xstrdup(val);
989 sym->used = 0;
990 sym->persist = persist;
992 TAILQ_INSERT_TAIL(&symhead, sym, entry);
993 return 0;
996 int
997 cmdline_symset(char *s)
999 char *sym, *val;
1000 int ret;
1002 if ((val = strrchr(s, '=')) == NULL)
1003 return -1;
1004 sym = xcalloc(1, val - s + 1);
1005 memcpy(sym, s, val - s);
1006 ret = symset(sym, val + 1, 1);
1007 free(sym);
1008 return ret;
1011 char *
1012 symget(const char *nam)
1014 struct sym *sym;
1016 TAILQ_FOREACH(sym, &symhead, entry) {
1017 if (strcmp(nam, sym->name) == 0) {
1018 sym->used = 1;
1019 return sym->val;
1022 return NULL;
1025 char *
1026 ensure_absolute_path(char *path)
1028 if (path == NULL || *path != '/')
1029 yyerror("not an absolute path: %s", path);
1030 return path;
1033 int
1034 check_block_code(int n)
1036 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1037 yyerror("invalid block code %d", n);
1038 return n;
1041 char *
1042 check_block_fmt(char *fmt)
1044 char *s;
1046 for (s = fmt; *s; ++s) {
1047 if (*s != '%')
1048 continue;
1049 switch (*++s) {
1050 case '%':
1051 case 'p':
1052 case 'q':
1053 case 'P':
1054 case 'N':
1055 break;
1056 default:
1057 yyerror("invalid format specifier %%%c", *s);
1061 return fmt;
1064 int
1065 check_strip_no(int n)
1067 if (n <= 0)
1068 yyerror("invalid strip number %d", n);
1069 return n;
1072 int
1073 check_port_num(int n)
1075 if (n <= 0 || n >= UINT16_MAX)
1076 yyerror("port number is %s: %d",
1077 n <= 0 ? "too small" : "too large",
1078 n);
1079 return n;
1082 int
1083 check_prefork_num(int n)
1085 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1086 yyerror("invalid prefork number %d", n);
1087 return n;
1090 void
1091 advance_loc(void)
1093 loc = new_location();
1094 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1097 void
1098 advance_proxy(void)
1100 proxy = new_proxy();
1101 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1104 void
1105 parsehp(char *str, char **host, const char **port, const char *def)
1107 char *at;
1108 const char *errstr;
1110 *host = str;
1112 if ((at = strchr(str, ':')) != NULL) {
1113 *at++ = '\0';
1114 *port = at;
1115 } else
1116 *port = def;
1118 strtonum(*port, 1, UINT16_MAX, &errstr);
1119 if (errstr != NULL)
1120 yyerror("port is %s: %s", errstr, *port);
1123 int
1124 fastcgi_conf(const char *path, const char *port)
1126 struct fcgi *f;
1127 int i = 0;
1129 TAILQ_FOREACH(f, &conf.fcgi, fcgi) {
1130 if (!strcmp(f->path, path) &&
1131 ((port == NULL && *f->port == '\0') ||
1132 !strcmp(f->port, port)))
1133 return i;
1134 ++i;
1137 f = xcalloc(1, sizeof(*f));
1138 f->id = i;
1139 (void)strlcpy(f->path, path, sizeof(f->path));
1140 if (port != NULL)
1141 (void)strlcpy(f->port, port, sizeof(f->port));
1142 TAILQ_INSERT_TAIL(&conf.fcgi, f, fcgi);
1144 return f->id;
1147 void
1148 add_param(char *name, char *val)
1150 struct envlist *e;
1151 struct envhead *h = &host->params;
1153 e = xcalloc(1, sizeof(*e));
1154 (void) strlcpy(e->name, name, sizeof(e->name));
1155 (void) strlcpy(e->value, val, sizeof(e->value));
1156 TAILQ_INSERT_TAIL(h, e, envs);
1159 int
1160 getservice(const char *n)
1162 struct servent *s;
1163 const char *errstr;
1164 long long llval;
1166 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1167 if (errstr) {
1168 s = getservbyname(n, "tcp");
1169 if (s == NULL)
1170 s = getservbyname(n, "udp");
1171 if (s == NULL)
1172 return (-1);
1173 return (ntohs(s->s_port));
1176 return ((unsigned short)llval);