3 * - configuration file parsing
4 * - management of global state
7 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
37 static void readconfig(adns_state ads, const char *filename);
39 static void addserver(adns_state ads, struct in_addr addr) {
43 for (i=0; i<ads->nservers; i++) {
44 if (ads->servers[i].addr.s_addr == addr.s_addr) {
45 adns__debug(ads,-1,0,"duplicate nameserver %s ignored",inet_ntoa(addr));
50 if (ads->nservers>=MAXSERVERS) {
51 adns__diag(ads,-1,0,"too many nameservers, ignoring %s",inet_ntoa(addr));
55 ss= ads->servers+ads->nservers;
60 static void saveerr(adns_state ads, int en) {
61 if (!ads->configerrno) ads->configerrno= en;
64 static void configparseerr(adns_state ads, const char *fn, int lno,
65 const char *fmt, ...) {
69 if (!ads->diagfile || (ads->iflags & adns_if_noerrprint)) return;
71 if (lno==-1) fprintf(ads->diagfile,"adns: %s: ",fn);
72 else fprintf(ads->diagfile,"adns: %s:%d: ",fn,lno);
74 vfprintf(ads->diagfile,fmt,al);
76 fputc('\n',ads->diagfile);
79 static int nextword(const char **bufp_io, const char **word_r, int *l_r) {
83 while (ctype_whitespace(*p)) p++;
87 while (*q && !ctype_whitespace(*q)) q++;
96 static void ccf_nameserver(adns_state ads, const char *fn, int lno, const char *buf) {
99 if (!inet_aton(buf,&ia)) {
100 configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
103 adns__debug(ads,-1,0,"using nameserver %s",inet_ntoa(ia));
107 static void ccf_search(adns_state ads, const char *fn, int lno, const char *buf) {
108 const char *bufp, *word;
109 char *newchars, **newptrs, **pp;
117 while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
119 newptrs= malloc(sizeof(char*)*count); if (!newptrs) { saveerr(ads,errno); return; }
120 newchars= malloc(tl); if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
124 while (nextword(&bufp,&word,&l)) {
126 memcpy(newchars,word,l);
131 free(ads->searchlist);
132 ads->nsearchlist= count;
133 ads->searchlist= newptrs;
136 static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *buf) {
138 char tbuf[200], *slash, *ep;
139 struct in_addr base, mask;
141 unsigned long initial, baselocal;
146 while (nextword(&buf,&word,&l)) {
147 if (ads->nsortlist >= MAXSORTLIST) {
148 adns__diag(ads,-1,0,"too many sortlist entries, ignoring %.*s onwards",l,word);
152 if (l >= sizeof(tbuf)) {
153 configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
157 memcpy(tbuf,word,l); tbuf[l]= 0;
158 slash= strchr(tbuf,'/');
159 if (slash) *slash++= 0;
161 if (!inet_aton(tbuf,&base)) {
162 configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
167 if (strchr(slash,'.')) {
168 if (!inet_aton(slash,&mask)) {
169 configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
172 if (base.s_addr & ~mask.s_addr) {
173 configparseerr(ads,fn,lno,
174 "mask `%s' in sortlist overlaps address `%s'",slash,tbuf);
178 initial= strtoul(slash,&ep,10);
179 if (*ep || initial>32) {
180 configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
183 mask.s_addr= htonl((0x0ffffffffUL) << (32-initial));
186 baselocal= ntohl(base.s_addr);
187 if (!baselocal & 0x080000000UL) /* class A */
188 mask.s_addr= htonl(0x0ff000000UL);
189 else if ((baselocal & 0x0c0000000UL) == 0x080000000UL)
190 mask.s_addr= htonl(0x0ffff0000UL); /* class B */
191 else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL)
192 mask.s_addr= htonl(0x0ff000000UL); /* class C */
194 configparseerr(ads,fn,lno,
195 "network address `%s' in sortlist is not in classed ranges,"
196 " must specify mask explicitly", tbuf);
201 ads->sortlist[ads->nsortlist].base= base;
202 ads->sortlist[ads->nsortlist].mask= mask;
207 static void ccf_options(adns_state ads, const char *fn, int lno, const char *buf) {
215 while (nextword(&buf,&word,&l)) {
216 if (l==5 && !memcmp(word,"debug",5)) {
217 ads->iflags |= adns_if_debug;
220 if (l>=6 && !memcmp(word,"ndots:",6)) {
221 v= strtoul(word+6,&ep,10);
222 if (l==6 || ep != word+l || v > INT_MAX) {
223 configparseerr(ads,fn,lno,"option `%.*s' malformed or has bad value",l,word);
229 adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
233 static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) {
237 static void ccf_include(adns_state ads, const char *fn, int lno, const char *buf) {
239 configparseerr(ads,fn,lno,"`include' directive with no filename");
245 static const struct configcommandinfo {
247 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
248 } configcommandinfos[]= {
249 { "nameserver", ccf_nameserver },
250 { "domain", ccf_search },
251 { "search", ccf_search },
252 { "sortlist", ccf_sortlist },
253 { "options", ccf_options },
254 { "clearnameservers", ccf_clearnss },
255 { "include", ccf_include },
264 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
265 int lno, char *buf, int buflen) {
266 FILE *file= src_io->file;
274 for (;;) { /* loop over chars */
276 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
281 adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
283 } else if (c == '\n') {
285 } else if (c == EOF) {
288 adns__diag(ads,-1,0,"%s:%d: read error: %s",filename,lno,strerror(errno));
304 while ((c= getc(file)) != EOF && c != '\n');
308 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
309 int lno, char *buf, int buflen) {
310 const char *cp= src_io->text;
313 if (!cp || !*cp) return -1;
315 if (*cp == ';' || *cp == '\n') cp++;
316 l= strcspn(cp,";\n");
320 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
330 static void readconfiggeneric(adns_state ads, const char *filename,
331 int (*getline)(adns_state ads, getline_ctx*,
332 const char *filename, int lno,
333 char *buf, int buflen),
334 /* Returns >=0 for success, -1 for EOF or error
335 * (error will have been reported), or -2 for
336 * bad line was encountered, try again.
338 getline_ctx gl_ctx) {
339 char linebuf[2000], *p, *q;
341 const struct configcommandinfo *ccip;
344 (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
346 if (l == -2) continue;
347 while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
350 while (ctype_whitespace(*p)) p++;
351 if (*p == '#' || !*p) continue;
353 while (*q && !ctype_whitespace(*q)) q++;
355 for (ccip=configcommandinfos;
356 ccip->name && !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
359 adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
363 while (ctype_whitespace(*q)) q++;
364 ccip->fn(ads,filename,lno,q);
368 static const char *instrum_getenv(adns_state ads, const char *envvar) {
371 value= getenv(envvar);
372 if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
373 else adns__debug(ads,-1,0,"environment variable %s set to `%s'",envvar,value);
377 static void readconfig(adns_state ads, const char *filename) {
380 gl_ctx.file= fopen(filename,"r");
382 if (errno == ENOENT) {
383 adns__debug(ads,-1,0,"configuration file `%s' does not exist",filename);
387 adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
388 filename,strerror(errno));
392 readconfiggeneric(ads,filename,gl_file,gl_ctx);
397 static void readconfigtext(adns_state ads, const char *text, const char *showname) {
401 readconfiggeneric(ads,showname,gl_text,gl_ctx);
404 static void readconfigenv(adns_state ads, const char *envvar) {
405 const char *filename;
407 if (ads->iflags & adns_if_noenv) {
408 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
411 filename= instrum_getenv(ads,envvar);
412 if (filename) readconfig(ads,filename);
415 static void readconfigenvtext(adns_state ads, const char *envvar) {
416 const char *textdata;
418 if (ads->iflags & adns_if_noenv) {
419 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
422 textdata= instrum_getenv(ads,envvar);
423 if (textdata) readconfigtext(ads,textdata,envvar);
427 int adns__setnonblock(adns_state ads, int fd) {
430 r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
432 r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
436 static int init_begin(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
439 ads= malloc(sizeof(*ads)); if (!ads) return errno;
442 ads->diagfile= diagfile;
444 LIST_INIT(ads->timew);
445 LIST_INIT(ads->childw);
446 LIST_INIT(ads->output);
449 ads->udpsocket= ads->tcpsocket= -1;
450 adns__vbuf_init(&ads->tcpsend);
451 adns__vbuf_init(&ads->tcprecv);
452 ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
454 ads->tcpstate= server_disconnected;
455 timerclear(&ads->tcptimeout);
462 static int init_finish(adns_state ads) {
464 struct protoent *proto;
467 if (!ads->nservers) {
468 if (ads->diagfile && ads->iflags & adns_if_debug)
469 fprintf(ads->diagfile,"adns: no nameservers, using localhost\n");
470 ia.s_addr= htonl(INADDR_LOOPBACK);
474 proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
475 ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
476 if (ads->udpsocket<0) { r= errno; goto x_free; }
478 r= adns__setnonblock(ads,ads->udpsocket);
479 if (r) { r= errno; goto x_closeudp; }
484 close(ads->udpsocket);
490 static void init_abort(adns_state ads) {
491 if (ads->nsearchlist) {
492 free(ads->searchlist[0]);
493 free(ads->searchlist);
498 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
500 const char *res_options, *adns_res_options;
503 r= init_begin(&ads, flags, diagfile ? diagfile : stderr);
506 res_options= instrum_getenv(ads,"RES_OPTIONS");
507 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
508 ccf_options(ads,"RES_OPTIONS",-1,res_options);
509 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
511 readconfig(ads,"/etc/resolv.conf");
512 readconfigenv(ads,"RES_CONF");
513 readconfigenv(ads,"ADNS_RES_CONF");
515 readconfigenvtext(ads,"RES_CONF_TEXT");
516 readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
518 ccf_options(ads,"RES_OPTIONS",-1,res_options);
519 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
521 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
522 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
524 if (ads->configerrno && ads->configerrno != EINVAL) {
537 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
538 FILE *diagfile, const char *configtext) {
542 r= init_begin(&ads, flags, diagfile); if (r) return r;
544 readconfigtext(ads,configtext,"<supplied configuration text>");
545 if (ads->configerrno) {
551 r= init_finish(ads); if (r) return r;
556 void adns_finish(adns_state ads) {
558 if (ads->timew.head) adns_cancel(ads->timew.head);
559 else if (ads->childw.head) adns_cancel(ads->childw.head);
560 else if (ads->output.head) adns_cancel(ads->output.head);
563 close(ads->udpsocket);
564 if (ads->tcpsocket >= 0) close(ads->tcpsocket);
565 adns__vbuf_free(&ads->tcpsend);
566 adns__vbuf_free(&ads->tcprecv);
570 void adns_forallqueries_begin(adns_state ads) {
572 ads->timew.head ? ads->timew.head :
573 ads->childw.head ? ads->childw.head :
577 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
580 nqu= ads->forallnext;
586 } else if (qu == ads->timew.tail) {
587 if (ads->childw.head) {
588 nqu= ads->childw.head;
590 nqu= ads->output.head;
592 } else if (qu == ads->childw.tail) {
593 nqu= ads->output.head;
597 if (!qu->parent) break;
599 ads->forallnext= nqu;
600 if (context_r) *context_r= qu->ctx.ext;
604 void adns__checkqueues(adns_state ads) {
605 adns_forallqueries_begin(ads);
606 while (adns_forallqueries_next(ads,0));