2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2007 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
28 #include <ac/socket.h>
29 #include <ac/string.h>
34 #include "../back-ldap/back-ldap.h"
35 #include "back-meta.h"
38 * meta_back_conndn_cmp
40 * compares two struct metaconn based on the value of the conn pointer
41 * and of the local DN; used by avl stuff
48 metaconn_t *mc1 = ( metaconn_t * )c1;
49 metaconn_t *mc2 = ( metaconn_t * )c2;
52 /* If local DNs don't match, it is definitely not a match */
53 /* For shared sessions, conn is NULL. Only explicitly
54 * bound sessions will have non-NULL conn.
56 rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
58 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
65 * meta_back_conndnmc_cmp
67 * compares two struct metaconn based on the value of the conn pointer,
68 * the local DN and the struct pointer; used by avl stuff
71 meta_back_conndnmc_cmp(
75 metaconn_t *mc1 = ( metaconn_t * )c1;
76 metaconn_t *mc2 = ( metaconn_t * )c2;
79 /* If local DNs don't match, it is definitely not a match */
80 /* For shared sessions, conn is NULL. Only explicitly
81 * bound sessions will have non-NULL conn.
83 rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
85 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
87 rc = SLAP_PTRCMP( mc1, mc2 );
97 * compares two struct metaconn based on the value of the conn pointer;
105 metaconn_t *mc1 = ( metaconn_t * )c1;
106 metaconn_t *mc2 = ( metaconn_t * )c2;
108 /* For shared sessions, conn is NULL. Only explicitly
109 * bound sessions will have non-NULL conn.
111 return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
115 * meta_back_conndn_dup
117 * returns -1 in case a duplicate struct metaconn has been inserted;
121 meta_back_conndn_dup(
125 metaconn_t *mc1 = ( metaconn_t * )c1;
126 metaconn_t *mc2 = ( metaconn_t * )c2;
128 /* Cannot have more than one shared session with same DN */
129 if ( mc1->mc_conn == mc2->mc_conn &&
130 dn_match( &mc1->mc_local_ndn, &mc2->mc_local_ndn ) )
139 * Debug stuff (got it from libavl)
141 #if META_BACK_PRINT_CONNTREE > 0
143 meta_back_ravl_print( Avlnode *root, int depth )
152 meta_back_ravl_print( root->avl_right, depth + 1 );
154 for ( i = 0; i < depth; i++ ) {
155 fprintf( stderr, "-" );
158 mc = (metaconn_t *)root->avl_data;
159 fprintf( stderr, "mc=%p local=\"%s\" conn=%p %s refcnt=%d%s\n",
161 mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "",
163 avl_bf2str( root->avl_bf ), mc->mc_refcnt,
164 LDAP_BACK_CONN_TAINTED( mc ) ? " tainted" : "" );
166 meta_back_ravl_print( root->avl_left, depth + 1 );
169 /* NOTE: duplicate from back-ldap/bind.c */
170 static char* priv2str[] = {
181 meta_back_print_conntree( metainfo_t *mi, char *msg )
185 fprintf( stderr, "========> %s\n", msg );
187 for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
191 fprintf( stderr, " %s[%d]\n", priv2str[ c ], mi->mi_conn_priv[ c ].mic_num );
193 LDAP_TAILQ_FOREACH( mc, &mi->mi_conn_priv[ c ].mic_priv, mc_q )
195 fprintf( stderr, " [%d] mc=%p local=\"%s\" conn=%p refcnt=%d flags=0x%08x\n",
198 mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "",
199 (void *)mc->mc_conn, mc->mc_refcnt, mc->msc_mscflags );
204 if ( mi->mi_conninfo.lai_tree == NULL ) {
205 fprintf( stderr, "\t(empty)\n" );
208 meta_back_ravl_print( mi->mi_conninfo.lai_tree, 0 );
211 fprintf( stderr, "<======== %s\n", msg );
213 #endif /* META_BACK_PRINT_CONNTREE */
221 * Allocates a connection structure, making room for all the referenced targets
227 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
229 int ntargets = mi->mi_ntargets;
231 assert( ntargets > 0 );
233 /* malloc all in one */
234 mc = ( metaconn_t * )ch_calloc( 1, sizeof( metaconn_t )
235 + sizeof( metasingleconn_t ) * ( ntargets - 1 ) );
242 mc->mc_authz_target = META_BOUND_NONE;
249 * meta_back_init_one_conn
251 * Initializes one connection
254 meta_back_init_one_conn(
260 ldap_back_send_t sendok,
263 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
264 metatarget_t *mt = mi->mi_targets[ candidate ];
265 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
268 int isauthz = ( candidate == mc->mc_authz_target );
272 #endif /* HAVE_TLS */
274 /* if the server is quarantined, and
275 * - the current interval did not expire yet, or
276 * - no more retries should occur,
277 * don't return the connection */
278 if ( mt->mt_isquarantined ) {
279 slap_retry_info_t *ri = &mt->mt_quarantine;
282 if ( mt->mt_quarantine.ri_interval ) {
283 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
284 if ( mt->mt_isquarantined == LDAP_BACK_FQ_YES ) {
285 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
286 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
288 if ( StatslogTest( LDAP_DEBUG_ANY ) ) {
289 char buf[ SLAP_TEXT_BUFLEN ];
291 snprintf( buf, sizeof( buf ),
292 "meta_back_init_one_conn[%d]: quarantine "
293 "retry block #%d try #%d",
294 candidate, ri->ri_idx, ri->ri_count );
295 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
296 op->o_log_prefix, buf, 0 );
300 mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
302 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
306 rs->sr_err = LDAP_UNAVAILABLE;
307 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
308 rs->sr_text = "Target is quarantined";
309 send_ldap_result( op, rs );
317 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
323 if ( LDAP_BACK_CONN_ISBOUND( msc )
324 || LDAP_BACK_CONN_ISANON( msc ) )
326 assert( msc->msc_ld != NULL );
327 rs->sr_err = LDAP_SUCCESS;
330 } else if ( META_BACK_CONN_CREATING( msc )
331 || LDAP_BACK_CONN_BINDING( msc ) )
333 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
335 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
338 ldap_pvt_thread_yield();
342 /* sounds more appropriate */
343 rs->sr_err = LDAP_BUSY;
344 rs->sr_text = "No connections to target are available";
347 } else if ( META_BACK_CONN_INITED( msc ) ) {
348 assert( msc->msc_ld != NULL );
349 rs->sr_err = LDAP_SUCCESS;
356 META_BACK_CONN_CREATING_SET( msc );
360 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
364 if ( rs->sr_err != LDAP_SUCCESS
366 && ( sendok & LDAP_BACK_SENDERR ) )
368 send_ldap_result( op, rs );
374 assert( msc->msc_ld == NULL );
377 * Attempts to initialize the connection to the target ds
379 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
380 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
382 is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
383 #endif /* HAVE_TLS */
384 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
385 if ( rs->sr_err != LDAP_SUCCESS ) {
390 * Set LDAP version. This will always succeed: If the client
391 * bound with a particular version, then so can we.
393 if ( mt->mt_version != 0 ) {
394 version = mt->mt_version;
396 } else if ( op->o_conn->c_protocol != 0 ) {
397 version = op->o_conn->c_protocol;
400 version = LDAP_VERSION3;
402 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
404 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
405 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
406 LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
409 /* start TLS ("tls [try-]{start|propagate}" statement) */
410 if ( ( LDAP_BACK_USE_TLS( mi )
411 || ( op->o_conn->c_is_tls
412 && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
415 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
417 * use asynchronous StartTLS; in case, chase referral
418 * FIXME: OpenLDAP does not return referral on StartTLS yet
422 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
423 if ( rs->sr_err == LDAP_SUCCESS ) {
424 LDAPMessage *res = NULL;
425 int rc, nretries = mt->mt_nretries;
428 LDAP_BACK_TV_SET( &tv );
431 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
434 rs->sr_err = LDAP_OTHER;
438 if ( nretries != 0 ) {
439 if ( nretries > 0 ) {
442 LDAP_BACK_TV_SET( &tv );
445 rs->sr_err = LDAP_OTHER;
449 /* only touch when activity actually took place... */
450 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
451 msc->msc_time = op->o_time;
456 if ( rc == LDAP_RES_EXTENDED ) {
457 struct berval *data = NULL;
459 /* NOTE: right now, data is unused, so don't get it */
460 rs->sr_err = ldap_parse_extended_result( msc->msc_ld,
461 res, NULL, NULL /* &data */ , 0 );
462 if ( rs->sr_err == LDAP_SUCCESS ) {
465 /* FIXME: matched? referrals? response controls? */
466 rs->sr_err = ldap_parse_result( msc->msc_ld,
467 res, &err, NULL, NULL, NULL, NULL, 1 );
470 if ( rs->sr_err == LDAP_SUCCESS ) {
474 /* FIXME: in case a referral
475 * is returned, should we try
476 * using it instead of the
478 if ( rs->sr_err == LDAP_SUCCESS ) {
479 ldap_install_tls( msc->msc_ld );
481 } else if ( rs->sr_err == LDAP_REFERRAL ) {
482 /* FIXME: LDAP_OPERATIONS_ERROR? */
483 rs->sr_err = LDAP_OTHER;
484 rs->sr_text = "Unwilling to chase referral "
485 "returned by Start TLS exop";
494 rs->sr_err = LDAP_OTHER;
501 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
503 * use synchronous StartTLS
505 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
506 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
508 /* if StartTLS is requested, only attempt it if the URL
509 * is not "ldaps://"; this may occur not only in case
510 * of misconfiguration, but also when used in the chain
511 * overlay, where the "uri" can be parsed out of a referral */
512 if ( rs->sr_err == LDAP_SERVER_DOWN
513 || ( rs->sr_err != LDAP_SUCCESS
514 && LDAP_BACK_TLS_CRITICAL( mi ) ) )
518 Debug( LDAP_DEBUG_ANY,
519 "### %s meta_back_init_one_conn(TLS) "
520 "ldap_unbind_ext[%d] ld=%p\n",
521 op->o_log_prefix, candidate,
522 (void *)msc->msc_ld );
523 #endif /* DEBUG_205 */
525 /* need to trash a failed Start TLS */
526 meta_clear_one_candidate( op, mc, candidate );
530 #endif /* HAVE_TLS */
533 * Set the network timeout if set
535 if ( mt->mt_network_timeout != 0 ) {
536 struct timeval network_timeout;
538 network_timeout.tv_usec = 0;
539 network_timeout.tv_sec = mt->mt_network_timeout;
541 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
542 (void *)&network_timeout );
546 * If the connection DN is not null, an attempt to rewrite it is made
550 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
551 ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
552 if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
553 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
554 memset( msc->msc_cred.bv_val, 0,
555 msc->msc_cred.bv_len );
557 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
561 ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
565 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
566 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
567 ber_memfree_x( msc->msc_cred.bv_val, NULL );
568 BER_BVZERO( &msc->msc_cred );
570 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
571 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
572 BER_BVZERO( &msc->msc_bound_ndn );
574 if ( !BER_BVISEMPTY( &op->o_ndn )
575 && SLAP_IS_AUTHZ_BACKEND( op )
579 dc.conn = op->o_conn;
584 * Rewrite the bind dn if needed
586 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
587 &msc->msc_bound_ndn ) )
591 Debug( LDAP_DEBUG_ANY,
592 "### %s meta_back_init_one_conn(rewrite) "
593 "ldap_unbind_ext[%d] ld=%p\n",
594 op->o_log_prefix, candidate,
595 (void *)msc->msc_ld );
596 #endif /* DEBUG_205 */
598 /* need to trash a connection not fully established */
599 meta_clear_one_candidate( op, mc, candidate );
603 /* copy the DN if needed */
604 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
605 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
608 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
611 ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
615 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
619 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
621 META_BACK_CONN_CREATING_CLEAR( msc );
622 if ( rs->sr_err == LDAP_SUCCESS ) {
624 * Sets a cookie for the rewrite session
626 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
627 META_BACK_CONN_INITED_SET( msc );
630 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
633 if ( rs->sr_err != LDAP_SUCCESS ) {
634 rs->sr_err = slap_map_api2result( rs );
635 if ( sendok & LDAP_BACK_SENDERR ) {
636 send_ldap_result( op, rs );
646 * Retries one connection
654 ldap_back_send_t sendok )
656 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
657 metatarget_t *mt = mi->mi_targets[ candidate ];
658 metaconn_t *mc = *mcp;
659 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
660 int rc = LDAP_UNAVAILABLE,
664 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
666 assert( !META_BACK_CONN_CREATING( msc ) );
667 binding = LDAP_BACK_CONN_BINDING( msc );
668 LDAP_BACK_CONN_BINDING_CLEAR( msc );
670 assert( mc->mc_refcnt > 0 );
671 if ( mc->mc_refcnt == 1 ) {
672 if ( StatslogTest( LDAP_DEBUG_ANY ) ) {
673 char buf[ SLAP_TEXT_BUFLEN ];
675 /* this lock is required; however,
676 * it's invoked only when logging is on */
677 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
678 snprintf( buf, sizeof( buf ),
679 "retrying URI=\"%s\" DN=\"%s\"",
681 BER_BVISNULL( &msc->msc_bound_ndn ) ?
682 "" : msc->msc_bound_ndn.bv_val );
683 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
685 Debug( LDAP_DEBUG_ANY,
686 "%s meta_back_retry[%d]: %s.\n",
687 op->o_log_prefix, candidate, buf );
690 meta_clear_one_candidate( op, mc, candidate );
691 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
693 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
695 /* mc here must be the regular mc, reset and ready for init */
696 rc = meta_back_init_one_conn( op, rs, mc, candidate,
697 LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 );
699 /* restore the "binding" flag, in case */
701 LDAP_BACK_CONN_BINDING_SET( msc );
704 if ( rc == LDAP_SUCCESS ) {
706 rc = meta_back_single_dobind( op, rs, mcp, candidate,
707 sendok, mt->mt_nretries, 0 );
709 Debug( LDAP_DEBUG_ANY,
710 "%s meta_back_retry[%d]: "
711 "meta_back_single_dobind=%d\n",
712 op->o_log_prefix, candidate, rc );
713 if ( rc == LDAP_SUCCESS ) {
714 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
715 !BER_BVISEMPTY( &msc->msc_bound_ndn ) )
717 LDAP_BACK_CONN_ISBOUND_SET( msc );
720 LDAP_BACK_CONN_ISANON_SET( msc );
723 /* when bound, dispose of the "binding" flag */
725 LDAP_BACK_CONN_BINDING_CLEAR( msc );
730 /* don't send twice */
731 sendok &= ~LDAP_BACK_SENDERR;
734 if ( rc != LDAP_SUCCESS ) {
735 SlapReply *candidates = meta_back_candidates_get( op );
737 candidates[ candidate ].sr_err = rc;
739 if ( *mcp != NULL ) {
740 if ( mc->mc_refcnt == 1 ) {
742 LDAP_BACK_CONN_BINDING_CLEAR( msc );
744 (void)meta_clear_one_candidate( op, mc, candidate );
747 LDAP_BACK_CONN_TAINTED_SET( mc );
748 /* only release if mandatory; otherwise
749 * let the caller do what's best before
751 if ( META_BACK_ONERR_STOP( mi ) ) {
752 meta_back_release_conn_lock( mi, mc, 0 );
756 #if META_BACK_PRINT_CONNTREE > 0
757 meta_back_print_conntree( mi, ">>> meta_back_retry" );
758 #endif /* META_BACK_PRINT_CONNTREE */
760 /* FIXME: could be done better, reworking meta_back_release_conn_lock() */
761 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
762 if ( mc->mc_q.tqe_prev != NULL ) {
763 assert( LDAP_BACK_CONN_CACHED( mc ) );
764 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
765 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
767 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
768 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
771 assert( !LDAP_BACK_CONN_CACHED( mc ) );
775 /* FIXME: check if in tree, for consistency? */
776 (void)avl_delete( &mi->mi_conninfo.lai_tree,
777 ( caddr_t )mc, meta_back_conndnmc_cmp );
779 LDAP_BACK_CONN_CACHED_CLEAR( mc );
781 #if META_BACK_PRINT_CONNTREE > 0
782 meta_back_print_conntree( mi, "<<< meta_back_retry" );
783 #endif /* META_BACK_PRINT_CONNTREE */
787 if ( sendok & LDAP_BACK_SENDERR ) {
789 rs->sr_text = "Unable to retry";
790 send_ldap_result( op, rs );
794 if ( quarantine && META_BACK_TGT_QUARANTINE( mt ) ) {
795 meta_back_quarantine( op, rs, candidate );
798 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
800 return rc == LDAP_SUCCESS ? 1 : 0;
804 * callback for unique candidate selection
807 meta_back_conn_cb( Operation *op, SlapReply *rs )
809 assert( op->o_tag == LDAP_REQ_SEARCH );
811 switch ( rs->sr_type ) {
813 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
829 meta_back_get_candidate(
834 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
838 * tries to get a unique candidate
839 * (takes care of default target)
841 candidate = meta_back_select_unique_candidate( mi, ndn );
844 * if any is found, inits the connection
846 if ( candidate == META_TARGET_NONE ) {
847 rs->sr_err = LDAP_NO_SUCH_OBJECT;
848 rs->sr_text = "No suitable candidate target found";
850 } else if ( candidate == META_TARGET_MULTIPLE ) {
853 SlapReply rs2 = { 0 };
854 slap_callback cb2 = { 0 };
857 /* try to get a unique match for the request ndn
858 * among the multiple candidates available */
859 op2.o_tag = LDAP_REQ_SEARCH;
861 op2.o_req_ndn = *ndn;
862 op2.ors_scope = LDAP_SCOPE_BASE;
863 op2.ors_deref = LDAP_DEREF_NEVER;
864 op2.ors_attrs = slap_anlist_no_attrs;
865 op2.ors_attrsonly = 0;
866 op2.ors_limit = NULL;
868 op2.ors_tlimit = SLAP_NO_LIMIT;
870 f.f_choice = LDAP_FILTER_PRESENT;
871 f.f_desc = slap_schema.si_ad_objectClass;
873 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
875 op2.o_callback = &cb2;
876 cb2.sc_response = meta_back_conn_cb;
877 cb2.sc_private = (void *)&candidate;
879 rc = op->o_bd->be_search( &op2, &rs2 );
881 switch ( rs2.sr_err ) {
884 rs->sr_err = rs2.sr_err;
887 case LDAP_SIZELIMIT_EXCEEDED:
888 /* if multiple candidates can serve the operation,
889 * and a default target is defined, and it is
890 * a candidate, try using it (FIXME: YMMV) */
891 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
892 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
893 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
895 candidate = mi->mi_defaulttarget;
896 rs->sr_err = LDAP_SUCCESS;
900 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
901 rs->sr_text = "Unable to select unique candidate target";
907 rs->sr_err = LDAP_SUCCESS;
913 static void *meta_back_candidates_dummy;
916 meta_back_candidates_keyfree(
920 metacandidates_t *mc = (metacandidates_t *)data;
922 ber_memfree_x( mc->mc_candidates, NULL );
923 ber_memfree_x( data, NULL );
927 meta_back_candidates_get( Operation *op )
929 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
930 metacandidates_t *mc;
932 if ( op->o_threadctx ) {
935 ldap_pvt_thread_pool_getkey( op->o_threadctx,
936 &meta_back_candidates_dummy, &data, NULL );
937 mc = (metacandidates_t *)data;
940 mc = mi->mi_candidates;
944 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
945 mc->mc_ntargets = mi->mi_ntargets;
946 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
947 if ( op->o_threadctx ) {
951 ldap_pvt_thread_pool_setkey( op->o_threadctx,
952 &meta_back_candidates_dummy, data,
953 meta_back_candidates_keyfree );
956 mi->mi_candidates = mc;
959 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
960 /* NOTE: in the future, may want to allow back-config
961 * to add/remove targets from back-meta... */
962 mc->mc_candidates = ch_realloc( mc->mc_candidates,
963 sizeof( SlapReply ) * mi->mi_ntargets );
964 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
965 sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
966 mc->mc_ntargets = mi->mi_ntargets;
969 return mc->mc_candidates;
975 * Prepares the connection structure
979 * - determine what DN is being requested:
981 * op requires candidate checks
983 * add unique parent of o_req_ndn
984 * bind unique^*[/all] o_req_ndn [no check]
985 * compare unique^+ o_req_ndn
986 * delete unique o_req_ndn
987 * modify unique o_req_ndn
988 * search any o_req_ndn
989 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
991 * - for ops that require the candidate to be unique, in case of multiple
992 * occurrences an internal search with sizeLimit=1 is performed
993 * if a unique candidate can actually be determined. If none is found,
994 * the operation aborts; if multiple are found, the default target
995 * is used if defined and candidate; otherwise the operation aborts.
997 * *^note: actually, the bind operation is handled much like a search;
998 * i.e. the bind is broadcast to all candidate targets.
1000 * +^note: actually, the compare operation is handled much like a search;
1001 * i.e. the compare is broadcast to all candidate targets, while checking
1002 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
1010 ldap_back_send_t sendok )
1012 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1013 metaconn_t *mc = NULL,
1015 int cached = META_TARGET_NONE,
1016 i = META_TARGET_NONE,
1022 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
1026 META_DNTYPE_NEWPARENT
1027 } dn_type = META_DNTYPE_ENTRY;
1028 struct berval ndn = op->o_req_ndn,
1031 SlapReply *candidates = meta_back_candidates_get( op );
1033 /* Internal searches are privileged and shared. So is root. */
1034 if ( ( !BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ALWAYS( mi ) )
1035 || ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ANON( mi ) )
1036 || op->o_do_not_cache || be_isroot( op ) )
1038 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
1039 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
1040 LDAP_BACK_PCONN_ROOTDN_SET( &mc_curr, op );
1042 } else if ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_NOANON( mi ) )
1044 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1045 BER_BVSTR( &mc_curr.mc_local_ndn, "" );
1046 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1049 mc_curr.mc_local_ndn = op->o_ndn;
1051 /* Explicit binds must not be shared */
1052 if ( !BER_BVISEMPTY( &op->o_ndn )
1053 || op->o_tag == LDAP_REQ_BIND
1054 || SLAP_IS_AUTHZ_BACKEND( op ) )
1056 mc_curr.mc_conn = op->o_conn;
1059 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1060 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1064 /* Explicit Bind requests always get their own conn */
1065 if ( sendok & LDAP_BACK_BINDING ) {
1066 mc_curr.mc_conn = op->o_conn;
1069 /* Searches for a metaconn in the avl tree */
1071 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1072 if ( LDAP_BACK_PCONN_ISPRIV( &mc_curr ) ) {
1073 /* lookup a conn that's not binding */
1074 LDAP_TAILQ_FOREACH( mc,
1075 &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv,
1078 if ( !LDAP_BACK_CONN_BINDING( mc ) && mc->mc_refcnt == 0 ) {
1084 if ( mc != LDAP_TAILQ_LAST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1085 metaconn_t, mc_q ) )
1087 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1089 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1090 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1094 } else if ( !LDAP_BACK_USE_TEMPORARIES( mi )
1095 && mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_num == mi->mi_conn_priv_max )
1097 mc = LDAP_TAILQ_FIRST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv );
1102 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1103 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1107 /* catch taint errors */
1108 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1110 /* Don't reuse connections while they're still binding
1111 * NOTE: only makes sense for binds */
1112 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1113 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1114 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1116 ldap_pvt_thread_yield();
1120 /* release conn, and create a temporary */
1124 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
1125 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
1127 #if META_BACK_PRINT_CONNTREE > 0
1128 meta_back_print_conntree( mi,
1129 ">>> meta_back_getconn(expired)" );
1130 #endif /* META_BACK_PRINT_CONNTREE */
1132 /* don't let anyone else use this expired connection */
1133 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1134 if ( mc->mc_q.tqe_prev != NULL ) {
1135 assert( LDAP_BACK_CONN_CACHED( mc ) );
1136 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1137 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1139 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1140 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1143 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1147 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1148 (caddr_t)mc, meta_back_conndnmc_cmp );
1151 #if META_BACK_PRINT_CONNTREE > 0
1152 meta_back_print_conntree( mi,
1153 "<<< meta_back_getconn(expired)" );
1154 #endif /* META_BACK_PRINT_CONNTREE */
1155 LDAP_BACK_CONN_TAINTED_SET( mc );
1156 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1158 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired (tainted).\n",
1159 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc ) );
1165 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1168 switch ( op->o_tag ) {
1170 /* if we go to selection, the entry must not exist,
1171 * and we must be able to resolve the parent */
1172 dn_type = META_DNTYPE_PARENT;
1173 dnParent( &ndn, &pndn );
1176 case LDAP_REQ_MODRDN:
1177 /* if nnewSuperior is not NULL, it must resolve
1178 * to the same candidate as the req_ndn */
1179 if ( op->orr_nnewSup ) {
1180 dn_type = META_DNTYPE_NEWPARENT;
1185 /* if bound as rootdn, the backend must bind to all targets
1186 * with the administrative identity
1187 * (unless pseoudoroot-bind-defer is TRUE) */
1188 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
1189 op_type = META_OP_REQUIRE_ALL;
1193 case LDAP_REQ_COMPARE:
1194 case LDAP_REQ_DELETE:
1195 case LDAP_REQ_MODIFY:
1196 /* just a unique candidate */
1199 case LDAP_REQ_SEARCH:
1200 /* allow multiple candidates for the searchBase */
1201 op_type = META_OP_ALLOW_MULTIPLE;
1205 /* right now, just break (exop?) */
1210 * require all connections ...
1212 if ( op_type == META_OP_REQUIRE_ALL ) {
1214 /* Looks like we didn't get a bind. Open a new session... */
1216 assert( new_conn == 0 );
1217 mc = metaconn_alloc( op );
1218 mc->mc_conn = mc_curr.mc_conn;
1219 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1221 if ( sendok & LDAP_BACK_BINDING ) {
1222 LDAP_BACK_CONN_BINDING_SET( mc );
1224 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1225 LDAP_BACK_CONN_ISPRIV_SET( mc );
1227 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1228 LDAP_BACK_CONN_ISANON_SET( mc );
1232 /* TODO: if any of the connections is binding,
1233 * release mc and create a new one */
1236 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1238 * The target is activated; if needed, it is
1241 candidates[ i ].sr_err = meta_back_init_one_conn( op,
1242 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1243 LDAP_BACK_DONTSEND, !new_conn );
1244 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1245 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
1246 LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
1248 META_CANDIDATE_SET( &candidates[ i ] );
1254 * FIXME: in case one target cannot
1255 * be init'd, should the other ones
1258 META_CANDIDATE_RESET( &candidates[ i ] );
1259 err = candidates[ i ].sr_err;
1264 if ( ncandidates == 0 ) {
1267 meta_back_conn_free( mc );
1270 meta_back_release_conn( mi, mc );
1273 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1274 rs->sr_text = "Unable to select valid candidates";
1276 if ( sendok & LDAP_BACK_SENDERR ) {
1277 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1278 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1280 send_ldap_result( op, rs );
1281 rs->sr_matched = NULL;
1291 * looks in cache, if any
1293 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1294 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1297 if ( op_type == META_OP_REQUIRE_SINGLE ) {
1298 metatarget_t *mt = NULL;
1299 metasingleconn_t *msc = NULL;
1303 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1304 META_CANDIDATE_RESET( &candidates[ j ] );
1308 * tries to get a unique candidate
1309 * (takes care of default target)
1311 if ( i == META_TARGET_NONE ) {
1312 i = meta_back_get_candidate( op, rs, &ndn );
1314 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1315 i = meta_back_get_candidate( op, rs, &pndn );
1318 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1320 meta_back_release_conn( mi, mc );
1323 if ( sendok & LDAP_BACK_SENDERR ) {
1324 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1325 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1327 send_ldap_result( op, rs );
1328 rs->sr_matched = NULL;
1335 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1338 meta_back_release_conn( mi, mc );
1341 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1342 rs->sr_text = "Cross-target rename not supported";
1343 if ( sendok & LDAP_BACK_SENDERR ) {
1344 send_ldap_result( op, rs );
1350 Debug( LDAP_DEBUG_TRACE,
1351 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1352 i, op->o_req_ndn.bv_val, 0 );
1355 /* Retries searching for a metaconn in the avl tree
1356 * the reason is that the connection might have been
1357 * created by meta_back_get_candidate() */
1358 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1360 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1361 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1362 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1364 /* catch taint errors */
1365 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1367 /* Don't reuse connections while they're still binding */
1368 if ( META_BACK_CONN_CREATING( &mc->mc_conns[ i ] )
1369 || LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) )
1371 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1372 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1373 ldap_pvt_thread_yield();
1383 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1386 /* Looks like we didn't get a bind. Open a new session... */
1388 assert( new_conn == 0 );
1389 mc = metaconn_alloc( op );
1390 mc->mc_conn = mc_curr.mc_conn;
1391 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1393 if ( sendok & LDAP_BACK_BINDING ) {
1394 LDAP_BACK_CONN_BINDING_SET( mc );
1396 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1397 LDAP_BACK_CONN_ISPRIV_SET( mc );
1399 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1400 LDAP_BACK_CONN_ISANON_SET( mc );
1406 * Clear all other candidates
1408 ( void )meta_clear_unused_candidates( op, i );
1410 mt = mi->mi_targets[ i ];
1411 msc = &mc->mc_conns[ i ];
1414 * The target is activated; if needed, it is
1415 * also init'd. In case of error, meta_back_init_one_conn
1416 * sends the appropriate result.
1418 err = meta_back_init_one_conn( op, rs, mc, i,
1419 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn );
1420 if ( err != LDAP_SUCCESS ) {
1422 * FIXME: in case one target cannot
1423 * be init'd, should the other ones
1426 META_CANDIDATE_RESET( &candidates[ i ] );
1429 meta_back_conn_free( mc );
1432 meta_back_release_conn( mi, mc );
1437 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
1438 LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
1441 candidates[ i ].sr_err = LDAP_SUCCESS;
1442 META_CANDIDATE_SET( &candidates[ i ] );
1450 * if no unique candidate ...
1454 /* Looks like we didn't get a bind. Open a new session... */
1456 assert( new_conn == 0 );
1457 mc = metaconn_alloc( op );
1458 mc->mc_conn = mc_curr.mc_conn;
1459 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1461 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1462 LDAP_BACK_CONN_ISPRIV_SET( mc );
1464 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1465 LDAP_BACK_CONN_ISANON_SET( mc );
1469 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1470 metatarget_t *mt = mi->mi_targets[ i ];
1472 META_CANDIDATE_RESET( &candidates[ i ] );
1475 || meta_back_is_candidate( mt, &op->o_req_ndn,
1476 LDAP_SCOPE_SUBTREE ) )
1480 * The target is activated; if needed, it is
1483 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1484 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1485 LDAP_BACK_DONTSEND, !new_conn );
1486 candidates[ i ].sr_err = lerr;
1487 if ( lerr == LDAP_SUCCESS ) {
1488 META_CANDIDATE_SET( &candidates[ i ] );
1491 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1492 op->o_log_prefix, i, 0 );
1494 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1495 META_CANDIDATE_SET( &candidates[ i ] );
1497 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1498 op->o_log_prefix, i,
1499 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1504 * FIXME: in case one target cannot
1505 * be init'd, should the other ones
1509 ( void )meta_clear_one_candidate( op, mc, i );
1511 /* leave the target candidate, but record the error for later use */
1514 if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1515 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined err=%d\n",
1516 op->o_log_prefix, i, lerr );
1519 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed err=%d\n",
1520 op->o_log_prefix, i, lerr );
1523 if ( META_BACK_ONERR_STOP( mi ) ) {
1524 if ( sendok & LDAP_BACK_SENDERR ) {
1525 send_ldap_result( op, rs );
1529 meta_back_conn_free( mc );
1532 meta_back_release_conn( mi, mc );
1543 ( void )meta_clear_one_candidate( op, mc, i );
1548 if ( ncandidates == 0 ) {
1551 meta_back_conn_free( mc );
1554 meta_back_release_conn( mi, mc );
1557 if ( rs->sr_err == LDAP_SUCCESS ) {
1558 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1559 rs->sr_text = "Unable to select valid candidates";
1562 if ( sendok & LDAP_BACK_SENDERR ) {
1563 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1564 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1566 send_ldap_result( op, rs );
1567 rs->sr_matched = NULL;
1575 /* clear out meta_back_init_one_conn non-fatal errors */
1576 rs->sr_err = LDAP_SUCCESS;
1579 /* touch the timestamp */
1580 if ( mi->mi_idle_timeout != 0 ) {
1581 mc->mc_time = op->o_time;
1585 if ( mi->mi_conn_ttl ) {
1586 mc->mc_create_time = op->o_time;
1590 * Inserts the newly created metaconn in the avl tree
1592 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1593 #if META_BACK_PRINT_CONNTREE > 0
1594 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1595 #endif /* META_BACK_PRINT_CONNTREE */
1597 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1598 if ( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num < mi->mi_conn_priv_max ) {
1599 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1600 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num++;
1601 LDAP_BACK_CONN_CACHED_SET( mc );
1604 LDAP_BACK_CONN_TAINTED_SET( mc );
1609 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1610 meta_back_conndn_cmp, meta_back_conndn_dup );
1611 LDAP_BACK_CONN_CACHED_SET( mc );
1614 #if META_BACK_PRINT_CONNTREE > 0
1615 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1616 #endif /* META_BACK_PRINT_CONNTREE */
1617 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1619 if ( !LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1621 * Err could be -1 in case a duplicate metaconn is inserted
1628 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1629 /* duplicate: free and try to get the newly created one */
1630 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1632 meta_back_conn_free( mc );
1638 LDAP_BACK_CONN_TAINTED_SET( mc );
1642 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1643 Debug( LDAP_DEBUG_ANY,
1644 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1645 op->o_log_prefix, ncandidates,
1646 LDAP_BACK_PCONN_ID( mc ) );
1649 meta_back_conn_free( mc );
1651 rs->sr_err = LDAP_OTHER;
1652 rs->sr_text = "Proxy bind collision";
1653 if ( sendok & LDAP_BACK_SENDERR ) {
1654 send_ldap_result( op, rs );
1660 Debug( LDAP_DEBUG_TRACE,
1661 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1662 op->o_log_prefix, ncandidates,
1663 LDAP_BACK_PCONN_ID( mc ) );
1666 Debug( LDAP_DEBUG_TRACE,
1667 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1668 op->o_log_prefix, ncandidates,
1669 LDAP_BACK_PCONN_ID( mc ) );
1676 meta_back_release_conn_lock(
1681 assert( mc != NULL );
1684 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1686 assert( mc->mc_refcnt > 0 );
1688 /* NOTE: the connection is removed if either it is tainted
1689 * or if it is shared and no one else is using it. This needs
1690 * to occur because for intrinsic reasons cached connections
1691 * that are not privileged would live forever and pollute
1692 * the connection space (and eat up resources). Maybe this
1693 * should be configurable... */
1694 if ( LDAP_BACK_CONN_TAINTED( mc ) ) {
1695 #if META_BACK_PRINT_CONNTREE > 0
1696 meta_back_print_conntree( mi, ">>> meta_back_release_conn" );
1697 #endif /* META_BACK_PRINT_CONNTREE */
1699 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1700 if ( mc->mc_q.tqe_prev != NULL ) {
1701 assert( LDAP_BACK_CONN_CACHED( mc ) );
1702 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1703 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1704 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1705 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1708 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1714 tmpmc = avl_delete( &mi->mi_conninfo.lai_tree,
1715 ( caddr_t )mc, meta_back_conndnmc_cmp );
1717 /* Overparanoid, but useful... */
1718 assert( tmpmc == NULL || tmpmc == mc );
1721 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1723 #if META_BACK_PRINT_CONNTREE > 0
1724 meta_back_print_conntree( mi, "<<< meta_back_release_conn" );
1725 #endif /* META_BACK_PRINT_CONNTREE */
1727 if ( mc->mc_refcnt == 0 ) {
1728 meta_back_conn_free( mc );
1733 if ( mc != NULL && LDAP_BACK_CONN_BINDING( mc ) ) {
1734 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1738 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1743 meta_back_quarantine(
1748 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1749 metatarget_t *mt = mi->mi_targets[ candidate ];
1751 slap_retry_info_t *ri = &mt->mt_quarantine;
1753 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1755 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1756 time_t new_last = slap_get_time();
1758 switch ( mt->mt_isquarantined ) {
1759 case LDAP_BACK_FQ_NO:
1760 if ( ri->ri_last == new_last ) {
1764 Debug( LDAP_DEBUG_ANY,
1765 "%s meta_back_quarantine[%d]: enter.\n",
1766 op->o_log_prefix, candidate, 0 );
1772 case LDAP_BACK_FQ_RETRYING:
1773 if ( StatslogTest( LDAP_DEBUG_ANY ) ) {
1774 char buf[ SLAP_TEXT_BUFLEN ];
1776 snprintf( buf, sizeof( buf ),
1777 "meta_back_quarantine[%d]: block #%d try #%d failed",
1778 candidate, ri->ri_idx, ri->ri_count );
1779 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1780 op->o_log_prefix, buf, 0 );
1784 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1785 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1796 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1797 ri->ri_last = new_last;
1799 } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1800 Debug( LDAP_DEBUG_ANY,
1801 "%s meta_back_quarantine[%d]: exit.\n",
1802 op->o_log_prefix, candidate, 0 );
1804 if ( mi->mi_quarantine_f ) {
1805 (void)mi->mi_quarantine_f( mi, candidate,
1806 mi->mi_quarantine_p );
1811 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1815 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );