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 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
355 ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
356 LDAP_BACK_CONN_ISPRIV_SET( msc );
359 BER_BVZERO( &msc->msc_cred );
360 BER_BVZERO( &msc->msc_bound_ndn );
361 if ( !BER_BVISEMPTY( &op->o_ndn )
362 && SLAP_IS_AUTHZ_BACKEND( op )
366 dc.conn = op->o_conn;
371 * Rewrite the bind dn if needed
373 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
374 &msc->msc_bound_ndn ) )
376 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
380 /* copy the DN idf needed */
381 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
382 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
386 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
390 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
392 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
395 if ( rs->sr_err == LDAP_SUCCESS ) {
397 * Sets a cookie for the rewrite session
399 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
402 rs->sr_err = slap_map_api2result( rs );
403 if ( sendok & LDAP_BACK_SENDERR ) {
404 send_ldap_result( op, rs );
415 * Retries one connection
423 ldap_back_send_t sendok )
425 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
426 metatarget_t *mt = &mi->mi_targets[ candidate ];
427 int rc = LDAP_UNAVAILABLE;
428 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
431 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
433 assert( mc->mc_refcnt > 0 );
435 if ( mc->mc_refcnt == 1 ) {
436 while ( ldap_pvt_thread_mutex_trylock( &mc->mc_mutex ) ) {
437 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
438 ldap_pvt_thread_yield();
442 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
444 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
446 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
448 /* mc here must be the regular mc, reset and ready for init */
449 rc = meta_back_init_one_conn( op, rs, mt, mc, msc,
450 LDAP_BACK_CONN_ISPRIV( mc ),
451 candidate == mc->mc_authz_target, sendok );
453 if ( rc == LDAP_SUCCESS ) {
454 rc = meta_back_single_dobind( op, rs, mc, candidate,
455 sendok, mt->mt_nretries, 0 );
458 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
461 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
463 return rc == LDAP_SUCCESS ? 1 : 0;
467 * callback for unique candidate selection
470 meta_back_conn_cb( Operation *op, SlapReply *rs )
472 assert( op->o_tag == LDAP_REQ_SEARCH );
474 switch ( rs->sr_type ) {
476 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
492 meta_back_get_candidate(
497 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
501 * tries to get a unique candidate
502 * (takes care of default target)
504 candidate = meta_back_select_unique_candidate( mi, ndn );
507 * if any is found, inits the connection
509 if ( candidate == META_TARGET_NONE ) {
510 rs->sr_err = LDAP_NO_SUCH_OBJECT;
511 rs->sr_text = "no suitable candidate target found";
513 } else if ( candidate == META_TARGET_MULTIPLE ) {
516 SlapReply rs2 = { 0 };
517 slap_callback cb2 = { 0 };
520 /* try to get a unique match for the request ndn
521 * among the multiple candidates available */
522 op2.o_tag = LDAP_REQ_SEARCH;
524 op2.o_req_ndn = *ndn;
525 op2.ors_scope = LDAP_SCOPE_BASE;
526 op2.ors_deref = LDAP_DEREF_NEVER;
527 op2.ors_attrs = slap_anlist_no_attrs;
528 op2.ors_attrsonly = 0;
529 op2.ors_limit = NULL;
531 op2.ors_tlimit = SLAP_NO_LIMIT;
533 f.f_choice = LDAP_FILTER_PRESENT;
534 f.f_desc = slap_schema.si_ad_objectClass;
536 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
538 op2.o_callback = &cb2;
539 cb2.sc_response = meta_back_conn_cb;
540 cb2.sc_private = (void *)&candidate;
542 rc = op->o_bd->be_search( &op2, &rs2 );
544 switch ( rs2.sr_err ) {
547 rs->sr_err = rs2.sr_err;
550 case LDAP_SIZELIMIT_EXCEEDED:
551 /* if multiple candidates can serve the operation,
552 * and a default target is defined, and it is
553 * a candidate, try using it (FIXME: YMMV) */
554 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
555 && meta_back_is_candidate( &mi->mi_targets[ mi->mi_defaulttarget ].mt_nsuffix,
556 mi->mi_targets[ mi->mi_defaulttarget ].mt_scope,
557 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
559 candidate = mi->mi_defaulttarget;
560 rs->sr_err = LDAP_SUCCESS;
564 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
565 rs->sr_text = "cannot select unique candidate target";
571 rs->sr_err = LDAP_SUCCESS;
577 static void *meta_back_candidates_dummy;
580 meta_back_candidates_keyfree(
584 metacandidates_t *mc = (metacandidates_t *)data;
586 ber_memfree_x( mc->mc_candidates, NULL );
587 ber_memfree_x( data, NULL );
591 meta_back_candidates_get( Operation *op )
593 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
594 metacandidates_t *mc;
596 if ( op->o_threadctx ) {
599 ldap_pvt_thread_pool_getkey( op->o_threadctx,
600 &meta_back_candidates_dummy, &data, NULL );
601 mc = (metacandidates_t *)data;
604 mc = mi->mi_candidates;
608 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
609 mc->mc_ntargets = mi->mi_ntargets;
610 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
611 if ( op->o_threadctx ) {
615 ldap_pvt_thread_pool_setkey( op->o_threadctx,
616 &meta_back_candidates_dummy, data,
617 meta_back_candidates_keyfree );
620 mi->mi_candidates = mc;
623 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
624 /* NOTE: in the future, may want to allow back-config
625 * to add/remove targets from back-meta... */
626 mc->mc_ntargets = mi->mi_ntargets;
627 mc->mc_candidates = ch_realloc( mc->mc_candidates,
628 sizeof( SlapReply ) * mc->mc_ntargets );
631 return mc->mc_candidates;
637 * Prepares the connection structure
641 * - determine what DN is being requested:
643 * op requires candidate checks
645 * add unique parent of o_req_ndn
646 * bind unique^*[/all] o_req_ndn [no check]
647 * compare unique^+ o_req_ndn
648 * delete unique o_req_ndn
649 * modify unique o_req_ndn
650 * search any o_req_ndn
651 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
653 * - for ops that require the candidate to be unique, in case of multiple
654 * occurrences an internal search with sizeLimit=1 is performed
655 * if a unique candidate can actually be determined. If none is found,
656 * the operation aborts; if multiple are found, the default target
657 * is used if defined and candidate; otherwise the operation aborts.
659 * *^note: actually, the bind operation is handled much like a search;
660 * i.e. the bind is broadcast to all candidate targets.
662 * +^note: actually, the compare operation is handled much like a search;
663 * i.e. the compare is broadcast to all candidate targets, while checking
664 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
672 ldap_back_send_t sendok )
674 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
675 metaconn_t *mc = NULL,
677 int cached = META_TARGET_NONE,
678 i = META_TARGET_NONE,
684 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
687 struct berval ndn = op->o_req_ndn,
690 SlapReply *candidates = meta_back_candidates_get( op );
692 /* Internal searches are privileged and shared. So is root. */
693 /* FIXME: there seem to be concurrency issues */
694 if ( op->o_do_not_cache || be_isroot( op ) ) {
695 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
696 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
697 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
700 mc_curr.mc_local_ndn = op->o_ndn;
702 /* Explicit binds must not be shared */
703 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
704 mc_curr.mc_conn = op->o_conn;
707 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
711 /* Searches for a metaconn in the avl tree */
712 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
713 mc = (metaconn_t *)avl_find( mi->mi_conntree,
714 (caddr_t)&mc_curr, meta_back_conn_cmp );
718 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
720 switch ( op->o_tag ) {
722 /* if we go to selection, the entry must not exist,
723 * and we must be able to resolve the parent */
725 dnParent( &ndn, &pndn );
728 case LDAP_REQ_MODRDN:
729 /* if nnewSuperior is not NULL, it must resolve
730 * to the same candidate as the req_ndn */
731 if ( op->orr_nnewSup ) {
737 /* if bound as rootdn, the backend must bind to all targets
738 * with the administrative identity */
739 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
740 op_type = META_OP_REQUIRE_ALL;
744 case LDAP_REQ_DELETE:
745 case LDAP_REQ_MODIFY:
746 /* just a unique candidate */
749 case LDAP_REQ_COMPARE:
750 case LDAP_REQ_SEARCH:
751 /* allow multiple candidates for the searchBase */
752 op_type = META_OP_ALLOW_MULTIPLE;
756 /* right now, just break (exop?) */
761 * require all connections ...
763 if ( op_type == META_OP_REQUIRE_ALL ) {
765 /* Looks like we didn't get a bind. Open a new session... */
767 mc = metaconn_alloc( op );
768 mc->mc_conn = mc_curr.mc_conn;
769 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
773 for ( i = 0; i < mi->mi_ntargets; i++ ) {
774 metatarget_t *mt = &mi->mi_targets[ i ];
775 metasingleconn_t *msc = &mc->mc_conns[ i ];
778 * The target is activated; if needed, it is
781 candidates[ i ].sr_err = meta_back_init_one_conn( op,
783 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
784 i == mc->mc_authz_target, sendok );
785 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
786 candidates[ i ].sr_tag = META_CANDIDATE;
792 * FIXME: in case one target cannot
793 * be init'd, should the other ones
796 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
797 err = candidates[ i ].sr_err;
802 if ( ncandidates == 0 ) {
804 meta_back_freeconn( op, mc );
807 meta_back_release_conn( op, mc );
810 rs->sr_err = LDAP_NO_SUCH_OBJECT;
811 rs->sr_text = "Unable to select valid candidates";
813 if ( sendok & LDAP_BACK_SENDERR ) {
814 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
815 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
817 send_ldap_result( op, rs );
819 rs->sr_matched = NULL;
829 * looks in cache, if any
831 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
832 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
835 if ( op_type == META_OP_REQUIRE_SINGLE ) {
836 metatarget_t *mt = NULL;
837 metasingleconn_t *msc = NULL;
841 for ( j = 0; j < mi->mi_ntargets; j++ ) {
842 candidates[ j ].sr_tag = META_NOT_CANDIDATE;
846 * tries to get a unique candidate
847 * (takes care of default target)
849 if ( i == META_TARGET_NONE ) {
850 i = meta_back_get_candidate( op, rs, &ndn );
852 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && parent ) {
853 i = meta_back_get_candidate( op, rs, &pndn );
856 if ( rs->sr_err != LDAP_SUCCESS ) {
858 meta_back_release_conn( op, mc );
861 if ( sendok & LDAP_BACK_SENDERR ) {
862 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
863 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
865 send_ldap_result( op, rs );
867 rs->sr_matched = NULL;
874 if ( newparent && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
877 meta_back_release_conn( op, mc );
880 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
881 rs->sr_text = "cross-target rename not supported";
882 if ( sendok & LDAP_BACK_SENDERR ) {
883 send_ldap_result( op, rs );
890 Debug( LDAP_DEBUG_TRACE,
891 "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
892 i, op->o_req_ndn.bv_val, 0 );
895 /* Retries searching for a metaconn in the avl tree
896 * the reason is that the connection might have been
897 * created by meta_back_get_candidate() */
898 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
899 mc = (metaconn_t *)avl_find( mi->mi_conntree,
900 (caddr_t)&mc_curr, meta_back_conn_cmp );
904 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
906 /* Looks like we didn't get a bind. Open a new session... */
908 mc = metaconn_alloc( op );
909 mc->mc_conn = mc_curr.mc_conn;
910 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
916 * Clear all other candidates
918 ( void )meta_clear_unused_candidates( op, i );
920 mt = &mi->mi_targets[ i ];
921 msc = &mc->mc_conns[ i ];
924 * The target is activated; if needed, it is
925 * also init'd. In case of error, meta_back_init_one_conn
926 * sends the appropriate result.
928 err = meta_back_init_one_conn( op, rs, mt, mc, msc,
929 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
930 i == mc->mc_authz_target, sendok );
931 if ( err != LDAP_SUCCESS ) {
933 * FIXME: in case one target cannot
934 * be init'd, should the other ones
937 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
939 (void)meta_clear_one_candidate( msc );
940 meta_back_freeconn( op, mc );
943 meta_back_release_conn( op, mc );
948 candidates[ i ].sr_err = LDAP_SUCCESS;
949 candidates[ i ].sr_tag = META_CANDIDATE;
957 * if no unique candidate ...
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 );
969 for ( i = 0; i < mi->mi_ntargets; i++ ) {
970 metatarget_t *mt = &mi->mi_targets[ i ];
971 metasingleconn_t *msc = &mc->mc_conns[ i ];
974 || meta_back_is_candidate( &mt->mt_nsuffix,
977 LDAP_SCOPE_SUBTREE ) )
981 * The target is activated; if needed, it is
984 int lerr = meta_back_init_one_conn( op, rs,
986 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
987 i == mc->mc_authz_target,
989 if ( lerr == LDAP_SUCCESS ) {
990 candidates[ i ].sr_tag = META_CANDIDATE;
991 candidates[ i ].sr_err = LDAP_SUCCESS;
994 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_init_one_conn(%d)\n",
995 op->o_log_prefix, i, 0 );
1000 * FIXME: in case one target cannot
1001 * be init'd, should the other ones
1005 ( void )meta_clear_one_candidate( msc );
1007 /* leave the target candidate, but record the error for later use */
1008 candidates[ i ].sr_err = lerr;
1011 Debug( LDAP_DEBUG_ANY, "%s: meta_back_init_one_conn(%d) failed: %d\n",
1012 op->o_log_prefix, i, lerr );
1019 ( void )meta_clear_one_candidate( msc );
1021 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1025 if ( ncandidates == 0 ) {
1027 meta_back_freeconn( op, mc );
1030 meta_back_release_conn( op, mc );
1033 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1034 rs->sr_text = "Unable to select valid candidates";
1036 if ( sendok & LDAP_BACK_SENDERR ) {
1037 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1038 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1040 send_ldap_result( op, rs );
1042 rs->sr_matched = NULL;
1050 /* clear out meta_back_init_one_conn non-fatal errors */
1051 rs->sr_err = LDAP_SUCCESS;
1057 * Inserts the newly created metaconn in the avl tree
1059 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
1060 err = avl_insert( &mi->mi_conntree, ( caddr_t )mc,
1061 meta_back_conn_cmp, meta_back_conn_dup );
1063 #if PRINT_CONNTREE > 0
1064 myprint( mi->mi_conntree );
1065 #endif /* PRINT_CONNTREE */
1067 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
1070 * Err could be -1 in case a duplicate metaconn is inserted
1072 * FIXME: what if the same client issues more than one
1073 * asynchronous operations?
1076 Debug( LDAP_DEBUG_ANY,
1077 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1078 op->o_log_prefix, ncandidates,
1079 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1081 rs->sr_err = LDAP_OTHER;
1082 rs->sr_text = "Internal server error";
1083 meta_back_freeconn( op, mc );
1084 if ( sendok & LDAP_BACK_SENDERR ) {
1085 send_ldap_result( op, rs );
1091 Debug( LDAP_DEBUG_TRACE,
1092 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1093 op->o_log_prefix, ncandidates,
1094 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1097 Debug( LDAP_DEBUG_TRACE,
1098 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1099 op->o_log_prefix, ncandidates,
1100 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1107 meta_back_release_conn(
1111 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1113 assert( mc != NULL );
1115 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
1116 assert( mc->mc_refcnt > 0 );
1118 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );