2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 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 * Set PRINT_CONNTREE larger than 0 to dump the connection tree (debug only)
40 #ifndef PRINT_CONNTREE
41 #define PRINT_CONNTREE 0
42 #endif /* !PRINT_CONNTREE */
45 * meta_back_conndn_cmp
47 * compares two struct metaconn based on the value of the conn pointer
48 * and of the local DN; used by avl stuff
55 metaconn_t *mc1 = ( metaconn_t * )c1;
56 metaconn_t *mc2 = ( metaconn_t * )c2;
59 /* If local DNs don't match, it is definitely not a match */
60 /* For shared sessions, conn is NULL. Only explicitly
61 * bound sessions will have non-NULL conn.
63 rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
65 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
72 * meta_back_conndnmc_cmp
74 * compares two struct metaconn based on the value of the conn pointer,
75 * the local DN and the struct pointer; used by avl stuff
78 meta_back_conndnmc_cmp(
82 metaconn_t *mc1 = ( metaconn_t * )c1;
83 metaconn_t *mc2 = ( metaconn_t * )c2;
86 /* If local DNs don't match, it is definitely not a match */
87 /* For shared sessions, conn is NULL. Only explicitly
88 * bound sessions will have non-NULL conn.
90 rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
92 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
94 rc = SLAP_PTRCMP( mc1, mc2 );
104 * compares two struct metaconn based on the value of the conn pointer;
112 metaconn_t *mc1 = ( metaconn_t * )c1;
113 metaconn_t *mc2 = ( metaconn_t * )c2;
115 /* For shared sessions, conn is NULL. Only explicitly
116 * bound sessions will have non-NULL conn.
118 return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
122 * meta_back_conndn_dup
124 * returns -1 in case a duplicate struct metaconn has been inserted;
128 meta_back_conndn_dup(
132 metaconn_t *mc1 = ( metaconn_t * )c1;
133 metaconn_t *mc2 = ( metaconn_t * )c2;
135 /* Cannot have more than one shared session with same DN */
136 if ( mc1->mc_conn == mc2->mc_conn &&
137 dn_match( &mc1->mc_local_ndn, &mc2->mc_local_ndn ) )
146 * Debug stuff (got it from libavl)
148 #if PRINT_CONNTREE > 0
150 ravl_print( Avlnode *root, int depth )
159 ravl_print( root->avl_right, depth + 1 );
161 for ( i = 0; i < depth; i++ ) {
162 fprintf( stderr, "-" );
165 mc = (metaconn_t *)root->avl_data;
166 fprintf( stderr, "mc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
168 mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "",
170 avl_bf2str( root->avl_bf ), mc->mc_refcnt );
172 ravl_print( root->avl_left, depth + 1 );
176 myprint( Avlnode *root, char *msg )
178 fprintf( stderr, "========> %s\n", msg );
181 fprintf( stderr, "\tNULL\n" );
184 ravl_print( root, 0 );
187 fprintf( stderr, "<======== %s\n", msg );
189 #endif /* PRINT_CONNTREE */
197 * Allocates a connection structure, making room for all the referenced targets
203 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
205 int i, ntargets = mi->mi_ntargets;
207 assert( ntargets > 0 );
209 /* malloc all in one */
210 mc = ( metaconn_t * )ch_malloc( sizeof( metaconn_t )
211 + sizeof( metasingleconn_t ) * ntargets );
216 for ( i = 0; i < ntargets; i++ ) {
217 mc->mc_conns[ i ].msc_ld = NULL;
218 BER_BVZERO( &mc->mc_conns[ i ].msc_bound_ndn );
219 BER_BVZERO( &mc->mc_conns[ i ].msc_cred );
220 mc->mc_conns[ i ].msc_mscflags = 0;
221 mc->mc_conns[ i ].msc_info = mi;
224 BER_BVZERO( &mc->mc_local_ndn );
225 mc->msc_mscflags = 0;
226 mc->mc_authz_target = META_BOUND_NONE;
237 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
239 assert( mc != NULL );
241 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
243 if ( --mc->mc_refcnt == 0 ) {
244 meta_back_conn_free( mc );
247 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
251 * meta_back_init_one_conn
253 * Initializes one connection
256 meta_back_init_one_conn(
262 ldap_back_send_t sendok )
264 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
265 metatarget_t *mt = mi->mi_targets[ candidate ];
266 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
269 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_isquarantined == LDAP_BACK_FQ_YES ) {
283 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
284 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
286 Debug( LDAP_DEBUG_ANY,
287 "%s: meta_back_init_one_conn quarantine "
288 "retry block #%d try #%d.\n",
289 op->o_log_prefix, ri->ri_idx, ri->ri_count );
290 mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
295 rs->sr_err = LDAP_UNAVAILABLE;
296 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
297 send_ldap_result( op, rs );
306 if ( msc->msc_ld != NULL ) {
307 return rs->sr_err = LDAP_SUCCESS;
310 msc->msc_mscflags = 0;
313 * Attempts to initialize the connection to the target ds
315 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
316 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
318 is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
319 #endif /* HAVE_TLS */
320 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
321 if ( rs->sr_err != LDAP_SUCCESS ) {
326 * Set LDAP version. This will always succeed: If the client
327 * bound with a particular version, then so can we.
329 if ( mt->mt_version != 0 ) {
330 version = mt->mt_version;
332 } else if ( op->o_conn->c_protocol != 0 ) {
333 version = op->o_conn->c_protocol;
336 version = LDAP_VERSION3;
338 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
339 ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
341 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
342 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
343 LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
346 /* start TLS ("tls [try-]{start|propagate}" statement) */
347 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
350 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
352 * use asynchronous StartTLS; in case, chase referral
353 * FIXME: OpenLDAP does not return referral on StartTLS yet
357 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
358 if ( rs->sr_err == LDAP_SUCCESS ) {
359 LDAPMessage *res = NULL;
360 int rc, nretries = mt->mt_nretries;
363 LDAP_BACK_TV_SET( &tv );
366 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
368 rs->sr_err = LDAP_OTHER;
370 } else if ( rc == 0 ) {
371 if ( nretries != 0 ) {
372 if ( nretries > 0 ) {
375 LDAP_BACK_TV_SET( &tv );
378 rs->sr_err = LDAP_OTHER;
380 } else if ( rc == LDAP_RES_EXTENDED ) {
381 struct berval *data = NULL;
383 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
385 if ( rs->sr_err == LDAP_SUCCESS ) {
388 rs->sr_err = ldap_parse_result( msc->msc_ld, res,
389 &err, NULL, NULL, NULL, NULL, 1 );
392 if ( rs->sr_err == LDAP_SUCCESS ) {
396 /* FIXME: in case a referral
397 * is returned, should we try
398 * using it instead of the
400 if ( rs->sr_err == LDAP_SUCCESS ) {
401 ldap_install_tls( msc->msc_ld );
403 } else if ( rs->sr_err == LDAP_REFERRAL ) {
404 rs->sr_err = LDAP_OTHER;
405 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
409 if ( data->bv_val ) {
410 ber_memfree( data->bv_val );
417 rs->sr_err = LDAP_OTHER;
424 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
426 * use synchronous StartTLS
428 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
429 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
431 /* if StartTLS is requested, only attempt it if the URL
432 * is not "ldaps://"; this may occur not only in case
433 * of misconfiguration, but also when used in the chain
434 * overlay, where the "uri" can be parsed out of a referral */
435 if ( rs->sr_err == LDAP_SERVER_DOWN
436 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
438 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
443 #endif /* HAVE_TLS */
446 * Set the network timeout if set
448 if ( mt->mt_network_timeout != 0 ) {
449 struct timeval network_timeout;
451 network_timeout.tv_usec = 0;
452 network_timeout.tv_sec = mt->mt_network_timeout;
454 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
455 (void *)&network_timeout );
459 * If the connection DN is not null, an attempt to rewrite it is made
463 if ( !BER_BVISNULL( &mt->mt_pseudorootdn ) ) {
464 ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
465 if ( !BER_BVISNULL( &mt->mt_pseudorootpw ) ) {
466 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
470 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
473 LDAP_BACK_CONN_ISPRIV_SET( msc );
476 BER_BVZERO( &msc->msc_cred );
477 BER_BVZERO( &msc->msc_bound_ndn );
478 if ( !BER_BVISEMPTY( &op->o_ndn )
479 && SLAP_IS_AUTHZ_BACKEND( op )
483 dc.conn = op->o_conn;
488 * Rewrite the bind dn if needed
490 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
491 &msc->msc_bound_ndn ) )
493 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
498 /* copy the DN idf needed */
499 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
500 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
504 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
508 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
511 if ( rs->sr_err == LDAP_SUCCESS ) {
513 * Sets a cookie for the rewrite session
515 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
518 rs->sr_err = slap_map_api2result( rs );
519 if ( sendok & LDAP_BACK_SENDERR ) {
520 send_ldap_result( op, rs );
531 * Retries one connection
539 ldap_back_send_t sendok )
541 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
542 metatarget_t *mt = mi->mi_targets[ candidate ];
543 metaconn_t *mc = *mcp;
544 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
545 int rc = LDAP_UNAVAILABLE,
546 binding = LDAP_BACK_CONN_BINDING( msc );
548 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
550 assert( mc->mc_refcnt > 0 );
551 if ( mc->mc_refcnt == 1 ) {
552 if ( LogTest( LDAP_DEBUG_ANY ) ) {
553 char buf[ SLAP_TEXT_BUFLEN ];
555 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
556 snprintf( buf, sizeof( buf ),
557 "retrying URI=\"%s\" DN=\"%s\"",
559 BER_BVISNULL( &msc->msc_bound_ndn ) ?
560 "" : msc->msc_bound_ndn.bv_val );
561 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
563 Debug( LDAP_DEBUG_ANY,
564 "%s meta_back_retry[%d]: %s.\n",
565 op->o_log_prefix, candidate, buf );
568 meta_clear_one_candidate( msc );
569 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
571 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
573 /* mc here must be the regular mc, reset and ready for init */
574 rc = meta_back_init_one_conn( op, rs, mc, candidate,
575 LDAP_BACK_CONN_ISPRIV( mc ), sendok );
577 LDAP_BACK_CONN_BINDING_SET( msc );
580 if ( rc == LDAP_SUCCESS ) {
581 rc = meta_back_single_dobind( op, rs, mcp, candidate,
582 sendok, mt->mt_nretries, 0 );
584 Debug( LDAP_DEBUG_ANY,
585 "%s meta_back_retry[%d]: "
586 "meta_back_single_dobind=%d\n",
587 op->o_log_prefix, candidate, rc );
588 if ( rc == LDAP_SUCCESS ) {
589 if ( be_isroot( op ) ) {
590 LDAP_BACK_CONN_ISBOUND_SET( msc );
592 LDAP_BACK_CONN_ISANON_SET( msc );
598 if ( rc != LDAP_SUCCESS ) {
599 SlapReply *candidates = meta_back_candidates_get( op );
601 candidates[ candidate ].sr_err = rc;
603 if ( *mcp != NULL ) {
605 LDAP_BACK_CONN_BINDING_CLEAR( msc );
607 LDAP_BACK_CONN_TAINTED_SET( mc );
608 /* only release if mandatory; otherwise
609 * let the caller do what's best before
611 if ( META_BACK_ONERR_STOP( mi ) ) {
612 meta_back_release_conn_lock( op, mc, 0 );
620 send_ldap_result( op, rs );
624 if ( META_BACK_QUARANTINE( mi ) ) {
625 meta_back_quarantine( op, rs, candidate, 0 );
628 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
630 return rc == LDAP_SUCCESS ? 1 : 0;
634 * callback for unique candidate selection
637 meta_back_conn_cb( Operation *op, SlapReply *rs )
639 assert( op->o_tag == LDAP_REQ_SEARCH );
641 switch ( rs->sr_type ) {
643 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
659 meta_back_get_candidate(
664 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
668 * tries to get a unique candidate
669 * (takes care of default target)
671 candidate = meta_back_select_unique_candidate( mi, ndn );
674 * if any is found, inits the connection
676 if ( candidate == META_TARGET_NONE ) {
677 rs->sr_err = LDAP_NO_SUCH_OBJECT;
678 rs->sr_text = "no suitable candidate target found";
680 } else if ( candidate == META_TARGET_MULTIPLE ) {
683 SlapReply rs2 = { 0 };
684 slap_callback cb2 = { 0 };
687 /* try to get a unique match for the request ndn
688 * among the multiple candidates available */
689 op2.o_tag = LDAP_REQ_SEARCH;
691 op2.o_req_ndn = *ndn;
692 op2.ors_scope = LDAP_SCOPE_BASE;
693 op2.ors_deref = LDAP_DEREF_NEVER;
694 op2.ors_attrs = slap_anlist_no_attrs;
695 op2.ors_attrsonly = 0;
696 op2.ors_limit = NULL;
698 op2.ors_tlimit = SLAP_NO_LIMIT;
700 f.f_choice = LDAP_FILTER_PRESENT;
701 f.f_desc = slap_schema.si_ad_objectClass;
703 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
705 op2.o_callback = &cb2;
706 cb2.sc_response = meta_back_conn_cb;
707 cb2.sc_private = (void *)&candidate;
709 rc = op->o_bd->be_search( &op2, &rs2 );
711 switch ( rs2.sr_err ) {
714 rs->sr_err = rs2.sr_err;
717 case LDAP_SIZELIMIT_EXCEEDED:
718 /* if multiple candidates can serve the operation,
719 * and a default target is defined, and it is
720 * a candidate, try using it (FIXME: YMMV) */
721 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
722 && meta_back_is_candidate( &mi->mi_targets[ mi->mi_defaulttarget ]->mt_nsuffix,
723 mi->mi_targets[ mi->mi_defaulttarget ]->mt_scope,
724 mi->mi_targets[ mi->mi_defaulttarget ]->mt_subtree_exclude,
725 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
727 candidate = mi->mi_defaulttarget;
728 rs->sr_err = LDAP_SUCCESS;
732 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
733 rs->sr_text = "cannot select unique candidate target";
739 rs->sr_err = LDAP_SUCCESS;
745 static void *meta_back_candidates_dummy;
748 meta_back_candidates_keyfree(
752 metacandidates_t *mc = (metacandidates_t *)data;
754 ber_memfree_x( mc->mc_candidates, NULL );
755 ber_memfree_x( data, NULL );
759 meta_back_candidates_get( Operation *op )
761 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
762 metacandidates_t *mc;
764 if ( op->o_threadctx ) {
767 ldap_pvt_thread_pool_getkey( op->o_threadctx,
768 &meta_back_candidates_dummy, &data, NULL );
769 mc = (metacandidates_t *)data;
772 mc = mi->mi_candidates;
776 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
777 mc->mc_ntargets = mi->mi_ntargets;
778 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
779 if ( op->o_threadctx ) {
783 ldap_pvt_thread_pool_setkey( op->o_threadctx,
784 &meta_back_candidates_dummy, data,
785 meta_back_candidates_keyfree );
788 mi->mi_candidates = mc;
791 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
792 /* NOTE: in the future, may want to allow back-config
793 * to add/remove targets from back-meta... */
794 mc->mc_candidates = ch_realloc( mc->mc_candidates,
795 sizeof( SlapReply ) * mi->mi_ntargets );
796 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
797 sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
798 mc->mc_ntargets = mi->mi_ntargets;
801 return mc->mc_candidates;
807 * Prepares the connection structure
811 * - determine what DN is being requested:
813 * op requires candidate checks
815 * add unique parent of o_req_ndn
816 * bind unique^*[/all] o_req_ndn [no check]
817 * compare unique^+ o_req_ndn
818 * delete unique o_req_ndn
819 * modify unique o_req_ndn
820 * search any o_req_ndn
821 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
823 * - for ops that require the candidate to be unique, in case of multiple
824 * occurrences an internal search with sizeLimit=1 is performed
825 * if a unique candidate can actually be determined. If none is found,
826 * the operation aborts; if multiple are found, the default target
827 * is used if defined and candidate; otherwise the operation aborts.
829 * *^note: actually, the bind operation is handled much like a search;
830 * i.e. the bind is broadcast to all candidate targets.
832 * +^note: actually, the compare operation is handled much like a search;
833 * i.e. the compare is broadcast to all candidate targets, while checking
834 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
842 ldap_back_send_t sendok )
844 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
845 metaconn_t *mc = NULL,
847 int cached = META_TARGET_NONE,
848 i = META_TARGET_NONE,
854 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
858 META_DNTYPE_NEWPARENT
859 } dn_type = META_DNTYPE_ENTRY;
860 struct berval ndn = op->o_req_ndn,
863 SlapReply *candidates = meta_back_candidates_get( op );
865 /* Internal searches are privileged and shared. So is root. */
866 /* FIXME: there seem to be concurrency issues */
867 if ( op->o_do_not_cache || be_isroot( op ) ) {
868 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
869 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
870 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
873 mc_curr.mc_local_ndn = op->o_ndn;
875 /* Explicit binds must not be shared */
876 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
877 mc_curr.mc_conn = op->o_conn;
880 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
884 /* Explicit Bind requests always get their own conn */
885 if ( !( sendok & LDAP_BACK_BINDING ) ) {
886 /* Searches for a metaconn in the avl tree */
888 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
889 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
890 (caddr_t)&mc_curr, meta_back_conndn_cmp );
892 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
893 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
895 /* don't let anyone else use this expired connection */
896 (void)avl_delete( &mi->mi_conninfo.lai_tree,
897 (caddr_t)mc, meta_back_conndnmc_cmp );
898 LDAP_BACK_CONN_TAINTED_SET( mc );
900 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired.\n",
901 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
904 /* Don't reuse connections while they're still binding */
905 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
906 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
907 ldap_pvt_thread_yield();
913 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
916 switch ( op->o_tag ) {
918 /* if we go to selection, the entry must not exist,
919 * and we must be able to resolve the parent */
920 dn_type = META_DNTYPE_PARENT;
921 dnParent( &ndn, &pndn );
924 case LDAP_REQ_MODRDN:
925 /* if nnewSuperior is not NULL, it must resolve
926 * to the same candidate as the req_ndn */
927 if ( op->orr_nnewSup ) {
928 dn_type = META_DNTYPE_NEWPARENT;
933 /* if bound as rootdn, the backend must bind to all targets
934 * with the administrative identity */
935 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
936 op_type = META_OP_REQUIRE_ALL;
940 case LDAP_REQ_DELETE:
941 case LDAP_REQ_MODIFY:
942 /* just a unique candidate */
945 case LDAP_REQ_COMPARE:
946 case LDAP_REQ_SEARCH:
947 /* allow multiple candidates for the searchBase */
948 op_type = META_OP_ALLOW_MULTIPLE;
952 /* right now, just break (exop?) */
957 * require all connections ...
959 if ( op_type == META_OP_REQUIRE_ALL ) {
961 /* Looks like we didn't get a bind. Open a new session... */
963 mc = metaconn_alloc( op );
964 mc->mc_conn = mc_curr.mc_conn;
965 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
967 if ( sendok & LDAP_BACK_BINDING ) {
968 LDAP_BACK_CONN_BINDING_SET( mc );
972 for ( i = 0; i < mi->mi_ntargets; i++ ) {
974 * The target is activated; if needed, it is
977 candidates[ i ].sr_err = meta_back_init_one_conn( op,
978 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
980 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
981 candidates[ i ].sr_tag = META_CANDIDATE;
987 * FIXME: in case one target cannot
988 * be init'd, should the other ones
991 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
992 err = candidates[ i ].sr_err;
997 if ( ncandidates == 0 ) {
999 meta_back_freeconn( op, mc );
1002 meta_back_release_conn( op, mc );
1005 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1006 rs->sr_text = "Unable to select valid candidates";
1008 if ( sendok & LDAP_BACK_SENDERR ) {
1009 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1010 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1012 send_ldap_result( op, rs );
1014 rs->sr_matched = NULL;
1024 * looks in cache, if any
1026 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1027 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1030 if ( op_type == META_OP_REQUIRE_SINGLE ) {
1031 metatarget_t *mt = NULL;
1032 metasingleconn_t *msc = NULL;
1036 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1037 candidates[ j ].sr_tag = META_NOT_CANDIDATE;
1041 * tries to get a unique candidate
1042 * (takes care of default target)
1044 if ( i == META_TARGET_NONE ) {
1045 i = meta_back_get_candidate( op, rs, &ndn );
1047 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1048 i = meta_back_get_candidate( op, rs, &pndn );
1051 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1053 meta_back_release_conn( op, mc );
1056 if ( sendok & LDAP_BACK_SENDERR ) {
1057 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1058 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1060 send_ldap_result( op, rs );
1062 rs->sr_matched = NULL;
1069 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1072 meta_back_release_conn( op, mc );
1075 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1076 rs->sr_text = "cross-target rename not supported";
1077 if ( sendok & LDAP_BACK_SENDERR ) {
1078 send_ldap_result( op, rs );
1085 Debug( LDAP_DEBUG_TRACE,
1086 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1087 i, op->o_req_ndn.bv_val, 0 );
1090 /* Retries searching for a metaconn in the avl tree
1091 * the reason is that the connection might have been
1092 * created by meta_back_get_candidate() */
1093 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1095 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1096 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1097 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1099 /* Don't reuse connections while they're still binding */
1100 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1101 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1102 ldap_pvt_thread_yield();
1108 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1111 /* Looks like we didn't get a bind. Open a new session... */
1113 mc = metaconn_alloc( op );
1114 mc->mc_conn = mc_curr.mc_conn;
1115 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1117 if ( sendok & LDAP_BACK_BINDING ) {
1118 LDAP_BACK_CONN_BINDING_SET( mc );
1124 * Clear all other candidates
1126 ( void )meta_clear_unused_candidates( op, i );
1128 mt = mi->mi_targets[ i ];
1129 msc = &mc->mc_conns[ i ];
1132 * The target is activated; if needed, it is
1133 * also init'd. In case of error, meta_back_init_one_conn
1134 * sends the appropriate result.
1136 err = meta_back_init_one_conn( op, rs, mc, i,
1137 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
1138 if ( err != LDAP_SUCCESS ) {
1140 * FIXME: in case one target cannot
1141 * be init'd, should the other ones
1144 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1146 (void)meta_clear_one_candidate( msc );
1147 meta_back_freeconn( op, mc );
1150 meta_back_release_conn( op, mc );
1155 candidates[ i ].sr_err = LDAP_SUCCESS;
1156 candidates[ i ].sr_tag = META_CANDIDATE;
1164 * if no unique candidate ...
1168 /* Looks like we didn't get a bind. Open a new session... */
1170 mc = metaconn_alloc( op );
1171 mc->mc_conn = mc_curr.mc_conn;
1172 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1174 if ( sendok & LDAP_BACK_BINDING ) {
1175 LDAP_BACK_CONN_BINDING_SET( mc );
1179 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1180 metatarget_t *mt = mi->mi_targets[ i ];
1181 metasingleconn_t *msc = &mc->mc_conns[ i ];
1184 || meta_back_is_candidate( &mt->mt_nsuffix,
1186 mt->mt_subtree_exclude,
1188 LDAP_SCOPE_SUBTREE ) )
1192 * The target is activated; if needed, it is
1195 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1196 LDAP_BACK_CONN_ISPRIV( &mc_curr ), LDAP_BACK_DONTSEND );
1197 if ( lerr == LDAP_SUCCESS ) {
1198 candidates[ i ].sr_tag = META_CANDIDATE;
1199 candidates[ i ].sr_err = LDAP_SUCCESS;
1202 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1203 op->o_log_prefix, i, 0 );
1208 * FIXME: in case one target cannot
1209 * be init'd, should the other ones
1213 ( void )meta_clear_one_candidate( msc );
1215 /* leave the target candidate, but record the error for later use */
1216 candidates[ i ].sr_err = lerr;
1219 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed: %d\n",
1220 op->o_log_prefix, i, lerr );
1222 if ( META_BACK_ONERR_STOP( mi ) ) {
1223 if ( sendok & LDAP_BACK_SENDERR ) {
1224 send_ldap_result( op, rs );
1228 meta_back_freeconn( op, mc );
1231 meta_back_release_conn( op, mc );
1243 ( void )meta_clear_one_candidate( msc );
1245 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1249 if ( ncandidates == 0 ) {
1251 meta_back_freeconn( op, mc );
1254 meta_back_release_conn( op, mc );
1257 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1258 rs->sr_text = "Unable to select valid candidates";
1260 if ( sendok & LDAP_BACK_SENDERR ) {
1261 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1262 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1264 send_ldap_result( op, rs );
1266 rs->sr_matched = NULL;
1274 /* clear out meta_back_init_one_conn non-fatal errors */
1275 rs->sr_err = LDAP_SUCCESS;
1278 /* touch the timestamp */
1279 if ( mi->mi_idle_timeout != 0 ) {
1280 mc->mc_time = op->o_time;
1284 if ( mi->mi_conn_ttl ) {
1285 mc->mc_create_time = op->o_time;
1289 * Inserts the newly created metaconn in the avl tree
1291 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1292 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1293 meta_back_conndn_cmp, meta_back_conndn_dup );
1295 #if PRINT_CONNTREE > 0
1296 myprint( mi->mi_conninfo.lai_tree, "meta_back_getconn" );
1297 #endif /* PRINT_CONNTREE */
1299 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1302 * Err could be -1 in case a duplicate metaconn is inserted
1309 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1310 /* duplicate: free and try to get the newly created one */
1313 LDAP_BACK_CONN_TAINTED_SET( mc );
1317 Debug( LDAP_DEBUG_ANY,
1318 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1319 op->o_log_prefix, ncandidates,
1320 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1322 meta_back_freeconn( op, mc );
1324 rs->sr_err = LDAP_OTHER;
1325 rs->sr_text = "proxy bind collision";
1326 if ( sendok & LDAP_BACK_SENDERR ) {
1327 send_ldap_result( op, rs );
1333 Debug( LDAP_DEBUG_TRACE,
1334 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1335 op->o_log_prefix, ncandidates,
1336 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1339 Debug( LDAP_DEBUG_TRACE,
1340 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1341 op->o_log_prefix, ncandidates,
1342 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1349 meta_back_release_conn_lock(
1354 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1356 assert( mc != NULL );
1359 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1361 assert( mc->mc_refcnt > 0 );
1363 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1364 if ( LDAP_BACK_CONN_TAINTED( mc ) ) {
1365 Debug( LDAP_DEBUG_TRACE, "%s meta_back_release_conn: mc=%p conn=%ld expired.\n",
1366 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1367 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1368 ( caddr_t )mc, meta_back_conndnmc_cmp );
1369 if ( mc->mc_refcnt == 0 ) {
1370 meta_clear_candidates( op, mc );
1371 meta_back_conn_free( mc );
1375 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1380 meta_back_quarantine(
1386 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1387 metatarget_t *mt = mi->mi_targets[ candidate ];
1389 slap_retry_info_t *ri = &mt->mt_quarantine;
1392 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1395 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1396 switch ( mt->mt_isquarantined ) {
1397 case LDAP_BACK_FQ_NO:
1398 Debug( LDAP_DEBUG_ANY,
1399 "%s: meta_back_quarantine enter.\n",
1400 op->o_log_prefix, 0, 0 );
1406 case LDAP_BACK_FQ_RETRYING:
1407 Debug( LDAP_DEBUG_ANY,
1408 "%s: meta_back_quarantine block #%d try #%d failed.\n",
1409 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1412 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1413 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1424 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1425 ri->ri_last = slap_get_time();
1427 } else if ( mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1428 Debug( LDAP_DEBUG_ANY,
1429 "%s: meta_back_quarantine exit.\n",
1430 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1434 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1438 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );