2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * dns_mt.c
2N/A *
2N/A * This file contains all the MT related routines for the DNS backend.
2N/A */
2N/A
2N/A#include "dns_common.h"
2N/A#include <dlfcn.h>
2N/A
2N/A/*
2N/A * If the DNS name service switch routines are used in a binary that depends
2N/A * on an older libresolv (libresolv.so.1, say), then having nss_dns.so.1 or
2N/A * libnss_dns.a depend on a newer libresolv (libresolv.so.2) will cause
2N/A * relocation problems. In particular, copy relocation of the _res structure
2N/A * (which changes in size from libresolv.so.1 to libresolv.so.2) could
2N/A * cause corruption, and result in a number of strange problems, including
2N/A * core dumps. Hence, we check if a libresolv is already loaded.
2N/A */
2N/A
2N/A#pragma init(_nss_dns_init)
2N/Astatic void _nss_dns_init(void);
2N/A
2N/Aextern struct hostent *res_gethostbyname(const char *);
2N/A#pragma weak res_gethostbyname
2N/A
2N/A#define RES_SET_NO_HOSTS_FALLBACK "__res_set_no_hosts_fallback"
2N/Aextern void __res_set_no_hosts_fallback(void);
2N/A#pragma weak __res_set_no_hosts_fallback
2N/A
2N/A#define RES_UNSET_NO_HOSTS_FALLBACK "__res_unset_no_hosts_fallback"
2N/Aextern void __res_unset_no_hosts_fallback(void);
2N/A#pragma weak __res_unset_no_hosts_fallback
2N/A
2N/A#define RES_GET_RES "__res_get_res"
2N/Aextern struct __res_state *__res_get_res(void);
2N/A#pragma weak __res_get_res
2N/A
2N/A#define RES_ENABLE_MT "__res_enable_mt"
2N/Aextern int __res_enable_mt(void);
2N/A#pragma weak __res_enable_mt
2N/A
2N/A#define RES_DISABLE_MT "__res_disable_mt"
2N/Aextern int __res_disable_mt(void);
2N/A#pragma weak __res_disable_mt
2N/A
2N/A#define RES_GET_H_ERRNO "__res_get_h_errno"
2N/Aextern int *__res_get_h_errno();
2N/A#pragma weak __res_get_h_errno
2N/A
2N/A#define __H_ERRNO "__h_errno"
2N/Aextern int *__h_errno(void);
2N/A#pragma weak __h_errno
2N/A
2N/A#define RES_OVERRIDE_RETRY "__res_override_retry"
2N/Aextern int __res_override_retry(int);
2N/A#pragma weak __res_override_retry
2N/A
2N/Astatic void __fallback_set_no_hosts(void);
2N/Astatic int *__fallback_h_errno(void);
2N/Astatic int __fallback_override_retry(int);
2N/Astatic int __is_mt_safe(void);
2N/A
2N/Avoid (*set_no_hosts_fallback)(void) = __fallback_set_no_hosts;
2N/Avoid (*unset_no_hosts_fallback)(void) = __fallback_set_no_hosts;
2N/Astruct __res_state *(*set_res_retry)() = 0;
2N/Aint (*enable_mt)() = 0;
2N/Aint (*disable_mt)() = 0;
2N/Aint *(*get_h_errno)(void) = 0;
2N/Aint (*override_retry)(int) = 0;
2N/A
2N/A/* Usually set from the Makefile */
2N/A#ifndef NSS_DNS_LIBRESOLV
2N/A#define NSS_DNS_LIBRESOLV "libresolv.so.2"
2N/A#endif
2N/A
2N/A/* From libresolv */
2N/Aextern int h_errno;
2N/A
2N/Amutex_t one_lane = DEFAULTMUTEX;
2N/A
2N/Avoid
2N/A_nss_dns_init(void)
2N/A{
2N/A void *reslib, (*f_void_ptr)();
2N/A
2N/A /* If no libresolv library, then load one */
2N/A if (res_gethostbyname == 0) {
2N/A if ((reslib =
2N/A dlopen(NSS_DNS_LIBRESOLV, RTLD_LAZY|RTLD_GLOBAL)) != 0) {
2N/A /* Turn off /etc/hosts fall back in libresolv */
2N/A if ((f_void_ptr = (void (*)(void))dlsym(reslib,
2N/A RES_SET_NO_HOSTS_FALLBACK)) != 0) {
2N/A set_no_hosts_fallback = f_void_ptr;
2N/A }
2N/A if ((f_void_ptr = (void (*)(void))dlsym(reslib,
2N/A RES_SET_NO_HOSTS_FALLBACK)) != 0) {
2N/A unset_no_hosts_fallback = f_void_ptr;
2N/A }
2N/A /* Set number of resolver retries */
2N/A if ((override_retry = (int (*)(int))dlsym(reslib,
2N/A RES_OVERRIDE_RETRY)) == 0) {
2N/A set_res_retry =
2N/A (struct __res_state *(*)(void))dlsym(reslib,
2N/A RES_GET_RES);
2N/A override_retry = __fallback_override_retry;
2N/A }
2N/A /*
2N/A * Select h_errno retrieval function. A BIND 8.2.2
2N/A * libresolv.so.2 will have __h_errno, a BIND 8.1.2
2N/A * one will have __res_get_h_errno, and other
2N/A * versions may have nothing at all.
2N/A *
2N/A * Also try to bind to the relevant MT enable/disable
2N/A * functions which are also dependent on the version
2N/A * of the BIND libresolv.so.2 being used.
2N/A */
2N/A if ((get_h_errno = (int *(*)(void))dlsym(reslib,
2N/A __H_ERRNO)) != 0) {
2N/A /* BIND 8.2.2 libresolv.so.2 is MT safe. */
2N/A enable_mt = __is_mt_safe;
2N/A disable_mt = __is_mt_safe;
2N/A } else {
2N/A if ((get_h_errno =
2N/A (int *(*)(void))dlsym(reslib,
2N/A RES_GET_H_ERRNO)) == 0) {
2N/A get_h_errno = __fallback_h_errno;
2N/A }
2N/A /*
2N/A * Pre-BIND 8.2.2 was not MT safe. Try to
2N/A * bind the MT enable/disable functions.
2N/A */
2N/A if ((enable_mt = (int (*)(void))dlsym(reslib,
2N/A RES_ENABLE_MT)) != 0 &&
2N/A (disable_mt = (int (*)(void))dlsym(reslib,
2N/A RES_DISABLE_MT)) == 0) {
2N/A enable_mt = 0;
2N/A }
2N/A }
2N/A }
2N/A } else {
2N/A /* Libresolv already loaded */
2N/A if ((f_void_ptr = __res_set_no_hosts_fallback) != 0) {
2N/A set_no_hosts_fallback = f_void_ptr;
2N/A }
2N/A if ((f_void_ptr = __res_unset_no_hosts_fallback) != 0) {
2N/A unset_no_hosts_fallback = f_void_ptr;
2N/A }
2N/A if ((override_retry = __res_override_retry) == 0) {
2N/A set_res_retry = __res_get_res;
2N/A override_retry = __fallback_override_retry;
2N/A }
2N/A if ((get_h_errno = __h_errno) == 0 &&
2N/A (get_h_errno = __res_get_h_errno) == 0) {
2N/A get_h_errno = __fallback_h_errno;
2N/A }
2N/A if (get_h_errno == __h_errno) {
2N/A enable_mt = __is_mt_safe;
2N/A disable_mt = __is_mt_safe;
2N/A } else {
2N/A if ((enable_mt = __res_enable_mt) != 0 &&
2N/A (disable_mt = __res_disable_mt) == 0) {
2N/A enable_mt = 0;
2N/A }
2N/A }
2N/A }
2N/A}
2N/A
2N/A
2N/A/*
2N/A *
2N/A * Integration of BIND 8.1.2 introduced two new Sun private functions,
2N/A * __res_enable_mt() and __res_disable_mt(), that enabled and disabled
2N/A * MT mode per-thread. These functions are in the private libresolv.so.2
2N/A * interface, and intended for use by nss_dns.so.1.
2N/A *
2N/A * BIND 8.2.2 removed the need for those two functions. As similar
2N/A * functionality was provided in BIND further up the stack. However the
2N/A * functions remain to satisfy any application that directly called upon
2N/A * them. Only, __res_enable_mt() was modified to return failure.
2N/A * Indicated by a non-zero return value. So that those unconventional
2N/A * applications would not then presume that res_send() and friends are
2N/A * MT-safe, when in fact they are not.
2N/A *
2N/A * To prevent nss_dns from locking inappropriately __is_mt_safe() is
2N/A * called in place of __res_enable_mt() and __res_disable_mt() if BIND
2N/A * 8.2.2 libresolv.so.2 being used. __is_mt_safe() returns success
2N/A * indicated by a return code of zero. Signifying that no locking is
2N/A * necessary.
2N/A *
2N/A * MT applications making calls to gethostby*_r() or getipnodeby*()
2N/A * linked to libresolv.so.1 or linked statically with pre-BIND 8.2.2
2N/A * libresolv.a, doubtful as we don't ship a static version, would require
2N/A * locking within the nsswitch back-end. Hence the mechanism can not
2N/A * simply be removed.
2N/A *
2N/A */
2N/Astatic int
2N/A__is_mt_safe(void) {
2N/A return (0);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Return pointer to the global h_errno variable
2N/A */
2N/Astatic int *
2N/A__fallback_h_errno(void) {
2N/A return (&h_errno);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * This function is called when the resolver library doesn't provide its
2N/A * own function to establish an override retry. If we can get a pointer
2N/A * to the per-thread _res (i.e., set_res_retry != 0), we set the retries
2N/A * directly, and return the previous number of retries. Otherwise, there's
2N/A * nothing to do.
2N/A */
2N/Astatic int
2N/A__fallback_override_retry(int retry) {
2N/A struct __res_state *res;
2N/A int old_retry = 0;
2N/A
2N/A if (set_res_retry != 0) {
2N/A res = set_res_retry();
2N/A old_retry = res->retry;
2N/A res->retry = retry;
2N/A }
2N/A return (old_retry);
2N/A}
2N/A
2N/A
2N/Astatic void
2N/A__fallback_set_no_hosts(void) {
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Common code to enable/disable MT mode, set/unset no-/etc/hosts fallback,
2N/A * and to set the number of retries.
2N/A */
2N/Avoid
2N/Aswitch_resolver_setup(int *mt_disabled, sigset_t *oldmask, int *old_retry) {
2N/A
2N/A /*
2N/A * Try to enable MT mode. If that isn't possible, mask signals,
2N/A * and mutex_lock.
2N/A */
2N/A *mt_disabled = 1;
2N/A if (enable_mt == 0 || (*mt_disabled = (*enable_mt)()) != 0) {
2N/A sigset_t newmask;
2N/A (void) sigfillset(&newmask);
2N/A (void) thr_sigsetmask(SIG_SETMASK, &newmask, oldmask);
2N/A (void) mutex_lock(&one_lane);
2N/A }
2N/A
2N/A /*
2N/A * Disable any fallback to /etc/hosts (or /etc/inet/ipnodes, when
2N/A * libresolv knows about that file).
2N/A */
2N/A (*set_no_hosts_fallback)();
2N/A
2N/A /*
2N/A * The NS switch wants to handle retries on its own.
2N/A */
2N/A *old_retry = (*override_retry)(1);
2N/A}
2N/A
2N/A
2N/Avoid
2N/Aswitch_resolver_reset(int mt_disabled, sigset_t oldmask, int old_retry) {
2N/A
2N/A if (mt_disabled) {
2N/A (void) mutex_unlock(&one_lane);
2N/A (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
2N/A } else {
2N/A (void) (*disable_mt)();
2N/A }
2N/A
2N/A (*unset_no_hosts_fallback)();
2N/A
2N/A (void) (*override_retry)(old_retry);
2N/A}