2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 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 );
180 * meta_back_conn_free
189 assert( mc != NULL );
190 assert( mc->mc_refcnt == 0 );
192 if ( !BER_BVISNULL( &mc->mc_local_ndn ) ) {
193 free( mc->mc_local_ndn.bv_val );
196 ldap_pvt_thread_mutex_destroy( &mc->mc_mutex );
205 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
207 assert( mc != NULL );
209 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
211 if ( --mc->mc_refcnt == 0 ) {
212 meta_back_conn_free( mc );
215 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
219 * meta_back_init_one_conn
221 * Initializes one connection
224 meta_back_init_one_conn(
228 metasingleconn_t *msc,
229 ldap_back_send_t sendok )
231 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
238 if ( msc->msc_ld != NULL ) {
239 return rs->sr_err = LDAP_SUCCESS;
243 * Attempts to initialize the connection to the target ds
245 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
246 if ( rs->sr_err != LDAP_SUCCESS ) {
251 * Set LDAP version. This will always succeed: If the client
252 * bound with a particular version, then so can we.
254 vers = op->o_conn->c_protocol;
255 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
257 /* automatically chase referrals ("chase-referrals"/"dont-chase-referrals" statement) */
258 if ( LDAP_BACK_CHASE_REFERRALS( mi ) ) {
259 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
263 /* start TLS ("tls [try-]{start|propagate}" statement) */
264 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
265 && !ldap_is_ldaps_url( mt->mt_uri ) )
267 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
269 * use asynchronous StartTLS; in case, chase referral
270 * FIXME: OpenLDAP does not return referral on StartTLS yet
274 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
275 if ( rs->sr_err == LDAP_SUCCESS ) {
276 LDAPMessage *res = NULL;
277 int rc, nretries = mt->mt_nretries;
280 LDAP_BACK_TV_SET( &tv );
283 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
285 rs->sr_err = LDAP_OTHER;
287 } else if ( rc == 0 ) {
288 if ( nretries != 0 ) {
289 if ( nretries > 0 ) {
292 LDAP_BACK_TV_SET( &tv );
295 rs->sr_err = LDAP_OTHER;
297 } else if ( rc == LDAP_RES_EXTENDED ) {
298 struct berval *data = NULL;
300 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
302 if ( rs->sr_err == LDAP_SUCCESS ) {
303 rs->sr_err = ldap_result2error( msc->msc_ld, res, 1 );
306 /* FIXME: in case a referral
307 * is returned, should we try
308 * using it instead of the
310 if ( rs->sr_err == LDAP_SUCCESS ) {
311 ldap_install_tls( msc->msc_ld );
313 } else if ( rs->sr_err == LDAP_REFERRAL ) {
314 rs->sr_err = LDAP_OTHER;
315 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
319 if ( data->bv_val ) {
320 ber_memfree( data->bv_val );
327 rs->sr_err = LDAP_OTHER;
334 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
336 * use synchronous StartTLS
338 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
339 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
341 /* if StartTLS is requested, only attempt it if the URL
342 * is not "ldaps://"; this may occur not only in case
343 * of misconfiguration, but also when used in the chain
344 * overlay, where the "uri" can be parsed out of a referral */
345 if ( rs->sr_err == LDAP_SERVER_DOWN
346 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
348 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
352 #endif /* HAVE_TLS */
355 * Set the network timeout if set
357 if ( mi->mi_network_timeout != 0 ) {
358 struct timeval network_timeout;
360 network_timeout.tv_usec = 0;
361 network_timeout.tv_sec = mi->mi_network_timeout;
363 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
364 (void *)&network_timeout );
368 * If the connection DN is not null, an attempt to rewrite it is made
370 if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
372 dc.conn = op->o_conn;
377 * Rewrite the bind dn if needed
379 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
380 &msc->msc_bound_ndn ) )
385 /* copy the DN idf needed */
386 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
387 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
390 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
393 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
396 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
399 if ( rs->sr_err == LDAP_SUCCESS ) {
401 * Sets a cookie for the rewrite session
403 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
406 rs->sr_err = slap_map_api2result( rs );
407 if ( sendok & LDAP_BACK_SENDERR ) {
408 send_ldap_result( op, rs );
419 * Retries one connection
427 ldap_back_send_t sendok )
429 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
430 metatarget_t *mt = &mi->mi_targets[ candidate ];
431 int rc = LDAP_UNAVAILABLE;
432 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
435 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
437 assert( mc->mc_refcnt > 0 );
439 if ( mc->mc_refcnt == 1 ) {
440 while ( ldap_pvt_thread_mutex_trylock( &mc->mc_mutex ) ) {
441 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
442 ldap_pvt_thread_yield();
446 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
448 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
450 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
452 /* mc here must be the regular mc, reset and ready for init */
453 rc = meta_back_init_one_conn( op, rs, mt, msc, sendok );
455 if ( rc == LDAP_SUCCESS ) {
456 rc = meta_back_single_dobind( op, rs, mc, candidate,
457 sendok, mt->mt_nretries, 0 );
460 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
463 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
465 return rc == LDAP_SUCCESS ? 1 : 0;
469 * callback for unique candidate selection
472 meta_back_conn_cb( Operation *op, SlapReply *rs )
474 assert( op->o_tag == LDAP_REQ_SEARCH );
476 switch ( rs->sr_type ) {
478 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
494 meta_back_get_candidate(
499 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
503 * tries to get a unique candidate
504 * (takes care of default target)
506 candidate = meta_back_select_unique_candidate( mi, ndn );
509 * if any is found, inits the connection
511 if ( candidate == META_TARGET_NONE ) {
512 rs->sr_err = LDAP_NO_SUCH_OBJECT;
513 rs->sr_text = "no suitable candidate target found";
515 } else if ( candidate == META_TARGET_MULTIPLE ) {
518 SlapReply rs2 = { 0 };
519 slap_callback cb2 = { 0 };
522 /* try to get a unique match for the request ndn
523 * among the multiple candidates available */
524 op2.o_tag = LDAP_REQ_SEARCH;
526 op2.o_req_ndn = *ndn;
527 op2.ors_scope = LDAP_SCOPE_BASE;
528 op2.ors_deref = LDAP_DEREF_NEVER;
529 op2.ors_attrs = slap_anlist_no_attrs;
530 op2.ors_attrsonly = 0;
531 op2.ors_limit = NULL;
533 op2.ors_tlimit = SLAP_NO_LIMIT;
535 f.f_choice = LDAP_FILTER_PRESENT;
536 f.f_desc = slap_schema.si_ad_objectClass;
538 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
540 op2.o_callback = &cb2;
541 cb2.sc_response = meta_back_conn_cb;
542 cb2.sc_private = (void *)&candidate;
544 rc = op->o_bd->be_search( &op2, &rs2 );
546 switch ( rs2.sr_err ) {
549 rs->sr_err = rs2.sr_err;
552 case LDAP_SIZELIMIT_EXCEEDED:
553 /* if multiple candidates can serve the operation,
554 * and a default target is defined, and it is
555 * a candidate, try using it (FIXME: YMMV) */
556 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
557 && meta_back_is_candidate( &mi->mi_targets[ mi->mi_defaulttarget ].mt_nsuffix,
558 mi->mi_targets[ mi->mi_defaulttarget ].mt_scope,
559 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
561 candidate = mi->mi_defaulttarget;
562 rs->sr_err = LDAP_SUCCESS;
566 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
567 rs->sr_text = "cannot select unique candidate target";
573 rs->sr_err = LDAP_SUCCESS;
579 static void *meta_back_candidates_dummy;
582 meta_back_candidates_keyfree(
586 metacandidates_t *mc = (metacandidates_t *)data;
588 ber_memfree_x( mc->mc_candidates, NULL );
589 ber_memfree_x( data, NULL );
593 meta_back_candidates_get( Operation *op )
595 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
596 metacandidates_t *mc;
598 if ( op->o_threadctx ) {
601 ldap_pvt_thread_pool_getkey( op->o_threadctx,
602 &meta_back_candidates_dummy, &data, NULL );
603 mc = (metacandidates_t *)data;
606 mc = mi->mi_candidates;
610 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
611 mc->mc_ntargets = mi->mi_ntargets;
612 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
613 if ( op->o_threadctx ) {
617 ldap_pvt_thread_pool_setkey( op->o_threadctx,
618 &meta_back_candidates_dummy, data,
619 meta_back_candidates_keyfree );
622 mi->mi_candidates = mc;
625 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
626 /* NOTE: in the future, may want to allow back-config
627 * to add/remove targets from back-meta... */
628 mc->mc_ntargets = mi->mi_ntargets;
629 mc->mc_candidates = ch_realloc( mc->mc_candidates,
630 sizeof( SlapReply ) * mc->mc_ntargets );
633 return mc->mc_candidates;
639 * Prepares the connection structure
643 * - determine what DN is being requested:
645 * op requires candidate checks
647 * add unique parent of o_req_ndn
648 * bind unique^*[/all] o_req_ndn [no check]
649 * compare unique^+ o_req_ndn
650 * delete unique o_req_ndn
651 * modify unique o_req_ndn
652 * search any o_req_ndn
653 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
655 * - for ops that require the candidate to be unique, in case of multiple
656 * occurrences an internal search with sizeLimit=1 is performed
657 * if a unique candidate can actually be determined. If none is found,
658 * the operation aborts; if multiple are found, the default target
659 * is used if defined and candidate; otherwise the operation aborts.
661 * *^note: actually, the bind operation is handled much like a search;
662 * i.e. the bind is broadcast to all candidate targets.
664 * +^note: actually, the compare operation is handled much like a search;
665 * i.e. the compare is broadcast to all candidate targets, while checking
666 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
674 ldap_back_send_t sendok )
676 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
677 metaconn_t *mc = NULL,
679 int cached = META_TARGET_NONE,
680 i = META_TARGET_NONE,
686 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
689 struct berval ndn = op->o_req_ndn,
692 SlapReply *candidates = meta_back_candidates_get( op );
694 /* Internal searches are privileged and shared. So is root. */
695 /* FIXME: there seem to be concurrency issues */
696 if ( op->o_do_not_cache || be_isroot( op ) ) {
697 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
698 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
699 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
702 mc_curr.mc_local_ndn = op->o_ndn;
704 /* Explicit binds must not be shared */
705 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
706 mc_curr.mc_conn = op->o_conn;
709 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
713 /* Searches for a metaconn in the avl tree */
714 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
715 mc = (metaconn_t *)avl_find( mi->mi_conntree,
716 (caddr_t)&mc_curr, meta_back_conn_cmp );
720 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
722 switch ( op->o_tag ) {
724 /* if we go to selection, the entry must not exist,
725 * and we must be able to resolve the parent */
727 dnParent( &ndn, &pndn );
730 case LDAP_REQ_MODRDN:
731 /* if nnewSuperior is not NULL, it must resolve
732 * to the same candidate as the req_ndn */
733 if ( op->orr_nnewSup ) {
739 /* if bound as rootdn, the backend must bind to all targets
740 * with the administrative identity */
741 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
742 op_type = META_OP_REQUIRE_ALL;
746 case LDAP_REQ_DELETE:
747 case LDAP_REQ_MODIFY:
748 /* just a unique candidate */
751 case LDAP_REQ_COMPARE:
752 case LDAP_REQ_SEARCH:
753 /* allow multiple candidates for the searchBase */
754 op_type = META_OP_ALLOW_MULTIPLE;
758 /* right now, just break (exop?) */
763 * require all connections ...
765 if ( op_type == META_OP_REQUIRE_ALL ) {
767 /* Looks like we didn't get a bind. Open a new session... */
769 mc = metaconn_alloc( op );
770 mc->mc_conn = mc_curr.mc_conn;
771 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
775 for ( i = 0; i < mi->mi_ntargets; i++ ) {
776 metatarget_t *mt = &mi->mi_targets[ i ];
777 metasingleconn_t *msc = &mc->mc_conns[ i ];
780 * The target is activated; if needed, it is
783 candidates[ i ].sr_err = meta_back_init_one_conn( op,
784 rs, mt, msc, sendok );
785 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
786 candidates[ i ].sr_tag = META_CANDIDATE;
789 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
790 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
791 ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
792 LDAP_BACK_CONN_ISPRIV_SET( msc );
795 BER_BVZERO( &msc->msc_cred );
796 BER_BVZERO( &msc->msc_bound_ndn );
797 if ( !BER_BVISEMPTY( &op->o_ndn )
798 && SLAP_IS_AUTHZ_BACKEND( op )
799 && i == mc->mc_authz_target )
801 ber_dupbv( &msc->msc_bound_ndn, &op->o_ndn );
808 * FIXME: in case one target cannot
809 * be init'd, should the other ones
812 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
813 err = candidates[ i ].sr_err;
818 if ( ncandidates == 0 ) {
820 meta_back_freeconn( op, mc );
823 meta_back_release_conn( op, mc );
826 rs->sr_err = LDAP_NO_SUCH_OBJECT;
827 rs->sr_text = "Unable to select valid candidates";
829 if ( sendok & LDAP_BACK_SENDERR ) {
830 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
831 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
833 send_ldap_result( op, rs );
835 rs->sr_matched = NULL;
845 * looks in cache, if any
847 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
848 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
851 if ( op_type == META_OP_REQUIRE_SINGLE ) {
852 metatarget_t *mt = NULL;
853 metasingleconn_t *msc = NULL;
857 for ( j = 0; j < mi->mi_ntargets; j++ ) {
858 candidates[ j ].sr_tag = META_NOT_CANDIDATE;
862 * tries to get a unique candidate
863 * (takes care of default target)
865 if ( i == META_TARGET_NONE ) {
866 i = meta_back_get_candidate( op, rs, &ndn );
868 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && parent ) {
869 i = meta_back_get_candidate( op, rs, &pndn );
872 if ( rs->sr_err != LDAP_SUCCESS ) {
874 meta_back_release_conn( op, mc );
877 if ( sendok & LDAP_BACK_SENDERR ) {
878 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
879 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
881 send_ldap_result( op, rs );
883 rs->sr_matched = NULL;
890 if ( newparent && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
893 meta_back_release_conn( op, mc );
896 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
897 rs->sr_text = "cross-target rename not supported";
898 if ( sendok & LDAP_BACK_SENDERR ) {
899 send_ldap_result( op, rs );
906 Debug( LDAP_DEBUG_TRACE,
907 "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
908 i, op->o_req_ndn.bv_val, 0 );
911 /* Retries searching for a metaconn in the avl tree
912 * the reason is that the connection might have been
913 * created by meta_back_get_candidate() */
914 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
915 mc = (metaconn_t *)avl_find( mi->mi_conntree,
916 (caddr_t)&mc_curr, meta_back_conn_cmp );
920 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
922 /* Looks like we didn't get a bind. Open a new session... */
924 mc = metaconn_alloc( op );
925 mc->mc_conn = mc_curr.mc_conn;
926 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
932 * Clear all other candidates
934 ( void )meta_clear_unused_candidates( op, i );
936 mt = &mi->mi_targets[ i ];
937 msc = &mc->mc_conns[ i ];
940 * The target is activated; if needed, it is
941 * also init'd. In case of error, meta_back_init_one_conn
942 * sends the appropriate result.
944 err = meta_back_init_one_conn( op, rs, mt, msc, sendok );
945 if ( err != LDAP_SUCCESS ) {
947 * FIXME: in case one target cannot
948 * be init'd, should the other ones
951 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
953 (void)meta_clear_one_candidate( msc );
954 meta_back_freeconn( op, mc );
957 meta_back_release_conn( op, mc );
962 candidates[ i ].sr_err = LDAP_SUCCESS;
963 candidates[ i ].sr_tag = META_CANDIDATE;
970 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
971 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
972 ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
973 LDAP_BACK_CONN_ISPRIV_SET( msc );
976 BER_BVZERO( &msc->msc_cred );
977 BER_BVZERO( &msc->msc_bound_ndn );
978 if ( !BER_BVISEMPTY( &op->o_ndn )
979 && SLAP_IS_AUTHZ_BACKEND( op )
980 && i == mc->mc_authz_target )
982 ber_dupbv( &msc->msc_bound_ndn, &op->o_ndn );
987 * if no unique candidate ...
991 /* Looks like we didn't get a bind. Open a new session... */
993 mc = metaconn_alloc( op );
994 mc->mc_conn = mc_curr.mc_conn;
995 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
999 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1000 metatarget_t *mt = &mi->mi_targets[ i ];
1001 metasingleconn_t *msc = &mc->mc_conns[ i ];
1004 || meta_back_is_candidate( &mt->mt_nsuffix,
1007 LDAP_SCOPE_SUBTREE ) )
1011 * The target is activated; if needed, it is
1014 int lerr = meta_back_init_one_conn( op, rs,
1016 if ( lerr == LDAP_SUCCESS ) {
1017 candidates[ i ].sr_tag = META_CANDIDATE;
1018 candidates[ i ].sr_err = LDAP_SUCCESS;
1021 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1022 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
1023 ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
1024 LDAP_BACK_CONN_ISPRIV_SET( msc );
1027 BER_BVZERO( &msc->msc_cred );
1028 BER_BVZERO( &msc->msc_bound_ndn );
1029 if ( !BER_BVISEMPTY( &op->o_ndn )
1030 && SLAP_IS_AUTHZ_BACKEND( op )
1031 && i == mc->mc_authz_target )
1033 ber_dupbv( &msc->msc_bound_ndn, &op->o_ndn );
1037 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_init_one_conn(%d)\n",
1038 op->o_log_prefix, i, 0 );
1043 * FIXME: in case one target cannot
1044 * be init'd, should the other ones
1048 ( void )meta_clear_one_candidate( msc );
1050 /* leave the target candidate, but record the error for later use */
1051 candidates[ i ].sr_err = lerr;
1054 Debug( LDAP_DEBUG_ANY, "%s: meta_back_init_one_conn(%d) failed: %d\n",
1055 op->o_log_prefix, i, lerr );
1062 ( void )meta_clear_one_candidate( msc );
1064 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1068 if ( ncandidates == 0 ) {
1070 meta_back_freeconn( op, mc );
1073 meta_back_release_conn( op, mc );
1076 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1077 rs->sr_text = "Unable to select valid candidates";
1079 if ( sendok & LDAP_BACK_SENDERR ) {
1080 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1081 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1083 send_ldap_result( op, rs );
1085 rs->sr_matched = NULL;
1093 /* clear out meta_back_init_one_conn non-fatal errors */
1094 rs->sr_err = LDAP_SUCCESS;
1100 * Inserts the newly created metaconn in the avl tree
1102 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
1103 err = avl_insert( &mi->mi_conntree, ( caddr_t )mc,
1104 meta_back_conn_cmp, meta_back_conn_dup );
1106 #if PRINT_CONNTREE > 0
1107 myprint( mi->mi_conntree );
1108 #endif /* PRINT_CONNTREE */
1110 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
1113 * Err could be -1 in case a duplicate metaconn is inserted
1115 * FIXME: what if the same client issues more than one
1116 * asynchronous operations?
1119 Debug( LDAP_DEBUG_ANY,
1120 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1121 op->o_log_prefix, ncandidates,
1122 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1124 rs->sr_err = LDAP_OTHER;
1125 rs->sr_text = "Internal server error";
1126 meta_back_freeconn( op, mc );
1127 if ( sendok & LDAP_BACK_SENDERR ) {
1128 send_ldap_result( op, rs );
1134 Debug( LDAP_DEBUG_TRACE,
1135 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1136 op->o_log_prefix, ncandidates,
1137 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1140 Debug( LDAP_DEBUG_TRACE,
1141 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1142 op->o_log_prefix, ncandidates,
1143 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1150 meta_back_release_conn(
1154 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1156 assert( mc != NULL );
1158 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
1159 assert( mc->mc_refcnt > 0 );
1161 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );