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