2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2009 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_print( metaconn_t *mc, char *avlstr )
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);
151 fputc( ']', stderr );
153 fprintf( stderr, " mc=%p local=\"%s\" conn=%p refcnt=%d%s %s\n",
155 mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "",
158 LDAP_BACK_CONN_TAINTED( mc ) ? " tainted" : "",
163 meta_back_ravl_print( Avlnode *root, int depth )
171 meta_back_ravl_print( root->avl_right, depth + 1 );
173 for ( i = 0; i < depth; i++ ) {
174 fprintf( stderr, "-" );
176 fputc( ' ', stderr );
178 meta_back_print( (metaconn_t *)root->avl_data,
179 avl_bf2str( root->avl_bf ) );
181 meta_back_ravl_print( root->avl_left, depth + 1 );
184 /* NOTE: duplicate from back-ldap/bind.c */
185 static char* priv2str[] = {
196 meta_back_print_conntree( metainfo_t *mi, char *msg )
200 fprintf( stderr, "========> %s\n", msg );
202 for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
206 fprintf( stderr, " %s[%d]\n", priv2str[ c ], mi->mi_conn_priv[ c ].mic_num );
208 LDAP_TAILQ_FOREACH( mc, &mi->mi_conn_priv[ c ].mic_priv, mc_q )
210 fprintf( stderr, " [%d] ", i );
211 meta_back_print( mc, "" );
216 if ( mi->mi_conninfo.lai_tree == NULL ) {
217 fprintf( stderr, "\t(empty)\n" );
220 meta_back_ravl_print( mi->mi_conninfo.lai_tree, 0 );
223 fprintf( stderr, "<======== %s\n", msg );
225 #endif /* META_BACK_PRINT_CONNTREE */
233 * Allocates a connection structure, making room for all the referenced targets
239 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
241 int ntargets = mi->mi_ntargets;
243 assert( ntargets > 0 );
245 /* malloc all in one */
246 mc = ( metaconn_t * )ch_calloc( 1, sizeof( metaconn_t )
247 + sizeof( metasingleconn_t ) * ( ntargets - 1 ) );
254 mc->mc_authz_target = META_BOUND_NONE;
261 * meta_back_init_one_conn
263 * Initializes one connection
266 meta_back_init_one_conn(
272 ldap_back_send_t sendok,
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 ];
280 int isauthz = ( candidate == mc->mc_authz_target );
284 #endif /* HAVE_TLS */
286 /* if the server is quarantined, and
287 * - the current interval did not expire yet, or
288 * - no more retries should occur,
289 * don't return the connection */
290 if ( mt->mt_isquarantined ) {
291 slap_retry_info_t *ri = &mt->mt_quarantine;
294 if ( mt->mt_quarantine.ri_interval ) {
295 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
296 dont_retry = ( mt->mt_isquarantined > LDAP_BACK_FQ_NO );
298 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
299 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
301 if ( LogTest( LDAP_DEBUG_ANY ) ) {
302 char buf[ SLAP_TEXT_BUFLEN ];
304 snprintf( buf, sizeof( buf ),
305 "meta_back_init_one_conn[%d]: quarantine "
306 "retry block #%d try #%d",
307 candidate, ri->ri_idx, ri->ri_count );
308 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
309 op->o_log_prefix, buf, 0 );
312 mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
316 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
320 rs->sr_err = LDAP_UNAVAILABLE;
321 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
322 rs->sr_text = "Target is quarantined";
323 send_ldap_result( op, rs );
331 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
337 if ( LDAP_BACK_CONN_ISBOUND( msc )
338 || LDAP_BACK_CONN_ISANON( msc ) )
340 assert( msc->msc_ld != NULL );
341 rs->sr_err = LDAP_SUCCESS;
344 } else if ( META_BACK_CONN_CREATING( msc )
345 || LDAP_BACK_CONN_BINDING( msc ) )
347 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
349 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
352 ldap_pvt_thread_yield();
356 /* sounds more appropriate */
357 rs->sr_err = LDAP_BUSY;
358 rs->sr_text = "No connections to target are available";
361 } else if ( META_BACK_CONN_INITED( msc ) ) {
362 assert( msc->msc_ld != NULL );
363 rs->sr_err = LDAP_SUCCESS;
370 META_BACK_CONN_CREATING_SET( msc );
374 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
378 if ( rs->sr_err != LDAP_SUCCESS
380 && ( sendok & LDAP_BACK_SENDERR ) )
382 send_ldap_result( op, rs );
388 assert( msc->msc_ld == NULL );
391 * Attempts to initialize the connection to the target ds
393 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
394 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
396 is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
397 #endif /* HAVE_TLS */
398 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
399 if ( rs->sr_err != LDAP_SUCCESS ) {
404 * Set LDAP version. This will always succeed: If the client
405 * bound with a particular version, then so can we.
407 if ( mt->mt_version != 0 ) {
408 version = mt->mt_version;
410 } else if ( op->o_conn->c_protocol != 0 ) {
411 version = op->o_conn->c_protocol;
414 version = LDAP_VERSION3;
416 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
417 ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
419 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
420 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
421 META_BACK_TGT_CHASE_REFERRALS( mt ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
424 /* start TLS ("tls [try-]{start|propagate}" statement) */
425 if ( ( META_BACK_TGT_USE_TLS( mt )
426 || ( op->o_conn->c_is_tls
427 && META_BACK_TGT_PROPAGATE_TLS( mt ) ) )
430 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
432 * use asynchronous StartTLS; in case, chase referral
433 * FIXME: OpenLDAP does not return referral on StartTLS yet
437 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
438 if ( rs->sr_err == LDAP_SUCCESS ) {
439 LDAPMessage *res = NULL;
440 int rc, nretries = mt->mt_nretries;
443 LDAP_BACK_TV_SET( &tv );
446 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
449 rs->sr_err = LDAP_OTHER;
453 if ( nretries != 0 ) {
454 if ( nretries > 0 ) {
457 LDAP_BACK_TV_SET( &tv );
460 rs->sr_err = LDAP_OTHER;
464 /* only touch when activity actually took place... */
465 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
466 msc->msc_time = op->o_time;
471 if ( rc == LDAP_RES_EXTENDED ) {
472 struct berval *data = NULL;
474 /* NOTE: right now, data is unused, so don't get it */
475 rs->sr_err = ldap_parse_extended_result( msc->msc_ld,
476 res, NULL, NULL /* &data */ , 0 );
477 if ( rs->sr_err == LDAP_SUCCESS ) {
480 /* FIXME: matched? referrals? response controls? */
481 rs->sr_err = ldap_parse_result( msc->msc_ld,
482 res, &err, NULL, NULL, NULL, NULL, 1 );
485 if ( rs->sr_err == LDAP_SUCCESS ) {
489 /* FIXME: in case a referral
490 * is returned, should we try
491 * using it instead of the
493 if ( rs->sr_err == LDAP_SUCCESS ) {
494 ldap_install_tls( msc->msc_ld );
496 } else if ( rs->sr_err == LDAP_REFERRAL ) {
497 /* FIXME: LDAP_OPERATIONS_ERROR? */
498 rs->sr_err = LDAP_OTHER;
499 rs->sr_text = "Unwilling to chase referral "
500 "returned by Start TLS exop";
509 rs->sr_err = LDAP_OTHER;
516 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
518 * use synchronous StartTLS
520 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
521 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
523 /* if StartTLS is requested, only attempt it if the URL
524 * is not "ldaps://"; this may occur not only in case
525 * of misconfiguration, but also when used in the chain
526 * overlay, where the "uri" can be parsed out of a referral */
527 if ( rs->sr_err == LDAP_SERVER_DOWN
528 || ( rs->sr_err != LDAP_SUCCESS
529 && META_BACK_TGT_TLS_CRITICAL( mt ) ) )
533 Debug( LDAP_DEBUG_ANY,
534 "### %s meta_back_init_one_conn(TLS) "
535 "ldap_unbind_ext[%d] ld=%p\n",
536 op->o_log_prefix, candidate,
537 (void *)msc->msc_ld );
538 #endif /* DEBUG_205 */
540 /* need to trash a failed Start TLS */
541 meta_clear_one_candidate( op, mc, candidate );
545 #endif /* HAVE_TLS */
548 * Set the network timeout if set
550 if ( mt->mt_network_timeout != 0 ) {
551 struct timeval network_timeout;
553 network_timeout.tv_usec = 0;
554 network_timeout.tv_sec = mt->mt_network_timeout;
556 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
557 (void *)&network_timeout );
561 * If the connection DN is not null, an attempt to rewrite it is made
565 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
566 ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
567 if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
568 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
569 memset( msc->msc_cred.bv_val, 0,
570 msc->msc_cred.bv_len );
572 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
576 ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
580 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
581 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
582 ber_memfree_x( msc->msc_cred.bv_val, NULL );
583 BER_BVZERO( &msc->msc_cred );
585 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
586 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
587 BER_BVZERO( &msc->msc_bound_ndn );
589 if ( !BER_BVISEMPTY( &op->o_ndn )
590 && SLAP_IS_AUTHZ_BACKEND( op )
594 dc.conn = op->o_conn;
599 * Rewrite the bind dn if needed
601 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
602 &msc->msc_bound_ndn ) )
606 Debug( LDAP_DEBUG_ANY,
607 "### %s meta_back_init_one_conn(rewrite) "
608 "ldap_unbind_ext[%d] ld=%p\n",
609 op->o_log_prefix, candidate,
610 (void *)msc->msc_ld );
611 #endif /* DEBUG_205 */
613 /* need to trash a connection not fully established */
614 meta_clear_one_candidate( op, mc, candidate );
618 /* copy the DN if needed */
619 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
620 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
623 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
626 ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
630 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
634 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
636 META_BACK_CONN_CREATING_CLEAR( msc );
637 if ( rs->sr_err == LDAP_SUCCESS ) {
639 * Sets a cookie for the rewrite session
641 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
642 META_BACK_CONN_INITED_SET( msc );
645 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
648 if ( rs->sr_err != LDAP_SUCCESS ) {
649 rs->sr_err = slap_map_api2result( rs );
650 if ( sendok & LDAP_BACK_SENDERR ) {
651 send_ldap_result( op, rs );
661 * Retries one connection
669 ldap_back_send_t sendok )
671 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
672 metatarget_t *mt = mi->mi_targets[ candidate ];
673 metaconn_t *mc = *mcp;
674 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
675 int rc = LDAP_UNAVAILABLE,
679 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
681 assert( !META_BACK_CONN_CREATING( msc ) );
682 binding = LDAP_BACK_CONN_BINDING( msc );
683 LDAP_BACK_CONN_BINDING_CLEAR( msc );
685 assert( mc->mc_refcnt > 0 );
686 if ( mc->mc_refcnt == 1 ) {
687 if ( LogTest( LDAP_DEBUG_ANY ) ) {
688 char buf[ SLAP_TEXT_BUFLEN ];
690 /* this lock is required; however,
691 * it's invoked only when logging is on */
692 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
693 snprintf( buf, sizeof( buf ),
694 "retrying URI=\"%s\" DN=\"%s\"",
696 BER_BVISNULL( &msc->msc_bound_ndn ) ?
697 "" : msc->msc_bound_ndn.bv_val );
698 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
700 Debug( LDAP_DEBUG_ANY,
701 "%s meta_back_retry[%d]: %s.\n",
702 op->o_log_prefix, candidate, buf );
705 meta_clear_one_candidate( op, mc, candidate );
706 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
708 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
710 /* mc here must be the regular mc, reset and ready for init */
711 rc = meta_back_init_one_conn( op, rs, mc, candidate,
712 LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 );
714 /* restore the "binding" flag, in case */
716 LDAP_BACK_CONN_BINDING_SET( msc );
719 if ( rc == LDAP_SUCCESS ) {
721 rc = meta_back_single_dobind( op, rs, mcp, candidate,
722 sendok, mt->mt_nretries, 0 );
724 Debug( LDAP_DEBUG_ANY,
725 "%s meta_back_retry[%d]: "
726 "meta_back_single_dobind=%d\n",
727 op->o_log_prefix, candidate, rc );
728 if ( rc == LDAP_SUCCESS ) {
729 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
730 !BER_BVISEMPTY( &msc->msc_bound_ndn ) )
732 LDAP_BACK_CONN_ISBOUND_SET( msc );
735 LDAP_BACK_CONN_ISANON_SET( msc );
738 /* when bound, dispose of the "binding" flag */
740 LDAP_BACK_CONN_BINDING_CLEAR( msc );
745 /* don't send twice */
746 sendok &= ~LDAP_BACK_SENDERR;
749 if ( rc != LDAP_SUCCESS ) {
750 SlapReply *candidates = meta_back_candidates_get( op );
752 candidates[ candidate ].sr_err = rc;
754 if ( *mcp != NULL ) {
755 if ( mc->mc_refcnt == 1 ) {
757 LDAP_BACK_CONN_BINDING_CLEAR( msc );
759 (void)meta_clear_one_candidate( op, mc, candidate );
762 LDAP_BACK_CONN_TAINTED_SET( mc );
763 /* only release if mandatory; otherwise
764 * let the caller do what's best before
766 if ( META_BACK_ONERR_STOP( mi ) ) {
767 meta_back_release_conn_lock( mi, mc, 0 );
771 #if META_BACK_PRINT_CONNTREE > 0
772 meta_back_print_conntree( mi, ">>> meta_back_retry" );
773 #endif /* META_BACK_PRINT_CONNTREE */
775 /* FIXME: could be done better, reworking meta_back_release_conn_lock() */
776 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
777 if ( mc->mc_q.tqe_prev != NULL ) {
778 assert( LDAP_BACK_CONN_CACHED( mc ) );
779 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
780 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
782 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
783 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
786 assert( !LDAP_BACK_CONN_CACHED( mc ) );
790 /* FIXME: check if in tree, for consistency? */
791 (void)avl_delete( &mi->mi_conninfo.lai_tree,
792 ( caddr_t )mc, meta_back_conndnmc_cmp );
794 LDAP_BACK_CONN_CACHED_CLEAR( mc );
796 #if META_BACK_PRINT_CONNTREE > 0
797 meta_back_print_conntree( mi, "<<< meta_back_retry" );
798 #endif /* META_BACK_PRINT_CONNTREE */
802 if ( sendok & LDAP_BACK_SENDERR ) {
804 rs->sr_text = "Unable to retry";
805 send_ldap_result( op, rs );
809 if ( quarantine && META_BACK_TGT_QUARANTINE( mt ) ) {
810 meta_back_quarantine( op, rs, candidate );
813 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
815 return rc == LDAP_SUCCESS ? 1 : 0;
819 * callback for unique candidate selection
822 meta_back_conn_cb( Operation *op, SlapReply *rs )
824 assert( op->o_tag == LDAP_REQ_SEARCH );
826 switch ( rs->sr_type ) {
828 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
844 meta_back_get_candidate(
849 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
853 * tries to get a unique candidate
854 * (takes care of default target)
856 candidate = meta_back_select_unique_candidate( mi, ndn );
859 * if any is found, inits the connection
861 if ( candidate == META_TARGET_NONE ) {
862 rs->sr_err = LDAP_NO_SUCH_OBJECT;
863 rs->sr_text = "No suitable candidate target found";
865 } else if ( candidate == META_TARGET_MULTIPLE ) {
867 SlapReply rs2 = { 0 };
868 slap_callback cb2 = { 0 };
871 /* try to get a unique match for the request ndn
872 * among the multiple candidates available */
873 op2.o_tag = LDAP_REQ_SEARCH;
875 op2.o_req_ndn = *ndn;
876 op2.ors_scope = LDAP_SCOPE_BASE;
877 op2.ors_deref = LDAP_DEREF_NEVER;
878 op2.ors_attrs = slap_anlist_no_attrs;
879 op2.ors_attrsonly = 0;
880 op2.ors_limit = NULL;
882 op2.ors_tlimit = SLAP_NO_LIMIT;
884 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
885 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
887 op2.o_callback = &cb2;
888 cb2.sc_response = meta_back_conn_cb;
889 cb2.sc_private = (void *)&candidate;
891 rc = op->o_bd->be_search( &op2, &rs2 );
893 switch ( rs2.sr_err ) {
896 rs->sr_err = rs2.sr_err;
899 case LDAP_SIZELIMIT_EXCEEDED:
900 /* if multiple candidates can serve the operation,
901 * and a default target is defined, and it is
902 * a candidate, try using it (FIXME: YMMV) */
903 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
904 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
905 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
907 candidate = mi->mi_defaulttarget;
908 rs->sr_err = LDAP_SUCCESS;
912 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
913 rs->sr_text = "Unable to select unique candidate target";
919 rs->sr_err = LDAP_SUCCESS;
925 static void *meta_back_candidates_dummy;
928 meta_back_candidates_keyfree(
932 metacandidates_t *mc = (metacandidates_t *)data;
934 ber_memfree_x( mc->mc_candidates, NULL );
935 ber_memfree_x( data, NULL );
939 meta_back_candidates_get( Operation *op )
941 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
942 metacandidates_t *mc;
944 if ( op->o_threadctx ) {
947 ldap_pvt_thread_pool_getkey( op->o_threadctx,
948 &meta_back_candidates_dummy, &data, NULL );
949 mc = (metacandidates_t *)data;
952 mc = mi->mi_candidates;
956 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
957 mc->mc_ntargets = mi->mi_ntargets;
958 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
959 if ( op->o_threadctx ) {
963 ldap_pvt_thread_pool_setkey( op->o_threadctx,
964 &meta_back_candidates_dummy, data,
965 meta_back_candidates_keyfree,
969 mi->mi_candidates = mc;
972 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
973 /* NOTE: in the future, may want to allow back-config
974 * to add/remove targets from back-meta... */
975 mc->mc_candidates = ch_realloc( mc->mc_candidates,
976 sizeof( SlapReply ) * mi->mi_ntargets );
977 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
978 sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
979 mc->mc_ntargets = mi->mi_ntargets;
982 return mc->mc_candidates;
988 * Prepares the connection structure
992 * - determine what DN is being requested:
994 * op requires candidate checks
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]
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.
1010 * *^note: actually, the bind operation is handled much like a search;
1011 * i.e. the bind is broadcast to all candidate targets.
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
1023 ldap_back_send_t sendok )
1025 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1026 metaconn_t *mc = NULL,
1028 int cached = META_TARGET_NONE,
1029 i = META_TARGET_NONE,
1035 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
1039 META_DNTYPE_NEWPARENT
1040 } dn_type = META_DNTYPE_ENTRY;
1041 struct berval ndn = op->o_req_ndn,
1044 SlapReply *candidates = meta_back_candidates_get( op );
1046 /* Internal searches are privileged and shared. So is root. */
1047 if ( ( !BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ALWAYS( mi ) )
1048 || ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ANON( mi ) )
1049 || op->o_do_not_cache || be_isroot( op ) )
1051 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
1052 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
1053 LDAP_BACK_PCONN_ROOTDN_SET( &mc_curr, op );
1055 } else if ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_NOANON( mi ) )
1057 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1058 BER_BVSTR( &mc_curr.mc_local_ndn, "" );
1059 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1062 mc_curr.mc_local_ndn = op->o_ndn;
1064 /* Explicit binds must not be shared */
1065 if ( !BER_BVISEMPTY( &op->o_ndn )
1066 || op->o_tag == LDAP_REQ_BIND
1067 || SLAP_IS_AUTHZ_BACKEND( op ) )
1069 mc_curr.mc_conn = op->o_conn;
1072 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1073 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1077 /* Explicit Bind requests always get their own conn */
1078 if ( sendok & LDAP_BACK_BINDING ) {
1079 mc_curr.mc_conn = op->o_conn;
1082 /* Searches for a metaconn in the avl tree */
1084 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1085 if ( LDAP_BACK_PCONN_ISPRIV( &mc_curr ) ) {
1086 /* lookup a conn that's not binding */
1087 LDAP_TAILQ_FOREACH( mc,
1088 &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv,
1091 if ( !LDAP_BACK_CONN_BINDING( mc ) && mc->mc_refcnt == 0 ) {
1097 if ( mc != LDAP_TAILQ_LAST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1098 metaconn_t, mc_q ) )
1100 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1102 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1103 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
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 )
1110 mc = LDAP_TAILQ_FIRST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv );
1115 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1116 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1120 /* catch taint errors */
1121 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
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 );
1129 ldap_pvt_thread_yield();
1133 /* release conn, and create a temporary */
1137 if ( ( 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 ) )
1140 #if META_BACK_PRINT_CONNTREE > 0
1141 meta_back_print_conntree( mi,
1142 ">>> meta_back_getconn(expired)" );
1143 #endif /* META_BACK_PRINT_CONNTREE */
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,
1152 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1153 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1156 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1160 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1161 (caddr_t)mc, meta_back_conndnmc_cmp );
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 );
1171 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired (tainted).\n",
1172 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc ) );
1178 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1181 switch ( op->o_tag ) {
1183 /* if we go to selection, the entry must not exist,
1184 * and we must be able to resolve the parent */
1185 dn_type = META_DNTYPE_PARENT;
1186 dnParent( &ndn, &pndn );
1189 case LDAP_REQ_MODRDN:
1190 /* if nnewSuperior is not NULL, it must resolve
1191 * to the same candidate as the req_ndn */
1192 if ( op->orr_nnewSup ) {
1193 dn_type = META_DNTYPE_NEWPARENT;
1198 /* if bound as rootdn, the backend must bind to all targets
1199 * with the administrative identity
1200 * (unless pseoudoroot-bind-defer is TRUE) */
1201 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
1202 op_type = META_OP_REQUIRE_ALL;
1206 case LDAP_REQ_COMPARE:
1207 case LDAP_REQ_DELETE:
1208 case LDAP_REQ_MODIFY:
1209 /* just a unique candidate */
1212 case LDAP_REQ_SEARCH:
1213 /* allow multiple candidates for the searchBase */
1214 op_type = META_OP_ALLOW_MULTIPLE;
1218 /* right now, just break (exop?) */
1223 * require all connections ...
1225 if ( op_type == META_OP_REQUIRE_ALL ) {
1227 /* Looks like we didn't get a bind. Open a new session... */
1229 assert( new_conn == 0 );
1230 mc = metaconn_alloc( op );
1231 mc->mc_conn = mc_curr.mc_conn;
1232 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1234 if ( sendok & LDAP_BACK_BINDING ) {
1235 LDAP_BACK_CONN_BINDING_SET( mc );
1237 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1238 LDAP_BACK_CONN_ISPRIV_SET( mc );
1240 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1241 LDAP_BACK_CONN_ISANON_SET( mc );
1245 /* TODO: if any of the connections is binding,
1246 * release mc and create a new one */
1249 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1251 * The target is activated; if needed, it is
1254 candidates[ i ].sr_err = meta_back_init_one_conn( op,
1255 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1256 LDAP_BACK_DONTSEND, !new_conn );
1257 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1258 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
1259 LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
1261 META_CANDIDATE_SET( &candidates[ i ] );
1267 * FIXME: in case one target cannot
1268 * be init'd, should the other ones
1271 META_CANDIDATE_RESET( &candidates[ i ] );
1272 err = candidates[ i ].sr_err;
1277 if ( ncandidates == 0 ) {
1280 meta_back_conn_free( mc );
1283 meta_back_release_conn( mi, mc );
1286 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1287 rs->sr_text = "Unable to select valid candidates";
1289 if ( sendok & LDAP_BACK_SENDERR ) {
1290 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1291 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1293 send_ldap_result( op, rs );
1294 rs->sr_matched = NULL;
1304 * looks in cache, if any
1306 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1307 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1310 if ( op_type == META_OP_REQUIRE_SINGLE ) {
1311 metatarget_t *mt = NULL;
1312 metasingleconn_t *msc = NULL;
1316 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1317 META_CANDIDATE_RESET( &candidates[ j ] );
1321 * tries to get a unique candidate
1322 * (takes care of default target)
1324 if ( i == META_TARGET_NONE ) {
1325 i = meta_back_get_candidate( op, rs, &ndn );
1327 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1328 i = meta_back_get_candidate( op, rs, &pndn );
1331 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1333 meta_back_release_conn( mi, mc );
1336 if ( sendok & LDAP_BACK_SENDERR ) {
1337 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1338 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1340 send_ldap_result( op, rs );
1341 rs->sr_matched = NULL;
1348 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1351 meta_back_release_conn( mi, mc );
1354 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1355 rs->sr_text = "Cross-target rename not supported";
1356 if ( sendok & LDAP_BACK_SENDERR ) {
1357 send_ldap_result( op, rs );
1363 Debug( LDAP_DEBUG_TRACE,
1364 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1365 i, op->o_req_ndn.bv_val, 0 );
1368 /* Retries searching for a metaconn in the avl tree
1369 * the reason is that the connection might have been
1370 * created by meta_back_get_candidate() */
1371 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1373 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1374 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1375 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1377 /* catch taint errors */
1378 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1380 /* Don't reuse connections while they're still binding */
1381 if ( META_BACK_CONN_CREATING( &mc->mc_conns[ i ] )
1382 || LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) )
1384 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1385 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1386 ldap_pvt_thread_yield();
1396 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1399 /* Looks like we didn't get a bind. Open a new session... */
1401 assert( new_conn == 0 );
1402 mc = metaconn_alloc( op );
1403 mc->mc_conn = mc_curr.mc_conn;
1404 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1406 if ( sendok & LDAP_BACK_BINDING ) {
1407 LDAP_BACK_CONN_BINDING_SET( mc );
1409 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1410 LDAP_BACK_CONN_ISPRIV_SET( mc );
1412 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1413 LDAP_BACK_CONN_ISANON_SET( mc );
1419 * Clear all other candidates
1421 ( void )meta_clear_unused_candidates( op, i );
1423 mt = mi->mi_targets[ i ];
1424 msc = &mc->mc_conns[ i ];
1427 * The target is activated; if needed, it is
1428 * also init'd. In case of error, meta_back_init_one_conn
1429 * sends the appropriate result.
1431 err = meta_back_init_one_conn( op, rs, mc, i,
1432 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn );
1433 if ( err != LDAP_SUCCESS ) {
1435 * FIXME: in case one target cannot
1436 * be init'd, should the other ones
1439 META_CANDIDATE_RESET( &candidates[ i ] );
1442 meta_back_conn_free( mc );
1445 meta_back_release_conn( mi, mc );
1450 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
1451 LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
1454 candidates[ i ].sr_err = LDAP_SUCCESS;
1455 META_CANDIDATE_SET( &candidates[ i ] );
1463 * if no unique candidate ...
1467 /* Looks like we didn't get a bind. Open a new session... */
1469 assert( new_conn == 0 );
1470 mc = metaconn_alloc( op );
1471 mc->mc_conn = mc_curr.mc_conn;
1472 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1474 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1475 LDAP_BACK_CONN_ISPRIV_SET( mc );
1477 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1478 LDAP_BACK_CONN_ISANON_SET( mc );
1482 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1483 metatarget_t *mt = mi->mi_targets[ i ];
1485 META_CANDIDATE_RESET( &candidates[ i ] );
1488 || meta_back_is_candidate( mt, &op->o_req_ndn,
1489 LDAP_SCOPE_SUBTREE ) )
1493 * The target is activated; if needed, it is
1496 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1497 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1498 LDAP_BACK_DONTSEND, !new_conn );
1499 candidates[ i ].sr_err = lerr;
1500 if ( lerr == LDAP_SUCCESS ) {
1501 META_CANDIDATE_SET( &candidates[ i ] );
1504 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1505 op->o_log_prefix, i, 0 );
1507 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1508 META_CANDIDATE_SET( &candidates[ i ] );
1510 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1511 op->o_log_prefix, i,
1512 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1517 * FIXME: in case one target cannot
1518 * be init'd, should the other ones
1522 ( void )meta_clear_one_candidate( op, mc, i );
1524 /* leave the target candidate, but record the error for later use */
1527 if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1528 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined err=%d\n",
1529 op->o_log_prefix, i, lerr );
1532 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed err=%d\n",
1533 op->o_log_prefix, i, lerr );
1536 if ( META_BACK_ONERR_STOP( mi ) ) {
1537 if ( sendok & LDAP_BACK_SENDERR ) {
1538 send_ldap_result( op, rs );
1542 meta_back_conn_free( mc );
1545 meta_back_release_conn( mi, mc );
1556 ( void )meta_clear_one_candidate( op, mc, i );
1561 if ( ncandidates == 0 ) {
1564 meta_back_conn_free( mc );
1567 meta_back_release_conn( mi, mc );
1570 if ( rs->sr_err == LDAP_SUCCESS ) {
1571 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1572 rs->sr_text = "Unable to select valid candidates";
1575 if ( sendok & LDAP_BACK_SENDERR ) {
1576 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1577 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1579 send_ldap_result( op, rs );
1580 rs->sr_matched = NULL;
1588 /* clear out meta_back_init_one_conn non-fatal errors */
1589 rs->sr_err = LDAP_SUCCESS;
1592 /* touch the timestamp */
1593 if ( mi->mi_idle_timeout != 0 ) {
1594 mc->mc_time = op->o_time;
1598 if ( mi->mi_conn_ttl ) {
1599 mc->mc_create_time = op->o_time;
1603 * Inserts the newly created metaconn in the avl tree
1605 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1606 #if META_BACK_PRINT_CONNTREE > 0
1607 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1608 #endif /* META_BACK_PRINT_CONNTREE */
1611 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1612 if ( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num < mi->mi_conn_priv_max ) {
1613 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1614 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num++;
1615 LDAP_BACK_CONN_CACHED_SET( mc );
1618 LDAP_BACK_CONN_TAINTED_SET( mc );
1622 } else if ( !( sendok & LDAP_BACK_BINDING ) ) {
1623 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1624 meta_back_conndn_cmp, meta_back_conndn_dup );
1625 LDAP_BACK_CONN_CACHED_SET( mc );
1628 #if META_BACK_PRINT_CONNTREE > 0
1629 meta_back_print_conntree( mi, "<<< meta_back_getconn" );
1630 #endif /* META_BACK_PRINT_CONNTREE */
1631 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1633 if ( !LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1635 * Err could be -1 in case a duplicate metaconn is inserted
1642 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1643 /* duplicate: free and try to get the newly created one */
1644 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1646 meta_back_conn_free( mc );
1652 LDAP_BACK_CONN_TAINTED_SET( mc );
1656 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1657 Debug( LDAP_DEBUG_ANY,
1658 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1659 op->o_log_prefix, ncandidates,
1660 LDAP_BACK_PCONN_ID( mc ) );
1663 meta_back_conn_free( mc );
1665 rs->sr_err = LDAP_OTHER;
1666 rs->sr_text = "Proxy bind collision";
1667 if ( sendok & LDAP_BACK_SENDERR ) {
1668 send_ldap_result( op, rs );
1674 Debug( LDAP_DEBUG_TRACE,
1675 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1676 op->o_log_prefix, ncandidates,
1677 LDAP_BACK_PCONN_ID( mc ) );
1680 Debug( LDAP_DEBUG_TRACE,
1681 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1682 op->o_log_prefix, ncandidates,
1683 LDAP_BACK_PCONN_ID( mc ) );
1690 meta_back_release_conn_lock(
1695 assert( mc != NULL );
1698 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1700 assert( mc->mc_refcnt > 0 );
1702 /* NOTE: the connection is removed if either it is tainted
1703 * or if it is shared and no one else is using it. This needs
1704 * to occur because for intrinsic reasons cached connections
1705 * that are not privileged would live forever and pollute
1706 * the connection space (and eat up resources). Maybe this
1707 * should be configurable... */
1708 if ( LDAP_BACK_CONN_TAINTED( mc ) || !LDAP_BACK_CONN_CACHED( mc ) ) {
1709 #if META_BACK_PRINT_CONNTREE > 0
1710 meta_back_print_conntree( mi, ">>> meta_back_release_conn" );
1711 #endif /* META_BACK_PRINT_CONNTREE */
1713 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1714 if ( mc->mc_q.tqe_prev != NULL ) {
1715 assert( LDAP_BACK_CONN_CACHED( mc ) );
1716 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1717 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1718 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1719 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1722 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1725 } else if ( LDAP_BACK_CONN_CACHED( mc ) ) {
1728 tmpmc = avl_delete( &mi->mi_conninfo.lai_tree,
1729 ( caddr_t )mc, meta_back_conndnmc_cmp );
1731 /* Overparanoid, but useful... */
1732 assert( tmpmc == NULL || tmpmc == mc );
1735 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1737 #if META_BACK_PRINT_CONNTREE > 0
1738 meta_back_print_conntree( mi, "<<< meta_back_release_conn" );
1739 #endif /* META_BACK_PRINT_CONNTREE */
1741 if ( mc->mc_refcnt == 0 ) {
1742 meta_back_conn_free( mc );
1747 if ( mc != NULL && LDAP_BACK_CONN_BINDING( mc ) ) {
1748 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1752 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1757 meta_back_quarantine(
1762 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1763 metatarget_t *mt = mi->mi_targets[ candidate ];
1765 slap_retry_info_t *ri = &mt->mt_quarantine;
1767 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1769 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1770 time_t new_last = slap_get_time();
1772 switch ( mt->mt_isquarantined ) {
1773 case LDAP_BACK_FQ_NO:
1774 if ( ri->ri_last == new_last ) {
1778 Debug( LDAP_DEBUG_ANY,
1779 "%s meta_back_quarantine[%d]: enter.\n",
1780 op->o_log_prefix, candidate, 0 );
1786 case LDAP_BACK_FQ_RETRYING:
1787 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1788 char buf[ SLAP_TEXT_BUFLEN ];
1790 snprintf( buf, sizeof( buf ),
1791 "meta_back_quarantine[%d]: block #%d try #%d failed",
1792 candidate, ri->ri_idx, ri->ri_count );
1793 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1794 op->o_log_prefix, buf, 0 );
1798 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1799 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1810 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1811 ri->ri_last = new_last;
1813 } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1814 Debug( LDAP_DEBUG_ANY,
1815 "%s meta_back_quarantine[%d]: exit.\n",
1816 op->o_log_prefix, candidate, 0 );
1818 if ( mi->mi_quarantine_f ) {
1819 (void)mi->mi_quarantine_f( mi, candidate,
1820 mi->mi_quarantine_p );
1825 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1829 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );