2N/A/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2N/A/*
2N/A * lib/kdb/kdb_ldap/ldap_handle.c
2N/A *
2N/A * Copyright (c) 2004-2005, Novell, Inc.
2N/A * All rights reserved.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions are met:
2N/A *
2N/A * * Redistributions of source code must retain the above copyright notice,
2N/A * this list of conditions and the following disclaimer.
2N/A * * Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in the
2N/A * documentation and/or other materials provided with the distribution.
2N/A * * The copyright holder's name is not used to endorse or promote products
2N/A * derived from this software without specific prior written permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2N/A * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2N/A * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2N/A * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2N/A * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2N/A * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2N/A * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2N/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2N/A * POSSIBILITY OF SUCH DAMAGE.
2N/A */
2N/A
2N/A#include "ldap_main.h"
2N/A
2N/A
2N/A#ifdef ASYNC_BIND
2N/A
2N/A/*
2N/A * Update the server info structure. In case of an asynchronous bind,
2N/A * this function is called to check the bind status. A flag
2N/A * server_info_upate_pending is refered before calling this function.
2N/A * This function sets the server_status to either ON or OFF and
2N/A * sets the server_info_udpate_pending to OFF.
2N/A * Do not lock the mutex here. The caller should lock it
2N/A */
2N/A
2N/Astatic krb5_error_code
2N/Akrb5_update_server_info(krb5_ldap_server_handle *ldap_server_handle,
2N/A krb5_ldap_server_info *server_info)
2N/A{
2N/A krb5_error_code st=0;
2N/A struct timeval ztime={0, 0};
2N/A LDAPMessage *result=NULL;
2N/A
2N/A if (ldap_server_handle == NULL || server_info == NULL)
2N/A return -1;
2N/A
2N/A while (st == 0) {
2N/A st = ldap_result(ldap_server_handle->ldap_handle, ldap_server_handle->msgid,
2N/A LDAP_MSG_ALL, &ztime, &result);
2N/A switch (st) {
2N/A case -1:
2N/A server_info->server_status = OFF;
2N/A time(&server_info->downtime);
2N/A break;
2N/A
2N/A case 0:
2N/A continue;
2N/A break;
2N/A
2N/A case LDAP_RES_BIND:
2N/A if ((st=ldap_result2error(ldap_server_handle->ldap_handle, result, 1)) == LDAP_SUCCESS) {
2N/A server_info->server_status = ON;
2N/A } else {
2N/A /* ?? */ krb5_set_error_message(0, 0, "%s", ldap_err2string(st));
2N/A server_info->server_status = OFF;
2N/A time(&server_info->downtime);
2N/A }
2N/A ldap_msgfree(result);
2N/A break;
2N/A default:
2N/A ldap_msgfree(result);
2N/A continue;
2N/A break;
2N/A }
2N/A }
2N/A ldap_server_handle->server_info_update_pending = FALSE;
2N/A return 0;
2N/A}
2N/A#endif
2N/A
2N/A/*
2N/A * Return ldap server handle from the pool. If the pool is exhausted return NULL.
2N/A * Do not lock the mutex, caller should lock it
2N/A */
2N/A
2N/Astatic krb5_ldap_server_handle *
2N/Akrb5_get_ldap_handle(krb5_ldap_context *ldap_context)
2N/A{
2N/A krb5_ldap_server_handle *ldap_server_handle=NULL;
2N/A krb5_ldap_server_info *ldap_server_info=NULL;
2N/A int cnt=0;
2N/A
2N/A while (ldap_context->server_info_list[cnt] != NULL) {
2N/A ldap_server_info = ldap_context->server_info_list[cnt];
2N/A if (ldap_server_info->server_status != OFF) {
2N/A if (ldap_server_info->ldap_server_handles != NULL) {
2N/A ldap_server_handle = ldap_server_info->ldap_server_handles;
2N/A ldap_server_info->ldap_server_handles = ldap_server_handle->next;
2N/A break;
2N/A#ifdef ASYNC_BIND
2N/A if (ldap_server_handle->server_info_update_pending == TRUE) {
2N/A krb5_update_server_info(context, ldap_server_handle,
2N/A ldap_server_info);
2N/A }
2N/A
2N/A if (ldap_server_info->server_status == ON) {
2N/A ldap_server_info->ldap_server_handles = ldap_server_handle->next;
2N/A break;
2N/A } else
2N/A ldap_server_handle = NULL;
2N/A#endif
2N/A }
2N/A }
2N/A ++cnt;
2N/A }
2N/A return ldap_server_handle;
2N/A}
2N/A
2N/A/*
2N/A * This is called incase krb5_get_ldap_handle returns NULL.
2N/A * Try getting a single connection (handle) and return the same by
2N/A * calling krb5_get_ldap_handle function.
2N/A * Do not lock the mutex here. The caller should lock it
2N/A */
2N/A
2N/Astatic krb5_ldap_server_handle *
2N/Akrb5_retry_get_ldap_handle(krb5_ldap_context *ldap_context,
2N/A krb5_error_code *st)
2N/A{
2N/A krb5_ldap_server_handle *ldap_server_handle=NULL;
2N/A
2N/A if ((*st=krb5_ldap_db_single_init(ldap_context)) != 0)
2N/A return NULL;
2N/A
2N/A ldap_server_handle = krb5_get_ldap_handle(ldap_context);
2N/A return ldap_server_handle;
2N/A}
2N/A
2N/A/*
2N/A * Put back the ldap server handle to the front of the list of handles of the
2N/A * ldap server info structure.
2N/A * Do not lock the mutex here. The caller should lock it.
2N/A */
2N/A
2N/Astatic krb5_error_code
2N/Akrb5_put_ldap_handle(krb5_ldap_server_handle *ldap_server_handle)
2N/A{
2N/A
2N/A if (ldap_server_handle == NULL)
2N/A return 0;
2N/A
2N/A ldap_server_handle->next = ldap_server_handle->server_info->ldap_server_handles;
2N/A ldap_server_handle->server_info->ldap_server_handles = ldap_server_handle;
2N/A return 0;
2N/A}
2N/A
2N/A/*
2N/A * Add a new ldap server handle structure to the server info structure.
2N/A * This function name can be changed to krb5_insert_ldap_handle.
2N/A * Do not lock the mutex here. The caller should lock it
2N/A */
2N/A
2N/Akrb5_error_code
2N/Akrb5_update_ldap_handle(krb5_ldap_server_handle *ldap_server_handle,
2N/A krb5_ldap_server_info *server_info)
2N/A{
2N/A
2N/A if (ldap_server_handle == NULL || server_info == NULL)
2N/A return 0;
2N/A
2N/A ldap_server_handle->next = server_info->ldap_server_handles;
2N/A server_info->ldap_server_handles = ldap_server_handle;
2N/A server_info->num_conns++;
2N/A ldap_server_handle->server_info = server_info;
2N/A return 0;
2N/A}
2N/A
2N/A/*
2N/A * Free up all the ldap server handles of the server info.
2N/A * This function is called when the ldap server returns LDAP_SERVER_DOWN.
2N/A */
2N/A
2N/Astatic krb5_error_code
2N/Akrb5_ldap_cleanup_handles(krb5_ldap_server_info *ldap_server_info)
2N/A{
2N/A krb5_ldap_server_handle *ldap_server_handle = NULL;
2N/A
2N/A while (ldap_server_info->ldap_server_handles != NULL) {
2N/A ldap_server_handle = ldap_server_info->ldap_server_handles;
2N/A ldap_server_info->ldap_server_handles = ldap_server_handle->next;
2N/A /* ldap_unbind_s(ldap_server_handle); */
2N/A /* Solaris kerberos: don't leak ldap handles */
2N/A ldap_unbind_s(ldap_server_handle->ldap_handle);
2N/A free (ldap_server_handle);
2N/A ldap_server_handle = NULL;
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/A/*
2N/A * wrapper function called from outside to get a handle.
2N/A */
2N/A
2N/Akrb5_error_code
2N/Akrb5_ldap_request_handle_from_pool(krb5_ldap_context *ldap_context,
2N/A krb5_ldap_server_handle **
2N/A ldap_server_handle)
2N/A{
2N/A krb5_error_code st=0;
2N/A
2N/A *ldap_server_handle = NULL;
2N/A
2N/A st = HNDL_LOCK(ldap_context);
2N/A if (st)
2N/A return st;
2N/A if (((*ldap_server_handle)=krb5_get_ldap_handle(ldap_context)) == NULL)
2N/A (*ldap_server_handle)=krb5_retry_get_ldap_handle(ldap_context, &st);
2N/A HNDL_UNLOCK(ldap_context);
2N/A return st;
2N/A}
2N/A
2N/A/*
2N/A * wrapper function wrapper called to get the next ldap server handle, when the current
2N/A * ldap server handle returns LDAP_SERVER_DOWN.
2N/A */
2N/A
2N/Akrb5_error_code
2N/Akrb5_ldap_request_next_handle_from_pool(krb5_ldap_context *ldap_context,
2N/A krb5_ldap_server_handle **
2N/A ldap_server_handle)
2N/A{
2N/A krb5_error_code st=0;
2N/A
2N/A st = HNDL_LOCK(ldap_context);
2N/A if (st)
2N/A return st;
2N/A (*ldap_server_handle)->server_info->server_status = OFF;
2N/A time(&(*ldap_server_handle)->server_info->downtime);
2N/A krb5_put_ldap_handle(*ldap_server_handle);
2N/A krb5_ldap_cleanup_handles((*ldap_server_handle)->server_info);
2N/A
2N/A if (((*ldap_server_handle)=krb5_get_ldap_handle(ldap_context)) == NULL)
2N/A (*ldap_server_handle)=krb5_retry_get_ldap_handle(ldap_context, &st);
2N/A HNDL_UNLOCK(ldap_context);
2N/A return st;
2N/A}
2N/A
2N/A/*
2N/A * wrapper function to call krb5_put_ldap_handle.
2N/A */
2N/A
2N/Avoid
2N/Akrb5_ldap_put_handle_to_pool(krb5_ldap_context *ldap_context,
2N/A krb5_ldap_server_handle *ldap_server_handle)
2N/A{
2N/A if (ldap_server_handle != NULL) {
2N/A if (HNDL_LOCK(ldap_context) == 0) {
2N/A krb5_put_ldap_handle(ldap_server_handle);
2N/A HNDL_UNLOCK(ldap_context);
2N/A }
2N/A }
2N/A return;
2N/A}