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 AttributeAssertion aa_alias = { NULL, BER_BVNULL, NULL };
155 DB_LOCK locka, lockr;
158 aliases = stack; /* IDL of all aliases in the database */
159 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
160 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
161 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
162 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
163 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
164 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
166 /* A copy of subscop, because subscop gets clobbered by
167 * the bdb_idl_union/intersection routines
169 subscop2 = tmp + BDB_IDL_DB_SIZE;
171 af.f_choice = LDAP_FILTER_EQUALITY;
172 af.f_ava = &aa_alias;
173 af.f_av_desc = slap_schema.si_ad_objectClass;
174 af.f_av_value = bv_alias;
177 /* Find all aliases in database */
178 BDB_IDL_ZERO( aliases );
179 rs->sr_err = bdb_filter_candidates( op, &af, aliases,
181 if (rs->sr_err != LDAP_SUCCESS) {
185 oldsubs[1] = e->e_id;
188 BDB_IDL_ZERO( visited );
189 BDB_IDL_ZERO( newsubs );
192 ido = bdb_idl_first( oldsubs, &cursoro );
195 /* Set curscop to only the aliases in the current scope. Start with
196 * all the aliases, obtain the IDL for the current scope, and then
197 * get the intersection of these two IDLs. Add the current scope
198 * to the cumulative list of candidates.
200 BDB_IDL_CPY( curscop, aliases );
201 rs->sr_err = bdb_dn2idl( op, e, subscop,
202 subscop2+BDB_IDL_DB_SIZE );
206 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
208 BDB_IDL_CPY(subscop2, subscop);
209 rs->sr_err = bdb_idl_intersection(curscop, subscop);
210 bdb_idl_union( ids, subscop2 );
212 /* Dereference all of the aliases in the current scope. */
214 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
215 ida = bdb_idl_next(curscop, &cursora))
219 rs->sr_err = bdb_cache_find_id(op, NULL,
220 ida, &ei, 0, locker, &lockr );
221 if (rs->sr_err != LDAP_SUCCESS) {
222 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
223 rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
228 /* This should only happen if the curscop IDL has maxed out and
229 * turned into a range that spans IDs indiscriminately
231 if (!is_entry_alias(a)) {
232 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
237 /* Actually dereference the alias */
239 a = deref_base( op, rs, a, &matched, locker, &lockr,
242 /* If the target was not already in our current candidates,
243 * make note of it in the newsubs list. Also
244 * set it in the scopes list so that bdb_search
247 if (bdb_idl_insert(ids, a->e_id) == 0) {
248 bdb_idl_insert(newsubs, a->e_id);
249 bdb_idl_insert(scopes, a->e_id);
251 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
254 } else if (matched) {
255 /* Alias could not be dereferenced, or it deref'd to
256 * an ID we've already seen. Ignore it.
258 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
263 /* If this is a OneLevel search, we're done; oldsubs only had one
264 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
266 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
268 ido = bdb_idl_next( oldsubs, &cursoro );
270 /* If we're done processing the old scopes, did we add any new
271 * scopes in this iteration? If so, go back and do those now.
274 if (BDB_IDL_IS_ZERO(newsubs)) break;
275 BDB_IDL_CPY(oldsubs, newsubs);
276 BDB_IDL_ZERO(newsubs);
278 ido = bdb_idl_first( oldsubs, &cursoro );
281 /* Find the entry corresponding to the next scope. If it can't
282 * be found, ignore it and move on. This should never happen;
283 * we should never see the ID of an entry that doesn't exist.
284 * Set the name so that the scope's IDL can be retrieved.
288 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
290 if ( rs->sr_err != LDAP_SUCCESS ) {
291 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
292 rs->sr_err == DB_LOCK_NOTGRANTED )
302 bdb_search( Operation *op, SlapReply *rs )
304 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
307 ID candidates[BDB_IDL_UM_SIZE];
308 ID scopes[BDB_IDL_DB_SIZE];
309 Entry *e = NULL, base, e_root = {0};
310 Entry *matched = NULL;
311 EntryInfo *ei, ei_root = {0};
312 struct berval realbase = BER_BVNULL;
317 AttributeName *attrs;
319 u_int32_t locker = 0;
321 struct bdb_op_info *opinfo = NULL;
324 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
325 attrs = op->oq_search.rs_attrs;
327 opinfo = (struct bdb_op_info *) op->o_private;
329 manageDSAit = get_manageDSAit( op );
331 if ( opinfo && opinfo->boi_txn ) {
332 ltid = opinfo->boi_txn;
333 locker = TXN_ID( ltid );
335 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
341 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
346 if ( op->o_req_ndn.bv_len == 0 ) {
347 /* DIT root special case */
348 ei_root.bei_e = &e_root;
349 ei_root.bei_parent = &ei_root;
350 e_root.e_private = &ei_root;
352 BER_BVSTR( &e_root.e_nname, "" );
353 BER_BVSTR( &e_root.e_name, "" );
355 rs->sr_err = LDAP_SUCCESS;
358 /* get entry with reader lock */
359 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei,
371 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
373 LOCK_ID_FREE (bdb->bi_dbenv, locker );
375 case DB_LOCK_DEADLOCK:
376 case DB_LOCK_NOTGRANTED:
379 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
381 LOCK_ID_FREE (bdb->bi_dbenv, locker );
385 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
386 BDB_IDL_ZERO(candidates);
387 e = deref_base( op, rs, e, &matched, locker, &lock,
392 struct berval matched_dn = BER_BVNULL;
394 if ( matched != NULL ) {
395 BerVarray erefs = NULL;
397 #ifdef SLAP_ACL_HONOR_DISCLOSE
398 /* return referral only if "disclose"
399 * is granted on the object */
400 if ( ! access_allowed( op, matched,
401 slap_schema.si_ad_entry,
402 NULL, ACL_DISCLOSE, NULL ) )
404 rs->sr_err = LDAP_NO_SUCH_OBJECT;
407 #endif /* SLAP_ACL_HONOR_DISCLOSE */
409 ber_dupbv( &matched_dn, &matched->e_name );
411 erefs = is_entry_referral( matched )
412 ? get_entry_referrals( op, matched )
414 rs->sr_err = LDAP_REFERRAL;
415 rs->sr_matched = matched_dn.bv_val;
418 #ifdef SLAP_ZONE_ALLOC
419 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
421 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
426 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
427 &op->o_req_dn, op->oq_search.rs_scope );
428 ber_bvarray_free( erefs );
432 #ifdef SLAP_ZONE_ALLOC
433 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
435 rs->sr_ref = referral_rewrite( default_referral,
436 NULL, &op->o_req_dn, op->oq_search.rs_scope );
437 rs->sr_err = LDAP_REFERRAL;
440 send_ldap_result( op, rs );
443 LOCK_ID_FREE (bdb->bi_dbenv, locker );
445 ber_bvarray_free( rs->sr_ref );
448 if ( !BER_BVISNULL( &matched_dn ) ) {
449 ber_memfree( matched_dn.bv_val );
450 rs->sr_matched = NULL;
455 #ifdef SLAP_ACL_HONOR_DISCLOSE
456 /* NOTE: __NEW__ "search" access is required
457 * on searchBase object */
458 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
459 NULL, ACL_SEARCH, NULL, &mask ) )
461 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
462 rs->sr_err = LDAP_NO_SUCH_OBJECT;
464 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
467 #ifdef SLAP_ZONE_ALLOC
468 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
470 if ( e != &e_root ) {
471 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
473 send_ldap_result( op, rs );
476 #endif /* SLAP_ACL_HONOR_DISCLOSE */
478 if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
479 /* entry is a referral, don't allow add */
480 struct berval matched_dn = BER_BVNULL;
481 BerVarray erefs = NULL;
483 ber_dupbv( &matched_dn, &e->e_name );
484 erefs = get_entry_referrals( op, e );
486 rs->sr_err = LDAP_REFERRAL;
488 #ifdef SLAP_ZONE_ALLOC
489 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
491 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
495 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
496 &op->o_req_dn, op->oq_search.rs_scope );
497 ber_bvarray_free( erefs );
500 rs->sr_text = "bad_referral object";
504 Debug( LDAP_DEBUG_TRACE,
505 LDAP_XSTRING(bdb_search) ": entry is referral\n",
508 rs->sr_matched = matched_dn.bv_val;
509 send_ldap_result( op, rs );
512 LOCK_ID_FREE (bdb->bi_dbenv, locker );
514 ber_bvarray_free( rs->sr_ref );
516 ber_memfree( matched_dn.bv_val );
517 rs->sr_matched = NULL;
521 if ( get_assert( op ) &&
522 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
524 rs->sr_err = LDAP_ASSERTION_FAILED;
525 #ifdef SLAP_ZONE_ALLOC
526 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
528 if ( e != &e_root ) {
529 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
531 send_ldap_result( op, rs );
535 /* compute it anyway; root does not use it */
536 stoptime = op->o_time + op->ors_tlimit;
538 /* need normalized dn below */
539 ber_dupbv( &realbase, &e->e_nname );
541 /* Copy info to base, must free entry before accessing the database
542 * in search_candidates, to avoid deadlocks.
544 base.e_private = e->e_private;
545 base.e_nname = realbase;
548 #ifdef SLAP_ZONE_ALLOC
549 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
551 if ( e != &e_root ) {
552 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
556 /* select candidates */
557 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
558 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
561 BDB_IDL_ZERO( candidates );
562 BDB_IDL_ZERO( scopes );
563 rs->sr_err = search_candidates( op, rs, &base,
564 locker, candidates, scopes );
567 /* start cursor at beginning of candidates.
571 if ( candidates[0] == 0 ) {
572 Debug( LDAP_DEBUG_TRACE,
573 LDAP_XSTRING(bdb_search) ": no candidates\n",
579 /* if not root and candidates exceed to-be-checked entries, abort */
580 if ( op->ors_limit /* isroot == FALSE */ &&
581 op->ors_limit->lms_s_unchecked != -1 &&
582 BDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
584 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
585 send_ldap_result( op, rs );
586 rs->sr_err = LDAP_SUCCESS;
590 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
591 !op->ors_limit->lms_s_pr_hide )
593 tentries = BDB_IDL_N(candidates);
596 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
597 PagedResultsState *ps = op->o_pagedresults_state;
598 /* deferred cookie parsing */
599 rs->sr_err = parse_paged_cookie( op, rs );
600 if ( rs->sr_err != LDAP_SUCCESS ) {
601 send_ldap_result( op, rs );
605 if ( (ID)( ps->ps_cookie ) == 0 ) {
606 id = bdb_idl_first( candidates, &cursor );
609 if ( ps->ps_size == 0 ) {
610 rs->sr_err = LDAP_SUCCESS;
611 rs->sr_text = "search abandoned by pagedResult size=0";
612 send_ldap_result( op, rs );
615 for ( id = bdb_idl_first( candidates, &cursor );
617 id <= (ID)( ps->ps_cookie );
618 id = bdb_idl_next( candidates, &cursor ) )
624 if ( cursor == NOID ) {
625 Debug( LDAP_DEBUG_TRACE,
626 LDAP_XSTRING(bdb_search)
627 ": no paged results candidates\n",
629 send_paged_response( op, rs, &lastid, 0 );
631 rs->sr_err = LDAP_OTHER;
637 for ( id = bdb_idl_first( candidates, &cursor );
638 id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
644 /* check for abandon */
645 if ( op->o_abandon ) {
646 rs->sr_err = SLAPD_ABANDON;
650 /* check time limit */
651 if ( op->ors_tlimit != SLAP_NO_LIMIT
652 && slap_get_time() > stoptime )
654 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
655 rs->sr_ref = rs->sr_v2ref;
656 send_ldap_result( op, rs );
657 rs->sr_err = LDAP_SUCCESS;
662 /* get the entry with reader lock */
664 rs->sr_err = bdb_cache_find_id( op, ltid,
665 id, &ei, 0, locker, &lock );
667 if (rs->sr_err == LDAP_BUSY) {
668 rs->sr_text = "ldap server busy";
669 send_ldap_result( op, rs );
672 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
673 || rs->sr_err == DB_LOCK_NOTGRANTED )
675 goto fetch_entry_retry;
678 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
685 if( !BDB_IDL_IS_RANGE(candidates) ) {
686 /* only complain for non-range IDLs */
687 Debug( LDAP_DEBUG_TRACE,
688 LDAP_XSTRING(bdb_search)
689 ": candidate %ld not found\n",
698 #ifdef BDB_SUBENTRIES
700 if ( is_entry_subentry( e ) ) {
701 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
702 if(!get_subentries_visibility( op )) {
703 /* only subentries are visible */
707 } else if ( get_subentries( op ) &&
708 !get_subentries_visibility( op ))
710 /* only subentries are visible */
714 } else if ( get_subentries_visibility( op )) {
715 /* only subentries are visible */
719 #endif /* BDB_SUBENTRIES */
721 /* Does this candidate actually satisfy the search scope?
723 * Note that we don't lock access to the bei_parent pointer.
724 * Since only leaf nodes can be deleted, the parent of any
725 * node will always be a valid node. Also since we have
726 * a Read lock on the data, it cannot be renamed out of the
727 * scope while we are looking at it, and unless we're using
728 * BDB_HIER, its parents cannot be moved either.
730 switch( op->ors_scope ) {
731 case LDAP_SCOPE_BASE:
732 /* This is always true, yes? */
733 if ( id == base.e_id ) scopeok = 1;
736 case LDAP_SCOPE_ONELEVEL:
737 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
740 #ifdef LDAP_SCOPE_CHILDREN
741 case LDAP_SCOPE_CHILDREN:
742 if ( id == base.e_id ) break;
745 case LDAP_SCOPE_SUBTREE: {
747 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
748 if ( tmp->bei_id == base.e_id ) {
756 /* aliases were already dereferenced in candidate list */
757 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
758 /* but if the search base is an alias, and we didn't
759 * deref it when finding, return it.
761 if ( is_entry_alias(e) &&
762 ((op->ors_deref & LDAP_DEREF_FINDING) ||
763 !bvmatch(&e->e_nname, &op->o_req_ndn)))
768 /* scopes is only non-empty for onelevel or subtree */
769 if ( !scopeok && BDB_IDL_N(scopes) ) {
771 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
772 x = bdb_idl_search( scopes, e->e_id );
773 if ( scopes[x] == e->e_id ) scopeok = 1;
775 /* subtree, walk up the tree */
776 EntryInfo *tmp = BEI(e);
777 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
778 x = bdb_idl_search( scopes, tmp->bei_id );
779 if ( scopes[x] == tmp->bei_id ) {
788 /* Not in scope, ignore it */
791 Debug( LDAP_DEBUG_TRACE,
792 LDAP_XSTRING(bdb_search)
793 ": %ld scope not okay\n",
799 * if it's a referral, add it to the list of referrals. only do
800 * this for non-base searches, and don't check the filter
801 * explicitly here since it's only a candidate anyway.
803 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
804 && is_entry_referral( e ) )
806 BerVarray erefs = get_entry_referrals( op, e );
807 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
808 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
809 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
811 send_search_reference( op, rs );
813 ber_bvarray_free( rs->sr_ref );
814 ber_bvarray_free( erefs );
820 if ( !manageDSAit && is_entry_glue( e )) {
824 /* if it matches the filter and scope, send it */
825 rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
827 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
828 /* check size limit */
829 if ( --op->ors_slimit == -1) {
830 #ifdef SLAP_ZONE_ALLOC
831 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
833 bdb_cache_return_entry_r( bdb->bi_dbenv,
834 &bdb->bi_cache, e, &lock );
837 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
838 rs->sr_ref = rs->sr_v2ref;
839 send_ldap_result( op, rs );
840 rs->sr_err = LDAP_SUCCESS;
844 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
845 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
846 send_paged_response( op, rs, &lastid, tentries );
855 rs->sr_attrs = op->oq_search.rs_attrs;
856 rs->sr_operational_attrs = NULL;
859 rs->sr_err = LDAP_SUCCESS;
860 result = send_search_entry( op, rs );
863 case 0: /* entry sent ok */
865 case 1: /* entry not sent */
867 case -1: /* connection closed */
868 #ifdef SLAP_ZONE_ALLOC
869 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
871 bdb_cache_return_entry_r(bdb->bi_dbenv,
872 &bdb->bi_cache, e, &lock);
875 rs->sr_err = LDAP_OTHER;
881 Debug( LDAP_DEBUG_TRACE,
882 LDAP_XSTRING(bdb_search)
883 ": %ld does not match filter\n",
889 /* free reader lock */
890 #ifdef SLAP_ZONE_ALLOC
891 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
893 bdb_cache_return_entry_r( bdb->bi_dbenv,
894 &bdb->bi_cache, e , &lock );
899 ldap_pvt_thread_yield();
904 rs->sr_ref = rs->sr_v2ref;
905 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
906 rs->sr_rspoid = NULL;
907 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
908 send_paged_response( op, rs, NULL, 0 );
910 send_ldap_result( op, rs );
913 rs->sr_err = LDAP_SUCCESS;
917 LOCK_ID_FREE( bdb->bi_dbenv, locker );
920 ber_bvarray_free( rs->sr_v2ref );
923 if( realbase.bv_val ) ch_free( realbase.bv_val );
929 static int base_candidate(
934 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
935 e->e_nname.bv_val, (long) e->e_id, 0);
942 /* Look for "objectClass Present" in this filter.
943 * Also count depth of filter tree while we're at it.
945 static int oc_filter(
954 if( cur > *max ) *max = cur;
956 switch( f->f_choice ) {
957 case LDAP_FILTER_PRESENT:
958 if (f->f_desc == slap_schema.si_ad_objectClass) {
963 case LDAP_FILTER_AND:
966 for ( f=f->f_and; f; f=f->f_next ) {
967 (void) oc_filter(f, cur, max);
977 static void search_stack_free( void *key, void *data )
979 ber_memfree_x(data, NULL);
982 static void *search_stack( Operation *op )
984 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
987 if ( op->o_threadctx ) {
988 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
991 ret = bdb->bi_search_stack;
995 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
997 if ( op->o_threadctx ) {
998 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
999 ret, search_stack_free );
1001 bdb->bi_search_stack = ret;
1007 static int search_candidates(
1015 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1017 Filter f, rf, xf, nf;
1019 AttributeAssertion aa_ref = { NULL, BER_BVNULL, NULL };
1020 #ifdef BDB_SUBENTRIES
1022 AttributeAssertion aa_subentry = { NULL, BER_BVNULL, NULL };
1026 * This routine takes as input a filter (user-filter)
1027 * and rewrites it as follows:
1028 * (&(scope=DN)[(objectClass=subentry)]
1029 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1032 Debug(LDAP_DEBUG_TRACE,
1033 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1034 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1036 xf.f_or = op->oq_search.rs_filter;
1037 xf.f_choice = LDAP_FILTER_OR;
1040 /* If the user's filter uses objectClass=*,
1041 * these clauses are redundant.
1043 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1044 && !get_subentries_visibility(op)) {
1045 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1046 /* match referral objects */
1047 struct berval bv_ref = BER_BVC( "referral" );
1048 rf.f_choice = LDAP_FILTER_EQUALITY;
1050 rf.f_av_desc = slap_schema.si_ad_objectClass;
1051 rf.f_av_value = bv_ref;
1052 rf.f_next = xf.f_or;
1059 f.f_choice = LDAP_FILTER_AND;
1061 /* Dummy; we compute scope separately now */
1062 nf.f_choice = SLAPD_FILTER_COMPUTED;
1063 nf.f_result = LDAP_SUCCESS;
1064 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1065 ? op->oq_search.rs_filter : &xf ;
1066 /* Filter depth increased again, adding dummy clause */
1069 #ifdef BDB_SUBENTRIES
1070 if( get_subentries_visibility( op ) ) {
1071 struct berval bv_subentry = BER_BVC( "subentry" );
1072 sf.f_choice = LDAP_FILTER_EQUALITY;
1073 sf.f_ava = &aa_subentry;
1074 sf.f_av_desc = slap_schema.si_ad_objectClass;
1075 sf.f_av_value = bv_subentry;
1076 sf.f_next = nf.f_next;
1081 /* Allocate IDL stack, plus 1 more for former tmp */
1082 if ( depth+1 > bdb->bi_search_stack_depth ) {
1083 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1085 stack = search_stack( op );
1088 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1089 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1091 rc = bdb_dn2idl( op, e, ids, stack );
1094 if ( rc == LDAP_SUCCESS ) {
1095 rc = bdb_filter_candidates( op, &f, ids,
1096 stack, stack+BDB_IDL_UM_SIZE );
1099 if ( depth+1 > bdb->bi_search_stack_depth ) {
1104 Debug(LDAP_DEBUG_TRACE,
1105 "bdb_search_candidates: failed (rc=%d)\n",
1109 Debug(LDAP_DEBUG_TRACE,
1110 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1112 (long) BDB_IDL_FIRST(ids),
1113 (long) BDB_IDL_LAST(ids) );
1120 parse_paged_cookie( Operation *op, SlapReply *rs )
1123 int rc = LDAP_SUCCESS;
1127 struct berval cookie = BER_BVNULL;
1128 PagedResultsState *ps = op->o_pagedresults_state;
1130 /* this function must be invoked only if the pagedResults
1131 * control has been detected, parsed and partially checked
1132 * by the frontend */
1133 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1135 /* look for the appropriate ctrl structure */
1136 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
1137 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
1143 if ( c[0] == NULL ) {
1144 rs->sr_text = "missing pagedResults control";
1145 return LDAP_PROTOCOL_ERROR;
1148 /* Tested by frontend */
1149 assert( c[0]->ldctl_value.bv_len > 0 );
1151 /* Parse the control value
1152 * realSearchControlValue ::= SEQUENCE {
1153 * size INTEGER (0..maxInt),
1154 * -- requested page size from client
1155 * -- result set size estimate from server
1156 * cookie OCTET STRING
1159 ber = ber_init( &c[0]->ldctl_value );
1160 if ( ber == NULL ) {
1161 rs->sr_text = "internal error";
1165 tag = ber_scanf( ber, "{im}", &size, &cookie );
1167 /* Tested by frontend */
1168 assert( tag != LBER_ERROR );
1169 assert( size >= 0 );
1171 /* cookie decoding/checks deferred to backend... */
1172 if ( cookie.bv_len ) {
1173 PagedResultsCookie reqcookie;
1174 if( cookie.bv_len != sizeof( reqcookie ) ) {
1176 rs->sr_text = "paged results cookie is invalid";
1177 rc = LDAP_PROTOCOL_ERROR;
1181 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
1183 if ( reqcookie > ps->ps_cookie ) {
1185 rs->sr_text = "paged results cookie is invalid";
1186 rc = LDAP_PROTOCOL_ERROR;
1189 } else if ( reqcookie < ps->ps_cookie ) {
1190 rs->sr_text = "paged results cookie is invalid or old";
1191 rc = LDAP_UNWILLING_TO_PERFORM;
1196 /* Initial request. Initialize state. */
1198 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
1199 /* There's another pagedResults control on the
1200 * same connection; reject new pagedResults controls
1201 * (allowed by RFC2696) */
1202 rs->sr_text = "paged results cookie unavailable; try later";
1203 rc = LDAP_UNWILLING_TO_PERFORM;
1212 (void)ber_free( ber, 1 );
1218 send_paged_response(
1224 LDAPControl ctrl, *ctrls[2];
1225 BerElementBuffer berbuf;
1226 BerElement *ber = (BerElement *)&berbuf;
1227 PagedResultsCookie respcookie;
1228 struct berval cookie;
1230 Debug(LDAP_DEBUG_ARGS,
1231 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1232 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1234 BER_BVZERO( &ctrl.ldctl_value );
1238 ber_init2( ber, NULL, LBER_USE_DER );
1241 respcookie = ( PagedResultsCookie )(*lastid);
1242 cookie.bv_len = sizeof( respcookie );
1243 cookie.bv_val = (char *)&respcookie;
1246 respcookie = ( PagedResultsCookie )0;
1247 BER_BVSTR( &cookie, "" );
1250 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1251 op->o_conn->c_pagedresults_state.ps_count =
1252 ((PagedResultsState *)op->o_pagedresults_state)->ps_count + rs->sr_nentries;
1254 /* return size of 0 -- no estimate */
1255 ber_printf( ber, "{iO}", 0, &cookie );
1257 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1261 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1262 ctrls[0]->ldctl_iscritical = 0;
1264 rs->sr_ctrls = ctrls;
1265 rs->sr_err = LDAP_SUCCESS;
1266 send_ldap_result( op, rs );
1267 rs->sr_ctrls = NULL;
1270 (void) ber_free_buf( ber );