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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* apr_ldap_rebind.c -- LDAP rebind callbacks for referrals
*
* The LDAP SDK allows a callback to be set to enable rebinding
* for referral processing.
*
*/
#include "apr.h"
#include "apu.h"
#include "apu_config.h"
#if APU_DSO_BUILD
#define APU_DSO_LDAP_BUILD
#endif
#include "apr_ldap.h"
#include "apr_errno.h"
#include "apr_strings.h"
#include "apr_ldap_rebind.h"
#include "stdio.h"
#if APR_HAS_LDAP
/* Used to store information about connections for use in the referral rebind callback. */
struct apr_ldap_rebind_entry {
apr_pool_t *pool;
LDAP *index;
const char *bindDN;
const char *bindPW;
struct apr_ldap_rebind_entry *next;
};
typedef struct apr_ldap_rebind_entry apr_ldap_rebind_entry_t;
#ifdef NETWARE
#include "apr_private.h"
#define get_apd APP_DATA* apd = (APP_DATA*)get_app_data(gLibId);
#define apr_ldap_xref_lock ((apr_thread_mutex_t *)(apd->gs_ldap_xref_lock))
#define xref_head ((apr_ldap_rebind_entry_t *)(apd->gs_xref_head))
#else
#if APR_HAS_THREADS
static apr_thread_mutex_t *apr_ldap_xref_lock = NULL;
#endif
static apr_ldap_rebind_entry_t *xref_head = NULL;
#endif
static int apr_ldap_rebind_set_callback(LDAP *ld);
static apr_status_t apr_ldap_rebind_remove_helper(void *data);
static apr_status_t apr_ldap_pool_cleanup_set_null(void *data_)
{
void **ptr = (void **)data_;
*ptr = NULL;
return APR_SUCCESS;
}
/* APR utility routine used to create the xref_lock. */
APU_DECLARE_LDAP(apr_status_t) apr_ldap_rebind_init(apr_pool_t *pool)
{
apr_status_t retcode = APR_SUCCESS;
#ifdef NETWARE
get_apd
#endif
#if APR_HAS_THREADS
/* run after apr_thread_mutex_create cleanup */
apr_pool_cleanup_register(pool, &apr_ldap_xref_lock, apr_ldap_pool_cleanup_set_null,
apr_pool_cleanup_null);
if (apr_ldap_xref_lock == NULL) {
retcode = apr_thread_mutex_create(&apr_ldap_xref_lock, APR_THREAD_MUTEX_DEFAULT, pool);
}
#endif
return(retcode);
}
APU_DECLARE_LDAP(apr_status_t) apr_ldap_rebind_add(apr_pool_t *pool,
LDAP *ld,
const char *bindDN,
const char *bindPW)
{
apr_status_t retcode = APR_SUCCESS;
apr_ldap_rebind_entry_t *new_xref;
#ifdef NETWARE
get_apd
#endif
new_xref = (apr_ldap_rebind_entry_t *)apr_pcalloc(pool, sizeof(apr_ldap_rebind_entry_t));
if (new_xref) {
new_xref->pool = pool;
new_xref->index = ld;
if (bindDN) {
new_xref->bindDN = apr_pstrdup(pool, bindDN);
}
if (bindPW) {
new_xref->bindPW = apr_pstrdup(pool, bindPW);
}
#if APR_HAS_THREADS
retcode = apr_thread_mutex_lock(apr_ldap_xref_lock);
if (retcode != APR_SUCCESS) {
return retcode;
}
#endif
new_xref->next = xref_head;
xref_head = new_xref;
#if APR_HAS_THREADS
retcode = apr_thread_mutex_unlock(apr_ldap_xref_lock);
if (retcode != APR_SUCCESS) {
return retcode;
}
#endif
}
else {
return(APR_ENOMEM);
}
retcode = apr_ldap_rebind_set_callback(ld);
if (APR_SUCCESS != retcode) {
apr_ldap_rebind_remove(ld);
return retcode;
}
apr_pool_cleanup_register(pool, ld,
apr_ldap_rebind_remove_helper,
apr_pool_cleanup_null);
return(APR_SUCCESS);
}
APU_DECLARE_LDAP(apr_status_t) apr_ldap_rebind_remove(LDAP *ld)
{
apr_ldap_rebind_entry_t *tmp_xref, *prev = NULL;
apr_status_t retcode = 0;
#ifdef NETWARE
get_apd
#endif
#if APR_HAS_THREADS
retcode = apr_thread_mutex_lock(apr_ldap_xref_lock);
if (retcode != APR_SUCCESS) {
return retcode;
}
#endif
tmp_xref = xref_head;
while ((tmp_xref) && (tmp_xref->index != ld)) {
prev = tmp_xref;
tmp_xref = tmp_xref->next;
}
if (tmp_xref) {
if (tmp_xref == xref_head) {
xref_head = xref_head->next;
}
else {
prev->next = tmp_xref->next;
}
/* tmp_xref and its contents were pool allocated so they don't need to be freed here. */
/* remove the cleanup, just in case this was done manually */
apr_pool_cleanup_kill(tmp_xref->pool, tmp_xref->index,
apr_ldap_rebind_remove_helper);
}
#if APR_HAS_THREADS
retcode = apr_thread_mutex_unlock(apr_ldap_xref_lock);
if (retcode != APR_SUCCESS) {
return retcode;
}
#endif
return APR_SUCCESS;
}
static apr_status_t apr_ldap_rebind_remove_helper(void *data)
{
LDAP *ld = (LDAP *)data;
apr_ldap_rebind_remove(ld);
return APR_SUCCESS;
}
#if APR_HAS_TIVOLI_LDAPSDK || APR_HAS_OPENLDAP_LDAPSDK || APR_HAS_NOVELL_LDAPSDK
static apr_ldap_rebind_entry_t *apr_ldap_rebind_lookup(LDAP *ld)
{
apr_ldap_rebind_entry_t *tmp_xref, *match = NULL;
#ifdef NETWARE
get_apd
#endif
#if APR_HAS_THREADS
apr_thread_mutex_lock(apr_ldap_xref_lock);
#endif
tmp_xref = xref_head;
while (tmp_xref) {
if (tmp_xref->index == ld) {
match = tmp_xref;
tmp_xref = NULL;
}
else {
tmp_xref = tmp_xref->next;
}
}
#if APR_HAS_THREADS
apr_thread_mutex_unlock(apr_ldap_xref_lock);
#endif
return (match);
}
#endif
#if APR_HAS_TIVOLI_LDAPSDK
/* LDAP_rebindproc() Tivoli LDAP style
* Rebind callback function. Called when chasing referrals. See API docs.
* ON ENTRY:
* ld Pointer to an LDAP control structure. (input only)
* binddnp Pointer to an Application DName used for binding (in *or* out)
* passwdp Pointer to the password associated with the DName (in *or* out)
* methodp Pointer to the Auth method (output only)
* freeit Flag to indicate if this is a lookup or a free request (input only)
*/
static int LDAP_rebindproc(LDAP *ld, char **binddnp, char **passwdp, int *methodp, int freeit)
{
if (!freeit) {
apr_ldap_rebind_entry_t *my_conn;
*methodp = LDAP_AUTH_SIMPLE;
my_conn = apr_ldap_rebind_lookup(ld);
if ((my_conn) && (my_conn->bindDN != NULL)) {
*binddnp = strdup(my_conn->bindDN);
*passwdp = strdup(my_conn->bindPW);
} else {
*binddnp = NULL;
*passwdp = NULL;
}
} else {
if (*binddnp) {
free(*binddnp);
}
if (*passwdp) {
free(*passwdp);
}
}
return LDAP_SUCCESS;
}
static int apr_ldap_rebind_set_callback(LDAP *ld)
{
ldap_set_rebind_proc(ld, (LDAPRebindProc)LDAP_rebindproc);
return APR_SUCCESS;
}
#elif APR_HAS_OPENLDAP_LDAPSDK
/* LDAP_rebindproc() openLDAP V3 style
* ON ENTRY:
* ld Pointer to an LDAP control structure. (input only)
* url Unused in this routine
* request Unused in this routine
* msgid Unused in this routine
* params Unused in this routine
*
* or
*
* ld Pointer to an LDAP control structure. (input only)
* url Unused in this routine
* request Unused in this routine
* msgid Unused in this routine
*/
#if defined(LDAP_SET_REBIND_PROC_THREE)
static int LDAP_rebindproc(LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
ber_int_t msgid, void *params)
#else
static int LDAP_rebindproc(LDAP *ld, LDAP_CONST char *url, int request,
ber_int_t msgid)
#endif
{
apr_ldap_rebind_entry_t *my_conn;
const char *bindDN = NULL;
const char *bindPW = NULL;
my_conn = apr_ldap_rebind_lookup(ld);
if ((my_conn) && (my_conn->bindDN != NULL)) {
bindDN = my_conn->bindDN;
bindPW = my_conn->bindPW;
}
return (ldap_bind_s(ld, bindDN, bindPW, LDAP_AUTH_SIMPLE));
}
static int apr_ldap_rebind_set_callback(LDAP *ld)
{
#if defined(LDAP_SET_REBIND_PROC_THREE)
ldap_set_rebind_proc(ld, LDAP_rebindproc, NULL);
#else
ldap_set_rebind_proc(ld, LDAP_rebindproc);
#endif
return APR_SUCCESS;
}
#elif APR_HAS_NOVELL_LDAPSDK
/* LDAP_rebindproc() openLDAP V3 style
* ON ENTRY:
* ld Pointer to an LDAP control structure. (input only)
* url Unused in this routine
* request Unused in this routine
* msgid Unused in this routine
*/
static int LDAP_rebindproc(LDAP *ld, LDAP_CONST char *url, int request, ber_int_t msgid)
{
apr_ldap_rebind_entry_t *my_conn;
const char *bindDN = NULL;
const char *bindPW = NULL;
my_conn = apr_ldap_rebind_lookup(ld);
if ((my_conn) && (my_conn->bindDN != NULL)) {
bindDN = my_conn->bindDN;
bindPW = my_conn->bindPW;
}
return (ldap_bind_s(ld, bindDN, bindPW, LDAP_AUTH_SIMPLE));
}
static int apr_ldap_rebind_set_callback(LDAP *ld)
{
ldap_set_rebind_proc(ld, LDAP_rebindproc);
return APR_SUCCESS;
}
#else /* Implementation not recognised */
static int apr_ldap_rebind_set_callback(LDAP *ld)
{
return APR_ENOTIMPL;
}
#endif
#endif /* APR_HAS_LDAP */
|