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