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,2014 Ian Jackson
9 * Copyright (C) 2014 Mark Wooding
10 * Copyright (C) 1999-2000,2003,2006 Tony Finch
11 * Copyright (C) 1991 Massachusetts Institute of Technology
12 * (See the file INSTALL for full details.)
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3, or (at your option)
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation.
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 salen) {
47 char buf[ADNS_ADDR2TEXT_BUFLEN];
49 for (i=0; i<ads->nservers; i++) {
50 if (adns__sockaddrs_equal(sa, &ads->servers[i].addr.sa)) {
51 adns__debug(ads,-1,0,"duplicate nameserver %s ignored",
52 adns__sockaddr_ntoa(sa, buf));
57 if (ads->nservers>=MAXSERVERS) {
58 adns__diag(ads,-1,0,"too many nameservers, ignoring %s",
59 adns__sockaddr_ntoa(sa, buf));
63 ss= ads->servers+ads->nservers;
64 assert(salen <= sizeof(ss->addr));
66 memcpy(&ss->addr, sa, salen);
70 static void freesearchlist(adns_state ads) {
71 if (ads->nsearchlist) free(*ads->searchlist);
72 free(ads->searchlist);
75 static void saveerr(adns_state ads, int en) {
76 if (!ads->configerrno) ads->configerrno= en;
79 static void configparseerr(adns_state ads, const char *fn, int lno,
80 const char *fmt, ...) {
84 if (!ads->logfn || (ads->iflags & adns_if_noerrprint)) return;
86 if (lno==-1) adns__lprintf(ads,"adns: %s: ",fn);
87 else adns__lprintf(ads,"adns: %s:%d: ",fn,lno);
89 adns__vlprintf(ads,fmt,al);
91 adns__lprintf(ads,"\n");
94 static int nextword(const char **bufp_io, const char **word_r, int *l_r) {
98 while (ctype_whitespace(*p)) p++;
102 while (*q && !ctype_whitespace(*q)) q++;
111 static void ccf_nameserver(adns_state ads, const char *fn,
112 int lno, const char *buf) {
114 char addrbuf[ADNS_ADDR2TEXT_BUFLEN];
117 a.len= sizeof(a.addr);
118 err= adns_text2addr(buf,DNS_PORT, 0, &a.addr.sa,&a.len);
123 configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
126 configparseerr(ads,fn,lno,"failed to parse nameserver address `%s': %s",
130 adns__debug(ads,-1,0,"using nameserver %s",
131 adns__sockaddr_ntoa(&a.addr.sa, addrbuf));
132 addserver(ads,&a.addr.sa,a.len);
135 static void ccf_search(adns_state ads, const char *fn,
136 int lno, const char *buf) {
137 const char *bufp, *word;
138 char *newchars, **newptrs, **pp;
146 while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
148 newptrs= malloc(sizeof(char*)*count);
149 if (!newptrs) { saveerr(ads,errno); return; }
151 newchars= malloc(tl);
152 if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
156 while (nextword(&bufp,&word,&l)) {
158 memcpy(newchars,word,l);
164 ads->nsearchlist= count;
165 ads->searchlist= newptrs;
168 static int gen_pton(const char *text, int want_af, adns_sockaddr *a) {
173 err= adns_text2addr(text,0, adns_qf_addrlit_scope_forbid,
175 if (err) { assert(err == EINVAL); return 0; }
176 if (want_af != AF_UNSPEC && a->sa.sa_family != want_af) return 0;
180 static void ccf_sortlist(adns_state ads, const char *fn,
181 int lno, const char *buf) {
183 char tbuf[200], *slash, *ep;
184 const char *maskwhat;
192 while (nextword(&buf,&word,&l)) {
193 if (ads->nsortlist >= MAXSORTLIST) {
194 adns__diag(ads,-1,0,"too many sortlist entries,"
195 " ignoring %.*s onwards",l,word);
199 if (l >= sizeof(tbuf)) {
200 configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
204 memcpy(tbuf,word,l); tbuf[l]= 0;
205 slash= strchr(tbuf,'/');
206 if (slash) *slash++= 0;
208 sl= &ads->sortlist[ads->nsortlist];
209 if (!gen_pton(tbuf, AF_UNSPEC, &sl->base)) {
210 configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
215 if (slash[strspn(slash, "0123456789")]) {
217 if (!gen_pton(slash, sl->base.sa.sa_family, &sl->mask)) {
218 configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
222 maskwhat = "prefix length";
223 initial= strtoul(slash,&ep,10);
224 if (*ep || initial>adns__addr_width(sl->base.sa.sa_family)) {
225 configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
228 sl->mask.sa.sa_family= sl->base.sa.sa_family;
229 adns__prefix_mask(&sl->mask, initial);
232 maskwhat = "implied prefix length";
233 initial= adns__guess_prefix_length(&sl->base);
235 configparseerr(ads,fn,lno, "network address `%s'"
236 " in sortlist is not in classed ranges,"
237 " must specify mask explicitly", tbuf);
240 sl->mask.sa.sa_family= sl->base.sa.sa_family;
241 adns__prefix_mask(&sl->mask, initial);
244 if (!adns__addr_matches(sl->base.sa.sa_family,
245 adns__sockaddr_addr(&sl->base.sa),
246 &sl->base,&sl->mask)) {
248 configparseerr(ads,fn,lno, "%s %d in sortlist"
249 " overlaps address `%s'",maskwhat,initial,tbuf);
251 configparseerr(ads,fn,lno, "%s `%s' in sortlist"
252 " overlaps address `%s'",maskwhat,slash,tbuf);
261 static void ccf_options(adns_state ads, const char *fn,
262 int lno, const char *buf) {
270 while (nextword(&buf,&word,&l)) {
271 if (l==5 && !memcmp(word,"debug",5)) {
272 ads->iflags |= adns_if_debug;
275 if (l>=6 && !memcmp(word,"ndots:",6)) {
276 v= strtoul(word+6,&ep,10);
277 if (l==6 || ep != word+l || v > INT_MAX) {
278 configparseerr(ads,fn,lno,"option `%.*s' malformed"
279 " or has bad value",l,word);
285 if (l>=12 && !memcmp(word,"adns_checkc:",12)) {
286 if (!strcmp(word+12,"none")) {
287 ads->iflags &= ~adns_if_checkc_freq;
288 ads->iflags |= adns_if_checkc_entex;
289 } else if (!strcmp(word+12,"entex")) {
290 ads->iflags &= ~adns_if_checkc_freq;
291 ads->iflags |= adns_if_checkc_entex;
292 } else if (!strcmp(word+12,"freq")) {
293 ads->iflags |= adns_if_checkc_freq;
295 configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' "
296 "(must be none, entex or freq", word+12);
300 if (l>=8 && !memcmp(word,"adns_af:",8)) {
302 ads->iflags &= ~adns_if_afmask;
303 if (strcmp(word,"any")) for (;;) {
304 i= strcspn(word,",");
305 if (i>=4 && !memcmp(word,"ipv4",4))
306 ads->iflags |= adns_if_permit_ipv4;
307 else if (i>=4 && !memcmp(word,"ipv6",4))
308 ads->iflags |= adns_if_permit_ipv6;
310 configparseerr(ads,fn,lno, "option adns_af has bad value `%.*s' "
311 "(must be `any' or list {`ipv4',`ipv6'},...)",
320 adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
324 static void ccf_clearnss(adns_state ads, const char *fn,
325 int lno, const char *buf) {
329 static void ccf_include(adns_state ads, const char *fn,
330 int lno, const char *buf) {
332 configparseerr(ads,fn,lno,"`include' directive with no filename");
335 readconfig(ads,buf,1);
338 static void ccf_lookup(adns_state ads, const char *fn, int lno,
345 configparseerr(ads,fn,lno,"`lookup' directive with no databases");
349 while (nextword(&buf,&word,&l)) {
350 if (l==4 && !memcmp(word,"bind",4)) {
352 } else if (l==4 && !memcmp(word,"file",4)) {
353 /* ignore this and hope /etc/hosts is not essential */
354 } else if (l==2 && !memcmp(word,"yp",2)) {
355 adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno);
358 adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
364 adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno);
367 static const struct configcommandinfo {
369 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
370 } configcommandinfos[]= {
371 { "nameserver", ccf_nameserver },
372 { "domain", ccf_search },
373 { "search", ccf_search },
374 { "sortlist", ccf_sortlist },
375 { "options", ccf_options },
376 { "clearnameservers", ccf_clearnss },
377 { "include", ccf_include },
378 { "lookup", ccf_lookup }, /* OpenBSD */
387 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
388 int lno, char *buf, int buflen) {
389 FILE *file= src_io->file;
397 for (;;) { /* loop over chars */
399 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
404 adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
406 } else if (c == '\n') {
408 } else if (c == EOF) {
411 adns__diag(ads,-1,0,"%s:%d: read error: %s",
412 filename,lno,strerror(errno));
428 while ((c= getc(file)) != EOF && c != '\n');
432 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
433 int lno, char *buf, int buflen) {
434 const char *cp= src_io->text;
437 if (!cp || !*cp) return -1;
439 if (*cp == ';' || *cp == '\n') cp++;
440 l= strcspn(cp,";\n");
444 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
454 static void readconfiggeneric(adns_state ads, const char *filename,
455 int (*getline)(adns_state ads, getline_ctx*,
456 const char *filename, int lno,
457 char *buf, int buflen),
458 /* Returns >=0 for success, -1 for EOF or error
459 * (error will have been reported), or -2 for
460 * bad line was encountered, try again.
462 getline_ctx gl_ctx) {
463 char linebuf[2000], *p, *q;
465 const struct configcommandinfo *ccip;
468 (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
470 if (l == -2) continue;
471 while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
474 while (ctype_whitespace(*p)) p++;
475 if (*p == '#' || *p == ';' || !*p) continue;
477 while (*q && !ctype_whitespace(*q)) q++;
479 for (ccip=configcommandinfos;
481 !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
484 adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
485 filename,lno,(int)(q-p),p);
488 while (ctype_whitespace(*q)) q++;
489 ccip->fn(ads,filename,lno,q);
493 static const char *instrum_getenv(adns_state ads, const char *envvar) {
496 value= getenv(envvar);
497 if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
498 else adns__debug(ads,-1,0,"environment variable %s"
499 " set to `%s'",envvar,value);
503 static void readconfig(adns_state ads, const char *filename, int warnmissing) {
506 gl_ctx.file= fopen(filename,"r");
508 if (errno == ENOENT) {
510 adns__debug(ads,-1,0, "configuration file"
511 " `%s' does not exist",filename);
515 adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
516 filename,strerror(errno));
520 readconfiggeneric(ads,filename,gl_file,gl_ctx);
525 static void readconfigtext(adns_state ads, const char *text,
526 const char *showname) {
530 readconfiggeneric(ads,showname,gl_text,gl_ctx);
533 static void readconfigenv(adns_state ads, const char *envvar) {
534 const char *filename;
536 if (ads->iflags & adns_if_noenv) {
537 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
540 filename= instrum_getenv(ads,envvar);
541 if (filename) readconfig(ads,filename,1);
544 static void readconfigenvtext(adns_state ads, const char *envvar) {
545 const char *textdata;
547 if (ads->iflags & adns_if_noenv) {
548 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
551 textdata= instrum_getenv(ads,envvar);
552 if (textdata) readconfigtext(ads,textdata,envvar);
556 int adns__setnonblock(adns_state ads, int fd) {
559 r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
561 r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
565 static int init_begin(adns_state *ads_r, adns_initflags flags,
566 adns_logcallbackfn *logfn, void *logfndata) {
570 ads= malloc(sizeof(*ads)); if (!ads) return errno;
574 ads->logfndata= logfndata;
576 LIST_INIT(ads->udpw);
577 LIST_INIT(ads->tcpw);
578 LIST_INIT(ads->childw);
579 LIST_INIT(ads->output);
580 LIST_INIT(ads->intdone);
585 adns__vbuf_init(&ads->tcpsend);
586 adns__vbuf_init(&ads->tcprecv);
587 ads->tcprecv_skip= 0;
588 ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
590 ads->tcpstate= server_disconnected;
591 timerclear(&ads->tcptimeout);
595 ads->rand48xsubi[0]= pid;
596 ads->rand48xsubi[1]= (unsigned long)pid >> 16;
597 ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16);
603 static int init_finish(adns_state ads) {
604 struct sockaddr_in sin;
605 struct protoent *proto;
606 struct udpsocket *udp;
610 if (!ads->nservers) {
611 if (ads->logfn && ads->iflags & adns_if_debug)
612 adns__lprintf(ads,"adns: no nameservers, using IPv4 localhost\n");
613 memset(&sin, 0, sizeof(sin));
614 sin.sin_family = AF_INET;
615 sin.sin_port = htons(DNS_PORT);
616 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
617 addserver(ads,(struct sockaddr *)&sin, sizeof(sin));
620 proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
622 for (i=0; i<ads->nservers; i++) {
623 if (adns__udpsocket_by_af(ads, ads->servers[i].addr.sa.sa_family))
625 assert(ads->nudpsockets < MAXUDP);
626 udp= &ads->udpsockets[ads->nudpsockets];
627 udp->af= ads->servers[i].addr.sa.sa_family;
628 udp->fd= socket(udp->af,SOCK_DGRAM,proto->p_proto);
629 if (udp->fd < 0) { r= errno; goto x_free; }
631 r= adns__setnonblock(ads,udp->fd);
632 if (r) { r= errno; goto x_closeudp; }
638 for (i=0; i<ads->nudpsockets; i++) close(ads->udpsockets[i].fd);
644 static void init_abort(adns_state ads) {
645 if (ads->nsearchlist) {
646 free(ads->searchlist[0]);
647 free(ads->searchlist);
652 static void logfn_file(adns_state ads, void *logfndata,
653 const char *fmt, va_list al) {
654 vfprintf(logfndata,fmt,al);
657 static int init_files(adns_state *ads_r, adns_initflags flags,
658 adns_logcallbackfn *logfn, void *logfndata) {
660 const char *res_options, *adns_res_options;
663 r= init_begin(&ads, flags, logfn, logfndata);
666 res_options= instrum_getenv(ads,"RES_OPTIONS");
667 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
668 ccf_options(ads,"RES_OPTIONS",-1,res_options);
669 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
671 readconfig(ads,"/etc/resolv.conf",1);
672 readconfig(ads,"/etc/resolv-adns.conf",0);
673 readconfigenv(ads,"RES_CONF");
674 readconfigenv(ads,"ADNS_RES_CONF");
676 readconfigenvtext(ads,"RES_CONF_TEXT");
677 readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
679 ccf_options(ads,"RES_OPTIONS",-1,res_options);
680 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
682 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
683 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
685 if (ads->configerrno && ads->configerrno != EINVAL) {
694 adns__consistency(ads,0,cc_entex);
699 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
700 return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr);
703 static int init_strcfg(adns_state *ads_r, adns_initflags flags,
704 adns_logcallbackfn *logfn, void *logfndata,
705 const char *configtext) {
709 r= init_begin(&ads, flags, logfn, logfndata);
712 readconfigtext(ads,configtext,"<supplied configuration text>");
713 if (ads->configerrno) {
719 r= init_finish(ads); if (r) return r;
720 adns__consistency(ads,0,cc_entex);
725 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
726 FILE *diagfile, const char *configtext) {
727 return init_strcfg(ads_r, flags,
728 diagfile ? logfn_file : 0, diagfile,
732 int adns_init_logfn(adns_state *newstate_r, adns_initflags flags,
733 const char *configtext /*0=>use default config files*/,
734 adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */,
735 void *logfndata /*0 with logfn==0 => discard*/) {
736 if (!logfn && logfndata)
739 return init_strcfg(newstate_r, flags, logfn, logfndata, configtext);
741 return init_files(newstate_r, flags, logfn, logfndata);
744 void adns_finish(adns_state ads) {
746 adns__consistency(ads,0,cc_entex);
748 if (ads->udpw.head) adns__cancel(ads->udpw.head);
749 else if (ads->tcpw.head) adns__cancel(ads->tcpw.head);
750 else if (ads->childw.head) adns__cancel(ads->childw.head);
751 else if (ads->output.head) adns__cancel(ads->output.head);
752 else if (ads->intdone.head) adns__cancel(ads->output.head);
755 for (i=0; i<ads->nudpsockets; i++) close(ads->udpsockets[i].fd);
756 if (ads->tcpsocket >= 0) close(ads->tcpsocket);
757 adns__vbuf_free(&ads->tcpsend);
758 adns__vbuf_free(&ads->tcprecv);
763 void adns_forallqueries_begin(adns_state ads) {
764 adns__consistency(ads,0,cc_entex);
766 ads->udpw.head ? ads->udpw.head :
767 ads->tcpw.head ? ads->tcpw.head :
768 ads->childw.head ? ads->childw.head :
772 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
775 adns__consistency(ads,0,cc_entex);
776 nqu= ads->forallnext;
782 } else if (qu == ads->udpw.tail) {
784 ads->tcpw.head ? ads->tcpw.head :
785 ads->childw.head ? ads->childw.head :
787 } else if (qu == ads->tcpw.tail) {
789 ads->childw.head ? ads->childw.head :
791 } else if (qu == ads->childw.tail) {
792 nqu= ads->output.head;
796 if (!qu->parent) break;
798 ads->forallnext= nqu;
799 if (context_r) *context_r= qu->ctx.ext;