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, const struct sockaddr *sa, int n) {
47 char buf[ADNS_ADDR2TEXT_BUFLEN];
49 if (sa->sa_family != AF_INET) {
50 adns__debug(ads,-1,0,"non-IPv4 nameserver %s ignored",
51 adns__sockaddr_ntoa(sa, buf));
55 for (i=0; i<ads->nservers; i++) {
56 if (adns__sockaddr_equal_p(sa, &ads->servers[i].addr.sa)) {
57 adns__debug(ads,-1,0,"duplicate nameserver %s ignored",
58 adns__sockaddr_ntoa(sa, buf));
63 if (ads->nservers>=MAXSERVERS) {
64 adns__diag(ads,-1,0,"too many nameservers, ignoring %s",
65 adns__sockaddr_ntoa(sa, buf));
69 ss= ads->servers+ads->nservers;
70 assert(n <= sizeof(ss->addr));
72 memcpy(&ss->addr, sa, n);
76 static void freesearchlist(adns_state ads) {
77 if (ads->nsearchlist) free(*ads->searchlist);
78 free(ads->searchlist);
81 static void saveerr(adns_state ads, int en) {
82 if (!ads->configerrno) ads->configerrno= en;
85 static void configparseerr(adns_state ads, const char *fn, int lno,
86 const char *fmt, ...) {
90 if (!ads->logfn || (ads->iflags & adns_if_noerrprint)) return;
92 if (lno==-1) adns__lprintf(ads,"adns: %s: ",fn);
93 else adns__lprintf(ads,"adns: %s:%d: ",fn,lno);
95 adns__vlprintf(ads,fmt,al);
97 adns__lprintf(ads,"\n");
100 static int nextword(const char **bufp_io, const char **word_r, int *l_r) {
104 while (ctype_whitespace(*p)) p++;
108 while (*q && !ctype_whitespace(*q)) q++;
117 static void ccf_nameserver(adns_state ads, const char *fn,
118 int lno, const char *buf) {
120 char addrbuf[ADNS_ADDR2TEXT_BUFLEN];
123 a.len= sizeof(a.addr);
124 err= adns_text2addr(buf,DNS_PORT, 0, &a.addr.sa,&a.len);
129 configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
132 configparseerr(ads,fn,lno,"failed to parse nameserver address `%s': %s",
136 adns__debug(ads,-1,0,"using nameserver %s",
137 adns__sockaddr_ntoa(&a.addr.sa, addrbuf));
138 addserver(ads,&a.addr.sa,a.len);
141 static void ccf_search(adns_state ads, const char *fn,
142 int lno, const char *buf) {
143 const char *bufp, *word;
144 char *newchars, **newptrs, **pp;
152 while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
154 newptrs= malloc(sizeof(char*)*count);
155 if (!newptrs) { saveerr(ads,errno); return; }
157 newchars= malloc(tl);
158 if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
162 while (nextword(&bufp,&word,&l)) {
164 memcpy(newchars,word,l);
170 ads->nsearchlist= count;
171 ads->searchlist= newptrs;
174 static int gen_pton(const char *text, int *af_io, union gen_addr *a) {
178 addr.len= sizeof(addr.addr);
179 err= adns_text2addr(text,0, adns_qf_addrlit_scope_forbid,
180 &addr.addr.sa, &addr.len);
181 if (err) { assert(err == EINVAL); return 0; }
182 if (*af_io == AF_UNSPEC) *af_io= addr.addr.sa.sa_family;
183 else if (*af_io != addr.addr.sa.sa_family) return 0;
184 adns__sockaddr_extract(&addr.addr.sa, a, 0);
188 static void ccf_sortlist(adns_state ads, const char *fn,
189 int lno, const char *buf) {
191 char tbuf[200], *slash, *ep;
192 const char *maskwhat;
201 while (nextword(&buf,&word,&l)) {
202 if (ads->nsortlist >= MAXSORTLIST) {
203 adns__diag(ads,-1,0,"too many sortlist entries,"
204 " ignoring %.*s onwards",l,word);
208 if (l >= sizeof(tbuf)) {
209 configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
213 memcpy(tbuf,word,l); tbuf[l]= 0;
214 slash= strchr(tbuf,'/');
215 if (slash) *slash++= 0;
217 sl= &ads->sortlist[ads->nsortlist];
219 if (!gen_pton(tbuf, &af, &sl->base)) {
220 configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
225 if (slash[strspn(slash, "0123456789")]) {
227 if (!gen_pton(slash,&af,&sl->mask)) {
228 configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
232 maskwhat = "prefix length";
233 initial= strtoul(slash,&ep,10);
234 if (*ep || initial>adns__addr_width(af)) {
235 configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
238 adns__prefix_mask(af, initial, &sl->mask);
241 maskwhat = "implied prefix length";
242 initial= adns__guess_prefix_length(af, &sl->base);
244 configparseerr(ads,fn,lno, "network address `%s'"
245 " in sortlist is not in classed ranges,"
246 " must specify mask explicitly", tbuf);
249 adns__prefix_mask(af, initial, &sl->mask);
252 if (!adns__addr_match_p(af,&sl->base, af,&sl->base,&sl->mask)) {
254 configparseerr(ads,fn,lno, "%s %d in sortlist"
255 " overlaps address `%s'",maskwhat,initial,tbuf);
257 configparseerr(ads,fn,lno, "%s `%s' in sortlist"
258 " overlaps address `%s'",maskwhat,slash,tbuf);
268 static void ccf_options(adns_state ads, const char *fn,
269 int lno, const char *buf) {
277 while (nextword(&buf,&word,&l)) {
278 if (l==5 && !memcmp(word,"debug",5)) {
279 ads->iflags |= adns_if_debug;
282 if (l>=6 && !memcmp(word,"ndots:",6)) {
283 v= strtoul(word+6,&ep,10);
284 if (l==6 || ep != word+l || v > INT_MAX) {
285 configparseerr(ads,fn,lno,"option `%.*s' malformed"
286 " or has bad value",l,word);
292 if (l>=12 && !memcmp(word,"adns_checkc:",12)) {
293 if (!strcmp(word+12,"none")) {
294 ads->iflags &= ~adns_if_checkc_freq;
295 ads->iflags |= adns_if_checkc_entex;
296 } else if (!strcmp(word+12,"entex")) {
297 ads->iflags &= ~adns_if_checkc_freq;
298 ads->iflags |= adns_if_checkc_entex;
299 } else if (!strcmp(word+12,"freq")) {
300 ads->iflags |= adns_if_checkc_freq;
302 configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' "
303 "(must be none, entex or freq", word+12);
307 adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
311 static void ccf_clearnss(adns_state ads, const char *fn,
312 int lno, const char *buf) {
316 static void ccf_include(adns_state ads, const char *fn,
317 int lno, const char *buf) {
319 configparseerr(ads,fn,lno,"`include' directive with no filename");
322 readconfig(ads,buf,1);
325 static void ccf_lookup(adns_state ads, const char *fn, int lno,
332 configparseerr(ads,fn,lno,"`lookup' directive with no databases");
336 while (nextword(&buf,&word,&l)) {
337 if (l==4 && !memcmp(word,"bind",4)) {
339 } else if (l==4 && !memcmp(word,"file",4)) {
340 /* ignore this and hope /etc/hosts is not essential */
341 } else if (l==2 && !memcmp(word,"yp",2)) {
342 adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno);
345 adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
351 adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno);
354 static const struct configcommandinfo {
356 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
357 } configcommandinfos[]= {
358 { "nameserver", ccf_nameserver },
359 { "domain", ccf_search },
360 { "search", ccf_search },
361 { "sortlist", ccf_sortlist },
362 { "options", ccf_options },
363 { "clearnameservers", ccf_clearnss },
364 { "include", ccf_include },
365 { "lookup", ccf_lookup }, /* OpenBSD */
374 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
375 int lno, char *buf, int buflen) {
376 FILE *file= src_io->file;
384 for (;;) { /* loop over chars */
386 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
391 adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
393 } else if (c == '\n') {
395 } else if (c == EOF) {
398 adns__diag(ads,-1,0,"%s:%d: read error: %s",
399 filename,lno,strerror(errno));
415 while ((c= getc(file)) != EOF && c != '\n');
419 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
420 int lno, char *buf, int buflen) {
421 const char *cp= src_io->text;
424 if (!cp || !*cp) return -1;
426 if (*cp == ';' || *cp == '\n') cp++;
427 l= strcspn(cp,";\n");
431 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
441 static void readconfiggeneric(adns_state ads, const char *filename,
442 int (*getline)(adns_state ads, getline_ctx*,
443 const char *filename, int lno,
444 char *buf, int buflen),
445 /* Returns >=0 for success, -1 for EOF or error
446 * (error will have been reported), or -2 for
447 * bad line was encountered, try again.
449 getline_ctx gl_ctx) {
450 char linebuf[2000], *p, *q;
452 const struct configcommandinfo *ccip;
455 (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
457 if (l == -2) continue;
458 while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
461 while (ctype_whitespace(*p)) p++;
462 if (*p == '#' || *p == ';' || !*p) continue;
464 while (*q && !ctype_whitespace(*q)) q++;
466 for (ccip=configcommandinfos;
468 !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
471 adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
472 filename,lno,(int)(q-p),p);
475 while (ctype_whitespace(*q)) q++;
476 ccip->fn(ads,filename,lno,q);
480 static const char *instrum_getenv(adns_state ads, const char *envvar) {
483 value= getenv(envvar);
484 if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
485 else adns__debug(ads,-1,0,"environment variable %s"
486 " set to `%s'",envvar,value);
490 static void readconfig(adns_state ads, const char *filename, int warnmissing) {
493 gl_ctx.file= fopen(filename,"r");
495 if (errno == ENOENT) {
497 adns__debug(ads,-1,0, "configuration file"
498 " `%s' does not exist",filename);
502 adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
503 filename,strerror(errno));
507 readconfiggeneric(ads,filename,gl_file,gl_ctx);
512 static void readconfigtext(adns_state ads, const char *text,
513 const char *showname) {
517 readconfiggeneric(ads,showname,gl_text,gl_ctx);
520 static void readconfigenv(adns_state ads, const char *envvar) {
521 const char *filename;
523 if (ads->iflags & adns_if_noenv) {
524 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
527 filename= instrum_getenv(ads,envvar);
528 if (filename) readconfig(ads,filename,1);
531 static void readconfigenvtext(adns_state ads, const char *envvar) {
532 const char *textdata;
534 if (ads->iflags & adns_if_noenv) {
535 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
538 textdata= instrum_getenv(ads,envvar);
539 if (textdata) readconfigtext(ads,textdata,envvar);
543 int adns__setnonblock(adns_state ads, int fd) {
546 r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
548 r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
552 static int init_begin(adns_state *ads_r, adns_initflags flags,
553 adns_logcallbackfn *logfn, void *logfndata) {
557 ads= malloc(sizeof(*ads)); if (!ads) return errno;
561 ads->logfndata= logfndata;
563 LIST_INIT(ads->udpw);
564 LIST_INIT(ads->tcpw);
565 LIST_INIT(ads->childw);
566 LIST_INIT(ads->output);
569 ads->udpsocket= ads->tcpsocket= -1;
570 adns__vbuf_init(&ads->tcpsend);
571 adns__vbuf_init(&ads->tcprecv);
572 ads->tcprecv_skip= 0;
573 ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
575 ads->tcpstate= server_disconnected;
576 timerclear(&ads->tcptimeout);
580 ads->rand48xsubi[0]= pid;
581 ads->rand48xsubi[1]= (unsigned long)pid >> 16;
582 ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16);
588 static int init_finish(adns_state ads) {
589 struct sockaddr_in sin;
590 struct protoent *proto;
593 if (!ads->nservers) {
594 if (ads->logfn && ads->iflags & adns_if_debug)
595 adns__lprintf(ads,"adns: no nameservers, using IPv4 localhost\n");
596 memset(&sin, 0, sizeof(sin));
597 sin.sin_family = AF_INET;
598 sin.sin_port = htons(DNS_PORT);
599 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
600 addserver(ads,(struct sockaddr *)&sin, sizeof(sin));
603 /* we can't cope with multiple transport address families yet */
604 for (i=0; i<ads->nservers; i++)
605 assert(ads->servers[i].addr.sa.sa_family==AF_INET);
607 proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
608 ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
609 if (ads->udpsocket<0) { r= errno; goto x_free; }
611 r= adns__setnonblock(ads,ads->udpsocket);
612 if (r) { r= errno; goto x_closeudp; }
617 close(ads->udpsocket);
623 static void init_abort(adns_state ads) {
624 if (ads->nsearchlist) {
625 free(ads->searchlist[0]);
626 free(ads->searchlist);
631 static void logfn_file(adns_state ads, void *logfndata,
632 const char *fmt, va_list al) {
633 vfprintf(logfndata,fmt,al);
636 static int init_files(adns_state *ads_r, adns_initflags flags,
637 adns_logcallbackfn *logfn, void *logfndata) {
639 const char *res_options, *adns_res_options;
642 r= init_begin(&ads, flags, logfn, logfndata);
645 res_options= instrum_getenv(ads,"RES_OPTIONS");
646 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
647 ccf_options(ads,"RES_OPTIONS",-1,res_options);
648 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
650 readconfig(ads,"/etc/resolv.conf",1);
651 readconfig(ads,"/etc/resolv-adns.conf",0);
652 readconfigenv(ads,"RES_CONF");
653 readconfigenv(ads,"ADNS_RES_CONF");
655 readconfigenvtext(ads,"RES_CONF_TEXT");
656 readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
658 ccf_options(ads,"RES_OPTIONS",-1,res_options);
659 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
661 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
662 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
664 if (ads->configerrno && ads->configerrno != EINVAL) {
673 adns__consistency(ads,0,cc_entex);
678 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
679 return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr);
682 static int init_strcfg(adns_state *ads_r, adns_initflags flags,
683 adns_logcallbackfn *logfn, void *logfndata,
684 const char *configtext) {
688 r= init_begin(&ads, flags, logfn, logfndata);
691 readconfigtext(ads,configtext,"<supplied configuration text>");
692 if (ads->configerrno) {
698 r= init_finish(ads); if (r) return r;
699 adns__consistency(ads,0,cc_entex);
704 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
705 FILE *diagfile, const char *configtext) {
706 return init_strcfg(ads_r, flags,
707 diagfile ? logfn_file : 0, diagfile,
711 int adns_init_logfn(adns_state *newstate_r, adns_initflags flags,
712 const char *configtext /*0=>use default config files*/,
713 adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */,
714 void *logfndata /*0 with logfn==0 => discard*/) {
715 if (!logfn && logfndata)
718 return init_strcfg(newstate_r, flags, logfn, logfndata, configtext);
720 return init_files(newstate_r, flags, logfn, logfndata);
723 void adns_finish(adns_state ads) {
724 adns__consistency(ads,0,cc_entex);
726 if (ads->udpw.head) adns_cancel(ads->udpw.head);
727 else if (ads->tcpw.head) adns_cancel(ads->tcpw.head);
728 else if (ads->childw.head) adns_cancel(ads->childw.head);
729 else if (ads->output.head) adns_cancel(ads->output.head);
732 close(ads->udpsocket);
733 if (ads->tcpsocket >= 0) close(ads->tcpsocket);
734 adns__vbuf_free(&ads->tcpsend);
735 adns__vbuf_free(&ads->tcprecv);
740 void adns_forallqueries_begin(adns_state ads) {
741 adns__consistency(ads,0,cc_entex);
743 ads->udpw.head ? ads->udpw.head :
744 ads->tcpw.head ? ads->tcpw.head :
745 ads->childw.head ? ads->childw.head :
749 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
752 adns__consistency(ads,0,cc_entex);
753 nqu= ads->forallnext;
759 } else if (qu == ads->udpw.tail) {
761 ads->tcpw.head ? ads->tcpw.head :
762 ads->childw.head ? ads->childw.head :
764 } else if (qu == ads->tcpw.tail) {
766 ads->childw.head ? ads->childw.head :
768 } else if (qu == ads->childw.tail) {
769 nqu= ads->output.head;
773 if (!qu->parent) break;
775 ads->forallnext= nqu;
776 if (context_r) *context_r= qu->ctx.ext;