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 static int bdb_pfid_cmp( const void *v_id1, const void *v_id2 );
47 static ID* bdb_id_dup( Operation *op, ID *id );
49 /* Dereference aliases for a single alias entry. Return the final
50 * dereferenced entry on success, NULL on any failure.
52 static Entry * deref_base (
62 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
67 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
68 rs->sr_text = "maximum deref depth exceeded";
70 while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
71 /* Remember the last entry we looked at, so we can
76 /* If this is part of a subtree or onelevel search,
77 * have we seen this ID before? If so, quit.
79 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
84 /* If we've seen this ID during this deref iteration,
87 if ( bdb_idl_insert( tmp, e->e_id ) ) {
88 rs->sr_err = LDAP_ALIAS_PROBLEM;
89 rs->sr_text = "circular alias";
94 /* If there was a problem getting the aliasedObjectName,
95 * get_alias_dn will have set the error status.
97 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
102 rs->sr_err = bdb_dn2entry( op, NULL, &ndn, &ei,
112 rs->sr_err = LDAP_ALIAS_PROBLEM;
113 rs->sr_text = "aliasedObject not found";
117 /* Free the previous entry, continue to work with the
118 * one we just retrieved.
120 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
124 /* We found a regular entry. Return this to the caller. The
125 * entry is still locked for Read.
127 if (!is_entry_alias(e)) {
128 rs->sr_err = LDAP_SUCCESS;
136 /* Look for and dereference all aliases within the search scope. Adds
137 * the dereferenced entries to the "ids" list. Requires "stack" to be
138 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
139 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
141 static int search_aliases(
150 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
151 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
152 ID cursora, ida, cursoro, ido, *subscop2;
155 struct berval bv_alias = BER_BVC( "alias" );
156 AttributeAssertion aa_alias = { NULL, BER_BVNULL, NULL };
158 DB_LOCK locka, lockr;
161 aliases = stack; /* IDL of all aliases in the database */
162 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
163 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
164 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
165 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
166 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
167 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
169 /* A copy of subscop, because subscop gets clobbered by
170 * the bdb_idl_union/intersection routines
172 subscop2 = tmp + BDB_IDL_DB_SIZE;
174 af.f_choice = LDAP_FILTER_EQUALITY;
175 af.f_ava = &aa_alias;
176 af.f_av_desc = slap_schema.si_ad_objectClass;
177 af.f_av_value = bv_alias;
180 /* Find all aliases in database */
181 BDB_IDL_ZERO( aliases );
182 rs->sr_err = bdb_filter_candidates( op, &af, aliases,
184 if (rs->sr_err != LDAP_SUCCESS) {
188 oldsubs[1] = e->e_id;
191 BDB_IDL_ZERO( visited );
192 BDB_IDL_ZERO( newsubs );
195 ido = bdb_idl_first( oldsubs, &cursoro );
198 /* Set curscop to only the aliases in the current scope. Start with
199 * all the aliases, obtain the IDL for the current scope, and then
200 * get the intersection of these two IDLs. Add the current scope
201 * to the cumulative list of candidates.
203 BDB_IDL_CPY( curscop, aliases );
204 rs->sr_err = bdb_dn2idl( op, e, subscop,
205 subscop2+BDB_IDL_DB_SIZE );
209 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
211 BDB_IDL_CPY(subscop2, subscop);
212 rs->sr_err = bdb_idl_intersection(curscop, subscop);
213 bdb_idl_union( ids, subscop2 );
215 /* Dereference all of the aliases in the current scope. */
217 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
218 ida = bdb_idl_next(curscop, &cursora))
222 rs->sr_err = bdb_cache_find_id(op, NULL,
223 ida, &ei, 0, locker, &lockr );
224 if (rs->sr_err != LDAP_SUCCESS) {
225 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
226 rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
231 /* This should only happen if the curscop IDL has maxed out and
232 * turned into a range that spans IDs indiscriminately
234 if (!is_entry_alias(a)) {
235 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
240 /* Actually dereference the alias */
242 a = deref_base( op, rs, a, &matched, locker, &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->bi_dbenv, &bdb->bi_cache,
257 } else if (matched) {
258 /* Alias could not be dereferenced, or it deref'd to
259 * an ID we've already seen. Ignore it.
261 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
266 /* If this is a OneLevel search, we're done; oldsubs only had one
267 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
269 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
271 ido = bdb_idl_next( oldsubs, &cursoro );
273 /* If we're done processing the old scopes, did we add any new
274 * scopes in this iteration? If so, go back and do those now.
277 if (BDB_IDL_IS_ZERO(newsubs)) break;
278 BDB_IDL_CPY(oldsubs, newsubs);
279 BDB_IDL_ZERO(newsubs);
281 ido = bdb_idl_first( oldsubs, &cursoro );
284 /* Find the entry corresponding to the next scope. If it can't
285 * be found, ignore it and move on. This should never happen;
286 * we should never see the ID of an entry that doesn't exist.
287 * Set the name so that the scope's IDL can be retrieved.
291 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
293 if ( rs->sr_err != LDAP_SUCCESS ) {
294 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
295 rs->sr_err == DB_LOCK_NOTGRANTED )
305 bdb_search( Operation *op, SlapReply *rs )
307 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
310 ID candidates[BDB_IDL_UM_SIZE];
311 ID scopes[BDB_IDL_DB_SIZE];
312 Entry *e = NULL, base, e_root = {0};
313 Entry *matched = NULL;
314 EntryInfo *ei, ei_root = {0};
315 struct berval realbase = BER_BVNULL;
320 AttributeName *attrs;
322 u_int32_t locker = 0;
324 struct bdb_op_info *opinfo = NULL;
327 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
328 attrs = op->oq_search.rs_attrs;
330 opinfo = (struct bdb_op_info *) op->o_private;
332 manageDSAit = get_manageDSAit( op );
334 if ( opinfo && opinfo->boi_txn ) {
335 ltid = opinfo->boi_txn;
336 locker = TXN_ID( ltid );
338 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
344 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
349 if ( op->o_req_ndn.bv_len == 0 ) {
350 /* DIT root special case */
351 ei_root.bei_e = &e_root;
352 ei_root.bei_parent = &ei_root;
353 e_root.e_private = &ei_root;
355 BER_BVSTR( &e_root.e_nname, "" );
356 BER_BVSTR( &e_root.e_name, "" );
358 rs->sr_err = LDAP_SUCCESS;
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 LOCK_ID_FREE (bdb->bi_dbenv, locker );
378 case DB_LOCK_DEADLOCK:
379 case DB_LOCK_NOTGRANTED:
382 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
384 LOCK_ID_FREE (bdb->bi_dbenv, locker );
388 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
389 BDB_IDL_ZERO(candidates);
390 e = deref_base( op, rs, e, &matched, locker, &lock,
395 struct berval matched_dn = BER_BVNULL;
397 if ( matched != NULL ) {
398 BerVarray erefs = NULL;
400 #ifdef SLAP_ACL_HONOR_DISCLOSE
401 /* return referral only if "disclose"
402 * is granted on the object */
403 if ( ! access_allowed( op, matched,
404 slap_schema.si_ad_entry,
405 NULL, ACL_DISCLOSE, NULL ) )
407 rs->sr_err = LDAP_NO_SUCH_OBJECT;
410 #endif /* SLAP_ACL_HONOR_DISCLOSE */
412 ber_dupbv( &matched_dn, &matched->e_name );
414 erefs = is_entry_referral( matched )
415 ? get_entry_referrals( op, matched )
417 rs->sr_err = LDAP_REFERRAL;
418 rs->sr_matched = matched_dn.bv_val;
421 #ifdef SLAP_ZONE_ALLOC
422 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
424 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
429 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
430 &op->o_req_dn, op->oq_search.rs_scope );
431 ber_bvarray_free( erefs );
435 #ifdef SLAP_ZONE_ALLOC
436 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
438 rs->sr_ref = referral_rewrite( default_referral,
439 NULL, &op->o_req_dn, op->oq_search.rs_scope );
440 rs->sr_err = LDAP_REFERRAL;
443 send_ldap_result( op, rs );
446 LOCK_ID_FREE (bdb->bi_dbenv, locker );
448 ber_bvarray_free( rs->sr_ref );
451 if ( !BER_BVISNULL( &matched_dn ) ) {
452 ber_memfree( matched_dn.bv_val );
453 rs->sr_matched = NULL;
458 #ifdef SLAP_ACL_HONOR_DISCLOSE
459 /* NOTE: __NEW__ "search" access is required
460 * on searchBase object */
461 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
462 NULL, ACL_SEARCH, NULL, &mask ) )
464 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
465 rs->sr_err = LDAP_NO_SUCH_OBJECT;
467 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
470 #ifdef SLAP_ZONE_ALLOC
471 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
473 if ( e != &e_root ) {
474 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
476 send_ldap_result( op, rs );
479 #endif /* SLAP_ACL_HONOR_DISCLOSE */
481 if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
482 /* entry is a referral, don't allow add */
483 struct berval matched_dn = BER_BVNULL;
484 BerVarray erefs = NULL;
486 ber_dupbv( &matched_dn, &e->e_name );
487 erefs = get_entry_referrals( op, e );
489 rs->sr_err = LDAP_REFERRAL;
491 #ifdef SLAP_ZONE_ALLOC
492 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
494 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
498 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
499 &op->o_req_dn, op->oq_search.rs_scope );
500 ber_bvarray_free( erefs );
503 rs->sr_text = "bad_referral object";
507 Debug( LDAP_DEBUG_TRACE,
508 LDAP_XSTRING(bdb_search) ": entry is referral\n",
511 rs->sr_matched = matched_dn.bv_val;
512 send_ldap_result( op, rs );
515 LOCK_ID_FREE (bdb->bi_dbenv, locker );
517 ber_bvarray_free( rs->sr_ref );
519 ber_memfree( matched_dn.bv_val );
520 rs->sr_matched = NULL;
524 if ( get_assert( op ) &&
525 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
527 rs->sr_err = LDAP_ASSERTION_FAILED;
528 #ifdef SLAP_ZONE_ALLOC
529 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
531 if ( e != &e_root ) {
532 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
534 send_ldap_result( op, rs );
538 /* compute it anyway; root does not use it */
539 stoptime = op->o_time + op->ors_tlimit;
541 /* need normalized dn below */
542 ber_dupbv( &realbase, &e->e_nname );
544 /* Copy info to base, must free entry before accessing the database
545 * in search_candidates, to avoid deadlocks.
547 base.e_private = e->e_private;
548 base.e_nname = realbase;
551 #ifdef SLAP_ZONE_ALLOC
552 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
554 if ( e != &e_root ) {
555 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
559 /* select candidates */
560 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
561 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
564 BDB_IDL_ZERO( candidates );
565 BDB_IDL_ZERO( scopes );
566 rs->sr_err = search_candidates( op, rs, &base,
567 locker, candidates, scopes );
570 /* start cursor at beginning of candidates.
574 if ( candidates[0] == 0 ) {
575 Debug( LDAP_DEBUG_TRACE,
576 LDAP_XSTRING(bdb_search) ": no candidates\n",
582 /* if not root and candidates exceed to-be-checked entries, abort */
583 if ( op->ors_limit /* isroot == FALSE */ &&
584 op->ors_limit->lms_s_unchecked != -1 &&
585 BDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
587 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
588 send_ldap_result( op, rs );
589 rs->sr_err = LDAP_SUCCESS;
593 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
594 !op->ors_limit->lms_s_pr_hide )
596 tentries = BDB_IDL_N(candidates);
599 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
600 PagedResultsState *ps = op->o_pagedresults_state;
601 /* deferred cookie parsing */
602 rs->sr_err = parse_paged_cookie( op, rs );
603 if ( rs->sr_err != LDAP_SUCCESS ) {
604 send_ldap_result( op, rs );
608 if ( (ID)( ps->ps_cookie ) == 0 ) {
609 id = bdb_idl_first( candidates, &cursor );
612 if ( ps->ps_size == 0 ) {
613 rs->sr_err = LDAP_SUCCESS;
614 rs->sr_text = "search abandoned by pagedResult size=0";
615 send_ldap_result( op, rs );
618 for ( id = bdb_idl_first( candidates, &cursor );
620 id <= (ID)( ps->ps_cookie );
621 id = bdb_idl_next( candidates, &cursor ) )
627 if ( cursor == NOID ) {
628 Debug( LDAP_DEBUG_TRACE,
629 LDAP_XSTRING(bdb_search)
630 ": no paged results candidates\n",
632 send_paged_response( op, rs, &lastid, 0 );
634 rs->sr_err = LDAP_OTHER;
642 for ( id = bdb_idl_first( candidates, &cursor );
643 id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
650 /* check for abandon */
651 if ( op->o_abandon ) {
652 rs->sr_err = SLAPD_ABANDON;
656 /* check time limit */
657 if ( op->ors_tlimit != SLAP_NO_LIMIT
658 && slap_get_time() > stoptime )
660 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
661 rs->sr_ref = rs->sr_v2ref;
662 send_ldap_result( op, rs );
663 rs->sr_err = LDAP_SUCCESS;
668 /* get the entry with reader lock */
670 rs->sr_err = bdb_cache_find_id( op, ltid,
671 id, &ei, 0, locker, &lock );
673 if (rs->sr_err == LDAP_BUSY) {
674 rs->sr_text = "ldap server busy";
675 send_ldap_result( op, rs );
678 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
679 || rs->sr_err == DB_LOCK_NOTGRANTED )
681 goto fetch_entry_retry;
684 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
691 if( !BDB_IDL_IS_RANGE(candidates) ) {
692 /* only complain for non-range IDLs */
693 Debug( LDAP_DEBUG_TRACE,
694 LDAP_XSTRING(bdb_search)
695 ": candidate %ld not found\n",
704 #ifdef BDB_SUBENTRIES
706 if ( is_entry_subentry( e ) ) {
707 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
708 if(!get_subentries_visibility( op )) {
709 /* only subentries are visible */
713 } else if ( get_subentries( op ) &&
714 !get_subentries_visibility( op ))
716 /* only subentries are visible */
720 } else if ( get_subentries_visibility( op )) {
721 /* only subentries are visible */
725 #endif /* BDB_SUBENTRIES */
727 /* Does this candidate actually satisfy the search scope?
729 * Note that we don't lock access to the bei_parent pointer.
730 * Since only leaf nodes can be deleted, the parent of any
731 * node will always be a valid node. Also since we have
732 * a Read lock on the data, it cannot be renamed out of the
733 * scope while we are looking at it, and unless we're using
734 * BDB_HIER, its parents cannot be moved either.
736 switch( op->ors_scope ) {
737 case LDAP_SCOPE_BASE:
738 /* This is always true, yes? */
739 if ( id == base.e_id ) scopeok = 1;
742 case LDAP_SCOPE_ONELEVEL:
743 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
746 #ifdef LDAP_SCOPE_CHILDREN
747 case LDAP_SCOPE_CHILDREN:
748 if ( id == base.e_id ) break;
751 case LDAP_SCOPE_SUBTREE: {
753 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
754 if ( tmp->bei_id == base.e_id ) {
762 /* aliases were already dereferenced in candidate list */
763 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
764 /* but if the search base is an alias, and we didn't
765 * deref it when finding, return it.
767 if ( is_entry_alias(e) &&
768 ((op->ors_deref & LDAP_DEREF_FINDING) ||
769 !bvmatch(&e->e_nname, &op->o_req_ndn)))
774 /* scopes is only non-empty for onelevel or subtree */
775 if ( !scopeok && BDB_IDL_N(scopes) ) {
777 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
778 x = bdb_idl_search( scopes, e->e_id );
779 if ( scopes[x] == e->e_id ) scopeok = 1;
781 /* subtree, walk up the tree */
782 EntryInfo *tmp = BEI(e);
783 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
784 x = bdb_idl_search( scopes, tmp->bei_id );
785 if ( scopes[x] == tmp->bei_id ) {
794 /* Not in scope, ignore it */
797 Debug( LDAP_DEBUG_TRACE,
798 LDAP_XSTRING(bdb_search)
799 ": %ld scope not okay\n",
805 * if it's a referral, add it to the list of referrals. only do
806 * this for non-base searches, and don't check the filter
807 * explicitly here since it's only a candidate anyway.
809 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
810 && is_entry_referral( e ) )
812 BerVarray erefs = get_entry_referrals( op, e );
813 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
814 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
815 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
817 send_search_reference( op, rs );
819 ber_bvarray_free( rs->sr_ref );
820 ber_bvarray_free( erefs );
826 if ( !manageDSAit && is_entry_glue( e )) {
830 /* if it matches the filter and scope, send it */
831 rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
833 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
834 /* check size limit */
835 if ( --op->ors_slimit == -1) {
836 #ifdef SLAP_ZONE_ALLOC
837 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
839 bdb_cache_return_entry_r( bdb->bi_dbenv,
840 &bdb->bi_cache, e, &lock );
843 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
844 rs->sr_ref = rs->sr_v2ref;
845 send_ldap_result( op, rs );
846 rs->sr_err = LDAP_SUCCESS;
850 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
851 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
852 send_paged_response( op, rs, &lastid, tentries );
861 rs->sr_attrs = op->oq_search.rs_attrs;
862 rs->sr_operational_attrs = NULL;
865 rs->sr_err = LDAP_SUCCESS;
866 result = send_search_entry( op, rs );
869 case 0: /* entry sent ok */
871 case 1: /* entry not sent */
873 case -1: /* connection closed */
874 #ifdef SLAP_ZONE_ALLOC
875 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
877 bdb_cache_return_entry_r(bdb->bi_dbenv,
878 &bdb->bi_cache, e, &lock);
881 rs->sr_err = LDAP_OTHER;
887 Debug( LDAP_DEBUG_TRACE,
888 LDAP_XSTRING(bdb_search)
889 ": %ld does not match filter\n",
895 /* free reader lock */
896 #ifdef SLAP_ZONE_ALLOC
897 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
899 bdb_cache_return_entry_r( bdb->bi_dbenv,
900 &bdb->bi_cache, e , &lock );
905 ldap_pvt_thread_yield();
910 rs->sr_ref = rs->sr_v2ref;
911 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
912 rs->sr_rspoid = NULL;
913 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
914 send_paged_response( op, rs, NULL, 0 );
916 send_ldap_result( op, rs );
919 rs->sr_err = LDAP_SUCCESS;
923 LOCK_ID_FREE( bdb->bi_dbenv, locker );
926 ber_bvarray_free( rs->sr_v2ref );
929 if( realbase.bv_val ) ch_free( realbase.bv_val );
935 static int base_candidate(
940 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
941 e->e_nname.bv_val, (long) e->e_id, 0);
948 /* Look for "objectClass Present" in this filter.
949 * Also count depth of filter tree while we're at it.
951 static int oc_filter(
960 if( cur > *max ) *max = cur;
962 switch( f->f_choice ) {
963 case LDAP_FILTER_PRESENT:
964 if (f->f_desc == slap_schema.si_ad_objectClass) {
969 case LDAP_FILTER_AND:
972 for ( f=f->f_and; f; f=f->f_next ) {
973 (void) oc_filter(f, cur, max);
983 static void search_stack_free( void *key, void *data )
985 ber_memfree_x(data, NULL);
988 static void *search_stack( Operation *op )
990 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
993 if ( op->o_threadctx ) {
994 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
997 ret = bdb->bi_search_stack;
1001 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1003 if ( op->o_threadctx ) {
1004 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1005 ret, search_stack_free );
1007 bdb->bi_search_stack = ret;
1013 static int search_candidates(
1021 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1023 Filter f, rf, xf, nf;
1025 AttributeAssertion aa_ref = { NULL, BER_BVNULL, NULL };
1026 #ifdef BDB_SUBENTRIES
1028 AttributeAssertion aa_subentry = { NULL, BER_BVNULL, NULL };
1032 * This routine takes as input a filter (user-filter)
1033 * and rewrites it as follows:
1034 * (&(scope=DN)[(objectClass=subentry)]
1035 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1038 Debug(LDAP_DEBUG_TRACE,
1039 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1040 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1042 xf.f_or = op->oq_search.rs_filter;
1043 xf.f_choice = LDAP_FILTER_OR;
1046 /* If the user's filter uses objectClass=*,
1047 * these clauses are redundant.
1049 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1050 && !get_subentries_visibility(op)) {
1051 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1052 /* match referral objects */
1053 struct berval bv_ref = BER_BVC( "referral" );
1054 rf.f_choice = LDAP_FILTER_EQUALITY;
1056 rf.f_av_desc = slap_schema.si_ad_objectClass;
1057 rf.f_av_value = bv_ref;
1058 rf.f_next = xf.f_or;
1065 f.f_choice = LDAP_FILTER_AND;
1067 /* Dummy; we compute scope separately now */
1068 nf.f_choice = SLAPD_FILTER_COMPUTED;
1069 nf.f_result = LDAP_SUCCESS;
1070 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1071 ? op->oq_search.rs_filter : &xf ;
1072 /* Filter depth increased again, adding dummy clause */
1075 #ifdef BDB_SUBENTRIES
1076 if( get_subentries_visibility( op ) ) {
1077 struct berval bv_subentry = BER_BVC( "subentry" );
1078 sf.f_choice = LDAP_FILTER_EQUALITY;
1079 sf.f_ava = &aa_subentry;
1080 sf.f_av_desc = slap_schema.si_ad_objectClass;
1081 sf.f_av_value = bv_subentry;
1082 sf.f_next = nf.f_next;
1087 /* Allocate IDL stack, plus 1 more for former tmp */
1088 if ( depth+1 > bdb->bi_search_stack_depth ) {
1089 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1091 stack = search_stack( op );
1094 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1095 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1097 rc = bdb_dn2idl( op, e, ids, stack );
1100 if ( rc == LDAP_SUCCESS ) {
1101 rc = bdb_filter_candidates( op, &f, ids,
1102 stack, stack+BDB_IDL_UM_SIZE );
1105 if ( depth+1 > bdb->bi_search_stack_depth ) {
1110 Debug(LDAP_DEBUG_TRACE,
1111 "bdb_search_candidates: failed (rc=%d)\n",
1115 Debug(LDAP_DEBUG_TRACE,
1116 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1118 (long) BDB_IDL_FIRST(ids),
1119 (long) BDB_IDL_LAST(ids) );
1126 parse_paged_cookie( Operation *op, SlapReply *rs )
1129 int rc = LDAP_SUCCESS;
1133 struct berval cookie = BER_BVNULL;
1134 PagedResultsState *ps = op->o_pagedresults_state;
1136 /* this function must be invoked only if the pagedResults
1137 * control has been detected, parsed and partially checked
1138 * by the frontend */
1139 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1141 /* look for the appropriate ctrl structure */
1142 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
1143 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
1149 if ( c[0] == NULL ) {
1150 rs->sr_text = "missing pagedResults control";
1151 return LDAP_PROTOCOL_ERROR;
1154 /* Tested by frontend */
1155 assert( c[0]->ldctl_value.bv_len > 0 );
1157 /* Parse the control value
1158 * realSearchControlValue ::= SEQUENCE {
1159 * size INTEGER (0..maxInt),
1160 * -- requested page size from client
1161 * -- result set size estimate from server
1162 * cookie OCTET STRING
1165 ber = ber_init( &c[0]->ldctl_value );
1166 if ( ber == NULL ) {
1167 rs->sr_text = "internal error";
1171 tag = ber_scanf( ber, "{im}", &size, &cookie );
1173 /* Tested by frontend */
1174 assert( tag != LBER_ERROR );
1175 assert( size >= 0 );
1177 /* cookie decoding/checks deferred to backend... */
1178 if ( cookie.bv_len ) {
1179 PagedResultsCookie reqcookie;
1180 if( cookie.bv_len != sizeof( reqcookie ) ) {
1182 rs->sr_text = "paged results cookie is invalid";
1183 rc = LDAP_PROTOCOL_ERROR;
1187 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
1189 if ( reqcookie > ps->ps_cookie ) {
1191 rs->sr_text = "paged results cookie is invalid";
1192 rc = LDAP_PROTOCOL_ERROR;
1195 } else if ( reqcookie < ps->ps_cookie ) {
1196 rs->sr_text = "paged results cookie is invalid or old";
1197 rc = LDAP_UNWILLING_TO_PERFORM;
1202 /* Initial request. Initialize state. */
1204 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
1205 /* There's another pagedResults control on the
1206 * same connection; reject new pagedResults controls
1207 * (allowed by RFC2696) */
1208 rs->sr_text = "paged results cookie unavailable; try later";
1209 rc = LDAP_UNWILLING_TO_PERFORM;
1218 (void)ber_free( ber, 1 );
1224 send_paged_response(
1230 LDAPControl ctrl, *ctrls[2];
1231 BerElementBuffer berbuf;
1232 BerElement *ber = (BerElement *)&berbuf;
1233 PagedResultsCookie respcookie;
1234 struct berval cookie;
1236 Debug(LDAP_DEBUG_ARGS,
1237 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1238 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1240 BER_BVZERO( &ctrl.ldctl_value );
1244 ber_init2( ber, NULL, LBER_USE_DER );
1247 respcookie = ( PagedResultsCookie )(*lastid);
1248 cookie.bv_len = sizeof( respcookie );
1249 cookie.bv_val = (char *)&respcookie;
1252 respcookie = ( PagedResultsCookie )0;
1253 BER_BVSTR( &cookie, "" );
1256 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1257 op->o_conn->c_pagedresults_state.ps_count =
1258 ((PagedResultsState *)op->o_pagedresults_state)->ps_count + rs->sr_nentries;
1260 /* return size of 0 -- no estimate */
1261 ber_printf( ber, "{iO}", 0, &cookie );
1263 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1267 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1268 ctrls[0]->ldctl_iscritical = 0;
1270 rs->sr_ctrls = ctrls;
1271 rs->sr_err = LDAP_SUCCESS;
1272 send_ldap_result( op, rs );
1273 rs->sr_ctrls = NULL;
1276 (void) ber_free_buf( ber );
1280 bdb_pfid_cmp( const void *v_id1, const void *v_id2 )
1282 const ID *p1 = v_id1, *p2 = v_id2;
1287 bdb_id_dup( Operation *op, ID *id )
1290 new = ch_malloc( sizeof(ID) );