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