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;
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;
319 AttributeName *attrs;
321 u_int32_t locker = 0;
323 struct bdb_op_info *opinfo = NULL;
326 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
327 attrs = op->oq_search.rs_attrs;
329 opinfo = (struct bdb_op_info *) op->o_private;
331 manageDSAit = get_manageDSAit( op );
333 if ( opinfo && opinfo->boi_txn ) {
334 ltid = opinfo->boi_txn;
335 locker = TXN_ID( ltid );
337 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
343 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
348 if ( op->o_req_ndn.bv_len == 0 ) {
349 /* DIT root special case */
350 ei_root.bei_e = &e_root;
351 ei_root.bei_parent = &ei_root;
352 e_root.e_private = &ei_root;
354 BER_BVSTR( &e_root.e_nname, "" );
355 BER_BVSTR( &e_root.e_name, "" );
357 rs->sr_err = LDAP_SUCCESS;
360 /* get entry with reader lock */
361 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei,
373 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
375 LOCK_ID_FREE (bdb->bi_dbenv, locker );
377 case DB_LOCK_DEADLOCK:
378 case DB_LOCK_NOTGRANTED:
381 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
383 LOCK_ID_FREE (bdb->bi_dbenv, locker );
387 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
388 BDB_IDL_ZERO(candidates);
389 e = deref_base( op, rs, e, &matched, locker, &lock,
394 struct berval matched_dn = BER_BVNULL;
396 if ( matched != NULL ) {
397 BerVarray erefs = NULL;
399 if ( ! access_allowed( op, matched,
400 slap_schema.si_ad_entry,
401 NULL, ACL_DISCLOSE, NULL ) )
403 rs->sr_err = LDAP_NO_SUCH_OBJECT;
406 ber_dupbv( &matched_dn, &matched->e_name );
408 erefs = is_entry_referral( matched )
409 ? get_entry_referrals( op, matched )
411 rs->sr_err = LDAP_REFERRAL;
412 rs->sr_matched = matched_dn.bv_val;
415 #ifdef SLAP_ZONE_ALLOC
416 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
418 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
423 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
424 &op->o_req_dn, op->oq_search.rs_scope );
425 ber_bvarray_free( erefs );
429 #ifdef SLAP_ZONE_ALLOC
430 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
432 rs->sr_ref = referral_rewrite( default_referral,
433 NULL, &op->o_req_dn, op->oq_search.rs_scope );
434 rs->sr_err = LDAP_REFERRAL;
437 send_ldap_result( op, rs );
440 LOCK_ID_FREE (bdb->bi_dbenv, locker );
442 ber_bvarray_free( rs->sr_ref );
445 if ( !BER_BVISNULL( &matched_dn ) ) {
446 ber_memfree( matched_dn.bv_val );
447 rs->sr_matched = NULL;
452 if ( ! access_allowed( op, e, slap_schema.si_ad_entry,
453 NULL, ACL_DISCLOSE, NULL ) )
455 rs->sr_err = LDAP_NO_SUCH_OBJECT;
457 #ifdef SLAP_ZONE_ALLOC
458 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
460 if ( e != &e_root ) {
461 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
463 send_ldap_result( op, rs );
467 if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
468 /* entry is a referral, don't allow add */
469 struct berval matched_dn = BER_BVNULL;
470 BerVarray erefs = NULL;
472 ber_dupbv( &matched_dn, &e->e_name );
473 erefs = get_entry_referrals( op, e );
475 rs->sr_err = LDAP_REFERRAL;
477 #ifdef SLAP_ZONE_ALLOC
478 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
480 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
484 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
485 &op->o_req_dn, op->oq_search.rs_scope );
486 ber_bvarray_free( erefs );
489 rs->sr_text = "bad_referral object";
493 Debug( LDAP_DEBUG_TRACE,
494 LDAP_XSTRING(bdb_search) ": entry is referral\n",
497 rs->sr_matched = matched_dn.bv_val;
498 send_ldap_result( op, rs );
501 LOCK_ID_FREE (bdb->bi_dbenv, locker );
503 ber_bvarray_free( rs->sr_ref );
505 ber_memfree( matched_dn.bv_val );
506 rs->sr_matched = NULL;
510 if ( get_assert( op ) &&
511 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
513 rs->sr_err = LDAP_ASSERTION_FAILED;
514 #ifdef SLAP_ZONE_ALLOC
515 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
517 if ( e != &e_root ) {
518 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
520 send_ldap_result( op, rs );
524 /* compute it anyway; root does not use it */
525 stoptime = op->o_time + op->ors_tlimit;
527 /* need normalized dn below */
528 ber_dupbv( &realbase, &e->e_nname );
530 /* Copy info to base, must free entry before accessing the database
531 * in search_candidates, to avoid deadlocks.
533 base.e_private = e->e_private;
534 base.e_nname = realbase;
537 #ifdef SLAP_ZONE_ALLOC
538 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
540 if ( e != &e_root ) {
541 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
545 /* select candidates */
546 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
547 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
550 BDB_IDL_ZERO( candidates );
551 BDB_IDL_ZERO( scopes );
552 rs->sr_err = search_candidates( op, rs, &base,
553 locker, candidates, scopes );
556 /* start cursor at beginning of candidates.
560 if ( candidates[0] == 0 ) {
561 Debug( LDAP_DEBUG_TRACE,
562 LDAP_XSTRING(bdb_search) ": no candidates\n",
568 /* if not root and candidates exceed to-be-checked entries, abort */
569 if ( op->ors_limit /* isroot == FALSE */ &&
570 op->ors_limit->lms_s_unchecked != -1 &&
571 BDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
573 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
574 send_ldap_result( op, rs );
575 rs->sr_err = LDAP_SUCCESS;
579 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
580 !op->ors_limit->lms_s_pr_hide )
582 tentries = BDB_IDL_N(candidates);
585 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
586 PagedResultsState *ps = op->o_pagedresults_state;
587 /* deferred cookie parsing */
588 rs->sr_err = parse_paged_cookie( op, rs );
589 if ( rs->sr_err != LDAP_SUCCESS ) {
590 send_ldap_result( op, rs );
594 if ( (ID)( ps->ps_cookie ) == 0 ) {
595 id = bdb_idl_first( candidates, &cursor );
598 if ( ps->ps_size == 0 ) {
599 rs->sr_err = LDAP_SUCCESS;
600 rs->sr_text = "search abandoned by pagedResult size=0";
601 send_ldap_result( op, rs );
604 for ( id = bdb_idl_first( candidates, &cursor );
606 id <= (ID)( ps->ps_cookie );
607 id = bdb_idl_next( candidates, &cursor ) )
613 if ( cursor == NOID ) {
614 Debug( LDAP_DEBUG_TRACE,
615 LDAP_XSTRING(bdb_search)
616 ": no paged results candidates\n",
618 send_paged_response( op, rs, &lastid, 0 );
620 rs->sr_err = LDAP_OTHER;
628 for ( id = bdb_idl_first( candidates, &cursor );
629 id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
636 /* check for abandon */
637 if ( op->o_abandon ) {
638 rs->sr_err = LDAP_SUCCESS;
642 /* check time limit */
643 if ( op->ors_tlimit != SLAP_NO_LIMIT
644 && slap_get_time() > stoptime )
646 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
647 rs->sr_ref = rs->sr_v2ref;
648 send_ldap_result( op, rs );
649 rs->sr_err = LDAP_SUCCESS;
654 /* get the entry with reader lock */
656 rs->sr_err = bdb_cache_find_id( op, ltid,
657 id, &ei, 0, locker, &lock );
659 if (rs->sr_err == LDAP_BUSY) {
660 rs->sr_text = "ldap server busy";
661 send_ldap_result( op, rs );
664 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
665 || rs->sr_err == DB_LOCK_NOTGRANTED )
667 goto fetch_entry_retry;
670 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
677 if( !BDB_IDL_IS_RANGE(candidates) ) {
678 /* only complain for non-range IDLs */
679 Debug( LDAP_DEBUG_TRACE,
680 LDAP_XSTRING(bdb_search)
681 ": candidate %ld not found\n",
690 #ifdef BDB_SUBENTRIES
692 if ( is_entry_subentry( e ) ) {
693 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
694 if(!get_subentries_visibility( op )) {
695 /* only subentries are visible */
699 } else if ( get_subentries( op ) &&
700 !get_subentries_visibility( op ))
702 /* only subentries are visible */
706 } else if ( get_subentries_visibility( op )) {
707 /* only subentries are visible */
711 #endif /* BDB_SUBENTRIES */
713 /* Does this candidate actually satisfy the search scope?
715 * Note that we don't lock access to the bei_parent pointer.
716 * Since only leaf nodes can be deleted, the parent of any
717 * node will always be a valid node. Also since we have
718 * a Read lock on the data, it cannot be renamed out of the
719 * scope while we are looking at it, and unless we're using
720 * BDB_HIER, its parents cannot be moved either.
722 switch( op->ors_scope ) {
723 case LDAP_SCOPE_BASE:
724 /* This is always true, yes? */
725 if ( id == base.e_id ) scopeok = 1;
728 case LDAP_SCOPE_ONELEVEL:
729 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
732 #ifdef LDAP_SCOPE_CHILDREN
733 case LDAP_SCOPE_CHILDREN:
734 if ( id == base.e_id ) break;
737 case LDAP_SCOPE_SUBTREE: {
739 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
740 if ( tmp->bei_id == base.e_id ) {
748 /* aliases were already dereferenced in candidate list */
749 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
750 /* but if the search base is an alias, and we didn't
751 * deref it when finding, return it.
753 if ( is_entry_alias(e) &&
754 ((op->ors_deref & LDAP_DEREF_FINDING) ||
755 !bvmatch(&e->e_nname, &op->o_req_ndn)))
760 /* scopes is only non-empty for onelevel or subtree */
761 if ( !scopeok && BDB_IDL_N(scopes) ) {
763 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
764 x = bdb_idl_search( scopes, e->e_id );
765 if ( scopes[x] == e->e_id ) scopeok = 1;
767 /* subtree, walk up the tree */
768 EntryInfo *tmp = BEI(e);
769 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
770 x = bdb_idl_search( scopes, tmp->bei_id );
771 if ( scopes[x] == tmp->bei_id ) {
780 /* Not in scope, ignore it */
783 Debug( LDAP_DEBUG_TRACE,
784 LDAP_XSTRING(bdb_search)
785 ": %ld scope not okay\n",
791 * if it's a referral, add it to the list of referrals. only do
792 * this for non-base searches, and don't check the filter
793 * explicitly here since it's only a candidate anyway.
795 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
796 && is_entry_referral( e ) )
798 BerVarray erefs = get_entry_referrals( op, e );
799 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
800 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
801 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
803 send_search_reference( op, rs );
805 ber_bvarray_free( rs->sr_ref );
806 ber_bvarray_free( erefs );
812 if ( !manageDSAit && is_entry_glue( e )) {
816 /* if it matches the filter and scope, send it */
817 rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
819 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
820 /* check size limit */
821 if ( --op->ors_slimit == -1) {
822 #ifdef SLAP_ZONE_ALLOC
823 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
825 bdb_cache_return_entry_r( bdb->bi_dbenv,
826 &bdb->bi_cache, e, &lock );
829 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
830 rs->sr_ref = rs->sr_v2ref;
831 send_ldap_result( op, rs );
832 rs->sr_err = LDAP_SUCCESS;
836 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
837 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
838 send_paged_response( op, rs, &lastid, tentries );
847 rs->sr_attrs = op->oq_search.rs_attrs;
848 rs->sr_operational_attrs = NULL;
851 rs->sr_err = LDAP_SUCCESS;
852 result = send_search_entry( op, rs );
855 case 0: /* entry sent ok */
857 case 1: /* entry not sent */
859 case -1: /* connection closed */
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);
867 rs->sr_err = LDAP_OTHER;
873 Debug( LDAP_DEBUG_TRACE,
874 LDAP_XSTRING(bdb_search)
875 ": %ld does not match filter\n",
881 /* free reader lock */
882 #ifdef SLAP_ZONE_ALLOC
883 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
885 bdb_cache_return_entry_r( bdb->bi_dbenv,
886 &bdb->bi_cache, e , &lock );
891 ldap_pvt_thread_yield();
896 rs->sr_ref = rs->sr_v2ref;
897 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
898 rs->sr_rspoid = NULL;
899 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
900 send_paged_response( op, rs, NULL, 0 );
902 send_ldap_result( op, rs );
905 rs->sr_err = LDAP_SUCCESS;
909 LOCK_ID_FREE( bdb->bi_dbenv, locker );
912 ber_bvarray_free( rs->sr_v2ref );
915 if( realbase.bv_val ) ch_free( realbase.bv_val );
921 static int base_candidate(
926 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
927 e->e_nname.bv_val, (long) e->e_id, 0);
934 /* Look for "objectClass Present" in this filter.
935 * Also count depth of filter tree while we're at it.
937 static int oc_filter(
946 if( cur > *max ) *max = cur;
948 switch( f->f_choice ) {
949 case LDAP_FILTER_PRESENT:
950 if (f->f_desc == slap_schema.si_ad_objectClass) {
955 case LDAP_FILTER_AND:
958 for ( f=f->f_and; f; f=f->f_next ) {
959 (void) oc_filter(f, cur, max);
969 static void search_stack_free( void *key, void *data )
971 ber_memfree_x(data, NULL);
974 static void *search_stack( Operation *op )
976 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
979 if ( op->o_threadctx ) {
980 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
983 ret = bdb->bi_search_stack;
987 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
989 if ( op->o_threadctx ) {
990 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
991 ret, search_stack_free );
993 bdb->bi_search_stack = ret;
999 static int search_candidates(
1007 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1009 Filter f, rf, xf, nf;
1011 AttributeAssertion aa_ref;
1012 #ifdef BDB_SUBENTRIES
1014 AttributeAssertion aa_subentry;
1018 * This routine takes as input a filter (user-filter)
1019 * and rewrites it as follows:
1020 * (&(scope=DN)[(objectClass=subentry)]
1021 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1024 Debug(LDAP_DEBUG_TRACE,
1025 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1026 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1028 xf.f_or = op->oq_search.rs_filter;
1029 xf.f_choice = LDAP_FILTER_OR;
1032 /* If the user's filter uses objectClass=*,
1033 * these clauses are redundant.
1035 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1036 && !get_subentries_visibility(op)) {
1037 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1038 /* match referral objects */
1039 struct berval bv_ref = BER_BVC( "referral" );
1040 rf.f_choice = LDAP_FILTER_EQUALITY;
1042 rf.f_av_desc = slap_schema.si_ad_objectClass;
1043 rf.f_av_value = bv_ref;
1044 rf.f_next = xf.f_or;
1051 f.f_choice = LDAP_FILTER_AND;
1053 /* Dummy; we compute scope separately now */
1054 nf.f_choice = SLAPD_FILTER_COMPUTED;
1055 nf.f_result = LDAP_SUCCESS;
1056 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1057 ? op->oq_search.rs_filter : &xf ;
1058 /* Filter depth increased again, adding dummy clause */
1061 #ifdef BDB_SUBENTRIES
1062 if( get_subentries_visibility( op ) ) {
1063 struct berval bv_subentry = BER_BVC( "subentry" );
1064 sf.f_choice = LDAP_FILTER_EQUALITY;
1065 sf.f_ava = &aa_subentry;
1066 sf.f_av_desc = slap_schema.si_ad_objectClass;
1067 sf.f_av_value = bv_subentry;
1068 sf.f_next = nf.f_next;
1073 /* Allocate IDL stack, plus 1 more for former tmp */
1074 if ( depth+1 > bdb->bi_search_stack_depth ) {
1075 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1077 stack = search_stack( op );
1080 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1081 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1083 rc = bdb_dn2idl( op, e, ids, stack );
1086 if ( rc == LDAP_SUCCESS ) {
1087 rc = bdb_filter_candidates( op, &f, ids,
1088 stack, stack+BDB_IDL_UM_SIZE );
1091 if ( depth+1 > bdb->bi_search_stack_depth ) {
1096 Debug(LDAP_DEBUG_TRACE,
1097 "bdb_search_candidates: failed (rc=%d)\n",
1101 Debug(LDAP_DEBUG_TRACE,
1102 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1104 (long) BDB_IDL_FIRST(ids),
1105 (long) BDB_IDL_LAST(ids) );
1112 parse_paged_cookie( Operation *op, SlapReply *rs )
1115 int rc = LDAP_SUCCESS;
1119 struct berval cookie = BER_BVNULL;
1120 PagedResultsState *ps = op->o_pagedresults_state;
1122 /* this function must be invoked only if the pagedResults
1123 * control has been detected, parsed and partially checked
1124 * by the frontend */
1125 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1127 /* look for the appropriate ctrl structure */
1128 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
1129 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
1135 if ( c[0] == NULL ) {
1136 rs->sr_text = "missing pagedResults control";
1137 return LDAP_PROTOCOL_ERROR;
1140 /* Tested by frontend */
1141 assert( c[0]->ldctl_value.bv_len > 0 );
1143 /* Parse the control value
1144 * realSearchControlValue ::= SEQUENCE {
1145 * size INTEGER (0..maxInt),
1146 * -- requested page size from client
1147 * -- result set size estimate from server
1148 * cookie OCTET STRING
1151 ber = ber_init( &c[0]->ldctl_value );
1152 if ( ber == NULL ) {
1153 rs->sr_text = "internal error";
1157 tag = ber_scanf( ber, "{im}", &size, &cookie );
1159 /* Tested by frontend */
1160 assert( tag != LBER_ERROR );
1161 assert( size >= 0 );
1163 /* cookie decoding/checks deferred to backend... */
1164 if ( cookie.bv_len ) {
1165 PagedResultsCookie reqcookie;
1166 if( cookie.bv_len != sizeof( reqcookie ) ) {
1168 rs->sr_text = "paged results cookie is invalid";
1169 rc = LDAP_PROTOCOL_ERROR;
1173 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
1175 if ( reqcookie > ps->ps_cookie ) {
1177 rs->sr_text = "paged results cookie is invalid";
1178 rc = LDAP_PROTOCOL_ERROR;
1181 } else if ( reqcookie < ps->ps_cookie ) {
1182 rs->sr_text = "paged results cookie is invalid or old";
1183 rc = LDAP_UNWILLING_TO_PERFORM;
1188 /* Initial request. Initialize state. */
1190 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
1191 /* There's another pagedResults control on the
1192 * same connection; reject new pagedResults controls
1193 * (allowed by RFC2696) */
1194 rs->sr_text = "paged results cookie unavailable; try later";
1195 rc = LDAP_UNWILLING_TO_PERFORM;
1204 (void)ber_free( ber, 1 );
1210 send_paged_response(
1216 LDAPControl ctrl, *ctrls[2];
1217 BerElementBuffer berbuf;
1218 BerElement *ber = (BerElement *)&berbuf;
1219 PagedResultsCookie respcookie;
1220 struct berval cookie;
1222 Debug(LDAP_DEBUG_ARGS,
1223 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1224 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1226 BER_BVZERO( &ctrl.ldctl_value );
1230 ber_init2( ber, NULL, LBER_USE_DER );
1233 respcookie = ( PagedResultsCookie )(*lastid);
1234 cookie.bv_len = sizeof( respcookie );
1235 cookie.bv_val = (char *)&respcookie;
1238 respcookie = ( PagedResultsCookie )0;
1239 BER_BVSTR( &cookie, "" );
1242 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1243 op->o_conn->c_pagedresults_state.ps_count =
1244 ((PagedResultsState *)op->o_pagedresults_state)->ps_count + rs->sr_nentries;
1246 /* return size of 0 -- no estimate */
1247 ber_printf( ber, "{iO}", 0, &cookie );
1249 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1253 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1254 ctrls[0]->ldctl_iscritical = 0;
1256 rs->sr_ctrls = ctrls;
1257 rs->sr_err = LDAP_SUCCESS;
1258 send_ldap_result( op, rs );
1259 rs->sr_ctrls = NULL;
1262 (void) ber_free_buf( ber );
1266 bdb_pfid_cmp( const void *v_id1, const void *v_id2 )
1268 const ID *p1 = v_id1, *p2 = v_id2;
1273 bdb_id_dup( Operation *op, ID *id )
1276 new = ch_malloc( sizeof(ID) );