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 LDAP_BACK_CONN_ISBOUND_CLEAR( &mc->mc_conns[ i ] );
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;
275 * Attempts to initialize the connection to the target ds
277 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
278 if ( rs->sr_err != LDAP_SUCCESS ) {
283 * Set LDAP version. This will always succeed: If the client
284 * bound with a particular version, then so can we.
286 vers = op->o_conn->c_protocol;
287 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
289 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
290 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
291 LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
294 /* start TLS ("tls [try-]{start|propagate}" statement) */
295 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
296 && !ldap_is_ldaps_url( mt->mt_uri ) )
298 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
300 * use asynchronous StartTLS; in case, chase referral
301 * FIXME: OpenLDAP does not return referral on StartTLS yet
305 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
306 if ( rs->sr_err == LDAP_SUCCESS ) {
307 LDAPMessage *res = NULL;
308 int rc, nretries = mt->mt_nretries;
311 LDAP_BACK_TV_SET( &tv );
314 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
316 rs->sr_err = LDAP_OTHER;
318 } else if ( rc == 0 ) {
319 if ( nretries != 0 ) {
320 if ( nretries > 0 ) {
323 LDAP_BACK_TV_SET( &tv );
326 rs->sr_err = LDAP_OTHER;
328 } else if ( rc == LDAP_RES_EXTENDED ) {
329 struct berval *data = NULL;
331 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
333 if ( rs->sr_err == LDAP_SUCCESS ) {
336 rs->sr_err = ldap_parse_result( msc->msc_ld, res,
337 &err, NULL, NULL, NULL, NULL, 1 );
340 if ( rs->sr_err == LDAP_SUCCESS ) {
344 /* FIXME: in case a referral
345 * is returned, should we try
346 * using it instead of the
348 if ( rs->sr_err == LDAP_SUCCESS ) {
349 ldap_install_tls( msc->msc_ld );
351 } else if ( rs->sr_err == LDAP_REFERRAL ) {
352 rs->sr_err = LDAP_OTHER;
353 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
357 if ( data->bv_val ) {
358 ber_memfree( data->bv_val );
365 rs->sr_err = LDAP_OTHER;
372 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
374 * use synchronous StartTLS
376 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
377 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
379 /* if StartTLS is requested, only attempt it if the URL
380 * is not "ldaps://"; this may occur not only in case
381 * of misconfiguration, but also when used in the chain
382 * overlay, where the "uri" can be parsed out of a referral */
383 if ( rs->sr_err == LDAP_SERVER_DOWN
384 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
386 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
390 #endif /* HAVE_TLS */
393 * Set the network timeout if set
395 if ( mt->mt_network_timeout != 0 ) {
396 struct timeval network_timeout;
398 network_timeout.tv_usec = 0;
399 network_timeout.tv_sec = mt->mt_network_timeout;
401 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
402 (void *)&network_timeout );
406 * If the connection DN is not null, an attempt to rewrite it is made
410 if ( !BER_BVISNULL( &mt->mt_pseudorootdn ) ) {
411 ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
412 if ( !BER_BVISNULL( &mt->mt_pseudorootpw ) ) {
413 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
417 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
420 LDAP_BACK_CONN_ISPRIV_SET( msc );
423 BER_BVZERO( &msc->msc_cred );
424 BER_BVZERO( &msc->msc_bound_ndn );
425 if ( !BER_BVISEMPTY( &op->o_ndn )
426 && SLAP_IS_AUTHZ_BACKEND( op )
430 dc.conn = op->o_conn;
435 * Rewrite the bind dn if needed
437 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
438 &msc->msc_bound_ndn ) )
440 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
444 /* copy the DN idf needed */
445 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
446 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
450 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
454 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
456 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
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 int rc = LDAP_UNAVAILABLE;
492 metaconn_t *mc = *mcp;
493 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
495 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
497 assert( mc->mc_refcnt > 0 );
498 if ( mc->mc_refcnt == 1 ) {
499 char buf[ SLAP_TEXT_BUFLEN ];
501 snprintf( buf, sizeof( buf ),
502 "retrying URI=\"%s\" DN=\"%s\"",
504 BER_BVISNULL( &msc->msc_bound_ndn ) ?
505 "" : msc->msc_bound_ndn.bv_val );
506 Debug( LDAP_DEBUG_ANY,
507 "%s meta_back_retry[%d]: %s.\n",
508 op->o_log_prefix, candidate, buf );
510 meta_clear_one_candidate( msc );
511 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
513 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
515 /* mc here must be the regular mc, reset and ready for init */
516 rc = meta_back_init_one_conn( op, rs, mt, mc, candidate,
517 LDAP_BACK_CONN_ISPRIV( mc ), sendok );
519 if ( rc == LDAP_SUCCESS ) {
520 rc = meta_back_single_dobind( op, rs, mcp, candidate,
521 sendok, mt->mt_nretries, 0 );
525 if ( rc != LDAP_SUCCESS ) {
526 meta_back_release_conn_lock( op, mc, 1, 0 );
530 rs->sr_err = LDAP_UNAVAILABLE;
531 rs->sr_text = "unable to retry";
532 send_ldap_result( op, rs );
536 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
538 return rc == LDAP_SUCCESS ? 1 : 0;
542 * callback for unique candidate selection
545 meta_back_conn_cb( Operation *op, SlapReply *rs )
547 assert( op->o_tag == LDAP_REQ_SEARCH );
549 switch ( rs->sr_type ) {
551 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
567 meta_back_get_candidate(
572 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
576 * tries to get a unique candidate
577 * (takes care of default target)
579 candidate = meta_back_select_unique_candidate( mi, ndn );
582 * if any is found, inits the connection
584 if ( candidate == META_TARGET_NONE ) {
585 rs->sr_err = LDAP_NO_SUCH_OBJECT;
586 rs->sr_text = "no suitable candidate target found";
588 } else if ( candidate == META_TARGET_MULTIPLE ) {
591 SlapReply rs2 = { 0 };
592 slap_callback cb2 = { 0 };
595 /* try to get a unique match for the request ndn
596 * among the multiple candidates available */
597 op2.o_tag = LDAP_REQ_SEARCH;
599 op2.o_req_ndn = *ndn;
600 op2.ors_scope = LDAP_SCOPE_BASE;
601 op2.ors_deref = LDAP_DEREF_NEVER;
602 op2.ors_attrs = slap_anlist_no_attrs;
603 op2.ors_attrsonly = 0;
604 op2.ors_limit = NULL;
606 op2.ors_tlimit = SLAP_NO_LIMIT;
608 f.f_choice = LDAP_FILTER_PRESENT;
609 f.f_desc = slap_schema.si_ad_objectClass;
611 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
613 op2.o_callback = &cb2;
614 cb2.sc_response = meta_back_conn_cb;
615 cb2.sc_private = (void *)&candidate;
617 rc = op->o_bd->be_search( &op2, &rs2 );
619 switch ( rs2.sr_err ) {
622 rs->sr_err = rs2.sr_err;
625 case LDAP_SIZELIMIT_EXCEEDED:
626 /* if multiple candidates can serve the operation,
627 * and a default target is defined, and it is
628 * a candidate, try using it (FIXME: YMMV) */
629 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
630 && meta_back_is_candidate( &mi->mi_targets[ mi->mi_defaulttarget ].mt_nsuffix,
631 mi->mi_targets[ mi->mi_defaulttarget ].mt_scope,
632 mi->mi_targets[ mi->mi_defaulttarget ].mt_subtree_exclude,
633 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
635 candidate = mi->mi_defaulttarget;
636 rs->sr_err = LDAP_SUCCESS;
640 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
641 rs->sr_text = "cannot select unique candidate target";
647 rs->sr_err = LDAP_SUCCESS;
653 static void *meta_back_candidates_dummy;
656 meta_back_candidates_keyfree(
660 metacandidates_t *mc = (metacandidates_t *)data;
662 ber_memfree_x( mc->mc_candidates, NULL );
663 ber_memfree_x( data, NULL );
667 meta_back_candidates_get( Operation *op )
669 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
670 metacandidates_t *mc;
672 if ( op->o_threadctx ) {
675 ldap_pvt_thread_pool_getkey( op->o_threadctx,
676 &meta_back_candidates_dummy, &data, NULL );
677 mc = (metacandidates_t *)data;
680 mc = mi->mi_candidates;
684 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
685 mc->mc_ntargets = mi->mi_ntargets;
686 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
687 if ( op->o_threadctx ) {
691 ldap_pvt_thread_pool_setkey( op->o_threadctx,
692 &meta_back_candidates_dummy, data,
693 meta_back_candidates_keyfree );
696 mi->mi_candidates = mc;
699 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
700 /* NOTE: in the future, may want to allow back-config
701 * to add/remove targets from back-meta... */
702 mc->mc_ntargets = mi->mi_ntargets;
703 mc->mc_candidates = ch_realloc( mc->mc_candidates,
704 sizeof( SlapReply ) * mc->mc_ntargets );
707 return mc->mc_candidates;
713 * Prepares the connection structure
717 * - determine what DN is being requested:
719 * op requires candidate checks
721 * add unique parent of o_req_ndn
722 * bind unique^*[/all] o_req_ndn [no check]
723 * compare unique^+ o_req_ndn
724 * delete unique o_req_ndn
725 * modify unique o_req_ndn
726 * search any o_req_ndn
727 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
729 * - for ops that require the candidate to be unique, in case of multiple
730 * occurrences an internal search with sizeLimit=1 is performed
731 * if a unique candidate can actually be determined. If none is found,
732 * the operation aborts; if multiple are found, the default target
733 * is used if defined and candidate; otherwise the operation aborts.
735 * *^note: actually, the bind operation is handled much like a search;
736 * i.e. the bind is broadcast to all candidate targets.
738 * +^note: actually, the compare operation is handled much like a search;
739 * i.e. the compare is broadcast to all candidate targets, while checking
740 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
748 ldap_back_send_t sendok )
750 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
751 metaconn_t *mc = NULL,
753 int cached = META_TARGET_NONE,
754 i = META_TARGET_NONE,
760 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
764 META_DNTYPE_NEWPARENT
765 } dn_type = META_DNTYPE_ENTRY;
766 struct berval ndn = op->o_req_ndn,
769 SlapReply *candidates = meta_back_candidates_get( op );
771 /* Internal searches are privileged and shared. So is root. */
772 /* FIXME: there seem to be concurrency issues */
773 if ( op->o_do_not_cache || be_isroot( op ) ) {
774 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
775 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
776 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
779 mc_curr.mc_local_ndn = op->o_ndn;
781 /* Explicit binds must not be shared */
782 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
783 mc_curr.mc_conn = op->o_conn;
786 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
790 /* Explicit Bind requests always get their own conn */
791 if ( !( sendok & LDAP_BACK_BINDING ) ) {
792 /* Searches for a metaconn in the avl tree */
794 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
795 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
796 (caddr_t)&mc_curr, meta_back_conndn_cmp );
798 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
799 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
801 /* don't let anyone else use this expired connection */
802 (void)avl_delete( &mi->mi_conninfo.lai_tree,
803 (caddr_t)mc, meta_back_conndnmc_cmp );
805 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired.\n",
806 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
809 /* Don't reuse connections while they're still binding */
810 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
811 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
812 ldap_pvt_thread_yield();
818 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
821 switch ( op->o_tag ) {
823 /* if we go to selection, the entry must not exist,
824 * and we must be able to resolve the parent */
825 dn_type = META_DNTYPE_PARENT;
826 dnParent( &ndn, &pndn );
829 case LDAP_REQ_MODRDN:
830 /* if nnewSuperior is not NULL, it must resolve
831 * to the same candidate as the req_ndn */
832 if ( op->orr_nnewSup ) {
833 dn_type = META_DNTYPE_NEWPARENT;
838 /* if bound as rootdn, the backend must bind to all targets
839 * with the administrative identity */
840 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
841 op_type = META_OP_REQUIRE_ALL;
845 case LDAP_REQ_DELETE:
846 case LDAP_REQ_MODIFY:
847 /* just a unique candidate */
850 case LDAP_REQ_COMPARE:
851 case LDAP_REQ_SEARCH:
852 /* allow multiple candidates for the searchBase */
853 op_type = META_OP_ALLOW_MULTIPLE;
857 /* right now, just break (exop?) */
862 * require all connections ...
864 if ( op_type == META_OP_REQUIRE_ALL ) {
866 /* Looks like we didn't get a bind. Open a new session... */
868 mc = metaconn_alloc( op );
869 mc->mc_conn = mc_curr.mc_conn;
870 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
872 if ( sendok & LDAP_BACK_BINDING ) {
873 LDAP_BACK_CONN_BINDING_SET( mc );
877 for ( i = 0; i < mi->mi_ntargets; i++ ) {
878 metatarget_t *mt = &mi->mi_targets[ i ];
881 * The target is activated; if needed, it is
884 candidates[ i ].sr_err = meta_back_init_one_conn( op,
886 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
887 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
888 candidates[ i ].sr_tag = META_CANDIDATE;
894 * FIXME: in case one target cannot
895 * be init'd, should the other ones
898 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
899 err = candidates[ i ].sr_err;
904 if ( ncandidates == 0 ) {
906 meta_back_freeconn( op, mc );
909 meta_back_release_conn( op, mc );
912 rs->sr_err = LDAP_NO_SUCH_OBJECT;
913 rs->sr_text = "Unable to select valid candidates";
915 if ( sendok & LDAP_BACK_SENDERR ) {
916 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
917 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
919 send_ldap_result( op, rs );
921 rs->sr_matched = NULL;
931 * looks in cache, if any
933 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
934 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
937 if ( op_type == META_OP_REQUIRE_SINGLE ) {
938 metatarget_t *mt = NULL;
939 metasingleconn_t *msc = NULL;
943 for ( j = 0; j < mi->mi_ntargets; j++ ) {
944 candidates[ j ].sr_tag = META_NOT_CANDIDATE;
948 * tries to get a unique candidate
949 * (takes care of default target)
951 if ( i == META_TARGET_NONE ) {
952 i = meta_back_get_candidate( op, rs, &ndn );
954 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
955 i = meta_back_get_candidate( op, rs, &pndn );
958 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
960 meta_back_release_conn( op, mc );
963 if ( sendok & LDAP_BACK_SENDERR ) {
964 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
965 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
967 send_ldap_result( op, rs );
969 rs->sr_matched = NULL;
976 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
979 meta_back_release_conn( op, mc );
982 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
983 rs->sr_text = "cross-target rename not supported";
984 if ( sendok & LDAP_BACK_SENDERR ) {
985 send_ldap_result( op, rs );
992 Debug( LDAP_DEBUG_TRACE,
993 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
994 i, op->o_req_ndn.bv_val, 0 );
997 /* Retries searching for a metaconn in the avl tree
998 * the reason is that the connection might have been
999 * created by meta_back_get_candidate() */
1000 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1002 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1003 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1004 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1006 /* Don't reuse connections while they're still binding */
1007 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1008 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1009 ldap_pvt_thread_yield();
1015 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1018 /* Looks like we didn't get a bind. Open a new session... */
1020 mc = metaconn_alloc( op );
1021 mc->mc_conn = mc_curr.mc_conn;
1022 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1024 if ( sendok & LDAP_BACK_BINDING ) {
1025 LDAP_BACK_CONN_BINDING_SET( mc );
1031 * Clear all other candidates
1033 ( void )meta_clear_unused_candidates( op, i );
1035 mt = &mi->mi_targets[ i ];
1036 msc = &mc->mc_conns[ i ];
1039 * The target is activated; if needed, it is
1040 * also init'd. In case of error, meta_back_init_one_conn
1041 * sends the appropriate result.
1043 err = meta_back_init_one_conn( op, rs, mt, mc, i,
1044 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
1045 if ( err != LDAP_SUCCESS ) {
1047 * FIXME: in case one target cannot
1048 * be init'd, should the other ones
1051 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1053 (void)meta_clear_one_candidate( msc );
1054 meta_back_freeconn( op, mc );
1057 meta_back_release_conn( op, mc );
1062 candidates[ i ].sr_err = LDAP_SUCCESS;
1063 candidates[ i ].sr_tag = META_CANDIDATE;
1071 * if no unique candidate ...
1075 /* Looks like we didn't get a bind. Open a new session... */
1077 mc = metaconn_alloc( op );
1078 mc->mc_conn = mc_curr.mc_conn;
1079 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1081 if ( sendok & LDAP_BACK_BINDING ) {
1082 LDAP_BACK_CONN_BINDING_SET( mc );
1086 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1087 metatarget_t *mt = &mi->mi_targets[ i ];
1088 metasingleconn_t *msc = &mc->mc_conns[ i ];
1091 || meta_back_is_candidate( &mt->mt_nsuffix,
1093 mt->mt_subtree_exclude,
1095 LDAP_SCOPE_SUBTREE ) )
1099 * The target is activated; if needed, it is
1102 int lerr = meta_back_init_one_conn( op, rs,
1104 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1106 if ( lerr == LDAP_SUCCESS ) {
1107 candidates[ i ].sr_tag = META_CANDIDATE;
1108 candidates[ i ].sr_err = LDAP_SUCCESS;
1111 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1112 op->o_log_prefix, i, 0 );
1117 * FIXME: in case one target cannot
1118 * be init'd, should the other ones
1122 ( void )meta_clear_one_candidate( msc );
1124 /* leave the target candidate, but record the error for later use */
1125 candidates[ i ].sr_err = lerr;
1128 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed: %d\n",
1129 op->o_log_prefix, i, lerr );
1136 ( void )meta_clear_one_candidate( msc );
1138 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1142 if ( ncandidates == 0 ) {
1144 meta_back_freeconn( op, mc );
1147 meta_back_release_conn( op, mc );
1150 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1151 rs->sr_text = "Unable to select valid candidates";
1153 if ( sendok & LDAP_BACK_SENDERR ) {
1154 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1155 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1157 send_ldap_result( op, rs );
1159 rs->sr_matched = NULL;
1167 /* clear out meta_back_init_one_conn non-fatal errors */
1168 rs->sr_err = LDAP_SUCCESS;
1171 /* touch the timestamp */
1172 if ( mi->mi_idle_timeout != 0 ) {
1173 mc->mc_time = op->o_time;
1178 if ( mi->mi_conn_ttl ) {
1179 mc->mc_create_time = op->o_time;
1183 * Inserts the newly created metaconn in the avl tree
1185 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1186 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1187 meta_back_conndn_cmp, meta_back_conndn_dup );
1189 #if PRINT_CONNTREE > 0
1190 myprint( mi->mi_conninfo.lai_tree );
1191 #endif /* PRINT_CONNTREE */
1193 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1196 * Err could be -1 in case a duplicate metaconn is inserted
1198 * FIXME: what if the same client issues more than one
1199 * asynchronous operations?
1202 Debug( LDAP_DEBUG_ANY,
1203 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1204 op->o_log_prefix, ncandidates,
1205 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1207 rs->sr_err = LDAP_OTHER;
1208 rs->sr_text = "Internal server error";
1209 meta_back_freeconn( op, mc );
1210 if ( sendok & LDAP_BACK_SENDERR ) {
1211 send_ldap_result( op, rs );
1217 Debug( LDAP_DEBUG_TRACE,
1218 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1219 op->o_log_prefix, ncandidates,
1220 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1223 Debug( LDAP_DEBUG_TRACE,
1224 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1225 op->o_log_prefix, ncandidates,
1226 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1233 meta_back_release_conn_lock(
1239 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1241 assert( mc != NULL );
1244 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1246 assert( mc->mc_refcnt > 0 );
1248 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1250 || ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
1251 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
1253 Debug( LDAP_DEBUG_TRACE, "%s meta_back_release_conn: mc=%p conn=%ld expired.\n",
1254 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1255 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1256 ( caddr_t )mc, meta_back_conndnmc_cmp );
1257 if ( mc->mc_refcnt == 0 ) {
1258 meta_clear_candidates( op, mc );
1259 meta_back_conn_free( mc );
1263 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );