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_ALL( bdb, 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.
272 rs->sr_err = bdb_cache_find_entry_id(op->o_bd, NULL, ido, &ei,
273 0, locker, &locka, op->o_tmpmemctx );
274 if (rs->sr_err != LDAP_SUCCESS) goto nextido;
276 sf->f_dn = &e->e_nname;
281 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
282 #define IS_BDB_REPLACE(type) (( type == LDAP_PSEARCH_BY_DELETE ) || \
283 ( type == LDAP_PSEARCH_BY_SCOPEOUT ))
284 #define IS_PSEARCH (op != sop)
287 bdb_abandon( Operation *op, SlapReply *rs )
290 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
292 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
293 if ( ps_list->o_connid == op->o_connid ) {
294 if ( ps_list->o_msgid == op->oq_abandon.rs_msgid ) {
295 ps_list->o_abandon = 1;
296 LDAP_LIST_REMOVE( ps_list, o_ps_link );
297 slap_op_free ( ps_list );
302 return LDAP_UNAVAILABLE;
306 bdb_cancel( Operation *op, SlapReply *rs )
309 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
311 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
312 if ( ps_list->o_connid == op->o_connid ) {
313 if ( ps_list->o_msgid == op->oq_cancel.rs_msgid ) {
314 ps_list->o_cancel = SLAP_CANCEL_DONE;
315 LDAP_LIST_REMOVE( ps_list, o_ps_link );
318 bdb_build_sync_done_ctrl( conn, ps_list, ps_list->ctrls,
319 1, &latest_entrycsn_bv );
320 send_ldap_result( conn, ps_list, LDAP_CANCELLED,
321 NULL, NULL, NULL, ps_list->ctrls, ps_list->nentries);
323 rs->sr_err = LDAP_CANCELLED;
324 send_ldap_result( ps_list, rs );
326 slap_op_free ( ps_list );
331 return LDAP_UNAVAILABLE;
334 int bdb_search( Operation *op, SlapReply *rs )
336 return bdb_do_search( op, rs, op, NULL, 0 );
339 /* For persistent searches, op is the currently executing operation,
340 * sop is the persistent search. For regular searches, sop = op.
343 bdb_do_search( Operation *op, SlapReply *rs, Operation *sop,
344 Entry *ps_e, int ps_type )
348 int bdb_search( Operation *op, SlapReply *rs )
351 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
354 ID candidates[BDB_IDL_UM_SIZE];
355 ID scopes[BDB_IDL_DB_SIZE];
356 Entry *e = NULL, dummy;
357 Entry *matched = NULL;
359 struct berval realbase = { 0, NULL };
363 AttributeName *attrs;
365 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
366 Filter cookief, csnfnot, csnfeq, csnfand, csnfge;
367 AttributeAssertion aa_ge, aa_eq;
370 struct berval entrycsn_bv = { 0, NULL };
372 struct berval latest_entrycsn_bv = { 0, NULL };
373 LDAPControl *ctrls[SLAP_SEARCH_MAX_CTRLS];
375 AttributeName uuid_attr[2];
378 int entry_sync_state = -1;
379 AttributeName null_attr;
382 struct slap_limits_set *limit = NULL;
385 u_int32_t locker = 0;
389 LDAP_LOG( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
391 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
394 attrs = sop->oq_search.rs_attrs;
396 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
397 #ifdef LDAP_CLIENT_UPDATE
398 if ( !IS_PSEARCH && sop->o_clientupdate_type & SLAP_LCUP_PERSIST ) {
399 sop->o_ps_protocol = LDAP_CLIENT_UPDATE;
400 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
405 /* psearch needs to be registered before refresh begins */
406 /* psearch and refresh transmission is serialized in send_ldap_ber() */
407 if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
408 sop->o_ps_protocol = LDAP_SYNC;
409 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
411 null_attr.an_desc = NULL;
412 null_attr.an_oc = NULL;
413 null_attr.an_name.bv_len = 0;
414 null_attr.an_name.bv_val = NULL;
417 for ( num_ctrls = 0; num_ctrls < SLAP_SEARCH_MAX_CTRLS; num_ctrls++ ) {
418 ctrls[num_ctrls] = NULL;
422 if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
423 #ifdef LDAP_CLIENT_UPDATE
424 if ( sop->o_ps_protocol == LDAP_CLIENT_UPDATE ) {
426 attrs[0].an_desc = slap_schema.si_ad_entryUUID;
427 attrs[0].an_oc = NULL;
428 attrs[0].an_name = attrs[0].an_desc->ad_cname;
429 attrs[1].an_desc = NULL;
430 attrs[1].an_oc = NULL;
431 attrs[1].an_name.bv_len = 0;
432 attrs[1].an_name.bv_val = NULL;
436 if (sop->o_ps_protocol == LDAP_SYNC ) {
438 attrs[0].an_desc = NULL;
439 attrs[0].an_oc = NULL;
440 attrs[0].an_name.bv_len = 0;
441 attrs[0].an_name.bv_val = NULL;
451 manageDSAit = get_manageDSAit( sop );
453 rs->sr_err = LOCK_ID (bdb->bi_dbenv, &locker );
459 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
463 if ( sop->o_req_ndn.bv_len == 0 ) {
464 /* DIT root special case */
465 e = (Entry *) &slap_entry_root;
469 /* get entry with reader lock */
470 rs->sr_err = bdb_dn2entry( op->o_bd, NULL, &sop->o_req_ndn, &ei,
471 1, locker, &lock, op->o_tmpmemctx );
476 matched = ei->bei_e; break;
478 e = ei->bei_e; break;
480 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
481 LOCK_ID_FREE (bdb->bi_dbenv, locker );
483 case DB_LOCK_DEADLOCK:
484 case DB_LOCK_NOTGRANTED:
487 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
488 LOCK_ID_FREE (bdb->bi_dbenv, locker );
492 if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
493 BDB_IDL_ZERO(candidates);
494 e = deref_base( op, rs, e, &matched, locker, &lock,
499 struct berval matched_dn = { 0, NULL };
501 if ( matched != NULL ) {
503 ber_dupbv( &matched_dn, &matched->e_name );
505 erefs = is_entry_referral( matched )
506 ? get_entry_referrals( op, matched )
509 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
514 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
515 &sop->o_req_dn, sop->oq_search.rs_scope );
516 ber_bvarray_free( erefs );
520 rs->sr_ref = referral_rewrite( default_referral,
521 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
524 rs->sr_err=LDAP_REFERRAL;
525 rs->sr_matched = matched_dn.bv_val;
526 send_ldap_result( sop, rs );
528 LOCK_ID_FREE (bdb->bi_dbenv, locker );
530 ber_bvarray_free( rs->sr_ref );
533 if ( matched_dn.bv_val ) {
534 ber_memfree( matched_dn.bv_val );
535 rs->sr_matched = NULL;
540 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
541 /* entry is a referral, don't allow add */
542 struct berval matched_dn;
545 ber_dupbv( &matched_dn, &e->e_name );
546 erefs = get_entry_referrals( op, e );
548 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
552 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
553 &sop->o_req_dn, sop->oq_search.rs_scope );
554 ber_bvarray_free( erefs );
558 LDAP_LOG ( OPERATION, RESULTS,
559 "bdb_search: entry is referral\n", 0, 0, 0 );
561 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
565 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
566 rs->sr_err = LDAP_REFERRAL;
567 rs->sr_matched = matched_dn.bv_val;
568 send_ldap_result( sop, rs );
570 LOCK_ID_FREE (bdb->bi_dbenv, locker );
571 ber_bvarray_free( rs->sr_ref );
573 ber_memfree( matched_dn.bv_val );
574 rs->sr_matched = NULL;
578 /* if not root, get appropriate limits */
579 if ( be_isroot( op->o_bd, &sop->o_ndn ) ) {
582 ( void ) get_limits( op->o_bd, &sop->o_ndn, &limit );
585 /* The time/size limits come first because they require very little
586 * effort, so there's no chance the candidates are selected and then
587 * the request is not honored only because of time/size constraints */
589 /* if no time limit requested, use soft limit (unless root!) */
591 if ( sop->oq_search.rs_tlimit == 0 ) {
592 sop->oq_search.rs_tlimit = -1; /* allow root to set no limit */
595 if ( sop->oq_search.rs_slimit == 0 ) {
596 sop->oq_search.rs_slimit = -1;
600 /* if no limit is required, use soft limit */
601 if ( sop->oq_search.rs_tlimit <= 0 ) {
602 sop->oq_search.rs_tlimit = limit->lms_t_soft;
604 /* if requested limit higher than hard limit, abort */
605 } else if ( sop->oq_search.rs_tlimit > limit->lms_t_hard ) {
606 /* no hard limit means use soft instead */
607 if ( limit->lms_t_hard == 0
608 && limit->lms_t_soft > -1
609 && sop->oq_search.rs_tlimit > limit->lms_t_soft ) {
610 sop->oq_search.rs_tlimit = limit->lms_t_soft;
612 /* positive hard limit means abort */
613 } else if ( limit->lms_t_hard > 0 ) {
614 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
615 send_ldap_result( sop, rs );
620 /* negative hard limit means no limit */
623 /* if no limit is required, use soft limit */
624 if ( sop->oq_search.rs_slimit <= 0 ) {
625 if ( get_pagedresults(sop) && limit->lms_s_pr != 0 ) {
626 sop->oq_search.rs_slimit = limit->lms_s_pr;
628 sop->oq_search.rs_slimit = limit->lms_s_soft;
631 /* if requested limit higher than hard limit, abort */
632 } else if ( sop->oq_search.rs_slimit > limit->lms_s_hard ) {
633 /* no hard limit means use soft instead */
634 if ( limit->lms_s_hard == 0
635 && limit->lms_s_soft > -1
636 && sop->oq_search.rs_slimit > limit->lms_s_soft ) {
637 sop->oq_search.rs_slimit = limit->lms_s_soft;
639 /* positive hard limit means abort */
640 } else if ( limit->lms_s_hard > 0 ) {
641 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
642 send_ldap_result( sop, rs );
647 /* negative hard limit means no limit */
651 /* compute it anyway; root does not use it */
652 stoptime = op->o_time + sop->oq_search.rs_tlimit;
654 /* need normalized dn below */
655 ber_dupbv( &realbase, &e->e_nname );
657 dummy.e_nname = realbase;
658 dummy.e_id = e->e_id;
660 if ( e != &slap_entry_root ) {
661 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
665 /* select candidates */
666 if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
667 rs->sr_err = base_candidate( op->o_bd, &dummy, candidates );
670 BDB_IDL_ALL( bdb, candidates );
671 BDB_IDL_ZERO( scopes );
672 rs->sr_err = search_candidates( op, sop, rs, &dummy, locker, candidates, scopes );
675 /* start cursor at beginning of candidates.
678 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
680 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
681 cursor = bdb_idl_search( candidates, ps_e->e_id );
682 if ( candidates[cursor] != ps_e->e_id ) {
683 rs->sr_err = LDAP_SUCCESS;
687 if ( ps_e->e_id < BDB_IDL_RANGE_FIRST(candidates)
688 || ps_e->e_id > BDB_IDL_RANGE_LAST(candidates)){
689 rs->sr_err = LDAP_SUCCESS;
694 candidates[1] = ps_e->e_id;
698 if ( candidates[0] == 0 ) {
700 LDAP_LOG ( OPERATION, RESULTS,
701 "bdb_search: no candidates\n", 0, 0, 0 );
703 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
707 rs->sr_err = LDAP_SUCCESS;
708 send_ldap_result( sop, rs );
713 /* if not root and candidates exceed to-be-checked entries, abort */
714 if ( !isroot && limit->lms_s_unchecked != -1 ) {
715 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
716 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
717 send_ldap_result( sop, rs );
723 if ( isroot || !limit->lms_s_pr_hide ) {
724 tentries = BDB_IDL_N(candidates);
727 #ifdef LDAP_CONTROL_PAGEDRESULTS
728 if ( get_pagedresults(sop) ) {
729 if ( sop->o_pagedresults_state.ps_cookie == 0 ) {
732 if ( sop->o_pagedresults_size == 0 ) {
733 rs->sr_err = LDAP_SUCCESS;
734 rs->sr_text = "search abandoned by pagedResult size=0";
735 send_ldap_result( sop, rs );
738 for ( id = bdb_idl_first( candidates, &cursor );
739 id != NOID && id <= (ID)( sop->o_pagedresults_state.ps_cookie );
740 id = bdb_idl_next( candidates, &cursor ) );
742 if ( cursor == NOID ) {
744 LDAP_LOG ( OPERATION, RESULTS,
745 "bdb_search: no paged results candidates\n",
748 Debug( LDAP_DEBUG_TRACE,
749 "bdb_search: no paged results candidates\n",
752 send_pagerequest_response( sop, rs, lastid, 0 );
761 #ifdef LDAP_CLIENT_UPDATE
762 if ( (sop->o_clientupdate_type & SLAP_LCUP_SYNC) ||
763 (IS_PSEARCH && sop->o_ps_protocol == LDAP_CLIENT_UPDATE ))
765 cookief.f_choice = LDAP_FILTER_AND;
766 cookief.f_and = &csnfnot;
767 cookief.f_next = NULL;
769 csnfnot.f_choice = LDAP_FILTER_NOT;
770 csnfnot.f_not = &csnfeq;
771 csnfnot.f_next = &csnfand;
773 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
774 csnfeq.f_ava = &aa_eq;
775 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
776 csnfeq.f_av_value = sop->o_clientupdate_state;
778 csnfand.f_choice = LDAP_FILTER_AND;
779 csnfand.f_and = &csnfge;
780 csnfand.f_next = NULL;
782 csnfge.f_choice = LDAP_FILTER_GE;
783 csnfge.f_ava = &aa_ge;
784 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
785 csnfge.f_av_value = sop->o_clientupdate_state;
786 csnfge.f_next = sop->oq_search.rs_filter;
789 #if defined(LDAP_CLIENT_UPDATE) && defined(LDAP_SYNC)
793 if ( (sop->o_sync_mode & SLAP_SYNC_REFRESH) ||
794 ( IS_PSEARCH && sop->o_ps_protocol == LDAP_SYNC ))
796 cookief.f_choice = LDAP_FILTER_AND;
797 cookief.f_and = &csnfnot;
798 cookief.f_next = NULL;
800 csnfnot.f_choice = LDAP_FILTER_NOT;
801 csnfnot.f_not = &csnfeq;
802 csnfnot.f_next = &csnfand;
804 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
805 csnfeq.f_ava = &aa_eq;
806 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
807 csnfeq.f_av_value = sop->o_sync_state;
809 csnfand.f_choice = LDAP_FILTER_AND;
810 csnfand.f_and = &csnfge;
811 csnfand.f_next = NULL;
813 csnfge.f_choice = LDAP_FILTER_GE;
814 csnfge.f_ava = &aa_ge;
815 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
816 csnfge.f_av_value = sop->o_sync_state;
817 csnfge.f_next = sop->oq_search.rs_filter;
821 for ( id = bdb_idl_first( candidates, &cursor );
823 id = bdb_idl_next( candidates, &cursor ) )
828 /* check for abandon */
829 if ( sop->o_abandon ) {
834 #ifdef LDAP_EXOP_X_CANCEL
835 if ( sop->o_cancel ) {
836 assert( sop->o_cancel == SLAP_CANCEL_REQ );
837 rs->sr_err = LDAP_CANCELLED;
838 send_ldap_result( sop, rs );
839 sop->o_cancel = SLAP_CANCEL_ACK;
845 /* check time limit */
846 if ( sop->oq_search.rs_tlimit != -1 && slap_get_time() > stoptime ) {
847 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
848 rs->sr_ref = rs->sr_v2ref;
849 send_ldap_result( sop, rs );
854 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
858 /* get the entry with reader lock */
860 rs->sr_err = bdb_cache_find_entry_id( op->o_bd, NULL,
861 id, &ei, 0, locker, &lock, op->o_tmpmemctx );
863 if (rs->sr_err == LDAP_BUSY) {
864 rs->sr_text = "ldap server busy";
865 send_ldap_result( sop, rs );
868 } else if ( rs->sr_err == DB_LOCK_DEADLOCK
869 || rs->sr_err == DB_LOCK_NOTGRANTED )
874 if ( ei && rs->sr_err == 0 ) {
881 if( !BDB_IDL_IS_RANGE(candidates) ) {
882 /* only complain for non-range IDLs */
884 LDAP_LOG ( OPERATION, RESULTS,
885 "bdb_search: candidate %ld not found\n",
888 Debug( LDAP_DEBUG_TRACE,
889 "bdb_search: candidate %ld not found\n",
896 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
903 #ifdef BDB_SUBENTRIES
904 if ( is_entry_subentry( e ) ) {
905 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
906 if(!get_subentries_visibility( sop )) {
907 /* only subentries are visible */
911 } else if ( get_subentries( sop ) &&
912 !get_subentries_visibility( sop ))
914 /* only subentries are visible */
918 } else if ( get_subentries_visibility( sop )) {
919 /* only subentries are visible */
925 /* aliases were already dereferenced in candidate list */
926 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
927 /* but if the search base is an alias, and we didn't
928 * deref it when finding, return it.
930 if ( is_entry_alias(e) &&
931 ((sop->ors_deref & LDAP_DEREF_FINDING)
932 || !bvmatch(&e->e_nname, &op->o_req_ndn)))
937 /* scopes is only non-empty for onelevel or subtree */
938 if ( BDB_IDL_N(scopes) ) {
940 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
941 x = bdb_idl_search( scopes,
943 if ( scopes[x] == e->e_id )
946 /* subtree, walk up the tree */
947 EntryInfo *tmp = BEI(e);
948 for (;tmp->bei_parent;
949 tmp=tmp->bei_parent) {
951 scopes, tmp->bei_id );
952 if ( scopes[x] == tmp->bei_id ) {
962 * if it's a referral, add it to the list of referrals. only do
963 * this for non-base searches, and don't check the filter
964 * explicitly here since it's only a candidate anyway.
966 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE &&
967 is_entry_referral( e ) )
972 if ( !scopeok && sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
973 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
974 dnParent( &e->e_nname, &dn );
975 scopeok = dn_match( &dn, &realbase );
977 scopeok = (realbase.bv_len == 0);
981 && sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE )
983 scopeok = dnIsSuffix( &e->e_nname, &realbase );
990 BerVarray erefs = get_entry_referrals( sop, e );
991 rs->sr_ref = referral_rewrite( erefs,
993 sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE
997 send_search_reference( sop, rs );
999 ber_bvarray_free( rs->sr_ref );
1000 ber_bvarray_free( erefs );
1005 LDAP_LOG(OPERATION, DETAIL2,
1006 "bdb_search: candidate referral %ld scope not okay\n",
1009 Debug( LDAP_DEBUG_TRACE,
1010 "bdb_search: candidate referral %ld scope not okay\n",
1018 /* if it matches the filter and scope, send it */
1019 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1021 if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1022 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1024 rs->sr_err = LDAP_COMPARE_TRUE;
1027 #ifdef LDAP_CLIENT_UPDATE
1028 if ( sop->o_clientupdate_type & SLAP_LCUP_SYNC ) {
1029 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1033 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1034 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1035 rs->sr_err = test_filter( sop,
1036 rs->sr_entry, sop->oq_search.rs_filter );
1037 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1038 if ( rc_sync == LDAP_COMPARE_TRUE ) {
1039 entry_sync_state = LDAP_SYNC_ADD;
1041 entry_sync_state = LDAP_SYNC_PRESENT;
1048 rs->sr_err = test_filter( sop,
1049 rs->sr_entry, sop->oq_search.rs_filter );
1051 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1055 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1059 if ( !scopeok && sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
1060 if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
1061 scopeok = (realbase.bv_len == 0);
1063 dnParent( &e->e_nname, &dn );
1064 scopeok = dn_match( &dn, &realbase );
1067 } else if ( !scopeok &&
1068 sop->oq_search.rs_scope == LDAP_SCOPE_SUBTREE )
1070 scopeok = dnIsSuffix( &e->e_nname, &realbase );
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 scope not okay\n", (long) id, 0, 0);
1278 Debug( LDAP_DEBUG_TRACE,
1279 "bdb_search: %ld scope not okay\n",
1285 LDAP_LOG ( OPERATION, RESULTS,
1286 "bdb_search: %ld does not match filter\n", (long) id, 0, 0);
1288 Debug( LDAP_DEBUG_TRACE,
1289 "bdb_search: %ld does not match filter\n",
1296 /* free reader lock */
1298 bdb_cache_return_entry_r( bdb->bi_dbenv,
1299 &bdb->bi_cache, e , &lock);
1302 rs->sr_entry = NULL;
1305 ldap_pvt_thread_yield();
1309 #ifdef LDAP_CLIENT_UPDATE
1310 if ( sop->o_clientupdate_type & SLAP_LCUP_SYNC ) {
1311 bdb_build_lcup_done_ctrl( sop, rs, ctrls,
1312 num_ctrls++, &latest_entrycsn_bv );
1314 rs->sr_ctrls = ctrls;
1315 rs->sr_ref = rs->sr_v2ref;
1316 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1317 send_ldap_result( sop, rs );
1319 ch_free( latest_entrycsn_bv.bv_val );
1320 latest_entrycsn_bv.bv_val = NULL;
1322 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1323 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1325 ch_free( ctrls[--num_ctrls] );
1326 ctrls[num_ctrls] = NULL;
1330 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1331 if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1332 /* refreshAndPersist mode */
1333 rs->sr_err = LDAP_SUCCESS;
1334 rs->sr_rspoid = LDAP_SYNC_INFO;
1335 rs->sr_ctrls = NULL;
1336 bdb_send_ldap_intermediate( sop, rs,
1337 LDAP_SYNC_REFRESH_DONE, &latest_entrycsn_bv );
1339 /* refreshOnly mode */
1340 bdb_build_sync_done_ctrl( sop, rs, ctrls,
1341 num_ctrls++, 1, &latest_entrycsn_bv );
1342 rs->sr_ctrls = ctrls;
1343 rs->sr_ref = rs->sr_v2ref;
1344 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1345 send_ldap_result( sop, rs );
1346 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1347 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
1349 ch_free( ctrls[--num_ctrls] );
1350 ctrls[num_ctrls] = NULL;
1353 ch_free( latest_entrycsn_bv.bv_val );
1354 latest_entrycsn_bv.bv_val = NULL;
1358 rs->sr_ctrls = NULL;
1359 rs->sr_ref = rs->sr_v2ref;
1360 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1361 send_ldap_result( sop, rs );
1365 rs->sr_err = LDAP_SUCCESS;
1368 if( !IS_PSEARCH && e != NULL ) {
1369 /* free reader lock */
1370 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1373 LOCK_ID_FREE (bdb->bi_dbenv, locker );
1375 if( rs->sr_v2ref ) {
1376 ber_bvarray_free( rs->sr_v2ref );
1377 rs->sr_v2ref = NULL;
1379 if( realbase.bv_val ) ch_free( realbase.bv_val );
1385 static int base_candidate(
1391 LDAP_LOG ( OPERATION, ENTRY,
1392 "base_candidate: base: \"%s\" (0x%08lx)\n",
1393 e->e_dn, (long) e->e_id, 0);
1395 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1396 e->e_dn, (long) e->e_id, 0);
1404 /* Look for "objectClass Present" in this filter.
1405 * Also count depth of filter tree while we're at it.
1407 static int oc_filter(
1415 if( cur > *max ) *max = cur;
1417 switch(f->f_choice) {
1418 case LDAP_FILTER_PRESENT:
1419 if (f->f_desc == slap_schema.si_ad_objectClass) {
1424 case LDAP_FILTER_AND:
1425 case LDAP_FILTER_OR:
1427 for (f=f->f_and; f; f=f->f_next) {
1428 (void) oc_filter(f, cur, max);
1438 static void search_stack_free( void *key, void *data)
1443 static void *search_stack(
1447 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1450 if ( op->o_threadctx ) {
1451 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1454 ret = bdb->bi_search_stack;
1458 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1460 if ( op->o_threadctx ) {
1461 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1462 ret, search_stack_free );
1464 bdb->bi_search_stack = ret;
1470 static int search_candidates(
1479 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1481 Filter f, scopef, rf, xf, nf;
1483 AttributeAssertion aa_ref;
1484 #ifdef BDB_SUBENTRIES
1486 AttributeAssertion aa_subentry;
1490 * This routine takes as input a filter (user-filter)
1491 * and rewrites it as follows:
1492 * (&(scope=DN)[(objectClass=subentry)]
1493 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1497 LDAP_LOG ( OPERATION, ENTRY,
1498 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1499 e->e_dn, (long) e->e_id, op->oq_search.rs_scope);
1501 Debug(LDAP_DEBUG_TRACE,
1502 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1503 e->e_dn, (long) e->e_id, op->oq_search.rs_scope );
1506 xf.f_or = op->oq_search.rs_filter;
1507 xf.f_choice = LDAP_FILTER_OR;
1510 /* If the user's filter uses objectClass=*,
1511 * these clauses are redundant.
1513 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1514 && !get_subentries_visibility(op) )
1516 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1517 /* match referral objects */
1518 struct berval bv_ref = { sizeof("referral")-1, "referral" };
1519 rf.f_choice = LDAP_FILTER_EQUALITY;
1521 rf.f_av_desc = slap_schema.si_ad_objectClass;
1522 rf.f_av_value = bv_ref;
1523 rf.f_next = xf.f_or;
1529 scopef.f_choice = op->oq_search.rs_scope == LDAP_SCOPE_SUBTREE
1530 ? SLAPD_FILTER_DN_SUBTREE
1531 : SLAPD_FILTER_DN_ONE;
1532 scopef.f_dn = &e->e_nname;
1533 scopef.f_next = NULL;
1536 f.f_choice = LDAP_FILTER_AND;
1538 /* Dummy; we compute scope separately now */
1539 nf.f_choice = LDAP_FILTER_NOT;
1540 nf.f_next = xf.f_or == op->oq_search.rs_filter
1541 ? op->oq_search.rs_filter : &xf ;
1542 /* Filter depth increased again, adding dummy clause */
1545 #ifdef BDB_SUBENTRIES
1546 if( get_subentries_visibility( op ) ) {
1547 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
1548 sf.f_choice = LDAP_FILTER_EQUALITY;
1549 sf.f_ava = &aa_subentry;
1550 sf.f_av_desc = slap_schema.si_ad_objectClass;
1551 sf.f_av_value = bv_subentry;
1552 sf.f_next = scopef.f_next;
1557 /* Allocate IDL stack, plus 1 more for former tmp */
1558 if ( depth+1 > bdb->bi_search_stack_depth ) {
1559 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1561 stack = search_stack( stackop );
1564 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1565 rc = search_aliases( op, rs, e, locker, &scopef, ids, scopes, stack );
1567 rc = bdb_filter_candidates( op, &scopef, ids,
1568 stack, stack+BDB_IDL_UM_SIZE );
1571 if ( rc == LDAP_SUCCESS ) {
1572 rc = bdb_filter_candidates( op, &f, ids,
1573 stack, stack+BDB_IDL_UM_SIZE );
1576 if ( depth+1 > bdb->bi_search_stack_depth ) {
1582 LDAP_LOG ( OPERATION, DETAIL1,
1583 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
1585 Debug(LDAP_DEBUG_TRACE,
1586 "bdb_search_candidates: failed (rc=%d)\n",
1592 LDAP_LOG ( OPERATION, DETAIL1,
1593 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1594 (long) ids[0], (long) BDB_IDL_FIRST(ids),
1595 (long) BDB_IDL_LAST(ids));
1597 Debug(LDAP_DEBUG_TRACE,
1598 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1600 (long) BDB_IDL_FIRST(ids),
1601 (long) BDB_IDL_LAST(ids) );
1608 #ifdef LDAP_CONTROL_PAGEDRESULTS
1610 send_pagerequest_response(
1616 LDAPControl ctrl, *ctrls[2];
1617 char berbuf[LBER_ELEMENT_SIZEOF];
1618 BerElement *ber = (BerElement *)berbuf;
1619 struct berval cookie = { 0, NULL };
1620 PagedResultsCookie respcookie;
1623 LDAP_LOG ( OPERATION, ENTRY,
1624 "send_pagerequest_response: lastid: (0x%08lx) "
1625 "nentries: (0x%081x)\n",
1626 lastid, rs->sr_nentries, NULL );
1628 Debug(LDAP_DEBUG_ARGS, "send_pagerequest_response: lastid: (0x%08lx) "
1629 "nentries: (0x%081x)\n", lastid, rs->sr_nentries, NULL );
1632 ctrl.ldctl_value.bv_val = NULL;
1636 ber_init2( ber, NULL, LBER_USE_DER );
1638 respcookie = ( PagedResultsCookie )lastid;
1639 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1640 cookie.bv_len = sizeof( respcookie );
1641 cookie.bv_val = (char *)&respcookie;
1644 * FIXME: we should consider sending an estimate of the entries
1645 * left, after appropriate security check is done
1647 ber_printf( ber, "{iO}", tentries, &cookie );
1649 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1653 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1654 ctrls[0]->ldctl_iscritical = 0;
1656 rs->sr_ctrls = ctrls;
1657 rs->sr_err = LDAP_SUCCESS;
1658 send_ldap_result( op, rs );
1661 (void) ber_free_buf( ber );
1665 #ifdef LDAP_CLIENT_UPDATE
1667 bdb_build_lcup_update_ctrl(
1672 LDAPControl **ctrls,
1674 struct berval *latest_entrycsn_bv,
1680 const char *text = NULL;
1682 char berbuf[LBER_ELEMENT_SIZEOF];
1683 BerElement *ber = (BerElement *)berbuf;
1685 struct berval entrycsn_bv = { 0, NULL };
1687 ber_init2( ber, 0, LBER_USE_DER );
1689 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1691 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
1692 AttributeDescription *desc = a->a_desc;
1693 if ( desc == slap_schema.si_ad_entryCSN ) {
1694 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
1695 if ( latest_entrycsn_bv->bv_val == NULL ) {
1696 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1698 res = value_match( &ret, desc,
1699 desc->ad_type->sat_ordering, 0,
1700 &entrycsn_bv, latest_entrycsn_bv, &text );
1701 if ( res != LDAP_SUCCESS ) {
1704 LDAP_LOG ( OPERATION, RESULTS,
1705 "bdb_search: value_match failed\n",
1708 Debug( LDAP_DEBUG_TRACE,
1709 "bdb_search: value_match failed\n",
1715 ch_free( latest_entrycsn_bv->bv_val );
1716 latest_entrycsn_bv->bv_val = NULL;
1717 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1723 if ( entry_count % op->o_clientupdate_interval == 0 ) {
1726 SLAP_LCUP_STATE_UPDATE_FALSE,
1728 LDAP_CUP_COOKIE_OID, &entrycsn_bv );
1729 } else { /* Do not send cookie */
1732 SLAP_LCUP_STATE_UPDATE_FALSE,
1736 ch_free( entrycsn_bv.bv_val );
1737 entrycsn_bv.bv_val = NULL;
1739 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_ENTRY_UPDATE;
1740 ctrls[num_ctrls]->ldctl_iscritical = op->o_clientupdate;
1741 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1743 ber_free_buf( ber );
1747 LDAP_LOG ( OPERATION, RESULTS,
1748 "bdb_build_lcup_ctrl: ber_flatten2 failed\n",
1751 Debug( LDAP_DEBUG_TRACE,
1752 "bdb_build_lcup_ctrl: ber_flatten2 failed\n",
1755 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1759 return LDAP_SUCCESS;
1763 bdb_build_lcup_done_ctrl(
1766 LDAPControl **ctrls,
1768 struct berval *latest_entrycsn_bv )
1771 char berbuf[LBER_ELEMENT_SIZEOF];
1772 BerElement *ber = (BerElement *)berbuf;
1774 ber_init2( ber, NULL, LBER_USE_DER );
1776 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1778 ber_printf( ber, "{sON}", LDAP_CUP_COOKIE_OID, latest_entrycsn_bv );
1780 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_CLIENT_UPDATE_DONE;
1781 ctrls[num_ctrls]->ldctl_iscritical = op->o_clientupdate;
1782 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1784 ber_free_buf( ber );
1788 LDAP_LOG ( OPERATION, RESULTS,
1789 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n", 0, 0, 0 );
1791 Debug( LDAP_DEBUG_TRACE,
1792 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1795 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1799 return LDAP_SUCCESS;
1805 bdb_build_sync_state_ctrl(
1809 int entry_sync_state,
1810 LDAPControl **ctrls,
1813 struct berval *latest_entrycsn_bv )
1818 const char *text = NULL;
1820 char berbuf[LBER_ELEMENT_SIZEOF];
1821 BerElement *ber = (BerElement *)berbuf;
1823 struct berval entryuuid_bv = { 0, NULL };
1824 struct berval entrycsn_bv = { 0, NULL };
1826 ber_init2( ber, 0, LBER_USE_DER );
1828 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1830 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
1831 AttributeDescription *desc = a->a_desc;
1832 if ( desc == slap_schema.si_ad_entryCSN ) {
1833 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
1834 if ( latest_entrycsn_bv->bv_val == NULL ) {
1835 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1837 res = value_match( &ret, desc,
1838 desc->ad_type->sat_ordering, 0,
1839 &entrycsn_bv, latest_entrycsn_bv, &text );
1840 if ( res != LDAP_SUCCESS ) {
1843 LDAP_LOG ( OPERATION, RESULTS,
1844 "bdb_search: value_match failed\n",
1847 Debug( LDAP_DEBUG_TRACE,
1848 "bdb_search: value_match failed\n",
1853 ch_free( latest_entrycsn_bv->bv_val );
1854 latest_entrycsn_bv->bv_val = NULL;
1855 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1858 } else if ( desc == slap_schema.si_ad_entryUUID ) {
1859 ber_dupbv( &entryuuid_bv, &a->a_vals[0] );
1863 if ( send_cookie ) {
1864 ber_printf( ber, "{eOON}",
1865 entry_sync_state, &entryuuid_bv, &entrycsn_bv );
1867 ber_printf( ber, "{eON}",
1868 entry_sync_state, &entryuuid_bv );
1871 ch_free( entrycsn_bv.bv_val );
1872 entrycsn_bv.bv_val = NULL;
1873 ch_free( entryuuid_bv.bv_val );
1874 entryuuid_bv.bv_val = NULL;
1876 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
1877 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
1878 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1880 ber_free_buf( ber );
1884 LDAP_LOG ( OPERATION, RESULTS,
1885 "bdb_build_sync_ctrl: ber_flatten2 failed\n",
1888 Debug( LDAP_DEBUG_TRACE,
1889 "bdb_build_sync_ctrl: ber_flatten2 failed\n",
1892 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1896 return LDAP_SUCCESS;
1900 bdb_build_sync_done_ctrl(
1903 LDAPControl **ctrls,
1906 struct berval *latest_entrycsn_bv )
1909 char berbuf[LBER_ELEMENT_SIZEOF];
1910 BerElement *ber = (BerElement *)berbuf;
1912 ber_init2( ber, NULL, LBER_USE_DER );
1914 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1916 if ( send_cookie ) {
1917 ber_printf( ber, "{ON}", latest_entrycsn_bv );
1919 ber_printf( ber, "{N}" );
1922 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
1923 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
1924 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1926 ber_free_buf( ber );
1930 LDAP_LOG ( OPERATION, RESULTS,
1931 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1934 Debug( LDAP_DEBUG_TRACE,
1935 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1938 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1942 return LDAP_SUCCESS;
1946 bdb_send_ldap_intermediate(
1950 struct berval *cookie )
1952 char berbuf[LBER_ELEMENT_SIZEOF];
1953 BerElement *ber = (BerElement *)berbuf;
1954 struct berval rspdata;
1958 ber_init2( ber, NULL, LBER_USE_DER );
1960 if ( cookie == NULL ) {
1961 ber_printf( ber, "{eN}", state );
1963 ber_printf( ber, "{eON}", state, cookie );
1966 ret = ber_flatten2( ber, &rspdata, 0 );
1970 LDAP_LOG ( OPERATION, RESULTS,
1971 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1974 Debug( LDAP_DEBUG_TRACE,
1975 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1978 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
1982 rs->sr_rspdata = &rspdata;
1983 send_ldap_intermediate_resp( op, rs );
1984 rs->sr_rspdata = NULL;
1985 ber_free_buf( ber );
1987 return LDAP_SUCCESS;