1 /* search.c - search operation */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
17 static int base_candidate(
22 static int search_candidates(
23 Operation *stackop, /* op with the current threadctx/slab cache */
24 Operation *sop, /* search op */
31 static void send_pagerequest_response(
37 /* Dereference aliases for a single alias entry. Return the final
38 * dereferenced entry on success, NULL on any failure.
40 static Entry * deref_base (
50 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
55 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
56 rs->sr_text = "maximum deref depth exceeded";
58 while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
60 /* Remember the last entry we looked at, so we can
65 /* If this is part of a subtree or onelevel search,
66 * have we seen this ID before? If so, quit.
68 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
73 /* If we've seen this ID during this deref iteration,
76 if ( bdb_idl_insert( tmp, e->e_id ) ) {
77 rs->sr_err = LDAP_ALIAS_PROBLEM;
78 rs->sr_text = "circular alias";
83 /* If there was a problem getting the aliasedObjectName,
84 * get_alias_dn will have set the error status.
86 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
91 rs->sr_err = bdb_dn2entry( op, NULL, &ndn, &ei,
94 if ( ei ) e = ei->bei_e;
98 rs->sr_err = LDAP_ALIAS_PROBLEM;
99 rs->sr_text = "aliasedObject not found";
103 /* Free the previous entry, continue to work with the
104 * one we just retrieved.
106 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
110 /* We found a regular entry. Return this to the caller. The
111 * entry is still locked for Read.
113 if (!is_entry_alias(e)) {
114 rs->sr_err = LDAP_SUCCESS;
122 /* Look for and dereference all aliases within the search scope. Adds
123 * the dereferenced entries to the "ids" list. Requires "stack" to be
124 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
125 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
127 static int search_aliases(
137 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
138 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
139 ID cursora, ida, cursoro, ido, *subscop2;
142 struct berval bv_alias = { sizeof("alias")-1, "alias" };
143 AttributeAssertion aa_alias;
145 DB_LOCK locka, lockr;
148 aliases = stack; /* IDL of all aliases in the database */
149 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
150 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
151 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
152 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
153 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
154 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
156 /* A copy of subscop, because subscop gets clobbered by
157 * the bdb_idl_union/intersection routines
159 subscop2 = tmp + BDB_IDL_DB_SIZE;
161 af.f_choice = LDAP_FILTER_EQUALITY;
162 af.f_ava = &aa_alias;
163 af.f_av_desc = slap_schema.si_ad_objectClass;
164 af.f_av_value = bv_alias;
167 /* Find all aliases in database */
168 BDB_IDL_ZERO( aliases );
169 rs->sr_err = bdb_filter_candidates( op, &af, aliases,
171 if (rs->sr_err != LDAP_SUCCESS) {
175 oldsubs[1] = e->e_id;
178 BDB_IDL_ZERO( visited );
179 BDB_IDL_ZERO( newsubs );
182 ido = bdb_idl_first( oldsubs, &cursoro );
185 /* Set curscop to only the aliases in the current scope. Start with
186 * all the aliases, obtain the IDL for the current scope, and then
187 * get the intersection of these two IDLs. Add the current scope
188 * to the cumulative list of candidates.
190 BDB_IDL_CPY( curscop, aliases );
191 rs->sr_err = bdb_dn2idl( op, e, subscop,
192 subscop2+BDB_IDL_DB_SIZE );
196 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
198 BDB_IDL_CPY(subscop2, subscop);
199 rs->sr_err = bdb_idl_intersection(curscop, subscop);
200 bdb_idl_union( ids, subscop2 );
202 /* Dereference all of the aliases in the current scope. */
204 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
205 ida = bdb_idl_next(curscop, &cursora))
208 rs->sr_err = bdb_cache_find_id(op, NULL,
209 ida, &ei, 0, locker, &lockr );
210 if (rs->sr_err != LDAP_SUCCESS) {
215 /* This should only happen if the curscop IDL has maxed out and
216 * turned into a range that spans IDs indiscriminately
218 if (!is_entry_alias(a)) {
219 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
224 /* Actually dereference the alias */
226 a = deref_base( op, rs, a, &matched, locker, &lockr,
229 /* If the target was not already in our current candidates,
230 * make note of it in the newsubs list. Also
231 * set it in the scopes list so that bdb_search
234 if (bdb_idl_insert(ids, a->e_id) == 0) {
235 bdb_idl_insert(newsubs, a->e_id);
236 bdb_idl_insert(scopes, a->e_id);
238 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
241 } else if (matched) {
242 /* Alias could not be dereferenced, or it deref'd to
243 * an ID we've already seen. Ignore it.
245 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
250 /* If this is a OneLevel search, we're done; oldsubs only had one
251 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
253 if (op->ors_scope != LDAP_SCOPE_SUBTREE) break;
255 ido = bdb_idl_next( oldsubs, &cursoro );
257 /* If we're done processing the old scopes, did we add any new
258 * scopes in this iteration? If so, go back and do those now.
261 if (BDB_IDL_IS_ZERO(newsubs)) break;
262 BDB_IDL_CPY(oldsubs, newsubs);
263 BDB_IDL_ZERO(newsubs);
265 ido = bdb_idl_first( oldsubs, &cursoro );
268 /* Find the entry corresponding to the next scope. If it can't
269 * be found, ignore it and move on. This should never happen;
270 * we should never see the ID of an entry that doesn't exist.
271 * Set the name so that the scope's IDL can be retrieved.
274 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
276 if (rs->sr_err != LDAP_SUCCESS) goto nextido;
283 int is_sync_protocol( Operation *op )
285 if ( op->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST )
290 #define IS_BDB_REPLACE(type) (( type == LDAP_PSEARCH_BY_DELETE ) || \
291 ( type == LDAP_PSEARCH_BY_SCOPEOUT ))
292 #define IS_PSEARCH (op != sop)
295 bdb_drop_psearch( Operation *op, ber_int_t msgid )
298 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
300 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
301 if ( ps_list->o_connid == op->o_connid ) {
302 if ( ps_list->o_msgid == msgid ) {
303 ps_list->o_abandon = 1;
304 LDAP_LIST_REMOVE( ps_list, o_ps_link );
305 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
306 LDAP_STAILQ_REMOVE( &op->o_conn->c_ops, ps_list,
308 LDAP_STAILQ_NEXT( ps_list, o_next ) = NULL;
309 op->o_conn->c_n_ops_executing--;
310 op->o_conn->c_n_ops_completed++;
311 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
321 bdb_abandon( Operation *op, SlapReply *rs )
325 ps = bdb_drop_psearch( op, op->oq_abandon.rs_msgid );
327 if ( ps->o_tmpmemctx ) {
328 sl_mem_destroy( NULL, ps->o_tmpmemctx );
333 return LDAP_UNAVAILABLE;
337 bdb_cancel( Operation *op, SlapReply *rs )
341 ps = bdb_drop_psearch( op, op->oq_cancel.rs_msgid );
343 rs->sr_err = LDAP_CANCELLED;
344 send_ldap_result( ps, rs );
345 if ( ps->o_tmpmemctx ) {
346 sl_mem_destroy( NULL, ps->o_tmpmemctx );
351 return LDAP_UNAVAILABLE;
354 int bdb_search( Operation *op, SlapReply *rs )
356 return bdb_do_search( op, rs, op, NULL, 0 );
359 /* For persistent searches, op is the currently executing operation,
360 * sop is the persistent search. For regular searches, sop = op.
363 bdb_do_search( Operation *op, SlapReply *rs, Operation *sop,
364 Entry *ps_e, int ps_type )
366 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
369 ID candidates[BDB_IDL_UM_SIZE];
370 ID scopes[BDB_IDL_DB_SIZE];
371 Entry *e = NULL, base;
372 Entry *matched = NULL;
374 struct berval realbase = { 0, NULL };
378 AttributeName *attrs;
380 Filter contextcsnand, contextcsnle, cookief, csnfnot, csnfeq, csnfand, csnfge;
381 AttributeAssertion aa_ge, aa_eq, aa_le;
383 struct berval *search_context_csn = NULL;
385 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
387 AttributeName uuid_attr[2];
389 int entry_sync_state = -1;
390 AttributeName null_attr;
391 int no_sync_state_change = 0;
392 struct slap_limits_set *limit = NULL;
395 u_int32_t locker = 0;
399 int sync_send_present_mode = 1;
406 LDAP_LOG( OPERATION, ENTRY, "bdb_search\n", 0, 0, 0 );
408 Debug( LDAP_DEBUG_TRACE, "=> bdb_search\n",
411 attrs = sop->oq_search.rs_attrs;
413 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
414 struct slap_session_entry *sent;
415 if ( sop->o_sync_state.sid >= 0 ) {
416 LDAP_LIST_FOREACH( sent, &bdb->bi_session_list, se_link ) {
417 if ( sent->se_id == sop->o_sync_state.sid ) {
418 sop->o_sync_slog_size = sent->se_size;
425 /* psearch needs to be registered before refresh begins */
426 /* psearch and refresh transmission is serialized in send_ldap_ber() */
427 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
428 ldap_pvt_thread_mutex_lock( &bdb->bi_pslist_mutex );
429 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
430 ldap_pvt_thread_mutex_unlock( &bdb->bi_pslist_mutex );
431 } else if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST
432 && sop->o_sync_slog_size >= 0 ) {
433 ldap_pvt_thread_mutex_lock( &bdb->bi_pslist_mutex );
434 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
435 if ( ps_list->o_sync_slog_size >= 0 ) {
436 if ( ps_list->o_sync_state.sid == sop->o_sync_state.sid ) {
444 if ( ps_list->o_sync_slog_omitcsn.bv_len != 0 ) {
445 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
446 if ( sop->o_sync_state.ctxcsn && sop->o_sync_state.ctxcsn->bv_val != NULL ) {
447 value_match( &match, slap_schema.si_ad_entryCSN, mr,
448 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
449 sop->o_sync_state.ctxcsn, &ps_list->o_sync_slog_omitcsn,
455 rs->sr_err = LDAP_SUCCESS;
456 rs->sr_rspoid = LDAP_SYNC_INFO;
458 bdb_send_ldap_intermediate( sop, rs,
459 LDAP_SYNC_STATE_MODE_DONE, NULL );
460 sync_send_present_mode = 0;
463 rs->sr_err = LDAP_SUCCESS;
464 rs->sr_rspoid = LDAP_SYNC_INFO;
466 bdb_send_ldap_intermediate( sop, rs,
467 LDAP_SYNC_STATE_MODE_DONE, NULL );
468 sync_send_present_mode = 0;
470 } else if ( sop->o_sync_slog_size >= 0 ) {
471 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
473 sop->o_sync_state.sid = -1;
475 ldap_pvt_thread_mutex_unlock( &bdb->bi_pslist_mutex );
478 null_attr.an_desc = NULL;
479 null_attr.an_oc = NULL;
480 null_attr.an_name.bv_len = 0;
481 null_attr.an_name.bv_val = NULL;
483 for( num_ctrls = 0; num_ctrls < SLAP_MAX_RESPONSE_CONTROLS; num_ctrls++ ) {
484 ctrls[num_ctrls] = NULL;
488 if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
490 attrs[0].an_desc = NULL;
491 attrs[0].an_oc = NULL;
492 attrs[0].an_name.bv_len = 0;
493 attrs[0].an_name.bv_val = NULL;
496 manageDSAit = get_manageDSAit( sop );
498 /* Sync control overrides manageDSAit */
500 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
501 if ( manageDSAit == SLAP_NO_CONTROL )
502 manageDSAit = SLAP_CRITICAL_CONTROL;
503 } else if ( IS_PSEARCH ) {
504 if ( manageDSAit == SLAP_NO_CONTROL )
505 manageDSAit = SLAP_CRITICAL_CONTROL;
508 rs->sr_err = LOCK_ID (bdb->bi_dbenv, &locker );
514 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
518 if ( sop->o_req_ndn.bv_len == 0 ) {
519 /* DIT root special case */
520 e = (Entry *) &slap_entry_root;
521 rs->sr_err = LDAP_SUCCESS;
524 /* get entry with reader lock */
525 rs->sr_err = bdb_dn2entry( op, NULL, &sop->o_req_ndn, &ei,
531 matched = ei->bei_e; break;
533 e = ei->bei_e; break;
535 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
536 LOCK_ID_FREE (bdb->bi_dbenv, locker );
538 case DB_LOCK_DEADLOCK:
539 case DB_LOCK_NOTGRANTED:
542 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
543 LOCK_ID_FREE (bdb->bi_dbenv, locker );
547 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
548 BDB_IDL_ZERO(candidates);
549 e = deref_base( op, rs, e, &matched, locker, &lock,
554 struct berval matched_dn = { 0, NULL };
556 if ( matched != NULL ) {
558 ber_dupbv( &matched_dn, &matched->e_name );
560 erefs = is_entry_referral( matched )
561 ? get_entry_referrals( op, matched )
564 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
569 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
570 &sop->o_req_dn, sop->oq_search.rs_scope );
571 ber_bvarray_free( erefs );
575 rs->sr_ref = referral_rewrite( default_referral,
576 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
579 rs->sr_err = LDAP_REFERRAL;
580 rs->sr_matched = matched_dn.bv_val;
581 send_ldap_result( sop, rs );
583 LOCK_ID_FREE (bdb->bi_dbenv, locker );
585 ber_bvarray_free( rs->sr_ref );
588 if ( matched_dn.bv_val ) {
589 ber_memfree( matched_dn.bv_val );
590 rs->sr_matched = NULL;
595 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
596 /* entry is a referral, don't allow add */
597 struct berval matched_dn;
600 ber_dupbv( &matched_dn, &e->e_name );
601 erefs = get_entry_referrals( op, e );
603 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
607 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
608 &sop->o_req_dn, sop->oq_search.rs_scope );
609 ber_bvarray_free( erefs );
613 LDAP_LOG ( OPERATION, RESULTS,
614 "bdb_search: entry is referral\n", 0, 0, 0 );
616 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
620 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
621 rs->sr_err = LDAP_REFERRAL;
622 rs->sr_matched = matched_dn.bv_val;
623 send_ldap_result( sop, rs );
625 LOCK_ID_FREE (bdb->bi_dbenv, locker );
626 ber_bvarray_free( rs->sr_ref );
628 ber_memfree( matched_dn.bv_val );
629 rs->sr_matched = NULL;
633 if ( get_assert( op ) &&
634 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
636 rs->sr_err = LDAP_ASSERTION_FAILED;
637 send_ldap_result( sop, rs );
641 /* if not root, get appropriate limits */
642 if ( be_isroot( op->o_bd, &sop->o_ndn ) ) {
645 ( void ) get_limits( op->o_bd, &sop->o_ndn, &limit );
648 /* The time/size limits come first because they require very little
649 * effort, so there's no chance the candidates are selected and then
650 * the request is not honored only because of time/size constraints */
652 /* if no time limit requested, use soft limit (unless root!) */
654 if ( sop->oq_search.rs_tlimit == 0 ) {
655 sop->oq_search.rs_tlimit = -1; /* allow root to set no limit */
658 if ( sop->oq_search.rs_slimit == 0 ) {
659 sop->oq_search.rs_slimit = -1;
663 /* if no limit is required, use soft limit */
664 if ( sop->oq_search.rs_tlimit <= 0 ) {
665 sop->oq_search.rs_tlimit = limit->lms_t_soft;
667 /* if requested limit higher than hard limit, abort */
668 } else if ( sop->oq_search.rs_tlimit > limit->lms_t_hard ) {
669 /* no hard limit means use soft instead */
670 if ( limit->lms_t_hard == 0
671 && limit->lms_t_soft > -1
672 && sop->oq_search.rs_tlimit > limit->lms_t_soft ) {
673 sop->oq_search.rs_tlimit = limit->lms_t_soft;
675 /* positive hard limit means abort */
676 } else if ( limit->lms_t_hard > 0 ) {
677 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
678 send_ldap_result( sop, rs );
679 rs->sr_err = LDAP_SUCCESS;
683 /* negative hard limit means no limit */
686 /* if no limit is required, use soft limit */
687 if ( sop->oq_search.rs_slimit <= 0 ) {
688 if ( get_pagedresults(sop) && limit->lms_s_pr != 0 ) {
689 sop->oq_search.rs_slimit = limit->lms_s_pr;
691 sop->oq_search.rs_slimit = limit->lms_s_soft;
694 /* if requested limit higher than hard limit, abort */
695 } else if ( sop->oq_search.rs_slimit > limit->lms_s_hard ) {
696 /* no hard limit means use soft instead */
697 if ( limit->lms_s_hard == 0
698 && limit->lms_s_soft > -1
699 && sop->oq_search.rs_slimit > limit->lms_s_soft ) {
700 sop->oq_search.rs_slimit = limit->lms_s_soft;
702 /* positive hard limit means abort */
703 } else if ( limit->lms_s_hard > 0 ) {
704 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
705 send_ldap_result( sop, rs );
706 rs->sr_err = LDAP_SUCCESS;
710 /* negative hard limit means no limit */
714 /* compute it anyway; root does not use it */
715 stoptime = op->o_time + sop->oq_search.rs_tlimit;
717 /* need normalized dn below */
718 ber_dupbv( &realbase, &e->e_nname );
720 /* Copy info to base, must free entry before accessing the database
721 * in search_candidates, to avoid deadlocks.
723 base.e_private = e->e_private;
724 base.e_nname = realbase;
727 if ( e != &slap_entry_root ) {
728 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
732 rs->sr_err = bdb_get_commit_csn( sop, rs, &search_context_csn, locker, &ctxcsn_lock );
734 if ( rs->sr_err != LDAP_SUCCESS ) {
735 send_ldap_error( sop, rs, rs->sr_err, "error in csn management in search" );
739 if ( sop->o_sync_mode != SLAP_SYNC_NONE && sop->o_sync_state.ctxcsn &&
740 sop->o_sync_state.ctxcsn->bv_val &&
741 ber_bvcmp( &sop->o_sync_state.ctxcsn[0], search_context_csn ) == 0 )
743 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
747 /* select candidates */
748 if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
749 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
752 BDB_IDL_ZERO( candidates );
753 BDB_IDL_ZERO( scopes );
754 rs->sr_err = search_candidates( op, sop, rs, &base, locker, candidates, scopes );
757 if ( sop->o_sync_mode != SLAP_SYNC_NONE ) {
758 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
761 /* start cursor at beginning of candidates.
765 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
766 cursor = bdb_idl_search( candidates, ps_e->e_id );
767 if ( candidates[cursor] != ps_e->e_id ) {
768 rs->sr_err = LDAP_SUCCESS;
772 if ( ps_e->e_id < BDB_IDL_RANGE_FIRST(candidates)
773 || ps_e->e_id > BDB_IDL_RANGE_LAST(candidates)){
774 rs->sr_err = LDAP_SUCCESS;
779 candidates[1] = ps_e->e_id;
782 if ( candidates[0] == 0 ) {
784 LDAP_LOG ( OPERATION, RESULTS,
785 "bdb_search: no candidates\n", 0, 0, 0 );
787 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
791 rs->sr_err = LDAP_SUCCESS;
793 send_ldap_result( sop, rs );
797 /* if not root and candidates exceed to-be-checked entries, abort */
798 if ( !isroot && limit->lms_s_unchecked != -1 ) {
799 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
800 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
801 send_ldap_result( sop, rs );
802 rs->sr_err = LDAP_SUCCESS;
807 if ( isroot || !limit->lms_s_pr_hide ) {
808 tentries = BDB_IDL_N(candidates);
811 #ifdef LDAP_CONTROL_PAGEDRESULTS
812 if ( get_pagedresults(sop) ) {
813 if ( sop->o_pagedresults_state.ps_cookie == 0 ) {
816 if ( sop->o_pagedresults_size == 0 ) {
817 rs->sr_err = LDAP_SUCCESS;
818 rs->sr_text = "search abandoned by pagedResult size=0";
819 send_ldap_result( sop, rs );
822 for ( id = bdb_idl_first( candidates, &cursor );
823 id != NOID && id <= (ID)( sop->o_pagedresults_state.ps_cookie );
824 id = bdb_idl_next( candidates, &cursor ) );
826 if ( cursor == NOID ) {
828 LDAP_LOG ( OPERATION, RESULTS,
829 "bdb_search: no paged results candidates\n",
832 Debug( LDAP_DEBUG_TRACE,
833 "bdb_search: no paged results candidates\n",
836 send_pagerequest_response( sop, rs, lastid, 0 );
838 rs->sr_err = LDAP_OTHER;
845 if ( (sop->o_sync_mode & SLAP_SYNC_REFRESH) || IS_PSEARCH )
849 cookief.f_choice = LDAP_FILTER_AND;
850 cookief.f_and = &csnfnot;
851 cookief.f_next = NULL;
853 csnfnot.f_choice = LDAP_FILTER_NOT;
854 csnfnot.f_not = &csnfeq;
855 csnfnot.f_next = &csnfand;
857 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
858 csnfeq.f_ava = &aa_eq;
859 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
860 if ( sop->o_sync_state.ctxcsn != NULL ) {
861 csnfeq.f_av_value = *sop->o_sync_state.ctxcsn;
863 csnfeq.f_av_value = slap_empty_bv;
866 csnfand.f_choice = LDAP_FILTER_AND;
867 csnfand.f_and = &csnfge;
868 csnfand.f_next = NULL;
870 csnfge.f_choice = LDAP_FILTER_GE;
871 csnfge.f_ava = &aa_ge;
872 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
873 if ( sop->o_sync_state.ctxcsn != NULL ) {
874 csnfge.f_av_value = *sop->o_sync_state.ctxcsn;
876 csnfge.f_av_value = slap_empty_bv;
879 if ( search_context_csn && !IS_PSEARCH ) {
880 csnfge.f_next = &contextcsnand;
882 contextcsnand.f_choice = LDAP_FILTER_AND;
883 contextcsnand.f_and = &contextcsnle;
884 contextcsnand.f_next = NULL;
886 contextcsnle.f_choice = LDAP_FILTER_LE;
887 contextcsnle.f_ava = &aa_le;
888 contextcsnle.f_av_desc = slap_schema.si_ad_entryCSN;
889 contextcsnle.f_av_value = *search_context_csn;
890 contextcsnle.f_next = sop->oq_search.rs_filter;
892 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
893 if ( sop->o_sync_state.ctxcsn &&
894 sop->o_sync_state.ctxcsn->bv_val != NULL ) {
895 value_match( &match, slap_schema.si_ad_entryCSN, mr,
896 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
897 &sop->o_sync_state.ctxcsn[0], search_context_csn,
902 no_sync_state_change = ( match >= 0 );
904 csnfge.f_next = sop->oq_search.rs_filter;
908 for ( id = bdb_idl_first( candidates, &cursor );
909 id != NOID && !no_sync_state_change;
910 id = bdb_idl_next( candidates, &cursor ) )
915 /* check for abandon */
916 if ( sop->o_abandon ) {
918 bdb_drop_psearch( sop, sop->o_msgid );
920 rs->sr_err = LDAP_SUCCESS;
924 #ifdef LDAP_EXOP_X_CANCEL
925 if ( sop->o_cancel ) {
926 assert( sop->o_cancel == SLAP_CANCEL_REQ );
927 rs->sr_err = LDAP_CANCELLED;
928 send_ldap_result( sop, rs );
929 sop->o_cancel = SLAP_CANCEL_ACK;
930 rs->sr_err = LDAP_SUCCESS;
935 /* check time limit */
936 if ( sop->oq_search.rs_tlimit != -1 && slap_get_time() > stoptime ) {
937 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
938 rs->sr_ref = rs->sr_v2ref;
939 send_ldap_result( sop, rs );
940 rs->sr_err = LDAP_SUCCESS;
947 /* get the entry with reader lock */
949 rs->sr_err = bdb_cache_find_id( op, NULL,
950 id, &ei, 0, locker, &lock );
952 if (rs->sr_err == LDAP_BUSY) {
953 rs->sr_text = "ldap server busy";
954 send_ldap_result( sop, rs );
957 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
958 || rs->sr_err == DB_LOCK_NOTGRANTED )
963 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
970 if( !BDB_IDL_IS_RANGE(candidates) ) {
971 /* only complain for non-range IDLs */
973 LDAP_LOG ( OPERATION, RESULTS,
974 "bdb_search: candidate %ld not found\n",
977 Debug( LDAP_DEBUG_TRACE,
978 "bdb_search: candidate %ld not found\n",
990 #ifdef BDB_SUBENTRIES
991 /* FIXME: send all but syncrepl
992 if ( !is_sync_protocol( sop ) ) {
994 if ( is_entry_subentry( e ) ) {
995 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
996 if(!get_subentries_visibility( sop )) {
997 /* only subentries are visible */
1001 } else if ( get_subentries( sop ) &&
1002 !get_subentries_visibility( sop ))
1004 /* only subentries are visible */
1008 } else if ( get_subentries_visibility( sop )) {
1009 /* only subentries are visible */
1017 /* Does this candidate actually satisfy the search scope?
1019 * Note that we don't lock access to the bei_parent pointer.
1020 * Since only leaf nodes can be deleted, the parent of any
1021 * node will always be a valid node. Also since we have
1022 * a Read lock on the data, it cannot be renamed out of the
1023 * scope while we are looking at it, and unless we're using
1024 * BDB_HIER, its parents cannot be moved either.
1026 switch( sop->ors_scope ) {
1027 case LDAP_SCOPE_BASE:
1028 /* This is always true, yes? */
1029 if ( id == base.e_id )
1032 case LDAP_SCOPE_ONELEVEL:
1033 if ( ei->bei_parent->bei_id == base.e_id )
1036 case LDAP_SCOPE_SUBTREE:
1038 for ( tmp = BEI(e); tmp->bei_parent;
1039 tmp = tmp->bei_parent ) {
1040 if ( tmp->bei_id == base.e_id ) {
1049 /* aliases were already dereferenced in candidate list */
1050 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
1051 /* but if the search base is an alias, and we didn't
1052 * deref it when finding, return it.
1054 if ( is_entry_alias(e) &&
1055 ((sop->ors_deref & LDAP_DEREF_FINDING)
1056 || !bvmatch(&e->e_nname, &op->o_req_ndn)))
1061 /* scopes is only non-empty for onelevel or subtree */
1062 if ( !scopeok && BDB_IDL_N(scopes) ) {
1064 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
1065 x = bdb_idl_search( scopes,
1067 if ( scopes[x] == e->e_id )
1070 /* subtree, walk up the tree */
1071 EntryInfo *tmp = BEI(e);
1072 for (;tmp->bei_parent;
1073 tmp=tmp->bei_parent) {
1075 scopes, tmp->bei_id );
1076 if ( scopes[x] == tmp->bei_id ) {
1086 /* Not in scope, ignore it */
1089 LDAP_LOG ( OPERATION, RESULTS,
1090 "bdb_search: %ld scope not okay\n",
1093 Debug( LDAP_DEBUG_TRACE,
1094 "bdb_search: %ld scope not okay\n",
1101 * if it's a referral, add it to the list of referrals. only do
1102 * this for non-base searches, and don't check the filter
1103 * explicitly here since it's only a candidate anyway.
1105 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1106 && is_entry_referral( e ) )
1108 BerVarray erefs = get_entry_referrals( sop, e );
1109 rs->sr_ref = referral_rewrite( erefs,
1111 sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE
1112 ? LDAP_SCOPE_SUBTREE
1113 : LDAP_SCOPE_BASE );
1115 send_search_reference( sop, rs );
1117 ber_bvarray_free( rs->sr_ref );
1118 ber_bvarray_free( erefs );
1124 if ( !manageDSAit && is_entry_glue( e )) {
1128 /* if it matches the filter and scope, send it */
1130 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1131 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1133 rs->sr_err = LDAP_COMPARE_TRUE;
1136 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1137 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1138 rs->sr_err = test_filter( sop, rs->sr_entry, &contextcsnand );
1139 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1140 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1141 if ( no_sync_state_change ) {
1143 LDAP_LOG ( OPERATION, RESULTS,
1144 "bdb_search: error in context csn management\n",
1147 Debug( LDAP_DEBUG_TRACE,
1148 "bdb_search: error in context csn management\n",
1152 entry_sync_state = LDAP_SYNC_ADD;
1154 if ( no_sync_state_change ) {
1157 entry_sync_state = LDAP_SYNC_PRESENT;
1161 rs->sr_err = test_filter( sop,
1162 rs->sr_entry, sop->oq_search.rs_filter );
1166 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1167 /* check size limit */
1168 if ( --sop->oq_search.rs_slimit == -1 &&
1169 sop->o_sync_slog_size == -1 ) {
1171 bdb_cache_return_entry_r( bdb->bi_dbenv,
1172 &bdb->bi_cache, e, &lock );
1175 rs->sr_entry = NULL;
1176 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1177 rs->sr_ref = rs->sr_v2ref;
1178 send_ldap_result( sop, rs );
1179 rs->sr_err = LDAP_SUCCESS;
1183 #ifdef LDAP_CONTROL_PAGEDRESULTS
1184 if ( get_pagedresults(sop) ) {
1185 if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
1186 send_pagerequest_response( sop, rs,
1198 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
1204 int premodify_found = 0;
1205 int entry_sync_state;
1207 if ( ps_type == LDAP_PSEARCH_BY_ADD ||
1208 ps_type == LDAP_PSEARCH_BY_DELETE ||
1209 ps_type == LDAP_PSEARCH_BY_MODIFY ||
1210 ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1212 if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1213 struct psid_entry* psid_e;
1214 LDAP_LIST_FOREACH( psid_e,
1215 &op->o_pm_list, ps_link)
1217 if( psid_e->ps_op == sop ) {
1218 premodify_found = 1;
1219 LDAP_LIST_REMOVE(psid_e, ps_link);
1223 if (psid_e != NULL) free (psid_e);
1225 if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1226 entry_sync_state = LDAP_SYNC_ADD;
1227 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1228 entry_sync_state = LDAP_SYNC_DELETE;
1229 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1230 if ( premodify_found ) {
1231 entry_sync_state = LDAP_SYNC_MODIFY;
1233 entry_sync_state = LDAP_SYNC_ADD;
1235 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT ) {
1236 entry_sync_state = LDAP_SYNC_DELETE;
1238 rs->sr_err = LDAP_OTHER;
1241 if ( sop->o_sync_slog_size != -1 ) {
1242 if ( entry_sync_state == LDAP_SYNC_DELETE ) {
1243 result = slap_add_session_log( op, sop, e );
1248 struct berval cookie;
1249 slap_compose_sync_cookie( sop, &cookie,
1251 sop->o_sync_state.sid );
1252 rs->sr_err = slap_build_sync_state_ctrl( sop,
1253 rs, e, entry_sync_state, ctrls,
1254 num_ctrls++, 1, &cookie );
1255 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1256 rs->sr_attrs = attrs;
1257 rs->sr_ctrls = ctrls;
1258 result = send_search_entry( sop, rs );
1259 if ( cookie.bv_val )
1260 ch_free( cookie.bv_val );
1261 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1262 ch_free( ctrls[--num_ctrls] );
1263 ctrls[num_ctrls] = NULL;
1264 rs->sr_ctrls = NULL;
1266 } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1267 struct psid_entry* psid_e;
1268 psid_e = (struct psid_entry *) ch_calloc (1,
1269 sizeof(struct psid_entry));
1270 psid_e->ps_op = sop;
1271 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1276 LDAP_LOG ( OPERATION, RESULTS,
1277 "bdb_search: invalid ps_type (%d) \n",
1280 Debug( LDAP_DEBUG_TRACE,
1281 "bdb_search: invalid ps_type (%d) \n",
1286 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1287 struct berval cookie;
1288 slap_compose_sync_cookie( sop, &cookie,
1290 sop->o_sync_state.sid );
1291 rs->sr_err = slap_build_sync_state_ctrl( sop,
1292 rs, e, entry_sync_state, ctrls,
1293 num_ctrls++, 0, &cookie );
1294 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1296 rs->sr_ctrls = ctrls;
1297 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1298 rs->sr_attrs = sop->oq_search.rs_attrs;
1299 result = send_search_entry( sop, rs );
1300 } else { /* PRESENT */
1301 if ( sync_send_present_mode ) {
1302 rs->sr_attrs = &null_attr;
1303 result = send_search_entry( sop, rs );
1309 if ( cookie.bv_val )
1310 ch_free( cookie.bv_val );
1311 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1312 ch_free( ctrls[--num_ctrls] );
1313 ctrls[num_ctrls] = NULL;
1314 rs->sr_ctrls = NULL;
1316 rs->sr_attrs = sop->oq_search.rs_attrs;
1317 rs->sr_ctrls = NULL;
1318 result = send_search_entry( sop, rs );
1323 case 0: /* entry sent ok */
1325 case 1: /* entry not sent */
1327 case -1: /* connection closed */
1329 bdb_cache_return_entry_r(bdb->bi_dbenv,
1330 &bdb->bi_cache, e, &lock);
1332 rs->sr_entry = NULL;
1333 rs->sr_err = LDAP_OTHER;
1339 LDAP_LOG ( OPERATION, RESULTS,
1340 "bdb_search: %ld does not match filter\n", (long) id, 0, 0);
1342 Debug( LDAP_DEBUG_TRACE,
1343 "bdb_search: %ld does not match filter\n",
1350 /* free reader lock */
1352 bdb_cache_return_entry_r( bdb->bi_dbenv,
1353 &bdb->bi_cache, e , &lock );
1354 if ( sop->o_nocaching ) {
1355 bdb_cache_delete_entry( bdb, ei, locker, &lock );
1359 rs->sr_entry = NULL;
1362 ldap_pvt_thread_yield();
1367 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1368 rs->sr_err = LDAP_SUCCESS;
1369 rs->sr_rspoid = LDAP_SYNC_INFO;
1370 rs->sr_ctrls = NULL;
1371 if ( sync_send_present_mode ) {
1372 struct berval cookie;
1373 slap_compose_sync_cookie( sop, &cookie,
1375 sop->o_sync_state.sid );
1376 bdb_send_ldap_intermediate( sop, rs,
1377 LDAP_SYNC_STATE_MODE_DONE, &cookie );
1378 if ( cookie.bv_val )
1379 ch_free( cookie.bv_val );
1382 if ( !sync_send_present_mode && !no_sync_state_change ) {
1384 ldap_pvt_thread_mutex_lock( &bdb->bi_pslist_mutex );
1385 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
1386 if ( ps_list->o_sync_slog_size > 0 ) {
1387 if ( ps_list->o_sync_state.sid == sop->o_sync_state.sid ) {
1395 slap_send_session_log( op, ps_list, rs );
1397 ldap_pvt_thread_mutex_unlock( &bdb->bi_pslist_mutex );
1400 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1401 /* refreshAndPersist mode */
1402 struct berval cookie;
1403 slap_compose_sync_cookie( sop, &cookie,
1405 sop->o_sync_state.sid );
1406 bdb_send_ldap_intermediate( sop, rs,
1407 LDAP_SYNC_LOG_MODE_DONE, &cookie );
1408 if ( cookie.bv_val ) {
1409 ch_free( cookie.bv_val );
1412 /* refreshOnly mode */
1413 struct berval cookie;
1414 slap_compose_sync_cookie( sop, &cookie,
1416 sop->o_sync_state.sid );
1417 slap_build_sync_done_ctrl( sop, rs, ctrls,
1418 num_ctrls++, 1, &cookie );
1419 rs->sr_ctrls = ctrls;
1420 rs->sr_ref = rs->sr_v2ref;
1421 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1422 send_ldap_result( sop, rs );
1423 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1424 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1426 ch_free( ctrls[--num_ctrls] );
1427 ctrls[num_ctrls] = NULL;
1428 if ( cookie.bv_val )
1429 ch_free( cookie.bv_val );
1432 rs->sr_ctrls = NULL;
1433 rs->sr_ref = rs->sr_v2ref;
1434 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1435 send_ldap_result( sop, rs );
1439 rs->sr_err = LDAP_SUCCESS;
1442 if( !IS_PSEARCH && e != NULL ) {
1443 /* free reader lock */
1444 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1447 LOCK_ID_FREE (bdb->bi_dbenv, locker );
1449 ber_bvfree( search_context_csn );
1451 if( rs->sr_v2ref ) {
1452 ber_bvarray_free( rs->sr_v2ref );
1453 rs->sr_v2ref = NULL;
1455 if( realbase.bv_val ) ch_free( realbase.bv_val );
1461 static int base_candidate(
1467 LDAP_LOG ( OPERATION, ENTRY,
1468 "base_candidate: base: \"%s\" (0x%08lx)\n",
1469 e->e_nname.bv_val, (long) e->e_id, 0);
1471 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1472 e->e_nname.bv_val, (long) e->e_id, 0);
1480 /* Look for "objectClass Present" in this filter.
1481 * Also count depth of filter tree while we're at it.
1483 static int oc_filter(
1491 if( cur > *max ) *max = cur;
1493 switch(f->f_choice) {
1494 case LDAP_FILTER_PRESENT:
1495 if (f->f_desc == slap_schema.si_ad_objectClass) {
1500 case LDAP_FILTER_AND:
1501 case LDAP_FILTER_OR:
1503 for (f=f->f_and; f; f=f->f_next) {
1504 (void) oc_filter(f, cur, max);
1514 static void search_stack_free( void *key, void *data )
1516 ber_memfree_x(data, NULL);
1519 static void *search_stack(
1523 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1526 if ( op->o_threadctx ) {
1527 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1530 ret = bdb->bi_search_stack;
1534 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1536 if ( op->o_threadctx ) {
1537 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1538 ret, search_stack_free );
1540 bdb->bi_search_stack = ret;
1546 static int search_candidates(
1555 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1557 Filter f, rf, xf, nf;
1559 AttributeAssertion aa_ref;
1560 #ifdef BDB_SUBENTRIES
1562 AttributeAssertion aa_subentry;
1566 * This routine takes as input a filter (user-filter)
1567 * and rewrites it as follows:
1568 * (&(scope=DN)[(objectClass=subentry)]
1569 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1573 LDAP_LOG ( OPERATION, ENTRY,
1574 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1575 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope);
1577 Debug(LDAP_DEBUG_TRACE,
1578 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1579 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1582 xf.f_or = op->oq_search.rs_filter;
1583 xf.f_choice = LDAP_FILTER_OR;
1586 /* If the user's filter uses objectClass=*,
1587 * these clauses are redundant.
1589 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1590 && !get_subentries_visibility(op)
1591 && !is_sync_protocol(op) )
1593 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1594 /* match referral objects */
1595 struct berval bv_ref = { sizeof("referral")-1, "referral" };
1596 rf.f_choice = LDAP_FILTER_EQUALITY;
1598 rf.f_av_desc = slap_schema.si_ad_objectClass;
1599 rf.f_av_value = bv_ref;
1600 rf.f_next = xf.f_or;
1607 f.f_choice = LDAP_FILTER_AND;
1609 /* Dummy; we compute scope separately now */
1610 nf.f_choice = SLAPD_FILTER_COMPUTED;
1611 nf.f_result = LDAP_SUCCESS;
1612 nf.f_next = xf.f_or == op->oq_search.rs_filter
1613 ? op->oq_search.rs_filter : &xf ;
1614 /* Filter depth increased again, adding dummy clause */
1617 #ifdef BDB_SUBENTRIES
1618 if( get_subentries_visibility( op ) ) {
1619 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
1620 sf.f_choice = LDAP_FILTER_EQUALITY;
1621 sf.f_ava = &aa_subentry;
1622 sf.f_av_desc = slap_schema.si_ad_objectClass;
1623 sf.f_av_value = bv_subentry;
1624 sf.f_next = nf.f_next;
1629 /* Allocate IDL stack, plus 1 more for former tmp */
1630 if ( depth+1 > bdb->bi_search_stack_depth ) {
1631 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1633 stack = search_stack( stackop );
1636 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1637 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1639 rc = bdb_dn2idl( op, e, ids, stack );
1642 if ( rc == LDAP_SUCCESS ) {
1643 rc = bdb_filter_candidates( op, &f, ids,
1644 stack, stack+BDB_IDL_UM_SIZE );
1647 if ( depth+1 > bdb->bi_search_stack_depth ) {
1653 LDAP_LOG ( OPERATION, DETAIL1,
1654 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
1656 Debug(LDAP_DEBUG_TRACE,
1657 "bdb_search_candidates: failed (rc=%d)\n",
1663 LDAP_LOG ( OPERATION, DETAIL1,
1664 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1665 (long) ids[0], (long) BDB_IDL_FIRST(ids),
1666 (long) BDB_IDL_LAST(ids));
1668 Debug(LDAP_DEBUG_TRACE,
1669 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1671 (long) BDB_IDL_FIRST(ids),
1672 (long) BDB_IDL_LAST(ids) );
1679 #ifdef LDAP_CONTROL_PAGEDRESULTS
1681 send_pagerequest_response(
1687 LDAPControl ctrl, *ctrls[2];
1688 BerElementBuffer berbuf;
1689 BerElement *ber = (BerElement *)&berbuf;
1690 struct berval cookie = { 0, NULL };
1691 PagedResultsCookie respcookie;
1694 LDAP_LOG ( OPERATION, ENTRY,
1695 "send_pagerequest_response: lastid: (0x%08lx) "
1696 "nentries: (0x%081x)\n",
1697 lastid, rs->sr_nentries, NULL );
1699 Debug(LDAP_DEBUG_ARGS, "send_pagerequest_response: lastid: (0x%08lx) "
1700 "nentries: (0x%081x)\n", lastid, rs->sr_nentries, NULL );
1703 ctrl.ldctl_value.bv_val = NULL;
1707 ber_init2( ber, NULL, LBER_USE_DER );
1709 respcookie = ( PagedResultsCookie )lastid;
1710 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1711 cookie.bv_len = sizeof( respcookie );
1712 cookie.bv_val = (char *)&respcookie;
1715 * FIXME: we should consider sending an estimate of the entries
1716 * left, after appropriate security check is done
1718 ber_printf( ber, "{iO}", tentries, &cookie );
1720 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1724 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1725 ctrls[0]->ldctl_iscritical = 0;
1727 rs->sr_ctrls = ctrls;
1728 rs->sr_err = LDAP_SUCCESS;
1729 send_ldap_result( op, rs );
1732 (void) ber_free_buf( ber );
1737 bdb_send_ldap_intermediate(
1741 struct berval *cookie )
1743 BerElementBuffer berbuf;
1744 BerElement *ber = (BerElement *)&berbuf;
1745 struct berval rspdata;
1749 ber_init2( ber, NULL, LBER_USE_DER );
1751 if ( cookie == NULL ) {
1752 ber_printf( ber, "{eN}", state );
1754 ber_printf( ber, "{eON}", state, cookie );
1757 ret = ber_flatten2( ber, &rspdata, 0 );
1761 LDAP_LOG ( OPERATION, RESULTS,
1762 "bdb_send_ldap_intermediate: ber_flatten2 failed\n",
1765 Debug( LDAP_DEBUG_TRACE,
1766 "bdb_send_ldap_intermediate: ber_flatten2 failed\n",
1769 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1773 rs->sr_rspdata = &rspdata;
1774 send_ldap_intermediate( op, rs );
1775 rs->sr_rspdata = NULL;
1776 ber_free_buf( ber );
1778 return LDAP_SUCCESS;