1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* apr_ldap_rebind.c -- LDAP rebind callbacks for referrals
19 * The LDAP SDK allows a callback to be set to enable rebinding
20 * for referral processing.
26 #include "apu_config.h"
29 #define APU_DSO_LDAP_BUILD
33 #include "apr_errno.h"
34 #include "apr_strings.h"
35 #include "apr_ldap_rebind.h"
41 /* Used to store information about connections for use in the referral rebind callback. */
42 struct apr_ldap_rebind_entry {
47 struct apr_ldap_rebind_entry *next;
49 typedef struct apr_ldap_rebind_entry apr_ldap_rebind_entry_t;
53 #include "apr_private.h"
54 #define get_apd APP_DATA* apd = (APP_DATA*)get_app_data(gLibId);
55 #define apr_ldap_xref_lock ((apr_thread_mutex_t *)(apd->gs_ldap_xref_lock))
56 #define xref_head ((apr_ldap_rebind_entry_t *)(apd->gs_xref_head))
59 static apr_thread_mutex_t *apr_ldap_xref_lock = NULL;
61 static apr_ldap_rebind_entry_t *xref_head = NULL;
64 static int apr_ldap_rebind_set_callback(LDAP *ld);
65 static apr_status_t apr_ldap_rebind_remove_helper(void *data);
67 static apr_status_t apr_ldap_pool_cleanup_set_null(void *data_)
69 void **ptr = (void **)data_;
75 /* APR utility routine used to create the xref_lock. */
76 APU_DECLARE_LDAP(apr_status_t) apr_ldap_rebind_init(apr_pool_t *pool)
78 apr_status_t retcode = APR_SUCCESS;
85 /* run after apr_thread_mutex_create cleanup */
86 apr_pool_cleanup_register(pool, &apr_ldap_xref_lock, apr_ldap_pool_cleanup_set_null,
87 apr_pool_cleanup_null);
89 if (apr_ldap_xref_lock == NULL) {
90 retcode = apr_thread_mutex_create(&apr_ldap_xref_lock, APR_THREAD_MUTEX_DEFAULT, pool);
98 APU_DECLARE_LDAP(apr_status_t) apr_ldap_rebind_add(apr_pool_t *pool,
103 apr_status_t retcode = APR_SUCCESS;
104 apr_ldap_rebind_entry_t *new_xref;
110 new_xref = (apr_ldap_rebind_entry_t *)apr_pcalloc(pool, sizeof(apr_ldap_rebind_entry_t));
112 new_xref->pool = pool;
113 new_xref->index = ld;
115 new_xref->bindDN = apr_pstrdup(pool, bindDN);
118 new_xref->bindPW = apr_pstrdup(pool, bindPW);
122 retcode = apr_thread_mutex_lock(apr_ldap_xref_lock);
123 if (retcode != APR_SUCCESS) {
128 new_xref->next = xref_head;
129 xref_head = new_xref;
132 retcode = apr_thread_mutex_unlock(apr_ldap_xref_lock);
133 if (retcode != APR_SUCCESS) {
142 retcode = apr_ldap_rebind_set_callback(ld);
143 if (APR_SUCCESS != retcode) {
144 apr_ldap_rebind_remove(ld);
148 apr_pool_cleanup_register(pool, ld,
149 apr_ldap_rebind_remove_helper,
150 apr_pool_cleanup_null);
156 APU_DECLARE_LDAP(apr_status_t) apr_ldap_rebind_remove(LDAP *ld)
158 apr_ldap_rebind_entry_t *tmp_xref, *prev = NULL;
159 apr_status_t retcode = 0;
166 retcode = apr_thread_mutex_lock(apr_ldap_xref_lock);
167 if (retcode != APR_SUCCESS) {
171 tmp_xref = xref_head;
173 while ((tmp_xref) && (tmp_xref->index != ld)) {
175 tmp_xref = tmp_xref->next;
179 if (tmp_xref == xref_head) {
180 xref_head = xref_head->next;
183 prev->next = tmp_xref->next;
186 /* tmp_xref and its contents were pool allocated so they don't need to be freed here. */
188 /* remove the cleanup, just in case this was done manually */
189 apr_pool_cleanup_kill(tmp_xref->pool, tmp_xref->index,
190 apr_ldap_rebind_remove_helper);
194 retcode = apr_thread_mutex_unlock(apr_ldap_xref_lock);
195 if (retcode != APR_SUCCESS) {
203 static apr_status_t apr_ldap_rebind_remove_helper(void *data)
205 LDAP *ld = (LDAP *)data;
206 apr_ldap_rebind_remove(ld);
210 #if APR_HAS_TIVOLI_LDAPSDK || APR_HAS_OPENLDAP_LDAPSDK || APR_HAS_NOVELL_LDAPSDK
211 static apr_ldap_rebind_entry_t *apr_ldap_rebind_lookup(LDAP *ld)
213 apr_ldap_rebind_entry_t *tmp_xref, *match = NULL;
220 apr_thread_mutex_lock(apr_ldap_xref_lock);
222 tmp_xref = xref_head;
225 if (tmp_xref->index == ld) {
230 tmp_xref = tmp_xref->next;
235 apr_thread_mutex_unlock(apr_ldap_xref_lock);
242 #if APR_HAS_TIVOLI_LDAPSDK
244 /* LDAP_rebindproc() Tivoli LDAP style
245 * Rebind callback function. Called when chasing referrals. See API docs.
247 * ld Pointer to an LDAP control structure. (input only)
248 * binddnp Pointer to an Application DName used for binding (in *or* out)
249 * passwdp Pointer to the password associated with the DName (in *or* out)
250 * methodp Pointer to the Auth method (output only)
251 * freeit Flag to indicate if this is a lookup or a free request (input only)
253 static int LDAP_rebindproc(LDAP *ld, char **binddnp, char **passwdp, int *methodp, int freeit)
256 apr_ldap_rebind_entry_t *my_conn;
258 *methodp = LDAP_AUTH_SIMPLE;
259 my_conn = apr_ldap_rebind_lookup(ld);
261 if ((my_conn) && (my_conn->bindDN != NULL)) {
262 *binddnp = strdup(my_conn->bindDN);
263 *passwdp = strdup(my_conn->bindPW);
280 static int apr_ldap_rebind_set_callback(LDAP *ld)
282 ldap_set_rebind_proc(ld, (LDAPRebindProc)LDAP_rebindproc);
286 #elif APR_HAS_OPENLDAP_LDAPSDK
288 /* LDAP_rebindproc() openLDAP V3 style
290 * ld Pointer to an LDAP control structure. (input only)
291 * url Unused in this routine
292 * request Unused in this routine
293 * msgid Unused in this routine
294 * params Unused in this routine
298 * ld Pointer to an LDAP control structure. (input only)
299 * url Unused in this routine
300 * request Unused in this routine
301 * msgid Unused in this routine
303 #if defined(LDAP_SET_REBIND_PROC_THREE)
304 static int LDAP_rebindproc(LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
305 ber_int_t msgid, void *params)
307 static int LDAP_rebindproc(LDAP *ld, LDAP_CONST char *url, int request,
311 apr_ldap_rebind_entry_t *my_conn;
312 const char *bindDN = NULL;
313 const char *bindPW = NULL;
315 my_conn = apr_ldap_rebind_lookup(ld);
317 if ((my_conn) && (my_conn->bindDN != NULL)) {
318 bindDN = my_conn->bindDN;
319 bindPW = my_conn->bindPW;
322 return (ldap_bind_s(ld, bindDN, bindPW, LDAP_AUTH_SIMPLE));
325 static int apr_ldap_rebind_set_callback(LDAP *ld)
327 #if defined(LDAP_SET_REBIND_PROC_THREE)
328 ldap_set_rebind_proc(ld, LDAP_rebindproc, NULL);
330 ldap_set_rebind_proc(ld, LDAP_rebindproc);
335 #elif APR_HAS_NOVELL_LDAPSDK
337 /* LDAP_rebindproc() openLDAP V3 style
339 * ld Pointer to an LDAP control structure. (input only)
340 * url Unused in this routine
341 * request Unused in this routine
342 * msgid Unused in this routine
344 static int LDAP_rebindproc(LDAP *ld, LDAP_CONST char *url, int request, ber_int_t msgid)
347 apr_ldap_rebind_entry_t *my_conn;
348 const char *bindDN = NULL;
349 const char *bindPW = NULL;
351 my_conn = apr_ldap_rebind_lookup(ld);
353 if ((my_conn) && (my_conn->bindDN != NULL)) {
354 bindDN = my_conn->bindDN;
355 bindPW = my_conn->bindPW;
358 return (ldap_bind_s(ld, bindDN, bindPW, LDAP_AUTH_SIMPLE));
361 static int apr_ldap_rebind_set_callback(LDAP *ld)
363 ldap_set_rebind_proc(ld, LDAP_rebindproc);
367 #else /* Implementation not recognised */
369 static int apr_ldap_rebind_set_callback(LDAP *ld)
377 #endif /* APR_HAS_LDAP */