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>
26 static int base_candidate(
31 static int search_candidates(
32 Operation *stackop, /* op with the current threadctx/slab cache */
33 Operation *sop, /* search op */
40 static int parse_paged_cookie( Operation *op, SlapReply *rs );
42 static void send_paged_response(
48 static int bdb_pfid_cmp( const void *v_id1, const void *v_id2 );
49 static ID* bdb_id_dup( Operation *op, ID *id );
51 /* Dereference aliases for a single alias entry. Return the final
52 * dereferenced entry on success, NULL on any failure.
54 static Entry * deref_base (
64 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
69 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
70 rs->sr_text = "maximum deref depth exceeded";
72 while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
73 /* Remember the last entry we looked at, so we can
78 /* If this is part of a subtree or onelevel search,
79 * have we seen this ID before? If so, quit.
81 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
86 /* If we've seen this ID during this deref iteration,
89 if ( bdb_idl_insert( tmp, e->e_id ) ) {
90 rs->sr_err = LDAP_ALIAS_PROBLEM;
91 rs->sr_text = "circular alias";
96 /* If there was a problem getting the aliasedObjectName,
97 * get_alias_dn will have set the error status.
99 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
104 rs->sr_err = bdb_dn2entry( op, NULL, &ndn, &ei,
114 rs->sr_err = LDAP_ALIAS_PROBLEM;
115 rs->sr_text = "aliasedObject not found";
119 /* Free the previous entry, continue to work with the
120 * one we just retrieved.
122 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
126 /* We found a regular entry. Return this to the caller. The
127 * entry is still locked for Read.
129 if (!is_entry_alias(e)) {
130 rs->sr_err = LDAP_SUCCESS;
138 /* Look for and dereference all aliases within the search scope. Adds
139 * the dereferenced entries to the "ids" list. Requires "stack" to be
140 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
141 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
143 static int search_aliases(
152 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
153 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
154 ID cursora, ida, cursoro, ido, *subscop2;
157 struct berval bv_alias = BER_BVC( "alias" );
158 AttributeAssertion aa_alias;
160 DB_LOCK locka, lockr;
163 aliases = stack; /* IDL of all aliases in the database */
164 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
165 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
166 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
167 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
168 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
169 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
171 /* A copy of subscop, because subscop gets clobbered by
172 * the bdb_idl_union/intersection routines
174 subscop2 = tmp + BDB_IDL_DB_SIZE;
176 af.f_choice = LDAP_FILTER_EQUALITY;
177 af.f_ava = &aa_alias;
178 af.f_av_desc = slap_schema.si_ad_objectClass;
179 af.f_av_value = bv_alias;
182 /* Find all aliases in database */
183 BDB_IDL_ZERO( aliases );
184 rs->sr_err = bdb_filter_candidates( op, &af, aliases,
186 if (rs->sr_err != LDAP_SUCCESS) {
190 oldsubs[1] = e->e_id;
193 BDB_IDL_ZERO( visited );
194 BDB_IDL_ZERO( newsubs );
197 ido = bdb_idl_first( oldsubs, &cursoro );
200 /* Set curscop to only the aliases in the current scope. Start with
201 * all the aliases, obtain the IDL for the current scope, and then
202 * get the intersection of these two IDLs. Add the current scope
203 * to the cumulative list of candidates.
205 BDB_IDL_CPY( curscop, aliases );
206 rs->sr_err = bdb_dn2idl( op, e, subscop,
207 subscop2+BDB_IDL_DB_SIZE );
211 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
213 BDB_IDL_CPY(subscop2, subscop);
214 rs->sr_err = bdb_idl_intersection(curscop, subscop);
215 bdb_idl_union( ids, subscop2 );
217 /* Dereference all of the aliases in the current scope. */
219 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
220 ida = bdb_idl_next(curscop, &cursora))
224 rs->sr_err = bdb_cache_find_id(op, NULL,
225 ida, &ei, 0, locker, &lockr );
226 if (rs->sr_err != LDAP_SUCCESS) {
227 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
228 rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
233 /* This should only happen if the curscop IDL has maxed out and
234 * turned into a range that spans IDs indiscriminately
236 if (!is_entry_alias(a)) {
237 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
242 /* Actually dereference the alias */
244 a = deref_base( op, rs, a, &matched, locker, &lockr,
247 /* If the target was not already in our current candidates,
248 * make note of it in the newsubs list. Also
249 * set it in the scopes list so that bdb_search
252 if (bdb_idl_insert(ids, a->e_id) == 0) {
253 bdb_idl_insert(newsubs, a->e_id);
254 bdb_idl_insert(scopes, a->e_id);
256 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
259 } else if (matched) {
260 /* Alias could not be dereferenced, or it deref'd to
261 * an ID we've already seen. Ignore it.
263 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
268 /* If this is a OneLevel search, we're done; oldsubs only had one
269 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
271 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
273 ido = bdb_idl_next( oldsubs, &cursoro );
275 /* If we're done processing the old scopes, did we add any new
276 * scopes in this iteration? If so, go back and do those now.
279 if (BDB_IDL_IS_ZERO(newsubs)) break;
280 BDB_IDL_CPY(oldsubs, newsubs);
281 BDB_IDL_ZERO(newsubs);
283 ido = bdb_idl_first( oldsubs, &cursoro );
286 /* Find the entry corresponding to the next scope. If it can't
287 * be found, ignore it and move on. This should never happen;
288 * we should never see the ID of an entry that doesn't exist.
289 * Set the name so that the scope's IDL can be retrieved.
293 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
295 if ( rs->sr_err != LDAP_SUCCESS ) {
296 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
297 rs->sr_err == DB_LOCK_NOTGRANTED )
306 #define is_sync_protocol(op) \
307 ((op)->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST)
309 #define IS_BDB_REPLACE(type) (( type == LDAP_PSEARCH_BY_DELETE ) || \
310 ( type == LDAP_PSEARCH_BY_SCOPEOUT ))
311 #define IS_PSEARCH (op != sop)
312 #define IS_POST_SEARCH ( op->ors_post_search_id != NOID )
315 bdb_drop_psearch( Operation *op, ber_int_t msgid )
318 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
320 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
321 if ( ps_list->o_connid == op->o_connid ) {
322 if ( ps_list->o_msgid == msgid ) {
323 ps_list->o_abandon = 1;
324 LDAP_LIST_REMOVE( ps_list, o_ps_link );
325 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
326 LDAP_STAILQ_REMOVE( &op->o_conn->c_ops, ps_list,
328 LDAP_STAILQ_NEXT( ps_list, o_next ) = NULL;
329 op->o_conn->c_n_ops_executing--;
330 op->o_conn->c_n_ops_completed++;
331 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
341 bdb_abandon( Operation *op, SlapReply *rs )
345 ps = bdb_drop_psearch( op, op->oq_abandon.rs_msgid );
348 if ( ps->o_tmpmemctx ) {
349 slap_sl_mem_destroy( NULL, ps->o_tmpmemctx );
353 return LDAP_UNAVAILABLE;
357 bdb_cancel( Operation *op, SlapReply *rs )
361 ps = bdb_drop_psearch( op, op->oq_cancel.rs_msgid );
363 rs->sr_err = LDAP_CANCELLED;
364 send_ldap_result( ps, rs );
366 if ( ps->o_tmpmemctx ) {
367 slap_sl_mem_destroy( NULL, ps->o_tmpmemctx );
371 return LDAP_UNAVAILABLE;
374 int bdb_search( Operation *op, SlapReply *rs )
377 struct pc_entry *pce = NULL;
378 struct pc_entry *tmp_pce = NULL;
382 ps_e.e_private = NULL;
383 ldap_pvt_thread_mutex_init( &op->o_pcmutex );
384 LDAP_TAILQ_INIT( &op->o_ps_pre_candidates );
385 LDAP_TAILQ_INIT( &op->o_ps_post_candidates );
387 op->ors_post_search_id = NOID;
388 rc = bdb_do_search( op, rs, op, NULL, 0 );
390 ldap_pvt_thread_mutex_lock( &op->o_pcmutex );
391 pce = LDAP_TAILQ_FIRST( &op->o_ps_post_candidates );
392 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
394 while ( rc == LDAP_SUCCESS && pce &&
395 op->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
397 ps_e.e_id = op->ors_post_search_id = pce->pc_id;
398 if ( op->o_sync_csn.bv_val ) {
399 ch_free( op->o_sync_csn.bv_val );
400 op->o_sync_csn.bv_val = NULL;
402 ber_dupbv( &op->o_sync_csn, &pce->pc_csn );
403 ber_dupbv( &ps_e.e_name, &pce->pc_ename );
404 ber_dupbv( &ps_e.e_nname, &pce->pc_enname );
405 a = ch_calloc( 1, sizeof( Attribute ));
406 a->a_desc = slap_schema.si_ad_entryUUID;
407 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
408 ber_dupbv( &a->a_vals[0], &pce->pc_entryUUID );
409 a->a_nvals = a->a_vals;
413 rc = bdb_do_search( op, rs, op, &ps_e, 0 );
416 ldap_pvt_thread_mutex_lock( &op->o_pcmutex );
417 pce = LDAP_TAILQ_NEXT( pce, pc_link );
418 LDAP_TAILQ_REMOVE( &op->o_ps_post_candidates, tmp_pce, pc_link );
419 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
421 ch_free( tmp_pce->pc_csn.bv_val );
422 ch_free( tmp_pce->pc_entryUUID.bv_val );
423 ch_free( tmp_pce->pc_ename.bv_val );
424 ch_free( tmp_pce->pc_enname.bv_val );
426 entry_clean( &ps_e );
431 #define BDB_PSEARCH_MAX_WAIT 3
432 int bdb_psearch( Operation *op, SlapReply *rs, Operation *sop,
433 Entry *ps_e, int ps_type )
436 struct pc_entry *pce = NULL;
437 struct pc_entry *p = NULL;
440 op->ors_post_search_id = NOID;
443 case LDAP_PSEARCH_BY_PREMODIFY:
444 case LDAP_PSEARCH_BY_PREDELETE:
446 if ( !op->o_ps_send_wait ) {
447 if ( sop->o_refresh_in_progress ) {
448 pce = (struct pc_entry *) ch_calloc(
449 1, sizeof( struct pc_entry ));
450 pce->pc_id = ps_e->e_id;
451 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
452 if ( LDAP_TAILQ_EMPTY( &sop->o_ps_pre_candidates )) {
453 LDAP_TAILQ_INSERT_HEAD(
454 &sop->o_ps_pre_candidates, pce, pc_link );
456 LDAP_TAILQ_FOREACH( p,
457 &sop->o_ps_pre_candidates, pc_link ) {
458 if ( p->pc_id > pce->pc_id )
463 LDAP_TAILQ_INSERT_BEFORE( p, pce, pc_link );
465 LDAP_TAILQ_INSERT_TAIL(
466 &sop->o_ps_pre_candidates,
470 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
472 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
476 pce = op->o_ps_send_wait;
479 /* Wait until refresh search send the entry */
480 while ( !pce->pc_sent ) {
481 if ( sop->o_refresh_in_progress ) {
482 if ( num_retries == BDB_PSEARCH_MAX_WAIT ) {
483 op->o_ps_send_wait = pce;
486 ldap_pvt_thread_yield();
487 bdb_trans_backoff( ++num_retries );
493 op->o_ps_send_wait = NULL;
495 if ( !sop->o_refresh_in_progress && !pce->pc_sent ) {
496 /* refresh ended without processing pce */
497 /* need to perform psearch for ps_e */
498 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
499 LDAP_TAILQ_REMOVE( &sop->o_ps_pre_candidates, pce, pc_link );
500 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
502 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
505 /* the pce entry was sent in the refresh phase */
506 if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
507 struct psid_entry* psid_e;
508 psid_e = (struct psid_entry *) ch_calloc(1,
509 sizeof(struct psid_entry));
511 LDAP_LIST_INSERT_HEAD( &op->o_pm_list, psid_e, ps_link );
514 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
515 LDAP_TAILQ_REMOVE( &sop->o_ps_pre_candidates, pce, pc_link );
516 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
521 case LDAP_PSEARCH_BY_DELETE:
522 case LDAP_PSEARCH_BY_SCOPEOUT:
523 case LDAP_PSEARCH_BY_ADD:
524 case LDAP_PSEARCH_BY_MODIFY:
525 ldap_pvt_thread_mutex_lock( &op->o_pcmutex );
526 if ( sop->o_refresh_in_progress ||
527 !LDAP_TAILQ_EMPTY( &sop->o_ps_post_candidates )) {
528 pce = (struct pc_entry *) ch_calloc( 1, sizeof( struct pc_entry ));
529 pce->pc_id = ps_e->e_id;
530 ber_dupbv( &pce->pc_csn, &op->o_sync_csn );
531 if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
533 for ( a = ps_e->e_attrs; a != NULL; a = a->a_next ) {
534 AttributeDescription *desc = a->a_desc;
535 if ( desc == slap_schema.si_ad_entryUUID ) {
536 ber_dupbv( &pce->pc_entryUUID, &a->a_nvals[0] );
540 ber_dupbv( &pce->pc_ename, &ps_e->e_name );
541 ber_dupbv( &pce->pc_enname, &ps_e->e_nname );
542 LDAP_TAILQ_INSERT_TAIL( &sop->o_ps_post_candidates, pce, pc_link );
543 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
545 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
546 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
551 Debug( LDAP_DEBUG_TRACE, "do_psearch: invalid psearch type\n",
557 /* For persistent searches, op is the currently executing operation,
558 * sop is the persistent search. For regular searches, sop = op.
561 bdb_do_search( Operation *op, SlapReply *rs, Operation *sop,
562 Entry *ps_e, int ps_type )
564 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
567 ID candidates[BDB_IDL_UM_SIZE];
568 ID scopes[BDB_IDL_DB_SIZE];
569 Entry *e = NULL, base, e_root = {0};
570 Entry *matched = NULL;
571 EntryInfo *ei, ei_root = {0};
572 struct berval realbase = BER_BVNULL;
576 AttributeName *attrs;
578 Filter contextcsnand, contextcsnle, cookief, csnfnot,
579 csnfeq, csnfand, csnfge;
580 AttributeAssertion aa_ge, aa_eq, aa_le;
581 struct berval *search_context_csn = NULL;
583 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
585 AttributeName uuid_attr[2];
587 int entry_sync_state = -1;
588 AttributeName null_attr;
589 int no_sync_state_change = 0;
591 u_int32_t locker = 0;
595 int sync_send_present_mode = 1;
601 struct pc_entry *pce = NULL;
602 BerVarray syncUUID_set = NULL;
603 int syncUUID_set_cnt = 0;
605 struct bdb_op_info *opinfo = NULL;
608 Debug( LDAP_DEBUG_TRACE, "=> bdb_search\n", 0, 0, 0);
609 attrs = sop->oq_search.rs_attrs;
611 opinfo = (struct bdb_op_info *) op->o_private;
613 if ( !IS_POST_SEARCH && !IS_PSEARCH &&
614 sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
615 struct slap_session_entry *sent;
616 if ( sop->o_sync_state.sid >= 0 ) {
617 LDAP_LIST_FOREACH( sent, &bdb->bi_session_list, se_link ) {
618 if ( sent->se_id == sop->o_sync_state.sid ) {
619 sop->o_sync_slog_size = sent->se_size;
626 /* psearch needs to be registered before refresh begins */
627 if ( !IS_POST_SEARCH && !IS_PSEARCH &&
628 sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
629 sop->o_refresh_in_progress = 1;
630 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
631 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
632 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
634 } else if ( !IS_POST_SEARCH && !IS_PSEARCH &&
635 sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST
636 && sop->o_sync_slog_size >= 0 )
638 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
639 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
640 if ( ps_list->o_sync_slog_size >= 0 ) {
641 if ( ps_list->o_sync_state.sid == sop->o_sync_state.sid ) {
649 if ( ps_list->o_sync_slog_omitcsn.bv_len != 0 ) {
650 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
651 if ( sop->o_sync_state.ctxcsn &&
652 sop->o_sync_state.ctxcsn->bv_val != NULL )
654 value_match( &match, slap_schema.si_ad_entryCSN, mr,
655 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
656 sop->o_sync_state.ctxcsn,
657 &ps_list->o_sync_slog_omitcsn,
663 sync_send_present_mode = 0;
666 sync_send_present_mode = 0;
668 } else if ( sop->o_sync_slog_size >= 0 ) {
669 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
671 sop->o_sync_state.sid = -1;
673 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
676 null_attr.an_desc = NULL;
677 null_attr.an_oc = NULL;
678 null_attr.an_oc_exclude = 0;
679 BER_BVZERO( &null_attr.an_name );
681 for( num_ctrls = 0; num_ctrls < SLAP_MAX_RESPONSE_CONTROLS; num_ctrls++ ) {
682 ctrls[num_ctrls] = NULL;
686 if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
688 attrs[0].an_desc = NULL;
689 attrs[0].an_oc = NULL;
690 attrs[0].an_oc_exclude = 0;
691 BER_BVZERO( &attrs[0].an_name );
694 manageDSAit = get_manageDSAit( sop );
696 /* Sync control overrides manageDSAit */
697 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
698 if ( manageDSAit == SLAP_NO_CONTROL ) {
699 manageDSAit = SLAP_CRITICAL_CONTROL;
701 } else if ( IS_PSEARCH ) {
702 if ( manageDSAit == SLAP_NO_CONTROL ) {
703 manageDSAit = SLAP_CRITICAL_CONTROL;
707 if ( opinfo && opinfo->boi_txn ) {
708 ltid = opinfo->boi_txn;
709 locker = TXN_ID( ltid );
711 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
717 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
722 if ( IS_POST_SEARCH ) {
725 candidates[1] = op->ors_post_search_id;
726 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );
730 if ( sop->o_req_ndn.bv_len == 0 ) {
731 /* DIT root special case */
732 ei_root.bei_e = &e_root;
733 ei_root.bei_parent = &ei_root;
734 e_root.e_private = &ei_root;
736 BER_BVSTR( &e_root.e_nname, "" );
737 BER_BVSTR( &e_root.e_name, "" );
739 rs->sr_err = LDAP_SUCCESS;
742 /* get entry with reader lock */
743 rs->sr_err = bdb_dn2entry( op, ltid, &sop->o_req_ndn, &ei,
755 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
757 LOCK_ID_FREE (bdb->bi_dbenv, locker );
759 case DB_LOCK_DEADLOCK:
760 case DB_LOCK_NOTGRANTED:
763 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
765 LOCK_ID_FREE (bdb->bi_dbenv, locker );
769 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
770 BDB_IDL_ZERO(candidates);
771 e = deref_base( op, rs, e, &matched, locker, &lock,
776 struct berval matched_dn = BER_BVNULL;
778 if ( matched != NULL ) {
780 ber_dupbv( &matched_dn, &matched->e_name );
782 erefs = is_entry_referral( matched )
783 ? get_entry_referrals( op, matched )
786 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
791 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
792 &sop->o_req_dn, sop->oq_search.rs_scope );
793 ber_bvarray_free( erefs );
797 rs->sr_ref = referral_rewrite( default_referral,
798 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
801 rs->sr_err = LDAP_REFERRAL;
802 rs->sr_matched = matched_dn.bv_val;
803 send_ldap_result( sop, rs );
806 LOCK_ID_FREE (bdb->bi_dbenv, locker );
808 ber_bvarray_free( rs->sr_ref );
811 if ( matched_dn.bv_val ) {
812 ber_memfree( matched_dn.bv_val );
813 rs->sr_matched = NULL;
818 if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
819 /* entry is a referral, don't allow add */
820 struct berval matched_dn;
823 ber_dupbv( &matched_dn, &e->e_name );
824 erefs = get_entry_referrals( op, e );
826 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
830 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
831 &sop->o_req_dn, sop->oq_search.rs_scope );
832 ber_bvarray_free( erefs );
835 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
838 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
839 rs->sr_err = LDAP_REFERRAL;
840 rs->sr_matched = matched_dn.bv_val;
841 send_ldap_result( sop, rs );
844 LOCK_ID_FREE (bdb->bi_dbenv, locker );
845 ber_bvarray_free( rs->sr_ref );
847 ber_memfree( matched_dn.bv_val );
848 rs->sr_matched = NULL;
852 if ( get_assert( op ) &&
853 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
855 rs->sr_err = LDAP_ASSERTION_FAILED;
856 send_ldap_result( sop, rs );
860 /* compute it anyway; root does not use it */
861 stoptime = op->o_time + sop->ors_tlimit;
863 /* need normalized dn below */
864 ber_dupbv( &realbase, &e->e_nname );
866 /* Copy info to base, must free entry before accessing the database
867 * in search_candidates, to avoid deadlocks.
869 base.e_private = e->e_private;
870 base.e_nname = realbase;
873 if ( e != &e_root ) {
874 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
879 rs->sr_err = bdb_get_commit_csn( sop, rs, &search_context_csn,
880 locker, &ctxcsn_lock );
882 if ( rs->sr_err != LDAP_SUCCESS ) {
883 send_ldap_error( sop, rs, rs->sr_err,
884 "error in csn management in search" );
888 if ( sop->o_sync_mode != SLAP_SYNC_NONE &&
889 sop->o_sync_state.ctxcsn &&
890 sop->o_sync_state.ctxcsn->bv_val &&
891 ber_bvcmp( &sop->o_sync_state.ctxcsn[0], search_context_csn ) == 0 )
893 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
897 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );
900 /* select candidates */
901 if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
902 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
905 BDB_IDL_ZERO( candidates );
906 BDB_IDL_ZERO( scopes );
907 rs->sr_err = search_candidates( op, sop, rs, &base,
908 locker, candidates, scopes );
911 if ( !IS_PSEARCH && sop->o_sync_mode != SLAP_SYNC_NONE ) {
912 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
915 /* start cursor at beginning of candidates.
919 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
920 cursor = bdb_idl_search( candidates, ps_e->e_id );
921 if ( candidates[cursor] != ps_e->e_id ) {
922 rs->sr_err = LDAP_SUCCESS;
925 } else if ( ps_e->e_id < BDB_IDL_RANGE_FIRST( candidates ) ||
926 ps_e->e_id > BDB_IDL_RANGE_LAST( candidates ))
928 rs->sr_err = LDAP_SUCCESS;
932 candidates[1] = ps_e->e_id;
935 if ( candidates[0] == 0 ) {
936 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
942 /* if not root and candidates exceed to-be-checked entries, abort */
943 if ( sop->ors_limit /* isroot == FALSE */ &&
944 sop->ors_limit->lms_s_unchecked != -1 &&
945 BDB_IDL_N(candidates) > (unsigned) sop->ors_limit->lms_s_unchecked )
947 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
948 send_ldap_result( sop, rs );
949 rs->sr_err = LDAP_SUCCESS;
953 if ( sop->ors_limit == NULL /* isroot == TRUE */ ||
954 !sop->ors_limit->lms_s_pr_hide )
956 tentries = BDB_IDL_N(candidates);
959 if ( get_pagedresults( sop ) > SLAP_NO_CONTROL ) {
960 /* deferred cookie parsing */
961 rs->sr_err = parse_paged_cookie( sop, rs );
962 if ( rs->sr_err != LDAP_SUCCESS ) {
963 send_ldap_result( sop, rs );
967 if ( (ID)( sop->o_pagedresults_state.ps_cookie ) == 0 ) {
968 id = bdb_idl_first( candidates, &cursor );
971 if ( sop->o_pagedresults_size == 0 ) {
972 rs->sr_err = LDAP_SUCCESS;
973 rs->sr_text = "search abandoned by pagedResult size=0";
974 send_ldap_result( sop, rs );
977 for ( id = bdb_idl_first( candidates, &cursor );
979 id <= (ID)( sop->o_pagedresults_state.ps_cookie );
980 id = bdb_idl_next( candidates, &cursor ) )
986 if ( cursor == NOID ) {
987 Debug( LDAP_DEBUG_TRACE,
988 "bdb_search: no paged results candidates\n",
990 send_paged_response( sop, rs, &lastid, 0 );
992 rs->sr_err = LDAP_OTHER;
998 if (( sop->o_sync_mode & SLAP_SYNC_REFRESH ) || IS_PSEARCH ) {
1001 cookief.f_choice = LDAP_FILTER_AND;
1002 cookief.f_and = &csnfnot;
1003 cookief.f_next = NULL;
1005 csnfnot.f_choice = LDAP_FILTER_NOT;
1006 csnfnot.f_not = &csnfeq;
1007 csnfnot.f_next = &csnfand;
1009 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
1010 csnfeq.f_ava = &aa_eq;
1011 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
1012 if ( sop->o_sync_state.ctxcsn != NULL ) {
1013 csnfeq.f_av_value = *sop->o_sync_state.ctxcsn;
1015 csnfeq.f_av_value = slap_empty_bv;
1018 csnfand.f_choice = LDAP_FILTER_AND;
1019 csnfand.f_and = &csnfge;
1020 csnfand.f_next = NULL;
1022 csnfge.f_choice = LDAP_FILTER_GE;
1023 csnfge.f_ava = &aa_ge;
1024 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
1025 if ( sop->o_sync_state.ctxcsn != NULL ) {
1026 csnfge.f_av_value = *sop->o_sync_state.ctxcsn;
1028 csnfge.f_av_value = slap_empty_bv;
1031 if ( search_context_csn && !IS_PSEARCH ) {
1032 csnfge.f_next = &contextcsnand;
1034 contextcsnand.f_choice = LDAP_FILTER_AND;
1035 contextcsnand.f_and = &contextcsnle;
1036 contextcsnand.f_next = NULL;
1038 contextcsnle.f_choice = LDAP_FILTER_LE;
1039 contextcsnle.f_ava = &aa_le;
1040 contextcsnle.f_av_desc = slap_schema.si_ad_entryCSN;
1041 contextcsnle.f_av_value = *search_context_csn;
1042 contextcsnle.f_next = sop->oq_search.rs_filter;
1044 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
1045 if ( sop->o_sync_state.ctxcsn &&
1046 sop->o_sync_state.ctxcsn->bv_val != NULL )
1048 value_match( &match, slap_schema.si_ad_entryCSN, mr,
1049 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1050 &sop->o_sync_state.ctxcsn[0], search_context_csn,
1055 no_sync_state_change = ( match >= 0 );
1057 csnfge.f_next = sop->oq_search.rs_filter;
1063 for ( id = bdb_idl_first( candidates, &cursor );
1064 id != NOID && !no_sync_state_change;
1065 id = bdb_idl_next( candidates, &cursor ) )
1072 if ( !IS_POST_SEARCH ) {
1073 idhole = (ID*) avl_find( sop->o_psearch_finished,
1074 (caddr_t)&id, bdb_pfid_cmp );
1076 avl_delete( &sop->o_psearch_finished,
1077 (caddr_t)idhole, bdb_pfid_cmp );
1078 sop->o_tmpfree( idhole, sop->o_tmpmemctx );
1082 if ( sop->o_refresh_in_progress ) {
1083 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
1084 pce = LDAP_TAILQ_FIRST( &sop->o_ps_pre_candidates );
1085 while ( pce && pce->pc_sent ) {
1086 pce = LDAP_TAILQ_NEXT( pce, pc_link );
1088 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
1091 if ( BDB_IDL_IS_RANGE( candidates ) ) {
1092 if ( pce->pc_id >= candidates[1] &&
1093 pce->pc_id <= candidates[2] &&
1094 pce->pc_id > cursor-1 ) {
1097 avl_insert( &sop->o_psearch_finished,
1098 (caddr_t)bdb_id_dup( sop, &pce->pc_id ),
1099 bdb_pfid_cmp, avl_dup_error );
1104 pos = bdb_idl_search(candidates, pce->pc_id);
1105 if ( pos > cursor-1 && pos <= candidates[0] ) {
1108 avl_insert( &sop->o_psearch_finished,
1109 (caddr_t)bdb_id_dup( sop, &pce->pc_id ),
1110 bdb_pfid_cmp, avl_dup_error );
1119 /* check for abandon */
1120 if ( sop->o_abandon ) {
1122 bdb_drop_psearch( sop, sop->o_msgid );
1124 rs->sr_err = LDAP_SUCCESS;
1128 if ( sop->o_cancel ) {
1129 assert( sop->o_cancel == SLAP_CANCEL_REQ );
1130 rs->sr_err = LDAP_CANCELLED;
1131 send_ldap_result( sop, rs );
1132 sop->o_cancel = SLAP_CANCEL_ACK;
1133 rs->sr_err = LDAP_SUCCESS;
1137 /* check time limit */
1138 if ( sop->ors_tlimit != SLAP_NO_LIMIT
1139 && slap_get_time() > stoptime )
1141 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1142 rs->sr_ref = rs->sr_v2ref;
1143 send_ldap_result( sop, rs );
1144 rs->sr_err = LDAP_SUCCESS;
1150 /* get the entry with reader lock */
1152 rs->sr_err = bdb_cache_find_id( op, ltid,
1153 id, &ei, 0, locker, &lock );
1155 if (rs->sr_err == LDAP_BUSY) {
1156 rs->sr_text = "ldap server busy";
1157 send_ldap_result( sop, rs );
1160 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
1161 || rs->sr_err == DB_LOCK_NOTGRANTED )
1163 goto id2entry_retry;
1166 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
1173 if ( IS_POST_SEARCH ) {
1174 /* send LDAP_SYNC_DELETE */
1175 rs->sr_entry = e = ps_e;
1176 goto post_search_no_entry;
1177 } else if( !BDB_IDL_IS_RANGE(candidates) ) {
1178 /* only complain for non-range IDLs */
1179 Debug( LDAP_DEBUG_TRACE,
1180 "bdb_search: candidate %ld not found\n",
1192 #ifdef BDB_SUBENTRIES
1193 /* FIXME: send all but syncrepl */
1195 if ( !is_sync_protocol( sop ) )
1198 if ( is_entry_subentry( e ) ) {
1199 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
1200 if(!get_subentries_visibility( sop )) {
1201 /* only subentries are visible */
1205 } else if ( get_subentries( sop ) &&
1206 !get_subentries_visibility( sop ))
1208 /* only subentries are visible */
1212 } else if ( get_subentries_visibility( sop )) {
1213 /* only subentries are visible */
1217 #endif /* BDB_SUBENTRIES */
1219 /* Does this candidate actually satisfy the search scope?
1221 * Note that we don't lock access to the bei_parent pointer.
1222 * Since only leaf nodes can be deleted, the parent of any
1223 * node will always be a valid node. Also since we have
1224 * a Read lock on the data, it cannot be renamed out of the
1225 * scope while we are looking at it, and unless we're using
1226 * BDB_HIER, its parents cannot be moved either.
1228 switch( sop->ors_scope ) {
1229 case LDAP_SCOPE_BASE:
1230 /* This is always true, yes? */
1231 if ( id == base.e_id ) scopeok = 1;
1234 case LDAP_SCOPE_ONELEVEL:
1235 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
1238 #ifdef LDAP_SCOPE_CHILDREN
1239 case LDAP_SCOPE_CHILDREN:
1240 if ( id == base.e_id ) break;
1243 case LDAP_SCOPE_SUBTREE: {
1245 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
1246 if ( tmp->bei_id == base.e_id ) {
1255 /* aliases were already dereferenced in candidate list */
1256 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
1257 /* but if the search base is an alias, and we didn't
1258 * deref it when finding, return it.
1260 if ( is_entry_alias(e) &&
1261 ((sop->ors_deref & LDAP_DEREF_FINDING) ||
1262 !bvmatch(&e->e_nname, &op->o_req_ndn)))
1267 /* scopes is only non-empty for onelevel or subtree */
1268 if ( !scopeok && BDB_IDL_N(scopes) ) {
1270 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
1271 x = bdb_idl_search( scopes, e->e_id );
1272 if ( scopes[x] == e->e_id ) scopeok = 1;
1274 /* subtree, walk up the tree */
1275 EntryInfo *tmp = BEI(e);
1276 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
1277 x = bdb_idl_search( scopes, tmp->bei_id );
1278 if ( scopes[x] == tmp->bei_id ) {
1288 /* Not in scope, ignore it */
1289 if ( !IS_POST_SEARCH && !scopeok ) {
1290 Debug( LDAP_DEBUG_TRACE,
1291 "bdb_search: %ld scope not okay\n",
1297 * if it's a referral, add it to the list of referrals. only do
1298 * this for non-base searches, and don't check the filter
1299 * explicitly here since it's only a candidate anyway.
1301 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1302 && is_entry_referral( e ) )
1304 BerVarray erefs = get_entry_referrals( sop, e );
1305 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1306 sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1307 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1309 send_search_reference( sop, rs );
1311 ber_bvarray_free( rs->sr_ref );
1312 ber_bvarray_free( erefs );
1318 if ( !manageDSAit && is_entry_glue( e )) {
1322 /* if it matches the filter and scope, send it */
1324 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1325 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1327 rs->sr_err = LDAP_COMPARE_TRUE;
1331 if ( !IS_POST_SEARCH ) {
1332 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1333 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1334 rs->sr_err = test_filter( sop, rs->sr_entry,
1336 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1337 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1338 if ( no_sync_state_change ) {
1339 Debug( LDAP_DEBUG_TRACE,
1341 "error in context csn management\n",
1344 entry_sync_state = LDAP_SYNC_ADD;
1347 if ( no_sync_state_change ) {
1350 entry_sync_state = LDAP_SYNC_PRESENT;
1354 rs->sr_err = test_filter( sop,
1355 rs->sr_entry, sop->oq_search.rs_filter );
1359 rs->sr_err = test_filter( sop,
1360 rs->sr_entry, sop->oq_search.rs_filter );
1362 rs->sr_err = LDAP_COMPARE_TRUE;
1367 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1368 /* check size limit */
1369 if ( --sop->ors_slimit == -1 &&
1370 sop->o_sync_slog_size == -1 )
1373 bdb_cache_return_entry_r( bdb->bi_dbenv,
1374 &bdb->bi_cache, e, &lock );
1377 rs->sr_entry = NULL;
1378 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1379 rs->sr_ref = rs->sr_v2ref;
1380 send_ldap_result( sop, rs );
1381 rs->sr_err = LDAP_SUCCESS;
1385 if ( get_pagedresults(sop) > SLAP_NO_CONTROL ) {
1386 if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
1387 send_paged_response( sop, rs, &lastid, tentries );
1397 if (IS_PSEARCH || IS_POST_SEARCH) {
1398 int premodify_found = 0;
1400 if ( IS_POST_SEARCH ||
1401 ps_type == LDAP_PSEARCH_BY_ADD ||
1402 ps_type == LDAP_PSEARCH_BY_DELETE ||
1403 ps_type == LDAP_PSEARCH_BY_MODIFY ||
1404 ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1406 if ( !IS_POST_SEARCH &&
1407 ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1408 struct psid_entry* psid_e;
1409 LDAP_LIST_FOREACH( psid_e,
1410 &op->o_pm_list, ps_link)
1412 if( psid_e->ps_op == sop ) {
1413 premodify_found = 1;
1414 LDAP_LIST_REMOVE(psid_e, ps_link);
1418 if (psid_e != NULL) free (psid_e);
1421 if ( IS_POST_SEARCH ) {
1423 entry_sync_state = LDAP_SYNC_ADD;
1425 post_search_no_entry:
1426 entry_sync_state = LDAP_SYNC_DELETE;
1428 } else if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1429 entry_sync_state = LDAP_SYNC_ADD;
1430 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1431 entry_sync_state = LDAP_SYNC_DELETE;
1432 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1433 if ( premodify_found ) {
1434 entry_sync_state = LDAP_SYNC_MODIFY;
1436 entry_sync_state = LDAP_SYNC_ADD;
1438 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT ) {
1439 entry_sync_state = LDAP_SYNC_DELETE;
1441 rs->sr_err = LDAP_OTHER;
1445 if ( sop->o_sync_slog_size != -1 ) {
1446 if ( entry_sync_state == LDAP_SYNC_DELETE ) {
1447 result = slap_add_session_log( op, sop, e );
1452 struct berval cookie;
1453 slap_compose_sync_cookie( sop, &cookie,
1455 sop->o_sync_state.sid,
1456 sop->o_sync_state.rid );
1457 rs->sr_err = slap_build_sync_state_ctrl(
1458 sop, rs, e, entry_sync_state, ctrls,
1459 num_ctrls++, 1, &cookie );
1460 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1461 if (!(IS_POST_SEARCH &&
1462 entry_sync_state == LDAP_SYNC_DELETE)) {
1463 rs->sr_attrs = attrs;
1465 rs->sr_attrs = NULL;
1467 rs->sr_operational_attrs = NULL;
1468 rs->sr_ctrls = ctrls;
1470 result = send_search_entry( sop, rs );
1471 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1473 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1475 slap_sl_free( ctrls[--num_ctrls],
1477 ctrls[num_ctrls] = NULL;
1478 rs->sr_ctrls = NULL;
1481 } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1482 struct psid_entry* psid_e;
1483 psid_e = (struct psid_entry *) ch_calloc(1,
1484 sizeof(struct psid_entry));
1485 psid_e->ps_op = sop;
1486 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1490 Debug( LDAP_DEBUG_TRACE,
1491 "bdb_search: invalid ps_type (%d) \n",
1496 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1497 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1498 rs->sr_err = slap_build_sync_state_ctrl(
1499 sop, rs, e, entry_sync_state, ctrls,
1500 num_ctrls++, 0, NULL );
1501 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1502 rs->sr_ctrls = ctrls;
1503 rs->sr_attrs = sop->oq_search.rs_attrs;
1504 rs->sr_operational_attrs = NULL;
1506 result = send_search_entry( sop, rs );
1508 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1510 slap_sl_free( ctrls[--num_ctrls],
1512 ctrls[num_ctrls] = NULL;
1513 rs->sr_ctrls = NULL;
1515 } else { /* PRESENT */
1516 if ( sync_send_present_mode ) {
1517 result = slap_build_syncUUID_set( sop,
1519 if ( result <= 0 ) {
1523 if ( syncUUID_set_cnt ==
1524 SLAP_SYNCUUID_SET_SIZE )
1526 rs->sr_err = LDAP_SUCCESS;
1527 rs->sr_rspoid = LDAP_SYNC_INFO;
1528 rs->sr_ctrls = NULL;
1529 result = slap_send_syncinfo( sop, rs,
1530 LDAP_TAG_SYNC_ID_SET,
1531 NULL, 0, syncUUID_set, 0 );
1532 if ( result != LDAP_SUCCESS ) {
1535 ber_bvarray_free_x( syncUUID_set,
1537 syncUUID_set = NULL;
1538 syncUUID_set_cnt = 0;
1548 rs->sr_attrs = sop->oq_search.rs_attrs;
1549 rs->sr_operational_attrs = NULL;
1550 rs->sr_ctrls = NULL;
1552 rs->sr_err = LDAP_SUCCESS;
1553 result = send_search_entry( sop, rs );
1558 case 0: /* entry sent ok */
1560 case 1: /* entry not sent */
1562 case -1: /* connection closed */
1564 bdb_cache_return_entry_r(bdb->bi_dbenv,
1565 &bdb->bi_cache, e, &lock);
1568 rs->sr_entry = NULL;
1569 rs->sr_err = LDAP_OTHER;
1575 Debug( LDAP_DEBUG_TRACE,
1576 "bdb_search: %ld does not match filter\n",
1582 /* free reader lock */
1584 if (!(IS_POST_SEARCH &&
1585 entry_sync_state == LDAP_SYNC_DELETE)) {
1586 bdb_cache_return_entry_r( bdb->bi_dbenv,
1587 &bdb->bi_cache, e , &lock );
1588 if ( sop->o_nocaching ) {
1589 bdb_cache_delete_entry( bdb, ei, locker, &lock );
1594 rs->sr_entry = NULL;
1597 if ( sop->o_refresh_in_progress ) {
1603 ldap_pvt_thread_yield();
1606 if ( syncUUID_set_cnt > 0 ) {
1607 rs->sr_err = LDAP_SUCCESS;
1608 rs->sr_rspoid = LDAP_SYNC_INFO;
1609 rs->sr_ctrls = NULL;
1610 slap_send_syncinfo( sop, rs, LDAP_TAG_SYNC_ID_SET,
1611 NULL, 0, syncUUID_set, 0 );
1612 ber_bvarray_free_x( syncUUID_set, sop->o_tmpmemctx );
1613 syncUUID_set_cnt = 0;
1617 if (!IS_PSEARCH && !IS_POST_SEARCH) {
1618 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1619 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1620 struct berval cookie;
1621 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1622 sop->o_sync_state.sid, sop->o_sync_state.rid );
1624 if ( sync_send_present_mode ) {
1625 rs->sr_err = LDAP_SUCCESS;
1626 rs->sr_rspoid = LDAP_SYNC_INFO;
1627 rs->sr_ctrls = NULL;
1628 slap_send_syncinfo( sop, rs,
1629 LDAP_TAG_SYNC_REFRESH_PRESENT, &cookie, 1, NULL, 0 );
1632 if ( !no_sync_state_change ) {
1634 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1635 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1638 if ( ps_list->o_sync_slog_size > 0 ) {
1639 if ( ps_list->o_sync_state.sid ==
1640 sop->o_sync_state.sid )
1649 rs->sr_err = LDAP_SUCCESS;
1650 rs->sr_rspoid = NULL;
1651 rs->sr_ctrls = NULL;
1652 slap_send_session_log( op, ps_list, rs );
1654 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1657 rs->sr_err = LDAP_SUCCESS;
1658 rs->sr_rspoid = LDAP_SYNC_INFO;
1659 rs->sr_ctrls = NULL;
1660 slap_send_syncinfo( sop, rs,
1661 LDAP_TAG_SYNC_REFRESH_DELETE, &cookie, 1, NULL, 0 );
1664 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1667 /* refreshOnly mode */
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 slap_build_sync_done_ctrl( sop, rs, ctrls,
1674 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1677 if ( !no_sync_state_change ) {
1679 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1680 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1683 if ( ps_list->o_sync_slog_size > 0 ) {
1684 if ( ps_list->o_sync_state.sid ==
1685 sop->o_sync_state.sid ) {
1693 slap_send_session_log( op, ps_list, rs );
1695 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1698 slap_build_sync_done_ctrl( sop, rs, ctrls,
1699 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_DELETES );
1702 rs->sr_ctrls = ctrls;
1703 rs->sr_ref = rs->sr_v2ref;
1704 rs->sr_err = (rs->sr_v2ref == NULL)
1705 ? LDAP_SUCCESS : LDAP_REFERRAL;
1706 rs->sr_rspoid = NULL;
1707 send_ldap_result( sop, rs );
1708 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1709 slap_sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val,
1712 slap_sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1713 ctrls[num_ctrls] = NULL;
1714 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1718 rs->sr_ctrls = NULL;
1719 rs->sr_ref = rs->sr_v2ref;
1720 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1721 rs->sr_rspoid = NULL;
1722 if ( get_pagedresults(sop) > SLAP_NO_CONTROL ) {
1723 send_paged_response( sop, rs, NULL, 0 );
1725 send_ldap_result( sop, rs );
1730 if ( sop->o_refresh_in_progress ) {
1731 sop->o_refresh_in_progress = 0;
1734 rs->sr_err = LDAP_SUCCESS;
1737 if ( sop->o_psearch_finished ) {
1738 avl_free( sop->o_psearch_finished, ch_free );
1741 if( !IS_PSEARCH && e != NULL ) {
1742 /* free reader lock */
1743 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1747 LOCK_ID_FREE( bdb->bi_dbenv, locker );
1749 ber_bvfree( search_context_csn );
1751 if( rs->sr_v2ref ) {
1752 ber_bvarray_free( rs->sr_v2ref );
1753 rs->sr_v2ref = NULL;
1755 if( realbase.bv_val ) ch_free( realbase.bv_val );
1761 static int base_candidate(
1766 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1767 e->e_nname.bv_val, (long) e->e_id, 0);
1774 /* Look for "objectClass Present" in this filter.
1775 * Also count depth of filter tree while we're at it.
1777 static int oc_filter(
1786 if( cur > *max ) *max = cur;
1788 switch( f->f_choice ) {
1789 case LDAP_FILTER_PRESENT:
1790 if (f->f_desc == slap_schema.si_ad_objectClass) {
1795 case LDAP_FILTER_AND:
1796 case LDAP_FILTER_OR:
1798 for ( f=f->f_and; f; f=f->f_next ) {
1799 (void) oc_filter(f, cur, max);
1809 static void search_stack_free( void *key, void *data )
1811 ber_memfree_x(data, NULL);
1814 static void *search_stack( Operation *op )
1816 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1819 if ( op->o_threadctx ) {
1820 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1823 ret = bdb->bi_search_stack;
1827 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1829 if ( op->o_threadctx ) {
1830 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1831 ret, search_stack_free );
1833 bdb->bi_search_stack = ret;
1839 static int search_candidates(
1848 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1850 Filter f, rf, xf, nf;
1852 AttributeAssertion aa_ref;
1853 #ifdef BDB_SUBENTRIES
1855 AttributeAssertion aa_subentry;
1859 * This routine takes as input a filter (user-filter)
1860 * and rewrites it as follows:
1861 * (&(scope=DN)[(objectClass=subentry)]
1862 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1865 Debug(LDAP_DEBUG_TRACE,
1866 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1867 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1869 xf.f_or = op->oq_search.rs_filter;
1870 xf.f_choice = LDAP_FILTER_OR;
1873 /* If the user's filter uses objectClass=*,
1874 * these clauses are redundant.
1876 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1877 && !get_subentries_visibility(op)
1878 && !is_sync_protocol(op) )
1880 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1881 /* match referral objects */
1882 struct berval bv_ref = BER_BVC( "referral" );
1883 rf.f_choice = LDAP_FILTER_EQUALITY;
1885 rf.f_av_desc = slap_schema.si_ad_objectClass;
1886 rf.f_av_value = bv_ref;
1887 rf.f_next = xf.f_or;
1894 f.f_choice = LDAP_FILTER_AND;
1896 /* Dummy; we compute scope separately now */
1897 nf.f_choice = SLAPD_FILTER_COMPUTED;
1898 nf.f_result = LDAP_SUCCESS;
1899 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1900 ? op->oq_search.rs_filter : &xf ;
1901 /* Filter depth increased again, adding dummy clause */
1904 #ifdef BDB_SUBENTRIES
1905 if( get_subentries_visibility( op ) ) {
1906 struct berval bv_subentry = BER_BVC( "SUBENTRY" );
1907 sf.f_choice = LDAP_FILTER_EQUALITY;
1908 sf.f_ava = &aa_subentry;
1909 sf.f_av_desc = slap_schema.si_ad_objectClass;
1910 sf.f_av_value = bv_subentry;
1911 sf.f_next = nf.f_next;
1916 /* Allocate IDL stack, plus 1 more for former tmp */
1917 if ( depth+1 > bdb->bi_search_stack_depth ) {
1918 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1920 stack = search_stack( stackop );
1923 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1924 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1926 rc = bdb_dn2idl( op, e, ids, stack );
1929 if ( rc == LDAP_SUCCESS ) {
1930 rc = bdb_filter_candidates( op, &f, ids,
1931 stack, stack+BDB_IDL_UM_SIZE );
1934 if ( depth+1 > bdb->bi_search_stack_depth ) {
1939 Debug(LDAP_DEBUG_TRACE,
1940 "bdb_search_candidates: failed (rc=%d)\n",
1944 Debug(LDAP_DEBUG_TRACE,
1945 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1947 (long) BDB_IDL_FIRST(ids),
1948 (long) BDB_IDL_LAST(ids) );
1955 parse_paged_cookie( Operation *op, SlapReply *rs )
1958 int rc = LDAP_SUCCESS;
1962 struct berval cookie = BER_BVNULL;
1964 /* this function must be invoked only if the pagedResults
1965 * control has been detected, parsed and partially checked
1966 * by the frontend */
1967 assert( get_pagedresults( op ) > SLAP_NO_CONTROL );
1969 /* look for the appropriate ctrl structure */
1970 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
1971 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
1977 if ( c[0] == NULL ) {
1978 rs->sr_text = "missing pagedResults control";
1979 return LDAP_PROTOCOL_ERROR;
1982 /* Already tested by frontend */
1983 assert( c[0]->ldctl_value.bv_len > 0 );
1985 if ( c[0]->ldctl_value.bv_len == 0 ) {
1986 rs->sr_text = "paged results control value is empty (or absent)";
1987 return LDAP_PROTOCOL_ERROR;
1991 /* Parse the control value
1992 * realSearchControlValue ::= SEQUENCE {
1993 * size INTEGER (0..maxInt),
1994 * -- requested page size from client
1995 * -- result set size estimate from server
1996 * cookie OCTET STRING
1999 ber = ber_init( &c[0]->ldctl_value );
2000 if ( ber == NULL ) {
2001 rs->sr_text = "internal error";
2005 tag = ber_scanf( ber, "{im}", &size, &cookie );
2007 /* Already tested by frontend */
2008 assert( tag != LBER_ERROR );
2010 if ( tag == LBER_ERROR ) {
2011 rs->sr_text = "paged results control could not be decoded";
2012 rc = LDAP_PROTOCOL_ERROR;
2017 /* Already tested by frontend */
2018 assert( size >= 0 );
2021 rs->sr_text = "paged results control size invalid";
2022 rc = LDAP_PROTOCOL_ERROR;
2027 /* cookie decoding/checks deferred to backend... */
2028 if ( cookie.bv_len ) {
2029 PagedResultsCookie reqcookie;
2030 if( cookie.bv_len != sizeof( reqcookie ) ) {
2032 rs->sr_text = "paged results cookie is invalid";
2033 rc = LDAP_PROTOCOL_ERROR;
2037 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
2039 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
2041 rs->sr_text = "paged results cookie is invalid";
2042 rc = LDAP_PROTOCOL_ERROR;
2045 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
2046 rs->sr_text = "paged results cookie is invalid or old";
2047 rc = LDAP_UNWILLING_TO_PERFORM;
2052 /* Initial request. Initialize state. */
2054 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
2055 /* There's another pagedResults control on the
2056 * same connection; reject new pagedResults controls
2057 * (allowed by RFC2696) */
2058 rs->sr_text = "paged results cookie unavailable; try later";
2059 rc = LDAP_UNWILLING_TO_PERFORM;
2063 op->o_pagedresults_state.ps_cookie = 0;
2064 op->o_pagedresults_state.ps_count = 0;
2068 (void)ber_free( ber, 1 );
2074 send_paged_response(
2080 LDAPControl ctrl, *ctrls[2];
2081 BerElementBuffer berbuf;
2082 BerElement *ber = (BerElement *)&berbuf;
2083 PagedResultsCookie respcookie;
2084 struct berval cookie;
2086 Debug(LDAP_DEBUG_ARGS,
2087 "send_paged_response: lastid=0x%08lx nentries=%d\n",
2088 lastid ? *lastid : 0, rs->sr_nentries, NULL );
2090 BER_BVZERO( &ctrl.ldctl_value );
2094 ber_init2( ber, NULL, LBER_USE_DER );
2097 respcookie = ( PagedResultsCookie )(*lastid);
2098 cookie.bv_len = sizeof( respcookie );
2099 cookie.bv_val = (char *)&respcookie;
2102 respcookie = ( PagedResultsCookie )0;
2103 BER_BVSTR( &cookie, "" );
2106 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
2107 op->o_conn->c_pagedresults_state.ps_count =
2108 op->o_pagedresults_state.ps_count + rs->sr_nentries;
2110 /* return size of 0 -- no estimate */
2111 ber_printf( ber, "{iO}", 0, &cookie );
2113 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
2117 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
2118 ctrls[0]->ldctl_iscritical = 0;
2120 rs->sr_ctrls = ctrls;
2121 rs->sr_err = LDAP_SUCCESS;
2122 send_ldap_result( op, rs );
2123 rs->sr_ctrls = NULL;
2126 (void) ber_free_buf( ber );
2130 bdb_pfid_cmp( const void *v_id1, const void *v_id2 )
2132 const ID *p1 = v_id1, *p2 = v_id2;
2137 bdb_id_dup( Operation *op, ID *id )
2140 new = ch_malloc( sizeof(ID) );