10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52830Sdjl * Common Development and Distribution License (the "License"). 62830Sdjl * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21*6812Sraf 220Sstevel@tonic-gate /* 23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 242830Sdjl * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * dns_mt.c 310Sstevel@tonic-gate * 320Sstevel@tonic-gate * This file contains all the MT related routines for the DNS backend. 330Sstevel@tonic-gate */ 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include "dns_common.h" 360Sstevel@tonic-gate #include <dlfcn.h> 370Sstevel@tonic-gate 380Sstevel@tonic-gate /* 390Sstevel@tonic-gate * If the DNS name service switch routines are used in a binary that depends 400Sstevel@tonic-gate * on an older libresolv (libresolv.so.1, say), then having nss_dns.so.1 or 410Sstevel@tonic-gate * libnss_dns.a depend on a newer libresolv (libresolv.so.2) will cause 420Sstevel@tonic-gate * relocation problems. In particular, copy relocation of the _res structure 430Sstevel@tonic-gate * (which changes in size from libresolv.so.1 to libresolv.so.2) could 440Sstevel@tonic-gate * cause corruption, and result in a number of strange problems, including 450Sstevel@tonic-gate * core dumps. Hence, we check if a libresolv is already loaded. 460Sstevel@tonic-gate */ 470Sstevel@tonic-gate 480Sstevel@tonic-gate #pragma init(_nss_dns_init) 490Sstevel@tonic-gate static void _nss_dns_init(void); 500Sstevel@tonic-gate 510Sstevel@tonic-gate extern struct hostent *res_gethostbyname(const char *); 520Sstevel@tonic-gate #pragma weak res_gethostbyname 530Sstevel@tonic-gate 540Sstevel@tonic-gate #define RES_SET_NO_HOSTS_FALLBACK "__res_set_no_hosts_fallback" 550Sstevel@tonic-gate extern void __res_set_no_hosts_fallback(void); 560Sstevel@tonic-gate #pragma weak __res_set_no_hosts_fallback 570Sstevel@tonic-gate 580Sstevel@tonic-gate #define RES_UNSET_NO_HOSTS_FALLBACK "__res_unset_no_hosts_fallback" 590Sstevel@tonic-gate extern void __res_unset_no_hosts_fallback(void); 600Sstevel@tonic-gate #pragma weak __res_unset_no_hosts_fallback 610Sstevel@tonic-gate 620Sstevel@tonic-gate #define RES_GET_RES "__res_get_res" 630Sstevel@tonic-gate extern struct __res_state *__res_get_res(void); 640Sstevel@tonic-gate #pragma weak __res_get_res 650Sstevel@tonic-gate 660Sstevel@tonic-gate #define RES_ENABLE_MT "__res_enable_mt" 670Sstevel@tonic-gate extern int __res_enable_mt(void); 680Sstevel@tonic-gate #pragma weak __res_enable_mt 690Sstevel@tonic-gate 700Sstevel@tonic-gate #define RES_DISABLE_MT "__res_disable_mt" 710Sstevel@tonic-gate extern int __res_disable_mt(void); 720Sstevel@tonic-gate #pragma weak __res_disable_mt 730Sstevel@tonic-gate 740Sstevel@tonic-gate #define RES_GET_H_ERRNO "__res_get_h_errno" 750Sstevel@tonic-gate extern int *__res_get_h_errno(); 760Sstevel@tonic-gate #pragma weak __res_get_h_errno 770Sstevel@tonic-gate 780Sstevel@tonic-gate #define __H_ERRNO "__h_errno" 790Sstevel@tonic-gate extern int *__h_errno(void); 800Sstevel@tonic-gate #pragma weak __h_errno 810Sstevel@tonic-gate 820Sstevel@tonic-gate #define RES_OVERRIDE_RETRY "__res_override_retry" 830Sstevel@tonic-gate extern int __res_override_retry(int); 840Sstevel@tonic-gate #pragma weak __res_override_retry 850Sstevel@tonic-gate 860Sstevel@tonic-gate static void __fallback_set_no_hosts(void); 870Sstevel@tonic-gate static int *__fallback_h_errno(void); 880Sstevel@tonic-gate static int __fallback_override_retry(int); 890Sstevel@tonic-gate static int __is_mt_safe(void); 900Sstevel@tonic-gate 910Sstevel@tonic-gate void (*set_no_hosts_fallback)(void) = __fallback_set_no_hosts; 920Sstevel@tonic-gate void (*unset_no_hosts_fallback)(void) = __fallback_set_no_hosts; 930Sstevel@tonic-gate struct __res_state *(*set_res_retry)() = 0; 940Sstevel@tonic-gate int (*enable_mt)() = 0; 950Sstevel@tonic-gate int (*disable_mt)() = 0; 960Sstevel@tonic-gate int *(*get_h_errno)(void) = 0; 970Sstevel@tonic-gate int (*override_retry)(int) = 0; 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* Usually set from the Makefile */ 1000Sstevel@tonic-gate #ifndef NSS_DNS_LIBRESOLV 1010Sstevel@tonic-gate #define NSS_DNS_LIBRESOLV "libresolv.so.2" 1020Sstevel@tonic-gate #endif 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate /* From libresolv */ 1050Sstevel@tonic-gate extern int h_errno; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate mutex_t one_lane = DEFAULTMUTEX; 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate void 1100Sstevel@tonic-gate _nss_dns_init(void) 1110Sstevel@tonic-gate { 1120Sstevel@tonic-gate void *reslib, (*f_void_ptr)(); 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate /* If no libresolv library, then load one */ 1152830Sdjl if (res_gethostbyname == 0) { 1160Sstevel@tonic-gate if ((reslib = 117*6812Sraf dlopen(NSS_DNS_LIBRESOLV, RTLD_LAZY|RTLD_GLOBAL)) != 0) { 1180Sstevel@tonic-gate /* Turn off /etc/hosts fall back in libresolv */ 1190Sstevel@tonic-gate if ((f_void_ptr = (void (*)(void))dlsym(reslib, 120*6812Sraf RES_SET_NO_HOSTS_FALLBACK)) != 0) { 1210Sstevel@tonic-gate set_no_hosts_fallback = f_void_ptr; 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate if ((f_void_ptr = (void (*)(void))dlsym(reslib, 124*6812Sraf RES_SET_NO_HOSTS_FALLBACK)) != 0) { 1250Sstevel@tonic-gate unset_no_hosts_fallback = f_void_ptr; 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate /* Set number of resolver retries */ 1280Sstevel@tonic-gate if ((override_retry = (int (*)(int))dlsym(reslib, 129*6812Sraf RES_OVERRIDE_RETRY)) == 0) { 1300Sstevel@tonic-gate set_res_retry = 131*6812Sraf (struct __res_state *(*)(void))dlsym(reslib, 132*6812Sraf RES_GET_RES); 1330Sstevel@tonic-gate override_retry = __fallback_override_retry; 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate /* 1360Sstevel@tonic-gate * Select h_errno retrieval function. A BIND 8.2.2 1370Sstevel@tonic-gate * libresolv.so.2 will have __h_errno, a BIND 8.1.2 1380Sstevel@tonic-gate * one will have __res_get_h_errno, and other 1390Sstevel@tonic-gate * versions may have nothing at all. 1400Sstevel@tonic-gate * 1410Sstevel@tonic-gate * Also try to bind to the relevant MT enable/disable 1420Sstevel@tonic-gate * functions which are also dependent on the version 1430Sstevel@tonic-gate * of the BIND libresolv.so.2 being used. 1440Sstevel@tonic-gate */ 1450Sstevel@tonic-gate if ((get_h_errno = (int *(*)(void))dlsym(reslib, 1460Sstevel@tonic-gate __H_ERRNO)) != 0) { 1470Sstevel@tonic-gate /* BIND 8.2.2 libresolv.so.2 is MT safe. */ 1480Sstevel@tonic-gate enable_mt = __is_mt_safe; 1490Sstevel@tonic-gate disable_mt = __is_mt_safe; 1500Sstevel@tonic-gate } else { 1510Sstevel@tonic-gate if ((get_h_errno = 1520Sstevel@tonic-gate (int *(*)(void))dlsym(reslib, 153*6812Sraf RES_GET_H_ERRNO)) == 0) { 1540Sstevel@tonic-gate get_h_errno = __fallback_h_errno; 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate /* 1570Sstevel@tonic-gate * Pre-BIND 8.2.2 was not MT safe. Try to 1580Sstevel@tonic-gate * bind the MT enable/disable functions. 1590Sstevel@tonic-gate */ 1600Sstevel@tonic-gate if ((enable_mt = (int (*)(void))dlsym(reslib, 1610Sstevel@tonic-gate RES_ENABLE_MT)) != 0 && 1620Sstevel@tonic-gate (disable_mt = (int (*)(void))dlsym(reslib, 163*6812Sraf RES_DISABLE_MT)) == 0) { 1640Sstevel@tonic-gate enable_mt = 0; 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate } else { 1690Sstevel@tonic-gate /* Libresolv already loaded */ 1700Sstevel@tonic-gate if ((f_void_ptr = __res_set_no_hosts_fallback) != 0) { 1710Sstevel@tonic-gate set_no_hosts_fallback = f_void_ptr; 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate if ((f_void_ptr = __res_unset_no_hosts_fallback) != 0) { 1740Sstevel@tonic-gate unset_no_hosts_fallback = f_void_ptr; 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate if ((override_retry = __res_override_retry) == 0) { 1770Sstevel@tonic-gate set_res_retry = __res_get_res; 1780Sstevel@tonic-gate override_retry = __fallback_override_retry; 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate if ((get_h_errno = __h_errno) == 0 && 181*6812Sraf (get_h_errno = __res_get_h_errno) == 0) { 1820Sstevel@tonic-gate get_h_errno = __fallback_h_errno; 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate if (get_h_errno == __h_errno) { 1850Sstevel@tonic-gate enable_mt = __is_mt_safe; 1860Sstevel@tonic-gate disable_mt = __is_mt_safe; 1870Sstevel@tonic-gate } else { 1880Sstevel@tonic-gate if ((enable_mt = __res_enable_mt) != 0 && 1890Sstevel@tonic-gate (disable_mt = __res_disable_mt) == 0) { 1900Sstevel@tonic-gate enable_mt = 0; 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate } 1940Sstevel@tonic-gate } 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate /* 1980Sstevel@tonic-gate * 1990Sstevel@tonic-gate * Integration of BIND 8.1.2 introduced two new Sun private functions, 2000Sstevel@tonic-gate * __res_enable_mt() and __res_disable_mt(), that enabled and disabled 2010Sstevel@tonic-gate * MT mode per-thread. These functions are in the private libresolv.so.2 2020Sstevel@tonic-gate * interface, and intended for use by nss_dns.so.1. 2030Sstevel@tonic-gate * 2040Sstevel@tonic-gate * BIND 8.2.2 removed the need for those two functions. As similar 2050Sstevel@tonic-gate * functionality was provided in BIND further up the stack. However the 2060Sstevel@tonic-gate * functions remain to satisfy any application that directly called upon 2070Sstevel@tonic-gate * them. Only, __res_enable_mt() was modified to return failure. 2080Sstevel@tonic-gate * Indicated by a non-zero return value. So that those unconventional 2090Sstevel@tonic-gate * applications would not then presume that res_send() and friends are 2100Sstevel@tonic-gate * MT-safe, when in fact they are not. 2110Sstevel@tonic-gate * 2120Sstevel@tonic-gate * To prevent nss_dns from locking inappropriately __is_mt_safe() is 2130Sstevel@tonic-gate * called in place of __res_enable_mt() and __res_disable_mt() if BIND 2140Sstevel@tonic-gate * 8.2.2 libresolv.so.2 being used. __is_mt_safe() returns success 2150Sstevel@tonic-gate * indicated by a return code of zero. Signifying that no locking is 2160Sstevel@tonic-gate * necessary. 2170Sstevel@tonic-gate * 2180Sstevel@tonic-gate * MT applications making calls to gethostby*_r() or getipnodeby*() 2190Sstevel@tonic-gate * linked to libresolv.so.1 or linked statically with pre-BIND 8.2.2 2200Sstevel@tonic-gate * libresolv.a, doubtful as we don't ship a static version, would require 2210Sstevel@tonic-gate * locking within the nsswitch back-end. Hence the mechanism can not 2220Sstevel@tonic-gate * simply be removed. 2230Sstevel@tonic-gate * 2240Sstevel@tonic-gate */ 2250Sstevel@tonic-gate static int 2260Sstevel@tonic-gate __is_mt_safe(void) { 2270Sstevel@tonic-gate return (0); 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* 2320Sstevel@tonic-gate * Return pointer to the global h_errno variable 2330Sstevel@tonic-gate */ 2340Sstevel@tonic-gate static int * 2350Sstevel@tonic-gate __fallback_h_errno(void) { 2360Sstevel@tonic-gate return (&h_errno); 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate /* 2410Sstevel@tonic-gate * This function is called when the resolver library doesn't provide its 2420Sstevel@tonic-gate * own function to establish an override retry. If we can get a pointer 2430Sstevel@tonic-gate * to the per-thread _res (i.e., set_res_retry != 0), we set the retries 2440Sstevel@tonic-gate * directly, and return the previous number of retries. Otherwise, there's 2450Sstevel@tonic-gate * nothing to do. 2460Sstevel@tonic-gate */ 2470Sstevel@tonic-gate static int 2480Sstevel@tonic-gate __fallback_override_retry(int retry) { 2490Sstevel@tonic-gate struct __res_state *res; 2500Sstevel@tonic-gate int old_retry = 0; 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate if (set_res_retry != 0) { 2530Sstevel@tonic-gate res = set_res_retry(); 2540Sstevel@tonic-gate old_retry = res->retry; 2550Sstevel@tonic-gate res->retry = retry; 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate return (old_retry); 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate static void 2620Sstevel@tonic-gate __fallback_set_no_hosts(void) { 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate /* 2670Sstevel@tonic-gate * Common code to enable/disable MT mode, set/unset no-/etc/hosts fallback, 2680Sstevel@tonic-gate * and to set the number of retries. 2690Sstevel@tonic-gate */ 2700Sstevel@tonic-gate void 2710Sstevel@tonic-gate switch_resolver_setup(int *mt_disabled, sigset_t *oldmask, int *old_retry) { 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate /* 2740Sstevel@tonic-gate * Try to enable MT mode. If that isn't possible, mask signals, 2750Sstevel@tonic-gate * and mutex_lock. 2760Sstevel@tonic-gate */ 2770Sstevel@tonic-gate *mt_disabled = 1; 2780Sstevel@tonic-gate if (enable_mt == 0 || (*mt_disabled = (*enable_mt)()) != 0) { 2790Sstevel@tonic-gate sigset_t newmask; 2800Sstevel@tonic-gate (void) sigfillset(&newmask); 281*6812Sraf (void) thr_sigsetmask(SIG_SETMASK, &newmask, oldmask); 282*6812Sraf (void) mutex_lock(&one_lane); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /* 2860Sstevel@tonic-gate * Disable any fallback to /etc/hosts (or /etc/inet/ipnodes, when 2870Sstevel@tonic-gate * libresolv knows about that file). 2880Sstevel@tonic-gate */ 2890Sstevel@tonic-gate (*set_no_hosts_fallback)(); 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate /* 2920Sstevel@tonic-gate * The NS switch wants to handle retries on its own. 2930Sstevel@tonic-gate */ 2940Sstevel@tonic-gate *old_retry = (*override_retry)(1); 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate void 2990Sstevel@tonic-gate switch_resolver_reset(int mt_disabled, sigset_t oldmask, int old_retry) { 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate if (mt_disabled) { 302*6812Sraf (void) mutex_unlock(&one_lane); 303*6812Sraf (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL); 3040Sstevel@tonic-gate } else { 3050Sstevel@tonic-gate (void) (*disable_mt)(); 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate (*unset_no_hosts_fallback)(); 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate (void) (*override_retry)(old_retry); 3110Sstevel@tonic-gate } 312