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;
231 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
233 assert( mc != NULL );
235 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
237 if ( --mc->mc_refcnt == 0 ) {
238 meta_back_conn_free( mc );
241 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
245 * meta_back_init_one_conn
247 * Initializes one connection
250 meta_back_init_one_conn(
256 ldap_back_send_t sendok )
258 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
259 metatarget_t *mt = mi->mi_targets[ candidate ];
260 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
263 int isauthz = ( candidate == mc->mc_authz_target );
266 #endif /* HAVE_TLS */
268 /* if the server is quarantined, and
269 * - the current interval did not expire yet, or
270 * - no more retries should occur,
271 * don't return the connection */
272 if ( mt->mt_isquarantined ) {
273 slap_retry_info_t *ri = &mt->mt_quarantine;
276 if ( mt->mt_isquarantined == LDAP_BACK_FQ_YES ) {
277 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
278 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
280 Debug( LDAP_DEBUG_ANY,
281 "%s: meta_back_init_one_conn quarantine "
282 "retry block #%d try #%d.\n",
283 op->o_log_prefix, ri->ri_idx, ri->ri_count );
284 mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
289 rs->sr_err = LDAP_UNAVAILABLE;
290 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
291 send_ldap_result( op, rs );
300 if ( msc->msc_ld != NULL ) {
301 return rs->sr_err = LDAP_SUCCESS;
304 msc->msc_mscflags = 0;
307 * Attempts to initialize the connection to the target ds
309 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
310 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
312 is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
313 #endif /* HAVE_TLS */
314 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
315 if ( rs->sr_err != LDAP_SUCCESS ) {
320 * Set LDAP version. This will always succeed: If the client
321 * bound with a particular version, then so can we.
323 if ( mt->mt_version != 0 ) {
324 version = mt->mt_version;
326 } else if ( op->o_conn->c_protocol != 0 ) {
327 version = op->o_conn->c_protocol;
330 version = LDAP_VERSION3;
332 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
333 ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
335 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
336 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
337 LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
340 /* start TLS ("tls [try-]{start|propagate}" statement) */
341 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
344 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
346 * use asynchronous StartTLS; in case, chase referral
347 * FIXME: OpenLDAP does not return referral on StartTLS yet
351 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
352 if ( rs->sr_err == LDAP_SUCCESS ) {
353 LDAPMessage *res = NULL;
354 int rc, nretries = mt->mt_nretries;
357 LDAP_BACK_TV_SET( &tv );
360 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
362 rs->sr_err = LDAP_OTHER;
364 } else if ( rc == 0 ) {
365 if ( nretries != 0 ) {
366 if ( nretries > 0 ) {
369 LDAP_BACK_TV_SET( &tv );
372 rs->sr_err = LDAP_OTHER;
374 } else if ( rc == LDAP_RES_EXTENDED ) {
375 struct berval *data = NULL;
377 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
379 if ( rs->sr_err == LDAP_SUCCESS ) {
382 rs->sr_err = ldap_parse_result( msc->msc_ld, res,
383 &err, NULL, NULL, NULL, NULL, 1 );
386 if ( rs->sr_err == LDAP_SUCCESS ) {
390 /* FIXME: in case a referral
391 * is returned, should we try
392 * using it instead of the
394 if ( rs->sr_err == LDAP_SUCCESS ) {
395 ldap_install_tls( msc->msc_ld );
397 } else if ( rs->sr_err == LDAP_REFERRAL ) {
398 rs->sr_err = LDAP_OTHER;
399 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
403 if ( data->bv_val ) {
404 ber_memfree( data->bv_val );
411 rs->sr_err = LDAP_OTHER;
418 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
420 * use synchronous StartTLS
422 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
423 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
425 /* if StartTLS is requested, only attempt it if the URL
426 * is not "ldaps://"; this may occur not only in case
427 * of misconfiguration, but also when used in the chain
428 * overlay, where the "uri" can be parsed out of a referral */
429 if ( rs->sr_err == LDAP_SERVER_DOWN
430 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
432 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
437 #endif /* HAVE_TLS */
440 * Set the network timeout if set
442 if ( mt->mt_network_timeout != 0 ) {
443 struct timeval network_timeout;
445 network_timeout.tv_usec = 0;
446 network_timeout.tv_sec = mt->mt_network_timeout;
448 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
449 (void *)&network_timeout );
453 * If the connection DN is not null, an attempt to rewrite it is made
457 if ( !BER_BVISNULL( &mt->mt_pseudorootdn ) ) {
458 ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
459 if ( !BER_BVISNULL( &mt->mt_pseudorootpw ) ) {
460 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
464 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
467 LDAP_BACK_CONN_ISPRIV_SET( msc );
470 BER_BVZERO( &msc->msc_cred );
471 BER_BVZERO( &msc->msc_bound_ndn );
472 if ( !BER_BVISEMPTY( &op->o_ndn )
473 && SLAP_IS_AUTHZ_BACKEND( op )
477 dc.conn = op->o_conn;
482 * Rewrite the bind dn if needed
484 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
485 &msc->msc_bound_ndn ) )
487 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
492 /* copy the DN if needed */
493 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
494 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
498 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
502 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
505 if ( rs->sr_err == LDAP_SUCCESS ) {
507 * Sets a cookie for the rewrite session
509 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
512 rs->sr_err = slap_map_api2result( rs );
513 if ( sendok & LDAP_BACK_SENDERR ) {
514 send_ldap_result( op, rs );
525 * Retries one connection
533 ldap_back_send_t sendok )
535 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
536 metatarget_t *mt = mi->mi_targets[ candidate ];
537 metaconn_t *mc = *mcp;
538 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
539 int rc = LDAP_UNAVAILABLE,
540 binding = LDAP_BACK_CONN_BINDING( msc );
542 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
544 assert( mc->mc_refcnt > 0 );
545 if ( mc->mc_refcnt == 1 ) {
546 if ( LogTest( LDAP_DEBUG_ANY ) ) {
547 char buf[ SLAP_TEXT_BUFLEN ];
549 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
550 snprintf( buf, sizeof( buf ),
551 "retrying URI=\"%s\" DN=\"%s\"",
553 BER_BVISNULL( &msc->msc_bound_ndn ) ?
554 "" : msc->msc_bound_ndn.bv_val );
555 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
557 Debug( LDAP_DEBUG_ANY,
558 "%s meta_back_retry[%d]: %s.\n",
559 op->o_log_prefix, candidate, buf );
562 meta_clear_one_candidate( msc );
563 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
565 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
567 /* mc here must be the regular mc, reset and ready for init */
568 rc = meta_back_init_one_conn( op, rs, mc, candidate,
569 LDAP_BACK_CONN_ISPRIV( mc ), sendok );
571 LDAP_BACK_CONN_BINDING_SET( msc );
574 if ( rc == LDAP_SUCCESS ) {
575 rc = meta_back_single_dobind( op, rs, mcp, candidate,
576 sendok, mt->mt_nretries, 0 );
578 Debug( LDAP_DEBUG_ANY,
579 "%s meta_back_retry[%d]: "
580 "meta_back_single_dobind=%d\n",
581 op->o_log_prefix, candidate, rc );
582 if ( rc == LDAP_SUCCESS ) {
583 if ( be_isroot( op ) ) {
584 LDAP_BACK_CONN_ISBOUND_SET( msc );
586 LDAP_BACK_CONN_ISANON_SET( msc );
592 if ( rc != LDAP_SUCCESS ) {
593 SlapReply *candidates = meta_back_candidates_get( op );
595 candidates[ candidate ].sr_err = rc;
597 if ( *mcp != NULL ) {
599 LDAP_BACK_CONN_BINDING_CLEAR( msc );
601 LDAP_BACK_CONN_TAINTED_SET( mc );
602 /* only release if mandatory; otherwise
603 * let the caller do what's best before
605 if ( META_BACK_ONERR_STOP( mi ) ) {
606 meta_back_release_conn_lock( op, mc, 0 );
614 send_ldap_result( op, rs );
618 if ( META_BACK_QUARANTINE( mi ) ) {
619 meta_back_quarantine( op, rs, candidate, 0 );
622 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
624 return rc == LDAP_SUCCESS ? 1 : 0;
628 * callback for unique candidate selection
631 meta_back_conn_cb( Operation *op, SlapReply *rs )
633 assert( op->o_tag == LDAP_REQ_SEARCH );
635 switch ( rs->sr_type ) {
637 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
653 meta_back_get_candidate(
658 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
662 * tries to get a unique candidate
663 * (takes care of default target)
665 candidate = meta_back_select_unique_candidate( mi, ndn );
668 * if any is found, inits the connection
670 if ( candidate == META_TARGET_NONE ) {
671 rs->sr_err = LDAP_NO_SUCH_OBJECT;
672 rs->sr_text = "no suitable candidate target found";
674 } else if ( candidate == META_TARGET_MULTIPLE ) {
677 SlapReply rs2 = { 0 };
678 slap_callback cb2 = { 0 };
681 /* try to get a unique match for the request ndn
682 * among the multiple candidates available */
683 op2.o_tag = LDAP_REQ_SEARCH;
685 op2.o_req_ndn = *ndn;
686 op2.ors_scope = LDAP_SCOPE_BASE;
687 op2.ors_deref = LDAP_DEREF_NEVER;
688 op2.ors_attrs = slap_anlist_no_attrs;
689 op2.ors_attrsonly = 0;
690 op2.ors_limit = NULL;
692 op2.ors_tlimit = SLAP_NO_LIMIT;
694 f.f_choice = LDAP_FILTER_PRESENT;
695 f.f_desc = slap_schema.si_ad_objectClass;
697 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
699 op2.o_callback = &cb2;
700 cb2.sc_response = meta_back_conn_cb;
701 cb2.sc_private = (void *)&candidate;
703 rc = op->o_bd->be_search( &op2, &rs2 );
705 switch ( rs2.sr_err ) {
708 rs->sr_err = rs2.sr_err;
711 case LDAP_SIZELIMIT_EXCEEDED:
712 /* if multiple candidates can serve the operation,
713 * and a default target is defined, and it is
714 * a candidate, try using it (FIXME: YMMV) */
715 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
716 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
717 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
719 candidate = mi->mi_defaulttarget;
720 rs->sr_err = LDAP_SUCCESS;
724 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
725 rs->sr_text = "cannot select unique candidate target";
731 rs->sr_err = LDAP_SUCCESS;
737 static void *meta_back_candidates_dummy;
740 meta_back_candidates_keyfree(
744 metacandidates_t *mc = (metacandidates_t *)data;
746 ber_memfree_x( mc->mc_candidates, NULL );
747 ber_memfree_x( data, NULL );
751 meta_back_candidates_get( Operation *op )
753 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
754 metacandidates_t *mc;
756 if ( op->o_threadctx ) {
759 ldap_pvt_thread_pool_getkey( op->o_threadctx,
760 &meta_back_candidates_dummy, &data, NULL );
761 mc = (metacandidates_t *)data;
764 mc = mi->mi_candidates;
768 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
769 mc->mc_ntargets = mi->mi_ntargets;
770 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
771 if ( op->o_threadctx ) {
775 ldap_pvt_thread_pool_setkey( op->o_threadctx,
776 &meta_back_candidates_dummy, data,
777 meta_back_candidates_keyfree );
780 mi->mi_candidates = mc;
783 } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
784 /* NOTE: in the future, may want to allow back-config
785 * to add/remove targets from back-meta... */
786 mc->mc_candidates = ch_realloc( mc->mc_candidates,
787 sizeof( SlapReply ) * mi->mi_ntargets );
788 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
789 sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
790 mc->mc_ntargets = mi->mi_ntargets;
793 return mc->mc_candidates;
799 * Prepares the connection structure
803 * - determine what DN is being requested:
805 * op requires candidate checks
807 * add unique parent of o_req_ndn
808 * bind unique^*[/all] o_req_ndn [no check]
809 * compare unique^+ o_req_ndn
810 * delete unique o_req_ndn
811 * modify unique o_req_ndn
812 * search any o_req_ndn
813 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
815 * - for ops that require the candidate to be unique, in case of multiple
816 * occurrences an internal search with sizeLimit=1 is performed
817 * if a unique candidate can actually be determined. If none is found,
818 * the operation aborts; if multiple are found, the default target
819 * is used if defined and candidate; otherwise the operation aborts.
821 * *^note: actually, the bind operation is handled much like a search;
822 * i.e. the bind is broadcast to all candidate targets.
824 * +^note: actually, the compare operation is handled much like a search;
825 * i.e. the compare is broadcast to all candidate targets, while checking
826 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
834 ldap_back_send_t sendok )
836 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
837 metaconn_t *mc = NULL,
839 int cached = META_TARGET_NONE,
840 i = META_TARGET_NONE,
846 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
850 META_DNTYPE_NEWPARENT
851 } dn_type = META_DNTYPE_ENTRY;
852 struct berval ndn = op->o_req_ndn,
855 SlapReply *candidates = meta_back_candidates_get( op );
857 /* Internal searches are privileged and shared. So is root. */
858 /* FIXME: there seem to be concurrency issues */
859 if ( op->o_do_not_cache || be_isroot( op ) ) {
860 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
861 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
862 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
865 mc_curr.mc_local_ndn = op->o_ndn;
867 /* Explicit binds must not be shared */
868 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
869 mc_curr.mc_conn = op->o_conn;
872 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
876 /* Explicit Bind requests always get their own conn */
877 if ( !( sendok & LDAP_BACK_BINDING ) ) {
878 /* Searches for a metaconn in the avl tree */
881 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
882 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
883 (caddr_t)&mc_curr, meta_back_conndn_cmp );
885 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
886 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
888 /* don't let anyone else use this expired connection */
889 (void)avl_delete( &mi->mi_conninfo.lai_tree,
890 (caddr_t)mc, meta_back_conndnmc_cmp );
891 LDAP_BACK_CONN_TAINTED_SET( mc );
893 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired.\n",
894 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
897 /* Don't reuse connections while they're still binding */
898 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
899 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
900 ldap_pvt_thread_yield();
906 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
909 switch ( op->o_tag ) {
911 /* if we go to selection, the entry must not exist,
912 * and we must be able to resolve the parent */
913 dn_type = META_DNTYPE_PARENT;
914 dnParent( &ndn, &pndn );
917 case LDAP_REQ_MODRDN:
918 /* if nnewSuperior is not NULL, it must resolve
919 * to the same candidate as the req_ndn */
920 if ( op->orr_nnewSup ) {
921 dn_type = META_DNTYPE_NEWPARENT;
926 /* if bound as rootdn, the backend must bind to all targets
927 * with the administrative identity */
928 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
929 op_type = META_OP_REQUIRE_ALL;
933 case LDAP_REQ_DELETE:
934 case LDAP_REQ_MODIFY:
935 /* just a unique candidate */
938 case LDAP_REQ_COMPARE:
939 case LDAP_REQ_SEARCH:
940 /* allow multiple candidates for the searchBase */
941 op_type = META_OP_ALLOW_MULTIPLE;
945 /* right now, just break (exop?) */
950 * require all connections ...
952 if ( op_type == META_OP_REQUIRE_ALL ) {
954 /* Looks like we didn't get a bind. Open a new session... */
956 assert( new_conn == 0 );
957 mc = metaconn_alloc( op );
958 mc->mc_conn = mc_curr.mc_conn;
959 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
961 if ( sendok & LDAP_BACK_BINDING ) {
962 LDAP_BACK_CONN_BINDING_SET( mc );
966 for ( i = 0; i < mi->mi_ntargets; i++ ) {
968 * The target is activated; if needed, it is
971 candidates[ i ].sr_err = meta_back_init_one_conn( op,
972 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
974 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
975 META_CANDIDATE_SET( &candidates[ i ] );
981 * FIXME: in case one target cannot
982 * be init'd, should the other ones
985 META_CANDIDATE_RESET( &candidates[ i ] );
986 err = candidates[ i ].sr_err;
991 if ( ncandidates == 0 ) {
993 meta_back_freeconn( op, mc );
996 meta_back_release_conn( op, mc );
999 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1000 rs->sr_text = "Unable to select valid candidates";
1002 if ( sendok & LDAP_BACK_SENDERR ) {
1003 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1004 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1006 send_ldap_result( op, rs );
1008 rs->sr_matched = NULL;
1018 * looks in cache, if any
1020 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1021 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1024 if ( op_type == META_OP_REQUIRE_SINGLE ) {
1025 metatarget_t *mt = NULL;
1026 metasingleconn_t *msc = NULL;
1030 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1031 META_CANDIDATE_RESET( &candidates[ j ] );
1035 * tries to get a unique candidate
1036 * (takes care of default target)
1038 if ( i == META_TARGET_NONE ) {
1039 i = meta_back_get_candidate( op, rs, &ndn );
1041 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1042 i = meta_back_get_candidate( op, rs, &pndn );
1045 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1047 meta_back_release_conn( op, mc );
1050 if ( sendok & LDAP_BACK_SENDERR ) {
1051 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1052 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1054 send_ldap_result( op, rs );
1056 rs->sr_matched = NULL;
1063 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1066 meta_back_release_conn( op, mc );
1069 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1070 rs->sr_text = "cross-target rename not supported";
1071 if ( sendok & LDAP_BACK_SENDERR ) {
1072 send_ldap_result( op, rs );
1079 Debug( LDAP_DEBUG_TRACE,
1080 "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1081 i, op->o_req_ndn.bv_val, 0 );
1084 /* Retries searching for a metaconn in the avl tree
1085 * the reason is that the connection might have been
1086 * created by meta_back_get_candidate() */
1087 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1089 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1090 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree,
1091 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1093 /* Don't reuse connections while they're still binding */
1094 if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1095 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1096 ldap_pvt_thread_yield();
1102 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1105 /* Looks like we didn't get a bind. Open a new session... */
1107 assert( new_conn == 0 );
1108 mc = metaconn_alloc( op );
1109 mc->mc_conn = mc_curr.mc_conn;
1110 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1112 if ( sendok & LDAP_BACK_BINDING ) {
1113 LDAP_BACK_CONN_BINDING_SET( mc );
1119 * Clear all other candidates
1121 ( void )meta_clear_unused_candidates( op, i );
1123 mt = mi->mi_targets[ i ];
1124 msc = &mc->mc_conns[ i ];
1127 * The target is activated; if needed, it is
1128 * also init'd. In case of error, meta_back_init_one_conn
1129 * sends the appropriate result.
1131 err = meta_back_init_one_conn( op, rs, mc, i,
1132 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
1133 if ( err != LDAP_SUCCESS ) {
1135 * FIXME: in case one target cannot
1136 * be init'd, should the other ones
1139 META_CANDIDATE_RESET( &candidates[ i ] );
1141 (void)meta_clear_one_candidate( msc );
1142 meta_back_freeconn( op, mc );
1145 meta_back_release_conn( op, mc );
1150 candidates[ i ].sr_err = LDAP_SUCCESS;
1151 META_CANDIDATE_SET( &candidates[ i ] );
1159 * if no unique candidate ...
1163 /* Looks like we didn't get a bind. Open a new session... */
1165 assert( new_conn == 0 );
1166 mc = metaconn_alloc( op );
1167 mc->mc_conn = mc_curr.mc_conn;
1168 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1170 if ( sendok & LDAP_BACK_BINDING ) {
1171 LDAP_BACK_CONN_BINDING_SET( mc );
1175 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1176 metatarget_t *mt = mi->mi_targets[ i ];
1177 metasingleconn_t *msc = &mc->mc_conns[ i ];
1180 || meta_back_is_candidate( mt, &op->o_req_ndn,
1181 LDAP_SCOPE_SUBTREE ) )
1185 * The target is activated; if needed, it is
1188 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1189 LDAP_BACK_CONN_ISPRIV( &mc_curr ), LDAP_BACK_DONTSEND );
1190 if ( lerr == LDAP_SUCCESS ) {
1191 META_CANDIDATE_SET( &candidates[ i ] );
1192 candidates[ i ].sr_err = LDAP_SUCCESS;
1195 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1196 op->o_log_prefix, i, 0 );
1201 * FIXME: in case one target cannot
1202 * be init'd, should the other ones
1206 ( void )meta_clear_one_candidate( msc );
1208 /* leave the target candidate, but record the error for later use */
1209 candidates[ i ].sr_err = lerr;
1212 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed: %d\n",
1213 op->o_log_prefix, i, lerr );
1215 if ( META_BACK_ONERR_STOP( mi ) ) {
1216 if ( sendok & LDAP_BACK_SENDERR ) {
1217 send_ldap_result( op, rs );
1221 meta_back_freeconn( op, mc );
1224 meta_back_release_conn( op, mc );
1236 ( void )meta_clear_one_candidate( msc );
1238 META_CANDIDATE_RESET( &candidates[ i ] );
1242 if ( ncandidates == 0 ) {
1244 meta_back_freeconn( op, mc );
1247 meta_back_release_conn( op, mc );
1250 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1251 rs->sr_text = "Unable to select valid candidates";
1253 if ( sendok & LDAP_BACK_SENDERR ) {
1254 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1255 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1257 send_ldap_result( op, rs );
1259 rs->sr_matched = NULL;
1267 /* clear out meta_back_init_one_conn non-fatal errors */
1268 rs->sr_err = LDAP_SUCCESS;
1271 /* touch the timestamp */
1272 if ( mi->mi_idle_timeout != 0 ) {
1273 mc->mc_time = op->o_time;
1277 if ( mi->mi_conn_ttl ) {
1278 mc->mc_create_time = op->o_time;
1282 * Inserts the newly created metaconn in the avl tree
1284 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1285 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1286 meta_back_conndn_cmp, meta_back_conndn_dup );
1288 #if PRINT_CONNTREE > 0
1289 myprint( mi->mi_conninfo.lai_tree, "meta_back_getconn" );
1290 #endif /* PRINT_CONNTREE */
1292 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1295 * Err could be -1 in case a duplicate metaconn is inserted
1302 if ( !( sendok & LDAP_BACK_BINDING ) ) {
1303 /* duplicate: free and try to get the newly created one */
1306 LDAP_BACK_CONN_TAINTED_SET( mc );
1310 Debug( LDAP_DEBUG_ANY,
1311 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1312 op->o_log_prefix, ncandidates,
1313 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1315 meta_back_freeconn( op, mc );
1317 rs->sr_err = LDAP_OTHER;
1318 rs->sr_text = "proxy bind collision";
1319 if ( sendok & LDAP_BACK_SENDERR ) {
1320 send_ldap_result( op, rs );
1326 Debug( LDAP_DEBUG_TRACE,
1327 "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1328 op->o_log_prefix, ncandidates,
1329 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1332 Debug( LDAP_DEBUG_TRACE,
1333 "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1334 op->o_log_prefix, ncandidates,
1335 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1342 meta_back_release_conn_lock(
1347 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1349 assert( mc != NULL );
1352 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1354 assert( mc->mc_refcnt > 0 );
1356 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1357 if ( LDAP_BACK_CONN_TAINTED( mc ) ) {
1358 Debug( LDAP_DEBUG_TRACE, "%s meta_back_release_conn: mc=%p conn=%ld expired.\n",
1359 op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1360 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1361 ( caddr_t )mc, meta_back_conndnmc_cmp );
1362 if ( mc->mc_refcnt == 0 ) {
1363 meta_clear_candidates( op, mc );
1364 meta_back_conn_free( mc );
1368 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1373 meta_back_quarantine(
1379 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1380 metatarget_t *mt = mi->mi_targets[ candidate ];
1382 slap_retry_info_t *ri = &mt->mt_quarantine;
1385 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1388 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1389 switch ( mt->mt_isquarantined ) {
1390 case LDAP_BACK_FQ_NO:
1391 Debug( LDAP_DEBUG_ANY,
1392 "%s: meta_back_quarantine enter.\n",
1393 op->o_log_prefix, 0, 0 );
1399 case LDAP_BACK_FQ_RETRYING:
1400 Debug( LDAP_DEBUG_ANY,
1401 "%s: meta_back_quarantine block #%d try #%d failed.\n",
1402 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1405 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1406 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1417 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1418 ri->ri_last = slap_get_time();
1420 } else if ( mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1421 Debug( LDAP_DEBUG_ANY,
1422 "%s: meta_back_quarantine exit.\n",
1423 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1425 if ( mi->mi_quarantine_f ) {
1426 (void)mi->mi_quarantine_f( mi, candidate,
1427 mi->mi_quarantine_p );
1432 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1436 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );