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) {
153 /* FIXME: Handle IPv6 addresses */
155 char tbuf[200], *slash, *ep;
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,'.')) {
186 if (!inet_aton(slash,&mask)) {
187 configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
190 if (base.s_addr & ~mask.s_addr) {
191 configparseerr(ads,fn,lno, "mask `%s' in sortlist"
192 " overlaps address `%s'",slash,tbuf);
196 /* Convert bitmask to prefix length */
199 for(bits=ntohl(mask.s_addr), initial = 0;
204 if (bits & 0xffffffff) {
205 configparseerr(ads,fn,lno,
206 "mask `%s' in sortlist is non-continuous",slash);
211 initial= strtoul(slash,&ep,10);
212 if (*ep || initial>32) {
213 configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
216 mask.s_addr= htonl((0x0ffffffffUL) << (32-initial));
219 baselocal= ntohl(base.s_addr);
220 if (!baselocal & 0x080000000UL) /* class A */
222 else if ((baselocal & 0x0c0000000UL) == 0x080000000UL)
223 initial= 16; /* class B */
224 else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL)
225 initial= 24; /* class C */
227 configparseerr(ads,fn,lno, "network address `%s'"
228 " in sortlist is not in classed ranges,"
229 " must specify mask explicitly", tbuf);
234 ads->sortlist[ads->nsortlist].family= AF_INET;
235 ads->sortlist[ads->nsortlist].base.inet= base;
236 ads->sortlist[ads->nsortlist].prefix= initial;
242 static void ccf_options(adns_state ads, const char *fn,
243 int lno, const char *buf) {
251 while (nextword(&buf,&word,&l)) {
252 if (l==5 && !memcmp(word,"debug",5)) {
253 ads->iflags |= adns_if_debug;
256 if (l>=6 && !memcmp(word,"ndots:",6)) {
257 v= strtoul(word+6,&ep,10);
258 if (l==6 || ep != word+l || v > INT_MAX) {
259 configparseerr(ads,fn,lno,"option `%.*s' malformed"
260 " or has bad value",l,word);
266 if (l>=12 && !memcmp(word,"adns_checkc:",12)) {
267 if (!strcmp(word+12,"none")) {
268 ads->iflags &= ~adns_if_checkc_freq;
269 ads->iflags |= adns_if_checkc_entex;
270 } else if (!strcmp(word+12,"entex")) {
271 ads->iflags &= ~adns_if_checkc_freq;
272 ads->iflags |= adns_if_checkc_entex;
273 } else if (!strcmp(word+12,"freq")) {
274 ads->iflags |= adns_if_checkc_freq;
276 configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' "
277 "(must be none, entex or freq", word+12);
281 adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
285 static void ccf_clearnss(adns_state ads, const char *fn,
286 int lno, const char *buf) {
290 static void ccf_include(adns_state ads, const char *fn,
291 int lno, const char *buf) {
293 configparseerr(ads,fn,lno,"`include' directive with no filename");
296 readconfig(ads,buf,1);
299 static void ccf_lookup(adns_state ads, const char *fn, int lno,
306 configparseerr(ads,fn,lno,"`lookup' directive with no databases");
310 while (nextword(&buf,&word,&l)) {
311 if (l==4 && !memcmp(word,"bind",4)) {
313 } else if (l==4 && !memcmp(word,"file",4)) {
314 /* ignore this and hope /etc/hosts is not essential */
315 } else if (l==2 && !memcmp(word,"yp",2)) {
316 adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno);
319 adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
325 adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno);
328 static const struct configcommandinfo {
330 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
331 } configcommandinfos[]= {
332 { "nameserver", ccf_nameserver },
333 { "domain", ccf_search },
334 { "search", ccf_search },
335 { "sortlist", ccf_sortlist },
336 { "options", ccf_options },
337 { "clearnameservers", ccf_clearnss },
338 { "include", ccf_include },
339 { "lookup", ccf_lookup }, /* OpenBSD */
348 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
349 int lno, char *buf, int buflen) {
350 FILE *file= src_io->file;
358 for (;;) { /* loop over chars */
360 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
365 adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
367 } else if (c == '\n') {
369 } else if (c == EOF) {
372 adns__diag(ads,-1,0,"%s:%d: read error: %s",
373 filename,lno,strerror(errno));
389 while ((c= getc(file)) != EOF && c != '\n');
393 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
394 int lno, char *buf, int buflen) {
395 const char *cp= src_io->text;
398 if (!cp || !*cp) return -1;
400 if (*cp == ';' || *cp == '\n') cp++;
401 l= strcspn(cp,";\n");
405 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
415 static void readconfiggeneric(adns_state ads, const char *filename,
416 int (*getline)(adns_state ads, getline_ctx*,
417 const char *filename, int lno,
418 char *buf, int buflen),
419 /* Returns >=0 for success, -1 for EOF or error
420 * (error will have been reported), or -2 for
421 * bad line was encountered, try again.
423 getline_ctx gl_ctx) {
424 char linebuf[2000], *p, *q;
426 const struct configcommandinfo *ccip;
429 (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
431 if (l == -2) continue;
432 while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
435 while (ctype_whitespace(*p)) p++;
436 if (*p == '#' || *p == ';' || !*p) continue;
438 while (*q && !ctype_whitespace(*q)) q++;
440 for (ccip=configcommandinfos;
442 !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
445 adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
446 filename,lno,(int)(q-p),p);
449 while (ctype_whitespace(*q)) q++;
450 ccip->fn(ads,filename,lno,q);
454 static const char *instrum_getenv(adns_state ads, const char *envvar) {
457 value= getenv(envvar);
458 if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
459 else adns__debug(ads,-1,0,"environment variable %s"
460 " set to `%s'",envvar,value);
464 static void readconfig(adns_state ads, const char *filename, int warnmissing) {
467 gl_ctx.file= fopen(filename,"re");
469 if (errno == ENOENT) {
471 adns__debug(ads,-1,0, "configuration file"
472 " `%s' does not exist",filename);
476 adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
477 filename,strerror(errno));
481 readconfiggeneric(ads,filename,gl_file,gl_ctx);
486 static void readconfigtext(adns_state ads, const char *text,
487 const char *showname) {
491 readconfiggeneric(ads,showname,gl_text,gl_ctx);
494 static void readconfigenv(adns_state ads, const char *envvar) {
495 const char *filename;
497 if (ads->iflags & adns_if_noenv) {
498 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
501 filename= instrum_getenv(ads,envvar);
502 if (filename) readconfig(ads,filename,1);
505 static void readconfigenvtext(adns_state ads, const char *envvar) {
506 const char *textdata;
508 if (ads->iflags & adns_if_noenv) {
509 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
512 textdata= instrum_getenv(ads,envvar);
513 if (textdata) readconfigtext(ads,textdata,envvar);
517 int adns__setnonblock(adns_state ads, int fd) {
520 r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
522 r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
526 static int init_begin(adns_state *ads_r, adns_initflags flags,
527 adns_logcallbackfn *logfn, void *logfndata) {
531 ads= malloc(sizeof(*ads)); if (!ads) return errno;
535 ads->logfndata= logfndata;
537 LIST_INIT(ads->udpw);
538 LIST_INIT(ads->tcpw);
539 LIST_INIT(ads->childw);
540 LIST_INIT(ads->output);
543 ads->udpsocket= ads->tcpsocket= -1;
544 adns__vbuf_init(&ads->tcpsend);
545 adns__vbuf_init(&ads->tcprecv);
546 ads->tcprecv_skip= 0;
547 ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
549 ads->tcpstate= server_disconnected;
550 timerclear(&ads->tcptimeout);
554 ads->rand48xsubi[0]= pid;
555 ads->rand48xsubi[1]= (unsigned long)pid >> 16;
556 ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16);
562 static int init_finish(adns_state ads) {
564 struct protoent *proto;
567 if (!ads->nservers) {
568 if (ads->logfn && ads->iflags & adns_if_debug)
569 adns__lprintf(ads,"adns: no nameservers, using localhost\n");
570 ia.s_addr= htonl(INADDR_LOOPBACK);
574 proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
575 ads->udpsocket= socket(AF_INET,SOCK_DGRAM|SOCK_CLOEXEC,proto->p_proto);
576 if (ads->udpsocket<0) { r= errno; goto x_free; }
578 r= adns__setnonblock(ads,ads->udpsocket);
579 if (r) { r= errno; goto x_closeudp; }
584 close(ads->udpsocket);
590 static void init_abort(adns_state ads) {
591 if (ads->nsearchlist) {
592 free(ads->searchlist[0]);
593 free(ads->searchlist);
598 static void logfn_file(adns_state ads, void *logfndata,
599 const char *fmt, va_list al) {
600 vfprintf(logfndata,fmt,al);
603 static int init_files(adns_state *ads_r, adns_initflags flags,
604 adns_logcallbackfn *logfn, void *logfndata) {
606 const char *res_options, *adns_res_options;
609 r= init_begin(&ads, flags, logfn, logfndata);
612 res_options= instrum_getenv(ads,"RES_OPTIONS");
613 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
614 ccf_options(ads,"RES_OPTIONS",-1,res_options);
615 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
617 readconfig(ads,"/etc/resolv.conf",1);
618 readconfig(ads,"/etc/resolv-adns.conf",0);
619 readconfigenv(ads,"RES_CONF");
620 readconfigenv(ads,"ADNS_RES_CONF");
622 readconfigenvtext(ads,"RES_CONF_TEXT");
623 readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
625 ccf_options(ads,"RES_OPTIONS",-1,res_options);
626 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
628 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
629 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
631 if (ads->configerrno && ads->configerrno != EINVAL) {
640 adns__consistency(ads,0,cc_entex);
645 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
646 return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr);
649 static int init_strcfg(adns_state *ads_r, adns_initflags flags,
650 adns_logcallbackfn *logfn, void *logfndata,
651 const char *configtext) {
655 r= init_begin(&ads, flags, logfn, logfndata);
658 readconfigtext(ads,configtext,"<supplied configuration text>");
659 if (ads->configerrno) {
665 r= init_finish(ads); if (r) return r;
666 adns__consistency(ads,0,cc_entex);
671 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
672 FILE *diagfile, const char *configtext) {
673 return init_strcfg(ads_r, flags,
674 diagfile ? logfn_file : 0, diagfile,
678 int adns_init_logfn(adns_state *newstate_r, adns_initflags flags,
679 const char *configtext /*0=>use default config files*/,
680 adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */,
681 void *logfndata /*0 with logfn==0 => discard*/) {
682 if (!logfn && logfndata)
685 return init_strcfg(newstate_r, flags, logfn, logfndata, configtext);
687 return init_files(newstate_r, flags, logfn, logfndata);
690 void adns_finish(adns_state ads) {
691 adns__consistency(ads,0,cc_entex);
693 if (ads->udpw.head) adns_cancel(ads->udpw.head);
694 else if (ads->tcpw.head) adns_cancel(ads->tcpw.head);
695 else if (ads->childw.head) adns_cancel(ads->childw.head);
696 else if (ads->output.head) adns_cancel(ads->output.head);
699 close(ads->udpsocket);
700 if (ads->tcpsocket >= 0) close(ads->tcpsocket);
701 adns__vbuf_free(&ads->tcpsend);
702 adns__vbuf_free(&ads->tcprecv);
707 void adns_forallqueries_begin(adns_state ads) {
708 adns__consistency(ads,0,cc_entex);
710 ads->udpw.head ? ads->udpw.head :
711 ads->tcpw.head ? ads->tcpw.head :
712 ads->childw.head ? ads->childw.head :
716 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
719 adns__consistency(ads,0,cc_entex);
720 nqu= ads->forallnext;
726 } else if (qu == ads->udpw.tail) {
728 ads->tcpw.head ? ads->tcpw.head :
729 ads->childw.head ? ads->childw.head :
731 } else if (qu == ads->tcpw.tail) {
733 ads->childw.head ? ads->childw.head :
735 } else if (qu == ads->childw.tail) {
736 nqu= ads->output.head;
740 if (!qu->parent) break;
742 ads->forallnext= nqu;
743 if (context_r) *context_r= qu->ctx.ext;