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 #define PRINT_CONNTREE 0
45 * compares two struct metaconn based on the value of the conn pointer;
53 metaconn_t *mc1 = ( metaconn_t * )c1;
54 metaconn_t *mc2 = ( metaconn_t * )c2;
57 /* If local DNs don't match, it is definitely not a match */
58 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
63 /* For shared sessions, conn is NULL. Only explicitly
64 * bound sessions will have non-NULL conn.
66 return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
72 * returns -1 in case a duplicate struct metaconn has been inserted;
80 metaconn_t *mc1 = ( metaconn_t * )c1;
81 metaconn_t *mc2 = ( metaconn_t * )c2;
83 /* Cannot have more than one shared session with same DN */
84 if ( dn_match( &mc1->mc_local_ndn, &mc2->mc_local_ndn ) &&
85 mc1->mc_conn == mc2->mc_conn )
94 * Debug stuff (got it from libavl)
96 #if PRINT_CONNTREE > 0
98 ravl_print( Avlnode *root, int depth )
101 metaconn_t *mc = (metaconn_t *)root->avl_data;
107 ravl_print( root->avl_right, depth + 1 );
109 for ( i = 0; i < depth; i++ ) {
113 printf( "c(%d%s%s) %d\n",
114 LDAP_BACK_PCONN_ID( mc->mc_conn ),
115 BER_BVISNULL( &mc->mc_local_ndn ) ? "" : ": ",
116 BER_BVISNULL( &mc->mc_local_ndn ) ? "" : mc->mc_local_ndn.bv_val,
119 ravl_print( root->avl_left, depth + 1 );
123 myprint( Avlnode *root )
125 printf( "********\n" );
128 printf( "\tNULL\n" );
130 ravl_print( root, 0 );
133 printf( "********\n" );
135 #endif /* PRINT_CONNTREE */
143 * Allocates a connection structure, making room for all the referenced targets
149 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
151 int i, ntargets = mi->mi_ntargets;
153 assert( ntargets > 0 );
155 /* malloc all in one */
156 mc = ( metaconn_t * )ch_malloc( sizeof( metaconn_t )
157 + sizeof( metasingleconn_t ) * ntargets );
162 for ( i = 0; i < ntargets; i++ ) {
163 mc->mc_conns[ i ].msc_ld = NULL;
164 BER_BVZERO( &mc->mc_conns[ i ].msc_bound_ndn );
165 BER_BVZERO( &mc->mc_conns[ i ].msc_cred );
166 LDAP_BACK_CONN_ISBOUND_CLEAR( &mc->mc_conns[ i ] );
167 mc->mc_conns[ i ].msc_info = mi;
170 BER_BVZERO( &mc->mc_local_ndn );
171 mc->msc_mscflags = 0;
172 mc->mc_authz_target = META_BOUND_NONE;
173 ldap_pvt_thread_mutex_init( &mc->mc_mutex );
185 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
187 assert( mc != NULL );
189 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
191 if ( --mc->mc_refcnt == 0 ) {
192 meta_back_conn_free( mc );
195 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
199 * meta_back_init_one_conn
201 * Initializes one connection
204 meta_back_init_one_conn(
211 ldap_back_send_t sendok )
213 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
214 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
217 int isauthz = ( candidate == mc->mc_authz_target );
222 if ( msc->msc_ld != NULL ) {
223 if ( mt->mt_idle_timeout == 0 ) {
224 return rs->sr_err = LDAP_SUCCESS;
227 if ( op->o_time > msc->msc_time + mt->mt_idle_timeout ) {
228 Debug( LDAP_DEBUG_TRACE,
229 "%s meta_back_init_one_conn[%d]: idle timeout.\n",
230 op->o_log_prefix, candidate, 0 );
231 if ( meta_back_retry( op, rs, mc, candidate, sendok ) ) {
236 msc->msc_time = op->o_time;
240 * Attempts to initialize the connection to the target ds
242 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
243 if ( rs->sr_err != LDAP_SUCCESS ) {
248 * Set LDAP version. This will always succeed: If the client
249 * bound with a particular version, then so can we.
251 vers = op->o_conn->c_protocol;
252 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
254 /* automatically chase referrals ("chase-referrals"/"dont-chase-referrals" statement) */
255 if ( LDAP_BACK_CHASE_REFERRALS( mi ) ) {
256 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
260 /* start TLS ("tls [try-]{start|propagate}" statement) */
261 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
262 && !ldap_is_ldaps_url( mt->mt_uri ) )
264 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
266 * use asynchronous StartTLS; in case, chase referral
267 * FIXME: OpenLDAP does not return referral on StartTLS yet
271 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
272 if ( rs->sr_err == LDAP_SUCCESS ) {
273 LDAPMessage *res = NULL;
274 int rc, nretries = mt->mt_nretries;
277 LDAP_BACK_TV_SET( &tv );
280 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
282 rs->sr_err = LDAP_OTHER;
284 } else if ( rc == 0 ) {
285 if ( nretries != 0 ) {
286 if ( nretries > 0 ) {
289 LDAP_BACK_TV_SET( &tv );
292 rs->sr_err = LDAP_OTHER;
294 } else if ( rc == LDAP_RES_EXTENDED ) {
295 struct berval *data = NULL;
297 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
299 if ( rs->sr_err == LDAP_SUCCESS ) {
302 rs->sr_err = ldap_parse_result( msc->msc_ld, res,
303 &err, NULL, NULL, NULL, NULL, 1 );
306 if ( rs->sr_err == LDAP_SUCCESS ) {
310 /* FIXME: in case a referral
311 * is returned, should we try
312 * using it instead of the
314 if ( rs->sr_err == LDAP_SUCCESS ) {
315 ldap_install_tls( msc->msc_ld );
317 } else if ( rs->sr_err == LDAP_REFERRAL ) {
318 rs->sr_err = LDAP_OTHER;
319 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
323 if ( data->bv_val ) {
324 ber_memfree( data->bv_val );
331 rs->sr_err = LDAP_OTHER;
338 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
340 * use synchronous StartTLS
342 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
343 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
345 /* if StartTLS is requested, only attempt it if the URL
346 * is not "ldaps://"; this may occur not only in case
347 * of misconfiguration, but also when used in the chain
348 * overlay, where the "uri" can be parsed out of a referral */
349 if ( rs->sr_err == LDAP_SERVER_DOWN
350 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
352 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
356 #endif /* HAVE_TLS */
359 * Set the network timeout if set
361 if ( mt->mt_network_timeout != 0 ) {
362 struct timeval network_timeout;
364 network_timeout.tv_usec = 0;
365 network_timeout.tv_sec = mt->mt_network_timeout;
367 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
368 (void *)&network_timeout );
372 * If the connection DN is not null, an attempt to rewrite it is made
376 if ( !BER_BVISNULL( &mt->mt_pseudorootdn ) ) {
377 ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
378 if ( !BER_BVISNULL( &mt->mt_pseudorootpw ) ) {
379 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
383 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
386 LDAP_BACK_CONN_ISPRIV_SET( msc );
389 BER_BVZERO( &msc->msc_cred );
390 BER_BVZERO( &msc->msc_bound_ndn );
391 if ( !BER_BVISEMPTY( &op->o_ndn )
392 && SLAP_IS_AUTHZ_BACKEND( op )
396 dc.conn = op->o_conn;
401 * Rewrite the bind dn if needed
403 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
404 &msc->msc_bound_ndn ) )
406 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
410 /* copy the DN idf needed */
411 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
412 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
416 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
420 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
422 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
425 if ( rs->sr_err == LDAP_SUCCESS ) {
427 * Sets a cookie for the rewrite session
429 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
431 if ( mt->mt_idle_timeout ) {
432 msc->msc_time = op->o_time;
436 rs->sr_err = slap_map_api2result( rs );
437 if ( sendok & LDAP_BACK_SENDERR ) {
438 send_ldap_result( op, rs );
449 * Retries one connection
457 ldap_back_send_t sendok )
459 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
460 metatarget_t *mt = &mi->mi_targets[ candidate ];
461 int rc = LDAP_UNAVAILABLE;
462 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
465 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
467 assert( mc->mc_refcnt > 0 );
469 if ( mc->mc_refcnt == 1 ) {
470 char buf[ SLAP_TEXT_BUFLEN ];
472 while ( ldap_pvt_thread_mutex_trylock( &mc->mc_mutex ) ) {
473 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
474 ldap_pvt_thread_yield();
478 snprintf( buf, sizeof( buf ),
479 "retrying URI=\"%s\" DN=\"%s\"",
481 BER_BVISNULL( &msc->msc_bound_ndn ) ?
482 "" : msc->msc_bound_ndn.bv_val );
483 Debug( LDAP_DEBUG_ANY,
484 "%s meta_back_retry[%d]: %s.\n",
485 op->o_log_prefix, candidate, buf );
487 meta_clear_one_candidate( msc );
488 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
490 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
492 /* mc here must be the regular mc, reset and ready for init */
493 rc = meta_back_init_one_conn( op, rs, mt, mc, candidate,
494 LDAP_BACK_CONN_ISPRIV( mc ), sendok );
496 if ( rc == LDAP_SUCCESS ) {
497 rc = meta_back_single_dobind( op, rs, mc, candidate,
498 sendok, mt->mt_nretries, 0 );
501 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
504 if ( rc != LDAP_SUCCESS ) {
508 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
510 return rc == LDAP_SUCCESS ? 1 : 0;
514 * callback for unique candidate selection
517 meta_back_conn_cb( Operation *op, SlapReply *rs )
519 assert( op->o_tag == LDAP_REQ_SEARCH );
521 switch ( rs->sr_type ) {
523 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
539 meta_back_get_candidate(
544 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
548 * tries to get a unique candidate
549 * (takes care of default target)
551 candidate = meta_back_select_unique_candidate( mi, ndn );
554 * if any is found, inits the connection
556 if ( candidate == META_TARGET_NONE ) {
557 rs->sr_err = LDAP_NO_SUCH_OBJECT;
558 rs->sr_text = "no suitable candidate target found";
560 } else if ( candidate == META_TARGET_MULTIPLE ) {
563 SlapReply rs2 = { 0 };
564 slap_callback cb2 = { 0 };
567 /* try to get a unique match for the request ndn
568 * among the multiple candidates available */
569 op2.o_tag = LDAP_REQ_SEARCH;
571 op2.o_req_ndn = *ndn;
572 op2.ors_scope = LDAP_SCOPE_BASE;
573 op2.ors_deref = LDAP_DEREF_NEVER;
574 op2.ors_attrs = slap_anlist_no_attrs;
575 op2.ors_attrsonly = 0;
576 op2.ors_limit = NULL;
578 op2.ors_tlimit = SLAP_NO_LIMIT;
580 f.f_choice = LDAP_FILTER_PRESENT;
581 f.f_desc = slap_schema.si_ad_objectClass;
583 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
585 op2.o_callback = &cb2;
586 cb2.sc_response = meta_back_conn_cb;
587 cb2.sc_private = (void *)&candidate;
589 rc = op->o_bd->be_search( &op2, &rs2 );
591 switch ( rs2.sr_err ) {
594 rs->sr_err = rs2.sr_err;
597 case LDAP_SIZELIMIT_EXCEEDED:
598 /* if multiple candidates can serve the operation,
599 * and a default target is defined, and it is
600 * a candidate, try using it (FIXME: YMMV) */
601 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
602 && meta_back_is_candidate( &mi->mi_targets[ mi->mi_defaulttarget ].mt_nsuffix,
603 mi->mi_targets[ mi->mi_defaulttarget ].mt_scope,
604 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
606 candidate = mi->mi_defaulttarget;
607 rs->sr_err = LDAP_SUCCESS;
611 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
612 rs->sr_text = "cannot select unique candidate target";
618 rs->sr_err = LDAP_SUCCESS;
624 static void *meta_back_candidates_dummy;
627 meta_back_candidates_keyfree(
631 metacandidates_t *mc = (metacandidates_t *)data;
633 ber_memfree_x( mc->mc_candidates, NULL );
634 ber_memfree_x( data, NULL );
638 meta_back_candidates_get( Operation *op )
640 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
641 metacandidates_t *mc;
643 if ( op->o_threadctx ) {
646 ldap_pvt_thread_pool_getkey( op->o_threadctx,
647 &meta_back_candidates_dummy, &data, NULL );
648 mc = (metacandidates_t *)data;
651 mc = mi->mi_candidates;
655 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
656 mc->mc_ntargets = mi->mi_ntargets;
657 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
658 if ( op->o_threadctx ) {
662 ldap_pvt_thread_pool_setkey( op->o_threadctx,
663 &meta_back_candidates_dummy, data,
664 meta_back_candidates_keyfree );
667 mi->mi_candidates = mc;
670 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
671 /* NOTE: in the future, may want to allow back-config
672 * to add/remove targets from back-meta... */
673 mc->mc_ntargets = mi->mi_ntargets;
674 mc->mc_candidates = ch_realloc( mc->mc_candidates,
675 sizeof( SlapReply ) * mc->mc_ntargets );
678 return mc->mc_candidates;
684 * Prepares the connection structure
688 * - determine what DN is being requested:
690 * op requires candidate checks
692 * add unique parent of o_req_ndn
693 * bind unique^*[/all] o_req_ndn [no check]
694 * compare unique^+ o_req_ndn
695 * delete unique o_req_ndn
696 * modify unique o_req_ndn
697 * search any o_req_ndn
698 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
700 * - for ops that require the candidate to be unique, in case of multiple
701 * occurrences an internal search with sizeLimit=1 is performed
702 * if a unique candidate can actually be determined. If none is found,
703 * the operation aborts; if multiple are found, the default target
704 * is used if defined and candidate; otherwise the operation aborts.
706 * *^note: actually, the bind operation is handled much like a search;
707 * i.e. the bind is broadcast to all candidate targets.
709 * +^note: actually, the compare operation is handled much like a search;
710 * i.e. the compare is broadcast to all candidate targets, while checking
711 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
719 ldap_back_send_t sendok )
721 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
722 metaconn_t *mc = NULL,
724 int cached = META_TARGET_NONE,
725 i = META_TARGET_NONE,
731 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
734 struct berval ndn = op->o_req_ndn,
737 SlapReply *candidates = meta_back_candidates_get( op );
739 /* Internal searches are privileged and shared. So is root. */
740 /* FIXME: there seem to be concurrency issues */
741 if ( op->o_do_not_cache || be_isroot( op ) ) {
742 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
743 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
744 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
747 mc_curr.mc_local_ndn = op->o_ndn;
749 /* Explicit binds must not be shared */
750 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
751 mc_curr.mc_conn = op->o_conn;
754 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
758 /* Explicit Bind requests always get their own conn */
759 if ( !( sendok & LDAP_BACK_BINDING ) ) {
760 /* Searches for a metaconn in the avl tree */
762 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
763 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
764 (caddr_t)&mc_curr, meta_back_conn_cmp );
766 if ( mc->mc_tainted ) {
767 rs->sr_err = LDAP_UNAVAILABLE;
768 rs->sr_text = "remote server unavailable";
769 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
773 /* Don't reuse connections while they're still binding */
774 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
775 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
776 ldap_pvt_thread_yield();
781 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
784 switch ( op->o_tag ) {
786 /* if we go to selection, the entry must not exist,
787 * and we must be able to resolve the parent */
789 dnParent( &ndn, &pndn );
792 case LDAP_REQ_MODRDN:
793 /* if nnewSuperior is not NULL, it must resolve
794 * to the same candidate as the req_ndn */
795 if ( op->orr_nnewSup ) {
801 /* if bound as rootdn, the backend must bind to all targets
802 * with the administrative identity */
803 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
804 op_type = META_OP_REQUIRE_ALL;
808 case LDAP_REQ_DELETE:
809 case LDAP_REQ_MODIFY:
810 /* just a unique candidate */
813 case LDAP_REQ_COMPARE:
814 case LDAP_REQ_SEARCH:
815 /* allow multiple candidates for the searchBase */
816 op_type = META_OP_ALLOW_MULTIPLE;
820 /* right now, just break (exop?) */
825 * require all connections ...
827 if ( op_type == META_OP_REQUIRE_ALL ) {
829 /* Looks like we didn't get a bind. Open a new session... */
831 mc = metaconn_alloc( op );
832 mc->mc_conn = mc_curr.mc_conn;
833 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
835 if ( sendok & LDAP_BACK_BINDING ) {
836 LDAP_BACK_CONN_BINDING_SET( mc );
840 for ( i = 0; i < mi->mi_ntargets; i++ ) {
841 metatarget_t *mt = &mi->mi_targets[ i ];
844 * The target is activated; if needed, it is
847 candidates[ i ].sr_err = meta_back_init_one_conn( op,
849 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
850 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
851 candidates[ i ].sr_tag = META_CANDIDATE;
857 * FIXME: in case one target cannot
858 * be init'd, should the other ones
861 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
862 err = candidates[ i ].sr_err;
867 if ( ncandidates == 0 ) {
869 meta_back_freeconn( op, mc );
872 meta_back_release_conn( op, mc );
875 rs->sr_err = LDAP_NO_SUCH_OBJECT;
876 rs->sr_text = "Unable to select valid candidates";
878 if ( sendok & LDAP_BACK_SENDERR ) {
879 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
880 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
882 send_ldap_result( op, rs );
884 rs->sr_matched = NULL;
894 * looks in cache, if any
896 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
897 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
900 if ( op_type == META_OP_REQUIRE_SINGLE ) {
901 metatarget_t *mt = NULL;
902 metasingleconn_t *msc = NULL;
906 for ( j = 0; j < mi->mi_ntargets; j++ ) {
907 candidates[ j ].sr_tag = META_NOT_CANDIDATE;
911 * tries to get a unique candidate
912 * (takes care of default target)
914 if ( i == META_TARGET_NONE ) {
915 i = meta_back_get_candidate( op, rs, &ndn );
917 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && parent ) {
918 i = meta_back_get_candidate( op, rs, &pndn );
921 if ( rs->sr_err != LDAP_SUCCESS ) {
923 meta_back_release_conn( op, mc );
926 if ( sendok & LDAP_BACK_SENDERR ) {
927 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
928 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
930 send_ldap_result( op, rs );
932 rs->sr_matched = NULL;
939 if ( newparent && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
942 meta_back_release_conn( op, mc );
945 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
946 rs->sr_text = "cross-target rename not supported";
947 if ( sendok & LDAP_BACK_SENDERR ) {
948 send_ldap_result( op, rs );
955 Debug( LDAP_DEBUG_TRACE,
956 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
957 i, op->o_req_ndn.bv_val, 0 );
960 /* Retries searching for a metaconn in the avl tree
961 * the reason is that the connection might have been
962 * created by meta_back_get_candidate() */
963 if ( !( sendok & LDAP_BACK_BINDING ) ) {
964 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
965 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
966 (caddr_t)&mc_curr, meta_back_conn_cmp );
970 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
973 /* Looks like we didn't get a bind. Open a new session... */
975 mc = metaconn_alloc( op );
976 mc->mc_conn = mc_curr.mc_conn;
977 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
979 if ( sendok & LDAP_BACK_BINDING ) {
980 LDAP_BACK_CONN_BINDING_SET( mc );
986 * Clear all other candidates
988 ( void )meta_clear_unused_candidates( op, i );
990 mt = &mi->mi_targets[ i ];
991 msc = &mc->mc_conns[ i ];
994 * The target is activated; if needed, it is
995 * also init'd. In case of error, meta_back_init_one_conn
996 * sends the appropriate result.
998 err = meta_back_init_one_conn( op, rs, mt, mc, i,
999 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
1000 if ( err != LDAP_SUCCESS ) {
1002 * FIXME: in case one target cannot
1003 * be init'd, should the other ones
1006 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1008 (void)meta_clear_one_candidate( msc );
1009 meta_back_freeconn( op, mc );
1012 meta_back_release_conn( op, mc );
1017 candidates[ i ].sr_err = LDAP_SUCCESS;
1018 candidates[ i ].sr_tag = META_CANDIDATE;
1026 * if no unique candidate ...
1030 /* Looks like we didn't get a bind. Open a new session... */
1032 mc = metaconn_alloc( op );
1033 mc->mc_conn = mc_curr.mc_conn;
1034 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1036 if ( sendok & LDAP_BACK_BINDING ) {
1037 LDAP_BACK_CONN_BINDING_SET( mc );
1041 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1042 metatarget_t *mt = &mi->mi_targets[ i ];
1043 metasingleconn_t *msc = &mc->mc_conns[ i ];
1046 || meta_back_is_candidate( &mt->mt_nsuffix,
1049 LDAP_SCOPE_SUBTREE ) )
1053 * The target is activated; if needed, it is
1056 int lerr = meta_back_init_one_conn( op, rs,
1058 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1060 if ( lerr == LDAP_SUCCESS ) {
1061 candidates[ i ].sr_tag = META_CANDIDATE;
1062 candidates[ i ].sr_err = LDAP_SUCCESS;
1065 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1066 op->o_log_prefix, i, 0 );
1071 * FIXME: in case one target cannot
1072 * be init'd, should the other ones
1076 ( void )meta_clear_one_candidate( msc );
1078 /* leave the target candidate, but record the error for later use */
1079 candidates[ i ].sr_err = lerr;
1082 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed: %d\n",
1083 op->o_log_prefix, i, lerr );
1090 ( void )meta_clear_one_candidate( msc );
1092 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1096 if ( ncandidates == 0 ) {
1098 meta_back_freeconn( op, mc );
1101 meta_back_release_conn( op, mc );
1104 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1105 rs->sr_text = "Unable to select valid candidates";
1107 if ( sendok & LDAP_BACK_SENDERR ) {
1108 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1109 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1111 send_ldap_result( op, rs );
1113 rs->sr_matched = NULL;
1121 /* clear out meta_back_init_one_conn non-fatal errors */
1122 rs->sr_err = LDAP_SUCCESS;
1128 * Inserts the newly created metaconn in the avl tree
1130 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1131 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1132 meta_back_conn_cmp, meta_back_conn_dup );
1134 #if PRINT_CONNTREE > 0
1135 myprint( mi->mi_conninfo.lai_tree );
1136 #endif /* PRINT_CONNTREE */
1138 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1141 * Err could be -1 in case a duplicate metaconn is inserted
1143 * FIXME: what if the same client issues more than one
1144 * asynchronous operations?
1147 Debug( LDAP_DEBUG_ANY,
1148 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1149 op->o_log_prefix, ncandidates,
1150 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1152 rs->sr_err = LDAP_OTHER;
1153 rs->sr_text = "Internal server error";
1154 meta_back_freeconn( op, mc );
1155 if ( sendok & LDAP_BACK_SENDERR ) {
1156 send_ldap_result( op, rs );
1162 Debug( LDAP_DEBUG_TRACE,
1163 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1164 op->o_log_prefix, ncandidates,
1165 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1168 Debug( LDAP_DEBUG_TRACE,
1169 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1170 op->o_log_prefix, ncandidates,
1171 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1178 meta_back_release_conn(
1182 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1184 assert( mc != NULL );
1186 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1187 assert( mc->mc_refcnt > 0 );
1189 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1190 if ( mc->mc_refcnt == 0 && mc->mc_tainted ) {
1191 (void)avl_delete( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1192 meta_back_conn_cmp );
1193 meta_back_conn_free( mc );
1195 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );