| 1 |
/* $OpenLDAP$ */ |
| 2 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 3 |
* |
| 4 |
* Copyright 1999-2024 The OpenLDAP Foundation. |
| 5 |
* Portions Copyright 2001-2003 Pierangelo Masarati. |
| 6 |
* Portions Copyright 1999-2003 Howard Chu. |
| 7 |
* All rights reserved. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted only as authorized by the OpenLDAP |
| 11 |
* Public License. |
| 12 |
* |
| 13 |
* A copy of this license is available in the file LICENSE in the |
| 14 |
* top-level directory of the distribution or, alternatively, at |
| 15 |
* <http://www.OpenLDAP.org/license.html>. |
| 16 |
*/ |
| 17 |
/* ACKNOWLEDGEMENTS: |
| 18 |
* This work was initially developed by the Howard Chu for inclusion |
| 19 |
* in OpenLDAP Software and subsequently enhanced by Pierangelo |
| 20 |
* Masarati. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "portable.h" |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
|
| 27 |
#include <ac/errno.h> |
| 28 |
#include <ac/socket.h> |
| 29 |
#include <ac/string.h> |
| 30 |
|
| 31 |
|
| 32 |
#define AVL_INTERNAL |
| 33 |
#include "slap.h" |
| 34 |
#include "../back-ldap/back-ldap.h" |
| 35 |
#include "back-meta.h" |
| 36 |
|
| 37 |
/* |
| 38 |
* meta_back_conndn_cmp |
| 39 |
* |
| 40 |
* compares two struct metaconn based on the value of the conn pointer |
| 41 |
* and of the local DN; used by avl stuff |
| 42 |
*/ |
| 43 |
int |
| 44 |
meta_back_conndn_cmp( |
| 45 |
const void *c1, |
| 46 |
const void *c2 ) |
| 47 |
{ |
| 48 |
metaconn_t *mc1 = ( metaconn_t * )c1; |
| 49 |
metaconn_t *mc2 = ( metaconn_t * )c2; |
| 50 |
int rc; |
| 51 |
|
| 52 |
/* If local DNs don't match, it is definitely not a match */ |
| 53 |
/* For shared sessions, conn is NULL. Only explicitly |
| 54 |
* bound sessions will have non-NULL conn. |
| 55 |
*/ |
| 56 |
rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn ); |
| 57 |
if ( rc == 0 ) { |
| 58 |
rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn ); |
| 59 |
} |
| 60 |
|
| 61 |
return rc; |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
* meta_back_conndnmc_cmp |
| 66 |
* |
| 67 |
* compares two struct metaconn based on the value of the conn pointer, |
| 68 |
* the local DN and the struct pointer; used by avl stuff |
| 69 |
*/ |
| 70 |
static int |
| 71 |
meta_back_conndnmc_cmp( |
| 72 |
const void *c1, |
| 73 |
const void *c2 ) |
| 74 |
{ |
| 75 |
metaconn_t *mc1 = ( metaconn_t * )c1; |
| 76 |
metaconn_t *mc2 = ( metaconn_t * )c2; |
| 77 |
int rc; |
| 78 |
|
| 79 |
/* If local DNs don't match, it is definitely not a match */ |
| 80 |
/* For shared sessions, conn is NULL. Only explicitly |
| 81 |
* bound sessions will have non-NULL conn. |
| 82 |
*/ |
| 83 |
rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn ); |
| 84 |
if ( rc == 0 ) { |
| 85 |
rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn ); |
| 86 |
if ( rc == 0 ) { |
| 87 |
rc = SLAP_PTRCMP( mc1, mc2 ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return rc; |
| 92 |
} |
| 93 |
|
| 94 |
/* |
| 95 |
* meta_back_conn_cmp |
| 96 |
* |
| 97 |
* compares two struct metaconn based on the value of the conn pointer; |
| 98 |
* used by avl stuff |
| 99 |
*/ |
| 100 |
int |
| 101 |
meta_back_conn_cmp( |
| 102 |
const void *c1, |
| 103 |
const void *c2 ) |
| 104 |
{ |
| 105 |
metaconn_t *mc1 = ( metaconn_t * )c1; |
| 106 |
metaconn_t *mc2 = ( metaconn_t * )c2; |
| 107 |
|
| 108 |
/* For shared sessions, conn is NULL. Only explicitly |
| 109 |
* bound sessions will have non-NULL conn. |
| 110 |
*/ |
| 111 |
return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn ); |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
* meta_back_conndn_dup |
| 116 |
* |
| 117 |
* returns -1 in case a duplicate struct metaconn has been inserted; |
| 118 |
* used by avl stuff |
| 119 |
*/ |
| 120 |
int |
| 121 |
meta_back_conndn_dup( |
| 122 |
void *c1, |
| 123 |
void *c2 ) |
| 124 |
{ |
| 125 |
metaconn_t *mc1 = ( metaconn_t * )c1; |
| 126 |
metaconn_t *mc2 = ( metaconn_t * )c2; |
| 127 |
|
| 128 |
/* Cannot have more than one shared session with same DN */ |
| 129 |
if ( mc1->mc_conn == mc2->mc_conn && |
| 130 |
dn_match( &mc1->mc_local_ndn, &mc2->mc_local_ndn ) ) |
| 131 |
{ |
| 132 |
return -1; |
| 133 |
} |
| 134 |
|
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
/* |
| 139 |
* Debug stuff (got it from libavl) |
| 140 |
*/ |
| 141 |
#if META_BACK_PRINT_CONNTREE > 0 |
| 142 |
static void |
| 143 |
meta_back_print( metaconn_t *mc, char *avlstr ) |
| 144 |
{ |
| 145 |
int i; |
| 146 |
|
| 147 |
fputs( "targets=[", stderr ); |
| 148 |
for ( i = 0; i < mc->mc_info->mi_ntargets; i++ ) { |
| 149 |
fputc( mc->mc_conns[ i ].msc_ld ? '*' : 'o', stderr); |
| 150 |
} |
| 151 |
fputc( ']', stderr ); |
| 152 |
|
| 153 |
fprintf( stderr, " mc=%p local=\"%s\" conn=%p refcnt=%d%s %s\n", |
| 154 |
(void *)mc, |
| 155 |
mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "", |
| 156 |
(void *)mc->mc_conn, |
| 157 |
mc->mc_refcnt, |
| 158 |
LDAP_BACK_CONN_TAINTED( mc ) ? " tainted" : "", |
| 159 |
avlstr ); |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
meta_back_ravl_print( TAvlnode *root, int depth ) |
| 164 |
{ |
| 165 |
int i; |
| 166 |
|
| 167 |
if ( root == 0 ) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
meta_back_ravl_print( root->avl_right, depth + 1 ); |
| 172 |
|
| 173 |
for ( i = 0; i < depth; i++ ) { |
| 174 |
fprintf( stderr, "-" ); |
| 175 |
} |
| 176 |
fputc( ' ', stderr ); |
| 177 |
|
| 178 |
meta_back_print( (metaconn_t *)root->avl_data, |
| 179 |
avl_bf2str( root->avl_bf ) ); |
| 180 |
|
| 181 |
meta_back_ravl_print( root->avl_left, depth + 1 ); |
| 182 |
} |
| 183 |
|
| 184 |
/* NOTE: duplicate from back-ldap/bind.c */ |
| 185 |
static char* priv2str[] = { |
| 186 |
"privileged", |
| 187 |
"privileged/TLS", |
| 188 |
"anonymous", |
| 189 |
"anonymous/TLS", |
| 190 |
"bind", |
| 191 |
"bind/TLS", |
| 192 |
NULL |
| 193 |
}; |
| 194 |
|
| 195 |
void |
| 196 |
meta_back_print_conntree( metainfo_t *mi, char *msg ) |
| 197 |
{ |
| 198 |
int c; |
| 199 |
|
| 200 |
fprintf( stderr, "========> %s\n", msg ); |
| 201 |
|
| 202 |
for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) { |
| 203 |
int i = 0; |
| 204 |
metaconn_t *mc; |
| 205 |
|
| 206 |
fprintf( stderr, " %s[%d]\n", priv2str[ c ], mi->mi_conn_priv[ c ].mic_num ); |
| 207 |
|
| 208 |
LDAP_TAILQ_FOREACH( mc, &mi->mi_conn_priv[ c ].mic_priv, mc_q ) |
| 209 |
{ |
| 210 |
fprintf( stderr, " [%d] ", i ); |
| 211 |
meta_back_print( mc, "" ); |
| 212 |
i++; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
if ( mi->mi_conninfo.lai_tree == NULL ) { |
| 217 |
fprintf( stderr, "\t(empty)\n" ); |
| 218 |
|
| 219 |
} else { |
| 220 |
meta_back_ravl_print( mi->mi_conninfo.lai_tree, 0 ); |
| 221 |
} |
| 222 |
|
| 223 |
fprintf( stderr, "<======== %s\n", msg ); |
| 224 |
} |
| 225 |
#endif /* META_BACK_PRINT_CONNTREE */ |
| 226 |
/* |
| 227 |
* End of debug stuff |
| 228 |
*/ |
| 229 |
|
| 230 |
/* |
| 231 |
* metaconn_alloc |
| 232 |
* |
| 233 |
* Allocates a connection structure, making room for all the referenced targets |
| 234 |
*/ |
| 235 |
static metaconn_t * |
| 236 |
metaconn_alloc( |
| 237 |
Operation *op ) |
| 238 |
{ |
| 239 |
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private; |
| 240 |
metaconn_t *mc; |
| 241 |
int ntargets = mi->mi_ntargets; |
| 242 |
|
| 243 |
assert( ntargets > 0 ); |
| 244 |
|
| 245 |
/* malloc all in one */ |
| 246 |
mc = ( metaconn_t * )ch_calloc( 1, sizeof( metaconn_t ) |
| 247 |
+ sizeof( metasingleconn_t ) * ( ntargets - 1 ) ); |
| 248 |
if ( mc == NULL ) { |
| 249 |
return NULL; |
| 250 |
} |
| 251 |
|
| 252 |
mc->mc_info = mi; |
| 253 |
|
| 254 |
mc->mc_authz_target = META_BOUND_NONE; |
| 255 |
mc->mc_refcnt = 1; |
| 256 |
|
| 257 |
return mc; |
| 258 |
} |
| 259 |
|
| 260 |
/* |
| 261 |
* meta_back_init_one_conn |
| 262 |
* |
| 263 |
* Initializes one connection |
| 264 |
*/ |
| 265 |
int |
| 266 |
meta_back_init_one_conn( |
| 267 |
Operation *op, |
| 268 |
SlapReply *rs, |
| 269 |
metaconn_t *mc, |
| 270 |
int candidate, |
| 271 |
int ispriv, |
| 272 |
ldap_back_send_t sendok, |
| 273 |
int dolock ) |
| 274 |
{ |
| 275 |
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private; |
| 276 |
metatarget_t *mt = mi->mi_targets[ candidate ]; |
| 277 |
metasingleconn_t *msc = &mc->mc_conns[ candidate ]; |
| 278 |
int version; |
| 279 |
dncookie dc; |
| 280 |
int isauthz = ( candidate == mc->mc_authz_target ); |
| 281 |
int do_return = 0; |
| 282 |
#ifdef HAVE_TLS |
| 283 |
int is_ldaps = 0; |
| 284 |
int do_start_tls = 0; |
| 285 |
#endif /* HAVE_TLS */ |
| 286 |
|
| 287 |
/* if the server is quarantined, and |
| 288 |
* - the current interval did not expire yet, or |
| 289 |
* - no more retries should occur, |
| 290 |
* don't return the connection */ |
| 291 |
if ( mt->mt_isquarantined ) { |
| 292 |
slap_retry_info_t *ri = &mt->mt_quarantine; |
| 293 |
int dont_retry = 0; |
| 294 |
|
| 295 |
if ( mt->mt_quarantine.ri_interval ) { |
| 296 |
ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex ); |
| 297 |
dont_retry = ( mt->mt_isquarantined > LDAP_BACK_FQ_NO ); |
| 298 |
if ( dont_retry ) { |
| 299 |
dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL |
| 300 |
|| slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] ); |
| 301 |
if ( !dont_retry ) { |
| 302 |
Debug(LDAP_DEBUG_ANY, |
| 303 |
"%s meta_back_init_one_conn[%d]: quarantine " "retry block #%d try #%d.\n", |
| 304 |
op->o_log_prefix, |
| 305 |
candidate, ri->ri_idx, |
| 306 |
ri->ri_count ); |
| 307 |
|
| 308 |
mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING; |
| 309 |
} |
| 310 |
|
| 311 |
} |
| 312 |
ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex ); |
| 313 |
} |
| 314 |
|
| 315 |
if ( dont_retry ) { |
| 316 |
rs->sr_err = LDAP_UNAVAILABLE; |
| 317 |
if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) { |
| 318 |
rs->sr_text = "Target is quarantined"; |
| 319 |
send_ldap_result( op, rs ); |
| 320 |
} |
| 321 |
return rs->sr_err; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
retry_lock:; |
| 326 |
if ( dolock ) { |
| 327 |
ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex ); |
| 328 |
} |
| 329 |
|
| 330 |
/* |
| 331 |
* Already init'ed |
| 332 |
*/ |
| 333 |
if ( LDAP_BACK_CONN_ISBOUND( msc ) |
| 334 |
|| LDAP_BACK_CONN_ISANON( msc ) ) |
| 335 |
{ |
| 336 |
assert( msc->msc_ld != NULL ); |
| 337 |
rs->sr_err = LDAP_SUCCESS; |
| 338 |
do_return = 1; |
| 339 |
|
| 340 |
} else if ( META_BACK_CONN_CREATING( msc ) |
| 341 |
|| LDAP_BACK_CONN_BINDING( msc ) ) |
| 342 |
{ |
| 343 |
if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) { |
| 344 |
if ( dolock ) { |
| 345 |
ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex ); |
| 346 |
} |
| 347 |
|
| 348 |
ldap_pvt_thread_yield(); |
| 349 |
goto retry_lock; |
| 350 |
} |
| 351 |
|
| 352 |
/* sounds more appropriate */ |
| 353 |
rs->sr_err = LDAP_BUSY; |
| 354 |
rs->sr_text = "No connections to target are available"; |
| 355 |
do_return = 1; |
| 356 |
|
| 357 |
} else if ( META_BACK_CONN_INITED( msc ) ) { |
| 358 |
assert( msc->msc_ld != NULL ); |
| 359 |
rs->sr_err = LDAP_SUCCESS; |
| 360 |
do_return = 1; |
| 361 |
|
| 362 |
} else { |
| 363 |
/* |
| 364 |
* creating... |
| 365 |
*/ |
| 366 |
META_BACK_CONN_CREATING_SET( msc ); |
| 367 |
} |
| 368 |
|
| 369 |
if ( dolock ) { |
| 370 |
ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex ); |
| 371 |
} |
| 372 |
|
| 373 |
if ( do_return ) { |
| 374 |
if ( rs->sr_err != LDAP_SUCCESS |
| 375 |
&& op->o_conn |
| 376 |
&& ( sendok & LDAP_BACK_SENDERR ) ) |
| 377 |
{ |
| 378 |
send_ldap_result( op, rs ); |
| 379 |
} |
| 380 |
|
| 381 |
return rs->sr_err; |
| 382 |
} |
| 383 |
|
| 384 |
assert( msc->msc_ld == NULL ); |
| 385 |
|
| 386 |
/* |
| 387 |
* Attempts to initialize the connection to the target ds |
| 388 |
*/ |
| 389 |
ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex ); |
| 390 |
rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri ); |
| 391 |
#ifdef HAVE_TLS |
| 392 |
is_ldaps = ldap_is_ldaps_url( mt->mt_uri ); |
| 393 |
#endif /* HAVE_TLS */ |
| 394 |
ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex ); |
| 395 |
if ( rs->sr_err != LDAP_SUCCESS ) { |
| 396 |
goto error_return; |
| 397 |
} |
| 398 |
|
| 399 |
/* |
| 400 |
* Set LDAP version. This will always succeed: If the client |
| 401 |
* bound with a particular version, then so can we. |
| 402 |
*/ |
| 403 |
if ( mt->mt_version != 0 ) { |
| 404 |
version = mt->mt_version; |
| 405 |
|
| 406 |
} else if ( op->o_conn->c_protocol != 0 ) { |
| 407 |
version = op->o_conn->c_protocol; |
| 408 |
|
| 409 |
} else { |
| 410 |
version = LDAP_VERSION3; |
| 411 |
} |
| 412 |
ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version ); |
| 413 |
ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p ); |
| 414 |
|
| 415 |
/* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */ |
| 416 |
ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS, |
| 417 |
META_BACK_TGT_CHASE_REFERRALS( mt ) ? LDAP_OPT_ON : LDAP_OPT_OFF ); |
| 418 |
|
| 419 |
slap_client_keepalive(msc->msc_ld, &mt->mt_tls.sb_keepalive); |
| 420 |
|
| 421 |
if ( mt->mt_tls.sb_tcp_user_timeout > 0 ) { |
| 422 |
ldap_set_option( msc->msc_ld, LDAP_OPT_TCP_USER_TIMEOUT, |
| 423 |
&mt->mt_tls.sb_tcp_user_timeout ); |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
#ifdef HAVE_TLS |
| 429 |
{ |
| 430 |
slap_bindconf *sb = NULL; |
| 431 |
|
| 432 |
if ( ispriv ) { |
| 433 |
sb = &mt->mt_idassert.si_bc; |
| 434 |
} else { |
| 435 |
sb = &mt->mt_tls; |
| 436 |
} |
| 437 |
|
| 438 |
bindconf_tls_set( sb, msc->msc_ld ); |
| 439 |
|
| 440 |
if ( !is_ldaps ) { |
| 441 |
if ( META_BACK_TGT_USE_TLS( mt ) |
| 442 |
|| ( op->o_conn->c_is_tls && META_BACK_TGT_PROPAGATE_TLS( mt ) ) ) |
| 443 |
{ |
| 444 |
do_start_tls = 1; |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
/* start TLS ("tls [try-]{start|propagate}" statement) */ |
| 450 |
if ( do_start_tls ) { |
| 451 |
#ifdef SLAP_STARTTLS_ASYNCHRONOUS |
| 452 |
/* |
| 453 |
* use asynchronous StartTLS; in case, chase referral |
| 454 |
* FIXME: OpenLDAP does not return referral on StartTLS yet |
| 455 |
*/ |
| 456 |
int msgid; |
| 457 |
|
| 458 |
rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid ); |
| 459 |
if ( rs->sr_err == LDAP_SUCCESS ) { |
| 460 |
LDAPMessage *res = NULL; |
| 461 |
int rc, nretries = mt->mt_nretries; |
| 462 |
struct timeval tv; |
| 463 |
|
| 464 |
LDAP_BACK_TV_SET( &tv ); |
| 465 |
|
| 466 |
retry:; |
| 467 |
rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ); |
| 468 |
switch ( rc ) { |
| 469 |
case -1: |
| 470 |
rs->sr_err = LDAP_UNAVAILABLE; |
| 471 |
rs->sr_text = "Remote server down"; |
| 472 |
break; |
| 473 |
|
| 474 |
case 0: |
| 475 |
if ( nretries != 0 ) { |
| 476 |
if ( nretries > 0 ) { |
| 477 |
nretries--; |
| 478 |
} |
| 479 |
LDAP_BACK_TV_SET( &tv ); |
| 480 |
goto retry; |
| 481 |
} |
| 482 |
rs->sr_err = LDAP_OTHER; |
| 483 |
rs->sr_text = "Timeout, no more retries"; |
| 484 |
break; |
| 485 |
|
| 486 |
default: |
| 487 |
/* only touch when activity actually took place... */ |
| 488 |
if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) { |
| 489 |
msc->msc_time = op->o_time; |
| 490 |
} |
| 491 |
break; |
| 492 |
} |
| 493 |
|
| 494 |
if ( rc == LDAP_RES_EXTENDED ) { |
| 495 |
struct berval *data = NULL; |
| 496 |
|
| 497 |
/* NOTE: right now, data is unused, so don't get it */ |
| 498 |
rs->sr_err = ldap_parse_extended_result( msc->msc_ld, |
| 499 |
res, NULL, NULL /* &data */ , 0 ); |
| 500 |
if ( rs->sr_err == LDAP_SUCCESS ) { |
| 501 |
int err; |
| 502 |
|
| 503 |
/* FIXME: matched? referrals? response controls? */ |
| 504 |
rs->sr_err = ldap_parse_result( msc->msc_ld, |
| 505 |
res, &err, NULL, NULL, NULL, NULL, 1 ); |
| 506 |
res = NULL; |
| 507 |
|
| 508 |
if ( rs->sr_err == LDAP_SUCCESS ) { |
| 509 |
rs->sr_err = err; |
| 510 |
} |
| 511 |
rs->sr_err = slap_map_api2result( rs ); |
| 512 |
|
| 513 |
/* FIXME: in case a referral |
| 514 |
* is returned, should we try |
| 515 |
* using it instead of the |
| 516 |
* configured URI? */ |
| 517 |
if ( rs->sr_err == LDAP_SUCCESS ) { |
| 518 |
rs->sr_err = ldap_install_tls( msc->msc_ld ); |
| 519 |
|
| 520 |
} else if ( rs->sr_err == LDAP_REFERRAL ) { |
| 521 |
/* FIXME: LDAP_OPERATIONS_ERROR? */ |
| 522 |
rs->sr_err = LDAP_OTHER; |
| 523 |
rs->sr_text = "Unwilling to chase referral " |
| 524 |
"returned by Start TLS exop"; |
| 525 |
} |
| 526 |
|
| 527 |
if ( data ) { |
| 528 |
ber_bvfree( data ); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
} else { |
| 533 |
rs->sr_err = LDAP_OTHER; |
| 534 |
rs->sr_text = "Unknown response to StartTLS request :" |
| 535 |
" an ExtendedResponse is expected"; |
| 536 |
} |
| 537 |
|
| 538 |
if ( res != NULL ) { |
| 539 |
ldap_msgfree( res ); |
| 540 |
} |
| 541 |
} |
| 542 |
#else /* ! SLAP_STARTTLS_ASYNCHRONOUS */ |
| 543 |
/* |
| 544 |
* use synchronous StartTLS |
| 545 |
*/ |
| 546 |
rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL ); |
| 547 |
#endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */ |
| 548 |
|
| 549 |
/* if StartTLS is requested, only attempt it if the URL |
| 550 |
* is not "ldaps://"; this may occur not only in case |
| 551 |
* of misconfiguration, but also when used in the chain |
| 552 |
* overlay, where the "uri" can be parsed out of a referral */ |
| 553 |
if ( rs->sr_err == LDAP_SERVER_DOWN |
| 554 |
|| ( rs->sr_err != LDAP_SUCCESS |
| 555 |
&& META_BACK_TGT_TLS_CRITICAL( mt ) ) ) |
| 556 |
{ |
| 557 |
|
| 558 |
#ifdef DEBUG_205 |
| 559 |
Debug( LDAP_DEBUG_ANY, |
| 560 |
"### %s meta_back_init_one_conn(TLS) " |
| 561 |
"ldap_unbind_ext[%d] ld=%p\n", |
| 562 |
op->o_log_prefix, candidate, |
| 563 |
(void *)msc->msc_ld ); |
| 564 |
#endif /* DEBUG_205 */ |
| 565 |
|
| 566 |
/* need to trash a failed Start TLS */ |
| 567 |
meta_clear_one_candidate( op, mc, candidate ); |
| 568 |
goto error_return; |
| 569 |
} |
| 570 |
} |
| 571 |
#endif /* HAVE_TLS */ |
| 572 |
|
| 573 |
/* |
| 574 |
* Set the network timeout if set |
| 575 |
*/ |
| 576 |
if ( mt->mt_network_timeout != 0 ) { |
| 577 |
struct timeval network_timeout; |
| 578 |
|
| 579 |
network_timeout.tv_usec = 0; |
| 580 |
network_timeout.tv_sec = mt->mt_network_timeout; |
| 581 |
|
| 582 |
ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT, |
| 583 |
(void *)&network_timeout ); |
| 584 |
} |
| 585 |
|
| 586 |
/* |
| 587 |
* If the connection DN is not null, an attempt to rewrite it is made |
| 588 |
*/ |
| 589 |
|
| 590 |
if ( ispriv ) { |
| 591 |
if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) { |
| 592 |
ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN ); |
| 593 |
if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) { |
| 594 |
if ( !BER_BVISNULL( &msc->msc_cred ) ) { |
| 595 |
memset( msc->msc_cred.bv_val, 0, |
| 596 |
msc->msc_cred.bv_len ); |
| 597 |
} |
| 598 |
ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd ); |
| 599 |
} |
| 600 |
LDAP_BACK_CONN_ISIDASSERT_SET( msc ); |
| 601 |
|
| 602 |
} else { |
| 603 |
ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv ); |
| 604 |
} |
| 605 |
|
| 606 |
} else { |
| 607 |
if ( !BER_BVISNULL( &msc->msc_cred ) ) { |
| 608 |
memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len ); |
| 609 |
ber_memfree_x( msc->msc_cred.bv_val, NULL ); |
| 610 |
BER_BVZERO( &msc->msc_cred ); |
| 611 |
} |
| 612 |
if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) { |
| 613 |
ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL ); |
| 614 |
BER_BVZERO( &msc->msc_bound_ndn ); |
| 615 |
} |
| 616 |
if ( !BER_BVISEMPTY( &op->o_ndn ) |
| 617 |
&& SLAP_IS_AUTHZ_BACKEND( op ) |
| 618 |
&& isauthz ) |
| 619 |
{ |
| 620 |
dc.target = mt; |
| 621 |
dc.conn = op->o_conn; |
| 622 |
dc.rs = rs; |
| 623 |
dc.ctx = "bindDN"; |
| 624 |
|
| 625 |
/* |
| 626 |
* Rewrite the bind dn if needed |
| 627 |
*/ |
| 628 |
if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn, |
| 629 |
&msc->msc_bound_ndn ) ) |
| 630 |
{ |
| 631 |
|
| 632 |
#ifdef DEBUG_205 |
| 633 |
Debug( LDAP_DEBUG_ANY, |
| 634 |
"### %s meta_back_init_one_conn(rewrite) " |
| 635 |
"ldap_unbind_ext[%d] ld=%p\n", |
| 636 |
op->o_log_prefix, candidate, |
| 637 |
(void *)msc->msc_ld ); |
| 638 |
#endif /* DEBUG_205 */ |
| 639 |
|
| 640 |
/* need to trash a connection not fully established */ |
| 641 |
meta_clear_one_candidate( op, mc, candidate ); |
| 642 |
goto error_return; |
| 643 |
} |
| 644 |
|
| 645 |
/* copy the DN if needed */ |
| 646 |
if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) { |
| 647 |
ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn ); |
| 648 |
} |
| 649 |
|
| 650 |
assert( !BER_BVISNULL( &msc->msc_bound_ndn ) ); |
| 651 |
|
| 652 |
} else { |
| 653 |
ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv ); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
assert( !BER_BVISNULL( &msc->msc_bound_ndn ) ); |
| 658 |
|
| 659 |
error_return:; |
| 660 |
if ( dolock ) { |
| 661 |
ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex ); |
| 662 |
} |
| 663 |
META_BACK_CONN_CREATING_CLEAR( msc ); |
| 664 |
if ( rs->sr_err == LDAP_SUCCESS ) { |
| 665 |
/* |
| 666 |
* Sets a cookie for the rewrite session |
| 667 |
*/ |
| 668 |
( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn ); |
| 669 |
META_BACK_CONN_INITED_SET( msc ); |
| 670 |
} |
| 671 |
if ( dolock ) { |
| 672 |
ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex ); |
| 673 |
} |
| 674 |
|
| 675 |
if ( rs->sr_err != LDAP_SUCCESS ) { |
| 676 |
/* Get the error message and print it in TRACE mode */ |
| 677 |
if ( LogTest( LDAP_DEBUG_TRACE ) ) { |
| 678 |
Log( LDAP_DEBUG_TRACE, ldap_syslog_level, "%s: meta_back_init_one_conn[%d] failed err=%d text=%s\n", |
| 679 |
op->o_log_prefix, candidate, rs->sr_err, rs->sr_text ); |
| 680 |
} |
| 681 |
|
| 682 |
rs->sr_err = slap_map_api2result( rs ); |
| 683 |
if ( sendok & LDAP_BACK_SENDERR ) { |
| 684 |
send_ldap_result( op, rs ); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
return rs->sr_err; |
| 689 |
} |
| 690 |
|
| 691 |
/* |
| 692 |
* meta_back_retry |
| 693 |
* |
| 694 |
* Retries one connection |
| 695 |
*/ |
| 696 |
int |
| 697 |
meta_back_retry( |
| 698 |
Operation *op, |
| 699 |
SlapReply *rs, |
| 700 |
metaconn_t **mcp, |
| 701 |
int candidate, |
| 702 |
ldap_back_send_t sendok, |
| 703 |
SlapReply *candidates ) |
| 704 |
{ |
| 705 |
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private; |
| 706 |
metatarget_t *mt = mi->mi_targets[ candidate ]; |
| 707 |
metaconn_t *mc = *mcp; |
| 708 |
metasingleconn_t *msc = &mc->mc_conns[ candidate ]; |
| 709 |
int rc = LDAP_UNAVAILABLE, |
| 710 |
binding, |
| 711 |
quarantine = 1; |
| 712 |
|
| 713 |
ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex ); |
| 714 |
|
| 715 |
assert( !META_BACK_CONN_CREATING( msc ) ); |
| 716 |
binding = LDAP_BACK_CONN_BINDING( msc ); |
| 717 |
LDAP_BACK_CONN_BINDING_CLEAR( msc ); |
| 718 |
|
| 719 |
assert( mc->mc_refcnt > 0 ); |
| 720 |
if ( mc->mc_refcnt == 1 ) { |
| 721 |
struct berval save_cred; |
| 722 |
|
| 723 |
if ( LogTest( LDAP_DEBUG_ANY ) ) { |
| 724 |
/* this lock is required; however, |
| 725 |
* it's invoked only when logging is on */ |
| 726 |
ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex ); |
| 727 |
Debug(LDAP_DEBUG_ANY, |
| 728 |
"%s meta_back_retry[%d]: retrying URI=\"%s\" DN=\"%s\".\n", |
| 729 |
op->o_log_prefix, candidate, mt->mt_uri, |
| 730 |
BER_BVISNULL(&msc->msc_bound_ndn) ? "" : msc->msc_bound_ndn.bv_val ); |
| 731 |
ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex ); |
| 732 |
} |
| 733 |
|
| 734 |
/* save credentials, if any, for later use; |
| 735 |
* meta_clear_one_candidate() would free them */ |
| 736 |
save_cred = msc->msc_cred; |
| 737 |
BER_BVZERO( &msc->msc_cred ); |
| 738 |
|
| 739 |
meta_clear_one_candidate( op, mc, candidate ); |
| 740 |
LDAP_BACK_CONN_ISBOUND_CLEAR( msc ); |
| 741 |
|
| 742 |
( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn ); |
| 743 |
|
| 744 |
/* mc here must be the regular mc, reset and ready for init */ |
| 745 |
rc = meta_back_init_one_conn( op, rs, mc, candidate, |
| 746 |
LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 ); |
| 747 |
|
| 748 |
/* restore credentials, if any and if needed; |
| 749 |
* meta_back_init_one_conn() restores msc_bound_ndn, if any; |
| 750 |
* if no msc_bound_ndn is restored, destroy credentials */ |
| 751 |
if ( !BER_BVISNULL( &msc->msc_bound_ndn ) |
| 752 |
&& BER_BVISNULL( &msc->msc_cred ) ) |
| 753 |
{ |
| 754 |
msc->msc_cred = save_cred; |
| 755 |
|
| 756 |
} else if ( !BER_BVISNULL( &save_cred ) ) { |
| 757 |
memset( save_cred.bv_val, 0, save_cred.bv_len ); |
| 758 |
ber_memfree_x( save_cred.bv_val, NULL ); |
| 759 |
} |
| 760 |
|
| 761 |
/* restore the "binding" flag, in case */ |
| 762 |
if ( binding ) { |
| 763 |
LDAP_BACK_CONN_BINDING_SET( msc ); |
| 764 |
} |
| 765 |
|
| 766 |
if ( rc == LDAP_SUCCESS ) { |
| 767 |
quarantine = 0; |
| 768 |
LDAP_BACK_CONN_BINDING_SET( msc ); binding = 1; |
| 769 |
rc = meta_back_single_dobind( op, rs, mcp, candidate, |
| 770 |
sendok, mt->mt_nretries, 0 ); |
| 771 |
|
| 772 |
Debug( LDAP_DEBUG_ANY, |
| 773 |
"%s meta_back_retry[%d]: " |
| 774 |
"meta_back_single_dobind=%d\n", |
| 775 |
op->o_log_prefix, candidate, rc ); |
| 776 |
if ( rc == LDAP_SUCCESS ) { |
| 777 |
if ( !BER_BVISNULL( &msc->msc_bound_ndn ) && |
| 778 |
!BER_BVISEMPTY( &msc->msc_bound_ndn ) ) |
| 779 |
{ |
| 780 |
LDAP_BACK_CONN_ISBOUND_SET( msc ); |
| 781 |
|
| 782 |
} else { |
| 783 |
LDAP_BACK_CONN_ISANON_SET( msc ); |
| 784 |
} |
| 785 |
|
| 786 |
/* when bound, dispose of the "binding" flag */ |
| 787 |
if ( binding ) { |
| 788 |
LDAP_BACK_CONN_BINDING_CLEAR( msc ); |
| 789 |
} |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
#if 0 /* ITS#7591, following stmt drops needed result msgs */ |
| 794 |
/* don't send twice */ |
| 795 |
sendok &= ~LDAP_BACK_SENDERR; |
| 796 |
#endif |
| 797 |
} |
| 798 |
|
| 799 |
if ( rc != LDAP_SUCCESS ) { |
| 800 |
SlapReply *candidates = meta_back_candidates_get( op ); |
| 801 |
|
| 802 |
candidates[ candidate ].sr_err = rc; |
| 803 |
|
| 804 |
if ( *mcp != NULL ) { |
| 805 |
if ( mc->mc_refcnt == 1 ) { |
| 806 |
if ( binding ) { |
| 807 |
LDAP_BACK_CONN_BINDING_CLEAR( msc ); |
| 808 |
} |
| 809 |
(void)meta_clear_one_candidate( op, mc, candidate ); |
| 810 |
} |
| 811 |
|
| 812 |
LDAP_BACK_CONN_TAINTED_SET( mc ); |
| 813 |
/* only release if mandatory; otherwise |
| 814 |
* let the caller do what's best before |
| 815 |
* releasing */ |
| 816 |
if ( META_BACK_ONERR_STOP( mi ) ) { |
| 817 |
meta_back_release_conn_lock( mi, mc, 0 ); |
| 818 |
*mcp = NULL; |
| 819 |
|
| 820 |
} else { |
| 821 |
#if META_BACK_PRINT_CONNTREE > 0 |
| 822 |
meta_back_print_conntree( mi, ">>> meta_back_retry" ); |
| 823 |
#endif /* META_BACK_PRINT_CONNTREE */ |
| 824 |
|
| 825 |
/* FIXME: could be done better, reworking meta_back_release_conn_lock() */ |
| 826 |
if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) { |
| 827 |
if ( mc->mc_q.tqe_prev != NULL ) { |
| 828 |
assert( LDAP_BACK_CONN_CACHED( mc ) ); |
| 829 |
assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 ); |
| 830 |
LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, |
| 831 |
mc, mc_q ); |
| 832 |
mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--; |
| 833 |
LDAP_TAILQ_ENTRY_INIT( mc, mc_q ); |
| 834 |
|
| 835 |
} else { |
| 836 |
assert( !LDAP_BACK_CONN_CACHED( mc ) ); |
| 837 |
} |
| 838 |
|
| 839 |
} else { |
| 840 |
/* FIXME: check if in tree, for consistency? */ |
| 841 |
(void)ldap_tavl_delete( &mi->mi_conninfo.lai_tree, |
| 842 |
( caddr_t )mc, meta_back_conndnmc_cmp ); |
| 843 |
} |
| 844 |
LDAP_BACK_CONN_CACHED_CLEAR( mc ); |
| 845 |
|
| 846 |
#if META_BACK_PRINT_CONNTREE > 0 |
| 847 |
meta_back_print_conntree( mi, "<<< meta_back_retry" ); |
| 848 |
#endif /* META_BACK_PRINT_CONNTREE */ |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
if ( sendok & LDAP_BACK_SENDERR ) { |
| 853 |
rs->sr_err = rc; |
| 854 |
rs->sr_text = "Unable to retry"; |
| 855 |
send_ldap_result( op, rs ); |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
if ( quarantine && META_BACK_TGT_QUARANTINE( mt ) ) { |
| 860 |
meta_back_quarantine( op, rs, candidate ); |
| 861 |
} |
| 862 |
|
| 863 |
ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex ); |
| 864 |
|
| 865 |
return rc == LDAP_SUCCESS ? 1 : 0; |
| 866 |
} |
| 867 |
|
| 868 |
/* |
| 869 |
* callback for unique candidate selection |
| 870 |
*/ |
| 871 |
static int |
| 872 |
meta_back_conn_cb( Operation *op, SlapReply *rs ) |
| 873 |
{ |
| 874 |
assert( op->o_tag == LDAP_REQ_SEARCH ); |
| 875 |
|
| 876 |
switch ( rs->sr_type ) { |
| 877 |
case REP_SEARCH: |
| 878 |
((long *)op->o_callback->sc_private)[0] = (long)op->o_private; |
| 879 |
break; |
| 880 |
|
| 881 |
case REP_SEARCHREF: |
| 882 |
case REP_RESULT: |
| 883 |
break; |
| 884 |
|
| 885 |
default: |
| 886 |
return rs->sr_err; |
| 887 |
} |
| 888 |
|
| 889 |
return 0; |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
static int |
| 894 |
meta_back_get_candidate( |
| 895 |
Operation *op, |
| 896 |
SlapReply *rs, |
| 897 |
struct berval *ndn ) |
| 898 |
{ |
| 899 |
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private; |
| 900 |
long candidate; |
| 901 |
|
| 902 |
/* |
| 903 |
* tries to get a unique candidate |
| 904 |
* (takes care of default target) |
| 905 |
*/ |
| 906 |
candidate = meta_back_select_unique_candidate( mi, ndn ); |
| 907 |
|
| 908 |
/* |
| 909 |
* if any is found, inits the connection |
| 910 |
*/ |
| 911 |
if ( candidate == META_TARGET_NONE ) { |
| 912 |
rs->sr_err = LDAP_NO_SUCH_OBJECT; |
| 913 |
rs->sr_text = "No suitable candidate target found"; |
| 914 |
|
| 915 |
} else if ( candidate == META_TARGET_MULTIPLE ) { |
| 916 |
Operation op2 = *op; |
| 917 |
SlapReply rs2 = { REP_RESULT }; |
| 918 |
slap_callback cb2 = { 0 }; |
| 919 |
int rc; |
| 920 |
|
| 921 |
/* try to get a unique match for the request ndn |
| 922 |
* among the multiple candidates available */ |
| 923 |
op2.o_tag = LDAP_REQ_SEARCH; |
| 924 |
op2.o_req_dn = *ndn; |
| 925 |
op2.o_req_ndn = *ndn; |
| 926 |
op2.ors_scope = LDAP_SCOPE_BASE; |
| 927 |
op2.ors_deref = LDAP_DEREF_NEVER; |
| 928 |
op2.ors_attrs = slap_anlist_no_attrs; |
| 929 |
op2.ors_attrsonly = 0; |
| 930 |
op2.ors_limit = NULL; |
| 931 |
op2.ors_slimit = 1; |
| 932 |
op2.ors_tlimit = SLAP_NO_LIMIT; |
| 933 |
|
| 934 |
op2.ors_filter = (Filter *)slap_filter_objectClass_pres; |
| 935 |
op2.ors_filterstr = *slap_filterstr_objectClass_pres; |
| 936 |
|
| 937 |
op2.o_callback = &cb2; |
| 938 |
cb2.sc_response = meta_back_conn_cb; |
| 939 |
cb2.sc_private = (void *)&candidate; |
| 940 |
|
| 941 |
rc = op->o_bd->be_search( &op2, &rs2 ); |
| 942 |
|
| 943 |
switch ( rs2.sr_err ) { |
| 944 |
case LDAP_SUCCESS: |
| 945 |
default: |
| 946 |
rs->sr_err = rs2.sr_err; |
| 947 |
break; |
| 948 |
|
| 949 |
case LDAP_SIZELIMIT_EXCEEDED: |
| 950 |
/* if multiple candidates can serve the operation, |
| 951 |
* and a default target is defined, and it is |
| 952 |
* a candidate, try using it (FIXME: YMMV) */ |
| 953 |
if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE |
| 954 |
&& meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ], |
| 955 |
ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) ) |
| 956 |
{ |
| 957 |
candidate = mi->mi_defaulttarget; |
| 958 |
rs->sr_err = LDAP_SUCCESS; |
| 959 |
rs->sr_text = NULL; |
| 960 |
|
| 961 |
} else { |
| 962 |
rs->sr_err = LDAP_UNWILLING_TO_PERFORM; |
| 963 |
rs->sr_text = "Unable to select unique candidate target"; |
| 964 |
} |
| 965 |
break; |
| 966 |
} |
| 967 |
|
| 968 |
} else { |
| 969 |
rs->sr_err = LDAP_SUCCESS; |
| 970 |
} |
| 971 |
|
| 972 |
return candidate; |
| 973 |
} |
| 974 |
|
| 975 |
SlapReply * |
| 976 |
meta_back_candidates_get( Operation *op ) |
| 977 |
{ |
| 978 |
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private; |
| 979 |
SlapReply *candidates; |
| 980 |
|
| 981 |
candidates = op->o_tmpcalloc( mi->mi_ntargets, sizeof( SlapReply ), op->o_tmpmemctx ); |
| 982 |
return candidates; |
| 983 |
} |
| 984 |
|
| 985 |
/* |
| 986 |
* meta_back_getconn |
| 987 |
* |
| 988 |
* Prepares the connection structure |
| 989 |
* |
| 990 |
* RATIONALE: |
| 991 |
* |
| 992 |
* - determine what DN is being requested: |
| 993 |
* |
| 994 |
* op requires candidate checks |
| 995 |
* |
| 996 |
* add unique parent of o_req_ndn |
| 997 |
* bind unique^*[/all] o_req_ndn [no check] |
| 998 |
* compare unique^+ o_req_ndn |
| 999 |
* delete unique o_req_ndn |
| 1000 |
* modify unique o_req_ndn |
| 1001 |
* search any o_req_ndn |
| 1002 |
* modrdn unique[, unique] o_req_ndn[, orr_nnewSup] |
| 1003 |
* |
| 1004 |
* - for ops that require the candidate to be unique, in case of multiple |
| 1005 |
* occurrences an internal search with sizeLimit=1 is performed |
| 1006 |
* if a unique candidate can actually be determined. If none is found, |
| 1007 |
* the operation aborts; if multiple are found, the default target |
| 1008 |
* is used if defined and candidate; otherwise the operation aborts. |
| 1009 |
* |
| 1010 |
* *^note: actually, the bind operation is handled much like a search; |
| 1011 |
* i.e. the bind is broadcast to all candidate targets. |
| 1012 |
* |
| 1013 |
* +^note: actually, the compare operation is handled much like a search; |
| 1014 |
* i.e. the compare is broadcast to all candidate targets, while checking |
| 1015 |
* that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is |
| 1016 |
* returned. |
| 1017 |
*/ |
| 1018 |
metaconn_t * |
| 1019 |
meta_back_getconn( |
| 1020 |
Operation *op, |
| 1021 |
SlapReply *rs, |
| 1022 |
int *candidate, |
| 1023 |
ldap_back_send_t sendok, |
| 1024 |
SlapReply *candidates ) |
| 1025 |
{ |
| 1026 |
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private; |
| 1027 |
metaconn_t *mc = NULL, |
| 1028 |
mc_curr = {{ 0 }}; |
| 1029 |
int cached = META_TARGET_NONE, |
| 1030 |
i = META_TARGET_NONE, |
| 1031 |
err = LDAP_SUCCESS, |
| 1032 |
new_conn = 0, |
| 1033 |
ncandidates = 0; |
| 1034 |
|
| 1035 |
|
| 1036 |
meta_op_type op_type = META_OP_REQUIRE_SINGLE; |
| 1037 |
enum { |
| 1038 |
META_DNTYPE_ENTRY, |
| 1039 |
META_DNTYPE_PARENT, |
| 1040 |
META_DNTYPE_NEWPARENT |
| 1041 |
} dn_type = META_DNTYPE_ENTRY; |
| 1042 |
struct berval ndn = op->o_req_ndn, |
| 1043 |
pndn; |
| 1044 |
|
| 1045 |
/* Internal searches are privileged and shared. So is root. */ |
| 1046 |
if ( ( !BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ALWAYS( mi ) ) |
| 1047 |
|| ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ANON( mi ) ) |
| 1048 |
|| op->o_do_not_cache || be_isroot( op ) ) |
| 1049 |
{ |
| 1050 |
LDAP_BACK_CONN_ISPRIV_SET( &mc_curr ); |
| 1051 |
mc_curr.mc_local_ndn = op->o_bd->be_rootndn; |
| 1052 |
LDAP_BACK_PCONN_ROOTDN_SET( &mc_curr, op ); |
| 1053 |
|
| 1054 |
} else if ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_NOANON( mi ) ) |
| 1055 |
{ |
| 1056 |
LDAP_BACK_CONN_ISANON_SET( &mc_curr ); |
| 1057 |
BER_BVSTR( &mc_curr.mc_local_ndn, "" ); |
| 1058 |
LDAP_BACK_PCONN_ANON_SET( &mc_curr, op ); |
| 1059 |
|
| 1060 |
} else { |
| 1061 |
mc_curr.mc_local_ndn = op->o_ndn; |
| 1062 |
|
| 1063 |
/* Explicit binds must not be shared */ |
| 1064 |
if ( !BER_BVISEMPTY( &op->o_ndn ) |
| 1065 |
|| op->o_tag == LDAP_REQ_BIND |
| 1066 |
|| SLAP_IS_AUTHZ_BACKEND( op ) ) |
| 1067 |
{ |
| 1068 |
mc_curr.mc_conn = op->o_conn; |
| 1069 |
|
| 1070 |
} else { |
| 1071 |
LDAP_BACK_CONN_ISANON_SET( &mc_curr ); |
| 1072 |
LDAP_BACK_PCONN_ANON_SET( &mc_curr, op ); |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
/* Explicit Bind requests always get their own conn */ |
| 1077 |
if ( sendok & LDAP_BACK_BINDING ) { |
| 1078 |
mc_curr.mc_conn = op->o_conn; |
| 1079 |
|
| 1080 |
} else { |
| 1081 |
/* Searches for a metaconn in the avl tree */ |
| 1082 |
retry_lock:; |
| 1083 |
ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex ); |
| 1084 |
if ( LDAP_BACK_PCONN_ISPRIV( &mc_curr ) ) { |
| 1085 |
/* lookup a conn that's not binding */ |
| 1086 |
LDAP_TAILQ_FOREACH( mc, |
| 1087 |
&mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv, |
| 1088 |
mc_q ) |
| 1089 |
{ |
| 1090 |
if ( !LDAP_BACK_CONN_BINDING( mc ) && mc->mc_refcnt == 0 ) { |
| 1091 |
break; |
| 1092 |
} |
| 1093 |
} |
| 1094 |
|
| 1095 |
if ( mc != NULL ) { |
| 1096 |
/* move to tail of queue */ |
| 1097 |
if ( mc != LDAP_TAILQ_LAST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, |
| 1098 |
mc_conn_priv_q ) ) |
| 1099 |
{ |
| 1100 |
LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, |
| 1101 |
mc, mc_q ); |
| 1102 |
LDAP_TAILQ_ENTRY_INIT( mc, mc_q ); |
| 1103 |
LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, |
| 1104 |
mc, mc_q ); |
| 1105 |
} |
| 1106 |
|
| 1107 |
} else if ( !LDAP_BACK_USE_TEMPORARIES( mi ) |
| 1108 |
&& mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_num == mi->mi_conn_priv_max ) |
| 1109 |
{ |
| 1110 |
mc = LDAP_TAILQ_FIRST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv ); |
| 1111 |
} |
| 1112 |
|
| 1113 |
|
| 1114 |
} else { |
| 1115 |
mc = (metaconn_t *)ldap_tavl_find( mi->mi_conninfo.lai_tree, |
| 1116 |
(caddr_t)&mc_curr, meta_back_conndn_cmp ); |
| 1117 |
} |
| 1118 |
|
| 1119 |
if ( mc ) { |
| 1120 |
/* catch taint errors */ |
| 1121 |
assert( !LDAP_BACK_CONN_TAINTED( mc ) ); |
| 1122 |
|
| 1123 |
/* Don't reuse connections while they're still binding |
| 1124 |
* NOTE: only makes sense for binds */ |
| 1125 |
if ( LDAP_BACK_CONN_BINDING( mc ) ) { |
| 1126 |
if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) { |
| 1127 |
ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex ); |
| 1128 |
|
| 1129 |
ldap_pvt_thread_yield(); |
| 1130 |
goto retry_lock; |
| 1131 |
} |
| 1132 |
|
| 1133 |
/* release conn, and create a temporary */ |
| 1134 |
mc = NULL; |
| 1135 |
|
| 1136 |
} else { |
| 1137 |
if ( mc->mc_refcnt == 0 && (( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl ) |
| 1138 |
|| ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout )) ) |
| 1139 |
{ |
| 1140 |
#if META_BACK_PRINT_CONNTREE > 0 |
| 1141 |
meta_back_print_conntree( mi, |
| 1142 |
">>> meta_back_getconn(expired)" ); |
| 1143 |
#endif /* META_BACK_PRINT_CONNTREE */ |
| 1144 |
|
| 1145 |
/* don't let anyone else use this expired connection */ |
| 1146 |
if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) { |
| 1147 |
if ( mc->mc_q.tqe_prev != NULL ) { |
| 1148 |
assert( LDAP_BACK_CONN_CACHED( mc ) ); |
| 1149 |
assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 ); |
| 1150 |
LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, |
| 1151 |
mc, mc_q ); |
| 1152 |
mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--; |
| 1153 |
LDAP_TAILQ_ENTRY_INIT( mc, mc_q ); |
| 1154 |
|
| 1155 |
} else { |
| 1156 |
assert( !LDAP_BACK_CONN_CACHED( mc ) ); |
| 1157 |
} |
| 1158 |
|
| 1159 |
} else { |
| 1160 |
(void)ldap_tavl_delete( &mi->mi_conninfo.lai_tree, |
| 1161 |
(caddr_t)mc, meta_back_conndnmc_cmp ); |
| 1162 |
} |
| 1163 |
|
| 1164 |
#if META_BACK_PRINT_CONNTREE > 0 |
| 1165 |
meta_back_print_conntree( mi, |
| 1166 |
"<<< meta_back_getconn(expired)" ); |
| 1167 |
#endif /* META_BACK_PRINT_CONNTREE */ |
| 1168 |
LDAP_BACK_CONN_TAINTED_SET( mc ); |
| 1169 |
LDAP_BACK_CONN_CACHED_CLEAR( mc ); |
| 1170 |
|
| 1171 |
if ( LogTest( LDAP_DEBUG_TRACE ) ) { |
| 1172 |
char buf[STRLENOF("4294967295U") + 1] = { 0 }; |
| 1173 |
mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) ); |
| 1174 |
|
| 1175 |
Debug( LDAP_DEBUG_TRACE, |
| 1176 |
"%s meta_back_getconn: mc=%p conn=%s expired (tainted).\n", |
| 1177 |
op->o_log_prefix, (void *)mc, buf ); |
| 1178 |
} |
| 1179 |
} |
| 1180 |
|
| 1181 |
mc->mc_refcnt++; |
| 1182 |
} |
| 1183 |
} |
| 1184 |
ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex ); |
| 1185 |
} |
| 1186 |
|
| 1187 |
switch ( op->o_tag ) { |
| 1188 |
case LDAP_REQ_ADD: |
| 1189 |
/* if we go to selection, the entry must not exist, |
| 1190 |
* and we must be able to resolve the parent */ |
| 1191 |
dn_type = META_DNTYPE_PARENT; |
| 1192 |
dnParent( &ndn, &pndn ); |
| 1193 |
break; |
| 1194 |
|
| 1195 |
case LDAP_REQ_MODRDN: |
| 1196 |
/* if nnewSuperior is not NULL, it must resolve |
| 1197 |
* to the same candidate as the req_ndn */ |
| 1198 |
if ( op->orr_nnewSup ) { |
| 1199 |
dn_type = META_DNTYPE_NEWPARENT; |
| 1200 |
} |
| 1201 |
break; |
| 1202 |
|
| 1203 |
case LDAP_REQ_BIND: |
| 1204 |
/* if bound as rootdn, the backend must bind to all targets |
| 1205 |
* with the administrative identity |
| 1206 |
* (unless pseoudoroot-bind-defer is TRUE) */ |
| 1207 |
if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) { |
| 1208 |
op_type = META_OP_REQUIRE_ALL; |
| 1209 |
} |
| 1210 |
break; |
| 1211 |
|
| 1212 |
case LDAP_REQ_COMPARE: |
| 1213 |
case LDAP_REQ_DELETE: |
| 1214 |
case LDAP_REQ_MODIFY: |
| 1215 |
/* just a unique candidate */ |
| 1216 |
break; |
| 1217 |
|
| 1218 |
case LDAP_REQ_SEARCH: |
| 1219 |
/* allow multiple candidates for the searchBase */ |
| 1220 |
op_type = META_OP_ALLOW_MULTIPLE; |
| 1221 |
break; |
| 1222 |
|
| 1223 |
default: |
| 1224 |
/* right now, just break (exop?) */ |
| 1225 |
break; |
| 1226 |
} |
| 1227 |
|
| 1228 |
/* |
| 1229 |
* require all connections ... |
| 1230 |
*/ |
| 1231 |
if ( op_type == META_OP_REQUIRE_ALL ) { |
| 1232 |
|
| 1233 |
/* Looks like we didn't get a bind. Open a new session... */ |
| 1234 |
if ( mc == NULL ) { |
| 1235 |
assert( new_conn == 0 ); |
| 1236 |
mc = metaconn_alloc( op ); |
| 1237 |
mc->mc_conn = mc_curr.mc_conn; |
| 1238 |
ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn ); |
| 1239 |
new_conn = 1; |
| 1240 |
if ( sendok & LDAP_BACK_BINDING ) { |
| 1241 |
LDAP_BACK_CONN_BINDING_SET( mc ); |
| 1242 |
} |
| 1243 |
if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) { |
| 1244 |
LDAP_BACK_CONN_ISPRIV_SET( mc ); |
| 1245 |
|
| 1246 |
} else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) { |
| 1247 |
LDAP_BACK_CONN_ISANON_SET( mc ); |
| 1248 |
} |
| 1249 |
|
| 1250 |
} else if ( 0 ) { |
| 1251 |
/* TODO: if any of the connections is binding, |
| 1252 |
* release mc and create a new one */ |
| 1253 |
} |
| 1254 |
|
| 1255 |
for ( i = 0; i < mi->mi_ntargets; i++ ) { |
| 1256 |
/* |
| 1257 |
* The target is activated; if needed, it is |
| 1258 |
* also init'd |
| 1259 |
*/ |
| 1260 |
candidates[ i ].sr_err = meta_back_init_one_conn( op, |
| 1261 |
rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ), |
| 1262 |
LDAP_BACK_DONTSEND, !new_conn ); |
| 1263 |
if ( candidates[ i ].sr_err == LDAP_SUCCESS ) { |
| 1264 |
if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) { |
| 1265 |
LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] ); |
| 1266 |
} |
| 1267 |
META_CANDIDATE_SET( &candidates[ i ] ); |
| 1268 |
ncandidates++; |
| 1269 |
|
| 1270 |
} else { |
| 1271 |
|
| 1272 |
/* |
| 1273 |
* FIXME: in case one target cannot |
| 1274 |
* be init'd, should the other ones |
| 1275 |
* be tried? |
| 1276 |
*/ |
| 1277 |
META_CANDIDATE_RESET( &candidates[ i ] ); |
| 1278 |
err = candidates[ i ].sr_err; |
| 1279 |
continue; |
| 1280 |
} |
| 1281 |
} |
| 1282 |
|
| 1283 |
if ( ncandidates == 0 ) { |
| 1284 |
if ( new_conn ) { |
| 1285 |
mc->mc_refcnt = 0; |
| 1286 |
meta_back_conn_free( mc ); |
| 1287 |
|
| 1288 |
} else { |
| 1289 |
meta_back_release_conn( mi, mc ); |
| 1290 |
} |
| 1291 |
|
| 1292 |
rs->sr_err = LDAP_NO_SUCH_OBJECT; |
| 1293 |
rs->sr_text = "Unable to select valid candidates"; |
| 1294 |
|
| 1295 |
if ( sendok & LDAP_BACK_SENDERR ) { |
| 1296 |
if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) { |
| 1297 |
rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val; |
| 1298 |
} |
| 1299 |
send_ldap_result( op, rs ); |
| 1300 |
rs->sr_matched = NULL; |
| 1301 |
} |
| 1302 |
|
| 1303 |
return NULL; |
| 1304 |
} |
| 1305 |
|
| 1306 |
goto done; |
| 1307 |
} |
| 1308 |
|
| 1309 |
/* |
| 1310 |
* looks in cache, if any |
| 1311 |
*/ |
| 1312 |
if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) { |
| 1313 |
cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn ); |
| 1314 |
} |
| 1315 |
|
| 1316 |
if ( op_type == META_OP_REQUIRE_SINGLE ) { |
| 1317 |
metatarget_t *mt = NULL; |
| 1318 |
metasingleconn_t *msc = NULL; |
| 1319 |
|
| 1320 |
int j; |
| 1321 |
|
| 1322 |
for ( j = 0; j < mi->mi_ntargets; j++ ) { |
| 1323 |
META_CANDIDATE_RESET( &candidates[ j ] ); |
| 1324 |
} |
| 1325 |
|
| 1326 |
/* |
| 1327 |
* tries to get a unique candidate |
| 1328 |
* (takes care of default target) |
| 1329 |
*/ |
| 1330 |
if ( i == META_TARGET_NONE ) { |
| 1331 |
i = meta_back_get_candidate( op, rs, &ndn ); |
| 1332 |
|
| 1333 |
if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) { |
| 1334 |
i = meta_back_get_candidate( op, rs, &pndn ); |
| 1335 |
} |
| 1336 |
|
| 1337 |
if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) { |
| 1338 |
if ( mc != NULL ) { |
| 1339 |
meta_back_release_conn( mi, mc ); |
| 1340 |
} |
| 1341 |
|
| 1342 |
if ( sendok & LDAP_BACK_SENDERR ) { |
| 1343 |
if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) { |
| 1344 |
rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val; |
| 1345 |
} |
| 1346 |
send_ldap_result( op, rs ); |
| 1347 |
rs->sr_matched = NULL; |
| 1348 |
} |
| 1349 |
|
| 1350 |
return NULL; |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i ) |
| 1355 |
{ |
| 1356 |
if ( mc != NULL ) { |
| 1357 |
meta_back_release_conn( mi, mc ); |
| 1358 |
} |
| 1359 |
|
| 1360 |
rs->sr_err = LDAP_UNWILLING_TO_PERFORM; |
| 1361 |
rs->sr_text = "Cross-target rename not supported"; |
| 1362 |
if ( sendok & LDAP_BACK_SENDERR ) { |
| 1363 |
send_ldap_result( op, rs ); |
| 1364 |
} |
| 1365 |
|
| 1366 |
return NULL; |
| 1367 |
} |
| 1368 |
|
| 1369 |
Debug( LDAP_DEBUG_TRACE, |
| 1370 |
"==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n", |
| 1371 |
i, op->o_req_ndn.bv_val ); |
| 1372 |
|
| 1373 |
if ( mc == NULL ) { |
| 1374 |
/* Retries searching for a metaconn in the avl tree |
| 1375 |
* the reason is that the connection might have been |
| 1376 |
* created by meta_back_get_candidate() */ |
| 1377 |
if ( !( sendok & LDAP_BACK_BINDING ) ) { |
| 1378 |
retry_lock2:; |
| 1379 |
ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex ); |
| 1380 |
mc = (metaconn_t *)ldap_tavl_find( mi->mi_conninfo.lai_tree, |
| 1381 |
(caddr_t)&mc_curr, meta_back_conndn_cmp ); |
| 1382 |
if ( mc != NULL ) { |
| 1383 |
/* catch taint errors */ |
| 1384 |
assert( !LDAP_BACK_CONN_TAINTED( mc ) ); |
| 1385 |
|
| 1386 |
/* Don't reuse connections while they're still binding */ |
| 1387 |
if ( META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) |
| 1388 |
|| LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) |
| 1389 |
{ |
| 1390 |
if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) { |
| 1391 |
ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex ); |
| 1392 |
ldap_pvt_thread_yield(); |
| 1393 |
goto retry_lock2; |
| 1394 |
} |
| 1395 |
|
| 1396 |
mc = NULL; |
| 1397 |
|
| 1398 |
} else { |
| 1399 |
mc->mc_refcnt++; |
| 1400 |
} |
| 1401 |
} |
| 1402 |
ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex ); |
| 1403 |
} |
| 1404 |
|
| 1405 |
/* Looks like we didn't get a bind. Open a new session... */ |
| 1406 |
if ( mc == NULL ) { |
| 1407 |
assert( new_conn == 0 ); |
| 1408 |
mc = metaconn_alloc( op ); |
| 1409 |
mc->mc_conn = mc_curr.mc_conn; |
| 1410 |
ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn ); |
| 1411 |
new_conn = 1; |
| 1412 |
if ( sendok & LDAP_BACK_BINDING ) { |
| 1413 |
LDAP_BACK_CONN_BINDING_SET( mc ); |
| 1414 |
} |
| 1415 |
if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) { |
| 1416 |
LDAP_BACK_CONN_ISPRIV_SET( mc ); |
| 1417 |
|
| 1418 |
} else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) { |
| 1419 |
LDAP_BACK_CONN_ISANON_SET( mc ); |
| 1420 |
} |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
/* |
| 1425 |
* Clear all other candidates |
| 1426 |
*/ |
| 1427 |
( void )meta_clear_unused_candidates( op, i, candidates ); |
| 1428 |
|
| 1429 |
mt = mi->mi_targets[ i ]; |
| 1430 |
msc = &mc->mc_conns[ i ]; |
| 1431 |
|
| 1432 |
/* |
| 1433 |
* The target is activated; if needed, it is |
| 1434 |
* also init'd. In case of error, meta_back_init_one_conn |
| 1435 |
* sends the appropriate result. |
| 1436 |
*/ |
| 1437 |
err = meta_back_init_one_conn( op, rs, mc, i, |
| 1438 |
LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn ); |
| 1439 |
if ( err != LDAP_SUCCESS ) { |
| 1440 |
/* |
| 1441 |
* FIXME: in case one target cannot |
| 1442 |
* be init'd, should the other ones |
| 1443 |
* be tried? |
| 1444 |
*/ |
| 1445 |
META_CANDIDATE_RESET( &candidates[ i ] ); |
| 1446 |
if ( new_conn ) { |
| 1447 |
mc->mc_refcnt = 0; |
| 1448 |
meta_back_conn_free( mc ); |
| 1449 |
|
| 1450 |
} else { |
| 1451 |
meta_back_release_conn( mi, mc ); |
| 1452 |
} |
| 1453 |
return NULL; |
| 1454 |
} |
| 1455 |
|
| 1456 |
if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) { |
| 1457 |
LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] ); |
| 1458 |
} |
| 1459 |
|
| 1460 |
candidates[ i ].sr_err = LDAP_SUCCESS; |
| 1461 |
META_CANDIDATE_SET( &candidates[ i ] ); |
| 1462 |
ncandidates++; |
| 1463 |
|
| 1464 |
if ( candidate ) { |
| 1465 |
*candidate = i; |
| 1466 |
} |
| 1467 |
|
| 1468 |
/* |
| 1469 |
* if no unique candidate ... |
| 1470 |
*/ |
| 1471 |
} else { |
| 1472 |
|
| 1473 |
/* Looks like we didn't get a bind. Open a new session... */ |
| 1474 |
if ( mc == NULL ) { |
| 1475 |
assert( new_conn == 0 ); |
| 1476 |
mc = metaconn_alloc( op ); |
| 1477 |
mc->mc_conn = mc_curr.mc_conn; |
| 1478 |
ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn ); |
| 1479 |
new_conn = 1; |
| 1480 |
if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) { |
| 1481 |
LDAP_BACK_CONN_ISPRIV_SET( mc ); |
| 1482 |
|
| 1483 |
} else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) { |
| 1484 |
LDAP_BACK_CONN_ISANON_SET( mc ); |
| 1485 |
} |
| 1486 |
} |
| 1487 |
|
| 1488 |
for ( i = 0; i < mi->mi_ntargets; i++ ) { |
| 1489 |
metatarget_t *mt = mi->mi_targets[ i ]; |
| 1490 |
|
| 1491 |
META_CANDIDATE_RESET( &candidates[ i ] ); |
| 1492 |
|
| 1493 |
if ( i == cached |
| 1494 |
|| meta_back_is_candidate( mt, &op->o_req_ndn, |
| 1495 |
op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_SUBTREE ) ) |
| 1496 |
{ |
| 1497 |
|
| 1498 |
/* |
| 1499 |
* The target is activated; if needed, it is |
| 1500 |
* also init'd |
| 1501 |
*/ |
| 1502 |
int lerr = meta_back_init_one_conn( op, rs, mc, i, |
| 1503 |
LDAP_BACK_CONN_ISPRIV( &mc_curr ), |
| 1504 |
LDAP_BACK_DONTSEND, !new_conn ); |
| 1505 |
candidates[ i ].sr_err = lerr; |
| 1506 |
if ( lerr == LDAP_SUCCESS ) { |
| 1507 |
META_CANDIDATE_SET( &candidates[ i ] ); |
| 1508 |
ncandidates++; |
| 1509 |
|
| 1510 |
Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n", |
| 1511 |
op->o_log_prefix, i ); |
| 1512 |
|
| 1513 |
} else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) { |
| 1514 |
META_CANDIDATE_SET( &candidates[ i ] ); |
| 1515 |
|
| 1516 |
Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n", |
| 1517 |
op->o_log_prefix, i, |
| 1518 |
mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" ); |
| 1519 |
|
| 1520 |
} else { |
| 1521 |
|
| 1522 |
/* |
| 1523 |
* FIXME: in case one target cannot |
| 1524 |
* be init'd, should the other ones |
| 1525 |
* be tried? |
| 1526 |
*/ |
| 1527 |
if ( new_conn ) { |
| 1528 |
( void )meta_clear_one_candidate( op, mc, i ); |
| 1529 |
} |
| 1530 |
/* leave the target candidate, but record the error for later use */ |
| 1531 |
err = lerr; |
| 1532 |
|
| 1533 |
if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) { |
| 1534 |
Log( LDAP_DEBUG_TRACE, ldap_syslog_level, "%s: meta_back_getconn[%d] quarantined err=%d text=%s\n", |
| 1535 |
op->o_log_prefix, i, lerr, rs->sr_text ); |
| 1536 |
|
| 1537 |
} else { |
| 1538 |
Log( LDAP_DEBUG_ANY, ldap_syslog, "%s: meta_back_getconn[%d] failed err=%d text=%s\n", |
| 1539 |
op->o_log_prefix, i, lerr, rs->sr_text ); |
| 1540 |
} |
| 1541 |
|
| 1542 |
if ( META_BACK_ONERR_STOP( mi ) ) { |
| 1543 |
if ( sendok & LDAP_BACK_SENDERR ) { |
| 1544 |
send_ldap_result( op, rs ); |
| 1545 |
} |
| 1546 |
if ( new_conn ) { |
| 1547 |
mc->mc_refcnt = 0; |
| 1548 |
meta_back_conn_free( mc ); |
| 1549 |
|
| 1550 |
} else { |
| 1551 |
meta_back_release_conn( mi, mc ); |
| 1552 |
} |
| 1553 |
|
| 1554 |
return NULL; |
| 1555 |
} |
| 1556 |
|
| 1557 |
continue; |
| 1558 |
} |
| 1559 |
|
| 1560 |
} else { |
| 1561 |
if ( new_conn ) { |
| 1562 |
( void )meta_clear_one_candidate( op, mc, i ); |
| 1563 |
} |
| 1564 |
} |
| 1565 |
} |
| 1566 |
|
| 1567 |
if ( ncandidates == 0 ) { |
| 1568 |
if ( new_conn ) { |
| 1569 |
mc->mc_refcnt = 0; |
| 1570 |
meta_back_conn_free( mc ); |
| 1571 |
|
| 1572 |
} else { |
| 1573 |
meta_back_release_conn( mi, mc ); |
| 1574 |
} |
| 1575 |
|
| 1576 |
if ( rs->sr_err == LDAP_SUCCESS ) { |
| 1577 |
rs->sr_err = LDAP_NO_SUCH_OBJECT; |
| 1578 |
rs->sr_text = "Unable to select valid candidates"; |
| 1579 |
} |
| 1580 |
|
| 1581 |
if ( sendok & LDAP_BACK_SENDERR ) { |
| 1582 |
if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) { |
| 1583 |
rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val; |
| 1584 |
} |
| 1585 |
send_ldap_result( op, rs ); |
| 1586 |
rs->sr_matched = NULL; |
| 1587 |
} |
| 1588 |
|
| 1589 |
return NULL; |
| 1590 |
} |
| 1591 |
} |
| 1592 |
|
| 1593 |
done:; |
| 1594 |
/* clear out meta_back_init_one_conn non-fatal errors */ |
| 1595 |
rs->sr_err = LDAP_SUCCESS; |
| 1596 |
rs->sr_text = NULL; |
| 1597 |
|
| 1598 |
/* touch the timestamp */ |
| 1599 |
if ( mi->mi_idle_timeout != 0 ) { |
| 1600 |
mc->mc_time = op->o_time; |
| 1601 |
} |
| 1602 |
|
| 1603 |
if ( new_conn ) { |
| 1604 |
if ( mi->mi_conn_ttl ) { |
| 1605 |
mc->mc_create_time = op->o_time; |
| 1606 |
} |
| 1607 |
|
| 1608 |
/* |
| 1609 |
* Inserts the newly created metaconn in the avl tree |
| 1610 |
*/ |
| 1611 |
ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex ); |
| 1612 |
#if META_BACK_PRINT_CONNTREE > 0 |
| 1613 |
meta_back_print_conntree( mi, ">>> meta_back_getconn" ); |
| 1614 |
#endif /* META_BACK_PRINT_CONNTREE */ |
| 1615 |
|
| 1616 |
err = 0; |
| 1617 |
if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) { |
| 1618 |
if ( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num < mi->mi_conn_priv_max ) { |
| 1619 |
LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q ); |
| 1620 |
mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num++; |
| 1621 |
LDAP_BACK_CONN_CACHED_SET( mc ); |
| 1622 |
|
| 1623 |
} else { |
| 1624 |
LDAP_BACK_CONN_TAINTED_SET( mc ); |
| 1625 |
} |
| 1626 |
rs->sr_err = 0; |
| 1627 |
|
| 1628 |
} else if ( !( sendok & LDAP_BACK_BINDING ) ) { |
| 1629 |
err = ldap_tavl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc, |
| 1630 |
meta_back_conndn_cmp, meta_back_conndn_dup ); |
| 1631 |
LDAP_BACK_CONN_CACHED_SET( mc ); |
| 1632 |
} |
| 1633 |
|
| 1634 |
#if META_BACK_PRINT_CONNTREE > 0 |
| 1635 |
meta_back_print_conntree( mi, "<<< meta_back_getconn" ); |
| 1636 |
#endif /* META_BACK_PRINT_CONNTREE */ |
| 1637 |
ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex ); |
| 1638 |
|
| 1639 |
if ( !LDAP_BACK_PCONN_ISPRIV( mc ) ) { |
| 1640 |
/* |
| 1641 |
* Err could be -1 in case a duplicate metaconn is inserted |
| 1642 |
*/ |
| 1643 |
switch ( err ) { |
| 1644 |
case 0: |
| 1645 |
break; |
| 1646 |
|
| 1647 |
case -1: |
| 1648 |
LDAP_BACK_CONN_CACHED_CLEAR( mc ); |
| 1649 |
/* duplicate: free and try to get the newly created one */ |
| 1650 |
if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( mi ) ) { |
| 1651 |
mc->mc_refcnt = 0; |
| 1652 |
meta_back_conn_free( mc ); |
| 1653 |
|
| 1654 |
new_conn = 0; |
| 1655 |
goto retry_lock; |
| 1656 |
} |
| 1657 |
|
| 1658 |
LDAP_BACK_CONN_TAINTED_SET( mc ); |
| 1659 |
break; |
| 1660 |
|
| 1661 |
default: |
| 1662 |
LDAP_BACK_CONN_CACHED_CLEAR( mc ); |
| 1663 |
if ( LogTest( LDAP_DEBUG_ANY ) ) { |
| 1664 |
char buf[STRLENOF("4294967295U") + 1] = { 0 }; |
| 1665 |
mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) ); |
| 1666 |
|
| 1667 |
Debug( LDAP_DEBUG_ANY, |
| 1668 |
"%s meta_back_getconn: candidates=%d conn=%s insert failed\n", |
| 1669 |
op->o_log_prefix, ncandidates, buf ); |
| 1670 |
} |
| 1671 |
|
| 1672 |
mc->mc_refcnt = 0; |
| 1673 |
meta_back_conn_free( mc ); |
| 1674 |
|
| 1675 |
rs->sr_err = LDAP_OTHER; |
| 1676 |
rs->sr_text = "Proxy bind collision"; |
| 1677 |
if ( sendok & LDAP_BACK_SENDERR ) { |
| 1678 |
send_ldap_result( op, rs ); |
| 1679 |
} |
| 1680 |
return NULL; |
| 1681 |
} |
| 1682 |
} |
| 1683 |
|
| 1684 |
if ( LogTest( LDAP_DEBUG_TRACE ) ) { |
| 1685 |
char buf[STRLENOF("4294967295U") + 1] = { 0 }; |
| 1686 |
mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) ); |
| 1687 |
|
| 1688 |
Debug( LDAP_DEBUG_TRACE, |
| 1689 |
"%s meta_back_getconn: candidates=%d conn=%s inserted\n", |
| 1690 |
op->o_log_prefix, ncandidates, buf ); |
| 1691 |
} |
| 1692 |
|
| 1693 |
} else { |
| 1694 |
if ( LogTest( LDAP_DEBUG_TRACE ) ) { |
| 1695 |
char buf[STRLENOF("4294967295U") + 1] = { 0 }; |
| 1696 |
mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) ); |
| 1697 |
|
| 1698 |
Debug( LDAP_DEBUG_TRACE, |
| 1699 |
"%s meta_back_getconn: candidates=%d conn=%s fetched\n", |
| 1700 |
op->o_log_prefix, ncandidates, buf ); |
| 1701 |
} |
| 1702 |
} |
| 1703 |
|
| 1704 |
return mc; |
| 1705 |
} |
| 1706 |
|
| 1707 |
void |
| 1708 |
meta_back_release_conn_lock( |
| 1709 |
metainfo_t *mi, |
| 1710 |
metaconn_t *mc, |
| 1711 |
int dolock ) |
| 1712 |
{ |
| 1713 |
assert( mc != NULL ); |
| 1714 |
|
| 1715 |
if ( dolock ) { |
| 1716 |
ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex ); |
| 1717 |
} |
| 1718 |
assert( mc->mc_refcnt > 0 ); |
| 1719 |
mc->mc_refcnt--; |
| 1720 |
/* NOTE: the connection is removed if either it is tainted |
| 1721 |
* or if it is shared and no one else is using it. This needs |
| 1722 |
* to occur because for intrinsic reasons cached connections |
| 1723 |
* that are not privileged would live forever and pollute |
| 1724 |
* the connection space (and eat up resources). Maybe this |
| 1725 |
* should be configurable... */ |
| 1726 |
if ( LDAP_BACK_CONN_TAINTED( mc ) || !LDAP_BACK_CONN_CACHED( mc ) ) { |
| 1727 |
#if META_BACK_PRINT_CONNTREE > 0 |
| 1728 |
meta_back_print_conntree( mi, ">>> meta_back_release_conn" ); |
| 1729 |
#endif /* META_BACK_PRINT_CONNTREE */ |
| 1730 |
|
| 1731 |
if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) { |
| 1732 |
if ( mc->mc_q.tqe_prev != NULL ) { |
| 1733 |
assert( LDAP_BACK_CONN_CACHED( mc ) ); |
| 1734 |
assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 ); |
| 1735 |
LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q ); |
| 1736 |
mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--; |
| 1737 |
LDAP_TAILQ_ENTRY_INIT( mc, mc_q ); |
| 1738 |
|
| 1739 |
} else { |
| 1740 |
assert( !LDAP_BACK_CONN_CACHED( mc ) ); |
| 1741 |
} |
| 1742 |
|
| 1743 |
} else if ( LDAP_BACK_CONN_CACHED( mc ) ) { |
| 1744 |
metaconn_t *tmpmc; |
| 1745 |
|
| 1746 |
tmpmc = ldap_tavl_delete( &mi->mi_conninfo.lai_tree, |
| 1747 |
( caddr_t )mc, meta_back_conndnmc_cmp ); |
| 1748 |
|
| 1749 |
/* Overparanoid, but useful... */ |
| 1750 |
assert( tmpmc == NULL || tmpmc == mc ); |
| 1751 |
} |
| 1752 |
|
| 1753 |
LDAP_BACK_CONN_CACHED_CLEAR( mc ); |
| 1754 |
|
| 1755 |
#if META_BACK_PRINT_CONNTREE > 0 |
| 1756 |
meta_back_print_conntree( mi, "<<< meta_back_release_conn" ); |
| 1757 |
#endif /* META_BACK_PRINT_CONNTREE */ |
| 1758 |
|
| 1759 |
if ( mc->mc_refcnt == 0 ) { |
| 1760 |
meta_back_conn_free( mc ); |
| 1761 |
mc = NULL; |
| 1762 |
} |
| 1763 |
} |
| 1764 |
|
| 1765 |
if ( mc != NULL && LDAP_BACK_CONN_BINDING( mc ) ) { |
| 1766 |
LDAP_BACK_CONN_BINDING_CLEAR( mc ); |
| 1767 |
} |
| 1768 |
|
| 1769 |
if ( dolock ) { |
| 1770 |
ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex ); |
| 1771 |
} |
| 1772 |
} |
| 1773 |
|
| 1774 |
void |
| 1775 |
meta_back_quarantine( |
| 1776 |
Operation *op, |
| 1777 |
SlapReply *rs, |
| 1778 |
int candidate ) |
| 1779 |
{ |
| 1780 |
metainfo_t *mi = (metainfo_t *)op->o_bd->be_private; |
| 1781 |
metatarget_t *mt = mi->mi_targets[ candidate ]; |
| 1782 |
|
| 1783 |
slap_retry_info_t *ri = &mt->mt_quarantine; |
| 1784 |
|
| 1785 |
ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex ); |
| 1786 |
|
| 1787 |
if ( rs->sr_err == LDAP_UNAVAILABLE ) { |
| 1788 |
time_t new_last = slap_get_time(); |
| 1789 |
|
| 1790 |
switch ( mt->mt_isquarantined ) { |
| 1791 |
case LDAP_BACK_FQ_NO: |
| 1792 |
if ( ri->ri_last == new_last ) { |
| 1793 |
goto done; |
| 1794 |
} |
| 1795 |
|
| 1796 |
Debug( LDAP_DEBUG_ANY, |
| 1797 |
"%s meta_back_quarantine[%d]: enter.\n", |
| 1798 |
op->o_log_prefix, candidate ); |
| 1799 |
|
| 1800 |
ri->ri_idx = 0; |
| 1801 |
ri->ri_count = 0; |
| 1802 |
break; |
| 1803 |
|
| 1804 |
case LDAP_BACK_FQ_RETRYING: |
| 1805 |
Debug(LDAP_DEBUG_ANY, |
| 1806 |
"%s meta_back_quarantine[%d]: block #%d try #%d failed.\n", |
| 1807 |
op->o_log_prefix, candidate, ri->ri_idx, |
| 1808 |
ri->ri_count ); |
| 1809 |
|
| 1810 |
++ri->ri_count; |
| 1811 |
if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER |
| 1812 |
&& ri->ri_count == ri->ri_num[ ri->ri_idx ] ) |
| 1813 |
{ |
| 1814 |
ri->ri_count = 0; |
| 1815 |
++ri->ri_idx; |
| 1816 |
} |
| 1817 |
break; |
| 1818 |
|
| 1819 |
default: |
| 1820 |
goto done; |
| 1821 |
} |
| 1822 |
|
| 1823 |
mt->mt_isquarantined = LDAP_BACK_FQ_YES; |
| 1824 |
ri->ri_last = new_last; |
| 1825 |
|
| 1826 |
} else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) { |
| 1827 |
Debug( LDAP_DEBUG_ANY, |
| 1828 |
"%s meta_back_quarantine[%d]: exit.\n", |
| 1829 |
op->o_log_prefix, candidate ); |
| 1830 |
|
| 1831 |
if ( mi->mi_quarantine_f ) { |
| 1832 |
(void)mi->mi_quarantine_f( mi, candidate, |
| 1833 |
mi->mi_quarantine_p ); |
| 1834 |
} |
| 1835 |
|
| 1836 |
ri->ri_count = 0; |
| 1837 |
ri->ri_idx = 0; |
| 1838 |
mt->mt_isquarantined = LDAP_BACK_FQ_NO; |
| 1839 |
} |
| 1840 |
|
| 1841 |
done:; |
| 1842 |
ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex ); |
| 1843 |
} |