1 /* search.c - ldbm backend search function */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
19 static ID_BLOCK *base_candidate(
20 Backend *be, Entry *e );
22 static ID_BLOCK *search_candidates(
23 Backend *be, Entry *e, Filter *filter,
24 int scope, int deref, int manageDSAit );
39 struct berval *filterstr,
43 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
45 const char *text = NULL;
50 BerVarray v2refs = NULL;
51 Entry *matched = NULL;
52 struct berval realbase = { 0, NULL };
54 int manageDSAit = get_manageDSAit( op );
55 int cscope = LDAP_SCOPE_DEFAULT;
57 struct slap_limits_set *limit = NULL;
61 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_search: enter\n", 0, 0, 0 );
63 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
66 /* grab giant lock for reading */
67 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
69 if ( nbase->bv_len == 0 ) {
70 /* DIT root special case */
71 e = (Entry *) &slap_entry_root;
73 /* need normalized dn below */
74 ber_dupbv( &realbase, &e->e_nname );
76 candidates = search_candidates( be, e, filter,
77 scope, deref, manageDSAit );
81 } else if ( deref & LDAP_DEREF_FINDING ) {
82 /* deref dn and get entry with reader lock */
83 e = deref_dn_r( be, nbase, &err, &matched, &text );
85 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
88 /* get entry with reader lock */
89 e = dn2entry_r( be, nbase, &matched );
90 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
95 struct berval matched_dn = { 0, NULL };
96 BerVarray refs = NULL;
98 if ( matched != NULL ) {
100 ber_dupbv( &matched_dn, &matched->e_name );
102 erefs = is_entry_referral( matched )
103 ? get_entry_referrals( be, conn, op, matched )
106 cache_return_entry_r( &li->li_cache, matched );
109 refs = referral_rewrite( erefs, &matched_dn,
112 ber_bvarray_free( erefs );
116 refs = referral_rewrite( default_referral,
120 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
122 send_ldap_result( conn, op, err, matched_dn.bv_val,
125 ber_bvarray_free( refs );
126 ber_memfree( matched_dn.bv_val );
130 if (!manageDSAit && is_entry_referral( e ) ) {
131 /* entry is a referral, don't allow add */
132 struct berval matched_dn;
136 ber_dupbv( &matched_dn, &e->e_name );
137 erefs = get_entry_referrals( be, conn, op, e );
140 cache_return_entry_r( &li->li_cache, e );
141 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
144 LDAP_LOG( BACK_LDBM, INFO,
145 "ldbm_search: entry (%s) is a referral.\n",
148 Debug( LDAP_DEBUG_TRACE,
149 "ldbm_search: entry is referral\n",
154 refs = referral_rewrite( erefs, &matched_dn,
157 ber_bvarray_free( erefs );
161 send_ldap_result( conn, op, LDAP_REFERRAL,
162 matched_dn.bv_val, NULL, refs, NULL );
163 ber_bvarray_free( refs );
166 send_ldap_result( conn, op, LDAP_OTHER,
168 "bad referral object", NULL, NULL );
171 ber_memfree( matched_dn.bv_val );
175 if ( is_entry_alias( e ) ) {
177 deref = LDAP_DEREF_NEVER;
180 if ( scope == LDAP_SCOPE_BASE ) {
181 cscope = LDAP_SCOPE_BASE;
182 candidates = base_candidate( be, e );
185 cscope = ( scope != LDAP_SCOPE_SUBTREE )
186 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
187 candidates = search_candidates( be, e, filter,
188 scope, deref, manageDSAit );
191 /* need normalized dn below */
192 ber_dupbv( &realbase, &e->e_nname );
194 cache_return_entry_r( &li->li_cache, e );
197 if ( candidates == NULL ) {
200 LDAP_LOG( BACK_LDBM, INFO,
201 "ldbm_search: no candidates\n" , 0, 0, 0);
203 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
207 send_search_result( conn, op,
209 NULL, NULL, NULL, NULL, 0 );
215 /* if not root, get appropriate limits */
216 if ( be_isroot( be, &op->o_ndn ) ) {
219 ( void ) get_limits( be, &op->o_ndn, &limit );
222 /* if candidates exceed to-be-checked entries, abort */
223 if ( !isroot && limit->lms_s_unchecked != -1 ) {
224 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
225 send_search_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
226 NULL, NULL, NULL, NULL, 0 );
232 /* if root an no specific limit is required, allow unlimited search */
243 /* if no limit is required, use soft limit */
245 tlimit = limit->lms_t_soft;
247 /* if requested limit higher than hard limit, abort */
248 } else if ( tlimit > limit->lms_t_hard ) {
249 /* no hard limit means use soft instead */
250 if ( limit->lms_t_hard == 0
251 && limit->lms_t_soft > -1
252 && tlimit > limit->lms_t_soft ) {
253 tlimit = limit->lms_t_soft;
255 /* positive hard limit means abort */
256 } else if ( limit->lms_t_hard > 0 ) {
257 send_search_result( conn, op,
258 LDAP_ADMINLIMIT_EXCEEDED,
259 NULL, NULL, NULL, NULL, 0 );
264 /* negative hard limit means no limit */
267 /* if no limit is required, use soft limit */
269 slimit = limit->lms_s_soft;
271 /* if requested limit higher than hard limit, abort */
272 } else if ( slimit > limit->lms_s_hard ) {
273 /* no hard limit means use soft instead */
274 if ( limit->lms_s_hard == 0
275 && limit->lms_s_soft > -1
276 && slimit > limit->lms_s_soft ) {
277 slimit = limit->lms_s_soft;
279 /* positive hard limit means abort */
280 } else if ( limit->lms_s_hard > 0 ) {
281 send_search_result( conn, op,
282 LDAP_ADMINLIMIT_EXCEEDED,
283 NULL, NULL, NULL, NULL, 0 );
288 /* negative hard limit means no limit */
292 /* compute it anyway; root does not use it */
293 stoptime = op->o_time + tlimit;
295 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
296 id = idl_nextid( candidates, &cursor ) )
301 /* check for abandon */
302 if ( op->o_abandon ) {
307 /* check time limit */
308 if ( tlimit != -1 && slap_get_time() > stoptime ) {
309 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
310 NULL, NULL, v2refs, NULL, nentries );
315 /* get the entry with reader lock */
316 e = id2entry_r( be, id );
320 LDAP_LOG( BACK_LDBM, INFO,
321 "ldbm_search: candidate %ld not found.\n", id, 0, 0 );
323 Debug( LDAP_DEBUG_TRACE,
324 "ldbm_search: candidate %ld not found\n",
331 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
336 e = deref_entry_r( be, e, &err, &matched, &text );
343 if( e->e_id == id ) {
348 /* need to skip alias which deref into scope */
349 if( scope & LDAP_SCOPE_ONELEVEL ) {
351 dnParent( &e->e_nname, &pdn );
352 if ( ber_bvcmp( &pdn, &realbase ) ) {
356 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
357 /* alias is within scope */
359 LDAP_LOG( BACK_LDBM, DETAIL1,
360 "ldbm_search: alias \"%s\" in subtree\n", e->e_dn, 0, 0 );
362 Debug( LDAP_DEBUG_TRACE,
363 "ldbm_search: alias \"%s\" in subtree\n",
374 * if it's a referral, add it to the list of referrals. only do
375 * this for non-base searches, and don't check the filter
376 * explicitly here since it's only a candidate anyway.
378 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
379 is_entry_referral( e ) )
384 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
385 if ( !be_issuffix( be, &e->e_nname ) ) {
386 dnParent( &e->e_nname, &dn );
387 scopeok = dn_match( &dn, &realbase );
389 scopeok = (realbase.bv_len == 0);
392 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
393 scopeok = dnIsSuffix( &e->e_nname, &realbase );
400 BerVarray erefs = get_entry_referrals(
402 BerVarray refs = referral_rewrite( erefs,
404 scope == LDAP_SCOPE_SUBTREE
408 send_search_reference( be, conn, op,
409 e, refs, NULL, &v2refs );
411 ber_bvarray_free( refs );
415 LDAP_LOG( BACK_LDBM, DETAIL2,
416 "ldbm_search: candidate referral %ld scope not okay\n",
419 Debug( LDAP_DEBUG_TRACE,
420 "ldbm_search: candidate referral %ld scope not okay\n",
428 /* if it matches the filter and scope, send it */
429 result = test_filter( be, conn, op, e, filter );
431 if ( result == LDAP_COMPARE_TRUE ) {
435 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
436 if ( !be_issuffix( be, &e->e_nname ) ) {
437 dnParent( &e->e_nname, &dn );
438 scopeok = dn_match( &dn, &realbase );
440 scopeok = (realbase.bv_len == 0);
443 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
444 scopeok = dnIsSuffix( &e->e_nname, &realbase );
451 /* check size limit */
452 if ( --slimit == -1 ) {
453 cache_return_entry_r( &li->li_cache, e );
454 send_search_result( conn, op,
455 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
456 v2refs, NULL, nentries );
462 result = send_search_entry(be, conn, op,
463 e, attrs, attrsonly, NULL);
466 case 0: /* entry sent ok */
469 case 1: /* entry not sent */
471 case -1: /* connection closed */
472 cache_return_entry_r( &li->li_cache, e );
479 LDAP_LOG( BACK_LDBM, DETAIL2,
480 "ldbm_search: candidate entry %ld scope not okay\n",
483 Debug( LDAP_DEBUG_TRACE,
484 "ldbm_search: candidate entry %ld scope not okay\n",
491 LDAP_LOG( BACK_LDBM, DETAIL2,
492 "ldbm_search: candidate entry %ld does not match filter\n",
495 Debug( LDAP_DEBUG_TRACE,
496 "ldbm_search: candidate entry %ld does not match filter\n",
503 /* free reader lock */
504 cache_return_entry_r( &li->li_cache, e );
507 ldap_pvt_thread_yield();
510 send_search_result( conn, op,
511 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
512 NULL, NULL, v2refs, NULL, nentries );
517 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
519 if( candidates != NULL )
520 idl_free( candidates );
522 if( v2refs ) ber_bvarray_free( v2refs );
523 if( realbase.bv_val ) free( realbase.bv_val );
536 LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
538 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
543 idl = idl_alloc( 1 );
544 idl_insert( &idl, e->e_id, 1 );
558 ID_BLOCK *candidates;
559 Filter f, fand, rf, af, xf;
560 AttributeAssertion aa_ref, aa_alias;
561 struct berval bv_ref = { sizeof("referral")-1, "referral" };
562 struct berval bv_alias = { sizeof("alias")-1, "alias" };
565 LDAP_LOG( BACK_LDBM, DETAIL1,
566 "search_candidates: base (%s) scope %d deref %d\n",
567 e->e_ndn, scope, deref );
569 Debug(LDAP_DEBUG_TRACE,
570 "search_candidates: base=\"%s\" s=%d d=%d\n",
571 e->e_ndn, scope, deref );
576 xf.f_choice = LDAP_FILTER_OR;
580 /* match referrals */
581 rf.f_choice = LDAP_FILTER_EQUALITY;
583 rf.f_av_desc = slap_schema.si_ad_objectClass;
584 rf.f_av_value = bv_ref;
589 if( deref & LDAP_DEREF_SEARCHING ) {
591 af.f_choice = LDAP_FILTER_EQUALITY;
592 af.f_ava = &aa_alias;
593 af.f_av_desc = slap_schema.si_ad_objectClass;
594 af.f_av_value = bv_alias;
600 f.f_choice = LDAP_FILTER_AND;
602 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
603 ? SLAPD_FILTER_DN_SUBTREE
604 : SLAPD_FILTER_DN_ONE;
605 fand.f_dn = &e->e_nname;
606 fand.f_next = xf.f_or == filter ? filter : &xf ;
608 candidates = filter_candidates( be, &f );
610 return( candidates );