]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/search.c
6b3447ef5ce4d15f109a1d5a4dc21fe779da42e4
[openldap] / servers / slapd / back-bdb / search.c
1 /* search.c - search operation */
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 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "idl.h"
15 #include "external.h"
16
17 static int base_candidate(
18         BackendDB       *be,
19         Entry   *e,
20         ID              *ids );
21 static int search_candidates(
22         BackendDB *be,
23         Operation *op,
24         Entry *e,
25         Filter *filter,
26         int scope,
27         int deref,
28         ID      *ids );
29
30 int
31 bdb_search(
32         BackendDB       *be,
33         Connection      *conn,
34         Operation       *op,
35         struct berval   *base,
36         struct berval   *nbase,
37         int             scope,
38         int             deref,
39         int             slimit,
40         int             tlimit,
41         Filter  *filter,
42         struct berval   *filterstr,
43         AttributeName   *attrs,
44         int             attrsonly )
45 {
46         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
47         int              abandon;
48         int             rc;
49         const char *text = NULL;
50         time_t          stoptime;
51         ID              id, cursor;
52         ID              candidates[BDB_IDL_UM_SIZE];
53         Entry           *e = NULL;
54         BerVarray v2refs = NULL;
55         Entry   *matched = NULL;
56         struct berval   realbase = { 0, NULL };
57         int             nentries = 0;
58         int             manageDSAit;
59
60         struct slap_limits_set *limit = NULL;
61         int isroot = 0;
62
63         Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
64                 0, 0, 0);
65
66         manageDSAit = get_manageDSAit( op );
67
68         if ( nbase->bv_len == 0 ) {
69                 /* DIT root special case */
70                 e = (Entry *) &slap_entry_root;
71                 rc = 0;
72         } else                                          
73 #ifdef BDB_ALIASES
74         /* get entry with reader lock */
75         if ( deref & LDAP_DEREF_FINDING ) {
76                 e = deref_dn_r( be, nbase-, &err, &matched, &text );
77
78         } else
79 #endif
80         {
81                 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0 );
82         }
83
84         switch(rc) {
85         case DB_NOTFOUND:
86         case 0:
87                 break;
88         default:
89                 if (e != NULL) {
90                         bdb_cache_return_entry_w(&bdb->bi_cache, e);
91                 }
92                 if (matched != NULL) {
93                         bdb_cache_return_entry_r(&bdb->bi_cache, matched);
94                 }
95                 send_ldap_result( conn, op, rc=LDAP_OTHER,
96                         NULL, "internal error", NULL, NULL );
97                 return rc;
98         }
99
100         if ( e == NULL ) {
101                 struct berval matched_dn = { 0, NULL };
102                 BerVarray refs = NULL;
103
104                 if ( matched != NULL ) {
105                         BerVarray erefs;
106
107                         ber_dupbv( &matched_dn, &matched->e_name );
108
109                         erefs = is_entry_referral( matched )
110                                 ? get_entry_referrals( be, conn, op, matched )
111                                 : NULL;
112
113                         bdb_cache_return_entry_r (&bdb->bi_cache, matched);
114                         matched = NULL;
115
116                         if( erefs ) {
117                                 refs = referral_rewrite( erefs, &matched_dn,
118                                         base, scope );
119                                 ber_bvarray_free( erefs );
120                         }
121
122                 } else {
123                         refs = referral_rewrite( default_referral,
124                                 NULL, base, scope );
125                 }
126
127                 send_ldap_result( conn, op,     rc=LDAP_REFERRAL ,
128                         matched_dn.bv_val, text, refs, NULL );
129
130                 if ( refs ) ber_bvarray_free( refs );
131                 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
132                 return rc;
133         }
134
135         if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
136                 /* entry is a referral, don't allow add */
137                 struct berval matched_dn;
138                 BerVarray erefs, refs;
139                 
140                 ber_dupbv( &matched_dn, &e->e_name );
141                 erefs = get_entry_referrals( be, conn, op, e );
142                 refs = NULL;
143
144                 bdb_cache_return_entry_r( &bdb->bi_cache, e );
145                 e = NULL;
146
147                 if( erefs ) {
148                         refs = referral_rewrite( erefs, &matched_dn,
149                                 base, scope );
150                         ber_bvarray_free( erefs );
151                 }
152
153                 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
154                         0, 0, 0 );
155
156                 send_ldap_result( conn, op, LDAP_REFERRAL,
157                         matched_dn.bv_val,
158                         refs ? NULL : "bad referral object",
159                         refs, NULL );
160
161                 ber_bvarray_free( refs );
162                 ber_memfree( matched_dn.bv_val );
163                 return 1;
164         }
165
166         /* if not root, get appropriate limits */
167         if ( be_isroot( be, &op->o_ndn ) ) {
168                 isroot = 1;
169         } else {
170                 ( void ) get_limits( be, &op->o_ndn, &limit );
171         }
172
173         /* The time/size limits come first because they require very little
174          * effort, so there's no chance the candidates are selected and then 
175          * the request is not honored only because of time/size constraints */
176
177         /* if no time limit requested, use soft limit (unless root!) */
178         if ( isroot ) {
179                 if ( tlimit == 0 ) {
180                         tlimit = -1;    /* allow root to set no limit */
181                 }
182
183                 if ( slimit == 0 ) {
184                         slimit = -1;
185                 }
186
187         } else {
188                 /* if no limit is required, use soft limit */
189                 if ( tlimit <= 0 ) {
190                         tlimit = limit->lms_t_soft;
191
192                 /* if requested limit higher than hard limit, abort */
193                 } else if ( tlimit > limit->lms_t_hard ) {
194                         /* no hard limit means use soft instead */
195                         if ( limit->lms_t_hard == 0 ) {
196                                 tlimit = limit->lms_t_soft;
197
198                         /* positive hard limit means abort */
199                         } else if ( limit->lms_t_hard > 0 ) {
200                                 send_search_result( conn, op, 
201                                                 LDAP_UNWILLING_TO_PERFORM,
202                                                 NULL, NULL, NULL, NULL, 0 );
203                                 rc = 0;
204                                 goto done;
205                         }
206                 
207                         /* negative hard limit means no limit */
208                 }
209                 
210                 /* if no limit is required, use soft limit */
211                 if ( slimit <= 0 ) {
212                         slimit = limit->lms_s_soft;
213
214                 /* if requested limit higher than hard limit, abort */
215                 } else if ( slimit > limit->lms_s_hard ) {
216                         /* no hard limit means use soft instead */
217                         if ( limit->lms_s_hard == 0 ) {
218                                 slimit = limit->lms_s_soft;
219
220                         /* positive hard limit means abort */
221                         } else if ( limit->lms_s_hard > 0 ) {
222                                 send_search_result( conn, op, 
223                                                 LDAP_UNWILLING_TO_PERFORM,
224                                                 NULL, NULL, NULL, NULL, 0 );
225                                 rc = 0; 
226                                 goto done;
227                         }
228                         
229                         /* negative hard limit means no limit */
230                 }
231         }
232
233         /* compute it anyway; root does not use it */
234         stoptime = op->o_time + tlimit;
235
236         /* select candidates */
237         if ( scope == LDAP_SCOPE_BASE ) {
238                 rc = base_candidate( be, e, candidates );
239
240         } else {
241                 BDB_IDL_ALL( bdb, candidates );
242                 rc = search_candidates( be, op, e, filter,
243                         scope, deref, candidates );
244         }
245
246         /* need normalized dn below */
247         ber_dupbv( &realbase, &e->e_nname );
248
249         /* start cursor at base entry's id 
250          * FIXME: hack to make "" base work */
251         cursor = e->e_id == NOID ? 1 : e->e_id;
252
253         if ( e != &slap_entry_root ) {
254                 bdb_cache_return_entry_r(&bdb->bi_cache, e);
255         }
256         e = NULL;
257
258         if ( candidates[0] == 0 ) {
259                 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
260                         0, 0, 0 );
261
262                 send_search_result( conn, op,
263                         LDAP_SUCCESS,
264                         NULL, NULL, NULL, NULL, 0 );
265
266                 rc = 1;
267                 goto done;
268         }
269
270         /* if not root and candidates exceed to-be-checked entries, abort */
271         if ( !isroot && limit->lms_s_unchecked != -1 ) {
272                 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
273                         send_search_result( conn, op, 
274                                         LDAP_UNWILLING_TO_PERFORM,
275                                         NULL, NULL, NULL, NULL, 0 );
276                         rc = 1;
277                         goto done;
278                 }
279         }
280
281         for ( id = bdb_idl_first( candidates, &cursor );
282                 id != NOID;
283                 id = bdb_idl_next( candidates, &cursor ) )
284         {
285                 int             scopeok = 0;
286
287                 /* check for abandon */
288                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
289                 abandon = op->o_abandon;
290                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
291
292                 if ( abandon ) {
293                         rc = 0;
294                         goto done;
295                 }
296
297                 /* check time limit */
298                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
299                         send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
300                                 NULL, NULL, v2refs, NULL, nentries );
301                         goto done;
302                 }
303
304                 /* get the entry with reader lock */
305                 rc = bdb_id2entry_r( be, NULL, id, &e );
306
307                 if ( e == NULL ) {
308                         if( !BDB_IDL_IS_RANGE(candidates) ) {
309                                 /* only complain for non-range IDLs */
310                                 Debug( LDAP_DEBUG_TRACE,
311                                         "bdb_search: candidate %ld not found\n",
312                                         (long) id, 0, 0 );
313                         }
314
315                         goto loop_continue;
316                 }
317
318                 if ( is_entry_subentry( e ) ) {
319                         if( scope != LDAP_SCOPE_BASE ) {
320                                 if(!get_subentries_visibility( op )) {
321                                         /* only subentries are visible */
322                                         goto loop_continue;
323                                 }
324                         } else if ( get_subentries( op ) &&
325                                 !get_subentries_visibility( op ))
326                         {
327                                 /* only subentries are visible */
328                                 goto loop_continue;
329                         }
330                 } else if ( get_subentries_visibility( op )) {
331                         /* only subentries are visible */
332                         goto loop_continue;
333                 }
334
335 #ifdef BDB_ALIASES
336                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
337                         Entry *matched;
338                         int err;
339                         const char *text;
340                         
341                         e = deref_entry_r( be, e, &err, &matched, &text );
342
343                         if( e == NULL ) {
344                                 e = matched;
345                                 goto loop_continue;
346                         }
347
348                         if( e->e_id == id ) {
349                                 /* circular loop */
350                                 goto loop_continue;
351                         }
352
353                         /* need to skip alias which deref into scope */
354                         if( scope & LDAP_SCOPE_ONELEVEL ) {
355                                 struct berval   pdn;
356                                 
357                                 dnParent( &e->e_nname, &pdn ):
358                                 if ( ber_bvcmp( pdn, &realbase ) ) {
359                                         goto loop_continue;
360                                 }
361
362                         } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
363                                 /* alias is within scope */
364                                 Debug( LDAP_DEBUG_TRACE,
365                                         "bdb_search: \"%s\" in subtree\n",
366                                         e->e_dn, 0, 0 );
367                                 goto loop_continue;
368                         }
369
370                         scopeok = 1;
371                 }
372 #endif
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                         BerVarray erefs = get_entry_referrals(
383                                 be, conn, op, e );
384                         BerVarray refs = referral_rewrite( erefs,
385                                 &e->e_name, NULL,
386                                 scope == LDAP_SCOPE_SUBTREE 
387                                         ? LDAP_SCOPE_SUBTREE
388                                         : LDAP_SCOPE_BASE );
389
390                         send_search_reference( be, conn, op,
391                                 e, refs, NULL, &v2refs );
392
393                         ber_bvarray_free( refs );
394
395                         goto loop_continue;
396                 }
397
398                 /* if it matches the filter and scope, send it */
399                 rc = test_filter( be, conn, op, e, filter );
400                 if ( rc == LDAP_COMPARE_TRUE ) {
401                         struct berval   dn;
402
403                         /* check scope */
404                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
405                                 if ( be_issuffix( be, &e->e_nname ) ) {
406                                         scopeok = (realbase.bv_len == 0);
407                                 } else {
408                                         dnParent( &e->e_nname, &dn );
409                                         scopeok = dn_match( &dn, &realbase );
410                                 }
411
412                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
413                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
414
415                         } else {
416                                 scopeok = 1;
417                         }
418
419                         if ( scopeok ) {
420                                 /* check size limit */
421                                 if ( --slimit == -1 ) {
422                                         bdb_cache_return_entry_r (&bdb->bi_cache, e);
423                                         e = NULL;
424                                         send_search_result( conn, op,
425                                                 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
426                                                 v2refs, NULL, nentries );
427                                         goto done;
428                                 }
429
430                                 if (e) {
431                                         int result;
432                                         
433                                         if( op->o_noop ) {
434                                                 result = 0;
435                                         } else {
436                                                 result = send_search_entry( be, conn, op,
437                                                         e, attrs, attrsonly, NULL);
438                                         }
439
440                                         switch (result) {
441                                         case 0:         /* entry sent ok */
442                                                 nentries++;
443                                                 break;
444                                         case 1:         /* entry not sent */
445                                                 break;
446                                         case -1:        /* connection closed */
447                                                 bdb_cache_return_entry_r(&bdb->bi_cache, e);
448                                                 e = NULL;
449                                                 rc = LDAP_OTHER;
450                                                 goto done;
451                                         }
452                                 }
453                         } else {
454                                 Debug( LDAP_DEBUG_TRACE,
455                                         "bdb_search: %ld scope not okay\n",
456                                         (long) id, 0, 0 );
457                         }
458                 } else {
459                         Debug( LDAP_DEBUG_TRACE,
460                                 "bdb_search: %ld does match filter\n",
461                                 (long) id, 0, 0 );
462                 }
463
464 loop_continue:
465                 if( e != NULL ) {
466                         /* free reader lock */
467                         bdb_cache_return_entry_r ( &bdb->bi_cache, e );
468                         e = NULL;
469                 }
470
471                 ldap_pvt_thread_yield();
472         }
473         send_search_result( conn, op,
474                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
475                 NULL, NULL, v2refs, NULL, nentries );
476
477         rc = 0;
478
479 done:
480         if( e != NULL ) {
481                 /* free reader lock */
482                 bdb_cache_return_entry_r ( &bdb->bi_cache, e );
483         }
484
485         if( v2refs ) ber_bvarray_free( v2refs );
486         if( realbase.bv_val ) ch_free( realbase.bv_val );
487
488         return rc;
489 }
490
491
492 static int base_candidate(
493         BackendDB       *be,
494         Entry   *e,
495         ID              *ids )
496 {
497         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
498                 e->e_dn, (long) e->e_id, 0);
499
500         ids[0] = 1;
501         ids[1] = e->e_id;
502         return 0;
503 }
504
505 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
506  * doesn't count, we're looking for explicit values.
507  */
508 static int oc_filter(
509         Filter *f
510 )
511 {
512         int rc = 0;
513
514         switch(f->f_choice) {
515         case LDAP_FILTER_EQUALITY:
516         case LDAP_FILTER_APPROX:
517                 if (f->f_av_desc == slap_schema.si_ad_objectClass)
518                         rc = 1;
519                 break;
520
521         case LDAP_FILTER_SUBSTRINGS:
522                 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
523                         rc = 1;
524                 break;
525
526         case LDAP_FILTER_AND:
527         case LDAP_FILTER_OR:
528                 for (f=f->f_and; f; f=f->f_next)
529                         if ((rc = oc_filter(f)))
530                                 break;
531                 break;
532         default:
533                 break;
534         }
535         return rc;
536 }
537
538 static int search_candidates(
539         BackendDB *be,
540         Operation *op,
541         Entry *e,
542         Filter *filter,
543         int scope,
544         int deref,
545         ID      *ids )
546 {
547         int rc;
548         Filter          f, scopef, sf, rf, xf;
549         ID              tmp[BDB_IDL_UM_SIZE];
550         AttributeAssertion aa_ref;
551         AttributeAssertion aa_subentry;
552         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
553 #ifdef BDB_ALIASES
554         Filter  af;
555         AttributeAssertion aa_alias;
556 #endif
557
558         /*
559          * This routine takes as input a filter (user-filter)
560          * and rewrites it as follows:
561          *      (&(scope=DN)[(objectClass=subentry)]
562          *              (|[(objectClass=referral)(objectClass=alias)](user-filter))
563          */
564
565         Debug(LDAP_DEBUG_TRACE,
566                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
567                 e->e_dn, (long) e->e_id, scope );
568
569         xf.f_or = filter;
570         xf.f_choice = LDAP_FILTER_OR;
571         xf.f_next = NULL;
572
573         /* If the user's filter doesn't mention objectClass, or if
574          * it just uses objectClass=*, these clauses are redundant.
575          */
576         if (oc_filter(filter) && !get_subentries_visibility(op) ) {
577                 if( !get_manageDSAit(op) ) { /* match referrals */
578                         struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
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 #ifdef BDB_ALIASES
588                 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
589                         struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
590                         af.f_choice = LDAP_FILTER_EQUALITY;
591                         af.f_ava = &aa_alias;
592                         af.f_av_desc = slap_schema.si_ad_objectClass;
593                         af.f_av_value = bv_alias;
594                         af.f_next = xf.f_or;
595                         xf.f_or = &af;
596                 }
597 #endif
598         }
599
600         f.f_next = NULL;
601         f.f_choice = LDAP_FILTER_AND;
602         f.f_and = &scopef;
603         scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
604                 ? SLAPD_FILTER_DN_SUBTREE
605                 : SLAPD_FILTER_DN_ONE;
606         scopef.f_dn = &e->e_nname;
607         scopef.f_next = xf.f_or == filter ? filter : &xf ;
608
609         if( get_subentries_visibility( op ) ) {
610                 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
611                 sf.f_choice = LDAP_FILTER_EQUALITY;
612                 sf.f_ava = &aa_subentry;
613                 sf.f_av_desc = slap_schema.si_ad_objectClass;
614                 sf.f_av_value = bv_subentry;
615                 sf.f_next = scopef.f_next;
616                 scopef.f_next = &sf;
617         }
618
619 #ifdef BDB_FILTER_INDICES
620         rc = bdb_filter_candidates( be, &f, ids, tmp );
621 #else
622         /* FIXME: Original code:
623         BDB_IDL_ID( bdb, ids, e->e_id );
624         * this is a hack to make "" base work; when bdb_filter_candidates
625         * is used this should not be needed any more */
626         BDB_IDL_ID( bdb, ids, (e->e_id == NOID ? 1 : e->e_id) );
627         rc = 0;
628 #endif
629
630         Debug(LDAP_DEBUG_TRACE,
631                 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
632                 (long) ids[0],
633                 (long) BDB_IDL_FIRST(ids),
634                 (long) BDB_IDL_LAST(ids) );
635
636         return rc;
637 }