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 );
184 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
186 assert( mc != NULL );
188 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
190 if ( --mc->mc_refcnt == 0 ) {
191 meta_back_conn_free( mc );
194 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
198 * meta_back_init_one_conn
200 * Initializes one connection
203 meta_back_init_one_conn(
208 metasingleconn_t *msc,
211 ldap_back_send_t sendok )
213 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
220 if ( msc->msc_ld != NULL ) {
221 return rs->sr_err = LDAP_SUCCESS;
225 * Attempts to initialize the connection to the target ds
227 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
228 if ( rs->sr_err != LDAP_SUCCESS ) {
233 * Set LDAP version. This will always succeed: If the client
234 * bound with a particular version, then so can we.
236 vers = op->o_conn->c_protocol;
237 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
239 /* automatically chase referrals ("chase-referrals"/"dont-chase-referrals" statement) */
240 if ( LDAP_BACK_CHASE_REFERRALS( mi ) ) {
241 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
245 /* start TLS ("tls [try-]{start|propagate}" statement) */
246 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
247 && !ldap_is_ldaps_url( mt->mt_uri ) )
249 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
251 * use asynchronous StartTLS; in case, chase referral
252 * FIXME: OpenLDAP does not return referral on StartTLS yet
256 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
257 if ( rs->sr_err == LDAP_SUCCESS ) {
258 LDAPMessage *res = NULL;
259 int rc, nretries = mt->mt_nretries;
262 LDAP_BACK_TV_SET( &tv );
265 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
267 rs->sr_err = LDAP_OTHER;
269 } else if ( rc == 0 ) {
270 if ( nretries != 0 ) {
271 if ( nretries > 0 ) {
274 LDAP_BACK_TV_SET( &tv );
277 rs->sr_err = LDAP_OTHER;
279 } else if ( rc == LDAP_RES_EXTENDED ) {
280 struct berval *data = NULL;
282 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
284 if ( rs->sr_err == LDAP_SUCCESS ) {
285 rs->sr_err = ldap_result2error( msc->msc_ld, res, 1 );
288 /* FIXME: in case a referral
289 * is returned, should we try
290 * using it instead of the
292 if ( rs->sr_err == LDAP_SUCCESS ) {
293 ldap_install_tls( msc->msc_ld );
295 } else if ( rs->sr_err == LDAP_REFERRAL ) {
296 rs->sr_err = LDAP_OTHER;
297 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
301 if ( data->bv_val ) {
302 ber_memfree( data->bv_val );
309 rs->sr_err = LDAP_OTHER;
316 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
318 * use synchronous StartTLS
320 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
321 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
323 /* if StartTLS is requested, only attempt it if the URL
324 * is not "ldaps://"; this may occur not only in case
325 * of misconfiguration, but also when used in the chain
326 * overlay, where the "uri" can be parsed out of a referral */
327 if ( rs->sr_err == LDAP_SERVER_DOWN
328 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
330 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
334 #endif /* HAVE_TLS */
337 * Set the network timeout if set
339 if ( mi->mi_network_timeout != 0 ) {
340 struct timeval network_timeout;
342 network_timeout.tv_usec = 0;
343 network_timeout.tv_sec = mi->mi_network_timeout;
345 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
346 (void *)&network_timeout );
350 * If the connection DN is not null, an attempt to rewrite it is made
354 if ( !BER_BVISNULL( &mt->mt_pseudorootdn ) ) {
355 ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
356 if ( !BER_BVISNULL( &mt->mt_pseudorootpw ) ) {
357 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
361 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
364 LDAP_BACK_CONN_ISPRIV_SET( msc );
367 BER_BVZERO( &msc->msc_cred );
368 BER_BVZERO( &msc->msc_bound_ndn );
369 if ( !BER_BVISEMPTY( &op->o_ndn )
370 && SLAP_IS_AUTHZ_BACKEND( op )
374 dc.conn = op->o_conn;
379 * Rewrite the bind dn if needed
381 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
382 &msc->msc_bound_ndn ) )
384 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
388 /* copy the DN idf needed */
389 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
390 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
394 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
398 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
400 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
403 if ( rs->sr_err == LDAP_SUCCESS ) {
405 * Sets a cookie for the rewrite session
407 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
410 rs->sr_err = slap_map_api2result( rs );
411 if ( sendok & LDAP_BACK_SENDERR ) {
412 send_ldap_result( op, rs );
423 * Retries one connection
431 ldap_back_send_t sendok )
433 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
434 metatarget_t *mt = &mi->mi_targets[ candidate ];
435 int rc = LDAP_UNAVAILABLE;
436 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
439 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
441 assert( mc->mc_refcnt > 0 );
443 if ( mc->mc_refcnt == 1 ) {
444 while ( ldap_pvt_thread_mutex_trylock( &mc->mc_mutex ) ) {
445 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
446 ldap_pvt_thread_yield();
450 meta_clear_one_candidate( msc );
451 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
453 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
455 /* mc here must be the regular mc, reset and ready for init */
456 rc = meta_back_init_one_conn( op, rs, mt, mc, msc,
457 LDAP_BACK_CONN_ISPRIV( mc ),
458 candidate == mc->mc_authz_target, sendok );
460 if ( rc == LDAP_SUCCESS ) {
461 rc = meta_back_single_dobind( op, rs, mc, candidate,
462 sendok, mt->mt_nretries, 0 );
465 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
468 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
470 return rc == LDAP_SUCCESS ? 1 : 0;
474 * callback for unique candidate selection
477 meta_back_conn_cb( Operation *op, SlapReply *rs )
479 assert( op->o_tag == LDAP_REQ_SEARCH );
481 switch ( rs->sr_type ) {
483 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
499 meta_back_get_candidate(
504 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
508 * tries to get a unique candidate
509 * (takes care of default target)
511 candidate = meta_back_select_unique_candidate( mi, ndn );
514 * if any is found, inits the connection
516 if ( candidate == META_TARGET_NONE ) {
517 rs->sr_err = LDAP_NO_SUCH_OBJECT;
518 rs->sr_text = "no suitable candidate target found";
520 } else if ( candidate == META_TARGET_MULTIPLE ) {
523 SlapReply rs2 = { 0 };
524 slap_callback cb2 = { 0 };
527 /* try to get a unique match for the request ndn
528 * among the multiple candidates available */
529 op2.o_tag = LDAP_REQ_SEARCH;
531 op2.o_req_ndn = *ndn;
532 op2.ors_scope = LDAP_SCOPE_BASE;
533 op2.ors_deref = LDAP_DEREF_NEVER;
534 op2.ors_attrs = slap_anlist_no_attrs;
535 op2.ors_attrsonly = 0;
536 op2.ors_limit = NULL;
538 op2.ors_tlimit = SLAP_NO_LIMIT;
540 f.f_choice = LDAP_FILTER_PRESENT;
541 f.f_desc = slap_schema.si_ad_objectClass;
543 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
545 op2.o_callback = &cb2;
546 cb2.sc_response = meta_back_conn_cb;
547 cb2.sc_private = (void *)&candidate;
549 rc = op->o_bd->be_search( &op2, &rs2 );
551 switch ( rs2.sr_err ) {
554 rs->sr_err = rs2.sr_err;
557 case LDAP_SIZELIMIT_EXCEEDED:
558 /* if multiple candidates can serve the operation,
559 * and a default target is defined, and it is
560 * a candidate, try using it (FIXME: YMMV) */
561 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
562 && meta_back_is_candidate( &mi->mi_targets[ mi->mi_defaulttarget ].mt_nsuffix,
563 mi->mi_targets[ mi->mi_defaulttarget ].mt_scope,
564 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
566 candidate = mi->mi_defaulttarget;
567 rs->sr_err = LDAP_SUCCESS;
571 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
572 rs->sr_text = "cannot select unique candidate target";
578 rs->sr_err = LDAP_SUCCESS;
584 static void *meta_back_candidates_dummy;
587 meta_back_candidates_keyfree(
591 metacandidates_t *mc = (metacandidates_t *)data;
593 ber_memfree_x( mc->mc_candidates, NULL );
594 ber_memfree_x( data, NULL );
598 meta_back_candidates_get( Operation *op )
600 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
601 metacandidates_t *mc;
603 if ( op->o_threadctx ) {
606 ldap_pvt_thread_pool_getkey( op->o_threadctx,
607 &meta_back_candidates_dummy, &data, NULL );
608 mc = (metacandidates_t *)data;
611 mc = mi->mi_candidates;
615 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
616 mc->mc_ntargets = mi->mi_ntargets;
617 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
618 if ( op->o_threadctx ) {
622 ldap_pvt_thread_pool_setkey( op->o_threadctx,
623 &meta_back_candidates_dummy, data,
624 meta_back_candidates_keyfree );
627 mi->mi_candidates = mc;
630 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
631 /* NOTE: in the future, may want to allow back-config
632 * to add/remove targets from back-meta... */
633 mc->mc_ntargets = mi->mi_ntargets;
634 mc->mc_candidates = ch_realloc( mc->mc_candidates,
635 sizeof( SlapReply ) * mc->mc_ntargets );
638 return mc->mc_candidates;
644 * Prepares the connection structure
648 * - determine what DN is being requested:
650 * op requires candidate checks
652 * add unique parent of o_req_ndn
653 * bind unique^*[/all] o_req_ndn [no check]
654 * compare unique^+ o_req_ndn
655 * delete unique o_req_ndn
656 * modify unique o_req_ndn
657 * search any o_req_ndn
658 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
660 * - for ops that require the candidate to be unique, in case of multiple
661 * occurrences an internal search with sizeLimit=1 is performed
662 * if a unique candidate can actually be determined. If none is found,
663 * the operation aborts; if multiple are found, the default target
664 * is used if defined and candidate; otherwise the operation aborts.
666 * *^note: actually, the bind operation is handled much like a search;
667 * i.e. the bind is broadcast to all candidate targets.
669 * +^note: actually, the compare operation is handled much like a search;
670 * i.e. the compare is broadcast to all candidate targets, while checking
671 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
679 ldap_back_send_t sendok )
681 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
682 metaconn_t *mc = NULL,
684 int cached = META_TARGET_NONE,
685 i = META_TARGET_NONE,
691 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
694 struct berval ndn = op->o_req_ndn,
697 SlapReply *candidates = meta_back_candidates_get( op );
699 /* Internal searches are privileged and shared. So is root. */
700 /* FIXME: there seem to be concurrency issues */
701 if ( op->o_do_not_cache || be_isroot( op ) ) {
702 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
703 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
704 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
707 mc_curr.mc_local_ndn = op->o_ndn;
709 /* Explicit binds must not be shared */
710 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
711 mc_curr.mc_conn = op->o_conn;
714 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
718 /* Searches for a metaconn in the avl tree */
719 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
720 mc = (metaconn_t *)avl_find( mi->mi_conntree,
721 (caddr_t)&mc_curr, meta_back_conn_cmp );
725 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
727 switch ( op->o_tag ) {
729 /* if we go to selection, the entry must not exist,
730 * and we must be able to resolve the parent */
732 dnParent( &ndn, &pndn );
735 case LDAP_REQ_MODRDN:
736 /* if nnewSuperior is not NULL, it must resolve
737 * to the same candidate as the req_ndn */
738 if ( op->orr_nnewSup ) {
744 /* if bound as rootdn, the backend must bind to all targets
745 * with the administrative identity */
746 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
747 op_type = META_OP_REQUIRE_ALL;
751 case LDAP_REQ_DELETE:
752 case LDAP_REQ_MODIFY:
753 /* just a unique candidate */
756 case LDAP_REQ_COMPARE:
757 case LDAP_REQ_SEARCH:
758 /* allow multiple candidates for the searchBase */
759 op_type = META_OP_ALLOW_MULTIPLE;
763 /* right now, just break (exop?) */
768 * require all connections ...
770 if ( op_type == META_OP_REQUIRE_ALL ) {
772 /* Looks like we didn't get a bind. Open a new session... */
774 mc = metaconn_alloc( op );
775 mc->mc_conn = mc_curr.mc_conn;
776 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
780 for ( i = 0; i < mi->mi_ntargets; i++ ) {
781 metatarget_t *mt = &mi->mi_targets[ i ];
782 metasingleconn_t *msc = &mc->mc_conns[ i ];
785 * The target is activated; if needed, it is
788 candidates[ i ].sr_err = meta_back_init_one_conn( op,
790 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
791 i == mc->mc_authz_target, sendok );
792 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
793 candidates[ i ].sr_tag = META_CANDIDATE;
799 * FIXME: in case one target cannot
800 * be init'd, should the other ones
803 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
804 err = candidates[ i ].sr_err;
809 if ( ncandidates == 0 ) {
811 meta_back_freeconn( op, mc );
814 meta_back_release_conn( op, mc );
817 rs->sr_err = LDAP_NO_SUCH_OBJECT;
818 rs->sr_text = "Unable to select valid candidates";
820 if ( sendok & LDAP_BACK_SENDERR ) {
821 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
822 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
824 send_ldap_result( op, rs );
826 rs->sr_matched = NULL;
836 * looks in cache, if any
838 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
839 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
842 if ( op_type == META_OP_REQUIRE_SINGLE ) {
843 metatarget_t *mt = NULL;
844 metasingleconn_t *msc = NULL;
848 for ( j = 0; j < mi->mi_ntargets; j++ ) {
849 candidates[ j ].sr_tag = META_NOT_CANDIDATE;
853 * tries to get a unique candidate
854 * (takes care of default target)
856 if ( i == META_TARGET_NONE ) {
857 i = meta_back_get_candidate( op, rs, &ndn );
859 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && parent ) {
860 i = meta_back_get_candidate( op, rs, &pndn );
863 if ( rs->sr_err != LDAP_SUCCESS ) {
865 meta_back_release_conn( op, mc );
868 if ( sendok & LDAP_BACK_SENDERR ) {
869 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
870 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
872 send_ldap_result( op, rs );
874 rs->sr_matched = NULL;
881 if ( newparent && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
884 meta_back_release_conn( op, mc );
887 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
888 rs->sr_text = "cross-target rename not supported";
889 if ( sendok & LDAP_BACK_SENDERR ) {
890 send_ldap_result( op, rs );
897 Debug( LDAP_DEBUG_TRACE,
898 "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
899 i, op->o_req_ndn.bv_val, 0 );
902 /* Retries searching for a metaconn in the avl tree
903 * the reason is that the connection might have been
904 * created by meta_back_get_candidate() */
905 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
906 mc = (metaconn_t *)avl_find( mi->mi_conntree,
907 (caddr_t)&mc_curr, meta_back_conn_cmp );
911 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
913 /* Looks like we didn't get a bind. Open a new session... */
915 mc = metaconn_alloc( op );
916 mc->mc_conn = mc_curr.mc_conn;
917 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
923 * Clear all other candidates
925 ( void )meta_clear_unused_candidates( op, i );
927 mt = &mi->mi_targets[ i ];
928 msc = &mc->mc_conns[ i ];
931 * The target is activated; if needed, it is
932 * also init'd. In case of error, meta_back_init_one_conn
933 * sends the appropriate result.
935 err = meta_back_init_one_conn( op, rs, mt, mc, msc,
936 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
937 i == mc->mc_authz_target, sendok );
938 if ( err != LDAP_SUCCESS ) {
940 * FIXME: in case one target cannot
941 * be init'd, should the other ones
944 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
946 (void)meta_clear_one_candidate( msc );
947 meta_back_freeconn( op, mc );
950 meta_back_release_conn( op, mc );
955 candidates[ i ].sr_err = LDAP_SUCCESS;
956 candidates[ i ].sr_tag = META_CANDIDATE;
964 * if no unique candidate ...
968 /* Looks like we didn't get a bind. Open a new session... */
970 mc = metaconn_alloc( op );
971 mc->mc_conn = mc_curr.mc_conn;
972 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
976 for ( i = 0; i < mi->mi_ntargets; i++ ) {
977 metatarget_t *mt = &mi->mi_targets[ i ];
978 metasingleconn_t *msc = &mc->mc_conns[ i ];
981 || meta_back_is_candidate( &mt->mt_nsuffix,
984 LDAP_SCOPE_SUBTREE ) )
988 * The target is activated; if needed, it is
991 int lerr = meta_back_init_one_conn( op, rs,
993 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
994 i == mc->mc_authz_target,
996 if ( lerr == LDAP_SUCCESS ) {
997 candidates[ i ].sr_tag = META_CANDIDATE;
998 candidates[ i ].sr_err = LDAP_SUCCESS;
1001 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_init_one_conn(%d)\n",
1002 op->o_log_prefix, i, 0 );
1007 * FIXME: in case one target cannot
1008 * be init'd, should the other ones
1012 ( void )meta_clear_one_candidate( msc );
1014 /* leave the target candidate, but record the error for later use */
1015 candidates[ i ].sr_err = lerr;
1018 Debug( LDAP_DEBUG_ANY, "%s: meta_back_init_one_conn(%d) failed: %d\n",
1019 op->o_log_prefix, i, lerr );
1026 ( void )meta_clear_one_candidate( msc );
1028 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1032 if ( ncandidates == 0 ) {
1034 meta_back_freeconn( op, mc );
1037 meta_back_release_conn( op, mc );
1040 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1041 rs->sr_text = "Unable to select valid candidates";
1043 if ( sendok & LDAP_BACK_SENDERR ) {
1044 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1045 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1047 send_ldap_result( op, rs );
1049 rs->sr_matched = NULL;
1057 /* clear out meta_back_init_one_conn non-fatal errors */
1058 rs->sr_err = LDAP_SUCCESS;
1064 * Inserts the newly created metaconn in the avl tree
1066 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
1067 err = avl_insert( &mi->mi_conntree, ( caddr_t )mc,
1068 meta_back_conn_cmp, meta_back_conn_dup );
1070 #if PRINT_CONNTREE > 0
1071 myprint( mi->mi_conntree );
1072 #endif /* PRINT_CONNTREE */
1074 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
1077 * Err could be -1 in case a duplicate metaconn is inserted
1079 * FIXME: what if the same client issues more than one
1080 * asynchronous operations?
1083 Debug( LDAP_DEBUG_ANY,
1084 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1085 op->o_log_prefix, ncandidates,
1086 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1088 rs->sr_err = LDAP_OTHER;
1089 rs->sr_text = "Internal server error";
1090 meta_back_freeconn( op, mc );
1091 if ( sendok & LDAP_BACK_SENDERR ) {
1092 send_ldap_result( op, rs );
1098 Debug( LDAP_DEBUG_TRACE,
1099 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1100 op->o_log_prefix, ncandidates,
1101 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1104 Debug( LDAP_DEBUG_TRACE,
1105 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1106 op->o_log_prefix, ncandidates,
1107 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1114 meta_back_release_conn(
1118 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1120 assert( mc != NULL );
1122 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
1123 assert( mc->mc_refcnt > 0 );
1125 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );