1 /* search.c - search operation */
4 * Copyright 1998-2002 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 struct berval *filterstr,
46 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
48 const char *text = NULL;
51 ID candidates[BDB_IDL_UM_SIZE];
53 BerVarray v2refs = NULL;
54 Entry *matched = NULL;
55 struct berval realbase = { 0, NULL };
59 struct slap_limits_set *limit = NULL;
62 #ifdef SLAP_X_FILTER_HASSUBORDINATES
63 int filter_hasSubordinates = 0;
64 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
70 LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
72 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
76 manageDSAit = get_manageDSAit( op );
78 rc = LOCK_ID (bdb->bi_dbenv, &locker );
83 send_ldap_result( conn, op, rc=LDAP_OTHER,
84 NULL, "internal error", NULL, NULL );
88 if ( nbase->bv_len == 0 ) {
89 /* DIT root special case */
90 e = (Entry *) &slap_entry_root;
94 /* get entry with reader lock */
95 if ( deref & LDAP_DEREF_FINDING ) {
96 e = deref_dn_r( be, nbase-, &err, &matched, &text );
102 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
111 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
113 if (matched != NULL) {
114 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
116 send_ldap_result( conn, op, LDAP_BUSY,
117 NULL, "ldap server busy", NULL, NULL );
118 LOCK_ID_FREE (bdb->bi_dbenv, locker );
120 case DB_LOCK_DEADLOCK:
121 case DB_LOCK_NOTGRANTED:
125 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
127 if (matched != NULL) {
128 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
130 send_ldap_result( conn, op, rc=LDAP_OTHER,
131 NULL, "internal error", NULL, NULL );
132 LOCK_ID_FREE (bdb->bi_dbenv, locker );
137 struct berval matched_dn = { 0, NULL };
138 BerVarray refs = NULL;
140 if ( matched != NULL ) {
142 ber_dupbv( &matched_dn, &matched->e_name );
144 erefs = is_entry_referral( matched )
145 ? get_entry_referrals( be, conn, op, matched )
148 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
152 refs = referral_rewrite( erefs, &matched_dn,
154 ber_bvarray_free( erefs );
158 refs = referral_rewrite( default_referral,
162 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
163 matched_dn.bv_val, text, refs, NULL );
165 LOCK_ID_FREE (bdb->bi_dbenv, locker );
166 if ( refs ) ber_bvarray_free( refs );
167 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
171 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
172 /* entry is a referral, don't allow add */
173 struct berval matched_dn;
174 BerVarray erefs, refs;
176 ber_dupbv( &matched_dn, &e->e_name );
177 erefs = get_entry_referrals( be, conn, op, e );
180 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
184 refs = referral_rewrite( erefs, &matched_dn,
186 ber_bvarray_free( erefs );
190 LDAP_LOG ( OPERATION, RESULTS,
191 "bdb_search: entry is referral\n", 0, 0, 0 );
193 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
197 send_ldap_result( conn, op, LDAP_REFERRAL,
199 refs ? NULL : "bad referral object",
202 LOCK_ID_FREE (bdb->bi_dbenv, locker );
203 ber_bvarray_free( refs );
204 ber_memfree( matched_dn.bv_val );
208 /* if not root, get appropriate limits */
209 if ( be_isroot( be, &op->o_ndn ) ) {
212 ( void ) get_limits( be, &op->o_ndn, &limit );
215 /* The time/size limits come first because they require very little
216 * effort, so there's no chance the candidates are selected and then
217 * the request is not honored only because of time/size constraints */
219 /* if no time limit requested, use soft limit (unless root!) */
222 tlimit = -1; /* allow root to set no limit */
230 /* if no limit is required, use soft limit */
232 tlimit = limit->lms_t_soft;
234 /* if requested limit higher than hard limit, abort */
235 } else if ( tlimit > limit->lms_t_hard ) {
236 /* no hard limit means use soft instead */
237 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
238 tlimit = limit->lms_t_soft;
240 /* positive hard limit means abort */
241 } else if ( limit->lms_t_hard > 0 ) {
242 send_search_result( conn, op,
243 LDAP_UNWILLING_TO_PERFORM,
244 NULL, NULL, NULL, NULL, 0 );
249 /* negative hard limit means no limit */
252 /* if no limit is required, use soft limit */
254 slimit = limit->lms_s_soft;
256 /* if requested limit higher than hard limit, abort */
257 } else if ( slimit > limit->lms_s_hard ) {
258 /* no hard limit means use soft instead */
259 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
260 slimit = limit->lms_s_soft;
262 /* positive hard limit means abort */
263 } else if ( limit->lms_s_hard > 0 ) {
264 send_search_result( conn, op,
265 LDAP_UNWILLING_TO_PERFORM,
266 NULL, NULL, NULL, NULL, 0 );
271 /* negative hard limit means no limit */
275 /* compute it anyway; root does not use it */
276 stoptime = op->o_time + tlimit;
278 /* select candidates */
279 if ( scope == LDAP_SCOPE_BASE ) {
280 rc = base_candidate( be, e, candidates );
283 BDB_IDL_ALL( bdb, candidates );
284 rc = search_candidates( be, op, e, filter,
285 scope, deref, candidates );
288 /* need normalized dn below */
289 ber_dupbv( &realbase, &e->e_nname );
291 /* start cursor at base entry's id
292 * FIXME: hack to make "" base work
293 * FIXME: moddn needs to assign new ID for this to work
295 cursor = e->e_id == NOID ? 1 : e->e_id;
297 if ( e != &slap_entry_root ) {
298 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
302 if ( candidates[0] == 0 ) {
304 LDAP_LOG ( OPERATION, RESULTS, "bdb_search: no candidates\n", 0, 0, 0 );
306 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
310 send_search_result( conn, op,
312 NULL, NULL, NULL, NULL, 0 );
318 /* if not root and candidates exceed to-be-checked entries, abort */
319 if ( !isroot && limit->lms_s_unchecked != -1 ) {
320 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
321 send_search_result( conn, op,
322 LDAP_ADMINLIMIT_EXCEEDED,
323 NULL, NULL, NULL, NULL, 0 );
329 #ifdef SLAP_X_FILTER_HASSUBORDINATES
331 * is hasSubordinates used in the filter ?
332 * FIXME: we may compute this directly when parsing the filter
334 filter_hasSubordinates = filter_has_subordinates( filter );
335 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
337 for ( id = bdb_idl_first( candidates, &cursor );
339 id = bdb_idl_next( candidates, &cursor ) )
342 #ifdef SLAP_X_FILTER_HASSUBORDINATES
343 Attribute *hasSubordinates = NULL;
344 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
346 /* check for abandon */
347 if ( op->o_abandon ) {
352 /* check time limit */
353 if ( tlimit != -1 && slap_get_time() > stoptime ) {
354 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
355 NULL, NULL, v2refs, NULL, nentries );
360 /* get the entry with reader lock */
361 rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
363 if (rc == LDAP_BUSY) {
364 send_ldap_result( conn, op, rc=LDAP_BUSY,
365 NULL, "ldap server busy", NULL, NULL );
368 } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
373 if( !BDB_IDL_IS_RANGE(candidates) ) {
374 /* only complain for non-range IDLs */
376 LDAP_LOG ( OPERATION, RESULTS,
377 "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
379 Debug( LDAP_DEBUG_TRACE,
380 "bdb_search: candidate %ld not found\n",
388 #ifdef BDB_SUBENTRIES
389 if ( is_entry_subentry( e ) ) {
390 if( scope != LDAP_SCOPE_BASE ) {
391 if(!get_subentries_visibility( op )) {
392 /* only subentries are visible */
396 } else if ( get_subentries( op ) &&
397 !get_subentries_visibility( op ))
399 /* only subentries are visible */
403 } else if ( get_subentries_visibility( op )) {
404 /* only subentries are visible */
410 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
415 e = deref_entry_r( be, e, &err, &matched, &text );
422 if( e->e_id == id ) {
427 /* need to skip alias which deref into scope */
428 if( scope & LDAP_SCOPE_ONELEVEL ) {
431 dnParent( &e->e_nname, &pdn ):
432 if ( ber_bvcmp( pdn, &realbase ) ) {
436 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
437 /* alias is within scope */
439 LDAP_LOG ( OPERATION, RESULTS,
440 "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
442 Debug( LDAP_DEBUG_TRACE,
443 "bdb_search: \"%s\" in subtree\n",
454 * if it's a referral, add it to the list of referrals. only do
455 * this for non-base searches, and don't check the filter
456 * explicitly here since it's only a candidate anyway.
458 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
459 is_entry_referral( e ) )
464 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
465 if ( !be_issuffix( be, &e->e_nname ) ) {
466 dnParent( &e->e_nname, &dn );
467 scopeok = dn_match( &dn, &realbase );
469 scopeok = (realbase.bv_len == 0);
472 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
473 scopeok = dnIsSuffix( &e->e_nname, &realbase );
480 BerVarray erefs = get_entry_referrals(
482 BerVarray refs = referral_rewrite( erefs,
484 scope == LDAP_SCOPE_SUBTREE
488 send_search_reference( be, conn, op,
489 e, refs, NULL, &v2refs );
491 ber_bvarray_free( refs );
495 LDAP_LOG(OPERATION, DETAIL2,
496 "bdb_search: candidate referral %ld scope not okay\n",
499 Debug( LDAP_DEBUG_TRACE,
500 "bdb_search: candidate referral %ld scope not okay\n",
508 #ifdef SLAP_X_FILTER_HASSUBORDINATES
510 * if hasSubordinates is used in the filter,
511 * append it to the entry's attributes
513 if ( filter_hasSubordinates ) {
516 rc = bdb_hasSubordinates( be, conn, op, e, &hs);
517 if ( rc != LDAP_SUCCESS ) {
521 hasSubordinates = slap_operational_hasSubordinate( hs == LDAP_COMPARE_TRUE );
522 if ( hasSubordinates == NULL ) {
526 hasSubordinates->a_next = e->e_attrs;
527 e->e_attrs = hasSubordinates;
529 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
531 /* if it matches the filter and scope, send it */
532 rc = test_filter( be, conn, op, e, filter );
534 #ifdef SLAP_X_FILTER_HASSUBORDINATES
535 if ( hasSubordinates ) {
537 * FIXME: this is fairly inefficient, because
538 * if hasSubordinates is among the required
539 * attrs, it will be added again later;
540 * maybe we should leave it and check
541 * check later if it's already present,
544 e->e_attrs = e->e_attrs->a_next;
545 attr_free( hasSubordinates );
547 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
549 if ( rc == LDAP_COMPARE_TRUE ) {
553 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
554 if ( be_issuffix( be, &e->e_nname ) ) {
555 scopeok = (realbase.bv_len == 0);
557 dnParent( &e->e_nname, &dn );
558 scopeok = dn_match( &dn, &realbase );
561 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
562 scopeok = dnIsSuffix( &e->e_nname, &realbase );
569 /* check size limit */
570 if ( --slimit == -1 ) {
571 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
573 send_search_result( conn, op,
574 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
575 v2refs, NULL, nentries );
582 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
587 result = send_search_entry( be, conn, op,
588 e, attrs, attrsonly, NULL);
594 case 0: /* entry sent ok */
597 case 1: /* entry not sent */
599 case -1: /* connection closed */
600 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
608 LDAP_LOG ( OPERATION, RESULTS,
609 "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
611 Debug( LDAP_DEBUG_TRACE,
612 "bdb_search: %ld scope not okay\n",
618 LDAP_LOG ( OPERATION, RESULTS,
619 "bdb_search: %ld does match filter\n", (long) id, 0, 0);
621 Debug( LDAP_DEBUG_TRACE,
622 "bdb_search: %ld does match filter\n",
629 /* free reader lock */
630 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e , &lock);
634 ldap_pvt_thread_yield();
636 send_search_result( conn, op,
637 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
638 NULL, NULL, v2refs, NULL, nentries );
644 /* free reader lock */
645 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
648 LOCK_ID_FREE (bdb->bi_dbenv, locker );
650 if( v2refs ) ber_bvarray_free( v2refs );
651 if( realbase.bv_val ) ch_free( realbase.bv_val );
657 static int base_candidate(
663 LDAP_LOG ( OPERATION, ENTRY,
664 "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
666 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
667 e->e_dn, (long) e->e_id, 0);
675 /* Look for "objectClass Present" in this filter.
676 * Also count depth of filter tree while we're at it.
678 static int oc_filter(
686 if( cur > *max ) *max = cur;
688 switch(f->f_choice) {
689 case LDAP_FILTER_PRESENT:
690 if (f->f_desc == slap_schema.si_ad_objectClass)
694 case LDAP_FILTER_AND:
697 for (f=f->f_and; f; f=f->f_next)
698 rc |= oc_filter(f, cur, max);
706 static int search_candidates(
716 Filter f, scopef, rf, xf;
718 AttributeAssertion aa_ref;
719 #ifdef BDB_SUBENTRIES
721 AttributeAssertion aa_subentry;
725 AttributeAssertion aa_alias;
729 * This routine takes as input a filter (user-filter)
730 * and rewrites it as follows:
731 * (&(scope=DN)[(objectClass=subentry)]
732 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
736 LDAP_LOG ( OPERATION, ENTRY,
737 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
738 e->e_dn, (long) e->e_id, scope);
740 Debug(LDAP_DEBUG_TRACE,
741 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
742 e->e_dn, (long) e->e_id, scope );
746 xf.f_choice = LDAP_FILTER_OR;
749 /* If the user's filter uses objectClass=*,
750 * these clauses are redundant.
752 if (!oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
753 if( !get_manageDSAit(op) ) { /* match referrals */
754 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
755 rf.f_choice = LDAP_FILTER_EQUALITY;
757 rf.f_av_desc = slap_schema.si_ad_objectClass;
758 rf.f_av_value = bv_ref;
764 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
765 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
766 af.f_choice = LDAP_FILTER_EQUALITY;
767 af.f_ava = &aa_alias;
768 af.f_av_desc = slap_schema.si_ad_objectClass;
769 af.f_av_value = bv_alias;
774 /* We added one of these clauses, filter depth increased */
775 if( xf.f_or != filter ) depth++;
779 f.f_choice = LDAP_FILTER_AND;
781 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
782 ? SLAPD_FILTER_DN_SUBTREE
783 : SLAPD_FILTER_DN_ONE;
784 scopef.f_dn = &e->e_nname;
785 scopef.f_next = xf.f_or == filter ? filter : &xf ;
786 /* Filter depth increased again, adding scope clause */
789 #ifdef BDB_SUBENTRIES
790 if( get_subentries_visibility( op ) ) {
791 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
792 sf.f_choice = LDAP_FILTER_EQUALITY;
793 sf.f_ava = &aa_subentry;
794 sf.f_av_desc = slap_schema.si_ad_objectClass;
795 sf.f_av_value = bv_subentry;
796 sf.f_next = scopef.f_next;
801 /* Allocate IDL stack, plus 1 more for former tmp */
802 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
804 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
810 LDAP_LOG ( OPERATION, DETAIL1,
811 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
813 Debug(LDAP_DEBUG_TRACE,
814 "bdb_search_candidates: failed (rc=%d)\n",
820 LDAP_LOG ( OPERATION, DETAIL1,
821 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
822 (long) ids[0], (long) BDB_IDL_FIRST(ids),
823 (long) BDB_IDL_LAST(ids));
825 Debug(LDAP_DEBUG_TRACE,
826 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
828 (long) BDB_IDL_FIRST(ids),
829 (long) BDB_IDL_LAST(ids) );