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