1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 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 int parse_paged_cookie( Operation *op, SlapReply *rs );
42 static void send_paged_response(
48 /* Dereference aliases for a single alias entry. Return the final
49 * dereferenced entry on success, NULL on any failure.
51 static Entry * deref_base (
61 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
66 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
67 rs->sr_text = "maximum deref depth exceeded";
69 while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
70 /* Remember the last entry we looked at, so we can
75 /* If this is part of a subtree or onelevel search,
76 * have we seen this ID before? If so, quit.
78 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
83 /* If we've seen this ID during this deref iteration,
86 if ( bdb_idl_insert( tmp, e->e_id ) ) {
87 rs->sr_err = LDAP_ALIAS_PROBLEM;
88 rs->sr_text = "circular alias";
93 /* If there was a problem getting the aliasedObjectName,
94 * get_alias_dn will have set the error status.
96 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
101 rs->sr_err = bdb_dn2entry( op, NULL, &ndn, &ei,
111 rs->sr_err = LDAP_ALIAS_PROBLEM;
112 rs->sr_text = "aliasedObject not found";
116 /* Free the previous entry, continue to work with the
117 * one we just retrieved.
119 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
123 /* We found a regular entry. Return this to the caller. The
124 * entry is still locked for Read.
126 if (!is_entry_alias(e)) {
127 rs->sr_err = LDAP_SUCCESS;
135 /* Look for and dereference all aliases within the search scope. Adds
136 * the dereferenced entries to the "ids" list. Requires "stack" to be
137 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
138 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
140 static int search_aliases(
149 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
150 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
151 ID cursora, ida, cursoro, ido, *subscop2;
154 struct berval bv_alias = BER_BVC( "alias" );
155 AttributeAssertion aa_alias;
157 DB_LOCK locka, lockr;
160 aliases = stack; /* IDL of all aliases in the database */
161 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
162 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
163 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
164 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
165 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
166 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
168 /* A copy of subscop, because subscop gets clobbered by
169 * the bdb_idl_union/intersection routines
171 subscop2 = tmp + BDB_IDL_DB_SIZE;
173 af.f_choice = LDAP_FILTER_EQUALITY;
174 af.f_ava = &aa_alias;
175 af.f_av_desc = slap_schema.si_ad_objectClass;
176 af.f_av_value = bv_alias;
179 /* Find all aliases in database */
180 BDB_IDL_ZERO( aliases );
181 rs->sr_err = bdb_filter_candidates( op, &af, aliases,
183 if (rs->sr_err != LDAP_SUCCESS) {
187 oldsubs[1] = e->e_id;
190 BDB_IDL_ZERO( visited );
191 BDB_IDL_ZERO( newsubs );
194 ido = bdb_idl_first( oldsubs, &cursoro );
197 /* Set curscop to only the aliases in the current scope. Start with
198 * all the aliases, obtain the IDL for the current scope, and then
199 * get the intersection of these two IDLs. Add the current scope
200 * to the cumulative list of candidates.
202 BDB_IDL_CPY( curscop, aliases );
203 rs->sr_err = bdb_dn2idl( op, e, subscop,
204 subscop2+BDB_IDL_DB_SIZE );
208 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
210 BDB_IDL_CPY(subscop2, subscop);
211 rs->sr_err = bdb_idl_intersection(curscop, subscop);
212 bdb_idl_union( ids, subscop2 );
214 /* Dereference all of the aliases in the current scope. */
216 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
217 ida = bdb_idl_next(curscop, &cursora))
221 rs->sr_err = bdb_cache_find_id(op, NULL,
222 ida, &ei, 0, locker, &lockr );
223 if (rs->sr_err != LDAP_SUCCESS) {
224 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
225 rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
230 /* This should only happen if the curscop IDL has maxed out and
231 * turned into a range that spans IDs indiscriminately
233 if (!is_entry_alias(a)) {
234 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
239 /* Actually dereference the alias */
241 a = deref_base( op, rs, a, &matched, locker, &lockr,
244 /* If the target was not already in our current candidates,
245 * make note of it in the newsubs list. Also
246 * set it in the scopes list so that bdb_search
249 if (bdb_idl_insert(ids, a->e_id) == 0) {
250 bdb_idl_insert(newsubs, a->e_id);
251 bdb_idl_insert(scopes, a->e_id);
253 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
256 } else if (matched) {
257 /* Alias could not be dereferenced, or it deref'd to
258 * an ID we've already seen. Ignore it.
260 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
265 /* If this is a OneLevel search, we're done; oldsubs only had one
266 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
268 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
270 ido = bdb_idl_next( oldsubs, &cursoro );
272 /* If we're done processing the old scopes, did we add any new
273 * scopes in this iteration? If so, go back and do those now.
276 if (BDB_IDL_IS_ZERO(newsubs)) break;
277 BDB_IDL_CPY(oldsubs, newsubs);
278 BDB_IDL_ZERO(newsubs);
280 ido = bdb_idl_first( oldsubs, &cursoro );
283 /* Find the entry corresponding to the next scope. If it can't
284 * be found, ignore it and move on. This should never happen;
285 * we should never see the ID of an entry that doesn't exist.
286 * Set the name so that the scope's IDL can be retrieved.
290 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
292 if ( rs->sr_err != LDAP_SUCCESS ) {
293 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
294 rs->sr_err == DB_LOCK_NOTGRANTED )
303 #define is_sync_protocol(op) \
304 ((op)->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST)
306 #define IS_BDB_REPLACE(type) (( type == LDAP_PSEARCH_BY_DELETE ) || \
307 ( type == LDAP_PSEARCH_BY_SCOPEOUT ))
308 #define IS_PSEARCH (op != sop)
311 bdb_drop_psearch( Operation *op, ber_int_t msgid )
314 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
316 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
317 if ( ps_list->o_connid == op->o_connid ) {
318 if ( ps_list->o_msgid == msgid ) {
319 ps_list->o_abandon = 1;
320 LDAP_LIST_REMOVE( ps_list, o_ps_link );
321 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
322 LDAP_STAILQ_REMOVE( &op->o_conn->c_ops, ps_list,
324 LDAP_STAILQ_NEXT( ps_list, o_next ) = NULL;
325 op->o_conn->c_n_ops_executing--;
326 op->o_conn->c_n_ops_completed++;
327 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
337 bdb_abandon( Operation *op, SlapReply *rs )
341 ps = bdb_drop_psearch( op, op->oq_abandon.rs_msgid );
343 if ( ps->o_tmpmemctx ) {
344 slap_sl_mem_destroy( NULL, ps->o_tmpmemctx );
349 return LDAP_UNAVAILABLE;
353 bdb_cancel( Operation *op, SlapReply *rs )
357 ps = bdb_drop_psearch( op, op->oq_cancel.rs_msgid );
359 rs->sr_err = LDAP_CANCELLED;
360 send_ldap_result( ps, rs );
361 if ( ps->o_tmpmemctx ) {
362 slap_sl_mem_destroy( NULL, ps->o_tmpmemctx );
367 return LDAP_UNAVAILABLE;
370 int bdb_search( Operation *op, SlapReply *rs )
372 return bdb_do_search( op, rs, op, NULL, 0 );
375 int bdb_psearch( Operation *op, SlapReply *rs, Operation *sop,
376 Entry *ps_e, int ps_type )
380 sop->o_private = op->o_private;
381 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
382 sop->o_private = NULL;
387 /* For persistent searches, op is the currently executing operation,
388 * sop is the persistent search. For regular searches, sop = op.
391 bdb_do_search( Operation *op, SlapReply *rs, Operation *sop,
392 Entry *ps_e, int ps_type )
394 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
397 ID candidates[BDB_IDL_UM_SIZE];
398 ID scopes[BDB_IDL_DB_SIZE];
399 Entry *e = NULL, base, e_root = {0};
400 Entry *matched = NULL;
401 EntryInfo *ei, ei_root = {0};
402 struct berval realbase = BER_BVNULL;
406 AttributeName *attrs;
408 Filter contextcsnand, contextcsnle, cookief, csnfnot,
409 csnfeq, csnfand, csnfge;
410 AttributeAssertion aa_ge, aa_eq, aa_le;
411 struct berval *search_context_csn = NULL;
413 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
415 AttributeName uuid_attr[2];
417 int entry_sync_state = -1;
418 AttributeName null_attr;
419 int no_sync_state_change = 0;
421 u_int32_t locker = 0;
425 int sync_send_present_mode = 1;
431 BerVarray syncUUID_set = NULL;
432 int syncUUID_set_cnt = 0;
434 struct bdb_op_info *opinfo = NULL;
438 LDAP_LOG( OPERATION, ENTRY, "bdb_search\n", 0, 0, 0 );
440 Debug( LDAP_DEBUG_TRACE, "=> bdb_search\n", 0, 0, 0);
442 attrs = sop->oq_search.rs_attrs;
444 opinfo = (struct bdb_op_info *) op->o_private;
446 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
447 struct slap_session_entry *sent;
448 if ( sop->o_sync_state.sid >= 0 ) {
449 LDAP_LIST_FOREACH( sent, &bdb->bi_session_list, se_link ) {
450 if ( sent->se_id == sop->o_sync_state.sid ) {
451 sop->o_sync_slog_size = sent->se_size;
458 /* psearch needs to be registered before refresh begins */
459 /* psearch and refresh transmission is serialized in send_ldap_ber() */
460 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
461 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
462 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
463 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
465 } else if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST
466 && sop->o_sync_slog_size >= 0 )
468 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
469 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
470 if ( ps_list->o_sync_slog_size >= 0 ) {
471 if ( ps_list->o_sync_state.sid == sop->o_sync_state.sid ) {
479 if ( ps_list->o_sync_slog_omitcsn.bv_len != 0 ) {
480 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
481 if ( sop->o_sync_state.ctxcsn &&
482 sop->o_sync_state.ctxcsn->bv_val != NULL )
484 value_match( &match, slap_schema.si_ad_entryCSN, mr,
485 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
486 sop->o_sync_state.ctxcsn,
487 &ps_list->o_sync_slog_omitcsn,
493 sync_send_present_mode = 0;
496 sync_send_present_mode = 0;
498 } else if ( sop->o_sync_slog_size >= 0 ) {
499 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
501 sop->o_sync_state.sid = -1;
503 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
506 null_attr.an_desc = NULL;
507 null_attr.an_oc = NULL;
508 null_attr.an_oc_exclude = 0;
509 BER_BVZERO( &null_attr.an_name );
511 for( num_ctrls = 0; num_ctrls < SLAP_MAX_RESPONSE_CONTROLS; num_ctrls++ ) {
512 ctrls[num_ctrls] = NULL;
516 if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
518 attrs[0].an_desc = NULL;
519 attrs[0].an_oc = NULL;
520 attrs[0].an_oc_exclude = 0;
521 BER_BVZERO( &attrs[0].an_name );
524 manageDSAit = get_manageDSAit( sop );
526 /* Sync control overrides manageDSAit */
527 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
528 if ( manageDSAit == SLAP_NO_CONTROL ) {
529 manageDSAit = SLAP_CRITICAL_CONTROL;
531 } else if ( IS_PSEARCH ) {
532 if ( manageDSAit == SLAP_NO_CONTROL ) {
533 manageDSAit = SLAP_CRITICAL_CONTROL;
537 if ( opinfo && opinfo->boi_txn ) {
538 ltid = opinfo->boi_txn;
539 locker = TXN_ID( ltid );
541 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
547 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
552 if ( sop->o_req_ndn.bv_len == 0 ) {
553 /* DIT root special case */
554 ei_root.bei_e = &e_root;
555 ei_root.bei_parent = &ei_root;
556 e_root.e_private = &ei_root;
558 BER_BVSTR( &e_root.e_nname, "" );
559 BER_BVSTR( &e_root.e_name, "" );
561 rs->sr_err = LDAP_SUCCESS;
564 /* get entry with reader lock */
565 rs->sr_err = bdb_dn2entry( op, ltid, &sop->o_req_ndn, &ei,
577 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
579 LOCK_ID_FREE (bdb->bi_dbenv, locker );
581 case DB_LOCK_DEADLOCK:
582 case DB_LOCK_NOTGRANTED:
585 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
587 LOCK_ID_FREE (bdb->bi_dbenv, locker );
591 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
592 BDB_IDL_ZERO(candidates);
593 e = deref_base( op, rs, e, &matched, locker, &lock,
598 struct berval matched_dn = BER_BVNULL;
600 if ( matched != NULL ) {
602 ber_dupbv( &matched_dn, &matched->e_name );
604 erefs = is_entry_referral( matched )
605 ? get_entry_referrals( op, matched )
608 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
613 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
614 &sop->o_req_dn, sop->oq_search.rs_scope );
615 ber_bvarray_free( erefs );
619 rs->sr_ref = referral_rewrite( default_referral,
620 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
623 rs->sr_err = LDAP_REFERRAL;
624 rs->sr_matched = matched_dn.bv_val;
625 send_ldap_result( sop, rs );
628 LOCK_ID_FREE (bdb->bi_dbenv, locker );
630 ber_bvarray_free( rs->sr_ref );
633 if ( matched_dn.bv_val ) {
634 ber_memfree( matched_dn.bv_val );
635 rs->sr_matched = NULL;
640 if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
641 /* entry is a referral, don't allow add */
642 struct berval matched_dn;
645 ber_dupbv( &matched_dn, &e->e_name );
646 erefs = get_entry_referrals( op, e );
648 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
652 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
653 &sop->o_req_dn, sop->oq_search.rs_scope );
654 ber_bvarray_free( erefs );
658 LDAP_LOG ( OPERATION, RESULTS,
659 "bdb_search: entry is referral\n", 0, 0, 0 );
661 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
665 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
666 rs->sr_err = LDAP_REFERRAL;
667 rs->sr_matched = matched_dn.bv_val;
668 send_ldap_result( sop, rs );
671 LOCK_ID_FREE (bdb->bi_dbenv, locker );
672 ber_bvarray_free( rs->sr_ref );
674 ber_memfree( matched_dn.bv_val );
675 rs->sr_matched = NULL;
679 if ( get_assert( op ) &&
680 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
682 rs->sr_err = LDAP_ASSERTION_FAILED;
683 send_ldap_result( sop, rs );
687 /* compute it anyway; root does not use it */
688 stoptime = op->o_time + sop->ors_tlimit;
690 /* need normalized dn below */
691 ber_dupbv( &realbase, &e->e_nname );
693 /* Copy info to base, must free entry before accessing the database
694 * in search_candidates, to avoid deadlocks.
696 base.e_private = e->e_private;
697 base.e_nname = realbase;
700 if ( e != &e_root ) {
701 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
706 rs->sr_err = bdb_get_commit_csn( sop, rs, &search_context_csn,
707 locker, &ctxcsn_lock );
709 if ( rs->sr_err != LDAP_SUCCESS ) {
710 send_ldap_error( sop, rs, rs->sr_err,
711 "error in csn management in search" );
715 if ( sop->o_sync_mode != SLAP_SYNC_NONE &&
716 sop->o_sync_state.ctxcsn &&
717 sop->o_sync_state.ctxcsn->bv_val &&
718 ber_bvcmp( &sop->o_sync_state.ctxcsn[0], search_context_csn ) == 0 )
720 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
724 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );
727 /* select candidates */
728 if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
729 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
732 BDB_IDL_ZERO( candidates );
733 BDB_IDL_ZERO( scopes );
734 rs->sr_err = search_candidates( op, sop, rs, &base,
735 locker, candidates, scopes );
738 if ( !IS_PSEARCH && sop->o_sync_mode != SLAP_SYNC_NONE ) {
739 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
742 /* start cursor at beginning of candidates.
746 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
747 cursor = bdb_idl_search( candidates, ps_e->e_id );
748 if ( candidates[cursor] != ps_e->e_id ) {
749 rs->sr_err = LDAP_SUCCESS;
752 } else if ( ps_e->e_id < BDB_IDL_RANGE_FIRST( candidates ) ||
753 ps_e->e_id > BDB_IDL_RANGE_LAST( candidates ))
755 rs->sr_err = LDAP_SUCCESS;
759 candidates[1] = ps_e->e_id;
762 if ( candidates[0] == 0 ) {
764 LDAP_LOG ( OPERATION, RESULTS,
765 "bdb_search: no candidates\n", 0, 0, 0 );
767 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
774 /* if not root and candidates exceed to-be-checked entries, abort */
775 if ( sop->ors_limit /* isroot == FALSE */ &&
776 sop->ors_limit->lms_s_unchecked != -1 &&
777 BDB_IDL_N(candidates) > (unsigned) sop->ors_limit->lms_s_unchecked )
779 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
780 send_ldap_result( sop, rs );
781 rs->sr_err = LDAP_SUCCESS;
785 if ( sop->ors_limit == NULL /* isroot == TRUE */ ||
786 !sop->ors_limit->lms_s_pr_hide )
788 tentries = BDB_IDL_N(candidates);
791 if ( get_pagedresults( sop ) > SLAP_NO_CONTROL ) {
792 /* deferred cookie parsing */
793 rs->sr_err = parse_paged_cookie( sop, rs );
794 if ( rs->sr_err != LDAP_SUCCESS ) {
795 send_ldap_result( sop, rs );
799 if ( (ID)( sop->o_pagedresults_state.ps_cookie ) == 0 ) {
800 id = bdb_idl_first( candidates, &cursor );
803 if ( sop->o_pagedresults_size == 0 ) {
804 rs->sr_err = LDAP_SUCCESS;
805 rs->sr_text = "search abandoned by pagedResult size=0";
806 send_ldap_result( sop, rs );
809 for ( id = bdb_idl_first( candidates, &cursor );
811 id <= (ID)( sop->o_pagedresults_state.ps_cookie );
812 id = bdb_idl_next( candidates, &cursor ) )
818 if ( cursor == NOID ) {
820 LDAP_LOG ( OPERATION, RESULTS,
821 "bdb_search: no paged results candidates\n",
824 Debug( LDAP_DEBUG_TRACE,
825 "bdb_search: no paged results candidates\n",
828 send_paged_response( sop, rs, &lastid, 0 );
830 rs->sr_err = LDAP_OTHER;
836 if (( sop->o_sync_mode & SLAP_SYNC_REFRESH ) || IS_PSEARCH ) {
839 cookief.f_choice = LDAP_FILTER_AND;
840 cookief.f_and = &csnfnot;
841 cookief.f_next = NULL;
843 csnfnot.f_choice = LDAP_FILTER_NOT;
844 csnfnot.f_not = &csnfeq;
845 csnfnot.f_next = &csnfand;
847 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
848 csnfeq.f_ava = &aa_eq;
849 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
850 if ( sop->o_sync_state.ctxcsn != NULL ) {
851 csnfeq.f_av_value = *sop->o_sync_state.ctxcsn;
853 csnfeq.f_av_value = slap_empty_bv;
856 csnfand.f_choice = LDAP_FILTER_AND;
857 csnfand.f_and = &csnfge;
858 csnfand.f_next = NULL;
860 csnfge.f_choice = LDAP_FILTER_GE;
861 csnfge.f_ava = &aa_ge;
862 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
863 if ( sop->o_sync_state.ctxcsn != NULL ) {
864 csnfge.f_av_value = *sop->o_sync_state.ctxcsn;
866 csnfge.f_av_value = slap_empty_bv;
869 if ( search_context_csn && !IS_PSEARCH ) {
870 csnfge.f_next = &contextcsnand;
872 contextcsnand.f_choice = LDAP_FILTER_AND;
873 contextcsnand.f_and = &contextcsnle;
874 contextcsnand.f_next = NULL;
876 contextcsnle.f_choice = LDAP_FILTER_LE;
877 contextcsnle.f_ava = &aa_le;
878 contextcsnle.f_av_desc = slap_schema.si_ad_entryCSN;
879 contextcsnle.f_av_value = *search_context_csn;
880 contextcsnle.f_next = sop->oq_search.rs_filter;
882 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
883 if ( sop->o_sync_state.ctxcsn &&
884 sop->o_sync_state.ctxcsn->bv_val != NULL )
886 value_match( &match, slap_schema.si_ad_entryCSN, mr,
887 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
888 &sop->o_sync_state.ctxcsn[0], search_context_csn,
893 no_sync_state_change = ( match >= 0 );
895 csnfge.f_next = sop->oq_search.rs_filter;
899 for ( id = bdb_idl_first( candidates, &cursor );
900 id != NOID && !no_sync_state_change;
901 id = bdb_idl_next( candidates, &cursor ) )
906 /* check for abandon */
907 if ( sop->o_abandon ) {
909 bdb_drop_psearch( sop, sop->o_msgid );
911 rs->sr_err = LDAP_SUCCESS;
915 if ( sop->o_cancel ) {
916 assert( sop->o_cancel == SLAP_CANCEL_REQ );
917 rs->sr_err = LDAP_CANCELLED;
918 send_ldap_result( sop, rs );
919 sop->o_cancel = SLAP_CANCEL_ACK;
920 rs->sr_err = LDAP_SUCCESS;
924 /* check time limit */
925 if ( sop->ors_tlimit != SLAP_NO_LIMIT
926 && slap_get_time() > stoptime )
928 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
929 rs->sr_ref = rs->sr_v2ref;
930 send_ldap_result( sop, rs );
931 rs->sr_err = LDAP_SUCCESS;
937 /* get the entry with reader lock */
939 rs->sr_err = bdb_cache_find_id( op, ltid,
940 id, &ei, 0, locker, &lock );
942 if (rs->sr_err == LDAP_BUSY) {
943 rs->sr_text = "ldap server busy";
944 send_ldap_result( sop, rs );
947 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
948 || rs->sr_err == DB_LOCK_NOTGRANTED )
953 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
960 if( !BDB_IDL_IS_RANGE(candidates) ) {
961 /* only complain for non-range IDLs */
963 LDAP_LOG ( OPERATION, RESULTS,
964 "bdb_search: candidate %ld not found\n",
967 Debug( LDAP_DEBUG_TRACE,
968 "bdb_search: candidate %ld not found\n",
981 #ifdef BDB_SUBENTRIES
982 /* FIXME: send all but syncrepl */
984 if ( !is_sync_protocol( sop ) )
987 if ( is_entry_subentry( e ) ) {
988 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
989 if(!get_subentries_visibility( sop )) {
990 /* only subentries are visible */
994 } else if ( get_subentries( sop ) &&
995 !get_subentries_visibility( sop ))
997 /* only subentries are visible */
1001 } else if ( get_subentries_visibility( sop )) {
1002 /* only subentries are visible */
1006 #endif /* BDB_SUBENTRIES */
1008 /* Does this candidate actually satisfy the search scope?
1010 * Note that we don't lock access to the bei_parent pointer.
1011 * Since only leaf nodes can be deleted, the parent of any
1012 * node will always be a valid node. Also since we have
1013 * a Read lock on the data, it cannot be renamed out of the
1014 * scope while we are looking at it, and unless we're using
1015 * BDB_HIER, its parents cannot be moved either.
1017 switch( sop->ors_scope ) {
1018 case LDAP_SCOPE_BASE:
1019 /* This is always true, yes? */
1020 if ( id == base.e_id ) scopeok = 1;
1023 case LDAP_SCOPE_ONELEVEL:
1024 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
1027 #ifdef LDAP_SCOPE_CHILDREN
1028 case LDAP_SCOPE_CHILDREN:
1029 if ( id == base.e_id ) break;
1032 case LDAP_SCOPE_SUBTREE: {
1034 for (tmp = BEI(e); tmp;
1035 tmp = tmp->bei_parent ) {
1036 if ( tmp->bei_id == base.e_id ) {
1045 /* aliases were already dereferenced in candidate list */
1046 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
1047 /* but if the search base is an alias, and we didn't
1048 * deref it when finding, return it.
1050 if ( is_entry_alias(e) &&
1051 ((sop->ors_deref & LDAP_DEREF_FINDING) ||
1052 !bvmatch(&e->e_nname, &op->o_req_ndn)))
1057 /* scopes is only non-empty for onelevel or subtree */
1058 if ( !scopeok && BDB_IDL_N(scopes) ) {
1060 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
1061 x = bdb_idl_search( scopes, e->e_id );
1062 if ( scopes[x] == e->e_id ) scopeok = 1;
1064 /* subtree, walk up the tree */
1065 EntryInfo *tmp = BEI(e);
1066 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
1067 x = bdb_idl_search( scopes, tmp->bei_id );
1068 if ( scopes[x] == tmp->bei_id ) {
1078 /* Not in scope, ignore it */
1081 LDAP_LOG ( OPERATION, RESULTS,
1082 "bdb_search: %ld scope not okay\n",
1085 Debug( LDAP_DEBUG_TRACE,
1086 "bdb_search: %ld scope not okay\n",
1093 * if it's a referral, add it to the list of referrals. only do
1094 * this for non-base searches, and don't check the filter
1095 * explicitly here since it's only a candidate anyway.
1097 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1098 && is_entry_referral( e ) )
1100 BerVarray erefs = get_entry_referrals( sop, e );
1101 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1102 sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1103 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1105 send_search_reference( sop, rs );
1107 ber_bvarray_free( rs->sr_ref );
1108 ber_bvarray_free( erefs );
1114 if ( !manageDSAit && is_entry_glue( e )) {
1118 /* if it matches the filter and scope, send it */
1120 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1121 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1123 rs->sr_err = LDAP_COMPARE_TRUE;
1127 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1128 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1129 rs->sr_err = test_filter( sop, rs->sr_entry, &contextcsnand );
1130 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1131 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1132 if ( no_sync_state_change ) {
1134 LDAP_LOG ( OPERATION, RESULTS,
1135 "bdb_search: error in context csn management\n",
1138 Debug( LDAP_DEBUG_TRACE,
1139 "bdb_search: error in context csn management\n",
1143 entry_sync_state = LDAP_SYNC_ADD;
1146 if ( no_sync_state_change ) {
1149 entry_sync_state = LDAP_SYNC_PRESENT;
1154 rs->sr_err = test_filter( sop,
1155 rs->sr_entry, sop->oq_search.rs_filter );
1159 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1160 /* check size limit */
1161 if ( --sop->ors_slimit == -1 &&
1162 sop->o_sync_slog_size == -1 )
1165 bdb_cache_return_entry_r( bdb->bi_dbenv,
1166 &bdb->bi_cache, e, &lock );
1169 rs->sr_entry = NULL;
1170 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1171 rs->sr_ref = rs->sr_v2ref;
1172 send_ldap_result( sop, rs );
1173 rs->sr_err = LDAP_SUCCESS;
1177 if ( get_pagedresults(sop) > SLAP_NO_CONTROL ) {
1178 if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
1179 send_paged_response( sop, rs, &lastid, tentries );
1190 int premodify_found = 0;
1191 int entry_sync_state;
1193 if ( ps_type == LDAP_PSEARCH_BY_ADD ||
1194 ps_type == LDAP_PSEARCH_BY_DELETE ||
1195 ps_type == LDAP_PSEARCH_BY_MODIFY ||
1196 ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1198 if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1199 struct psid_entry* psid_e;
1200 LDAP_LIST_FOREACH( psid_e,
1201 &op->o_pm_list, ps_link)
1203 if( psid_e->ps_op == sop ) {
1204 premodify_found = 1;
1205 LDAP_LIST_REMOVE(psid_e, ps_link);
1209 if (psid_e != NULL) free (psid_e);
1212 if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1213 entry_sync_state = LDAP_SYNC_ADD;
1214 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1215 entry_sync_state = LDAP_SYNC_DELETE;
1216 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1217 if ( premodify_found ) {
1218 entry_sync_state = LDAP_SYNC_MODIFY;
1220 entry_sync_state = LDAP_SYNC_ADD;
1222 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT ) {
1223 entry_sync_state = LDAP_SYNC_DELETE;
1225 rs->sr_err = LDAP_OTHER;
1229 if ( sop->o_sync_slog_size != -1 ) {
1230 if ( entry_sync_state == LDAP_SYNC_DELETE ) {
1231 result = slap_add_session_log( op, sop, e );
1236 struct berval cookie;
1237 slap_compose_sync_cookie( sop, &cookie,
1239 sop->o_sync_state.sid,
1240 sop->o_sync_state.rid );
1241 rs->sr_err = slap_build_sync_state_ctrl( sop,
1242 rs, e, entry_sync_state, ctrls,
1243 num_ctrls++, 1, &cookie );
1244 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1245 rs->sr_attrs = attrs;
1246 rs->sr_operational_attrs = NULL;
1247 rs->sr_ctrls = ctrls;
1249 result = send_search_entry( sop, rs );
1250 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1252 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1254 slap_sl_free( ctrls[--num_ctrls],
1256 ctrls[num_ctrls] = NULL;
1257 rs->sr_ctrls = NULL;
1260 } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1261 struct psid_entry* psid_e;
1262 psid_e = (struct psid_entry *) ch_calloc(1,
1263 sizeof(struct psid_entry));
1264 psid_e->ps_op = sop;
1265 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1270 LDAP_LOG ( OPERATION, RESULTS,
1271 "bdb_search: invalid ps_type (%d) \n",
1274 Debug( LDAP_DEBUG_TRACE,
1275 "bdb_search: invalid ps_type (%d) \n",
1281 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1282 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1283 rs->sr_err = slap_build_sync_state_ctrl( sop,
1284 rs, e, entry_sync_state, ctrls,
1285 num_ctrls++, 0, NULL );
1286 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1287 rs->sr_ctrls = ctrls;
1288 rs->sr_attrs = sop->oq_search.rs_attrs;
1289 rs->sr_operational_attrs = NULL;
1291 result = send_search_entry( sop, rs );
1293 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1295 slap_sl_free( ctrls[--num_ctrls],
1297 ctrls[num_ctrls] = NULL;
1298 rs->sr_ctrls = NULL;
1300 } else { /* PRESENT */
1301 if ( sync_send_present_mode ) {
1302 result = slap_build_syncUUID_set( sop,
1304 if ( result <= 0 ) {
1308 if ( syncUUID_set_cnt ==
1309 SLAP_SYNCUUID_SET_SIZE )
1311 rs->sr_err = LDAP_SUCCESS;
1312 rs->sr_rspoid = LDAP_SYNC_INFO;
1313 rs->sr_ctrls = NULL;
1314 result = slap_send_syncinfo( sop, rs,
1315 LDAP_TAG_SYNC_ID_SET,
1316 NULL, 0, syncUUID_set, 0 );
1317 if ( result != LDAP_SUCCESS ) {
1320 ber_bvarray_free_x( syncUUID_set,
1322 syncUUID_set = NULL;
1323 syncUUID_set_cnt = 0;
1333 rs->sr_attrs = sop->oq_search.rs_attrs;
1334 rs->sr_operational_attrs = NULL;
1335 rs->sr_ctrls = NULL;
1337 rs->sr_err = LDAP_SUCCESS;
1338 result = send_search_entry( sop, rs );
1343 case 0: /* entry sent ok */
1345 case 1: /* entry not sent */
1347 case -1: /* connection closed */
1349 bdb_cache_return_entry_r(bdb->bi_dbenv,
1350 &bdb->bi_cache, e, &lock);
1353 rs->sr_entry = NULL;
1354 rs->sr_err = LDAP_OTHER;
1361 LDAP_LOG ( OPERATION, RESULTS,
1362 "bdb_search: %ld does not match filter\n", (long) id, 0, 0);
1364 Debug( LDAP_DEBUG_TRACE,
1365 "bdb_search: %ld does not match filter\n",
1372 /* free reader lock */
1374 bdb_cache_return_entry_r( bdb->bi_dbenv,
1375 &bdb->bi_cache, e , &lock );
1376 if ( sop->o_nocaching ) {
1377 bdb_cache_delete_entry( bdb, ei, locker, &lock );
1381 rs->sr_entry = NULL;
1384 ldap_pvt_thread_yield();
1387 if ( syncUUID_set_cnt > 0 ) {
1388 rs->sr_err = LDAP_SUCCESS;
1389 rs->sr_rspoid = LDAP_SYNC_INFO;
1390 rs->sr_ctrls = NULL;
1391 slap_send_syncinfo( sop, rs, LDAP_TAG_SYNC_ID_SET,
1392 NULL, 0, syncUUID_set, 0 );
1393 ber_bvarray_free_x( syncUUID_set, sop->o_tmpmemctx );
1394 syncUUID_set_cnt = 0;
1399 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1400 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1401 struct berval cookie;
1402 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1403 sop->o_sync_state.sid, sop->o_sync_state.rid );
1405 if ( sync_send_present_mode ) {
1406 rs->sr_err = LDAP_SUCCESS;
1407 rs->sr_rspoid = LDAP_SYNC_INFO;
1408 rs->sr_ctrls = NULL;
1409 slap_send_syncinfo( sop, rs,
1410 LDAP_TAG_SYNC_REFRESH_PRESENT, &cookie, 1, NULL, 0 );
1413 if ( !no_sync_state_change ) {
1415 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1416 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1419 if ( ps_list->o_sync_slog_size > 0 ) {
1420 if ( ps_list->o_sync_state.sid ==
1421 sop->o_sync_state.sid )
1430 rs->sr_err = LDAP_SUCCESS;
1431 rs->sr_rspoid = NULL;
1432 rs->sr_ctrls = NULL;
1433 slap_send_session_log( op, ps_list, rs );
1435 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1438 rs->sr_err = LDAP_SUCCESS;
1439 rs->sr_rspoid = LDAP_SYNC_INFO;
1440 rs->sr_ctrls = NULL;
1441 slap_send_syncinfo( sop, rs,
1442 LDAP_TAG_SYNC_REFRESH_DELETE, &cookie, 1, NULL, 0 );
1445 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1448 /* refreshOnly mode */
1449 struct berval cookie;
1450 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1451 sop->o_sync_state.sid, sop->o_sync_state.rid );
1453 if ( sync_send_present_mode ) {
1454 slap_build_sync_done_ctrl( sop, rs, ctrls,
1455 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1458 if ( !no_sync_state_change ) {
1460 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1461 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1464 if ( ps_list->o_sync_slog_size > 0 ) {
1465 if ( ps_list->o_sync_state.sid ==
1466 sop->o_sync_state.sid ) {
1474 slap_send_session_log( op, ps_list, rs );
1476 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1479 slap_build_sync_done_ctrl( sop, rs, ctrls,
1480 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_DELETES );
1483 rs->sr_ctrls = ctrls;
1484 rs->sr_ref = rs->sr_v2ref;
1485 rs->sr_err = (rs->sr_v2ref == NULL)
1486 ? 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 slap_sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val,
1493 slap_sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1494 ctrls[num_ctrls] = NULL;
1495 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1499 rs->sr_ctrls = NULL;
1500 rs->sr_ref = rs->sr_v2ref;
1501 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1502 rs->sr_rspoid = NULL;
1503 if ( get_pagedresults(sop) > SLAP_NO_CONTROL ) {
1504 send_paged_response( sop, rs, NULL, 0 );
1506 send_ldap_result( sop, rs );
1511 rs->sr_err = LDAP_SUCCESS;
1514 if( !IS_PSEARCH && e != NULL ) {
1515 /* free reader lock */
1516 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1520 LOCK_ID_FREE( bdb->bi_dbenv, locker );
1522 ber_bvfree( search_context_csn );
1524 if( rs->sr_v2ref ) {
1525 ber_bvarray_free( rs->sr_v2ref );
1526 rs->sr_v2ref = NULL;
1528 if( realbase.bv_val ) ch_free( realbase.bv_val );
1534 static int base_candidate(
1540 LDAP_LOG ( OPERATION, ENTRY,
1541 "base_candidate: base: \"%s\" (0x%08lx)\n",
1542 e->e_nname.bv_val, (long) e->e_id, 0);
1544 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1545 e->e_nname.bv_val, (long) e->e_id, 0);
1553 /* Look for "objectClass Present" in this filter.
1554 * Also count depth of filter tree while we're at it.
1556 static int oc_filter(
1565 if( cur > *max ) *max = cur;
1567 switch( f->f_choice ) {
1568 case LDAP_FILTER_PRESENT:
1569 if (f->f_desc == slap_schema.si_ad_objectClass) {
1574 case LDAP_FILTER_AND:
1575 case LDAP_FILTER_OR:
1577 for ( f=f->f_and; f; f=f->f_next ) {
1578 (void) oc_filter(f, cur, max);
1588 static void search_stack_free( void *key, void *data )
1590 ber_memfree_x(data, NULL);
1593 static void *search_stack( Operation *op )
1595 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1598 if ( op->o_threadctx ) {
1599 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1602 ret = bdb->bi_search_stack;
1606 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1608 if ( op->o_threadctx ) {
1609 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1610 ret, search_stack_free );
1612 bdb->bi_search_stack = ret;
1618 static int search_candidates(
1627 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1629 Filter f, rf, xf, nf;
1631 AttributeAssertion aa_ref;
1632 #ifdef BDB_SUBENTRIES
1634 AttributeAssertion aa_subentry;
1638 * This routine takes as input a filter (user-filter)
1639 * and rewrites it as follows:
1640 * (&(scope=DN)[(objectClass=subentry)]
1641 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1645 LDAP_LOG ( OPERATION, ENTRY,
1646 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1647 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope);
1649 Debug(LDAP_DEBUG_TRACE,
1650 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1651 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1654 xf.f_or = op->oq_search.rs_filter;
1655 xf.f_choice = LDAP_FILTER_OR;
1658 /* If the user's filter uses objectClass=*,
1659 * these clauses are redundant.
1661 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1662 && !get_subentries_visibility(op)
1663 && !is_sync_protocol(op) )
1665 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1666 /* match referral objects */
1667 struct berval bv_ref = BER_BVC( "referral" );
1668 rf.f_choice = LDAP_FILTER_EQUALITY;
1670 rf.f_av_desc = slap_schema.si_ad_objectClass;
1671 rf.f_av_value = bv_ref;
1672 rf.f_next = xf.f_or;
1679 f.f_choice = LDAP_FILTER_AND;
1681 /* Dummy; we compute scope separately now */
1682 nf.f_choice = SLAPD_FILTER_COMPUTED;
1683 nf.f_result = LDAP_SUCCESS;
1684 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1685 ? op->oq_search.rs_filter : &xf ;
1686 /* Filter depth increased again, adding dummy clause */
1689 #ifdef BDB_SUBENTRIES
1690 if( get_subentries_visibility( op ) ) {
1691 struct berval bv_subentry = BER_BVC( "SUBENTRY" );
1692 sf.f_choice = LDAP_FILTER_EQUALITY;
1693 sf.f_ava = &aa_subentry;
1694 sf.f_av_desc = slap_schema.si_ad_objectClass;
1695 sf.f_av_value = bv_subentry;
1696 sf.f_next = nf.f_next;
1701 /* Allocate IDL stack, plus 1 more for former tmp */
1702 if ( depth+1 > bdb->bi_search_stack_depth ) {
1703 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1705 stack = search_stack( stackop );
1708 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1709 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1711 rc = bdb_dn2idl( op, e, ids, stack );
1714 if ( rc == LDAP_SUCCESS ) {
1715 rc = bdb_filter_candidates( op, &f, ids,
1716 stack, stack+BDB_IDL_UM_SIZE );
1719 if ( depth+1 > bdb->bi_search_stack_depth ) {
1725 LDAP_LOG ( OPERATION, DETAIL1,
1726 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
1728 Debug(LDAP_DEBUG_TRACE,
1729 "bdb_search_candidates: failed (rc=%d)\n",
1735 LDAP_LOG ( OPERATION, DETAIL1,
1736 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1737 (long) ids[0], (long) BDB_IDL_FIRST(ids),
1738 (long) BDB_IDL_LAST(ids));
1740 Debug(LDAP_DEBUG_TRACE,
1741 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1743 (long) BDB_IDL_FIRST(ids),
1744 (long) BDB_IDL_LAST(ids) );
1752 parse_paged_cookie( Operation *op, SlapReply *rs )
1755 int rc = LDAP_SUCCESS;
1759 struct berval cookie = BER_BVNULL;
1761 /* this function must be invoked only if the pagedResults
1762 * control has been detected, parsed and partially checked
1763 * by the frontend */
1764 assert( get_pagedresults( op ) > SLAP_NO_CONTROL );
1766 /* look for the appropriate ctrl structure */
1767 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
1768 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
1774 if ( c[0] == NULL ) {
1775 rs->sr_text = "missing pagedResults control";
1776 return LDAP_PROTOCOL_ERROR;
1779 /* Already tested by frontend */
1780 assert( c[0]->ldctl_value.bv_len > 0 );
1782 if ( c[0]->ldctl_value.bv_len == 0 ) {
1783 rs->sr_text = "paged results control value is empty (or absent)";
1784 return LDAP_PROTOCOL_ERROR;
1788 /* Parse the control value
1789 * realSearchControlValue ::= SEQUENCE {
1790 * size INTEGER (0..maxInt),
1791 * -- requested page size from client
1792 * -- result set size estimate from server
1793 * cookie OCTET STRING
1796 ber = ber_init( &c[0]->ldctl_value );
1797 if ( ber == NULL ) {
1798 rs->sr_text = "internal error";
1802 tag = ber_scanf( ber, "{im}", &size, &cookie );
1804 /* Already tested by frontend */
1805 assert( tag != LBER_ERROR );
1807 if ( tag == LBER_ERROR ) {
1808 rs->sr_text = "paged results control could not be decoded";
1809 rc = LDAP_PROTOCOL_ERROR;
1814 /* Already tested by frontend */
1815 assert( size >= 0 );
1818 rs->sr_text = "paged results control size invalid";
1819 rc = LDAP_PROTOCOL_ERROR;
1824 /* cookie decoding/checks deferred to backend... */
1825 if ( cookie.bv_len ) {
1826 PagedResultsCookie reqcookie;
1827 if( cookie.bv_len != sizeof( reqcookie ) ) {
1829 rs->sr_text = "paged results cookie is invalid";
1830 rc = LDAP_PROTOCOL_ERROR;
1834 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
1836 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
1838 rs->sr_text = "paged results cookie is invalid";
1839 rc = LDAP_PROTOCOL_ERROR;
1842 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
1843 rs->sr_text = "paged results cookie is invalid or old";
1844 rc = LDAP_UNWILLING_TO_PERFORM;
1849 /* Initial request. Initialize state. */
1851 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
1852 /* There's another pagedResults control on the
1853 * same connection; reject new pagedResults controls
1854 * (allowed by RFC2696) */
1855 rs->sr_text = "paged results cookie unavailable; try later";
1856 rc = LDAP_UNWILLING_TO_PERFORM;
1860 op->o_pagedresults_state.ps_cookie = 0;
1861 op->o_pagedresults_state.ps_count = 0;
1865 (void)ber_free( ber, 1 );
1871 send_paged_response(
1877 LDAPControl ctrl, *ctrls[2];
1878 BerElementBuffer berbuf;
1879 BerElement *ber = (BerElement *)&berbuf;
1880 PagedResultsCookie respcookie;
1881 struct berval cookie;
1884 LDAP_LOG ( OPERATION, ENTRY,
1885 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1886 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1888 Debug(LDAP_DEBUG_ARGS,
1889 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1890 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1893 BER_BVZERO( &ctrl.ldctl_value );
1897 ber_init2( ber, NULL, LBER_USE_DER );
1900 respcookie = ( PagedResultsCookie )(*lastid);
1901 cookie.bv_len = sizeof( respcookie );
1902 cookie.bv_val = (char *)&respcookie;
1905 respcookie = ( PagedResultsCookie )0;
1906 BER_BVSTR( &cookie, "" );
1909 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1910 op->o_conn->c_pagedresults_state.ps_count =
1911 op->o_pagedresults_state.ps_count + rs->sr_nentries;
1913 /* return size of 0 -- no estimate */
1914 ber_printf( ber, "{iO}", 0, &cookie );
1916 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1920 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1921 ctrls[0]->ldctl_iscritical = 0;
1923 rs->sr_ctrls = ctrls;
1924 rs->sr_err = LDAP_SUCCESS;
1925 send_ldap_result( op, rs );
1926 rs->sr_ctrls = NULL;
1929 (void) ber_free_buf( ber );