1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
25 static int base_candidate(
30 static int search_candidates(
31 Operation *stackop, /* op with the current threadctx/slab cache */
32 Operation *sop, /* search op */
39 static int parse_paged_cookie( Operation *op, SlapReply *rs );
41 static void send_paged_response(
47 static int bdb_pfid_cmp( const void *v_id1, const void *v_id2 );
48 static ID* bdb_id_dup( Operation *op, ID *id );
50 /* Dereference aliases for a single alias entry. Return the final
51 * dereferenced entry on success, NULL on any failure.
53 static Entry * deref_base (
63 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
68 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
69 rs->sr_text = "maximum deref depth exceeded";
71 while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
72 /* Remember the last entry we looked at, so we can
77 /* If this is part of a subtree or onelevel search,
78 * have we seen this ID before? If so, quit.
80 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
85 /* If we've seen this ID during this deref iteration,
88 if ( bdb_idl_insert( tmp, e->e_id ) ) {
89 rs->sr_err = LDAP_ALIAS_PROBLEM;
90 rs->sr_text = "circular alias";
95 /* If there was a problem getting the aliasedObjectName,
96 * get_alias_dn will have set the error status.
98 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
103 rs->sr_err = bdb_dn2entry( op, NULL, &ndn, &ei,
113 rs->sr_err = LDAP_ALIAS_PROBLEM;
114 rs->sr_text = "aliasedObject not found";
118 /* Free the previous entry, continue to work with the
119 * one we just retrieved.
121 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
125 /* We found a regular entry. Return this to the caller. The
126 * entry is still locked for Read.
128 if (!is_entry_alias(e)) {
129 rs->sr_err = LDAP_SUCCESS;
137 /* Look for and dereference all aliases within the search scope. Adds
138 * the dereferenced entries to the "ids" list. Requires "stack" to be
139 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
140 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
142 static int search_aliases(
151 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
152 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
153 ID cursora, ida, cursoro, ido, *subscop2;
156 struct berval bv_alias = BER_BVC( "alias" );
157 AttributeAssertion aa_alias;
159 DB_LOCK locka, lockr;
162 aliases = stack; /* IDL of all aliases in the database */
163 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
164 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
165 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
166 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
167 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
168 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
170 /* A copy of subscop, because subscop gets clobbered by
171 * the bdb_idl_union/intersection routines
173 subscop2 = tmp + BDB_IDL_DB_SIZE;
175 af.f_choice = LDAP_FILTER_EQUALITY;
176 af.f_ava = &aa_alias;
177 af.f_av_desc = slap_schema.si_ad_objectClass;
178 af.f_av_value = bv_alias;
181 /* Find all aliases in database */
182 BDB_IDL_ZERO( aliases );
183 rs->sr_err = bdb_filter_candidates( op, &af, aliases,
185 if (rs->sr_err != LDAP_SUCCESS) {
189 oldsubs[1] = e->e_id;
192 BDB_IDL_ZERO( visited );
193 BDB_IDL_ZERO( newsubs );
196 ido = bdb_idl_first( oldsubs, &cursoro );
199 /* Set curscop to only the aliases in the current scope. Start with
200 * all the aliases, obtain the IDL for the current scope, and then
201 * get the intersection of these two IDLs. Add the current scope
202 * to the cumulative list of candidates.
204 BDB_IDL_CPY( curscop, aliases );
205 rs->sr_err = bdb_dn2idl( op, e, subscop,
206 subscop2+BDB_IDL_DB_SIZE );
210 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
212 BDB_IDL_CPY(subscop2, subscop);
213 rs->sr_err = bdb_idl_intersection(curscop, subscop);
214 bdb_idl_union( ids, subscop2 );
216 /* Dereference all of the aliases in the current scope. */
218 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
219 ida = bdb_idl_next(curscop, &cursora))
223 rs->sr_err = bdb_cache_find_id(op, NULL,
224 ida, &ei, 0, locker, &lockr );
225 if (rs->sr_err != LDAP_SUCCESS) {
226 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
227 rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
232 /* This should only happen if the curscop IDL has maxed out and
233 * turned into a range that spans IDs indiscriminately
235 if (!is_entry_alias(a)) {
236 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
241 /* Actually dereference the alias */
243 a = deref_base( op, rs, a, &matched, locker, &lockr,
246 /* If the target was not already in our current candidates,
247 * make note of it in the newsubs list. Also
248 * set it in the scopes list so that bdb_search
251 if (bdb_idl_insert(ids, a->e_id) == 0) {
252 bdb_idl_insert(newsubs, a->e_id);
253 bdb_idl_insert(scopes, a->e_id);
255 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
258 } else if (matched) {
259 /* Alias could not be dereferenced, or it deref'd to
260 * an ID we've already seen. Ignore it.
262 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
267 /* If this is a OneLevel search, we're done; oldsubs only had one
268 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
270 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
272 ido = bdb_idl_next( oldsubs, &cursoro );
274 /* If we're done processing the old scopes, did we add any new
275 * scopes in this iteration? If so, go back and do those now.
278 if (BDB_IDL_IS_ZERO(newsubs)) break;
279 BDB_IDL_CPY(oldsubs, newsubs);
280 BDB_IDL_ZERO(newsubs);
282 ido = bdb_idl_first( oldsubs, &cursoro );
285 /* Find the entry corresponding to the next scope. If it can't
286 * be found, ignore it and move on. This should never happen;
287 * we should never see the ID of an entry that doesn't exist.
288 * Set the name so that the scope's IDL can be retrieved.
292 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
294 if ( rs->sr_err != LDAP_SUCCESS ) {
295 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
296 rs->sr_err == DB_LOCK_NOTGRANTED )
307 #define is_sync_protocol(op) \
308 ((op)->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST)
310 #define IS_BDB_REPLACE(type) (( type == LDAP_PSEARCH_BY_DELETE ) || \
311 ( type == LDAP_PSEARCH_BY_SCOPEOUT ))
312 #define IS_PSEARCH (op != sop)
313 #define IS_POST_SEARCH ( op->ors_post_search_id != NOID )
316 bdb_drop_psearch( Operation *op, ber_int_t msgid )
319 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
321 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
322 if ( ps_list->o_connid == op->o_connid ) {
323 if ( ps_list->o_msgid == msgid ) {
324 ps_list->o_abandon = 1;
325 LDAP_LIST_REMOVE( ps_list, o_ps_link );
326 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
327 LDAP_STAILQ_REMOVE( &op->o_conn->c_ops, ps_list,
329 LDAP_STAILQ_NEXT( ps_list, o_next ) = NULL;
330 op->o_conn->c_n_ops_executing--;
331 op->o_conn->c_n_ops_completed++;
332 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
342 bdb_abandon( Operation *op, SlapReply *rs )
345 void *saved_tmpmemctx;
347 ps = bdb_drop_psearch( op, op->oq_abandon.rs_msgid );
349 saved_tmpmemctx = ps->o_tmpmemctx;
351 if (!BER_BVISNULL(&ps->o_req_dn)) {
352 slap_sl_free(ps->o_req_dn.bv_val, ps->o_tmpmemctx );
354 if (!BER_BVISNULL(&ps->o_req_ndn)) {
355 slap_sl_free(ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
357 if (!BER_BVISNULL(&ps->ors_filterstr)) {
358 ps->o_tmpfree(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
360 if (ps->ors_filter != NULL) {
361 filter_free_x(ps, ps->ors_filter);
363 if (ps->ors_attrs != NULL) {
364 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
369 if ( saved_tmpmemctx ) {
370 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
375 return LDAP_UNAVAILABLE;
379 bdb_cancel( Operation *op, SlapReply *rs )
382 void *saved_tmpmemctx;
384 ps = bdb_drop_psearch( op, op->oq_cancel.rs_msgid );
386 saved_tmpmemctx = ps->o_tmpmemctx;
388 rs->sr_err = LDAP_CANCELLED;
389 send_ldap_result( ps, rs );
391 if (!BER_BVISNULL(&ps->o_req_dn)) {
392 slap_sl_free(ps->o_req_dn.bv_val, ps->o_tmpmemctx );
394 if (!BER_BVISNULL(&ps->o_req_ndn)) {
395 slap_sl_free(ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
397 if (!BER_BVISNULL(&ps->ors_filterstr)) {
398 ps->o_tmpfree(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
400 if (ps->ors_filter != NULL) {
401 filter_free_x(ps, ps->ors_filter);
403 if (ps->ors_attrs != NULL) {
404 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
409 if ( saved_tmpmemctx ) {
410 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
415 return LDAP_UNAVAILABLE;
418 int bdb_search( Operation *op, SlapReply *rs )
421 struct pc_entry *pce = NULL;
422 struct pc_entry *tmp_pce = NULL;
426 ps_e.e_private = NULL;
427 ldap_pvt_thread_mutex_init( &op->o_pcmutex );
428 LDAP_TAILQ_INIT( &op->o_ps_pre_candidates );
429 LDAP_TAILQ_INIT( &op->o_ps_post_candidates );
431 op->ors_post_search_id = NOID;
432 rc = bdb_do_search( op, rs, op, NULL, 0 );
434 ldap_pvt_thread_mutex_lock( &op->o_pcmutex );
435 pce = LDAP_TAILQ_FIRST( &op->o_ps_post_candidates );
436 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
438 while ( rc == LDAP_SUCCESS && pce &&
439 op->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
441 ps_e.e_id = op->ors_post_search_id = pce->pc_id;
442 if ( op->o_sync_csn.bv_val ) {
443 ch_free( op->o_sync_csn.bv_val );
444 op->o_sync_csn.bv_val = NULL;
446 ber_dupbv( &op->o_sync_csn, &pce->pc_csn );
447 ber_dupbv( &ps_e.e_name, &pce->pc_ename );
448 ber_dupbv( &ps_e.e_nname, &pce->pc_enname );
449 a = ch_calloc( 1, sizeof( Attribute ));
450 a->a_desc = slap_schema.si_ad_entryUUID;
451 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
452 ber_dupbv( &a->a_vals[0], &pce->pc_entryUUID );
453 a->a_nvals = a->a_vals;
457 rc = bdb_do_search( op, rs, op, &ps_e, 0 );
460 ldap_pvt_thread_mutex_lock( &op->o_pcmutex );
461 pce = LDAP_TAILQ_NEXT( pce, pc_link );
462 LDAP_TAILQ_REMOVE( &op->o_ps_post_candidates, tmp_pce, pc_link );
463 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
465 ch_free( tmp_pce->pc_csn.bv_val );
466 ch_free( tmp_pce->pc_entryUUID.bv_val );
467 ch_free( tmp_pce->pc_ename.bv_val );
468 ch_free( tmp_pce->pc_enname.bv_val );
470 entry_clean( &ps_e );
475 #define BDB_PSEARCH_MAX_WAIT 3
476 int bdb_psearch( Operation *op, SlapReply *rs, Operation *sop,
477 Entry *ps_e, int ps_type )
480 struct pc_entry *pce = NULL;
481 struct pc_entry *p = NULL;
484 op->ors_post_search_id = NOID;
487 case LDAP_PSEARCH_BY_PREMODIFY:
488 case LDAP_PSEARCH_BY_PREDELETE:
490 if ( !op->o_ps_send_wait ) {
491 if ( sop->o_refresh_in_progress ) {
492 pce = (struct pc_entry *) ch_calloc(
493 1, sizeof( struct pc_entry ));
494 pce->pc_id = ps_e->e_id;
495 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
496 if ( LDAP_TAILQ_EMPTY( &sop->o_ps_pre_candidates )) {
497 LDAP_TAILQ_INSERT_HEAD(
498 &sop->o_ps_pre_candidates, pce, pc_link );
500 LDAP_TAILQ_FOREACH( p,
501 &sop->o_ps_pre_candidates, pc_link ) {
502 if ( p->pc_id > pce->pc_id )
507 LDAP_TAILQ_INSERT_BEFORE( p, pce, pc_link );
509 LDAP_TAILQ_INSERT_TAIL(
510 &sop->o_ps_pre_candidates,
514 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
516 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
520 pce = op->o_ps_send_wait;
523 /* Wait until refresh search send the entry */
524 while ( !pce->pc_sent ) {
525 if ( sop->o_refresh_in_progress ) {
526 if ( num_retries == BDB_PSEARCH_MAX_WAIT ) {
527 op->o_ps_send_wait = pce;
530 ldap_pvt_thread_yield();
531 bdb_trans_backoff( ++num_retries );
537 op->o_ps_send_wait = NULL;
539 if ( !sop->o_refresh_in_progress && !pce->pc_sent ) {
540 /* refresh ended without processing pce */
541 /* need to perform psearch for ps_e */
542 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
543 LDAP_TAILQ_REMOVE( &sop->o_ps_pre_candidates, pce, pc_link );
544 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
546 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
549 /* the pce entry was sent in the refresh phase */
550 if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
551 struct psid_entry* psid_e;
552 psid_e = (struct psid_entry *) ch_calloc(1,
553 sizeof(struct psid_entry));
555 LDAP_LIST_INSERT_HEAD( &op->o_pm_list, psid_e, ps_link );
558 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
559 LDAP_TAILQ_REMOVE( &sop->o_ps_pre_candidates, pce, pc_link );
560 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
565 case LDAP_PSEARCH_BY_DELETE:
566 case LDAP_PSEARCH_BY_SCOPEOUT:
567 case LDAP_PSEARCH_BY_ADD:
568 case LDAP_PSEARCH_BY_MODIFY:
569 ldap_pvt_thread_mutex_lock( &op->o_pcmutex );
570 if ( sop->o_refresh_in_progress ||
571 !LDAP_TAILQ_EMPTY( &sop->o_ps_post_candidates )) {
572 pce = (struct pc_entry *) ch_calloc( 1, sizeof( struct pc_entry ));
573 pce->pc_id = ps_e->e_id;
574 ber_dupbv( &pce->pc_csn, &op->o_sync_csn );
575 if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
577 for ( a = ps_e->e_attrs; a != NULL; a = a->a_next ) {
578 AttributeDescription *desc = a->a_desc;
579 if ( desc == slap_schema.si_ad_entryUUID ) {
580 ber_dupbv( &pce->pc_entryUUID, &a->a_nvals[0] );
584 ber_dupbv( &pce->pc_ename, &ps_e->e_name );
585 ber_dupbv( &pce->pc_enname, &ps_e->e_nname );
586 LDAP_TAILQ_INSERT_TAIL( &sop->o_ps_post_candidates, pce, pc_link );
587 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
589 ldap_pvt_thread_mutex_unlock( &op->o_pcmutex );
590 rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
595 Debug( LDAP_DEBUG_TRACE, "do_psearch: invalid psearch type\n",
601 int bdb_search( Operation *op, SlapReply *rs )
603 return bdb_do_search( op, rs, op, NULL, 0 );
607 /* For persistent searches, op is the currently executing operation,
608 * sop is the persistent search. For regular searches, sop = op.
611 bdb_do_search( Operation *op, SlapReply *rs, Operation *sop,
612 Entry *ps_e, int ps_type )
614 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
617 ID candidates[BDB_IDL_UM_SIZE];
618 ID scopes[BDB_IDL_DB_SIZE];
619 Entry *e = NULL, base, e_root = {0};
620 Entry *matched = NULL;
621 EntryInfo *ei, ei_root = {0};
622 struct berval realbase = BER_BVNULL;
626 AttributeName *attrs;
628 u_int32_t locker = 0;
630 struct bdb_op_info *opinfo = NULL;
634 Filter contextcsnand, contextcsnle, cookief, csnfnot,
635 csnfeq, csnfand, csnfge;
636 AttributeAssertion aa_ge, aa_eq, aa_le;
637 struct berval *search_context_csn = NULL;
639 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
641 AttributeName uuid_attr[2];
643 int entry_sync_state = -1;
644 AttributeName null_attr;
645 int no_sync_state_change = 0;
649 int sync_send_present_mode = 1;
655 struct pc_entry *pce = NULL;
656 BerVarray syncUUID_set = NULL;
657 int syncUUID_set_cnt = 0;
661 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
662 attrs = sop->oq_search.rs_attrs;
664 opinfo = (struct bdb_op_info *) op->o_private;
667 if ( !IS_POST_SEARCH && !IS_PSEARCH &&
668 sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
669 struct slap_session_entry *sent;
670 if ( sop->o_sync_state.sid >= 0 ) {
671 LDAP_LIST_FOREACH( sent, &bdb->bi_session_list, se_link ) {
672 if ( sent->se_id == sop->o_sync_state.sid ) {
673 sop->o_sync_slog_size = sent->se_size;
680 /* psearch needs to be registered before refresh begins */
681 if ( !IS_POST_SEARCH && !IS_PSEARCH &&
682 sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
683 sop->o_refresh_in_progress = 1;
684 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
685 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
686 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
688 } else if ( !IS_POST_SEARCH && !IS_PSEARCH &&
689 sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST
690 && sop->o_sync_slog_size >= 0 )
692 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
693 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
694 if ( ps_list->o_sync_slog_size >= 0 ) {
695 if ( ps_list->o_sync_state.sid == sop->o_sync_state.sid ) {
703 if ( ps_list->o_sync_slog_omitcsn.bv_len != 0 ) {
704 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
705 if ( sop->o_sync_state.ctxcsn &&
706 sop->o_sync_state.ctxcsn->bv_val != NULL )
708 value_match( &match, slap_schema.si_ad_entryCSN, mr,
709 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
710 sop->o_sync_state.ctxcsn,
711 &ps_list->o_sync_slog_omitcsn,
717 sync_send_present_mode = 0;
720 sync_send_present_mode = 0;
722 } else if ( sop->o_sync_slog_size >= 0 ) {
723 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
725 sop->o_sync_state.sid = -1;
727 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
730 null_attr.an_desc = NULL;
731 null_attr.an_oc = NULL;
732 null_attr.an_oc_exclude = 0;
733 BER_BVZERO( &null_attr.an_name );
735 for( num_ctrls = 0; num_ctrls < SLAP_MAX_RESPONSE_CONTROLS; num_ctrls++ ) {
736 ctrls[num_ctrls] = NULL;
740 if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
742 attrs[0].an_desc = NULL;
743 attrs[0].an_oc = NULL;
744 attrs[0].an_oc_exclude = 0;
745 BER_BVZERO( &attrs[0].an_name );
749 manageDSAit = get_manageDSAit( sop );
752 /* Sync control overrides manageDSAit */
753 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
754 if ( manageDSAit == SLAP_CONTROL_NONE ) {
755 manageDSAit = SLAP_CONTROL_CRITICAL;
757 } else if ( IS_PSEARCH ) {
758 if ( manageDSAit == SLAP_CONTROL_NONE ) {
759 manageDSAit = SLAP_CONTROL_CRITICAL;
764 if ( opinfo && opinfo->boi_txn ) {
765 ltid = opinfo->boi_txn;
766 locker = TXN_ID( ltid );
768 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
774 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
780 if ( IS_POST_SEARCH ) {
783 candidates[1] = op->ors_post_search_id;
784 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );
789 if ( sop->o_req_ndn.bv_len == 0 ) {
790 /* DIT root special case */
791 ei_root.bei_e = &e_root;
792 ei_root.bei_parent = &ei_root;
793 e_root.e_private = &ei_root;
795 BER_BVSTR( &e_root.e_nname, "" );
796 BER_BVSTR( &e_root.e_name, "" );
798 rs->sr_err = LDAP_SUCCESS;
801 /* get entry with reader lock */
802 rs->sr_err = bdb_dn2entry( op, ltid, &sop->o_req_ndn, &ei,
814 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
816 LOCK_ID_FREE (bdb->bi_dbenv, locker );
818 case DB_LOCK_DEADLOCK:
819 case DB_LOCK_NOTGRANTED:
822 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
824 LOCK_ID_FREE (bdb->bi_dbenv, locker );
828 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
829 BDB_IDL_ZERO(candidates);
830 e = deref_base( op, rs, e, &matched, locker, &lock,
835 struct berval matched_dn = BER_BVNULL;
837 if ( matched != NULL ) {
840 if ( ! access_allowed( op, matched,
841 slap_schema.si_ad_entry,
842 NULL, ACL_DISCLOSE, NULL ) )
844 rs->sr_err = LDAP_NO_SUCH_OBJECT;
847 ber_dupbv( &matched_dn, &matched->e_name );
849 erefs = is_entry_referral( matched )
850 ? get_entry_referrals( op, matched )
852 rs->sr_err = LDAP_REFERRAL;
853 rs->sr_matched = matched_dn.bv_val;
856 #ifdef SLAP_ZONE_ALLOC
857 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
859 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
864 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
865 &sop->o_req_dn, sop->oq_search.rs_scope );
866 ber_bvarray_free( erefs );
870 #ifdef SLAP_ZONE_ALLOC
871 slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
873 rs->sr_ref = referral_rewrite( default_referral,
874 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
875 rs->sr_err = LDAP_REFERRAL;
878 send_ldap_result( sop, rs );
881 LOCK_ID_FREE (bdb->bi_dbenv, locker );
883 ber_bvarray_free( rs->sr_ref );
886 if ( !BER_BVISNULL( &matched_dn ) ) {
887 ber_memfree( matched_dn.bv_val );
888 rs->sr_matched = NULL;
893 if ( ! access_allowed( op, e, slap_schema.si_ad_entry,
894 NULL, ACL_DISCLOSE, NULL ) )
896 rs->sr_err = LDAP_NO_SUCH_OBJECT;
898 #ifdef SLAP_ZONE_ALLOC
899 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
901 if ( e != &e_root ) {
902 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
904 send_ldap_result( sop, rs );
908 if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
909 /* entry is a referral, don't allow add */
910 struct berval matched_dn = BER_BVNULL;
911 BerVarray erefs = NULL;
913 ber_dupbv( &matched_dn, &e->e_name );
914 erefs = get_entry_referrals( op, e );
916 rs->sr_err = LDAP_REFERRAL;
918 #ifdef SLAP_ZONE_ALLOC
919 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
921 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
925 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
926 &sop->o_req_dn, sop->oq_search.rs_scope );
927 ber_bvarray_free( erefs );
930 rs->sr_text = "bad_referral object";
934 Debug( LDAP_DEBUG_TRACE,
935 LDAP_XSTRING(bdb_search) ": entry is referral\n",
938 rs->sr_matched = matched_dn.bv_val;
939 send_ldap_result( sop, rs );
942 LOCK_ID_FREE (bdb->bi_dbenv, locker );
944 ber_bvarray_free( rs->sr_ref );
946 ber_memfree( matched_dn.bv_val );
947 rs->sr_matched = NULL;
951 if ( get_assert( op ) &&
952 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
954 rs->sr_err = LDAP_ASSERTION_FAILED;
955 #ifdef SLAP_ZONE_ALLOC
956 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
958 if ( e != &e_root ) {
959 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
961 send_ldap_result( sop, rs );
965 /* compute it anyway; root does not use it */
966 stoptime = op->o_time + sop->ors_tlimit;
968 /* need normalized dn below */
969 ber_dupbv( &realbase, &e->e_nname );
971 /* Copy info to base, must free entry before accessing the database
972 * in search_candidates, to avoid deadlocks.
974 base.e_private = e->e_private;
975 base.e_nname = realbase;
978 #ifdef SLAP_ZONE_ALLOC
979 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
981 if ( e != &e_root ) {
982 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
988 rs->sr_err = bdb_get_commit_csn( sop, rs, &search_context_csn,
989 locker, &ctxcsn_lock );
991 if ( rs->sr_err != LDAP_SUCCESS ) {
992 send_ldap_error( sop, rs, rs->sr_err,
993 "error in csn management in search" );
997 if ( sop->o_sync_mode != SLAP_SYNC_NONE &&
998 sop->o_sync_state.ctxcsn &&
999 sop->o_sync_state.ctxcsn->bv_val &&
1000 ber_bvcmp( &sop->o_sync_state.ctxcsn[0], search_context_csn ) == 0 )
1002 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
1006 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );
1010 /* select candidates */
1011 if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
1012 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
1015 BDB_IDL_ZERO( candidates );
1016 BDB_IDL_ZERO( scopes );
1017 rs->sr_err = search_candidates( op, sop, rs, &base,
1018 locker, candidates, scopes );
1022 if ( !IS_PSEARCH && sop->o_sync_mode != SLAP_SYNC_NONE ) {
1023 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
1027 /* start cursor at beginning of candidates.
1032 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
1033 cursor = bdb_idl_search( candidates, ps_e->e_id );
1034 if ( candidates[cursor] != ps_e->e_id ) {
1035 rs->sr_err = LDAP_SUCCESS;
1038 } else if ( ps_e->e_id < BDB_IDL_RANGE_FIRST( candidates ) ||
1039 ps_e->e_id > BDB_IDL_RANGE_LAST( candidates ))
1041 rs->sr_err = LDAP_SUCCESS;
1045 candidates[1] = ps_e->e_id;
1049 if ( candidates[0] == 0 ) {
1050 Debug( LDAP_DEBUG_TRACE,
1051 LDAP_XSTRING(bdb_search) ": no candidates\n",
1057 /* if not root and candidates exceed to-be-checked entries, abort */
1058 if ( sop->ors_limit /* isroot == FALSE */ &&
1059 sop->ors_limit->lms_s_unchecked != -1 &&
1060 BDB_IDL_N(candidates) > (unsigned) sop->ors_limit->lms_s_unchecked )
1062 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1063 send_ldap_result( sop, rs );
1064 rs->sr_err = LDAP_SUCCESS;
1068 if ( sop->ors_limit == NULL /* isroot == TRUE */ ||
1069 !sop->ors_limit->lms_s_pr_hide )
1071 tentries = BDB_IDL_N(candidates);
1074 if ( get_pagedresults( sop ) > SLAP_CONTROL_IGNORED ) {
1075 PagedResultsState *ps = sop->o_pagedresults_state;
1076 /* deferred cookie parsing */
1077 rs->sr_err = parse_paged_cookie( sop, rs );
1078 if ( rs->sr_err != LDAP_SUCCESS ) {
1079 send_ldap_result( sop, rs );
1083 if ( (ID)( ps->ps_cookie ) == 0 ) {
1084 id = bdb_idl_first( candidates, &cursor );
1087 if ( ps->ps_size == 0 ) {
1088 rs->sr_err = LDAP_SUCCESS;
1089 rs->sr_text = "search abandoned by pagedResult size=0";
1090 send_ldap_result( sop, rs );
1093 for ( id = bdb_idl_first( candidates, &cursor );
1095 id <= (ID)( ps->ps_cookie );
1096 id = bdb_idl_next( candidates, &cursor ) )
1102 if ( cursor == NOID ) {
1103 Debug( LDAP_DEBUG_TRACE,
1104 LDAP_XSTRING(bdb_search)
1105 ": no paged results candidates\n",
1107 send_paged_response( sop, rs, &lastid, 0 );
1109 rs->sr_err = LDAP_OTHER;
1116 if (( sop->o_sync_mode & SLAP_SYNC_REFRESH ) || IS_PSEARCH ) {
1119 cookief.f_choice = LDAP_FILTER_AND;
1120 cookief.f_and = &csnfnot;
1121 cookief.f_next = NULL;
1123 csnfnot.f_choice = LDAP_FILTER_NOT;
1124 csnfnot.f_not = &csnfeq;
1125 csnfnot.f_next = &csnfand;
1127 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
1128 csnfeq.f_ava = &aa_eq;
1129 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
1130 if ( sop->o_sync_state.ctxcsn != NULL ) {
1131 csnfeq.f_av_value = *sop->o_sync_state.ctxcsn;
1133 csnfeq.f_av_value = slap_empty_bv;
1136 csnfand.f_choice = LDAP_FILTER_AND;
1137 csnfand.f_and = &csnfge;
1138 csnfand.f_next = NULL;
1140 csnfge.f_choice = LDAP_FILTER_GE;
1141 csnfge.f_ava = &aa_ge;
1142 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
1143 if ( sop->o_sync_state.ctxcsn != NULL ) {
1144 csnfge.f_av_value = *sop->o_sync_state.ctxcsn;
1146 csnfge.f_av_value = slap_empty_bv;
1149 if ( search_context_csn && !IS_PSEARCH ) {
1150 csnfge.f_next = &contextcsnand;
1152 contextcsnand.f_choice = LDAP_FILTER_AND;
1153 contextcsnand.f_and = &contextcsnle;
1154 contextcsnand.f_next = NULL;
1156 contextcsnle.f_choice = LDAP_FILTER_LE;
1157 contextcsnle.f_ava = &aa_le;
1158 contextcsnle.f_av_desc = slap_schema.si_ad_entryCSN;
1159 contextcsnle.f_av_value = *search_context_csn;
1160 contextcsnle.f_next = sop->oq_search.rs_filter;
1162 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
1163 if ( sop->o_sync_state.ctxcsn &&
1164 sop->o_sync_state.ctxcsn->bv_val != NULL )
1166 value_match( &match, slap_schema.si_ad_entryCSN, mr,
1167 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1168 &sop->o_sync_state.ctxcsn[0], search_context_csn,
1173 no_sync_state_change = ( match >= 0 );
1175 csnfge.f_next = sop->oq_search.rs_filter;
1182 for ( id = bdb_idl_first( candidates, &cursor );
1185 && !no_sync_state_change
1187 ; id = bdb_idl_next( candidates, &cursor ) )
1195 if ( !IS_POST_SEARCH ) {
1196 idhole = (ID*) avl_find( sop->o_psearch_finished,
1197 (caddr_t)&id, bdb_pfid_cmp );
1199 avl_delete( &sop->o_psearch_finished,
1200 (caddr_t)idhole, bdb_pfid_cmp );
1201 sop->o_tmpfree( idhole, sop->o_tmpmemctx );
1205 if ( sop->o_refresh_in_progress ) {
1206 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
1207 pce = LDAP_TAILQ_FIRST( &sop->o_ps_pre_candidates );
1208 while ( pce && pce->pc_sent ) {
1209 pce = LDAP_TAILQ_NEXT( pce, pc_link );
1211 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
1214 if ( BDB_IDL_IS_RANGE( candidates ) ) {
1215 if ( pce->pc_id >= candidates[1] &&
1216 pce->pc_id <= candidates[2] &&
1217 pce->pc_id > cursor-1 ) {
1220 avl_insert( &sop->o_psearch_finished,
1221 (caddr_t)bdb_id_dup( sop, &pce->pc_id ),
1222 bdb_pfid_cmp, avl_dup_error );
1227 pos = bdb_idl_search(candidates, pce->pc_id);
1228 if ( pos > cursor-1 && pos <= candidates[0] ) {
1231 avl_insert( &sop->o_psearch_finished,
1232 (caddr_t)bdb_id_dup( sop, &pce->pc_id ),
1233 bdb_pfid_cmp, avl_dup_error );
1242 /* check for abandon */
1243 if ( sop->o_abandon ) {
1245 bdb_drop_psearch( sop, sop->o_msgid );
1247 rs->sr_err = LDAP_SUCCESS;
1251 if ( sop->o_cancel ) {
1252 assert( sop->o_cancel == SLAP_CANCEL_REQ );
1253 rs->sr_err = LDAP_CANCELLED;
1254 send_ldap_result( sop, rs );
1255 sop->o_cancel = SLAP_CANCEL_ACK;
1256 rs->sr_err = LDAP_SUCCESS;
1260 /* check for abandon */
1261 if ( sop->o_abandon ) {
1262 rs->sr_err = LDAP_SUCCESS;
1267 /* check time limit */
1268 if ( sop->ors_tlimit != SLAP_NO_LIMIT
1269 && slap_get_time() > stoptime )
1271 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1272 rs->sr_ref = rs->sr_v2ref;
1273 send_ldap_result( sop, rs );
1274 rs->sr_err = LDAP_SUCCESS;
1282 /* get the entry with reader lock */
1284 rs->sr_err = bdb_cache_find_id( op, ltid,
1285 id, &ei, 0, locker, &lock );
1287 if (rs->sr_err == LDAP_BUSY) {
1288 rs->sr_text = "ldap server busy";
1289 send_ldap_result( sop, rs );
1292 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
1293 || rs->sr_err == DB_LOCK_NOTGRANTED )
1295 goto fetch_entry_retry;
1298 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
1306 if ( IS_POST_SEARCH ) {
1307 /* send LDAP_SYNC_DELETE */
1308 rs->sr_entry = e = ps_e;
1309 goto post_search_no_entry;
1312 if( !BDB_IDL_IS_RANGE(candidates) ) {
1313 /* only complain for non-range IDLs */
1314 Debug( LDAP_DEBUG_TRACE,
1315 LDAP_XSTRING(bdb_search)
1316 ": candidate %ld not found\n",
1330 #ifdef BDB_SUBENTRIES
1331 /* FIXME: send all but syncrepl */
1333 if ( !is_sync_protocol( sop ) )
1336 if ( is_entry_subentry( e ) ) {
1337 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
1338 if(!get_subentries_visibility( sop )) {
1339 /* only subentries are visible */
1343 } else if ( get_subentries( sop ) &&
1344 !get_subentries_visibility( sop ))
1346 /* only subentries are visible */
1350 } else if ( get_subentries_visibility( sop )) {
1351 /* only subentries are visible */
1355 #endif /* BDB_SUBENTRIES */
1357 /* Does this candidate actually satisfy the search scope?
1359 * Note that we don't lock access to the bei_parent pointer.
1360 * Since only leaf nodes can be deleted, the parent of any
1361 * node will always be a valid node. Also since we have
1362 * a Read lock on the data, it cannot be renamed out of the
1363 * scope while we are looking at it, and unless we're using
1364 * BDB_HIER, its parents cannot be moved either.
1366 switch( sop->ors_scope ) {
1367 case LDAP_SCOPE_BASE:
1368 /* This is always true, yes? */
1369 if ( id == base.e_id ) scopeok = 1;
1372 case LDAP_SCOPE_ONELEVEL:
1373 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
1376 #ifdef LDAP_SCOPE_CHILDREN
1377 case LDAP_SCOPE_CHILDREN:
1378 if ( id == base.e_id ) break;
1381 case LDAP_SCOPE_SUBTREE: {
1383 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
1384 if ( tmp->bei_id == base.e_id ) {
1392 /* aliases were already dereferenced in candidate list */
1393 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
1394 /* but if the search base is an alias, and we didn't
1395 * deref it when finding, return it.
1397 if ( is_entry_alias(e) &&
1398 ((sop->ors_deref & LDAP_DEREF_FINDING) ||
1399 !bvmatch(&e->e_nname, &op->o_req_ndn)))
1404 /* scopes is only non-empty for onelevel or subtree */
1405 if ( !scopeok && BDB_IDL_N(scopes) ) {
1407 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
1408 x = bdb_idl_search( scopes, e->e_id );
1409 if ( scopes[x] == e->e_id ) scopeok = 1;
1411 /* subtree, walk up the tree */
1412 EntryInfo *tmp = BEI(e);
1413 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
1414 x = bdb_idl_search( scopes, tmp->bei_id );
1415 if ( scopes[x] == tmp->bei_id ) {
1424 /* Not in scope, ignore it */
1426 if ( !IS_POST_SEARCH && !scopeok )
1431 Debug( LDAP_DEBUG_TRACE,
1432 LDAP_XSTRING(bdb_search)
1433 ": %ld scope not okay\n",
1439 * if it's a referral, add it to the list of referrals. only do
1440 * this for non-base searches, and don't check the filter
1441 * explicitly here since it's only a candidate anyway.
1443 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1444 && is_entry_referral( e ) )
1446 BerVarray erefs = get_entry_referrals( sop, e );
1447 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1448 sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1449 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1451 send_search_reference( sop, rs );
1453 ber_bvarray_free( rs->sr_ref );
1454 ber_bvarray_free( erefs );
1460 if ( !manageDSAit && is_entry_glue( e )) {
1464 /* if it matches the filter and scope, send it */
1466 rs->sr_err = test_filter( sop, rs->sr_entry, sop->oq_search.rs_filter );
1469 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1470 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1472 rs->sr_err = LDAP_COMPARE_TRUE;
1476 if ( !IS_POST_SEARCH ) {
1477 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1478 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1479 rs->sr_err = test_filter( sop, rs->sr_entry,
1481 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1482 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1483 if ( no_sync_state_change ) {
1484 Debug( LDAP_DEBUG_TRACE,
1485 LDAP_XSTRING(bdb_search) ": "
1486 "error in context csn management\n",
1489 entry_sync_state = LDAP_SYNC_ADD;
1492 if ( no_sync_state_change ) {
1495 entry_sync_state = LDAP_SYNC_PRESENT;
1499 rs->sr_err = test_filter( sop,
1500 rs->sr_entry, sop->oq_search.rs_filter );
1504 rs->sr_err = test_filter( sop,
1505 rs->sr_entry, sop->oq_search.rs_filter );
1507 rs->sr_err = LDAP_COMPARE_TRUE;
1513 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1514 /* check size limit */
1515 if ( --sop->ors_slimit == -1
1517 && sop->o_sync_slog_size == -1
1523 #ifdef SLAP_ZONE_ALLOC
1524 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
1526 bdb_cache_return_entry_r( bdb->bi_dbenv,
1527 &bdb->bi_cache, e, &lock );
1532 rs->sr_entry = NULL;
1533 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1534 rs->sr_ref = rs->sr_v2ref;
1535 send_ldap_result( sop, rs );
1536 rs->sr_err = LDAP_SUCCESS;
1540 if ( get_pagedresults(sop) > SLAP_CONTROL_IGNORED ) {
1541 if ( rs->sr_nentries >= ((PagedResultsState *)sop->o_pagedresults_state)->ps_size ) {
1542 send_paged_response( sop, rs, &lastid, tentries );
1552 if (IS_PSEARCH || IS_POST_SEARCH) {
1553 int premodify_found = 0;
1555 if ( IS_POST_SEARCH ||
1556 ps_type == LDAP_PSEARCH_BY_ADD ||
1557 ps_type == LDAP_PSEARCH_BY_DELETE ||
1558 ps_type == LDAP_PSEARCH_BY_MODIFY ||
1559 ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1561 if ( !IS_POST_SEARCH &&
1562 ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1563 struct psid_entry* psid_e;
1564 LDAP_LIST_FOREACH( psid_e,
1565 &op->o_pm_list, ps_link)
1567 if( psid_e->ps_op == sop ) {
1568 premodify_found = 1;
1569 LDAP_LIST_REMOVE(psid_e, ps_link);
1573 if (psid_e != NULL) free (psid_e);
1576 if ( IS_POST_SEARCH ) {
1578 entry_sync_state = LDAP_SYNC_ADD;
1580 post_search_no_entry:
1581 entry_sync_state = LDAP_SYNC_DELETE;
1583 } else if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1584 entry_sync_state = LDAP_SYNC_ADD;
1585 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1586 entry_sync_state = LDAP_SYNC_DELETE;
1587 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1588 if ( premodify_found ) {
1589 entry_sync_state = LDAP_SYNC_MODIFY;
1591 entry_sync_state = LDAP_SYNC_ADD;
1593 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT ) {
1594 entry_sync_state = LDAP_SYNC_DELETE;
1596 rs->sr_err = LDAP_OTHER;
1600 if ( sop->o_sync_slog_size != -1 ) {
1601 if ( entry_sync_state == LDAP_SYNC_DELETE ) {
1602 result = slap_add_session_log( op, sop, e );
1607 struct berval cookie;
1608 slap_compose_sync_cookie( sop, &cookie,
1610 sop->o_sync_state.sid,
1611 sop->o_sync_state.rid );
1612 rs->sr_err = slap_build_sync_state_ctrl(
1613 sop, rs, e, entry_sync_state, ctrls,
1614 num_ctrls++, 1, &cookie );
1615 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1616 if (!(IS_POST_SEARCH &&
1617 entry_sync_state == LDAP_SYNC_DELETE)) {
1618 rs->sr_attrs = attrs;
1620 rs->sr_attrs = NULL;
1622 rs->sr_operational_attrs = NULL;
1623 rs->sr_ctrls = ctrls;
1625 result = send_search_entry( sop, rs );
1626 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1628 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1630 slap_sl_free( ctrls[--num_ctrls],
1632 ctrls[num_ctrls] = NULL;
1633 rs->sr_ctrls = NULL;
1636 } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1637 struct psid_entry* psid_e;
1638 psid_e = (struct psid_entry *) ch_calloc(1,
1639 sizeof(struct psid_entry));
1640 psid_e->ps_op = sop;
1641 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1645 Debug( LDAP_DEBUG_TRACE,
1646 LDAP_XSTRING(bdb_search)
1647 ": invalid ps_type (%d) \n",
1652 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1653 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1654 rs->sr_err = slap_build_sync_state_ctrl(
1655 sop, rs, e, entry_sync_state, ctrls,
1656 num_ctrls++, 0, NULL );
1657 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1658 rs->sr_ctrls = ctrls;
1659 rs->sr_attrs = sop->oq_search.rs_attrs;
1660 rs->sr_operational_attrs = NULL;
1662 result = send_search_entry( sop, rs );
1664 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1666 slap_sl_free( ctrls[--num_ctrls],
1668 ctrls[num_ctrls] = NULL;
1669 rs->sr_ctrls = NULL;
1671 } else { /* PRESENT */
1672 if ( sync_send_present_mode ) {
1673 result = slap_build_syncUUID_set( sop,
1675 if ( result <= 0 ) {
1679 if ( syncUUID_set_cnt ==
1680 SLAP_SYNCUUID_SET_SIZE )
1682 rs->sr_err = LDAP_SUCCESS;
1683 rs->sr_rspoid = LDAP_SYNC_INFO;
1684 rs->sr_ctrls = NULL;
1685 result = slap_send_syncinfo( sop, rs,
1686 LDAP_TAG_SYNC_ID_SET,
1687 NULL, 0, syncUUID_set, 0 );
1688 if ( result != LDAP_SUCCESS ) {
1691 ber_bvarray_free_x( syncUUID_set,
1693 syncUUID_set = NULL;
1694 syncUUID_set_cnt = 0;
1705 rs->sr_attrs = sop->oq_search.rs_attrs;
1706 rs->sr_operational_attrs = NULL;
1707 rs->sr_ctrls = NULL;
1709 rs->sr_err = LDAP_SUCCESS;
1710 result = send_search_entry( sop, rs );
1717 case 0: /* entry sent ok */
1719 case 1: /* entry not sent */
1721 case -1: /* connection closed */
1725 #ifdef SLAP_ZONE_ALLOC
1726 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
1728 bdb_cache_return_entry_r(bdb->bi_dbenv,
1729 &bdb->bi_cache, e, &lock);
1734 rs->sr_entry = NULL;
1735 rs->sr_err = LDAP_OTHER;
1741 Debug( LDAP_DEBUG_TRACE,
1742 LDAP_XSTRING(bdb_search)
1743 ": %ld does not match filter\n",
1749 /* free reader lock */
1752 if (!(IS_POST_SEARCH &&
1753 entry_sync_state == LDAP_SYNC_DELETE)) {
1754 #ifdef SLAP_ZONE_ALLOC
1755 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
1757 bdb_cache_return_entry_r( bdb->bi_dbenv,
1758 &bdb->bi_cache, e , &lock );
1759 if ( sop->o_nocaching ) {
1760 bdb_cache_delete_entry( bdb, ei, locker, &lock );
1765 #ifdef SLAP_ZONE_ALLOC
1766 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
1768 bdb_cache_return_entry_r( bdb->bi_dbenv,
1769 &bdb->bi_cache, e , &lock );
1772 rs->sr_entry = NULL;
1776 if ( sop->o_refresh_in_progress ) {
1783 ldap_pvt_thread_yield();
1787 if ( syncUUID_set_cnt > 0 ) {
1788 rs->sr_err = LDAP_SUCCESS;
1789 rs->sr_rspoid = LDAP_SYNC_INFO;
1790 rs->sr_ctrls = NULL;
1791 slap_send_syncinfo( sop, rs, LDAP_TAG_SYNC_ID_SET,
1792 NULL, 0, syncUUID_set, 0 );
1793 ber_bvarray_free_x( syncUUID_set, sop->o_tmpmemctx );
1794 syncUUID_set_cnt = 0;
1800 if (!IS_PSEARCH && !IS_POST_SEARCH) {
1801 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1802 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1803 struct berval cookie;
1804 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1805 sop->o_sync_state.sid, sop->o_sync_state.rid );
1807 if ( sync_send_present_mode ) {
1808 rs->sr_err = LDAP_SUCCESS;
1809 rs->sr_rspoid = LDAP_SYNC_INFO;
1810 rs->sr_ctrls = NULL;
1811 slap_send_syncinfo( sop, rs,
1812 LDAP_TAG_SYNC_REFRESH_PRESENT, &cookie, 1, NULL, 0 );
1815 if ( !no_sync_state_change ) {
1817 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1818 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1821 if ( ps_list->o_sync_slog_size > 0 ) {
1822 if ( ps_list->o_sync_state.sid ==
1823 sop->o_sync_state.sid )
1832 rs->sr_err = LDAP_SUCCESS;
1833 rs->sr_rspoid = NULL;
1834 rs->sr_ctrls = NULL;
1835 slap_send_session_log( op, ps_list, rs );
1837 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1840 rs->sr_err = LDAP_SUCCESS;
1841 rs->sr_rspoid = LDAP_SYNC_INFO;
1842 rs->sr_ctrls = NULL;
1843 slap_send_syncinfo( sop, rs,
1844 LDAP_TAG_SYNC_REFRESH_DELETE, &cookie, 1, NULL, 0 );
1847 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1850 /* refreshOnly mode */
1851 struct berval cookie;
1852 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1853 sop->o_sync_state.sid, sop->o_sync_state.rid );
1855 if ( sync_send_present_mode ) {
1856 slap_build_sync_done_ctrl( sop, rs, ctrls,
1857 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1860 if ( !no_sync_state_change ) {
1862 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1863 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1866 if ( ps_list->o_sync_slog_size > 0 ) {
1867 if ( ps_list->o_sync_state.sid ==
1868 sop->o_sync_state.sid ) {
1876 slap_send_session_log( op, ps_list, rs );
1878 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1881 slap_build_sync_done_ctrl( sop, rs, ctrls,
1882 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_DELETES );
1885 rs->sr_ctrls = ctrls;
1886 rs->sr_ref = rs->sr_v2ref;
1887 rs->sr_err = (rs->sr_v2ref == NULL)
1888 ? LDAP_SUCCESS : LDAP_REFERRAL;
1889 rs->sr_rspoid = NULL;
1890 send_ldap_result( sop, rs );
1891 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1892 slap_sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val,
1895 slap_sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1896 ctrls[num_ctrls] = NULL;
1897 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1902 rs->sr_ctrls = NULL;
1903 rs->sr_ref = rs->sr_v2ref;
1904 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1905 rs->sr_rspoid = NULL;
1906 if ( get_pagedresults(sop) > SLAP_CONTROL_IGNORED ) {
1907 send_paged_response( sop, rs, NULL, 0 );
1909 send_ldap_result( sop, rs );
1915 if ( sop->o_refresh_in_progress ) {
1916 sop->o_refresh_in_progress = 0;
1920 rs->sr_err = LDAP_SUCCESS;
1924 if ( sop->o_psearch_finished ) {
1925 avl_free( sop->o_psearch_finished, ch_free );
1928 if( !IS_PSEARCH && e != NULL ) {
1929 /* free reader lock */
1930 #ifdef SLAP_ZONE_ALLOC
1931 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
1933 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1935 ber_bvfree( search_context_csn );
1939 LOCK_ID_FREE( bdb->bi_dbenv, locker );
1941 if( rs->sr_v2ref ) {
1942 ber_bvarray_free( rs->sr_v2ref );
1943 rs->sr_v2ref = NULL;
1945 if( realbase.bv_val ) ch_free( realbase.bv_val );
1951 static int base_candidate(
1956 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1957 e->e_nname.bv_val, (long) e->e_id, 0);
1964 /* Look for "objectClass Present" in this filter.
1965 * Also count depth of filter tree while we're at it.
1967 static int oc_filter(
1976 if( cur > *max ) *max = cur;
1978 switch( f->f_choice ) {
1979 case LDAP_FILTER_PRESENT:
1980 if (f->f_desc == slap_schema.si_ad_objectClass) {
1985 case LDAP_FILTER_AND:
1986 case LDAP_FILTER_OR:
1988 for ( f=f->f_and; f; f=f->f_next ) {
1989 (void) oc_filter(f, cur, max);
1999 static void search_stack_free( void *key, void *data )
2001 ber_memfree_x(data, NULL);
2004 static void *search_stack( Operation *op )
2006 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
2009 if ( op->o_threadctx ) {
2010 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
2013 ret = bdb->bi_search_stack;
2017 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
2019 if ( op->o_threadctx ) {
2020 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
2021 ret, search_stack_free );
2023 bdb->bi_search_stack = ret;
2029 static int search_candidates(
2038 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
2040 Filter f, rf, xf, nf;
2042 AttributeAssertion aa_ref;
2043 #ifdef BDB_SUBENTRIES
2045 AttributeAssertion aa_subentry;
2049 * This routine takes as input a filter (user-filter)
2050 * and rewrites it as follows:
2051 * (&(scope=DN)[(objectClass=subentry)]
2052 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
2055 Debug(LDAP_DEBUG_TRACE,
2056 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
2057 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
2059 xf.f_or = op->oq_search.rs_filter;
2060 xf.f_choice = LDAP_FILTER_OR;
2063 /* If the user's filter uses objectClass=*,
2064 * these clauses are redundant.
2066 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
2067 && !get_subentries_visibility(op)
2069 && !is_sync_protocol(op)
2072 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
2073 /* match referral objects */
2074 struct berval bv_ref = BER_BVC( "referral" );
2075 rf.f_choice = LDAP_FILTER_EQUALITY;
2077 rf.f_av_desc = slap_schema.si_ad_objectClass;
2078 rf.f_av_value = bv_ref;
2079 rf.f_next = xf.f_or;
2086 f.f_choice = LDAP_FILTER_AND;
2088 /* Dummy; we compute scope separately now */
2089 nf.f_choice = SLAPD_FILTER_COMPUTED;
2090 nf.f_result = LDAP_SUCCESS;
2091 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
2092 ? op->oq_search.rs_filter : &xf ;
2093 /* Filter depth increased again, adding dummy clause */
2096 #ifdef BDB_SUBENTRIES
2097 if( get_subentries_visibility( op ) ) {
2098 struct berval bv_subentry = BER_BVC( "subentry" );
2099 sf.f_choice = LDAP_FILTER_EQUALITY;
2100 sf.f_ava = &aa_subentry;
2101 sf.f_av_desc = slap_schema.si_ad_objectClass;
2102 sf.f_av_value = bv_subentry;
2103 sf.f_next = nf.f_next;
2108 /* Allocate IDL stack, plus 1 more for former tmp */
2109 if ( depth+1 > bdb->bi_search_stack_depth ) {
2110 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
2112 stack = search_stack( stackop );
2115 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
2116 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
2118 rc = bdb_dn2idl( op, e, ids, stack );
2121 if ( rc == LDAP_SUCCESS ) {
2122 rc = bdb_filter_candidates( op, &f, ids,
2123 stack, stack+BDB_IDL_UM_SIZE );
2126 if ( depth+1 > bdb->bi_search_stack_depth ) {
2131 Debug(LDAP_DEBUG_TRACE,
2132 "bdb_search_candidates: failed (rc=%d)\n",
2136 Debug(LDAP_DEBUG_TRACE,
2137 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
2139 (long) BDB_IDL_FIRST(ids),
2140 (long) BDB_IDL_LAST(ids) );
2147 parse_paged_cookie( Operation *op, SlapReply *rs )
2150 int rc = LDAP_SUCCESS;
2154 struct berval cookie = BER_BVNULL;
2155 PagedResultsState *ps = op->o_pagedresults_state;
2157 /* this function must be invoked only if the pagedResults
2158 * control has been detected, parsed and partially checked
2159 * by the frontend */
2160 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
2162 /* look for the appropriate ctrl structure */
2163 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
2164 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
2170 if ( c[0] == NULL ) {
2171 rs->sr_text = "missing pagedResults control";
2172 return LDAP_PROTOCOL_ERROR;
2175 /* Tested by frontend */
2176 assert( c[0]->ldctl_value.bv_len > 0 );
2178 /* Parse the control value
2179 * realSearchControlValue ::= SEQUENCE {
2180 * size INTEGER (0..maxInt),
2181 * -- requested page size from client
2182 * -- result set size estimate from server
2183 * cookie OCTET STRING
2186 ber = ber_init( &c[0]->ldctl_value );
2187 if ( ber == NULL ) {
2188 rs->sr_text = "internal error";
2192 tag = ber_scanf( ber, "{im}", &size, &cookie );
2194 /* Tested by frontend */
2195 assert( tag != LBER_ERROR );
2196 assert( size >= 0 );
2198 /* cookie decoding/checks deferred to backend... */
2199 if ( cookie.bv_len ) {
2200 PagedResultsCookie reqcookie;
2201 if( cookie.bv_len != sizeof( reqcookie ) ) {
2203 rs->sr_text = "paged results cookie is invalid";
2204 rc = LDAP_PROTOCOL_ERROR;
2208 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
2210 if ( reqcookie > ps->ps_cookie ) {
2212 rs->sr_text = "paged results cookie is invalid";
2213 rc = LDAP_PROTOCOL_ERROR;
2216 } else if ( reqcookie < ps->ps_cookie ) {
2217 rs->sr_text = "paged results cookie is invalid or old";
2218 rc = LDAP_UNWILLING_TO_PERFORM;
2223 /* Initial request. Initialize state. */
2225 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
2226 /* There's another pagedResults control on the
2227 * same connection; reject new pagedResults controls
2228 * (allowed by RFC2696) */
2229 rs->sr_text = "paged results cookie unavailable; try later";
2230 rc = LDAP_UNWILLING_TO_PERFORM;
2239 (void)ber_free( ber, 1 );
2245 send_paged_response(
2251 LDAPControl ctrl, *ctrls[2];
2252 BerElementBuffer berbuf;
2253 BerElement *ber = (BerElement *)&berbuf;
2254 PagedResultsCookie respcookie;
2255 struct berval cookie;
2257 Debug(LDAP_DEBUG_ARGS,
2258 "send_paged_response: lastid=0x%08lx nentries=%d\n",
2259 lastid ? *lastid : 0, rs->sr_nentries, NULL );
2261 BER_BVZERO( &ctrl.ldctl_value );
2265 ber_init2( ber, NULL, LBER_USE_DER );
2268 respcookie = ( PagedResultsCookie )(*lastid);
2269 cookie.bv_len = sizeof( respcookie );
2270 cookie.bv_val = (char *)&respcookie;
2273 respcookie = ( PagedResultsCookie )0;
2274 BER_BVSTR( &cookie, "" );
2277 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
2278 op->o_conn->c_pagedresults_state.ps_count =
2279 ((PagedResultsState *)op->o_pagedresults_state)->ps_count + rs->sr_nentries;
2281 /* return size of 0 -- no estimate */
2282 ber_printf( ber, "{iO}", 0, &cookie );
2284 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
2288 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
2289 ctrls[0]->ldctl_iscritical = 0;
2291 rs->sr_ctrls = ctrls;
2292 rs->sr_err = LDAP_SUCCESS;
2293 send_ldap_result( op, rs );
2294 rs->sr_ctrls = NULL;
2297 (void) ber_free_buf( ber );
2301 bdb_pfid_cmp( const void *v_id1, const void *v_id2 )
2303 const ID *p1 = v_id1, *p2 = v_id2;
2308 bdb_id_dup( Operation *op, ID *id )
2311 new = ch_malloc( sizeof(ID) );