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