1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2009 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,
106 if ( rs->sr_err == DB_LOCK_DEADLOCK )
116 rs->sr_err = LDAP_ALIAS_PROBLEM;
117 rs->sr_text = "aliasedObject not found";
121 /* Free the previous entry, continue to work with the
122 * one we just retrieved.
124 bdb_cache_return_entry_r( bdb, *matched, lock);
127 /* We found a regular entry. Return this to the caller. The
128 * entry is still locked for Read.
130 if (!is_entry_alias(e)) {
131 rs->sr_err = LDAP_SUCCESS;
139 /* Look for and dereference all aliases within the search scope. Adds
140 * the dereferenced entries to the "ids" list. Requires "stack" to be
141 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
142 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
144 static int search_aliases(
153 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
154 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
155 ID cursora, ida, cursoro, ido, *subscop2;
158 struct berval bv_alias = BER_BVC( "alias" );
159 AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
161 DB_LOCK locka, lockr;
164 aliases = stack; /* IDL of all aliases in the database */
165 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
166 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
167 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
168 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
169 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
170 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
172 /* A copy of subscop, because subscop gets clobbered by
173 * the bdb_idl_union/intersection routines
175 subscop2 = tmp + BDB_IDL_DB_SIZE;
177 af.f_choice = LDAP_FILTER_EQUALITY;
178 af.f_ava = &aa_alias;
179 af.f_av_desc = slap_schema.si_ad_objectClass;
180 af.f_av_value = bv_alias;
183 /* Find all aliases in database */
184 BDB_IDL_ZERO( aliases );
185 rs->sr_err = bdb_filter_candidates( op, txn, &af, aliases,
187 if (rs->sr_err != LDAP_SUCCESS) {
191 oldsubs[1] = e->e_id;
194 BDB_IDL_ZERO( visited );
195 BDB_IDL_ZERO( newsubs );
198 ido = bdb_idl_first( oldsubs, &cursoro );
201 /* Set curscop to only the aliases in the current scope. Start with
202 * all the aliases, obtain the IDL for the current scope, and then
203 * get the intersection of these two IDLs. Add the current scope
204 * to the cumulative list of candidates.
206 BDB_IDL_CPY( curscop, aliases );
207 rs->sr_err = bdb_dn2idl( op, txn, &e->e_nname, BEI(e), subscop,
208 subscop2+BDB_IDL_DB_SIZE );
213 bdb_cache_return_entry_r (bdb, e, &locka);
215 if ( rs->sr_err == DB_LOCK_DEADLOCK )
218 BDB_IDL_CPY(subscop2, subscop);
219 rs->sr_err = bdb_idl_intersection(curscop, subscop);
220 bdb_idl_union( ids, subscop2 );
222 /* Dereference all of the aliases in the current scope. */
224 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
225 ida = bdb_idl_next(curscop, &cursora))
229 rs->sr_err = bdb_cache_find_id(op, txn,
230 ida, &ei, 0, &lockr );
231 if (rs->sr_err != LDAP_SUCCESS) {
232 if ( rs->sr_err == DB_LOCK_DEADLOCK )
234 if ( rs->sr_err == DB_LOCK_NOTGRANTED )
240 /* This should only happen if the curscop IDL has maxed out and
241 * turned into a range that spans IDs indiscriminately
243 if (!is_entry_alias(a)) {
244 bdb_cache_return_entry_r (bdb, a, &lockr);
248 /* Actually dereference the alias */
250 a = deref_base( op, rs, a, &matched, txn, &lockr,
253 /* If the target was not already in our current candidates,
254 * make note of it in the newsubs list. Also
255 * set it in the scopes list so that bdb_search
258 if (bdb_idl_insert(ids, a->e_id) == 0) {
259 bdb_idl_insert(newsubs, a->e_id);
260 bdb_idl_insert(scopes, a->e_id);
262 bdb_cache_return_entry_r( bdb, a, &lockr);
264 } else if ( rs->sr_err == DB_LOCK_DEADLOCK ) {
266 } else if (matched) {
267 /* Alias could not be dereferenced, or it deref'd to
268 * an ID we've already seen. Ignore it.
270 bdb_cache_return_entry_r( bdb, matched, &lockr );
274 /* If this is a OneLevel search, we're done; oldsubs only had one
275 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
277 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
279 ido = bdb_idl_next( oldsubs, &cursoro );
281 /* If we're done processing the old scopes, did we add any new
282 * scopes in this iteration? If so, go back and do those now.
285 if (BDB_IDL_IS_ZERO(newsubs)) break;
286 BDB_IDL_CPY(oldsubs, newsubs);
287 BDB_IDL_ZERO(newsubs);
289 ido = bdb_idl_first( oldsubs, &cursoro );
292 /* Find the entry corresponding to the next scope. If it can't
293 * be found, ignore it and move on. This should never happen;
294 * we should never see the ID of an entry that doesn't exist.
295 * Set the name so that the scope's IDL can be retrieved.
299 rs->sr_err = bdb_cache_find_id(op, txn, ido, &ei,
301 if ( rs->sr_err != LDAP_SUCCESS ) {
302 if ( rs->sr_err == DB_LOCK_DEADLOCK )
304 if ( rs->sr_err == DB_LOCK_NOTGRANTED )
314 bdb_search( Operation *op, SlapReply *rs )
316 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
319 ID candidates[BDB_IDL_UM_SIZE];
320 ID scopes[BDB_IDL_DB_SIZE];
321 Entry *e = NULL, base, *e_root;
322 Entry *matched = NULL;
324 AttributeName *attrs;
325 struct berval realbase = BER_BVNULL;
330 unsigned nentries = 0;
334 struct bdb_op_info *opinfo = NULL;
338 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
339 attrs = op->oq_search.rs_attrs;
341 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
342 if ( oex->oe_key == bdb )
345 opinfo = (struct bdb_op_info *) oex;
347 manageDSAit = get_manageDSAit( op );
349 if ( opinfo && opinfo->boi_txn ) {
350 ltid = opinfo->boi_txn;
352 rs->sr_err = bdb_reader_get( op, bdb->bi_dbenv, <id );
358 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
363 e_root = bdb->bi_cache.c_dntree.bei_e;
364 if ( op->o_req_ndn.bv_len == 0 ) {
365 /* DIT root special case */
366 ei = e_root->e_private;
367 rs->sr_err = LDAP_SUCCESS;
369 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
370 BDB_IDL_ZERO(candidates);
373 /* get entry with reader lock */
374 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei,
385 case DB_LOCK_DEADLOCK:
387 ltid->flags &= ~TXN_DEADLOCK;
390 opinfo->boi_err = rs->sr_err;
393 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
395 case DB_LOCK_NOTGRANTED:
398 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
402 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
403 if ( matched && is_entry_alias( matched )) {
406 stub.bv_val = op->o_req_ndn.bv_val;
407 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
408 e = deref_base( op, rs, matched, &matched, ltid, &lock,
411 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
413 bdb_cache_return_entry_r (bdb, e, &lock);
417 } else if ( e && is_entry_alias( e )) {
418 e = deref_base( op, rs, e, &matched, ltid, &lock,
424 struct berval matched_dn = BER_BVNULL;
426 if ( matched != NULL ) {
427 BerVarray erefs = NULL;
429 /* return referral only if "disclose"
430 * is granted on the object */
431 if ( ! access_allowed( op, matched,
432 slap_schema.si_ad_entry,
433 NULL, ACL_DISCLOSE, NULL ) )
435 rs->sr_err = LDAP_NO_SUCH_OBJECT;
438 ber_dupbv( &matched_dn, &matched->e_name );
440 erefs = is_entry_referral( matched )
441 ? get_entry_referrals( op, matched )
443 if ( rs->sr_err == DB_NOTFOUND )
444 rs->sr_err = LDAP_REFERRAL;
445 rs->sr_matched = matched_dn.bv_val;
448 #ifdef SLAP_ZONE_ALLOC
449 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
451 bdb_cache_return_entry_r (bdb, matched, &lock);
455 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
456 &op->o_req_dn, op->oq_search.rs_scope );
457 ber_bvarray_free( erefs );
461 #ifdef SLAP_ZONE_ALLOC
462 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
464 rs->sr_ref = referral_rewrite( default_referral,
465 NULL, &op->o_req_dn, op->oq_search.rs_scope );
466 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
469 send_ldap_result( op, rs );
472 ber_bvarray_free( rs->sr_ref );
475 if ( !BER_BVISNULL( &matched_dn ) ) {
476 ber_memfree( matched_dn.bv_val );
477 rs->sr_matched = NULL;
482 /* NOTE: __NEW__ "search" access is required
483 * on searchBase object */
484 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
485 NULL, ACL_SEARCH, NULL, &mask ) )
487 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
488 rs->sr_err = LDAP_NO_SUCH_OBJECT;
490 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
493 #ifdef SLAP_ZONE_ALLOC
494 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
497 bdb_cache_return_entry_r(bdb, e, &lock);
499 send_ldap_result( op, rs );
503 if ( !manageDSAit && e != e_root && is_entry_referral( e ) ) {
504 /* entry is a referral, don't allow add */
505 struct berval matched_dn = BER_BVNULL;
506 BerVarray erefs = NULL;
508 ber_dupbv( &matched_dn, &e->e_name );
509 erefs = get_entry_referrals( op, e );
511 rs->sr_err = LDAP_REFERRAL;
513 #ifdef SLAP_ZONE_ALLOC
514 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
516 bdb_cache_return_entry_r( bdb, e, &lock );
520 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
521 &op->o_req_dn, op->oq_search.rs_scope );
522 ber_bvarray_free( erefs );
525 rs->sr_text = "bad_referral object";
529 Debug( LDAP_DEBUG_TRACE,
530 LDAP_XSTRING(bdb_search) ": entry is referral\n",
533 rs->sr_matched = matched_dn.bv_val;
534 send_ldap_result( op, rs );
536 ber_bvarray_free( rs->sr_ref );
538 ber_memfree( matched_dn.bv_val );
539 rs->sr_matched = NULL;
543 if ( get_assert( op ) &&
544 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
546 rs->sr_err = LDAP_ASSERTION_FAILED;
547 #ifdef SLAP_ZONE_ALLOC
548 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
551 bdb_cache_return_entry_r(bdb, e, &lock);
553 send_ldap_result( op, rs );
557 /* compute it anyway; root does not use it */
558 stoptime = op->o_time + op->ors_tlimit;
560 /* need normalized dn below */
561 ber_dupbv( &realbase, &e->e_nname );
563 /* Copy info to base, must free entry before accessing the database
564 * in search_candidates, to avoid deadlocks.
566 base.e_private = e->e_private;
567 base.e_nname = realbase;
570 #ifdef SLAP_ZONE_ALLOC
571 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
574 bdb_cache_return_entry_r(bdb, e, &lock);
578 /* select candidates */
579 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
580 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
584 BDB_IDL_ZERO( candidates );
585 BDB_IDL_ZERO( scopes );
586 rs->sr_err = search_candidates( op, rs, &base,
587 ltid, candidates, scopes );
588 if ( rs->sr_err == DB_LOCK_DEADLOCK ) {
590 ltid->flags &= ~TXN_DEADLOCK;
593 opinfo->boi_err = rs->sr_err;
594 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
599 /* start cursor at beginning of candidates.
603 if ( candidates[0] == 0 ) {
604 Debug( LDAP_DEBUG_TRACE,
605 LDAP_XSTRING(bdb_search) ": no candidates\n",
611 /* if not root and candidates exceed to-be-checked entries, abort */
612 if ( op->ors_limit /* isroot == FALSE */ &&
613 op->ors_limit->lms_s_unchecked != -1 &&
614 BDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
616 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
617 send_ldap_result( op, rs );
618 rs->sr_err = LDAP_SUCCESS;
622 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
623 !op->ors_limit->lms_s_pr_hide )
625 tentries = BDB_IDL_N(candidates);
628 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
629 PagedResultsState *ps = op->o_pagedresults_state;
630 /* deferred cookie parsing */
631 rs->sr_err = parse_paged_cookie( op, rs );
632 if ( rs->sr_err != LDAP_SUCCESS ) {
633 send_ldap_result( op, rs );
637 cursor = (ID) ps->ps_cookie;
638 if ( cursor && ps->ps_size == 0 ) {
639 rs->sr_err = LDAP_SUCCESS;
640 rs->sr_text = "search abandoned by pagedResult size=0";
641 send_ldap_result( op, rs );
644 id = bdb_idl_first( candidates, &cursor );
646 Debug( LDAP_DEBUG_TRACE,
647 LDAP_XSTRING(bdb_search)
648 ": no paged results candidates\n",
650 send_paged_response( op, rs, &lastid, 0 );
652 rs->sr_err = LDAP_OTHER;
655 nentries = ps->ps_count;
656 if ( id == (ID)ps->ps_cookie )
657 id = bdb_idl_next( candidates, &cursor );
661 for ( id = bdb_idl_first( candidates, &cursor );
662 id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
668 /* check for abandon */
669 if ( op->o_abandon ) {
670 rs->sr_err = SLAPD_ABANDON;
671 send_ldap_result( op, rs );
675 /* mostly needed by internal searches,
676 * e.g. related to syncrepl, for whom
677 * abandon does not get set... */
678 if ( slapd_shutdown ) {
679 rs->sr_err = LDAP_UNAVAILABLE;
680 send_ldap_disconnect( op, rs );
684 /* check time limit */
685 if ( op->ors_tlimit != SLAP_NO_LIMIT
686 && slap_get_time() > stoptime )
688 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
689 rs->sr_ref = rs->sr_v2ref;
690 send_ldap_result( op, rs );
691 rs->sr_err = LDAP_SUCCESS;
695 /* If we inspect more entries than will
696 * fit into the entry cache, stop caching
697 * any subsequent entries
700 if ( nentries > bdb->bi_cache.c_maxsize && !idflag ) {
705 /* get the entry with reader lock */
707 rs->sr_err = bdb_cache_find_id( op, ltid,
708 id, &ei, idflag, &lock );
710 if (rs->sr_err == LDAP_BUSY) {
711 rs->sr_text = "ldap server busy";
712 send_ldap_result( op, rs );
715 } else if ( rs->sr_err == DB_LOCK_DEADLOCK ) {
717 ltid->flags &= ~TXN_DEADLOCK;
718 goto fetch_entry_retry;
720 opinfo->boi_err = rs->sr_err;
721 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
724 } else if ( rs->sr_err == DB_LOCK_NOTGRANTED )
726 goto fetch_entry_retry;
727 } else if ( rs->sr_err == LDAP_OTHER ) {
728 rs->sr_text = "internal error";
729 send_ldap_result( op, rs );
733 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
740 if( !BDB_IDL_IS_RANGE(candidates) ) {
741 /* only complain for non-range IDLs */
742 Debug( LDAP_DEBUG_TRACE,
743 LDAP_XSTRING(bdb_search)
744 ": candidate %ld not found\n",
753 if ( is_entry_subentry( e ) ) {
754 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
755 if(!get_subentries_visibility( op )) {
756 /* only subentries are visible */
760 } else if ( get_subentries( op ) &&
761 !get_subentries_visibility( op ))
763 /* only subentries are visible */
767 } else if ( get_subentries_visibility( op )) {
768 /* only subentries are visible */
772 /* Does this candidate actually satisfy the search scope?
774 * Note that we don't lock access to the bei_parent pointer.
775 * Since only leaf nodes can be deleted, the parent of any
776 * node will always be a valid node. Also since we have
777 * a Read lock on the data, it cannot be renamed out of the
778 * scope while we are looking at it, and unless we're using
779 * BDB_HIER, its parents cannot be moved either.
782 switch( op->ors_scope ) {
783 case LDAP_SCOPE_BASE:
784 /* This is always true, yes? */
785 if ( id == base.e_id ) scopeok = 1;
788 case LDAP_SCOPE_ONELEVEL:
789 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
792 #ifdef LDAP_SCOPE_CHILDREN
793 case LDAP_SCOPE_CHILDREN:
794 if ( id == base.e_id ) break;
797 case LDAP_SCOPE_SUBTREE: {
799 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
800 if ( tmp->bei_id == base.e_id ) {
808 /* aliases were already dereferenced in candidate list */
809 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
810 /* but if the search base is an alias, and we didn't
811 * deref it when finding, return it.
813 if ( is_entry_alias(e) &&
814 ((op->ors_deref & LDAP_DEREF_FINDING) ||
815 !bvmatch(&e->e_nname, &op->o_req_ndn)))
820 /* scopes is only non-empty for onelevel or subtree */
821 if ( !scopeok && BDB_IDL_N(scopes) ) {
823 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
824 x = bdb_idl_search( scopes, e->e_id );
825 if ( scopes[x] == e->e_id ) scopeok = 1;
827 /* subtree, walk up the tree */
828 EntryInfo *tmp = BEI(e);
829 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
830 x = bdb_idl_search( scopes, tmp->bei_id );
831 if ( scopes[x] == tmp->bei_id ) {
840 /* Not in scope, ignore it */
843 Debug( LDAP_DEBUG_TRACE,
844 LDAP_XSTRING(bdb_search)
845 ": %ld scope not okay\n",
851 * if it's a referral, add it to the list of referrals. only do
852 * this for non-base searches, and don't check the filter
853 * explicitly here since it's only a candidate anyway.
855 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
856 && is_entry_referral( e ) )
858 struct bdb_op_info bois;
859 struct bdb_lock_info blis;
860 BerVarray erefs = get_entry_referrals( op, e );
861 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
862 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
863 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
865 /* Must set lockinfo so that entry_release will work */
867 bois.boi_oe.oe_key = bdb;
870 bois.boi_acl_cache = op->o_do_not_cache;
871 bois.boi_flag = BOI_DONTFREE;
872 bois.boi_locks = &blis;
873 blis.bli_next = NULL;
874 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &bois.boi_oe,
877 blis.bli_next = opinfo->boi_locks;
878 opinfo->boi_locks = &blis;
880 blis.bli_id = e->e_id;
881 blis.bli_lock = lock;
882 blis.bli_flag = BLI_DONTFREE;
884 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
886 send_search_reference( op, rs );
888 if ( blis.bli_flag ) {
889 #ifdef SLAP_ZONE_ALLOC
890 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
892 bdb_cache_return_entry_r(bdb, e, &lock);
894 opinfo->boi_locks = blis.bli_next;
896 LDAP_SLIST_REMOVE( &op->o_extra, &bois.boi_oe,
903 ber_bvarray_free( rs->sr_ref );
904 ber_bvarray_free( erefs );
910 if ( !manageDSAit && is_entry_glue( e )) {
914 /* if it matches the filter and scope, send it */
915 rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
917 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
918 /* check size limit */
919 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
920 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
921 #ifdef SLAP_ZONE_ALLOC
922 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
924 bdb_cache_return_entry_r( bdb, e, &lock );
926 send_paged_response( op, rs, &lastid, tentries );
933 struct bdb_op_info bois;
934 struct bdb_lock_info blis;
936 /* Must set lockinfo so that entry_release will work */
938 bois.boi_oe.oe_key = bdb;
941 bois.boi_acl_cache = op->o_do_not_cache;
942 bois.boi_flag = BOI_DONTFREE;
943 bois.boi_locks = &blis;
944 blis.bli_next = NULL;
945 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &bois.boi_oe,
948 blis.bli_next = opinfo->boi_locks;
949 opinfo->boi_locks = &blis;
951 blis.bli_id = e->e_id;
952 blis.bli_lock = lock;
953 blis.bli_flag = BLI_DONTFREE;
956 rs->sr_attrs = op->oq_search.rs_attrs;
957 rs->sr_operational_attrs = NULL;
959 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
960 rs->sr_err = LDAP_SUCCESS;
961 rs->sr_err = send_search_entry( op, rs );
963 /* send_search_entry will usually free it.
964 * an overlay might leave its own copy here;
965 * bli_flag will be 0 if lock was already released.
967 if ( blis.bli_flag ) {
968 #ifdef SLAP_ZONE_ALLOC
969 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
971 bdb_cache_return_entry_r(bdb, e, &lock);
973 opinfo->boi_locks = blis.bli_next;
975 LDAP_SLIST_REMOVE( &op->o_extra, &bois.boi_oe,
982 switch ( rs->sr_err ) {
983 case LDAP_SUCCESS: /* entry sent ok */
985 default: /* entry not sent */
987 case LDAP_UNAVAILABLE:
988 case LDAP_SIZELIMIT_EXCEEDED:
989 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
990 rs->sr_ref = rs->sr_v2ref;
991 send_ldap_result( op, rs );
992 rs->sr_err = LDAP_SUCCESS;
995 rs->sr_err = LDAP_OTHER;
1002 Debug( LDAP_DEBUG_TRACE,
1003 LDAP_XSTRING(bdb_search)
1004 ": %ld does not match filter\n",
1010 /* free reader lock */
1011 #ifdef SLAP_ZONE_ALLOC
1012 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
1014 bdb_cache_return_entry_r( bdb, e , &lock );
1016 rs->sr_entry = NULL;
1021 rs->sr_ctrls = NULL;
1022 rs->sr_ref = rs->sr_v2ref;
1023 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1024 rs->sr_rspoid = NULL;
1025 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
1026 send_paged_response( op, rs, NULL, 0 );
1028 send_ldap_result( op, rs );
1031 rs->sr_err = LDAP_SUCCESS;
1034 if( rs->sr_v2ref ) {
1035 ber_bvarray_free( rs->sr_v2ref );
1036 rs->sr_v2ref = NULL;
1038 if( realbase.bv_val ) ch_free( realbase.bv_val );
1044 static int base_candidate(
1049 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1050 e->e_nname.bv_val, (long) e->e_id, 0);
1057 /* Look for "objectClass Present" in this filter.
1058 * Also count depth of filter tree while we're at it.
1060 static int oc_filter(
1067 assert( f != NULL );
1069 if( cur > *max ) *max = cur;
1071 switch( f->f_choice ) {
1072 case LDAP_FILTER_PRESENT:
1073 if (f->f_desc == slap_schema.si_ad_objectClass) {
1078 case LDAP_FILTER_AND:
1079 case LDAP_FILTER_OR:
1081 for ( f=f->f_and; f; f=f->f_next ) {
1082 (void) oc_filter(f, cur, max);
1092 static void search_stack_free( void *key, void *data )
1094 ber_memfree_x(data, NULL);
1097 static void *search_stack( Operation *op )
1099 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1102 if ( op->o_threadctx ) {
1103 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
1106 ret = bdb->bi_search_stack;
1110 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1112 if ( op->o_threadctx ) {
1113 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1114 ret, search_stack_free, NULL, NULL );
1116 bdb->bi_search_stack = ret;
1122 static int search_candidates(
1130 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1132 Filter f, rf, xf, nf;
1134 AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1136 AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1139 * This routine takes as input a filter (user-filter)
1140 * and rewrites it as follows:
1141 * (&(scope=DN)[(objectClass=subentry)]
1142 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1145 Debug(LDAP_DEBUG_TRACE,
1146 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1147 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1149 xf.f_or = op->oq_search.rs_filter;
1150 xf.f_choice = LDAP_FILTER_OR;
1153 /* If the user's filter uses objectClass=*,
1154 * these clauses are redundant.
1156 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1157 && !get_subentries_visibility(op)) {
1158 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1159 /* match referral objects */
1160 struct berval bv_ref = BER_BVC( "referral" );
1161 rf.f_choice = LDAP_FILTER_EQUALITY;
1163 rf.f_av_desc = slap_schema.si_ad_objectClass;
1164 rf.f_av_value = bv_ref;
1165 rf.f_next = xf.f_or;
1172 f.f_choice = LDAP_FILTER_AND;
1174 /* Dummy; we compute scope separately now */
1175 nf.f_choice = SLAPD_FILTER_COMPUTED;
1176 nf.f_result = LDAP_SUCCESS;
1177 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1178 ? op->oq_search.rs_filter : &xf ;
1179 /* Filter depth increased again, adding dummy clause */
1182 if( get_subentries_visibility( op ) ) {
1183 struct berval bv_subentry = BER_BVC( "subentry" );
1184 sf.f_choice = LDAP_FILTER_EQUALITY;
1185 sf.f_ava = &aa_subentry;
1186 sf.f_av_desc = slap_schema.si_ad_objectClass;
1187 sf.f_av_value = bv_subentry;
1188 sf.f_next = nf.f_next;
1192 /* Allocate IDL stack, plus 1 more for former tmp */
1193 if ( depth+1 > bdb->bi_search_stack_depth ) {
1194 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1196 stack = search_stack( op );
1199 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1200 rc = search_aliases( op, rs, e, txn, ids, scopes, stack );
1202 rc = bdb_dn2idl( op, txn, &e->e_nname, BEI(e), ids, stack );
1205 if ( rc == LDAP_SUCCESS ) {
1206 rc = bdb_filter_candidates( op, txn, &f, ids,
1207 stack, stack+BDB_IDL_UM_SIZE );
1210 if ( depth+1 > bdb->bi_search_stack_depth ) {
1215 Debug(LDAP_DEBUG_TRACE,
1216 "bdb_search_candidates: failed (rc=%d)\n",
1220 Debug(LDAP_DEBUG_TRACE,
1221 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1223 (long) BDB_IDL_FIRST(ids),
1224 (long) BDB_IDL_LAST(ids) );
1231 parse_paged_cookie( Operation *op, SlapReply *rs )
1233 int rc = LDAP_SUCCESS;
1234 PagedResultsState *ps = op->o_pagedresults_state;
1236 /* this function must be invoked only if the pagedResults
1237 * control has been detected, parsed and partially checked
1238 * by the frontend */
1239 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1241 /* cookie decoding/checks deferred to backend... */
1242 if ( ps->ps_cookieval.bv_len ) {
1243 PagedResultsCookie reqcookie;
1244 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1246 rs->sr_text = "paged results cookie is invalid";
1247 rc = LDAP_PROTOCOL_ERROR;
1251 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1253 if ( reqcookie > ps->ps_cookie ) {
1255 rs->sr_text = "paged results cookie is invalid";
1256 rc = LDAP_PROTOCOL_ERROR;
1259 } else if ( reqcookie < ps->ps_cookie ) {
1260 rs->sr_text = "paged results cookie is invalid or old";
1261 rc = LDAP_UNWILLING_TO_PERFORM;
1273 send_paged_response(
1279 LDAPControl *ctrls[2];
1280 BerElementBuffer berbuf;
1281 BerElement *ber = (BerElement *)&berbuf;
1282 PagedResultsCookie respcookie;
1283 struct berval cookie;
1285 Debug(LDAP_DEBUG_ARGS,
1286 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1287 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1291 ber_init2( ber, NULL, LBER_USE_DER );
1294 respcookie = ( PagedResultsCookie )(*lastid);
1295 cookie.bv_len = sizeof( respcookie );
1296 cookie.bv_val = (char *)&respcookie;
1299 respcookie = ( PagedResultsCookie )0;
1300 BER_BVSTR( &cookie, "" );
1303 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1304 op->o_conn->c_pagedresults_state.ps_count =
1305 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1308 /* return size of 0 -- no estimate */
1309 ber_printf( ber, "{iO}", 0, &cookie );
1311 ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1312 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1316 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1317 ctrls[0]->ldctl_iscritical = 0;
1319 slap_add_ctrls( op, rs, ctrls );
1320 rs->sr_err = LDAP_SUCCESS;
1321 send_ldap_result( op, rs );
1324 (void) ber_free_buf( ber );