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.
32 #include <arpa/inet.h>
36 static void readconfig(adns_state ads, const char *filename);
38 static void addserver(adns_state ads, struct in_addr addr) {
42 for (i=0; i<ads->nservers; i++) {
43 if (ads->servers[i].addr.s_addr == addr.s_addr) {
44 adns__debug(ads,-1,0,"duplicate nameserver %s ignored",inet_ntoa(addr));
49 if (ads->nservers>=MAXSERVERS) {
50 adns__diag(ads,-1,0,"too many nameservers, ignoring %s",inet_ntoa(addr));
54 ss= ads->servers+ads->nservers;
59 static void saveerr(adns_state ads, int en) {
60 if (!ads->configerrno) ads->configerrno= en;
63 static void configparseerr(adns_state ads, const char *fn, int lno,
64 const char *fmt, ...) {
68 if (!ads->diagfile || (ads->iflags & adns_if_noerrprint)) return;
70 if (lno==-1) fprintf(ads->diagfile,"adns: %s: ",fn);
71 else fprintf(ads->diagfile,"adns: %s:%d: ",fn,lno);
73 vfprintf(ads->diagfile,fmt,al);
75 fputc('\n',ads->diagfile);
78 static int nextword(const char **bufp_io, const char **word_r, int *l_r) {
82 while (ctype_whitespace(*p)) p++;
86 while (*q && !ctype_whitespace(*q)) q++;
95 static void ccf_nameserver(adns_state ads, const char *fn, int lno, const char *buf) {
98 if (!inet_aton(buf,&ia)) {
99 configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
102 adns__debug(ads,-1,0,"using nameserver %s",inet_ntoa(ia));
106 static void ccf_search(adns_state ads, const char *fn, int lno, const char *buf) {
107 const char *bufp, *word;
108 char *newchars, **newptrs, **pp;
116 while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
118 newptrs= malloc(sizeof(char*)*count); if (!newptrs) { saveerr(ads,errno); return; }
119 newchars= malloc(tl); if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
123 while (nextword(&bufp,&word,&l)) {
125 memcpy(newchars,word,l);
130 free(ads->searchlist);
131 ads->nsearchlist= count;
132 ads->searchlist= newptrs;
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);
156 memcpy(tbuf,word,l); tbuf[l]= 0;
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) {
214 while (nextword(&buf,&word,&l)) {
215 if (l==5 && !memcmp(word,"debug",5)) {
216 ads->iflags |= adns_if_debug;
219 if (l>=6 && !memcmp(word,"ndots:",6)) {
220 v= strtoul(word+6,&ep,10);
221 if (l==6 || ep != word+l || v > INT_MAX) {
222 configparseerr(ads,fn,lno,"option `%.*s' malformed or has bad value",l,word);
228 adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
232 static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) {
236 static void ccf_include(adns_state ads, const char *fn, int lno, const char *buf) {
238 configparseerr(ads,fn,lno,"`include' directive with no filename");
244 static const struct configcommandinfo {
246 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
247 } configcommandinfos[]= {
248 { "nameserver", ccf_nameserver },
249 { "domain", ccf_search },
250 { "search", ccf_search },
251 { "sortlist", ccf_sortlist },
252 { "options", ccf_options },
253 { "clearnameservers", ccf_clearnss },
254 { "include", ccf_include },
263 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
264 int lno, char *buf, int buflen) {
265 FILE *file= src_io->file;
273 for (;;) { /* loop over chars */
275 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
280 adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
282 } else if (c == '\n') {
284 } else if (c == EOF) {
287 adns__diag(ads,-1,0,"%s:%d: read error: %s",filename,lno,strerror(errno));
303 while ((c= getc(file)) != EOF && c != '\n');
307 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
308 int lno, char *buf, int buflen) {
309 const char *cp= src_io->text;
312 if (!cp || !*cp) return -1;
314 if (*cp == ';' || *cp == '\n') cp++;
315 l= strcspn(cp,";\n");
319 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
329 static void readconfiggeneric(adns_state ads, const char *filename,
330 int (*getline)(adns_state ads, getline_ctx*,
331 const char *filename, int lno,
332 char *buf, int buflen),
333 /* Returns >=0 for success, -1 for EOF or error
334 * (error will have been reported), or -2 for
335 * bad line was encountered, try again.
337 getline_ctx gl_ctx) {
338 char linebuf[2000], *p, *q;
340 const struct configcommandinfo *ccip;
343 (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
345 if (l == -2) continue;
346 while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
349 while (ctype_whitespace(*p)) p++;
350 if (*p == '#' || !*p) continue;
352 while (*q && !ctype_whitespace(*q)) q++;
354 for (ccip=configcommandinfos;
355 ccip->name && !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
358 adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
362 while (ctype_whitespace(*q)) q++;
363 ccip->fn(ads,filename,lno,q);
367 static const char *instrum_getenv(adns_state ads, const char *envvar) {
370 value= getenv(envvar);
371 if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
372 else adns__debug(ads,-1,0,"environment variable %s set to `%s'",envvar,value);
376 static void readconfig(adns_state ads, const char *filename) {
379 gl_ctx.file= fopen(filename,"r");
381 if (errno == ENOENT) {
382 adns__debug(ads,-1,0,"configuration file `%s' does not exist",filename);
386 adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
387 filename,strerror(errno));
391 readconfiggeneric(ads,filename,gl_file,gl_ctx);
396 static void readconfigtext(adns_state ads, const char *text, const char *showname) {
400 readconfiggeneric(ads,showname,gl_text,gl_ctx);
403 static void readconfigenv(adns_state ads, const char *envvar) {
404 const char *filename;
406 if (ads->iflags & adns_if_noenv) {
407 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
410 filename= instrum_getenv(ads,envvar);
411 if (filename) readconfig(ads,filename);
414 static void readconfigenvtext(adns_state ads, const char *envvar) {
415 const char *textdata;
417 if (ads->iflags & adns_if_noenv) {
418 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
421 textdata= instrum_getenv(ads,envvar);
422 if (textdata) readconfigtext(ads,textdata,envvar);
426 int adns__setnonblock(adns_state ads, int fd) {
429 r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
431 r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
435 static int init_begin(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
438 ads= malloc(sizeof(*ads)); if (!ads) return errno;
441 ads->diagfile= diagfile;
442 LIST_INIT(ads->timew);
443 LIST_INIT(ads->childw);
444 LIST_INIT(ads->output);
446 ads->udpsocket= ads->tcpsocket= -1;
447 adns__vbuf_init(&ads->tcpsend);
448 adns__vbuf_init(&ads->tcprecv);
449 ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
450 ads->tcpstate= server_disconnected;
453 timerclear(&ads->tcptimeout);
459 static int init_finish(adns_state ads) {
461 struct protoent *proto;
464 if (!ads->nservers) {
465 if (ads->diagfile && ads->iflags & adns_if_debug)
466 fprintf(ads->diagfile,"adns: no nameservers, using localhost\n");
467 ia.s_addr= htonl(INADDR_LOOPBACK);
471 proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
472 ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
473 if (ads->udpsocket<0) { r= errno; goto x_free; }
475 r= adns__setnonblock(ads,ads->udpsocket);
476 if (r) { r= errno; goto x_closeudp; }
481 close(ads->udpsocket);
487 static void init_abort(adns_state ads) {
488 if (ads->nsearchlist) {
489 free(ads->searchlist[0]);
490 free(ads->searchlist);
495 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
497 const char *res_options, *adns_res_options;
500 r= init_begin(&ads, flags, diagfile ? diagfile : stderr);
503 res_options= instrum_getenv(ads,"RES_OPTIONS");
504 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
505 ccf_options(ads,"RES_OPTIONS",-1,res_options);
506 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
508 readconfig(ads,"/etc/resolv.conf");
509 readconfigenv(ads,"RES_CONF");
510 readconfigenv(ads,"ADNS_RES_CONF");
512 readconfigenvtext(ads,"RES_CONF_TEXT");
513 readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
515 ccf_options(ads,"RES_OPTIONS",-1,res_options);
516 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
518 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
519 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
521 if (ads->configerrno && ads->configerrno != EINVAL) {
534 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
535 FILE *diagfile, const char *configtext) {
539 r= init_begin(&ads, flags, diagfile); if (r) return r;
541 readconfigtext(ads,configtext,"<supplied configuration text>");
542 if (ads->configerrno) {
548 r= init_finish(ads); if (r) return r;
553 void adns_finish(adns_state ads) {
555 if (ads->timew.head) adns_cancel(ads->timew.head);
556 else if (ads->childw.head) adns_cancel(ads->childw.head);
557 else if (ads->output.head) adns_cancel(ads->output.head);
560 close(ads->udpsocket);
561 if (ads->tcpsocket >= 0) close(ads->tcpsocket);
562 adns__vbuf_free(&ads->tcpsend);
563 adns__vbuf_free(&ads->tcprecv);