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 #ifdef LDAP_CLIENT_UPDATE
60 Filter lcupf, csnfnot, csnfeq, csnfand, csnfge;
61 AttributeAssertion aa_ge, aa_eq;
64 #endif /* LDAP_CLIENT_UPDATE */
66 struct slap_limits_set *limit = NULL;
69 #ifdef SLAP_X_FILTER_HASSUBORDINATES
70 int filter_hasSubordinates = 0;
71 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
77 LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
79 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
83 manageDSAit = get_manageDSAit( op );
85 rc = LOCK_ID (bdb->bi_dbenv, &locker );
90 send_ldap_result( conn, op, rc=LDAP_OTHER,
91 NULL, "internal error", NULL, NULL );
95 if ( nbase->bv_len == 0 ) {
96 /* DIT root special case */
97 e = (Entry *) &slap_entry_root;
101 /* get entry with reader lock */
102 if ( deref & LDAP_DEREF_FINDING ) {
103 e = deref_dn_r( be, nbase-, &err, &matched, &text );
109 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
118 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
120 if (matched != NULL) {
121 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
123 send_ldap_result( conn, op, LDAP_BUSY,
124 NULL, "ldap server busy", NULL, NULL );
125 LOCK_ID_FREE (bdb->bi_dbenv, locker );
127 case DB_LOCK_DEADLOCK:
128 case DB_LOCK_NOTGRANTED:
132 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
134 if (matched != NULL) {
135 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
137 send_ldap_result( conn, op, rc=LDAP_OTHER,
138 NULL, "internal error", NULL, NULL );
139 LOCK_ID_FREE (bdb->bi_dbenv, locker );
144 struct berval matched_dn = { 0, NULL };
145 BerVarray refs = NULL;
147 if ( matched != NULL ) {
149 ber_dupbv( &matched_dn, &matched->e_name );
151 erefs = is_entry_referral( matched )
152 ? get_entry_referrals( be, conn, op, matched )
155 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
159 refs = referral_rewrite( erefs, &matched_dn,
161 ber_bvarray_free( erefs );
165 refs = referral_rewrite( default_referral,
169 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
170 matched_dn.bv_val, text, refs, NULL );
172 LOCK_ID_FREE (bdb->bi_dbenv, locker );
173 if ( refs ) ber_bvarray_free( refs );
174 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
178 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
179 /* entry is a referral, don't allow add */
180 struct berval matched_dn;
181 BerVarray erefs, refs;
183 ber_dupbv( &matched_dn, &e->e_name );
184 erefs = get_entry_referrals( be, conn, op, e );
187 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
191 refs = referral_rewrite( erefs, &matched_dn,
193 ber_bvarray_free( erefs );
197 LDAP_LOG ( OPERATION, RESULTS,
198 "bdb_search: entry is referral\n", 0, 0, 0 );
200 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
204 send_ldap_result( conn, op, LDAP_REFERRAL,
206 refs ? NULL : "bad referral object",
209 LOCK_ID_FREE (bdb->bi_dbenv, locker );
210 ber_bvarray_free( refs );
211 ber_memfree( matched_dn.bv_val );
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 /* The time/size limits come first because they require very little
223 * effort, so there's no chance the candidates are selected and then
224 * the request is not honored only because of time/size constraints */
226 /* if no time limit requested, use soft limit (unless root!) */
229 tlimit = -1; /* allow root to set no limit */
237 /* if no limit is required, use soft limit */
239 tlimit = limit->lms_t_soft;
241 /* if requested limit higher than hard limit, abort */
242 } else if ( tlimit > limit->lms_t_hard ) {
243 /* no hard limit means use soft instead */
244 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
245 tlimit = limit->lms_t_soft;
247 /* positive hard limit means abort */
248 } else if ( limit->lms_t_hard > 0 ) {
249 send_search_result( conn, op,
250 LDAP_UNWILLING_TO_PERFORM,
251 NULL, NULL, NULL, NULL, 0 );
256 /* negative hard limit means no limit */
259 /* if no limit is required, use soft limit */
261 slimit = limit->lms_s_soft;
263 /* if requested limit higher than hard limit, abort */
264 } else if ( slimit > limit->lms_s_hard ) {
265 /* no hard limit means use soft instead */
266 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
267 slimit = limit->lms_s_soft;
269 /* positive hard limit means abort */
270 } else if ( limit->lms_s_hard > 0 ) {
271 send_search_result( conn, op,
272 LDAP_UNWILLING_TO_PERFORM,
273 NULL, NULL, NULL, NULL, 0 );
278 /* negative hard limit means no limit */
282 /* compute it anyway; root does not use it */
283 stoptime = op->o_time + tlimit;
285 /* select candidates */
286 if ( scope == LDAP_SCOPE_BASE ) {
287 rc = base_candidate( be, e, candidates );
290 BDB_IDL_ALL( bdb, candidates );
291 rc = search_candidates( be, op, e, filter,
292 scope, deref, candidates );
295 /* need normalized dn below */
296 ber_dupbv( &realbase, &e->e_nname );
298 /* start cursor at base entry's id
299 * FIXME: hack to make "" base work
300 * FIXME: moddn needs to assign new ID for this to work
302 cursor = e->e_id == NOID ? 1 : e->e_id;
304 if ( e != &slap_entry_root ) {
305 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
309 if ( candidates[0] == 0 ) {
311 LDAP_LOG ( OPERATION, RESULTS, "bdb_search: no candidates\n", 0, 0, 0 );
313 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
317 send_search_result( conn, op,
319 NULL, NULL, NULL, NULL, 0 );
325 /* if not root and candidates exceed to-be-checked entries, abort */
326 if ( !isroot && limit->lms_s_unchecked != -1 ) {
327 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
328 send_search_result( conn, op,
329 LDAP_ADMINLIMIT_EXCEEDED,
330 NULL, NULL, NULL, NULL, 0 );
336 #ifdef SLAP_X_FILTER_HASSUBORDINATES
338 * is hasSubordinates used in the filter ?
339 * FIXME: we may compute this directly when parsing the filter
341 filter_hasSubordinates = filter_has_subordinates( filter );
342 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
344 #ifdef LDAP_CLIENT_UPDATE
345 if ( op->o_clientupdatetype == SYNCHRONIZE_ONLY ||
346 op->o_clientupdatetype == SYNCHRONIZE_AND_PERSIST ) {
347 lcupf.f_choice = LDAP_FILTER_AND;
348 lcupf.f_and = &csnfnot;
351 csnfnot.f_choice = LDAP_FILTER_NOT;
352 csnfnot.f_not = &csnfeq;
353 csnfnot.f_next = &csnfand;
355 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
356 csnfeq.f_ava = &aa_eq;
357 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
358 ber_dupbv(&csnfeq.f_av_value, op->o_clientupdatestate);
360 csnfand.f_choice = LDAP_FILTER_AND;
361 csnfand.f_and = &csnfge;
362 csnfand.f_next = NULL;
364 csnfge.f_choice = LDAP_FILTER_GE;
365 csnfge.f_ava = &aa_ge;
366 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
367 ber_dupbv(&csnfge.f_av_value, op->o_clientupdatestate);
368 csnfge.f_next = filter;
370 #endif /* LDAP_CLIENT_UPDATE */
372 for ( id = bdb_idl_first( candidates, &cursor );
374 id = bdb_idl_next( candidates, &cursor ) )
377 #ifdef SLAP_X_FILTER_HASSUBORDINATES
378 Attribute *hasSubordinates = NULL;
379 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
381 /* check for abandon */
382 if ( op->o_abandon ) {
387 /* check time limit */
388 if ( tlimit != -1 && slap_get_time() > stoptime ) {
389 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
390 NULL, NULL, v2refs, NULL, nentries );
395 /* get the entry with reader lock */
396 rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
398 if (rc == LDAP_BUSY) {
399 send_ldap_result( conn, op, rc=LDAP_BUSY,
400 NULL, "ldap server busy", NULL, NULL );
403 } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
408 if( !BDB_IDL_IS_RANGE(candidates) ) {
409 /* only complain for non-range IDLs */
411 LDAP_LOG ( OPERATION, RESULTS,
412 "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
414 Debug( LDAP_DEBUG_TRACE,
415 "bdb_search: candidate %ld not found\n",
423 #ifdef BDB_SUBENTRIES
424 if ( is_entry_subentry( e ) ) {
425 if( scope != LDAP_SCOPE_BASE ) {
426 if(!get_subentries_visibility( op )) {
427 /* only subentries are visible */
431 } else if ( get_subentries( op ) &&
432 !get_subentries_visibility( op ))
434 /* only subentries are visible */
438 } else if ( get_subentries_visibility( op )) {
439 /* only subentries are visible */
445 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
450 e = deref_entry_r( be, e, &err, &matched, &text );
457 if( e->e_id == id ) {
462 /* need to skip alias which deref into scope */
463 if( scope & LDAP_SCOPE_ONELEVEL ) {
466 dnParent( &e->e_nname, &pdn ):
467 if ( ber_bvcmp( pdn, &realbase ) ) {
471 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
472 /* alias is within scope */
474 LDAP_LOG ( OPERATION, RESULTS,
475 "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
477 Debug( LDAP_DEBUG_TRACE,
478 "bdb_search: \"%s\" in subtree\n",
489 * if it's a referral, add it to the list of referrals. only do
490 * this for non-base searches, and don't check the filter
491 * explicitly here since it's only a candidate anyway.
493 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
494 is_entry_referral( e ) )
499 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
500 if ( !be_issuffix( be, &e->e_nname ) ) {
501 dnParent( &e->e_nname, &dn );
502 scopeok = dn_match( &dn, &realbase );
504 scopeok = (realbase.bv_len == 0);
507 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
508 scopeok = dnIsSuffix( &e->e_nname, &realbase );
515 BerVarray erefs = get_entry_referrals(
517 BerVarray refs = referral_rewrite( erefs,
519 scope == LDAP_SCOPE_SUBTREE
523 send_search_reference( be, conn, op,
524 e, refs, NULL, &v2refs );
526 ber_bvarray_free( refs );
530 LDAP_LOG(OPERATION, DETAIL2,
531 "bdb_search: candidate referral %ld scope not okay\n",
534 Debug( LDAP_DEBUG_TRACE,
535 "bdb_search: candidate referral %ld scope not okay\n",
543 #ifdef SLAP_X_FILTER_HASSUBORDINATES
545 * if hasSubordinates is used in the filter,
546 * append it to the entry's attributes
548 if ( filter_hasSubordinates ) {
551 rc = bdb_hasSubordinates( be, conn, op, e, &hs);
552 if ( rc != LDAP_SUCCESS ) {
556 hasSubordinates = slap_operational_hasSubordinate( hs == LDAP_COMPARE_TRUE );
557 if ( hasSubordinates == NULL ) {
561 hasSubordinates->a_next = e->e_attrs;
562 e->e_attrs = hasSubordinates;
564 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
566 /* if it matches the filter and scope, send it */
567 #ifdef LDAP_CLIENT_UPDATE
568 if ( op->o_clientupdatetype == SYNCHRONIZE_ONLY ||
569 op->o_clientupdatetype == SYNCHRONIZE_AND_PERSIST ) {
570 rc = test_filter( be, conn, op, e, &lcupf );
573 rc = test_filter( be, conn, op, e, filter );
575 #else /* LDAP_CLIENT_UPDATE */
576 rc = test_filter( be, conn, op, e, filter );
577 #endif /* LDAP_CLIENT_UPDATE */
579 #ifdef SLAP_X_FILTER_HASSUBORDINATES
580 if ( hasSubordinates ) {
582 * FIXME: this is fairly inefficient, because
583 * if hasSubordinates is among the required
584 * attrs, it will be added again later;
585 * maybe we should leave it and check
586 * check later if it's already present,
589 e->e_attrs = e->e_attrs->a_next;
590 attr_free( hasSubordinates );
592 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
594 if ( rc == LDAP_COMPARE_TRUE ) {
598 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
599 if ( be_issuffix( be, &e->e_nname ) ) {
600 scopeok = (realbase.bv_len == 0);
602 dnParent( &e->e_nname, &dn );
603 scopeok = dn_match( &dn, &realbase );
606 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
607 scopeok = dnIsSuffix( &e->e_nname, &realbase );
614 /* check size limit */
615 if ( --slimit == -1 ) {
616 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
618 send_search_result( conn, op,
619 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
620 v2refs, NULL, nentries );
627 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
632 result = send_search_entry( be, conn, op,
633 e, attrs, attrsonly, NULL);
640 case 0: /* entry sent ok */
643 case 1: /* entry not sent */
645 case -1: /* connection closed */
646 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
654 LDAP_LOG ( OPERATION, RESULTS,
655 "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
657 Debug( LDAP_DEBUG_TRACE,
658 "bdb_search: %ld scope not okay\n",
664 LDAP_LOG ( OPERATION, RESULTS,
665 "bdb_search: %ld does match filter\n", (long) id, 0, 0);
667 Debug( LDAP_DEBUG_TRACE,
668 "bdb_search: %ld does match filter\n",
675 /* free reader lock */
676 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e , &lock);
680 ldap_pvt_thread_yield();
683 send_search_result( conn, op,
684 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
685 NULL, NULL, v2refs, NULL, nentries );
691 /* free reader lock */
692 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
695 LOCK_ID_FREE (bdb->bi_dbenv, locker );
697 if( v2refs ) ber_bvarray_free( v2refs );
698 if( realbase.bv_val ) ch_free( realbase.bv_val );
704 static int base_candidate(
710 LDAP_LOG ( OPERATION, ENTRY,
711 "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
713 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
714 e->e_dn, (long) e->e_id, 0);
722 /* Look for "objectClass Present" in this filter.
723 * Also count depth of filter tree while we're at it.
725 static int oc_filter(
733 if( cur > *max ) *max = cur;
735 switch(f->f_choice) {
736 case LDAP_FILTER_PRESENT:
737 if (f->f_desc == slap_schema.si_ad_objectClass)
741 case LDAP_FILTER_AND:
744 for (f=f->f_and; f; f=f->f_next)
745 rc |= oc_filter(f, cur, max);
753 static int search_candidates(
763 Filter f, scopef, rf, xf;
765 AttributeAssertion aa_ref;
766 #ifdef BDB_SUBENTRIES
768 AttributeAssertion aa_subentry;
772 AttributeAssertion aa_alias;
776 * This routine takes as input a filter (user-filter)
777 * and rewrites it as follows:
778 * (&(scope=DN)[(objectClass=subentry)]
779 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
783 LDAP_LOG ( OPERATION, ENTRY,
784 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
785 e->e_dn, (long) e->e_id, scope);
787 Debug(LDAP_DEBUG_TRACE,
788 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
789 e->e_dn, (long) e->e_id, scope );
793 xf.f_choice = LDAP_FILTER_OR;
796 /* If the user's filter uses objectClass=*,
797 * these clauses are redundant.
799 if (!oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
800 if( !get_manageDSAit(op) ) { /* match referrals */
801 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
802 rf.f_choice = LDAP_FILTER_EQUALITY;
804 rf.f_av_desc = slap_schema.si_ad_objectClass;
805 rf.f_av_value = bv_ref;
811 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
812 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
813 af.f_choice = LDAP_FILTER_EQUALITY;
814 af.f_ava = &aa_alias;
815 af.f_av_desc = slap_schema.si_ad_objectClass;
816 af.f_av_value = bv_alias;
821 /* We added one of these clauses, filter depth increased */
822 if( xf.f_or != filter ) depth++;
826 f.f_choice = LDAP_FILTER_AND;
828 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
829 ? SLAPD_FILTER_DN_SUBTREE
830 : SLAPD_FILTER_DN_ONE;
831 scopef.f_dn = &e->e_nname;
832 scopef.f_next = xf.f_or == filter ? filter : &xf ;
833 /* Filter depth increased again, adding scope clause */
836 #ifdef BDB_SUBENTRIES
837 if( get_subentries_visibility( op ) ) {
838 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
839 sf.f_choice = LDAP_FILTER_EQUALITY;
840 sf.f_ava = &aa_subentry;
841 sf.f_av_desc = slap_schema.si_ad_objectClass;
842 sf.f_av_value = bv_subentry;
843 sf.f_next = scopef.f_next;
848 /* Allocate IDL stack, plus 1 more for former tmp */
849 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
851 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
857 LDAP_LOG ( OPERATION, DETAIL1,
858 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
860 Debug(LDAP_DEBUG_TRACE,
861 "bdb_search_candidates: failed (rc=%d)\n",
867 LDAP_LOG ( OPERATION, DETAIL1,
868 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
869 (long) ids[0], (long) BDB_IDL_FIRST(ids),
870 (long) BDB_IDL_LAST(ids));
872 Debug(LDAP_DEBUG_TRACE,
873 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
875 (long) BDB_IDL_FIRST(ids),
876 (long) BDB_IDL_LAST(ids) );