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 );
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;
319 ID id, cursor, nsubs, ncand;
321 ID candidates[MDB_IDL_UM_SIZE];
323 Entry *e = NULL, *base = NULL;
324 Entry *matched = NULL;
325 AttributeName *attrs;
331 MDB_cursor *mci, *mcd;
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 rs->sr_err = mdb_cursor_open( ltid, mdb->mi_dn2id, &mcd );
360 mdb_cursor_close( mci );
361 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
365 scopes = scope_chunk_get( op );
370 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
371 MDB_IDL_ZERO(candidates);
374 /* get entry with reader lock */
375 rs->sr_err = mdb_dn2entry( op, ltid, mcd, &op->o_req_ndn, &e, &nsubs, 1 );
385 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
388 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
392 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
393 if ( matched && is_entry_alias( matched )) {
396 stub.bv_val = op->o_req_ndn.bv_val;
397 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
398 e = deref_base( op, rs, matched, &matched, ltid,
401 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
403 mdb_entry_return(op, e);
407 } else if ( e && is_entry_alias( e )) {
408 e = deref_base( op, rs, e, &matched, ltid,
414 struct berval matched_dn = BER_BVNULL;
416 if ( matched != NULL ) {
417 BerVarray erefs = NULL;
419 /* return referral only if "disclose"
420 * is granted on the object */
421 if ( ! access_allowed( op, matched,
422 slap_schema.si_ad_entry,
423 NULL, ACL_DISCLOSE, NULL ) )
425 rs->sr_err = LDAP_NO_SUCH_OBJECT;
428 ber_dupbv( &matched_dn, &matched->e_name );
430 erefs = is_entry_referral( matched )
431 ? get_entry_referrals( op, matched )
433 if ( rs->sr_err == MDB_NOTFOUND )
434 rs->sr_err = LDAP_REFERRAL;
435 rs->sr_matched = matched_dn.bv_val;
438 mdb_entry_return(op, matched);
442 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
443 &op->o_req_dn, op->oq_search.rs_scope );
444 ber_bvarray_free( erefs );
448 rs->sr_ref = referral_rewrite( default_referral,
449 NULL, &op->o_req_dn, op->oq_search.rs_scope );
450 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
453 send_ldap_result( op, rs );
456 ber_bvarray_free( rs->sr_ref );
459 if ( !BER_BVISNULL( &matched_dn ) ) {
460 ber_memfree( matched_dn.bv_val );
461 rs->sr_matched = NULL;
466 /* NOTE: __NEW__ "search" access is required
467 * on searchBase object */
468 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
469 NULL, ACL_SEARCH, NULL, &mask ) )
471 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
472 rs->sr_err = LDAP_NO_SUCH_OBJECT;
474 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
477 mdb_entry_return( op,e);
478 send_ldap_result( op, rs );
482 if ( !manageDSAit && is_entry_referral( e ) ) {
483 /* entry is a referral */
484 struct berval matched_dn = BER_BVNULL;
485 BerVarray erefs = NULL;
487 ber_dupbv( &matched_dn, &e->e_name );
488 erefs = get_entry_referrals( op, e );
490 rs->sr_err = LDAP_REFERRAL;
492 mdb_entry_return( op, e );
496 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
497 &op->o_req_dn, op->oq_search.rs_scope );
498 ber_bvarray_free( erefs );
501 rs->sr_text = "bad_referral object";
505 Debug( LDAP_DEBUG_TRACE,
506 LDAP_XSTRING(mdb_search) ": entry is referral\n",
509 rs->sr_matched = matched_dn.bv_val;
510 send_ldap_result( op, rs );
512 ber_bvarray_free( rs->sr_ref );
514 ber_memfree( matched_dn.bv_val );
515 rs->sr_matched = NULL;
519 if ( get_assert( op ) &&
520 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
522 rs->sr_err = LDAP_ASSERTION_FAILED;
523 mdb_entry_return( op,e);
524 send_ldap_result( op, rs );
528 /* compute it anyway; root does not use it */
529 stoptime = op->o_time + op->ors_tlimit;
535 /* select candidates */
536 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
537 rs->sr_err = base_candidate( op->o_bd, base, candidates );
540 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
543 key.mv_data = &base->e_id;
544 key.mv_size = sizeof( ID );
545 mdb_cursor_get( mcd, &key, &data, MDB_SET );
546 mdb_cursor_count( mcd, &nkids );
548 } else if ( !base->e_id ) {
549 /* we don't maintain nsubs for entryID 0.
550 * just grab entry count from id2entry stat
553 mdb_stat( ltid, mdb->mi_id2entry, &ms );
554 nsubs = ms.ms_entries;
556 MDB_IDL_ZERO( candidates );
558 scopes[1].mid = base->e_id;
559 scopes[1].mval.mv_data = NULL;
560 rs->sr_err = search_candidates( op, rs, base,
561 ltid, mci, candidates, scopes );
562 ncand = MDB_IDL_N( candidates );
563 if ( !base->e_id || ncand == NOID ) {
564 /* grab entry count from id2entry stat
567 mdb_stat( ltid, mdb->mi_id2entry, &ms );
569 nsubs = ms.ms_entries;
571 ncand = ms.ms_entries;
575 /* start cursor at beginning of candidates.
579 if ( candidates[0] == 0 ) {
580 Debug( LDAP_DEBUG_TRACE,
581 LDAP_XSTRING(mdb_search) ": no candidates\n",
587 /* if not root and candidates exceed to-be-checked entries, abort */
588 if ( op->ors_limit /* isroot == FALSE */ &&
589 op->ors_limit->lms_s_unchecked != -1 &&
590 ncand > (unsigned) op->ors_limit->lms_s_unchecked )
592 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
593 send_ldap_result( op, rs );
594 rs->sr_err = LDAP_SUCCESS;
598 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
599 !op->ors_limit->lms_s_pr_hide )
604 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
605 PagedResultsState *ps = op->o_pagedresults_state;
606 /* deferred cookie parsing */
607 rs->sr_err = parse_paged_cookie( op, rs );
608 if ( rs->sr_err != LDAP_SUCCESS ) {
609 send_ldap_result( op, rs );
613 cursor = (ID) ps->ps_cookie;
614 if ( cursor && ps->ps_size == 0 ) {
615 rs->sr_err = LDAP_SUCCESS;
616 rs->sr_text = "search abandoned by pagedResult size=0";
617 send_ldap_result( op, rs );
620 id = mdb_idl_first( candidates, &cursor );
622 Debug( LDAP_DEBUG_TRACE,
623 LDAP_XSTRING(mdb_search)
624 ": no paged results candidates\n",
626 send_paged_response( op, rs, &lastid, 0 );
628 rs->sr_err = LDAP_OTHER;
631 if ( id == (ID)ps->ps_cookie )
632 id = mdb_idl_next( candidates, &cursor );
635 if ( nsubs < ncand ) {
637 /* Do scope-based search */
640 rc = mdb_dn2id_walk( op, &isc );
646 id = mdb_idl_first( candidates, &cursor );
656 /* check for abandon */
657 if ( op->o_abandon ) {
658 rs->sr_err = SLAPD_ABANDON;
659 send_ldap_result( op, rs );
663 /* mostly needed by internal searches,
664 * e.g. related to syncrepl, for whom
665 * abandon does not get set... */
666 if ( slapd_shutdown ) {
667 rs->sr_err = LDAP_UNAVAILABLE;
668 send_ldap_disconnect( op, rs );
672 /* check time limit */
673 if ( op->ors_tlimit != SLAP_NO_LIMIT
674 && slap_get_time() > stoptime )
676 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
677 rs->sr_ref = rs->sr_v2ref;
678 send_ldap_result( op, rs );
679 rs->sr_err = LDAP_SUCCESS;
684 if ( nsubs < ncand ) {
686 /* Is this entry in the candidate list? */
688 if (MDB_IDL_IS_RANGE( candidates )) {
689 if ( id >= MDB_IDL_RANGE_FIRST( candidates ) &&
690 id <= MDB_IDL_RANGE_LAST( candidates ))
693 i = mdb_idl_search( candidates, id );
694 if ( candidates[i] == id )
702 /* Does this candidate actually satisfy the search scope?
706 switch( op->ors_scope ) {
707 case LDAP_SCOPE_BASE:
708 /* This is always true, yes? */
709 if ( id == base->e_id ) scopeok = 1;
712 #ifdef LDAP_SCOPE_CHILDREN
713 case LDAP_SCOPE_CHILDREN:
714 if ( id == base->e_id ) break;
717 case LDAP_SCOPE_SUBTREE:
718 if ( id == base->e_id ) {
723 case LDAP_SCOPE_ONELEVEL:
726 rs->sr_err = mdb_idscopes( op, &isc );
727 if ( rs->sr_err == MDB_SUCCESS ) {
731 if ( rs->sr_err == MDB_NOTFOUND )
737 /* Not in scope, ignore it */
740 Debug( LDAP_DEBUG_TRACE,
741 LDAP_XSTRING(mdb_search)
742 ": %ld scope not okay\n",
748 if ( id == base->e_id ) {
753 rs->sr_err = mdb_id2edata( op, mci, id, &edata );
754 if ( rs->sr_err == MDB_NOTFOUND ) {
759 if( !MDB_IDL_IS_RANGE(candidates) ) {
760 /* only complain for non-range IDLs */
761 Debug( LDAP_DEBUG_TRACE,
762 LDAP_XSTRING(mdb_search)
763 ": candidate %ld not found\n",
766 /* get the next ID from the DB */
767 rs->sr_err = mdb_get_nextid( mci, &cursor );
768 if ( rs->sr_err == MDB_NOTFOUND ) {
772 rs->sr_err = LDAP_OTHER;
773 rs->sr_text = "internal error in get_nextid";
774 send_ldap_result( op, rs );
781 } else if ( rs->sr_err ) {
782 rs->sr_err = LDAP_OTHER;
783 rs->sr_text = "internal error in mdb_id2edata";
784 send_ldap_result( op, rs );
788 rs->sr_err = mdb_entry_decode( op, &edata, &e );
790 rs->sr_err = LDAP_OTHER;
791 rs->sr_text = "internal error in mdb_entry_decode";
792 send_ldap_result( op, rs );
796 e->e_name.bv_val = NULL;
797 e->e_nname.bv_val = NULL;
800 if ( is_entry_subentry( e ) ) {
801 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
802 if(!get_subentries_visibility( op )) {
803 /* only subentries are visible */
807 } else if ( get_subentries( op ) &&
808 !get_subentries_visibility( op ))
810 /* only subentries are visible */
814 } else if ( get_subentries_visibility( op )) {
815 /* only subentries are visible */
819 /* aliases were already dereferenced in candidate list */
820 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
821 /* but if the search base is an alias, and we didn't
822 * deref it when finding, return it.
824 if ( is_entry_alias(e) &&
825 ((op->ors_deref & LDAP_DEREF_FINDING) || e != base ))
831 if ( !manageDSAit && is_entry_glue( e )) {
836 struct berval pdn, pndn;
840 /* child of base, just append RDNs to base->e_name */
841 if ( nsubs < ncand || isc.nscope == 1 ) {
843 pndn = base->e_nname;
845 mdb_id2name( op, ltid, &isc.mc, scopes[isc.nscope].mid, &pdn, &pndn );
847 e->e_name.bv_len = pdn.bv_len;
848 e->e_nname.bv_len = pndn.bv_len;
849 for (i=0; i<isc.numrdns; i++) {
850 e->e_name.bv_len += isc.rdns[i].bv_len + 1;
851 e->e_nname.bv_len += isc.nrdns[i].bv_len + 1;
853 e->e_name.bv_val = op->o_tmpalloc(e->e_name.bv_len + 1, op->o_tmpmemctx);
854 e->e_nname.bv_val = op->o_tmpalloc(e->e_nname.bv_len + 1, op->o_tmpmemctx);
855 d = e->e_name.bv_val;
856 n = e->e_nname.bv_val;
858 /* RDNs are in top-down order */
859 for (i=isc.numrdns-1; i>=0; i--) {
860 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
861 d += isc.rdns[i].bv_len;
863 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
864 n += isc.nrdns[i].bv_len;
868 /* RDNs are in bottom-up order */
869 for (i=0; i<isc.numrdns; i++) {
870 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
871 d += isc.rdns[i].bv_len;
873 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
874 n += isc.nrdns[i].bv_len;
880 memcpy(d, pdn.bv_val, pdn.bv_len+1);
881 memcpy(n, pndn.bv_val, pndn.bv_len+1);
888 if (pndn.bv_val != base->e_nname.bv_val) {
889 op->o_tmpfree(pndn.bv_val, op->o_tmpmemctx);
890 op->o_tmpfree(pdn.bv_val, op->o_tmpmemctx);
895 * if it's a referral, add it to the list of referrals. only do
896 * this for non-base searches, and don't check the filter
897 * explicitly here since it's only a candidate anyway.
899 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
900 && is_entry_referral( e ) )
902 BerVarray erefs = get_entry_referrals( op, e );
903 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
904 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
905 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
910 send_search_reference( op, rs );
912 mdb_entry_return( op, e );
916 ber_bvarray_free( rs->sr_ref );
917 ber_bvarray_free( erefs );
923 /* if it matches the filter and scope, send it */
924 rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
926 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
927 /* check size limit */
928 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
929 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
930 mdb_entry_return( op, e );
932 send_paged_response( op, rs, &lastid, tentries );
940 rs->sr_attrs = op->oq_search.rs_attrs;
941 rs->sr_operational_attrs = NULL;
944 RS_ASSERT( e->e_private != NULL );
946 rs->sr_err = LDAP_SUCCESS;
947 rs->sr_err = send_search_entry( op, rs );
951 mdb_entry_return( op, e );
954 switch ( rs->sr_err ) {
955 case LDAP_SUCCESS: /* entry sent ok */
957 default: /* entry not sent */
959 case LDAP_UNAVAILABLE:
960 case LDAP_SIZELIMIT_EXCEEDED:
961 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
962 rs->sr_ref = rs->sr_v2ref;
963 send_ldap_result( op, rs );
964 rs->sr_err = LDAP_SUCCESS;
967 rs->sr_err = LDAP_OTHER;
974 Debug( LDAP_DEBUG_TRACE,
975 LDAP_XSTRING(mdb_search)
976 ": %ld does not match filter\n",
983 mdb_entry_return( op, e );
984 RS_ASSERT( rs->sr_entry == NULL );
989 if ( nsubs < ncand ) {
990 int rc = mdb_dn2id_walk( op, &isc );
996 id = mdb_idl_next( candidates, &cursor );
1001 rs->sr_ctrls = NULL;
1002 rs->sr_ref = rs->sr_v2ref;
1003 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1004 rs->sr_rspoid = NULL;
1005 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
1006 send_paged_response( op, rs, NULL, 0 );
1008 send_ldap_result( op, rs );
1011 rs->sr_err = LDAP_SUCCESS;
1014 mdb_cursor_close( mcd );
1015 mdb_cursor_close( mci );
1016 if ( moi == &opinfo ) {
1017 mdb_txn_reset( moi->moi_txn );
1018 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
1020 if( rs->sr_v2ref ) {
1021 ber_bvarray_free( rs->sr_v2ref );
1022 rs->sr_v2ref = NULL;
1025 mdb_entry_return( op,base);
1026 scope_chunk_ret( op, scopes );
1032 static int base_candidate(
1037 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1038 e->e_nname.bv_val, (long) e->e_id, 0);
1045 /* Look for "objectClass Present" in this filter.
1046 * Also count depth of filter tree while we're at it.
1048 static int oc_filter(
1055 assert( f != NULL );
1057 if( cur > *max ) *max = cur;
1059 switch( f->f_choice ) {
1060 case LDAP_FILTER_PRESENT:
1061 if (f->f_desc == slap_schema.si_ad_objectClass) {
1066 case LDAP_FILTER_AND:
1067 case LDAP_FILTER_OR:
1069 for ( f=f->f_and; f; f=f->f_next ) {
1070 (void) oc_filter(f, cur, max);
1080 static void search_stack_free( void *key, void *data )
1082 ber_memfree_x(data, NULL);
1085 static void *search_stack( Operation *op )
1087 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1090 if ( op->o_threadctx ) {
1091 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
1094 ret = mdb->mi_search_stack;
1098 ret = ch_malloc( mdb->mi_search_stack_depth * MDB_IDL_UM_SIZE
1100 if ( op->o_threadctx ) {
1101 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1102 ret, search_stack_free, NULL, NULL );
1104 mdb->mi_search_stack = ret;
1110 static int search_candidates(
1119 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1121 Filter *f, rf, xf, nf, sf;
1123 AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1124 AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1127 * This routine takes as input a filter (user-filter)
1128 * and rewrites it as follows:
1129 * (&(scope=DN)[(objectClass=subentry)]
1130 * (|[(objectClass=referral)](user-filter))
1133 Debug(LDAP_DEBUG_TRACE,
1134 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1135 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1137 f = op->oq_search.rs_filter;
1139 /* If the user's filter uses objectClass=*,
1140 * these clauses are redundant.
1142 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1143 && !get_subentries_visibility(op)) {
1144 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1145 /* match referral objects */
1146 struct berval bv_ref = BER_BVC( "referral" );
1147 rf.f_choice = LDAP_FILTER_EQUALITY;
1149 rf.f_av_desc = slap_schema.si_ad_objectClass;
1150 rf.f_av_value = bv_ref;
1153 xf.f_choice = LDAP_FILTER_OR;
1160 if( get_subentries_visibility( op ) ) {
1161 struct berval bv_subentry = BER_BVC( "subentry" );
1162 sf.f_choice = LDAP_FILTER_EQUALITY;
1163 sf.f_ava = &aa_subentry;
1164 sf.f_av_desc = slap_schema.si_ad_objectClass;
1165 sf.f_av_value = bv_subentry;
1167 nf.f_choice = LDAP_FILTER_AND;
1174 /* Allocate IDL stack, plus 1 more for former tmp */
1175 if ( depth+1 > mdb->mi_search_stack_depth ) {
1176 stack = ch_malloc( (depth + 1) * MDB_IDL_UM_SIZE * sizeof( ID ) );
1178 stack = search_stack( op );
1181 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1182 rc = search_aliases( op, rs, e->e_id, txn, mci, scopes, stack );
1187 if ( rc == LDAP_SUCCESS ) {
1188 rc = mdb_filter_candidates( op, txn, f, ids,
1189 stack, stack+MDB_IDL_UM_SIZE );
1192 if ( depth+1 > mdb->mi_search_stack_depth ) {
1197 Debug(LDAP_DEBUG_TRACE,
1198 "mdb_search_candidates: failed (rc=%d)\n",
1202 Debug(LDAP_DEBUG_TRACE,
1203 "mdb_search_candidates: id=%ld first=%ld last=%ld\n",
1205 (long) MDB_IDL_FIRST(ids),
1206 (long) MDB_IDL_LAST(ids) );
1213 parse_paged_cookie( Operation *op, SlapReply *rs )
1215 int rc = LDAP_SUCCESS;
1216 PagedResultsState *ps = op->o_pagedresults_state;
1218 /* this function must be invoked only if the pagedResults
1219 * control has been detected, parsed and partially checked
1220 * by the frontend */
1221 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1223 /* cookie decoding/checks deferred to backend... */
1224 if ( ps->ps_cookieval.bv_len ) {
1225 PagedResultsCookie reqcookie;
1226 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1228 rs->sr_text = "paged results cookie is invalid";
1229 rc = LDAP_PROTOCOL_ERROR;
1233 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1235 if ( reqcookie > ps->ps_cookie ) {
1237 rs->sr_text = "paged results cookie is invalid";
1238 rc = LDAP_PROTOCOL_ERROR;
1241 } else if ( reqcookie < ps->ps_cookie ) {
1242 rs->sr_text = "paged results cookie is invalid or old";
1243 rc = LDAP_UNWILLING_TO_PERFORM;
1248 /* we're going to use ps_cookie */
1249 op->o_conn->c_pagedresults_state.ps_cookie = 0;
1258 send_paged_response(
1264 LDAPControl *ctrls[2];
1265 BerElementBuffer berbuf;
1266 BerElement *ber = (BerElement *)&berbuf;
1267 PagedResultsCookie respcookie;
1268 struct berval cookie;
1270 Debug(LDAP_DEBUG_ARGS,
1271 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1272 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1276 ber_init2( ber, NULL, LBER_USE_DER );
1279 respcookie = ( PagedResultsCookie )(*lastid);
1280 cookie.bv_len = sizeof( respcookie );
1281 cookie.bv_val = (char *)&respcookie;
1284 respcookie = ( PagedResultsCookie )0;
1285 BER_BVSTR( &cookie, "" );
1288 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1289 op->o_conn->c_pagedresults_state.ps_count =
1290 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1293 /* return size of 0 -- no estimate */
1294 ber_printf( ber, "{iO}", 0, &cookie );
1296 ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1297 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1301 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1302 ctrls[0]->ldctl_iscritical = 0;
1304 slap_add_ctrls( op, rs, ctrls );
1305 rs->sr_err = LDAP_SUCCESS;
1306 send_ldap_result( op, rs );
1309 (void) ber_free_buf( ber );