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 * meta_back_conndn_cmp
40 * compares two struct metaconn based on the value of the conn pointer
41 * and of the local DN; used by avl stuff
48 metaconn_t *mc1 = ( metaconn_t * )c1;
49 metaconn_t *mc2 = ( metaconn_t * )c2;
52 /* If local DNs don't match, it is definitely not a match */
53 /* For shared sessions, conn is NULL. Only explicitly
54 * bound sessions will have non-NULL conn.
56 rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
58 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
65 * meta_back_conndnmc_cmp
67 * compares two struct metaconn based on the value of the conn pointer,
68 * the local DN and the struct pointer; used by avl stuff
71 meta_back_conndnmc_cmp(
75 metaconn_t *mc1 = ( metaconn_t * )c1;
76 metaconn_t *mc2 = ( metaconn_t * )c2;
79 /* If local DNs don't match, it is definitely not a match */
80 /* For shared sessions, conn is NULL. Only explicitly
81 * bound sessions will have non-NULL conn.
83 rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
85 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
87 rc = SLAP_PTRCMP( mc1, mc2 );
97 * compares two struct metaconn based on the value of the conn pointer;
105 metaconn_t *mc1 = ( metaconn_t * )c1;
106 metaconn_t *mc2 = ( metaconn_t * )c2;
108 /* For shared sessions, conn is NULL. Only explicitly
109 * bound sessions will have non-NULL conn.
111 return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
115 * meta_back_conndn_dup
117 * returns -1 in case a duplicate struct metaconn has been inserted;
121 meta_back_conndn_dup(
125 metaconn_t *mc1 = ( metaconn_t * )c1;
126 metaconn_t *mc2 = ( metaconn_t * )c2;
128 /* Cannot have more than one shared session with same DN */
129 if ( mc1->mc_conn == mc2->mc_conn &&
130 dn_match( &mc1->mc_local_ndn, &mc2->mc_local_ndn ) )
139 * Debug stuff (got it from libavl)
141 #if META_BACK_PRINT_CONNTREE > 0
143 ravl_print( Avlnode *root, int depth )
152 ravl_print( root->avl_right, depth + 1 );
154 for ( i = 0; i < depth; i++ ) {
155 fprintf( stderr, "-" );
158 mc = (metaconn_t *)root->avl_data;
159 fprintf( stderr, "mc=%p local=\"%s\" conn=%p %s refcnt=%d%s\n",
161 mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "",
163 avl_bf2str( root->avl_bf ), mc->mc_refcnt,
164 LDAP_BACK_CONN_TAINTED( mc ) ? " tainted" : "" );
166 ravl_print( root->avl_left, depth + 1 );
170 meta_back_print_conntree( Avlnode *root, char *msg )
172 fprintf( stderr, "========> %s\n", msg );
175 fprintf( stderr, "\t(empty)\n" );
178 ravl_print( root, 0 );
181 fprintf( stderr, "<======== %s\n", msg );
183 #endif /* META_BACK_PRINT_CONNTREE */
191 * Allocates a connection structure, making room for all the referenced targets
197 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
199 int ntargets = mi->mi_ntargets;
201 assert( ntargets > 0 );
203 /* malloc all in one */
204 mc = ( metaconn_t * )ch_calloc( 1, sizeof( metaconn_t )
205 + sizeof( metasingleconn_t ) * ( ntargets - 1 ) );
212 mc->mc_authz_target = META_BOUND_NONE;
219 * meta_back_init_one_conn
221 * Initializes one connection
224 meta_back_init_one_conn(
230 ldap_back_send_t sendok,
233 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
234 metatarget_t *mt = mi->mi_targets[ candidate ];
235 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
238 int isauthz = ( candidate == mc->mc_authz_target );
242 #endif /* HAVE_TLS */
244 /* if the server is quarantined, and
245 * - the current interval did not expire yet, or
246 * - no more retries should occur,
247 * don't return the connection */
248 if ( mt->mt_isquarantined ) {
249 slap_retry_info_t *ri = &mt->mt_quarantine;
252 if ( mt->mt_isquarantined == LDAP_BACK_FQ_YES ) {
253 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
254 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
256 if ( LogTest( LDAP_DEBUG_ANY ) ) {
257 char buf[ SLAP_TEXT_BUFLEN ];
259 snprintf( buf, sizeof( buf ),
260 "meta_back_init_one_conn[%d]: quarantine "
261 "retry block #%d try #%d",
262 candidate, ri->ri_idx, ri->ri_count );
263 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
264 op->o_log_prefix, buf, 0 );
267 mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
272 rs->sr_err = LDAP_UNAVAILABLE;
273 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
274 send_ldap_result( op, rs );
282 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
288 if ( LDAP_BACK_CONN_ISBOUND( msc )
289 || LDAP_BACK_CONN_ISANON( msc ) )
291 assert( msc->msc_ld != NULL );
292 rs->sr_err = LDAP_SUCCESS;
295 } else if ( META_BACK_CONN_CREATING( msc )
296 || LDAP_BACK_CONN_BINDING( msc ) )
298 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
300 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
303 ldap_pvt_thread_yield();
307 /* sounds more appropriate */
308 rs->sr_err = LDAP_BUSY;
311 } else if ( META_BACK_CONN_INITED( msc ) ) {
312 assert( msc->msc_ld != NULL );
313 rs->sr_err = LDAP_SUCCESS;
320 META_BACK_CONN_CREATING_SET( msc );
324 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
328 if ( rs->sr_err != LDAP_SUCCESS && op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
329 send_ldap_result( op, rs );
335 assert( msc->msc_ld == NULL );
338 * Attempts to initialize the connection to the target ds
340 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
341 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
343 is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
344 #endif /* HAVE_TLS */
345 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
346 if ( rs->sr_err != LDAP_SUCCESS ) {
351 * Set LDAP version. This will always succeed: If the client
352 * bound with a particular version, then so can we.
354 if ( mt->mt_version != 0 ) {
355 version = mt->mt_version;
357 } else if ( op->o_conn->c_protocol != 0 ) {
358 version = op->o_conn->c_protocol;
361 version = LDAP_VERSION3;
363 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
364 ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
366 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
367 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
368 LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
371 /* start TLS ("tls [try-]{start|propagate}" statement) */
372 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
375 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
377 * use asynchronous StartTLS; in case, chase referral
378 * FIXME: OpenLDAP does not return referral on StartTLS yet
382 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
383 if ( rs->sr_err == LDAP_SUCCESS ) {
384 LDAPMessage *res = NULL;
385 int rc, nretries = mt->mt_nretries;
388 LDAP_BACK_TV_SET( &tv );
391 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
394 rs->sr_err = LDAP_OTHER;
398 if ( nretries != 0 ) {
399 if ( nretries > 0 ) {
402 LDAP_BACK_TV_SET( &tv );
405 rs->sr_err = LDAP_OTHER;
409 /* only touch when activity actually took place... */
410 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
411 msc->msc_time = op->o_time;
416 if ( rc == LDAP_RES_EXTENDED ) {
417 struct berval *data = NULL;
419 /* NOTE: right now, data is unused, so don't get it */
420 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
421 NULL, NULL /* &data */ , 0 );
422 if ( rs->sr_err == LDAP_SUCCESS ) {
425 /* FIXME: matched? referrals? response controls? */
426 rs->sr_err = ldap_parse_result( msc->msc_ld, res,
427 &err, NULL, NULL, NULL, NULL, 1 );
430 if ( rs->sr_err == LDAP_SUCCESS ) {
434 /* FIXME: in case a referral
435 * is returned, should we try
436 * using it instead of the
438 if ( rs->sr_err == LDAP_SUCCESS ) {
439 ldap_install_tls( msc->msc_ld );
441 } else if ( rs->sr_err == LDAP_REFERRAL ) {
442 /* FIXME: LDAP_OPERATIONS_ERROR? */
443 rs->sr_err = LDAP_OTHER;
444 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
448 if ( data->bv_val ) {
449 ber_memfree( data->bv_val );
456 rs->sr_err = LDAP_OTHER;
463 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
465 * use synchronous StartTLS
467 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
468 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
470 /* if StartTLS is requested, only attempt it if the URL
471 * is not "ldaps://"; this may occur not only in case
472 * of misconfiguration, but also when used in the chain
473 * overlay, where the "uri" can be parsed out of a referral */
474 if ( rs->sr_err == LDAP_SERVER_DOWN
475 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
479 Debug( LDAP_DEBUG_ANY, "### %s meta_back_init_one_conn(TLS) ldap_unbind_ext[%d] ld=%p\n",
480 op->o_log_prefix, candidate, (void *)msc->msc_ld );
481 #endif /* DEBUG_205 */
483 /* need to trash a failed Start TLS */
484 meta_clear_one_candidate( op, mc, candidate );
488 #endif /* HAVE_TLS */
491 * Set the network timeout if set
493 if ( mt->mt_network_timeout != 0 ) {
494 struct timeval network_timeout;
496 network_timeout.tv_usec = 0;
497 network_timeout.tv_sec = mt->mt_network_timeout;
499 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
500 (void *)&network_timeout );
504 * If the connection DN is not null, an attempt to rewrite it is made
508 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
509 ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
510 if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
511 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
512 memset( msc->msc_cred.bv_val, 0,
513 msc->msc_cred.bv_len );
515 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
519 ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
523 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
524 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
525 ber_memfree_x( msc->msc_cred.bv_val, NULL );
526 BER_BVZERO( &msc->msc_cred );
528 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
529 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
530 BER_BVZERO( &msc->msc_bound_ndn );
532 if ( !BER_BVISEMPTY( &op->o_ndn )
533 && SLAP_IS_AUTHZ_BACKEND( op )
537 dc.conn = op->o_conn;
542 * Rewrite the bind dn if needed
544 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
545 &msc->msc_bound_ndn ) )
549 Debug( LDAP_DEBUG_ANY, "### %s meta_back_init_one_conn(rewrite) ldap_unbind_ext[%d] ld=%p\n",
550 op->o_log_prefix, candidate, (void *)msc->msc_ld );
551 #endif /* DEBUG_205 */
553 /* need to trash a connection not fully established */
554 meta_clear_one_candidate( op, mc, candidate );
558 /* copy the DN if needed */
559 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
560 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
563 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
566 ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
570 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
574 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
576 META_BACK_CONN_CREATING_CLEAR( msc );
577 if ( rs->sr_err == LDAP_SUCCESS ) {
579 * Sets a cookie for the rewrite session
581 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
582 META_BACK_CONN_INITED_SET( msc );
585 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
588 if ( rs->sr_err != LDAP_SUCCESS ) {
589 rs->sr_err = slap_map_api2result( rs );
590 if ( sendok & LDAP_BACK_SENDERR ) {
591 send_ldap_result( op, rs );
602 * Retries one connection
610 ldap_back_send_t sendok )
612 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
613 metatarget_t *mt = mi->mi_targets[ candidate ];
614 metaconn_t *mc = *mcp;
615 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
616 int rc = LDAP_UNAVAILABLE,
619 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
621 assert( !META_BACK_CONN_CREATING( msc ) );
622 binding = LDAP_BACK_CONN_BINDING( msc );
623 LDAP_BACK_CONN_BINDING_CLEAR( msc );
625 assert( mc->mc_refcnt > 0 );
626 if ( mc->mc_refcnt == 1 ) {
627 if ( LogTest( LDAP_DEBUG_ANY ) ) {
628 char buf[ SLAP_TEXT_BUFLEN ];
630 /* this lock is required; however,
631 * it's invoked only when logging is on */
632 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
633 snprintf( buf, sizeof( buf ),
634 "retrying URI=\"%s\" DN=\"%s\"",
636 BER_BVISNULL( &msc->msc_bound_ndn ) ?
637 "" : msc->msc_bound_ndn.bv_val );
638 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
640 Debug( LDAP_DEBUG_ANY,
641 "%s meta_back_retry[%d]: %s.\n",
642 op->o_log_prefix, candidate, buf );
645 meta_clear_one_candidate( op, mc, candidate );
646 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
648 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
650 /* mc here must be the regular mc, reset and ready for init */
651 rc = meta_back_init_one_conn( op, rs, mc, candidate,
652 LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 );
654 /* restore the "binding" flag, in case */
656 LDAP_BACK_CONN_BINDING_SET( msc );
659 if ( rc == LDAP_SUCCESS ) {
660 rc = meta_back_single_dobind( op, rs, mcp, candidate,
661 sendok, mt->mt_nretries, 0 );
663 Debug( LDAP_DEBUG_ANY,
664 "%s meta_back_retry[%d]: "
665 "meta_back_single_dobind=%d\n",
666 op->o_log_prefix, candidate, rc );
667 if ( rc == LDAP_SUCCESS ) {
668 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
669 !BER_BVISEMPTY( &msc->msc_bound_ndn ) )
671 LDAP_BACK_CONN_ISBOUND_SET( msc );
674 LDAP_BACK_CONN_ISANON_SET( msc );
677 /* when bound, dispose of the "binding" flag */
679 LDAP_BACK_CONN_BINDING_CLEAR( msc );
684 /* don't send twice */
685 sendok &= ~LDAP_BACK_SENDERR;
688 if ( rc != LDAP_SUCCESS ) {
689 SlapReply *candidates = meta_back_candidates_get( op );
691 candidates[ candidate ].sr_err = rc;
693 if ( *mcp != NULL ) {
694 if ( mc->mc_refcnt == 1 ) {
696 LDAP_BACK_CONN_BINDING_CLEAR( msc );
698 (void)meta_clear_one_candidate( op, mc, candidate );
701 LDAP_BACK_CONN_TAINTED_SET( mc );
702 /* only release if mandatory; otherwise
703 * let the caller do what's best before
705 if ( META_BACK_ONERR_STOP( mi ) ) {
706 meta_back_release_conn_lock( op, mc, 0 );
710 #if META_BACK_PRINT_CONNTREE > 0
711 meta_back_print_conntree( mi->mi_conninfo.lai_tree, ">>> meta_back_retry" );
712 #endif /* META_BACK_PRINT_CONNTREE */
713 /* FIXME: could be done better, reworking meta_back_release_conn_lock() */
714 (void)avl_delete( &mi->mi_conninfo.lai_tree,
715 ( caddr_t )mc, meta_back_conndnmc_cmp );
716 #if META_BACK_PRINT_CONNTREE > 0
717 meta_back_print_conntree( mi->mi_conninfo.lai_tree, "<<< meta_back_retry" );
718 #endif /* META_BACK_PRINT_CONNTREE */
722 if ( sendok & LDAP_BACK_SENDERR ) {
725 send_ldap_result( op, rs );
729 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
730 meta_back_quarantine( op, rs, candidate );
733 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
735 return rc == LDAP_SUCCESS ? 1 : 0;
739 * callback for unique candidate selection
742 meta_back_conn_cb( Operation *op, SlapReply *rs )
744 assert( op->o_tag == LDAP_REQ_SEARCH );
746 switch ( rs->sr_type ) {
748 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
764 meta_back_get_candidate(
769 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
773 * tries to get a unique candidate
774 * (takes care of default target)
776 candidate = meta_back_select_unique_candidate( mi, ndn );
779 * if any is found, inits the connection
781 if ( candidate == META_TARGET_NONE ) {
782 rs->sr_err = LDAP_NO_SUCH_OBJECT;
783 rs->sr_text = "no suitable candidate target found";
785 } else if ( candidate == META_TARGET_MULTIPLE ) {
788 SlapReply rs2 = { 0 };
789 slap_callback cb2 = { 0 };
792 /* try to get a unique match for the request ndn
793 * among the multiple candidates available */
794 op2.o_tag = LDAP_REQ_SEARCH;
796 op2.o_req_ndn = *ndn;
797 op2.ors_scope = LDAP_SCOPE_BASE;
798 op2.ors_deref = LDAP_DEREF_NEVER;
799 op2.ors_attrs = slap_anlist_no_attrs;
800 op2.ors_attrsonly = 0;
801 op2.ors_limit = NULL;
803 op2.ors_tlimit = SLAP_NO_LIMIT;
805 f.f_choice = LDAP_FILTER_PRESENT;
806 f.f_desc = slap_schema.si_ad_objectClass;
808 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
810 op2.o_callback = &cb2;
811 cb2.sc_response = meta_back_conn_cb;
812 cb2.sc_private = (void *)&candidate;
814 rc = op->o_bd->be_search( &op2, &rs2 );
816 switch ( rs2.sr_err ) {
819 rs->sr_err = rs2.sr_err;
822 case LDAP_SIZELIMIT_EXCEEDED:
823 /* if multiple candidates can serve the operation,
824 * and a default target is defined, and it is
825 * a candidate, try using it (FIXME: YMMV) */
826 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
827 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
828 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
830 candidate = mi->mi_defaulttarget;
831 rs->sr_err = LDAP_SUCCESS;
835 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
836 rs->sr_text = "cannot select unique candidate target";
842 rs->sr_err = LDAP_SUCCESS;
848 static void *meta_back_candidates_dummy;
851 meta_back_candidates_keyfree(
855 metacandidates_t *mc = (metacandidates_t *)data;
857 ber_memfree_x( mc->mc_candidates, NULL );
858 ber_memfree_x( data, NULL );
862 meta_back_candidates_get( Operation *op )
864 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
865 metacandidates_t *mc;
867 if ( op->o_threadctx ) {
870 ldap_pvt_thread_pool_getkey( op->o_threadctx,
871 &meta_back_candidates_dummy, &data, NULL );
872 mc = (metacandidates_t *)data;
875 mc = mi->mi_candidates;
879 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
880 mc->mc_ntargets = mi->mi_ntargets;
881 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
882 if ( op->o_threadctx ) {
886 ldap_pvt_thread_pool_setkey( op->o_threadctx,
887 &meta_back_candidates_dummy, data,
888 meta_back_candidates_keyfree );
891 mi->mi_candidates = mc;
894 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
895 /* NOTE: in the future, may want to allow back-config
896 * to add/remove targets from back-meta... */
897 mc->mc_candidates = ch_realloc( mc->mc_candidates,
898 sizeof( SlapReply ) * mi->mi_ntargets );
899 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
900 sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
901 mc->mc_ntargets = mi->mi_ntargets;
904 return mc->mc_candidates;
910 * Prepares the connection structure
914 * - determine what DN is being requested:
916 * op requires candidate checks
918 * add unique parent of o_req_ndn
919 * bind unique^*[/all] o_req_ndn [no check]
920 * compare unique^+ o_req_ndn
921 * delete unique o_req_ndn
922 * modify unique o_req_ndn
923 * search any o_req_ndn
924 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
926 * - for ops that require the candidate to be unique, in case of multiple
927 * occurrences an internal search with sizeLimit=1 is performed
928 * if a unique candidate can actually be determined. If none is found,
929 * the operation aborts; if multiple are found, the default target
930 * is used if defined and candidate; otherwise the operation aborts.
932 * *^note: actually, the bind operation is handled much like a search;
933 * i.e. the bind is broadcast to all candidate targets.
935 * +^note: actually, the compare operation is handled much like a search;
936 * i.e. the compare is broadcast to all candidate targets, while checking
937 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
945 ldap_back_send_t sendok )
947 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
948 metaconn_t *mc = NULL,
950 int cached = META_TARGET_NONE,
951 i = META_TARGET_NONE,
957 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
961 META_DNTYPE_NEWPARENT
962 } dn_type = META_DNTYPE_ENTRY;
963 struct berval ndn = op->o_req_ndn,
966 SlapReply *candidates = meta_back_candidates_get( op );
968 /* Internal searches are privileged and shared. So is root. */
969 /* FIXME: there seems to be concurrency issues */
970 if ( META_BACK_PROXYAUTHZ_ALWAYS( mi ) || op->o_do_not_cache || be_isroot( op ) ) {
971 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
972 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
973 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
976 mc_curr.mc_local_ndn = op->o_ndn;
978 /* Explicit binds must not be shared */
979 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
980 mc_curr.mc_conn = op->o_conn;
983 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
987 /* Explicit Bind requests always get their own conn */
988 if ( !( sendok & LDAP_BACK_BINDING ) ) {
989 /* Searches for a metaconn in the avl tree */
991 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
992 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
993 (caddr_t)&mc_curr, meta_back_conndn_cmp );
995 /* catch taint errors */
996 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
998 /* Don't reuse connections while they're still binding
999 * NOTE: only makes sense for binds */
1000 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1001 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1002 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1004 ldap_pvt_thread_yield();
1008 /* release conn, and create a temporary */
1012 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
1013 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
1015 #if META_BACK_PRINT_CONNTREE > 0
1016 meta_back_print_conntree( mi->mi_conninfo.lai_tree,
1017 ">>> meta_back_getconn(expired)" );
1018 #endif /* META_BACK_PRINT_CONNTREE */
1019 /* don't let anyone else use this expired connection */
1020 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1021 (caddr_t)mc, meta_back_conndnmc_cmp );
1022 #if META_BACK_PRINT_CONNTREE > 0
1023 meta_back_print_conntree( mi->mi_conninfo.lai_tree,
1024 "<<< meta_back_getconn(expired)" );
1025 #endif /* META_BACK_PRINT_CONNTREE */
1026 LDAP_BACK_CONN_TAINTED_SET( mc );
1028 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired (tainted).\n",
1029 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc ) );
1035 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1038 switch ( op->o_tag ) {
1040 /* if we go to selection, the entry must not exist,
1041 * and we must be able to resolve the parent */
1042 dn_type = META_DNTYPE_PARENT;
1043 dnParent( &ndn, &pndn );
1046 case LDAP_REQ_MODRDN:
1047 /* if nnewSuperior is not NULL, it must resolve
1048 * to the same candidate as the req_ndn */
1049 if ( op->orr_nnewSup ) {
1050 dn_type = META_DNTYPE_NEWPARENT;
1055 /* if bound as rootdn, the backend must bind to all targets
1056 * with the administrative identity */
1057 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
1058 op_type = META_OP_REQUIRE_ALL;
1062 case LDAP_REQ_COMPARE:
1063 case LDAP_REQ_DELETE:
1064 case LDAP_REQ_MODIFY:
1065 /* just a unique candidate */
1068 case LDAP_REQ_SEARCH:
1069 /* allow multiple candidates for the searchBase */
1070 op_type = META_OP_ALLOW_MULTIPLE;
1074 /* right now, just break (exop?) */
1079 * require all connections ...
1081 if ( op_type == META_OP_REQUIRE_ALL ) {
1083 /* Looks like we didn't get a bind. Open a new session... */
1085 assert( new_conn == 0 );
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 );
1093 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1094 LDAP_BACK_CONN_ISPRIV_SET( mc );
1098 /* TODO: if any of the connections is binding,
1099 * release mc and create a new one */
1102 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1104 * The target is activated; if needed, it is
1107 candidates[ i ].sr_err = meta_back_init_one_conn( op,
1108 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1109 LDAP_BACK_DONTSEND, !new_conn );
1110 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1111 META_CANDIDATE_SET( &candidates[ i ] );
1117 * FIXME: in case one target cannot
1118 * be init'd, should the other ones
1121 META_CANDIDATE_RESET( &candidates[ i ] );
1122 err = candidates[ i ].sr_err;
1127 if ( ncandidates == 0 ) {
1130 meta_back_conn_free( mc );
1133 meta_back_release_conn( op, mc );
1136 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1137 rs->sr_text = "Unable to select valid candidates";
1139 if ( sendok & LDAP_BACK_SENDERR ) {
1140 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1141 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1143 send_ldap_result( op, rs );
1145 rs->sr_matched = NULL;
1155 * looks in cache, if any
1157 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1158 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1161 if ( op_type == META_OP_REQUIRE_SINGLE ) {
1162 metatarget_t *mt = NULL;
1163 metasingleconn_t *msc = NULL;
1167 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1168 META_CANDIDATE_RESET( &candidates[ j ] );
1172 * tries to get a unique candidate
1173 * (takes care of default target)
1175 if ( i == META_TARGET_NONE ) {
1176 i = meta_back_get_candidate( op, rs, &ndn );
1178 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1179 i = meta_back_get_candidate( op, rs, &pndn );
1182 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1184 meta_back_release_conn( op, mc );
1187 if ( sendok & LDAP_BACK_SENDERR ) {
1188 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1189 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1191 send_ldap_result( op, rs );
1193 rs->sr_matched = NULL;
1200 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1203 meta_back_release_conn( op, mc );
1206 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1207 rs->sr_text = "cross-target rename not supported";
1208 if ( sendok & LDAP_BACK_SENDERR ) {
1209 send_ldap_result( op, rs );
1216 Debug( LDAP_DEBUG_TRACE,
1217 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1218 i, op->o_req_ndn.bv_val, 0 );
1221 /* Retries searching for a metaconn in the avl tree
1222 * the reason is that the connection might have been
1223 * created by meta_back_get_candidate() */
1224 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1226 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1227 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1228 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1230 /* catch taint errors */
1231 assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1233 /* Don't reuse connections while they're still binding */
1234 if ( META_BACK_CONN_CREATING( &mc->mc_conns[ i ] )
1235 || LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) )
1237 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1238 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1239 ldap_pvt_thread_yield();
1249 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1252 /* Looks like we didn't get a bind. Open a new session... */
1254 assert( new_conn == 0 );
1255 mc = metaconn_alloc( op );
1256 mc->mc_conn = mc_curr.mc_conn;
1257 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1259 if ( sendok & LDAP_BACK_BINDING ) {
1260 LDAP_BACK_CONN_BINDING_SET( mc );
1262 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1263 LDAP_BACK_CONN_ISPRIV_SET( mc );
1269 * Clear all other candidates
1271 ( void )meta_clear_unused_candidates( op, i );
1273 mt = mi->mi_targets[ i ];
1274 msc = &mc->mc_conns[ i ];
1277 * The target is activated; if needed, it is
1278 * also init'd. In case of error, meta_back_init_one_conn
1279 * sends the appropriate result.
1281 err = meta_back_init_one_conn( op, rs, mc, i,
1282 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn );
1283 if ( err != LDAP_SUCCESS ) {
1285 * FIXME: in case one target cannot
1286 * be init'd, should the other ones
1289 META_CANDIDATE_RESET( &candidates[ i ] );
1292 meta_back_conn_free( mc );
1295 meta_back_release_conn( op, mc );
1300 candidates[ i ].sr_err = LDAP_SUCCESS;
1301 META_CANDIDATE_SET( &candidates[ i ] );
1309 * if no unique candidate ...
1313 /* Looks like we didn't get a bind. Open a new session... */
1315 assert( new_conn == 0 );
1316 mc = metaconn_alloc( op );
1317 mc->mc_conn = mc_curr.mc_conn;
1318 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1320 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1321 LDAP_BACK_CONN_ISPRIV_SET( mc );
1325 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1326 metatarget_t *mt = mi->mi_targets[ i ];
1328 META_CANDIDATE_RESET( &candidates[ i ] );
1331 || meta_back_is_candidate( mt, &op->o_req_ndn,
1332 LDAP_SCOPE_SUBTREE ) )
1336 * The target is activated; if needed, it is
1339 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1340 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1341 LDAP_BACK_DONTSEND, !new_conn );
1342 if ( lerr == LDAP_SUCCESS ) {
1343 META_CANDIDATE_SET( &candidates[ i ] );
1344 candidates[ i ].sr_err = LDAP_SUCCESS;
1347 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1348 op->o_log_prefix, i, 0 );
1350 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1351 META_CANDIDATE_SET( &candidates[ i ] );
1352 candidates[ i ].sr_err = LDAP_UNAVAILABLE;
1354 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1355 op->o_log_prefix, i,
1356 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1361 * FIXME: in case one target cannot
1362 * be init'd, should the other ones
1366 ( void )meta_clear_one_candidate( op, mc, i );
1368 /* leave the target candidate, but record the error for later use */
1369 candidates[ i ].sr_err = lerr;
1372 if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1373 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined err=%d\n",
1374 op->o_log_prefix, i, lerr );
1377 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed err=%d\n",
1378 op->o_log_prefix, i, lerr );
1381 if ( META_BACK_ONERR_STOP( mi ) ) {
1382 if ( sendok & LDAP_BACK_SENDERR ) {
1383 send_ldap_result( op, rs );
1388 meta_back_conn_free( mc );
1391 meta_back_release_conn( op, mc );
1403 ( void )meta_clear_one_candidate( op, mc, i );
1408 if ( ncandidates == 0 ) {
1411 meta_back_conn_free( mc );
1414 meta_back_release_conn( op, mc );
1417 if ( rs->sr_err == LDAP_SUCCESS ) {
1418 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1419 rs->sr_text = "Unable to select valid candidates";
1422 if ( sendok & LDAP_BACK_SENDERR ) {
1423 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1424 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1426 send_ldap_result( op, rs );
1428 rs->sr_matched = NULL;
1436 /* clear out meta_back_init_one_conn non-fatal errors */
1437 rs->sr_err = LDAP_SUCCESS;
1440 /* touch the timestamp */
1441 if ( mi->mi_idle_timeout != 0 ) {
1442 mc->mc_time = op->o_time;
1446 if ( mi->mi_conn_ttl ) {
1447 mc->mc_create_time = op->o_time;
1451 * Inserts the newly created metaconn in the avl tree
1453 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1454 #if META_BACK_PRINT_CONNTREE > 0
1455 meta_back_print_conntree( mi->mi_conninfo.lai_tree, ">>> meta_back_getconn" );
1456 #endif /* META_BACK_PRINT_CONNTREE */
1457 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1458 meta_back_conndn_cmp, meta_back_conndn_dup );
1459 #if META_BACK_PRINT_CONNTREE > 0
1460 meta_back_print_conntree( mi->mi_conninfo.lai_tree, ">>> meta_back_getconn" );
1461 #endif /* META_BACK_PRINT_CONNTREE */
1462 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1465 * Err could be -1 in case a duplicate metaconn is inserted
1472 /* duplicate: free and try to get the newly created one */
1473 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1475 meta_back_conn_free( mc );
1481 LDAP_BACK_CONN_TAINTED_SET( mc );
1485 Debug( LDAP_DEBUG_ANY,
1486 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1487 op->o_log_prefix, ncandidates,
1488 LDAP_BACK_PCONN_ID( mc ) );
1491 meta_back_conn_free( mc );
1493 rs->sr_err = LDAP_OTHER;
1494 rs->sr_text = "proxy bind collision";
1495 if ( sendok & LDAP_BACK_SENDERR ) {
1496 send_ldap_result( op, rs );
1502 Debug( LDAP_DEBUG_TRACE,
1503 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1504 op->o_log_prefix, ncandidates,
1505 LDAP_BACK_PCONN_ID( mc ) );
1508 Debug( LDAP_DEBUG_TRACE,
1509 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1510 op->o_log_prefix, ncandidates,
1511 LDAP_BACK_PCONN_ID( mc ) );
1518 meta_back_release_conn_lock(
1523 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1525 assert( mc != NULL );
1528 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1530 assert( mc->mc_refcnt > 0 );
1532 /* NOTE: the connection is removed if either it is tainted
1533 * or if it is shared and no one else is using it. This needs
1534 * to occur because for intrinsic reasons cached connections
1535 * that are not privileged would live forever and pollute
1536 * the connection space (and eat up resources). Maybe this
1537 * should be configurable... */
1538 if ( LDAP_BACK_CONN_TAINTED( mc ) ||
1539 ( !LDAP_BACK_CONN_ISPRIV( mc ) && LDAP_BACK_PCONN_ISPRIV( mc ) && mc->mc_refcnt == 0 ) )
1543 Debug( LDAP_DEBUG_TRACE, "%s meta_back_release_conn: mc=%p conn=%ld tainted.\n",
1544 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc ) );
1545 #if META_BACK_PRINT_CONNTREE > 0
1546 meta_back_print_conntree( mi->mi_conninfo.lai_tree, ">>> meta_back_release_conn" );
1547 #endif /* META_BACK_PRINT_CONNTREE */
1548 tmpmc = avl_delete( &mi->mi_conninfo.lai_tree,
1549 ( caddr_t )mc, meta_back_conndnmc_cmp );
1550 #if META_BACK_PRINT_CONNTREE > 0
1551 meta_back_print_conntree( mi->mi_conninfo.lai_tree, "<<< meta_back_release_conn" );
1552 #endif /* META_BACK_PRINT_CONNTREE */
1553 if ( tmpmc == NULL ) {
1554 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_release_conn: unable to find mc=%p\n",
1555 op->o_log_prefix, (void *)mc, 0 );
1557 assert( tmpmc == mc );
1560 if ( mc->mc_refcnt == 0 ) {
1561 meta_back_conn_free( mc );
1566 if ( mc != NULL && LDAP_BACK_CONN_BINDING( mc ) ) {
1567 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1571 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1576 meta_back_quarantine(
1581 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1582 metatarget_t *mt = mi->mi_targets[ candidate ];
1584 slap_retry_info_t *ri = &mt->mt_quarantine;
1586 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1588 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1589 time_t new_last = slap_get_time();
1591 switch ( mt->mt_isquarantined ) {
1592 case LDAP_BACK_FQ_NO:
1593 if ( ri->ri_last == new_last ) {
1597 Debug( LDAP_DEBUG_ANY,
1598 "%s meta_back_quarantine[%d]: enter.\n",
1599 op->o_log_prefix, candidate, 0 );
1605 case LDAP_BACK_FQ_RETRYING:
1606 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1607 char buf[ SLAP_TEXT_BUFLEN ];
1609 snprintf( buf, sizeof( buf ),
1610 "meta_back_quarantine[%d]: block #%d try #%d failed",
1611 candidate, ri->ri_idx, ri->ri_count );
1612 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1613 op->o_log_prefix, buf, 0 );
1617 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1618 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1629 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1630 ri->ri_last = new_last;
1632 } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1633 Debug( LDAP_DEBUG_ANY,
1634 "%s meta_back_quarantine[%d]: exit.\n",
1635 op->o_log_prefix, candidate, 0 );
1637 if ( mi->mi_quarantine_f ) {
1638 (void)mi->mi_quarantine_f( mi, candidate,
1639 mi->mi_quarantine_p );
1644 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1648 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );