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,
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, *matched, lock);
125 /* We found a regular entry. Return this to the caller. The
126 * entry is still locked for Read.
128 if (!is_entry_alias(e)) {
129 rs->sr_err = LDAP_SUCCESS;
137 /* Look for and dereference all aliases within the search scope. Adds
138 * the dereferenced entries to the "ids" list. Requires "stack" to be
139 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
140 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
142 static int search_aliases(
151 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
152 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
153 ID cursora, ida, cursoro, ido, *subscop2;
156 struct berval bv_alias = BER_BVC( "alias" );
157 AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
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, txn, &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, txn, &e->e_nname, BEI(e), subscop,
206 subscop2+BDB_IDL_DB_SIZE );
207 if ( rs->sr_err == DB_LOCK_DEADLOCK )
213 bdb_cache_return_entry_r (bdb, e, &locka);
215 BDB_IDL_CPY(subscop2, subscop);
216 rs->sr_err = bdb_idl_intersection(curscop, subscop);
217 bdb_idl_union( ids, subscop2 );
219 /* Dereference all of the aliases in the current scope. */
221 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
222 ida = bdb_idl_next(curscop, &cursora))
226 rs->sr_err = bdb_cache_find_id(op, txn,
227 ida, &ei, 0, &lockr );
228 if (rs->sr_err != LDAP_SUCCESS) {
229 if ( rs->sr_err == DB_LOCK_DEADLOCK )
231 if ( rs->sr_err == DB_LOCK_NOTGRANTED )
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, a, &lockr);
245 /* Actually dereference the alias */
247 a = deref_base( op, rs, a, &matched, txn, &lockr,
250 /* If the target was not already in our current candidates,
251 * make note of it in the newsubs list. Also
252 * set it in the scopes list so that bdb_search
255 if (bdb_idl_insert(ids, a->e_id) == 0) {
256 bdb_idl_insert(newsubs, a->e_id);
257 bdb_idl_insert(scopes, a->e_id);
259 bdb_cache_return_entry_r( bdb, a, &lockr);
261 } else if ( rs->sr_err == DB_LOCK_DEADLOCK ) {
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, matched, &lockr );
271 /* If this is a OneLevel search, we're done; oldsubs only had one
272 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
274 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
276 ido = bdb_idl_next( oldsubs, &cursoro );
278 /* If we're done processing the old scopes, did we add any new
279 * scopes in this iteration? If so, go back and do those now.
282 if (BDB_IDL_IS_ZERO(newsubs)) break;
283 BDB_IDL_CPY(oldsubs, newsubs);
284 BDB_IDL_ZERO(newsubs);
286 ido = bdb_idl_first( oldsubs, &cursoro );
289 /* Find the entry corresponding to the next scope. If it can't
290 * be found, ignore it and move on. This should never happen;
291 * we should never see the ID of an entry that doesn't exist.
292 * Set the name so that the scope's IDL can be retrieved.
296 rs->sr_err = bdb_cache_find_id(op, txn, ido, &ei,
298 if ( rs->sr_err != LDAP_SUCCESS ) {
299 if ( rs->sr_err == DB_LOCK_DEADLOCK )
301 if ( 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;
319 Entry *matched = NULL;
321 AttributeName *attrs;
322 struct berval realbase = BER_BVNULL;
326 int tentries = 0, nentries = 0;
330 struct bdb_op_info *opinfo = NULL;
334 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
335 attrs = op->oq_search.rs_attrs;
337 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
338 if ( oex->oe_key == bdb )
341 opinfo = (struct bdb_op_info *) oex;
343 manageDSAit = get_manageDSAit( op );
345 if ( opinfo && opinfo->boi_txn ) {
346 ltid = opinfo->boi_txn;
348 rs->sr_err = bdb_reader_get( op, bdb->bi_dbenv, <id );
354 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
359 e_root = bdb->bi_cache.c_dntree.bei_e;
360 if ( op->o_req_ndn.bv_len == 0 ) {
361 /* DIT root special case */
362 ei = e_root->e_private;
363 rs->sr_err = LDAP_SUCCESS;
365 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
366 BDB_IDL_ZERO(candidates);
369 /* get entry with reader lock */
370 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei,
381 case DB_LOCK_DEADLOCK:
384 opinfo->boi_err = rs->sr_err;
387 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
389 case DB_LOCK_NOTGRANTED:
392 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
396 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
397 if ( matched && is_entry_alias( matched )) {
400 stub.bv_val = op->o_req_ndn.bv_val;
401 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
402 e = deref_base( op, rs, matched, &matched, ltid, &lock,
405 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
407 bdb_cache_return_entry_r (bdb, e, &lock);
411 } else if ( e && is_entry_alias( e )) {
412 e = deref_base( op, rs, e, &matched, ltid, &lock,
418 struct berval matched_dn = BER_BVNULL;
420 if ( matched != NULL ) {
421 BerVarray erefs = NULL;
423 /* return referral only if "disclose"
424 * is granted on the object */
425 if ( ! access_allowed( op, matched,
426 slap_schema.si_ad_entry,
427 NULL, ACL_DISCLOSE, NULL ) )
429 rs->sr_err = LDAP_NO_SUCH_OBJECT;
432 ber_dupbv( &matched_dn, &matched->e_name );
434 erefs = is_entry_referral( matched )
435 ? get_entry_referrals( op, matched )
437 if ( rs->sr_err == DB_NOTFOUND )
438 rs->sr_err = LDAP_REFERRAL;
439 rs->sr_matched = matched_dn.bv_val;
442 #ifdef SLAP_ZONE_ALLOC
443 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
445 bdb_cache_return_entry_r (bdb, matched, &lock);
449 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
450 &op->o_req_dn, op->oq_search.rs_scope );
451 ber_bvarray_free( erefs );
455 #ifdef SLAP_ZONE_ALLOC
456 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
458 rs->sr_ref = referral_rewrite( default_referral,
459 NULL, &op->o_req_dn, op->oq_search.rs_scope );
460 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
463 send_ldap_result( op, rs );
466 ber_bvarray_free( rs->sr_ref );
469 if ( !BER_BVISNULL( &matched_dn ) ) {
470 ber_memfree( matched_dn.bv_val );
471 rs->sr_matched = NULL;
476 /* NOTE: __NEW__ "search" access is required
477 * on searchBase object */
478 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
479 NULL, ACL_SEARCH, NULL, &mask ) )
481 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
482 rs->sr_err = LDAP_NO_SUCH_OBJECT;
484 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
487 #ifdef SLAP_ZONE_ALLOC
488 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
491 bdb_cache_return_entry_r(bdb, e, &lock);
493 send_ldap_result( op, rs );
497 if ( !manageDSAit && e != e_root && is_entry_referral( e ) ) {
498 /* entry is a referral, don't allow add */
499 struct berval matched_dn = BER_BVNULL;
500 BerVarray erefs = NULL;
502 ber_dupbv( &matched_dn, &e->e_name );
503 erefs = get_entry_referrals( op, e );
505 rs->sr_err = LDAP_REFERRAL;
507 #ifdef SLAP_ZONE_ALLOC
508 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
510 bdb_cache_return_entry_r( bdb, e, &lock );
514 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
515 &op->o_req_dn, op->oq_search.rs_scope );
516 ber_bvarray_free( erefs );
519 rs->sr_text = "bad_referral object";
523 Debug( LDAP_DEBUG_TRACE,
524 LDAP_XSTRING(bdb_search) ": entry is referral\n",
527 rs->sr_matched = matched_dn.bv_val;
528 send_ldap_result( op, rs );
530 ber_bvarray_free( rs->sr_ref );
532 ber_memfree( matched_dn.bv_val );
533 rs->sr_matched = NULL;
537 if ( get_assert( op ) &&
538 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
540 rs->sr_err = LDAP_ASSERTION_FAILED;
541 #ifdef SLAP_ZONE_ALLOC
542 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
545 bdb_cache_return_entry_r(bdb, e, &lock);
547 send_ldap_result( op, rs );
551 /* compute it anyway; root does not use it */
552 stoptime = op->o_time + op->ors_tlimit;
554 /* need normalized dn below */
555 ber_dupbv( &realbase, &e->e_nname );
557 /* Copy info to base, must free entry before accessing the database
558 * in search_candidates, to avoid deadlocks.
560 base.e_private = e->e_private;
561 base.e_nname = realbase;
564 #ifdef SLAP_ZONE_ALLOC
565 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
568 bdb_cache_return_entry_r(bdb, e, &lock);
572 /* select candidates */
573 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
574 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
577 BDB_IDL_ZERO( candidates );
578 BDB_IDL_ZERO( scopes );
579 rs->sr_err = search_candidates( op, rs, &base,
580 ltid, candidates, scopes );
581 if ( rs->sr_err == DB_LOCK_DEADLOCK ) {
583 opinfo->boi_err = rs->sr_err;
584 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
589 /* start cursor at beginning of candidates.
593 if ( candidates[0] == 0 ) {
594 Debug( LDAP_DEBUG_TRACE,
595 LDAP_XSTRING(bdb_search) ": no candidates\n",
601 /* if not root and candidates exceed to-be-checked entries, abort */
602 if ( op->ors_limit /* isroot == FALSE */ &&
603 op->ors_limit->lms_s_unchecked != -1 &&
604 BDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
606 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
607 send_ldap_result( op, rs );
608 rs->sr_err = LDAP_SUCCESS;
612 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
613 !op->ors_limit->lms_s_pr_hide )
615 tentries = BDB_IDL_N(candidates);
618 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
619 PagedResultsState *ps = op->o_pagedresults_state;
620 /* deferred cookie parsing */
621 rs->sr_err = parse_paged_cookie( op, rs );
622 if ( rs->sr_err != LDAP_SUCCESS ) {
623 send_ldap_result( op, rs );
627 cursor = (ID) ps->ps_cookie;
628 if ( cursor && ps->ps_size == 0 ) {
629 rs->sr_err = LDAP_SUCCESS;
630 rs->sr_text = "search abandoned by pagedResult size=0";
631 send_ldap_result( op, rs );
634 id = bdb_idl_first( candidates, &cursor );
636 Debug( LDAP_DEBUG_TRACE,
637 LDAP_XSTRING(bdb_search)
638 ": no paged results candidates\n",
640 send_paged_response( op, rs, &lastid, 0 );
642 rs->sr_err = LDAP_OTHER;
645 nentries = ps->ps_count;
646 if ( id == (ID)ps->ps_cookie )
647 id = bdb_idl_next( candidates, &cursor );
651 for ( id = bdb_idl_first( candidates, &cursor );
652 id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
658 /* check for abandon */
659 if ( op->o_abandon ) {
660 rs->sr_err = SLAPD_ABANDON;
664 /* mostly needed by internal searches,
665 * e.g. related to syncrepl, for whom
666 * abandon does not get set... */
667 if ( slapd_shutdown ) {
668 rs->sr_err = LDAP_UNAVAILABLE;
669 send_ldap_disconnect( op, rs );
673 /* check time limit */
674 if ( op->ors_tlimit != SLAP_NO_LIMIT
675 && slap_get_time() > stoptime )
677 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
678 rs->sr_ref = rs->sr_v2ref;
679 send_ldap_result( op, rs );
680 rs->sr_err = LDAP_SUCCESS;
684 /* If we inspect more entries than will
685 * fit into the entry cache, stop caching
686 * any subsequent entries
689 if ( nentries > bdb->bi_cache.c_maxsize && !idflag )
693 /* get the entry with reader lock */
695 rs->sr_err = bdb_cache_find_id( op, ltid,
696 id, &ei, idflag, &lock );
698 if (rs->sr_err == LDAP_BUSY) {
699 rs->sr_text = "ldap server busy";
700 send_ldap_result( op, rs );
703 } else if ( rs->sr_err == DB_LOCK_DEADLOCK ) {
705 opinfo->boi_err = rs->sr_err;
706 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
709 } else if ( rs->sr_err == DB_LOCK_NOTGRANTED )
711 goto fetch_entry_retry;
712 } else if ( rs->sr_err == LDAP_OTHER ) {
713 rs->sr_text = "internal error";
714 send_ldap_result( op, rs );
718 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
725 if( !BDB_IDL_IS_RANGE(candidates) ) {
726 /* only complain for non-range IDLs */
727 Debug( LDAP_DEBUG_TRACE,
728 LDAP_XSTRING(bdb_search)
729 ": candidate %ld not found\n",
738 if ( is_entry_subentry( e ) ) {
739 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
740 if(!get_subentries_visibility( op )) {
741 /* only subentries are visible */
745 } else if ( get_subentries( op ) &&
746 !get_subentries_visibility( op ))
748 /* only subentries are visible */
752 } else if ( get_subentries_visibility( op )) {
753 /* only subentries are visible */
757 /* Does this candidate actually satisfy the search scope?
759 * Note that we don't lock access to the bei_parent pointer.
760 * Since only leaf nodes can be deleted, the parent of any
761 * node will always be a valid node. Also since we have
762 * a Read lock on the data, it cannot be renamed out of the
763 * scope while we are looking at it, and unless we're using
764 * BDB_HIER, its parents cannot be moved either.
767 switch( op->ors_scope ) {
768 case LDAP_SCOPE_BASE:
769 /* This is always true, yes? */
770 if ( id == base.e_id ) scopeok = 1;
773 case LDAP_SCOPE_ONELEVEL:
774 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
777 #ifdef LDAP_SCOPE_CHILDREN
778 case LDAP_SCOPE_CHILDREN:
779 if ( id == base.e_id ) break;
782 case LDAP_SCOPE_SUBTREE: {
784 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
785 if ( tmp->bei_id == base.e_id ) {
793 /* aliases were already dereferenced in candidate list */
794 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
795 /* but if the search base is an alias, and we didn't
796 * deref it when finding, return it.
798 if ( is_entry_alias(e) &&
799 ((op->ors_deref & LDAP_DEREF_FINDING) ||
800 !bvmatch(&e->e_nname, &op->o_req_ndn)))
805 /* scopes is only non-empty for onelevel or subtree */
806 if ( !scopeok && BDB_IDL_N(scopes) ) {
808 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
809 x = bdb_idl_search( scopes, e->e_id );
810 if ( scopes[x] == e->e_id ) scopeok = 1;
812 /* subtree, walk up the tree */
813 EntryInfo *tmp = BEI(e);
814 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
815 x = bdb_idl_search( scopes, tmp->bei_id );
816 if ( scopes[x] == tmp->bei_id ) {
825 /* Not in scope, ignore it */
828 Debug( LDAP_DEBUG_TRACE,
829 LDAP_XSTRING(bdb_search)
830 ": %ld scope not okay\n",
836 * if it's a referral, add it to the list of referrals. only do
837 * this for non-base searches, and don't check the filter
838 * explicitly here since it's only a candidate anyway.
840 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
841 && is_entry_referral( e ) )
843 BerVarray erefs = get_entry_referrals( op, e );
844 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
845 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
846 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
848 send_search_reference( op, rs );
850 ber_bvarray_free( rs->sr_ref );
851 ber_bvarray_free( erefs );
857 if ( !manageDSAit && is_entry_glue( e )) {
861 /* if it matches the filter and scope, send it */
862 rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
864 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
865 /* check size limit */
866 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
867 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
868 #ifdef SLAP_ZONE_ALLOC
869 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
871 bdb_cache_return_entry_r( bdb, e, &lock );
873 send_paged_response( op, rs, &lastid, tentries );
881 rs->sr_attrs = op->oq_search.rs_attrs;
882 rs->sr_operational_attrs = NULL;
885 rs->sr_err = LDAP_SUCCESS;
886 rs->sr_err = send_search_entry( op, rs );
888 switch ( rs->sr_err ) {
889 case LDAP_SUCCESS: /* entry sent ok */
891 default: /* entry not sent */
893 case LDAP_UNAVAILABLE:
894 case LDAP_SIZELIMIT_EXCEEDED:
895 #ifdef SLAP_ZONE_ALLOC
896 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
898 bdb_cache_return_entry_r(bdb, e, &lock);
901 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
902 rs->sr_ref = rs->sr_v2ref;
903 send_ldap_result( op, rs );
904 rs->sr_err = LDAP_SUCCESS;
907 rs->sr_err = LDAP_OTHER;
914 Debug( LDAP_DEBUG_TRACE,
915 LDAP_XSTRING(bdb_search)
916 ": %ld does not match filter\n",
922 /* free reader lock */
923 #ifdef SLAP_ZONE_ALLOC
924 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
926 bdb_cache_return_entry_r( bdb, e , &lock );
934 rs->sr_ref = rs->sr_v2ref;
935 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
936 rs->sr_rspoid = NULL;
937 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
938 send_paged_response( op, rs, NULL, 0 );
940 send_ldap_result( op, rs );
943 rs->sr_err = LDAP_SUCCESS;
947 ber_bvarray_free( rs->sr_v2ref );
950 if( realbase.bv_val ) ch_free( realbase.bv_val );
956 static int base_candidate(
961 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
962 e->e_nname.bv_val, (long) e->e_id, 0);
969 /* Look for "objectClass Present" in this filter.
970 * Also count depth of filter tree while we're at it.
972 static int oc_filter(
981 if( cur > *max ) *max = cur;
983 switch( f->f_choice ) {
984 case LDAP_FILTER_PRESENT:
985 if (f->f_desc == slap_schema.si_ad_objectClass) {
990 case LDAP_FILTER_AND:
993 for ( f=f->f_and; f; f=f->f_next ) {
994 (void) oc_filter(f, cur, max);
1004 static void search_stack_free( void *key, void *data )
1006 ber_memfree_x(data, NULL);
1009 static void *search_stack( Operation *op )
1011 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1014 if ( op->o_threadctx ) {
1015 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
1018 ret = bdb->bi_search_stack;
1022 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1024 if ( op->o_threadctx ) {
1025 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1026 ret, search_stack_free, NULL, NULL );
1028 bdb->bi_search_stack = ret;
1034 static int search_candidates(
1042 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1044 Filter f, rf, xf, nf;
1046 AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1048 AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1051 * This routine takes as input a filter (user-filter)
1052 * and rewrites it as follows:
1053 * (&(scope=DN)[(objectClass=subentry)]
1054 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1057 Debug(LDAP_DEBUG_TRACE,
1058 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1059 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1061 xf.f_or = op->oq_search.rs_filter;
1062 xf.f_choice = LDAP_FILTER_OR;
1065 /* If the user's filter uses objectClass=*,
1066 * these clauses are redundant.
1068 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1069 && !get_subentries_visibility(op)) {
1070 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1071 /* match referral objects */
1072 struct berval bv_ref = BER_BVC( "referral" );
1073 rf.f_choice = LDAP_FILTER_EQUALITY;
1075 rf.f_av_desc = slap_schema.si_ad_objectClass;
1076 rf.f_av_value = bv_ref;
1077 rf.f_next = xf.f_or;
1084 f.f_choice = LDAP_FILTER_AND;
1086 /* Dummy; we compute scope separately now */
1087 nf.f_choice = SLAPD_FILTER_COMPUTED;
1088 nf.f_result = LDAP_SUCCESS;
1089 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1090 ? op->oq_search.rs_filter : &xf ;
1091 /* Filter depth increased again, adding dummy clause */
1094 if( get_subentries_visibility( op ) ) {
1095 struct berval bv_subentry = BER_BVC( "subentry" );
1096 sf.f_choice = LDAP_FILTER_EQUALITY;
1097 sf.f_ava = &aa_subentry;
1098 sf.f_av_desc = slap_schema.si_ad_objectClass;
1099 sf.f_av_value = bv_subentry;
1100 sf.f_next = nf.f_next;
1104 /* Allocate IDL stack, plus 1 more for former tmp */
1105 if ( depth+1 > bdb->bi_search_stack_depth ) {
1106 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1108 stack = search_stack( op );
1111 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1112 rc = search_aliases( op, rs, e, txn, ids, scopes, stack );
1114 rc = bdb_dn2idl( op, txn, &e->e_nname, BEI(e), ids, stack );
1117 if ( rc == LDAP_SUCCESS ) {
1118 rc = bdb_filter_candidates( op, txn, &f, ids,
1119 stack, stack+BDB_IDL_UM_SIZE );
1122 if ( depth+1 > bdb->bi_search_stack_depth ) {
1127 Debug(LDAP_DEBUG_TRACE,
1128 "bdb_search_candidates: failed (rc=%d)\n",
1132 Debug(LDAP_DEBUG_TRACE,
1133 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1135 (long) BDB_IDL_FIRST(ids),
1136 (long) BDB_IDL_LAST(ids) );
1143 parse_paged_cookie( Operation *op, SlapReply *rs )
1145 int rc = LDAP_SUCCESS;
1146 PagedResultsState *ps = op->o_pagedresults_state;
1148 /* this function must be invoked only if the pagedResults
1149 * control has been detected, parsed and partially checked
1150 * by the frontend */
1151 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1153 /* cookie decoding/checks deferred to backend... */
1154 if ( ps->ps_cookieval.bv_len ) {
1155 PagedResultsCookie reqcookie;
1156 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1158 rs->sr_text = "paged results cookie is invalid";
1159 rc = LDAP_PROTOCOL_ERROR;
1163 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1165 if ( reqcookie > ps->ps_cookie ) {
1167 rs->sr_text = "paged results cookie is invalid";
1168 rc = LDAP_PROTOCOL_ERROR;
1171 } else if ( reqcookie < ps->ps_cookie ) {
1172 rs->sr_text = "paged results cookie is invalid or old";
1173 rc = LDAP_UNWILLING_TO_PERFORM;
1185 send_paged_response(
1191 LDAPControl ctrl, *ctrls[2];
1192 BerElementBuffer berbuf;
1193 BerElement *ber = (BerElement *)&berbuf;
1194 PagedResultsCookie respcookie;
1195 struct berval cookie;
1197 Debug(LDAP_DEBUG_ARGS,
1198 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1199 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1201 BER_BVZERO( &ctrl.ldctl_value );
1205 ber_init2( ber, NULL, LBER_USE_DER );
1208 respcookie = ( PagedResultsCookie )(*lastid);
1209 cookie.bv_len = sizeof( respcookie );
1210 cookie.bv_val = (char *)&respcookie;
1213 respcookie = ( PagedResultsCookie )0;
1214 BER_BVSTR( &cookie, "" );
1217 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1218 op->o_conn->c_pagedresults_state.ps_count =
1219 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1222 /* return size of 0 -- no estimate */
1223 ber_printf( ber, "{iO}", 0, &cookie );
1225 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1229 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1230 ctrls[0]->ldctl_iscritical = 0;
1232 rs->sr_ctrls = ctrls;
1233 rs->sr_err = LDAP_SUCCESS;
1234 send_ldap_result( op, rs );
1235 rs->sr_ctrls = NULL;
1238 (void) ber_free_buf( ber );