| 1 | /* |
| 2 | * setup.c |
| 3 | * - configuration file parsing |
| 4 | * - management of global state |
| 5 | */ |
| 6 | /* |
| 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.) |
| 12 | * |
| 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) |
| 16 | * any later version. |
| 17 | * |
| 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. |
| 22 | * |
| 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. |
| 26 | */ |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <errno.h> |
| 30 | #include <limits.h> |
| 31 | #include <unistd.h> |
| 32 | #include <fcntl.h> |
| 33 | |
| 34 | #include <sys/types.h> |
| 35 | #include <netdb.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <netinet/in.h> |
| 38 | #include <arpa/inet.h> |
| 39 | |
| 40 | #include "internal.h" |
| 41 | |
| 42 | static void readconfig(adns_state ads, const char *filename, int warnmissing); |
| 43 | |
| 44 | static void addserver(adns_state ads, const struct sockaddr *sa, int n) { |
| 45 | int i; |
| 46 | adns_rr_addr *ss; |
| 47 | const struct sockaddr_in *sin; |
| 48 | |
| 49 | assert(sa->sa_family==AF_INET); assert(n>=sizeof(*sin)); |
| 50 | sin= (const void *)sa; |
| 51 | |
| 52 | for (i=0; i<ads->nservers; i++) { |
| 53 | assert(ads->servers[i].addr.sa.sa_family==AF_INET); |
| 54 | if (ads->servers[i].addr.inet.sin_addr.s_addr == sin->sin_addr.s_addr) { |
| 55 | adns__debug(ads,-1,0,"duplicate nameserver %s ignored", |
| 56 | inet_ntoa(sin->sin_addr)); |
| 57 | return; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (ads->nservers>=MAXSERVERS) { |
| 62 | adns__diag(ads,-1,0,"too many nameservers, ignoring %s", |
| 63 | inet_ntoa(sin->sin_addr)); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | ss= ads->servers+ads->nservers; |
| 68 | assert(n <= sizeof(ss->addr)); |
| 69 | ss->len = n; |
| 70 | memcpy(&ss->addr, sa, n); |
| 71 | ads->nservers++; |
| 72 | } |
| 73 | |
| 74 | static void freesearchlist(adns_state ads) { |
| 75 | if (ads->nsearchlist) free(*ads->searchlist); |
| 76 | free(ads->searchlist); |
| 77 | } |
| 78 | |
| 79 | static void saveerr(adns_state ads, int en) { |
| 80 | if (!ads->configerrno) ads->configerrno= en; |
| 81 | } |
| 82 | |
| 83 | static void configparseerr(adns_state ads, const char *fn, int lno, |
| 84 | const char *fmt, ...) { |
| 85 | va_list al; |
| 86 | |
| 87 | saveerr(ads,EINVAL); |
| 88 | if (!ads->logfn || (ads->iflags & adns_if_noerrprint)) return; |
| 89 | |
| 90 | if (lno==-1) adns__lprintf(ads,"adns: %s: ",fn); |
| 91 | else adns__lprintf(ads,"adns: %s:%d: ",fn,lno); |
| 92 | va_start(al,fmt); |
| 93 | adns__vlprintf(ads,fmt,al); |
| 94 | va_end(al); |
| 95 | adns__lprintf(ads,"\n"); |
| 96 | } |
| 97 | |
| 98 | static int nextword(const char **bufp_io, const char **word_r, int *l_r) { |
| 99 | const char *p, *q; |
| 100 | |
| 101 | p= *bufp_io; |
| 102 | while (ctype_whitespace(*p)) p++; |
| 103 | if (!*p) return 0; |
| 104 | |
| 105 | q= p; |
| 106 | while (*q && !ctype_whitespace(*q)) q++; |
| 107 | |
| 108 | *l_r= q-p; |
| 109 | *word_r= p; |
| 110 | *bufp_io= q; |
| 111 | |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | static void ccf_nameserver(adns_state ads, const char *fn, |
| 116 | int lno, const char *buf) { |
| 117 | struct sockaddr_in sin; |
| 118 | |
| 119 | memset(&sin,0,sizeof(sin)); |
| 120 | sin.sin_family= AF_INET; |
| 121 | sin.sin_port= htons(DNS_PORT); |
| 122 | if (!inet_aton(buf,&sin.sin_addr)) { |
| 123 | configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf); |
| 124 | return; |
| 125 | } |
| 126 | adns__debug(ads,-1,0,"using nameserver %s",inet_ntoa(sin.sin_addr)); |
| 127 | addserver(ads,(const struct sockaddr *)&sin,sizeof(sin)); |
| 128 | } |
| 129 | |
| 130 | static void ccf_search(adns_state ads, const char *fn, |
| 131 | int lno, const char *buf) { |
| 132 | const char *bufp, *word; |
| 133 | char *newchars, **newptrs, **pp; |
| 134 | int count, tl, l; |
| 135 | |
| 136 | if (!buf) return; |
| 137 | |
| 138 | bufp= buf; |
| 139 | count= 0; |
| 140 | tl= 0; |
| 141 | while (nextword(&bufp,&word,&l)) { count++; tl += l+1; } |
| 142 | |
| 143 | newptrs= malloc(sizeof(char*)*count); |
| 144 | if (!newptrs) { saveerr(ads,errno); return; } |
| 145 | |
| 146 | newchars= malloc(tl); |
| 147 | if (!newchars) { saveerr(ads,errno); free(newptrs); return; } |
| 148 | |
| 149 | bufp= buf; |
| 150 | pp= newptrs; |
| 151 | while (nextword(&bufp,&word,&l)) { |
| 152 | *pp++= newchars; |
| 153 | memcpy(newchars,word,l); |
| 154 | newchars += l; |
| 155 | *newchars++ = 0; |
| 156 | } |
| 157 | |
| 158 | freesearchlist(ads); |
| 159 | ads->nsearchlist= count; |
| 160 | ads->searchlist= newptrs; |
| 161 | } |
| 162 | |
| 163 | static void ccf_sortlist(adns_state ads, const char *fn, |
| 164 | int lno, const char *buf) { |
| 165 | const char *word; |
| 166 | char tbuf[200], *slash, *ep; |
| 167 | const char *maskwhat; |
| 168 | struct in_addr base, mask; |
| 169 | int l; |
| 170 | unsigned long initial, baselocal; |
| 171 | |
| 172 | if (!buf) return; |
| 173 | |
| 174 | ads->nsortlist= 0; |
| 175 | while (nextword(&buf,&word,&l)) { |
| 176 | if (ads->nsortlist >= MAXSORTLIST) { |
| 177 | adns__diag(ads,-1,0,"too many sortlist entries," |
| 178 | " ignoring %.*s onwards",l,word); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | if (l >= sizeof(tbuf)) { |
| 183 | configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word); |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | memcpy(tbuf,word,l); tbuf[l]= 0; |
| 188 | slash= strchr(tbuf,'/'); |
| 189 | if (slash) *slash++= 0; |
| 190 | |
| 191 | if (!inet_aton(tbuf,&base)) { |
| 192 | configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf); |
| 193 | continue; |
| 194 | } |
| 195 | |
| 196 | if (slash) { |
| 197 | if (strchr(slash,'.')) { |
| 198 | maskwhat = "mask"; |
| 199 | if (!inet_aton(slash,&mask)) { |
| 200 | configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash); |
| 201 | continue; |
| 202 | } |
| 203 | } else { |
| 204 | maskwhat = "prefix length"; |
| 205 | initial= strtoul(slash,&ep,10); |
| 206 | if (*ep || initial>32) { |
| 207 | configparseerr(ads,fn,lno,"mask length `%s' invalid",slash); |
| 208 | continue; |
| 209 | } |
| 210 | mask.s_addr= htonl((0x0ffffffffUL) << (32-initial)); |
| 211 | } |
| 212 | } else { |
| 213 | maskwhat = "implied mask"; |
| 214 | baselocal= ntohl(base.s_addr); |
| 215 | if (!(baselocal & 0x080000000UL)) /* class A */ |
| 216 | mask.s_addr= htonl(0x0ff000000UL); |
| 217 | else if ((baselocal & 0x0c0000000UL) == 0x080000000UL) |
| 218 | mask.s_addr= htonl(0x0ffff0000UL); /* class B */ |
| 219 | else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL) |
| 220 | mask.s_addr= htonl(0x0ff000000UL); /* class C */ |
| 221 | else { |
| 222 | configparseerr(ads,fn,lno, "network address `%s'" |
| 223 | " in sortlist is not in classed ranges," |
| 224 | " must specify mask explicitly", tbuf); |
| 225 | continue; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (base.s_addr & ~mask.s_addr) { |
| 230 | configparseerr(ads,fn,lno, "%s `%s' in sortlist" |
| 231 | " overlaps address `%s'",maskwhat, |
| 232 | slash ? slash : inet_ntoa(mask), tbuf); |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | ads->sortlist[ads->nsortlist].af= AF_INET; |
| 237 | ads->sortlist[ads->nsortlist].base.v4= base; |
| 238 | ads->sortlist[ads->nsortlist].mask.v4= mask; |
| 239 | ads->nsortlist++; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static void ccf_options(adns_state ads, const char *fn, |
| 244 | int lno, const char *buf) { |
| 245 | const char *word; |
| 246 | char *ep; |
| 247 | unsigned long v; |
| 248 | int l; |
| 249 | |
| 250 | if (!buf) return; |
| 251 | |
| 252 | while (nextword(&buf,&word,&l)) { |
| 253 | if (l==5 && !memcmp(word,"debug",5)) { |
| 254 | ads->iflags |= adns_if_debug; |
| 255 | continue; |
| 256 | } |
| 257 | if (l>=6 && !memcmp(word,"ndots:",6)) { |
| 258 | v= strtoul(word+6,&ep,10); |
| 259 | if (l==6 || ep != word+l || v > INT_MAX) { |
| 260 | configparseerr(ads,fn,lno,"option `%.*s' malformed" |
| 261 | " or has bad value",l,word); |
| 262 | continue; |
| 263 | } |
| 264 | ads->searchndots= v; |
| 265 | continue; |
| 266 | } |
| 267 | if (l>=12 && !memcmp(word,"adns_checkc:",12)) { |
| 268 | if (!strcmp(word+12,"none")) { |
| 269 | ads->iflags &= ~adns_if_checkc_freq; |
| 270 | ads->iflags |= adns_if_checkc_entex; |
| 271 | } else if (!strcmp(word+12,"entex")) { |
| 272 | ads->iflags &= ~adns_if_checkc_freq; |
| 273 | ads->iflags |= adns_if_checkc_entex; |
| 274 | } else if (!strcmp(word+12,"freq")) { |
| 275 | ads->iflags |= adns_if_checkc_freq; |
| 276 | } else { |
| 277 | configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' " |
| 278 | "(must be none, entex or freq", word+12); |
| 279 | } |
| 280 | continue; |
| 281 | } |
| 282 | adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | static void ccf_clearnss(adns_state ads, const char *fn, |
| 287 | int lno, const char *buf) { |
| 288 | ads->nservers= 0; |
| 289 | } |
| 290 | |
| 291 | static void ccf_include(adns_state ads, const char *fn, |
| 292 | int lno, const char *buf) { |
| 293 | if (!*buf) { |
| 294 | configparseerr(ads,fn,lno,"`include' directive with no filename"); |
| 295 | return; |
| 296 | } |
| 297 | readconfig(ads,buf,1); |
| 298 | } |
| 299 | |
| 300 | static void ccf_lookup(adns_state ads, const char *fn, int lno, |
| 301 | const char *buf) { |
| 302 | int found_bind=0; |
| 303 | const char *word; |
| 304 | int l; |
| 305 | |
| 306 | if (!*buf) { |
| 307 | configparseerr(ads,fn,lno,"`lookup' directive with no databases"); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | while (nextword(&buf,&word,&l)) { |
| 312 | if (l==4 && !memcmp(word,"bind",4)) { |
| 313 | found_bind=1; |
| 314 | } else if (l==4 && !memcmp(word,"file",4)) { |
| 315 | /* ignore this and hope /etc/hosts is not essential */ |
| 316 | } else if (l==2 && !memcmp(word,"yp",2)) { |
| 317 | adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno); |
| 318 | found_bind=-1; |
| 319 | } else { |
| 320 | adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'", |
| 321 | fn,lno, l,word); |
| 322 | found_bind=-1; |
| 323 | } |
| 324 | } |
| 325 | if (!found_bind) |
| 326 | adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno); |
| 327 | } |
| 328 | |
| 329 | static const struct configcommandinfo { |
| 330 | const char *name; |
| 331 | void (*fn)(adns_state ads, const char *fn, int lno, const char *buf); |
| 332 | } configcommandinfos[]= { |
| 333 | { "nameserver", ccf_nameserver }, |
| 334 | { "domain", ccf_search }, |
| 335 | { "search", ccf_search }, |
| 336 | { "sortlist", ccf_sortlist }, |
| 337 | { "options", ccf_options }, |
| 338 | { "clearnameservers", ccf_clearnss }, |
| 339 | { "include", ccf_include }, |
| 340 | { "lookup", ccf_lookup }, /* OpenBSD */ |
| 341 | { 0 } |
| 342 | }; |
| 343 | |
| 344 | typedef union { |
| 345 | FILE *file; |
| 346 | const char *text; |
| 347 | } getline_ctx; |
| 348 | |
| 349 | static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename, |
| 350 | int lno, char *buf, int buflen) { |
| 351 | FILE *file= src_io->file; |
| 352 | int c, i; |
| 353 | char *p; |
| 354 | |
| 355 | p= buf; |
| 356 | buflen--; |
| 357 | i= 0; |
| 358 | |
| 359 | for (;;) { /* loop over chars */ |
| 360 | if (i == buflen) { |
| 361 | adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno); |
| 362 | goto x_badline; |
| 363 | } |
| 364 | c= getc(file); |
| 365 | if (!c) { |
| 366 | adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno); |
| 367 | goto x_badline; |
| 368 | } else if (c == '\n') { |
| 369 | break; |
| 370 | } else if (c == EOF) { |
| 371 | if (ferror(file)) { |
| 372 | saveerr(ads,errno); |
| 373 | adns__diag(ads,-1,0,"%s:%d: read error: %s", |
| 374 | filename,lno,strerror(errno)); |
| 375 | return -1; |
| 376 | } |
| 377 | if (!i) return -1; |
| 378 | break; |
| 379 | } else { |
| 380 | *p++= c; |
| 381 | i++; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | *p++= 0; |
| 386 | return i; |
| 387 | |
| 388 | x_badline: |
| 389 | saveerr(ads,EINVAL); |
| 390 | while ((c= getc(file)) != EOF && c != '\n'); |
| 391 | return -2; |
| 392 | } |
| 393 | |
| 394 | static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename, |
| 395 | int lno, char *buf, int buflen) { |
| 396 | const char *cp= src_io->text; |
| 397 | int l; |
| 398 | |
| 399 | if (!cp || !*cp) return -1; |
| 400 | |
| 401 | if (*cp == ';' || *cp == '\n') cp++; |
| 402 | l= strcspn(cp,";\n"); |
| 403 | src_io->text = cp+l; |
| 404 | |
| 405 | if (l >= buflen) { |
| 406 | adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno); |
| 407 | saveerr(ads,EINVAL); |
| 408 | return -2; |
| 409 | } |
| 410 | |
| 411 | memcpy(buf,cp,l); |
| 412 | buf[l]= 0; |
| 413 | return l; |
| 414 | } |
| 415 | |
| 416 | static void readconfiggeneric(adns_state ads, const char *filename, |
| 417 | int (*getline)(adns_state ads, getline_ctx*, |
| 418 | const char *filename, int lno, |
| 419 | char *buf, int buflen), |
| 420 | /* Returns >=0 for success, -1 for EOF or error |
| 421 | * (error will have been reported), or -2 for |
| 422 | * bad line was encountered, try again. |
| 423 | */ |
| 424 | getline_ctx gl_ctx) { |
| 425 | char linebuf[2000], *p, *q; |
| 426 | int lno, l, dirl; |
| 427 | const struct configcommandinfo *ccip; |
| 428 | |
| 429 | for (lno=1; |
| 430 | (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1; |
| 431 | lno++) { |
| 432 | if (l == -2) continue; |
| 433 | while (l>0 && ctype_whitespace(linebuf[l-1])) l--; |
| 434 | linebuf[l]= 0; |
| 435 | p= linebuf; |
| 436 | while (ctype_whitespace(*p)) p++; |
| 437 | if (*p == '#' || *p == ';' || !*p) continue; |
| 438 | q= p; |
| 439 | while (*q && !ctype_whitespace(*q)) q++; |
| 440 | dirl= q-p; |
| 441 | for (ccip=configcommandinfos; |
| 442 | ccip->name && |
| 443 | !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p)); |
| 444 | ccip++); |
| 445 | if (!ccip->name) { |
| 446 | adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'", |
| 447 | filename,lno,(int)(q-p),p); |
| 448 | continue; |
| 449 | } |
| 450 | while (ctype_whitespace(*q)) q++; |
| 451 | ccip->fn(ads,filename,lno,q); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | static const char *instrum_getenv(adns_state ads, const char *envvar) { |
| 456 | const char *value; |
| 457 | |
| 458 | value= getenv(envvar); |
| 459 | if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar); |
| 460 | else adns__debug(ads,-1,0,"environment variable %s" |
| 461 | " set to `%s'",envvar,value); |
| 462 | return value; |
| 463 | } |
| 464 | |
| 465 | static void readconfig(adns_state ads, const char *filename, int warnmissing) { |
| 466 | getline_ctx gl_ctx; |
| 467 | |
| 468 | gl_ctx.file= fopen(filename,"r"); |
| 469 | if (!gl_ctx.file) { |
| 470 | if (errno == ENOENT) { |
| 471 | if (warnmissing) |
| 472 | adns__debug(ads,-1,0, "configuration file" |
| 473 | " `%s' does not exist",filename); |
| 474 | return; |
| 475 | } |
| 476 | saveerr(ads,errno); |
| 477 | adns__diag(ads,-1,0,"cannot open configuration file `%s': %s", |
| 478 | filename,strerror(errno)); |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | readconfiggeneric(ads,filename,gl_file,gl_ctx); |
| 483 | |
| 484 | fclose(gl_ctx.file); |
| 485 | } |
| 486 | |
| 487 | static void readconfigtext(adns_state ads, const char *text, |
| 488 | const char *showname) { |
| 489 | getline_ctx gl_ctx; |
| 490 | |
| 491 | gl_ctx.text= text; |
| 492 | readconfiggeneric(ads,showname,gl_text,gl_ctx); |
| 493 | } |
| 494 | |
| 495 | static void readconfigenv(adns_state ads, const char *envvar) { |
| 496 | const char *filename; |
| 497 | |
| 498 | if (ads->iflags & adns_if_noenv) { |
| 499 | adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar); |
| 500 | return; |
| 501 | } |
| 502 | filename= instrum_getenv(ads,envvar); |
| 503 | if (filename) readconfig(ads,filename,1); |
| 504 | } |
| 505 | |
| 506 | static void readconfigenvtext(adns_state ads, const char *envvar) { |
| 507 | const char *textdata; |
| 508 | |
| 509 | if (ads->iflags & adns_if_noenv) { |
| 510 | adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar); |
| 511 | return; |
| 512 | } |
| 513 | textdata= instrum_getenv(ads,envvar); |
| 514 | if (textdata) readconfigtext(ads,textdata,envvar); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | int adns__setnonblock(adns_state ads, int fd) { |
| 519 | int r; |
| 520 | |
| 521 | r= fcntl(fd,F_GETFL,0); if (r<0) return errno; |
| 522 | r |= O_NONBLOCK; |
| 523 | r= fcntl(fd,F_SETFL,r); if (r<0) return errno; |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static int init_begin(adns_state *ads_r, adns_initflags flags, |
| 528 | adns_logcallbackfn *logfn, void *logfndata) { |
| 529 | adns_state ads; |
| 530 | pid_t pid; |
| 531 | |
| 532 | ads= malloc(sizeof(*ads)); if (!ads) return errno; |
| 533 | |
| 534 | ads->iflags= flags; |
| 535 | ads->logfn= logfn; |
| 536 | ads->logfndata= logfndata; |
| 537 | ads->configerrno= 0; |
| 538 | LIST_INIT(ads->udpw); |
| 539 | LIST_INIT(ads->tcpw); |
| 540 | LIST_INIT(ads->childw); |
| 541 | LIST_INIT(ads->output); |
| 542 | ads->forallnext= 0; |
| 543 | ads->nextid= 0x311f; |
| 544 | ads->udpsocket= ads->tcpsocket= -1; |
| 545 | adns__vbuf_init(&ads->tcpsend); |
| 546 | adns__vbuf_init(&ads->tcprecv); |
| 547 | ads->tcprecv_skip= 0; |
| 548 | ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0; |
| 549 | ads->searchndots= 1; |
| 550 | ads->tcpstate= server_disconnected; |
| 551 | timerclear(&ads->tcptimeout); |
| 552 | ads->searchlist= 0; |
| 553 | |
| 554 | pid= getpid(); |
| 555 | ads->rand48xsubi[0]= pid; |
| 556 | ads->rand48xsubi[1]= (unsigned long)pid >> 16; |
| 557 | ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16); |
| 558 | |
| 559 | *ads_r= ads; |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int init_finish(adns_state ads) { |
| 564 | struct sockaddr_in sin; |
| 565 | struct protoent *proto; |
| 566 | int i, r; |
| 567 | |
| 568 | if (!ads->nservers) { |
| 569 | if (ads->logfn && ads->iflags & adns_if_debug) |
| 570 | adns__lprintf(ads,"adns: no nameservers, using IPv4 localhost\n"); |
| 571 | memset(&sin, 0, sizeof(sin)); |
| 572 | sin.sin_family = AF_INET; |
| 573 | sin.sin_port = htons(DNS_PORT); |
| 574 | sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 575 | addserver(ads,(struct sockaddr *)&sin, sizeof(sin)); |
| 576 | } |
| 577 | |
| 578 | /* we can't cope with multiple transport address families yet */ |
| 579 | for (i=0; i<ads->nservers; i++) |
| 580 | assert(ads->servers[i].addr.sa.sa_family==AF_INET); |
| 581 | |
| 582 | proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; } |
| 583 | ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto); |
| 584 | if (ads->udpsocket<0) { r= errno; goto x_free; } |
| 585 | |
| 586 | r= adns__setnonblock(ads,ads->udpsocket); |
| 587 | if (r) { r= errno; goto x_closeudp; } |
| 588 | |
| 589 | return 0; |
| 590 | |
| 591 | x_closeudp: |
| 592 | close(ads->udpsocket); |
| 593 | x_free: |
| 594 | free(ads); |
| 595 | return r; |
| 596 | } |
| 597 | |
| 598 | static void init_abort(adns_state ads) { |
| 599 | if (ads->nsearchlist) { |
| 600 | free(ads->searchlist[0]); |
| 601 | free(ads->searchlist); |
| 602 | } |
| 603 | free(ads); |
| 604 | } |
| 605 | |
| 606 | static void logfn_file(adns_state ads, void *logfndata, |
| 607 | const char *fmt, va_list al) { |
| 608 | vfprintf(logfndata,fmt,al); |
| 609 | } |
| 610 | |
| 611 | static int init_files(adns_state *ads_r, adns_initflags flags, |
| 612 | adns_logcallbackfn *logfn, void *logfndata) { |
| 613 | adns_state ads; |
| 614 | const char *res_options, *adns_res_options; |
| 615 | int r; |
| 616 | |
| 617 | r= init_begin(&ads, flags, logfn, logfndata); |
| 618 | if (r) return r; |
| 619 | |
| 620 | res_options= instrum_getenv(ads,"RES_OPTIONS"); |
| 621 | adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS"); |
| 622 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
| 623 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); |
| 624 | |
| 625 | readconfig(ads,"/etc/resolv.conf",1); |
| 626 | readconfig(ads,"/etc/resolv-adns.conf",0); |
| 627 | readconfigenv(ads,"RES_CONF"); |
| 628 | readconfigenv(ads,"ADNS_RES_CONF"); |
| 629 | |
| 630 | readconfigenvtext(ads,"RES_CONF_TEXT"); |
| 631 | readconfigenvtext(ads,"ADNS_RES_CONF_TEXT"); |
| 632 | |
| 633 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
| 634 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); |
| 635 | |
| 636 | ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN")); |
| 637 | ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN")); |
| 638 | |
| 639 | if (ads->configerrno && ads->configerrno != EINVAL) { |
| 640 | r= ads->configerrno; |
| 641 | init_abort(ads); |
| 642 | return r; |
| 643 | } |
| 644 | |
| 645 | r= init_finish(ads); |
| 646 | if (r) return r; |
| 647 | |
| 648 | adns__consistency(ads,0,cc_entex); |
| 649 | *ads_r= ads; |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) { |
| 654 | return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr); |
| 655 | } |
| 656 | |
| 657 | static int init_strcfg(adns_state *ads_r, adns_initflags flags, |
| 658 | adns_logcallbackfn *logfn, void *logfndata, |
| 659 | const char *configtext) { |
| 660 | adns_state ads; |
| 661 | int r; |
| 662 | |
| 663 | r= init_begin(&ads, flags, logfn, logfndata); |
| 664 | if (r) return r; |
| 665 | |
| 666 | readconfigtext(ads,configtext,"<supplied configuration text>"); |
| 667 | if (ads->configerrno) { |
| 668 | r= ads->configerrno; |
| 669 | init_abort(ads); |
| 670 | return r; |
| 671 | } |
| 672 | |
| 673 | r= init_finish(ads); if (r) return r; |
| 674 | adns__consistency(ads,0,cc_entex); |
| 675 | *ads_r= ads; |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | int adns_init_strcfg(adns_state *ads_r, adns_initflags flags, |
| 680 | FILE *diagfile, const char *configtext) { |
| 681 | return init_strcfg(ads_r, flags, |
| 682 | diagfile ? logfn_file : 0, diagfile, |
| 683 | configtext); |
| 684 | } |
| 685 | |
| 686 | int adns_init_logfn(adns_state *newstate_r, adns_initflags flags, |
| 687 | const char *configtext /*0=>use default config files*/, |
| 688 | adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */, |
| 689 | void *logfndata /*0 with logfn==0 => discard*/) { |
| 690 | if (!logfn && logfndata) |
| 691 | logfn= logfn_file; |
| 692 | if (configtext) |
| 693 | return init_strcfg(newstate_r, flags, logfn, logfndata, configtext); |
| 694 | else |
| 695 | return init_files(newstate_r, flags, logfn, logfndata); |
| 696 | } |
| 697 | |
| 698 | void adns_finish(adns_state ads) { |
| 699 | adns__consistency(ads,0,cc_entex); |
| 700 | for (;;) { |
| 701 | if (ads->udpw.head) adns_cancel(ads->udpw.head); |
| 702 | else if (ads->tcpw.head) adns_cancel(ads->tcpw.head); |
| 703 | else if (ads->childw.head) adns_cancel(ads->childw.head); |
| 704 | else if (ads->output.head) adns_cancel(ads->output.head); |
| 705 | else break; |
| 706 | } |
| 707 | close(ads->udpsocket); |
| 708 | if (ads->tcpsocket >= 0) close(ads->tcpsocket); |
| 709 | adns__vbuf_free(&ads->tcpsend); |
| 710 | adns__vbuf_free(&ads->tcprecv); |
| 711 | freesearchlist(ads); |
| 712 | free(ads); |
| 713 | } |
| 714 | |
| 715 | void adns_forallqueries_begin(adns_state ads) { |
| 716 | adns__consistency(ads,0,cc_entex); |
| 717 | ads->forallnext= |
| 718 | ads->udpw.head ? ads->udpw.head : |
| 719 | ads->tcpw.head ? ads->tcpw.head : |
| 720 | ads->childw.head ? ads->childw.head : |
| 721 | ads->output.head; |
| 722 | } |
| 723 | |
| 724 | adns_query adns_forallqueries_next(adns_state ads, void **context_r) { |
| 725 | adns_query qu, nqu; |
| 726 | |
| 727 | adns__consistency(ads,0,cc_entex); |
| 728 | nqu= ads->forallnext; |
| 729 | for (;;) { |
| 730 | qu= nqu; |
| 731 | if (!qu) return 0; |
| 732 | if (qu->next) { |
| 733 | nqu= qu->next; |
| 734 | } else if (qu == ads->udpw.tail) { |
| 735 | nqu= |
| 736 | ads->tcpw.head ? ads->tcpw.head : |
| 737 | ads->childw.head ? ads->childw.head : |
| 738 | ads->output.head; |
| 739 | } else if (qu == ads->tcpw.tail) { |
| 740 | nqu= |
| 741 | ads->childw.head ? ads->childw.head : |
| 742 | ads->output.head; |
| 743 | } else if (qu == ads->childw.tail) { |
| 744 | nqu= ads->output.head; |
| 745 | } else { |
| 746 | nqu= 0; |
| 747 | } |
| 748 | if (!qu->parent) break; |
| 749 | } |
| 750 | ads->forallnext= nqu; |
| 751 | if (context_r) *context_r= qu->ctx.ext; |
| 752 | return qu; |
| 753 | } |