]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/search.c
Fix typos in previous commit
[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                 BDB_IDL_ALL( bdb, candidates );
235                 rc = search_candidates( be, e, filter,
236                         scope, deref, manageDSAit, candidates );
237         }
238
239         /* need normalized dn below */
240         realbase = ch_strdup( e->e_ndn );
241
242         /* start cursor at base entry's id 
243          * FIXME: hack to make "" base work */
244         cursor = e->e_id == NOID ? 1 : e->e_id;
245
246         if ( e != &slap_entry_root ) {
247                 bdb_entry_return( be, e );
248         }
249         e = NULL;
250
251         if ( candidates[0] == 0 ) {
252                 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
253                         0, 0, 0 );
254
255                 send_search_result( conn, op,
256                         LDAP_SUCCESS,
257                         NULL, NULL, NULL, NULL, 0 );
258
259                 rc = 1;
260                 goto done;
261         }
262
263         /* if not root and candidates exceed to-be-checked entries, abort */
264         if ( !isroot && limit->lms_s_unchecked != -1 ) {
265                 if ( BDB_IDL_N(candidates) > limit->lms_s_unchecked ) {
266                         send_search_result( conn, op, 
267                                         LDAP_UNWILLING_TO_PERFORM,
268                                         NULL, NULL, NULL, NULL, 0 );
269                         rc = 1;
270                         goto done;
271                 }
272         }
273
274         for ( id = bdb_idl_first( candidates, &cursor );
275                 id != NOID;
276                 id = bdb_idl_next( candidates, &cursor ) )
277         {
278                 int             scopeok = 0;
279
280                 /* check for abandon */
281                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
282                 abandon = op->o_abandon;
283                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
284
285                 if ( abandon ) {
286                         rc = 0;
287                         goto done;
288                 }
289
290                 /* check time limit */
291                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
292                         send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
293                                 NULL, NULL, v2refs, NULL, nentries );
294                         goto done;
295                 }
296
297                 /* get the entry with reader lock */
298                 rc = bdb_id2entry( be, NULL, id, &e );
299
300                 if ( e == NULL ) {
301                         if( !BDB_IDL_IS_RANGE(candidates) ) {
302                                 /* only complain for non-range IDLs */
303                                 Debug( LDAP_DEBUG_TRACE,
304                                         "bdb_search: candidate %ld not found\n",
305                                         (long) id, 0, 0 );
306                         }
307
308                         goto loop_continue;
309                 }
310
311 #ifdef BDB_ALIASES
312                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
313                         Entry *matched;
314                         int err;
315                         const char *text;
316                         
317                         e = deref_entry_r( be, e, &err, &matched, &text );
318
319                         if( e == NULL ) {
320                                 e = matched;
321                                 goto loop_continue;
322                         }
323
324                         if( e->e_id == id ) {
325                                 /* circular loop */
326                                 goto loop_continue;
327                         }
328
329                         /* need to skip alias which deref into scope */
330                         if( scope & LDAP_SCOPE_ONELEVEL ) {
331                                 char *pdn = dn_parent( NULL, e->e_ndn );
332                                 if ( pdn != NULL ) {
333                                         if( strcmp( pdn, realbase ) ) {
334                                                 free( pdn );
335                                                 goto loop_continue;
336                                         }
337                                         free(pdn);
338                                 }
339
340                         } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
341                                 /* alias is within scope */
342                                 Debug( LDAP_DEBUG_TRACE,
343                                         "bdb_search: \"%s\" in subtree\n",
344                                         e->e_dn, 0, 0 );
345                                 goto loop_continue;
346                         }
347
348                         scopeok = 1;
349                 }
350 #endif
351
352                 /*
353                  * if it's a referral, add it to the list of referrals. only do
354                  * this for non-base searches, and don't check the filter
355                  * explicitly here since it's only a candidate anyway.
356                  */
357                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
358                         is_entry_referral( e ) )
359                 {
360                         struct berval **erefs = get_entry_referrals(
361                                 be, conn, op, e, NULL, scope );
362                         struct berval **refs = referral_rewrite( erefs, e->e_dn, NULL,
363                                 scope == LDAP_SCOPE_SUBTREE 
364                                         ? LDAP_SCOPE_SUBTREE
365                                         : LDAP_SCOPE_BASE );
366
367                         send_search_reference( be, conn, op,
368                                 e, refs, NULL, &v2refs );
369
370                         ber_bvecfree( refs );
371
372                         goto loop_continue;
373                 }
374
375                 /* if it matches the filter and scope, send it */
376                 rc = test_filter( be, conn, op, e, filter );
377                 if ( rc == LDAP_COMPARE_TRUE ) {
378                         char    *dn;
379
380                         /* check scope */
381                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
382                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
383                                         (void) dn_normalize( dn );
384                                         scopeok = (dn == realbase)
385                                                 ? 1
386                                                 : (strcmp( dn, realbase ) ? 0 : 1 );
387                                         free( dn );
388
389                                 } else {
390                                         scopeok = (realbase == NULL || *realbase == '\0');
391                                 }
392
393                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
394                                 dn = ch_strdup( e->e_ndn );
395                                 scopeok = dn_issuffix( dn, realbase );
396                                 free( dn );
397
398                         } else {
399                                 scopeok = 1;
400                         }
401
402                         if ( scopeok ) {
403                                 /* check size limit */
404                                 if ( --slimit == -1 ) {
405                                         bdb_entry_return( be, e );
406                                         e = NULL;
407                                         send_search_result( conn, op,
408                                                 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
409                                                 v2refs, NULL, nentries );
410                                         goto done;
411                                 }
412
413                                 if (e) {
414                                         int result = send_search_entry( be, conn, op,
415                                                 e, attrs, attrsonly, NULL);
416
417                                         switch (result) {
418                                         case 0:         /* entry sent ok */
419                                                 nentries++;
420                                                 break;
421                                         case 1:         /* entry not sent */
422                                                 break;
423                                         case -1:        /* connection closed */
424                                                 bdb_entry_return( be, e );
425                                                 e = NULL;
426                                                 rc = LDAP_OTHER;
427                                                 goto done;
428                                         }
429                                 }
430                         } else {
431                                 Debug( LDAP_DEBUG_TRACE,
432                                         "bdb_search: %ld scope not okay\n",
433                                         (long) id, 0, 0 );
434                         }
435                 } else {
436                         Debug( LDAP_DEBUG_TRACE,
437                                 "bdb_search: %ld does match filter\n",
438                                 (long) id, 0, 0 );
439                 }
440
441 loop_continue:
442                 if( e != NULL ) {
443                         /* free reader lock */
444                         bdb_entry_return( be, e );
445                 }
446
447                 ldap_pvt_thread_yield();
448         }
449         send_search_result( conn, op,
450                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
451                 NULL, NULL, v2refs, NULL, nentries );
452
453         rc = 0;
454
455 done:
456         ber_bvecfree( v2refs );
457         if( realbase ) ch_free( realbase );
458
459         return rc;
460 }
461
462
463 static int base_candidate(
464         BackendDB       *be,
465         Entry   *e,
466         ID              *ids )
467 {
468         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
469                 e->e_dn, (long) e->e_id, 0);
470
471         ids[0] = 1;
472         ids[1] = e->e_id;
473         return 0;
474 }
475
476 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
477  * doesn't count, we're looking for explicit values.
478  */
479 static int oc_filter(
480         Filter *f
481 )
482 {
483         int rc = 0;
484
485         switch(f->f_choice) {
486         case LDAP_FILTER_EQUALITY:
487         case LDAP_FILTER_APPROX:
488                 if (f->f_av_desc == slap_schema.si_ad_objectClass)
489                         rc = 1;
490                 break;
491
492         case LDAP_FILTER_SUBSTRINGS:
493                 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
494                         rc = 1;
495                 break;
496
497         case LDAP_FILTER_AND:
498         case LDAP_FILTER_OR:
499                 for (f=f->f_and; f; f=f->f_next)
500                         if ((rc = oc_filter(f)))
501                                 break;
502                 break;
503         default:
504                 break;
505         }
506         return rc;
507 }
508
509 static int search_candidates(
510         BackendDB *be,
511         Entry *e,
512         Filter *filter,
513         int scope,
514         int deref,
515         int manageDSAit,
516         ID      *ids )
517 {
518         int rc;
519         Filter          f, fand, rf, xf;
520         ID              tmp[BDB_IDL_UM_SIZE];
521         AttributeAssertion aa_ref;
522         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
523 #ifdef BDB_ALIASES
524         Filter  af;
525         AttributeAssertion aa_alias;
526 #endif
527
528         Debug(LDAP_DEBUG_TRACE,
529                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
530                 e->e_dn, (long) e->e_id, scope );
531
532         xf.f_or = filter;
533         xf.f_choice = LDAP_FILTER_OR;
534         xf.f_next = NULL;
535
536         /* If the user's filter doesn't mention objectClass, or if
537          * it just uses objectClass=*, these clauses are redundant.
538          */
539         if (oc_filter(filter)) {
540                 if( !manageDSAit ) { /* match referrals */
541                         static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
542                         rf.f_choice = LDAP_FILTER_EQUALITY;
543                         rf.f_ava = &aa_ref;
544                         rf.f_av_desc = slap_schema.si_ad_objectClass;
545                         rf.f_av_value = &bv_ref;
546                         rf.f_next = xf.f_or;
547                         xf.f_or = &rf;
548                 }
549
550 #ifdef BDB_ALIASES
551                 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
552                         static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
553                         af.f_choice = LDAP_FILTER_EQUALITY;
554                         af.f_ava = &aa_alias;
555                         af.f_av_desc = slap_schema.si_ad_objectClass;
556                         af.f_av_value = &bv_alias;
557                         af.f_next = xf.f_or;
558                         xf.f_or = &af;
559                 }
560 #endif
561         }
562
563         f.f_next = NULL;
564         f.f_choice = LDAP_FILTER_AND;
565         f.f_and = &fand;
566         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
567                 ? SLAPD_FILTER_DN_SUBTREE
568                 : SLAPD_FILTER_DN_ONE;
569         fand.f_dn = e->e_ndn;
570         fand.f_next = xf.f_or == filter ? filter : &xf ;
571
572
573 #ifdef BDB_FILTER_INDICES
574         rc = bdb_filter_candidates( be, &f, ids, tmp );
575 #else
576         /* FIXME: Original code:
577         BDB_IDL_ID( bdb, ids, e->e_id );
578         * this is a hack to make "" base work; when bdb_filter_candidates
579         * is used this should not be needed any more */
580         BDB_IDL_ID( bdb, ids, (e->e_id == NOID ? 1 : e->e_id) );
581         rc = 0;
582 #endif
583
584         Debug(LDAP_DEBUG_TRACE,
585                 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
586                 (long) ids[0],
587                 (long) BDB_IDL_FIRST(ids),
588                 (long) BDB_IDL_LAST(ids) );
589
590         return rc;
591 }