1 /* search.c - search operation */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
17 static int base_candidate(
21 static int search_candidates(
22 Operation *stackop, /* op with the current threadctx/slab cache */
23 Operation *sop, /* search op */
28 static void send_pagerequest_response(
34 /* Dereference aliases for a single alias entry. Return the final
35 * dereferenced entry on success, NULL on any failure.
37 static Entry * deref_base (
47 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
51 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
52 rs->sr_text = "maximum deref depth exceeded";
54 while (BDB_IDL_N(tmp) < be->be_max_deref_depth) {
56 /* Remember the last entry we looked at, so we can
61 /* If this is part of a subtree or onelevel search,
62 * have we seen this ID before? If so, quit.
64 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
69 /* If we've seen this ID during this deref iteration,
72 if ( bdb_idl_insert( tmp, e->e_id ) ) {
73 rs->sr_err = LDAP_ALIAS_PROBLEM;
74 rs->sr_text = "circular alias";
79 /* If there was a problem getting the aliasedObjectName,
80 * get_alias_dn will have set the error status.
82 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
87 rs->sr_err = bdb_dn2entry_r( be, NULL, &ndn, &e,
88 NULL, 0, locker, &lockr );
90 rs->sr_err = LDAP_ALIAS_PROBLEM;
91 rs->sr_text = "aliasedObject not found";
95 /* Free the previous entry, continue to work with the
96 * one we just retrieved.
98 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
102 /* We found a regular entry. Return this to the caller. The
103 * entry is still locked for Read.
105 if (!is_entry_alias(e)) {
106 rs->sr_err = LDAP_SUCCESS;
114 /* Look for and dereference all aliases within the search scope. Adds
115 * the dereferenced entries to the "ids" list. Requires "stack" to be
116 * able to hold 8 levels of IDLs.
118 static int search_aliases(
128 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
129 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
130 ID cursora, ida, cursoro, ido, *subscop2;
132 struct berval bv_alias = { sizeof("alias")-1, "alias" };
133 AttributeAssertion aa_alias;
135 DB_LOCK locka, lockr;
139 aliases = stack; /* IDL of all aliases in the database */
140 curscop = aliases + BDB_IDL_UM_SIZE; /* Aliases in the current scope */
141 subscop = curscop + BDB_IDL_UM_SIZE; /* The current scope */
142 visited = subscop + BDB_IDL_UM_SIZE; /* IDs we've seen in this search */
143 newsubs = visited + BDB_IDL_UM_SIZE; /* New subtrees we've added */
144 oldsubs = newsubs + BDB_IDL_UM_SIZE; /* Subtrees added previously */
145 tmp = oldsubs + BDB_IDL_UM_SIZE; /* Scratch space for deref_base() */
147 /* A copy of subscop, because subscop gets clobbered by
148 * the bdb_idl_union/intersection routines
150 subscop2 = tmp + BDB_IDL_UM_SIZE;
152 af.f_choice = LDAP_FILTER_EQUALITY;
153 af.f_ava = &aa_alias;
154 af.f_av_desc = slap_schema.si_ad_objectClass;
155 af.f_av_value = bv_alias;
158 /* Find all aliases in database */
159 BDB_IDL_ALL( bdb, aliases );
160 rs->sr_err = bdb_filter_candidates( op->o_bd, &af, aliases,
162 if (rs->sr_err != LDAP_SUCCESS) {
166 oldsubs[1] = e->e_id;
169 BDB_IDL_ZERO( visited );
170 BDB_IDL_ZERO( newsubs );
173 ido = bdb_idl_first( oldsubs, &cursoro );
176 /* Set curscop to only the aliases in the current scope. Start with
177 * all the aliases, obtain the IDL for the current scope, and then
178 * get the intersection of these two IDLs. Make sure to save a copy
179 * of the scope IDL since it gets clobbered.
181 BDB_IDL_CPY( curscop, aliases );
182 rs->sr_err = bdb_filter_candidates( op->o_bd, sf, subscop, NULL, NULL );
186 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
188 BDB_IDL_CPY(subscop2, subscop);
189 rs->sr_err = bdb_idl_intersection(curscop, subscop);
190 BDB_IDL_CPY(subscop, subscop2);
192 /* Dereference all of the aliases in the current scope. */
194 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
195 ida = bdb_idl_next(curscop, &cursora))
197 rs->sr_err = bdb_id2entry_r(op->o_bd, NULL, ida, &a,
199 if (rs->sr_err != LDAP_SUCCESS) {
203 /* This should only happen if the curscop IDL has maxed out and
204 * turned into a range that spans IDs indiscriminately
206 if (!is_entry_alias(a)) {
207 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
212 /* Actually dereference the alias */
214 a = deref_base( op->o_bd, rs, a, &matched, locker, &lockr,
217 /* If the target was not already in our current search scope,
218 * make note of it in the newsubs list.
219 * FIXME: Somehow we have to propagate these new scopes back
222 if (bdb_idl_insert(subscop, a->e_id) == 0) {
223 bdb_idl_insert(newsubs, a->e_id);
225 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
228 } else if (matched) {
229 /* Alias could not be dereferenced, or it deref'd to
230 * an ID we've already seen. Ignore it.
232 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
237 /* All of the deref'd aliases have been added to subscop. Add these
238 * to our candidate list.
240 bdb_idl_union( ids, subscop );
242 /* If this is a OneLevel search, we're done; oldsubs only had one
243 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
245 if (op->ors_scope != LDAP_SCOPE_SUBTREE) break;
247 ido = bdb_idl_next( oldsubs, &cursoro );
249 /* If we're done processing the old scopes, did we add any new
250 * scopes in this iteration? If so, go back and do those now.
253 if (BDB_IDL_IS_ZERO(newsubs)) break;
254 BDB_IDL_CPY(oldsubs, newsubs);
255 BDB_IDL_ZERO(newsubs);
257 ido = bdb_idl_first( oldsubs, &cursoro );
260 /* Find the entry corresponding to the next scope. If it can't
261 * be found, ignore it and move on. This should never happen;
262 * we should never see the ID of an entry that doesn't exist.
263 * Set the name so that the scope's IDL can be retrieved.
265 rs->sr_err = bdb_id2entry_r(op->o_bd, NULL, ido, &e, locker, &locka);
266 if (rs->sr_err != LDAP_SUCCESS) goto nextido;
267 sf->f_dn = &e->e_nname;
272 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
273 #define IS_BDB_REPLACE(type) (( type == LDAP_PSEARCH_BY_DELETE ) || \
274 ( type == LDAP_PSEARCH_BY_SCOPEOUT ))
275 #define IS_PSEARCH (op != sop)
278 bdb_abandon( Operation *op, SlapReply *rs )
281 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
283 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
284 if ( ps_list->o_connid == op->o_connid ) {
285 if ( ps_list->o_msgid == op->oq_abandon.rs_msgid ) {
286 ps_list->o_abandon = 1;
287 LDAP_LIST_REMOVE( ps_list, o_ps_link );
288 slap_op_free ( ps_list );
293 return LDAP_UNAVAILABLE;
297 bdb_cancel( Operation *op, SlapReply *rs )
300 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
302 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
303 if ( ps_list->o_connid == op->o_connid ) {
304 if ( ps_list->o_msgid == op->oq_cancel.rs_msgid ) {
305 ps_list->o_cancel = SLAP_CANCEL_DONE;
306 LDAP_LIST_REMOVE( ps_list, o_ps_link );
309 bdb_build_sync_done_ctrl( conn, ps_list, ps_list->ctrls,
310 1, &latest_entrycsn_bv );
311 send_ldap_result( conn, ps_list, LDAP_CANCELLED,
312 NULL, NULL, NULL, ps_list->ctrls, ps_list->nentries);
314 rs->sr_err = LDAP_CANCELLED;
315 send_ldap_result( ps_list, rs );
317 slap_op_free ( ps_list );
322 return LDAP_UNAVAILABLE;
325 int bdb_search( Operation *op, SlapReply *rs )
327 return bdb_do_search( op, rs, op, NULL, 0 );
330 /* For persistent searches, op is the currently executing operation,
331 * sop is the persistent search. For regular searches, sop = op.
334 bdb_do_search( Operation *op, SlapReply *rs, Operation *sop,
335 Entry *ps_e, int ps_type )
339 int bdb_search( Operation *op, SlapReply *rs )
342 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
345 ID candidates[BDB_IDL_UM_SIZE];
347 Entry *matched = NULL;
348 struct berval realbase = { 0, NULL };
352 AttributeName *attrs;
354 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
355 Filter cookief, csnfnot, csnfeq, csnfand, csnfge;
356 AttributeAssertion aa_ge, aa_eq;
359 struct berval entrycsn_bv = { 0, NULL };
361 struct berval latest_entrycsn_bv = { 0, NULL };
362 LDAPControl *ctrls[SLAP_SEARCH_MAX_CTRLS];
364 AttributeName uuid_attr[2];
367 int entry_sync_state = -1;
368 AttributeName null_attr;
371 struct slap_limits_set *limit = NULL;
374 u_int32_t locker = 0;
378 LDAP_LOG( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
380 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
383 attrs = sop->oq_search.rs_attrs;
385 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
386 #ifdef LDAP_CLIENT_UPDATE
387 if ( !IS_PSEARCH && sop->o_clientupdate_type & SLAP_LCUP_PERSIST ) {
388 sop->o_ps_protocol = LDAP_CLIENT_UPDATE;
389 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
394 /* psearch needs to be registered before refresh begins */
395 /* psearch and refresh transmission is serialized in send_ldap_ber() */
396 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
397 sop->o_ps_protocol = LDAP_SYNC;
398 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
400 null_attr.an_desc = NULL;
401 null_attr.an_oc = NULL;
402 null_attr.an_name.bv_len = 0;
403 null_attr.an_name.bv_val = NULL;
406 for ( num_ctrls = 0; num_ctrls < SLAP_SEARCH_MAX_CTRLS; num_ctrls++ ) {
407 ctrls[num_ctrls] = NULL;
411 if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
412 #ifdef LDAP_CLIENT_UPDATE
413 if ( sop->o_ps_protocol == LDAP_CLIENT_UPDATE ) {
415 attrs[0].an_desc = slap_schema.si_ad_entryUUID;
416 attrs[0].an_oc = NULL;
417 attrs[0].an_name = attrs[0].an_desc->ad_cname;
418 attrs[1].an_desc = NULL;
419 attrs[1].an_oc = NULL;
420 attrs[1].an_name.bv_len = 0;
421 attrs[1].an_name.bv_val = NULL;
425 if (sop->o_ps_protocol == LDAP_SYNC ) {
427 attrs[0].an_desc = NULL;
428 attrs[0].an_oc = NULL;
429 attrs[0].an_name.bv_len = 0;
430 attrs[0].an_name.bv_val = NULL;
440 manageDSAit = get_manageDSAit( sop );
442 rs->sr_err = LOCK_ID (bdb->bi_dbenv, &locker );
448 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
452 if ( sop->o_req_ndn.bv_len == 0 ) {
453 /* DIT root special case */
454 e = (Entry *) &slap_entry_root;
458 /* get entry with reader lock */
459 rs->sr_err = bdb_dn2entry_r( op->o_bd, NULL, &sop->o_req_ndn, &e,
460 &matched, 0, locker, &lock );
469 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
471 if (matched != NULL) {
472 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache,
475 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
476 LOCK_ID_FREE (bdb->bi_dbenv, locker );
478 case DB_LOCK_DEADLOCK:
479 case DB_LOCK_NOTGRANTED:
483 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
485 if (matched != NULL) {
486 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache,
489 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
490 LOCK_ID_FREE (bdb->bi_dbenv, locker );
494 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
495 BDB_IDL_ZERO(candidates);
496 e = deref_base( op->o_bd, rs, e, &matched, locker, &lock,
501 struct berval matched_dn = { 0, NULL };
503 if ( matched != NULL ) {
505 ber_dupbv( &matched_dn, &matched->e_name );
507 erefs = is_entry_referral( matched )
508 ? get_entry_referrals( op, matched )
511 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
516 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
517 &sop->o_req_dn, sop->oq_search.rs_scope );
518 ber_bvarray_free( erefs );
522 rs->sr_ref = referral_rewrite( default_referral,
523 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
526 rs->sr_err=LDAP_REFERRAL;
527 rs->sr_matched = matched_dn.bv_val;
528 send_ldap_result( sop, rs );
530 LOCK_ID_FREE (bdb->bi_dbenv, locker );
532 ber_bvarray_free( rs->sr_ref );
535 if ( matched_dn.bv_val ) {
536 ber_memfree( matched_dn.bv_val );
537 rs->sr_matched = NULL;
542 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
543 /* entry is a referral, don't allow add */
544 struct berval matched_dn;
547 ber_dupbv( &matched_dn, &e->e_name );
548 erefs = get_entry_referrals( op, e );
550 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
554 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
555 &sop->o_req_dn, sop->oq_search.rs_scope );
556 ber_bvarray_free( erefs );
560 LDAP_LOG ( OPERATION, RESULTS,
561 "bdb_search: entry is referral\n", 0, 0, 0 );
563 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
567 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
568 rs->sr_err = LDAP_REFERRAL;
569 rs->sr_matched = matched_dn.bv_val;
570 send_ldap_result( sop, rs );
572 LOCK_ID_FREE (bdb->bi_dbenv, locker );
573 ber_bvarray_free( rs->sr_ref );
575 ber_memfree( matched_dn.bv_val );
576 rs->sr_matched = NULL;
580 /* if not root, get appropriate limits */
581 if ( be_isroot( op->o_bd, &sop->o_ndn ) ) {
584 ( void ) get_limits( op->o_bd, &sop->o_ndn, &limit );
587 /* The time/size limits come first because they require very little
588 * effort, so there's no chance the candidates are selected and then
589 * the request is not honored only because of time/size constraints */
591 /* if no time limit requested, use soft limit (unless root!) */
593 if ( sop->oq_search.rs_tlimit == 0 ) {
594 sop->oq_search.rs_tlimit = -1; /* allow root to set no limit */
597 if ( sop->oq_search.rs_slimit == 0 ) {
598 sop->oq_search.rs_slimit = -1;
602 /* if no limit is required, use soft limit */
603 if ( sop->oq_search.rs_tlimit <= 0 ) {
604 sop->oq_search.rs_tlimit = limit->lms_t_soft;
606 /* if requested limit higher than hard limit, abort */
607 } else if ( sop->oq_search.rs_tlimit > limit->lms_t_hard ) {
608 /* no hard limit means use soft instead */
609 if ( limit->lms_t_hard == 0
610 && limit->lms_t_soft > -1
611 && sop->oq_search.rs_tlimit > limit->lms_t_soft ) {
612 sop->oq_search.rs_tlimit = limit->lms_t_soft;
614 /* positive hard limit means abort */
615 } else if ( limit->lms_t_hard > 0 ) {
616 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
617 send_ldap_result( sop, rs );
622 /* negative hard limit means no limit */
625 /* if no limit is required, use soft limit */
626 if ( sop->oq_search.rs_slimit <= 0 ) {
627 if ( get_pagedresults(sop) && limit->lms_s_pr != 0 ) {
628 sop->oq_search.rs_slimit = limit->lms_s_pr;
630 sop->oq_search.rs_slimit = limit->lms_s_soft;
633 /* if requested limit higher than hard limit, abort */
634 } else if ( sop->oq_search.rs_slimit > limit->lms_s_hard ) {
635 /* no hard limit means use soft instead */
636 if ( limit->lms_s_hard == 0
637 && limit->lms_s_soft > -1
638 && sop->oq_search.rs_slimit > limit->lms_s_soft ) {
639 sop->oq_search.rs_slimit = limit->lms_s_soft;
641 /* positive hard limit means abort */
642 } else if ( limit->lms_s_hard > 0 ) {
643 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
644 send_ldap_result( sop, rs );
649 /* negative hard limit means no limit */
653 /* compute it anyway; root does not use it */
654 stoptime = op->o_time + sop->oq_search.rs_tlimit;
656 /* select candidates */
657 if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
658 rs->sr_err = base_candidate( op->o_bd, e, candidates );
661 BDB_IDL_ALL( bdb, candidates );
662 rs->sr_err = search_candidates( op, sop, rs, e, locker, candidates );
665 /* start cursor at beginning of candidates.
668 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
670 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
671 cursor = bdb_idl_search( candidates, ps_e->e_id );
672 if ( candidates[cursor] != ps_e->e_id ) {
673 rs->sr_err = LDAP_SUCCESS;
677 if ( ps_e->e_id < BDB_IDL_RANGE_FIRST(candidates)
678 || ps_e->e_id > BDB_IDL_RANGE_LAST(candidates)){
679 rs->sr_err = LDAP_SUCCESS;
684 candidates[1] = ps_e->e_id;
688 /* need normalized dn below */
689 ber_dupbv( &realbase, &e->e_nname );
691 if ( e != &slap_entry_root ) {
692 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
696 if ( candidates[0] == 0 ) {
698 LDAP_LOG ( OPERATION, RESULTS,
699 "bdb_search: no candidates\n", 0, 0, 0 );
701 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
705 rs->sr_err = LDAP_SUCCESS;
706 send_ldap_result( sop, rs );
711 /* if not root and candidates exceed to-be-checked entries, abort */
712 if ( !isroot && limit->lms_s_unchecked != -1 ) {
713 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
714 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
715 send_ldap_result( sop, rs );
721 if ( isroot || !limit->lms_s_pr_hide ) {
722 tentries = BDB_IDL_N(candidates);
725 #ifdef LDAP_CONTROL_PAGEDRESULTS
726 if ( get_pagedresults(sop) ) {
727 if ( sop->o_pagedresults_state.ps_cookie == 0 ) {
730 if ( sop->o_pagedresults_size == 0 ) {
731 rs->sr_err = LDAP_SUCCESS;
732 rs->sr_text = "search abandoned by pagedResult size=0";
733 send_ldap_result( sop, rs );
736 for ( id = bdb_idl_first( candidates, &cursor );
737 id != NOID && id <= (ID)( sop->o_pagedresults_state.ps_cookie );
738 id = bdb_idl_next( candidates, &cursor ) );
740 if ( cursor == NOID ) {
742 LDAP_LOG ( OPERATION, RESULTS,
743 "bdb_search: no paged results candidates\n",
746 Debug( LDAP_DEBUG_TRACE,
747 "bdb_search: no paged results candidates\n",
750 send_pagerequest_response( sop, rs, lastid, 0 );
759 #ifdef LDAP_CLIENT_UPDATE
760 if ( (sop->o_clientupdate_type & SLAP_LCUP_SYNC) ||
761 (IS_PSEARCH && sop->o_ps_protocol == LDAP_CLIENT_UPDATE ))
763 cookief.f_choice = LDAP_FILTER_AND;
764 cookief.f_and = &csnfnot;
765 cookief.f_next = NULL;
767 csnfnot.f_choice = LDAP_FILTER_NOT;
768 csnfnot.f_not = &csnfeq;
769 csnfnot.f_next = &csnfand;
771 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
772 csnfeq.f_ava = &aa_eq;
773 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
774 csnfeq.f_av_value = sop->o_clientupdate_state;
776 csnfand.f_choice = LDAP_FILTER_AND;
777 csnfand.f_and = &csnfge;
778 csnfand.f_next = NULL;
780 csnfge.f_choice = LDAP_FILTER_GE;
781 csnfge.f_ava = &aa_ge;
782 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
783 csnfge.f_av_value = sop->o_clientupdate_state;
784 csnfge.f_next = sop->oq_search.rs_filter;
787 #if defined(LDAP_CLIENT_UPDATE) && defined(LDAP_SYNC)
791 if ( (sop->o_sync_mode & SLAP_SYNC_REFRESH) ||
792 ( IS_PSEARCH && sop->o_ps_protocol == LDAP_SYNC ))
794 cookief.f_choice = LDAP_FILTER_AND;
795 cookief.f_and = &csnfnot;
796 cookief.f_next = NULL;
798 csnfnot.f_choice = LDAP_FILTER_NOT;
799 csnfnot.f_not = &csnfeq;
800 csnfnot.f_next = &csnfand;
802 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
803 csnfeq.f_ava = &aa_eq;
804 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
805 csnfeq.f_av_value = sop->o_sync_state;
807 csnfand.f_choice = LDAP_FILTER_AND;
808 csnfand.f_and = &csnfge;
809 csnfand.f_next = NULL;
811 csnfge.f_choice = LDAP_FILTER_GE;
812 csnfge.f_ava = &aa_ge;
813 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
814 csnfge.f_av_value = sop->o_sync_state;
815 csnfge.f_next = sop->oq_search.rs_filter;
819 for ( id = bdb_idl_first( candidates, &cursor );
821 id = bdb_idl_next( candidates, &cursor ) )
826 /* check for abandon */
827 if ( sop->o_abandon ) {
832 #ifdef LDAP_EXOP_X_CANCEL
833 if ( sop->o_cancel ) {
834 assert( sop->o_cancel == SLAP_CANCEL_REQ );
835 rs->sr_err = LDAP_CANCELLED;
836 send_ldap_result( sop, rs );
837 sop->o_cancel = SLAP_CANCEL_ACK;
843 /* check time limit */
844 if ( sop->oq_search.rs_tlimit != -1 && slap_get_time() > stoptime ) {
845 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
846 rs->sr_ref = rs->sr_v2ref;
847 send_ldap_result( sop, rs );
852 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
856 /* get the entry with reader lock */
857 rs->sr_err = bdb_id2entry_r( op->o_bd, NULL, id,
860 if (rs->sr_err == LDAP_BUSY) {
861 rs->sr_text = "ldap server busy";
862 send_ldap_result( sop, rs );
865 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
866 || rs->sr_err == DB_LOCK_NOTGRANTED )
872 if( !BDB_IDL_IS_RANGE(candidates) ) {
873 /* only complain for non-range IDLs */
875 LDAP_LOG ( OPERATION, RESULTS,
876 "bdb_search: candidate %ld not found\n",
879 Debug( LDAP_DEBUG_TRACE,
880 "bdb_search: candidate %ld not found\n",
887 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
894 #ifdef BDB_SUBENTRIES
895 if ( is_entry_subentry( e ) ) {
896 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
897 if(!get_subentries_visibility( sop )) {
898 /* only subentries are visible */
902 } else if ( get_subentries( sop ) &&
903 !get_subentries_visibility( sop ))
905 /* only subentries are visible */
909 } else if ( get_subentries_visibility( sop )) {
910 /* only subentries are visible */
916 /* aliases were already dereferenced in candidate list */
917 if ( (sop->ors_deref & LDAP_DEREF_SEARCHING) && is_entry_alias(e)) {
918 /* but if the search base is an alias, and we didn't
919 * deref it when finding, return it.
921 if ( (sop->ors_deref & LDAP_DEREF_FINDING)
922 || !bvmatch(&e->e_nname, &op->o_req_ndn))
929 * if it's a referral, add it to the list of referrals. only do
930 * this for non-base searches, and don't check the filter
931 * explicitly here since it's only a candidate anyway.
933 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE &&
934 is_entry_referral( e ) )
939 if ( !scopeok && sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
940 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
941 dnParent( &e->e_nname, &dn );
942 scopeok = dn_match( &dn, &realbase );
944 scopeok = (realbase.bv_len == 0);
948 && sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE )
950 scopeok = dnIsSuffix( &e->e_nname, &realbase );
957 BerVarray erefs = get_entry_referrals( sop, e );
958 rs->sr_ref = referral_rewrite( erefs,
960 sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE
964 send_search_reference( sop, rs );
966 ber_bvarray_free( rs->sr_ref );
967 ber_bvarray_free( erefs );
972 LDAP_LOG(OPERATION, DETAIL2,
973 "bdb_search: candidate referral %ld scope not okay\n",
976 Debug( LDAP_DEBUG_TRACE,
977 "bdb_search: candidate referral %ld scope not okay\n",
985 /* if it matches the filter and scope, send it */
986 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
988 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
989 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
991 rs->sr_err = LDAP_COMPARE_TRUE;
994 #ifdef LDAP_CLIENT_UPDATE
995 if ( sop->o_clientupdate_type & SLAP_LCUP_SYNC ) {
996 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1000 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1001 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1002 rs->sr_err = test_filter( sop,
1003 rs->sr_entry, sop->oq_search.rs_filter );
1004 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1005 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1006 entry_sync_state = LDAP_SYNC_ADD;
1008 entry_sync_state = LDAP_SYNC_PRESENT;
1015 rs->sr_err = test_filter( sop,
1016 rs->sr_entry, sop->oq_search.rs_filter );
1018 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1022 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1026 if ( !scopeok && sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
1027 if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
1028 scopeok = (realbase.bv_len == 0);
1030 dnParent( &e->e_nname, &dn );
1031 scopeok = dn_match( &dn, &realbase );
1034 } else if ( !scopeok &&
1035 sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE )
1037 scopeok = dnIsSuffix( &e->e_nname, &realbase );
1044 /* check size limit */
1045 if ( --sop->oq_search.rs_slimit == -1 ) {
1046 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1049 bdb_cache_return_entry_r( bdb->bi_dbenv,
1050 &bdb->bi_cache, e, &lock );
1052 rs->sr_entry = NULL;
1053 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1054 rs->sr_ref = rs->sr_v2ref;
1055 send_ldap_result( sop, rs );
1059 #ifdef LDAP_CONTROL_PAGEDRESULTS
1060 if ( get_pagedresults(sop) ) {
1061 if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
1062 send_pagerequest_response( sop, rs,
1074 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
1079 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1082 int premodify_found = 0;
1083 int entry_sync_state;
1086 if ( ps_type == LDAP_PSEARCH_BY_ADD ||
1087 ps_type == LDAP_PSEARCH_BY_DELETE ||
1088 ps_type == LDAP_PSEARCH_BY_MODIFY ||
1089 ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1091 if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1092 struct psid_entry* psid_e;
1093 LDAP_LIST_FOREACH( psid_e,
1094 &op->o_pm_list, ps_link)
1096 if( psid_e->ps_op == sop ) {
1098 premodify_found = 1;
1100 LDAP_LIST_REMOVE(psid_e, ps_link);
1104 if (psid_e != NULL) free (psid_e);
1107 if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1108 entry_sync_state = LDAP_SYNC_ADD;
1109 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1110 entry_sync_state = LDAP_SYNC_DELETE;
1111 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1112 if ( premodify_found ) {
1113 entry_sync_state = LDAP_SYNC_MODIFY;
1115 entry_sync_state = LDAP_SYNC_ADD;
1117 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1118 entry_sync_state = LDAP_SYNC_DELETE;
1125 #ifdef LDAP_CLIENT_UPDATE
1126 if ( sop->o_ps_protocol == LDAP_CLIENT_UPDATE ) {
1127 int entry_count = ++sop->o_ps_entries;
1128 if ( IS_BDB_REPLACE(ps_type) ) {
1129 rs->sr_err = bdb_build_lcup_update_ctrl( sop,
1130 rs, e, entry_count, ctrls,
1131 num_ctrls++, &latest_entrycsn_bv,
1132 SLAP_LCUP_ENTRY_DELETED_TRUE );
1134 rs->sr_err = bdb_build_lcup_update_ctrl( sop,
1135 rs, e, entry_count, ctrls,
1136 num_ctrls++, &latest_entrycsn_bv,
1137 SLAP_LCUP_ENTRY_DELETED_FALSE );
1139 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1140 rs->sr_attrs = attrs;
1141 rs->sr_ctrls = ctrls;
1142 result = send_search_entry( sop, rs );
1143 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1144 ch_free( ctrls[--num_ctrls] );
1145 ctrls[num_ctrls] = NULL;
1146 rs->sr_ctrls = NULL;
1150 if ( sop->o_ps_protocol == LDAP_SYNC ) {
1151 rs->sr_err = bdb_build_sync_state_ctrl( sop,
1152 rs, e, entry_sync_state, ctrls,
1153 num_ctrls++, 1, &latest_entrycsn_bv );
1154 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1155 rs->sr_attrs = attrs;
1156 rs->sr_ctrls = ctrls;
1157 result = send_search_entry( sop, rs );
1158 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1159 ch_free( ctrls[--num_ctrls] );
1160 ctrls[num_ctrls] = NULL;
1161 rs->sr_ctrls = NULL;
1169 } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1170 struct psid_entry* psid_e;
1171 psid_e = (struct psid_entry *) calloc (1,
1172 sizeof(struct psid_entry));
1173 psid_e->ps_op = sop;
1174 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1178 printf("Error !\n");
1181 #ifdef LDAP_CLIENT_UPDATE
1182 if ( sop->o_clientupdate_type & SLAP_LCUP_SYNC ) {
1183 rs->sr_err = bdb_build_lcup_update_ctrl( sop,
1184 rs, e, ++entry_count, ctrls,
1185 num_ctrls++, &latest_entrycsn_bv,
1186 SLAP_LCUP_ENTRY_DELETED_FALSE );
1187 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1188 rs->sr_ctrls = ctrls;
1189 rs->sr_attrs = sop->oq_search.rs_attrs;
1190 result = send_search_entry( sop, rs );
1191 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1192 ch_free( ctrls[--num_ctrls] );
1193 ctrls[num_ctrls] = NULL;
1194 rs->sr_ctrls = NULL;
1198 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1199 rs->sr_err = bdb_build_sync_state_ctrl( sop,
1200 rs, e, entry_sync_state, ctrls,
1201 num_ctrls++, 0, &latest_entrycsn_bv );
1202 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1204 rs->sr_ctrls = ctrls;
1205 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1206 rs->sr_attrs = sop->oq_search.rs_attrs;
1207 } else { /* PRESENT */
1208 rs->sr_attrs = &null_attr;
1210 result = send_search_entry( sop, rs );
1211 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1212 ch_free( ctrls[--num_ctrls] );
1213 ctrls[num_ctrls] = NULL;
1214 rs->sr_ctrls = NULL;
1219 rs->sr_attrs = sop->oq_search.rs_attrs;
1220 rs->sr_ctrls = NULL;
1221 result = send_search_entry( sop, rs );
1226 case 0: /* entry sent ok */
1228 case 1: /* entry not sent */
1230 case -1: /* connection closed */
1232 bdb_cache_return_entry_r(bdb->bi_dbenv,
1233 &bdb->bi_cache, e, &lock);
1235 rs->sr_entry = NULL;
1236 rs->sr_err = LDAP_OTHER;
1242 LDAP_LOG ( OPERATION, RESULTS,
1243 "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
1245 Debug( LDAP_DEBUG_TRACE,
1246 "bdb_search: %ld scope not okay\n",
1252 LDAP_LOG ( OPERATION, RESULTS,
1253 "bdb_search: %ld does not match filter\n", (long) id, 0, 0);
1255 Debug( LDAP_DEBUG_TRACE,
1256 "bdb_search: %ld does not match filter\n",
1263 /* free reader lock */
1265 bdb_cache_return_entry_r( bdb->bi_dbenv,
1266 &bdb->bi_cache, e , &lock);
1269 rs->sr_entry = NULL;
1272 ldap_pvt_thread_yield();
1276 #ifdef LDAP_CLIENT_UPDATE
1277 if ( sop->o_clientupdate_type & SLAP_LCUP_SYNC ) {
1278 bdb_build_lcup_done_ctrl( sop, rs, ctrls,
1279 num_ctrls++, &latest_entrycsn_bv );
1281 rs->sr_ctrls = ctrls;
1282 rs->sr_ref = rs->sr_v2ref;
1283 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1284 send_ldap_result( sop, rs );
1286 ch_free( latest_entrycsn_bv.bv_val );
1287 latest_entrycsn_bv.bv_val = NULL;
1289 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1290 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1292 ch_free( ctrls[--num_ctrls] );
1293 ctrls[num_ctrls] = NULL;
1297 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1298 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1299 /* refreshAndPersist mode */
1300 rs->sr_err = LDAP_SUCCESS;
1301 rs->sr_rspoid = LDAP_SYNC_INFO;
1302 rs->sr_ctrls = NULL;
1303 bdb_send_ldap_intermediate( sop, rs,
1304 LDAP_SYNC_REFRESH_DONE, &latest_entrycsn_bv );
1306 /* refreshOnly mode */
1307 bdb_build_sync_done_ctrl( sop, rs, ctrls,
1308 num_ctrls++, 1, &latest_entrycsn_bv );
1309 rs->sr_ctrls = ctrls;
1310 rs->sr_ref = rs->sr_v2ref;
1311 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1312 send_ldap_result( sop, rs );
1313 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1314 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1316 ch_free( ctrls[--num_ctrls] );
1317 ctrls[num_ctrls] = NULL;
1320 ch_free( latest_entrycsn_bv.bv_val );
1321 latest_entrycsn_bv.bv_val = NULL;
1325 rs->sr_ctrls = NULL;
1326 rs->sr_ref = rs->sr_v2ref;
1327 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1328 send_ldap_result( sop, rs );
1332 rs->sr_err = LDAP_SUCCESS;
1335 if( !IS_PSEARCH && e != NULL ) {
1336 /* free reader lock */
1337 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1340 LOCK_ID_FREE (bdb->bi_dbenv, locker );
1342 if( rs->sr_v2ref ) {
1343 ber_bvarray_free( rs->sr_v2ref );
1344 rs->sr_v2ref = NULL;
1346 if( realbase.bv_val ) ch_free( realbase.bv_val );
1352 static int base_candidate(
1358 LDAP_LOG ( OPERATION, ENTRY,
1359 "base_candidate: base: \"%s\" (0x%08lx)\n",
1360 e->e_dn, (long) e->e_id, 0);
1362 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1363 e->e_dn, (long) e->e_id, 0);
1371 /* Look for "objectClass Present" in this filter.
1372 * Also count depth of filter tree while we're at it.
1374 static int oc_filter(
1382 if( cur > *max ) *max = cur;
1384 switch(f->f_choice) {
1385 case LDAP_FILTER_PRESENT:
1386 if (f->f_desc == slap_schema.si_ad_objectClass) {
1391 case LDAP_FILTER_AND:
1392 case LDAP_FILTER_OR:
1394 for (f=f->f_and; f; f=f->f_next) {
1395 (void) oc_filter(f, cur, max);
1405 static void search_stack_free( void *key, void *data)
1410 static void *search_stack(
1414 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1417 if ( op->o_threadctx ) {
1418 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1421 ret = bdb->bi_search_stack;
1425 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1427 if ( op->o_threadctx ) {
1428 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1429 ret, search_stack_free );
1431 bdb->bi_search_stack = ret;
1437 static int search_candidates(
1445 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1447 Filter f, scopef, rf, xf, nf;
1449 AttributeAssertion aa_ref;
1450 #ifdef BDB_SUBENTRIES
1452 AttributeAssertion aa_subentry;
1456 * This routine takes as input a filter (user-filter)
1457 * and rewrites it as follows:
1458 * (&(scope=DN)[(objectClass=subentry)]
1459 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1463 LDAP_LOG ( OPERATION, ENTRY,
1464 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1465 e->e_dn, (long) e->e_id, op->oq_search.rs_scope);
1467 Debug(LDAP_DEBUG_TRACE,
1468 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1469 e->e_dn, (long) e->e_id, op->oq_search.rs_scope );
1472 xf.f_or = op->oq_search.rs_filter;
1473 xf.f_choice = LDAP_FILTER_OR;
1476 /* If the user's filter uses objectClass=*,
1477 * these clauses are redundant.
1479 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1480 && !get_subentries_visibility(op) )
1482 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1483 /* match referral objects */
1484 struct berval bv_ref = { sizeof("referral")-1, "referral" };
1485 rf.f_choice = LDAP_FILTER_EQUALITY;
1487 rf.f_av_desc = slap_schema.si_ad_objectClass;
1488 rf.f_av_value = bv_ref;
1489 rf.f_next = xf.f_or;
1495 scopef.f_choice = op->oq_search.rs_scope == LDAP_SCOPE_SUBTREE
1496 ? SLAPD_FILTER_DN_SUBTREE
1497 : SLAPD_FILTER_DN_ONE;
1498 scopef.f_dn = &e->e_nname;
1499 scopef.f_next = NULL;
1502 f.f_choice = LDAP_FILTER_AND;
1504 /* Dummy; we compute scope separately now */
1505 nf.f_choice = LDAP_FILTER_NOT;
1506 nf.f_next = xf.f_or == op->oq_search.rs_filter
1507 ? op->oq_search.rs_filter : &xf ;
1508 /* Filter depth increased again, adding dummy clause */
1511 #ifdef BDB_SUBENTRIES
1512 if( get_subentries_visibility( op ) ) {
1513 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
1514 sf.f_choice = LDAP_FILTER_EQUALITY;
1515 sf.f_ava = &aa_subentry;
1516 sf.f_av_desc = slap_schema.si_ad_objectClass;
1517 sf.f_av_value = bv_subentry;
1518 sf.f_next = scopef.f_next;
1523 /* Allocate IDL stack, plus 1 more for former tmp */
1524 if ( depth+1 > bdb->bi_search_stack_depth ) {
1525 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1527 stack = search_stack( stackop );
1530 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1531 rc = search_aliases( op, rs, e, locker, &scopef, ids, stack );
1533 rc = bdb_filter_candidates( op->o_bd, &scopef, ids,
1534 stack, stack+BDB_IDL_UM_SIZE );
1537 if ( rc == LDAP_SUCCESS ) {
1538 rc = bdb_filter_candidates( op->o_bd, &f, ids,
1539 stack, stack+BDB_IDL_UM_SIZE );
1542 if ( depth+1 > bdb->bi_search_stack_depth ) {
1548 LDAP_LOG ( OPERATION, DETAIL1,
1549 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
1551 Debug(LDAP_DEBUG_TRACE,
1552 "bdb_search_candidates: failed (rc=%d)\n",
1558 LDAP_LOG ( OPERATION, DETAIL1,
1559 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1560 (long) ids[0], (long) BDB_IDL_FIRST(ids),
1561 (long) BDB_IDL_LAST(ids));
1563 Debug(LDAP_DEBUG_TRACE,
1564 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1566 (long) BDB_IDL_FIRST(ids),
1567 (long) BDB_IDL_LAST(ids) );
1574 #ifdef LDAP_CONTROL_PAGEDRESULTS
1576 send_pagerequest_response(
1582 LDAPControl ctrl, *ctrls[2];
1583 char berbuf[LBER_ELEMENT_SIZEOF];
1584 BerElement *ber = (BerElement *)berbuf;
1585 struct berval cookie = { 0, NULL };
1586 PagedResultsCookie respcookie;
1589 LDAP_LOG ( OPERATION, ENTRY,
1590 "send_pagerequest_response: lastid: (0x%08lx) "
1591 "nentries: (0x%081x)\n",
1592 lastid, rs->sr_nentries, NULL );
1594 Debug(LDAP_DEBUG_ARGS, "send_pagerequest_response: lastid: (0x%08lx) "
1595 "nentries: (0x%081x)\n", lastid, rs->sr_nentries, NULL );
1598 ctrl.ldctl_value.bv_val = NULL;
1602 ber_init2( ber, NULL, LBER_USE_DER );
1604 respcookie = ( PagedResultsCookie )lastid;
1605 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1606 cookie.bv_len = sizeof( respcookie );
1607 cookie.bv_val = (char *)&respcookie;
1610 * FIXME: we should consider sending an estimate of the entries
1611 * left, after appropriate security check is done
1613 ber_printf( ber, "{iO}", tentries, &cookie );
1615 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1619 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1620 ctrls[0]->ldctl_iscritical = 0;
1622 rs->sr_ctrls = ctrls;
1623 rs->sr_err = LDAP_SUCCESS;
1624 send_ldap_result( op, rs );
1627 (void) ber_free_buf( ber );
1631 #ifdef LDAP_CLIENT_UPDATE
1633 bdb_build_lcup_update_ctrl(
1638 LDAPControl **ctrls,
1640 struct berval *latest_entrycsn_bv,
1646 const char *text = NULL;
1648 char berbuf[LBER_ELEMENT_SIZEOF];
1649 BerElement *ber = (BerElement *)berbuf;
1651 struct berval entrycsn_bv = { 0, NULL };
1653 ber_init2( ber, 0, LBER_USE_DER );
1655 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1657 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
1658 AttributeDescription *desc = a->a_desc;
1659 if ( desc == slap_schema.si_ad_entryCSN ) {
1660 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
1661 if ( latest_entrycsn_bv->bv_val == NULL ) {
1662 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1664 res = value_match( &ret, desc,
1665 desc->ad_type->sat_ordering, 0,
1666 &entrycsn_bv, latest_entrycsn_bv, &text );
1667 if ( res != LDAP_SUCCESS ) {
1670 LDAP_LOG ( OPERATION, RESULTS,
1671 "bdb_search: value_match failed\n",
1674 Debug( LDAP_DEBUG_TRACE,
1675 "bdb_search: value_match failed\n",
1681 ch_free( latest_entrycsn_bv->bv_val );
1682 latest_entrycsn_bv->bv_val = NULL;
1683 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1689 if ( entry_count % op->o_clientupdate_interval == 0 ) {
1692 SLAP_LCUP_STATE_UPDATE_FALSE,
1694 LDAP_CUP_COOKIE_OID, &entrycsn_bv );
1695 } else { /* Do not send cookie */
1698 SLAP_LCUP_STATE_UPDATE_FALSE,
1702 ch_free( entrycsn_bv.bv_val );
1703 entrycsn_bv.bv_val = NULL;
1705 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_ENTRY_UPDATE;
1706 ctrls[num_ctrls]->ldctl_iscritical = op->o_clientupdate;
1707 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1709 ber_free_buf( ber );
1713 LDAP_LOG ( OPERATION, RESULTS,
1714 "bdb_build_lcup_ctrl: ber_flatten2 failed\n",
1717 Debug( LDAP_DEBUG_TRACE,
1718 "bdb_build_lcup_ctrl: ber_flatten2 failed\n",
1721 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1725 return LDAP_SUCCESS;
1729 bdb_build_lcup_done_ctrl(
1732 LDAPControl **ctrls,
1734 struct berval *latest_entrycsn_bv )
1737 char berbuf[LBER_ELEMENT_SIZEOF];
1738 BerElement *ber = (BerElement *)berbuf;
1740 ber_init2( ber, NULL, LBER_USE_DER );
1742 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1744 ber_printf( ber, "{sON}", LDAP_CUP_COOKIE_OID, latest_entrycsn_bv );
1746 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_CLIENT_UPDATE_DONE;
1747 ctrls[num_ctrls]->ldctl_iscritical = op->o_clientupdate;
1748 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1750 ber_free_buf( ber );
1754 LDAP_LOG ( OPERATION, RESULTS,
1755 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n", 0, 0, 0 );
1757 Debug( LDAP_DEBUG_TRACE,
1758 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1761 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1765 return LDAP_SUCCESS;
1771 bdb_build_sync_state_ctrl(
1775 int entry_sync_state,
1776 LDAPControl **ctrls,
1779 struct berval *latest_entrycsn_bv )
1784 const char *text = NULL;
1786 char berbuf[LBER_ELEMENT_SIZEOF];
1787 BerElement *ber = (BerElement *)berbuf;
1789 struct berval entryuuid_bv = { 0, NULL };
1790 struct berval entrycsn_bv = { 0, NULL };
1792 ber_init2( ber, 0, LBER_USE_DER );
1794 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1796 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
1797 AttributeDescription *desc = a->a_desc;
1798 if ( desc == slap_schema.si_ad_entryCSN ) {
1799 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
1800 if ( latest_entrycsn_bv->bv_val == NULL ) {
1801 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1803 res = value_match( &ret, desc,
1804 desc->ad_type->sat_ordering, 0,
1805 &entrycsn_bv, latest_entrycsn_bv, &text );
1806 if ( res != LDAP_SUCCESS ) {
1809 LDAP_LOG ( OPERATION, RESULTS,
1810 "bdb_search: value_match failed\n",
1813 Debug( LDAP_DEBUG_TRACE,
1814 "bdb_search: value_match failed\n",
1819 ch_free( latest_entrycsn_bv->bv_val );
1820 latest_entrycsn_bv->bv_val = NULL;
1821 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1824 } else if ( desc == slap_schema.si_ad_entryUUID ) {
1825 ber_dupbv( &entryuuid_bv, &a->a_vals[0] );
1829 if ( send_cookie ) {
1830 ber_printf( ber, "{eOON}",
1831 entry_sync_state, &entryuuid_bv, &entrycsn_bv );
1833 ber_printf( ber, "{eON}",
1834 entry_sync_state, &entryuuid_bv );
1837 ch_free( entrycsn_bv.bv_val );
1838 entrycsn_bv.bv_val = NULL;
1839 ch_free( entryuuid_bv.bv_val );
1840 entryuuid_bv.bv_val = NULL;
1842 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
1843 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
1844 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1846 ber_free_buf( ber );
1850 LDAP_LOG ( OPERATION, RESULTS,
1851 "bdb_build_sync_ctrl: ber_flatten2 failed\n",
1854 Debug( LDAP_DEBUG_TRACE,
1855 "bdb_build_sync_ctrl: ber_flatten2 failed\n",
1858 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1862 return LDAP_SUCCESS;
1866 bdb_build_sync_done_ctrl(
1869 LDAPControl **ctrls,
1872 struct berval *latest_entrycsn_bv )
1875 char berbuf[LBER_ELEMENT_SIZEOF];
1876 BerElement *ber = (BerElement *)berbuf;
1878 ber_init2( ber, NULL, LBER_USE_DER );
1880 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1882 if ( send_cookie ) {
1883 ber_printf( ber, "{ON}", latest_entrycsn_bv );
1885 ber_printf( ber, "{N}" );
1888 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
1889 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
1890 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1892 ber_free_buf( ber );
1896 LDAP_LOG ( OPERATION, RESULTS,
1897 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1900 Debug( LDAP_DEBUG_TRACE,
1901 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1904 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1908 return LDAP_SUCCESS;
1912 bdb_send_ldap_intermediate(
1916 struct berval *cookie )
1918 char berbuf[LBER_ELEMENT_SIZEOF];
1919 BerElement *ber = (BerElement *)berbuf;
1920 struct berval rspdata;
1924 ber_init2( ber, NULL, LBER_USE_DER );
1926 if ( cookie == NULL ) {
1927 ber_printf( ber, "{eN}", state );
1929 ber_printf( ber, "{eON}", state, cookie );
1932 ret = ber_flatten2( ber, &rspdata, 0 );
1936 LDAP_LOG ( OPERATION, RESULTS,
1937 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1940 Debug( LDAP_DEBUG_TRACE,
1941 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1944 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1948 rs->sr_rspdata = &rspdata;
1949 send_ldap_intermediate_resp( op, rs );
1950 rs->sr_rspdata = NULL;
1951 ber_free_buf( ber );
1953 return LDAP_SUCCESS;