xref: /illumos-gate/usr/src/lib/libresolv/res_init.c (revision 948f2876ce2a3010558f4f6937e16086ebcd36f2)
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 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 /*
32  * University Copyright- Copyright (c) 1982, 1986, 1988
33  * The Regents of the University of California
34  * All Rights Reserved
35  *
36  * University Acknowledgment- Portions of this document are derived from
37  * software developed by the University of California, Berkeley, and its
38  * contributors.
39  */
40 
41 #pragma ident	"%Z%%M%	%I%	%E% SMI"
42 
43 #include "c_synonyms.h"
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 int
86 res_init(void)
87 {
88 	register FILE *fp;
89 	register char *cp, **pp;
90 	register int n;
91 	char buf[BUFSIZ];
92 #ifdef SYSV
93 	extern char *strchr();
94 #else
95 	extern char *index();
96 #endif
97 	extern char *strcpy(), *strncpy();
98 	extern char *getenv();
99 	int nserv = 0;    /* number of nameserver records read from file */
100 	int haveenv = 0;
101 	int havesearch = 0;
102 
103 	_res.nsaddr.sin_addr.s_addr =  htonl(INADDR_LOOPBACK); /* INADDR_ANY */
104 	_res.nsaddr.sin_family = AF_INET;
105 	_res.nsaddr.sin_port = htons(NAMESERVER_PORT);
106 	_res.nscount = 1;
107 
108 #ifdef SIOCGIFNUM
109 	{	int numifs, s, n, int_up;
110 		struct ifconf ifc;
111 		register struct ifreq *ifrp;
112 		struct ifreq ifr;
113 		unsigned bufsize;
114 		unsigned int flags;
115 		char *buf;
116 		extern void *malloc();
117 
118 		if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
119 			perror("socket");
120 			return (-1);
121 		}
122 		if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
123 			numifs = MAXIFS;
124 		}
125 		bufsize = numifs * sizeof (struct ifreq);
126 		buf = (char *)malloc(bufsize);
127 		if (buf == NULL) {
128 			perror("out of memory");
129 			close(s);
130 			return (-1);
131 		}
132 		ifc.ifc_len = bufsize;
133 		ifc.ifc_buf = buf;
134 		if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
135 			perror("ifconfig: SIOCGIFCONF");
136 			close(s);
137 			free(buf);
138 			return (-1);
139 		}
140 
141 		int_up = 0;
142 		ifrp = ifc.ifc_req;
143 		for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0;
144 								n--, ifrp++) {
145 			memset((void *) &ifr, 0, sizeof (ifr));
146 			strncpy(ifr.ifr_name, ifrp->ifr_name,
147 							sizeof (ifr.ifr_name));
148 			if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) < 0) {
149 				perror("SIOCGIFFLAGS");
150 				close(s);
151 				free(buf);
152 				return (-1);
153 			}
154 			flags = ifr.ifr_flags;
155 			/* we are looking for a non-loopback interface */
156 			if ((flags & IFF_UP) && ((flags & IFF_LOOPBACK) == 0))
157 				int_up = 1;
158 		}
159 		close(s);
160 		free(buf);
161 		if (int_up == 0) /* all the non-LOOPBACK interfaces are DOWN */
162 			return (-1);
163 	}
164 #endif /* SIOCGIFNUM */
165 
166 
167 	/*
168 	 * for the benefit of hidden NIS domains, we use the same procedure
169 	 * as sendmail: convert leading + to dot, then drop to first dot
170 	 */
171 	getdomainname(buf, BUFSIZ);
172 	if (buf[0] == '+')
173 	buf[0] = '.';
174 #ifdef SYSV
175 	cp = strchr(buf, (int)'.');
176 #else
177 	cp = index(buf, '.');
178 #endif
179 	if (cp == NULL)
180 		strcpy(_res.defdname, buf);
181 	else
182 		strcpy(_res.defdname, cp+1);
183 
184 	/* Allow user to override the local domain definition */
185 	if ((cp = getenv("LOCALDOMAIN")) != NULL) {
186 	(void) strncpy(_res.defdname, cp, sizeof (_res.defdname));
187 	haveenv++;
188 	}
189 
190 	if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
191 	/* read the config file */
192 	while (fgets(buf, sizeof (buf), fp) != NULL) {
193 	    /* read default domain name */
194 	    if (!strncmp(buf, "domain", sizeof ("domain") - 1)) {
195 		if (haveenv)	/* skip if have from environ */
196 			    continue;
197 		cp = buf + sizeof ("domain") - 1;
198 		while (*cp == ' ' || *cp == '\t')
199 		    cp++;
200 		if ((*cp == '\0') || (*cp == '\n'))
201 		    continue;
202 		(void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1);
203 #ifdef SYSV
204 		if ((cp = strchr(_res.defdname, (int)'\n')) != NULL)
205 #else
206 		if ((cp = index(_res.defdname, '\n')) != NULL)
207 #endif
208 		    *cp = '\0';
209 		havesearch = 0;
210 		continue;
211 	    }
212 	    /* set search list */
213 	    if (!strncmp(buf, "search", sizeof ("search") - 1)) {
214 		if (haveenv)	/* skip if have from environ */
215 		    continue;
216 		cp = buf + sizeof ("search") - 1;
217 		while (*cp == ' ' || *cp == '\t')
218 		    cp++;
219 		if ((*cp == '\0') || (*cp == '\n'))
220 		    continue;
221 		(void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1);
222 #ifdef SYSV
223 		if ((cp = strchr(_res.defdname, (int)'\n')) != NULL)
224 #else
225 		if ((cp = index(_res.defdname, '\n')) != NULL)
226 #endif
227 		    *cp = '\0';
228 		/*
229 		 * Set search list to be blank-separated strings
230 		 * on rest of line.
231 		 */
232 		cp = _res.defdname;
233 		pp = _res.dnsrch;
234 		*pp++ = cp;
235 		for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
236 		    if (*cp == ' ' || *cp == '\t') {
237 			*cp = 0;
238 			n = 1;
239 		    } else if (n) {
240 			*pp++ = cp;
241 			n = 0;
242 		    }
243 		}
244 		/* null terminate last domain if there are excess */
245 		while (*cp != '\0' && *cp != ' ' && *cp != '\t')
246 		    cp++;
247 		*cp = '\0';
248 		*pp++ = 0;
249 		havesearch = 1;
250 		continue;
251 	    }
252 	    /* read nameservers to query */
253 	    if (!strncmp(buf, "nameserver", sizeof ("nameserver") - 1) &&
254 		(nserv < MAXNS)) {
255 		cp = buf + sizeof ("nameserver") - 1;
256 		while (*cp == ' ' || *cp == '\t')
257 		    cp++;
258 		if ((*cp == '\0') || (*cp == '\n'))
259 		    continue;
260 		if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
261 				inet_addr(cp)) == (unsigned) -1) {
262 		    _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
263 		    continue;
264 		}
265 		_res.nsaddr_list[nserv].sin_family = AF_INET;
266 		_res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
267 		nserv++;
268 		continue;
269 	    }
270 	}
271 	if (nserv > 1)
272 	    _res.nscount = nserv;
273 	(void) fclose(fp);
274 	}
275 	if (_res.defdname[0] == 0) {
276 	if (gethostname(buf, sizeof (_res.defdname)) == 0 &&
277 #ifdef SYSV
278 	    (cp = strchr(buf, (int)'.')))
279 #else
280 	    (cp = index(buf, '.')))
281 #endif
282 		(void) strcpy(_res.defdname, cp + 1);
283 	}
284 
285 	/* find components of local domain that might be searched */
286 	if (havesearch == 0) {
287 	pp = _res.dnsrch;
288 	*pp++ = _res.defdname;
289 	for (cp = _res.defdname, n = 0; *cp; cp++)
290 	    if (*cp == '.')
291 		n++;
292 	cp = _res.defdname;
293 	for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; n--) {
294 #ifdef SYSV
295 	    cp = strchr(cp, (int)'.');
296 #else
297 	    cp = index(cp, '.');
298 #endif
299 	    *pp++ = ++cp;
300 	}
301 	*pp++ = 0;
302 	}
303 	_res.options |= RES_INIT;
304 	return (0);
305 }
306