]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
More struct berval DN changes
[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     struct berval       **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         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                 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->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->bv_val, text, refs, NULL );
121
122                 ber_bvecfree( refs );
123                 ber_bvfree( matched_dn );
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 = ber_bvdup( &e->e_name );
130                 struct berval **erefs = get_entry_referrals( be,
131                         conn, op, e );
132                 struct berval **refs = NULL;
133
134                 cache_return_entry_r( &li->li_cache, e );
135
136 #ifdef NEW_LOGGING
137                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
138                         "ldbm_search: entry (%s) is a referral.\n",
139                         e->e_dn ));
140 #else
141                 Debug( LDAP_DEBUG_TRACE,
142                         "ldbm_search: entry is referral\n",
143                         0, 0, 0 );
144 #endif
145
146                 if( erefs ) {
147                         refs = referral_rewrite( erefs, matched_dn,
148                                 base, scope );
149
150                         ber_bvecfree( erefs );
151                 }
152
153                 if( refs ) {
154                         send_ldap_result( conn, op, LDAP_REFERRAL,
155                                 matched_dn->bv_val, NULL, refs, NULL );
156                         ber_bvecfree( refs );
157
158                 } else {
159                         send_ldap_result( conn, op, LDAP_OTHER,
160                                 matched_dn->bv_val,
161                         "bad referral object", NULL, NULL );
162                 }
163
164                 ber_bvfree( matched_dn );
165                 return 1;
166         }
167
168         if ( is_entry_alias( e ) ) {
169                 /* don't deref */
170                 deref = LDAP_DEREF_NEVER;
171         }
172
173         if ( scope == LDAP_SCOPE_BASE ) {
174                 cscope = LDAP_SCOPE_BASE;
175                 candidates = base_candidate( be, e );
176
177         } else {
178                 cscope = ( scope != LDAP_SCOPE_SUBTREE )
179                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
180                 candidates = search_candidates( be, e, filter,
181                     scope, deref, manageDSAit );
182         }
183
184         /* need normalized dn below */
185         realbase = ch_strdup( e->e_ndn );
186
187         cache_return_entry_r( &li->li_cache, e );
188
189 searchit:
190         if ( candidates == NULL ) {
191                 /* no candidates */
192 #ifdef NEW_LOGGING
193                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
194                         "ldbm_search: no candidates\n" ));
195 #else
196                 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
197                         0, 0, 0 );
198 #endif
199
200                 send_search_result( conn, op,
201                         LDAP_SUCCESS,
202                         NULL, NULL, NULL, NULL, 0 );
203
204                 rc = 1;
205                 goto done;
206         }
207
208         /* if not root, get appropriate limits */
209         if ( be_isroot( be, &op->o_ndn ) ) {
210                 isroot = 1;
211         } else {
212                 ( void ) get_limits( be, &op->o_ndn, &limit );
213         }
214
215         /* if candidates exceed to-be-checked entries, abort */
216         if ( !isroot && limit->lms_s_unchecked != -1 ) {
217                 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
218                         send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
219                                         NULL, NULL, NULL, NULL, 0 );
220                         rc = 0;
221                         goto done;
222                 }
223         }
224         
225         /* if root an no specific limit is required, allow unlimited search */
226         if ( isroot ) {
227                 if ( tlimit == 0 ) {
228                         tlimit = -1;
229                 }
230
231                 if ( slimit == 0 ) {
232                         slimit = -1;
233                 }
234
235         } else {
236                 /* if no limit is required, use soft limit */
237                 if ( tlimit <= 0 ) {
238                         tlimit = limit->lms_t_soft;
239                 
240                 /* if requested limit higher than hard limit, abort */
241                 } else if ( tlimit > limit->lms_t_hard ) {
242                         /* no hard limit means use soft instead */
243                         if ( limit->lms_t_hard == 0 ) {
244                                 tlimit = limit->lms_t_soft;
245                         
246                         /* positive hard limit means abort */
247                         } else if ( limit->lms_t_hard > 0 ) {
248                                 send_search_result( conn, op, 
249                                                 LDAP_UNWILLING_TO_PERFORM,
250                                                 NULL, NULL, NULL, NULL, 0 );
251                                 rc = 0; 
252                                 goto done;
253                         }
254
255                         /* negative hard limit means no limit */
256                 }
257
258                 /* if no limit is required, use soft limit */
259                 if ( slimit <= 0 ) {
260                         slimit = limit->lms_s_soft;
261
262                 /* if requested limit higher than hard limit, abort */
263                 } else if ( slimit > limit->lms_s_hard ) {
264                         /* no hard limit means use soft instead */
265                         if ( limit->lms_s_hard == 0 ) {
266                                 slimit = limit->lms_s_soft;
267
268                         /* positive hard limit means abort */
269                         } else if ( limit->lms_s_hard > 0 ) {
270                                 send_search_result( conn, op,
271                                                 LDAP_UNWILLING_TO_PERFORM,
272                                                 NULL, NULL, NULL, NULL, 0 );
273                                 rc = 0;
274                                 goto done;
275                         }
276
277                         /* negative hard limit means no limit */
278                 }
279         }
280
281         /* compute it anyway; root does not use it */
282         stoptime = op->o_time + tlimit;
283
284         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
285             id = idl_nextid( candidates, &cursor ) )
286         {
287                 int scopeok = 0;
288
289                 /* check for abandon */
290                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
291
292                 if ( op->o_abandon ) {
293                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
294                         rc = 0;
295                         goto done;
296                 }
297
298                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
299
300                 /* check time limit */
301                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
302                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
303                                 NULL, NULL, v2refs, NULL, nentries );
304                         rc = 0;
305                         goto done;
306                 }
307
308                 /* get the entry with reader lock */
309                 e = id2entry_r( be, id );
310
311                 if ( e == NULL ) {
312 #ifdef NEW_LOGGING
313                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
314                                 "ldbm_search: candidate %ld not found.\n", id ));
315 #else
316                         Debug( LDAP_DEBUG_TRACE,
317                                 "ldbm_search: candidate %ld not found\n",
318                                 id, 0, 0 );
319 #endif
320
321                         goto loop_continue;
322                 }
323
324                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
325                         Entry *matched;
326                         int err;
327                         const char *text;
328                         
329                         e = deref_entry_r( be, e, &err, &matched, &text );
330
331                         if( e == NULL ) {
332                                 e = matched;
333                                 goto loop_continue;
334                         }
335
336                         if( e->e_id == id ) {
337                                 /* circular loop */
338                                 goto loop_continue;
339                         }
340
341                         /* need to skip alias which deref into scope */
342                         if( scope & LDAP_SCOPE_ONELEVEL ) {
343                                 char *pdn = dn_parent( NULL, e->e_ndn );
344                                 if ( pdn != NULL ) {
345                                         if( strcmp( pdn, realbase ) ) {
346                                                 free( pdn );
347                                                 goto loop_continue;
348                                         }
349                                 }
350
351                         } else if ( dn_issuffix( e->e_ndn, 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)
382                                                 ? 1
383                                                 : (strcmp( dn, realbase ) ? 0 : 1 );
384                                 } else {
385                                         scopeok = (realbase == NULL || *realbase == '\0');
386                                 }
387
388                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
389                                 dn = ch_strdup( e->e_ndn );
390                                 scopeok = dn_issuffix( dn, realbase );
391                                 free( dn );
392
393                         } else {
394                                 scopeok = 1;
395                         }
396
397                         if( scopeok ) {
398                                 struct berval **erefs = get_entry_referrals(
399                                         be, conn, op, e );
400                                 struct berval **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_bvecfree( 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)
434                                                 ? 1
435                                                 : (strcmp( dn, realbase ) ? 0 : 1 );
436                                 } else {
437                                         scopeok = (realbase == NULL || *realbase == '\0');
438                                 }
439
440                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
441                                 dn = ch_strdup( e->e_ndn );
442                                 scopeok = dn_issuffix( dn, realbase );
443                                 free( dn );
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         if( candidates != NULL )
515                 idl_free( candidates );
516
517         ber_bvecfree( v2refs );
518         if( realbase ) free( realbase );
519
520         return rc;
521 }
522
523 static ID_BLOCK *
524 base_candidate(
525     Backend     *be,
526         Entry   *e )
527 {
528         ID_BLOCK                *idl;
529
530 #ifdef NEW_LOGGING
531         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
532                    "base_candidate: base (%s)\n", e->e_dn ));
533 #else
534         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
535                 e->e_dn, 0, 0);
536 #endif
537
538
539         idl = idl_alloc( 1 );
540         idl_insert( &idl, e->e_id, 1 );
541
542         return( idl );
543 }
544
545 static ID_BLOCK *
546 search_candidates(
547     Backend     *be,
548     Entry       *e,
549     Filter      *filter,
550     int         scope,
551         int             deref,
552         int             manageDSAit )
553 {
554         ID_BLOCK                *candidates;
555         Filter          f, fand, rf, af, xf;
556     AttributeAssertion aa_ref, aa_alias;
557         static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
558         static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
559
560 #ifdef NEW_LOGGING
561         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
562                    "search_candidates: base (%s) scope %d deref %d\n",
563                    e->e_ndn, scope, deref ));
564 #else
565         Debug(LDAP_DEBUG_TRACE,
566                 "search_candidates: base=\"%s\" s=%d d=%d\n",
567                 e->e_ndn, scope, deref );
568 #endif
569
570
571         xf.f_or = filter;
572         xf.f_choice = LDAP_FILTER_OR;
573         xf.f_next = NULL;
574
575         if( !manageDSAit ) {
576                 /* match referrals */
577                 rf.f_choice = LDAP_FILTER_EQUALITY;
578                 rf.f_ava = &aa_ref;
579                 rf.f_av_desc = slap_schema.si_ad_objectClass;
580                 rf.f_av_value = &bv_ref;
581                 rf.f_next = xf.f_or;
582                 xf.f_or = &rf;
583         }
584
585         if( deref & LDAP_DEREF_SEARCHING ) {
586                 /* match aliases */
587                 af.f_choice = LDAP_FILTER_EQUALITY;
588                 af.f_ava = &aa_alias;
589                 af.f_av_desc = slap_schema.si_ad_objectClass;
590                 af.f_av_value = &bv_alias;
591                 af.f_next = xf.f_or;
592                 xf.f_or = &af;
593         }
594
595         f.f_next = NULL;
596         f.f_choice = LDAP_FILTER_AND;
597         f.f_and = &fand;
598         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
599                 ? SLAPD_FILTER_DN_SUBTREE
600                 : SLAPD_FILTER_DN_ONE;
601         fand.f_dn = &e->e_nname;
602         fand.f_next = xf.f_or == filter ? filter : &xf ;
603
604         candidates = filter_candidates( be, &f );
605
606         return( candidates );
607 }