1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
26 static int base_candidate(
31 static int search_candidates(
32 Operation *stackop, /* op with the current threadctx/slab cache */
33 Operation *sop, /* search op */
40 static void send_pagerequest_response(
46 /* Dereference aliases for a single alias entry. Return the final
47 * dereferenced entry on success, NULL on any failure.
49 static Entry * deref_base (
59 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
64 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
65 rs->sr_text = "maximum deref depth exceeded";
67 while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
69 /* Remember the last entry we looked at, so we can
74 /* If this is part of a subtree or onelevel search,
75 * have we seen this ID before? If so, quit.
77 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
82 /* If we've seen this ID during this deref iteration,
85 if ( bdb_idl_insert( tmp, e->e_id ) ) {
86 rs->sr_err = LDAP_ALIAS_PROBLEM;
87 rs->sr_text = "circular alias";
92 /* If there was a problem getting the aliasedObjectName,
93 * get_alias_dn will have set the error status.
95 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
100 rs->sr_err = bdb_dn2entry( op, NULL, &ndn, &ei,
103 if ( ei ) e = ei->bei_e;
107 rs->sr_err = LDAP_ALIAS_PROBLEM;
108 rs->sr_text = "aliasedObject not found";
112 /* Free the previous entry, continue to work with the
113 * one we just retrieved.
115 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
119 /* We found a regular entry. Return this to the caller. The
120 * entry is still locked for Read.
122 if (!is_entry_alias(e)) {
123 rs->sr_err = LDAP_SUCCESS;
131 /* Look for and dereference all aliases within the search scope. Adds
132 * the dereferenced entries to the "ids" list. Requires "stack" to be
133 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
134 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
136 static int search_aliases(
146 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
147 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
148 ID cursora, ida, cursoro, ido, *subscop2;
151 struct berval bv_alias = { sizeof("alias")-1, "alias" };
152 AttributeAssertion aa_alias;
154 DB_LOCK locka, lockr;
157 aliases = stack; /* IDL of all aliases in the database */
158 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
159 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
160 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
161 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
162 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
163 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
165 /* A copy of subscop, because subscop gets clobbered by
166 * the bdb_idl_union/intersection routines
168 subscop2 = tmp + BDB_IDL_DB_SIZE;
170 af.f_choice = LDAP_FILTER_EQUALITY;
171 af.f_ava = &aa_alias;
172 af.f_av_desc = slap_schema.si_ad_objectClass;
173 af.f_av_value = bv_alias;
176 /* Find all aliases in database */
177 BDB_IDL_ZERO( aliases );
178 rs->sr_err = bdb_filter_candidates( op, &af, aliases,
180 if (rs->sr_err != LDAP_SUCCESS) {
184 oldsubs[1] = e->e_id;
187 BDB_IDL_ZERO( visited );
188 BDB_IDL_ZERO( newsubs );
191 ido = bdb_idl_first( oldsubs, &cursoro );
194 /* Set curscop to only the aliases in the current scope. Start with
195 * all the aliases, obtain the IDL for the current scope, and then
196 * get the intersection of these two IDLs. Add the current scope
197 * to the cumulative list of candidates.
199 BDB_IDL_CPY( curscop, aliases );
200 rs->sr_err = bdb_dn2idl( op, e, subscop,
201 subscop2+BDB_IDL_DB_SIZE );
205 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
207 BDB_IDL_CPY(subscop2, subscop);
208 rs->sr_err = bdb_idl_intersection(curscop, subscop);
209 bdb_idl_union( ids, subscop2 );
211 /* Dereference all of the aliases in the current scope. */
213 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
214 ida = bdb_idl_next(curscop, &cursora))
217 rs->sr_err = bdb_cache_find_id(op, NULL,
218 ida, &ei, 0, locker, &lockr );
219 if (rs->sr_err != LDAP_SUCCESS) {
224 /* This should only happen if the curscop IDL has maxed out and
225 * turned into a range that spans IDs indiscriminately
227 if (!is_entry_alias(a)) {
228 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
233 /* Actually dereference the alias */
235 a = deref_base( op, rs, a, &matched, locker, &lockr,
238 /* If the target was not already in our current candidates,
239 * make note of it in the newsubs list. Also
240 * set it in the scopes list so that bdb_search
243 if (bdb_idl_insert(ids, a->e_id) == 0) {
244 bdb_idl_insert(newsubs, a->e_id);
245 bdb_idl_insert(scopes, a->e_id);
247 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
250 } else if (matched) {
251 /* Alias could not be dereferenced, or it deref'd to
252 * an ID we've already seen. Ignore it.
254 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
259 /* If this is a OneLevel search, we're done; oldsubs only had one
260 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
262 if (op->ors_scope != LDAP_SCOPE_SUBTREE) break;
264 ido = bdb_idl_next( oldsubs, &cursoro );
266 /* If we're done processing the old scopes, did we add any new
267 * scopes in this iteration? If so, go back and do those now.
270 if (BDB_IDL_IS_ZERO(newsubs)) break;
271 BDB_IDL_CPY(oldsubs, newsubs);
272 BDB_IDL_ZERO(newsubs);
274 ido = bdb_idl_first( oldsubs, &cursoro );
277 /* Find the entry corresponding to the next scope. If it can't
278 * be found, ignore it and move on. This should never happen;
279 * we should never see the ID of an entry that doesn't exist.
280 * Set the name so that the scope's IDL can be retrieved.
283 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
285 if (rs->sr_err != LDAP_SUCCESS) goto nextido;
292 int is_sync_protocol( Operation *op )
294 if ( op->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST )
299 #define IS_BDB_REPLACE(type) (( type == LDAP_PSEARCH_BY_DELETE ) || \
300 ( type == LDAP_PSEARCH_BY_SCOPEOUT ))
301 #define IS_PSEARCH (op != sop)
304 bdb_drop_psearch( Operation *op, ber_int_t msgid )
307 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
309 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
310 if ( ps_list->o_connid == op->o_connid ) {
311 if ( ps_list->o_msgid == msgid ) {
312 ps_list->o_abandon = 1;
313 LDAP_LIST_REMOVE( ps_list, o_ps_link );
314 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
315 LDAP_STAILQ_REMOVE( &op->o_conn->c_ops, ps_list,
317 LDAP_STAILQ_NEXT( ps_list, o_next ) = NULL;
318 op->o_conn->c_n_ops_executing--;
319 op->o_conn->c_n_ops_completed++;
320 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
330 bdb_abandon( Operation *op, SlapReply *rs )
334 ps = bdb_drop_psearch( op, op->oq_abandon.rs_msgid );
336 if ( ps->o_tmpmemctx ) {
337 sl_mem_destroy( NULL, ps->o_tmpmemctx );
342 return LDAP_UNAVAILABLE;
346 bdb_cancel( Operation *op, SlapReply *rs )
350 ps = bdb_drop_psearch( op, op->oq_cancel.rs_msgid );
352 rs->sr_err = LDAP_CANCELLED;
353 send_ldap_result( ps, rs );
354 if ( ps->o_tmpmemctx ) {
355 sl_mem_destroy( NULL, ps->o_tmpmemctx );
360 return LDAP_UNAVAILABLE;
363 int bdb_search( Operation *op, SlapReply *rs )
365 return bdb_do_search( op, rs, op, NULL, 0 );
368 /* For persistent searches, op is the currently executing operation,
369 * sop is the persistent search. For regular searches, sop = op.
372 bdb_do_search( Operation *op, SlapReply *rs, Operation *sop,
373 Entry *ps_e, int ps_type )
375 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
378 ID candidates[BDB_IDL_UM_SIZE];
379 ID scopes[BDB_IDL_DB_SIZE];
380 Entry *e = NULL, base;
381 Entry *matched = NULL;
383 struct berval realbase = { 0, NULL };
387 AttributeName *attrs;
389 Filter contextcsnand, contextcsnle, cookief, csnfnot,
390 csnfeq, csnfand, csnfge;
391 AttributeAssertion aa_ge, aa_eq, aa_le;
393 struct berval *search_context_csn = NULL;
395 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
397 AttributeName uuid_attr[2];
399 int entry_sync_state = -1;
400 AttributeName null_attr;
401 int no_sync_state_change = 0;
402 struct slap_limits_set *limit = NULL;
405 u_int32_t locker = 0;
409 int sync_send_present_mode = 1;
415 BerVarray syncUUID_set = NULL;
416 int syncUUID_set_cnt = 0;
419 LDAP_LOG( OPERATION, ENTRY, "bdb_search\n", 0, 0, 0 );
421 Debug( LDAP_DEBUG_TRACE, "=> bdb_search\n",
424 attrs = sop->oq_search.rs_attrs;
426 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
427 struct slap_session_entry *sent;
428 if ( sop->o_sync_state.sid >= 0 ) {
429 LDAP_LIST_FOREACH( sent, &bdb->bi_session_list, se_link ) {
430 if ( sent->se_id == sop->o_sync_state.sid ) {
431 sop->o_sync_slog_size = sent->se_size;
438 /* psearch needs to be registered before refresh begins */
439 /* psearch and refresh transmission is serialized in send_ldap_ber() */
440 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
441 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
442 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
443 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
445 } else if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST
446 && sop->o_sync_slog_size >= 0 )
448 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
449 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
450 if ( ps_list->o_sync_slog_size >= 0 ) {
451 if ( ps_list->o_sync_state.sid == sop->o_sync_state.sid ) {
459 if ( ps_list->o_sync_slog_omitcsn.bv_len != 0 ) {
460 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
461 if ( sop->o_sync_state.ctxcsn &&
462 sop->o_sync_state.ctxcsn->bv_val != NULL ) {
463 value_match( &match, slap_schema.si_ad_entryCSN, mr,
464 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
465 sop->o_sync_state.ctxcsn,
466 &ps_list->o_sync_slog_omitcsn,
472 sync_send_present_mode = 0;
475 sync_send_present_mode = 0;
477 } else if ( sop->o_sync_slog_size >= 0 ) {
478 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
480 sop->o_sync_state.sid = -1;
482 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
485 null_attr.an_desc = NULL;
486 null_attr.an_oc = NULL;
487 null_attr.an_oc_exclude = 0;
488 null_attr.an_name.bv_len = 0;
489 null_attr.an_name.bv_val = NULL;
491 for( num_ctrls = 0; num_ctrls < SLAP_MAX_RESPONSE_CONTROLS; num_ctrls++ ) {
492 ctrls[num_ctrls] = NULL;
496 if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
498 attrs[0].an_desc = NULL;
499 attrs[0].an_oc = NULL;
500 attrs[0].an_oc_exclude = 0;
501 attrs[0].an_name.bv_len = 0;
502 attrs[0].an_name.bv_val = NULL;
505 manageDSAit = get_manageDSAit( sop );
507 /* Sync control overrides manageDSAit */
509 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
510 if ( manageDSAit == SLAP_NO_CONTROL ) {
511 manageDSAit = SLAP_CRITICAL_CONTROL;
513 } else if ( IS_PSEARCH ) {
514 if ( manageDSAit == SLAP_NO_CONTROL ) {
515 manageDSAit = SLAP_CRITICAL_CONTROL;
519 rs->sr_err = LOCK_ID (bdb->bi_dbenv, &locker );
525 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
529 if ( sop->o_req_ndn.bv_len == 0 ) {
530 /* DIT root special case */
531 e = (Entry *) &slap_entry_root;
532 rs->sr_err = LDAP_SUCCESS;
535 /* get entry with reader lock */
536 rs->sr_err = bdb_dn2entry( op, NULL, &sop->o_req_ndn, &ei,
548 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
549 LOCK_ID_FREE (bdb->bi_dbenv, locker );
551 case DB_LOCK_DEADLOCK:
552 case DB_LOCK_NOTGRANTED:
555 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
556 LOCK_ID_FREE (bdb->bi_dbenv, locker );
560 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
561 BDB_IDL_ZERO(candidates);
562 e = deref_base( op, rs, e, &matched, locker, &lock,
567 struct berval matched_dn = { 0, NULL };
569 if ( matched != NULL ) {
571 ber_dupbv( &matched_dn, &matched->e_name );
573 erefs = is_entry_referral( matched )
574 ? get_entry_referrals( op, matched )
577 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
582 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
583 &sop->o_req_dn, sop->oq_search.rs_scope );
584 ber_bvarray_free( erefs );
588 rs->sr_ref = referral_rewrite( default_referral,
589 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
592 rs->sr_err = LDAP_REFERRAL;
593 rs->sr_matched = matched_dn.bv_val;
594 send_ldap_result( sop, rs );
596 LOCK_ID_FREE (bdb->bi_dbenv, locker );
598 ber_bvarray_free( rs->sr_ref );
601 if ( matched_dn.bv_val ) {
602 ber_memfree( matched_dn.bv_val );
603 rs->sr_matched = NULL;
608 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
609 /* entry is a referral, don't allow add */
610 struct berval matched_dn;
613 ber_dupbv( &matched_dn, &e->e_name );
614 erefs = get_entry_referrals( op, e );
616 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
620 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
621 &sop->o_req_dn, sop->oq_search.rs_scope );
622 ber_bvarray_free( erefs );
626 LDAP_LOG ( OPERATION, RESULTS,
627 "bdb_search: entry is referral\n", 0, 0, 0 );
629 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
633 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
634 rs->sr_err = LDAP_REFERRAL;
635 rs->sr_matched = matched_dn.bv_val;
636 send_ldap_result( sop, rs );
638 LOCK_ID_FREE (bdb->bi_dbenv, locker );
639 ber_bvarray_free( rs->sr_ref );
641 ber_memfree( matched_dn.bv_val );
642 rs->sr_matched = NULL;
646 if ( get_assert( op ) &&
647 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
649 rs->sr_err = LDAP_ASSERTION_FAILED;
650 send_ldap_result( sop, rs );
654 /* if not root, get appropriate limits */
655 if ( be_isroot( op->o_bd, &sop->o_ndn ) ) {
658 ( void ) get_limits( op->o_bd, &sop->o_ndn, &limit );
661 /* The time/size limits come first because they require very little
662 * effort, so there's no chance the candidates are selected and then
663 * the request is not honored only because of time/size constraints */
665 /* if no time limit requested, use soft limit (unless root!) */
667 if ( sop->oq_search.rs_tlimit == 0 ) {
668 sop->oq_search.rs_tlimit = -1; /* allow root to set no limit */
671 if ( sop->oq_search.rs_slimit == 0 ) {
672 sop->oq_search.rs_slimit = -1;
676 /* if no limit is required, use soft limit */
677 if ( sop->oq_search.rs_tlimit <= 0 ) {
678 sop->oq_search.rs_tlimit = limit->lms_t_soft;
680 /* if requested limit higher than hard limit, abort */
681 } else if ( sop->oq_search.rs_tlimit > limit->lms_t_hard ) {
682 /* no hard limit means use soft instead */
683 if ( limit->lms_t_hard == 0
684 && limit->lms_t_soft > -1
685 && sop->oq_search.rs_tlimit > limit->lms_t_soft ) {
686 sop->oq_search.rs_tlimit = limit->lms_t_soft;
688 /* positive hard limit means abort */
689 } else if ( limit->lms_t_hard > 0 ) {
690 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
691 send_ldap_result( sop, rs );
692 rs->sr_err = LDAP_SUCCESS;
696 /* negative hard limit means no limit */
699 /* if no limit is required, use soft limit */
700 if ( sop->oq_search.rs_slimit <= 0 ) {
701 if ( get_pagedresults(sop) && limit->lms_s_pr != 0 ) {
702 sop->oq_search.rs_slimit = limit->lms_s_pr;
704 sop->oq_search.rs_slimit = limit->lms_s_soft;
707 /* if requested limit higher than hard limit, abort */
708 } else if ( sop->oq_search.rs_slimit > limit->lms_s_hard ) {
709 /* no hard limit means use soft instead */
710 if ( limit->lms_s_hard == 0
711 && limit->lms_s_soft > -1
712 && sop->oq_search.rs_slimit > limit->lms_s_soft ) {
713 sop->oq_search.rs_slimit = limit->lms_s_soft;
715 /* positive hard limit means abort */
716 } else if ( limit->lms_s_hard > 0 ) {
717 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
718 send_ldap_result( sop, rs );
719 rs->sr_err = LDAP_SUCCESS;
723 /* negative hard limit means no limit */
727 /* compute it anyway; root does not use it */
728 stoptime = op->o_time + sop->oq_search.rs_tlimit;
730 /* need normalized dn below */
731 ber_dupbv( &realbase, &e->e_nname );
733 /* Copy info to base, must free entry before accessing the database
734 * in search_candidates, to avoid deadlocks.
736 base.e_private = e->e_private;
737 base.e_nname = realbase;
740 if ( e != &slap_entry_root ) {
741 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
746 rs->sr_err = bdb_get_commit_csn( sop, rs, &search_context_csn,
747 locker, &ctxcsn_lock );
749 if ( rs->sr_err != LDAP_SUCCESS ) {
750 send_ldap_error( sop, rs, rs->sr_err,
751 "error in csn management in search" );
755 if ( sop->o_sync_mode != SLAP_SYNC_NONE &&
756 sop->o_sync_state.ctxcsn &&
757 sop->o_sync_state.ctxcsn->bv_val &&
758 ber_bvcmp( &sop->o_sync_state.ctxcsn[0], search_context_csn ) == 0 )
760 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
764 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );
767 /* select candidates */
768 if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
769 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
772 BDB_IDL_ZERO( candidates );
773 BDB_IDL_ZERO( scopes );
774 rs->sr_err = search_candidates( op, sop, rs, &base,
775 locker, candidates, scopes );
779 if ( sop->o_sync_mode != SLAP_SYNC_NONE ) {
780 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
784 /* start cursor at beginning of candidates.
788 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
789 cursor = bdb_idl_search( candidates, ps_e->e_id );
790 if ( candidates[cursor] != ps_e->e_id ) {
791 rs->sr_err = LDAP_SUCCESS;
795 if ( ps_e->e_id < BDB_IDL_RANGE_FIRST(candidates)
796 || ps_e->e_id > BDB_IDL_RANGE_LAST(candidates)){
797 rs->sr_err = LDAP_SUCCESS;
802 candidates[1] = ps_e->e_id;
805 if ( candidates[0] == 0 ) {
807 LDAP_LOG ( OPERATION, RESULTS,
808 "bdb_search: no candidates\n", 0, 0, 0 );
810 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
814 rs->sr_err = LDAP_SUCCESS;
816 send_ldap_result( sop, rs );
820 /* if not root and candidates exceed to-be-checked entries, abort */
821 if ( !isroot && limit->lms_s_unchecked != -1 ) {
822 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
823 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
824 send_ldap_result( sop, rs );
825 rs->sr_err = LDAP_SUCCESS;
830 if ( isroot || !limit->lms_s_pr_hide ) {
831 tentries = BDB_IDL_N(candidates);
834 if ( get_pagedresults(sop) ) {
835 if ( sop->o_pagedresults_state.ps_cookie == 0 ) {
838 if ( sop->o_pagedresults_size == 0 ) {
839 rs->sr_err = LDAP_SUCCESS;
840 rs->sr_text = "search abandoned by pagedResult size=0";
841 send_ldap_result( sop, rs );
844 for ( id = bdb_idl_first( candidates, &cursor );
845 id != NOID && id <= (ID)( sop->o_pagedresults_state.ps_cookie );
846 id = bdb_idl_next( candidates, &cursor ) );
848 if ( cursor == NOID ) {
850 LDAP_LOG ( OPERATION, RESULTS,
851 "bdb_search: no paged results candidates\n",
854 Debug( LDAP_DEBUG_TRACE,
855 "bdb_search: no paged results candidates\n",
858 send_pagerequest_response( sop, rs, lastid, 0 );
860 rs->sr_err = LDAP_OTHER;
866 if ( (sop->o_sync_mode & SLAP_SYNC_REFRESH) || IS_PSEARCH ) {
869 cookief.f_choice = LDAP_FILTER_AND;
870 cookief.f_and = &csnfnot;
871 cookief.f_next = NULL;
873 csnfnot.f_choice = LDAP_FILTER_NOT;
874 csnfnot.f_not = &csnfeq;
875 csnfnot.f_next = &csnfand;
877 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
878 csnfeq.f_ava = &aa_eq;
879 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
880 if ( sop->o_sync_state.ctxcsn != NULL ) {
881 csnfeq.f_av_value = *sop->o_sync_state.ctxcsn;
883 csnfeq.f_av_value = slap_empty_bv;
886 csnfand.f_choice = LDAP_FILTER_AND;
887 csnfand.f_and = &csnfge;
888 csnfand.f_next = NULL;
890 csnfge.f_choice = LDAP_FILTER_GE;
891 csnfge.f_ava = &aa_ge;
892 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
893 if ( sop->o_sync_state.ctxcsn != NULL ) {
894 csnfge.f_av_value = *sop->o_sync_state.ctxcsn;
896 csnfge.f_av_value = slap_empty_bv;
899 if ( search_context_csn && !IS_PSEARCH ) {
900 csnfge.f_next = &contextcsnand;
902 contextcsnand.f_choice = LDAP_FILTER_AND;
903 contextcsnand.f_and = &contextcsnle;
904 contextcsnand.f_next = NULL;
906 contextcsnle.f_choice = LDAP_FILTER_LE;
907 contextcsnle.f_ava = &aa_le;
908 contextcsnle.f_av_desc = slap_schema.si_ad_entryCSN;
909 contextcsnle.f_av_value = *search_context_csn;
910 contextcsnle.f_next = sop->oq_search.rs_filter;
912 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
913 if ( sop->o_sync_state.ctxcsn &&
914 sop->o_sync_state.ctxcsn->bv_val != NULL )
916 value_match( &match, slap_schema.si_ad_entryCSN, mr,
917 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
918 &sop->o_sync_state.ctxcsn[0], search_context_csn,
923 no_sync_state_change = ( match >= 0 );
925 csnfge.f_next = sop->oq_search.rs_filter;
929 for ( id = bdb_idl_first( candidates, &cursor );
930 id != NOID && !no_sync_state_change;
931 id = bdb_idl_next( candidates, &cursor ) )
936 /* check for abandon */
937 if ( sop->o_abandon ) {
939 bdb_drop_psearch( sop, sop->o_msgid );
941 rs->sr_err = LDAP_SUCCESS;
945 if ( sop->o_cancel ) {
946 assert( sop->o_cancel == SLAP_CANCEL_REQ );
947 rs->sr_err = LDAP_CANCELLED;
948 send_ldap_result( sop, rs );
949 sop->o_cancel = SLAP_CANCEL_ACK;
950 rs->sr_err = LDAP_SUCCESS;
954 /* check time limit */
955 if ( sop->oq_search.rs_tlimit != -1 && slap_get_time() > stoptime ) {
956 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
957 rs->sr_ref = rs->sr_v2ref;
958 send_ldap_result( sop, rs );
959 rs->sr_err = LDAP_SUCCESS;
966 /* get the entry with reader lock */
968 rs->sr_err = bdb_cache_find_id( op, NULL,
969 id, &ei, 0, locker, &lock );
971 if (rs->sr_err == LDAP_BUSY) {
972 rs->sr_text = "ldap server busy";
973 send_ldap_result( sop, rs );
976 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
977 || rs->sr_err == DB_LOCK_NOTGRANTED )
982 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
989 if( !BDB_IDL_IS_RANGE(candidates) ) {
990 /* only complain for non-range IDLs */
992 LDAP_LOG ( OPERATION, RESULTS,
993 "bdb_search: candidate %ld not found\n",
996 Debug( LDAP_DEBUG_TRACE,
997 "bdb_search: candidate %ld not found\n",
1009 #ifdef BDB_SUBENTRIES
1010 /* FIXME: send all but syncrepl */
1012 if ( !is_sync_protocol( sop ) )
1015 if ( is_entry_subentry( e ) ) {
1016 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
1017 if(!get_subentries_visibility( sop )) {
1018 /* only subentries are visible */
1022 } else if ( get_subentries( sop ) &&
1023 !get_subentries_visibility( sop ))
1025 /* only subentries are visible */
1029 } else if ( get_subentries_visibility( sop )) {
1030 /* only subentries are visible */
1034 #endif /* BDB_SUBENTRIES */
1036 /* Does this candidate actually satisfy the search scope?
1038 * Note that we don't lock access to the bei_parent pointer.
1039 * Since only leaf nodes can be deleted, the parent of any
1040 * node will always be a valid node. Also since we have
1041 * a Read lock on the data, it cannot be renamed out of the
1042 * scope while we are looking at it, and unless we're using
1043 * BDB_HIER, its parents cannot be moved either.
1045 switch( sop->ors_scope ) {
1046 case LDAP_SCOPE_BASE:
1047 /* This is always true, yes? */
1048 if ( id == base.e_id ) scopeok = 1;
1051 case LDAP_SCOPE_ONELEVEL:
1052 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
1055 case LDAP_SCOPE_SUBTREE: {
1057 for ( tmp = BEI(e); tmp->bei_parent;
1058 tmp = tmp->bei_parent )
1060 if ( tmp->bei_id == base.e_id ) {
1069 /* aliases were already dereferenced in candidate list */
1070 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
1071 /* but if the search base is an alias, and we didn't
1072 * deref it when finding, return it.
1074 if ( is_entry_alias(e) &&
1075 ((sop->ors_deref & LDAP_DEREF_FINDING) ||
1076 !bvmatch(&e->e_nname, &op->o_req_ndn)))
1081 /* scopes is only non-empty for onelevel or subtree */
1082 if ( !scopeok && BDB_IDL_N(scopes) ) {
1084 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
1085 x = bdb_idl_search( scopes,
1087 if ( scopes[x] == e->e_id )
1090 /* subtree, walk up the tree */
1091 EntryInfo *tmp = BEI(e);
1092 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
1093 x = bdb_idl_search( scopes, tmp->bei_id );
1094 if ( scopes[x] == tmp->bei_id ) {
1104 /* Not in scope, ignore it */
1107 LDAP_LOG ( OPERATION, RESULTS,
1108 "bdb_search: %ld scope not okay\n",
1111 Debug( LDAP_DEBUG_TRACE,
1112 "bdb_search: %ld scope not okay\n",
1119 * if it's a referral, add it to the list of referrals. only do
1120 * this for non-base searches, and don't check the filter
1121 * explicitly here since it's only a candidate anyway.
1123 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1124 && is_entry_referral( e ) )
1126 BerVarray erefs = get_entry_referrals( sop, e );
1127 rs->sr_ref = referral_rewrite( erefs,
1129 sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE
1130 ? LDAP_SCOPE_SUBTREE
1131 : LDAP_SCOPE_BASE );
1133 send_search_reference( sop, rs );
1135 ber_bvarray_free( rs->sr_ref );
1136 ber_bvarray_free( erefs );
1142 if ( !manageDSAit && is_entry_glue( e )) {
1146 /* if it matches the filter and scope, send it */
1148 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1149 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1151 rs->sr_err = LDAP_COMPARE_TRUE;
1154 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1155 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1156 rs->sr_err = test_filter( sop, rs->sr_entry, &contextcsnand );
1157 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1158 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1159 if ( no_sync_state_change ) {
1161 LDAP_LOG ( OPERATION, RESULTS,
1162 "bdb_search: error in context csn management\n",
1165 Debug( LDAP_DEBUG_TRACE,
1166 "bdb_search: error in context csn management\n",
1170 entry_sync_state = LDAP_SYNC_ADD;
1172 if ( no_sync_state_change ) {
1175 entry_sync_state = LDAP_SYNC_PRESENT;
1179 rs->sr_err = test_filter( sop,
1180 rs->sr_entry, sop->oq_search.rs_filter );
1184 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1185 /* check size limit */
1186 if ( --sop->oq_search.rs_slimit == -1 &&
1187 sop->o_sync_slog_size == -1 )
1190 bdb_cache_return_entry_r( bdb->bi_dbenv,
1191 &bdb->bi_cache, e, &lock );
1194 rs->sr_entry = NULL;
1195 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1196 rs->sr_ref = rs->sr_v2ref;
1197 send_ldap_result( sop, rs );
1198 rs->sr_err = LDAP_SUCCESS;
1202 if ( get_pagedresults(sop) ) {
1203 if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
1204 send_pagerequest_response( sop, rs,
1216 int premodify_found = 0;
1217 int entry_sync_state;
1219 if ( ps_type == LDAP_PSEARCH_BY_ADD ||
1220 ps_type == LDAP_PSEARCH_BY_DELETE ||
1221 ps_type == LDAP_PSEARCH_BY_MODIFY ||
1222 ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1224 if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1225 struct psid_entry* psid_e;
1226 LDAP_LIST_FOREACH( psid_e,
1227 &op->o_pm_list, ps_link)
1229 if( psid_e->ps_op == sop ) {
1230 premodify_found = 1;
1231 LDAP_LIST_REMOVE(psid_e, ps_link);
1235 if (psid_e != NULL) free (psid_e);
1237 if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1238 entry_sync_state = LDAP_SYNC_ADD;
1239 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1240 entry_sync_state = LDAP_SYNC_DELETE;
1241 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1242 if ( premodify_found ) {
1243 entry_sync_state = LDAP_SYNC_MODIFY;
1245 entry_sync_state = LDAP_SYNC_ADD;
1247 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT ) {
1248 entry_sync_state = LDAP_SYNC_DELETE;
1250 rs->sr_err = LDAP_OTHER;
1253 if ( sop->o_sync_slog_size != -1 ) {
1254 if ( entry_sync_state == LDAP_SYNC_DELETE ) {
1255 result = slap_add_session_log( op, sop, e );
1260 struct berval cookie;
1261 slap_compose_sync_cookie( sop, &cookie,
1263 sop->o_sync_state.sid,
1264 sop->o_sync_state.rid );
1265 rs->sr_err = slap_build_sync_state_ctrl( sop,
1266 rs, e, entry_sync_state, ctrls,
1267 num_ctrls++, 1, &cookie );
1268 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1269 rs->sr_attrs = attrs;
1270 rs->sr_ctrls = ctrls;
1271 result = send_search_entry( sop, rs );
1272 if ( cookie.bv_val )
1273 ch_free( cookie.bv_val );
1274 sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val,
1276 sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1277 ctrls[num_ctrls] = NULL;
1278 rs->sr_ctrls = NULL;
1280 } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1281 struct psid_entry* psid_e;
1282 psid_e = (struct psid_entry *) ch_calloc (1,
1283 sizeof(struct psid_entry));
1284 psid_e->ps_op = sop;
1285 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1290 LDAP_LOG ( OPERATION, RESULTS,
1291 "bdb_search: invalid ps_type (%d) \n",
1294 Debug( LDAP_DEBUG_TRACE,
1295 "bdb_search: invalid ps_type (%d) \n",
1300 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1302 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1303 rs->sr_err = slap_build_sync_state_ctrl( sop,
1304 rs, e, entry_sync_state, ctrls,
1305 num_ctrls++, 0, NULL );
1306 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1307 rs->sr_ctrls = ctrls;
1308 rs->sr_attrs = sop->oq_search.rs_attrs;
1309 result = send_search_entry( sop, rs );
1310 sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val,
1312 sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1313 ctrls[num_ctrls] = NULL;
1314 rs->sr_ctrls = NULL;
1315 } else { /* PRESENT */
1316 if ( sync_send_present_mode ) {
1317 result = slap_build_syncUUID_set( sop,
1319 if ( result <= 0 ) {
1323 if ( syncUUID_set_cnt == SLAP_SYNCUUID_SET_SIZE ) {
1324 rs->sr_err = LDAP_SUCCESS;
1325 rs->sr_rspoid = LDAP_SYNC_INFO;
1326 rs->sr_ctrls = NULL;
1327 result = slap_send_syncinfo( sop, rs,
1328 LDAP_TAG_SYNC_ID_SET,
1329 NULL, 0, syncUUID_set, 0 );
1330 if ( result != LDAP_SUCCESS )
1332 ber_bvarray_free_x( syncUUID_set,
1334 syncUUID_set = NULL;
1335 syncUUID_set_cnt = 0;
1343 rs->sr_attrs = sop->oq_search.rs_attrs;
1344 rs->sr_ctrls = NULL;
1345 result = send_search_entry( sop, rs );
1350 case 0: /* entry sent ok */
1352 case 1: /* entry not sent */
1354 case -1: /* connection closed */
1356 bdb_cache_return_entry_r(bdb->bi_dbenv,
1357 &bdb->bi_cache, e, &lock);
1359 rs->sr_entry = NULL;
1360 rs->sr_err = LDAP_OTHER;
1366 LDAP_LOG ( OPERATION, RESULTS,
1367 "bdb_search: %ld does not match filter\n", (long) id, 0, 0);
1369 Debug( LDAP_DEBUG_TRACE,
1370 "bdb_search: %ld does not match filter\n",
1377 /* free reader lock */
1379 bdb_cache_return_entry_r( bdb->bi_dbenv,
1380 &bdb->bi_cache, e , &lock );
1381 if ( sop->o_nocaching ) {
1382 bdb_cache_delete_entry( bdb, ei, locker, &lock );
1386 rs->sr_entry = NULL;
1389 ldap_pvt_thread_yield();
1392 if ( syncUUID_set_cnt > 0 ) {
1393 rs->sr_err = LDAP_SUCCESS;
1394 rs->sr_rspoid = LDAP_SYNC_INFO;
1395 rs->sr_ctrls = NULL;
1396 slap_send_syncinfo( sop, rs, LDAP_TAG_SYNC_ID_SET,
1397 NULL, 0, syncUUID_set, 0 );
1398 ber_bvarray_free_x( syncUUID_set, sop->o_tmpmemctx );
1399 syncUUID_set_cnt = 0;
1404 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1405 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1406 struct berval cookie;
1407 slap_compose_sync_cookie( sop, &cookie,
1409 sop->o_sync_state.sid,
1410 sop->o_sync_state.rid );
1412 if ( sync_send_present_mode ) {
1413 rs->sr_err = LDAP_SUCCESS;
1414 rs->sr_rspoid = LDAP_SYNC_INFO;
1415 rs->sr_ctrls = NULL;
1416 slap_send_syncinfo( sop, rs,
1417 LDAP_TAG_SYNC_REFRESH_PRESENT, &cookie, 1, NULL, 0 );
1419 if ( !no_sync_state_change ) {
1421 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1422 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
1423 if ( ps_list->o_sync_slog_size > 0 ) {
1424 if ( ps_list->o_sync_state.sid == sop->o_sync_state.sid ) {
1432 rs->sr_err = LDAP_SUCCESS;
1433 rs->sr_rspoid = NULL;
1434 rs->sr_ctrls = NULL;
1435 slap_send_session_log( op, ps_list, rs );
1437 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1439 rs->sr_err = LDAP_SUCCESS;
1440 rs->sr_rspoid = LDAP_SYNC_INFO;
1441 rs->sr_ctrls = NULL;
1442 slap_send_syncinfo( sop, rs,
1443 LDAP_TAG_SYNC_REFRESH_DELETE, &cookie, 1, NULL, 0 );
1446 if ( cookie.bv_val ) {
1447 ch_free( cookie.bv_val );
1450 /* refreshOnly mode */
1451 struct berval cookie;
1452 slap_compose_sync_cookie( sop, &cookie,
1454 sop->o_sync_state.sid,
1455 sop->o_sync_state.rid );
1457 if ( sync_send_present_mode ) {
1458 slap_build_sync_done_ctrl( sop, rs, ctrls,
1459 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1461 if ( !no_sync_state_change ) {
1463 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1464 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1466 if ( ps_list->o_sync_slog_size > 0 ) {
1467 if ( ps_list->o_sync_state.sid ==
1468 sop->o_sync_state.sid ) {
1476 slap_send_session_log( op, ps_list, rs );
1478 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1480 slap_build_sync_done_ctrl( sop, rs, ctrls,
1481 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_DELETES );
1484 rs->sr_ctrls = ctrls;
1485 rs->sr_ref = rs->sr_v2ref;
1486 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1487 rs->sr_rspoid = NULL;
1488 send_ldap_result( sop, rs );
1489 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1490 sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val, sop->o_tmpmemctx );
1492 sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1493 ctrls[num_ctrls] = NULL;
1494 if ( cookie.bv_val )
1495 ch_free( cookie.bv_val );
1498 rs->sr_ctrls = NULL;
1499 rs->sr_ref = rs->sr_v2ref;
1500 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1501 rs->sr_rspoid = NULL;
1502 send_ldap_result( sop, rs );
1506 rs->sr_err = LDAP_SUCCESS;
1509 if( !IS_PSEARCH && e != NULL ) {
1510 /* free reader lock */
1511 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1514 LOCK_ID_FREE (bdb->bi_dbenv, locker );
1516 ber_bvfree( search_context_csn );
1518 if( rs->sr_v2ref ) {
1519 ber_bvarray_free( rs->sr_v2ref );
1520 rs->sr_v2ref = NULL;
1522 if( realbase.bv_val ) ch_free( realbase.bv_val );
1528 static int base_candidate(
1534 LDAP_LOG ( OPERATION, ENTRY,
1535 "base_candidate: base: \"%s\" (0x%08lx)\n",
1536 e->e_nname.bv_val, (long) e->e_id, 0);
1538 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1539 e->e_nname.bv_val, (long) e->e_id, 0);
1547 /* Look for "objectClass Present" in this filter.
1548 * Also count depth of filter tree while we're at it.
1550 static int oc_filter(
1560 if( cur > *max ) *max = cur;
1562 switch(f->f_choice) {
1563 case LDAP_FILTER_PRESENT:
1564 if (f->f_desc == slap_schema.si_ad_objectClass) {
1569 case LDAP_FILTER_AND:
1570 case LDAP_FILTER_OR:
1572 for (f=f->f_and; f; f=f->f_next) {
1573 (void) oc_filter(f, cur, max);
1583 static void search_stack_free( void *key, void *data )
1585 ber_memfree_x(data, NULL);
1588 static void *search_stack( Operation *op )
1590 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1593 if ( op->o_threadctx ) {
1594 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1597 ret = bdb->bi_search_stack;
1601 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1603 if ( op->o_threadctx ) {
1604 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1605 ret, search_stack_free );
1607 bdb->bi_search_stack = ret;
1613 static int search_candidates(
1622 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1624 Filter f, rf, xf, nf;
1626 AttributeAssertion aa_ref;
1627 #ifdef BDB_SUBENTRIES
1629 AttributeAssertion aa_subentry;
1633 * This routine takes as input a filter (user-filter)
1634 * and rewrites it as follows:
1635 * (&(scope=DN)[(objectClass=subentry)]
1636 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1640 LDAP_LOG ( OPERATION, ENTRY,
1641 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1642 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope);
1644 Debug(LDAP_DEBUG_TRACE,
1645 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1646 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1649 xf.f_or = op->oq_search.rs_filter;
1650 xf.f_choice = LDAP_FILTER_OR;
1653 /* If the user's filter uses objectClass=*,
1654 * these clauses are redundant.
1656 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1657 && !get_subentries_visibility(op)
1658 && !is_sync_protocol(op) )
1660 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1661 /* match referral objects */
1662 struct berval bv_ref = { sizeof("referral")-1, "referral" };
1663 rf.f_choice = LDAP_FILTER_EQUALITY;
1665 rf.f_av_desc = slap_schema.si_ad_objectClass;
1666 rf.f_av_value = bv_ref;
1667 rf.f_next = xf.f_or;
1674 f.f_choice = LDAP_FILTER_AND;
1676 /* Dummy; we compute scope separately now */
1677 nf.f_choice = SLAPD_FILTER_COMPUTED;
1678 nf.f_result = LDAP_SUCCESS;
1679 nf.f_next = xf.f_or == op->oq_search.rs_filter
1680 ? op->oq_search.rs_filter : &xf ;
1681 /* Filter depth increased again, adding dummy clause */
1684 #ifdef BDB_SUBENTRIES
1685 if( get_subentries_visibility( op ) ) {
1686 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
1687 sf.f_choice = LDAP_FILTER_EQUALITY;
1688 sf.f_ava = &aa_subentry;
1689 sf.f_av_desc = slap_schema.si_ad_objectClass;
1690 sf.f_av_value = bv_subentry;
1691 sf.f_next = nf.f_next;
1696 /* Allocate IDL stack, plus 1 more for former tmp */
1697 if ( depth+1 > bdb->bi_search_stack_depth ) {
1698 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1700 stack = search_stack( stackop );
1703 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1704 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1706 rc = bdb_dn2idl( op, e, ids, stack );
1709 if ( rc == LDAP_SUCCESS ) {
1710 rc = bdb_filter_candidates( op, &f, ids,
1711 stack, stack+BDB_IDL_UM_SIZE );
1714 if ( depth+1 > bdb->bi_search_stack_depth ) {
1720 LDAP_LOG ( OPERATION, DETAIL1,
1721 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
1723 Debug(LDAP_DEBUG_TRACE,
1724 "bdb_search_candidates: failed (rc=%d)\n",
1730 LDAP_LOG ( OPERATION, DETAIL1,
1731 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1732 (long) ids[0], (long) BDB_IDL_FIRST(ids),
1733 (long) BDB_IDL_LAST(ids));
1735 Debug(LDAP_DEBUG_TRACE,
1736 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1738 (long) BDB_IDL_FIRST(ids),
1739 (long) BDB_IDL_LAST(ids) );
1747 send_pagerequest_response(
1753 LDAPControl ctrl, *ctrls[2];
1754 BerElementBuffer berbuf;
1755 BerElement *ber = (BerElement *)&berbuf;
1756 struct berval cookie = { 0, NULL };
1757 PagedResultsCookie respcookie;
1760 LDAP_LOG ( OPERATION, ENTRY,
1761 "send_pagerequest_response: lastid: (0x%08lx) "
1762 "nentries: (0x%081x)\n",
1763 lastid, rs->sr_nentries, NULL );
1765 Debug(LDAP_DEBUG_ARGS, "send_pagerequest_response: lastid: (0x%08lx) "
1766 "nentries: (0x%081x)\n", lastid, rs->sr_nentries, NULL );
1769 ctrl.ldctl_value.bv_val = NULL;
1773 ber_init2( ber, NULL, LBER_USE_DER );
1775 respcookie = ( PagedResultsCookie )lastid;
1776 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1777 cookie.bv_len = sizeof( respcookie );
1778 cookie.bv_val = (char *)&respcookie;
1781 * FIXME: we should consider sending an estimate of the entries
1782 * left, after appropriate security check is done
1784 ber_printf( ber, "{iO}", tentries, &cookie );
1786 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1790 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1791 ctrls[0]->ldctl_iscritical = 0;
1793 rs->sr_ctrls = ctrls;
1794 rs->sr_err = LDAP_SUCCESS;
1795 send_ldap_result( op, rs );
1798 (void) ber_free_buf( ber );