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