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(
35 #endif /* LDAP_CONTROL_PAGEDRESULTS */
49 struct berval *filterstr,
53 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
55 const char *text = NULL;
58 ID candidates[BDB_IDL_UM_SIZE];
60 BerVarray v2refs = NULL;
61 Entry *matched = NULL;
62 struct berval realbase = { 0, NULL };
65 #ifdef LDAP_CONTROL_PAGEDRESULTS
68 #endif /* LDAP_CONTROL_PAGEDRESULTS */
70 #ifdef LDAP_CLIENT_UPDATE
71 Filter lcupf, csnfnot, csnfeq, csnfand, csnfge;
72 AttributeAssertion aa_ge, aa_eq;
74 struct berval entrycsn_bv = { 0, NULL };
75 struct berval latest_entrycsn_bv = { 0, NULL };
76 #endif /* LDAP_CLIENT_UPDATE */
78 struct slap_limits_set *limit = NULL;
85 LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
87 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
91 #ifdef LDAP_CLIENT_UPDATE
92 if ( op->o_clientupdate_type & SLAP_LCUP_PERSIST ) {
93 bdb_add_psearch_spec( be, conn, op, base, base, scope,
94 deref, slimit, tlimit, filter, filterstr, attrs, attrsonly );
100 manageDSAit = get_manageDSAit( op );
102 #ifdef LDAP_CONTROL_PAGEDRESULTS
103 pagedresults = get_pagedresults( op );
104 #endif /* LDAP_CONTROL_PAGEDRESULTS */
106 rc = LOCK_ID (bdb->bi_dbenv, &locker );
111 send_ldap_result( conn, op, rc=LDAP_OTHER,
112 NULL, "internal error", NULL, NULL );
116 if ( nbase->bv_len == 0 ) {
117 /* DIT root special case */
118 e = (Entry *) &slap_entry_root;
122 /* get entry with reader lock */
123 if ( deref & LDAP_DEREF_FINDING ) {
124 e = deref_dn_r( be, nbase-, &err, &matched, &text );
130 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
139 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
141 if (matched != NULL) {
142 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
144 send_ldap_result( conn, op, LDAP_BUSY,
145 NULL, "ldap server busy", NULL, NULL );
146 LOCK_ID_FREE (bdb->bi_dbenv, locker );
148 case DB_LOCK_DEADLOCK:
149 case DB_LOCK_NOTGRANTED:
153 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
155 if (matched != NULL) {
156 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
158 send_ldap_result( conn, op, rc=LDAP_OTHER,
159 NULL, "internal error", NULL, NULL );
160 LOCK_ID_FREE (bdb->bi_dbenv, locker );
165 struct berval matched_dn = { 0, NULL };
166 BerVarray refs = NULL;
168 if ( matched != NULL ) {
170 ber_dupbv( &matched_dn, &matched->e_name );
172 erefs = is_entry_referral( matched )
173 ? get_entry_referrals( be, conn, op, matched )
176 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
180 refs = referral_rewrite( erefs, &matched_dn,
182 ber_bvarray_free( erefs );
186 refs = referral_rewrite( default_referral,
190 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
191 matched_dn.bv_val, text, refs, NULL );
193 LOCK_ID_FREE (bdb->bi_dbenv, locker );
194 if ( refs ) ber_bvarray_free( refs );
195 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
199 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
200 /* entry is a referral, don't allow add */
201 struct berval matched_dn;
202 BerVarray erefs, refs;
204 ber_dupbv( &matched_dn, &e->e_name );
205 erefs = get_entry_referrals( be, conn, op, e );
208 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
212 refs = referral_rewrite( erefs, &matched_dn,
214 ber_bvarray_free( erefs );
218 LDAP_LOG ( OPERATION, RESULTS,
219 "bdb_search: entry is referral\n", 0, 0, 0 );
221 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
225 send_ldap_result( conn, op, LDAP_REFERRAL,
227 refs ? NULL : "bad referral object",
230 LOCK_ID_FREE (bdb->bi_dbenv, locker );
231 ber_bvarray_free( refs );
232 ber_memfree( matched_dn.bv_val );
236 /* if not root, get appropriate limits */
237 if ( be_isroot( be, &op->o_ndn ) ) {
240 ( void ) get_limits( be, &op->o_ndn, &limit );
243 /* The time/size limits come first because they require very little
244 * effort, so there's no chance the candidates are selected and then
245 * the request is not honored only because of time/size constraints */
247 /* if no time limit requested, use soft limit (unless root!) */
250 tlimit = -1; /* allow root to set no limit */
258 /* if no limit is required, use soft limit */
260 tlimit = limit->lms_t_soft;
262 /* if requested limit higher than hard limit, abort */
263 } else if ( tlimit > limit->lms_t_hard ) {
264 /* no hard limit means use soft instead */
265 if ( limit->lms_t_hard == 0
266 && limit->lms_t_soft > -1
267 && tlimit > limit->lms_t_soft ) {
268 tlimit = limit->lms_t_soft;
270 /* positive hard limit means abort */
271 } else if ( limit->lms_t_hard > 0 ) {
272 send_search_result( conn, op,
273 LDAP_ADMINLIMIT_EXCEEDED,
274 NULL, NULL, NULL, NULL, 0 );
279 /* negative hard limit means no limit */
282 /* if no limit is required, use soft limit */
284 slimit = limit->lms_s_soft;
286 /* if requested limit higher than hard limit, abort */
287 } else if ( slimit > limit->lms_s_hard ) {
288 /* no hard limit means use soft instead */
289 if ( limit->lms_s_hard == 0
290 && limit->lms_s_soft > -1
291 && slimit > limit->lms_s_soft ) {
292 slimit = limit->lms_s_soft;
294 /* positive hard limit means abort */
295 } else if ( limit->lms_s_hard > 0 ) {
296 send_search_result( conn, op,
297 LDAP_ADMINLIMIT_EXCEEDED,
298 NULL, NULL, NULL, NULL, 0 );
303 /* negative hard limit means no limit */
307 /* compute it anyway; root does not use it */
308 stoptime = op->o_time + tlimit;
310 /* select candidates */
311 if ( scope == LDAP_SCOPE_BASE ) {
312 rc = base_candidate( be, e, candidates );
315 BDB_IDL_ALL( bdb, candidates );
316 rc = search_candidates( be, op, e, filter,
317 scope, deref, candidates );
320 /* need normalized dn below */
321 ber_dupbv( &realbase, &e->e_nname );
323 /* start cursor at beginning of candidates.
327 if ( e != &slap_entry_root ) {
328 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
332 if ( candidates[0] == 0 ) {
334 LDAP_LOG ( OPERATION, RESULTS,
335 "bdb_search: no candidates\n", 0, 0, 0 );
337 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
341 send_search_result( conn, op,
343 NULL, NULL, NULL, NULL, 0 );
349 /* if not root and candidates exceed to-be-checked entries, abort */
350 if ( !isroot && limit->lms_s_unchecked != -1 ) {
351 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
352 send_search_result( conn, op,
353 LDAP_ADMINLIMIT_EXCEEDED,
354 NULL, NULL, NULL, NULL, 0 );
360 #ifdef LDAP_CONTROL_PAGEDRESULTS
361 if ( pagedresults ) {
362 if ( op->o_pagedresults_state.ps_cookie == 0 ) {
365 if ( op->o_pagedresults_size == 0 ) {
366 send_search_result( conn, op, LDAP_SUCCESS,
367 NULL, "search abandoned by pagedResult size=0",
371 for ( id = bdb_idl_first( candidates, &cursor );
372 id != NOID && id <= (ID)( op->o_pagedresults_state.ps_cookie );
373 id = bdb_idl_next( candidates, &cursor ) );
375 if ( cursor == NOID ) {
377 LDAP_LOG ( OPERATION, RESULTS,
378 "bdb_search: no paged results candidates\n",
381 Debug( LDAP_DEBUG_TRACE,
382 "bdb_search: no paged results candidates\n",
385 send_pagerequest_response( conn, op, lastid, 0 );
392 #endif /* LDAP_CONTROL_PAGEDRESULTS */
394 #ifdef LDAP_CLIENT_UPDATE
395 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
396 lcupf.f_choice = LDAP_FILTER_AND;
397 lcupf.f_and = &csnfnot;
400 csnfnot.f_choice = LDAP_FILTER_NOT;
401 csnfnot.f_not = &csnfeq;
402 csnfnot.f_next = &csnfand;
404 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
405 csnfeq.f_ava = &aa_eq;
406 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
407 ber_dupbv( &csnfeq.f_av_value, &op->o_clientupdate_state );
409 csnfand.f_choice = LDAP_FILTER_AND;
410 csnfand.f_and = &csnfge;
411 csnfand.f_next = NULL;
413 csnfge.f_choice = LDAP_FILTER_GE;
414 csnfge.f_ava = &aa_ge;
415 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
416 ber_dupbv( &csnfge.f_av_value, &op->o_clientupdate_state );
417 csnfge.f_next = filter;
419 #endif /* LDAP_CLIENT_UPDATE */
421 for ( id = bdb_idl_first( candidates, &cursor );
423 id = bdb_idl_next( candidates, &cursor ) )
428 #ifdef LDAP_CONTROL_PAGEDRESULTS
430 #endif /* LDAP_CONTROL_PAGEDRESULTS */
432 /* check for abandon */
433 if ( op->o_abandon ) {
438 /* check time limit */
439 if ( tlimit != -1 && slap_get_time() > stoptime ) {
440 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
441 NULL, NULL, v2refs, NULL, nentries );
446 /* get the entry with reader lock */
447 rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
449 if (rc == LDAP_BUSY) {
450 send_ldap_result( conn, op, rc=LDAP_BUSY,
451 NULL, "ldap server busy", NULL, NULL );
454 } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
459 if( !BDB_IDL_IS_RANGE(candidates) ) {
460 /* only complain for non-range IDLs */
462 LDAP_LOG ( OPERATION, RESULTS,
463 "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
465 Debug( LDAP_DEBUG_TRACE,
466 "bdb_search: candidate %ld not found\n",
474 #ifdef BDB_SUBENTRIES
475 if ( is_entry_subentry( e ) ) {
476 if( scope != LDAP_SCOPE_BASE ) {
477 if(!get_subentries_visibility( op )) {
478 /* only subentries are visible */
482 } else if ( get_subentries( op ) &&
483 !get_subentries_visibility( op ))
485 /* only subentries are visible */
489 } else if ( get_subentries_visibility( op )) {
490 /* only subentries are visible */
496 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
501 e = deref_entry_r( be, e, &err, &matched, &text );
508 if( e->e_id == id ) {
513 /* need to skip alias which deref into scope */
514 if( scope & LDAP_SCOPE_ONELEVEL ) {
517 dnParent( &e->e_nname, &pdn ):
518 if ( ber_bvcmp( pdn, &realbase ) ) {
522 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
523 /* alias is within scope */
525 LDAP_LOG ( OPERATION, RESULTS,
526 "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
528 Debug( LDAP_DEBUG_TRACE,
529 "bdb_search: \"%s\" in subtree\n",
540 * if it's a referral, add it to the list of referrals. only do
541 * this for non-base searches, and don't check the filter
542 * explicitly here since it's only a candidate anyway.
544 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
545 is_entry_referral( e ) )
550 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
551 if ( !be_issuffix( be, &e->e_nname ) ) {
552 dnParent( &e->e_nname, &dn );
553 scopeok = dn_match( &dn, &realbase );
555 scopeok = (realbase.bv_len == 0);
558 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
559 scopeok = dnIsSuffix( &e->e_nname, &realbase );
566 BerVarray erefs = get_entry_referrals(
568 BerVarray refs = referral_rewrite( erefs,
570 scope == LDAP_SCOPE_SUBTREE
574 send_search_reference( be, conn, op,
575 e, refs, NULL, &v2refs );
577 ber_bvarray_free( refs );
581 LDAP_LOG(OPERATION, DETAIL2,
582 "bdb_search: candidate referral %ld scope not okay\n",
585 Debug( LDAP_DEBUG_TRACE,
586 "bdb_search: candidate referral %ld scope not okay\n",
594 /* if it matches the filter and scope, send it */
595 #ifdef LDAP_CLIENT_UPDATE
596 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
597 rc = test_filter( be, conn, op, e, &lcupf );
599 #endif /* LDAP_CLIENT_UPDATE */
601 rc = test_filter( be, conn, op, e, filter );
604 if ( rc == LDAP_COMPARE_TRUE ) {
608 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
609 if ( be_issuffix( be, &e->e_nname ) ) {
610 scopeok = (realbase.bv_len == 0);
612 dnParent( &e->e_nname, &dn );
613 scopeok = dn_match( &dn, &realbase );
616 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
617 scopeok = dnIsSuffix( &e->e_nname, &realbase );
624 /* check size limit */
625 if ( --slimit == -1 ) {
626 bdb_cache_return_entry_r( bdb->bi_dbenv,
627 &bdb->bi_cache, e, &lock );
629 send_search_result( conn, op,
630 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
631 v2refs, NULL, nentries );
634 #ifdef LDAP_CONTROL_PAGEDRESULTS
635 if ( pagedresults ) {
636 if ( nentries >= op->o_pagedresults_size ) {
637 send_pagerequest_response( conn, op, lastid, nentries );
642 #endif /* LDAP_CONTROL_PAGEDRESULTS */
647 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
653 #ifdef LDAP_CLIENT_UPDATE
654 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
658 const char *text = NULL;
659 LDAPControl *ctrls[2];
662 BerElement *ber = ber_alloc_t( LBER_USE_DER );
666 LDAP_LOG ( OPERATION, RESULTS,
667 "bdb_search: ber_alloc_t failed\n",
670 Debug( LDAP_DEBUG_TRACE,
671 "bdb_search: ber_alloc_t failed\n",
674 send_ldap_result( conn, op, rc=LDAP_OTHER,
675 NULL, "internal error", NULL, NULL );
681 ctrls[0] = ch_malloc ( sizeof ( LDAPControl ) );
684 if ( entry_count % op->o_clientupdate_interval == 0 ) {
686 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
687 AttributeDescription *desc = a->a_desc;
688 if ( desc == slap_schema.si_ad_entryCSN ) {
689 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
690 if ( latest_entrycsn_bv.bv_val == NULL ) {
691 ber_dupbv( &latest_entrycsn_bv, &entrycsn_bv );
693 res = value_match( &ret, desc,
694 desc->ad_type->sat_ordering,
695 SLAP_MR_ASSERTION_SYNTAX_MATCH,
696 &entrycsn_bv, &latest_entrycsn_bv, &text );
697 if ( res != LDAP_SUCCESS ) {
700 LDAP_LOG ( OPERATION, RESULTS,
701 "bdb_search: value_match failed\n",
704 Debug( LDAP_DEBUG_TRACE,
705 "bdb_search: value_match failed\n",
711 ch_free( latest_entrycsn_bv.bv_val );
712 latest_entrycsn_bv.bv_val = NULL;
713 ber_dupbv( &latest_entrycsn_bv,
722 SLAP_LCUP_STATE_UPDATE_FALSE,
723 SLAP_LCUP_ENTRY_DELETED_FALSE,
724 LCUP_COOKIE_OID, &entrycsn_bv );
726 ch_free( entrycsn_bv.bv_val );
727 entrycsn_bv.bv_val = NULL;
730 /* Do not send cookie */
733 SLAP_LCUP_STATE_UPDATE_FALSE,
734 SLAP_LCUP_ENTRY_DELETED_FALSE );
737 ctrls[0]->ldctl_oid = LDAP_CONTROL_ENTRY_UPDATE;
738 ctrls[0]->ldctl_iscritical = op->o_clientupdate;
739 ret = ber_flatten( ber, &bv );
743 LDAP_LOG ( OPERATION, RESULTS,
744 "bdb_search: ber_flatten failed\n",
747 Debug( LDAP_DEBUG_TRACE,
748 "bdb_search: ber_flatten failed\n",
751 send_ldap_result( conn, op, rc=LDAP_OTHER,
752 NULL, "internal error", NULL, NULL );
756 ber_dupbv( &ctrls[0]->ldctl_value, bv );
758 result = send_search_entry( be, conn, op,
759 e, attrs, attrsonly, ctrls);
761 ch_free( ctrls[0]->ldctl_value.bv_val );
766 #endif /* LDAP_CLIENT_UPDATE */
768 result = send_search_entry( be, conn, op,
769 e, attrs, attrsonly, NULL);
774 case 0: /* entry sent ok */
777 case 1: /* entry not sent */
779 case -1: /* connection closed */
780 bdb_cache_return_entry_r(bdb->bi_dbenv,
781 &bdb->bi_cache, e, &lock);
789 LDAP_LOG ( OPERATION, RESULTS,
790 "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
792 Debug( LDAP_DEBUG_TRACE,
793 "bdb_search: %ld scope not okay\n",
799 LDAP_LOG ( OPERATION, RESULTS,
800 "bdb_search: %ld does match filter\n", (long) id, 0, 0);
802 Debug( LDAP_DEBUG_TRACE,
803 "bdb_search: %ld does match filter\n",
810 /* free reader lock */
811 bdb_cache_return_entry_r( bdb->bi_dbenv,
812 &bdb->bi_cache, e , &lock);
816 ldap_pvt_thread_yield();
819 #ifdef LDAP_CLIENT_UPDATE
820 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
822 LDAPControl *ctrls[2];
823 BerElement *ber = ber_alloc_t( LBER_USE_DER );
828 LDAP_LOG ( OPERATION, RESULTS,
829 "bdb_search: ber_alloc_t failed\n", 0, 0, 0 );
831 Debug( LDAP_DEBUG_TRACE, "bdb_search: ber_alloc_t failed\n",
834 send_ldap_result( conn, op, rc=LDAP_OTHER,
835 NULL, "internal error", NULL, NULL );
839 ctrls[0] = ch_malloc ( sizeof ( LDAPControl ) );
842 ber_printf( ber, "{sO", LCUP_COOKIE_OID, &latest_entrycsn_bv );
843 ber_printf( ber, "N}" );
845 ctrls[0]->ldctl_oid = LDAP_CONTROL_CLIENT_UPDATE_DONE;
846 ctrls[0]->ldctl_iscritical = op->o_clientupdate;
847 ret = ber_flatten( ber, &bv );
851 LDAP_LOG ( OPERATION, RESULTS,
852 "bdb_search: ber_flatten failed\n", 0, 0, 0 );
854 Debug( LDAP_DEBUG_TRACE, "bdb_search: ber_flatten failed\n",
857 send_ldap_result( conn, op, rc=LDAP_OTHER,
858 NULL, "internal error", NULL, NULL );
862 ber_dupbv( &ctrls[0]->ldctl_value, bv );
864 send_search_result( conn, op,
865 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
866 NULL, NULL, v2refs, ctrls, nentries );
868 ch_free( latest_entrycsn_bv.bv_val );
869 latest_entrycsn_bv.bv_val = NULL;
870 ch_free( ctrls[0]->ldctl_value.bv_val );
875 #endif /* LDAP_CLIENT_UPDATE */
877 send_search_result( conn, op,
878 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
879 NULL, NULL, v2refs, NULL, nentries );
886 /* free reader lock */
887 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
890 #ifdef LDAP_CLIENT_UPDATE
891 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
892 if ( csnfeq.f_ava != NULL && csnfeq.f_av_value.bv_val != NULL ) {
893 ch_free( csnfeq.f_av_value.bv_val );
896 if ( csnfge.f_ava != NULL && csnfge.f_av_value.bv_val != NULL ) {
897 ch_free( csnfge.f_av_value.bv_val );
900 #endif /* LDAP_CLIENT_UPDATE */
902 LOCK_ID_FREE (bdb->bi_dbenv, locker );
904 if( v2refs ) ber_bvarray_free( v2refs );
905 if( realbase.bv_val ) ch_free( realbase.bv_val );
911 static int base_candidate(
917 LDAP_LOG ( OPERATION, ENTRY,
918 "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
920 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
921 e->e_dn, (long) e->e_id, 0);
929 /* Look for "objectClass Present" in this filter.
930 * Also count depth of filter tree while we're at it.
932 static int oc_filter(
940 if( cur > *max ) *max = cur;
942 switch(f->f_choice) {
943 case LDAP_FILTER_PRESENT:
944 if (f->f_desc == slap_schema.si_ad_objectClass) {
949 case LDAP_FILTER_AND:
952 for (f=f->f_and; f; f=f->f_next) {
953 (void) oc_filter(f, cur, max);
963 static int search_candidates(
973 Filter f, scopef, rf, xf;
975 AttributeAssertion aa_ref;
976 #ifdef BDB_SUBENTRIES
978 AttributeAssertion aa_subentry;
982 AttributeAssertion aa_alias;
986 * This routine takes as input a filter (user-filter)
987 * and rewrites it as follows:
988 * (&(scope=DN)[(objectClass=subentry)]
989 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
993 LDAP_LOG ( OPERATION, ENTRY,
994 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
995 e->e_dn, (long) e->e_id, scope);
997 Debug(LDAP_DEBUG_TRACE,
998 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
999 e->e_dn, (long) e->e_id, scope );
1003 xf.f_choice = LDAP_FILTER_OR;
1006 /* If the user's filter uses objectClass=*,
1007 * these clauses are redundant.
1009 if (!oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
1010 if( !get_manageDSAit(op) ) { /* match referrals */
1011 struct berval bv_ref = { sizeof("referral")-1, "referral" };
1012 rf.f_choice = LDAP_FILTER_EQUALITY;
1014 rf.f_av_desc = slap_schema.si_ad_objectClass;
1015 rf.f_av_value = bv_ref;
1016 rf.f_next = xf.f_or;
1021 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
1022 struct berval bv_alias = { sizeof("alias")-1, "alias" };
1023 af.f_choice = LDAP_FILTER_EQUALITY;
1024 af.f_ava = &aa_alias;
1025 af.f_av_desc = slap_schema.si_ad_objectClass;
1026 af.f_av_value = bv_alias;
1027 af.f_next = xf.f_or;
1031 /* We added one of these clauses, filter depth increased */
1032 if( xf.f_or != filter ) depth++;
1036 f.f_choice = LDAP_FILTER_AND;
1038 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
1039 ? SLAPD_FILTER_DN_SUBTREE
1040 : SLAPD_FILTER_DN_ONE;
1041 scopef.f_dn = &e->e_nname;
1042 scopef.f_next = xf.f_or == filter ? filter : &xf ;
1043 /* Filter depth increased again, adding scope clause */
1046 #ifdef BDB_SUBENTRIES
1047 if( get_subentries_visibility( op ) ) {
1048 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
1049 sf.f_choice = LDAP_FILTER_EQUALITY;
1050 sf.f_ava = &aa_subentry;
1051 sf.f_av_desc = slap_schema.si_ad_objectClass;
1052 sf.f_av_value = bv_subentry;
1053 sf.f_next = scopef.f_next;
1054 scopef.f_next = &sf;
1058 /* Allocate IDL stack, plus 1 more for former tmp */
1059 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1061 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
1067 LDAP_LOG ( OPERATION, DETAIL1,
1068 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
1070 Debug(LDAP_DEBUG_TRACE,
1071 "bdb_search_candidates: failed (rc=%d)\n",
1077 LDAP_LOG ( OPERATION, DETAIL1,
1078 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1079 (long) ids[0], (long) BDB_IDL_FIRST(ids),
1080 (long) BDB_IDL_LAST(ids));
1082 Debug(LDAP_DEBUG_TRACE,
1083 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1085 (long) BDB_IDL_FIRST(ids),
1086 (long) BDB_IDL_LAST(ids) );
1093 #ifdef LDAP_CONTROL_PAGEDRESULTS
1095 send_pagerequest_response(
1101 LDAPControl ctrl, *ctrls[2];
1103 struct berval *bvalp, cookie = { 0, NULL };
1104 PagedResultsCookie respcookie;
1107 LDAP_LOG ( OPERATION, ENTRY,
1108 "send_pagerequest_response: lastid: (0x%08lx) "
1109 "nentries: (0x%081x)\n",
1112 Debug(LDAP_DEBUG_ARGS, "send_pagerequest_response: lastid: (0x%08lx) "
1113 "nentries: (0x%081x)\n", lastid, nentries, NULL );
1116 ctrl.ldctl_value.bv_val = NULL;
1120 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1124 respcookie = ( PagedResultsCookie )lastid;
1125 conn->c_pagedresults_state.ps_cookie = respcookie;
1126 cookie.bv_len = sizeof( respcookie );
1127 cookie.bv_val = (char *)&respcookie;
1130 * FIXME: we should consider sending an estimate of the entries
1131 * left, after appropriate security check is done
1133 ber_printf( ber, "{iO}", 0, &cookie );
1135 if ( ber_flatten( ber, &bvalp ) == LBER_ERROR ) {
1139 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1140 ctrls[0]->ldctl_value = ( *bvalp );
1141 ctrls[0]->ldctl_iscritical = 0;
1143 send_search_result( conn, op,
1145 NULL, NULL, NULL, ctrls, nentries );
1148 if ( ctrls[0]->ldctl_value.bv_val ) {
1149 ch_free( ctrls[0]->ldctl_value.bv_val );
1152 #endif /* LDAP_CONTROL_PAGEDRESULTS */