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, "=> " LDAP_XSTRING(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_CONTROL_NONE ) {
741 manageDSAit = SLAP_CONTROL_CRITICAL;
743 } else if ( IS_PSEARCH ) {
744 if ( manageDSAit == SLAP_CONTROL_NONE ) {
745 manageDSAit = SLAP_CONTROL_CRITICAL;
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,
878 LDAP_XSTRING(bdb_search) ": entry is referral\n",
881 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
882 rs->sr_err = LDAP_REFERRAL;
883 rs->sr_matched = matched_dn.bv_val;
884 send_ldap_result( sop, rs );
887 LOCK_ID_FREE (bdb->bi_dbenv, locker );
888 ber_bvarray_free( rs->sr_ref );
890 ber_memfree( matched_dn.bv_val );
891 rs->sr_matched = NULL;
895 if ( get_assert( op ) &&
896 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
898 rs->sr_err = LDAP_ASSERTION_FAILED;
899 send_ldap_result( sop, rs );
903 /* compute it anyway; root does not use it */
904 stoptime = op->o_time + sop->ors_tlimit;
906 /* need normalized dn below */
907 ber_dupbv( &realbase, &e->e_nname );
909 /* Copy info to base, must free entry before accessing the database
910 * in search_candidates, to avoid deadlocks.
912 base.e_private = e->e_private;
913 base.e_nname = realbase;
916 if ( e != &e_root ) {
917 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
922 rs->sr_err = bdb_get_commit_csn( sop, rs, &search_context_csn,
923 locker, &ctxcsn_lock );
925 if ( rs->sr_err != LDAP_SUCCESS ) {
926 send_ldap_error( sop, rs, rs->sr_err,
927 "error in csn management in search" );
931 if ( sop->o_sync_mode != SLAP_SYNC_NONE &&
932 sop->o_sync_state.ctxcsn &&
933 sop->o_sync_state.ctxcsn->bv_val &&
934 ber_bvcmp( &sop->o_sync_state.ctxcsn[0], search_context_csn ) == 0 )
936 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
940 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );
943 /* select candidates */
944 if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
945 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
948 BDB_IDL_ZERO( candidates );
949 BDB_IDL_ZERO( scopes );
950 rs->sr_err = search_candidates( op, sop, rs, &base,
951 locker, candidates, scopes );
954 if ( !IS_PSEARCH && sop->o_sync_mode != SLAP_SYNC_NONE ) {
955 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
958 /* start cursor at beginning of candidates.
962 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
963 cursor = bdb_idl_search( candidates, ps_e->e_id );
964 if ( candidates[cursor] != ps_e->e_id ) {
965 rs->sr_err = LDAP_SUCCESS;
968 } else if ( ps_e->e_id < BDB_IDL_RANGE_FIRST( candidates ) ||
969 ps_e->e_id > BDB_IDL_RANGE_LAST( candidates ))
971 rs->sr_err = LDAP_SUCCESS;
975 candidates[1] = ps_e->e_id;
978 if ( candidates[0] == 0 ) {
979 Debug( LDAP_DEBUG_TRACE,
980 LDAP_XSTRING(bdb_search) ": no candidates\n",
986 /* if not root and candidates exceed to-be-checked entries, abort */
987 if ( sop->ors_limit /* isroot == FALSE */ &&
988 sop->ors_limit->lms_s_unchecked != -1 &&
989 BDB_IDL_N(candidates) > (unsigned) sop->ors_limit->lms_s_unchecked )
991 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
992 send_ldap_result( sop, rs );
993 rs->sr_err = LDAP_SUCCESS;
997 if ( sop->ors_limit == NULL /* isroot == TRUE */ ||
998 !sop->ors_limit->lms_s_pr_hide )
1000 tentries = BDB_IDL_N(candidates);
1003 if ( get_pagedresults( sop ) > SLAP_CONTROL_IGNORED ) {
1004 PagedResultsState *ps = sop->o_pagedresults_state;
1005 /* deferred cookie parsing */
1006 rs->sr_err = parse_paged_cookie( sop, rs );
1007 if ( rs->sr_err != LDAP_SUCCESS ) {
1008 send_ldap_result( sop, rs );
1012 if ( (ID)( ps->ps_cookie ) == 0 ) {
1013 id = bdb_idl_first( candidates, &cursor );
1016 if ( ps->ps_size == 0 ) {
1017 rs->sr_err = LDAP_SUCCESS;
1018 rs->sr_text = "search abandoned by pagedResult size=0";
1019 send_ldap_result( sop, rs );
1022 for ( id = bdb_idl_first( candidates, &cursor );
1024 id <= (ID)( ps->ps_cookie );
1025 id = bdb_idl_next( candidates, &cursor ) )
1031 if ( cursor == NOID ) {
1032 Debug( LDAP_DEBUG_TRACE,
1033 LDAP_XSTRING(bdb_search)
1034 ": no paged results candidates\n",
1036 send_paged_response( sop, rs, &lastid, 0 );
1038 rs->sr_err = LDAP_OTHER;
1044 if (( sop->o_sync_mode & SLAP_SYNC_REFRESH ) || IS_PSEARCH ) {
1047 cookief.f_choice = LDAP_FILTER_AND;
1048 cookief.f_and = &csnfnot;
1049 cookief.f_next = NULL;
1051 csnfnot.f_choice = LDAP_FILTER_NOT;
1052 csnfnot.f_not = &csnfeq;
1053 csnfnot.f_next = &csnfand;
1055 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
1056 csnfeq.f_ava = &aa_eq;
1057 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
1058 if ( sop->o_sync_state.ctxcsn != NULL ) {
1059 csnfeq.f_av_value = *sop->o_sync_state.ctxcsn;
1061 csnfeq.f_av_value = slap_empty_bv;
1064 csnfand.f_choice = LDAP_FILTER_AND;
1065 csnfand.f_and = &csnfge;
1066 csnfand.f_next = NULL;
1068 csnfge.f_choice = LDAP_FILTER_GE;
1069 csnfge.f_ava = &aa_ge;
1070 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
1071 if ( sop->o_sync_state.ctxcsn != NULL ) {
1072 csnfge.f_av_value = *sop->o_sync_state.ctxcsn;
1074 csnfge.f_av_value = slap_empty_bv;
1077 if ( search_context_csn && !IS_PSEARCH ) {
1078 csnfge.f_next = &contextcsnand;
1080 contextcsnand.f_choice = LDAP_FILTER_AND;
1081 contextcsnand.f_and = &contextcsnle;
1082 contextcsnand.f_next = NULL;
1084 contextcsnle.f_choice = LDAP_FILTER_LE;
1085 contextcsnle.f_ava = &aa_le;
1086 contextcsnle.f_av_desc = slap_schema.si_ad_entryCSN;
1087 contextcsnle.f_av_value = *search_context_csn;
1088 contextcsnle.f_next = sop->oq_search.rs_filter;
1090 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
1091 if ( sop->o_sync_state.ctxcsn &&
1092 sop->o_sync_state.ctxcsn->bv_val != NULL )
1094 value_match( &match, slap_schema.si_ad_entryCSN, mr,
1095 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1096 &sop->o_sync_state.ctxcsn[0], search_context_csn,
1101 no_sync_state_change = ( match >= 0 );
1103 csnfge.f_next = sop->oq_search.rs_filter;
1109 for ( id = bdb_idl_first( candidates, &cursor );
1110 id != NOID && !no_sync_state_change;
1111 id = bdb_idl_next( candidates, &cursor ) )
1118 if ( !IS_POST_SEARCH ) {
1119 idhole = (ID*) avl_find( sop->o_psearch_finished,
1120 (caddr_t)&id, bdb_pfid_cmp );
1122 avl_delete( &sop->o_psearch_finished,
1123 (caddr_t)idhole, bdb_pfid_cmp );
1124 sop->o_tmpfree( idhole, sop->o_tmpmemctx );
1128 if ( sop->o_refresh_in_progress ) {
1129 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
1130 pce = LDAP_TAILQ_FIRST( &sop->o_ps_pre_candidates );
1131 while ( pce && pce->pc_sent ) {
1132 pce = LDAP_TAILQ_NEXT( pce, pc_link );
1134 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
1137 if ( BDB_IDL_IS_RANGE( candidates ) ) {
1138 if ( pce->pc_id >= candidates[1] &&
1139 pce->pc_id <= candidates[2] &&
1140 pce->pc_id > cursor-1 ) {
1143 avl_insert( &sop->o_psearch_finished,
1144 (caddr_t)bdb_id_dup( sop, &pce->pc_id ),
1145 bdb_pfid_cmp, avl_dup_error );
1150 pos = bdb_idl_search(candidates, pce->pc_id);
1151 if ( pos > cursor-1 && pos <= candidates[0] ) {
1154 avl_insert( &sop->o_psearch_finished,
1155 (caddr_t)bdb_id_dup( sop, &pce->pc_id ),
1156 bdb_pfid_cmp, avl_dup_error );
1165 /* check for abandon */
1166 if ( sop->o_abandon ) {
1168 bdb_drop_psearch( sop, sop->o_msgid );
1170 rs->sr_err = LDAP_SUCCESS;
1174 if ( sop->o_cancel ) {
1175 assert( sop->o_cancel == SLAP_CANCEL_REQ );
1176 rs->sr_err = LDAP_CANCELLED;
1177 send_ldap_result( sop, rs );
1178 sop->o_cancel = SLAP_CANCEL_ACK;
1179 rs->sr_err = LDAP_SUCCESS;
1183 /* check time limit */
1184 if ( sop->ors_tlimit != SLAP_NO_LIMIT
1185 && slap_get_time() > stoptime )
1187 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1188 rs->sr_ref = rs->sr_v2ref;
1189 send_ldap_result( sop, rs );
1190 rs->sr_err = LDAP_SUCCESS;
1196 /* get the entry with reader lock */
1198 rs->sr_err = bdb_cache_find_id( op, ltid,
1199 id, &ei, 0, locker, &lock );
1201 if (rs->sr_err == LDAP_BUSY) {
1202 rs->sr_text = "ldap server busy";
1203 send_ldap_result( sop, rs );
1206 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
1207 || rs->sr_err == DB_LOCK_NOTGRANTED )
1209 goto id2entry_retry;
1212 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
1219 if ( IS_POST_SEARCH ) {
1220 /* send LDAP_SYNC_DELETE */
1221 rs->sr_entry = e = ps_e;
1222 goto post_search_no_entry;
1223 } else if( !BDB_IDL_IS_RANGE(candidates) ) {
1224 /* only complain for non-range IDLs */
1225 Debug( LDAP_DEBUG_TRACE,
1226 LDAP_XSTRING(bdb_search)
1227 ": candidate %ld not found\n",
1239 #ifdef BDB_SUBENTRIES
1240 /* FIXME: send all but syncrepl */
1242 if ( !is_sync_protocol( sop ) )
1245 if ( is_entry_subentry( e ) ) {
1246 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
1247 if(!get_subentries_visibility( sop )) {
1248 /* only subentries are visible */
1252 } else if ( get_subentries( sop ) &&
1253 !get_subentries_visibility( sop ))
1255 /* only subentries are visible */
1259 } else if ( get_subentries_visibility( sop )) {
1260 /* only subentries are visible */
1264 #endif /* BDB_SUBENTRIES */
1266 /* Does this candidate actually satisfy the search scope?
1268 * Note that we don't lock access to the bei_parent pointer.
1269 * Since only leaf nodes can be deleted, the parent of any
1270 * node will always be a valid node. Also since we have
1271 * a Read lock on the data, it cannot be renamed out of the
1272 * scope while we are looking at it, and unless we're using
1273 * BDB_HIER, its parents cannot be moved either.
1275 switch( sop->ors_scope ) {
1276 case LDAP_SCOPE_BASE:
1277 /* This is always true, yes? */
1278 if ( id == base.e_id ) scopeok = 1;
1281 case LDAP_SCOPE_ONELEVEL:
1282 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
1285 #ifdef LDAP_SCOPE_CHILDREN
1286 case LDAP_SCOPE_CHILDREN:
1287 if ( id == base.e_id ) break;
1290 case LDAP_SCOPE_SUBTREE: {
1292 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
1293 if ( tmp->bei_id == base.e_id ) {
1301 /* aliases were already dereferenced in candidate list */
1302 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
1303 /* but if the search base is an alias, and we didn't
1304 * deref it when finding, return it.
1306 if ( is_entry_alias(e) &&
1307 ((sop->ors_deref & LDAP_DEREF_FINDING) ||
1308 !bvmatch(&e->e_nname, &op->o_req_ndn)))
1313 /* scopes is only non-empty for onelevel or subtree */
1314 if ( !scopeok && BDB_IDL_N(scopes) ) {
1316 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
1317 x = bdb_idl_search( scopes, e->e_id );
1318 if ( scopes[x] == e->e_id ) scopeok = 1;
1320 /* subtree, walk up the tree */
1321 EntryInfo *tmp = BEI(e);
1322 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
1323 x = bdb_idl_search( scopes, tmp->bei_id );
1324 if ( scopes[x] == tmp->bei_id ) {
1333 /* Not in scope, ignore it */
1334 if ( !IS_POST_SEARCH && !scopeok ) {
1335 Debug( LDAP_DEBUG_TRACE,
1336 LDAP_XSTRING(bdb_search)
1337 ": %ld scope not okay\n",
1343 * if it's a referral, add it to the list of referrals. only do
1344 * this for non-base searches, and don't check the filter
1345 * explicitly here since it's only a candidate anyway.
1347 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1348 && is_entry_referral( e ) )
1350 BerVarray erefs = get_entry_referrals( sop, e );
1351 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1352 sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1353 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1355 send_search_reference( sop, rs );
1357 ber_bvarray_free( rs->sr_ref );
1358 ber_bvarray_free( erefs );
1364 if ( !manageDSAit && is_entry_glue( e )) {
1368 /* if it matches the filter and scope, send it */
1370 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1371 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1373 rs->sr_err = LDAP_COMPARE_TRUE;
1377 if ( !IS_POST_SEARCH ) {
1378 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1379 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1380 rs->sr_err = test_filter( sop, rs->sr_entry,
1382 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1383 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1384 if ( no_sync_state_change ) {
1385 Debug( LDAP_DEBUG_TRACE,
1386 LDAP_XSTRING(bdb_search) ": "
1387 "error in context csn management\n",
1390 entry_sync_state = LDAP_SYNC_ADD;
1393 if ( no_sync_state_change ) {
1396 entry_sync_state = LDAP_SYNC_PRESENT;
1400 rs->sr_err = test_filter( sop,
1401 rs->sr_entry, sop->oq_search.rs_filter );
1405 rs->sr_err = test_filter( sop,
1406 rs->sr_entry, sop->oq_search.rs_filter );
1408 rs->sr_err = LDAP_COMPARE_TRUE;
1413 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1414 /* check size limit */
1415 if ( --sop->ors_slimit == -1 &&
1416 sop->o_sync_slog_size == -1 )
1419 bdb_cache_return_entry_r( bdb->bi_dbenv,
1420 &bdb->bi_cache, e, &lock );
1423 rs->sr_entry = NULL;
1424 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1425 rs->sr_ref = rs->sr_v2ref;
1426 send_ldap_result( sop, rs );
1427 rs->sr_err = LDAP_SUCCESS;
1431 if ( get_pagedresults(sop) > SLAP_CONTROL_IGNORED ) {
1432 if ( rs->sr_nentries >= ((PagedResultsState *)sop->o_pagedresults_state)->ps_size ) {
1433 send_paged_response( sop, rs, &lastid, tentries );
1443 if (IS_PSEARCH || IS_POST_SEARCH) {
1444 int premodify_found = 0;
1446 if ( IS_POST_SEARCH ||
1447 ps_type == LDAP_PSEARCH_BY_ADD ||
1448 ps_type == LDAP_PSEARCH_BY_DELETE ||
1449 ps_type == LDAP_PSEARCH_BY_MODIFY ||
1450 ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1452 if ( !IS_POST_SEARCH &&
1453 ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1454 struct psid_entry* psid_e;
1455 LDAP_LIST_FOREACH( psid_e,
1456 &op->o_pm_list, ps_link)
1458 if( psid_e->ps_op == sop ) {
1459 premodify_found = 1;
1460 LDAP_LIST_REMOVE(psid_e, ps_link);
1464 if (psid_e != NULL) free (psid_e);
1467 if ( IS_POST_SEARCH ) {
1469 entry_sync_state = LDAP_SYNC_ADD;
1471 post_search_no_entry:
1472 entry_sync_state = LDAP_SYNC_DELETE;
1474 } else if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1475 entry_sync_state = LDAP_SYNC_ADD;
1476 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1477 entry_sync_state = LDAP_SYNC_DELETE;
1478 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1479 if ( premodify_found ) {
1480 entry_sync_state = LDAP_SYNC_MODIFY;
1482 entry_sync_state = LDAP_SYNC_ADD;
1484 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT ) {
1485 entry_sync_state = LDAP_SYNC_DELETE;
1487 rs->sr_err = LDAP_OTHER;
1491 if ( sop->o_sync_slog_size != -1 ) {
1492 if ( entry_sync_state == LDAP_SYNC_DELETE ) {
1493 result = slap_add_session_log( op, sop, e );
1498 struct berval cookie;
1499 slap_compose_sync_cookie( sop, &cookie,
1501 sop->o_sync_state.sid,
1502 sop->o_sync_state.rid );
1503 rs->sr_err = slap_build_sync_state_ctrl(
1504 sop, rs, e, entry_sync_state, ctrls,
1505 num_ctrls++, 1, &cookie );
1506 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1507 if (!(IS_POST_SEARCH &&
1508 entry_sync_state == LDAP_SYNC_DELETE)) {
1509 rs->sr_attrs = attrs;
1511 rs->sr_attrs = NULL;
1513 rs->sr_operational_attrs = NULL;
1514 rs->sr_ctrls = ctrls;
1516 result = send_search_entry( sop, rs );
1517 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1519 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1521 slap_sl_free( ctrls[--num_ctrls],
1523 ctrls[num_ctrls] = NULL;
1524 rs->sr_ctrls = NULL;
1527 } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1528 struct psid_entry* psid_e;
1529 psid_e = (struct psid_entry *) ch_calloc(1,
1530 sizeof(struct psid_entry));
1531 psid_e->ps_op = sop;
1532 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1536 Debug( LDAP_DEBUG_TRACE,
1537 LDAP_XSTRING(bdb_search)
1538 ": invalid ps_type (%d) \n",
1543 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1544 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1545 rs->sr_err = slap_build_sync_state_ctrl(
1546 sop, rs, e, entry_sync_state, ctrls,
1547 num_ctrls++, 0, NULL );
1548 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1549 rs->sr_ctrls = ctrls;
1550 rs->sr_attrs = sop->oq_search.rs_attrs;
1551 rs->sr_operational_attrs = NULL;
1553 result = send_search_entry( sop, rs );
1555 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1557 slap_sl_free( ctrls[--num_ctrls],
1559 ctrls[num_ctrls] = NULL;
1560 rs->sr_ctrls = NULL;
1562 } else { /* PRESENT */
1563 if ( sync_send_present_mode ) {
1564 result = slap_build_syncUUID_set( sop,
1566 if ( result <= 0 ) {
1570 if ( syncUUID_set_cnt ==
1571 SLAP_SYNCUUID_SET_SIZE )
1573 rs->sr_err = LDAP_SUCCESS;
1574 rs->sr_rspoid = LDAP_SYNC_INFO;
1575 rs->sr_ctrls = NULL;
1576 result = slap_send_syncinfo( sop, rs,
1577 LDAP_TAG_SYNC_ID_SET,
1578 NULL, 0, syncUUID_set, 0 );
1579 if ( result != LDAP_SUCCESS ) {
1582 ber_bvarray_free_x( syncUUID_set,
1584 syncUUID_set = NULL;
1585 syncUUID_set_cnt = 0;
1595 rs->sr_attrs = sop->oq_search.rs_attrs;
1596 rs->sr_operational_attrs = NULL;
1597 rs->sr_ctrls = NULL;
1599 rs->sr_err = LDAP_SUCCESS;
1600 result = send_search_entry( sop, rs );
1605 case 0: /* entry sent ok */
1607 case 1: /* entry not sent */
1609 case -1: /* connection closed */
1611 bdb_cache_return_entry_r(bdb->bi_dbenv,
1612 &bdb->bi_cache, e, &lock);
1615 rs->sr_entry = NULL;
1616 rs->sr_err = LDAP_OTHER;
1622 Debug( LDAP_DEBUG_TRACE,
1623 LDAP_XSTRING(bdb_search)
1624 ": %ld does not match filter\n",
1630 /* free reader lock */
1632 if (!(IS_POST_SEARCH &&
1633 entry_sync_state == LDAP_SYNC_DELETE)) {
1634 bdb_cache_return_entry_r( bdb->bi_dbenv,
1635 &bdb->bi_cache, e , &lock );
1636 if ( sop->o_nocaching ) {
1637 bdb_cache_delete_entry( bdb, ei, locker, &lock );
1642 rs->sr_entry = NULL;
1645 if ( sop->o_refresh_in_progress ) {
1651 ldap_pvt_thread_yield();
1654 if ( syncUUID_set_cnt > 0 ) {
1655 rs->sr_err = LDAP_SUCCESS;
1656 rs->sr_rspoid = LDAP_SYNC_INFO;
1657 rs->sr_ctrls = NULL;
1658 slap_send_syncinfo( sop, rs, LDAP_TAG_SYNC_ID_SET,
1659 NULL, 0, syncUUID_set, 0 );
1660 ber_bvarray_free_x( syncUUID_set, sop->o_tmpmemctx );
1661 syncUUID_set_cnt = 0;
1665 if (!IS_PSEARCH && !IS_POST_SEARCH) {
1666 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1667 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1668 struct berval cookie;
1669 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1670 sop->o_sync_state.sid, sop->o_sync_state.rid );
1672 if ( sync_send_present_mode ) {
1673 rs->sr_err = LDAP_SUCCESS;
1674 rs->sr_rspoid = LDAP_SYNC_INFO;
1675 rs->sr_ctrls = NULL;
1676 slap_send_syncinfo( sop, rs,
1677 LDAP_TAG_SYNC_REFRESH_PRESENT, &cookie, 1, NULL, 0 );
1680 if ( !no_sync_state_change ) {
1682 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1683 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1686 if ( ps_list->o_sync_slog_size > 0 ) {
1687 if ( ps_list->o_sync_state.sid ==
1688 sop->o_sync_state.sid )
1697 rs->sr_err = LDAP_SUCCESS;
1698 rs->sr_rspoid = NULL;
1699 rs->sr_ctrls = NULL;
1700 slap_send_session_log( op, ps_list, rs );
1702 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1705 rs->sr_err = LDAP_SUCCESS;
1706 rs->sr_rspoid = LDAP_SYNC_INFO;
1707 rs->sr_ctrls = NULL;
1708 slap_send_syncinfo( sop, rs,
1709 LDAP_TAG_SYNC_REFRESH_DELETE, &cookie, 1, NULL, 0 );
1712 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1715 /* refreshOnly mode */
1716 struct berval cookie;
1717 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1718 sop->o_sync_state.sid, sop->o_sync_state.rid );
1720 if ( sync_send_present_mode ) {
1721 slap_build_sync_done_ctrl( sop, rs, ctrls,
1722 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1725 if ( !no_sync_state_change ) {
1727 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1728 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1731 if ( ps_list->o_sync_slog_size > 0 ) {
1732 if ( ps_list->o_sync_state.sid ==
1733 sop->o_sync_state.sid ) {
1741 slap_send_session_log( op, ps_list, rs );
1743 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1746 slap_build_sync_done_ctrl( sop, rs, ctrls,
1747 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_DELETES );
1750 rs->sr_ctrls = ctrls;
1751 rs->sr_ref = rs->sr_v2ref;
1752 rs->sr_err = (rs->sr_v2ref == NULL)
1753 ? LDAP_SUCCESS : LDAP_REFERRAL;
1754 rs->sr_rspoid = NULL;
1755 send_ldap_result( sop, rs );
1756 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1757 slap_sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val,
1760 slap_sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1761 ctrls[num_ctrls] = NULL;
1762 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1766 rs->sr_ctrls = NULL;
1767 rs->sr_ref = rs->sr_v2ref;
1768 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1769 rs->sr_rspoid = NULL;
1770 if ( get_pagedresults(sop) > SLAP_CONTROL_IGNORED ) {
1771 send_paged_response( sop, rs, NULL, 0 );
1773 send_ldap_result( sop, rs );
1778 if ( sop->o_refresh_in_progress ) {
1779 sop->o_refresh_in_progress = 0;
1782 rs->sr_err = LDAP_SUCCESS;
1785 if ( sop->o_psearch_finished ) {
1786 avl_free( sop->o_psearch_finished, ch_free );
1789 if( !IS_PSEARCH && e != NULL ) {
1790 /* free reader lock */
1791 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1795 LOCK_ID_FREE( bdb->bi_dbenv, locker );
1797 ber_bvfree( search_context_csn );
1799 if( rs->sr_v2ref ) {
1800 ber_bvarray_free( rs->sr_v2ref );
1801 rs->sr_v2ref = NULL;
1803 if( realbase.bv_val ) ch_free( realbase.bv_val );
1809 static int base_candidate(
1814 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1815 e->e_nname.bv_val, (long) e->e_id, 0);
1822 /* Look for "objectClass Present" in this filter.
1823 * Also count depth of filter tree while we're at it.
1825 static int oc_filter(
1834 if( cur > *max ) *max = cur;
1836 switch( f->f_choice ) {
1837 case LDAP_FILTER_PRESENT:
1838 if (f->f_desc == slap_schema.si_ad_objectClass) {
1843 case LDAP_FILTER_AND:
1844 case LDAP_FILTER_OR:
1846 for ( f=f->f_and; f; f=f->f_next ) {
1847 (void) oc_filter(f, cur, max);
1857 static void search_stack_free( void *key, void *data )
1859 ber_memfree_x(data, NULL);
1862 static void *search_stack( Operation *op )
1864 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1867 if ( op->o_threadctx ) {
1868 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1871 ret = bdb->bi_search_stack;
1875 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1877 if ( op->o_threadctx ) {
1878 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1879 ret, search_stack_free );
1881 bdb->bi_search_stack = ret;
1887 static int search_candidates(
1896 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1898 Filter f, rf, xf, nf;
1900 AttributeAssertion aa_ref;
1901 #ifdef BDB_SUBENTRIES
1903 AttributeAssertion aa_subentry;
1907 * This routine takes as input a filter (user-filter)
1908 * and rewrites it as follows:
1909 * (&(scope=DN)[(objectClass=subentry)]
1910 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1913 Debug(LDAP_DEBUG_TRACE,
1914 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1915 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1917 xf.f_or = op->oq_search.rs_filter;
1918 xf.f_choice = LDAP_FILTER_OR;
1921 /* If the user's filter uses objectClass=*,
1922 * these clauses are redundant.
1924 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1925 && !get_subentries_visibility(op)
1926 && !is_sync_protocol(op) )
1928 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1929 /* match referral objects */
1930 struct berval bv_ref = BER_BVC( "referral" );
1931 rf.f_choice = LDAP_FILTER_EQUALITY;
1933 rf.f_av_desc = slap_schema.si_ad_objectClass;
1934 rf.f_av_value = bv_ref;
1935 rf.f_next = xf.f_or;
1942 f.f_choice = LDAP_FILTER_AND;
1944 /* Dummy; we compute scope separately now */
1945 nf.f_choice = SLAPD_FILTER_COMPUTED;
1946 nf.f_result = LDAP_SUCCESS;
1947 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1948 ? op->oq_search.rs_filter : &xf ;
1949 /* Filter depth increased again, adding dummy clause */
1952 #ifdef BDB_SUBENTRIES
1953 if( get_subentries_visibility( op ) ) {
1954 struct berval bv_subentry = BER_BVC( "subentry" );
1955 sf.f_choice = LDAP_FILTER_EQUALITY;
1956 sf.f_ava = &aa_subentry;
1957 sf.f_av_desc = slap_schema.si_ad_objectClass;
1958 sf.f_av_value = bv_subentry;
1959 sf.f_next = nf.f_next;
1964 /* Allocate IDL stack, plus 1 more for former tmp */
1965 if ( depth+1 > bdb->bi_search_stack_depth ) {
1966 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1968 stack = search_stack( stackop );
1971 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1972 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1974 rc = bdb_dn2idl( op, e, ids, stack );
1977 if ( rc == LDAP_SUCCESS ) {
1978 rc = bdb_filter_candidates( op, &f, ids,
1979 stack, stack+BDB_IDL_UM_SIZE );
1982 if ( depth+1 > bdb->bi_search_stack_depth ) {
1987 Debug(LDAP_DEBUG_TRACE,
1988 "bdb_search_candidates: failed (rc=%d)\n",
1992 Debug(LDAP_DEBUG_TRACE,
1993 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1995 (long) BDB_IDL_FIRST(ids),
1996 (long) BDB_IDL_LAST(ids) );
2003 parse_paged_cookie( Operation *op, SlapReply *rs )
2006 int rc = LDAP_SUCCESS;
2010 struct berval cookie = BER_BVNULL;
2011 PagedResultsState *ps = op->o_pagedresults_state;
2013 /* this function must be invoked only if the pagedResults
2014 * control has been detected, parsed and partially checked
2015 * by the frontend */
2016 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
2018 /* look for the appropriate ctrl structure */
2019 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
2020 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
2026 if ( c[0] == NULL ) {
2027 rs->sr_text = "missing pagedResults control";
2028 return LDAP_PROTOCOL_ERROR;
2031 /* Tested by frontend */
2032 assert( c[0]->ldctl_value.bv_len > 0 );
2034 /* Parse the control value
2035 * realSearchControlValue ::= SEQUENCE {
2036 * size INTEGER (0..maxInt),
2037 * -- requested page size from client
2038 * -- result set size estimate from server
2039 * cookie OCTET STRING
2042 ber = ber_init( &c[0]->ldctl_value );
2043 if ( ber == NULL ) {
2044 rs->sr_text = "internal error";
2048 tag = ber_scanf( ber, "{im}", &size, &cookie );
2050 /* Tested by frontend */
2051 assert( tag != LBER_ERROR );
2052 assert( size >= 0 );
2054 /* cookie decoding/checks deferred to backend... */
2055 if ( cookie.bv_len ) {
2056 PagedResultsCookie reqcookie;
2057 if( cookie.bv_len != sizeof( reqcookie ) ) {
2059 rs->sr_text = "paged results cookie is invalid";
2060 rc = LDAP_PROTOCOL_ERROR;
2064 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
2066 if ( reqcookie > ps->ps_cookie ) {
2068 rs->sr_text = "paged results cookie is invalid";
2069 rc = LDAP_PROTOCOL_ERROR;
2072 } else if ( reqcookie < ps->ps_cookie ) {
2073 rs->sr_text = "paged results cookie is invalid or old";
2074 rc = LDAP_UNWILLING_TO_PERFORM;
2079 /* Initial request. Initialize state. */
2081 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
2082 /* There's another pagedResults control on the
2083 * same connection; reject new pagedResults controls
2084 * (allowed by RFC2696) */
2085 rs->sr_text = "paged results cookie unavailable; try later";
2086 rc = LDAP_UNWILLING_TO_PERFORM;
2095 (void)ber_free( ber, 1 );
2101 send_paged_response(
2107 LDAPControl ctrl, *ctrls[2];
2108 BerElementBuffer berbuf;
2109 BerElement *ber = (BerElement *)&berbuf;
2110 PagedResultsCookie respcookie;
2111 struct berval cookie;
2113 Debug(LDAP_DEBUG_ARGS,
2114 "send_paged_response: lastid=0x%08lx nentries=%d\n",
2115 lastid ? *lastid : 0, rs->sr_nentries, NULL );
2117 BER_BVZERO( &ctrl.ldctl_value );
2121 ber_init2( ber, NULL, LBER_USE_DER );
2124 respcookie = ( PagedResultsCookie )(*lastid);
2125 cookie.bv_len = sizeof( respcookie );
2126 cookie.bv_val = (char *)&respcookie;
2129 respcookie = ( PagedResultsCookie )0;
2130 BER_BVSTR( &cookie, "" );
2133 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
2134 op->o_conn->c_pagedresults_state.ps_count =
2135 ((PagedResultsState *)op->o_pagedresults_state)->ps_count + rs->sr_nentries;
2137 /* return size of 0 -- no estimate */
2138 ber_printf( ber, "{iO}", 0, &cookie );
2140 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
2144 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
2145 ctrls[0]->ldctl_iscritical = 0;
2147 rs->sr_ctrls = ctrls;
2148 rs->sr_err = LDAP_SUCCESS;
2149 send_ldap_result( op, rs );
2150 rs->sr_ctrls = NULL;
2153 (void) ber_free_buf( ber );
2157 bdb_pfid_cmp( const void *v_id1, const void *v_id2 )
2159 const ID *p1 = v_id1, *p2 = v_id2;
2164 bdb_id_dup( Operation *op, ID *id )
2167 new = ch_malloc( sizeof(ID) );