1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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";
67 while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
68 /* Remember the last entry we looked at, so we can
73 /* If this is part of a subtree or onelevel search,
74 * have we seen this ID before? If so, quit.
76 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
81 /* If we've seen this ID during this deref iteration,
84 if ( bdb_idl_insert( tmp, e->e_id ) ) {
85 rs->sr_err = LDAP_ALIAS_PROBLEM;
86 rs->sr_text = "circular alias";
91 /* If there was a problem getting the aliasedObjectName,
92 * get_alias_dn will have set the error status.
94 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
99 rs->sr_err = bdb_dn2entry( op, NULL, &ndn, &ei,
109 rs->sr_err = LDAP_ALIAS_PROBLEM;
110 rs->sr_text = "aliasedObject not found";
114 /* Free the previous entry, continue to work with the
115 * one we just retrieved.
117 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
121 /* We found a regular entry. Return this to the caller. The
122 * entry is still locked for Read.
124 if (!is_entry_alias(e)) {
125 rs->sr_err = LDAP_SUCCESS;
133 /* Look for and dereference all aliases within the search scope. Adds
134 * the dereferenced entries to the "ids" list. Requires "stack" to be
135 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
136 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
138 static int search_aliases(
147 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
148 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
149 ID cursora, ida, cursoro, ido, *subscop2;
152 struct berval bv_alias = BER_BVC( "alias" );
153 #ifdef LDAP_COMP_MATCH
154 AttributeAssertion aa_alias = { NULL, BER_BVNULL, NULL };
156 AttributeAssertion aa_alias = { NULL, BER_BVNULL };
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, &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, e, subscop,
206 subscop2+BDB_IDL_DB_SIZE );
210 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, 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, NULL,
224 ida, &ei, 0, locker, &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->bi_dbenv, &bdb->bi_cache,
241 /* Actually dereference the alias */
243 a = deref_base( op, rs, a, &matched, locker, &lockr,
246 /* If the target was not already in our current candidates,
247 * make note of it in the newsubs list. Also
248 * set it in the scopes list so that bdb_search
251 if (bdb_idl_insert(ids, a->e_id) == 0) {
252 bdb_idl_insert(newsubs, a->e_id);
253 bdb_idl_insert(scopes, a->e_id);
255 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
258 } else if (matched) {
259 /* Alias could not be dereferenced, or it deref'd to
260 * an ID we've already seen. Ignore it.
262 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
267 /* If this is a OneLevel search, we're done; oldsubs only had one
268 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
270 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
272 ido = bdb_idl_next( oldsubs, &cursoro );
274 /* If we're done processing the old scopes, did we add any new
275 * scopes in this iteration? If so, go back and do those now.
278 if (BDB_IDL_IS_ZERO(newsubs)) break;
279 BDB_IDL_CPY(oldsubs, newsubs);
280 BDB_IDL_ZERO(newsubs);
282 ido = bdb_idl_first( oldsubs, &cursoro );
285 /* Find the entry corresponding to the next scope. If it can't
286 * be found, ignore it and move on. This should never happen;
287 * we should never see the ID of an entry that doesn't exist.
288 * Set the name so that the scope's IDL can be retrieved.
292 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
294 if ( rs->sr_err != LDAP_SUCCESS ) {
295 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
296 rs->sr_err == DB_LOCK_NOTGRANTED )
306 bdb_search( Operation *op, SlapReply *rs )
308 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
311 ID candidates[BDB_IDL_UM_SIZE];
312 ID scopes[BDB_IDL_DB_SIZE];
313 Entry *e = NULL, base, e_root = {0};
314 Entry *matched = NULL;
315 EntryInfo *ei, ei_root = {0};
316 struct berval realbase = BER_BVNULL;
321 AttributeName *attrs;
323 u_int32_t locker = 0;
325 struct bdb_op_info *opinfo = NULL;
328 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
329 attrs = op->oq_search.rs_attrs;
331 opinfo = (struct bdb_op_info *) op->o_private;
333 manageDSAit = get_manageDSAit( op );
335 if ( opinfo && opinfo->boi_txn ) {
336 ltid = opinfo->boi_txn;
337 locker = TXN_ID( ltid );
339 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
345 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
350 if ( op->o_req_ndn.bv_len == 0 ) {
351 /* DIT root special case */
352 ei_root.bei_e = &e_root;
353 ei_root.bei_parent = &ei_root;
354 e_root.e_private = &ei_root;
356 BER_BVSTR( &e_root.e_nname, "" );
357 BER_BVSTR( &e_root.e_name, "" );
359 rs->sr_err = LDAP_SUCCESS;
362 /* get entry with reader lock */
363 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei,
375 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
377 LOCK_ID_FREE (bdb->bi_dbenv, locker );
379 case DB_LOCK_DEADLOCK:
380 case DB_LOCK_NOTGRANTED:
383 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
385 LOCK_ID_FREE (bdb->bi_dbenv, locker );
389 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
390 BDB_IDL_ZERO(candidates);
391 e = deref_base( op, rs, e, &matched, locker, &lock,
396 struct berval matched_dn = BER_BVNULL;
398 if ( matched != NULL ) {
399 BerVarray erefs = NULL;
401 #ifdef SLAP_ACL_HONOR_DISCLOSE
402 /* return referral only if "disclose"
403 * is granted on the object */
404 if ( ! access_allowed( op, matched,
405 slap_schema.si_ad_entry,
406 NULL, ACL_DISCLOSE, NULL ) )
408 rs->sr_err = LDAP_NO_SUCH_OBJECT;
411 #endif /* SLAP_ACL_HONOR_DISCLOSE */
413 ber_dupbv( &matched_dn, &matched->e_name );
415 erefs = is_entry_referral( matched )
416 ? get_entry_referrals( op, matched )
418 rs->sr_err = LDAP_REFERRAL;
419 rs->sr_matched = matched_dn.bv_val;
422 #ifdef SLAP_ZONE_ALLOC
423 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
425 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
430 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
431 &op->o_req_dn, op->oq_search.rs_scope );
432 ber_bvarray_free( erefs );
436 #ifdef SLAP_ZONE_ALLOC
437 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
439 rs->sr_ref = referral_rewrite( default_referral,
440 NULL, &op->o_req_dn, op->oq_search.rs_scope );
441 rs->sr_err = LDAP_REFERRAL;
444 send_ldap_result( op, rs );
447 LOCK_ID_FREE (bdb->bi_dbenv, locker );
449 ber_bvarray_free( rs->sr_ref );
452 if ( !BER_BVISNULL( &matched_dn ) ) {
453 ber_memfree( matched_dn.bv_val );
454 rs->sr_matched = NULL;
459 #ifdef SLAP_ACL_HONOR_DISCLOSE
460 /* NOTE: __NEW__ "search" access is required
461 * on searchBase object */
462 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
463 NULL, ACL_SEARCH, NULL, &mask ) )
465 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
466 rs->sr_err = LDAP_NO_SUCH_OBJECT;
468 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
471 #ifdef SLAP_ZONE_ALLOC
472 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
474 if ( e != &e_root ) {
475 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
477 send_ldap_result( op, rs );
480 #endif /* SLAP_ACL_HONOR_DISCLOSE */
482 if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
483 /* entry is a referral, don't allow add */
484 struct berval matched_dn = BER_BVNULL;
485 BerVarray erefs = NULL;
487 ber_dupbv( &matched_dn, &e->e_name );
488 erefs = get_entry_referrals( op, e );
490 rs->sr_err = LDAP_REFERRAL;
492 #ifdef SLAP_ZONE_ALLOC
493 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
495 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
499 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
500 &op->o_req_dn, op->oq_search.rs_scope );
501 ber_bvarray_free( erefs );
504 rs->sr_text = "bad_referral object";
508 Debug( LDAP_DEBUG_TRACE,
509 LDAP_XSTRING(bdb_search) ": entry is referral\n",
512 rs->sr_matched = matched_dn.bv_val;
513 send_ldap_result( op, rs );
516 LOCK_ID_FREE (bdb->bi_dbenv, locker );
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);
532 if ( e != &e_root ) {
533 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, 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);
555 if ( e != &e_root ) {
556 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, 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 locker, 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 if ( (ID)( ps->ps_cookie ) == 0 ) {
610 id = bdb_idl_first( candidates, &cursor );
613 if ( ps->ps_size == 0 ) {
614 rs->sr_err = LDAP_SUCCESS;
615 rs->sr_text = "search abandoned by pagedResult size=0";
616 send_ldap_result( op, rs );
619 for ( id = bdb_idl_first( candidates, &cursor );
621 id <= (ID)( ps->ps_cookie );
622 id = bdb_idl_next( candidates, &cursor ) )
628 if ( cursor == NOID ) {
629 Debug( LDAP_DEBUG_TRACE,
630 LDAP_XSTRING(bdb_search)
631 ": no paged results candidates\n",
633 send_paged_response( op, rs, &lastid, 0 );
635 rs->sr_err = LDAP_OTHER;
641 for ( id = bdb_idl_first( candidates, &cursor );
642 id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
648 /* check for abandon */
649 if ( op->o_abandon ) {
650 rs->sr_err = SLAPD_ABANDON;
654 /* check time limit */
655 if ( op->ors_tlimit != SLAP_NO_LIMIT
656 && slap_get_time() > stoptime )
658 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
659 rs->sr_ref = rs->sr_v2ref;
660 send_ldap_result( op, rs );
661 rs->sr_err = LDAP_SUCCESS;
666 /* get the entry with reader lock */
668 rs->sr_err = bdb_cache_find_id( op, ltid,
669 id, &ei, 0, locker, &lock );
671 if (rs->sr_err == LDAP_BUSY) {
672 rs->sr_text = "ldap server busy";
673 send_ldap_result( op, rs );
676 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
677 || rs->sr_err == DB_LOCK_NOTGRANTED )
679 goto fetch_entry_retry;
682 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
689 if( !BDB_IDL_IS_RANGE(candidates) ) {
690 /* only complain for non-range IDLs */
691 Debug( LDAP_DEBUG_TRACE,
692 LDAP_XSTRING(bdb_search)
693 ": candidate %ld not found\n",
702 #ifdef BDB_SUBENTRIES
704 if ( is_entry_subentry( e ) ) {
705 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
706 if(!get_subentries_visibility( op )) {
707 /* only subentries are visible */
711 } else if ( get_subentries( op ) &&
712 !get_subentries_visibility( op ))
714 /* only subentries are visible */
718 } else if ( get_subentries_visibility( op )) {
719 /* only subentries are visible */
723 #endif /* BDB_SUBENTRIES */
725 /* Does this candidate actually satisfy the search scope?
727 * Note that we don't lock access to the bei_parent pointer.
728 * Since only leaf nodes can be deleted, the parent of any
729 * node will always be a valid node. Also since we have
730 * a Read lock on the data, it cannot be renamed out of the
731 * scope while we are looking at it, and unless we're using
732 * BDB_HIER, its parents cannot be moved either.
734 switch( op->ors_scope ) {
735 case LDAP_SCOPE_BASE:
736 /* This is always true, yes? */
737 if ( id == base.e_id ) scopeok = 1;
740 case LDAP_SCOPE_ONELEVEL:
741 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
744 #ifdef LDAP_SCOPE_CHILDREN
745 case LDAP_SCOPE_CHILDREN:
746 if ( id == base.e_id ) break;
749 case LDAP_SCOPE_SUBTREE: {
751 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
752 if ( tmp->bei_id == base.e_id ) {
760 /* aliases were already dereferenced in candidate list */
761 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
762 /* but if the search base is an alias, and we didn't
763 * deref it when finding, return it.
765 if ( is_entry_alias(e) &&
766 ((op->ors_deref & LDAP_DEREF_FINDING) ||
767 !bvmatch(&e->e_nname, &op->o_req_ndn)))
772 /* scopes is only non-empty for onelevel or subtree */
773 if ( !scopeok && BDB_IDL_N(scopes) ) {
775 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
776 x = bdb_idl_search( scopes, e->e_id );
777 if ( scopes[x] == e->e_id ) scopeok = 1;
779 /* subtree, walk up the tree */
780 EntryInfo *tmp = BEI(e);
781 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
782 x = bdb_idl_search( scopes, tmp->bei_id );
783 if ( scopes[x] == tmp->bei_id ) {
792 /* Not in scope, ignore it */
795 Debug( LDAP_DEBUG_TRACE,
796 LDAP_XSTRING(bdb_search)
797 ": %ld scope not okay\n",
803 * if it's a referral, add it to the list of referrals. only do
804 * this for non-base searches, and don't check the filter
805 * explicitly here since it's only a candidate anyway.
807 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
808 && is_entry_referral( e ) )
810 BerVarray erefs = get_entry_referrals( op, e );
811 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
812 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
813 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
815 send_search_reference( op, rs );
817 ber_bvarray_free( rs->sr_ref );
818 ber_bvarray_free( erefs );
824 if ( !manageDSAit && is_entry_glue( e )) {
828 /* if it matches the filter and scope, send it */
829 rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
831 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
832 /* check size limit */
833 if ( --op->ors_slimit == -1) {
834 #ifdef SLAP_ZONE_ALLOC
835 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
837 bdb_cache_return_entry_r( bdb->bi_dbenv,
838 &bdb->bi_cache, e, &lock );
841 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
842 rs->sr_ref = rs->sr_v2ref;
843 send_ldap_result( op, rs );
844 rs->sr_err = LDAP_SUCCESS;
848 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
849 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
850 send_paged_response( op, rs, &lastid, tentries );
859 rs->sr_attrs = op->oq_search.rs_attrs;
860 rs->sr_operational_attrs = NULL;
863 rs->sr_err = LDAP_SUCCESS;
864 result = send_search_entry( op, rs );
867 case 0: /* entry sent ok */
869 case 1: /* entry not sent */
871 case -1: /* connection closed */
872 #ifdef SLAP_ZONE_ALLOC
873 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
875 bdb_cache_return_entry_r(bdb->bi_dbenv,
876 &bdb->bi_cache, e, &lock);
879 rs->sr_err = LDAP_OTHER;
885 Debug( LDAP_DEBUG_TRACE,
886 LDAP_XSTRING(bdb_search)
887 ": %ld does not match filter\n",
893 /* free reader lock */
894 #ifdef SLAP_ZONE_ALLOC
895 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
897 bdb_cache_return_entry_r( bdb->bi_dbenv,
898 &bdb->bi_cache, e , &lock );
903 ldap_pvt_thread_yield();
908 rs->sr_ref = rs->sr_v2ref;
909 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
910 rs->sr_rspoid = NULL;
911 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
912 send_paged_response( op, rs, NULL, 0 );
914 send_ldap_result( op, rs );
917 rs->sr_err = LDAP_SUCCESS;
921 LOCK_ID_FREE( bdb->bi_dbenv, locker );
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, 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, search_stack,
1003 ret, search_stack_free );
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 #ifdef LDAP_COMP_MATCH
1024 AttributeAssertion aa_ref = { NULL, BER_BVNULL, NULL };
1026 AttributeAssertion aa_ref = { NULL, BER_BVNULL };
1028 #ifdef BDB_SUBENTRIES
1030 #ifdef LDAP_COMP_MATCH
1031 AttributeAssertion aa_subentry = { NULL, BER_BVNULL, NULL };
1033 AttributeAssertion aa_subentry = { NULL, BER_BVNULL };
1038 * This routine takes as input a filter (user-filter)
1039 * and rewrites it as follows:
1040 * (&(scope=DN)[(objectClass=subentry)]
1041 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1044 Debug(LDAP_DEBUG_TRACE,
1045 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1046 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1048 xf.f_or = op->oq_search.rs_filter;
1049 xf.f_choice = LDAP_FILTER_OR;
1052 /* If the user's filter uses objectClass=*,
1053 * these clauses are redundant.
1055 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1056 && !get_subentries_visibility(op)) {
1057 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1058 /* match referral objects */
1059 struct berval bv_ref = BER_BVC( "referral" );
1060 rf.f_choice = LDAP_FILTER_EQUALITY;
1062 rf.f_av_desc = slap_schema.si_ad_objectClass;
1063 rf.f_av_value = bv_ref;
1064 rf.f_next = xf.f_or;
1071 f.f_choice = LDAP_FILTER_AND;
1073 /* Dummy; we compute scope separately now */
1074 nf.f_choice = SLAPD_FILTER_COMPUTED;
1075 nf.f_result = LDAP_SUCCESS;
1076 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1077 ? op->oq_search.rs_filter : &xf ;
1078 /* Filter depth increased again, adding dummy clause */
1081 #ifdef BDB_SUBENTRIES
1082 if( get_subentries_visibility( op ) ) {
1083 struct berval bv_subentry = BER_BVC( "subentry" );
1084 sf.f_choice = LDAP_FILTER_EQUALITY;
1085 sf.f_ava = &aa_subentry;
1086 sf.f_av_desc = slap_schema.si_ad_objectClass;
1087 sf.f_av_value = bv_subentry;
1088 sf.f_next = nf.f_next;
1093 /* Allocate IDL stack, plus 1 more for former tmp */
1094 if ( depth+1 > bdb->bi_search_stack_depth ) {
1095 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1097 stack = search_stack( op );
1100 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1101 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1103 rc = bdb_dn2idl( op, e, ids, stack );
1106 if ( rc == LDAP_SUCCESS ) {
1107 rc = bdb_filter_candidates( op, &f, ids,
1108 stack, stack+BDB_IDL_UM_SIZE );
1111 if ( depth+1 > bdb->bi_search_stack_depth ) {
1116 Debug(LDAP_DEBUG_TRACE,
1117 "bdb_search_candidates: failed (rc=%d)\n",
1121 Debug(LDAP_DEBUG_TRACE,
1122 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1124 (long) BDB_IDL_FIRST(ids),
1125 (long) BDB_IDL_LAST(ids) );
1132 parse_paged_cookie( Operation *op, SlapReply *rs )
1135 int rc = LDAP_SUCCESS;
1139 struct berval cookie = BER_BVNULL;
1140 PagedResultsState *ps = op->o_pagedresults_state;
1142 /* this function must be invoked only if the pagedResults
1143 * control has been detected, parsed and partially checked
1144 * by the frontend */
1145 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1147 /* look for the appropriate ctrl structure */
1148 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
1149 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
1155 if ( c[0] == NULL ) {
1156 rs->sr_text = "missing pagedResults control";
1157 return LDAP_PROTOCOL_ERROR;
1160 /* Tested by frontend */
1161 assert( c[0]->ldctl_value.bv_len > 0 );
1163 /* Parse the control value
1164 * realSearchControlValue ::= SEQUENCE {
1165 * size INTEGER (0..maxInt),
1166 * -- requested page size from client
1167 * -- result set size estimate from server
1168 * cookie OCTET STRING
1171 ber = ber_init( &c[0]->ldctl_value );
1172 if ( ber == NULL ) {
1173 rs->sr_text = "internal error";
1177 tag = ber_scanf( ber, "{im}", &size, &cookie );
1179 /* Tested by frontend */
1180 assert( tag != LBER_ERROR );
1181 assert( size >= 0 );
1183 /* cookie decoding/checks deferred to backend... */
1184 if ( cookie.bv_len ) {
1185 PagedResultsCookie reqcookie;
1186 if( cookie.bv_len != sizeof( reqcookie ) ) {
1188 rs->sr_text = "paged results cookie is invalid";
1189 rc = LDAP_PROTOCOL_ERROR;
1193 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
1195 if ( reqcookie > ps->ps_cookie ) {
1197 rs->sr_text = "paged results cookie is invalid";
1198 rc = LDAP_PROTOCOL_ERROR;
1201 } else if ( reqcookie < ps->ps_cookie ) {
1202 rs->sr_text = "paged results cookie is invalid or old";
1203 rc = LDAP_UNWILLING_TO_PERFORM;
1208 /* Initial request. Initialize state. */
1210 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
1211 /* There's another pagedResults control on the
1212 * same connection; reject new pagedResults controls
1213 * (allowed by RFC2696) */
1214 rs->sr_text = "paged results cookie unavailable; try later";
1215 rc = LDAP_UNWILLING_TO_PERFORM;
1224 (void)ber_free( ber, 1 );
1230 send_paged_response(
1236 LDAPControl ctrl, *ctrls[2];
1237 BerElementBuffer berbuf;
1238 BerElement *ber = (BerElement *)&berbuf;
1239 PagedResultsCookie respcookie;
1240 struct berval cookie;
1242 Debug(LDAP_DEBUG_ARGS,
1243 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1244 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1246 BER_BVZERO( &ctrl.ldctl_value );
1250 ber_init2( ber, NULL, LBER_USE_DER );
1253 respcookie = ( PagedResultsCookie )(*lastid);
1254 cookie.bv_len = sizeof( respcookie );
1255 cookie.bv_val = (char *)&respcookie;
1258 respcookie = ( PagedResultsCookie )0;
1259 BER_BVSTR( &cookie, "" );
1262 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1263 op->o_conn->c_pagedresults_state.ps_count =
1264 ((PagedResultsState *)op->o_pagedresults_state)->ps_count + rs->sr_nentries;
1266 /* return size of 0 -- no estimate */
1267 ber_printf( ber, "{iO}", 0, &cookie );
1269 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1273 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1274 ctrls[0]->ldctl_iscritical = 0;
1276 rs->sr_ctrls = ctrls;
1277 rs->sr_err = LDAP_SUCCESS;
1278 send_ldap_result( op, rs );
1279 rs->sr_ctrls = NULL;
1282 (void) ber_free_buf( ber );