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