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 struct conf *conf;
40 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
41 static struct file {
42 TAILQ_ENTRY(file) entry;
43 FILE *stream;
44 char *name;
45 size_t ungetpos;
46 size_t ungetsize;
47 u_char *ungetbuf;
48 int eof_reached;
49 int lineno;
50 int errors;
51 } *file, *topfile;
53 struct file *pushfile(const char *, int);
54 int popfile(void);
55 int yyparse(void);
56 int yylex(void);
57 void yyerror(const char *, ...)
58 __attribute__((__format__ (printf, 1, 2)))
59 __attribute__((__nonnull__ (1)));
60 void yywarn(const char *, ...)
61 __attribute__((__format__ (printf, 1, 2)))
62 __attribute__((__nonnull__ (1)));
63 int kw_cmp(const void *, const void *);
64 int lookup(char *);
65 int igetc(void);
66 int lgetc(int);
67 void lungetc(int);
68 int findeol(void);
70 /*
71 * #define YYDEBUG 1
72 * int yydebug = 1;
73 */
75 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
76 struct sym {
77 TAILQ_ENTRY(sym) entry;
78 int used;
79 int persist;
80 char *name;
81 char *val;
82 };
84 int symset(const char *, const char *, int);
85 char *symget(const char *);
87 struct vhost *new_vhost(void);
88 struct location *new_location(void);
89 struct proxy *new_proxy(void);
90 char *ensure_absolute_path(char*);
91 int check_block_code(int);
92 char *check_block_fmt(char*);
93 int check_strip_no(int);
94 int check_port_num(int);
95 int check_prefork_num(int);
96 void advance_loc(void);
97 void advance_proxy(void);
98 void parsehp(char *, char **, const char **, const char *);
99 int fastcgi_conf(const char *, const char *);
100 void add_param(char *, char *);
101 int getservice(const char *);
103 static struct vhost *host;
104 static struct location *loc;
105 static struct proxy *proxy;
106 static char *current_media;
107 static int errors;
109 typedef struct {
110 union {
111 char *string;
112 int number;
113 } v;
114 int lineno;
115 } YYSTYPE;
117 %}
119 /* for bison: */
120 /* %define parse.error verbose */
122 %token ALIAS AUTO
123 %token BLOCK
124 %token CA CERT CHROOT CLIENT
125 %token DEFAULT
126 %token FASTCGI FOR_HOST
127 %token INCLUDE INDEX IPV6
128 %token KEY
129 %token LANG LOCATION LOG
130 %token OCSP OFF ON
131 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
132 %token RELAY_TO REQUIRE RETURN ROOT
133 %token SERVER SNI STRIP
134 %token TCP TOEXT TYPE TYPES
135 %token USE_TLS USER
136 %token VERIFYNAME
138 %token ERROR
140 %token <v.string> STRING
141 %token <v.number> NUM
143 %type <v.number> bool proxy_port
144 %type <v.string> string numberstring
146 %%
148 conf : /* empty */
149 | conf include '\n'
150 | conf '\n'
151 | conf varset '\n'
152 | conf option '\n'
153 | conf vhost '\n'
154 | conf types '\n'
155 | conf error '\n' { file->errors++; }
158 include : INCLUDE STRING {
159 struct file *nfile;
161 if ((nfile = pushfile($2, 0)) == NULL) {
162 yyerror("failed to include file %s", $2);
163 free($2);
164 YYERROR;
166 free($2);
168 file = nfile;
169 lungetc('\n');
173 bool : ON { $$ = 1; }
174 | OFF { $$ = 0; }
177 string : string STRING {
178 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
179 free($1);
180 free($2);
181 yyerror("string: asprintf: %s", strerror(errno));
182 YYERROR;
184 free($1);
185 free($2);
187 | STRING
190 numberstring : NUM {
191 char *s;
192 if (asprintf(&s, "%d", $1) == -1) {
193 yyerror("asprintf: number");
194 YYERROR;
196 $$ = s;
198 | STRING
201 varset : STRING '=' string {
202 char *s = $1;
203 while (*s++) {
204 if (isspace((unsigned char)*s)) {
205 yyerror("macro name cannot contain "
206 "whitespaces");
207 free($1);
208 free($3);
209 YYERROR;
212 symset($1, $3, 0);
213 free($1);
214 free($3);
218 option : CHROOT string {
219 if (strlcpy(conf->chroot, $2, sizeof(conf->chroot)) >=
220 sizeof(conf->chroot))
221 yyerror("chroot path too long");
222 free($2);
224 | IPV6 bool { conf->ipv6 = $2; }
225 | PORT NUM { conf->port = check_port_num($2); }
226 | PREFORK NUM { conf->prefork = check_prefork_num($2); }
227 | PROTOCOLS string {
228 if (tls_config_parse_protocols(&conf->protos, $2) == -1)
229 yyerror("invalid protocols string \"%s\"", $2);
230 free($2);
232 | USER string {
233 if (strlcpy(conf->user, $2, sizeof(conf->user)) >=
234 sizeof(conf->user))
235 yyerror("user name too long");
236 free($2);
240 vhost : SERVER string {
241 host = new_vhost();
242 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
244 loc = new_location();
245 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
247 TAILQ_INIT(&host->proxies);
249 (void) strlcpy(loc->match, "*", sizeof(loc->match));
250 (void) strlcpy(host->domain, $2, sizeof(host->domain));
252 if (strstr($2, "xn--") != NULL) {
253 yywarn("\"%s\" looks like punycode: you "
254 "should use the decoded hostname", $2);
257 free($2);
258 } '{' optnl servbody '}' {
259 if (host->cert_path == NULL ||
260 host->key_path == NULL)
261 yyerror("invalid vhost definition: %s", $2);
263 | error '}' { yyerror("bad server directive"); }
266 servbody : /* empty */
267 | servbody servopt optnl
268 | servbody location optnl
269 | servbody proxy optnl
272 servopt : ALIAS string {
273 struct alist *a;
275 a = xcalloc(1, sizeof(*a));
276 (void) strlcpy(a->alias, $2, sizeof(a->alias));
277 free($2);
278 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
280 | CERT string {
281 ensure_absolute_path($2);
282 free(host->cert_path);
283 host->cert_path = $2;
285 | KEY string {
286 ensure_absolute_path($2);
287 free(host->key_path);
288 host->key_path = $2;
290 | OCSP string {
291 ensure_absolute_path($2);
292 free(host->ocsp_path);
293 host->ocsp_path = $2;
295 | PARAM string '=' string {
296 add_param($2, $4);
298 | locopt
301 proxy : PROXY { advance_proxy(); }
302 proxy_matches '{' optnl proxy_opts '}' {
303 if (*proxy->host == '\0')
304 yyerror("invalid proxy block: missing `relay-to' option");
306 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
307 (proxy->cert_path != NULL && proxy->key_path == NULL))
308 yyerror("invalid proxy block: missing cert or key");
312 proxy_matches : /* empty */
313 | proxy_matches proxy_match
316 proxy_port : /* empty */ { $$ = 1965; }
317 | PORT STRING {
318 if (($$ = getservice($2)) == -1)
319 yyerror("invalid port number %s", $2);
320 free($2);
322 | PORT NUM { $$ = $2; }
325 proxy_match : PROTO string {
326 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
327 free($2);
329 | FOR_HOST string proxy_port {
330 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
331 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
332 "%d", $3);
333 free($2);
337 proxy_opts : /* empty */
338 | proxy_opts proxy_opt optnl
341 proxy_opt : CERT string {
342 free(proxy->cert);
343 ensure_absolute_path($2);
344 proxy->cert_path = $2;
346 | KEY string {
347 free(proxy->key);
348 ensure_absolute_path($2);
349 proxy->key_path = $2;
351 | PROTOCOLS string {
352 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
353 yyerror("invalid protocols string \"%s\"", $2);
354 free($2);
356 | RELAY_TO string proxy_port {
357 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
358 (void) snprintf(proxy->port, sizeof(proxy->port),
359 "%d", $3);
360 free($2);
362 | REQUIRE CLIENT CA string {
363 ensure_absolute_path($4);
364 proxy->reqca_path = $4;
366 | SNI string {
367 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
368 free($2);
370 | USE_TLS bool {
371 proxy->notls = !$2;
373 | VERIFYNAME bool {
374 proxy->noverifyname = !$2;
378 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
379 /* drop the starting '/' if any */
380 if (*$3 == '/')
381 memmove($3, $3+1, strlen($3));
382 (void) strlcpy(loc->match, $3, sizeof(loc->match));
383 free($3);
385 | error '}'
388 locopts : /* empty */
389 | locopts locopt optnl
392 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
393 | BLOCK RETURN NUM string {
394 check_block_fmt($4);
395 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
396 loc->block_code = check_block_code($3);
397 free($4);
399 | BLOCK RETURN NUM {
400 (void) strlcpy(loc->block_fmt, "temporary failure",
401 sizeof(loc->block_fmt));
402 loc->block_code = check_block_code($3);
403 if ($3 >= 30 && $3 < 40)
404 yyerror("missing `meta' for block return %d", $3);
406 | BLOCK {
407 (void) strlcpy(loc->block_fmt, "temporary failure",
408 sizeof(loc->block_fmt));
409 loc->block_code = 40;
411 | DEFAULT TYPE string {
412 (void) strlcpy(loc->default_mime, $3,
413 sizeof(loc->default_mime));
414 free($3);
416 | FASTCGI fastcgi
417 | INDEX string {
418 (void) strlcpy(loc->index, $2, sizeof(loc->index));
419 free($2);
421 | LANG string {
422 (void) strlcpy(loc->lang, $2,
423 sizeof(loc->lang));
424 free($2);
426 | LOG bool { loc->disable_log = !$2; }
427 | REQUIRE CLIENT CA string {
428 ensure_absolute_path($4);
429 loc->reqca_path = $4;
431 | ROOT string {
432 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
433 free($2);
435 | STRIP NUM { loc->strip = check_strip_no($2); }
438 fastcgi : string {
439 loc->fcgi = fastcgi_conf($1, NULL);
440 free($1);
442 | TCP string PORT NUM {
443 char *c;
444 if (asprintf(&c, "%d", $4) == -1)
445 fatal("asprintf");
446 loc->fcgi = fastcgi_conf($2, c);
447 free($2);
449 | TCP string {
450 loc->fcgi = fastcgi_conf($2, "9000");
451 free($2);
453 | TCP string PORT string {
454 loc->fcgi = fastcgi_conf($2, $4);
455 free($2);
456 free($4);
460 types : TYPES '{' optnl mediaopts_l '}' ;
462 mediaopts_l : mediaopts_l mediaoptsl nl
463 | mediaoptsl nl
466 mediaoptsl : STRING {
467 free(current_media);
468 current_media = $1;
469 } medianames_l optsemicolon
470 | include
473 medianames_l : medianames_l medianamesl
474 | medianamesl
477 medianamesl : numberstring {
478 if (add_mime(&conf->mime, current_media, $1) == -1)
479 fatal("add_mime");
480 free($1);
484 nl : '\n' optnl
487 optnl : '\n' optnl /* zero or more newlines */
488 | ';' optnl /* semicolons too */
489 | /*empty*/
492 optsemicolon : ';'
496 %%
498 static const struct keyword {
499 const char *word;
500 int token;
501 } keywords[] = {
502 /* these MUST be sorted */
503 {"alias", ALIAS},
504 {"auto", AUTO},
505 {"block", BLOCK},
506 {"ca", CA},
507 {"cert", CERT},
508 {"chroot", CHROOT},
509 {"client", CLIENT},
510 {"default", DEFAULT},
511 {"fastcgi", FASTCGI},
512 {"for-host", FOR_HOST},
513 {"include", INCLUDE},
514 {"index", INDEX},
515 {"ipv6", IPV6},
516 {"key", KEY},
517 {"lang", LANG},
518 {"location", LOCATION},
519 {"log", LOG},
520 {"ocsp", OCSP},
521 {"off", OFF},
522 {"on", ON},
523 {"param", PARAM},
524 {"port", PORT},
525 {"prefork", PREFORK},
526 {"proto", PROTO},
527 {"protocols", PROTOCOLS},
528 {"proxy", PROXY},
529 {"relay-to", RELAY_TO},
530 {"require", REQUIRE},
531 {"return", RETURN},
532 {"root", ROOT},
533 {"server", SERVER},
534 {"sni", SNI},
535 {"strip", STRIP},
536 {"tcp", TCP},
537 {"to-ext", TOEXT},
538 {"type", TYPE},
539 {"types", TYPES},
540 {"use-tls", USE_TLS},
541 {"user", USER},
542 {"verifyname", VERIFYNAME},
543 };
545 void
546 yyerror(const char *msg, ...)
548 va_list ap;
550 file->errors++;
552 va_start(ap, msg);
553 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
554 vfprintf(stderr, msg, ap);
555 fprintf(stderr, "\n");
556 va_end(ap);
559 void
560 yywarn(const char *msg, ...)
562 va_list ap;
564 va_start(ap, msg);
565 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
566 vfprintf(stderr, msg, ap);
567 fprintf(stderr, "\n");
568 va_end(ap);
571 int
572 kw_cmp(const void *k, const void *e)
574 return strcmp(k, ((struct keyword *)e)->word);
577 int
578 lookup(char *s)
580 const struct keyword *p;
582 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
583 sizeof(keywords[0]), kw_cmp);
585 if (p)
586 return p->token;
587 else
588 return STRING;
591 #define START_EXPAND 1
592 #define DONE_EXPAND 2
594 static int expanding;
596 int
597 igetc(void)
599 int c;
601 while (1) {
602 if (file->ungetpos > 0)
603 c = file->ungetbuf[--file->ungetpos];
604 else
605 c = getc(file->stream);
607 if (c == START_EXPAND)
608 expanding = 1;
609 else if (c == DONE_EXPAND)
610 expanding = 0;
611 else
612 break;
614 return c;
617 int
618 lgetc(int quotec)
620 int c, next;
622 if (quotec) {
623 if ((c = igetc()) == EOF) {
624 yyerror("reached end of file while parsing "
625 "quoted string");
626 if (file == topfile || popfile() == EOF)
627 return EOF;
628 return quotec;
630 return c;
633 while ((c = igetc()) == '\\') {
634 next = igetc();
635 if (next != '\n') {
636 c = next;
637 break;
639 yylval.lineno = file->lineno;
640 file->lineno++;
643 if (c == EOF) {
644 /*
645 * Fake EOL when hit EOF for the first time. This gets line
646 * count right if last line in included file is syntactically
647 * invalid and has no newline.
648 */
649 if (file->eof_reached == 0) {
650 file->eof_reached = 1;
651 return '\n';
653 while (c == EOF) {
654 if (file == topfile || popfile() == EOF)
655 return EOF;
656 c = igetc();
659 return c;
662 void
663 lungetc(int c)
665 if (c == EOF)
666 return;
668 if (file->ungetpos >= file->ungetsize) {
669 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
670 if (p == NULL)
671 fatal("lungetc");
672 file->ungetbuf = p;
673 file->ungetsize *= 2;
675 file->ungetbuf[file->ungetpos++] = c;
678 int
679 findeol(void)
681 int c;
683 /* Skip to either EOF or the first real EOL. */
684 while (1) {
685 c = lgetc(0);
686 if (c == '\n') {
687 file->lineno++;
688 break;
690 if (c == EOF)
691 break;
693 return ERROR;
696 int
697 yylex(void)
699 char buf[8096];
700 char *p, *val;
701 int quotec, next, c;
702 int token;
704 top:
705 p = buf;
706 while ((c = lgetc(0)) == ' ' || c == '\t')
707 ; /* nothing */
709 yylval.lineno = file->lineno;
710 if (c == '#')
711 while ((c = lgetc(0)) != '\n' && c != EOF)
712 ; /* nothing */
713 if (c == '$' && !expanding) {
714 while (1) {
715 if ((c = lgetc(0)) == EOF)
716 return 0;
717 if (p + 1 >= buf + sizeof(buf) -1) {
718 yyerror("string too long");
719 return findeol();
721 if (isalnum(c) || c == '_') {
722 *p++ = c;
723 continue;
725 *p = '\0';
726 lungetc(c);
727 break;
729 val = symget(buf);
730 if (val == NULL) {
731 yyerror("macro `%s' not defined", buf);
732 return findeol();
734 yylval.v.string = xstrdup(val);
735 return STRING;
737 if (c == '@' && !expanding) {
738 while (1) {
739 if ((c = lgetc(0)) == EOF)
740 return 0;
742 if (p + 1 >= buf + sizeof(buf) - 1) {
743 yyerror("string too long");
744 return findeol();
746 if (isalnum(c) || c == '_') {
747 *p++ = c;
748 continue;
750 *p = '\0';
751 lungetc(c);
752 break;
754 val = symget(buf);
755 if (val == NULL) {
756 yyerror("macro '%s' not defined", buf);
757 return findeol();
759 p = val + strlen(val) - 1;
760 lungetc(DONE_EXPAND);
761 while (p >= val) {
762 lungetc(*p);
763 p--;
765 lungetc(START_EXPAND);
766 goto top;
769 switch (c) {
770 case '\'':
771 case '"':
772 quotec = c;
773 while (1) {
774 if ((c = lgetc(quotec)) == EOF)
775 return 0;
776 if (c == '\n') {
777 file->lineno++;
778 continue;
779 } else if (c == '\\') {
780 if ((next = lgetc(quotec)) == EOF)
781 return (0);
782 if (next == quotec || next == ' ' ||
783 next == '\t')
784 c = next;
785 else if (next == '\n') {
786 file->lineno++;
787 continue;
788 } else
789 lungetc(next);
790 } else if (c == quotec) {
791 *p = '\0';
792 break;
793 } else if (c == '\0') {
794 yyerror("invalid syntax");
795 return findeol();
797 if (p + 1 >= buf + sizeof(buf) - 1) {
798 yyerror("string too long");
799 return findeol();
801 *p++ = c;
803 yylval.v.string = strdup(buf);
804 if (yylval.v.string == NULL)
805 fatal("yylex: strdup");
806 return STRING;
809 #define allowed_to_end_number(x) \
810 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
812 if (c == '-' || isdigit(c)) {
813 do {
814 *p++ = c;
815 if ((size_t)(p-buf) >= sizeof(buf)) {
816 yyerror("string too long");
817 return findeol();
819 } while ((c = lgetc(0)) != EOF && isdigit(c));
820 lungetc(c);
821 if (p == buf + 1 && buf[0] == '-')
822 goto nodigits;
823 if (c == EOF || allowed_to_end_number(c)) {
824 const char *errstr = NULL;
826 *p = '\0';
827 yylval.v.number = strtonum(buf, LLONG_MIN,
828 LLONG_MAX, &errstr);
829 if (errstr) {
830 yyerror("\"%s\" invalid number: %s",
831 buf, errstr);
832 return findeol();
834 return NUM;
835 } else {
836 nodigits:
837 while (p > buf + 1)
838 lungetc(*--p);
839 c = *--p;
840 if (c == '-')
841 return c;
845 #define allowed_in_string(x) \
846 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
847 x != '{' && x != '}' && \
848 x != '!' && x != '=' && x != '#' && \
849 x != ',' && x != ';'))
851 if (isalnum(c) || c == ':' || c == '_') {
852 do {
853 *p++ = c;
854 if ((size_t)(p-buf) >= sizeof(buf)) {
855 yyerror("string too long");
856 return findeol();
858 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
859 lungetc(c);
860 *p = '\0';
861 if ((token = lookup(buf)) == STRING)
862 yylval.v.string = xstrdup(buf);
863 return token;
865 if (c == '\n') {
866 yylval.lineno = file->lineno;
867 file->lineno++;
869 if (c == EOF)
870 return 0;
871 return c;
874 struct file *
875 pushfile(const char *name, int secret)
877 struct file *nfile;
879 nfile = xcalloc(1, sizeof(*nfile));
880 nfile->name = xstrdup(name);
881 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
882 log_warn("can't open %s", nfile->name);
883 free(nfile->name);
884 free(nfile);
885 return NULL;
887 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
888 nfile->ungetsize = 16;
889 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
890 TAILQ_INSERT_TAIL(&files, nfile, entry);
891 return nfile;
894 int
895 popfile(void)
897 struct file *prev;
899 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
900 prev->errors += file->errors;
902 TAILQ_REMOVE(&files, file, entry);
903 fclose(file->stream);
904 free(file->name);
905 free(file->ungetbuf);
906 free(file);
907 file = prev;
908 return file ? 0 : EOF;
911 int
912 parse_conf(struct conf *c, const char *filename)
914 struct sym *sym, *next;
916 conf = c;
918 file = pushfile(filename, 0);
919 if (file == NULL)
920 return -1;
921 topfile = file;
923 yyparse();
924 errors = file->errors;
925 popfile();
927 /* Free macros and check which have not been used. */
928 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
929 /* TODO: warn if !sym->used */
930 if (!sym->persist) {
931 free(sym->name);
932 free(sym->val);
933 TAILQ_REMOVE(&symhead, sym, entry);
934 free(sym);
938 if (errors)
939 return -1;
940 return 0;
943 int
944 symset(const char *name, const char *val, int persist)
946 struct sym *sym;
948 TAILQ_FOREACH(sym, &symhead, entry) {
949 if (!strcmp(name, sym->name))
950 break;
953 if (sym != NULL) {
954 if (sym->persist)
955 return 0;
956 else {
957 free(sym->name);
958 free(sym->val);
959 TAILQ_REMOVE(&symhead, sym, entry);
960 free(sym);
964 sym = xcalloc(1, sizeof(*sym));
965 sym->name = xstrdup(name);
966 sym->val = xstrdup(val);
967 sym->used = 0;
968 sym->persist = persist;
970 TAILQ_INSERT_TAIL(&symhead, sym, entry);
971 return 0;
974 int
975 cmdline_symset(char *s)
977 char *sym, *val;
978 int ret;
980 if ((val = strrchr(s, '=')) == NULL)
981 return -1;
982 sym = xcalloc(1, val - s + 1);
983 memcpy(sym, s, val - s);
984 ret = symset(sym, val + 1, 1);
985 free(sym);
986 return ret;
989 char *
990 symget(const char *nam)
992 struct sym *sym;
994 TAILQ_FOREACH(sym, &symhead, entry) {
995 if (strcmp(nam, sym->name) == 0) {
996 sym->used = 1;
997 return sym->val;
1000 return NULL;
1003 char *
1004 ensure_absolute_path(char *path)
1006 if (path == NULL || *path != '/')
1007 yyerror("not an absolute path: %s", path);
1008 return path;
1011 int
1012 check_block_code(int n)
1014 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1015 yyerror("invalid block code %d", n);
1016 return n;
1019 char *
1020 check_block_fmt(char *fmt)
1022 char *s;
1024 for (s = fmt; *s; ++s) {
1025 if (*s != '%')
1026 continue;
1027 switch (*++s) {
1028 case '%':
1029 case 'p':
1030 case 'q':
1031 case 'P':
1032 case 'N':
1033 break;
1034 default:
1035 yyerror("invalid format specifier %%%c", *s);
1039 return fmt;
1042 int
1043 check_strip_no(int n)
1045 if (n <= 0)
1046 yyerror("invalid strip number %d", n);
1047 return n;
1050 int
1051 check_port_num(int n)
1053 if (n <= 0 || n >= UINT16_MAX)
1054 yyerror("port number is %s: %d",
1055 n <= 0 ? "too small" : "too large",
1056 n);
1057 return n;
1060 int
1061 check_prefork_num(int n)
1063 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1064 yyerror("invalid prefork number %d", n);
1065 return n;
1068 void
1069 advance_loc(void)
1071 loc = new_location();
1072 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1075 void
1076 advance_proxy(void)
1078 proxy = new_proxy();
1079 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1082 void
1083 parsehp(char *str, char **host, const char **port, const char *def)
1085 char *at;
1086 const char *errstr;
1088 *host = str;
1090 if ((at = strchr(str, ':')) != NULL) {
1091 *at++ = '\0';
1092 *port = at;
1093 } else
1094 *port = def;
1096 strtonum(*port, 1, UINT16_MAX, &errstr);
1097 if (errstr != NULL)
1098 yyerror("port is %s: %s", errstr, *port);
1101 int
1102 fastcgi_conf(const char *path, const char *port)
1104 struct fcgi *f;
1105 int i = 0;
1107 TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1108 if (!strcmp(f->path, path) &&
1109 ((port == NULL && *f->port == '\0') ||
1110 !strcmp(f->port, port)))
1111 return i;
1112 ++i;
1115 f = xcalloc(1, sizeof(*f));
1116 f->id = i;
1117 (void)strlcpy(f->path, path, sizeof(f->path));
1118 if (port != NULL)
1119 (void)strlcpy(f->port, port, sizeof(f->port));
1120 TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1122 return f->id;
1125 void
1126 add_param(char *name, char *val)
1128 struct envlist *e;
1129 struct envhead *h = &host->params;
1131 e = xcalloc(1, sizeof(*e));
1132 (void) strlcpy(e->name, name, sizeof(e->name));
1133 (void) strlcpy(e->value, val, sizeof(e->value));
1134 TAILQ_INSERT_TAIL(h, e, envs);
1137 int
1138 getservice(const char *n)
1140 struct servent *s;
1141 const char *errstr;
1142 long long llval;
1144 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1145 if (errstr) {
1146 s = getservbyname(n, "tcp");
1147 if (s == NULL)
1148 s = getservbyname(n, "udp");
1149 if (s == NULL)
1150 return (-1);
1151 return (ntohs(s->s_port));
1154 return ((unsigned short)llval);