Home | History | Annotate | Line # | Download | only in back-mdb
search.c revision 1.1.1.1.2.2
      1  1.1.1.1.2.2  tls /*	$NetBSD: search.c,v 1.1.1.1.2.2 2014/08/10 07:09:49 tls Exp $	*/
      2  1.1.1.1.2.2  tls 
      3  1.1.1.1.2.2  tls /* search.c - search operation */
      4  1.1.1.1.2.2  tls /* $OpenLDAP$ */
      5  1.1.1.1.2.2  tls /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6  1.1.1.1.2.2  tls  *
      7  1.1.1.1.2.2  tls  * Copyright 2000-2014 The OpenLDAP Foundation.
      8  1.1.1.1.2.2  tls  * All rights reserved.
      9  1.1.1.1.2.2  tls  *
     10  1.1.1.1.2.2  tls  * Redistribution and use in source and binary forms, with or without
     11  1.1.1.1.2.2  tls  * modification, are permitted only as authorized by the OpenLDAP
     12  1.1.1.1.2.2  tls  * Public License.
     13  1.1.1.1.2.2  tls  *
     14  1.1.1.1.2.2  tls  * A copy of this license is available in the file LICENSE in the
     15  1.1.1.1.2.2  tls  * top-level directory of the distribution or, alternatively, at
     16  1.1.1.1.2.2  tls  * <http://www.OpenLDAP.org/license.html>.
     17  1.1.1.1.2.2  tls  */
     18  1.1.1.1.2.2  tls 
     19  1.1.1.1.2.2  tls #include "portable.h"
     20  1.1.1.1.2.2  tls 
     21  1.1.1.1.2.2  tls #include <stdio.h>
     22  1.1.1.1.2.2  tls #include <ac/string.h>
     23  1.1.1.1.2.2  tls 
     24  1.1.1.1.2.2  tls #include "back-mdb.h"
     25  1.1.1.1.2.2  tls #include "idl.h"
     26  1.1.1.1.2.2  tls 
     27  1.1.1.1.2.2  tls static int base_candidate(
     28  1.1.1.1.2.2  tls 	BackendDB	*be,
     29  1.1.1.1.2.2  tls 	Entry	*e,
     30  1.1.1.1.2.2  tls 	ID		*ids );
     31  1.1.1.1.2.2  tls 
     32  1.1.1.1.2.2  tls static int search_candidates(
     33  1.1.1.1.2.2  tls 	Operation *op,
     34  1.1.1.1.2.2  tls 	SlapReply *rs,
     35  1.1.1.1.2.2  tls 	Entry *e,
     36  1.1.1.1.2.2  tls 	MDB_txn *txn,
     37  1.1.1.1.2.2  tls 	MDB_cursor *mci,
     38  1.1.1.1.2.2  tls 	ID	*ids,
     39  1.1.1.1.2.2  tls 	ID2L	scopes );
     40  1.1.1.1.2.2  tls 
     41  1.1.1.1.2.2  tls static int parse_paged_cookie( Operation *op, SlapReply *rs );
     42  1.1.1.1.2.2  tls 
     43  1.1.1.1.2.2  tls static void send_paged_response(
     44  1.1.1.1.2.2  tls 	Operation *op,
     45  1.1.1.1.2.2  tls 	SlapReply *rs,
     46  1.1.1.1.2.2  tls 	ID  *lastid,
     47  1.1.1.1.2.2  tls 	int tentries );
     48  1.1.1.1.2.2  tls 
     49  1.1.1.1.2.2  tls /* Dereference aliases for a single alias entry. Return the final
     50  1.1.1.1.2.2  tls  * dereferenced entry on success, NULL on any failure.
     51  1.1.1.1.2.2  tls  */
     52  1.1.1.1.2.2  tls static Entry * deref_base (
     53  1.1.1.1.2.2  tls 	Operation *op,
     54  1.1.1.1.2.2  tls 	SlapReply *rs,
     55  1.1.1.1.2.2  tls 	Entry *e,
     56  1.1.1.1.2.2  tls 	Entry **matched,
     57  1.1.1.1.2.2  tls 	MDB_txn *txn,
     58  1.1.1.1.2.2  tls 	ID	*tmp,
     59  1.1.1.1.2.2  tls 	ID	*visited )
     60  1.1.1.1.2.2  tls {
     61  1.1.1.1.2.2  tls 	struct berval ndn;
     62  1.1.1.1.2.2  tls 
     63  1.1.1.1.2.2  tls 	rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
     64  1.1.1.1.2.2  tls 	rs->sr_text = "maximum deref depth exceeded";
     65  1.1.1.1.2.2  tls 
     66  1.1.1.1.2.2  tls 	for (;;) {
     67  1.1.1.1.2.2  tls 		/* Remember the last entry we looked at, so we can
     68  1.1.1.1.2.2  tls 		 * report broken links
     69  1.1.1.1.2.2  tls 		 */
     70  1.1.1.1.2.2  tls 		*matched = e;
     71  1.1.1.1.2.2  tls 
     72  1.1.1.1.2.2  tls 		if (MDB_IDL_N(tmp) >= op->o_bd->be_max_deref_depth) {
     73  1.1.1.1.2.2  tls 			e = NULL;
     74  1.1.1.1.2.2  tls 			break;
     75  1.1.1.1.2.2  tls 		}
     76  1.1.1.1.2.2  tls 
     77  1.1.1.1.2.2  tls 		/* If this is part of a subtree or onelevel search,
     78  1.1.1.1.2.2  tls 		 * have we seen this ID before? If so, quit.
     79  1.1.1.1.2.2  tls 		 */
     80  1.1.1.1.2.2  tls 		if ( visited && mdb_idl_insert( visited, e->e_id ) ) {
     81  1.1.1.1.2.2  tls 			e = NULL;
     82  1.1.1.1.2.2  tls 			break;
     83  1.1.1.1.2.2  tls 		}
     84  1.1.1.1.2.2  tls 
     85  1.1.1.1.2.2  tls 		/* If we've seen this ID during this deref iteration,
     86  1.1.1.1.2.2  tls 		 * we've hit a loop.
     87  1.1.1.1.2.2  tls 		 */
     88  1.1.1.1.2.2  tls 		if ( mdb_idl_insert( tmp, e->e_id ) ) {
     89  1.1.1.1.2.2  tls 			rs->sr_err = LDAP_ALIAS_PROBLEM;
     90  1.1.1.1.2.2  tls 			rs->sr_text = "circular alias";
     91  1.1.1.1.2.2  tls 			e = NULL;
     92  1.1.1.1.2.2  tls 			break;
     93  1.1.1.1.2.2  tls 		}
     94  1.1.1.1.2.2  tls 
     95  1.1.1.1.2.2  tls 		/* If there was a problem getting the aliasedObjectName,
     96  1.1.1.1.2.2  tls 		 * get_alias_dn will have set the error status.
     97  1.1.1.1.2.2  tls 		 */
     98  1.1.1.1.2.2  tls 		if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
     99  1.1.1.1.2.2  tls 			e = NULL;
    100  1.1.1.1.2.2  tls 			break;
    101  1.1.1.1.2.2  tls 		}
    102  1.1.1.1.2.2  tls 
    103  1.1.1.1.2.2  tls 		rs->sr_err = mdb_dn2entry( op, txn, NULL, &ndn, &e, NULL, 0 );
    104  1.1.1.1.2.2  tls 		if (rs->sr_err) {
    105  1.1.1.1.2.2  tls 			rs->sr_err = LDAP_ALIAS_PROBLEM;
    106  1.1.1.1.2.2  tls 			rs->sr_text = "aliasedObject not found";
    107  1.1.1.1.2.2  tls 			break;
    108  1.1.1.1.2.2  tls 		}
    109  1.1.1.1.2.2  tls 
    110  1.1.1.1.2.2  tls 		/* Free the previous entry, continue to work with the
    111  1.1.1.1.2.2  tls 		 * one we just retrieved.
    112  1.1.1.1.2.2  tls 		 */
    113  1.1.1.1.2.2  tls 		mdb_entry_return( op, *matched );
    114  1.1.1.1.2.2  tls 
    115  1.1.1.1.2.2  tls 		/* We found a regular entry. Return this to the caller.
    116  1.1.1.1.2.2  tls 		 */
    117  1.1.1.1.2.2  tls 		if (!is_entry_alias(e)) {
    118  1.1.1.1.2.2  tls 			rs->sr_err = LDAP_SUCCESS;
    119  1.1.1.1.2.2  tls 			rs->sr_text = NULL;
    120  1.1.1.1.2.2  tls 			break;
    121  1.1.1.1.2.2  tls 		}
    122  1.1.1.1.2.2  tls 	}
    123  1.1.1.1.2.2  tls 	return e;
    124  1.1.1.1.2.2  tls }
    125  1.1.1.1.2.2  tls 
    126  1.1.1.1.2.2  tls /* Look for and dereference all aliases within the search scope.
    127  1.1.1.1.2.2  tls  * Requires "stack" to be able to hold 6 levels of DB_SIZE IDLs.
    128  1.1.1.1.2.2  tls  * Of course we're hardcoded to require a minimum of 8 UM_SIZE
    129  1.1.1.1.2.2  tls  * IDLs so this is never a problem.
    130  1.1.1.1.2.2  tls  */
    131  1.1.1.1.2.2  tls static int search_aliases(
    132  1.1.1.1.2.2  tls 	Operation *op,
    133  1.1.1.1.2.2  tls 	SlapReply *rs,
    134  1.1.1.1.2.2  tls 	ID e_id,
    135  1.1.1.1.2.2  tls 	MDB_txn *txn,
    136  1.1.1.1.2.2  tls 	MDB_cursor *mci,
    137  1.1.1.1.2.2  tls 	ID2L scopes,
    138  1.1.1.1.2.2  tls 	ID *stack )
    139  1.1.1.1.2.2  tls {
    140  1.1.1.1.2.2  tls 	ID *aliases, *curscop, *visited, *newsubs, *oldsubs, *tmp;
    141  1.1.1.1.2.2  tls 	ID cursora, ida, cursoro, ido;
    142  1.1.1.1.2.2  tls 	Entry *matched, *a;
    143  1.1.1.1.2.2  tls 	struct berval bv_alias = BER_BVC( "alias" );
    144  1.1.1.1.2.2  tls 	AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
    145  1.1.1.1.2.2  tls 	Filter	af;
    146  1.1.1.1.2.2  tls 	int first = 1;
    147  1.1.1.1.2.2  tls 
    148  1.1.1.1.2.2  tls 	aliases = stack;	/* IDL of all aliases in the database */
    149  1.1.1.1.2.2  tls 	curscop = aliases + MDB_IDL_DB_SIZE;	/* Aliases in the current scope */
    150  1.1.1.1.2.2  tls 	visited = curscop + MDB_IDL_DB_SIZE;	/* IDs we've seen in this search */
    151  1.1.1.1.2.2  tls 	newsubs = visited + MDB_IDL_DB_SIZE;	/* New subtrees we've added */
    152  1.1.1.1.2.2  tls 	oldsubs = newsubs + MDB_IDL_DB_SIZE;	/* Subtrees added previously */
    153  1.1.1.1.2.2  tls 	tmp = oldsubs + MDB_IDL_DB_SIZE;	/* Scratch space for deref_base() */
    154  1.1.1.1.2.2  tls 
    155  1.1.1.1.2.2  tls 	af.f_choice = LDAP_FILTER_EQUALITY;
    156  1.1.1.1.2.2  tls 	af.f_ava = &aa_alias;
    157  1.1.1.1.2.2  tls 	af.f_av_desc = slap_schema.si_ad_objectClass;
    158  1.1.1.1.2.2  tls 	af.f_av_value = bv_alias;
    159  1.1.1.1.2.2  tls 	af.f_next = NULL;
    160  1.1.1.1.2.2  tls 
    161  1.1.1.1.2.2  tls 	/* Find all aliases in database */
    162  1.1.1.1.2.2  tls 	MDB_IDL_ZERO( aliases );
    163  1.1.1.1.2.2  tls 	rs->sr_err = mdb_filter_candidates( op, txn, &af, aliases,
    164  1.1.1.1.2.2  tls 		curscop, visited );
    165  1.1.1.1.2.2  tls 	if (rs->sr_err != LDAP_SUCCESS || MDB_IDL_IS_ZERO( aliases )) {
    166  1.1.1.1.2.2  tls 		return rs->sr_err;
    167  1.1.1.1.2.2  tls 	}
    168  1.1.1.1.2.2  tls 	oldsubs[0] = 1;
    169  1.1.1.1.2.2  tls 	oldsubs[1] = e_id;
    170  1.1.1.1.2.2  tls 
    171  1.1.1.1.2.2  tls 	MDB_IDL_ZERO( visited );
    172  1.1.1.1.2.2  tls 	MDB_IDL_ZERO( newsubs );
    173  1.1.1.1.2.2  tls 
    174  1.1.1.1.2.2  tls 	cursoro = 0;
    175  1.1.1.1.2.2  tls 	ido = mdb_idl_first( oldsubs, &cursoro );
    176  1.1.1.1.2.2  tls 
    177  1.1.1.1.2.2  tls 	for (;;) {
    178  1.1.1.1.2.2  tls 		/* Set curscop to only the aliases in the current scope. Start with
    179  1.1.1.1.2.2  tls 		 * all the aliases, then get the intersection with the scope.
    180  1.1.1.1.2.2  tls 		 */
    181  1.1.1.1.2.2  tls 		rs->sr_err = mdb_idscope( op, txn, e_id, aliases, curscop );
    182  1.1.1.1.2.2  tls 
    183  1.1.1.1.2.2  tls 		/* Dereference all of the aliases in the current scope. */
    184  1.1.1.1.2.2  tls 		cursora = 0;
    185  1.1.1.1.2.2  tls 		for (ida = mdb_idl_first(curscop, &cursora); ida != NOID;
    186  1.1.1.1.2.2  tls 			ida = mdb_idl_next(curscop, &cursora))
    187  1.1.1.1.2.2  tls 		{
    188  1.1.1.1.2.2  tls 			rs->sr_err = mdb_id2entry(op, mci, ida, &a);
    189  1.1.1.1.2.2  tls 			if (rs->sr_err != LDAP_SUCCESS) {
    190  1.1.1.1.2.2  tls 				continue;
    191  1.1.1.1.2.2  tls 			}
    192  1.1.1.1.2.2  tls 
    193  1.1.1.1.2.2  tls 			/* This should only happen if the curscop IDL has maxed out and
    194  1.1.1.1.2.2  tls 			 * turned into a range that spans IDs indiscriminately
    195  1.1.1.1.2.2  tls 			 */
    196  1.1.1.1.2.2  tls 			if (!is_entry_alias(a)) {
    197  1.1.1.1.2.2  tls 				mdb_entry_return(op, a);
    198  1.1.1.1.2.2  tls 				continue;
    199  1.1.1.1.2.2  tls 			}
    200  1.1.1.1.2.2  tls 
    201  1.1.1.1.2.2  tls 			/* Actually dereference the alias */
    202  1.1.1.1.2.2  tls 			MDB_IDL_ZERO(tmp);
    203  1.1.1.1.2.2  tls 			a = deref_base( op, rs, a, &matched, txn,
    204  1.1.1.1.2.2  tls 				tmp, visited );
    205  1.1.1.1.2.2  tls 			if (a) {
    206  1.1.1.1.2.2  tls 				/* If the target was not already in our current scopes,
    207  1.1.1.1.2.2  tls 				 * make note of it in the newsubs list.
    208  1.1.1.1.2.2  tls 				 */
    209  1.1.1.1.2.2  tls 				ID2 mid;
    210  1.1.1.1.2.2  tls 				mid.mid = a->e_id;
    211  1.1.1.1.2.2  tls 				mid.mval.mv_data = NULL;
    212  1.1.1.1.2.2  tls 				if (mdb_id2l_insert(scopes, &mid) == 0) {
    213  1.1.1.1.2.2  tls 					mdb_idl_insert(newsubs, a->e_id);
    214  1.1.1.1.2.2  tls 				}
    215  1.1.1.1.2.2  tls 				mdb_entry_return( op, a );
    216  1.1.1.1.2.2  tls 
    217  1.1.1.1.2.2  tls 			} else if (matched) {
    218  1.1.1.1.2.2  tls 				/* Alias could not be dereferenced, or it deref'd to
    219  1.1.1.1.2.2  tls 				 * an ID we've already seen. Ignore it.
    220  1.1.1.1.2.2  tls 				 */
    221  1.1.1.1.2.2  tls 				mdb_entry_return( op, matched );
    222  1.1.1.1.2.2  tls 				rs->sr_text = NULL;
    223  1.1.1.1.2.2  tls 				rs->sr_err = 0;
    224  1.1.1.1.2.2  tls 			}
    225  1.1.1.1.2.2  tls 		}
    226  1.1.1.1.2.2  tls 		/* If this is a OneLevel search, we're done; oldsubs only had one
    227  1.1.1.1.2.2  tls 		 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
    228  1.1.1.1.2.2  tls 		 */
    229  1.1.1.1.2.2  tls 		if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
    230  1.1.1.1.2.2  tls nextido:
    231  1.1.1.1.2.2  tls 		ido = mdb_idl_next( oldsubs, &cursoro );
    232  1.1.1.1.2.2  tls 
    233  1.1.1.1.2.2  tls 		/* If we're done processing the old scopes, did we add any new
    234  1.1.1.1.2.2  tls 		 * scopes in this iteration? If so, go back and do those now.
    235  1.1.1.1.2.2  tls 		 */
    236  1.1.1.1.2.2  tls 		if (ido == NOID) {
    237  1.1.1.1.2.2  tls 			if (MDB_IDL_IS_ZERO(newsubs)) break;
    238  1.1.1.1.2.2  tls 			MDB_IDL_CPY(oldsubs, newsubs);
    239  1.1.1.1.2.2  tls 			MDB_IDL_ZERO(newsubs);
    240  1.1.1.1.2.2  tls 			cursoro = 0;
    241  1.1.1.1.2.2  tls 			ido = mdb_idl_first( oldsubs, &cursoro );
    242  1.1.1.1.2.2  tls 		}
    243  1.1.1.1.2.2  tls 
    244  1.1.1.1.2.2  tls 		/* Find the entry corresponding to the next scope. If it can't
    245  1.1.1.1.2.2  tls 		 * be found, ignore it and move on. This should never happen;
    246  1.1.1.1.2.2  tls 		 * we should never see the ID of an entry that doesn't exist.
    247  1.1.1.1.2.2  tls 		 */
    248  1.1.1.1.2.2  tls 		{
    249  1.1.1.1.2.2  tls 			MDB_val edata;
    250  1.1.1.1.2.2  tls 			rs->sr_err = mdb_id2edata(op, mci, ido, &edata);
    251  1.1.1.1.2.2  tls 			if ( rs->sr_err != MDB_SUCCESS ) {
    252  1.1.1.1.2.2  tls 				goto nextido;
    253  1.1.1.1.2.2  tls 			}
    254  1.1.1.1.2.2  tls 			e_id = ido;
    255  1.1.1.1.2.2  tls 		}
    256  1.1.1.1.2.2  tls 	}
    257  1.1.1.1.2.2  tls 	return rs->sr_err;
    258  1.1.1.1.2.2  tls }
    259  1.1.1.1.2.2  tls 
    260  1.1.1.1.2.2  tls /* Get the next ID from the DB. Used if the candidate list is
    261  1.1.1.1.2.2  tls  * a range and simple iteration hits missing entryIDs
    262  1.1.1.1.2.2  tls  */
    263  1.1.1.1.2.2  tls static int
    264  1.1.1.1.2.2  tls mdb_get_nextid(MDB_cursor *mci, ID *cursor)
    265  1.1.1.1.2.2  tls {
    266  1.1.1.1.2.2  tls 	MDB_val key;
    267  1.1.1.1.2.2  tls 	ID id;
    268  1.1.1.1.2.2  tls 	int rc;
    269  1.1.1.1.2.2  tls 
    270  1.1.1.1.2.2  tls 	id = *cursor + 1;
    271  1.1.1.1.2.2  tls 	key.mv_data = &id;
    272  1.1.1.1.2.2  tls 	key.mv_size = sizeof(ID);
    273  1.1.1.1.2.2  tls 	rc = mdb_cursor_get( mci, &key, NULL, MDB_SET_RANGE );
    274  1.1.1.1.2.2  tls 	if ( rc )
    275  1.1.1.1.2.2  tls 		return rc;
    276  1.1.1.1.2.2  tls 	memcpy( cursor, key.mv_data, sizeof(ID));
    277  1.1.1.1.2.2  tls 	return 0;
    278  1.1.1.1.2.2  tls }
    279  1.1.1.1.2.2  tls 
    280  1.1.1.1.2.2  tls static void scope_chunk_free( void *key, void *data )
    281  1.1.1.1.2.2  tls {
    282  1.1.1.1.2.2  tls 	ID2 *p1, *p2;
    283  1.1.1.1.2.2  tls 	for (p1 = data; p1; p1 = p2) {
    284  1.1.1.1.2.2  tls 		p2 = p1[0].mval.mv_data;
    285  1.1.1.1.2.2  tls 		ber_memfree_x(p1, NULL);
    286  1.1.1.1.2.2  tls 	}
    287  1.1.1.1.2.2  tls }
    288  1.1.1.1.2.2  tls 
    289  1.1.1.1.2.2  tls static ID2 *scope_chunk_get( Operation *op )
    290  1.1.1.1.2.2  tls {
    291  1.1.1.1.2.2  tls 	struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
    292  1.1.1.1.2.2  tls 	ID2 *ret = NULL;
    293  1.1.1.1.2.2  tls 
    294  1.1.1.1.2.2  tls 	ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
    295  1.1.1.1.2.2  tls 			(void *)&ret, NULL );
    296  1.1.1.1.2.2  tls 	if ( !ret ) {
    297  1.1.1.1.2.2  tls 		ret = ch_malloc( MDB_IDL_UM_SIZE * sizeof( ID2 ));
    298  1.1.1.1.2.2  tls 	} else {
    299  1.1.1.1.2.2  tls 		void *r2 = ret[0].mval.mv_data;
    300  1.1.1.1.2.2  tls 		ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
    301  1.1.1.1.2.2  tls 			r2, scope_chunk_free, NULL, NULL );
    302  1.1.1.1.2.2  tls 	}
    303  1.1.1.1.2.2  tls 	return ret;
    304  1.1.1.1.2.2  tls }
    305  1.1.1.1.2.2  tls 
    306  1.1.1.1.2.2  tls static void scope_chunk_ret( Operation *op, ID2 *scopes )
    307  1.1.1.1.2.2  tls {
    308  1.1.1.1.2.2  tls 	struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
    309  1.1.1.1.2.2  tls 	void *ret = NULL;
    310  1.1.1.1.2.2  tls 
    311  1.1.1.1.2.2  tls 	ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
    312  1.1.1.1.2.2  tls 			&ret, NULL );
    313  1.1.1.1.2.2  tls 	scopes[0].mval.mv_data = ret;
    314  1.1.1.1.2.2  tls 	ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
    315  1.1.1.1.2.2  tls 			(void *)scopes, scope_chunk_free, NULL, NULL );
    316  1.1.1.1.2.2  tls }
    317  1.1.1.1.2.2  tls 
    318  1.1.1.1.2.2  tls int
    319  1.1.1.1.2.2  tls mdb_search( Operation *op, SlapReply *rs )
    320  1.1.1.1.2.2  tls {
    321  1.1.1.1.2.2  tls 	struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
    322  1.1.1.1.2.2  tls 	ID		id, cursor, nsubs, ncand, cscope;
    323  1.1.1.1.2.2  tls 	ID		lastid = NOID;
    324  1.1.1.1.2.2  tls 	ID		candidates[MDB_IDL_UM_SIZE];
    325  1.1.1.1.2.2  tls 	ID		iscopes[MDB_IDL_DB_SIZE];
    326  1.1.1.1.2.2  tls 	ID2		*scopes;
    327  1.1.1.1.2.2  tls 	Entry		*e = NULL, *base = NULL;
    328  1.1.1.1.2.2  tls 	Entry		*matched = NULL;
    329  1.1.1.1.2.2  tls 	AttributeName	*attrs;
    330  1.1.1.1.2.2  tls 	slap_mask_t	mask;
    331  1.1.1.1.2.2  tls 	time_t		stoptime;
    332  1.1.1.1.2.2  tls 	int		manageDSAit;
    333  1.1.1.1.2.2  tls 	int		tentries = 0;
    334  1.1.1.1.2.2  tls 	IdScopes	isc;
    335  1.1.1.1.2.2  tls 	MDB_cursor	*mci, *mcd;
    336  1.1.1.1.2.2  tls 
    337  1.1.1.1.2.2  tls 	mdb_op_info	opinfo = {{{0}}}, *moi = &opinfo;
    338  1.1.1.1.2.2  tls 	MDB_txn			*ltid = NULL;
    339  1.1.1.1.2.2  tls 
    340  1.1.1.1.2.2  tls 	Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_search) "\n", 0, 0, 0);
    341  1.1.1.1.2.2  tls 	attrs = op->oq_search.rs_attrs;
    342  1.1.1.1.2.2  tls 
    343  1.1.1.1.2.2  tls 	manageDSAit = get_manageDSAit( op );
    344  1.1.1.1.2.2  tls 
    345  1.1.1.1.2.2  tls 	rs->sr_err = mdb_opinfo_get( op, mdb, 1, &moi );
    346  1.1.1.1.2.2  tls 	switch(rs->sr_err) {
    347  1.1.1.1.2.2  tls 	case 0:
    348  1.1.1.1.2.2  tls 		break;
    349  1.1.1.1.2.2  tls 	default:
    350  1.1.1.1.2.2  tls 		send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
    351  1.1.1.1.2.2  tls 		return rs->sr_err;
    352  1.1.1.1.2.2  tls 	}
    353  1.1.1.1.2.2  tls 
    354  1.1.1.1.2.2  tls 	ltid = moi->moi_txn;
    355  1.1.1.1.2.2  tls 
    356  1.1.1.1.2.2  tls 	rs->sr_err = mdb_cursor_open( ltid, mdb->mi_id2entry, &mci );
    357  1.1.1.1.2.2  tls 	if ( rs->sr_err ) {
    358  1.1.1.1.2.2  tls 		send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
    359  1.1.1.1.2.2  tls 		return rs->sr_err;
    360  1.1.1.1.2.2  tls 	}
    361  1.1.1.1.2.2  tls 
    362  1.1.1.1.2.2  tls 	rs->sr_err = mdb_cursor_open( ltid, mdb->mi_dn2id, &mcd );
    363  1.1.1.1.2.2  tls 	if ( rs->sr_err ) {
    364  1.1.1.1.2.2  tls 		mdb_cursor_close( mci );
    365  1.1.1.1.2.2  tls 		send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
    366  1.1.1.1.2.2  tls 		return rs->sr_err;
    367  1.1.1.1.2.2  tls 	}
    368  1.1.1.1.2.2  tls 
    369  1.1.1.1.2.2  tls 	scopes = scope_chunk_get( op );
    370  1.1.1.1.2.2  tls 	isc.mt = ltid;
    371  1.1.1.1.2.2  tls 	isc.mc = mcd;
    372  1.1.1.1.2.2  tls 	isc.scopes = scopes;
    373  1.1.1.1.2.2  tls 	isc.oscope = op->ors_scope;
    374  1.1.1.1.2.2  tls 
    375  1.1.1.1.2.2  tls 	if ( op->ors_deref & LDAP_DEREF_FINDING ) {
    376  1.1.1.1.2.2  tls 		MDB_IDL_ZERO(candidates);
    377  1.1.1.1.2.2  tls 	}
    378  1.1.1.1.2.2  tls dn2entry_retry:
    379  1.1.1.1.2.2  tls 	/* get entry with reader lock */
    380  1.1.1.1.2.2  tls 	rs->sr_err = mdb_dn2entry( op, ltid, mcd, &op->o_req_ndn, &e, &nsubs, 1 );
    381  1.1.1.1.2.2  tls 
    382  1.1.1.1.2.2  tls 	switch(rs->sr_err) {
    383  1.1.1.1.2.2  tls 	case MDB_NOTFOUND:
    384  1.1.1.1.2.2  tls 		matched = e;
    385  1.1.1.1.2.2  tls 		e = NULL;
    386  1.1.1.1.2.2  tls 		break;
    387  1.1.1.1.2.2  tls 	case 0:
    388  1.1.1.1.2.2  tls 		break;
    389  1.1.1.1.2.2  tls 	case LDAP_BUSY:
    390  1.1.1.1.2.2  tls 		send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
    391  1.1.1.1.2.2  tls 		goto done;
    392  1.1.1.1.2.2  tls 	default:
    393  1.1.1.1.2.2  tls 		send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
    394  1.1.1.1.2.2  tls 		goto done;
    395  1.1.1.1.2.2  tls 	}
    396  1.1.1.1.2.2  tls 
    397  1.1.1.1.2.2  tls 	if ( op->ors_deref & LDAP_DEREF_FINDING ) {
    398  1.1.1.1.2.2  tls 		if ( matched && is_entry_alias( matched )) {
    399  1.1.1.1.2.2  tls 			struct berval stub;
    400  1.1.1.1.2.2  tls 
    401  1.1.1.1.2.2  tls 			stub.bv_val = op->o_req_ndn.bv_val;
    402  1.1.1.1.2.2  tls 			stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
    403  1.1.1.1.2.2  tls 			e = deref_base( op, rs, matched, &matched, ltid,
    404  1.1.1.1.2.2  tls 				candidates, NULL );
    405  1.1.1.1.2.2  tls 			if ( e ) {
    406  1.1.1.1.2.2  tls 				build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
    407  1.1.1.1.2.2  tls 					op->o_tmpmemctx );
    408  1.1.1.1.2.2  tls 				mdb_entry_return(op, e);
    409  1.1.1.1.2.2  tls 				matched = NULL;
    410  1.1.1.1.2.2  tls 				goto dn2entry_retry;
    411  1.1.1.1.2.2  tls 			}
    412  1.1.1.1.2.2  tls 		} else if ( e && is_entry_alias( e )) {
    413  1.1.1.1.2.2  tls 			e = deref_base( op, rs, e, &matched, ltid,
    414  1.1.1.1.2.2  tls 				candidates, NULL );
    415  1.1.1.1.2.2  tls 		}
    416  1.1.1.1.2.2  tls 	}
    417  1.1.1.1.2.2  tls 
    418  1.1.1.1.2.2  tls 	if ( e == NULL ) {
    419  1.1.1.1.2.2  tls 		struct berval matched_dn = BER_BVNULL;
    420  1.1.1.1.2.2  tls 
    421  1.1.1.1.2.2  tls 		if ( matched != NULL ) {
    422  1.1.1.1.2.2  tls 			BerVarray erefs = NULL;
    423  1.1.1.1.2.2  tls 
    424  1.1.1.1.2.2  tls 			/* return referral only if "disclose"
    425  1.1.1.1.2.2  tls 			 * is granted on the object */
    426  1.1.1.1.2.2  tls 			if ( ! access_allowed( op, matched,
    427  1.1.1.1.2.2  tls 						slap_schema.si_ad_entry,
    428  1.1.1.1.2.2  tls 						NULL, ACL_DISCLOSE, NULL ) )
    429  1.1.1.1.2.2  tls 			{
    430  1.1.1.1.2.2  tls 				rs->sr_err = LDAP_NO_SUCH_OBJECT;
    431  1.1.1.1.2.2  tls 
    432  1.1.1.1.2.2  tls 			} else {
    433  1.1.1.1.2.2  tls 				ber_dupbv( &matched_dn, &matched->e_name );
    434  1.1.1.1.2.2  tls 
    435  1.1.1.1.2.2  tls 				erefs = is_entry_referral( matched )
    436  1.1.1.1.2.2  tls 					? get_entry_referrals( op, matched )
    437  1.1.1.1.2.2  tls 					: NULL;
    438  1.1.1.1.2.2  tls 				if ( rs->sr_err == MDB_NOTFOUND )
    439  1.1.1.1.2.2  tls 					rs->sr_err = LDAP_REFERRAL;
    440  1.1.1.1.2.2  tls 				rs->sr_matched = matched_dn.bv_val;
    441  1.1.1.1.2.2  tls 			}
    442  1.1.1.1.2.2  tls 
    443  1.1.1.1.2.2  tls 			mdb_entry_return(op, matched);
    444  1.1.1.1.2.2  tls 			matched = NULL;
    445  1.1.1.1.2.2  tls 
    446  1.1.1.1.2.2  tls 			if ( erefs ) {
    447  1.1.1.1.2.2  tls 				rs->sr_ref = referral_rewrite( erefs, &matched_dn,
    448  1.1.1.1.2.2  tls 					&op->o_req_dn, op->oq_search.rs_scope );
    449  1.1.1.1.2.2  tls 				ber_bvarray_free( erefs );
    450  1.1.1.1.2.2  tls 			}
    451  1.1.1.1.2.2  tls 
    452  1.1.1.1.2.2  tls 		} else {
    453  1.1.1.1.2.2  tls 			rs->sr_ref = referral_rewrite( default_referral,
    454  1.1.1.1.2.2  tls 				NULL, &op->o_req_dn, op->oq_search.rs_scope );
    455  1.1.1.1.2.2  tls 			rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
    456  1.1.1.1.2.2  tls 		}
    457  1.1.1.1.2.2  tls 
    458  1.1.1.1.2.2  tls 		send_ldap_result( op, rs );
    459  1.1.1.1.2.2  tls 
    460  1.1.1.1.2.2  tls 		if ( rs->sr_ref ) {
    461  1.1.1.1.2.2  tls 			ber_bvarray_free( rs->sr_ref );
    462  1.1.1.1.2.2  tls 			rs->sr_ref = NULL;
    463  1.1.1.1.2.2  tls 		}
    464  1.1.1.1.2.2  tls 		if ( !BER_BVISNULL( &matched_dn ) ) {
    465  1.1.1.1.2.2  tls 			ber_memfree( matched_dn.bv_val );
    466  1.1.1.1.2.2  tls 			rs->sr_matched = NULL;
    467  1.1.1.1.2.2  tls 		}
    468  1.1.1.1.2.2  tls 		goto done;
    469  1.1.1.1.2.2  tls 	}
    470  1.1.1.1.2.2  tls 
    471  1.1.1.1.2.2  tls 	/* NOTE: __NEW__ "search" access is required
    472  1.1.1.1.2.2  tls 	 * on searchBase object */
    473  1.1.1.1.2.2  tls 	if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
    474  1.1.1.1.2.2  tls 				NULL, ACL_SEARCH, NULL, &mask ) )
    475  1.1.1.1.2.2  tls 	{
    476  1.1.1.1.2.2  tls 		if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
    477  1.1.1.1.2.2  tls 			rs->sr_err = LDAP_NO_SUCH_OBJECT;
    478  1.1.1.1.2.2  tls 		} else {
    479  1.1.1.1.2.2  tls 			rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
    480  1.1.1.1.2.2  tls 		}
    481  1.1.1.1.2.2  tls 
    482  1.1.1.1.2.2  tls 		mdb_entry_return( op,e);
    483  1.1.1.1.2.2  tls 		send_ldap_result( op, rs );
    484  1.1.1.1.2.2  tls 		goto done;
    485  1.1.1.1.2.2  tls 	}
    486  1.1.1.1.2.2  tls 
    487  1.1.1.1.2.2  tls 	if ( !manageDSAit && is_entry_referral( e ) ) {
    488  1.1.1.1.2.2  tls 		/* entry is a referral */
    489  1.1.1.1.2.2  tls 		struct berval matched_dn = BER_BVNULL;
    490  1.1.1.1.2.2  tls 		BerVarray erefs = NULL;
    491  1.1.1.1.2.2  tls 
    492  1.1.1.1.2.2  tls 		ber_dupbv( &matched_dn, &e->e_name );
    493  1.1.1.1.2.2  tls 		erefs = get_entry_referrals( op, e );
    494  1.1.1.1.2.2  tls 
    495  1.1.1.1.2.2  tls 		rs->sr_err = LDAP_REFERRAL;
    496  1.1.1.1.2.2  tls 
    497  1.1.1.1.2.2  tls 		mdb_entry_return( op, e );
    498  1.1.1.1.2.2  tls 		e = NULL;
    499  1.1.1.1.2.2  tls 
    500  1.1.1.1.2.2  tls 		if ( erefs ) {
    501  1.1.1.1.2.2  tls 			rs->sr_ref = referral_rewrite( erefs, &matched_dn,
    502  1.1.1.1.2.2  tls 				&op->o_req_dn, op->oq_search.rs_scope );
    503  1.1.1.1.2.2  tls 			ber_bvarray_free( erefs );
    504  1.1.1.1.2.2  tls 
    505  1.1.1.1.2.2  tls 			if ( !rs->sr_ref ) {
    506  1.1.1.1.2.2  tls 				rs->sr_text = "bad_referral object";
    507  1.1.1.1.2.2  tls 			}
    508  1.1.1.1.2.2  tls 		}
    509  1.1.1.1.2.2  tls 
    510  1.1.1.1.2.2  tls 		Debug( LDAP_DEBUG_TRACE,
    511  1.1.1.1.2.2  tls 			LDAP_XSTRING(mdb_search) ": entry is referral\n",
    512  1.1.1.1.2.2  tls 			0, 0, 0 );
    513  1.1.1.1.2.2  tls 
    514  1.1.1.1.2.2  tls 		rs->sr_matched = matched_dn.bv_val;
    515  1.1.1.1.2.2  tls 		send_ldap_result( op, rs );
    516  1.1.1.1.2.2  tls 
    517  1.1.1.1.2.2  tls 		ber_bvarray_free( rs->sr_ref );
    518  1.1.1.1.2.2  tls 		rs->sr_ref = NULL;
    519  1.1.1.1.2.2  tls 		ber_memfree( matched_dn.bv_val );
    520  1.1.1.1.2.2  tls 		rs->sr_matched = NULL;
    521  1.1.1.1.2.2  tls 		goto done;
    522  1.1.1.1.2.2  tls 	}
    523  1.1.1.1.2.2  tls 
    524  1.1.1.1.2.2  tls 	if ( get_assert( op ) &&
    525  1.1.1.1.2.2  tls 		( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
    526  1.1.1.1.2.2  tls 	{
    527  1.1.1.1.2.2  tls 		rs->sr_err = LDAP_ASSERTION_FAILED;
    528  1.1.1.1.2.2  tls 		mdb_entry_return( op,e);
    529  1.1.1.1.2.2  tls 		send_ldap_result( op, rs );
    530  1.1.1.1.2.2  tls 		goto done;
    531  1.1.1.1.2.2  tls 	}
    532  1.1.1.1.2.2  tls 
    533  1.1.1.1.2.2  tls 	/* compute it anyway; root does not use it */
    534  1.1.1.1.2.2  tls 	stoptime = op->o_time + op->ors_tlimit;
    535  1.1.1.1.2.2  tls 
    536  1.1.1.1.2.2  tls 	base = e;
    537  1.1.1.1.2.2  tls 
    538  1.1.1.1.2.2  tls 	e = NULL;
    539  1.1.1.1.2.2  tls 
    540  1.1.1.1.2.2  tls 	/* select candidates */
    541  1.1.1.1.2.2  tls 	if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
    542  1.1.1.1.2.2  tls 		rs->sr_err = base_candidate( op->o_bd, base, candidates );
    543  1.1.1.1.2.2  tls 		scopes[0].mid = 0;
    544  1.1.1.1.2.2  tls 		ncand = 1;
    545  1.1.1.1.2.2  tls 	} else {
    546  1.1.1.1.2.2  tls 		if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
    547  1.1.1.1.2.2  tls 			size_t nkids;
    548  1.1.1.1.2.2  tls 			MDB_val key, data;
    549  1.1.1.1.2.2  tls 			key.mv_data = &base->e_id;
    550  1.1.1.1.2.2  tls 			key.mv_size = sizeof( ID );
    551  1.1.1.1.2.2  tls 			mdb_cursor_get( mcd, &key, &data, MDB_SET );
    552  1.1.1.1.2.2  tls 			mdb_cursor_count( mcd, &nkids );
    553  1.1.1.1.2.2  tls 			nsubs = nkids - 1;
    554  1.1.1.1.2.2  tls 		} else if ( !base->e_id ) {
    555  1.1.1.1.2.2  tls 			/* we don't maintain nsubs for entryID 0.
    556  1.1.1.1.2.2  tls 			 * just grab entry count from id2entry stat
    557  1.1.1.1.2.2  tls 			 */
    558  1.1.1.1.2.2  tls 			MDB_stat ms;
    559  1.1.1.1.2.2  tls 			mdb_stat( ltid, mdb->mi_id2entry, &ms );
    560  1.1.1.1.2.2  tls 			nsubs = ms.ms_entries;
    561  1.1.1.1.2.2  tls 		}
    562  1.1.1.1.2.2  tls 		MDB_IDL_ZERO( candidates );
    563  1.1.1.1.2.2  tls 		scopes[0].mid = 1;
    564  1.1.1.1.2.2  tls 		scopes[1].mid = base->e_id;
    565  1.1.1.1.2.2  tls 		scopes[1].mval.mv_data = NULL;
    566  1.1.1.1.2.2  tls 		rs->sr_err = search_candidates( op, rs, base,
    567  1.1.1.1.2.2  tls 			ltid, mci, candidates, scopes );
    568  1.1.1.1.2.2  tls 		ncand = MDB_IDL_N( candidates );
    569  1.1.1.1.2.2  tls 		if ( !base->e_id || ncand == NOID ) {
    570  1.1.1.1.2.2  tls 			/* grab entry count from id2entry stat
    571  1.1.1.1.2.2  tls 			 */
    572  1.1.1.1.2.2  tls 			MDB_stat ms;
    573  1.1.1.1.2.2  tls 			mdb_stat( ltid, mdb->mi_id2entry, &ms );
    574  1.1.1.1.2.2  tls 			if ( !base->e_id )
    575  1.1.1.1.2.2  tls 				nsubs = ms.ms_entries;
    576  1.1.1.1.2.2  tls 			if ( ncand == NOID )
    577  1.1.1.1.2.2  tls 				ncand = ms.ms_entries;
    578  1.1.1.1.2.2  tls 		}
    579  1.1.1.1.2.2  tls 	}
    580  1.1.1.1.2.2  tls 
    581  1.1.1.1.2.2  tls 	/* start cursor at beginning of candidates.
    582  1.1.1.1.2.2  tls 	 */
    583  1.1.1.1.2.2  tls 	cursor = 0;
    584  1.1.1.1.2.2  tls 
    585  1.1.1.1.2.2  tls 	if ( candidates[0] == 0 ) {
    586  1.1.1.1.2.2  tls 		Debug( LDAP_DEBUG_TRACE,
    587  1.1.1.1.2.2  tls 			LDAP_XSTRING(mdb_search) ": no candidates\n",
    588  1.1.1.1.2.2  tls 			0, 0, 0 );
    589  1.1.1.1.2.2  tls 
    590  1.1.1.1.2.2  tls 		goto nochange;
    591  1.1.1.1.2.2  tls 	}
    592  1.1.1.1.2.2  tls 
    593  1.1.1.1.2.2  tls 	/* if not root and candidates exceed to-be-checked entries, abort */
    594  1.1.1.1.2.2  tls 	if ( op->ors_limit	/* isroot == FALSE */ &&
    595  1.1.1.1.2.2  tls 		op->ors_limit->lms_s_unchecked != -1 &&
    596  1.1.1.1.2.2  tls 		ncand > (unsigned) op->ors_limit->lms_s_unchecked )
    597  1.1.1.1.2.2  tls 	{
    598  1.1.1.1.2.2  tls 		rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
    599  1.1.1.1.2.2  tls 		send_ldap_result( op, rs );
    600  1.1.1.1.2.2  tls 		rs->sr_err = LDAP_SUCCESS;
    601  1.1.1.1.2.2  tls 		goto done;
    602  1.1.1.1.2.2  tls 	}
    603  1.1.1.1.2.2  tls 
    604  1.1.1.1.2.2  tls 	if ( op->ors_limit == NULL	/* isroot == TRUE */ ||
    605  1.1.1.1.2.2  tls 		!op->ors_limit->lms_s_pr_hide )
    606  1.1.1.1.2.2  tls 	{
    607  1.1.1.1.2.2  tls 		tentries = ncand;
    608  1.1.1.1.2.2  tls 	}
    609  1.1.1.1.2.2  tls 
    610  1.1.1.1.2.2  tls 	if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
    611  1.1.1.1.2.2  tls 		PagedResultsState *ps = op->o_pagedresults_state;
    612  1.1.1.1.2.2  tls 		/* deferred cookie parsing */
    613  1.1.1.1.2.2  tls 		rs->sr_err = parse_paged_cookie( op, rs );
    614  1.1.1.1.2.2  tls 		if ( rs->sr_err != LDAP_SUCCESS ) {
    615  1.1.1.1.2.2  tls 			send_ldap_result( op, rs );
    616  1.1.1.1.2.2  tls 			goto done;
    617  1.1.1.1.2.2  tls 		}
    618  1.1.1.1.2.2  tls 
    619  1.1.1.1.2.2  tls 		cursor = (ID) ps->ps_cookie;
    620  1.1.1.1.2.2  tls 		if ( cursor && ps->ps_size == 0 ) {
    621  1.1.1.1.2.2  tls 			rs->sr_err = LDAP_SUCCESS;
    622  1.1.1.1.2.2  tls 			rs->sr_text = "search abandoned by pagedResult size=0";
    623  1.1.1.1.2.2  tls 			send_ldap_result( op, rs );
    624  1.1.1.1.2.2  tls 			goto done;
    625  1.1.1.1.2.2  tls 		}
    626  1.1.1.1.2.2  tls 		id = mdb_idl_first( candidates, &cursor );
    627  1.1.1.1.2.2  tls 		if ( id == NOID ) {
    628  1.1.1.1.2.2  tls 			Debug( LDAP_DEBUG_TRACE,
    629  1.1.1.1.2.2  tls 				LDAP_XSTRING(mdb_search)
    630  1.1.1.1.2.2  tls 				": no paged results candidates\n",
    631  1.1.1.1.2.2  tls 				0, 0, 0 );
    632  1.1.1.1.2.2  tls 			send_paged_response( op, rs, &lastid, 0 );
    633  1.1.1.1.2.2  tls 
    634  1.1.1.1.2.2  tls 			rs->sr_err = LDAP_OTHER;
    635  1.1.1.1.2.2  tls 			goto done;
    636  1.1.1.1.2.2  tls 		}
    637  1.1.1.1.2.2  tls 		if ( id == (ID)ps->ps_cookie )
    638  1.1.1.1.2.2  tls 			id = mdb_idl_next( candidates, &cursor );
    639  1.1.1.1.2.2  tls 		nsubs = ncand;	/* always bypass scope'd search */
    640  1.1.1.1.2.2  tls 		goto loop_begin;
    641  1.1.1.1.2.2  tls 	}
    642  1.1.1.1.2.2  tls 	if ( nsubs < ncand ) {
    643  1.1.1.1.2.2  tls 		int rc;
    644  1.1.1.1.2.2  tls 		/* Do scope-based search */
    645  1.1.1.1.2.2  tls 
    646  1.1.1.1.2.2  tls 		/* if any alias scopes were set, save them */
    647  1.1.1.1.2.2  tls 		if (scopes[0].mid > 1) {
    648  1.1.1.1.2.2  tls 			cursor = 1;
    649  1.1.1.1.2.2  tls 			for (cscope = 1; cscope <= scopes[0].mid; cscope++) {
    650  1.1.1.1.2.2  tls 				/* Ignore the original base */
    651  1.1.1.1.2.2  tls 				if (scopes[cscope].mid == base->e_id)
    652  1.1.1.1.2.2  tls 					continue;
    653  1.1.1.1.2.2  tls 				iscopes[cursor++] = scopes[cscope].mid;
    654  1.1.1.1.2.2  tls 			}
    655  1.1.1.1.2.2  tls 			iscopes[0] = scopes[0].mid - 1;
    656  1.1.1.1.2.2  tls 		} else {
    657  1.1.1.1.2.2  tls 			iscopes[0] = 0;
    658  1.1.1.1.2.2  tls 		}
    659  1.1.1.1.2.2  tls 
    660  1.1.1.1.2.2  tls 		isc.id = base->e_id;
    661  1.1.1.1.2.2  tls 		isc.numrdns = 0;
    662  1.1.1.1.2.2  tls 		rc = mdb_dn2id_walk( op, &isc );
    663  1.1.1.1.2.2  tls 		if ( rc )
    664  1.1.1.1.2.2  tls 			id = NOID;
    665  1.1.1.1.2.2  tls 		else
    666  1.1.1.1.2.2  tls 			id = isc.id;
    667  1.1.1.1.2.2  tls 		cscope = 0;
    668  1.1.1.1.2.2  tls 	} else {
    669  1.1.1.1.2.2  tls 		id = mdb_idl_first( candidates, &cursor );
    670  1.1.1.1.2.2  tls 	}
    671  1.1.1.1.2.2  tls 
    672  1.1.1.1.2.2  tls 	while (id != NOID)
    673  1.1.1.1.2.2  tls 	{
    674  1.1.1.1.2.2  tls 		int scopeok;
    675  1.1.1.1.2.2  tls 		MDB_val edata;
    676  1.1.1.1.2.2  tls 
    677  1.1.1.1.2.2  tls loop_begin:
    678  1.1.1.1.2.2  tls 
    679  1.1.1.1.2.2  tls 		/* check for abandon */
    680  1.1.1.1.2.2  tls 		if ( op->o_abandon ) {
    681  1.1.1.1.2.2  tls 			rs->sr_err = SLAPD_ABANDON;
    682  1.1.1.1.2.2  tls 			send_ldap_result( op, rs );
    683  1.1.1.1.2.2  tls 			goto done;
    684  1.1.1.1.2.2  tls 		}
    685  1.1.1.1.2.2  tls 
    686  1.1.1.1.2.2  tls 		/* mostly needed by internal searches,
    687  1.1.1.1.2.2  tls 		 * e.g. related to syncrepl, for whom
    688  1.1.1.1.2.2  tls 		 * abandon does not get set... */
    689  1.1.1.1.2.2  tls 		if ( slapd_shutdown ) {
    690  1.1.1.1.2.2  tls 			rs->sr_err = LDAP_UNAVAILABLE;
    691  1.1.1.1.2.2  tls 			send_ldap_disconnect( op, rs );
    692  1.1.1.1.2.2  tls 			goto done;
    693  1.1.1.1.2.2  tls 		}
    694  1.1.1.1.2.2  tls 
    695  1.1.1.1.2.2  tls 		/* check time limit */
    696  1.1.1.1.2.2  tls 		if ( op->ors_tlimit != SLAP_NO_LIMIT
    697  1.1.1.1.2.2  tls 				&& slap_get_time() > stoptime )
    698  1.1.1.1.2.2  tls 		{
    699  1.1.1.1.2.2  tls 			rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
    700  1.1.1.1.2.2  tls 			rs->sr_ref = rs->sr_v2ref;
    701  1.1.1.1.2.2  tls 			send_ldap_result( op, rs );
    702  1.1.1.1.2.2  tls 			rs->sr_err = LDAP_SUCCESS;
    703  1.1.1.1.2.2  tls 			goto done;
    704  1.1.1.1.2.2  tls 		}
    705  1.1.1.1.2.2  tls 
    706  1.1.1.1.2.2  tls 
    707  1.1.1.1.2.2  tls 		if ( nsubs < ncand ) {
    708  1.1.1.1.2.2  tls 			unsigned i;
    709  1.1.1.1.2.2  tls 			/* Is this entry in the candidate list? */
    710  1.1.1.1.2.2  tls 			scopeok = 0;
    711  1.1.1.1.2.2  tls 			if (MDB_IDL_IS_RANGE( candidates )) {
    712  1.1.1.1.2.2  tls 				if ( id >= MDB_IDL_RANGE_FIRST( candidates ) &&
    713  1.1.1.1.2.2  tls 					id <= MDB_IDL_RANGE_LAST( candidates ))
    714  1.1.1.1.2.2  tls 					scopeok = 1;
    715  1.1.1.1.2.2  tls 			} else {
    716  1.1.1.1.2.2  tls 				i = mdb_idl_search( candidates, id );
    717  1.1.1.1.2.2  tls 				if ( candidates[i] == id )
    718  1.1.1.1.2.2  tls 					scopeok = 1;
    719  1.1.1.1.2.2  tls 			}
    720  1.1.1.1.2.2  tls 			if ( scopeok )
    721  1.1.1.1.2.2  tls 				goto scopeok;
    722  1.1.1.1.2.2  tls 			goto loop_continue;
    723  1.1.1.1.2.2  tls 		}
    724  1.1.1.1.2.2  tls 
    725  1.1.1.1.2.2  tls 		/* Does this candidate actually satisfy the search scope?
    726  1.1.1.1.2.2  tls 		 */
    727  1.1.1.1.2.2  tls 		scopeok = 0;
    728  1.1.1.1.2.2  tls 		isc.numrdns = 0;
    729  1.1.1.1.2.2  tls 		switch( op->ors_scope ) {
    730  1.1.1.1.2.2  tls 		case LDAP_SCOPE_BASE:
    731  1.1.1.1.2.2  tls 			/* This is always true, yes? */
    732  1.1.1.1.2.2  tls 			if ( id == base->e_id ) scopeok = 1;
    733  1.1.1.1.2.2  tls 			break;
    734  1.1.1.1.2.2  tls 
    735  1.1.1.1.2.2  tls #ifdef LDAP_SCOPE_CHILDREN
    736  1.1.1.1.2.2  tls 		case LDAP_SCOPE_CHILDREN:
    737  1.1.1.1.2.2  tls 			if ( id == base->e_id ) break;
    738  1.1.1.1.2.2  tls 			/* Fall-thru */
    739  1.1.1.1.2.2  tls #endif
    740  1.1.1.1.2.2  tls 		case LDAP_SCOPE_SUBTREE:
    741  1.1.1.1.2.2  tls 			if ( id == base->e_id ) {
    742  1.1.1.1.2.2  tls 				scopeok = 1;
    743  1.1.1.1.2.2  tls 				break;
    744  1.1.1.1.2.2  tls 			}
    745  1.1.1.1.2.2  tls 			/* Fall-thru */
    746  1.1.1.1.2.2  tls 		case LDAP_SCOPE_ONELEVEL:
    747  1.1.1.1.2.2  tls 			isc.id = id;
    748  1.1.1.1.2.2  tls 			isc.nscope = 0;
    749  1.1.1.1.2.2  tls 			rs->sr_err = mdb_idscopes( op, &isc );
    750  1.1.1.1.2.2  tls 			if ( rs->sr_err == MDB_SUCCESS ) {
    751  1.1.1.1.2.2  tls 				if ( isc.nscope )
    752  1.1.1.1.2.2  tls 					scopeok = 1;
    753  1.1.1.1.2.2  tls 			} else {
    754  1.1.1.1.2.2  tls 				if ( rs->sr_err == MDB_NOTFOUND )
    755  1.1.1.1.2.2  tls 					goto notfound;
    756  1.1.1.1.2.2  tls 			}
    757  1.1.1.1.2.2  tls 			break;
    758  1.1.1.1.2.2  tls 		}
    759  1.1.1.1.2.2  tls 
    760  1.1.1.1.2.2  tls 		/* Not in scope, ignore it */
    761  1.1.1.1.2.2  tls 		if ( !scopeok )
    762  1.1.1.1.2.2  tls 		{
    763  1.1.1.1.2.2  tls 			Debug( LDAP_DEBUG_TRACE,
    764  1.1.1.1.2.2  tls 				LDAP_XSTRING(mdb_search)
    765  1.1.1.1.2.2  tls 				": %ld scope not okay\n",
    766  1.1.1.1.2.2  tls 				(long) id, 0, 0 );
    767  1.1.1.1.2.2  tls 			goto loop_continue;
    768  1.1.1.1.2.2  tls 		}
    769  1.1.1.1.2.2  tls 
    770  1.1.1.1.2.2  tls scopeok:
    771  1.1.1.1.2.2  tls 		if ( id == base->e_id ) {
    772  1.1.1.1.2.2  tls 			e = base;
    773  1.1.1.1.2.2  tls 		} else {
    774  1.1.1.1.2.2  tls 
    775  1.1.1.1.2.2  tls 			/* get the entry */
    776  1.1.1.1.2.2  tls 			rs->sr_err = mdb_id2edata( op, mci, id, &edata );
    777  1.1.1.1.2.2  tls 			if ( rs->sr_err == MDB_NOTFOUND ) {
    778  1.1.1.1.2.2  tls notfound:
    779  1.1.1.1.2.2  tls 				if( nsubs < ncand )
    780  1.1.1.1.2.2  tls 					goto loop_continue;
    781  1.1.1.1.2.2  tls 
    782  1.1.1.1.2.2  tls 				if( !MDB_IDL_IS_RANGE(candidates) ) {
    783  1.1.1.1.2.2  tls 					/* only complain for non-range IDLs */
    784  1.1.1.1.2.2  tls 					Debug( LDAP_DEBUG_TRACE,
    785  1.1.1.1.2.2  tls 						LDAP_XSTRING(mdb_search)
    786  1.1.1.1.2.2  tls 						": candidate %ld not found\n",
    787  1.1.1.1.2.2  tls 						(long) id, 0, 0 );
    788  1.1.1.1.2.2  tls 				} else {
    789  1.1.1.1.2.2  tls 					/* get the next ID from the DB */
    790  1.1.1.1.2.2  tls 					rs->sr_err = mdb_get_nextid( mci, &cursor );
    791  1.1.1.1.2.2  tls 					if ( rs->sr_err == MDB_NOTFOUND ) {
    792  1.1.1.1.2.2  tls 						break;
    793  1.1.1.1.2.2  tls 					}
    794  1.1.1.1.2.2  tls 					if ( rs->sr_err ) {
    795  1.1.1.1.2.2  tls 						rs->sr_err = LDAP_OTHER;
    796  1.1.1.1.2.2  tls 						rs->sr_text = "internal error in get_nextid";
    797  1.1.1.1.2.2  tls 						send_ldap_result( op, rs );
    798  1.1.1.1.2.2  tls 						goto done;
    799  1.1.1.1.2.2  tls 					}
    800  1.1.1.1.2.2  tls 					cursor--;
    801  1.1.1.1.2.2  tls 				}
    802  1.1.1.1.2.2  tls 
    803  1.1.1.1.2.2  tls 				goto loop_continue;
    804  1.1.1.1.2.2  tls 			} else if ( rs->sr_err ) {
    805  1.1.1.1.2.2  tls 				rs->sr_err = LDAP_OTHER;
    806  1.1.1.1.2.2  tls 				rs->sr_text = "internal error in mdb_id2edata";
    807  1.1.1.1.2.2  tls 				send_ldap_result( op, rs );
    808  1.1.1.1.2.2  tls 				goto done;
    809  1.1.1.1.2.2  tls 			}
    810  1.1.1.1.2.2  tls 
    811  1.1.1.1.2.2  tls 			rs->sr_err = mdb_entry_decode( op, &edata, &e );
    812  1.1.1.1.2.2  tls 			if ( rs->sr_err ) {
    813  1.1.1.1.2.2  tls 				rs->sr_err = LDAP_OTHER;
    814  1.1.1.1.2.2  tls 				rs->sr_text = "internal error in mdb_entry_decode";
    815  1.1.1.1.2.2  tls 				send_ldap_result( op, rs );
    816  1.1.1.1.2.2  tls 				goto done;
    817  1.1.1.1.2.2  tls 			}
    818  1.1.1.1.2.2  tls 			e->e_id = id;
    819  1.1.1.1.2.2  tls 			e->e_name.bv_val = NULL;
    820  1.1.1.1.2.2  tls 			e->e_nname.bv_val = NULL;
    821  1.1.1.1.2.2  tls 		}
    822  1.1.1.1.2.2  tls 
    823  1.1.1.1.2.2  tls 		if ( is_entry_subentry( e ) ) {
    824  1.1.1.1.2.2  tls 			if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
    825  1.1.1.1.2.2  tls 				if(!get_subentries_visibility( op )) {
    826  1.1.1.1.2.2  tls 					/* only subentries are visible */
    827  1.1.1.1.2.2  tls 					goto loop_continue;
    828  1.1.1.1.2.2  tls 				}
    829  1.1.1.1.2.2  tls 
    830  1.1.1.1.2.2  tls 			} else if ( get_subentries( op ) &&
    831  1.1.1.1.2.2  tls 				!get_subentries_visibility( op ))
    832  1.1.1.1.2.2  tls 			{
    833  1.1.1.1.2.2  tls 				/* only subentries are visible */
    834  1.1.1.1.2.2  tls 				goto loop_continue;
    835  1.1.1.1.2.2  tls 			}
    836  1.1.1.1.2.2  tls 
    837  1.1.1.1.2.2  tls 		} else if ( get_subentries_visibility( op )) {
    838  1.1.1.1.2.2  tls 			/* only subentries are visible */
    839  1.1.1.1.2.2  tls 			goto loop_continue;
    840  1.1.1.1.2.2  tls 		}
    841  1.1.1.1.2.2  tls 
    842  1.1.1.1.2.2  tls 		/* aliases were already dereferenced in candidate list */
    843  1.1.1.1.2.2  tls 		if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
    844  1.1.1.1.2.2  tls 			/* but if the search base is an alias, and we didn't
    845  1.1.1.1.2.2  tls 			 * deref it when finding, return it.
    846  1.1.1.1.2.2  tls 			 */
    847  1.1.1.1.2.2  tls 			if ( is_entry_alias(e) &&
    848  1.1.1.1.2.2  tls 				((op->ors_deref & LDAP_DEREF_FINDING) || e != base ))
    849  1.1.1.1.2.2  tls 			{
    850  1.1.1.1.2.2  tls 				goto loop_continue;
    851  1.1.1.1.2.2  tls 			}
    852  1.1.1.1.2.2  tls 		}
    853  1.1.1.1.2.2  tls 
    854  1.1.1.1.2.2  tls 		if ( !manageDSAit && is_entry_glue( e )) {
    855  1.1.1.1.2.2  tls 			goto loop_continue;
    856  1.1.1.1.2.2  tls 		}
    857  1.1.1.1.2.2  tls 
    858  1.1.1.1.2.2  tls 		if (e != base) {
    859  1.1.1.1.2.2  tls 			struct berval pdn, pndn;
    860  1.1.1.1.2.2  tls 			char *d, *n;
    861  1.1.1.1.2.2  tls 			int i;
    862  1.1.1.1.2.2  tls 
    863  1.1.1.1.2.2  tls 			/* child of base, just append RDNs to base->e_name */
    864  1.1.1.1.2.2  tls 			if ( nsubs < ncand || isc.scopes[isc.nscope].mid == base->e_id ) {
    865  1.1.1.1.2.2  tls 				pdn = base->e_name;
    866  1.1.1.1.2.2  tls 				pndn = base->e_nname;
    867  1.1.1.1.2.2  tls 			} else {
    868  1.1.1.1.2.2  tls 				mdb_id2name( op, ltid, &isc.mc, scopes[isc.nscope].mid, &pdn, &pndn );
    869  1.1.1.1.2.2  tls 			}
    870  1.1.1.1.2.2  tls 			e->e_name.bv_len = pdn.bv_len;
    871  1.1.1.1.2.2  tls 			e->e_nname.bv_len = pndn.bv_len;
    872  1.1.1.1.2.2  tls 			for (i=0; i<isc.numrdns; i++) {
    873  1.1.1.1.2.2  tls 				e->e_name.bv_len += isc.rdns[i].bv_len + 1;
    874  1.1.1.1.2.2  tls 				e->e_nname.bv_len += isc.nrdns[i].bv_len + 1;
    875  1.1.1.1.2.2  tls 			}
    876  1.1.1.1.2.2  tls 			e->e_name.bv_val = op->o_tmpalloc(e->e_name.bv_len + 1, op->o_tmpmemctx);
    877  1.1.1.1.2.2  tls 			e->e_nname.bv_val = op->o_tmpalloc(e->e_nname.bv_len + 1, op->o_tmpmemctx);
    878  1.1.1.1.2.2  tls 			d = e->e_name.bv_val;
    879  1.1.1.1.2.2  tls 			n = e->e_nname.bv_val;
    880  1.1.1.1.2.2  tls 			if (nsubs < ncand) {
    881  1.1.1.1.2.2  tls 				/* RDNs are in top-down order */
    882  1.1.1.1.2.2  tls 				for (i=isc.numrdns-1; i>=0; i--) {
    883  1.1.1.1.2.2  tls 					memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
    884  1.1.1.1.2.2  tls 					d += isc.rdns[i].bv_len;
    885  1.1.1.1.2.2  tls 					*d++ = ',';
    886  1.1.1.1.2.2  tls 					memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
    887  1.1.1.1.2.2  tls 					n += isc.nrdns[i].bv_len;
    888  1.1.1.1.2.2  tls 					*n++ = ',';
    889  1.1.1.1.2.2  tls 				}
    890  1.1.1.1.2.2  tls 			} else {
    891  1.1.1.1.2.2  tls 				/* RDNs are in bottom-up order */
    892  1.1.1.1.2.2  tls 				for (i=0; i<isc.numrdns; i++) {
    893  1.1.1.1.2.2  tls 					memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
    894  1.1.1.1.2.2  tls 					d += isc.rdns[i].bv_len;
    895  1.1.1.1.2.2  tls 					*d++ = ',';
    896  1.1.1.1.2.2  tls 					memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
    897  1.1.1.1.2.2  tls 					n += isc.nrdns[i].bv_len;
    898  1.1.1.1.2.2  tls 					*n++ = ',';
    899  1.1.1.1.2.2  tls 				}
    900  1.1.1.1.2.2  tls 			}
    901  1.1.1.1.2.2  tls 
    902  1.1.1.1.2.2  tls 			if (pdn.bv_len) {
    903  1.1.1.1.2.2  tls 				memcpy(d, pdn.bv_val, pdn.bv_len+1);
    904  1.1.1.1.2.2  tls 				memcpy(n, pndn.bv_val, pndn.bv_len+1);
    905  1.1.1.1.2.2  tls 			} else {
    906  1.1.1.1.2.2  tls 				*--d = '\0';
    907  1.1.1.1.2.2  tls 				*--n = '\0';
    908  1.1.1.1.2.2  tls 				e->e_name.bv_len--;
    909  1.1.1.1.2.2  tls 				e->e_nname.bv_len--;
    910  1.1.1.1.2.2  tls 			}
    911  1.1.1.1.2.2  tls 			if (pndn.bv_val != base->e_nname.bv_val) {
    912  1.1.1.1.2.2  tls 				op->o_tmpfree(pndn.bv_val, op->o_tmpmemctx);
    913  1.1.1.1.2.2  tls 				op->o_tmpfree(pdn.bv_val, op->o_tmpmemctx);
    914  1.1.1.1.2.2  tls 			}
    915  1.1.1.1.2.2  tls 		}
    916  1.1.1.1.2.2  tls 
    917  1.1.1.1.2.2  tls 		/*
    918  1.1.1.1.2.2  tls 		 * if it's a referral, add it to the list of referrals. only do
    919  1.1.1.1.2.2  tls 		 * this for non-base searches, and don't check the filter
    920  1.1.1.1.2.2  tls 		 * explicitly here since it's only a candidate anyway.
    921  1.1.1.1.2.2  tls 		 */
    922  1.1.1.1.2.2  tls 		if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
    923  1.1.1.1.2.2  tls 			&& is_entry_referral( e ) )
    924  1.1.1.1.2.2  tls 		{
    925  1.1.1.1.2.2  tls 			BerVarray erefs = get_entry_referrals( op, e );
    926  1.1.1.1.2.2  tls 			rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
    927  1.1.1.1.2.2  tls 				op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
    928  1.1.1.1.2.2  tls 					? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
    929  1.1.1.1.2.2  tls 
    930  1.1.1.1.2.2  tls 			rs->sr_entry = e;
    931  1.1.1.1.2.2  tls 			rs->sr_flags = 0;
    932  1.1.1.1.2.2  tls 
    933  1.1.1.1.2.2  tls 			send_search_reference( op, rs );
    934  1.1.1.1.2.2  tls 
    935  1.1.1.1.2.2  tls 			mdb_entry_return( op, e );
    936  1.1.1.1.2.2  tls 			rs->sr_entry = NULL;
    937  1.1.1.1.2.2  tls 			e = NULL;
    938  1.1.1.1.2.2  tls 
    939  1.1.1.1.2.2  tls 			ber_bvarray_free( rs->sr_ref );
    940  1.1.1.1.2.2  tls 			ber_bvarray_free( erefs );
    941  1.1.1.1.2.2  tls 			rs->sr_ref = NULL;
    942  1.1.1.1.2.2  tls 
    943  1.1.1.1.2.2  tls 			goto loop_continue;
    944  1.1.1.1.2.2  tls 		}
    945  1.1.1.1.2.2  tls 
    946  1.1.1.1.2.2  tls 		/* if it matches the filter and scope, send it */
    947  1.1.1.1.2.2  tls 		rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
    948  1.1.1.1.2.2  tls 
    949  1.1.1.1.2.2  tls 		if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
    950  1.1.1.1.2.2  tls 			/* check size limit */
    951  1.1.1.1.2.2  tls 			if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
    952  1.1.1.1.2.2  tls 				if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
    953  1.1.1.1.2.2  tls 					mdb_entry_return( op, e );
    954  1.1.1.1.2.2  tls 					e = NULL;
    955  1.1.1.1.2.2  tls 					send_paged_response( op, rs, &lastid, tentries );
    956  1.1.1.1.2.2  tls 					goto done;
    957  1.1.1.1.2.2  tls 				}
    958  1.1.1.1.2.2  tls 				lastid = id;
    959  1.1.1.1.2.2  tls 			}
    960  1.1.1.1.2.2  tls 
    961  1.1.1.1.2.2  tls 			if (e) {
    962  1.1.1.1.2.2  tls 				/* safe default */
    963  1.1.1.1.2.2  tls 				rs->sr_attrs = op->oq_search.rs_attrs;
    964  1.1.1.1.2.2  tls 				rs->sr_operational_attrs = NULL;
    965  1.1.1.1.2.2  tls 				rs->sr_ctrls = NULL;
    966  1.1.1.1.2.2  tls 				rs->sr_entry = e;
    967  1.1.1.1.2.2  tls 				RS_ASSERT( e->e_private != NULL );
    968  1.1.1.1.2.2  tls 				rs->sr_flags = 0;
    969  1.1.1.1.2.2  tls 				rs->sr_err = LDAP_SUCCESS;
    970  1.1.1.1.2.2  tls 				rs->sr_err = send_search_entry( op, rs );
    971  1.1.1.1.2.2  tls 				rs->sr_attrs = NULL;
    972  1.1.1.1.2.2  tls 				rs->sr_entry = NULL;
    973  1.1.1.1.2.2  tls 				if (e != base)
    974  1.1.1.1.2.2  tls 					mdb_entry_return( op, e );
    975  1.1.1.1.2.2  tls 				e = NULL;
    976  1.1.1.1.2.2  tls 
    977  1.1.1.1.2.2  tls 				switch ( rs->sr_err ) {
    978  1.1.1.1.2.2  tls 				case LDAP_SUCCESS:	/* entry sent ok */
    979  1.1.1.1.2.2  tls 					break;
    980  1.1.1.1.2.2  tls 				default:		/* entry not sent */
    981  1.1.1.1.2.2  tls 					break;
    982  1.1.1.1.2.2  tls 				case LDAP_BUSY:
    983  1.1.1.1.2.2  tls 					send_ldap_result( op, rs );
    984  1.1.1.1.2.2  tls 					goto done;
    985  1.1.1.1.2.2  tls 				case LDAP_UNAVAILABLE:
    986  1.1.1.1.2.2  tls 				case LDAP_SIZELIMIT_EXCEEDED:
    987  1.1.1.1.2.2  tls 					if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
    988  1.1.1.1.2.2  tls 						rs->sr_ref = rs->sr_v2ref;
    989  1.1.1.1.2.2  tls 						send_ldap_result( op, rs );
    990  1.1.1.1.2.2  tls 						rs->sr_err = LDAP_SUCCESS;
    991  1.1.1.1.2.2  tls 
    992  1.1.1.1.2.2  tls 					} else {
    993  1.1.1.1.2.2  tls 						rs->sr_err = LDAP_OTHER;
    994  1.1.1.1.2.2  tls 					}
    995  1.1.1.1.2.2  tls 					goto done;
    996  1.1.1.1.2.2  tls 				}
    997  1.1.1.1.2.2  tls 			}
    998  1.1.1.1.2.2  tls 
    999  1.1.1.1.2.2  tls 		} else {
   1000  1.1.1.1.2.2  tls 			Debug( LDAP_DEBUG_TRACE,
   1001  1.1.1.1.2.2  tls 				LDAP_XSTRING(mdb_search)
   1002  1.1.1.1.2.2  tls 				": %ld does not match filter\n",
   1003  1.1.1.1.2.2  tls 				(long) id, 0, 0 );
   1004  1.1.1.1.2.2  tls 		}
   1005  1.1.1.1.2.2  tls 
   1006  1.1.1.1.2.2  tls loop_continue:
   1007  1.1.1.1.2.2  tls 		if( e != NULL ) {
   1008  1.1.1.1.2.2  tls 			if ( e != base )
   1009  1.1.1.1.2.2  tls 				mdb_entry_return( op, e );
   1010  1.1.1.1.2.2  tls 			RS_ASSERT( rs->sr_entry == NULL );
   1011  1.1.1.1.2.2  tls 			e = NULL;
   1012  1.1.1.1.2.2  tls 			rs->sr_entry = NULL;
   1013  1.1.1.1.2.2  tls 		}
   1014  1.1.1.1.2.2  tls 
   1015  1.1.1.1.2.2  tls 		if ( nsubs < ncand ) {
   1016  1.1.1.1.2.2  tls 			int rc = mdb_dn2id_walk( op, &isc );
   1017  1.1.1.1.2.2  tls 			if (rc) {
   1018  1.1.1.1.2.2  tls 				id = NOID;
   1019  1.1.1.1.2.2  tls 				/* We got to the end of a subtree. If there are any
   1020  1.1.1.1.2.2  tls 				 * alias scopes left, search them too.
   1021  1.1.1.1.2.2  tls 				 */
   1022  1.1.1.1.2.2  tls 				while (iscopes[0] && cscope < iscopes[0]) {
   1023  1.1.1.1.2.2  tls 					cscope++;
   1024  1.1.1.1.2.2  tls 					isc.id = iscopes[cscope];
   1025  1.1.1.1.2.2  tls 					if ( base )
   1026  1.1.1.1.2.2  tls 						mdb_entry_return( op, base );
   1027  1.1.1.1.2.2  tls 					rs->sr_err = mdb_id2entry(op, mci, isc.id, &base);
   1028  1.1.1.1.2.2  tls 					if ( !rs->sr_err ) {
   1029  1.1.1.1.2.2  tls 						mdb_id2name( op, ltid, &isc.mc, isc.id, &base->e_name, &base->e_nname );
   1030  1.1.1.1.2.2  tls 						isc.numrdns = 0;
   1031  1.1.1.1.2.2  tls 						if (isc.oscope == LDAP_SCOPE_ONELEVEL)
   1032  1.1.1.1.2.2  tls 							isc.oscope = LDAP_SCOPE_BASE;
   1033  1.1.1.1.2.2  tls 						rc = mdb_dn2id_walk( op, &isc );
   1034  1.1.1.1.2.2  tls 						if ( !rc ) {
   1035  1.1.1.1.2.2  tls 							id = isc.id;
   1036  1.1.1.1.2.2  tls 							break;
   1037  1.1.1.1.2.2  tls 						}
   1038  1.1.1.1.2.2  tls 					}
   1039  1.1.1.1.2.2  tls 				}
   1040  1.1.1.1.2.2  tls 			} else
   1041  1.1.1.1.2.2  tls 				id = isc.id;
   1042  1.1.1.1.2.2  tls 		} else {
   1043  1.1.1.1.2.2  tls 			id = mdb_idl_next( candidates, &cursor );
   1044  1.1.1.1.2.2  tls 		}
   1045  1.1.1.1.2.2  tls 	}
   1046  1.1.1.1.2.2  tls 
   1047  1.1.1.1.2.2  tls nochange:
   1048  1.1.1.1.2.2  tls 	rs->sr_ctrls = NULL;
   1049  1.1.1.1.2.2  tls 	rs->sr_ref = rs->sr_v2ref;
   1050  1.1.1.1.2.2  tls 	rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
   1051  1.1.1.1.2.2  tls 	rs->sr_rspoid = NULL;
   1052  1.1.1.1.2.2  tls 	if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
   1053  1.1.1.1.2.2  tls 		send_paged_response( op, rs, NULL, 0 );
   1054  1.1.1.1.2.2  tls 	} else {
   1055  1.1.1.1.2.2  tls 		send_ldap_result( op, rs );
   1056  1.1.1.1.2.2  tls 	}
   1057  1.1.1.1.2.2  tls 
   1058  1.1.1.1.2.2  tls 	rs->sr_err = LDAP_SUCCESS;
   1059  1.1.1.1.2.2  tls 
   1060  1.1.1.1.2.2  tls done:
   1061  1.1.1.1.2.2  tls 	mdb_cursor_close( mcd );
   1062  1.1.1.1.2.2  tls 	mdb_cursor_close( mci );
   1063  1.1.1.1.2.2  tls 	if ( moi == &opinfo ) {
   1064  1.1.1.1.2.2  tls 		mdb_txn_reset( moi->moi_txn );
   1065  1.1.1.1.2.2  tls 		LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
   1066  1.1.1.1.2.2  tls 	} else {
   1067  1.1.1.1.2.2  tls 		moi->moi_ref--;
   1068  1.1.1.1.2.2  tls 	}
   1069  1.1.1.1.2.2  tls 	if( rs->sr_v2ref ) {
   1070  1.1.1.1.2.2  tls 		ber_bvarray_free( rs->sr_v2ref );
   1071  1.1.1.1.2.2  tls 		rs->sr_v2ref = NULL;
   1072  1.1.1.1.2.2  tls 	}
   1073  1.1.1.1.2.2  tls 	if (base)
   1074  1.1.1.1.2.2  tls 		mdb_entry_return( op,base);
   1075  1.1.1.1.2.2  tls 	scope_chunk_ret( op, scopes );
   1076  1.1.1.1.2.2  tls 
   1077  1.1.1.1.2.2  tls 	return rs->sr_err;
   1078  1.1.1.1.2.2  tls }
   1079  1.1.1.1.2.2  tls 
   1080  1.1.1.1.2.2  tls 
   1081  1.1.1.1.2.2  tls static int base_candidate(
   1082  1.1.1.1.2.2  tls 	BackendDB	*be,
   1083  1.1.1.1.2.2  tls 	Entry	*e,
   1084  1.1.1.1.2.2  tls 	ID		*ids )
   1085  1.1.1.1.2.2  tls {
   1086  1.1.1.1.2.2  tls 	Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
   1087  1.1.1.1.2.2  tls 		e->e_nname.bv_val, (long) e->e_id, 0);
   1088  1.1.1.1.2.2  tls 
   1089  1.1.1.1.2.2  tls 	ids[0] = 1;
   1090  1.1.1.1.2.2  tls 	ids[1] = e->e_id;
   1091  1.1.1.1.2.2  tls 	return 0;
   1092  1.1.1.1.2.2  tls }
   1093  1.1.1.1.2.2  tls 
   1094  1.1.1.1.2.2  tls /* Look for "objectClass Present" in this filter.
   1095  1.1.1.1.2.2  tls  * Also count depth of filter tree while we're at it.
   1096  1.1.1.1.2.2  tls  */
   1097  1.1.1.1.2.2  tls static int oc_filter(
   1098  1.1.1.1.2.2  tls 	Filter *f,
   1099  1.1.1.1.2.2  tls 	int cur,
   1100  1.1.1.1.2.2  tls 	int *max )
   1101  1.1.1.1.2.2  tls {
   1102  1.1.1.1.2.2  tls 	int rc = 0;
   1103  1.1.1.1.2.2  tls 
   1104  1.1.1.1.2.2  tls 	assert( f != NULL );
   1105  1.1.1.1.2.2  tls 
   1106  1.1.1.1.2.2  tls 	if( cur > *max ) *max = cur;
   1107  1.1.1.1.2.2  tls 
   1108  1.1.1.1.2.2  tls 	switch( f->f_choice ) {
   1109  1.1.1.1.2.2  tls 	case LDAP_FILTER_PRESENT:
   1110  1.1.1.1.2.2  tls 		if (f->f_desc == slap_schema.si_ad_objectClass) {
   1111  1.1.1.1.2.2  tls 			rc = 1;
   1112  1.1.1.1.2.2  tls 		}
   1113  1.1.1.1.2.2  tls 		break;
   1114  1.1.1.1.2.2  tls 
   1115  1.1.1.1.2.2  tls 	case LDAP_FILTER_AND:
   1116  1.1.1.1.2.2  tls 	case LDAP_FILTER_OR:
   1117  1.1.1.1.2.2  tls 		cur++;
   1118  1.1.1.1.2.2  tls 		for ( f=f->f_and; f; f=f->f_next ) {
   1119  1.1.1.1.2.2  tls 			(void) oc_filter(f, cur, max);
   1120  1.1.1.1.2.2  tls 		}
   1121  1.1.1.1.2.2  tls 		break;
   1122  1.1.1.1.2.2  tls 
   1123  1.1.1.1.2.2  tls 	default:
   1124  1.1.1.1.2.2  tls 		break;
   1125  1.1.1.1.2.2  tls 	}
   1126  1.1.1.1.2.2  tls 	return rc;
   1127  1.1.1.1.2.2  tls }
   1128  1.1.1.1.2.2  tls 
   1129  1.1.1.1.2.2  tls static void search_stack_free( void *key, void *data )
   1130  1.1.1.1.2.2  tls {
   1131  1.1.1.1.2.2  tls 	ber_memfree_x(data, NULL);
   1132  1.1.1.1.2.2  tls }
   1133  1.1.1.1.2.2  tls 
   1134  1.1.1.1.2.2  tls static void *search_stack( Operation *op )
   1135  1.1.1.1.2.2  tls {
   1136  1.1.1.1.2.2  tls 	struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
   1137  1.1.1.1.2.2  tls 	void *ret = NULL;
   1138  1.1.1.1.2.2  tls 
   1139  1.1.1.1.2.2  tls 	if ( op->o_threadctx ) {
   1140  1.1.1.1.2.2  tls 		ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
   1141  1.1.1.1.2.2  tls 			&ret, NULL );
   1142  1.1.1.1.2.2  tls 	} else {
   1143  1.1.1.1.2.2  tls 		ret = mdb->mi_search_stack;
   1144  1.1.1.1.2.2  tls 	}
   1145  1.1.1.1.2.2  tls 
   1146  1.1.1.1.2.2  tls 	if ( !ret ) {
   1147  1.1.1.1.2.2  tls 		ret = ch_malloc( mdb->mi_search_stack_depth * MDB_IDL_UM_SIZE
   1148  1.1.1.1.2.2  tls 			* sizeof( ID ) );
   1149  1.1.1.1.2.2  tls 		if ( op->o_threadctx ) {
   1150  1.1.1.1.2.2  tls 			ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
   1151  1.1.1.1.2.2  tls 				ret, search_stack_free, NULL, NULL );
   1152  1.1.1.1.2.2  tls 		} else {
   1153  1.1.1.1.2.2  tls 			mdb->mi_search_stack = ret;
   1154  1.1.1.1.2.2  tls 		}
   1155  1.1.1.1.2.2  tls 	}
   1156  1.1.1.1.2.2  tls 	return ret;
   1157  1.1.1.1.2.2  tls }
   1158  1.1.1.1.2.2  tls 
   1159  1.1.1.1.2.2  tls static int search_candidates(
   1160  1.1.1.1.2.2  tls 	Operation *op,
   1161  1.1.1.1.2.2  tls 	SlapReply *rs,
   1162  1.1.1.1.2.2  tls 	Entry *e,
   1163  1.1.1.1.2.2  tls 	MDB_txn *txn,
   1164  1.1.1.1.2.2  tls 	MDB_cursor *mci,
   1165  1.1.1.1.2.2  tls 	ID	*ids,
   1166  1.1.1.1.2.2  tls 	ID2L	scopes )
   1167  1.1.1.1.2.2  tls {
   1168  1.1.1.1.2.2  tls 	struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
   1169  1.1.1.1.2.2  tls 	int rc, depth = 1;
   1170  1.1.1.1.2.2  tls 	Filter		*f, rf, xf, nf, sf;
   1171  1.1.1.1.2.2  tls 	ID		*stack;
   1172  1.1.1.1.2.2  tls 	AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
   1173  1.1.1.1.2.2  tls 	AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
   1174  1.1.1.1.2.2  tls 
   1175  1.1.1.1.2.2  tls 	/*
   1176  1.1.1.1.2.2  tls 	 * This routine takes as input a filter (user-filter)
   1177  1.1.1.1.2.2  tls 	 * and rewrites it as follows:
   1178  1.1.1.1.2.2  tls 	 *	(&(scope=DN)[(objectClass=subentry)]
   1179  1.1.1.1.2.2  tls 	 *		(|[(objectClass=referral)](user-filter))
   1180  1.1.1.1.2.2  tls 	 */
   1181  1.1.1.1.2.2  tls 
   1182  1.1.1.1.2.2  tls 	Debug(LDAP_DEBUG_TRACE,
   1183  1.1.1.1.2.2  tls 		"search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
   1184  1.1.1.1.2.2  tls 		e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
   1185  1.1.1.1.2.2  tls 
   1186  1.1.1.1.2.2  tls 	f = op->oq_search.rs_filter;
   1187  1.1.1.1.2.2  tls 
   1188  1.1.1.1.2.2  tls 	/* If the user's filter uses objectClass=*,
   1189  1.1.1.1.2.2  tls 	 * these clauses are redundant.
   1190  1.1.1.1.2.2  tls 	 */
   1191  1.1.1.1.2.2  tls 	if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
   1192  1.1.1.1.2.2  tls 		&& !get_subentries_visibility(op)) {
   1193  1.1.1.1.2.2  tls 		if( !get_manageDSAit(op) && !get_domainScope(op) ) {
   1194  1.1.1.1.2.2  tls 			/* match referral objects */
   1195  1.1.1.1.2.2  tls 			struct berval bv_ref = BER_BVC( "referral" );
   1196  1.1.1.1.2.2  tls 			rf.f_choice = LDAP_FILTER_EQUALITY;
   1197  1.1.1.1.2.2  tls 			rf.f_ava = &aa_ref;
   1198  1.1.1.1.2.2  tls 			rf.f_av_desc = slap_schema.si_ad_objectClass;
   1199  1.1.1.1.2.2  tls 			rf.f_av_value = bv_ref;
   1200  1.1.1.1.2.2  tls 			rf.f_next = f;
   1201  1.1.1.1.2.2  tls 			xf.f_or = &rf;
   1202  1.1.1.1.2.2  tls 			xf.f_choice = LDAP_FILTER_OR;
   1203  1.1.1.1.2.2  tls 			xf.f_next = NULL;
   1204  1.1.1.1.2.2  tls 			f = &xf;
   1205  1.1.1.1.2.2  tls 			depth++;
   1206  1.1.1.1.2.2  tls 		}
   1207  1.1.1.1.2.2  tls 	}
   1208  1.1.1.1.2.2  tls 
   1209  1.1.1.1.2.2  tls 	if( get_subentries_visibility( op ) ) {
   1210  1.1.1.1.2.2  tls 		struct berval bv_subentry = BER_BVC( "subentry" );
   1211  1.1.1.1.2.2  tls 		sf.f_choice = LDAP_FILTER_EQUALITY;
   1212  1.1.1.1.2.2  tls 		sf.f_ava = &aa_subentry;
   1213  1.1.1.1.2.2  tls 		sf.f_av_desc = slap_schema.si_ad_objectClass;
   1214  1.1.1.1.2.2  tls 		sf.f_av_value = bv_subentry;
   1215  1.1.1.1.2.2  tls 		sf.f_next = f;
   1216  1.1.1.1.2.2  tls 		nf.f_choice = LDAP_FILTER_AND;
   1217  1.1.1.1.2.2  tls 		nf.f_and = &sf;
   1218  1.1.1.1.2.2  tls 		nf.f_next = NULL;
   1219  1.1.1.1.2.2  tls 		f = &nf;
   1220  1.1.1.1.2.2  tls 		depth++;
   1221  1.1.1.1.2.2  tls 	}
   1222  1.1.1.1.2.2  tls 
   1223  1.1.1.1.2.2  tls 	/* Allocate IDL stack, plus 1 more for former tmp */
   1224  1.1.1.1.2.2  tls 	if ( depth+1 > mdb->mi_search_stack_depth ) {
   1225  1.1.1.1.2.2  tls 		stack = ch_malloc( (depth + 1) * MDB_IDL_UM_SIZE * sizeof( ID ) );
   1226  1.1.1.1.2.2  tls 	} else {
   1227  1.1.1.1.2.2  tls 		stack = search_stack( op );
   1228  1.1.1.1.2.2  tls 	}
   1229  1.1.1.1.2.2  tls 
   1230  1.1.1.1.2.2  tls 	if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
   1231  1.1.1.1.2.2  tls 		rc = search_aliases( op, rs, e->e_id, txn, mci, scopes, stack );
   1232  1.1.1.1.2.2  tls 	} else {
   1233  1.1.1.1.2.2  tls 		rc = LDAP_SUCCESS;
   1234  1.1.1.1.2.2  tls 	}
   1235  1.1.1.1.2.2  tls 
   1236  1.1.1.1.2.2  tls 	if ( rc == LDAP_SUCCESS ) {
   1237  1.1.1.1.2.2  tls 		rc = mdb_filter_candidates( op, txn, f, ids,
   1238  1.1.1.1.2.2  tls 			stack, stack+MDB_IDL_UM_SIZE );
   1239  1.1.1.1.2.2  tls 	}
   1240  1.1.1.1.2.2  tls 
   1241  1.1.1.1.2.2  tls 	if ( depth+1 > mdb->mi_search_stack_depth ) {
   1242  1.1.1.1.2.2  tls 		ch_free( stack );
   1243  1.1.1.1.2.2  tls 	}
   1244  1.1.1.1.2.2  tls 
   1245  1.1.1.1.2.2  tls 	if( rc ) {
   1246  1.1.1.1.2.2  tls 		Debug(LDAP_DEBUG_TRACE,
   1247  1.1.1.1.2.2  tls 			"mdb_search_candidates: failed (rc=%d)\n",
   1248  1.1.1.1.2.2  tls 			rc, NULL, NULL );
   1249  1.1.1.1.2.2  tls 
   1250  1.1.1.1.2.2  tls 	} else {
   1251  1.1.1.1.2.2  tls 		Debug(LDAP_DEBUG_TRACE,
   1252  1.1.1.1.2.2  tls 			"mdb_search_candidates: id=%ld first=%ld last=%ld\n",
   1253  1.1.1.1.2.2  tls 			(long) ids[0],
   1254  1.1.1.1.2.2  tls 			(long) MDB_IDL_FIRST(ids),
   1255  1.1.1.1.2.2  tls 			(long) MDB_IDL_LAST(ids) );
   1256  1.1.1.1.2.2  tls 	}
   1257  1.1.1.1.2.2  tls 
   1258  1.1.1.1.2.2  tls 	return rc;
   1259  1.1.1.1.2.2  tls }
   1260  1.1.1.1.2.2  tls 
   1261  1.1.1.1.2.2  tls static int
   1262  1.1.1.1.2.2  tls parse_paged_cookie( Operation *op, SlapReply *rs )
   1263  1.1.1.1.2.2  tls {
   1264  1.1.1.1.2.2  tls 	int		rc = LDAP_SUCCESS;
   1265  1.1.1.1.2.2  tls 	PagedResultsState *ps = op->o_pagedresults_state;
   1266  1.1.1.1.2.2  tls 
   1267  1.1.1.1.2.2  tls 	/* this function must be invoked only if the pagedResults
   1268  1.1.1.1.2.2  tls 	 * control has been detected, parsed and partially checked
   1269  1.1.1.1.2.2  tls 	 * by the frontend */
   1270  1.1.1.1.2.2  tls 	assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
   1271  1.1.1.1.2.2  tls 
   1272  1.1.1.1.2.2  tls 	/* cookie decoding/checks deferred to backend... */
   1273  1.1.1.1.2.2  tls 	if ( ps->ps_cookieval.bv_len ) {
   1274  1.1.1.1.2.2  tls 		PagedResultsCookie reqcookie;
   1275  1.1.1.1.2.2  tls 		if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
   1276  1.1.1.1.2.2  tls 			/* bad cookie */
   1277  1.1.1.1.2.2  tls 			rs->sr_text = "paged results cookie is invalid";
   1278  1.1.1.1.2.2  tls 			rc = LDAP_PROTOCOL_ERROR;
   1279  1.1.1.1.2.2  tls 			goto done;
   1280  1.1.1.1.2.2  tls 		}
   1281  1.1.1.1.2.2  tls 
   1282  1.1.1.1.2.2  tls 		AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
   1283  1.1.1.1.2.2  tls 
   1284  1.1.1.1.2.2  tls 		if ( reqcookie > ps->ps_cookie ) {
   1285  1.1.1.1.2.2  tls 			/* bad cookie */
   1286  1.1.1.1.2.2  tls 			rs->sr_text = "paged results cookie is invalid";
   1287  1.1.1.1.2.2  tls 			rc = LDAP_PROTOCOL_ERROR;
   1288  1.1.1.1.2.2  tls 			goto done;
   1289  1.1.1.1.2.2  tls 
   1290  1.1.1.1.2.2  tls 		} else if ( reqcookie < ps->ps_cookie ) {
   1291  1.1.1.1.2.2  tls 			rs->sr_text = "paged results cookie is invalid or old";
   1292  1.1.1.1.2.2  tls 			rc = LDAP_UNWILLING_TO_PERFORM;
   1293  1.1.1.1.2.2  tls 			goto done;
   1294  1.1.1.1.2.2  tls 		}
   1295  1.1.1.1.2.2  tls 
   1296  1.1.1.1.2.2  tls 	} else {
   1297  1.1.1.1.2.2  tls 		/* we're going to use ps_cookie */
   1298  1.1.1.1.2.2  tls 		op->o_conn->c_pagedresults_state.ps_cookie = 0;
   1299  1.1.1.1.2.2  tls 	}
   1300  1.1.1.1.2.2  tls 
   1301  1.1.1.1.2.2  tls done:;
   1302  1.1.1.1.2.2  tls 
   1303  1.1.1.1.2.2  tls 	return rc;
   1304  1.1.1.1.2.2  tls }
   1305  1.1.1.1.2.2  tls 
   1306  1.1.1.1.2.2  tls static void
   1307  1.1.1.1.2.2  tls send_paged_response(
   1308  1.1.1.1.2.2  tls 	Operation	*op,
   1309  1.1.1.1.2.2  tls 	SlapReply	*rs,
   1310  1.1.1.1.2.2  tls 	ID		*lastid,
   1311  1.1.1.1.2.2  tls 	int		tentries )
   1312  1.1.1.1.2.2  tls {
   1313  1.1.1.1.2.2  tls 	LDAPControl	*ctrls[2];
   1314  1.1.1.1.2.2  tls 	BerElementBuffer berbuf;
   1315  1.1.1.1.2.2  tls 	BerElement	*ber = (BerElement *)&berbuf;
   1316  1.1.1.1.2.2  tls 	PagedResultsCookie respcookie;
   1317  1.1.1.1.2.2  tls 	struct berval cookie;
   1318  1.1.1.1.2.2  tls 
   1319  1.1.1.1.2.2  tls 	Debug(LDAP_DEBUG_ARGS,
   1320  1.1.1.1.2.2  tls 		"send_paged_response: lastid=0x%08lx nentries=%d\n",
   1321  1.1.1.1.2.2  tls 		lastid ? *lastid : 0, rs->sr_nentries, NULL );
   1322  1.1.1.1.2.2  tls 
   1323  1.1.1.1.2.2  tls 	ctrls[1] = NULL;
   1324  1.1.1.1.2.2  tls 
   1325  1.1.1.1.2.2  tls 	ber_init2( ber, NULL, LBER_USE_DER );
   1326  1.1.1.1.2.2  tls 
   1327  1.1.1.1.2.2  tls 	if ( lastid ) {
   1328  1.1.1.1.2.2  tls 		respcookie = ( PagedResultsCookie )(*lastid);
   1329  1.1.1.1.2.2  tls 		cookie.bv_len = sizeof( respcookie );
   1330  1.1.1.1.2.2  tls 		cookie.bv_val = (char *)&respcookie;
   1331  1.1.1.1.2.2  tls 
   1332  1.1.1.1.2.2  tls 	} else {
   1333  1.1.1.1.2.2  tls 		respcookie = ( PagedResultsCookie )0;
   1334  1.1.1.1.2.2  tls 		BER_BVSTR( &cookie, "" );
   1335  1.1.1.1.2.2  tls 	}
   1336  1.1.1.1.2.2  tls 
   1337  1.1.1.1.2.2  tls 	op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
   1338  1.1.1.1.2.2  tls 	op->o_conn->c_pagedresults_state.ps_count =
   1339  1.1.1.1.2.2  tls 		((PagedResultsState *)op->o_pagedresults_state)->ps_count +
   1340  1.1.1.1.2.2  tls 		rs->sr_nentries;
   1341  1.1.1.1.2.2  tls 
   1342  1.1.1.1.2.2  tls 	/* return size of 0 -- no estimate */
   1343  1.1.1.1.2.2  tls 	ber_printf( ber, "{iO}", 0, &cookie );
   1344  1.1.1.1.2.2  tls 
   1345  1.1.1.1.2.2  tls 	ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
   1346  1.1.1.1.2.2  tls 	if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
   1347  1.1.1.1.2.2  tls 		goto done;
   1348  1.1.1.1.2.2  tls 	}
   1349  1.1.1.1.2.2  tls 
   1350  1.1.1.1.2.2  tls 	ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
   1351  1.1.1.1.2.2  tls 	ctrls[0]->ldctl_iscritical = 0;
   1352  1.1.1.1.2.2  tls 
   1353  1.1.1.1.2.2  tls 	slap_add_ctrls( op, rs, ctrls );
   1354  1.1.1.1.2.2  tls 	rs->sr_err = LDAP_SUCCESS;
   1355  1.1.1.1.2.2  tls 	send_ldap_result( op, rs );
   1356  1.1.1.1.2.2  tls 
   1357  1.1.1.1.2.2  tls done:
   1358  1.1.1.1.2.2  tls 	(void) ber_free_buf( ber );
   1359  1.1.1.1.2.2  tls }
   1360