Home | History | Annotate | Line # | Download | only in back-meta
search.c revision 1.1
      1  1.1  lukem /* $OpenLDAP: pkg/ldap/servers/slapd/back-meta/search.c,v 1.146.2.11 2008/04/21 17:03:23 quanah Exp $ */
      2  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      3  1.1  lukem  *
      4  1.1  lukem  * Copyright 1999-2008 The OpenLDAP Foundation.
      5  1.1  lukem  * Portions Copyright 2001-2003 Pierangelo Masarati.
      6  1.1  lukem  * Portions Copyright 1999-2003 Howard Chu.
      7  1.1  lukem  * All rights reserved.
      8  1.1  lukem  *
      9  1.1  lukem  * Redistribution and use in source and binary forms, with or without
     10  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
     11  1.1  lukem  * Public License.
     12  1.1  lukem  *
     13  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     14  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     15  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     16  1.1  lukem  */
     17  1.1  lukem /* ACKNOWLEDGEMENTS:
     18  1.1  lukem  * This work was initially developed by the Howard Chu for inclusion
     19  1.1  lukem  * in OpenLDAP Software and subsequently enhanced by Pierangelo
     20  1.1  lukem  * Masarati.
     21  1.1  lukem  */
     22  1.1  lukem 
     23  1.1  lukem #include "portable.h"
     24  1.1  lukem 
     25  1.1  lukem #include <stdio.h>
     26  1.1  lukem 
     27  1.1  lukem #include <ac/socket.h>
     28  1.1  lukem #include <ac/string.h>
     29  1.1  lukem #include <ac/time.h>
     30  1.1  lukem 
     31  1.1  lukem #include "lutil.h"
     32  1.1  lukem #include "slap.h"
     33  1.1  lukem #include "../back-ldap/back-ldap.h"
     34  1.1  lukem #include "back-meta.h"
     35  1.1  lukem #undef ldap_debug	/* silence a warning in ldap-int.h */
     36  1.1  lukem #include "ldap_log.h"
     37  1.1  lukem #include "../../../libraries/libldap/ldap-int.h"
     38  1.1  lukem 
     39  1.1  lukem /* IGNORE means that target does not (no longer) participate
     40  1.1  lukem  * in the search;
     41  1.1  lukem  * NOTREADY means the search on that target has not been initialized yet
     42  1.1  lukem  */
     43  1.1  lukem #define	META_MSGID_IGNORE	(-1)
     44  1.1  lukem #define	META_MSGID_NEED_BIND	(-2)
     45  1.1  lukem #define	META_MSGID_CONNECTING	(-3)
     46  1.1  lukem 
     47  1.1  lukem static int
     48  1.1  lukem meta_send_entry(
     49  1.1  lukem 	Operation 	*op,
     50  1.1  lukem 	SlapReply	*rs,
     51  1.1  lukem 	metaconn_t	*mc,
     52  1.1  lukem 	int 		i,
     53  1.1  lukem 	LDAPMessage 	*e );
     54  1.1  lukem 
     55  1.1  lukem typedef enum meta_search_candidate_t {
     56  1.1  lukem 	META_SEARCH_UNDEFINED = -2,
     57  1.1  lukem 	META_SEARCH_ERR = -1,
     58  1.1  lukem 	META_SEARCH_NOT_CANDIDATE,
     59  1.1  lukem 	META_SEARCH_CANDIDATE,
     60  1.1  lukem 	META_SEARCH_BINDING,
     61  1.1  lukem 	META_SEARCH_NEED_BIND,
     62  1.1  lukem 	META_SEARCH_CONNECTING
     63  1.1  lukem } meta_search_candidate_t;
     64  1.1  lukem 
     65  1.1  lukem /*
     66  1.1  lukem  * meta_search_dobind_init()
     67  1.1  lukem  *
     68  1.1  lukem  * initiates bind for a candidate target of a search.
     69  1.1  lukem  */
     70  1.1  lukem static meta_search_candidate_t
     71  1.1  lukem meta_search_dobind_init(
     72  1.1  lukem 	Operation		*op,
     73  1.1  lukem 	SlapReply		*rs,
     74  1.1  lukem 	metaconn_t		**mcp,
     75  1.1  lukem 	int			candidate,
     76  1.1  lukem 	SlapReply		*candidates )
     77  1.1  lukem {
     78  1.1  lukem 	metaconn_t		*mc = *mcp;
     79  1.1  lukem 	metainfo_t		*mi = ( metainfo_t * )op->o_bd->be_private;
     80  1.1  lukem 	metatarget_t		*mt = mi->mi_targets[ candidate ];
     81  1.1  lukem 	metasingleconn_t	*msc = &mc->mc_conns[ candidate ];
     82  1.1  lukem 
     83  1.1  lukem 	struct berval		binddn = msc->msc_bound_ndn,
     84  1.1  lukem 				cred = msc->msc_cred;
     85  1.1  lukem 	int			method;
     86  1.1  lukem 
     87  1.1  lukem 	int			rc;
     88  1.1  lukem 
     89  1.1  lukem 	meta_search_candidate_t	retcode;
     90  1.1  lukem 
     91  1.1  lukem 	Debug( LDAP_DEBUG_TRACE, "%s >>> meta_search_dobind_init[%d]\n",
     92  1.1  lukem 		op->o_log_prefix, candidate, 0 );
     93  1.1  lukem 
     94  1.1  lukem 	/*
     95  1.1  lukem 	 * all the targets are already bound as pseudoroot
     96  1.1  lukem 	 */
     97  1.1  lukem 	if ( mc->mc_authz_target == META_BOUND_ALL ) {
     98  1.1  lukem 		return META_SEARCH_CANDIDATE;
     99  1.1  lukem 	}
    100  1.1  lukem 
    101  1.1  lukem 	retcode = META_SEARCH_BINDING;
    102  1.1  lukem 	ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
    103  1.1  lukem 	if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
    104  1.1  lukem 		/* already bound (or anonymous) */
    105  1.1  lukem 
    106  1.1  lukem #ifdef DEBUG_205
    107  1.1  lukem 		char	buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
    108  1.1  lukem 		int	bound = 0;
    109  1.1  lukem 
    110  1.1  lukem 		if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
    111  1.1  lukem 			bound = 1;
    112  1.1  lukem 		}
    113  1.1  lukem 
    114  1.1  lukem 		snprintf( buf, sizeof( buf ), " mc=%p ld=%p%s DN=\"%s\"",
    115  1.1  lukem 			(void *)mc, (void *)msc->msc_ld,
    116  1.1  lukem 			bound ? " bound" : " anonymous",
    117  1.1  lukem 			bound == 0 ? "" : msc->msc_bound_ndn.bv_val );
    118  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
    119  1.1  lukem 			op->o_log_prefix, candidate, buf );
    120  1.1  lukem #endif /* DEBUG_205 */
    121  1.1  lukem 
    122  1.1  lukem 		retcode = META_SEARCH_CANDIDATE;
    123  1.1  lukem 
    124  1.1  lukem 	} else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) ) {
    125  1.1  lukem 		/* another thread is binding the target for this conn; wait */
    126  1.1  lukem 
    127  1.1  lukem #ifdef DEBUG_205
    128  1.1  lukem 		char	buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
    129  1.1  lukem 
    130  1.1  lukem 		snprintf( buf, sizeof( buf ), " mc=%p ld=%p needbind",
    131  1.1  lukem 			(void *)mc, (void *)msc->msc_ld );
    132  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
    133  1.1  lukem 			op->o_log_prefix, candidate, buf );
    134  1.1  lukem #endif /* DEBUG_205 */
    135  1.1  lukem 
    136  1.1  lukem 		candidates[ candidate ].sr_msgid = META_MSGID_NEED_BIND;
    137  1.1  lukem 		retcode = META_SEARCH_NEED_BIND;
    138  1.1  lukem 
    139  1.1  lukem 	} else {
    140  1.1  lukem 		/* we'll need to bind the target for this conn */
    141  1.1  lukem 
    142  1.1  lukem #ifdef DEBUG_205
    143  1.1  lukem 		char buf[ SLAP_TEXT_BUFLEN ];
    144  1.1  lukem 
    145  1.1  lukem 		snprintf( buf, sizeof( buf ), " mc=%p ld=%p binding",
    146  1.1  lukem 			(void *)mc, (void *)msc->msc_ld );
    147  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
    148  1.1  lukem 			op->o_log_prefix, candidate, buf );
    149  1.1  lukem #endif /* DEBUG_205 */
    150  1.1  lukem 
    151  1.1  lukem 		if ( msc->msc_ld == NULL ) {
    152  1.1  lukem 			/* for some reason (e.g. because formerly in "binding"
    153  1.1  lukem 			 * state, with eventual connection expiration or invalidation)
    154  1.1  lukem 			 * it was not initialized as expected */
    155  1.1  lukem 
    156  1.1  lukem 			Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p ld=NULL\n",
    157  1.1  lukem 				op->o_log_prefix, candidate, (void *)mc );
    158  1.1  lukem 
    159  1.1  lukem 			rc = meta_back_init_one_conn( op, rs, *mcp, candidate,
    160  1.1  lukem 				LDAP_BACK_CONN_ISPRIV( *mcp ), LDAP_BACK_DONTSEND, 0 );
    161  1.1  lukem 			switch ( rc ) {
    162  1.1  lukem 			case LDAP_SUCCESS:
    163  1.1  lukem 				assert( msc->msc_ld != NULL );
    164  1.1  lukem 				break;
    165  1.1  lukem 
    166  1.1  lukem 			case LDAP_SERVER_DOWN:
    167  1.1  lukem 			case LDAP_UNAVAILABLE:
    168  1.1  lukem 				ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
    169  1.1  lukem 				goto down;
    170  1.1  lukem 
    171  1.1  lukem 			default:
    172  1.1  lukem 				ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
    173  1.1  lukem 				goto other;
    174  1.1  lukem 			}
    175  1.1  lukem 		}
    176  1.1  lukem 
    177  1.1  lukem 		LDAP_BACK_CONN_BINDING_SET( msc );
    178  1.1  lukem 	}
    179  1.1  lukem 
    180  1.1  lukem 	ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
    181  1.1  lukem 
    182  1.1  lukem 	if ( retcode != META_SEARCH_BINDING ) {
    183  1.1  lukem 		return retcode;
    184  1.1  lukem 	}
    185  1.1  lukem 
    186  1.1  lukem 	/* NOTE: this obsoletes pseudorootdn */
    187  1.1  lukem 	if ( op->o_conn != NULL &&
    188  1.1  lukem 		!op->o_do_not_cache &&
    189  1.1  lukem 		( BER_BVISNULL( &msc->msc_bound_ndn ) ||
    190  1.1  lukem 			BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
    191  1.1  lukem 			( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
    192  1.1  lukem 	{
    193  1.1  lukem 		rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, LDAP_BACK_DONTSEND, &binddn, &cred, &method );
    194  1.1  lukem 		if ( rc != LDAP_SUCCESS ) {
    195  1.1  lukem 			goto down;
    196  1.1  lukem 		}
    197  1.1  lukem 
    198  1.1  lukem 		/* NOTE: we copy things here, even if bind didn't succeed yet,
    199  1.1  lukem 		 * because the connection is not shared until bind is over */
    200  1.1  lukem 		if ( !BER_BVISNULL( &binddn ) ) {
    201  1.1  lukem 			ber_bvreplace( &msc->msc_bound_ndn, &binddn );
    202  1.1  lukem 			if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &cred ) ) {
    203  1.1  lukem 				if ( !BER_BVISNULL( &msc->msc_cred ) ) {
    204  1.1  lukem 					memset( msc->msc_cred.bv_val, 0,
    205  1.1  lukem 						msc->msc_cred.bv_len );
    206  1.1  lukem 				}
    207  1.1  lukem 				ber_bvreplace( &msc->msc_cred, &cred );
    208  1.1  lukem 			}
    209  1.1  lukem 		}
    210  1.1  lukem 
    211  1.1  lukem 		if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
    212  1.1  lukem 			/* apparently, idassert was configured with SASL bind,
    213  1.1  lukem 			 * so bind occurred inside meta_back_proxy_authz_cred() */
    214  1.1  lukem 			ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
    215  1.1  lukem 			LDAP_BACK_CONN_BINDING_CLEAR( msc );
    216  1.1  lukem 			ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
    217  1.1  lukem 			return META_SEARCH_CANDIDATE;
    218  1.1  lukem 		}
    219  1.1  lukem 
    220  1.1  lukem 		/* paranoid */
    221  1.1  lukem 		switch ( method ) {
    222  1.1  lukem 		case LDAP_AUTH_NONE:
    223  1.1  lukem 		case LDAP_AUTH_SIMPLE:
    224  1.1  lukem 			/* do a simple bind with binddn, cred */
    225  1.1  lukem 			break;
    226  1.1  lukem 
    227  1.1  lukem 		default:
    228  1.1  lukem 			assert( 0 );
    229  1.1  lukem 			break;
    230  1.1  lukem 		}
    231  1.1  lukem 	}
    232  1.1  lukem 
    233  1.1  lukem 	assert( msc->msc_ld != NULL );
    234  1.1  lukem 
    235  1.1  lukem 	/* connect must be async only the first time... */
    236  1.1  lukem 	ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_ON );
    237  1.1  lukem 
    238  1.1  lukem retry:;
    239  1.1  lukem 	rc = ldap_sasl_bind( msc->msc_ld, binddn.bv_val, LDAP_SASL_SIMPLE, &cred,
    240  1.1  lukem 			NULL, NULL, &candidates[ candidate ].sr_msgid );
    241  1.1  lukem 
    242  1.1  lukem #ifdef DEBUG_205
    243  1.1  lukem 	{
    244  1.1  lukem 		char buf[ SLAP_TEXT_BUFLEN ];
    245  1.1  lukem 
    246  1.1  lukem 		snprintf( buf, sizeof( buf ), "meta_search_dobind_init[%d] mc=%p ld=%p rc=%d",
    247  1.1  lukem 			candidate, (void *)mc, (void *)mc->mc_conns[ candidate ].msc_ld, rc );
    248  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "### %s %s\n",
    249  1.1  lukem 			op->o_log_prefix, buf, 0 );
    250  1.1  lukem 	}
    251  1.1  lukem #endif /* DEBUG_205 */
    252  1.1  lukem 
    253  1.1  lukem 	switch ( rc ) {
    254  1.1  lukem 	case LDAP_SUCCESS:
    255  1.1  lukem 		assert( candidates[ candidate ].sr_msgid >= 0 );
    256  1.1  lukem 		META_BINDING_SET( &candidates[ candidate ] );
    257  1.1  lukem 		return META_SEARCH_BINDING;
    258  1.1  lukem 
    259  1.1  lukem 	case LDAP_X_CONNECTING:
    260  1.1  lukem 		/* must retry, same conn */
    261  1.1  lukem 		candidates[ candidate ].sr_msgid = META_MSGID_CONNECTING;
    262  1.1  lukem 		ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
    263  1.1  lukem 		LDAP_BACK_CONN_BINDING_CLEAR( msc );
    264  1.1  lukem 		ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
    265  1.1  lukem 		return META_SEARCH_CONNECTING;
    266  1.1  lukem 
    267  1.1  lukem 	case LDAP_SERVER_DOWN:
    268  1.1  lukem down:;
    269  1.1  lukem 		/* This is the worst thing that could happen:
    270  1.1  lukem 		 * the search will wait until the retry is over. */
    271  1.1  lukem 		if ( !META_IS_RETRYING( &candidates[ candidate ] ) ) {
    272  1.1  lukem 			META_RETRYING_SET( &candidates[ candidate ] );
    273  1.1  lukem 
    274  1.1  lukem 			ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
    275  1.1  lukem 
    276  1.1  lukem 			assert( mc->mc_refcnt > 0 );
    277  1.1  lukem 			if ( LogTest( LDAP_DEBUG_ANY ) ) {
    278  1.1  lukem 				char	buf[ SLAP_TEXT_BUFLEN ];
    279  1.1  lukem 
    280  1.1  lukem 				/* this lock is required; however,
    281  1.1  lukem 				 * it's invoked only when logging is on */
    282  1.1  lukem 				ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
    283  1.1  lukem 				snprintf( buf, sizeof( buf ),
    284  1.1  lukem 					"retrying URI=\"%s\" DN=\"%s\"",
    285  1.1  lukem 					mt->mt_uri,
    286  1.1  lukem 					BER_BVISNULL( &msc->msc_bound_ndn ) ?
    287  1.1  lukem 						"" : msc->msc_bound_ndn.bv_val );
    288  1.1  lukem 				ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
    289  1.1  lukem 
    290  1.1  lukem 				Debug( LDAP_DEBUG_ANY,
    291  1.1  lukem 					"%s meta_search_dobind_init[%d]: %s.\n",
    292  1.1  lukem 					op->o_log_prefix, candidate, buf );
    293  1.1  lukem 			}
    294  1.1  lukem 
    295  1.1  lukem 			meta_clear_one_candidate( op, mc, candidate );
    296  1.1  lukem 			LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
    297  1.1  lukem 
    298  1.1  lukem 			( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
    299  1.1  lukem 
    300  1.1  lukem 			/* mc here must be the regular mc, reset and ready for init */
    301  1.1  lukem 			rc = meta_back_init_one_conn( op, rs, mc, candidate,
    302  1.1  lukem 				LDAP_BACK_CONN_ISPRIV( mc ), LDAP_BACK_DONTSEND, 0 );
    303  1.1  lukem 
    304  1.1  lukem 			if ( rc == LDAP_SUCCESS ) {
    305  1.1  lukem 				LDAP_BACK_CONN_BINDING_SET( msc );
    306  1.1  lukem 			}
    307  1.1  lukem 
    308  1.1  lukem 			ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
    309  1.1  lukem 
    310  1.1  lukem 			if ( rc == LDAP_SUCCESS ) {
    311  1.1  lukem 				candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
    312  1.1  lukem 				goto retry;
    313  1.1  lukem 			}
    314  1.1  lukem 		}
    315  1.1  lukem 
    316  1.1  lukem 		if ( *mcp == NULL ) {
    317  1.1  lukem 			retcode = META_SEARCH_ERR;
    318  1.1  lukem 			rs->sr_err = LDAP_UNAVAILABLE;
    319  1.1  lukem 			candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
    320  1.1  lukem 			break;
    321  1.1  lukem 		}
    322  1.1  lukem 		/* fall thru */
    323  1.1  lukem 
    324  1.1  lukem 	default:
    325  1.1  lukem other:;
    326  1.1  lukem 		rs->sr_err = rc;
    327  1.1  lukem 		rc = slap_map_api2result( rs );
    328  1.1  lukem 
    329  1.1  lukem 		ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
    330  1.1  lukem 		meta_clear_one_candidate( op, mc, candidate );
    331  1.1  lukem 		candidates[ candidate ].sr_err = rc;
    332  1.1  lukem 		if ( META_BACK_ONERR_STOP( mi ) ) {
    333  1.1  lukem 			LDAP_BACK_CONN_TAINTED_SET( mc );
    334  1.1  lukem 			meta_back_release_conn_lock( mi, mc, 0 );
    335  1.1  lukem 			*mcp = NULL;
    336  1.1  lukem 			rs->sr_err = rc;
    337  1.1  lukem 
    338  1.1  lukem 			retcode = META_SEARCH_ERR;
    339  1.1  lukem 
    340  1.1  lukem 		} else {
    341  1.1  lukem 			retcode = META_SEARCH_NOT_CANDIDATE;
    342  1.1  lukem 		}
    343  1.1  lukem 		candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
    344  1.1  lukem 		ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
    345  1.1  lukem 		break;
    346  1.1  lukem 	}
    347  1.1  lukem 
    348  1.1  lukem 	return retcode;
    349  1.1  lukem }
    350  1.1  lukem 
    351  1.1  lukem static meta_search_candidate_t
    352  1.1  lukem meta_search_dobind_result(
    353  1.1  lukem 	Operation		*op,
    354  1.1  lukem 	SlapReply		*rs,
    355  1.1  lukem 	metaconn_t		**mcp,
    356  1.1  lukem 	int			candidate,
    357  1.1  lukem 	SlapReply		*candidates,
    358  1.1  lukem 	LDAPMessage		*res )
    359  1.1  lukem {
    360  1.1  lukem 	metainfo_t		*mi = ( metainfo_t * )op->o_bd->be_private;
    361  1.1  lukem 	metaconn_t		*mc = *mcp;
    362  1.1  lukem 	metasingleconn_t	*msc = &mc->mc_conns[ candidate ];
    363  1.1  lukem 
    364  1.1  lukem 	meta_search_candidate_t	retcode = META_SEARCH_NOT_CANDIDATE;
    365  1.1  lukem 	int			rc;
    366  1.1  lukem 
    367  1.1  lukem 	assert( msc->msc_ld != NULL );
    368  1.1  lukem 
    369  1.1  lukem 	/* FIXME: matched? referrals? response controls? */
    370  1.1  lukem 	rc = ldap_parse_result( msc->msc_ld, res,
    371  1.1  lukem 		&candidates[ candidate ].sr_err,
    372  1.1  lukem 		NULL, NULL, NULL, NULL, 0 );
    373  1.1  lukem 	if ( rc != LDAP_SUCCESS ) {
    374  1.1  lukem 		candidates[ candidate ].sr_err = rc;
    375  1.1  lukem 
    376  1.1  lukem 	} else {
    377  1.1  lukem 		rc = slap_map_api2result( &candidates[ candidate ] );
    378  1.1  lukem 	}
    379  1.1  lukem 
    380  1.1  lukem 	ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
    381  1.1  lukem 	LDAP_BACK_CONN_BINDING_CLEAR( msc );
    382  1.1  lukem 	if ( rc != LDAP_SUCCESS ) {
    383  1.1  lukem 		meta_clear_one_candidate( op, mc, candidate );
    384  1.1  lukem 		candidates[ candidate ].sr_err = rc;
    385  1.1  lukem 		if ( META_BACK_ONERR_STOP( mi ) ) {
    386  1.1  lukem 	        	LDAP_BACK_CONN_TAINTED_SET( mc );
    387  1.1  lukem 			meta_back_release_conn_lock( mi, mc, 0 );
    388  1.1  lukem 			*mcp = NULL;
    389  1.1  lukem 			retcode = META_SEARCH_ERR;
    390  1.1  lukem 			rs->sr_err = rc;
    391  1.1  lukem 		}
    392  1.1  lukem 
    393  1.1  lukem 	} else {
    394  1.1  lukem 		/* FIXME: check if bound as idassert authcDN! */
    395  1.1  lukem 		if ( BER_BVISNULL( &msc->msc_bound_ndn )
    396  1.1  lukem 			|| BER_BVISEMPTY( &msc->msc_bound_ndn ) )
    397  1.1  lukem 		{
    398  1.1  lukem 			LDAP_BACK_CONN_ISANON_SET( msc );
    399  1.1  lukem 
    400  1.1  lukem 		} else {
    401  1.1  lukem 			LDAP_BACK_CONN_ISBOUND_SET( msc );
    402  1.1  lukem 		}
    403  1.1  lukem 		retcode = META_SEARCH_CANDIDATE;
    404  1.1  lukem 
    405  1.1  lukem 		/* connect must be async */
    406  1.1  lukem 		ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_OFF );
    407  1.1  lukem 	}
    408  1.1  lukem 
    409  1.1  lukem 	candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
    410  1.1  lukem 	META_BINDING_CLEAR( &candidates[ candidate ] );
    411  1.1  lukem 
    412  1.1  lukem 	ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
    413  1.1  lukem 
    414  1.1  lukem 	return retcode;
    415  1.1  lukem }
    416  1.1  lukem 
    417  1.1  lukem static meta_search_candidate_t
    418  1.1  lukem meta_back_search_start(
    419  1.1  lukem 	Operation		*op,
    420  1.1  lukem 	SlapReply		*rs,
    421  1.1  lukem 	dncookie		*dc,
    422  1.1  lukem 	metaconn_t		**mcp,
    423  1.1  lukem 	int			candidate,
    424  1.1  lukem 	SlapReply		*candidates )
    425  1.1  lukem {
    426  1.1  lukem 	metainfo_t		*mi = ( metainfo_t * )op->o_bd->be_private;
    427  1.1  lukem 	metatarget_t		*mt = mi->mi_targets[ candidate ];
    428  1.1  lukem 	metasingleconn_t	*msc = &(*mcp)->mc_conns[ candidate ];
    429  1.1  lukem 	struct berval		realbase = op->o_req_dn;
    430  1.1  lukem 	int			realscope = op->ors_scope;
    431  1.1  lukem 	struct berval		mbase = BER_BVNULL;
    432  1.1  lukem 	struct berval		mfilter = BER_BVNULL;
    433  1.1  lukem 	char			**mapped_attrs = NULL;
    434  1.1  lukem 	int			rc;
    435  1.1  lukem 	meta_search_candidate_t	retcode;
    436  1.1  lukem 	struct timeval		tv, *tvp = NULL;
    437  1.1  lukem 	int			nretries = 1;
    438  1.1  lukem 	LDAPControl		**ctrls = NULL;
    439  1.1  lukem 
    440  1.1  lukem 	/* this should not happen; just in case... */
    441  1.1  lukem 	if ( msc->msc_ld == NULL ) {
    442  1.1  lukem 		Debug( LDAP_DEBUG_ANY,
    443  1.1  lukem 			"%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
    444  1.1  lukem 			op->o_log_prefix, candidate,
    445  1.1  lukem 			META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
    446  1.1  lukem 		candidates[ candidate ].sr_err = LDAP_OTHER;
    447  1.1  lukem 		if ( META_BACK_ONERR_STOP( mi ) ) {
    448  1.1  lukem 			return META_SEARCH_ERR;
    449  1.1  lukem 		}
    450  1.1  lukem 		candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
    451  1.1  lukem 		return META_SEARCH_NOT_CANDIDATE;
    452  1.1  lukem 	}
    453  1.1  lukem 
    454  1.1  lukem 	Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate, 0 );
    455  1.1  lukem 
    456  1.1  lukem 	/*
    457  1.1  lukem 	 * modifies the base according to the scope, if required
    458  1.1  lukem 	 */
    459  1.1  lukem 	if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
    460  1.1  lukem 		switch ( op->ors_scope ) {
    461  1.1  lukem 		case LDAP_SCOPE_SUBTREE:
    462  1.1  lukem 			/*
    463  1.1  lukem 			 * make the target suffix the new base
    464  1.1  lukem 			 * FIXME: this is very forgiving, because
    465  1.1  lukem 			 * "illegal" searchBases may be turned
    466  1.1  lukem 			 * into the suffix of the target; however,
    467  1.1  lukem 			 * the requested searchBase already passed
    468  1.1  lukem 			 * thru the candidate analyzer...
    469  1.1  lukem 			 */
    470  1.1  lukem 			if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
    471  1.1  lukem 				realbase = mt->mt_nsuffix;
    472  1.1  lukem 				if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
    473  1.1  lukem 					realscope = LDAP_SCOPE_SUBORDINATE;
    474  1.1  lukem 				}
    475  1.1  lukem 
    476  1.1  lukem 			} else {
    477  1.1  lukem 				/*
    478  1.1  lukem 				 * this target is no longer candidate
    479  1.1  lukem 				 */
    480  1.1  lukem 				retcode = META_SEARCH_NOT_CANDIDATE;
    481  1.1  lukem 				goto doreturn;
    482  1.1  lukem 			}
    483  1.1  lukem 			break;
    484  1.1  lukem 
    485  1.1  lukem 		case LDAP_SCOPE_SUBORDINATE:
    486  1.1  lukem 		case LDAP_SCOPE_ONELEVEL:
    487  1.1  lukem 		{
    488  1.1  lukem 			struct berval	rdn = mt->mt_nsuffix;
    489  1.1  lukem 			rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
    490  1.1  lukem 			if ( dnIsOneLevelRDN( &rdn )
    491  1.1  lukem 					&& dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
    492  1.1  lukem 			{
    493  1.1  lukem 				/*
    494  1.1  lukem 				 * if there is exactly one level,
    495  1.1  lukem 				 * make the target suffix the new
    496  1.1  lukem 				 * base, and make scope "base"
    497  1.1  lukem 				 */
    498  1.1  lukem 				realbase = mt->mt_nsuffix;
    499  1.1  lukem 				if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
    500  1.1  lukem 					if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
    501  1.1  lukem 						realscope = LDAP_SCOPE_SUBORDINATE;
    502  1.1  lukem 					} else {
    503  1.1  lukem 						realscope = LDAP_SCOPE_SUBTREE;
    504  1.1  lukem 					}
    505  1.1  lukem 				} else {
    506  1.1  lukem 					realscope = LDAP_SCOPE_BASE;
    507  1.1  lukem 				}
    508  1.1  lukem 				break;
    509  1.1  lukem 			} /* else continue with the next case */
    510  1.1  lukem 		}
    511  1.1  lukem 
    512  1.1  lukem 		case LDAP_SCOPE_BASE:
    513  1.1  lukem 			/*
    514  1.1  lukem 			 * this target is no longer candidate
    515  1.1  lukem 			 */
    516  1.1  lukem 			retcode = META_SEARCH_NOT_CANDIDATE;
    517  1.1  lukem 			goto doreturn;
    518  1.1  lukem 		}
    519  1.1  lukem 	}
    520  1.1  lukem 
    521  1.1  lukem 	/* initiate dobind */
    522  1.1  lukem 	retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
    523  1.1  lukem 
    524  1.1  lukem 	Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
    525  1.1  lukem 
    526  1.1  lukem 	if ( retcode != META_SEARCH_CANDIDATE ) {
    527  1.1  lukem 		goto doreturn;
    528  1.1  lukem 	}
    529  1.1  lukem 
    530  1.1  lukem 	/*
    531  1.1  lukem 	 * Rewrite the search base, if required
    532  1.1  lukem 	 */
    533  1.1  lukem 	dc->target = mt;
    534  1.1  lukem 	dc->ctx = "searchBase";
    535  1.1  lukem 	switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
    536  1.1  lukem 	case LDAP_SUCCESS:
    537  1.1  lukem 		break;
    538  1.1  lukem 
    539  1.1  lukem 	case LDAP_UNWILLING_TO_PERFORM:
    540  1.1  lukem 		rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
    541  1.1  lukem 		rs->sr_text = "Operation not allowed";
    542  1.1  lukem 		send_ldap_result( op, rs );
    543  1.1  lukem 		retcode = META_SEARCH_ERR;
    544  1.1  lukem 		goto doreturn;
    545  1.1  lukem 
    546  1.1  lukem 	default:
    547  1.1  lukem 
    548  1.1  lukem 		/*
    549  1.1  lukem 		 * this target is no longer candidate
    550  1.1  lukem 		 */
    551  1.1  lukem 		retcode = META_SEARCH_NOT_CANDIDATE;
    552  1.1  lukem 		goto doreturn;
    553  1.1  lukem 	}
    554  1.1  lukem 
    555  1.1  lukem 	/*
    556  1.1  lukem 	 * Maps filter
    557  1.1  lukem 	 */
    558  1.1  lukem 	rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
    559  1.1  lukem 			&mfilter, BACKLDAP_MAP );
    560  1.1  lukem 	switch ( rc ) {
    561  1.1  lukem 	case LDAP_SUCCESS:
    562  1.1  lukem 		break;
    563  1.1  lukem 
    564  1.1  lukem 	case LDAP_COMPARE_FALSE:
    565  1.1  lukem 	default:
    566  1.1  lukem 		/*
    567  1.1  lukem 		 * this target is no longer candidate
    568  1.1  lukem 		 */
    569  1.1  lukem 		retcode = META_SEARCH_NOT_CANDIDATE;
    570  1.1  lukem 		goto done;
    571  1.1  lukem 	}
    572  1.1  lukem 
    573  1.1  lukem 	/*
    574  1.1  lukem 	 * Maps required attributes
    575  1.1  lukem 	 */
    576  1.1  lukem 	rc = ldap_back_map_attrs( &mt->mt_rwmap.rwm_at,
    577  1.1  lukem 			op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
    578  1.1  lukem 	if ( rc != LDAP_SUCCESS ) {
    579  1.1  lukem 		/*
    580  1.1  lukem 		 * this target is no longer candidate
    581  1.1  lukem 		 */
    582  1.1  lukem 		retcode = META_SEARCH_NOT_CANDIDATE;
    583  1.1  lukem 		goto done;
    584  1.1  lukem 	}
    585  1.1  lukem 
    586  1.1  lukem 	/* should we check return values? */
    587  1.1  lukem 	if ( op->ors_deref != -1 ) {
    588  1.1  lukem 		assert( msc->msc_ld != NULL );
    589  1.1  lukem 		(void)ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
    590  1.1  lukem 				( void * )&op->ors_deref );
    591  1.1  lukem 	}
    592  1.1  lukem 
    593  1.1  lukem 	if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
    594  1.1  lukem 		tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
    595  1.1  lukem 		tv.tv_usec = 0;
    596  1.1  lukem 		tvp = &tv;
    597  1.1  lukem 	}
    598  1.1  lukem 
    599  1.1  lukem retry:;
    600  1.1  lukem 	ctrls = op->o_ctrls;
    601  1.1  lukem 	if ( meta_back_controls_add( op, rs, *mcp, candidate, &ctrls )
    602  1.1  lukem 		!= LDAP_SUCCESS )
    603  1.1  lukem 	{
    604  1.1  lukem 		candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
    605  1.1  lukem 		retcode = META_SEARCH_NOT_CANDIDATE;
    606  1.1  lukem 		goto done;
    607  1.1  lukem 	}
    608  1.1  lukem 
    609  1.1  lukem 	/*
    610  1.1  lukem 	 * Starts the search
    611  1.1  lukem 	 */
    612  1.1  lukem 	assert( msc->msc_ld != NULL );
    613  1.1  lukem 	rc = ldap_search_ext( msc->msc_ld,
    614  1.1  lukem 			mbase.bv_val, realscope, mfilter.bv_val,
    615  1.1  lukem 			mapped_attrs, op->ors_attrsonly,
    616  1.1  lukem 			ctrls, NULL, tvp, op->ors_slimit,
    617  1.1  lukem 			&candidates[ candidate ].sr_msgid );
    618  1.1  lukem 	switch ( rc ) {
    619  1.1  lukem 	case LDAP_SUCCESS:
    620  1.1  lukem 		retcode = META_SEARCH_CANDIDATE;
    621  1.1  lukem 		break;
    622  1.1  lukem 
    623  1.1  lukem 	case LDAP_SERVER_DOWN:
    624  1.1  lukem 		if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
    625  1.1  lukem 			nretries = 0;
    626  1.1  lukem 			/* if the identity changed, there might be need to re-authz */
    627  1.1  lukem 			(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
    628  1.1  lukem 			goto retry;
    629  1.1  lukem 		}
    630  1.1  lukem 
    631  1.1  lukem 		if ( *mcp == NULL ) {
    632  1.1  lukem 			retcode = META_SEARCH_ERR;
    633  1.1  lukem 			candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
    634  1.1  lukem 			break;
    635  1.1  lukem 		}
    636  1.1  lukem 		/* fall thru */
    637  1.1  lukem 
    638  1.1  lukem 	default:
    639  1.1  lukem 		candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
    640  1.1  lukem 		retcode = META_SEARCH_NOT_CANDIDATE;
    641  1.1  lukem 	}
    642  1.1  lukem 
    643  1.1  lukem done:;
    644  1.1  lukem 	(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
    645  1.1  lukem 
    646  1.1  lukem 	if ( mapped_attrs ) {
    647  1.1  lukem 		free( mapped_attrs );
    648  1.1  lukem 	}
    649  1.1  lukem 	if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
    650  1.1  lukem 		free( mfilter.bv_val );
    651  1.1  lukem 	}
    652  1.1  lukem 	if ( mbase.bv_val != realbase.bv_val ) {
    653  1.1  lukem 		free( mbase.bv_val );
    654  1.1  lukem 	}
    655  1.1  lukem 
    656  1.1  lukem doreturn:;
    657  1.1  lukem 	Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
    658  1.1  lukem 
    659  1.1  lukem 	return retcode;
    660  1.1  lukem }
    661  1.1  lukem 
    662  1.1  lukem int
    663  1.1  lukem meta_back_search( Operation *op, SlapReply *rs )
    664  1.1  lukem {
    665  1.1  lukem 	metainfo_t	*mi = ( metainfo_t * )op->o_bd->be_private;
    666  1.1  lukem 	metaconn_t	*mc;
    667  1.1  lukem 	struct timeval	save_tv = { 0, 0 },
    668  1.1  lukem 			tv;
    669  1.1  lukem 	time_t		stoptime = (time_t)(-1),
    670  1.1  lukem 			lastres_time = slap_get_time(),
    671  1.1  lukem 			timeout = 0;
    672  1.1  lukem 	int		rc = 0, sres = LDAP_SUCCESS;
    673  1.1  lukem 	char		*matched = NULL;
    674  1.1  lukem 	int		last = 0, ncandidates = 0,
    675  1.1  lukem 			initial_candidates = 0, candidate_match = 0,
    676  1.1  lukem 			needbind = 0;
    677  1.1  lukem 	ldap_back_send_t	sendok = LDAP_BACK_SENDERR;
    678  1.1  lukem 	long		i;
    679  1.1  lukem 	dncookie	dc;
    680  1.1  lukem 	int		is_ok = 0;
    681  1.1  lukem 	void		*savepriv;
    682  1.1  lukem 	SlapReply	*candidates = NULL;
    683  1.1  lukem 
    684  1.1  lukem 	/*
    685  1.1  lukem 	 * controls are set in ldap_back_dobind()
    686  1.1  lukem 	 *
    687  1.1  lukem 	 * FIXME: in case of values return filter, we might want
    688  1.1  lukem 	 * to map attrs and maybe rewrite value
    689  1.1  lukem 	 */
    690  1.1  lukem getconn:;
    691  1.1  lukem 	mc = meta_back_getconn( op, rs, NULL, sendok );
    692  1.1  lukem 	if ( !mc ) {
    693  1.1  lukem 		return rs->sr_err;
    694  1.1  lukem 	}
    695  1.1  lukem 
    696  1.1  lukem 	dc.conn = op->o_conn;
    697  1.1  lukem 	dc.rs = rs;
    698  1.1  lukem 
    699  1.1  lukem 	if ( candidates == NULL ) candidates = meta_back_candidates_get( op );
    700  1.1  lukem 	/*
    701  1.1  lukem 	 * Inits searches
    702  1.1  lukem 	 */
    703  1.1  lukem 	for ( i = 0; i < mi->mi_ntargets; i++ ) {
    704  1.1  lukem 		/* reset sr_msgid; it is used in most loops
    705  1.1  lukem 		 * to check if that target is still to be considered */
    706  1.1  lukem 		candidates[ i ].sr_msgid = META_MSGID_IGNORE;
    707  1.1  lukem 
    708  1.1  lukem 		/* a target is marked as candidate by meta_back_getconn();
    709  1.1  lukem 		 * if for any reason (an error, it's over or so) it is
    710  1.1  lukem 		 * no longer active, sr_msgid is set to META_MSGID_IGNORE
    711  1.1  lukem 		 * but it remains candidate, which means it has been active
    712  1.1  lukem 		 * at some point during the operation.  This allows to
    713  1.1  lukem 		 * use its response code and more to compute the final
    714  1.1  lukem 		 * response */
    715  1.1  lukem 		if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
    716  1.1  lukem 			continue;
    717  1.1  lukem 		}
    718  1.1  lukem 
    719  1.1  lukem 		candidates[ i ].sr_matched = NULL;
    720  1.1  lukem 		candidates[ i ].sr_text = NULL;
    721  1.1  lukem 		candidates[ i ].sr_ref = NULL;
    722  1.1  lukem 		candidates[ i ].sr_ctrls = NULL;
    723  1.1  lukem 
    724  1.1  lukem 		/* get largest timeout among candidates */
    725  1.1  lukem 		if ( mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ]
    726  1.1  lukem 			&& mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ] > timeout )
    727  1.1  lukem 		{
    728  1.1  lukem 			timeout = mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ];
    729  1.1  lukem 		}
    730  1.1  lukem 	}
    731  1.1  lukem 
    732  1.1  lukem 	for ( i = 0; i < mi->mi_ntargets; i++ ) {
    733  1.1  lukem 		if ( !META_IS_CANDIDATE( &candidates[ i ] )
    734  1.1  lukem 			|| candidates[ i ].sr_err != LDAP_SUCCESS )
    735  1.1  lukem 		{
    736  1.1  lukem 			continue;
    737  1.1  lukem 		}
    738  1.1  lukem 
    739  1.1  lukem 		switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
    740  1.1  lukem 		{
    741  1.1  lukem 		case META_SEARCH_NOT_CANDIDATE:
    742  1.1  lukem 			candidates[ i ].sr_msgid = META_MSGID_IGNORE;
    743  1.1  lukem 			break;
    744  1.1  lukem 
    745  1.1  lukem 		case META_SEARCH_NEED_BIND:
    746  1.1  lukem 			++needbind;
    747  1.1  lukem 			/* fallthru */
    748  1.1  lukem 
    749  1.1  lukem 		case META_SEARCH_CONNECTING:
    750  1.1  lukem 		case META_SEARCH_CANDIDATE:
    751  1.1  lukem 		case META_SEARCH_BINDING:
    752  1.1  lukem 			candidates[ i ].sr_type = REP_INTERMEDIATE;
    753  1.1  lukem 			++ncandidates;
    754  1.1  lukem 			break;
    755  1.1  lukem 
    756  1.1  lukem 		case META_SEARCH_ERR:
    757  1.1  lukem 			savepriv = op->o_private;
    758  1.1  lukem 			op->o_private = (void *)i;
    759  1.1  lukem 			send_ldap_result( op, rs );
    760  1.1  lukem 			op->o_private = savepriv;
    761  1.1  lukem 			rc = -1;
    762  1.1  lukem 			goto finish;
    763  1.1  lukem 
    764  1.1  lukem 		default:
    765  1.1  lukem 			assert( 0 );
    766  1.1  lukem 			break;
    767  1.1  lukem 		}
    768  1.1  lukem 	}
    769  1.1  lukem 
    770  1.1  lukem 	if ( ncandidates > 0 && needbind == ncandidates ) {
    771  1.1  lukem 		/*
    772  1.1  lukem 		 * give up the second time...
    773  1.1  lukem 		 *
    774  1.1  lukem 		 * NOTE: this should not occur the second time, since a fresh
    775  1.1  lukem 		 * connection has ben created; however, targets may also
    776  1.1  lukem 		 * need bind because the bind timed out or so.
    777  1.1  lukem 		 */
    778  1.1  lukem 		if ( sendok & LDAP_BACK_BINDING ) {
    779  1.1  lukem 			Debug( LDAP_DEBUG_ANY,
    780  1.1  lukem 				"%s meta_back_search: unable to initialize conn\n",
    781  1.1  lukem 				op->o_log_prefix, 0, 0 );
    782  1.1  lukem 			rs->sr_err = LDAP_UNAVAILABLE;
    783  1.1  lukem 			rs->sr_text = "unable to initialize connection to remote targets";
    784  1.1  lukem 			send_ldap_result( op, rs );
    785  1.1  lukem 			rc = -1;
    786  1.1  lukem 			goto finish;
    787  1.1  lukem 		}
    788  1.1  lukem 
    789  1.1  lukem 		/* FIXME: better create a separate connection? */
    790  1.1  lukem 		sendok |= LDAP_BACK_BINDING;
    791  1.1  lukem 
    792  1.1  lukem #ifdef DEBUG_205
    793  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
    794  1.1  lukem 			op->o_log_prefix, (void *)mc, 0 );
    795  1.1  lukem #endif /* DEBUG_205 */
    796  1.1  lukem 
    797  1.1  lukem 		meta_back_release_conn( mi, mc );
    798  1.1  lukem 		mc = NULL;
    799  1.1  lukem 
    800  1.1  lukem 		needbind = 0;
    801  1.1  lukem 		ncandidates = 0;
    802  1.1  lukem 
    803  1.1  lukem 		goto getconn;
    804  1.1  lukem 	}
    805  1.1  lukem 
    806  1.1  lukem 	initial_candidates = ncandidates;
    807  1.1  lukem 
    808  1.1  lukem 	if ( LogTest( LDAP_DEBUG_TRACE ) ) {
    809  1.1  lukem 		char	cnd[ SLAP_TEXT_BUFLEN ];
    810  1.1  lukem 		int	c;
    811  1.1  lukem 
    812  1.1  lukem 		for ( c = 0; c < mi->mi_ntargets; c++ ) {
    813  1.1  lukem 			if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
    814  1.1  lukem 				cnd[ c ] = '*';
    815  1.1  lukem 			} else {
    816  1.1  lukem 				cnd[ c ] = ' ';
    817  1.1  lukem 			}
    818  1.1  lukem 		}
    819  1.1  lukem 		cnd[ c ] = '\0';
    820  1.1  lukem 
    821  1.1  lukem 		Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
    822  1.1  lukem 			"cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
    823  1.1  lukem 	}
    824  1.1  lukem 
    825  1.1  lukem 	if ( initial_candidates == 0 ) {
    826  1.1  lukem 		/* NOTE: here we are not sending any matchedDN;
    827  1.1  lukem 		 * this is intended, because if the back-meta
    828  1.1  lukem 		 * is serving this search request, but no valid
    829  1.1  lukem 		 * candidate could be looked up, it means that
    830  1.1  lukem 		 * there is a hole in the mapping of the targets
    831  1.1  lukem 		 * and thus no knowledge of any remote superior
    832  1.1  lukem 		 * is available */
    833  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
    834  1.1  lukem 			"base=\"%s\" scope=%d: "
    835  1.1  lukem 			"no candidate could be selected\n",
    836  1.1  lukem 			op->o_log_prefix, op->o_req_dn.bv_val,
    837  1.1  lukem 			op->ors_scope );
    838  1.1  lukem 
    839  1.1  lukem 		/* FIXME: we're sending the first error we encounter;
    840  1.1  lukem 		 * maybe we should pick the worst... */
    841  1.1  lukem 		rc = LDAP_NO_SUCH_OBJECT;
    842  1.1  lukem 		for ( i = 0; i < mi->mi_ntargets; i++ ) {
    843  1.1  lukem 			if ( META_IS_CANDIDATE( &candidates[ i ] )
    844  1.1  lukem 				&& candidates[ i ].sr_err != LDAP_SUCCESS )
    845  1.1  lukem 			{
    846  1.1  lukem 				rc = candidates[ i ].sr_err;
    847  1.1  lukem 				break;
    848  1.1  lukem 			}
    849  1.1  lukem 		}
    850  1.1  lukem 
    851  1.1  lukem 		send_ldap_error( op, rs, rc, NULL );
    852  1.1  lukem 
    853  1.1  lukem 		goto finish;
    854  1.1  lukem 	}
    855  1.1  lukem 
    856  1.1  lukem 	/* We pull apart the ber result, stuff it into a slapd entry, and
    857  1.1  lukem 	 * let send_search_entry stuff it back into ber format. Slow & ugly,
    858  1.1  lukem 	 * but this is necessary for version matching, and for ACL processing.
    859  1.1  lukem 	 */
    860  1.1  lukem 
    861  1.1  lukem 	if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
    862  1.1  lukem 		stoptime = op->o_time + op->ors_tlimit;
    863  1.1  lukem 	}
    864  1.1  lukem 
    865  1.1  lukem 	/*
    866  1.1  lukem 	 * In case there are no candidates, no cycle takes place...
    867  1.1  lukem 	 *
    868  1.1  lukem 	 * FIXME: we might use a queue, to better balance the load
    869  1.1  lukem 	 * among the candidates
    870  1.1  lukem 	 */
    871  1.1  lukem 	for ( rc = 0; ncandidates > 0; ) {
    872  1.1  lukem 		int	gotit = 0,
    873  1.1  lukem 			doabandon = 0,
    874  1.1  lukem 			alreadybound = ncandidates;
    875  1.1  lukem 
    876  1.1  lukem 		/* check timeout */
    877  1.1  lukem 		if ( timeout && lastres_time > 0
    878  1.1  lukem 			&& ( slap_get_time() - lastres_time ) > timeout )
    879  1.1  lukem 		{
    880  1.1  lukem 			doabandon = 1;
    881  1.1  lukem 			rs->sr_text = "Operation timed out";
    882  1.1  lukem 			rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
    883  1.1  lukem 				LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
    884  1.1  lukem 			savepriv = op->o_private;
    885  1.1  lukem 			op->o_private = (void *)i;
    886  1.1  lukem 			send_ldap_result( op, rs );
    887  1.1  lukem 			op->o_private = savepriv;
    888  1.1  lukem 			goto finish;
    889  1.1  lukem 		}
    890  1.1  lukem 
    891  1.1  lukem 		/* check time limit */
    892  1.1  lukem 		if ( op->ors_tlimit != SLAP_NO_LIMIT
    893  1.1  lukem 				&& slap_get_time() > stoptime )
    894  1.1  lukem 		{
    895  1.1  lukem 			doabandon = 1;
    896  1.1  lukem 			rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
    897  1.1  lukem 			savepriv = op->o_private;
    898  1.1  lukem 			op->o_private = (void *)i;
    899  1.1  lukem 			send_ldap_result( op, rs );
    900  1.1  lukem 			op->o_private = savepriv;
    901  1.1  lukem 			goto finish;
    902  1.1  lukem 		}
    903  1.1  lukem 
    904  1.1  lukem 		for ( i = 0; i < mi->mi_ntargets; i++ ) {
    905  1.1  lukem 			meta_search_candidate_t	retcode = META_SEARCH_UNDEFINED;
    906  1.1  lukem 			metasingleconn_t	*msc = &mc->mc_conns[ i ];
    907  1.1  lukem 			LDAPMessage		*res = NULL, *msg;
    908  1.1  lukem 
    909  1.1  lukem 			/* if msgid is invalid, don't ldap_result() */
    910  1.1  lukem 			if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
    911  1.1  lukem 				continue;
    912  1.1  lukem 			}
    913  1.1  lukem 
    914  1.1  lukem 			/* if target still needs bind, retry */
    915  1.1  lukem 			if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND
    916  1.1  lukem 				|| candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
    917  1.1  lukem 			{
    918  1.1  lukem 				/* initiate dobind */
    919  1.1  lukem 				retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
    920  1.1  lukem 
    921  1.1  lukem 				Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
    922  1.1  lukem 					op->o_log_prefix, i, retcode );
    923  1.1  lukem 
    924  1.1  lukem 				switch ( retcode ) {
    925  1.1  lukem 				case META_SEARCH_NEED_BIND:
    926  1.1  lukem 					alreadybound--;
    927  1.1  lukem 					/* fallthru */
    928  1.1  lukem 
    929  1.1  lukem 				case META_SEARCH_CONNECTING:
    930  1.1  lukem 				case META_SEARCH_BINDING:
    931  1.1  lukem 					break;
    932  1.1  lukem 
    933  1.1  lukem 				case META_SEARCH_ERR:
    934  1.1  lukem 					candidates[ i ].sr_err = rs->sr_err;
    935  1.1  lukem 					if ( META_BACK_ONERR_STOP( mi ) ) {
    936  1.1  lukem 						savepriv = op->o_private;
    937  1.1  lukem 						op->o_private = (void *)i;
    938  1.1  lukem 						send_ldap_result( op, rs );
    939  1.1  lukem 						op->o_private = savepriv;
    940  1.1  lukem 						goto finish;
    941  1.1  lukem 					}
    942  1.1  lukem 					/* fallthru */
    943  1.1  lukem 
    944  1.1  lukem 				case META_SEARCH_NOT_CANDIDATE:
    945  1.1  lukem 					/*
    946  1.1  lukem 					 * When no candidates are left,
    947  1.1  lukem 					 * the outer cycle finishes
    948  1.1  lukem 					 */
    949  1.1  lukem 					candidates[ i ].sr_msgid = META_MSGID_IGNORE;
    950  1.1  lukem 					assert( ncandidates > 0 );
    951  1.1  lukem 					--ncandidates;
    952  1.1  lukem 					break;
    953  1.1  lukem 
    954  1.1  lukem 				case META_SEARCH_CANDIDATE:
    955  1.1  lukem 					candidates[ i ].sr_msgid = META_MSGID_IGNORE;
    956  1.1  lukem 					switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
    957  1.1  lukem 					{
    958  1.1  lukem 					case META_SEARCH_CANDIDATE:
    959  1.1  lukem 						assert( candidates[ i ].sr_msgid >= 0 );
    960  1.1  lukem 						break;
    961  1.1  lukem 
    962  1.1  lukem 					case META_SEARCH_ERR:
    963  1.1  lukem 						candidates[ i ].sr_err = rs->sr_err;
    964  1.1  lukem 						if ( META_BACK_ONERR_STOP( mi ) ) {
    965  1.1  lukem 							savepriv = op->o_private;
    966  1.1  lukem 							op->o_private = (void *)i;
    967  1.1  lukem 							send_ldap_result( op, rs );
    968  1.1  lukem 							op->o_private = savepriv;
    969  1.1  lukem 							goto finish;
    970  1.1  lukem 						}
    971  1.1  lukem 						/* fallthru */
    972  1.1  lukem 
    973  1.1  lukem 					case META_SEARCH_NOT_CANDIDATE:
    974  1.1  lukem 						/* means that meta_back_search_start()
    975  1.1  lukem 						 * failed but onerr == continue */
    976  1.1  lukem 						candidates[ i ].sr_msgid = META_MSGID_IGNORE;
    977  1.1  lukem 						assert( ncandidates > 0 );
    978  1.1  lukem 						--ncandidates;
    979  1.1  lukem 						break;
    980  1.1  lukem 
    981  1.1  lukem 					default:
    982  1.1  lukem 						/* impossible */
    983  1.1  lukem 						assert( 0 );
    984  1.1  lukem 						break;
    985  1.1  lukem 					}
    986  1.1  lukem 					break;
    987  1.1  lukem 
    988  1.1  lukem 				default:
    989  1.1  lukem 					/* impossible */
    990  1.1  lukem 					assert( 0 );
    991  1.1  lukem 					break;
    992  1.1  lukem 				}
    993  1.1  lukem 				continue;
    994  1.1  lukem 			}
    995  1.1  lukem 
    996  1.1  lukem 			/* check for abandon */
    997  1.1  lukem 			if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
    998  1.1  lukem 				break;
    999  1.1  lukem 			}
   1000  1.1  lukem 
   1001  1.1  lukem #ifdef DEBUG_205
   1002  1.1  lukem 			if ( msc->msc_ld == NULL ) {
   1003  1.1  lukem 				char	buf[ SLAP_TEXT_BUFLEN ];
   1004  1.1  lukem 
   1005  1.1  lukem 				ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
   1006  1.1  lukem 				snprintf( buf, sizeof( buf ),
   1007  1.1  lukem 					"%s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n",
   1008  1.1  lukem 					op->o_log_prefix, (long)i, (void *)mc,
   1009  1.1  lukem 					candidates[ i ].sr_msgid,
   1010  1.1  lukem 					META_IS_BINDING( &candidates[ i ] ) ? " binding" : "",
   1011  1.1  lukem 					LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ? " connbinding" : "",
   1012  1.1  lukem 					META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) ? " conncreating" : "" );
   1013  1.1  lukem 				ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
   1014  1.1  lukem 
   1015  1.1  lukem 				Debug( LDAP_DEBUG_ANY, "!!! %s\n", buf, 0, 0 );
   1016  1.1  lukem 			}
   1017  1.1  lukem #endif /* DEBUG_205 */
   1018  1.1  lukem 
   1019  1.1  lukem 			/*
   1020  1.1  lukem 			 * FIXME: handle time limit as well?
   1021  1.1  lukem 			 * Note that target servers are likely
   1022  1.1  lukem 			 * to handle it, so at some time we'll
   1023  1.1  lukem 			 * get a LDAP_TIMELIMIT_EXCEEDED from
   1024  1.1  lukem 			 * one of them ...
   1025  1.1  lukem 			 */
   1026  1.1  lukem 			tv = save_tv;
   1027  1.1  lukem 			rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
   1028  1.1  lukem 					LDAP_MSG_RECEIVED, &tv, &res );
   1029  1.1  lukem 			switch ( rc ) {
   1030  1.1  lukem 			case 0:
   1031  1.1  lukem 				/* FIXME: res should not need to be freed */
   1032  1.1  lukem 				assert( res == NULL );
   1033  1.1  lukem 				continue;
   1034  1.1  lukem 
   1035  1.1  lukem 			case -1:
   1036  1.1  lukem really_bad:;
   1037  1.1  lukem 				/* something REALLY bad happened! */
   1038  1.1  lukem 				if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
   1039  1.1  lukem 					candidates[ i ].sr_type = REP_RESULT;
   1040  1.1  lukem 
   1041  1.1  lukem 					if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
   1042  1.1  lukem 						candidates[ i ].sr_msgid = META_MSGID_IGNORE;
   1043  1.1  lukem 						switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
   1044  1.1  lukem 						{
   1045  1.1  lukem 							/* means that failed but onerr == continue */
   1046  1.1  lukem 						case META_SEARCH_NOT_CANDIDATE:
   1047  1.1  lukem 							candidates[ i ].sr_msgid = META_MSGID_IGNORE;
   1048  1.1  lukem 
   1049  1.1  lukem 							assert( ncandidates > 0 );
   1050  1.1  lukem 							--ncandidates;
   1051  1.1  lukem 
   1052  1.1  lukem 							candidates[ i ].sr_err = rs->sr_err;
   1053  1.1  lukem 							if ( META_BACK_ONERR_STOP( mi ) ) {
   1054  1.1  lukem 								savepriv = op->o_private;
   1055  1.1  lukem 								op->o_private = (void *)i;
   1056  1.1  lukem 								send_ldap_result( op, rs );
   1057  1.1  lukem 								op->o_private = savepriv;
   1058  1.1  lukem 								goto finish;
   1059  1.1  lukem 							}
   1060  1.1  lukem 							/* fall thru */
   1061  1.1  lukem 
   1062  1.1  lukem 						case META_SEARCH_CANDIDATE:
   1063  1.1  lukem 							/* get back into business... */
   1064  1.1  lukem 							continue;
   1065  1.1  lukem 
   1066  1.1  lukem 						case META_SEARCH_BINDING:
   1067  1.1  lukem 						case META_SEARCH_CONNECTING:
   1068  1.1  lukem 						case META_SEARCH_NEED_BIND:
   1069  1.1  lukem 						case META_SEARCH_UNDEFINED:
   1070  1.1  lukem 							assert( 0 );
   1071  1.1  lukem 
   1072  1.1  lukem 						default:
   1073  1.1  lukem 							/* unrecoverable error */
   1074  1.1  lukem 							candidates[ i ].sr_msgid = META_MSGID_IGNORE;
   1075  1.1  lukem 							rc = rs->sr_err = LDAP_OTHER;
   1076  1.1  lukem 							goto finish;
   1077  1.1  lukem 						}
   1078  1.1  lukem 					}
   1079  1.1  lukem 
   1080  1.1  lukem 					candidates[ i ].sr_err = rs->sr_err;
   1081  1.1  lukem 					if ( META_BACK_ONERR_STOP( mi ) ) {
   1082  1.1  lukem 						savepriv = op->o_private;
   1083  1.1  lukem 						op->o_private = (void *)i;
   1084  1.1  lukem 						send_ldap_result( op, rs );
   1085  1.1  lukem 						op->o_private = savepriv;
   1086  1.1  lukem 						goto finish;
   1087  1.1  lukem 					}
   1088  1.1  lukem 				}
   1089  1.1  lukem 
   1090  1.1  lukem 				/*
   1091  1.1  lukem 				 * When no candidates are left,
   1092  1.1  lukem 				 * the outer cycle finishes
   1093  1.1  lukem 				 */
   1094  1.1  lukem 				candidates[ i ].sr_msgid = META_MSGID_IGNORE;
   1095  1.1  lukem 				assert( ncandidates > 0 );
   1096  1.1  lukem 				--ncandidates;
   1097  1.1  lukem 				rs->sr_err = candidates[ i ].sr_err;
   1098  1.1  lukem 				continue;
   1099  1.1  lukem 
   1100  1.1  lukem 			default:
   1101  1.1  lukem 				lastres_time = slap_get_time();
   1102  1.1  lukem 
   1103  1.1  lukem 				/* only touch when activity actually took place... */
   1104  1.1  lukem 				if ( mi->mi_idle_timeout != 0 && msc->msc_time < lastres_time ) {
   1105  1.1  lukem 					msc->msc_time = lastres_time;
   1106  1.1  lukem 				}
   1107  1.1  lukem 				break;
   1108  1.1  lukem 			}
   1109  1.1  lukem 
   1110  1.1  lukem 			for ( msg = ldap_first_message( msc->msc_ld, res );
   1111  1.1  lukem 				msg != NULL;
   1112  1.1  lukem 				msg = ldap_next_message( msc->msc_ld, msg ) )
   1113  1.1  lukem 			{
   1114  1.1  lukem 				rc = ldap_msgtype( msg );
   1115  1.1  lukem 				if ( rc == LDAP_RES_SEARCH_ENTRY ) {
   1116  1.1  lukem 					LDAPMessage	*e;
   1117  1.1  lukem 
   1118  1.1  lukem 					if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
   1119  1.1  lukem 						/* don't retry any more... */
   1120  1.1  lukem 						candidates[ i ].sr_type = REP_RESULT;
   1121  1.1  lukem 					}
   1122  1.1  lukem 
   1123  1.1  lukem 					is_ok++;
   1124  1.1  lukem 
   1125  1.1  lukem 					e = ldap_first_entry( msc->msc_ld, msg );
   1126  1.1  lukem 					savepriv = op->o_private;
   1127  1.1  lukem 					op->o_private = (void *)i;
   1128  1.1  lukem 					rs->sr_err = meta_send_entry( op, rs, mc, i, e );
   1129  1.1  lukem 
   1130  1.1  lukem 					switch ( rs->sr_err ) {
   1131  1.1  lukem 					case LDAP_SIZELIMIT_EXCEEDED:
   1132  1.1  lukem 						savepriv = op->o_private;
   1133  1.1  lukem 						op->o_private = (void *)i;
   1134  1.1  lukem 						send_ldap_result( op, rs );
   1135  1.1  lukem 						op->o_private = savepriv;
   1136  1.1  lukem 						rs->sr_err = LDAP_SUCCESS;
   1137  1.1  lukem 						ldap_msgfree( res );
   1138  1.1  lukem 						res = NULL;
   1139  1.1  lukem 						goto finish;
   1140  1.1  lukem 
   1141  1.1  lukem 					case LDAP_UNAVAILABLE:
   1142  1.1  lukem 						rs->sr_err = LDAP_OTHER;
   1143  1.1  lukem 						ldap_msgfree( res );
   1144  1.1  lukem 						res = NULL;
   1145  1.1  lukem 						goto finish;
   1146  1.1  lukem 					}
   1147  1.1  lukem 					op->o_private = savepriv;
   1148  1.1  lukem 
   1149  1.1  lukem 					/* don't wait any longer... */
   1150  1.1  lukem 					gotit = 1;
   1151  1.1  lukem 					save_tv.tv_sec = 0;
   1152  1.1  lukem 					save_tv.tv_usec = 0;
   1153  1.1  lukem 
   1154  1.1  lukem 				} else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
   1155  1.1  lukem 					char		**references = NULL;
   1156  1.1  lukem 					int		cnt;
   1157  1.1  lukem 
   1158  1.1  lukem 					if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
   1159  1.1  lukem 						/* don't retry any more... */
   1160  1.1  lukem 						candidates[ i ].sr_type = REP_RESULT;
   1161  1.1  lukem 					}
   1162  1.1  lukem 
   1163  1.1  lukem 					is_ok++;
   1164  1.1  lukem 
   1165  1.1  lukem 					rc = ldap_parse_reference( msc->msc_ld, msg,
   1166  1.1  lukem 							&references, &rs->sr_ctrls, 0 );
   1167  1.1  lukem 
   1168  1.1  lukem 					if ( rc != LDAP_SUCCESS ) {
   1169  1.1  lukem 						continue;
   1170  1.1  lukem 					}
   1171  1.1  lukem 
   1172  1.1  lukem 					if ( references == NULL ) {
   1173  1.1  lukem 						continue;
   1174  1.1  lukem 					}
   1175  1.1  lukem 
   1176  1.1  lukem #ifdef ENABLE_REWRITE
   1177  1.1  lukem 					dc.ctx = "referralDN";
   1178  1.1  lukem #else /* ! ENABLE_REWRITE */
   1179  1.1  lukem 					dc.tofrom = 0;
   1180  1.1  lukem 					dc.normalized = 0;
   1181  1.1  lukem #endif /* ! ENABLE_REWRITE */
   1182  1.1  lukem 
   1183  1.1  lukem 					/* FIXME: merge all and return at the end */
   1184  1.1  lukem 
   1185  1.1  lukem 					for ( cnt = 0; references[ cnt ]; cnt++ )
   1186  1.1  lukem 						;
   1187  1.1  lukem 
   1188  1.1  lukem 					rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
   1189  1.1  lukem 
   1190  1.1  lukem 					for ( cnt = 0; references[ cnt ]; cnt++ ) {
   1191  1.1  lukem 						ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
   1192  1.1  lukem 					}
   1193  1.1  lukem 					BER_BVZERO( &rs->sr_ref[ cnt ] );
   1194  1.1  lukem 
   1195  1.1  lukem 					( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
   1196  1.1  lukem 
   1197  1.1  lukem 					if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
   1198  1.1  lukem 						/* ignore return value by now */
   1199  1.1  lukem 						savepriv = op->o_private;
   1200  1.1  lukem 						op->o_private = (void *)i;
   1201  1.1  lukem 						( void )send_search_reference( op, rs );
   1202  1.1  lukem 						op->o_private = savepriv;
   1203  1.1  lukem 
   1204  1.1  lukem 						ber_bvarray_free( rs->sr_ref );
   1205  1.1  lukem 						rs->sr_ref = NULL;
   1206  1.1  lukem 					}
   1207  1.1  lukem 
   1208  1.1  lukem 					/* cleanup */
   1209  1.1  lukem 					if ( references ) {
   1210  1.1  lukem 						ber_memvfree( (void **)references );
   1211  1.1  lukem 					}
   1212  1.1  lukem 
   1213  1.1  lukem 					if ( rs->sr_ctrls ) {
   1214  1.1  lukem 						ldap_controls_free( rs->sr_ctrls );
   1215  1.1  lukem 						rs->sr_ctrls = NULL;
   1216  1.1  lukem 					}
   1217  1.1  lukem 
   1218  1.1  lukem 				} else if ( rc == LDAP_RES_SEARCH_RESULT ) {
   1219  1.1  lukem 					char		buf[ SLAP_TEXT_BUFLEN ];
   1220  1.1  lukem 					char		**references = NULL;
   1221  1.1  lukem 
   1222  1.1  lukem 					if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
   1223  1.1  lukem 						/* don't retry any more... */
   1224  1.1  lukem 						candidates[ i ].sr_type = REP_RESULT;
   1225  1.1  lukem 					}
   1226  1.1  lukem 
   1227  1.1  lukem 					candidates[ i ].sr_msgid = META_MSGID_IGNORE;
   1228  1.1  lukem 
   1229  1.1  lukem 					/* NOTE: ignores response controls
   1230  1.1  lukem 					 * (and intermediate response controls
   1231  1.1  lukem 					 * as well, except for those with search
   1232  1.1  lukem 					 * references); this may not be correct,
   1233  1.1  lukem 					 * but if they're not ignored then
   1234  1.1  lukem 					 * back-meta would need to merge them
   1235  1.1  lukem 					 * consistently (think of pagedResults...)
   1236  1.1  lukem 					 */
   1237  1.1  lukem 					/* FIXME: response controls? */
   1238  1.1  lukem 					rs->sr_err = ldap_parse_result( msc->msc_ld,
   1239  1.1  lukem 						msg,
   1240  1.1  lukem 						&candidates[ i ].sr_err,
   1241  1.1  lukem 						(char **)&candidates[ i ].sr_matched,
   1242  1.1  lukem 						NULL /* (char **)&candidates[ i ].sr_text */ ,
   1243  1.1  lukem 						&references,
   1244  1.1  lukem 						NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
   1245  1.1  lukem 						0 );
   1246  1.1  lukem 					if ( rs->sr_err != LDAP_SUCCESS ) {
   1247  1.1  lukem 						sres = slap_map_api2result( &candidates[ i ] );
   1248  1.1  lukem 						candidates[ i ].sr_type = REP_RESULT;
   1249  1.1  lukem 						ldap_msgfree( res );
   1250  1.1  lukem 						res = NULL;
   1251  1.1  lukem 						goto really_bad;
   1252  1.1  lukem 					}
   1253  1.1  lukem 
   1254  1.1  lukem 					rs->sr_err = candidates[ i ].sr_err;
   1255  1.1  lukem 
   1256  1.1  lukem 					/* massage matchedDN if need be */
   1257  1.1  lukem 					if ( candidates[ i ].sr_matched != NULL ) {
   1258  1.1  lukem 						struct berval	match, mmatch;
   1259  1.1  lukem 
   1260  1.1  lukem 						ber_str2bv( candidates[ i ].sr_matched,
   1261  1.1  lukem 							0, 0, &match );
   1262  1.1  lukem 						candidates[ i ].sr_matched = NULL;
   1263  1.1  lukem 
   1264  1.1  lukem 						dc.ctx = "matchedDN";
   1265  1.1  lukem 						dc.target = mi->mi_targets[ i ];
   1266  1.1  lukem 						if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
   1267  1.1  lukem 							if ( mmatch.bv_val == match.bv_val ) {
   1268  1.1  lukem 								candidates[ i ].sr_matched
   1269  1.1  lukem 									= ch_strdup( mmatch.bv_val );
   1270  1.1  lukem 
   1271  1.1  lukem 							} else {
   1272  1.1  lukem 								candidates[ i ].sr_matched = mmatch.bv_val;
   1273  1.1  lukem 							}
   1274  1.1  lukem 
   1275  1.1  lukem 							candidate_match++;
   1276  1.1  lukem 						}
   1277  1.1  lukem 						ldap_memfree( match.bv_val );
   1278  1.1  lukem 					}
   1279  1.1  lukem 
   1280  1.1  lukem 					/* add references to array */
   1281  1.1  lukem 					/* RFC 4511: referrals can only appear
   1282  1.1  lukem 					 * if result code is LDAP_REFERRAL */
   1283  1.1  lukem 					if ( references != NULL
   1284  1.1  lukem 						&& references[ 0 ] != NULL
   1285  1.1  lukem 						&& references[ 0 ][ 0 ] != '\0' )
   1286  1.1  lukem 					{
   1287  1.1  lukem 						if ( rs->sr_err != LDAP_REFERRAL ) {
   1288  1.1  lukem 							Debug( LDAP_DEBUG_ANY,
   1289  1.1  lukem 								"%s meta_back_search[%ld]: "
   1290  1.1  lukem 								"got referrals with err=%d\n",
   1291  1.1  lukem 								op->o_log_prefix,
   1292  1.1  lukem 								i, rs->sr_err );
   1293  1.1  lukem 
   1294  1.1  lukem 						} else {
   1295  1.1  lukem 							BerVarray	sr_ref;
   1296  1.1  lukem 							int		cnt;
   1297  1.1  lukem 
   1298  1.1  lukem 							for ( cnt = 0; references[ cnt ]; cnt++ )
   1299  1.1  lukem 								;
   1300  1.1  lukem 
   1301  1.1  lukem 							sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
   1302  1.1  lukem 
   1303  1.1  lukem 							for ( cnt = 0; references[ cnt ]; cnt++ ) {
   1304  1.1  lukem 								ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
   1305  1.1  lukem 							}
   1306  1.1  lukem 							BER_BVZERO( &sr_ref[ cnt ] );
   1307  1.1  lukem 
   1308  1.1  lukem 							( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
   1309  1.1  lukem 
   1310  1.1  lukem 							if ( rs->sr_v2ref == NULL ) {
   1311  1.1  lukem 								rs->sr_v2ref = sr_ref;
   1312  1.1  lukem 
   1313  1.1  lukem 							} else {
   1314  1.1  lukem 								for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
   1315  1.1  lukem 									ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
   1316  1.1  lukem 								}
   1317  1.1  lukem 								ber_memfree( sr_ref );
   1318  1.1  lukem 							}
   1319  1.1  lukem 						}
   1320  1.1  lukem 
   1321  1.1  lukem 					} else if ( rs->sr_err == LDAP_REFERRAL ) {
   1322  1.1  lukem 						Debug( LDAP_DEBUG_ANY,
   1323  1.1  lukem 							"%s meta_back_search[%ld]: "
   1324  1.1  lukem 							"got err=%d with null "
   1325  1.1  lukem 							"or empty referrals\n",
   1326  1.1  lukem 							op->o_log_prefix,
   1327  1.1  lukem 							i, rs->sr_err );
   1328  1.1  lukem 
   1329  1.1  lukem 						rs->sr_err = LDAP_NO_SUCH_OBJECT;
   1330  1.1  lukem 					}
   1331  1.1  lukem 
   1332  1.1  lukem 					/* cleanup */
   1333  1.1  lukem 					ber_memvfree( (void **)references );
   1334  1.1  lukem 
   1335  1.1  lukem 					sres = slap_map_api2result( rs );
   1336  1.1  lukem 
   1337  1.1  lukem 					if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
   1338  1.1  lukem 						snprintf( buf, sizeof( buf ),
   1339  1.1  lukem 							"%s meta_back_search[%ld] "
   1340  1.1  lukem 							"match=\"%s\" err=%ld",
   1341  1.1  lukem 							op->o_log_prefix, i,
   1342  1.1  lukem 							candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
   1343  1.1  lukem 							(long) candidates[ i ].sr_err );
   1344  1.1  lukem 						if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
   1345  1.1  lukem 							Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
   1346  1.1  lukem 
   1347  1.1  lukem 						} else {
   1348  1.1  lukem 							Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
   1349  1.1  lukem 								buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
   1350  1.1  lukem 						}
   1351  1.1  lukem 					}
   1352  1.1  lukem 
   1353  1.1  lukem 					switch ( sres ) {
   1354  1.1  lukem 					case LDAP_NO_SUCH_OBJECT:
   1355  1.1  lukem 						/* is_ok is touched any time a valid
   1356  1.1  lukem 						 * (even intermediate) result is
   1357  1.1  lukem 						 * returned; as a consequence, if
   1358  1.1  lukem 						 * a candidate returns noSuchObject
   1359  1.1  lukem 						 * it is ignored and the candidate
   1360  1.1  lukem 						 * is simply demoted. */
   1361  1.1  lukem 						if ( is_ok ) {
   1362  1.1  lukem 							sres = LDAP_SUCCESS;
   1363  1.1  lukem 						}
   1364  1.1  lukem 						break;
   1365  1.1  lukem 
   1366  1.1  lukem 					case LDAP_SUCCESS:
   1367  1.1  lukem 					case LDAP_REFERRAL:
   1368  1.1  lukem 						is_ok++;
   1369  1.1  lukem 						break;
   1370  1.1  lukem 
   1371  1.1  lukem 					case LDAP_SIZELIMIT_EXCEEDED:
   1372  1.1  lukem 						/* if a target returned sizelimitExceeded
   1373  1.1  lukem 						 * and the entry count is equal to the
   1374  1.1  lukem 						 * proxy's limit, the target would have
   1375  1.1  lukem 						 * returned more, and the error must be
   1376  1.1  lukem 						 * propagated to the client; otherwise,
   1377  1.1  lukem 						 * the target enforced a limit lower
   1378  1.1  lukem 						 * than what requested by the proxy;
   1379  1.1  lukem 						 * ignore it */
   1380  1.1  lukem 						candidates[ i ].sr_err = rs->sr_err;
   1381  1.1  lukem 						if ( rs->sr_nentries == op->ors_slimit
   1382  1.1  lukem 							|| META_BACK_ONERR_STOP( mi ) )
   1383  1.1  lukem 						{
   1384  1.1  lukem 							savepriv = op->o_private;
   1385  1.1  lukem 							op->o_private = (void *)i;
   1386  1.1  lukem 							send_ldap_result( op, rs );
   1387  1.1  lukem 							op->o_private = savepriv;
   1388  1.1  lukem 							ldap_msgfree( res );
   1389  1.1  lukem 							res = NULL;
   1390  1.1  lukem 							goto finish;
   1391  1.1  lukem 						}
   1392  1.1  lukem 						break;
   1393  1.1  lukem 
   1394  1.1  lukem 					default:
   1395  1.1  lukem 						candidates[ i ].sr_err = rs->sr_err;
   1396  1.1  lukem 						if ( META_BACK_ONERR_STOP( mi ) ) {
   1397  1.1  lukem 							savepriv = op->o_private;
   1398  1.1  lukem 							op->o_private = (void *)i;
   1399  1.1  lukem 							send_ldap_result( op, rs );
   1400  1.1  lukem 							op->o_private = savepriv;
   1401  1.1  lukem 							ldap_msgfree( res );
   1402  1.1  lukem 							res = NULL;
   1403  1.1  lukem 							goto finish;
   1404  1.1  lukem 						}
   1405  1.1  lukem 						break;
   1406  1.1  lukem 					}
   1407  1.1  lukem 
   1408  1.1  lukem 					last = i;
   1409  1.1  lukem 					rc = 0;
   1410  1.1  lukem 
   1411  1.1  lukem 					/*
   1412  1.1  lukem 					 * When no candidates are left,
   1413  1.1  lukem 					 * the outer cycle finishes
   1414  1.1  lukem 					 */
   1415  1.1  lukem 					assert( ncandidates > 0 );
   1416  1.1  lukem 					--ncandidates;
   1417  1.1  lukem 
   1418  1.1  lukem 				} else if ( rc == LDAP_RES_BIND ) {
   1419  1.1  lukem 					meta_search_candidate_t	retcode;
   1420  1.1  lukem 
   1421  1.1  lukem 					retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
   1422  1.1  lukem 					if ( retcode == META_SEARCH_CANDIDATE ) {
   1423  1.1  lukem 						candidates[ i ].sr_msgid = META_MSGID_IGNORE;
   1424  1.1  lukem 						retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates );
   1425  1.1  lukem 					}
   1426  1.1  lukem 
   1427  1.1  lukem 					switch ( retcode ) {
   1428  1.1  lukem 					case META_SEARCH_CANDIDATE:
   1429  1.1  lukem 						break;
   1430  1.1  lukem 
   1431  1.1  lukem 						/* means that failed but onerr == continue */
   1432  1.1  lukem 					case META_SEARCH_NOT_CANDIDATE:
   1433  1.1  lukem 					case META_SEARCH_ERR:
   1434  1.1  lukem 						candidates[ i ].sr_msgid = META_MSGID_IGNORE;
   1435  1.1  lukem 						assert( ncandidates > 0 );
   1436  1.1  lukem 						--ncandidates;
   1437  1.1  lukem 
   1438  1.1  lukem 						candidates[ i ].sr_err = rs->sr_err;
   1439  1.1  lukem 						if ( META_BACK_ONERR_STOP( mi ) ) {
   1440  1.1  lukem 							savepriv = op->o_private;
   1441  1.1  lukem 							op->o_private = (void *)i;
   1442  1.1  lukem 							send_ldap_result( op, rs );
   1443  1.1  lukem 							op->o_private = savepriv;
   1444  1.1  lukem 							ldap_msgfree( res );
   1445  1.1  lukem 							res = NULL;
   1446  1.1  lukem 							goto finish;
   1447  1.1  lukem 						}
   1448  1.1  lukem 						goto free_message;
   1449  1.1  lukem 
   1450  1.1  lukem 					default:
   1451  1.1  lukem 						assert( 0 );
   1452  1.1  lukem 						break;
   1453  1.1  lukem 					}
   1454  1.1  lukem 
   1455  1.1  lukem 				} else {
   1456  1.1  lukem 					assert( 0 );
   1457  1.1  lukem 					ldap_msgfree( res );
   1458  1.1  lukem 					res = NULL;
   1459  1.1  lukem 					goto really_bad;
   1460  1.1  lukem 				}
   1461  1.1  lukem 			}
   1462  1.1  lukem 
   1463  1.1  lukem free_message:;
   1464  1.1  lukem 			ldap_msgfree( res );
   1465  1.1  lukem 			res = NULL;
   1466  1.1  lukem 		}
   1467  1.1  lukem 
   1468  1.1  lukem 		/* check for abandon */
   1469  1.1  lukem 		if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
   1470  1.1  lukem 			for ( i = 0; i < mi->mi_ntargets; i++ ) {
   1471  1.1  lukem 				if ( candidates[ i ].sr_msgid >= 0
   1472  1.1  lukem 					|| candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
   1473  1.1  lukem 				{
   1474  1.1  lukem 					if ( META_IS_BINDING( &candidates[ i ] )
   1475  1.1  lukem 						|| candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
   1476  1.1  lukem 					{
   1477  1.1  lukem 						ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
   1478  1.1  lukem 						if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
   1479  1.1  lukem 							|| candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
   1480  1.1  lukem 						{
   1481  1.1  lukem 							/* if still binding, destroy */
   1482  1.1  lukem 
   1483  1.1  lukem #ifdef DEBUG_205
   1484  1.1  lukem 							char buf[ SLAP_TEXT_BUFLEN ];
   1485  1.1  lukem 
   1486  1.1  lukem 							snprintf( buf, sizeof( buf), "%s meta_back_search(abandon) "
   1487  1.1  lukem 								"ldap_unbind_ext[%ld] mc=%p ld=%p",
   1488  1.1  lukem 								op->o_log_prefix, i, (void *)mc,
   1489  1.1  lukem 								(void *)mc->mc_conns[i].msc_ld );
   1490  1.1  lukem 
   1491  1.1  lukem 							Debug( LDAP_DEBUG_ANY, "### %s\n", buf, 0, 0 );
   1492  1.1  lukem #endif /* DEBUG_205 */
   1493  1.1  lukem 
   1494  1.1  lukem 							meta_clear_one_candidate( op, mc, i );
   1495  1.1  lukem 						}
   1496  1.1  lukem 						ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
   1497  1.1  lukem 						META_BINDING_CLEAR( &candidates[ i ] );
   1498  1.1  lukem 
   1499  1.1  lukem 					} else {
   1500  1.1  lukem 						(void)meta_back_cancel( mc, op, rs,
   1501  1.1  lukem 							candidates[ i ].sr_msgid, i,
   1502  1.1  lukem 							LDAP_BACK_DONTSEND );
   1503  1.1  lukem 					}
   1504  1.1  lukem 
   1505  1.1  lukem 					candidates[ i ].sr_msgid = META_MSGID_IGNORE;
   1506  1.1  lukem 					assert( ncandidates > 0 );
   1507  1.1  lukem 					--ncandidates;
   1508  1.1  lukem 				}
   1509  1.1  lukem 			}
   1510  1.1  lukem 
   1511  1.1  lukem 			if ( op->o_abandon ) {
   1512  1.1  lukem 				rc = SLAPD_ABANDON;
   1513  1.1  lukem 			}
   1514  1.1  lukem 
   1515  1.1  lukem 			/* let send_ldap_result play cleanup handlers (ITS#4645) */
   1516  1.1  lukem 			break;
   1517  1.1  lukem 		}
   1518  1.1  lukem 
   1519  1.1  lukem 		/* if no entry was found during this loop,
   1520  1.1  lukem 		 * set a minimal timeout */
   1521  1.1  lukem 		if ( ncandidates > 0 && gotit == 0 ) {
   1522  1.1  lukem 			if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
   1523  1.1  lukem 				save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
   1524  1.1  lukem 
   1525  1.1  lukem 				/* arbitrarily limit to something between 1 and 2 minutes */
   1526  1.1  lukem 			} else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
   1527  1.1  lukem 				|| save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
   1528  1.1  lukem 			{
   1529  1.1  lukem 				/* double the timeout */
   1530  1.1  lukem 				lutil_timermul( &save_tv, 2, &save_tv );
   1531  1.1  lukem 			}
   1532  1.1  lukem 
   1533  1.1  lukem 			if ( alreadybound == 0 ) {
   1534  1.1  lukem 				tv = save_tv;
   1535  1.1  lukem 				(void)select( 0, NULL, NULL, NULL, &tv );
   1536  1.1  lukem 
   1537  1.1  lukem 			} else {
   1538  1.1  lukem 				ldap_pvt_thread_yield();
   1539  1.1  lukem 			}
   1540  1.1  lukem 		}
   1541  1.1  lukem 	}
   1542  1.1  lukem 
   1543  1.1  lukem 	if ( rc == -1 ) {
   1544  1.1  lukem 		/*
   1545  1.1  lukem 		 * FIXME: need a better strategy to handle errors
   1546  1.1  lukem 		 */
   1547  1.1  lukem 		if ( mc ) {
   1548  1.1  lukem 			rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
   1549  1.1  lukem 				-1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
   1550  1.1  lukem 				LDAP_BACK_SENDERR );
   1551  1.1  lukem 		} else {
   1552  1.1  lukem 			rc = rs->sr_err;
   1553  1.1  lukem 		}
   1554  1.1  lukem 		goto finish;
   1555  1.1  lukem 	}
   1556  1.1  lukem 
   1557  1.1  lukem 	/*
   1558  1.1  lukem 	 * Rewrite the matched portion of the search base, if required
   1559  1.1  lukem 	 *
   1560  1.1  lukem 	 * FIXME: only the last one gets caught!
   1561  1.1  lukem 	 */
   1562  1.1  lukem 	savepriv = op->o_private;
   1563  1.1  lukem 	op->o_private = (void *)(long)mi->mi_ntargets;
   1564  1.1  lukem 	if ( candidate_match > 0 ) {
   1565  1.1  lukem 		struct berval	pmatched = BER_BVNULL;
   1566  1.1  lukem 
   1567  1.1  lukem 		/* we use the first one */
   1568  1.1  lukem 		for ( i = 0; i < mi->mi_ntargets; i++ ) {
   1569  1.1  lukem 			if ( META_IS_CANDIDATE( &candidates[ i ] )
   1570  1.1  lukem 					&& candidates[ i ].sr_matched != NULL )
   1571  1.1  lukem 			{
   1572  1.1  lukem 				struct berval	bv, pbv;
   1573  1.1  lukem 				int		rc;
   1574  1.1  lukem 
   1575  1.1  lukem 				/* if we got success, and this target
   1576  1.1  lukem 				 * returned noSuchObject, and its suffix
   1577  1.1  lukem 				 * is a superior of the searchBase,
   1578  1.1  lukem 				 * ignore the matchedDN */
   1579  1.1  lukem 				if ( sres == LDAP_SUCCESS
   1580  1.1  lukem 					&& candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
   1581  1.1  lukem 					&& op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
   1582  1.1  lukem 				{
   1583  1.1  lukem 					free( (char *)candidates[ i ].sr_matched );
   1584  1.1  lukem 					candidates[ i ].sr_matched = NULL;
   1585  1.1  lukem 					continue;
   1586  1.1  lukem 				}
   1587  1.1  lukem 
   1588  1.1  lukem 				ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
   1589  1.1  lukem 				rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
   1590  1.1  lukem 
   1591  1.1  lukem 				if ( rc == LDAP_SUCCESS ) {
   1592  1.1  lukem 
   1593  1.1  lukem 					/* NOTE: if they all are superiors
   1594  1.1  lukem 					 * of the baseDN, the shorter is also
   1595  1.1  lukem 					 * superior of the longer... */
   1596  1.1  lukem 					if ( pbv.bv_len > pmatched.bv_len ) {
   1597  1.1  lukem 						if ( !BER_BVISNULL( &pmatched ) ) {
   1598  1.1  lukem 							op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
   1599  1.1  lukem 						}
   1600  1.1  lukem 						pmatched = pbv;
   1601  1.1  lukem 						op->o_private = (void *)i;
   1602  1.1  lukem 
   1603  1.1  lukem 					} else {
   1604  1.1  lukem 						op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
   1605  1.1  lukem 					}
   1606  1.1  lukem 				}
   1607  1.1  lukem 
   1608  1.1  lukem 				if ( candidates[ i ].sr_matched != NULL ) {
   1609  1.1  lukem 					free( (char *)candidates[ i ].sr_matched );
   1610  1.1  lukem 					candidates[ i ].sr_matched = NULL;
   1611  1.1  lukem 				}
   1612  1.1  lukem 			}
   1613  1.1  lukem 		}
   1614  1.1  lukem 
   1615  1.1  lukem 		if ( !BER_BVISNULL( &pmatched ) ) {
   1616  1.1  lukem 			matched = pmatched.bv_val;
   1617  1.1  lukem 		}
   1618  1.1  lukem 
   1619  1.1  lukem 	} else if ( sres == LDAP_NO_SUCH_OBJECT ) {
   1620  1.1  lukem 		matched = op->o_bd->be_suffix[ 0 ].bv_val;
   1621  1.1  lukem 	}
   1622  1.1  lukem 
   1623  1.1  lukem 	/*
   1624  1.1  lukem 	 * In case we returned at least one entry, we return LDAP_SUCCESS
   1625  1.1  lukem 	 * otherwise, the latter error code we got
   1626  1.1  lukem 	 */
   1627  1.1  lukem 
   1628  1.1  lukem 	if ( sres == LDAP_SUCCESS ) {
   1629  1.1  lukem 		if ( rs->sr_v2ref ) {
   1630  1.1  lukem 			sres = LDAP_REFERRAL;
   1631  1.1  lukem 		}
   1632  1.1  lukem 
   1633  1.1  lukem 		if ( META_BACK_ONERR_REPORT( mi ) ) {
   1634  1.1  lukem 			/*
   1635  1.1  lukem 			 * Report errors, if any
   1636  1.1  lukem 			 *
   1637  1.1  lukem 			 * FIXME: we should handle error codes and return the more
   1638  1.1  lukem 			 * important/reasonable
   1639  1.1  lukem 			 */
   1640  1.1  lukem 			for ( i = 0; i < mi->mi_ntargets; i++ ) {
   1641  1.1  lukem 				if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
   1642  1.1  lukem 					continue;
   1643  1.1  lukem 				}
   1644  1.1  lukem 
   1645  1.1  lukem 				if ( candidates[ i ].sr_err != LDAP_SUCCESS
   1646  1.1  lukem 					&& candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
   1647  1.1  lukem 				{
   1648  1.1  lukem 					sres = candidates[ i ].sr_err;
   1649  1.1  lukem 					break;
   1650  1.1  lukem 				}
   1651  1.1  lukem 			}
   1652  1.1  lukem 		}
   1653  1.1  lukem 	}
   1654  1.1  lukem 
   1655  1.1  lukem 	rs->sr_err = sres;
   1656  1.1  lukem 	rs->sr_matched = matched;
   1657  1.1  lukem 	rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
   1658  1.1  lukem 	send_ldap_result( op, rs );
   1659  1.1  lukem 	op->o_private = savepriv;
   1660  1.1  lukem 	rs->sr_matched = NULL;
   1661  1.1  lukem 	rs->sr_ref = NULL;
   1662  1.1  lukem 
   1663  1.1  lukem finish:;
   1664  1.1  lukem 	if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
   1665  1.1  lukem 		op->o_tmpfree( matched, op->o_tmpmemctx );
   1666  1.1  lukem 	}
   1667  1.1  lukem 
   1668  1.1  lukem 	if ( rs->sr_v2ref ) {
   1669  1.1  lukem 		ber_bvarray_free( rs->sr_v2ref );
   1670  1.1  lukem 	}
   1671  1.1  lukem 
   1672  1.1  lukem 	for ( i = 0; i < mi->mi_ntargets; i++ ) {
   1673  1.1  lukem 		if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
   1674  1.1  lukem 			continue;
   1675  1.1  lukem 		}
   1676  1.1  lukem 
   1677  1.1  lukem 		if ( mc ) {
   1678  1.1  lukem 			if ( META_IS_BINDING( &candidates[ i ] )
   1679  1.1  lukem 				|| candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
   1680  1.1  lukem 			{
   1681  1.1  lukem 				ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
   1682  1.1  lukem 				if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
   1683  1.1  lukem 					|| candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
   1684  1.1  lukem 				{
   1685  1.1  lukem 					assert( candidates[ i ].sr_msgid >= 0
   1686  1.1  lukem 						|| candidates[ i ].sr_msgid == META_MSGID_CONNECTING );
   1687  1.1  lukem 					assert( mc->mc_conns[ i ].msc_ld != NULL );
   1688  1.1  lukem 
   1689  1.1  lukem #ifdef DEBUG_205
   1690  1.1  lukem 					Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
   1691  1.1  lukem 						"ldap_unbind_ext[%ld] ld=%p\n",
   1692  1.1  lukem 						op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
   1693  1.1  lukem #endif /* DEBUG_205 */
   1694  1.1  lukem 
   1695  1.1  lukem 					/* if still binding, destroy */
   1696  1.1  lukem 					meta_clear_one_candidate( op, mc, i );
   1697  1.1  lukem 				}
   1698  1.1  lukem 				ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
   1699  1.1  lukem 				META_BINDING_CLEAR( &candidates[ i ] );
   1700  1.1  lukem 
   1701  1.1  lukem 			} else if ( candidates[ i ].sr_msgid >= 0 ) {
   1702  1.1  lukem 				(void)meta_back_cancel( mc, op, rs,
   1703  1.1  lukem 					candidates[ i ].sr_msgid, i,
   1704  1.1  lukem 					LDAP_BACK_DONTSEND );
   1705  1.1  lukem 			}
   1706  1.1  lukem 		}
   1707  1.1  lukem 
   1708  1.1  lukem 		if ( candidates[ i ].sr_matched ) {
   1709  1.1  lukem 			free( (char *)candidates[ i ].sr_matched );
   1710  1.1  lukem 			candidates[ i ].sr_matched = NULL;
   1711  1.1  lukem 		}
   1712  1.1  lukem 
   1713  1.1  lukem 		if ( candidates[ i ].sr_text ) {
   1714  1.1  lukem 			ldap_memfree( (char *)candidates[ i ].sr_text );
   1715  1.1  lukem 			candidates[ i ].sr_text = NULL;
   1716  1.1  lukem 		}
   1717  1.1  lukem 
   1718  1.1  lukem 		if ( candidates[ i ].sr_ref ) {
   1719  1.1  lukem 			ber_bvarray_free( candidates[ i ].sr_ref );
   1720  1.1  lukem 			candidates[ i ].sr_ref = NULL;
   1721  1.1  lukem 		}
   1722  1.1  lukem 
   1723  1.1  lukem 		if ( candidates[ i ].sr_ctrls ) {
   1724  1.1  lukem 			ldap_controls_free( candidates[ i ].sr_ctrls );
   1725  1.1  lukem 			candidates[ i ].sr_ctrls = NULL;
   1726  1.1  lukem 		}
   1727  1.1  lukem 
   1728  1.1  lukem 		if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
   1729  1.1  lukem 			meta_back_quarantine( op, &candidates[ i ], i );
   1730  1.1  lukem 		}
   1731  1.1  lukem 
   1732  1.1  lukem 		/* only in case of timelimit exceeded, if the timelimit exceeded because
   1733  1.1  lukem 		 * one contacted target never responded, invalidate the connection
   1734  1.1  lukem 		 * NOTE: should we quarantine the target as well?  right now, the connection
   1735  1.1  lukem 		 * is invalidated; the next time it will be recreated and the target
   1736  1.1  lukem 		 * will be quarantined if it cannot be contacted */
   1737  1.1  lukem 		if ( mi->mi_idle_timeout != 0
   1738  1.1  lukem 			&& rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
   1739  1.1  lukem 			&& op->o_time > mc->mc_conns[ i ].msc_time )
   1740  1.1  lukem 		{
   1741  1.1  lukem 			/* don't let anyone else use this expired connection */
   1742  1.1  lukem 			LDAP_BACK_CONN_TAINTED_SET( mc );
   1743  1.1  lukem 		}
   1744  1.1  lukem 	}
   1745  1.1  lukem 
   1746  1.1  lukem 	if ( mc ) {
   1747  1.1  lukem 		meta_back_release_conn( mi, mc );
   1748  1.1  lukem 	}
   1749  1.1  lukem 
   1750  1.1  lukem 	return rs->sr_err;
   1751  1.1  lukem }
   1752  1.1  lukem 
   1753  1.1  lukem static int
   1754  1.1  lukem meta_send_entry(
   1755  1.1  lukem 	Operation 	*op,
   1756  1.1  lukem 	SlapReply	*rs,
   1757  1.1  lukem 	metaconn_t	*mc,
   1758  1.1  lukem 	int 		target,
   1759  1.1  lukem 	LDAPMessage 	*e )
   1760  1.1  lukem {
   1761  1.1  lukem 	metainfo_t 		*mi = ( metainfo_t * )op->o_bd->be_private;
   1762  1.1  lukem 	struct berval		a, mapped;
   1763  1.1  lukem 	int			check_duplicate_attrs = 0;
   1764  1.1  lukem 	Entry 			ent = { 0 };
   1765  1.1  lukem 	BerElement 		ber = *e->lm_ber;
   1766  1.1  lukem 	Attribute 		*attr, **attrp;
   1767  1.1  lukem 	struct berval 		bdn,
   1768  1.1  lukem 				dn = BER_BVNULL;
   1769  1.1  lukem 	const char 		*text;
   1770  1.1  lukem 	dncookie		dc;
   1771  1.1  lukem 	int			rc;
   1772  1.1  lukem 
   1773  1.1  lukem 	if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
   1774  1.1  lukem 		return LDAP_DECODING_ERROR;
   1775  1.1  lukem 	}
   1776  1.1  lukem 
   1777  1.1  lukem 	/*
   1778  1.1  lukem 	 * Rewrite the dn of the result, if needed
   1779  1.1  lukem 	 */
   1780  1.1  lukem 	dc.target = mi->mi_targets[ target ];
   1781  1.1  lukem 	dc.conn = op->o_conn;
   1782  1.1  lukem 	dc.rs = rs;
   1783  1.1  lukem 	dc.ctx = "searchResult";
   1784  1.1  lukem 
   1785  1.1  lukem 	rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
   1786  1.1  lukem 	if ( rs->sr_err != LDAP_SUCCESS) {
   1787  1.1  lukem 		return rs->sr_err;
   1788  1.1  lukem 	}
   1789  1.1  lukem 
   1790  1.1  lukem 	/*
   1791  1.1  lukem 	 * Note: this may fail if the target host(s) schema differs
   1792  1.1  lukem 	 * from the one known to the meta, and a DN with unknown
   1793  1.1  lukem 	 * attributes is returned.
   1794  1.1  lukem 	 *
   1795  1.1  lukem 	 * FIXME: should we log anything, or delegate to dnNormalize?
   1796  1.1  lukem 	 */
   1797  1.1  lukem 	rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
   1798  1.1  lukem 		op->o_tmpmemctx );
   1799  1.1  lukem 	if ( dn.bv_val != bdn.bv_val ) {
   1800  1.1  lukem 		free( dn.bv_val );
   1801  1.1  lukem 	}
   1802  1.1  lukem 	BER_BVZERO( &dn );
   1803  1.1  lukem 
   1804  1.1  lukem 	if ( rc != LDAP_SUCCESS ) {
   1805  1.1  lukem 		return LDAP_INVALID_DN_SYNTAX;
   1806  1.1  lukem 	}
   1807  1.1  lukem 
   1808  1.1  lukem 	/*
   1809  1.1  lukem 	 * cache dn
   1810  1.1  lukem 	 */
   1811  1.1  lukem 	if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
   1812  1.1  lukem 		( void )meta_dncache_update_entry( &mi->mi_cache,
   1813  1.1  lukem 				&ent.e_nname, target );
   1814  1.1  lukem 	}
   1815  1.1  lukem 
   1816  1.1  lukem 	attrp = &ent.e_attrs;
   1817  1.1  lukem 
   1818  1.1  lukem 	dc.ctx = "searchAttrDN";
   1819  1.1  lukem 	while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
   1820  1.1  lukem 		int				last = 0;
   1821  1.1  lukem 		slap_syntax_validate_func	*validate;
   1822  1.1  lukem 		slap_syntax_transform_func	*pretty;
   1823  1.1  lukem 
   1824  1.1  lukem 		ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at,
   1825  1.1  lukem 				&a, &mapped, BACKLDAP_REMAP );
   1826  1.1  lukem 		if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
   1827  1.1  lukem 			( void )ber_scanf( &ber, "x" /* [W] */ );
   1828  1.1  lukem 			continue;
   1829  1.1  lukem 		}
   1830  1.1  lukem 		if ( mapped.bv_val != a.bv_val ) {
   1831  1.1  lukem 			/* will need to check for duplicate attrs */
   1832  1.1  lukem 			check_duplicate_attrs++;
   1833  1.1  lukem 		}
   1834  1.1  lukem 		attr = attr_alloc( NULL );
   1835  1.1  lukem 		if ( attr == NULL ) {
   1836  1.1  lukem 			continue;
   1837  1.1  lukem 		}
   1838  1.1  lukem 		if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
   1839  1.1  lukem 				!= LDAP_SUCCESS) {
   1840  1.1  lukem 			if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
   1841  1.1  lukem 				SLAP_AD_PROXIED ) != LDAP_SUCCESS )
   1842  1.1  lukem 			{
   1843  1.1  lukem 				char	buf[ SLAP_TEXT_BUFLEN ];
   1844  1.1  lukem 
   1845  1.1  lukem 				snprintf( buf, sizeof( buf ),
   1846  1.1  lukem 					"%s meta_send_entry(\"%s\"): "
   1847  1.1  lukem 					"slap_bv2undef_ad(%s): %s\n",
   1848  1.1  lukem 					op->o_log_prefix, ent.e_name.bv_val,
   1849  1.1  lukem 					mapped.bv_val, text );
   1850  1.1  lukem 
   1851  1.1  lukem 				Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
   1852  1.1  lukem 				attr_free( attr );
   1853  1.1  lukem 				continue;
   1854  1.1  lukem 			}
   1855  1.1  lukem 		}
   1856  1.1  lukem 
   1857  1.1  lukem 		/* no subschemaSubentry */
   1858  1.1  lukem 		if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
   1859  1.1  lukem 			|| attr->a_desc == slap_schema.si_ad_entryDN )
   1860  1.1  lukem 		{
   1861  1.1  lukem 
   1862  1.1  lukem 			/*
   1863  1.1  lukem 			 * We eat target's subschemaSubentry because
   1864  1.1  lukem 			 * a search for this value is likely not
   1865  1.1  lukem 			 * to resolve to the appropriate backend;
   1866  1.1  lukem 			 * later, the local subschemaSubentry is
   1867  1.1  lukem 			 * added.
   1868  1.1  lukem 			 *
   1869  1.1  lukem 			 * We also eat entryDN because the frontend
   1870  1.1  lukem 			 * will reattach it without checking if already
   1871  1.1  lukem 			 * present...
   1872  1.1  lukem 			 */
   1873  1.1  lukem 			( void )ber_scanf( &ber, "x" /* [W] */ );
   1874  1.1  lukem 
   1875  1.1  lukem 			attr_free(attr);
   1876  1.1  lukem 			continue;
   1877  1.1  lukem 		}
   1878  1.1  lukem 
   1879  1.1  lukem 		if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
   1880  1.1  lukem 				|| attr->a_vals == NULL )
   1881  1.1  lukem 		{
   1882  1.1  lukem 			attr->a_vals = (struct berval *)&slap_dummy_bv;
   1883  1.1  lukem 
   1884  1.1  lukem 		} else {
   1885  1.1  lukem 			for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
   1886  1.1  lukem 				;
   1887  1.1  lukem 		}
   1888  1.1  lukem 		attr->a_numvals = last;
   1889  1.1  lukem 
   1890  1.1  lukem 		validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
   1891  1.1  lukem 		pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
   1892  1.1  lukem 
   1893  1.1  lukem 		if ( !validate && !pretty ) {
   1894  1.1  lukem 			attr_free( attr );
   1895  1.1  lukem 			goto next_attr;
   1896  1.1  lukem 		}
   1897  1.1  lukem 
   1898  1.1  lukem 		if ( attr->a_desc == slap_schema.si_ad_objectClass
   1899  1.1  lukem 				|| attr->a_desc == slap_schema.si_ad_structuralObjectClass )
   1900  1.1  lukem 		{
   1901  1.1  lukem 			struct berval 	*bv;
   1902  1.1  lukem 
   1903  1.1  lukem 			for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
   1904  1.1  lukem 				ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
   1905  1.1  lukem 						bv, &mapped, BACKLDAP_REMAP );
   1906  1.1  lukem 				if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
   1907  1.1  lukem remove_oc:;
   1908  1.1  lukem 					free( bv->bv_val );
   1909  1.1  lukem 					BER_BVZERO( bv );
   1910  1.1  lukem 					if ( --last < 0 ) {
   1911  1.1  lukem 						break;
   1912  1.1  lukem 					}
   1913  1.1  lukem 					*bv = attr->a_vals[ last ];
   1914  1.1  lukem 					BER_BVZERO( &attr->a_vals[ last ] );
   1915  1.1  lukem 					bv--;
   1916  1.1  lukem 
   1917  1.1  lukem 				} else if ( mapped.bv_val != bv->bv_val ) {
   1918  1.1  lukem 					int	i;
   1919  1.1  lukem 
   1920  1.1  lukem 					for ( i = 0; !BER_BVISNULL( &attr->a_vals[ i ] ); i++ ) {
   1921  1.1  lukem 						if ( &attr->a_vals[ i ] == bv ) {
   1922  1.1  lukem 							continue;
   1923  1.1  lukem 						}
   1924  1.1  lukem 
   1925  1.1  lukem 						if ( ber_bvstrcasecmp( &mapped, &attr->a_vals[ i ] ) == 0 ) {
   1926  1.1  lukem 							break;
   1927  1.1  lukem 						}
   1928  1.1  lukem 					}
   1929  1.1  lukem 
   1930  1.1  lukem 					if ( !BER_BVISNULL( &attr->a_vals[ i ] ) ) {
   1931  1.1  lukem 						goto remove_oc;
   1932  1.1  lukem 					}
   1933  1.1  lukem 
   1934  1.1  lukem 					ber_bvreplace( bv, &mapped );
   1935  1.1  lukem 				}
   1936  1.1  lukem 			}
   1937  1.1  lukem 		/*
   1938  1.1  lukem 		 * It is necessary to try to rewrite attributes with
   1939  1.1  lukem 		 * dn syntax because they might be used in ACLs as
   1940  1.1  lukem 		 * members of groups; since ACLs are applied to the
   1941  1.1  lukem 		 * rewritten stuff, no dn-based subecj clause could
   1942  1.1  lukem 		 * be used at the ldap backend side (see
   1943  1.1  lukem 		 * http://www.OpenLDAP.org/faq/data/cache/452.html)
   1944  1.1  lukem 		 * The problem can be overcome by moving the dn-based
   1945  1.1  lukem 		 * ACLs to the target directory server, and letting
   1946  1.1  lukem 		 * everything pass thru the ldap backend.
   1947  1.1  lukem 		 */
   1948  1.1  lukem 		} else {
   1949  1.1  lukem 			int	i;
   1950  1.1  lukem 
   1951  1.1  lukem 			if ( attr->a_desc->ad_type->sat_syntax ==
   1952  1.1  lukem 				slap_schema.si_syn_distinguishedName )
   1953  1.1  lukem 			{
   1954  1.1  lukem 				ldap_dnattr_result_rewrite( &dc, attr->a_vals );
   1955  1.1  lukem 
   1956  1.1  lukem 			} else if ( attr->a_desc == slap_schema.si_ad_ref ) {
   1957  1.1  lukem 				ldap_back_referral_result_rewrite( &dc, attr->a_vals );
   1958  1.1  lukem 
   1959  1.1  lukem 			}
   1960  1.1  lukem 
   1961  1.1  lukem 			for ( i = 0; i < last; i++ ) {
   1962  1.1  lukem 				struct berval	pval;
   1963  1.1  lukem 				int		rc;
   1964  1.1  lukem 
   1965  1.1  lukem 				if ( pretty ) {
   1966  1.1  lukem 					rc = pretty( attr->a_desc->ad_type->sat_syntax,
   1967  1.1  lukem 						&attr->a_vals[i], &pval, NULL );
   1968  1.1  lukem 
   1969  1.1  lukem 				} else {
   1970  1.1  lukem 					rc = validate( attr->a_desc->ad_type->sat_syntax,
   1971  1.1  lukem 						&attr->a_vals[i] );
   1972  1.1  lukem 				}
   1973  1.1  lukem 
   1974  1.1  lukem 				if ( rc ) {
   1975  1.1  lukem 					LBER_FREE( attr->a_vals[i].bv_val );
   1976  1.1  lukem 					if ( --last == i ) {
   1977  1.1  lukem 						BER_BVZERO( &attr->a_vals[ i ] );
   1978  1.1  lukem 						break;
   1979  1.1  lukem 					}
   1980  1.1  lukem 					attr->a_vals[i] = attr->a_vals[last];
   1981  1.1  lukem 					BER_BVZERO( &attr->a_vals[last] );
   1982  1.1  lukem 					i--;
   1983  1.1  lukem 					continue;
   1984  1.1  lukem 				}
   1985  1.1  lukem 
   1986  1.1  lukem 				if ( pretty ) {
   1987  1.1  lukem 					LBER_FREE( attr->a_vals[i].bv_val );
   1988  1.1  lukem 					attr->a_vals[i] = pval;
   1989  1.1  lukem 				}
   1990  1.1  lukem 			}
   1991  1.1  lukem 
   1992  1.1  lukem 			if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
   1993  1.1  lukem 				attr_free( attr );
   1994  1.1  lukem 				goto next_attr;
   1995  1.1  lukem 			}
   1996  1.1  lukem 		}
   1997  1.1  lukem 
   1998  1.1  lukem 		if ( last && attr->a_desc->ad_type->sat_equality &&
   1999  1.1  lukem 			attr->a_desc->ad_type->sat_equality->smr_normalize )
   2000  1.1  lukem 		{
   2001  1.1  lukem 			int i;
   2002  1.1  lukem 
   2003  1.1  lukem 			attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
   2004  1.1  lukem 			for ( i = 0; i<last; i++ ) {
   2005  1.1  lukem 				attr->a_desc->ad_type->sat_equality->smr_normalize(
   2006  1.1  lukem 					SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
   2007  1.1  lukem 					attr->a_desc->ad_type->sat_syntax,
   2008  1.1  lukem 					attr->a_desc->ad_type->sat_equality,
   2009  1.1  lukem 					&attr->a_vals[i], &attr->a_nvals[i],
   2010  1.1  lukem 					NULL );
   2011  1.1  lukem 			}
   2012  1.1  lukem 			BER_BVZERO( &attr->a_nvals[i] );
   2013  1.1  lukem 
   2014  1.1  lukem 		} else {
   2015  1.1  lukem 			attr->a_nvals = attr->a_vals;
   2016  1.1  lukem 		}
   2017  1.1  lukem 
   2018  1.1  lukem 		*attrp = attr;
   2019  1.1  lukem 		attrp = &attr->a_next;
   2020  1.1  lukem next_attr:;
   2021  1.1  lukem 	}
   2022  1.1  lukem 
   2023  1.1  lukem 	/* only check if some mapping occurred */
   2024  1.1  lukem 	if ( check_duplicate_attrs ) {
   2025  1.1  lukem 		Attribute	**ap;
   2026  1.1  lukem 
   2027  1.1  lukem 		for ( ap = &ent.e_attrs; *ap != NULL; ap = &(*ap)->a_next ) {
   2028  1.1  lukem 			Attribute	**tap;
   2029  1.1  lukem 
   2030  1.1  lukem 			for ( tap = &(*ap)->a_next; *tap != NULL; ) {
   2031  1.1  lukem 				if ( (*tap)->a_desc == (*ap)->a_desc ) {
   2032  1.1  lukem 					Entry		e = { 0 };
   2033  1.1  lukem 					Modification	mod = { 0 };
   2034  1.1  lukem 					const char	*text = NULL;
   2035  1.1  lukem 					char		textbuf[ SLAP_TEXT_BUFLEN ];
   2036  1.1  lukem 					Attribute	*next = (*tap)->a_next;
   2037  1.1  lukem 
   2038  1.1  lukem 					BER_BVSTR( &e.e_name, "" );
   2039  1.1  lukem 					BER_BVSTR( &e.e_nname, "" );
   2040  1.1  lukem 					e.e_attrs = *ap;
   2041  1.1  lukem 					mod.sm_op = LDAP_MOD_ADD;
   2042  1.1  lukem 					mod.sm_desc = (*ap)->a_desc;
   2043  1.1  lukem 					mod.sm_type = mod.sm_desc->ad_cname;
   2044  1.1  lukem 					mod.sm_numvals = (*ap)->a_numvals;
   2045  1.1  lukem 					mod.sm_values = (*tap)->a_vals;
   2046  1.1  lukem 					if ( (*tap)->a_nvals != (*tap)->a_vals ) {
   2047  1.1  lukem 						mod.sm_nvalues = (*tap)->a_nvals;
   2048  1.1  lukem 					}
   2049  1.1  lukem 
   2050  1.1  lukem 					(void)modify_add_values( &e, &mod,
   2051  1.1  lukem 						/* permissive */ 1,
   2052  1.1  lukem 						&text, textbuf, sizeof( textbuf ) );
   2053  1.1  lukem 
   2054  1.1  lukem 					/* should not insert new attrs! */
   2055  1.1  lukem 					assert( e.e_attrs == *ap );
   2056  1.1  lukem 
   2057  1.1  lukem 					attr_free( *tap );
   2058  1.1  lukem 					*tap = next;
   2059  1.1  lukem 
   2060  1.1  lukem 				} else {
   2061  1.1  lukem 					tap = &(*tap)->a_next;
   2062  1.1  lukem 				}
   2063  1.1  lukem 			}
   2064  1.1  lukem 		}
   2065  1.1  lukem 	}
   2066  1.1  lukem 
   2067  1.1  lukem 	ldap_get_entry_controls( mc->mc_conns[target].msc_ld,
   2068  1.1  lukem 		e, &rs->sr_ctrls );
   2069  1.1  lukem 	rs->sr_entry = &ent;
   2070  1.1  lukem 	rs->sr_attrs = op->ors_attrs;
   2071  1.1  lukem 	rs->sr_operational_attrs = NULL;
   2072  1.1  lukem 	rs->sr_flags = 0;
   2073  1.1  lukem 	rs->sr_err = LDAP_SUCCESS;
   2074  1.1  lukem 	rc = send_search_entry( op, rs );
   2075  1.1  lukem 	switch ( rc ) {
   2076  1.1  lukem 	case LDAP_UNAVAILABLE:
   2077  1.1  lukem 		rc = LDAP_OTHER;
   2078  1.1  lukem 		break;
   2079  1.1  lukem 	}
   2080  1.1  lukem 	rs->sr_entry = NULL;
   2081  1.1  lukem 	rs->sr_attrs = NULL;
   2082  1.1  lukem 	if ( rs->sr_ctrls != NULL ) {
   2083  1.1  lukem 		ldap_controls_free( rs->sr_ctrls );
   2084  1.1  lukem 		rs->sr_ctrls = NULL;
   2085  1.1  lukem 	}
   2086  1.1  lukem 	if ( !BER_BVISNULL( &ent.e_name ) ) {
   2087  1.1  lukem 		free( ent.e_name.bv_val );
   2088  1.1  lukem 		BER_BVZERO( &ent.e_name );
   2089  1.1  lukem 	}
   2090  1.1  lukem 	if ( !BER_BVISNULL( &ent.e_nname ) ) {
   2091  1.1  lukem 		free( ent.e_nname.bv_val );
   2092  1.1  lukem 		BER_BVZERO( &ent.e_nname );
   2093  1.1  lukem 	}
   2094  1.1  lukem 	entry_clean( &ent );
   2095  1.1  lukem 
   2096  1.1  lukem 	return rc;
   2097  1.1  lukem }
   2098  1.1  lukem 
   2099