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_conndn_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 );
70 * meta_back_conndnmc_cmp
72 * compares two struct metaconn based on the value of the conn pointer,
73 * the local DN and the struct pointer; used by avl stuff
76 meta_back_conndnmc_cmp(
80 metaconn_t *mc1 = ( metaconn_t * )c1;
81 metaconn_t *mc2 = ( metaconn_t * )c2;
84 /* If local DNs don't match, it is definitely not a match */
85 /* For shared sessions, conn is NULL. Only explicitly
86 * bound sessions will have non-NULL conn.
88 rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
90 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
92 rc = SLAP_PTRCMP( mc1, mc2 );
102 * compares two struct metaconn based on the value of the conn pointer;
110 metaconn_t *mc1 = ( metaconn_t * )c1;
111 metaconn_t *mc2 = ( metaconn_t * )c2;
113 /* For shared sessions, conn is NULL. Only explicitly
114 * bound sessions will have non-NULL conn.
116 return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
120 * meta_back_conndn_dup
122 * returns -1 in case a duplicate struct metaconn has been inserted;
126 meta_back_conndn_dup(
130 metaconn_t *mc1 = ( metaconn_t * )c1;
131 metaconn_t *mc2 = ( metaconn_t * )c2;
133 /* Cannot have more than one shared session with same DN */
134 if ( mc1->mc_conn == mc2->mc_conn &&
135 dn_match( &mc1->mc_local_ndn, &mc2->mc_local_ndn ) )
144 * Debug stuff (got it from libavl)
146 #if PRINT_CONNTREE > 0
148 ravl_print( Avlnode *root, int depth )
151 metaconn_t *mc = (metaconn_t *)root->avl_data;
157 ravl_print( root->avl_right, depth + 1 );
159 for ( i = 0; i < depth; i++ ) {
163 printf( "c(%d%s%s) %d\n",
164 LDAP_BACK_PCONN_ID( mc->mc_conn ),
165 BER_BVISNULL( &mc->mc_local_ndn ) ? "" : ": ",
166 BER_BVISNULL( &mc->mc_local_ndn ) ? "" : mc->mc_local_ndn.bv_val,
169 ravl_print( root->avl_left, depth + 1 );
173 myprint( Avlnode *root )
175 printf( "********\n" );
178 printf( "\tNULL\n" );
180 ravl_print( root, 0 );
183 printf( "********\n" );
185 #endif /* PRINT_CONNTREE */
193 * Allocates a connection structure, making room for all the referenced targets
199 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
201 int i, ntargets = mi->mi_ntargets;
203 assert( ntargets > 0 );
205 /* malloc all in one */
206 mc = ( metaconn_t * )ch_malloc( sizeof( metaconn_t )
207 + sizeof( metasingleconn_t ) * ntargets );
212 for ( i = 0; i < ntargets; i++ ) {
213 mc->mc_conns[ i ].msc_ld = NULL;
214 BER_BVZERO( &mc->mc_conns[ i ].msc_bound_ndn );
215 BER_BVZERO( &mc->mc_conns[ i ].msc_cred );
216 mc->mc_conns[ i ].msc_mscflags = 0;
217 mc->mc_conns[ i ].msc_info = mi;
220 BER_BVZERO( &mc->mc_local_ndn );
221 mc->msc_mscflags = 0;
222 mc->mc_authz_target = META_BOUND_NONE;
233 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
235 assert( mc != NULL );
237 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
239 if ( --mc->mc_refcnt == 0 ) {
240 meta_back_conn_free( mc );
243 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
247 * meta_back_init_one_conn
249 * Initializes one connection
252 meta_back_init_one_conn(
259 ldap_back_send_t sendok )
261 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
262 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
265 int isauthz = ( candidate == mc->mc_authz_target );
270 if ( msc->msc_ld != NULL ) {
271 return rs->sr_err = LDAP_SUCCESS;
274 msc->msc_mscflags = 0;
277 * Attempts to initialize the connection to the target ds
279 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
280 if ( rs->sr_err != LDAP_SUCCESS ) {
285 * Set LDAP version. This will always succeed: If the client
286 * bound with a particular version, then so can we.
288 vers = op->o_conn->c_protocol;
289 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
291 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
292 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
293 LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
296 /* start TLS ("tls [try-]{start|propagate}" statement) */
297 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
298 && !ldap_is_ldaps_url( mt->mt_uri ) )
300 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
302 * use asynchronous StartTLS; in case, chase referral
303 * FIXME: OpenLDAP does not return referral on StartTLS yet
307 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
308 if ( rs->sr_err == LDAP_SUCCESS ) {
309 LDAPMessage *res = NULL;
310 int rc, nretries = mt->mt_nretries;
313 LDAP_BACK_TV_SET( &tv );
316 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
318 rs->sr_err = LDAP_OTHER;
320 } else if ( rc == 0 ) {
321 if ( nretries != 0 ) {
322 if ( nretries > 0 ) {
325 LDAP_BACK_TV_SET( &tv );
328 rs->sr_err = LDAP_OTHER;
330 } else if ( rc == LDAP_RES_EXTENDED ) {
331 struct berval *data = NULL;
333 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
335 if ( rs->sr_err == LDAP_SUCCESS ) {
338 rs->sr_err = ldap_parse_result( msc->msc_ld, res,
339 &err, NULL, NULL, NULL, NULL, 1 );
342 if ( rs->sr_err == LDAP_SUCCESS ) {
346 /* FIXME: in case a referral
347 * is returned, should we try
348 * using it instead of the
350 if ( rs->sr_err == LDAP_SUCCESS ) {
351 ldap_install_tls( msc->msc_ld );
353 } else if ( rs->sr_err == LDAP_REFERRAL ) {
354 rs->sr_err = LDAP_OTHER;
355 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
359 if ( data->bv_val ) {
360 ber_memfree( data->bv_val );
367 rs->sr_err = LDAP_OTHER;
374 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
376 * use synchronous StartTLS
378 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
379 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
381 /* if StartTLS is requested, only attempt it if the URL
382 * is not "ldaps://"; this may occur not only in case
383 * of misconfiguration, but also when used in the chain
384 * overlay, where the "uri" can be parsed out of a referral */
385 if ( rs->sr_err == LDAP_SERVER_DOWN
386 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
388 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
392 #endif /* HAVE_TLS */
395 * Set the network timeout if set
397 if ( mt->mt_network_timeout != 0 ) {
398 struct timeval network_timeout;
400 network_timeout.tv_usec = 0;
401 network_timeout.tv_sec = mt->mt_network_timeout;
403 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
404 (void *)&network_timeout );
408 * If the connection DN is not null, an attempt to rewrite it is made
412 if ( !BER_BVISNULL( &mt->mt_pseudorootdn ) ) {
413 ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
414 if ( !BER_BVISNULL( &mt->mt_pseudorootpw ) ) {
415 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
419 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
422 LDAP_BACK_CONN_ISPRIV_SET( msc );
425 BER_BVZERO( &msc->msc_cred );
426 BER_BVZERO( &msc->msc_bound_ndn );
427 if ( !BER_BVISEMPTY( &op->o_ndn )
428 && SLAP_IS_AUTHZ_BACKEND( op )
432 dc.conn = op->o_conn;
437 * Rewrite the bind dn if needed
439 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
440 &msc->msc_bound_ndn ) )
442 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
446 /* copy the DN idf needed */
447 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
448 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
452 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
456 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
459 if ( rs->sr_err == LDAP_SUCCESS ) {
461 * Sets a cookie for the rewrite session
463 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
466 rs->sr_err = slap_map_api2result( rs );
467 if ( sendok & LDAP_BACK_SENDERR ) {
468 send_ldap_result( op, rs );
479 * Retries one connection
487 ldap_back_send_t sendok )
489 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
490 metatarget_t *mt = &mi->mi_targets[ candidate ];
491 metaconn_t *mc = *mcp;
492 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
493 int rc = LDAP_UNAVAILABLE,
494 binding = LDAP_BACK_CONN_BINDING( msc );
496 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
498 assert( mc->mc_refcnt > 0 );
499 if ( mc->mc_refcnt == 1 ) {
500 char buf[ SLAP_TEXT_BUFLEN ];
502 snprintf( buf, sizeof( buf ),
503 "retrying URI=\"%s\" DN=\"%s\"",
505 BER_BVISNULL( &msc->msc_bound_ndn ) ?
506 "" : msc->msc_bound_ndn.bv_val );
507 Debug( LDAP_DEBUG_ANY,
508 "%s meta_back_retry[%d]: %s.\n",
509 op->o_log_prefix, candidate, buf );
511 meta_clear_one_candidate( msc );
512 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
514 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
516 /* mc here must be the regular mc, reset and ready for init */
517 rc = meta_back_init_one_conn( op, rs, mt, mc, candidate,
518 LDAP_BACK_CONN_ISPRIV( mc ), sendok );
520 LDAP_BACK_CONN_BINDING_SET( msc );
523 if ( rc == LDAP_SUCCESS ) {
524 rc = meta_back_single_dobind( op, rs, mcp, candidate,
525 sendok, mt->mt_nretries, 0 );
529 if ( rc != LDAP_SUCCESS ) {
530 if ( *mcp != NULL ) {
532 LDAP_BACK_CONN_BINDING_CLEAR( msc );
534 meta_back_release_conn_lock( op, mc, 1, 0 );
539 rs->sr_err = LDAP_UNAVAILABLE;
541 send_ldap_result( op, rs );
545 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
547 return rc == LDAP_SUCCESS ? 1 : 0;
551 * callback for unique candidate selection
554 meta_back_conn_cb( Operation *op, SlapReply *rs )
556 assert( op->o_tag == LDAP_REQ_SEARCH );
558 switch ( rs->sr_type ) {
560 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
576 meta_back_get_candidate(
581 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
585 * tries to get a unique candidate
586 * (takes care of default target)
588 candidate = meta_back_select_unique_candidate( mi, ndn );
591 * if any is found, inits the connection
593 if ( candidate == META_TARGET_NONE ) {
594 rs->sr_err = LDAP_NO_SUCH_OBJECT;
595 rs->sr_text = "no suitable candidate target found";
597 } else if ( candidate == META_TARGET_MULTIPLE ) {
600 SlapReply rs2 = { 0 };
601 slap_callback cb2 = { 0 };
604 /* try to get a unique match for the request ndn
605 * among the multiple candidates available */
606 op2.o_tag = LDAP_REQ_SEARCH;
608 op2.o_req_ndn = *ndn;
609 op2.ors_scope = LDAP_SCOPE_BASE;
610 op2.ors_deref = LDAP_DEREF_NEVER;
611 op2.ors_attrs = slap_anlist_no_attrs;
612 op2.ors_attrsonly = 0;
613 op2.ors_limit = NULL;
615 op2.ors_tlimit = SLAP_NO_LIMIT;
617 f.f_choice = LDAP_FILTER_PRESENT;
618 f.f_desc = slap_schema.si_ad_objectClass;
620 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
622 op2.o_callback = &cb2;
623 cb2.sc_response = meta_back_conn_cb;
624 cb2.sc_private = (void *)&candidate;
626 rc = op->o_bd->be_search( &op2, &rs2 );
628 switch ( rs2.sr_err ) {
631 rs->sr_err = rs2.sr_err;
634 case LDAP_SIZELIMIT_EXCEEDED:
635 /* if multiple candidates can serve the operation,
636 * and a default target is defined, and it is
637 * a candidate, try using it (FIXME: YMMV) */
638 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
639 && meta_back_is_candidate( &mi->mi_targets[ mi->mi_defaulttarget ].mt_nsuffix,
640 mi->mi_targets[ mi->mi_defaulttarget ].mt_scope,
641 mi->mi_targets[ mi->mi_defaulttarget ].mt_subtree_exclude,
642 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
644 candidate = mi->mi_defaulttarget;
645 rs->sr_err = LDAP_SUCCESS;
649 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
650 rs->sr_text = "cannot select unique candidate target";
656 rs->sr_err = LDAP_SUCCESS;
662 static void *meta_back_candidates_dummy;
665 meta_back_candidates_keyfree(
669 metacandidates_t *mc = (metacandidates_t *)data;
671 ber_memfree_x( mc->mc_candidates, NULL );
672 ber_memfree_x( data, NULL );
676 meta_back_candidates_get( Operation *op )
678 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
679 metacandidates_t *mc;
681 if ( op->o_threadctx ) {
684 ldap_pvt_thread_pool_getkey( op->o_threadctx,
685 &meta_back_candidates_dummy, &data, NULL );
686 mc = (metacandidates_t *)data;
689 mc = mi->mi_candidates;
693 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
694 mc->mc_ntargets = mi->mi_ntargets;
695 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
696 if ( op->o_threadctx ) {
700 ldap_pvt_thread_pool_setkey( op->o_threadctx,
701 &meta_back_candidates_dummy, data,
702 meta_back_candidates_keyfree );
705 mi->mi_candidates = mc;
708 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
709 /* NOTE: in the future, may want to allow back-config
710 * to add/remove targets from back-meta... */
711 mc->mc_ntargets = mi->mi_ntargets;
712 mc->mc_candidates = ch_realloc( mc->mc_candidates,
713 sizeof( SlapReply ) * mc->mc_ntargets );
716 return mc->mc_candidates;
722 * Prepares the connection structure
726 * - determine what DN is being requested:
728 * op requires candidate checks
730 * add unique parent of o_req_ndn
731 * bind unique^*[/all] o_req_ndn [no check]
732 * compare unique^+ o_req_ndn
733 * delete unique o_req_ndn
734 * modify unique o_req_ndn
735 * search any o_req_ndn
736 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
738 * - for ops that require the candidate to be unique, in case of multiple
739 * occurrences an internal search with sizeLimit=1 is performed
740 * if a unique candidate can actually be determined. If none is found,
741 * the operation aborts; if multiple are found, the default target
742 * is used if defined and candidate; otherwise the operation aborts.
744 * *^note: actually, the bind operation is handled much like a search;
745 * i.e. the bind is broadcast to all candidate targets.
747 * +^note: actually, the compare operation is handled much like a search;
748 * i.e. the compare is broadcast to all candidate targets, while checking
749 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
757 ldap_back_send_t sendok )
759 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
760 metaconn_t *mc = NULL,
762 int cached = META_TARGET_NONE,
763 i = META_TARGET_NONE,
769 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
773 META_DNTYPE_NEWPARENT
774 } dn_type = META_DNTYPE_ENTRY;
775 struct berval ndn = op->o_req_ndn,
778 SlapReply *candidates = meta_back_candidates_get( op );
780 /* Internal searches are privileged and shared. So is root. */
781 /* FIXME: there seem to be concurrency issues */
782 if ( op->o_do_not_cache || be_isroot( op ) ) {
783 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
784 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
785 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
788 mc_curr.mc_local_ndn = op->o_ndn;
790 /* Explicit binds must not be shared */
791 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
792 mc_curr.mc_conn = op->o_conn;
795 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
799 /* Explicit Bind requests always get their own conn */
800 if ( !( sendok & LDAP_BACK_BINDING ) ) {
801 /* Searches for a metaconn in the avl tree */
803 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
804 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
805 (caddr_t)&mc_curr, meta_back_conndn_cmp );
807 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
808 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
810 /* don't let anyone else use this expired connection */
811 (void)avl_delete( &mi->mi_conninfo.lai_tree,
812 (caddr_t)mc, meta_back_conndnmc_cmp );
814 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired.\n",
815 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
818 /* Don't reuse connections while they're still binding */
819 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
820 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
821 ldap_pvt_thread_yield();
827 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
830 switch ( op->o_tag ) {
832 /* if we go to selection, the entry must not exist,
833 * and we must be able to resolve the parent */
834 dn_type = META_DNTYPE_PARENT;
835 dnParent( &ndn, &pndn );
838 case LDAP_REQ_MODRDN:
839 /* if nnewSuperior is not NULL, it must resolve
840 * to the same candidate as the req_ndn */
841 if ( op->orr_nnewSup ) {
842 dn_type = META_DNTYPE_NEWPARENT;
847 /* if bound as rootdn, the backend must bind to all targets
848 * with the administrative identity */
849 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
850 op_type = META_OP_REQUIRE_ALL;
854 case LDAP_REQ_DELETE:
855 case LDAP_REQ_MODIFY:
856 /* just a unique candidate */
859 case LDAP_REQ_COMPARE:
860 case LDAP_REQ_SEARCH:
861 /* allow multiple candidates for the searchBase */
862 op_type = META_OP_ALLOW_MULTIPLE;
866 /* right now, just break (exop?) */
871 * require all connections ...
873 if ( op_type == META_OP_REQUIRE_ALL ) {
875 /* Looks like we didn't get a bind. Open a new session... */
877 mc = metaconn_alloc( op );
878 mc->mc_conn = mc_curr.mc_conn;
879 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
881 if ( sendok & LDAP_BACK_BINDING ) {
882 LDAP_BACK_CONN_BINDING_SET( mc );
886 for ( i = 0; i < mi->mi_ntargets; i++ ) {
887 metatarget_t *mt = &mi->mi_targets[ i ];
890 * The target is activated; if needed, it is
893 candidates[ i ].sr_err = meta_back_init_one_conn( op,
895 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
896 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
897 candidates[ i ].sr_tag = META_CANDIDATE;
903 * FIXME: in case one target cannot
904 * be init'd, should the other ones
907 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
908 err = candidates[ i ].sr_err;
913 if ( ncandidates == 0 ) {
915 meta_back_freeconn( op, mc );
918 meta_back_release_conn( op, mc );
921 rs->sr_err = LDAP_NO_SUCH_OBJECT;
922 rs->sr_text = "Unable to select valid candidates";
924 if ( sendok & LDAP_BACK_SENDERR ) {
925 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
926 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
928 send_ldap_result( op, rs );
930 rs->sr_matched = NULL;
940 * looks in cache, if any
942 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
943 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
946 if ( op_type == META_OP_REQUIRE_SINGLE ) {
947 metatarget_t *mt = NULL;
948 metasingleconn_t *msc = NULL;
952 for ( j = 0; j < mi->mi_ntargets; j++ ) {
953 candidates[ j ].sr_tag = META_NOT_CANDIDATE;
957 * tries to get a unique candidate
958 * (takes care of default target)
960 if ( i == META_TARGET_NONE ) {
961 i = meta_back_get_candidate( op, rs, &ndn );
963 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
964 i = meta_back_get_candidate( op, rs, &pndn );
967 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
969 meta_back_release_conn( op, mc );
972 if ( sendok & LDAP_BACK_SENDERR ) {
973 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
974 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
976 send_ldap_result( op, rs );
978 rs->sr_matched = NULL;
985 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
988 meta_back_release_conn( op, mc );
991 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
992 rs->sr_text = "cross-target rename not supported";
993 if ( sendok & LDAP_BACK_SENDERR ) {
994 send_ldap_result( op, rs );
1001 Debug( LDAP_DEBUG_TRACE,
1002 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1003 i, op->o_req_ndn.bv_val, 0 );
1006 /* Retries searching for a metaconn in the avl tree
1007 * the reason is that the connection might have been
1008 * created by meta_back_get_candidate() */
1009 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1011 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1012 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1013 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1015 /* Don't reuse connections while they're still binding */
1016 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1017 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1018 ldap_pvt_thread_yield();
1024 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1027 /* Looks like we didn't get a bind. Open a new session... */
1029 mc = metaconn_alloc( op );
1030 mc->mc_conn = mc_curr.mc_conn;
1031 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1033 if ( sendok & LDAP_BACK_BINDING ) {
1034 LDAP_BACK_CONN_BINDING_SET( mc );
1040 * Clear all other candidates
1042 ( void )meta_clear_unused_candidates( op, i );
1044 mt = &mi->mi_targets[ i ];
1045 msc = &mc->mc_conns[ i ];
1048 * The target is activated; if needed, it is
1049 * also init'd. In case of error, meta_back_init_one_conn
1050 * sends the appropriate result.
1052 err = meta_back_init_one_conn( op, rs, mt, mc, i,
1053 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
1054 if ( err != LDAP_SUCCESS ) {
1056 * FIXME: in case one target cannot
1057 * be init'd, should the other ones
1060 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1062 (void)meta_clear_one_candidate( msc );
1063 meta_back_freeconn( op, mc );
1066 meta_back_release_conn( op, mc );
1071 candidates[ i ].sr_err = LDAP_SUCCESS;
1072 candidates[ i ].sr_tag = META_CANDIDATE;
1080 * if no unique candidate ...
1084 /* Looks like we didn't get a bind. Open a new session... */
1086 mc = metaconn_alloc( op );
1087 mc->mc_conn = mc_curr.mc_conn;
1088 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1090 if ( sendok & LDAP_BACK_BINDING ) {
1091 LDAP_BACK_CONN_BINDING_SET( mc );
1095 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1096 metatarget_t *mt = &mi->mi_targets[ i ];
1097 metasingleconn_t *msc = &mc->mc_conns[ i ];
1100 || meta_back_is_candidate( &mt->mt_nsuffix,
1102 mt->mt_subtree_exclude,
1104 LDAP_SCOPE_SUBTREE ) )
1108 * The target is activated; if needed, it is
1111 int lerr = meta_back_init_one_conn( op, rs,
1113 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1115 if ( lerr == LDAP_SUCCESS ) {
1116 candidates[ i ].sr_tag = META_CANDIDATE;
1117 candidates[ i ].sr_err = LDAP_SUCCESS;
1120 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1121 op->o_log_prefix, i, 0 );
1126 * FIXME: in case one target cannot
1127 * be init'd, should the other ones
1131 ( void )meta_clear_one_candidate( msc );
1133 /* leave the target candidate, but record the error for later use */
1134 candidates[ i ].sr_err = lerr;
1137 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed: %d\n",
1138 op->o_log_prefix, i, lerr );
1145 ( void )meta_clear_one_candidate( msc );
1147 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1151 if ( ncandidates == 0 ) {
1153 meta_back_freeconn( op, mc );
1156 meta_back_release_conn( op, mc );
1159 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1160 rs->sr_text = "Unable to select valid candidates";
1162 if ( sendok & LDAP_BACK_SENDERR ) {
1163 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1164 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1166 send_ldap_result( op, rs );
1168 rs->sr_matched = NULL;
1176 /* clear out meta_back_init_one_conn non-fatal errors */
1177 rs->sr_err = LDAP_SUCCESS;
1180 /* touch the timestamp */
1181 if ( mi->mi_idle_timeout != 0 ) {
1182 mc->mc_time = op->o_time;
1187 if ( mi->mi_conn_ttl ) {
1188 mc->mc_create_time = op->o_time;
1192 * Inserts the newly created metaconn in the avl tree
1194 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1195 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1196 meta_back_conndn_cmp, meta_back_conndn_dup );
1198 #if PRINT_CONNTREE > 0
1199 myprint( mi->mi_conninfo.lai_tree );
1200 #endif /* PRINT_CONNTREE */
1202 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1205 * Err could be -1 in case a duplicate metaconn is inserted
1207 * FIXME: what if the same client issues more than one
1208 * asynchronous operations?
1211 Debug( LDAP_DEBUG_ANY,
1212 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1213 op->o_log_prefix, ncandidates,
1214 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1216 rs->sr_err = LDAP_OTHER;
1217 rs->sr_text = "Internal server error";
1218 meta_back_freeconn( op, mc );
1219 if ( sendok & LDAP_BACK_SENDERR ) {
1220 send_ldap_result( op, rs );
1226 Debug( LDAP_DEBUG_TRACE,
1227 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1228 op->o_log_prefix, ncandidates,
1229 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1232 Debug( LDAP_DEBUG_TRACE,
1233 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1234 op->o_log_prefix, ncandidates,
1235 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1242 meta_back_release_conn_lock(
1248 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1250 assert( mc != NULL );
1253 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1255 assert( mc->mc_refcnt > 0 );
1257 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1259 || ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
1260 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
1262 Debug( LDAP_DEBUG_TRACE, "%s meta_back_release_conn: mc=%p conn=%ld expired.\n",
1263 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1264 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1265 ( caddr_t )mc, meta_back_conndnmc_cmp );
1266 if ( mc->mc_refcnt == 0 ) {
1267 meta_clear_candidates( op, mc );
1268 meta_back_conn_free( mc );
1272 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );