]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/search.c
final dn_parent elimination in back-bdb
[openldap] / servers / slapd / back-bdb / search.c
1 /* search.c - search operation */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "idl.h"
15 #include "external.h"
16
17 static int base_candidate(
18         BackendDB       *be,
19         Entry   *e,
20         ID              *ids );
21 static int search_candidates(
22         BackendDB *be,
23         Operation *op,
24         Entry *e,
25         Filter *filter,
26         int scope,
27         int deref,
28         ID      *ids );
29
30 int
31 bdb_search(
32         BackendDB       *be,
33         Connection      *conn,
34         Operation       *op,
35         struct berval   *base,
36         struct berval   *nbase,
37         int             scope,
38         int             deref,
39         int             slimit,
40         int             tlimit,
41         Filter  *filter,
42         struct berval   *filterstr,
43         AttributeName   *attrs,
44         int             attrsonly )
45 {
46         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
47         int              abandon;
48         int             rc;
49         const char *text = NULL;
50         time_t          stoptime;
51         ID              id, cursor;
52         ID              candidates[BDB_IDL_UM_SIZE];
53         Entry           *e = NULL;
54         BerVarray v2refs = NULL;
55         Entry   *matched = NULL;
56         struct berval   realbase = { 0, NULL };
57         int             nentries = 0;
58         int             manageDSAit;
59
60         struct slap_limits_set *limit = NULL;
61         int isroot = 0;
62
63         Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
64                 0, 0, 0);
65
66         manageDSAit = get_manageDSAit( op );
67
68         if ( nbase->bv_len == 0 ) {
69                 /* DIT root special case */
70                 e = (Entry *) &slap_entry_root;
71                 rc = 0;
72         } else                                          
73 #ifdef BDB_ALIASES
74         /* get entry with reader lock */
75         if ( deref & LDAP_DEREF_FINDING ) {
76                 e = deref_dn_r( be, nbase-, &err, &matched, &text );
77
78         } else
79 #endif
80         {
81                 rc = bdb_dn2entry( be, NULL, nbase, &e, &matched, 0 );
82         }
83
84         switch(rc) {
85         case DB_NOTFOUND:
86         case 0:
87                 break;
88         default:
89                 send_ldap_result( conn, op, rc=LDAP_OTHER,
90                         NULL, "internal error", NULL, NULL );
91                 return rc;
92         }
93
94         if ( e == NULL ) {
95                 struct berval matched_dn = { 0, NULL };
96                 BerVarray refs = NULL;
97
98                 if ( matched != NULL ) {
99                         BerVarray erefs;
100
101                         ber_dupbv( &matched_dn, &matched->e_name );
102
103                         erefs = is_entry_referral( matched )
104                                 ? get_entry_referrals( be, conn, op, matched )
105                                 : NULL;
106
107                         bdb_entry_return( be, matched );
108                         matched = NULL;
109
110                         if( erefs ) {
111                                 refs = referral_rewrite( erefs, &matched_dn,
112                                         base, scope );
113                                 ber_bvarray_free( erefs );
114                         }
115
116                 } else {
117                         refs = referral_rewrite( default_referral,
118                                 NULL, base, scope );
119                 }
120
121                 send_ldap_result( conn, op,     rc=LDAP_REFERRAL ,
122                         matched_dn.bv_val, text, refs, NULL );
123
124                 if ( refs ) ber_bvarray_free( refs );
125                 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
126                 return rc;
127         }
128
129         if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
130                 /* entry is a referral, don't allow add */
131                 struct berval matched_dn;
132                 BerVarray erefs, refs;
133                 
134                 ber_dupbv( &matched_dn, &e->e_name );
135                 erefs = get_entry_referrals( be, conn, op, e );
136                 refs = NULL;
137
138                 bdb_entry_return( be, e );
139                 e = NULL;
140
141                 if( erefs ) {
142                         refs = referral_rewrite( erefs, &matched_dn,
143                                 base, scope );
144                         ber_bvarray_free( erefs );
145                 }
146
147                 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
148                         0, 0, 0 );
149
150                 send_ldap_result( conn, op, LDAP_REFERRAL,
151                         matched_dn.bv_val,
152                         refs ? NULL : "bad referral object",
153                         refs, NULL );
154
155                 ber_bvarray_free( refs );
156                 ber_memfree( matched_dn.bv_val );
157                 return 1;
158         }
159
160         /* if not root, get appropriate limits */
161         if ( be_isroot( be, &op->o_ndn ) ) {
162                 isroot = 1;
163         } else {
164                 ( void ) get_limits( be, &op->o_ndn, &limit );
165         }
166
167         /* The time/size limits come first because they require very little
168          * effort, so there's no chance the candidates are selected and then 
169          * the request is not honored only because of time/size constraints */
170
171         /* if no time limit requested, use soft limit (unless root!) */
172         if ( isroot ) {
173                 if ( tlimit == 0 ) {
174                         tlimit = -1;    /* allow root to set no limit */
175                 }
176
177                 if ( slimit == 0 ) {
178                         slimit = -1;
179                 }
180
181         } else {
182                 /* if no limit is required, use soft limit */
183                 if ( tlimit <= 0 ) {
184                         tlimit = limit->lms_t_soft;
185
186                 /* if requested limit higher than hard limit, abort */
187                 } else if ( tlimit > limit->lms_t_hard ) {
188                         /* no hard limit means use soft instead */
189                         if ( limit->lms_t_hard == 0 ) {
190                                 tlimit = limit->lms_t_soft;
191
192                         /* positive hard limit means abort */
193                         } else if ( limit->lms_t_hard > 0 ) {
194                                 send_search_result( conn, op, 
195                                                 LDAP_UNWILLING_TO_PERFORM,
196                                                 NULL, NULL, NULL, NULL, 0 );
197                                 rc = 0;
198                                 goto done;
199                         }
200                 
201                         /* negative hard limit means no limit */
202                 }
203                 
204                 /* if no limit is required, use soft limit */
205                 if ( slimit <= 0 ) {
206                         slimit = limit->lms_s_soft;
207
208                 /* if requested limit higher than hard limit, abort */
209                 } else if ( slimit > limit->lms_s_hard ) {
210                         /* no hard limit means use soft instead */
211                         if ( limit->lms_s_hard == 0 ) {
212                                 slimit = limit->lms_s_soft;
213
214                         /* positive hard limit means abort */
215                         } else if ( limit->lms_s_hard > 0 ) {
216                                 send_search_result( conn, op, 
217                                                 LDAP_UNWILLING_TO_PERFORM,
218                                                 NULL, NULL, NULL, NULL, 0 );
219                                 rc = 0; 
220                                 goto done;
221                         }
222                         
223                         /* negative hard limit means no limit */
224                 }
225         }
226
227         /* compute it anyway; root does not use it */
228         stoptime = op->o_time + tlimit;
229
230         /* select candidates */
231         if ( scope == LDAP_SCOPE_BASE ) {
232                 rc = base_candidate( be, e, candidates );
233
234         } else {
235                 BDB_IDL_ALL( bdb, candidates );
236                 rc = search_candidates( be, op, e, filter,
237                         scope, deref, candidates );
238         }
239
240         /* need normalized dn below */
241         ber_dupbv( &realbase, &e->e_nname );
242
243         /* start cursor at base entry's id 
244          * FIXME: hack to make "" base work */
245         cursor = e->e_id == NOID ? 1 : e->e_id;
246
247         if ( e != &slap_entry_root ) {
248                 bdb_entry_return( be, e );
249         }
250         e = NULL;
251
252         if ( candidates[0] == 0 ) {
253                 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
254                         0, 0, 0 );
255
256                 send_search_result( conn, op,
257                         LDAP_SUCCESS,
258                         NULL, NULL, NULL, NULL, 0 );
259
260                 rc = 1;
261                 goto done;
262         }
263
264         /* if not root and candidates exceed to-be-checked entries, abort */
265         if ( !isroot && limit->lms_s_unchecked != -1 ) {
266                 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
267                         send_search_result( conn, op, 
268                                         LDAP_UNWILLING_TO_PERFORM,
269                                         NULL, NULL, NULL, NULL, 0 );
270                         rc = 1;
271                         goto done;
272                 }
273         }
274
275         for ( id = bdb_idl_first( candidates, &cursor );
276                 id != NOID;
277                 id = bdb_idl_next( candidates, &cursor ) )
278         {
279                 int             scopeok = 0;
280
281                 /* check for abandon */
282                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
283                 abandon = op->o_abandon;
284                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
285
286                 if ( abandon ) {
287                         rc = 0;
288                         goto done;
289                 }
290
291                 /* check time limit */
292                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
293                         send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
294                                 NULL, NULL, v2refs, NULL, nentries );
295                         goto done;
296                 }
297
298                 /* get the entry with reader lock */
299                 rc = bdb_id2entry( be, NULL, id, &e );
300
301                 if ( e == NULL ) {
302                         if( !BDB_IDL_IS_RANGE(candidates) ) {
303                                 /* only complain for non-range IDLs */
304                                 Debug( LDAP_DEBUG_TRACE,
305                                         "bdb_search: candidate %ld not found\n",
306                                         (long) id, 0, 0 );
307                         }
308
309                         goto loop_continue;
310                 }
311
312                 if ( is_entry_subentry( e ) ) {
313                         if( scope != LDAP_SCOPE_BASE ) {
314                                 if(!get_subentries_visibility( op )) {
315                                         /* only subentries are visible */
316                                         goto loop_continue;
317                                 }
318                         } else if ( get_subentries( op ) &&
319                                 !get_subentries_visibility( op ))
320                         {
321                                 /* only subentries are visible */
322                                 goto loop_continue;
323                         }
324                 } else if ( get_subentries_visibility( op )) {
325                         /* only subentries are visible */
326                         goto loop_continue;
327                 }
328
329 #ifdef BDB_ALIASES
330                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
331                         Entry *matched;
332                         int err;
333                         const char *text;
334                         
335                         e = deref_entry_r( be, e, &err, &matched, &text );
336
337                         if( e == NULL ) {
338                                 e = matched;
339                                 goto loop_continue;
340                         }
341
342                         if( e->e_id == id ) {
343                                 /* circular loop */
344                                 goto loop_continue;
345                         }
346
347                         /* need to skip alias which deref into scope */
348                         if( scope & LDAP_SCOPE_ONELEVEL ) {
349                                 char            *pdn;
350                                 ber_len_t       plen;
351                                 
352                                 if ( dnParent( e->e_nname.bv_val, &pdn ) == LDAP_SUCCESS ) {
353                                         plen = e->e_nname.bv_len - ( pdn - e->e_nname.bv_val );
354                                         if ( plen != realbase.bv_len || strcmp( pdn, realbase.bv_val ) ) {
355                                                 goto loop_continue;
356                                         }
357                                 }
358
359                         } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
360                                 /* alias is within scope */
361                                 Debug( LDAP_DEBUG_TRACE,
362                                         "bdb_search: \"%s\" in subtree\n",
363                                         e->e_dn, 0, 0 );
364                                 goto loop_continue;
365                         }
366
367                         scopeok = 1;
368                 }
369 #endif
370
371                 /*
372                  * if it's a referral, add it to the list of referrals. only do
373                  * this for non-base searches, and don't check the filter
374                  * explicitly here since it's only a candidate anyway.
375                  */
376                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
377                         is_entry_referral( e ) )
378                 {
379                         BerVarray erefs = get_entry_referrals(
380                                 be, conn, op, e );
381                         BerVarray refs = referral_rewrite( erefs,
382                                 &e->e_name, NULL,
383                                 scope == LDAP_SCOPE_SUBTREE 
384                                         ? LDAP_SCOPE_SUBTREE
385                                         : LDAP_SCOPE_BASE );
386
387                         send_search_reference( be, conn, op,
388                                 e, refs, NULL, &v2refs );
389
390                         ber_bvarray_free( refs );
391
392                         goto loop_continue;
393                 }
394
395                 /* if it matches the filter and scope, send it */
396                 rc = test_filter( be, conn, op, e, filter );
397                 if ( rc == LDAP_COMPARE_TRUE ) {
398                         char    *dn;
399
400                         /* check scope */
401                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
402                                 if ( be_issuffix( be, e->e_nname.bv_val ) ) {
403                                         scopeok = (realbase.bv_len == 0);
404                                 } else {
405                                         dnParent( e->e_nname.bv_val, (const char **)&dn );
406                                         scopeok = (dn == realbase.bv_val)
407                                                 ? 1
408                                                 : (strcmp( dn, realbase.bv_val ) ? 0 : 1 );
409                                 }
410
411                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
412                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
413
414                         } else {
415                                 scopeok = 1;
416                         }
417
418                         if ( scopeok ) {
419                                 /* check size limit */
420                                 if ( --slimit == -1 ) {
421                                         bdb_entry_return( be, e );
422                                         e = NULL;
423                                         send_search_result( conn, op,
424                                                 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
425                                                 v2refs, NULL, nentries );
426                                         goto done;
427                                 }
428
429                                 if (e) {
430                                         int result = send_search_entry( be, conn, op,
431                                                 e, attrs, attrsonly, NULL);
432
433                                         switch (result) {
434                                         case 0:         /* entry sent ok */
435                                                 nentries++;
436                                                 break;
437                                         case 1:         /* entry not sent */
438                                                 break;
439                                         case -1:        /* connection closed */
440                                                 bdb_entry_return( be, e );
441                                                 e = NULL;
442                                                 rc = LDAP_OTHER;
443                                                 goto done;
444                                         }
445                                 }
446                         } else {
447                                 Debug( LDAP_DEBUG_TRACE,
448                                         "bdb_search: %ld scope not okay\n",
449                                         (long) id, 0, 0 );
450                         }
451                 } else {
452                         Debug( LDAP_DEBUG_TRACE,
453                                 "bdb_search: %ld does match filter\n",
454                                 (long) id, 0, 0 );
455                 }
456
457 loop_continue:
458                 if( e != NULL ) {
459                         /* free reader lock */
460                         bdb_entry_return( be, e );
461                 }
462
463                 ldap_pvt_thread_yield();
464         }
465         send_search_result( conn, op,
466                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
467                 NULL, NULL, v2refs, NULL, nentries );
468
469         rc = 0;
470
471 done:
472         if( v2refs ) ber_bvarray_free( v2refs );
473         if( realbase.bv_val ) ch_free( realbase.bv_val );
474
475         return rc;
476 }
477
478
479 static int base_candidate(
480         BackendDB       *be,
481         Entry   *e,
482         ID              *ids )
483 {
484         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
485                 e->e_dn, (long) e->e_id, 0);
486
487         ids[0] = 1;
488         ids[1] = e->e_id;
489         return 0;
490 }
491
492 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
493  * doesn't count, we're looking for explicit values.
494  */
495 static int oc_filter(
496         Filter *f
497 )
498 {
499         int rc = 0;
500
501         switch(f->f_choice) {
502         case LDAP_FILTER_EQUALITY:
503         case LDAP_FILTER_APPROX:
504                 if (f->f_av_desc == slap_schema.si_ad_objectClass)
505                         rc = 1;
506                 break;
507
508         case LDAP_FILTER_SUBSTRINGS:
509                 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
510                         rc = 1;
511                 break;
512
513         case LDAP_FILTER_AND:
514         case LDAP_FILTER_OR:
515                 for (f=f->f_and; f; f=f->f_next)
516                         if ((rc = oc_filter(f)))
517                                 break;
518                 break;
519         default:
520                 break;
521         }
522         return rc;
523 }
524
525 static int search_candidates(
526         BackendDB *be,
527         Operation *op,
528         Entry *e,
529         Filter *filter,
530         int scope,
531         int deref,
532         ID      *ids )
533 {
534         int rc;
535         Filter          f, scopef, sf, rf, xf;
536         ID              tmp[BDB_IDL_UM_SIZE];
537         AttributeAssertion aa_ref;
538         AttributeAssertion aa_subentry;
539         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
540 #ifdef BDB_ALIASES
541         Filter  af;
542         AttributeAssertion aa_alias;
543 #endif
544
545         /*
546          * This routine takes as input a filter (user-filter)
547          * and rewrites it as follows:
548          *      (&(scope=DN)[(objectClass=subentry)]
549          *              (|[(objectClass=referral)(objectClass=alias)](user-filter))
550          */
551
552         Debug(LDAP_DEBUG_TRACE,
553                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
554                 e->e_dn, (long) e->e_id, scope );
555
556         xf.f_or = filter;
557         xf.f_choice = LDAP_FILTER_OR;
558         xf.f_next = NULL;
559
560         /* If the user's filter doesn't mention objectClass, or if
561          * it just uses objectClass=*, these clauses are redundant.
562          */
563         if (oc_filter(filter) && !get_subentries_visibility(op) ) {
564                 if( !get_manageDSAit(op) ) { /* match referrals */
565                         struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
566                         rf.f_choice = LDAP_FILTER_EQUALITY;
567                         rf.f_ava = &aa_ref;
568                         rf.f_av_desc = slap_schema.si_ad_objectClass;
569                         rf.f_av_value = bv_ref;
570                         rf.f_next = xf.f_or;
571                         xf.f_or = &rf;
572                 }
573
574 #ifdef BDB_ALIASES
575                 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
576                         struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
577                         af.f_choice = LDAP_FILTER_EQUALITY;
578                         af.f_ava = &aa_alias;
579                         af.f_av_desc = slap_schema.si_ad_objectClass;
580                         af.f_av_value = bv_alias;
581                         af.f_next = xf.f_or;
582                         xf.f_or = &af;
583                 }
584 #endif
585         }
586
587         f.f_next = NULL;
588         f.f_choice = LDAP_FILTER_AND;
589         f.f_and = &scopef;
590         scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
591                 ? SLAPD_FILTER_DN_SUBTREE
592                 : SLAPD_FILTER_DN_ONE;
593         scopef.f_dn = &e->e_nname;
594         scopef.f_next = xf.f_or == filter ? filter : &xf ;
595
596         if( get_subentries_visibility( op ) ) {
597                 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
598                 sf.f_choice = LDAP_FILTER_EQUALITY;
599                 sf.f_ava = &aa_subentry;
600                 sf.f_av_desc = slap_schema.si_ad_objectClass;
601                 sf.f_av_value = bv_subentry;
602                 sf.f_next = scopef.f_next;
603                 scopef.f_next = &sf;
604         }
605
606 #ifdef BDB_FILTER_INDICES
607         rc = bdb_filter_candidates( be, &f, ids, tmp );
608 #else
609         /* FIXME: Original code:
610         BDB_IDL_ID( bdb, ids, e->e_id );
611         * this is a hack to make "" base work; when bdb_filter_candidates
612         * is used this should not be needed any more */
613         BDB_IDL_ID( bdb, ids, (e->e_id == NOID ? 1 : e->e_id) );
614         rc = 0;
615 #endif
616
617         Debug(LDAP_DEBUG_TRACE,
618                 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
619                 (long) ids[0],
620                 (long) BDB_IDL_FIRST(ids),
621                 (long) BDB_IDL_LAST(ids) );
622
623         return rc;
624 }