1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2012 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 || 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 );
223 /* If this is a OneLevel search, we're done; oldsubs only had one
224 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
226 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
228 ido = mdb_idl_next( oldsubs, &cursoro );
230 /* If we're done processing the old scopes, did we add any new
231 * scopes in this iteration? If so, go back and do those now.
234 if (MDB_IDL_IS_ZERO(newsubs)) break;
235 MDB_IDL_CPY(oldsubs, newsubs);
236 MDB_IDL_ZERO(newsubs);
238 ido = mdb_idl_first( oldsubs, &cursoro );
241 /* Find the entry corresponding to the next scope. If it can't
242 * be found, ignore it and move on. This should never happen;
243 * we should never see the ID of an entry that doesn't exist.
247 rs->sr_err = mdb_id2edata(op, mci, ido, &edata);
248 if ( rs->sr_err != MDB_SUCCESS ) {
257 /* Get the next ID from the DB. Used if the candidate list is
258 * a range and simple iteration hits missing entryIDs
261 mdb_get_nextid(MDB_cursor *mci, ID *cursor)
269 key.mv_size = sizeof(ID);
270 rc = mdb_cursor_get( mci, &key, NULL, MDB_SET_RANGE );
273 memcpy( cursor, key.mv_data, sizeof(ID));
277 static void scope_chunk_free( void *key, void *data )
280 for (p1 = data; p1; p1 = p2) {
281 p2 = p1[0].mval.mv_data;
282 ber_memfree_x(p1, NULL);
286 static ID2 *scope_chunk_get( Operation *op )
288 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
291 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
292 (void *)&ret, NULL );
294 ret = ch_malloc( MDB_IDL_UM_SIZE * sizeof( ID2 ));
296 void *r2 = ret[0].mval.mv_data;
297 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
298 r2, scope_chunk_free, NULL, NULL );
303 static void scope_chunk_ret( Operation *op, ID2 *scopes )
305 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
308 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
310 scopes[0].mval.mv_data = ret;
311 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
312 (void *)scopes, scope_chunk_free, NULL, NULL );
316 mdb_search( Operation *op, SlapReply *rs )
318 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
321 ID candidates[MDB_IDL_UM_SIZE];
323 Entry *e = NULL, *base = NULL;
324 Entry *matched = NULL;
325 AttributeName *attrs;
333 mdb_op_info opinfo = {{{0}}}, *moi = &opinfo;
334 MDB_txn *ltid = NULL;
336 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_search) "\n", 0, 0, 0);
337 attrs = op->oq_search.rs_attrs;
339 manageDSAit = get_manageDSAit( op );
341 rs->sr_err = mdb_opinfo_get( op, mdb, 1, &moi );
346 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
352 rs->sr_err = mdb_cursor_open( ltid, mdb->mi_id2entry, &mci );
354 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
358 scopes = scope_chunk_get( op );
363 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
364 MDB_IDL_ZERO(candidates);
367 /* get entry with reader lock */
368 rs->sr_err = mdb_dn2entry( op, ltid, NULL, &op->o_req_ndn, &e, 1 );
378 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
381 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
385 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
386 if ( matched && is_entry_alias( matched )) {
389 stub.bv_val = op->o_req_ndn.bv_val;
390 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
391 e = deref_base( op, rs, matched, &matched, ltid,
394 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
396 mdb_entry_return(op, e);
400 } else if ( e && is_entry_alias( e )) {
401 e = deref_base( op, rs, e, &matched, ltid,
407 struct berval matched_dn = BER_BVNULL;
409 if ( matched != NULL ) {
410 BerVarray erefs = NULL;
412 /* return referral only if "disclose"
413 * is granted on the object */
414 if ( ! access_allowed( op, matched,
415 slap_schema.si_ad_entry,
416 NULL, ACL_DISCLOSE, NULL ) )
418 rs->sr_err = LDAP_NO_SUCH_OBJECT;
421 ber_dupbv( &matched_dn, &matched->e_name );
423 erefs = is_entry_referral( matched )
424 ? get_entry_referrals( op, matched )
426 if ( rs->sr_err == MDB_NOTFOUND )
427 rs->sr_err = LDAP_REFERRAL;
428 rs->sr_matched = matched_dn.bv_val;
431 mdb_entry_return(op, matched);
435 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
436 &op->o_req_dn, op->oq_search.rs_scope );
437 ber_bvarray_free( erefs );
441 rs->sr_ref = referral_rewrite( default_referral,
442 NULL, &op->o_req_dn, op->oq_search.rs_scope );
443 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
446 send_ldap_result( op, rs );
449 ber_bvarray_free( rs->sr_ref );
452 if ( !BER_BVISNULL( &matched_dn ) ) {
453 ber_memfree( matched_dn.bv_val );
454 rs->sr_matched = NULL;
459 /* NOTE: __NEW__ "search" access is required
460 * on searchBase object */
461 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
462 NULL, ACL_SEARCH, NULL, &mask ) )
464 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
465 rs->sr_err = LDAP_NO_SUCH_OBJECT;
467 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
470 mdb_entry_return( op,e);
471 send_ldap_result( op, rs );
475 if ( !manageDSAit && is_entry_referral( e ) ) {
476 /* entry is a referral */
477 struct berval matched_dn = BER_BVNULL;
478 BerVarray erefs = NULL;
480 ber_dupbv( &matched_dn, &e->e_name );
481 erefs = get_entry_referrals( op, e );
483 rs->sr_err = LDAP_REFERRAL;
485 mdb_entry_return( op, e );
489 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
490 &op->o_req_dn, op->oq_search.rs_scope );
491 ber_bvarray_free( erefs );
494 rs->sr_text = "bad_referral object";
498 Debug( LDAP_DEBUG_TRACE,
499 LDAP_XSTRING(mdb_search) ": entry is referral\n",
502 rs->sr_matched = matched_dn.bv_val;
503 send_ldap_result( op, rs );
505 ber_bvarray_free( rs->sr_ref );
507 ber_memfree( matched_dn.bv_val );
508 rs->sr_matched = NULL;
512 if ( get_assert( op ) &&
513 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
515 rs->sr_err = LDAP_ASSERTION_FAILED;
516 mdb_entry_return( op,e);
517 send_ldap_result( op, rs );
521 /* compute it anyway; root does not use it */
522 stoptime = op->o_time + op->ors_tlimit;
528 /* select candidates */
529 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
530 rs->sr_err = base_candidate( op->o_bd, base, candidates );
533 MDB_IDL_ZERO( candidates );
535 scopes[1].mid = base->e_id;
536 scopes[1].mval.mv_data = NULL;
537 rs->sr_err = search_candidates( op, rs, base,
538 ltid, mci, candidates, scopes );
541 /* start cursor at beginning of candidates.
545 if ( candidates[0] == 0 ) {
546 Debug( LDAP_DEBUG_TRACE,
547 LDAP_XSTRING(mdb_search) ": no candidates\n",
553 /* if not root and candidates exceed to-be-checked entries, abort */
554 if ( op->ors_limit /* isroot == FALSE */ &&
555 op->ors_limit->lms_s_unchecked != -1 &&
556 MDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
558 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
559 send_ldap_result( op, rs );
560 rs->sr_err = LDAP_SUCCESS;
564 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
565 !op->ors_limit->lms_s_pr_hide )
567 tentries = MDB_IDL_N(candidates);
570 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
571 PagedResultsState *ps = op->o_pagedresults_state;
572 /* deferred cookie parsing */
573 rs->sr_err = parse_paged_cookie( op, rs );
574 if ( rs->sr_err != LDAP_SUCCESS ) {
575 send_ldap_result( op, rs );
579 cursor = (ID) ps->ps_cookie;
580 if ( cursor && ps->ps_size == 0 ) {
581 rs->sr_err = LDAP_SUCCESS;
582 rs->sr_text = "search abandoned by pagedResult size=0";
583 send_ldap_result( op, rs );
586 id = mdb_idl_first( candidates, &cursor );
588 Debug( LDAP_DEBUG_TRACE,
589 LDAP_XSTRING(mdb_search)
590 ": no paged results candidates\n",
592 send_paged_response( op, rs, &lastid, 0 );
594 rs->sr_err = LDAP_OTHER;
597 if ( id == (ID)ps->ps_cookie )
598 id = mdb_idl_next( candidates, &cursor );
602 for ( id = mdb_idl_first( candidates, &cursor );
603 id != NOID ; id = mdb_idl_next( candidates, &cursor ) )
610 /* check for abandon */
611 if ( op->o_abandon ) {
612 rs->sr_err = SLAPD_ABANDON;
613 send_ldap_result( op, rs );
617 /* mostly needed by internal searches,
618 * e.g. related to syncrepl, for whom
619 * abandon does not get set... */
620 if ( slapd_shutdown ) {
621 rs->sr_err = LDAP_UNAVAILABLE;
622 send_ldap_disconnect( op, rs );
626 /* check time limit */
627 if ( op->ors_tlimit != SLAP_NO_LIMIT
628 && slap_get_time() > stoptime )
630 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
631 rs->sr_ref = rs->sr_v2ref;
632 send_ldap_result( op, rs );
633 rs->sr_err = LDAP_SUCCESS;
637 if ( id == base->e_id ) {
642 rs->sr_err = mdb_id2edata( op, mci, id, &edata );
643 if ( rs->sr_err == MDB_NOTFOUND ) {
644 if( !MDB_IDL_IS_RANGE(candidates) ) {
645 /* only complain for non-range IDLs */
646 Debug( LDAP_DEBUG_TRACE,
647 LDAP_XSTRING(mdb_search)
648 ": candidate %ld not found\n",
651 /* get the next ID from the DB */
652 rs->sr_err = mdb_get_nextid( mci, &cursor );
653 if ( rs->sr_err == MDB_NOTFOUND ) {
657 rs->sr_err = LDAP_OTHER;
658 rs->sr_text = "internal error in get_nextid";
659 send_ldap_result( op, rs );
666 } else if ( rs->sr_err ) {
667 rs->sr_err = LDAP_OTHER;
668 rs->sr_text = "internal error in mdb_id2edata";
669 send_ldap_result( op, rs );
674 /* Does this candidate actually satisfy the search scope?
678 switch( op->ors_scope ) {
679 case LDAP_SCOPE_BASE:
680 /* This is always true, yes? */
681 if ( id == base->e_id ) scopeok = 1;
684 #ifdef LDAP_SCOPE_CHILDREN
685 case LDAP_SCOPE_CHILDREN:
686 if ( id == base->e_id ) break;
689 case LDAP_SCOPE_SUBTREE:
690 if ( id == base->e_id ) {
695 case LDAP_SCOPE_ONELEVEL:
697 if ( mdb_idscopes( op, &isc ) == MDB_SUCCESS ) scopeok = 1;
701 /* Not in scope, ignore it */
704 Debug( LDAP_DEBUG_TRACE,
705 LDAP_XSTRING(mdb_search)
706 ": %ld scope not okay\n",
711 if ( id != base->e_id ) {
712 rs->sr_err = mdb_entry_decode( op, &edata, &e );
714 rs->sr_err = LDAP_OTHER;
715 rs->sr_text = "internal error in mdb_entry_decode";
716 send_ldap_result( op, rs );
720 e->e_name.bv_val = NULL;
721 e->e_nname.bv_val = NULL;
724 if ( is_entry_subentry( e ) ) {
725 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
726 if(!get_subentries_visibility( op )) {
727 /* only subentries are visible */
731 } else if ( get_subentries( op ) &&
732 !get_subentries_visibility( op ))
734 /* only subentries are visible */
738 } else if ( get_subentries_visibility( op )) {
739 /* only subentries are visible */
743 /* aliases were already dereferenced in candidate list */
744 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
745 /* but if the search base is an alias, and we didn't
746 * deref it when finding, return it.
748 if ( is_entry_alias(e) &&
749 ((op->ors_deref & LDAP_DEREF_FINDING) ||
750 !bvmatch(&e->e_nname, &op->o_req_ndn)))
756 if ( !manageDSAit && is_entry_glue( e )) {
761 struct berval pdn, pndn;
764 /* child of base, just append RDNs to base->e_name */
765 if ( isc.nscope == 1 ) {
767 pndn = base->e_nname;
769 mdb_id2name( op, ltid, &isc.mc, scopes[isc.nscope].mid, &pdn, &pndn );
771 e->e_name.bv_len = pdn.bv_len;
772 e->e_nname.bv_len = pndn.bv_len;
773 for (i=0; i<isc.numrdns; i++) {
774 e->e_name.bv_len += isc.rdns[i].bv_len + 1;
775 e->e_nname.bv_len += isc.nrdns[i].bv_len + 1;
777 e->e_name.bv_val = op->o_tmpalloc(e->e_name.bv_len + 1, op->o_tmpmemctx);
778 e->e_nname.bv_val = op->o_tmpalloc(e->e_nname.bv_len + 1, op->o_tmpmemctx);
779 d = e->e_name.bv_val;
780 n = e->e_nname.bv_val;
781 for (i=0; i<isc.numrdns; i++) {
782 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
783 d += isc.rdns[i].bv_len;
785 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
786 n += isc.nrdns[i].bv_len;
790 memcpy(d, pdn.bv_val, pdn.bv_len+1);
791 memcpy(n, pndn.bv_val, pndn.bv_len+1);
798 if (isc.nscope != 1) {
799 op->o_tmpfree(pndn.bv_val, op->o_tmpmemctx);
800 op->o_tmpfree(pdn.bv_val, op->o_tmpmemctx);
805 * if it's a referral, add it to the list of referrals. only do
806 * this for non-base searches, and don't check the filter
807 * explicitly here since it's only a candidate anyway.
809 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
810 && is_entry_referral( e ) )
812 BerVarray erefs = get_entry_referrals( op, e );
813 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
814 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
815 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
820 send_search_reference( op, rs );
822 mdb_entry_return( op, e );
826 ber_bvarray_free( rs->sr_ref );
827 ber_bvarray_free( erefs );
833 /* if it matches the filter and scope, send it */
834 rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
836 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
837 /* check size limit */
838 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
839 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
840 mdb_entry_return( op, e );
842 send_paged_response( op, rs, &lastid, tentries );
850 rs->sr_attrs = op->oq_search.rs_attrs;
851 rs->sr_operational_attrs = NULL;
854 RS_ASSERT( e->e_private != NULL );
856 rs->sr_err = LDAP_SUCCESS;
857 rs->sr_err = send_search_entry( op, rs );
861 mdb_entry_return( op, e );
864 switch ( rs->sr_err ) {
865 case LDAP_SUCCESS: /* entry sent ok */
867 default: /* entry not sent */
869 case LDAP_UNAVAILABLE:
870 case LDAP_SIZELIMIT_EXCEEDED:
871 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
872 rs->sr_ref = rs->sr_v2ref;
873 send_ldap_result( op, rs );
874 rs->sr_err = LDAP_SUCCESS;
877 rs->sr_err = LDAP_OTHER;
884 Debug( LDAP_DEBUG_TRACE,
885 LDAP_XSTRING(mdb_search)
886 ": %ld does not match filter\n",
893 mdb_entry_return( op, e );
894 RS_ASSERT( rs->sr_entry == NULL );
902 rs->sr_ref = rs->sr_v2ref;
903 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
904 rs->sr_rspoid = NULL;
905 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
906 send_paged_response( op, rs, NULL, 0 );
908 send_ldap_result( op, rs );
911 rs->sr_err = LDAP_SUCCESS;
915 mdb_cursor_close( isc.mc );
917 mdb_cursor_close( mci );
918 if ( moi == &opinfo ) {
919 mdb_txn_reset( moi->moi_txn );
920 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
923 ber_bvarray_free( rs->sr_v2ref );
927 mdb_entry_return( op,base);
928 scope_chunk_ret( op, scopes );
934 static int base_candidate(
939 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
940 e->e_nname.bv_val, (long) e->e_id, 0);
947 /* Look for "objectClass Present" in this filter.
948 * Also count depth of filter tree while we're at it.
950 static int oc_filter(
959 if( cur > *max ) *max = cur;
961 switch( f->f_choice ) {
962 case LDAP_FILTER_PRESENT:
963 if (f->f_desc == slap_schema.si_ad_objectClass) {
968 case LDAP_FILTER_AND:
971 for ( f=f->f_and; f; f=f->f_next ) {
972 (void) oc_filter(f, cur, max);
982 static void search_stack_free( void *key, void *data )
984 ber_memfree_x(data, NULL);
987 static void *search_stack( Operation *op )
989 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
992 if ( op->o_threadctx ) {
993 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
996 ret = mdb->mi_search_stack;
1000 ret = ch_malloc( mdb->mi_search_stack_depth * MDB_IDL_UM_SIZE
1002 if ( op->o_threadctx ) {
1003 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1004 ret, search_stack_free, NULL, NULL );
1006 mdb->mi_search_stack = ret;
1012 static int search_candidates(
1021 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1023 Filter *f, rf, xf, nf, sf;
1025 AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1026 AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1029 * This routine takes as input a filter (user-filter)
1030 * and rewrites it as follows:
1031 * (&(scope=DN)[(objectClass=subentry)]
1032 * (|[(objectClass=referral)](user-filter))
1035 Debug(LDAP_DEBUG_TRACE,
1036 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1037 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1039 f = op->oq_search.rs_filter;
1041 /* If the user's filter uses objectClass=*,
1042 * these clauses are redundant.
1044 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1045 && !get_subentries_visibility(op)) {
1046 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1047 /* match referral objects */
1048 struct berval bv_ref = BER_BVC( "referral" );
1049 rf.f_choice = LDAP_FILTER_EQUALITY;
1051 rf.f_av_desc = slap_schema.si_ad_objectClass;
1052 rf.f_av_value = bv_ref;
1055 xf.f_choice = LDAP_FILTER_OR;
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;
1069 nf.f_choice = LDAP_FILTER_AND;
1076 /* Allocate IDL stack, plus 1 more for former tmp */
1077 if ( depth+1 > mdb->mi_search_stack_depth ) {
1078 stack = ch_malloc( (depth + 1) * MDB_IDL_UM_SIZE * sizeof( ID ) );
1080 stack = search_stack( op );
1083 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1084 rc = search_aliases( op, rs, e->e_id, txn, mci, scopes, stack );
1089 if ( rc == LDAP_SUCCESS ) {
1090 rc = mdb_filter_candidates( op, txn, f, ids,
1091 stack, stack+MDB_IDL_UM_SIZE );
1094 if ( depth+1 > mdb->mi_search_stack_depth ) {
1099 Debug(LDAP_DEBUG_TRACE,
1100 "mdb_search_candidates: failed (rc=%d)\n",
1104 Debug(LDAP_DEBUG_TRACE,
1105 "mdb_search_candidates: id=%ld first=%ld last=%ld\n",
1107 (long) MDB_IDL_FIRST(ids),
1108 (long) MDB_IDL_LAST(ids) );
1115 parse_paged_cookie( Operation *op, SlapReply *rs )
1117 int rc = LDAP_SUCCESS;
1118 PagedResultsState *ps = op->o_pagedresults_state;
1120 /* this function must be invoked only if the pagedResults
1121 * control has been detected, parsed and partially checked
1122 * by the frontend */
1123 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1125 /* cookie decoding/checks deferred to backend... */
1126 if ( ps->ps_cookieval.bv_len ) {
1127 PagedResultsCookie reqcookie;
1128 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1130 rs->sr_text = "paged results cookie is invalid";
1131 rc = LDAP_PROTOCOL_ERROR;
1135 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1137 if ( reqcookie > ps->ps_cookie ) {
1139 rs->sr_text = "paged results cookie is invalid";
1140 rc = LDAP_PROTOCOL_ERROR;
1143 } else if ( reqcookie < ps->ps_cookie ) {
1144 rs->sr_text = "paged results cookie is invalid or old";
1145 rc = LDAP_UNWILLING_TO_PERFORM;
1150 /* we're going to use ps_cookie */
1151 op->o_conn->c_pagedresults_state.ps_cookie = 0;
1160 send_paged_response(
1166 LDAPControl *ctrls[2];
1167 BerElementBuffer berbuf;
1168 BerElement *ber = (BerElement *)&berbuf;
1169 PagedResultsCookie respcookie;
1170 struct berval cookie;
1172 Debug(LDAP_DEBUG_ARGS,
1173 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1174 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1178 ber_init2( ber, NULL, LBER_USE_DER );
1181 respcookie = ( PagedResultsCookie )(*lastid);
1182 cookie.bv_len = sizeof( respcookie );
1183 cookie.bv_val = (char *)&respcookie;
1186 respcookie = ( PagedResultsCookie )0;
1187 BER_BVSTR( &cookie, "" );
1190 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1191 op->o_conn->c_pagedresults_state.ps_count =
1192 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1195 /* return size of 0 -- no estimate */
1196 ber_printf( ber, "{iO}", 0, &cookie );
1198 ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1199 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1203 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1204 ctrls[0]->ldctl_iscritical = 0;
1206 slap_add_ctrls( op, rs, ctrls );
1207 rs->sr_err = LDAP_SUCCESS;
1208 send_ldap_result( op, rs );
1211 (void) ber_free_buf( ber );