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 #ifndef PRINT_CONNTREE
41 #define PRINT_CONNTREE 0
42 #endif /* !PRINT_CONNTREE */
45 * meta_back_conndn_cmp
47 * compares two struct metaconn based on the value of the conn pointer
48 * and of the local DN; used by avl stuff
55 metaconn_t *mc1 = ( metaconn_t * )c1;
56 metaconn_t *mc2 = ( metaconn_t * )c2;
59 /* If local DNs don't match, it is definitely not a match */
60 /* For shared sessions, conn is NULL. Only explicitly
61 * bound sessions will have non-NULL conn.
63 rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
65 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
72 * meta_back_conndnmc_cmp
74 * compares two struct metaconn based on the value of the conn pointer,
75 * the local DN and the struct pointer; used by avl stuff
78 meta_back_conndnmc_cmp(
82 metaconn_t *mc1 = ( metaconn_t * )c1;
83 metaconn_t *mc2 = ( metaconn_t * )c2;
86 /* If local DNs don't match, it is definitely not a match */
87 /* For shared sessions, conn is NULL. Only explicitly
88 * bound sessions will have non-NULL conn.
90 rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
92 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
94 rc = SLAP_PTRCMP( mc1, mc2 );
104 * compares two struct metaconn based on the value of the conn pointer;
112 metaconn_t *mc1 = ( metaconn_t * )c1;
113 metaconn_t *mc2 = ( metaconn_t * )c2;
115 /* For shared sessions, conn is NULL. Only explicitly
116 * bound sessions will have non-NULL conn.
118 return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
122 * meta_back_conndn_dup
124 * returns -1 in case a duplicate struct metaconn has been inserted;
128 meta_back_conndn_dup(
132 metaconn_t *mc1 = ( metaconn_t * )c1;
133 metaconn_t *mc2 = ( metaconn_t * )c2;
135 /* Cannot have more than one shared session with same DN */
136 if ( mc1->mc_conn == mc2->mc_conn &&
137 dn_match( &mc1->mc_local_ndn, &mc2->mc_local_ndn ) )
146 * Debug stuff (got it from libavl)
148 #if PRINT_CONNTREE > 0
150 ravl_print( Avlnode *root, int depth )
159 ravl_print( root->avl_right, depth + 1 );
161 for ( i = 0; i < depth; i++ ) {
162 fprintf( stderr, "-" );
165 mc = (metaconn_t *)root->avl_data;
166 fprintf( stderr, "mc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
168 mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "",
170 avl_bf2str( root->avl_bf ), mc->mc_refcnt );
172 ravl_print( root->avl_left, depth + 1 );
176 myprint( Avlnode *root, char *msg )
178 fprintf( stderr, "========> %s\n", msg );
181 fprintf( stderr, "\tNULL\n" );
184 ravl_print( root, 0 );
187 fprintf( stderr, "<======== %s\n", msg );
189 #endif /* PRINT_CONNTREE */
197 * Allocates a connection structure, making room for all the referenced targets
203 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
205 int i, ntargets = mi->mi_ntargets;
207 assert( ntargets > 0 );
209 /* malloc all in one */
210 mc = ( metaconn_t * )ch_calloc( 1, sizeof( metaconn_t )
211 + sizeof( metasingleconn_t ) * ntargets );
216 for ( i = 0; i < ntargets; i++ ) {
217 mc->mc_conns[ i ].msc_info = mi;
220 mc->mc_authz_target = META_BOUND_NONE;
227 * meta_back_init_one_conn
229 * Initializes one connection
232 meta_back_init_one_conn(
238 ldap_back_send_t sendok )
240 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
241 metatarget_t *mt = mi->mi_targets[ candidate ];
242 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
245 int isauthz = ( candidate == mc->mc_authz_target );
248 #endif /* HAVE_TLS */
250 /* if the server is quarantined, and
251 * - the current interval did not expire yet, or
252 * - no more retries should occur,
253 * don't return the connection */
254 if ( mt->mt_isquarantined ) {
255 slap_retry_info_t *ri = &mt->mt_quarantine;
258 if ( mt->mt_isquarantined == LDAP_BACK_FQ_YES ) {
259 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
260 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
262 if ( LogTest( LDAP_DEBUG_ANY ) ) {
263 char buf[ SLAP_TEXT_BUFLEN ];
265 snprintf( buf, sizeof( buf ),
266 "meta_back_init_one_conn[%d]: quarantine "
267 "retry block #%d try #%d",
268 candidate, ri->ri_idx, ri->ri_count );
269 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
270 op->o_log_prefix, buf, 0 );
273 mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
278 rs->sr_err = LDAP_UNAVAILABLE;
279 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
280 send_ldap_result( op, rs );
289 if ( msc->msc_ld != NULL ) {
290 return rs->sr_err = LDAP_SUCCESS;
293 msc->msc_mscflags = 0;
296 * Attempts to initialize the connection to the target ds
298 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
299 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
301 is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
302 #endif /* HAVE_TLS */
303 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
304 if ( rs->sr_err != LDAP_SUCCESS ) {
309 * Set LDAP version. This will always succeed: If the client
310 * bound with a particular version, then so can we.
312 if ( mt->mt_version != 0 ) {
313 version = mt->mt_version;
315 } else if ( op->o_conn->c_protocol != 0 ) {
316 version = op->o_conn->c_protocol;
319 version = LDAP_VERSION3;
321 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
322 ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
324 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
325 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
326 LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
329 /* start TLS ("tls [try-]{start|propagate}" statement) */
330 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
333 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
335 * use asynchronous StartTLS; in case, chase referral
336 * FIXME: OpenLDAP does not return referral on StartTLS yet
340 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
341 if ( rs->sr_err == LDAP_SUCCESS ) {
342 LDAPMessage *res = NULL;
343 int rc, nretries = mt->mt_nretries;
346 LDAP_BACK_TV_SET( &tv );
349 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
352 rs->sr_err = LDAP_OTHER;
356 if ( nretries != 0 ) {
357 if ( nretries > 0 ) {
360 LDAP_BACK_TV_SET( &tv );
363 rs->sr_err = LDAP_OTHER;
367 /* only touch when activity actually took place... */
368 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
369 msc->msc_time = op->o_time;
374 if ( rc == LDAP_RES_EXTENDED ) {
375 struct berval *data = NULL;
377 /* NOTE: right now, data is unused, so don't get it */
378 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
379 NULL, NULL /* &data */ , 0 );
380 if ( rs->sr_err == LDAP_SUCCESS ) {
383 /* FIXME: matched? referrals? response controls? */
384 rs->sr_err = ldap_parse_result( msc->msc_ld, res,
385 &err, NULL, NULL, NULL, NULL, 1 );
388 if ( rs->sr_err == LDAP_SUCCESS ) {
392 /* FIXME: in case a referral
393 * is returned, should we try
394 * using it instead of the
396 if ( rs->sr_err == LDAP_SUCCESS ) {
397 ldap_install_tls( msc->msc_ld );
399 } else if ( rs->sr_err == LDAP_REFERRAL ) {
400 /* FIXME: LDAP_OPERATIONS_ERROR? */
401 rs->sr_err = LDAP_OTHER;
402 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
406 if ( data->bv_val ) {
407 ber_memfree( data->bv_val );
414 rs->sr_err = LDAP_OTHER;
421 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
423 * use synchronous StartTLS
425 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
426 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
428 /* if StartTLS is requested, only attempt it if the URL
429 * is not "ldaps://"; this may occur not only in case
430 * of misconfiguration, but also when used in the chain
431 * overlay, where the "uri" can be parsed out of a referral */
432 if ( rs->sr_err == LDAP_SERVER_DOWN
433 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
435 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
440 #endif /* HAVE_TLS */
443 * Set the network timeout if set
445 if ( mt->mt_network_timeout != 0 ) {
446 struct timeval network_timeout;
448 network_timeout.tv_usec = 0;
449 network_timeout.tv_sec = mt->mt_network_timeout;
451 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
452 (void *)&network_timeout );
456 * If the connection DN is not null, an attempt to rewrite it is made
460 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
461 ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
462 if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
463 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
467 ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
470 LDAP_BACK_CONN_ISPRIV_SET( msc );
473 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
474 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
475 ber_memfree_x( msc->msc_cred.bv_val, NULL );
476 BER_BVZERO( &msc->msc_cred );
478 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
479 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
480 BER_BVZERO( &msc->msc_bound_ndn );
482 if ( !BER_BVISEMPTY( &op->o_ndn )
483 && SLAP_IS_AUTHZ_BACKEND( op )
487 dc.conn = op->o_conn;
492 * Rewrite the bind dn if needed
494 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
495 &msc->msc_bound_ndn ) )
497 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
502 /* copy the DN if needed */
503 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
504 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
508 ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
512 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
515 if ( rs->sr_err == LDAP_SUCCESS ) {
517 * Sets a cookie for the rewrite session
519 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
522 rs->sr_err = slap_map_api2result( rs );
523 if ( sendok & LDAP_BACK_SENDERR ) {
524 send_ldap_result( op, rs );
535 * Retries one connection
543 ldap_back_send_t sendok )
545 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
546 metatarget_t *mt = mi->mi_targets[ candidate ];
547 metaconn_t *mc = *mcp;
548 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
549 int rc = LDAP_UNAVAILABLE,
552 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
553 binding = LDAP_BACK_CONN_BINDING( msc );
555 assert( mc->mc_refcnt > 0 );
556 if ( mc->mc_refcnt == 1 ) {
557 if ( LogTest( LDAP_DEBUG_ANY ) ) {
558 char buf[ SLAP_TEXT_BUFLEN ];
560 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
561 snprintf( buf, sizeof( buf ),
562 "retrying URI=\"%s\" DN=\"%s\"",
564 BER_BVISNULL( &msc->msc_bound_ndn ) ?
565 "" : msc->msc_bound_ndn.bv_val );
566 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
568 Debug( LDAP_DEBUG_ANY,
569 "%s meta_back_retry[%d]: %s.\n",
570 op->o_log_prefix, candidate, buf );
573 meta_clear_one_candidate( msc );
574 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
576 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
578 /* mc here must be the regular mc, reset and ready for init */
579 rc = meta_back_init_one_conn( op, rs, mc, candidate,
580 LDAP_BACK_CONN_ISPRIV( mc ), sendok );
582 LDAP_BACK_CONN_BINDING_SET( msc );
585 if ( rc == LDAP_SUCCESS ) {
586 rc = meta_back_single_dobind( op, rs, mcp, candidate,
587 sendok, mt->mt_nretries, 0 );
589 Debug( LDAP_DEBUG_ANY,
590 "%s meta_back_retry[%d]: "
591 "meta_back_single_dobind=%d\n",
592 op->o_log_prefix, candidate, rc );
593 if ( rc == LDAP_SUCCESS ) {
594 if ( be_isroot( op ) ) {
595 LDAP_BACK_CONN_ISBOUND_SET( msc );
597 LDAP_BACK_CONN_ISANON_SET( msc );
603 if ( rc != LDAP_SUCCESS ) {
604 SlapReply *candidates = meta_back_candidates_get( op );
606 candidates[ candidate ].sr_err = rc;
608 if ( *mcp != NULL ) {
610 LDAP_BACK_CONN_BINDING_CLEAR( msc );
612 LDAP_BACK_CONN_TAINTED_SET( mc );
613 /* only release if mandatory; otherwise
614 * let the caller do what's best before
616 if ( META_BACK_ONERR_STOP( mi ) ) {
617 meta_back_release_conn_lock( op, mc, 0 );
625 send_ldap_result( op, rs );
629 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
630 meta_back_quarantine( op, rs, candidate );
633 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
635 return rc == LDAP_SUCCESS ? 1 : 0;
639 * callback for unique candidate selection
642 meta_back_conn_cb( Operation *op, SlapReply *rs )
644 assert( op->o_tag == LDAP_REQ_SEARCH );
646 switch ( rs->sr_type ) {
648 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
664 meta_back_get_candidate(
669 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
673 * tries to get a unique candidate
674 * (takes care of default target)
676 candidate = meta_back_select_unique_candidate( mi, ndn );
679 * if any is found, inits the connection
681 if ( candidate == META_TARGET_NONE ) {
682 rs->sr_err = LDAP_NO_SUCH_OBJECT;
683 rs->sr_text = "no suitable candidate target found";
685 } else if ( candidate == META_TARGET_MULTIPLE ) {
688 SlapReply rs2 = { 0 };
689 slap_callback cb2 = { 0 };
692 /* try to get a unique match for the request ndn
693 * among the multiple candidates available */
694 op2.o_tag = LDAP_REQ_SEARCH;
696 op2.o_req_ndn = *ndn;
697 op2.ors_scope = LDAP_SCOPE_BASE;
698 op2.ors_deref = LDAP_DEREF_NEVER;
699 op2.ors_attrs = slap_anlist_no_attrs;
700 op2.ors_attrsonly = 0;
701 op2.ors_limit = NULL;
703 op2.ors_tlimit = SLAP_NO_LIMIT;
705 f.f_choice = LDAP_FILTER_PRESENT;
706 f.f_desc = slap_schema.si_ad_objectClass;
708 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
710 op2.o_callback = &cb2;
711 cb2.sc_response = meta_back_conn_cb;
712 cb2.sc_private = (void *)&candidate;
714 rc = op->o_bd->be_search( &op2, &rs2 );
716 switch ( rs2.sr_err ) {
719 rs->sr_err = rs2.sr_err;
722 case LDAP_SIZELIMIT_EXCEEDED:
723 /* if multiple candidates can serve the operation,
724 * and a default target is defined, and it is
725 * a candidate, try using it (FIXME: YMMV) */
726 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
727 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
728 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
730 candidate = mi->mi_defaulttarget;
731 rs->sr_err = LDAP_SUCCESS;
735 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
736 rs->sr_text = "cannot select unique candidate target";
742 rs->sr_err = LDAP_SUCCESS;
748 static void *meta_back_candidates_dummy;
751 meta_back_candidates_keyfree(
755 metacandidates_t *mc = (metacandidates_t *)data;
757 ber_memfree_x( mc->mc_candidates, NULL );
758 ber_memfree_x( data, NULL );
762 meta_back_candidates_get( Operation *op )
764 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
765 metacandidates_t *mc;
767 if ( op->o_threadctx ) {
770 ldap_pvt_thread_pool_getkey( op->o_threadctx,
771 &meta_back_candidates_dummy, &data, NULL );
772 mc = (metacandidates_t *)data;
775 mc = mi->mi_candidates;
779 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
780 mc->mc_ntargets = mi->mi_ntargets;
781 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
782 if ( op->o_threadctx ) {
786 ldap_pvt_thread_pool_setkey( op->o_threadctx,
787 &meta_back_candidates_dummy, data,
788 meta_back_candidates_keyfree );
791 mi->mi_candidates = mc;
794 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
795 /* NOTE: in the future, may want to allow back-config
796 * to add/remove targets from back-meta... */
797 mc->mc_candidates = ch_realloc( mc->mc_candidates,
798 sizeof( SlapReply ) * mi->mi_ntargets );
799 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
800 sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
801 mc->mc_ntargets = mi->mi_ntargets;
804 return mc->mc_candidates;
810 * Prepares the connection structure
814 * - determine what DN is being requested:
816 * op requires candidate checks
818 * add unique parent of o_req_ndn
819 * bind unique^*[/all] o_req_ndn [no check]
820 * compare unique^+ o_req_ndn
821 * delete unique o_req_ndn
822 * modify unique o_req_ndn
823 * search any o_req_ndn
824 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
826 * - for ops that require the candidate to be unique, in case of multiple
827 * occurrences an internal search with sizeLimit=1 is performed
828 * if a unique candidate can actually be determined. If none is found,
829 * the operation aborts; if multiple are found, the default target
830 * is used if defined and candidate; otherwise the operation aborts.
832 * *^note: actually, the bind operation is handled much like a search;
833 * i.e. the bind is broadcast to all candidate targets.
835 * +^note: actually, the compare operation is handled much like a search;
836 * i.e. the compare is broadcast to all candidate targets, while checking
837 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
845 ldap_back_send_t sendok )
847 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
848 metaconn_t *mc = NULL,
850 int cached = META_TARGET_NONE,
851 i = META_TARGET_NONE,
857 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
861 META_DNTYPE_NEWPARENT
862 } dn_type = META_DNTYPE_ENTRY;
863 struct berval ndn = op->o_req_ndn,
866 SlapReply *candidates = meta_back_candidates_get( op );
868 /* Internal searches are privileged and shared. So is root. */
869 /* FIXME: there seem to be concurrency issues */
870 if ( op->o_do_not_cache || be_isroot( op ) ) {
871 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
872 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
873 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
876 mc_curr.mc_local_ndn = op->o_ndn;
878 /* Explicit binds must not be shared */
879 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
880 mc_curr.mc_conn = op->o_conn;
883 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
887 /* Explicit Bind requests always get their own conn */
888 if ( !( sendok & LDAP_BACK_BINDING ) ) {
889 /* Searches for a metaconn in the avl tree */
891 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
892 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
893 (caddr_t)&mc_curr, meta_back_conndn_cmp );
895 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
896 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
898 /* don't let anyone else use this expired connection */
899 (void)avl_delete( &mi->mi_conninfo.lai_tree,
900 (caddr_t)mc, meta_back_conndnmc_cmp );
901 LDAP_BACK_CONN_TAINTED_SET( mc );
903 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired.\n",
904 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
907 /* Don't reuse connections while they're still binding */
908 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
909 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
910 ldap_pvt_thread_yield();
916 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
919 switch ( op->o_tag ) {
921 /* if we go to selection, the entry must not exist,
922 * and we must be able to resolve the parent */
923 dn_type = META_DNTYPE_PARENT;
924 dnParent( &ndn, &pndn );
927 case LDAP_REQ_MODRDN:
928 /* if nnewSuperior is not NULL, it must resolve
929 * to the same candidate as the req_ndn */
930 if ( op->orr_nnewSup ) {
931 dn_type = META_DNTYPE_NEWPARENT;
936 /* if bound as rootdn, the backend must bind to all targets
937 * with the administrative identity */
938 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
939 op_type = META_OP_REQUIRE_ALL;
943 case LDAP_REQ_DELETE:
944 case LDAP_REQ_MODIFY:
945 /* just a unique candidate */
948 case LDAP_REQ_COMPARE:
949 case LDAP_REQ_SEARCH:
950 /* allow multiple candidates for the searchBase */
951 op_type = META_OP_ALLOW_MULTIPLE;
955 /* right now, just break (exop?) */
960 * require all connections ...
962 if ( op_type == META_OP_REQUIRE_ALL ) {
964 /* Looks like we didn't get a bind. Open a new session... */
966 assert( new_conn == 0 );
967 mc = metaconn_alloc( op );
968 mc->mc_conn = mc_curr.mc_conn;
969 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
971 if ( sendok & LDAP_BACK_BINDING ) {
972 LDAP_BACK_CONN_BINDING_SET( mc );
976 for ( i = 0; i < mi->mi_ntargets; i++ ) {
978 * The target is activated; if needed, it is
981 candidates[ i ].sr_err = meta_back_init_one_conn( op,
982 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
984 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
985 META_CANDIDATE_SET( &candidates[ i ] );
991 * FIXME: in case one target cannot
992 * be init'd, should the other ones
995 META_CANDIDATE_RESET( &candidates[ i ] );
996 err = candidates[ i ].sr_err;
1001 if ( ncandidates == 0 ) {
1004 meta_back_conn_free( mc );
1007 meta_back_release_conn( op, mc );
1010 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1011 rs->sr_text = "Unable to select valid candidates";
1013 if ( sendok & LDAP_BACK_SENDERR ) {
1014 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1015 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1017 send_ldap_result( op, rs );
1019 rs->sr_matched = NULL;
1029 * looks in cache, if any
1031 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1032 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1035 if ( op_type == META_OP_REQUIRE_SINGLE ) {
1036 metatarget_t *mt = NULL;
1037 metasingleconn_t *msc = NULL;
1041 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1042 META_CANDIDATE_RESET( &candidates[ j ] );
1046 * tries to get a unique candidate
1047 * (takes care of default target)
1049 if ( i == META_TARGET_NONE ) {
1050 i = meta_back_get_candidate( op, rs, &ndn );
1052 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1053 i = meta_back_get_candidate( op, rs, &pndn );
1056 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1058 meta_back_release_conn( op, mc );
1061 if ( sendok & LDAP_BACK_SENDERR ) {
1062 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1063 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1065 send_ldap_result( op, rs );
1067 rs->sr_matched = NULL;
1074 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1077 meta_back_release_conn( op, mc );
1080 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1081 rs->sr_text = "cross-target rename not supported";
1082 if ( sendok & LDAP_BACK_SENDERR ) {
1083 send_ldap_result( op, rs );
1090 Debug( LDAP_DEBUG_TRACE,
1091 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1092 i, op->o_req_ndn.bv_val, 0 );
1095 /* Retries searching for a metaconn in the avl tree
1096 * the reason is that the connection might have been
1097 * created by meta_back_get_candidate() */
1098 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1100 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1101 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1102 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1104 /* Don't reuse connections while they're still binding */
1105 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1106 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1107 ldap_pvt_thread_yield();
1113 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1116 /* Looks like we didn't get a bind. Open a new session... */
1118 assert( new_conn == 0 );
1119 mc = metaconn_alloc( op );
1120 mc->mc_conn = mc_curr.mc_conn;
1121 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1123 if ( sendok & LDAP_BACK_BINDING ) {
1124 LDAP_BACK_CONN_BINDING_SET( mc );
1130 * Clear all other candidates
1132 ( void )meta_clear_unused_candidates( op, i );
1134 mt = mi->mi_targets[ i ];
1135 msc = &mc->mc_conns[ i ];
1138 * The target is activated; if needed, it is
1139 * also init'd. In case of error, meta_back_init_one_conn
1140 * sends the appropriate result.
1142 err = meta_back_init_one_conn( op, rs, mc, i,
1143 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
1144 if ( err != LDAP_SUCCESS ) {
1146 * FIXME: in case one target cannot
1147 * be init'd, should the other ones
1150 META_CANDIDATE_RESET( &candidates[ i ] );
1153 meta_back_conn_free( mc );
1156 meta_back_release_conn( op, mc );
1161 candidates[ i ].sr_err = LDAP_SUCCESS;
1162 META_CANDIDATE_SET( &candidates[ i ] );
1170 * if no unique candidate ...
1174 /* Looks like we didn't get a bind. Open a new session... */
1176 assert( new_conn == 0 );
1177 mc = metaconn_alloc( op );
1178 mc->mc_conn = mc_curr.mc_conn;
1179 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1181 if ( sendok & LDAP_BACK_BINDING ) {
1182 LDAP_BACK_CONN_BINDING_SET( mc );
1186 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1187 metatarget_t *mt = mi->mi_targets[ i ];
1188 metasingleconn_t *msc = &mc->mc_conns[ i ];
1191 || meta_back_is_candidate( mt, &op->o_req_ndn,
1192 LDAP_SCOPE_SUBTREE ) )
1196 * The target is activated; if needed, it is
1199 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1200 LDAP_BACK_CONN_ISPRIV( &mc_curr ), LDAP_BACK_DONTSEND );
1201 if ( lerr == LDAP_SUCCESS ) {
1202 META_CANDIDATE_SET( &candidates[ i ] );
1203 candidates[ i ].sr_err = LDAP_SUCCESS;
1206 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1207 op->o_log_prefix, i, 0 );
1209 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1210 META_CANDIDATE_SET( &candidates[ i ] );
1211 candidates[ i ].sr_err = LDAP_UNAVAILABLE;
1213 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1214 op->o_log_prefix, i,
1215 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1220 * FIXME: in case one target cannot
1221 * be init'd, should the other ones
1225 ( void )meta_clear_one_candidate( msc );
1227 /* leave the target candidate, but record the error for later use */
1228 candidates[ i ].sr_err = lerr;
1231 if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1232 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined: %d\n",
1233 op->o_log_prefix, i, lerr );
1236 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed: %d\n",
1237 op->o_log_prefix, i, lerr );
1240 if ( META_BACK_ONERR_STOP( mi ) ) {
1241 if ( sendok & LDAP_BACK_SENDERR ) {
1242 send_ldap_result( op, rs );
1247 meta_back_conn_free( mc );
1250 meta_back_release_conn( op, mc );
1262 ( void )meta_clear_one_candidate( msc );
1264 META_CANDIDATE_RESET( &candidates[ i ] );
1268 if ( ncandidates == 0 ) {
1271 meta_back_conn_free( mc );
1274 meta_back_release_conn( op, mc );
1277 if ( rs->sr_err == LDAP_SUCCESS ) {
1278 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1279 rs->sr_text = "Unable to select valid candidates";
1282 if ( sendok & LDAP_BACK_SENDERR ) {
1283 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1284 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1286 send_ldap_result( op, rs );
1288 rs->sr_matched = NULL;
1296 /* clear out meta_back_init_one_conn non-fatal errors */
1297 rs->sr_err = LDAP_SUCCESS;
1300 /* touch the timestamp */
1301 if ( mi->mi_idle_timeout != 0 ) {
1302 mc->mc_time = op->o_time;
1306 if ( mi->mi_conn_ttl ) {
1307 mc->mc_create_time = op->o_time;
1311 * Inserts the newly created metaconn in the avl tree
1313 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1314 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1315 meta_back_conndn_cmp, meta_back_conndn_dup );
1317 #if PRINT_CONNTREE > 0
1318 myprint( mi->mi_conninfo.lai_tree, "meta_back_getconn" );
1319 #endif /* PRINT_CONNTREE */
1321 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1324 * Err could be -1 in case a duplicate metaconn is inserted
1331 /* duplicate: free and try to get the newly created one */
1332 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1336 LDAP_BACK_CONN_TAINTED_SET( mc );
1340 Debug( LDAP_DEBUG_ANY,
1341 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1342 op->o_log_prefix, ncandidates,
1343 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1346 meta_back_conn_free( mc );
1348 rs->sr_err = LDAP_OTHER;
1349 rs->sr_text = "proxy bind collision";
1350 if ( sendok & LDAP_BACK_SENDERR ) {
1351 send_ldap_result( op, rs );
1357 Debug( LDAP_DEBUG_TRACE,
1358 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1359 op->o_log_prefix, ncandidates,
1360 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1363 Debug( LDAP_DEBUG_TRACE,
1364 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1365 op->o_log_prefix, ncandidates,
1366 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1373 meta_back_release_conn_lock(
1378 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1380 assert( mc != NULL );
1383 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1385 assert( mc->mc_refcnt > 0 );
1387 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1388 if ( LDAP_BACK_CONN_TAINTED( mc ) ) {
1389 Debug( LDAP_DEBUG_TRACE, "%s meta_back_release_conn: mc=%p conn=%ld tainted.\n",
1390 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1391 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1392 ( caddr_t )mc, meta_back_conndnmc_cmp );
1393 if ( mc->mc_refcnt == 0 ) {
1394 meta_back_conn_free( mc );
1398 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1403 meta_back_quarantine(
1408 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1409 metatarget_t *mt = mi->mi_targets[ candidate ];
1411 slap_retry_info_t *ri = &mt->mt_quarantine;
1413 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1415 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1416 time_t new_last = slap_get_time();
1418 switch ( mt->mt_isquarantined ) {
1419 case LDAP_BACK_FQ_NO:
1420 if ( ri->ri_last == new_last ) {
1424 Debug( LDAP_DEBUG_ANY,
1425 "%s meta_back_quarantine[%d]: enter.\n",
1426 op->o_log_prefix, candidate, 0 );
1432 case LDAP_BACK_FQ_RETRYING:
1433 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1434 char buf[ SLAP_TEXT_BUFLEN ];
1436 snprintf( buf, sizeof( buf ),
1437 "meta_back_quarantine[%d]: block #%d try #%d failed",
1438 candidate, ri->ri_idx, ri->ri_count );
1439 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1440 op->o_log_prefix, buf, 0 );
1444 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1445 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1456 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1457 ri->ri_last = new_last;
1459 } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1460 Debug( LDAP_DEBUG_ANY,
1461 "%s meta_back_quarantine[%d]: exit.\n",
1462 op->o_log_prefix, candidate, 0 );
1464 if ( mi->mi_quarantine_f ) {
1465 (void)mi->mi_quarantine_f( mi, candidate,
1466 mi->mi_quarantine_p );
1471 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1475 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );