17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5cb5caa98Sdjl * Common Development and Distribution License (the "License"). 6cb5caa98Sdjl * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21*7257d1b4Sraf 227c478bd9Sstevel@tonic-gate /* 23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24cb5caa98Sdjl * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * dns_mt.c 317c478bd9Sstevel@tonic-gate * 327c478bd9Sstevel@tonic-gate * This file contains all the MT related routines for the DNS backend. 337c478bd9Sstevel@tonic-gate */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #include "dns_common.h" 367c478bd9Sstevel@tonic-gate #include <dlfcn.h> 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate /* 397c478bd9Sstevel@tonic-gate * If the DNS name service switch routines are used in a binary that depends 407c478bd9Sstevel@tonic-gate * on an older libresolv (libresolv.so.1, say), then having nss_dns.so.1 or 417c478bd9Sstevel@tonic-gate * libnss_dns.a depend on a newer libresolv (libresolv.so.2) will cause 427c478bd9Sstevel@tonic-gate * relocation problems. In particular, copy relocation of the _res structure 437c478bd9Sstevel@tonic-gate * (which changes in size from libresolv.so.1 to libresolv.so.2) could 447c478bd9Sstevel@tonic-gate * cause corruption, and result in a number of strange problems, including 457c478bd9Sstevel@tonic-gate * core dumps. Hence, we check if a libresolv is already loaded. 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate #pragma init(_nss_dns_init) 497c478bd9Sstevel@tonic-gate static void _nss_dns_init(void); 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate extern struct hostent *res_gethostbyname(const char *); 527c478bd9Sstevel@tonic-gate #pragma weak res_gethostbyname 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate #define RES_SET_NO_HOSTS_FALLBACK "__res_set_no_hosts_fallback" 557c478bd9Sstevel@tonic-gate extern void __res_set_no_hosts_fallback(void); 567c478bd9Sstevel@tonic-gate #pragma weak __res_set_no_hosts_fallback 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #define RES_UNSET_NO_HOSTS_FALLBACK "__res_unset_no_hosts_fallback" 597c478bd9Sstevel@tonic-gate extern void __res_unset_no_hosts_fallback(void); 607c478bd9Sstevel@tonic-gate #pragma weak __res_unset_no_hosts_fallback 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate #define RES_GET_RES "__res_get_res" 637c478bd9Sstevel@tonic-gate extern struct __res_state *__res_get_res(void); 647c478bd9Sstevel@tonic-gate #pragma weak __res_get_res 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #define RES_ENABLE_MT "__res_enable_mt" 677c478bd9Sstevel@tonic-gate extern int __res_enable_mt(void); 687c478bd9Sstevel@tonic-gate #pragma weak __res_enable_mt 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #define RES_DISABLE_MT "__res_disable_mt" 717c478bd9Sstevel@tonic-gate extern int __res_disable_mt(void); 727c478bd9Sstevel@tonic-gate #pragma weak __res_disable_mt 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate #define RES_GET_H_ERRNO "__res_get_h_errno" 757c478bd9Sstevel@tonic-gate extern int *__res_get_h_errno(); 767c478bd9Sstevel@tonic-gate #pragma weak __res_get_h_errno 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate #define __H_ERRNO "__h_errno" 797c478bd9Sstevel@tonic-gate extern int *__h_errno(void); 807c478bd9Sstevel@tonic-gate #pragma weak __h_errno 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate #define RES_OVERRIDE_RETRY "__res_override_retry" 837c478bd9Sstevel@tonic-gate extern int __res_override_retry(int); 847c478bd9Sstevel@tonic-gate #pragma weak __res_override_retry 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate static void __fallback_set_no_hosts(void); 877c478bd9Sstevel@tonic-gate static int *__fallback_h_errno(void); 887c478bd9Sstevel@tonic-gate static int __fallback_override_retry(int); 897c478bd9Sstevel@tonic-gate static int __is_mt_safe(void); 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate void (*set_no_hosts_fallback)(void) = __fallback_set_no_hosts; 927c478bd9Sstevel@tonic-gate void (*unset_no_hosts_fallback)(void) = __fallback_set_no_hosts; 937c478bd9Sstevel@tonic-gate struct __res_state *(*set_res_retry)() = 0; 947c478bd9Sstevel@tonic-gate int (*enable_mt)() = 0; 957c478bd9Sstevel@tonic-gate int (*disable_mt)() = 0; 967c478bd9Sstevel@tonic-gate int *(*get_h_errno)(void) = 0; 977c478bd9Sstevel@tonic-gate int (*override_retry)(int) = 0; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* Usually set from the Makefile */ 1007c478bd9Sstevel@tonic-gate #ifndef NSS_DNS_LIBRESOLV 1017c478bd9Sstevel@tonic-gate #define NSS_DNS_LIBRESOLV "libresolv.so.2" 1027c478bd9Sstevel@tonic-gate #endif 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate /* From libresolv */ 1057c478bd9Sstevel@tonic-gate extern int h_errno; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate mutex_t one_lane = DEFAULTMUTEX; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate void 1107c478bd9Sstevel@tonic-gate _nss_dns_init(void) 1117c478bd9Sstevel@tonic-gate { 1127c478bd9Sstevel@tonic-gate void *reslib, (*f_void_ptr)(); 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate /* If no libresolv library, then load one */ 115cb5caa98Sdjl if (res_gethostbyname == 0) { 1167c478bd9Sstevel@tonic-gate if ((reslib = 1177c478bd9Sstevel@tonic-gate dlopen(NSS_DNS_LIBRESOLV, RTLD_LAZY|RTLD_GLOBAL)) != 0) { 1187c478bd9Sstevel@tonic-gate /* Turn off /etc/hosts fall back in libresolv */ 1197c478bd9Sstevel@tonic-gate if ((f_void_ptr = (void (*)(void))dlsym(reslib, 1207c478bd9Sstevel@tonic-gate RES_SET_NO_HOSTS_FALLBACK)) != 0) { 1217c478bd9Sstevel@tonic-gate set_no_hosts_fallback = f_void_ptr; 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate if ((f_void_ptr = (void (*)(void))dlsym(reslib, 1247c478bd9Sstevel@tonic-gate RES_SET_NO_HOSTS_FALLBACK)) != 0) { 1257c478bd9Sstevel@tonic-gate unset_no_hosts_fallback = f_void_ptr; 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate /* Set number of resolver retries */ 1287c478bd9Sstevel@tonic-gate if ((override_retry = (int (*)(int))dlsym(reslib, 1297c478bd9Sstevel@tonic-gate RES_OVERRIDE_RETRY)) == 0) { 1307c478bd9Sstevel@tonic-gate set_res_retry = 1317c478bd9Sstevel@tonic-gate (struct __res_state *(*)(void))dlsym(reslib, 1327c478bd9Sstevel@tonic-gate RES_GET_RES); 1337c478bd9Sstevel@tonic-gate override_retry = __fallback_override_retry; 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate /* 1367c478bd9Sstevel@tonic-gate * Select h_errno retrieval function. A BIND 8.2.2 1377c478bd9Sstevel@tonic-gate * libresolv.so.2 will have __h_errno, a BIND 8.1.2 1387c478bd9Sstevel@tonic-gate * one will have __res_get_h_errno, and other 1397c478bd9Sstevel@tonic-gate * versions may have nothing at all. 1407c478bd9Sstevel@tonic-gate * 1417c478bd9Sstevel@tonic-gate * Also try to bind to the relevant MT enable/disable 1427c478bd9Sstevel@tonic-gate * functions which are also dependent on the version 1437c478bd9Sstevel@tonic-gate * of the BIND libresolv.so.2 being used. 1447c478bd9Sstevel@tonic-gate */ 1457c478bd9Sstevel@tonic-gate if ((get_h_errno = (int *(*)(void))dlsym(reslib, 1467c478bd9Sstevel@tonic-gate __H_ERRNO)) != 0) { 1477c478bd9Sstevel@tonic-gate /* BIND 8.2.2 libresolv.so.2 is MT safe. */ 1487c478bd9Sstevel@tonic-gate enable_mt = __is_mt_safe; 1497c478bd9Sstevel@tonic-gate disable_mt = __is_mt_safe; 1507c478bd9Sstevel@tonic-gate } else { 1517c478bd9Sstevel@tonic-gate if ((get_h_errno = 1527c478bd9Sstevel@tonic-gate (int *(*)(void))dlsym(reslib, 1537c478bd9Sstevel@tonic-gate RES_GET_H_ERRNO)) == 0) { 1547c478bd9Sstevel@tonic-gate get_h_errno = __fallback_h_errno; 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate /* 1577c478bd9Sstevel@tonic-gate * Pre-BIND 8.2.2 was not MT safe. Try to 1587c478bd9Sstevel@tonic-gate * bind the MT enable/disable functions. 1597c478bd9Sstevel@tonic-gate */ 1607c478bd9Sstevel@tonic-gate if ((enable_mt = (int (*)(void))dlsym(reslib, 1617c478bd9Sstevel@tonic-gate RES_ENABLE_MT)) != 0 && 1627c478bd9Sstevel@tonic-gate (disable_mt = (int (*)(void))dlsym(reslib, 1637c478bd9Sstevel@tonic-gate RES_DISABLE_MT)) == 0) { 1647c478bd9Sstevel@tonic-gate enable_mt = 0; 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate } else { 1697c478bd9Sstevel@tonic-gate /* Libresolv already loaded */ 1707c478bd9Sstevel@tonic-gate if ((f_void_ptr = __res_set_no_hosts_fallback) != 0) { 1717c478bd9Sstevel@tonic-gate set_no_hosts_fallback = f_void_ptr; 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate if ((f_void_ptr = __res_unset_no_hosts_fallback) != 0) { 1747c478bd9Sstevel@tonic-gate unset_no_hosts_fallback = f_void_ptr; 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate if ((override_retry = __res_override_retry) == 0) { 1777c478bd9Sstevel@tonic-gate set_res_retry = __res_get_res; 1787c478bd9Sstevel@tonic-gate override_retry = __fallback_override_retry; 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate if ((get_h_errno = __h_errno) == 0 && 1817c478bd9Sstevel@tonic-gate (get_h_errno = __res_get_h_errno) == 0) { 1827c478bd9Sstevel@tonic-gate get_h_errno = __fallback_h_errno; 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate if (get_h_errno == __h_errno) { 1857c478bd9Sstevel@tonic-gate enable_mt = __is_mt_safe; 1867c478bd9Sstevel@tonic-gate disable_mt = __is_mt_safe; 1877c478bd9Sstevel@tonic-gate } else { 1887c478bd9Sstevel@tonic-gate if ((enable_mt = __res_enable_mt) != 0 && 1897c478bd9Sstevel@tonic-gate (disable_mt = __res_disable_mt) == 0) { 1907c478bd9Sstevel@tonic-gate enable_mt = 0; 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* 1987c478bd9Sstevel@tonic-gate * 1997c478bd9Sstevel@tonic-gate * Integration of BIND 8.1.2 introduced two new Sun private functions, 2007c478bd9Sstevel@tonic-gate * __res_enable_mt() and __res_disable_mt(), that enabled and disabled 2017c478bd9Sstevel@tonic-gate * MT mode per-thread. These functions are in the private libresolv.so.2 2027c478bd9Sstevel@tonic-gate * interface, and intended for use by nss_dns.so.1. 2037c478bd9Sstevel@tonic-gate * 2047c478bd9Sstevel@tonic-gate * BIND 8.2.2 removed the need for those two functions. As similar 2057c478bd9Sstevel@tonic-gate * functionality was provided in BIND further up the stack. However the 2067c478bd9Sstevel@tonic-gate * functions remain to satisfy any application that directly called upon 2077c478bd9Sstevel@tonic-gate * them. Only, __res_enable_mt() was modified to return failure. 2087c478bd9Sstevel@tonic-gate * Indicated by a non-zero return value. So that those unconventional 2097c478bd9Sstevel@tonic-gate * applications would not then presume that res_send() and friends are 2107c478bd9Sstevel@tonic-gate * MT-safe, when in fact they are not. 2117c478bd9Sstevel@tonic-gate * 2127c478bd9Sstevel@tonic-gate * To prevent nss_dns from locking inappropriately __is_mt_safe() is 2137c478bd9Sstevel@tonic-gate * called in place of __res_enable_mt() and __res_disable_mt() if BIND 2147c478bd9Sstevel@tonic-gate * 8.2.2 libresolv.so.2 being used. __is_mt_safe() returns success 2157c478bd9Sstevel@tonic-gate * indicated by a return code of zero. Signifying that no locking is 2167c478bd9Sstevel@tonic-gate * necessary. 2177c478bd9Sstevel@tonic-gate * 2187c478bd9Sstevel@tonic-gate * MT applications making calls to gethostby*_r() or getipnodeby*() 2197c478bd9Sstevel@tonic-gate * linked to libresolv.so.1 or linked statically with pre-BIND 8.2.2 2207c478bd9Sstevel@tonic-gate * libresolv.a, doubtful as we don't ship a static version, would require 2217c478bd9Sstevel@tonic-gate * locking within the nsswitch back-end. Hence the mechanism can not 2227c478bd9Sstevel@tonic-gate * simply be removed. 2237c478bd9Sstevel@tonic-gate * 2247c478bd9Sstevel@tonic-gate */ 2257c478bd9Sstevel@tonic-gate static int 2267c478bd9Sstevel@tonic-gate __is_mt_safe(void) { 2277c478bd9Sstevel@tonic-gate return (0); 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate /* 2327c478bd9Sstevel@tonic-gate * Return pointer to the global h_errno variable 2337c478bd9Sstevel@tonic-gate */ 2347c478bd9Sstevel@tonic-gate static int * 2357c478bd9Sstevel@tonic-gate __fallback_h_errno(void) { 2367c478bd9Sstevel@tonic-gate return (&h_errno); 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate /* 2417c478bd9Sstevel@tonic-gate * This function is called when the resolver library doesn't provide its 2427c478bd9Sstevel@tonic-gate * own function to establish an override retry. If we can get a pointer 2437c478bd9Sstevel@tonic-gate * to the per-thread _res (i.e., set_res_retry != 0), we set the retries 2447c478bd9Sstevel@tonic-gate * directly, and return the previous number of retries. Otherwise, there's 2457c478bd9Sstevel@tonic-gate * nothing to do. 2467c478bd9Sstevel@tonic-gate */ 2477c478bd9Sstevel@tonic-gate static int 2487c478bd9Sstevel@tonic-gate __fallback_override_retry(int retry) { 2497c478bd9Sstevel@tonic-gate struct __res_state *res; 2507c478bd9Sstevel@tonic-gate int old_retry = 0; 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate if (set_res_retry != 0) { 2537c478bd9Sstevel@tonic-gate res = set_res_retry(); 2547c478bd9Sstevel@tonic-gate old_retry = res->retry; 2557c478bd9Sstevel@tonic-gate res->retry = retry; 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate return (old_retry); 2587c478bd9Sstevel@tonic-gate } 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate static void 2627c478bd9Sstevel@tonic-gate __fallback_set_no_hosts(void) { 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate /* 2677c478bd9Sstevel@tonic-gate * Common code to enable/disable MT mode, set/unset no-/etc/hosts fallback, 2687c478bd9Sstevel@tonic-gate * and to set the number of retries. 2697c478bd9Sstevel@tonic-gate */ 2707c478bd9Sstevel@tonic-gate void 2717c478bd9Sstevel@tonic-gate switch_resolver_setup(int *mt_disabled, sigset_t *oldmask, int *old_retry) { 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * Try to enable MT mode. If that isn't possible, mask signals, 2757c478bd9Sstevel@tonic-gate * and mutex_lock. 2767c478bd9Sstevel@tonic-gate */ 2777c478bd9Sstevel@tonic-gate *mt_disabled = 1; 2787c478bd9Sstevel@tonic-gate if (enable_mt == 0 || (*mt_disabled = (*enable_mt)()) != 0) { 2797c478bd9Sstevel@tonic-gate sigset_t newmask; 2807c478bd9Sstevel@tonic-gate (void) sigfillset(&newmask); 281*7257d1b4Sraf (void) thr_sigsetmask(SIG_SETMASK, &newmask, oldmask); 282*7257d1b4Sraf (void) mutex_lock(&one_lane); 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate /* 2867c478bd9Sstevel@tonic-gate * Disable any fallback to /etc/hosts (or /etc/inet/ipnodes, when 2877c478bd9Sstevel@tonic-gate * libresolv knows about that file). 2887c478bd9Sstevel@tonic-gate */ 2897c478bd9Sstevel@tonic-gate (*set_no_hosts_fallback)(); 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate /* 2927c478bd9Sstevel@tonic-gate * The NS switch wants to handle retries on its own. 2937c478bd9Sstevel@tonic-gate */ 2947c478bd9Sstevel@tonic-gate *old_retry = (*override_retry)(1); 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate void 2997c478bd9Sstevel@tonic-gate switch_resolver_reset(int mt_disabled, sigset_t oldmask, int old_retry) { 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate if (mt_disabled) { 302*7257d1b4Sraf (void) mutex_unlock(&one_lane); 303*7257d1b4Sraf (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL); 3047c478bd9Sstevel@tonic-gate } else { 3057c478bd9Sstevel@tonic-gate (void) (*disable_mt)(); 3067c478bd9Sstevel@tonic-gate } 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate (*unset_no_hosts_fallback)(); 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate (void) (*override_retry)(old_retry); 3117c478bd9Sstevel@tonic-gate } 312