2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2013 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 int do_start_tls = 0;
285 #endif /* HAVE_TLS */
287 /* if the server is quarantined, and
288 * - the current interval did not expire yet, or
289 * - no more retries should occur,
290 * don't return the connection */
291 if ( mt->mt_isquarantined ) {
292 slap_retry_info_t *ri = &mt->mt_quarantine;
295 if ( mt->mt_quarantine.ri_interval ) {
296 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
297 dont_retry = ( mt->mt_isquarantined > LDAP_BACK_FQ_NO );
299 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
300 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
302 if ( LogTest( LDAP_DEBUG_ANY ) ) {
303 char buf[ SLAP_TEXT_BUFLEN ];
305 snprintf( buf, sizeof( buf ),
306 "meta_back_init_one_conn[%d]: quarantine "
307 "retry block #%d try #%d",
308 candidate, ri->ri_idx, ri->ri_count );
309 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
310 op->o_log_prefix, buf, 0 );
313 mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
317 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
321 rs->sr_err = LDAP_UNAVAILABLE;
322 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
323 rs->sr_text = "Target is quarantined";
324 send_ldap_result( op, rs );
332 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
338 if ( LDAP_BACK_CONN_ISBOUND( msc )
339 || LDAP_BACK_CONN_ISANON( msc ) )
341 assert( msc->msc_ld != NULL );
342 rs->sr_err = LDAP_SUCCESS;
345 } else if ( META_BACK_CONN_CREATING( msc )
346 || LDAP_BACK_CONN_BINDING( msc ) )
348 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
350 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
353 ldap_pvt_thread_yield();
357 /* sounds more appropriate */
358 rs->sr_err = LDAP_BUSY;
359 rs->sr_text = "No connections to target are available";
362 } else if ( META_BACK_CONN_INITED( msc ) ) {
363 assert( msc->msc_ld != NULL );
364 rs->sr_err = LDAP_SUCCESS;
371 META_BACK_CONN_CREATING_SET( msc );
375 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
379 if ( rs->sr_err != LDAP_SUCCESS
381 && ( sendok & LDAP_BACK_SENDERR ) )
383 send_ldap_result( op, rs );
389 assert( msc->msc_ld == NULL );
392 * Attempts to initialize the connection to the target ds
394 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
395 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
397 is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
398 #endif /* HAVE_TLS */
399 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
400 if ( rs->sr_err != LDAP_SUCCESS ) {
405 * Set LDAP version. This will always succeed: If the client
406 * bound with a particular version, then so can we.
408 if ( mt->mt_version != 0 ) {
409 version = mt->mt_version;
411 } else if ( op->o_conn->c_protocol != 0 ) {
412 version = op->o_conn->c_protocol;
415 version = LDAP_VERSION3;
417 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
418 ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
420 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
421 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
422 META_BACK_TGT_CHASE_REFERRALS( mt ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
424 slap_client_keepalive(msc->msc_ld, &mt->mt_tls.sb_keepalive);
428 slap_bindconf *sb = NULL;
431 sb = &mt->mt_idassert.si_bc;
436 if ( sb->sb_tls_do_init ) {
437 bindconf_tls_set( sb, msc->msc_ld );
438 } else if ( sb->sb_tls_ctx ) {
439 ldap_set_option( msc->msc_ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
442 if ( sb == &mt->mt_idassert.si_bc && sb->sb_tls_ctx ) {
445 } else if ( META_BACK_TGT_USE_TLS( mt )
446 || ( op->o_conn->c_is_tls && META_BACK_TGT_PROPAGATE_TLS( mt ) ) )
452 /* start TLS ("tls [try-]{start|propagate}" statement) */
453 if ( do_start_tls ) {
454 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
456 * use asynchronous StartTLS; in case, chase referral
457 * FIXME: OpenLDAP does not return referral on StartTLS yet
461 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
462 if ( rs->sr_err == LDAP_SUCCESS ) {
463 LDAPMessage *res = NULL;
464 int rc, nretries = mt->mt_nretries;
467 LDAP_BACK_TV_SET( &tv );
470 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
473 rs->sr_err = LDAP_OTHER;
477 if ( nretries != 0 ) {
478 if ( nretries > 0 ) {
481 LDAP_BACK_TV_SET( &tv );
484 rs->sr_err = LDAP_OTHER;
488 /* only touch when activity actually took place... */
489 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
490 msc->msc_time = op->o_time;
495 if ( rc == LDAP_RES_EXTENDED ) {
496 struct berval *data = NULL;
498 /* NOTE: right now, data is unused, so don't get it */
499 rs->sr_err = ldap_parse_extended_result( msc->msc_ld,
500 res, NULL, NULL /* &data */ , 0 );
501 if ( rs->sr_err == LDAP_SUCCESS ) {
504 /* FIXME: matched? referrals? response controls? */
505 rs->sr_err = ldap_parse_result( msc->msc_ld,
506 res, &err, NULL, NULL, NULL, NULL, 1 );
509 if ( rs->sr_err == LDAP_SUCCESS ) {
512 rs->sr_err = slap_map_api2result( rs );
514 /* FIXME: in case a referral
515 * is returned, should we try
516 * using it instead of the
518 if ( rs->sr_err == LDAP_SUCCESS ) {
519 ldap_install_tls( msc->msc_ld );
521 } else if ( rs->sr_err == LDAP_REFERRAL ) {
522 /* FIXME: LDAP_OPERATIONS_ERROR? */
523 rs->sr_err = LDAP_OTHER;
524 rs->sr_text = "Unwilling to chase referral "
525 "returned by Start TLS exop";
534 rs->sr_err = LDAP_OTHER;
541 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
543 * use synchronous StartTLS
545 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
546 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
548 /* if StartTLS is requested, only attempt it if the URL
549 * is not "ldaps://"; this may occur not only in case
550 * of misconfiguration, but also when used in the chain
551 * overlay, where the "uri" can be parsed out of a referral */
552 if ( rs->sr_err == LDAP_SERVER_DOWN
553 || ( rs->sr_err != LDAP_SUCCESS
554 && META_BACK_TGT_TLS_CRITICAL( mt ) ) )
558 Debug( LDAP_DEBUG_ANY,
559 "### %s meta_back_init_one_conn(TLS) "
560 "ldap_unbind_ext[%d] ld=%p\n",
561 op->o_log_prefix, candidate,
562 (void *)msc->msc_ld );
563 #endif /* DEBUG_205 */
565 /* need to trash a failed Start TLS */
566 meta_clear_one_candidate( op, mc, candidate );
570 #endif /* HAVE_TLS */
573 * Set the network timeout if set
575 if ( mt->mt_network_timeout != 0 ) {
576 struct timeval network_timeout;
578 network_timeout.tv_usec = 0;
579 network_timeout.tv_sec = mt->mt_network_timeout;
581 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
582 (void *)&network_timeout );
586 * If the connection DN is not null, an attempt to rewrite it is made
590 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
591 ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
592 if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
593 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
594 memset( msc->msc_cred.bv_val, 0,
595 msc->msc_cred.bv_len );
597 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
599 LDAP_BACK_CONN_ISIDASSERT_SET( msc );
602 ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
606 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
607 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
608 ber_memfree_x( msc->msc_cred.bv_val, NULL );
609 BER_BVZERO( &msc->msc_cred );
611 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
612 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
613 BER_BVZERO( &msc->msc_bound_ndn );
615 if ( !BER_BVISEMPTY( &op->o_ndn )
616 && SLAP_IS_AUTHZ_BACKEND( op )
620 dc.conn = op->o_conn;
625 * Rewrite the bind dn if needed
627 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
628 &msc->msc_bound_ndn ) )
632 Debug( LDAP_DEBUG_ANY,
633 "### %s meta_back_init_one_conn(rewrite) "
634 "ldap_unbind_ext[%d] ld=%p\n",
635 op->o_log_prefix, candidate,
636 (void *)msc->msc_ld );
637 #endif /* DEBUG_205 */
639 /* need to trash a connection not fully established */
640 meta_clear_one_candidate( op, mc, candidate );
644 /* copy the DN if needed */
645 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
646 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
649 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
652 ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
656 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
660 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
662 META_BACK_CONN_CREATING_CLEAR( msc );
663 if ( rs->sr_err == LDAP_SUCCESS ) {
665 * Sets a cookie for the rewrite session
667 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
668 META_BACK_CONN_INITED_SET( msc );
671 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
674 if ( rs->sr_err != LDAP_SUCCESS ) {
675 rs->sr_err = slap_map_api2result( rs );
676 if ( sendok & LDAP_BACK_SENDERR ) {
677 send_ldap_result( op, rs );
687 * Retries one connection
695 ldap_back_send_t sendok )
697 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
698 metatarget_t *mt = mi->mi_targets[ candidate ];
699 metaconn_t *mc = *mcp;
700 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
701 int rc = LDAP_UNAVAILABLE,
705 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
707 assert( !META_BACK_CONN_CREATING( msc ) );
708 binding = LDAP_BACK_CONN_BINDING( msc );
709 LDAP_BACK_CONN_BINDING_CLEAR( msc );
711 assert( mc->mc_refcnt > 0 );
712 if ( mc->mc_refcnt == 1 ) {
713 struct berval save_cred;
715 if ( LogTest( LDAP_DEBUG_ANY ) ) {
716 char buf[ SLAP_TEXT_BUFLEN ];
718 /* this lock is required; however,
719 * it's invoked only when logging is on */
720 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
721 snprintf( buf, sizeof( buf ),
722 "retrying URI=\"%s\" DN=\"%s\"",
724 BER_BVISNULL( &msc->msc_bound_ndn ) ?
725 "" : msc->msc_bound_ndn.bv_val );
726 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
728 Debug( LDAP_DEBUG_ANY,
729 "%s meta_back_retry[%d]: %s.\n",
730 op->o_log_prefix, candidate, buf );
733 /* save credentials, if any, for later use;
734 * meta_clear_one_candidate() would free them */
735 save_cred = msc->msc_cred;
736 BER_BVZERO( &msc->msc_cred );
738 meta_clear_one_candidate( op, mc, candidate );
739 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
741 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
743 /* mc here must be the regular mc, reset and ready for init */
744 rc = meta_back_init_one_conn( op, rs, mc, candidate,
745 LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 );
747 /* restore credentials, if any and if needed;
748 * meta_back_init_one_conn() restores msc_bound_ndn, if any;
749 * if no msc_bound_ndn is restored, destroy credentials */
750 if ( !BER_BVISNULL( &msc->msc_bound_ndn )
751 && BER_BVISNULL( &msc->msc_cred ) )
753 msc->msc_cred = save_cred;
755 } else if ( !BER_BVISNULL( &save_cred ) ) {
756 memset( save_cred.bv_val, 0, save_cred.bv_len );
757 ber_memfree_x( save_cred.bv_val, NULL );
760 /* restore the "binding" flag, in case */
762 LDAP_BACK_CONN_BINDING_SET( msc );
765 if ( rc == LDAP_SUCCESS ) {
767 LDAP_BACK_CONN_BINDING_SET( msc ); binding = 1;
768 rc = meta_back_single_dobind( op, rs, mcp, candidate,
769 sendok, mt->mt_nretries, 0 );
771 Debug( LDAP_DEBUG_ANY,
772 "%s meta_back_retry[%d]: "
773 "meta_back_single_dobind=%d\n",
774 op->o_log_prefix, candidate, rc );
775 if ( rc == LDAP_SUCCESS ) {
776 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
777 !BER_BVISEMPTY( &msc->msc_bound_ndn ) )
779 LDAP_BACK_CONN_ISBOUND_SET( msc );
782 LDAP_BACK_CONN_ISANON_SET( msc );
785 /* when bound, dispose of the "binding" flag */
787 LDAP_BACK_CONN_BINDING_CLEAR( msc );
792 #if 0 /* ITS#7591, following stmt drops needed result msgs */
793 /* don't send twice */
794 sendok &= ~LDAP_BACK_SENDERR;
798 if ( rc != LDAP_SUCCESS ) {
799 SlapReply *candidates = meta_back_candidates_get( op );
801 candidates[ candidate ].sr_err = rc;
803 if ( *mcp != NULL ) {
804 if ( mc->mc_refcnt == 1 ) {
806 LDAP_BACK_CONN_BINDING_CLEAR( msc );
808 (void)meta_clear_one_candidate( op, mc, candidate );
811 LDAP_BACK_CONN_TAINTED_SET( mc );
812 /* only release if mandatory; otherwise
813 * let the caller do what's best before
815 if ( META_BACK_ONERR_STOP( mi ) ) {
816 meta_back_release_conn_lock( mi, mc, 0 );
820 #if META_BACK_PRINT_CONNTREE > 0
821 meta_back_print_conntree( mi, ">>> meta_back_retry" );
822 #endif /* META_BACK_PRINT_CONNTREE */
824 /* FIXME: could be done better, reworking meta_back_release_conn_lock() */
825 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
826 if ( mc->mc_q.tqe_prev != NULL ) {
827 assert( LDAP_BACK_CONN_CACHED( mc ) );
828 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
829 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
831 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
832 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
835 assert( !LDAP_BACK_CONN_CACHED( mc ) );
839 /* FIXME: check if in tree, for consistency? */
840 (void)avl_delete( &mi->mi_conninfo.lai_tree,
841 ( caddr_t )mc, meta_back_conndnmc_cmp );
843 LDAP_BACK_CONN_CACHED_CLEAR( mc );
845 #if META_BACK_PRINT_CONNTREE > 0
846 meta_back_print_conntree( mi, "<<< meta_back_retry" );
847 #endif /* META_BACK_PRINT_CONNTREE */
851 if ( sendok & LDAP_BACK_SENDERR ) {
853 rs->sr_text = "Unable to retry";
854 send_ldap_result( op, rs );
858 if ( quarantine && META_BACK_TGT_QUARANTINE( mt ) ) {
859 meta_back_quarantine( op, rs, candidate );
862 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
864 return rc == LDAP_SUCCESS ? 1 : 0;
868 * callback for unique candidate selection
871 meta_back_conn_cb( Operation *op, SlapReply *rs )
873 assert( op->o_tag == LDAP_REQ_SEARCH );
875 switch ( rs->sr_type ) {
877 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
893 meta_back_get_candidate(
898 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
902 * tries to get a unique candidate
903 * (takes care of default target)
905 candidate = meta_back_select_unique_candidate( mi, ndn );
908 * if any is found, inits the connection
910 if ( candidate == META_TARGET_NONE ) {
911 rs->sr_err = LDAP_NO_SUCH_OBJECT;
912 rs->sr_text = "No suitable candidate target found";
914 } else if ( candidate == META_TARGET_MULTIPLE ) {
916 SlapReply rs2 = { REP_RESULT };
917 slap_callback cb2 = { 0 };
920 /* try to get a unique match for the request ndn
921 * among the multiple candidates available */
922 op2.o_tag = LDAP_REQ_SEARCH;
924 op2.o_req_ndn = *ndn;
925 op2.ors_scope = LDAP_SCOPE_BASE;
926 op2.ors_deref = LDAP_DEREF_NEVER;
927 op2.ors_attrs = slap_anlist_no_attrs;
928 op2.ors_attrsonly = 0;
929 op2.ors_limit = NULL;
931 op2.ors_tlimit = SLAP_NO_LIMIT;
933 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
934 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
936 op2.o_callback = &cb2;
937 cb2.sc_response = meta_back_conn_cb;
938 cb2.sc_private = (void *)&candidate;
940 rc = op->o_bd->be_search( &op2, &rs2 );
942 switch ( rs2.sr_err ) {
945 rs->sr_err = rs2.sr_err;
948 case LDAP_SIZELIMIT_EXCEEDED:
949 /* if multiple candidates can serve the operation,
950 * and a default target is defined, and it is
951 * a candidate, try using it (FIXME: YMMV) */
952 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
953 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
954 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
956 candidate = mi->mi_defaulttarget;
957 rs->sr_err = LDAP_SUCCESS;
961 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
962 rs->sr_text = "Unable to select unique candidate target";
968 rs->sr_err = LDAP_SUCCESS;
974 static void *meta_back_candidates_dummy;
977 meta_back_candidates_keyfree(
981 metacandidates_t *mc = (metacandidates_t *)data;
983 ber_memfree_x( mc->mc_candidates, NULL );
984 ber_memfree_x( data, NULL );
988 meta_back_candidates_get( Operation *op )
990 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
991 metacandidates_t *mc;
993 if ( op->o_threadctx ) {
996 ldap_pvt_thread_pool_getkey( op->o_threadctx,
997 &meta_back_candidates_dummy, &data, NULL );
998 mc = (metacandidates_t *)data;
1001 mc = mi->mi_candidates;
1005 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
1006 mc->mc_ntargets = mi->mi_ntargets;
1007 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
1008 if ( op->o_threadctx ) {
1012 ldap_pvt_thread_pool_setkey( op->o_threadctx,
1013 &meta_back_candidates_dummy, data,
1014 meta_back_candidates_keyfree,
1018 mi->mi_candidates = mc;
1021 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
1022 /* NOTE: in the future, may want to allow back-config
1023 * to add/remove targets from back-meta... */
1024 mc->mc_candidates = ch_realloc( mc->mc_candidates,
1025 sizeof( SlapReply ) * mi->mi_ntargets );
1026 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
1027 sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
1028 mc->mc_ntargets = mi->mi_ntargets;
1031 return mc->mc_candidates;
1037 * Prepares the connection structure
1041 * - determine what DN is being requested:
1043 * op requires candidate checks
1045 * add unique parent of o_req_ndn
1046 * bind unique^*[/all] o_req_ndn [no check]
1047 * compare unique^+ o_req_ndn
1048 * delete unique o_req_ndn
1049 * modify unique o_req_ndn
1050 * search any o_req_ndn
1051 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
1053 * - for ops that require the candidate to be unique, in case of multiple
1054 * occurrences an internal search with sizeLimit=1 is performed
1055 * if a unique candidate can actually be determined. If none is found,
1056 * the operation aborts; if multiple are found, the default target
1057 * is used if defined and candidate; otherwise the operation aborts.
1059 * *^note: actually, the bind operation is handled much like a search;
1060 * i.e. the bind is broadcast to all candidate targets.
1062 * +^note: actually, the compare operation is handled much like a search;
1063 * i.e. the compare is broadcast to all candidate targets, while checking
1064 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
1072 ldap_back_send_t sendok )
1074 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1075 metaconn_t *mc = NULL,
1077 int cached = META_TARGET_NONE,
1078 i = META_TARGET_NONE,
1084 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
1088 META_DNTYPE_NEWPARENT
1089 } dn_type = META_DNTYPE_ENTRY;
1090 struct berval ndn = op->o_req_ndn,
1093 SlapReply *candidates = meta_back_candidates_get( op );
1095 /* Internal searches are privileged and shared. So is root. */
1096 if ( ( !BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ALWAYS( mi ) )
1097 || ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ANON( mi ) )
1098 || op->o_do_not_cache || be_isroot( op ) )
1100 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
1101 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
1102 LDAP_BACK_PCONN_ROOTDN_SET( &mc_curr, op );
1104 } else if ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_NOANON( mi ) )
1106 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1107 BER_BVSTR( &mc_curr.mc_local_ndn, "" );
1108 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1111 mc_curr.mc_local_ndn = op->o_ndn;
1113 /* Explicit binds must not be shared */
1114 if ( !BER_BVISEMPTY( &op->o_ndn )
1115 || op->o_tag == LDAP_REQ_BIND
1116 || SLAP_IS_AUTHZ_BACKEND( op ) )
1118 mc_curr.mc_conn = op->o_conn;
1121 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1122 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1126 /* Explicit Bind requests always get their own conn */
1127 if ( sendok & LDAP_BACK_BINDING ) {
1128 mc_curr.mc_conn = op->o_conn;
1131 /* Searches for a metaconn in the avl tree */
1133 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1134 if ( LDAP_BACK_PCONN_ISPRIV( &mc_curr ) ) {
1135 /* lookup a conn that's not binding */
1136 LDAP_TAILQ_FOREACH( mc,
1137 &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv,
1140 if ( !LDAP_BACK_CONN_BINDING( mc ) && mc->mc_refcnt == 0 ) {
1146 /* move to tail of queue */
1147 if ( mc != LDAP_TAILQ_LAST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1148 metaconn_t, mc_q ) )
1150 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1152 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1153 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1157 } else if ( !LDAP_BACK_USE_TEMPORARIES( mi )
1158 && mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_num == mi->mi_conn_priv_max )
1160 mc = LDAP_TAILQ_FIRST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv );
1165 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1166 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1170 /* catch taint errors */
1171 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1173 /* Don't reuse connections while they're still binding
1174 * NOTE: only makes sense for binds */
1175 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1176 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1177 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1179 ldap_pvt_thread_yield();
1183 /* release conn, and create a temporary */
1187 if ( mc->mc_refcnt == 0 && (( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
1188 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout )) )
1190 #if META_BACK_PRINT_CONNTREE > 0
1191 meta_back_print_conntree( mi,
1192 ">>> meta_back_getconn(expired)" );
1193 #endif /* META_BACK_PRINT_CONNTREE */
1195 /* don't let anyone else use this expired connection */
1196 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1197 if ( mc->mc_q.tqe_prev != NULL ) {
1198 assert( LDAP_BACK_CONN_CACHED( mc ) );
1199 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1200 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1202 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1203 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1206 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1210 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1211 (caddr_t)mc, meta_back_conndnmc_cmp );
1214 #if META_BACK_PRINT_CONNTREE > 0
1215 meta_back_print_conntree( mi,
1216 "<<< meta_back_getconn(expired)" );
1217 #endif /* META_BACK_PRINT_CONNTREE */
1218 LDAP_BACK_CONN_TAINTED_SET( mc );
1219 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1221 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1222 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1223 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1225 Debug( LDAP_DEBUG_TRACE,
1226 "%s meta_back_getconn: mc=%p conn=%s expired (tainted).\n",
1227 op->o_log_prefix, (void *)mc, buf );
1234 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1237 switch ( op->o_tag ) {
1239 /* if we go to selection, the entry must not exist,
1240 * and we must be able to resolve the parent */
1241 dn_type = META_DNTYPE_PARENT;
1242 dnParent( &ndn, &pndn );
1245 case LDAP_REQ_MODRDN:
1246 /* if nnewSuperior is not NULL, it must resolve
1247 * to the same candidate as the req_ndn */
1248 if ( op->orr_nnewSup ) {
1249 dn_type = META_DNTYPE_NEWPARENT;
1254 /* if bound as rootdn, the backend must bind to all targets
1255 * with the administrative identity
1256 * (unless pseoudoroot-bind-defer is TRUE) */
1257 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
1258 op_type = META_OP_REQUIRE_ALL;
1262 case LDAP_REQ_COMPARE:
1263 case LDAP_REQ_DELETE:
1264 case LDAP_REQ_MODIFY:
1265 /* just a unique candidate */
1268 case LDAP_REQ_SEARCH:
1269 /* allow multiple candidates for the searchBase */
1270 op_type = META_OP_ALLOW_MULTIPLE;
1274 /* right now, just break (exop?) */
1279 * require all connections ...
1281 if ( op_type == META_OP_REQUIRE_ALL ) {
1283 /* Looks like we didn't get a bind. Open a new session... */
1285 assert( new_conn == 0 );
1286 mc = metaconn_alloc( op );
1287 mc->mc_conn = mc_curr.mc_conn;
1288 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1290 if ( sendok & LDAP_BACK_BINDING ) {
1291 LDAP_BACK_CONN_BINDING_SET( mc );
1293 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1294 LDAP_BACK_CONN_ISPRIV_SET( mc );
1296 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1297 LDAP_BACK_CONN_ISANON_SET( mc );
1301 /* TODO: if any of the connections is binding,
1302 * release mc and create a new one */
1305 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1307 * The target is activated; if needed, it is
1310 candidates[ i ].sr_err = meta_back_init_one_conn( op,
1311 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1312 LDAP_BACK_DONTSEND, !new_conn );
1313 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1314 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
1315 LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
1317 META_CANDIDATE_SET( &candidates[ i ] );
1323 * FIXME: in case one target cannot
1324 * be init'd, should the other ones
1327 META_CANDIDATE_RESET( &candidates[ i ] );
1328 err = candidates[ i ].sr_err;
1333 if ( ncandidates == 0 ) {
1336 meta_back_conn_free( mc );
1339 meta_back_release_conn( mi, mc );
1342 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1343 rs->sr_text = "Unable to select valid candidates";
1345 if ( sendok & LDAP_BACK_SENDERR ) {
1346 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1347 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1349 send_ldap_result( op, rs );
1350 rs->sr_matched = NULL;
1360 * looks in cache, if any
1362 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1363 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1366 if ( op_type == META_OP_REQUIRE_SINGLE ) {
1367 metatarget_t *mt = NULL;
1368 metasingleconn_t *msc = NULL;
1372 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1373 META_CANDIDATE_RESET( &candidates[ j ] );
1377 * tries to get a unique candidate
1378 * (takes care of default target)
1380 if ( i == META_TARGET_NONE ) {
1381 i = meta_back_get_candidate( op, rs, &ndn );
1383 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1384 i = meta_back_get_candidate( op, rs, &pndn );
1387 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1389 meta_back_release_conn( mi, mc );
1392 if ( sendok & LDAP_BACK_SENDERR ) {
1393 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1394 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1396 send_ldap_result( op, rs );
1397 rs->sr_matched = NULL;
1404 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1407 meta_back_release_conn( mi, mc );
1410 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1411 rs->sr_text = "Cross-target rename not supported";
1412 if ( sendok & LDAP_BACK_SENDERR ) {
1413 send_ldap_result( op, rs );
1419 Debug( LDAP_DEBUG_TRACE,
1420 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1421 i, op->o_req_ndn.bv_val, 0 );
1424 /* Retries searching for a metaconn in the avl tree
1425 * the reason is that the connection might have been
1426 * created by meta_back_get_candidate() */
1427 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1429 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1430 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1431 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1433 /* catch taint errors */
1434 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1436 /* Don't reuse connections while they're still binding */
1437 if ( META_BACK_CONN_CREATING( &mc->mc_conns[ i ] )
1438 || LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) )
1440 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1441 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1442 ldap_pvt_thread_yield();
1452 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1455 /* Looks like we didn't get a bind. Open a new session... */
1457 assert( new_conn == 0 );
1458 mc = metaconn_alloc( op );
1459 mc->mc_conn = mc_curr.mc_conn;
1460 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1462 if ( sendok & LDAP_BACK_BINDING ) {
1463 LDAP_BACK_CONN_BINDING_SET( mc );
1465 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1466 LDAP_BACK_CONN_ISPRIV_SET( mc );
1468 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1469 LDAP_BACK_CONN_ISANON_SET( mc );
1475 * Clear all other candidates
1477 ( void )meta_clear_unused_candidates( op, i );
1479 mt = mi->mi_targets[ i ];
1480 msc = &mc->mc_conns[ i ];
1483 * The target is activated; if needed, it is
1484 * also init'd. In case of error, meta_back_init_one_conn
1485 * sends the appropriate result.
1487 err = meta_back_init_one_conn( op, rs, mc, i,
1488 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn );
1489 if ( err != LDAP_SUCCESS ) {
1491 * FIXME: in case one target cannot
1492 * be init'd, should the other ones
1495 META_CANDIDATE_RESET( &candidates[ i ] );
1498 meta_back_conn_free( mc );
1501 meta_back_release_conn( mi, mc );
1506 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
1507 LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
1510 candidates[ i ].sr_err = LDAP_SUCCESS;
1511 META_CANDIDATE_SET( &candidates[ i ] );
1519 * if no unique candidate ...
1523 /* Looks like we didn't get a bind. Open a new session... */
1525 assert( new_conn == 0 );
1526 mc = metaconn_alloc( op );
1527 mc->mc_conn = mc_curr.mc_conn;
1528 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1530 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1531 LDAP_BACK_CONN_ISPRIV_SET( mc );
1533 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1534 LDAP_BACK_CONN_ISANON_SET( mc );
1538 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1539 metatarget_t *mt = mi->mi_targets[ i ];
1541 META_CANDIDATE_RESET( &candidates[ i ] );
1544 || meta_back_is_candidate( mt, &op->o_req_ndn,
1545 op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_SUBTREE ) )
1549 * The target is activated; if needed, it is
1552 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1553 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1554 LDAP_BACK_DONTSEND, !new_conn );
1555 candidates[ i ].sr_err = lerr;
1556 if ( lerr == LDAP_SUCCESS ) {
1557 META_CANDIDATE_SET( &candidates[ i ] );
1560 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1561 op->o_log_prefix, i, 0 );
1563 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1564 META_CANDIDATE_SET( &candidates[ i ] );
1566 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1567 op->o_log_prefix, i,
1568 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1573 * FIXME: in case one target cannot
1574 * be init'd, should the other ones
1578 ( void )meta_clear_one_candidate( op, mc, i );
1580 /* leave the target candidate, but record the error for later use */
1583 if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1584 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined err=%d\n",
1585 op->o_log_prefix, i, lerr );
1588 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed err=%d\n",
1589 op->o_log_prefix, i, lerr );
1592 if ( META_BACK_ONERR_STOP( mi ) ) {
1593 if ( sendok & LDAP_BACK_SENDERR ) {
1594 send_ldap_result( op, rs );
1598 meta_back_conn_free( mc );
1601 meta_back_release_conn( mi, mc );
1612 ( void )meta_clear_one_candidate( op, mc, i );
1617 if ( ncandidates == 0 ) {
1620 meta_back_conn_free( mc );
1623 meta_back_release_conn( mi, mc );
1626 if ( rs->sr_err == LDAP_SUCCESS ) {
1627 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1628 rs->sr_text = "Unable to select valid candidates";
1631 if ( sendok & LDAP_BACK_SENDERR ) {
1632 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1633 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1635 send_ldap_result( op, rs );
1636 rs->sr_matched = NULL;
1644 /* clear out meta_back_init_one_conn non-fatal errors */
1645 rs->sr_err = LDAP_SUCCESS;
1648 /* touch the timestamp */
1649 if ( mi->mi_idle_timeout != 0 ) {
1650 mc->mc_time = op->o_time;
1654 if ( mi->mi_conn_ttl ) {
1655 mc->mc_create_time = op->o_time;
1659 * Inserts the newly created metaconn in the avl tree
1661 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1662 #if META_BACK_PRINT_CONNTREE > 0
1663 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1664 #endif /* META_BACK_PRINT_CONNTREE */
1667 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1668 if ( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num < mi->mi_conn_priv_max ) {
1669 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1670 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num++;
1671 LDAP_BACK_CONN_CACHED_SET( mc );
1674 LDAP_BACK_CONN_TAINTED_SET( mc );
1678 } else if ( !( sendok & LDAP_BACK_BINDING ) ) {
1679 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1680 meta_back_conndn_cmp, meta_back_conndn_dup );
1681 LDAP_BACK_CONN_CACHED_SET( mc );
1684 #if META_BACK_PRINT_CONNTREE > 0
1685 meta_back_print_conntree( mi, "<<< meta_back_getconn" );
1686 #endif /* META_BACK_PRINT_CONNTREE */
1687 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1689 if ( !LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1691 * Err could be -1 in case a duplicate metaconn is inserted
1698 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1699 /* duplicate: free and try to get the newly created one */
1700 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1702 meta_back_conn_free( mc );
1708 LDAP_BACK_CONN_TAINTED_SET( mc );
1712 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1713 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1714 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1715 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1717 Debug( LDAP_DEBUG_ANY,
1718 "%s meta_back_getconn: candidates=%d conn=%s insert failed\n",
1719 op->o_log_prefix, ncandidates, buf );
1723 meta_back_conn_free( mc );
1725 rs->sr_err = LDAP_OTHER;
1726 rs->sr_text = "Proxy bind collision";
1727 if ( sendok & LDAP_BACK_SENDERR ) {
1728 send_ldap_result( op, rs );
1734 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1735 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1736 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1738 Debug( LDAP_DEBUG_TRACE,
1739 "%s meta_back_getconn: candidates=%d conn=%s inserted\n",
1740 op->o_log_prefix, ncandidates, buf );
1744 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1745 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1746 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1748 Debug( LDAP_DEBUG_TRACE,
1749 "%s meta_back_getconn: candidates=%d conn=%s fetched\n",
1750 op->o_log_prefix, ncandidates, buf );
1758 meta_back_release_conn_lock(
1763 assert( mc != NULL );
1766 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1768 assert( mc->mc_refcnt > 0 );
1770 /* NOTE: the connection is removed if either it is tainted
1771 * or if it is shared and no one else is using it. This needs
1772 * to occur because for intrinsic reasons cached connections
1773 * that are not privileged would live forever and pollute
1774 * the connection space (and eat up resources). Maybe this
1775 * should be configurable... */
1776 if ( LDAP_BACK_CONN_TAINTED( mc ) || !LDAP_BACK_CONN_CACHED( mc ) ) {
1777 #if META_BACK_PRINT_CONNTREE > 0
1778 meta_back_print_conntree( mi, ">>> meta_back_release_conn" );
1779 #endif /* META_BACK_PRINT_CONNTREE */
1781 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1782 if ( mc->mc_q.tqe_prev != NULL ) {
1783 assert( LDAP_BACK_CONN_CACHED( mc ) );
1784 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1785 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1786 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1787 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1790 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1793 } else if ( LDAP_BACK_CONN_CACHED( mc ) ) {
1796 tmpmc = avl_delete( &mi->mi_conninfo.lai_tree,
1797 ( caddr_t )mc, meta_back_conndnmc_cmp );
1799 /* Overparanoid, but useful... */
1800 assert( tmpmc == NULL || tmpmc == mc );
1803 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1805 #if META_BACK_PRINT_CONNTREE > 0
1806 meta_back_print_conntree( mi, "<<< meta_back_release_conn" );
1807 #endif /* META_BACK_PRINT_CONNTREE */
1809 if ( mc->mc_refcnt == 0 ) {
1810 meta_back_conn_free( mc );
1815 if ( mc != NULL && LDAP_BACK_CONN_BINDING( mc ) ) {
1816 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1820 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1825 meta_back_quarantine(
1830 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1831 metatarget_t *mt = mi->mi_targets[ candidate ];
1833 slap_retry_info_t *ri = &mt->mt_quarantine;
1835 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1837 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1838 time_t new_last = slap_get_time();
1840 switch ( mt->mt_isquarantined ) {
1841 case LDAP_BACK_FQ_NO:
1842 if ( ri->ri_last == new_last ) {
1846 Debug( LDAP_DEBUG_ANY,
1847 "%s meta_back_quarantine[%d]: enter.\n",
1848 op->o_log_prefix, candidate, 0 );
1854 case LDAP_BACK_FQ_RETRYING:
1855 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1856 char buf[ SLAP_TEXT_BUFLEN ];
1858 snprintf( buf, sizeof( buf ),
1859 "meta_back_quarantine[%d]: block #%d try #%d failed",
1860 candidate, ri->ri_idx, ri->ri_count );
1861 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1862 op->o_log_prefix, buf, 0 );
1866 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1867 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1878 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1879 ri->ri_last = new_last;
1881 } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1882 Debug( LDAP_DEBUG_ANY,
1883 "%s meta_back_quarantine[%d]: exit.\n",
1884 op->o_log_prefix, candidate, 0 );
1886 if ( mi->mi_quarantine_f ) {
1887 (void)mi->mi_quarantine_f( mi, candidate,
1888 mi->mi_quarantine_p );
1893 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1897 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );