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
43 * meta_back_dnconn_cmp
45 * compares two struct metaconn based on the value of the conn pointer
46 * and of the local DN; used by avl stuff
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 /* For shared sessions, conn is NULL. Only explicitly
59 * bound sessions will have non-NULL conn.
61 rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
63 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
72 * compares two struct metaconn based on the value of the conn pointer;
80 metaconn_t *mc1 = ( metaconn_t * )c1;
81 metaconn_t *mc2 = ( metaconn_t * )c2;
83 /* For shared sessions, conn is NULL. Only explicitly
84 * bound sessions will have non-NULL conn.
86 return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
90 * meta_back_dnconn_dup
92 * returns -1 in case a duplicate struct metaconn has been inserted;
100 metaconn_t *mc1 = ( metaconn_t * )c1;
101 metaconn_t *mc2 = ( metaconn_t * )c2;
103 /* Cannot have more than one shared session with same DN */
104 if ( mc1->mc_conn == mc2->mc_conn &&
105 dn_match( &mc1->mc_local_ndn, &mc2->mc_local_ndn ) )
114 * Debug stuff (got it from libavl)
116 #if PRINT_CONNTREE > 0
118 ravl_print( Avlnode *root, int depth )
121 metaconn_t *mc = (metaconn_t *)root->avl_data;
127 ravl_print( root->avl_right, depth + 1 );
129 for ( i = 0; i < depth; i++ ) {
133 printf( "c(%d%s%s) %d\n",
134 LDAP_BACK_PCONN_ID( mc->mc_conn ),
135 BER_BVISNULL( &mc->mc_local_ndn ) ? "" : ": ",
136 BER_BVISNULL( &mc->mc_local_ndn ) ? "" : mc->mc_local_ndn.bv_val,
139 ravl_print( root->avl_left, depth + 1 );
143 myprint( Avlnode *root )
145 printf( "********\n" );
148 printf( "\tNULL\n" );
150 ravl_print( root, 0 );
153 printf( "********\n" );
155 #endif /* PRINT_CONNTREE */
163 * Allocates a connection structure, making room for all the referenced targets
169 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
171 int i, ntargets = mi->mi_ntargets;
173 assert( ntargets > 0 );
175 /* malloc all in one */
176 mc = ( metaconn_t * )ch_malloc( sizeof( metaconn_t )
177 + sizeof( metasingleconn_t ) * ntargets );
182 for ( i = 0; i < ntargets; i++ ) {
183 mc->mc_conns[ i ].msc_ld = NULL;
184 BER_BVZERO( &mc->mc_conns[ i ].msc_bound_ndn );
185 BER_BVZERO( &mc->mc_conns[ i ].msc_cred );
186 LDAP_BACK_CONN_ISBOUND_CLEAR( &mc->mc_conns[ i ] );
187 mc->mc_conns[ i ].msc_info = mi;
190 BER_BVZERO( &mc->mc_local_ndn );
191 mc->msc_mscflags = 0;
192 mc->mc_authz_target = META_BOUND_NONE;
193 ldap_pvt_thread_mutex_init( &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_conninfo.lai_mutex );
211 if ( --mc->mc_refcnt == 0 ) {
212 meta_back_conn_free( mc );
215 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
219 * meta_back_init_one_conn
221 * Initializes one connection
224 meta_back_init_one_conn(
231 ldap_back_send_t sendok )
233 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
234 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
237 int isauthz = ( candidate == mc->mc_authz_target );
242 if ( msc->msc_ld != NULL ) {
245 if ( ( mt->mt_idle_timeout != 0 && op->o_time > msc->msc_time + mt->mt_idle_timeout )
246 || ( mt->mt_conn_ttl != 0 && op->o_time > msc->msc_create_time + mt->mt_conn_ttl ) )
248 Debug( LDAP_DEBUG_TRACE,
249 "%s meta_back_init_one_conn[%d]: idle timeout/ttl.\n",
250 op->o_log_prefix, candidate, 0 );
251 if ( meta_back_retry( op, rs, mc, candidate, sendok ) ) {
258 if ( mt->mt_idle_timeout != 0 ) {
259 msc->msc_time = op->o_time;
263 return rs->sr_err = LDAP_SUCCESS;
268 * Attempts to initialize the connection to the target ds
270 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
271 if ( rs->sr_err != LDAP_SUCCESS ) {
276 * Set LDAP version. This will always succeed: If the client
277 * bound with a particular version, then so can we.
279 vers = op->o_conn->c_protocol;
280 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
282 /* automatically chase referrals ("chase-referrals"/"dont-chase-referrals" statement) */
283 if ( LDAP_BACK_CHASE_REFERRALS( mi ) ) {
284 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
288 /* start TLS ("tls [try-]{start|propagate}" statement) */
289 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
290 && !ldap_is_ldaps_url( mt->mt_uri ) )
292 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
294 * use asynchronous StartTLS; in case, chase referral
295 * FIXME: OpenLDAP does not return referral on StartTLS yet
299 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
300 if ( rs->sr_err == LDAP_SUCCESS ) {
301 LDAPMessage *res = NULL;
302 int rc, nretries = mt->mt_nretries;
305 LDAP_BACK_TV_SET( &tv );
308 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
310 rs->sr_err = LDAP_OTHER;
312 } else if ( rc == 0 ) {
313 if ( nretries != 0 ) {
314 if ( nretries > 0 ) {
317 LDAP_BACK_TV_SET( &tv );
320 rs->sr_err = LDAP_OTHER;
322 } else if ( rc == LDAP_RES_EXTENDED ) {
323 struct berval *data = NULL;
325 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
327 if ( rs->sr_err == LDAP_SUCCESS ) {
330 rs->sr_err = ldap_parse_result( msc->msc_ld, res,
331 &err, NULL, NULL, NULL, NULL, 1 );
334 if ( rs->sr_err == LDAP_SUCCESS ) {
338 /* FIXME: in case a referral
339 * is returned, should we try
340 * using it instead of the
342 if ( rs->sr_err == LDAP_SUCCESS ) {
343 ldap_install_tls( msc->msc_ld );
345 } else if ( rs->sr_err == LDAP_REFERRAL ) {
346 rs->sr_err = LDAP_OTHER;
347 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
351 if ( data->bv_val ) {
352 ber_memfree( data->bv_val );
359 rs->sr_err = LDAP_OTHER;
366 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
368 * use synchronous StartTLS
370 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
371 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
373 /* if StartTLS is requested, only attempt it if the URL
374 * is not "ldaps://"; this may occur not only in case
375 * of misconfiguration, but also when used in the chain
376 * overlay, where the "uri" can be parsed out of a referral */
377 if ( rs->sr_err == LDAP_SERVER_DOWN
378 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
380 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
384 #endif /* HAVE_TLS */
387 * Set the network timeout if set
389 if ( mt->mt_network_timeout != 0 ) {
390 struct timeval network_timeout;
392 network_timeout.tv_usec = 0;
393 network_timeout.tv_sec = mt->mt_network_timeout;
395 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
396 (void *)&network_timeout );
400 * If the connection DN is not null, an attempt to rewrite it is made
404 if ( !BER_BVISNULL( &mt->mt_pseudorootdn ) ) {
405 ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
406 if ( !BER_BVISNULL( &mt->mt_pseudorootpw ) ) {
407 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
411 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
414 LDAP_BACK_CONN_ISPRIV_SET( msc );
417 BER_BVZERO( &msc->msc_cred );
418 BER_BVZERO( &msc->msc_bound_ndn );
419 if ( !BER_BVISEMPTY( &op->o_ndn )
420 && SLAP_IS_AUTHZ_BACKEND( op )
424 dc.conn = op->o_conn;
429 * Rewrite the bind dn if needed
431 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
432 &msc->msc_bound_ndn ) )
434 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
438 /* copy the DN idf needed */
439 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
440 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
444 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
448 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
450 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
453 if ( rs->sr_err == LDAP_SUCCESS ) {
455 * Sets a cookie for the rewrite session
457 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
459 if ( mt->mt_idle_timeout ) {
460 msc->msc_time = op->o_time;
463 if ( mt->mt_conn_ttl ) {
464 msc->msc_create_time = op->o_time;
468 rs->sr_err = slap_map_api2result( rs );
469 if ( sendok & LDAP_BACK_SENDERR ) {
470 send_ldap_result( op, rs );
479 * meta_back_retry_lock
481 * Retries one connection
484 meta_back_retry_lock(
489 ldap_back_send_t sendok,
492 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
493 metatarget_t *mt = &mi->mi_targets[ candidate ];
494 int rc = LDAP_UNAVAILABLE;
495 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
498 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
500 assert( mc->mc_refcnt > 0 );
502 if ( mc->mc_refcnt == 1 ) {
503 char buf[ SLAP_TEXT_BUFLEN ];
505 while ( dolock && ldap_pvt_thread_mutex_trylock( &mc->mc_mutex ) ) {
506 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
507 ldap_pvt_thread_yield();
511 snprintf( buf, sizeof( buf ),
512 "retrying URI=\"%s\" DN=\"%s\"",
514 BER_BVISNULL( &msc->msc_bound_ndn ) ?
515 "" : msc->msc_bound_ndn.bv_val );
516 Debug( LDAP_DEBUG_ANY,
517 "%s meta_back_retry[%d]: %s.\n",
518 op->o_log_prefix, candidate, buf );
520 meta_clear_one_candidate( msc );
521 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
523 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
525 /* mc here must be the regular mc, reset and ready for init */
526 rc = meta_back_init_one_conn( op, rs, mt, mc, candidate,
527 LDAP_BACK_CONN_ISPRIV( mc ), sendok );
529 if ( rc == LDAP_SUCCESS ) {
530 if ( be_isroot( op ) && !BER_BVISNULL( &mi->mi_targets[ candidate ].mt_pseudorootdn ) )
534 op2.o_tag = LDAP_REQ_BIND;
535 op2.o_req_dn = mi->mi_targets[ candidate ].mt_pseudorootdn;
536 op2.o_req_ndn = mi->mi_targets[ candidate ].mt_pseudorootdn;
537 op2.orb_cred = mi->mi_targets[ candidate ].mt_pseudorootpw;
538 op2.orb_method = LDAP_AUTH_SIMPLE;
540 rc = meta_back_single_bind( &op2, rs, mc, candidate, 0 );
543 rc = meta_back_single_dobind( op, rs, mc, candidate,
544 sendok, mt->mt_nretries, 0 );
549 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
553 if ( rc != LDAP_SUCCESS ) {
557 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
559 return rc == LDAP_SUCCESS ? 1 : 0;
563 * callback for unique candidate selection
566 meta_back_conn_cb( Operation *op, SlapReply *rs )
568 assert( op->o_tag == LDAP_REQ_SEARCH );
570 switch ( rs->sr_type ) {
572 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
588 meta_back_get_candidate(
593 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
597 * tries to get a unique candidate
598 * (takes care of default target)
600 candidate = meta_back_select_unique_candidate( mi, ndn );
603 * if any is found, inits the connection
605 if ( candidate == META_TARGET_NONE ) {
606 rs->sr_err = LDAP_NO_SUCH_OBJECT;
607 rs->sr_text = "no suitable candidate target found";
609 } else if ( candidate == META_TARGET_MULTIPLE ) {
612 SlapReply rs2 = { 0 };
613 slap_callback cb2 = { 0 };
616 /* try to get a unique match for the request ndn
617 * among the multiple candidates available */
618 op2.o_tag = LDAP_REQ_SEARCH;
620 op2.o_req_ndn = *ndn;
621 op2.ors_scope = LDAP_SCOPE_BASE;
622 op2.ors_deref = LDAP_DEREF_NEVER;
623 op2.ors_attrs = slap_anlist_no_attrs;
624 op2.ors_attrsonly = 0;
625 op2.ors_limit = NULL;
627 op2.ors_tlimit = SLAP_NO_LIMIT;
629 f.f_choice = LDAP_FILTER_PRESENT;
630 f.f_desc = slap_schema.si_ad_objectClass;
632 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
634 op2.o_callback = &cb2;
635 cb2.sc_response = meta_back_conn_cb;
636 cb2.sc_private = (void *)&candidate;
638 rc = op->o_bd->be_search( &op2, &rs2 );
640 switch ( rs2.sr_err ) {
643 rs->sr_err = rs2.sr_err;
646 case LDAP_SIZELIMIT_EXCEEDED:
647 /* if multiple candidates can serve the operation,
648 * and a default target is defined, and it is
649 * a candidate, try using it (FIXME: YMMV) */
650 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
651 && meta_back_is_candidate( &mi->mi_targets[ mi->mi_defaulttarget ].mt_nsuffix,
652 mi->mi_targets[ mi->mi_defaulttarget ].mt_scope,
653 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
655 candidate = mi->mi_defaulttarget;
656 rs->sr_err = LDAP_SUCCESS;
660 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
661 rs->sr_text = "cannot select unique candidate target";
667 rs->sr_err = LDAP_SUCCESS;
673 static void *meta_back_candidates_dummy;
676 meta_back_candidates_keyfree(
680 metacandidates_t *mc = (metacandidates_t *)data;
682 ber_memfree_x( mc->mc_candidates, NULL );
683 ber_memfree_x( data, NULL );
687 meta_back_candidates_get( Operation *op )
689 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
690 metacandidates_t *mc;
692 if ( op->o_threadctx ) {
695 ldap_pvt_thread_pool_getkey( op->o_threadctx,
696 &meta_back_candidates_dummy, &data, NULL );
697 mc = (metacandidates_t *)data;
700 mc = mi->mi_candidates;
704 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
705 mc->mc_ntargets = mi->mi_ntargets;
706 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
707 if ( op->o_threadctx ) {
711 ldap_pvt_thread_pool_setkey( op->o_threadctx,
712 &meta_back_candidates_dummy, data,
713 meta_back_candidates_keyfree );
716 mi->mi_candidates = mc;
719 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
720 /* NOTE: in the future, may want to allow back-config
721 * to add/remove targets from back-meta... */
722 mc->mc_ntargets = mi->mi_ntargets;
723 mc->mc_candidates = ch_realloc( mc->mc_candidates,
724 sizeof( SlapReply ) * mc->mc_ntargets );
727 return mc->mc_candidates;
733 * Prepares the connection structure
737 * - determine what DN is being requested:
739 * op requires candidate checks
741 * add unique parent of o_req_ndn
742 * bind unique^*[/all] o_req_ndn [no check]
743 * compare unique^+ o_req_ndn
744 * delete unique o_req_ndn
745 * modify unique o_req_ndn
746 * search any o_req_ndn
747 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
749 * - for ops that require the candidate to be unique, in case of multiple
750 * occurrences an internal search with sizeLimit=1 is performed
751 * if a unique candidate can actually be determined. If none is found,
752 * the operation aborts; if multiple are found, the default target
753 * is used if defined and candidate; otherwise the operation aborts.
755 * *^note: actually, the bind operation is handled much like a search;
756 * i.e. the bind is broadcast to all candidate targets.
758 * +^note: actually, the compare operation is handled much like a search;
759 * i.e. the compare is broadcast to all candidate targets, while checking
760 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
768 ldap_back_send_t sendok )
770 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
771 metaconn_t *mc = NULL,
773 int cached = META_TARGET_NONE,
774 i = META_TARGET_NONE,
780 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
783 struct berval ndn = op->o_req_ndn,
786 SlapReply *candidates = meta_back_candidates_get( op );
788 /* Internal searches are privileged and shared. So is root. */
789 /* FIXME: there seem to be concurrency issues */
790 if ( op->o_do_not_cache || be_isroot( op ) ) {
791 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
792 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
793 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
796 mc_curr.mc_local_ndn = op->o_ndn;
798 /* Explicit binds must not be shared */
799 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
800 mc_curr.mc_conn = op->o_conn;
803 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
807 /* Explicit Bind requests always get their own conn */
808 if ( !( sendok & LDAP_BACK_BINDING ) ) {
809 /* Searches for a metaconn in the avl tree */
811 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
812 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
813 (caddr_t)&mc_curr, meta_back_dnconn_cmp );
815 if ( mc->mc_tainted ) {
816 rs->sr_err = LDAP_UNAVAILABLE;
817 rs->sr_text = "remote server unavailable";
818 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
822 /* Don't reuse connections while they're still binding */
823 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
824 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
825 ldap_pvt_thread_yield();
830 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
833 switch ( op->o_tag ) {
835 /* if we go to selection, the entry must not exist,
836 * and we must be able to resolve the parent */
838 dnParent( &ndn, &pndn );
841 case LDAP_REQ_MODRDN:
842 /* if nnewSuperior is not NULL, it must resolve
843 * to the same candidate as the req_ndn */
844 if ( op->orr_nnewSup ) {
850 /* if bound as rootdn, the backend must bind to all targets
851 * with the administrative identity */
852 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
853 op_type = META_OP_REQUIRE_ALL;
857 case LDAP_REQ_DELETE:
858 case LDAP_REQ_MODIFY:
859 /* just a unique candidate */
862 case LDAP_REQ_COMPARE:
863 case LDAP_REQ_SEARCH:
864 /* allow multiple candidates for the searchBase */
865 op_type = META_OP_ALLOW_MULTIPLE;
869 /* right now, just break (exop?) */
874 * require all connections ...
876 if ( op_type == META_OP_REQUIRE_ALL ) {
878 /* Looks like we didn't get a bind. Open a new session... */
880 mc = metaconn_alloc( op );
881 mc->mc_conn = mc_curr.mc_conn;
882 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
884 if ( sendok & LDAP_BACK_BINDING ) {
885 LDAP_BACK_CONN_BINDING_SET( mc );
889 for ( i = 0; i < mi->mi_ntargets; i++ ) {
890 metatarget_t *mt = &mi->mi_targets[ i ];
893 * The target is activated; if needed, it is
896 candidates[ i ].sr_err = meta_back_init_one_conn( op,
898 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
899 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
900 candidates[ i ].sr_tag = META_CANDIDATE;
906 * FIXME: in case one target cannot
907 * be init'd, should the other ones
910 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
911 err = candidates[ i ].sr_err;
916 if ( ncandidates == 0 ) {
918 meta_back_freeconn( op, mc );
921 meta_back_release_conn( op, mc );
924 rs->sr_err = LDAP_NO_SUCH_OBJECT;
925 rs->sr_text = "Unable to select valid candidates";
927 if ( sendok & LDAP_BACK_SENDERR ) {
928 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
929 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
931 send_ldap_result( op, rs );
933 rs->sr_matched = NULL;
943 * looks in cache, if any
945 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
946 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
949 if ( op_type == META_OP_REQUIRE_SINGLE ) {
950 metatarget_t *mt = NULL;
951 metasingleconn_t *msc = NULL;
955 for ( j = 0; j < mi->mi_ntargets; j++ ) {
956 candidates[ j ].sr_tag = META_NOT_CANDIDATE;
960 * tries to get a unique candidate
961 * (takes care of default target)
963 if ( i == META_TARGET_NONE ) {
964 i = meta_back_get_candidate( op, rs, &ndn );
966 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && parent ) {
967 i = meta_back_get_candidate( op, rs, &pndn );
970 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
972 meta_back_release_conn( op, mc );
975 if ( sendok & LDAP_BACK_SENDERR ) {
976 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
977 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
979 send_ldap_result( op, rs );
981 rs->sr_matched = NULL;
988 if ( newparent && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
991 meta_back_release_conn( op, mc );
994 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
995 rs->sr_text = "cross-target rename not supported";
996 if ( sendok & LDAP_BACK_SENDERR ) {
997 send_ldap_result( op, rs );
1004 Debug( LDAP_DEBUG_TRACE,
1005 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1006 i, op->o_req_ndn.bv_val, 0 );
1009 /* Retries searching for a metaconn in the avl tree
1010 * the reason is that the connection might have been
1011 * created by meta_back_get_candidate() */
1012 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1013 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1014 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1015 (caddr_t)&mc_curr, meta_back_dnconn_cmp );
1019 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1022 /* Looks like we didn't get a bind. Open a new session... */
1024 mc = metaconn_alloc( op );
1025 mc->mc_conn = mc_curr.mc_conn;
1026 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1028 if ( sendok & LDAP_BACK_BINDING ) {
1029 LDAP_BACK_CONN_BINDING_SET( mc );
1035 * Clear all other candidates
1037 ( void )meta_clear_unused_candidates( op, i );
1039 mt = &mi->mi_targets[ i ];
1040 msc = &mc->mc_conns[ i ];
1043 * The target is activated; if needed, it is
1044 * also init'd. In case of error, meta_back_init_one_conn
1045 * sends the appropriate result.
1047 err = meta_back_init_one_conn( op, rs, mt, mc, i,
1048 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
1049 if ( err != LDAP_SUCCESS ) {
1051 * FIXME: in case one target cannot
1052 * be init'd, should the other ones
1055 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1057 (void)meta_clear_one_candidate( msc );
1058 meta_back_freeconn( op, mc );
1061 meta_back_release_conn( op, mc );
1066 candidates[ i ].sr_err = LDAP_SUCCESS;
1067 candidates[ i ].sr_tag = META_CANDIDATE;
1075 * if no unique candidate ...
1079 /* Looks like we didn't get a bind. Open a new session... */
1081 mc = metaconn_alloc( op );
1082 mc->mc_conn = mc_curr.mc_conn;
1083 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1085 if ( sendok & LDAP_BACK_BINDING ) {
1086 LDAP_BACK_CONN_BINDING_SET( mc );
1090 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1091 metatarget_t *mt = &mi->mi_targets[ i ];
1092 metasingleconn_t *msc = &mc->mc_conns[ i ];
1095 || meta_back_is_candidate( &mt->mt_nsuffix,
1098 LDAP_SCOPE_SUBTREE ) )
1102 * The target is activated; if needed, it is
1105 int lerr = meta_back_init_one_conn( op, rs,
1107 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1109 if ( lerr == LDAP_SUCCESS ) {
1110 candidates[ i ].sr_tag = META_CANDIDATE;
1111 candidates[ i ].sr_err = LDAP_SUCCESS;
1114 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1115 op->o_log_prefix, i, 0 );
1120 * FIXME: in case one target cannot
1121 * be init'd, should the other ones
1125 ( void )meta_clear_one_candidate( msc );
1127 /* leave the target candidate, but record the error for later use */
1128 candidates[ i ].sr_err = lerr;
1131 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed: %d\n",
1132 op->o_log_prefix, i, lerr );
1139 ( void )meta_clear_one_candidate( msc );
1141 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1145 if ( ncandidates == 0 ) {
1147 meta_back_freeconn( op, mc );
1150 meta_back_release_conn( op, mc );
1153 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1154 rs->sr_text = "Unable to select valid candidates";
1156 if ( sendok & LDAP_BACK_SENDERR ) {
1157 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1158 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1160 send_ldap_result( op, rs );
1162 rs->sr_matched = NULL;
1170 /* clear out meta_back_init_one_conn non-fatal errors */
1171 rs->sr_err = LDAP_SUCCESS;
1177 * Inserts the newly created metaconn in the avl tree
1179 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1180 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1181 meta_back_dnconn_cmp, meta_back_dnconn_dup );
1183 #if PRINT_CONNTREE > 0
1184 myprint( mi->mi_conninfo.lai_tree );
1185 #endif /* PRINT_CONNTREE */
1187 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1190 * Err could be -1 in case a duplicate metaconn is inserted
1192 * FIXME: what if the same client issues more than one
1193 * asynchronous operations?
1196 Debug( LDAP_DEBUG_ANY,
1197 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1198 op->o_log_prefix, ncandidates,
1199 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1201 rs->sr_err = LDAP_OTHER;
1202 rs->sr_text = "Internal server error";
1203 meta_back_freeconn( op, mc );
1204 if ( sendok & LDAP_BACK_SENDERR ) {
1205 send_ldap_result( op, rs );
1211 Debug( LDAP_DEBUG_TRACE,
1212 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1213 op->o_log_prefix, ncandidates,
1214 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1217 Debug( LDAP_DEBUG_TRACE,
1218 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1219 op->o_log_prefix, ncandidates,
1220 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1227 meta_back_release_conn(
1231 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1233 assert( mc != NULL );
1235 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1236 assert( mc->mc_refcnt > 0 );
1238 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1239 if ( mc->mc_refcnt == 0 && mc->mc_tainted ) {
1240 (void)avl_delete( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1241 meta_back_dnconn_cmp );
1242 meta_back_conn_free( mc );
1244 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );