1 /* search.c - search operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
25 static int base_candidate(
30 static int search_candidates(
31 Operation *stackop, /* op with the current threadctx/slab cache */
32 Operation *sop, /* search op */
39 static int parse_paged_cookie( Operation *op, SlapReply *rs );
41 static void send_paged_response(
47 static int bdb_pfid_cmp( const void *v_id1, const void *v_id2 );
48 static ID* bdb_id_dup( Operation *op, ID *id );
50 /* Dereference aliases for a single alias entry. Return the final
51 * dereferenced entry on success, NULL on any failure.
53 static Entry * deref_base (
63 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
68 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
69 rs->sr_text = "maximum deref depth exceeded";
71 while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
72 /* Remember the last entry we looked at, so we can
77 /* If this is part of a subtree or onelevel search,
78 * have we seen this ID before? If so, quit.
80 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
85 /* If we've seen this ID during this deref iteration,
88 if ( bdb_idl_insert( tmp, e->e_id ) ) {
89 rs->sr_err = LDAP_ALIAS_PROBLEM;
90 rs->sr_text = "circular alias";
95 /* If there was a problem getting the aliasedObjectName,
96 * get_alias_dn will have set the error status.
98 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
103 rs->sr_err = bdb_dn2entry( op, NULL, &ndn, &ei,
113 rs->sr_err = LDAP_ALIAS_PROBLEM;
114 rs->sr_text = "aliasedObject not found";
118 /* Free the previous entry, continue to work with the
119 * one we just retrieved.
121 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
125 /* We found a regular entry. Return this to the caller. The
126 * entry is still locked for Read.
128 if (!is_entry_alias(e)) {
129 rs->sr_err = LDAP_SUCCESS;
137 /* Look for and dereference all aliases within the search scope. Adds
138 * the dereferenced entries to the "ids" list. Requires "stack" to be
139 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
140 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
142 static int search_aliases(
151 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
152 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
153 ID cursora, ida, cursoro, ido, *subscop2;
156 struct berval bv_alias = BER_BVC( "alias" );
157 AttributeAssertion aa_alias;
159 DB_LOCK locka, lockr;
162 aliases = stack; /* IDL of all aliases in the database */
163 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
164 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
165 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
166 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
167 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
168 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
170 /* A copy of subscop, because subscop gets clobbered by
171 * the bdb_idl_union/intersection routines
173 subscop2 = tmp + BDB_IDL_DB_SIZE;
175 af.f_choice = LDAP_FILTER_EQUALITY;
176 af.f_ava = &aa_alias;
177 af.f_av_desc = slap_schema.si_ad_objectClass;
178 af.f_av_value = bv_alias;
181 /* Find all aliases in database */
182 BDB_IDL_ZERO( aliases );
183 rs->sr_err = bdb_filter_candidates( op, &af, aliases,
185 if (rs->sr_err != LDAP_SUCCESS) {
189 oldsubs[1] = e->e_id;
192 BDB_IDL_ZERO( visited );
193 BDB_IDL_ZERO( newsubs );
196 ido = bdb_idl_first( oldsubs, &cursoro );
199 /* Set curscop to only the aliases in the current scope. Start with
200 * all the aliases, obtain the IDL for the current scope, and then
201 * get the intersection of these two IDLs. Add the current scope
202 * to the cumulative list of candidates.
204 BDB_IDL_CPY( curscop, aliases );
205 rs->sr_err = bdb_dn2idl( op, e, subscop,
206 subscop2+BDB_IDL_DB_SIZE );
210 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
212 BDB_IDL_CPY(subscop2, subscop);
213 rs->sr_err = bdb_idl_intersection(curscop, subscop);
214 bdb_idl_union( ids, subscop2 );
216 /* Dereference all of the aliases in the current scope. */
218 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
219 ida = bdb_idl_next(curscop, &cursora))
223 rs->sr_err = bdb_cache_find_id(op, NULL,
224 ida, &ei, 0, locker, &lockr );
225 if (rs->sr_err != LDAP_SUCCESS) {
226 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
227 rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
232 /* This should only happen if the curscop IDL has maxed out and
233 * turned into a range that spans IDs indiscriminately
235 if (!is_entry_alias(a)) {
236 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
241 /* Actually dereference the alias */
243 a = deref_base( op, rs, a, &matched, locker, &lockr,
246 /* If the target was not already in our current candidates,
247 * make note of it in the newsubs list. Also
248 * set it in the scopes list so that bdb_search
251 if (bdb_idl_insert(ids, a->e_id) == 0) {
252 bdb_idl_insert(newsubs, a->e_id);
253 bdb_idl_insert(scopes, a->e_id);
255 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
258 } else if (matched) {
259 /* Alias could not be dereferenced, or it deref'd to
260 * an ID we've already seen. Ignore it.
262 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
267 /* If this is a OneLevel search, we're done; oldsubs only had one
268 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
270 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
272 ido = bdb_idl_next( oldsubs, &cursoro );
274 /* If we're done processing the old scopes, did we add any new
275 * scopes in this iteration? If so, go back and do those now.
278 if (BDB_IDL_IS_ZERO(newsubs)) break;
279 BDB_IDL_CPY(oldsubs, newsubs);
280 BDB_IDL_ZERO(newsubs);
282 ido = bdb_idl_first( oldsubs, &cursoro );
285 /* Find the entry corresponding to the next scope. If it can't
286 * be found, ignore it and move on. This should never happen;
287 * we should never see the ID of an entry that doesn't exist.
288 * Set the name so that the scope's IDL can be retrieved.
292 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
294 if ( rs->sr_err != LDAP_SUCCESS ) {
295 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
296 rs->sr_err == DB_LOCK_NOTGRANTED )
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 ) {
839 ber_dupbv( &matched_dn, &matched->e_name );
841 erefs = is_entry_referral( matched )
842 ? get_entry_referrals( op, matched )
845 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
850 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
851 &sop->o_req_dn, sop->oq_search.rs_scope );
852 ber_bvarray_free( erefs );
856 rs->sr_ref = referral_rewrite( default_referral,
857 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
860 rs->sr_err = LDAP_REFERRAL;
861 rs->sr_matched = matched_dn.bv_val;
862 send_ldap_result( sop, rs );
865 LOCK_ID_FREE (bdb->bi_dbenv, locker );
867 ber_bvarray_free( rs->sr_ref );
870 if ( matched_dn.bv_val ) {
871 ber_memfree( matched_dn.bv_val );
872 rs->sr_matched = NULL;
877 if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
878 /* entry is a referral, don't allow add */
879 struct berval matched_dn;
882 ber_dupbv( &matched_dn, &e->e_name );
883 erefs = get_entry_referrals( op, e );
885 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
889 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
890 &sop->o_req_dn, sop->oq_search.rs_scope );
891 ber_bvarray_free( erefs );
894 Debug( LDAP_DEBUG_TRACE,
895 LDAP_XSTRING(bdb_search) ": entry is referral\n",
898 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
899 rs->sr_err = LDAP_REFERRAL;
900 rs->sr_matched = matched_dn.bv_val;
901 send_ldap_result( sop, rs );
904 LOCK_ID_FREE (bdb->bi_dbenv, locker );
905 ber_bvarray_free( rs->sr_ref );
907 ber_memfree( matched_dn.bv_val );
908 rs->sr_matched = NULL;
912 if ( get_assert( op ) &&
913 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
915 rs->sr_err = LDAP_ASSERTION_FAILED;
916 send_ldap_result( sop, rs );
920 /* compute it anyway; root does not use it */
921 stoptime = op->o_time + sop->ors_tlimit;
923 /* need normalized dn below */
924 ber_dupbv( &realbase, &e->e_nname );
926 /* Copy info to base, must free entry before accessing the database
927 * in search_candidates, to avoid deadlocks.
929 base.e_private = e->e_private;
930 base.e_nname = realbase;
933 if ( e != &e_root ) {
934 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
940 rs->sr_err = bdb_get_commit_csn( sop, rs, &search_context_csn,
941 locker, &ctxcsn_lock );
943 if ( rs->sr_err != LDAP_SUCCESS ) {
944 send_ldap_error( sop, rs, rs->sr_err,
945 "error in csn management in search" );
949 if ( sop->o_sync_mode != SLAP_SYNC_NONE &&
950 sop->o_sync_state.ctxcsn &&
951 sop->o_sync_state.ctxcsn->bv_val &&
952 ber_bvcmp( &sop->o_sync_state.ctxcsn[0], search_context_csn ) == 0 )
954 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
958 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );
962 /* select candidates */
963 if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
964 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
967 BDB_IDL_ZERO( candidates );
968 BDB_IDL_ZERO( scopes );
969 rs->sr_err = search_candidates( op, sop, rs, &base,
970 locker, candidates, scopes );
974 if ( !IS_PSEARCH && sop->o_sync_mode != SLAP_SYNC_NONE ) {
975 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
979 /* start cursor at beginning of candidates.
984 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
985 cursor = bdb_idl_search( candidates, ps_e->e_id );
986 if ( candidates[cursor] != ps_e->e_id ) {
987 rs->sr_err = LDAP_SUCCESS;
990 } else if ( ps_e->e_id < BDB_IDL_RANGE_FIRST( candidates ) ||
991 ps_e->e_id > BDB_IDL_RANGE_LAST( candidates ))
993 rs->sr_err = LDAP_SUCCESS;
997 candidates[1] = ps_e->e_id;
1001 if ( candidates[0] == 0 ) {
1002 Debug( LDAP_DEBUG_TRACE,
1003 LDAP_XSTRING(bdb_search) ": no candidates\n",
1009 /* if not root and candidates exceed to-be-checked entries, abort */
1010 if ( sop->ors_limit /* isroot == FALSE */ &&
1011 sop->ors_limit->lms_s_unchecked != -1 &&
1012 BDB_IDL_N(candidates) > (unsigned) sop->ors_limit->lms_s_unchecked )
1014 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1015 send_ldap_result( sop, rs );
1016 rs->sr_err = LDAP_SUCCESS;
1020 if ( sop->ors_limit == NULL /* isroot == TRUE */ ||
1021 !sop->ors_limit->lms_s_pr_hide )
1023 tentries = BDB_IDL_N(candidates);
1026 if ( get_pagedresults( sop ) > SLAP_CONTROL_IGNORED ) {
1027 PagedResultsState *ps = sop->o_pagedresults_state;
1028 /* deferred cookie parsing */
1029 rs->sr_err = parse_paged_cookie( sop, rs );
1030 if ( rs->sr_err != LDAP_SUCCESS ) {
1031 send_ldap_result( sop, rs );
1035 if ( (ID)( ps->ps_cookie ) == 0 ) {
1036 id = bdb_idl_first( candidates, &cursor );
1039 if ( ps->ps_size == 0 ) {
1040 rs->sr_err = LDAP_SUCCESS;
1041 rs->sr_text = "search abandoned by pagedResult size=0";
1042 send_ldap_result( sop, rs );
1045 for ( id = bdb_idl_first( candidates, &cursor );
1047 id <= (ID)( ps->ps_cookie );
1048 id = bdb_idl_next( candidates, &cursor ) )
1054 if ( cursor == NOID ) {
1055 Debug( LDAP_DEBUG_TRACE,
1056 LDAP_XSTRING(bdb_search)
1057 ": no paged results candidates\n",
1059 send_paged_response( sop, rs, &lastid, 0 );
1061 rs->sr_err = LDAP_OTHER;
1068 if (( sop->o_sync_mode & SLAP_SYNC_REFRESH ) || IS_PSEARCH ) {
1071 cookief.f_choice = LDAP_FILTER_AND;
1072 cookief.f_and = &csnfnot;
1073 cookief.f_next = NULL;
1075 csnfnot.f_choice = LDAP_FILTER_NOT;
1076 csnfnot.f_not = &csnfeq;
1077 csnfnot.f_next = &csnfand;
1079 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
1080 csnfeq.f_ava = &aa_eq;
1081 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
1082 if ( sop->o_sync_state.ctxcsn != NULL ) {
1083 csnfeq.f_av_value = *sop->o_sync_state.ctxcsn;
1085 csnfeq.f_av_value = slap_empty_bv;
1088 csnfand.f_choice = LDAP_FILTER_AND;
1089 csnfand.f_and = &csnfge;
1090 csnfand.f_next = NULL;
1092 csnfge.f_choice = LDAP_FILTER_GE;
1093 csnfge.f_ava = &aa_ge;
1094 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
1095 if ( sop->o_sync_state.ctxcsn != NULL ) {
1096 csnfge.f_av_value = *sop->o_sync_state.ctxcsn;
1098 csnfge.f_av_value = slap_empty_bv;
1101 if ( search_context_csn && !IS_PSEARCH ) {
1102 csnfge.f_next = &contextcsnand;
1104 contextcsnand.f_choice = LDAP_FILTER_AND;
1105 contextcsnand.f_and = &contextcsnle;
1106 contextcsnand.f_next = NULL;
1108 contextcsnle.f_choice = LDAP_FILTER_LE;
1109 contextcsnle.f_ava = &aa_le;
1110 contextcsnle.f_av_desc = slap_schema.si_ad_entryCSN;
1111 contextcsnle.f_av_value = *search_context_csn;
1112 contextcsnle.f_next = sop->oq_search.rs_filter;
1114 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
1115 if ( sop->o_sync_state.ctxcsn &&
1116 sop->o_sync_state.ctxcsn->bv_val != NULL )
1118 value_match( &match, slap_schema.si_ad_entryCSN, mr,
1119 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1120 &sop->o_sync_state.ctxcsn[0], search_context_csn,
1125 no_sync_state_change = ( match >= 0 );
1127 csnfge.f_next = sop->oq_search.rs_filter;
1134 for ( id = bdb_idl_first( candidates, &cursor );
1137 && !no_sync_state_change
1139 ; id = bdb_idl_next( candidates, &cursor ) )
1147 if ( !IS_POST_SEARCH ) {
1148 idhole = (ID*) avl_find( sop->o_psearch_finished,
1149 (caddr_t)&id, bdb_pfid_cmp );
1151 avl_delete( &sop->o_psearch_finished,
1152 (caddr_t)idhole, bdb_pfid_cmp );
1153 sop->o_tmpfree( idhole, sop->o_tmpmemctx );
1157 if ( sop->o_refresh_in_progress ) {
1158 ldap_pvt_thread_mutex_lock( &sop->o_pcmutex );
1159 pce = LDAP_TAILQ_FIRST( &sop->o_ps_pre_candidates );
1160 while ( pce && pce->pc_sent ) {
1161 pce = LDAP_TAILQ_NEXT( pce, pc_link );
1163 ldap_pvt_thread_mutex_unlock( &sop->o_pcmutex );
1166 if ( BDB_IDL_IS_RANGE( candidates ) ) {
1167 if ( pce->pc_id >= candidates[1] &&
1168 pce->pc_id <= candidates[2] &&
1169 pce->pc_id > cursor-1 ) {
1172 avl_insert( &sop->o_psearch_finished,
1173 (caddr_t)bdb_id_dup( sop, &pce->pc_id ),
1174 bdb_pfid_cmp, avl_dup_error );
1179 pos = bdb_idl_search(candidates, pce->pc_id);
1180 if ( pos > cursor-1 && pos <= candidates[0] ) {
1183 avl_insert( &sop->o_psearch_finished,
1184 (caddr_t)bdb_id_dup( sop, &pce->pc_id ),
1185 bdb_pfid_cmp, avl_dup_error );
1194 /* check for abandon */
1195 if ( sop->o_abandon ) {
1197 bdb_drop_psearch( sop, sop->o_msgid );
1199 rs->sr_err = LDAP_SUCCESS;
1203 if ( sop->o_cancel ) {
1204 assert( sop->o_cancel == SLAP_CANCEL_REQ );
1205 rs->sr_err = LDAP_CANCELLED;
1206 send_ldap_result( sop, rs );
1207 sop->o_cancel = SLAP_CANCEL_ACK;
1208 rs->sr_err = LDAP_SUCCESS;
1212 /* check for abandon */
1213 if ( sop->o_abandon ) {
1214 rs->sr_err = LDAP_SUCCESS;
1219 /* check time limit */
1220 if ( sop->ors_tlimit != SLAP_NO_LIMIT
1221 && slap_get_time() > stoptime )
1223 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1224 rs->sr_ref = rs->sr_v2ref;
1225 send_ldap_result( sop, rs );
1226 rs->sr_err = LDAP_SUCCESS;
1234 /* get the entry with reader lock */
1236 rs->sr_err = bdb_cache_find_id( op, ltid,
1237 id, &ei, 0, locker, &lock );
1239 if (rs->sr_err == LDAP_BUSY) {
1240 rs->sr_text = "ldap server busy";
1241 send_ldap_result( sop, rs );
1244 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
1245 || rs->sr_err == DB_LOCK_NOTGRANTED )
1247 goto id2entry_retry;
1250 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
1258 if ( IS_POST_SEARCH ) {
1259 /* send LDAP_SYNC_DELETE */
1260 rs->sr_entry = e = ps_e;
1261 goto post_search_no_entry;
1264 if( !BDB_IDL_IS_RANGE(candidates) ) {
1265 /* only complain for non-range IDLs */
1266 Debug( LDAP_DEBUG_TRACE,
1267 LDAP_XSTRING(bdb_search)
1268 ": candidate %ld not found\n",
1282 #ifdef BDB_SUBENTRIES
1283 /* FIXME: send all but syncrepl */
1285 if ( !is_sync_protocol( sop ) )
1288 if ( is_entry_subentry( e ) ) {
1289 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
1290 if(!get_subentries_visibility( sop )) {
1291 /* only subentries are visible */
1295 } else if ( get_subentries( sop ) &&
1296 !get_subentries_visibility( sop ))
1298 /* only subentries are visible */
1302 } else if ( get_subentries_visibility( sop )) {
1303 /* only subentries are visible */
1307 #endif /* BDB_SUBENTRIES */
1309 /* Does this candidate actually satisfy the search scope?
1311 * Note that we don't lock access to the bei_parent pointer.
1312 * Since only leaf nodes can be deleted, the parent of any
1313 * node will always be a valid node. Also since we have
1314 * a Read lock on the data, it cannot be renamed out of the
1315 * scope while we are looking at it, and unless we're using
1316 * BDB_HIER, its parents cannot be moved either.
1318 switch( sop->ors_scope ) {
1319 case LDAP_SCOPE_BASE:
1320 /* This is always true, yes? */
1321 if ( id == base.e_id ) scopeok = 1;
1324 case LDAP_SCOPE_ONELEVEL:
1325 if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
1328 #ifdef LDAP_SCOPE_CHILDREN
1329 case LDAP_SCOPE_CHILDREN:
1330 if ( id == base.e_id ) break;
1333 case LDAP_SCOPE_SUBTREE: {
1335 for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
1336 if ( tmp->bei_id == base.e_id ) {
1344 /* aliases were already dereferenced in candidate list */
1345 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
1346 /* but if the search base is an alias, and we didn't
1347 * deref it when finding, return it.
1349 if ( is_entry_alias(e) &&
1350 ((sop->ors_deref & LDAP_DEREF_FINDING) ||
1351 !bvmatch(&e->e_nname, &op->o_req_ndn)))
1356 /* scopes is only non-empty for onelevel or subtree */
1357 if ( !scopeok && BDB_IDL_N(scopes) ) {
1359 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
1360 x = bdb_idl_search( scopes, e->e_id );
1361 if ( scopes[x] == e->e_id ) scopeok = 1;
1363 /* subtree, walk up the tree */
1364 EntryInfo *tmp = BEI(e);
1365 for (;tmp->bei_parent; tmp=tmp->bei_parent) {
1366 x = bdb_idl_search( scopes, tmp->bei_id );
1367 if ( scopes[x] == tmp->bei_id ) {
1376 /* Not in scope, ignore it */
1378 if ( !IS_POST_SEARCH && !scopeok )
1383 Debug( LDAP_DEBUG_TRACE,
1384 LDAP_XSTRING(bdb_search)
1385 ": %ld scope not okay\n",
1391 * if it's a referral, add it to the list of referrals. only do
1392 * this for non-base searches, and don't check the filter
1393 * explicitly here since it's only a candidate anyway.
1395 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1396 && is_entry_referral( e ) )
1398 BerVarray erefs = get_entry_referrals( sop, e );
1399 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1400 sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1401 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1403 send_search_reference( sop, rs );
1405 ber_bvarray_free( rs->sr_ref );
1406 ber_bvarray_free( erefs );
1412 if ( !manageDSAit && is_entry_glue( e )) {
1416 /* if it matches the filter and scope, send it */
1418 rs->sr_err = test_filter( sop, rs->sr_entry, sop->oq_search.rs_filter );
1421 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1422 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1424 rs->sr_err = LDAP_COMPARE_TRUE;
1428 if ( !IS_POST_SEARCH ) {
1429 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1430 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1431 rs->sr_err = test_filter( sop, rs->sr_entry,
1433 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1434 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1435 if ( no_sync_state_change ) {
1436 Debug( LDAP_DEBUG_TRACE,
1437 LDAP_XSTRING(bdb_search) ": "
1438 "error in context csn management\n",
1441 entry_sync_state = LDAP_SYNC_ADD;
1444 if ( no_sync_state_change ) {
1447 entry_sync_state = LDAP_SYNC_PRESENT;
1451 rs->sr_err = test_filter( sop,
1452 rs->sr_entry, sop->oq_search.rs_filter );
1456 rs->sr_err = test_filter( sop,
1457 rs->sr_entry, sop->oq_search.rs_filter );
1459 rs->sr_err = LDAP_COMPARE_TRUE;
1465 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1466 /* check size limit */
1467 if ( --sop->ors_slimit == -1
1469 && sop->o_sync_slog_size == -1
1475 bdb_cache_return_entry_r( bdb->bi_dbenv,
1476 &bdb->bi_cache, e, &lock );
1481 rs->sr_entry = NULL;
1482 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1483 rs->sr_ref = rs->sr_v2ref;
1484 send_ldap_result( sop, rs );
1485 rs->sr_err = LDAP_SUCCESS;
1489 if ( get_pagedresults(sop) > SLAP_CONTROL_IGNORED ) {
1490 if ( rs->sr_nentries >= ((PagedResultsState *)sop->o_pagedresults_state)->ps_size ) {
1491 send_paged_response( sop, rs, &lastid, tentries );
1501 if (IS_PSEARCH || IS_POST_SEARCH) {
1502 int premodify_found = 0;
1504 if ( IS_POST_SEARCH ||
1505 ps_type == LDAP_PSEARCH_BY_ADD ||
1506 ps_type == LDAP_PSEARCH_BY_DELETE ||
1507 ps_type == LDAP_PSEARCH_BY_MODIFY ||
1508 ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1510 if ( !IS_POST_SEARCH &&
1511 ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1512 struct psid_entry* psid_e;
1513 LDAP_LIST_FOREACH( psid_e,
1514 &op->o_pm_list, ps_link)
1516 if( psid_e->ps_op == sop ) {
1517 premodify_found = 1;
1518 LDAP_LIST_REMOVE(psid_e, ps_link);
1522 if (psid_e != NULL) free (psid_e);
1525 if ( IS_POST_SEARCH ) {
1527 entry_sync_state = LDAP_SYNC_ADD;
1529 post_search_no_entry:
1530 entry_sync_state = LDAP_SYNC_DELETE;
1532 } else if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1533 entry_sync_state = LDAP_SYNC_ADD;
1534 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1535 entry_sync_state = LDAP_SYNC_DELETE;
1536 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1537 if ( premodify_found ) {
1538 entry_sync_state = LDAP_SYNC_MODIFY;
1540 entry_sync_state = LDAP_SYNC_ADD;
1542 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT ) {
1543 entry_sync_state = LDAP_SYNC_DELETE;
1545 rs->sr_err = LDAP_OTHER;
1549 if ( sop->o_sync_slog_size != -1 ) {
1550 if ( entry_sync_state == LDAP_SYNC_DELETE ) {
1551 result = slap_add_session_log( op, sop, e );
1556 struct berval cookie;
1557 slap_compose_sync_cookie( sop, &cookie,
1559 sop->o_sync_state.sid,
1560 sop->o_sync_state.rid );
1561 rs->sr_err = slap_build_sync_state_ctrl(
1562 sop, rs, e, entry_sync_state, ctrls,
1563 num_ctrls++, 1, &cookie );
1564 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1565 if (!(IS_POST_SEARCH &&
1566 entry_sync_state == LDAP_SYNC_DELETE)) {
1567 rs->sr_attrs = attrs;
1569 rs->sr_attrs = NULL;
1571 rs->sr_operational_attrs = NULL;
1572 rs->sr_ctrls = ctrls;
1574 result = send_search_entry( sop, rs );
1575 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1577 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1579 slap_sl_free( ctrls[--num_ctrls],
1581 ctrls[num_ctrls] = NULL;
1582 rs->sr_ctrls = NULL;
1585 } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1586 struct psid_entry* psid_e;
1587 psid_e = (struct psid_entry *) ch_calloc(1,
1588 sizeof(struct psid_entry));
1589 psid_e->ps_op = sop;
1590 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1594 Debug( LDAP_DEBUG_TRACE,
1595 LDAP_XSTRING(bdb_search)
1596 ": invalid ps_type (%d) \n",
1601 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1602 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1603 rs->sr_err = slap_build_sync_state_ctrl(
1604 sop, rs, e, entry_sync_state, ctrls,
1605 num_ctrls++, 0, NULL );
1606 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1607 rs->sr_ctrls = ctrls;
1608 rs->sr_attrs = sop->oq_search.rs_attrs;
1609 rs->sr_operational_attrs = NULL;
1611 result = send_search_entry( sop, rs );
1613 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1615 slap_sl_free( ctrls[--num_ctrls],
1617 ctrls[num_ctrls] = NULL;
1618 rs->sr_ctrls = NULL;
1620 } else { /* PRESENT */
1621 if ( sync_send_present_mode ) {
1622 result = slap_build_syncUUID_set( sop,
1624 if ( result <= 0 ) {
1628 if ( syncUUID_set_cnt ==
1629 SLAP_SYNCUUID_SET_SIZE )
1631 rs->sr_err = LDAP_SUCCESS;
1632 rs->sr_rspoid = LDAP_SYNC_INFO;
1633 rs->sr_ctrls = NULL;
1634 result = slap_send_syncinfo( sop, rs,
1635 LDAP_TAG_SYNC_ID_SET,
1636 NULL, 0, syncUUID_set, 0 );
1637 if ( result != LDAP_SUCCESS ) {
1640 ber_bvarray_free_x( syncUUID_set,
1642 syncUUID_set = NULL;
1643 syncUUID_set_cnt = 0;
1654 rs->sr_attrs = sop->oq_search.rs_attrs;
1655 rs->sr_operational_attrs = NULL;
1656 rs->sr_ctrls = NULL;
1658 rs->sr_err = LDAP_SUCCESS;
1659 result = send_search_entry( sop, rs );
1666 case 0: /* entry sent ok */
1668 case 1: /* entry not sent */
1670 case -1: /* connection closed */
1674 bdb_cache_return_entry_r(bdb->bi_dbenv,
1675 &bdb->bi_cache, e, &lock);
1680 rs->sr_entry = NULL;
1681 rs->sr_err = LDAP_OTHER;
1687 Debug( LDAP_DEBUG_TRACE,
1688 LDAP_XSTRING(bdb_search)
1689 ": %ld does not match filter\n",
1695 /* free reader lock */
1698 if (!(IS_POST_SEARCH &&
1699 entry_sync_state == LDAP_SYNC_DELETE)) {
1700 bdb_cache_return_entry_r( bdb->bi_dbenv,
1701 &bdb->bi_cache, e , &lock );
1702 if ( sop->o_nocaching ) {
1703 bdb_cache_delete_entry( bdb, ei, locker, &lock );
1708 bdb_cache_return_entry_r( bdb->bi_dbenv,
1709 &bdb->bi_cache, e , &lock );
1712 rs->sr_entry = NULL;
1716 if ( sop->o_refresh_in_progress ) {
1723 ldap_pvt_thread_yield();
1727 if ( syncUUID_set_cnt > 0 ) {
1728 rs->sr_err = LDAP_SUCCESS;
1729 rs->sr_rspoid = LDAP_SYNC_INFO;
1730 rs->sr_ctrls = NULL;
1731 slap_send_syncinfo( sop, rs, LDAP_TAG_SYNC_ID_SET,
1732 NULL, 0, syncUUID_set, 0 );
1733 ber_bvarray_free_x( syncUUID_set, sop->o_tmpmemctx );
1734 syncUUID_set_cnt = 0;
1740 if (!IS_PSEARCH && !IS_POST_SEARCH) {
1741 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1742 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1743 struct berval cookie;
1744 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1745 sop->o_sync_state.sid, sop->o_sync_state.rid );
1747 if ( sync_send_present_mode ) {
1748 rs->sr_err = LDAP_SUCCESS;
1749 rs->sr_rspoid = LDAP_SYNC_INFO;
1750 rs->sr_ctrls = NULL;
1751 slap_send_syncinfo( sop, rs,
1752 LDAP_TAG_SYNC_REFRESH_PRESENT, &cookie, 1, NULL, 0 );
1755 if ( !no_sync_state_change ) {
1757 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1758 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1761 if ( ps_list->o_sync_slog_size > 0 ) {
1762 if ( ps_list->o_sync_state.sid ==
1763 sop->o_sync_state.sid )
1772 rs->sr_err = LDAP_SUCCESS;
1773 rs->sr_rspoid = NULL;
1774 rs->sr_ctrls = NULL;
1775 slap_send_session_log( op, ps_list, rs );
1777 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1780 rs->sr_err = LDAP_SUCCESS;
1781 rs->sr_rspoid = LDAP_SYNC_INFO;
1782 rs->sr_ctrls = NULL;
1783 slap_send_syncinfo( sop, rs,
1784 LDAP_TAG_SYNC_REFRESH_DELETE, &cookie, 1, NULL, 0 );
1787 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1790 /* refreshOnly mode */
1791 struct berval cookie;
1792 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1793 sop->o_sync_state.sid, sop->o_sync_state.rid );
1795 if ( sync_send_present_mode ) {
1796 slap_build_sync_done_ctrl( sop, rs, ctrls,
1797 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1800 if ( !no_sync_state_change ) {
1802 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1803 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1806 if ( ps_list->o_sync_slog_size > 0 ) {
1807 if ( ps_list->o_sync_state.sid ==
1808 sop->o_sync_state.sid ) {
1816 slap_send_session_log( op, ps_list, rs );
1818 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1821 slap_build_sync_done_ctrl( sop, rs, ctrls,
1822 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_DELETES );
1825 rs->sr_ctrls = ctrls;
1826 rs->sr_ref = rs->sr_v2ref;
1827 rs->sr_err = (rs->sr_v2ref == NULL)
1828 ? LDAP_SUCCESS : LDAP_REFERRAL;
1829 rs->sr_rspoid = NULL;
1830 send_ldap_result( sop, rs );
1831 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1832 slap_sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val,
1835 slap_sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1836 ctrls[num_ctrls] = NULL;
1837 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1842 rs->sr_ctrls = NULL;
1843 rs->sr_ref = rs->sr_v2ref;
1844 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1845 rs->sr_rspoid = NULL;
1846 if ( get_pagedresults(sop) > SLAP_CONTROL_IGNORED ) {
1847 send_paged_response( sop, rs, NULL, 0 );
1849 send_ldap_result( sop, rs );
1855 if ( sop->o_refresh_in_progress ) {
1856 sop->o_refresh_in_progress = 0;
1860 rs->sr_err = LDAP_SUCCESS;
1864 if ( sop->o_psearch_finished ) {
1865 avl_free( sop->o_psearch_finished, ch_free );
1868 if( !IS_PSEARCH && e != NULL ) {
1869 /* free reader lock */
1870 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1872 ber_bvfree( search_context_csn );
1876 LOCK_ID_FREE( bdb->bi_dbenv, locker );
1878 if( rs->sr_v2ref ) {
1879 ber_bvarray_free( rs->sr_v2ref );
1880 rs->sr_v2ref = NULL;
1882 if( realbase.bv_val ) ch_free( realbase.bv_val );
1888 static int base_candidate(
1893 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1894 e->e_nname.bv_val, (long) e->e_id, 0);
1901 /* Look for "objectClass Present" in this filter.
1902 * Also count depth of filter tree while we're at it.
1904 static int oc_filter(
1913 if( cur > *max ) *max = cur;
1915 switch( f->f_choice ) {
1916 case LDAP_FILTER_PRESENT:
1917 if (f->f_desc == slap_schema.si_ad_objectClass) {
1922 case LDAP_FILTER_AND:
1923 case LDAP_FILTER_OR:
1925 for ( f=f->f_and; f; f=f->f_next ) {
1926 (void) oc_filter(f, cur, max);
1936 static void search_stack_free( void *key, void *data )
1938 ber_memfree_x(data, NULL);
1941 static void *search_stack( Operation *op )
1943 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1946 if ( op->o_threadctx ) {
1947 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1950 ret = bdb->bi_search_stack;
1954 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1956 if ( op->o_threadctx ) {
1957 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1958 ret, search_stack_free );
1960 bdb->bi_search_stack = ret;
1966 static int search_candidates(
1975 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1977 Filter f, rf, xf, nf;
1979 AttributeAssertion aa_ref;
1980 #ifdef BDB_SUBENTRIES
1982 AttributeAssertion aa_subentry;
1986 * This routine takes as input a filter (user-filter)
1987 * and rewrites it as follows:
1988 * (&(scope=DN)[(objectClass=subentry)]
1989 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1992 Debug(LDAP_DEBUG_TRACE,
1993 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1994 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1996 xf.f_or = op->oq_search.rs_filter;
1997 xf.f_choice = LDAP_FILTER_OR;
2000 /* If the user's filter uses objectClass=*,
2001 * these clauses are redundant.
2003 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
2004 && !get_subentries_visibility(op)
2006 && !is_sync_protocol(op)
2009 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
2010 /* match referral objects */
2011 struct berval bv_ref = BER_BVC( "referral" );
2012 rf.f_choice = LDAP_FILTER_EQUALITY;
2014 rf.f_av_desc = slap_schema.si_ad_objectClass;
2015 rf.f_av_value = bv_ref;
2016 rf.f_next = xf.f_or;
2023 f.f_choice = LDAP_FILTER_AND;
2025 /* Dummy; we compute scope separately now */
2026 nf.f_choice = SLAPD_FILTER_COMPUTED;
2027 nf.f_result = LDAP_SUCCESS;
2028 nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
2029 ? op->oq_search.rs_filter : &xf ;
2030 /* Filter depth increased again, adding dummy clause */
2033 #ifdef BDB_SUBENTRIES
2034 if( get_subentries_visibility( op ) ) {
2035 struct berval bv_subentry = BER_BVC( "subentry" );
2036 sf.f_choice = LDAP_FILTER_EQUALITY;
2037 sf.f_ava = &aa_subentry;
2038 sf.f_av_desc = slap_schema.si_ad_objectClass;
2039 sf.f_av_value = bv_subentry;
2040 sf.f_next = nf.f_next;
2045 /* Allocate IDL stack, plus 1 more for former tmp */
2046 if ( depth+1 > bdb->bi_search_stack_depth ) {
2047 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
2049 stack = search_stack( stackop );
2052 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
2053 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
2055 rc = bdb_dn2idl( op, e, ids, stack );
2058 if ( rc == LDAP_SUCCESS ) {
2059 rc = bdb_filter_candidates( op, &f, ids,
2060 stack, stack+BDB_IDL_UM_SIZE );
2063 if ( depth+1 > bdb->bi_search_stack_depth ) {
2068 Debug(LDAP_DEBUG_TRACE,
2069 "bdb_search_candidates: failed (rc=%d)\n",
2073 Debug(LDAP_DEBUG_TRACE,
2074 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
2076 (long) BDB_IDL_FIRST(ids),
2077 (long) BDB_IDL_LAST(ids) );
2084 parse_paged_cookie( Operation *op, SlapReply *rs )
2087 int rc = LDAP_SUCCESS;
2091 struct berval cookie = BER_BVNULL;
2092 PagedResultsState *ps = op->o_pagedresults_state;
2094 /* this function must be invoked only if the pagedResults
2095 * control has been detected, parsed and partially checked
2096 * by the frontend */
2097 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
2099 /* look for the appropriate ctrl structure */
2100 for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
2101 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
2107 if ( c[0] == NULL ) {
2108 rs->sr_text = "missing pagedResults control";
2109 return LDAP_PROTOCOL_ERROR;
2112 /* Tested by frontend */
2113 assert( c[0]->ldctl_value.bv_len > 0 );
2115 /* Parse the control value
2116 * realSearchControlValue ::= SEQUENCE {
2117 * size INTEGER (0..maxInt),
2118 * -- requested page size from client
2119 * -- result set size estimate from server
2120 * cookie OCTET STRING
2123 ber = ber_init( &c[0]->ldctl_value );
2124 if ( ber == NULL ) {
2125 rs->sr_text = "internal error";
2129 tag = ber_scanf( ber, "{im}", &size, &cookie );
2131 /* Tested by frontend */
2132 assert( tag != LBER_ERROR );
2133 assert( size >= 0 );
2135 /* cookie decoding/checks deferred to backend... */
2136 if ( cookie.bv_len ) {
2137 PagedResultsCookie reqcookie;
2138 if( cookie.bv_len != sizeof( reqcookie ) ) {
2140 rs->sr_text = "paged results cookie is invalid";
2141 rc = LDAP_PROTOCOL_ERROR;
2145 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
2147 if ( reqcookie > ps->ps_cookie ) {
2149 rs->sr_text = "paged results cookie is invalid";
2150 rc = LDAP_PROTOCOL_ERROR;
2153 } else if ( reqcookie < ps->ps_cookie ) {
2154 rs->sr_text = "paged results cookie is invalid or old";
2155 rc = LDAP_UNWILLING_TO_PERFORM;
2160 /* Initial request. Initialize state. */
2162 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
2163 /* There's another pagedResults control on the
2164 * same connection; reject new pagedResults controls
2165 * (allowed by RFC2696) */
2166 rs->sr_text = "paged results cookie unavailable; try later";
2167 rc = LDAP_UNWILLING_TO_PERFORM;
2176 (void)ber_free( ber, 1 );
2182 send_paged_response(
2188 LDAPControl ctrl, *ctrls[2];
2189 BerElementBuffer berbuf;
2190 BerElement *ber = (BerElement *)&berbuf;
2191 PagedResultsCookie respcookie;
2192 struct berval cookie;
2194 Debug(LDAP_DEBUG_ARGS,
2195 "send_paged_response: lastid=0x%08lx nentries=%d\n",
2196 lastid ? *lastid : 0, rs->sr_nentries, NULL );
2198 BER_BVZERO( &ctrl.ldctl_value );
2202 ber_init2( ber, NULL, LBER_USE_DER );
2205 respcookie = ( PagedResultsCookie )(*lastid);
2206 cookie.bv_len = sizeof( respcookie );
2207 cookie.bv_val = (char *)&respcookie;
2210 respcookie = ( PagedResultsCookie )0;
2211 BER_BVSTR( &cookie, "" );
2214 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
2215 op->o_conn->c_pagedresults_state.ps_count =
2216 ((PagedResultsState *)op->o_pagedresults_state)->ps_count + rs->sr_nentries;
2218 /* return size of 0 -- no estimate */
2219 ber_printf( ber, "{iO}", 0, &cookie );
2221 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
2225 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
2226 ctrls[0]->ldctl_iscritical = 0;
2228 rs->sr_ctrls = ctrls;
2229 rs->sr_err = LDAP_SUCCESS;
2230 send_ldap_result( op, rs );
2231 rs->sr_ctrls = NULL;
2234 (void) ber_free_buf( ber );
2238 bdb_pfid_cmp( const void *v_id1, const void *v_id2 )
2240 const ID *p1 = v_id1, *p2 = v_id2;
2245 bdb_id_dup( Operation *op, ID *id )
2248 new = ch_malloc( sizeof(ID) );