3 * - configuration file parsing
4 * - management of global state
8 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
10 * It is part of adns, which is
11 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
12 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
43 static void readconfig(adns_state ads
, const char *filename
, int warnmissing
);
45 static void addserver(adns_state ads
, struct in_addr addr
) {
49 for (i
=0; i
<ads
->nservers
; i
++) {
50 if (ads
->servers
[i
].addr
.s_addr
== addr
.s_addr
) {
51 adns__debug(ads
,-1,0,"duplicate nameserver %s ignored",inet_ntoa(addr
));
56 if (ads
->nservers
>=MAXSERVERS
) {
57 adns__diag(ads
,-1,0,"too many nameservers, ignoring %s",inet_ntoa(addr
));
61 ss
= ads
->servers
+ads
->nservers
;
66 static void freesearchlist(adns_state ads
) {
67 if (ads
->nsearchlist
) free(*ads
->searchlist
);
68 free(ads
->searchlist
);
71 static void saveerr(adns_state ads
, int en
) {
72 if (!ads
->configerrno
) ads
->configerrno
= en
;
75 static void configparseerr(adns_state ads
, const char *fn
, int lno
,
76 const char *fmt
, ...) {
80 if (!ads
->diagfile
|| (ads
->iflags
& adns_if_noerrprint
)) return;
82 if (lno
==-1) fprintf(ads
->diagfile
,"adns: %s: ",fn
);
83 else fprintf(ads
->diagfile
,"adns: %s:%d: ",fn
,lno
);
85 vfprintf(ads
->diagfile
,fmt
,al
);
87 fputc('\n',ads
->diagfile
);
90 static int nextword(const char **bufp_io
, const char **word_r
, int *l_r
) {
94 while (ctype_whitespace(*p
)) p
++;
98 while (*q
&& !ctype_whitespace(*q
)) q
++;
107 static void ccf_nameserver(adns_state ads
, const char *fn
, 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
, int lno
, const char *buf
) {
119 const char *bufp
, *word
;
120 char *newchars
, **newptrs
, **pp
;
128 while (nextword(&bufp
,&word
,&l
)) { count
++; tl
+= l
+1; }
130 newptrs
= malloc(sizeof(char*)*count
); if (!newptrs
) { saveerr(ads
,errno
); return; }
131 newchars
= malloc(tl
); if (!newchars
) { saveerr(ads
,errno
); free(newptrs
); return; }
135 while (nextword(&bufp
,&word
,&l
)) {
137 memcpy(newchars
,word
,l
);
143 ads
->nsearchlist
= count
;
144 ads
->searchlist
= newptrs
;
147 static void ccf_sortlist(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
149 char tbuf
[200], *slash
, *ep
;
150 struct in_addr base
, mask
;
152 unsigned long initial
, baselocal
;
157 while (nextword(&buf
,&word
,&l
)) {
158 if (ads
->nsortlist
>= MAXSORTLIST
) {
159 adns__diag(ads
,-1,0,"too many sortlist entries, ignoring %.*s onwards",l
,word
);
163 if (l
>= sizeof(tbuf
)) {
164 configparseerr(ads
,fn
,lno
,"sortlist entry `%.*s' too long",l
,word
);
168 memcpy(tbuf
,word
,l
); tbuf
[l
]= 0;
169 slash
= strchr(tbuf
,'/');
170 if (slash
) *slash
++= 0;
172 if (!inet_aton(tbuf
,&base
)) {
173 configparseerr(ads
,fn
,lno
,"invalid address `%s' in sortlist",tbuf
);
178 if (strchr(slash
,'.')) {
179 if (!inet_aton(slash
,&mask
)) {
180 configparseerr(ads
,fn
,lno
,"invalid mask `%s' in sortlist",slash
);
183 if (base
.s_addr
& ~mask
.s_addr
) {
184 configparseerr(ads
,fn
,lno
,
185 "mask `%s' in sortlist overlaps address `%s'",slash
,tbuf
);
189 initial
= strtoul(slash
,&ep
,10);
190 if (*ep
|| initial
>32) {
191 configparseerr(ads
,fn
,lno
,"mask length `%s' invalid",slash
);
194 mask
.s_addr
= htonl((0x0ffffffffUL
) << (32-initial
));
197 baselocal
= ntohl(base
.s_addr
);
198 if (!baselocal
& 0x080000000UL
) /* class A */
199 mask
.s_addr
= htonl(0x0ff000000UL
);
200 else if ((baselocal
& 0x0c0000000UL
) == 0x080000000UL
)
201 mask
.s_addr
= htonl(0x0ffff0000UL
); /* class B */
202 else if ((baselocal
& 0x0f0000000UL
) == 0x0e0000000UL
)
203 mask
.s_addr
= htonl(0x0ff000000UL
); /* class C */
205 configparseerr(ads
,fn
,lno
,
206 "network address `%s' in sortlist is not in classed ranges,"
207 " must specify mask explicitly", tbuf
);
212 ads
->sortlist
[ads
->nsortlist
].base
= base
;
213 ads
->sortlist
[ads
->nsortlist
].mask
= mask
;
218 static void ccf_options(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
226 while (nextword(&buf
,&word
,&l
)) {
227 if (l
==5 && !memcmp(word
,"debug",5)) {
228 ads
->iflags
|= adns_if_debug
;
231 if (l
>=6 && !memcmp(word
,"ndots:",6)) {
232 v
= strtoul(word
+6,&ep
,10);
233 if (l
==6 || ep
!= word
+l
|| v
> INT_MAX
) {
234 configparseerr(ads
,fn
,lno
,"option `%.*s' malformed or has bad value",l
,word
);
240 if (l
>=12 && !memcmp(word
,"adns_checkc:",12)) {
241 if (!strcmp(word
+12,"none")) {
242 ads
->iflags
&= ~adns_if_checkc_freq
;
243 ads
->iflags
|= adns_if_checkc_entex
;
244 } else if (!strcmp(word
+12,"entex")) {
245 ads
->iflags
&= ~adns_if_checkc_freq
;
246 ads
->iflags
|= adns_if_checkc_entex
;
247 } else if (!strcmp(word
+12,"freq")) {
248 ads
->iflags
|= adns_if_checkc_freq
;
250 configparseerr(ads
,fn
,lno
, "option adns_checkc has bad value `%s' "
251 "(must be none, entex or freq", word
+12);
255 adns__diag(ads
,-1,0,"%s:%d: unknown option `%.*s'", fn
,lno
, l
,word
);
259 static void ccf_clearnss(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
263 static void ccf_include(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
265 configparseerr(ads
,fn
,lno
,"`include' directive with no filename");
268 readconfig(ads
,buf
,1);
271 static void ccf_lookup(adns_state ads
, const char *fn
, int lno
,
278 configparseerr(ads
,fn
,lno
,"`lookup' directive with no databases");
282 while (nextword(&buf
,&word
,&l
)) {
283 if (l
==4 && !memcmp(word
,"bind",4)) {
285 } else if (l
==4 && !memcmp(word
,"file",4)) {
286 /* ignore this and hope /etc/hosts is not essential */
287 } else if (l
==2 && !memcmp(word
,"yp",2)) {
288 adns__diag(ads
,-1,0,"%s:%d: yp lookups not supported by adns", fn
,lno
);
291 adns__diag(ads
,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
297 adns__diag(ads
,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn
,lno
);
300 static const struct configcommandinfo
{
302 void (*fn
)(adns_state ads
, const char *fn
, int lno
, const char *buf
);
303 } configcommandinfos
[]= {
304 { "nameserver", ccf_nameserver
},
305 { "domain", ccf_search
},
306 { "search", ccf_search
},
307 { "sortlist", ccf_sortlist
},
308 { "options", ccf_options
},
309 { "clearnameservers", ccf_clearnss
},
310 { "include", ccf_include
},
311 { "lookup", ccf_lookup
}, /* OpenBSD */
320 static int gl_file(adns_state ads
, getline_ctx
*src_io
, const char *filename
,
321 int lno
, char *buf
, int buflen
) {
322 FILE *file
= src_io
->file
;
330 for (;;) { /* loop over chars */
332 adns__diag(ads
,-1,0,"%s:%d: line too long, ignored",filename
,lno
);
337 adns__diag(ads
,-1,0,"%s:%d: line contains nul, ignored",filename
,lno
);
339 } else if (c
== '\n') {
341 } else if (c
== EOF
) {
344 adns__diag(ads
,-1,0,"%s:%d: read error: %s",filename
,lno
,strerror(errno
));
360 while ((c
= getc(file
)) != EOF
&& c
!= '\n');
364 static int gl_text(adns_state ads
, getline_ctx
*src_io
, const char *filename
,
365 int lno
, char *buf
, int buflen
) {
366 const char *cp
= src_io
->text
;
369 if (!cp
|| !*cp
) return -1;
371 if (*cp
== ';' || *cp
== '\n') cp
++;
372 l
= strcspn(cp
,";\n");
376 adns__diag(ads
,-1,0,"%s:%d: line too long, ignored",filename
,lno
);
386 static void readconfiggeneric(adns_state ads
, const char *filename
,
387 int (*getline
)(adns_state ads
, getline_ctx
*,
388 const char *filename
, int lno
,
389 char *buf
, int buflen
),
390 /* Returns >=0 for success, -1 for EOF or error
391 * (error will have been reported), or -2 for
392 * bad line was encountered, try again.
394 getline_ctx gl_ctx
) {
395 char linebuf
[2000], *p
, *q
;
397 const struct configcommandinfo
*ccip
;
400 (l
= getline(ads
,&gl_ctx
, filename
,lno
, linebuf
,sizeof(linebuf
))) != -1;
402 if (l
== -2) continue;
403 while (l
>0 && ctype_whitespace(linebuf
[l
-1])) l
--;
406 while (ctype_whitespace(*p
)) p
++;
407 if (*p
== '#' || !*p
) continue;
409 while (*q
&& !ctype_whitespace(*q
)) q
++;
411 for (ccip
=configcommandinfos
;
412 ccip
->name
&& !(strlen(ccip
->name
)==dirl
&& !memcmp(ccip
->name
,p
,q
-p
));
415 adns__diag(ads
,-1,0,"%s:%d: unknown configuration directive `%.*s'",
419 while (ctype_whitespace(*q
)) q
++;
420 ccip
->fn(ads
,filename
,lno
,q
);
424 static const char *instrum_getenv(adns_state ads
, const char *envvar
) {
427 value
= getenv(envvar
);
428 if (!value
) adns__debug(ads
,-1,0,"environment variable %s not set",envvar
);
429 else adns__debug(ads
,-1,0,"environment variable %s set to `%s'",envvar
,value
);
433 static void readconfig(adns_state ads
, const char *filename
, int warnmissing
) {
436 gl_ctx
.file
= fopen(filename
,"r");
438 if (errno
== ENOENT
) {
440 adns__debug(ads
,-1,0,"configuration file `%s' does not exist",filename
);
444 adns__diag(ads
,-1,0,"cannot open configuration file `%s': %s",
445 filename
,strerror(errno
));
449 readconfiggeneric(ads
,filename
,gl_file
,gl_ctx
);
454 static void readconfigtext(adns_state ads
, const char *text
, const char *showname
) {
458 readconfiggeneric(ads
,showname
,gl_text
,gl_ctx
);
461 static void readconfigenv(adns_state ads
, const char *envvar
) {
462 const char *filename
;
464 if (ads
->iflags
& adns_if_noenv
) {
465 adns__debug(ads
,-1,0,"not checking environment variable `%s'",envvar
);
468 filename
= instrum_getenv(ads
,envvar
);
469 if (filename
) readconfig(ads
,filename
,1);
472 static void readconfigenvtext(adns_state ads
, const char *envvar
) {
473 const char *textdata
;
475 if (ads
->iflags
& adns_if_noenv
) {
476 adns__debug(ads
,-1,0,"not checking environment variable `%s'",envvar
);
479 textdata
= instrum_getenv(ads
,envvar
);
480 if (textdata
) readconfigtext(ads
,textdata
,envvar
);
484 int adns__setnonblock(adns_state ads
, int fd
) {
487 r
= fcntl(fd
,F_GETFL
,0); if (r
<0) return errno
;
489 r
= fcntl(fd
,F_SETFL
,r
); if (r
<0) return errno
;
493 static int init_begin(adns_state
*ads_r
, adns_initflags flags
, FILE *diagfile
) {
496 ads
= malloc(sizeof(*ads
)); if (!ads
) return errno
;
499 ads
->diagfile
= diagfile
;
501 LIST_INIT(ads
->udpw
);
502 LIST_INIT(ads
->tcpw
);
503 LIST_INIT(ads
->childw
);
504 LIST_INIT(ads
->output
);
507 ads
->udpsocket
= ads
->tcpsocket
= -1;
508 adns__vbuf_init(&ads
->tcpsend
);
509 adns__vbuf_init(&ads
->tcprecv
);
510 ads
->tcprecv_skip
= 0;
511 ads
->nservers
= ads
->nsortlist
= ads
->nsearchlist
= ads
->tcpserver
= 0;
513 ads
->tcpstate
= server_disconnected
;
514 timerclear(&ads
->tcptimeout
);
521 static int init_finish(adns_state ads
) {
523 struct protoent
*proto
;
526 if (!ads
->nservers
) {
527 if (ads
->diagfile
&& ads
->iflags
& adns_if_debug
)
528 fprintf(ads
->diagfile
,"adns: no nameservers, using localhost\n");
529 ia
.s_addr
= htonl(INADDR_LOOPBACK
);
533 proto
= getprotobyname("udp"); if (!proto
) { r
= ENOPROTOOPT
; goto x_free
; }
534 ads
->udpsocket
= socket(AF_INET
,SOCK_DGRAM
,proto
->p_proto
);
535 if (ads
->udpsocket
<0) { r
= errno
; goto x_free
; }
537 r
= adns__setnonblock(ads
,ads
->udpsocket
);
538 if (r
) { r
= errno
; goto x_closeudp
; }
543 close(ads
->udpsocket
);
549 static void init_abort(adns_state ads
) {
550 if (ads
->nsearchlist
) {
551 free(ads
->searchlist
[0]);
552 free(ads
->searchlist
);
557 int adns_init(adns_state
*ads_r
, adns_initflags flags
, FILE *diagfile
) {
559 const char *res_options
, *adns_res_options
;
562 r
= init_begin(&ads
, flags
, diagfile ? diagfile
: stderr
);
565 res_options
= instrum_getenv(ads
,"RES_OPTIONS");
566 adns_res_options
= instrum_getenv(ads
,"ADNS_RES_OPTIONS");
567 ccf_options(ads
,"RES_OPTIONS",-1,res_options
);
568 ccf_options(ads
,"ADNS_RES_OPTIONS",-1,adns_res_options
);
570 readconfig(ads
,"/etc/resolv.conf",1);
571 readconfig(ads
,"/etc/resolv-adns.conf",0);
572 readconfigenv(ads
,"RES_CONF");
573 readconfigenv(ads
,"ADNS_RES_CONF");
575 readconfigenvtext(ads
,"RES_CONF_TEXT");
576 readconfigenvtext(ads
,"ADNS_RES_CONF_TEXT");
578 ccf_options(ads
,"RES_OPTIONS",-1,res_options
);
579 ccf_options(ads
,"ADNS_RES_OPTIONS",-1,adns_res_options
);
581 ccf_search(ads
,"LOCALDOMAIN",-1,instrum_getenv(ads
,"LOCALDOMAIN"));
582 ccf_search(ads
,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads
,"ADNS_LOCALDOMAIN"));
584 if (ads
->configerrno
&& ads
->configerrno
!= EINVAL
) {
593 adns__consistency(ads
,0,cc_entex
);
598 int adns_init_strcfg(adns_state
*ads_r
, adns_initflags flags
,
599 FILE *diagfile
, const char *configtext
) {
603 r
= init_begin(&ads
, flags
, diagfile
); if (r
) return r
;
605 readconfigtext(ads
,configtext
,"<supplied configuration text>");
606 if (ads
->configerrno
) {
612 r
= init_finish(ads
); if (r
) return r
;
613 adns__consistency(ads
,0,cc_entex
);
619 void adns_finish(adns_state ads
) {
620 adns__consistency(ads
,0,cc_entex
);
622 if (ads
->udpw
.head
) adns_cancel(ads
->udpw
.head
);
623 else if (ads
->tcpw
.head
) adns_cancel(ads
->tcpw
.head
);
624 else if (ads
->childw
.head
) adns_cancel(ads
->childw
.head
);
625 else if (ads
->output
.head
) adns_cancel(ads
->output
.head
);
628 close(ads
->udpsocket
);
629 if (ads
->tcpsocket
>= 0) close(ads
->tcpsocket
);
630 adns__vbuf_free(&ads
->tcpsend
);
631 adns__vbuf_free(&ads
->tcprecv
);
636 void adns_forallqueries_begin(adns_state ads
) {
637 adns__consistency(ads
,0,cc_entex
);
639 ads
->udpw
.head ? ads
->udpw
.head
:
640 ads
->tcpw
.head ? ads
->tcpw
.head
:
641 ads
->childw
.head ? ads
->childw
.head
:
645 adns_query
adns_forallqueries_next(adns_state ads
, void **context_r
) {
648 adns__consistency(ads
,0,cc_entex
);
649 nqu
= ads
->forallnext
;
655 } else if (qu
== ads
->udpw
.tail
) {
657 ads
->tcpw
.head ? ads
->tcpw
.head
:
658 ads
->childw
.head ? ads
->childw
.head
:
660 } else if (qu
== ads
->tcpw
.tail
) {
662 ads
->childw
.head ? ads
->childw
.head
:
664 } else if (qu
== ads
->childw
.tail
) {
665 nqu
= ads
->output
.head
;
669 if (!qu
->parent
) break;
671 ads
->forallnext
= nqu
;
672 if (context_r
) *context_r
= qu
->ctx
.ext
;