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;
60 #ifdef SLAP_X_FILTER_HASSUBORDINATES
61 int filter_hasSubordinates = 0;
62 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
65 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_search: enter\n", 0, 0, 0 );
67 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
70 /* grab giant lock for reading */
71 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
73 if ( nbase->bv_len == 0 ) {
74 /* DIT root special case */
75 e = (Entry *) &slap_entry_root;
77 /* need normalized dn below */
78 ber_dupbv( &realbase, &e->e_nname );
80 candidates = search_candidates( be, e, filter,
81 scope, deref, manageDSAit );
85 } else if ( deref & LDAP_DEREF_FINDING ) {
86 /* deref dn and get entry with reader lock */
87 e = deref_dn_r( be, nbase, &err, &matched, &text );
89 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
92 /* get entry with reader lock */
93 e = dn2entry_r( be, nbase, &matched );
94 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
99 struct berval matched_dn = { 0, NULL };
100 BerVarray refs = NULL;
102 if ( matched != NULL ) {
104 ber_dupbv( &matched_dn, &matched->e_name );
106 erefs = is_entry_referral( matched )
107 ? get_entry_referrals( be, conn, op, matched )
110 cache_return_entry_r( &li->li_cache, matched );
113 refs = referral_rewrite( erefs, &matched_dn,
116 ber_bvarray_free( erefs );
120 refs = referral_rewrite( default_referral,
124 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
126 send_ldap_result( conn, op, err, matched_dn.bv_val,
129 ber_bvarray_free( refs );
130 ber_memfree( matched_dn.bv_val );
134 if (!manageDSAit && is_entry_referral( e ) ) {
135 /* entry is a referral, don't allow add */
136 struct berval matched_dn;
140 ber_dupbv( &matched_dn, &e->e_name );
141 erefs = get_entry_referrals( be, conn, op, e );
144 cache_return_entry_r( &li->li_cache, e );
145 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
148 LDAP_LOG( BACK_LDBM, INFO,
149 "ldbm_search: entry (%s) is a referral.\n",
152 Debug( LDAP_DEBUG_TRACE,
153 "ldbm_search: entry is referral\n",
158 refs = referral_rewrite( erefs, &matched_dn,
161 ber_bvarray_free( erefs );
165 send_ldap_result( conn, op, LDAP_REFERRAL,
166 matched_dn.bv_val, NULL, refs, NULL );
167 ber_bvarray_free( refs );
170 send_ldap_result( conn, op, LDAP_OTHER,
172 "bad referral object", NULL, NULL );
175 ber_memfree( matched_dn.bv_val );
179 if ( is_entry_alias( e ) ) {
181 deref = LDAP_DEREF_NEVER;
184 if ( scope == LDAP_SCOPE_BASE ) {
185 cscope = LDAP_SCOPE_BASE;
186 candidates = base_candidate( be, e );
189 cscope = ( scope != LDAP_SCOPE_SUBTREE )
190 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
191 candidates = search_candidates( be, e, filter,
192 scope, deref, manageDSAit );
195 /* need normalized dn below */
196 ber_dupbv( &realbase, &e->e_nname );
198 cache_return_entry_r( &li->li_cache, e );
201 if ( candidates == NULL ) {
204 LDAP_LOG( BACK_LDBM, INFO,
205 "ldbm_search: no candidates\n" , 0, 0, 0);
207 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
211 send_search_result( conn, op,
213 NULL, NULL, NULL, NULL, 0 );
219 /* if not root, get appropriate limits */
220 if ( be_isroot( be, &op->o_ndn ) ) {
223 ( void ) get_limits( be, &op->o_ndn, &limit );
226 /* if candidates exceed to-be-checked entries, abort */
227 if ( !isroot && limit->lms_s_unchecked != -1 ) {
228 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
229 send_search_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
230 NULL, NULL, NULL, NULL, 0 );
236 /* if root an no specific limit is required, allow unlimited search */
247 /* if no limit is required, use soft limit */
249 tlimit = limit->lms_t_soft;
251 /* if requested limit higher than hard limit, abort */
252 } else if ( tlimit > limit->lms_t_hard ) {
253 /* no hard limit means use soft instead */
254 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
255 tlimit = limit->lms_t_soft;
257 /* positive hard limit means abort */
258 } else if ( limit->lms_t_hard > 0 ) {
259 send_search_result( conn, op,
260 LDAP_UNWILLING_TO_PERFORM,
261 NULL, NULL, NULL, NULL, 0 );
266 /* negative hard limit means no limit */
269 /* if no limit is required, use soft limit */
271 slimit = limit->lms_s_soft;
273 /* if requested limit higher than hard limit, abort */
274 } else if ( slimit > limit->lms_s_hard ) {
275 /* no hard limit means use soft instead */
276 if ( limit->lms_s_hard == 0 && 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_UNWILLING_TO_PERFORM,
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 #ifdef SLAP_X_FILTER_HASSUBORDINATES
297 * is hasSubordinates used in the filter ?
298 * FIXME: we may compute this directly when parsing the filter
300 filter_hasSubordinates = filter_has_subordinates( filter );
301 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
303 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
304 id = idl_nextid( candidates, &cursor ) )
308 #ifdef SLAP_X_FILTER_HASSUBORDINATES
309 Attribute *hasSubordinates = NULL;
310 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
312 /* check for abandon */
313 if ( op->o_abandon ) {
318 /* check time limit */
319 if ( tlimit != -1 && slap_get_time() > stoptime ) {
320 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
321 NULL, NULL, v2refs, NULL, nentries );
326 /* get the entry with reader lock */
327 e = id2entry_r( be, id );
331 LDAP_LOG( BACK_LDBM, INFO,
332 "ldbm_search: candidate %ld not found.\n", id, 0, 0 );
334 Debug( LDAP_DEBUG_TRACE,
335 "ldbm_search: candidate %ld not found\n",
342 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
347 e = deref_entry_r( be, e, &err, &matched, &text );
354 if( e->e_id == id ) {
359 /* need to skip alias which deref into scope */
360 if( scope & LDAP_SCOPE_ONELEVEL ) {
362 dnParent( &e->e_nname, &pdn );
363 if ( ber_bvcmp( &pdn, &realbase ) ) {
367 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
368 /* alias is within scope */
370 LDAP_LOG( BACK_LDBM, DETAIL1,
371 "ldbm_search: alias \"%s\" in subtree\n", e->e_dn, 0, 0 );
373 Debug( LDAP_DEBUG_TRACE,
374 "ldbm_search: alias \"%s\" in subtree\n",
385 * if it's a referral, add it to the list of referrals. only do
386 * this for non-base searches, and don't check the filter
387 * explicitly here since it's only a candidate anyway.
389 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
390 is_entry_referral( e ) )
395 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
396 if ( !be_issuffix( be, &e->e_nname ) ) {
397 dnParent( &e->e_nname, &dn );
398 scopeok = dn_match( &dn, &realbase );
400 scopeok = (realbase.bv_len == 0);
403 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
404 scopeok = dnIsSuffix( &e->e_nname, &realbase );
411 BerVarray erefs = get_entry_referrals(
413 BerVarray refs = referral_rewrite( erefs,
415 scope == LDAP_SCOPE_SUBTREE
419 send_search_reference( be, conn, op,
420 e, refs, NULL, &v2refs );
422 ber_bvarray_free( refs );
426 LDAP_LOG( BACK_LDBM, DETAIL2,
427 "ldbm_search: candidate referral %ld scope not okay\n",
430 Debug( LDAP_DEBUG_TRACE,
431 "ldbm_search: candidate referral %ld scope not okay\n",
439 #ifdef SLAP_X_FILTER_HASSUBORDINATES
441 * if hasSubordinates is used in the filter,
442 * append it to the entry's attributes
444 if ( filter_hasSubordinates ) {
447 hs = has_children( be, e );
448 hasSubordinates = slap_operational_hasSubordinate( hs );
449 if ( hasSubordinates == NULL ) {
453 hasSubordinates->a_next = e->e_attrs;
454 e->e_attrs = hasSubordinates;
456 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
458 /* if it matches the filter and scope, send it */
459 result = test_filter( be, conn, op, e, filter );
461 #ifdef SLAP_X_FILTER_HASSUBORDINATES
462 if ( hasSubordinates ) {
464 * FIXME: this is fairly inefficient, because
465 * if hasSubordinates is among the required
466 * attrs, it will be added again later;
467 * maybe we should leave it and check
468 * check later if it's already present,
471 e->e_attrs = e->e_attrs->a_next;
472 attr_free( hasSubordinates );
474 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
476 if ( result == LDAP_COMPARE_TRUE ) {
480 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
481 if ( !be_issuffix( be, &e->e_nname ) ) {
482 dnParent( &e->e_nname, &dn );
483 scopeok = dn_match( &dn, &realbase );
485 scopeok = (realbase.bv_len == 0);
488 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
489 scopeok = dnIsSuffix( &e->e_nname, &realbase );
496 /* check size limit */
497 if ( --slimit == -1 ) {
498 cache_return_entry_r( &li->li_cache, e );
499 send_search_result( conn, op,
500 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
501 v2refs, NULL, nentries );
507 result = send_search_entry(be, conn, op,
508 e, attrs, attrsonly, NULL);
511 case 0: /* entry sent ok */
514 case 1: /* entry not sent */
516 case -1: /* connection closed */
517 cache_return_entry_r( &li->li_cache, e );
524 LDAP_LOG( BACK_LDBM, DETAIL2,
525 "ldbm_search: candidate entry %ld scope not okay\n",
528 Debug( LDAP_DEBUG_TRACE,
529 "ldbm_search: candidate entry %ld scope not okay\n",
536 LDAP_LOG( BACK_LDBM, DETAIL2,
537 "ldbm_search: candidate entry %ld does not match filter\n",
540 Debug( LDAP_DEBUG_TRACE,
541 "ldbm_search: candidate entry %ld does not match filter\n",
548 /* free reader lock */
549 cache_return_entry_r( &li->li_cache, e );
552 ldap_pvt_thread_yield();
555 send_search_result( conn, op,
556 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
557 NULL, NULL, v2refs, NULL, nentries );
562 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
564 if( candidates != NULL )
565 idl_free( candidates );
567 if( v2refs ) ber_bvarray_free( v2refs );
568 if( realbase.bv_val ) free( realbase.bv_val );
581 LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
583 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
588 idl = idl_alloc( 1 );
589 idl_insert( &idl, e->e_id, 1 );
603 ID_BLOCK *candidates;
604 Filter f, fand, rf, af, xf;
605 AttributeAssertion aa_ref, aa_alias;
606 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
607 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
610 LDAP_LOG( BACK_LDBM, DETAIL1,
611 "search_candidates: base (%s) scope %d deref %d\n",
612 e->e_ndn, scope, deref );
614 Debug(LDAP_DEBUG_TRACE,
615 "search_candidates: base=\"%s\" s=%d d=%d\n",
616 e->e_ndn, scope, deref );
621 xf.f_choice = LDAP_FILTER_OR;
625 /* match referrals */
626 rf.f_choice = LDAP_FILTER_EQUALITY;
628 rf.f_av_desc = slap_schema.si_ad_objectClass;
629 rf.f_av_value = bv_ref;
634 if( deref & LDAP_DEREF_SEARCHING ) {
636 af.f_choice = LDAP_FILTER_EQUALITY;
637 af.f_ava = &aa_alias;
638 af.f_av_desc = slap_schema.si_ad_objectClass;
639 af.f_av_value = bv_alias;
645 f.f_choice = LDAP_FILTER_AND;
647 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
648 ? SLAPD_FILTER_DN_SUBTREE
649 : SLAPD_FILTER_DN_ONE;
650 fand.f_dn = &e->e_nname;
651 fand.f_next = xf.f_or == filter ? filter : &xf ;
653 candidates = filter_candidates( be, &f );
655 return( candidates );