1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1993, 1998-2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * dns_mt.c
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * This file contains all the MT related routines for the DNS backend.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include "dns_common.h"
36*0Sstevel@tonic-gate #include <dlfcn.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate  * If the DNS name service switch routines are used in a binary that depends
40*0Sstevel@tonic-gate  * on an older libresolv (libresolv.so.1, say), then having nss_dns.so.1 or
41*0Sstevel@tonic-gate  * libnss_dns.a depend on a newer libresolv (libresolv.so.2) will cause
42*0Sstevel@tonic-gate  * relocation problems. In particular, copy relocation of the _res structure
43*0Sstevel@tonic-gate  * (which changes in size from libresolv.so.1 to libresolv.so.2) could
44*0Sstevel@tonic-gate  * cause corruption, and result in a number of strange problems, including
45*0Sstevel@tonic-gate  * core dumps. Hence, we check if a libresolv is already loaded.
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #pragma init(_nss_dns_init)
49*0Sstevel@tonic-gate static void	_nss_dns_init(void);
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate extern struct hostent *res_gethostbyname(const char *);
52*0Sstevel@tonic-gate #pragma weak	res_gethostbyname
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #define		RES_SET_NO_HOSTS_FALLBACK	"__res_set_no_hosts_fallback"
55*0Sstevel@tonic-gate extern void	__res_set_no_hosts_fallback(void);
56*0Sstevel@tonic-gate #pragma weak	__res_set_no_hosts_fallback
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #define		RES_UNSET_NO_HOSTS_FALLBACK	"__res_unset_no_hosts_fallback"
59*0Sstevel@tonic-gate extern void	__res_unset_no_hosts_fallback(void);
60*0Sstevel@tonic-gate #pragma weak	__res_unset_no_hosts_fallback
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #define		RES_GET_RES	"__res_get_res"
63*0Sstevel@tonic-gate extern struct __res_state	*__res_get_res(void);
64*0Sstevel@tonic-gate #pragma weak	__res_get_res
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate #define		RES_ENABLE_MT			"__res_enable_mt"
67*0Sstevel@tonic-gate extern int	__res_enable_mt(void);
68*0Sstevel@tonic-gate #pragma weak	__res_enable_mt
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #define		RES_DISABLE_MT			"__res_disable_mt"
71*0Sstevel@tonic-gate extern int	__res_disable_mt(void);
72*0Sstevel@tonic-gate #pragma weak	__res_disable_mt
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate #define		RES_GET_H_ERRNO			"__res_get_h_errno"
75*0Sstevel@tonic-gate extern int	*__res_get_h_errno();
76*0Sstevel@tonic-gate #pragma weak	__res_get_h_errno
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate #define		__H_ERRNO			"__h_errno"
79*0Sstevel@tonic-gate extern int	*__h_errno(void);
80*0Sstevel@tonic-gate #pragma weak	__h_errno
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate #define		RES_OVERRIDE_RETRY		"__res_override_retry"
83*0Sstevel@tonic-gate extern int	__res_override_retry(int);
84*0Sstevel@tonic-gate #pragma weak	__res_override_retry
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate static void	__fallback_set_no_hosts(void);
87*0Sstevel@tonic-gate static int	*__fallback_h_errno(void);
88*0Sstevel@tonic-gate static int	__fallback_override_retry(int);
89*0Sstevel@tonic-gate static int	__is_mt_safe(void);
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate void	(*set_no_hosts_fallback)(void) = __fallback_set_no_hosts;
92*0Sstevel@tonic-gate void	(*unset_no_hosts_fallback)(void) = __fallback_set_no_hosts;
93*0Sstevel@tonic-gate struct __res_state	*(*set_res_retry)() = 0;
94*0Sstevel@tonic-gate int	(*enable_mt)() = 0;
95*0Sstevel@tonic-gate int	(*disable_mt)() = 0;
96*0Sstevel@tonic-gate int	*(*get_h_errno)(void) = 0;
97*0Sstevel@tonic-gate int	(*override_retry)(int) = 0;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /* Usually set from the Makefile */
100*0Sstevel@tonic-gate #ifndef	NSS_DNS_LIBRESOLV
101*0Sstevel@tonic-gate #define	NSS_DNS_LIBRESOLV	"libresolv.so.2"
102*0Sstevel@tonic-gate #endif
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate /* From libresolv */
105*0Sstevel@tonic-gate extern	int	h_errno;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate mutex_t	one_lane = DEFAULTMUTEX;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate void
110*0Sstevel@tonic-gate _nss_dns_init(void)
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 	struct hostent	*(*f_hostent_ptr)();
113*0Sstevel@tonic-gate 	void		*reslib, (*f_void_ptr)();
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	/* If no libresolv library, then load one */
116*0Sstevel@tonic-gate 	if ((f_hostent_ptr = res_gethostbyname) == 0) {
117*0Sstevel@tonic-gate 		if ((reslib =
118*0Sstevel@tonic-gate 		dlopen(NSS_DNS_LIBRESOLV, RTLD_LAZY|RTLD_GLOBAL)) != 0) {
119*0Sstevel@tonic-gate 			/* Turn off /etc/hosts fall back in libresolv */
120*0Sstevel@tonic-gate 			if ((f_void_ptr = (void (*)(void))dlsym(reslib,
121*0Sstevel@tonic-gate 				RES_SET_NO_HOSTS_FALLBACK)) != 0) {
122*0Sstevel@tonic-gate 				set_no_hosts_fallback = f_void_ptr;
123*0Sstevel@tonic-gate 			}
124*0Sstevel@tonic-gate 			if ((f_void_ptr = (void (*)(void))dlsym(reslib,
125*0Sstevel@tonic-gate 				RES_SET_NO_HOSTS_FALLBACK)) != 0) {
126*0Sstevel@tonic-gate 				unset_no_hosts_fallback = f_void_ptr;
127*0Sstevel@tonic-gate 			}
128*0Sstevel@tonic-gate 			/* Set number of resolver retries */
129*0Sstevel@tonic-gate 			if ((override_retry = (int (*)(int))dlsym(reslib,
130*0Sstevel@tonic-gate 				RES_OVERRIDE_RETRY)) == 0) {
131*0Sstevel@tonic-gate 				set_res_retry =
132*0Sstevel@tonic-gate 				(struct __res_state *(*)(void))dlsym(reslib,
133*0Sstevel@tonic-gate 					RES_GET_RES);
134*0Sstevel@tonic-gate 				override_retry = __fallback_override_retry;
135*0Sstevel@tonic-gate 			}
136*0Sstevel@tonic-gate 			/*
137*0Sstevel@tonic-gate 			 * Select h_errno retrieval function. A BIND 8.2.2
138*0Sstevel@tonic-gate 			 * libresolv.so.2 will have __h_errno, a BIND 8.1.2
139*0Sstevel@tonic-gate 			 * one will have __res_get_h_errno, and other
140*0Sstevel@tonic-gate 			 * versions may have nothing at all.
141*0Sstevel@tonic-gate 			 *
142*0Sstevel@tonic-gate 			 * Also try to bind to the relevant MT enable/disable
143*0Sstevel@tonic-gate 			 * functions which are also dependent on the version
144*0Sstevel@tonic-gate 			 * of the BIND libresolv.so.2 being used.
145*0Sstevel@tonic-gate 			 */
146*0Sstevel@tonic-gate 			if ((get_h_errno = (int *(*)(void))dlsym(reslib,
147*0Sstevel@tonic-gate 			    __H_ERRNO)) != 0) {
148*0Sstevel@tonic-gate 				/* BIND 8.2.2 libresolv.so.2 is MT safe. */
149*0Sstevel@tonic-gate 				enable_mt = __is_mt_safe;
150*0Sstevel@tonic-gate 				disable_mt = __is_mt_safe;
151*0Sstevel@tonic-gate 			} else {
152*0Sstevel@tonic-gate 				if ((get_h_errno =
153*0Sstevel@tonic-gate 				    (int *(*)(void))dlsym(reslib,
154*0Sstevel@tonic-gate 					RES_GET_H_ERRNO)) == 0) {
155*0Sstevel@tonic-gate 					get_h_errno = __fallback_h_errno;
156*0Sstevel@tonic-gate 				}
157*0Sstevel@tonic-gate 				/*
158*0Sstevel@tonic-gate 				 * Pre-BIND 8.2.2 was not MT safe.  Try to
159*0Sstevel@tonic-gate 				 * bind the MT enable/disable functions.
160*0Sstevel@tonic-gate 				 */
161*0Sstevel@tonic-gate 				if ((enable_mt = (int (*)(void))dlsym(reslib,
162*0Sstevel@tonic-gate 				    RES_ENABLE_MT)) != 0 &&
163*0Sstevel@tonic-gate 				    (disable_mt = (int (*)(void))dlsym(reslib,
164*0Sstevel@tonic-gate 					RES_DISABLE_MT)) == 0) {
165*0Sstevel@tonic-gate 					enable_mt = 0;
166*0Sstevel@tonic-gate 				}
167*0Sstevel@tonic-gate 			}
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 	} else {
170*0Sstevel@tonic-gate 		/* Libresolv already loaded */
171*0Sstevel@tonic-gate 		if ((f_void_ptr = __res_set_no_hosts_fallback) != 0) {
172*0Sstevel@tonic-gate 			set_no_hosts_fallback = f_void_ptr;
173*0Sstevel@tonic-gate 		}
174*0Sstevel@tonic-gate 		if ((f_void_ptr = __res_unset_no_hosts_fallback) != 0) {
175*0Sstevel@tonic-gate 			unset_no_hosts_fallback = f_void_ptr;
176*0Sstevel@tonic-gate 		}
177*0Sstevel@tonic-gate 		if ((override_retry = __res_override_retry) == 0) {
178*0Sstevel@tonic-gate 			set_res_retry = __res_get_res;
179*0Sstevel@tonic-gate 			override_retry = __fallback_override_retry;
180*0Sstevel@tonic-gate 		}
181*0Sstevel@tonic-gate 		if ((get_h_errno = __h_errno) == 0 &&
182*0Sstevel@tonic-gate 			(get_h_errno = __res_get_h_errno) == 0) {
183*0Sstevel@tonic-gate 			get_h_errno = __fallback_h_errno;
184*0Sstevel@tonic-gate 		}
185*0Sstevel@tonic-gate 		if (get_h_errno == __h_errno) {
186*0Sstevel@tonic-gate 			enable_mt = __is_mt_safe;
187*0Sstevel@tonic-gate 			disable_mt = __is_mt_safe;
188*0Sstevel@tonic-gate 		} else {
189*0Sstevel@tonic-gate 			if ((enable_mt = __res_enable_mt) != 0 &&
190*0Sstevel@tonic-gate 			    (disable_mt = __res_disable_mt) == 0) {
191*0Sstevel@tonic-gate 				enable_mt = 0;
192*0Sstevel@tonic-gate 			}
193*0Sstevel@tonic-gate 		}
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate /*
199*0Sstevel@tonic-gate  *
200*0Sstevel@tonic-gate  * Integration of BIND 8.1.2 introduced two new Sun private functions,
201*0Sstevel@tonic-gate  * __res_enable_mt() and __res_disable_mt(), that enabled and disabled
202*0Sstevel@tonic-gate  * MT mode per-thread. These functions are in the private libresolv.so.2
203*0Sstevel@tonic-gate  * interface, and intended for use by nss_dns.so.1.
204*0Sstevel@tonic-gate  *
205*0Sstevel@tonic-gate  * BIND 8.2.2 removed the need for those two functions.  As similar
206*0Sstevel@tonic-gate  * functionality was provided in BIND further up the stack. However the
207*0Sstevel@tonic-gate  * functions remain to satisfy any application that directly called upon
208*0Sstevel@tonic-gate  * them.  Only, __res_enable_mt() was modified to return failure.
209*0Sstevel@tonic-gate  * Indicated by a non-zero return value.  So that those unconventional
210*0Sstevel@tonic-gate  * applications would not then presume that res_send() and friends are
211*0Sstevel@tonic-gate  * MT-safe, when in fact they are not.
212*0Sstevel@tonic-gate  *
213*0Sstevel@tonic-gate  * To prevent nss_dns from locking inappropriately __is_mt_safe() is
214*0Sstevel@tonic-gate  * called in place of __res_enable_mt() and __res_disable_mt() if BIND
215*0Sstevel@tonic-gate  * 8.2.2 libresolv.so.2 being used.  __is_mt_safe() returns success
216*0Sstevel@tonic-gate  * indicated by a return code of zero. Signifying that no locking is
217*0Sstevel@tonic-gate  * necessary.
218*0Sstevel@tonic-gate  *
219*0Sstevel@tonic-gate  * MT applications making calls to gethostby*_r() or getipnodeby*()
220*0Sstevel@tonic-gate  * linked to libresolv.so.1 or linked statically with pre-BIND 8.2.2
221*0Sstevel@tonic-gate  * libresolv.a, doubtful as we don't ship a static version, would require
222*0Sstevel@tonic-gate  * locking within the nsswitch back-end.  Hence the mechanism can not
223*0Sstevel@tonic-gate  * simply be removed.
224*0Sstevel@tonic-gate  *
225*0Sstevel@tonic-gate  */
226*0Sstevel@tonic-gate static int
227*0Sstevel@tonic-gate __is_mt_safe(void) {
228*0Sstevel@tonic-gate 	return (0);
229*0Sstevel@tonic-gate }
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate /*
233*0Sstevel@tonic-gate  * Return pointer to the global h_errno variable
234*0Sstevel@tonic-gate  */
235*0Sstevel@tonic-gate static int *
236*0Sstevel@tonic-gate __fallback_h_errno(void) {
237*0Sstevel@tonic-gate 	return (&h_errno);
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate /*
242*0Sstevel@tonic-gate  * This function is called when the resolver library doesn't provide its
243*0Sstevel@tonic-gate  * own function to establish an override retry. If we can get a pointer
244*0Sstevel@tonic-gate  * to the per-thread _res (i.e., set_res_retry != 0), we set the retries
245*0Sstevel@tonic-gate  * directly, and return the previous number of retries. Otherwise, there's
246*0Sstevel@tonic-gate  * nothing to do.
247*0Sstevel@tonic-gate  */
248*0Sstevel@tonic-gate static int
249*0Sstevel@tonic-gate __fallback_override_retry(int retry) {
250*0Sstevel@tonic-gate 	struct __res_state	*res;
251*0Sstevel@tonic-gate 	int			old_retry = 0;
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	if (set_res_retry != 0) {
254*0Sstevel@tonic-gate 		res = set_res_retry();
255*0Sstevel@tonic-gate 		old_retry = res->retry;
256*0Sstevel@tonic-gate 		res->retry = retry;
257*0Sstevel@tonic-gate 	}
258*0Sstevel@tonic-gate 	return (old_retry);
259*0Sstevel@tonic-gate }
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate static void
263*0Sstevel@tonic-gate __fallback_set_no_hosts(void) {
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate /*
268*0Sstevel@tonic-gate  * Common code to enable/disable MT mode, set/unset no-/etc/hosts fallback,
269*0Sstevel@tonic-gate  * and to set the number of retries.
270*0Sstevel@tonic-gate  */
271*0Sstevel@tonic-gate void
272*0Sstevel@tonic-gate switch_resolver_setup(int *mt_disabled, sigset_t *oldmask, int *old_retry) {
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	/*
275*0Sstevel@tonic-gate 	 * Try to enable MT mode. If that isn't possible, mask signals,
276*0Sstevel@tonic-gate 	 * and mutex_lock.
277*0Sstevel@tonic-gate 	 */
278*0Sstevel@tonic-gate 	*mt_disabled = 1;
279*0Sstevel@tonic-gate 	if (enable_mt == 0 || (*mt_disabled = (*enable_mt)()) != 0) {
280*0Sstevel@tonic-gate 		sigset_t	newmask;
281*0Sstevel@tonic-gate 		(void) sigfillset(&newmask);
282*0Sstevel@tonic-gate 		_thr_sigsetmask(SIG_SETMASK, &newmask, oldmask);
283*0Sstevel@tonic-gate 		_mutex_lock(&one_lane);
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	/*
287*0Sstevel@tonic-gate 	 * Disable any fallback to /etc/hosts (or /etc/inet/ipnodes, when
288*0Sstevel@tonic-gate 	 * libresolv knows about that file).
289*0Sstevel@tonic-gate 	 */
290*0Sstevel@tonic-gate 	(*set_no_hosts_fallback)();
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	/*
293*0Sstevel@tonic-gate 	 * The NS switch wants to handle retries on its own.
294*0Sstevel@tonic-gate 	 */
295*0Sstevel@tonic-gate 	*old_retry = (*override_retry)(1);
296*0Sstevel@tonic-gate }
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate void
300*0Sstevel@tonic-gate switch_resolver_reset(int mt_disabled, sigset_t oldmask, int old_retry) {
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	if (mt_disabled) {
303*0Sstevel@tonic-gate 		_mutex_unlock(&one_lane);
304*0Sstevel@tonic-gate 		_thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
305*0Sstevel@tonic-gate 	} else {
306*0Sstevel@tonic-gate 		(void) (*disable_mt)();
307*0Sstevel@tonic-gate 	}
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	(*unset_no_hosts_fallback)();
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	(void) (*override_retry)(old_retry);
312*0Sstevel@tonic-gate }
313