1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2013 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(
39 static int parse_paged_cookie( Operation *op, SlapReply *rs );
41 static void send_paged_response(
47 /* Dereference aliases for a single alias entry. Return the final
48 * dereferenced entry on success, NULL on any failure.
50 static Entry * deref_base (
61 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
62 rs->sr_text = "maximum deref depth exceeded";
65 /* Remember the last entry we looked at, so we can
70 if (MDB_IDL_N(tmp) >= op->o_bd->be_max_deref_depth) {
75 /* If this is part of a subtree or onelevel search,
76 * have we seen this ID before? If so, quit.
78 if ( visited && mdb_idl_insert( visited, e->e_id ) ) {
83 /* If we've seen this ID during this deref iteration,
86 if ( mdb_idl_insert( tmp, e->e_id ) ) {
87 rs->sr_err = LDAP_ALIAS_PROBLEM;
88 rs->sr_text = "circular alias";
93 /* If there was a problem getting the aliasedObjectName,
94 * get_alias_dn will have set the error status.
96 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
101 rs->sr_err = mdb_dn2entry( op, txn, NULL, &ndn, &e, NULL, 0 );
103 rs->sr_err = LDAP_ALIAS_PROBLEM;
104 rs->sr_text = "aliasedObject not found";
108 /* Free the previous entry, continue to work with the
109 * one we just retrieved.
111 mdb_entry_return( op, *matched );
113 /* We found a regular entry. Return this to the caller.
115 if (!is_entry_alias(e)) {
116 rs->sr_err = LDAP_SUCCESS;
124 /* Look for and dereference all aliases within the search scope.
125 * Requires "stack" to be able to hold 6 levels of DB_SIZE IDLs.
126 * Of course we're hardcoded to require a minimum of 8 UM_SIZE
127 * IDLs so this is never a problem.
129 static int search_aliases(
138 ID *aliases, *curscop, *visited, *newsubs, *oldsubs, *tmp;
139 ID cursora, ida, cursoro, ido;
141 struct berval bv_alias = BER_BVC( "alias" );
142 AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
146 aliases = stack; /* IDL of all aliases in the database */
147 curscop = aliases + MDB_IDL_DB_SIZE; /* Aliases in the current scope */
148 visited = curscop + MDB_IDL_DB_SIZE; /* IDs we've seen in this search */
149 newsubs = visited + MDB_IDL_DB_SIZE; /* New subtrees we've added */
150 oldsubs = newsubs + MDB_IDL_DB_SIZE; /* Subtrees added previously */
151 tmp = oldsubs + MDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
153 af.f_choice = LDAP_FILTER_EQUALITY;
154 af.f_ava = &aa_alias;
155 af.f_av_desc = slap_schema.si_ad_objectClass;
156 af.f_av_value = bv_alias;
159 /* Find all aliases in database */
160 MDB_IDL_ZERO( aliases );
161 rs->sr_err = mdb_filter_candidates( op, txn, &af, aliases,
163 if (rs->sr_err != LDAP_SUCCESS || MDB_IDL_IS_ZERO( aliases )) {
169 MDB_IDL_ZERO( visited );
170 MDB_IDL_ZERO( newsubs );
173 ido = mdb_idl_first( oldsubs, &cursoro );
176 /* Set curscop to only the aliases in the current scope. Start with
177 * all the aliases, then get the intersection with the scope.
179 rs->sr_err = mdb_idscope( op, txn, e_id, aliases, curscop );
181 /* Dereference all of the aliases in the current scope. */
183 for (ida = mdb_idl_first(curscop, &cursora); ida != NOID;
184 ida = mdb_idl_next(curscop, &cursora))
186 rs->sr_err = mdb_id2entry(op, mci, ida, &a);
187 if (rs->sr_err != LDAP_SUCCESS) {
191 /* This should only happen if the curscop IDL has maxed out and
192 * turned into a range that spans IDs indiscriminately
194 if (!is_entry_alias(a)) {
195 mdb_entry_return(op, a);
199 /* Actually dereference the alias */
201 a = deref_base( op, rs, a, &matched, txn,
204 /* If the target was not already in our current scopes,
205 * make note of it in the newsubs list.
209 mid.mval.mv_data = NULL;
210 if (mdb_id2l_insert(scopes, &mid) == 0) {
211 mdb_idl_insert(newsubs, a->e_id);
213 mdb_entry_return( op, a );
215 } else if (matched) {
216 /* Alias could not be dereferenced, or it deref'd to
217 * an ID we've already seen. Ignore it.
219 mdb_entry_return( op, matched );
224 /* If this is a OneLevel search, we're done; oldsubs only had one
225 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
227 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
229 ido = mdb_idl_next( oldsubs, &cursoro );
231 /* If we're done processing the old scopes, did we add any new
232 * scopes in this iteration? If so, go back and do those now.
235 if (MDB_IDL_IS_ZERO(newsubs)) break;
236 MDB_IDL_CPY(oldsubs, newsubs);
237 MDB_IDL_ZERO(newsubs);
239 ido = mdb_idl_first( oldsubs, &cursoro );
242 /* Find the entry corresponding to the next scope. If it can't
243 * be found, ignore it and move on. This should never happen;
244 * we should never see the ID of an entry that doesn't exist.
248 rs->sr_err = mdb_id2edata(op, mci, ido, &edata);
249 if ( rs->sr_err != MDB_SUCCESS ) {
258 /* Get the next ID from the DB. Used if the candidate list is
259 * a range and simple iteration hits missing entryIDs
262 mdb_get_nextid(MDB_cursor *mci, ID *cursor)
270 key.mv_size = sizeof(ID);
271 rc = mdb_cursor_get( mci, &key, NULL, MDB_SET_RANGE );
274 memcpy( cursor, key.mv_data, sizeof(ID));
278 static void scope_chunk_free( void *key, void *data )
281 for (p1 = data; p1; p1 = p2) {
282 p2 = p1[0].mval.mv_data;
283 ber_memfree_x(p1, NULL);
287 static ID2 *scope_chunk_get( Operation *op )
289 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
292 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
293 (void *)&ret, NULL );
295 ret = ch_malloc( MDB_IDL_UM_SIZE * sizeof( ID2 ));
297 void *r2 = ret[0].mval.mv_data;
298 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
299 r2, scope_chunk_free, NULL, NULL );
304 static void scope_chunk_ret( Operation *op, ID2 *scopes )
306 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
309 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
311 scopes[0].mval.mv_data = ret;
312 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
313 (void *)scopes, scope_chunk_free, NULL, NULL );
317 mdb_search( Operation *op, SlapReply *rs )
319 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
320 ID id, cursor, nsubs, ncand, cscope;
322 ID candidates[MDB_IDL_UM_SIZE];
323 ID iscopes[MDB_IDL_DB_SIZE];
325 Entry *e = NULL, *base = NULL;
326 Entry *matched = NULL;
327 AttributeName *attrs;
333 MDB_cursor *mci, *mcd;
335 mdb_op_info opinfo = {{{0}}}, *moi = &opinfo;
336 MDB_txn *ltid = NULL;
338 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_search) "\n", 0, 0, 0);
339 attrs = op->oq_search.rs_attrs;
341 manageDSAit = get_manageDSAit( op );
343 rs->sr_err = mdb_opinfo_get( op, mdb, 1, &moi );
348 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
354 rs->sr_err = mdb_cursor_open( ltid, mdb->mi_id2entry, &mci );
356 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
360 rs->sr_err = mdb_cursor_open( ltid, mdb->mi_dn2id, &mcd );
362 mdb_cursor_close( mci );
363 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
367 scopes = scope_chunk_get( op );
371 isc.oscope = op->ors_scope;
373 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
374 MDB_IDL_ZERO(candidates);
377 /* get entry with reader lock */
378 rs->sr_err = mdb_dn2entry( op, ltid, mcd, &op->o_req_ndn, &e, &nsubs, 1 );
388 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
391 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
395 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
396 if ( matched && is_entry_alias( matched )) {
399 stub.bv_val = op->o_req_ndn.bv_val;
400 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
401 e = deref_base( op, rs, matched, &matched, ltid,
404 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
406 mdb_entry_return(op, e);
410 } else if ( e && is_entry_alias( e )) {
411 e = deref_base( op, rs, e, &matched, ltid,
417 struct berval matched_dn = BER_BVNULL;
419 if ( matched != NULL ) {
420 BerVarray erefs = NULL;
422 /* return referral only if "disclose"
423 * is granted on the object */
424 if ( ! access_allowed( op, matched,
425 slap_schema.si_ad_entry,
426 NULL, ACL_DISCLOSE, NULL ) )
428 rs->sr_err = LDAP_NO_SUCH_OBJECT;
431 ber_dupbv( &matched_dn, &matched->e_name );
433 erefs = is_entry_referral( matched )
434 ? get_entry_referrals( op, matched )
436 if ( rs->sr_err == MDB_NOTFOUND )
437 rs->sr_err = LDAP_REFERRAL;
438 rs->sr_matched = matched_dn.bv_val;
441 mdb_entry_return(op, matched);
445 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
446 &op->o_req_dn, op->oq_search.rs_scope );
447 ber_bvarray_free( erefs );
451 rs->sr_ref = referral_rewrite( default_referral,
452 NULL, &op->o_req_dn, op->oq_search.rs_scope );
453 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
456 send_ldap_result( op, rs );
459 ber_bvarray_free( rs->sr_ref );
462 if ( !BER_BVISNULL( &matched_dn ) ) {
463 ber_memfree( matched_dn.bv_val );
464 rs->sr_matched = NULL;
469 /* NOTE: __NEW__ "search" access is required
470 * on searchBase object */
471 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
472 NULL, ACL_SEARCH, NULL, &mask ) )
474 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
475 rs->sr_err = LDAP_NO_SUCH_OBJECT;
477 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
480 mdb_entry_return( op,e);
481 send_ldap_result( op, rs );
485 if ( !manageDSAit && is_entry_referral( e ) ) {
486 /* entry is a referral */
487 struct berval matched_dn = BER_BVNULL;
488 BerVarray erefs = NULL;
490 ber_dupbv( &matched_dn, &e->e_name );
491 erefs = get_entry_referrals( op, e );
493 rs->sr_err = LDAP_REFERRAL;
495 mdb_entry_return( op, e );
499 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
500 &op->o_req_dn, op->oq_search.rs_scope );
501 ber_bvarray_free( erefs );
504 rs->sr_text = "bad_referral object";
508 Debug( LDAP_DEBUG_TRACE,
509 LDAP_XSTRING(mdb_search) ": entry is referral\n",
512 rs->sr_matched = matched_dn.bv_val;
513 send_ldap_result( op, rs );
515 ber_bvarray_free( rs->sr_ref );
517 ber_memfree( matched_dn.bv_val );
518 rs->sr_matched = NULL;
522 if ( get_assert( op ) &&
523 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
525 rs->sr_err = LDAP_ASSERTION_FAILED;
526 mdb_entry_return( op,e);
527 send_ldap_result( op, rs );
531 /* compute it anyway; root does not use it */
532 stoptime = op->o_time + op->ors_tlimit;
538 /* select candidates */
539 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
540 rs->sr_err = base_candidate( op->o_bd, base, candidates );
544 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
547 key.mv_data = &base->e_id;
548 key.mv_size = sizeof( ID );
549 mdb_cursor_get( mcd, &key, &data, MDB_SET );
550 mdb_cursor_count( mcd, &nkids );
552 } else if ( !base->e_id ) {
553 /* we don't maintain nsubs for entryID 0.
554 * just grab entry count from id2entry stat
557 mdb_stat( ltid, mdb->mi_id2entry, &ms );
558 nsubs = ms.ms_entries;
560 MDB_IDL_ZERO( candidates );
562 scopes[1].mid = base->e_id;
563 scopes[1].mval.mv_data = NULL;
564 rs->sr_err = search_candidates( op, rs, base,
565 ltid, mci, candidates, scopes );
566 ncand = MDB_IDL_N( candidates );
567 if ( !base->e_id || ncand == NOID ) {
568 /* grab entry count from id2entry stat
571 mdb_stat( ltid, mdb->mi_id2entry, &ms );
573 nsubs = ms.ms_entries;
575 ncand = ms.ms_entries;
579 /* start cursor at beginning of candidates.
583 if ( candidates[0] == 0 ) {
584 Debug( LDAP_DEBUG_TRACE,
585 LDAP_XSTRING(mdb_search) ": no candidates\n",
591 /* if not root and candidates exceed to-be-checked entries, abort */
592 if ( op->ors_limit /* isroot == FALSE */ &&
593 op->ors_limit->lms_s_unchecked != -1 &&
594 ncand > (unsigned) op->ors_limit->lms_s_unchecked )
596 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
597 send_ldap_result( op, rs );
598 rs->sr_err = LDAP_SUCCESS;
602 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
603 !op->ors_limit->lms_s_pr_hide )
608 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
609 PagedResultsState *ps = op->o_pagedresults_state;
610 /* deferred cookie parsing */
611 rs->sr_err = parse_paged_cookie( op, rs );
612 if ( rs->sr_err != LDAP_SUCCESS ) {
613 send_ldap_result( op, rs );
617 cursor = (ID) ps->ps_cookie;
618 if ( cursor && ps->ps_size == 0 ) {
619 rs->sr_err = LDAP_SUCCESS;
620 rs->sr_text = "search abandoned by pagedResult size=0";
621 send_ldap_result( op, rs );
624 id = mdb_idl_first( candidates, &cursor );
626 Debug( LDAP_DEBUG_TRACE,
627 LDAP_XSTRING(mdb_search)
628 ": no paged results candidates\n",
630 send_paged_response( op, rs, &lastid, 0 );
632 rs->sr_err = LDAP_OTHER;
635 if ( id == (ID)ps->ps_cookie )
636 id = mdb_idl_next( candidates, &cursor );
637 nsubs = ncand; /* always bypass scope'd search */
640 if ( nsubs < ncand ) {
642 /* Do scope-based search */
644 /* if any alias scopes were set, save them */
645 if (scopes[0].mid > 1) {
647 for (cscope = 1; cscope <= scopes[0].mid; cscope++) {
648 /* Ignore the original base */
649 if (scopes[cscope].mid == base->e_id)
651 iscopes[cursor++] = scopes[cscope].mid;
653 iscopes[0] = scopes[0].mid - 1;
660 rc = mdb_dn2id_walk( op, &isc );
667 id = mdb_idl_first( candidates, &cursor );
677 /* check for abandon */
678 if ( op->o_abandon ) {
679 rs->sr_err = SLAPD_ABANDON;
680 send_ldap_result( op, rs );
684 /* mostly needed by internal searches,
685 * e.g. related to syncrepl, for whom
686 * abandon does not get set... */
687 if ( slapd_shutdown ) {
688 rs->sr_err = LDAP_UNAVAILABLE;
689 send_ldap_disconnect( op, rs );
693 /* check time limit */
694 if ( op->ors_tlimit != SLAP_NO_LIMIT
695 && slap_get_time() > stoptime )
697 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
698 rs->sr_ref = rs->sr_v2ref;
699 send_ldap_result( op, rs );
700 rs->sr_err = LDAP_SUCCESS;
705 if ( nsubs < ncand ) {
707 /* Is this entry in the candidate list? */
709 if (MDB_IDL_IS_RANGE( candidates )) {
710 if ( id >= MDB_IDL_RANGE_FIRST( candidates ) &&
711 id <= MDB_IDL_RANGE_LAST( candidates ))
714 i = mdb_idl_search( candidates, id );
715 if ( candidates[i] == id )
723 /* Does this candidate actually satisfy the search scope?
727 switch( op->ors_scope ) {
728 case LDAP_SCOPE_BASE:
729 /* This is always true, yes? */
730 if ( id == base->e_id ) scopeok = 1;
733 #ifdef LDAP_SCOPE_CHILDREN
734 case LDAP_SCOPE_CHILDREN:
735 if ( id == base->e_id ) break;
738 case LDAP_SCOPE_SUBTREE:
739 if ( id == base->e_id ) {
744 case LDAP_SCOPE_ONELEVEL:
747 rs->sr_err = mdb_idscopes( op, &isc );
748 if ( rs->sr_err == MDB_SUCCESS ) {
752 if ( rs->sr_err == MDB_NOTFOUND )
758 /* Not in scope, ignore it */
761 Debug( LDAP_DEBUG_TRACE,
762 LDAP_XSTRING(mdb_search)
763 ": %ld scope not okay\n",
769 if ( id == base->e_id ) {
774 rs->sr_err = mdb_id2edata( op, mci, id, &edata );
775 if ( rs->sr_err == MDB_NOTFOUND ) {
780 if( !MDB_IDL_IS_RANGE(candidates) ) {
781 /* only complain for non-range IDLs */
782 Debug( LDAP_DEBUG_TRACE,
783 LDAP_XSTRING(mdb_search)
784 ": candidate %ld not found\n",
787 /* get the next ID from the DB */
788 rs->sr_err = mdb_get_nextid( mci, &cursor );
789 if ( rs->sr_err == MDB_NOTFOUND ) {
793 rs->sr_err = LDAP_OTHER;
794 rs->sr_text = "internal error in get_nextid";
795 send_ldap_result( op, rs );
802 } else if ( rs->sr_err ) {
803 rs->sr_err = LDAP_OTHER;
804 rs->sr_text = "internal error in mdb_id2edata";
805 send_ldap_result( op, rs );
809 rs->sr_err = mdb_entry_decode( op, &edata, &e );
811 rs->sr_err = LDAP_OTHER;
812 rs->sr_text = "internal error in mdb_entry_decode";
813 send_ldap_result( op, rs );
817 e->e_name.bv_val = NULL;
818 e->e_nname.bv_val = NULL;
821 if ( is_entry_subentry( e ) ) {
822 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
823 if(!get_subentries_visibility( op )) {
824 /* only subentries are visible */
828 } else if ( get_subentries( op ) &&
829 !get_subentries_visibility( op ))
831 /* only subentries are visible */
835 } else if ( get_subentries_visibility( op )) {
836 /* only subentries are visible */
840 /* aliases were already dereferenced in candidate list */
841 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
842 /* but if the search base is an alias, and we didn't
843 * deref it when finding, return it.
845 if ( is_entry_alias(e) &&
846 ((op->ors_deref & LDAP_DEREF_FINDING) || e != base ))
852 if ( !manageDSAit && is_entry_glue( e )) {
857 struct berval pdn, pndn;
861 /* child of base, just append RDNs to base->e_name */
862 if ( nsubs < ncand || isc.scopes[isc.nscope].mid == base->e_id ) {
864 pndn = base->e_nname;
866 mdb_id2name( op, ltid, &isc.mc, scopes[isc.nscope].mid, &pdn, &pndn );
868 e->e_name.bv_len = pdn.bv_len;
869 e->e_nname.bv_len = pndn.bv_len;
870 for (i=0; i<isc.numrdns; i++) {
871 e->e_name.bv_len += isc.rdns[i].bv_len + 1;
872 e->e_nname.bv_len += isc.nrdns[i].bv_len + 1;
874 e->e_name.bv_val = op->o_tmpalloc(e->e_name.bv_len + 1, op->o_tmpmemctx);
875 e->e_nname.bv_val = op->o_tmpalloc(e->e_nname.bv_len + 1, op->o_tmpmemctx);
876 d = e->e_name.bv_val;
877 n = e->e_nname.bv_val;
879 /* RDNs are in top-down order */
880 for (i=isc.numrdns-1; i>=0; i--) {
881 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
882 d += isc.rdns[i].bv_len;
884 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
885 n += isc.nrdns[i].bv_len;
889 /* RDNs are in bottom-up order */
890 for (i=0; i<isc.numrdns; i++) {
891 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
892 d += isc.rdns[i].bv_len;
894 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
895 n += isc.nrdns[i].bv_len;
901 memcpy(d, pdn.bv_val, pdn.bv_len+1);
902 memcpy(n, pndn.bv_val, pndn.bv_len+1);
909 if (pndn.bv_val != base->e_nname.bv_val) {
910 op->o_tmpfree(pndn.bv_val, op->o_tmpmemctx);
911 op->o_tmpfree(pdn.bv_val, op->o_tmpmemctx);
916 * if it's a referral, add it to the list of referrals. only do
917 * this for non-base searches, and don't check the filter
918 * explicitly here since it's only a candidate anyway.
920 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
921 && is_entry_referral( e ) )
923 BerVarray erefs = get_entry_referrals( op, e );
924 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
925 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
926 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
931 send_search_reference( op, rs );
933 mdb_entry_return( op, e );
937 ber_bvarray_free( rs->sr_ref );
938 ber_bvarray_free( erefs );
944 /* if it matches the filter and scope, send it */
945 rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
947 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
948 /* check size limit */
949 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
950 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
951 mdb_entry_return( op, e );
953 send_paged_response( op, rs, &lastid, tentries );
961 rs->sr_attrs = op->oq_search.rs_attrs;
962 rs->sr_operational_attrs = NULL;
965 RS_ASSERT( e->e_private != NULL );
967 rs->sr_err = LDAP_SUCCESS;
968 rs->sr_err = send_search_entry( op, rs );
972 mdb_entry_return( op, e );
975 switch ( rs->sr_err ) {
976 case LDAP_SUCCESS: /* entry sent ok */
978 default: /* entry not sent */
980 case LDAP_UNAVAILABLE:
981 case LDAP_SIZELIMIT_EXCEEDED:
982 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
983 rs->sr_ref = rs->sr_v2ref;
984 send_ldap_result( op, rs );
985 rs->sr_err = LDAP_SUCCESS;
988 rs->sr_err = LDAP_OTHER;
995 Debug( LDAP_DEBUG_TRACE,
996 LDAP_XSTRING(mdb_search)
997 ": %ld does not match filter\n",
1004 mdb_entry_return( op, e );
1005 RS_ASSERT( rs->sr_entry == NULL );
1007 rs->sr_entry = NULL;
1010 if ( nsubs < ncand ) {
1011 int rc = mdb_dn2id_walk( op, &isc );
1014 /* We got to the end of a subtree. If there are any
1015 * alias scopes left, search them too.
1017 while (iscopes[0] && cscope < iscopes[0]) {
1019 isc.id = iscopes[cscope];
1021 mdb_entry_return( op, base );
1022 rs->sr_err = mdb_id2entry(op, mci, isc.id, &base);
1023 if ( !rs->sr_err ) {
1024 mdb_id2name( op, ltid, &isc.mc, isc.id, &base->e_name, &base->e_nname );
1026 if (isc.oscope == LDAP_SCOPE_ONELEVEL)
1027 isc.oscope = LDAP_SCOPE_BASE;
1028 rc = mdb_dn2id_walk( op, &isc );
1038 id = mdb_idl_next( candidates, &cursor );
1043 rs->sr_ctrls = NULL;
1044 rs->sr_ref = rs->sr_v2ref;
1045 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1046 rs->sr_rspoid = NULL;
1047 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
1048 send_paged_response( op, rs, NULL, 0 );
1050 send_ldap_result( op, rs );
1053 rs->sr_err = LDAP_SUCCESS;
1056 mdb_cursor_close( mcd );
1057 mdb_cursor_close( mci );
1058 if ( moi == &opinfo ) {
1059 mdb_txn_reset( moi->moi_txn );
1060 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
1064 if( rs->sr_v2ref ) {
1065 ber_bvarray_free( rs->sr_v2ref );
1066 rs->sr_v2ref = NULL;
1069 mdb_entry_return( op,base);
1070 scope_chunk_ret( op, scopes );
1076 static int base_candidate(
1081 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1082 e->e_nname.bv_val, (long) e->e_id, 0);
1089 /* Look for "objectClass Present" in this filter.
1090 * Also count depth of filter tree while we're at it.
1092 static int oc_filter(
1099 assert( f != NULL );
1101 if( cur > *max ) *max = cur;
1103 switch( f->f_choice ) {
1104 case LDAP_FILTER_PRESENT:
1105 if (f->f_desc == slap_schema.si_ad_objectClass) {
1110 case LDAP_FILTER_AND:
1111 case LDAP_FILTER_OR:
1113 for ( f=f->f_and; f; f=f->f_next ) {
1114 (void) oc_filter(f, cur, max);
1124 static void search_stack_free( void *key, void *data )
1126 ber_memfree_x(data, NULL);
1129 static void *search_stack( Operation *op )
1131 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1134 if ( op->o_threadctx ) {
1135 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
1138 ret = mdb->mi_search_stack;
1142 ret = ch_malloc( mdb->mi_search_stack_depth * MDB_IDL_UM_SIZE
1144 if ( op->o_threadctx ) {
1145 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1146 ret, search_stack_free, NULL, NULL );
1148 mdb->mi_search_stack = ret;
1154 static int search_candidates(
1163 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1165 Filter *f, rf, xf, nf, sf;
1167 AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1168 AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1171 * This routine takes as input a filter (user-filter)
1172 * and rewrites it as follows:
1173 * (&(scope=DN)[(objectClass=subentry)]
1174 * (|[(objectClass=referral)](user-filter))
1177 Debug(LDAP_DEBUG_TRACE,
1178 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1179 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1181 f = op->oq_search.rs_filter;
1183 /* If the user's filter uses objectClass=*,
1184 * these clauses are redundant.
1186 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1187 && !get_subentries_visibility(op)) {
1188 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1189 /* match referral objects */
1190 struct berval bv_ref = BER_BVC( "referral" );
1191 rf.f_choice = LDAP_FILTER_EQUALITY;
1193 rf.f_av_desc = slap_schema.si_ad_objectClass;
1194 rf.f_av_value = bv_ref;
1197 xf.f_choice = LDAP_FILTER_OR;
1204 if( get_subentries_visibility( op ) ) {
1205 struct berval bv_subentry = BER_BVC( "subentry" );
1206 sf.f_choice = LDAP_FILTER_EQUALITY;
1207 sf.f_ava = &aa_subentry;
1208 sf.f_av_desc = slap_schema.si_ad_objectClass;
1209 sf.f_av_value = bv_subentry;
1211 nf.f_choice = LDAP_FILTER_AND;
1218 /* Allocate IDL stack, plus 1 more for former tmp */
1219 if ( depth+1 > mdb->mi_search_stack_depth ) {
1220 stack = ch_malloc( (depth + 1) * MDB_IDL_UM_SIZE * sizeof( ID ) );
1222 stack = search_stack( op );
1225 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1226 rc = search_aliases( op, rs, e->e_id, txn, mci, scopes, stack );
1231 if ( rc == LDAP_SUCCESS ) {
1232 rc = mdb_filter_candidates( op, txn, f, ids,
1233 stack, stack+MDB_IDL_UM_SIZE );
1236 if ( depth+1 > mdb->mi_search_stack_depth ) {
1241 Debug(LDAP_DEBUG_TRACE,
1242 "mdb_search_candidates: failed (rc=%d)\n",
1246 Debug(LDAP_DEBUG_TRACE,
1247 "mdb_search_candidates: id=%ld first=%ld last=%ld\n",
1249 (long) MDB_IDL_FIRST(ids),
1250 (long) MDB_IDL_LAST(ids) );
1257 parse_paged_cookie( Operation *op, SlapReply *rs )
1259 int rc = LDAP_SUCCESS;
1260 PagedResultsState *ps = op->o_pagedresults_state;
1262 /* this function must be invoked only if the pagedResults
1263 * control has been detected, parsed and partially checked
1264 * by the frontend */
1265 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1267 /* cookie decoding/checks deferred to backend... */
1268 if ( ps->ps_cookieval.bv_len ) {
1269 PagedResultsCookie reqcookie;
1270 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1272 rs->sr_text = "paged results cookie is invalid";
1273 rc = LDAP_PROTOCOL_ERROR;
1277 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1279 if ( reqcookie > ps->ps_cookie ) {
1281 rs->sr_text = "paged results cookie is invalid";
1282 rc = LDAP_PROTOCOL_ERROR;
1285 } else if ( reqcookie < ps->ps_cookie ) {
1286 rs->sr_text = "paged results cookie is invalid or old";
1287 rc = LDAP_UNWILLING_TO_PERFORM;
1292 /* we're going to use ps_cookie */
1293 op->o_conn->c_pagedresults_state.ps_cookie = 0;
1302 send_paged_response(
1308 LDAPControl *ctrls[2];
1309 BerElementBuffer berbuf;
1310 BerElement *ber = (BerElement *)&berbuf;
1311 PagedResultsCookie respcookie;
1312 struct berval cookie;
1314 Debug(LDAP_DEBUG_ARGS,
1315 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1316 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1320 ber_init2( ber, NULL, LBER_USE_DER );
1323 respcookie = ( PagedResultsCookie )(*lastid);
1324 cookie.bv_len = sizeof( respcookie );
1325 cookie.bv_val = (char *)&respcookie;
1328 respcookie = ( PagedResultsCookie )0;
1329 BER_BVSTR( &cookie, "" );
1332 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1333 op->o_conn->c_pagedresults_state.ps_count =
1334 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1337 /* return size of 0 -- no estimate */
1338 ber_printf( ber, "{iO}", 0, &cookie );
1340 ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1341 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1345 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1346 ctrls[0]->ldctl_iscritical = 0;
1348 slap_add_ctrls( op, rs, ctrls );
1349 rs->sr_err = LDAP_SUCCESS;
1350 send_ldap_result( op, rs );
1353 (void) ber_free_buf( ber );