1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 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, 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) {
167 oldsubs[1] = e->e_id;
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->e_id, aliases, curscop );
184 mdb_entry_return( op, e );
187 /* Dereference all of the aliases in the current scope. */
189 for (ida = mdb_idl_first(curscop, &cursora); ida != NOID;
190 ida = mdb_idl_next(curscop, &cursora))
192 rs->sr_err = mdb_id2entry(op, mci, ida, &a);
193 if (rs->sr_err != LDAP_SUCCESS) {
197 /* This should only happen if the curscop IDL has maxed out and
198 * turned into a range that spans IDs indiscriminately
200 if (!is_entry_alias(a)) {
201 mdb_entry_return(op, a);
205 /* Actually dereference the alias */
207 a = deref_base( op, rs, a, &matched, txn,
210 /* If the target was not already in our current scopes,
211 * make note of it in the newsubs list.
215 mid.mval.mv_data = NULL;
216 if (mdb_id2l_insert(scopes, &mid) == 0) {
217 mdb_idl_insert(newsubs, a->e_id);
219 mdb_entry_return( op, a );
221 } else if (matched) {
222 /* Alias could not be dereferenced, or it deref'd to
223 * an ID we've already seen. Ignore it.
225 mdb_entry_return( op, matched );
229 /* If this is a OneLevel search, we're done; oldsubs only had one
230 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
232 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
234 ido = mdb_idl_next( oldsubs, &cursoro );
236 /* If we're done processing the old scopes, did we add any new
237 * scopes in this iteration? If so, go back and do those now.
240 if (MDB_IDL_IS_ZERO(newsubs)) break;
241 MDB_IDL_CPY(oldsubs, newsubs);
242 MDB_IDL_ZERO(newsubs);
244 ido = mdb_idl_first( oldsubs, &cursoro );
247 /* Find the entry corresponding to the next scope. If it can't
248 * be found, ignore it and move on. This should never happen;
249 * we should never see the ID of an entry that doesn't exist.
251 rs->sr_err = mdb_id2entry(op, mci, ido, &e);
252 if ( rs->sr_err != LDAP_SUCCESS ) {
259 /* Get the next ID from the DB. Used if the candidate list is
260 * a range and simple iteration hits missing entryIDs
263 mdb_get_nextid(MDB_cursor *mci, ID *cursor)
271 key.mv_size = sizeof(ID);
272 rc = mdb_cursor_get( mci, &key, NULL, MDB_SET_RANGE );
275 memcpy( cursor, key.mv_data, sizeof(ID));
279 static void scope_chunk_free( void *key, void *data )
282 for (p1 = data; p1; p1 = p2) {
283 p2 = p1[0].mval.mv_data;
284 ber_memfree_x(p1, NULL);
288 static ID2 *scope_chunk_get( Operation *op )
290 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
293 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
294 (void *)&ret, NULL );
296 ret = ch_malloc( MDB_IDL_UM_SIZE * sizeof( ID2 ));
298 void *r2 = ret[0].mval.mv_data;
299 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
300 r2, scope_chunk_free, NULL, NULL );
305 static void scope_chunk_ret( Operation *op, ID2 *scopes )
307 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
310 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
312 scopes[0].mval.mv_data = ret;
313 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
314 (void *)scopes, scope_chunk_free, NULL, NULL );
318 mdb_search( Operation *op, SlapReply *rs )
320 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
323 ID candidates[MDB_IDL_UM_SIZE];
325 Entry *e = NULL, *base = NULL;
326 Entry *matched = NULL;
327 AttributeName *attrs;
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 scopes = scope_chunk_get( op );
365 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
366 MDB_IDL_ZERO(candidates);
369 /* get entry with reader lock */
370 rs->sr_err = mdb_dn2entry( op, ltid, NULL, &op->o_req_ndn, &e, 1 );
380 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
383 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
387 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
388 if ( matched && is_entry_alias( matched )) {
391 stub.bv_val = op->o_req_ndn.bv_val;
392 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
393 e = deref_base( op, rs, matched, &matched, ltid,
396 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
398 mdb_entry_return(op, e);
402 } else if ( e && is_entry_alias( e )) {
403 e = deref_base( op, rs, e, &matched, ltid,
409 struct berval matched_dn = BER_BVNULL;
411 if ( matched != NULL ) {
412 BerVarray erefs = NULL;
414 /* return referral only if "disclose"
415 * is granted on the object */
416 if ( ! access_allowed( op, matched,
417 slap_schema.si_ad_entry,
418 NULL, ACL_DISCLOSE, NULL ) )
420 rs->sr_err = LDAP_NO_SUCH_OBJECT;
423 ber_dupbv( &matched_dn, &matched->e_name );
425 erefs = is_entry_referral( matched )
426 ? get_entry_referrals( op, matched )
428 if ( rs->sr_err == MDB_NOTFOUND )
429 rs->sr_err = LDAP_REFERRAL;
430 rs->sr_matched = matched_dn.bv_val;
433 mdb_entry_return(op, matched);
437 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
438 &op->o_req_dn, op->oq_search.rs_scope );
439 ber_bvarray_free( erefs );
443 rs->sr_ref = referral_rewrite( default_referral,
444 NULL, &op->o_req_dn, op->oq_search.rs_scope );
445 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
448 send_ldap_result( op, rs );
451 ber_bvarray_free( rs->sr_ref );
454 if ( !BER_BVISNULL( &matched_dn ) ) {
455 ber_memfree( matched_dn.bv_val );
456 rs->sr_matched = NULL;
461 /* NOTE: __NEW__ "search" access is required
462 * on searchBase object */
463 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
464 NULL, ACL_SEARCH, NULL, &mask ) )
466 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
467 rs->sr_err = LDAP_NO_SUCH_OBJECT;
469 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
472 mdb_entry_return( op,e);
473 send_ldap_result( op, rs );
477 if ( !manageDSAit && is_entry_referral( e ) ) {
478 /* entry is a referral */
479 struct berval matched_dn = BER_BVNULL;
480 BerVarray erefs = NULL;
482 ber_dupbv( &matched_dn, &e->e_name );
483 erefs = get_entry_referrals( op, e );
485 rs->sr_err = LDAP_REFERRAL;
487 mdb_entry_return( op, e );
491 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
492 &op->o_req_dn, op->oq_search.rs_scope );
493 ber_bvarray_free( erefs );
496 rs->sr_text = "bad_referral object";
500 Debug( LDAP_DEBUG_TRACE,
501 LDAP_XSTRING(mdb_search) ": entry is referral\n",
504 rs->sr_matched = matched_dn.bv_val;
505 send_ldap_result( op, rs );
507 ber_bvarray_free( rs->sr_ref );
509 ber_memfree( matched_dn.bv_val );
510 rs->sr_matched = NULL;
514 if ( get_assert( op ) &&
515 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
517 rs->sr_err = LDAP_ASSERTION_FAILED;
518 mdb_entry_return( op,e);
519 send_ldap_result( op, rs );
523 /* compute it anyway; root does not use it */
524 stoptime = op->o_time + op->ors_tlimit;
530 /* select candidates */
531 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
532 rs->sr_err = base_candidate( op->o_bd, base, candidates );
535 MDB_IDL_ZERO( candidates );
537 scopes[1].mid = base->e_id;
538 scopes[1].mval.mv_data = NULL;
539 rs->sr_err = search_candidates( op, rs, base,
540 ltid, mci, candidates, scopes );
543 /* start cursor at beginning of candidates.
547 if ( candidates[0] == 0 ) {
548 Debug( LDAP_DEBUG_TRACE,
549 LDAP_XSTRING(mdb_search) ": no candidates\n",
555 /* if not root and candidates exceed to-be-checked entries, abort */
556 if ( op->ors_limit /* isroot == FALSE */ &&
557 op->ors_limit->lms_s_unchecked != -1 &&
558 MDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
560 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
561 send_ldap_result( op, rs );
562 rs->sr_err = LDAP_SUCCESS;
566 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
567 !op->ors_limit->lms_s_pr_hide )
569 tentries = MDB_IDL_N(candidates);
572 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
573 PagedResultsState *ps = op->o_pagedresults_state;
574 /* deferred cookie parsing */
575 rs->sr_err = parse_paged_cookie( op, rs );
576 if ( rs->sr_err != LDAP_SUCCESS ) {
577 send_ldap_result( op, rs );
581 cursor = (ID) ps->ps_cookie;
582 if ( cursor && ps->ps_size == 0 ) {
583 rs->sr_err = LDAP_SUCCESS;
584 rs->sr_text = "search abandoned by pagedResult size=0";
585 send_ldap_result( op, rs );
588 id = mdb_idl_first( candidates, &cursor );
590 Debug( LDAP_DEBUG_TRACE,
591 LDAP_XSTRING(mdb_search)
592 ": no paged results candidates\n",
594 send_paged_response( op, rs, &lastid, 0 );
596 rs->sr_err = LDAP_OTHER;
599 if ( id == (ID)ps->ps_cookie )
600 id = mdb_idl_next( candidates, &cursor );
604 for ( id = mdb_idl_first( candidates, &cursor );
605 id != NOID ; id = mdb_idl_next( candidates, &cursor ) )
611 /* check for abandon */
612 if ( op->o_abandon ) {
613 rs->sr_err = SLAPD_ABANDON;
614 send_ldap_result( op, rs );
618 /* mostly needed by internal searches,
619 * e.g. related to syncrepl, for whom
620 * abandon does not get set... */
621 if ( slapd_shutdown ) {
622 rs->sr_err = LDAP_UNAVAILABLE;
623 send_ldap_disconnect( op, rs );
627 /* check time limit */
628 if ( op->ors_tlimit != SLAP_NO_LIMIT
629 && slap_get_time() > stoptime )
631 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
632 rs->sr_ref = rs->sr_v2ref;
633 send_ldap_result( op, rs );
634 rs->sr_err = LDAP_SUCCESS;
638 if ( id == base->e_id ) {
643 rs->sr_err = mdb_id2entry( op, mci, id, &e );
645 if (rs->sr_err == LDAP_BUSY) {
646 rs->sr_text = "ldap server busy";
647 send_ldap_result( op, rs );
650 } else if ( rs->sr_err == LDAP_OTHER ) {
651 rs->sr_text = "internal error";
652 send_ldap_result( op, rs );
657 if( !MDB_IDL_IS_RANGE(candidates) ) {
658 /* only complain for non-range IDLs */
659 Debug( LDAP_DEBUG_TRACE,
660 LDAP_XSTRING(mdb_search)
661 ": candidate %ld not found\n",
664 /* get the next ID from the DB */
665 rs->sr_err = mdb_get_nextid( mci, &cursor );
666 if ( rs->sr_err == MDB_NOTFOUND ) {
670 rs->sr_err = LDAP_OTHER;
671 rs->sr_text = "internal error in get_nextid";
672 send_ldap_result( op, rs );
682 if ( is_entry_subentry( e ) ) {
683 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
684 if(!get_subentries_visibility( op )) {
685 /* only subentries are visible */
689 } else if ( get_subentries( op ) &&
690 !get_subentries_visibility( op ))
692 /* only subentries are visible */
696 } else if ( get_subentries_visibility( op )) {
697 /* only subentries are visible */
701 /* Does this candidate actually satisfy the search scope?
705 switch( op->ors_scope ) {
706 case LDAP_SCOPE_BASE:
707 /* This is always true, yes? */
708 if ( id == base->e_id ) scopeok = 1;
711 #ifdef LDAP_SCOPE_CHILDREN
712 case LDAP_SCOPE_CHILDREN:
713 if ( id == base->e_id ) break;
716 case LDAP_SCOPE_SUBTREE:
717 if ( id == base->e_id ) {
722 case LDAP_SCOPE_ONELEVEL:
724 if ( mdb_idscopes( op, &isc ) == MDB_SUCCESS ) scopeok = 1;
728 /* aliases were already dereferenced in candidate list */
729 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
730 /* but if the search base is an alias, and we didn't
731 * deref it when finding, return it.
733 if ( is_entry_alias(e) &&
734 ((op->ors_deref & LDAP_DEREF_FINDING) ||
735 !bvmatch(&e->e_nname, &op->o_req_ndn)))
741 /* Not in scope, ignore it */
744 Debug( LDAP_DEBUG_TRACE,
745 LDAP_XSTRING(mdb_search)
746 ": %ld scope not okay\n",
751 if ( !manageDSAit && is_entry_glue( e )) {
756 struct berval pdn, pndn;
759 /* child of base, just append RDNs to base->e_name */
760 if ( isc.nscope == 1 ) {
762 pndn = base->e_nname;
764 mdb_id2name( op, ltid, &isc.mc, scopes[isc.nscope].mid, &pdn, &pndn );
766 e->e_name.bv_len = pdn.bv_len;
767 e->e_nname.bv_len = pndn.bv_len;
768 for (i=0; i<isc.numrdns; i++) {
769 e->e_name.bv_len += isc.rdns[i].bv_len + 1;
770 e->e_nname.bv_len += isc.nrdns[i].bv_len + 1;
772 e->e_name.bv_val = op->o_tmpalloc(e->e_name.bv_len + 1, op->o_tmpmemctx);
773 e->e_nname.bv_val = op->o_tmpalloc(e->e_nname.bv_len + 1, op->o_tmpmemctx);
774 d = e->e_name.bv_val;
775 n = e->e_nname.bv_val;
776 for (i=0; i<isc.numrdns; i++) {
777 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
778 d += isc.rdns[i].bv_len;
780 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
781 n += isc.nrdns[i].bv_len;
785 memcpy(d, pdn.bv_val, pdn.bv_len+1);
786 memcpy(n, pndn.bv_val, pndn.bv_len+1);
793 if (isc.nscope != 1) {
794 op->o_tmpfree(pndn.bv_val, op->o_tmpmemctx);
795 op->o_tmpfree(pdn.bv_val, op->o_tmpmemctx);
800 * if it's a referral, add it to the list of referrals. only do
801 * this for non-base searches, and don't check the filter
802 * explicitly here since it's only a candidate anyway.
804 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
805 && is_entry_referral( e ) )
807 BerVarray erefs = get_entry_referrals( op, e );
808 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
809 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
810 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
815 send_search_reference( op, rs );
817 mdb_entry_return( op, e );
821 ber_bvarray_free( rs->sr_ref );
822 ber_bvarray_free( erefs );
828 /* if it matches the filter and scope, send it */
829 rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
831 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
832 /* check size limit */
833 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
834 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
835 mdb_entry_return( op, e );
837 send_paged_response( op, rs, &lastid, tentries );
845 rs->sr_attrs = op->oq_search.rs_attrs;
846 rs->sr_operational_attrs = NULL;
849 RS_ASSERT( e->e_private != NULL );
851 rs->sr_err = LDAP_SUCCESS;
852 rs->sr_err = send_search_entry( op, rs );
856 mdb_entry_return( op, e );
859 switch ( rs->sr_err ) {
860 case LDAP_SUCCESS: /* entry sent ok */
862 default: /* entry not sent */
864 case LDAP_UNAVAILABLE:
865 case LDAP_SIZELIMIT_EXCEEDED:
866 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
867 rs->sr_ref = rs->sr_v2ref;
868 send_ldap_result( op, rs );
869 rs->sr_err = LDAP_SUCCESS;
872 rs->sr_err = LDAP_OTHER;
879 Debug( LDAP_DEBUG_TRACE,
880 LDAP_XSTRING(mdb_search)
881 ": %ld does not match filter\n",
888 mdb_entry_return( op, e );
889 RS_ASSERT( rs->sr_entry == NULL );
897 rs->sr_ref = rs->sr_v2ref;
898 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
899 rs->sr_rspoid = NULL;
900 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
901 send_paged_response( op, rs, NULL, 0 );
903 send_ldap_result( op, rs );
906 rs->sr_err = LDAP_SUCCESS;
910 mdb_cursor_close( isc.mc );
912 mdb_cursor_close( mci );
913 if ( moi == &opinfo ) {
914 mdb_txn_reset( moi->moi_txn );
915 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
918 ber_bvarray_free( rs->sr_v2ref );
922 mdb_entry_return( op,base);
923 scope_chunk_ret( op, scopes );
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 mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
987 if ( op->o_threadctx ) {
988 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
991 ret = mdb->mi_search_stack;
995 ret = ch_malloc( mdb->mi_search_stack_depth * MDB_IDL_UM_SIZE
997 if ( op->o_threadctx ) {
998 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
999 ret, search_stack_free, NULL, NULL );
1001 mdb->mi_search_stack = ret;
1007 static int search_candidates(
1016 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1018 Filter *f, rf, xf, nf, sf;
1020 AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1021 AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1024 * This routine takes as input a filter (user-filter)
1025 * and rewrites it as follows:
1026 * (&(scope=DN)[(objectClass=subentry)]
1027 * (|[(objectClass=referral)](user-filter))
1030 Debug(LDAP_DEBUG_TRACE,
1031 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1032 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1034 f = op->oq_search.rs_filter;
1036 /* If the user's filter uses objectClass=*,
1037 * these clauses are redundant.
1039 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1040 && !get_subentries_visibility(op)) {
1041 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1042 /* match referral objects */
1043 struct berval bv_ref = BER_BVC( "referral" );
1044 rf.f_choice = LDAP_FILTER_EQUALITY;
1046 rf.f_av_desc = slap_schema.si_ad_objectClass;
1047 rf.f_av_value = bv_ref;
1050 xf.f_choice = LDAP_FILTER_OR;
1057 if( get_subentries_visibility( op ) ) {
1058 struct berval bv_subentry = BER_BVC( "subentry" );
1059 sf.f_choice = LDAP_FILTER_EQUALITY;
1060 sf.f_ava = &aa_subentry;
1061 sf.f_av_desc = slap_schema.si_ad_objectClass;
1062 sf.f_av_value = bv_subentry;
1064 nf.f_choice = LDAP_FILTER_AND;
1071 /* Allocate IDL stack, plus 1 more for former tmp */
1072 if ( depth+1 > mdb->mi_search_stack_depth ) {
1073 stack = ch_malloc( (depth + 1) * MDB_IDL_UM_SIZE * sizeof( ID ) );
1075 stack = search_stack( op );
1078 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1079 rc = search_aliases( op, rs, e, txn, mci, scopes, stack );
1084 if ( rc == LDAP_SUCCESS ) {
1085 rc = mdb_filter_candidates( op, txn, f, ids,
1086 stack, stack+MDB_IDL_UM_SIZE );
1089 if ( depth+1 > mdb->mi_search_stack_depth ) {
1094 Debug(LDAP_DEBUG_TRACE,
1095 "mdb_search_candidates: failed (rc=%d)\n",
1099 Debug(LDAP_DEBUG_TRACE,
1100 "mdb_search_candidates: id=%ld first=%ld last=%ld\n",
1102 (long) MDB_IDL_FIRST(ids),
1103 (long) MDB_IDL_LAST(ids) );
1110 parse_paged_cookie( Operation *op, SlapReply *rs )
1112 int rc = LDAP_SUCCESS;
1113 PagedResultsState *ps = op->o_pagedresults_state;
1115 /* this function must be invoked only if the pagedResults
1116 * control has been detected, parsed and partially checked
1117 * by the frontend */
1118 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1120 /* cookie decoding/checks deferred to backend... */
1121 if ( ps->ps_cookieval.bv_len ) {
1122 PagedResultsCookie reqcookie;
1123 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1125 rs->sr_text = "paged results cookie is invalid";
1126 rc = LDAP_PROTOCOL_ERROR;
1130 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1132 if ( reqcookie > ps->ps_cookie ) {
1134 rs->sr_text = "paged results cookie is invalid";
1135 rc = LDAP_PROTOCOL_ERROR;
1138 } else if ( reqcookie < ps->ps_cookie ) {
1139 rs->sr_text = "paged results cookie is invalid or old";
1140 rc = LDAP_UNWILLING_TO_PERFORM;
1145 /* we're going to use ps_cookie */
1146 op->o_conn->c_pagedresults_state.ps_cookie = 0;
1155 send_paged_response(
1161 LDAPControl *ctrls[2];
1162 BerElementBuffer berbuf;
1163 BerElement *ber = (BerElement *)&berbuf;
1164 PagedResultsCookie respcookie;
1165 struct berval cookie;
1167 Debug(LDAP_DEBUG_ARGS,
1168 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1169 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1173 ber_init2( ber, NULL, LBER_USE_DER );
1176 respcookie = ( PagedResultsCookie )(*lastid);
1177 cookie.bv_len = sizeof( respcookie );
1178 cookie.bv_val = (char *)&respcookie;
1181 respcookie = ( PagedResultsCookie )0;
1182 BER_BVSTR( &cookie, "" );
1185 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1186 op->o_conn->c_pagedresults_state.ps_count =
1187 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1190 /* return size of 0 -- no estimate */
1191 ber_printf( ber, "{iO}", 0, &cookie );
1193 ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1194 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1198 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1199 ctrls[0]->ldctl_iscritical = 0;
1201 slap_add_ctrls( op, rs, ctrls );
1202 rs->sr_err = LDAP_SUCCESS;
1203 send_ldap_result( op, rs );
1206 (void) ber_free_buf( ber );