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 */
29 static void send_pagerequest_response(
35 /* Dereference aliases for a single alias entry. Return the final
36 * dereferenced entry on success, NULL on any failure.
38 static Entry * deref_base (
48 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
53 rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
54 rs->sr_text = "maximum deref depth exceeded";
56 while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
58 /* Remember the last entry we looked at, so we can
63 /* If this is part of a subtree or onelevel search,
64 * have we seen this ID before? If so, quit.
66 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
71 /* If we've seen this ID during this deref iteration,
74 if ( bdb_idl_insert( tmp, e->e_id ) ) {
75 rs->sr_err = LDAP_ALIAS_PROBLEM;
76 rs->sr_text = "circular alias";
81 /* If there was a problem getting the aliasedObjectName,
82 * get_alias_dn will have set the error status.
84 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
89 rs->sr_err = bdb_dn2entry( op->o_bd, NULL, &ndn, &ei,
90 0, locker, &lockr, op->o_tmpmemctx );
92 if ( ei ) e = ei->bei_e;
96 rs->sr_err = LDAP_ALIAS_PROBLEM;
97 rs->sr_text = "aliasedObject not found";
101 /* Free the previous entry, continue to work with the
102 * one we just retrieved.
104 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
108 /* We found a regular entry. Return this to the caller. The
109 * entry is still locked for Read.
111 if (!is_entry_alias(e)) {
112 rs->sr_err = LDAP_SUCCESS;
120 /* Look for and dereference all aliases within the search scope. Adds
121 * the dereferenced entries to the "ids" list. Requires "stack" to be
122 * able to hold 8 levels of DB_SIZE IDLs.
124 static int search_aliases(
135 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
136 ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
137 ID cursora, ida, cursoro, ido, *subscop2;
140 struct berval bv_alias = { sizeof("alias")-1, "alias" };
141 AttributeAssertion aa_alias;
143 DB_LOCK locka, lockr;
147 aliases = stack; /* IDL of all aliases in the database */
148 curscop = aliases + BDB_IDL_DB_SIZE; /* Aliases in the current scope */
149 subscop = curscop + BDB_IDL_DB_SIZE; /* The current scope */
150 visited = subscop + BDB_IDL_DB_SIZE; /* IDs we've seen in this search */
151 newsubs = visited + BDB_IDL_DB_SIZE; /* New subtrees we've added */
152 oldsubs = newsubs + BDB_IDL_DB_SIZE; /* Subtrees added previously */
153 tmp = oldsubs + BDB_IDL_DB_SIZE; /* Scratch space for deref_base() */
155 /* A copy of subscop, because subscop gets clobbered by
156 * the bdb_idl_union/intersection routines
158 subscop2 = tmp + BDB_IDL_DB_SIZE;
160 af.f_choice = LDAP_FILTER_EQUALITY;
161 af.f_ava = &aa_alias;
162 af.f_av_desc = slap_schema.si_ad_objectClass;
163 af.f_av_value = bv_alias;
166 /* Find all aliases in database */
167 BDB_IDL_ZERO( aliases );
168 rs->sr_err = bdb_filter_candidates( op, &af, aliases,
170 if (rs->sr_err != LDAP_SUCCESS) {
174 oldsubs[1] = e->e_id;
177 BDB_IDL_ZERO( visited );
178 BDB_IDL_ZERO( newsubs );
181 ido = bdb_idl_first( oldsubs, &cursoro );
184 /* Set curscop to only the aliases in the current scope. Start with
185 * all the aliases, obtain the IDL for the current scope, and then
186 * get the intersection of these two IDLs. Add the current scope
187 * to the cumulative list of candidates.
189 BDB_IDL_CPY( curscop, aliases );
190 rs->sr_err = bdb_filter_candidates( op, sf, subscop, NULL, NULL );
194 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
196 BDB_IDL_CPY(subscop2, subscop);
197 rs->sr_err = bdb_idl_intersection(curscop, subscop);
198 bdb_idl_union( ids, subscop2 );
200 /* Dereference all of the aliases in the current scope. */
202 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
203 ida = bdb_idl_next(curscop, &cursora))
206 rs->sr_err = bdb_cache_find_entry_id(op->o_bd, NULL,
207 ida, &ei, 0, locker, &lockr, op->o_tmpmemctx );
208 if (rs->sr_err != LDAP_SUCCESS) {
213 /* This should only happen if the curscop IDL has maxed out and
214 * turned into a range that spans IDs indiscriminately
216 if (!is_entry_alias(a)) {
217 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
222 /* Actually dereference the alias */
224 a = deref_base( op, rs, a, &matched, locker, &lockr,
227 /* If the target was not already in our current candidates,
228 * make note of it in the newsubs list. Also
229 * set it in the scopes list so that bdb_search
232 if (bdb_idl_insert(ids, a->e_id) == 0) {
233 bdb_idl_insert(newsubs, a->e_id);
234 bdb_idl_insert(scopes, a->e_id);
236 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
239 } else if (matched) {
240 /* Alias could not be dereferenced, or it deref'd to
241 * an ID we've already seen. Ignore it.
243 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
248 /* If this is a OneLevel search, we're done; oldsubs only had one
249 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
251 if (op->ors_scope != LDAP_SCOPE_SUBTREE) break;
253 ido = bdb_idl_next( oldsubs, &cursoro );
255 /* If we're done processing the old scopes, did we add any new
256 * scopes in this iteration? If so, go back and do those now.
259 if (BDB_IDL_IS_ZERO(newsubs)) break;
260 BDB_IDL_CPY(oldsubs, newsubs);
261 BDB_IDL_ZERO(newsubs);
263 ido = bdb_idl_first( oldsubs, &cursoro );
266 /* Find the entry corresponding to the next scope. If it can't
267 * be found, ignore it and move on. This should never happen;
268 * we should never see the ID of an entry that doesn't exist.
269 * Set the name so that the scope's IDL can be retrieved.
273 rs->sr_err = bdb_cache_find_entry_id(op->o_bd, NULL, ido, &ei,
274 0, locker, &locka, op->o_tmpmemctx );
275 if (rs->sr_err != LDAP_SUCCESS) goto nextido;
277 sf->f_dn = &e->e_nname;
279 /* bdb_dn2idl uses IDs for keys, not DNs */
280 sf->f_dn = (struct berval *)&ido;
286 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
287 #define IS_BDB_REPLACE(type) (( type == LDAP_PSEARCH_BY_DELETE ) || \
288 ( type == LDAP_PSEARCH_BY_SCOPEOUT ))
289 #define IS_PSEARCH (op != sop)
292 bdb_abandon( Operation *op, SlapReply *rs )
295 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
297 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
298 if ( ps_list->o_connid == op->o_connid ) {
299 if ( ps_list->o_msgid == op->oq_abandon.rs_msgid ) {
300 ps_list->o_abandon = 1;
301 LDAP_LIST_REMOVE( ps_list, o_ps_link );
302 slap_op_free ( ps_list );
307 return LDAP_UNAVAILABLE;
311 bdb_cancel( Operation *op, SlapReply *rs )
314 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
316 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
317 if ( ps_list->o_connid == op->o_connid ) {
318 if ( ps_list->o_msgid == op->oq_cancel.rs_msgid ) {
319 ps_list->o_cancel = SLAP_CANCEL_DONE;
320 LDAP_LIST_REMOVE( ps_list, o_ps_link );
323 bdb_build_sync_done_ctrl( conn, ps_list, ps_list->ctrls,
324 1, &latest_entrycsn_bv );
325 send_ldap_result( conn, ps_list, LDAP_CANCELLED,
326 NULL, NULL, NULL, ps_list->ctrls, ps_list->nentries);
328 rs->sr_err = LDAP_CANCELLED;
329 send_ldap_result( ps_list, rs );
331 slap_op_free ( ps_list );
336 return LDAP_UNAVAILABLE;
339 int bdb_search( Operation *op, SlapReply *rs )
341 return bdb_do_search( op, rs, op, NULL, 0 );
344 /* For persistent searches, op is the currently executing operation,
345 * sop is the persistent search. For regular searches, sop = op.
348 bdb_do_search( Operation *op, SlapReply *rs, Operation *sop,
349 Entry *ps_e, int ps_type )
353 int bdb_search( Operation *op, SlapReply *rs )
356 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
359 ID candidates[BDB_IDL_UM_SIZE];
360 ID scopes[BDB_IDL_DB_SIZE];
361 Entry *e = NULL, base;
362 Entry *matched = NULL;
364 struct berval realbase = { 0, NULL };
368 AttributeName *attrs;
370 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
371 Filter cookief, csnfnot, csnfeq, csnfand, csnfge;
372 AttributeAssertion aa_ge, aa_eq;
375 struct berval entrycsn_bv = { 0, NULL };
377 struct berval latest_entrycsn_bv = { 0, NULL };
378 LDAPControl *ctrls[SLAP_SEARCH_MAX_CTRLS];
380 AttributeName uuid_attr[2];
383 int entry_sync_state = -1;
384 AttributeName null_attr;
387 struct slap_limits_set *limit = NULL;
390 u_int32_t locker = 0;
394 LDAP_LOG( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
396 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
399 attrs = sop->oq_search.rs_attrs;
401 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
402 #ifdef LDAP_CLIENT_UPDATE
403 if ( !IS_PSEARCH && sop->o_clientupdate_type & SLAP_LCUP_PERSIST ) {
404 sop->o_ps_protocol = LDAP_CLIENT_UPDATE;
405 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
410 /* psearch needs to be registered before refresh begins */
411 /* psearch and refresh transmission is serialized in send_ldap_ber() */
412 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
413 sop->o_ps_protocol = LDAP_SYNC;
414 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
416 null_attr.an_desc = NULL;
417 null_attr.an_oc = NULL;
418 null_attr.an_name.bv_len = 0;
419 null_attr.an_name.bv_val = NULL;
422 for ( num_ctrls = 0; num_ctrls < SLAP_SEARCH_MAX_CTRLS; num_ctrls++ ) {
423 ctrls[num_ctrls] = NULL;
427 if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
428 #ifdef LDAP_CLIENT_UPDATE
429 if ( sop->o_ps_protocol == LDAP_CLIENT_UPDATE ) {
431 attrs[0].an_desc = slap_schema.si_ad_entryUUID;
432 attrs[0].an_oc = NULL;
433 attrs[0].an_name = attrs[0].an_desc->ad_cname;
434 attrs[1].an_desc = NULL;
435 attrs[1].an_oc = NULL;
436 attrs[1].an_name.bv_len = 0;
437 attrs[1].an_name.bv_val = NULL;
441 if (sop->o_ps_protocol == LDAP_SYNC ) {
443 attrs[0].an_desc = NULL;
444 attrs[0].an_oc = NULL;
445 attrs[0].an_name.bv_len = 0;
446 attrs[0].an_name.bv_val = NULL;
456 manageDSAit = get_manageDSAit( sop );
458 rs->sr_err = LOCK_ID (bdb->bi_dbenv, &locker );
464 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
468 if ( sop->o_req_ndn.bv_len == 0 ) {
469 /* DIT root special case */
470 e = (Entry *) &slap_entry_root;
474 /* get entry with reader lock */
475 rs->sr_err = bdb_dn2entry( op->o_bd, NULL, &sop->o_req_ndn, &ei,
476 1, locker, &lock, op->o_tmpmemctx );
481 matched = ei->bei_e; break;
483 e = ei->bei_e; break;
485 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
486 LOCK_ID_FREE (bdb->bi_dbenv, locker );
488 case DB_LOCK_DEADLOCK:
489 case DB_LOCK_NOTGRANTED:
492 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
493 LOCK_ID_FREE (bdb->bi_dbenv, locker );
497 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
498 BDB_IDL_ZERO(candidates);
499 e = deref_base( op, rs, e, &matched, locker, &lock,
504 struct berval matched_dn = { 0, NULL };
506 if ( matched != NULL ) {
508 ber_dupbv( &matched_dn, &matched->e_name );
510 erefs = is_entry_referral( matched )
511 ? get_entry_referrals( op, matched )
514 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
519 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
520 &sop->o_req_dn, sop->oq_search.rs_scope );
521 ber_bvarray_free( erefs );
525 rs->sr_ref = referral_rewrite( default_referral,
526 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
529 rs->sr_err=LDAP_REFERRAL;
530 rs->sr_matched = matched_dn.bv_val;
531 send_ldap_result( sop, rs );
533 LOCK_ID_FREE (bdb->bi_dbenv, locker );
535 ber_bvarray_free( rs->sr_ref );
538 if ( matched_dn.bv_val ) {
539 ber_memfree( matched_dn.bv_val );
540 rs->sr_matched = NULL;
545 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
546 /* entry is a referral, don't allow add */
547 struct berval matched_dn;
550 ber_dupbv( &matched_dn, &e->e_name );
551 erefs = get_entry_referrals( op, e );
553 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
557 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
558 &sop->o_req_dn, sop->oq_search.rs_scope );
559 ber_bvarray_free( erefs );
563 LDAP_LOG ( OPERATION, RESULTS,
564 "bdb_search: entry is referral\n", 0, 0, 0 );
566 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
570 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
571 rs->sr_err = LDAP_REFERRAL;
572 rs->sr_matched = matched_dn.bv_val;
573 send_ldap_result( sop, rs );
575 LOCK_ID_FREE (bdb->bi_dbenv, locker );
576 ber_bvarray_free( rs->sr_ref );
578 ber_memfree( matched_dn.bv_val );
579 rs->sr_matched = NULL;
583 /* if not root, get appropriate limits */
584 if ( be_isroot( op->o_bd, &sop->o_ndn ) ) {
587 ( void ) get_limits( op->o_bd, &sop->o_ndn, &limit );
590 /* The time/size limits come first because they require very little
591 * effort, so there's no chance the candidates are selected and then
592 * the request is not honored only because of time/size constraints */
594 /* if no time limit requested, use soft limit (unless root!) */
596 if ( sop->oq_search.rs_tlimit == 0 ) {
597 sop->oq_search.rs_tlimit = -1; /* allow root to set no limit */
600 if ( sop->oq_search.rs_slimit == 0 ) {
601 sop->oq_search.rs_slimit = -1;
605 /* if no limit is required, use soft limit */
606 if ( sop->oq_search.rs_tlimit <= 0 ) {
607 sop->oq_search.rs_tlimit = limit->lms_t_soft;
609 /* if requested limit higher than hard limit, abort */
610 } else if ( sop->oq_search.rs_tlimit > limit->lms_t_hard ) {
611 /* no hard limit means use soft instead */
612 if ( limit->lms_t_hard == 0
613 && limit->lms_t_soft > -1
614 && sop->oq_search.rs_tlimit > limit->lms_t_soft ) {
615 sop->oq_search.rs_tlimit = limit->lms_t_soft;
617 /* positive hard limit means abort */
618 } else if ( limit->lms_t_hard > 0 ) {
619 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
620 send_ldap_result( sop, rs );
625 /* negative hard limit means no limit */
628 /* if no limit is required, use soft limit */
629 if ( sop->oq_search.rs_slimit <= 0 ) {
630 if ( get_pagedresults(sop) && limit->lms_s_pr != 0 ) {
631 sop->oq_search.rs_slimit = limit->lms_s_pr;
633 sop->oq_search.rs_slimit = limit->lms_s_soft;
636 /* if requested limit higher than hard limit, abort */
637 } else if ( sop->oq_search.rs_slimit > limit->lms_s_hard ) {
638 /* no hard limit means use soft instead */
639 if ( limit->lms_s_hard == 0
640 && limit->lms_s_soft > -1
641 && sop->oq_search.rs_slimit > limit->lms_s_soft ) {
642 sop->oq_search.rs_slimit = limit->lms_s_soft;
644 /* positive hard limit means abort */
645 } else if ( limit->lms_s_hard > 0 ) {
646 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
647 send_ldap_result( sop, rs );
652 /* negative hard limit means no limit */
656 /* compute it anyway; root does not use it */
657 stoptime = op->o_time + sop->oq_search.rs_tlimit;
659 /* need normalized dn below */
660 ber_dupbv( &realbase, &e->e_nname );
662 /* Copy info to base, must free entry before accessing the database
663 * in search_candidates, to avoid deadlocks.
665 base.e_nname = realbase;
668 if ( e != &slap_entry_root ) {
669 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
673 /* select candidates */
674 if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
675 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
678 BDB_IDL_ZERO( candidates );
679 BDB_IDL_ZERO( scopes );
680 rs->sr_err = search_candidates( op, sop, rs, &base, locker, candidates, scopes );
683 /* start cursor at beginning of candidates.
686 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
688 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
689 cursor = bdb_idl_search( candidates, ps_e->e_id );
690 if ( candidates[cursor] != ps_e->e_id ) {
691 rs->sr_err = LDAP_SUCCESS;
695 if ( ps_e->e_id < BDB_IDL_RANGE_FIRST(candidates)
696 || ps_e->e_id > BDB_IDL_RANGE_LAST(candidates)){
697 rs->sr_err = LDAP_SUCCESS;
702 candidates[1] = ps_e->e_id;
706 if ( candidates[0] == 0 ) {
708 LDAP_LOG ( OPERATION, RESULTS,
709 "bdb_search: no candidates\n", 0, 0, 0 );
711 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
715 rs->sr_err = LDAP_SUCCESS;
716 send_ldap_result( sop, rs );
721 /* if not root and candidates exceed to-be-checked entries, abort */
722 if ( !isroot && limit->lms_s_unchecked != -1 ) {
723 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
724 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
725 send_ldap_result( sop, rs );
731 if ( isroot || !limit->lms_s_pr_hide ) {
732 tentries = BDB_IDL_N(candidates);
735 #ifdef LDAP_CONTROL_PAGEDRESULTS
736 if ( get_pagedresults(sop) ) {
737 if ( sop->o_pagedresults_state.ps_cookie == 0 ) {
740 if ( sop->o_pagedresults_size == 0 ) {
741 rs->sr_err = LDAP_SUCCESS;
742 rs->sr_text = "search abandoned by pagedResult size=0";
743 send_ldap_result( sop, rs );
746 for ( id = bdb_idl_first( candidates, &cursor );
747 id != NOID && id <= (ID)( sop->o_pagedresults_state.ps_cookie );
748 id = bdb_idl_next( candidates, &cursor ) );
750 if ( cursor == NOID ) {
752 LDAP_LOG ( OPERATION, RESULTS,
753 "bdb_search: no paged results candidates\n",
756 Debug( LDAP_DEBUG_TRACE,
757 "bdb_search: no paged results candidates\n",
760 send_pagerequest_response( sop, rs, lastid, 0 );
769 #ifdef LDAP_CLIENT_UPDATE
770 if ( (sop->o_clientupdate_type & SLAP_LCUP_SYNC) ||
771 (IS_PSEARCH && sop->o_ps_protocol == LDAP_CLIENT_UPDATE ))
773 cookief.f_choice = LDAP_FILTER_AND;
774 cookief.f_and = &csnfnot;
775 cookief.f_next = NULL;
777 csnfnot.f_choice = LDAP_FILTER_NOT;
778 csnfnot.f_not = &csnfeq;
779 csnfnot.f_next = &csnfand;
781 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
782 csnfeq.f_ava = &aa_eq;
783 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
784 csnfeq.f_av_value = sop->o_clientupdate_state;
786 csnfand.f_choice = LDAP_FILTER_AND;
787 csnfand.f_and = &csnfge;
788 csnfand.f_next = NULL;
790 csnfge.f_choice = LDAP_FILTER_GE;
791 csnfge.f_ava = &aa_ge;
792 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
793 csnfge.f_av_value = sop->o_clientupdate_state;
794 csnfge.f_next = sop->oq_search.rs_filter;
797 #if defined(LDAP_CLIENT_UPDATE) && defined(LDAP_SYNC)
801 if ( (sop->o_sync_mode & SLAP_SYNC_REFRESH) ||
802 ( IS_PSEARCH && sop->o_ps_protocol == LDAP_SYNC ))
804 cookief.f_choice = LDAP_FILTER_AND;
805 cookief.f_and = &csnfnot;
806 cookief.f_next = NULL;
808 csnfnot.f_choice = LDAP_FILTER_NOT;
809 csnfnot.f_not = &csnfeq;
810 csnfnot.f_next = &csnfand;
812 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
813 csnfeq.f_ava = &aa_eq;
814 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
815 csnfeq.f_av_value = sop->o_sync_state;
817 csnfand.f_choice = LDAP_FILTER_AND;
818 csnfand.f_and = &csnfge;
819 csnfand.f_next = NULL;
821 csnfge.f_choice = LDAP_FILTER_GE;
822 csnfge.f_ava = &aa_ge;
823 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
824 csnfge.f_av_value = sop->o_sync_state;
825 csnfge.f_next = sop->oq_search.rs_filter;
829 for ( id = bdb_idl_first( candidates, &cursor );
831 id = bdb_idl_next( candidates, &cursor ) )
836 /* check for abandon */
837 if ( sop->o_abandon ) {
842 #ifdef LDAP_EXOP_X_CANCEL
843 if ( sop->o_cancel ) {
844 assert( sop->o_cancel == SLAP_CANCEL_REQ );
845 rs->sr_err = LDAP_CANCELLED;
846 send_ldap_result( sop, rs );
847 sop->o_cancel = SLAP_CANCEL_ACK;
853 /* check time limit */
854 if ( sop->oq_search.rs_tlimit != -1 && slap_get_time() > stoptime ) {
855 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
856 rs->sr_ref = rs->sr_v2ref;
857 send_ldap_result( sop, rs );
862 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
866 /* get the entry with reader lock */
868 rs->sr_err = bdb_cache_find_entry_id( op->o_bd, NULL,
869 id, &ei, 0, locker, &lock, op->o_tmpmemctx );
871 if (rs->sr_err == LDAP_BUSY) {
872 rs->sr_text = "ldap server busy";
873 send_ldap_result( sop, rs );
876 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
877 || rs->sr_err == DB_LOCK_NOTGRANTED )
882 if ( ei && rs->sr_err == 0 ) {
889 if( !BDB_IDL_IS_RANGE(candidates) ) {
890 /* only complain for non-range IDLs */
892 LDAP_LOG ( OPERATION, RESULTS,
893 "bdb_search: candidate %ld not found\n",
896 Debug( LDAP_DEBUG_TRACE,
897 "bdb_search: candidate %ld not found\n",
904 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
911 #ifdef BDB_SUBENTRIES
912 if ( is_entry_subentry( e ) ) {
913 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
914 if(!get_subentries_visibility( sop )) {
915 /* only subentries are visible */
919 } else if ( get_subentries( sop ) &&
920 !get_subentries_visibility( sop ))
922 /* only subentries are visible */
926 } else if ( get_subentries_visibility( sop )) {
927 /* only subentries are visible */
932 /* Does this candidate actually satisfy the search scope?
934 * Note that we don't lock access to the bei_parent pointer.
935 * Since only leaf nodes can be deleted, the parent of any
936 * node will always be a valid node. Also since we have
937 * a Read lock on the data, it cannot be renamed out of the
938 * scope while we are looking at it, and unless we're using
939 * BDB_HIER, its parents cannot be moved either.
941 switch( op->ors_scope ) {
942 case LDAP_SCOPE_BASE:
943 /* This is always true, yes? */
944 if ( id == base.e_id )
947 case LDAP_SCOPE_ONELEVEL:
948 if ( ei->bei_parent->bei_id == base.e_id )
951 case LDAP_SCOPE_SUBTREE:
953 for ( tmp = BEI(e); tmp->bei_parent;
954 tmp = tmp->bei_parent ) {
955 if ( tmp->bei_id == base.e_id ) {
964 /* aliases were already dereferenced in candidate list */
965 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
966 /* but if the search base is an alias, and we didn't
967 * deref it when finding, return it.
969 if ( is_entry_alias(e) &&
970 ((sop->ors_deref & LDAP_DEREF_FINDING)
971 || !bvmatch(&e->e_nname, &op->o_req_ndn)))
976 /* scopes is only non-empty for onelevel or subtree */
977 if ( !scopeok && BDB_IDL_N(scopes) ) {
979 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
980 x = bdb_idl_search( scopes,
982 if ( scopes[x] == e->e_id )
985 /* subtree, walk up the tree */
986 EntryInfo *tmp = BEI(e);
987 for (;tmp->bei_parent;
988 tmp=tmp->bei_parent) {
990 scopes, tmp->bei_id );
991 if ( scopes[x] == tmp->bei_id ) {
1001 /* Not in scope, ignore it */
1004 LDAP_LOG ( OPERATION, RESULTS,
1005 "bdb_search: %ld scope not okay\n",
1008 Debug( LDAP_DEBUG_TRACE,
1009 "bdb_search: %ld scope not okay\n",
1016 * if it's a referral, add it to the list of referrals. only do
1017 * this for non-base searches, and don't check the filter
1018 * explicitly here since it's only a candidate anyway.
1020 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1021 && is_entry_referral( e ) )
1023 BerVarray erefs = get_entry_referrals( sop, e );
1024 rs->sr_ref = referral_rewrite( erefs,
1026 sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE
1027 ? LDAP_SCOPE_SUBTREE
1028 : LDAP_SCOPE_BASE );
1030 send_search_reference( sop, rs );
1032 ber_bvarray_free( rs->sr_ref );
1033 ber_bvarray_free( erefs );
1039 /* if it matches the filter and scope, send it */
1040 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1042 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1043 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1045 rs->sr_err = LDAP_COMPARE_TRUE;
1048 #ifdef LDAP_CLIENT_UPDATE
1049 if ( sop->o_clientupdate_type & SLAP_LCUP_SYNC ) {
1050 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1054 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1055 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1056 rs->sr_err = test_filter( sop,
1057 rs->sr_entry, sop->oq_search.rs_filter );
1058 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1059 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1060 entry_sync_state = LDAP_SYNC_ADD;
1062 entry_sync_state = LDAP_SYNC_PRESENT;
1069 rs->sr_err = test_filter( sop,
1070 rs->sr_entry, sop->oq_search.rs_filter );
1072 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1076 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1077 /* check size limit */
1078 if ( --sop->oq_search.rs_slimit == -1 ) {
1079 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1082 bdb_cache_return_entry_r( bdb->bi_dbenv,
1083 &bdb->bi_cache, e, &lock );
1085 rs->sr_entry = NULL;
1086 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1087 rs->sr_ref = rs->sr_v2ref;
1088 send_ldap_result( sop, rs );
1092 #ifdef LDAP_CONTROL_PAGEDRESULTS
1093 if ( get_pagedresults(sop) ) {
1094 if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
1095 send_pagerequest_response( sop, rs,
1107 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
1112 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1115 int premodify_found = 0;
1116 int entry_sync_state;
1119 if ( ps_type == LDAP_PSEARCH_BY_ADD ||
1120 ps_type == LDAP_PSEARCH_BY_DELETE ||
1121 ps_type == LDAP_PSEARCH_BY_MODIFY ||
1122 ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1124 if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1125 struct psid_entry* psid_e;
1126 LDAP_LIST_FOREACH( psid_e,
1127 &op->o_pm_list, ps_link)
1129 if( psid_e->ps_op == sop ) {
1131 premodify_found = 1;
1133 LDAP_LIST_REMOVE(psid_e, ps_link);
1137 if (psid_e != NULL) free (psid_e);
1140 if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1141 entry_sync_state = LDAP_SYNC_ADD;
1142 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1143 entry_sync_state = LDAP_SYNC_DELETE;
1144 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1145 if ( premodify_found ) {
1146 entry_sync_state = LDAP_SYNC_MODIFY;
1148 entry_sync_state = LDAP_SYNC_ADD;
1150 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1151 entry_sync_state = LDAP_SYNC_DELETE;
1158 #ifdef LDAP_CLIENT_UPDATE
1159 if ( sop->o_ps_protocol == LDAP_CLIENT_UPDATE ) {
1160 int entry_count = ++sop->o_ps_entries;
1161 if ( IS_BDB_REPLACE(ps_type) ) {
1162 rs->sr_err = bdb_build_lcup_update_ctrl( sop,
1163 rs, e, entry_count, ctrls,
1164 num_ctrls++, &latest_entrycsn_bv,
1165 SLAP_LCUP_ENTRY_DELETED_TRUE );
1167 rs->sr_err = bdb_build_lcup_update_ctrl( sop,
1168 rs, e, entry_count, ctrls,
1169 num_ctrls++, &latest_entrycsn_bv,
1170 SLAP_LCUP_ENTRY_DELETED_FALSE );
1172 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1173 rs->sr_attrs = attrs;
1174 rs->sr_ctrls = ctrls;
1175 result = send_search_entry( sop, rs );
1176 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1177 ch_free( ctrls[--num_ctrls] );
1178 ctrls[num_ctrls] = NULL;
1179 rs->sr_ctrls = NULL;
1183 if ( sop->o_ps_protocol == LDAP_SYNC ) {
1184 rs->sr_err = bdb_build_sync_state_ctrl( sop,
1185 rs, e, entry_sync_state, ctrls,
1186 num_ctrls++, 1, &latest_entrycsn_bv );
1187 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1188 rs->sr_attrs = attrs;
1189 rs->sr_ctrls = ctrls;
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;
1202 } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1203 struct psid_entry* psid_e;
1204 psid_e = (struct psid_entry *) calloc (1,
1205 sizeof(struct psid_entry));
1206 psid_e->ps_op = sop;
1207 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1211 printf("Error !\n");
1214 #ifdef LDAP_CLIENT_UPDATE
1215 if ( sop->o_clientupdate_type & SLAP_LCUP_SYNC ) {
1216 rs->sr_err = bdb_build_lcup_update_ctrl( sop,
1217 rs, e, ++entry_count, ctrls,
1218 num_ctrls++, &latest_entrycsn_bv,
1219 SLAP_LCUP_ENTRY_DELETED_FALSE );
1220 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1221 rs->sr_ctrls = ctrls;
1222 rs->sr_attrs = sop->oq_search.rs_attrs;
1223 result = send_search_entry( sop, rs );
1224 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1225 ch_free( ctrls[--num_ctrls] );
1226 ctrls[num_ctrls] = NULL;
1227 rs->sr_ctrls = NULL;
1231 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1232 rs->sr_err = bdb_build_sync_state_ctrl( sop,
1233 rs, e, entry_sync_state, ctrls,
1234 num_ctrls++, 0, &latest_entrycsn_bv );
1235 if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1237 rs->sr_ctrls = ctrls;
1238 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1239 rs->sr_attrs = sop->oq_search.rs_attrs;
1240 } else { /* PRESENT */
1241 rs->sr_attrs = &null_attr;
1243 result = send_search_entry( sop, rs );
1244 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1245 ch_free( ctrls[--num_ctrls] );
1246 ctrls[num_ctrls] = NULL;
1247 rs->sr_ctrls = NULL;
1252 rs->sr_attrs = sop->oq_search.rs_attrs;
1253 rs->sr_ctrls = NULL;
1254 result = send_search_entry( sop, rs );
1259 case 0: /* entry sent ok */
1261 case 1: /* entry not sent */
1263 case -1: /* connection closed */
1265 bdb_cache_return_entry_r(bdb->bi_dbenv,
1266 &bdb->bi_cache, e, &lock);
1268 rs->sr_entry = NULL;
1269 rs->sr_err = LDAP_OTHER;
1275 LDAP_LOG ( OPERATION, RESULTS,
1276 "bdb_search: %ld does not match filter\n", (long) id, 0, 0);
1278 Debug( LDAP_DEBUG_TRACE,
1279 "bdb_search: %ld does not match filter\n",
1286 /* free reader lock */
1288 bdb_cache_return_entry_r( bdb->bi_dbenv,
1289 &bdb->bi_cache, e , &lock);
1292 rs->sr_entry = NULL;
1295 ldap_pvt_thread_yield();
1299 #ifdef LDAP_CLIENT_UPDATE
1300 if ( sop->o_clientupdate_type & SLAP_LCUP_SYNC ) {
1301 bdb_build_lcup_done_ctrl( sop, rs, ctrls,
1302 num_ctrls++, &latest_entrycsn_bv );
1304 rs->sr_ctrls = ctrls;
1305 rs->sr_ref = rs->sr_v2ref;
1306 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1307 send_ldap_result( sop, rs );
1309 ch_free( latest_entrycsn_bv.bv_val );
1310 latest_entrycsn_bv.bv_val = NULL;
1312 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1313 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1315 ch_free( ctrls[--num_ctrls] );
1316 ctrls[num_ctrls] = NULL;
1320 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1321 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1322 /* refreshAndPersist mode */
1323 rs->sr_err = LDAP_SUCCESS;
1324 rs->sr_rspoid = LDAP_SYNC_INFO;
1325 rs->sr_ctrls = NULL;
1326 bdb_send_ldap_intermediate( sop, rs,
1327 LDAP_SYNC_REFRESH_DONE, &latest_entrycsn_bv );
1329 /* refreshOnly mode */
1330 bdb_build_sync_done_ctrl( sop, rs, ctrls,
1331 num_ctrls++, 1, &latest_entrycsn_bv );
1332 rs->sr_ctrls = ctrls;
1333 rs->sr_ref = rs->sr_v2ref;
1334 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1335 send_ldap_result( sop, rs );
1336 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1337 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1339 ch_free( ctrls[--num_ctrls] );
1340 ctrls[num_ctrls] = NULL;
1343 ch_free( latest_entrycsn_bv.bv_val );
1344 latest_entrycsn_bv.bv_val = NULL;
1348 rs->sr_ctrls = NULL;
1349 rs->sr_ref = rs->sr_v2ref;
1350 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1351 send_ldap_result( sop, rs );
1355 rs->sr_err = LDAP_SUCCESS;
1358 if( !IS_PSEARCH && e != NULL ) {
1359 /* free reader lock */
1360 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1363 LOCK_ID_FREE (bdb->bi_dbenv, locker );
1365 if( rs->sr_v2ref ) {
1366 ber_bvarray_free( rs->sr_v2ref );
1367 rs->sr_v2ref = NULL;
1369 if( realbase.bv_val ) ch_free( realbase.bv_val );
1375 static int base_candidate(
1381 LDAP_LOG ( OPERATION, ENTRY,
1382 "base_candidate: base: \"%s\" (0x%08lx)\n",
1383 e->e_nname.bv_val, (long) e->e_id, 0);
1385 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1386 e->e_nname.bv_val, (long) e->e_id, 0);
1394 /* Look for "objectClass Present" in this filter.
1395 * Also count depth of filter tree while we're at it.
1397 static int oc_filter(
1405 if( cur > *max ) *max = cur;
1407 switch(f->f_choice) {
1408 case LDAP_FILTER_PRESENT:
1409 if (f->f_desc == slap_schema.si_ad_objectClass) {
1414 case LDAP_FILTER_AND:
1415 case LDAP_FILTER_OR:
1417 for (f=f->f_and; f; f=f->f_next) {
1418 (void) oc_filter(f, cur, max);
1428 static void search_stack_free( void *key, void *data )
1430 ber_memfree_x(data, NULL);
1433 static void *search_stack(
1437 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1440 if ( op->o_threadctx ) {
1441 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1444 ret = bdb->bi_search_stack;
1448 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1450 if ( op->o_threadctx ) {
1451 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1452 ret, search_stack_free );
1454 bdb->bi_search_stack = ret;
1460 static int search_candidates(
1469 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1471 Filter f, scopef, rf, xf, nf;
1473 AttributeAssertion aa_ref;
1474 #ifdef BDB_SUBENTRIES
1476 AttributeAssertion aa_subentry;
1480 * This routine takes as input a filter (user-filter)
1481 * and rewrites it as follows:
1482 * (&(scope=DN)[(objectClass=subentry)]
1483 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1487 LDAP_LOG ( OPERATION, ENTRY,
1488 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1489 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope);
1491 Debug(LDAP_DEBUG_TRACE,
1492 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1493 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1496 xf.f_or = op->oq_search.rs_filter;
1497 xf.f_choice = LDAP_FILTER_OR;
1500 /* If the user's filter uses objectClass=*,
1501 * these clauses are redundant.
1503 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1504 && !get_subentries_visibility(op) )
1506 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1507 /* match referral objects */
1508 struct berval bv_ref = { sizeof("referral")-1, "referral" };
1509 rf.f_choice = LDAP_FILTER_EQUALITY;
1511 rf.f_av_desc = slap_schema.si_ad_objectClass;
1512 rf.f_av_value = bv_ref;
1513 rf.f_next = xf.f_or;
1519 scopef.f_choice = op->oq_search.rs_scope == LDAP_SCOPE_SUBTREE
1520 ? SLAPD_FILTER_DN_SUBTREE
1521 : SLAPD_FILTER_DN_ONE;
1523 scopef.f_dn = (struct berval *)&e->e_id;
1525 scopef.f_dn = &e->e_nname;
1527 scopef.f_next = NULL;
1530 f.f_choice = LDAP_FILTER_AND;
1532 /* Dummy; we compute scope separately now */
1533 nf.f_choice = SLAPD_FILTER_COMPUTED;
1534 nf.f_next = xf.f_or == op->oq_search.rs_filter
1535 ? op->oq_search.rs_filter : &xf ;
1536 /* Filter depth increased again, adding dummy clause */
1539 #ifdef BDB_SUBENTRIES
1540 if( get_subentries_visibility( op ) ) {
1541 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
1542 sf.f_choice = LDAP_FILTER_EQUALITY;
1543 sf.f_ava = &aa_subentry;
1544 sf.f_av_desc = slap_schema.si_ad_objectClass;
1545 sf.f_av_value = bv_subentry;
1546 sf.f_next = nf.f_next;
1551 /* Allocate IDL stack, plus 1 more for former tmp */
1552 if ( depth+1 > bdb->bi_search_stack_depth ) {
1553 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1555 stack = search_stack( stackop );
1558 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1559 rc = search_aliases( op, rs, e, locker, &scopef, ids, scopes, stack );
1561 rc = bdb_filter_candidates( op, &scopef, ids,
1562 stack, stack+BDB_IDL_UM_SIZE );
1565 if ( rc == LDAP_SUCCESS ) {
1566 rc = bdb_filter_candidates( op, &f, ids,
1567 stack, stack+BDB_IDL_UM_SIZE );
1570 if ( depth+1 > bdb->bi_search_stack_depth ) {
1576 LDAP_LOG ( OPERATION, DETAIL1,
1577 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
1579 Debug(LDAP_DEBUG_TRACE,
1580 "bdb_search_candidates: failed (rc=%d)\n",
1586 LDAP_LOG ( OPERATION, DETAIL1,
1587 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1588 (long) ids[0], (long) BDB_IDL_FIRST(ids),
1589 (long) BDB_IDL_LAST(ids));
1591 Debug(LDAP_DEBUG_TRACE,
1592 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1594 (long) BDB_IDL_FIRST(ids),
1595 (long) BDB_IDL_LAST(ids) );
1602 #ifdef LDAP_CONTROL_PAGEDRESULTS
1604 send_pagerequest_response(
1610 LDAPControl ctrl, *ctrls[2];
1611 char berbuf[LBER_ELEMENT_SIZEOF];
1612 BerElement *ber = (BerElement *)berbuf;
1613 struct berval cookie = { 0, NULL };
1614 PagedResultsCookie respcookie;
1617 LDAP_LOG ( OPERATION, ENTRY,
1618 "send_pagerequest_response: lastid: (0x%08lx) "
1619 "nentries: (0x%081x)\n",
1620 lastid, rs->sr_nentries, NULL );
1622 Debug(LDAP_DEBUG_ARGS, "send_pagerequest_response: lastid: (0x%08lx) "
1623 "nentries: (0x%081x)\n", lastid, rs->sr_nentries, NULL );
1626 ctrl.ldctl_value.bv_val = NULL;
1630 ber_init2( ber, NULL, LBER_USE_DER );
1632 respcookie = ( PagedResultsCookie )lastid;
1633 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1634 cookie.bv_len = sizeof( respcookie );
1635 cookie.bv_val = (char *)&respcookie;
1638 * FIXME: we should consider sending an estimate of the entries
1639 * left, after appropriate security check is done
1641 ber_printf( ber, "{iO}", tentries, &cookie );
1643 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1647 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1648 ctrls[0]->ldctl_iscritical = 0;
1650 rs->sr_ctrls = ctrls;
1651 rs->sr_err = LDAP_SUCCESS;
1652 send_ldap_result( op, rs );
1655 (void) ber_free_buf( ber );
1659 #ifdef LDAP_CLIENT_UPDATE
1661 bdb_build_lcup_update_ctrl(
1666 LDAPControl **ctrls,
1668 struct berval *latest_entrycsn_bv,
1674 const char *text = NULL;
1676 char berbuf[LBER_ELEMENT_SIZEOF];
1677 BerElement *ber = (BerElement *)berbuf;
1679 struct berval entrycsn_bv = { 0, NULL };
1681 ber_init2( ber, 0, LBER_USE_DER );
1683 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1685 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
1686 AttributeDescription *desc = a->a_desc;
1687 if ( desc == slap_schema.si_ad_entryCSN ) {
1688 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
1689 if ( latest_entrycsn_bv->bv_val == NULL ) {
1690 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1692 res = value_match( &ret, desc,
1693 desc->ad_type->sat_ordering, 0,
1694 &entrycsn_bv, latest_entrycsn_bv, &text );
1695 if ( res != LDAP_SUCCESS ) {
1698 LDAP_LOG ( OPERATION, RESULTS,
1699 "bdb_search: value_match failed\n",
1702 Debug( LDAP_DEBUG_TRACE,
1703 "bdb_search: value_match failed\n",
1709 ch_free( latest_entrycsn_bv->bv_val );
1710 latest_entrycsn_bv->bv_val = NULL;
1711 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1717 if ( entry_count % op->o_clientupdate_interval == 0 ) {
1720 SLAP_LCUP_STATE_UPDATE_FALSE,
1722 LDAP_CUP_COOKIE_OID, &entrycsn_bv );
1723 } else { /* Do not send cookie */
1726 SLAP_LCUP_STATE_UPDATE_FALSE,
1730 ch_free( entrycsn_bv.bv_val );
1731 entrycsn_bv.bv_val = NULL;
1733 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_ENTRY_UPDATE;
1734 ctrls[num_ctrls]->ldctl_iscritical = op->o_clientupdate;
1735 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1737 ber_free_buf( ber );
1741 LDAP_LOG ( OPERATION, RESULTS,
1742 "bdb_build_lcup_ctrl: ber_flatten2 failed\n",
1745 Debug( LDAP_DEBUG_TRACE,
1746 "bdb_build_lcup_ctrl: ber_flatten2 failed\n",
1749 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1753 return LDAP_SUCCESS;
1757 bdb_build_lcup_done_ctrl(
1760 LDAPControl **ctrls,
1762 struct berval *latest_entrycsn_bv )
1765 char berbuf[LBER_ELEMENT_SIZEOF];
1766 BerElement *ber = (BerElement *)berbuf;
1768 ber_init2( ber, NULL, LBER_USE_DER );
1770 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1772 ber_printf( ber, "{sON}", LDAP_CUP_COOKIE_OID, latest_entrycsn_bv );
1774 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_CLIENT_UPDATE_DONE;
1775 ctrls[num_ctrls]->ldctl_iscritical = op->o_clientupdate;
1776 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1778 ber_free_buf( ber );
1782 LDAP_LOG ( OPERATION, RESULTS,
1783 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n", 0, 0, 0 );
1785 Debug( LDAP_DEBUG_TRACE,
1786 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1789 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1793 return LDAP_SUCCESS;
1799 bdb_build_sync_state_ctrl(
1803 int entry_sync_state,
1804 LDAPControl **ctrls,
1807 struct berval *latest_entrycsn_bv )
1812 const char *text = NULL;
1814 char berbuf[LBER_ELEMENT_SIZEOF];
1815 BerElement *ber = (BerElement *)berbuf;
1817 struct berval entryuuid_bv = { 0, NULL };
1818 struct berval entrycsn_bv = { 0, NULL };
1820 ber_init2( ber, 0, LBER_USE_DER );
1822 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1824 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
1825 AttributeDescription *desc = a->a_desc;
1826 if ( desc == slap_schema.si_ad_entryCSN ) {
1827 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
1828 if ( latest_entrycsn_bv->bv_val == NULL ) {
1829 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1831 res = value_match( &ret, desc,
1832 desc->ad_type->sat_ordering, 0,
1833 &entrycsn_bv, latest_entrycsn_bv, &text );
1834 if ( res != LDAP_SUCCESS ) {
1837 LDAP_LOG ( OPERATION, RESULTS,
1838 "bdb_search: value_match failed\n",
1841 Debug( LDAP_DEBUG_TRACE,
1842 "bdb_search: value_match failed\n",
1847 ch_free( latest_entrycsn_bv->bv_val );
1848 latest_entrycsn_bv->bv_val = NULL;
1849 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1852 } else if ( desc == slap_schema.si_ad_entryUUID ) {
1853 ber_dupbv( &entryuuid_bv, &a->a_vals[0] );
1857 if ( send_cookie ) {
1858 ber_printf( ber, "{eOON}",
1859 entry_sync_state, &entryuuid_bv, &entrycsn_bv );
1861 ber_printf( ber, "{eON}",
1862 entry_sync_state, &entryuuid_bv );
1865 ch_free( entrycsn_bv.bv_val );
1866 entrycsn_bv.bv_val = NULL;
1867 ch_free( entryuuid_bv.bv_val );
1868 entryuuid_bv.bv_val = NULL;
1870 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
1871 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
1872 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1874 ber_free_buf( ber );
1878 LDAP_LOG ( OPERATION, RESULTS,
1879 "bdb_build_sync_ctrl: ber_flatten2 failed\n",
1882 Debug( LDAP_DEBUG_TRACE,
1883 "bdb_build_sync_ctrl: ber_flatten2 failed\n",
1886 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1890 return LDAP_SUCCESS;
1894 bdb_build_sync_done_ctrl(
1897 LDAPControl **ctrls,
1900 struct berval *latest_entrycsn_bv )
1903 char berbuf[LBER_ELEMENT_SIZEOF];
1904 BerElement *ber = (BerElement *)berbuf;
1906 ber_init2( ber, NULL, LBER_USE_DER );
1908 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1910 if ( send_cookie ) {
1911 ber_printf( ber, "{ON}", latest_entrycsn_bv );
1913 ber_printf( ber, "{N}" );
1916 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
1917 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
1918 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1920 ber_free_buf( ber );
1924 LDAP_LOG ( OPERATION, RESULTS,
1925 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1928 Debug( LDAP_DEBUG_TRACE,
1929 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1932 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1936 return LDAP_SUCCESS;
1940 bdb_send_ldap_intermediate(
1944 struct berval *cookie )
1946 char berbuf[LBER_ELEMENT_SIZEOF];
1947 BerElement *ber = (BerElement *)berbuf;
1948 struct berval rspdata;
1952 ber_init2( ber, NULL, LBER_USE_DER );
1954 if ( cookie == NULL ) {
1955 ber_printf( ber, "{eN}", state );
1957 ber_printf( ber, "{eON}", state, cookie );
1960 ret = ber_flatten2( ber, &rspdata, 0 );
1964 LDAP_LOG ( OPERATION, RESULTS,
1965 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1968 Debug( LDAP_DEBUG_TRACE,
1969 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1972 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1976 rs->sr_rspdata = &rspdata;
1977 send_ldap_intermediate_resp( op, rs );
1978 rs->sr_rspdata = NULL;
1979 ber_free_buf( ber );
1981 return LDAP_SUCCESS;