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