2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2010 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 ) {
488 rs->sr_err = slap_map_api2result( rs );
490 /* FIXME: in case a referral
491 * is returned, should we try
492 * using it instead of the
494 if ( rs->sr_err == LDAP_SUCCESS ) {
495 ldap_install_tls( msc->msc_ld );
497 } else if ( rs->sr_err == LDAP_REFERRAL ) {
498 /* FIXME: LDAP_OPERATIONS_ERROR? */
499 rs->sr_err = LDAP_OTHER;
500 rs->sr_text = "Unwilling to chase referral "
501 "returned by Start TLS exop";
510 rs->sr_err = LDAP_OTHER;
517 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
519 * use synchronous StartTLS
521 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
522 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
524 /* if StartTLS is requested, only attempt it if the URL
525 * is not "ldaps://"; this may occur not only in case
526 * of misconfiguration, but also when used in the chain
527 * overlay, where the "uri" can be parsed out of a referral */
528 if ( rs->sr_err == LDAP_SERVER_DOWN
529 || ( rs->sr_err != LDAP_SUCCESS
530 && META_BACK_TGT_TLS_CRITICAL( mt ) ) )
534 Debug( LDAP_DEBUG_ANY,
535 "### %s meta_back_init_one_conn(TLS) "
536 "ldap_unbind_ext[%d] ld=%p\n",
537 op->o_log_prefix, candidate,
538 (void *)msc->msc_ld );
539 #endif /* DEBUG_205 */
541 /* need to trash a failed Start TLS */
542 meta_clear_one_candidate( op, mc, candidate );
546 #endif /* HAVE_TLS */
549 * Set the network timeout if set
551 if ( mt->mt_network_timeout != 0 ) {
552 struct timeval network_timeout;
554 network_timeout.tv_usec = 0;
555 network_timeout.tv_sec = mt->mt_network_timeout;
557 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
558 (void *)&network_timeout );
562 * If the connection DN is not null, an attempt to rewrite it is made
566 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
567 ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
568 if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
569 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
570 memset( msc->msc_cred.bv_val, 0,
571 msc->msc_cred.bv_len );
573 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
577 ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
581 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
582 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
583 ber_memfree_x( msc->msc_cred.bv_val, NULL );
584 BER_BVZERO( &msc->msc_cred );
586 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
587 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
588 BER_BVZERO( &msc->msc_bound_ndn );
590 if ( !BER_BVISEMPTY( &op->o_ndn )
591 && SLAP_IS_AUTHZ_BACKEND( op )
595 dc.conn = op->o_conn;
600 * Rewrite the bind dn if needed
602 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
603 &msc->msc_bound_ndn ) )
607 Debug( LDAP_DEBUG_ANY,
608 "### %s meta_back_init_one_conn(rewrite) "
609 "ldap_unbind_ext[%d] ld=%p\n",
610 op->o_log_prefix, candidate,
611 (void *)msc->msc_ld );
612 #endif /* DEBUG_205 */
614 /* need to trash a connection not fully established */
615 meta_clear_one_candidate( op, mc, candidate );
619 /* copy the DN if needed */
620 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
621 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
624 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
627 ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
631 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
635 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
637 META_BACK_CONN_CREATING_CLEAR( msc );
638 if ( rs->sr_err == LDAP_SUCCESS ) {
640 * Sets a cookie for the rewrite session
642 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
643 META_BACK_CONN_INITED_SET( msc );
646 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
649 if ( rs->sr_err != LDAP_SUCCESS ) {
650 rs->sr_err = slap_map_api2result( rs );
651 if ( sendok & LDAP_BACK_SENDERR ) {
652 send_ldap_result( op, rs );
662 * Retries one connection
670 ldap_back_send_t sendok )
672 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
673 metatarget_t *mt = mi->mi_targets[ candidate ];
674 metaconn_t *mc = *mcp;
675 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
676 int rc = LDAP_UNAVAILABLE,
680 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
682 assert( !META_BACK_CONN_CREATING( msc ) );
683 binding = LDAP_BACK_CONN_BINDING( msc );
684 LDAP_BACK_CONN_BINDING_CLEAR( msc );
686 assert( mc->mc_refcnt > 0 );
687 if ( mc->mc_refcnt == 1 ) {
688 if ( LogTest( LDAP_DEBUG_ANY ) ) {
689 char buf[ SLAP_TEXT_BUFLEN ];
691 /* this lock is required; however,
692 * it's invoked only when logging is on */
693 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
694 snprintf( buf, sizeof( buf ),
695 "retrying URI=\"%s\" DN=\"%s\"",
697 BER_BVISNULL( &msc->msc_bound_ndn ) ?
698 "" : msc->msc_bound_ndn.bv_val );
699 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
701 Debug( LDAP_DEBUG_ANY,
702 "%s meta_back_retry[%d]: %s.\n",
703 op->o_log_prefix, candidate, buf );
706 meta_clear_one_candidate( op, mc, candidate );
707 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
709 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
711 /* mc here must be the regular mc, reset and ready for init */
712 rc = meta_back_init_one_conn( op, rs, mc, candidate,
713 LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 );
715 /* restore the "binding" flag, in case */
717 LDAP_BACK_CONN_BINDING_SET( msc );
720 if ( rc == LDAP_SUCCESS ) {
722 rc = meta_back_single_dobind( op, rs, mcp, candidate,
723 sendok, mt->mt_nretries, 0 );
725 Debug( LDAP_DEBUG_ANY,
726 "%s meta_back_retry[%d]: "
727 "meta_back_single_dobind=%d\n",
728 op->o_log_prefix, candidate, rc );
729 if ( rc == LDAP_SUCCESS ) {
730 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
731 !BER_BVISEMPTY( &msc->msc_bound_ndn ) )
733 LDAP_BACK_CONN_ISBOUND_SET( msc );
736 LDAP_BACK_CONN_ISANON_SET( msc );
739 /* when bound, dispose of the "binding" flag */
741 LDAP_BACK_CONN_BINDING_CLEAR( msc );
746 /* don't send twice */
747 sendok &= ~LDAP_BACK_SENDERR;
750 if ( rc != LDAP_SUCCESS ) {
751 SlapReply *candidates = meta_back_candidates_get( op );
753 candidates[ candidate ].sr_err = rc;
755 if ( *mcp != NULL ) {
756 if ( mc->mc_refcnt == 1 ) {
758 LDAP_BACK_CONN_BINDING_CLEAR( msc );
760 (void)meta_clear_one_candidate( op, mc, candidate );
763 LDAP_BACK_CONN_TAINTED_SET( mc );
764 /* only release if mandatory; otherwise
765 * let the caller do what's best before
767 if ( META_BACK_ONERR_STOP( mi ) ) {
768 meta_back_release_conn_lock( mi, mc, 0 );
772 #if META_BACK_PRINT_CONNTREE > 0
773 meta_back_print_conntree( mi, ">>> meta_back_retry" );
774 #endif /* META_BACK_PRINT_CONNTREE */
776 /* FIXME: could be done better, reworking meta_back_release_conn_lock() */
777 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
778 if ( mc->mc_q.tqe_prev != NULL ) {
779 assert( LDAP_BACK_CONN_CACHED( mc ) );
780 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
781 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
783 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
784 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
787 assert( !LDAP_BACK_CONN_CACHED( mc ) );
791 /* FIXME: check if in tree, for consistency? */
792 (void)avl_delete( &mi->mi_conninfo.lai_tree,
793 ( caddr_t )mc, meta_back_conndnmc_cmp );
795 LDAP_BACK_CONN_CACHED_CLEAR( mc );
797 #if META_BACK_PRINT_CONNTREE > 0
798 meta_back_print_conntree( mi, "<<< meta_back_retry" );
799 #endif /* META_BACK_PRINT_CONNTREE */
803 if ( sendok & LDAP_BACK_SENDERR ) {
805 rs->sr_text = "Unable to retry";
806 send_ldap_result( op, rs );
810 if ( quarantine && META_BACK_TGT_QUARANTINE( mt ) ) {
811 meta_back_quarantine( op, rs, candidate );
814 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
816 return rc == LDAP_SUCCESS ? 1 : 0;
820 * callback for unique candidate selection
823 meta_back_conn_cb( Operation *op, SlapReply *rs )
825 assert( op->o_tag == LDAP_REQ_SEARCH );
827 switch ( rs->sr_type ) {
829 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
845 meta_back_get_candidate(
850 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
854 * tries to get a unique candidate
855 * (takes care of default target)
857 candidate = meta_back_select_unique_candidate( mi, ndn );
860 * if any is found, inits the connection
862 if ( candidate == META_TARGET_NONE ) {
863 rs->sr_err = LDAP_NO_SUCH_OBJECT;
864 rs->sr_text = "No suitable candidate target found";
866 } else if ( candidate == META_TARGET_MULTIPLE ) {
868 SlapReply rs2 = { 0 };
869 slap_callback cb2 = { 0 };
872 /* try to get a unique match for the request ndn
873 * among the multiple candidates available */
874 op2.o_tag = LDAP_REQ_SEARCH;
876 op2.o_req_ndn = *ndn;
877 op2.ors_scope = LDAP_SCOPE_BASE;
878 op2.ors_deref = LDAP_DEREF_NEVER;
879 op2.ors_attrs = slap_anlist_no_attrs;
880 op2.ors_attrsonly = 0;
881 op2.ors_limit = NULL;
883 op2.ors_tlimit = SLAP_NO_LIMIT;
885 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
886 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
888 op2.o_callback = &cb2;
889 cb2.sc_response = meta_back_conn_cb;
890 cb2.sc_private = (void *)&candidate;
892 rc = op->o_bd->be_search( &op2, &rs2 );
894 switch ( rs2.sr_err ) {
897 rs->sr_err = rs2.sr_err;
900 case LDAP_SIZELIMIT_EXCEEDED:
901 /* if multiple candidates can serve the operation,
902 * and a default target is defined, and it is
903 * a candidate, try using it (FIXME: YMMV) */
904 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
905 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
906 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
908 candidate = mi->mi_defaulttarget;
909 rs->sr_err = LDAP_SUCCESS;
913 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
914 rs->sr_text = "Unable to select unique candidate target";
920 rs->sr_err = LDAP_SUCCESS;
926 static void *meta_back_candidates_dummy;
929 meta_back_candidates_keyfree(
933 metacandidates_t *mc = (metacandidates_t *)data;
935 ber_memfree_x( mc->mc_candidates, NULL );
936 ber_memfree_x( data, NULL );
940 meta_back_candidates_get( Operation *op )
942 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
943 metacandidates_t *mc;
945 if ( op->o_threadctx ) {
948 ldap_pvt_thread_pool_getkey( op->o_threadctx,
949 &meta_back_candidates_dummy, &data, NULL );
950 mc = (metacandidates_t *)data;
953 mc = mi->mi_candidates;
957 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
958 mc->mc_ntargets = mi->mi_ntargets;
959 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
960 if ( op->o_threadctx ) {
964 ldap_pvt_thread_pool_setkey( op->o_threadctx,
965 &meta_back_candidates_dummy, data,
966 meta_back_candidates_keyfree,
970 mi->mi_candidates = mc;
973 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
974 /* NOTE: in the future, may want to allow back-config
975 * to add/remove targets from back-meta... */
976 mc->mc_candidates = ch_realloc( mc->mc_candidates,
977 sizeof( SlapReply ) * mi->mi_ntargets );
978 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
979 sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
980 mc->mc_ntargets = mi->mi_ntargets;
983 return mc->mc_candidates;
989 * Prepares the connection structure
993 * - determine what DN is being requested:
995 * op requires candidate checks
997 * add unique parent of o_req_ndn
998 * bind unique^*[/all] o_req_ndn [no check]
999 * compare unique^+ o_req_ndn
1000 * delete unique o_req_ndn
1001 * modify unique o_req_ndn
1002 * search any o_req_ndn
1003 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
1005 * - for ops that require the candidate to be unique, in case of multiple
1006 * occurrences an internal search with sizeLimit=1 is performed
1007 * if a unique candidate can actually be determined. If none is found,
1008 * the operation aborts; if multiple are found, the default target
1009 * is used if defined and candidate; otherwise the operation aborts.
1011 * *^note: actually, the bind operation is handled much like a search;
1012 * i.e. the bind is broadcast to all candidate targets.
1014 * +^note: actually, the compare operation is handled much like a search;
1015 * i.e. the compare is broadcast to all candidate targets, while checking
1016 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
1024 ldap_back_send_t sendok )
1026 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1027 metaconn_t *mc = NULL,
1029 int cached = META_TARGET_NONE,
1030 i = META_TARGET_NONE,
1036 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
1040 META_DNTYPE_NEWPARENT
1041 } dn_type = META_DNTYPE_ENTRY;
1042 struct berval ndn = op->o_req_ndn,
1045 SlapReply *candidates = meta_back_candidates_get( op );
1047 /* Internal searches are privileged and shared. So is root. */
1048 if ( ( !BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ALWAYS( mi ) )
1049 || ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ANON( mi ) )
1050 || op->o_do_not_cache || be_isroot( op ) )
1052 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
1053 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
1054 LDAP_BACK_PCONN_ROOTDN_SET( &mc_curr, op );
1056 } else if ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_NOANON( mi ) )
1058 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1059 BER_BVSTR( &mc_curr.mc_local_ndn, "" );
1060 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1063 mc_curr.mc_local_ndn = op->o_ndn;
1065 /* Explicit binds must not be shared */
1066 if ( !BER_BVISEMPTY( &op->o_ndn )
1067 || op->o_tag == LDAP_REQ_BIND
1068 || SLAP_IS_AUTHZ_BACKEND( op ) )
1070 mc_curr.mc_conn = op->o_conn;
1073 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1074 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1078 /* Explicit Bind requests always get their own conn */
1079 if ( sendok & LDAP_BACK_BINDING ) {
1080 mc_curr.mc_conn = op->o_conn;
1083 /* Searches for a metaconn in the avl tree */
1085 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1086 if ( LDAP_BACK_PCONN_ISPRIV( &mc_curr ) ) {
1087 /* lookup a conn that's not binding */
1088 LDAP_TAILQ_FOREACH( mc,
1089 &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv,
1092 if ( !LDAP_BACK_CONN_BINDING( mc ) && mc->mc_refcnt == 0 ) {
1098 if ( mc != LDAP_TAILQ_LAST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1099 metaconn_t, mc_q ) )
1101 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1103 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1104 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1108 } else if ( !LDAP_BACK_USE_TEMPORARIES( mi )
1109 && mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_num == mi->mi_conn_priv_max )
1111 mc = LDAP_TAILQ_FIRST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv );
1116 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1117 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1121 /* catch taint errors */
1122 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1124 /* Don't reuse connections while they're still binding
1125 * NOTE: only makes sense for binds */
1126 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1127 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1128 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1130 ldap_pvt_thread_yield();
1134 /* release conn, and create a temporary */
1138 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
1139 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
1141 #if META_BACK_PRINT_CONNTREE > 0
1142 meta_back_print_conntree( mi,
1143 ">>> meta_back_getconn(expired)" );
1144 #endif /* META_BACK_PRINT_CONNTREE */
1146 /* don't let anyone else use this expired connection */
1147 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1148 if ( mc->mc_q.tqe_prev != NULL ) {
1149 assert( LDAP_BACK_CONN_CACHED( mc ) );
1150 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1151 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1153 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1154 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1157 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1161 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1162 (caddr_t)mc, meta_back_conndnmc_cmp );
1165 #if META_BACK_PRINT_CONNTREE > 0
1166 meta_back_print_conntree( mi,
1167 "<<< meta_back_getconn(expired)" );
1168 #endif /* META_BACK_PRINT_CONNTREE */
1169 LDAP_BACK_CONN_TAINTED_SET( mc );
1170 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1172 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1173 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1174 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1176 Debug( LDAP_DEBUG_TRACE,
1177 "%s meta_back_getconn: mc=%p conn=%s expired (tainted).\n",
1178 op->o_log_prefix, (void *)mc, buf );
1185 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1188 switch ( op->o_tag ) {
1190 /* if we go to selection, the entry must not exist,
1191 * and we must be able to resolve the parent */
1192 dn_type = META_DNTYPE_PARENT;
1193 dnParent( &ndn, &pndn );
1196 case LDAP_REQ_MODRDN:
1197 /* if nnewSuperior is not NULL, it must resolve
1198 * to the same candidate as the req_ndn */
1199 if ( op->orr_nnewSup ) {
1200 dn_type = META_DNTYPE_NEWPARENT;
1205 /* if bound as rootdn, the backend must bind to all targets
1206 * with the administrative identity
1207 * (unless pseoudoroot-bind-defer is TRUE) */
1208 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
1209 op_type = META_OP_REQUIRE_ALL;
1213 case LDAP_REQ_COMPARE:
1214 case LDAP_REQ_DELETE:
1215 case LDAP_REQ_MODIFY:
1216 /* just a unique candidate */
1219 case LDAP_REQ_SEARCH:
1220 /* allow multiple candidates for the searchBase */
1221 op_type = META_OP_ALLOW_MULTIPLE;
1225 /* right now, just break (exop?) */
1230 * require all connections ...
1232 if ( op_type == META_OP_REQUIRE_ALL ) {
1234 /* Looks like we didn't get a bind. Open a new session... */
1236 assert( new_conn == 0 );
1237 mc = metaconn_alloc( op );
1238 mc->mc_conn = mc_curr.mc_conn;
1239 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1241 if ( sendok & LDAP_BACK_BINDING ) {
1242 LDAP_BACK_CONN_BINDING_SET( mc );
1244 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1245 LDAP_BACK_CONN_ISPRIV_SET( mc );
1247 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1248 LDAP_BACK_CONN_ISANON_SET( mc );
1252 /* TODO: if any of the connections is binding,
1253 * release mc and create a new one */
1256 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1258 * The target is activated; if needed, it is
1261 candidates[ i ].sr_err = meta_back_init_one_conn( op,
1262 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1263 LDAP_BACK_DONTSEND, !new_conn );
1264 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1265 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
1266 LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
1268 META_CANDIDATE_SET( &candidates[ i ] );
1274 * FIXME: in case one target cannot
1275 * be init'd, should the other ones
1278 META_CANDIDATE_RESET( &candidates[ i ] );
1279 err = candidates[ i ].sr_err;
1284 if ( ncandidates == 0 ) {
1287 meta_back_conn_free( mc );
1290 meta_back_release_conn( mi, mc );
1293 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1294 rs->sr_text = "Unable to select valid candidates";
1296 if ( sendok & LDAP_BACK_SENDERR ) {
1297 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1298 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1300 send_ldap_result( op, rs );
1301 rs->sr_matched = NULL;
1311 * looks in cache, if any
1313 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1314 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1317 if ( op_type == META_OP_REQUIRE_SINGLE ) {
1318 metatarget_t *mt = NULL;
1319 metasingleconn_t *msc = NULL;
1323 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1324 META_CANDIDATE_RESET( &candidates[ j ] );
1328 * tries to get a unique candidate
1329 * (takes care of default target)
1331 if ( i == META_TARGET_NONE ) {
1332 i = meta_back_get_candidate( op, rs, &ndn );
1334 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1335 i = meta_back_get_candidate( op, rs, &pndn );
1338 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1340 meta_back_release_conn( mi, mc );
1343 if ( sendok & LDAP_BACK_SENDERR ) {
1344 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1345 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1347 send_ldap_result( op, rs );
1348 rs->sr_matched = NULL;
1355 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1358 meta_back_release_conn( mi, mc );
1361 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1362 rs->sr_text = "Cross-target rename not supported";
1363 if ( sendok & LDAP_BACK_SENDERR ) {
1364 send_ldap_result( op, rs );
1370 Debug( LDAP_DEBUG_TRACE,
1371 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1372 i, op->o_req_ndn.bv_val, 0 );
1375 /* Retries searching for a metaconn in the avl tree
1376 * the reason is that the connection might have been
1377 * created by meta_back_get_candidate() */
1378 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1380 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1381 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1382 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1384 /* catch taint errors */
1385 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1387 /* Don't reuse connections while they're still binding */
1388 if ( META_BACK_CONN_CREATING( &mc->mc_conns[ i ] )
1389 || LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) )
1391 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1392 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1393 ldap_pvt_thread_yield();
1403 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1406 /* Looks like we didn't get a bind. Open a new session... */
1408 assert( new_conn == 0 );
1409 mc = metaconn_alloc( op );
1410 mc->mc_conn = mc_curr.mc_conn;
1411 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1413 if ( sendok & LDAP_BACK_BINDING ) {
1414 LDAP_BACK_CONN_BINDING_SET( mc );
1416 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1417 LDAP_BACK_CONN_ISPRIV_SET( mc );
1419 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1420 LDAP_BACK_CONN_ISANON_SET( mc );
1426 * Clear all other candidates
1428 ( void )meta_clear_unused_candidates( op, i );
1430 mt = mi->mi_targets[ i ];
1431 msc = &mc->mc_conns[ i ];
1434 * The target is activated; if needed, it is
1435 * also init'd. In case of error, meta_back_init_one_conn
1436 * sends the appropriate result.
1438 err = meta_back_init_one_conn( op, rs, mc, i,
1439 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn );
1440 if ( err != LDAP_SUCCESS ) {
1442 * FIXME: in case one target cannot
1443 * be init'd, should the other ones
1446 META_CANDIDATE_RESET( &candidates[ i ] );
1449 meta_back_conn_free( mc );
1452 meta_back_release_conn( mi, mc );
1457 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
1458 LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
1461 candidates[ i ].sr_err = LDAP_SUCCESS;
1462 META_CANDIDATE_SET( &candidates[ i ] );
1470 * if no unique candidate ...
1474 /* Looks like we didn't get a bind. Open a new session... */
1476 assert( new_conn == 0 );
1477 mc = metaconn_alloc( op );
1478 mc->mc_conn = mc_curr.mc_conn;
1479 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1481 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1482 LDAP_BACK_CONN_ISPRIV_SET( mc );
1484 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1485 LDAP_BACK_CONN_ISANON_SET( mc );
1489 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1490 metatarget_t *mt = mi->mi_targets[ i ];
1492 META_CANDIDATE_RESET( &candidates[ i ] );
1495 || meta_back_is_candidate( mt, &op->o_req_ndn,
1496 LDAP_SCOPE_SUBTREE ) )
1500 * The target is activated; if needed, it is
1503 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1504 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1505 LDAP_BACK_DONTSEND, !new_conn );
1506 candidates[ i ].sr_err = lerr;
1507 if ( lerr == LDAP_SUCCESS ) {
1508 META_CANDIDATE_SET( &candidates[ i ] );
1511 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1512 op->o_log_prefix, i, 0 );
1514 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1515 META_CANDIDATE_SET( &candidates[ i ] );
1517 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1518 op->o_log_prefix, i,
1519 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1524 * FIXME: in case one target cannot
1525 * be init'd, should the other ones
1529 ( void )meta_clear_one_candidate( op, mc, i );
1531 /* leave the target candidate, but record the error for later use */
1534 if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1535 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined err=%d\n",
1536 op->o_log_prefix, i, lerr );
1539 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed err=%d\n",
1540 op->o_log_prefix, i, lerr );
1543 if ( META_BACK_ONERR_STOP( mi ) ) {
1544 if ( sendok & LDAP_BACK_SENDERR ) {
1545 send_ldap_result( op, rs );
1549 meta_back_conn_free( mc );
1552 meta_back_release_conn( mi, mc );
1563 ( void )meta_clear_one_candidate( op, mc, i );
1568 if ( ncandidates == 0 ) {
1571 meta_back_conn_free( mc );
1574 meta_back_release_conn( mi, mc );
1577 if ( rs->sr_err == LDAP_SUCCESS ) {
1578 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1579 rs->sr_text = "Unable to select valid candidates";
1582 if ( sendok & LDAP_BACK_SENDERR ) {
1583 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1584 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1586 send_ldap_result( op, rs );
1587 rs->sr_matched = NULL;
1595 /* clear out meta_back_init_one_conn non-fatal errors */
1596 rs->sr_err = LDAP_SUCCESS;
1599 /* touch the timestamp */
1600 if ( mi->mi_idle_timeout != 0 ) {
1601 mc->mc_time = op->o_time;
1605 if ( mi->mi_conn_ttl ) {
1606 mc->mc_create_time = op->o_time;
1610 * Inserts the newly created metaconn in the avl tree
1612 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1613 #if META_BACK_PRINT_CONNTREE > 0
1614 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1615 #endif /* META_BACK_PRINT_CONNTREE */
1618 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1619 if ( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num < mi->mi_conn_priv_max ) {
1620 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1621 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num++;
1622 LDAP_BACK_CONN_CACHED_SET( mc );
1625 LDAP_BACK_CONN_TAINTED_SET( mc );
1629 } else if ( !( sendok & LDAP_BACK_BINDING ) ) {
1630 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1631 meta_back_conndn_cmp, meta_back_conndn_dup );
1632 LDAP_BACK_CONN_CACHED_SET( mc );
1635 #if META_BACK_PRINT_CONNTREE > 0
1636 meta_back_print_conntree( mi, "<<< meta_back_getconn" );
1637 #endif /* META_BACK_PRINT_CONNTREE */
1638 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1640 if ( !LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1642 * Err could be -1 in case a duplicate metaconn is inserted
1649 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1650 /* duplicate: free and try to get the newly created one */
1651 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1653 meta_back_conn_free( mc );
1659 LDAP_BACK_CONN_TAINTED_SET( mc );
1663 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1664 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1665 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1666 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1668 Debug( LDAP_DEBUG_ANY,
1669 "%s meta_back_getconn: candidates=%d conn=%s insert failed\n",
1670 op->o_log_prefix, ncandidates, buf );
1674 meta_back_conn_free( mc );
1676 rs->sr_err = LDAP_OTHER;
1677 rs->sr_text = "Proxy bind collision";
1678 if ( sendok & LDAP_BACK_SENDERR ) {
1679 send_ldap_result( op, rs );
1685 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1686 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1687 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1689 Debug( LDAP_DEBUG_TRACE,
1690 "%s meta_back_getconn: candidates=%d conn=%s inserted\n",
1691 op->o_log_prefix, ncandidates, buf );
1695 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1696 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1697 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1699 Debug( LDAP_DEBUG_TRACE,
1700 "%s meta_back_getconn: candidates=%d conn=%s fetched\n",
1701 op->o_log_prefix, ncandidates, buf );
1709 meta_back_release_conn_lock(
1714 assert( mc != NULL );
1717 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1719 assert( mc->mc_refcnt > 0 );
1721 /* NOTE: the connection is removed if either it is tainted
1722 * or if it is shared and no one else is using it. This needs
1723 * to occur because for intrinsic reasons cached connections
1724 * that are not privileged would live forever and pollute
1725 * the connection space (and eat up resources). Maybe this
1726 * should be configurable... */
1727 if ( LDAP_BACK_CONN_TAINTED( mc ) || !LDAP_BACK_CONN_CACHED( mc ) ) {
1728 #if META_BACK_PRINT_CONNTREE > 0
1729 meta_back_print_conntree( mi, ">>> meta_back_release_conn" );
1730 #endif /* META_BACK_PRINT_CONNTREE */
1732 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1733 if ( mc->mc_q.tqe_prev != NULL ) {
1734 assert( LDAP_BACK_CONN_CACHED( mc ) );
1735 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1736 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1737 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1738 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1741 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1744 } else if ( LDAP_BACK_CONN_CACHED( mc ) ) {
1747 tmpmc = avl_delete( &mi->mi_conninfo.lai_tree,
1748 ( caddr_t )mc, meta_back_conndnmc_cmp );
1750 /* Overparanoid, but useful... */
1751 assert( tmpmc == NULL || tmpmc == mc );
1754 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1756 #if META_BACK_PRINT_CONNTREE > 0
1757 meta_back_print_conntree( mi, "<<< meta_back_release_conn" );
1758 #endif /* META_BACK_PRINT_CONNTREE */
1760 if ( mc->mc_refcnt == 0 ) {
1761 meta_back_conn_free( mc );
1766 if ( mc != NULL && LDAP_BACK_CONN_BINDING( mc ) ) {
1767 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1771 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1776 meta_back_quarantine(
1781 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1782 metatarget_t *mt = mi->mi_targets[ candidate ];
1784 slap_retry_info_t *ri = &mt->mt_quarantine;
1786 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1788 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1789 time_t new_last = slap_get_time();
1791 switch ( mt->mt_isquarantined ) {
1792 case LDAP_BACK_FQ_NO:
1793 if ( ri->ri_last == new_last ) {
1797 Debug( LDAP_DEBUG_ANY,
1798 "%s meta_back_quarantine[%d]: enter.\n",
1799 op->o_log_prefix, candidate, 0 );
1805 case LDAP_BACK_FQ_RETRYING:
1806 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1807 char buf[ SLAP_TEXT_BUFLEN ];
1809 snprintf( buf, sizeof( buf ),
1810 "meta_back_quarantine[%d]: block #%d try #%d failed",
1811 candidate, ri->ri_idx, ri->ri_count );
1812 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1813 op->o_log_prefix, buf, 0 );
1817 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1818 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1829 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1830 ri->ri_last = new_last;
1832 } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1833 Debug( LDAP_DEBUG_ANY,
1834 "%s meta_back_quarantine[%d]: exit.\n",
1835 op->o_log_prefix, candidate, 0 );
1837 if ( mi->mi_quarantine_f ) {
1838 (void)mi->mi_quarantine_f( mi, candidate,
1839 mi->mi_quarantine_p );
1844 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1848 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );