summaryrefslogtreecommitdiffstats
path: root/src/plugins/kdb/ldap/libkdb_ldap/ldap_handle.c
blob: 2188b2d3190e8c06f20ef5c084e95926138c0aaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* plugins/kdb/ldap/libkdb_ldap/ldap_handle.c */
/*
 * Copyright (c) 2004-2005, Novell, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *   * The copyright holder's name is not used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "ldap_main.h"


#ifdef ASYNC_BIND

/*
 * Update the server info structure. In case of an asynchronous bind,
 * this function is called to check the bind status. A flag
 * server_info_upate_pending is refered before calling this function.
 * This function sets the server_status to either ON or OFF and
 * sets the server_info_udpate_pending to OFF.
 * Do not lock the mutex here. The caller should lock it
 */

static krb5_error_code
krb5_update_server_info(krb5_ldap_server_handle *ldap_server_handle,
                        krb5_ldap_server_info *server_info)
{
    krb5_error_code            st=0;
    struct timeval             ztime={0, 0};
    LDAPMessage                *result=NULL;

    if (ldap_server_handle == NULL || server_info == NULL)
        return -1;

    while (st == 0) {
        st = ldap_result(ldap_server_handle->ldap_handle, ldap_server_handle->msgid,
                         LDAP_MSG_ALL, &ztime, &result);
        switch (st) {
        case -1:
            server_info->server_status = OFF;
            time(&server_info->downtime);
            break;

        case 0:
            continue;
            break;

        case LDAP_RES_BIND:
            if ((st=ldap_result2error(ldap_server_handle->ldap_handle, result, 1)) == LDAP_SUCCESS) {
                server_info->server_status = ON;
            } else {
                /* ?? */        krb5_set_error_message(0, 0, "%s", ldap_err2string(st));
                server_info->server_status = OFF;
                time(&server_info->downtime);
            }
            ldap_msgfree(result);
            break;
        default:
            ldap_msgfree(result);
            continue;
            break;
        }
    }
    ldap_server_handle->server_info_update_pending = FALSE;
    return 0;
}
#endif

/*
 * Return ldap server handle from the pool. If the pool is exhausted return NULL.
 * Do not lock the mutex, caller should lock it
 */

static krb5_ldap_server_handle *
krb5_get_ldap_handle(krb5_ldap_context *ldap_context)
{
    krb5_ldap_server_handle    *ldap_server_handle=NULL;
    krb5_ldap_server_info      *ldap_server_info=NULL;
    int                        cnt=0;

    while (ldap_context->server_info_list[cnt] != NULL) {
        ldap_server_info = ldap_context->server_info_list[cnt];
        if (ldap_server_info->server_status != OFF) {
            if (ldap_server_info->ldap_server_handles != NULL) {
                ldap_server_handle = ldap_server_info->ldap_server_handles;
                ldap_server_info->ldap_server_handles = ldap_server_handle->next;
                break;
#ifdef ASYNC_BIND
                if (ldap_server_handle->server_info_update_pending == TRUE) {
                    krb5_update_server_info(context, ldap_server_handle,
                                            ldap_server_info);
                }

                if (ldap_server_info->server_status == ON) {
                    ldap_server_info->ldap_server_handles = ldap_server_handle->next;
                    break;
                } else
                    ldap_server_handle = NULL;
#endif
            }
        }
        ++cnt;
    }
    return ldap_server_handle;
}

/*
 * This is called incase krb5_get_ldap_handle returns NULL.
 * Try getting a single connection (handle) and return the same by
 * calling krb5_get_ldap_handle function.
 * Do not lock the mutex here. The caller should lock it
 */

static krb5_ldap_server_handle *
krb5_retry_get_ldap_handle(krb5_ldap_context *ldap_context,
                           krb5_error_code *st)
{
    krb5_ldap_server_handle    *ldap_server_handle=NULL;

    if ((*st=krb5_ldap_db_single_init(ldap_context)) != 0)
        return NULL;

    ldap_server_handle = krb5_get_ldap_handle(ldap_context);
    return ldap_server_handle;
}

/*
 * Put back the ldap server handle to the front of the list of handles of the
 * ldap server info structure.
 * Do not lock the mutex here. The caller should lock it.
 */

static krb5_error_code
krb5_put_ldap_handle(krb5_ldap_server_handle *ldap_server_handle)
{

    if (ldap_server_handle == NULL)
        return 0;

    ldap_server_handle->next = ldap_server_handle->server_info->ldap_server_handles;
    ldap_server_handle->server_info->ldap_server_handles = ldap_server_handle;
    return 0;
}

/*
 * Add a new ldap server handle structure to the server info structure.
 * This function name can be changed to krb5_insert_ldap_handle.
 * Do not lock the mutex here. The caller should lock it
 */

krb5_error_code
krb5_update_ldap_handle(krb5_ldap_server_handle *ldap_server_handle,
                        krb5_ldap_server_info *server_info)
{

    if (ldap_server_handle == NULL || server_info == NULL)
        return 0;

    ldap_server_handle->next = server_info->ldap_server_handles;
    server_info->ldap_server_handles = ldap_server_handle;
    server_info->num_conns++;
    ldap_server_handle->server_info = server_info;
    return 0;
}

/*
 * Free up all the ldap server handles of the server info.
 * This function is called when the ldap server returns LDAP_SERVER_DOWN.
 */

static krb5_error_code
krb5_ldap_cleanup_handles(krb5_ldap_server_info *ldap_server_info)
{
    krb5_ldap_server_handle    *ldap_server_handle = NULL;

    while (ldap_server_info->ldap_server_handles != NULL) {
        ldap_server_handle = ldap_server_info->ldap_server_handles;
        ldap_server_info->ldap_server_handles = ldap_server_handle->next;
        /* ldap_unbind_s(ldap_server_handle); */
        free (ldap_server_handle);
        ldap_server_handle = NULL;
    }
    return 0;
}

/*
 * wrapper function called from outside to get a handle.
 */

krb5_error_code
krb5_ldap_request_handle_from_pool(krb5_ldap_context *ldap_context,
                                   krb5_ldap_server_handle **
                                   ldap_server_handle)
{
    krb5_error_code            st=0;

    *ldap_server_handle = NULL;

    HNDL_LOCK(ldap_context);
    if (((*ldap_server_handle)=krb5_get_ldap_handle(ldap_context)) == NULL)
        (*ldap_server_handle)=krb5_retry_get_ldap_handle(ldap_context, &st);
    HNDL_UNLOCK(ldap_context);
    return st;
}

/*
 * wrapper function wrapper called to get the next ldap server handle, when the current
 * ldap server handle returns LDAP_SERVER_DOWN.
 */

krb5_error_code
krb5_ldap_request_next_handle_from_pool(krb5_ldap_context *ldap_context,
                                        krb5_ldap_server_handle **
                                        ldap_server_handle)
{
    krb5_error_code            st=0;

    HNDL_LOCK(ldap_context);
    (*ldap_server_handle)->server_info->server_status = OFF;
    time(&(*ldap_server_handle)->server_info->downtime);
    krb5_put_ldap_handle(*ldap_server_handle);
    krb5_ldap_cleanup_handles((*ldap_server_handle)->server_info);

    if (((*ldap_server_handle)=krb5_get_ldap_handle(ldap_context)) == NULL)
        (*ldap_server_handle)=krb5_retry_get_ldap_handle(ldap_context, &st);
    HNDL_UNLOCK(ldap_context);
    return st;
}

/*
 * wrapper function to call krb5_put_ldap_handle.
 */

void
krb5_ldap_put_handle_to_pool(krb5_ldap_context *ldap_context,
                             krb5_ldap_server_handle *ldap_server_handle)
{
    if (ldap_server_handle != NULL) {
        HNDL_LOCK(ldap_context);
        krb5_put_ldap_handle(ldap_server_handle);
        HNDL_UNLOCK(ldap_context);
    }
    return;
}