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);
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 saveerr(adns_state ads, int en) {
66 if (!ads->configerrno) ads->configerrno= en;
69 static void configparseerr(adns_state ads, const char *fn, int lno,
70 const char *fmt, ...) {
74 if (!ads->diagfile || (ads->iflags & adns_if_noerrprint)) return;
76 if (lno==-1) fprintf(ads->diagfile,"adns: %s: ",fn);
77 else fprintf(ads->diagfile,"adns: %s:%d: ",fn,lno);
79 vfprintf(ads->diagfile,fmt,al);
81 fputc('\n',ads->diagfile);
84 static int nextword(const char **bufp_io, const char **word_r, int *l_r) {
88 while (ctype_whitespace(*p)) p++;
92 while (*q && !ctype_whitespace(*q)) q++;
101 static void ccf_nameserver(adns_state ads, const char *fn, int lno, const char *buf) {
104 if (!inet_aton(buf,&ia)) {
105 configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
108 adns__debug(ads,-1,0,"using nameserver %s",inet_ntoa(ia));
112 static void ccf_search(adns_state ads, const char *fn, int lno, const char *buf) {
113 const char *bufp, *word;
114 char *newchars, **newptrs, **pp;
122 while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
124 newptrs= malloc(sizeof(char*)*count); if (!newptrs) { saveerr(ads,errno); return; }
125 newchars= malloc(tl); if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
129 while (nextword(&bufp,&word,&l)) {
131 memcpy(newchars,word,l);
136 free(ads->searchlist);
137 ads->nsearchlist= count;
138 ads->searchlist= newptrs;
141 static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *buf) {
143 char tbuf[200], *slash, *ep;
144 struct in_addr base, mask;
146 unsigned long initial, baselocal;
151 while (nextword(&buf,&word,&l)) {
152 if (ads->nsortlist >= MAXSORTLIST) {
153 adns__diag(ads,-1,0,"too many sortlist entries, ignoring %.*s onwards",l,word);
157 if (l >= sizeof(tbuf)) {
158 configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
162 memcpy(tbuf,word,l); tbuf[l]= 0;
163 slash= strchr(tbuf,'/');
164 if (slash) *slash++= 0;
166 if (!inet_aton(tbuf,&base)) {
167 configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
172 if (strchr(slash,'.')) {
173 if (!inet_aton(slash,&mask)) {
174 configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
177 if (base.s_addr & ~mask.s_addr) {
178 configparseerr(ads,fn,lno,
179 "mask `%s' in sortlist overlaps address `%s'",slash,tbuf);
183 initial= strtoul(slash,&ep,10);
184 if (*ep || initial>32) {
185 configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
188 mask.s_addr= htonl((0x0ffffffffUL) << (32-initial));
191 baselocal= ntohl(base.s_addr);
192 if (!baselocal & 0x080000000UL) /* class A */
193 mask.s_addr= htonl(0x0ff000000UL);
194 else if ((baselocal & 0x0c0000000UL) == 0x080000000UL)
195 mask.s_addr= htonl(0x0ffff0000UL); /* class B */
196 else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL)
197 mask.s_addr= htonl(0x0ff000000UL); /* class C */
199 configparseerr(ads,fn,lno,
200 "network address `%s' in sortlist is not in classed ranges,"
201 " must specify mask explicitly", tbuf);
206 ads->sortlist[ads->nsortlist].base= base;
207 ads->sortlist[ads->nsortlist].mask= mask;
212 static void ccf_options(adns_state ads, const char *fn, int lno, const char *buf) {
220 while (nextword(&buf,&word,&l)) {
221 if (l==5 && !memcmp(word,"debug",5)) {
222 ads->iflags |= adns_if_debug;
225 if (l>=6 && !memcmp(word,"ndots:",6)) {
226 v= strtoul(word+6,&ep,10);
227 if (l==6 || ep != word+l || v > INT_MAX) {
228 configparseerr(ads,fn,lno,"option `%.*s' malformed or has bad value",l,word);
234 if (l>=12 && !memcmp(word,"adns_checkc:",12)) {
235 if (!strcmp(word+12,"none")) {
236 ads->iflags &= ~adns_if_checkc_freq;
237 ads->iflags |= adns_if_checkc_entex;
238 } else if (!strcmp(word+12,"entex")) {
239 ads->iflags &= ~adns_if_checkc_freq;
240 ads->iflags |= adns_if_checkc_entex;
241 } else if (!strcmp(word+12,"freq")) {
242 ads->iflags |= adns_if_checkc_freq;
244 configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' "
245 "(must be none, entex or freq", word+12);
249 adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
253 static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) {
257 static void ccf_include(adns_state ads, const char *fn, int lno, const char *buf) {
259 configparseerr(ads,fn,lno,"`include' directive with no filename");
265 static const struct configcommandinfo {
267 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
268 } configcommandinfos[]= {
269 { "nameserver", ccf_nameserver },
270 { "domain", ccf_search },
271 { "search", ccf_search },
272 { "sortlist", ccf_sortlist },
273 { "options", ccf_options },
274 { "clearnameservers", ccf_clearnss },
275 { "include", ccf_include },
284 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
285 int lno, char *buf, int buflen) {
286 FILE *file= src_io->file;
294 for (;;) { /* loop over chars */
296 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
301 adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
303 } else if (c == '\n') {
305 } else if (c == EOF) {
308 adns__diag(ads,-1,0,"%s:%d: read error: %s",filename,lno,strerror(errno));
324 while ((c= getc(file)) != EOF && c != '\n');
328 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
329 int lno, char *buf, int buflen) {
330 const char *cp= src_io->text;
333 if (!cp || !*cp) return -1;
335 if (*cp == ';' || *cp == '\n') cp++;
336 l= strcspn(cp,";\n");
340 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
350 static void readconfiggeneric(adns_state ads, const char *filename,
351 int (*getline)(adns_state ads, getline_ctx*,
352 const char *filename, int lno,
353 char *buf, int buflen),
354 /* Returns >=0 for success, -1 for EOF or error
355 * (error will have been reported), or -2 for
356 * bad line was encountered, try again.
358 getline_ctx gl_ctx) {
359 char linebuf[2000], *p, *q;
361 const struct configcommandinfo *ccip;
364 (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
366 if (l == -2) continue;
367 while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
370 while (ctype_whitespace(*p)) p++;
371 if (*p == '#' || !*p) continue;
373 while (*q && !ctype_whitespace(*q)) q++;
375 for (ccip=configcommandinfos;
376 ccip->name && !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
379 adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
383 while (ctype_whitespace(*q)) q++;
384 ccip->fn(ads,filename,lno,q);
388 static const char *instrum_getenv(adns_state ads, const char *envvar) {
391 value= getenv(envvar);
392 if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
393 else adns__debug(ads,-1,0,"environment variable %s set to `%s'",envvar,value);
397 static void readconfig(adns_state ads, const char *filename) {
400 gl_ctx.file= fopen(filename,"r");
402 if (errno == ENOENT) {
403 adns__debug(ads,-1,0,"configuration file `%s' does not exist",filename);
407 adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
408 filename,strerror(errno));
412 readconfiggeneric(ads,filename,gl_file,gl_ctx);
417 static void readconfigtext(adns_state ads, const char *text, const char *showname) {
421 readconfiggeneric(ads,showname,gl_text,gl_ctx);
424 static void readconfigenv(adns_state ads, const char *envvar) {
425 const char *filename;
427 if (ads->iflags & adns_if_noenv) {
428 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
431 filename= instrum_getenv(ads,envvar);
432 if (filename) readconfig(ads,filename);
435 static void readconfigenvtext(adns_state ads, const char *envvar) {
436 const char *textdata;
438 if (ads->iflags & adns_if_noenv) {
439 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
442 textdata= instrum_getenv(ads,envvar);
443 if (textdata) readconfigtext(ads,textdata,envvar);
447 int adns__setnonblock(adns_state ads, int fd) {
450 r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
452 r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
456 static int init_begin(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
459 ads= malloc(sizeof(*ads)); if (!ads) return errno;
462 ads->diagfile= diagfile;
464 LIST_INIT(ads->timew);
465 LIST_INIT(ads->childw);
466 LIST_INIT(ads->output);
469 ads->udpsocket= ads->tcpsocket= -1;
470 adns__vbuf_init(&ads->tcpsend);
471 adns__vbuf_init(&ads->tcprecv);
472 ads->tcprecv_skip= 0;
473 ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
475 ads->tcpstate= server_disconnected;
476 timerclear(&ads->tcptimeout);
483 static int init_finish(adns_state ads) {
485 struct protoent *proto;
488 if (!ads->nservers) {
489 if (ads->diagfile && ads->iflags & adns_if_debug)
490 fprintf(ads->diagfile,"adns: no nameservers, using localhost\n");
491 ia.s_addr= htonl(INADDR_LOOPBACK);
495 proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
496 ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
497 if (ads->udpsocket<0) { r= errno; goto x_free; }
499 r= adns__setnonblock(ads,ads->udpsocket);
500 if (r) { r= errno; goto x_closeudp; }
505 close(ads->udpsocket);
511 static void init_abort(adns_state ads) {
512 if (ads->nsearchlist) {
513 free(ads->searchlist[0]);
514 free(ads->searchlist);
519 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
521 const char *res_options, *adns_res_options;
524 r= init_begin(&ads, flags, diagfile ? diagfile : stderr);
527 res_options= instrum_getenv(ads,"RES_OPTIONS");
528 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
529 ccf_options(ads,"RES_OPTIONS",-1,res_options);
530 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
532 readconfig(ads,"/etc/resolv.conf");
533 readconfigenv(ads,"RES_CONF");
534 readconfigenv(ads,"ADNS_RES_CONF");
536 readconfigenvtext(ads,"RES_CONF_TEXT");
537 readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
539 ccf_options(ads,"RES_OPTIONS",-1,res_options);
540 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
542 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
543 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
545 if (ads->configerrno && ads->configerrno != EINVAL) {
554 adns__consistency(ads,0,cc_entex);
559 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
560 FILE *diagfile, const char *configtext) {
564 r= init_begin(&ads, flags, diagfile); if (r) return r;
566 readconfigtext(ads,configtext,"<supplied configuration text>");
567 if (ads->configerrno) {
573 r= init_finish(ads); if (r) return r;
574 adns__consistency(ads,0,cc_entex);
580 void adns_finish(adns_state ads) {
581 adns__consistency(ads,0,cc_entex);
583 if (ads->timew.head) adns_cancel(ads->timew.head);
584 else if (ads->childw.head) adns_cancel(ads->childw.head);
585 else if (ads->output.head) adns_cancel(ads->output.head);
588 close(ads->udpsocket);
589 if (ads->tcpsocket >= 0) close(ads->tcpsocket);
590 adns__vbuf_free(&ads->tcpsend);
591 adns__vbuf_free(&ads->tcprecv);
595 void adns_forallqueries_begin(adns_state ads) {
596 adns__consistency(ads,0,cc_entex);
598 ads->timew.head ? ads->timew.head :
599 ads->childw.head ? ads->childw.head :
603 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
606 adns__consistency(ads,0,cc_entex);
607 nqu= ads->forallnext;
613 } else if (qu == ads->timew.tail) {
614 if (ads->childw.head) {
615 nqu= ads->childw.head;
617 nqu= ads->output.head;
619 } else if (qu == ads->childw.tail) {
620 nqu= ads->output.head;
624 if (!qu->parent) break;
626 ads->forallnext= nqu;
627 if (context_r) *context_r= qu->ctx.ext;
631 void adns__checkqueues(adns_state ads) {
632 adns_forallqueries_begin(ads);
633 while (adns_forallqueries_next(ads,0));