3 * - configuration file parsing
4 * - management of global state
7 * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include <arpa/inet.h>
35 static void readconfig(adns_state ads
, const char *filename
);
37 static void addserver(adns_state ads
, struct in_addr addr
) {
41 for (i
=0; i
<ads
->nservers
; i
++) {
42 if (ads
->servers
[i
].addr
.s_addr
== addr
.s_addr
) {
43 adns__debug(ads
,-1,0,"duplicate nameserver %s ignored",inet_ntoa(addr
));
48 if (ads
->nservers
>=MAXSERVERS
) {
49 adns__diag(ads
,-1,0,"too many nameservers, ignoring %s",inet_ntoa(addr
));
53 ss
= ads
->servers
+ads
->nservers
;
58 static void saveerr(adns_state ads
, int en
) {
59 if (!ads
->configerrno
) ads
->configerrno
= en
;
62 static void configparseerr(adns_state ads
, const char *fn
, int lno
,
63 const char *fmt
, ...) {
67 if (!ads
->diagfile
|| (ads
->iflags
& adns_if_noerrprint
)) return;
69 if (lno
==-1) fprintf(ads
->diagfile
,"adns: %s: ",fn
);
70 else fprintf(ads
->diagfile
,"adns: %s:%d: ",fn
,lno
);
72 vfprintf(ads
->diagfile
,fmt
,al
);
74 fputc('\n',ads
->diagfile
);
77 static void ccf_nameserver(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
80 if (!inet_aton(buf
,&ia
)) {
81 configparseerr(ads
,fn
,lno
,"invalid nameserver address `%s'",buf
);
84 adns__debug(ads
,-1,0,"using nameserver %s",inet_ntoa(ia
));
88 static void ccf_search(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
90 adns__diag(ads
,-1,0,"warning - `search' ignored fixme");
93 static void ccf_sortlist(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
94 adns__diag(ads
,-1,0,"warning - `sortlist' ignored fixme");
97 static void ccf_options(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
99 adns__diag(ads
,-1,0,"warning - `options' ignored fixme");
102 static void ccf_clearnss(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
106 static void ccf_include(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
108 configparseerr(ads
,fn
,lno
,"`include' directive with no filename");
114 static const struct configcommandinfo
{
116 void (*fn
)(adns_state ads
, const char *fn
, int lno
, const char *buf
);
117 } configcommandinfos
[]= {
118 { "nameserver", ccf_nameserver
},
119 { "domain", ccf_search
},
120 { "search", ccf_search
},
121 { "sortlist", ccf_sortlist
},
122 { "options", ccf_options
},
123 { "clearnameservers", ccf_clearnss
},
124 { "include", ccf_include
},
133 static int gl_file(adns_state ads
, getline_ctx
*src_io
, const char *filename
,
134 int lno
, char *buf
, int buflen
) {
135 FILE *file
= src_io
->file
;
143 for (;;) { /* loop over chars */
145 adns__diag(ads
,-1,0,"%s:%d: line too long, ignored",filename
,lno
);
150 adns__diag(ads
,-1,0,"%s:%d: line contains nul, ignored",filename
,lno
);
152 } else if (c
== '\n') {
154 } else if (c
== EOF
) {
157 adns__diag(ads
,-1,0,"%s:%d: read error: %s",filename
,lno
,strerror(errno
));
173 while ((c
= getc(file
)) != EOF
&& c
!= '\n');
177 static int gl_text(adns_state ads
, getline_ctx
*src_io
, const char *filename
,
178 int lno
, char *buf
, int buflen
) {
179 const char *cp
= src_io
->text
, *nn
;
186 l
= nn ? nn
-cp
: strlen(cp
);
187 src_io
->text
= nn ? nn
+1 : 0;
190 adns__diag(ads
,-1,0,"%s:%d: line too long, ignored",filename
,lno
);
200 static void readconfiggeneric(adns_state ads
, const char *filename
,
201 int (*getline
)(adns_state ads
, getline_ctx
*,
202 const char *filename
, int lno
,
203 char *buf
, int buflen
),
204 /* Returns >=0 for success, -1 for EOF or error
205 * (error will have been reported), or -2 for
206 * bad line was encountered, try again.
208 getline_ctx gl_ctx
) {
209 char linebuf
[2000], *p
, *q
;
211 const struct configcommandinfo
*ccip
;
214 (l
= getline(ads
,&gl_ctx
, filename
,lno
, linebuf
,sizeof(linebuf
))) != -1;
216 if (l
== -2) continue;
217 while (l
>0 && ctype_whitespace(linebuf
[l
-1])) l
--;
220 while (ctype_whitespace(*p
)) p
++;
221 if (*p
== '#' || !*p
) continue;
223 while (*q
&& !ctype_whitespace(*q
)) q
++;
225 for (ccip
=configcommandinfos
;
226 ccip
->name
&& !(strlen(ccip
->name
)==dirl
&& !memcmp(ccip
->name
,p
,q
-p
));
229 adns__diag(ads
,-1,0,"%s:%d: unknown configuration directive `%.*s'",
233 while (ctype_whitespace(*q
)) q
++;
234 ccip
->fn(ads
,filename
,lno
,q
);
238 static const char *instrum_getenv(adns_state ads
, const char *envvar
) {
241 value
= getenv(envvar
);
242 if (!value
) adns__debug(ads
,-1,0,"environment variable %s not set",envvar
);
243 else adns__debug(ads
,-1,0,"environment variable %s set to `%s'",envvar
,value
);
247 static void readconfig(adns_state ads
, const char *filename
) {
250 gl_ctx
.file
= fopen(filename
,"r");
252 if (errno
== ENOENT
) {
253 adns__debug(ads
,-1,0,"configuration file `%s' does not exist",filename
);
257 adns__diag(ads
,-1,0,"cannot open configuration file `%s': %s",
258 filename
,strerror(errno
));
262 readconfiggeneric(ads
,filename
,gl_file
,gl_ctx
);
267 static void readconfigtext(adns_state ads
, const char *text
, const char *showname
) {
271 readconfiggeneric(ads
,showname
,gl_text
,gl_ctx
);
274 static void readconfigenv(adns_state ads
, const char *envvar
) {
275 const char *filename
;
277 if (ads
->iflags
& adns_if_noenv
) {
278 adns__debug(ads
,-1,0,"not checking environment variable `%s'",envvar
);
281 filename
= instrum_getenv(ads
,envvar
);
282 if (filename
) readconfig(ads
,filename
);
286 int adns__setnonblock(adns_state ads
, int fd
) {
289 r
= fcntl(fd
,F_GETFL
,0); if (r
<0) return errno
;
291 r
= fcntl(fd
,F_SETFL
,r
); if (r
<0) return errno
;
295 static int init_begin(adns_state
*ads_r
, adns_initflags flags
, FILE *diagfile
) {
298 ads
= malloc(sizeof(*ads
)); if (!ads
) return errno
;
301 ads
->diagfile
= diagfile
;
302 LIST_INIT(ads
->timew
);
303 LIST_INIT(ads
->childw
);
304 LIST_INIT(ads
->output
);
306 ads
->udpsocket
= ads
->tcpsocket
= -1;
307 adns__vbuf_init(&ads
->tcpsend
);
308 adns__vbuf_init(&ads
->tcprecv
);
309 ads
->nservers
= ads
->tcpserver
= 0;
310 ads
->tcpstate
= server_disconnected
;
311 timerclear(&ads
->tcptimeout
);
317 static int init_finish(adns_state ads
) {
319 struct protoent
*proto
;
322 if (!ads
->nservers
) {
323 if (ads
->diagfile
&& ads
->iflags
& adns_if_debug
)
324 fprintf(ads
->diagfile
,"adns: no nameservers, using localhost\n");
325 ia
.s_addr
= INADDR_LOOPBACK
;
329 proto
= getprotobyname("udp"); if (!proto
) { r
= ENOPROTOOPT
; goto x_free
; }
330 ads
->udpsocket
= socket(AF_INET
,SOCK_DGRAM
,proto
->p_proto
);
331 if (ads
->udpsocket
<0) { r
= errno
; goto x_free
; }
333 r
= adns__setnonblock(ads
,ads
->udpsocket
);
334 if (r
) { r
= errno
; goto x_closeudp
; }
339 close(ads
->udpsocket
);
345 int adns_init(adns_state
*ads_r
, adns_initflags flags
, FILE *diagfile
) {
347 const char *res_options
, *adns_res_options
;
350 r
= init_begin(&ads
, flags
, diagfile ? diagfile
: stderr
);
353 res_options
= instrum_getenv(ads
,"RES_OPTIONS");
354 adns_res_options
= instrum_getenv(ads
,"ADNS_RES_OPTIONS");
355 ccf_options(ads
,"RES_OPTIONS",-1,res_options
);
356 ccf_options(ads
,"ADNS_RES_OPTIONS",-1,adns_res_options
);
358 readconfig(ads
,"/etc/resolv.conf");
359 readconfigenv(ads
,"RES_CONF");
360 readconfigenv(ads
,"ADNS_RES_CONF");
362 ccf_options(ads
,"RES_OPTIONS",-1,res_options
);
363 ccf_options(ads
,"ADNS_RES_OPTIONS",-1,adns_res_options
);
365 ccf_search(ads
,"LOCALDOMAIN",-1,instrum_getenv(ads
,"LOCALDOMAIN"));
366 ccf_search(ads
,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads
,"ADNS_LOCALDOMAIN"));
375 int adns_init_strcfg(adns_state
*ads_r
, adns_initflags flags
,
376 FILE *diagfile
, const char *configtext
) {
380 r
= init_begin(&ads
, flags
, diagfile
); if (r
) return r
;
382 readconfigtext(ads
,configtext
,"<supplied configuration text>");
383 if (ads
->configerrno
) {
389 r
= init_finish(ads
); if (r
) return r
;
394 void adns_finish(adns_state ads
) {
396 if (ads
->timew
.head
) adns_cancel(ads
->timew
.head
);
397 else if (ads
->childw
.head
) adns_cancel(ads
->childw
.head
);
398 else if (ads
->output
.head
) adns_cancel(ads
->output
.head
);
401 close(ads
->udpsocket
);
402 if (ads
->tcpsocket
>= 0) close(ads
->tcpsocket
);
403 adns__vbuf_free(&ads
->tcpsend
);
404 adns__vbuf_free(&ads
->tcprecv
);