1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 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(
31 Operation *stackop, /* op with the current threadctx/slab cache */
32 Operation *sop, /* search op */
39 static int parse_paged_cookie( Operation *op, SlapReply *rs );
41 static void send_paged_response(
47 static int bdb_pfid_cmp( const void *v_id1, const void *v_id2 );
48 static ID* bdb_id_dup( Operation *op, ID *id );
50 /* Dereference aliases for a single alias entry. Return the final
51 * dereferenced entry on success, NULL on any failure.
53 static Entry * deref_base (
63 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
68 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
69 rs->sr_text = "maximum deref depth exceeded";
71 while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
72 /* Remember the last entry we looked at, so we can
77 /* If this is part of a subtree or onelevel search,
78 * have we seen this ID before? If so, quit.
80 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
85 /* If we've seen this ID during this deref iteration,
88 if ( bdb_idl_insert( tmp, e->e_id ) ) {
89 rs->sr_err = LDAP_ALIAS_PROBLEM;
90 rs->sr_text = "circular alias";
95 /* If there was a problem getting the aliasedObjectName,
96 * get_alias_dn will have set the error status.
98 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
103 rs->sr_err = bdb_dn2entry( op, NULL, &ndn, &ei,
113 rs->sr_err = LDAP_ALIAS_PROBLEM;
114 rs->sr_text = "aliasedObject not found";
118 /* Free the previous entry, continue to work with the
119 * one we just retrieved.
121 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
125 /* We found a regular entry. Return this to the caller. The
126 * entry is still locked for Read.
128 if (!is_entry_alias(e)) {
129 rs->sr_err = LDAP_SUCCESS;
137 /* Look for and dereference all aliases within the search scope. Adds
138 * the dereferenced entries to the "ids" list. Requires "stack" to be
139 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
140 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
142 static int search_aliases(
151 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
152 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
153 ID cursora, ida, cursoro, ido, *subscop2;
156 struct berval bv_alias = BER_BVC( "alias" );
157 AttributeAssertion aa_alias;
159 DB_LOCK locka, lockr;
162 aliases = stack; /* IDL of all aliases in the database */
163 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
164 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
165 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
166 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
167 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
168 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
170 /* A copy of subscop, because subscop gets clobbered by
171 * the bdb_idl_union/intersection routines
173 subscop2 = tmp + BDB_IDL_DB_SIZE;
175 af.f_choice = LDAP_FILTER_EQUALITY;
176 af.f_ava = &aa_alias;
177 af.f_av_desc = slap_schema.si_ad_objectClass;
178 af.f_av_value = bv_alias;
181 /* Find all aliases in database */
182 BDB_IDL_ZERO( aliases );
183 rs->sr_err = bdb_filter_candidates( op, &af, aliases,
185 if (rs->sr_err != LDAP_SUCCESS) {
189 oldsubs[1] = e->e_id;
192 BDB_IDL_ZERO( visited );
193 BDB_IDL_ZERO( newsubs );
196 ido = bdb_idl_first( oldsubs, &cursoro );
199 /* Set curscop to only the aliases in the current scope. Start with
200 * all the aliases, obtain the IDL for the current scope, and then
201 * get the intersection of these two IDLs. Add the current scope
202 * to the cumulative list of candidates.
204 BDB_IDL_CPY( curscop, aliases );
205 rs->sr_err = bdb_dn2idl( op, e, subscop,
206 subscop2+BDB_IDL_DB_SIZE );
210 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
212 BDB_IDL_CPY(subscop2, subscop);
213 rs->sr_err = bdb_idl_intersection(curscop, subscop);
214 bdb_idl_union( ids, subscop2 );
216 /* Dereference all of the aliases in the current scope. */
218 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
219 ida = bdb_idl_next(curscop, &cursora))
223 rs->sr_err = bdb_cache_find_id(op, NULL,
224 ida, &ei, 0, locker, &lockr );
225 if (rs->sr_err != LDAP_SUCCESS) {
226 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
227 rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
232 /* This should only happen if the curscop IDL has maxed out and
233 * turned into a range that spans IDs indiscriminately
235 if (!is_entry_alias(a)) {
236 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
241 /* Actually dereference the alias */
243 a = deref_base( op, rs, a, &matched, locker, &lockr,
246 /* If the target was not already in our current candidates,
247 * make note of it in the newsubs list. Also
248 * set it in the scopes list so that bdb_search
251 if (bdb_idl_insert(ids, a->e_id) == 0) {
252 bdb_idl_insert(newsubs, a->e_id);
253 bdb_idl_insert(scopes, a->e_id);
255 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
258 } else if (matched) {
259 /* Alias could not be dereferenced, or it deref'd to
260 * an ID we've already seen. Ignore it.
262 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
267 /* If this is a OneLevel search, we're done; oldsubs only had one
268 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
270 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
272 ido = bdb_idl_next( oldsubs, &cursoro );
274 /* If we're done processing the old scopes, did we add any new
275 * scopes in this iteration? If so, go back and do those now.
278 if (BDB_IDL_IS_ZERO(newsubs)) break;
279 BDB_IDL_CPY(oldsubs, newsubs);
280 BDB_IDL_ZERO(newsubs);
282 ido = bdb_idl_first( oldsubs, &cursoro );
285 /* Find the entry corresponding to the next scope. If it can't
286 * be found, ignore it and move on. This should never happen;
287 * we should never see the ID of an entry that doesn't exist.
288 * Set the name so that the scope's IDL can be retrieved.
292 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
294 if ( rs->sr_err != LDAP_SUCCESS ) {
295 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
296 rs->sr_err == DB_LOCK_NOTGRANTED )
305 #define is_sync_protocol(op) \
306 ((op)->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST)
308 #define IS_BDB_REPLACE(type) (( type == LDAP_PSEARCH_BY_DELETE ) || \
309 ( type == LDAP_PSEARCH_BY_SCOPEOUT ))
310 #define IS_PSEARCH (op != sop)
311 #define IS_POST_SEARCH ( op->ors_post_search_id != NOID )
314 bdb_drop_psearch( Operation *op, ber_int_t msgid )
317 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
319 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
320 if ( ps_list->o_connid == op->o_connid ) {
321 if ( ps_list->o_msgid == msgid ) {
322 ps_list->o_abandon = 1;
323 LDAP_LIST_REMOVE( ps_list, o_ps_link );
324 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
325 LDAP_STAILQ_REMOVE( &op->o_conn->c_ops, ps_list,
327 LDAP_STAILQ_NEXT( ps_list, o_next ) = NULL;
328 op->o_conn->c_n_ops_executing--;
329 op->o_conn->c_n_ops_completed++;
330 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
340 bdb_abandon( Operation *op, SlapReply *rs )
343 void *saved_tmpmemctx;
345 ps = bdb_drop_psearch( op, op->oq_abandon.rs_msgid );
347 saved_tmpmemctx = ps->o_tmpmemctx;
349 if (!BER_BVISNULL(&ps->o_req_dn)) {
350 slap_sl_free(ps->o_req_dn.bv_val, ps->o_tmpmemctx );
352 if (!BER_BVISNULL(&ps->o_req_ndn)) {
353 slap_sl_free(ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
355 if (!BER_BVISNULL(&ps->ors_filterstr)) {
356 ps->o_tmpfree(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
358 if (ps->ors_filter != NULL) {
359 filter_free_x(ps, ps->ors_filter);
361 if (ps->ors_attrs != NULL) {
362 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
367 if ( saved_tmpmemctx ) {
368 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
373 return LDAP_UNAVAILABLE;
377 bdb_cancel( Operation *op, SlapReply *rs )
380 void *saved_tmpmemctx;
382 ps = bdb_drop_psearch( op, op->oq_cancel.rs_msgid );
384 saved_tmpmemctx = ps->o_tmpmemctx;
386 rs->sr_err = LDAP_CANCELLED;
387 send_ldap_result( ps, rs );
389 if (!BER_BVISNULL(&ps->o_req_dn)) {
390 slap_sl_free(ps->o_req_dn.bv_val, ps->o_tmpmemctx );
392 if (!BER_BVISNULL(&ps->o_req_ndn)) {
393 slap_sl_free(ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
395 if (!BER_BVISNULL(&ps->ors_filterstr)) {
396 ps->o_tmpfree(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
398 if (ps->ors_filter != NULL) {
399 filter_free_x(ps, ps->ors_filter);
401 if (ps->ors_attrs != NULL) {
402 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
407 if ( saved_tmpmemctx ) {
408 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
413 return LDAP_UNAVAILABLE;
416 int bdb_search( Operation *op, SlapReply *rs )
419 struct pc_entry *pce = NULL;
420 struct pc_entry *tmp_pce = NULL;
424 ps_e.e_private = NULL;
425 ldap_pvt_thread_mutex_init( &op->o_pcmutex );
426 LDAP_TAILQ_INIT( &op->o_ps_pre_candidates );
427 LDAP_TAILQ_INIT( &op->o_ps_post_candidates );
429 op->ors_post_search_id = NOID;
430 rc = bdb_do_search( op, rs, op, NULL, 0 );
432 ldap_pvt_thread_mutex_lock( &op->o_pcmutex );
433 pce = LDAP_TAILQ_FIRST( &op->o_ps_post_candidates );
434 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
436 while ( rc == LDAP_SUCCESS && pce &&
437 op->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
439 ps_e.e_id = op->ors_post_search_id = pce->pc_id;
440 if ( op->o_sync_csn.bv_val ) {
441 ch_free( op->o_sync_csn.bv_val );
442 op->o_sync_csn.bv_val = NULL;
444 ber_dupbv( &op->o_sync_csn, &pce->pc_csn );
445 ber_dupbv( &ps_e.e_name, &pce->pc_ename );
446 ber_dupbv( &ps_e.e_nname, &pce->pc_enname );
447 a = ch_calloc( 1, sizeof( Attribute ));
448 a->a_desc = slap_schema.si_ad_entryUUID;
449 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
450 ber_dupbv( &a->a_vals[0], &pce->pc_entryUUID );
451 a->a_nvals = a->a_vals;
455 rc = bdb_do_search( op, rs, op, &ps_e, 0 );
458 ldap_pvt_thread_mutex_lock( &op->o_pcmutex );
459 pce = LDAP_TAILQ_NEXT( pce, pc_link );
460 LDAP_TAILQ_REMOVE( &op->o_ps_post_candidates, tmp_pce, pc_link );
461 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
463 ch_free( tmp_pce->pc_csn.bv_val );
464 ch_free( tmp_pce->pc_entryUUID.bv_val );
465 ch_free( tmp_pce->pc_ename.bv_val );
466 ch_free( tmp_pce->pc_enname.bv_val );
468 entry_clean( &ps_e );
473 #define BDB_PSEARCH_MAX_WAIT 3
474 int bdb_psearch( Operation *op, SlapReply *rs, Operation *sop,
475 Entry *ps_e, int ps_type )
478 struct pc_entry *pce = NULL;
479 struct pc_entry *p = NULL;
482 op->ors_post_search_id = NOID;
485 case LDAP_PSEARCH_BY_PREMODIFY:
486 case LDAP_PSEARCH_BY_PREDELETE:
488 if ( !op->o_ps_send_wait ) {
489 if ( sop->o_refresh_in_progress ) {
490 pce = (struct pc_entry *) ch_calloc(
491 1, sizeof( struct pc_entry ));
492 pce->pc_id = ps_e->e_id;
493 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
494 if ( LDAP_TAILQ_EMPTY( &sop->o_ps_pre_candidates )) {
495 LDAP_TAILQ_INSERT_HEAD(
496 &sop->o_ps_pre_candidates, pce, pc_link );
498 LDAP_TAILQ_FOREACH( p,
499 &sop->o_ps_pre_candidates, pc_link ) {
500 if ( p->pc_id > pce->pc_id )
505 LDAP_TAILQ_INSERT_BEFORE( p, pce, pc_link );
507 LDAP_TAILQ_INSERT_TAIL(
508 &sop->o_ps_pre_candidates,
512 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
514 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
518 pce = op->o_ps_send_wait;
521 /* Wait until refresh search send the entry */
522 while ( !pce->pc_sent ) {
523 if ( sop->o_refresh_in_progress ) {
524 if ( num_retries == BDB_PSEARCH_MAX_WAIT ) {
525 op->o_ps_send_wait = pce;
528 ldap_pvt_thread_yield();
529 bdb_trans_backoff( ++num_retries );
535 op->o_ps_send_wait = NULL;
537 if ( !sop->o_refresh_in_progress && !pce->pc_sent ) {
538 /* refresh ended without processing pce */
539 /* need to perform psearch for ps_e */
540 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
541 LDAP_TAILQ_REMOVE( &sop->o_ps_pre_candidates, pce, pc_link );
542 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
544 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
547 /* the pce entry was sent in the refresh phase */
548 if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
549 struct psid_entry* psid_e;
550 psid_e = (struct psid_entry *) ch_calloc(1,
551 sizeof(struct psid_entry));
553 LDAP_LIST_INSERT_HEAD( &op->o_pm_list, psid_e, ps_link );
556 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
557 LDAP_TAILQ_REMOVE( &sop->o_ps_pre_candidates, pce, pc_link );
558 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
563 case LDAP_PSEARCH_BY_DELETE:
564 case LDAP_PSEARCH_BY_SCOPEOUT:
565 case LDAP_PSEARCH_BY_ADD:
566 case LDAP_PSEARCH_BY_MODIFY:
567 ldap_pvt_thread_mutex_lock( &op->o_pcmutex );
568 if ( sop->o_refresh_in_progress ||
569 !LDAP_TAILQ_EMPTY( &sop->o_ps_post_candidates )) {
570 pce = (struct pc_entry *) ch_calloc( 1, sizeof( struct pc_entry ));
571 pce->pc_id = ps_e->e_id;
572 ber_dupbv( &pce->pc_csn, &op->o_sync_csn );
573 if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
575 for ( a = ps_e->e_attrs; a != NULL; a = a->a_next ) {
576 AttributeDescription *desc = a->a_desc;
577 if ( desc == slap_schema.si_ad_entryUUID ) {
578 ber_dupbv( &pce->pc_entryUUID, &a->a_nvals[0] );
582 ber_dupbv( &pce->pc_ename, &ps_e->e_name );
583 ber_dupbv( &pce->pc_enname, &ps_e->e_nname );
584 LDAP_TAILQ_INSERT_TAIL( &sop->o_ps_post_candidates, pce, pc_link );
585 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
587 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
588 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
593 Debug( LDAP_DEBUG_TRACE, "do_psearch: invalid psearch type\n",
599 /* For persistent searches, op is the currently executing operation,
600 * sop is the persistent search. For regular searches, sop = op.
603 bdb_do_search( Operation *op, SlapReply *rs, Operation *sop,
604 Entry *ps_e, int ps_type )
606 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
609 ID candidates[BDB_IDL_UM_SIZE];
610 ID scopes[BDB_IDL_DB_SIZE];
611 Entry *e = NULL, base, e_root = {0};
612 Entry *matched = NULL;
613 EntryInfo *ei, ei_root = {0};
614 struct berval realbase = BER_BVNULL;
618 AttributeName *attrs;
620 Filter contextcsnand, contextcsnle, cookief, csnfnot,
621 csnfeq, csnfand, csnfge;
622 AttributeAssertion aa_ge, aa_eq, aa_le;
623 struct berval *search_context_csn = NULL;
625 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
627 AttributeName uuid_attr[2];
629 int entry_sync_state = -1;
630 AttributeName null_attr;
631 int no_sync_state_change = 0;
633 u_int32_t locker = 0;
637 int sync_send_present_mode = 1;
643 struct pc_entry *pce = NULL;
644 BerVarray syncUUID_set = NULL;
645 int syncUUID_set_cnt = 0;
647 struct bdb_op_info *opinfo = NULL;
650 Debug( LDAP_DEBUG_TRACE, "=> bdb_search\n", 0, 0, 0);
651 attrs = sop->oq_search.rs_attrs;
653 opinfo = (struct bdb_op_info *) op->o_private;
655 if ( !IS_POST_SEARCH && !IS_PSEARCH &&
656 sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
657 struct slap_session_entry *sent;
658 if ( sop->o_sync_state.sid >= 0 ) {
659 LDAP_LIST_FOREACH( sent, &bdb->bi_session_list, se_link ) {
660 if ( sent->se_id == sop->o_sync_state.sid ) {
661 sop->o_sync_slog_size = sent->se_size;
668 /* psearch needs to be registered before refresh begins */
669 if ( !IS_POST_SEARCH && !IS_PSEARCH &&
670 sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
671 sop->o_refresh_in_progress = 1;
672 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
673 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
674 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
676 } else if ( !IS_POST_SEARCH && !IS_PSEARCH &&
677 sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST
678 && sop->o_sync_slog_size >= 0 )
680 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
681 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
682 if ( ps_list->o_sync_slog_size >= 0 ) {
683 if ( ps_list->o_sync_state.sid == sop->o_sync_state.sid ) {
691 if ( ps_list->o_sync_slog_omitcsn.bv_len != 0 ) {
692 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
693 if ( sop->o_sync_state.ctxcsn &&
694 sop->o_sync_state.ctxcsn->bv_val != NULL )
696 value_match( &match, slap_schema.si_ad_entryCSN, mr,
697 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
698 sop->o_sync_state.ctxcsn,
699 &ps_list->o_sync_slog_omitcsn,
705 sync_send_present_mode = 0;
708 sync_send_present_mode = 0;
710 } else if ( sop->o_sync_slog_size >= 0 ) {
711 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
713 sop->o_sync_state.sid = -1;
715 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
718 null_attr.an_desc = NULL;
719 null_attr.an_oc = NULL;
720 null_attr.an_oc_exclude = 0;
721 BER_BVZERO( &null_attr.an_name );
723 for( num_ctrls = 0; num_ctrls < SLAP_MAX_RESPONSE_CONTROLS; num_ctrls++ ) {
724 ctrls[num_ctrls] = NULL;
728 if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
730 attrs[0].an_desc = NULL;
731 attrs[0].an_oc = NULL;
732 attrs[0].an_oc_exclude = 0;
733 BER_BVZERO( &attrs[0].an_name );
736 manageDSAit = get_manageDSAit( sop );
738 /* Sync control overrides manageDSAit */
739 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
740 if ( manageDSAit == SLAP_NO_CONTROL ) {
741 manageDSAit = SLAP_CRITICAL_CONTROL;
743 } else if ( IS_PSEARCH ) {
744 if ( manageDSAit == SLAP_NO_CONTROL ) {
745 manageDSAit = SLAP_CRITICAL_CONTROL;
749 if ( opinfo && opinfo->boi_txn ) {
750 ltid = opinfo->boi_txn;
751 locker = TXN_ID( ltid );
753 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
759 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
764 if ( IS_POST_SEARCH ) {
767 candidates[1] = op->ors_post_search_id;
768 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );
772 if ( sop->o_req_ndn.bv_len == 0 ) {
773 /* DIT root special case */
774 ei_root.bei_e = &e_root;
775 ei_root.bei_parent = &ei_root;
776 e_root.e_private = &ei_root;
778 BER_BVSTR( &e_root.e_nname, "" );
779 BER_BVSTR( &e_root.e_name, "" );
781 rs->sr_err = LDAP_SUCCESS;
784 /* get entry with reader lock */
785 rs->sr_err = bdb_dn2entry( op, ltid, &sop->o_req_ndn, &ei,
797 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
799 LOCK_ID_FREE (bdb->bi_dbenv, locker );
801 case DB_LOCK_DEADLOCK:
802 case DB_LOCK_NOTGRANTED:
805 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
807 LOCK_ID_FREE (bdb->bi_dbenv, locker );
811 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
812 BDB_IDL_ZERO(candidates);
813 e = deref_base( op, rs, e, &matched, locker, &lock,
818 struct berval matched_dn = BER_BVNULL;
820 if ( matched != NULL ) {
822 ber_dupbv( &matched_dn, &matched->e_name );
824 erefs = is_entry_referral( matched )
825 ? get_entry_referrals( op, matched )
828 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
833 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
834 &sop->o_req_dn, sop->oq_search.rs_scope );
835 ber_bvarray_free( erefs );
839 rs->sr_ref = referral_rewrite( default_referral,
840 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
843 rs->sr_err = LDAP_REFERRAL;
844 rs->sr_matched = matched_dn.bv_val;
845 send_ldap_result( sop, rs );
848 LOCK_ID_FREE (bdb->bi_dbenv, locker );
850 ber_bvarray_free( rs->sr_ref );
853 if ( matched_dn.bv_val ) {
854 ber_memfree( matched_dn.bv_val );
855 rs->sr_matched = NULL;
860 if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
861 /* entry is a referral, don't allow add */
862 struct berval matched_dn;
865 ber_dupbv( &matched_dn, &e->e_name );
866 erefs = get_entry_referrals( op, e );
868 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
872 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
873 &sop->o_req_dn, sop->oq_search.rs_scope );
874 ber_bvarray_free( erefs );
877 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
880 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
881 rs->sr_err = LDAP_REFERRAL;
882 rs->sr_matched = matched_dn.bv_val;
883 send_ldap_result( sop, rs );
886 LOCK_ID_FREE (bdb->bi_dbenv, locker );
887 ber_bvarray_free( rs->sr_ref );
889 ber_memfree( matched_dn.bv_val );
890 rs->sr_matched = NULL;
894 if ( get_assert( op ) &&
895 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
897 rs->sr_err = LDAP_ASSERTION_FAILED;
898 send_ldap_result( sop, rs );
902 /* compute it anyway; root does not use it */
903 stoptime = op->o_time + sop->ors_tlimit;
905 /* need normalized dn below */
906 ber_dupbv( &realbase, &e->e_nname );
908 /* Copy info to base, must free entry before accessing the database
909 * in search_candidates, to avoid deadlocks.
911 base.e_private = e->e_private;
912 base.e_nname = realbase;
915 if ( e != &e_root ) {
916 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
921 rs->sr_err = bdb_get_commit_csn( sop, rs, &search_context_csn,
922 locker, &ctxcsn_lock );
924 if ( rs->sr_err != LDAP_SUCCESS ) {
925 send_ldap_error( sop, rs, rs->sr_err,
926 "error in csn management in search" );
930 if ( sop->o_sync_mode != SLAP_SYNC_NONE &&
931 sop->o_sync_state.ctxcsn &&
932 sop->o_sync_state.ctxcsn->bv_val &&
933 ber_bvcmp( &sop->o_sync_state.ctxcsn[0], search_context_csn ) == 0 )
935 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
939 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );
942 /* select candidates */
943 if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
944 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
947 BDB_IDL_ZERO( candidates );
948 BDB_IDL_ZERO( scopes );
949 rs->sr_err = search_candidates( op, sop, rs, &base,
950 locker, candidates, scopes );
953 if ( !IS_PSEARCH && sop->o_sync_mode != SLAP_SYNC_NONE ) {
954 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
957 /* start cursor at beginning of candidates.
961 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
962 cursor = bdb_idl_search( candidates, ps_e->e_id );
963 if ( candidates[cursor] != ps_e->e_id ) {
964 rs->sr_err = LDAP_SUCCESS;
967 } else if ( ps_e->e_id < BDB_IDL_RANGE_FIRST( candidates ) ||
968 ps_e->e_id > BDB_IDL_RANGE_LAST( candidates ))
970 rs->sr_err = LDAP_SUCCESS;
974 candidates[1] = ps_e->e_id;
977 if ( candidates[0] == 0 ) {
978 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
984 /* if not root and candidates exceed to-be-checked entries, abort */
985 if ( sop->ors_limit /* isroot == FALSE */ &&
986 sop->ors_limit->lms_s_unchecked != -1 &&
987 BDB_IDL_N(candidates) > (unsigned) sop->ors_limit->lms_s_unchecked )
989 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
990 send_ldap_result( sop, rs );
991 rs->sr_err = LDAP_SUCCESS;
995 if ( sop->ors_limit == NULL /* isroot == TRUE */ ||
996 !sop->ors_limit->lms_s_pr_hide )
998 tentries = BDB_IDL_N(candidates);
1001 if ( get_pagedresults( sop ) > SLAP_NO_CONTROL ) {
1002 /* deferred cookie parsing */
1003 rs->sr_err = parse_paged_cookie( sop, rs );
1004 if ( rs->sr_err != LDAP_SUCCESS ) {
1005 send_ldap_result( sop, rs );
1009 if ( (ID)( sop->o_pagedresults_state.ps_cookie ) == 0 ) {
1010 id = bdb_idl_first( candidates, &cursor );
1013 if ( sop->o_pagedresults_size == 0 ) {
1014 rs->sr_err = LDAP_SUCCESS;
1015 rs->sr_text = "search abandoned by pagedResult size=0";
1016 send_ldap_result( sop, rs );
1019 for ( id = bdb_idl_first( candidates, &cursor );
1021 id <= (ID)( sop->o_pagedresults_state.ps_cookie );
1022 id = bdb_idl_next( candidates, &cursor ) )
1028 if ( cursor == NOID ) {
1029 Debug( LDAP_DEBUG_TRACE,
1030 "bdb_search: no paged results candidates\n",
1032 send_paged_response( sop, rs, &lastid, 0 );
1034 rs->sr_err = LDAP_OTHER;
1040 if (( sop->o_sync_mode & SLAP_SYNC_REFRESH ) || IS_PSEARCH ) {
1043 cookief.f_choice = LDAP_FILTER_AND;
1044 cookief.f_and = &csnfnot;
1045 cookief.f_next = NULL;
1047 csnfnot.f_choice = LDAP_FILTER_NOT;
1048 csnfnot.f_not = &csnfeq;
1049 csnfnot.f_next = &csnfand;
1051 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
1052 csnfeq.f_ava = &aa_eq;
1053 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
1054 if ( sop->o_sync_state.ctxcsn != NULL ) {
1055 csnfeq.f_av_value = *sop->o_sync_state.ctxcsn;
1057 csnfeq.f_av_value = slap_empty_bv;
1060 csnfand.f_choice = LDAP_FILTER_AND;
1061 csnfand.f_and = &csnfge;
1062 csnfand.f_next = NULL;
1064 csnfge.f_choice = LDAP_FILTER_GE;
1065 csnfge.f_ava = &aa_ge;
1066 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
1067 if ( sop->o_sync_state.ctxcsn != NULL ) {
1068 csnfge.f_av_value = *sop->o_sync_state.ctxcsn;
1070 csnfge.f_av_value = slap_empty_bv;
1073 if ( search_context_csn && !IS_PSEARCH ) {
1074 csnfge.f_next = &contextcsnand;
1076 contextcsnand.f_choice = LDAP_FILTER_AND;
1077 contextcsnand.f_and = &contextcsnle;
1078 contextcsnand.f_next = NULL;
1080 contextcsnle.f_choice = LDAP_FILTER_LE;
1081 contextcsnle.f_ava = &aa_le;
1082 contextcsnle.f_av_desc = slap_schema.si_ad_entryCSN;
1083 contextcsnle.f_av_value = *search_context_csn;
1084 contextcsnle.f_next = sop->oq_search.rs_filter;
1086 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
1087 if ( sop->o_sync_state.ctxcsn &&
1088 sop->o_sync_state.ctxcsn->bv_val != NULL )
1090 value_match( &match, slap_schema.si_ad_entryCSN, mr,
1091 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1092 &sop->o_sync_state.ctxcsn[0], search_context_csn,
1097 no_sync_state_change = ( match >= 0 );
1099 csnfge.f_next = sop->oq_search.rs_filter;
1105 for ( id = bdb_idl_first( candidates, &cursor );
1106 id != NOID && !no_sync_state_change;
1107 id = bdb_idl_next( candidates, &cursor ) )
1114 if ( !IS_POST_SEARCH ) {
1115 idhole = (ID*) avl_find( sop->o_psearch_finished,
1116 (caddr_t)&id, bdb_pfid_cmp );
1118 avl_delete( &sop->o_psearch_finished,
1119 (caddr_t)idhole, bdb_pfid_cmp );
1120 sop->o_tmpfree( idhole, sop->o_tmpmemctx );
1124 if ( sop->o_refresh_in_progress ) {
1125 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
1126 pce = LDAP_TAILQ_FIRST( &sop->o_ps_pre_candidates );
1127 while ( pce && pce->pc_sent ) {
1128 pce = LDAP_TAILQ_NEXT( pce, pc_link );
1130 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
1133 if ( BDB_IDL_IS_RANGE( candidates ) ) {
1134 if ( pce->pc_id >= candidates[1] &&
1135 pce->pc_id <= candidates[2] &&
1136 pce->pc_id > cursor-1 ) {
1139 avl_insert( &sop->o_psearch_finished,
1140 (caddr_t)bdb_id_dup( sop, &pce->pc_id ),
1141 bdb_pfid_cmp, avl_dup_error );
1146 pos = bdb_idl_search(candidates, pce->pc_id);
1147 if ( pos > cursor-1 && pos <= candidates[0] ) {
1150 avl_insert( &sop->o_psearch_finished,
1151 (caddr_t)bdb_id_dup( sop, &pce->pc_id ),
1152 bdb_pfid_cmp, avl_dup_error );
1161 /* check for abandon */
1162 if ( sop->o_abandon ) {
1164 bdb_drop_psearch( sop, sop->o_msgid );
1166 rs->sr_err = LDAP_SUCCESS;
1170 if ( sop->o_cancel ) {
1171 assert( sop->o_cancel == SLAP_CANCEL_REQ );
1172 rs->sr_err = LDAP_CANCELLED;
1173 send_ldap_result( sop, rs );
1174 sop->o_cancel = SLAP_CANCEL_ACK;
1175 rs->sr_err = LDAP_SUCCESS;
1179 /* check time limit */
1180 if ( sop->ors_tlimit != SLAP_NO_LIMIT
1181 && slap_get_time() > stoptime )
1183 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1184 rs->sr_ref = rs->sr_v2ref;
1185 send_ldap_result( sop, rs );
1186 rs->sr_err = LDAP_SUCCESS;
1192 /* get the entry with reader lock */
1194 rs->sr_err = bdb_cache_find_id( op, ltid,
1195 id, &ei, 0, locker, &lock );
1197 if (rs->sr_err == LDAP_BUSY) {
1198 rs->sr_text = "ldap server busy";
1199 send_ldap_result( sop, rs );
1202 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
1203 || rs->sr_err == DB_LOCK_NOTGRANTED )
1205 goto id2entry_retry;
1208 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
1215 if ( IS_POST_SEARCH ) {
1216 /* send LDAP_SYNC_DELETE */
1217 rs->sr_entry = e = ps_e;
1218 goto post_search_no_entry;
1219 } else if( !BDB_IDL_IS_RANGE(candidates) ) {
1220 /* only complain for non-range IDLs */
1221 Debug( LDAP_DEBUG_TRACE,
1222 "bdb_search: candidate %ld not found\n",
1234 #ifdef BDB_SUBENTRIES
1235 /* FIXME: send all but syncrepl */
1237 if ( !is_sync_protocol( sop ) )
1240 if ( is_entry_subentry( e ) ) {
1241 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
1242 if(!get_subentries_visibility( sop )) {
1243 /* only subentries are visible */
1247 } else if ( get_subentries( sop ) &&
1248 !get_subentries_visibility( sop ))
1250 /* only subentries are visible */
1254 } else if ( get_subentries_visibility( sop )) {
1255 /* only subentries are visible */
1259 #endif /* BDB_SUBENTRIES */
1261 /* Does this candidate actually satisfy the search scope?
1263 * Note that we don't lock access to the bei_parent pointer.
1264 * Since only leaf nodes can be deleted, the parent of any
1265 * node will always be a valid node. Also since we have
1266 * a Read lock on the data, it cannot be renamed out of the
1267 * scope while we are looking at it, and unless we're using
1268 * BDB_HIER, its parents cannot be moved either.
1270 switch( sop->ors_scope ) {
1271 case LDAP_SCOPE_BASE:
1272 /* This is always true, yes? */
1273 if ( id == base.e_id ) scopeok = 1;
1276 case LDAP_SCOPE_ONELEVEL:
1277 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
1280 #ifdef LDAP_SCOPE_CHILDREN
1281 case LDAP_SCOPE_CHILDREN:
1282 if ( id == base.e_id ) break;
1285 case LDAP_SCOPE_SUBTREE: {
1287 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
1288 if ( tmp->bei_id == base.e_id ) {
1296 /* aliases were already dereferenced in candidate list */
1297 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
1298 /* but if the search base is an alias, and we didn't
1299 * deref it when finding, return it.
1301 if ( is_entry_alias(e) &&
1302 ((sop->ors_deref & LDAP_DEREF_FINDING) ||
1303 !bvmatch(&e->e_nname, &op->o_req_ndn)))
1308 /* scopes is only non-empty for onelevel or subtree */
1309 if ( !scopeok && BDB_IDL_N(scopes) ) {
1311 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
1312 x = bdb_idl_search( scopes, e->e_id );
1313 if ( scopes[x] == e->e_id ) scopeok = 1;
1315 /* subtree, walk up the tree */
1316 EntryInfo *tmp = BEI(e);
1317 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
1318 x = bdb_idl_search( scopes, tmp->bei_id );
1319 if ( scopes[x] == tmp->bei_id ) {
1328 /* Not in scope, ignore it */
1329 if ( !IS_POST_SEARCH && !scopeok ) {
1330 Debug( LDAP_DEBUG_TRACE,
1331 "bdb_search: %ld scope not okay\n",
1337 * if it's a referral, add it to the list of referrals. only do
1338 * this for non-base searches, and don't check the filter
1339 * explicitly here since it's only a candidate anyway.
1341 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1342 && is_entry_referral( e ) )
1344 BerVarray erefs = get_entry_referrals( sop, e );
1345 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1346 sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1347 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1349 send_search_reference( sop, rs );
1351 ber_bvarray_free( rs->sr_ref );
1352 ber_bvarray_free( erefs );
1358 if ( !manageDSAit && is_entry_glue( e )) {
1362 /* if it matches the filter and scope, send it */
1364 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1365 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1367 rs->sr_err = LDAP_COMPARE_TRUE;
1371 if ( !IS_POST_SEARCH ) {
1372 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1373 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1374 rs->sr_err = test_filter( sop, rs->sr_entry,
1376 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1377 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1378 if ( no_sync_state_change ) {
1379 Debug( LDAP_DEBUG_TRACE,
1381 "error in context csn management\n",
1384 entry_sync_state = LDAP_SYNC_ADD;
1387 if ( no_sync_state_change ) {
1390 entry_sync_state = LDAP_SYNC_PRESENT;
1394 rs->sr_err = test_filter( sop,
1395 rs->sr_entry, sop->oq_search.rs_filter );
1399 rs->sr_err = test_filter( sop,
1400 rs->sr_entry, sop->oq_search.rs_filter );
1402 rs->sr_err = LDAP_COMPARE_TRUE;
1407 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1408 /* check size limit */
1409 if ( --sop->ors_slimit == -1 &&
1410 sop->o_sync_slog_size == -1 )
1413 bdb_cache_return_entry_r( bdb->bi_dbenv,
1414 &bdb->bi_cache, e, &lock );
1417 rs->sr_entry = NULL;
1418 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1419 rs->sr_ref = rs->sr_v2ref;
1420 send_ldap_result( sop, rs );
1421 rs->sr_err = LDAP_SUCCESS;
1425 if ( get_pagedresults(sop) > SLAP_NO_CONTROL ) {
1426 if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
1427 send_paged_response( sop, rs, &lastid, tentries );
1437 if (IS_PSEARCH || IS_POST_SEARCH) {
1438 int premodify_found = 0;
1440 if ( IS_POST_SEARCH ||
1441 ps_type == LDAP_PSEARCH_BY_ADD ||
1442 ps_type == LDAP_PSEARCH_BY_DELETE ||
1443 ps_type == LDAP_PSEARCH_BY_MODIFY ||
1444 ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1446 if ( !IS_POST_SEARCH &&
1447 ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1448 struct psid_entry* psid_e;
1449 LDAP_LIST_FOREACH( psid_e,
1450 &op->o_pm_list, ps_link)
1452 if( psid_e->ps_op == sop ) {
1453 premodify_found = 1;
1454 LDAP_LIST_REMOVE(psid_e, ps_link);
1458 if (psid_e != NULL) free (psid_e);
1461 if ( IS_POST_SEARCH ) {
1463 entry_sync_state = LDAP_SYNC_ADD;
1465 post_search_no_entry:
1466 entry_sync_state = LDAP_SYNC_DELETE;
1468 } else if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1469 entry_sync_state = LDAP_SYNC_ADD;
1470 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1471 entry_sync_state = LDAP_SYNC_DELETE;
1472 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1473 if ( premodify_found ) {
1474 entry_sync_state = LDAP_SYNC_MODIFY;
1476 entry_sync_state = LDAP_SYNC_ADD;
1478 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT ) {
1479 entry_sync_state = LDAP_SYNC_DELETE;
1481 rs->sr_err = LDAP_OTHER;
1485 if ( sop->o_sync_slog_size != -1 ) {
1486 if ( entry_sync_state == LDAP_SYNC_DELETE ) {
1487 result = slap_add_session_log( op, sop, e );
1492 struct berval cookie;
1493 slap_compose_sync_cookie( sop, &cookie,
1495 sop->o_sync_state.sid,
1496 sop->o_sync_state.rid );
1497 rs->sr_err = slap_build_sync_state_ctrl(
1498 sop, rs, e, entry_sync_state, ctrls,
1499 num_ctrls++, 1, &cookie );
1500 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1501 if (!(IS_POST_SEARCH &&
1502 entry_sync_state == LDAP_SYNC_DELETE)) {
1503 rs->sr_attrs = attrs;
1505 rs->sr_attrs = NULL;
1507 rs->sr_operational_attrs = NULL;
1508 rs->sr_ctrls = ctrls;
1510 result = send_search_entry( sop, rs );
1511 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1513 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1515 slap_sl_free( ctrls[--num_ctrls],
1517 ctrls[num_ctrls] = NULL;
1518 rs->sr_ctrls = NULL;
1521 } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1522 struct psid_entry* psid_e;
1523 psid_e = (struct psid_entry *) ch_calloc(1,
1524 sizeof(struct psid_entry));
1525 psid_e->ps_op = sop;
1526 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1530 Debug( LDAP_DEBUG_TRACE,
1531 "bdb_search: invalid ps_type (%d) \n",
1536 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1537 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1538 rs->sr_err = slap_build_sync_state_ctrl(
1539 sop, rs, e, entry_sync_state, ctrls,
1540 num_ctrls++, 0, NULL );
1541 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1542 rs->sr_ctrls = ctrls;
1543 rs->sr_attrs = sop->oq_search.rs_attrs;
1544 rs->sr_operational_attrs = NULL;
1546 result = send_search_entry( sop, rs );
1548 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1550 slap_sl_free( ctrls[--num_ctrls],
1552 ctrls[num_ctrls] = NULL;
1553 rs->sr_ctrls = NULL;
1555 } else { /* PRESENT */
1556 if ( sync_send_present_mode ) {
1557 result = slap_build_syncUUID_set( sop,
1559 if ( result <= 0 ) {
1563 if ( syncUUID_set_cnt ==
1564 SLAP_SYNCUUID_SET_SIZE )
1566 rs->sr_err = LDAP_SUCCESS;
1567 rs->sr_rspoid = LDAP_SYNC_INFO;
1568 rs->sr_ctrls = NULL;
1569 result = slap_send_syncinfo( sop, rs,
1570 LDAP_TAG_SYNC_ID_SET,
1571 NULL, 0, syncUUID_set, 0 );
1572 if ( result != LDAP_SUCCESS ) {
1575 ber_bvarray_free_x( syncUUID_set,
1577 syncUUID_set = NULL;
1578 syncUUID_set_cnt = 0;
1588 rs->sr_attrs = sop->oq_search.rs_attrs;
1589 rs->sr_operational_attrs = NULL;
1590 rs->sr_ctrls = NULL;
1592 rs->sr_err = LDAP_SUCCESS;
1593 result = send_search_entry( sop, rs );
1598 case 0: /* entry sent ok */
1600 case 1: /* entry not sent */
1602 case -1: /* connection closed */
1604 bdb_cache_return_entry_r(bdb->bi_dbenv,
1605 &bdb->bi_cache, e, &lock);
1608 rs->sr_entry = NULL;
1609 rs->sr_err = LDAP_OTHER;
1615 Debug( LDAP_DEBUG_TRACE,
1616 "bdb_search: %ld does not match filter\n",
1622 /* free reader lock */
1624 if (!(IS_POST_SEARCH &&
1625 entry_sync_state == LDAP_SYNC_DELETE)) {
1626 bdb_cache_return_entry_r( bdb->bi_dbenv,
1627 &bdb->bi_cache, e , &lock );
1628 if ( sop->o_nocaching ) {
1629 bdb_cache_delete_entry( bdb, ei, locker, &lock );
1634 rs->sr_entry = NULL;
1637 if ( sop->o_refresh_in_progress ) {
1643 ldap_pvt_thread_yield();
1646 if ( syncUUID_set_cnt > 0 ) {
1647 rs->sr_err = LDAP_SUCCESS;
1648 rs->sr_rspoid = LDAP_SYNC_INFO;
1649 rs->sr_ctrls = NULL;
1650 slap_send_syncinfo( sop, rs, LDAP_TAG_SYNC_ID_SET,
1651 NULL, 0, syncUUID_set, 0 );
1652 ber_bvarray_free_x( syncUUID_set, sop->o_tmpmemctx );
1653 syncUUID_set_cnt = 0;
1657 if (!IS_PSEARCH && !IS_POST_SEARCH) {
1658 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1659 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1660 struct berval cookie;
1661 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1662 sop->o_sync_state.sid, sop->o_sync_state.rid );
1664 if ( sync_send_present_mode ) {
1665 rs->sr_err = LDAP_SUCCESS;
1666 rs->sr_rspoid = LDAP_SYNC_INFO;
1667 rs->sr_ctrls = NULL;
1668 slap_send_syncinfo( sop, rs,
1669 LDAP_TAG_SYNC_REFRESH_PRESENT, &cookie, 1, NULL, 0 );
1672 if ( !no_sync_state_change ) {
1674 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1675 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1678 if ( ps_list->o_sync_slog_size > 0 ) {
1679 if ( ps_list->o_sync_state.sid ==
1680 sop->o_sync_state.sid )
1689 rs->sr_err = LDAP_SUCCESS;
1690 rs->sr_rspoid = NULL;
1691 rs->sr_ctrls = NULL;
1692 slap_send_session_log( op, ps_list, rs );
1694 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1697 rs->sr_err = LDAP_SUCCESS;
1698 rs->sr_rspoid = LDAP_SYNC_INFO;
1699 rs->sr_ctrls = NULL;
1700 slap_send_syncinfo( sop, rs,
1701 LDAP_TAG_SYNC_REFRESH_DELETE, &cookie, 1, NULL, 0 );
1704 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1707 /* refreshOnly mode */
1708 struct berval cookie;
1709 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1710 sop->o_sync_state.sid, sop->o_sync_state.rid );
1712 if ( sync_send_present_mode ) {
1713 slap_build_sync_done_ctrl( sop, rs, ctrls,
1714 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1717 if ( !no_sync_state_change ) {
1719 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1720 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1723 if ( ps_list->o_sync_slog_size > 0 ) {
1724 if ( ps_list->o_sync_state.sid ==
1725 sop->o_sync_state.sid ) {
1733 slap_send_session_log( op, ps_list, rs );
1735 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1738 slap_build_sync_done_ctrl( sop, rs, ctrls,
1739 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_DELETES );
1742 rs->sr_ctrls = ctrls;
1743 rs->sr_ref = rs->sr_v2ref;
1744 rs->sr_err = (rs->sr_v2ref == NULL)
1745 ? LDAP_SUCCESS : LDAP_REFERRAL;
1746 rs->sr_rspoid = NULL;
1747 send_ldap_result( sop, rs );
1748 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1749 slap_sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val,
1752 slap_sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1753 ctrls[num_ctrls] = NULL;
1754 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1758 rs->sr_ctrls = NULL;
1759 rs->sr_ref = rs->sr_v2ref;
1760 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1761 rs->sr_rspoid = NULL;
1762 if ( get_pagedresults(sop) > SLAP_NO_CONTROL ) {
1763 send_paged_response( sop, rs, NULL, 0 );
1765 send_ldap_result( sop, rs );
1770 if ( sop->o_refresh_in_progress ) {
1771 sop->o_refresh_in_progress = 0;
1774 rs->sr_err = LDAP_SUCCESS;
1777 if ( sop->o_psearch_finished ) {
1778 avl_free( sop->o_psearch_finished, ch_free );
1781 if( !IS_PSEARCH && e != NULL ) {
1782 /* free reader lock */
1783 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1787 LOCK_ID_FREE( bdb->bi_dbenv, locker );
1789 ber_bvfree( search_context_csn );
1791 if( rs->sr_v2ref ) {
1792 ber_bvarray_free( rs->sr_v2ref );
1793 rs->sr_v2ref = NULL;
1795 if( realbase.bv_val ) ch_free( realbase.bv_val );
1801 static int base_candidate(
1806 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1807 e->e_nname.bv_val, (long) e->e_id, 0);
1814 /* Look for "objectClass Present" in this filter.
1815 * Also count depth of filter tree while we're at it.
1817 static int oc_filter(
1826 if( cur > *max ) *max = cur;
1828 switch( f->f_choice ) {
1829 case LDAP_FILTER_PRESENT:
1830 if (f->f_desc == slap_schema.si_ad_objectClass) {
1835 case LDAP_FILTER_AND:
1836 case LDAP_FILTER_OR:
1838 for ( f=f->f_and; f; f=f->f_next ) {
1839 (void) oc_filter(f, cur, max);
1849 static void search_stack_free( void *key, void *data )
1851 ber_memfree_x(data, NULL);
1854 static void *search_stack( Operation *op )
1856 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1859 if ( op->o_threadctx ) {
1860 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1863 ret = bdb->bi_search_stack;
1867 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1869 if ( op->o_threadctx ) {
1870 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1871 ret, search_stack_free );
1873 bdb->bi_search_stack = ret;
1879 static int search_candidates(
1888 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1890 Filter f, rf, xf, nf;
1892 AttributeAssertion aa_ref;
1893 #ifdef BDB_SUBENTRIES
1895 AttributeAssertion aa_subentry;
1899 * This routine takes as input a filter (user-filter)
1900 * and rewrites it as follows:
1901 * (&(scope=DN)[(objectClass=subentry)]
1902 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1905 Debug(LDAP_DEBUG_TRACE,
1906 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1907 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1909 xf.f_or = op->oq_search.rs_filter;
1910 xf.f_choice = LDAP_FILTER_OR;
1913 /* If the user's filter uses objectClass=*,
1914 * these clauses are redundant.
1916 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1917 && !get_subentries_visibility(op)
1918 && !is_sync_protocol(op) )
1920 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1921 /* match referral objects */
1922 struct berval bv_ref = BER_BVC( "referral" );
1923 rf.f_choice = LDAP_FILTER_EQUALITY;
1925 rf.f_av_desc = slap_schema.si_ad_objectClass;
1926 rf.f_av_value = bv_ref;
1927 rf.f_next = xf.f_or;
1934 f.f_choice = LDAP_FILTER_AND;
1936 /* Dummy; we compute scope separately now */
1937 nf.f_choice = SLAPD_FILTER_COMPUTED;
1938 nf.f_result = LDAP_SUCCESS;
1939 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1940 ? op->oq_search.rs_filter : &xf ;
1941 /* Filter depth increased again, adding dummy clause */
1944 #ifdef BDB_SUBENTRIES
1945 if( get_subentries_visibility( op ) ) {
1946 struct berval bv_subentry = BER_BVC( "subentry" );
1947 sf.f_choice = LDAP_FILTER_EQUALITY;
1948 sf.f_ava = &aa_subentry;
1949 sf.f_av_desc = slap_schema.si_ad_objectClass;
1950 sf.f_av_value = bv_subentry;
1951 sf.f_next = nf.f_next;
1956 /* Allocate IDL stack, plus 1 more for former tmp */
1957 if ( depth+1 > bdb->bi_search_stack_depth ) {
1958 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1960 stack = search_stack( stackop );
1963 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1964 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1966 rc = bdb_dn2idl( op, e, ids, stack );
1969 if ( rc == LDAP_SUCCESS ) {
1970 rc = bdb_filter_candidates( op, &f, ids,
1971 stack, stack+BDB_IDL_UM_SIZE );
1974 if ( depth+1 > bdb->bi_search_stack_depth ) {
1979 Debug(LDAP_DEBUG_TRACE,
1980 "bdb_search_candidates: failed (rc=%d)\n",
1984 Debug(LDAP_DEBUG_TRACE,
1985 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1987 (long) BDB_IDL_FIRST(ids),
1988 (long) BDB_IDL_LAST(ids) );
1995 parse_paged_cookie( Operation *op, SlapReply *rs )
1998 int rc = LDAP_SUCCESS;
2002 struct berval cookie = BER_BVNULL;
2004 /* this function must be invoked only if the pagedResults
2005 * control has been detected, parsed and partially checked
2006 * by the frontend */
2007 assert( get_pagedresults( op ) > SLAP_NO_CONTROL );
2009 /* look for the appropriate ctrl structure */
2010 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
2011 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
2017 if ( c[0] == NULL ) {
2018 rs->sr_text = "missing pagedResults control";
2019 return LDAP_PROTOCOL_ERROR;
2022 /* Tested by frontend */
2023 assert( c[0]->ldctl_value.bv_len > 0 );
2025 /* Parse the control value
2026 * realSearchControlValue ::= SEQUENCE {
2027 * size INTEGER (0..maxInt),
2028 * -- requested page size from client
2029 * -- result set size estimate from server
2030 * cookie OCTET STRING
2033 ber = ber_init( &c[0]->ldctl_value );
2034 if ( ber == NULL ) {
2035 rs->sr_text = "internal error";
2039 tag = ber_scanf( ber, "{im}", &size, &cookie );
2041 /* Tested by frontend */
2042 assert( tag != LBER_ERROR );
2043 assert( size >= 0 );
2045 /* cookie decoding/checks deferred to backend... */
2046 if ( cookie.bv_len ) {
2047 PagedResultsCookie reqcookie;
2048 if( cookie.bv_len != sizeof( reqcookie ) ) {
2050 rs->sr_text = "paged results cookie is invalid";
2051 rc = LDAP_PROTOCOL_ERROR;
2055 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
2057 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
2059 rs->sr_text = "paged results cookie is invalid";
2060 rc = LDAP_PROTOCOL_ERROR;
2063 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
2064 rs->sr_text = "paged results cookie is invalid or old";
2065 rc = LDAP_UNWILLING_TO_PERFORM;
2070 /* Initial request. Initialize state. */
2072 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
2073 /* There's another pagedResults control on the
2074 * same connection; reject new pagedResults controls
2075 * (allowed by RFC2696) */
2076 rs->sr_text = "paged results cookie unavailable; try later";
2077 rc = LDAP_UNWILLING_TO_PERFORM;
2081 op->o_pagedresults_state.ps_cookie = 0;
2082 op->o_pagedresults_state.ps_count = 0;
2086 (void)ber_free( ber, 1 );
2092 send_paged_response(
2098 LDAPControl ctrl, *ctrls[2];
2099 BerElementBuffer berbuf;
2100 BerElement *ber = (BerElement *)&berbuf;
2101 PagedResultsCookie respcookie;
2102 struct berval cookie;
2104 Debug(LDAP_DEBUG_ARGS,
2105 "send_paged_response: lastid=0x%08lx nentries=%d\n",
2106 lastid ? *lastid : 0, rs->sr_nentries, NULL );
2108 BER_BVZERO( &ctrl.ldctl_value );
2112 ber_init2( ber, NULL, LBER_USE_DER );
2115 respcookie = ( PagedResultsCookie )(*lastid);
2116 cookie.bv_len = sizeof( respcookie );
2117 cookie.bv_val = (char *)&respcookie;
2120 respcookie = ( PagedResultsCookie )0;
2121 BER_BVSTR( &cookie, "" );
2124 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
2125 op->o_conn->c_pagedresults_state.ps_count =
2126 op->o_pagedresults_state.ps_count + rs->sr_nentries;
2128 /* return size of 0 -- no estimate */
2129 ber_printf( ber, "{iO}", 0, &cookie );
2131 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
2135 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
2136 ctrls[0]->ldctl_iscritical = 0;
2138 rs->sr_ctrls = ctrls;
2139 rs->sr_err = LDAP_SUCCESS;
2140 send_ldap_result( op, rs );
2141 rs->sr_ctrls = NULL;
2144 (void) ber_free_buf( ber );
2148 bdb_pfid_cmp( const void *v_id1, const void *v_id2 )
2150 const ID *p1 = v_id1, *p2 = v_id2;
2155 bdb_id_dup( Operation *op, ID *id )
2158 new = ch_malloc( sizeof(ID) );