1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 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, NULL, &ndn, &ei,
114 rs->sr_err = LDAP_ALIAS_PROBLEM;
115 rs->sr_text = "aliasedObject not found";
119 /* Free the previous entry, continue to work with the
120 * one we just retrieved.
122 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
126 /* We found a regular entry. Return this to the caller. The
127 * entry is still locked for Read.
129 if (!is_entry_alias(e)) {
130 rs->sr_err = LDAP_SUCCESS;
138 /* Look for and dereference all aliases within the search scope. Adds
139 * the dereferenced entries to the "ids" list. Requires "stack" to be
140 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
141 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
143 static int search_aliases(
152 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
153 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
154 ID cursora, ida, cursoro, ido, *subscop2;
157 struct berval bv_alias = BER_BVC( "alias" );
158 #ifdef LDAP_COMP_MATCH
159 AttributeAssertion aa_alias = { NULL, BER_BVNULL, NULL };
161 AttributeAssertion aa_alias = { NULL, BER_BVNULL };
164 DB_LOCK locka, lockr;
167 aliases = stack; /* IDL of all aliases in the database */
168 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
169 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
170 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
171 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
172 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
173 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
175 /* A copy of subscop, because subscop gets clobbered by
176 * the bdb_idl_union/intersection routines
178 subscop2 = tmp + BDB_IDL_DB_SIZE;
180 af.f_choice = LDAP_FILTER_EQUALITY;
181 af.f_ava = &aa_alias;
182 af.f_av_desc = slap_schema.si_ad_objectClass;
183 af.f_av_value = bv_alias;
186 /* Find all aliases in database */
187 BDB_IDL_ZERO( aliases );
188 rs->sr_err = bdb_filter_candidates( op, &af, aliases,
190 if (rs->sr_err != LDAP_SUCCESS) {
194 oldsubs[1] = e->e_id;
197 BDB_IDL_ZERO( visited );
198 BDB_IDL_ZERO( newsubs );
201 ido = bdb_idl_first( oldsubs, &cursoro );
204 /* Set curscop to only the aliases in the current scope. Start with
205 * all the aliases, obtain the IDL for the current scope, and then
206 * get the intersection of these two IDLs. Add the current scope
207 * to the cumulative list of candidates.
209 BDB_IDL_CPY( curscop, aliases );
210 rs->sr_err = bdb_dn2idl( op, e, subscop,
211 subscop2+BDB_IDL_DB_SIZE );
215 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
217 BDB_IDL_CPY(subscop2, subscop);
218 rs->sr_err = bdb_idl_intersection(curscop, subscop);
219 bdb_idl_union( ids, subscop2 );
221 /* Dereference all of the aliases in the current scope. */
223 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
224 ida = bdb_idl_next(curscop, &cursora))
228 rs->sr_err = bdb_cache_find_id(op, NULL,
229 ida, &ei, 0, locker, &lockr );
230 if (rs->sr_err != LDAP_SUCCESS) {
231 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
232 rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
237 /* This should only happen if the curscop IDL has maxed out and
238 * turned into a range that spans IDs indiscriminately
240 if (!is_entry_alias(a)) {
241 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
246 /* Actually dereference the alias */
248 a = deref_base( op, rs, a, &matched, locker, &lockr,
251 /* If the target was not already in our current candidates,
252 * make note of it in the newsubs list. Also
253 * set it in the scopes list so that bdb_search
256 if (bdb_idl_insert(ids, a->e_id) == 0) {
257 bdb_idl_insert(newsubs, a->e_id);
258 bdb_idl_insert(scopes, a->e_id);
260 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
263 } else if (matched) {
264 /* Alias could not be dereferenced, or it deref'd to
265 * an ID we've already seen. Ignore it.
267 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
272 /* If this is a OneLevel search, we're done; oldsubs only had one
273 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
275 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
277 ido = bdb_idl_next( oldsubs, &cursoro );
279 /* If we're done processing the old scopes, did we add any new
280 * scopes in this iteration? If so, go back and do those now.
283 if (BDB_IDL_IS_ZERO(newsubs)) break;
284 BDB_IDL_CPY(oldsubs, newsubs);
285 BDB_IDL_ZERO(newsubs);
287 ido = bdb_idl_first( oldsubs, &cursoro );
290 /* Find the entry corresponding to the next scope. If it can't
291 * be found, ignore it and move on. This should never happen;
292 * we should never see the ID of an entry that doesn't exist.
293 * Set the name so that the scope's IDL can be retrieved.
297 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
299 if ( rs->sr_err != LDAP_SUCCESS ) {
300 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
301 rs->sr_err == DB_LOCK_NOTGRANTED )
311 bdb_search( Operation *op, SlapReply *rs )
313 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
316 ID candidates[BDB_IDL_UM_SIZE];
317 ID scopes[BDB_IDL_DB_SIZE];
318 Entry *e = NULL, base, e_root = {0};
319 Entry *matched = NULL;
320 EntryInfo *ei, ei_root = {0};
321 struct berval realbase = BER_BVNULL;
322 #ifdef SLAP_ACL_HONOR_DISCLOSE
328 AttributeName *attrs;
330 u_int32_t locker = 0;
332 struct bdb_op_info *opinfo = NULL;
335 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
336 attrs = op->oq_search.rs_attrs;
338 opinfo = (struct bdb_op_info *) op->o_private;
340 manageDSAit = get_manageDSAit( op );
342 if ( opinfo && opinfo->boi_txn ) {
343 ltid = opinfo->boi_txn;
344 locker = TXN_ID( ltid );
346 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
352 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
357 if ( op->o_req_ndn.bv_len == 0 ) {
358 /* DIT root special case */
359 ei_root.bei_e = &e_root;
360 ei_root.bei_parent = &ei_root;
361 e_root.e_private = &ei_root;
363 e_root.e_ocflags = SLAP_OC_GLUE | SLAP_OC__END;
364 BER_BVSTR( &e_root.e_nname, "" );
365 BER_BVSTR( &e_root.e_name, "" );
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,
386 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
388 LOCK_ID_FREE (bdb->bi_dbenv, locker );
390 case DB_LOCK_DEADLOCK:
391 case DB_LOCK_NOTGRANTED:
394 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
396 LOCK_ID_FREE (bdb->bi_dbenv, locker );
400 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
401 if ( matched && is_entry_alias( matched )) {
404 stub.bv_val = op->o_req_ndn.bv_val;
405 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
406 e = deref_base( op, rs, matched, &matched, locker, &lock,
409 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
411 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
416 } else if ( e && is_entry_alias( e )) {
417 e = deref_base( op, rs, e, &matched, locker, &lock,
423 struct berval matched_dn = BER_BVNULL;
425 if ( matched != NULL ) {
426 BerVarray erefs = NULL;
428 #ifdef SLAP_ACL_HONOR_DISCLOSE
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 #endif /* SLAP_ACL_HONOR_DISCLOSE */
440 ber_dupbv( &matched_dn, &matched->e_name );
442 erefs = is_entry_referral( matched )
443 ? get_entry_referrals( op, matched )
445 if ( rs->sr_err == DB_NOTFOUND )
446 rs->sr_err = LDAP_REFERRAL;
447 rs->sr_matched = matched_dn.bv_val;
450 #ifdef SLAP_ZONE_ALLOC
451 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
453 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
458 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
459 &op->o_req_dn, op->oq_search.rs_scope );
460 ber_bvarray_free( erefs );
464 #ifdef SLAP_ZONE_ALLOC
465 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
467 rs->sr_ref = referral_rewrite( default_referral,
468 NULL, &op->o_req_dn, op->oq_search.rs_scope );
469 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
472 send_ldap_result( op, rs );
475 LOCK_ID_FREE (bdb->bi_dbenv, locker );
477 ber_bvarray_free( rs->sr_ref );
480 if ( !BER_BVISNULL( &matched_dn ) ) {
481 ber_memfree( matched_dn.bv_val );
482 rs->sr_matched = NULL;
487 #ifdef SLAP_ACL_HONOR_DISCLOSE
488 /* NOTE: __NEW__ "search" access is required
489 * on searchBase object */
490 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
491 NULL, ACL_SEARCH, NULL, &mask ) )
493 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
494 rs->sr_err = LDAP_NO_SUCH_OBJECT;
496 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
499 #ifdef SLAP_ZONE_ALLOC
500 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
502 if ( e != &e_root ) {
503 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
505 send_ldap_result( op, rs );
508 #endif /* SLAP_ACL_HONOR_DISCLOSE */
510 if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
511 /* entry is a referral, don't allow add */
512 struct berval matched_dn = BER_BVNULL;
513 BerVarray erefs = NULL;
515 ber_dupbv( &matched_dn, &e->e_name );
516 erefs = get_entry_referrals( op, e );
518 rs->sr_err = LDAP_REFERRAL;
520 #ifdef SLAP_ZONE_ALLOC
521 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
523 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
527 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
528 &op->o_req_dn, op->oq_search.rs_scope );
529 ber_bvarray_free( erefs );
532 rs->sr_text = "bad_referral object";
536 Debug( LDAP_DEBUG_TRACE,
537 LDAP_XSTRING(bdb_search) ": entry is referral\n",
540 rs->sr_matched = matched_dn.bv_val;
541 send_ldap_result( op, rs );
544 LOCK_ID_FREE (bdb->bi_dbenv, locker );
546 ber_bvarray_free( rs->sr_ref );
548 ber_memfree( matched_dn.bv_val );
549 rs->sr_matched = NULL;
553 if ( get_assert( op ) &&
554 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
556 rs->sr_err = LDAP_ASSERTION_FAILED;
557 #ifdef SLAP_ZONE_ALLOC
558 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
560 if ( e != &e_root ) {
561 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
563 send_ldap_result( op, rs );
567 /* compute it anyway; root does not use it */
568 stoptime = op->o_time + op->ors_tlimit;
570 /* need normalized dn below */
571 ber_dupbv( &realbase, &e->e_nname );
573 /* Copy info to base, must free entry before accessing the database
574 * in search_candidates, to avoid deadlocks.
576 base.e_private = e->e_private;
577 base.e_nname = realbase;
580 #ifdef SLAP_ZONE_ALLOC
581 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
583 if ( e != &e_root ) {
584 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
588 /* select candidates */
589 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
590 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
593 BDB_IDL_ZERO( candidates );
594 BDB_IDL_ZERO( scopes );
595 rs->sr_err = search_candidates( op, rs, &base,
596 locker, candidates, scopes );
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 if ( (ID)( ps->ps_cookie ) == 0 ) {
638 id = bdb_idl_first( candidates, &cursor );
641 if ( ps->ps_size == 0 ) {
642 rs->sr_err = LDAP_SUCCESS;
643 rs->sr_text = "search abandoned by pagedResult size=0";
644 send_ldap_result( op, rs );
647 for ( id = bdb_idl_first( candidates, &cursor );
649 id <= (ID)( ps->ps_cookie );
650 id = bdb_idl_next( candidates, &cursor ) )
656 if ( cursor == NOID ) {
657 Debug( LDAP_DEBUG_TRACE,
658 LDAP_XSTRING(bdb_search)
659 ": no paged results candidates\n",
661 send_paged_response( op, rs, &lastid, 0 );
663 rs->sr_err = LDAP_OTHER;
669 for ( id = bdb_idl_first( candidates, &cursor );
670 id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
676 /* check for abandon */
677 if ( op->o_abandon ) {
678 rs->sr_err = SLAPD_ABANDON;
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;
694 /* get the entry with reader lock */
696 rs->sr_err = bdb_cache_find_id( op, ltid,
697 id, &ei, 0, locker, &lock );
699 if (rs->sr_err == LDAP_BUSY) {
700 rs->sr_text = "ldap server busy";
701 send_ldap_result( op, rs );
704 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
705 || rs->sr_err == DB_LOCK_NOTGRANTED )
707 goto fetch_entry_retry;
710 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
717 if( !BDB_IDL_IS_RANGE(candidates) ) {
718 /* only complain for non-range IDLs */
719 Debug( LDAP_DEBUG_TRACE,
720 LDAP_XSTRING(bdb_search)
721 ": candidate %ld not found\n",
730 if ( is_entry_subentry( e ) ) {
731 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
732 if(!get_subentries_visibility( op )) {
733 /* only subentries are visible */
737 } else if ( get_subentries( op ) &&
738 !get_subentries_visibility( op ))
740 /* only subentries are visible */
744 } else if ( get_subentries_visibility( op )) {
745 /* only subentries are visible */
749 /* Does this candidate actually satisfy the search scope?
751 * Note that we don't lock access to the bei_parent pointer.
752 * Since only leaf nodes can be deleted, the parent of any
753 * node will always be a valid node. Also since we have
754 * a Read lock on the data, it cannot be renamed out of the
755 * scope while we are looking at it, and unless we're using
756 * BDB_HIER, its parents cannot be moved either.
759 switch( op->ors_scope ) {
760 case LDAP_SCOPE_BASE:
761 /* This is always true, yes? */
762 if ( id == base.e_id ) scopeok = 1;
765 case LDAP_SCOPE_ONELEVEL:
766 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
769 #ifdef LDAP_SCOPE_CHILDREN
770 case LDAP_SCOPE_CHILDREN:
771 if ( id == base.e_id ) break;
774 case LDAP_SCOPE_SUBTREE: {
776 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
777 if ( tmp->bei_id == base.e_id ) {
785 /* aliases were already dereferenced in candidate list */
786 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
787 /* but if the search base is an alias, and we didn't
788 * deref it when finding, return it.
790 if ( is_entry_alias(e) &&
791 ((op->ors_deref & LDAP_DEREF_FINDING) ||
792 !bvmatch(&e->e_nname, &op->o_req_ndn)))
797 /* scopes is only non-empty for onelevel or subtree */
798 if ( !scopeok && BDB_IDL_N(scopes) ) {
800 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
801 x = bdb_idl_search( scopes, e->e_id );
802 if ( scopes[x] == e->e_id ) scopeok = 1;
804 /* subtree, walk up the tree */
805 EntryInfo *tmp = BEI(e);
806 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
807 x = bdb_idl_search( scopes, tmp->bei_id );
808 if ( scopes[x] == tmp->bei_id ) {
817 /* Not in scope, ignore it */
820 Debug( LDAP_DEBUG_TRACE,
821 LDAP_XSTRING(bdb_search)
822 ": %ld scope not okay\n",
828 * if it's a referral, add it to the list of referrals. only do
829 * this for non-base searches, and don't check the filter
830 * explicitly here since it's only a candidate anyway.
832 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
833 && is_entry_referral( e ) )
835 BerVarray erefs = get_entry_referrals( op, e );
836 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
837 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
838 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
840 send_search_reference( op, rs );
842 ber_bvarray_free( rs->sr_ref );
843 ber_bvarray_free( erefs );
849 if ( !manageDSAit && is_entry_glue( e )) {
853 /* if it matches the filter and scope, send it */
854 rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
856 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
857 /* check size limit */
858 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
859 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
860 #ifdef SLAP_ZONE_ALLOC
861 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
863 bdb_cache_return_entry_r( bdb->bi_dbenv,
864 &bdb->bi_cache, e, &lock );
866 send_paged_response( op, rs, &lastid, tentries );
874 rs->sr_attrs = op->oq_search.rs_attrs;
875 rs->sr_operational_attrs = NULL;
878 rs->sr_err = LDAP_SUCCESS;
879 rs->sr_err = send_search_entry( op, rs );
881 switch ( rs->sr_err ) {
882 case LDAP_SUCCESS: /* entry sent ok */
884 default: /* entry not sent */
886 case LDAP_UNAVAILABLE:
887 case LDAP_SIZELIMIT_EXCEEDED:
888 #ifdef SLAP_ZONE_ALLOC
889 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
891 bdb_cache_return_entry_r(bdb->bi_dbenv,
892 &bdb->bi_cache, e, &lock);
895 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
896 rs->sr_ref = rs->sr_v2ref;
897 send_ldap_result( op, rs );
898 rs->sr_err = LDAP_SUCCESS;
901 rs->sr_err = LDAP_OTHER;
908 Debug( LDAP_DEBUG_TRACE,
909 LDAP_XSTRING(bdb_search)
910 ": %ld does not match filter\n",
916 /* free reader lock */
917 #ifdef SLAP_ZONE_ALLOC
918 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
920 bdb_cache_return_entry_r( bdb->bi_dbenv,
921 &bdb->bi_cache, e , &lock );
929 rs->sr_ref = rs->sr_v2ref;
930 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
931 rs->sr_rspoid = NULL;
932 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
933 send_paged_response( op, rs, NULL, 0 );
935 send_ldap_result( op, rs );
938 rs->sr_err = LDAP_SUCCESS;
942 LOCK_ID_FREE( bdb->bi_dbenv, locker );
945 ber_bvarray_free( rs->sr_v2ref );
948 if( realbase.bv_val ) ch_free( realbase.bv_val );
954 static int base_candidate(
959 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
960 e->e_nname.bv_val, (long) e->e_id, 0);
967 /* Look for "objectClass Present" in this filter.
968 * Also count depth of filter tree while we're at it.
970 static int oc_filter(
979 if( cur > *max ) *max = cur;
981 switch( f->f_choice ) {
982 case LDAP_FILTER_PRESENT:
983 if (f->f_desc == slap_schema.si_ad_objectClass) {
988 case LDAP_FILTER_AND:
991 for ( f=f->f_and; f; f=f->f_next ) {
992 (void) oc_filter(f, cur, max);
1002 static void search_stack_free( void *key, void *data )
1004 ber_memfree_x(data, NULL);
1007 static void *search_stack( Operation *op )
1009 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1012 if ( op->o_threadctx ) {
1013 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1016 ret = bdb->bi_search_stack;
1020 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1022 if ( op->o_threadctx ) {
1023 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1024 ret, search_stack_free );
1026 bdb->bi_search_stack = ret;
1032 static int search_candidates(
1040 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1042 Filter f, rf, xf, nf;
1044 #ifdef LDAP_COMP_MATCH
1045 AttributeAssertion aa_ref = { NULL, BER_BVNULL, NULL };
1047 AttributeAssertion aa_ref = { NULL, BER_BVNULL };
1050 #ifdef LDAP_COMP_MATCH
1051 AttributeAssertion aa_subentry = { NULL, BER_BVNULL, NULL };
1053 AttributeAssertion aa_subentry = { NULL, BER_BVNULL };
1057 * This routine takes as input a filter (user-filter)
1058 * and rewrites it as follows:
1059 * (&(scope=DN)[(objectClass=subentry)]
1060 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1063 Debug(LDAP_DEBUG_TRACE,
1064 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1065 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1067 xf.f_or = op->oq_search.rs_filter;
1068 xf.f_choice = LDAP_FILTER_OR;
1071 /* If the user's filter uses objectClass=*,
1072 * these clauses are redundant.
1074 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1075 && !get_subentries_visibility(op)) {
1076 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1077 /* match referral objects */
1078 struct berval bv_ref = BER_BVC( "referral" );
1079 rf.f_choice = LDAP_FILTER_EQUALITY;
1081 rf.f_av_desc = slap_schema.si_ad_objectClass;
1082 rf.f_av_value = bv_ref;
1083 rf.f_next = xf.f_or;
1090 f.f_choice = LDAP_FILTER_AND;
1092 /* Dummy; we compute scope separately now */
1093 nf.f_choice = SLAPD_FILTER_COMPUTED;
1094 nf.f_result = LDAP_SUCCESS;
1095 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1096 ? op->oq_search.rs_filter : &xf ;
1097 /* Filter depth increased again, adding dummy clause */
1100 if( get_subentries_visibility( op ) ) {
1101 struct berval bv_subentry = BER_BVC( "subentry" );
1102 sf.f_choice = LDAP_FILTER_EQUALITY;
1103 sf.f_ava = &aa_subentry;
1104 sf.f_av_desc = slap_schema.si_ad_objectClass;
1105 sf.f_av_value = bv_subentry;
1106 sf.f_next = nf.f_next;
1110 /* Allocate IDL stack, plus 1 more for former tmp */
1111 if ( depth+1 > bdb->bi_search_stack_depth ) {
1112 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1114 stack = search_stack( op );
1117 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1118 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1120 rc = bdb_dn2idl( op, e, ids, stack );
1123 if ( rc == LDAP_SUCCESS ) {
1124 rc = bdb_filter_candidates( op, &f, ids,
1125 stack, stack+BDB_IDL_UM_SIZE );
1128 if ( depth+1 > bdb->bi_search_stack_depth ) {
1133 Debug(LDAP_DEBUG_TRACE,
1134 "bdb_search_candidates: failed (rc=%d)\n",
1138 Debug(LDAP_DEBUG_TRACE,
1139 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1141 (long) BDB_IDL_FIRST(ids),
1142 (long) BDB_IDL_LAST(ids) );
1149 parse_paged_cookie( Operation *op, SlapReply *rs )
1152 int rc = LDAP_SUCCESS;
1156 struct berval cookie = BER_BVNULL;
1157 PagedResultsState *ps = op->o_pagedresults_state;
1159 /* this function must be invoked only if the pagedResults
1160 * control has been detected, parsed and partially checked
1161 * by the frontend */
1162 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1164 /* look for the appropriate ctrl structure */
1165 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
1166 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
1172 if ( c[0] == NULL ) {
1173 rs->sr_text = "missing pagedResults control";
1174 return LDAP_PROTOCOL_ERROR;
1177 /* Tested by frontend */
1178 assert( c[0]->ldctl_value.bv_len > 0 );
1180 /* Parse the control value
1181 * realSearchControlValue ::= SEQUENCE {
1182 * size INTEGER (0..maxInt),
1183 * -- requested page size from client
1184 * -- result set size estimate from server
1185 * cookie OCTET STRING
1188 ber = ber_init( &c[0]->ldctl_value );
1189 if ( ber == NULL ) {
1190 rs->sr_text = "internal error";
1194 tag = ber_scanf( ber, "{im}", &size, &cookie );
1196 /* Tested by frontend */
1197 assert( tag != LBER_ERROR );
1198 assert( size >= 0 );
1200 /* cookie decoding/checks deferred to backend... */
1201 if ( cookie.bv_len ) {
1202 PagedResultsCookie reqcookie;
1203 if( cookie.bv_len != sizeof( reqcookie ) ) {
1205 rs->sr_text = "paged results cookie is invalid";
1206 rc = LDAP_PROTOCOL_ERROR;
1210 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
1212 if ( reqcookie > ps->ps_cookie ) {
1214 rs->sr_text = "paged results cookie is invalid";
1215 rc = LDAP_PROTOCOL_ERROR;
1218 } else if ( reqcookie < ps->ps_cookie ) {
1219 rs->sr_text = "paged results cookie is invalid or old";
1220 rc = LDAP_UNWILLING_TO_PERFORM;
1225 /* Initial request. Initialize state. */
1227 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
1228 /* There's another pagedResults control on the
1229 * same connection; reject new pagedResults controls
1230 * (allowed by RFC2696) */
1231 rs->sr_text = "paged results cookie unavailable; try later";
1232 rc = LDAP_UNWILLING_TO_PERFORM;
1241 (void)ber_free( ber, 1 );
1247 send_paged_response(
1253 LDAPControl ctrl, *ctrls[2];
1254 BerElementBuffer berbuf;
1255 BerElement *ber = (BerElement *)&berbuf;
1256 PagedResultsCookie respcookie;
1257 struct berval cookie;
1259 Debug(LDAP_DEBUG_ARGS,
1260 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1261 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1263 BER_BVZERO( &ctrl.ldctl_value );
1267 ber_init2( ber, NULL, LBER_USE_DER );
1270 respcookie = ( PagedResultsCookie )(*lastid);
1271 cookie.bv_len = sizeof( respcookie );
1272 cookie.bv_val = (char *)&respcookie;
1275 respcookie = ( PagedResultsCookie )0;
1276 BER_BVSTR( &cookie, "" );
1279 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1280 op->o_conn->c_pagedresults_state.ps_count =
1281 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1284 /* return size of 0 -- no estimate */
1285 ber_printf( ber, "{iO}", 0, &cookie );
1287 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1291 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1292 ctrls[0]->ldctl_iscritical = 0;
1294 rs->sr_ctrls = ctrls;
1295 rs->sr_err = LDAP_SUCCESS;
1296 send_ldap_result( op, rs );
1297 rs->sr_ctrls = NULL;
1300 (void) ber_free_buf( ber );