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 ( LogTest( 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 );
403 ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
405 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
406 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
407 LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
410 /* start TLS ("tls [try-]{start|propagate}" statement) */
411 if ( ( LDAP_BACK_USE_TLS( mi )
412 || ( op->o_conn->c_is_tls
413 && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
416 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
418 * use asynchronous StartTLS; in case, chase referral
419 * FIXME: OpenLDAP does not return referral on StartTLS yet
423 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
424 if ( rs->sr_err == LDAP_SUCCESS ) {
425 LDAPMessage *res = NULL;
426 int rc, nretries = mt->mt_nretries;
429 LDAP_BACK_TV_SET( &tv );
432 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
435 rs->sr_err = LDAP_OTHER;
439 if ( nretries != 0 ) {
440 if ( nretries > 0 ) {
443 LDAP_BACK_TV_SET( &tv );
446 rs->sr_err = LDAP_OTHER;
450 /* only touch when activity actually took place... */
451 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
452 msc->msc_time = op->o_time;
457 if ( rc == LDAP_RES_EXTENDED ) {
458 struct berval *data = NULL;
460 /* NOTE: right now, data is unused, so don't get it */
461 rs->sr_err = ldap_parse_extended_result( msc->msc_ld,
462 res, NULL, NULL /* &data */ , 0 );
463 if ( rs->sr_err == LDAP_SUCCESS ) {
466 /* FIXME: matched? referrals? response controls? */
467 rs->sr_err = ldap_parse_result( msc->msc_ld,
468 res, &err, NULL, NULL, NULL, NULL, 1 );
471 if ( rs->sr_err == LDAP_SUCCESS ) {
475 /* FIXME: in case a referral
476 * is returned, should we try
477 * using it instead of the
479 if ( rs->sr_err == LDAP_SUCCESS ) {
480 ldap_install_tls( msc->msc_ld );
482 } else if ( rs->sr_err == LDAP_REFERRAL ) {
483 /* FIXME: LDAP_OPERATIONS_ERROR? */
484 rs->sr_err = LDAP_OTHER;
485 rs->sr_text = "Unwilling to chase referral "
486 "returned by Start TLS exop";
495 rs->sr_err = LDAP_OTHER;
502 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
504 * use synchronous StartTLS
506 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
507 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
509 /* if StartTLS is requested, only attempt it if the URL
510 * is not "ldaps://"; this may occur not only in case
511 * of misconfiguration, but also when used in the chain
512 * overlay, where the "uri" can be parsed out of a referral */
513 if ( rs->sr_err == LDAP_SERVER_DOWN
514 || ( rs->sr_err != LDAP_SUCCESS
515 && LDAP_BACK_TLS_CRITICAL( mi ) ) )
519 Debug( LDAP_DEBUG_ANY,
520 "### %s meta_back_init_one_conn(TLS) "
521 "ldap_unbind_ext[%d] ld=%p\n",
522 op->o_log_prefix, candidate,
523 (void *)msc->msc_ld );
524 #endif /* DEBUG_205 */
526 /* need to trash a failed Start TLS */
527 meta_clear_one_candidate( op, mc, candidate );
531 #endif /* HAVE_TLS */
534 * Set the network timeout if set
536 if ( mt->mt_network_timeout != 0 ) {
537 struct timeval network_timeout;
539 network_timeout.tv_usec = 0;
540 network_timeout.tv_sec = mt->mt_network_timeout;
542 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
543 (void *)&network_timeout );
547 * If the connection DN is not null, an attempt to rewrite it is made
551 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
552 ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
553 if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
554 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
555 memset( msc->msc_cred.bv_val, 0,
556 msc->msc_cred.bv_len );
558 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
562 ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
566 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
567 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
568 ber_memfree_x( msc->msc_cred.bv_val, NULL );
569 BER_BVZERO( &msc->msc_cred );
571 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
572 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
573 BER_BVZERO( &msc->msc_bound_ndn );
575 if ( !BER_BVISEMPTY( &op->o_ndn )
576 && SLAP_IS_AUTHZ_BACKEND( op )
580 dc.conn = op->o_conn;
585 * Rewrite the bind dn if needed
587 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
588 &msc->msc_bound_ndn ) )
592 Debug( LDAP_DEBUG_ANY,
593 "### %s meta_back_init_one_conn(rewrite) "
594 "ldap_unbind_ext[%d] ld=%p\n",
595 op->o_log_prefix, candidate,
596 (void *)msc->msc_ld );
597 #endif /* DEBUG_205 */
599 /* need to trash a connection not fully established */
600 meta_clear_one_candidate( op, mc, candidate );
604 /* copy the DN if needed */
605 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
606 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
609 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
612 ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
616 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
620 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
622 META_BACK_CONN_CREATING_CLEAR( msc );
623 if ( rs->sr_err == LDAP_SUCCESS ) {
625 * Sets a cookie for the rewrite session
627 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
628 META_BACK_CONN_INITED_SET( msc );
631 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
634 if ( rs->sr_err != LDAP_SUCCESS ) {
635 rs->sr_err = slap_map_api2result( rs );
636 if ( sendok & LDAP_BACK_SENDERR ) {
637 send_ldap_result( op, rs );
647 * Retries one connection
655 ldap_back_send_t sendok )
657 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
658 metatarget_t *mt = mi->mi_targets[ candidate ];
659 metaconn_t *mc = *mcp;
660 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
661 int rc = LDAP_UNAVAILABLE,
665 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
667 assert( !META_BACK_CONN_CREATING( msc ) );
668 binding = LDAP_BACK_CONN_BINDING( msc );
669 LDAP_BACK_CONN_BINDING_CLEAR( msc );
671 assert( mc->mc_refcnt > 0 );
672 if ( mc->mc_refcnt == 1 ) {
673 if ( LogTest( LDAP_DEBUG_ANY ) ) {
674 char buf[ SLAP_TEXT_BUFLEN ];
676 /* this lock is required; however,
677 * it's invoked only when logging is on */
678 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
679 snprintf( buf, sizeof( buf ),
680 "retrying URI=\"%s\" DN=\"%s\"",
682 BER_BVISNULL( &msc->msc_bound_ndn ) ?
683 "" : msc->msc_bound_ndn.bv_val );
684 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
686 Debug( LDAP_DEBUG_ANY,
687 "%s meta_back_retry[%d]: %s.\n",
688 op->o_log_prefix, candidate, buf );
691 meta_clear_one_candidate( op, mc, candidate );
692 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
694 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
696 /* mc here must be the regular mc, reset and ready for init */
697 rc = meta_back_init_one_conn( op, rs, mc, candidate,
698 LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 );
700 /* restore the "binding" flag, in case */
702 LDAP_BACK_CONN_BINDING_SET( msc );
705 if ( rc == LDAP_SUCCESS ) {
707 rc = meta_back_single_dobind( op, rs, mcp, candidate,
708 sendok, mt->mt_nretries, 0 );
710 Debug( LDAP_DEBUG_ANY,
711 "%s meta_back_retry[%d]: "
712 "meta_back_single_dobind=%d\n",
713 op->o_log_prefix, candidate, rc );
714 if ( rc == LDAP_SUCCESS ) {
715 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
716 !BER_BVISEMPTY( &msc->msc_bound_ndn ) )
718 LDAP_BACK_CONN_ISBOUND_SET( msc );
721 LDAP_BACK_CONN_ISANON_SET( msc );
724 /* when bound, dispose of the "binding" flag */
726 LDAP_BACK_CONN_BINDING_CLEAR( msc );
731 /* don't send twice */
732 sendok &= ~LDAP_BACK_SENDERR;
735 if ( rc != LDAP_SUCCESS ) {
736 SlapReply *candidates = meta_back_candidates_get( op );
738 candidates[ candidate ].sr_err = rc;
740 if ( *mcp != NULL ) {
741 if ( mc->mc_refcnt == 1 ) {
743 LDAP_BACK_CONN_BINDING_CLEAR( msc );
745 (void)meta_clear_one_candidate( op, mc, candidate );
748 LDAP_BACK_CONN_TAINTED_SET( mc );
749 /* only release if mandatory; otherwise
750 * let the caller do what's best before
752 if ( META_BACK_ONERR_STOP( mi ) ) {
753 meta_back_release_conn_lock( mi, mc, 0 );
757 #if META_BACK_PRINT_CONNTREE > 0
758 meta_back_print_conntree( mi, ">>> meta_back_retry" );
759 #endif /* META_BACK_PRINT_CONNTREE */
761 /* FIXME: could be done better, reworking meta_back_release_conn_lock() */
762 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
763 if ( mc->mc_q.tqe_prev != NULL ) {
764 assert( LDAP_BACK_CONN_CACHED( mc ) );
765 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
766 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
768 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
769 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
772 assert( !LDAP_BACK_CONN_CACHED( mc ) );
776 /* FIXME: check if in tree, for consistency? */
777 (void)avl_delete( &mi->mi_conninfo.lai_tree,
778 ( caddr_t )mc, meta_back_conndnmc_cmp );
780 LDAP_BACK_CONN_CACHED_CLEAR( mc );
782 #if META_BACK_PRINT_CONNTREE > 0
783 meta_back_print_conntree( mi, "<<< meta_back_retry" );
784 #endif /* META_BACK_PRINT_CONNTREE */
788 if ( sendok & LDAP_BACK_SENDERR ) {
790 rs->sr_text = "Unable to retry";
791 send_ldap_result( op, rs );
795 if ( quarantine && META_BACK_TGT_QUARANTINE( mt ) ) {
796 meta_back_quarantine( op, rs, candidate );
799 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
801 return rc == LDAP_SUCCESS ? 1 : 0;
805 * callback for unique candidate selection
808 meta_back_conn_cb( Operation *op, SlapReply *rs )
810 assert( op->o_tag == LDAP_REQ_SEARCH );
812 switch ( rs->sr_type ) {
814 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
830 meta_back_get_candidate(
835 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
839 * tries to get a unique candidate
840 * (takes care of default target)
842 candidate = meta_back_select_unique_candidate( mi, ndn );
845 * if any is found, inits the connection
847 if ( candidate == META_TARGET_NONE ) {
848 rs->sr_err = LDAP_NO_SUCH_OBJECT;
849 rs->sr_text = "No suitable candidate target found";
851 } 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 op2.ors_filter = slap_filter_objectClass_pres;
871 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
873 op2.o_callback = &cb2;
874 cb2.sc_response = meta_back_conn_cb;
875 cb2.sc_private = (void *)&candidate;
877 rc = op->o_bd->be_search( &op2, &rs2 );
879 switch ( rs2.sr_err ) {
882 rs->sr_err = rs2.sr_err;
885 case LDAP_SIZELIMIT_EXCEEDED:
886 /* if multiple candidates can serve the operation,
887 * and a default target is defined, and it is
888 * a candidate, try using it (FIXME: YMMV) */
889 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
890 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
891 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
893 candidate = mi->mi_defaulttarget;
894 rs->sr_err = LDAP_SUCCESS;
898 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
899 rs->sr_text = "Unable to select unique candidate target";
905 rs->sr_err = LDAP_SUCCESS;
911 static void *meta_back_candidates_dummy;
914 meta_back_candidates_keyfree(
918 metacandidates_t *mc = (metacandidates_t *)data;
920 ber_memfree_x( mc->mc_candidates, NULL );
921 ber_memfree_x( data, NULL );
925 meta_back_candidates_get( Operation *op )
927 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
928 metacandidates_t *mc;
930 if ( op->o_threadctx ) {
933 ldap_pvt_thread_pool_getkey( op->o_threadctx,
934 &meta_back_candidates_dummy, &data, NULL );
935 mc = (metacandidates_t *)data;
938 mc = mi->mi_candidates;
942 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
943 mc->mc_ntargets = mi->mi_ntargets;
944 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
945 if ( op->o_threadctx ) {
949 ldap_pvt_thread_pool_setkey( op->o_threadctx,
950 &meta_back_candidates_dummy, data,
951 meta_back_candidates_keyfree );
954 mi->mi_candidates = mc;
957 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
958 /* NOTE: in the future, may want to allow back-config
959 * to add/remove targets from back-meta... */
960 mc->mc_candidates = ch_realloc( mc->mc_candidates,
961 sizeof( SlapReply ) * mi->mi_ntargets );
962 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
963 sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
964 mc->mc_ntargets = mi->mi_ntargets;
967 return mc->mc_candidates;
973 * Prepares the connection structure
977 * - determine what DN is being requested:
979 * op requires candidate checks
981 * add unique parent of o_req_ndn
982 * bind unique^*[/all] o_req_ndn [no check]
983 * compare unique^+ o_req_ndn
984 * delete unique o_req_ndn
985 * modify unique o_req_ndn
986 * search any o_req_ndn
987 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
989 * - for ops that require the candidate to be unique, in case of multiple
990 * occurrences an internal search with sizeLimit=1 is performed
991 * if a unique candidate can actually be determined. If none is found,
992 * the operation aborts; if multiple are found, the default target
993 * is used if defined and candidate; otherwise the operation aborts.
995 * *^note: actually, the bind operation is handled much like a search;
996 * i.e. the bind is broadcast to all candidate targets.
998 * +^note: actually, the compare operation is handled much like a search;
999 * i.e. the compare is broadcast to all candidate targets, while checking
1000 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
1008 ldap_back_send_t sendok )
1010 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1011 metaconn_t *mc = NULL,
1013 int cached = META_TARGET_NONE,
1014 i = META_TARGET_NONE,
1020 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
1024 META_DNTYPE_NEWPARENT
1025 } dn_type = META_DNTYPE_ENTRY;
1026 struct berval ndn = op->o_req_ndn,
1029 SlapReply *candidates = meta_back_candidates_get( op );
1031 /* Internal searches are privileged and shared. So is root. */
1032 if ( ( !BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ALWAYS( mi ) )
1033 || ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ANON( mi ) )
1034 || op->o_do_not_cache || be_isroot( op ) )
1036 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
1037 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
1038 LDAP_BACK_PCONN_ROOTDN_SET( &mc_curr, op );
1040 } else if ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_NOANON( mi ) )
1042 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1043 BER_BVSTR( &mc_curr.mc_local_ndn, "" );
1044 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1047 mc_curr.mc_local_ndn = op->o_ndn;
1049 /* Explicit binds must not be shared */
1050 if ( !BER_BVISEMPTY( &op->o_ndn )
1051 || op->o_tag == LDAP_REQ_BIND
1052 || SLAP_IS_AUTHZ_BACKEND( op ) )
1054 mc_curr.mc_conn = op->o_conn;
1057 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1058 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1062 /* Explicit Bind requests always get their own conn */
1063 if ( sendok & LDAP_BACK_BINDING ) {
1064 mc_curr.mc_conn = op->o_conn;
1067 /* Searches for a metaconn in the avl tree */
1069 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1070 if ( LDAP_BACK_PCONN_ISPRIV( &mc_curr ) ) {
1071 /* lookup a conn that's not binding */
1072 LDAP_TAILQ_FOREACH( mc,
1073 &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv,
1076 if ( !LDAP_BACK_CONN_BINDING( mc ) && mc->mc_refcnt == 0 ) {
1082 if ( mc != LDAP_TAILQ_LAST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1083 metaconn_t, mc_q ) )
1085 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1087 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1088 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1092 } else if ( !LDAP_BACK_USE_TEMPORARIES( mi )
1093 && mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_num == mi->mi_conn_priv_max )
1095 mc = LDAP_TAILQ_FIRST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv );
1100 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1101 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1105 /* catch taint errors */
1106 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1108 /* Don't reuse connections while they're still binding
1109 * NOTE: only makes sense for binds */
1110 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1111 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1112 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1114 ldap_pvt_thread_yield();
1118 /* release conn, and create a temporary */
1122 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
1123 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
1125 #if META_BACK_PRINT_CONNTREE > 0
1126 meta_back_print_conntree( mi,
1127 ">>> meta_back_getconn(expired)" );
1128 #endif /* META_BACK_PRINT_CONNTREE */
1130 /* don't let anyone else use this expired connection */
1131 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1132 if ( mc->mc_q.tqe_prev != NULL ) {
1133 assert( LDAP_BACK_CONN_CACHED( mc ) );
1134 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1135 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1137 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1138 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1141 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1145 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1146 (caddr_t)mc, meta_back_conndnmc_cmp );
1149 #if META_BACK_PRINT_CONNTREE > 0
1150 meta_back_print_conntree( mi,
1151 "<<< meta_back_getconn(expired)" );
1152 #endif /* META_BACK_PRINT_CONNTREE */
1153 LDAP_BACK_CONN_TAINTED_SET( mc );
1154 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1156 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired (tainted).\n",
1157 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc ) );
1163 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1166 switch ( op->o_tag ) {
1168 /* if we go to selection, the entry must not exist,
1169 * and we must be able to resolve the parent */
1170 dn_type = META_DNTYPE_PARENT;
1171 dnParent( &ndn, &pndn );
1174 case LDAP_REQ_MODRDN:
1175 /* if nnewSuperior is not NULL, it must resolve
1176 * to the same candidate as the req_ndn */
1177 if ( op->orr_nnewSup ) {
1178 dn_type = META_DNTYPE_NEWPARENT;
1183 /* if bound as rootdn, the backend must bind to all targets
1184 * with the administrative identity */
1185 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
1186 op_type = META_OP_REQUIRE_ALL;
1190 case LDAP_REQ_COMPARE:
1191 case LDAP_REQ_DELETE:
1192 case LDAP_REQ_MODIFY:
1193 /* just a unique candidate */
1196 case LDAP_REQ_SEARCH:
1197 /* allow multiple candidates for the searchBase */
1198 op_type = META_OP_ALLOW_MULTIPLE;
1202 /* right now, just break (exop?) */
1207 * require all connections ...
1209 if ( op_type == META_OP_REQUIRE_ALL ) {
1211 /* Looks like we didn't get a bind. Open a new session... */
1213 assert( new_conn == 0 );
1214 mc = metaconn_alloc( op );
1215 mc->mc_conn = mc_curr.mc_conn;
1216 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1218 if ( sendok & LDAP_BACK_BINDING ) {
1219 LDAP_BACK_CONN_BINDING_SET( mc );
1221 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1222 LDAP_BACK_CONN_ISPRIV_SET( mc );
1224 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1225 LDAP_BACK_CONN_ISANON_SET( mc );
1229 /* TODO: if any of the connections is binding,
1230 * release mc and create a new one */
1233 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1235 * The target is activated; if needed, it is
1238 candidates[ i ].sr_err = meta_back_init_one_conn( op,
1239 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1240 LDAP_BACK_DONTSEND, !new_conn );
1241 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1242 META_CANDIDATE_SET( &candidates[ i ] );
1248 * FIXME: in case one target cannot
1249 * be init'd, should the other ones
1252 META_CANDIDATE_RESET( &candidates[ i ] );
1253 err = candidates[ i ].sr_err;
1258 if ( ncandidates == 0 ) {
1261 meta_back_conn_free( mc );
1264 meta_back_release_conn( mi, mc );
1267 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1268 rs->sr_text = "Unable to select valid candidates";
1270 if ( sendok & LDAP_BACK_SENDERR ) {
1271 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1272 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1274 send_ldap_result( op, rs );
1275 rs->sr_matched = NULL;
1285 * looks in cache, if any
1287 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1288 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1291 if ( op_type == META_OP_REQUIRE_SINGLE ) {
1292 metatarget_t *mt = NULL;
1293 metasingleconn_t *msc = NULL;
1297 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1298 META_CANDIDATE_RESET( &candidates[ j ] );
1302 * tries to get a unique candidate
1303 * (takes care of default target)
1305 if ( i == META_TARGET_NONE ) {
1306 i = meta_back_get_candidate( op, rs, &ndn );
1308 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1309 i = meta_back_get_candidate( op, rs, &pndn );
1312 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1314 meta_back_release_conn( mi, mc );
1317 if ( sendok & LDAP_BACK_SENDERR ) {
1318 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1319 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1321 send_ldap_result( op, rs );
1322 rs->sr_matched = NULL;
1329 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1332 meta_back_release_conn( mi, mc );
1335 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1336 rs->sr_text = "Cross-target rename not supported";
1337 if ( sendok & LDAP_BACK_SENDERR ) {
1338 send_ldap_result( op, rs );
1344 Debug( LDAP_DEBUG_TRACE,
1345 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1346 i, op->o_req_ndn.bv_val, 0 );
1349 /* Retries searching for a metaconn in the avl tree
1350 * the reason is that the connection might have been
1351 * created by meta_back_get_candidate() */
1352 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1354 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1355 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1356 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1358 /* catch taint errors */
1359 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1361 /* Don't reuse connections while they're still binding */
1362 if ( META_BACK_CONN_CREATING( &mc->mc_conns[ i ] )
1363 || LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) )
1365 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1366 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1367 ldap_pvt_thread_yield();
1377 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1380 /* Looks like we didn't get a bind. Open a new session... */
1382 assert( new_conn == 0 );
1383 mc = metaconn_alloc( op );
1384 mc->mc_conn = mc_curr.mc_conn;
1385 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1387 if ( sendok & LDAP_BACK_BINDING ) {
1388 LDAP_BACK_CONN_BINDING_SET( mc );
1390 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1391 LDAP_BACK_CONN_ISPRIV_SET( mc );
1393 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1394 LDAP_BACK_CONN_ISANON_SET( mc );
1400 * Clear all other candidates
1402 ( void )meta_clear_unused_candidates( op, i );
1404 mt = mi->mi_targets[ i ];
1405 msc = &mc->mc_conns[ i ];
1408 * The target is activated; if needed, it is
1409 * also init'd. In case of error, meta_back_init_one_conn
1410 * sends the appropriate result.
1412 err = meta_back_init_one_conn( op, rs, mc, i,
1413 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn );
1414 if ( err != LDAP_SUCCESS ) {
1416 * FIXME: in case one target cannot
1417 * be init'd, should the other ones
1420 META_CANDIDATE_RESET( &candidates[ i ] );
1423 meta_back_conn_free( mc );
1426 meta_back_release_conn( mi, mc );
1431 candidates[ i ].sr_err = LDAP_SUCCESS;
1432 META_CANDIDATE_SET( &candidates[ i ] );
1440 * if no unique candidate ...
1444 /* Looks like we didn't get a bind. Open a new session... */
1446 assert( new_conn == 0 );
1447 mc = metaconn_alloc( op );
1448 mc->mc_conn = mc_curr.mc_conn;
1449 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1451 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1452 LDAP_BACK_CONN_ISPRIV_SET( mc );
1454 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1455 LDAP_BACK_CONN_ISANON_SET( mc );
1459 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1460 metatarget_t *mt = mi->mi_targets[ i ];
1462 META_CANDIDATE_RESET( &candidates[ i ] );
1465 || meta_back_is_candidate( mt, &op->o_req_ndn,
1466 LDAP_SCOPE_SUBTREE ) )
1470 * The target is activated; if needed, it is
1473 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1474 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1475 LDAP_BACK_DONTSEND, !new_conn );
1476 candidates[ i ].sr_err = lerr;
1477 if ( lerr == LDAP_SUCCESS ) {
1478 META_CANDIDATE_SET( &candidates[ i ] );
1481 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1482 op->o_log_prefix, i, 0 );
1484 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1485 META_CANDIDATE_SET( &candidates[ i ] );
1487 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1488 op->o_log_prefix, i,
1489 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1494 * FIXME: in case one target cannot
1495 * be init'd, should the other ones
1499 ( void )meta_clear_one_candidate( op, mc, i );
1501 /* leave the target candidate, but record the error for later use */
1504 if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1505 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined err=%d\n",
1506 op->o_log_prefix, i, lerr );
1509 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed err=%d\n",
1510 op->o_log_prefix, i, lerr );
1513 if ( META_BACK_ONERR_STOP( mi ) ) {
1514 if ( sendok & LDAP_BACK_SENDERR ) {
1515 send_ldap_result( op, rs );
1519 meta_back_conn_free( mc );
1522 meta_back_release_conn( mi, mc );
1533 ( void )meta_clear_one_candidate( op, mc, i );
1538 if ( ncandidates == 0 ) {
1541 meta_back_conn_free( mc );
1544 meta_back_release_conn( mi, mc );
1547 if ( rs->sr_err == LDAP_SUCCESS ) {
1548 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1549 rs->sr_text = "Unable to select valid candidates";
1552 if ( sendok & LDAP_BACK_SENDERR ) {
1553 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1554 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1556 send_ldap_result( op, rs );
1557 rs->sr_matched = NULL;
1565 /* clear out meta_back_init_one_conn non-fatal errors */
1566 rs->sr_err = LDAP_SUCCESS;
1569 /* touch the timestamp */
1570 if ( mi->mi_idle_timeout != 0 ) {
1571 mc->mc_time = op->o_time;
1575 if ( mi->mi_conn_ttl ) {
1576 mc->mc_create_time = op->o_time;
1580 * Inserts the newly created metaconn in the avl tree
1582 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1583 #if META_BACK_PRINT_CONNTREE > 0
1584 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1585 #endif /* META_BACK_PRINT_CONNTREE */
1588 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1589 if ( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num < mi->mi_conn_priv_max ) {
1590 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1591 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num++;
1592 LDAP_BACK_CONN_CACHED_SET( mc );
1595 LDAP_BACK_CONN_TAINTED_SET( mc );
1599 } else if ( !( sendok & LDAP_BACK_BINDING ) ) {
1600 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1601 meta_back_conndn_cmp, meta_back_conndn_dup );
1602 LDAP_BACK_CONN_CACHED_SET( mc );
1605 #if META_BACK_PRINT_CONNTREE > 0
1606 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1607 #endif /* META_BACK_PRINT_CONNTREE */
1608 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1610 if ( !LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1612 * Err could be -1 in case a duplicate metaconn is inserted
1619 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1620 /* duplicate: free and try to get the newly created one */
1621 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1623 meta_back_conn_free( mc );
1629 LDAP_BACK_CONN_TAINTED_SET( mc );
1633 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1634 Debug( LDAP_DEBUG_ANY,
1635 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1636 op->o_log_prefix, ncandidates,
1637 LDAP_BACK_PCONN_ID( mc ) );
1640 meta_back_conn_free( mc );
1642 rs->sr_err = LDAP_OTHER;
1643 rs->sr_text = "Proxy bind collision";
1644 if ( sendok & LDAP_BACK_SENDERR ) {
1645 send_ldap_result( op, rs );
1651 Debug( LDAP_DEBUG_TRACE,
1652 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1653 op->o_log_prefix, ncandidates,
1654 LDAP_BACK_PCONN_ID( mc ) );
1657 Debug( LDAP_DEBUG_TRACE,
1658 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1659 op->o_log_prefix, ncandidates,
1660 LDAP_BACK_PCONN_ID( mc ) );
1667 meta_back_release_conn_lock(
1672 assert( mc != NULL );
1675 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1677 assert( mc->mc_refcnt > 0 );
1679 /* NOTE: the connection is removed if either it is tainted
1680 * or if it is shared and no one else is using it. This needs
1681 * to occur because for intrinsic reasons cached connections
1682 * that are not privileged would live forever and pollute
1683 * the connection space (and eat up resources). Maybe this
1684 * should be configurable... */
1685 if ( LDAP_BACK_CONN_TAINTED( mc ) || !LDAP_BACK_CONN_CACHED( mc ) ) {
1686 #if META_BACK_PRINT_CONNTREE > 0
1687 meta_back_print_conntree( mi, ">>> meta_back_release_conn" );
1688 #endif /* META_BACK_PRINT_CONNTREE */
1690 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1691 if ( mc->mc_q.tqe_prev != NULL ) {
1692 assert( LDAP_BACK_CONN_CACHED( mc ) );
1693 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1694 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1695 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1696 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1699 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1702 } else if ( LDAP_BACK_CONN_CACHED( mc ) ) {
1705 tmpmc = avl_delete( &mi->mi_conninfo.lai_tree,
1706 ( caddr_t )mc, meta_back_conndnmc_cmp );
1708 /* Overparanoid, but useful... */
1709 assert( tmpmc == NULL || tmpmc == mc );
1712 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1714 #if META_BACK_PRINT_CONNTREE > 0
1715 meta_back_print_conntree( mi, "<<< meta_back_release_conn" );
1716 #endif /* META_BACK_PRINT_CONNTREE */
1718 if ( mc->mc_refcnt == 0 ) {
1719 meta_back_conn_free( mc );
1724 if ( mc != NULL && LDAP_BACK_CONN_BINDING( mc ) ) {
1725 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1729 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1734 meta_back_quarantine(
1739 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1740 metatarget_t *mt = mi->mi_targets[ candidate ];
1742 slap_retry_info_t *ri = &mt->mt_quarantine;
1744 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1746 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1747 time_t new_last = slap_get_time();
1749 switch ( mt->mt_isquarantined ) {
1750 case LDAP_BACK_FQ_NO:
1751 if ( ri->ri_last == new_last ) {
1755 Debug( LDAP_DEBUG_ANY,
1756 "%s meta_back_quarantine[%d]: enter.\n",
1757 op->o_log_prefix, candidate, 0 );
1763 case LDAP_BACK_FQ_RETRYING:
1764 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1765 char buf[ SLAP_TEXT_BUFLEN ];
1767 snprintf( buf, sizeof( buf ),
1768 "meta_back_quarantine[%d]: block #%d try #%d failed",
1769 candidate, ri->ri_idx, ri->ri_count );
1770 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1771 op->o_log_prefix, buf, 0 );
1775 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1776 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1787 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1788 ri->ri_last = new_last;
1790 } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1791 Debug( LDAP_DEBUG_ANY,
1792 "%s meta_back_quarantine[%d]: exit.\n",
1793 op->o_log_prefix, candidate, 0 );
1795 if ( mi->mi_quarantine_f ) {
1796 (void)mi->mi_quarantine_f( mi, candidate,
1797 mi->mi_quarantine_p );
1802 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1806 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );