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