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