1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 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>
25 static int base_candidate(
30 static int search_candidates(
38 static int parse_paged_cookie( Operation *op, SlapReply *rs );
40 static void send_paged_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 (
60 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
61 rs->sr_text = "maximum deref depth exceeded";
64 /* Remember the last entry we looked at, so we can
69 if (MDB_IDL_N(tmp) >= op->o_bd->be_max_deref_depth) {
74 /* If this is part of a subtree or onelevel search,
75 * have we seen this ID before? If so, quit.
77 if ( visited && mdb_idl_insert( visited, e->e_id ) ) {
82 /* If we've seen this ID during this deref iteration,
85 if ( mdb_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 = mdb_dn2entry( op, txn, &ndn, &e, 0 );
102 rs->sr_err = LDAP_ALIAS_PROBLEM;
103 rs->sr_text = "aliasedObject not found";
107 /* Free the previous entry, continue to work with the
108 * one we just retrieved.
110 mdb_entry_return( *matched );
112 /* We found a regular entry. Return this to the caller.
114 if (!is_entry_alias(e)) {
115 rs->sr_err = LDAP_SUCCESS;
123 /* Look for and dereference all aliases within the search scope.
124 * Requires "stack" to be able to hold 6 levels of DB_SIZE IDLs.
125 * Of course we're hardcoded to require a minimum of 8 UM_SIZE
126 * IDLs so this is never a problem.
128 static int search_aliases(
136 ID *aliases, *curscop, *visited, *newsubs, *oldsubs, *tmp;
137 ID cursora, ida, cursoro, ido;
139 struct berval bv_alias = BER_BVC( "alias" );
140 AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
144 aliases = stack; /* IDL of all aliases in the database */
145 curscop = aliases + MDB_IDL_DB_SIZE; /* Aliases in the current scope */
146 visited = curscop + MDB_IDL_DB_SIZE; /* IDs we've seen in this search */
147 newsubs = visited + MDB_IDL_DB_SIZE; /* New subtrees we've added */
148 oldsubs = newsubs + MDB_IDL_DB_SIZE; /* Subtrees added previously */
149 tmp = oldsubs + MDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
151 af.f_choice = LDAP_FILTER_EQUALITY;
152 af.f_ava = &aa_alias;
153 af.f_av_desc = slap_schema.si_ad_objectClass;
154 af.f_av_value = bv_alias;
157 /* Find all aliases in database */
158 MDB_IDL_ZERO( aliases );
159 rs->sr_err = mdb_filter_candidates( op, txn, &af, aliases,
161 if (rs->sr_err != LDAP_SUCCESS) {
165 oldsubs[1] = e->e_id;
167 MDB_IDL_ZERO( visited );
168 MDB_IDL_ZERO( newsubs );
171 ido = mdb_idl_first( oldsubs, &cursoro );
174 /* Set curscop to only the aliases in the current scope. Start with
175 * all the aliases, then get the intersection with the scope.
177 rs->sr_err = mdb_idscope( op, txn, e->e_id, aliases, curscop );
182 mdb_entry_return( e );
185 /* Dereference all of the aliases in the current scope. */
187 for (ida = mdb_idl_first(curscop, &cursora); ida != NOID;
188 ida = mdb_idl_next(curscop, &cursora))
190 rs->sr_err = mdb_id2entry(op, txn, ida, &a);
191 if (rs->sr_err != LDAP_SUCCESS) {
195 /* This should only happen if the curscop IDL has maxed out and
196 * turned into a range that spans IDs indiscriminately
198 if (!is_entry_alias(a)) {
199 mdb_entry_return (a);
203 /* Actually dereference the alias */
205 a = deref_base( op, rs, a, &matched, txn,
208 /* If the target was not already in our current scopes,
209 * make note of it in the newsubs list.
211 if (mdb_idl_insert(scopes, a->e_id) == 0) {
212 mdb_idl_insert(newsubs, a->e_id);
214 mdb_entry_return( a );
216 } else if (matched) {
217 /* Alias could not be dereferenced, or it deref'd to
218 * an ID we've already seen. Ignore it.
220 mdb_entry_return( matched );
224 /* If this is a OneLevel search, we're done; oldsubs only had one
225 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
227 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
229 ido = mdb_idl_next( oldsubs, &cursoro );
231 /* If we're done processing the old scopes, did we add any new
232 * scopes in this iteration? If so, go back and do those now.
235 if (MDB_IDL_IS_ZERO(newsubs)) break;
236 MDB_IDL_CPY(oldsubs, newsubs);
237 MDB_IDL_ZERO(newsubs);
239 ido = mdb_idl_first( oldsubs, &cursoro );
242 /* Find the entry corresponding to the next scope. If it can't
243 * be found, ignore it and move on. This should never happen;
244 * we should never see the ID of an entry that doesn't exist.
246 rs->sr_err = mdb_id2entry(op, txn, ido, &e);
247 if ( rs->sr_err != LDAP_SUCCESS ) {
254 /* Get the next ID from the DB. Used if the candidate list is
255 * a range and simple iteration hits missing entryIDs
258 mdb_get_nextid(struct mdb_info *mdb, MDB_txn *txn, ID *cursor)
266 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &curs );
270 key.mv_size = sizeof(ID);
271 rc = mdb_cursor_get( curs, &key, NULL, MDB_SET_RANGE );
272 mdb_cursor_close( curs );
275 memcpy( cursor, key.mv_data, sizeof(ID));
280 mdb_search( Operation *op, SlapReply *rs )
282 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
285 ID candidates[MDB_IDL_UM_SIZE];
286 ID scopes[MDB_IDL_DB_SIZE];
287 Entry *e = NULL, *base = NULL;
288 Entry *matched = NULL;
289 AttributeName *attrs;
296 mdb_op_info opinfo = {0}, *moi = &opinfo;
297 MDB_txn *ltid = NULL;
299 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_search) "\n", 0, 0, 0);
300 attrs = op->oq_search.rs_attrs;
302 manageDSAit = get_manageDSAit( op );
304 rs->sr_err = mdb_opinfo_get( op, mdb, 1, &moi );
309 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
317 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
318 MDB_IDL_ZERO(candidates);
321 /* get entry with reader lock */
322 rs->sr_err = mdb_dn2entry( op, ltid, &op->o_req_ndn, &e, 1 );
332 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
335 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
339 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
340 if ( matched && is_entry_alias( matched )) {
343 stub.bv_val = op->o_req_ndn.bv_val;
344 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
345 e = deref_base( op, rs, matched, &matched, ltid,
348 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
350 mdb_entry_return (e);
354 } else if ( e && is_entry_alias( e )) {
355 e = deref_base( op, rs, e, &matched, ltid,
361 struct berval matched_dn = BER_BVNULL;
363 if ( matched != NULL ) {
364 BerVarray erefs = NULL;
366 /* return referral only if "disclose"
367 * is granted on the object */
368 if ( ! access_allowed( op, matched,
369 slap_schema.si_ad_entry,
370 NULL, ACL_DISCLOSE, NULL ) )
372 rs->sr_err = LDAP_NO_SUCH_OBJECT;
375 ber_dupbv( &matched_dn, &matched->e_name );
377 erefs = is_entry_referral( matched )
378 ? get_entry_referrals( op, matched )
380 if ( rs->sr_err == MDB_NOTFOUND )
381 rs->sr_err = LDAP_REFERRAL;
382 rs->sr_matched = matched_dn.bv_val;
385 mdb_entry_return (matched);
389 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
390 &op->o_req_dn, op->oq_search.rs_scope );
391 ber_bvarray_free( erefs );
395 rs->sr_ref = referral_rewrite( default_referral,
396 NULL, &op->o_req_dn, op->oq_search.rs_scope );
397 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
400 send_ldap_result( op, rs );
403 ber_bvarray_free( rs->sr_ref );
406 if ( !BER_BVISNULL( &matched_dn ) ) {
407 ber_memfree( matched_dn.bv_val );
408 rs->sr_matched = NULL;
413 /* NOTE: __NEW__ "search" access is required
414 * on searchBase object */
415 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
416 NULL, ACL_SEARCH, NULL, &mask ) )
418 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
419 rs->sr_err = LDAP_NO_SUCH_OBJECT;
421 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
425 send_ldap_result( op, rs );
429 if ( !manageDSAit && is_entry_referral( e ) ) {
430 /* entry is a referral */
431 struct berval matched_dn = BER_BVNULL;
432 BerVarray erefs = NULL;
434 ber_dupbv( &matched_dn, &e->e_name );
435 erefs = get_entry_referrals( op, e );
437 rs->sr_err = LDAP_REFERRAL;
439 mdb_entry_return( e );
443 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
444 &op->o_req_dn, op->oq_search.rs_scope );
445 ber_bvarray_free( erefs );
448 rs->sr_text = "bad_referral object";
452 Debug( LDAP_DEBUG_TRACE,
453 LDAP_XSTRING(mdb_search) ": entry is referral\n",
456 rs->sr_matched = matched_dn.bv_val;
457 send_ldap_result( op, rs );
459 ber_bvarray_free( rs->sr_ref );
461 ber_memfree( matched_dn.bv_val );
462 rs->sr_matched = NULL;
466 if ( get_assert( op ) &&
467 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
469 rs->sr_err = LDAP_ASSERTION_FAILED;
471 send_ldap_result( op, rs );
475 /* compute it anyway; root does not use it */
476 stoptime = op->o_time + op->ors_tlimit;
482 /* select candidates */
483 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
484 rs->sr_err = base_candidate( op->o_bd, base, candidates );
487 MDB_IDL_ZERO( candidates );
488 MDB_IDL_ZERO( scopes );
489 mdb_idl_insert( scopes, base->e_id );
490 rs->sr_err = search_candidates( op, rs, base,
491 ltid, candidates, scopes );
494 /* start cursor at beginning of candidates.
498 if ( candidates[0] == 0 ) {
499 Debug( LDAP_DEBUG_TRACE,
500 LDAP_XSTRING(mdb_search) ": no candidates\n",
506 /* if not root and candidates exceed to-be-checked entries, abort */
507 if ( op->ors_limit /* isroot == FALSE */ &&
508 op->ors_limit->lms_s_unchecked != -1 &&
509 MDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
511 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
512 send_ldap_result( op, rs );
513 rs->sr_err = LDAP_SUCCESS;
517 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
518 !op->ors_limit->lms_s_pr_hide )
520 tentries = MDB_IDL_N(candidates);
523 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
524 PagedResultsState *ps = op->o_pagedresults_state;
525 /* deferred cookie parsing */
526 rs->sr_err = parse_paged_cookie( op, rs );
527 if ( rs->sr_err != LDAP_SUCCESS ) {
528 send_ldap_result( op, rs );
532 cursor = (ID) ps->ps_cookie;
533 if ( cursor && ps->ps_size == 0 ) {
534 rs->sr_err = LDAP_SUCCESS;
535 rs->sr_text = "search abandoned by pagedResult size=0";
536 send_ldap_result( op, rs );
539 id = mdb_idl_first( candidates, &cursor );
541 Debug( LDAP_DEBUG_TRACE,
542 LDAP_XSTRING(mdb_search)
543 ": no paged results candidates\n",
545 send_paged_response( op, rs, &lastid, 0 );
547 rs->sr_err = LDAP_OTHER;
550 if ( id == (ID)ps->ps_cookie )
551 id = mdb_idl_next( candidates, &cursor );
557 for ( id = mdb_idl_first( candidates, &cursor );
558 id != NOID ; id = mdb_idl_next( candidates, &cursor ) )
564 /* check for abandon */
565 if ( op->o_abandon ) {
566 rs->sr_err = SLAPD_ABANDON;
567 send_ldap_result( op, rs );
571 /* mostly needed by internal searches,
572 * e.g. related to syncrepl, for whom
573 * abandon does not get set... */
574 if ( slapd_shutdown ) {
575 rs->sr_err = LDAP_UNAVAILABLE;
576 send_ldap_disconnect( op, rs );
580 /* check time limit */
581 if ( op->ors_tlimit != SLAP_NO_LIMIT
582 && slap_get_time() > stoptime )
584 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
585 rs->sr_ref = rs->sr_v2ref;
586 send_ldap_result( op, rs );
587 rs->sr_err = LDAP_SUCCESS;
591 if ( id == base->e_id ) {
596 rs->sr_err = mdb_id2entry( op, ltid, id, &e );
598 if (rs->sr_err == LDAP_BUSY) {
599 rs->sr_text = "ldap server busy";
600 send_ldap_result( op, rs );
603 } else if ( rs->sr_err == LDAP_OTHER ) {
604 rs->sr_text = "internal error";
605 send_ldap_result( op, rs );
610 if( !MDB_IDL_IS_RANGE(candidates) ) {
611 /* only complain for non-range IDLs */
612 Debug( LDAP_DEBUG_TRACE,
613 LDAP_XSTRING(mdb_search)
614 ": candidate %ld not found\n",
617 /* get the next ID from the DB */
618 rs->sr_err = mdb_get_nextid( mdb, ltid, &cursor );
619 if ( rs->sr_err == MDB_NOTFOUND ) {
623 rs->sr_err = LDAP_OTHER;
624 rs->sr_text = "internal error in get_nextid";
625 send_ldap_result( op, rs );
635 if ( is_entry_subentry( e ) ) {
636 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
637 if(!get_subentries_visibility( op )) {
638 /* only subentries are visible */
642 } else if ( get_subentries( op ) &&
643 !get_subentries_visibility( op ))
645 /* only subentries are visible */
649 } else if ( get_subentries_visibility( op )) {
650 /* only subentries are visible */
654 /* Does this candidate actually satisfy the search scope?
658 switch( op->ors_scope ) {
659 case LDAP_SCOPE_BASE:
660 /* This is always true, yes? */
661 if ( id == base->e_id ) scopeok = 1;
664 #ifdef LDAP_SCOPE_CHILDREN
665 case LDAP_SCOPE_CHILDREN:
666 if ( id == base->e_id ) break;
669 case LDAP_SCOPE_SUBTREE:
670 if ( id == base->e_id ) {
675 case LDAP_SCOPE_ONELEVEL:
677 if ( mdb_idscopes( op, &isc ) == MDB_SUCCESS ) scopeok = 1;
681 /* aliases were already dereferenced in candidate list */
682 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
683 /* but if the search base is an alias, and we didn't
684 * deref it when finding, return it.
686 if ( is_entry_alias(e) &&
687 ((op->ors_deref & LDAP_DEREF_FINDING) ||
688 !bvmatch(&e->e_nname, &op->o_req_ndn)))
694 /* Not in scope, ignore it */
697 Debug( LDAP_DEBUG_TRACE,
698 LDAP_XSTRING(mdb_search)
699 ": %ld scope not okay\n",
704 if ( !manageDSAit && is_entry_glue( e )) {
709 struct berval pdn, pndn;
712 /* child of base, just append RDNs to base->e_name */
713 if ( isc.nscope == 1 ) {
715 pndn = base->e_nname;
717 mdb_id2name( op, ltid, &isc.mc, scopes[isc.nscope], &pdn, &pndn );
719 e->e_name.bv_len = pdn.bv_len;
720 e->e_nname.bv_len = pndn.bv_len;
721 for (i=0; i<isc.numrdns; i++) {
722 e->e_name.bv_len += isc.rdns[i].bv_len + 1;
723 e->e_nname.bv_len += isc.nrdns[i].bv_len + 1;
725 e->e_name.bv_val = op->o_tmpalloc(e->e_name.bv_len + 1, op->o_tmpmemctx);
726 e->e_nname.bv_val = op->o_tmpalloc(e->e_nname.bv_len + 1, op->o_tmpmemctx);
727 d = e->e_name.bv_val;
728 n = e->e_nname.bv_val;
729 for (i=0; i<isc.numrdns; i++) {
730 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
731 d += isc.rdns[i].bv_len;
733 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
734 n += isc.nrdns[i].bv_len;
737 memcpy(d, pdn.bv_val, pdn.bv_len+1);
738 memcpy(n, pndn.bv_val, pndn.bv_len+1);
739 if (isc.nscope != 1) {
740 op->o_tmpfree(pndn.bv_val, op->o_tmpmemctx);
741 op->o_tmpfree(pdn.bv_val, op->o_tmpmemctx);
746 * if it's a referral, add it to the list of referrals. only do
747 * this for non-base searches, and don't check the filter
748 * explicitly here since it's only a candidate anyway.
750 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
751 && is_entry_referral( e ) )
753 BerVarray erefs = get_entry_referrals( op, e );
754 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
755 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
756 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
761 send_search_reference( op, rs );
763 mdb_entry_return( e );
767 ber_bvarray_free( rs->sr_ref );
768 ber_bvarray_free( erefs );
774 /* if it matches the filter and scope, send it */
775 rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
777 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
778 /* check size limit */
779 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
780 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
781 mdb_entry_return( e );
783 send_paged_response( op, rs, &lastid, tentries );
791 rs->sr_attrs = op->oq_search.rs_attrs;
792 rs->sr_operational_attrs = NULL;
795 RS_ASSERT( e->e_private != NULL );
797 rs->sr_err = LDAP_SUCCESS;
798 rs->sr_err = send_search_entry( op, rs );
802 mdb_entry_return( e );
805 switch ( rs->sr_err ) {
806 case LDAP_SUCCESS: /* entry sent ok */
808 default: /* entry not sent */
810 case LDAP_UNAVAILABLE:
811 case LDAP_SIZELIMIT_EXCEEDED:
812 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
813 rs->sr_ref = rs->sr_v2ref;
814 send_ldap_result( op, rs );
815 rs->sr_err = LDAP_SUCCESS;
818 rs->sr_err = LDAP_OTHER;
825 Debug( LDAP_DEBUG_TRACE,
826 LDAP_XSTRING(mdb_search)
827 ": %ld does not match filter\n",
834 mdb_entry_return( e );
835 RS_ASSERT( rs->sr_entry == NULL );
843 rs->sr_ref = rs->sr_v2ref;
844 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
845 rs->sr_rspoid = NULL;
846 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
847 send_paged_response( op, rs, NULL, 0 );
849 send_ldap_result( op, rs );
852 rs->sr_err = LDAP_SUCCESS;
855 if ( moi == &opinfo ) {
856 mdb_txn_reset( moi->moi_txn );
857 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
860 mdb_cursor_close( isc.mc );
862 ber_bvarray_free( rs->sr_v2ref );
866 mdb_entry_return(base);
872 static int base_candidate(
877 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
878 e->e_nname.bv_val, (long) e->e_id, 0);
885 /* Look for "objectClass Present" in this filter.
886 * Also count depth of filter tree while we're at it.
888 static int oc_filter(
897 if( cur > *max ) *max = cur;
899 switch( f->f_choice ) {
900 case LDAP_FILTER_PRESENT:
901 if (f->f_desc == slap_schema.si_ad_objectClass) {
906 case LDAP_FILTER_AND:
909 for ( f=f->f_and; f; f=f->f_next ) {
910 (void) oc_filter(f, cur, max);
920 static void search_stack_free( void *key, void *data )
922 ber_memfree_x(data, NULL);
925 static void *search_stack( Operation *op )
927 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
930 if ( op->o_threadctx ) {
931 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
934 ret = mdb->mi_search_stack;
938 ret = ch_malloc( mdb->mi_search_stack_depth * MDB_IDL_UM_SIZE
940 if ( op->o_threadctx ) {
941 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
942 ret, search_stack_free, NULL, NULL );
944 mdb->mi_search_stack = ret;
950 static int search_candidates(
958 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
960 Filter *f, rf, xf, nf, sf;
962 AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
963 AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
966 * This routine takes as input a filter (user-filter)
967 * and rewrites it as follows:
968 * (&(scope=DN)[(objectClass=subentry)]
969 * (|[(objectClass=referral)](user-filter))
972 Debug(LDAP_DEBUG_TRACE,
973 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
974 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
976 f = op->oq_search.rs_filter;
978 /* If the user's filter uses objectClass=*,
979 * these clauses are redundant.
981 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
982 && !get_subentries_visibility(op)) {
983 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
984 /* match referral objects */
985 struct berval bv_ref = BER_BVC( "referral" );
986 rf.f_choice = LDAP_FILTER_EQUALITY;
988 rf.f_av_desc = slap_schema.si_ad_objectClass;
989 rf.f_av_value = bv_ref;
992 xf.f_choice = LDAP_FILTER_OR;
999 if( get_subentries_visibility( op ) ) {
1000 struct berval bv_subentry = BER_BVC( "subentry" );
1001 sf.f_choice = LDAP_FILTER_EQUALITY;
1002 sf.f_ava = &aa_subentry;
1003 sf.f_av_desc = slap_schema.si_ad_objectClass;
1004 sf.f_av_value = bv_subentry;
1006 nf.f_choice = LDAP_FILTER_AND;
1013 /* Allocate IDL stack, plus 1 more for former tmp */
1014 if ( depth+1 > mdb->mi_search_stack_depth ) {
1015 stack = ch_malloc( (depth + 1) * MDB_IDL_UM_SIZE * sizeof( ID ) );
1017 stack = search_stack( op );
1020 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1021 rc = search_aliases( op, rs, e, txn, scopes, stack );
1026 if ( rc == LDAP_SUCCESS ) {
1027 rc = mdb_filter_candidates( op, txn, f, ids,
1028 stack, stack+MDB_IDL_UM_SIZE );
1031 if ( depth+1 > mdb->mi_search_stack_depth ) {
1036 Debug(LDAP_DEBUG_TRACE,
1037 "mdb_search_candidates: failed (rc=%d)\n",
1041 Debug(LDAP_DEBUG_TRACE,
1042 "mdb_search_candidates: id=%ld first=%ld last=%ld\n",
1044 (long) MDB_IDL_FIRST(ids),
1045 (long) MDB_IDL_LAST(ids) );
1052 parse_paged_cookie( Operation *op, SlapReply *rs )
1054 int rc = LDAP_SUCCESS;
1055 PagedResultsState *ps = op->o_pagedresults_state;
1057 /* this function must be invoked only if the pagedResults
1058 * control has been detected, parsed and partially checked
1059 * by the frontend */
1060 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1062 /* cookie decoding/checks deferred to backend... */
1063 if ( ps->ps_cookieval.bv_len ) {
1064 PagedResultsCookie reqcookie;
1065 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1067 rs->sr_text = "paged results cookie is invalid";
1068 rc = LDAP_PROTOCOL_ERROR;
1072 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1074 if ( reqcookie > ps->ps_cookie ) {
1076 rs->sr_text = "paged results cookie is invalid";
1077 rc = LDAP_PROTOCOL_ERROR;
1080 } else if ( reqcookie < ps->ps_cookie ) {
1081 rs->sr_text = "paged results cookie is invalid or old";
1082 rc = LDAP_UNWILLING_TO_PERFORM;
1087 /* we're going to use ps_cookie */
1088 op->o_conn->c_pagedresults_state.ps_cookie = 0;
1097 send_paged_response(
1103 LDAPControl *ctrls[2];
1104 BerElementBuffer berbuf;
1105 BerElement *ber = (BerElement *)&berbuf;
1106 PagedResultsCookie respcookie;
1107 struct berval cookie;
1109 Debug(LDAP_DEBUG_ARGS,
1110 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1111 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1115 ber_init2( ber, NULL, LBER_USE_DER );
1118 respcookie = ( PagedResultsCookie )(*lastid);
1119 cookie.bv_len = sizeof( respcookie );
1120 cookie.bv_val = (char *)&respcookie;
1123 respcookie = ( PagedResultsCookie )0;
1124 BER_BVSTR( &cookie, "" );
1127 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1128 op->o_conn->c_pagedresults_state.ps_count =
1129 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1132 /* return size of 0 -- no estimate */
1133 ber_printf( ber, "{iO}", 0, &cookie );
1135 ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1136 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1140 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1141 ctrls[0]->ldctl_iscritical = 0;
1143 slap_add_ctrls( op, rs, ctrls );
1144 rs->sr_err = LDAP_SUCCESS;
1145 send_ldap_result( op, rs );
1148 (void) ber_free_buf( ber );