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 const struct sockaddr_in
*sin
;
49 assert(sa
->sa_family
==AF_INET
); /* for inet_ntoa */
50 sin
= (const void *)sa
;
52 for (i
=0; i
<ads
->nservers
; i
++) {
53 if (adns__sockaddr_equal_p(sa
, &ads
->servers
[i
].addr
.sa
)) {
54 adns__debug(ads
,-1,0,"duplicate nameserver %s ignored",
55 inet_ntoa(sin
->sin_addr
));
60 if (ads
->nservers
>=MAXSERVERS
) {
61 adns__diag(ads
,-1,0,"too many nameservers, ignoring %s",
62 inet_ntoa(sin
->sin_addr
));
66 ss
= ads
->servers
+ads
->nservers
;
67 assert(n
<= sizeof(ss
->addr
));
69 memcpy(&ss
->addr
, sa
, n
);
73 static void freesearchlist(adns_state ads
) {
74 if (ads
->nsearchlist
) free(*ads
->searchlist
);
75 free(ads
->searchlist
);
78 static void saveerr(adns_state ads
, int en
) {
79 if (!ads
->configerrno
) ads
->configerrno
= en
;
82 static void configparseerr(adns_state ads
, const char *fn
, int lno
,
83 const char *fmt
, ...) {
87 if (!ads
->logfn
|| (ads
->iflags
& adns_if_noerrprint
)) return;
89 if (lno
==-1) adns__lprintf(ads
,"adns: %s: ",fn
);
90 else adns__lprintf(ads
,"adns: %s:%d: ",fn
,lno
);
92 adns__vlprintf(ads
,fmt
,al
);
94 adns__lprintf(ads
,"\n");
97 static int nextword(const char **bufp_io
, const char **word_r
, int *l_r
) {
101 while (ctype_whitespace(*p
)) p
++;
105 while (*q
&& !ctype_whitespace(*q
)) q
++;
114 static void ccf_nameserver(adns_state ads
, const char *fn
,
115 int lno
, const char *buf
) {
116 struct sockaddr_in sin
;
118 memset(&sin
,0,sizeof(sin
));
119 sin
.sin_family
= AF_INET
;
120 sin
.sin_port
= htons(DNS_PORT
);
121 if (!inet_aton(buf
,&sin
.sin_addr
)) {
122 configparseerr(ads
,fn
,lno
,"invalid nameserver address `%s'",buf
);
125 adns__debug(ads
,-1,0,"using nameserver %s",inet_ntoa(sin
.sin_addr
));
126 addserver(ads
,(const struct sockaddr
*)&sin
,sizeof(sin
));
129 static void ccf_search(adns_state ads
, const char *fn
,
130 int lno
, const char *buf
) {
131 const char *bufp
, *word
;
132 char *newchars
, **newptrs
, **pp
;
140 while (nextword(&bufp
,&word
,&l
)) { count
++; tl
+= l
+1; }
142 newptrs
= malloc(sizeof(char*)*count
);
143 if (!newptrs
) { saveerr(ads
,errno
); return; }
145 newchars
= malloc(tl
);
146 if (!newchars
) { saveerr(ads
,errno
); free(newptrs
); return; }
150 while (nextword(&bufp
,&word
,&l
)) {
152 memcpy(newchars
,word
,l
);
158 ads
->nsearchlist
= count
;
159 ads
->searchlist
= newptrs
;
162 static void ccf_sortlist(adns_state ads
, const char *fn
,
163 int lno
, const char *buf
) {
165 char tbuf
[200], *slash
, *ep
;
166 const char *maskwhat
;
175 while (nextword(&buf
,&word
,&l
)) {
176 if (ads
->nsortlist
>= MAXSORTLIST
) {
177 adns__diag(ads
,-1,0,"too many sortlist entries,"
178 " ignoring %.*s onwards",l
,word
);
182 if (l
>= sizeof(tbuf
)) {
183 configparseerr(ads
,fn
,lno
,"sortlist entry `%.*s' too long",l
,word
);
187 memcpy(tbuf
,word
,l
); tbuf
[l
]= 0;
188 slash
= strchr(tbuf
,'/');
189 if (slash
) *slash
++= 0;
191 sl
= &ads
->sortlist
[ads
->nsortlist
];
192 if (!inet_aton(tbuf
, &sl
->base
.v4
)) {
193 configparseerr(ads
,fn
,lno
,"invalid address `%s' in sortlist",tbuf
);
199 if (slash
[strspn(slash
, "0123456789")]) {
201 if (!inet_aton(slash
, &sl
->mask
.v4
)) {
202 configparseerr(ads
,fn
,lno
,"invalid mask `%s' in sortlist",slash
);
206 maskwhat
= "prefix length";
207 initial
= strtoul(slash
,&ep
,10);
208 if (*ep
|| initial
>adns__addr_width(af
)) {
209 configparseerr(ads
,fn
,lno
,"mask length `%s' invalid",slash
);
212 adns__prefix_mask(af
, initial
, &sl
->mask
);
215 maskwhat
= "implied prefix length";
216 initial
= adns__guess_prefix_length(af
, &sl
->base
);
218 configparseerr(ads
,fn
,lno
, "network address `%s'"
219 " in sortlist is not in classed ranges,"
220 " must specify mask explicitly", tbuf
);
223 adns__prefix_mask(af
, initial
, &sl
->mask
);
226 if (!adns__addr_match_p(af
,&sl
->base
, af
,&sl
->base
,&sl
->mask
)) {
228 configparseerr(ads
,fn
,lno
, "%s %d in sortlist"
229 " overlaps address `%s'",maskwhat
,initial
,tbuf
);
231 configparseerr(ads
,fn
,lno
, "%s `%s' in sortlist"
232 " overlaps address `%s'",maskwhat
,slash
,tbuf
);
242 static void ccf_options(adns_state ads
, const char *fn
,
243 int lno
, const char *buf
) {
251 while (nextword(&buf
,&word
,&l
)) {
252 if (l
==5 && !memcmp(word
,"debug",5)) {
253 ads
->iflags
|= adns_if_debug
;
256 if (l
>=6 && !memcmp(word
,"ndots:",6)) {
257 v
= strtoul(word
+6,&ep
,10);
258 if (l
==6 || ep
!= word
+l
|| v
> INT_MAX
) {
259 configparseerr(ads
,fn
,lno
,"option `%.*s' malformed"
260 " or has bad value",l
,word
);
266 if (l
>=12 && !memcmp(word
,"adns_checkc:",12)) {
267 if (!strcmp(word
+12,"none")) {
268 ads
->iflags
&= ~adns_if_checkc_freq
;
269 ads
->iflags
|= adns_if_checkc_entex
;
270 } else if (!strcmp(word
+12,"entex")) {
271 ads
->iflags
&= ~adns_if_checkc_freq
;
272 ads
->iflags
|= adns_if_checkc_entex
;
273 } else if (!strcmp(word
+12,"freq")) {
274 ads
->iflags
|= adns_if_checkc_freq
;
276 configparseerr(ads
,fn
,lno
, "option adns_checkc has bad value `%s' "
277 "(must be none, entex or freq", word
+12);
281 adns__diag(ads
,-1,0,"%s:%d: unknown option `%.*s'", fn
,lno
, l
,word
);
285 static void ccf_clearnss(adns_state ads
, const char *fn
,
286 int lno
, const char *buf
) {
290 static void ccf_include(adns_state ads
, const char *fn
,
291 int lno
, const char *buf
) {
293 configparseerr(ads
,fn
,lno
,"`include' directive with no filename");
296 readconfig(ads
,buf
,1);
299 static void ccf_lookup(adns_state ads
, const char *fn
, int lno
,
306 configparseerr(ads
,fn
,lno
,"`lookup' directive with no databases");
310 while (nextword(&buf
,&word
,&l
)) {
311 if (l
==4 && !memcmp(word
,"bind",4)) {
313 } else if (l
==4 && !memcmp(word
,"file",4)) {
314 /* ignore this and hope /etc/hosts is not essential */
315 } else if (l
==2 && !memcmp(word
,"yp",2)) {
316 adns__diag(ads
,-1,0,"%s:%d: yp lookups not supported by adns", fn
,lno
);
319 adns__diag(ads
,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
325 adns__diag(ads
,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn
,lno
);
328 static const struct configcommandinfo
{
330 void (*fn
)(adns_state ads
, const char *fn
, int lno
, const char *buf
);
331 } configcommandinfos
[]= {
332 { "nameserver", ccf_nameserver
},
333 { "domain", ccf_search
},
334 { "search", ccf_search
},
335 { "sortlist", ccf_sortlist
},
336 { "options", ccf_options
},
337 { "clearnameservers", ccf_clearnss
},
338 { "include", ccf_include
},
339 { "lookup", ccf_lookup
}, /* OpenBSD */
348 static int gl_file(adns_state ads
, getline_ctx
*src_io
, const char *filename
,
349 int lno
, char *buf
, int buflen
) {
350 FILE *file
= src_io
->file
;
358 for (;;) { /* loop over chars */
360 adns__diag(ads
,-1,0,"%s:%d: line too long, ignored",filename
,lno
);
365 adns__diag(ads
,-1,0,"%s:%d: line contains nul, ignored",filename
,lno
);
367 } else if (c
== '\n') {
369 } else if (c
== EOF
) {
372 adns__diag(ads
,-1,0,"%s:%d: read error: %s",
373 filename
,lno
,strerror(errno
));
389 while ((c
= getc(file
)) != EOF
&& c
!= '\n');
393 static int gl_text(adns_state ads
, getline_ctx
*src_io
, const char *filename
,
394 int lno
, char *buf
, int buflen
) {
395 const char *cp
= src_io
->text
;
398 if (!cp
|| !*cp
) return -1;
400 if (*cp
== ';' || *cp
== '\n') cp
++;
401 l
= strcspn(cp
,";\n");
405 adns__diag(ads
,-1,0,"%s:%d: line too long, ignored",filename
,lno
);
415 static void readconfiggeneric(adns_state ads
, const char *filename
,
416 int (*getline
)(adns_state ads
, getline_ctx
*,
417 const char *filename
, int lno
,
418 char *buf
, int buflen
),
419 /* Returns >=0 for success, -1 for EOF or error
420 * (error will have been reported), or -2 for
421 * bad line was encountered, try again.
423 getline_ctx gl_ctx
) {
424 char linebuf
[2000], *p
, *q
;
426 const struct configcommandinfo
*ccip
;
429 (l
= getline(ads
,&gl_ctx
, filename
,lno
, linebuf
,sizeof(linebuf
))) != -1;
431 if (l
== -2) continue;
432 while (l
>0 && ctype_whitespace(linebuf
[l
-1])) l
--;
435 while (ctype_whitespace(*p
)) p
++;
436 if (*p
== '#' || *p
== ';' || !*p
) continue;
438 while (*q
&& !ctype_whitespace(*q
)) q
++;
440 for (ccip
=configcommandinfos
;
442 !(strlen(ccip
->name
)==dirl
&& !memcmp(ccip
->name
,p
,q
-p
));
445 adns__diag(ads
,-1,0,"%s:%d: unknown configuration directive `%.*s'",
446 filename
,lno
,(int)(q
-p
),p
);
449 while (ctype_whitespace(*q
)) q
++;
450 ccip
->fn(ads
,filename
,lno
,q
);
454 static const char *instrum_getenv(adns_state ads
, const char *envvar
) {
457 value
= getenv(envvar
);
458 if (!value
) adns__debug(ads
,-1,0,"environment variable %s not set",envvar
);
459 else adns__debug(ads
,-1,0,"environment variable %s"
460 " set to `%s'",envvar
,value
);
464 static void readconfig(adns_state ads
, const char *filename
, int warnmissing
) {
467 gl_ctx
.file
= fopen(filename
,"r");
469 if (errno
== ENOENT
) {
471 adns__debug(ads
,-1,0, "configuration file"
472 " `%s' does not exist",filename
);
476 adns__diag(ads
,-1,0,"cannot open configuration file `%s': %s",
477 filename
,strerror(errno
));
481 readconfiggeneric(ads
,filename
,gl_file
,gl_ctx
);
486 static void readconfigtext(adns_state ads
, const char *text
,
487 const char *showname
) {
491 readconfiggeneric(ads
,showname
,gl_text
,gl_ctx
);
494 static void readconfigenv(adns_state ads
, const char *envvar
) {
495 const char *filename
;
497 if (ads
->iflags
& adns_if_noenv
) {
498 adns__debug(ads
,-1,0,"not checking environment variable `%s'",envvar
);
501 filename
= instrum_getenv(ads
,envvar
);
502 if (filename
) readconfig(ads
,filename
,1);
505 static void readconfigenvtext(adns_state ads
, const char *envvar
) {
506 const char *textdata
;
508 if (ads
->iflags
& adns_if_noenv
) {
509 adns__debug(ads
,-1,0,"not checking environment variable `%s'",envvar
);
512 textdata
= instrum_getenv(ads
,envvar
);
513 if (textdata
) readconfigtext(ads
,textdata
,envvar
);
517 int adns__setnonblock(adns_state ads
, int fd
) {
520 r
= fcntl(fd
,F_GETFL
,0); if (r
<0) return errno
;
522 r
= fcntl(fd
,F_SETFL
,r
); if (r
<0) return errno
;
526 static int init_begin(adns_state
*ads_r
, adns_initflags flags
,
527 adns_logcallbackfn
*logfn
, void *logfndata
) {
531 ads
= malloc(sizeof(*ads
)); if (!ads
) return errno
;
535 ads
->logfndata
= logfndata
;
537 LIST_INIT(ads
->udpw
);
538 LIST_INIT(ads
->tcpw
);
539 LIST_INIT(ads
->childw
);
540 LIST_INIT(ads
->output
);
543 ads
->udpsocket
= ads
->tcpsocket
= -1;
544 adns__vbuf_init(&ads
->tcpsend
);
545 adns__vbuf_init(&ads
->tcprecv
);
546 ads
->tcprecv_skip
= 0;
547 ads
->nservers
= ads
->nsortlist
= ads
->nsearchlist
= ads
->tcpserver
= 0;
549 ads
->tcpstate
= server_disconnected
;
550 timerclear(&ads
->tcptimeout
);
554 ads
->rand48xsubi
[0]= pid
;
555 ads
->rand48xsubi
[1]= (unsigned long)pid
>> 16;
556 ads
->rand48xsubi
[2]= pid
^ ((unsigned long)pid
>> 16);
562 static int init_finish(adns_state ads
) {
563 struct sockaddr_in sin
;
564 struct protoent
*proto
;
567 if (!ads
->nservers
) {
568 if (ads
->logfn
&& ads
->iflags
& adns_if_debug
)
569 adns__lprintf(ads
,"adns: no nameservers, using IPv4 localhost\n");
570 memset(&sin
, 0, sizeof(sin
));
571 sin
.sin_family
= AF_INET
;
572 sin
.sin_port
= htons(DNS_PORT
);
573 sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
574 addserver(ads
,(struct sockaddr
*)&sin
, sizeof(sin
));
577 /* we can't cope with multiple transport address families yet */
578 for (i
=0; i
<ads
->nservers
; i
++)
579 assert(ads
->servers
[i
].addr
.sa
.sa_family
==AF_INET
);
581 proto
= getprotobyname("udp"); if (!proto
) { r
= ENOPROTOOPT
; goto x_free
; }
582 ads
->udpsocket
= socket(AF_INET
,SOCK_DGRAM
,proto
->p_proto
);
583 if (ads
->udpsocket
<0) { r
= errno
; goto x_free
; }
585 r
= adns__setnonblock(ads
,ads
->udpsocket
);
586 if (r
) { r
= errno
; goto x_closeudp
; }
591 close(ads
->udpsocket
);
597 static void init_abort(adns_state ads
) {
598 if (ads
->nsearchlist
) {
599 free(ads
->searchlist
[0]);
600 free(ads
->searchlist
);
605 static void logfn_file(adns_state ads
, void *logfndata
,
606 const char *fmt
, va_list al
) {
607 vfprintf(logfndata
,fmt
,al
);
610 static int init_files(adns_state
*ads_r
, adns_initflags flags
,
611 adns_logcallbackfn
*logfn
, void *logfndata
) {
613 const char *res_options
, *adns_res_options
;
616 r
= init_begin(&ads
, flags
, logfn
, logfndata
);
619 res_options
= instrum_getenv(ads
,"RES_OPTIONS");
620 adns_res_options
= instrum_getenv(ads
,"ADNS_RES_OPTIONS");
621 ccf_options(ads
,"RES_OPTIONS",-1,res_options
);
622 ccf_options(ads
,"ADNS_RES_OPTIONS",-1,adns_res_options
);
624 readconfig(ads
,"/etc/resolv.conf",1);
625 readconfig(ads
,"/etc/resolv-adns.conf",0);
626 readconfigenv(ads
,"RES_CONF");
627 readconfigenv(ads
,"ADNS_RES_CONF");
629 readconfigenvtext(ads
,"RES_CONF_TEXT");
630 readconfigenvtext(ads
,"ADNS_RES_CONF_TEXT");
632 ccf_options(ads
,"RES_OPTIONS",-1,res_options
);
633 ccf_options(ads
,"ADNS_RES_OPTIONS",-1,adns_res_options
);
635 ccf_search(ads
,"LOCALDOMAIN",-1,instrum_getenv(ads
,"LOCALDOMAIN"));
636 ccf_search(ads
,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads
,"ADNS_LOCALDOMAIN"));
638 if (ads
->configerrno
&& ads
->configerrno
!= EINVAL
) {
647 adns__consistency(ads
,0,cc_entex
);
652 int adns_init(adns_state
*ads_r
, adns_initflags flags
, FILE *diagfile
) {
653 return init_files(ads_r
, flags
, logfn_file
, diagfile ? diagfile
: stderr
);
656 static int init_strcfg(adns_state
*ads_r
, adns_initflags flags
,
657 adns_logcallbackfn
*logfn
, void *logfndata
,
658 const char *configtext
) {
662 r
= init_begin(&ads
, flags
, logfn
, logfndata
);
665 readconfigtext(ads
,configtext
,"<supplied configuration text>");
666 if (ads
->configerrno
) {
672 r
= init_finish(ads
); if (r
) return r
;
673 adns__consistency(ads
,0,cc_entex
);
678 int adns_init_strcfg(adns_state
*ads_r
, adns_initflags flags
,
679 FILE *diagfile
, const char *configtext
) {
680 return init_strcfg(ads_r
, flags
,
681 diagfile ? logfn_file
: 0, diagfile
,
685 int adns_init_logfn(adns_state
*newstate_r
, adns_initflags flags
,
686 const char *configtext
/*0=>use default config files*/,
687 adns_logcallbackfn
*logfn
/*0=>logfndata is a FILE* */,
688 void *logfndata
/*0 with logfn==0 => discard*/) {
689 if (!logfn
&& logfndata
)
692 return init_strcfg(newstate_r
, flags
, logfn
, logfndata
, configtext
);
694 return init_files(newstate_r
, flags
, logfn
, logfndata
);
697 void adns_finish(adns_state ads
) {
698 adns__consistency(ads
,0,cc_entex
);
700 if (ads
->udpw
.head
) adns_cancel(ads
->udpw
.head
);
701 else if (ads
->tcpw
.head
) adns_cancel(ads
->tcpw
.head
);
702 else if (ads
->childw
.head
) adns_cancel(ads
->childw
.head
);
703 else if (ads
->output
.head
) adns_cancel(ads
->output
.head
);
706 close(ads
->udpsocket
);
707 if (ads
->tcpsocket
>= 0) close(ads
->tcpsocket
);
708 adns__vbuf_free(&ads
->tcpsend
);
709 adns__vbuf_free(&ads
->tcprecv
);
714 void adns_forallqueries_begin(adns_state ads
) {
715 adns__consistency(ads
,0,cc_entex
);
717 ads
->udpw
.head ? ads
->udpw
.head
:
718 ads
->tcpw
.head ? ads
->tcpw
.head
:
719 ads
->childw
.head ? ads
->childw
.head
:
723 adns_query
adns_forallqueries_next(adns_state ads
, void **context_r
) {
726 adns__consistency(ads
,0,cc_entex
);
727 nqu
= ads
->forallnext
;
733 } else if (qu
== ads
->udpw
.tail
) {
735 ads
->tcpw
.head ? ads
->tcpw
.head
:
736 ads
->childw
.head ? ads
->childw
.head
:
738 } else if (qu
== ads
->tcpw
.tail
) {
740 ads
->childw
.head ? ads
->childw
.head
:
742 } else if (qu
== ads
->childw
.tail
) {
743 nqu
= ads
->output
.head
;
747 if (!qu
->parent
) break;
749 ads
->forallnext
= nqu
;
750 if (context_r
) *context_r
= qu
->ctx
.ext
;