1 /* conn.c - handles connections to remote targets */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2016 The OpenLDAP Foundation.
6 * Portions Copyright 2016 Symas Corporation.
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>.
19 + * This work was developed by Symas Corporation
20 + * based on back-meta module for inclusion in OpenLDAP Software.
21 + * This work was sponsored by Ericsson. */
28 #include <ac/socket.h>
29 #include <ac/string.h>
34 #include "../back-ldap/back-ldap.h"
35 #include "back-asyncmeta.h"
39 * Debug stuff (got it from libavl)
41 #if META_BACK_PRINT_CONNTREE > 0
44 asyncmeta_back_ravl_print( Avlnode *root, int depth )
52 asyncmeta_back_ravl_print( root->avl_right, depth + 1 );
54 for ( i = 0; i < depth; i++ ) {
55 fprintf( stderr, "-" );
59 asyncmeta_back_print( (a_metaconn_t *)root->avl_data,
60 avl_bf2str( root->avl_bf ) );
62 asyncmeta_back_ravl_print( root->avl_left, depth + 1 );
65 /* NOTE: duplicate from back-ldap/bind.c */
66 static char* priv2str[] = {
76 #endif /* META_BACK_PRINT_CONNTREE */
82 * asyncmeta_conn_alloc
84 * Allocates a connection structure, making room for all the referenced targets
91 int ntargets = mi->mi_ntargets;
93 assert( ntargets > 0 );
95 /* malloc all in one */
96 mc = ( a_metaconn_t * )ch_calloc( 1, sizeof( a_metaconn_t ) + ntargets * sizeof( a_metasingleconn_t ));
102 ldap_pvt_thread_mutex_init( &mc->mc_om_mutex);
103 mc->mc_authz_target = META_BOUND_NONE;
104 mc->mc_conns = (a_metasingleconn_t *)(mc+1);
109 * asyncmeta_init_one_conn
111 * Initializes one connection
114 asyncmeta_init_one_conn(
120 ldap_back_send_t sendok,
123 a_metainfo_t *mi = mc->mc_info;
124 a_metatarget_t *mt = mi->mi_targets[ candidate ];
125 a_metasingleconn_t *msc = NULL;
128 int isauthz = ( candidate == mc->mc_authz_target );
134 int do_start_tls = 0;
135 #endif /* HAVE_TLS */
137 /* if the server is quarantined, and
138 * - the current interval did not expire yet, or
139 * - no more retries should occur,
140 * don't return the connection */
141 if ( mt->mt_isquarantined ) {
142 slap_retry_info_t *ri = &mt->mt_quarantine;
145 if ( mt->mt_quarantine.ri_interval ) {
146 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
147 dont_retry = ( mt->mt_isquarantined > LDAP_BACK_FQ_NO );
149 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
150 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
152 if ( LogTest( LDAP_DEBUG_ANY ) ) {
153 char buf[ SLAP_TEXT_BUFLEN ];
155 snprintf( buf, sizeof( buf ),
156 "asyncmeta_init_one_conn[%d]: quarantine "
157 "retry block #%d try #%d",
158 candidate, ri->ri_idx, ri->ri_count );
159 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
160 op->o_log_prefix, buf, 0 );
163 mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
167 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
171 rs->sr_err = LDAP_UNAVAILABLE;
172 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
173 rs->sr_text = "Target is quarantined";
174 send_ldap_result( op, rs );
179 msc = &mc->mc_conns[candidate];
184 if ( LDAP_BACK_CONN_ISBOUND( msc )
185 || LDAP_BACK_CONN_ISANON( msc ) )
187 assert( msc->msc_ld != NULL );
188 rs->sr_err = LDAP_SUCCESS;
191 } else if ( META_BACK_CONN_CREATING( msc )
192 || LDAP_BACK_CONN_BINDING( msc ) )
194 /* sounds more appropriate */
197 ldap_pvt_thread_yield();
200 rs->sr_err = LDAP_UNAVAILABLE;
203 } else if ( META_BACK_CONN_INITED( msc ) ) {
204 assert( msc->msc_ld != NULL );
205 rs->sr_err = LDAP_SUCCESS;
212 META_BACK_CONN_CREATING_SET( msc );
216 if ( rs->sr_err != LDAP_SUCCESS
218 && ( sendok & LDAP_BACK_SENDERR ) )
220 send_ldap_result( op, rs );
226 assert( msc->msc_ld == NULL );
229 * Attempts to initialize the connection to the target ds
231 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
233 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
235 is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
236 #endif /* HAVE_TLS */
237 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
238 if ( rs->sr_err != LDAP_SUCCESS ) {
241 msc->msc_ldr = ldap_dup(msc->msc_ld);
243 ldap_ld_free(msc->msc_ld, 0, NULL, NULL);
244 rs->sr_err = LDAP_NO_MEMORY;
249 * Set LDAP version. This will always succeed: If the client
250 * bound with a particular version, then so can we.
252 if ( mt->mt_version != 0 ) {
253 version = mt->mt_version;
255 } else if ( op->o_conn->c_protocol != 0 ) {
256 version = op->o_conn->c_protocol;
259 version = LDAP_VERSION3;
261 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
262 ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
264 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
265 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
266 META_BACK_TGT_CHASE_REFERRALS( mt ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
268 slap_client_keepalive(msc->msc_ld, &mt->mt_tls.sb_keepalive);
272 slap_bindconf *sb = NULL;
275 sb = &mt->mt_idassert.si_bc;
280 if ( sb->sb_tls_do_init ) {
281 bindconf_tls_set( sb, msc->msc_ld );
282 } else if ( sb->sb_tls_ctx ) {
283 ldap_set_option( msc->msc_ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
287 if ( sb == &mt->mt_idassert.si_bc && sb->sb_tls_ctx ) {
290 } else if ( META_BACK_TGT_USE_TLS( mt )
291 || ( op->o_conn->c_is_tls && META_BACK_TGT_PROPAGATE_TLS( mt ) ) )
298 /* start TLS ("tls [try-]{start|propagate}" statement) */
299 if ( do_start_tls ) {
300 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
302 * use asynchronous StartTLS; in case, chase referral
303 * FIXME: OpenLDAP does not return referral on StartTLS yet
307 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
308 if ( rs->sr_err == LDAP_SUCCESS ) {
309 LDAPMessage *res = NULL;
310 int rc, nretries = mt->mt_nretries;
313 LDAP_BACK_TV_SET( &tv );
316 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
319 rs->sr_err = LDAP_OTHER;
323 if ( nretries != 0 ) {
324 if ( nretries > 0 ) {
327 LDAP_BACK_TV_SET( &tv );
330 rs->sr_err = LDAP_OTHER;
334 /* only touch when activity actually took place... */
335 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
336 msc->msc_time = op->o_time;
341 if ( rc == LDAP_RES_EXTENDED ) {
342 struct berval *data = NULL;
344 /* NOTE: right now, data is unused, so don't get it */
345 rs->sr_err = ldap_parse_extended_result( msc->msc_ld,
346 res, NULL, NULL /* &data */ , 0 );
347 if ( rs->sr_err == LDAP_SUCCESS ) {
350 /* FIXME: matched? referrals? response controls? */
351 rs->sr_err = ldap_parse_result( msc->msc_ld,
352 res, &err, NULL, NULL, NULL, NULL, 1 );
355 if ( rs->sr_err == LDAP_SUCCESS ) {
358 rs->sr_err = slap_map_api2result( rs );
360 /* FIXME: in case a referral
361 * is returned, should we try
362 * using it instead of the
364 if ( rs->sr_err == LDAP_SUCCESS ) {
365 ldap_install_tls( msc->msc_ld );
367 } else if ( rs->sr_err == LDAP_REFERRAL ) {
368 /* FIXME: LDAP_OPERATIONS_ERROR? */
369 rs->sr_err = LDAP_OTHER;
370 rs->sr_text = "Unwilling to chase referral "
371 "returned by Start TLS exop";
380 rs->sr_err = LDAP_OTHER;
387 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
389 * use synchronous StartTLS
391 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
392 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
394 /* if StartTLS is requested, only attempt it if the URL
395 * is not "ldaps://"; this may occur not only in case
396 * of misconfiguration, but also when used in the chain
397 * overlay, where the "uri" can be parsed out of a referral */
398 if ( rs->sr_err == LDAP_SERVER_DOWN
399 || ( rs->sr_err != LDAP_SUCCESS
400 && META_BACK_TGT_TLS_CRITICAL( mt ) ) )
404 Debug( LDAP_DEBUG_ANY,
405 "### %s asyncmeta_init_one_conn(TLS) "
406 "ldap_unbind_ext[%d] ld=%p\n",
407 op->o_log_prefix, candidate,
408 (void *)msc->msc_ld );
409 #endif /* DEBUG_205 */
414 #endif /* HAVE_TLS */
417 * Set the network timeout if set
419 if ( mt->mt_network_timeout != 0 ) {
420 struct timeval network_timeout;
422 network_timeout.tv_usec = 0;
423 network_timeout.tv_sec = mt->mt_network_timeout;
425 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
426 (void *)&network_timeout );
430 * If the connection DN is not null, an attempt to rewrite it is made
434 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
435 ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
436 if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
437 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
438 memset( msc->msc_cred.bv_val, 0,
439 msc->msc_cred.bv_len );
441 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
443 LDAP_BACK_CONN_ISIDASSERT_SET( msc );
446 ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
450 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
451 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
452 ber_memfree_x( msc->msc_cred.bv_val, NULL );
453 BER_BVZERO( &msc->msc_cred );
455 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
456 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
457 BER_BVZERO( &msc->msc_bound_ndn );
459 if ( !BER_BVISEMPTY( &op->o_ndn )
460 && SLAP_IS_AUTHZ_BACKEND( op )
464 dc.conn = op->o_conn;
469 * Rewrite the bind dn if needed
471 if ( asyncmeta_dn_massage( &dc, &op->o_conn->c_dn,
472 &msc->msc_bound_ndn ) )
476 Debug( LDAP_DEBUG_ANY,
477 "### %s asyncmeta_init_one_conn(rewrite) "
478 "ldap_unbind_ext[%d] ld=%p\n",
479 op->o_log_prefix, candidate,
480 (void *)msc->msc_ld );
481 #endif /* DEBUG_205 */
485 /* copy the DN if needed */
486 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
487 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
490 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
493 ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
496 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
501 META_BACK_CONN_CREATING_CLEAR( msc );
503 if ( rs->sr_err == LDAP_SUCCESS && msc != NULL) {
505 * Sets a cookie for the rewrite session
507 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
508 META_BACK_CONN_INITED_SET( msc );
511 if ( rs->sr_err != LDAP_SUCCESS ) {
512 rs->sr_err = slap_map_api2result( rs );
513 if ( sendok & LDAP_BACK_SENDERR ) {
514 send_ldap_result( op, rs );
523 * Retries one connection
531 ldap_back_send_t sendok )
533 a_metaconn_t *mc = *mcp;
534 a_metainfo_t *mi = mc->mc_info;
535 a_metatarget_t *mt = mi->mi_targets[ candidate ];
536 a_metasingleconn_t *msc = &mc->mc_conns[ candidate ];
537 int rc = LDAP_UNAVAILABLE,
541 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex );
542 struct berval save_cred;
544 if ( LogTest( LDAP_DEBUG_ANY ) ) {
545 char buf[ SLAP_TEXT_BUFLEN ];
547 /* this lock is required; however,
548 * it's invoked only when logging is on */
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 asyncmeta_retry[%d]: %s.\n",
559 op->o_log_prefix, candidate, buf );
562 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
563 /* mc here must be the regular mc, reset and ready for init */
564 rc = asyncmeta_init_one_conn( op, rs, mc, candidate,
565 LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 );
567 if ( rc != LDAP_SUCCESS ) {
568 if ( sendok & LDAP_BACK_SENDERR ) {
569 /* init_one_conn has set the result */
570 send_ldap_result( op, rs );
574 if ( rc == LDAP_SUCCESS ) {
576 LDAP_BACK_CONN_BINDING_SET( msc ); binding = 1;
577 /* todo this must be dobind_init */
578 rc = asyncmeta_back_single_dobind( op, rs, mcp, candidate,
579 sendok, mt->mt_nretries, 0 );
581 Debug( LDAP_DEBUG_ANY,
582 "%s asyncmeta_retry[%d]: "
583 "asyncmeta_single_dobind=%d\n",
584 op->o_log_prefix, candidate, rc );
585 if ( rc == LDAP_SUCCESS ) {
586 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
587 !BER_BVISEMPTY( &msc->msc_bound_ndn ) )
589 LDAP_BACK_CONN_ISBOUND_SET( msc );
592 LDAP_BACK_CONN_ISANON_SET( msc );
595 /* when bound, dispose of the "binding" flag */
597 LDAP_BACK_CONN_BINDING_CLEAR( msc );
603 if ( rc != LDAP_SUCCESS ) {
604 if (mc->mc_active < 1) {
605 asyncmeta_clear_one_msc(NULL, mc, candidate);
607 if ( sendok & LDAP_BACK_SENDERR ) {
609 rs->sr_text = "Unable to retry";
610 send_ldap_result( op, rs );
614 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex );
615 if ( quarantine && META_BACK_TGT_QUARANTINE( mt ) ) {
616 asyncmeta_quarantine( op, mi, rs, candidate );
619 return rc == LDAP_SUCCESS ? 1 : 0;
623 * callback for unique candidate selection
626 asyncmeta_conn_cb( Operation *op, SlapReply *rs )
628 assert( op->o_tag == LDAP_REQ_SEARCH );
630 switch ( rs->sr_type ) {
632 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
648 asyncmeta_get_candidate(
653 a_metainfo_t *mi = ( a_metainfo_t * )op->o_bd->be_private;
657 * tries to get a unique candidate
658 * (takes care of default target)
660 candidate = asyncmeta_select_unique_candidate( mi, ndn );
663 * if any is found, inits the connection
665 if ( candidate == META_TARGET_NONE ) {
666 rs->sr_err = LDAP_NO_SUCH_OBJECT;
667 rs->sr_text = "No suitable candidate target found";
669 } else if ( candidate == META_TARGET_MULTIPLE ) {
671 SlapReply rs2 = { REP_RESULT };
672 slap_callback cb2 = { 0 };
675 /* try to get a unique match for the request ndn
676 * among the multiple candidates available */
677 op2.o_tag = LDAP_REQ_SEARCH;
679 op2.o_req_ndn = *ndn;
680 op2.ors_scope = LDAP_SCOPE_BASE;
681 op2.ors_deref = LDAP_DEREF_NEVER;
682 op2.ors_attrs = slap_anlist_no_attrs;
683 op2.ors_attrsonly = 0;
684 op2.ors_limit = NULL;
686 op2.ors_tlimit = SLAP_NO_LIMIT;
688 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
689 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
691 op2.o_callback = &cb2;
692 cb2.sc_response = asyncmeta_conn_cb;
693 cb2.sc_private = (void *)&candidate;
695 rc = op->o_bd->be_search( &op2, &rs2 );
697 switch ( rs2.sr_err ) {
700 rs->sr_err = rs2.sr_err;
703 case LDAP_SIZELIMIT_EXCEEDED:
704 /* if multiple candidates can serve the operation,
705 * and a default target is defined, and it is
706 * a candidate, try using it (FIXME: YMMV) */
707 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
708 && asyncmeta_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
709 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
711 candidate = mi->mi_defaulttarget;
712 rs->sr_err = LDAP_SUCCESS;
716 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
717 rs->sr_text = "Unable to select unique candidate target";
723 rs->sr_err = LDAP_SUCCESS;
729 static void *asyncmeta_candidates_dummy;
732 asyncmeta_candidates_keyfree(
736 a_metacandidates_t *mc = (a_metacandidates_t *)data;
738 ber_memfree_x( mc->mc_candidates, NULL );
739 ber_memfree_x( data, NULL );
746 * Prepares the connection structure
750 * - determine what DN is being requested:
752 * op requires candidate checks
754 * add unique parent of o_req_ndn
755 * bind unique^*[/all] o_req_ndn [no check]
756 * compare unique^+ o_req_ndn
757 * delete unique o_req_ndn
758 * modify unique o_req_ndn
759 * search any o_req_ndn
760 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
762 * - for ops that require the candidate to be unique, in case of multiple
763 * occurrences an internal search with sizeLimit=1 is performed
764 * if a unique candidate can actually be determined. If none is found,
765 * the operation aborts; if multiple are found, the default target
766 * is used if defined and candidate; otherwise the operation aborts.
768 * *^note: actually, the bind operation is handled much like a search;
769 * i.e. the bind is broadcast to all candidate targets.
771 * +^note: actually, the compare operation is handled much like a search;
772 * i.e. the compare is broadcast to all candidate targets, while checking
773 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
780 SlapReply *candidates,
782 ldap_back_send_t sendok,
785 a_metainfo_t *mi = ( a_metainfo_t * )op->o_bd->be_private;
786 a_metaconn_t *mc = NULL,
788 int cached = META_TARGET_NONE,
789 i = META_TARGET_NONE,
795 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
799 META_DNTYPE_NEWPARENT
800 } dn_type = META_DNTYPE_ENTRY;
801 struct berval ndn = op->o_req_ndn,
805 mc = asyncmeta_conn_alloc(mi);
808 mc = asyncmeta_get_next_mc(mi);
811 ldap_pvt_thread_mutex_lock(&mc->mc_om_mutex);
812 /* Internal searches are privileged and shared. So is root. */
813 if ( ( !BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ALWAYS( mi ) )
814 || ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ANON( mi ) )
815 || op->o_do_not_cache || be_isroot( op ) )
817 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
818 LDAP_BACK_PCONN_ROOTDN_SET( &mc_curr, op );
820 } else if ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_NOANON( mi ) )
822 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
823 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
826 /* Explicit binds must not be shared */
827 if ( !BER_BVISEMPTY( &op->o_ndn )
828 || op->o_tag == LDAP_REQ_BIND
829 || SLAP_IS_AUTHZ_BACKEND( op ) )
831 //mc_curr.mc_conn = op->o_conn;
834 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
835 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
839 switch ( op->o_tag ) {
841 /* if we go to selection, the entry must not exist,
842 * and we must be able to resolve the parent */
843 dn_type = META_DNTYPE_PARENT;
844 dnParent( &ndn, &pndn );
847 case LDAP_REQ_MODRDN:
848 /* if nnewSuperior is not NULL, it must resolve
849 * to the same candidate as the req_ndn */
850 if ( op->orr_nnewSup ) {
851 dn_type = META_DNTYPE_NEWPARENT;
856 /* if bound as rootdn, the backend must bind to all targets
857 * with the administrative identity
858 * (unless pseoudoroot-bind-defer is TRUE) */
859 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
860 op_type = META_OP_REQUIRE_ALL;
864 case LDAP_REQ_COMPARE:
865 case LDAP_REQ_DELETE:
866 case LDAP_REQ_MODIFY:
867 /* just a unique candidate */
870 case LDAP_REQ_SEARCH:
871 /* allow multiple candidates for the searchBase */
872 op_type = META_OP_ALLOW_MULTIPLE;
876 /* right now, just break (exop?) */
881 * require all connections ...
883 if ( op_type == META_OP_REQUIRE_ALL ) {
884 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
885 LDAP_BACK_CONN_ISPRIV_SET( mc );
887 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
888 LDAP_BACK_CONN_ISANON_SET( mc );
891 for ( i = 0; i < mi->mi_ntargets; i++ ) {
893 * The target is activated; if needed, it is
896 candidates[ i ].sr_err = asyncmeta_init_one_conn( op,
897 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
898 LDAP_BACK_DONTSEND, !new_conn );
899 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
900 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
901 LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
903 META_CANDIDATE_SET( &candidates[ i ] );
909 * FIXME: in case one target cannot
910 * be init'd, should the other ones
913 META_CANDIDATE_RESET( &candidates[ i ] );
914 err = candidates[ i ].sr_err;
919 if ( ncandidates == 0 ) {
920 rs->sr_err = LDAP_NO_SUCH_OBJECT;
921 rs->sr_text = "Unable to select valid candidates";
923 if ( sendok & LDAP_BACK_SENDERR ) {
924 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
925 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
927 send_ldap_result( op, rs );
928 rs->sr_matched = NULL;
930 ldap_pvt_thread_mutex_unlock(&mc->mc_om_mutex);
931 if ( alloc_new > 0) {
932 asyncmeta_back_conn_free( mc );
941 * looks in cache, if any
943 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
944 cached = i = asyncmeta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
947 if ( op_type == META_OP_REQUIRE_SINGLE ) {
948 a_metatarget_t *mt = NULL;
949 a_metasingleconn_t *msc = NULL;
953 for ( j = 0; j < mi->mi_ntargets; j++ ) {
954 META_CANDIDATE_RESET( &candidates[ j ] );
958 * tries to get a unique candidate
959 * (takes care of default target)
961 if ( i == META_TARGET_NONE ) {
962 i = asyncmeta_get_candidate( op, rs, &ndn );
964 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
965 i = asyncmeta_get_candidate( op, rs, &pndn );
968 if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
969 if ( sendok & LDAP_BACK_SENDERR ) {
970 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
971 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
973 send_ldap_result( op, rs );
974 rs->sr_matched = NULL;
976 ldap_pvt_thread_mutex_unlock(&mc->mc_om_mutex);
977 if ( mc != NULL && alloc_new ) {
978 asyncmeta_back_conn_free( mc );
984 if ( dn_type == META_DNTYPE_NEWPARENT && asyncmeta_get_candidate( op, rs, op->orr_nnewSup ) != i )
986 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
987 rs->sr_text = "Cross-target rename not supported";
988 if ( sendok & LDAP_BACK_SENDERR ) {
989 send_ldap_result( op, rs );
991 ldap_pvt_thread_mutex_unlock(&mc->mc_om_mutex);
992 if ( mc != NULL && alloc_new > 0 ) {
993 asyncmeta_back_conn_free( mc );
998 Debug( LDAP_DEBUG_TRACE,
999 "==>asyncmeta__getconn: got target=%d for ndn=\"%s\" from cache\n",
1000 i, op->o_req_ndn.bv_val, 0 );
1001 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1002 LDAP_BACK_CONN_ISPRIV_SET( mc );
1004 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1005 LDAP_BACK_CONN_ISANON_SET( mc );
1009 * Clear all other candidates
1011 ( void )asyncmeta_clear_unused_candidates( op, i , mc, candidates);
1013 mt = mi->mi_targets[ i ];
1014 msc = &mc->mc_conns[ i ];
1017 * The target is activated; if needed, it is
1018 * also init'd. In case of error, asyncmeta_init_one_conn
1019 * sends the appropriate result.
1021 err = asyncmeta_init_one_conn( op, rs, mc, i,
1022 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn );
1023 if ( err != LDAP_SUCCESS ) {
1025 * FIXME: in case one target cannot
1026 * be init'd, should the other ones
1029 META_CANDIDATE_RESET( &candidates[ i ] );
1030 ldap_pvt_thread_mutex_unlock(&mc->mc_om_mutex);
1031 if ( mc != NULL && alloc_new > 0 ) {
1032 asyncmeta_back_conn_free( mc );
1037 candidates[ i ].sr_err = LDAP_SUCCESS;
1038 META_CANDIDATE_SET( &candidates[ i ] );
1046 * if no unique candidate ...
1049 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1050 LDAP_BACK_CONN_ISPRIV_SET( mc );
1052 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1053 LDAP_BACK_CONN_ISANON_SET( mc );
1056 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1057 a_metatarget_t *mt = mi->mi_targets[ i ];
1059 META_CANDIDATE_RESET( &candidates[ i ] );
1062 || asyncmeta_is_candidate( mt, &op->o_req_ndn,
1063 op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_SUBTREE ) )
1067 * The target is activated; if needed, it is
1070 int lerr = asyncmeta_init_one_conn( op, rs, mc, i,
1071 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1072 LDAP_BACK_DONTSEND, !new_conn );
1073 candidates[ i ].sr_err = lerr;
1074 if ( lerr == LDAP_SUCCESS ) {
1075 META_CANDIDATE_SET( &candidates[ i ] );
1078 Debug( LDAP_DEBUG_TRACE, "%s: asyncmeta_getconn[%d]\n",
1079 op->o_log_prefix, i, 0 );
1081 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1082 META_CANDIDATE_SET( &candidates[ i ] );
1084 Debug( LDAP_DEBUG_TRACE, "%s: asyncmeta_getconn[%d] %s\n",
1085 op->o_log_prefix, i,
1086 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1091 * FIXME: in case one target cannot
1092 * be init'd, should the other ones
1095 /* leave the target candidate, but record the error for later use */
1098 if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1099 Debug( LDAP_DEBUG_TRACE, "%s: asyncmeta_getconn[%d] quarantined err=%d\n",
1100 op->o_log_prefix, i, lerr );
1103 Debug( LDAP_DEBUG_ANY, "%s: asyncmeta_getconn[%d] failed err=%d\n",
1104 op->o_log_prefix, i, lerr );
1107 if ( META_BACK_ONERR_STOP( mi ) ) {
1108 if ( sendok & LDAP_BACK_SENDERR ) {
1109 send_ldap_result( op, rs );
1111 ldap_pvt_thread_mutex_unlock(&mc->mc_om_mutex);
1112 if ( alloc_new > 0 ) {
1113 asyncmeta_back_conn_free( mc );
1125 if ( ncandidates == 0 ) {
1126 if ( rs->sr_err == LDAP_SUCCESS ) {
1127 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1128 rs->sr_text = "Unable to select valid candidates";
1131 if ( sendok & LDAP_BACK_SENDERR ) {
1132 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1133 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1135 send_ldap_result( op, rs );
1136 rs->sr_matched = NULL;
1138 if ( alloc_new > 0 ) {
1139 asyncmeta_back_conn_free( mc );
1142 ldap_pvt_thread_mutex_unlock(&mc->mc_om_mutex);
1148 /* clear out meta_back_init_one_conn non-fatal errors */
1149 rs->sr_err = LDAP_SUCCESS;
1153 if ( !LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1155 * Err could be -1 in case a duplicate metaconn is inserted
1161 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1162 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1163 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1164 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1166 Debug( LDAP_DEBUG_ANY,
1167 "%s asyncmeta_getconn: candidates=%d conn=%s insert failed\n",
1168 op->o_log_prefix, ncandidates, buf );
1171 asyncmeta_back_conn_free( mc );
1173 rs->sr_err = LDAP_OTHER;
1174 rs->sr_text = "Proxy bind collision";
1175 if ( sendok & LDAP_BACK_SENDERR ) {
1176 send_ldap_result( op, rs );
1182 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1183 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1184 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1186 Debug( LDAP_DEBUG_TRACE,
1187 "%s asyncmeta_getconn: candidates=%d conn=%s inserted\n",
1188 op->o_log_prefix, ncandidates, buf );
1192 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1193 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1194 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1196 Debug( LDAP_DEBUG_TRACE,
1197 "%s asyncmeta_getconn: candidates=%d conn=%s fetched\n",
1198 op->o_log_prefix, ncandidates, buf );
1201 ldap_pvt_thread_mutex_unlock(&mc->mc_om_mutex);
1206 asyncmeta_quarantine(
1212 a_metatarget_t *mt = mi->mi_targets[ candidate ];
1214 slap_retry_info_t *ri = &mt->mt_quarantine;
1216 ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1218 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1219 time_t new_last = slap_get_time();
1221 switch ( mt->mt_isquarantined ) {
1222 case LDAP_BACK_FQ_NO:
1223 if ( ri->ri_last == new_last ) {
1227 Debug( LDAP_DEBUG_ANY,
1228 "%s asyncmeta_quarantine[%d]: enter.\n",
1229 op->o_log_prefix, candidate, 0 );
1235 case LDAP_BACK_FQ_RETRYING:
1236 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1237 char buf[ SLAP_TEXT_BUFLEN ];
1239 snprintf( buf, sizeof( buf ),
1240 "asyncmeta_quarantine[%d]: block #%d try #%d failed",
1241 candidate, ri->ri_idx, ri->ri_count );
1242 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1243 op->o_log_prefix, buf, 0 );
1247 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1248 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1259 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1260 ri->ri_last = new_last;
1262 } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1263 Debug( LDAP_DEBUG_ANY,
1264 "%s asyncmeta_quarantine[%d]: exit.\n",
1265 op->o_log_prefix, candidate, 0 );
1267 if ( mi->mi_quarantine_f ) {
1268 (void)mi->mi_quarantine_f( mi, candidate,
1269 mi->mi_quarantine_p );
1274 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1275 mt->mt_timeout_ops = 0;
1279 ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
1283 asyncmeta_get_next_mc( a_metainfo_t *mi )
1285 a_metaconn_t *mc = NULL;
1286 ldap_pvt_thread_mutex_lock( &mi->mi_mc_mutex );
1287 if (mi->mi_next_conn >= mi->mi_num_conns-1) {
1288 mi->mi_next_conn = 0;
1293 mc = &mi->mi_conns[mi->mi_next_conn];
1294 ldap_pvt_thread_mutex_unlock( &mi->mi_mc_mutex );
1298 int asyncmeta_start_listeners(a_metaconn_t *mc, SlapReply *candidates)
1301 for (i = 0; i < mc->mc_info->mi_ntargets; i++) {
1302 asyncmeta_start_one_listener(mc, candidates, i);
1304 return LDAP_SUCCESS;
1307 int asyncmeta_start_one_listener(a_metaconn_t *mc, SlapReply *candidates, int candidate)
1309 a_metasingleconn_t *msc;
1313 msc = &mc->mc_conns[candidate];
1314 if (msc->msc_ld == NULL || !META_IS_CANDIDATE( &candidates[ candidate ] )) {
1315 return LDAP_SUCCESS;
1317 if ( msc->conn == NULL) {
1318 ldap_get_option( msc->msc_ld, LDAP_OPT_DESC, &s );
1320 /* Todo a meaningful log pls */
1323 msc->conn = connection_client_setup( s, asyncmeta_op_handle_result, mc );
1325 connection_client_enable( msc->conn );
1326 return LDAP_SUCCESS;