2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
28 #include <ac/socket.h>
29 #include <ac/string.h>
34 #include "../back-ldap/back-ldap.h"
35 #include "back-meta.h"
38 * meta_back_conndn_cmp
40 * compares two struct metaconn based on the value of the conn pointer
41 * and of the local DN; used by avl stuff
48 metaconn_t *mc1 = ( metaconn_t * )c1;
49 metaconn_t *mc2 = ( metaconn_t * )c2;
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.
56 rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
58 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
65 * meta_back_conndnmc_cmp
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
71 meta_back_conndnmc_cmp(
75 metaconn_t *mc1 = ( metaconn_t * )c1;
76 metaconn_t *mc2 = ( metaconn_t * )c2;
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.
83 rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
85 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
87 rc = SLAP_PTRCMP( mc1, mc2 );
97 * compares two struct metaconn based on the value of the conn pointer;
105 metaconn_t *mc1 = ( metaconn_t * )c1;
106 metaconn_t *mc2 = ( metaconn_t * )c2;
108 /* For shared sessions, conn is NULL. Only explicitly
109 * bound sessions will have non-NULL conn.
111 return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
115 * meta_back_conndn_dup
117 * returns -1 in case a duplicate struct metaconn has been inserted;
121 meta_back_conndn_dup(
125 metaconn_t *mc1 = ( metaconn_t * )c1;
126 metaconn_t *mc2 = ( metaconn_t * )c2;
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 ) )
139 * Debug stuff (got it from libavl)
141 #if META_BACK_PRINT_CONNTREE > 0
143 ravl_print( Avlnode *root, int depth )
152 ravl_print( root->avl_right, depth + 1 );
154 for ( i = 0; i < depth; i++ ) {
155 fprintf( stderr, "-" );
158 mc = (metaconn_t *)root->avl_data;
159 fprintf( stderr, "mc=%p local=\"%s\" conn=%p %s refcnt=%d%s\n",
161 mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "",
163 avl_bf2str( root->avl_bf ), mc->mc_refcnt,
164 LDAP_BACK_CONN_TAINTED( mc ) ? " tainted" : "" );
166 ravl_print( root->avl_left, depth + 1 );
170 meta_back_print_conntree( Avlnode *root, char *msg )
172 fprintf( stderr, "========> %s\n", msg );
175 fprintf( stderr, "\t(empty)\n" );
178 ravl_print( root, 0 );
181 fprintf( stderr, "<======== %s\n", msg );
183 #endif /* META_BACK_PRINT_CONNTREE */
191 * Allocates a connection structure, making room for all the referenced targets
197 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
199 int ntargets = mi->mi_ntargets;
201 assert( ntargets > 0 );
203 /* malloc all in one */
204 mc = ( metaconn_t * )ch_calloc( 1, sizeof( metaconn_t )
205 + sizeof( metasingleconn_t ) * ntargets );
212 mc->mc_authz_target = META_BOUND_NONE;
219 * meta_back_init_one_conn
221 * Initializes one connection
224 meta_back_init_one_conn(
230 ldap_back_send_t sendok )
232 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
233 metatarget_t *mt = mi->mi_targets[ candidate ];
234 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
237 int isauthz = ( candidate == mc->mc_authz_target );
240 #endif /* HAVE_TLS */
242 /* if the server is quarantined, and
243 * - the current interval did not expire yet, or
244 * - no more retries should occur,
245 * don't return the connection */
246 if ( mt->mt_isquarantined ) {
247 slap_retry_info_t *ri = &mt->mt_quarantine;
250 if ( mt->mt_isquarantined == LDAP_BACK_FQ_YES ) {
251 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
252 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
254 if ( LogTest( LDAP_DEBUG_ANY ) ) {
255 char buf[ SLAP_TEXT_BUFLEN ];
257 snprintf( buf, sizeof( buf ),
258 "meta_back_init_one_conn[%d]: quarantine "
259 "retry block #%d try #%d",
260 candidate, ri->ri_idx, ri->ri_count );
261 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
262 op->o_log_prefix, buf, 0 );
265 mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
270 rs->sr_err = LDAP_UNAVAILABLE;
271 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
272 send_ldap_result( op, rs );
281 if ( msc->msc_ld != NULL ) {
282 return rs->sr_err = LDAP_SUCCESS;
285 msc->msc_mscflags = 0;
288 * Attempts to initialize the connection to the target ds
290 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
291 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
293 is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
294 #endif /* HAVE_TLS */
295 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
296 if ( rs->sr_err != LDAP_SUCCESS ) {
301 * Set LDAP version. This will always succeed: If the client
302 * bound with a particular version, then so can we.
304 if ( mt->mt_version != 0 ) {
305 version = mt->mt_version;
307 } else if ( op->o_conn->c_protocol != 0 ) {
308 version = op->o_conn->c_protocol;
311 version = LDAP_VERSION3;
313 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
314 ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
316 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
317 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
318 LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
321 /* start TLS ("tls [try-]{start|propagate}" statement) */
322 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
325 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
327 * use asynchronous StartTLS; in case, chase referral
328 * FIXME: OpenLDAP does not return referral on StartTLS yet
332 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
333 if ( rs->sr_err == LDAP_SUCCESS ) {
334 LDAPMessage *res = NULL;
335 int rc, nretries = mt->mt_nretries;
338 LDAP_BACK_TV_SET( &tv );
341 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
344 rs->sr_err = LDAP_OTHER;
348 if ( nretries != 0 ) {
349 if ( nretries > 0 ) {
352 LDAP_BACK_TV_SET( &tv );
355 rs->sr_err = LDAP_OTHER;
359 /* only touch when activity actually took place... */
360 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
361 msc->msc_time = op->o_time;
366 if ( rc == LDAP_RES_EXTENDED ) {
367 struct berval *data = NULL;
369 /* NOTE: right now, data is unused, so don't get it */
370 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
371 NULL, NULL /* &data */ , 0 );
372 if ( rs->sr_err == LDAP_SUCCESS ) {
375 /* FIXME: matched? referrals? response controls? */
376 rs->sr_err = ldap_parse_result( msc->msc_ld, res,
377 &err, NULL, NULL, NULL, NULL, 1 );
380 if ( rs->sr_err == LDAP_SUCCESS ) {
384 /* FIXME: in case a referral
385 * is returned, should we try
386 * using it instead of the
388 if ( rs->sr_err == LDAP_SUCCESS ) {
389 ldap_install_tls( msc->msc_ld );
391 } else if ( rs->sr_err == LDAP_REFERRAL ) {
392 /* FIXME: LDAP_OPERATIONS_ERROR? */
393 rs->sr_err = LDAP_OTHER;
394 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
398 if ( data->bv_val ) {
399 ber_memfree( data->bv_val );
406 rs->sr_err = LDAP_OTHER;
413 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
415 * use synchronous StartTLS
417 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
418 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
420 /* if StartTLS is requested, only attempt it if the URL
421 * is not "ldaps://"; this may occur not only in case
422 * of misconfiguration, but also when used in the chain
423 * overlay, where the "uri" can be parsed out of a referral */
424 if ( rs->sr_err == LDAP_SERVER_DOWN
425 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
429 Debug( LDAP_DEBUG_ANY, "### %s meta_back_init_one_conn(TLS) ldap_unbind_ext[%d] ld=%p\n",
430 op->o_log_prefix, candidate, (void *)msc->msc_ld );
431 #endif /* DEBUG_205 */
433 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
438 #endif /* HAVE_TLS */
441 * Set the network timeout if set
443 if ( mt->mt_network_timeout != 0 ) {
444 struct timeval network_timeout;
446 network_timeout.tv_usec = 0;
447 network_timeout.tv_sec = mt->mt_network_timeout;
449 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
450 (void *)&network_timeout );
454 * If the connection DN is not null, an attempt to rewrite it is made
458 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
459 ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
460 if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
461 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
465 ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
469 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
470 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
471 ber_memfree_x( msc->msc_cred.bv_val, NULL );
472 BER_BVZERO( &msc->msc_cred );
474 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
475 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
476 BER_BVZERO( &msc->msc_bound_ndn );
478 if ( !BER_BVISEMPTY( &op->o_ndn )
479 && SLAP_IS_AUTHZ_BACKEND( op )
483 dc.conn = op->o_conn;
488 * Rewrite the bind dn if needed
490 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
491 &msc->msc_bound_ndn ) )
495 Debug( LDAP_DEBUG_ANY, "### %s meta_back_init_one_conn(rewrite) ldap_unbind_ext[%d] ld=%p\n",
496 op->o_log_prefix, candidate, (void *)msc->msc_ld );
497 #endif /* DEBUG_205 */
499 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
504 /* copy the DN if needed */
505 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
506 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
510 ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
514 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
517 if ( rs->sr_err == LDAP_SUCCESS ) {
519 * Sets a cookie for the rewrite session
521 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
524 rs->sr_err = slap_map_api2result( rs );
525 if ( sendok & LDAP_BACK_SENDERR ) {
526 send_ldap_result( op, rs );
537 * Retries one connection
545 ldap_back_send_t sendok )
547 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
548 metatarget_t *mt = mi->mi_targets[ candidate ];
549 metaconn_t *mc = *mcp;
550 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
551 int rc = LDAP_UNAVAILABLE,
554 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
555 binding = LDAP_BACK_CONN_BINDING( msc );
557 assert( mc->mc_refcnt > 0 );
558 if ( mc->mc_refcnt == 1 ) {
559 if ( LogTest( LDAP_DEBUG_ANY ) ) {
560 char buf[ SLAP_TEXT_BUFLEN ];
562 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
563 snprintf( buf, sizeof( buf ),
564 "retrying URI=\"%s\" DN=\"%s\"",
566 BER_BVISNULL( &msc->msc_bound_ndn ) ?
567 "" : msc->msc_bound_ndn.bv_val );
568 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
570 Debug( LDAP_DEBUG_ANY,
571 "%s meta_back_retry[%d]: %s.\n",
572 op->o_log_prefix, candidate, buf );
575 meta_clear_one_candidate( op, mc, candidate );
576 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
578 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
580 /* mc here must be the regular mc, reset and ready for init */
581 rc = meta_back_init_one_conn( op, rs, mc, candidate,
582 LDAP_BACK_CONN_ISPRIV( mc ), sendok );
584 /* restore the "binding" flag, in case */
586 LDAP_BACK_CONN_BINDING_SET( msc );
589 if ( rc == LDAP_SUCCESS ) {
590 rc = meta_back_single_dobind( op, rs, mcp, candidate,
591 sendok, mt->mt_nretries, 0 );
593 Debug( LDAP_DEBUG_ANY,
594 "%s meta_back_retry[%d]: "
595 "meta_back_single_dobind=%d\n",
596 op->o_log_prefix, candidate, rc );
597 if ( rc == LDAP_SUCCESS ) {
598 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
599 !BER_BVISEMPTY( &msc->msc_bound_ndn ) )
601 LDAP_BACK_CONN_ISBOUND_SET( msc );
604 LDAP_BACK_CONN_ISANON_SET( msc );
607 /* when bound, dispose of the "binding" flag */
609 LDAP_BACK_CONN_BINDING_CLEAR( msc );
615 if ( rc != LDAP_SUCCESS ) {
616 SlapReply *candidates = meta_back_candidates_get( op );
618 candidates[ candidate ].sr_err = rc;
620 if ( *mcp != NULL ) {
622 LDAP_BACK_CONN_BINDING_CLEAR( msc );
624 LDAP_BACK_CONN_TAINTED_SET( mc );
625 /* only release if mandatory; otherwise
626 * let the caller do what's best before
628 if ( META_BACK_ONERR_STOP( mi ) ) {
629 meta_back_release_conn_lock( op, mc, 0 );
637 send_ldap_result( op, rs );
641 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
642 meta_back_quarantine( op, rs, candidate );
645 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
647 return rc == LDAP_SUCCESS ? 1 : 0;
651 * callback for unique candidate selection
654 meta_back_conn_cb( Operation *op, SlapReply *rs )
656 assert( op->o_tag == LDAP_REQ_SEARCH );
658 switch ( rs->sr_type ) {
660 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
676 meta_back_get_candidate(
681 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
685 * tries to get a unique candidate
686 * (takes care of default target)
688 candidate = meta_back_select_unique_candidate( mi, ndn );
691 * if any is found, inits the connection
693 if ( candidate == META_TARGET_NONE ) {
694 rs->sr_err = LDAP_NO_SUCH_OBJECT;
695 rs->sr_text = "no suitable candidate target found";
697 } else if ( candidate == META_TARGET_MULTIPLE ) {
700 SlapReply rs2 = { 0 };
701 slap_callback cb2 = { 0 };
704 /* try to get a unique match for the request ndn
705 * among the multiple candidates available */
706 op2.o_tag = LDAP_REQ_SEARCH;
708 op2.o_req_ndn = *ndn;
709 op2.ors_scope = LDAP_SCOPE_BASE;
710 op2.ors_deref = LDAP_DEREF_NEVER;
711 op2.ors_attrs = slap_anlist_no_attrs;
712 op2.ors_attrsonly = 0;
713 op2.ors_limit = NULL;
715 op2.ors_tlimit = SLAP_NO_LIMIT;
717 f.f_choice = LDAP_FILTER_PRESENT;
718 f.f_desc = slap_schema.si_ad_objectClass;
720 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
722 op2.o_callback = &cb2;
723 cb2.sc_response = meta_back_conn_cb;
724 cb2.sc_private = (void *)&candidate;
726 rc = op->o_bd->be_search( &op2, &rs2 );
728 switch ( rs2.sr_err ) {
731 rs->sr_err = rs2.sr_err;
734 case LDAP_SIZELIMIT_EXCEEDED:
735 /* if multiple candidates can serve the operation,
736 * and a default target is defined, and it is
737 * a candidate, try using it (FIXME: YMMV) */
738 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
739 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
740 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
742 candidate = mi->mi_defaulttarget;
743 rs->sr_err = LDAP_SUCCESS;
747 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
748 rs->sr_text = "cannot select unique candidate target";
754 rs->sr_err = LDAP_SUCCESS;
760 static void *meta_back_candidates_dummy;
763 meta_back_candidates_keyfree(
767 metacandidates_t *mc = (metacandidates_t *)data;
769 ber_memfree_x( mc->mc_candidates, NULL );
770 ber_memfree_x( data, NULL );
774 meta_back_candidates_get( Operation *op )
776 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
777 metacandidates_t *mc;
779 if ( op->o_threadctx ) {
782 ldap_pvt_thread_pool_getkey( op->o_threadctx,
783 &meta_back_candidates_dummy, &data, NULL );
784 mc = (metacandidates_t *)data;
787 mc = mi->mi_candidates;
791 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
792 mc->mc_ntargets = mi->mi_ntargets;
793 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
794 if ( op->o_threadctx ) {
798 ldap_pvt_thread_pool_setkey( op->o_threadctx,
799 &meta_back_candidates_dummy, data,
800 meta_back_candidates_keyfree );
803 mi->mi_candidates = mc;
806 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
807 /* NOTE: in the future, may want to allow back-config
808 * to add/remove targets from back-meta... */
809 mc->mc_candidates = ch_realloc( mc->mc_candidates,
810 sizeof( SlapReply ) * mi->mi_ntargets );
811 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
812 sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
813 mc->mc_ntargets = mi->mi_ntargets;
816 return mc->mc_candidates;
822 * Prepares the connection structure
826 * - determine what DN is being requested:
828 * op requires candidate checks
830 * add unique parent of o_req_ndn
831 * bind unique^*[/all] o_req_ndn [no check]
832 * compare unique^+ o_req_ndn
833 * delete unique o_req_ndn
834 * modify unique o_req_ndn
835 * search any o_req_ndn
836 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
838 * - for ops that require the candidate to be unique, in case of multiple
839 * occurrences an internal search with sizeLimit=1 is performed
840 * if a unique candidate can actually be determined. If none is found,
841 * the operation aborts; if multiple are found, the default target
842 * is used if defined and candidate; otherwise the operation aborts.
844 * *^note: actually, the bind operation is handled much like a search;
845 * i.e. the bind is broadcast to all candidate targets.
847 * +^note: actually, the compare operation is handled much like a search;
848 * i.e. the compare is broadcast to all candidate targets, while checking
849 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
857 ldap_back_send_t sendok )
859 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
860 metaconn_t *mc = NULL,
862 int cached = META_TARGET_NONE,
863 i = META_TARGET_NONE,
869 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
873 META_DNTYPE_NEWPARENT
874 } dn_type = META_DNTYPE_ENTRY;
875 struct berval ndn = op->o_req_ndn,
878 SlapReply *candidates = meta_back_candidates_get( op );
880 /* Internal searches are privileged and shared. So is root. */
881 /* FIXME: there seem to be concurrency issues */
882 if ( op->o_do_not_cache || be_isroot( op ) ) {
883 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
884 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
885 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
888 mc_curr.mc_local_ndn = op->o_ndn;
890 /* Explicit binds must not be shared */
891 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
892 mc_curr.mc_conn = op->o_conn;
895 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
899 /* Explicit Bind requests always get their own conn */
900 if ( !( sendok & LDAP_BACK_BINDING ) ) {
901 /* Searches for a metaconn in the avl tree */
903 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
904 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
905 (caddr_t)&mc_curr, meta_back_conndn_cmp );
907 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
908 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
910 #if META_BACK_PRINT_CONNTREE > 0
911 meta_back_print_conntree( mi->mi_conninfo.lai_tree, ">>> meta_back_getconn" );
912 #endif /* META_BACK_PRINT_CONNTREE */
913 /* don't let anyone else use this expired connection */
914 (void)avl_delete( &mi->mi_conninfo.lai_tree,
915 (caddr_t)mc, meta_back_conndnmc_cmp );
916 #if META_BACK_PRINT_CONNTREE > 0
917 meta_back_print_conntree( mi->mi_conninfo.lai_tree, "<<< meta_back_getconn" );
918 #endif /* META_BACK_PRINT_CONNTREE */
919 LDAP_BACK_CONN_TAINTED_SET( mc );
921 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired.\n",
922 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc ) );
925 /* Don't reuse connections while they're still binding
926 * NOTE: only makes sense for binds */
927 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
928 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
929 ldap_pvt_thread_yield();
935 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
938 switch ( op->o_tag ) {
940 /* if we go to selection, the entry must not exist,
941 * and we must be able to resolve the parent */
942 dn_type = META_DNTYPE_PARENT;
943 dnParent( &ndn, &pndn );
946 case LDAP_REQ_MODRDN:
947 /* if nnewSuperior is not NULL, it must resolve
948 * to the same candidate as the req_ndn */
949 if ( op->orr_nnewSup ) {
950 dn_type = META_DNTYPE_NEWPARENT;
955 /* if bound as rootdn, the backend must bind to all targets
956 * with the administrative identity */
957 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
958 op_type = META_OP_REQUIRE_ALL;
962 case LDAP_REQ_COMPARE:
963 case LDAP_REQ_DELETE:
964 case LDAP_REQ_MODIFY:
965 /* just a unique candidate */
968 case LDAP_REQ_SEARCH:
969 /* allow multiple candidates for the searchBase */
970 op_type = META_OP_ALLOW_MULTIPLE;
974 /* right now, just break (exop?) */
979 * require all connections ...
981 if ( op_type == META_OP_REQUIRE_ALL ) {
983 /* Looks like we didn't get a bind. Open a new session... */
985 assert( new_conn == 0 );
986 mc = metaconn_alloc( op );
987 mc->mc_conn = mc_curr.mc_conn;
988 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
990 if ( sendok & LDAP_BACK_BINDING ) {
991 LDAP_BACK_CONN_BINDING_SET( mc );
993 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
994 LDAP_BACK_CONN_ISPRIV_SET( mc );
998 /* TODO: if any of the connections is binding,
999 * release mc and create a new one */
1002 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1004 * The target is activated; if needed, it is
1007 candidates[ i ].sr_err = meta_back_init_one_conn( op,
1008 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1010 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1011 META_CANDIDATE_SET( &candidates[ i ] );
1017 * FIXME: in case one target cannot
1018 * be init'd, should the other ones
1021 META_CANDIDATE_RESET( &candidates[ i ] );
1022 err = candidates[ i ].sr_err;
1027 if ( ncandidates == 0 ) {
1030 meta_back_conn_free( mc );
1033 meta_back_release_conn( op, mc );
1036 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1037 rs->sr_text = "Unable to select valid candidates";
1039 if ( sendok & LDAP_BACK_SENDERR ) {
1040 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1041 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1043 send_ldap_result( op, rs );
1045 rs->sr_matched = NULL;
1055 * looks in cache, if any
1057 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1058 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1061 if ( op_type == META_OP_REQUIRE_SINGLE ) {
1062 metatarget_t *mt = NULL;
1063 metasingleconn_t *msc = NULL;
1067 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1068 META_CANDIDATE_RESET( &candidates[ j ] );
1072 * tries to get a unique candidate
1073 * (takes care of default target)
1075 if ( i == META_TARGET_NONE ) {
1076 i = meta_back_get_candidate( op, rs, &ndn );
1078 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1079 i = meta_back_get_candidate( op, rs, &pndn );
1082 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1084 meta_back_release_conn( op, mc );
1087 if ( sendok & LDAP_BACK_SENDERR ) {
1088 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1089 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1091 send_ldap_result( op, rs );
1093 rs->sr_matched = NULL;
1100 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1103 meta_back_release_conn( op, mc );
1106 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1107 rs->sr_text = "cross-target rename not supported";
1108 if ( sendok & LDAP_BACK_SENDERR ) {
1109 send_ldap_result( op, rs );
1116 Debug( LDAP_DEBUG_TRACE,
1117 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1118 i, op->o_req_ndn.bv_val, 0 );
1121 /* Retries searching for a metaconn in the avl tree
1122 * the reason is that the connection might have been
1123 * created by meta_back_get_candidate() */
1124 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1126 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1127 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1128 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1130 /* Don't reuse connections while they're still binding */
1131 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1132 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1133 ldap_pvt_thread_yield();
1138 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1141 /* Looks like we didn't get a bind. Open a new session... */
1143 assert( new_conn == 0 );
1144 mc = metaconn_alloc( op );
1145 mc->mc_conn = mc_curr.mc_conn;
1146 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1148 if ( sendok & LDAP_BACK_BINDING ) {
1149 LDAP_BACK_CONN_BINDING_SET( mc );
1151 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1152 LDAP_BACK_CONN_ISPRIV_SET( mc );
1158 * Clear all other candidates
1160 ( void )meta_clear_unused_candidates( op, i );
1162 mt = mi->mi_targets[ i ];
1163 msc = &mc->mc_conns[ i ];
1166 * The target is activated; if needed, it is
1167 * also init'd. In case of error, meta_back_init_one_conn
1168 * sends the appropriate result.
1170 err = meta_back_init_one_conn( op, rs, mc, i,
1171 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
1172 if ( err != LDAP_SUCCESS ) {
1174 * FIXME: in case one target cannot
1175 * be init'd, should the other ones
1178 META_CANDIDATE_RESET( &candidates[ i ] );
1181 meta_back_conn_free( mc );
1184 meta_back_release_conn( op, mc );
1189 candidates[ i ].sr_err = LDAP_SUCCESS;
1190 META_CANDIDATE_SET( &candidates[ i ] );
1198 * if no unique candidate ...
1202 /* Looks like we didn't get a bind. Open a new session... */
1204 assert( new_conn == 0 );
1205 mc = metaconn_alloc( op );
1206 mc->mc_conn = mc_curr.mc_conn;
1207 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1209 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1210 LDAP_BACK_CONN_ISPRIV_SET( mc );
1214 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1215 metatarget_t *mt = mi->mi_targets[ i ];
1217 META_CANDIDATE_RESET( &candidates[ i ] );
1220 || meta_back_is_candidate( mt, &op->o_req_ndn,
1221 LDAP_SCOPE_SUBTREE ) )
1225 * The target is activated; if needed, it is
1228 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1229 LDAP_BACK_CONN_ISPRIV( &mc_curr ), LDAP_BACK_DONTSEND );
1230 if ( lerr == LDAP_SUCCESS ) {
1231 META_CANDIDATE_SET( &candidates[ i ] );
1232 candidates[ i ].sr_err = LDAP_SUCCESS;
1235 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1236 op->o_log_prefix, i, 0 );
1238 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1239 META_CANDIDATE_SET( &candidates[ i ] );
1240 candidates[ i ].sr_err = LDAP_UNAVAILABLE;
1242 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1243 op->o_log_prefix, i,
1244 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1249 * FIXME: in case one target cannot
1250 * be init'd, should the other ones
1254 ( void )meta_clear_one_candidate( op, mc, i );
1256 /* leave the target candidate, but record the error for later use */
1257 candidates[ i ].sr_err = lerr;
1260 if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1261 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined: %d\n",
1262 op->o_log_prefix, i, lerr );
1265 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed: %d\n",
1266 op->o_log_prefix, i, lerr );
1269 if ( META_BACK_ONERR_STOP( mi ) ) {
1270 if ( sendok & LDAP_BACK_SENDERR ) {
1271 send_ldap_result( op, rs );
1276 meta_back_conn_free( mc );
1279 meta_back_release_conn( op, mc );
1291 ( void )meta_clear_one_candidate( op, mc, i );
1296 if ( ncandidates == 0 ) {
1299 meta_back_conn_free( mc );
1302 meta_back_release_conn( op, mc );
1305 if ( rs->sr_err == LDAP_SUCCESS ) {
1306 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1307 rs->sr_text = "Unable to select valid candidates";
1310 if ( sendok & LDAP_BACK_SENDERR ) {
1311 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1312 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1314 send_ldap_result( op, rs );
1316 rs->sr_matched = NULL;
1324 /* clear out meta_back_init_one_conn non-fatal errors */
1325 rs->sr_err = LDAP_SUCCESS;
1328 /* touch the timestamp */
1329 if ( mi->mi_idle_timeout != 0 ) {
1330 mc->mc_time = op->o_time;
1334 if ( mi->mi_conn_ttl ) {
1335 mc->mc_create_time = op->o_time;
1339 * Inserts the newly created metaconn in the avl tree
1341 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1342 #if META_BACK_PRINT_CONNTREE > 0
1343 meta_back_print_conntree( mi->mi_conninfo.lai_tree, ">>> meta_back_getconn" );
1344 #endif /* META_BACK_PRINT_CONNTREE */
1345 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1346 meta_back_conndn_cmp, meta_back_conndn_dup );
1347 #if META_BACK_PRINT_CONNTREE > 0
1348 meta_back_print_conntree( mi->mi_conninfo.lai_tree, ">>> meta_back_getconn" );
1349 #endif /* META_BACK_PRINT_CONNTREE */
1350 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1353 * Err could be -1 in case a duplicate metaconn is inserted
1360 /* duplicate: free and try to get the newly created one */
1361 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1365 LDAP_BACK_CONN_TAINTED_SET( mc );
1369 Debug( LDAP_DEBUG_ANY,
1370 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1371 op->o_log_prefix, ncandidates,
1372 LDAP_BACK_PCONN_ID( mc ) );
1375 meta_back_conn_free( mc );
1377 rs->sr_err = LDAP_OTHER;
1378 rs->sr_text = "proxy bind collision";
1379 if ( sendok & LDAP_BACK_SENDERR ) {
1380 send_ldap_result( op, rs );
1386 Debug( LDAP_DEBUG_TRACE,
1387 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1388 op->o_log_prefix, ncandidates,
1389 LDAP_BACK_PCONN_ID( mc ) );
1392 Debug( LDAP_DEBUG_TRACE,
1393 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1394 op->o_log_prefix, ncandidates,
1395 LDAP_BACK_PCONN_ID( mc ) );
1402 meta_back_release_conn_lock(
1407 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1409 assert( mc != NULL );
1412 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1414 assert( mc->mc_refcnt > 0 );
1416 /* NOTE: the connection is removed if either it is tainted
1417 * or if it is shared and no one else is using it. This needs
1418 * to occur because for intrinsic reasons cached connections
1419 * that are not privileged would live forever and pollute
1420 * the connection space (and eat up resources). Maybe this
1421 * should be configurable... */
1422 if ( LDAP_BACK_CONN_TAINTED( mc ) ||
1423 ( !LDAP_BACK_CONN_ISPRIV( mc ) &&
1424 LDAP_BACK_PCONN_ISPRIV( mc ) &&
1425 mc->mc_refcnt == 0 ) )
1427 Debug( LDAP_DEBUG_TRACE, "%s meta_back_release_conn: mc=%p conn=%ld tainted.\n",
1428 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc ) );
1429 #if META_BACK_PRINT_CONNTREE > 0
1430 meta_back_print_conntree( mi->mi_conninfo.lai_tree, ">>> meta_back_release_conn" );
1431 #endif /* META_BACK_PRINT_CONNTREE */
1432 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1433 ( caddr_t )mc, meta_back_conndnmc_cmp );
1434 #if META_BACK_PRINT_CONNTREE > 0
1435 meta_back_print_conntree( mi->mi_conninfo.lai_tree, "<<< meta_back_release_conn" );
1436 #endif /* META_BACK_PRINT_CONNTREE */
1437 if ( mc->mc_refcnt == 0 ) {
1438 meta_back_conn_free( mc );
1444 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1448 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1453 meta_back_quarantine(
1458 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1459 metatarget_t *mt = mi->mi_targets[ candidate ];
1461 slap_retry_info_t *ri = &mt->mt_quarantine;
1463 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1465 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1466 time_t new_last = slap_get_time();
1468 switch ( mt->mt_isquarantined ) {
1469 case LDAP_BACK_FQ_NO:
1470 if ( ri->ri_last == new_last ) {
1474 Debug( LDAP_DEBUG_ANY,
1475 "%s meta_back_quarantine[%d]: enter.\n",
1476 op->o_log_prefix, candidate, 0 );
1482 case LDAP_BACK_FQ_RETRYING:
1483 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1484 char buf[ SLAP_TEXT_BUFLEN ];
1486 snprintf( buf, sizeof( buf ),
1487 "meta_back_quarantine[%d]: block #%d try #%d failed",
1488 candidate, ri->ri_idx, ri->ri_count );
1489 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1490 op->o_log_prefix, buf, 0 );
1494 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1495 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1506 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1507 ri->ri_last = new_last;
1509 } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1510 Debug( LDAP_DEBUG_ANY,
1511 "%s meta_back_quarantine[%d]: exit.\n",
1512 op->o_log_prefix, candidate, 0 );
1514 if ( mi->mi_quarantine_f ) {
1515 (void)mi->mi_quarantine_f( mi, candidate,
1516 mi->mi_quarantine_p );
1521 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1525 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );