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(
29 static void send_pagerequest_response(
48 struct berval *filterstr,
52 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
54 const char *text = NULL;
57 ID candidates[BDB_IDL_UM_SIZE];
59 BerVarray v2refs = NULL;
60 Entry *matched = NULL;
61 struct berval realbase = { 0, NULL };
67 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
68 Filter cookief, csnfnot, csnfeq, csnfand, csnfge;
69 AttributeAssertion aa_ge, aa_eq;
71 struct berval latest_entrycsn_bv = { 0, NULL };
72 LDAPControl *ctrls[SLAP_SEARCH_MAX_CTRLS];
79 AttributeName null_attr;
82 struct slap_limits_set *limit = NULL;
87 struct bdb_op_info opinfo;
90 LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
92 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
96 #ifdef LDAP_CLIENT_UPDATE
97 if ( op->o_clientupdate_type & SLAP_LCUP_PERSIST ) {
98 bdb_add_psearch_spec( be, conn, op, base, base, scope, deref, slimit,
99 tlimit, filter, filterstr, attrs, attrsonly, LDAP_CLIENT_UPDATE );
103 #if defined(LDAP_CLIENT_UPDATE) && defined(LDAP_SYNC)
107 /* psearch needs to be registered before refresh begins */
108 /* psearch and refresh transmission is serialized in send_ldap_ber() */
109 if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
110 bdb_add_psearch_spec( be, conn, op, base, base, scope, deref, slimit,
111 tlimit, filter, filterstr, attrs, attrsonly, LDAP_SYNC );
113 null_attr.an_desc = NULL;
114 null_attr.an_oc = NULL;
115 null_attr.an_name.bv_len = 0;
116 null_attr.an_name.bv_val = NULL;
119 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
120 for ( num_ctrls = 0; num_ctrls < SLAP_SEARCH_MAX_CTRLS; num_ctrls++ )
121 ctrls[num_ctrls] = NULL;
126 manageDSAit = get_manageDSAit( op );
128 rc = LOCK_ID (bdb->bi_dbenv, &locker );
134 send_ldap_result( conn, op, rc=LDAP_OTHER,
135 NULL, "internal error", NULL, NULL );
140 opinfo.boi_txn = NULL;
141 opinfo.boi_locker = locker;
143 op->o_private = &opinfo;
145 if ( nbase->bv_len == 0 ) {
146 /* DIT root special case */
147 e = (Entry *) &slap_entry_root;
151 /* get entry with reader lock */
152 if ( deref & LDAP_DEREF_FINDING ) {
153 e = deref_dn_r( be, nbase-, &err, &matched, &text );
159 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
168 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
170 if (matched != NULL) {
171 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
173 send_ldap_result( conn, op, LDAP_BUSY,
174 NULL, "ldap server busy", NULL, NULL );
175 LOCK_ID_FREE (bdb->bi_dbenv, locker );
177 case DB_LOCK_DEADLOCK:
178 case DB_LOCK_NOTGRANTED:
182 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
184 if (matched != NULL) {
185 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
187 send_ldap_result( conn, op, rc=LDAP_OTHER,
188 NULL, "internal error", NULL, NULL );
189 LOCK_ID_FREE (bdb->bi_dbenv, locker );
194 struct berval matched_dn = { 0, NULL };
195 BerVarray refs = NULL;
197 if ( matched != NULL ) {
199 ber_dupbv( &matched_dn, &matched->e_name );
201 erefs = is_entry_referral( matched )
202 ? get_entry_referrals( be, conn, op, matched )
205 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
209 refs = referral_rewrite( erefs, &matched_dn,
211 ber_bvarray_free( erefs );
215 refs = referral_rewrite( default_referral,
219 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
220 matched_dn.bv_val, text, refs, NULL );
222 LOCK_ID_FREE (bdb->bi_dbenv, locker );
223 if ( refs ) ber_bvarray_free( refs );
224 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
228 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
229 /* entry is a referral, don't allow add */
230 struct berval matched_dn;
231 BerVarray erefs, refs;
233 ber_dupbv( &matched_dn, &e->e_name );
234 erefs = get_entry_referrals( be, conn, op, e );
237 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
241 refs = referral_rewrite( erefs, &matched_dn,
243 ber_bvarray_free( erefs );
247 LDAP_LOG ( OPERATION, RESULTS,
248 "bdb_search: entry is referral\n", 0, 0, 0 );
250 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
254 send_ldap_result( conn, op, LDAP_REFERRAL,
256 refs ? NULL : "bad referral object",
259 LOCK_ID_FREE (bdb->bi_dbenv, locker );
260 ber_bvarray_free( refs );
261 ber_memfree( matched_dn.bv_val );
265 /* if not root, get appropriate limits */
266 if ( be_isroot( be, &op->o_ndn ) ) {
269 ( void ) get_limits( be, &op->o_ndn, &limit );
272 /* The time/size limits come first because they require very little
273 * effort, so there's no chance the candidates are selected and then
274 * the request is not honored only because of time/size constraints */
276 /* if no time limit requested, use soft limit (unless root!) */
279 tlimit = -1; /* allow root to set no limit */
287 /* if no limit is required, use soft limit */
289 tlimit = limit->lms_t_soft;
291 /* if requested limit higher than hard limit, abort */
292 } else if ( tlimit > limit->lms_t_hard ) {
293 /* no hard limit means use soft instead */
294 if ( limit->lms_t_hard == 0
295 && limit->lms_t_soft > -1
296 && tlimit > limit->lms_t_soft ) {
297 tlimit = limit->lms_t_soft;
299 /* positive hard limit means abort */
300 } else if ( limit->lms_t_hard > 0 ) {
301 send_search_result( conn, op,
302 LDAP_ADMINLIMIT_EXCEEDED,
303 NULL, NULL, NULL, NULL, 0 );
308 /* negative hard limit means no limit */
311 /* if no limit is required, use soft limit */
313 if ( get_pagedresults(op) && limit->lms_s_pr != 0 ) {
314 slimit = limit->lms_s_pr;
316 slimit = limit->lms_s_soft;
319 /* if requested limit higher than hard limit, abort */
320 } else if ( slimit > limit->lms_s_hard ) {
321 /* no hard limit means use soft instead */
322 if ( limit->lms_s_hard == 0
323 && limit->lms_s_soft > -1
324 && slimit > limit->lms_s_soft ) {
325 slimit = limit->lms_s_soft;
327 /* positive hard limit means abort */
328 } else if ( limit->lms_s_hard > 0 ) {
329 send_search_result( conn, op,
330 LDAP_ADMINLIMIT_EXCEEDED,
331 NULL, NULL, NULL, NULL, 0 );
336 /* negative hard limit means no limit */
340 /* compute it anyway; root does not use it */
341 stoptime = op->o_time + tlimit;
343 /* select candidates */
344 if ( scope == LDAP_SCOPE_BASE ) {
345 rc = base_candidate( be, e, candidates );
348 BDB_IDL_ALL( bdb, candidates );
349 rc = search_candidates( be, op, e, filter,
350 scope, deref, candidates );
353 /* need normalized dn below */
354 ber_dupbv( &realbase, &e->e_nname );
356 /* start cursor at beginning of candidates.
360 if ( e != &slap_entry_root ) {
361 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
365 if ( candidates[0] == 0 ) {
367 LDAP_LOG ( OPERATION, RESULTS,
368 "bdb_search: no candidates\n", 0, 0, 0 );
370 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
374 send_search_result( conn, op,
376 NULL, NULL, NULL, NULL, 0 );
382 /* if not root and candidates exceed to-be-checked entries, abort */
383 if ( !isroot && limit->lms_s_unchecked != -1 ) {
384 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
385 send_search_result( conn, op,
386 LDAP_ADMINLIMIT_EXCEEDED,
387 NULL, NULL, NULL, NULL, 0 );
393 if ( isroot || !limit->lms_s_pr_hide ) {
394 tentries = BDB_IDL_N(candidates);
397 #ifdef LDAP_CONTROL_PAGEDRESULTS
398 if ( get_pagedresults(op) ) {
399 if ( op->o_pagedresults_state.ps_cookie == 0 ) {
402 if ( op->o_pagedresults_size == 0 ) {
403 send_search_result( conn, op, LDAP_SUCCESS,
404 NULL, "search abandoned by pagedResult size=0",
408 for ( id = bdb_idl_first( candidates, &cursor );
409 id != NOID && id <= (ID)( op->o_pagedresults_state.ps_cookie );
410 id = bdb_idl_next( candidates, &cursor ) );
412 if ( cursor == NOID ) {
414 LDAP_LOG ( OPERATION, RESULTS,
415 "bdb_search: no paged results candidates\n",
418 Debug( LDAP_DEBUG_TRACE,
419 "bdb_search: no paged results candidates\n",
422 send_pagerequest_response( conn, op, lastid, 0, 0 );
431 #ifdef LDAP_CLIENT_UPDATE
432 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
433 cookief.f_choice = LDAP_FILTER_AND;
434 cookief.f_and = &csnfnot;
435 cookief.f_next = NULL;
437 csnfnot.f_choice = LDAP_FILTER_NOT;
438 csnfnot.f_not = &csnfeq;
439 csnfnot.f_next = &csnfand;
441 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
442 csnfeq.f_ava = &aa_eq;
443 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
444 ber_dupbv( &csnfeq.f_av_value, &op->o_clientupdate_state );
446 csnfand.f_choice = LDAP_FILTER_AND;
447 csnfand.f_and = &csnfge;
448 csnfand.f_next = NULL;
450 csnfge.f_choice = LDAP_FILTER_GE;
451 csnfge.f_ava = &aa_ge;
452 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
453 ber_dupbv( &csnfge.f_av_value, &op->o_clientupdate_state );
454 csnfge.f_next = filter;
457 #if defined(LDAP_CLIENT_UPDATE) && defined(LDAP_SYNC)
461 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
462 cookief.f_choice = LDAP_FILTER_AND;
463 cookief.f_and = &csnfnot;
464 cookief.f_next = NULL;
466 csnfnot.f_choice = LDAP_FILTER_NOT;
467 csnfnot.f_not = &csnfeq;
468 csnfnot.f_next = &csnfand;
470 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
471 csnfeq.f_ava = &aa_eq;
472 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
473 ber_dupbv( &csnfeq.f_av_value, &op->o_sync_state );
475 csnfand.f_choice = LDAP_FILTER_AND;
476 csnfand.f_and = &csnfge;
477 csnfand.f_next = NULL;
479 csnfge.f_choice = LDAP_FILTER_GE;
480 csnfge.f_ava = &aa_ge;
481 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
482 ber_dupbv( &csnfge.f_av_value, &op->o_sync_state );
483 csnfge.f_next = filter;
487 for ( id = bdb_idl_first( candidates, &cursor );
489 id = bdb_idl_next( candidates, &cursor ) )
495 /* check for abandon */
496 if ( op->o_abandon ) {
501 #ifdef LDAP_EXOP_X_CANCEL
502 if ( op->o_cancel ) {
503 assert( op->o_cancel == LDAP_CANCEL_REQ );
505 send_search_result( conn, op, LDAP_CANCELLED,
506 NULL, NULL, NULL, NULL, 0 );
507 op->o_cancel = LDAP_CANCEL_ACK;
512 /* check time limit */
513 if ( tlimit != -1 && slap_get_time() > stoptime ) {
514 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
515 NULL, NULL, v2refs, NULL, nentries );
520 /* get the entry with reader lock */
521 rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
523 if (rc == LDAP_BUSY) {
524 send_ldap_result( conn, op, rc=LDAP_BUSY,
525 NULL, "ldap server busy", NULL, NULL );
528 } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
533 if( !BDB_IDL_IS_RANGE(candidates) ) {
534 /* only complain for non-range IDLs */
536 LDAP_LOG ( OPERATION, RESULTS,
537 "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
539 Debug( LDAP_DEBUG_TRACE,
540 "bdb_search: candidate %ld not found\n",
548 #ifdef BDB_SUBENTRIES
549 if ( is_entry_subentry( e ) ) {
550 if( scope != LDAP_SCOPE_BASE ) {
551 if(!get_subentries_visibility( op )) {
552 /* only subentries are visible */
556 } else if ( get_subentries( op ) &&
557 !get_subentries_visibility( op ))
559 /* only subentries are visible */
563 } else if ( get_subentries_visibility( op )) {
564 /* only subentries are visible */
570 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
575 e = deref_entry_r( be, e, &err, &matched, &text );
582 if( e->e_id == id ) {
587 /* need to skip alias which deref into scope */
588 if( scope & LDAP_SCOPE_ONELEVEL ) {
591 dnParent( &e->e_nname, &pdn ):
592 if ( ber_bvcmp( pdn, &realbase ) ) {
596 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
597 /* alias is within scope */
599 LDAP_LOG ( OPERATION, RESULTS,
600 "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
602 Debug( LDAP_DEBUG_TRACE,
603 "bdb_search: \"%s\" in subtree\n",
614 * if it's a referral, add it to the list of referrals. only do
615 * this for non-base searches, and don't check the filter
616 * explicitly here since it's only a candidate anyway.
618 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
619 is_entry_referral( e ) )
624 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
625 if ( !be_issuffix( be, &e->e_nname ) ) {
626 dnParent( &e->e_nname, &dn );
627 scopeok = dn_match( &dn, &realbase );
629 scopeok = (realbase.bv_len == 0);
632 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
633 scopeok = dnIsSuffix( &e->e_nname, &realbase );
640 BerVarray erefs = get_entry_referrals(
642 BerVarray refs = referral_rewrite( erefs,
644 scope == LDAP_SCOPE_SUBTREE
648 send_search_reference( be, conn, op,
649 e, refs, NULL, &v2refs );
651 ber_bvarray_free( refs );
655 LDAP_LOG(OPERATION, DETAIL2,
656 "bdb_search: candidate referral %ld scope not okay\n",
659 Debug( LDAP_DEBUG_TRACE,
660 "bdb_search: candidate referral %ld scope not okay\n",
668 /* if it matches the filter and scope, send it */
669 #ifdef LDAP_CLIENT_UPDATE
670 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
671 rc = test_filter( be, conn, op, e, &cookief );
675 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
676 rc_sync = test_filter( be, conn, op, e, &cookief );
677 rc = test_filter( be, conn, op, e, filter );
678 if ( rc == LDAP_COMPARE_TRUE ) {
679 if ( rc_sync == LDAP_COMPARE_TRUE ) {
680 entry_sync_state = LDAP_SYNC_ADD;
682 entry_sync_state = LDAP_SYNC_PRESENT;
688 rc = test_filter( be, conn, op, e, filter );
691 if ( rc == LDAP_COMPARE_TRUE ) {
695 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
696 if ( be_issuffix( be, &e->e_nname ) ) {
697 scopeok = (realbase.bv_len == 0);
699 dnParent( &e->e_nname, &dn );
700 scopeok = dn_match( &dn, &realbase );
703 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
704 scopeok = dnIsSuffix( &e->e_nname, &realbase );
711 /* check size limit */
712 if ( --slimit == -1 ) {
713 bdb_cache_return_entry_r( bdb->bi_dbenv,
714 &bdb->bi_cache, e, &lock );
716 send_search_result( conn, op,
717 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
718 v2refs, NULL, nentries );
722 #ifdef LDAP_CONTROL_PAGEDRESULTS
723 if ( get_pagedresults(op) ) {
724 if ( nentries >= op->o_pagedresults_size ) {
725 send_pagerequest_response( conn, op,
726 lastid, nentries, tentries );
736 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
742 #ifdef LDAP_CLIENT_UPDATE
743 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
744 rc = bdb_build_lcup_update_ctrl( conn, op, e, ++entry_count, ctrls,
745 num_ctrls++, &latest_entrycsn_bv, SLAP_LCUP_ENTRY_DELETED_FALSE );
746 if ( rc != LDAP_SUCCESS )
748 result = send_search_entry( be, conn, op,
749 e, attrs, attrsonly, ctrls);
751 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL )
752 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
753 ch_free( ctrls[--num_ctrls] );
754 ctrls[num_ctrls] = NULL;
758 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
759 rc = bdb_build_sync_state_ctrl( conn, op, e, entry_sync_state, ctrls,
760 num_ctrls++, 0, &latest_entrycsn_bv );
761 if ( rc != LDAP_SUCCESS )
764 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
765 result = send_search_entry( be, conn, op,
766 e, attrs, attrsonly, ctrls);
767 } else { /* PRESENT */
768 result = send_search_entry( be, conn, op,
769 e, &null_attr, attrsonly, ctrls);
772 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL )
773 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
774 ch_free( ctrls[--num_ctrls] );
775 ctrls[num_ctrls] = NULL;
780 result = send_search_entry( be, conn, op,
781 e, attrs, attrsonly, NULL);
786 case 0: /* entry sent ok */
789 case 1: /* entry not sent */
791 case -1: /* connection closed */
792 bdb_cache_return_entry_r(bdb->bi_dbenv,
793 &bdb->bi_cache, e, &lock);
801 LDAP_LOG ( OPERATION, RESULTS,
802 "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
804 Debug( LDAP_DEBUG_TRACE,
805 "bdb_search: %ld scope not okay\n",
811 LDAP_LOG ( OPERATION, RESULTS,
812 "bdb_search: %ld does match filter\n", (long) id, 0, 0);
814 Debug( LDAP_DEBUG_TRACE,
815 "bdb_search: %ld does match filter\n",
822 /* free reader lock */
823 bdb_cache_return_entry_r( bdb->bi_dbenv,
824 &bdb->bi_cache, e , &lock);
828 ldap_pvt_thread_yield();
831 #ifdef LDAP_CLIENT_UPDATE
832 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
833 bdb_build_lcup_done_ctrl( conn, op, ctrls, num_ctrls++, &latest_entrycsn_bv );
835 send_search_result( conn, op,
836 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
837 NULL, NULL, v2refs, ctrls, nentries );
839 ch_free( latest_entrycsn_bv.bv_val );
840 latest_entrycsn_bv.bv_val = NULL;
842 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL )
843 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
844 ch_free( ctrls[--num_ctrls] );
845 ctrls[num_ctrls] = NULL;
849 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
850 if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
851 /* refreshAndPersist mode */
852 bdb_send_ldap_intermediate( conn, op,
853 LDAP_SUCCESS, NULL, NULL, NULL, LDAP_SYNC_INFO,
854 LDAP_SYNC_REFRESH_DONE, &latest_entrycsn_bv, NULL );
856 /* refreshOnly mode */
857 bdb_build_sync_done_ctrl( conn, op, ctrls, num_ctrls++, 1, &latest_entrycsn_bv );
858 send_search_result( conn, op,
859 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
860 NULL, NULL, v2refs, ctrls, nentries );
861 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL )
862 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
863 ch_free( ctrls[--num_ctrls] );
864 ctrls[num_ctrls] = NULL;
867 ch_free( latest_entrycsn_bv.bv_val );
868 latest_entrycsn_bv.bv_val = NULL;
872 send_search_result( conn, op,
873 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
874 NULL, NULL, v2refs, NULL, nentries );
881 /* free reader lock */
882 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
885 #ifdef LDAP_CLIENT_UPDATE
886 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
887 if ( csnfeq.f_ava != NULL && csnfeq.f_av_value.bv_val != NULL ) {
888 ch_free( csnfeq.f_av_value.bv_val );
891 if ( csnfge.f_ava != NULL && csnfge.f_av_value.bv_val != NULL ) {
892 ch_free( csnfge.f_av_value.bv_val );
896 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
900 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
901 if ( csnfeq.f_ava != NULL && csnfeq.f_av_value.bv_val != NULL ) {
902 ch_free( csnfeq.f_av_value.bv_val );
905 if ( csnfge.f_ava != NULL && csnfge.f_av_value.bv_val != NULL ) {
906 ch_free( csnfge.f_av_value.bv_val );
911 LOCK_ID_FREE (bdb->bi_dbenv, locker );
913 if( v2refs ) ber_bvarray_free( v2refs );
914 if( realbase.bv_val ) ch_free( realbase.bv_val );
920 static int base_candidate(
926 LDAP_LOG ( OPERATION, ENTRY,
927 "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
929 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
930 e->e_dn, (long) e->e_id, 0);
938 /* Look for "objectClass Present" in this filter.
939 * Also count depth of filter tree while we're at it.
941 static int oc_filter(
949 if( cur > *max ) *max = cur;
951 switch(f->f_choice) {
952 case LDAP_FILTER_PRESENT:
953 if (f->f_desc == slap_schema.si_ad_objectClass) {
958 case LDAP_FILTER_AND:
961 for (f=f->f_and; f; f=f->f_next) {
962 (void) oc_filter(f, cur, max);
972 static void search_stack_free( void *key, void *data)
977 static void *search_stack(
982 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
985 if ( op->o_threadctx ) {
986 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
989 ret = bdb->bi_search_stack;
993 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE * sizeof( ID ) );
994 if ( op->o_threadctx ) {
995 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
996 ret, search_stack_free );
998 bdb->bi_search_stack = ret;
1004 static int search_candidates(
1013 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
1015 Filter f, scopef, rf, xf;
1017 AttributeAssertion aa_ref;
1018 #ifdef BDB_SUBENTRIES
1020 AttributeAssertion aa_subentry;
1024 AttributeAssertion aa_alias;
1028 * This routine takes as input a filter (user-filter)
1029 * and rewrites it as follows:
1030 * (&(scope=DN)[(objectClass=subentry)]
1031 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1035 LDAP_LOG ( OPERATION, ENTRY,
1036 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1037 e->e_dn, (long) e->e_id, scope);
1039 Debug(LDAP_DEBUG_TRACE,
1040 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1041 e->e_dn, (long) e->e_id, scope );
1045 xf.f_choice = LDAP_FILTER_OR;
1048 /* If the user's filter uses objectClass=*,
1049 * these clauses are redundant.
1051 if (!oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
1052 if( !get_manageDSAit(op) ) { /* match referrals */
1053 struct berval bv_ref = { sizeof("referral")-1, "referral" };
1054 rf.f_choice = LDAP_FILTER_EQUALITY;
1056 rf.f_av_desc = slap_schema.si_ad_objectClass;
1057 rf.f_av_value = bv_ref;
1058 rf.f_next = xf.f_or;
1063 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
1064 struct berval bv_alias = { sizeof("alias")-1, "alias" };
1065 af.f_choice = LDAP_FILTER_EQUALITY;
1066 af.f_ava = &aa_alias;
1067 af.f_av_desc = slap_schema.si_ad_objectClass;
1068 af.f_av_value = bv_alias;
1069 af.f_next = xf.f_or;
1073 /* We added one of these clauses, filter depth increased */
1074 if( xf.f_or != filter ) depth++;
1078 f.f_choice = LDAP_FILTER_AND;
1080 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
1081 ? SLAPD_FILTER_DN_SUBTREE
1082 : SLAPD_FILTER_DN_ONE;
1083 scopef.f_dn = &e->e_nname;
1084 scopef.f_next = xf.f_or == filter ? filter : &xf ;
1085 /* Filter depth increased again, adding scope clause */
1088 #ifdef BDB_SUBENTRIES
1089 if( get_subentries_visibility( op ) ) {
1090 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
1091 sf.f_choice = LDAP_FILTER_EQUALITY;
1092 sf.f_ava = &aa_subentry;
1093 sf.f_av_desc = slap_schema.si_ad_objectClass;
1094 sf.f_av_value = bv_subentry;
1095 sf.f_next = scopef.f_next;
1096 scopef.f_next = &sf;
1100 /* Allocate IDL stack, plus 1 more for former tmp */
1101 if ( depth+1 > bdb->bi_search_stack_depth ) {
1102 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1104 stack = search_stack( be, op );
1107 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
1109 if ( depth+1 > bdb->bi_search_stack_depth ) {
1115 LDAP_LOG ( OPERATION, DETAIL1,
1116 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
1118 Debug(LDAP_DEBUG_TRACE,
1119 "bdb_search_candidates: failed (rc=%d)\n",
1125 LDAP_LOG ( OPERATION, DETAIL1,
1126 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1127 (long) ids[0], (long) BDB_IDL_FIRST(ids),
1128 (long) BDB_IDL_LAST(ids));
1130 Debug(LDAP_DEBUG_TRACE,
1131 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1133 (long) BDB_IDL_FIRST(ids),
1134 (long) BDB_IDL_LAST(ids) );
1141 #ifdef LDAP_CONTROL_PAGEDRESULTS
1143 send_pagerequest_response(
1150 LDAPControl ctrl, *ctrls[2];
1151 char berbuf[LBER_ELEMENT_SIZEOF];
1152 BerElement *ber = (BerElement *)berbuf;
1153 struct berval cookie = { 0, NULL };
1154 PagedResultsCookie respcookie;
1157 LDAP_LOG ( OPERATION, ENTRY,
1158 "send_pagerequest_response: lastid: (0x%08lx) "
1159 "nentries: (0x%081x)\n",
1160 lastid, nentries, NULL );
1162 Debug(LDAP_DEBUG_ARGS, "send_pagerequest_response: lastid: (0x%08lx) "
1163 "nentries: (0x%081x)\n", lastid, nentries, NULL );
1166 ctrl.ldctl_value.bv_val = NULL;
1170 ber_init2( ber, NULL, LBER_USE_DER );
1172 respcookie = ( PagedResultsCookie )lastid;
1173 conn->c_pagedresults_state.ps_cookie = respcookie;
1174 cookie.bv_len = sizeof( respcookie );
1175 cookie.bv_val = (char *)&respcookie;
1178 * FIXME: we should consider sending an estimate of the entries
1179 * left, after appropriate security check is done
1181 ber_printf( ber, "{iO}", tentries, &cookie );
1183 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1187 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1188 ctrls[0]->ldctl_iscritical = 0;
1190 send_search_result( conn, op,
1192 NULL, NULL, NULL, ctrls, nentries );
1195 (void) ber_free_buf( ber );
1199 #ifdef LDAP_CLIENT_UPDATE
1201 bdb_build_lcup_update_ctrl(
1206 LDAPControl **ctrls,
1208 struct berval *latest_entrycsn_bv,
1215 const char *text = NULL;
1217 char berbuf[LBER_ELEMENT_SIZEOF];
1218 BerElement *ber = (BerElement *)berbuf;
1220 struct berval entrycsn_bv = { 0, NULL };
1222 ber_init2( ber, 0, LBER_USE_DER );
1224 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1226 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
1227 AttributeDescription *desc = a->a_desc;
1228 if ( desc == slap_schema.si_ad_entryCSN ) {
1229 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
1230 if ( latest_entrycsn_bv->bv_val == NULL ) {
1231 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1233 res = value_match( &ret, desc,
1234 desc->ad_type->sat_ordering,
1235 SLAP_MR_ASSERTION_SYNTAX_MATCH,
1236 &entrycsn_bv, latest_entrycsn_bv, &text );
1237 if ( res != LDAP_SUCCESS ) {
1240 LDAP_LOG ( OPERATION, RESULTS,
1241 "bdb_search: value_match failed\n",
1244 Debug( LDAP_DEBUG_TRACE,
1245 "bdb_search: value_match failed\n",
1251 ch_free( latest_entrycsn_bv->bv_val );
1252 latest_entrycsn_bv->bv_val = NULL;
1253 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1259 if ( entry_count % op->o_clientupdate_interval == 0 )
1262 SLAP_LCUP_STATE_UPDATE_FALSE,
1264 LDAP_LCUP_COOKIE_OID, &entrycsn_bv );
1265 else /* Do not send cookie */
1268 SLAP_LCUP_STATE_UPDATE_FALSE,
1271 ch_free( entrycsn_bv.bv_val );
1272 entrycsn_bv.bv_val = NULL;
1274 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_ENTRY_UPDATE;
1275 ctrls[num_ctrls]->ldctl_iscritical = op->o_clientupdate;
1276 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1278 ber_free_buf( ber );
1282 LDAP_LOG ( OPERATION, RESULTS,
1283 "bdb_build_lcup_ctrl: ber_flatten2 failed\n",
1286 Debug( LDAP_DEBUG_TRACE,
1287 "bdb_build_lcup_ctrl: ber_flatten2 failed\n",
1290 send_ldap_result( conn, op, rc=LDAP_OTHER,
1291 NULL, "internal error", NULL, NULL );
1295 return LDAP_SUCCESS;
1299 bdb_build_lcup_done_ctrl(
1302 LDAPControl **ctrls,
1304 struct berval *latest_entrycsn_bv )
1307 char berbuf[LBER_ELEMENT_SIZEOF];
1308 BerElement *ber = (BerElement *)berbuf;
1310 ber_init2( ber, NULL, LBER_USE_DER );
1312 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1314 ber_printf( ber, "{sO", LDAP_LCUP_COOKIE_OID, latest_entrycsn_bv );
1315 ber_printf( ber, "N}" );
1317 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_CLIENT_UPDATE_DONE;
1318 ctrls[num_ctrls]->ldctl_iscritical = op->o_clientupdate;
1319 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1321 ber_free_buf( ber );
1325 LDAP_LOG ( OPERATION, RESULTS,
1326 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n", 0, 0, 0 );
1328 Debug( LDAP_DEBUG_TRACE, "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1331 send_ldap_result( conn, op, rc=LDAP_OTHER,
1332 NULL, "internal error", NULL, NULL );
1336 return LDAP_SUCCESS;
1342 bdb_build_sync_state_ctrl(
1346 int entry_sync_state,
1347 LDAPControl **ctrls,
1350 struct berval *latest_entrycsn_bv )
1356 const char *text = NULL;
1358 char berbuf[LBER_ELEMENT_SIZEOF];
1359 BerElement *ber = (BerElement *)berbuf;
1361 struct berval entryuuid_bv = { 0, NULL };
1362 struct berval entrycsn_bv = { 0, NULL };
1364 ber_init2( ber, 0, LBER_USE_DER );
1366 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1368 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
1369 AttributeDescription *desc = a->a_desc;
1370 if ( desc == slap_schema.si_ad_entryCSN ) {
1371 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
1372 if ( latest_entrycsn_bv->bv_val == NULL ) {
1373 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1375 res = value_match( &ret, desc,
1376 desc->ad_type->sat_ordering,
1377 SLAP_MR_ASSERTION_SYNTAX_MATCH,
1378 &entrycsn_bv, latest_entrycsn_bv, &text );
1379 if ( res != LDAP_SUCCESS ) {
1382 LDAP_LOG ( OPERATION, RESULTS,
1383 "bdb_search: value_match failed\n",
1386 Debug( LDAP_DEBUG_TRACE,
1387 "bdb_search: value_match failed\n",
1392 ch_free( latest_entrycsn_bv->bv_val );
1393 latest_entrycsn_bv->bv_val = NULL;
1394 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1397 } else if ( desc == slap_schema.si_ad_entryUUID ) {
1398 ber_dupbv( &entryuuid_bv, &a->a_vals[0] );
1403 ber_printf( ber, "{eOON}", entry_sync_state, &entryuuid_bv, &entrycsn_bv );
1405 ber_printf( ber, "{eON}", entry_sync_state, &entryuuid_bv );
1407 ch_free( entrycsn_bv.bv_val );
1408 entrycsn_bv.bv_val = NULL;
1409 ch_free( entryuuid_bv.bv_val );
1410 entryuuid_bv.bv_val = NULL;
1412 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
1413 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
1414 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1416 ber_free_buf( ber );
1420 LDAP_LOG ( OPERATION, RESULTS,
1421 "bdb_build_sync_ctrl: ber_flatten2 failed\n",
1424 Debug( LDAP_DEBUG_TRACE,
1425 "bdb_build_sync_ctrl: ber_flatten2 failed\n",
1428 send_ldap_result( conn, op, rc=LDAP_OTHER,
1429 NULL, "internal error", NULL, NULL );
1433 return LDAP_SUCCESS;
1437 bdb_build_sync_done_ctrl(
1440 LDAPControl **ctrls,
1443 struct berval *latest_entrycsn_bv )
1446 char berbuf[LBER_ELEMENT_SIZEOF];
1447 BerElement *ber = (BerElement *)berbuf;
1449 ber_init2( ber, NULL, LBER_USE_DER );
1451 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1453 if ( send_cookie ) {
1454 ber_printf( ber, "{ON}", latest_entrycsn_bv );
1456 ber_printf( ber, "{N}" );
1459 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
1460 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
1461 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1463 ber_free_buf( ber );
1467 LDAP_LOG ( OPERATION, RESULTS,
1468 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n", 0, 0, 0 );
1470 Debug( LDAP_DEBUG_TRACE, "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1473 send_ldap_result( conn, op, rc=LDAP_OTHER,
1474 NULL, "internal error", NULL, NULL );
1478 return LDAP_SUCCESS;
1482 bdb_send_ldap_intermediate(
1486 const char *matched,
1491 struct berval *cookie,
1492 LDAPControl **ctrls )
1494 char berbuf[LBER_ELEMENT_SIZEOF];
1495 BerElement *ber = (BerElement *)berbuf;
1496 struct berval rspdata;
1500 ber_init2( ber, NULL, LBER_USE_DER );
1502 if ( cookie == NULL )
1503 ber_printf( ber, "{eN}", state );
1505 ber_printf( ber, "{eON}", state, cookie );
1507 ret = ber_flatten2( ber, &rspdata, 0 );
1511 LDAP_LOG ( OPERATION, RESULTS,
1512 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n", 0, 0, 0 );
1514 Debug( LDAP_DEBUG_TRACE, "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1517 send_ldap_result( conn, op, rc=LDAP_OTHER,
1518 NULL, "internal error", NULL, NULL );
1522 send_ldap_intermediate_resp( conn, op, err, matched, text, refs, rspoid, &rspdata, ctrls );
1524 ber_free_buf( ber );
1526 return LDAP_SUCCESS;