3 * - configuration file parsing
4 * - management of global state
7 * This file is part of adns, which is Copyright (C) 1997-1999 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 int nextword(const char **bufp_io
, const char **word_r
, int *l_r
) {
81 while (ctype_whitespace(*p
)) p
++;
85 while (*q
&& !ctype_whitespace(*q
)) q
++;
94 static void ccf_nameserver(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
97 if (!inet_aton(buf
,&ia
)) {
98 configparseerr(ads
,fn
,lno
,"invalid nameserver address `%s'",buf
);
101 adns__debug(ads
,-1,0,"using nameserver %s",inet_ntoa(ia
));
105 static void ccf_search(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
106 const char *bufp
, *word
;
107 char *newchars
, **newptrs
, **pp
;
115 while (nextword(&bufp
,&word
,&l
)) { count
++; tl
+= l
+1; }
117 newptrs
= malloc(sizeof(char*)*count
); if (!newptrs
) { saveerr(ads
,errno
); return; }
118 newchars
= malloc(tl
); if (!newchars
) { saveerr(ads
,errno
); free(newptrs
); return; }
122 while (nextword(&bufp
,&word
,&l
)) {
124 memcpy(newchars
,word
,l
);
129 free(ads
->searchlist
);
130 ads
->nsearchlist
= count
;
131 ads
->searchlist
= newptrs
;
132 /* fixme: actually pay attention */
135 static void ccf_sortlist(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
137 char tbuf
[200], *slash
, *ep
;
138 struct in_addr base
, mask
;
140 unsigned long initial
, baselocal
;
145 while (nextword(&buf
,&word
,&l
)) {
146 if (ads
->nsortlist
>= MAXSORTLIST
) {
147 adns__diag(ads
,-1,0,"too many sortlist entries, ignoring %.*s onwards",l
,word
);
151 if (l
>= sizeof(tbuf
)) {
152 configparseerr(ads
,fn
,lno
,"sortlist entry `%.*s' too long",l
,word
);
157 slash
= strchr(tbuf
,'/');
158 if (slash
) *slash
++= 0;
160 if (!inet_aton(tbuf
,&base
)) {
161 configparseerr(ads
,fn
,lno
,"invalid address `%s' in sortlist",tbuf
);
166 if (strchr(slash
,'.')) {
167 if (!inet_aton(slash
,&mask
)) {
168 configparseerr(ads
,fn
,lno
,"invalid mask `%s' in sortlist",slash
);
171 if (base
.s_addr
& ~mask
.s_addr
) {
172 configparseerr(ads
,fn
,lno
,
173 "mask `%s' in sortlist overlaps address `%s'",slash
,tbuf
);
177 initial
= strtoul(slash
,&ep
,10);
178 if (*ep
|| initial
>32) {
179 configparseerr(ads
,fn
,lno
,"mask length `%s' invalid",slash
);
182 mask
.s_addr
= htonl((0x0ffffffffUL
) << (32-initial
));
185 baselocal
= ntohl(base
.s_addr
);
186 if (!baselocal
& 0x080000000UL
) /* class A */
187 mask
.s_addr
= htonl(0x0ff000000UL
);
188 else if ((baselocal
& 0x0c0000000UL
) == 0x080000000UL
)
189 mask
.s_addr
= htonl(0x0ffff0000UL
); /* class B */
190 else if ((baselocal
& 0x0f0000000UL
) == 0x0e0000000UL
)
191 mask
.s_addr
= htonl(0x0ff000000UL
); /* class C */
193 configparseerr(ads
,fn
,lno
,
194 "network address `%s' in sortlist is not in classed ranges,"
195 " must specify mask explicitly", tbuf
);
200 ads
->sortlist
[ads
->nsortlist
].base
= base
;
201 ads
->sortlist
[ads
->nsortlist
].mask
= mask
;
206 static void ccf_options(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
208 adns__diag(ads
,-1,0,"warning - `options' ignored fixme");
211 static void ccf_clearnss(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
215 static void ccf_include(adns_state ads
, const char *fn
, int lno
, const char *buf
) {
217 configparseerr(ads
,fn
,lno
,"`include' directive with no filename");
223 static const struct configcommandinfo
{
225 void (*fn
)(adns_state ads
, const char *fn
, int lno
, const char *buf
);
226 } configcommandinfos
[]= {
227 { "nameserver", ccf_nameserver
},
228 { "domain", ccf_search
},
229 { "search", ccf_search
},
230 { "sortlist", ccf_sortlist
},
231 { "options", ccf_options
},
232 { "clearnameservers", ccf_clearnss
},
233 { "include", ccf_include
},
242 static int gl_file(adns_state ads
, getline_ctx
*src_io
, const char *filename
,
243 int lno
, char *buf
, int buflen
) {
244 FILE *file
= src_io
->file
;
252 for (;;) { /* loop over chars */
254 adns__diag(ads
,-1,0,"%s:%d: line too long, ignored",filename
,lno
);
259 adns__diag(ads
,-1,0,"%s:%d: line contains nul, ignored",filename
,lno
);
261 } else if (c
== '\n') {
263 } else if (c
== EOF
) {
266 adns__diag(ads
,-1,0,"%s:%d: read error: %s",filename
,lno
,strerror(errno
));
282 while ((c
= getc(file
)) != EOF
&& c
!= '\n');
286 static int gl_text(adns_state ads
, getline_ctx
*src_io
, const char *filename
,
287 int lno
, char *buf
, int buflen
) {
288 const char *cp
= src_io
->text
;
291 if (!cp
|| !*cp
) return -1;
293 if (*cp
== ';' || *cp
== '\n') cp
++;
294 l
= strcspn(cp
,";\n");
298 adns__diag(ads
,-1,0,"%s:%d: line too long, ignored",filename
,lno
);
308 static void readconfiggeneric(adns_state ads
, const char *filename
,
309 int (*getline
)(adns_state ads
, getline_ctx
*,
310 const char *filename
, int lno
,
311 char *buf
, int buflen
),
312 /* Returns >=0 for success, -1 for EOF or error
313 * (error will have been reported), or -2 for
314 * bad line was encountered, try again.
316 getline_ctx gl_ctx
) {
317 char linebuf
[2000], *p
, *q
;
319 const struct configcommandinfo
*ccip
;
322 (l
= getline(ads
,&gl_ctx
, filename
,lno
, linebuf
,sizeof(linebuf
))) != -1;
324 if (l
== -2) continue;
325 while (l
>0 && ctype_whitespace(linebuf
[l
-1])) l
--;
328 while (ctype_whitespace(*p
)) p
++;
329 if (*p
== '#' || !*p
) continue;
331 while (*q
&& !ctype_whitespace(*q
)) q
++;
333 for (ccip
=configcommandinfos
;
334 ccip
->name
&& !(strlen(ccip
->name
)==dirl
&& !memcmp(ccip
->name
,p
,q
-p
));
337 adns__diag(ads
,-1,0,"%s:%d: unknown configuration directive `%.*s'",
341 while (ctype_whitespace(*q
)) q
++;
342 ccip
->fn(ads
,filename
,lno
,q
);
346 static const char *instrum_getenv(adns_state ads
, const char *envvar
) {
349 value
= getenv(envvar
);
350 if (!value
) adns__debug(ads
,-1,0,"environment variable %s not set",envvar
);
351 else adns__debug(ads
,-1,0,"environment variable %s set to `%s'",envvar
,value
);
355 static void readconfig(adns_state ads
, const char *filename
) {
358 gl_ctx
.file
= fopen(filename
,"r");
360 if (errno
== ENOENT
) {
361 adns__debug(ads
,-1,0,"configuration file `%s' does not exist",filename
);
365 adns__diag(ads
,-1,0,"cannot open configuration file `%s': %s",
366 filename
,strerror(errno
));
370 readconfiggeneric(ads
,filename
,gl_file
,gl_ctx
);
375 static void readconfigtext(adns_state ads
, const char *text
, const char *showname
) {
379 readconfiggeneric(ads
,showname
,gl_text
,gl_ctx
);
382 static void readconfigenv(adns_state ads
, const char *envvar
) {
383 const char *filename
;
385 if (ads
->iflags
& adns_if_noenv
) {
386 adns__debug(ads
,-1,0,"not checking environment variable `%s'",envvar
);
389 filename
= instrum_getenv(ads
,envvar
);
390 if (filename
) readconfig(ads
,filename
);
393 static void readconfigenvtext(adns_state ads
, const char *envvar
) {
394 const char *textdata
;
396 if (ads
->iflags
& adns_if_noenv
) {
397 adns__debug(ads
,-1,0,"not checking environment variable `%s'",envvar
);
400 textdata
= instrum_getenv(ads
,envvar
);
401 if (textdata
) readconfigtext(ads
,textdata
,envvar
);
405 int adns__setnonblock(adns_state ads
, int fd
) {
408 r
= fcntl(fd
,F_GETFL
,0); if (r
<0) return errno
;
410 r
= fcntl(fd
,F_SETFL
,r
); if (r
<0) return errno
;
414 static int init_begin(adns_state
*ads_r
, adns_initflags flags
, FILE *diagfile
) {
417 ads
= malloc(sizeof(*ads
)); if (!ads
) return errno
;
420 ads
->diagfile
= diagfile
;
421 LIST_INIT(ads
->timew
);
422 LIST_INIT(ads
->childw
);
423 LIST_INIT(ads
->output
);
425 ads
->udpsocket
= ads
->tcpsocket
= -1;
426 adns__vbuf_init(&ads
->tcpsend
);
427 adns__vbuf_init(&ads
->tcprecv
);
428 ads
->nservers
= ads
->nsortlist
= ads
->nsearchlist
= ads
->tcpserver
= 0;
429 ads
->tcpstate
= server_disconnected
;
432 timerclear(&ads
->tcptimeout
);
438 static int init_finish(adns_state ads
) {
440 struct protoent
*proto
;
443 if (!ads
->nservers
) {
444 if (ads
->diagfile
&& ads
->iflags
& adns_if_debug
)
445 fprintf(ads
->diagfile
,"adns: no nameservers, using localhost\n");
446 ia
.s_addr
= htonl(INADDR_LOOPBACK
);
450 proto
= getprotobyname("udp"); if (!proto
) { r
= ENOPROTOOPT
; goto x_free
; }
451 ads
->udpsocket
= socket(AF_INET
,SOCK_DGRAM
,proto
->p_proto
);
452 if (ads
->udpsocket
<0) { r
= errno
; goto x_free
; }
454 r
= adns__setnonblock(ads
,ads
->udpsocket
);
455 if (r
) { r
= errno
; goto x_closeudp
; }
460 close(ads
->udpsocket
);
466 static void init_abort(adns_state ads
) {
467 if (ads
->nsearchlist
) {
468 free(ads
->searchlist
[0]);
469 free(ads
->searchlist
);
474 int adns_init(adns_state
*ads_r
, adns_initflags flags
, FILE *diagfile
) {
476 const char *res_options
, *adns_res_options
;
479 r
= init_begin(&ads
, flags
, diagfile ? diagfile
: stderr
);
482 res_options
= instrum_getenv(ads
,"RES_OPTIONS");
483 adns_res_options
= instrum_getenv(ads
,"ADNS_RES_OPTIONS");
484 ccf_options(ads
,"RES_OPTIONS",-1,res_options
);
485 ccf_options(ads
,"ADNS_RES_OPTIONS",-1,adns_res_options
);
487 readconfig(ads
,"/etc/resolv.conf");
488 readconfigenv(ads
,"RES_CONF");
489 readconfigenv(ads
,"ADNS_RES_CONF");
491 readconfigenvtext(ads
,"RES_CONF_TEXT");
492 readconfigenvtext(ads
,"ADNS_RES_CONF_TEXT");
494 ccf_options(ads
,"RES_OPTIONS",-1,res_options
);
495 ccf_options(ads
,"ADNS_RES_OPTIONS",-1,adns_res_options
);
497 ccf_search(ads
,"LOCALDOMAIN",-1,instrum_getenv(ads
,"LOCALDOMAIN"));
498 ccf_search(ads
,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads
,"ADNS_LOCALDOMAIN"));
500 if (ads
->configerrno
&& ads
->configerrno
!= EINVAL
) {
513 int adns_init_strcfg(adns_state
*ads_r
, adns_initflags flags
,
514 FILE *diagfile
, const char *configtext
) {
518 r
= init_begin(&ads
, flags
, diagfile
); if (r
) return r
;
520 readconfigtext(ads
,configtext
,"<supplied configuration text>");
521 if (ads
->configerrno
) {
527 r
= init_finish(ads
); if (r
) return r
;
532 void adns_finish(adns_state ads
) {
534 if (ads
->timew
.head
) adns_cancel(ads
->timew
.head
);
535 else if (ads
->childw
.head
) adns_cancel(ads
->childw
.head
);
536 else if (ads
->output
.head
) adns_cancel(ads
->output
.head
);
539 close(ads
->udpsocket
);
540 if (ads
->tcpsocket
>= 0) close(ads
->tcpsocket
);
541 adns__vbuf_free(&ads
->tcpsend
);
542 adns__vbuf_free(&ads
->tcprecv
);