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 <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
38 static void readconfig(adns_state ads, const char *filename);
40 static void addserver(adns_state ads, struct in_addr addr) {
44 for (i=0; i<ads->nservers; i++) {
45 if (ads->servers[i].addr.s_addr == addr.s_addr) {
46 adns__debug(ads,-1,0,"duplicate nameserver %s ignored",inet_ntoa(addr));
51 if (ads->nservers>=MAXSERVERS) {
52 adns__diag(ads,-1,0,"too many nameservers, ignoring %s",inet_ntoa(addr));
56 ss= ads->servers+ads->nservers;
61 static void saveerr(adns_state ads, int en) {
62 if (!ads->configerrno) ads->configerrno= en;
65 static void configparseerr(adns_state ads, const char *fn, int lno,
66 const char *fmt, ...) {
70 if (!ads->diagfile || (ads->iflags & adns_if_noerrprint)) return;
72 if (lno==-1) fprintf(ads->diagfile,"adns: %s: ",fn);
73 else fprintf(ads->diagfile,"adns: %s:%d: ",fn,lno);
75 vfprintf(ads->diagfile,fmt,al);
77 fputc('\n',ads->diagfile);
80 static int nextword(const char **bufp_io, const char **word_r, int *l_r) {
84 while (ctype_whitespace(*p)) p++;
88 while (*q && !ctype_whitespace(*q)) q++;
97 static void ccf_nameserver(adns_state ads, const char *fn, int lno, const char *buf) {
100 if (!inet_aton(buf,&ia)) {
101 configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
104 adns__debug(ads,-1,0,"using nameserver %s",inet_ntoa(ia));
108 static void ccf_search(adns_state ads, const char *fn, int lno, const char *buf) {
109 const char *bufp, *word;
110 char *newchars, **newptrs, **pp;
118 while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
120 newptrs= malloc(sizeof(char*)*count); if (!newptrs) { saveerr(ads,errno); return; }
121 newchars= malloc(tl); if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
125 while (nextword(&bufp,&word,&l)) {
127 memcpy(newchars,word,l);
132 free(ads->searchlist);
133 ads->nsearchlist= count;
134 ads->searchlist= newptrs;
137 static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *buf) {
139 char tbuf[200], *slash, *ep;
140 struct in_addr base, mask;
142 unsigned long initial, baselocal;
147 while (nextword(&buf,&word,&l)) {
148 if (ads->nsortlist >= MAXSORTLIST) {
149 adns__diag(ads,-1,0,"too many sortlist entries, ignoring %.*s onwards",l,word);
153 if (l >= sizeof(tbuf)) {
154 configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
158 memcpy(tbuf,word,l); tbuf[l]= 0;
159 slash= strchr(tbuf,'/');
160 if (slash) *slash++= 0;
162 if (!inet_aton(tbuf,&base)) {
163 configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
168 if (strchr(slash,'.')) {
169 if (!inet_aton(slash,&mask)) {
170 configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
173 if (base.s_addr & ~mask.s_addr) {
174 configparseerr(ads,fn,lno,
175 "mask `%s' in sortlist overlaps address `%s'",slash,tbuf);
179 initial= strtoul(slash,&ep,10);
180 if (*ep || initial>32) {
181 configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
184 mask.s_addr= htonl((0x0ffffffffUL) << (32-initial));
187 baselocal= ntohl(base.s_addr);
188 if (!baselocal & 0x080000000UL) /* class A */
189 mask.s_addr= htonl(0x0ff000000UL);
190 else if ((baselocal & 0x0c0000000UL) == 0x080000000UL)
191 mask.s_addr= htonl(0x0ffff0000UL); /* class B */
192 else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL)
193 mask.s_addr= htonl(0x0ff000000UL); /* class C */
195 configparseerr(ads,fn,lno,
196 "network address `%s' in sortlist is not in classed ranges,"
197 " must specify mask explicitly", tbuf);
202 ads->sortlist[ads->nsortlist].base= base;
203 ads->sortlist[ads->nsortlist].mask= mask;
208 static void ccf_options(adns_state ads, const char *fn, int lno, const char *buf) {
216 while (nextword(&buf,&word,&l)) {
217 if (l==5 && !memcmp(word,"debug",5)) {
218 ads->iflags |= adns_if_debug;
221 if (l>=6 && !memcmp(word,"ndots:",6)) {
222 v= strtoul(word+6,&ep,10);
223 if (l==6 || ep != word+l || v > INT_MAX) {
224 configparseerr(ads,fn,lno,"option `%.*s' malformed or has bad value",l,word);
230 adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
234 static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) {
238 static void ccf_include(adns_state ads, const char *fn, int lno, const char *buf) {
240 configparseerr(ads,fn,lno,"`include' directive with no filename");
246 static const struct configcommandinfo {
248 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
249 } configcommandinfos[]= {
250 { "nameserver", ccf_nameserver },
251 { "domain", ccf_search },
252 { "search", ccf_search },
253 { "sortlist", ccf_sortlist },
254 { "options", ccf_options },
255 { "clearnameservers", ccf_clearnss },
256 { "include", ccf_include },
265 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
266 int lno, char *buf, int buflen) {
267 FILE *file= src_io->file;
275 for (;;) { /* loop over chars */
277 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
282 adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
284 } else if (c == '\n') {
286 } else if (c == EOF) {
289 adns__diag(ads,-1,0,"%s:%d: read error: %s",filename,lno,strerror(errno));
305 while ((c= getc(file)) != EOF && c != '\n');
309 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
310 int lno, char *buf, int buflen) {
311 const char *cp= src_io->text;
314 if (!cp || !*cp) return -1;
316 if (*cp == ';' || *cp == '\n') cp++;
317 l= strcspn(cp,";\n");
321 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
331 static void readconfiggeneric(adns_state ads, const char *filename,
332 int (*getline)(adns_state ads, getline_ctx*,
333 const char *filename, int lno,
334 char *buf, int buflen),
335 /* Returns >=0 for success, -1 for EOF or error
336 * (error will have been reported), or -2 for
337 * bad line was encountered, try again.
339 getline_ctx gl_ctx) {
340 char linebuf[2000], *p, *q;
342 const struct configcommandinfo *ccip;
345 (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
347 if (l == -2) continue;
348 while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
351 while (ctype_whitespace(*p)) p++;
352 if (*p == '#' || !*p) continue;
354 while (*q && !ctype_whitespace(*q)) q++;
356 for (ccip=configcommandinfos;
357 ccip->name && !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
360 adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
364 while (ctype_whitespace(*q)) q++;
365 ccip->fn(ads,filename,lno,q);
369 static const char *instrum_getenv(adns_state ads, const char *envvar) {
372 value= getenv(envvar);
373 if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
374 else adns__debug(ads,-1,0,"environment variable %s set to `%s'",envvar,value);
378 static void readconfig(adns_state ads, const char *filename) {
381 gl_ctx.file= fopen(filename,"r");
383 if (errno == ENOENT) {
384 adns__debug(ads,-1,0,"configuration file `%s' does not exist",filename);
388 adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
389 filename,strerror(errno));
393 readconfiggeneric(ads,filename,gl_file,gl_ctx);
398 static void readconfigtext(adns_state ads, const char *text, const char *showname) {
402 readconfiggeneric(ads,showname,gl_text,gl_ctx);
405 static void readconfigenv(adns_state ads, const char *envvar) {
406 const char *filename;
408 if (ads->iflags & adns_if_noenv) {
409 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
412 filename= instrum_getenv(ads,envvar);
413 if (filename) readconfig(ads,filename);
416 static void readconfigenvtext(adns_state ads, const char *envvar) {
417 const char *textdata;
419 if (ads->iflags & adns_if_noenv) {
420 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
423 textdata= instrum_getenv(ads,envvar);
424 if (textdata) readconfigtext(ads,textdata,envvar);
428 int adns__setnonblock(adns_state ads, int fd) {
431 r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
433 r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
437 static int init_begin(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
440 ads= malloc(sizeof(*ads)); if (!ads) return errno;
443 ads->diagfile= diagfile;
444 LIST_INIT(ads->timew);
445 LIST_INIT(ads->childw);
446 LIST_INIT(ads->output);
449 ads->udpsocket= ads->tcpsocket= -1;
450 adns__vbuf_init(&ads->tcpsend);
451 adns__vbuf_init(&ads->tcprecv);
452 ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
453 ads->tcpstate= server_disconnected;
456 timerclear(&ads->tcptimeout);
462 static int init_finish(adns_state ads) {
464 struct protoent *proto;
467 if (!ads->nservers) {
468 if (ads->diagfile && ads->iflags & adns_if_debug)
469 fprintf(ads->diagfile,"adns: no nameservers, using localhost\n");
470 ia.s_addr= htonl(INADDR_LOOPBACK);
474 proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
475 ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
476 if (ads->udpsocket<0) { r= errno; goto x_free; }
478 r= adns__setnonblock(ads,ads->udpsocket);
479 if (r) { r= errno; goto x_closeudp; }
484 close(ads->udpsocket);
490 static void init_abort(adns_state ads) {
491 if (ads->nsearchlist) {
492 free(ads->searchlist[0]);
493 free(ads->searchlist);
498 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
500 const char *res_options, *adns_res_options;
503 r= init_begin(&ads, flags, diagfile ? diagfile : stderr);
506 res_options= instrum_getenv(ads,"RES_OPTIONS");
507 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
508 ccf_options(ads,"RES_OPTIONS",-1,res_options);
509 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
511 readconfig(ads,"/etc/resolv.conf");
512 readconfigenv(ads,"RES_CONF");
513 readconfigenv(ads,"ADNS_RES_CONF");
515 readconfigenvtext(ads,"RES_CONF_TEXT");
516 readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
518 ccf_options(ads,"RES_OPTIONS",-1,res_options);
519 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
521 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
522 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
524 if (ads->configerrno && ads->configerrno != EINVAL) {
537 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
538 FILE *diagfile, const char *configtext) {
542 r= init_begin(&ads, flags, diagfile); if (r) return r;
544 readconfigtext(ads,configtext,"<supplied configuration text>");
545 if (ads->configerrno) {
551 r= init_finish(ads); if (r) return r;
556 void adns_finish(adns_state ads) {
558 if (ads->timew.head) adns_cancel(ads->timew.head);
559 else if (ads->childw.head) adns_cancel(ads->childw.head);
560 else if (ads->output.head) adns_cancel(ads->output.head);
563 close(ads->udpsocket);
564 if (ads->tcpsocket >= 0) close(ads->tcpsocket);
565 adns__vbuf_free(&ads->tcpsend);
566 adns__vbuf_free(&ads->tcprecv);
570 void adns_forallqueries_begin(adns_state ads) {
572 ads->timew.head ? ads->timew.head :
573 ads->childw.head ? ads->childw.head :
577 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
580 nqu= ads->forallnext;
585 qu->next ? qu->next :
586 qu == ads->timew.tail ? (ads->childw.head ? ads->childw.head : ads->output.head) :
587 qu == ads->childw.tail ? ads->output.head :
589 if (!qu->parent) break;
591 ads->forallnext= nqu;
592 if (context_r) *context_r= qu->ctx.ext;