]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
Suck in HEAD changes since 2.1alpha
[openldap] / servers / slapd / back-ldbm / search.c
1 /* search.c - ldbm backend search function */
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
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
18
19 static ID_BLOCK *base_candidate(
20         Backend *be, Entry *e );
21
22 static ID_BLOCK *search_candidates(
23         Backend *be, Entry *e, Filter *filter,
24         int scope, int deref, int manageDSAit );
25
26
27 int
28 ldbm_back_search(
29     Backend     *be,
30     Connection  *conn,
31     Operation   *op,
32     struct berval       *base,
33     struct berval       *nbase,
34     int         scope,
35     int         deref,
36     int         slimit,
37     int         tlimit,
38     Filter      *filter,
39     struct berval       *filterstr,
40     AttributeName       *attrs,
41     int         attrsonly )
42 {
43         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
44         int             rc, err;
45         const char *text = NULL;
46         time_t          stoptime;
47         ID_BLOCK                *candidates;
48         ID              id, cursor;
49         Entry           *e;
50         BerVarray               v2refs = NULL;
51         Entry   *matched = NULL;
52         struct berval   realbase = { 0, NULL };
53         int             nentries = 0;
54         int             manageDSAit = get_manageDSAit( op );
55         int             cscope = LDAP_SCOPE_DEFAULT;
56
57         struct slap_limits_set *limit = NULL;
58         int isroot = 0;
59                 
60 #ifdef NEW_LOGGING
61         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
62                 "ldbm_back_search: enter\n" ));
63 #else
64         Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
65 #endif
66
67         /* grab giant lock for reading */
68         ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
69
70         if ( nbase->bv_len == 0 ) {
71                 /* DIT root special case */
72                 e = (Entry *) &slap_entry_root;
73
74                 /* need normalized dn below */
75                 ber_dupbv( &realbase, &e->e_nname );
76
77                 candidates = search_candidates( be, e, filter,
78                     scope, deref, manageDSAit );
79
80                 goto searchit;
81                 
82         } else if ( deref & LDAP_DEREF_FINDING ) {
83                 /* deref dn and get entry with reader lock */
84                 e = deref_dn_r( be, nbase, &err, &matched, &text );
85
86                 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
87
88         } else {
89                 /* get entry with reader lock */
90                 e = dn2entry_r( be, nbase, &matched );
91                 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
92                 text = NULL;
93         }
94
95         if ( e == NULL ) {
96                 struct berval matched_dn = { 0, NULL };
97                 BerVarray refs = NULL;
98
99                 if ( matched != NULL ) {
100                         BerVarray erefs;
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                         cache_return_entry_r( &li->li_cache, matched );
108
109                         if( erefs ) {
110                                 refs = referral_rewrite( erefs, &matched_dn,
111                                         base, scope );
112
113                                 ber_bvarray_free( erefs );
114                         }
115
116                 } else {
117                         refs = referral_rewrite( default_referral,
118                                 NULL, base, scope );
119                 }
120
121                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
122
123                 send_ldap_result( conn, op, err, matched_dn.bv_val, 
124                         text, refs, NULL );
125
126                 ber_bvarray_free( refs );
127                 ber_memfree( matched_dn.bv_val );
128                 return 1;
129         }
130
131         if (!manageDSAit && is_entry_referral( e ) ) {
132                 /* entry is a referral, don't allow add */
133                 struct berval matched_dn;
134                 BerVarray erefs;
135                 BerVarray refs;
136
137                 ber_dupbv( &matched_dn, &e->e_name );
138                 erefs = get_entry_referrals( be, conn, op, e );
139                 refs = NULL;
140
141                 cache_return_entry_r( &li->li_cache, e );
142                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
143
144 #ifdef NEW_LOGGING
145                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
146                         "ldbm_search: entry (%s) is a referral.\n",
147                         e->e_dn ));
148 #else
149                 Debug( LDAP_DEBUG_TRACE,
150                         "ldbm_search: entry is referral\n",
151                         0, 0, 0 );
152 #endif
153
154                 if( erefs ) {
155                         refs = referral_rewrite( erefs, &matched_dn,
156                                 base, scope );
157
158                         ber_bvarray_free( erefs );
159                 }
160
161                 if( refs ) {
162                         send_ldap_result( conn, op, LDAP_REFERRAL,
163                                 matched_dn.bv_val, NULL, refs, NULL );
164                         ber_bvarray_free( refs );
165
166                 } else {
167                         send_ldap_result( conn, op, LDAP_OTHER,
168                                 matched_dn.bv_val,
169                         "bad referral object", NULL, NULL );
170                 }
171
172                 ber_memfree( matched_dn.bv_val );
173                 return 1;
174         }
175
176         if ( is_entry_alias( e ) ) {
177                 /* don't deref */
178                 deref = LDAP_DEREF_NEVER;
179         }
180
181         if ( scope == LDAP_SCOPE_BASE ) {
182                 cscope = LDAP_SCOPE_BASE;
183                 candidates = base_candidate( be, e );
184
185         } else {
186                 cscope = ( scope != LDAP_SCOPE_SUBTREE )
187                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
188                 candidates = search_candidates( be, e, filter,
189                     scope, deref, manageDSAit );
190         }
191
192         /* need normalized dn below */
193         ber_dupbv( &realbase, &e->e_nname );
194
195         cache_return_entry_r( &li->li_cache, e );
196
197 searchit:
198         if ( candidates == NULL ) {
199                 /* no candidates */
200 #ifdef NEW_LOGGING
201                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
202                         "ldbm_search: no candidates\n" ));
203 #else
204                 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
205                         0, 0, 0 );
206 #endif
207
208                 send_search_result( conn, op,
209                         LDAP_SUCCESS,
210                         NULL, NULL, NULL, NULL, 0 );
211
212                 rc = 1;
213                 goto done;
214         }
215
216         /* if not root, get appropriate limits */
217         if ( be_isroot( be, &op->o_ndn ) ) {
218                 isroot = 1;
219         } else {
220                 ( void ) get_limits( be, &op->o_ndn, &limit );
221         }
222
223         /* if candidates exceed to-be-checked entries, abort */
224         if ( !isroot && limit->lms_s_unchecked != -1 ) {
225                 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
226                         send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
227                                         NULL, NULL, NULL, NULL, 0 );
228                         rc = 0;
229                         goto done;
230                 }
231         }
232         
233         /* if root an no specific limit is required, allow unlimited search */
234         if ( isroot ) {
235                 if ( tlimit == 0 ) {
236                         tlimit = -1;
237                 }
238
239                 if ( slimit == 0 ) {
240                         slimit = -1;
241                 }
242
243         } else {
244                 /* if no limit is required, use soft limit */
245                 if ( tlimit <= 0 ) {
246                         tlimit = limit->lms_t_soft;
247                 
248                 /* if requested limit higher than hard limit, abort */
249                 } else if ( tlimit > limit->lms_t_hard ) {
250                         /* no hard limit means use soft instead */
251                         if ( limit->lms_t_hard == 0 ) {
252                                 tlimit = limit->lms_t_soft;
253                         
254                         /* positive hard limit means abort */
255                         } else if ( limit->lms_t_hard > 0 ) {
256                                 send_search_result( conn, op, 
257                                                 LDAP_UNWILLING_TO_PERFORM,
258                                                 NULL, NULL, NULL, NULL, 0 );
259                                 rc = 0; 
260                                 goto done;
261                         }
262
263                         /* negative hard limit means no limit */
264                 }
265
266                 /* if no limit is required, use soft limit */
267                 if ( slimit <= 0 ) {
268                         slimit = limit->lms_s_soft;
269
270                 /* if requested limit higher than hard limit, abort */
271                 } else if ( slimit > limit->lms_s_hard ) {
272                         /* no hard limit means use soft instead */
273                         if ( limit->lms_s_hard == 0 ) {
274                                 slimit = limit->lms_s_soft;
275
276                         /* positive hard limit means abort */
277                         } else if ( limit->lms_s_hard > 0 ) {
278                                 send_search_result( conn, op,
279                                                 LDAP_UNWILLING_TO_PERFORM,
280                                                 NULL, NULL, NULL, NULL, 0 );
281                                 rc = 0;
282                                 goto done;
283                         }
284
285                         /* negative hard limit means no limit */
286                 }
287         }
288
289         /* compute it anyway; root does not use it */
290         stoptime = op->o_time + tlimit;
291
292         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
293             id = idl_nextid( candidates, &cursor ) )
294         {
295                 int scopeok = 0;
296
297                 /* check for abandon */
298                 if ( op->o_abandon ) {
299                         rc = 0;
300                         goto done;
301                 }
302
303                 /* check time limit */
304                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
305                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
306                                 NULL, NULL, v2refs, NULL, nentries );
307                         rc = 0;
308                         goto done;
309                 }
310
311                 /* get the entry with reader lock */
312                 e = id2entry_r( be, id );
313
314                 if ( e == NULL ) {
315 #ifdef NEW_LOGGING
316                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
317                                 "ldbm_search: candidate %ld not found.\n", id ));
318 #else
319                         Debug( LDAP_DEBUG_TRACE,
320                                 "ldbm_search: candidate %ld not found\n",
321                                 id, 0, 0 );
322 #endif
323
324                         goto loop_continue;
325                 }
326
327                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
328                         Entry *matched;
329                         int err;
330                         const char *text;
331                         
332                         e = deref_entry_r( be, e, &err, &matched, &text );
333
334                         if( e == NULL ) {
335                                 e = matched;
336                                 goto loop_continue;
337                         }
338
339                         if( e->e_id == id ) {
340                                 /* circular loop */
341                                 goto loop_continue;
342                         }
343
344                         /* need to skip alias which deref into scope */
345                         if( scope & LDAP_SCOPE_ONELEVEL ) {
346                                 struct berval pdn;
347                                 dnParent( &e->e_nname, &pdn );
348                                 if ( ber_bvcmp( &pdn, &realbase ) ) {
349                                         goto loop_continue;
350                                 }
351
352                         } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
353                                 /* alias is within scope */
354 #ifdef NEW_LOGGING
355                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
356                                         "ldbm_search: alias \"%s\" in subtree\n", e->e_dn ));
357 #else
358                                 Debug( LDAP_DEBUG_TRACE,
359                                         "ldbm_search: alias \"%s\" in subtree\n",
360                                         e->e_dn, 0, 0 );
361 #endif
362
363                                 goto loop_continue;
364                         }
365
366                         scopeok = 1;
367                 }
368
369                 /*
370                  * if it's a referral, add it to the list of referrals. only do
371                  * this for non-base searches, and don't check the filter
372                  * explicitly here since it's only a candidate anyway.
373                  */
374                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
375                         is_entry_referral( e ) )
376                 {
377                         struct berval   dn;
378
379                         /* check scope */
380                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
381                                 if ( !be_issuffix( be, &e->e_nname ) ) {
382                                         dnParent( &e->e_nname, &dn );
383                                         scopeok = dn_match( &dn, &realbase );
384                                 } else {
385                                         scopeok = (realbase.bv_len == 0);
386                                 }
387
388                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
389                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
390
391                         } else {
392                                 scopeok = 1;
393                         }
394
395                         if( scopeok ) {
396                                 BerVarray erefs = get_entry_referrals(
397                                         be, conn, op, e );
398                                 BerVarray refs = referral_rewrite( erefs,
399                                         &e->e_name, NULL,
400                                         scope == LDAP_SCOPE_SUBTREE
401                                                 ? LDAP_SCOPE_SUBTREE
402                                                 : LDAP_SCOPE_BASE );
403
404                                 send_search_reference( be, conn, op,
405                                         e, refs, NULL, &v2refs );
406
407                                 ber_bvarray_free( refs );
408
409                         } else {
410 #ifdef NEW_LOGGING
411                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
412                                         "ldbm_search: candidate referral %ld scope not okay\n",
413                                         id ));
414 #else
415                                 Debug( LDAP_DEBUG_TRACE,
416                                         "ldbm_search: candidate referral %ld scope not okay\n",
417                                         id, 0, 0 );
418 #endif
419                         }
420
421                         goto loop_continue;
422                 }
423
424                 /* if it matches the filter and scope, send it */
425                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
426                         struct berval   dn;
427
428                         /* check scope */
429                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
430                                 if ( !be_issuffix( be, &e->e_nname ) ) {
431                                         dnParent( &e->e_nname, &dn );
432                                         scopeok = dn_match( &dn, &realbase );
433                                 } else {
434                                         scopeok = (realbase.bv_len == 0);
435                                 }
436
437                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
438                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
439
440                         } else {
441                                 scopeok = 1;
442                         }
443
444                         if ( scopeok ) {
445                                 /* check size limit */
446                                 if ( --slimit == -1 ) {
447                                         cache_return_entry_r( &li->li_cache, e );
448                                         send_search_result( conn, op,
449                                                 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
450                                                 v2refs, NULL, nentries );
451                                         rc = 0;
452                                         goto done;
453                                 }
454
455                                 if (e) {
456                                         int result = send_search_entry(be, conn, op,
457                                                 e, attrs, attrsonly, NULL);
458
459                                         switch (result) {
460                                         case 0:         /* entry sent ok */
461                                                 nentries++;
462                                                 break;
463                                         case 1:         /* entry not sent */
464                                                 break;
465                                         case -1:        /* connection closed */
466                                                 cache_return_entry_r( &li->li_cache, e );
467                                                 rc = 0;
468                                                 goto done;
469                                         }
470                                 }
471                         } else {
472 #ifdef NEW_LOGGING
473                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
474                                         "ldbm_search: candidate entry %ld scope not okay\n", id ));
475 #else
476                                 Debug( LDAP_DEBUG_TRACE,
477                                         "ldbm_search: candidate entry %ld scope not okay\n",
478                                         id, 0, 0 );
479 #endif
480                         }
481
482                 } else {
483 #ifdef NEW_LOGGING
484                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
485                                 "ldbm_search: candidate entry %ld does not match filter\n", id ));
486 #else
487                         Debug( LDAP_DEBUG_TRACE,
488                                 "ldbm_search: candidate entry %ld does not match filter\n",
489                                 id, 0, 0 );
490 #endif
491                 }
492
493 loop_continue:
494                 if( e != NULL ) {
495                         /* free reader lock */
496                         cache_return_entry_r( &li->li_cache, e );
497                 }
498
499                 ldap_pvt_thread_yield();
500         }
501
502         send_search_result( conn, op,
503                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
504                 NULL, NULL, v2refs, NULL, nentries );
505
506         rc = 0;
507
508 done:
509         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
510
511         if( candidates != NULL )
512                 idl_free( candidates );
513
514         if( v2refs ) ber_bvarray_free( v2refs );
515         if( realbase.bv_val ) free( realbase.bv_val );
516
517         return rc;
518 }
519
520 static ID_BLOCK *
521 base_candidate(
522     Backend     *be,
523         Entry   *e )
524 {
525         ID_BLOCK                *idl;
526
527 #ifdef NEW_LOGGING
528         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
529                    "base_candidate: base (%s)\n", e->e_dn ));
530 #else
531         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
532                 e->e_dn, 0, 0);
533 #endif
534
535
536         idl = idl_alloc( 1 );
537         idl_insert( &idl, e->e_id, 1 );
538
539         return( idl );
540 }
541
542 static ID_BLOCK *
543 search_candidates(
544     Backend     *be,
545     Entry       *e,
546     Filter      *filter,
547     int         scope,
548         int             deref,
549         int             manageDSAit )
550 {
551         ID_BLOCK                *candidates;
552         Filter          f, fand, rf, af, xf;
553     AttributeAssertion aa_ref, aa_alias;
554         struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
555         struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
556
557 #ifdef NEW_LOGGING
558         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
559                    "search_candidates: base (%s) scope %d deref %d\n",
560                    e->e_ndn, scope, deref ));
561 #else
562         Debug(LDAP_DEBUG_TRACE,
563                 "search_candidates: base=\"%s\" s=%d d=%d\n",
564                 e->e_ndn, scope, deref );
565 #endif
566
567
568         xf.f_or = filter;
569         xf.f_choice = LDAP_FILTER_OR;
570         xf.f_next = NULL;
571
572         if( !manageDSAit ) {
573                 /* match referrals */
574                 rf.f_choice = LDAP_FILTER_EQUALITY;
575                 rf.f_ava = &aa_ref;
576                 rf.f_av_desc = slap_schema.si_ad_objectClass;
577                 rf.f_av_value = bv_ref;
578                 rf.f_next = xf.f_or;
579                 xf.f_or = &rf;
580         }
581
582         if( deref & LDAP_DEREF_SEARCHING ) {
583                 /* match aliases */
584                 af.f_choice = LDAP_FILTER_EQUALITY;
585                 af.f_ava = &aa_alias;
586                 af.f_av_desc = slap_schema.si_ad_objectClass;
587                 af.f_av_value = bv_alias;
588                 af.f_next = xf.f_or;
589                 xf.f_or = &af;
590         }
591
592         f.f_next = NULL;
593         f.f_choice = LDAP_FILTER_AND;
594         f.f_and = &fand;
595         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
596                 ? SLAPD_FILTER_DN_SUBTREE
597                 : SLAPD_FILTER_DN_ONE;
598         fand.f_dn = &e->e_nname;
599         fand.f_next = xf.f_or == filter ? filter : &xf ;
600
601         candidates = filter_candidates( be, &f );
602
603         return( candidates );
604 }