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 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
1188 op_type = META_OP_REQUIRE_ALL;
1192 case LDAP_REQ_COMPARE:
1193 case LDAP_REQ_DELETE:
1194 case LDAP_REQ_MODIFY:
1195 /* just a unique candidate */
1198 case LDAP_REQ_SEARCH:
1199 /* allow multiple candidates for the searchBase */
1200 op_type = META_OP_ALLOW_MULTIPLE;
1204 /* right now, just break (exop?) */
1209 * require all connections ...
1211 if ( op_type == META_OP_REQUIRE_ALL ) {
1213 /* Looks like we didn't get a bind. Open a new session... */
1215 assert( new_conn == 0 );
1216 mc = metaconn_alloc( op );
1217 mc->mc_conn = mc_curr.mc_conn;
1218 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1220 if ( sendok & LDAP_BACK_BINDING ) {
1221 LDAP_BACK_CONN_BINDING_SET( mc );
1223 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1224 LDAP_BACK_CONN_ISPRIV_SET( mc );
1226 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1227 LDAP_BACK_CONN_ISANON_SET( mc );
1231 /* TODO: if any of the connections is binding,
1232 * release mc and create a new one */
1235 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1237 * The target is activated; if needed, it is
1240 candidates[ i ].sr_err = meta_back_init_one_conn( op,
1241 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1242 LDAP_BACK_DONTSEND, !new_conn );
1243 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1244 META_CANDIDATE_SET( &candidates[ i ] );
1250 * FIXME: in case one target cannot
1251 * be init'd, should the other ones
1254 META_CANDIDATE_RESET( &candidates[ i ] );
1255 err = candidates[ i ].sr_err;
1260 if ( ncandidates == 0 ) {
1263 meta_back_conn_free( mc );
1266 meta_back_release_conn( mi, mc );
1269 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1270 rs->sr_text = "Unable to select valid candidates";
1272 if ( sendok & LDAP_BACK_SENDERR ) {
1273 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1274 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1276 send_ldap_result( op, rs );
1277 rs->sr_matched = NULL;
1287 * looks in cache, if any
1289 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1290 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1293 if ( op_type == META_OP_REQUIRE_SINGLE ) {
1294 metatarget_t *mt = NULL;
1295 metasingleconn_t *msc = NULL;
1299 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1300 META_CANDIDATE_RESET( &candidates[ j ] );
1304 * tries to get a unique candidate
1305 * (takes care of default target)
1307 if ( i == META_TARGET_NONE ) {
1308 i = meta_back_get_candidate( op, rs, &ndn );
1310 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1311 i = meta_back_get_candidate( op, rs, &pndn );
1314 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1316 meta_back_release_conn( mi, mc );
1319 if ( sendok & LDAP_BACK_SENDERR ) {
1320 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1321 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1323 send_ldap_result( op, rs );
1324 rs->sr_matched = NULL;
1331 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1334 meta_back_release_conn( mi, mc );
1337 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1338 rs->sr_text = "Cross-target rename not supported";
1339 if ( sendok & LDAP_BACK_SENDERR ) {
1340 send_ldap_result( op, rs );
1346 Debug( LDAP_DEBUG_TRACE,
1347 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1348 i, op->o_req_ndn.bv_val, 0 );
1351 /* Retries searching for a metaconn in the avl tree
1352 * the reason is that the connection might have been
1353 * created by meta_back_get_candidate() */
1354 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1356 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1357 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1358 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1360 /* catch taint errors */
1361 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1363 /* Don't reuse connections while they're still binding */
1364 if ( META_BACK_CONN_CREATING( &mc->mc_conns[ i ] )
1365 || LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) )
1367 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1368 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1369 ldap_pvt_thread_yield();
1379 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1382 /* Looks like we didn't get a bind. Open a new session... */
1384 assert( new_conn == 0 );
1385 mc = metaconn_alloc( op );
1386 mc->mc_conn = mc_curr.mc_conn;
1387 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1389 if ( sendok & LDAP_BACK_BINDING ) {
1390 LDAP_BACK_CONN_BINDING_SET( mc );
1392 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1393 LDAP_BACK_CONN_ISPRIV_SET( mc );
1395 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1396 LDAP_BACK_CONN_ISANON_SET( mc );
1402 * Clear all other candidates
1404 ( void )meta_clear_unused_candidates( op, i );
1406 mt = mi->mi_targets[ i ];
1407 msc = &mc->mc_conns[ i ];
1410 * The target is activated; if needed, it is
1411 * also init'd. In case of error, meta_back_init_one_conn
1412 * sends the appropriate result.
1414 err = meta_back_init_one_conn( op, rs, mc, i,
1415 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn );
1416 if ( err != LDAP_SUCCESS ) {
1418 * FIXME: in case one target cannot
1419 * be init'd, should the other ones
1422 META_CANDIDATE_RESET( &candidates[ i ] );
1425 meta_back_conn_free( mc );
1428 meta_back_release_conn( mi, mc );
1433 candidates[ i ].sr_err = LDAP_SUCCESS;
1434 META_CANDIDATE_SET( &candidates[ i ] );
1442 * if no unique candidate ...
1446 /* Looks like we didn't get a bind. Open a new session... */
1448 assert( new_conn == 0 );
1449 mc = metaconn_alloc( op );
1450 mc->mc_conn = mc_curr.mc_conn;
1451 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1453 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1454 LDAP_BACK_CONN_ISPRIV_SET( mc );
1456 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1457 LDAP_BACK_CONN_ISANON_SET( mc );
1461 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1462 metatarget_t *mt = mi->mi_targets[ i ];
1464 META_CANDIDATE_RESET( &candidates[ i ] );
1467 || meta_back_is_candidate( mt, &op->o_req_ndn,
1468 LDAP_SCOPE_SUBTREE ) )
1472 * The target is activated; if needed, it is
1475 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1476 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1477 LDAP_BACK_DONTSEND, !new_conn );
1478 candidates[ i ].sr_err = lerr;
1479 if ( lerr == LDAP_SUCCESS ) {
1480 META_CANDIDATE_SET( &candidates[ i ] );
1483 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1484 op->o_log_prefix, i, 0 );
1486 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1487 META_CANDIDATE_SET( &candidates[ i ] );
1489 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1490 op->o_log_prefix, i,
1491 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1496 * FIXME: in case one target cannot
1497 * be init'd, should the other ones
1501 ( void )meta_clear_one_candidate( op, mc, i );
1503 /* leave the target candidate, but record the error for later use */
1506 if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1507 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined err=%d\n",
1508 op->o_log_prefix, i, lerr );
1511 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed err=%d\n",
1512 op->o_log_prefix, i, lerr );
1515 if ( META_BACK_ONERR_STOP( mi ) ) {
1516 if ( sendok & LDAP_BACK_SENDERR ) {
1517 send_ldap_result( op, rs );
1521 meta_back_conn_free( mc );
1524 meta_back_release_conn( mi, mc );
1535 ( void )meta_clear_one_candidate( op, mc, i );
1540 if ( ncandidates == 0 ) {
1543 meta_back_conn_free( mc );
1546 meta_back_release_conn( mi, mc );
1549 if ( rs->sr_err == LDAP_SUCCESS ) {
1550 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1551 rs->sr_text = "Unable to select valid candidates";
1554 if ( sendok & LDAP_BACK_SENDERR ) {
1555 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1556 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1558 send_ldap_result( op, rs );
1559 rs->sr_matched = NULL;
1567 /* clear out meta_back_init_one_conn non-fatal errors */
1568 rs->sr_err = LDAP_SUCCESS;
1571 /* touch the timestamp */
1572 if ( mi->mi_idle_timeout != 0 ) {
1573 mc->mc_time = op->o_time;
1577 if ( mi->mi_conn_ttl ) {
1578 mc->mc_create_time = op->o_time;
1582 * Inserts the newly created metaconn in the avl tree
1584 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1585 #if META_BACK_PRINT_CONNTREE > 0
1586 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1587 #endif /* META_BACK_PRINT_CONNTREE */
1589 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1590 if ( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num < mi->mi_conn_priv_max ) {
1591 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1592 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num++;
1593 LDAP_BACK_CONN_CACHED_SET( mc );
1596 LDAP_BACK_CONN_TAINTED_SET( mc );
1601 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1602 meta_back_conndn_cmp, meta_back_conndn_dup );
1603 LDAP_BACK_CONN_CACHED_SET( mc );
1606 #if META_BACK_PRINT_CONNTREE > 0
1607 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1608 #endif /* META_BACK_PRINT_CONNTREE */
1609 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1611 if ( !LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1613 * Err could be -1 in case a duplicate metaconn is inserted
1620 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1621 /* duplicate: free and try to get the newly created one */
1622 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1624 meta_back_conn_free( mc );
1630 LDAP_BACK_CONN_TAINTED_SET( mc );
1634 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1635 Debug( LDAP_DEBUG_ANY,
1636 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1637 op->o_log_prefix, ncandidates,
1638 LDAP_BACK_PCONN_ID( mc ) );
1641 meta_back_conn_free( mc );
1643 rs->sr_err = LDAP_OTHER;
1644 rs->sr_text = "Proxy bind collision";
1645 if ( sendok & LDAP_BACK_SENDERR ) {
1646 send_ldap_result( op, rs );
1652 Debug( LDAP_DEBUG_TRACE,
1653 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1654 op->o_log_prefix, ncandidates,
1655 LDAP_BACK_PCONN_ID( mc ) );
1658 Debug( LDAP_DEBUG_TRACE,
1659 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1660 op->o_log_prefix, ncandidates,
1661 LDAP_BACK_PCONN_ID( mc ) );
1668 meta_back_release_conn_lock(
1673 assert( mc != NULL );
1676 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1678 assert( mc->mc_refcnt > 0 );
1680 /* NOTE: the connection is removed if either it is tainted
1681 * or if it is shared and no one else is using it. This needs
1682 * to occur because for intrinsic reasons cached connections
1683 * that are not privileged would live forever and pollute
1684 * the connection space (and eat up resources). Maybe this
1685 * should be configurable... */
1686 if ( LDAP_BACK_CONN_TAINTED( mc ) ) {
1687 #if META_BACK_PRINT_CONNTREE > 0
1688 meta_back_print_conntree( mi, ">>> meta_back_release_conn" );
1689 #endif /* META_BACK_PRINT_CONNTREE */
1691 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1692 if ( mc->mc_q.tqe_prev != NULL ) {
1693 assert( LDAP_BACK_CONN_CACHED( mc ) );
1694 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1695 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1696 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1697 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1700 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1706 tmpmc = avl_delete( &mi->mi_conninfo.lai_tree,
1707 ( caddr_t )mc, meta_back_conndnmc_cmp );
1709 /* Overparanoid, but useful... */
1710 assert( tmpmc == NULL || tmpmc == mc );
1713 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1715 #if META_BACK_PRINT_CONNTREE > 0
1716 meta_back_print_conntree( mi, "<<< meta_back_release_conn" );
1717 #endif /* META_BACK_PRINT_CONNTREE */
1719 if ( mc->mc_refcnt == 0 ) {
1720 meta_back_conn_free( mc );
1725 if ( mc != NULL && LDAP_BACK_CONN_BINDING( mc ) ) {
1726 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1730 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1735 meta_back_quarantine(
1740 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1741 metatarget_t *mt = mi->mi_targets[ candidate ];
1743 slap_retry_info_t *ri = &mt->mt_quarantine;
1745 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1747 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1748 time_t new_last = slap_get_time();
1750 switch ( mt->mt_isquarantined ) {
1751 case LDAP_BACK_FQ_NO:
1752 if ( ri->ri_last == new_last ) {
1756 Debug( LDAP_DEBUG_ANY,
1757 "%s meta_back_quarantine[%d]: enter.\n",
1758 op->o_log_prefix, candidate, 0 );
1764 case LDAP_BACK_FQ_RETRYING:
1765 if ( StatslogTest( LDAP_DEBUG_ANY ) ) {
1766 char buf[ SLAP_TEXT_BUFLEN ];
1768 snprintf( buf, sizeof( buf ),
1769 "meta_back_quarantine[%d]: block #%d try #%d failed",
1770 candidate, ri->ri_idx, ri->ri_count );
1771 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1772 op->o_log_prefix, buf, 0 );
1776 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1777 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1788 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1789 ri->ri_last = new_last;
1791 } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1792 Debug( LDAP_DEBUG_ANY,
1793 "%s meta_back_quarantine[%d]: exit.\n",
1794 op->o_log_prefix, candidate, 0 );
1796 if ( mi->mi_quarantine_f ) {
1797 (void)mi->mi_quarantine_f( mi, candidate,
1798 mi->mi_quarantine_p );
1803 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1807 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );