2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2007 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 meta_back_ravl_print( Avlnode *root, int depth )
152 meta_back_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 meta_back_ravl_print( root->avl_left, depth + 1 );
169 /* NOTE: duplicate from back-ldap/bind.c */
170 static char* priv2str[] = {
181 meta_back_print_conntree( metainfo_t *mi, char *msg )
185 fprintf( stderr, "========> %s\n", msg );
187 for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
191 fprintf( stderr, " %s[%d]\n", priv2str[ c ], mi->mi_conn_priv[ c ].mic_num );
193 LDAP_TAILQ_FOREACH( mc, &mi->mi_conn_priv[ c ].mic_priv, mc_q )
195 fprintf( stderr, " [%d] mc=%p local=\"%s\" conn=%p refcnt=%d flags=0x%08x\n",
198 mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "",
199 (void *)mc->mc_conn, mc->mc_refcnt, mc->msc_mscflags );
204 if ( mi->mi_conninfo.lai_tree == NULL ) {
205 fprintf( stderr, "\t(empty)\n" );
208 meta_back_ravl_print( mi->mi_conninfo.lai_tree, 0 );
211 fprintf( stderr, "<======== %s\n", msg );
213 #endif /* META_BACK_PRINT_CONNTREE */
221 * Allocates a connection structure, making room for all the referenced targets
227 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
229 int ntargets = mi->mi_ntargets;
231 assert( ntargets > 0 );
233 /* malloc all in one */
234 mc = ( metaconn_t * )ch_calloc( 1, sizeof( metaconn_t )
235 + sizeof( metasingleconn_t ) * ( ntargets - 1 ) );
242 mc->mc_authz_target = META_BOUND_NONE;
249 * meta_back_init_one_conn
251 * Initializes one connection
254 meta_back_init_one_conn(
260 ldap_back_send_t sendok,
263 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
264 metatarget_t *mt = mi->mi_targets[ candidate ];
265 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
268 int isauthz = ( candidate == mc->mc_authz_target );
272 #endif /* HAVE_TLS */
274 /* if the server is quarantined, and
275 * - the current interval did not expire yet, or
276 * - no more retries should occur,
277 * don't return the connection */
278 if ( mt->mt_isquarantined ) {
279 slap_retry_info_t *ri = &mt->mt_quarantine;
282 if ( mt->mt_isquarantined == LDAP_BACK_FQ_YES ) {
283 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
284 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
286 if ( LogTest( LDAP_DEBUG_ANY ) ) {
287 char buf[ SLAP_TEXT_BUFLEN ];
289 snprintf( buf, sizeof( buf ),
290 "meta_back_init_one_conn[%d]: quarantine "
291 "retry block #%d try #%d",
292 candidate, ri->ri_idx, ri->ri_count );
293 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
294 op->o_log_prefix, buf, 0 );
297 mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
302 rs->sr_err = LDAP_UNAVAILABLE;
303 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
304 send_ldap_result( op, rs );
312 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
318 if ( LDAP_BACK_CONN_ISBOUND( msc )
319 || LDAP_BACK_CONN_ISANON( msc ) )
321 assert( msc->msc_ld != NULL );
322 rs->sr_err = LDAP_SUCCESS;
325 } else if ( META_BACK_CONN_CREATING( msc )
326 || LDAP_BACK_CONN_BINDING( msc ) )
328 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
330 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
333 ldap_pvt_thread_yield();
337 /* sounds more appropriate */
338 rs->sr_err = LDAP_BUSY;
341 } else if ( META_BACK_CONN_INITED( msc ) ) {
342 assert( msc->msc_ld != NULL );
343 rs->sr_err = LDAP_SUCCESS;
350 META_BACK_CONN_CREATING_SET( msc );
354 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
358 if ( rs->sr_err != LDAP_SUCCESS && op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
359 send_ldap_result( op, rs );
365 assert( msc->msc_ld == NULL );
368 * Attempts to initialize the connection to the target ds
370 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
371 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
373 is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
374 #endif /* HAVE_TLS */
375 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
376 if ( rs->sr_err != LDAP_SUCCESS ) {
381 * Set LDAP version. This will always succeed: If the client
382 * bound with a particular version, then so can we.
384 if ( mt->mt_version != 0 ) {
385 version = mt->mt_version;
387 } else if ( op->o_conn->c_protocol != 0 ) {
388 version = op->o_conn->c_protocol;
391 version = LDAP_VERSION3;
393 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
394 ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
396 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
397 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
398 LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
401 /* start TLS ("tls [try-]{start|propagate}" statement) */
402 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
405 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
407 * use asynchronous StartTLS; in case, chase referral
408 * FIXME: OpenLDAP does not return referral on StartTLS yet
412 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
413 if ( rs->sr_err == LDAP_SUCCESS ) {
414 LDAPMessage *res = NULL;
415 int rc, nretries = mt->mt_nretries;
418 LDAP_BACK_TV_SET( &tv );
421 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
424 rs->sr_err = LDAP_OTHER;
428 if ( nretries != 0 ) {
429 if ( nretries > 0 ) {
432 LDAP_BACK_TV_SET( &tv );
435 rs->sr_err = LDAP_OTHER;
439 /* only touch when activity actually took place... */
440 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
441 msc->msc_time = op->o_time;
446 if ( rc == LDAP_RES_EXTENDED ) {
447 struct berval *data = NULL;
449 /* NOTE: right now, data is unused, so don't get it */
450 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
451 NULL, NULL /* &data */ , 0 );
452 if ( rs->sr_err == LDAP_SUCCESS ) {
455 /* FIXME: matched? referrals? response controls? */
456 rs->sr_err = ldap_parse_result( msc->msc_ld, res,
457 &err, NULL, NULL, NULL, NULL, 1 );
460 if ( rs->sr_err == LDAP_SUCCESS ) {
464 /* FIXME: in case a referral
465 * is returned, should we try
466 * using it instead of the
468 if ( rs->sr_err == LDAP_SUCCESS ) {
469 ldap_install_tls( msc->msc_ld );
471 } else if ( rs->sr_err == LDAP_REFERRAL ) {
472 /* FIXME: LDAP_OPERATIONS_ERROR? */
473 rs->sr_err = LDAP_OTHER;
474 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
478 if ( data->bv_val ) {
479 ber_memfree( data->bv_val );
486 rs->sr_err = LDAP_OTHER;
493 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
495 * use synchronous StartTLS
497 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
498 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
500 /* if StartTLS is requested, only attempt it if the URL
501 * is not "ldaps://"; this may occur not only in case
502 * of misconfiguration, but also when used in the chain
503 * overlay, where the "uri" can be parsed out of a referral */
504 if ( rs->sr_err == LDAP_SERVER_DOWN
505 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
509 Debug( LDAP_DEBUG_ANY, "### %s meta_back_init_one_conn(TLS) ldap_unbind_ext[%d] ld=%p\n",
510 op->o_log_prefix, candidate, (void *)msc->msc_ld );
511 #endif /* DEBUG_205 */
513 /* need to trash a failed Start TLS */
514 meta_clear_one_candidate( op, mc, candidate );
518 #endif /* HAVE_TLS */
521 * Set the network timeout if set
523 if ( mt->mt_network_timeout != 0 ) {
524 struct timeval network_timeout;
526 network_timeout.tv_usec = 0;
527 network_timeout.tv_sec = mt->mt_network_timeout;
529 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
530 (void *)&network_timeout );
534 * If the connection DN is not null, an attempt to rewrite it is made
538 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
539 ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
540 if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
541 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
542 memset( msc->msc_cred.bv_val, 0,
543 msc->msc_cred.bv_len );
545 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
549 ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
553 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
554 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
555 ber_memfree_x( msc->msc_cred.bv_val, NULL );
556 BER_BVZERO( &msc->msc_cred );
558 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
559 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
560 BER_BVZERO( &msc->msc_bound_ndn );
562 if ( !BER_BVISEMPTY( &op->o_ndn )
563 && SLAP_IS_AUTHZ_BACKEND( op )
567 dc.conn = op->o_conn;
572 * Rewrite the bind dn if needed
574 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
575 &msc->msc_bound_ndn ) )
579 Debug( LDAP_DEBUG_ANY, "### %s meta_back_init_one_conn(rewrite) ldap_unbind_ext[%d] ld=%p\n",
580 op->o_log_prefix, candidate, (void *)msc->msc_ld );
581 #endif /* DEBUG_205 */
583 /* need to trash a connection not fully established */
584 meta_clear_one_candidate( op, mc, candidate );
588 /* copy the DN if needed */
589 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
590 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
593 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
596 ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
600 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
604 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
606 META_BACK_CONN_CREATING_CLEAR( msc );
607 if ( rs->sr_err == LDAP_SUCCESS ) {
609 * Sets a cookie for the rewrite session
611 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
612 META_BACK_CONN_INITED_SET( msc );
615 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
618 if ( rs->sr_err != LDAP_SUCCESS ) {
619 rs->sr_err = slap_map_api2result( rs );
620 if ( sendok & LDAP_BACK_SENDERR ) {
621 send_ldap_result( op, rs );
632 * Retries one connection
640 ldap_back_send_t sendok )
642 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
643 metatarget_t *mt = mi->mi_targets[ candidate ];
644 metaconn_t *mc = *mcp;
645 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
646 int rc = LDAP_UNAVAILABLE,
650 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
652 assert( !META_BACK_CONN_CREATING( msc ) );
653 binding = LDAP_BACK_CONN_BINDING( msc );
654 LDAP_BACK_CONN_BINDING_CLEAR( msc );
656 assert( mc->mc_refcnt > 0 );
657 if ( mc->mc_refcnt == 1 ) {
658 if ( LogTest( LDAP_DEBUG_ANY ) ) {
659 char buf[ SLAP_TEXT_BUFLEN ];
661 /* this lock is required; however,
662 * it's invoked only when logging is on */
663 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
664 snprintf( buf, sizeof( buf ),
665 "retrying URI=\"%s\" DN=\"%s\"",
667 BER_BVISNULL( &msc->msc_bound_ndn ) ?
668 "" : msc->msc_bound_ndn.bv_val );
669 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
671 Debug( LDAP_DEBUG_ANY,
672 "%s meta_back_retry[%d]: %s.\n",
673 op->o_log_prefix, candidate, buf );
676 meta_clear_one_candidate( op, mc, candidate );
677 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
679 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
681 /* mc here must be the regular mc, reset and ready for init */
682 rc = meta_back_init_one_conn( op, rs, mc, candidate,
683 LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 );
685 /* restore the "binding" flag, in case */
687 LDAP_BACK_CONN_BINDING_SET( msc );
690 if ( rc == LDAP_SUCCESS ) {
692 rc = meta_back_single_dobind( op, rs, mcp, candidate,
693 sendok, mt->mt_nretries, 0 );
695 Debug( LDAP_DEBUG_ANY,
696 "%s meta_back_retry[%d]: "
697 "meta_back_single_dobind=%d\n",
698 op->o_log_prefix, candidate, rc );
699 if ( rc == LDAP_SUCCESS ) {
700 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
701 !BER_BVISEMPTY( &msc->msc_bound_ndn ) )
703 LDAP_BACK_CONN_ISBOUND_SET( msc );
706 LDAP_BACK_CONN_ISANON_SET( msc );
709 /* when bound, dispose of the "binding" flag */
711 LDAP_BACK_CONN_BINDING_CLEAR( msc );
716 /* don't send twice */
717 sendok &= ~LDAP_BACK_SENDERR;
720 if ( rc != LDAP_SUCCESS ) {
721 SlapReply *candidates = meta_back_candidates_get( op );
723 candidates[ candidate ].sr_err = rc;
725 if ( *mcp != NULL ) {
726 if ( mc->mc_refcnt == 1 ) {
728 LDAP_BACK_CONN_BINDING_CLEAR( msc );
730 (void)meta_clear_one_candidate( op, mc, candidate );
733 LDAP_BACK_CONN_TAINTED_SET( mc );
734 /* only release if mandatory; otherwise
735 * let the caller do what's best before
737 if ( META_BACK_ONERR_STOP( mi ) ) {
738 meta_back_release_conn_lock( op, mc, 0 );
742 #if META_BACK_PRINT_CONNTREE > 0
743 meta_back_print_conntree( mi, ">>> meta_back_retry" );
744 #endif /* META_BACK_PRINT_CONNTREE */
746 /* FIXME: could be done better, reworking meta_back_release_conn_lock() */
747 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
748 if ( mc->mc_q.tqe_prev != NULL ) {
749 assert( LDAP_BACK_CONN_CACHED( mc ) );
750 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
751 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
753 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
754 mc->mc_q.tqe_prev = NULL;
755 mc->mc_q.tqe_next = NULL;
758 assert( !LDAP_BACK_CONN_CACHED( mc ) );
762 /* FIXME: check if in tree, for consistency? */
763 (void)avl_delete( &mi->mi_conninfo.lai_tree,
764 ( caddr_t )mc, meta_back_conndnmc_cmp );
766 LDAP_BACK_CONN_CACHED_CLEAR( mc );
768 #if META_BACK_PRINT_CONNTREE > 0
769 meta_back_print_conntree( mi, "<<< meta_back_retry" );
770 #endif /* META_BACK_PRINT_CONNTREE */
774 if ( sendok & LDAP_BACK_SENDERR ) {
777 send_ldap_result( op, rs );
781 if ( quarantine && META_BACK_TGT_QUARANTINE( mt ) ) {
782 meta_back_quarantine( op, rs, candidate );
785 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
787 return rc == LDAP_SUCCESS ? 1 : 0;
791 * callback for unique candidate selection
794 meta_back_conn_cb( Operation *op, SlapReply *rs )
796 assert( op->o_tag == LDAP_REQ_SEARCH );
798 switch ( rs->sr_type ) {
800 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
816 meta_back_get_candidate(
821 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
825 * tries to get a unique candidate
826 * (takes care of default target)
828 candidate = meta_back_select_unique_candidate( mi, ndn );
831 * if any is found, inits the connection
833 if ( candidate == META_TARGET_NONE ) {
834 rs->sr_err = LDAP_NO_SUCH_OBJECT;
835 rs->sr_text = "no suitable candidate target found";
837 } else if ( candidate == META_TARGET_MULTIPLE ) {
840 SlapReply rs2 = { 0 };
841 slap_callback cb2 = { 0 };
844 /* try to get a unique match for the request ndn
845 * among the multiple candidates available */
846 op2.o_tag = LDAP_REQ_SEARCH;
848 op2.o_req_ndn = *ndn;
849 op2.ors_scope = LDAP_SCOPE_BASE;
850 op2.ors_deref = LDAP_DEREF_NEVER;
851 op2.ors_attrs = slap_anlist_no_attrs;
852 op2.ors_attrsonly = 0;
853 op2.ors_limit = NULL;
855 op2.ors_tlimit = SLAP_NO_LIMIT;
857 f.f_choice = LDAP_FILTER_PRESENT;
858 f.f_desc = slap_schema.si_ad_objectClass;
860 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
862 op2.o_callback = &cb2;
863 cb2.sc_response = meta_back_conn_cb;
864 cb2.sc_private = (void *)&candidate;
866 rc = op->o_bd->be_search( &op2, &rs2 );
868 switch ( rs2.sr_err ) {
871 rs->sr_err = rs2.sr_err;
874 case LDAP_SIZELIMIT_EXCEEDED:
875 /* if multiple candidates can serve the operation,
876 * and a default target is defined, and it is
877 * a candidate, try using it (FIXME: YMMV) */
878 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
879 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
880 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
882 candidate = mi->mi_defaulttarget;
883 rs->sr_err = LDAP_SUCCESS;
887 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
888 rs->sr_text = "cannot select unique candidate target";
894 rs->sr_err = LDAP_SUCCESS;
900 static void *meta_back_candidates_dummy;
903 meta_back_candidates_keyfree(
907 metacandidates_t *mc = (metacandidates_t *)data;
909 ber_memfree_x( mc->mc_candidates, NULL );
910 ber_memfree_x( data, NULL );
914 meta_back_candidates_get( Operation *op )
916 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
917 metacandidates_t *mc;
919 if ( op->o_threadctx ) {
922 ldap_pvt_thread_pool_getkey( op->o_threadctx,
923 &meta_back_candidates_dummy, &data, NULL );
924 mc = (metacandidates_t *)data;
927 mc = mi->mi_candidates;
931 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
932 mc->mc_ntargets = mi->mi_ntargets;
933 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
934 if ( op->o_threadctx ) {
938 ldap_pvt_thread_pool_setkey( op->o_threadctx,
939 &meta_back_candidates_dummy, data,
940 meta_back_candidates_keyfree );
943 mi->mi_candidates = mc;
946 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
947 /* NOTE: in the future, may want to allow back-config
948 * to add/remove targets from back-meta... */
949 mc->mc_candidates = ch_realloc( mc->mc_candidates,
950 sizeof( SlapReply ) * mi->mi_ntargets );
951 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
952 sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
953 mc->mc_ntargets = mi->mi_ntargets;
956 return mc->mc_candidates;
962 * Prepares the connection structure
966 * - determine what DN is being requested:
968 * op requires candidate checks
970 * add unique parent of o_req_ndn
971 * bind unique^*[/all] o_req_ndn [no check]
972 * compare unique^+ o_req_ndn
973 * delete unique o_req_ndn
974 * modify unique o_req_ndn
975 * search any o_req_ndn
976 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
978 * - for ops that require the candidate to be unique, in case of multiple
979 * occurrences an internal search with sizeLimit=1 is performed
980 * if a unique candidate can actually be determined. If none is found,
981 * the operation aborts; if multiple are found, the default target
982 * is used if defined and candidate; otherwise the operation aborts.
984 * *^note: actually, the bind operation is handled much like a search;
985 * i.e. the bind is broadcast to all candidate targets.
987 * +^note: actually, the compare operation is handled much like a search;
988 * i.e. the compare is broadcast to all candidate targets, while checking
989 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
997 ldap_back_send_t sendok )
999 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1000 metaconn_t *mc = NULL,
1002 int cached = META_TARGET_NONE,
1003 i = META_TARGET_NONE,
1009 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
1013 META_DNTYPE_NEWPARENT
1014 } dn_type = META_DNTYPE_ENTRY;
1015 struct berval ndn = op->o_req_ndn,
1018 SlapReply *candidates = meta_back_candidates_get( op );
1020 /* Internal searches are privileged and shared. So is root. */
1021 /* FIXME: there seems to be concurrency issues */
1022 if ( META_BACK_PROXYAUTHZ_ALWAYS( mi ) || op->o_do_not_cache || be_isroot( op ) ) {
1023 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
1024 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
1025 LDAP_BACK_PCONN_ROOTDN_SET( &mc_curr, op );
1028 mc_curr.mc_local_ndn = op->o_ndn;
1030 /* Explicit binds must not be shared */
1031 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
1032 mc_curr.mc_conn = op->o_conn;
1035 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1036 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1040 /* Explicit Bind requests always get their own conn */
1041 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1042 /* Searches for a metaconn in the avl tree */
1044 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1045 if ( LDAP_BACK_PCONN_ISPRIV( &mc_curr ) ) {
1046 /* lookup a conn that's not binding */
1047 LDAP_TAILQ_FOREACH( mc,
1048 &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv,
1051 if ( !LDAP_BACK_CONN_BINDING( mc ) && mc->mc_refcnt == 0 ) {
1057 if ( mc != LDAP_TAILQ_LAST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1058 metaconn_t, mc_q ) )
1060 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1062 mc->mc_q.tqe_prev = NULL;
1063 mc->mc_q.tqe_next = NULL;
1064 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1068 } else if ( !LDAP_BACK_USE_TEMPORARIES( mi )
1069 && mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_num == mi->mi_conn_priv_max )
1071 mc = LDAP_TAILQ_FIRST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv );
1076 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1077 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1081 /* catch taint errors */
1082 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1084 /* Don't reuse connections while they're still binding
1085 * NOTE: only makes sense for binds */
1086 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1087 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1088 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1090 ldap_pvt_thread_yield();
1094 /* release conn, and create a temporary */
1098 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
1099 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
1101 #if META_BACK_PRINT_CONNTREE > 0
1102 meta_back_print_conntree( mi,
1103 ">>> meta_back_getconn(expired)" );
1104 #endif /* META_BACK_PRINT_CONNTREE */
1106 /* don't let anyone else use this expired connection */
1107 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1108 if ( mc->mc_q.tqe_prev != NULL ) {
1109 assert( LDAP_BACK_CONN_CACHED( mc ) );
1110 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1111 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1113 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1114 mc->mc_q.tqe_prev = NULL;
1115 mc->mc_q.tqe_next = NULL;
1118 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1122 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1123 (caddr_t)mc, meta_back_conndnmc_cmp );
1126 #if META_BACK_PRINT_CONNTREE > 0
1127 meta_back_print_conntree( mi,
1128 "<<< meta_back_getconn(expired)" );
1129 #endif /* META_BACK_PRINT_CONNTREE */
1130 LDAP_BACK_CONN_TAINTED_SET( mc );
1131 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1133 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired (tainted).\n",
1134 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc ) );
1140 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1143 switch ( op->o_tag ) {
1145 /* if we go to selection, the entry must not exist,
1146 * and we must be able to resolve the parent */
1147 dn_type = META_DNTYPE_PARENT;
1148 dnParent( &ndn, &pndn );
1151 case LDAP_REQ_MODRDN:
1152 /* if nnewSuperior is not NULL, it must resolve
1153 * to the same candidate as the req_ndn */
1154 if ( op->orr_nnewSup ) {
1155 dn_type = META_DNTYPE_NEWPARENT;
1160 /* if bound as rootdn, the backend must bind to all targets
1161 * with the administrative identity */
1162 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
1163 op_type = META_OP_REQUIRE_ALL;
1167 case LDAP_REQ_COMPARE:
1168 case LDAP_REQ_DELETE:
1169 case LDAP_REQ_MODIFY:
1170 /* just a unique candidate */
1173 case LDAP_REQ_SEARCH:
1174 /* allow multiple candidates for the searchBase */
1175 op_type = META_OP_ALLOW_MULTIPLE;
1179 /* right now, just break (exop?) */
1184 * require all connections ...
1186 if ( op_type == META_OP_REQUIRE_ALL ) {
1188 /* Looks like we didn't get a bind. Open a new session... */
1190 assert( new_conn == 0 );
1191 mc = metaconn_alloc( op );
1192 mc->mc_conn = mc_curr.mc_conn;
1193 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1195 if ( sendok & LDAP_BACK_BINDING ) {
1196 LDAP_BACK_CONN_BINDING_SET( mc );
1198 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1199 LDAP_BACK_CONN_ISPRIV_SET( mc );
1201 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1202 LDAP_BACK_CONN_ISANON_SET( mc );
1206 /* TODO: if any of the connections is binding,
1207 * release mc and create a new one */
1210 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1212 * The target is activated; if needed, it is
1215 candidates[ i ].sr_err = meta_back_init_one_conn( op,
1216 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1217 LDAP_BACK_DONTSEND, !new_conn );
1218 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1219 META_CANDIDATE_SET( &candidates[ i ] );
1225 * FIXME: in case one target cannot
1226 * be init'd, should the other ones
1229 META_CANDIDATE_RESET( &candidates[ i ] );
1230 err = candidates[ i ].sr_err;
1235 if ( ncandidates == 0 ) {
1238 meta_back_conn_free( mc );
1241 meta_back_release_conn( op, mc );
1244 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1245 rs->sr_text = "Unable to select valid candidates";
1247 if ( sendok & LDAP_BACK_SENDERR ) {
1248 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1249 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1251 send_ldap_result( op, rs );
1253 rs->sr_matched = NULL;
1263 * looks in cache, if any
1265 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1266 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1269 if ( op_type == META_OP_REQUIRE_SINGLE ) {
1270 metatarget_t *mt = NULL;
1271 metasingleconn_t *msc = NULL;
1275 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1276 META_CANDIDATE_RESET( &candidates[ j ] );
1280 * tries to get a unique candidate
1281 * (takes care of default target)
1283 if ( i == META_TARGET_NONE ) {
1284 i = meta_back_get_candidate( op, rs, &ndn );
1286 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1287 i = meta_back_get_candidate( op, rs, &pndn );
1290 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1292 meta_back_release_conn( op, mc );
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;
1299 send_ldap_result( op, rs );
1301 rs->sr_matched = NULL;
1308 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1311 meta_back_release_conn( op, mc );
1314 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1315 rs->sr_text = "cross-target rename not supported";
1316 if ( sendok & LDAP_BACK_SENDERR ) {
1317 send_ldap_result( op, rs );
1324 Debug( LDAP_DEBUG_TRACE,
1325 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1326 i, op->o_req_ndn.bv_val, 0 );
1329 /* Retries searching for a metaconn in the avl tree
1330 * the reason is that the connection might have been
1331 * created by meta_back_get_candidate() */
1332 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1334 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1335 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1336 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1338 /* catch taint errors */
1339 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1341 /* Don't reuse connections while they're still binding */
1342 if ( META_BACK_CONN_CREATING( &mc->mc_conns[ i ] )
1343 || LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) )
1345 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1346 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1347 ldap_pvt_thread_yield();
1357 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1360 /* Looks like we didn't get a bind. Open a new session... */
1362 assert( new_conn == 0 );
1363 mc = metaconn_alloc( op );
1364 mc->mc_conn = mc_curr.mc_conn;
1365 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1367 if ( sendok & LDAP_BACK_BINDING ) {
1368 LDAP_BACK_CONN_BINDING_SET( mc );
1370 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1371 LDAP_BACK_CONN_ISPRIV_SET( mc );
1373 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1374 LDAP_BACK_CONN_ISANON_SET( mc );
1380 * Clear all other candidates
1382 ( void )meta_clear_unused_candidates( op, i );
1384 mt = mi->mi_targets[ i ];
1385 msc = &mc->mc_conns[ i ];
1388 * The target is activated; if needed, it is
1389 * also init'd. In case of error, meta_back_init_one_conn
1390 * sends the appropriate result.
1392 err = meta_back_init_one_conn( op, rs, mc, i,
1393 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn );
1394 if ( err != LDAP_SUCCESS ) {
1396 * FIXME: in case one target cannot
1397 * be init'd, should the other ones
1400 META_CANDIDATE_RESET( &candidates[ i ] );
1403 meta_back_conn_free( mc );
1406 meta_back_release_conn( op, mc );
1411 candidates[ i ].sr_err = LDAP_SUCCESS;
1412 META_CANDIDATE_SET( &candidates[ i ] );
1420 * if no unique candidate ...
1424 /* Looks like we didn't get a bind. Open a new session... */
1426 assert( new_conn == 0 );
1427 mc = metaconn_alloc( op );
1428 mc->mc_conn = mc_curr.mc_conn;
1429 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1431 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1432 LDAP_BACK_CONN_ISPRIV_SET( mc );
1434 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1435 LDAP_BACK_CONN_ISANON_SET( mc );
1439 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1440 metatarget_t *mt = mi->mi_targets[ i ];
1442 META_CANDIDATE_RESET( &candidates[ i ] );
1445 || meta_back_is_candidate( mt, &op->o_req_ndn,
1446 LDAP_SCOPE_SUBTREE ) )
1450 * The target is activated; if needed, it is
1453 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1454 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1455 LDAP_BACK_DONTSEND, !new_conn );
1456 candidates[ i ].sr_err = lerr;
1457 if ( lerr == LDAP_SUCCESS ) {
1458 META_CANDIDATE_SET( &candidates[ i ] );
1461 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1462 op->o_log_prefix, i, 0 );
1464 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1465 META_CANDIDATE_SET( &candidates[ i ] );
1467 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1468 op->o_log_prefix, i,
1469 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1474 * FIXME: in case one target cannot
1475 * be init'd, should the other ones
1479 ( void )meta_clear_one_candidate( op, mc, i );
1481 /* leave the target candidate, but record the error for later use */
1484 if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1485 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined err=%d\n",
1486 op->o_log_prefix, i, lerr );
1489 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed err=%d\n",
1490 op->o_log_prefix, i, lerr );
1493 if ( META_BACK_ONERR_STOP( mi ) ) {
1494 if ( sendok & LDAP_BACK_SENDERR ) {
1495 send_ldap_result( op, rs );
1500 meta_back_conn_free( mc );
1503 meta_back_release_conn( op, mc );
1515 ( void )meta_clear_one_candidate( op, mc, i );
1520 if ( ncandidates == 0 ) {
1523 meta_back_conn_free( mc );
1526 meta_back_release_conn( op, mc );
1529 if ( rs->sr_err == LDAP_SUCCESS ) {
1530 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1531 rs->sr_text = "Unable to select valid candidates";
1534 if ( sendok & LDAP_BACK_SENDERR ) {
1535 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1536 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1538 send_ldap_result( op, rs );
1540 rs->sr_matched = NULL;
1548 /* clear out meta_back_init_one_conn non-fatal errors */
1549 rs->sr_err = LDAP_SUCCESS;
1552 /* touch the timestamp */
1553 if ( mi->mi_idle_timeout != 0 ) {
1554 mc->mc_time = op->o_time;
1558 if ( mi->mi_conn_ttl ) {
1559 mc->mc_create_time = op->o_time;
1563 * Inserts the newly created metaconn in the avl tree
1565 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1566 #if META_BACK_PRINT_CONNTREE > 0
1567 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1568 #endif /* META_BACK_PRINT_CONNTREE */
1570 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1571 if ( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num < mi->mi_conn_priv_max ) {
1572 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1573 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num++;
1574 LDAP_BACK_CONN_CACHED_SET( mc );
1577 LDAP_BACK_CONN_TAINTED_SET( mc );
1582 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1583 meta_back_conndn_cmp, meta_back_conndn_dup );
1584 LDAP_BACK_CONN_CACHED_SET( mc );
1587 #if META_BACK_PRINT_CONNTREE > 0
1588 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1589 #endif /* META_BACK_PRINT_CONNTREE */
1590 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1592 if ( !LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1594 * Err could be -1 in case a duplicate metaconn is inserted
1601 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1602 /* duplicate: free and try to get the newly created one */
1603 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1605 meta_back_conn_free( mc );
1611 LDAP_BACK_CONN_TAINTED_SET( mc );
1615 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1616 Debug( LDAP_DEBUG_ANY,
1617 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1618 op->o_log_prefix, ncandidates,
1619 LDAP_BACK_PCONN_ID( mc ) );
1622 meta_back_conn_free( mc );
1624 rs->sr_err = LDAP_OTHER;
1625 rs->sr_text = "proxy bind collision";
1626 if ( sendok & LDAP_BACK_SENDERR ) {
1627 send_ldap_result( op, rs );
1634 Debug( LDAP_DEBUG_TRACE,
1635 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1636 op->o_log_prefix, ncandidates,
1637 LDAP_BACK_PCONN_ID( mc ) );
1640 Debug( LDAP_DEBUG_TRACE,
1641 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1642 op->o_log_prefix, ncandidates,
1643 LDAP_BACK_PCONN_ID( mc ) );
1650 meta_back_release_conn_lock(
1655 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1657 assert( mc != NULL );
1660 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1662 assert( mc->mc_refcnt > 0 );
1664 /* NOTE: the connection is removed if either it is tainted
1665 * or if it is shared and no one else is using it. This needs
1666 * to occur because for intrinsic reasons cached connections
1667 * that are not privileged would live forever and pollute
1668 * the connection space (and eat up resources). Maybe this
1669 * should be configurable... */
1670 if ( LDAP_BACK_CONN_TAINTED( mc ) ) {
1671 Debug( LDAP_DEBUG_TRACE, "%s meta_back_release_conn: mc=%p conn=%ld tainted.\n",
1672 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc ) );
1673 #if META_BACK_PRINT_CONNTREE > 0
1674 meta_back_print_conntree( mi, ">>> meta_back_release_conn" );
1675 #endif /* META_BACK_PRINT_CONNTREE */
1677 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1678 if ( mc->mc_q.tqe_prev != NULL ) {
1679 assert( LDAP_BACK_CONN_CACHED( mc ) );
1680 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1681 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1682 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1685 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1687 mc->mc_q.tqe_prev = NULL;
1688 mc->mc_q.tqe_next = NULL;
1693 tmpmc = avl_delete( &mi->mi_conninfo.lai_tree,
1694 ( caddr_t )mc, meta_back_conndnmc_cmp );
1696 if ( tmpmc == NULL ) {
1697 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_release_conn: unable to find mc=%p\n",
1698 op->o_log_prefix, (void *)mc, 0 );
1700 assert( tmpmc == mc );
1704 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1706 #if META_BACK_PRINT_CONNTREE > 0
1707 meta_back_print_conntree( mi, "<<< meta_back_release_conn" );
1708 #endif /* META_BACK_PRINT_CONNTREE */
1710 if ( mc->mc_refcnt == 0 ) {
1711 meta_back_conn_free( mc );
1716 if ( mc != NULL && LDAP_BACK_CONN_BINDING( mc ) ) {
1717 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1721 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1726 meta_back_quarantine(
1731 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1732 metatarget_t *mt = mi->mi_targets[ candidate ];
1734 slap_retry_info_t *ri = &mt->mt_quarantine;
1736 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1738 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1739 time_t new_last = slap_get_time();
1741 switch ( mt->mt_isquarantined ) {
1742 case LDAP_BACK_FQ_NO:
1743 if ( ri->ri_last == new_last ) {
1747 Debug( LDAP_DEBUG_ANY,
1748 "%s meta_back_quarantine[%d]: enter.\n",
1749 op->o_log_prefix, candidate, 0 );
1755 case LDAP_BACK_FQ_RETRYING:
1756 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1757 char buf[ SLAP_TEXT_BUFLEN ];
1759 snprintf( buf, sizeof( buf ),
1760 "meta_back_quarantine[%d]: block #%d try #%d failed",
1761 candidate, ri->ri_idx, ri->ri_count );
1762 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1763 op->o_log_prefix, buf, 0 );
1767 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1768 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1779 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1780 ri->ri_last = new_last;
1782 } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1783 Debug( LDAP_DEBUG_ANY,
1784 "%s meta_back_quarantine[%d]: exit.\n",
1785 op->o_log_prefix, candidate, 0 );
1787 if ( mi->mi_quarantine_f ) {
1788 (void)mi->mi_quarantine_f( mi, candidate,
1789 mi->mi_quarantine_p );
1794 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1798 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );