res_init.c revision 156956
1156952Sume/* 2156952Sume * Copyright (c) 1985, 1989, 1993 3156952Sume * The Regents of the University of California. All rights reserved. 4156952Sume * 5156952Sume * Redistribution and use in source and binary forms, with or without 6156952Sume * modification, are permitted provided that the following conditions 7156952Sume * are met: 8156952Sume * 1. Redistributions of source code must retain the above copyright 9156952Sume * notice, this list of conditions and the following disclaimer. 10156952Sume * 2. Redistributions in binary form must reproduce the above copyright 11156952Sume * notice, this list of conditions and the following disclaimer in the 12156952Sume * documentation and/or other materials provided with the distribution. 13156952Sume * 3. All advertising materials mentioning features or use of this software 14156952Sume * must display the following acknowledgement: 15156952Sume * This product includes software developed by the University of 16156952Sume * California, Berkeley and its contributors. 17156952Sume * 4. Neither the name of the University nor the names of its contributors 18156952Sume * may be used to endorse or promote products derived from this software 19156952Sume * without specific prior written permission. 20156952Sume * 21156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24156952Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31156952Sume * SUCH DAMAGE. 32156952Sume */ 33156952Sume 34156952Sume/* 35156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation. 36156952Sume * 37156952Sume * Permission to use, copy, modify, and distribute this software for any 38156952Sume * purpose with or without fee is hereby granted, provided that the above 39156952Sume * copyright notice and this permission notice appear in all copies, and that 40156952Sume * the name of Digital Equipment Corporation not be used in advertising or 41156952Sume * publicity pertaining to distribution of the document or software without 42156952Sume * specific, written prior permission. 43156952Sume * 44156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 45156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 46156952Sume * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 47156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 48156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 49156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 50156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 51156952Sume * SOFTWARE. 52156952Sume */ 53156952Sume 54156952Sume/* 55156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 56156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 57156952Sume * 58156952Sume * Permission to use, copy, modify, and distribute this software for any 59156952Sume * purpose with or without fee is hereby granted, provided that the above 60156952Sume * copyright notice and this permission notice appear in all copies. 61156952Sume * 62156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 63156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 64156952Sume * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 65156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 66156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 67156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 68156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 69156952Sume */ 70156952Sume 71156952Sume#if defined(LIBC_SCCS) && !defined(lint) 72156952Sumestatic const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; 73156952Sumestatic const char rcsid[] = "$Id: res_init.c,v 1.9.2.5.4.5 2005/11/03 00:00:52 marka Exp $"; 74156952Sume#endif /* LIBC_SCCS and not lint */ 75156956Sume#include <sys/cdefs.h> 76156956Sume__FBSDID("$FreeBSD: head/lib/libc/resolv/res_init.c 156956 2006-03-21 15:37:16Z ume $"); 77156952Sume 78156952Sume#include "port_before.h" 79156952Sume 80156952Sume#include <sys/types.h> 81156952Sume#include <sys/param.h> 82156952Sume#include <sys/socket.h> 83156952Sume#include <sys/time.h> 84156952Sume 85156952Sume#include <netinet/in.h> 86156952Sume#include <arpa/inet.h> 87156952Sume#include <arpa/nameser.h> 88156952Sume 89156952Sume#include <ctype.h> 90156952Sume#include <stdio.h> 91156952Sume#include <stdlib.h> 92156952Sume#include <string.h> 93156952Sume#include <unistd.h> 94156952Sume#include <netdb.h> 95156952Sume 96156952Sume#include "port_after.h" 97156952Sume 98156952Sume/* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */ 99156952Sume#include <resolv.h> 100156952Sume 101156952Sume#include "res_private.h" 102156952Sume 103156952Sume/* Options. Should all be left alone. */ 104156952Sume#define RESOLVSORT 105156952Sume#define DEBUG 106156952Sume 107156952Sume#ifdef SOLARIS2 108156952Sume#include <sys/systeminfo.h> 109156952Sume#endif 110156952Sume 111156956Sumestatic void res_setoptions(res_state, const char *, const char *); 112156952Sume 113156952Sume#ifdef RESOLVSORT 114156952Sumestatic const char sort_mask[] = "/&"; 115156952Sume#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) 116156956Sumestatic u_int32_t net_mask(struct in_addr); 117156952Sume#endif 118156952Sume 119156952Sume#if !defined(isascii) /* XXX - could be a function */ 120156952Sume# define isascii(c) (!(c & 0200)) 121156952Sume#endif 122156952Sume 123156952Sume/* 124156952Sume * Resolver state default settings. 125156952Sume */ 126156952Sume 127156952Sume/* 128156952Sume * Set up default settings. If the configuration file exist, the values 129156952Sume * there will have precedence. Otherwise, the server address is set to 130156952Sume * INADDR_ANY and the default domain name comes from the gethostname(). 131156952Sume * 132156952Sume * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 133156952Sume * rather than INADDR_ANY ("0.0.0.0") as the default name server address 134156952Sume * since it was noted that INADDR_ANY actually meant ``the first interface 135156952Sume * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, 136156952Sume * it had to be "up" in order for you to reach your own name server. It 137156952Sume * was later decided that since the recommended practice is to always 138156952Sume * install local static routes through 127.0.0.1 for all your network 139156952Sume * interfaces, that we could solve this problem without a code change. 140156952Sume * 141156952Sume * The configuration file should always be used, since it is the only way 142156952Sume * to specify a default domain. If you are running a server on your local 143156952Sume * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" 144156952Sume * in the configuration file. 145156952Sume * 146156952Sume * Return 0 if completes successfully, -1 on error 147156952Sume */ 148156952Sumeint 149156952Sumeres_ninit(res_state statp) { 150156952Sume extern int __res_vinit(res_state, int); 151156952Sume 152156952Sume return (__res_vinit(statp, 0)); 153156952Sume} 154156952Sume 155156952Sume/* This function has to be reachable by res_data.c but not publically. */ 156156952Sumeint 157156952Sume__res_vinit(res_state statp, int preinit) { 158156956Sume FILE *fp; 159156956Sume char *cp, **pp; 160156956Sume int n; 161156952Sume char buf[BUFSIZ]; 162156952Sume int nserv = 0; /* number of nameserver records read from file */ 163156952Sume int haveenv = 0; 164156952Sume int havesearch = 0; 165156952Sume#ifdef RESOLVSORT 166156952Sume int nsort = 0; 167156952Sume char *net; 168156952Sume#endif 169156952Sume int dots; 170156952Sume union res_sockaddr_union u[2]; 171156952Sume 172156952Sume if (statp->_u._ext.ext != NULL) 173156952Sume res_ndestroy(statp); 174156952Sume 175156952Sume if (!preinit) { 176156952Sume statp->retrans = RES_TIMEOUT; 177156952Sume statp->retry = RES_DFLRETRY; 178156952Sume statp->options = RES_DEFAULT; 179156952Sume statp->id = res_randomid(); 180156952Sume } 181156952Sume 182156952Sume memset(u, 0, sizeof(u)); 183156952Sume#ifdef USELOOPBACK 184156952Sume u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); 185156952Sume#else 186156952Sume u[nserv].sin.sin_addr.s_addr = INADDR_ANY; 187156952Sume#endif 188156952Sume u[nserv].sin.sin_family = AF_INET; 189156952Sume u[nserv].sin.sin_port = htons(NAMESERVER_PORT); 190156952Sume#ifdef HAVE_SA_LEN 191156952Sume u[nserv].sin.sin_len = sizeof(struct sockaddr_in); 192156952Sume#endif 193156952Sume nserv++; 194156952Sume#ifdef HAS_INET6_STRUCTS 195156952Sume#ifdef USELOOPBACK 196156952Sume u[nserv].sin6.sin6_addr = in6addr_loopback; 197156952Sume#else 198156952Sume u[nserv].sin6.sin6_addr = in6addr_any; 199156952Sume#endif 200156952Sume u[nserv].sin6.sin6_family = AF_INET6; 201156952Sume u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT); 202156952Sume#ifdef HAVE_SA_LEN 203156952Sume u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6); 204156952Sume#endif 205156952Sume nserv++; 206156952Sume#endif 207156952Sume statp->nscount = 0; 208156952Sume statp->ndots = 1; 209156952Sume statp->pfcode = 0; 210156952Sume statp->_vcsock = -1; 211156952Sume statp->_flags = 0; 212156952Sume statp->qhook = NULL; 213156952Sume statp->rhook = NULL; 214156952Sume statp->_u._ext.nscount = 0; 215156952Sume statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext)); 216156952Sume if (statp->_u._ext.ext != NULL) { 217156952Sume memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext)); 218156952Sume statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr; 219156952Sume strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa"); 220156952Sume strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int"); 221156952Sume } else 222156952Sume return (-1); 223156952Sume#ifdef RESOLVSORT 224156952Sume statp->nsort = 0; 225156952Sume#endif 226156952Sume res_setservers(statp, u, nserv); 227156952Sume 228156952Sume#ifdef SOLARIS2 229156952Sume /* 230156952Sume * The old libresolv derived the defaultdomain from NIS/NIS+. 231156952Sume * We want to keep this behaviour 232156952Sume */ 233156952Sume { 234156952Sume char buf[sizeof(statp->defdname)], *cp; 235156952Sume int ret; 236156952Sume 237156952Sume if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 && 238156952Sume (unsigned int)ret <= sizeof(buf)) { 239156952Sume if (buf[0] == '+') 240156952Sume buf[0] = '.'; 241156952Sume cp = strchr(buf, '.'); 242156952Sume if (cp == NULL) { 243156952Sume if (strlcpy(statp->defdname, buf, 244156952Sume sizeof(statp->defdname)) 245156952Sume >= sizeof(statp->defdname)) 246156952Sume goto freedata; 247156952Sume } else { 248156952Sume if (strlcpy(statp->defdname, cp+1, 249156952Sume sizeof(statp->defdname)) 250156952Sume >= sizeof(statp->defdname)) 251156952Sume goto freedata; 252156952Sume } 253156952Sume } 254156952Sume } 255156952Sume#endif /* SOLARIS2 */ 256156952Sume 257156952Sume /* Allow user to override the local domain definition */ 258156956Sume if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) { 259156952Sume (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 260156952Sume statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 261156952Sume haveenv++; 262156952Sume 263156952Sume /* 264156952Sume * Set search list to be blank-separated strings 265156952Sume * from rest of env value. Permits users of LOCALDOMAIN 266156952Sume * to still have a search list, and anyone to set the 267156952Sume * one that they want to use as an individual (even more 268156952Sume * important now that the rfc1535 stuff restricts searches) 269156952Sume */ 270156952Sume cp = statp->defdname; 271156952Sume pp = statp->dnsrch; 272156952Sume *pp++ = cp; 273156952Sume for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { 274156952Sume if (*cp == '\n') /* silly backwards compat */ 275156952Sume break; 276156952Sume else if (*cp == ' ' || *cp == '\t') { 277156952Sume *cp = 0; 278156952Sume n = 1; 279156952Sume } else if (n) { 280156952Sume *pp++ = cp; 281156952Sume n = 0; 282156952Sume havesearch = 1; 283156952Sume } 284156952Sume } 285156952Sume /* null terminate last domain if there are excess */ 286156952Sume while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') 287156952Sume cp++; 288156952Sume *cp = '\0'; 289156952Sume *pp++ = 0; 290156952Sume } 291156952Sume 292156952Sume#define MATCH(line, name) \ 293156952Sume (!strncmp(line, name, sizeof(name) - 1) && \ 294156952Sume (line[sizeof(name) - 1] == ' ' || \ 295156952Sume line[sizeof(name) - 1] == '\t')) 296156952Sume 297156952Sume nserv = 0; 298156952Sume if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { 299156952Sume /* read the config file */ 300156952Sume while (fgets(buf, sizeof(buf), fp) != NULL) { 301156952Sume /* skip comments */ 302156952Sume if (*buf == ';' || *buf == '#') 303156952Sume continue; 304156952Sume /* read default domain name */ 305156952Sume if (MATCH(buf, "domain")) { 306156952Sume if (haveenv) /* skip if have from environ */ 307156952Sume continue; 308156952Sume cp = buf + sizeof("domain") - 1; 309156952Sume while (*cp == ' ' || *cp == '\t') 310156952Sume cp++; 311156952Sume if ((*cp == '\0') || (*cp == '\n')) 312156952Sume continue; 313156952Sume strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 314156952Sume statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 315156952Sume if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) 316156952Sume *cp = '\0'; 317156952Sume havesearch = 0; 318156952Sume continue; 319156952Sume } 320156952Sume /* set search list */ 321156952Sume if (MATCH(buf, "search")) { 322156952Sume if (haveenv) /* skip if have from environ */ 323156952Sume continue; 324156952Sume cp = buf + sizeof("search") - 1; 325156952Sume while (*cp == ' ' || *cp == '\t') 326156952Sume cp++; 327156952Sume if ((*cp == '\0') || (*cp == '\n')) 328156952Sume continue; 329156952Sume strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 330156952Sume statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 331156952Sume if ((cp = strchr(statp->defdname, '\n')) != NULL) 332156952Sume *cp = '\0'; 333156952Sume /* 334156952Sume * Set search list to be blank-separated strings 335156952Sume * on rest of line. 336156952Sume */ 337156952Sume cp = statp->defdname; 338156952Sume pp = statp->dnsrch; 339156952Sume *pp++ = cp; 340156952Sume for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { 341156952Sume if (*cp == ' ' || *cp == '\t') { 342156952Sume *cp = 0; 343156952Sume n = 1; 344156952Sume } else if (n) { 345156952Sume *pp++ = cp; 346156952Sume n = 0; 347156952Sume } 348156952Sume } 349156952Sume /* null terminate last domain if there are excess */ 350156952Sume while (*cp != '\0' && *cp != ' ' && *cp != '\t') 351156952Sume cp++; 352156952Sume *cp = '\0'; 353156952Sume *pp++ = 0; 354156952Sume havesearch = 1; 355156952Sume continue; 356156952Sume } 357156952Sume /* read nameservers to query */ 358156952Sume if (MATCH(buf, "nameserver") && nserv < MAXNS) { 359156952Sume struct addrinfo hints, *ai; 360156952Sume char sbuf[NI_MAXSERV]; 361156952Sume const size_t minsiz = 362156952Sume sizeof(statp->_u._ext.ext->nsaddrs[0]); 363156952Sume 364156952Sume cp = buf + sizeof("nameserver") - 1; 365156952Sume while (*cp == ' ' || *cp == '\t') 366156952Sume cp++; 367156952Sume cp[strcspn(cp, ";# \t\n")] = '\0'; 368156952Sume if ((*cp != '\0') && (*cp != '\n')) { 369156952Sume memset(&hints, 0, sizeof(hints)); 370156952Sume hints.ai_family = PF_UNSPEC; 371156952Sume hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 372156952Sume hints.ai_flags = AI_NUMERICHOST; 373156952Sume sprintf(sbuf, "%u", NAMESERVER_PORT); 374156952Sume if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && 375156952Sume ai->ai_addrlen <= minsiz) { 376156952Sume if (statp->_u._ext.ext != NULL) { 377156952Sume memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 378156952Sume ai->ai_addr, ai->ai_addrlen); 379156952Sume } 380156952Sume if (ai->ai_addrlen <= 381156952Sume sizeof(statp->nsaddr_list[nserv])) { 382156952Sume memcpy(&statp->nsaddr_list[nserv], 383156952Sume ai->ai_addr, ai->ai_addrlen); 384156952Sume } else 385156952Sume statp->nsaddr_list[nserv].sin_family = 0; 386156952Sume freeaddrinfo(ai); 387156952Sume nserv++; 388156952Sume } 389156952Sume } 390156952Sume continue; 391156952Sume } 392156952Sume#ifdef RESOLVSORT 393156952Sume if (MATCH(buf, "sortlist")) { 394156952Sume struct in_addr a; 395156956Sume struct in6_addr a6; 396156956Sume int m, i; 397156956Sume u_char *u; 398156956Sume struct __res_state_ext *ext = statp->_u._ext.ext; 399156952Sume 400156952Sume cp = buf + sizeof("sortlist") - 1; 401156952Sume while (nsort < MAXRESOLVSORT) { 402156952Sume while (*cp == ' ' || *cp == '\t') 403156952Sume cp++; 404156952Sume if (*cp == '\0' || *cp == '\n' || *cp == ';') 405156952Sume break; 406156952Sume net = cp; 407156952Sume while (*cp && !ISSORTMASK(*cp) && *cp != ';' && 408156952Sume isascii(*cp) && !isspace((unsigned char)*cp)) 409156952Sume cp++; 410156952Sume n = *cp; 411156952Sume *cp = 0; 412156952Sume if (inet_aton(net, &a)) { 413156952Sume statp->sort_list[nsort].addr = a; 414156952Sume if (ISSORTMASK(n)) { 415156952Sume *cp++ = n; 416156952Sume net = cp; 417156952Sume while (*cp && *cp != ';' && 418156952Sume isascii(*cp) && 419156952Sume !isspace((unsigned char)*cp)) 420156952Sume cp++; 421156952Sume n = *cp; 422156952Sume *cp = 0; 423156952Sume if (inet_aton(net, &a)) { 424156952Sume statp->sort_list[nsort].mask = a.s_addr; 425156952Sume } else { 426156952Sume statp->sort_list[nsort].mask = 427156952Sume net_mask(statp->sort_list[nsort].addr); 428156952Sume } 429156952Sume } else { 430156952Sume statp->sort_list[nsort].mask = 431156952Sume net_mask(statp->sort_list[nsort].addr); 432156952Sume } 433156956Sume ext->sort_list[nsort].af = AF_INET; 434156956Sume ext->sort_list[nsort].addr.ina = 435156956Sume statp->sort_list[nsort].addr; 436156956Sume ext->sort_list[nsort].mask.ina.s_addr = 437156956Sume statp->sort_list[nsort].mask; 438156952Sume nsort++; 439156952Sume } 440156956Sume else if (inet_pton(AF_INET6, net, &a6) == 1) { 441156956Sume 442156956Sume ext->sort_list[nsort].af = AF_INET6; 443156956Sume ext->sort_list[nsort].addr.in6a = a6; 444156956Sume u = (u_char *)&ext->sort_list[nsort].mask.in6a; 445156956Sume *cp++ = n; 446156956Sume net = cp; 447156956Sume while (*cp && *cp != ';' && 448156956Sume isascii(*cp) && !isspace(*cp)) 449156956Sume cp++; 450156956Sume m = n; 451156956Sume n = *cp; 452156956Sume *cp = 0; 453156956Sume switch (m) { 454156956Sume case '/': 455156956Sume m = atoi(net); 456156956Sume break; 457156956Sume case '&': 458156956Sume if (inet_pton(AF_INET6, net, u) == 1) { 459156956Sume m = -1; 460156956Sume break; 461156956Sume } 462156956Sume /*FALLTHROUGH*/ 463156956Sume default: 464156956Sume m = sizeof(struct in6_addr) * CHAR_BIT; 465156956Sume break; 466156956Sume } 467156956Sume if (m >= 0) { 468156956Sume for (i = 0; i < sizeof(struct in6_addr); i++) { 469156956Sume if (m <= 0) { 470156956Sume *u = 0; 471156956Sume } else { 472156956Sume m -= CHAR_BIT; 473156956Sume *u = (u_char)~0; 474156956Sume if (m < 0) 475156956Sume *u <<= -m; 476156956Sume } 477156956Sume u++; 478156956Sume } 479156956Sume } 480156956Sume statp->sort_list[nsort].addr.s_addr = 481156956Sume (u_int32_t)0xffffffff; 482156956Sume statp->sort_list[nsort].mask = 483156956Sume (u_int32_t)0xffffffff; 484156956Sume nsort++; 485156956Sume } 486156952Sume *cp = n; 487156952Sume } 488156952Sume continue; 489156952Sume } 490156952Sume#endif 491156952Sume if (MATCH(buf, "options")) { 492156952Sume res_setoptions(statp, buf + sizeof("options") - 1, "conf"); 493156952Sume continue; 494156952Sume } 495156952Sume } 496156952Sume if (nserv > 0) 497156952Sume statp->nscount = nserv; 498156952Sume#ifdef RESOLVSORT 499156952Sume statp->nsort = nsort; 500156952Sume#endif 501156952Sume (void) fclose(fp); 502156952Sume } 503156952Sume/* 504156952Sume * Last chance to get a nameserver. This should not normally 505156952Sume * be necessary 506156952Sume */ 507156952Sume#ifdef NO_RESOLV_CONF 508156952Sume if(nserv == 0) 509156952Sume nserv = get_nameservers(statp); 510156952Sume#endif 511156952Sume 512156952Sume if (statp->defdname[0] == 0 && 513156952Sume gethostname(buf, sizeof(statp->defdname) - 1) == 0 && 514156952Sume (cp = strchr(buf, '.')) != NULL) 515156952Sume strcpy(statp->defdname, cp + 1); 516156952Sume 517156952Sume /* find components of local domain that might be searched */ 518156952Sume if (havesearch == 0) { 519156952Sume pp = statp->dnsrch; 520156952Sume *pp++ = statp->defdname; 521156952Sume *pp = NULL; 522156952Sume 523156952Sume dots = 0; 524156952Sume for (cp = statp->defdname; *cp; cp++) 525156952Sume dots += (*cp == '.'); 526156952Sume 527156952Sume cp = statp->defdname; 528156952Sume while (pp < statp->dnsrch + MAXDFLSRCH) { 529156952Sume if (dots < LOCALDOMAINPARTS) 530156952Sume break; 531156952Sume cp = strchr(cp, '.') + 1; /* we know there is one */ 532156952Sume *pp++ = cp; 533156952Sume dots--; 534156952Sume } 535156952Sume *pp = NULL; 536156952Sume#ifdef DEBUG 537156952Sume if (statp->options & RES_DEBUG) { 538156952Sume printf(";; res_init()... default dnsrch list:\n"); 539156952Sume for (pp = statp->dnsrch; *pp; pp++) 540156952Sume printf(";;\t%s\n", *pp); 541156952Sume printf(";;\t..END..\n"); 542156952Sume } 543156952Sume#endif 544156952Sume } 545156952Sume 546156956Sume if (issetugid()) 547156956Sume statp->options |= RES_NOALIASES; 548156956Sume else if ((cp = getenv("RES_OPTIONS")) != NULL) 549156952Sume res_setoptions(statp, cp, "env"); 550156952Sume statp->options |= RES_INIT; 551156952Sume return (0); 552156952Sume 553156952Sume#ifdef SOLARIS2 554156952Sume freedata: 555156952Sume if (statp->_u._ext.ext != NULL) { 556156952Sume free(statp->_u._ext.ext); 557156952Sume statp->_u._ext.ext = NULL; 558156952Sume } 559156952Sume return (-1); 560156952Sume#endif 561156952Sume} 562156952Sume 563156952Sumestatic void 564156952Sumeres_setoptions(res_state statp, const char *options, const char *source) 565156952Sume{ 566156952Sume const char *cp = options; 567156952Sume int i; 568156956Sume#ifndef _LIBC 569156952Sume struct __res_state_ext *ext = statp->_u._ext.ext; 570156956Sume#endif 571156952Sume 572156952Sume#ifdef DEBUG 573156952Sume if (statp->options & RES_DEBUG) 574156952Sume printf(";; res_setoptions(\"%s\", \"%s\")...\n", 575156952Sume options, source); 576156952Sume#endif 577156952Sume while (*cp) { 578156952Sume /* skip leading and inner runs of spaces */ 579156952Sume while (*cp == ' ' || *cp == '\t') 580156952Sume cp++; 581156952Sume /* search for and process individual options */ 582156952Sume if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { 583156952Sume i = atoi(cp + sizeof("ndots:") - 1); 584156952Sume if (i <= RES_MAXNDOTS) 585156952Sume statp->ndots = i; 586156952Sume else 587156952Sume statp->ndots = RES_MAXNDOTS; 588156952Sume#ifdef DEBUG 589156952Sume if (statp->options & RES_DEBUG) 590156952Sume printf(";;\tndots=%d\n", statp->ndots); 591156952Sume#endif 592156952Sume } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { 593156952Sume i = atoi(cp + sizeof("timeout:") - 1); 594156952Sume if (i <= RES_MAXRETRANS) 595156952Sume statp->retrans = i; 596156952Sume else 597156952Sume statp->retrans = RES_MAXRETRANS; 598156952Sume#ifdef DEBUG 599156952Sume if (statp->options & RES_DEBUG) 600156952Sume printf(";;\ttimeout=%d\n", statp->retrans); 601156952Sume#endif 602156952Sume#ifdef SOLARIS2 603156952Sume } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) { 604156952Sume /* 605156952Sume * For backward compatibility, 'retrans' is 606156952Sume * supported as an alias for 'timeout', though 607156952Sume * without an imposed maximum. 608156952Sume */ 609156952Sume statp->retrans = atoi(cp + sizeof("retrans:") - 1); 610156952Sume } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){ 611156952Sume /* 612156952Sume * For backward compatibility, 'retry' is 613156952Sume * supported as an alias for 'attempts', though 614156952Sume * without an imposed maximum. 615156952Sume */ 616156952Sume statp->retry = atoi(cp + sizeof("retry:") - 1); 617156952Sume#endif /* SOLARIS2 */ 618156952Sume } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){ 619156952Sume i = atoi(cp + sizeof("attempts:") - 1); 620156952Sume if (i <= RES_MAXRETRY) 621156952Sume statp->retry = i; 622156952Sume else 623156952Sume statp->retry = RES_MAXRETRY; 624156952Sume#ifdef DEBUG 625156952Sume if (statp->options & RES_DEBUG) 626156952Sume printf(";;\tattempts=%d\n", statp->retry); 627156952Sume#endif 628156952Sume } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { 629156952Sume#ifdef DEBUG 630156952Sume if (!(statp->options & RES_DEBUG)) { 631156952Sume printf(";; res_setoptions(\"%s\", \"%s\")..\n", 632156952Sume options, source); 633156952Sume statp->options |= RES_DEBUG; 634156952Sume } 635156952Sume printf(";;\tdebug\n"); 636156952Sume#endif 637156952Sume } else if (!strncmp(cp, "no_tld_query", 638156952Sume sizeof("no_tld_query") - 1) || 639156952Sume !strncmp(cp, "no-tld-query", 640156952Sume sizeof("no-tld-query") - 1)) { 641156952Sume statp->options |= RES_NOTLDQUERY; 642156952Sume } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) { 643156952Sume statp->options |= RES_USE_INET6; 644156956Sume } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) { 645156956Sume statp->options |= RES_INSECURE1; 646156956Sume } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) { 647156956Sume statp->options |= RES_INSECURE2; 648156952Sume } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) { 649156952Sume statp->options |= RES_ROTATE; 650156952Sume } else if (!strncmp(cp, "no-check-names", 651156952Sume sizeof("no-check-names") - 1)) { 652156952Sume statp->options |= RES_NOCHECKNAME; 653156952Sume } 654156952Sume#ifdef RES_USE_EDNS0 655156952Sume else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { 656156952Sume statp->options |= RES_USE_EDNS0; 657156952Sume } 658156952Sume#endif 659156956Sume#ifndef _LIBC 660156952Sume else if (!strncmp(cp, "dname", sizeof("dname") - 1)) { 661156952Sume statp->options |= RES_USE_DNAME; 662156952Sume } 663156952Sume else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) { 664156952Sume if (ext == NULL) 665156952Sume goto skip; 666156952Sume cp += sizeof("nibble:") - 1; 667156952Sume i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1); 668156952Sume strncpy(ext->nsuffix, cp, i); 669156952Sume ext->nsuffix[i] = '\0'; 670156952Sume } 671156952Sume else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) { 672156952Sume if (ext == NULL) 673156952Sume goto skip; 674156952Sume cp += sizeof("nibble2:") - 1; 675156952Sume i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1); 676156952Sume strncpy(ext->nsuffix2, cp, i); 677156952Sume ext->nsuffix2[i] = '\0'; 678156952Sume } 679156952Sume else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) { 680156952Sume cp += sizeof("v6revmode:") - 1; 681156952Sume /* "nibble" and "bitstring" used to be valid */ 682156952Sume if (!strncmp(cp, "single", sizeof("single") - 1)) { 683156952Sume statp->options |= RES_NO_NIBBLE2; 684156952Sume } else if (!strncmp(cp, "both", sizeof("both") - 1)) { 685156952Sume statp->options &= 686156952Sume ~RES_NO_NIBBLE2; 687156952Sume } 688156952Sume } 689156956Sume#endif 690156952Sume else { 691156952Sume /* XXX - print a warning here? */ 692156952Sume } 693156956Sume#ifndef _LIBC 694156952Sume skip: 695156956Sume#endif 696156952Sume /* skip to next run of spaces */ 697156952Sume while (*cp && *cp != ' ' && *cp != '\t') 698156952Sume cp++; 699156952Sume } 700156952Sume} 701156952Sume 702156952Sume#ifdef RESOLVSORT 703156952Sume/* XXX - should really support CIDR which means explicit masks always. */ 704156952Sumestatic u_int32_t 705156952Sumenet_mask(in) /* XXX - should really use system's version of this */ 706156952Sume struct in_addr in; 707156952Sume{ 708156956Sume u_int32_t i = ntohl(in.s_addr); 709156952Sume 710156952Sume if (IN_CLASSA(i)) 711156952Sume return (htonl(IN_CLASSA_NET)); 712156952Sume else if (IN_CLASSB(i)) 713156952Sume return (htonl(IN_CLASSB_NET)); 714156952Sume return (htonl(IN_CLASSC_NET)); 715156952Sume} 716156952Sume#endif 717156952Sume 718156952Sumeu_int 719156952Sumeres_randomid(void) { 720156952Sume struct timeval now; 721156952Sume 722156952Sume gettimeofday(&now, NULL); 723156952Sume return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid())); 724156952Sume} 725156952Sume 726156952Sume/* 727156952Sume * This routine is for closing the socket if a virtual circuit is used and 728156952Sume * the program wants to close it. This provides support for endhostent() 729156952Sume * which expects to close the socket. 730156952Sume * 731156952Sume * This routine is not expected to be user visible. 732156952Sume */ 733156952Sumevoid 734156952Sumeres_nclose(res_state statp) { 735156952Sume int ns; 736156952Sume 737156952Sume if (statp->_vcsock >= 0) { 738156952Sume (void) close(statp->_vcsock); 739156952Sume statp->_vcsock = -1; 740156952Sume statp->_flags &= ~(RES_F_VC | RES_F_CONN); 741156952Sume } 742156952Sume for (ns = 0; ns < statp->_u._ext.nscount; ns++) { 743156952Sume if (statp->_u._ext.nssocks[ns] != -1) { 744156952Sume (void) close(statp->_u._ext.nssocks[ns]); 745156952Sume statp->_u._ext.nssocks[ns] = -1; 746156952Sume } 747156952Sume } 748156952Sume} 749156952Sume 750156952Sumevoid 751156952Sumeres_ndestroy(res_state statp) { 752156952Sume res_nclose(statp); 753156952Sume if (statp->_u._ext.ext != NULL) 754156952Sume free(statp->_u._ext.ext); 755156952Sume statp->options &= ~RES_INIT; 756156952Sume statp->_u._ext.ext = NULL; 757156952Sume} 758156952Sume 759156956Sume#ifndef _LIBC 760156952Sumeconst char * 761156952Sumeres_get_nibblesuffix(res_state statp) { 762156952Sume if (statp->_u._ext.ext) 763156952Sume return (statp->_u._ext.ext->nsuffix); 764156952Sume return ("ip6.arpa"); 765156952Sume} 766156952Sume 767156952Sumeconst char * 768156952Sumeres_get_nibblesuffix2(res_state statp) { 769156952Sume if (statp->_u._ext.ext) 770156952Sume return (statp->_u._ext.ext->nsuffix2); 771156952Sume return ("ip6.int"); 772156952Sume} 773156956Sume#endif 774156952Sume 775156952Sumevoid 776156952Sumeres_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) { 777156952Sume int i, nserv; 778156952Sume size_t size; 779156952Sume 780156952Sume /* close open servers */ 781156952Sume res_nclose(statp); 782156952Sume 783156952Sume /* cause rtt times to be forgotten */ 784156952Sume statp->_u._ext.nscount = 0; 785156952Sume 786156952Sume nserv = 0; 787156952Sume for (i = 0; i < cnt && nserv < MAXNS; i++) { 788156952Sume switch (set->sin.sin_family) { 789156952Sume case AF_INET: 790156952Sume size = sizeof(set->sin); 791156952Sume if (statp->_u._ext.ext) 792156952Sume memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 793156952Sume &set->sin, size); 794156952Sume if (size <= sizeof(statp->nsaddr_list[nserv])) 795156952Sume memcpy(&statp->nsaddr_list[nserv], 796156952Sume &set->sin, size); 797156952Sume else 798156952Sume statp->nsaddr_list[nserv].sin_family = 0; 799156952Sume nserv++; 800156952Sume break; 801156952Sume 802156952Sume#ifdef HAS_INET6_STRUCTS 803156952Sume case AF_INET6: 804156952Sume size = sizeof(set->sin6); 805156952Sume if (statp->_u._ext.ext) 806156952Sume memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 807156952Sume &set->sin6, size); 808156952Sume if (size <= sizeof(statp->nsaddr_list[nserv])) 809156952Sume memcpy(&statp->nsaddr_list[nserv], 810156952Sume &set->sin6, size); 811156952Sume else 812156952Sume statp->nsaddr_list[nserv].sin_family = 0; 813156952Sume nserv++; 814156952Sume break; 815156952Sume#endif 816156952Sume 817156952Sume default: 818156952Sume break; 819156952Sume } 820156952Sume set++; 821156952Sume } 822156952Sume statp->nscount = nserv; 823156952Sume 824156952Sume} 825156952Sume 826156952Sumeint 827156952Sumeres_getservers(res_state statp, union res_sockaddr_union *set, int cnt) { 828156952Sume int i; 829156952Sume size_t size; 830156952Sume u_int16_t family; 831156952Sume 832156952Sume for (i = 0; i < statp->nscount && i < cnt; i++) { 833156952Sume if (statp->_u._ext.ext) 834156952Sume family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family; 835156952Sume else 836156952Sume family = statp->nsaddr_list[i].sin_family; 837156952Sume 838156952Sume switch (family) { 839156952Sume case AF_INET: 840156952Sume size = sizeof(set->sin); 841156952Sume if (statp->_u._ext.ext) 842156952Sume memcpy(&set->sin, 843156952Sume &statp->_u._ext.ext->nsaddrs[i], 844156952Sume size); 845156952Sume else 846156952Sume memcpy(&set->sin, &statp->nsaddr_list[i], 847156952Sume size); 848156952Sume break; 849156952Sume 850156952Sume#ifdef HAS_INET6_STRUCTS 851156952Sume case AF_INET6: 852156952Sume size = sizeof(set->sin6); 853156952Sume if (statp->_u._ext.ext) 854156952Sume memcpy(&set->sin6, 855156952Sume &statp->_u._ext.ext->nsaddrs[i], 856156952Sume size); 857156952Sume else 858156952Sume memcpy(&set->sin6, &statp->nsaddr_list[i], 859156952Sume size); 860156952Sume break; 861156952Sume#endif 862156952Sume 863156952Sume default: 864156952Sume set->sin.sin_family = 0; 865156952Sume break; 866156952Sume } 867156952Sume set++; 868156952Sume } 869156952Sume return (statp->nscount); 870156952Sume} 871