1 /* search.c - search operation */
4 * Copyright 1998-2002 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 #ifdef LDAP_CONTROL_PAGEDRESULTS
30 static void send_pagerequest_response(
36 #endif /* LDAP_CONTROL_PAGEDRESULTS */
50 struct berval *filterstr,
54 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
56 const char *text = NULL;
59 ID candidates[BDB_IDL_UM_SIZE];
61 BerVarray v2refs = NULL;
62 Entry *matched = NULL;
63 struct berval realbase = { 0, NULL };
66 #ifdef LDAP_CONTROL_PAGEDRESULTS
70 #endif /* LDAP_CONTROL_PAGEDRESULTS */
72 #ifdef LDAP_CLIENT_UPDATE
73 Filter lcupf, csnfnot, csnfeq, csnfand, csnfge;
74 AttributeAssertion aa_ge, aa_eq;
76 struct berval entrycsn_bv = { 0, NULL };
77 struct berval latest_entrycsn_bv = { 0, NULL };
78 #endif /* LDAP_CLIENT_UPDATE */
80 struct slap_limits_set *limit = NULL;
87 LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
89 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
93 #ifdef LDAP_CLIENT_UPDATE
94 if ( op->o_clientupdate_type & SLAP_LCUP_PERSIST ) {
95 bdb_add_psearch_spec( be, conn, op, base, base, scope,
96 deref, slimit, tlimit, filter, filterstr, attrs, attrsonly );
102 manageDSAit = get_manageDSAit( op );
104 #ifdef LDAP_CONTROL_PAGEDRESULTS
105 pagedresults = get_pagedresults( op );
106 #endif /* LDAP_CONTROL_PAGEDRESULTS */
108 rc = LOCK_ID (bdb->bi_dbenv, &locker );
113 send_ldap_result( conn, op, rc=LDAP_OTHER,
114 NULL, "internal error", NULL, NULL );
118 if ( nbase->bv_len == 0 ) {
119 /* DIT root special case */
120 e = (Entry *) &slap_entry_root;
124 /* get entry with reader lock */
125 if ( deref & LDAP_DEREF_FINDING ) {
126 e = deref_dn_r( be, nbase-, &err, &matched, &text );
132 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
141 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
143 if (matched != NULL) {
144 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
146 send_ldap_result( conn, op, LDAP_BUSY,
147 NULL, "ldap server busy", NULL, NULL );
148 LOCK_ID_FREE (bdb->bi_dbenv, locker );
150 case DB_LOCK_DEADLOCK:
151 case DB_LOCK_NOTGRANTED:
155 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
157 if (matched != NULL) {
158 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
160 send_ldap_result( conn, op, rc=LDAP_OTHER,
161 NULL, "internal error", NULL, NULL );
162 LOCK_ID_FREE (bdb->bi_dbenv, locker );
167 struct berval matched_dn = { 0, NULL };
168 BerVarray refs = NULL;
170 if ( matched != NULL ) {
172 ber_dupbv( &matched_dn, &matched->e_name );
174 erefs = is_entry_referral( matched )
175 ? get_entry_referrals( be, conn, op, matched )
178 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
182 refs = referral_rewrite( erefs, &matched_dn,
184 ber_bvarray_free( erefs );
188 refs = referral_rewrite( default_referral,
192 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
193 matched_dn.bv_val, text, refs, NULL );
195 LOCK_ID_FREE (bdb->bi_dbenv, locker );
196 if ( refs ) ber_bvarray_free( refs );
197 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
201 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
202 /* entry is a referral, don't allow add */
203 struct berval matched_dn;
204 BerVarray erefs, refs;
206 ber_dupbv( &matched_dn, &e->e_name );
207 erefs = get_entry_referrals( be, conn, op, e );
210 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
214 refs = referral_rewrite( erefs, &matched_dn,
216 ber_bvarray_free( erefs );
220 LDAP_LOG ( OPERATION, RESULTS,
221 "bdb_search: entry is referral\n", 0, 0, 0 );
223 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
227 send_ldap_result( conn, op, LDAP_REFERRAL,
229 refs ? NULL : "bad referral object",
232 LOCK_ID_FREE (bdb->bi_dbenv, locker );
233 ber_bvarray_free( refs );
234 ber_memfree( matched_dn.bv_val );
238 /* if not root, get appropriate limits */
239 if ( be_isroot( be, &op->o_ndn ) ) {
242 ( void ) get_limits( be, &op->o_ndn, &limit );
245 /* The time/size limits come first because they require very little
246 * effort, so there's no chance the candidates are selected and then
247 * the request is not honored only because of time/size constraints */
249 /* if no time limit requested, use soft limit (unless root!) */
252 tlimit = -1; /* allow root to set no limit */
260 /* if no limit is required, use soft limit */
262 tlimit = limit->lms_t_soft;
264 /* if requested limit higher than hard limit, abort */
265 } else if ( tlimit > limit->lms_t_hard ) {
266 /* no hard limit means use soft instead */
267 if ( limit->lms_t_hard == 0
268 && limit->lms_t_soft > -1
269 && tlimit > limit->lms_t_soft ) {
270 tlimit = limit->lms_t_soft;
272 /* positive hard limit means abort */
273 } else if ( limit->lms_t_hard > 0 ) {
274 send_search_result( conn, op,
275 LDAP_ADMINLIMIT_EXCEEDED,
276 NULL, NULL, NULL, NULL, 0 );
281 /* negative hard limit means no limit */
284 /* if no limit is required, use soft limit */
286 #ifdef LDAP_CONTROL_PAGEDRESULTS
287 if ( pagedresults && limit->lms_s_pr != 0 ) {
288 slimit = limit->lms_s_pr;
290 #endif /* LDAP_CONTROL_PAGEDRESULTS */
291 slimit = limit->lms_s_soft;
292 #ifdef LDAP_CONTROL_PAGEDRESULTS
294 #endif /* LDAP_CONTROL_PAGEDRESULTS */
296 /* if requested limit higher than hard limit, abort */
297 } else if ( slimit > limit->lms_s_hard ) {
298 /* no hard limit means use soft instead */
299 if ( limit->lms_s_hard == 0
300 && limit->lms_s_soft > -1
301 && slimit > limit->lms_s_soft ) {
302 slimit = limit->lms_s_soft;
304 /* positive hard limit means abort */
305 } else if ( limit->lms_s_hard > 0 ) {
306 send_search_result( conn, op,
307 LDAP_ADMINLIMIT_EXCEEDED,
308 NULL, NULL, NULL, NULL, 0 );
313 /* negative hard limit means no limit */
317 /* compute it anyway; root does not use it */
318 stoptime = op->o_time + tlimit;
320 /* select candidates */
321 if ( scope == LDAP_SCOPE_BASE ) {
322 rc = base_candidate( be, e, candidates );
325 BDB_IDL_ALL( bdb, candidates );
326 rc = search_candidates( be, op, e, filter,
327 scope, deref, candidates );
330 /* need normalized dn below */
331 ber_dupbv( &realbase, &e->e_nname );
333 /* start cursor at beginning of candidates.
337 if ( e != &slap_entry_root ) {
338 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
342 if ( candidates[0] == 0 ) {
344 LDAP_LOG ( OPERATION, RESULTS,
345 "bdb_search: no candidates\n", 0, 0, 0 );
347 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
351 send_search_result( conn, op,
353 NULL, NULL, NULL, NULL, 0 );
359 /* if not root and candidates exceed to-be-checked entries, abort */
360 if ( !isroot && limit->lms_s_unchecked != -1 ) {
361 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
362 send_search_result( conn, op,
363 LDAP_ADMINLIMIT_EXCEEDED,
364 NULL, NULL, NULL, NULL, 0 );
370 #ifdef LDAP_CONTROL_PAGEDRESULTS
371 if ( isroot || !limit->lms_s_pr_hide ) {
372 tentries = BDB_IDL_N(candidates);
374 #endif /* LDAP_CONTROL_PAGEDRESULTS */
376 #ifdef LDAP_CONTROL_PAGEDRESULTS
377 if ( pagedresults ) {
378 if ( op->o_pagedresults_state.ps_cookie == 0 ) {
381 if ( op->o_pagedresults_size == 0 ) {
382 send_search_result( conn, op, LDAP_SUCCESS,
383 NULL, "search abandoned by pagedResult size=0",
387 for ( id = bdb_idl_first( candidates, &cursor );
388 id != NOID && id <= (ID)( op->o_pagedresults_state.ps_cookie );
389 id = bdb_idl_next( candidates, &cursor ) );
391 if ( cursor == NOID ) {
393 LDAP_LOG ( OPERATION, RESULTS,
394 "bdb_search: no paged results candidates\n",
397 Debug( LDAP_DEBUG_TRACE,
398 "bdb_search: no paged results candidates\n",
401 send_pagerequest_response( conn, op, lastid, 0, 0 );
408 #endif /* LDAP_CONTROL_PAGEDRESULTS */
410 #ifdef LDAP_CLIENT_UPDATE
411 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
412 lcupf.f_choice = LDAP_FILTER_AND;
413 lcupf.f_and = &csnfnot;
416 csnfnot.f_choice = LDAP_FILTER_NOT;
417 csnfnot.f_not = &csnfeq;
418 csnfnot.f_next = &csnfand;
420 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
421 csnfeq.f_ava = &aa_eq;
422 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
423 ber_dupbv( &csnfeq.f_av_value, &op->o_clientupdate_state );
425 csnfand.f_choice = LDAP_FILTER_AND;
426 csnfand.f_and = &csnfge;
427 csnfand.f_next = NULL;
429 csnfge.f_choice = LDAP_FILTER_GE;
430 csnfge.f_ava = &aa_ge;
431 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
432 ber_dupbv( &csnfge.f_av_value, &op->o_clientupdate_state );
433 csnfge.f_next = filter;
435 #endif /* LDAP_CLIENT_UPDATE */
437 for ( id = bdb_idl_first( candidates, &cursor );
439 id = bdb_idl_next( candidates, &cursor ) )
444 #ifdef LDAP_CONTROL_PAGEDRESULTS
446 #endif /* LDAP_CONTROL_PAGEDRESULTS */
448 /* check for abandon */
449 if ( op->o_abandon ) {
454 /* check time limit */
455 if ( tlimit != -1 && slap_get_time() > stoptime ) {
456 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
457 NULL, NULL, v2refs, NULL, nentries );
462 /* get the entry with reader lock */
463 rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
465 if (rc == LDAP_BUSY) {
466 send_ldap_result( conn, op, rc=LDAP_BUSY,
467 NULL, "ldap server busy", NULL, NULL );
470 } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
475 if( !BDB_IDL_IS_RANGE(candidates) ) {
476 /* only complain for non-range IDLs */
478 LDAP_LOG ( OPERATION, RESULTS,
479 "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
481 Debug( LDAP_DEBUG_TRACE,
482 "bdb_search: candidate %ld not found\n",
490 #ifdef BDB_SUBENTRIES
491 if ( is_entry_subentry( e ) ) {
492 if( scope != LDAP_SCOPE_BASE ) {
493 if(!get_subentries_visibility( op )) {
494 /* only subentries are visible */
498 } else if ( get_subentries( op ) &&
499 !get_subentries_visibility( op ))
501 /* only subentries are visible */
505 } else if ( get_subentries_visibility( op )) {
506 /* only subentries are visible */
512 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
517 e = deref_entry_r( be, e, &err, &matched, &text );
524 if( e->e_id == id ) {
529 /* need to skip alias which deref into scope */
530 if( scope & LDAP_SCOPE_ONELEVEL ) {
533 dnParent( &e->e_nname, &pdn ):
534 if ( ber_bvcmp( pdn, &realbase ) ) {
538 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
539 /* alias is within scope */
541 LDAP_LOG ( OPERATION, RESULTS,
542 "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
544 Debug( LDAP_DEBUG_TRACE,
545 "bdb_search: \"%s\" in subtree\n",
556 * if it's a referral, add it to the list of referrals. only do
557 * this for non-base searches, and don't check the filter
558 * explicitly here since it's only a candidate anyway.
560 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
561 is_entry_referral( e ) )
566 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
567 if ( !be_issuffix( be, &e->e_nname ) ) {
568 dnParent( &e->e_nname, &dn );
569 scopeok = dn_match( &dn, &realbase );
571 scopeok = (realbase.bv_len == 0);
574 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
575 scopeok = dnIsSuffix( &e->e_nname, &realbase );
582 BerVarray erefs = get_entry_referrals(
584 BerVarray refs = referral_rewrite( erefs,
586 scope == LDAP_SCOPE_SUBTREE
590 send_search_reference( be, conn, op,
591 e, refs, NULL, &v2refs );
593 ber_bvarray_free( refs );
597 LDAP_LOG(OPERATION, DETAIL2,
598 "bdb_search: candidate referral %ld scope not okay\n",
601 Debug( LDAP_DEBUG_TRACE,
602 "bdb_search: candidate referral %ld scope not okay\n",
610 /* if it matches the filter and scope, send it */
611 #ifdef LDAP_CLIENT_UPDATE
612 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
613 rc = test_filter( be, conn, op, e, &lcupf );
615 #endif /* LDAP_CLIENT_UPDATE */
617 rc = test_filter( be, conn, op, e, filter );
620 if ( rc == LDAP_COMPARE_TRUE ) {
624 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
625 if ( be_issuffix( be, &e->e_nname ) ) {
626 scopeok = (realbase.bv_len == 0);
628 dnParent( &e->e_nname, &dn );
629 scopeok = dn_match( &dn, &realbase );
632 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
633 scopeok = dnIsSuffix( &e->e_nname, &realbase );
640 /* check size limit */
641 if ( --slimit == -1 ) {
642 bdb_cache_return_entry_r( bdb->bi_dbenv,
643 &bdb->bi_cache, e, &lock );
645 send_search_result( conn, op,
646 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
647 v2refs, NULL, nentries );
650 #ifdef LDAP_CONTROL_PAGEDRESULTS
651 if ( pagedresults ) {
652 if ( nentries >= op->o_pagedresults_size ) {
653 send_pagerequest_response( conn, op, lastid, nentries, tentries );
658 #endif /* LDAP_CONTROL_PAGEDRESULTS */
663 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
669 #ifdef LDAP_CLIENT_UPDATE
670 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
674 const char *text = NULL;
675 LDAPControl *ctrls[2];
678 BerElement *ber = ber_alloc_t( LBER_USE_DER );
682 LDAP_LOG ( OPERATION, RESULTS,
683 "bdb_search: ber_alloc_t failed\n",
686 Debug( LDAP_DEBUG_TRACE,
687 "bdb_search: ber_alloc_t failed\n",
690 send_ldap_result( conn, op, rc=LDAP_OTHER,
691 NULL, "internal error", NULL, NULL );
697 ctrls[0] = ch_malloc ( sizeof ( LDAPControl ) );
700 if ( entry_count % op->o_clientupdate_interval == 0 ) {
702 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
703 AttributeDescription *desc = a->a_desc;
704 if ( desc == slap_schema.si_ad_entryCSN ) {
705 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
706 if ( latest_entrycsn_bv.bv_val == NULL ) {
707 ber_dupbv( &latest_entrycsn_bv, &entrycsn_bv );
709 res = value_match( &ret, desc,
710 desc->ad_type->sat_ordering,
711 SLAP_MR_ASSERTION_SYNTAX_MATCH,
712 &entrycsn_bv, &latest_entrycsn_bv, &text );
713 if ( res != LDAP_SUCCESS ) {
716 LDAP_LOG ( OPERATION, RESULTS,
717 "bdb_search: value_match failed\n",
720 Debug( LDAP_DEBUG_TRACE,
721 "bdb_search: value_match failed\n",
727 ch_free( latest_entrycsn_bv.bv_val );
728 latest_entrycsn_bv.bv_val = NULL;
729 ber_dupbv( &latest_entrycsn_bv,
738 SLAP_LCUP_STATE_UPDATE_FALSE,
739 SLAP_LCUP_ENTRY_DELETED_FALSE,
740 LCUP_COOKIE_OID, &entrycsn_bv );
742 ch_free( entrycsn_bv.bv_val );
743 entrycsn_bv.bv_val = NULL;
746 /* Do not send cookie */
749 SLAP_LCUP_STATE_UPDATE_FALSE,
750 SLAP_LCUP_ENTRY_DELETED_FALSE );
753 ctrls[0]->ldctl_oid = LDAP_CONTROL_ENTRY_UPDATE;
754 ctrls[0]->ldctl_iscritical = op->o_clientupdate;
755 ret = ber_flatten( ber, &bv );
759 LDAP_LOG ( OPERATION, RESULTS,
760 "bdb_search: ber_flatten failed\n",
763 Debug( LDAP_DEBUG_TRACE,
764 "bdb_search: ber_flatten failed\n",
767 send_ldap_result( conn, op, rc=LDAP_OTHER,
768 NULL, "internal error", NULL, NULL );
772 ber_dupbv( &ctrls[0]->ldctl_value, bv );
774 result = send_search_entry( be, conn, op,
775 e, attrs, attrsonly, ctrls);
777 ch_free( ctrls[0]->ldctl_value.bv_val );
782 #endif /* LDAP_CLIENT_UPDATE */
784 result = send_search_entry( be, conn, op,
785 e, attrs, attrsonly, NULL);
790 case 0: /* entry sent ok */
793 case 1: /* entry not sent */
795 case -1: /* connection closed */
796 bdb_cache_return_entry_r(bdb->bi_dbenv,
797 &bdb->bi_cache, e, &lock);
805 LDAP_LOG ( OPERATION, RESULTS,
806 "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
808 Debug( LDAP_DEBUG_TRACE,
809 "bdb_search: %ld scope not okay\n",
815 LDAP_LOG ( OPERATION, RESULTS,
816 "bdb_search: %ld does match filter\n", (long) id, 0, 0);
818 Debug( LDAP_DEBUG_TRACE,
819 "bdb_search: %ld does match filter\n",
826 /* free reader lock */
827 bdb_cache_return_entry_r( bdb->bi_dbenv,
828 &bdb->bi_cache, e , &lock);
832 ldap_pvt_thread_yield();
835 #ifdef LDAP_CLIENT_UPDATE
836 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
838 LDAPControl *ctrls[2];
839 BerElement *ber = ber_alloc_t( LBER_USE_DER );
844 LDAP_LOG ( OPERATION, RESULTS,
845 "bdb_search: ber_alloc_t failed\n", 0, 0, 0 );
847 Debug( LDAP_DEBUG_TRACE, "bdb_search: ber_alloc_t failed\n",
850 send_ldap_result( conn, op, rc=LDAP_OTHER,
851 NULL, "internal error", NULL, NULL );
855 ctrls[0] = ch_malloc ( sizeof ( LDAPControl ) );
858 ber_printf( ber, "{sO", LCUP_COOKIE_OID, &latest_entrycsn_bv );
859 ber_printf( ber, "N}" );
861 ctrls[0]->ldctl_oid = LDAP_CONTROL_CLIENT_UPDATE_DONE;
862 ctrls[0]->ldctl_iscritical = op->o_clientupdate;
863 ret = ber_flatten( ber, &bv );
867 LDAP_LOG ( OPERATION, RESULTS,
868 "bdb_search: ber_flatten failed\n", 0, 0, 0 );
870 Debug( LDAP_DEBUG_TRACE, "bdb_search: ber_flatten failed\n",
873 send_ldap_result( conn, op, rc=LDAP_OTHER,
874 NULL, "internal error", NULL, NULL );
878 ber_dupbv( &ctrls[0]->ldctl_value, bv );
880 send_search_result( conn, op,
881 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
882 NULL, NULL, v2refs, ctrls, nentries );
884 ch_free( latest_entrycsn_bv.bv_val );
885 latest_entrycsn_bv.bv_val = NULL;
886 ch_free( ctrls[0]->ldctl_value.bv_val );
891 #endif /* LDAP_CLIENT_UPDATE */
893 send_search_result( conn, op,
894 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
895 NULL, NULL, v2refs, NULL, nentries );
902 /* free reader lock */
903 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
906 #ifdef LDAP_CLIENT_UPDATE
907 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
908 if ( csnfeq.f_ava != NULL && csnfeq.f_av_value.bv_val != NULL ) {
909 ch_free( csnfeq.f_av_value.bv_val );
912 if ( csnfge.f_ava != NULL && csnfge.f_av_value.bv_val != NULL ) {
913 ch_free( csnfge.f_av_value.bv_val );
916 #endif /* LDAP_CLIENT_UPDATE */
918 LOCK_ID_FREE (bdb->bi_dbenv, locker );
920 if( v2refs ) ber_bvarray_free( v2refs );
921 if( realbase.bv_val ) ch_free( realbase.bv_val );
927 static int base_candidate(
933 LDAP_LOG ( OPERATION, ENTRY,
934 "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
936 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
937 e->e_dn, (long) e->e_id, 0);
945 /* Look for "objectClass Present" in this filter.
946 * Also count depth of filter tree while we're at it.
948 static int oc_filter(
956 if( cur > *max ) *max = cur;
958 switch(f->f_choice) {
959 case LDAP_FILTER_PRESENT:
960 if (f->f_desc == slap_schema.si_ad_objectClass) {
965 case LDAP_FILTER_AND:
968 for (f=f->f_and; f; f=f->f_next) {
969 (void) oc_filter(f, cur, max);
979 static int search_candidates(
989 Filter f, scopef, rf, xf;
991 AttributeAssertion aa_ref;
992 #ifdef BDB_SUBENTRIES
994 AttributeAssertion aa_subentry;
998 AttributeAssertion aa_alias;
1002 * This routine takes as input a filter (user-filter)
1003 * and rewrites it as follows:
1004 * (&(scope=DN)[(objectClass=subentry)]
1005 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
1009 LDAP_LOG ( OPERATION, ENTRY,
1010 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1011 e->e_dn, (long) e->e_id, scope);
1013 Debug(LDAP_DEBUG_TRACE,
1014 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1015 e->e_dn, (long) e->e_id, scope );
1019 xf.f_choice = LDAP_FILTER_OR;
1022 /* If the user's filter uses objectClass=*,
1023 * these clauses are redundant.
1025 if (!oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
1026 if( !get_manageDSAit(op) ) { /* match referrals */
1027 struct berval bv_ref = { sizeof("referral")-1, "referral" };
1028 rf.f_choice = LDAP_FILTER_EQUALITY;
1030 rf.f_av_desc = slap_schema.si_ad_objectClass;
1031 rf.f_av_value = bv_ref;
1032 rf.f_next = xf.f_or;
1037 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
1038 struct berval bv_alias = { sizeof("alias")-1, "alias" };
1039 af.f_choice = LDAP_FILTER_EQUALITY;
1040 af.f_ava = &aa_alias;
1041 af.f_av_desc = slap_schema.si_ad_objectClass;
1042 af.f_av_value = bv_alias;
1043 af.f_next = xf.f_or;
1047 /* We added one of these clauses, filter depth increased */
1048 if( xf.f_or != filter ) depth++;
1052 f.f_choice = LDAP_FILTER_AND;
1054 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
1055 ? SLAPD_FILTER_DN_SUBTREE
1056 : SLAPD_FILTER_DN_ONE;
1057 scopef.f_dn = &e->e_nname;
1058 scopef.f_next = xf.f_or == filter ? filter : &xf ;
1059 /* Filter depth increased again, adding scope clause */
1062 #ifdef BDB_SUBENTRIES
1063 if( get_subentries_visibility( op ) ) {
1064 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
1065 sf.f_choice = LDAP_FILTER_EQUALITY;
1066 sf.f_ava = &aa_subentry;
1067 sf.f_av_desc = slap_schema.si_ad_objectClass;
1068 sf.f_av_value = bv_subentry;
1069 sf.f_next = scopef.f_next;
1070 scopef.f_next = &sf;
1074 /* Allocate IDL stack, plus 1 more for former tmp */
1075 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1077 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
1083 LDAP_LOG ( OPERATION, DETAIL1,
1084 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
1086 Debug(LDAP_DEBUG_TRACE,
1087 "bdb_search_candidates: failed (rc=%d)\n",
1093 LDAP_LOG ( OPERATION, DETAIL1,
1094 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1095 (long) ids[0], (long) BDB_IDL_FIRST(ids),
1096 (long) BDB_IDL_LAST(ids));
1098 Debug(LDAP_DEBUG_TRACE,
1099 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1101 (long) BDB_IDL_FIRST(ids),
1102 (long) BDB_IDL_LAST(ids) );
1109 #ifdef LDAP_CONTROL_PAGEDRESULTS
1111 send_pagerequest_response(
1118 LDAPControl ctrl, *ctrls[2];
1120 struct berval *bvalp, cookie = { 0, NULL };
1121 PagedResultsCookie respcookie;
1124 LDAP_LOG ( OPERATION, ENTRY,
1125 "send_pagerequest_response: lastid: (0x%08lx) "
1126 "nentries: (0x%081x)\n",
1129 Debug(LDAP_DEBUG_ARGS, "send_pagerequest_response: lastid: (0x%08lx) "
1130 "nentries: (0x%081x)\n", lastid, nentries, NULL );
1133 ctrl.ldctl_value.bv_val = NULL;
1137 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1141 respcookie = ( PagedResultsCookie )lastid;
1142 conn->c_pagedresults_state.ps_cookie = respcookie;
1143 cookie.bv_len = sizeof( respcookie );
1144 cookie.bv_val = (char *)&respcookie;
1147 * FIXME: we should consider sending an estimate of the entries
1148 * left, after appropriate security check is done
1150 ber_printf( ber, "{iO}", tentries, &cookie );
1152 if ( ber_flatten( ber, &bvalp ) == LBER_ERROR ) {
1156 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1157 ctrls[0]->ldctl_value = ( *bvalp );
1158 ctrls[0]->ldctl_iscritical = 0;
1160 send_search_result( conn, op,
1162 NULL, NULL, NULL, ctrls, nentries );
1165 (void) ber_free( ber, 1 );
1166 if ( ctrls[0]->ldctl_value.bv_val ) {
1167 ch_free( ctrls[0]->ldctl_value.bv_val );
1170 #endif /* LDAP_CONTROL_PAGEDRESULTS */