1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2014 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(
40 static int parse_paged_cookie( Operation *op, SlapReply *rs );
42 static void send_paged_response(
48 /* Dereference aliases for a single alias entry. Return the final
49 * dereferenced entry on success, NULL on any failure.
51 static Entry * deref_base (
62 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
63 rs->sr_text = "maximum deref depth exceeded";
66 /* Remember the last entry we looked at, so we can
71 if (MDB_IDL_N(tmp) >= op->o_bd->be_max_deref_depth) {
76 /* If this is part of a subtree or onelevel search,
77 * have we seen this ID before? If so, quit.
79 if ( visited && mdb_idl_insert( visited, e->e_id ) ) {
84 /* If we've seen this ID during this deref iteration,
87 if ( mdb_idl_insert( tmp, e->e_id ) ) {
88 rs->sr_err = LDAP_ALIAS_PROBLEM;
89 rs->sr_text = "circular alias";
94 /* If there was a problem getting the aliasedObjectName,
95 * get_alias_dn will have set the error status.
97 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
102 rs->sr_err = mdb_dn2entry( op, txn, NULL, &ndn, &e, NULL, 0 );
104 rs->sr_err = LDAP_ALIAS_PROBLEM;
105 rs->sr_text = "aliasedObject not found";
109 /* Free the previous entry, continue to work with the
110 * one we just retrieved.
112 mdb_entry_return( op, *matched );
114 /* We found a regular entry. Return this to the caller.
116 if (!is_entry_alias(e)) {
117 rs->sr_err = LDAP_SUCCESS;
125 /* Look for and dereference all aliases within the search scope.
126 * Requires "stack" to be able to hold 6 levels of DB_SIZE IDLs.
127 * Of course we're hardcoded to require a minimum of 8 UM_SIZE
128 * IDLs so this is never a problem.
130 static int search_aliases(
139 ID *aliases, *curscop, *visited, *newsubs, *oldsubs, *tmp;
140 ID cursora, ida, cursoro, ido;
142 struct berval bv_alias = BER_BVC( "alias" );
143 AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
147 aliases = stack; /* IDL of all aliases in the database */
148 curscop = aliases + MDB_IDL_DB_SIZE; /* Aliases in the current scope */
149 visited = curscop + MDB_IDL_DB_SIZE; /* IDs we've seen in this search */
150 newsubs = visited + MDB_IDL_DB_SIZE; /* New subtrees we've added */
151 oldsubs = newsubs + MDB_IDL_DB_SIZE; /* Subtrees added previously */
152 tmp = oldsubs + MDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
154 af.f_choice = LDAP_FILTER_EQUALITY;
155 af.f_ava = &aa_alias;
156 af.f_av_desc = slap_schema.si_ad_objectClass;
157 af.f_av_value = bv_alias;
160 /* Find all aliases in database */
161 MDB_IDL_ZERO( aliases );
162 rs->sr_err = mdb_filter_candidates( op, txn, &af, aliases,
164 if (rs->sr_err != LDAP_SUCCESS || MDB_IDL_IS_ZERO( aliases )) {
170 MDB_IDL_ZERO( visited );
171 MDB_IDL_ZERO( newsubs );
174 ido = mdb_idl_first( oldsubs, &cursoro );
177 /* Set curscop to only the aliases in the current scope. Start with
178 * all the aliases, then get the intersection with the scope.
180 rs->sr_err = mdb_idscope( op, txn, e_id, aliases, curscop );
182 /* Dereference all of the aliases in the current scope. */
184 for (ida = mdb_idl_first(curscop, &cursora); ida != NOID;
185 ida = mdb_idl_next(curscop, &cursora))
187 rs->sr_err = mdb_id2entry(op, mci, ida, &a);
188 if (rs->sr_err != LDAP_SUCCESS) {
192 /* This should only happen if the curscop IDL has maxed out and
193 * turned into a range that spans IDs indiscriminately
195 if (!is_entry_alias(a)) {
196 mdb_entry_return(op, a);
200 /* Actually dereference the alias */
202 a = deref_base( op, rs, a, &matched, txn,
205 /* If the target was not already in our current scopes,
206 * make note of it in the newsubs list.
210 mid.mval.mv_data = NULL;
211 if (mdb_id2l_insert(scopes, &mid) == 0) {
212 mdb_idl_insert(newsubs, a->e_id);
214 mdb_entry_return( op, a );
216 } else if (matched) {
217 /* Alias could not be dereferenced, or it deref'd to
218 * an ID we've already seen. Ignore it.
220 mdb_entry_return( op, matched );
225 /* If this is a OneLevel search, we're done; oldsubs only had one
226 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
228 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
230 ido = mdb_idl_next( oldsubs, &cursoro );
232 /* If we're done processing the old scopes, did we add any new
233 * scopes in this iteration? If so, go back and do those now.
236 if (MDB_IDL_IS_ZERO(newsubs)) break;
237 MDB_IDL_CPY(oldsubs, newsubs);
238 MDB_IDL_ZERO(newsubs);
240 ido = mdb_idl_first( oldsubs, &cursoro );
243 /* Find the entry corresponding to the next scope. If it can't
244 * be found, ignore it and move on. This should never happen;
245 * we should never see the ID of an entry that doesn't exist.
249 rs->sr_err = mdb_id2edata(op, mci, ido, &edata);
250 if ( rs->sr_err != MDB_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 );
317 static void *search_stack( Operation *op );
319 typedef struct ww_ctx {
321 MDB_cursor *mcd; /* if set, save cursor context */
327 /* ITS#7904 if we get blocked while writing results to client,
328 * release the current reader txn and reacquire it after we
330 * Slight problem - if we're doing a scope-based walk (mdb_dn2id_walk)
331 * to return results, we need to remember the state of the mcd cursor.
332 * If the node that cursor was pointing to gets deleted while we're
333 * blocked, we may be unable to restore the cursor position. In that
334 * case return an LDAP_BUSY error - let the client know this search
335 * couldn't succeed, but might succeed on a retry.
338 mdb_writewait( Operation *op, slap_callback *sc )
340 ww_ctx *ww = sc->sc_private;
344 mdb_cursor_get( ww->mcd, &key, &data, MDB_GET_CURRENT );
345 memcpy( &ww->key, key.mv_data, sizeof(ID) );
346 ww->data.mv_size = data.mv_size;
347 ww->data.mv_data = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
348 memcpy(ww->data.mv_data, data.mv_data, data.mv_size);
350 mdb_txn_reset( ww->txn );
356 mdb_waitfixup( Operation *op, ww_ctx *ww, MDB_cursor *mci, MDB_cursor *mcd )
360 mdb_txn_renew( ww->txn );
361 mdb_cursor_renew( ww->txn, mci );
362 mdb_cursor_renew( ww->txn, mcd );
365 key.mv_size = sizeof(ID);
366 key.mv_data = &ww->key;
368 rc = mdb_cursor_get( mcd, &key, &data, MDB_GET_BOTH );
369 if ( rc == MDB_NOTFOUND ) {
371 rc = mdb_cursor_get( mcd, &key, &data, MDB_GET_BOTH_RANGE );
372 /* the loop will skip this node using NEXT_DUP but we want it
373 * sent, so go back one space first
375 if ( rc == MDB_SUCCESS )
376 mdb_cursor_get( mcd, &key, &data, MDB_PREV_DUP );
382 op->o_tmpfree( ww->data.mv_data, op->o_tmpmemctx );
383 ww->data.mv_data = NULL;
389 mdb_search( Operation *op, SlapReply *rs )
391 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
392 ID id, cursor, nsubs, ncand, cscope;
394 ID candidates[MDB_IDL_UM_SIZE];
395 ID iscopes[MDB_IDL_DB_SIZE];
398 Entry *e = NULL, *base = NULL;
399 Entry *matched = NULL;
400 AttributeName *attrs;
406 MDB_cursor *mci, *mcd;
408 slap_callback cb = { 0 };
410 mdb_op_info opinfo = {{{0}}}, *moi = &opinfo;
411 MDB_txn *ltid = NULL;
413 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_search) "\n", 0, 0, 0);
414 attrs = op->oq_search.rs_attrs;
416 manageDSAit = get_manageDSAit( op );
418 rs->sr_err = mdb_opinfo_get( op, mdb, 1, &moi );
423 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
429 rs->sr_err = mdb_cursor_open( ltid, mdb->mi_id2entry, &mci );
431 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
435 rs->sr_err = mdb_cursor_open( ltid, mdb->mi_dn2id, &mcd );
437 mdb_cursor_close( mci );
438 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
442 scopes = scope_chunk_get( op );
443 stack = search_stack( op );
447 isc.oscope = op->ors_scope;
450 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
451 MDB_IDL_ZERO(candidates);
454 /* get entry with reader lock */
455 rs->sr_err = mdb_dn2entry( op, ltid, mcd, &op->o_req_ndn, &e, &nsubs, 1 );
465 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
468 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
472 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
473 if ( matched && is_entry_alias( matched )) {
476 stub.bv_val = op->o_req_ndn.bv_val;
477 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
478 e = deref_base( op, rs, matched, &matched, ltid,
481 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
483 mdb_entry_return(op, e);
487 } else if ( e && is_entry_alias( e )) {
488 e = deref_base( op, rs, e, &matched, ltid,
494 struct berval matched_dn = BER_BVNULL;
496 if ( matched != NULL ) {
497 BerVarray erefs = NULL;
499 /* return referral only if "disclose"
500 * is granted on the object */
501 if ( ! access_allowed( op, matched,
502 slap_schema.si_ad_entry,
503 NULL, ACL_DISCLOSE, NULL ) )
505 rs->sr_err = LDAP_NO_SUCH_OBJECT;
508 ber_dupbv( &matched_dn, &matched->e_name );
510 erefs = is_entry_referral( matched )
511 ? get_entry_referrals( op, matched )
513 if ( rs->sr_err == MDB_NOTFOUND )
514 rs->sr_err = LDAP_REFERRAL;
515 rs->sr_matched = matched_dn.bv_val;
518 mdb_entry_return(op, matched);
522 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
523 &op->o_req_dn, op->oq_search.rs_scope );
524 ber_bvarray_free( erefs );
528 rs->sr_ref = referral_rewrite( default_referral,
529 NULL, &op->o_req_dn, op->oq_search.rs_scope );
530 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
533 send_ldap_result( op, rs );
536 ber_bvarray_free( rs->sr_ref );
539 if ( !BER_BVISNULL( &matched_dn ) ) {
540 ber_memfree( matched_dn.bv_val );
541 rs->sr_matched = NULL;
546 /* NOTE: __NEW__ "search" access is required
547 * on searchBase object */
548 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
549 NULL, ACL_SEARCH, NULL, &mask ) )
551 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
552 rs->sr_err = LDAP_NO_SUCH_OBJECT;
554 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
557 mdb_entry_return( op,e);
558 send_ldap_result( op, rs );
562 if ( !manageDSAit && is_entry_referral( e ) ) {
563 /* entry is a referral */
564 struct berval matched_dn = BER_BVNULL;
565 BerVarray erefs = NULL;
567 ber_dupbv( &matched_dn, &e->e_name );
568 erefs = get_entry_referrals( op, e );
570 rs->sr_err = LDAP_REFERRAL;
572 mdb_entry_return( op, e );
576 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
577 &op->o_req_dn, op->oq_search.rs_scope );
578 ber_bvarray_free( erefs );
581 rs->sr_text = "bad_referral object";
585 Debug( LDAP_DEBUG_TRACE,
586 LDAP_XSTRING(mdb_search) ": entry is referral\n",
589 rs->sr_matched = matched_dn.bv_val;
590 send_ldap_result( op, rs );
592 ber_bvarray_free( rs->sr_ref );
594 ber_memfree( matched_dn.bv_val );
595 rs->sr_matched = NULL;
599 if ( get_assert( op ) &&
600 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
602 rs->sr_err = LDAP_ASSERTION_FAILED;
603 mdb_entry_return( op,e);
604 send_ldap_result( op, rs );
608 /* compute it anyway; root does not use it */
609 stoptime = op->o_time + op->ors_tlimit;
615 /* select candidates */
616 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
617 rs->sr_err = base_candidate( op->o_bd, base, candidates );
621 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
624 key.mv_data = &base->e_id;
625 key.mv_size = sizeof( ID );
626 mdb_cursor_get( mcd, &key, &data, MDB_SET );
627 mdb_cursor_count( mcd, &nkids );
629 } else if ( !base->e_id ) {
630 /* we don't maintain nsubs for entryID 0.
631 * just grab entry count from id2entry stat
634 mdb_stat( ltid, mdb->mi_id2entry, &ms );
635 nsubs = ms.ms_entries;
637 MDB_IDL_ZERO( candidates );
639 scopes[1].mid = base->e_id;
640 scopes[1].mval.mv_data = NULL;
641 rs->sr_err = search_candidates( op, rs, base,
642 ltid, mci, candidates, scopes, stack );
643 ncand = MDB_IDL_N( candidates );
644 if ( !base->e_id || ncand == NOID ) {
645 /* grab entry count from id2entry stat
648 mdb_stat( ltid, mdb->mi_id2entry, &ms );
650 nsubs = ms.ms_entries;
652 ncand = ms.ms_entries;
656 /* start cursor at beginning of candidates.
660 if ( candidates[0] == 0 ) {
661 Debug( LDAP_DEBUG_TRACE,
662 LDAP_XSTRING(mdb_search) ": no candidates\n",
668 /* if not root and candidates exceed to-be-checked entries, abort */
669 if ( op->ors_limit /* isroot == FALSE */ &&
670 op->ors_limit->lms_s_unchecked != -1 &&
671 ncand > (unsigned) op->ors_limit->lms_s_unchecked )
673 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
674 send_ldap_result( op, rs );
675 rs->sr_err = LDAP_SUCCESS;
679 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
680 !op->ors_limit->lms_s_pr_hide )
685 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
686 PagedResultsState *ps = op->o_pagedresults_state;
687 /* deferred cookie parsing */
688 rs->sr_err = parse_paged_cookie( op, rs );
689 if ( rs->sr_err != LDAP_SUCCESS ) {
690 send_ldap_result( op, rs );
694 cursor = (ID) ps->ps_cookie;
695 if ( cursor && ps->ps_size == 0 ) {
696 rs->sr_err = LDAP_SUCCESS;
697 rs->sr_text = "search abandoned by pagedResult size=0";
698 send_ldap_result( op, rs );
701 id = mdb_idl_first( candidates, &cursor );
703 Debug( LDAP_DEBUG_TRACE,
704 LDAP_XSTRING(mdb_search)
705 ": no paged results candidates\n",
707 send_paged_response( op, rs, &lastid, 0 );
709 rs->sr_err = LDAP_OTHER;
712 if ( id == (ID)ps->ps_cookie )
713 id = mdb_idl_next( candidates, &cursor );
714 nsubs = ncand; /* always bypass scope'd search */
717 if ( nsubs < ncand ) {
719 /* Do scope-based search */
721 /* if any alias scopes were set, save them */
722 if (scopes[0].mid > 1) {
724 for (cscope = 1; cscope <= scopes[0].mid; cscope++) {
725 iscopes[cursor++] = scopes[cscope].mid;
727 iscopes[0] = scopes[0].mid;
735 rc = mdb_dn2id_walk( op, &isc );
740 cscope = 1; /* skip original base */
742 id = mdb_idl_first( candidates, &cursor );
747 /* If we're running in our own read txn */
748 if ( moi == &opinfo ) {
749 cb.sc_writewait = mdb_writewait;
750 cb.sc_private = &wwctx;
752 cb.sc_next = op->o_callback;
753 op->o_callback = &cb;
763 /* check for abandon */
764 if ( op->o_abandon ) {
765 rs->sr_err = SLAPD_ABANDON;
766 send_ldap_result( op, rs );
770 /* mostly needed by internal searches,
771 * e.g. related to syncrepl, for whom
772 * abandon does not get set... */
773 if ( slapd_shutdown ) {
774 rs->sr_err = LDAP_UNAVAILABLE;
775 send_ldap_disconnect( op, rs );
779 /* check time limit */
780 if ( op->ors_tlimit != SLAP_NO_LIMIT
781 && slap_get_time() > stoptime )
783 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
784 rs->sr_ref = rs->sr_v2ref;
785 send_ldap_result( op, rs );
786 rs->sr_err = LDAP_SUCCESS;
791 if ( nsubs < ncand ) {
793 /* Is this entry in the candidate list? */
795 if (MDB_IDL_IS_RANGE( candidates )) {
796 if ( id >= MDB_IDL_RANGE_FIRST( candidates ) &&
797 id <= MDB_IDL_RANGE_LAST( candidates ))
800 i = mdb_idl_search( candidates, id );
801 if ( candidates[i] == id )
809 /* Does this candidate actually satisfy the search scope?
813 switch( op->ors_scope ) {
814 case LDAP_SCOPE_BASE:
815 /* This is always true, yes? */
816 if ( id == base->e_id ) scopeok = 1;
819 #ifdef LDAP_SCOPE_CHILDREN
820 case LDAP_SCOPE_CHILDREN:
821 if ( id == base->e_id ) break;
824 case LDAP_SCOPE_SUBTREE:
825 if ( id == base->e_id ) {
830 case LDAP_SCOPE_ONELEVEL:
833 rs->sr_err = mdb_idscopes( op, &isc );
834 if ( rs->sr_err == MDB_SUCCESS ) {
838 if ( rs->sr_err == MDB_NOTFOUND )
844 /* Not in scope, ignore it */
847 Debug( LDAP_DEBUG_TRACE,
848 LDAP_XSTRING(mdb_search)
849 ": %ld scope not okay\n",
855 if ( id == base->e_id ) {
860 rs->sr_err = mdb_id2edata( op, mci, id, &edata );
861 if ( rs->sr_err == MDB_NOTFOUND ) {
866 if( !MDB_IDL_IS_RANGE(candidates) ) {
867 /* only complain for non-range IDLs */
868 Debug( LDAP_DEBUG_TRACE,
869 LDAP_XSTRING(mdb_search)
870 ": candidate %ld not found\n",
873 /* get the next ID from the DB */
874 rs->sr_err = mdb_get_nextid( mci, &cursor );
875 if ( rs->sr_err == MDB_NOTFOUND ) {
879 rs->sr_err = LDAP_OTHER;
880 rs->sr_text = "internal error in get_nextid";
881 send_ldap_result( op, rs );
888 } else if ( rs->sr_err ) {
889 rs->sr_err = LDAP_OTHER;
890 rs->sr_text = "internal error in mdb_id2edata";
891 send_ldap_result( op, rs );
895 rs->sr_err = mdb_entry_decode( op, ltid, &edata, &e );
897 rs->sr_err = LDAP_OTHER;
898 rs->sr_text = "internal error in mdb_entry_decode";
899 send_ldap_result( op, rs );
903 e->e_name.bv_val = NULL;
904 e->e_nname.bv_val = NULL;
907 if ( is_entry_subentry( e ) ) {
908 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
909 if(!get_subentries_visibility( op )) {
910 /* only subentries are visible */
914 } else if ( get_subentries( op ) &&
915 !get_subentries_visibility( op ))
917 /* only subentries are visible */
921 } else if ( get_subentries_visibility( op )) {
922 /* only subentries are visible */
926 /* aliases were already dereferenced in candidate list */
927 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
928 /* but if the search base is an alias, and we didn't
929 * deref it when finding, return it.
931 if ( is_entry_alias(e) &&
932 ((op->ors_deref & LDAP_DEREF_FINDING) || e != base ))
938 if ( !manageDSAit && is_entry_glue( e )) {
943 struct berval pdn, pndn;
947 /* child of base, just append RDNs to base->e_name */
948 if ( nsubs < ncand || isc.scopes[isc.nscope].mid == base->e_id ) {
950 pndn = base->e_nname;
952 mdb_id2name( op, ltid, &isc.mc, scopes[isc.nscope].mid, &pdn, &pndn, NULL );
954 e->e_name.bv_len = pdn.bv_len;
955 e->e_nname.bv_len = pndn.bv_len;
956 for (i=0; i<isc.numrdns; i++) {
957 e->e_name.bv_len += isc.rdns[i].bv_len + 1;
958 e->e_nname.bv_len += isc.nrdns[i].bv_len + 1;
960 e->e_name.bv_val = op->o_tmpalloc(e->e_name.bv_len + 1, op->o_tmpmemctx);
961 e->e_nname.bv_val = op->o_tmpalloc(e->e_nname.bv_len + 1, op->o_tmpmemctx);
962 d = e->e_name.bv_val;
963 n = e->e_nname.bv_val;
965 /* RDNs are in top-down order */
966 for (i=isc.numrdns-1; i>=0; i--) {
967 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
968 d += isc.rdns[i].bv_len;
970 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
971 n += isc.nrdns[i].bv_len;
975 /* RDNs are in bottom-up order */
976 for (i=0; i<isc.numrdns; i++) {
977 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
978 d += isc.rdns[i].bv_len;
980 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
981 n += isc.nrdns[i].bv_len;
987 memcpy(d, pdn.bv_val, pdn.bv_len+1);
988 memcpy(n, pndn.bv_val, pndn.bv_len+1);
995 if (pndn.bv_val != base->e_nname.bv_val) {
996 op->o_tmpfree(pndn.bv_val, op->o_tmpmemctx);
997 op->o_tmpfree(pdn.bv_val, op->o_tmpmemctx);
1002 * if it's a referral, add it to the list of referrals. only do
1003 * this for non-base searches, and don't check the filter
1004 * explicitly here since it's only a candidate anyway.
1006 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
1007 && is_entry_referral( e ) )
1009 BerVarray erefs = get_entry_referrals( op, e );
1010 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1011 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1012 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1017 send_search_reference( op, rs );
1020 mdb_entry_return( op, e );
1021 rs->sr_entry = NULL;
1024 ber_bvarray_free( rs->sr_ref );
1025 ber_bvarray_free( erefs );
1029 rs->sr_err = mdb_waitfixup( op, &wwctx, mci, mcd );
1031 send_ldap_result( op, rs );
1039 /* if it matches the filter and scope, send it */
1040 rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
1042 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1043 /* check size limit */
1044 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
1045 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
1046 mdb_entry_return( op, e );
1048 send_paged_response( op, rs, &lastid, tentries );
1056 rs->sr_attrs = op->oq_search.rs_attrs;
1057 rs->sr_operational_attrs = NULL;
1058 rs->sr_ctrls = NULL;
1060 RS_ASSERT( e->e_private != NULL );
1062 rs->sr_err = LDAP_SUCCESS;
1063 rs->sr_err = send_search_entry( op, rs );
1064 rs->sr_attrs = NULL;
1065 rs->sr_entry = NULL;
1067 mdb_entry_return( op, e );
1070 switch ( rs->sr_err ) {
1071 case LDAP_SUCCESS: /* entry sent ok */
1073 default: /* entry not sent */
1076 send_ldap_result( op, rs );
1078 case LDAP_UNAVAILABLE:
1079 case LDAP_SIZELIMIT_EXCEEDED:
1080 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1081 rs->sr_ref = rs->sr_v2ref;
1082 send_ldap_result( op, rs );
1083 rs->sr_err = LDAP_SUCCESS;
1086 rs->sr_err = LDAP_OTHER;
1091 rs->sr_err = mdb_waitfixup( op, &wwctx, mci, mcd );
1093 send_ldap_result( op, rs );
1100 Debug( LDAP_DEBUG_TRACE,
1101 LDAP_XSTRING(mdb_search)
1102 ": %ld does not match filter\n",
1109 mdb_entry_return( op, e );
1110 RS_ASSERT( rs->sr_entry == NULL );
1112 rs->sr_entry = NULL;
1115 if ( nsubs < ncand ) {
1116 int rc = mdb_dn2id_walk( op, &isc );
1119 /* We got to the end of a subtree. If there are any
1120 * alias scopes left, search them too.
1122 while (iscopes[0] && cscope < iscopes[0]) {
1124 isc.id = iscopes[cscope];
1126 mdb_entry_return( op, base );
1127 rs->sr_err = mdb_id2entry(op, mci, isc.id, &base);
1128 if ( !rs->sr_err ) {
1129 rc = mdb_id2name( op, ltid, &isc.mc, isc.id, &base->e_name, &base->e_nname,
1130 op->ors_scope == LDAP_SCOPE_SUBTREE ? iscopes : NULL );
1131 if ( rc == MDB_KEYEXIST ) {
1132 mdb_entry_return( op, base );
1137 if (isc.oscope == LDAP_SCOPE_ONELEVEL)
1138 isc.oscope = LDAP_SCOPE_BASE;
1139 rc = mdb_dn2id_walk( op, &isc );
1149 id = mdb_idl_next( candidates, &cursor );
1154 rs->sr_ctrls = NULL;
1155 rs->sr_ref = rs->sr_v2ref;
1156 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1157 rs->sr_rspoid = NULL;
1158 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
1159 send_paged_response( op, rs, NULL, 0 );
1161 send_ldap_result( op, rs );
1164 rs->sr_err = LDAP_SUCCESS;
1167 if ( cb.sc_private ) {
1168 /* remove our writewait callback */
1169 slap_callback **scp = &op->o_callback;
1171 if ( *scp == &cb ) {
1173 cb.sc_private = NULL;
1178 mdb_cursor_close( mcd );
1179 mdb_cursor_close( mci );
1180 if ( moi == &opinfo ) {
1181 mdb_txn_reset( moi->moi_txn );
1182 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
1186 if( rs->sr_v2ref ) {
1187 ber_bvarray_free( rs->sr_v2ref );
1188 rs->sr_v2ref = NULL;
1191 mdb_entry_return( op, base );
1192 scope_chunk_ret( op, scopes );
1198 static int base_candidate(
1203 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1204 e->e_nname.bv_val, (long) e->e_id, 0);
1211 /* Look for "objectClass Present" in this filter.
1212 * Also count depth of filter tree while we're at it.
1214 static int oc_filter(
1221 assert( f != NULL );
1223 if( cur > *max ) *max = cur;
1225 switch( f->f_choice ) {
1226 case LDAP_FILTER_PRESENT:
1227 if (f->f_desc == slap_schema.si_ad_objectClass) {
1232 case LDAP_FILTER_AND:
1233 case LDAP_FILTER_OR:
1235 for ( f=f->f_and; f; f=f->f_next ) {
1236 (void) oc_filter(f, cur, max);
1246 static void search_stack_free( void *key, void *data )
1248 ber_memfree_x(data, NULL);
1251 static void *search_stack( Operation *op )
1253 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1256 if ( op->o_threadctx ) {
1257 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
1260 ret = mdb->mi_search_stack;
1264 ret = ch_malloc( mdb->mi_search_stack_depth * MDB_IDL_UM_SIZE
1266 if ( op->o_threadctx ) {
1267 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1268 ret, search_stack_free, NULL, NULL );
1270 mdb->mi_search_stack = ret;
1276 static int search_candidates(
1286 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1288 Filter *f, rf, xf, nf, sf;
1289 AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1290 AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1293 * This routine takes as input a filter (user-filter)
1294 * and rewrites it as follows:
1295 * (&(scope=DN)[(objectClass=subentry)]
1296 * (|[(objectClass=referral)](user-filter))
1299 Debug(LDAP_DEBUG_TRACE,
1300 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1301 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1303 f = op->oq_search.rs_filter;
1305 /* If the user's filter uses objectClass=*,
1306 * these clauses are redundant.
1308 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1309 && !get_subentries_visibility(op)) {
1310 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1311 /* match referral objects */
1312 struct berval bv_ref = BER_BVC( "referral" );
1313 rf.f_choice = LDAP_FILTER_EQUALITY;
1315 rf.f_av_desc = slap_schema.si_ad_objectClass;
1316 rf.f_av_value = bv_ref;
1319 xf.f_choice = LDAP_FILTER_OR;
1326 if( get_subentries_visibility( op ) ) {
1327 struct berval bv_subentry = BER_BVC( "subentry" );
1328 sf.f_choice = LDAP_FILTER_EQUALITY;
1329 sf.f_ava = &aa_subentry;
1330 sf.f_av_desc = slap_schema.si_ad_objectClass;
1331 sf.f_av_value = bv_subentry;
1333 nf.f_choice = LDAP_FILTER_AND;
1340 /* Allocate IDL stack, plus 1 more for former tmp */
1341 if ( depth+1 > mdb->mi_search_stack_depth ) {
1342 stack = ch_malloc( (depth + 1) * MDB_IDL_UM_SIZE * sizeof( ID ) );
1345 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1346 rc = search_aliases( op, rs, e->e_id, txn, mci, scopes, stack );
1351 if ( rc == LDAP_SUCCESS ) {
1352 rc = mdb_filter_candidates( op, txn, f, ids,
1353 stack, stack+MDB_IDL_UM_SIZE );
1356 if ( depth+1 > mdb->mi_search_stack_depth ) {
1361 Debug(LDAP_DEBUG_TRACE,
1362 "mdb_search_candidates: failed (rc=%d)\n",
1366 Debug(LDAP_DEBUG_TRACE,
1367 "mdb_search_candidates: id=%ld first=%ld last=%ld\n",
1369 (long) MDB_IDL_FIRST(ids),
1370 (long) MDB_IDL_LAST(ids) );
1377 parse_paged_cookie( Operation *op, SlapReply *rs )
1379 int rc = LDAP_SUCCESS;
1380 PagedResultsState *ps = op->o_pagedresults_state;
1382 /* this function must be invoked only if the pagedResults
1383 * control has been detected, parsed and partially checked
1384 * by the frontend */
1385 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1387 /* cookie decoding/checks deferred to backend... */
1388 if ( ps->ps_cookieval.bv_len ) {
1389 PagedResultsCookie reqcookie;
1390 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1392 rs->sr_text = "paged results cookie is invalid";
1393 rc = LDAP_PROTOCOL_ERROR;
1397 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1399 if ( reqcookie > ps->ps_cookie ) {
1401 rs->sr_text = "paged results cookie is invalid";
1402 rc = LDAP_PROTOCOL_ERROR;
1405 } else if ( reqcookie < ps->ps_cookie ) {
1406 rs->sr_text = "paged results cookie is invalid or old";
1407 rc = LDAP_UNWILLING_TO_PERFORM;
1412 /* we're going to use ps_cookie */
1413 op->o_conn->c_pagedresults_state.ps_cookie = 0;
1422 send_paged_response(
1428 LDAPControl *ctrls[2];
1429 BerElementBuffer berbuf;
1430 BerElement *ber = (BerElement *)&berbuf;
1431 PagedResultsCookie respcookie;
1432 struct berval cookie;
1434 Debug(LDAP_DEBUG_ARGS,
1435 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1436 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1440 ber_init2( ber, NULL, LBER_USE_DER );
1443 respcookie = ( PagedResultsCookie )(*lastid);
1444 cookie.bv_len = sizeof( respcookie );
1445 cookie.bv_val = (char *)&respcookie;
1448 respcookie = ( PagedResultsCookie )0;
1449 BER_BVSTR( &cookie, "" );
1452 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1453 op->o_conn->c_pagedresults_state.ps_count =
1454 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1457 /* return size of 0 -- no estimate */
1458 ber_printf( ber, "{iO}", 0, &cookie );
1460 ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1461 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1465 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1466 ctrls[0]->ldctl_iscritical = 0;
1468 slap_add_ctrls( op, rs, ctrls );
1469 rs->sr_err = LDAP_SUCCESS;
1470 send_ldap_result( op, rs );
1473 (void) ber_free_buf( ber );