61f4f67c4b88a43d21273f9c73643062c1e4adca
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
, struct in_addr addr
) {
48 for (i
=0; i
<ads
->nservers
; i
++) {
49 if (ads
->servers
[i
].addr
.s_addr
== addr
.s_addr
) {
50 adns__debug(ads
,-1,0,"duplicate nameserver %s ignored",inet_ntoa(addr
));
55 if (ads
->nservers
>=MAXSERVERS
) {
56 adns__diag(ads
,-1,0,"too many nameservers, ignoring %s",inet_ntoa(addr
));
60 ss
= ads
->servers
+ads
->nservers
;
65 static void freesearchlist(adns_state ads
) {
66 if (ads
->nsearchlist
) free(*ads
->searchlist
);
67 free(ads
->searchlist
);
70 static void saveerr(adns_state ads
, int en
) {
71 if (!ads
->configerrno
) ads
->configerrno
= en
;
74 static void configparseerr(adns_state ads
, const char *fn
, int lno
,
75 const char *fmt
, ...) {
79 if (!ads
->logfn
|| (ads
->iflags
& adns_if_noerrprint
)) return;
81 if (lno
==-1) adns__lprintf(ads
,"adns: %s: ",fn
);
82 else adns__lprintf(ads
,"adns: %s:%d: ",fn
,lno
);
84 adns__vlprintf(ads
,fmt
,al
);
86 adns__lprintf(ads
,"\n");
89 static int nextword(const char **bufp_io
, const char **word_r
, int *l_r
) {
93 while (ctype_whitespace(*p
)) p
++;
97 while (*q
&& !ctype_whitespace(*q
)) q
++;
106 static void ccf_nameserver(adns_state ads
, const char *fn
,
107 int lno
, const char *buf
) {
110 if (!inet_aton(buf
,&ia
)) {
111 configparseerr(ads
,fn
,lno
,"invalid nameserver address `%s'",buf
);
114 adns__debug(ads
,-1,0,"using nameserver %s",inet_ntoa(ia
));
118 static void ccf_search(adns_state ads
, const char *fn
,
119 int lno
, const char *buf
) {
120 const char *bufp
, *word
;
121 char *newchars
, **newptrs
, **pp
;
129 while (nextword(&bufp
,&word
,&l
)) { count
++; tl
+= l
+1; }
131 newptrs
= malloc(sizeof(char*)*count
);
132 if (!newptrs
) { saveerr(ads
,errno
); return; }
134 newchars
= malloc(tl
);
135 if (!newchars
) { saveerr(ads
,errno
); free(newptrs
); return; }
139 while (nextword(&bufp
,&word
,&l
)) {
141 memcpy(newchars
,word
,l
);
147 ads
->nsearchlist
= count
;
148 ads
->searchlist
= newptrs
;
151 static void ccf_sortlist(adns_state ads
, const char *fn
,
152 int lno
, const char *buf
) {
154 char tbuf
[200], *slash
, *ep
;
155 const char *maskwhat
;
164 while (nextword(&buf
,&word
,&l
)) {
165 if (ads
->nsortlist
>= MAXSORTLIST
) {
166 adns__diag(ads
,-1,0,"too many sortlist entries,"
167 " ignoring %.*s onwards",l
,word
);
171 if (l
>= sizeof(tbuf
)) {
172 configparseerr(ads
,fn
,lno
,"sortlist entry `%.*s' too long",l
,word
);
176 memcpy(tbuf
,word
,l
); tbuf
[l
]= 0;
177 slash
= strchr(tbuf
,'/');
178 if (slash
) *slash
++= 0;
180 sl
= &ads
->sortlist
[ads
->nsortlist
];
182 if (strchr(tbuf
, ':'))
183 ai
= &adns__inet6_afinfo
;
185 ai
= &adns__inet_afinfo
;
187 if (!inet_pton(ai
->af
, tbuf
, &sl
->base
)) {
188 configparseerr(ads
,fn
,lno
,"invalid address `%s' in sortlist",tbuf
);
193 if (strchr(slash
,ai
->delim
)) {
195 if (!inet_pton(ai
->af
,slash
,&sl
->mask
)) {
196 configparseerr(ads
,fn
,lno
,"invalid mask `%s' in sortlist",slash
);
200 maskwhat
= "prefix length";
201 initial
= strtoul(slash
,&ep
,10);
202 if (*ep
|| initial
>ai
->width
) {
203 configparseerr(ads
,fn
,lno
,"mask length `%s' invalid",slash
);
206 ai
->prefix_mask(initial
, &sl
->mask
);
209 maskwhat
= "implied prefix length";
210 initial
= ai
->guess_len(&sl
->base
);
212 configparseerr(ads
,fn
,lno
, "network address `%s'"
213 " in sortlist is not in classed ranges,"
214 " must specify mask explicitly", tbuf
);
217 ai
->prefix_mask(initial
, &sl
->mask
);
220 if (!ai
->matchp(&sl
->base
, &sl
->base
, &sl
->mask
)) {
222 configparseerr(ads
,fn
,lno
, "%s %d in sortlist"
223 " overlaps address `%s'",maskwhat
,initial
,tbuf
);
225 configparseerr(ads
,fn
,lno
, "%s `%s' in sortlist"
226 " overlaps address `%s'",maskwhat
,slash
,tbuf
);
236 static void ccf_options(adns_state ads
, const char *fn
,
237 int lno
, const char *buf
) {
245 while (nextword(&buf
,&word
,&l
)) {
246 if (l
==5 && !memcmp(word
,"debug",5)) {
247 ads
->iflags
|= adns_if_debug
;
250 if (l
>=6 && !memcmp(word
,"ndots:",6)) {
251 v
= strtoul(word
+6,&ep
,10);
252 if (l
==6 || ep
!= word
+l
|| v
> INT_MAX
) {
253 configparseerr(ads
,fn
,lno
,"option `%.*s' malformed"
254 " or has bad value",l
,word
);
260 if (l
>=12 && !memcmp(word
,"adns_checkc:",12)) {
261 if (!strcmp(word
+12,"none")) {
262 ads
->iflags
&= ~adns_if_checkc_freq
;
263 ads
->iflags
|= adns_if_checkc_entex
;
264 } else if (!strcmp(word
+12,"entex")) {
265 ads
->iflags
&= ~adns_if_checkc_freq
;
266 ads
->iflags
|= adns_if_checkc_entex
;
267 } else if (!strcmp(word
+12,"freq")) {
268 ads
->iflags
|= adns_if_checkc_freq
;
270 configparseerr(ads
,fn
,lno
, "option adns_checkc has bad value `%s' "
271 "(must be none, entex or freq", word
+12);
275 adns__diag(ads
,-1,0,"%s:%d: unknown option `%.*s'", fn
,lno
, l
,word
);
279 static void ccf_clearnss(adns_state ads
, const char *fn
,
280 int lno
, const char *buf
) {
284 static void ccf_include(adns_state ads
, const char *fn
,
285 int lno
, const char *buf
) {
287 configparseerr(ads
,fn
,lno
,"`include' directive with no filename");
290 readconfig(ads
,buf
,1);
293 static void ccf_lookup(adns_state ads
, const char *fn
, int lno
,
300 configparseerr(ads
,fn
,lno
,"`lookup' directive with no databases");
304 while (nextword(&buf
,&word
,&l
)) {
305 if (l
==4 && !memcmp(word
,"bind",4)) {
307 } else if (l
==4 && !memcmp(word
,"file",4)) {
308 /* ignore this and hope /etc/hosts is not essential */
309 } else if (l
==2 && !memcmp(word
,"yp",2)) {
310 adns__diag(ads
,-1,0,"%s:%d: yp lookups not supported by adns", fn
,lno
);
313 adns__diag(ads
,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
319 adns__diag(ads
,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn
,lno
);
322 static const struct configcommandinfo
{
324 void (*fn
)(adns_state ads
, const char *fn
, int lno
, const char *buf
);
325 } configcommandinfos
[]= {
326 { "nameserver", ccf_nameserver
},
327 { "domain", ccf_search
},
328 { "search", ccf_search
},
329 { "sortlist", ccf_sortlist
},
330 { "options", ccf_options
},
331 { "clearnameservers", ccf_clearnss
},
332 { "include", ccf_include
},
333 { "lookup", ccf_lookup
}, /* OpenBSD */
342 static int gl_file(adns_state ads
, getline_ctx
*src_io
, const char *filename
,
343 int lno
, char *buf
, int buflen
) {
344 FILE *file
= src_io
->file
;
352 for (;;) { /* loop over chars */
354 adns__diag(ads
,-1,0,"%s:%d: line too long, ignored",filename
,lno
);
359 adns__diag(ads
,-1,0,"%s:%d: line contains nul, ignored",filename
,lno
);
361 } else if (c
== '\n') {
363 } else if (c
== EOF
) {
366 adns__diag(ads
,-1,0,"%s:%d: read error: %s",
367 filename
,lno
,strerror(errno
));
383 while ((c
= getc(file
)) != EOF
&& c
!= '\n');
387 static int gl_text(adns_state ads
, getline_ctx
*src_io
, const char *filename
,
388 int lno
, char *buf
, int buflen
) {
389 const char *cp
= src_io
->text
;
392 if (!cp
|| !*cp
) return -1;
394 if (*cp
== ';' || *cp
== '\n') cp
++;
395 l
= strcspn(cp
,";\n");
399 adns__diag(ads
,-1,0,"%s:%d: line too long, ignored",filename
,lno
);
409 static void readconfiggeneric(adns_state ads
, const char *filename
,
410 int (*getline
)(adns_state ads
, getline_ctx
*,
411 const char *filename
, int lno
,
412 char *buf
, int buflen
),
413 /* Returns >=0 for success, -1 for EOF or error
414 * (error will have been reported), or -2 for
415 * bad line was encountered, try again.
417 getline_ctx gl_ctx
) {
418 char linebuf
[2000], *p
, *q
;
420 const struct configcommandinfo
*ccip
;
423 (l
= getline(ads
,&gl_ctx
, filename
,lno
, linebuf
,sizeof(linebuf
))) != -1;
425 if (l
== -2) continue;
426 while (l
>0 && ctype_whitespace(linebuf
[l
-1])) l
--;
429 while (ctype_whitespace(*p
)) p
++;
430 if (*p
== '#' || *p
== ';' || !*p
) continue;
432 while (*q
&& !ctype_whitespace(*q
)) q
++;
434 for (ccip
=configcommandinfos
;
436 !(strlen(ccip
->name
)==dirl
&& !memcmp(ccip
->name
,p
,q
-p
));
439 adns__diag(ads
,-1,0,"%s:%d: unknown configuration directive `%.*s'",
440 filename
,lno
,(int)(q
-p
),p
);
443 while (ctype_whitespace(*q
)) q
++;
444 ccip
->fn(ads
,filename
,lno
,q
);
448 static const char *instrum_getenv(adns_state ads
, const char *envvar
) {
451 value
= getenv(envvar
);
452 if (!value
) adns__debug(ads
,-1,0,"environment variable %s not set",envvar
);
453 else adns__debug(ads
,-1,0,"environment variable %s"
454 " set to `%s'",envvar
,value
);
458 static void readconfig(adns_state ads
, const char *filename
, int warnmissing
) {
461 gl_ctx
.file
= fopen(filename
,"r");
463 if (errno
== ENOENT
) {
465 adns__debug(ads
,-1,0, "configuration file"
466 " `%s' does not exist",filename
);
470 adns__diag(ads
,-1,0,"cannot open configuration file `%s': %s",
471 filename
,strerror(errno
));
475 readconfiggeneric(ads
,filename
,gl_file
,gl_ctx
);
480 static void readconfigtext(adns_state ads
, const char *text
,
481 const char *showname
) {
485 readconfiggeneric(ads
,showname
,gl_text
,gl_ctx
);
488 static void readconfigenv(adns_state ads
, const char *envvar
) {
489 const char *filename
;
491 if (ads
->iflags
& adns_if_noenv
) {
492 adns__debug(ads
,-1,0,"not checking environment variable `%s'",envvar
);
495 filename
= instrum_getenv(ads
,envvar
);
496 if (filename
) readconfig(ads
,filename
,1);
499 static void readconfigenvtext(adns_state ads
, const char *envvar
) {
500 const char *textdata
;
502 if (ads
->iflags
& adns_if_noenv
) {
503 adns__debug(ads
,-1,0,"not checking environment variable `%s'",envvar
);
506 textdata
= instrum_getenv(ads
,envvar
);
507 if (textdata
) readconfigtext(ads
,textdata
,envvar
);
511 int adns__setnonblock(adns_state ads
, int fd
) {
514 r
= fcntl(fd
,F_GETFL
,0); if (r
<0) return errno
;
516 r
= fcntl(fd
,F_SETFL
,r
); if (r
<0) return errno
;
520 static int init_begin(adns_state
*ads_r
, adns_initflags flags
,
521 adns_logcallbackfn
*logfn
, void *logfndata
) {
525 ads
= malloc(sizeof(*ads
)); if (!ads
) return errno
;
529 ads
->logfndata
= logfndata
;
531 LIST_INIT(ads
->udpw
);
532 LIST_INIT(ads
->tcpw
);
533 LIST_INIT(ads
->childw
);
534 LIST_INIT(ads
->output
);
537 ads
->udpsocket
= ads
->tcpsocket
= -1;
538 adns__vbuf_init(&ads
->tcpsend
);
539 adns__vbuf_init(&ads
->tcprecv
);
540 ads
->tcprecv_skip
= 0;
541 ads
->nservers
= ads
->nsortlist
= ads
->nsearchlist
= ads
->tcpserver
= 0;
543 ads
->tcpstate
= server_disconnected
;
544 timerclear(&ads
->tcptimeout
);
548 ads
->rand48xsubi
[0]= pid
;
549 ads
->rand48xsubi
[1]= (unsigned long)pid
>> 16;
550 ads
->rand48xsubi
[2]= pid
^ ((unsigned long)pid
>> 16);
556 static int init_finish(adns_state ads
) {
558 struct protoent
*proto
;
561 if (!ads
->nservers
) {
562 if (ads
->logfn
&& ads
->iflags
& adns_if_debug
)
563 adns__lprintf(ads
,"adns: no nameservers, using localhost\n");
564 ia
.s_addr
= htonl(INADDR_LOOPBACK
);
568 proto
= getprotobyname("udp"); if (!proto
) { r
= ENOPROTOOPT
; goto x_free
; }
569 ads
->udpsocket
= socket(AF_INET
,SOCK_DGRAM
,proto
->p_proto
);
570 if (ads
->udpsocket
<0) { r
= errno
; goto x_free
; }
572 r
= adns__setnonblock(ads
,ads
->udpsocket
);
573 if (r
) { r
= errno
; goto x_closeudp
; }
578 close(ads
->udpsocket
);
584 static void init_abort(adns_state ads
) {
585 if (ads
->nsearchlist
) {
586 free(ads
->searchlist
[0]);
587 free(ads
->searchlist
);
592 static void logfn_file(adns_state ads
, void *logfndata
,
593 const char *fmt
, va_list al
) {
594 vfprintf(logfndata
,fmt
,al
);
597 static int init_files(adns_state
*ads_r
, adns_initflags flags
,
598 adns_logcallbackfn
*logfn
, void *logfndata
) {
600 const char *res_options
, *adns_res_options
;
603 r
= init_begin(&ads
, flags
, logfn
, logfndata
);
606 res_options
= instrum_getenv(ads
,"RES_OPTIONS");
607 adns_res_options
= instrum_getenv(ads
,"ADNS_RES_OPTIONS");
608 ccf_options(ads
,"RES_OPTIONS",-1,res_options
);
609 ccf_options(ads
,"ADNS_RES_OPTIONS",-1,adns_res_options
);
611 readconfig(ads
,"/etc/resolv.conf",1);
612 readconfig(ads
,"/etc/resolv-adns.conf",0);
613 readconfigenv(ads
,"RES_CONF");
614 readconfigenv(ads
,"ADNS_RES_CONF");
616 readconfigenvtext(ads
,"RES_CONF_TEXT");
617 readconfigenvtext(ads
,"ADNS_RES_CONF_TEXT");
619 ccf_options(ads
,"RES_OPTIONS",-1,res_options
);
620 ccf_options(ads
,"ADNS_RES_OPTIONS",-1,adns_res_options
);
622 ccf_search(ads
,"LOCALDOMAIN",-1,instrum_getenv(ads
,"LOCALDOMAIN"));
623 ccf_search(ads
,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads
,"ADNS_LOCALDOMAIN"));
625 if (ads
->configerrno
&& ads
->configerrno
!= EINVAL
) {
634 adns__consistency(ads
,0,cc_entex
);
639 int adns_init(adns_state
*ads_r
, adns_initflags flags
, FILE *diagfile
) {
640 return init_files(ads_r
, flags
, logfn_file
, diagfile ? diagfile
: stderr
);
643 static int init_strcfg(adns_state
*ads_r
, adns_initflags flags
,
644 adns_logcallbackfn
*logfn
, void *logfndata
,
645 const char *configtext
) {
649 r
= init_begin(&ads
, flags
, logfn
, logfndata
);
652 readconfigtext(ads
,configtext
,"<supplied configuration text>");
653 if (ads
->configerrno
) {
659 r
= init_finish(ads
); if (r
) return r
;
660 adns__consistency(ads
,0,cc_entex
);
665 int adns_init_strcfg(adns_state
*ads_r
, adns_initflags flags
,
666 FILE *diagfile
, const char *configtext
) {
667 return init_strcfg(ads_r
, flags
,
668 diagfile ? logfn_file
: 0, diagfile
,
672 int adns_init_logfn(adns_state
*newstate_r
, adns_initflags flags
,
673 const char *configtext
/*0=>use default config files*/,
674 adns_logcallbackfn
*logfn
/*0=>logfndata is a FILE* */,
675 void *logfndata
/*0 with logfn==0 => discard*/) {
676 if (!logfn
&& logfndata
)
679 return init_strcfg(newstate_r
, flags
, logfn
, logfndata
, configtext
);
681 return init_files(newstate_r
, flags
, logfn
, logfndata
);
684 void adns_finish(adns_state ads
) {
685 adns__consistency(ads
,0,cc_entex
);
687 if (ads
->udpw
.head
) adns_cancel(ads
->udpw
.head
);
688 else if (ads
->tcpw
.head
) adns_cancel(ads
->tcpw
.head
);
689 else if (ads
->childw
.head
) adns_cancel(ads
->childw
.head
);
690 else if (ads
->output
.head
) adns_cancel(ads
->output
.head
);
693 close(ads
->udpsocket
);
694 if (ads
->tcpsocket
>= 0) close(ads
->tcpsocket
);
695 adns__vbuf_free(&ads
->tcpsend
);
696 adns__vbuf_free(&ads
->tcprecv
);
701 void adns_forallqueries_begin(adns_state ads
) {
702 adns__consistency(ads
,0,cc_entex
);
704 ads
->udpw
.head ? ads
->udpw
.head
:
705 ads
->tcpw
.head ? ads
->tcpw
.head
:
706 ads
->childw
.head ? ads
->childw
.head
:
710 adns_query
adns_forallqueries_next(adns_state ads
, void **context_r
) {
713 adns__consistency(ads
,0,cc_entex
);
714 nqu
= ads
->forallnext
;
720 } else if (qu
== ads
->udpw
.tail
) {
722 ads
->tcpw
.head ? ads
->tcpw
.head
:
723 ads
->childw
.head ? ads
->childw
.head
:
725 } else if (qu
== ads
->tcpw
.tail
) {
727 ads
->childw
.head ? ads
->childw
.head
:
729 } else if (qu
== ads
->childw
.tail
) {
730 nqu
= ads
->output
.head
;
734 if (!qu
->parent
) break;
736 ads
->forallnext
= nqu
;
737 if (context_r
) *context_r
= qu
->ctx
.ext
;