1 /*- 2 * SPDX-License-Identifier: (BSD-3-Clause AND ISC) 3 * 4 * Copyright (c) 1985, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 34 * 35 * Permission to use, copy, modify, and distribute this software for any 36 * purpose with or without fee is hereby granted, provided that the above 37 * copyright notice and this permission notice appear in all copies, and that 38 * the name of Digital Equipment Corporation not be used in advertising or 39 * publicity pertaining to distribution of the document or software without 40 * specific, written prior permission. 41 * 42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 49 * SOFTWARE. 50 */ 51 52 /* 53 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 54 * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 55 * 56 * Permission to use, copy, modify, and distribute this software for any 57 * purpose with or without fee is hereby granted, provided that the above 58 * copyright notice and this permission notice appear in all copies. 59 * 60 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 61 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 62 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 63 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 64 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 65 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 66 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 67 */ 68 69 #if defined(LIBC_SCCS) && !defined(lint) 70 static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; 71 static const char rcsid[] = "$Id: res_init.c,v 1.26 2008/12/11 09:59:00 marka Exp $"; 72 #endif /* LIBC_SCCS and not lint */ 73 #include <sys/cdefs.h> 74 __FBSDID("$FreeBSD$"); 75 76 #include "port_before.h" 77 78 #include "namespace.h" 79 80 #include <sys/param.h> 81 #include <sys/socket.h> 82 #include <sys/stat.h> 83 #include <sys/time.h> 84 85 #include <netinet/in.h> 86 #include <arpa/inet.h> 87 #include <arpa/nameser.h> 88 89 #include <ctype.h> 90 #include <stdio.h> 91 #include <stdlib.h> 92 #include <string.h> 93 #include <unistd.h> 94 #include <netdb.h> 95 96 #ifndef HAVE_MD5 97 # include "../dst/md5.h" 98 #else 99 # ifdef SOLARIS2 100 # include <sys/md5.h> 101 # elif _LIBC 102 # include <md5.h> 103 # endif 104 #endif 105 #ifndef _MD5_H_ 106 # define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */ 107 #endif 108 109 #include "un-namespace.h" 110 111 #include "port_after.h" 112 113 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */ 114 #include <resolv.h> 115 116 #include "res_private.h" 117 118 /*% Options. Should all be left alone. */ 119 #define RESOLVSORT 120 #ifndef DEBUG 121 #define DEBUG 122 #endif 123 124 #ifdef SOLARIS2 125 #include <sys/systeminfo.h> 126 #endif 127 128 static void res_setoptions(res_state, const char *, const char *); 129 130 #ifdef RESOLVSORT 131 static const char sort_mask[] = "/&"; 132 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) 133 static u_int32_t net_mask(struct in_addr); 134 #endif 135 136 #if !defined(isascii) /*%< XXX - could be a function */ 137 # define isascii(c) (!(c & 0200)) 138 #endif 139 140 /* 141 * Resolver state default settings. 142 */ 143 144 /*% 145 * Set up default settings. If the configuration file exist, the values 146 * there will have precedence. Otherwise, the server address is set to 147 * INADDR_ANY and the default domain name comes from the gethostname(). 148 * 149 * An interim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 150 * rather than INADDR_ANY ("0.0.0.0") as the default name server address 151 * since it was noted that INADDR_ANY actually meant ``the first interface 152 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, 153 * it had to be "up" in order for you to reach your own name server. It 154 * was later decided that since the recommended practice is to always 155 * install local static routes through 127.0.0.1 for all your network 156 * interfaces, that we could solve this problem without a code change. 157 * 158 * The configuration file should always be used, since it is the only way 159 * to specify a default domain. If you are running a server on your local 160 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" 161 * in the configuration file. 162 * 163 * Return 0 if completes successfully, -1 on error 164 */ 165 int 166 res_ninit(res_state statp) { 167 extern int __res_vinit(res_state, int); 168 169 return (__res_vinit(statp, 0)); 170 } 171 172 /*% This function has to be reachable by res_data.c but not publicly. */ 173 int 174 __res_vinit(res_state statp, int preinit) { 175 FILE *fp; 176 char *cp, **pp; 177 int n; 178 char buf[BUFSIZ]; 179 int nserv = 0; /*%< number of nameserver records read from file */ 180 int haveenv = 0; 181 int havesearch = 0; 182 #ifdef RESOLVSORT 183 int nsort = 0; 184 char *net; 185 #endif 186 int dots; 187 union res_sockaddr_union u[2]; 188 int maxns = MAXNS; 189 190 RES_SET_H_ERRNO(statp, 0); 191 if (statp->_u._ext.ext != NULL) 192 res_ndestroy(statp); 193 194 if (!preinit) { 195 statp->retrans = RES_TIMEOUT; 196 statp->retry = RES_DFLRETRY; 197 statp->options = RES_DEFAULT; 198 } 199 200 statp->_rnd = malloc(16); 201 res_rndinit(statp); 202 statp->id = res_nrandomid(statp); 203 204 memset(u, 0, sizeof(u)); 205 #ifdef USELOOPBACK 206 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); 207 #else 208 u[nserv].sin.sin_addr.s_addr = INADDR_ANY; 209 #endif 210 u[nserv].sin.sin_family = AF_INET; 211 u[nserv].sin.sin_port = htons(NAMESERVER_PORT); 212 #ifdef HAVE_SA_LEN 213 u[nserv].sin.sin_len = sizeof(struct sockaddr_in); 214 #endif 215 nserv++; 216 #ifdef HAS_INET6_STRUCTS 217 #ifdef USELOOPBACK 218 u[nserv].sin6.sin6_addr = in6addr_loopback; 219 #else 220 u[nserv].sin6.sin6_addr = in6addr_any; 221 #endif 222 u[nserv].sin6.sin6_family = AF_INET6; 223 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT); 224 #ifdef HAVE_SA_LEN 225 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6); 226 #endif 227 nserv++; 228 #endif 229 statp->nscount = 0; 230 statp->ndots = 1; 231 statp->pfcode = 0; 232 statp->_vcsock = -1; 233 statp->_flags = 0; 234 statp->qhook = NULL; 235 statp->rhook = NULL; 236 statp->_u._ext.nscount = 0; 237 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext)); 238 if (statp->_u._ext.ext != NULL) { 239 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext)); 240 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr; 241 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa"); 242 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int"); 243 statp->_u._ext.ext->reload_period = 2; 244 } else { 245 /* 246 * Historically res_init() rarely, if at all, failed. 247 * Examples and applications exist which do not check 248 * our return code. Furthermore several applications 249 * simply call us to get the systems domainname. So 250 * rather then immediately fail here we store the 251 * failure, which is returned later, in h_errno. And 252 * prevent the collection of 'nameserver' information 253 * by setting maxns to 0. Thus applications that fail 254 * to check our return code wont be able to make 255 * queries anyhow. 256 */ 257 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 258 maxns = 0; 259 } 260 #ifdef RESOLVSORT 261 statp->nsort = 0; 262 #endif 263 res_setservers(statp, u, nserv); 264 265 #ifdef SOLARIS2 266 /* 267 * The old libresolv derived the defaultdomain from NIS/NIS+. 268 * We want to keep this behaviour 269 */ 270 { 271 char buf[sizeof(statp->defdname)], *cp; 272 int ret; 273 274 if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 && 275 (unsigned int)ret <= sizeof(buf)) { 276 if (buf[0] == '+') 277 buf[0] = '.'; 278 cp = strchr(buf, '.'); 279 cp = (cp == NULL) ? buf : (cp + 1); 280 strncpy(statp->defdname, cp, 281 sizeof(statp->defdname) - 1); 282 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 283 } 284 } 285 #endif /* SOLARIS2 */ 286 287 /* Allow user to override the local domain definition */ 288 if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) { 289 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 290 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 291 haveenv++; 292 293 /* 294 * Set search list to be blank-separated strings 295 * from rest of env value. Permits users of LOCALDOMAIN 296 * to still have a search list, and anyone to set the 297 * one that they want to use as an individual (even more 298 * important now that the rfc1535 stuff restricts searches) 299 */ 300 cp = statp->defdname; 301 pp = statp->dnsrch; 302 *pp++ = cp; 303 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { 304 if (*cp == '\n') /*%< silly backwards compat */ 305 break; 306 else if (*cp == ' ' || *cp == '\t') { 307 *cp = 0; 308 n = 1; 309 } else if (n) { 310 *pp++ = cp; 311 n = 0; 312 havesearch = 1; 313 } 314 } 315 /* null terminate last domain if there are excess */ 316 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') 317 cp++; 318 *cp = '\0'; 319 *pp++ = NULL; 320 } 321 322 #define MATCH(line, name) \ 323 (!strncmp(line, name, sizeof(name) - 1) && \ 324 (line[sizeof(name) - 1] == ' ' || \ 325 line[sizeof(name) - 1] == '\t')) 326 327 nserv = 0; 328 if ((fp = fopen(_PATH_RESCONF, "re")) != NULL) { 329 struct stat sb; 330 struct timespec now; 331 332 if (statp->_u._ext.ext != NULL) { 333 if (_fstat(fileno(fp), &sb) == 0) { 334 statp->_u._ext.ext->conf_mtim = sb.st_mtim; 335 if (clock_gettime(CLOCK_MONOTONIC_FAST, &now) == 0) { 336 statp->_u._ext.ext->conf_stat = now.tv_sec; 337 } 338 } 339 } 340 341 /* read the config file */ 342 while (fgets(buf, sizeof(buf), fp) != NULL) { 343 /* skip comments */ 344 if (*buf == ';' || *buf == '#') 345 continue; 346 /* read default domain name */ 347 if (MATCH(buf, "domain")) { 348 if (haveenv) /*%< skip if have from environ */ 349 continue; 350 cp = buf + sizeof("domain") - 1; 351 while (*cp == ' ' || *cp == '\t') 352 cp++; 353 if ((*cp == '\0') || (*cp == '\n')) 354 continue; 355 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 356 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 357 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) 358 *cp = '\0'; 359 havesearch = 0; 360 continue; 361 } 362 /* set search list */ 363 if (MATCH(buf, "search")) { 364 if (haveenv) /*%< skip if have from environ */ 365 continue; 366 cp = buf + sizeof("search") - 1; 367 while (*cp == ' ' || *cp == '\t') 368 cp++; 369 if ((*cp == '\0') || (*cp == '\n')) 370 continue; 371 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 372 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 373 if ((cp = strchr(statp->defdname, '\n')) != NULL) 374 *cp = '\0'; 375 /* 376 * Set search list to be blank-separated strings 377 * on rest of line. 378 */ 379 cp = statp->defdname; 380 pp = statp->dnsrch; 381 *pp++ = cp; 382 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { 383 if (*cp == ' ' || *cp == '\t') { 384 *cp = 0; 385 n = 1; 386 } else if (n) { 387 *pp++ = cp; 388 n = 0; 389 } 390 } 391 /* null terminate last domain if there are excess */ 392 while (*cp != '\0' && *cp != ' ' && *cp != '\t') 393 cp++; 394 *cp = '\0'; 395 *pp++ = NULL; 396 havesearch = 1; 397 continue; 398 } 399 /* read nameservers to query */ 400 if (MATCH(buf, "nameserver") && nserv < maxns) { 401 struct addrinfo hints, *ai; 402 char sbuf[NI_MAXSERV]; 403 const size_t minsiz = 404 sizeof(statp->_u._ext.ext->nsaddrs[0]); 405 406 cp = buf + sizeof("nameserver") - 1; 407 while (*cp == ' ' || *cp == '\t') 408 cp++; 409 cp[strcspn(cp, ";# \t\n")] = '\0'; 410 if ((*cp != '\0') && (*cp != '\n')) { 411 memset(&hints, 0, sizeof(hints)); 412 hints.ai_family = PF_UNSPEC; 413 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 414 hints.ai_flags = AI_NUMERICHOST; 415 sprintf(sbuf, "%u", NAMESERVER_PORT); 416 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0) { 417 if (ai->ai_addrlen <= minsiz) { 418 if (statp->_u._ext.ext != NULL) { 419 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 420 ai->ai_addr, ai->ai_addrlen); 421 } 422 if (ai->ai_addrlen <= 423 sizeof(statp->nsaddr_list[nserv])) { 424 memcpy(&statp->nsaddr_list[nserv], 425 ai->ai_addr, ai->ai_addrlen); 426 } else 427 statp->nsaddr_list[nserv].sin_family = 0; 428 nserv++; 429 } 430 freeaddrinfo(ai); 431 } 432 } 433 continue; 434 } 435 #ifdef RESOLVSORT 436 if (MATCH(buf, "sortlist")) { 437 struct in_addr a; 438 struct in6_addr a6; 439 int m, i; 440 u_char *u; 441 struct __res_state_ext *ext = statp->_u._ext.ext; 442 443 cp = buf + sizeof("sortlist") - 1; 444 while (nsort < MAXRESOLVSORT) { 445 while (*cp == ' ' || *cp == '\t') 446 cp++; 447 if (*cp == '\0' || *cp == '\n' || *cp == ';') 448 break; 449 net = cp; 450 while (*cp && !ISSORTMASK(*cp) && *cp != ';' && 451 isascii(*cp) && !isspace((unsigned char)*cp)) 452 cp++; 453 n = *cp; 454 *cp = 0; 455 if (inet_aton(net, &a)) { 456 statp->sort_list[nsort].addr = a; 457 if (ISSORTMASK(n)) { 458 *cp++ = n; 459 net = cp; 460 while (*cp && *cp != ';' && 461 isascii(*cp) && 462 !isspace((unsigned char)*cp)) 463 cp++; 464 n = *cp; 465 *cp = 0; 466 if (inet_aton(net, &a)) { 467 statp->sort_list[nsort].mask = a.s_addr; 468 } else { 469 statp->sort_list[nsort].mask = 470 net_mask(statp->sort_list[nsort].addr); 471 } 472 } else { 473 statp->sort_list[nsort].mask = 474 net_mask(statp->sort_list[nsort].addr); 475 } 476 ext->sort_list[nsort].af = AF_INET; 477 ext->sort_list[nsort].addr.ina = 478 statp->sort_list[nsort].addr; 479 ext->sort_list[nsort].mask.ina.s_addr = 480 statp->sort_list[nsort].mask; 481 nsort++; 482 } 483 else if (inet_pton(AF_INET6, net, &a6) == 1) { 484 485 ext->sort_list[nsort].af = AF_INET6; 486 ext->sort_list[nsort].addr.in6a = a6; 487 u = (u_char *)&ext->sort_list[nsort].mask.in6a; 488 *cp++ = n; 489 net = cp; 490 while (*cp && *cp != ';' && 491 isascii(*cp) && !isspace(*cp)) 492 cp++; 493 m = n; 494 n = *cp; 495 *cp = 0; 496 switch (m) { 497 case '/': 498 m = atoi(net); 499 break; 500 case '&': 501 if (inet_pton(AF_INET6, net, u) == 1) { 502 m = -1; 503 break; 504 } 505 /*FALLTHROUGH*/ 506 default: 507 m = sizeof(struct in6_addr) * CHAR_BIT; 508 break; 509 } 510 if (m >= 0) { 511 for (i = 0; i < sizeof(struct in6_addr); i++) { 512 if (m <= 0) { 513 *u = 0; 514 } else { 515 m -= CHAR_BIT; 516 *u = (u_char)~0; 517 if (m < 0) 518 *u <<= -m; 519 } 520 u++; 521 } 522 } 523 statp->sort_list[nsort].addr.s_addr = 524 (u_int32_t)0xffffffff; 525 statp->sort_list[nsort].mask = 526 (u_int32_t)0xffffffff; 527 nsort++; 528 } 529 *cp = n; 530 } 531 continue; 532 } 533 #endif 534 if (MATCH(buf, "options")) { 535 res_setoptions(statp, buf + sizeof("options") - 1, "conf"); 536 continue; 537 } 538 } 539 if (nserv > 0) 540 statp->nscount = nserv; 541 #ifdef RESOLVSORT 542 statp->nsort = nsort; 543 #endif 544 (void) fclose(fp); 545 } 546 /* 547 * Last chance to get a nameserver. This should not normally 548 * be necessary 549 */ 550 #ifdef NO_RESOLV_CONF 551 if(nserv == 0) 552 nserv = get_nameservers(statp); 553 #endif 554 555 if (statp->defdname[0] == 0 && 556 gethostname(buf, sizeof(statp->defdname) - 1) == 0 && 557 (cp = strchr(buf, '.')) != NULL) 558 strcpy(statp->defdname, cp + 1); 559 560 /* find components of local domain that might be searched */ 561 if (havesearch == 0) { 562 pp = statp->dnsrch; 563 *pp++ = statp->defdname; 564 *pp = NULL; 565 566 dots = 0; 567 for (cp = statp->defdname; *cp; cp++) 568 dots += (*cp == '.'); 569 570 cp = statp->defdname; 571 while (pp < statp->dnsrch + MAXDFLSRCH) { 572 if (dots < LOCALDOMAINPARTS) 573 break; 574 cp = strchr(cp, '.') + 1; /*%< we know there is one */ 575 *pp++ = cp; 576 dots--; 577 } 578 *pp = NULL; 579 #ifdef DEBUG 580 if (statp->options & RES_DEBUG) { 581 printf(";; res_init()... default dnsrch list:\n"); 582 for (pp = statp->dnsrch; *pp; pp++) 583 printf(";;\t%s\n", *pp); 584 printf(";;\t..END..\n"); 585 } 586 #endif 587 } 588 589 if (issetugid()) 590 statp->options |= RES_NOALIASES; 591 else if ((cp = getenv("RES_OPTIONS")) != NULL) 592 res_setoptions(statp, cp, "env"); 593 statp->options |= RES_INIT; 594 return (statp->res_h_errno); 595 } 596 597 static void 598 res_setoptions(res_state statp, const char *options, const char *source) 599 { 600 const char *cp = options; 601 int i; 602 struct __res_state_ext *ext = statp->_u._ext.ext; 603 604 #ifdef DEBUG 605 if (statp->options & RES_DEBUG) 606 printf(";; res_setoptions(\"%s\", \"%s\")...\n", 607 options, source); 608 #endif 609 while (*cp) { 610 /* skip leading and inner runs of spaces */ 611 while (*cp == ' ' || *cp == '\t') 612 cp++; 613 /* search for and process individual options */ 614 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { 615 i = atoi(cp + sizeof("ndots:") - 1); 616 if (i <= RES_MAXNDOTS) 617 statp->ndots = i; 618 else 619 statp->ndots = RES_MAXNDOTS; 620 #ifdef DEBUG 621 if (statp->options & RES_DEBUG) 622 printf(";;\tndots=%d\n", statp->ndots); 623 #endif 624 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { 625 i = atoi(cp + sizeof("timeout:") - 1); 626 if (i <= RES_MAXRETRANS) 627 statp->retrans = i; 628 else 629 statp->retrans = RES_MAXRETRANS; 630 #ifdef DEBUG 631 if (statp->options & RES_DEBUG) 632 printf(";;\ttimeout=%d\n", statp->retrans); 633 #endif 634 #ifdef SOLARIS2 635 } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) { 636 /* 637 * For backward compatibility, 'retrans' is 638 * supported as an alias for 'timeout', though 639 * without an imposed maximum. 640 */ 641 statp->retrans = atoi(cp + sizeof("retrans:") - 1); 642 } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){ 643 /* 644 * For backward compatibility, 'retry' is 645 * supported as an alias for 'attempts', though 646 * without an imposed maximum. 647 */ 648 statp->retry = atoi(cp + sizeof("retry:") - 1); 649 #endif /* SOLARIS2 */ 650 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){ 651 i = atoi(cp + sizeof("attempts:") - 1); 652 if (i <= RES_MAXRETRY) 653 statp->retry = i; 654 else 655 statp->retry = RES_MAXRETRY; 656 #ifdef DEBUG 657 if (statp->options & RES_DEBUG) 658 printf(";;\tattempts=%d\n", statp->retry); 659 #endif 660 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { 661 #ifdef DEBUG 662 if (!(statp->options & RES_DEBUG)) { 663 printf(";; res_setoptions(\"%s\", \"%s\")..\n", 664 options, source); 665 statp->options |= RES_DEBUG; 666 } 667 printf(";;\tdebug\n"); 668 #endif 669 } else if (!strncmp(cp, "no_tld_query", 670 sizeof("no_tld_query") - 1) || 671 !strncmp(cp, "no-tld-query", 672 sizeof("no-tld-query") - 1)) { 673 statp->options |= RES_NOTLDQUERY; 674 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) { 675 statp->options |= RES_USE_INET6; 676 } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) { 677 statp->options |= RES_INSECURE1; 678 } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) { 679 statp->options |= RES_INSECURE2; 680 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) { 681 statp->options |= RES_ROTATE; 682 } else if (!strncmp(cp, "usevc", sizeof("usevc") - 1)) { 683 statp->options |= RES_USEVC; 684 } else if (!strncmp(cp, "no-check-names", 685 sizeof("no-check-names") - 1)) { 686 statp->options |= RES_NOCHECKNAME; 687 } else if (!strncmp(cp, "reload-period:", 688 sizeof("reload-period:") - 1)) { 689 if (ext != NULL) { 690 ext->reload_period = (u_short) 691 atoi(cp + sizeof("reload-period:") - 1); 692 } 693 } 694 #ifdef RES_USE_EDNS0 695 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { 696 statp->options |= RES_USE_EDNS0; 697 } 698 #endif 699 #ifndef _LIBC 700 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) { 701 statp->options |= RES_USE_DNAME; 702 } 703 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) { 704 if (ext == NULL) 705 goto skip; 706 cp += sizeof("nibble:") - 1; 707 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1); 708 strncpy(ext->nsuffix, cp, i); 709 ext->nsuffix[i] = '\0'; 710 } 711 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) { 712 if (ext == NULL) 713 goto skip; 714 cp += sizeof("nibble2:") - 1; 715 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1); 716 strncpy(ext->nsuffix2, cp, i); 717 ext->nsuffix2[i] = '\0'; 718 } 719 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) { 720 cp += sizeof("v6revmode:") - 1; 721 /* "nibble" and "bitstring" used to be valid */ 722 if (!strncmp(cp, "single", sizeof("single") - 1)) { 723 statp->options |= RES_NO_NIBBLE2; 724 } else if (!strncmp(cp, "both", sizeof("both") - 1)) { 725 statp->options &= 726 ~RES_NO_NIBBLE2; 727 } 728 } 729 #endif 730 else { 731 /* XXX - print a warning here? */ 732 } 733 #ifndef _LIBC 734 skip: 735 #endif 736 /* skip to next run of spaces */ 737 while (*cp && *cp != ' ' && *cp != '\t') 738 cp++; 739 } 740 } 741 742 #ifdef RESOLVSORT 743 /* XXX - should really support CIDR which means explicit masks always. */ 744 static u_int32_t 745 net_mask(struct in_addr in) /*!< XXX - should really use system's version of this */ 746 { 747 u_int32_t i = ntohl(in.s_addr); 748 749 if (IN_CLASSA(i)) 750 return (htonl(IN_CLASSA_NET)); 751 else if (IN_CLASSB(i)) 752 return (htonl(IN_CLASSB_NET)); 753 return (htonl(IN_CLASSC_NET)); 754 } 755 #endif 756 757 static u_char srnd[16]; 758 759 void 760 res_rndinit(res_state statp) 761 { 762 struct timeval now; 763 u_int32_t u32; 764 u_int16_t u16; 765 u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd; 766 767 gettimeofday(&now, NULL); 768 u32 = now.tv_sec; 769 memcpy(rnd, &u32, 4); 770 u32 = now.tv_usec; 771 memcpy(rnd + 4, &u32, 4); 772 u32 += now.tv_sec; 773 memcpy(rnd + 8, &u32, 4); 774 u16 = getpid(); 775 memcpy(rnd + 12, &u16, 2); 776 } 777 778 u_int 779 res_nrandomid(res_state statp) { 780 struct timeval now; 781 u_int16_t u16; 782 MD5_CTX ctx; 783 u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd; 784 785 gettimeofday(&now, NULL); 786 u16 = (u_int16_t) (now.tv_sec ^ now.tv_usec); 787 memcpy(rnd + 14, &u16, 2); 788 #ifndef HAVE_MD5 789 MD5_Init(&ctx); 790 MD5_Update(&ctx, rnd, 16); 791 MD5_Final(rnd, &ctx); 792 #else 793 MD5Init(&ctx); 794 MD5Update(&ctx, rnd, 16); 795 MD5Final(rnd, &ctx); 796 #endif 797 memcpy(&u16, rnd + 14, 2); 798 return ((u_int) u16); 799 } 800 801 /*% 802 * This routine is for closing the socket if a virtual circuit is used and 803 * the program wants to close it. This provides support for endhostent() 804 * which expects to close the socket. 805 * 806 * This routine is not expected to be user visible. 807 */ 808 void 809 res_nclose(res_state statp) { 810 int ns; 811 812 if (statp->_vcsock >= 0) { 813 (void) _close(statp->_vcsock); 814 statp->_vcsock = -1; 815 statp->_flags &= ~(RES_F_VC | RES_F_CONN); 816 } 817 for (ns = 0; ns < statp->_u._ext.nscount; ns++) { 818 if (statp->_u._ext.nssocks[ns] != -1) { 819 (void) _close(statp->_u._ext.nssocks[ns]); 820 statp->_u._ext.nssocks[ns] = -1; 821 } 822 } 823 } 824 825 void 826 res_ndestroy(res_state statp) { 827 res_nclose(statp); 828 if (statp->_u._ext.ext != NULL) { 829 free(statp->_u._ext.ext); 830 statp->_u._ext.ext = NULL; 831 } 832 if (statp->_rnd != NULL) { 833 free(statp->_rnd); 834 statp->_rnd = NULL; 835 } 836 statp->options &= ~RES_INIT; 837 } 838 839 #ifndef _LIBC 840 const char * 841 res_get_nibblesuffix(res_state statp) { 842 if (statp->_u._ext.ext) 843 return (statp->_u._ext.ext->nsuffix); 844 return ("ip6.arpa"); 845 } 846 847 const char * 848 res_get_nibblesuffix2(res_state statp) { 849 if (statp->_u._ext.ext) 850 return (statp->_u._ext.ext->nsuffix2); 851 return ("ip6.int"); 852 } 853 #endif 854 855 void 856 res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) { 857 int i, nserv; 858 size_t size; 859 860 /* close open servers */ 861 res_nclose(statp); 862 863 /* cause rtt times to be forgotten */ 864 statp->_u._ext.nscount = 0; 865 866 nserv = 0; 867 for (i = 0; i < cnt && nserv < MAXNS; i++) { 868 switch (set->sin.sin_family) { 869 case AF_INET: 870 size = sizeof(set->sin); 871 if (statp->_u._ext.ext) 872 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 873 &set->sin, size); 874 if (size <= sizeof(statp->nsaddr_list[nserv])) 875 memcpy(&statp->nsaddr_list[nserv], 876 &set->sin, size); 877 else 878 statp->nsaddr_list[nserv].sin_family = 0; 879 nserv++; 880 break; 881 882 #ifdef HAS_INET6_STRUCTS 883 case AF_INET6: 884 size = sizeof(set->sin6); 885 if (statp->_u._ext.ext) 886 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 887 &set->sin6, size); 888 if (size <= sizeof(statp->nsaddr_list[nserv])) 889 memcpy(&statp->nsaddr_list[nserv], 890 &set->sin6, size); 891 else 892 statp->nsaddr_list[nserv].sin_family = 0; 893 nserv++; 894 break; 895 #endif 896 897 default: 898 break; 899 } 900 set++; 901 } 902 statp->nscount = nserv; 903 904 } 905 906 int 907 res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) { 908 int i; 909 size_t size; 910 u_int16_t family; 911 912 for (i = 0; i < statp->nscount && i < cnt; i++) { 913 if (statp->_u._ext.ext) 914 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family; 915 else 916 family = statp->nsaddr_list[i].sin_family; 917 918 switch (family) { 919 case AF_INET: 920 size = sizeof(set->sin); 921 if (statp->_u._ext.ext) 922 memcpy(&set->sin, 923 &statp->_u._ext.ext->nsaddrs[i], 924 size); 925 else 926 memcpy(&set->sin, &statp->nsaddr_list[i], 927 size); 928 break; 929 930 #ifdef HAS_INET6_STRUCTS 931 case AF_INET6: 932 size = sizeof(set->sin6); 933 if (statp->_u._ext.ext) 934 memcpy(&set->sin6, 935 &statp->_u._ext.ext->nsaddrs[i], 936 size); 937 else 938 memcpy(&set->sin6, &statp->nsaddr_list[i], 939 size); 940 break; 941 #endif 942 943 default: 944 set->sin.sin_family = 0; 945 break; 946 } 947 set++; 948 } 949 return (statp->nscount); 950 } 951 952 /*! \file */ 953