res_init.c revision 269867
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 * 4. Neither the name of the University nor the names of its contributors
14156952Sume *    may be used to endorse or promote products derived from this software
15156952Sume *    without specific prior written permission.
16156952Sume *
17156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20156952Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27156952Sume * SUCH DAMAGE.
28156952Sume */
29156952Sume
30156952Sume/*
31156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation.
32156952Sume *
33156952Sume * Permission to use, copy, modify, and distribute this software for any
34156952Sume * purpose with or without fee is hereby granted, provided that the above
35156952Sume * copyright notice and this permission notice appear in all copies, and that
36156952Sume * the name of Digital Equipment Corporation not be used in advertising or
37156952Sume * publicity pertaining to distribution of the document or software without
38156952Sume * specific, written prior permission.
39156952Sume *
40156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42156952Sume * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
43156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47156952Sume * SOFTWARE.
48156952Sume */
49156952Sume
50156952Sume/*
51156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
52156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
53156952Sume *
54156952Sume * Permission to use, copy, modify, and distribute this software for any
55156952Sume * purpose with or without fee is hereby granted, provided that the above
56156952Sume * copyright notice and this permission notice appear in all copies.
57156952Sume *
58156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
59156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
60156952Sume * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
61156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
62156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
63156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
64156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
65156952Sume */
66156952Sume
67156952Sume#if defined(LIBC_SCCS) && !defined(lint)
68156952Sumestatic const char sccsid[] = "@(#)res_init.c	8.1 (Berkeley) 6/7/93";
69269867Sumestatic const char rcsid[] = "$Id: res_init.c,v 1.26 2008/12/11 09:59:00 marka Exp $";
70156952Sume#endif /* LIBC_SCCS and not lint */
71156956Sume#include <sys/cdefs.h>
72156956Sume__FBSDID("$FreeBSD: head/lib/libc/resolv/res_init.c 269867 2014-08-12 12:36:06Z ume $");
73156952Sume
74156952Sume#include "port_before.h"
75156952Sume
76160965Sume#include "namespace.h"
77160965Sume
78156952Sume#include <sys/types.h>
79156952Sume#include <sys/param.h>
80156952Sume#include <sys/socket.h>
81156952Sume#include <sys/time.h>
82156952Sume
83156952Sume#include <netinet/in.h>
84156952Sume#include <arpa/inet.h>
85156952Sume#include <arpa/nameser.h>
86156952Sume
87156952Sume#include <ctype.h>
88156952Sume#include <stdio.h>
89156952Sume#include <stdlib.h>
90156952Sume#include <string.h>
91156952Sume#include <unistd.h>
92156952Sume#include <netdb.h>
93156952Sume
94269867Sume#ifndef HAVE_MD5
95269867Sume# include "../dst/md5.h"
96269867Sume#else
97269867Sume# ifdef SOLARIS2
98269867Sume#  include <sys/md5.h>
99269867Sume# elif _LIBC
100269867Sume# include <md5.h>
101269867Sume# endif
102269867Sume#endif
103269867Sume#ifndef _MD5_H_
104269867Sume# define _MD5_H_ 1	/*%< make sure we do not include rsaref md5.h file */
105269867Sume#endif
106269867Sume
107160965Sume#include "un-namespace.h"
108160965Sume
109156952Sume#include "port_after.h"
110156952Sume
111156952Sume/* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
112156952Sume#include <resolv.h>
113156952Sume
114156952Sume#include "res_private.h"
115156952Sume
116170244Sume/*% Options.  Should all be left alone. */
117156952Sume#define RESOLVSORT
118156952Sume#define DEBUG
119156952Sume
120156952Sume#ifdef SOLARIS2
121156952Sume#include <sys/systeminfo.h>
122156952Sume#endif
123156952Sume
124156956Sumestatic void res_setoptions(res_state, const char *, const char *);
125156952Sume
126156952Sume#ifdef RESOLVSORT
127156952Sumestatic const char sort_mask[] = "/&";
128156952Sume#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
129156956Sumestatic u_int32_t net_mask(struct in_addr);
130156952Sume#endif
131156952Sume
132170244Sume#if !defined(isascii)	/*%< XXX - could be a function */
133156952Sume# define isascii(c) (!(c & 0200))
134156952Sume#endif
135156952Sume
136156952Sume/*
137156952Sume * Resolver state default settings.
138156952Sume */
139156952Sume
140170244Sume/*%
141156952Sume * Set up default settings.  If the configuration file exist, the values
142156952Sume * there will have precedence.  Otherwise, the server address is set to
143156952Sume * INADDR_ANY and the default domain name comes from the gethostname().
144156952Sume *
145156952Sume * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
146156952Sume * rather than INADDR_ANY ("0.0.0.0") as the default name server address
147156952Sume * since it was noted that INADDR_ANY actually meant ``the first interface
148156952Sume * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
149156952Sume * it had to be "up" in order for you to reach your own name server.  It
150156952Sume * was later decided that since the recommended practice is to always
151156952Sume * install local static routes through 127.0.0.1 for all your network
152156952Sume * interfaces, that we could solve this problem without a code change.
153156952Sume *
154156952Sume * The configuration file should always be used, since it is the only way
155156952Sume * to specify a default domain.  If you are running a server on your local
156156952Sume * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
157156952Sume * in the configuration file.
158156952Sume *
159156952Sume * Return 0 if completes successfully, -1 on error
160156952Sume */
161156952Sumeint
162156952Sumeres_ninit(res_state statp) {
163156952Sume	extern int __res_vinit(res_state, int);
164156952Sume
165156952Sume	return (__res_vinit(statp, 0));
166156952Sume}
167156952Sume
168170244Sume/*% This function has to be reachable by res_data.c but not publically. */
169156952Sumeint
170156952Sume__res_vinit(res_state statp, int preinit) {
171156956Sume	FILE *fp;
172156956Sume	char *cp, **pp;
173156956Sume	int n;
174156952Sume	char buf[BUFSIZ];
175170244Sume	int nserv = 0;    /*%< number of nameserver records read from file */
176156952Sume	int haveenv = 0;
177156952Sume	int havesearch = 0;
178156952Sume#ifdef RESOLVSORT
179156952Sume	int nsort = 0;
180156952Sume	char *net;
181156952Sume#endif
182156952Sume	int dots;
183156952Sume	union res_sockaddr_union u[2];
184174226Sume	int maxns = MAXNS;
185156952Sume
186174226Sume	RES_SET_H_ERRNO(statp, 0);
187156952Sume	if (statp->_u._ext.ext != NULL)
188156952Sume		res_ndestroy(statp);
189156952Sume
190156952Sume	if (!preinit) {
191156952Sume		statp->retrans = RES_TIMEOUT;
192156952Sume		statp->retry = RES_DFLRETRY;
193156952Sume		statp->options = RES_DEFAULT;
194156952Sume	}
195156952Sume
196269867Sume	statp->_rnd = malloc(16);
197269867Sume	res_rndinit(statp);
198269867Sume	statp->id = res_nrandomid(statp);
199269867Sume
200156952Sume	memset(u, 0, sizeof(u));
201156952Sume#ifdef USELOOPBACK
202156952Sume	u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
203156952Sume#else
204156952Sume	u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
205156952Sume#endif
206156952Sume	u[nserv].sin.sin_family = AF_INET;
207156952Sume	u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
208156952Sume#ifdef HAVE_SA_LEN
209156952Sume	u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
210156952Sume#endif
211156952Sume	nserv++;
212156952Sume#ifdef HAS_INET6_STRUCTS
213156952Sume#ifdef USELOOPBACK
214156952Sume	u[nserv].sin6.sin6_addr = in6addr_loopback;
215156952Sume#else
216156952Sume	u[nserv].sin6.sin6_addr = in6addr_any;
217156952Sume#endif
218156952Sume	u[nserv].sin6.sin6_family = AF_INET6;
219156952Sume	u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
220156952Sume#ifdef HAVE_SA_LEN
221156952Sume	u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
222156952Sume#endif
223156952Sume	nserv++;
224156952Sume#endif
225156952Sume	statp->nscount = 0;
226156952Sume	statp->ndots = 1;
227156952Sume	statp->pfcode = 0;
228156952Sume	statp->_vcsock = -1;
229156952Sume	statp->_flags = 0;
230156952Sume	statp->qhook = NULL;
231156952Sume	statp->rhook = NULL;
232156952Sume	statp->_u._ext.nscount = 0;
233156952Sume	statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
234156952Sume	if (statp->_u._ext.ext != NULL) {
235156952Sume	        memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
236156952Sume		statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
237156952Sume		strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
238156952Sume		strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
239174226Sume	} else {
240174226Sume		/*
241174226Sume		 * Historically res_init() rarely, if at all, failed.
242174226Sume		 * Examples and applications exist which do not check
243174226Sume		 * our return code.  Furthermore several applications
244174226Sume		 * simply call us to get the systems domainname.  So
245174226Sume		 * rather then immediately fail here we store the
246174226Sume		 * failure, which is returned later, in h_errno.  And
247174226Sume		 * prevent the collection of 'nameserver' information
248174226Sume		 * by setting maxns to 0.  Thus applications that fail
249174226Sume		 * to check our return code wont be able to make
250174226Sume		 * queries anyhow.
251174226Sume		 */
252174226Sume		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
253174226Sume		maxns = 0;
254174226Sume	}
255156952Sume#ifdef RESOLVSORT
256156952Sume	statp->nsort = 0;
257156952Sume#endif
258156952Sume	res_setservers(statp, u, nserv);
259156952Sume
260156952Sume#ifdef	SOLARIS2
261156952Sume	/*
262156952Sume	 * The old libresolv derived the defaultdomain from NIS/NIS+.
263156952Sume	 * We want to keep this behaviour
264156952Sume	 */
265156952Sume	{
266156952Sume		char buf[sizeof(statp->defdname)], *cp;
267156952Sume		int ret;
268156952Sume
269156952Sume		if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 &&
270156952Sume			(unsigned int)ret <= sizeof(buf)) {
271156952Sume			if (buf[0] == '+')
272156952Sume				buf[0] = '.';
273156952Sume			cp = strchr(buf, '.');
274165258Sume			cp = (cp == NULL) ? buf : (cp + 1);
275174226Sume			strncpy(statp->defdname, cp,
276174226Sume				sizeof(statp->defdname) - 1);
277174226Sume			statp->defdname[sizeof(statp->defdname) - 1] = '\0';
278156952Sume		}
279156952Sume	}
280156952Sume#endif	/* SOLARIS2 */
281156952Sume
282156952Sume	/* Allow user to override the local domain definition */
283156956Sume	if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) {
284156952Sume		(void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
285156952Sume		statp->defdname[sizeof(statp->defdname) - 1] = '\0';
286156952Sume		haveenv++;
287156952Sume
288156952Sume		/*
289156952Sume		 * Set search list to be blank-separated strings
290156952Sume		 * from rest of env value.  Permits users of LOCALDOMAIN
291156952Sume		 * to still have a search list, and anyone to set the
292156952Sume		 * one that they want to use as an individual (even more
293156952Sume		 * important now that the rfc1535 stuff restricts searches)
294156952Sume		 */
295156952Sume		cp = statp->defdname;
296156952Sume		pp = statp->dnsrch;
297156952Sume		*pp++ = cp;
298156952Sume		for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
299170244Sume			if (*cp == '\n')	/*%< silly backwards compat */
300156952Sume				break;
301156952Sume			else if (*cp == ' ' || *cp == '\t') {
302156952Sume				*cp = 0;
303156952Sume				n = 1;
304156952Sume			} else if (n) {
305156952Sume				*pp++ = cp;
306156952Sume				n = 0;
307156952Sume				havesearch = 1;
308156952Sume			}
309156952Sume		}
310156952Sume		/* null terminate last domain if there are excess */
311156952Sume		while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
312156952Sume			cp++;
313156952Sume		*cp = '\0';
314156952Sume		*pp++ = 0;
315156952Sume	}
316156952Sume
317156952Sume#define	MATCH(line, name) \
318156952Sume	(!strncmp(line, name, sizeof(name) - 1) && \
319156952Sume	(line[sizeof(name) - 1] == ' ' || \
320156952Sume	 line[sizeof(name) - 1] == '\t'))
321156952Sume
322156952Sume	nserv = 0;
323254700Sjilles	if ((fp = fopen(_PATH_RESCONF, "re")) != NULL) {
324156952Sume	    /* read the config file */
325156952Sume	    while (fgets(buf, sizeof(buf), fp) != NULL) {
326156952Sume		/* skip comments */
327156952Sume		if (*buf == ';' || *buf == '#')
328156952Sume			continue;
329156952Sume		/* read default domain name */
330156952Sume		if (MATCH(buf, "domain")) {
331170244Sume		    if (haveenv)	/*%< skip if have from environ */
332156952Sume			    continue;
333156952Sume		    cp = buf + sizeof("domain") - 1;
334156952Sume		    while (*cp == ' ' || *cp == '\t')
335156952Sume			    cp++;
336156952Sume		    if ((*cp == '\0') || (*cp == '\n'))
337156952Sume			    continue;
338156952Sume		    strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
339156952Sume		    statp->defdname[sizeof(statp->defdname) - 1] = '\0';
340156952Sume		    if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
341156952Sume			    *cp = '\0';
342156952Sume		    havesearch = 0;
343156952Sume		    continue;
344156952Sume		}
345156952Sume		/* set search list */
346156952Sume		if (MATCH(buf, "search")) {
347170244Sume		    if (haveenv)	/*%< skip if have from environ */
348156952Sume			    continue;
349156952Sume		    cp = buf + sizeof("search") - 1;
350156952Sume		    while (*cp == ' ' || *cp == '\t')
351156952Sume			    cp++;
352156952Sume		    if ((*cp == '\0') || (*cp == '\n'))
353156952Sume			    continue;
354156952Sume		    strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
355156952Sume		    statp->defdname[sizeof(statp->defdname) - 1] = '\0';
356156952Sume		    if ((cp = strchr(statp->defdname, '\n')) != NULL)
357156952Sume			    *cp = '\0';
358156952Sume		    /*
359156952Sume		     * Set search list to be blank-separated strings
360156952Sume		     * on rest of line.
361156952Sume		     */
362156952Sume		    cp = statp->defdname;
363156952Sume		    pp = statp->dnsrch;
364156952Sume		    *pp++ = cp;
365156952Sume		    for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
366156952Sume			    if (*cp == ' ' || *cp == '\t') {
367156952Sume				    *cp = 0;
368156952Sume				    n = 1;
369156952Sume			    } else if (n) {
370156952Sume				    *pp++ = cp;
371156952Sume				    n = 0;
372156952Sume			    }
373156952Sume		    }
374156952Sume		    /* null terminate last domain if there are excess */
375156952Sume		    while (*cp != '\0' && *cp != ' ' && *cp != '\t')
376156952Sume			    cp++;
377156952Sume		    *cp = '\0';
378156952Sume		    *pp++ = 0;
379156952Sume		    havesearch = 1;
380156952Sume		    continue;
381156952Sume		}
382156952Sume		/* read nameservers to query */
383174226Sume		if (MATCH(buf, "nameserver") && nserv < maxns) {
384156952Sume		    struct addrinfo hints, *ai;
385156952Sume		    char sbuf[NI_MAXSERV];
386156952Sume		    const size_t minsiz =
387156952Sume		        sizeof(statp->_u._ext.ext->nsaddrs[0]);
388156952Sume
389156952Sume		    cp = buf + sizeof("nameserver") - 1;
390156952Sume		    while (*cp == ' ' || *cp == '\t')
391156952Sume			cp++;
392156952Sume		    cp[strcspn(cp, ";# \t\n")] = '\0';
393156952Sume		    if ((*cp != '\0') && (*cp != '\n')) {
394156952Sume			memset(&hints, 0, sizeof(hints));
395156952Sume			hints.ai_family = PF_UNSPEC;
396156952Sume			hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
397156952Sume			hints.ai_flags = AI_NUMERICHOST;
398156952Sume			sprintf(sbuf, "%u", NAMESERVER_PORT);
399156952Sume			if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
400156952Sume			    ai->ai_addrlen <= minsiz) {
401156952Sume			    if (statp->_u._ext.ext != NULL) {
402156952Sume				memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
403156952Sume				    ai->ai_addr, ai->ai_addrlen);
404156952Sume			    }
405156952Sume			    if (ai->ai_addrlen <=
406156952Sume			        sizeof(statp->nsaddr_list[nserv])) {
407156952Sume				memcpy(&statp->nsaddr_list[nserv],
408156952Sume				    ai->ai_addr, ai->ai_addrlen);
409156952Sume			    } else
410156952Sume				statp->nsaddr_list[nserv].sin_family = 0;
411156952Sume			    freeaddrinfo(ai);
412156952Sume			    nserv++;
413156952Sume			}
414156952Sume		    }
415156952Sume		    continue;
416156952Sume		}
417156952Sume#ifdef RESOLVSORT
418156952Sume		if (MATCH(buf, "sortlist")) {
419156952Sume		    struct in_addr a;
420156956Sume		    struct in6_addr a6;
421156956Sume		    int m, i;
422156956Sume		    u_char *u;
423156956Sume		    struct __res_state_ext *ext = statp->_u._ext.ext;
424156952Sume
425156952Sume		    cp = buf + sizeof("sortlist") - 1;
426156952Sume		    while (nsort < MAXRESOLVSORT) {
427156952Sume			while (*cp == ' ' || *cp == '\t')
428156952Sume			    cp++;
429156952Sume			if (*cp == '\0' || *cp == '\n' || *cp == ';')
430156952Sume			    break;
431156952Sume			net = cp;
432156952Sume			while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
433156952Sume			       isascii(*cp) && !isspace((unsigned char)*cp))
434156952Sume				cp++;
435156952Sume			n = *cp;
436156952Sume			*cp = 0;
437156952Sume			if (inet_aton(net, &a)) {
438156952Sume			    statp->sort_list[nsort].addr = a;
439156952Sume			    if (ISSORTMASK(n)) {
440156952Sume				*cp++ = n;
441156952Sume				net = cp;
442156952Sume				while (*cp && *cp != ';' &&
443156952Sume					isascii(*cp) &&
444156952Sume					!isspace((unsigned char)*cp))
445156952Sume				    cp++;
446156952Sume				n = *cp;
447156952Sume				*cp = 0;
448156952Sume				if (inet_aton(net, &a)) {
449156952Sume				    statp->sort_list[nsort].mask = a.s_addr;
450156952Sume				} else {
451156952Sume				    statp->sort_list[nsort].mask =
452156952Sume					net_mask(statp->sort_list[nsort].addr);
453156952Sume				}
454156952Sume			    } else {
455156952Sume				statp->sort_list[nsort].mask =
456156952Sume				    net_mask(statp->sort_list[nsort].addr);
457156952Sume			    }
458156956Sume			    ext->sort_list[nsort].af = AF_INET;
459156956Sume			    ext->sort_list[nsort].addr.ina =
460156956Sume				statp->sort_list[nsort].addr;
461156956Sume			    ext->sort_list[nsort].mask.ina.s_addr =
462156956Sume				statp->sort_list[nsort].mask;
463156952Sume			    nsort++;
464156952Sume			}
465156956Sume			else if (inet_pton(AF_INET6, net, &a6) == 1) {
466156956Sume
467156956Sume			    ext->sort_list[nsort].af = AF_INET6;
468156956Sume			    ext->sort_list[nsort].addr.in6a = a6;
469156956Sume			    u = (u_char *)&ext->sort_list[nsort].mask.in6a;
470156956Sume			    *cp++ = n;
471156956Sume			    net = cp;
472156956Sume			    while (*cp && *cp != ';' &&
473156956Sume				    isascii(*cp) && !isspace(*cp))
474156956Sume				cp++;
475156956Sume			    m = n;
476156956Sume			    n = *cp;
477156956Sume			    *cp = 0;
478156956Sume			    switch (m) {
479156956Sume			    case '/':
480156956Sume				m = atoi(net);
481156956Sume				break;
482156956Sume			    case '&':
483156956Sume				if (inet_pton(AF_INET6, net, u) == 1) {
484156956Sume				    m = -1;
485156956Sume				    break;
486156956Sume				}
487156956Sume				/*FALLTHROUGH*/
488156956Sume			    default:
489156956Sume				m = sizeof(struct in6_addr) * CHAR_BIT;
490156956Sume				break;
491156956Sume			    }
492156956Sume			    if (m >= 0) {
493156956Sume				for (i = 0; i < sizeof(struct in6_addr); i++) {
494156956Sume				    if (m <= 0) {
495156956Sume					*u = 0;
496156956Sume				    } else {
497156956Sume					m -= CHAR_BIT;
498156956Sume					*u = (u_char)~0;
499156956Sume					if (m < 0)
500156956Sume					    *u <<= -m;
501156956Sume				    }
502156956Sume				    u++;
503156956Sume				}
504156956Sume			    }
505156956Sume			    statp->sort_list[nsort].addr.s_addr =
506156956Sume				(u_int32_t)0xffffffff;
507156956Sume			    statp->sort_list[nsort].mask =
508156956Sume				(u_int32_t)0xffffffff;
509156956Sume			    nsort++;
510156956Sume			}
511156952Sume			*cp = n;
512156952Sume		    }
513156952Sume		    continue;
514156952Sume		}
515156952Sume#endif
516156952Sume		if (MATCH(buf, "options")) {
517156952Sume		    res_setoptions(statp, buf + sizeof("options") - 1, "conf");
518156952Sume		    continue;
519156952Sume		}
520156952Sume	    }
521156952Sume	    if (nserv > 0)
522156952Sume		statp->nscount = nserv;
523156952Sume#ifdef RESOLVSORT
524156952Sume	    statp->nsort = nsort;
525156952Sume#endif
526156952Sume	    (void) fclose(fp);
527156952Sume	}
528156952Sume/*
529156952Sume * Last chance to get a nameserver.  This should not normally
530156952Sume * be necessary
531156952Sume */
532156952Sume#ifdef NO_RESOLV_CONF
533156952Sume	if(nserv == 0)
534156952Sume		nserv = get_nameservers(statp);
535156952Sume#endif
536156952Sume
537156952Sume	if (statp->defdname[0] == 0 &&
538156952Sume	    gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
539156952Sume	    (cp = strchr(buf, '.')) != NULL)
540156952Sume		strcpy(statp->defdname, cp + 1);
541156952Sume
542156952Sume	/* find components of local domain that might be searched */
543156952Sume	if (havesearch == 0) {
544156952Sume		pp = statp->dnsrch;
545156952Sume		*pp++ = statp->defdname;
546156952Sume		*pp = NULL;
547156952Sume
548156952Sume		dots = 0;
549156952Sume		for (cp = statp->defdname; *cp; cp++)
550156952Sume			dots += (*cp == '.');
551156952Sume
552156952Sume		cp = statp->defdname;
553156952Sume		while (pp < statp->dnsrch + MAXDFLSRCH) {
554156952Sume			if (dots < LOCALDOMAINPARTS)
555156952Sume				break;
556170244Sume			cp = strchr(cp, '.') + 1;    /*%< we know there is one */
557156952Sume			*pp++ = cp;
558156952Sume			dots--;
559156952Sume		}
560156952Sume		*pp = NULL;
561156952Sume#ifdef DEBUG
562156952Sume		if (statp->options & RES_DEBUG) {
563156952Sume			printf(";; res_init()... default dnsrch list:\n");
564156952Sume			for (pp = statp->dnsrch; *pp; pp++)
565156952Sume				printf(";;\t%s\n", *pp);
566156952Sume			printf(";;\t..END..\n");
567156952Sume		}
568156952Sume#endif
569156952Sume	}
570156952Sume
571156956Sume	if (issetugid())
572156956Sume		statp->options |= RES_NOALIASES;
573156956Sume	else if ((cp = getenv("RES_OPTIONS")) != NULL)
574156952Sume		res_setoptions(statp, cp, "env");
575156952Sume	statp->options |= RES_INIT;
576174226Sume	return (statp->res_h_errno);
577156952Sume}
578156952Sume
579156952Sumestatic void
580156952Sumeres_setoptions(res_state statp, const char *options, const char *source)
581156952Sume{
582156952Sume	const char *cp = options;
583156952Sume	int i;
584156956Sume#ifndef _LIBC
585156952Sume	struct __res_state_ext *ext = statp->_u._ext.ext;
586156956Sume#endif
587156952Sume
588156952Sume#ifdef DEBUG
589156952Sume	if (statp->options & RES_DEBUG)
590156952Sume		printf(";; res_setoptions(\"%s\", \"%s\")...\n",
591156952Sume		       options, source);
592156952Sume#endif
593156952Sume	while (*cp) {
594156952Sume		/* skip leading and inner runs of spaces */
595156952Sume		while (*cp == ' ' || *cp == '\t')
596156952Sume			cp++;
597156952Sume		/* search for and process individual options */
598156952Sume		if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
599156952Sume			i = atoi(cp + sizeof("ndots:") - 1);
600156952Sume			if (i <= RES_MAXNDOTS)
601156952Sume				statp->ndots = i;
602156952Sume			else
603156952Sume				statp->ndots = RES_MAXNDOTS;
604156952Sume#ifdef DEBUG
605156952Sume			if (statp->options & RES_DEBUG)
606156952Sume				printf(";;\tndots=%d\n", statp->ndots);
607156952Sume#endif
608156952Sume		} else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
609156952Sume			i = atoi(cp + sizeof("timeout:") - 1);
610156952Sume			if (i <= RES_MAXRETRANS)
611156952Sume				statp->retrans = i;
612156952Sume			else
613156952Sume				statp->retrans = RES_MAXRETRANS;
614156952Sume#ifdef DEBUG
615156952Sume			if (statp->options & RES_DEBUG)
616156952Sume				printf(";;\ttimeout=%d\n", statp->retrans);
617156952Sume#endif
618156952Sume#ifdef	SOLARIS2
619156952Sume		} else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) {
620156952Sume			/*
621156952Sume		 	 * For backward compatibility, 'retrans' is
622156952Sume		 	 * supported as an alias for 'timeout', though
623156952Sume		 	 * without an imposed maximum.
624156952Sume		 	 */
625156952Sume			statp->retrans = atoi(cp + sizeof("retrans:") - 1);
626156952Sume		} else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){
627156952Sume			/*
628156952Sume			 * For backward compatibility, 'retry' is
629156952Sume			 * supported as an alias for 'attempts', though
630156952Sume			 * without an imposed maximum.
631156952Sume			 */
632156952Sume			statp->retry = atoi(cp + sizeof("retry:") - 1);
633156952Sume#endif	/* SOLARIS2 */
634156952Sume		} else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
635156952Sume			i = atoi(cp + sizeof("attempts:") - 1);
636156952Sume			if (i <= RES_MAXRETRY)
637156952Sume				statp->retry = i;
638156952Sume			else
639156952Sume				statp->retry = RES_MAXRETRY;
640156952Sume#ifdef DEBUG
641156952Sume			if (statp->options & RES_DEBUG)
642156952Sume				printf(";;\tattempts=%d\n", statp->retry);
643156952Sume#endif
644156952Sume		} else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
645156952Sume#ifdef DEBUG
646156952Sume			if (!(statp->options & RES_DEBUG)) {
647156952Sume				printf(";; res_setoptions(\"%s\", \"%s\")..\n",
648156952Sume				       options, source);
649156952Sume				statp->options |= RES_DEBUG;
650156952Sume			}
651156952Sume			printf(";;\tdebug\n");
652156952Sume#endif
653156952Sume		} else if (!strncmp(cp, "no_tld_query",
654156952Sume				    sizeof("no_tld_query") - 1) ||
655156952Sume			   !strncmp(cp, "no-tld-query",
656156952Sume				    sizeof("no-tld-query") - 1)) {
657156952Sume			statp->options |= RES_NOTLDQUERY;
658156952Sume		} else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
659156952Sume			statp->options |= RES_USE_INET6;
660156956Sume		} else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) {
661156956Sume		       statp->options |= RES_INSECURE1;
662156956Sume		} else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) {
663156956Sume		       statp->options |= RES_INSECURE2;
664156952Sume		} else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
665156952Sume			statp->options |= RES_ROTATE;
666156952Sume		} else if (!strncmp(cp, "no-check-names",
667156952Sume				    sizeof("no-check-names") - 1)) {
668156952Sume			statp->options |= RES_NOCHECKNAME;
669156952Sume		}
670156952Sume#ifdef RES_USE_EDNS0
671156952Sume		else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
672156952Sume			statp->options |= RES_USE_EDNS0;
673156952Sume		}
674156952Sume#endif
675156956Sume#ifndef _LIBC
676156952Sume		else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
677156952Sume			statp->options |= RES_USE_DNAME;
678156952Sume		}
679156952Sume		else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
680156952Sume			if (ext == NULL)
681156952Sume				goto skip;
682156952Sume			cp += sizeof("nibble:") - 1;
683156952Sume			i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
684156952Sume			strncpy(ext->nsuffix, cp, i);
685156952Sume			ext->nsuffix[i] = '\0';
686156952Sume		}
687156952Sume		else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
688156952Sume			if (ext == NULL)
689156952Sume				goto skip;
690156952Sume			cp += sizeof("nibble2:") - 1;
691156952Sume			i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
692156952Sume			strncpy(ext->nsuffix2, cp, i);
693156952Sume			ext->nsuffix2[i] = '\0';
694156952Sume		}
695156952Sume		else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
696156952Sume			cp += sizeof("v6revmode:") - 1;
697156952Sume			/* "nibble" and "bitstring" used to be valid */
698156952Sume			if (!strncmp(cp, "single", sizeof("single") - 1)) {
699156952Sume				statp->options |= RES_NO_NIBBLE2;
700156952Sume			} else if (!strncmp(cp, "both", sizeof("both") - 1)) {
701156952Sume				statp->options &=
702156952Sume					 ~RES_NO_NIBBLE2;
703156952Sume			}
704156952Sume		}
705156956Sume#endif
706156952Sume		else {
707156952Sume			/* XXX - print a warning here? */
708156952Sume		}
709156956Sume#ifndef _LIBC
710156952Sume   skip:
711156956Sume#endif
712156952Sume		/* skip to next run of spaces */
713156952Sume		while (*cp && *cp != ' ' && *cp != '\t')
714156952Sume			cp++;
715156952Sume	}
716156952Sume}
717156952Sume
718156952Sume#ifdef RESOLVSORT
719156952Sume/* XXX - should really support CIDR which means explicit masks always. */
720156952Sumestatic u_int32_t
721170244Sumenet_mask(in)		/*!< XXX - should really use system's version of this  */
722156952Sume	struct in_addr in;
723156952Sume{
724156956Sume	u_int32_t i = ntohl(in.s_addr);
725156952Sume
726156952Sume	if (IN_CLASSA(i))
727156952Sume		return (htonl(IN_CLASSA_NET));
728156952Sume	else if (IN_CLASSB(i))
729156952Sume		return (htonl(IN_CLASSB_NET));
730156952Sume	return (htonl(IN_CLASSC_NET));
731156952Sume}
732156952Sume#endif
733156952Sume
734269867Sumestatic u_char srnd[16];
735269867Sume
736269867Sumevoid
737269867Sumeres_rndinit(res_state statp)
738269867Sume{
739269867Sume	struct timeval now;
740269867Sume	u_int32_t u32;
741269867Sume	u_int16_t u16;
742269867Sume	u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd;
743269867Sume
744269867Sume	gettimeofday(&now, NULL);
745269867Sume	u32 = now.tv_sec;
746269867Sume	memcpy(rnd, &u32, 4);
747269867Sume	u32 = now.tv_usec;
748269867Sume	memcpy(rnd + 4, &u32, 4);
749269867Sume	u32 += now.tv_sec;
750269867Sume	memcpy(rnd + 8, &u32, 4);
751269867Sume	u16 = getpid();
752269867Sume	memcpy(rnd + 12, &u16, 2);
753269867Sume}
754269867Sume
755156952Sumeu_int
756269867Sumeres_nrandomid(res_state statp) {
757156952Sume	struct timeval now;
758269867Sume	u_int16_t u16;
759269867Sume	MD5_CTX ctx;
760269867Sume	u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd;
761156952Sume
762156952Sume	gettimeofday(&now, NULL);
763269867Sume	u16 = (u_int16_t) (now.tv_sec ^ now.tv_usec);
764269867Sume	memcpy(rnd + 14, &u16, 2);
765269867Sume#ifndef HAVE_MD5
766269867Sume	MD5_Init(&ctx);
767269867Sume	MD5_Update(&ctx, rnd, 16);
768269867Sume	MD5_Final(rnd, &ctx);
769269867Sume#else
770269867Sume	MD5Init(&ctx);
771269867Sume	MD5Update(&ctx, rnd, 16);
772269867Sume	MD5Final(rnd, &ctx);
773269867Sume#endif
774269867Sume	memcpy(&u16, rnd + 14, 2);
775269867Sume	return ((u_int) u16);
776156952Sume}
777156952Sume
778170244Sume/*%
779156952Sume * This routine is for closing the socket if a virtual circuit is used and
780156952Sume * the program wants to close it.  This provides support for endhostent()
781156952Sume * which expects to close the socket.
782156952Sume *
783156952Sume * This routine is not expected to be user visible.
784156952Sume */
785156952Sumevoid
786156952Sumeres_nclose(res_state statp) {
787156952Sume	int ns;
788156952Sume
789156952Sume	if (statp->_vcsock >= 0) {
790160965Sume		(void) _close(statp->_vcsock);
791156952Sume		statp->_vcsock = -1;
792156952Sume		statp->_flags &= ~(RES_F_VC | RES_F_CONN);
793156952Sume	}
794156952Sume	for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
795156952Sume		if (statp->_u._ext.nssocks[ns] != -1) {
796160965Sume			(void) _close(statp->_u._ext.nssocks[ns]);
797156952Sume			statp->_u._ext.nssocks[ns] = -1;
798156952Sume		}
799156952Sume	}
800156952Sume}
801156952Sume
802156952Sumevoid
803156952Sumeres_ndestroy(res_state statp) {
804156952Sume	res_nclose(statp);
805269867Sume	if (statp->_u._ext.ext != NULL) {
806156952Sume		free(statp->_u._ext.ext);
807269867Sume		statp->_u._ext.ext = NULL;
808269867Sume	}
809269867Sume	if (statp->_rnd != NULL) {
810269867Sume		free(statp->_rnd);
811269867Sume		statp->_rnd = NULL;
812269867Sume	}
813156952Sume	statp->options &= ~RES_INIT;
814156952Sume}
815156952Sume
816156956Sume#ifndef _LIBC
817156952Sumeconst char *
818156952Sumeres_get_nibblesuffix(res_state statp) {
819156952Sume	if (statp->_u._ext.ext)
820156952Sume		return (statp->_u._ext.ext->nsuffix);
821156952Sume	return ("ip6.arpa");
822156952Sume}
823156952Sume
824156952Sumeconst char *
825156952Sumeres_get_nibblesuffix2(res_state statp) {
826156952Sume	if (statp->_u._ext.ext)
827156952Sume		return (statp->_u._ext.ext->nsuffix2);
828156952Sume	return ("ip6.int");
829156952Sume}
830156956Sume#endif
831156952Sume
832156952Sumevoid
833156952Sumeres_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
834156952Sume	int i, nserv;
835156952Sume	size_t size;
836156952Sume
837156952Sume	/* close open servers */
838156952Sume	res_nclose(statp);
839156952Sume
840156952Sume	/* cause rtt times to be forgotten */
841156952Sume	statp->_u._ext.nscount = 0;
842156952Sume
843156952Sume	nserv = 0;
844156952Sume	for (i = 0; i < cnt && nserv < MAXNS; i++) {
845156952Sume		switch (set->sin.sin_family) {
846156952Sume		case AF_INET:
847156952Sume			size = sizeof(set->sin);
848156952Sume			if (statp->_u._ext.ext)
849156952Sume				memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
850156952Sume					&set->sin, size);
851156952Sume			if (size <= sizeof(statp->nsaddr_list[nserv]))
852156952Sume				memcpy(&statp->nsaddr_list[nserv],
853156952Sume					&set->sin, size);
854156952Sume			else
855156952Sume				statp->nsaddr_list[nserv].sin_family = 0;
856156952Sume			nserv++;
857156952Sume			break;
858156952Sume
859156952Sume#ifdef HAS_INET6_STRUCTS
860156952Sume		case AF_INET6:
861156952Sume			size = sizeof(set->sin6);
862156952Sume			if (statp->_u._ext.ext)
863156952Sume				memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
864156952Sume					&set->sin6, size);
865156952Sume			if (size <= sizeof(statp->nsaddr_list[nserv]))
866156952Sume				memcpy(&statp->nsaddr_list[nserv],
867156952Sume					&set->sin6, size);
868156952Sume			else
869156952Sume				statp->nsaddr_list[nserv].sin_family = 0;
870156952Sume			nserv++;
871156952Sume			break;
872156952Sume#endif
873156952Sume
874156952Sume		default:
875156952Sume			break;
876156952Sume		}
877156952Sume		set++;
878156952Sume	}
879156952Sume	statp->nscount = nserv;
880156952Sume
881156952Sume}
882156952Sume
883156952Sumeint
884156952Sumeres_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
885156952Sume	int i;
886156952Sume	size_t size;
887156952Sume	u_int16_t family;
888156952Sume
889156952Sume	for (i = 0; i < statp->nscount && i < cnt; i++) {
890156952Sume		if (statp->_u._ext.ext)
891156952Sume			family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
892156952Sume		else
893156952Sume			family = statp->nsaddr_list[i].sin_family;
894156952Sume
895156952Sume		switch (family) {
896156952Sume		case AF_INET:
897156952Sume			size = sizeof(set->sin);
898156952Sume			if (statp->_u._ext.ext)
899156952Sume				memcpy(&set->sin,
900156952Sume				       &statp->_u._ext.ext->nsaddrs[i],
901156952Sume				       size);
902156952Sume			else
903156952Sume				memcpy(&set->sin, &statp->nsaddr_list[i],
904156952Sume				       size);
905156952Sume			break;
906156952Sume
907156952Sume#ifdef HAS_INET6_STRUCTS
908156952Sume		case AF_INET6:
909156952Sume			size = sizeof(set->sin6);
910156952Sume			if (statp->_u._ext.ext)
911156952Sume				memcpy(&set->sin6,
912156952Sume				       &statp->_u._ext.ext->nsaddrs[i],
913156952Sume				       size);
914156952Sume			else
915156952Sume				memcpy(&set->sin6, &statp->nsaddr_list[i],
916156952Sume				       size);
917156952Sume			break;
918156952Sume#endif
919156952Sume
920156952Sume		default:
921156952Sume			set->sin.sin_family = 0;
922156952Sume			break;
923156952Sume		}
924156952Sume		set++;
925156952Sume	}
926156952Sume	return (statp->nscount);
927156952Sume}
928170244Sume
929170244Sume/*! \file */
930