3 * - configuration file parsing
4 * - management of global state
7 * This file is part of adns, which is
8 * Copyright (C) 1997-2000,2003,2006 Ian Jackson
9 * Copyright (C) 1999-2000,2003,2006 Tony Finch
10 * Copyright (C) 1991 Massachusetts Institute of Technology
11 * (See the file INSTALL for full details.)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
42 static void readconfig(adns_state ads
, const char *filename
, int warnmissing
);
44 static void addserver(adns_state ads
, const struct sockaddr
*sa
, int n
) {
47 char buf
[ADNS_ADDR2TEXT_BUFLEN
];
49 for (i
=0; i
<ads
->nservers
; i
++) {
50 if (adns__sockaddr_equal_p(sa
, &ads
->servers
[i
].addr
.sa
)) {
51 adns__debug(ads
,-1,0,"duplicate nameserver %s ignored",
52 adns__sockaddr_ntoa(sa
, buf
));
57 if (ads
->nservers
>=MAXSERVERS
) {
58 adns__diag(ads
,-1,0,"too many nameservers, ignoring %s",
59 adns__sockaddr_ntoa(sa
, buf
));
63 ss
= ads
->servers
+ads
->nservers
;
64 assert(n
<= sizeof(ss
->addr
));
66 memcpy(&ss
->addr
, sa
, n
);
70 static void freesearchlist(adns_state ads
) {
71 if (ads
->nsearchlist
) free(*ads
->searchlist
);
72 free(ads
->searchlist
);
75 static void saveerr(adns_state ads
, int en
) {
76 if (!ads
->configerrno
) ads
->configerrno
= en
;
79 static void configparseerr(adns_state ads
, const char *fn
, int lno
,
80 const char *fmt
, ...) {
84 if (!ads
->logfn
|| (ads
->iflags
& adns_if_noerrprint
)) return;
86 if (lno
==-1) adns__lprintf(ads
,"adns: %s: ",fn
);
87 else adns__lprintf(ads
,"adns: %s:%d: ",fn
,lno
);
89 adns__vlprintf(ads
,fmt
,al
);
91 adns__lprintf(ads
,"\n");
94 static int nextword(const char **bufp_io
, const char **word_r
, int *l_r
) {
98 while (ctype_whitespace(*p
)) p
++;
102 while (*q
&& !ctype_whitespace(*q
)) q
++;
111 static void ccf_nameserver(adns_state ads
, const char *fn
,
112 int lno
, const char *buf
) {
114 char addrbuf
[ADNS_ADDR2TEXT_BUFLEN
];
117 a
.len
= sizeof(a
.addr
);
118 err
= adns_text2addr(buf
,DNS_PORT
, 0, &a
.addr
.sa
,&a
.len
);
123 configparseerr(ads
,fn
,lno
,"invalid nameserver address `%s'",buf
);
126 configparseerr(ads
,fn
,lno
,"failed to parse nameserver address `%s': %s",
130 adns__debug(ads
,-1,0,"using nameserver %s",
131 adns__sockaddr_ntoa(&a
.addr
.sa
, addrbuf
));
132 addserver(ads
,&a
.addr
.sa
,a
.len
);
135 static void ccf_search(adns_state ads
, const char *fn
,
136 int lno
, const char *buf
) {
137 const char *bufp
, *word
;
138 char *newchars
, **newptrs
, **pp
;
146 while (nextword(&bufp
,&word
,&l
)) { count
++; tl
+= l
+1; }
148 newptrs
= malloc(sizeof(char*)*count
);
149 if (!newptrs
) { saveerr(ads
,errno
); return; }
151 newchars
= malloc(tl
);
152 if (!newchars
) { saveerr(ads
,errno
); free(newptrs
); return; }
156 while (nextword(&bufp
,&word
,&l
)) {
158 memcpy(newchars
,word
,l
);
164 ads
->nsearchlist
= count
;
165 ads
->searchlist
= newptrs
;
168 static int gen_pton(const char *text
, int *af_io
, union gen_addr
*a
) {
172 addr
.len
= sizeof(addr
.addr
);
173 err
= adns_text2addr(text
,0, adns_qf_addrlit_scope_forbid
,
174 &addr
.addr
.sa
, &addr
.len
);
175 if (err
) { assert(err
== EINVAL
); return 0; }
176 if (*af_io
== AF_UNSPEC
) *af_io
= addr
.addr
.sa
.sa_family
;
177 else if (*af_io
!= addr
.addr
.sa
.sa_family
) return 0;
178 adns__sockaddr_extract(&addr
.addr
.sa
, a
, 0);
182 static void ccf_sortlist(adns_state ads
, const char *fn
,
183 int lno
, const char *buf
) {
185 char tbuf
[200], *slash
, *ep
;
186 const char *maskwhat
;
195 while (nextword(&buf
,&word
,&l
)) {
196 if (ads
->nsortlist
>= MAXSORTLIST
) {
197 adns__diag(ads
,-1,0,"too many sortlist entries,"
198 " ignoring %.*s onwards",l
,word
);
202 if (l
>= sizeof(tbuf
)) {
203 configparseerr(ads
,fn
,lno
,"sortlist entry `%.*s' too long",l
,word
);
207 memcpy(tbuf
,word
,l
); tbuf
[l
]= 0;
208 slash
= strchr(tbuf
,'/');
209 if (slash
) *slash
++= 0;
211 sl
= &ads
->sortlist
[ads
->nsortlist
];
213 if (!gen_pton(tbuf
, &af
, &sl
->base
)) {
214 configparseerr(ads
,fn
,lno
,"invalid address `%s' in sortlist",tbuf
);
219 if (slash
[strspn(slash
, "0123456789")]) {
221 if (!gen_pton(slash
,&af
,&sl
->mask
)) {
222 configparseerr(ads
,fn
,lno
,"invalid mask `%s' in sortlist",slash
);
226 maskwhat
= "prefix length";
227 initial
= strtoul(slash
,&ep
,10);
228 if (*ep
|| initial
>adns__addr_width(af
)) {
229 configparseerr(ads
,fn
,lno
,"mask length `%s' invalid",slash
);
232 adns__prefix_mask(af
, initial
, &sl
->mask
);
235 maskwhat
= "implied prefix length";
236 initial
= adns__guess_prefix_length(af
, &sl
->base
);
238 configparseerr(ads
,fn
,lno
, "network address `%s'"
239 " in sortlist is not in classed ranges,"
240 " must specify mask explicitly", tbuf
);
243 adns__prefix_mask(af
, initial
, &sl
->mask
);
246 if (!adns__addr_match_p(af
,&sl
->base
, af
,&sl
->base
,&sl
->mask
)) {
248 configparseerr(ads
,fn
,lno
, "%s %d in sortlist"
249 " overlaps address `%s'",maskwhat
,initial
,tbuf
);
251 configparseerr(ads
,fn
,lno
, "%s `%s' in sortlist"
252 " overlaps address `%s'",maskwhat
,slash
,tbuf
);
262 static void ccf_options(adns_state ads
, const char *fn
,
263 int lno
, const char *buf
) {
271 while (nextword(&buf
,&word
,&l
)) {
272 if (l
==5 && !memcmp(word
,"debug",5)) {
273 ads
->iflags
|= adns_if_debug
;
276 if (l
>=6 && !memcmp(word
,"ndots:",6)) {
277 v
= strtoul(word
+6,&ep
,10);
278 if (l
==6 || ep
!= word
+l
|| v
> INT_MAX
) {
279 configparseerr(ads
,fn
,lno
,"option `%.*s' malformed"
280 " or has bad value",l
,word
);
286 if (l
>=12 && !memcmp(word
,"adns_checkc:",12)) {
287 if (!strcmp(word
+12,"none")) {
288 ads
->iflags
&= ~adns_if_checkc_freq
;
289 ads
->iflags
|= adns_if_checkc_entex
;
290 } else if (!strcmp(word
+12,"entex")) {
291 ads
->iflags
&= ~adns_if_checkc_freq
;
292 ads
->iflags
|= adns_if_checkc_entex
;
293 } else if (!strcmp(word
+12,"freq")) {
294 ads
->iflags
|= adns_if_checkc_freq
;
296 configparseerr(ads
,fn
,lno
, "option adns_checkc has bad value `%s' "
297 "(must be none, entex or freq", word
+12);
301 adns__diag(ads
,-1,0,"%s:%d: unknown option `%.*s'", fn
,lno
, l
,word
);
305 static void ccf_clearnss(adns_state ads
, const char *fn
,
306 int lno
, const char *buf
) {
310 static void ccf_include(adns_state ads
, const char *fn
,
311 int lno
, const char *buf
) {
313 configparseerr(ads
,fn
,lno
,"`include' directive with no filename");
316 readconfig(ads
,buf
,1);
319 static void ccf_lookup(adns_state ads
, const char *fn
, int lno
,
326 configparseerr(ads
,fn
,lno
,"`lookup' directive with no databases");
330 while (nextword(&buf
,&word
,&l
)) {
331 if (l
==4 && !memcmp(word
,"bind",4)) {
333 } else if (l
==4 && !memcmp(word
,"file",4)) {
334 /* ignore this and hope /etc/hosts is not essential */
335 } else if (l
==2 && !memcmp(word
,"yp",2)) {
336 adns__diag(ads
,-1,0,"%s:%d: yp lookups not supported by adns", fn
,lno
);
339 adns__diag(ads
,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
345 adns__diag(ads
,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn
,lno
);
348 static const struct configcommandinfo
{
350 void (*fn
)(adns_state ads
, const char *fn
, int lno
, const char *buf
);
351 } configcommandinfos
[]= {
352 { "nameserver", ccf_nameserver
},
353 { "domain", ccf_search
},
354 { "search", ccf_search
},
355 { "sortlist", ccf_sortlist
},
356 { "options", ccf_options
},
357 { "clearnameservers", ccf_clearnss
},
358 { "include", ccf_include
},
359 { "lookup", ccf_lookup
}, /* OpenBSD */
368 static int gl_file(adns_state ads
, getline_ctx
*src_io
, const char *filename
,
369 int lno
, char *buf
, int buflen
) {
370 FILE *file
= src_io
->file
;
378 for (;;) { /* loop over chars */
380 adns__diag(ads
,-1,0,"%s:%d: line too long, ignored",filename
,lno
);
385 adns__diag(ads
,-1,0,"%s:%d: line contains nul, ignored",filename
,lno
);
387 } else if (c
== '\n') {
389 } else if (c
== EOF
) {
392 adns__diag(ads
,-1,0,"%s:%d: read error: %s",
393 filename
,lno
,strerror(errno
));
409 while ((c
= getc(file
)) != EOF
&& c
!= '\n');
413 static int gl_text(adns_state ads
, getline_ctx
*src_io
, const char *filename
,
414 int lno
, char *buf
, int buflen
) {
415 const char *cp
= src_io
->text
;
418 if (!cp
|| !*cp
) return -1;
420 if (*cp
== ';' || *cp
== '\n') cp
++;
421 l
= strcspn(cp
,";\n");
425 adns__diag(ads
,-1,0,"%s:%d: line too long, ignored",filename
,lno
);
435 static void readconfiggeneric(adns_state ads
, const char *filename
,
436 int (*getline
)(adns_state ads
, getline_ctx
*,
437 const char *filename
, int lno
,
438 char *buf
, int buflen
),
439 /* Returns >=0 for success, -1 for EOF or error
440 * (error will have been reported), or -2 for
441 * bad line was encountered, try again.
443 getline_ctx gl_ctx
) {
444 char linebuf
[2000], *p
, *q
;
446 const struct configcommandinfo
*ccip
;
449 (l
= getline(ads
,&gl_ctx
, filename
,lno
, linebuf
,sizeof(linebuf
))) != -1;
451 if (l
== -2) continue;
452 while (l
>0 && ctype_whitespace(linebuf
[l
-1])) l
--;
455 while (ctype_whitespace(*p
)) p
++;
456 if (*p
== '#' || *p
== ';' || !*p
) continue;
458 while (*q
&& !ctype_whitespace(*q
)) q
++;
460 for (ccip
=configcommandinfos
;
462 !(strlen(ccip
->name
)==dirl
&& !memcmp(ccip
->name
,p
,q
-p
));
465 adns__diag(ads
,-1,0,"%s:%d: unknown configuration directive `%.*s'",
466 filename
,lno
,(int)(q
-p
),p
);
469 while (ctype_whitespace(*q
)) q
++;
470 ccip
->fn(ads
,filename
,lno
,q
);
474 static const char *instrum_getenv(adns_state ads
, const char *envvar
) {
477 value
= getenv(envvar
);
478 if (!value
) adns__debug(ads
,-1,0,"environment variable %s not set",envvar
);
479 else adns__debug(ads
,-1,0,"environment variable %s"
480 " set to `%s'",envvar
,value
);
484 static void readconfig(adns_state ads
, const char *filename
, int warnmissing
) {
487 gl_ctx
.file
= fopen(filename
,"r");
489 if (errno
== ENOENT
) {
491 adns__debug(ads
,-1,0, "configuration file"
492 " `%s' does not exist",filename
);
496 adns__diag(ads
,-1,0,"cannot open configuration file `%s': %s",
497 filename
,strerror(errno
));
501 readconfiggeneric(ads
,filename
,gl_file
,gl_ctx
);
506 static void readconfigtext(adns_state ads
, const char *text
,
507 const char *showname
) {
511 readconfiggeneric(ads
,showname
,gl_text
,gl_ctx
);
514 static void readconfigenv(adns_state ads
, const char *envvar
) {
515 const char *filename
;
517 if (ads
->iflags
& adns_if_noenv
) {
518 adns__debug(ads
,-1,0,"not checking environment variable `%s'",envvar
);
521 filename
= instrum_getenv(ads
,envvar
);
522 if (filename
) readconfig(ads
,filename
,1);
525 static void readconfigenvtext(adns_state ads
, const char *envvar
) {
526 const char *textdata
;
528 if (ads
->iflags
& adns_if_noenv
) {
529 adns__debug(ads
,-1,0,"not checking environment variable `%s'",envvar
);
532 textdata
= instrum_getenv(ads
,envvar
);
533 if (textdata
) readconfigtext(ads
,textdata
,envvar
);
537 int adns__setnonblock(adns_state ads
, int fd
) {
540 r
= fcntl(fd
,F_GETFL
,0); if (r
<0) return errno
;
542 r
= fcntl(fd
,F_SETFL
,r
); if (r
<0) return errno
;
546 static int init_begin(adns_state
*ads_r
, adns_initflags flags
,
547 adns_logcallbackfn
*logfn
, void *logfndata
) {
551 ads
= malloc(sizeof(*ads
)); if (!ads
) return errno
;
555 ads
->logfndata
= logfndata
;
557 LIST_INIT(ads
->udpw
);
558 LIST_INIT(ads
->tcpw
);
559 LIST_INIT(ads
->childw
);
560 LIST_INIT(ads
->output
);
565 adns__vbuf_init(&ads
->tcpsend
);
566 adns__vbuf_init(&ads
->tcprecv
);
567 ads
->tcprecv_skip
= 0;
568 ads
->nservers
= ads
->nsortlist
= ads
->nsearchlist
= ads
->tcpserver
= 0;
570 ads
->tcpstate
= server_disconnected
;
571 timerclear(&ads
->tcptimeout
);
575 ads
->rand48xsubi
[0]= pid
;
576 ads
->rand48xsubi
[1]= (unsigned long)pid
>> 16;
577 ads
->rand48xsubi
[2]= pid
^ ((unsigned long)pid
>> 16);
583 static int init_finish(adns_state ads
) {
584 struct sockaddr_in sin
;
585 struct protoent
*proto
;
586 struct udpsocket
*udp
;
590 if (!ads
->nservers
) {
591 if (ads
->logfn
&& ads
->iflags
& adns_if_debug
)
592 adns__lprintf(ads
,"adns: no nameservers, using IPv4 localhost\n");
593 memset(&sin
, 0, sizeof(sin
));
594 sin
.sin_family
= AF_INET
;
595 sin
.sin_port
= htons(DNS_PORT
);
596 sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
597 addserver(ads
,(struct sockaddr
*)&sin
, sizeof(sin
));
600 proto
= getprotobyname("udp"); if (!proto
) { r
= ENOPROTOOPT
; goto x_free
; }
602 for (i
=0; i
<ads
->nservers
; i
++) {
603 if (adns__udpsocket_by_af(ads
, ads
->servers
[i
].addr
.sa
.sa_family
))
605 assert(ads
->nudp
< MAXUDP
);
606 udp
= &ads
->udpsocket
[ads
->nudp
];
607 udp
->af
= ads
->servers
[i
].addr
.sa
.sa_family
;
608 udp
->fd
= socket(udp
->af
,SOCK_DGRAM
,proto
->p_proto
);
609 if (udp
->fd
< 0) { r
= errno
; goto x_free
; }
611 r
= adns__setnonblock(ads
,udp
->fd
);
612 if (r
) { r
= errno
; goto x_closeudp
; }
618 for (j
=0; j
<ads
->nudp
; j
++) close(ads
->udpsocket
[j
].fd
);
624 static void init_abort(adns_state ads
) {
625 if (ads
->nsearchlist
) {
626 free(ads
->searchlist
[0]);
627 free(ads
->searchlist
);
632 static void logfn_file(adns_state ads
, void *logfndata
,
633 const char *fmt
, va_list al
) {
634 vfprintf(logfndata
,fmt
,al
);
637 static int init_files(adns_state
*ads_r
, adns_initflags flags
,
638 adns_logcallbackfn
*logfn
, void *logfndata
) {
640 const char *res_options
, *adns_res_options
;
643 r
= init_begin(&ads
, flags
, logfn
, logfndata
);
646 res_options
= instrum_getenv(ads
,"RES_OPTIONS");
647 adns_res_options
= instrum_getenv(ads
,"ADNS_RES_OPTIONS");
648 ccf_options(ads
,"RES_OPTIONS",-1,res_options
);
649 ccf_options(ads
,"ADNS_RES_OPTIONS",-1,adns_res_options
);
651 readconfig(ads
,"/etc/resolv.conf",1);
652 readconfig(ads
,"/etc/resolv-adns.conf",0);
653 readconfigenv(ads
,"RES_CONF");
654 readconfigenv(ads
,"ADNS_RES_CONF");
656 readconfigenvtext(ads
,"RES_CONF_TEXT");
657 readconfigenvtext(ads
,"ADNS_RES_CONF_TEXT");
659 ccf_options(ads
,"RES_OPTIONS",-1,res_options
);
660 ccf_options(ads
,"ADNS_RES_OPTIONS",-1,adns_res_options
);
662 ccf_search(ads
,"LOCALDOMAIN",-1,instrum_getenv(ads
,"LOCALDOMAIN"));
663 ccf_search(ads
,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads
,"ADNS_LOCALDOMAIN"));
665 if (ads
->configerrno
&& ads
->configerrno
!= EINVAL
) {
674 adns__consistency(ads
,0,cc_entex
);
679 int adns_init(adns_state
*ads_r
, adns_initflags flags
, FILE *diagfile
) {
680 return init_files(ads_r
, flags
, logfn_file
, diagfile ? diagfile
: stderr
);
683 static int init_strcfg(adns_state
*ads_r
, adns_initflags flags
,
684 adns_logcallbackfn
*logfn
, void *logfndata
,
685 const char *configtext
) {
689 r
= init_begin(&ads
, flags
, logfn
, logfndata
);
692 readconfigtext(ads
,configtext
,"<supplied configuration text>");
693 if (ads
->configerrno
) {
699 r
= init_finish(ads
); if (r
) return r
;
700 adns__consistency(ads
,0,cc_entex
);
705 int adns_init_strcfg(adns_state
*ads_r
, adns_initflags flags
,
706 FILE *diagfile
, const char *configtext
) {
707 return init_strcfg(ads_r
, flags
,
708 diagfile ? logfn_file
: 0, diagfile
,
712 int adns_init_logfn(adns_state
*newstate_r
, adns_initflags flags
,
713 const char *configtext
/*0=>use default config files*/,
714 adns_logcallbackfn
*logfn
/*0=>logfndata is a FILE* */,
715 void *logfndata
/*0 with logfn==0 => discard*/) {
716 if (!logfn
&& logfndata
)
719 return init_strcfg(newstate_r
, flags
, logfn
, logfndata
, configtext
);
721 return init_files(newstate_r
, flags
, logfn
, logfndata
);
724 void adns_finish(adns_state ads
) {
726 adns__consistency(ads
,0,cc_entex
);
728 if (ads
->udpw
.head
) adns_cancel(ads
->udpw
.head
);
729 else if (ads
->tcpw
.head
) adns_cancel(ads
->tcpw
.head
);
730 else if (ads
->childw
.head
) adns_cancel(ads
->childw
.head
);
731 else if (ads
->output
.head
) adns_cancel(ads
->output
.head
);
734 for (i
=0; i
<ads
->nudp
; i
++) close(ads
->udpsocket
[i
].fd
);
735 if (ads
->tcpsocket
>= 0) close(ads
->tcpsocket
);
736 adns__vbuf_free(&ads
->tcpsend
);
737 adns__vbuf_free(&ads
->tcprecv
);
742 void adns_forallqueries_begin(adns_state ads
) {
743 adns__consistency(ads
,0,cc_entex
);
745 ads
->udpw
.head ? ads
->udpw
.head
:
746 ads
->tcpw
.head ? ads
->tcpw
.head
:
747 ads
->childw
.head ? ads
->childw
.head
:
751 adns_query
adns_forallqueries_next(adns_state ads
, void **context_r
) {
754 adns__consistency(ads
,0,cc_entex
);
755 nqu
= ads
->forallnext
;
761 } else if (qu
== ads
->udpw
.tail
) {
763 ads
->tcpw
.head ? ads
->tcpw
.head
:
764 ads
->childw
.head ? ads
->childw
.head
:
766 } else if (qu
== ads
->tcpw
.tail
) {
768 ads
->childw
.head ? ads
->childw
.head
:
770 } else if (qu
== ads
->childw
.tail
) {
771 nqu
= ads
->output
.head
;
775 if (!qu
->parent
) break;
777 ads
->forallnext
= nqu
;
778 if (context_r
) *context_r
= qu
->ctx
.ext
;