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 const afinfo *const transport_aftab[] = {
50 static const afinfo *find_afinfo(int af)
54 for (i = 0; transport_aftab[i]; i++)
55 if (transport_aftab[i]->af == af) return transport_aftab[i];
59 static void addserver(adns_state ads, struct sockaddr *sa, int n) {
64 ai = find_afinfo(sa->sa_family);
67 "nameserver %s for unknown address family %d ignored",
68 adns__sockaddr_ntoa(sa, n), sa->sa_family);
71 for (i=0; i<ads->nservers; i++) {
72 if (ads->servers[i].addr.sa.sa_family == sa->sa_family &&
73 ai->sockaddr_equalp(sa, &ads->servers[i].addr.sa)) {
74 adns__debug(ads,-1,0,"duplicate nameserver %s ignored",
75 adns__sockaddr_ntoa(sa, n));
80 if (ads->nservers>=MAXSERVERS) {
81 adns__diag(ads,-1,0,"too many nameservers, ignoring %s",
82 adns__sockaddr_ntoa(sa, n));
86 ss= ads->servers+ads->nservers;
87 assert(n <= sizeof(ss->addr));
89 memcpy(&ss->addr, sa, n);
93 static void freesearchlist(adns_state ads) {
94 if (ads->nsearchlist) free(*ads->searchlist);
95 free(ads->searchlist);
98 static void saveerr(adns_state ads, int en) {
99 if (!ads->configerrno) ads->configerrno= en;
102 static void configparseerr(adns_state ads, const char *fn, int lno,
103 const char *fmt, ...) {
107 if (!ads->logfn || (ads->iflags & adns_if_noerrprint)) return;
109 if (lno==-1) adns__lprintf(ads,"adns: %s: ",fn);
110 else adns__lprintf(ads,"adns: %s:%d: ",fn,lno);
112 adns__vlprintf(ads,fmt,al);
114 adns__lprintf(ads,"\n");
117 static int nextword(const char **bufp_io, const char **word_r, int *l_r) {
121 while (ctype_whitespace(*p)) p++;
125 while (*q && !ctype_whitespace(*q)) q++;
134 static void ccf_nameserver(adns_state ads, const char *fn,
135 int lno, const char *buf) {
136 struct addrinfo *ai, ai_hint = { 0 };
139 ai_hint.ai_family = AF_UNSPEC;
140 ai_hint.ai_socktype = SOCK_DGRAM;
141 ai_hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
143 err = getaddrinfo(buf, STRINGIFY(DNS_PORT), &ai_hint, &ai);
145 configparseerr(ads,fn,lno,"invalid nameserver address `%s' (%s)",
146 buf, gai_strerror(err));
150 adns__debug(ads,-1,0,"using nameserver %s",
151 adns__sockaddr_ntoa(ai->ai_addr, ai->ai_addrlen));
152 addserver(ads, ai->ai_addr, ai->ai_addrlen);
156 static void ccf_search(adns_state ads, const char *fn,
157 int lno, const char *buf) {
158 const char *bufp, *word;
159 char *newchars, **newptrs, **pp;
167 while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
169 newptrs= malloc(sizeof(char*)*count);
170 if (!newptrs) { saveerr(ads,errno); return; }
172 newchars= malloc(tl);
173 if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
177 while (nextword(&bufp,&word,&l)) {
179 memcpy(newchars,word,l);
185 ads->nsearchlist= count;
186 ads->searchlist= newptrs;
189 static void ccf_sortlist(adns_state ads, const char *fn,
190 int lno, const char *buf) {
192 char tbuf[200], *slash, *ep;
193 const char *maskwhat;
202 while (nextword(&buf,&word,&l)) {
203 if (ads->nsortlist >= MAXSORTLIST) {
204 adns__diag(ads,-1,0,"too many sortlist entries,"
205 " ignoring %.*s onwards",l,word);
209 if (l >= sizeof(tbuf)) {
210 configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
214 memcpy(tbuf,word,l); tbuf[l]= 0;
215 slash= strchr(tbuf,'/');
216 if (slash) *slash++= 0;
218 sl= &ads->sortlist[ads->nsortlist];
220 if (strchr(tbuf, ':'))
221 ai= &adns__inet6_afinfo;
223 ai= &adns__inet_afinfo;
225 if (!inet_pton(ai->af, tbuf, &sl->base)) {
226 configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
231 if (strchr(slash,ai->delim)) {
233 if (!inet_pton(ai->af,slash,&sl->mask)) {
234 configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
238 maskwhat = "prefix length";
239 initial= strtoul(slash,&ep,10);
240 if (*ep || initial>ai->width) {
241 configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
244 ai->prefix_mask(initial, &sl->mask);
247 maskwhat = "implied prefix length";
248 initial = ai->guess_len(&sl->base);
250 configparseerr(ads,fn,lno, "network address `%s'"
251 " in sortlist is not in classed ranges,"
252 " must specify mask explicitly", tbuf);
255 ai->prefix_mask(initial, &sl->mask);
258 if (!ai->matchp(&sl->base, &sl->base, &sl->mask)) {
260 configparseerr(ads,fn,lno, "%s %d in sortlist"
261 " overlaps address `%s'",maskwhat,initial,tbuf);
263 configparseerr(ads,fn,lno, "%s `%s' in sortlist"
264 " overlaps address `%s'",maskwhat,slash,tbuf);
274 static void ccf_options(adns_state ads, const char *fn,
275 int lno, const char *buf) {
283 while (nextword(&buf,&word,&l)) {
284 if (l==5 && !memcmp(word,"debug",5)) {
285 ads->iflags |= adns_if_debug;
288 if (l>=6 && !memcmp(word,"ndots:",6)) {
289 v= strtoul(word+6,&ep,10);
290 if (l==6 || ep != word+l || v > INT_MAX) {
291 configparseerr(ads,fn,lno,"option `%.*s' malformed"
292 " or has bad value",l,word);
298 if (l>=12 && !memcmp(word,"adns_checkc:",12)) {
299 if (!strcmp(word+12,"none")) {
300 ads->iflags &= ~adns_if_checkc_freq;
301 ads->iflags |= adns_if_checkc_entex;
302 } else if (!strcmp(word+12,"entex")) {
303 ads->iflags &= ~adns_if_checkc_freq;
304 ads->iflags |= adns_if_checkc_entex;
305 } else if (!strcmp(word+12,"freq")) {
306 ads->iflags |= adns_if_checkc_freq;
308 configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' "
309 "(must be none, entex or freq", word+12);
313 adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
317 static void ccf_clearnss(adns_state ads, const char *fn,
318 int lno, const char *buf) {
322 static void ccf_include(adns_state ads, const char *fn,
323 int lno, const char *buf) {
325 configparseerr(ads,fn,lno,"`include' directive with no filename");
328 readconfig(ads,buf,1);
331 static void ccf_lookup(adns_state ads, const char *fn, int lno,
338 configparseerr(ads,fn,lno,"`lookup' directive with no databases");
342 while (nextword(&buf,&word,&l)) {
343 if (l==4 && !memcmp(word,"bind",4)) {
345 } else if (l==4 && !memcmp(word,"file",4)) {
346 /* ignore this and hope /etc/hosts is not essential */
347 } else if (l==2 && !memcmp(word,"yp",2)) {
348 adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno);
351 adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
357 adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno);
360 static const struct configcommandinfo {
362 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
363 } configcommandinfos[]= {
364 { "nameserver", ccf_nameserver },
365 { "domain", ccf_search },
366 { "search", ccf_search },
367 { "sortlist", ccf_sortlist },
368 { "options", ccf_options },
369 { "clearnameservers", ccf_clearnss },
370 { "include", ccf_include },
371 { "lookup", ccf_lookup }, /* OpenBSD */
380 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
381 int lno, char *buf, int buflen) {
382 FILE *file= src_io->file;
390 for (;;) { /* loop over chars */
392 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
397 adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
399 } else if (c == '\n') {
401 } else if (c == EOF) {
404 adns__diag(ads,-1,0,"%s:%d: read error: %s",
405 filename,lno,strerror(errno));
421 while ((c= getc(file)) != EOF && c != '\n');
425 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
426 int lno, char *buf, int buflen) {
427 const char *cp= src_io->text;
430 if (!cp || !*cp) return -1;
432 if (*cp == ';' || *cp == '\n') cp++;
433 l= strcspn(cp,";\n");
437 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
447 static void readconfiggeneric(adns_state ads, const char *filename,
448 int (*getline)(adns_state ads, getline_ctx*,
449 const char *filename, int lno,
450 char *buf, int buflen),
451 /* Returns >=0 for success, -1 for EOF or error
452 * (error will have been reported), or -2 for
453 * bad line was encountered, try again.
455 getline_ctx gl_ctx) {
456 char linebuf[2000], *p, *q;
458 const struct configcommandinfo *ccip;
461 (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
463 if (l == -2) continue;
464 while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
467 while (ctype_whitespace(*p)) p++;
468 if (*p == '#' || *p == ';' || !*p) continue;
470 while (*q && !ctype_whitespace(*q)) q++;
472 for (ccip=configcommandinfos;
474 !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
477 adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
478 filename,lno,(int)(q-p),p);
481 while (ctype_whitespace(*q)) q++;
482 ccip->fn(ads,filename,lno,q);
486 static const char *instrum_getenv(adns_state ads, const char *envvar) {
489 value= getenv(envvar);
490 if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
491 else adns__debug(ads,-1,0,"environment variable %s"
492 " set to `%s'",envvar,value);
496 static void readconfig(adns_state ads, const char *filename, int warnmissing) {
499 gl_ctx.file= fopen(filename,"r");
501 if (errno == ENOENT) {
503 adns__debug(ads,-1,0, "configuration file"
504 " `%s' does not exist",filename);
508 adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
509 filename,strerror(errno));
513 readconfiggeneric(ads,filename,gl_file,gl_ctx);
518 static void readconfigtext(adns_state ads, const char *text,
519 const char *showname) {
523 readconfiggeneric(ads,showname,gl_text,gl_ctx);
526 static void readconfigenv(adns_state ads, const char *envvar) {
527 const char *filename;
529 if (ads->iflags & adns_if_noenv) {
530 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
533 filename= instrum_getenv(ads,envvar);
534 if (filename) readconfig(ads,filename,1);
537 static void readconfigenvtext(adns_state ads, const char *envvar) {
538 const char *textdata;
540 if (ads->iflags & adns_if_noenv) {
541 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
544 textdata= instrum_getenv(ads,envvar);
545 if (textdata) readconfigtext(ads,textdata,envvar);
549 int adns__setnonblock(adns_state ads, int fd) {
552 r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
554 r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
558 static int init_begin(adns_state *ads_r, adns_initflags flags,
559 adns_logcallbackfn *logfn, void *logfndata) {
563 ads= malloc(sizeof(*ads)); if (!ads) return errno;
567 ads->logfndata= logfndata;
569 LIST_INIT(ads->udpw);
570 LIST_INIT(ads->tcpw);
571 LIST_INIT(ads->childw);
572 LIST_INIT(ads->output);
577 adns__vbuf_init(&ads->tcpsend);
578 adns__vbuf_init(&ads->tcprecv);
579 ads->tcprecv_skip= 0;
580 ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
582 ads->tcpstate= server_disconnected;
583 timerclear(&ads->tcptimeout);
587 ads->rand48xsubi[0]= pid;
588 ads->rand48xsubi[1]= (unsigned long)pid >> 16;
589 ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16);
595 static int init_finish(adns_state ads) {
596 struct sockaddr_in sin;
597 struct protoent *proto;
598 struct udpsocket *udp;
602 if (!ads->nservers) {
603 if (ads->logfn && ads->iflags & adns_if_debug)
604 adns__lprintf(ads,"adns: no nameservers, using IPv4 localhost\n");
605 memset(&sin, 0, sizeof(sin));
606 sin.sin_port = htons(DNS_PORT);
607 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
608 addserver(ads,(struct sockaddr *)&sin, sizeof(sin));
611 proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
613 for (i = 0; i < ads->nservers; i++) {
614 for (j = 0; j < ads->nudp; j++) {
615 if (ads->udpsocket[j].ai->af == ads->servers[i].addr.sa.sa_family)
619 assert(ads->nudp < MAXUDP);
620 udp = &ads->udpsocket[ads->nudp];
621 udp->ai = find_afinfo(ads->servers[i].addr.sa.sa_family);
623 udp->fd = socket(udp->ai->af,SOCK_DGRAM,proto->p_proto);
624 if (udp->fd < 0) { r= errno; goto x_free; }
625 r= adns__setnonblock(ads,udp->fd);
626 if (r) { r= errno; goto x_closeudp; }
635 for (j = 0; j < ads->nudp; j++) close(ads->udpsocket[j].fd);
641 static void init_abort(adns_state ads) {
642 if (ads->nsearchlist) {
643 free(ads->searchlist[0]);
644 free(ads->searchlist);
649 static void logfn_file(adns_state ads, void *logfndata,
650 const char *fmt, va_list al) {
651 vfprintf(logfndata,fmt,al);
654 static int init_files(adns_state *ads_r, adns_initflags flags,
655 adns_logcallbackfn *logfn, void *logfndata) {
657 const char *res_options, *adns_res_options;
660 r= init_begin(&ads, flags, logfn, logfndata);
663 res_options= instrum_getenv(ads,"RES_OPTIONS");
664 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
665 ccf_options(ads,"RES_OPTIONS",-1,res_options);
666 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
668 readconfig(ads,"/etc/resolv.conf",1);
669 readconfig(ads,"/etc/resolv-adns.conf",0);
670 readconfigenv(ads,"RES_CONF");
671 readconfigenv(ads,"ADNS_RES_CONF");
673 readconfigenvtext(ads,"RES_CONF_TEXT");
674 readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
676 ccf_options(ads,"RES_OPTIONS",-1,res_options);
677 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
679 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
680 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
682 if (ads->configerrno && ads->configerrno != EINVAL) {
691 adns__consistency(ads,0,cc_entex);
696 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
697 return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr);
700 static int init_strcfg(adns_state *ads_r, adns_initflags flags,
701 adns_logcallbackfn *logfn, void *logfndata,
702 const char *configtext) {
706 r= init_begin(&ads, flags, logfn, logfndata);
709 readconfigtext(ads,configtext,"<supplied configuration text>");
710 if (ads->configerrno) {
716 r= init_finish(ads); if (r) return r;
717 adns__consistency(ads,0,cc_entex);
722 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
723 FILE *diagfile, const char *configtext) {
724 return init_strcfg(ads_r, flags,
725 diagfile ? logfn_file : 0, diagfile,
729 int adns_init_logfn(adns_state *newstate_r, adns_initflags flags,
730 const char *configtext /*0=>use default config files*/,
731 adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */,
732 void *logfndata /*0 with logfn==0 => discard*/) {
733 if (!logfn && logfndata)
736 return init_strcfg(newstate_r, flags, logfn, logfndata, configtext);
738 return init_files(newstate_r, flags, logfn, logfndata);
741 void adns_finish(adns_state ads) {
743 adns__consistency(ads,0,cc_entex);
745 if (ads->udpw.head) adns_cancel(ads->udpw.head);
746 else if (ads->tcpw.head) adns_cancel(ads->tcpw.head);
747 else if (ads->childw.head) adns_cancel(ads->childw.head);
748 else if (ads->output.head) adns_cancel(ads->output.head);
751 for (i = 0; i < ads->nudp; i++) close(ads->udpsocket[i].fd);
752 if (ads->tcpsocket >= 0) close(ads->tcpsocket);
753 adns__vbuf_free(&ads->tcpsend);
754 adns__vbuf_free(&ads->tcprecv);
759 void adns_forallqueries_begin(adns_state ads) {
760 adns__consistency(ads,0,cc_entex);
762 ads->udpw.head ? ads->udpw.head :
763 ads->tcpw.head ? ads->tcpw.head :
764 ads->childw.head ? ads->childw.head :
768 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
771 adns__consistency(ads,0,cc_entex);
772 nqu= ads->forallnext;
778 } else if (qu == ads->udpw.tail) {
780 ads->tcpw.head ? ads->tcpw.head :
781 ads->childw.head ? ads->childw.head :
783 } else if (qu == ads->tcpw.tail) {
785 ads->childw.head ? ads->childw.head :
787 } else if (qu == ads->childw.tail) {
788 nqu= ads->output.head;
792 if (!qu->parent) break;
794 ads->forallnext= nqu;
795 if (context_r) *context_r= qu->ctx.ext;