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*2830Sdjl  * Common Development and Distribution License (the "License").
6*2830Sdjl  * 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  */
210Sstevel@tonic-gate /*
22*2830Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*2830Sdjl  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * dns_mt.c
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * This file contains all the MT related routines for the DNS backend.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "dns_common.h"
350Sstevel@tonic-gate #include <dlfcn.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * If the DNS name service switch routines are used in a binary that depends
390Sstevel@tonic-gate  * on an older libresolv (libresolv.so.1, say), then having nss_dns.so.1 or
400Sstevel@tonic-gate  * libnss_dns.a depend on a newer libresolv (libresolv.so.2) will cause
410Sstevel@tonic-gate  * relocation problems. In particular, copy relocation of the _res structure
420Sstevel@tonic-gate  * (which changes in size from libresolv.so.1 to libresolv.so.2) could
430Sstevel@tonic-gate  * cause corruption, and result in a number of strange problems, including
440Sstevel@tonic-gate  * core dumps. Hence, we check if a libresolv is already loaded.
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #pragma init(_nss_dns_init)
480Sstevel@tonic-gate static void	_nss_dns_init(void);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate extern struct hostent *res_gethostbyname(const char *);
510Sstevel@tonic-gate #pragma weak	res_gethostbyname
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define		RES_SET_NO_HOSTS_FALLBACK	"__res_set_no_hosts_fallback"
540Sstevel@tonic-gate extern void	__res_set_no_hosts_fallback(void);
550Sstevel@tonic-gate #pragma weak	__res_set_no_hosts_fallback
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #define		RES_UNSET_NO_HOSTS_FALLBACK	"__res_unset_no_hosts_fallback"
580Sstevel@tonic-gate extern void	__res_unset_no_hosts_fallback(void);
590Sstevel@tonic-gate #pragma weak	__res_unset_no_hosts_fallback
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define		RES_GET_RES	"__res_get_res"
620Sstevel@tonic-gate extern struct __res_state	*__res_get_res(void);
630Sstevel@tonic-gate #pragma weak	__res_get_res
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define		RES_ENABLE_MT			"__res_enable_mt"
660Sstevel@tonic-gate extern int	__res_enable_mt(void);
670Sstevel@tonic-gate #pragma weak	__res_enable_mt
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #define		RES_DISABLE_MT			"__res_disable_mt"
700Sstevel@tonic-gate extern int	__res_disable_mt(void);
710Sstevel@tonic-gate #pragma weak	__res_disable_mt
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #define		RES_GET_H_ERRNO			"__res_get_h_errno"
740Sstevel@tonic-gate extern int	*__res_get_h_errno();
750Sstevel@tonic-gate #pragma weak	__res_get_h_errno
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define		__H_ERRNO			"__h_errno"
780Sstevel@tonic-gate extern int	*__h_errno(void);
790Sstevel@tonic-gate #pragma weak	__h_errno
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define		RES_OVERRIDE_RETRY		"__res_override_retry"
820Sstevel@tonic-gate extern int	__res_override_retry(int);
830Sstevel@tonic-gate #pragma weak	__res_override_retry
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static void	__fallback_set_no_hosts(void);
860Sstevel@tonic-gate static int	*__fallback_h_errno(void);
870Sstevel@tonic-gate static int	__fallback_override_retry(int);
880Sstevel@tonic-gate static int	__is_mt_safe(void);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate void	(*set_no_hosts_fallback)(void) = __fallback_set_no_hosts;
910Sstevel@tonic-gate void	(*unset_no_hosts_fallback)(void) = __fallback_set_no_hosts;
920Sstevel@tonic-gate struct __res_state	*(*set_res_retry)() = 0;
930Sstevel@tonic-gate int	(*enable_mt)() = 0;
940Sstevel@tonic-gate int	(*disable_mt)() = 0;
950Sstevel@tonic-gate int	*(*get_h_errno)(void) = 0;
960Sstevel@tonic-gate int	(*override_retry)(int) = 0;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /* Usually set from the Makefile */
990Sstevel@tonic-gate #ifndef	NSS_DNS_LIBRESOLV
1000Sstevel@tonic-gate #define	NSS_DNS_LIBRESOLV	"libresolv.so.2"
1010Sstevel@tonic-gate #endif
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /* From libresolv */
1040Sstevel@tonic-gate extern	int	h_errno;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate mutex_t	one_lane = DEFAULTMUTEX;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate void
1090Sstevel@tonic-gate _nss_dns_init(void)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	void		*reslib, (*f_void_ptr)();
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/* If no libresolv library, then load one */
114*2830Sdjl 	if (res_gethostbyname == 0) {
1150Sstevel@tonic-gate 		if ((reslib =
1160Sstevel@tonic-gate 		dlopen(NSS_DNS_LIBRESOLV, RTLD_LAZY|RTLD_GLOBAL)) != 0) {
1170Sstevel@tonic-gate 			/* Turn off /etc/hosts fall back in libresolv */
1180Sstevel@tonic-gate 			if ((f_void_ptr = (void (*)(void))dlsym(reslib,
1190Sstevel@tonic-gate 				RES_SET_NO_HOSTS_FALLBACK)) != 0) {
1200Sstevel@tonic-gate 				set_no_hosts_fallback = f_void_ptr;
1210Sstevel@tonic-gate 			}
1220Sstevel@tonic-gate 			if ((f_void_ptr = (void (*)(void))dlsym(reslib,
1230Sstevel@tonic-gate 				RES_SET_NO_HOSTS_FALLBACK)) != 0) {
1240Sstevel@tonic-gate 				unset_no_hosts_fallback = f_void_ptr;
1250Sstevel@tonic-gate 			}
1260Sstevel@tonic-gate 			/* Set number of resolver retries */
1270Sstevel@tonic-gate 			if ((override_retry = (int (*)(int))dlsym(reslib,
1280Sstevel@tonic-gate 				RES_OVERRIDE_RETRY)) == 0) {
1290Sstevel@tonic-gate 				set_res_retry =
1300Sstevel@tonic-gate 				(struct __res_state *(*)(void))dlsym(reslib,
1310Sstevel@tonic-gate 					RES_GET_RES);
1320Sstevel@tonic-gate 				override_retry = __fallback_override_retry;
1330Sstevel@tonic-gate 			}
1340Sstevel@tonic-gate 			/*
1350Sstevel@tonic-gate 			 * Select h_errno retrieval function. A BIND 8.2.2
1360Sstevel@tonic-gate 			 * libresolv.so.2 will have __h_errno, a BIND 8.1.2
1370Sstevel@tonic-gate 			 * one will have __res_get_h_errno, and other
1380Sstevel@tonic-gate 			 * versions may have nothing at all.
1390Sstevel@tonic-gate 			 *
1400Sstevel@tonic-gate 			 * Also try to bind to the relevant MT enable/disable
1410Sstevel@tonic-gate 			 * functions which are also dependent on the version
1420Sstevel@tonic-gate 			 * of the BIND libresolv.so.2 being used.
1430Sstevel@tonic-gate 			 */
1440Sstevel@tonic-gate 			if ((get_h_errno = (int *(*)(void))dlsym(reslib,
1450Sstevel@tonic-gate 			    __H_ERRNO)) != 0) {
1460Sstevel@tonic-gate 				/* BIND 8.2.2 libresolv.so.2 is MT safe. */
1470Sstevel@tonic-gate 				enable_mt = __is_mt_safe;
1480Sstevel@tonic-gate 				disable_mt = __is_mt_safe;
1490Sstevel@tonic-gate 			} else {
1500Sstevel@tonic-gate 				if ((get_h_errno =
1510Sstevel@tonic-gate 				    (int *(*)(void))dlsym(reslib,
1520Sstevel@tonic-gate 					RES_GET_H_ERRNO)) == 0) {
1530Sstevel@tonic-gate 					get_h_errno = __fallback_h_errno;
1540Sstevel@tonic-gate 				}
1550Sstevel@tonic-gate 				/*
1560Sstevel@tonic-gate 				 * Pre-BIND 8.2.2 was not MT safe.  Try to
1570Sstevel@tonic-gate 				 * bind the MT enable/disable functions.
1580Sstevel@tonic-gate 				 */
1590Sstevel@tonic-gate 				if ((enable_mt = (int (*)(void))dlsym(reslib,
1600Sstevel@tonic-gate 				    RES_ENABLE_MT)) != 0 &&
1610Sstevel@tonic-gate 				    (disable_mt = (int (*)(void))dlsym(reslib,
1620Sstevel@tonic-gate 					RES_DISABLE_MT)) == 0) {
1630Sstevel@tonic-gate 					enable_mt = 0;
1640Sstevel@tonic-gate 				}
1650Sstevel@tonic-gate 			}
1660Sstevel@tonic-gate 		}
1670Sstevel@tonic-gate 	} else {
1680Sstevel@tonic-gate 		/* Libresolv already loaded */
1690Sstevel@tonic-gate 		if ((f_void_ptr = __res_set_no_hosts_fallback) != 0) {
1700Sstevel@tonic-gate 			set_no_hosts_fallback = f_void_ptr;
1710Sstevel@tonic-gate 		}
1720Sstevel@tonic-gate 		if ((f_void_ptr = __res_unset_no_hosts_fallback) != 0) {
1730Sstevel@tonic-gate 			unset_no_hosts_fallback = f_void_ptr;
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 		if ((override_retry = __res_override_retry) == 0) {
1760Sstevel@tonic-gate 			set_res_retry = __res_get_res;
1770Sstevel@tonic-gate 			override_retry = __fallback_override_retry;
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 		if ((get_h_errno = __h_errno) == 0 &&
1800Sstevel@tonic-gate 			(get_h_errno = __res_get_h_errno) == 0) {
1810Sstevel@tonic-gate 			get_h_errno = __fallback_h_errno;
1820Sstevel@tonic-gate 		}
1830Sstevel@tonic-gate 		if (get_h_errno == __h_errno) {
1840Sstevel@tonic-gate 			enable_mt = __is_mt_safe;
1850Sstevel@tonic-gate 			disable_mt = __is_mt_safe;
1860Sstevel@tonic-gate 		} else {
1870Sstevel@tonic-gate 			if ((enable_mt = __res_enable_mt) != 0 &&
1880Sstevel@tonic-gate 			    (disable_mt = __res_disable_mt) == 0) {
1890Sstevel@tonic-gate 				enable_mt = 0;
1900Sstevel@tonic-gate 			}
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate  *
1980Sstevel@tonic-gate  * Integration of BIND 8.1.2 introduced two new Sun private functions,
1990Sstevel@tonic-gate  * __res_enable_mt() and __res_disable_mt(), that enabled and disabled
2000Sstevel@tonic-gate  * MT mode per-thread. These functions are in the private libresolv.so.2
2010Sstevel@tonic-gate  * interface, and intended for use by nss_dns.so.1.
2020Sstevel@tonic-gate  *
2030Sstevel@tonic-gate  * BIND 8.2.2 removed the need for those two functions.  As similar
2040Sstevel@tonic-gate  * functionality was provided in BIND further up the stack. However the
2050Sstevel@tonic-gate  * functions remain to satisfy any application that directly called upon
2060Sstevel@tonic-gate  * them.  Only, __res_enable_mt() was modified to return failure.
2070Sstevel@tonic-gate  * Indicated by a non-zero return value.  So that those unconventional
2080Sstevel@tonic-gate  * applications would not then presume that res_send() and friends are
2090Sstevel@tonic-gate  * MT-safe, when in fact they are not.
2100Sstevel@tonic-gate  *
2110Sstevel@tonic-gate  * To prevent nss_dns from locking inappropriately __is_mt_safe() is
2120Sstevel@tonic-gate  * called in place of __res_enable_mt() and __res_disable_mt() if BIND
2130Sstevel@tonic-gate  * 8.2.2 libresolv.so.2 being used.  __is_mt_safe() returns success
2140Sstevel@tonic-gate  * indicated by a return code of zero. Signifying that no locking is
2150Sstevel@tonic-gate  * necessary.
2160Sstevel@tonic-gate  *
2170Sstevel@tonic-gate  * MT applications making calls to gethostby*_r() or getipnodeby*()
2180Sstevel@tonic-gate  * linked to libresolv.so.1 or linked statically with pre-BIND 8.2.2
2190Sstevel@tonic-gate  * libresolv.a, doubtful as we don't ship a static version, would require
2200Sstevel@tonic-gate  * locking within the nsswitch back-end.  Hence the mechanism can not
2210Sstevel@tonic-gate  * simply be removed.
2220Sstevel@tonic-gate  *
2230Sstevel@tonic-gate  */
2240Sstevel@tonic-gate static int
2250Sstevel@tonic-gate __is_mt_safe(void) {
2260Sstevel@tonic-gate 	return (0);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * Return pointer to the global h_errno variable
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate static int *
2340Sstevel@tonic-gate __fallback_h_errno(void) {
2350Sstevel@tonic-gate 	return (&h_errno);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  * This function is called when the resolver library doesn't provide its
2410Sstevel@tonic-gate  * own function to establish an override retry. If we can get a pointer
2420Sstevel@tonic-gate  * to the per-thread _res (i.e., set_res_retry != 0), we set the retries
2430Sstevel@tonic-gate  * directly, and return the previous number of retries. Otherwise, there's
2440Sstevel@tonic-gate  * nothing to do.
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate static int
2470Sstevel@tonic-gate __fallback_override_retry(int retry) {
2480Sstevel@tonic-gate 	struct __res_state	*res;
2490Sstevel@tonic-gate 	int			old_retry = 0;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if (set_res_retry != 0) {
2520Sstevel@tonic-gate 		res = set_res_retry();
2530Sstevel@tonic-gate 		old_retry = res->retry;
2540Sstevel@tonic-gate 		res->retry = retry;
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 	return (old_retry);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate static void
2610Sstevel@tonic-gate __fallback_set_no_hosts(void) {
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate  * Common code to enable/disable MT mode, set/unset no-/etc/hosts fallback,
2670Sstevel@tonic-gate  * and to set the number of retries.
2680Sstevel@tonic-gate  */
2690Sstevel@tonic-gate void
2700Sstevel@tonic-gate switch_resolver_setup(int *mt_disabled, sigset_t *oldmask, int *old_retry) {
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * Try to enable MT mode. If that isn't possible, mask signals,
2740Sstevel@tonic-gate 	 * and mutex_lock.
2750Sstevel@tonic-gate 	 */
2760Sstevel@tonic-gate 	*mt_disabled = 1;
2770Sstevel@tonic-gate 	if (enable_mt == 0 || (*mt_disabled = (*enable_mt)()) != 0) {
2780Sstevel@tonic-gate 		sigset_t	newmask;
2790Sstevel@tonic-gate 		(void) sigfillset(&newmask);
2800Sstevel@tonic-gate 		_thr_sigsetmask(SIG_SETMASK, &newmask, oldmask);
2810Sstevel@tonic-gate 		_mutex_lock(&one_lane);
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	/*
2850Sstevel@tonic-gate 	 * Disable any fallback to /etc/hosts (or /etc/inet/ipnodes, when
2860Sstevel@tonic-gate 	 * libresolv knows about that file).
2870Sstevel@tonic-gate 	 */
2880Sstevel@tonic-gate 	(*set_no_hosts_fallback)();
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/*
2910Sstevel@tonic-gate 	 * The NS switch wants to handle retries on its own.
2920Sstevel@tonic-gate 	 */
2930Sstevel@tonic-gate 	*old_retry = (*override_retry)(1);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate void
2980Sstevel@tonic-gate switch_resolver_reset(int mt_disabled, sigset_t oldmask, int old_retry) {
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (mt_disabled) {
3010Sstevel@tonic-gate 		_mutex_unlock(&one_lane);
3020Sstevel@tonic-gate 		_thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
3030Sstevel@tonic-gate 	} else {
3040Sstevel@tonic-gate 		(void) (*disable_mt)();
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	(*unset_no_hosts_fallback)();
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	(void) (*override_retry)(old_retry);
3100Sstevel@tonic-gate }
311