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