1 /* search.c - ldbm backend search function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/string.h>
22 #include <ac/socket.h>
25 #include "back-ldbm.h"
26 #include "proto-back-ldbm.h"
28 static ID_BLOCK *base_candidate(
29 Backend *be, Entry *e );
31 static ID_BLOCK *search_candidates(
32 Operation *op, Entry *e, Filter *filter,
33 int scope, int deref, int manageDSAit );
41 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
43 const char *text = NULL;
48 Entry *matched = NULL;
49 struct berval realbase = BER_BVNULL;
50 int manageDSAit = get_manageDSAit( op );
53 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_search: enter\n", 0, 0, 0 );
55 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
58 /* grab giant lock for reading */
59 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
61 if ( op->o_req_ndn.bv_len == 0 ) {
62 /* DIT root special case */
63 e = (Entry *) &slap_entry_root;
65 /* need normalized dn below */
66 ber_dupbv( &realbase, &e->e_nname );
68 candidates = search_candidates( op, e, op->ors_filter,
69 op->ors_scope, op->ors_deref,
70 manageDSAit || get_domainScope(op) );
74 } else if ( op->ors_deref & LDAP_DEREF_FINDING ) {
75 /* deref dn and get entry with reader lock */
76 e = deref_dn_r( op->o_bd, &op->o_req_ndn,
77 &rs->sr_err, &matched, &rs->sr_text );
79 if( rs->sr_err == LDAP_NO_SUCH_OBJECT ) rs->sr_err = LDAP_REFERRAL;
82 /* get entry with reader lock */
83 e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched );
84 rs->sr_err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
89 struct berval matched_dn = BER_BVNULL;
91 if ( matched != NULL ) {
93 ber_dupbv( &matched_dn, &matched->e_name );
95 erefs = is_entry_referral( matched )
96 ? get_entry_referrals( op, matched )
99 cache_return_entry_r( &li->li_cache, matched );
102 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
103 &op->o_req_dn, op->ors_scope );
105 ber_bvarray_free( erefs );
109 rs->sr_ref = referral_rewrite( default_referral,
110 NULL, &op->o_req_dn, op->ors_scope );
113 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
115 rs->sr_matched = matched_dn.bv_val;
116 send_ldap_result( op, rs );
118 ber_bvarray_free( rs->sr_ref );
119 ber_memfree( matched_dn.bv_val );
121 rs->sr_matched = NULL;
122 return LDAP_REFERRAL;
125 if (!manageDSAit && is_entry_referral( e ) ) {
126 /* entry is a referral, don't allow add */
127 struct berval matched_dn;
130 ber_dupbv( &matched_dn, &e->e_name );
131 erefs = get_entry_referrals( op, e );
134 cache_return_entry_r( &li->li_cache, e );
135 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
138 LDAP_LOG( BACK_LDBM, INFO,
139 "ldbm_search: entry (%s) is a referral.\n",
142 Debug( LDAP_DEBUG_TRACE,
143 "ldbm_search: entry is referral\n",
148 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
149 &op->o_req_dn, op->ors_scope );
151 ber_bvarray_free( erefs );
154 rs->sr_matched = matched_dn.bv_val;
156 rs->sr_err = LDAP_REFERRAL;
157 send_ldap_result( op, rs );
158 ber_bvarray_free( rs->sr_ref );
161 send_ldap_error( op, rs, LDAP_OTHER,
162 "bad referral object" );
165 ber_memfree( matched_dn.bv_val );
167 rs->sr_matched = NULL;
171 if ( is_entry_alias( e ) ) {
173 op->ors_deref = LDAP_DEREF_NEVER;
176 if ( op->ors_scope == LDAP_SCOPE_BASE ) {
177 candidates = base_candidate( op->o_bd, e );
180 candidates = search_candidates( op, e, op->ors_filter,
181 op->ors_scope, op->ors_deref, manageDSAit );
184 /* need normalized dn below */
185 ber_dupbv( &realbase, &e->e_nname );
187 cache_return_entry_r( &li->li_cache, e );
190 if ( candidates == NULL ) {
193 LDAP_LOG( BACK_LDBM, INFO,
194 "ldbm_search: no candidates\n" , 0, 0, 0);
196 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
200 rs->sr_err = LDAP_SUCCESS;
201 send_ldap_result( op, rs );
207 /* if candidates exceed to-be-checked entries, abort */
208 if ( op->ors_limit /* isroot == FALSE */
209 && op->ors_limit->lms_s_unchecked != -1
210 && ID_BLOCK_NIDS( candidates ) > (unsigned) op->ors_limit->lms_s_unchecked )
212 send_ldap_error( op, rs, LDAP_ADMINLIMIT_EXCEEDED, NULL );
217 /* compute it anyway; root does not use it */
218 stoptime = op->o_time + op->ors_tlimit;
219 rs->sr_attrs = op->ors_attrs;
221 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
222 id = idl_nextid( candidates, &cursor ) )
227 /* check for abandon */
228 if ( op->o_abandon ) {
233 /* check time limit */
234 if ( op->ors_tlimit != SLAP_NO_LIMIT
235 && slap_get_time() > stoptime )
237 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
238 send_ldap_result( op, rs );
243 /* get the entry with reader lock */
244 e = id2entry_r( op->o_bd, id );
248 LDAP_LOG( BACK_LDBM, INFO,
249 "ldbm_search: candidate %ld not found.\n", id, 0, 0 );
251 Debug( LDAP_DEBUG_TRACE,
252 "ldbm_search: candidate %ld not found\n",
261 #ifdef LDBM_SUBENTRIES
262 if ( is_entry_subentry( e ) ) {
263 if( op->ors_scope != LDAP_SCOPE_BASE ) {
264 if(!get_subentries_visibility( op )) {
265 /* only subentries are visible */
268 } else if ( get_subentries( op ) &&
269 !get_subentries_visibility( op ))
271 /* only subentries are visible */
274 } else if ( get_subentries_visibility( op )) {
275 /* only subentries are visible */
280 if ( op->ors_deref & LDAP_DEREF_SEARCHING &&
281 is_entry_alias( e ) )
287 e = deref_entry_r( op->o_bd, e, &err, &matched, &text );
294 if( e->e_id == id ) {
299 /* need to skip alias which deref into scope */
300 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
302 dnParent( &e->e_nname, &pdn );
303 if ( ber_bvcmp( &pdn, &realbase ) ) {
307 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
308 /* alias is within scope */
310 LDAP_LOG( BACK_LDBM, DETAIL1,
311 "ldbm_search: alias \"%s\" in subtree\n",
314 Debug( LDAP_DEBUG_TRACE,
315 "ldbm_search: alias \"%s\" in subtree\n",
328 * If it's a referral, add it to the list of referrals.
329 * Only do this for non-base searches, and don't check
330 * the filter explicitly here since it's only a candidate
333 if ( !manageDSAit && op->ors_scope != LDAP_SCOPE_BASE &&
334 is_entry_referral( e ) )
339 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
340 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
341 dnParent( &e->e_nname, &dn );
342 scopeok = dn_match( &dn, &realbase );
344 scopeok = (realbase.bv_len == 0);
348 && op->ors_scope == LDAP_SCOPE_SUBTREE )
350 scopeok = dnIsSuffix( &e->e_nname, &realbase );
352 #ifdef LDAP_SCOPE_SUBORDINATE
354 && op->ors_scope == LDAP_SCOPE_SUBORDINATE )
356 scopeok = !dn_match( &e->e_nname, &realbase )
357 && dnIsSuffix( &e->e_nname, &realbase );
365 BerVarray erefs = get_entry_referrals( op, e );
366 rs->sr_ref = referral_rewrite( erefs,
368 op->ors_scope == LDAP_SCOPE_ONELEVEL
370 : LDAP_SCOPE_SUBTREE );
372 send_search_reference( op, rs );
374 ber_bvarray_free( rs->sr_ref );
379 LDAP_LOG( BACK_LDBM, DETAIL2,
380 "ldbm_search: candidate referral %ld scope not okay\n",
383 Debug( LDAP_DEBUG_TRACE,
384 "ldbm_search: candidate referral %ld scope not okay\n",
392 if ( !manageDSAit && is_entry_glue( e )) {
396 /* if it matches the filter and scope, send it */
397 result = test_filter( op, e, op->ors_filter );
399 if ( result == LDAP_COMPARE_TRUE ) {
403 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
404 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
405 dnParent( &e->e_nname, &dn );
406 scopeok = dn_match( &dn, &realbase );
408 scopeok = (realbase.bv_len == 0);
411 } else if ( !scopeok &&
412 op->ors_scope == LDAP_SCOPE_SUBTREE )
414 scopeok = dnIsSuffix( &e->e_nname, &realbase );
416 #ifdef LDAP_SCOPE_SUBORDINATE
417 } else if ( !scopeok &&
418 op->ors_scope == LDAP_SCOPE_SUBORDINATE )
420 scopeok = !dn_match( &e->e_nname, &realbase )
421 && dnIsSuffix( &e->e_nname, &realbase );
429 /* check size limit */
430 if ( --op->ors_slimit == -1 ) {
431 cache_return_entry_r( &li->li_cache, e );
432 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
434 send_ldap_result( op, rs );
441 result = send_search_entry( op, rs );
444 case 0: /* entry sent ok */
446 case 1: /* entry not sent */
448 case -1: /* connection closed */
449 cache_return_entry_r( &li->li_cache, e );
457 LDAP_LOG( BACK_LDBM, DETAIL2,
458 "ldbm_search: candidate entry %ld scope not okay\n",
461 Debug( LDAP_DEBUG_TRACE,
462 "ldbm_search: candidate entry %ld scope not okay\n",
469 LDAP_LOG( BACK_LDBM, DETAIL2,
470 "ldbm_search: candidate entry %ld does not match filter\n",
473 Debug( LDAP_DEBUG_TRACE,
474 "ldbm_search: candidate entry %ld does not match filter\n",
481 /* free reader lock */
482 cache_return_entry_r( &li->li_cache, e );
485 ldap_pvt_thread_yield();
488 rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
489 rs->sr_ref = rs->sr_v2ref;
490 send_ldap_result( op, rs );
495 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
497 if( candidates != NULL )
498 idl_free( candidates );
500 if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
501 if( realbase.bv_val ) free( realbase.bv_val );
514 LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
516 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
521 idl = idl_alloc( 1 );
522 idl_insert( &idl, e->e_id, 1 );
536 ID_BLOCK *candidates;
537 Filter f, fand, rf, af, xf;
538 AttributeAssertion aa_ref, aa_alias;
539 struct berval bv_ref = { sizeof("referral")-1, "referral" };
540 struct berval bv_alias = { sizeof("alias")-1, "alias" };
541 #ifdef LDBM_SUBENTRIES
543 AttributeAssertion aa_subentry;
547 LDAP_LOG( BACK_LDBM, DETAIL1,
548 "search_candidates: base (%s) scope %d deref %d\n",
549 e->e_ndn, scope, deref );
551 Debug(LDAP_DEBUG_TRACE,
552 "search_candidates: base=\"%s\" s=%d d=%d\n",
553 e->e_ndn, scope, deref );
558 xf.f_choice = LDAP_FILTER_OR;
562 /* match referrals */
563 rf.f_choice = LDAP_FILTER_EQUALITY;
565 rf.f_av_desc = slap_schema.si_ad_objectClass;
566 rf.f_av_value = bv_ref;
571 if( deref & LDAP_DEREF_SEARCHING ) {
573 af.f_choice = LDAP_FILTER_EQUALITY;
574 af.f_ava = &aa_alias;
575 af.f_av_desc = slap_schema.si_ad_objectClass;
576 af.f_av_value = bv_alias;
582 f.f_choice = LDAP_FILTER_AND;
584 fand.f_choice = scope == LDAP_SCOPE_ONELEVEL
585 ? SLAPD_FILTER_DN_ONE
586 : SLAPD_FILTER_DN_SUBTREE;
587 fand.f_dn = &e->e_nname;
588 fand.f_next = xf.f_or == filter ? filter : &xf ;
590 #ifdef LDBM_SUBENTRIES
591 if ( get_subentries_visibility( op )) {
592 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
593 sf.f_choice = LDAP_FILTER_EQUALITY;
594 sf.f_ava = &aa_subentry;
595 sf.f_av_desc = slap_schema.si_ad_objectClass;
596 sf.f_av_value = bv_subentry;
597 sf.f_next = fand.f_next;
602 candidates = filter_candidates( op, &f );
603 return( candidates );