1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 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 (
59 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
64 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
65 rs->sr_text = "maximum deref depth exceeded";
68 /* Remember the last entry we looked at, so we can
73 if (BDB_IDL_N(tmp) >= op->o_bd->be_max_deref_depth) {
78 /* If this is part of a subtree or onelevel search,
79 * have we seen this ID before? If so, quit.
81 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
86 /* If we've seen this ID during this deref iteration,
89 if ( bdb_idl_insert( tmp, e->e_id ) ) {
90 rs->sr_err = LDAP_ALIAS_PROBLEM;
91 rs->sr_text = "circular alias";
96 /* If there was a problem getting the aliasedObjectName,
97 * get_alias_dn will have set the error status.
99 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
104 rs->sr_err = bdb_dn2entry( op, txn, &ndn, &ei,
114 rs->sr_err = LDAP_ALIAS_PROBLEM;
115 rs->sr_text = "aliasedObject not found";
119 /* Free the previous entry, continue to work with the
120 * one we just retrieved.
122 bdb_cache_return_entry_r( bdb, *matched, lock);
125 /* We found a regular entry. Return this to the caller. The
126 * entry is still locked for Read.
128 if (!is_entry_alias(e)) {
129 rs->sr_err = LDAP_SUCCESS;
137 /* Look for and dereference all aliases within the search scope. Adds
138 * the dereferenced entries to the "ids" list. Requires "stack" to be
139 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
140 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
142 static int search_aliases(
151 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
152 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
153 ID cursora, ida, cursoro, ido, *subscop2;
156 struct berval bv_alias = BER_BVC( "alias" );
157 AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
159 DB_LOCK locka, lockr;
162 aliases = stack; /* IDL of all aliases in the database */
163 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
164 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
165 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
166 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
167 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
168 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
170 /* A copy of subscop, because subscop gets clobbered by
171 * the bdb_idl_union/intersection routines
173 subscop2 = tmp + BDB_IDL_DB_SIZE;
175 af.f_choice = LDAP_FILTER_EQUALITY;
176 af.f_ava = &aa_alias;
177 af.f_av_desc = slap_schema.si_ad_objectClass;
178 af.f_av_value = bv_alias;
181 /* Find all aliases in database */
182 BDB_IDL_ZERO( aliases );
183 rs->sr_err = bdb_filter_candidates( op, txn, &af, aliases,
185 if (rs->sr_err != LDAP_SUCCESS) {
189 oldsubs[1] = e->e_id;
192 BDB_IDL_ZERO( visited );
193 BDB_IDL_ZERO( newsubs );
196 ido = bdb_idl_first( oldsubs, &cursoro );
199 /* Set curscop to only the aliases in the current scope. Start with
200 * all the aliases, obtain the IDL for the current scope, and then
201 * get the intersection of these two IDLs. Add the current scope
202 * to the cumulative list of candidates.
204 BDB_IDL_CPY( curscop, aliases );
205 rs->sr_err = bdb_dn2idl( op, txn, &e->e_nname, BEI(e), subscop,
206 subscop2+BDB_IDL_DB_SIZE );
210 bdb_cache_return_entry_r (bdb, e, &locka);
212 BDB_IDL_CPY(subscop2, subscop);
213 rs->sr_err = bdb_idl_intersection(curscop, subscop);
214 bdb_idl_union( ids, subscop2 );
216 /* Dereference all of the aliases in the current scope. */
218 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
219 ida = bdb_idl_next(curscop, &cursora))
223 rs->sr_err = bdb_cache_find_id(op, txn,
224 ida, &ei, 0, &lockr );
225 if (rs->sr_err != LDAP_SUCCESS) {
226 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
227 rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
232 /* This should only happen if the curscop IDL has maxed out and
233 * turned into a range that spans IDs indiscriminately
235 if (!is_entry_alias(a)) {
236 bdb_cache_return_entry_r (bdb, a, &lockr);
240 /* Actually dereference the alias */
242 a = deref_base( op, rs, a, &matched, txn, &lockr,
245 /* If the target was not already in our current candidates,
246 * make note of it in the newsubs list. Also
247 * set it in the scopes list so that bdb_search
250 if (bdb_idl_insert(ids, a->e_id) == 0) {
251 bdb_idl_insert(newsubs, a->e_id);
252 bdb_idl_insert(scopes, a->e_id);
254 bdb_cache_return_entry_r( bdb, a, &lockr);
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, matched, &lockr );
264 /* If this is a OneLevel search, we're done; oldsubs only had one
265 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
267 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
269 ido = bdb_idl_next( oldsubs, &cursoro );
271 /* If we're done processing the old scopes, did we add any new
272 * scopes in this iteration? If so, go back and do those now.
275 if (BDB_IDL_IS_ZERO(newsubs)) break;
276 BDB_IDL_CPY(oldsubs, newsubs);
277 BDB_IDL_ZERO(newsubs);
279 ido = bdb_idl_first( oldsubs, &cursoro );
282 /* Find the entry corresponding to the next scope. If it can't
283 * be found, ignore it and move on. This should never happen;
284 * we should never see the ID of an entry that doesn't exist.
285 * Set the name so that the scope's IDL can be retrieved.
289 rs->sr_err = bdb_cache_find_id(op, txn, ido, &ei,
291 if ( rs->sr_err != LDAP_SUCCESS ) {
292 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
293 rs->sr_err == DB_LOCK_NOTGRANTED )
303 bdb_search( Operation *op, SlapReply *rs )
305 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
308 ID candidates[BDB_IDL_UM_SIZE];
309 ID scopes[BDB_IDL_DB_SIZE];
310 Entry *e = NULL, base, *e_root;
311 Entry *matched = NULL;
313 AttributeName *attrs;
314 struct berval realbase = BER_BVNULL;
318 int tentries = 0, nentries = 0;
322 struct bdb_op_info *opinfo = NULL;
326 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
327 attrs = op->oq_search.rs_attrs;
329 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
330 if ( oex->oe_key == bdb )
333 opinfo = (struct bdb_op_info *) oex;
335 manageDSAit = get_manageDSAit( op );
337 if ( opinfo && opinfo->boi_txn ) {
338 ltid = opinfo->boi_txn;
340 rs->sr_err = bdb_reader_get( op, bdb->bi_dbenv, <id );
346 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
351 e_root = bdb->bi_cache.c_dntree.bei_e;
352 if ( op->o_req_ndn.bv_len == 0 ) {
353 /* DIT root special case */
354 ei = e_root->e_private;
355 rs->sr_err = LDAP_SUCCESS;
357 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
358 BDB_IDL_ZERO(candidates);
361 /* get entry with reader lock */
362 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei,
374 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
376 case DB_LOCK_DEADLOCK:
377 case DB_LOCK_NOTGRANTED:
380 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
384 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
385 if ( matched && is_entry_alias( matched )) {
388 stub.bv_val = op->o_req_ndn.bv_val;
389 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
390 e = deref_base( op, rs, matched, &matched, ltid, &lock,
393 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
395 bdb_cache_return_entry_r (bdb, e, &lock);
399 } else if ( e && is_entry_alias( e )) {
400 e = deref_base( op, rs, e, &matched, ltid, &lock,
406 struct berval matched_dn = BER_BVNULL;
408 if ( matched != NULL ) {
409 BerVarray erefs = NULL;
411 /* return referral only if "disclose"
412 * is granted on the object */
413 if ( ! access_allowed( op, matched,
414 slap_schema.si_ad_entry,
415 NULL, ACL_DISCLOSE, NULL ) )
417 rs->sr_err = LDAP_NO_SUCH_OBJECT;
420 ber_dupbv( &matched_dn, &matched->e_name );
422 erefs = is_entry_referral( matched )
423 ? get_entry_referrals( op, matched )
425 if ( rs->sr_err == DB_NOTFOUND )
426 rs->sr_err = LDAP_REFERRAL;
427 rs->sr_matched = matched_dn.bv_val;
430 #ifdef SLAP_ZONE_ALLOC
431 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
433 bdb_cache_return_entry_r (bdb, matched, &lock);
437 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
438 &op->o_req_dn, op->oq_search.rs_scope );
439 ber_bvarray_free( erefs );
443 #ifdef SLAP_ZONE_ALLOC
444 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
446 rs->sr_ref = referral_rewrite( default_referral,
447 NULL, &op->o_req_dn, op->oq_search.rs_scope );
448 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
451 send_ldap_result( op, rs );
454 ber_bvarray_free( rs->sr_ref );
457 if ( !BER_BVISNULL( &matched_dn ) ) {
458 ber_memfree( matched_dn.bv_val );
459 rs->sr_matched = NULL;
464 /* NOTE: __NEW__ "search" access is required
465 * on searchBase object */
466 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
467 NULL, ACL_SEARCH, NULL, &mask ) )
469 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
470 rs->sr_err = LDAP_NO_SUCH_OBJECT;
472 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
475 #ifdef SLAP_ZONE_ALLOC
476 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
479 bdb_cache_return_entry_r(bdb, e, &lock);
481 send_ldap_result( op, rs );
485 if ( !manageDSAit && e != e_root && is_entry_referral( e ) ) {
486 /* entry is a referral, don't allow add */
487 struct berval matched_dn = BER_BVNULL;
488 BerVarray erefs = NULL;
490 ber_dupbv( &matched_dn, &e->e_name );
491 erefs = get_entry_referrals( op, e );
493 rs->sr_err = LDAP_REFERRAL;
495 #ifdef SLAP_ZONE_ALLOC
496 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
498 bdb_cache_return_entry_r( bdb, e, &lock );
502 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
503 &op->o_req_dn, op->oq_search.rs_scope );
504 ber_bvarray_free( erefs );
507 rs->sr_text = "bad_referral object";
511 Debug( LDAP_DEBUG_TRACE,
512 LDAP_XSTRING(bdb_search) ": entry is referral\n",
515 rs->sr_matched = matched_dn.bv_val;
516 send_ldap_result( op, rs );
518 ber_bvarray_free( rs->sr_ref );
520 ber_memfree( matched_dn.bv_val );
521 rs->sr_matched = NULL;
525 if ( get_assert( op ) &&
526 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
528 rs->sr_err = LDAP_ASSERTION_FAILED;
529 #ifdef SLAP_ZONE_ALLOC
530 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
533 bdb_cache_return_entry_r(bdb, e, &lock);
535 send_ldap_result( op, rs );
539 /* compute it anyway; root does not use it */
540 stoptime = op->o_time + op->ors_tlimit;
542 /* need normalized dn below */
543 ber_dupbv( &realbase, &e->e_nname );
545 /* Copy info to base, must free entry before accessing the database
546 * in search_candidates, to avoid deadlocks.
548 base.e_private = e->e_private;
549 base.e_nname = realbase;
552 #ifdef SLAP_ZONE_ALLOC
553 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
556 bdb_cache_return_entry_r(bdb, e, &lock);
560 /* select candidates */
561 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
562 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
565 BDB_IDL_ZERO( candidates );
566 BDB_IDL_ZERO( scopes );
567 rs->sr_err = search_candidates( op, rs, &base,
568 ltid, candidates, scopes );
571 /* start cursor at beginning of candidates.
575 if ( candidates[0] == 0 ) {
576 Debug( LDAP_DEBUG_TRACE,
577 LDAP_XSTRING(bdb_search) ": no candidates\n",
583 /* if not root and candidates exceed to-be-checked entries, abort */
584 if ( op->ors_limit /* isroot == FALSE */ &&
585 op->ors_limit->lms_s_unchecked != -1 &&
586 BDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
588 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
589 send_ldap_result( op, rs );
590 rs->sr_err = LDAP_SUCCESS;
594 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
595 !op->ors_limit->lms_s_pr_hide )
597 tentries = BDB_IDL_N(candidates);
600 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
601 PagedResultsState *ps = op->o_pagedresults_state;
602 /* deferred cookie parsing */
603 rs->sr_err = parse_paged_cookie( op, rs );
604 if ( rs->sr_err != LDAP_SUCCESS ) {
605 send_ldap_result( op, rs );
609 cursor = (ID) ps->ps_cookie;
610 if ( cursor && ps->ps_size == 0 ) {
611 rs->sr_err = LDAP_SUCCESS;
612 rs->sr_text = "search abandoned by pagedResult size=0";
613 send_ldap_result( op, rs );
616 id = bdb_idl_first( candidates, &cursor );
618 Debug( LDAP_DEBUG_TRACE,
619 LDAP_XSTRING(bdb_search)
620 ": no paged results candidates\n",
622 send_paged_response( op, rs, &lastid, 0 );
624 rs->sr_err = LDAP_OTHER;
627 nentries = ps->ps_count;
628 if ( id == (ID)ps->ps_cookie )
629 id = bdb_idl_next( candidates, &cursor );
633 for ( id = bdb_idl_first( candidates, &cursor );
634 id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
640 /* check for abandon */
641 if ( op->o_abandon ) {
642 rs->sr_err = SLAPD_ABANDON;
646 /* mostly needed by internal searches,
647 * e.g. related to syncrepl, for whom
648 * abandon does not get set... */
649 if ( slapd_shutdown ) {
650 rs->sr_err = LDAP_UNAVAILABLE;
651 send_ldap_disconnect( op, rs );
655 /* check time limit */
656 if ( op->ors_tlimit != SLAP_NO_LIMIT
657 && slap_get_time() > stoptime )
659 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
660 rs->sr_ref = rs->sr_v2ref;
661 send_ldap_result( op, rs );
662 rs->sr_err = LDAP_SUCCESS;
666 /* If we inspect more entries than will
667 * fit into the entry cache, stop caching
668 * any subsequent entries
671 if ( nentries > bdb->bi_cache.c_maxsize && !idflag )
675 /* get the entry with reader lock */
677 rs->sr_err = bdb_cache_find_id( op, ltid,
678 id, &ei, idflag, &lock );
680 if (rs->sr_err == LDAP_BUSY) {
681 rs->sr_text = "ldap server busy";
682 send_ldap_result( op, rs );
685 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
686 || rs->sr_err == DB_LOCK_NOTGRANTED )
688 goto fetch_entry_retry;
689 } else if ( rs->sr_err == LDAP_OTHER ) {
690 rs->sr_text = "internal error";
691 send_ldap_result( op, rs );
695 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
702 if( !BDB_IDL_IS_RANGE(candidates) ) {
703 /* only complain for non-range IDLs */
704 Debug( LDAP_DEBUG_TRACE,
705 LDAP_XSTRING(bdb_search)
706 ": candidate %ld not found\n",
715 if ( is_entry_subentry( e ) ) {
716 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
717 if(!get_subentries_visibility( op )) {
718 /* only subentries are visible */
722 } else if ( get_subentries( op ) &&
723 !get_subentries_visibility( op ))
725 /* only subentries are visible */
729 } else if ( get_subentries_visibility( op )) {
730 /* only subentries are visible */
734 /* Does this candidate actually satisfy the search scope?
736 * Note that we don't lock access to the bei_parent pointer.
737 * Since only leaf nodes can be deleted, the parent of any
738 * node will always be a valid node. Also since we have
739 * a Read lock on the data, it cannot be renamed out of the
740 * scope while we are looking at it, and unless we're using
741 * BDB_HIER, its parents cannot be moved either.
744 switch( op->ors_scope ) {
745 case LDAP_SCOPE_BASE:
746 /* This is always true, yes? */
747 if ( id == base.e_id ) scopeok = 1;
750 case LDAP_SCOPE_ONELEVEL:
751 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
754 #ifdef LDAP_SCOPE_CHILDREN
755 case LDAP_SCOPE_CHILDREN:
756 if ( id == base.e_id ) break;
759 case LDAP_SCOPE_SUBTREE: {
761 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
762 if ( tmp->bei_id == base.e_id ) {
770 /* aliases were already dereferenced in candidate list */
771 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
772 /* but if the search base is an alias, and we didn't
773 * deref it when finding, return it.
775 if ( is_entry_alias(e) &&
776 ((op->ors_deref & LDAP_DEREF_FINDING) ||
777 !bvmatch(&e->e_nname, &op->o_req_ndn)))
782 /* scopes is only non-empty for onelevel or subtree */
783 if ( !scopeok && BDB_IDL_N(scopes) ) {
785 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
786 x = bdb_idl_search( scopes, e->e_id );
787 if ( scopes[x] == e->e_id ) scopeok = 1;
789 /* subtree, walk up the tree */
790 EntryInfo *tmp = BEI(e);
791 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
792 x = bdb_idl_search( scopes, tmp->bei_id );
793 if ( scopes[x] == tmp->bei_id ) {
802 /* Not in scope, ignore it */
805 Debug( LDAP_DEBUG_TRACE,
806 LDAP_XSTRING(bdb_search)
807 ": %ld scope not okay\n",
813 * if it's a referral, add it to the list of referrals. only do
814 * this for non-base searches, and don't check the filter
815 * explicitly here since it's only a candidate anyway.
817 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
818 && is_entry_referral( e ) )
820 BerVarray erefs = get_entry_referrals( op, e );
821 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
822 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
823 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
825 send_search_reference( op, rs );
827 ber_bvarray_free( rs->sr_ref );
828 ber_bvarray_free( erefs );
834 if ( !manageDSAit && is_entry_glue( e )) {
838 /* if it matches the filter and scope, send it */
839 rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
841 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
842 /* check size limit */
843 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
844 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
845 #ifdef SLAP_ZONE_ALLOC
846 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
848 bdb_cache_return_entry_r( bdb, e, &lock );
850 send_paged_response( op, rs, &lastid, tentries );
858 rs->sr_attrs = op->oq_search.rs_attrs;
859 rs->sr_operational_attrs = NULL;
862 rs->sr_err = LDAP_SUCCESS;
863 rs->sr_err = send_search_entry( op, rs );
865 switch ( rs->sr_err ) {
866 case LDAP_SUCCESS: /* entry sent ok */
868 default: /* entry not sent */
870 case LDAP_UNAVAILABLE:
871 case LDAP_SIZELIMIT_EXCEEDED:
872 #ifdef SLAP_ZONE_ALLOC
873 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
875 bdb_cache_return_entry_r(bdb, e, &lock);
878 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
879 rs->sr_ref = rs->sr_v2ref;
880 send_ldap_result( op, rs );
881 rs->sr_err = LDAP_SUCCESS;
884 rs->sr_err = LDAP_OTHER;
891 Debug( LDAP_DEBUG_TRACE,
892 LDAP_XSTRING(bdb_search)
893 ": %ld does not match filter\n",
899 /* free reader lock */
900 #ifdef SLAP_ZONE_ALLOC
901 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
903 bdb_cache_return_entry_r( bdb, e , &lock );
911 rs->sr_ref = rs->sr_v2ref;
912 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
913 rs->sr_rspoid = NULL;
914 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
915 send_paged_response( op, rs, NULL, 0 );
917 send_ldap_result( op, rs );
920 rs->sr_err = LDAP_SUCCESS;
924 ber_bvarray_free( rs->sr_v2ref );
927 if( realbase.bv_val ) ch_free( realbase.bv_val );
933 static int base_candidate(
938 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
939 e->e_nname.bv_val, (long) e->e_id, 0);
946 /* Look for "objectClass Present" in this filter.
947 * Also count depth of filter tree while we're at it.
949 static int oc_filter(
958 if( cur > *max ) *max = cur;
960 switch( f->f_choice ) {
961 case LDAP_FILTER_PRESENT:
962 if (f->f_desc == slap_schema.si_ad_objectClass) {
967 case LDAP_FILTER_AND:
970 for ( f=f->f_and; f; f=f->f_next ) {
971 (void) oc_filter(f, cur, max);
981 static void search_stack_free( void *key, void *data )
983 ber_memfree_x(data, NULL);
986 static void *search_stack( Operation *op )
988 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
991 if ( op->o_threadctx ) {
992 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
995 ret = bdb->bi_search_stack;
999 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1001 if ( op->o_threadctx ) {
1002 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1003 ret, search_stack_free, NULL, NULL );
1005 bdb->bi_search_stack = ret;
1011 static int search_candidates(
1019 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1021 Filter f, rf, xf, nf;
1023 AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1025 AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1028 * This routine takes as input a filter (user-filter)
1029 * and rewrites it as follows:
1030 * (&(scope=DN)[(objectClass=subentry)]
1031 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1034 Debug(LDAP_DEBUG_TRACE,
1035 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1036 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1038 xf.f_or = op->oq_search.rs_filter;
1039 xf.f_choice = LDAP_FILTER_OR;
1042 /* If the user's filter uses objectClass=*,
1043 * these clauses are redundant.
1045 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1046 && !get_subentries_visibility(op)) {
1047 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1048 /* match referral objects */
1049 struct berval bv_ref = BER_BVC( "referral" );
1050 rf.f_choice = LDAP_FILTER_EQUALITY;
1052 rf.f_av_desc = slap_schema.si_ad_objectClass;
1053 rf.f_av_value = bv_ref;
1054 rf.f_next = xf.f_or;
1061 f.f_choice = LDAP_FILTER_AND;
1063 /* Dummy; we compute scope separately now */
1064 nf.f_choice = SLAPD_FILTER_COMPUTED;
1065 nf.f_result = LDAP_SUCCESS;
1066 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1067 ? op->oq_search.rs_filter : &xf ;
1068 /* Filter depth increased again, adding dummy clause */
1071 if( get_subentries_visibility( op ) ) {
1072 struct berval bv_subentry = BER_BVC( "subentry" );
1073 sf.f_choice = LDAP_FILTER_EQUALITY;
1074 sf.f_ava = &aa_subentry;
1075 sf.f_av_desc = slap_schema.si_ad_objectClass;
1076 sf.f_av_value = bv_subentry;
1077 sf.f_next = nf.f_next;
1081 /* Allocate IDL stack, plus 1 more for former tmp */
1082 if ( depth+1 > bdb->bi_search_stack_depth ) {
1083 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1085 stack = search_stack( op );
1088 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1089 rc = search_aliases( op, rs, e, txn, ids, scopes, stack );
1091 rc = bdb_dn2idl( op, txn, &e->e_nname, BEI(e), ids, stack );
1094 if ( rc == LDAP_SUCCESS ) {
1095 rc = bdb_filter_candidates( op, txn, &f, ids,
1096 stack, stack+BDB_IDL_UM_SIZE );
1099 if ( depth+1 > bdb->bi_search_stack_depth ) {
1104 Debug(LDAP_DEBUG_TRACE,
1105 "bdb_search_candidates: failed (rc=%d)\n",
1109 Debug(LDAP_DEBUG_TRACE,
1110 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1112 (long) BDB_IDL_FIRST(ids),
1113 (long) BDB_IDL_LAST(ids) );
1120 parse_paged_cookie( Operation *op, SlapReply *rs )
1122 int rc = LDAP_SUCCESS;
1123 PagedResultsState *ps = op->o_pagedresults_state;
1125 /* this function must be invoked only if the pagedResults
1126 * control has been detected, parsed and partially checked
1127 * by the frontend */
1128 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1130 /* cookie decoding/checks deferred to backend... */
1131 if ( ps->ps_cookieval.bv_len ) {
1132 PagedResultsCookie reqcookie;
1133 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1135 rs->sr_text = "paged results cookie is invalid";
1136 rc = LDAP_PROTOCOL_ERROR;
1140 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1142 if ( reqcookie > ps->ps_cookie ) {
1144 rs->sr_text = "paged results cookie is invalid";
1145 rc = LDAP_PROTOCOL_ERROR;
1148 } else if ( reqcookie < ps->ps_cookie ) {
1149 rs->sr_text = "paged results cookie is invalid or old";
1150 rc = LDAP_UNWILLING_TO_PERFORM;
1162 send_paged_response(
1168 LDAPControl ctrl, *ctrls[2];
1169 BerElementBuffer berbuf;
1170 BerElement *ber = (BerElement *)&berbuf;
1171 PagedResultsCookie respcookie;
1172 struct berval cookie;
1174 Debug(LDAP_DEBUG_ARGS,
1175 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1176 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1178 BER_BVZERO( &ctrl.ldctl_value );
1182 ber_init2( ber, NULL, LBER_USE_DER );
1185 respcookie = ( PagedResultsCookie )(*lastid);
1186 cookie.bv_len = sizeof( respcookie );
1187 cookie.bv_val = (char *)&respcookie;
1190 respcookie = ( PagedResultsCookie )0;
1191 BER_BVSTR( &cookie, "" );
1194 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1195 op->o_conn->c_pagedresults_state.ps_count =
1196 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1199 /* return size of 0 -- no estimate */
1200 ber_printf( ber, "{iO}", 0, &cookie );
1202 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1206 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1207 ctrls[0]->ldctl_iscritical = 0;
1209 rs->sr_ctrls = ctrls;
1210 rs->sr_err = LDAP_SUCCESS;
1211 send_ldap_result( op, rs );
1212 rs->sr_ctrls = NULL;
1215 (void) ber_free_buf( ber );