xref: /titanic_41/usr/src/lib/libresolv/res_init.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7257d1b4Sraf  * Common Development and Distribution License (the "License").
6*7257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21e8031f0aSraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
447c478bd9Sstevel@tonic-gate #include <sys/socket.h>
457c478bd9Sstevel@tonic-gate #include <netinet/in.h>
467c478bd9Sstevel@tonic-gate #include <stdio.h>
477c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
487c478bd9Sstevel@tonic-gate #include <resolv.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <netinet/in.h>
517c478bd9Sstevel@tonic-gate #include <net/if.h>
527c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
537c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	MAXIFS	256
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Resolver state default settings
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate struct state _res = {
627c478bd9Sstevel@tonic-gate 	RES_TIMEOUT,		/* retransmition time interval */
637c478bd9Sstevel@tonic-gate 	4,				/* number of times to retransmit */
647c478bd9Sstevel@tonic-gate 	RES_DEFAULT,		/* options flags */
657c478bd9Sstevel@tonic-gate 	1,				/* number of name servers */
667c478bd9Sstevel@tonic-gate };
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Set up default settings.  If the configuration file exist, the values
707c478bd9Sstevel@tonic-gate  * there will have precedence.  Otherwise, the server address is set to
717c478bd9Sstevel@tonic-gate  * INADDR_LOOPBACK and the default domain name comes from the gethostname().
727c478bd9Sstevel@tonic-gate  * BUT if the NIS/RPC domain name is set, that is used if all else fails.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * The configuration file should only be used if you want to redefine your
757c478bd9Sstevel@tonic-gate  * domain or run without a server on your machine.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * Note the user can always override then domain name with the environment
787c478bd9Sstevel@tonic-gate  * variable LOCALDOMAIN which has absolute priority.
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  * Return 0 if completes successfully, -1 on error
827c478bd9Sstevel@tonic-gate  */
836a1c6faaSanay int
res_init(void)846a1c6faaSanay res_init(void)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	register FILE *fp;
877c478bd9Sstevel@tonic-gate 	register char *cp, **pp;
887c478bd9Sstevel@tonic-gate 	register int n;
897c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
907c478bd9Sstevel@tonic-gate #ifdef SYSV
917c478bd9Sstevel@tonic-gate 	extern char *strchr();
927c478bd9Sstevel@tonic-gate #else
937c478bd9Sstevel@tonic-gate 	extern char *index();
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 	extern char *strcpy(), *strncpy();
967c478bd9Sstevel@tonic-gate 	extern char *getenv();
977c478bd9Sstevel@tonic-gate 	int nserv = 0;    /* number of nameserver records read from file */
987c478bd9Sstevel@tonic-gate 	int haveenv = 0;
997c478bd9Sstevel@tonic-gate 	int havesearch = 0;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	_res.nsaddr.sin_addr.s_addr =  htonl(INADDR_LOOPBACK); /* INADDR_ANY */
1027c478bd9Sstevel@tonic-gate 	_res.nsaddr.sin_family = AF_INET;
1037c478bd9Sstevel@tonic-gate 	_res.nsaddr.sin_port = htons(NAMESERVER_PORT);
1047c478bd9Sstevel@tonic-gate 	_res.nscount = 1;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #ifdef SIOCGIFNUM
1077c478bd9Sstevel@tonic-gate 	{	int numifs, s, n, int_up;
1087c478bd9Sstevel@tonic-gate 		struct ifconf ifc;
1097c478bd9Sstevel@tonic-gate 		register struct ifreq *ifrp;
1107c478bd9Sstevel@tonic-gate 		struct ifreq ifr;
1117c478bd9Sstevel@tonic-gate 		unsigned bufsize;
1127c478bd9Sstevel@tonic-gate 		unsigned int flags;
1137c478bd9Sstevel@tonic-gate 		char *buf;
1147c478bd9Sstevel@tonic-gate 		extern void *malloc();
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1177c478bd9Sstevel@tonic-gate 			perror("socket");
1187c478bd9Sstevel@tonic-gate 			return (-1);
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
1217c478bd9Sstevel@tonic-gate 			numifs = MAXIFS;
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 		bufsize = numifs * sizeof (struct ifreq);
1247c478bd9Sstevel@tonic-gate 		buf = (char *)malloc(bufsize);
1257c478bd9Sstevel@tonic-gate 		if (buf == NULL) {
1267c478bd9Sstevel@tonic-gate 			perror("out of memory");
1277c478bd9Sstevel@tonic-gate 			close(s);
1287c478bd9Sstevel@tonic-gate 			return (-1);
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 		ifc.ifc_len = bufsize;
1317c478bd9Sstevel@tonic-gate 		ifc.ifc_buf = buf;
1327c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
1337c478bd9Sstevel@tonic-gate 			perror("ifconfig: SIOCGIFCONF");
1347c478bd9Sstevel@tonic-gate 			close(s);
1357c478bd9Sstevel@tonic-gate 			free(buf);
1367c478bd9Sstevel@tonic-gate 			return (-1);
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		int_up = 0;
1407c478bd9Sstevel@tonic-gate 		ifrp = ifc.ifc_req;
1417c478bd9Sstevel@tonic-gate 		for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0;
1427c478bd9Sstevel@tonic-gate 								n--, ifrp++) {
1437c478bd9Sstevel@tonic-gate 			memset((void *) &ifr, 0, sizeof (ifr));
1447c478bd9Sstevel@tonic-gate 			strncpy(ifr.ifr_name, ifrp->ifr_name,
1457c478bd9Sstevel@tonic-gate 							sizeof (ifr.ifr_name));
1467c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) < 0) {
1477c478bd9Sstevel@tonic-gate 				perror("SIOCGIFFLAGS");
1487c478bd9Sstevel@tonic-gate 				close(s);
1497c478bd9Sstevel@tonic-gate 				free(buf);
1507c478bd9Sstevel@tonic-gate 				return (-1);
1517c478bd9Sstevel@tonic-gate 			}
1527c478bd9Sstevel@tonic-gate 			flags = ifr.ifr_flags;
1537c478bd9Sstevel@tonic-gate 			/* we are looking for a non-loopback interface */
1547c478bd9Sstevel@tonic-gate 			if ((flags & IFF_UP) && ((flags & IFF_LOOPBACK) == 0))
1557c478bd9Sstevel@tonic-gate 				int_up = 1;
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 		close(s);
1587c478bd9Sstevel@tonic-gate 		free(buf);
1597c478bd9Sstevel@tonic-gate 		if (int_up == 0) /* all the non-LOOPBACK interfaces are DOWN */
1607c478bd9Sstevel@tonic-gate 			return (-1);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate #endif /* SIOCGIFNUM */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 * for the benefit of hidden NIS domains, we use the same procedure
1677c478bd9Sstevel@tonic-gate 	 * as sendmail: convert leading + to dot, then drop to first dot
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 	getdomainname(buf, BUFSIZ);
1707c478bd9Sstevel@tonic-gate 	if (buf[0] == '+')
1717c478bd9Sstevel@tonic-gate 	buf[0] = '.';
1727c478bd9Sstevel@tonic-gate #ifdef SYSV
1737c478bd9Sstevel@tonic-gate 	cp = strchr(buf, (int)'.');
1747c478bd9Sstevel@tonic-gate #else
1757c478bd9Sstevel@tonic-gate 	cp = index(buf, '.');
1767c478bd9Sstevel@tonic-gate #endif
1777c478bd9Sstevel@tonic-gate 	if (cp == NULL)
1787c478bd9Sstevel@tonic-gate 		strcpy(_res.defdname, buf);
1797c478bd9Sstevel@tonic-gate 	else
1807c478bd9Sstevel@tonic-gate 		strcpy(_res.defdname, cp+1);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/* Allow user to override the local domain definition */
1837c478bd9Sstevel@tonic-gate 	if ((cp = getenv("LOCALDOMAIN")) != NULL) {
1847c478bd9Sstevel@tonic-gate 	(void) strncpy(_res.defdname, cp, sizeof (_res.defdname));
1857c478bd9Sstevel@tonic-gate 	haveenv++;
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
1897c478bd9Sstevel@tonic-gate 	/* read the config file */
1907c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
1917c478bd9Sstevel@tonic-gate 	    /* read default domain name */
1927c478bd9Sstevel@tonic-gate 	    if (!strncmp(buf, "domain", sizeof ("domain") - 1)) {
1937c478bd9Sstevel@tonic-gate 		if (haveenv)	/* skip if have from environ */
1947c478bd9Sstevel@tonic-gate 			    continue;
1957c478bd9Sstevel@tonic-gate 		cp = buf + sizeof ("domain") - 1;
1967c478bd9Sstevel@tonic-gate 		while (*cp == ' ' || *cp == '\t')
1977c478bd9Sstevel@tonic-gate 		    cp++;
1987c478bd9Sstevel@tonic-gate 		if ((*cp == '\0') || (*cp == '\n'))
1997c478bd9Sstevel@tonic-gate 		    continue;
2007c478bd9Sstevel@tonic-gate 		(void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1);
2017c478bd9Sstevel@tonic-gate #ifdef SYSV
2027c478bd9Sstevel@tonic-gate 		if ((cp = strchr(_res.defdname, (int)'\n')) != NULL)
2037c478bd9Sstevel@tonic-gate #else
2047c478bd9Sstevel@tonic-gate 		if ((cp = index(_res.defdname, '\n')) != NULL)
2057c478bd9Sstevel@tonic-gate #endif
2067c478bd9Sstevel@tonic-gate 		    *cp = '\0';
2077c478bd9Sstevel@tonic-gate 		havesearch = 0;
2087c478bd9Sstevel@tonic-gate 		continue;
2097c478bd9Sstevel@tonic-gate 	    }
2107c478bd9Sstevel@tonic-gate 	    /* set search list */
2117c478bd9Sstevel@tonic-gate 	    if (!strncmp(buf, "search", sizeof ("search") - 1)) {
2127c478bd9Sstevel@tonic-gate 		if (haveenv)	/* skip if have from environ */
2137c478bd9Sstevel@tonic-gate 		    continue;
2147c478bd9Sstevel@tonic-gate 		cp = buf + sizeof ("search") - 1;
2157c478bd9Sstevel@tonic-gate 		while (*cp == ' ' || *cp == '\t')
2167c478bd9Sstevel@tonic-gate 		    cp++;
2177c478bd9Sstevel@tonic-gate 		if ((*cp == '\0') || (*cp == '\n'))
2187c478bd9Sstevel@tonic-gate 		    continue;
2197c478bd9Sstevel@tonic-gate 		(void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1);
2207c478bd9Sstevel@tonic-gate #ifdef SYSV
2217c478bd9Sstevel@tonic-gate 		if ((cp = strchr(_res.defdname, (int)'\n')) != NULL)
2227c478bd9Sstevel@tonic-gate #else
2237c478bd9Sstevel@tonic-gate 		if ((cp = index(_res.defdname, '\n')) != NULL)
2247c478bd9Sstevel@tonic-gate #endif
2257c478bd9Sstevel@tonic-gate 		    *cp = '\0';
2267c478bd9Sstevel@tonic-gate 		/*
2277c478bd9Sstevel@tonic-gate 		 * Set search list to be blank-separated strings
2287c478bd9Sstevel@tonic-gate 		 * on rest of line.
2297c478bd9Sstevel@tonic-gate 		 */
2307c478bd9Sstevel@tonic-gate 		cp = _res.defdname;
2317c478bd9Sstevel@tonic-gate 		pp = _res.dnsrch;
2327c478bd9Sstevel@tonic-gate 		*pp++ = cp;
2337c478bd9Sstevel@tonic-gate 		for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
2347c478bd9Sstevel@tonic-gate 		    if (*cp == ' ' || *cp == '\t') {
2357c478bd9Sstevel@tonic-gate 			*cp = 0;
2367c478bd9Sstevel@tonic-gate 			n = 1;
2377c478bd9Sstevel@tonic-gate 		    } else if (n) {
2387c478bd9Sstevel@tonic-gate 			*pp++ = cp;
2397c478bd9Sstevel@tonic-gate 			n = 0;
2407c478bd9Sstevel@tonic-gate 		    }
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 		/* null terminate last domain if there are excess */
2437c478bd9Sstevel@tonic-gate 		while (*cp != '\0' && *cp != ' ' && *cp != '\t')
2447c478bd9Sstevel@tonic-gate 		    cp++;
2457c478bd9Sstevel@tonic-gate 		*cp = '\0';
2467c478bd9Sstevel@tonic-gate 		*pp++ = 0;
2477c478bd9Sstevel@tonic-gate 		havesearch = 1;
2487c478bd9Sstevel@tonic-gate 		continue;
2497c478bd9Sstevel@tonic-gate 	    }
2507c478bd9Sstevel@tonic-gate 	    /* read nameservers to query */
2517c478bd9Sstevel@tonic-gate 	    if (!strncmp(buf, "nameserver", sizeof ("nameserver") - 1) &&
2527c478bd9Sstevel@tonic-gate 		(nserv < MAXNS)) {
2537c478bd9Sstevel@tonic-gate 		cp = buf + sizeof ("nameserver") - 1;
2547c478bd9Sstevel@tonic-gate 		while (*cp == ' ' || *cp == '\t')
2557c478bd9Sstevel@tonic-gate 		    cp++;
2567c478bd9Sstevel@tonic-gate 		if ((*cp == '\0') || (*cp == '\n'))
2577c478bd9Sstevel@tonic-gate 		    continue;
2587c478bd9Sstevel@tonic-gate 		if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
2597c478bd9Sstevel@tonic-gate 				inet_addr(cp)) == (unsigned) -1) {
2607c478bd9Sstevel@tonic-gate 		    _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
2617c478bd9Sstevel@tonic-gate 		    continue;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 		_res.nsaddr_list[nserv].sin_family = AF_INET;
2647c478bd9Sstevel@tonic-gate 		_res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
2657c478bd9Sstevel@tonic-gate 		nserv++;
2667c478bd9Sstevel@tonic-gate 		continue;
2677c478bd9Sstevel@tonic-gate 	    }
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 	if (nserv > 1)
2707c478bd9Sstevel@tonic-gate 	    _res.nscount = nserv;
2717c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	if (_res.defdname[0] == 0) {
2747c478bd9Sstevel@tonic-gate 	if (gethostname(buf, sizeof (_res.defdname)) == 0 &&
2757c478bd9Sstevel@tonic-gate #ifdef SYSV
2767c478bd9Sstevel@tonic-gate 	    (cp = strchr(buf, (int)'.')))
2777c478bd9Sstevel@tonic-gate #else
2787c478bd9Sstevel@tonic-gate 	    (cp = index(buf, '.')))
2797c478bd9Sstevel@tonic-gate #endif
2807c478bd9Sstevel@tonic-gate 		(void) strcpy(_res.defdname, cp + 1);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	/* find components of local domain that might be searched */
2847c478bd9Sstevel@tonic-gate 	if (havesearch == 0) {
2857c478bd9Sstevel@tonic-gate 	pp = _res.dnsrch;
2867c478bd9Sstevel@tonic-gate 	*pp++ = _res.defdname;
2877c478bd9Sstevel@tonic-gate 	for (cp = _res.defdname, n = 0; *cp; cp++)
2887c478bd9Sstevel@tonic-gate 	    if (*cp == '.')
2897c478bd9Sstevel@tonic-gate 		n++;
2907c478bd9Sstevel@tonic-gate 	cp = _res.defdname;
2917c478bd9Sstevel@tonic-gate 	for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; n--) {
2927c478bd9Sstevel@tonic-gate #ifdef SYSV
2937c478bd9Sstevel@tonic-gate 	    cp = strchr(cp, (int)'.');
2947c478bd9Sstevel@tonic-gate #else
2957c478bd9Sstevel@tonic-gate 	    cp = index(cp, '.');
2967c478bd9Sstevel@tonic-gate #endif
2977c478bd9Sstevel@tonic-gate 	    *pp++ = ++cp;
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	*pp++ = 0;
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 	_res.options |= RES_INIT;
3027c478bd9Sstevel@tonic-gate 	return (0);
3037c478bd9Sstevel@tonic-gate }
304