]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/search.c
Addition of servers/slapd/lcup.c and servers/slapd/back-bdb/lcup.c for persistent...
[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             rc;
48         const char *text = NULL;
49         time_t          stoptime;
50         ID              id, cursor;
51         ID              candidates[BDB_IDL_UM_SIZE];
52         Entry           *e = NULL;
53         BerVarray v2refs = NULL;
54         Entry   *matched = NULL;
55         struct berval   realbase = { 0, NULL };
56         int             nentries = 0;
57         int             manageDSAit;
58
59 #ifdef LDAP_CLIENT_UPDATE
60         Filter lcupf, csnfnot, csnfeq, csnfand, csnfge;
61         AttributeAssertion aa_ge, aa_eq;
62         int             entry_count = 0;
63         struct berval entrycsn_bv = { 0, NULL };
64         struct berval latest_entrycsn_bv = { 0, NULL };
65 #endif /* LDAP_CLIENT_UPDATE */
66
67         struct slap_limits_set *limit = NULL;
68         int isroot = 0;
69
70 #ifdef SLAP_X_FILTER_HASSUBORDINATES
71         int             filter_hasSubordinates = 0;
72 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
73
74         u_int32_t       locker;
75         DB_LOCK         lock;
76
77 #ifdef NEW_LOGGING
78         LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
79 #else
80         Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
81                 0, 0, 0);
82 #endif
83
84 #ifdef LDAP_CLIENT_UPDATE
85         if ( op->o_clientupdate_type & SLAP_LCUP_PERSIST ) {
86                 bdb_add_psearch_spec( be, conn, op, base, base, scope,
87                         deref, slimit, tlimit, filter, filterstr, attrs, attrsonly );
88                 return LDAP_SUCCESS;
89         }
90 #endif
91
92
93         manageDSAit = get_manageDSAit( op );
94
95         rc = LOCK_ID (bdb->bi_dbenv, &locker );
96         switch(rc) {
97         case 0:
98                 break;
99         default:
100                 send_ldap_result( conn, op, rc=LDAP_OTHER,
101                         NULL, "internal error", NULL, NULL );
102                 return rc;
103         }
104
105         if ( nbase->bv_len == 0 ) {
106                 /* DIT root special case */
107                 e = (Entry *) &slap_entry_root;
108                 rc = 0;
109         } else                                          
110 #ifdef BDB_ALIASES
111         /* get entry with reader lock */
112         if ( deref & LDAP_DEREF_FINDING ) {
113                 e = deref_dn_r( be, nbase-, &err, &matched, &text );
114
115         } else
116 #endif
117         {
118 dn2entry_retry:
119                 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
120         }
121
122         switch(rc) {
123         case DB_NOTFOUND:
124         case 0:
125                 break;
126         case LDAP_BUSY:
127                 if (e != NULL) {
128                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
129                 }
130                 if (matched != NULL) {
131                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
132                 }
133                 send_ldap_result( conn, op, LDAP_BUSY,
134                         NULL, "ldap server busy", NULL, NULL );
135                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
136                 return LDAP_BUSY;
137         case DB_LOCK_DEADLOCK:
138         case DB_LOCK_NOTGRANTED:
139                 goto dn2entry_retry;
140         default:
141                 if (e != NULL) {
142                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
143                 }
144                 if (matched != NULL) {
145                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
146                 }
147                 send_ldap_result( conn, op, rc=LDAP_OTHER,
148                         NULL, "internal error", NULL, NULL );
149                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
150                 return rc;
151         }
152
153         if ( e == NULL ) {
154                 struct berval matched_dn = { 0, NULL };
155                 BerVarray refs = NULL;
156
157                 if ( matched != NULL ) {
158                         BerVarray erefs;
159                         ber_dupbv( &matched_dn, &matched->e_name );
160
161                         erefs = is_entry_referral( matched )
162                                 ? get_entry_referrals( be, conn, op, matched )
163                                 : NULL;
164
165                         bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
166                         matched = NULL;
167
168                         if( erefs ) {
169                                 refs = referral_rewrite( erefs, &matched_dn,
170                                         base, scope );
171                                 ber_bvarray_free( erefs );
172                         }
173
174                 } else {
175                         refs = referral_rewrite( default_referral,
176                                 NULL, base, scope );
177                 }
178
179                 send_ldap_result( conn, op,     rc=LDAP_REFERRAL ,
180                         matched_dn.bv_val, text, refs, NULL );
181
182                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
183                 if ( refs ) ber_bvarray_free( refs );
184                 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
185                 return rc;
186         }
187
188         if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
189                 /* entry is a referral, don't allow add */
190                 struct berval matched_dn;
191                 BerVarray erefs, refs;
192                 
193                 ber_dupbv( &matched_dn, &e->e_name );
194                 erefs = get_entry_referrals( be, conn, op, e );
195                 refs = NULL;
196
197                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
198                 e = NULL;
199
200                 if( erefs ) {
201                         refs = referral_rewrite( erefs, &matched_dn,
202                                 base, scope );
203                         ber_bvarray_free( erefs );
204                 }
205
206 #ifdef NEW_LOGGING
207                 LDAP_LOG ( OPERATION, RESULTS, 
208                         "bdb_search: entry is referral\n", 0, 0, 0 );
209 #else
210                 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
211                         0, 0, 0 );
212 #endif
213
214                 send_ldap_result( conn, op, LDAP_REFERRAL,
215                         matched_dn.bv_val,
216                         refs ? NULL : "bad referral object",
217                         refs, NULL );
218
219                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
220                 ber_bvarray_free( refs );
221                 ber_memfree( matched_dn.bv_val );
222                 return 1;
223         }
224
225         /* if not root, get appropriate limits */
226         if ( be_isroot( be, &op->o_ndn ) ) {
227                 isroot = 1;
228         } else {
229                 ( void ) get_limits( be, &op->o_ndn, &limit );
230         }
231
232         /* The time/size limits come first because they require very little
233          * effort, so there's no chance the candidates are selected and then 
234          * the request is not honored only because of time/size constraints */
235
236         /* if no time limit requested, use soft limit (unless root!) */
237         if ( isroot ) {
238                 if ( tlimit == 0 ) {
239                         tlimit = -1;    /* allow root to set no limit */
240                 }
241
242                 if ( slimit == 0 ) {
243                         slimit = -1;
244                 }
245
246         } else {
247                 /* if no limit is required, use soft limit */
248                 if ( tlimit <= 0 ) {
249                         tlimit = limit->lms_t_soft;
250
251                 /* if requested limit higher than hard limit, abort */
252                 } else if ( tlimit > limit->lms_t_hard ) {
253                         /* no hard limit means use soft instead */
254                         if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
255                                 tlimit = limit->lms_t_soft;
256
257                         /* positive hard limit means abort */
258                         } else if ( limit->lms_t_hard > 0 ) {
259                                 send_search_result( conn, op, 
260                                                 LDAP_UNWILLING_TO_PERFORM,
261                                                 NULL, NULL, NULL, NULL, 0 );
262                                 rc = 0;
263                                 goto done;
264                         }
265                 
266                         /* negative hard limit means no limit */
267                 }
268                 
269                 /* if no limit is required, use soft limit */
270                 if ( slimit <= 0 ) {
271                         slimit = limit->lms_s_soft;
272
273                 /* if requested limit higher than hard limit, abort */
274                 } else if ( slimit > limit->lms_s_hard ) {
275                         /* no hard limit means use soft instead */
276                         if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
277                                 slimit = limit->lms_s_soft;
278
279                         /* positive hard limit means abort */
280                         } else if ( limit->lms_s_hard > 0 ) {
281                                 send_search_result( conn, op, 
282                                                 LDAP_UNWILLING_TO_PERFORM,
283                                                 NULL, NULL, NULL, NULL, 0 );
284                                 rc = 0; 
285                                 goto done;
286                         }
287                         
288                         /* negative hard limit means no limit */
289                 }
290         }
291
292         /* compute it anyway; root does not use it */
293         stoptime = op->o_time + tlimit;
294
295         /* select candidates */
296         if ( scope == LDAP_SCOPE_BASE ) {
297                 rc = base_candidate( be, e, candidates );
298
299         } else {
300                 BDB_IDL_ALL( bdb, candidates );
301                 rc = search_candidates( be, op, e, filter,
302                         scope, deref, candidates );
303         }
304
305         /* need normalized dn below */
306         ber_dupbv( &realbase, &e->e_nname );
307
308         /* start cursor at beginning of candidates.
309          */
310         cursor = 0;
311
312         if ( e != &slap_entry_root ) {
313                 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
314         }
315         e = NULL;
316
317         if ( candidates[0] == 0 ) {
318 #ifdef NEW_LOGGING
319                 LDAP_LOG ( OPERATION, RESULTS,
320                         "bdb_search: no candidates\n", 0, 0, 0 );
321 #else
322                 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
323                         0, 0, 0 );
324 #endif
325
326                 send_search_result( conn, op,
327                         LDAP_SUCCESS,
328                         NULL, NULL, NULL, NULL, 0 );
329
330                 rc = 1;
331                 goto done;
332         }
333
334         /* if not root and candidates exceed to-be-checked entries, abort */
335         if ( !isroot && limit->lms_s_unchecked != -1 ) {
336                 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
337                         send_search_result( conn, op, 
338                                         LDAP_ADMINLIMIT_EXCEEDED,
339                                         NULL, NULL, NULL, NULL, 0 );
340                         rc = 1;
341                         goto done;
342                 }
343         }
344
345 #ifdef SLAP_X_FILTER_HASSUBORDINATES
346         /*
347          * is hasSubordinates used in the filter ?
348          * FIXME: we may compute this directly when parsing the filter
349          */
350         filter_hasSubordinates = filter_has_subordinates( filter );
351 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
352
353 #ifdef LDAP_CLIENT_UPDATE
354         if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
355                 lcupf.f_choice = LDAP_FILTER_AND;
356                 lcupf.f_and = &csnfnot;
357                 lcupf.f_next = NULL;
358
359                 csnfnot.f_choice = LDAP_FILTER_NOT;
360                 csnfnot.f_not = &csnfeq;
361                 csnfnot.f_next = &csnfand;
362
363                 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
364                 csnfeq.f_ava = &aa_eq;
365                 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
366                 ber_dupbv( &csnfeq.f_av_value, &op->o_clientupdate_state );
367
368                 csnfand.f_choice = LDAP_FILTER_AND;
369                 csnfand.f_and = &csnfge;
370                 csnfand.f_next = NULL;
371
372                 csnfge.f_choice = LDAP_FILTER_GE;
373                 csnfge.f_ava = &aa_ge;
374                 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
375                 ber_dupbv( &csnfge.f_av_value, &op->o_clientupdate_state );
376                 csnfge.f_next = filter;
377         }
378 #endif /* LDAP_CLIENT_UPDATE */
379
380         for ( id = bdb_idl_first( candidates, &cursor );
381                 id != NOID;
382                 id = bdb_idl_next( candidates, &cursor ) )
383         {
384                 int             scopeok = 0;
385 #ifdef SLAP_X_FILTER_HASSUBORDINATES
386                 Attribute       *hasSubordinates = NULL;
387 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
388
389                 /* check for abandon */
390                 if ( op->o_abandon ) {
391                         rc = 0;
392                         goto done;
393                 }
394
395                 /* check time limit */
396                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
397                         send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
398                                 NULL, NULL, v2refs, NULL, nentries );
399                         goto done;
400                 }
401
402 id2entry_retry:
403                 /* get the entry with reader lock */
404                 rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
405
406                 if (rc == LDAP_BUSY) {
407                         send_ldap_result( conn, op, rc=LDAP_BUSY,
408                                 NULL, "ldap server busy", NULL, NULL );
409                         goto done;
410
411                 } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
412                         goto id2entry_retry;    
413                 }
414
415                 if ( e == NULL ) {
416                         if( !BDB_IDL_IS_RANGE(candidates) ) {
417                                 /* only complain for non-range IDLs */
418 #ifdef NEW_LOGGING
419                                 LDAP_LOG ( OPERATION, RESULTS,
420                                         "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
421 #else
422                                 Debug( LDAP_DEBUG_TRACE,
423                                         "bdb_search: candidate %ld not found\n",
424                                         (long) id, 0, 0 );
425 #endif
426                         }
427
428                         goto loop_continue;
429                 }
430
431 #ifdef BDB_SUBENTRIES
432                 if ( is_entry_subentry( e ) ) {
433                         if( scope != LDAP_SCOPE_BASE ) {
434                                 if(!get_subentries_visibility( op )) {
435                                         /* only subentries are visible */
436                                         goto loop_continue;
437                                 }
438
439                         } else if ( get_subentries( op ) &&
440                                 !get_subentries_visibility( op ))
441                         {
442                                 /* only subentries are visible */
443                                 goto loop_continue;
444                         }
445
446                 } else if ( get_subentries_visibility( op )) {
447                         /* only subentries are visible */
448                         goto loop_continue;
449                 }
450 #endif
451
452 #ifdef BDB_ALIASES
453                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
454                         Entry *matched;
455                         int err;
456                         const char *text;
457                         
458                         e = deref_entry_r( be, e, &err, &matched, &text );
459
460                         if( e == NULL ) {
461                                 e = matched;
462                                 goto loop_continue;
463                         }
464
465                         if( e->e_id == id ) {
466                                 /* circular loop */
467                                 goto loop_continue;
468                         }
469
470                         /* need to skip alias which deref into scope */
471                         if( scope & LDAP_SCOPE_ONELEVEL ) {
472                                 struct berval   pdn;
473                                 
474                                 dnParent( &e->e_nname, &pdn ):
475                                 if ( ber_bvcmp( pdn, &realbase ) ) {
476                                         goto loop_continue;
477                                 }
478
479                         } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
480                                 /* alias is within scope */
481 #ifdef NEW_LOGGING
482                                 LDAP_LOG ( OPERATION, RESULTS,
483                                         "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
484 #else
485                                 Debug( LDAP_DEBUG_TRACE,
486                                         "bdb_search: \"%s\" in subtree\n",
487                                         e->e_dn, 0, 0 );
488 #endif
489                                 goto loop_continue;
490                         }
491
492                         scopeok = 1;
493                 }
494 #endif
495
496                 /*
497                  * if it's a referral, add it to the list of referrals. only do
498                  * this for non-base searches, and don't check the filter
499                  * explicitly here since it's only a candidate anyway.
500                  */
501                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
502                         is_entry_referral( e ) )
503                 {
504                         struct berval   dn;
505
506                         /* check scope */
507                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
508                                 if ( !be_issuffix( be, &e->e_nname ) ) {
509                                         dnParent( &e->e_nname, &dn );
510                                         scopeok = dn_match( &dn, &realbase );
511                                 } else {
512                                         scopeok = (realbase.bv_len == 0);
513                                 }
514
515                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
516                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
517
518                         } else {
519                                 scopeok = 1;
520                         }
521
522                         if( scopeok ) {
523                                 BerVarray erefs = get_entry_referrals(
524                                         be, conn, op, e );
525                                 BerVarray refs = referral_rewrite( erefs,
526                                         &e->e_name, NULL,
527                                         scope == LDAP_SCOPE_SUBTREE
528                                                 ? LDAP_SCOPE_SUBTREE
529                                                 : LDAP_SCOPE_BASE );
530
531                                 send_search_reference( be, conn, op,
532                                         e, refs, NULL, &v2refs );
533
534                                 ber_bvarray_free( refs );
535
536                         } else {
537 #ifdef NEW_LOGGING
538                                 LDAP_LOG(OPERATION, DETAIL2, 
539                                         "bdb_search: candidate referral %ld scope not okay\n",
540                                         id, 0, 0 );
541 #else
542                                 Debug( LDAP_DEBUG_TRACE,
543                                         "bdb_search: candidate referral %ld scope not okay\n",
544                                         id, 0, 0 );
545 #endif
546                         }
547
548                         goto loop_continue;
549                 }
550
551 #ifdef SLAP_X_FILTER_HASSUBORDINATES
552                 /*
553                  * if hasSubordinates is used in the filter,
554                  * append it to the entry's attributes
555                  */
556                 if ( filter_hasSubordinates ) {
557                         int     hs;
558
559                         rc = bdb_hasSubordinates( be, conn, op, e, &hs);
560                         if ( rc != LDAP_SUCCESS ) {
561                                 goto loop_continue;
562                         }
563
564                         hasSubordinates = slap_operational_hasSubordinate(
565                                 hs == LDAP_COMPARE_TRUE );
566
567                         if ( hasSubordinates == NULL ) {
568                                 goto loop_continue;
569                         }
570
571                         hasSubordinates->a_next = e->e_attrs;
572                         e->e_attrs = hasSubordinates;
573                 }
574 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
575
576                 /* if it matches the filter and scope, send it */
577 #ifdef LDAP_CLIENT_UPDATE
578                 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
579                         rc = test_filter( be, conn, op, e, &lcupf );
580                 } else
581 #endif /* LDAP_CLIENT_UPDATE */
582                 {
583                         rc = test_filter( be, conn, op, e, filter );
584                 }
585
586 #ifdef SLAP_X_FILTER_HASSUBORDINATES
587                 if ( hasSubordinates ) {
588                         /*
589                          * FIXME: this is fairly inefficient, because 
590                          * if hasSubordinates is among the required
591                          * attrs, it will be added again later;
592                          * maybe we should leave it and check
593                          * check later if it's already present,
594                          * if required
595                          */
596                         e->e_attrs = e->e_attrs->a_next;
597                         attr_free( hasSubordinates );
598                 }
599 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
600
601                 if ( rc == LDAP_COMPARE_TRUE ) {
602                         struct berval   dn;
603
604                         /* check scope */
605                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
606                                 if ( be_issuffix( be, &e->e_nname ) ) {
607                                         scopeok = (realbase.bv_len == 0);
608                                 } else {
609                                         dnParent( &e->e_nname, &dn );
610                                         scopeok = dn_match( &dn, &realbase );
611                                 }
612
613                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
614                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
615
616                         } else {
617                                 scopeok = 1;
618                         }
619
620                         if ( scopeok ) {
621                                 /* check size limit */
622                                 if ( --slimit == -1 ) {
623                                         bdb_cache_return_entry_r( bdb->bi_dbenv,
624                                                 &bdb->bi_cache, e, &lock );
625                                         e = NULL;
626                                         send_search_result( conn, op,
627                                                 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
628                                                 v2refs, NULL, nentries );
629                                         goto done;
630                                 }
631
632                                 if (e) {
633                                         int result;
634                                         
635 #if 0   /* noop is masked SLAP_CTRL_UPDATE */
636                                         if( op->o_noop ) {
637                                                 result = 0;
638                                         } else
639 #endif
640                                         {
641 #ifdef LDAP_CLIENT_UPDATE
642                                                 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
643                                                         Attribute* a;
644                                                         int ret;
645                                                         int res;
646                                                         const char *text = NULL;
647                                                         LDAPControl *ctrls[2];
648                                                         struct berval *bv;
649
650                                                         BerElement *ber = ber_alloc_t( LBER_USE_DER );
651
652                                                         if ( ber == NULL ) {
653 #ifdef NEW_LOGGING
654                                                                 LDAP_LOG ( OPERATION, RESULTS, 
655                                                                         "bdb_search: ber_alloc_t failed\n",
656                                                                         0, 0, 0 );
657 #else
658                                                                 Debug( LDAP_DEBUG_TRACE,
659                                                                         "bdb_search: ber_alloc_t failed\n",
660                                                                         0, 0, 0 );
661 #endif
662                                                                 send_ldap_result( conn, op, rc=LDAP_OTHER,
663                                                                         NULL, "internal error", NULL, NULL );
664                                                                 goto done;
665                                                         }
666
667                                                         entry_count++;
668
669                                                         ctrls[0] = ch_malloc ( sizeof ( LDAPControl ) );
670                                                         ctrls[1] = NULL;
671
672                                                         if ( entry_count % op->o_clientupdate_interval == 0 ) {
673                                                                 /* Send cookie */
674                                                                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
675                                                                         AttributeDescription *desc = a->a_desc;
676                                                                         if ( desc == slap_schema.si_ad_entryCSN ) {
677                                                                                 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
678                                                                                 if ( latest_entrycsn_bv.bv_val == NULL ) {
679                                                                                         ber_dupbv( &latest_entrycsn_bv, &entrycsn_bv );
680                                                                                 } else {
681                                                                                         res = value_match( &ret, desc,
682                                                                                                 desc->ad_type->sat_ordering,
683                                                                                                 SLAP_MR_ASSERTION_SYNTAX_MATCH,
684                                                                                                 &entrycsn_bv, &latest_entrycsn_bv, &text );
685                                                                                         if ( res != LDAP_SUCCESS ) {
686                                                                                                 ret = 0;
687 #ifdef NEW_LOGGING
688                                                                                                 LDAP_LOG ( OPERATION, RESULTS, 
689                                                                                                         "bdb_search: value_match failed\n",
690                                                                                                         0, 0, 0 );
691 #else
692                                                                                                 Debug( LDAP_DEBUG_TRACE,
693                                                                                                         "bdb_search: value_match failed\n",
694                                                                                                         0, 0, 0 );
695 #endif
696                                                                                         }
697
698                                                                                         if ( ret > 0 ) {
699                                                                                                 ch_free( latest_entrycsn_bv.bv_val );
700                                                                                                 latest_entrycsn_bv.bv_val = NULL;
701                                                                                                 ber_dupbv( &latest_entrycsn_bv,
702                                                                                                         &entrycsn_bv );
703                                                                                         }
704                                                                                 }
705                                                                         }
706                                                                 }
707
708                                                                 ber_printf( ber,
709                                                                         "{bb{sON}N}",
710                                                                         SLAP_LCUP_STATE_UPDATE_FALSE,
711                                                                         SLAP_LCUP_ENTRY_DELETED_FALSE,
712                                                                         LCUP_COOKIE_OID, &entrycsn_bv );
713
714                                                                 ch_free( entrycsn_bv.bv_val );
715                                                                 entrycsn_bv.bv_val = NULL;
716
717                                                         } else {
718                                                                 /* Do not send cookie */
719                                                                 ber_printf( ber,
720                                                                         "{bbN}",
721                                                                         SLAP_LCUP_STATE_UPDATE_FALSE,
722                                                                         SLAP_LCUP_ENTRY_DELETED_FALSE );
723                                                         }
724
725                                                         ctrls[0]->ldctl_oid = LDAP_CONTROL_ENTRY_UPDATE;
726                                                         ctrls[0]->ldctl_iscritical = op->o_clientupdate;
727                                                         ret = ber_flatten( ber, &bv );
728
729                                                         if ( ret < 0 ) {
730 #ifdef NEW_LOGGING
731                                                                 LDAP_LOG ( OPERATION, RESULTS, 
732                                                                         "bdb_search: ber_flatten failed\n",
733                                                                         0, 0, 0 );
734 #else
735                                                                 Debug( LDAP_DEBUG_TRACE,
736                                                                         "bdb_search: ber_flatten failed\n",
737                                                                         0, 0, 0 );
738 #endif
739                                                                 send_ldap_result( conn, op, rc=LDAP_OTHER,
740                                                                         NULL, "internal error", NULL, NULL );
741                                                                 goto done;
742                                                         }
743
744                                                         ber_dupbv( &ctrls[0]->ldctl_value, bv );
745                                                         
746                                                         result = send_search_entry( be, conn, op,
747                                                                 e, attrs, attrsonly, ctrls);
748
749                                                         ch_free( ctrls[0]->ldctl_value.bv_val );
750                                                         ch_free( ctrls[0] );
751                                                         ber_free( ber, 1 );
752                                                         ber_bvfree( bv );
753                                                 } else
754 #endif /* LDAP_CLIENT_UPDATE */
755                                                 {
756                                                         result = send_search_entry( be, conn, op,
757                                                                 e, attrs, attrsonly, NULL);
758                                                 }
759                                         }
760
761                                         switch (result) {
762                                         case 0:         /* entry sent ok */
763                                                 nentries++;
764                                                 break;
765                                         case 1:         /* entry not sent */
766                                                 break;
767                                         case -1:        /* connection closed */
768                                                 bdb_cache_return_entry_r(bdb->bi_dbenv,
769                                                         &bdb->bi_cache, e, &lock);
770                                                 e = NULL;
771                                                 rc = LDAP_OTHER;
772                                                 goto done;
773                                         }
774                                 }
775                         } else {
776 #ifdef NEW_LOGGING
777                                 LDAP_LOG ( OPERATION, RESULTS,
778                                         "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
779 #else
780                                 Debug( LDAP_DEBUG_TRACE,
781                                         "bdb_search: %ld scope not okay\n",
782                                         (long) id, 0, 0 );
783 #endif
784                         }
785                 } else {
786 #ifdef NEW_LOGGING
787                         LDAP_LOG ( OPERATION, RESULTS,
788                                 "bdb_search: %ld does match filter\n", (long) id, 0, 0);
789 #else
790                         Debug( LDAP_DEBUG_TRACE,
791                                 "bdb_search: %ld does match filter\n",
792                                 (long) id, 0, 0 );
793 #endif
794                 }
795
796 loop_continue:
797                 if( e != NULL ) {
798                         /* free reader lock */
799                         bdb_cache_return_entry_r( bdb->bi_dbenv,
800                                 &bdb->bi_cache, e , &lock);
801                         e = NULL;
802                 }
803
804                 ldap_pvt_thread_yield();
805         }
806
807 #ifdef LDAP_CLIENT_UPDATE
808         if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
809                 int ret;
810                 LDAPControl *ctrls[2];
811                 BerElement *ber = ber_alloc_t( LBER_USE_DER );
812                 struct berval *bv;
813
814                 if ( ber == NULL ) {
815 #ifdef NEW_LOGGING
816                         LDAP_LOG ( OPERATION, RESULTS, 
817                                 "bdb_search: ber_alloc_t failed\n", 0, 0, 0 );
818 #else
819                         Debug( LDAP_DEBUG_TRACE, "bdb_search: ber_alloc_t failed\n",
820                                 0, 0, 0 );
821 #endif
822                         send_ldap_result( conn, op, rc=LDAP_OTHER,
823                                 NULL, "internal error", NULL, NULL );
824                         goto done;
825                 }
826
827                 ctrls[0] = ch_malloc ( sizeof ( LDAPControl ) );
828                 ctrls[1] = NULL;
829
830                 ber_printf( ber, "{sO", LCUP_COOKIE_OID, &latest_entrycsn_bv );
831                 ber_printf( ber, "N}" );
832
833                 ctrls[0]->ldctl_oid = LDAP_CONTROL_CLIENT_UPDATE_DONE;
834                 ctrls[0]->ldctl_iscritical = op->o_clientupdate;
835                 ret = ber_flatten( ber, &bv );
836
837                 if ( ret < 0 ) {
838 #ifdef NEW_LOGGING
839                         LDAP_LOG ( OPERATION, RESULTS, 
840                                 "bdb_search: ber_flatten failed\n", 0, 0, 0 );
841 #else
842                         Debug( LDAP_DEBUG_TRACE, "bdb_search: ber_flatten failed\n",
843                                 0, 0, 0 );
844 #endif
845                         send_ldap_result( conn, op, rc=LDAP_OTHER,
846                                 NULL, "internal error", NULL, NULL );
847                         goto done;
848                 }
849
850                 ber_dupbv( &ctrls[0]->ldctl_value, bv );
851
852                 send_search_result( conn, op,
853                         v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
854                         NULL, NULL, v2refs, ctrls, nentries );
855
856                 ch_free( latest_entrycsn_bv.bv_val );
857                 latest_entrycsn_bv.bv_val = NULL;
858                 ch_free( ctrls[0]->ldctl_value.bv_val );
859                 ch_free( ctrls[0] );
860                 ber_free( ber, 1 );
861                 ber_bvfree( bv );
862         } else
863 #endif /* LDAP_CLIENT_UPDATE */
864         {
865                 send_search_result( conn, op,
866                         v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
867                         NULL, NULL, v2refs, NULL, nentries );
868         }
869
870         rc = 0;
871
872 done:
873         if( e != NULL ) {
874                 /* free reader lock */
875                 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
876         }
877
878 #ifdef LDAP_CLIENT_UPDATE
879         if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
880                 if ( csnfeq.f_ava != NULL && csnfeq.f_av_value.bv_val != NULL ) {
881                         ch_free( csnfeq.f_av_value.bv_val );
882                 }
883         
884                 if ( csnfge.f_ava != NULL && csnfge.f_av_value.bv_val != NULL ) {
885                         ch_free( csnfge.f_av_value.bv_val );
886                 }
887         }
888 #endif /* LDAP_CLIENT_UPDATE */
889
890         LOCK_ID_FREE (bdb->bi_dbenv, locker );
891
892         if( v2refs ) ber_bvarray_free( v2refs );
893         if( realbase.bv_val ) ch_free( realbase.bv_val );
894
895         return rc;
896 }
897
898
899 static int base_candidate(
900         BackendDB       *be,
901         Entry   *e,
902         ID              *ids )
903 {
904 #ifdef NEW_LOGGING
905         LDAP_LOG ( OPERATION, ENTRY,
906                 "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
907 #else
908         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
909                 e->e_dn, (long) e->e_id, 0);
910 #endif
911
912         ids[0] = 1;
913         ids[1] = e->e_id;
914         return 0;
915 }
916
917 /* Look for "objectClass Present" in this filter.
918  * Also count depth of filter tree while we're at it.
919  */
920 static int oc_filter(
921         Filter *f,
922         int cur,
923         int *max
924 )
925 {
926         int rc = 0;
927
928         if( cur > *max ) *max = cur;
929
930         switch(f->f_choice) {
931         case LDAP_FILTER_PRESENT:
932                 if (f->f_desc == slap_schema.si_ad_objectClass) {
933                         rc = 1;
934                 }
935                 break;
936
937         case LDAP_FILTER_AND:
938         case LDAP_FILTER_OR:
939                 cur++;
940                 for (f=f->f_and; f; f=f->f_next) {
941                         (void) oc_filter(f, cur, max);
942                 }
943                 break;
944
945         default:
946                 break;
947         }
948         return rc;
949 }
950
951 static int search_candidates(
952         BackendDB *be,
953         Operation *op,
954         Entry *e,
955         Filter *filter,
956         int scope,
957         int deref,
958         ID      *ids )
959 {
960         int rc, depth = 1;
961         Filter          f, scopef, rf, xf;
962         ID              *stack;
963         AttributeAssertion aa_ref;
964 #ifdef BDB_SUBENTRIES
965         Filter  sf;
966         AttributeAssertion aa_subentry;
967 #endif
968 #ifdef BDB_ALIASES
969         Filter  af;
970         AttributeAssertion aa_alias;
971 #endif
972
973         /*
974          * This routine takes as input a filter (user-filter)
975          * and rewrites it as follows:
976          *      (&(scope=DN)[(objectClass=subentry)]
977          *              (|[(objectClass=referral)(objectClass=alias)](user-filter))
978          */
979
980 #ifdef NEW_LOGGING
981         LDAP_LOG ( OPERATION, ENTRY,
982                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n", 
983                 e->e_dn, (long) e->e_id, scope);
984 #else
985         Debug(LDAP_DEBUG_TRACE,
986                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
987                 e->e_dn, (long) e->e_id, scope );
988 #endif
989
990         xf.f_or = filter;
991         xf.f_choice = LDAP_FILTER_OR;
992         xf.f_next = NULL;
993
994         /* If the user's filter uses objectClass=*,
995          * these clauses are redundant.
996          */
997         if (!oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
998                 if( !get_manageDSAit(op) ) { /* match referrals */
999                         struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
1000                         rf.f_choice = LDAP_FILTER_EQUALITY;
1001                         rf.f_ava = &aa_ref;
1002                         rf.f_av_desc = slap_schema.si_ad_objectClass;
1003                         rf.f_av_value = bv_ref;
1004                         rf.f_next = xf.f_or;
1005                         xf.f_or = &rf;
1006                 }
1007
1008 #ifdef BDB_ALIASES
1009                 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
1010                         struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
1011                         af.f_choice = LDAP_FILTER_EQUALITY;
1012                         af.f_ava = &aa_alias;
1013                         af.f_av_desc = slap_schema.si_ad_objectClass;
1014                         af.f_av_value = bv_alias;
1015                         af.f_next = xf.f_or;
1016                         xf.f_or = &af;
1017                 }
1018 #endif
1019                 /* We added one of these clauses, filter depth increased */
1020                 if( xf.f_or != filter ) depth++;
1021         }
1022
1023         f.f_next = NULL;
1024         f.f_choice = LDAP_FILTER_AND;
1025         f.f_and = &scopef;
1026         scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
1027                 ? SLAPD_FILTER_DN_SUBTREE
1028                 : SLAPD_FILTER_DN_ONE;
1029         scopef.f_dn = &e->e_nname;
1030         scopef.f_next = xf.f_or == filter ? filter : &xf ;
1031         /* Filter depth increased again, adding scope clause */
1032         depth++;
1033
1034 #ifdef BDB_SUBENTRIES
1035         if( get_subentries_visibility( op ) ) {
1036                 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
1037                 sf.f_choice = LDAP_FILTER_EQUALITY;
1038                 sf.f_ava = &aa_subentry;
1039                 sf.f_av_desc = slap_schema.si_ad_objectClass;
1040                 sf.f_av_value = bv_subentry;
1041                 sf.f_next = scopef.f_next;
1042                 scopef.f_next = &sf;
1043         }
1044 #endif
1045
1046         /* Allocate IDL stack, plus 1 more for former tmp */
1047         stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1048
1049         rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
1050
1051         ch_free( stack );
1052
1053         if( rc ) {
1054 #ifdef NEW_LOGGING
1055                 LDAP_LOG ( OPERATION, DETAIL1,
1056                         "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0  );
1057 #else
1058                 Debug(LDAP_DEBUG_TRACE,
1059                         "bdb_search_candidates: failed (rc=%d)\n",
1060                         rc, NULL, NULL );
1061 #endif
1062
1063         } else {
1064 #ifdef NEW_LOGGING
1065                 LDAP_LOG ( OPERATION, DETAIL1,
1066                         "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1067                         (long) ids[0], (long) BDB_IDL_FIRST(ids), 
1068                         (long) BDB_IDL_LAST(ids));
1069 #else
1070                 Debug(LDAP_DEBUG_TRACE,
1071                         "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1072                         (long) ids[0],
1073                         (long) BDB_IDL_FIRST(ids),
1074                         (long) BDB_IDL_LAST(ids) );
1075 #endif
1076         }
1077
1078         return rc;
1079 }
1080