1 /* search.c - search operation */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
17 static int base_candidate(
21 static int search_candidates(
42 const char *filterstr,
46 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
49 const char *text = NULL;
52 ID candidates[BDB_IDL_UM_SIZE];
54 struct berval **v2refs = NULL;
55 Entry *matched = NULL;
56 char *realbase = NULL;
60 struct slap_limits_set *limit = NULL;
63 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
66 manageDSAit = get_manageDSAit( op );
68 if ( *nbase == '\0' ) {
69 /* DIT root special case */
70 e = (Entry *) &slap_entry_root;
74 /* get entry with reader lock */
75 if ( deref & LDAP_DEREF_FINDING ) {
76 e = deref_dn_r( be, nbase, &err, &matched, &text );
81 rc = bdb_dn2entry( be, NULL, nbase, &e, &matched, 0 );
89 send_ldap_result( conn, op, rc=LDAP_OTHER,
90 NULL, "internal error", NULL, NULL );
95 char *matched_dn = NULL;
96 struct berval **refs = NULL;
98 if ( matched != NULL ) {
99 struct berval **erefs;
100 matched_dn = ch_strdup( matched->e_dn );
102 erefs = is_entry_referral( matched )
103 ? get_entry_referrals( be, conn, op, matched,
107 bdb_entry_return( be, matched );
111 refs = referral_rewrite( erefs, matched_dn,
113 ber_bvecfree( erefs );
117 refs = referral_rewrite( default_referral,
121 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
122 matched_dn, text, refs, NULL );
124 ber_bvecfree( refs );
130 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
131 /* entry is a referral, don't allow add */
132 char *matched_dn = ch_strdup( e->e_dn );
133 struct berval **erefs = get_entry_referrals( be,
134 conn, op, e, base, scope );
135 struct berval **refs = NULL;
137 bdb_entry_return( be, e );
141 refs = referral_rewrite( erefs, matched_dn,
143 ber_bvecfree( erefs );
146 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
149 send_ldap_result( conn, op, LDAP_REFERRAL,
150 matched_dn, refs ? NULL : "bad referral object",
153 ber_bvecfree( refs );
159 /* if not root, get appropriate limits */
160 if ( be_isroot( be, op->o_ndn ) ) {
163 ( void ) get_limits( be, op->o_ndn, &limit );
166 /* The time/size limits come first because they require very little
167 * effort, so there's no chance the candidates are selected and then
168 * the request is not honored only because of time/size constraints */
170 /* if no time limit requested, use soft limit (unless root!) */
173 tlimit = -1; /* allow root to set no limit */
181 /* if no limit is required, use soft limit */
183 tlimit = limit->lms_t_soft;
185 /* if requested limit higher than hard limit, abort */
186 } else if ( tlimit > limit->lms_t_hard ) {
187 /* no hard limit means use soft instead */
188 if ( limit->lms_t_hard == 0 ) {
189 tlimit = limit->lms_t_soft;
191 /* positive hard limit means abort */
192 } else if ( limit->lms_t_hard > 0 ) {
193 send_search_result( conn, op,
194 LDAP_UNWILLING_TO_PERFORM,
195 NULL, NULL, NULL, NULL, 0 );
200 /* negative hard limit means no limit */
203 /* if no limit is required, use soft limit */
205 slimit = limit->lms_s_soft;
207 /* if requested limit higher than hard limit, abort */
208 } else if ( slimit > limit->lms_s_hard ) {
209 /* no hard limit means use soft instead */
210 if ( limit->lms_s_hard == 0 ) {
211 slimit = limit->lms_s_soft;
213 /* positive hard limit means abort */
214 } else if ( limit->lms_s_hard > 0 ) {
215 send_search_result( conn, op,
216 LDAP_UNWILLING_TO_PERFORM,
217 NULL, NULL, NULL, NULL, 0 );
222 /* negative hard limit means no limit */
226 /* compute it anyway; root does not use it */
227 stoptime = op->o_time + tlimit;
229 /* select candidates */
230 if ( scope == LDAP_SCOPE_BASE ) {
231 rc = base_candidate( be, e, candidates );
234 BDB_IDL_ALL( bdb, candidates );
235 rc = search_candidates( be, e, filter,
236 scope, deref, manageDSAit, candidates );
239 /* need normalized dn below */
240 realbase = ch_strdup( e->e_ndn );
242 /* start cursor at base entry's id
243 * FIXME: hack to make "" base work */
244 cursor = e->e_id == NOID ? 1 : e->e_id;
246 if ( e != &slap_entry_root ) {
247 bdb_entry_return( be, e );
251 if ( candidates[0] == 0 ) {
252 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
255 send_search_result( conn, op,
257 NULL, NULL, NULL, NULL, 0 );
263 /* if not root and candidates exceed to-be-checked entries, abort */
264 if ( !isroot && limit->lms_s_unchecked != -1 ) {
265 if ( BDB_IDL_N(candidates) > limit->lms_s_unchecked ) {
266 send_search_result( conn, op,
267 LDAP_UNWILLING_TO_PERFORM,
268 NULL, NULL, NULL, NULL, 0 );
274 for ( id = bdb_idl_first( candidates, &cursor );
276 id = bdb_idl_next( candidates, &cursor ) )
280 /* check for abandon */
281 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
282 abandon = op->o_abandon;
283 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
290 /* check time limit */
291 if ( tlimit != -1 && slap_get_time() > stoptime ) {
292 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
293 NULL, NULL, v2refs, NULL, nentries );
297 /* get the entry with reader lock */
298 rc = bdb_id2entry( be, NULL, id, &e );
301 if( !BDB_IDL_IS_RANGE(candidates) ) {
302 /* only complain for non-range IDLs */
303 Debug( LDAP_DEBUG_TRACE,
304 "bdb_search: candidate %ld not found\n",
312 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
317 e = deref_entry_r( be, e, &err, &matched, &text );
324 if( e->e_id == id ) {
329 /* need to skip alias which deref into scope */
330 if( scope & LDAP_SCOPE_ONELEVEL ) {
331 char *pdn = dn_parent( NULL, e->e_ndn );
333 if( strcmp( pdn, realbase ) ) {
340 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
341 /* alias is within scope */
342 Debug( LDAP_DEBUG_TRACE,
343 "bdb_search: \"%s\" in subtree\n",
353 * if it's a referral, add it to the list of referrals. only do
354 * this for non-base searches, and don't check the filter
355 * explicitly here since it's only a candidate anyway.
357 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
358 is_entry_referral( e ) )
360 struct berval **erefs = get_entry_referrals(
361 be, conn, op, e, NULL, scope );
362 struct berval **ref = referral_rewrite( eref, e->e_dn, NULL,
363 scope == LDAP_SCOPE_SUBTREE
367 send_search_reference( be, conn, op,
368 e, refs, NULL, &v2refs );
370 ber_bvecfree( refs );
375 /* if it matches the filter and scope, send it */
376 rc = test_filter( be, conn, op, e, filter );
377 if ( rc == LDAP_COMPARE_TRUE ) {
381 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
382 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
383 (void) dn_normalize( dn );
384 scopeok = (dn == realbase)
386 : (strcmp( dn, realbase ) ? 0 : 1 );
390 scopeok = (realbase == NULL || *realbase == '\0');
393 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
394 dn = ch_strdup( e->e_ndn );
395 scopeok = dn_issuffix( dn, realbase );
403 /* check size limit */
404 if ( --slimit == -1 ) {
405 bdb_entry_return( be, e );
407 send_search_result( conn, op,
408 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
409 v2refs, NULL, nentries );
414 int result = send_search_entry( be, conn, op,
415 e, attrs, attrsonly, NULL);
418 case 0: /* entry sent ok */
421 case 1: /* entry not sent */
423 case -1: /* connection closed */
424 bdb_entry_return( be, e );
431 Debug( LDAP_DEBUG_TRACE,
432 "bdb_search: %ld scope not okay\n",
436 Debug( LDAP_DEBUG_TRACE,
437 "bdb_search: %ld does match filter\n",
443 /* free reader lock */
444 bdb_entry_return( be, e );
447 ldap_pvt_thread_yield();
449 send_search_result( conn, op,
450 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
451 NULL, NULL, v2refs, NULL, nentries );
456 ber_bvecfree( v2refs );
457 if( realbase ) ch_free( realbase );
463 static int base_candidate(
468 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
469 e->e_dn, (long) e->e_id, 0);
476 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
477 * doesn't count, we're looking for explicit values.
479 static int oc_filter(
485 switch(f->f_choice) {
486 case LDAP_FILTER_EQUALITY:
487 case LDAP_FILTER_APPROX:
488 if (f->f_av_desc == slap_schema.si_ad_objectClass)
492 case LDAP_FILTER_SUBSTRINGS:
493 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
497 case LDAP_FILTER_AND:
499 for (f=f->f_and; f; f=f->f_next)
500 if ((rc = oc_filter(f)))
509 static int search_candidates(
519 Filter f, fand, rf, xf;
520 ID tmp[BDB_IDL_UM_SIZE];
521 AttributeAssertion aa_ref;
522 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
525 AttributeAssertion aa_alias;
528 Debug(LDAP_DEBUG_TRACE,
529 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
530 e->e_dn, (long) e->e_id, scope );
533 xf.f_choice = LDAP_FILTER_OR;
536 /* If the user's filter doesn't mention objectClass, or if
537 * it just uses objectClass=*, these clauses are redundant.
539 if (oc_filter(filter)) {
540 if( !manageDSAit ) { /* match referrals */
541 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
542 rf.f_choice = LDAP_FILTER_EQUALITY;
544 rf.f_av_desc = slap_schema.si_ad_objectClass;
545 rf.f_av_value = &bv_ref;
551 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
552 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
553 af.f_choice = LDAP_FILTER_EQUALITY;
554 af.f_ava = &aa_alias;
555 af.f_av_desc = slap_schema.si_ad_objectClass;
556 af.f_av_value = &bv_alias;
564 f.f_choice = LDAP_FILTER_AND;
566 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
567 ? SLAPD_FILTER_DN_SUBTREE
568 : SLAPD_FILTER_DN_ONE;
569 fand.f_dn = e->e_ndn;
570 fand.f_next = xf.f_or == filter ? filter : &xf ;
573 #ifdef BDB_FILTER_INDICES
574 rc = bdb_filter_candidates( be, &f, ids, tmp );
576 /* FIXME: Original code:
577 BDB_IDL_ID( bdb, ids, e->e_id );
578 * this is a hack to make "" base work; when bdb_filter_candidates
579 * is used this should not be needed any more */
580 BDB_IDL_ID( bdb, ids, (e->e_id == NOID ? 1 : e->e_id) );
584 Debug(LDAP_DEBUG_TRACE,
585 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
587 (long) BDB_IDL_FIRST(ids),
588 (long) BDB_IDL_LAST(ids) );