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(
39 static int parse_paged_cookie( Operation *op, SlapReply *rs );
41 static void send_paged_response(
47 /* Dereference aliases for a single alias entry. Return the final
48 * dereferenced entry on success, NULL on any failure.
50 static Entry * deref_base (
61 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
62 rs->sr_text = "maximum deref depth exceeded";
65 /* Remember the last entry we looked at, so we can
70 if (MDB_IDL_N(tmp) >= op->o_bd->be_max_deref_depth) {
75 /* If this is part of a subtree or onelevel search,
76 * have we seen this ID before? If so, quit.
78 if ( visited && mdb_idl_insert( visited, e->e_id ) ) {
83 /* If we've seen this ID during this deref iteration,
86 if ( mdb_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 = mdb_dn2entry( op, txn, &ndn, &e, 0 );
103 rs->sr_err = LDAP_ALIAS_PROBLEM;
104 rs->sr_text = "aliasedObject not found";
108 /* Free the previous entry, continue to work with the
109 * one we just retrieved.
111 mdb_entry_return( op, *matched );
113 /* We found a regular entry. Return this to the caller.
115 if (!is_entry_alias(e)) {
116 rs->sr_err = LDAP_SUCCESS;
124 /* Look for and dereference all aliases within the search scope.
125 * Requires "stack" to be able to hold 6 levels of DB_SIZE IDLs.
126 * Of course we're hardcoded to require a minimum of 8 UM_SIZE
127 * IDLs so this is never a problem.
129 static int search_aliases(
138 ID *aliases, *curscop, *visited, *newsubs, *oldsubs, *tmp;
139 ID cursora, ida, cursoro, ido;
141 struct berval bv_alias = BER_BVC( "alias" );
142 AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
146 aliases = stack; /* IDL of all aliases in the database */
147 curscop = aliases + MDB_IDL_DB_SIZE; /* Aliases in the current scope */
148 visited = curscop + MDB_IDL_DB_SIZE; /* IDs we've seen in this search */
149 newsubs = visited + MDB_IDL_DB_SIZE; /* New subtrees we've added */
150 oldsubs = newsubs + MDB_IDL_DB_SIZE; /* Subtrees added previously */
151 tmp = oldsubs + MDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
153 af.f_choice = LDAP_FILTER_EQUALITY;
154 af.f_ava = &aa_alias;
155 af.f_av_desc = slap_schema.si_ad_objectClass;
156 af.f_av_value = bv_alias;
159 /* Find all aliases in database */
160 MDB_IDL_ZERO( aliases );
161 rs->sr_err = mdb_filter_candidates( op, txn, &af, aliases,
163 if (rs->sr_err != LDAP_SUCCESS) {
167 oldsubs[1] = e->e_id;
169 MDB_IDL_ZERO( visited );
170 MDB_IDL_ZERO( newsubs );
173 ido = mdb_idl_first( oldsubs, &cursoro );
176 /* Set curscop to only the aliases in the current scope. Start with
177 * all the aliases, then get the intersection with the scope.
179 rs->sr_err = mdb_idscope( op, txn, e->e_id, aliases, curscop );
184 mdb_entry_return( op, e );
187 /* Dereference all of the aliases in the current scope. */
189 for (ida = mdb_idl_first(curscop, &cursora); ida != NOID;
190 ida = mdb_idl_next(curscop, &cursora))
192 rs->sr_err = mdb_id2entry(op, mci, ida, &a);
193 if (rs->sr_err != LDAP_SUCCESS) {
197 /* This should only happen if the curscop IDL has maxed out and
198 * turned into a range that spans IDs indiscriminately
200 if (!is_entry_alias(a)) {
201 mdb_entry_return(op, a);
205 /* Actually dereference the alias */
207 a = deref_base( op, rs, a, &matched, txn,
210 /* If the target was not already in our current scopes,
211 * make note of it in the newsubs list.
213 if (mdb_idl_insert(scopes, a->e_id) == 0) {
214 mdb_idl_insert(newsubs, a->e_id);
216 mdb_entry_return( op, a );
218 } else if (matched) {
219 /* Alias could not be dereferenced, or it deref'd to
220 * an ID we've already seen. Ignore it.
222 mdb_entry_return( op, matched );
226 /* If this is a OneLevel search, we're done; oldsubs only had one
227 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
229 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
231 ido = mdb_idl_next( oldsubs, &cursoro );
233 /* If we're done processing the old scopes, did we add any new
234 * scopes in this iteration? If so, go back and do those now.
237 if (MDB_IDL_IS_ZERO(newsubs)) break;
238 MDB_IDL_CPY(oldsubs, newsubs);
239 MDB_IDL_ZERO(newsubs);
241 ido = mdb_idl_first( oldsubs, &cursoro );
244 /* Find the entry corresponding to the next scope. If it can't
245 * be found, ignore it and move on. This should never happen;
246 * we should never see the ID of an entry that doesn't exist.
248 rs->sr_err = mdb_id2entry(op, mci, ido, &e);
249 if ( rs->sr_err != LDAP_SUCCESS ) {
256 /* Get the next ID from the DB. Used if the candidate list is
257 * a range and simple iteration hits missing entryIDs
260 mdb_get_nextid(MDB_cursor *mci, ID *cursor)
268 key.mv_size = sizeof(ID);
269 rc = mdb_cursor_get( mci, &key, NULL, MDB_SET_RANGE );
272 memcpy( cursor, key.mv_data, sizeof(ID));
277 mdb_search( Operation *op, SlapReply *rs )
279 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
282 ID candidates[MDB_IDL_UM_SIZE];
283 ID scopes[MDB_IDL_DB_SIZE];
284 Entry *e = NULL, *base = NULL;
285 Entry *matched = NULL;
286 AttributeName *attrs;
294 mdb_op_info opinfo = {0}, *moi = &opinfo;
295 MDB_txn *ltid = NULL;
297 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_search) "\n", 0, 0, 0);
298 attrs = op->oq_search.rs_attrs;
300 manageDSAit = get_manageDSAit( op );
302 rs->sr_err = mdb_opinfo_get( op, mdb, 1, &moi );
307 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
316 rs->sr_err = mdb_cursor_open( ltid, mdb->mi_id2entry, &mci );
318 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
322 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
323 MDB_IDL_ZERO(candidates);
326 /* get entry with reader lock */
327 rs->sr_err = mdb_dn2entry( op, ltid, &op->o_req_ndn, &e, 1 );
337 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
340 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
344 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
345 if ( matched && is_entry_alias( matched )) {
348 stub.bv_val = op->o_req_ndn.bv_val;
349 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
350 e = deref_base( op, rs, matched, &matched, ltid,
353 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
355 mdb_entry_return(op, e);
359 } else if ( e && is_entry_alias( e )) {
360 e = deref_base( op, rs, e, &matched, ltid,
366 struct berval matched_dn = BER_BVNULL;
368 if ( matched != NULL ) {
369 BerVarray erefs = NULL;
371 /* return referral only if "disclose"
372 * is granted on the object */
373 if ( ! access_allowed( op, matched,
374 slap_schema.si_ad_entry,
375 NULL, ACL_DISCLOSE, NULL ) )
377 rs->sr_err = LDAP_NO_SUCH_OBJECT;
380 ber_dupbv( &matched_dn, &matched->e_name );
382 erefs = is_entry_referral( matched )
383 ? get_entry_referrals( op, matched )
385 if ( rs->sr_err == MDB_NOTFOUND )
386 rs->sr_err = LDAP_REFERRAL;
387 rs->sr_matched = matched_dn.bv_val;
390 mdb_entry_return(op, matched);
394 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
395 &op->o_req_dn, op->oq_search.rs_scope );
396 ber_bvarray_free( erefs );
400 rs->sr_ref = referral_rewrite( default_referral,
401 NULL, &op->o_req_dn, op->oq_search.rs_scope );
402 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
405 send_ldap_result( op, rs );
408 ber_bvarray_free( rs->sr_ref );
411 if ( !BER_BVISNULL( &matched_dn ) ) {
412 ber_memfree( matched_dn.bv_val );
413 rs->sr_matched = NULL;
418 /* NOTE: __NEW__ "search" access is required
419 * on searchBase object */
420 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
421 NULL, ACL_SEARCH, NULL, &mask ) )
423 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
424 rs->sr_err = LDAP_NO_SUCH_OBJECT;
426 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
429 mdb_entry_return( op,e);
430 send_ldap_result( op, rs );
434 if ( !manageDSAit && is_entry_referral( e ) ) {
435 /* entry is a referral */
436 struct berval matched_dn = BER_BVNULL;
437 BerVarray erefs = NULL;
439 ber_dupbv( &matched_dn, &e->e_name );
440 erefs = get_entry_referrals( op, e );
442 rs->sr_err = LDAP_REFERRAL;
444 mdb_entry_return( op, e );
448 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
449 &op->o_req_dn, op->oq_search.rs_scope );
450 ber_bvarray_free( erefs );
453 rs->sr_text = "bad_referral object";
457 Debug( LDAP_DEBUG_TRACE,
458 LDAP_XSTRING(mdb_search) ": entry is referral\n",
461 rs->sr_matched = matched_dn.bv_val;
462 send_ldap_result( op, rs );
464 ber_bvarray_free( rs->sr_ref );
466 ber_memfree( matched_dn.bv_val );
467 rs->sr_matched = NULL;
471 if ( get_assert( op ) &&
472 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
474 rs->sr_err = LDAP_ASSERTION_FAILED;
475 mdb_entry_return( op,e);
476 send_ldap_result( op, rs );
480 /* compute it anyway; root does not use it */
481 stoptime = op->o_time + op->ors_tlimit;
487 /* select candidates */
488 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
489 rs->sr_err = base_candidate( op->o_bd, base, candidates );
492 MDB_IDL_ZERO( candidates );
493 MDB_IDL_ZERO( scopes );
494 mdb_idl_insert( scopes, base->e_id );
495 rs->sr_err = search_candidates( op, rs, base,
496 ltid, mci, candidates, scopes );
499 /* start cursor at beginning of candidates.
503 if ( candidates[0] == 0 ) {
504 Debug( LDAP_DEBUG_TRACE,
505 LDAP_XSTRING(mdb_search) ": no candidates\n",
511 /* if not root and candidates exceed to-be-checked entries, abort */
512 if ( op->ors_limit /* isroot == FALSE */ &&
513 op->ors_limit->lms_s_unchecked != -1 &&
514 MDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
516 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
517 send_ldap_result( op, rs );
518 rs->sr_err = LDAP_SUCCESS;
522 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
523 !op->ors_limit->lms_s_pr_hide )
525 tentries = MDB_IDL_N(candidates);
528 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
529 PagedResultsState *ps = op->o_pagedresults_state;
530 /* deferred cookie parsing */
531 rs->sr_err = parse_paged_cookie( op, rs );
532 if ( rs->sr_err != LDAP_SUCCESS ) {
533 send_ldap_result( op, rs );
537 cursor = (ID) ps->ps_cookie;
538 if ( cursor && ps->ps_size == 0 ) {
539 rs->sr_err = LDAP_SUCCESS;
540 rs->sr_text = "search abandoned by pagedResult size=0";
541 send_ldap_result( op, rs );
544 id = mdb_idl_first( candidates, &cursor );
546 Debug( LDAP_DEBUG_TRACE,
547 LDAP_XSTRING(mdb_search)
548 ": no paged results candidates\n",
550 send_paged_response( op, rs, &lastid, 0 );
552 rs->sr_err = LDAP_OTHER;
555 if ( id == (ID)ps->ps_cookie )
556 id = mdb_idl_next( candidates, &cursor );
560 for ( id = mdb_idl_first( candidates, &cursor );
561 id != NOID ; id = mdb_idl_next( candidates, &cursor ) )
567 /* check for abandon */
568 if ( op->o_abandon ) {
569 rs->sr_err = SLAPD_ABANDON;
570 send_ldap_result( op, rs );
574 /* mostly needed by internal searches,
575 * e.g. related to syncrepl, for whom
576 * abandon does not get set... */
577 if ( slapd_shutdown ) {
578 rs->sr_err = LDAP_UNAVAILABLE;
579 send_ldap_disconnect( op, rs );
583 /* check time limit */
584 if ( op->ors_tlimit != SLAP_NO_LIMIT
585 && slap_get_time() > stoptime )
587 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
588 rs->sr_ref = rs->sr_v2ref;
589 send_ldap_result( op, rs );
590 rs->sr_err = LDAP_SUCCESS;
594 if ( id == base->e_id ) {
599 rs->sr_err = mdb_id2entry( op, mci, id, &e );
601 if (rs->sr_err == LDAP_BUSY) {
602 rs->sr_text = "ldap server busy";
603 send_ldap_result( op, rs );
606 } else if ( rs->sr_err == LDAP_OTHER ) {
607 rs->sr_text = "internal error";
608 send_ldap_result( op, rs );
613 if( !MDB_IDL_IS_RANGE(candidates) ) {
614 /* only complain for non-range IDLs */
615 Debug( LDAP_DEBUG_TRACE,
616 LDAP_XSTRING(mdb_search)
617 ": candidate %ld not found\n",
620 /* get the next ID from the DB */
621 rs->sr_err = mdb_get_nextid( mci, &cursor );
622 if ( rs->sr_err == MDB_NOTFOUND ) {
626 rs->sr_err = LDAP_OTHER;
627 rs->sr_text = "internal error in get_nextid";
628 send_ldap_result( op, rs );
638 if ( is_entry_subentry( e ) ) {
639 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
640 if(!get_subentries_visibility( op )) {
641 /* only subentries are visible */
645 } else if ( get_subentries( op ) &&
646 !get_subentries_visibility( op ))
648 /* only subentries are visible */
652 } else if ( get_subentries_visibility( op )) {
653 /* only subentries are visible */
657 /* Does this candidate actually satisfy the search scope?
661 switch( op->ors_scope ) {
662 case LDAP_SCOPE_BASE:
663 /* This is always true, yes? */
664 if ( id == base->e_id ) scopeok = 1;
667 #ifdef LDAP_SCOPE_CHILDREN
668 case LDAP_SCOPE_CHILDREN:
669 if ( id == base->e_id ) break;
672 case LDAP_SCOPE_SUBTREE:
673 if ( id == base->e_id ) {
678 case LDAP_SCOPE_ONELEVEL:
680 if ( mdb_idscopes( op, &isc ) == MDB_SUCCESS ) scopeok = 1;
684 /* aliases were already dereferenced in candidate list */
685 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
686 /* but if the search base is an alias, and we didn't
687 * deref it when finding, return it.
689 if ( is_entry_alias(e) &&
690 ((op->ors_deref & LDAP_DEREF_FINDING) ||
691 !bvmatch(&e->e_nname, &op->o_req_ndn)))
697 /* Not in scope, ignore it */
700 Debug( LDAP_DEBUG_TRACE,
701 LDAP_XSTRING(mdb_search)
702 ": %ld scope not okay\n",
707 if ( !manageDSAit && is_entry_glue( e )) {
712 struct berval pdn, pndn;
715 /* child of base, just append RDNs to base->e_name */
716 if ( isc.nscope == 1 ) {
718 pndn = base->e_nname;
720 mdb_id2name( op, ltid, &isc.mc, scopes[isc.nscope], &pdn, &pndn );
722 e->e_name.bv_len = pdn.bv_len;
723 e->e_nname.bv_len = pndn.bv_len;
724 for (i=0; i<isc.numrdns; i++) {
725 e->e_name.bv_len += isc.rdns[i].bv_len + 1;
726 e->e_nname.bv_len += isc.nrdns[i].bv_len + 1;
728 e->e_name.bv_val = op->o_tmpalloc(e->e_name.bv_len + 1, op->o_tmpmemctx);
729 e->e_nname.bv_val = op->o_tmpalloc(e->e_nname.bv_len + 1, op->o_tmpmemctx);
730 d = e->e_name.bv_val;
731 n = e->e_nname.bv_val;
732 for (i=0; i<isc.numrdns; i++) {
733 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
734 d += isc.rdns[i].bv_len;
736 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
737 n += isc.nrdns[i].bv_len;
741 memcpy(d, pdn.bv_val, pdn.bv_len+1);
742 memcpy(n, pndn.bv_val, pndn.bv_len+1);
749 if (isc.nscope != 1) {
750 op->o_tmpfree(pndn.bv_val, op->o_tmpmemctx);
751 op->o_tmpfree(pdn.bv_val, op->o_tmpmemctx);
756 * if it's a referral, add it to the list of referrals. only do
757 * this for non-base searches, and don't check the filter
758 * explicitly here since it's only a candidate anyway.
760 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
761 && is_entry_referral( e ) )
763 BerVarray erefs = get_entry_referrals( op, e );
764 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
765 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
766 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
771 send_search_reference( op, rs );
773 mdb_entry_return( op, e );
777 ber_bvarray_free( rs->sr_ref );
778 ber_bvarray_free( erefs );
784 /* if it matches the filter and scope, send it */
785 rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
787 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
788 /* check size limit */
789 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
790 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
791 mdb_entry_return( op, e );
793 send_paged_response( op, rs, &lastid, tentries );
801 rs->sr_attrs = op->oq_search.rs_attrs;
802 rs->sr_operational_attrs = NULL;
805 RS_ASSERT( e->e_private != NULL );
807 rs->sr_err = LDAP_SUCCESS;
808 rs->sr_err = send_search_entry( op, rs );
812 mdb_entry_return( op, e );
815 switch ( rs->sr_err ) {
816 case LDAP_SUCCESS: /* entry sent ok */
818 default: /* entry not sent */
820 case LDAP_UNAVAILABLE:
821 case LDAP_SIZELIMIT_EXCEEDED:
822 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
823 rs->sr_ref = rs->sr_v2ref;
824 send_ldap_result( op, rs );
825 rs->sr_err = LDAP_SUCCESS;
828 rs->sr_err = LDAP_OTHER;
835 Debug( LDAP_DEBUG_TRACE,
836 LDAP_XSTRING(mdb_search)
837 ": %ld does not match filter\n",
844 mdb_entry_return( op, e );
845 RS_ASSERT( rs->sr_entry == NULL );
853 rs->sr_ref = rs->sr_v2ref;
854 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
855 rs->sr_rspoid = NULL;
856 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
857 send_paged_response( op, rs, NULL, 0 );
859 send_ldap_result( op, rs );
862 rs->sr_err = LDAP_SUCCESS;
866 mdb_cursor_close( isc.mc );
868 mdb_cursor_close( mci );
869 if ( moi == &opinfo ) {
870 mdb_txn_reset( moi->moi_txn );
871 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
874 ber_bvarray_free( rs->sr_v2ref );
878 mdb_entry_return( op,base);
884 static int base_candidate(
889 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
890 e->e_nname.bv_val, (long) e->e_id, 0);
897 /* Look for "objectClass Present" in this filter.
898 * Also count depth of filter tree while we're at it.
900 static int oc_filter(
909 if( cur > *max ) *max = cur;
911 switch( f->f_choice ) {
912 case LDAP_FILTER_PRESENT:
913 if (f->f_desc == slap_schema.si_ad_objectClass) {
918 case LDAP_FILTER_AND:
921 for ( f=f->f_and; f; f=f->f_next ) {
922 (void) oc_filter(f, cur, max);
932 static void search_stack_free( void *key, void *data )
934 ber_memfree_x(data, NULL);
937 static void *search_stack( Operation *op )
939 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
942 if ( op->o_threadctx ) {
943 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
946 ret = mdb->mi_search_stack;
950 ret = ch_malloc( mdb->mi_search_stack_depth * MDB_IDL_UM_SIZE
952 if ( op->o_threadctx ) {
953 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
954 ret, search_stack_free, NULL, NULL );
956 mdb->mi_search_stack = ret;
962 static int search_candidates(
971 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
973 Filter *f, rf, xf, nf, sf;
975 AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
976 AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
979 * This routine takes as input a filter (user-filter)
980 * and rewrites it as follows:
981 * (&(scope=DN)[(objectClass=subentry)]
982 * (|[(objectClass=referral)](user-filter))
985 Debug(LDAP_DEBUG_TRACE,
986 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
987 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
989 f = op->oq_search.rs_filter;
991 /* If the user's filter uses objectClass=*,
992 * these clauses are redundant.
994 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
995 && !get_subentries_visibility(op)) {
996 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
997 /* match referral objects */
998 struct berval bv_ref = BER_BVC( "referral" );
999 rf.f_choice = LDAP_FILTER_EQUALITY;
1001 rf.f_av_desc = slap_schema.si_ad_objectClass;
1002 rf.f_av_value = bv_ref;
1005 xf.f_choice = LDAP_FILTER_OR;
1012 if( get_subentries_visibility( op ) ) {
1013 struct berval bv_subentry = BER_BVC( "subentry" );
1014 sf.f_choice = LDAP_FILTER_EQUALITY;
1015 sf.f_ava = &aa_subentry;
1016 sf.f_av_desc = slap_schema.si_ad_objectClass;
1017 sf.f_av_value = bv_subentry;
1019 nf.f_choice = LDAP_FILTER_AND;
1026 /* Allocate IDL stack, plus 1 more for former tmp */
1027 if ( depth+1 > mdb->mi_search_stack_depth ) {
1028 stack = ch_malloc( (depth + 1) * MDB_IDL_UM_SIZE * sizeof( ID ) );
1030 stack = search_stack( op );
1033 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1034 rc = search_aliases( op, rs, e, txn, mci, scopes, stack );
1039 if ( rc == LDAP_SUCCESS ) {
1040 rc = mdb_filter_candidates( op, txn, f, ids,
1041 stack, stack+MDB_IDL_UM_SIZE );
1044 if ( depth+1 > mdb->mi_search_stack_depth ) {
1049 Debug(LDAP_DEBUG_TRACE,
1050 "mdb_search_candidates: failed (rc=%d)\n",
1054 Debug(LDAP_DEBUG_TRACE,
1055 "mdb_search_candidates: id=%ld first=%ld last=%ld\n",
1057 (long) MDB_IDL_FIRST(ids),
1058 (long) MDB_IDL_LAST(ids) );
1065 parse_paged_cookie( Operation *op, SlapReply *rs )
1067 int rc = LDAP_SUCCESS;
1068 PagedResultsState *ps = op->o_pagedresults_state;
1070 /* this function must be invoked only if the pagedResults
1071 * control has been detected, parsed and partially checked
1072 * by the frontend */
1073 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1075 /* cookie decoding/checks deferred to backend... */
1076 if ( ps->ps_cookieval.bv_len ) {
1077 PagedResultsCookie reqcookie;
1078 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1080 rs->sr_text = "paged results cookie is invalid";
1081 rc = LDAP_PROTOCOL_ERROR;
1085 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1087 if ( reqcookie > ps->ps_cookie ) {
1089 rs->sr_text = "paged results cookie is invalid";
1090 rc = LDAP_PROTOCOL_ERROR;
1093 } else if ( reqcookie < ps->ps_cookie ) {
1094 rs->sr_text = "paged results cookie is invalid or old";
1095 rc = LDAP_UNWILLING_TO_PERFORM;
1100 /* we're going to use ps_cookie */
1101 op->o_conn->c_pagedresults_state.ps_cookie = 0;
1110 send_paged_response(
1116 LDAPControl *ctrls[2];
1117 BerElementBuffer berbuf;
1118 BerElement *ber = (BerElement *)&berbuf;
1119 PagedResultsCookie respcookie;
1120 struct berval cookie;
1122 Debug(LDAP_DEBUG_ARGS,
1123 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1124 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1128 ber_init2( ber, NULL, LBER_USE_DER );
1131 respcookie = ( PagedResultsCookie )(*lastid);
1132 cookie.bv_len = sizeof( respcookie );
1133 cookie.bv_val = (char *)&respcookie;
1136 respcookie = ( PagedResultsCookie )0;
1137 BER_BVSTR( &cookie, "" );
1140 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1141 op->o_conn->c_pagedresults_state.ps_count =
1142 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1145 /* return size of 0 -- no estimate */
1146 ber_printf( ber, "{iO}", 0, &cookie );
1148 ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1149 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1153 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1154 ctrls[0]->ldctl_iscritical = 0;
1156 slap_add_ctrls( op, rs, ctrls );
1157 rs->sr_err = LDAP_SUCCESS;
1158 send_ldap_result( op, rs );
1161 (void) ber_free_buf( ber );