xref: /onnv-gate/usr/src/lib/libresolv/res_init.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211219Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <sys/sockio.h>
440Sstevel@tonic-gate #include <sys/socket.h>
450Sstevel@tonic-gate #include <netinet/in.h>
460Sstevel@tonic-gate #include <stdio.h>
470Sstevel@tonic-gate #include <arpa/nameser.h>
480Sstevel@tonic-gate #include <resolv.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <netinet/in.h>
510Sstevel@tonic-gate #include <net/if.h>
520Sstevel@tonic-gate #include <netinet/if_ether.h>
530Sstevel@tonic-gate #include <arpa/inet.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define	MAXIFS	256
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * Resolver state default settings
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate struct state _res = {
620Sstevel@tonic-gate 	RES_TIMEOUT,		/* retransmition time interval */
630Sstevel@tonic-gate 	4,				/* number of times to retransmit */
640Sstevel@tonic-gate 	RES_DEFAULT,		/* options flags */
650Sstevel@tonic-gate 	1,				/* number of name servers */
660Sstevel@tonic-gate };
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * Set up default settings.  If the configuration file exist, the values
700Sstevel@tonic-gate  * there will have precedence.  Otherwise, the server address is set to
710Sstevel@tonic-gate  * INADDR_LOOPBACK and the default domain name comes from the gethostname().
720Sstevel@tonic-gate  * BUT if the NIS/RPC domain name is set, that is used if all else fails.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * The configuration file should only be used if you want to redefine your
750Sstevel@tonic-gate  * domain or run without a server on your machine.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * Note the user can always override then domain name with the environment
780Sstevel@tonic-gate  * variable LOCALDOMAIN which has absolute priority.
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  * Return 0 if completes successfully, -1 on error
820Sstevel@tonic-gate  */
83237Sanay int
res_init(void)84237Sanay res_init(void)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	register FILE *fp;
870Sstevel@tonic-gate 	register char *cp, **pp;
880Sstevel@tonic-gate 	register int n;
890Sstevel@tonic-gate 	char buf[BUFSIZ];
900Sstevel@tonic-gate #ifdef SYSV
910Sstevel@tonic-gate 	extern char *strchr();
920Sstevel@tonic-gate #else
930Sstevel@tonic-gate 	extern char *index();
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate 	extern char *strcpy(), *strncpy();
960Sstevel@tonic-gate 	extern char *getenv();
970Sstevel@tonic-gate 	int nserv = 0;    /* number of nameserver records read from file */
980Sstevel@tonic-gate 	int haveenv = 0;
990Sstevel@tonic-gate 	int havesearch = 0;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	_res.nsaddr.sin_addr.s_addr =  htonl(INADDR_LOOPBACK); /* INADDR_ANY */
1020Sstevel@tonic-gate 	_res.nsaddr.sin_family = AF_INET;
1030Sstevel@tonic-gate 	_res.nsaddr.sin_port = htons(NAMESERVER_PORT);
1040Sstevel@tonic-gate 	_res.nscount = 1;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #ifdef SIOCGIFNUM
1070Sstevel@tonic-gate 	{	int numifs, s, n, int_up;
1080Sstevel@tonic-gate 		struct ifconf ifc;
1090Sstevel@tonic-gate 		register struct ifreq *ifrp;
1100Sstevel@tonic-gate 		struct ifreq ifr;
1110Sstevel@tonic-gate 		unsigned bufsize;
1120Sstevel@tonic-gate 		unsigned int flags;
1130Sstevel@tonic-gate 		char *buf;
1140Sstevel@tonic-gate 		extern void *malloc();
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1170Sstevel@tonic-gate 			perror("socket");
1180Sstevel@tonic-gate 			return (-1);
1190Sstevel@tonic-gate 		}
1200Sstevel@tonic-gate 		if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
1210Sstevel@tonic-gate 			numifs = MAXIFS;
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 		bufsize = numifs * sizeof (struct ifreq);
1240Sstevel@tonic-gate 		buf = (char *)malloc(bufsize);
1250Sstevel@tonic-gate 		if (buf == NULL) {
1260Sstevel@tonic-gate 			perror("out of memory");
1270Sstevel@tonic-gate 			close(s);
1280Sstevel@tonic-gate 			return (-1);
1290Sstevel@tonic-gate 		}
1300Sstevel@tonic-gate 		ifc.ifc_len = bufsize;
1310Sstevel@tonic-gate 		ifc.ifc_buf = buf;
1320Sstevel@tonic-gate 		if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
1330Sstevel@tonic-gate 			perror("ifconfig: SIOCGIFCONF");
1340Sstevel@tonic-gate 			close(s);
1350Sstevel@tonic-gate 			free(buf);
1360Sstevel@tonic-gate 			return (-1);
1370Sstevel@tonic-gate 		}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 		int_up = 0;
1400Sstevel@tonic-gate 		ifrp = ifc.ifc_req;
1410Sstevel@tonic-gate 		for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0;
1420Sstevel@tonic-gate 								n--, ifrp++) {
1430Sstevel@tonic-gate 			memset((void *) &ifr, 0, sizeof (ifr));
1440Sstevel@tonic-gate 			strncpy(ifr.ifr_name, ifrp->ifr_name,
1450Sstevel@tonic-gate 							sizeof (ifr.ifr_name));
1460Sstevel@tonic-gate 			if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) < 0) {
1470Sstevel@tonic-gate 				perror("SIOCGIFFLAGS");
1480Sstevel@tonic-gate 				close(s);
1490Sstevel@tonic-gate 				free(buf);
1500Sstevel@tonic-gate 				return (-1);
1510Sstevel@tonic-gate 			}
1520Sstevel@tonic-gate 			flags = ifr.ifr_flags;
1530Sstevel@tonic-gate 			/* we are looking for a non-loopback interface */
1540Sstevel@tonic-gate 			if ((flags & IFF_UP) && ((flags & IFF_LOOPBACK) == 0))
1550Sstevel@tonic-gate 				int_up = 1;
1560Sstevel@tonic-gate 		}
1570Sstevel@tonic-gate 		close(s);
1580Sstevel@tonic-gate 		free(buf);
1590Sstevel@tonic-gate 		if (int_up == 0) /* all the non-LOOPBACK interfaces are DOWN */
1600Sstevel@tonic-gate 			return (-1);
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate #endif /* SIOCGIFNUM */
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/*
1660Sstevel@tonic-gate 	 * for the benefit of hidden NIS domains, we use the same procedure
1670Sstevel@tonic-gate 	 * as sendmail: convert leading + to dot, then drop to first dot
1680Sstevel@tonic-gate 	 */
1690Sstevel@tonic-gate 	getdomainname(buf, BUFSIZ);
1700Sstevel@tonic-gate 	if (buf[0] == '+')
1710Sstevel@tonic-gate 	buf[0] = '.';
1720Sstevel@tonic-gate #ifdef SYSV
1730Sstevel@tonic-gate 	cp = strchr(buf, (int)'.');
1740Sstevel@tonic-gate #else
1750Sstevel@tonic-gate 	cp = index(buf, '.');
1760Sstevel@tonic-gate #endif
1770Sstevel@tonic-gate 	if (cp == NULL)
1780Sstevel@tonic-gate 		strcpy(_res.defdname, buf);
1790Sstevel@tonic-gate 	else
1800Sstevel@tonic-gate 		strcpy(_res.defdname, cp+1);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/* Allow user to override the local domain definition */
1830Sstevel@tonic-gate 	if ((cp = getenv("LOCALDOMAIN")) != NULL) {
1840Sstevel@tonic-gate 	(void) strncpy(_res.defdname, cp, sizeof (_res.defdname));
1850Sstevel@tonic-gate 	haveenv++;
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
1890Sstevel@tonic-gate 	/* read the config file */
1900Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
1910Sstevel@tonic-gate 	    /* read default domain name */
1920Sstevel@tonic-gate 	    if (!strncmp(buf, "domain", sizeof ("domain") - 1)) {
1930Sstevel@tonic-gate 		if (haveenv)	/* skip if have from environ */
1940Sstevel@tonic-gate 			    continue;
1950Sstevel@tonic-gate 		cp = buf + sizeof ("domain") - 1;
1960Sstevel@tonic-gate 		while (*cp == ' ' || *cp == '\t')
1970Sstevel@tonic-gate 		    cp++;
1980Sstevel@tonic-gate 		if ((*cp == '\0') || (*cp == '\n'))
1990Sstevel@tonic-gate 		    continue;
2000Sstevel@tonic-gate 		(void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1);
2010Sstevel@tonic-gate #ifdef SYSV
2020Sstevel@tonic-gate 		if ((cp = strchr(_res.defdname, (int)'\n')) != NULL)
2030Sstevel@tonic-gate #else
2040Sstevel@tonic-gate 		if ((cp = index(_res.defdname, '\n')) != NULL)
2050Sstevel@tonic-gate #endif
2060Sstevel@tonic-gate 		    *cp = '\0';
2070Sstevel@tonic-gate 		havesearch = 0;
2080Sstevel@tonic-gate 		continue;
2090Sstevel@tonic-gate 	    }
2100Sstevel@tonic-gate 	    /* set search list */
2110Sstevel@tonic-gate 	    if (!strncmp(buf, "search", sizeof ("search") - 1)) {
2120Sstevel@tonic-gate 		if (haveenv)	/* skip if have from environ */
2130Sstevel@tonic-gate 		    continue;
2140Sstevel@tonic-gate 		cp = buf + sizeof ("search") - 1;
2150Sstevel@tonic-gate 		while (*cp == ' ' || *cp == '\t')
2160Sstevel@tonic-gate 		    cp++;
2170Sstevel@tonic-gate 		if ((*cp == '\0') || (*cp == '\n'))
2180Sstevel@tonic-gate 		    continue;
2190Sstevel@tonic-gate 		(void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1);
2200Sstevel@tonic-gate #ifdef SYSV
2210Sstevel@tonic-gate 		if ((cp = strchr(_res.defdname, (int)'\n')) != NULL)
2220Sstevel@tonic-gate #else
2230Sstevel@tonic-gate 		if ((cp = index(_res.defdname, '\n')) != NULL)
2240Sstevel@tonic-gate #endif
2250Sstevel@tonic-gate 		    *cp = '\0';
2260Sstevel@tonic-gate 		/*
2270Sstevel@tonic-gate 		 * Set search list to be blank-separated strings
2280Sstevel@tonic-gate 		 * on rest of line.
2290Sstevel@tonic-gate 		 */
2300Sstevel@tonic-gate 		cp = _res.defdname;
2310Sstevel@tonic-gate 		pp = _res.dnsrch;
2320Sstevel@tonic-gate 		*pp++ = cp;
2330Sstevel@tonic-gate 		for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
2340Sstevel@tonic-gate 		    if (*cp == ' ' || *cp == '\t') {
2350Sstevel@tonic-gate 			*cp = 0;
2360Sstevel@tonic-gate 			n = 1;
2370Sstevel@tonic-gate 		    } else if (n) {
2380Sstevel@tonic-gate 			*pp++ = cp;
2390Sstevel@tonic-gate 			n = 0;
2400Sstevel@tonic-gate 		    }
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 		/* null terminate last domain if there are excess */
2430Sstevel@tonic-gate 		while (*cp != '\0' && *cp != ' ' && *cp != '\t')
2440Sstevel@tonic-gate 		    cp++;
2450Sstevel@tonic-gate 		*cp = '\0';
2460Sstevel@tonic-gate 		*pp++ = 0;
2470Sstevel@tonic-gate 		havesearch = 1;
2480Sstevel@tonic-gate 		continue;
2490Sstevel@tonic-gate 	    }
2500Sstevel@tonic-gate 	    /* read nameservers to query */
2510Sstevel@tonic-gate 	    if (!strncmp(buf, "nameserver", sizeof ("nameserver") - 1) &&
2520Sstevel@tonic-gate 		(nserv < MAXNS)) {
2530Sstevel@tonic-gate 		cp = buf + sizeof ("nameserver") - 1;
2540Sstevel@tonic-gate 		while (*cp == ' ' || *cp == '\t')
2550Sstevel@tonic-gate 		    cp++;
2560Sstevel@tonic-gate 		if ((*cp == '\0') || (*cp == '\n'))
2570Sstevel@tonic-gate 		    continue;
2580Sstevel@tonic-gate 		if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
2590Sstevel@tonic-gate 				inet_addr(cp)) == (unsigned) -1) {
2600Sstevel@tonic-gate 		    _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
2610Sstevel@tonic-gate 		    continue;
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 		_res.nsaddr_list[nserv].sin_family = AF_INET;
2640Sstevel@tonic-gate 		_res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
2650Sstevel@tonic-gate 		nserv++;
2660Sstevel@tonic-gate 		continue;
2670Sstevel@tonic-gate 	    }
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 	if (nserv > 1)
2700Sstevel@tonic-gate 	    _res.nscount = nserv;
2710Sstevel@tonic-gate 	(void) fclose(fp);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 	if (_res.defdname[0] == 0) {
2740Sstevel@tonic-gate 	if (gethostname(buf, sizeof (_res.defdname)) == 0 &&
2750Sstevel@tonic-gate #ifdef SYSV
2760Sstevel@tonic-gate 	    (cp = strchr(buf, (int)'.')))
2770Sstevel@tonic-gate #else
2780Sstevel@tonic-gate 	    (cp = index(buf, '.')))
2790Sstevel@tonic-gate #endif
2800Sstevel@tonic-gate 		(void) strcpy(_res.defdname, cp + 1);
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	/* find components of local domain that might be searched */
2840Sstevel@tonic-gate 	if (havesearch == 0) {
2850Sstevel@tonic-gate 	pp = _res.dnsrch;
2860Sstevel@tonic-gate 	*pp++ = _res.defdname;
2870Sstevel@tonic-gate 	for (cp = _res.defdname, n = 0; *cp; cp++)
2880Sstevel@tonic-gate 	    if (*cp == '.')
2890Sstevel@tonic-gate 		n++;
2900Sstevel@tonic-gate 	cp = _res.defdname;
2910Sstevel@tonic-gate 	for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; n--) {
2920Sstevel@tonic-gate #ifdef SYSV
2930Sstevel@tonic-gate 	    cp = strchr(cp, (int)'.');
2940Sstevel@tonic-gate #else
2950Sstevel@tonic-gate 	    cp = index(cp, '.');
2960Sstevel@tonic-gate #endif
2970Sstevel@tonic-gate 	    *pp++ = ++cp;
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 	*pp++ = 0;
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 	_res.options |= RES_INIT;
3020Sstevel@tonic-gate 	return (0);
3030Sstevel@tonic-gate }
304