]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
Add IDL debugging code from SuSE.
[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     const char  *base,
33     const char  *nbase,
34     int         scope,
35     int         deref,
36     int         slimit,
37     int         tlimit,
38     Filter      *filter,
39     const char  *filterstr,
40     char        **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         char    *realbase = NULL;
53         int             nentries = 0;
54         int             manageDSAit = get_manageDSAit( op );
55
56         struct slap_limits_set *limit = NULL;
57         int isroot = 0;
58                 
59 #ifdef NEW_LOGGING
60         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
61                    "ldbm_back_search: enter\n" ));
62 #else
63         Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
64 #endif
65
66
67         if ( *nbase == '\0' ) {
68                 /* DIT root special case */
69                 static const Entry root = { NOID, "", "", NULL, NULL };
70                 e = (Entry *) &root;
71
72                 /* need normalized dn below */
73                 realbase = ch_strdup( e->e_ndn );
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                 char *matched_dn = NULL;
95                 struct berval **refs = NULL;
96
97                 if ( matched != NULL ) {
98                         matched_dn = ch_strdup( matched->e_dn );
99
100                         refs = is_entry_referral( matched )
101                                 ? get_entry_referrals( be, conn, op, matched )
102                                 : NULL;
103
104                         cache_return_entry_r( &li->li_cache, matched );
105
106                 } else {
107                         refs = default_referral;
108                 }
109
110                 send_ldap_result( conn, op, err,
111                         matched_dn, text, refs, NULL );
112
113                 if( matched != NULL ) {
114                         ber_bvecfree( refs );
115                         free( matched_dn );
116                 }
117
118                 return 1;
119         }
120
121         if (!manageDSAit && is_entry_referral( e ) ) {
122                 /* entry is a referral, don't allow add */
123                 char *matched_dn = ch_strdup( e->e_dn );
124                 struct berval **refs = get_entry_referrals( be,
125                         conn, op, e );
126
127                 cache_return_entry_r( &li->li_cache, e );
128
129 #ifdef NEW_LOGGING
130                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
131                            "ldbm_search: entry (%s) is a referral.\n",
132                            e->e_dn ));
133 #else
134                 Debug( LDAP_DEBUG_TRACE,
135                         "ldbm_search: entry is referral\n",
136                         0, 0, 0 );
137 #endif
138
139
140                 send_ldap_result( conn, op, LDAP_REFERRAL,
141                     matched_dn, NULL, refs, NULL );
142
143                 ber_bvecfree( refs );
144                 free( matched_dn );
145
146                 return 1;
147         }
148
149         if ( is_entry_alias( e ) ) {
150                 /* don't deref */
151                 deref = LDAP_DEREF_NEVER;
152         }
153
154         if ( scope == LDAP_SCOPE_BASE ) {
155                 candidates = base_candidate( be, e );
156
157         } else {
158                 candidates = search_candidates( be, e, filter,
159                     scope, deref, manageDSAit );
160         }
161
162         /* need normalized dn below */
163         realbase = ch_strdup( e->e_ndn );
164
165         cache_return_entry_r( &li->li_cache, e );
166
167 searchit:
168         if ( candidates == NULL ) {
169                 /* no candidates */
170 #ifdef NEW_LOGGING
171                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
172                            "ldbm_search: no candidates\n" ));
173 #else
174                 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
175                         0, 0, 0 );
176 #endif
177
178
179                 send_search_result( conn, op,
180                         LDAP_SUCCESS,
181                         NULL, NULL, NULL, NULL, 0 );
182
183                 rc = 1;
184                 goto done;
185         }
186
187         /* if not root, get appropriate limits */
188         if ( be_isroot( be, op->o_ndn ) ) {
189                 isroot = 1;
190         } else {
191                 ( void ) get_limits( be, op->o_ndn, &limit );
192         }
193
194         /* if candidates exceed to-be-checked entries, abort */
195         if ( !isroot && limit->lms_s_unchecked != -1 ) {
196                 if ( ID_BLOCK_NIDS( candidates ) > limit->lms_s_unchecked ) {
197                         send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
198                                         NULL, NULL, NULL, NULL, 0 );
199                         rc = 0;
200                         goto done;
201                 }
202         }
203         
204         /* if no time limit requested, use soft limit (unless root!) */
205         if ( tlimit <= 0 ) {
206                 if ( isroot ) {
207                         tlimit = -1;    /* allow root to set no limit */
208                 } else {
209                         tlimit = limit->lms_t_soft;
210                 }
211                 
212         /* if requested limit higher than hard limit, abort */
213         } else if ( tlimit > limit->lms_t_hard ) {
214                 /* no hard limit means use soft instead */
215                 if ( limit->lms_t_hard == 0 ) {
216                         tlimit = limit->lms_t_soft;
217                         
218                 /* positive hard limit means abort */
219                 } else if ( limit->lms_t_hard > 0 ) {
220                         send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
221                                         NULL, NULL, NULL, NULL, 0 );
222                         rc = 0; 
223                         goto done;
224                 }
225
226                 /* negative hard limit means no limit */
227         }
228
229         /* compute it anyway; root does not use it */
230         stoptime = op->o_time + tlimit;
231
232         /* if no size limit requested, use soft limit (unless root!) */
233         if ( slimit == 0 ) {
234                 if ( isroot ) {
235                         slimit = -1;    /* allow root to set no limit */
236                 } else {
237                         slimit = limit->lms_s_soft;
238                 }
239         
240         /* if requested limit higher than hard limit, abort */
241         } else if ( slimit > limit->lms_s_hard ) {
242                 /* no hard limit means use soft instead */
243                 if ( limit->lms_s_hard == 0 ) {
244                         slimit = limit->lms_s_soft;
245
246                 /* positive hard limit means abort */
247                 } else if ( limit->lms_s_hard > 0 ) {
248                         send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
249                                         NULL, NULL, NULL, NULL, 0 );
250                         rc = 0;
251                         goto done;
252                 }
253
254                 /* negative hard limit means no limit */
255         }
256
257         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
258             id = idl_nextid( candidates, &cursor ) )
259         {
260                 int             scopeok = 0;
261
262                 /* check for abandon */
263                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
264
265                 if ( op->o_abandon ) {
266                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
267                         rc = 0;
268                         goto done;
269                 }
270
271                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
272
273                 /* check time limit */
274                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
275                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
276                                 NULL, NULL, v2refs, NULL, nentries );
277                         rc = 0;
278                         goto done;
279                 }
280
281                 /* get the entry with reader lock */
282                 e = id2entry_r( be, id );
283
284                 if ( e == NULL ) {
285 #ifdef NEW_LOGGING
286                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
287                                    "ldbm_search: candidate %ld not found.\n", id ));
288 #else
289                         Debug( LDAP_DEBUG_TRACE,
290                                 "ldbm_search: candidate %ld not found\n",
291                                 id, 0, 0 );
292 #endif
293
294
295                         goto loop_continue;
296                 }
297
298                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
299                         Entry *matched;
300                         int err;
301                         const char *text;
302                         
303                         e = deref_entry_r( be, e, &err, &matched, &text );
304
305                         if( e == NULL ) {
306                                 e = matched;
307                                 goto loop_continue;
308                         }
309
310                         if( e->e_id == id ) {
311                                 /* circular loop */
312                                 goto loop_continue;
313                         }
314
315                         /* need to skip alias which deref into scope */
316                         if( scope & LDAP_SCOPE_ONELEVEL ) {
317                                 char *pdn = dn_parent( NULL, e->e_ndn );
318                                 if ( pdn != NULL ) {
319                                         if( strcmp( pdn, realbase ) ) {
320                                                 free( pdn );
321                                                 goto loop_continue;
322                                         }
323                                         free(pdn);
324                                 }
325
326                         } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
327                                 /* alias is within scope */
328 #ifdef NEW_LOGGING
329                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
330                                            "ldbm_search: \"%s\" in subtree\n", e->e_dn ));
331 #else
332                                 Debug( LDAP_DEBUG_TRACE,
333                                         "ldbm_search: \"%s\" in subtree\n",
334                                         e->e_dn, 0, 0 );
335 #endif
336
337                                 goto loop_continue;
338                         }
339
340                         scopeok = 1;
341                 }
342
343                 /*
344                  * if it's a referral, add it to the list of referrals. only do
345                  * this for non-base searches, and don't check the filter
346                  * explicitly here since it's only a candidate anyway.
347                  */
348                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
349                         is_entry_referral( e ) )
350                 {
351                         struct berval **refs = get_entry_referrals(
352                                 be, conn, op, e );
353
354                         send_search_reference( be, conn, op,
355                                 e, refs, scope, NULL, &v2refs );
356
357                         ber_bvecfree( refs );
358
359                         goto loop_continue;
360                 }
361
362                 /* if it matches the filter and scope, send it */
363                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
364                         char    *dn;
365
366                         /* check scope */
367                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
368                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
369                                         (void) dn_normalize( dn );
370                                         scopeok = (dn == realbase)
371                                                 ? 1
372                                                 : (strcmp( dn, realbase ) ? 0 : 1 );
373                                         free( dn );
374
375                                 } else {
376                                         scopeok = (realbase == NULL || *realbase == '\0');
377                                 }
378
379                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
380                                 dn = ch_strdup( e->e_ndn );
381                                 scopeok = dn_issuffix( dn, realbase );
382                                 free( dn );
383
384                         } else {
385                                 scopeok = 1;
386                         }
387
388                         if ( scopeok ) {
389                                 /* check size limit */
390                                 if ( --slimit == -1 ) {
391                                         cache_return_entry_r( &li->li_cache, e );
392                                         send_search_result( conn, op,
393                                                 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
394                                                 v2refs, NULL, nentries );
395                                         rc = 0;
396                                         goto done;
397                                 }
398
399                                 if (e) {
400                                         int result = send_search_entry(be, conn, op,
401                                                 e, attrs, attrsonly, NULL);
402
403                                         switch (result) {
404                                         case 0:         /* entry sent ok */
405                                                 nentries++;
406                                                 break;
407                                         case 1:         /* entry not sent */
408                                                 break;
409                                         case -1:        /* connection closed */
410                                                 cache_return_entry_r( &li->li_cache, e );
411                                                 rc = 0;
412                                                 goto done;
413                                         }
414                                 }
415                         } else {
416 #ifdef NEW_LOGGING
417                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
418                                            "ldbm_search: candidate %ld scope not okay\n", id ));
419 #else
420                                 Debug( LDAP_DEBUG_TRACE,
421                                         "ldbm_search: candidate %ld scope not okay\n",
422                                         id, 0, 0 );
423 #endif
424
425                         }
426                 } else {
427 #ifdef NEW_LOGGING
428                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
429                                    "ldbm_search: candidate %ld does not match filter\n", id ));
430 #else
431                         Debug( LDAP_DEBUG_TRACE,
432                                 "ldbm_search: candidate %ld does not match filter\n",
433                                 id, 0, 0 );
434 #endif
435
436                 }
437
438 loop_continue:
439                 if( e != NULL ) {
440                         /* free reader lock */
441                         cache_return_entry_r( &li->li_cache, e );
442                 }
443
444                 ldap_pvt_thread_yield();
445         }
446         send_search_result( conn, op,
447                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
448                 NULL, NULL, v2refs, NULL, nentries );
449
450         rc = 0;
451
452 done:
453         if( candidates != NULL )
454                 idl_free( candidates );
455
456         ber_bvecfree( v2refs );
457         if( realbase ) free( realbase );
458
459         return rc;
460 }
461
462 static ID_BLOCK *
463 base_candidate(
464     Backend     *be,
465         Entry   *e )
466 {
467         ID_BLOCK                *idl;
468
469 #ifdef NEW_LOGGING
470         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
471                    "base_candidate: base (%s)\n", e->e_dn ));
472 #else
473         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
474                 e->e_dn, 0, 0);
475 #endif
476
477
478         idl = idl_alloc( 1 );
479         idl_insert( &idl, e->e_id, 1 );
480
481         return( idl );
482 }
483
484 static ID_BLOCK *
485 search_candidates(
486     Backend     *be,
487     Entry       *e,
488     Filter      *filter,
489     int         scope,
490         int             deref,
491         int             manageDSAit )
492 {
493         ID_BLOCK                *candidates;
494         Filter          f, fand, rf, af, xf;
495     AttributeAssertion aa_ref, aa_alias;
496         static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
497         static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
498
499 #ifdef NEW_LOGGING
500         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
501                    "search_candidates: base (%s) scope %d deref %d\n",
502                    e->e_ndn, scope, deref ));
503 #else
504         Debug(LDAP_DEBUG_TRACE,
505                 "search_candidates: base=\"%s\" s=%d d=%d\n",
506                 e->e_ndn, scope, deref );
507 #endif
508
509
510         xf.f_or = filter;
511         xf.f_choice = LDAP_FILTER_OR;
512         xf.f_next = NULL;
513
514         if( !manageDSAit ) {
515                 /* match referrals */
516                 rf.f_choice = LDAP_FILTER_EQUALITY;
517                 rf.f_ava = &aa_ref;
518                 rf.f_av_desc = slap_schema.si_ad_objectClass;
519                 rf.f_av_value = &bv_ref;
520                 rf.f_next = xf.f_or;
521                 xf.f_or = &rf;
522         }
523
524         if( deref & LDAP_DEREF_SEARCHING ) {
525                 /* match aliases */
526                 af.f_choice = LDAP_FILTER_EQUALITY;
527                 af.f_ava = &aa_alias;
528                 af.f_av_desc = slap_schema.si_ad_objectClass;
529                 af.f_av_value = &bv_alias;
530                 af.f_next = xf.f_or;
531                 xf.f_or = &af;
532         }
533
534         f.f_next = NULL;
535         f.f_choice = LDAP_FILTER_AND;
536         f.f_and = &fand;
537         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
538                 ? SLAPD_FILTER_DN_SUBTREE
539                 : SLAPD_FILTER_DN_ONE;
540         fand.f_dn = e->e_ndn;
541         fand.f_next = xf.f_or == filter ? filter : &xf ;
542
543         candidates = filter_candidates( be, &f );
544
545         return( candidates );
546 }