]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
Add a default case with assert() just in case.
[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
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, &matched );
89                 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
90                 text = NULL;
91         }
92
93         if ( e == NULL ) {
94                 struct berval matched_dn = { 0, NULL };
95                 BerVarray refs = NULL;
96
97                 if ( matched != NULL ) {
98                         BerVarray erefs;
99                         ber_dupbv( &matched_dn, &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_bvarray_free( erefs );
112                         }
113
114                 } else {
115                         refs = referral_rewrite( default_referral,
116                                 NULL, base, scope );
117                 }
118
119                 send_ldap_result( conn, op, err, matched_dn.bv_val, 
120                         text, refs, NULL );
121
122                 ber_bvarray_free( refs );
123                 ber_memfree( matched_dn.bv_val );
124                 return 1;
125         }
126
127         if (!manageDSAit && is_entry_referral( e ) ) {
128                 /* entry is a referral, don't allow add */
129                 struct berval matched_dn;
130                 BerVarray erefs;
131                 BerVarray refs;
132
133                 ber_dupbv( &matched_dn, &e->e_name );
134                 erefs = get_entry_referrals( be, conn, op, e );
135                 refs = NULL;
136
137                 cache_return_entry_r( &li->li_cache, e );
138
139 #ifdef NEW_LOGGING
140                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
141                         "ldbm_search: entry (%s) is a referral.\n",
142                         e->e_dn ));
143 #else
144                 Debug( LDAP_DEBUG_TRACE,
145                         "ldbm_search: entry is referral\n",
146                         0, 0, 0 );
147 #endif
148
149                 if( erefs ) {
150                         refs = referral_rewrite( erefs, &matched_dn,
151                                 base, scope );
152
153                         ber_bvarray_free( erefs );
154                 }
155
156                 if( refs ) {
157                         send_ldap_result( conn, op, LDAP_REFERRAL,
158                                 matched_dn.bv_val, NULL, refs, NULL );
159                         ber_bvarray_free( refs );
160
161                 } else {
162                         send_ldap_result( conn, op, LDAP_OTHER,
163                                 matched_dn.bv_val,
164                         "bad referral object", NULL, NULL );
165                 }
166
167                 ber_memfree( matched_dn.bv_val );
168                 return 1;
169         }
170
171         if ( is_entry_alias( e ) ) {
172                 /* don't deref */
173                 deref = LDAP_DEREF_NEVER;
174         }
175
176         if ( scope == LDAP_SCOPE_BASE ) {
177                 cscope = LDAP_SCOPE_BASE;
178                 candidates = base_candidate( be, e );
179
180         } else {
181                 cscope = ( scope != LDAP_SCOPE_SUBTREE )
182                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
183                 candidates = search_candidates( be, e, filter,
184                     scope, deref, manageDSAit );
185         }
186
187         /* need normalized dn below */
188         ber_dupbv( &realbase, &e->e_nname );
189
190         cache_return_entry_r( &li->li_cache, e );
191
192 searchit:
193         if ( candidates == NULL ) {
194                 /* no candidates */
195 #ifdef NEW_LOGGING
196                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
197                         "ldbm_search: no candidates\n" ));
198 #else
199                 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
200                         0, 0, 0 );
201 #endif
202
203                 send_search_result( conn, op,
204                         LDAP_SUCCESS,
205                         NULL, NULL, NULL, NULL, 0 );
206
207                 rc = 1;
208                 goto done;
209         }
210
211         /* if not root, get appropriate limits */
212         if ( be_isroot( be, &op->o_ndn ) ) {
213                 isroot = 1;
214         } else {
215                 ( void ) get_limits( be, &op->o_ndn, &limit );
216         }
217
218         /* if candidates exceed to-be-checked entries, abort */
219         if ( !isroot && limit->lms_s_unchecked != -1 ) {
220                 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
221                         send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
222                                         NULL, NULL, NULL, NULL, 0 );
223                         rc = 0;
224                         goto done;
225                 }
226         }
227         
228         /* if root an no specific limit is required, allow unlimited search */
229         if ( isroot ) {
230                 if ( tlimit == 0 ) {
231                         tlimit = -1;
232                 }
233
234                 if ( slimit == 0 ) {
235                         slimit = -1;
236                 }
237
238         } else {
239                 /* if no limit is required, use soft limit */
240                 if ( tlimit <= 0 ) {
241                         tlimit = limit->lms_t_soft;
242                 
243                 /* if requested limit higher than hard limit, abort */
244                 } else if ( tlimit > limit->lms_t_hard ) {
245                         /* no hard limit means use soft instead */
246                         if ( limit->lms_t_hard == 0 ) {
247                                 tlimit = limit->lms_t_soft;
248                         
249                         /* positive hard limit means abort */
250                         } else if ( limit->lms_t_hard > 0 ) {
251                                 send_search_result( conn, op, 
252                                                 LDAP_UNWILLING_TO_PERFORM,
253                                                 NULL, NULL, NULL, NULL, 0 );
254                                 rc = 0; 
255                                 goto done;
256                         }
257
258                         /* negative hard limit means no limit */
259                 }
260
261                 /* if no limit is required, use soft limit */
262                 if ( slimit <= 0 ) {
263                         slimit = limit->lms_s_soft;
264
265                 /* if requested limit higher than hard limit, abort */
266                 } else if ( slimit > limit->lms_s_hard ) {
267                         /* no hard limit means use soft instead */
268                         if ( limit->lms_s_hard == 0 ) {
269                                 slimit = limit->lms_s_soft;
270
271                         /* positive hard limit means abort */
272                         } else if ( limit->lms_s_hard > 0 ) {
273                                 send_search_result( conn, op,
274                                                 LDAP_UNWILLING_TO_PERFORM,
275                                                 NULL, NULL, NULL, NULL, 0 );
276                                 rc = 0;
277                                 goto done;
278                         }
279
280                         /* negative hard limit means no limit */
281                 }
282         }
283
284         /* compute it anyway; root does not use it */
285         stoptime = op->o_time + tlimit;
286
287         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
288             id = idl_nextid( candidates, &cursor ) )
289         {
290                 int scopeok = 0;
291
292                 /* check for abandon */
293                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
294
295                 if ( op->o_abandon ) {
296                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
297                         rc = 0;
298                         goto done;
299                 }
300
301                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
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                                 char *pdn = dn_parent( NULL, e->e_ndn );
347                                 if ( pdn != NULL ) {
348                                         if( strcmp( pdn, realbase.bv_val ) ) {
349                                                 goto loop_continue;
350                                         }
351                                 }
352
353                         } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
354                                 /* alias is within scope */
355 #ifdef NEW_LOGGING
356                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
357                                         "ldbm_search: alias \"%s\" in subtree\n", e->e_dn ));
358 #else
359                                 Debug( LDAP_DEBUG_TRACE,
360                                         "ldbm_search: alias \"%s\" in subtree\n",
361                                         e->e_dn, 0, 0 );
362 #endif
363
364                                 goto loop_continue;
365                         }
366
367                         scopeok = 1;
368                 }
369
370                 /*
371                  * if it's a referral, add it to the list of referrals. only do
372                  * this for non-base searches, and don't check the filter
373                  * explicitly here since it's only a candidate anyway.
374                  */
375                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
376                         is_entry_referral( e ) )
377                 {
378                         char    *dn;
379
380                         /* check scope */
381                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
382                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
383                                         scopeok = (dn == realbase.bv_val)
384                                                 ? 1
385                                                 : (strcmp( dn, realbase.bv_val ) ? 0 : 1 );
386                                 } else {
387                                         scopeok = (realbase.bv_len == 0);
388                                 }
389
390                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
391                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
392
393                         } else {
394                                 scopeok = 1;
395                         }
396
397                         if( scopeok ) {
398                                 BerVarray erefs = get_entry_referrals(
399                                         be, conn, op, e );
400                                 BerVarray refs = referral_rewrite( erefs,
401                                         &e->e_name, NULL,
402                                         scope == LDAP_SCOPE_SUBTREE
403                                                 ? LDAP_SCOPE_SUBTREE
404                                                 : LDAP_SCOPE_BASE );
405
406                                 send_search_reference( be, conn, op,
407                                         e, refs, NULL, &v2refs );
408
409                                 ber_bvarray_free( refs );
410
411                         } else {
412 #ifdef NEW_LOGGING
413                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
414                                         "ldbm_search: candidate referral %ld scope not okay\n",
415                                         id ));
416 #else
417                                 Debug( LDAP_DEBUG_TRACE,
418                                         "ldbm_search: candidate referral %ld scope not okay\n",
419                                         id, 0, 0 );
420 #endif
421                         }
422
423                         goto loop_continue;
424                 }
425
426                 /* if it matches the filter and scope, send it */
427                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
428                         char    *dn;
429
430                         /* check scope */
431                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
432                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
433                                         scopeok = (dn == realbase.bv_val)
434                                                 ? 1
435                                                 : (strcmp( dn, realbase.bv_val ) ? 0 : 1 );
436                                 } else {
437                                         scopeok = (realbase.bv_len == 0);
438                                 }
439
440                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
441                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
442
443                         } else {
444                                 scopeok = 1;
445                         }
446
447                         if ( scopeok ) {
448                                 /* check size limit */
449                                 if ( --slimit == -1 ) {
450                                         cache_return_entry_r( &li->li_cache, e );
451                                         send_search_result( conn, op,
452                                                 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
453                                                 v2refs, NULL, nentries );
454                                         rc = 0;
455                                         goto done;
456                                 }
457
458                                 if (e) {
459                                         int result = send_search_entry(be, conn, op,
460                                                 e, attrs, attrsonly, NULL);
461
462                                         switch (result) {
463                                         case 0:         /* entry sent ok */
464                                                 nentries++;
465                                                 break;
466                                         case 1:         /* entry not sent */
467                                                 break;
468                                         case -1:        /* connection closed */
469                                                 cache_return_entry_r( &li->li_cache, e );
470                                                 rc = 0;
471                                                 goto done;
472                                         }
473                                 }
474                         } else {
475 #ifdef NEW_LOGGING
476                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
477                                         "ldbm_search: candidate entry %ld scope not okay\n", id ));
478 #else
479                                 Debug( LDAP_DEBUG_TRACE,
480                                         "ldbm_search: candidate entry %ld scope not okay\n",
481                                         id, 0, 0 );
482 #endif
483                         }
484
485                 } else {
486 #ifdef NEW_LOGGING
487                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
488                                 "ldbm_search: candidate entry %ld does not match filter\n", id ));
489 #else
490                         Debug( LDAP_DEBUG_TRACE,
491                                 "ldbm_search: candidate entry %ld does not match filter\n",
492                                 id, 0, 0 );
493 #endif
494                 }
495
496 loop_continue:
497                 if( e != NULL ) {
498                         /* free reader lock */
499                         cache_return_entry_r( &li->li_cache, e );
500                 }
501
502                 ldap_pvt_thread_yield();
503         }
504
505         send_search_result( conn, op,
506                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
507                 NULL, NULL, v2refs, NULL, nentries );
508
509         rc = 0;
510
511 done:
512         if( candidates != NULL )
513                 idl_free( candidates );
514
515         if( v2refs ) ber_bvarray_free( v2refs );
516         if( realbase.bv_val ) free( realbase.bv_val );
517
518         return rc;
519 }
520
521 static ID_BLOCK *
522 base_candidate(
523     Backend     *be,
524         Entry   *e )
525 {
526         ID_BLOCK                *idl;
527
528 #ifdef NEW_LOGGING
529         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
530                    "base_candidate: base (%s)\n", e->e_dn ));
531 #else
532         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
533                 e->e_dn, 0, 0);
534 #endif
535
536
537         idl = idl_alloc( 1 );
538         idl_insert( &idl, e->e_id, 1 );
539
540         return( idl );
541 }
542
543 static ID_BLOCK *
544 search_candidates(
545     Backend     *be,
546     Entry       *e,
547     Filter      *filter,
548     int         scope,
549         int             deref,
550         int             manageDSAit )
551 {
552         ID_BLOCK                *candidates;
553         Filter          f, fand, rf, af, xf;
554     AttributeAssertion aa_ref, aa_alias;
555         struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
556         struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
557
558 #ifdef NEW_LOGGING
559         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
560                    "search_candidates: base (%s) scope %d deref %d\n",
561                    e->e_ndn, scope, deref ));
562 #else
563         Debug(LDAP_DEBUG_TRACE,
564                 "search_candidates: base=\"%s\" s=%d d=%d\n",
565                 e->e_ndn, scope, deref );
566 #endif
567
568
569         xf.f_or = filter;
570         xf.f_choice = LDAP_FILTER_OR;
571         xf.f_next = NULL;
572
573         if( !manageDSAit ) {
574                 /* match referrals */
575                 rf.f_choice = LDAP_FILTER_EQUALITY;
576                 rf.f_ava = &aa_ref;
577                 rf.f_av_desc = slap_schema.si_ad_objectClass;
578                 rf.f_av_value = bv_ref;
579                 rf.f_next = xf.f_or;
580                 xf.f_or = &rf;
581         }
582
583         if( deref & LDAP_DEREF_SEARCHING ) {
584                 /* match aliases */
585                 af.f_choice = LDAP_FILTER_EQUALITY;
586                 af.f_ava = &aa_alias;
587                 af.f_av_desc = slap_schema.si_ad_objectClass;
588                 af.f_av_value = bv_alias;
589                 af.f_next = xf.f_or;
590                 xf.f_or = &af;
591         }
592
593         f.f_next = NULL;
594         f.f_choice = LDAP_FILTER_AND;
595         f.f_and = &fand;
596         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
597                 ? SLAPD_FILTER_DN_SUBTREE
598                 : SLAPD_FILTER_DN_ONE;
599         fand.f_dn = &e->e_nname;
600         fand.f_next = xf.f_or == filter ? filter : &xf ;
601
602         candidates = filter_candidates( be, &f );
603
604         return( candidates );
605 }