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;
89 LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
91 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
95 #ifdef LDAP_CLIENT_UPDATE
96 if ( op->o_clientupdate_type & SLAP_LCUP_PERSIST ) {
97 bdb_add_psearch_spec( be, conn, op, base, base, scope, deref, slimit,
98 tlimit, filter, filterstr, attrs, attrsonly, LDAP_CLIENT_UPDATE );
102 #if defined(LDAP_CLIENT_UPDATE) && defined(LDAP_SYNC)
106 /* psearch needs to be registered before refresh begins */
107 /* psearch and refresh transmission is serialized in send_ldap_ber() */
108 if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
109 bdb_add_psearch_spec( be, conn, op, base, base, scope, deref, slimit,
110 tlimit, filter, filterstr, attrs, attrsonly, LDAP_SYNC );
112 null_attr.an_desc = NULL;
113 null_attr.an_oc = NULL;
114 null_attr.an_name.bv_len = 0;
115 null_attr.an_name.bv_val = NULL;
118 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
119 for ( num_ctrls = 0; num_ctrls < SLAP_SEARCH_MAX_CTRLS; num_ctrls++ )
120 ctrls[num_ctrls] = NULL;
125 manageDSAit = get_manageDSAit( op );
127 rc = LOCK_ID (bdb->bi_dbenv, &locker );
133 send_ldap_result( conn, op, rc=LDAP_OTHER,
134 NULL, "internal error", NULL, NULL );
138 if ( nbase->bv_len == 0 ) {
139 /* DIT root special case */
140 e = (Entry *) &slap_entry_root;
144 /* get entry with reader lock */
145 if ( deref & LDAP_DEREF_FINDING ) {
146 e = deref_dn_r( be, nbase-, &err, &matched, &text );
152 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
161 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
163 if (matched != NULL) {
164 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
166 send_ldap_result( conn, op, LDAP_BUSY,
167 NULL, "ldap server busy", NULL, NULL );
168 LOCK_ID_FREE (bdb->bi_dbenv, locker );
170 case DB_LOCK_DEADLOCK:
171 case DB_LOCK_NOTGRANTED:
175 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
177 if (matched != NULL) {
178 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
180 send_ldap_result( conn, op, rc=LDAP_OTHER,
181 NULL, "internal error", NULL, NULL );
182 LOCK_ID_FREE (bdb->bi_dbenv, locker );
187 struct berval matched_dn = { 0, NULL };
188 BerVarray refs = NULL;
190 if ( matched != NULL ) {
192 ber_dupbv( &matched_dn, &matched->e_name );
194 erefs = is_entry_referral( matched )
195 ? get_entry_referrals( be, conn, op, matched )
198 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
202 refs = referral_rewrite( erefs, &matched_dn,
204 ber_bvarray_free( erefs );
208 refs = referral_rewrite( default_referral,
212 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
213 matched_dn.bv_val, text, refs, NULL );
215 LOCK_ID_FREE (bdb->bi_dbenv, locker );
216 if ( refs ) ber_bvarray_free( refs );
217 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
221 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
222 /* entry is a referral, don't allow add */
223 struct berval matched_dn;
224 BerVarray erefs, refs;
226 ber_dupbv( &matched_dn, &e->e_name );
227 erefs = get_entry_referrals( be, conn, op, e );
230 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
234 refs = referral_rewrite( erefs, &matched_dn,
236 ber_bvarray_free( erefs );
240 LDAP_LOG ( OPERATION, RESULTS,
241 "bdb_search: entry is referral\n", 0, 0, 0 );
243 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
247 send_ldap_result( conn, op, LDAP_REFERRAL,
249 refs ? NULL : "bad referral object",
252 LOCK_ID_FREE (bdb->bi_dbenv, locker );
253 ber_bvarray_free( refs );
254 ber_memfree( matched_dn.bv_val );
258 /* if not root, get appropriate limits */
259 if ( be_isroot( be, &op->o_ndn ) ) {
262 ( void ) get_limits( be, &op->o_ndn, &limit );
265 /* The time/size limits come first because they require very little
266 * effort, so there's no chance the candidates are selected and then
267 * the request is not honored only because of time/size constraints */
269 /* if no time limit requested, use soft limit (unless root!) */
272 tlimit = -1; /* allow root to set no limit */
280 /* if no limit is required, use soft limit */
282 tlimit = limit->lms_t_soft;
284 /* if requested limit higher than hard limit, abort */
285 } else if ( tlimit > limit->lms_t_hard ) {
286 /* no hard limit means use soft instead */
287 if ( limit->lms_t_hard == 0
288 && limit->lms_t_soft > -1
289 && tlimit > limit->lms_t_soft ) {
290 tlimit = limit->lms_t_soft;
292 /* positive hard limit means abort */
293 } else if ( limit->lms_t_hard > 0 ) {
294 send_search_result( conn, op,
295 LDAP_ADMINLIMIT_EXCEEDED,
296 NULL, NULL, NULL, NULL, 0 );
301 /* negative hard limit means no limit */
304 /* if no limit is required, use soft limit */
306 if ( get_pagedresults(op) && limit->lms_s_pr != 0 ) {
307 slimit = limit->lms_s_pr;
309 slimit = limit->lms_s_soft;
312 /* if requested limit higher than hard limit, abort */
313 } else if ( slimit > limit->lms_s_hard ) {
314 /* no hard limit means use soft instead */
315 if ( limit->lms_s_hard == 0
316 && limit->lms_s_soft > -1
317 && slimit > limit->lms_s_soft ) {
318 slimit = limit->lms_s_soft;
320 /* positive hard limit means abort */
321 } else if ( limit->lms_s_hard > 0 ) {
322 send_search_result( conn, op,
323 LDAP_ADMINLIMIT_EXCEEDED,
324 NULL, NULL, NULL, NULL, 0 );
329 /* negative hard limit means no limit */
333 /* compute it anyway; root does not use it */
334 stoptime = op->o_time + tlimit;
336 /* select candidates */
337 if ( scope == LDAP_SCOPE_BASE ) {
338 rc = base_candidate( be, e, candidates );
341 BDB_IDL_ALL( bdb, candidates );
342 rc = search_candidates( be, op, e, filter,
343 scope, deref, candidates );
346 /* need normalized dn below */
347 ber_dupbv( &realbase, &e->e_nname );
349 /* start cursor at beginning of candidates.
353 if ( e != &slap_entry_root ) {
354 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
358 if ( candidates[0] == 0 ) {
360 LDAP_LOG ( OPERATION, RESULTS,
361 "bdb_search: no candidates\n", 0, 0, 0 );
363 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
367 send_search_result( conn, op,
369 NULL, NULL, NULL, NULL, 0 );
375 /* if not root and candidates exceed to-be-checked entries, abort */
376 if ( !isroot && limit->lms_s_unchecked != -1 ) {
377 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
378 send_search_result( conn, op,
379 LDAP_ADMINLIMIT_EXCEEDED,
380 NULL, NULL, NULL, NULL, 0 );
386 if ( isroot || !limit->lms_s_pr_hide ) {
387 tentries = BDB_IDL_N(candidates);
390 #ifdef LDAP_CONTROL_PAGEDRESULTS
391 if ( get_pagedresults(op) ) {
392 if ( op->o_pagedresults_state.ps_cookie == 0 ) {
395 if ( op->o_pagedresults_size == 0 ) {
396 send_search_result( conn, op, LDAP_SUCCESS,
397 NULL, "search abandoned by pagedResult size=0",
401 for ( id = bdb_idl_first( candidates, &cursor );
402 id != NOID && id <= (ID)( op->o_pagedresults_state.ps_cookie );
403 id = bdb_idl_next( candidates, &cursor ) );
405 if ( cursor == NOID ) {
407 LDAP_LOG ( OPERATION, RESULTS,
408 "bdb_search: no paged results candidates\n",
411 Debug( LDAP_DEBUG_TRACE,
412 "bdb_search: no paged results candidates\n",
415 send_pagerequest_response( conn, op, lastid, 0, 0 );
424 #ifdef LDAP_CLIENT_UPDATE
425 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
426 cookief.f_choice = LDAP_FILTER_AND;
427 cookief.f_and = &csnfnot;
428 cookief.f_next = NULL;
430 csnfnot.f_choice = LDAP_FILTER_NOT;
431 csnfnot.f_not = &csnfeq;
432 csnfnot.f_next = &csnfand;
434 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
435 csnfeq.f_ava = &aa_eq;
436 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
437 ber_dupbv( &csnfeq.f_av_value, &op->o_clientupdate_state );
439 csnfand.f_choice = LDAP_FILTER_AND;
440 csnfand.f_and = &csnfge;
441 csnfand.f_next = NULL;
443 csnfge.f_choice = LDAP_FILTER_GE;
444 csnfge.f_ava = &aa_ge;
445 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
446 ber_dupbv( &csnfge.f_av_value, &op->o_clientupdate_state );
447 csnfge.f_next = filter;
450 #if defined(LDAP_CLIENT_UPDATE) && defined(LDAP_SYNC)
454 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
455 cookief.f_choice = LDAP_FILTER_AND;
456 cookief.f_and = &csnfnot;
457 cookief.f_next = NULL;
459 csnfnot.f_choice = LDAP_FILTER_NOT;
460 csnfnot.f_not = &csnfeq;
461 csnfnot.f_next = &csnfand;
463 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
464 csnfeq.f_ava = &aa_eq;
465 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
466 ber_dupbv( &csnfeq.f_av_value, &op->o_sync_state );
468 csnfand.f_choice = LDAP_FILTER_AND;
469 csnfand.f_and = &csnfge;
470 csnfand.f_next = NULL;
472 csnfge.f_choice = LDAP_FILTER_GE;
473 csnfge.f_ava = &aa_ge;
474 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
475 ber_dupbv( &csnfge.f_av_value, &op->o_sync_state );
476 csnfge.f_next = filter;
480 for ( id = bdb_idl_first( candidates, &cursor );
482 id = bdb_idl_next( candidates, &cursor ) )
488 /* check for abandon */
489 if ( op->o_abandon ) {
494 #ifdef LDAP_EXOP_X_CANCEL
495 if ( op->o_cancel ) {
496 assert( op->o_cancel == SLAP_CANCEL_REQ );
498 send_search_result( conn, op, LDAP_CANCELLED,
499 NULL, NULL, NULL, NULL, 0 );
500 op->o_cancel = SLAP_CANCEL_ACK;
505 /* check time limit */
506 if ( tlimit != -1 && slap_get_time() > stoptime ) {
507 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
508 NULL, NULL, v2refs, NULL, nentries );
513 /* get the entry with reader lock */
514 rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
516 if (rc == LDAP_BUSY) {
517 send_ldap_result( conn, op, rc=LDAP_BUSY,
518 NULL, "ldap server busy", NULL, NULL );
521 } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
526 if( !BDB_IDL_IS_RANGE(candidates) ) {
527 /* only complain for non-range IDLs */
529 LDAP_LOG ( OPERATION, RESULTS,
530 "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
532 Debug( LDAP_DEBUG_TRACE,
533 "bdb_search: candidate %ld not found\n",
541 #ifdef BDB_SUBENTRIES
542 if ( is_entry_subentry( e ) ) {
543 if( scope != LDAP_SCOPE_BASE ) {
544 if(!get_subentries_visibility( op )) {
545 /* only subentries are visible */
549 } else if ( get_subentries( op ) &&
550 !get_subentries_visibility( op ))
552 /* only subentries are visible */
556 } else if ( get_subentries_visibility( op )) {
557 /* only subentries are visible */
563 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
568 e = deref_entry_r( be, e, &err, &matched, &text );
575 if( e->e_id == id ) {
580 /* need to skip alias which deref into scope */
581 if( scope & LDAP_SCOPE_ONELEVEL ) {
584 dnParent( &e->e_nname, &pdn ):
585 if ( ber_bvcmp( pdn, &realbase ) ) {
589 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
590 /* alias is within scope */
592 LDAP_LOG ( OPERATION, RESULTS,
593 "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
595 Debug( LDAP_DEBUG_TRACE,
596 "bdb_search: \"%s\" in subtree\n",
607 * if it's a referral, add it to the list of referrals. only do
608 * this for non-base searches, and don't check the filter
609 * explicitly here since it's only a candidate anyway.
611 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
612 is_entry_referral( e ) )
617 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
618 if ( !be_issuffix( be, &e->e_nname ) ) {
619 dnParent( &e->e_nname, &dn );
620 scopeok = dn_match( &dn, &realbase );
622 scopeok = (realbase.bv_len == 0);
625 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
626 scopeok = dnIsSuffix( &e->e_nname, &realbase );
633 BerVarray erefs = get_entry_referrals(
635 BerVarray refs = referral_rewrite( erefs,
637 scope == LDAP_SCOPE_SUBTREE
641 send_search_reference( be, conn, op,
642 e, refs, NULL, &v2refs );
644 ber_bvarray_free( refs );
648 LDAP_LOG(OPERATION, DETAIL2,
649 "bdb_search: candidate referral %ld scope not okay\n",
652 Debug( LDAP_DEBUG_TRACE,
653 "bdb_search: candidate referral %ld scope not okay\n",
661 /* if it matches the filter and scope, send it */
662 #ifdef LDAP_CLIENT_UPDATE
663 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
664 rc = test_filter( be, conn, op, e, &cookief );
668 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
669 rc_sync = test_filter( be, conn, op, e, &cookief );
670 rc = test_filter( be, conn, op, e, filter );
671 if ( rc == LDAP_COMPARE_TRUE ) {
672 if ( rc_sync == LDAP_COMPARE_TRUE ) {
673 entry_sync_state = LDAP_SYNC_ADD;
675 entry_sync_state = LDAP_SYNC_PRESENT;
681 rc = test_filter( be, conn, op, e, filter );
684 if ( rc == LDAP_COMPARE_TRUE ) {
688 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
689 if ( be_issuffix( be, &e->e_nname ) ) {
690 scopeok = (realbase.bv_len == 0);
692 dnParent( &e->e_nname, &dn );
693 scopeok = dn_match( &dn, &realbase );
696 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
697 scopeok = dnIsSuffix( &e->e_nname, &realbase );
704 /* check size limit */
705 if ( --slimit == -1 ) {
706 bdb_cache_return_entry_r( bdb->bi_dbenv,
707 &bdb->bi_cache, e, &lock );
709 send_search_result( conn, op,
710 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
711 v2refs, NULL, nentries );
715 #ifdef LDAP_CONTROL_PAGEDRESULTS
716 if ( get_pagedresults(op) ) {
717 if ( nentries >= op->o_pagedresults_size ) {
718 send_pagerequest_response( conn, op,
719 lastid, nentries, tentries );
729 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
735 #ifdef LDAP_CLIENT_UPDATE
736 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
737 rc = bdb_build_lcup_update_ctrl( conn, op, e, ++entry_count, ctrls,
738 num_ctrls++, &latest_entrycsn_bv, SLAP_LCUP_ENTRY_DELETED_FALSE );
739 if ( rc != LDAP_SUCCESS )
741 result = send_search_entry( be, conn, op,
742 e, attrs, attrsonly, ctrls);
744 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL )
745 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
746 ch_free( ctrls[--num_ctrls] );
747 ctrls[num_ctrls] = NULL;
751 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
752 rc = bdb_build_sync_state_ctrl( conn, op, e, entry_sync_state, ctrls,
753 num_ctrls++, 0, &latest_entrycsn_bv );
754 if ( rc != LDAP_SUCCESS )
757 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
758 result = send_search_entry( be, conn, op,
759 e, attrs, attrsonly, ctrls);
760 } else { /* PRESENT */
761 result = send_search_entry( be, conn, op,
762 e, &null_attr, attrsonly, ctrls);
765 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL )
766 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
767 ch_free( ctrls[--num_ctrls] );
768 ctrls[num_ctrls] = NULL;
773 result = send_search_entry( be, conn, op,
774 e, attrs, attrsonly, NULL);
779 case 0: /* entry sent ok */
782 case 1: /* entry not sent */
784 case -1: /* connection closed */
785 bdb_cache_return_entry_r(bdb->bi_dbenv,
786 &bdb->bi_cache, e, &lock);
794 LDAP_LOG ( OPERATION, RESULTS,
795 "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
797 Debug( LDAP_DEBUG_TRACE,
798 "bdb_search: %ld scope not okay\n",
804 LDAP_LOG ( OPERATION, RESULTS,
805 "bdb_search: %ld does not match filter\n", (long) id, 0, 0);
807 Debug( LDAP_DEBUG_TRACE,
808 "bdb_search: %ld does not match filter\n",
815 /* free reader lock */
816 bdb_cache_return_entry_r( bdb->bi_dbenv,
817 &bdb->bi_cache, e , &lock);
821 ldap_pvt_thread_yield();
824 #ifdef LDAP_CLIENT_UPDATE
825 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
826 bdb_build_lcup_done_ctrl( conn, op, ctrls, num_ctrls++, &latest_entrycsn_bv );
828 send_search_result( conn, op,
829 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
830 NULL, NULL, v2refs, ctrls, nentries );
832 ch_free( latest_entrycsn_bv.bv_val );
833 latest_entrycsn_bv.bv_val = NULL;
835 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL )
836 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
837 ch_free( ctrls[--num_ctrls] );
838 ctrls[num_ctrls] = NULL;
842 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
843 if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
844 /* refreshAndPersist mode */
845 bdb_send_ldap_intermediate( conn, op,
846 LDAP_SUCCESS, NULL, NULL, NULL, LDAP_SYNC_INFO,
847 LDAP_SYNC_REFRESH_DONE, &latest_entrycsn_bv, NULL );
849 /* refreshOnly mode */
850 bdb_build_sync_done_ctrl( conn, op, ctrls, num_ctrls++, 1, &latest_entrycsn_bv );
851 send_search_result( conn, op,
852 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
853 NULL, NULL, v2refs, ctrls, nentries );
854 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL )
855 ch_free( ctrls[num_ctrls-1]->ldctl_value.bv_val );
856 ch_free( ctrls[--num_ctrls] );
857 ctrls[num_ctrls] = NULL;
860 ch_free( latest_entrycsn_bv.bv_val );
861 latest_entrycsn_bv.bv_val = NULL;
865 send_search_result( conn, op,
866 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
867 NULL, NULL, v2refs, NULL, nentries );
874 /* free reader lock */
875 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
878 #ifdef LDAP_CLIENT_UPDATE
879 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
880 if ( csnfeq.f_ava != NULL && csnfeq.f_av_value.bv_val != NULL ) {
881 ch_free( csnfeq.f_av_value.bv_val );
884 if ( csnfge.f_ava != NULL && csnfge.f_av_value.bv_val != NULL ) {
885 ch_free( csnfge.f_av_value.bv_val );
889 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
893 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
894 if ( csnfeq.f_ava != NULL && csnfeq.f_av_value.bv_val != NULL ) {
895 ch_free( csnfeq.f_av_value.bv_val );
898 if ( csnfge.f_ava != NULL && csnfge.f_av_value.bv_val != NULL ) {
899 ch_free( csnfge.f_av_value.bv_val );
904 LOCK_ID_FREE (bdb->bi_dbenv, locker );
906 if( v2refs ) ber_bvarray_free( v2refs );
907 if( realbase.bv_val ) ch_free( realbase.bv_val );
913 static int base_candidate(
919 LDAP_LOG ( OPERATION, ENTRY,
920 "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
922 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
923 e->e_dn, (long) e->e_id, 0);
931 /* Look for "objectClass Present" in this filter.
932 * Also count depth of filter tree while we're at it.
934 static int oc_filter(
942 if( cur > *max ) *max = cur;
944 switch(f->f_choice) {
945 case LDAP_FILTER_PRESENT:
946 if (f->f_desc == slap_schema.si_ad_objectClass) {
951 case LDAP_FILTER_AND:
954 for (f=f->f_and; f; f=f->f_next) {
955 (void) oc_filter(f, cur, max);
965 static void search_stack_free( void *key, void *data)
970 static void *search_stack(
975 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
978 if ( op->o_threadctx ) {
979 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
982 ret = bdb->bi_search_stack;
986 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE * sizeof( ID ) );
987 if ( op->o_threadctx ) {
988 ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
989 ret, search_stack_free );
991 bdb->bi_search_stack = ret;
997 static int search_candidates(
1006 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
1008 Filter f, scopef, rf, xf;
1010 AttributeAssertion aa_ref;
1011 #ifdef BDB_SUBENTRIES
1013 AttributeAssertion aa_subentry;
1017 AttributeAssertion aa_alias;
1021 * This routine takes as input a filter (user-filter)
1022 * and rewrites it as follows:
1023 * (&(scope=DN)[(objectClass=subentry)]
1024 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1028 LDAP_LOG ( OPERATION, ENTRY,
1029 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1030 e->e_dn, (long) e->e_id, scope);
1032 Debug(LDAP_DEBUG_TRACE,
1033 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1034 e->e_dn, (long) e->e_id, scope );
1038 xf.f_choice = LDAP_FILTER_OR;
1041 /* If the user's filter uses objectClass=*,
1042 * these clauses are redundant.
1044 if (!oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
1045 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1046 /* match referral objects */
1047 struct berval bv_ref = { sizeof("referral")-1, "referral" };
1048 rf.f_choice = LDAP_FILTER_EQUALITY;
1050 rf.f_av_desc = slap_schema.si_ad_objectClass;
1051 rf.f_av_value = bv_ref;
1052 rf.f_next = xf.f_or;
1057 if( deref & LDAP_DEREF_SEARCHING ) {
1058 /* match alias objects */
1059 struct berval bv_alias = { sizeof("alias")-1, "alias" };
1060 af.f_choice = LDAP_FILTER_EQUALITY;
1061 af.f_ava = &aa_alias;
1062 af.f_av_desc = slap_schema.si_ad_objectClass;
1063 af.f_av_value = bv_alias;
1064 af.f_next = xf.f_or;
1068 /* We added one of these clauses, filter depth increased */
1069 if( xf.f_or != filter ) depth++;
1073 f.f_choice = LDAP_FILTER_AND;
1075 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
1076 ? SLAPD_FILTER_DN_SUBTREE
1077 : SLAPD_FILTER_DN_ONE;
1078 scopef.f_dn = &e->e_nname;
1079 scopef.f_next = xf.f_or == filter ? filter : &xf ;
1080 /* Filter depth increased again, adding scope clause */
1083 #ifdef BDB_SUBENTRIES
1084 if( get_subentries_visibility( op ) ) {
1085 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
1086 sf.f_choice = LDAP_FILTER_EQUALITY;
1087 sf.f_ava = &aa_subentry;
1088 sf.f_av_desc = slap_schema.si_ad_objectClass;
1089 sf.f_av_value = bv_subentry;
1090 sf.f_next = scopef.f_next;
1091 scopef.f_next = &sf;
1095 /* Allocate IDL stack, plus 1 more for former tmp */
1096 if ( depth+1 > bdb->bi_search_stack_depth ) {
1097 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1099 stack = search_stack( be, op );
1102 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
1104 if ( depth+1 > bdb->bi_search_stack_depth ) {
1110 LDAP_LOG ( OPERATION, DETAIL1,
1111 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
1113 Debug(LDAP_DEBUG_TRACE,
1114 "bdb_search_candidates: failed (rc=%d)\n",
1120 LDAP_LOG ( OPERATION, DETAIL1,
1121 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1122 (long) ids[0], (long) BDB_IDL_FIRST(ids),
1123 (long) BDB_IDL_LAST(ids));
1125 Debug(LDAP_DEBUG_TRACE,
1126 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1128 (long) BDB_IDL_FIRST(ids),
1129 (long) BDB_IDL_LAST(ids) );
1136 #ifdef LDAP_CONTROL_PAGEDRESULTS
1138 send_pagerequest_response(
1145 LDAPControl ctrl, *ctrls[2];
1146 char berbuf[LBER_ELEMENT_SIZEOF];
1147 BerElement *ber = (BerElement *)berbuf;
1148 struct berval cookie = { 0, NULL };
1149 PagedResultsCookie respcookie;
1152 LDAP_LOG ( OPERATION, ENTRY,
1153 "send_pagerequest_response: lastid: (0x%08lx) "
1154 "nentries: (0x%081x)\n",
1155 lastid, nentries, NULL );
1157 Debug(LDAP_DEBUG_ARGS, "send_pagerequest_response: lastid: (0x%08lx) "
1158 "nentries: (0x%081x)\n", lastid, nentries, NULL );
1161 ctrl.ldctl_value.bv_val = NULL;
1165 ber_init2( ber, NULL, LBER_USE_DER );
1167 respcookie = ( PagedResultsCookie )lastid;
1168 conn->c_pagedresults_state.ps_cookie = respcookie;
1169 cookie.bv_len = sizeof( respcookie );
1170 cookie.bv_val = (char *)&respcookie;
1173 * FIXME: we should consider sending an estimate of the entries
1174 * left, after appropriate security check is done
1176 ber_printf( ber, "{iO}", tentries, &cookie );
1178 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1182 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1183 ctrls[0]->ldctl_iscritical = 0;
1185 send_search_result( conn, op,
1187 NULL, NULL, NULL, ctrls, nentries );
1190 (void) ber_free_buf( ber );
1194 #ifdef LDAP_CLIENT_UPDATE
1196 bdb_build_lcup_update_ctrl(
1201 LDAPControl **ctrls,
1203 struct berval *latest_entrycsn_bv,
1210 const char *text = NULL;
1212 char berbuf[LBER_ELEMENT_SIZEOF];
1213 BerElement *ber = (BerElement *)berbuf;
1215 struct berval entrycsn_bv = { 0, NULL };
1217 ber_init2( ber, 0, LBER_USE_DER );
1219 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1221 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
1222 AttributeDescription *desc = a->a_desc;
1223 if ( desc == slap_schema.si_ad_entryCSN ) {
1224 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
1225 if ( latest_entrycsn_bv->bv_val == NULL ) {
1226 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1228 res = value_match( &ret, desc,
1229 desc->ad_type->sat_ordering, 0,
1230 &entrycsn_bv, latest_entrycsn_bv, &text );
1231 if ( res != LDAP_SUCCESS ) {
1234 LDAP_LOG ( OPERATION, RESULTS,
1235 "bdb_search: value_match failed\n",
1238 Debug( LDAP_DEBUG_TRACE,
1239 "bdb_search: value_match failed\n",
1245 ch_free( latest_entrycsn_bv->bv_val );
1246 latest_entrycsn_bv->bv_val = NULL;
1247 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1253 if ( entry_count % op->o_clientupdate_interval == 0 )
1256 SLAP_LCUP_STATE_UPDATE_FALSE,
1258 LDAP_CUP_COOKIE_OID, &entrycsn_bv );
1259 else /* Do not send cookie */
1262 SLAP_LCUP_STATE_UPDATE_FALSE,
1265 ch_free( entrycsn_bv.bv_val );
1266 entrycsn_bv.bv_val = NULL;
1268 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_ENTRY_UPDATE;
1269 ctrls[num_ctrls]->ldctl_iscritical = op->o_clientupdate;
1270 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1272 ber_free_buf( ber );
1276 LDAP_LOG ( OPERATION, RESULTS,
1277 "bdb_build_lcup_ctrl: ber_flatten2 failed\n",
1280 Debug( LDAP_DEBUG_TRACE,
1281 "bdb_build_lcup_ctrl: ber_flatten2 failed\n",
1284 send_ldap_result( conn, op, rc=LDAP_OTHER,
1285 NULL, "internal error", NULL, NULL );
1289 return LDAP_SUCCESS;
1293 bdb_build_lcup_done_ctrl(
1296 LDAPControl **ctrls,
1298 struct berval *latest_entrycsn_bv )
1301 char berbuf[LBER_ELEMENT_SIZEOF];
1302 BerElement *ber = (BerElement *)berbuf;
1304 ber_init2( ber, NULL, LBER_USE_DER );
1306 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1308 ber_printf( ber, "{sO", LDAP_CUP_COOKIE_OID, latest_entrycsn_bv );
1309 ber_printf( ber, "N}" );
1311 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_CLIENT_UPDATE_DONE;
1312 ctrls[num_ctrls]->ldctl_iscritical = op->o_clientupdate;
1313 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1315 ber_free_buf( ber );
1319 LDAP_LOG ( OPERATION, RESULTS,
1320 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n", 0, 0, 0 );
1322 Debug( LDAP_DEBUG_TRACE, "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1325 send_ldap_result( conn, op, rc=LDAP_OTHER,
1326 NULL, "internal error", NULL, NULL );
1330 return LDAP_SUCCESS;
1336 bdb_build_sync_state_ctrl(
1340 int entry_sync_state,
1341 LDAPControl **ctrls,
1344 struct berval *latest_entrycsn_bv )
1350 const char *text = NULL;
1352 char berbuf[LBER_ELEMENT_SIZEOF];
1353 BerElement *ber = (BerElement *)berbuf;
1355 struct berval entryuuid_bv = { 0, NULL };
1356 struct berval entrycsn_bv = { 0, NULL };
1358 ber_init2( ber, 0, LBER_USE_DER );
1360 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1362 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
1363 AttributeDescription *desc = a->a_desc;
1364 if ( desc == slap_schema.si_ad_entryCSN ) {
1365 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
1366 if ( latest_entrycsn_bv->bv_val == NULL ) {
1367 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1369 res = value_match( &ret, desc,
1370 desc->ad_type->sat_ordering, 0,
1371 &entrycsn_bv, latest_entrycsn_bv, &text );
1372 if ( res != LDAP_SUCCESS ) {
1375 LDAP_LOG ( OPERATION, RESULTS,
1376 "bdb_search: value_match failed\n",
1379 Debug( LDAP_DEBUG_TRACE,
1380 "bdb_search: value_match failed\n",
1385 ch_free( latest_entrycsn_bv->bv_val );
1386 latest_entrycsn_bv->bv_val = NULL;
1387 ber_dupbv( latest_entrycsn_bv, &entrycsn_bv );
1390 } else if ( desc == slap_schema.si_ad_entryUUID ) {
1391 ber_dupbv( &entryuuid_bv, &a->a_vals[0] );
1396 ber_printf( ber, "{eOON}", entry_sync_state, &entryuuid_bv, &entrycsn_bv );
1398 ber_printf( ber, "{eON}", entry_sync_state, &entryuuid_bv );
1400 ch_free( entrycsn_bv.bv_val );
1401 entrycsn_bv.bv_val = NULL;
1402 ch_free( entryuuid_bv.bv_val );
1403 entryuuid_bv.bv_val = NULL;
1405 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
1406 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
1407 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1409 ber_free_buf( ber );
1413 LDAP_LOG ( OPERATION, RESULTS,
1414 "bdb_build_sync_ctrl: ber_flatten2 failed\n",
1417 Debug( LDAP_DEBUG_TRACE,
1418 "bdb_build_sync_ctrl: ber_flatten2 failed\n",
1421 send_ldap_result( conn, op, rc=LDAP_OTHER,
1422 NULL, "internal error", NULL, NULL );
1426 return LDAP_SUCCESS;
1430 bdb_build_sync_done_ctrl(
1433 LDAPControl **ctrls,
1436 struct berval *latest_entrycsn_bv )
1439 char berbuf[LBER_ELEMENT_SIZEOF];
1440 BerElement *ber = (BerElement *)berbuf;
1442 ber_init2( ber, NULL, LBER_USE_DER );
1444 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
1446 if ( send_cookie ) {
1447 ber_printf( ber, "{ON}", latest_entrycsn_bv );
1449 ber_printf( ber, "{N}" );
1452 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
1453 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
1454 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
1456 ber_free_buf( ber );
1460 LDAP_LOG ( OPERATION, RESULTS,
1461 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n", 0, 0, 0 );
1463 Debug( LDAP_DEBUG_TRACE, "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1466 send_ldap_result( conn, op, rc=LDAP_OTHER,
1467 NULL, "internal error", NULL, NULL );
1471 return LDAP_SUCCESS;
1475 bdb_send_ldap_intermediate(
1479 const char *matched,
1484 struct berval *cookie,
1485 LDAPControl **ctrls )
1487 char berbuf[LBER_ELEMENT_SIZEOF];
1488 BerElement *ber = (BerElement *)berbuf;
1489 struct berval rspdata;
1493 ber_init2( ber, NULL, LBER_USE_DER );
1495 if ( cookie == NULL )
1496 ber_printf( ber, "{eN}", state );
1498 ber_printf( ber, "{eON}", state, cookie );
1500 ret = ber_flatten2( ber, &rspdata, 0 );
1504 LDAP_LOG ( OPERATION, RESULTS,
1505 "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n", 0, 0, 0 );
1507 Debug( LDAP_DEBUG_TRACE, "bdb_build_lcup_done_ctrl: ber_flatten2 failed\n",
1510 send_ldap_result( conn, op, rc=LDAP_OTHER,
1511 NULL, "internal error", NULL, NULL );
1515 send_ldap_intermediate_resp( conn, op, err, matched, text, refs, rspoid, &rspdata, ctrls );
1517 ber_free_buf( ber );
1519 return LDAP_SUCCESS;