3 * - configuration file parsing
4 * - management of global state
8 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
10 * It is part of adns, which is
11 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
12 * Copyright (C) 1999 Tony Finch <dot@dotat.at>
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 2, 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,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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->diagfile || (ads->iflags & adns_if_noerrprint)) return;
81 if (lno==-1) fprintf(ads->diagfile,"adns: %s: ",fn);
82 else fprintf(ads->diagfile,"adns: %s:%d: ",fn,lno);
84 vfprintf(ads->diagfile,fmt,al);
86 fputc('\n',ads->diagfile);
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, int lno, const char *buf) {
109 if (!inet_aton(buf,&ia)) {
110 configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
113 adns__debug(ads,-1,0,"using nameserver %s",inet_ntoa(ia));
117 static void ccf_search(adns_state ads, const char *fn, int lno, const char *buf) {
118 const char *bufp, *word;
119 char *newchars, **newptrs, **pp;
127 while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
129 newptrs= malloc(sizeof(char*)*count); if (!newptrs) { saveerr(ads,errno); return; }
130 newchars= malloc(tl); if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
134 while (nextword(&bufp,&word,&l)) {
136 memcpy(newchars,word,l);
142 ads->nsearchlist= count;
143 ads->searchlist= newptrs;
146 static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *buf) {
148 char tbuf[200], *slash, *ep;
149 struct in_addr base, mask;
151 unsigned long initial, baselocal;
156 while (nextword(&buf,&word,&l)) {
157 if (ads->nsortlist >= MAXSORTLIST) {
158 adns__diag(ads,-1,0,"too many sortlist entries, ignoring %.*s onwards",l,word);
162 if (l >= sizeof(tbuf)) {
163 configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
167 memcpy(tbuf,word,l); tbuf[l]= 0;
168 slash= strchr(tbuf,'/');
169 if (slash) *slash++= 0;
171 if (!inet_aton(tbuf,&base)) {
172 configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
177 if (strchr(slash,'.')) {
178 if (!inet_aton(slash,&mask)) {
179 configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
182 if (base.s_addr & ~mask.s_addr) {
183 configparseerr(ads,fn,lno,
184 "mask `%s' in sortlist overlaps address `%s'",slash,tbuf);
188 initial= strtoul(slash,&ep,10);
189 if (*ep || initial>32) {
190 configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
193 mask.s_addr= htonl((0x0ffffffffUL) << (32-initial));
196 baselocal= ntohl(base.s_addr);
197 if (!baselocal & 0x080000000UL) /* class A */
198 mask.s_addr= htonl(0x0ff000000UL);
199 else if ((baselocal & 0x0c0000000UL) == 0x080000000UL)
200 mask.s_addr= htonl(0x0ffff0000UL); /* class B */
201 else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL)
202 mask.s_addr= htonl(0x0ff000000UL); /* class C */
204 configparseerr(ads,fn,lno,
205 "network address `%s' in sortlist is not in classed ranges,"
206 " must specify mask explicitly", tbuf);
211 ads->sortlist[ads->nsortlist].base= base;
212 ads->sortlist[ads->nsortlist].mask= mask;
217 static void ccf_options(adns_state ads, const char *fn, int lno, const char *buf) {
225 while (nextword(&buf,&word,&l)) {
226 if (l==5 && !memcmp(word,"debug",5)) {
227 ads->iflags |= adns_if_debug;
230 if (l>=6 && !memcmp(word,"ndots:",6)) {
231 v= strtoul(word+6,&ep,10);
232 if (l==6 || ep != word+l || v > INT_MAX) {
233 configparseerr(ads,fn,lno,"option `%.*s' malformed or has bad value",l,word);
239 if (l>=12 && !memcmp(word,"adns_checkc:",12)) {
240 if (!strcmp(word+12,"none")) {
241 ads->iflags &= ~adns_if_checkc_freq;
242 ads->iflags |= adns_if_checkc_entex;
243 } else if (!strcmp(word+12,"entex")) {
244 ads->iflags &= ~adns_if_checkc_freq;
245 ads->iflags |= adns_if_checkc_entex;
246 } else if (!strcmp(word+12,"freq")) {
247 ads->iflags |= adns_if_checkc_freq;
249 configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' "
250 "(must be none, entex or freq", word+12);
254 adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
258 static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) {
262 static void ccf_include(adns_state ads, const char *fn, int lno, const char *buf) {
264 configparseerr(ads,fn,lno,"`include' directive with no filename");
267 readconfig(ads,buf,1);
270 static const struct configcommandinfo {
272 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
273 } configcommandinfos[]= {
274 { "nameserver", ccf_nameserver },
275 { "domain", ccf_search },
276 { "search", ccf_search },
277 { "sortlist", ccf_sortlist },
278 { "options", ccf_options },
279 { "clearnameservers", ccf_clearnss },
280 { "include", ccf_include },
289 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
290 int lno, char *buf, int buflen) {
291 FILE *file= src_io->file;
299 for (;;) { /* loop over chars */
301 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
306 adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
308 } else if (c == '\n') {
310 } else if (c == EOF) {
313 adns__diag(ads,-1,0,"%s:%d: read error: %s",filename,lno,strerror(errno));
329 while ((c= getc(file)) != EOF && c != '\n');
333 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
334 int lno, char *buf, int buflen) {
335 const char *cp= src_io->text;
338 if (!cp || !*cp) return -1;
340 if (*cp == ';' || *cp == '\n') cp++;
341 l= strcspn(cp,";\n");
345 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
355 static void readconfiggeneric(adns_state ads, const char *filename,
356 int (*getline)(adns_state ads, getline_ctx*,
357 const char *filename, int lno,
358 char *buf, int buflen),
359 /* Returns >=0 for success, -1 for EOF or error
360 * (error will have been reported), or -2 for
361 * bad line was encountered, try again.
363 getline_ctx gl_ctx) {
364 char linebuf[2000], *p, *q;
366 const struct configcommandinfo *ccip;
369 (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
371 if (l == -2) continue;
372 while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
375 while (ctype_whitespace(*p)) p++;
376 if (*p == '#' || !*p) continue;
378 while (*q && !ctype_whitespace(*q)) q++;
380 for (ccip=configcommandinfos;
381 ccip->name && !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
384 adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
388 while (ctype_whitespace(*q)) q++;
389 ccip->fn(ads,filename,lno,q);
393 static const char *instrum_getenv(adns_state ads, const char *envvar) {
396 value= getenv(envvar);
397 if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
398 else adns__debug(ads,-1,0,"environment variable %s set to `%s'",envvar,value);
402 static void readconfig(adns_state ads, const char *filename, int warnmissing) {
405 gl_ctx.file= fopen(filename,"r");
407 if (errno == ENOENT) {
409 adns__debug(ads,-1,0,"configuration file `%s' does not exist",filename);
413 adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
414 filename,strerror(errno));
418 readconfiggeneric(ads,filename,gl_file,gl_ctx);
423 static void readconfigtext(adns_state ads, const char *text, const char *showname) {
427 readconfiggeneric(ads,showname,gl_text,gl_ctx);
430 static void readconfigenv(adns_state ads, const char *envvar) {
431 const char *filename;
433 if (ads->iflags & adns_if_noenv) {
434 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
437 filename= instrum_getenv(ads,envvar);
438 if (filename) readconfig(ads,filename,1);
441 static void readconfigenvtext(adns_state ads, const char *envvar) {
442 const char *textdata;
444 if (ads->iflags & adns_if_noenv) {
445 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
448 textdata= instrum_getenv(ads,envvar);
449 if (textdata) readconfigtext(ads,textdata,envvar);
453 int adns__setnonblock(adns_state ads, int fd) {
456 r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
458 r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
462 static int init_begin(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
465 ads= malloc(sizeof(*ads)); if (!ads) return errno;
468 ads->diagfile= diagfile;
470 LIST_INIT(ads->udpw);
471 LIST_INIT(ads->tcpw);
472 LIST_INIT(ads->childw);
473 LIST_INIT(ads->output);
476 ads->udpsocket= ads->tcpsocket= -1;
477 ads->bug_if_query_done_now= 0;
478 adns__vbuf_init(&ads->tcpsend);
479 adns__vbuf_init(&ads->tcprecv);
480 ads->tcprecv_skip= 0;
481 ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
483 ads->tcpstate= server_disconnected;
484 timerclear(&ads->tcptimeout);
491 static int init_finish(adns_state ads) {
493 struct protoent *proto;
496 if (!ads->nservers) {
497 if (ads->diagfile && ads->iflags & adns_if_debug)
498 fprintf(ads->diagfile,"adns: no nameservers, using localhost\n");
499 ia.s_addr= htonl(INADDR_LOOPBACK);
503 proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
504 ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
505 if (ads->udpsocket<0) { r= errno; goto x_free; }
507 r= adns__setnonblock(ads,ads->udpsocket);
508 if (r) { r= errno; goto x_closeudp; }
513 close(ads->udpsocket);
519 static void init_abort(adns_state ads) {
520 if (ads->nsearchlist) {
521 free(ads->searchlist[0]);
522 free(ads->searchlist);
527 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
529 const char *res_options, *adns_res_options;
532 r= init_begin(&ads, flags, diagfile ? diagfile : stderr);
535 res_options= instrum_getenv(ads,"RES_OPTIONS");
536 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
537 ccf_options(ads,"RES_OPTIONS",-1,res_options);
538 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
540 readconfig(ads,"/etc/resolv.conf",1);
541 readconfig(ads,"/etc/resolv-adns.conf",0);
542 readconfigenv(ads,"RES_CONF");
543 readconfigenv(ads,"ADNS_RES_CONF");
545 readconfigenvtext(ads,"RES_CONF_TEXT");
546 readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
548 ccf_options(ads,"RES_OPTIONS",-1,res_options);
549 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
551 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
552 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
554 if (ads->configerrno && ads->configerrno != EINVAL) {
563 adns__consistency(ads,0,cc_entex);
568 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
569 FILE *diagfile, const char *configtext) {
573 r= init_begin(&ads, flags, diagfile); if (r) return r;
575 readconfigtext(ads,configtext,"<supplied configuration text>");
576 if (ads->configerrno) {
582 r= init_finish(ads); if (r) return r;
583 adns__consistency(ads,0,cc_entex);
589 void adns_finish(adns_state ads) {
590 adns__consistency(ads,0,cc_entex);
592 if (ads->udpw.head) adns_cancel(ads->udpw.head);
593 else if (ads->tcpw.head) adns_cancel(ads->tcpw.head);
594 else if (ads->childw.head) adns_cancel(ads->childw.head);
595 else if (ads->output.head) adns_cancel(ads->output.head);
598 close(ads->udpsocket);
599 if (ads->tcpsocket >= 0) close(ads->tcpsocket);
600 adns__vbuf_free(&ads->tcpsend);
601 adns__vbuf_free(&ads->tcprecv);
606 void adns_forallqueries_begin(adns_state ads) {
607 adns__consistency(ads,0,cc_entex);
609 ads->udpw.head ? ads->udpw.head :
610 ads->tcpw.head ? ads->tcpw.head :
611 ads->childw.head ? ads->childw.head :
615 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
618 adns__consistency(ads,0,cc_entex);
619 nqu= ads->forallnext;
625 } else if (qu == ads->udpw.tail) {
627 ads->tcpw.head ? ads->tcpw.head :
628 ads->childw.head ? ads->childw.head :
630 } else if (qu == ads->tcpw.tail) {
632 ads->childw.head ? ads->childw.head :
634 } else if (qu == ads->childw.tail) {
635 nqu= ads->output.head;
639 if (!qu->parent) break;
641 ads->forallnext= nqu;
642 if (context_r) *context_r= qu->ctx.ext;