]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
Remove abandon cruft
[openldap] / servers / slapd / back-ldbm / search.c
1 /* search.c - ldbm backend search function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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     const char  *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         struct berval **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
68         if ( nbase->bv_len == 0 ) {
69                 /* DIT root special case */
70                 e = (Entry *) &slap_entry_root;
71
72                 /* need normalized dn below */
73                 ber_dupbv( &realbase, &e->e_nname );
74
75                 candidates = search_candidates( be, e, filter,
76                     scope, deref, manageDSAit );
77
78                 goto searchit;
79                 
80         } else if ( deref & LDAP_DEREF_FINDING ) {
81                 /* deref dn and get entry with reader lock */
82                 e = deref_dn_r( be, nbase, &err, &matched, &text );
83
84                 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
85
86         } else {
87                 /* get entry with reader lock */
88                 e = dn2entry_r( be, nbase->bv_val, &matched );
89                 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
90                 text = NULL;
91         }
92
93         if ( e == NULL ) {
94                 struct berval *matched_dn = NULL;
95                 struct berval **refs = NULL;
96
97                 if ( matched != NULL ) {
98                         struct berval **erefs;
99                         matched_dn = ber_bvdup( &matched->e_name );
100
101                         erefs = is_entry_referral( matched )
102                                 ? get_entry_referrals( be, conn, op, matched )
103                                 : NULL;
104
105                         cache_return_entry_r( &li->li_cache, matched );
106
107                         if( erefs ) {
108                                 refs = referral_rewrite( erefs, matched_dn,
109                                         base, scope );
110
111                                 ber_bvecfree( erefs );
112                         }
113
114                 } else {
115                         refs = referral_rewrite( default_referral,
116                                 NULL, base, scope );
117                 }
118
119                 send_ldap_result( conn, op, err,
120                         matched_dn ? matched_dn->bv_val : NULL, 
121                         text, refs, NULL );
122
123                 ber_bvecfree( refs );
124                 ber_bvfree( matched_dn );
125                 return 1;
126         }
127
128         if (!manageDSAit && is_entry_referral( e ) ) {
129                 /* entry is a referral, don't allow add */
130                 struct berval *matched_dn = ber_bvdup( &e->e_name );
131                 struct berval **erefs = get_entry_referrals( be,
132                         conn, op, e );
133                 struct berval **refs = NULL;
134
135                 cache_return_entry_r( &li->li_cache, e );
136
137 #ifdef NEW_LOGGING
138                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
139                         "ldbm_search: entry (%s) is a referral.\n",
140                         e->e_dn ));
141 #else
142                 Debug( LDAP_DEBUG_TRACE,
143                         "ldbm_search: entry is referral\n",
144                         0, 0, 0 );
145 #endif
146
147                 if( erefs ) {
148                         refs = referral_rewrite( erefs, matched_dn,
149                                 base, scope );
150
151                         ber_bvecfree( erefs );
152                 }
153
154                 if( refs ) {
155                         send_ldap_result( conn, op, LDAP_REFERRAL,
156                                 matched_dn->bv_val, NULL, refs, NULL );
157                         ber_bvecfree( refs );
158
159                 } else {
160                         send_ldap_result( conn, op, LDAP_OTHER,
161                                 matched_dn->bv_val,
162                         "bad referral object", NULL, NULL );
163                 }
164
165                 ber_bvfree( matched_dn );
166                 return 1;
167         }
168
169         if ( is_entry_alias( e ) ) {
170                 /* don't deref */
171                 deref = LDAP_DEREF_NEVER;
172         }
173
174         if ( scope == LDAP_SCOPE_BASE ) {
175                 cscope = LDAP_SCOPE_BASE;
176                 candidates = base_candidate( be, e );
177
178         } else {
179                 cscope = ( scope != LDAP_SCOPE_SUBTREE )
180                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
181                 candidates = search_candidates( be, e, filter,
182                     scope, deref, manageDSAit );
183         }
184
185         /* need normalized dn below */
186         ber_dupbv( &realbase, &e->e_nname );
187
188         cache_return_entry_r( &li->li_cache, e );
189
190 searchit:
191         if ( candidates == NULL ) {
192                 /* no candidates */
193 #ifdef NEW_LOGGING
194                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
195                         "ldbm_search: no candidates\n" ));
196 #else
197                 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
198                         0, 0, 0 );
199 #endif
200
201                 send_search_result( conn, op,
202                         LDAP_SUCCESS,
203                         NULL, NULL, NULL, NULL, 0 );
204
205                 rc = 1;
206                 goto done;
207         }
208
209         /* if not root, get appropriate limits */
210         if ( be_isroot( be, &op->o_ndn ) ) {
211                 isroot = 1;
212         } else {
213                 ( void ) get_limits( be, &op->o_ndn, &limit );
214         }
215
216         /* if candidates exceed to-be-checked entries, abort */
217         if ( !isroot && limit->lms_s_unchecked != -1 ) {
218                 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
219                         send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
220                                         NULL, NULL, NULL, NULL, 0 );
221                         rc = 0;
222                         goto done;
223                 }
224         }
225         
226         /* if root an no specific limit is required, allow unlimited search */
227         if ( isroot ) {
228                 if ( tlimit == 0 ) {
229                         tlimit = -1;
230                 }
231
232                 if ( slimit == 0 ) {
233                         slimit = -1;
234                 }
235
236         } else {
237                 /* if no limit is required, use soft limit */
238                 if ( tlimit <= 0 ) {
239                         tlimit = limit->lms_t_soft;
240                 
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 ) {
245                                 tlimit = limit->lms_t_soft;
246                         
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 );
252                                 rc = 0; 
253                                 goto done;
254                         }
255
256                         /* negative hard limit means no limit */
257                 }
258
259                 /* if no limit is required, use soft limit */
260                 if ( slimit <= 0 ) {
261                         slimit = limit->lms_s_soft;
262
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 ) {
267                                 slimit = limit->lms_s_soft;
268
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 );
274                                 rc = 0;
275                                 goto done;
276                         }
277
278                         /* negative hard limit means no limit */
279                 }
280         }
281
282         /* compute it anyway; root does not use it */
283         stoptime = op->o_time + tlimit;
284
285         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
286             id = idl_nextid( candidates, &cursor ) )
287         {
288                 int scopeok = 0;
289
290                 /* check for abandon */
291                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
292
293                 if ( op->o_abandon ) {
294                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
295                         rc = 0;
296                         goto done;
297                 }
298
299                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
300
301                 /* check time limit */
302                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
303                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
304                                 NULL, NULL, v2refs, NULL, nentries );
305                         rc = 0;
306                         goto done;
307                 }
308
309                 /* get the entry with reader lock */
310                 e = id2entry_r( be, id );
311
312                 if ( e == NULL ) {
313 #ifdef NEW_LOGGING
314                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
315                                 "ldbm_search: candidate %ld not found.\n", id ));
316 #else
317                         Debug( LDAP_DEBUG_TRACE,
318                                 "ldbm_search: candidate %ld not found\n",
319                                 id, 0, 0 );
320 #endif
321
322                         goto loop_continue;
323                 }
324
325                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
326                         Entry *matched;
327                         int err;
328                         const char *text;
329                         
330                         e = deref_entry_r( be, e, &err, &matched, &text );
331
332                         if( e == NULL ) {
333                                 e = matched;
334                                 goto loop_continue;
335                         }
336
337                         if( e->e_id == id ) {
338                                 /* circular loop */
339                                 goto loop_continue;
340                         }
341
342                         /* need to skip alias which deref into scope */
343                         if( scope & LDAP_SCOPE_ONELEVEL ) {
344                                 char *pdn = dn_parent( NULL, e->e_ndn );
345                                 if ( pdn != NULL ) {
346                                         if( strcmp( pdn, realbase.bv_val ) ) {
347                                                 goto loop_continue;
348                                         }
349                                 }
350
351                         } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
352                                 /* alias is within scope */
353 #ifdef NEW_LOGGING
354                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
355                                         "ldbm_search: alias \"%s\" in subtree\n", e->e_dn ));
356 #else
357                                 Debug( LDAP_DEBUG_TRACE,
358                                         "ldbm_search: alias \"%s\" in subtree\n",
359                                         e->e_dn, 0, 0 );
360 #endif
361
362                                 goto loop_continue;
363                         }
364
365                         scopeok = 1;
366                 }
367
368                 /*
369                  * if it's a referral, add it to the list of referrals. only do
370                  * this for non-base searches, and don't check the filter
371                  * explicitly here since it's only a candidate anyway.
372                  */
373                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
374                         is_entry_referral( e ) )
375                 {
376                         char    *dn;
377
378                         /* check scope */
379                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
380                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
381                                         scopeok = (dn == realbase.bv_val)
382                                                 ? 1
383                                                 : (strcmp( dn, realbase.bv_val ) ? 0 : 1 );
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                                 struct berval **erefs = get_entry_referrals(
397                                         be, conn, op, e );
398                                 struct berval **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_bvecfree( 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                         char    *dn;
427
428                         /* check scope */
429                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
430                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
431                                         scopeok = (dn == realbase.bv_val)
432                                                 ? 1
433                                                 : (strcmp( dn, realbase.bv_val ) ? 0 : 1 );
434                                 } else {
435                                         scopeok = (realbase.bv_len == 0);
436                                 }
437
438                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
439                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
440
441                         } else {
442                                 scopeok = 1;
443                         }
444
445                         if ( scopeok ) {
446                                 /* check size limit */
447                                 if ( --slimit == -1 ) {
448                                         cache_return_entry_r( &li->li_cache, e );
449                                         send_search_result( conn, op,
450                                                 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
451                                                 v2refs, NULL, nentries );
452                                         rc = 0;
453                                         goto done;
454                                 }
455
456                                 if (e) {
457                                         int result = send_search_entry(be, conn, op,
458                                                 e, attrs, attrsonly, NULL);
459
460                                         switch (result) {
461                                         case 0:         /* entry sent ok */
462                                                 nentries++;
463                                                 break;
464                                         case 1:         /* entry not sent */
465                                                 break;
466                                         case -1:        /* connection closed */
467                                                 cache_return_entry_r( &li->li_cache, e );
468                                                 rc = 0;
469                                                 goto done;
470                                         }
471                                 }
472                         } else {
473 #ifdef NEW_LOGGING
474                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
475                                         "ldbm_search: candidate entry %ld scope not okay\n", id ));
476 #else
477                                 Debug( LDAP_DEBUG_TRACE,
478                                         "ldbm_search: candidate entry %ld scope not okay\n",
479                                         id, 0, 0 );
480 #endif
481                         }
482
483                 } else {
484 #ifdef NEW_LOGGING
485                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
486                                 "ldbm_search: candidate entry %ld does not match filter\n", id ));
487 #else
488                         Debug( LDAP_DEBUG_TRACE,
489                                 "ldbm_search: candidate entry %ld does not match filter\n",
490                                 id, 0, 0 );
491 #endif
492                 }
493
494 loop_continue:
495                 if( e != NULL ) {
496                         /* free reader lock */
497                         cache_return_entry_r( &li->li_cache, e );
498                 }
499
500                 ldap_pvt_thread_yield();
501         }
502
503         send_search_result( conn, op,
504                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
505                 NULL, NULL, v2refs, NULL, nentries );
506
507         rc = 0;
508
509 done:
510         if( candidates != NULL )
511                 idl_free( candidates );
512
513         if( v2refs ) ber_bvecfree( v2refs );
514         if( realbase.bv_val ) free( realbase.bv_val );
515
516         return rc;
517 }
518
519 static ID_BLOCK *
520 base_candidate(
521     Backend     *be,
522         Entry   *e )
523 {
524         ID_BLOCK                *idl;
525
526 #ifdef NEW_LOGGING
527         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
528                    "base_candidate: base (%s)\n", e->e_dn ));
529 #else
530         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
531                 e->e_dn, 0, 0);
532 #endif
533
534
535         idl = idl_alloc( 1 );
536         idl_insert( &idl, e->e_id, 1 );
537
538         return( idl );
539 }
540
541 static ID_BLOCK *
542 search_candidates(
543     Backend     *be,
544     Entry       *e,
545     Filter      *filter,
546     int         scope,
547         int             deref,
548         int             manageDSAit )
549 {
550         ID_BLOCK                *candidates;
551         Filter          f, fand, rf, af, xf;
552     AttributeAssertion aa_ref, aa_alias;
553         struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
554         struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
555
556 #ifdef NEW_LOGGING
557         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
558                    "search_candidates: base (%s) scope %d deref %d\n",
559                    e->e_ndn, scope, deref ));
560 #else
561         Debug(LDAP_DEBUG_TRACE,
562                 "search_candidates: base=\"%s\" s=%d d=%d\n",
563                 e->e_ndn, scope, deref );
564 #endif
565
566
567         xf.f_or = filter;
568         xf.f_choice = LDAP_FILTER_OR;
569         xf.f_next = NULL;
570
571         if( !manageDSAit ) {
572                 /* match referrals */
573                 rf.f_choice = LDAP_FILTER_EQUALITY;
574                 rf.f_ava = &aa_ref;
575                 rf.f_av_desc = slap_schema.si_ad_objectClass;
576                 rf.f_av_value = bv_ref;
577                 rf.f_next = xf.f_or;
578                 xf.f_or = &rf;
579         }
580
581         if( deref & LDAP_DEREF_SEARCHING ) {
582                 /* match aliases */
583                 af.f_choice = LDAP_FILTER_EQUALITY;
584                 af.f_ava = &aa_alias;
585                 af.f_av_desc = slap_schema.si_ad_objectClass;
586                 af.f_av_value = bv_alias;
587                 af.f_next = xf.f_or;
588                 xf.f_or = &af;
589         }
590
591         f.f_next = NULL;
592         f.f_choice = LDAP_FILTER_AND;
593         f.f_and = &fand;
594         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
595                 ? SLAPD_FILTER_DN_SUBTREE
596                 : SLAPD_FILTER_DN_ONE;
597         fand.f_dn = &e->e_nname;
598         fand.f_next = xf.f_or == filter ? filter : &xf ;
599
600         candidates = filter_candidates( be, &f );
601
602         return( candidates );
603 }