xref: /titanic_44/usr/src/lib/libresolv/res_init.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include "synonyms.h"
43 
44 #include <sys/types.h>
45 #include <sys/sockio.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <stdio.h>
49 #include <arpa/nameser.h>
50 #include <resolv.h>
51 
52 #include <netinet/in.h>
53 #include <net/if.h>
54 #include <netinet/if_ether.h>
55 #include <arpa/inet.h>
56 
57 #define	MAXIFS	256
58 
59 /*
60  * Resolver state default settings
61  */
62 
63 struct state _res = {
64 	RES_TIMEOUT,		/* retransmition time interval */
65 	4,				/* number of times to retransmit */
66 	RES_DEFAULT,		/* options flags */
67 	1,				/* number of name servers */
68 };
69 
70 /*
71  * Set up default settings.  If the configuration file exist, the values
72  * there will have precedence.  Otherwise, the server address is set to
73  * INADDR_LOOPBACK and the default domain name comes from the gethostname().
74  * BUT if the NIS/RPC domain name is set, that is used if all else fails.
75  *
76  * The configuration file should only be used if you want to redefine your
77  * domain or run without a server on your machine.
78  *
79  * Note the user can always override then domain name with the environment
80  * variable LOCALDOMAIN which has absolute priority.
81  *
82  *
83  * Return 0 if completes successfully, -1 on error
84  */
85 res_init()
86 {
87 	register FILE *fp;
88 	register char *cp, **pp;
89 	register int n;
90 	char buf[BUFSIZ];
91 #ifdef SYSV
92 	extern char *strchr();
93 #else
94 	extern char *index();
95 #endif
96 	extern char *strcpy(), *strncpy();
97 	extern char *getenv();
98 	int nserv = 0;    /* number of nameserver records read from file */
99 	int haveenv = 0;
100 	int havesearch = 0;
101 
102 	_res.nsaddr.sin_addr.s_addr =  htonl(INADDR_LOOPBACK); /* INADDR_ANY */
103 	_res.nsaddr.sin_family = AF_INET;
104 	_res.nsaddr.sin_port = htons(NAMESERVER_PORT);
105 	_res.nscount = 1;
106 
107 #ifdef SIOCGIFNUM
108 	{	int numifs, s, n, int_up;
109 		struct ifconf ifc;
110 		register struct ifreq *ifrp;
111 		struct ifreq ifr;
112 		unsigned bufsize;
113 		unsigned int flags;
114 		char *buf;
115 		extern void *malloc();
116 
117 		if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
118 			perror("socket");
119 			return (-1);
120 		}
121 		if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
122 			numifs = MAXIFS;
123 		}
124 		bufsize = numifs * sizeof (struct ifreq);
125 		buf = (char *)malloc(bufsize);
126 		if (buf == NULL) {
127 			perror("out of memory");
128 			close(s);
129 			return (-1);
130 		}
131 		ifc.ifc_len = bufsize;
132 		ifc.ifc_buf = buf;
133 		if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
134 			perror("ifconfig: SIOCGIFCONF");
135 			close(s);
136 			free(buf);
137 			return (-1);
138 		}
139 
140 		int_up = 0;
141 		ifrp = ifc.ifc_req;
142 		for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0;
143 								n--, ifrp++) {
144 			memset((void *) &ifr, 0, sizeof (ifr));
145 			strncpy(ifr.ifr_name, ifrp->ifr_name,
146 							sizeof (ifr.ifr_name));
147 			if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) < 0) {
148 				perror("SIOCGIFFLAGS");
149 				close(s);
150 				free(buf);
151 				return (-1);
152 			}
153 			flags = ifr.ifr_flags;
154 			/* we are looking for a non-loopback interface */
155 			if ((flags & IFF_UP) && ((flags & IFF_LOOPBACK) == 0))
156 				int_up = 1;
157 		}
158 		close(s);
159 		free(buf);
160 		if (int_up == 0) /* all the non-LOOPBACK interfaces are DOWN */
161 			return (-1);
162 	}
163 #endif /* SIOCGIFNUM */
164 
165 
166 	/*
167 	 * for the benefit of hidden NIS domains, we use the same procedure
168 	 * as sendmail: convert leading + to dot, then drop to first dot
169 	 */
170 	getdomainname(buf, BUFSIZ);
171 	if (buf[0] == '+')
172 	buf[0] = '.';
173 #ifdef SYSV
174 	cp = strchr(buf, (int)'.');
175 #else
176 	cp = index(buf, '.');
177 #endif
178 	if (cp == NULL)
179 		strcpy(_res.defdname, buf);
180 	else
181 		strcpy(_res.defdname, cp+1);
182 
183 	/* Allow user to override the local domain definition */
184 	if ((cp = getenv("LOCALDOMAIN")) != NULL) {
185 	(void) strncpy(_res.defdname, cp, sizeof (_res.defdname));
186 	haveenv++;
187 	}
188 
189 	if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
190 	/* read the config file */
191 	while (fgets(buf, sizeof (buf), fp) != NULL) {
192 	    /* read default domain name */
193 	    if (!strncmp(buf, "domain", sizeof ("domain") - 1)) {
194 		if (haveenv)	/* skip if have from environ */
195 			    continue;
196 		cp = buf + sizeof ("domain") - 1;
197 		while (*cp == ' ' || *cp == '\t')
198 		    cp++;
199 		if ((*cp == '\0') || (*cp == '\n'))
200 		    continue;
201 		(void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1);
202 #ifdef SYSV
203 		if ((cp = strchr(_res.defdname, (int)'\n')) != NULL)
204 #else
205 		if ((cp = index(_res.defdname, '\n')) != NULL)
206 #endif
207 		    *cp = '\0';
208 		havesearch = 0;
209 		continue;
210 	    }
211 	    /* set search list */
212 	    if (!strncmp(buf, "search", sizeof ("search") - 1)) {
213 		if (haveenv)	/* skip if have from environ */
214 		    continue;
215 		cp = buf + sizeof ("search") - 1;
216 		while (*cp == ' ' || *cp == '\t')
217 		    cp++;
218 		if ((*cp == '\0') || (*cp == '\n'))
219 		    continue;
220 		(void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1);
221 #ifdef SYSV
222 		if ((cp = strchr(_res.defdname, (int)'\n')) != NULL)
223 #else
224 		if ((cp = index(_res.defdname, '\n')) != NULL)
225 #endif
226 		    *cp = '\0';
227 		/*
228 		 * Set search list to be blank-separated strings
229 		 * on rest of line.
230 		 */
231 		cp = _res.defdname;
232 		pp = _res.dnsrch;
233 		*pp++ = cp;
234 		for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
235 		    if (*cp == ' ' || *cp == '\t') {
236 			*cp = 0;
237 			n = 1;
238 		    } else if (n) {
239 			*pp++ = cp;
240 			n = 0;
241 		    }
242 		}
243 		/* null terminate last domain if there are excess */
244 		while (*cp != '\0' && *cp != ' ' && *cp != '\t')
245 		    cp++;
246 		*cp = '\0';
247 		*pp++ = 0;
248 		havesearch = 1;
249 		continue;
250 	    }
251 	    /* read nameservers to query */
252 	    if (!strncmp(buf, "nameserver", sizeof ("nameserver") - 1) &&
253 		(nserv < MAXNS)) {
254 		cp = buf + sizeof ("nameserver") - 1;
255 		while (*cp == ' ' || *cp == '\t')
256 		    cp++;
257 		if ((*cp == '\0') || (*cp == '\n'))
258 		    continue;
259 		if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
260 				inet_addr(cp)) == (unsigned) -1) {
261 		    _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
262 		    continue;
263 		}
264 		_res.nsaddr_list[nserv].sin_family = AF_INET;
265 		_res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
266 		nserv++;
267 		continue;
268 	    }
269 	}
270 	if (nserv > 1)
271 	    _res.nscount = nserv;
272 	(void) fclose(fp);
273 	}
274 	if (_res.defdname[0] == 0) {
275 	if (gethostname(buf, sizeof (_res.defdname)) == 0 &&
276 #ifdef SYSV
277 	    (cp = strchr(buf, (int)'.')))
278 #else
279 	    (cp = index(buf, '.')))
280 #endif
281 		(void) strcpy(_res.defdname, cp + 1);
282 	}
283 
284 	/* find components of local domain that might be searched */
285 	if (havesearch == 0) {
286 	pp = _res.dnsrch;
287 	*pp++ = _res.defdname;
288 	for (cp = _res.defdname, n = 0; *cp; cp++)
289 	    if (*cp == '.')
290 		n++;
291 	cp = _res.defdname;
292 	for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; n--) {
293 #ifdef SYSV
294 	    cp = strchr(cp, (int)'.');
295 #else
296 	    cp = index(cp, '.');
297 #endif
298 	    *pp++ = ++cp;
299 	}
300 	*pp++ = 0;
301 	}
302 	_res.options |= RES_INIT;
303 	return (0);
304 }
305