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 );
344 if ( ps->o_tmpmemctx ) {
345 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 );
362 if ( ps->o_tmpmemctx ) {
363 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;
437 Debug( LDAP_DEBUG_TRACE, "=> bdb_search\n", 0, 0, 0);
438 attrs = sop->oq_search.rs_attrs;
440 opinfo = (struct bdb_op_info *) op->o_private;
442 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
443 struct slap_session_entry *sent;
444 if ( sop->o_sync_state.sid >= 0 ) {
445 LDAP_LIST_FOREACH( sent, &bdb->bi_session_list, se_link ) {
446 if ( sent->se_id == sop->o_sync_state.sid ) {
447 sop->o_sync_slog_size = sent->se_size;
454 /* psearch needs to be registered before refresh begins */
455 /* psearch and refresh transmission is serialized in send_ldap_ber() */
456 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
457 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
458 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
459 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
461 } else if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST
462 && sop->o_sync_slog_size >= 0 )
464 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
465 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
466 if ( ps_list->o_sync_slog_size >= 0 ) {
467 if ( ps_list->o_sync_state.sid == sop->o_sync_state.sid ) {
475 if ( ps_list->o_sync_slog_omitcsn.bv_len != 0 ) {
476 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
477 if ( sop->o_sync_state.ctxcsn &&
478 sop->o_sync_state.ctxcsn->bv_val != NULL )
480 value_match( &match, slap_schema.si_ad_entryCSN, mr,
481 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
482 sop->o_sync_state.ctxcsn,
483 &ps_list->o_sync_slog_omitcsn,
489 sync_send_present_mode = 0;
492 sync_send_present_mode = 0;
494 } else if ( sop->o_sync_slog_size >= 0 ) {
495 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
497 sop->o_sync_state.sid = -1;
499 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
502 null_attr.an_desc = NULL;
503 null_attr.an_oc = NULL;
504 null_attr.an_oc_exclude = 0;
505 BER_BVZERO( &null_attr.an_name );
507 for( num_ctrls = 0; num_ctrls < SLAP_MAX_RESPONSE_CONTROLS; num_ctrls++ ) {
508 ctrls[num_ctrls] = NULL;
512 if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
514 attrs[0].an_desc = NULL;
515 attrs[0].an_oc = NULL;
516 attrs[0].an_oc_exclude = 0;
517 BER_BVZERO( &attrs[0].an_name );
520 manageDSAit = get_manageDSAit( sop );
522 /* Sync control overrides manageDSAit */
523 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
524 if ( manageDSAit == SLAP_NO_CONTROL ) {
525 manageDSAit = SLAP_CRITICAL_CONTROL;
527 } else if ( IS_PSEARCH ) {
528 if ( manageDSAit == SLAP_NO_CONTROL ) {
529 manageDSAit = SLAP_CRITICAL_CONTROL;
533 if ( opinfo && opinfo->boi_txn ) {
534 ltid = opinfo->boi_txn;
535 locker = TXN_ID( ltid );
537 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
543 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
548 if ( sop->o_req_ndn.bv_len == 0 ) {
549 /* DIT root special case */
550 ei_root.bei_e = &e_root;
551 ei_root.bei_parent = &ei_root;
552 e_root.e_private = &ei_root;
554 BER_BVSTR( &e_root.e_nname, "" );
555 BER_BVSTR( &e_root.e_name, "" );
557 rs->sr_err = LDAP_SUCCESS;
560 /* get entry with reader lock */
561 rs->sr_err = bdb_dn2entry( op, ltid, &sop->o_req_ndn, &ei,
573 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
575 LOCK_ID_FREE (bdb->bi_dbenv, locker );
577 case DB_LOCK_DEADLOCK:
578 case DB_LOCK_NOTGRANTED:
581 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
583 LOCK_ID_FREE (bdb->bi_dbenv, locker );
587 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
588 BDB_IDL_ZERO(candidates);
589 e = deref_base( op, rs, e, &matched, locker, &lock,
594 struct berval matched_dn = BER_BVNULL;
596 if ( matched != NULL ) {
598 ber_dupbv( &matched_dn, &matched->e_name );
600 erefs = is_entry_referral( matched )
601 ? get_entry_referrals( op, matched )
604 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
609 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
610 &sop->o_req_dn, sop->oq_search.rs_scope );
611 ber_bvarray_free( erefs );
615 rs->sr_ref = referral_rewrite( SLAPD_GLOBAL(default_referral),
616 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
619 rs->sr_err = LDAP_REFERRAL;
620 rs->sr_matched = matched_dn.bv_val;
621 send_ldap_result( sop, rs );
624 LOCK_ID_FREE (bdb->bi_dbenv, locker );
626 ber_bvarray_free( rs->sr_ref );
629 if ( matched_dn.bv_val ) {
630 ber_memfree( matched_dn.bv_val );
631 rs->sr_matched = NULL;
636 if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
637 /* entry is a referral, don't allow add */
638 struct berval matched_dn;
641 ber_dupbv( &matched_dn, &e->e_name );
642 erefs = get_entry_referrals( op, e );
644 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
648 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
649 &sop->o_req_dn, sop->oq_search.rs_scope );
650 ber_bvarray_free( erefs );
653 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
656 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
657 rs->sr_err = LDAP_REFERRAL;
658 rs->sr_matched = matched_dn.bv_val;
659 send_ldap_result( sop, rs );
662 LOCK_ID_FREE (bdb->bi_dbenv, locker );
663 ber_bvarray_free( rs->sr_ref );
665 ber_memfree( matched_dn.bv_val );
666 rs->sr_matched = NULL;
670 if ( get_assert( op ) &&
671 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
673 rs->sr_err = LDAP_ASSERTION_FAILED;
674 send_ldap_result( sop, rs );
678 /* compute it anyway; root does not use it */
679 stoptime = op->o_time + sop->ors_tlimit;
681 /* need normalized dn below */
682 ber_dupbv( &realbase, &e->e_nname );
684 /* Copy info to base, must free entry before accessing the database
685 * in search_candidates, to avoid deadlocks.
687 base.e_private = e->e_private;
688 base.e_nname = realbase;
691 if ( e != &e_root ) {
692 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
697 rs->sr_err = bdb_get_commit_csn( sop, rs, &search_context_csn,
698 locker, &ctxcsn_lock );
700 if ( rs->sr_err != LDAP_SUCCESS ) {
701 send_ldap_error( sop, rs, rs->sr_err,
702 "error in csn management in search" );
706 if ( sop->o_sync_mode != SLAP_SYNC_NONE &&
707 sop->o_sync_state.ctxcsn &&
708 sop->o_sync_state.ctxcsn->bv_val &&
709 ber_bvcmp( &sop->o_sync_state.ctxcsn[0], search_context_csn ) == 0 )
711 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
715 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );
718 /* select candidates */
719 if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
720 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
723 BDB_IDL_ZERO( candidates );
724 BDB_IDL_ZERO( scopes );
725 rs->sr_err = search_candidates( op, sop, rs, &base,
726 locker, candidates, scopes );
729 if ( !IS_PSEARCH && sop->o_sync_mode != SLAP_SYNC_NONE ) {
730 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
733 /* start cursor at beginning of candidates.
737 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
738 cursor = bdb_idl_search( candidates, ps_e->e_id );
739 if ( candidates[cursor] != ps_e->e_id ) {
740 rs->sr_err = LDAP_SUCCESS;
743 } else if ( ps_e->e_id < BDB_IDL_RANGE_FIRST( candidates ) ||
744 ps_e->e_id > BDB_IDL_RANGE_LAST( candidates ))
746 rs->sr_err = LDAP_SUCCESS;
750 candidates[1] = ps_e->e_id;
753 if ( candidates[0] == 0 ) {
754 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
760 /* if not root and candidates exceed to-be-checked entries, abort */
761 if ( sop->ors_limit /* isroot == FALSE */ &&
762 sop->ors_limit->lms_s_unchecked != -1 &&
763 BDB_IDL_N(candidates) > (unsigned) sop->ors_limit->lms_s_unchecked )
765 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
766 send_ldap_result( sop, rs );
767 rs->sr_err = LDAP_SUCCESS;
771 if ( sop->ors_limit == NULL /* isroot == TRUE */ ||
772 !sop->ors_limit->lms_s_pr_hide )
774 tentries = BDB_IDL_N(candidates);
777 if ( get_pagedresults( sop ) > SLAP_NO_CONTROL ) {
778 /* deferred cookie parsing */
779 rs->sr_err = parse_paged_cookie( sop, rs );
780 if ( rs->sr_err != LDAP_SUCCESS ) {
781 send_ldap_result( sop, rs );
785 if ( (ID)( sop->o_pagedresults_state.ps_cookie ) == 0 ) {
786 id = bdb_idl_first( candidates, &cursor );
789 if ( sop->o_pagedresults_size == 0 ) {
790 rs->sr_err = LDAP_SUCCESS;
791 rs->sr_text = "search abandoned by pagedResult size=0";
792 send_ldap_result( sop, rs );
795 for ( id = bdb_idl_first( candidates, &cursor );
797 id <= (ID)( sop->o_pagedresults_state.ps_cookie );
798 id = bdb_idl_next( candidates, &cursor ) )
804 if ( cursor == NOID ) {
805 Debug( LDAP_DEBUG_TRACE,
806 "bdb_search: no paged results candidates\n",
808 send_paged_response( sop, rs, &lastid, 0 );
810 rs->sr_err = LDAP_OTHER;
816 if (( sop->o_sync_mode & SLAP_SYNC_REFRESH ) || IS_PSEARCH ) {
819 cookief.f_choice = LDAP_FILTER_AND;
820 cookief.f_and = &csnfnot;
821 cookief.f_next = NULL;
823 csnfnot.f_choice = LDAP_FILTER_NOT;
824 csnfnot.f_not = &csnfeq;
825 csnfnot.f_next = &csnfand;
827 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
828 csnfeq.f_ava = &aa_eq;
829 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
830 if ( sop->o_sync_state.ctxcsn != NULL ) {
831 csnfeq.f_av_value = *sop->o_sync_state.ctxcsn;
833 csnfeq.f_av_value = slap_empty_bv;
836 csnfand.f_choice = LDAP_FILTER_AND;
837 csnfand.f_and = &csnfge;
838 csnfand.f_next = NULL;
840 csnfge.f_choice = LDAP_FILTER_GE;
841 csnfge.f_ava = &aa_ge;
842 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
843 if ( sop->o_sync_state.ctxcsn != NULL ) {
844 csnfge.f_av_value = *sop->o_sync_state.ctxcsn;
846 csnfge.f_av_value = slap_empty_bv;
849 if ( search_context_csn && !IS_PSEARCH ) {
850 csnfge.f_next = &contextcsnand;
852 contextcsnand.f_choice = LDAP_FILTER_AND;
853 contextcsnand.f_and = &contextcsnle;
854 contextcsnand.f_next = NULL;
856 contextcsnle.f_choice = LDAP_FILTER_LE;
857 contextcsnle.f_ava = &aa_le;
858 contextcsnle.f_av_desc = slap_schema.si_ad_entryCSN;
859 contextcsnle.f_av_value = *search_context_csn;
860 contextcsnle.f_next = sop->oq_search.rs_filter;
862 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
863 if ( sop->o_sync_state.ctxcsn &&
864 sop->o_sync_state.ctxcsn->bv_val != NULL )
866 value_match( &match, slap_schema.si_ad_entryCSN, mr,
867 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
868 &sop->o_sync_state.ctxcsn[0], search_context_csn,
873 no_sync_state_change = ( match >= 0 );
875 csnfge.f_next = sop->oq_search.rs_filter;
879 for ( id = bdb_idl_first( candidates, &cursor );
880 id != NOID && !no_sync_state_change;
881 id = bdb_idl_next( candidates, &cursor ) )
886 /* check for abandon */
887 if ( sop->o_abandon ) {
889 bdb_drop_psearch( sop, sop->o_msgid );
891 rs->sr_err = LDAP_SUCCESS;
895 if ( sop->o_cancel ) {
896 assert( sop->o_cancel == SLAP_CANCEL_REQ );
897 rs->sr_err = LDAP_CANCELLED;
898 send_ldap_result( sop, rs );
899 sop->o_cancel = SLAP_CANCEL_ACK;
900 rs->sr_err = LDAP_SUCCESS;
904 /* check time limit */
905 if ( sop->ors_tlimit != SLAP_NO_LIMIT
906 && slap_get_time() > stoptime )
908 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
909 rs->sr_ref = rs->sr_v2ref;
910 send_ldap_result( sop, rs );
911 rs->sr_err = LDAP_SUCCESS;
917 /* get the entry with reader lock */
919 rs->sr_err = bdb_cache_find_id( op, ltid,
920 id, &ei, 0, locker, &lock );
922 if (rs->sr_err == LDAP_BUSY) {
923 rs->sr_text = "ldap server busy";
924 send_ldap_result( sop, rs );
927 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
928 || rs->sr_err == DB_LOCK_NOTGRANTED )
933 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
940 if( !BDB_IDL_IS_RANGE(candidates) ) {
941 /* only complain for non-range IDLs */
942 Debug( LDAP_DEBUG_TRACE,
943 "bdb_search: candidate %ld not found\n",
955 #ifdef BDB_SUBENTRIES
956 /* FIXME: send all but syncrepl */
958 if ( !is_sync_protocol( sop ) )
961 if ( is_entry_subentry( e ) ) {
962 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
963 if(!get_subentries_visibility( sop )) {
964 /* only subentries are visible */
968 } else if ( get_subentries( sop ) &&
969 !get_subentries_visibility( sop ))
971 /* only subentries are visible */
975 } else if ( get_subentries_visibility( sop )) {
976 /* only subentries are visible */
980 #endif /* BDB_SUBENTRIES */
982 /* Does this candidate actually satisfy the search scope?
984 * Note that we don't lock access to the bei_parent pointer.
985 * Since only leaf nodes can be deleted, the parent of any
986 * node will always be a valid node. Also since we have
987 * a Read lock on the data, it cannot be renamed out of the
988 * scope while we are looking at it, and unless we're using
989 * BDB_HIER, its parents cannot be moved either.
991 switch( sop->ors_scope ) {
992 case LDAP_SCOPE_BASE:
993 /* This is always true, yes? */
994 if ( id == base.e_id ) scopeok = 1;
997 case LDAP_SCOPE_ONELEVEL:
998 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
1001 #ifdef LDAP_SCOPE_CHILDREN
1002 case LDAP_SCOPE_CHILDREN:
1003 if ( id == base.e_id ) break;
1006 case LDAP_SCOPE_SUBTREE: {
1008 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
1009 if ( tmp->bei_id == base.e_id ) {
1018 /* aliases were already dereferenced in candidate list */
1019 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
1020 /* but if the search base is an alias, and we didn't
1021 * deref it when finding, return it.
1023 if ( is_entry_alias(e) &&
1024 ((sop->ors_deref & LDAP_DEREF_FINDING) ||
1025 !bvmatch(&e->e_nname, &op->o_req_ndn)))
1030 /* scopes is only non-empty for onelevel or subtree */
1031 if ( !scopeok && BDB_IDL_N(scopes) ) {
1033 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
1034 x = bdb_idl_search( scopes, e->e_id );
1035 if ( scopes[x] == e->e_id ) scopeok = 1;
1037 /* subtree, walk up the tree */
1038 EntryInfo *tmp = BEI(e);
1039 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
1040 x = bdb_idl_search( scopes, tmp->bei_id );
1041 if ( scopes[x] == tmp->bei_id ) {
1051 /* Not in scope, ignore it */
1053 Debug( LDAP_DEBUG_TRACE,
1054 "bdb_search: %ld scope not okay\n",
1060 * if it's a referral, add it to the list of referrals. only do
1061 * this for non-base searches, and don't check the filter
1062 * explicitly here since it's only a candidate anyway.
1064 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1065 && is_entry_referral( e ) )
1067 BerVarray erefs = get_entry_referrals( sop, e );
1068 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1069 sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1070 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1072 send_search_reference( sop, rs );
1074 ber_bvarray_free( rs->sr_ref );
1075 ber_bvarray_free( erefs );
1081 if ( !manageDSAit && is_entry_glue( e )) {
1085 /* if it matches the filter and scope, send it */
1087 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1088 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1090 rs->sr_err = LDAP_COMPARE_TRUE;
1094 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1095 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1096 rs->sr_err = test_filter( sop, rs->sr_entry, &contextcsnand );
1097 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1098 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1099 if ( no_sync_state_change ) {
1100 Debug( LDAP_DEBUG_TRACE,
1101 "bdb_search: error in context csn management\n",
1104 entry_sync_state = LDAP_SYNC_ADD;
1107 if ( no_sync_state_change ) {
1110 entry_sync_state = LDAP_SYNC_PRESENT;
1115 rs->sr_err = test_filter( sop,
1116 rs->sr_entry, sop->oq_search.rs_filter );
1120 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1121 /* check size limit */
1122 if ( --sop->ors_slimit == -1 &&
1123 sop->o_sync_slog_size == -1 )
1126 bdb_cache_return_entry_r( bdb->bi_dbenv,
1127 &bdb->bi_cache, e, &lock );
1130 rs->sr_entry = NULL;
1131 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1132 rs->sr_ref = rs->sr_v2ref;
1133 send_ldap_result( sop, rs );
1134 rs->sr_err = LDAP_SUCCESS;
1138 if ( get_pagedresults(sop) > SLAP_NO_CONTROL ) {
1139 if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
1140 send_paged_response( sop, rs, &lastid, tentries );
1151 int premodify_found = 0;
1152 int entry_sync_state;
1154 if ( ps_type == LDAP_PSEARCH_BY_ADD ||
1155 ps_type == LDAP_PSEARCH_BY_DELETE ||
1156 ps_type == LDAP_PSEARCH_BY_MODIFY ||
1157 ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1159 if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1160 struct psid_entry* psid_e;
1161 LDAP_LIST_FOREACH( psid_e,
1162 &op->o_pm_list, ps_link)
1164 if( psid_e->ps_op == sop ) {
1165 premodify_found = 1;
1166 LDAP_LIST_REMOVE(psid_e, ps_link);
1170 if (psid_e != NULL) free (psid_e);
1173 if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1174 entry_sync_state = LDAP_SYNC_ADD;
1175 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1176 entry_sync_state = LDAP_SYNC_DELETE;
1177 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1178 if ( premodify_found ) {
1179 entry_sync_state = LDAP_SYNC_MODIFY;
1181 entry_sync_state = LDAP_SYNC_ADD;
1183 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT ) {
1184 entry_sync_state = LDAP_SYNC_DELETE;
1186 rs->sr_err = LDAP_OTHER;
1190 if ( sop->o_sync_slog_size != -1 ) {
1191 if ( entry_sync_state == LDAP_SYNC_DELETE ) {
1192 result = slap_add_session_log( op, sop, e );
1197 struct berval cookie;
1198 slap_compose_sync_cookie( sop, &cookie,
1200 sop->o_sync_state.sid,
1201 sop->o_sync_state.rid );
1202 rs->sr_err = slap_build_sync_state_ctrl( sop,
1203 rs, e, entry_sync_state, ctrls,
1204 num_ctrls++, 1, &cookie );
1205 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1206 rs->sr_attrs = attrs;
1207 rs->sr_operational_attrs = NULL;
1208 rs->sr_ctrls = ctrls;
1210 result = send_search_entry( sop, rs );
1211 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1213 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1215 slap_sl_free( ctrls[--num_ctrls],
1217 ctrls[num_ctrls] = NULL;
1218 rs->sr_ctrls = NULL;
1221 } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1222 struct psid_entry* psid_e;
1223 psid_e = (struct psid_entry *) ch_calloc(1,
1224 sizeof(struct psid_entry));
1225 psid_e->ps_op = sop;
1226 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1230 Debug( LDAP_DEBUG_TRACE,
1231 "bdb_search: invalid ps_type (%d) \n",
1236 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1237 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1238 rs->sr_err = slap_build_sync_state_ctrl( sop,
1239 rs, e, entry_sync_state, ctrls,
1240 num_ctrls++, 0, NULL );
1241 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1242 rs->sr_ctrls = ctrls;
1243 rs->sr_attrs = sop->oq_search.rs_attrs;
1244 rs->sr_operational_attrs = NULL;
1246 result = send_search_entry( sop, rs );
1248 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1250 slap_sl_free( ctrls[--num_ctrls],
1252 ctrls[num_ctrls] = NULL;
1253 rs->sr_ctrls = NULL;
1255 } else { /* PRESENT */
1256 if ( sync_send_present_mode ) {
1257 result = slap_build_syncUUID_set( sop,
1259 if ( result <= 0 ) {
1263 if ( syncUUID_set_cnt ==
1264 SLAP_SYNCUUID_SET_SIZE )
1266 rs->sr_err = LDAP_SUCCESS;
1267 rs->sr_rspoid = LDAP_SYNC_INFO;
1268 rs->sr_ctrls = NULL;
1269 result = slap_send_syncinfo( sop, rs,
1270 LDAP_TAG_SYNC_ID_SET,
1271 NULL, 0, syncUUID_set, 0 );
1272 if ( result != LDAP_SUCCESS ) {
1275 ber_bvarray_free_x( syncUUID_set,
1277 syncUUID_set = NULL;
1278 syncUUID_set_cnt = 0;
1288 rs->sr_attrs = sop->oq_search.rs_attrs;
1289 rs->sr_operational_attrs = NULL;
1290 rs->sr_ctrls = NULL;
1292 rs->sr_err = LDAP_SUCCESS;
1293 result = send_search_entry( sop, rs );
1298 case 0: /* entry sent ok */
1300 case 1: /* entry not sent */
1302 case -1: /* connection closed */
1304 bdb_cache_return_entry_r(bdb->bi_dbenv,
1305 &bdb->bi_cache, e, &lock);
1308 rs->sr_entry = NULL;
1309 rs->sr_err = LDAP_OTHER;
1315 Debug( LDAP_DEBUG_TRACE,
1316 "bdb_search: %ld does not match filter\n",
1322 /* free reader lock */
1324 bdb_cache_return_entry_r( bdb->bi_dbenv,
1325 &bdb->bi_cache, e , &lock );
1326 if ( sop->o_nocaching ) {
1327 bdb_cache_delete_entry( bdb, ei, locker, &lock );
1331 rs->sr_entry = NULL;
1334 ldap_pvt_thread_yield();
1337 if ( syncUUID_set_cnt > 0 ) {
1338 rs->sr_err = LDAP_SUCCESS;
1339 rs->sr_rspoid = LDAP_SYNC_INFO;
1340 rs->sr_ctrls = NULL;
1341 slap_send_syncinfo( sop, rs, LDAP_TAG_SYNC_ID_SET,
1342 NULL, 0, syncUUID_set, 0 );
1343 ber_bvarray_free_x( syncUUID_set, sop->o_tmpmemctx );
1344 syncUUID_set_cnt = 0;
1349 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1350 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1351 struct berval cookie;
1352 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1353 sop->o_sync_state.sid, sop->o_sync_state.rid );
1355 if ( sync_send_present_mode ) {
1356 rs->sr_err = LDAP_SUCCESS;
1357 rs->sr_rspoid = LDAP_SYNC_INFO;
1358 rs->sr_ctrls = NULL;
1359 slap_send_syncinfo( sop, rs,
1360 LDAP_TAG_SYNC_REFRESH_PRESENT, &cookie, 1, NULL, 0 );
1363 if ( !no_sync_state_change ) {
1365 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1366 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1369 if ( ps_list->o_sync_slog_size > 0 ) {
1370 if ( ps_list->o_sync_state.sid ==
1371 sop->o_sync_state.sid )
1380 rs->sr_err = LDAP_SUCCESS;
1381 rs->sr_rspoid = NULL;
1382 rs->sr_ctrls = NULL;
1383 slap_send_session_log( op, ps_list, rs );
1385 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1388 rs->sr_err = LDAP_SUCCESS;
1389 rs->sr_rspoid = LDAP_SYNC_INFO;
1390 rs->sr_ctrls = NULL;
1391 slap_send_syncinfo( sop, rs,
1392 LDAP_TAG_SYNC_REFRESH_DELETE, &cookie, 1, NULL, 0 );
1395 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1398 /* refreshOnly mode */
1399 struct berval cookie;
1400 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1401 sop->o_sync_state.sid, sop->o_sync_state.rid );
1403 if ( sync_send_present_mode ) {
1404 slap_build_sync_done_ctrl( sop, rs, ctrls,
1405 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1408 if ( !no_sync_state_change ) {
1410 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1411 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1414 if ( ps_list->o_sync_slog_size > 0 ) {
1415 if ( ps_list->o_sync_state.sid ==
1416 sop->o_sync_state.sid ) {
1424 slap_send_session_log( op, ps_list, rs );
1426 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1429 slap_build_sync_done_ctrl( sop, rs, ctrls,
1430 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_DELETES );
1433 rs->sr_ctrls = ctrls;
1434 rs->sr_ref = rs->sr_v2ref;
1435 rs->sr_err = (rs->sr_v2ref == NULL)
1436 ? LDAP_SUCCESS : LDAP_REFERRAL;
1437 rs->sr_rspoid = NULL;
1438 send_ldap_result( sop, rs );
1439 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1440 slap_sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val,
1443 slap_sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1444 ctrls[num_ctrls] = NULL;
1445 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1449 rs->sr_ctrls = NULL;
1450 rs->sr_ref = rs->sr_v2ref;
1451 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1452 rs->sr_rspoid = NULL;
1453 if ( get_pagedresults(sop) > SLAP_NO_CONTROL ) {
1454 send_paged_response( sop, rs, NULL, 0 );
1456 send_ldap_result( sop, rs );
1461 rs->sr_err = LDAP_SUCCESS;
1464 if( !IS_PSEARCH && e != NULL ) {
1465 /* free reader lock */
1466 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1470 LOCK_ID_FREE( bdb->bi_dbenv, locker );
1472 ber_bvfree( search_context_csn );
1474 if( rs->sr_v2ref ) {
1475 ber_bvarray_free( rs->sr_v2ref );
1476 rs->sr_v2ref = NULL;
1478 if( realbase.bv_val ) ch_free( realbase.bv_val );
1484 static int base_candidate(
1489 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1490 e->e_nname.bv_val, (long) e->e_id, 0);
1497 /* Look for "objectClass Present" in this filter.
1498 * Also count depth of filter tree while we're at it.
1500 static int oc_filter(
1509 if( cur > *max ) *max = cur;
1511 switch( f->f_choice ) {
1512 case LDAP_FILTER_PRESENT:
1513 if (f->f_desc == slap_schema.si_ad_objectClass) {
1518 case LDAP_FILTER_AND:
1519 case LDAP_FILTER_OR:
1521 for ( f=f->f_and; f; f=f->f_next ) {
1522 (void) oc_filter(f, cur, max);
1532 static void search_stack_free( void *key, void *data )
1534 ber_memfree_x(data, NULL);
1537 static void *search_stack( Operation *op )
1539 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1542 if ( op->o_threadctx ) {
1543 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1546 ret = bdb->bi_search_stack;
1550 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1552 if ( op->o_threadctx ) {
1553 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1554 ret, search_stack_free );
1556 bdb->bi_search_stack = ret;
1562 static int search_candidates(
1571 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1573 Filter f, rf, xf, nf;
1575 AttributeAssertion aa_ref;
1576 #ifdef BDB_SUBENTRIES
1578 AttributeAssertion aa_subentry;
1582 * This routine takes as input a filter (user-filter)
1583 * and rewrites it as follows:
1584 * (&(scope=DN)[(objectClass=subentry)]
1585 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1588 Debug(LDAP_DEBUG_TRACE,
1589 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1590 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1592 xf.f_or = op->oq_search.rs_filter;
1593 xf.f_choice = LDAP_FILTER_OR;
1596 /* If the user's filter uses objectClass=*,
1597 * these clauses are redundant.
1599 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1600 && !get_subentries_visibility(op)
1601 && !is_sync_protocol(op) )
1603 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1604 /* match referral objects */
1605 struct berval bv_ref = BER_BVC( "referral" );
1606 rf.f_choice = LDAP_FILTER_EQUALITY;
1608 rf.f_av_desc = slap_schema.si_ad_objectClass;
1609 rf.f_av_value = bv_ref;
1610 rf.f_next = xf.f_or;
1617 f.f_choice = LDAP_FILTER_AND;
1619 /* Dummy; we compute scope separately now */
1620 nf.f_choice = SLAPD_FILTER_COMPUTED;
1621 nf.f_result = LDAP_SUCCESS;
1622 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1623 ? op->oq_search.rs_filter : &xf ;
1624 /* Filter depth increased again, adding dummy clause */
1627 #ifdef BDB_SUBENTRIES
1628 if( get_subentries_visibility( op ) ) {
1629 struct berval bv_subentry = BER_BVC( "SUBENTRY" );
1630 sf.f_choice = LDAP_FILTER_EQUALITY;
1631 sf.f_ava = &aa_subentry;
1632 sf.f_av_desc = slap_schema.si_ad_objectClass;
1633 sf.f_av_value = bv_subentry;
1634 sf.f_next = nf.f_next;
1639 /* Allocate IDL stack, plus 1 more for former tmp */
1640 if ( depth+1 > bdb->bi_search_stack_depth ) {
1641 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1643 stack = search_stack( stackop );
1646 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1647 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1649 rc = bdb_dn2idl( op, e, ids, stack );
1652 if ( rc == LDAP_SUCCESS ) {
1653 rc = bdb_filter_candidates( op, &f, ids,
1654 stack, stack+BDB_IDL_UM_SIZE );
1657 if ( depth+1 > bdb->bi_search_stack_depth ) {
1662 Debug(LDAP_DEBUG_TRACE,
1663 "bdb_search_candidates: failed (rc=%d)\n",
1667 Debug(LDAP_DEBUG_TRACE,
1668 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1670 (long) BDB_IDL_FIRST(ids),
1671 (long) BDB_IDL_LAST(ids) );
1678 parse_paged_cookie( Operation *op, SlapReply *rs )
1681 int rc = LDAP_SUCCESS;
1685 struct berval cookie = BER_BVNULL;
1687 /* this function must be invoked only if the pagedResults
1688 * control has been detected, parsed and partially checked
1689 * by the frontend */
1690 assert( get_pagedresults( op ) > SLAP_NO_CONTROL );
1692 /* look for the appropriate ctrl structure */
1693 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
1694 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
1700 if ( c[0] == NULL ) {
1701 rs->sr_text = "missing pagedResults control";
1702 return LDAP_PROTOCOL_ERROR;
1705 /* Already tested by frontend */
1706 assert( c[0]->ldctl_value.bv_len > 0 );
1708 if ( c[0]->ldctl_value.bv_len == 0 ) {
1709 rs->sr_text = "paged results control value is empty (or absent)";
1710 return LDAP_PROTOCOL_ERROR;
1714 /* Parse the control value
1715 * realSearchControlValue ::= SEQUENCE {
1716 * size INTEGER (0..maxInt),
1717 * -- requested page size from client
1718 * -- result set size estimate from server
1719 * cookie OCTET STRING
1722 ber = ber_init( &c[0]->ldctl_value );
1723 if ( ber == NULL ) {
1724 rs->sr_text = "internal error";
1728 tag = ber_scanf( ber, "{im}", &size, &cookie );
1730 /* Already tested by frontend */
1731 assert( tag != LBER_ERROR );
1733 if ( tag == LBER_ERROR ) {
1734 rs->sr_text = "paged results control could not be decoded";
1735 rc = LDAP_PROTOCOL_ERROR;
1740 /* Already tested by frontend */
1741 assert( size >= 0 );
1744 rs->sr_text = "paged results control size invalid";
1745 rc = LDAP_PROTOCOL_ERROR;
1750 /* cookie decoding/checks deferred to backend... */
1751 if ( cookie.bv_len ) {
1752 PagedResultsCookie reqcookie;
1753 if( cookie.bv_len != sizeof( reqcookie ) ) {
1755 rs->sr_text = "paged results cookie is invalid";
1756 rc = LDAP_PROTOCOL_ERROR;
1760 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
1762 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
1764 rs->sr_text = "paged results cookie is invalid";
1765 rc = LDAP_PROTOCOL_ERROR;
1768 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
1769 rs->sr_text = "paged results cookie is invalid or old";
1770 rc = LDAP_UNWILLING_TO_PERFORM;
1775 /* Initial request. Initialize state. */
1777 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
1778 /* There's another pagedResults control on the
1779 * same connection; reject new pagedResults controls
1780 * (allowed by RFC2696) */
1781 rs->sr_text = "paged results cookie unavailable; try later";
1782 rc = LDAP_UNWILLING_TO_PERFORM;
1786 op->o_pagedresults_state.ps_cookie = 0;
1787 op->o_pagedresults_state.ps_count = 0;
1791 (void)ber_free( ber, 1 );
1797 send_paged_response(
1803 LDAPControl ctrl, *ctrls[2];
1804 BerElementBuffer berbuf;
1805 BerElement *ber = (BerElement *)&berbuf;
1806 PagedResultsCookie respcookie;
1807 struct berval cookie;
1809 Debug(LDAP_DEBUG_ARGS,
1810 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1811 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1813 BER_BVZERO( &ctrl.ldctl_value );
1817 ber_init2( ber, NULL, LBER_USE_DER );
1820 respcookie = ( PagedResultsCookie )(*lastid);
1821 cookie.bv_len = sizeof( respcookie );
1822 cookie.bv_val = (char *)&respcookie;
1825 respcookie = ( PagedResultsCookie )0;
1826 BER_BVSTR( &cookie, "" );
1829 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1830 op->o_conn->c_pagedresults_state.ps_count =
1831 op->o_pagedresults_state.ps_count + rs->sr_nentries;
1833 /* return size of 0 -- no estimate */
1834 ber_printf( ber, "{iO}", 0, &cookie );
1836 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1840 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1841 ctrls[0]->ldctl_iscritical = 0;
1843 rs->sr_ctrls = ctrls;
1844 rs->sr_err = LDAP_SUCCESS;
1845 send_ldap_result( op, rs );
1846 rs->sr_ctrls = NULL;
1849 (void) ber_free_buf( ber );