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,
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;
329 int tentries = 0, nentries = 0;
333 struct bdb_op_info *opinfo = NULL;
337 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
338 attrs = op->oq_search.rs_attrs;
340 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
341 if ( oex->oe_key == bdb )
344 opinfo = (struct bdb_op_info *) oex;
346 manageDSAit = get_manageDSAit( op );
348 if ( opinfo && opinfo->boi_txn ) {
349 ltid = opinfo->boi_txn;
351 rs->sr_err = bdb_reader_get( op, bdb->bi_dbenv, <id );
357 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
362 e_root = bdb->bi_cache.c_dntree.bei_e;
363 if ( op->o_req_ndn.bv_len == 0 ) {
364 /* DIT root special case */
365 ei = e_root->e_private;
366 rs->sr_err = LDAP_SUCCESS;
368 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
369 BDB_IDL_ZERO(candidates);
372 /* get entry with reader lock */
373 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei,
384 case DB_LOCK_DEADLOCK:
386 ltid->flags &= ~TXN_DEADLOCK;
389 opinfo->boi_err = rs->sr_err;
392 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
394 case DB_LOCK_NOTGRANTED:
397 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
401 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
402 if ( matched && is_entry_alias( matched )) {
405 stub.bv_val = op->o_req_ndn.bv_val;
406 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
407 e = deref_base( op, rs, matched, &matched, ltid, &lock,
410 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
412 bdb_cache_return_entry_r (bdb, e, &lock);
416 } else if ( e && is_entry_alias( e )) {
417 e = deref_base( op, rs, e, &matched, ltid, &lock,
423 struct berval matched_dn = BER_BVNULL;
425 if ( matched != NULL ) {
426 BerVarray erefs = NULL;
428 /* return referral only if "disclose"
429 * is granted on the object */
430 if ( ! access_allowed( op, matched,
431 slap_schema.si_ad_entry,
432 NULL, ACL_DISCLOSE, NULL ) )
434 rs->sr_err = LDAP_NO_SUCH_OBJECT;
437 ber_dupbv( &matched_dn, &matched->e_name );
439 erefs = is_entry_referral( matched )
440 ? get_entry_referrals( op, matched )
442 if ( rs->sr_err == DB_NOTFOUND )
443 rs->sr_err = LDAP_REFERRAL;
444 rs->sr_matched = matched_dn.bv_val;
447 #ifdef SLAP_ZONE_ALLOC
448 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
450 bdb_cache_return_entry_r (bdb, matched, &lock);
454 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
455 &op->o_req_dn, op->oq_search.rs_scope );
456 ber_bvarray_free( erefs );
460 #ifdef SLAP_ZONE_ALLOC
461 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
463 rs->sr_ref = referral_rewrite( default_referral,
464 NULL, &op->o_req_dn, op->oq_search.rs_scope );
465 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
468 send_ldap_result( op, rs );
471 ber_bvarray_free( rs->sr_ref );
474 if ( !BER_BVISNULL( &matched_dn ) ) {
475 ber_memfree( matched_dn.bv_val );
476 rs->sr_matched = NULL;
481 /* NOTE: __NEW__ "search" access is required
482 * on searchBase object */
483 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
484 NULL, ACL_SEARCH, NULL, &mask ) )
486 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
487 rs->sr_err = LDAP_NO_SUCH_OBJECT;
489 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
492 #ifdef SLAP_ZONE_ALLOC
493 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
496 bdb_cache_return_entry_r(bdb, e, &lock);
498 send_ldap_result( op, rs );
502 if ( !manageDSAit && e != e_root && is_entry_referral( e ) ) {
503 /* entry is a referral, don't allow add */
504 struct berval matched_dn = BER_BVNULL;
505 BerVarray erefs = NULL;
507 ber_dupbv( &matched_dn, &e->e_name );
508 erefs = get_entry_referrals( op, e );
510 rs->sr_err = LDAP_REFERRAL;
512 #ifdef SLAP_ZONE_ALLOC
513 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
515 bdb_cache_return_entry_r( bdb, e, &lock );
519 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
520 &op->o_req_dn, op->oq_search.rs_scope );
521 ber_bvarray_free( erefs );
524 rs->sr_text = "bad_referral object";
528 Debug( LDAP_DEBUG_TRACE,
529 LDAP_XSTRING(bdb_search) ": entry is referral\n",
532 rs->sr_matched = matched_dn.bv_val;
533 send_ldap_result( op, rs );
535 ber_bvarray_free( rs->sr_ref );
537 ber_memfree( matched_dn.bv_val );
538 rs->sr_matched = NULL;
542 if ( get_assert( op ) &&
543 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
545 rs->sr_err = LDAP_ASSERTION_FAILED;
546 #ifdef SLAP_ZONE_ALLOC
547 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
550 bdb_cache_return_entry_r(bdb, e, &lock);
552 send_ldap_result( op, rs );
556 /* compute it anyway; root does not use it */
557 stoptime = op->o_time + op->ors_tlimit;
559 /* need normalized dn below */
560 ber_dupbv( &realbase, &e->e_nname );
562 /* Copy info to base, must free entry before accessing the database
563 * in search_candidates, to avoid deadlocks.
565 base.e_private = e->e_private;
566 base.e_nname = realbase;
569 #ifdef SLAP_ZONE_ALLOC
570 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
573 bdb_cache_return_entry_r(bdb, e, &lock);
577 /* select candidates */
578 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
579 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
583 BDB_IDL_ZERO( candidates );
584 BDB_IDL_ZERO( scopes );
585 rs->sr_err = search_candidates( op, rs, &base,
586 ltid, candidates, scopes );
587 if ( rs->sr_err == DB_LOCK_DEADLOCK ) {
589 ltid->flags &= ~TXN_DEADLOCK;
592 opinfo->boi_err = rs->sr_err;
593 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
598 /* start cursor at beginning of candidates.
602 if ( candidates[0] == 0 ) {
603 Debug( LDAP_DEBUG_TRACE,
604 LDAP_XSTRING(bdb_search) ": no candidates\n",
610 /* if not root and candidates exceed to-be-checked entries, abort */
611 if ( op->ors_limit /* isroot == FALSE */ &&
612 op->ors_limit->lms_s_unchecked != -1 &&
613 BDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
615 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
616 send_ldap_result( op, rs );
617 rs->sr_err = LDAP_SUCCESS;
621 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
622 !op->ors_limit->lms_s_pr_hide )
624 tentries = BDB_IDL_N(candidates);
627 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
628 PagedResultsState *ps = op->o_pagedresults_state;
629 /* deferred cookie parsing */
630 rs->sr_err = parse_paged_cookie( op, rs );
631 if ( rs->sr_err != LDAP_SUCCESS ) {
632 send_ldap_result( op, rs );
636 cursor = (ID) ps->ps_cookie;
637 if ( cursor && ps->ps_size == 0 ) {
638 rs->sr_err = LDAP_SUCCESS;
639 rs->sr_text = "search abandoned by pagedResult size=0";
640 send_ldap_result( op, rs );
643 id = bdb_idl_first( candidates, &cursor );
645 Debug( LDAP_DEBUG_TRACE,
646 LDAP_XSTRING(bdb_search)
647 ": no paged results candidates\n",
649 send_paged_response( op, rs, &lastid, 0 );
651 rs->sr_err = LDAP_OTHER;
654 nentries = ps->ps_count;
655 if ( id == (ID)ps->ps_cookie )
656 id = bdb_idl_next( candidates, &cursor );
660 for ( id = bdb_idl_first( candidates, &cursor );
661 id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
667 /* check for abandon */
668 if ( op->o_abandon ) {
669 rs->sr_err = SLAPD_ABANDON;
673 /* mostly needed by internal searches,
674 * e.g. related to syncrepl, for whom
675 * abandon does not get set... */
676 if ( slapd_shutdown ) {
677 rs->sr_err = LDAP_UNAVAILABLE;
678 send_ldap_disconnect( op, rs );
682 /* check time limit */
683 if ( op->ors_tlimit != SLAP_NO_LIMIT
684 && slap_get_time() > stoptime )
686 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
687 rs->sr_ref = rs->sr_v2ref;
688 send_ldap_result( op, rs );
689 rs->sr_err = LDAP_SUCCESS;
693 /* If we inspect more entries than will
694 * fit into the entry cache, stop caching
695 * any subsequent entries
698 if ( nentries > bdb->bi_cache.c_maxsize && !idflag )
702 /* get the entry with reader lock */
704 rs->sr_err = bdb_cache_find_id( op, ltid,
705 id, &ei, idflag, &lock );
707 if (rs->sr_err == LDAP_BUSY) {
708 rs->sr_text = "ldap server busy";
709 send_ldap_result( op, rs );
712 } else if ( rs->sr_err == DB_LOCK_DEADLOCK ) {
714 ltid->flags &= ~TXN_DEADLOCK;
715 goto fetch_entry_retry;
717 opinfo->boi_err = rs->sr_err;
718 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
721 } else if ( rs->sr_err == DB_LOCK_NOTGRANTED )
723 goto fetch_entry_retry;
724 } else if ( rs->sr_err == LDAP_OTHER ) {
725 rs->sr_text = "internal error";
726 send_ldap_result( op, rs );
730 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
737 if( !BDB_IDL_IS_RANGE(candidates) ) {
738 /* only complain for non-range IDLs */
739 Debug( LDAP_DEBUG_TRACE,
740 LDAP_XSTRING(bdb_search)
741 ": candidate %ld not found\n",
750 if ( is_entry_subentry( e ) ) {
751 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
752 if(!get_subentries_visibility( op )) {
753 /* only subentries are visible */
757 } else if ( get_subentries( op ) &&
758 !get_subentries_visibility( op ))
760 /* only subentries are visible */
764 } else if ( get_subentries_visibility( op )) {
765 /* only subentries are visible */
769 /* Does this candidate actually satisfy the search scope?
771 * Note that we don't lock access to the bei_parent pointer.
772 * Since only leaf nodes can be deleted, the parent of any
773 * node will always be a valid node. Also since we have
774 * a Read lock on the data, it cannot be renamed out of the
775 * scope while we are looking at it, and unless we're using
776 * BDB_HIER, its parents cannot be moved either.
779 switch( op->ors_scope ) {
780 case LDAP_SCOPE_BASE:
781 /* This is always true, yes? */
782 if ( id == base.e_id ) scopeok = 1;
785 case LDAP_SCOPE_ONELEVEL:
786 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
789 #ifdef LDAP_SCOPE_CHILDREN
790 case LDAP_SCOPE_CHILDREN:
791 if ( id == base.e_id ) break;
794 case LDAP_SCOPE_SUBTREE: {
796 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
797 if ( tmp->bei_id == base.e_id ) {
805 /* aliases were already dereferenced in candidate list */
806 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
807 /* but if the search base is an alias, and we didn't
808 * deref it when finding, return it.
810 if ( is_entry_alias(e) &&
811 ((op->ors_deref & LDAP_DEREF_FINDING) ||
812 !bvmatch(&e->e_nname, &op->o_req_ndn)))
817 /* scopes is only non-empty for onelevel or subtree */
818 if ( !scopeok && BDB_IDL_N(scopes) ) {
820 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
821 x = bdb_idl_search( scopes, e->e_id );
822 if ( scopes[x] == e->e_id ) scopeok = 1;
824 /* subtree, walk up the tree */
825 EntryInfo *tmp = BEI(e);
826 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
827 x = bdb_idl_search( scopes, tmp->bei_id );
828 if ( scopes[x] == tmp->bei_id ) {
837 /* Not in scope, ignore it */
840 Debug( LDAP_DEBUG_TRACE,
841 LDAP_XSTRING(bdb_search)
842 ": %ld scope not okay\n",
848 * if it's a referral, add it to the list of referrals. only do
849 * this for non-base searches, and don't check the filter
850 * explicitly here since it's only a candidate anyway.
852 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
853 && is_entry_referral( e ) )
855 BerVarray erefs = get_entry_referrals( op, e );
856 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
857 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
858 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
860 /* free reader lock */
861 #ifdef SLAP_ZONE_ALLOC
862 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
864 bdb_cache_return_entry_r( bdb, e, &lock );
867 send_search_reference( op, rs );
869 ber_bvarray_free( rs->sr_ref );
870 ber_bvarray_free( erefs );
876 if ( !manageDSAit && is_entry_glue( e )) {
880 /* if it matches the filter and scope, send it */
881 rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
883 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
884 /* check size limit */
885 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
886 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
887 #ifdef SLAP_ZONE_ALLOC
888 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
890 bdb_cache_return_entry_r( bdb, e, &lock );
892 send_paged_response( op, rs, &lastid, tentries );
899 struct bdb_op_info bois;
900 struct bdb_lock_info blis;
902 /* Must set lockinfo so that entry_release will work */
904 bois.boi_oe.oe_key = bdb;
907 bois.boi_acl_cache = op->o_do_not_cache;
908 bois.boi_flag = BOI_DONTFREE;
909 bois.boi_locks = &blis;
910 blis.bli_next = NULL;
911 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &bois.boi_oe,
914 blis.bli_next = opinfo->boi_locks;
915 opinfo->boi_locks = &blis;
917 blis.bli_id = e->e_id;
918 blis.bli_lock = lock;
919 blis.bli_flag = BLI_DONTFREE;
922 rs->sr_attrs = op->oq_search.rs_attrs;
923 rs->sr_operational_attrs = NULL;
925 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
926 rs->sr_err = LDAP_SUCCESS;
927 rs->sr_err = send_search_entry( op, rs );
929 /* send_search_entry will usually free it.
930 * an overlay might leave its own copy here;
931 * bli_flag will be 0 if lock was already released.
933 if ( blis.bli_flag ) {
934 #ifdef SLAP_ZONE_ALLOC
935 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
937 bdb_cache_return_entry_r(bdb, e, &lock);
939 opinfo->boi_locks = blis.bli_next;
941 LDAP_SLIST_REMOVE( &op->o_extra, &bois.boi_oe,
948 switch ( rs->sr_err ) {
949 case LDAP_SUCCESS: /* entry sent ok */
951 default: /* entry not sent */
953 case LDAP_UNAVAILABLE:
954 case LDAP_SIZELIMIT_EXCEEDED:
955 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
956 rs->sr_ref = rs->sr_v2ref;
957 send_ldap_result( op, rs );
958 rs->sr_err = LDAP_SUCCESS;
961 rs->sr_err = LDAP_OTHER;
968 Debug( LDAP_DEBUG_TRACE,
969 LDAP_XSTRING(bdb_search)
970 ": %ld does not match filter\n",
976 /* free reader lock */
977 #ifdef SLAP_ZONE_ALLOC
978 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
980 bdb_cache_return_entry_r( bdb, e , &lock );
988 rs->sr_ref = rs->sr_v2ref;
989 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
990 rs->sr_rspoid = NULL;
991 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
992 send_paged_response( op, rs, NULL, 0 );
994 send_ldap_result( op, rs );
997 rs->sr_err = LDAP_SUCCESS;
1000 if( rs->sr_v2ref ) {
1001 ber_bvarray_free( rs->sr_v2ref );
1002 rs->sr_v2ref = NULL;
1004 if( realbase.bv_val ) ch_free( realbase.bv_val );
1010 static int base_candidate(
1015 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1016 e->e_nname.bv_val, (long) e->e_id, 0);
1023 /* Look for "objectClass Present" in this filter.
1024 * Also count depth of filter tree while we're at it.
1026 static int oc_filter(
1033 assert( f != NULL );
1035 if( cur > *max ) *max = cur;
1037 switch( f->f_choice ) {
1038 case LDAP_FILTER_PRESENT:
1039 if (f->f_desc == slap_schema.si_ad_objectClass) {
1044 case LDAP_FILTER_AND:
1045 case LDAP_FILTER_OR:
1047 for ( f=f->f_and; f; f=f->f_next ) {
1048 (void) oc_filter(f, cur, max);
1058 static void search_stack_free( void *key, void *data )
1060 ber_memfree_x(data, NULL);
1063 static void *search_stack( Operation *op )
1065 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1068 if ( op->o_threadctx ) {
1069 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
1072 ret = bdb->bi_search_stack;
1076 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1078 if ( op->o_threadctx ) {
1079 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1080 ret, search_stack_free, NULL, NULL );
1082 bdb->bi_search_stack = ret;
1088 static int search_candidates(
1096 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1098 Filter f, rf, xf, nf;
1100 AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1102 AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1105 * This routine takes as input a filter (user-filter)
1106 * and rewrites it as follows:
1107 * (&(scope=DN)[(objectClass=subentry)]
1108 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1111 Debug(LDAP_DEBUG_TRACE,
1112 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1113 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1115 xf.f_or = op->oq_search.rs_filter;
1116 xf.f_choice = LDAP_FILTER_OR;
1119 /* If the user's filter uses objectClass=*,
1120 * these clauses are redundant.
1122 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1123 && !get_subentries_visibility(op)) {
1124 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1125 /* match referral objects */
1126 struct berval bv_ref = BER_BVC( "referral" );
1127 rf.f_choice = LDAP_FILTER_EQUALITY;
1129 rf.f_av_desc = slap_schema.si_ad_objectClass;
1130 rf.f_av_value = bv_ref;
1131 rf.f_next = xf.f_or;
1138 f.f_choice = LDAP_FILTER_AND;
1140 /* Dummy; we compute scope separately now */
1141 nf.f_choice = SLAPD_FILTER_COMPUTED;
1142 nf.f_result = LDAP_SUCCESS;
1143 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1144 ? op->oq_search.rs_filter : &xf ;
1145 /* Filter depth increased again, adding dummy clause */
1148 if( get_subentries_visibility( op ) ) {
1149 struct berval bv_subentry = BER_BVC( "subentry" );
1150 sf.f_choice = LDAP_FILTER_EQUALITY;
1151 sf.f_ava = &aa_subentry;
1152 sf.f_av_desc = slap_schema.si_ad_objectClass;
1153 sf.f_av_value = bv_subentry;
1154 sf.f_next = nf.f_next;
1158 /* Allocate IDL stack, plus 1 more for former tmp */
1159 if ( depth+1 > bdb->bi_search_stack_depth ) {
1160 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1162 stack = search_stack( op );
1165 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1166 rc = search_aliases( op, rs, e, txn, ids, scopes, stack );
1168 rc = bdb_dn2idl( op, txn, &e->e_nname, BEI(e), ids, stack );
1171 if ( rc == LDAP_SUCCESS ) {
1172 rc = bdb_filter_candidates( op, txn, &f, ids,
1173 stack, stack+BDB_IDL_UM_SIZE );
1176 if ( depth+1 > bdb->bi_search_stack_depth ) {
1181 Debug(LDAP_DEBUG_TRACE,
1182 "bdb_search_candidates: failed (rc=%d)\n",
1186 Debug(LDAP_DEBUG_TRACE,
1187 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1189 (long) BDB_IDL_FIRST(ids),
1190 (long) BDB_IDL_LAST(ids) );
1197 parse_paged_cookie( Operation *op, SlapReply *rs )
1199 int rc = LDAP_SUCCESS;
1200 PagedResultsState *ps = op->o_pagedresults_state;
1202 /* this function must be invoked only if the pagedResults
1203 * control has been detected, parsed and partially checked
1204 * by the frontend */
1205 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1207 /* cookie decoding/checks deferred to backend... */
1208 if ( ps->ps_cookieval.bv_len ) {
1209 PagedResultsCookie reqcookie;
1210 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1212 rs->sr_text = "paged results cookie is invalid";
1213 rc = LDAP_PROTOCOL_ERROR;
1217 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1219 if ( reqcookie > ps->ps_cookie ) {
1221 rs->sr_text = "paged results cookie is invalid";
1222 rc = LDAP_PROTOCOL_ERROR;
1225 } else if ( reqcookie < ps->ps_cookie ) {
1226 rs->sr_text = "paged results cookie is invalid or old";
1227 rc = LDAP_UNWILLING_TO_PERFORM;
1239 send_paged_response(
1245 LDAPControl ctrl, *ctrls[2];
1246 BerElementBuffer berbuf;
1247 BerElement *ber = (BerElement *)&berbuf;
1248 PagedResultsCookie respcookie;
1249 struct berval cookie;
1251 Debug(LDAP_DEBUG_ARGS,
1252 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1253 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1255 BER_BVZERO( &ctrl.ldctl_value );
1259 ber_init2( ber, NULL, LBER_USE_DER );
1262 respcookie = ( PagedResultsCookie )(*lastid);
1263 cookie.bv_len = sizeof( respcookie );
1264 cookie.bv_val = (char *)&respcookie;
1267 respcookie = ( PagedResultsCookie )0;
1268 BER_BVSTR( &cookie, "" );
1271 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1272 op->o_conn->c_pagedresults_state.ps_count =
1273 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1276 /* return size of 0 -- no estimate */
1277 ber_printf( ber, "{iO}", 0, &cookie );
1279 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1283 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1284 ctrls[0]->ldctl_iscritical = 0;
1286 rs->sr_ctrls = ctrls;
1287 rs->sr_err = LDAP_SUCCESS;
1288 send_ldap_result( op, rs );
1289 rs->sr_ctrls = NULL;
1292 (void) ber_free_buf( ber );