]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
Fix referrals and alias. For ldbm and I think for bdb, this is necessary
[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( BACK_LDBM, ENTRY, "ldbm_back_search: enter\n", 0, 0, 0 );
62 #else
63         Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
64 #endif
65
66         /* grab giant lock for reading */
67         ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
68
69         if ( nbase->bv_len == 0 ) {
70                 /* DIT root special case */
71                 e = (Entry *) &slap_entry_root;
72
73                 /* need normalized dn below */
74                 ber_dupbv( &realbase, &e->e_nname );
75
76                 candidates = search_candidates( be, e, filter,
77                     scope, deref, manageDSAit );
78
79                 goto searchit;
80                 
81         } else if ( deref & LDAP_DEREF_FINDING ) {
82                 /* deref dn and get entry with reader lock */
83                 e = deref_dn_r( be, nbase, &err, &matched, &text );
84
85                 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
86
87         } else {
88                 /* get entry with reader lock */
89                 e = dn2entry_r( be, nbase, &matched );
90                 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
91                 text = NULL;
92         }
93
94         if ( e == NULL ) {
95                 struct berval matched_dn = { 0, NULL };
96                 BerVarray refs = NULL;
97
98                 if ( matched != NULL ) {
99                         BerVarray erefs;
100                         ber_dupbv( &matched_dn, &matched->e_name );
101
102                         erefs = is_entry_referral( matched )
103                                 ? get_entry_referrals( be, conn, op, matched )
104                                 : NULL;
105
106                         cache_return_entry_r( &li->li_cache, matched );
107
108                         if( erefs ) {
109                                 refs = referral_rewrite( erefs, &matched_dn,
110                                         base, scope );
111
112                                 ber_bvarray_free( erefs );
113                         }
114
115                 } else {
116                         refs = referral_rewrite( default_referral,
117                                 NULL, base, scope );
118                 }
119
120                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
121
122                 send_ldap_result( conn, op, err, matched_dn.bv_val, 
123                         text, refs, NULL );
124
125                 ber_bvarray_free( refs );
126                 ber_memfree( matched_dn.bv_val );
127                 return 1;
128         }
129
130         if (!manageDSAit && is_entry_referral( e ) ) {
131                 /* entry is a referral, don't allow add */
132                 struct berval matched_dn;
133                 BerVarray erefs;
134                 BerVarray refs;
135
136                 ber_dupbv( &matched_dn, &e->e_name );
137                 erefs = get_entry_referrals( be, conn, op, e );
138                 refs = NULL;
139
140                 cache_return_entry_r( &li->li_cache, e );
141                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
142
143 #ifdef NEW_LOGGING
144                 LDAP_LOG( BACK_LDBM, INFO,
145                         "ldbm_search: entry (%s) is a referral.\n",
146                         e->e_dn, 0, 0 );
147 #else
148                 Debug( LDAP_DEBUG_TRACE,
149                         "ldbm_search: entry is referral\n",
150                         0, 0, 0 );
151 #endif
152
153                 if( erefs ) {
154                         refs = referral_rewrite( erefs, &matched_dn,
155                                 base, scope );
156
157                         ber_bvarray_free( erefs );
158                 }
159
160                 if( refs ) {
161                         send_ldap_result( conn, op, LDAP_REFERRAL,
162                                 matched_dn.bv_val, NULL, refs, NULL );
163                         ber_bvarray_free( refs );
164
165                 } else {
166                         send_ldap_result( conn, op, LDAP_OTHER,
167                                 matched_dn.bv_val,
168                         "bad referral object", NULL, NULL );
169                 }
170
171                 ber_memfree( matched_dn.bv_val );
172                 return 1;
173         }
174
175         if ( is_entry_alias( e ) ) {
176                 /* don't deref */
177                 deref = LDAP_DEREF_NEVER;
178         }
179
180         if ( scope == LDAP_SCOPE_BASE ) {
181                 cscope = LDAP_SCOPE_BASE;
182                 candidates = base_candidate( be, e );
183
184         } else {
185                 cscope = ( scope != LDAP_SCOPE_SUBTREE )
186                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
187                 candidates = search_candidates( be, e, filter,
188                     scope, deref, manageDSAit );
189         }
190
191         /* need normalized dn below */
192         ber_dupbv( &realbase, &e->e_nname );
193
194         cache_return_entry_r( &li->li_cache, e );
195
196 searchit:
197         if ( candidates == NULL ) {
198                 /* no candidates */
199 #ifdef NEW_LOGGING
200                 LDAP_LOG( BACK_LDBM, INFO,
201                         "ldbm_search: no candidates\n" , 0, 0, 0);
202 #else
203                 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
204                         0, 0, 0 );
205 #endif
206
207                 send_search_result( conn, op,
208                         LDAP_SUCCESS,
209                         NULL, NULL, NULL, NULL, 0 );
210
211                 rc = 1;
212                 goto done;
213         }
214
215         /* if not root, get appropriate limits */
216         if ( be_isroot( be, &op->o_ndn ) ) {
217                 isroot = 1;
218         } else {
219                 ( void ) get_limits( be, &op->o_ndn, &limit );
220         }
221
222         /* if candidates exceed to-be-checked entries, abort */
223         if ( !isroot && limit->lms_s_unchecked != -1 ) {
224                 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
225                         send_search_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
226                                         NULL, NULL, NULL, NULL, 0 );
227                         rc = 0;
228                         goto done;
229                 }
230         }
231         
232         /* if root an no specific limit is required, allow unlimited search */
233         if ( isroot ) {
234                 if ( tlimit == 0 ) {
235                         tlimit = -1;
236                 }
237
238                 if ( slimit == 0 ) {
239                         slimit = -1;
240                 }
241
242         } else {
243                 /* if no limit is required, use soft limit */
244                 if ( tlimit <= 0 ) {
245                         tlimit = limit->lms_t_soft;
246                 
247                 /* if requested limit higher than hard limit, abort */
248                 } else if ( tlimit > limit->lms_t_hard ) {
249                         /* no hard limit means use soft instead */
250                         if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
251                                 tlimit = limit->lms_t_soft;
252                         
253                         /* positive hard limit means abort */
254                         } else if ( limit->lms_t_hard > 0 ) {
255                                 send_search_result( conn, op, 
256                                                 LDAP_UNWILLING_TO_PERFORM,
257                                                 NULL, NULL, NULL, NULL, 0 );
258                                 rc = 0; 
259                                 goto done;
260                         }
261
262                         /* negative hard limit means no limit */
263                 }
264
265                 /* if no limit is required, use soft limit */
266                 if ( slimit <= 0 ) {
267                         slimit = limit->lms_s_soft;
268
269                 /* if requested limit higher than hard limit, abort */
270                 } else if ( slimit > limit->lms_s_hard ) {
271                         /* no hard limit means use soft instead */
272                         if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
273                                 slimit = limit->lms_s_soft;
274
275                         /* positive hard limit means abort */
276                         } else if ( limit->lms_s_hard > 0 ) {
277                                 send_search_result( conn, op,
278                                                 LDAP_UNWILLING_TO_PERFORM,
279                                                 NULL, NULL, NULL, NULL, 0 );
280                                 rc = 0;
281                                 goto done;
282                         }
283
284                         /* negative hard limit means no limit */
285                 }
286         }
287
288         /* compute it anyway; root does not use it */
289         stoptime = op->o_time + tlimit;
290
291         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
292             id = idl_nextid( candidates, &cursor ) )
293         {
294                 int scopeok = 0;
295                 int result = 0;
296
297                 /* check for abandon */
298                 if ( op->o_abandon ) {
299                         rc = 0;
300                         goto done;
301                 }
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( BACK_LDBM, INFO,
317                                 "ldbm_search: candidate %ld not found.\n", id, 0, 0 );
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                                 struct berval pdn;
347                                 dnParent( &e->e_nname, &pdn );
348                                 if ( ber_bvcmp( &pdn, &realbase ) ) {
349                                         goto loop_continue;
350                                 }
351
352                         } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
353                                 /* alias is within scope */
354 #ifdef NEW_LOGGING
355                                 LDAP_LOG( BACK_LDBM, DETAIL1,
356                                         "ldbm_search: alias \"%s\" in subtree\n", e->e_dn, 0, 0 );
357 #else
358                                 Debug( LDAP_DEBUG_TRACE,
359                                         "ldbm_search: alias \"%s\" in subtree\n",
360                                         e->e_dn, 0, 0 );
361 #endif
362
363                                 goto loop_continue;
364                         }
365
366                         scopeok = 1;
367                 }
368
369                 /*
370                  * if it's a referral, add it to the list of referrals. only do
371                  * this for non-base searches, and don't check the filter
372                  * explicitly here since it's only a candidate anyway.
373                  */
374                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
375                         is_entry_referral( e ) )
376                 {
377                         struct berval   dn;
378
379                         /* check scope */
380                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
381                                 if ( !be_issuffix( be, &e->e_nname ) ) {
382                                         dnParent( &e->e_nname, &dn );
383                                         scopeok = dn_match( &dn, &realbase );
384                                 } else {
385                                         scopeok = (realbase.bv_len == 0);
386                                 }
387
388                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
389                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
390
391                         } else {
392                                 scopeok = 1;
393                         }
394
395                         if( scopeok ) {
396                                 BerVarray erefs = get_entry_referrals(
397                                         be, conn, op, e );
398                                 BerVarray refs = referral_rewrite( erefs,
399                                         &e->e_name, NULL,
400                                         scope == LDAP_SCOPE_SUBTREE
401                                                 ? LDAP_SCOPE_SUBTREE
402                                                 : LDAP_SCOPE_BASE );
403
404                                 send_search_reference( be, conn, op,
405                                         e, refs, NULL, &v2refs );
406
407                                 ber_bvarray_free( refs );
408
409                         } else {
410 #ifdef NEW_LOGGING
411                                 LDAP_LOG( BACK_LDBM, DETAIL2,
412                                         "ldbm_search: candidate referral %ld scope not okay\n",
413                                         id, 0, 0 );
414 #else
415                                 Debug( LDAP_DEBUG_TRACE,
416                                         "ldbm_search: candidate referral %ld scope not okay\n",
417                                         id, 0, 0 );
418 #endif
419                         }
420
421                         goto loop_continue;
422                 }
423
424                 /* if it matches the filter and scope, send it */
425                 result = test_filter( be, conn, op, e, filter );
426
427                 if ( result == LDAP_COMPARE_TRUE ) {
428                         struct berval   dn;
429
430                         /* check scope */
431                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
432                                 if ( !be_issuffix( be, &e->e_nname ) ) {
433                                         dnParent( &e->e_nname, &dn );
434                                         scopeok = dn_match( &dn, &realbase );
435                                 } else {
436                                         scopeok = (realbase.bv_len == 0);
437                                 }
438
439                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
440                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
441
442                         } else {
443                                 scopeok = 1;
444                         }
445
446                         if ( scopeok ) {
447                                 /* check size limit */
448                                 if ( --slimit == -1 ) {
449                                         cache_return_entry_r( &li->li_cache, e );
450                                         send_search_result( conn, op,
451                                                 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
452                                                 v2refs, NULL, nentries );
453                                         rc = 0;
454                                         goto done;
455                                 }
456
457                                 if (e) {
458                                         result = send_search_entry(be, conn, op,
459                                                 e, attrs, attrsonly, NULL);
460
461                                         switch (result) {
462                                         case 0:         /* entry sent ok */
463                                                 nentries++;
464                                                 break;
465                                         case 1:         /* entry not sent */
466                                                 break;
467                                         case -1:        /* connection closed */
468                                                 cache_return_entry_r( &li->li_cache, e );
469                                                 rc = 0;
470                                                 goto done;
471                                         }
472                                 }
473                         } else {
474 #ifdef NEW_LOGGING
475                                 LDAP_LOG( BACK_LDBM, DETAIL2,
476                                         "ldbm_search: candidate entry %ld scope not okay\n", 
477                                         id, 0, 0 );
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( BACK_LDBM, DETAIL2,
488                                 "ldbm_search: candidate entry %ld does not match filter\n", 
489                                 id, 0, 0 );
490 #else
491                         Debug( LDAP_DEBUG_TRACE,
492                                 "ldbm_search: candidate entry %ld does not match filter\n",
493                                 id, 0, 0 );
494 #endif
495                 }
496
497 loop_continue:
498                 if( e != NULL ) {
499                         /* free reader lock */
500                         cache_return_entry_r( &li->li_cache, e );
501                 }
502
503                 ldap_pvt_thread_yield();
504         }
505
506         send_search_result( conn, op,
507                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
508                 NULL, NULL, v2refs, NULL, nentries );
509
510         rc = 0;
511
512 done:
513         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
514
515         if( candidates != NULL )
516                 idl_free( candidates );
517
518         if( v2refs ) ber_bvarray_free( v2refs );
519         if( realbase.bv_val ) free( realbase.bv_val );
520
521         return rc;
522 }
523
524 static ID_BLOCK *
525 base_candidate(
526     Backend     *be,
527         Entry   *e )
528 {
529         ID_BLOCK                *idl;
530
531 #ifdef NEW_LOGGING
532         LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
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         struct berval bv_ref = { sizeof("referral")-1, "referral" };
558         struct berval bv_alias = { sizeof("alias")-1, "alias" };
559
560 #ifdef NEW_LOGGING
561         LDAP_LOG( BACK_LDBM, 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 }