1 /* $OpenLDAP: pkg/ldap/servers/slapd/back-meta/conn.c,v 1.86.2.15 2008/04/14 21:19:57 quanah Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2008 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
28 #include <ac/socket.h>
29 #include <ac/string.h>
34 #include "../back-ldap/back-ldap.h"
35 #include "back-meta.h"
38 * meta_back_conndn_cmp
40 * compares two struct metaconn based on the value of the conn pointer
41 * and of the local DN; used by avl stuff
48 metaconn_t
*mc1
= ( metaconn_t
* )c1
;
49 metaconn_t
*mc2
= ( metaconn_t
* )c2
;
52 /* If local DNs don't match, it is definitely not a match */
53 /* For shared sessions, conn is NULL. Only explicitly
54 * bound sessions will have non-NULL conn.
56 rc
= SLAP_PTRCMP( mc1
->mc_conn
, mc2
->mc_conn
);
58 rc
= ber_bvcmp( &mc1
->mc_local_ndn
, &mc2
->mc_local_ndn
);
65 * meta_back_conndnmc_cmp
67 * compares two struct metaconn based on the value of the conn pointer,
68 * the local DN and the struct pointer; used by avl stuff
71 meta_back_conndnmc_cmp(
75 metaconn_t
*mc1
= ( metaconn_t
* )c1
;
76 metaconn_t
*mc2
= ( metaconn_t
* )c2
;
79 /* If local DNs don't match, it is definitely not a match */
80 /* For shared sessions, conn is NULL. Only explicitly
81 * bound sessions will have non-NULL conn.
83 rc
= SLAP_PTRCMP( mc1
->mc_conn
, mc2
->mc_conn
);
85 rc
= ber_bvcmp( &mc1
->mc_local_ndn
, &mc2
->mc_local_ndn
);
87 rc
= SLAP_PTRCMP( mc1
, mc2
);
97 * compares two struct metaconn based on the value of the conn pointer;
105 metaconn_t
*mc1
= ( metaconn_t
* )c1
;
106 metaconn_t
*mc2
= ( metaconn_t
* )c2
;
108 /* For shared sessions, conn is NULL. Only explicitly
109 * bound sessions will have non-NULL conn.
111 return SLAP_PTRCMP( mc1
->mc_conn
, mc2
->mc_conn
);
115 * meta_back_conndn_dup
117 * returns -1 in case a duplicate struct metaconn has been inserted;
121 meta_back_conndn_dup(
125 metaconn_t
*mc1
= ( metaconn_t
* )c1
;
126 metaconn_t
*mc2
= ( metaconn_t
* )c2
;
128 /* Cannot have more than one shared session with same DN */
129 if ( mc1
->mc_conn
== mc2
->mc_conn
&&
130 dn_match( &mc1
->mc_local_ndn
, &mc2
->mc_local_ndn
) )
139 * Debug stuff (got it from libavl)
141 #if META_BACK_PRINT_CONNTREE > 0
143 meta_back_print( metaconn_t
*mc
, char *avlstr
)
147 fputs( "targets=[", stderr
);
148 for ( i
= 0; i
< mc
->mc_info
->mi_ntargets
; i
++ ) {
149 fputc( mc
->mc_conns
[ i
].msc_ld
? '*' : 'o', stderr
);
151 fputc( ']', stderr
);
153 fprintf( stderr
, " mc=%p local=\"%s\" conn=%p refcnt=%d%s %s\n",
155 mc
->mc_local_ndn
.bv_val
? mc
->mc_local_ndn
.bv_val
: "",
158 LDAP_BACK_CONN_TAINTED( mc
) ? " tainted" : "",
163 meta_back_ravl_print( Avlnode
*root
, int depth
)
171 meta_back_ravl_print( root
->avl_right
, depth
+ 1 );
173 for ( i
= 0; i
< depth
; i
++ ) {
174 fprintf( stderr
, "-" );
176 fputc( ' ', stderr
);
178 meta_back_print( (metaconn_t
*)root
->avl_data
,
179 avl_bf2str( root
->avl_bf
) );
181 meta_back_ravl_print( root
->avl_left
, depth
+ 1 );
184 /* NOTE: duplicate from back-ldap/bind.c */
185 static char* priv2str
[] = {
196 meta_back_print_conntree( metainfo_t
*mi
, char *msg
)
200 fprintf( stderr
, "========> %s\n", msg
);
202 for ( c
= LDAP_BACK_PCONN_FIRST
; c
< LDAP_BACK_PCONN_LAST
; c
++ ) {
206 fprintf( stderr
, " %s[%d]\n", priv2str
[ c
], mi
->mi_conn_priv
[ c
].mic_num
);
208 LDAP_TAILQ_FOREACH( mc
, &mi
->mi_conn_priv
[ c
].mic_priv
, mc_q
)
210 fprintf( stderr
, " [%d] ", i
);
211 meta_back_print( mc
, "" );
216 if ( mi
->mi_conninfo
.lai_tree
== NULL
) {
217 fprintf( stderr
, "\t(empty)\n" );
220 meta_back_ravl_print( mi
->mi_conninfo
.lai_tree
, 0 );
223 fprintf( stderr
, "<======== %s\n", msg
);
225 #endif /* META_BACK_PRINT_CONNTREE */
233 * Allocates a connection structure, making room for all the referenced targets
239 metainfo_t
*mi
= ( metainfo_t
* )op
->o_bd
->be_private
;
241 int ntargets
= mi
->mi_ntargets
;
243 assert( ntargets
> 0 );
245 /* malloc all in one */
246 mc
= ( metaconn_t
* )ch_calloc( 1, sizeof( metaconn_t
)
247 + sizeof( metasingleconn_t
) * ( ntargets
- 1 ) );
254 mc
->mc_authz_target
= META_BOUND_NONE
;
261 * meta_back_init_one_conn
263 * Initializes one connection
266 meta_back_init_one_conn(
272 ldap_back_send_t sendok
,
275 metainfo_t
*mi
= ( metainfo_t
* )op
->o_bd
->be_private
;
276 metatarget_t
*mt
= mi
->mi_targets
[ candidate
];
277 metasingleconn_t
*msc
= &mc
->mc_conns
[ candidate
];
280 int isauthz
= ( candidate
== mc
->mc_authz_target
);
284 #endif /* HAVE_TLS */
286 /* if the server is quarantined, and
287 * - the current interval did not expire yet, or
288 * - no more retries should occur,
289 * don't return the connection */
290 if ( mt
->mt_isquarantined
) {
291 slap_retry_info_t
*ri
= &mt
->mt_quarantine
;
294 if ( mt
->mt_quarantine
.ri_interval
) {
295 ldap_pvt_thread_mutex_lock( &mt
->mt_quarantine_mutex
);
296 if ( mt
->mt_isquarantined
== LDAP_BACK_FQ_YES
) {
297 dont_retry
= ( ri
->ri_num
[ ri
->ri_idx
] == SLAP_RETRYNUM_TAIL
298 || slap_get_time() < ri
->ri_last
+ ri
->ri_interval
[ ri
->ri_idx
] );
300 if ( LogTest( LDAP_DEBUG_ANY
) ) {
301 char buf
[ SLAP_TEXT_BUFLEN
];
303 snprintf( buf
, sizeof( buf
),
304 "meta_back_init_one_conn[%d]: quarantine "
305 "retry block #%d try #%d",
306 candidate
, ri
->ri_idx
, ri
->ri_count
);
307 Debug( LDAP_DEBUG_ANY
, "%s %s.\n",
308 op
->o_log_prefix
, buf
, 0 );
312 mt
->mt_isquarantined
= LDAP_BACK_FQ_RETRYING
;
314 ldap_pvt_thread_mutex_unlock( &mt
->mt_quarantine_mutex
);
318 rs
->sr_err
= LDAP_UNAVAILABLE
;
319 if ( op
->o_conn
&& ( sendok
& LDAP_BACK_SENDERR
) ) {
320 rs
->sr_text
= "Target is quarantined";
321 send_ldap_result( op
, rs
);
329 ldap_pvt_thread_mutex_lock( &mi
->mi_conninfo
.lai_mutex
);
335 if ( LDAP_BACK_CONN_ISBOUND( msc
)
336 || LDAP_BACK_CONN_ISANON( msc
) )
338 assert( msc
->msc_ld
!= NULL
);
339 rs
->sr_err
= LDAP_SUCCESS
;
342 } else if ( META_BACK_CONN_CREATING( msc
)
343 || LDAP_BACK_CONN_BINDING( msc
) )
345 if ( !LDAP_BACK_USE_TEMPORARIES( mi
) ) {
347 ldap_pvt_thread_mutex_unlock( &mi
->mi_conninfo
.lai_mutex
);
350 ldap_pvt_thread_yield();
354 /* sounds more appropriate */
355 rs
->sr_err
= LDAP_BUSY
;
356 rs
->sr_text
= "No connections to target are available";
359 } else if ( META_BACK_CONN_INITED( msc
) ) {
360 assert( msc
->msc_ld
!= NULL
);
361 rs
->sr_err
= LDAP_SUCCESS
;
368 META_BACK_CONN_CREATING_SET( msc
);
372 ldap_pvt_thread_mutex_unlock( &mi
->mi_conninfo
.lai_mutex
);
376 if ( rs
->sr_err
!= LDAP_SUCCESS
378 && ( sendok
& LDAP_BACK_SENDERR
) )
380 send_ldap_result( op
, rs
);
386 assert( msc
->msc_ld
== NULL
);
389 * Attempts to initialize the connection to the target ds
391 ldap_pvt_thread_mutex_lock( &mt
->mt_uri_mutex
);
392 rs
->sr_err
= ldap_initialize( &msc
->msc_ld
, mt
->mt_uri
);
394 is_ldaps
= ldap_is_ldaps_url( mt
->mt_uri
);
395 #endif /* HAVE_TLS */
396 ldap_pvt_thread_mutex_unlock( &mt
->mt_uri_mutex
);
397 if ( rs
->sr_err
!= LDAP_SUCCESS
) {
402 * Set LDAP version. This will always succeed: If the client
403 * bound with a particular version, then so can we.
405 if ( mt
->mt_version
!= 0 ) {
406 version
= mt
->mt_version
;
408 } else if ( op
->o_conn
->c_protocol
!= 0 ) {
409 version
= op
->o_conn
->c_protocol
;
412 version
= LDAP_VERSION3
;
414 ldap_set_option( msc
->msc_ld
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
415 ldap_set_urllist_proc( msc
->msc_ld
, mt
->mt_urllist_f
, mt
->mt_urllist_p
);
417 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
418 ldap_set_option( msc
->msc_ld
, LDAP_OPT_REFERRALS
,
419 LDAP_BACK_CHASE_REFERRALS( mi
) ? LDAP_OPT_ON
: LDAP_OPT_OFF
);
422 /* start TLS ("tls [try-]{start|propagate}" statement) */
423 if ( ( LDAP_BACK_USE_TLS( mi
)
424 || ( op
->o_conn
->c_is_tls
425 && LDAP_BACK_PROPAGATE_TLS( mi
) ) )
428 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
430 * use asynchronous StartTLS; in case, chase referral
431 * FIXME: OpenLDAP does not return referral on StartTLS yet
435 rs
->sr_err
= ldap_start_tls( msc
->msc_ld
, NULL
, NULL
, &msgid
);
436 if ( rs
->sr_err
== LDAP_SUCCESS
) {
437 LDAPMessage
*res
= NULL
;
438 int rc
, nretries
= mt
->mt_nretries
;
441 LDAP_BACK_TV_SET( &tv
);
444 rc
= ldap_result( msc
->msc_ld
, msgid
, LDAP_MSG_ALL
, &tv
, &res
);
447 rs
->sr_err
= LDAP_OTHER
;
451 if ( nretries
!= 0 ) {
452 if ( nretries
> 0 ) {
455 LDAP_BACK_TV_SET( &tv
);
458 rs
->sr_err
= LDAP_OTHER
;
462 /* only touch when activity actually took place... */
463 if ( mi
->mi_idle_timeout
!= 0 && msc
->msc_time
< op
->o_time
) {
464 msc
->msc_time
= op
->o_time
;
469 if ( rc
== LDAP_RES_EXTENDED
) {
470 struct berval
*data
= NULL
;
472 /* NOTE: right now, data is unused, so don't get it */
473 rs
->sr_err
= ldap_parse_extended_result( msc
->msc_ld
,
474 res
, NULL
, NULL
/* &data */ , 0 );
475 if ( rs
->sr_err
== LDAP_SUCCESS
) {
478 /* FIXME: matched? referrals? response controls? */
479 rs
->sr_err
= ldap_parse_result( msc
->msc_ld
,
480 res
, &err
, NULL
, NULL
, NULL
, NULL
, 1 );
483 if ( rs
->sr_err
== LDAP_SUCCESS
) {
487 /* FIXME: in case a referral
488 * is returned, should we try
489 * using it instead of the
491 if ( rs
->sr_err
== LDAP_SUCCESS
) {
492 ldap_install_tls( msc
->msc_ld
);
494 } else if ( rs
->sr_err
== LDAP_REFERRAL
) {
495 /* FIXME: LDAP_OPERATIONS_ERROR? */
496 rs
->sr_err
= LDAP_OTHER
;
497 rs
->sr_text
= "Unwilling to chase referral "
498 "returned by Start TLS exop";
507 rs
->sr_err
= LDAP_OTHER
;
514 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
516 * use synchronous StartTLS
518 rs
->sr_err
= ldap_start_tls_s( msc
->msc_ld
, NULL
, NULL
);
519 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
521 /* if StartTLS is requested, only attempt it if the URL
522 * is not "ldaps://"; this may occur not only in case
523 * of misconfiguration, but also when used in the chain
524 * overlay, where the "uri" can be parsed out of a referral */
525 if ( rs
->sr_err
== LDAP_SERVER_DOWN
526 || ( rs
->sr_err
!= LDAP_SUCCESS
527 && LDAP_BACK_TLS_CRITICAL( mi
) ) )
531 Debug( LDAP_DEBUG_ANY
,
532 "### %s meta_back_init_one_conn(TLS) "
533 "ldap_unbind_ext[%d] ld=%p\n",
534 op
->o_log_prefix
, candidate
,
535 (void *)msc
->msc_ld
);
536 #endif /* DEBUG_205 */
538 /* need to trash a failed Start TLS */
539 meta_clear_one_candidate( op
, mc
, candidate
);
543 #endif /* HAVE_TLS */
546 * Set the network timeout if set
548 if ( mt
->mt_network_timeout
!= 0 ) {
549 struct timeval network_timeout
;
551 network_timeout
.tv_usec
= 0;
552 network_timeout
.tv_sec
= mt
->mt_network_timeout
;
554 ldap_set_option( msc
->msc_ld
, LDAP_OPT_NETWORK_TIMEOUT
,
555 (void *)&network_timeout
);
559 * If the connection DN is not null, an attempt to rewrite it is made
563 if ( !BER_BVISNULL( &mt
->mt_idassert_authcDN
) ) {
564 ber_bvreplace( &msc
->msc_bound_ndn
, &mt
->mt_idassert_authcDN
);
565 if ( !BER_BVISNULL( &mt
->mt_idassert_passwd
) ) {
566 if ( !BER_BVISNULL( &msc
->msc_cred
) ) {
567 memset( msc
->msc_cred
.bv_val
, 0,
568 msc
->msc_cred
.bv_len
);
570 ber_bvreplace( &msc
->msc_cred
, &mt
->mt_idassert_passwd
);
574 ber_bvreplace( &msc
->msc_bound_ndn
, &slap_empty_bv
);
578 if ( !BER_BVISNULL( &msc
->msc_cred
) ) {
579 memset( msc
->msc_cred
.bv_val
, 0, msc
->msc_cred
.bv_len
);
580 ber_memfree_x( msc
->msc_cred
.bv_val
, NULL
);
581 BER_BVZERO( &msc
->msc_cred
);
583 if ( !BER_BVISNULL( &msc
->msc_bound_ndn
) ) {
584 ber_memfree_x( msc
->msc_bound_ndn
.bv_val
, NULL
);
585 BER_BVZERO( &msc
->msc_bound_ndn
);
587 if ( !BER_BVISEMPTY( &op
->o_ndn
)
588 && SLAP_IS_AUTHZ_BACKEND( op
)
592 dc
.conn
= op
->o_conn
;
597 * Rewrite the bind dn if needed
599 if ( ldap_back_dn_massage( &dc
, &op
->o_conn
->c_dn
,
600 &msc
->msc_bound_ndn
) )
604 Debug( LDAP_DEBUG_ANY
,
605 "### %s meta_back_init_one_conn(rewrite) "
606 "ldap_unbind_ext[%d] ld=%p\n",
607 op
->o_log_prefix
, candidate
,
608 (void *)msc
->msc_ld
);
609 #endif /* DEBUG_205 */
611 /* need to trash a connection not fully established */
612 meta_clear_one_candidate( op
, mc
, candidate
);
616 /* copy the DN if needed */
617 if ( msc
->msc_bound_ndn
.bv_val
== op
->o_conn
->c_dn
.bv_val
) {
618 ber_dupbv( &msc
->msc_bound_ndn
, &op
->o_conn
->c_dn
);
621 assert( !BER_BVISNULL( &msc
->msc_bound_ndn
) );
624 ber_dupbv( &msc
->msc_bound_ndn
, (struct berval
*)&slap_empty_bv
);
628 assert( !BER_BVISNULL( &msc
->msc_bound_ndn
) );
632 ldap_pvt_thread_mutex_lock( &mi
->mi_conninfo
.lai_mutex
);
634 META_BACK_CONN_CREATING_CLEAR( msc
);
635 if ( rs
->sr_err
== LDAP_SUCCESS
) {
637 * Sets a cookie for the rewrite session
639 ( void )rewrite_session_init( mt
->mt_rwmap
.rwm_rw
, op
->o_conn
);
640 META_BACK_CONN_INITED_SET( msc
);
643 ldap_pvt_thread_mutex_unlock( &mi
->mi_conninfo
.lai_mutex
);
646 if ( rs
->sr_err
!= LDAP_SUCCESS
) {
647 rs
->sr_err
= slap_map_api2result( rs
);
648 if ( sendok
& LDAP_BACK_SENDERR
) {
649 send_ldap_result( op
, rs
);
659 * Retries one connection
667 ldap_back_send_t sendok
)
669 metainfo_t
*mi
= ( metainfo_t
* )op
->o_bd
->be_private
;
670 metatarget_t
*mt
= mi
->mi_targets
[ candidate
];
671 metaconn_t
*mc
= *mcp
;
672 metasingleconn_t
*msc
= &mc
->mc_conns
[ candidate
];
673 int rc
= LDAP_UNAVAILABLE
,
677 ldap_pvt_thread_mutex_lock( &mi
->mi_conninfo
.lai_mutex
);
679 assert( !META_BACK_CONN_CREATING( msc
) );
680 binding
= LDAP_BACK_CONN_BINDING( msc
);
681 LDAP_BACK_CONN_BINDING_CLEAR( msc
);
683 assert( mc
->mc_refcnt
> 0 );
684 if ( mc
->mc_refcnt
== 1 ) {
685 if ( LogTest( LDAP_DEBUG_ANY
) ) {
686 char buf
[ SLAP_TEXT_BUFLEN
];
688 /* this lock is required; however,
689 * it's invoked only when logging is on */
690 ldap_pvt_thread_mutex_lock( &mt
->mt_uri_mutex
);
691 snprintf( buf
, sizeof( buf
),
692 "retrying URI=\"%s\" DN=\"%s\"",
694 BER_BVISNULL( &msc
->msc_bound_ndn
) ?
695 "" : msc
->msc_bound_ndn
.bv_val
);
696 ldap_pvt_thread_mutex_unlock( &mt
->mt_uri_mutex
);
698 Debug( LDAP_DEBUG_ANY
,
699 "%s meta_back_retry[%d]: %s.\n",
700 op
->o_log_prefix
, candidate
, buf
);
703 meta_clear_one_candidate( op
, mc
, candidate
);
704 LDAP_BACK_CONN_ISBOUND_CLEAR( msc
);
706 ( void )rewrite_session_delete( mt
->mt_rwmap
.rwm_rw
, op
->o_conn
);
708 /* mc here must be the regular mc, reset and ready for init */
709 rc
= meta_back_init_one_conn( op
, rs
, mc
, candidate
,
710 LDAP_BACK_CONN_ISPRIV( mc
), sendok
, 0 );
712 /* restore the "binding" flag, in case */
714 LDAP_BACK_CONN_BINDING_SET( msc
);
717 if ( rc
== LDAP_SUCCESS
) {
719 rc
= meta_back_single_dobind( op
, rs
, mcp
, candidate
,
720 sendok
, mt
->mt_nretries
, 0 );
722 Debug( LDAP_DEBUG_ANY
,
723 "%s meta_back_retry[%d]: "
724 "meta_back_single_dobind=%d\n",
725 op
->o_log_prefix
, candidate
, rc
);
726 if ( rc
== LDAP_SUCCESS
) {
727 if ( !BER_BVISNULL( &msc
->msc_bound_ndn
) &&
728 !BER_BVISEMPTY( &msc
->msc_bound_ndn
) )
730 LDAP_BACK_CONN_ISBOUND_SET( msc
);
733 LDAP_BACK_CONN_ISANON_SET( msc
);
736 /* when bound, dispose of the "binding" flag */
738 LDAP_BACK_CONN_BINDING_CLEAR( msc
);
743 /* don't send twice */
744 sendok
&= ~LDAP_BACK_SENDERR
;
747 if ( rc
!= LDAP_SUCCESS
) {
748 SlapReply
*candidates
= meta_back_candidates_get( op
);
750 candidates
[ candidate
].sr_err
= rc
;
752 if ( *mcp
!= NULL
) {
753 if ( mc
->mc_refcnt
== 1 ) {
755 LDAP_BACK_CONN_BINDING_CLEAR( msc
);
757 (void)meta_clear_one_candidate( op
, mc
, candidate
);
760 LDAP_BACK_CONN_TAINTED_SET( mc
);
761 /* only release if mandatory; otherwise
762 * let the caller do what's best before
764 if ( META_BACK_ONERR_STOP( mi
) ) {
765 meta_back_release_conn_lock( mi
, mc
, 0 );
769 #if META_BACK_PRINT_CONNTREE > 0
770 meta_back_print_conntree( mi
, ">>> meta_back_retry" );
771 #endif /* META_BACK_PRINT_CONNTREE */
773 /* FIXME: could be done better, reworking meta_back_release_conn_lock() */
774 if ( LDAP_BACK_PCONN_ISPRIV( mc
) ) {
775 if ( mc
->mc_q
.tqe_prev
!= NULL
) {
776 assert( LDAP_BACK_CONN_CACHED( mc
) );
777 assert( mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_num
> 0 );
778 LDAP_TAILQ_REMOVE( &mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_priv
,
780 mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_num
--;
781 LDAP_TAILQ_ENTRY_INIT( mc
, mc_q
);
784 assert( !LDAP_BACK_CONN_CACHED( mc
) );
788 /* FIXME: check if in tree, for consistency? */
789 (void)avl_delete( &mi
->mi_conninfo
.lai_tree
,
790 ( caddr_t
)mc
, meta_back_conndnmc_cmp
);
792 LDAP_BACK_CONN_CACHED_CLEAR( mc
);
794 #if META_BACK_PRINT_CONNTREE > 0
795 meta_back_print_conntree( mi
, "<<< meta_back_retry" );
796 #endif /* META_BACK_PRINT_CONNTREE */
800 if ( sendok
& LDAP_BACK_SENDERR
) {
802 rs
->sr_text
= "Unable to retry";
803 send_ldap_result( op
, rs
);
807 if ( quarantine
&& META_BACK_TGT_QUARANTINE( mt
) ) {
808 meta_back_quarantine( op
, rs
, candidate
);
811 ldap_pvt_thread_mutex_unlock( &mi
->mi_conninfo
.lai_mutex
);
813 return rc
== LDAP_SUCCESS
? 1 : 0;
817 * callback for unique candidate selection
820 meta_back_conn_cb( Operation
*op
, SlapReply
*rs
)
822 assert( op
->o_tag
== LDAP_REQ_SEARCH
);
824 switch ( rs
->sr_type
) {
826 ((long *)op
->o_callback
->sc_private
)[0] = (long)op
->o_private
;
842 meta_back_get_candidate(
847 metainfo_t
*mi
= ( metainfo_t
* )op
->o_bd
->be_private
;
851 * tries to get a unique candidate
852 * (takes care of default target)
854 candidate
= meta_back_select_unique_candidate( mi
, ndn
);
857 * if any is found, inits the connection
859 if ( candidate
== META_TARGET_NONE
) {
860 rs
->sr_err
= LDAP_NO_SUCH_OBJECT
;
861 rs
->sr_text
= "No suitable candidate target found";
863 } else if ( candidate
== META_TARGET_MULTIPLE
) {
865 SlapReply rs2
= { 0 };
866 slap_callback cb2
= { 0 };
869 /* try to get a unique match for the request ndn
870 * among the multiple candidates available */
871 op2
.o_tag
= LDAP_REQ_SEARCH
;
873 op2
.o_req_ndn
= *ndn
;
874 op2
.ors_scope
= LDAP_SCOPE_BASE
;
875 op2
.ors_deref
= LDAP_DEREF_NEVER
;
876 op2
.ors_attrs
= slap_anlist_no_attrs
;
877 op2
.ors_attrsonly
= 0;
878 op2
.ors_limit
= NULL
;
880 op2
.ors_tlimit
= SLAP_NO_LIMIT
;
882 op2
.ors_filter
= (Filter
*)slap_filter_objectClass_pres
;
883 op2
.ors_filterstr
= *slap_filterstr_objectClass_pres
;
885 op2
.o_callback
= &cb2
;
886 cb2
.sc_response
= meta_back_conn_cb
;
887 cb2
.sc_private
= (void *)&candidate
;
889 rc
= op
->o_bd
->be_search( &op2
, &rs2
);
891 switch ( rs2
.sr_err
) {
894 rs
->sr_err
= rs2
.sr_err
;
897 case LDAP_SIZELIMIT_EXCEEDED
:
898 /* if multiple candidates can serve the operation,
899 * and a default target is defined, and it is
900 * a candidate, try using it (FIXME: YMMV) */
901 if ( mi
->mi_defaulttarget
!= META_DEFAULT_TARGET_NONE
902 && meta_back_is_candidate( mi
->mi_targets
[ mi
->mi_defaulttarget
],
903 ndn
, op
->o_tag
== LDAP_REQ_SEARCH
? op
->ors_scope
: LDAP_SCOPE_BASE
) )
905 candidate
= mi
->mi_defaulttarget
;
906 rs
->sr_err
= LDAP_SUCCESS
;
910 rs
->sr_err
= LDAP_UNWILLING_TO_PERFORM
;
911 rs
->sr_text
= "Unable to select unique candidate target";
917 rs
->sr_err
= LDAP_SUCCESS
;
923 static void *meta_back_candidates_dummy
;
926 meta_back_candidates_keyfree(
930 metacandidates_t
*mc
= (metacandidates_t
*)data
;
932 ber_memfree_x( mc
->mc_candidates
, NULL
);
933 ber_memfree_x( data
, NULL
);
937 meta_back_candidates_get( Operation
*op
)
939 metainfo_t
*mi
= ( metainfo_t
* )op
->o_bd
->be_private
;
940 metacandidates_t
*mc
;
942 if ( op
->o_threadctx
) {
945 ldap_pvt_thread_pool_getkey( op
->o_threadctx
,
946 &meta_back_candidates_dummy
, &data
, NULL
);
947 mc
= (metacandidates_t
*)data
;
950 mc
= mi
->mi_candidates
;
954 mc
= ch_calloc( sizeof( metacandidates_t
), 1 );
955 mc
->mc_ntargets
= mi
->mi_ntargets
;
956 mc
->mc_candidates
= ch_calloc( sizeof( SlapReply
), mc
->mc_ntargets
);
957 if ( op
->o_threadctx
) {
961 ldap_pvt_thread_pool_setkey( op
->o_threadctx
,
962 &meta_back_candidates_dummy
, data
,
963 meta_back_candidates_keyfree
,
967 mi
->mi_candidates
= mc
;
970 } else if ( mc
->mc_ntargets
< mi
->mi_ntargets
) {
971 /* NOTE: in the future, may want to allow back-config
972 * to add/remove targets from back-meta... */
973 mc
->mc_candidates
= ch_realloc( mc
->mc_candidates
,
974 sizeof( SlapReply
) * mi
->mi_ntargets
);
975 memset( &mc
->mc_candidates
[ mc
->mc_ntargets
], 0,
976 sizeof( SlapReply
) * ( mi
->mi_ntargets
- mc
->mc_ntargets
) );
977 mc
->mc_ntargets
= mi
->mi_ntargets
;
980 return mc
->mc_candidates
;
986 * Prepares the connection structure
990 * - determine what DN is being requested:
992 * op requires candidate checks
994 * add unique parent of o_req_ndn
995 * bind unique^*[/all] o_req_ndn [no check]
996 * compare unique^+ o_req_ndn
997 * delete unique o_req_ndn
998 * modify unique o_req_ndn
999 * search any o_req_ndn
1000 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
1002 * - for ops that require the candidate to be unique, in case of multiple
1003 * occurrences an internal search with sizeLimit=1 is performed
1004 * if a unique candidate can actually be determined. If none is found,
1005 * the operation aborts; if multiple are found, the default target
1006 * is used if defined and candidate; otherwise the operation aborts.
1008 * *^note: actually, the bind operation is handled much like a search;
1009 * i.e. the bind is broadcast to all candidate targets.
1011 * +^note: actually, the compare operation is handled much like a search;
1012 * i.e. the compare is broadcast to all candidate targets, while checking
1013 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
1021 ldap_back_send_t sendok
)
1023 metainfo_t
*mi
= ( metainfo_t
* )op
->o_bd
->be_private
;
1024 metaconn_t
*mc
= NULL
,
1026 int cached
= META_TARGET_NONE
,
1027 i
= META_TARGET_NONE
,
1033 meta_op_type op_type
= META_OP_REQUIRE_SINGLE
;
1037 META_DNTYPE_NEWPARENT
1038 } dn_type
= META_DNTYPE_ENTRY
;
1039 struct berval ndn
= op
->o_req_ndn
,
1042 SlapReply
*candidates
= meta_back_candidates_get( op
);
1044 /* Internal searches are privileged and shared. So is root. */
1045 if ( ( !BER_BVISEMPTY( &op
->o_ndn
) && META_BACK_PROXYAUTHZ_ALWAYS( mi
) )
1046 || ( BER_BVISEMPTY( &op
->o_ndn
) && META_BACK_PROXYAUTHZ_ANON( mi
) )
1047 || op
->o_do_not_cache
|| be_isroot( op
) )
1049 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr
);
1050 mc_curr
.mc_local_ndn
= op
->o_bd
->be_rootndn
;
1051 LDAP_BACK_PCONN_ROOTDN_SET( &mc_curr
, op
);
1053 } else if ( BER_BVISEMPTY( &op
->o_ndn
) && META_BACK_PROXYAUTHZ_NOANON( mi
) )
1055 LDAP_BACK_CONN_ISANON_SET( &mc_curr
);
1056 BER_BVSTR( &mc_curr
.mc_local_ndn
, "" );
1057 LDAP_BACK_PCONN_ANON_SET( &mc_curr
, op
);
1060 mc_curr
.mc_local_ndn
= op
->o_ndn
;
1062 /* Explicit binds must not be shared */
1063 if ( !BER_BVISEMPTY( &op
->o_ndn
)
1064 || op
->o_tag
== LDAP_REQ_BIND
1065 || SLAP_IS_AUTHZ_BACKEND( op
) )
1067 mc_curr
.mc_conn
= op
->o_conn
;
1070 LDAP_BACK_CONN_ISANON_SET( &mc_curr
);
1071 LDAP_BACK_PCONN_ANON_SET( &mc_curr
, op
);
1075 /* Explicit Bind requests always get their own conn */
1076 if ( sendok
& LDAP_BACK_BINDING
) {
1077 mc_curr
.mc_conn
= op
->o_conn
;
1080 /* Searches for a metaconn in the avl tree */
1082 ldap_pvt_thread_mutex_lock( &mi
->mi_conninfo
.lai_mutex
);
1083 if ( LDAP_BACK_PCONN_ISPRIV( &mc_curr
) ) {
1084 /* lookup a conn that's not binding */
1085 LDAP_TAILQ_FOREACH( mc
,
1086 &mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( &mc_curr
) ].mic_priv
,
1089 if ( !LDAP_BACK_CONN_BINDING( mc
) && mc
->mc_refcnt
== 0 ) {
1095 if ( mc
!= LDAP_TAILQ_LAST( &mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_priv
,
1096 metaconn_t
, mc_q
) )
1098 LDAP_TAILQ_REMOVE( &mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_priv
,
1100 LDAP_TAILQ_ENTRY_INIT( mc
, mc_q
);
1101 LDAP_TAILQ_INSERT_TAIL( &mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_priv
,
1105 } else if ( !LDAP_BACK_USE_TEMPORARIES( mi
)
1106 && mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( &mc_curr
) ].mic_num
== mi
->mi_conn_priv_max
)
1108 mc
= LDAP_TAILQ_FIRST( &mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( &mc_curr
) ].mic_priv
);
1113 mc
= (metaconn_t
*)avl_find( mi
->mi_conninfo
.lai_tree
,
1114 (caddr_t
)&mc_curr
, meta_back_conndn_cmp
);
1118 /* catch taint errors */
1119 assert( !LDAP_BACK_CONN_TAINTED( mc
) );
1121 /* Don't reuse connections while they're still binding
1122 * NOTE: only makes sense for binds */
1123 if ( LDAP_BACK_CONN_BINDING( mc
) ) {
1124 if ( !LDAP_BACK_USE_TEMPORARIES( mi
) ) {
1125 ldap_pvt_thread_mutex_unlock( &mi
->mi_conninfo
.lai_mutex
);
1127 ldap_pvt_thread_yield();
1131 /* release conn, and create a temporary */
1135 if ( ( mi
->mi_conn_ttl
!= 0 && op
->o_time
> mc
->mc_create_time
+ mi
->mi_conn_ttl
)
1136 || ( mi
->mi_idle_timeout
!= 0 && op
->o_time
> mc
->mc_time
+ mi
->mi_idle_timeout
) )
1138 #if META_BACK_PRINT_CONNTREE > 0
1139 meta_back_print_conntree( mi
,
1140 ">>> meta_back_getconn(expired)" );
1141 #endif /* META_BACK_PRINT_CONNTREE */
1143 /* don't let anyone else use this expired connection */
1144 if ( LDAP_BACK_PCONN_ISPRIV( mc
) ) {
1145 if ( mc
->mc_q
.tqe_prev
!= NULL
) {
1146 assert( LDAP_BACK_CONN_CACHED( mc
) );
1147 assert( mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_num
> 0 );
1148 LDAP_TAILQ_REMOVE( &mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_priv
,
1150 mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_num
--;
1151 LDAP_TAILQ_ENTRY_INIT( mc
, mc_q
);
1154 assert( !LDAP_BACK_CONN_CACHED( mc
) );
1158 (void)avl_delete( &mi
->mi_conninfo
.lai_tree
,
1159 (caddr_t
)mc
, meta_back_conndnmc_cmp
);
1162 #if META_BACK_PRINT_CONNTREE > 0
1163 meta_back_print_conntree( mi
,
1164 "<<< meta_back_getconn(expired)" );
1165 #endif /* META_BACK_PRINT_CONNTREE */
1166 LDAP_BACK_CONN_TAINTED_SET( mc
);
1167 LDAP_BACK_CONN_CACHED_CLEAR( mc
);
1169 Debug( LDAP_DEBUG_TRACE
, "%s meta_back_getconn: mc=%p conn=%ld expired (tainted).\n",
1170 op
->o_log_prefix
, (void *)mc
, LDAP_BACK_PCONN_ID( mc
) );
1176 ldap_pvt_thread_mutex_unlock( &mi
->mi_conninfo
.lai_mutex
);
1179 switch ( op
->o_tag
) {
1181 /* if we go to selection, the entry must not exist,
1182 * and we must be able to resolve the parent */
1183 dn_type
= META_DNTYPE_PARENT
;
1184 dnParent( &ndn
, &pndn
);
1187 case LDAP_REQ_MODRDN
:
1188 /* if nnewSuperior is not NULL, it must resolve
1189 * to the same candidate as the req_ndn */
1190 if ( op
->orr_nnewSup
) {
1191 dn_type
= META_DNTYPE_NEWPARENT
;
1196 /* if bound as rootdn, the backend must bind to all targets
1197 * with the administrative identity
1198 * (unless pseoudoroot-bind-defer is TRUE) */
1199 if ( op
->orb_method
== LDAP_AUTH_SIMPLE
&& be_isroot_pw( op
) ) {
1200 op_type
= META_OP_REQUIRE_ALL
;
1204 case LDAP_REQ_COMPARE
:
1205 case LDAP_REQ_DELETE
:
1206 case LDAP_REQ_MODIFY
:
1207 /* just a unique candidate */
1210 case LDAP_REQ_SEARCH
:
1211 /* allow multiple candidates for the searchBase */
1212 op_type
= META_OP_ALLOW_MULTIPLE
;
1216 /* right now, just break (exop?) */
1221 * require all connections ...
1223 if ( op_type
== META_OP_REQUIRE_ALL
) {
1225 /* Looks like we didn't get a bind. Open a new session... */
1227 assert( new_conn
== 0 );
1228 mc
= metaconn_alloc( op
);
1229 mc
->mc_conn
= mc_curr
.mc_conn
;
1230 ber_dupbv( &mc
->mc_local_ndn
, &mc_curr
.mc_local_ndn
);
1232 if ( sendok
& LDAP_BACK_BINDING
) {
1233 LDAP_BACK_CONN_BINDING_SET( mc
);
1235 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr
) ) {
1236 LDAP_BACK_CONN_ISPRIV_SET( mc
);
1238 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr
) ) {
1239 LDAP_BACK_CONN_ISANON_SET( mc
);
1243 /* TODO: if any of the connections is binding,
1244 * release mc and create a new one */
1247 for ( i
= 0; i
< mi
->mi_ntargets
; i
++ ) {
1249 * The target is activated; if needed, it is
1252 candidates
[ i
].sr_err
= meta_back_init_one_conn( op
,
1253 rs
, mc
, i
, LDAP_BACK_CONN_ISPRIV( &mc_curr
),
1254 LDAP_BACK_DONTSEND
, !new_conn
);
1255 if ( candidates
[ i
].sr_err
== LDAP_SUCCESS
) {
1256 if ( new_conn
&& ( sendok
& LDAP_BACK_BINDING
) ) {
1257 LDAP_BACK_CONN_BINDING_SET( &mc
->mc_conns
[ i
] );
1259 META_CANDIDATE_SET( &candidates
[ i
] );
1265 * FIXME: in case one target cannot
1266 * be init'd, should the other ones
1269 META_CANDIDATE_RESET( &candidates
[ i
] );
1270 err
= candidates
[ i
].sr_err
;
1275 if ( ncandidates
== 0 ) {
1278 meta_back_conn_free( mc
);
1281 meta_back_release_conn( mi
, mc
);
1284 rs
->sr_err
= LDAP_NO_SUCH_OBJECT
;
1285 rs
->sr_text
= "Unable to select valid candidates";
1287 if ( sendok
& LDAP_BACK_SENDERR
) {
1288 if ( rs
->sr_err
== LDAP_NO_SUCH_OBJECT
) {
1289 rs
->sr_matched
= op
->o_bd
->be_suffix
[ 0 ].bv_val
;
1291 send_ldap_result( op
, rs
);
1292 rs
->sr_matched
= NULL
;
1302 * looks in cache, if any
1304 if ( mi
->mi_cache
.ttl
!= META_DNCACHE_DISABLED
) {
1305 cached
= i
= meta_dncache_get_target( &mi
->mi_cache
, &op
->o_req_ndn
);
1308 if ( op_type
== META_OP_REQUIRE_SINGLE
) {
1309 metatarget_t
*mt
= NULL
;
1310 metasingleconn_t
*msc
= NULL
;
1314 for ( j
= 0; j
< mi
->mi_ntargets
; j
++ ) {
1315 META_CANDIDATE_RESET( &candidates
[ j
] );
1319 * tries to get a unique candidate
1320 * (takes care of default target)
1322 if ( i
== META_TARGET_NONE
) {
1323 i
= meta_back_get_candidate( op
, rs
, &ndn
);
1325 if ( rs
->sr_err
== LDAP_NO_SUCH_OBJECT
&& dn_type
== META_DNTYPE_PARENT
) {
1326 i
= meta_back_get_candidate( op
, rs
, &pndn
);
1329 if ( i
< 0 || rs
->sr_err
!= LDAP_SUCCESS
) {
1331 meta_back_release_conn( mi
, mc
);
1334 if ( sendok
& LDAP_BACK_SENDERR
) {
1335 if ( rs
->sr_err
== LDAP_NO_SUCH_OBJECT
) {
1336 rs
->sr_matched
= op
->o_bd
->be_suffix
[ 0 ].bv_val
;
1338 send_ldap_result( op
, rs
);
1339 rs
->sr_matched
= NULL
;
1346 if ( dn_type
== META_DNTYPE_NEWPARENT
&& meta_back_get_candidate( op
, rs
, op
->orr_nnewSup
) != i
)
1349 meta_back_release_conn( mi
, mc
);
1352 rs
->sr_err
= LDAP_UNWILLING_TO_PERFORM
;
1353 rs
->sr_text
= "Cross-target rename not supported";
1354 if ( sendok
& LDAP_BACK_SENDERR
) {
1355 send_ldap_result( op
, rs
);
1361 Debug( LDAP_DEBUG_TRACE
,
1362 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1363 i
, op
->o_req_ndn
.bv_val
, 0 );
1366 /* Retries searching for a metaconn in the avl tree
1367 * the reason is that the connection might have been
1368 * created by meta_back_get_candidate() */
1369 if ( !( sendok
& LDAP_BACK_BINDING
) ) {
1371 ldap_pvt_thread_mutex_lock( &mi
->mi_conninfo
.lai_mutex
);
1372 mc
= (metaconn_t
*)avl_find( mi
->mi_conninfo
.lai_tree
,
1373 (caddr_t
)&mc_curr
, meta_back_conndn_cmp
);
1375 /* catch taint errors */
1376 assert( !LDAP_BACK_CONN_TAINTED( mc
) );
1378 /* Don't reuse connections while they're still binding */
1379 if ( META_BACK_CONN_CREATING( &mc
->mc_conns
[ i
] )
1380 || LDAP_BACK_CONN_BINDING( &mc
->mc_conns
[ i
] ) )
1382 if ( !LDAP_BACK_USE_TEMPORARIES( mi
) ) {
1383 ldap_pvt_thread_mutex_unlock( &mi
->mi_conninfo
.lai_mutex
);
1384 ldap_pvt_thread_yield();
1394 ldap_pvt_thread_mutex_unlock( &mi
->mi_conninfo
.lai_mutex
);
1397 /* Looks like we didn't get a bind. Open a new session... */
1399 assert( new_conn
== 0 );
1400 mc
= metaconn_alloc( op
);
1401 mc
->mc_conn
= mc_curr
.mc_conn
;
1402 ber_dupbv( &mc
->mc_local_ndn
, &mc_curr
.mc_local_ndn
);
1404 if ( sendok
& LDAP_BACK_BINDING
) {
1405 LDAP_BACK_CONN_BINDING_SET( mc
);
1407 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr
) ) {
1408 LDAP_BACK_CONN_ISPRIV_SET( mc
);
1410 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr
) ) {
1411 LDAP_BACK_CONN_ISANON_SET( mc
);
1417 * Clear all other candidates
1419 ( void )meta_clear_unused_candidates( op
, i
);
1421 mt
= mi
->mi_targets
[ i
];
1422 msc
= &mc
->mc_conns
[ i
];
1425 * The target is activated; if needed, it is
1426 * also init'd. In case of error, meta_back_init_one_conn
1427 * sends the appropriate result.
1429 err
= meta_back_init_one_conn( op
, rs
, mc
, i
,
1430 LDAP_BACK_CONN_ISPRIV( &mc_curr
), sendok
, !new_conn
);
1431 if ( err
!= LDAP_SUCCESS
) {
1433 * FIXME: in case one target cannot
1434 * be init'd, should the other ones
1437 META_CANDIDATE_RESET( &candidates
[ i
] );
1440 meta_back_conn_free( mc
);
1443 meta_back_release_conn( mi
, mc
);
1448 if ( new_conn
&& ( sendok
& LDAP_BACK_BINDING
) ) {
1449 LDAP_BACK_CONN_BINDING_SET( &mc
->mc_conns
[ i
] );
1452 candidates
[ i
].sr_err
= LDAP_SUCCESS
;
1453 META_CANDIDATE_SET( &candidates
[ i
] );
1461 * if no unique candidate ...
1465 /* Looks like we didn't get a bind. Open a new session... */
1467 assert( new_conn
== 0 );
1468 mc
= metaconn_alloc( op
);
1469 mc
->mc_conn
= mc_curr
.mc_conn
;
1470 ber_dupbv( &mc
->mc_local_ndn
, &mc_curr
.mc_local_ndn
);
1472 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr
) ) {
1473 LDAP_BACK_CONN_ISPRIV_SET( mc
);
1475 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr
) ) {
1476 LDAP_BACK_CONN_ISANON_SET( mc
);
1480 for ( i
= 0; i
< mi
->mi_ntargets
; i
++ ) {
1481 metatarget_t
*mt
= mi
->mi_targets
[ i
];
1483 META_CANDIDATE_RESET( &candidates
[ i
] );
1486 || meta_back_is_candidate( mt
, &op
->o_req_ndn
,
1487 LDAP_SCOPE_SUBTREE
) )
1491 * The target is activated; if needed, it is
1494 int lerr
= meta_back_init_one_conn( op
, rs
, mc
, i
,
1495 LDAP_BACK_CONN_ISPRIV( &mc_curr
),
1496 LDAP_BACK_DONTSEND
, !new_conn
);
1497 candidates
[ i
].sr_err
= lerr
;
1498 if ( lerr
== LDAP_SUCCESS
) {
1499 META_CANDIDATE_SET( &candidates
[ i
] );
1502 Debug( LDAP_DEBUG_TRACE
, "%s: meta_back_getconn[%d]\n",
1503 op
->o_log_prefix
, i
, 0 );
1505 } else if ( lerr
== LDAP_UNAVAILABLE
&& !META_BACK_ONERR_STOP( mi
) ) {
1506 META_CANDIDATE_SET( &candidates
[ i
] );
1508 Debug( LDAP_DEBUG_TRACE
, "%s: meta_back_getconn[%d] %s\n",
1509 op
->o_log_prefix
, i
,
1510 mt
->mt_isquarantined
!= LDAP_BACK_FQ_NO
? "quarantined" : "unavailable" );
1515 * FIXME: in case one target cannot
1516 * be init'd, should the other ones
1520 ( void )meta_clear_one_candidate( op
, mc
, i
);
1522 /* leave the target candidate, but record the error for later use */
1525 if ( lerr
== LDAP_UNAVAILABLE
&& mt
->mt_isquarantined
!= LDAP_BACK_FQ_NO
) {
1526 Debug( LDAP_DEBUG_TRACE
, "%s: meta_back_getconn[%d] quarantined err=%d\n",
1527 op
->o_log_prefix
, i
, lerr
);
1530 Debug( LDAP_DEBUG_ANY
, "%s: meta_back_getconn[%d] failed err=%d\n",
1531 op
->o_log_prefix
, i
, lerr
);
1534 if ( META_BACK_ONERR_STOP( mi
) ) {
1535 if ( sendok
& LDAP_BACK_SENDERR
) {
1536 send_ldap_result( op
, rs
);
1540 meta_back_conn_free( mc
);
1543 meta_back_release_conn( mi
, mc
);
1554 ( void )meta_clear_one_candidate( op
, mc
, i
);
1559 if ( ncandidates
== 0 ) {
1562 meta_back_conn_free( mc
);
1565 meta_back_release_conn( mi
, mc
);
1568 if ( rs
->sr_err
== LDAP_SUCCESS
) {
1569 rs
->sr_err
= LDAP_NO_SUCH_OBJECT
;
1570 rs
->sr_text
= "Unable to select valid candidates";
1573 if ( sendok
& LDAP_BACK_SENDERR
) {
1574 if ( rs
->sr_err
== LDAP_NO_SUCH_OBJECT
) {
1575 rs
->sr_matched
= op
->o_bd
->be_suffix
[ 0 ].bv_val
;
1577 send_ldap_result( op
, rs
);
1578 rs
->sr_matched
= NULL
;
1586 /* clear out meta_back_init_one_conn non-fatal errors */
1587 rs
->sr_err
= LDAP_SUCCESS
;
1590 /* touch the timestamp */
1591 if ( mi
->mi_idle_timeout
!= 0 ) {
1592 mc
->mc_time
= op
->o_time
;
1596 if ( mi
->mi_conn_ttl
) {
1597 mc
->mc_create_time
= op
->o_time
;
1601 * Inserts the newly created metaconn in the avl tree
1603 ldap_pvt_thread_mutex_lock( &mi
->mi_conninfo
.lai_mutex
);
1604 #if META_BACK_PRINT_CONNTREE > 0
1605 meta_back_print_conntree( mi
, ">>> meta_back_getconn" );
1606 #endif /* META_BACK_PRINT_CONNTREE */
1609 if ( LDAP_BACK_PCONN_ISPRIV( mc
) ) {
1610 if ( mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_num
< mi
->mi_conn_priv_max
) {
1611 LDAP_TAILQ_INSERT_TAIL( &mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_priv
, mc
, mc_q
);
1612 mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_num
++;
1613 LDAP_BACK_CONN_CACHED_SET( mc
);
1616 LDAP_BACK_CONN_TAINTED_SET( mc
);
1620 } else if ( !( sendok
& LDAP_BACK_BINDING
) ) {
1621 err
= avl_insert( &mi
->mi_conninfo
.lai_tree
, ( caddr_t
)mc
,
1622 meta_back_conndn_cmp
, meta_back_conndn_dup
);
1623 LDAP_BACK_CONN_CACHED_SET( mc
);
1626 #if META_BACK_PRINT_CONNTREE > 0
1627 meta_back_print_conntree( mi
, "<<< meta_back_getconn" );
1628 #endif /* META_BACK_PRINT_CONNTREE */
1629 ldap_pvt_thread_mutex_unlock( &mi
->mi_conninfo
.lai_mutex
);
1631 if ( !LDAP_BACK_PCONN_ISPRIV( mc
) ) {
1633 * Err could be -1 in case a duplicate metaconn is inserted
1640 LDAP_BACK_CONN_CACHED_CLEAR( mc
);
1641 /* duplicate: free and try to get the newly created one */
1642 if ( !( sendok
& LDAP_BACK_BINDING
) && !LDAP_BACK_USE_TEMPORARIES( mi
) ) {
1644 meta_back_conn_free( mc
);
1650 LDAP_BACK_CONN_TAINTED_SET( mc
);
1654 LDAP_BACK_CONN_CACHED_CLEAR( mc
);
1655 Debug( LDAP_DEBUG_ANY
,
1656 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1657 op
->o_log_prefix
, ncandidates
,
1658 LDAP_BACK_PCONN_ID( mc
) );
1661 meta_back_conn_free( mc
);
1663 rs
->sr_err
= LDAP_OTHER
;
1664 rs
->sr_text
= "Proxy bind collision";
1665 if ( sendok
& LDAP_BACK_SENDERR
) {
1666 send_ldap_result( op
, rs
);
1672 Debug( LDAP_DEBUG_TRACE
,
1673 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1674 op
->o_log_prefix
, ncandidates
,
1675 LDAP_BACK_PCONN_ID( mc
) );
1678 Debug( LDAP_DEBUG_TRACE
,
1679 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1680 op
->o_log_prefix
, ncandidates
,
1681 LDAP_BACK_PCONN_ID( mc
) );
1688 meta_back_release_conn_lock(
1693 assert( mc
!= NULL
);
1696 ldap_pvt_thread_mutex_lock( &mi
->mi_conninfo
.lai_mutex
);
1698 assert( mc
->mc_refcnt
> 0 );
1700 /* NOTE: the connection is removed if either it is tainted
1701 * or if it is shared and no one else is using it. This needs
1702 * to occur because for intrinsic reasons cached connections
1703 * that are not privileged would live forever and pollute
1704 * the connection space (and eat up resources). Maybe this
1705 * should be configurable... */
1706 if ( LDAP_BACK_CONN_TAINTED( mc
) || !LDAP_BACK_CONN_CACHED( mc
) ) {
1707 #if META_BACK_PRINT_CONNTREE > 0
1708 meta_back_print_conntree( mi
, ">>> meta_back_release_conn" );
1709 #endif /* META_BACK_PRINT_CONNTREE */
1711 if ( LDAP_BACK_PCONN_ISPRIV( mc
) ) {
1712 if ( mc
->mc_q
.tqe_prev
!= NULL
) {
1713 assert( LDAP_BACK_CONN_CACHED( mc
) );
1714 assert( mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_num
> 0 );
1715 LDAP_TAILQ_REMOVE( &mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_priv
, mc
, mc_q
);
1716 mi
->mi_conn_priv
[ LDAP_BACK_CONN2PRIV( mc
) ].mic_num
--;
1717 LDAP_TAILQ_ENTRY_INIT( mc
, mc_q
);
1720 assert( !LDAP_BACK_CONN_CACHED( mc
) );
1723 } else if ( LDAP_BACK_CONN_CACHED( mc
) ) {
1726 tmpmc
= avl_delete( &mi
->mi_conninfo
.lai_tree
,
1727 ( caddr_t
)mc
, meta_back_conndnmc_cmp
);
1729 /* Overparanoid, but useful... */
1730 assert( tmpmc
== NULL
|| tmpmc
== mc
);
1733 LDAP_BACK_CONN_CACHED_CLEAR( mc
);
1735 #if META_BACK_PRINT_CONNTREE > 0
1736 meta_back_print_conntree( mi
, "<<< meta_back_release_conn" );
1737 #endif /* META_BACK_PRINT_CONNTREE */
1739 if ( mc
->mc_refcnt
== 0 ) {
1740 meta_back_conn_free( mc
);
1745 if ( mc
!= NULL
&& LDAP_BACK_CONN_BINDING( mc
) ) {
1746 LDAP_BACK_CONN_BINDING_CLEAR( mc
);
1750 ldap_pvt_thread_mutex_unlock( &mi
->mi_conninfo
.lai_mutex
);
1755 meta_back_quarantine(
1760 metainfo_t
*mi
= (metainfo_t
*)op
->o_bd
->be_private
;
1761 metatarget_t
*mt
= mi
->mi_targets
[ candidate
];
1763 slap_retry_info_t
*ri
= &mt
->mt_quarantine
;
1765 ldap_pvt_thread_mutex_lock( &mt
->mt_quarantine_mutex
);
1767 if ( rs
->sr_err
== LDAP_UNAVAILABLE
) {
1768 time_t new_last
= slap_get_time();
1770 switch ( mt
->mt_isquarantined
) {
1771 case LDAP_BACK_FQ_NO
:
1772 if ( ri
->ri_last
== new_last
) {
1776 Debug( LDAP_DEBUG_ANY
,
1777 "%s meta_back_quarantine[%d]: enter.\n",
1778 op
->o_log_prefix
, candidate
, 0 );
1784 case LDAP_BACK_FQ_RETRYING
:
1785 if ( LogTest( LDAP_DEBUG_ANY
) ) {
1786 char buf
[ SLAP_TEXT_BUFLEN
];
1788 snprintf( buf
, sizeof( buf
),
1789 "meta_back_quarantine[%d]: block #%d try #%d failed",
1790 candidate
, ri
->ri_idx
, ri
->ri_count
);
1791 Debug( LDAP_DEBUG_ANY
, "%s %s.\n",
1792 op
->o_log_prefix
, buf
, 0 );
1796 if ( ri
->ri_num
[ ri
->ri_idx
] != SLAP_RETRYNUM_FOREVER
1797 && ri
->ri_count
== ri
->ri_num
[ ri
->ri_idx
] )
1808 mt
->mt_isquarantined
= LDAP_BACK_FQ_YES
;
1809 ri
->ri_last
= new_last
;
1811 } else if ( mt
->mt_isquarantined
== LDAP_BACK_FQ_RETRYING
) {
1812 Debug( LDAP_DEBUG_ANY
,
1813 "%s meta_back_quarantine[%d]: exit.\n",
1814 op
->o_log_prefix
, candidate
, 0 );
1816 if ( mi
->mi_quarantine_f
) {
1817 (void)mi
->mi_quarantine_f( mi
, candidate
,
1818 mi
->mi_quarantine_p
);
1823 mt
->mt_isquarantined
= LDAP_BACK_FQ_NO
;
1827 ldap_pvt_thread_mutex_unlock( &mt
->mt_quarantine_mutex
);