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 )
344 void *saved_tmpmemctx;
346 ps = bdb_drop_psearch( op, op->oq_abandon.rs_msgid );
348 saved_tmpmemctx = ps->o_tmpmemctx;
350 if (!BER_BVISNULL(&ps->o_req_dn)) {
351 slap_sl_free(ps->o_req_dn.bv_val, ps->o_tmpmemctx );
353 if (!BER_BVISNULL(&ps->o_req_ndn)) {
354 slap_sl_free(ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
356 if (!BER_BVISNULL(&ps->ors_filterstr)) {
357 ps->o_tmpfree(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
359 if (ps->ors_filter != NULL) {
360 filter_free_x(ps, ps->ors_filter);
362 if (ps->ors_attrs != NULL) {
363 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
368 if ( saved_tmpmemctx ) {
369 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
374 return LDAP_UNAVAILABLE;
378 bdb_cancel( Operation *op, SlapReply *rs )
381 void *saved_tmpmemctx;
383 ps = bdb_drop_psearch( op, op->oq_cancel.rs_msgid );
385 saved_tmpmemctx = ps->o_tmpmemctx;
387 rs->sr_err = LDAP_CANCELLED;
388 send_ldap_result( ps, rs );
390 if (!BER_BVISNULL(&ps->o_req_dn)) {
391 slap_sl_free(ps->o_req_dn.bv_val, ps->o_tmpmemctx );
393 if (!BER_BVISNULL(&ps->o_req_ndn)) {
394 slap_sl_free(ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
396 if (!BER_BVISNULL(&ps->ors_filterstr)) {
397 ps->o_tmpfree(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
399 if (ps->ors_filter != NULL) {
400 filter_free_x(ps, ps->ors_filter);
402 if (ps->ors_attrs != NULL) {
403 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
408 if ( saved_tmpmemctx ) {
409 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
414 return LDAP_UNAVAILABLE;
417 int bdb_search( Operation *op, SlapReply *rs )
420 struct pc_entry *pce = NULL;
421 struct pc_entry *tmp_pce = NULL;
425 ps_e.e_private = NULL;
426 ldap_pvt_thread_mutex_init( &op->o_pcmutex );
427 LDAP_TAILQ_INIT( &op->o_ps_pre_candidates );
428 LDAP_TAILQ_INIT( &op->o_ps_post_candidates );
430 op->ors_post_search_id = NOID;
431 rc = bdb_do_search( op, rs, op, NULL, 0 );
433 ldap_pvt_thread_mutex_lock( &op->o_pcmutex );
434 pce = LDAP_TAILQ_FIRST( &op->o_ps_post_candidates );
435 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
437 while ( rc == LDAP_SUCCESS && pce &&
438 op->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
440 ps_e.e_id = op->ors_post_search_id = pce->pc_id;
441 if ( op->o_sync_csn.bv_val ) {
442 ch_free( op->o_sync_csn.bv_val );
443 op->o_sync_csn.bv_val = NULL;
445 ber_dupbv( &op->o_sync_csn, &pce->pc_csn );
446 ber_dupbv( &ps_e.e_name, &pce->pc_ename );
447 ber_dupbv( &ps_e.e_nname, &pce->pc_enname );
448 a = ch_calloc( 1, sizeof( Attribute ));
449 a->a_desc = slap_schema.si_ad_entryUUID;
450 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
451 ber_dupbv( &a->a_vals[0], &pce->pc_entryUUID );
452 a->a_nvals = a->a_vals;
456 rc = bdb_do_search( op, rs, op, &ps_e, 0 );
459 ldap_pvt_thread_mutex_lock( &op->o_pcmutex );
460 pce = LDAP_TAILQ_NEXT( pce, pc_link );
461 LDAP_TAILQ_REMOVE( &op->o_ps_post_candidates, tmp_pce, pc_link );
462 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
464 ch_free( tmp_pce->pc_csn.bv_val );
465 ch_free( tmp_pce->pc_entryUUID.bv_val );
466 ch_free( tmp_pce->pc_ename.bv_val );
467 ch_free( tmp_pce->pc_enname.bv_val );
469 entry_clean( &ps_e );
474 #define BDB_PSEARCH_MAX_WAIT 3
475 int bdb_psearch( Operation *op, SlapReply *rs, Operation *sop,
476 Entry *ps_e, int ps_type )
479 struct pc_entry *pce = NULL;
480 struct pc_entry *p = NULL;
483 op->ors_post_search_id = NOID;
486 case LDAP_PSEARCH_BY_PREMODIFY:
487 case LDAP_PSEARCH_BY_PREDELETE:
489 if ( !op->o_ps_send_wait ) {
490 if ( sop->o_refresh_in_progress ) {
491 pce = (struct pc_entry *) ch_calloc(
492 1, sizeof( struct pc_entry ));
493 pce->pc_id = ps_e->e_id;
494 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
495 if ( LDAP_TAILQ_EMPTY( &sop->o_ps_pre_candidates )) {
496 LDAP_TAILQ_INSERT_HEAD(
497 &sop->o_ps_pre_candidates, pce, pc_link );
499 LDAP_TAILQ_FOREACH( p,
500 &sop->o_ps_pre_candidates, pc_link ) {
501 if ( p->pc_id > pce->pc_id )
506 LDAP_TAILQ_INSERT_BEFORE( p, pce, pc_link );
508 LDAP_TAILQ_INSERT_TAIL(
509 &sop->o_ps_pre_candidates,
513 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
515 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
519 pce = op->o_ps_send_wait;
522 /* Wait until refresh search send the entry */
523 while ( !pce->pc_sent ) {
524 if ( sop->o_refresh_in_progress ) {
525 if ( num_retries == BDB_PSEARCH_MAX_WAIT ) {
526 op->o_ps_send_wait = pce;
529 ldap_pvt_thread_yield();
530 bdb_trans_backoff( ++num_retries );
536 op->o_ps_send_wait = NULL;
538 if ( !sop->o_refresh_in_progress && !pce->pc_sent ) {
539 /* refresh ended without processing pce */
540 /* need to perform psearch for ps_e */
541 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
542 LDAP_TAILQ_REMOVE( &sop->o_ps_pre_candidates, pce, pc_link );
543 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
545 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
548 /* the pce entry was sent in the refresh phase */
549 if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
550 struct psid_entry* psid_e;
551 psid_e = (struct psid_entry *) ch_calloc(1,
552 sizeof(struct psid_entry));
554 LDAP_LIST_INSERT_HEAD( &op->o_pm_list, psid_e, ps_link );
557 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
558 LDAP_TAILQ_REMOVE( &sop->o_ps_pre_candidates, pce, pc_link );
559 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
564 case LDAP_PSEARCH_BY_DELETE:
565 case LDAP_PSEARCH_BY_SCOPEOUT:
566 case LDAP_PSEARCH_BY_ADD:
567 case LDAP_PSEARCH_BY_MODIFY:
568 ldap_pvt_thread_mutex_lock( &op->o_pcmutex );
569 if ( sop->o_refresh_in_progress ||
570 !LDAP_TAILQ_EMPTY( &sop->o_ps_post_candidates )) {
571 pce = (struct pc_entry *) ch_calloc( 1, sizeof( struct pc_entry ));
572 pce->pc_id = ps_e->e_id;
573 ber_dupbv( &pce->pc_csn, &op->o_sync_csn );
574 if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
576 for ( a = ps_e->e_attrs; a != NULL; a = a->a_next ) {
577 AttributeDescription *desc = a->a_desc;
578 if ( desc == slap_schema.si_ad_entryUUID ) {
579 ber_dupbv( &pce->pc_entryUUID, &a->a_nvals[0] );
583 ber_dupbv( &pce->pc_ename, &ps_e->e_name );
584 ber_dupbv( &pce->pc_enname, &ps_e->e_nname );
585 LDAP_TAILQ_INSERT_TAIL( &sop->o_ps_post_candidates, pce, pc_link );
586 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
588 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
589 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
594 Debug( LDAP_DEBUG_TRACE, "do_psearch: invalid psearch type\n",
600 /* For persistent searches, op is the currently executing operation,
601 * sop is the persistent search. For regular searches, sop = op.
604 bdb_do_search( Operation *op, SlapReply *rs, Operation *sop,
605 Entry *ps_e, int ps_type )
607 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
610 ID candidates[BDB_IDL_UM_SIZE];
611 ID scopes[BDB_IDL_DB_SIZE];
612 Entry *e = NULL, base, e_root = {0};
613 Entry *matched = NULL;
614 EntryInfo *ei, ei_root = {0};
615 struct berval realbase = BER_BVNULL;
619 AttributeName *attrs;
621 Filter contextcsnand, contextcsnle, cookief, csnfnot,
622 csnfeq, csnfand, csnfge;
623 AttributeAssertion aa_ge, aa_eq, aa_le;
624 struct berval *search_context_csn = NULL;
626 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
628 AttributeName uuid_attr[2];
630 int entry_sync_state = -1;
631 AttributeName null_attr;
632 int no_sync_state_change = 0;
634 u_int32_t locker = 0;
638 int sync_send_present_mode = 1;
644 struct pc_entry *pce = NULL;
645 BerVarray syncUUID_set = NULL;
646 int syncUUID_set_cnt = 0;
648 struct bdb_op_info *opinfo = NULL;
651 Debug( LDAP_DEBUG_TRACE, "=> bdb_search\n", 0, 0, 0);
652 attrs = sop->oq_search.rs_attrs;
654 opinfo = (struct bdb_op_info *) op->o_private;
656 if ( !IS_POST_SEARCH && !IS_PSEARCH &&
657 sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
658 struct slap_session_entry *sent;
659 if ( sop->o_sync_state.sid >= 0 ) {
660 LDAP_LIST_FOREACH( sent, &bdb->bi_session_list, se_link ) {
661 if ( sent->se_id == sop->o_sync_state.sid ) {
662 sop->o_sync_slog_size = sent->se_size;
669 /* psearch needs to be registered before refresh begins */
670 if ( !IS_POST_SEARCH && !IS_PSEARCH &&
671 sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
672 sop->o_refresh_in_progress = 1;
673 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
674 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
675 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
677 } else if ( !IS_POST_SEARCH && !IS_PSEARCH &&
678 sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST
679 && sop->o_sync_slog_size >= 0 )
681 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
682 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
683 if ( ps_list->o_sync_slog_size >= 0 ) {
684 if ( ps_list->o_sync_state.sid == sop->o_sync_state.sid ) {
692 if ( ps_list->o_sync_slog_omitcsn.bv_len != 0 ) {
693 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
694 if ( sop->o_sync_state.ctxcsn &&
695 sop->o_sync_state.ctxcsn->bv_val != NULL )
697 value_match( &match, slap_schema.si_ad_entryCSN, mr,
698 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
699 sop->o_sync_state.ctxcsn,
700 &ps_list->o_sync_slog_omitcsn,
706 sync_send_present_mode = 0;
709 sync_send_present_mode = 0;
711 } else if ( sop->o_sync_slog_size >= 0 ) {
712 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
714 sop->o_sync_state.sid = -1;
716 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
719 null_attr.an_desc = NULL;
720 null_attr.an_oc = NULL;
721 null_attr.an_oc_exclude = 0;
722 BER_BVZERO( &null_attr.an_name );
724 for( num_ctrls = 0; num_ctrls < SLAP_MAX_RESPONSE_CONTROLS; num_ctrls++ ) {
725 ctrls[num_ctrls] = NULL;
729 if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
731 attrs[0].an_desc = NULL;
732 attrs[0].an_oc = NULL;
733 attrs[0].an_oc_exclude = 0;
734 BER_BVZERO( &attrs[0].an_name );
737 manageDSAit = get_manageDSAit( sop );
739 /* Sync control overrides manageDSAit */
740 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
741 if ( manageDSAit == SLAP_NO_CONTROL ) {
742 manageDSAit = SLAP_CRITICAL_CONTROL;
744 } else if ( IS_PSEARCH ) {
745 if ( manageDSAit == SLAP_NO_CONTROL ) {
746 manageDSAit = SLAP_CRITICAL_CONTROL;
750 if ( opinfo && opinfo->boi_txn ) {
751 ltid = opinfo->boi_txn;
752 locker = TXN_ID( ltid );
754 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
760 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
765 if ( IS_POST_SEARCH ) {
768 candidates[1] = op->ors_post_search_id;
769 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );
773 if ( sop->o_req_ndn.bv_len == 0 ) {
774 /* DIT root special case */
775 ei_root.bei_e = &e_root;
776 ei_root.bei_parent = &ei_root;
777 e_root.e_private = &ei_root;
779 BER_BVSTR( &e_root.e_nname, "" );
780 BER_BVSTR( &e_root.e_name, "" );
782 rs->sr_err = LDAP_SUCCESS;
785 /* get entry with reader lock */
786 rs->sr_err = bdb_dn2entry( op, ltid, &sop->o_req_ndn, &ei,
798 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
800 LOCK_ID_FREE (bdb->bi_dbenv, locker );
802 case DB_LOCK_DEADLOCK:
803 case DB_LOCK_NOTGRANTED:
806 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
808 LOCK_ID_FREE (bdb->bi_dbenv, locker );
812 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
813 BDB_IDL_ZERO(candidates);
814 e = deref_base( op, rs, e, &matched, locker, &lock,
819 struct berval matched_dn = BER_BVNULL;
821 if ( matched != NULL ) {
823 ber_dupbv( &matched_dn, &matched->e_name );
825 erefs = is_entry_referral( matched )
826 ? get_entry_referrals( op, matched )
829 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
834 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
835 &sop->o_req_dn, sop->oq_search.rs_scope );
836 ber_bvarray_free( erefs );
840 rs->sr_ref = referral_rewrite( default_referral,
841 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
844 rs->sr_err = LDAP_REFERRAL;
845 rs->sr_matched = matched_dn.bv_val;
846 send_ldap_result( sop, rs );
849 LOCK_ID_FREE (bdb->bi_dbenv, locker );
851 ber_bvarray_free( rs->sr_ref );
854 if ( matched_dn.bv_val ) {
855 ber_memfree( matched_dn.bv_val );
856 rs->sr_matched = NULL;
861 if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
862 /* entry is a referral, don't allow add */
863 struct berval matched_dn;
866 ber_dupbv( &matched_dn, &e->e_name );
867 erefs = get_entry_referrals( op, e );
869 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
873 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
874 &sop->o_req_dn, sop->oq_search.rs_scope );
875 ber_bvarray_free( erefs );
878 Debug( LDAP_DEBUG_TRACE, "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, "bdb_search: no candidates\n",
985 /* if not root and candidates exceed to-be-checked entries, abort */
986 if ( sop->ors_limit /* isroot == FALSE */ &&
987 sop->ors_limit->lms_s_unchecked != -1 &&
988 BDB_IDL_N(candidates) > (unsigned) sop->ors_limit->lms_s_unchecked )
990 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
991 send_ldap_result( sop, rs );
992 rs->sr_err = LDAP_SUCCESS;
996 if ( sop->ors_limit == NULL /* isroot == TRUE */ ||
997 !sop->ors_limit->lms_s_pr_hide )
999 tentries = BDB_IDL_N(candidates);
1002 if ( get_pagedresults( sop ) > SLAP_NO_CONTROL ) {
1003 /* deferred cookie parsing */
1004 rs->sr_err = parse_paged_cookie( sop, rs );
1005 if ( rs->sr_err != LDAP_SUCCESS ) {
1006 send_ldap_result( sop, rs );
1010 if ( (ID)( sop->o_pagedresults_state.ps_cookie ) == 0 ) {
1011 id = bdb_idl_first( candidates, &cursor );
1014 if ( sop->o_pagedresults_size == 0 ) {
1015 rs->sr_err = LDAP_SUCCESS;
1016 rs->sr_text = "search abandoned by pagedResult size=0";
1017 send_ldap_result( sop, rs );
1020 for ( id = bdb_idl_first( candidates, &cursor );
1022 id <= (ID)( sop->o_pagedresults_state.ps_cookie );
1023 id = bdb_idl_next( candidates, &cursor ) )
1029 if ( cursor == NOID ) {
1030 Debug( LDAP_DEBUG_TRACE,
1031 "bdb_search: no paged results candidates\n",
1033 send_paged_response( sop, rs, &lastid, 0 );
1035 rs->sr_err = LDAP_OTHER;
1041 if (( sop->o_sync_mode & SLAP_SYNC_REFRESH ) || IS_PSEARCH ) {
1044 cookief.f_choice = LDAP_FILTER_AND;
1045 cookief.f_and = &csnfnot;
1046 cookief.f_next = NULL;
1048 csnfnot.f_choice = LDAP_FILTER_NOT;
1049 csnfnot.f_not = &csnfeq;
1050 csnfnot.f_next = &csnfand;
1052 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
1053 csnfeq.f_ava = &aa_eq;
1054 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
1055 if ( sop->o_sync_state.ctxcsn != NULL ) {
1056 csnfeq.f_av_value = *sop->o_sync_state.ctxcsn;
1058 csnfeq.f_av_value = slap_empty_bv;
1061 csnfand.f_choice = LDAP_FILTER_AND;
1062 csnfand.f_and = &csnfge;
1063 csnfand.f_next = NULL;
1065 csnfge.f_choice = LDAP_FILTER_GE;
1066 csnfge.f_ava = &aa_ge;
1067 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
1068 if ( sop->o_sync_state.ctxcsn != NULL ) {
1069 csnfge.f_av_value = *sop->o_sync_state.ctxcsn;
1071 csnfge.f_av_value = slap_empty_bv;
1074 if ( search_context_csn && !IS_PSEARCH ) {
1075 csnfge.f_next = &contextcsnand;
1077 contextcsnand.f_choice = LDAP_FILTER_AND;
1078 contextcsnand.f_and = &contextcsnle;
1079 contextcsnand.f_next = NULL;
1081 contextcsnle.f_choice = LDAP_FILTER_LE;
1082 contextcsnle.f_ava = &aa_le;
1083 contextcsnle.f_av_desc = slap_schema.si_ad_entryCSN;
1084 contextcsnle.f_av_value = *search_context_csn;
1085 contextcsnle.f_next = sop->oq_search.rs_filter;
1087 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
1088 if ( sop->o_sync_state.ctxcsn &&
1089 sop->o_sync_state.ctxcsn->bv_val != NULL )
1091 value_match( &match, slap_schema.si_ad_entryCSN, mr,
1092 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1093 &sop->o_sync_state.ctxcsn[0], search_context_csn,
1098 no_sync_state_change = ( match >= 0 );
1100 csnfge.f_next = sop->oq_search.rs_filter;
1106 for ( id = bdb_idl_first( candidates, &cursor );
1107 id != NOID && !no_sync_state_change;
1108 id = bdb_idl_next( candidates, &cursor ) )
1115 if ( !IS_POST_SEARCH ) {
1116 idhole = (ID*) avl_find( sop->o_psearch_finished,
1117 (caddr_t)&id, bdb_pfid_cmp );
1119 avl_delete( &sop->o_psearch_finished,
1120 (caddr_t)idhole, bdb_pfid_cmp );
1121 sop->o_tmpfree( idhole, sop->o_tmpmemctx );
1125 if ( sop->o_refresh_in_progress ) {
1126 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
1127 pce = LDAP_TAILQ_FIRST( &sop->o_ps_pre_candidates );
1128 while ( pce && pce->pc_sent ) {
1129 pce = LDAP_TAILQ_NEXT( pce, pc_link );
1131 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
1134 if ( BDB_IDL_IS_RANGE( candidates ) ) {
1135 if ( pce->pc_id >= candidates[1] &&
1136 pce->pc_id <= candidates[2] &&
1137 pce->pc_id > cursor-1 ) {
1140 avl_insert( &sop->o_psearch_finished,
1141 (caddr_t)bdb_id_dup( sop, &pce->pc_id ),
1142 bdb_pfid_cmp, avl_dup_error );
1147 pos = bdb_idl_search(candidates, pce->pc_id);
1148 if ( pos > cursor-1 && pos <= candidates[0] ) {
1151 avl_insert( &sop->o_psearch_finished,
1152 (caddr_t)bdb_id_dup( sop, &pce->pc_id ),
1153 bdb_pfid_cmp, avl_dup_error );
1162 /* check for abandon */
1163 if ( sop->o_abandon ) {
1165 bdb_drop_psearch( sop, sop->o_msgid );
1167 rs->sr_err = LDAP_SUCCESS;
1171 if ( sop->o_cancel ) {
1172 assert( sop->o_cancel == SLAP_CANCEL_REQ );
1173 rs->sr_err = LDAP_CANCELLED;
1174 send_ldap_result( sop, rs );
1175 sop->o_cancel = SLAP_CANCEL_ACK;
1176 rs->sr_err = LDAP_SUCCESS;
1180 /* check time limit */
1181 if ( sop->ors_tlimit != SLAP_NO_LIMIT
1182 && slap_get_time() > stoptime )
1184 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1185 rs->sr_ref = rs->sr_v2ref;
1186 send_ldap_result( sop, rs );
1187 rs->sr_err = LDAP_SUCCESS;
1193 /* get the entry with reader lock */
1195 rs->sr_err = bdb_cache_find_id( op, ltid,
1196 id, &ei, 0, locker, &lock );
1198 if (rs->sr_err == LDAP_BUSY) {
1199 rs->sr_text = "ldap server busy";
1200 send_ldap_result( sop, rs );
1203 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
1204 || rs->sr_err == DB_LOCK_NOTGRANTED )
1206 goto id2entry_retry;
1209 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
1216 if ( IS_POST_SEARCH ) {
1217 /* send LDAP_SYNC_DELETE */
1218 rs->sr_entry = e = ps_e;
1219 goto post_search_no_entry;
1220 } else if( !BDB_IDL_IS_RANGE(candidates) ) {
1221 /* only complain for non-range IDLs */
1222 Debug( LDAP_DEBUG_TRACE,
1223 "bdb_search: candidate %ld not found\n",
1235 #ifdef BDB_SUBENTRIES
1236 /* FIXME: send all but syncrepl */
1238 if ( !is_sync_protocol( sop ) )
1241 if ( is_entry_subentry( e ) ) {
1242 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
1243 if(!get_subentries_visibility( sop )) {
1244 /* only subentries are visible */
1248 } else if ( get_subentries( sop ) &&
1249 !get_subentries_visibility( sop ))
1251 /* only subentries are visible */
1255 } else if ( get_subentries_visibility( sop )) {
1256 /* only subentries are visible */
1260 #endif /* BDB_SUBENTRIES */
1262 /* Does this candidate actually satisfy the search scope?
1264 * Note that we don't lock access to the bei_parent pointer.
1265 * Since only leaf nodes can be deleted, the parent of any
1266 * node will always be a valid node. Also since we have
1267 * a Read lock on the data, it cannot be renamed out of the
1268 * scope while we are looking at it, and unless we're using
1269 * BDB_HIER, its parents cannot be moved either.
1271 switch( sop->ors_scope ) {
1272 case LDAP_SCOPE_BASE:
1273 /* This is always true, yes? */
1274 if ( id == base.e_id ) scopeok = 1;
1277 case LDAP_SCOPE_ONELEVEL:
1278 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
1281 #ifdef LDAP_SCOPE_CHILDREN
1282 case LDAP_SCOPE_CHILDREN:
1283 if ( id == base.e_id ) break;
1286 case LDAP_SCOPE_SUBTREE: {
1288 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
1289 if ( tmp->bei_id == base.e_id ) {
1297 /* aliases were already dereferenced in candidate list */
1298 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
1299 /* but if the search base is an alias, and we didn't
1300 * deref it when finding, return it.
1302 if ( is_entry_alias(e) &&
1303 ((sop->ors_deref & LDAP_DEREF_FINDING) ||
1304 !bvmatch(&e->e_nname, &op->o_req_ndn)))
1309 /* scopes is only non-empty for onelevel or subtree */
1310 if ( !scopeok && BDB_IDL_N(scopes) ) {
1312 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
1313 x = bdb_idl_search( scopes, e->e_id );
1314 if ( scopes[x] == e->e_id ) scopeok = 1;
1316 /* subtree, walk up the tree */
1317 EntryInfo *tmp = BEI(e);
1318 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
1319 x = bdb_idl_search( scopes, tmp->bei_id );
1320 if ( scopes[x] == tmp->bei_id ) {
1329 /* Not in scope, ignore it */
1330 if ( !IS_POST_SEARCH && !scopeok ) {
1331 Debug( LDAP_DEBUG_TRACE,
1332 "bdb_search: %ld scope not okay\n",
1338 * if it's a referral, add it to the list of referrals. only do
1339 * this for non-base searches, and don't check the filter
1340 * explicitly here since it's only a candidate anyway.
1342 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1343 && is_entry_referral( e ) )
1345 BerVarray erefs = get_entry_referrals( sop, e );
1346 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1347 sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1348 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1350 send_search_reference( sop, rs );
1352 ber_bvarray_free( rs->sr_ref );
1353 ber_bvarray_free( erefs );
1359 if ( !manageDSAit && is_entry_glue( e )) {
1363 /* if it matches the filter and scope, send it */
1365 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1366 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1368 rs->sr_err = LDAP_COMPARE_TRUE;
1372 if ( !IS_POST_SEARCH ) {
1373 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1374 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1375 rs->sr_err = test_filter( sop, rs->sr_entry,
1377 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1378 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1379 if ( no_sync_state_change ) {
1380 Debug( LDAP_DEBUG_TRACE,
1382 "error in context csn management\n",
1385 entry_sync_state = LDAP_SYNC_ADD;
1388 if ( no_sync_state_change ) {
1391 entry_sync_state = LDAP_SYNC_PRESENT;
1395 rs->sr_err = test_filter( sop,
1396 rs->sr_entry, sop->oq_search.rs_filter );
1400 rs->sr_err = test_filter( sop,
1401 rs->sr_entry, sop->oq_search.rs_filter );
1403 rs->sr_err = LDAP_COMPARE_TRUE;
1408 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1409 /* check size limit */
1410 if ( --sop->ors_slimit == -1 &&
1411 sop->o_sync_slog_size == -1 )
1414 bdb_cache_return_entry_r( bdb->bi_dbenv,
1415 &bdb->bi_cache, e, &lock );
1418 rs->sr_entry = NULL;
1419 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1420 rs->sr_ref = rs->sr_v2ref;
1421 send_ldap_result( sop, rs );
1422 rs->sr_err = LDAP_SUCCESS;
1426 if ( get_pagedresults(sop) > SLAP_NO_CONTROL ) {
1427 if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
1428 send_paged_response( sop, rs, &lastid, tentries );
1438 if (IS_PSEARCH || IS_POST_SEARCH) {
1439 int premodify_found = 0;
1441 if ( IS_POST_SEARCH ||
1442 ps_type == LDAP_PSEARCH_BY_ADD ||
1443 ps_type == LDAP_PSEARCH_BY_DELETE ||
1444 ps_type == LDAP_PSEARCH_BY_MODIFY ||
1445 ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1447 if ( !IS_POST_SEARCH &&
1448 ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1449 struct psid_entry* psid_e;
1450 LDAP_LIST_FOREACH( psid_e,
1451 &op->o_pm_list, ps_link)
1453 if( psid_e->ps_op == sop ) {
1454 premodify_found = 1;
1455 LDAP_LIST_REMOVE(psid_e, ps_link);
1459 if (psid_e != NULL) free (psid_e);
1462 if ( IS_POST_SEARCH ) {
1464 entry_sync_state = LDAP_SYNC_ADD;
1466 post_search_no_entry:
1467 entry_sync_state = LDAP_SYNC_DELETE;
1469 } else if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1470 entry_sync_state = LDAP_SYNC_ADD;
1471 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1472 entry_sync_state = LDAP_SYNC_DELETE;
1473 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1474 if ( premodify_found ) {
1475 entry_sync_state = LDAP_SYNC_MODIFY;
1477 entry_sync_state = LDAP_SYNC_ADD;
1479 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT ) {
1480 entry_sync_state = LDAP_SYNC_DELETE;
1482 rs->sr_err = LDAP_OTHER;
1486 if ( sop->o_sync_slog_size != -1 ) {
1487 if ( entry_sync_state == LDAP_SYNC_DELETE ) {
1488 result = slap_add_session_log( op, sop, e );
1493 struct berval cookie;
1494 slap_compose_sync_cookie( sop, &cookie,
1496 sop->o_sync_state.sid,
1497 sop->o_sync_state.rid );
1498 rs->sr_err = slap_build_sync_state_ctrl(
1499 sop, rs, e, entry_sync_state, ctrls,
1500 num_ctrls++, 1, &cookie );
1501 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1502 if (!(IS_POST_SEARCH &&
1503 entry_sync_state == LDAP_SYNC_DELETE)) {
1504 rs->sr_attrs = attrs;
1506 rs->sr_attrs = NULL;
1508 rs->sr_operational_attrs = NULL;
1509 rs->sr_ctrls = ctrls;
1511 result = send_search_entry( sop, rs );
1512 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1514 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1516 slap_sl_free( ctrls[--num_ctrls],
1518 ctrls[num_ctrls] = NULL;
1519 rs->sr_ctrls = NULL;
1522 } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1523 struct psid_entry* psid_e;
1524 psid_e = (struct psid_entry *) ch_calloc(1,
1525 sizeof(struct psid_entry));
1526 psid_e->ps_op = sop;
1527 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1531 Debug( LDAP_DEBUG_TRACE,
1532 "bdb_search: invalid ps_type (%d) \n",
1537 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1538 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1539 rs->sr_err = slap_build_sync_state_ctrl(
1540 sop, rs, e, entry_sync_state, ctrls,
1541 num_ctrls++, 0, NULL );
1542 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1543 rs->sr_ctrls = ctrls;
1544 rs->sr_attrs = sop->oq_search.rs_attrs;
1545 rs->sr_operational_attrs = NULL;
1547 result = send_search_entry( sop, rs );
1549 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1551 slap_sl_free( ctrls[--num_ctrls],
1553 ctrls[num_ctrls] = NULL;
1554 rs->sr_ctrls = NULL;
1556 } else { /* PRESENT */
1557 if ( sync_send_present_mode ) {
1558 result = slap_build_syncUUID_set( sop,
1560 if ( result <= 0 ) {
1564 if ( syncUUID_set_cnt ==
1565 SLAP_SYNCUUID_SET_SIZE )
1567 rs->sr_err = LDAP_SUCCESS;
1568 rs->sr_rspoid = LDAP_SYNC_INFO;
1569 rs->sr_ctrls = NULL;
1570 result = slap_send_syncinfo( sop, rs,
1571 LDAP_TAG_SYNC_ID_SET,
1572 NULL, 0, syncUUID_set, 0 );
1573 if ( result != LDAP_SUCCESS ) {
1576 ber_bvarray_free_x( syncUUID_set,
1578 syncUUID_set = NULL;
1579 syncUUID_set_cnt = 0;
1589 rs->sr_attrs = sop->oq_search.rs_attrs;
1590 rs->sr_operational_attrs = NULL;
1591 rs->sr_ctrls = NULL;
1593 rs->sr_err = LDAP_SUCCESS;
1594 result = send_search_entry( sop, rs );
1599 case 0: /* entry sent ok */
1601 case 1: /* entry not sent */
1603 case -1: /* connection closed */
1605 bdb_cache_return_entry_r(bdb->bi_dbenv,
1606 &bdb->bi_cache, e, &lock);
1609 rs->sr_entry = NULL;
1610 rs->sr_err = LDAP_OTHER;
1616 Debug( LDAP_DEBUG_TRACE,
1617 "bdb_search: %ld does not match filter\n",
1623 /* free reader lock */
1625 if (!(IS_POST_SEARCH &&
1626 entry_sync_state == LDAP_SYNC_DELETE)) {
1627 bdb_cache_return_entry_r( bdb->bi_dbenv,
1628 &bdb->bi_cache, e , &lock );
1629 if ( sop->o_nocaching ) {
1630 bdb_cache_delete_entry( bdb, ei, locker, &lock );
1635 rs->sr_entry = NULL;
1638 if ( sop->o_refresh_in_progress ) {
1644 ldap_pvt_thread_yield();
1647 if ( syncUUID_set_cnt > 0 ) {
1648 rs->sr_err = LDAP_SUCCESS;
1649 rs->sr_rspoid = LDAP_SYNC_INFO;
1650 rs->sr_ctrls = NULL;
1651 slap_send_syncinfo( sop, rs, LDAP_TAG_SYNC_ID_SET,
1652 NULL, 0, syncUUID_set, 0 );
1653 ber_bvarray_free_x( syncUUID_set, sop->o_tmpmemctx );
1654 syncUUID_set_cnt = 0;
1658 if (!IS_PSEARCH && !IS_POST_SEARCH) {
1659 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1660 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1661 struct berval cookie;
1662 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1663 sop->o_sync_state.sid, sop->o_sync_state.rid );
1665 if ( sync_send_present_mode ) {
1666 rs->sr_err = LDAP_SUCCESS;
1667 rs->sr_rspoid = LDAP_SYNC_INFO;
1668 rs->sr_ctrls = NULL;
1669 slap_send_syncinfo( sop, rs,
1670 LDAP_TAG_SYNC_REFRESH_PRESENT, &cookie, 1, NULL, 0 );
1673 if ( !no_sync_state_change ) {
1675 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1676 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1679 if ( ps_list->o_sync_slog_size > 0 ) {
1680 if ( ps_list->o_sync_state.sid ==
1681 sop->o_sync_state.sid )
1690 rs->sr_err = LDAP_SUCCESS;
1691 rs->sr_rspoid = NULL;
1692 rs->sr_ctrls = NULL;
1693 slap_send_session_log( op, ps_list, rs );
1695 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1698 rs->sr_err = LDAP_SUCCESS;
1699 rs->sr_rspoid = LDAP_SYNC_INFO;
1700 rs->sr_ctrls = NULL;
1701 slap_send_syncinfo( sop, rs,
1702 LDAP_TAG_SYNC_REFRESH_DELETE, &cookie, 1, NULL, 0 );
1705 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1708 /* refreshOnly mode */
1709 struct berval cookie;
1710 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1711 sop->o_sync_state.sid, sop->o_sync_state.rid );
1713 if ( sync_send_present_mode ) {
1714 slap_build_sync_done_ctrl( sop, rs, ctrls,
1715 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1718 if ( !no_sync_state_change ) {
1720 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1721 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1724 if ( ps_list->o_sync_slog_size > 0 ) {
1725 if ( ps_list->o_sync_state.sid ==
1726 sop->o_sync_state.sid ) {
1734 slap_send_session_log( op, ps_list, rs );
1736 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1739 slap_build_sync_done_ctrl( sop, rs, ctrls,
1740 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_DELETES );
1743 rs->sr_ctrls = ctrls;
1744 rs->sr_ref = rs->sr_v2ref;
1745 rs->sr_err = (rs->sr_v2ref == NULL)
1746 ? LDAP_SUCCESS : LDAP_REFERRAL;
1747 rs->sr_rspoid = NULL;
1748 send_ldap_result( sop, rs );
1749 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1750 slap_sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val,
1753 slap_sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1754 ctrls[num_ctrls] = NULL;
1755 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1759 rs->sr_ctrls = NULL;
1760 rs->sr_ref = rs->sr_v2ref;
1761 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1762 rs->sr_rspoid = NULL;
1763 if ( get_pagedresults(sop) > SLAP_NO_CONTROL ) {
1764 send_paged_response( sop, rs, NULL, 0 );
1766 send_ldap_result( sop, rs );
1771 if ( sop->o_refresh_in_progress ) {
1772 sop->o_refresh_in_progress = 0;
1775 rs->sr_err = LDAP_SUCCESS;
1778 if ( sop->o_psearch_finished ) {
1779 avl_free( sop->o_psearch_finished, ch_free );
1782 if( !IS_PSEARCH && e != NULL ) {
1783 /* free reader lock */
1784 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1788 LOCK_ID_FREE( bdb->bi_dbenv, locker );
1790 ber_bvfree( search_context_csn );
1792 if( rs->sr_v2ref ) {
1793 ber_bvarray_free( rs->sr_v2ref );
1794 rs->sr_v2ref = NULL;
1796 if( realbase.bv_val ) ch_free( realbase.bv_val );
1802 static int base_candidate(
1807 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1808 e->e_nname.bv_val, (long) e->e_id, 0);
1815 /* Look for "objectClass Present" in this filter.
1816 * Also count depth of filter tree while we're at it.
1818 static int oc_filter(
1827 if( cur > *max ) *max = cur;
1829 switch( f->f_choice ) {
1830 case LDAP_FILTER_PRESENT:
1831 if (f->f_desc == slap_schema.si_ad_objectClass) {
1836 case LDAP_FILTER_AND:
1837 case LDAP_FILTER_OR:
1839 for ( f=f->f_and; f; f=f->f_next ) {
1840 (void) oc_filter(f, cur, max);
1850 static void search_stack_free( void *key, void *data )
1852 ber_memfree_x(data, NULL);
1855 static void *search_stack( Operation *op )
1857 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1860 if ( op->o_threadctx ) {
1861 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1864 ret = bdb->bi_search_stack;
1868 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1870 if ( op->o_threadctx ) {
1871 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1872 ret, search_stack_free );
1874 bdb->bi_search_stack = ret;
1880 static int search_candidates(
1889 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1891 Filter f, rf, xf, nf;
1893 AttributeAssertion aa_ref;
1894 #ifdef BDB_SUBENTRIES
1896 AttributeAssertion aa_subentry;
1900 * This routine takes as input a filter (user-filter)
1901 * and rewrites it as follows:
1902 * (&(scope=DN)[(objectClass=subentry)]
1903 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1906 Debug(LDAP_DEBUG_TRACE,
1907 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1908 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1910 xf.f_or = op->oq_search.rs_filter;
1911 xf.f_choice = LDAP_FILTER_OR;
1914 /* If the user's filter uses objectClass=*,
1915 * these clauses are redundant.
1917 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1918 && !get_subentries_visibility(op)
1919 && !is_sync_protocol(op) )
1921 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1922 /* match referral objects */
1923 struct berval bv_ref = BER_BVC( "referral" );
1924 rf.f_choice = LDAP_FILTER_EQUALITY;
1926 rf.f_av_desc = slap_schema.si_ad_objectClass;
1927 rf.f_av_value = bv_ref;
1928 rf.f_next = xf.f_or;
1935 f.f_choice = LDAP_FILTER_AND;
1937 /* Dummy; we compute scope separately now */
1938 nf.f_choice = SLAPD_FILTER_COMPUTED;
1939 nf.f_result = LDAP_SUCCESS;
1940 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1941 ? op->oq_search.rs_filter : &xf ;
1942 /* Filter depth increased again, adding dummy clause */
1945 #ifdef BDB_SUBENTRIES
1946 if( get_subentries_visibility( op ) ) {
1947 struct berval bv_subentry = BER_BVC( "subentry" );
1948 sf.f_choice = LDAP_FILTER_EQUALITY;
1949 sf.f_ava = &aa_subentry;
1950 sf.f_av_desc = slap_schema.si_ad_objectClass;
1951 sf.f_av_value = bv_subentry;
1952 sf.f_next = nf.f_next;
1957 /* Allocate IDL stack, plus 1 more for former tmp */
1958 if ( depth+1 > bdb->bi_search_stack_depth ) {
1959 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1961 stack = search_stack( stackop );
1964 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1965 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1967 rc = bdb_dn2idl( op, e, ids, stack );
1970 if ( rc == LDAP_SUCCESS ) {
1971 rc = bdb_filter_candidates( op, &f, ids,
1972 stack, stack+BDB_IDL_UM_SIZE );
1975 if ( depth+1 > bdb->bi_search_stack_depth ) {
1980 Debug(LDAP_DEBUG_TRACE,
1981 "bdb_search_candidates: failed (rc=%d)\n",
1985 Debug(LDAP_DEBUG_TRACE,
1986 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1988 (long) BDB_IDL_FIRST(ids),
1989 (long) BDB_IDL_LAST(ids) );
1996 parse_paged_cookie( Operation *op, SlapReply *rs )
1999 int rc = LDAP_SUCCESS;
2003 struct berval cookie = BER_BVNULL;
2005 /* this function must be invoked only if the pagedResults
2006 * control has been detected, parsed and partially checked
2007 * by the frontend */
2008 assert( get_pagedresults( op ) > SLAP_NO_CONTROL );
2010 /* look for the appropriate ctrl structure */
2011 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
2012 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
2018 if ( c[0] == NULL ) {
2019 rs->sr_text = "missing pagedResults control";
2020 return LDAP_PROTOCOL_ERROR;
2023 /* Tested by frontend */
2024 assert( c[0]->ldctl_value.bv_len > 0 );
2026 /* Parse the control value
2027 * realSearchControlValue ::= SEQUENCE {
2028 * size INTEGER (0..maxInt),
2029 * -- requested page size from client
2030 * -- result set size estimate from server
2031 * cookie OCTET STRING
2034 ber = ber_init( &c[0]->ldctl_value );
2035 if ( ber == NULL ) {
2036 rs->sr_text = "internal error";
2040 tag = ber_scanf( ber, "{im}", &size, &cookie );
2042 /* Tested by frontend */
2043 assert( tag != LBER_ERROR );
2044 assert( size >= 0 );
2046 /* cookie decoding/checks deferred to backend... */
2047 if ( cookie.bv_len ) {
2048 PagedResultsCookie reqcookie;
2049 if( cookie.bv_len != sizeof( reqcookie ) ) {
2051 rs->sr_text = "paged results cookie is invalid";
2052 rc = LDAP_PROTOCOL_ERROR;
2056 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
2058 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
2060 rs->sr_text = "paged results cookie is invalid";
2061 rc = LDAP_PROTOCOL_ERROR;
2064 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
2065 rs->sr_text = "paged results cookie is invalid or old";
2066 rc = LDAP_UNWILLING_TO_PERFORM;
2071 /* Initial request. Initialize state. */
2073 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
2074 /* There's another pagedResults control on the
2075 * same connection; reject new pagedResults controls
2076 * (allowed by RFC2696) */
2077 rs->sr_text = "paged results cookie unavailable; try later";
2078 rc = LDAP_UNWILLING_TO_PERFORM;
2082 op->o_pagedresults_state.ps_cookie = 0;
2083 op->o_pagedresults_state.ps_count = 0;
2087 (void)ber_free( ber, 1 );
2093 send_paged_response(
2099 LDAPControl ctrl, *ctrls[2];
2100 BerElementBuffer berbuf;
2101 BerElement *ber = (BerElement *)&berbuf;
2102 PagedResultsCookie respcookie;
2103 struct berval cookie;
2105 Debug(LDAP_DEBUG_ARGS,
2106 "send_paged_response: lastid=0x%08lx nentries=%d\n",
2107 lastid ? *lastid : 0, rs->sr_nentries, NULL );
2109 BER_BVZERO( &ctrl.ldctl_value );
2113 ber_init2( ber, NULL, LBER_USE_DER );
2116 respcookie = ( PagedResultsCookie )(*lastid);
2117 cookie.bv_len = sizeof( respcookie );
2118 cookie.bv_val = (char *)&respcookie;
2121 respcookie = ( PagedResultsCookie )0;
2122 BER_BVSTR( &cookie, "" );
2125 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
2126 op->o_conn->c_pagedresults_state.ps_count =
2127 op->o_pagedresults_state.ps_count + rs->sr_nentries;
2129 /* return size of 0 -- no estimate */
2130 ber_printf( ber, "{iO}", 0, &cookie );
2132 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
2136 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
2137 ctrls[0]->ldctl_iscritical = 0;
2139 rs->sr_ctrls = ctrls;
2140 rs->sr_err = LDAP_SUCCESS;
2141 send_ldap_result( op, rs );
2142 rs->sr_ctrls = NULL;
2145 (void) ber_free_buf( ber );
2149 bdb_pfid_cmp( const void *v_id1, const void *v_id2 )
2151 const ID *p1 = v_id1, *p2 = v_id2;
2156 bdb_id_dup( Operation *op, ID *id )
2159 new = ch_malloc( sizeof(ID) );