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 );
268 #endif /* HAVE_TLS */
273 if ( msc->msc_ld != NULL ) {
274 return rs->sr_err = LDAP_SUCCESS;
277 msc->msc_mscflags = 0;
280 * Attempts to initialize the connection to the target ds
282 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
283 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
285 is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
286 #endif /* HAVE_TLS */
287 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
288 if ( rs->sr_err != LDAP_SUCCESS ) {
293 * Set LDAP version. This will always succeed: If the client
294 * bound with a particular version, then so can we.
296 if ( mt->mt_version != 0 ) {
297 version = mt->mt_version;
299 } else if ( op->o_conn->c_protocol != 0 ) {
300 version = op->o_conn->c_protocol;
303 version = LDAP_VERSION3;
305 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
306 ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
308 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
309 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
310 LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
313 /* start TLS ("tls [try-]{start|propagate}" statement) */
314 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
317 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
319 * use asynchronous StartTLS; in case, chase referral
320 * FIXME: OpenLDAP does not return referral on StartTLS yet
324 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
325 if ( rs->sr_err == LDAP_SUCCESS ) {
326 LDAPMessage *res = NULL;
327 int rc, nretries = mt->mt_nretries;
330 LDAP_BACK_TV_SET( &tv );
333 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
335 rs->sr_err = LDAP_OTHER;
337 } else if ( rc == 0 ) {
338 if ( nretries != 0 ) {
339 if ( nretries > 0 ) {
342 LDAP_BACK_TV_SET( &tv );
345 rs->sr_err = LDAP_OTHER;
347 } else if ( rc == LDAP_RES_EXTENDED ) {
348 struct berval *data = NULL;
350 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
352 if ( rs->sr_err == LDAP_SUCCESS ) {
355 rs->sr_err = ldap_parse_result( msc->msc_ld, res,
356 &err, NULL, NULL, NULL, NULL, 1 );
359 if ( rs->sr_err == LDAP_SUCCESS ) {
363 /* FIXME: in case a referral
364 * is returned, should we try
365 * using it instead of the
367 if ( rs->sr_err == LDAP_SUCCESS ) {
368 ldap_install_tls( msc->msc_ld );
370 } else if ( rs->sr_err == LDAP_REFERRAL ) {
371 rs->sr_err = LDAP_OTHER;
372 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
376 if ( data->bv_val ) {
377 ber_memfree( data->bv_val );
384 rs->sr_err = LDAP_OTHER;
391 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
393 * use synchronous StartTLS
395 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
396 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
398 /* if StartTLS is requested, only attempt it if the URL
399 * is not "ldaps://"; this may occur not only in case
400 * of misconfiguration, but also when used in the chain
401 * overlay, where the "uri" can be parsed out of a referral */
402 if ( rs->sr_err == LDAP_SERVER_DOWN
403 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
405 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
410 #endif /* HAVE_TLS */
413 * Set the network timeout if set
415 if ( mt->mt_network_timeout != 0 ) {
416 struct timeval network_timeout;
418 network_timeout.tv_usec = 0;
419 network_timeout.tv_sec = mt->mt_network_timeout;
421 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
422 (void *)&network_timeout );
426 * If the connection DN is not null, an attempt to rewrite it is made
430 if ( !BER_BVISNULL( &mt->mt_pseudorootdn ) ) {
431 ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
432 if ( !BER_BVISNULL( &mt->mt_pseudorootpw ) ) {
433 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
437 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
440 LDAP_BACK_CONN_ISPRIV_SET( msc );
443 BER_BVZERO( &msc->msc_cred );
444 BER_BVZERO( &msc->msc_bound_ndn );
445 if ( !BER_BVISEMPTY( &op->o_ndn )
446 && SLAP_IS_AUTHZ_BACKEND( op )
450 dc.conn = op->o_conn;
455 * Rewrite the bind dn if needed
457 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
458 &msc->msc_bound_ndn ) )
460 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
465 /* copy the DN idf needed */
466 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
467 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
471 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
475 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
478 if ( rs->sr_err == LDAP_SUCCESS ) {
480 * Sets a cookie for the rewrite session
482 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
485 rs->sr_err = slap_map_api2result( rs );
486 if ( sendok & LDAP_BACK_SENDERR ) {
487 send_ldap_result( op, rs );
498 * Retries one connection
506 ldap_back_send_t sendok )
508 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
509 metatarget_t *mt = mi->mi_targets[ candidate ];
510 metaconn_t *mc = *mcp;
511 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
512 int rc = LDAP_UNAVAILABLE,
513 binding = LDAP_BACK_CONN_BINDING( msc );
515 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
517 assert( mc->mc_refcnt > 0 );
518 if ( mc->mc_refcnt == 1 ) {
519 if ( LogTest( LDAP_DEBUG_ANY ) ) {
520 char buf[ SLAP_TEXT_BUFLEN ];
522 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
523 snprintf( buf, sizeof( buf ),
524 "retrying URI=\"%s\" DN=\"%s\"",
526 BER_BVISNULL( &msc->msc_bound_ndn ) ?
527 "" : msc->msc_bound_ndn.bv_val );
528 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
530 Debug( LDAP_DEBUG_ANY,
531 "%s meta_back_retry[%d]: %s.\n",
532 op->o_log_prefix, candidate, buf );
535 meta_clear_one_candidate( msc );
536 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
538 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
540 /* mc here must be the regular mc, reset and ready for init */
541 rc = meta_back_init_one_conn( op, rs, mt, mc, candidate,
542 LDAP_BACK_CONN_ISPRIV( mc ), sendok );
544 LDAP_BACK_CONN_BINDING_SET( msc );
547 if ( rc == LDAP_SUCCESS ) {
548 rc = meta_back_single_dobind( op, rs, mcp, candidate,
549 sendok, mt->mt_nretries, 0 );
551 Debug( LDAP_DEBUG_ANY,
552 "%s meta_back_retry[%d]: "
553 "meta_back_single_dobind=%d\n",
554 op->o_log_prefix, candidate, rc );
555 if ( rc == LDAP_SUCCESS ) {
556 if ( be_isroot( op ) ) {
557 LDAP_BACK_CONN_ISBOUND_SET( msc );
559 LDAP_BACK_CONN_ISANON_SET( msc );
565 if ( rc != LDAP_SUCCESS ) {
566 SlapReply *candidates = meta_back_candidates_get( op );
568 candidates[ candidate ].sr_err = rc;
570 if ( *mcp != NULL ) {
572 LDAP_BACK_CONN_BINDING_CLEAR( msc );
574 LDAP_BACK_CONN_TAINTED_SET( mc );
575 /* only release if mandatory; otherwise
576 * let the caller do what's best before
578 if ( META_BACK_ONERR_STOP( mi ) ) {
579 meta_back_release_conn_lock( op, mc, 0 );
587 send_ldap_result( op, rs );
591 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
593 return rc == LDAP_SUCCESS ? 1 : 0;
597 * callback for unique candidate selection
600 meta_back_conn_cb( Operation *op, SlapReply *rs )
602 assert( op->o_tag == LDAP_REQ_SEARCH );
604 switch ( rs->sr_type ) {
606 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
622 meta_back_get_candidate(
627 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
631 * tries to get a unique candidate
632 * (takes care of default target)
634 candidate = meta_back_select_unique_candidate( mi, ndn );
637 * if any is found, inits the connection
639 if ( candidate == META_TARGET_NONE ) {
640 rs->sr_err = LDAP_NO_SUCH_OBJECT;
641 rs->sr_text = "no suitable candidate target found";
643 } else if ( candidate == META_TARGET_MULTIPLE ) {
646 SlapReply rs2 = { 0 };
647 slap_callback cb2 = { 0 };
650 /* try to get a unique match for the request ndn
651 * among the multiple candidates available */
652 op2.o_tag = LDAP_REQ_SEARCH;
654 op2.o_req_ndn = *ndn;
655 op2.ors_scope = LDAP_SCOPE_BASE;
656 op2.ors_deref = LDAP_DEREF_NEVER;
657 op2.ors_attrs = slap_anlist_no_attrs;
658 op2.ors_attrsonly = 0;
659 op2.ors_limit = NULL;
661 op2.ors_tlimit = SLAP_NO_LIMIT;
663 f.f_choice = LDAP_FILTER_PRESENT;
664 f.f_desc = slap_schema.si_ad_objectClass;
666 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
668 op2.o_callback = &cb2;
669 cb2.sc_response = meta_back_conn_cb;
670 cb2.sc_private = (void *)&candidate;
672 rc = op->o_bd->be_search( &op2, &rs2 );
674 switch ( rs2.sr_err ) {
677 rs->sr_err = rs2.sr_err;
680 case LDAP_SIZELIMIT_EXCEEDED:
681 /* if multiple candidates can serve the operation,
682 * and a default target is defined, and it is
683 * a candidate, try using it (FIXME: YMMV) */
684 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
685 && meta_back_is_candidate( &mi->mi_targets[ mi->mi_defaulttarget ]->mt_nsuffix,
686 mi->mi_targets[ mi->mi_defaulttarget ]->mt_scope,
687 mi->mi_targets[ mi->mi_defaulttarget ]->mt_subtree_exclude,
688 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
690 candidate = mi->mi_defaulttarget;
691 rs->sr_err = LDAP_SUCCESS;
695 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
696 rs->sr_text = "cannot select unique candidate target";
702 rs->sr_err = LDAP_SUCCESS;
708 static void *meta_back_candidates_dummy;
711 meta_back_candidates_keyfree(
715 metacandidates_t *mc = (metacandidates_t *)data;
717 ber_memfree_x( mc->mc_candidates, NULL );
718 ber_memfree_x( data, NULL );
722 meta_back_candidates_get( Operation *op )
724 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
725 metacandidates_t *mc;
727 if ( op->o_threadctx ) {
730 ldap_pvt_thread_pool_getkey( op->o_threadctx,
731 &meta_back_candidates_dummy, &data, NULL );
732 mc = (metacandidates_t *)data;
735 mc = mi->mi_candidates;
739 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
740 mc->mc_ntargets = mi->mi_ntargets;
741 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
742 if ( op->o_threadctx ) {
746 ldap_pvt_thread_pool_setkey( op->o_threadctx,
747 &meta_back_candidates_dummy, data,
748 meta_back_candidates_keyfree );
751 mi->mi_candidates = mc;
754 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
755 /* NOTE: in the future, may want to allow back-config
756 * to add/remove targets from back-meta... */
757 mc->mc_candidates = ch_realloc( mc->mc_candidates,
758 sizeof( SlapReply ) * mi->mi_ntargets );
759 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
760 sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
761 mc->mc_ntargets = mi->mi_ntargets;
764 return mc->mc_candidates;
770 * Prepares the connection structure
774 * - determine what DN is being requested:
776 * op requires candidate checks
778 * add unique parent of o_req_ndn
779 * bind unique^*[/all] o_req_ndn [no check]
780 * compare unique^+ o_req_ndn
781 * delete unique o_req_ndn
782 * modify unique o_req_ndn
783 * search any o_req_ndn
784 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
786 * - for ops that require the candidate to be unique, in case of multiple
787 * occurrences an internal search with sizeLimit=1 is performed
788 * if a unique candidate can actually be determined. If none is found,
789 * the operation aborts; if multiple are found, the default target
790 * is used if defined and candidate; otherwise the operation aborts.
792 * *^note: actually, the bind operation is handled much like a search;
793 * i.e. the bind is broadcast to all candidate targets.
795 * +^note: actually, the compare operation is handled much like a search;
796 * i.e. the compare is broadcast to all candidate targets, while checking
797 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
805 ldap_back_send_t sendok )
807 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
808 metaconn_t *mc = NULL,
810 int cached = META_TARGET_NONE,
811 i = META_TARGET_NONE,
817 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
821 META_DNTYPE_NEWPARENT
822 } dn_type = META_DNTYPE_ENTRY;
823 struct berval ndn = op->o_req_ndn,
826 SlapReply *candidates = meta_back_candidates_get( op );
828 /* Internal searches are privileged and shared. So is root. */
829 /* FIXME: there seem to be concurrency issues */
830 if ( op->o_do_not_cache || be_isroot( op ) ) {
831 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
832 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
833 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
836 mc_curr.mc_local_ndn = op->o_ndn;
838 /* Explicit binds must not be shared */
839 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
840 mc_curr.mc_conn = op->o_conn;
843 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
847 /* Explicit Bind requests always get their own conn */
848 if ( !( sendok & LDAP_BACK_BINDING ) ) {
849 /* Searches for a metaconn in the avl tree */
851 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
852 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
853 (caddr_t)&mc_curr, meta_back_conndn_cmp );
855 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
856 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
858 /* don't let anyone else use this expired connection */
859 (void)avl_delete( &mi->mi_conninfo.lai_tree,
860 (caddr_t)mc, meta_back_conndnmc_cmp );
861 LDAP_BACK_CONN_TAINTED_SET( mc );
863 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired.\n",
864 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
867 /* Don't reuse connections while they're still binding */
868 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
869 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
870 ldap_pvt_thread_yield();
876 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
879 switch ( op->o_tag ) {
881 /* if we go to selection, the entry must not exist,
882 * and we must be able to resolve the parent */
883 dn_type = META_DNTYPE_PARENT;
884 dnParent( &ndn, &pndn );
887 case LDAP_REQ_MODRDN:
888 /* if nnewSuperior is not NULL, it must resolve
889 * to the same candidate as the req_ndn */
890 if ( op->orr_nnewSup ) {
891 dn_type = META_DNTYPE_NEWPARENT;
896 /* if bound as rootdn, the backend must bind to all targets
897 * with the administrative identity */
898 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
899 op_type = META_OP_REQUIRE_ALL;
903 case LDAP_REQ_DELETE:
904 case LDAP_REQ_MODIFY:
905 /* just a unique candidate */
908 case LDAP_REQ_COMPARE:
909 case LDAP_REQ_SEARCH:
910 /* allow multiple candidates for the searchBase */
911 op_type = META_OP_ALLOW_MULTIPLE;
915 /* right now, just break (exop?) */
920 * require all connections ...
922 if ( op_type == META_OP_REQUIRE_ALL ) {
924 /* Looks like we didn't get a bind. Open a new session... */
926 mc = metaconn_alloc( op );
927 mc->mc_conn = mc_curr.mc_conn;
928 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
930 if ( sendok & LDAP_BACK_BINDING ) {
931 LDAP_BACK_CONN_BINDING_SET( mc );
935 for ( i = 0; i < mi->mi_ntargets; i++ ) {
936 metatarget_t *mt = mi->mi_targets[ i ];
939 * The target is activated; if needed, it is
942 candidates[ i ].sr_err = meta_back_init_one_conn( op,
944 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
945 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
946 candidates[ i ].sr_tag = META_CANDIDATE;
952 * FIXME: in case one target cannot
953 * be init'd, should the other ones
956 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
957 err = candidates[ i ].sr_err;
962 if ( ncandidates == 0 ) {
964 meta_back_freeconn( op, mc );
967 meta_back_release_conn( op, mc );
970 rs->sr_err = LDAP_NO_SUCH_OBJECT;
971 rs->sr_text = "Unable to select valid candidates";
973 if ( sendok & LDAP_BACK_SENDERR ) {
974 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
975 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
977 send_ldap_result( op, rs );
979 rs->sr_matched = NULL;
989 * looks in cache, if any
991 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
992 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
995 if ( op_type == META_OP_REQUIRE_SINGLE ) {
996 metatarget_t *mt = NULL;
997 metasingleconn_t *msc = NULL;
1001 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1002 candidates[ j ].sr_tag = META_NOT_CANDIDATE;
1006 * tries to get a unique candidate
1007 * (takes care of default target)
1009 if ( i == META_TARGET_NONE ) {
1010 i = meta_back_get_candidate( op, rs, &ndn );
1012 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1013 i = meta_back_get_candidate( op, rs, &pndn );
1016 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1018 meta_back_release_conn( op, mc );
1021 if ( sendok & LDAP_BACK_SENDERR ) {
1022 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1023 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1025 send_ldap_result( op, rs );
1027 rs->sr_matched = NULL;
1034 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1037 meta_back_release_conn( op, mc );
1040 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1041 rs->sr_text = "cross-target rename not supported";
1042 if ( sendok & LDAP_BACK_SENDERR ) {
1043 send_ldap_result( op, rs );
1050 Debug( LDAP_DEBUG_TRACE,
1051 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1052 i, op->o_req_ndn.bv_val, 0 );
1055 /* Retries searching for a metaconn in the avl tree
1056 * the reason is that the connection might have been
1057 * created by meta_back_get_candidate() */
1058 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1060 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1061 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1062 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1064 /* Don't reuse connections while they're still binding */
1065 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1066 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1067 ldap_pvt_thread_yield();
1073 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1076 /* Looks like we didn't get a bind. Open a new session... */
1078 mc = metaconn_alloc( op );
1079 mc->mc_conn = mc_curr.mc_conn;
1080 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1082 if ( sendok & LDAP_BACK_BINDING ) {
1083 LDAP_BACK_CONN_BINDING_SET( mc );
1089 * Clear all other candidates
1091 ( void )meta_clear_unused_candidates( op, i );
1093 mt = mi->mi_targets[ i ];
1094 msc = &mc->mc_conns[ i ];
1097 * The target is activated; if needed, it is
1098 * also init'd. In case of error, meta_back_init_one_conn
1099 * sends the appropriate result.
1101 err = meta_back_init_one_conn( op, rs, mt, mc, i,
1102 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
1103 if ( err != LDAP_SUCCESS ) {
1105 * FIXME: in case one target cannot
1106 * be init'd, should the other ones
1109 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1111 (void)meta_clear_one_candidate( msc );
1112 meta_back_freeconn( op, mc );
1115 meta_back_release_conn( op, mc );
1120 candidates[ i ].sr_err = LDAP_SUCCESS;
1121 candidates[ i ].sr_tag = META_CANDIDATE;
1129 * if no unique candidate ...
1133 /* Looks like we didn't get a bind. Open a new session... */
1135 mc = metaconn_alloc( op );
1136 mc->mc_conn = mc_curr.mc_conn;
1137 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1139 if ( sendok & LDAP_BACK_BINDING ) {
1140 LDAP_BACK_CONN_BINDING_SET( mc );
1144 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1145 metatarget_t *mt = mi->mi_targets[ i ];
1146 metasingleconn_t *msc = &mc->mc_conns[ i ];
1149 || meta_back_is_candidate( &mt->mt_nsuffix,
1151 mt->mt_subtree_exclude,
1153 LDAP_SCOPE_SUBTREE ) )
1157 * The target is activated; if needed, it is
1160 int lerr = meta_back_init_one_conn( op, rs,
1162 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1164 if ( lerr == LDAP_SUCCESS ) {
1165 candidates[ i ].sr_tag = META_CANDIDATE;
1166 candidates[ i ].sr_err = LDAP_SUCCESS;
1169 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1170 op->o_log_prefix, i, 0 );
1175 * FIXME: in case one target cannot
1176 * be init'd, should the other ones
1180 ( void )meta_clear_one_candidate( msc );
1182 /* leave the target candidate, but record the error for later use */
1183 candidates[ i ].sr_err = lerr;
1186 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed: %d\n",
1187 op->o_log_prefix, i, lerr );
1194 ( void )meta_clear_one_candidate( msc );
1196 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1200 if ( ncandidates == 0 ) {
1202 meta_back_freeconn( op, mc );
1205 meta_back_release_conn( op, mc );
1208 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1209 rs->sr_text = "Unable to select valid candidates";
1211 if ( sendok & LDAP_BACK_SENDERR ) {
1212 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1213 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1215 send_ldap_result( op, rs );
1217 rs->sr_matched = NULL;
1225 /* clear out meta_back_init_one_conn non-fatal errors */
1226 rs->sr_err = LDAP_SUCCESS;
1229 /* touch the timestamp */
1230 if ( mi->mi_idle_timeout != 0 ) {
1231 mc->mc_time = op->o_time;
1235 if ( mi->mi_conn_ttl ) {
1236 mc->mc_create_time = op->o_time;
1240 * Inserts the newly created metaconn in the avl tree
1242 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1243 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1244 meta_back_conndn_cmp, meta_back_conndn_dup );
1246 #if PRINT_CONNTREE > 0
1247 myprint( mi->mi_conninfo.lai_tree );
1248 #endif /* PRINT_CONNTREE */
1250 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1253 * Err could be -1 in case a duplicate metaconn is inserted
1260 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1261 /* duplicate: free and try to get the newly created one */
1264 LDAP_BACK_CONN_TAINTED_SET( mc );
1268 Debug( LDAP_DEBUG_ANY,
1269 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1270 op->o_log_prefix, ncandidates,
1271 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1273 meta_back_freeconn( op, mc );
1275 rs->sr_err = LDAP_OTHER;
1276 rs->sr_text = "proxy bind collision";
1277 if ( sendok & LDAP_BACK_SENDERR ) {
1278 send_ldap_result( op, rs );
1284 Debug( LDAP_DEBUG_TRACE,
1285 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1286 op->o_log_prefix, ncandidates,
1287 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1290 Debug( LDAP_DEBUG_TRACE,
1291 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1292 op->o_log_prefix, ncandidates,
1293 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1300 meta_back_release_conn_lock(
1305 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1307 assert( mc != NULL );
1310 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1312 assert( mc->mc_refcnt > 0 );
1314 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1315 if ( LDAP_BACK_CONN_TAINTED( mc ) ) {
1316 Debug( LDAP_DEBUG_TRACE, "%s meta_back_release_conn: mc=%p conn=%ld expired.\n",
1317 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1318 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1319 ( caddr_t )mc, meta_back_conndnmc_cmp );
1320 if ( mc->mc_refcnt == 0 ) {
1321 meta_clear_candidates( op, mc );
1322 meta_back_conn_free( mc );
1326 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );