3 * - configuration file parsing
4 * - management of global state
7 * This file is part of adns, which is
8 * Copyright (C) 1997-2000,2003,2006 Ian Jackson
9 * Copyright (C) 1999-2000,2003,2006 Tony Finch
10 * Copyright (C) 1991 Massachusetts Institute of Technology
11 * (See the file INSTALL for full details.)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
42 static void readconfig(adns_state ads, const char *filename, int warnmissing);
44 static void addserver(adns_state ads, struct in_addr addr) {
48 for (i=0; i<ads->nservers; i++) {
49 if (ads->servers[i].addr.s_addr == addr.s_addr) {
50 adns__debug(ads,-1,0,"duplicate nameserver %s ignored",inet_ntoa(addr));
55 if (ads->nservers>=MAXSERVERS) {
56 adns__diag(ads,-1,0,"too many nameservers, ignoring %s",inet_ntoa(addr));
60 ss= ads->servers+ads->nservers;
65 static void freesearchlist(adns_state ads) {
66 if (ads->nsearchlist) free(*ads->searchlist);
67 free(ads->searchlist);
70 static void saveerr(adns_state ads, int en) {
71 if (!ads->configerrno) ads->configerrno= en;
74 static void configparseerr(adns_state ads, const char *fn, int lno,
75 const char *fmt, ...) {
79 if (!ads->logfn || (ads->iflags & adns_if_noerrprint)) return;
81 if (lno==-1) adns__lprintf(ads,"adns: %s: ",fn);
82 else adns__lprintf(ads,"adns: %s:%d: ",fn,lno);
84 adns__vlprintf(ads,fmt,al);
86 adns__lprintf(ads,"\n");
89 static int nextword(const char **bufp_io, const char **word_r, int *l_r) {
93 while (ctype_whitespace(*p)) p++;
97 while (*q && !ctype_whitespace(*q)) q++;
106 static void ccf_nameserver(adns_state ads, const char *fn,
107 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,
119 int lno, const char *buf) {
120 const char *bufp, *word;
121 char *newchars, **newptrs, **pp;
129 while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
131 newptrs= malloc(sizeof(char*)*count);
132 if (!newptrs) { saveerr(ads,errno); return; }
134 newchars= malloc(tl);
135 if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
139 while (nextword(&bufp,&word,&l)) {
141 memcpy(newchars,word,l);
147 ads->nsearchlist= count;
148 ads->searchlist= newptrs;
151 static void ccf_sortlist(adns_state ads, const char *fn,
152 int lno, const char *buf) {
154 char tbuf[200], *slash, *ep;
155 struct in_addr base, mask;
157 unsigned long initial, baselocal;
162 while (nextword(&buf,&word,&l)) {
163 if (ads->nsortlist >= MAXSORTLIST) {
164 adns__diag(ads,-1,0,"too many sortlist entries,"
165 " ignoring %.*s onwards",l,word);
169 if (l >= sizeof(tbuf)) {
170 configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
174 memcpy(tbuf,word,l); tbuf[l]= 0;
175 slash= strchr(tbuf,'/');
176 if (slash) *slash++= 0;
178 if (!inet_aton(tbuf,&base)) {
179 configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
184 if (strchr(slash,'.')) {
185 if (!inet_aton(slash,&mask)) {
186 configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
189 if (base.s_addr & ~mask.s_addr) {
190 configparseerr(ads,fn,lno, "mask `%s' in sortlist"
191 " overlaps address `%s'",slash,tbuf);
195 initial= strtoul(slash,&ep,10);
196 if (*ep || initial>32) {
197 configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
200 mask.s_addr= htonl((0x0ffffffffUL) << (32-initial));
203 baselocal= ntohl(base.s_addr);
204 if (!baselocal & 0x080000000UL) /* class A */
205 mask.s_addr= htonl(0x0ff000000UL);
206 else if ((baselocal & 0x0c0000000UL) == 0x080000000UL)
207 mask.s_addr= htonl(0x0ffff0000UL); /* class B */
208 else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL)
209 mask.s_addr= htonl(0x0ff000000UL); /* class C */
211 configparseerr(ads,fn,lno, "network address `%s'"
212 " in sortlist is not in classed ranges,"
213 " must specify mask explicitly", tbuf);
218 ads->sortlist[ads->nsortlist].base= base;
219 ads->sortlist[ads->nsortlist].mask= mask;
224 static void ccf_options(adns_state ads, const char *fn,
225 int lno, const char *buf) {
233 while (nextword(&buf,&word,&l)) {
234 if (l==5 && !memcmp(word,"debug",5)) {
235 ads->iflags |= adns_if_debug;
238 if (l>=6 && !memcmp(word,"ndots:",6)) {
239 v= strtoul(word+6,&ep,10);
240 if (l==6 || ep != word+l || v > INT_MAX) {
241 configparseerr(ads,fn,lno,"option `%.*s' malformed"
242 " or has bad value",l,word);
248 if (l>=12 && !memcmp(word,"adns_checkc:",12)) {
249 if (!strcmp(word+12,"none")) {
250 ads->iflags &= ~adns_if_checkc_freq;
251 ads->iflags |= adns_if_checkc_entex;
252 } else if (!strcmp(word+12,"entex")) {
253 ads->iflags &= ~adns_if_checkc_freq;
254 ads->iflags |= adns_if_checkc_entex;
255 } else if (!strcmp(word+12,"freq")) {
256 ads->iflags |= adns_if_checkc_freq;
258 configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' "
259 "(must be none, entex or freq", word+12);
263 adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
267 static void ccf_clearnss(adns_state ads, const char *fn,
268 int lno, const char *buf) {
272 static void ccf_include(adns_state ads, const char *fn,
273 int lno, const char *buf) {
275 configparseerr(ads,fn,lno,"`include' directive with no filename");
278 readconfig(ads,buf,1);
281 static void ccf_lookup(adns_state ads, const char *fn, int lno,
288 configparseerr(ads,fn,lno,"`lookup' directive with no databases");
292 while (nextword(&buf,&word,&l)) {
293 if (l==4 && !memcmp(word,"bind",4)) {
295 } else if (l==4 && !memcmp(word,"file",4)) {
296 /* ignore this and hope /etc/hosts is not essential */
297 } else if (l==2 && !memcmp(word,"yp",2)) {
298 adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno);
301 adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
307 adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno);
310 static const struct configcommandinfo {
312 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
313 } configcommandinfos[]= {
314 { "nameserver", ccf_nameserver },
315 { "domain", ccf_search },
316 { "search", ccf_search },
317 { "sortlist", ccf_sortlist },
318 { "options", ccf_options },
319 { "clearnameservers", ccf_clearnss },
320 { "include", ccf_include },
321 { "lookup", ccf_lookup }, /* OpenBSD */
330 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
331 int lno, char *buf, int buflen) {
332 FILE *file= src_io->file;
340 for (;;) { /* loop over chars */
342 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
347 adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
349 } else if (c == '\n') {
351 } else if (c == EOF) {
354 adns__diag(ads,-1,0,"%s:%d: read error: %s",
355 filename,lno,strerror(errno));
371 while ((c= getc(file)) != EOF && c != '\n');
375 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
376 int lno, char *buf, int buflen) {
377 const char *cp= src_io->text;
380 if (!cp || !*cp) return -1;
382 if (*cp == ';' || *cp == '\n') cp++;
383 l= strcspn(cp,";\n");
387 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
397 static void readconfiggeneric(adns_state ads, const char *filename,
398 int (*getline)(adns_state ads, getline_ctx*,
399 const char *filename, int lno,
400 char *buf, int buflen),
401 /* Returns >=0 for success, -1 for EOF or error
402 * (error will have been reported), or -2 for
403 * bad line was encountered, try again.
405 getline_ctx gl_ctx) {
406 char linebuf[2000], *p, *q;
408 const struct configcommandinfo *ccip;
411 (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
413 if (l == -2) continue;
414 while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
417 while (ctype_whitespace(*p)) p++;
418 if (*p == '#' || *p == ';' || !*p) continue;
420 while (*q && !ctype_whitespace(*q)) q++;
422 for (ccip=configcommandinfos;
424 !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
427 adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
428 filename,lno,(int)(q-p),p);
431 while (ctype_whitespace(*q)) q++;
432 ccip->fn(ads,filename,lno,q);
436 static const char *instrum_getenv(adns_state ads, const char *envvar) {
439 value= getenv(envvar);
440 if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
441 else adns__debug(ads,-1,0,"environment variable %s"
442 " set to `%s'",envvar,value);
446 static void readconfig(adns_state ads, const char *filename, int warnmissing) {
449 gl_ctx.file= fopen(filename,"r");
451 if (errno == ENOENT) {
453 adns__debug(ads,-1,0, "configuration file"
454 " `%s' does not exist",filename);
458 adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
459 filename,strerror(errno));
463 readconfiggeneric(ads,filename,gl_file,gl_ctx);
468 static void readconfigtext(adns_state ads, const char *text,
469 const char *showname) {
473 readconfiggeneric(ads,showname,gl_text,gl_ctx);
476 static void readconfigenv(adns_state ads, const char *envvar) {
477 const char *filename;
479 if (ads->iflags & adns_if_noenv) {
480 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
483 filename= instrum_getenv(ads,envvar);
484 if (filename) readconfig(ads,filename,1);
487 static void readconfigenvtext(adns_state ads, const char *envvar) {
488 const char *textdata;
490 if (ads->iflags & adns_if_noenv) {
491 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
494 textdata= instrum_getenv(ads,envvar);
495 if (textdata) readconfigtext(ads,textdata,envvar);
499 int adns__setnonblock(adns_state ads, int fd) {
502 r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
504 r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
508 static int init_begin(adns_state *ads_r, adns_initflags flags,
509 adns_logcallbackfn *logfn, void *logfndata) {
513 ads= malloc(sizeof(*ads)); if (!ads) return errno;
517 ads->logfndata= logfndata;
519 LIST_INIT(ads->udpw);
520 LIST_INIT(ads->tcpw);
521 LIST_INIT(ads->childw);
522 LIST_INIT(ads->output);
525 ads->udpsocket= ads->tcpsocket= -1;
526 adns__vbuf_init(&ads->tcpsend);
527 adns__vbuf_init(&ads->tcprecv);
528 ads->tcprecv_skip= 0;
529 ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
531 ads->tcpstate= server_disconnected;
532 timerclear(&ads->tcptimeout);
536 ads->rand48xsubi[0]= pid;
537 ads->rand48xsubi[1]= (unsigned long)pid >> 16;
538 ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16);
544 static int init_finish(adns_state ads) {
546 struct protoent *proto;
549 if (!ads->nservers) {
550 if (ads->logfn && ads->iflags & adns_if_debug)
551 adns__lprintf(ads,"adns: no nameservers, using localhost\n");
552 ia.s_addr= htonl(INADDR_LOOPBACK);
556 proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
557 ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
558 if (ads->udpsocket<0) { r= errno; goto x_free; }
560 r= adns__setnonblock(ads,ads->udpsocket);
561 if (r) { r= errno; goto x_closeudp; }
566 close(ads->udpsocket);
572 static void init_abort(adns_state ads) {
573 if (ads->nsearchlist) {
574 free(ads->searchlist[0]);
575 free(ads->searchlist);
580 static void logfn_file(adns_state ads, void *logfndata,
581 const char *fmt, va_list al) {
582 vfprintf(logfndata,fmt,al);
585 static int init_files(adns_state *ads_r, adns_initflags flags,
586 adns_logcallbackfn *logfn, void *logfndata) {
588 const char *res_options, *adns_res_options;
591 r= init_begin(&ads, flags, logfn, logfndata);
594 res_options= instrum_getenv(ads,"RES_OPTIONS");
595 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
596 ccf_options(ads,"RES_OPTIONS",-1,res_options);
597 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
599 readconfig(ads,"/etc/resolv.conf",1);
600 readconfig(ads,"/etc/resolv-adns.conf",0);
601 readconfigenv(ads,"RES_CONF");
602 readconfigenv(ads,"ADNS_RES_CONF");
604 readconfigenvtext(ads,"RES_CONF_TEXT");
605 readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
607 ccf_options(ads,"RES_OPTIONS",-1,res_options);
608 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
610 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
611 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
613 if (ads->configerrno && ads->configerrno != EINVAL) {
622 adns__consistency(ads,0,cc_entex);
627 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
628 return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr);
631 static int init_strcfg(adns_state *ads_r, adns_initflags flags,
632 adns_logcallbackfn *logfn, void *logfndata,
633 const char *configtext) {
637 r= init_begin(&ads, flags, logfn, logfndata);
640 readconfigtext(ads,configtext,"<supplied configuration text>");
641 if (ads->configerrno) {
647 r= init_finish(ads); if (r) return r;
648 adns__consistency(ads,0,cc_entex);
653 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
654 FILE *diagfile, const char *configtext) {
655 return init_strcfg(ads_r, flags,
656 diagfile ? logfn_file : 0, diagfile,
660 int adns_init_logfn(adns_state *newstate_r, adns_initflags flags,
661 const char *configtext /*0=>use default config files*/,
662 adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */,
663 void *logfndata /*0 with logfn==0 => discard*/) {
664 if (!logfn && logfndata)
667 return init_strcfg(newstate_r, flags, logfn, logfndata, configtext);
669 return init_files(newstate_r, flags, logfn, logfndata);
672 void adns_finish(adns_state ads) {
673 adns__consistency(ads,0,cc_entex);
675 if (ads->udpw.head) adns_cancel(ads->udpw.head);
676 else if (ads->tcpw.head) adns_cancel(ads->tcpw.head);
677 else if (ads->childw.head) adns_cancel(ads->childw.head);
678 else if (ads->output.head) adns_cancel(ads->output.head);
681 close(ads->udpsocket);
682 if (ads->tcpsocket >= 0) close(ads->tcpsocket);
683 adns__vbuf_free(&ads->tcpsend);
684 adns__vbuf_free(&ads->tcprecv);
689 void adns_forallqueries_begin(adns_state ads) {
690 adns__consistency(ads,0,cc_entex);
692 ads->udpw.head ? ads->udpw.head :
693 ads->tcpw.head ? ads->tcpw.head :
694 ads->childw.head ? ads->childw.head :
698 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
701 adns__consistency(ads,0,cc_entex);
702 nqu= ads->forallnext;
708 } else if (qu == ads->udpw.tail) {
710 ads->tcpw.head ? ads->tcpw.head :
711 ads->childw.head ? ads->childw.head :
713 } else if (qu == ads->tcpw.tail) {
715 ads->childw.head ? ads->childw.head :
717 } else if (qu == ads->childw.tail) {
718 nqu= ads->output.head;
722 if (!qu->parent) break;
724 ads->forallnext= nqu;
725 if (context_r) *context_r= qu->ctx.ext;