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, *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 #ifdef LDAP_COMP_MATCH
158 AttributeAssertion aa_alias = { NULL, BER_BVNULL, NULL };
160 AttributeAssertion aa_alias = { NULL, BER_BVNULL };
163 DB_LOCK locka, lockr;
166 aliases = stack; /* IDL of all aliases in the database */
167 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
168 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
169 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
170 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
171 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
172 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
174 /* A copy of subscop, because subscop gets clobbered by
175 * the bdb_idl_union/intersection routines
177 subscop2 = tmp + BDB_IDL_DB_SIZE;
179 af.f_choice = LDAP_FILTER_EQUALITY;
180 af.f_ava = &aa_alias;
181 af.f_av_desc = slap_schema.si_ad_objectClass;
182 af.f_av_value = bv_alias;
185 /* Find all aliases in database */
186 BDB_IDL_ZERO( aliases );
187 rs->sr_err = bdb_filter_candidates( op, locker, &af, aliases,
189 if (rs->sr_err != LDAP_SUCCESS) {
193 oldsubs[1] = e->e_id;
196 BDB_IDL_ZERO( visited );
197 BDB_IDL_ZERO( newsubs );
200 ido = bdb_idl_first( oldsubs, &cursoro );
203 /* Set curscop to only the aliases in the current scope. Start with
204 * all the aliases, obtain the IDL for the current scope, and then
205 * get the intersection of these two IDLs. Add the current scope
206 * to the cumulative list of candidates.
208 BDB_IDL_CPY( curscop, aliases );
209 rs->sr_err = bdb_dn2idl( op, locker, &e->e_nname, BEI(e), subscop,
210 subscop2+BDB_IDL_DB_SIZE );
214 bdb_cache_return_entry_r (bdb, e, &locka);
216 BDB_IDL_CPY(subscop2, subscop);
217 rs->sr_err = bdb_idl_intersection(curscop, subscop);
218 bdb_idl_union( ids, subscop2 );
220 /* Dereference all of the aliases in the current scope. */
222 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
223 ida = bdb_idl_next(curscop, &cursora))
227 rs->sr_err = bdb_cache_find_id(op, NULL,
228 ida, &ei, 0, locker, &lockr );
229 if (rs->sr_err != LDAP_SUCCESS) {
230 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
231 rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
236 /* This should only happen if the curscop IDL has maxed out and
237 * turned into a range that spans IDs indiscriminately
239 if (!is_entry_alias(a)) {
240 bdb_cache_return_entry_r (bdb, a, &lockr);
244 /* Actually dereference the alias */
246 a = deref_base( op, rs, a, &matched, locker, &lockr,
249 /* If the target was not already in our current candidates,
250 * make note of it in the newsubs list. Also
251 * set it in the scopes list so that bdb_search
254 if (bdb_idl_insert(ids, a->e_id) == 0) {
255 bdb_idl_insert(newsubs, a->e_id);
256 bdb_idl_insert(scopes, a->e_id);
258 bdb_cache_return_entry_r( bdb, a, &lockr);
260 } else if (matched) {
261 /* Alias could not be dereferenced, or it deref'd to
262 * an ID we've already seen. Ignore it.
264 bdb_cache_return_entry_r( bdb, matched, &lockr );
268 /* If this is a OneLevel search, we're done; oldsubs only had one
269 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
271 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
273 ido = bdb_idl_next( oldsubs, &cursoro );
275 /* If we're done processing the old scopes, did we add any new
276 * scopes in this iteration? If so, go back and do those now.
279 if (BDB_IDL_IS_ZERO(newsubs)) break;
280 BDB_IDL_CPY(oldsubs, newsubs);
281 BDB_IDL_ZERO(newsubs);
283 ido = bdb_idl_first( oldsubs, &cursoro );
286 /* Find the entry corresponding to the next scope. If it can't
287 * be found, ignore it and move on. This should never happen;
288 * we should never see the ID of an entry that doesn't exist.
289 * Set the name so that the scope's IDL can be retrieved.
293 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
295 if ( rs->sr_err != LDAP_SUCCESS ) {
296 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
297 rs->sr_err == DB_LOCK_NOTGRANTED )
307 bdb_search( Operation *op, SlapReply *rs )
309 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
312 ID candidates[BDB_IDL_UM_SIZE];
313 ID scopes[BDB_IDL_DB_SIZE];
314 Entry *e = NULL, base, *e_root;
315 Entry *matched = NULL;
317 struct berval realbase = BER_BVNULL;
322 AttributeName *attrs;
324 u_int32_t locker = 0;
326 struct bdb_op_info *opinfo = NULL;
329 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
330 attrs = op->oq_search.rs_attrs;
332 opinfo = (struct bdb_op_info *) op->o_private;
334 manageDSAit = get_manageDSAit( op );
336 if ( opinfo && opinfo->boi_txn ) {
337 ltid = opinfo->boi_txn;
338 locker = TXN_ID( ltid );
340 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
346 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
351 e_root = bdb->bi_cache.c_dntree.bei_e;
352 if ( op->o_req_ndn.bv_len == 0 ) {
353 /* DIT root special case */
354 ei = e_root->e_private;
355 rs->sr_err = LDAP_SUCCESS;
357 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
358 BDB_IDL_ZERO(candidates);
361 /* get entry with reader lock */
362 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei,
374 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
376 LOCK_ID_FREE (bdb->bi_dbenv, locker );
378 case DB_LOCK_DEADLOCK:
379 case DB_LOCK_NOTGRANTED:
382 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
384 LOCK_ID_FREE (bdb->bi_dbenv, locker );
388 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
389 if ( matched && is_entry_alias( matched )) {
392 stub.bv_val = op->o_req_ndn.bv_val;
393 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
394 e = deref_base( op, rs, matched, &matched, locker, &lock,
397 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
399 bdb_cache_return_entry_r (bdb, e, &lock);
403 } else if ( e && is_entry_alias( e )) {
404 e = deref_base( op, rs, e, &matched, locker, &lock,
410 struct berval matched_dn = BER_BVNULL;
412 if ( matched != NULL ) {
413 BerVarray erefs = NULL;
415 /* return referral only if "disclose"
416 * is granted on the object */
417 if ( ! access_allowed( op, matched,
418 slap_schema.si_ad_entry,
419 NULL, ACL_DISCLOSE, NULL ) )
421 rs->sr_err = LDAP_NO_SUCH_OBJECT;
424 ber_dupbv( &matched_dn, &matched->e_name );
426 erefs = is_entry_referral( matched )
427 ? get_entry_referrals( op, matched )
429 if ( rs->sr_err == DB_NOTFOUND )
430 rs->sr_err = LDAP_REFERRAL;
431 rs->sr_matched = matched_dn.bv_val;
434 #ifdef SLAP_ZONE_ALLOC
435 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
437 bdb_cache_return_entry_r (bdb, matched, &lock);
441 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
442 &op->o_req_dn, op->oq_search.rs_scope );
443 ber_bvarray_free( erefs );
447 #ifdef SLAP_ZONE_ALLOC
448 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
450 rs->sr_ref = referral_rewrite( default_referral,
451 NULL, &op->o_req_dn, op->oq_search.rs_scope );
452 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
455 send_ldap_result( op, rs );
458 LOCK_ID_FREE (bdb->bi_dbenv, locker );
460 ber_bvarray_free( rs->sr_ref );
463 if ( !BER_BVISNULL( &matched_dn ) ) {
464 ber_memfree( matched_dn.bv_val );
465 rs->sr_matched = NULL;
470 /* NOTE: __NEW__ "search" access is required
471 * on searchBase object */
472 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
473 NULL, ACL_SEARCH, NULL, &mask ) )
475 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
476 rs->sr_err = LDAP_NO_SUCH_OBJECT;
478 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
481 #ifdef SLAP_ZONE_ALLOC
482 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
485 bdb_cache_return_entry_r(bdb, e, &lock);
487 send_ldap_result( op, rs );
491 if ( !manageDSAit && e != e_root && is_entry_referral( e ) ) {
492 /* entry is a referral, don't allow add */
493 struct berval matched_dn = BER_BVNULL;
494 BerVarray erefs = NULL;
496 ber_dupbv( &matched_dn, &e->e_name );
497 erefs = get_entry_referrals( op, e );
499 rs->sr_err = LDAP_REFERRAL;
501 #ifdef SLAP_ZONE_ALLOC
502 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
504 bdb_cache_return_entry_r( bdb, e, &lock );
508 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
509 &op->o_req_dn, op->oq_search.rs_scope );
510 ber_bvarray_free( erefs );
513 rs->sr_text = "bad_referral object";
517 Debug( LDAP_DEBUG_TRACE,
518 LDAP_XSTRING(bdb_search) ": entry is referral\n",
521 rs->sr_matched = matched_dn.bv_val;
522 send_ldap_result( op, rs );
525 LOCK_ID_FREE (bdb->bi_dbenv, locker );
527 ber_bvarray_free( rs->sr_ref );
529 ber_memfree( matched_dn.bv_val );
530 rs->sr_matched = NULL;
534 if ( get_assert( op ) &&
535 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
537 rs->sr_err = LDAP_ASSERTION_FAILED;
538 #ifdef SLAP_ZONE_ALLOC
539 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
542 bdb_cache_return_entry_r(bdb, e, &lock);
544 send_ldap_result( op, rs );
548 /* compute it anyway; root does not use it */
549 stoptime = op->o_time + op->ors_tlimit;
551 /* need normalized dn below */
552 ber_dupbv( &realbase, &e->e_nname );
554 /* Copy info to base, must free entry before accessing the database
555 * in search_candidates, to avoid deadlocks.
557 base.e_private = e->e_private;
558 base.e_nname = realbase;
561 #ifdef SLAP_ZONE_ALLOC
562 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
565 bdb_cache_return_entry_r(bdb, e, &lock);
569 /* select candidates */
570 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
571 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
574 BDB_IDL_ZERO( candidates );
575 BDB_IDL_ZERO( scopes );
576 rs->sr_err = search_candidates( op, rs, &base,
577 locker, candidates, scopes );
580 /* start cursor at beginning of candidates.
584 if ( candidates[0] == 0 ) {
585 Debug( LDAP_DEBUG_TRACE,
586 LDAP_XSTRING(bdb_search) ": no candidates\n",
592 /* if not root and candidates exceed to-be-checked entries, abort */
593 if ( op->ors_limit /* isroot == FALSE */ &&
594 op->ors_limit->lms_s_unchecked != -1 &&
595 BDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
597 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
598 send_ldap_result( op, rs );
599 rs->sr_err = LDAP_SUCCESS;
603 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
604 !op->ors_limit->lms_s_pr_hide )
606 tentries = BDB_IDL_N(candidates);
609 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
610 PagedResultsState *ps = op->o_pagedresults_state;
611 /* deferred cookie parsing */
612 rs->sr_err = parse_paged_cookie( op, rs );
613 if ( rs->sr_err != LDAP_SUCCESS ) {
614 send_ldap_result( op, rs );
618 if ( (ID)( ps->ps_cookie ) == 0 ) {
619 id = bdb_idl_first( candidates, &cursor );
622 if ( ps->ps_size == 0 ) {
623 rs->sr_err = LDAP_SUCCESS;
624 rs->sr_text = "search abandoned by pagedResult size=0";
625 send_ldap_result( op, rs );
628 for ( id = bdb_idl_first( candidates, &cursor );
630 id <= (ID)( ps->ps_cookie );
631 id = bdb_idl_next( candidates, &cursor ) )
637 if ( cursor == NOID ) {
638 Debug( LDAP_DEBUG_TRACE,
639 LDAP_XSTRING(bdb_search)
640 ": no paged results candidates\n",
642 send_paged_response( op, rs, &lastid, 0 );
644 rs->sr_err = LDAP_OTHER;
650 for ( id = bdb_idl_first( candidates, &cursor );
651 id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
657 /* check for abandon */
658 if ( op->o_abandon ) {
659 rs->sr_err = SLAPD_ABANDON;
663 /* check time limit */
664 if ( op->ors_tlimit != SLAP_NO_LIMIT
665 && slap_get_time() > stoptime )
667 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
668 rs->sr_ref = rs->sr_v2ref;
669 send_ldap_result( op, rs );
670 rs->sr_err = LDAP_SUCCESS;
675 /* get the entry with reader lock */
677 rs->sr_err = bdb_cache_find_id( op, ltid,
678 id, &ei, 0, locker, &lock );
680 if (rs->sr_err == LDAP_BUSY) {
681 rs->sr_text = "ldap server busy";
682 send_ldap_result( op, rs );
685 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
686 || rs->sr_err == DB_LOCK_NOTGRANTED )
688 goto fetch_entry_retry;
691 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
698 if( !BDB_IDL_IS_RANGE(candidates) ) {
699 /* only complain for non-range IDLs */
700 Debug( LDAP_DEBUG_TRACE,
701 LDAP_XSTRING(bdb_search)
702 ": candidate %ld not found\n",
711 if ( is_entry_subentry( e ) ) {
712 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
713 if(!get_subentries_visibility( op )) {
714 /* only subentries are visible */
718 } else if ( get_subentries( op ) &&
719 !get_subentries_visibility( op ))
721 /* only subentries are visible */
725 } else if ( get_subentries_visibility( op )) {
726 /* only subentries are visible */
730 /* Does this candidate actually satisfy the search scope?
732 * Note that we don't lock access to the bei_parent pointer.
733 * Since only leaf nodes can be deleted, the parent of any
734 * node will always be a valid node. Also since we have
735 * a Read lock on the data, it cannot be renamed out of the
736 * scope while we are looking at it, and unless we're using
737 * BDB_HIER, its parents cannot be moved either.
740 switch( op->ors_scope ) {
741 case LDAP_SCOPE_BASE:
742 /* This is always true, yes? */
743 if ( id == base.e_id ) scopeok = 1;
746 case LDAP_SCOPE_ONELEVEL:
747 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
750 #ifdef LDAP_SCOPE_CHILDREN
751 case LDAP_SCOPE_CHILDREN:
752 if ( id == base.e_id ) break;
755 case LDAP_SCOPE_SUBTREE: {
757 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
758 if ( tmp->bei_id == base.e_id ) {
766 /* aliases were already dereferenced in candidate list */
767 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
768 /* but if the search base is an alias, and we didn't
769 * deref it when finding, return it.
771 if ( is_entry_alias(e) &&
772 ((op->ors_deref & LDAP_DEREF_FINDING) ||
773 !bvmatch(&e->e_nname, &op->o_req_ndn)))
778 /* scopes is only non-empty for onelevel or subtree */
779 if ( !scopeok && BDB_IDL_N(scopes) ) {
781 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
782 x = bdb_idl_search( scopes, e->e_id );
783 if ( scopes[x] == e->e_id ) scopeok = 1;
785 /* subtree, walk up the tree */
786 EntryInfo *tmp = BEI(e);
787 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
788 x = bdb_idl_search( scopes, tmp->bei_id );
789 if ( scopes[x] == tmp->bei_id ) {
798 /* Not in scope, ignore it */
801 Debug( LDAP_DEBUG_TRACE,
802 LDAP_XSTRING(bdb_search)
803 ": %ld scope not okay\n",
809 * if it's a referral, add it to the list of referrals. only do
810 * this for non-base searches, and don't check the filter
811 * explicitly here since it's only a candidate anyway.
813 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
814 && is_entry_referral( e ) )
816 BerVarray erefs = get_entry_referrals( op, e );
817 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
818 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
819 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
821 send_search_reference( op, rs );
823 ber_bvarray_free( rs->sr_ref );
824 ber_bvarray_free( erefs );
830 if ( !manageDSAit && is_entry_glue( e )) {
834 /* if it matches the filter and scope, send it */
835 rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
837 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
838 /* check size limit */
839 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
840 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
841 #ifdef SLAP_ZONE_ALLOC
842 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
844 bdb_cache_return_entry_r( bdb, e, &lock );
846 send_paged_response( op, rs, &lastid, tentries );
854 rs->sr_attrs = op->oq_search.rs_attrs;
855 rs->sr_operational_attrs = NULL;
858 rs->sr_err = LDAP_SUCCESS;
859 rs->sr_err = send_search_entry( op, rs );
861 switch ( rs->sr_err ) {
862 case LDAP_SUCCESS: /* entry sent ok */
864 default: /* entry not sent */
866 case LDAP_UNAVAILABLE:
867 case LDAP_SIZELIMIT_EXCEEDED:
868 #ifdef SLAP_ZONE_ALLOC
869 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
871 bdb_cache_return_entry_r(bdb, e, &lock);
874 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
875 rs->sr_ref = rs->sr_v2ref;
876 send_ldap_result( op, rs );
877 rs->sr_err = LDAP_SUCCESS;
880 rs->sr_err = LDAP_OTHER;
887 Debug( LDAP_DEBUG_TRACE,
888 LDAP_XSTRING(bdb_search)
889 ": %ld does not match filter\n",
895 /* free reader lock */
896 #ifdef SLAP_ZONE_ALLOC
897 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
899 bdb_cache_return_entry_r( bdb, e , &lock );
907 rs->sr_ref = rs->sr_v2ref;
908 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
909 rs->sr_rspoid = NULL;
910 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
911 send_paged_response( op, rs, NULL, 0 );
913 send_ldap_result( op, rs );
916 rs->sr_err = LDAP_SUCCESS;
920 LOCK_ID_FREE( bdb->bi_dbenv, locker );
923 ber_bvarray_free( rs->sr_v2ref );
926 if( realbase.bv_val ) ch_free( realbase.bv_val );
932 static int base_candidate(
937 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
938 e->e_nname.bv_val, (long) e->e_id, 0);
945 /* Look for "objectClass Present" in this filter.
946 * Also count depth of filter tree while we're at it.
948 static int oc_filter(
957 if( cur > *max ) *max = cur;
959 switch( f->f_choice ) {
960 case LDAP_FILTER_PRESENT:
961 if (f->f_desc == slap_schema.si_ad_objectClass) {
966 case LDAP_FILTER_AND:
969 for ( f=f->f_and; f; f=f->f_next ) {
970 (void) oc_filter(f, cur, max);
980 static void search_stack_free( void *key, void *data )
982 ber_memfree_x(data, NULL);
985 static void *search_stack( Operation *op )
987 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
990 if ( op->o_threadctx ) {
991 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
994 ret = bdb->bi_search_stack;
998 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1000 if ( op->o_threadctx ) {
1001 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1002 ret, search_stack_free );
1004 bdb->bi_search_stack = ret;
1010 static int search_candidates(
1018 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1020 Filter f, rf, xf, nf;
1022 #ifdef LDAP_COMP_MATCH
1023 AttributeAssertion aa_ref = { NULL, BER_BVNULL, NULL };
1025 AttributeAssertion aa_ref = { NULL, BER_BVNULL };
1028 #ifdef LDAP_COMP_MATCH
1029 AttributeAssertion aa_subentry = { NULL, BER_BVNULL, NULL };
1031 AttributeAssertion aa_subentry = { NULL, BER_BVNULL };
1035 * This routine takes as input a filter (user-filter)
1036 * and rewrites it as follows:
1037 * (&(scope=DN)[(objectClass=subentry)]
1038 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1041 Debug(LDAP_DEBUG_TRACE,
1042 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1043 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1045 xf.f_or = op->oq_search.rs_filter;
1046 xf.f_choice = LDAP_FILTER_OR;
1049 /* If the user's filter uses objectClass=*,
1050 * these clauses are redundant.
1052 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1053 && !get_subentries_visibility(op)) {
1054 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1055 /* match referral objects */
1056 struct berval bv_ref = BER_BVC( "referral" );
1057 rf.f_choice = LDAP_FILTER_EQUALITY;
1059 rf.f_av_desc = slap_schema.si_ad_objectClass;
1060 rf.f_av_value = bv_ref;
1061 rf.f_next = xf.f_or;
1068 f.f_choice = LDAP_FILTER_AND;
1070 /* Dummy; we compute scope separately now */
1071 nf.f_choice = SLAPD_FILTER_COMPUTED;
1072 nf.f_result = LDAP_SUCCESS;
1073 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1074 ? op->oq_search.rs_filter : &xf ;
1075 /* Filter depth increased again, adding dummy clause */
1078 if( get_subentries_visibility( op ) ) {
1079 struct berval bv_subentry = BER_BVC( "subentry" );
1080 sf.f_choice = LDAP_FILTER_EQUALITY;
1081 sf.f_ava = &aa_subentry;
1082 sf.f_av_desc = slap_schema.si_ad_objectClass;
1083 sf.f_av_value = bv_subentry;
1084 sf.f_next = nf.f_next;
1088 /* Allocate IDL stack, plus 1 more for former tmp */
1089 if ( depth+1 > bdb->bi_search_stack_depth ) {
1090 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1092 stack = search_stack( op );
1095 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1096 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1098 rc = bdb_dn2idl( op, locker, &e->e_nname, BEI(e), ids, stack );
1101 if ( rc == LDAP_SUCCESS ) {
1102 rc = bdb_filter_candidates( op, locker, &f, ids,
1103 stack, stack+BDB_IDL_UM_SIZE );
1106 if ( depth+1 > bdb->bi_search_stack_depth ) {
1111 Debug(LDAP_DEBUG_TRACE,
1112 "bdb_search_candidates: failed (rc=%d)\n",
1116 Debug(LDAP_DEBUG_TRACE,
1117 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1119 (long) BDB_IDL_FIRST(ids),
1120 (long) BDB_IDL_LAST(ids) );
1127 parse_paged_cookie( Operation *op, SlapReply *rs )
1130 int rc = LDAP_SUCCESS;
1134 struct berval cookie = BER_BVNULL;
1135 PagedResultsState *ps = op->o_pagedresults_state;
1137 /* this function must be invoked only if the pagedResults
1138 * control has been detected, parsed and partially checked
1139 * by the frontend */
1140 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1142 /* look for the appropriate ctrl structure */
1143 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
1144 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
1150 if ( c[0] == NULL ) {
1151 rs->sr_text = "missing pagedResults control";
1152 return LDAP_PROTOCOL_ERROR;
1155 /* Tested by frontend */
1156 assert( c[0]->ldctl_value.bv_len > 0 );
1158 /* Parse the control value
1159 * realSearchControlValue ::= SEQUENCE {
1160 * size INTEGER (0..maxInt),
1161 * -- requested page size from client
1162 * -- result set size estimate from server
1163 * cookie OCTET STRING
1166 ber = ber_init( &c[0]->ldctl_value );
1167 if ( ber == NULL ) {
1168 rs->sr_text = "internal error";
1172 tag = ber_scanf( ber, "{im}", &size, &cookie );
1174 /* Tested by frontend */
1175 assert( tag != LBER_ERROR );
1176 assert( size >= 0 );
1178 /* cookie decoding/checks deferred to backend... */
1179 if ( cookie.bv_len ) {
1180 PagedResultsCookie reqcookie;
1181 if( cookie.bv_len != sizeof( reqcookie ) ) {
1183 rs->sr_text = "paged results cookie is invalid";
1184 rc = LDAP_PROTOCOL_ERROR;
1188 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
1190 if ( reqcookie > ps->ps_cookie ) {
1192 rs->sr_text = "paged results cookie is invalid";
1193 rc = LDAP_PROTOCOL_ERROR;
1196 } else if ( reqcookie < ps->ps_cookie ) {
1197 rs->sr_text = "paged results cookie is invalid or old";
1198 rc = LDAP_UNWILLING_TO_PERFORM;
1203 /* Initial request. Initialize state. */
1205 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
1206 /* There's another pagedResults control on the
1207 * same connection; reject new pagedResults controls
1208 * (allowed by RFC2696) */
1209 rs->sr_text = "paged results cookie unavailable; try later";
1210 rc = LDAP_UNWILLING_TO_PERFORM;
1219 (void)ber_free( ber, 1 );
1225 send_paged_response(
1231 LDAPControl ctrl, *ctrls[2];
1232 BerElementBuffer berbuf;
1233 BerElement *ber = (BerElement *)&berbuf;
1234 PagedResultsCookie respcookie;
1235 struct berval cookie;
1237 Debug(LDAP_DEBUG_ARGS,
1238 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1239 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1241 BER_BVZERO( &ctrl.ldctl_value );
1245 ber_init2( ber, NULL, LBER_USE_DER );
1248 respcookie = ( PagedResultsCookie )(*lastid);
1249 cookie.bv_len = sizeof( respcookie );
1250 cookie.bv_val = (char *)&respcookie;
1253 respcookie = ( PagedResultsCookie )0;
1254 BER_BVSTR( &cookie, "" );
1257 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1258 op->o_conn->c_pagedresults_state.ps_count =
1259 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1262 /* return size of 0 -- no estimate */
1263 ber_printf( ber, "{iO}", 0, &cookie );
1265 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1269 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1270 ctrls[0]->ldctl_iscritical = 0;
1272 rs->sr_ctrls = ctrls;
1273 rs->sr_err = LDAP_SUCCESS;
1274 send_ldap_result( op, rs );
1275 rs->sr_ctrls = NULL;
1278 (void) ber_free_buf( ber );