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 && tlimit > limit->lms_t_soft ) {
251 tlimit = limit->lms_t_soft;
253 /* positive hard limit means abort */
254 } else if ( limit->lms_t_hard > 0 ) {
255 send_search_result( conn, op,
256 LDAP_UNWILLING_TO_PERFORM,
257 NULL, NULL, NULL, NULL, 0 );
262 /* negative hard limit means no limit */
265 /* if no limit is required, use soft limit */
267 slimit = limit->lms_s_soft;
269 /* if requested limit higher than hard limit, abort */
270 } else if ( slimit > limit->lms_s_hard ) {
271 /* no hard limit means use soft instead */
272 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
273 slimit = limit->lms_s_soft;
275 /* positive hard limit means abort */
276 } else if ( limit->lms_s_hard > 0 ) {
277 send_search_result( conn, op,
278 LDAP_UNWILLING_TO_PERFORM,
279 NULL, NULL, NULL, NULL, 0 );
284 /* negative hard limit means no limit */
288 /* compute it anyway; root does not use it */
289 stoptime = op->o_time + tlimit;
291 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
292 id = idl_nextid( candidates, &cursor ) )
296 /* check for abandon */
297 if ( op->o_abandon ) {
302 /* check time limit */
303 if ( tlimit != -1 && slap_get_time() > stoptime ) {
304 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
305 NULL, NULL, v2refs, NULL, nentries );
310 /* get the entry with reader lock */
311 e = id2entry_r( be, id );
315 LDAP_LOG( BACK_LDBM, INFO,
316 "ldbm_search: candidate %ld not found.\n", id, 0, 0 );
318 Debug( LDAP_DEBUG_TRACE,
319 "ldbm_search: candidate %ld not found\n",
326 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
331 e = deref_entry_r( be, e, &err, &matched, &text );
338 if( e->e_id == id ) {
343 /* need to skip alias which deref into scope */
344 if( scope & LDAP_SCOPE_ONELEVEL ) {
346 dnParent( &e->e_nname, &pdn );
347 if ( ber_bvcmp( &pdn, &realbase ) ) {
351 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
352 /* alias is within scope */
354 LDAP_LOG( BACK_LDBM, DETAIL1,
355 "ldbm_search: alias \"%s\" in subtree\n", e->e_dn, 0, 0 );
357 Debug( LDAP_DEBUG_TRACE,
358 "ldbm_search: alias \"%s\" in subtree\n",
369 * if it's a referral, add it to the list of referrals. only do
370 * this for non-base searches, and don't check the filter
371 * explicitly here since it's only a candidate anyway.
373 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
374 is_entry_referral( e ) )
379 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
380 if ( !be_issuffix( be, &e->e_nname ) ) {
381 dnParent( &e->e_nname, &dn );
382 scopeok = dn_match( &dn, &realbase );
384 scopeok = (realbase.bv_len == 0);
387 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
388 scopeok = dnIsSuffix( &e->e_nname, &realbase );
395 BerVarray erefs = get_entry_referrals(
397 BerVarray refs = referral_rewrite( erefs,
399 scope == LDAP_SCOPE_SUBTREE
403 send_search_reference( be, conn, op,
404 e, refs, NULL, &v2refs );
406 ber_bvarray_free( refs );
410 LDAP_LOG( BACK_LDBM, DETAIL2,
411 "ldbm_search: candidate referral %ld scope not okay\n",
414 Debug( LDAP_DEBUG_TRACE,
415 "ldbm_search: candidate referral %ld scope not okay\n",
423 /* if it matches the filter and scope, send it */
424 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
428 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
429 if ( !be_issuffix( be, &e->e_nname ) ) {
430 dnParent( &e->e_nname, &dn );
431 scopeok = dn_match( &dn, &realbase );
433 scopeok = (realbase.bv_len == 0);
436 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
437 scopeok = dnIsSuffix( &e->e_nname, &realbase );
444 /* check size limit */
445 if ( --slimit == -1 ) {
446 cache_return_entry_r( &li->li_cache, e );
447 send_search_result( conn, op,
448 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
449 v2refs, NULL, nentries );
455 int result = send_search_entry(be, conn, op,
456 e, attrs, attrsonly, NULL);
459 case 0: /* entry sent ok */
462 case 1: /* entry not sent */
464 case -1: /* connection closed */
465 cache_return_entry_r( &li->li_cache, e );
472 LDAP_LOG( BACK_LDBM, DETAIL2,
473 "ldbm_search: candidate entry %ld scope not okay\n",
476 Debug( LDAP_DEBUG_TRACE,
477 "ldbm_search: candidate entry %ld scope not okay\n",
484 LDAP_LOG( BACK_LDBM, DETAIL2,
485 "ldbm_search: candidate entry %ld does not match filter\n",
488 Debug( LDAP_DEBUG_TRACE,
489 "ldbm_search: candidate entry %ld does not match filter\n",
496 /* free reader lock */
497 cache_return_entry_r( &li->li_cache, e );
500 ldap_pvt_thread_yield();
503 send_search_result( conn, op,
504 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
505 NULL, NULL, v2refs, NULL, nentries );
510 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
512 if( candidates != NULL )
513 idl_free( candidates );
515 if( v2refs ) ber_bvarray_free( v2refs );
516 if( realbase.bv_val ) free( realbase.bv_val );
529 LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
531 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
536 idl = idl_alloc( 1 );
537 idl_insert( &idl, e->e_id, 1 );
551 ID_BLOCK *candidates;
552 Filter f, fand, rf, af, xf;
553 AttributeAssertion aa_ref, aa_alias;
554 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
555 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
558 LDAP_LOG( BACK_LDBM, DETAIL1,
559 "search_candidates: base (%s) scope %d deref %d\n",
560 e->e_ndn, scope, deref );
562 Debug(LDAP_DEBUG_TRACE,
563 "search_candidates: base=\"%s\" s=%d d=%d\n",
564 e->e_ndn, scope, deref );
569 xf.f_choice = LDAP_FILTER_OR;
573 /* match referrals */
574 rf.f_choice = LDAP_FILTER_EQUALITY;
576 rf.f_av_desc = slap_schema.si_ad_objectClass;
577 rf.f_av_value = bv_ref;
582 if( deref & LDAP_DEREF_SEARCHING ) {
584 af.f_choice = LDAP_FILTER_EQUALITY;
585 af.f_ava = &aa_alias;
586 af.f_av_desc = slap_schema.si_ad_objectClass;
587 af.f_av_value = bv_alias;
593 f.f_choice = LDAP_FILTER_AND;
595 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
596 ? SLAPD_FILTER_DN_SUBTREE
597 : SLAPD_FILTER_DN_ONE;
598 fand.f_dn = &e->e_nname;
599 fand.f_next = xf.f_or == filter ? filter : &xf ;
601 candidates = filter_candidates( be, &f );
603 return( candidates );