]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
Add giant lock code back in... (it's my flakey devbox that needed work)
[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                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
299
300                 if ( op->o_abandon ) {
301                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
302                         rc = 0;
303                         goto done;
304                 }
305
306                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
307
308                 /* check time limit */
309                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
310                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
311                                 NULL, NULL, v2refs, NULL, nentries );
312                         rc = 0;
313                         goto done;
314                 }
315
316                 /* get the entry with reader lock */
317                 e = id2entry_r( be, id );
318
319                 if ( e == NULL ) {
320 #ifdef NEW_LOGGING
321                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
322                                 "ldbm_search: candidate %ld not found.\n", id ));
323 #else
324                         Debug( LDAP_DEBUG_TRACE,
325                                 "ldbm_search: candidate %ld not found\n",
326                                 id, 0, 0 );
327 #endif
328
329                         goto loop_continue;
330                 }
331
332                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
333                         Entry *matched;
334                         int err;
335                         const char *text;
336                         
337                         e = deref_entry_r( be, e, &err, &matched, &text );
338
339                         if( e == NULL ) {
340                                 e = matched;
341                                 goto loop_continue;
342                         }
343
344                         if( e->e_id == id ) {
345                                 /* circular loop */
346                                 goto loop_continue;
347                         }
348
349                         /* need to skip alias which deref into scope */
350                         if( scope & LDAP_SCOPE_ONELEVEL ) {
351                                 struct berval pdn;
352                                 dnParent( &e->e_nname, &pdn );
353                                 if ( ber_bvcmp( &pdn, &realbase ) ) {
354                                         goto loop_continue;
355                                 }
356
357                         } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
358                                 /* alias is within scope */
359 #ifdef NEW_LOGGING
360                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
361                                         "ldbm_search: alias \"%s\" in subtree\n", e->e_dn ));
362 #else
363                                 Debug( LDAP_DEBUG_TRACE,
364                                         "ldbm_search: alias \"%s\" in subtree\n",
365                                         e->e_dn, 0, 0 );
366 #endif
367
368                                 goto loop_continue;
369                         }
370
371                         scopeok = 1;
372                 }
373
374                 /*
375                  * if it's a referral, add it to the list of referrals. only do
376                  * this for non-base searches, and don't check the filter
377                  * explicitly here since it's only a candidate anyway.
378                  */
379                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
380                         is_entry_referral( e ) )
381                 {
382                         struct berval   dn;
383
384                         /* check scope */
385                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
386                                 if ( !be_issuffix( be, &e->e_nname ) ) {
387                                         dnParent( &e->e_nname, &dn );
388                                         scopeok = dn_match( &dn, &realbase );
389                                 } else {
390                                         scopeok = (realbase.bv_len == 0);
391                                 }
392
393                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
394                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
395
396                         } else {
397                                 scopeok = 1;
398                         }
399
400                         if( scopeok ) {
401                                 BerVarray erefs = get_entry_referrals(
402                                         be, conn, op, e );
403                                 BerVarray refs = referral_rewrite( erefs,
404                                         &e->e_name, NULL,
405                                         scope == LDAP_SCOPE_SUBTREE
406                                                 ? LDAP_SCOPE_SUBTREE
407                                                 : LDAP_SCOPE_BASE );
408
409                                 send_search_reference( be, conn, op,
410                                         e, refs, NULL, &v2refs );
411
412                                 ber_bvarray_free( refs );
413
414                         } else {
415 #ifdef NEW_LOGGING
416                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
417                                         "ldbm_search: candidate referral %ld scope not okay\n",
418                                         id ));
419 #else
420                                 Debug( LDAP_DEBUG_TRACE,
421                                         "ldbm_search: candidate referral %ld scope not okay\n",
422                                         id, 0, 0 );
423 #endif
424                         }
425
426                         goto loop_continue;
427                 }
428
429                 /* if it matches the filter and scope, send it */
430                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
431                         struct berval   dn;
432
433                         /* check scope */
434                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
435                                 if ( !be_issuffix( be, &e->e_nname ) ) {
436                                         dnParent( &e->e_nname, &dn );
437                                         scopeok = dn_match( &dn, &realbase );
438                                 } else {
439                                         scopeok = (realbase.bv_len == 0);
440                                 }
441
442                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
443                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
444
445                         } else {
446                                 scopeok = 1;
447                         }
448
449                         if ( scopeok ) {
450                                 /* check size limit */
451                                 if ( --slimit == -1 ) {
452                                         cache_return_entry_r( &li->li_cache, e );
453                                         send_search_result( conn, op,
454                                                 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
455                                                 v2refs, NULL, nentries );
456                                         rc = 0;
457                                         goto done;
458                                 }
459
460                                 if (e) {
461                                         int result = send_search_entry(be, conn, op,
462                                                 e, attrs, attrsonly, NULL);
463
464                                         switch (result) {
465                                         case 0:         /* entry sent ok */
466                                                 nentries++;
467                                                 break;
468                                         case 1:         /* entry not sent */
469                                                 break;
470                                         case -1:        /* connection closed */
471                                                 cache_return_entry_r( &li->li_cache, e );
472                                                 rc = 0;
473                                                 goto done;
474                                         }
475                                 }
476                         } else {
477 #ifdef NEW_LOGGING
478                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
479                                         "ldbm_search: candidate entry %ld scope not okay\n", id ));
480 #else
481                                 Debug( LDAP_DEBUG_TRACE,
482                                         "ldbm_search: candidate entry %ld scope not okay\n",
483                                         id, 0, 0 );
484 #endif
485                         }
486
487                 } else {
488 #ifdef NEW_LOGGING
489                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
490                                 "ldbm_search: candidate entry %ld does not match filter\n", id ));
491 #else
492                         Debug( LDAP_DEBUG_TRACE,
493                                 "ldbm_search: candidate entry %ld does not match filter\n",
494                                 id, 0, 0 );
495 #endif
496                 }
497
498 loop_continue:
499                 if( e != NULL ) {
500                         /* free reader lock */
501                         cache_return_entry_r( &li->li_cache, e );
502                 }
503
504                 ldap_pvt_thread_yield();
505         }
506
507         send_search_result( conn, op,
508                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
509                 NULL, NULL, v2refs, NULL, nentries );
510
511         rc = 0;
512
513 done:
514         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
515
516         if( candidates != NULL )
517                 idl_free( candidates );
518
519         if( v2refs ) ber_bvarray_free( v2refs );
520         if( realbase.bv_val ) free( realbase.bv_val );
521
522         return rc;
523 }
524
525 static ID_BLOCK *
526 base_candidate(
527     Backend     *be,
528         Entry   *e )
529 {
530         ID_BLOCK                *idl;
531
532 #ifdef NEW_LOGGING
533         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
534                    "base_candidate: base (%s)\n", e->e_dn ));
535 #else
536         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
537                 e->e_dn, 0, 0);
538 #endif
539
540
541         idl = idl_alloc( 1 );
542         idl_insert( &idl, e->e_id, 1 );
543
544         return( idl );
545 }
546
547 static ID_BLOCK *
548 search_candidates(
549     Backend     *be,
550     Entry       *e,
551     Filter      *filter,
552     int         scope,
553         int             deref,
554         int             manageDSAit )
555 {
556         ID_BLOCK                *candidates;
557         Filter          f, fand, rf, af, xf;
558     AttributeAssertion aa_ref, aa_alias;
559         struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
560         struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
561
562 #ifdef NEW_LOGGING
563         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
564                    "search_candidates: base (%s) scope %d deref %d\n",
565                    e->e_ndn, scope, deref ));
566 #else
567         Debug(LDAP_DEBUG_TRACE,
568                 "search_candidates: base=\"%s\" s=%d d=%d\n",
569                 e->e_ndn, scope, deref );
570 #endif
571
572
573         xf.f_or = filter;
574         xf.f_choice = LDAP_FILTER_OR;
575         xf.f_next = NULL;
576
577         if( !manageDSAit ) {
578                 /* match referrals */
579                 rf.f_choice = LDAP_FILTER_EQUALITY;
580                 rf.f_ava = &aa_ref;
581                 rf.f_av_desc = slap_schema.si_ad_objectClass;
582                 rf.f_av_value = bv_ref;
583                 rf.f_next = xf.f_or;
584                 xf.f_or = &rf;
585         }
586
587         if( deref & LDAP_DEREF_SEARCHING ) {
588                 /* match aliases */
589                 af.f_choice = LDAP_FILTER_EQUALITY;
590                 af.f_ava = &aa_alias;
591                 af.f_av_desc = slap_schema.si_ad_objectClass;
592                 af.f_av_value = bv_alias;
593                 af.f_next = xf.f_or;
594                 xf.f_or = &af;
595         }
596
597         f.f_next = NULL;
598         f.f_choice = LDAP_FILTER_AND;
599         f.f_and = &fand;
600         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
601                 ? SLAPD_FILTER_DN_SUBTREE
602                 : SLAPD_FILTER_DN_ONE;
603         fand.f_dn = &e->e_nname;
604         fand.f_next = xf.f_or == filter ? filter : &xf ;
605
606         candidates = filter_candidates( be, &f );
607
608         return( candidates );
609 }