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,
44 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
46 const char *text = NULL;
51 struct berval **v2refs = NULL;
52 Entry *matched = NULL;
53 char *realbase = NULL;
55 int manageDSAit = get_manageDSAit( op );
57 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
59 /* get entry with reader lock */
60 if ( deref & LDAP_DEREF_FINDING ) {
61 e = deref_dn_r( be, nbase, &err, &matched, &text );
64 e = dn2entry_r( be, nbase, &matched );
65 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
70 char *matched_dn = NULL;
71 struct berval **refs = NULL;
73 if ( matched != NULL ) {
74 matched_dn = ch_strdup( matched->e_dn );
76 refs = is_entry_referral( matched )
77 ? get_entry_referrals( be, conn, op, matched )
80 cache_return_entry_r( &li->li_cache, matched );
82 refs = default_referral;
85 send_ldap_result( conn, op, err,
86 matched_dn, text, refs, NULL );
88 if( matched != NULL ) {
96 if (!manageDSAit && is_entry_referral( e ) ) {
97 /* entry is a referral, don't allow add */
98 char *matched_dn = ch_strdup( e->e_dn );
99 struct berval **refs = get_entry_referrals( be,
102 cache_return_entry_r( &li->li_cache, e );
104 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
107 send_ldap_result( conn, op, LDAP_REFERRAL,
108 matched_dn, NULL, refs, NULL );
110 ber_bvecfree( refs );
116 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
117 tlimit = -1; /* allow root to set no limit */
119 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
120 be->be_timelimit : tlimit;
121 stoptime = op->o_time + tlimit;
124 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
125 slimit = -1; /* allow root to set no limit */
127 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
128 be->be_sizelimit : slimit;
131 if ( scope == LDAP_SCOPE_BASE ) {
132 candidates = base_candidate( be, e );
135 candidates = search_candidates( be, e, filter,
136 scope, deref, manageDSAit );
139 /* need normalized dn below */
140 realbase = ch_strdup( e->e_ndn );
142 cache_return_entry_r( &li->li_cache, e );
144 if ( candidates == NULL ) {
146 Debug( LDAP_DEBUG_TRACE, "no candidates\n", 0,
149 send_search_result( conn, op,
151 NULL, NULL, NULL, NULL, 0 );
157 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
158 id = idl_nextid( candidates, &cursor ) )
162 /* check for abandon */
163 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
165 if ( op->o_abandon ) {
166 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
171 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
173 /* check time limit */
174 if ( tlimit != -1 && slap_get_time() > stoptime ) {
175 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
176 NULL, NULL, v2refs, NULL, nentries );
181 /* get the entry with reader lock */
182 e = id2entry_r( be, id );
185 Debug( LDAP_DEBUG_ARGS, "search: candidate %ld not found\n",
191 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
196 e = deref_entry_r( be, e, &err, &matched, &text );
203 if( e->e_id == id ) {
208 /* need to skip alias which deref into scope */
209 if( scope & LDAP_SCOPE_ONELEVEL ) {
210 char *pdn = dn_parent( NULL, e->e_ndn );
212 if( strcmp( pdn, realbase ) ) {
219 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
220 /* alias is within scope */
221 Debug( LDAP_DEBUG_ARGS, "search: \"%s\" in subtree\n",
230 * if it's a referral, add it to the list of referrals. only do
231 * this for non-base searches, and don't check the filter
232 * explicitly here since it's only a candidate anyway.
234 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
235 is_entry_referral( e ) )
237 struct berval **refs = get_entry_referrals(
240 send_search_reference( be, conn, op,
241 e, refs, scope, NULL, &v2refs );
243 ber_bvecfree( refs );
248 /* if it matches the filter and scope, send it */
249 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
253 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
254 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
255 (void) dn_normalize( dn );
256 scopeok = (dn == realbase)
258 : (strcmp( dn, realbase ) ? 0 : 1 );
262 scopeok = (realbase == NULL || *realbase == '\0');
265 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
266 dn = ch_strdup( e->e_ndn );
267 scopeok = dn_issuffix( dn, realbase );
275 /* check size limit */
276 if ( --slimit == -1 ) {
277 cache_return_entry_r( &li->li_cache, e );
278 send_search_result( conn, op,
279 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
280 v2refs, NULL, nentries );
287 #ifdef BROKEN_NUM_SUBORDINATES
288 /* Tack on subordinates attr */
289 ID_BLOCK *idl = NULL;
290 char CATTR_SUBS[] = "numsubordinates";
293 charray_inlist(attrs,
296 idl = dn2idl(be, e->e_ndn,
301 struct berval val, *vals[2];
310 val.bv_len = strlen(buf);
312 attr_merge(e, CATTR_SUBS,
318 result = send_search_entry(be, conn, op,
319 e, attrs, attrsonly, NULL);
321 #ifdef BROKEN_NUM_SUBORDINATES
325 attr_delete(&e->e_attrs,
332 case 0: /* entry sent ok */
335 case 1: /* entry not sent */
337 case -1: /* connection closed */
338 cache_return_entry_r( &li->li_cache, e );
344 Debug( LDAP_DEBUG_TRACE, "candidate %ld scope not okay\n",
348 Debug( LDAP_DEBUG_TRACE, "candidate %ld does match filter\n",
354 /* free reader lock */
355 cache_return_entry_r( &li->li_cache, e );
358 ldap_pvt_thread_yield();
360 send_search_result( conn, op,
361 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
362 NULL, NULL, v2refs, NULL, nentries );
367 if( candidates != NULL )
368 idl_free( candidates );
370 ber_bvecfree( v2refs );
371 if( realbase ) free( realbase );
384 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
387 idl = idl_alloc( 1 );
388 idl_insert( &idl, e->e_id, 1 );
403 ID_BLOCK *candidates;
404 #ifdef SLAPD_SCHEMA_NOT_COMPAT
405 candidates = filter_candidates( be, filter );
408 Filter rf, rf_or, af, af_or, lf, lf_and;
409 #ifdef SLAPD_SCHEMA_NOT_COMPAT
410 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
413 Debug(LDAP_DEBUG_TRACE, "search_candidates: base=\"%s\" s=%d d=%d\n",
414 e->e_ndn, scope, deref );
417 #ifndef SLAPD_SCHEMA_NOT_COMPAT
419 /* match referrals */
421 rf.f_choice = LDAP_FILTER_OR;
423 rf.f_or->f_choice = LDAP_FILTER_EQUALITY;
424 #ifdef SLAPD_SCHEMA_NOT_COMPAT
425 rf.f_or->f_av_desc = ad_objectClass;
426 rf.f_or->f_av_value = ber_bvstrdup("REFERRAL");
428 rf.f_or->f_avtype = ch_strdup( "objectclass" );
429 rf.f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
430 rf.f_or->f_avvalue.bv_len = sizeof("REFERRAL")-1;
432 rf.f_or->f_next = filter;
438 if( deref & LDAP_DEREF_SEARCHING ) {
441 af.f_choice = LDAP_FILTER_OR;
443 af.f_or->f_choice = LDAP_FILTER_EQUALITY;
444 #ifdef SLAPD_SCHEMA_NOT_COMPAT
445 af.f_or->f_av_desc = objectClass;
446 af.f_or->f_av_value = ber_bvstrdup("ALIAS");
448 af.f_or->f_avtype = ch_strdup( "objectclass" );
449 af.f_or->f_avvalue.bv_val = ch_strdup( "ALIAS" );
450 af.f_or->f_avvalue.bv_len = sizeof("ALIAS")-1;
452 af.f_or->f_next = filter;
458 if ( scope == LDAP_SCOPE_SUBTREE ) {
460 lf.f_choice = LDAP_FILTER_AND;
462 lf.f_and->f_choice = SLAPD_FILTER_DN_SUBTREE;
463 lf.f_and->f_dn = e->e_ndn;
464 lf.f_and->f_next = filter;
467 } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
469 lf.f_choice = LDAP_FILTER_AND;
471 lf.f_and->f_choice = SLAPD_FILTER_DN_ONE;
472 lf.f_and->f_dn = e->e_ndn;
473 lf.f_and->f_next = filter;
477 candidates = filter_candidates( be, filter );
479 /* free dynamically allocated bits */
480 if( af.f_or != NULL ) {
481 #ifdef SLAPD_SCHEMA_NOT_COMPAT
482 ber_bvfree( af.f_or->f_av_value );
484 free( af.f_or->f_avtype );
485 free( af.f_or->f_avvalue.bv_val );
489 if( rf.f_or != NULL ) {
490 #ifdef SLAPD_SCHEMA_NOT_COMPAT
491 ber_bvfree( rf.f_or->f_av_value );
493 free( rf.f_or->f_avtype );
494 free( rf.f_or->f_avvalue.bv_val );
499 return( candidates );