1 /* search.c - ldbm backend search function */
4 * Copyright 1998-2000 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 const char *filterstr,
43 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
45 const char *text = NULL;
50 struct berval **v2refs = NULL;
51 Entry *matched = NULL;
52 char *realbase = NULL;
54 int manageDSAit = get_manageDSAit( op );
56 struct slap_limits_set *limit = NULL;
60 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
61 "ldbm_back_search: enter\n" ));
63 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
67 if ( *nbase == '\0' ) {
68 /* DIT root special case */
69 static const Entry root = { NOID, "", "", NULL, NULL };
72 /* need normalized dn below */
73 realbase = ch_strdup( e->e_ndn );
75 candidates = search_candidates( be, e, filter,
76 scope, deref, manageDSAit );
80 } else if ( deref & LDAP_DEREF_FINDING ) {
81 /* deref dn and get entry with reader lock */
82 e = deref_dn_r( be, nbase, &err, &matched, &text );
84 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
87 /* get entry with reader lock */
88 e = dn2entry_r( be, nbase, &matched );
89 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
94 char *matched_dn = NULL;
95 struct berval **refs = NULL;
97 if ( matched != NULL ) {
98 matched_dn = ch_strdup( matched->e_dn );
100 refs = is_entry_referral( matched )
101 ? get_entry_referrals( be, conn, op, matched )
104 cache_return_entry_r( &li->li_cache, matched );
107 refs = default_referral;
110 send_ldap_result( conn, op, err,
111 matched_dn, text, refs, NULL );
113 if( matched != NULL ) {
114 ber_bvecfree( refs );
121 if (!manageDSAit && is_entry_referral( e ) ) {
122 /* entry is a referral, don't allow add */
123 char *matched_dn = ch_strdup( e->e_dn );
124 struct berval **refs = get_entry_referrals( be,
127 cache_return_entry_r( &li->li_cache, e );
130 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
131 "ldbm_search: entry (%s) is a referral.\n",
134 Debug( LDAP_DEBUG_TRACE,
135 "ldbm_search: entry is referral\n",
140 send_ldap_result( conn, op, LDAP_REFERRAL,
141 matched_dn, NULL, refs, NULL );
143 ber_bvecfree( refs );
149 if ( is_entry_alias( e ) ) {
151 deref = LDAP_DEREF_NEVER;
154 if ( scope == LDAP_SCOPE_BASE ) {
155 candidates = base_candidate( be, e );
158 candidates = search_candidates( be, e, filter,
159 scope, deref, manageDSAit );
162 /* need normalized dn below */
163 realbase = ch_strdup( e->e_ndn );
165 cache_return_entry_r( &li->li_cache, e );
168 if ( candidates == NULL ) {
171 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
172 "ldbm_search: no candidates\n" ));
174 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
179 send_search_result( conn, op,
181 NULL, NULL, NULL, NULL, 0 );
187 /* if not root, get appropriate limits */
188 if ( be_isroot( be, op->o_ndn ) ) {
191 ( void ) get_limits( be, op->o_ndn, &limit );
194 /* if candidates exceed to-be-checked entries, abort */
195 if ( !isroot && limit->lms_s_unchecked != -1 ) {
196 if ( ID_BLOCK_NIDS( candidates ) > limit->lms_s_unchecked ) {
197 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
198 NULL, NULL, NULL, NULL, 0 );
204 /* if no time limit requested, use soft limit (unless root!) */
207 tlimit = -1; /* allow root to set no limit */
209 tlimit = limit->lms_t_soft;
212 /* if requested limit higher than hard limit, abort */
213 } else if ( tlimit > limit->lms_t_hard ) {
214 /* no hard limit means use soft instead */
215 if ( limit->lms_t_hard == 0 ) {
216 tlimit = limit->lms_t_soft;
218 /* positive hard limit means abort */
219 } else if ( limit->lms_t_hard > 0 ) {
220 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
221 NULL, NULL, NULL, NULL, 0 );
226 /* negative hard limit means no limit */
229 /* compute it anyway; root does not use it */
230 stoptime = op->o_time + tlimit;
232 /* if no size limit requested, use soft limit (unless root!) */
235 slimit = -1; /* allow root to set no limit */
237 slimit = limit->lms_s_soft;
240 /* if requested limit higher than hard limit, abort */
241 } else if ( slimit > limit->lms_s_hard ) {
242 /* no hard limit means use soft instead */
243 if ( limit->lms_s_hard == 0 ) {
244 slimit = limit->lms_s_soft;
246 /* positive hard limit means abort */
247 } else if ( limit->lms_s_hard > 0 ) {
248 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
249 NULL, NULL, NULL, NULL, 0 );
254 /* negative hard limit means no limit */
257 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
258 id = idl_nextid( candidates, &cursor ) )
262 /* check for abandon */
263 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
265 if ( op->o_abandon ) {
266 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
271 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
273 /* check time limit */
274 if ( tlimit != -1 && slap_get_time() > stoptime ) {
275 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
276 NULL, NULL, v2refs, NULL, nentries );
281 /* get the entry with reader lock */
282 e = id2entry_r( be, id );
286 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
287 "ldbm_search: candidate %ld not found.\n", id ));
289 Debug( LDAP_DEBUG_TRACE,
290 "ldbm_search: candidate %ld not found\n",
298 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
303 e = deref_entry_r( be, e, &err, &matched, &text );
310 if( e->e_id == id ) {
315 /* need to skip alias which deref into scope */
316 if( scope & LDAP_SCOPE_ONELEVEL ) {
317 char *pdn = dn_parent( NULL, e->e_ndn );
319 if( strcmp( pdn, realbase ) ) {
326 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
327 /* alias is within scope */
329 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
330 "ldbm_search: \"%s\" in subtree\n", e->e_dn ));
332 Debug( LDAP_DEBUG_TRACE,
333 "ldbm_search: \"%s\" in subtree\n",
344 * if it's a referral, add it to the list of referrals. only do
345 * this for non-base searches, and don't check the filter
346 * explicitly here since it's only a candidate anyway.
348 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
349 is_entry_referral( e ) )
354 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
355 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
356 (void) dn_normalize( dn );
357 scopeok = (dn == realbase)
359 : (strcmp( dn, realbase ) ? 0 : 1 );
363 scopeok = (realbase == NULL || *realbase == '\0');
366 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
367 dn = ch_strdup( e->e_ndn );
368 scopeok = dn_issuffix( dn, realbase );
376 struct berval **refs = get_entry_referrals(
379 send_search_reference( be, conn, op,
380 e, refs, scope, NULL, &v2refs );
382 ber_bvecfree( refs );
386 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
387 "ldbm_search: candidate referral %ld scope not okay\n",
390 Debug( LDAP_DEBUG_TRACE,
391 "ldbm_search: candidate referral %ld scope not okay\n",
399 /* if it matches the filter and scope, send it */
400 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
404 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
405 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
406 (void) dn_normalize( dn );
407 scopeok = (dn == realbase)
409 : (strcmp( dn, realbase ) ? 0 : 1 );
413 scopeok = (realbase == NULL || *realbase == '\0');
416 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
417 dn = ch_strdup( e->e_ndn );
418 scopeok = dn_issuffix( dn, realbase );
426 /* check size limit */
427 if ( --slimit == -1 ) {
428 cache_return_entry_r( &li->li_cache, e );
429 send_search_result( conn, op,
430 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
431 v2refs, NULL, nentries );
437 int result = send_search_entry(be, conn, op,
438 e, attrs, attrsonly, NULL);
441 case 0: /* entry sent ok */
444 case 1: /* entry not sent */
446 case -1: /* connection closed */
447 cache_return_entry_r( &li->li_cache, e );
454 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
455 "ldbm_search: candidate %ld scope not okay\n", id ));
457 Debug( LDAP_DEBUG_TRACE,
458 "ldbm_search: candidate %ld scope not okay\n",
465 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
466 "ldbm_search: candidate %ld does not match filter\n", id ));
468 Debug( LDAP_DEBUG_TRACE,
469 "ldbm_search: candidate %ld does not match filter\n",
477 /* free reader lock */
478 cache_return_entry_r( &li->li_cache, e );
481 ldap_pvt_thread_yield();
483 send_search_result( conn, op,
484 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
485 NULL, NULL, v2refs, NULL, nentries );
490 if( candidates != NULL )
491 idl_free( candidates );
493 ber_bvecfree( v2refs );
494 if( realbase ) free( realbase );
507 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
508 "base_candidate: base (%s)\n", e->e_dn ));
510 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
515 idl = idl_alloc( 1 );
516 idl_insert( &idl, e->e_id, 1 );
530 ID_BLOCK *candidates;
531 Filter f, fand, rf, af, xf;
532 AttributeAssertion aa_ref, aa_alias;
533 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
534 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
537 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
538 "search_candidates: base (%s) scope %d deref %d\n",
539 e->e_ndn, scope, deref ));
541 Debug(LDAP_DEBUG_TRACE,
542 "search_candidates: base=\"%s\" s=%d d=%d\n",
543 e->e_ndn, scope, deref );
548 xf.f_choice = LDAP_FILTER_OR;
552 /* match referrals */
553 rf.f_choice = LDAP_FILTER_EQUALITY;
555 rf.f_av_desc = slap_schema.si_ad_objectClass;
556 rf.f_av_value = &bv_ref;
561 if( deref & LDAP_DEREF_SEARCHING ) {
563 af.f_choice = LDAP_FILTER_EQUALITY;
564 af.f_ava = &aa_alias;
565 af.f_av_desc = slap_schema.si_ad_objectClass;
566 af.f_av_value = &bv_alias;
572 f.f_choice = LDAP_FILTER_AND;
574 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
575 ? SLAPD_FILTER_DN_SUBTREE
576 : SLAPD_FILTER_DN_ONE;
577 fand.f_dn = e->e_ndn;
578 fand.f_next = xf.f_or == filter ? filter : &xf ;
580 candidates = filter_candidates( be, &f );
582 return( candidates );