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 const char *maskwhat;
156 struct in_addr base, mask;
158 unsigned long initial, baselocal;
163 while (nextword(&buf,&word,&l)) {
164 if (ads->nsortlist >= MAXSORTLIST) {
165 adns__diag(ads,-1,0,"too many sortlist entries,"
166 " ignoring %.*s onwards",l,word);
170 if (l >= sizeof(tbuf)) {
171 configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
175 memcpy(tbuf,word,l); tbuf[l]= 0;
176 slash= strchr(tbuf,'/');
177 if (slash) *slash++= 0;
179 if (!inet_aton(tbuf,&base)) {
180 configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
185 if (strchr(slash,'.')) {
187 if (!inet_aton(slash,&mask)) {
188 configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
192 maskwhat = "prefix length";
193 initial= strtoul(slash,&ep,10);
194 if (*ep || initial>32) {
195 configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
198 mask.s_addr= htonl((0x0ffffffffUL) << (32-initial));
201 maskwhat = "implied mask";
202 baselocal= ntohl(base.s_addr);
203 if (!(baselocal & 0x080000000UL)) /* class A */
204 mask.s_addr= htonl(0x0ff000000UL);
205 else if ((baselocal & 0x0c0000000UL) == 0x080000000UL)
206 mask.s_addr= htonl(0x0ffff0000UL); /* class B */
207 else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL)
208 mask.s_addr= htonl(0x0ff000000UL); /* class C */
210 configparseerr(ads,fn,lno, "network address `%s'"
211 " in sortlist is not in classed ranges,"
212 " must specify mask explicitly", tbuf);
217 if (base.s_addr & ~mask.s_addr) {
218 configparseerr(ads,fn,lno, "%s `%s' in sortlist"
219 " overlaps address `%s'",maskwhat,
220 slash ? slash : inet_ntoa(mask), tbuf);
224 ads->sortlist[ads->nsortlist].base= base;
225 ads->sortlist[ads->nsortlist].mask= mask;
230 static void ccf_options(adns_state ads, const char *fn,
231 int lno, const char *buf) {
239 while (nextword(&buf,&word,&l)) {
240 if (l==5 && !memcmp(word,"debug",5)) {
241 ads->iflags |= adns_if_debug;
244 if (l>=6 && !memcmp(word,"ndots:",6)) {
245 v= strtoul(word+6,&ep,10);
246 if (l==6 || ep != word+l || v > INT_MAX) {
247 configparseerr(ads,fn,lno,"option `%.*s' malformed"
248 " or has bad value",l,word);
254 if (l>=12 && !memcmp(word,"adns_checkc:",12)) {
255 if (!strcmp(word+12,"none")) {
256 ads->iflags &= ~adns_if_checkc_freq;
257 ads->iflags |= adns_if_checkc_entex;
258 } else if (!strcmp(word+12,"entex")) {
259 ads->iflags &= ~adns_if_checkc_freq;
260 ads->iflags |= adns_if_checkc_entex;
261 } else if (!strcmp(word+12,"freq")) {
262 ads->iflags |= adns_if_checkc_freq;
264 configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' "
265 "(must be none, entex or freq", word+12);
269 adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
273 static void ccf_clearnss(adns_state ads, const char *fn,
274 int lno, const char *buf) {
278 static void ccf_include(adns_state ads, const char *fn,
279 int lno, const char *buf) {
281 configparseerr(ads,fn,lno,"`include' directive with no filename");
284 readconfig(ads,buf,1);
287 static void ccf_lookup(adns_state ads, const char *fn, int lno,
294 configparseerr(ads,fn,lno,"`lookup' directive with no databases");
298 while (nextword(&buf,&word,&l)) {
299 if (l==4 && !memcmp(word,"bind",4)) {
301 } else if (l==4 && !memcmp(word,"file",4)) {
302 /* ignore this and hope /etc/hosts is not essential */
303 } else if (l==2 && !memcmp(word,"yp",2)) {
304 adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno);
307 adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
313 adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno);
316 static const struct configcommandinfo {
318 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
319 } configcommandinfos[]= {
320 { "nameserver", ccf_nameserver },
321 { "domain", ccf_search },
322 { "search", ccf_search },
323 { "sortlist", ccf_sortlist },
324 { "options", ccf_options },
325 { "clearnameservers", ccf_clearnss },
326 { "include", ccf_include },
327 { "lookup", ccf_lookup }, /* OpenBSD */
336 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
337 int lno, char *buf, int buflen) {
338 FILE *file= src_io->file;
346 for (;;) { /* loop over chars */
348 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
353 adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
355 } else if (c == '\n') {
357 } else if (c == EOF) {
360 adns__diag(ads,-1,0,"%s:%d: read error: %s",
361 filename,lno,strerror(errno));
377 while ((c= getc(file)) != EOF && c != '\n');
381 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
382 int lno, char *buf, int buflen) {
383 const char *cp= src_io->text;
386 if (!cp || !*cp) return -1;
388 if (*cp == ';' || *cp == '\n') cp++;
389 l= strcspn(cp,";\n");
393 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
403 static void readconfiggeneric(adns_state ads, const char *filename,
404 int (*getline)(adns_state ads, getline_ctx*,
405 const char *filename, int lno,
406 char *buf, int buflen),
407 /* Returns >=0 for success, -1 for EOF or error
408 * (error will have been reported), or -2 for
409 * bad line was encountered, try again.
411 getline_ctx gl_ctx) {
412 char linebuf[2000], *p, *q;
414 const struct configcommandinfo *ccip;
417 (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
419 if (l == -2) continue;
420 while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
423 while (ctype_whitespace(*p)) p++;
424 if (*p == '#' || *p == ';' || !*p) continue;
426 while (*q && !ctype_whitespace(*q)) q++;
428 for (ccip=configcommandinfos;
430 !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
433 adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
434 filename,lno,(int)(q-p),p);
437 while (ctype_whitespace(*q)) q++;
438 ccip->fn(ads,filename,lno,q);
442 static const char *instrum_getenv(adns_state ads, const char *envvar) {
445 value= getenv(envvar);
446 if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
447 else adns__debug(ads,-1,0,"environment variable %s"
448 " set to `%s'",envvar,value);
452 static void readconfig(adns_state ads, const char *filename, int warnmissing) {
455 gl_ctx.file= fopen(filename,"r");
457 if (errno == ENOENT) {
459 adns__debug(ads,-1,0, "configuration file"
460 " `%s' does not exist",filename);
464 adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
465 filename,strerror(errno));
469 readconfiggeneric(ads,filename,gl_file,gl_ctx);
474 static void readconfigtext(adns_state ads, const char *text,
475 const char *showname) {
479 readconfiggeneric(ads,showname,gl_text,gl_ctx);
482 static void readconfigenv(adns_state ads, const char *envvar) {
483 const char *filename;
485 if (ads->iflags & adns_if_noenv) {
486 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
489 filename= instrum_getenv(ads,envvar);
490 if (filename) readconfig(ads,filename,1);
493 static void readconfigenvtext(adns_state ads, const char *envvar) {
494 const char *textdata;
496 if (ads->iflags & adns_if_noenv) {
497 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
500 textdata= instrum_getenv(ads,envvar);
501 if (textdata) readconfigtext(ads,textdata,envvar);
505 int adns__setnonblock(adns_state ads, int fd) {
508 r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
510 r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
514 static int init_begin(adns_state *ads_r, adns_initflags flags,
515 adns_logcallbackfn *logfn, void *logfndata) {
519 ads= malloc(sizeof(*ads)); if (!ads) return errno;
523 ads->logfndata= logfndata;
525 LIST_INIT(ads->udpw);
526 LIST_INIT(ads->tcpw);
527 LIST_INIT(ads->childw);
528 LIST_INIT(ads->output);
531 ads->udpsocket= ads->tcpsocket= -1;
532 adns__vbuf_init(&ads->tcpsend);
533 adns__vbuf_init(&ads->tcprecv);
534 ads->tcprecv_skip= 0;
535 ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
537 ads->tcpstate= server_disconnected;
538 timerclear(&ads->tcptimeout);
542 ads->rand48xsubi[0]= pid;
543 ads->rand48xsubi[1]= (unsigned long)pid >> 16;
544 ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16);
550 static int init_finish(adns_state ads) {
552 struct protoent *proto;
555 if (!ads->nservers) {
556 if (ads->logfn && ads->iflags & adns_if_debug)
557 adns__lprintf(ads,"adns: no nameservers, using localhost\n");
558 ia.s_addr= htonl(INADDR_LOOPBACK);
562 proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
563 ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
564 if (ads->udpsocket<0) { r= errno; goto x_free; }
566 r= adns__setnonblock(ads,ads->udpsocket);
567 if (r) { r= errno; goto x_closeudp; }
572 close(ads->udpsocket);
578 static void init_abort(adns_state ads) {
579 if (ads->nsearchlist) {
580 free(ads->searchlist[0]);
581 free(ads->searchlist);
586 static void logfn_file(adns_state ads, void *logfndata,
587 const char *fmt, va_list al) {
588 vfprintf(logfndata,fmt,al);
591 static int init_files(adns_state *ads_r, adns_initflags flags,
592 adns_logcallbackfn *logfn, void *logfndata) {
594 const char *res_options, *adns_res_options;
597 r= init_begin(&ads, flags, logfn, logfndata);
600 res_options= instrum_getenv(ads,"RES_OPTIONS");
601 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
602 ccf_options(ads,"RES_OPTIONS",-1,res_options);
603 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
605 readconfig(ads,"/etc/resolv.conf",1);
606 readconfig(ads,"/etc/resolv-adns.conf",0);
607 readconfigenv(ads,"RES_CONF");
608 readconfigenv(ads,"ADNS_RES_CONF");
610 readconfigenvtext(ads,"RES_CONF_TEXT");
611 readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
613 ccf_options(ads,"RES_OPTIONS",-1,res_options);
614 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
616 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
617 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
619 if (ads->configerrno && ads->configerrno != EINVAL) {
628 adns__consistency(ads,0,cc_entex);
633 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
634 return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr);
637 static int init_strcfg(adns_state *ads_r, adns_initflags flags,
638 adns_logcallbackfn *logfn, void *logfndata,
639 const char *configtext) {
643 r= init_begin(&ads, flags, logfn, logfndata);
646 readconfigtext(ads,configtext,"<supplied configuration text>");
647 if (ads->configerrno) {
653 r= init_finish(ads); if (r) return r;
654 adns__consistency(ads,0,cc_entex);
659 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
660 FILE *diagfile, const char *configtext) {
661 return init_strcfg(ads_r, flags,
662 diagfile ? logfn_file : 0, diagfile,
666 int adns_init_logfn(adns_state *newstate_r, adns_initflags flags,
667 const char *configtext /*0=>use default config files*/,
668 adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */,
669 void *logfndata /*0 with logfn==0 => discard*/) {
670 if (!logfn && logfndata)
673 return init_strcfg(newstate_r, flags, logfn, logfndata, configtext);
675 return init_files(newstate_r, flags, logfn, logfndata);
678 void adns_finish(adns_state ads) {
679 adns__consistency(ads,0,cc_entex);
681 if (ads->udpw.head) adns_cancel(ads->udpw.head);
682 else if (ads->tcpw.head) adns_cancel(ads->tcpw.head);
683 else if (ads->childw.head) adns_cancel(ads->childw.head);
684 else if (ads->output.head) adns_cancel(ads->output.head);
687 close(ads->udpsocket);
688 if (ads->tcpsocket >= 0) close(ads->tcpsocket);
689 adns__vbuf_free(&ads->tcpsend);
690 adns__vbuf_free(&ads->tcprecv);
695 void adns_forallqueries_begin(adns_state ads) {
696 adns__consistency(ads,0,cc_entex);
698 ads->udpw.head ? ads->udpw.head :
699 ads->tcpw.head ? ads->tcpw.head :
700 ads->childw.head ? ads->childw.head :
704 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
707 adns__consistency(ads,0,cc_entex);
708 nqu= ads->forallnext;
714 } else if (qu == ads->udpw.tail) {
716 ads->tcpw.head ? ads->tcpw.head :
717 ads->childw.head ? ads->childw.head :
719 } else if (qu == ads->tcpw.tail) {
721 ads->childw.head ? ads->childw.head :
723 } else if (qu == ads->childw.tail) {
724 nqu= ads->output.head;
728 if (!qu->parent) break;
730 ads->forallnext= nqu;
731 if (context_r) *context_r= qu->ctx.ext;