]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/search.c
unifdef -DSLAP_ACL_HONOR_DISCLOSE
[openldap] / servers / slapd / back-bdb / search.c
1 /* search.c - search operation */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2006 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-bdb.h"
23 #include "idl.h"
24
25 static int base_candidate(
26         BackendDB       *be,
27         Entry   *e,
28         ID              *ids );
29
30 static int search_candidates(
31         Operation *op,
32         SlapReply *rs,
33         Entry *e,
34         u_int32_t locker,
35         ID      *ids,
36         ID      *scopes );
37
38 static int parse_paged_cookie( Operation *op, SlapReply *rs );
39
40 static void send_paged_response( 
41         Operation *op,
42         SlapReply *rs,
43         ID  *lastid,
44         int tentries );
45
46 /* Dereference aliases for a single alias entry. Return the final
47  * dereferenced entry on success, NULL on any failure.
48  */
49 static Entry * deref_base (
50         Operation *op,
51         SlapReply *rs,
52         Entry *e,
53         Entry **matched,
54         u_int32_t locker,
55         DB_LOCK *lock,
56         ID      *tmp,
57         ID      *visited )
58 {
59         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
60         struct berval ndn;
61         EntryInfo *ei;
62         DB_LOCK lockr;
63
64         rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
65         rs->sr_text = "maximum deref depth exceeded";
66
67         for (;;) {
68                 /* Remember the last entry we looked at, so we can
69                  * report broken links
70                  */
71                 *matched = e;
72
73                 if (BDB_IDL_N(tmp) >= op->o_bd->be_max_deref_depth) {
74                         e = NULL;
75                         break;
76                 }
77
78                 /* If this is part of a subtree or onelevel search,
79                  * have we seen this ID before? If so, quit.
80                  */
81                 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
82                         e = NULL;
83                         break;
84                 }
85
86                 /* If we've seen this ID during this deref iteration,
87                  * we've hit a loop.
88                  */
89                 if ( bdb_idl_insert( tmp, e->e_id ) ) {
90                         rs->sr_err = LDAP_ALIAS_PROBLEM;
91                         rs->sr_text = "circular alias";
92                         e = NULL;
93                         break;
94                 }
95
96                 /* If there was a problem getting the aliasedObjectName,
97                  * get_alias_dn will have set the error status.
98                  */
99                 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
100                         e = NULL;
101                         break;
102                 }
103
104                 rs->sr_err = bdb_dn2entry( op, NULL, &ndn, &ei,
105                         0, locker, &lockr );
106
107                 if ( ei ) {
108                         e = ei->bei_e;
109                 } else {
110                         e = NULL;
111                 }
112
113                 if (!e) {
114                         rs->sr_err = LDAP_ALIAS_PROBLEM;
115                         rs->sr_text = "aliasedObject not found";
116                         break;
117                 }
118
119                 /* Free the previous entry, continue to work with the
120                  * one we just retrieved.
121                  */
122                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
123                         *matched, lock);
124                 *lock = lockr;
125
126                 /* We found a regular entry. Return this to the caller. The
127                  * entry is still locked for Read.
128                  */
129                 if (!is_entry_alias(e)) {
130                         rs->sr_err = LDAP_SUCCESS;
131                         rs->sr_text = NULL;
132                         break;
133                 }
134         }
135         return e;
136 }
137
138 /* Look for and dereference all aliases within the search scope. Adds
139  * the dereferenced entries to the "ids" list. Requires "stack" to be
140  * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
141  * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
142  */
143 static int search_aliases(
144         Operation *op,
145         SlapReply *rs,
146         Entry *e,
147         u_int32_t locker,
148         ID *ids,
149         ID *scopes,
150         ID *stack )
151 {
152         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
153         ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
154         ID cursora, ida, cursoro, ido, *subscop2;
155         Entry *matched, *a;
156         EntryInfo *ei;
157         struct berval bv_alias = BER_BVC( "alias" );
158 #ifdef LDAP_COMP_MATCH
159         AttributeAssertion aa_alias = { NULL, BER_BVNULL, NULL };
160 #else
161         AttributeAssertion aa_alias = { NULL, BER_BVNULL };
162 #endif
163         Filter  af;
164         DB_LOCK locka, lockr;
165         int first = 1;
166
167         aliases = stack;        /* IDL of all aliases in the database */
168         curscop = aliases + BDB_IDL_DB_SIZE;    /* Aliases in the current scope */
169         subscop = curscop + BDB_IDL_DB_SIZE;    /* The current scope */
170         visited = subscop + BDB_IDL_DB_SIZE;    /* IDs we've seen in this search */
171         newsubs = visited + BDB_IDL_DB_SIZE;    /* New subtrees we've added */
172         oldsubs = newsubs + BDB_IDL_DB_SIZE;    /* Subtrees added previously */
173         tmp = oldsubs + BDB_IDL_DB_SIZE;        /* Scratch space for deref_base() */
174
175         /* A copy of subscop, because subscop gets clobbered by
176          * the bdb_idl_union/intersection routines
177          */
178         subscop2 = tmp + BDB_IDL_DB_SIZE;
179
180         af.f_choice = LDAP_FILTER_EQUALITY;
181         af.f_ava = &aa_alias;
182         af.f_av_desc = slap_schema.si_ad_objectClass;
183         af.f_av_value = bv_alias;
184         af.f_next = NULL;
185
186         /* Find all aliases in database */
187         BDB_IDL_ZERO( aliases );
188         rs->sr_err = bdb_filter_candidates( op, &af, aliases,
189                 curscop, visited );
190         if (rs->sr_err != LDAP_SUCCESS) {
191                 return rs->sr_err;
192         }
193         oldsubs[0] = 1;
194         oldsubs[1] = e->e_id;
195
196         BDB_IDL_ZERO( ids );
197         BDB_IDL_ZERO( visited );
198         BDB_IDL_ZERO( newsubs );
199
200         cursoro = 0;
201         ido = bdb_idl_first( oldsubs, &cursoro );
202
203         for (;;) {
204                 /* Set curscop to only the aliases in the current scope. Start with
205                  * all the aliases, obtain the IDL for the current scope, and then
206                  * get the intersection of these two IDLs. Add the current scope
207                  * to the cumulative list of candidates.
208                  */
209                 BDB_IDL_CPY( curscop, aliases );
210                 rs->sr_err = bdb_dn2idl( op, e, subscop,
211                         subscop2+BDB_IDL_DB_SIZE );
212                 if (first) {
213                         first = 0;
214                 } else {
215                         bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
216                 }
217                 BDB_IDL_CPY(subscop2, subscop);
218                 rs->sr_err = bdb_idl_intersection(curscop, subscop);
219                 bdb_idl_union( ids, subscop2 );
220
221                 /* Dereference all of the aliases in the current scope. */
222                 cursora = 0;
223                 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
224                         ida = bdb_idl_next(curscop, &cursora))
225                 {
226                         ei = NULL;
227 retry1:
228                         rs->sr_err = bdb_cache_find_id(op, NULL,
229                                 ida, &ei, 0, locker, &lockr );
230                         if (rs->sr_err != LDAP_SUCCESS) {
231                                 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
232                                         rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
233                                 continue;
234                         }
235                         a = ei->bei_e;
236
237                         /* This should only happen if the curscop IDL has maxed out and
238                          * turned into a range that spans IDs indiscriminately
239                          */
240                         if (!is_entry_alias(a)) {
241                                 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
242                                         a, &lockr);
243                                 continue;
244                         }
245
246                         /* Actually dereference the alias */
247                         BDB_IDL_ZERO(tmp);
248                         a = deref_base( op, rs, a, &matched, locker, &lockr,
249                                 tmp, visited );
250                         if (a) {
251                                 /* If the target was not already in our current candidates,
252                                  * make note of it in the newsubs list. Also
253                                  * set it in the scopes list so that bdb_search
254                                  * can check it.
255                                  */
256                                 if (bdb_idl_insert(ids, a->e_id) == 0) {
257                                         bdb_idl_insert(newsubs, a->e_id);
258                                         bdb_idl_insert(scopes, a->e_id);
259                                 }
260                                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
261                                         a, &lockr);
262
263                         } else if (matched) {
264                                 /* Alias could not be dereferenced, or it deref'd to
265                                  * an ID we've already seen. Ignore it.
266                                  */
267                                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
268                                         matched, &lockr );
269                                 rs->sr_text = NULL;
270                         }
271                 }
272                 /* If this is a OneLevel search, we're done; oldsubs only had one
273                  * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
274                  */
275                 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
276 nextido:
277                 ido = bdb_idl_next( oldsubs, &cursoro );
278                 
279                 /* If we're done processing the old scopes, did we add any new
280                  * scopes in this iteration? If so, go back and do those now.
281                  */
282                 if (ido == NOID) {
283                         if (BDB_IDL_IS_ZERO(newsubs)) break;
284                         BDB_IDL_CPY(oldsubs, newsubs);
285                         BDB_IDL_ZERO(newsubs);
286                         cursoro = 0;
287                         ido = bdb_idl_first( oldsubs, &cursoro );
288                 }
289
290                 /* Find the entry corresponding to the next scope. If it can't
291                  * be found, ignore it and move on. This should never happen;
292                  * we should never see the ID of an entry that doesn't exist.
293                  * Set the name so that the scope's IDL can be retrieved.
294                  */
295                 ei = NULL;
296 sameido:
297                 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
298                         0, locker, &locka );
299                 if ( rs->sr_err != LDAP_SUCCESS ) {
300                         if ( rs->sr_err == DB_LOCK_DEADLOCK ||
301                                 rs->sr_err == DB_LOCK_NOTGRANTED )
302                                 goto sameido;
303                         goto nextido;
304                 }
305                 e = ei->bei_e;
306         }
307         return rs->sr_err;
308 }
309
310 int
311 bdb_search( Operation *op, SlapReply *rs )
312 {
313         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
314         time_t          stoptime;
315         ID              id, cursor;
316         ID              candidates[BDB_IDL_UM_SIZE];
317         ID              scopes[BDB_IDL_DB_SIZE];
318         Entry           *e = NULL, base, e_root = {0};
319         Entry           *matched = NULL;
320         EntryInfo       *ei, ei_root = {0};
321         struct berval   realbase = BER_BVNULL;
322         slap_mask_t     mask;
323         int             manageDSAit;
324         int             tentries = 0;
325         ID              lastid = NOID;
326         AttributeName   *attrs;
327
328         u_int32_t       locker = 0;
329         DB_LOCK         lock;
330         struct  bdb_op_info     *opinfo = NULL;
331         DB_TXN                  *ltid = NULL;
332
333         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
334         attrs = op->oq_search.rs_attrs;
335
336         opinfo = (struct bdb_op_info *) op->o_private;
337
338         manageDSAit = get_manageDSAit( op );
339
340         if ( opinfo && opinfo->boi_txn ) {
341                 ltid = opinfo->boi_txn;
342                 locker = TXN_ID( ltid );
343         } else {
344                 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
345
346                 switch(rs->sr_err) {
347                 case 0:
348                         break;
349                 default:
350                         send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
351                         return rs->sr_err;
352                 }
353         }
354
355         if ( op->o_req_ndn.bv_len == 0 ) {
356                 /* DIT root special case */
357                 ei_root.bei_e = &e_root;
358                 ei_root.bei_parent = &ei_root;
359                 e_root.e_private = &ei_root;
360                 e_root.e_id = 0;
361                 BER_BVSTR( &e_root.e_nname, "" );
362                 BER_BVSTR( &e_root.e_name, "" );
363                 ei = &ei_root;
364                 rs->sr_err = LDAP_SUCCESS;
365         } else {
366                 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
367                         BDB_IDL_ZERO(candidates);
368                 }
369 dn2entry_retry:
370                 /* get entry with reader lock */
371                 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei,
372                         1, locker, &lock );
373         }
374
375         switch(rs->sr_err) {
376         case DB_NOTFOUND:
377                 matched = ei->bei_e;
378                 break;
379         case 0:
380                 e = ei->bei_e;
381                 break;
382         case LDAP_BUSY:
383                 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
384                 if ( !opinfo )
385                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
386                 return LDAP_BUSY;
387         case DB_LOCK_DEADLOCK:
388         case DB_LOCK_NOTGRANTED:
389                 goto dn2entry_retry;
390         default:
391                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
392                 if ( !opinfo )
393                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
394                 return rs->sr_err;
395         }
396
397         if ( op->ors_deref & LDAP_DEREF_FINDING ) {
398                 if ( matched && is_entry_alias( matched )) {
399                         struct berval stub;
400
401                         stub.bv_val = op->o_req_ndn.bv_val;
402                         stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
403                         e = deref_base( op, rs, matched, &matched, locker, &lock,
404                                 candidates, NULL );
405                         if ( e ) {
406                                 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
407                                         op->o_tmpmemctx );
408                                 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
409                                         e, &lock);
410                                 matched = NULL;
411                                 goto dn2entry_retry;
412                         }
413                 } else if ( e && is_entry_alias( e )) {
414                         e = deref_base( op, rs, e, &matched, locker, &lock,
415                                 candidates, NULL );
416                 }
417         }
418
419         if ( e == NULL ) {
420                 struct berval matched_dn = BER_BVNULL;
421
422                 if ( matched != NULL ) {
423                         BerVarray erefs = NULL;
424
425                         /* return referral only if "disclose"
426                          * is granted on the object */
427                         if ( ! access_allowed( op, matched,
428                                                 slap_schema.si_ad_entry,
429                                                 NULL, ACL_DISCLOSE, NULL ) )
430                         {
431                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
432
433                         } else {
434                                 ber_dupbv( &matched_dn, &matched->e_name );
435
436                                 erefs = is_entry_referral( matched )
437                                         ? get_entry_referrals( op, matched )
438                                         : NULL;
439                                 if ( rs->sr_err == DB_NOTFOUND )
440                                         rs->sr_err = LDAP_REFERRAL;
441                                 rs->sr_matched = matched_dn.bv_val;
442                         }
443
444 #ifdef SLAP_ZONE_ALLOC
445                         slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
446 #endif
447                         bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
448                                 matched, &lock);
449                         matched = NULL;
450
451                         if ( erefs ) {
452                                 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
453                                         &op->o_req_dn, op->oq_search.rs_scope );
454                                 ber_bvarray_free( erefs );
455                         }
456
457                 } else {
458 #ifdef SLAP_ZONE_ALLOC
459                         slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
460 #endif
461                         rs->sr_ref = referral_rewrite( default_referral,
462                                 NULL, &op->o_req_dn, op->oq_search.rs_scope );
463                         rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
464                 }
465
466                 send_ldap_result( op, rs );
467
468                 if ( !opinfo )
469                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
470                 if ( rs->sr_ref ) {
471                         ber_bvarray_free( rs->sr_ref );
472                         rs->sr_ref = NULL;
473                 }
474                 if ( !BER_BVISNULL( &matched_dn ) ) {
475                         ber_memfree( matched_dn.bv_val );
476                         rs->sr_matched = NULL;
477                 }
478                 return rs->sr_err;
479         }
480
481         /* NOTE: __NEW__ "search" access is required
482          * on searchBase object */
483         if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
484                                 NULL, ACL_SEARCH, NULL, &mask ) )
485         {
486                 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
487                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
488                 } else {
489                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
490                 }
491
492 #ifdef SLAP_ZONE_ALLOC
493                 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
494 #endif
495                 if ( e != &e_root ) {
496                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
497                 }
498                 send_ldap_result( op, rs );
499                 return rs->sr_err;
500         }
501
502         if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
503                 /* entry is a referral, don't allow add */
504                 struct berval matched_dn = BER_BVNULL;
505                 BerVarray erefs = NULL;
506                 
507                 ber_dupbv( &matched_dn, &e->e_name );
508                 erefs = get_entry_referrals( op, e );
509
510                 rs->sr_err = LDAP_REFERRAL;
511
512 #ifdef SLAP_ZONE_ALLOC
513                 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
514 #endif
515                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
516                 e = NULL;
517
518                 if ( erefs ) {
519                         rs->sr_ref = referral_rewrite( erefs, &matched_dn,
520                                 &op->o_req_dn, op->oq_search.rs_scope );
521                         ber_bvarray_free( erefs );
522
523                         if ( !rs->sr_ref ) {
524                                 rs->sr_text = "bad_referral object";
525                         }
526                 }
527
528                 Debug( LDAP_DEBUG_TRACE,
529                         LDAP_XSTRING(bdb_search) ": entry is referral\n",
530                         0, 0, 0 );
531
532                 rs->sr_matched = matched_dn.bv_val;
533                 send_ldap_result( op, rs );
534
535                 if ( !opinfo ) {
536                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
537                 }
538                 ber_bvarray_free( rs->sr_ref );
539                 rs->sr_ref = NULL;
540                 ber_memfree( matched_dn.bv_val );
541                 rs->sr_matched = NULL;
542                 return 1;
543         }
544
545         if ( get_assert( op ) &&
546                 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
547         {
548                 rs->sr_err = LDAP_ASSERTION_FAILED;
549 #ifdef SLAP_ZONE_ALLOC
550                 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
551 #endif
552                 if ( e != &e_root ) {
553                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
554                 }
555                 send_ldap_result( op, rs );
556                 return 1;
557         }
558
559         /* compute it anyway; root does not use it */
560         stoptime = op->o_time + op->ors_tlimit;
561
562         /* need normalized dn below */
563         ber_dupbv( &realbase, &e->e_nname );
564
565         /* Copy info to base, must free entry before accessing the database
566          * in search_candidates, to avoid deadlocks.
567          */
568         base.e_private = e->e_private;
569         base.e_nname = realbase;
570         base.e_id = e->e_id;
571
572 #ifdef SLAP_ZONE_ALLOC
573         slap_zn_runlock(bdb->bi_cache.c_zctx, e);
574 #endif
575         if ( e != &e_root ) {
576                 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
577         }
578         e = NULL;
579
580         /* select candidates */
581         if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
582                 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
583
584         } else {
585                 BDB_IDL_ZERO( candidates );
586                 BDB_IDL_ZERO( scopes );
587                 rs->sr_err = search_candidates( op, rs, &base,
588                         locker, candidates, scopes );
589         }
590
591         /* start cursor at beginning of candidates.
592          */
593         cursor = 0;
594
595         if ( candidates[0] == 0 ) {
596                 Debug( LDAP_DEBUG_TRACE,
597                         LDAP_XSTRING(bdb_search) ": no candidates\n",
598                         0, 0, 0 );
599
600                 goto nochange;
601         }
602
603         /* if not root and candidates exceed to-be-checked entries, abort */
604         if ( op->ors_limit      /* isroot == FALSE */ &&
605                 op->ors_limit->lms_s_unchecked != -1 &&
606                 BDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
607         {
608                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
609                 send_ldap_result( op, rs );
610                 rs->sr_err = LDAP_SUCCESS;
611                 goto done;
612         }
613
614         if ( op->ors_limit == NULL      /* isroot == TRUE */ ||
615                 !op->ors_limit->lms_s_pr_hide )
616         {
617                 tentries = BDB_IDL_N(candidates);
618         }
619
620         if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
621                 PagedResultsState *ps = op->o_pagedresults_state;
622                 /* deferred cookie parsing */
623                 rs->sr_err = parse_paged_cookie( op, rs );
624                 if ( rs->sr_err != LDAP_SUCCESS ) {
625                         send_ldap_result( op, rs );
626                         goto done;
627                 }
628
629                 if ( (ID)( ps->ps_cookie ) == 0 ) {
630                         id = bdb_idl_first( candidates, &cursor );
631
632                 } else {
633                         if ( ps->ps_size == 0 ) {
634                                 rs->sr_err = LDAP_SUCCESS;
635                                 rs->sr_text = "search abandoned by pagedResult size=0";
636                                 send_ldap_result( op, rs );
637                                 goto done;
638                         }
639                         for ( id = bdb_idl_first( candidates, &cursor );
640                                 id != NOID &&
641                                         id <= (ID)( ps->ps_cookie );
642                                 id = bdb_idl_next( candidates, &cursor ) )
643                         {
644                                 /* empty */;
645                         }
646                 }
647
648                 if ( cursor == NOID ) {
649                         Debug( LDAP_DEBUG_TRACE, 
650                                 LDAP_XSTRING(bdb_search)
651                                 ": no paged results candidates\n",
652                                 0, 0, 0 );
653                         send_paged_response( op, rs, &lastid, 0 );
654
655                         rs->sr_err = LDAP_OTHER;
656                         goto done;
657                 }
658                 goto loop_begin;
659         }
660
661         for ( id = bdb_idl_first( candidates, &cursor );
662                   id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
663         {
664                 int scopeok;
665
666 loop_begin:
667
668                 /* check for abandon */
669                 if ( op->o_abandon ) {
670                         rs->sr_err = SLAPD_ABANDON;
671                         goto done;
672                 }
673
674                 /* check time limit */
675                 if ( op->ors_tlimit != SLAP_NO_LIMIT
676                                 && slap_get_time() > stoptime )
677                 {
678                         rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
679                         rs->sr_ref = rs->sr_v2ref;
680                         send_ldap_result( op, rs );
681                         rs->sr_err = LDAP_SUCCESS;
682                         goto done;
683                 }
684
685 fetch_entry_retry:
686                         /* get the entry with reader lock */
687                         ei = NULL;
688                         rs->sr_err = bdb_cache_find_id( op, ltid,
689                                 id, &ei, 0, locker, &lock );
690
691                         if (rs->sr_err == LDAP_BUSY) {
692                                 rs->sr_text = "ldap server busy";
693                                 send_ldap_result( op, rs );
694                                 goto done;
695
696                         } else if ( rs->sr_err == DB_LOCK_DEADLOCK
697                                 || rs->sr_err == DB_LOCK_NOTGRANTED )
698                         {
699                                 goto fetch_entry_retry;
700                         }
701
702                         if ( ei && rs->sr_err == LDAP_SUCCESS ) {
703                                 e = ei->bei_e;
704                         } else {
705                                 e = NULL;
706                         }
707
708                         if ( e == NULL ) {
709                                 if( !BDB_IDL_IS_RANGE(candidates) ) {
710                                         /* only complain for non-range IDLs */
711                                         Debug( LDAP_DEBUG_TRACE,
712                                                 LDAP_XSTRING(bdb_search)
713                                                 ": candidate %ld not found\n",
714                                                 (long) id, 0, 0 );
715                                 }
716
717                                 goto loop_continue;
718                         }
719
720                 rs->sr_entry = e;
721
722                 if ( is_entry_subentry( e ) ) {
723                         if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
724                                 if(!get_subentries_visibility( op )) {
725                                         /* only subentries are visible */
726                                         goto loop_continue;
727                                 }
728
729                         } else if ( get_subentries( op ) &&
730                                 !get_subentries_visibility( op ))
731                         {
732                                 /* only subentries are visible */
733                                 goto loop_continue;
734                         }
735
736                 } else if ( get_subentries_visibility( op )) {
737                         /* only subentries are visible */
738                         goto loop_continue;
739                 }
740
741                 /* Does this candidate actually satisfy the search scope?
742                  *
743                  * Note that we don't lock access to the bei_parent pointer.
744                  * Since only leaf nodes can be deleted, the parent of any
745                  * node will always be a valid node. Also since we have
746                  * a Read lock on the data, it cannot be renamed out of the
747                  * scope while we are looking at it, and unless we're using
748                  * BDB_HIER, its parents cannot be moved either.
749                  */
750                 scopeok = 0;
751                 switch( op->ors_scope ) {
752                 case LDAP_SCOPE_BASE:
753                         /* This is always true, yes? */
754                         if ( id == base.e_id ) scopeok = 1;
755                         break;
756
757                 case LDAP_SCOPE_ONELEVEL:
758                         if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
759                         break;
760
761 #ifdef LDAP_SCOPE_CHILDREN
762                 case LDAP_SCOPE_CHILDREN:
763                         if ( id == base.e_id ) break;
764                         /* Fall-thru */
765 #endif
766                 case LDAP_SCOPE_SUBTREE: {
767                         EntryInfo *tmp;
768                         for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
769                                 if ( tmp->bei_id == base.e_id ) {
770                                         scopeok = 1;
771                                         break;
772                                 }
773                         }
774                         } break;
775                 }
776
777                 /* aliases were already dereferenced in candidate list */
778                 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
779                         /* but if the search base is an alias, and we didn't
780                          * deref it when finding, return it.
781                          */
782                         if ( is_entry_alias(e) &&
783                                 ((op->ors_deref & LDAP_DEREF_FINDING) ||
784                                         !bvmatch(&e->e_nname, &op->o_req_ndn)))
785                         {
786                                 goto loop_continue;
787                         }
788
789                         /* scopes is only non-empty for onelevel or subtree */
790                         if ( !scopeok && BDB_IDL_N(scopes) ) {
791                                 unsigned x;
792                                 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
793                                         x = bdb_idl_search( scopes, e->e_id );
794                                         if ( scopes[x] == e->e_id ) scopeok = 1;
795                                 } else {
796                                         /* subtree, walk up the tree */
797                                         EntryInfo *tmp = BEI(e);
798                                         for (;tmp->bei_parent; tmp=tmp->bei_parent) {
799                                                 x = bdb_idl_search( scopes, tmp->bei_id );
800                                                 if ( scopes[x] == tmp->bei_id ) {
801                                                         scopeok = 1;
802                                                         break;
803                                                 }
804                                         }
805                                 }
806                         }
807                 }
808
809                 /* Not in scope, ignore it */
810                 if ( !scopeok )
811                 {
812                         Debug( LDAP_DEBUG_TRACE,
813                                 LDAP_XSTRING(bdb_search)
814                                 ": %ld scope not okay\n",
815                                 (long) id, 0, 0 );
816                         goto loop_continue;
817                 }
818
819                 /*
820                  * if it's a referral, add it to the list of referrals. only do
821                  * this for non-base searches, and don't check the filter
822                  * explicitly here since it's only a candidate anyway.
823                  */
824                 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
825                         && is_entry_referral( e ) )
826                 {
827                         BerVarray erefs = get_entry_referrals( op, e );
828                         rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
829                                 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
830                                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
831
832                         send_search_reference( op, rs );
833
834                         ber_bvarray_free( rs->sr_ref );
835                         ber_bvarray_free( erefs );
836                         rs->sr_ref = NULL;
837
838                         goto loop_continue;
839                 }
840
841                 if ( !manageDSAit && is_entry_glue( e )) {
842                         goto loop_continue;
843                 }
844
845                 /* if it matches the filter and scope, send it */
846                 rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
847
848                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
849                         /* check size limit */
850                         if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
851                                 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
852 #ifdef SLAP_ZONE_ALLOC
853                                         slap_zn_runlock(bdb->bi_cache.c_zctx, e);
854 #endif
855                                         bdb_cache_return_entry_r( bdb->bi_dbenv,
856                                                         &bdb->bi_cache, e, &lock );
857                                         e = NULL;
858                                         send_paged_response( op, rs, &lastid, tentries );
859                                         goto done;
860                                 }
861                                 lastid = id;
862                         }
863
864                         if (e) {
865                                 /* safe default */
866                                 rs->sr_attrs = op->oq_search.rs_attrs;
867                                 rs->sr_operational_attrs = NULL;
868                                 rs->sr_ctrls = NULL;
869                                 rs->sr_flags = 0;
870                                 rs->sr_err = LDAP_SUCCESS;
871                                 rs->sr_err = send_search_entry( op, rs );
872
873                                 switch ( rs->sr_err ) {
874                                 case LDAP_SUCCESS:      /* entry sent ok */
875                                         break;
876                                 default:                /* entry not sent */
877                                         break;
878                                 case LDAP_UNAVAILABLE:
879                                 case LDAP_SIZELIMIT_EXCEEDED:
880 #ifdef SLAP_ZONE_ALLOC
881                                         slap_zn_runlock(bdb->bi_cache.c_zctx, e);
882 #endif
883                                         bdb_cache_return_entry_r(bdb->bi_dbenv,
884                                                 &bdb->bi_cache, e, &lock);
885                                         e = NULL;
886                                         rs->sr_entry = NULL;
887                                         if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
888                                                 rs->sr_ref = rs->sr_v2ref;
889                                                 send_ldap_result( op, rs );
890                                                 rs->sr_err = LDAP_SUCCESS;
891
892                                         } else {
893                                                 rs->sr_err = LDAP_OTHER;
894                                         }
895                                         goto done;
896                                 }
897                         }
898
899                 } else {
900                         Debug( LDAP_DEBUG_TRACE,
901                                 LDAP_XSTRING(bdb_search)
902                                 ": %ld does not match filter\n",
903                                 (long) id, 0, 0 );
904                 }
905
906 loop_continue:
907                 if( e != NULL ) {
908                         /* free reader lock */
909 #ifdef SLAP_ZONE_ALLOC
910                         slap_zn_runlock(bdb->bi_cache.c_zctx, e);
911 #endif
912                         bdb_cache_return_entry_r( bdb->bi_dbenv,
913                                 &bdb->bi_cache, e , &lock );
914                         e = NULL;
915                         rs->sr_entry = NULL;
916                 }
917         }
918
919 nochange:
920         rs->sr_ctrls = NULL;
921         rs->sr_ref = rs->sr_v2ref;
922         rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
923         rs->sr_rspoid = NULL;
924         if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
925                 send_paged_response( op, rs, NULL, 0 );
926         } else {
927                 send_ldap_result( op, rs );
928         }
929
930         rs->sr_err = LDAP_SUCCESS;
931
932 done:
933         if ( !opinfo )
934                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
935
936         if( rs->sr_v2ref ) {
937                 ber_bvarray_free( rs->sr_v2ref );
938                 rs->sr_v2ref = NULL;
939         }
940         if( realbase.bv_val ) ch_free( realbase.bv_val );
941
942         return rs->sr_err;
943 }
944
945
946 static int base_candidate(
947         BackendDB       *be,
948         Entry   *e,
949         ID              *ids )
950 {
951         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
952                 e->e_nname.bv_val, (long) e->e_id, 0);
953
954         ids[0] = 1;
955         ids[1] = e->e_id;
956         return 0;
957 }
958
959 /* Look for "objectClass Present" in this filter.
960  * Also count depth of filter tree while we're at it.
961  */
962 static int oc_filter(
963         Filter *f,
964         int cur,
965         int *max )
966 {
967         int rc = 0;
968
969         assert( f != NULL );
970
971         if( cur > *max ) *max = cur;
972
973         switch( f->f_choice ) {
974         case LDAP_FILTER_PRESENT:
975                 if (f->f_desc == slap_schema.si_ad_objectClass) {
976                         rc = 1;
977                 }
978                 break;
979
980         case LDAP_FILTER_AND:
981         case LDAP_FILTER_OR:
982                 cur++;
983                 for ( f=f->f_and; f; f=f->f_next ) {
984                         (void) oc_filter(f, cur, max);
985                 }
986                 break;
987
988         default:
989                 break;
990         }
991         return rc;
992 }
993
994 static void search_stack_free( void *key, void *data )
995 {
996         ber_memfree_x(data, NULL);
997 }
998
999 static void *search_stack( Operation *op )
1000 {
1001         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1002         void *ret = NULL;
1003
1004         if ( op->o_threadctx ) {
1005                 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1006                         &ret, NULL );
1007         } else {
1008                 ret = bdb->bi_search_stack;
1009         }
1010
1011         if ( !ret ) {
1012                 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1013                         * sizeof( ID ) );
1014                 if ( op->o_threadctx ) {
1015                         ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1016                                 ret, search_stack_free );
1017                 } else {
1018                         bdb->bi_search_stack = ret;
1019                 }
1020         }
1021         return ret;
1022 }
1023
1024 static int search_candidates(
1025         Operation *op,
1026         SlapReply *rs,
1027         Entry *e,
1028         u_int32_t locker,
1029         ID      *ids,
1030         ID      *scopes )
1031 {
1032         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1033         int rc, depth = 1;
1034         Filter          f, rf, xf, nf;
1035         ID              *stack;
1036 #ifdef LDAP_COMP_MATCH
1037         AttributeAssertion aa_ref = { NULL, BER_BVNULL, NULL };
1038 #else
1039         AttributeAssertion aa_ref = { NULL, BER_BVNULL };
1040 #endif
1041         Filter  sf;
1042 #ifdef LDAP_COMP_MATCH
1043         AttributeAssertion aa_subentry = { NULL, BER_BVNULL, NULL };
1044 #else
1045         AttributeAssertion aa_subentry = { NULL, BER_BVNULL };
1046 #endif
1047
1048         /*
1049          * This routine takes as input a filter (user-filter)
1050          * and rewrites it as follows:
1051          *      (&(scope=DN)[(objectClass=subentry)]
1052          *              (|[(objectClass=referral)(objectClass=alias)](user-filter))
1053          */
1054
1055         Debug(LDAP_DEBUG_TRACE,
1056                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1057                 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1058
1059         xf.f_or = op->oq_search.rs_filter;
1060         xf.f_choice = LDAP_FILTER_OR;
1061         xf.f_next = NULL;
1062
1063         /* If the user's filter uses objectClass=*,
1064          * these clauses are redundant.
1065          */
1066         if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1067                 && !get_subentries_visibility(op)) {
1068                 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1069                         /* match referral objects */
1070                         struct berval bv_ref = BER_BVC( "referral" );
1071                         rf.f_choice = LDAP_FILTER_EQUALITY;
1072                         rf.f_ava = &aa_ref;
1073                         rf.f_av_desc = slap_schema.si_ad_objectClass;
1074                         rf.f_av_value = bv_ref;
1075                         rf.f_next = xf.f_or;
1076                         xf.f_or = &rf;
1077                         depth++;
1078                 }
1079         }
1080
1081         f.f_next = NULL;
1082         f.f_choice = LDAP_FILTER_AND;
1083         f.f_and = &nf;
1084         /* Dummy; we compute scope separately now */
1085         nf.f_choice = SLAPD_FILTER_COMPUTED;
1086         nf.f_result = LDAP_SUCCESS;
1087         nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1088                 ? op->oq_search.rs_filter : &xf ;
1089         /* Filter depth increased again, adding dummy clause */
1090         depth++;
1091
1092         if( get_subentries_visibility( op ) ) {
1093                 struct berval bv_subentry = BER_BVC( "subentry" );
1094                 sf.f_choice = LDAP_FILTER_EQUALITY;
1095                 sf.f_ava = &aa_subentry;
1096                 sf.f_av_desc = slap_schema.si_ad_objectClass;
1097                 sf.f_av_value = bv_subentry;
1098                 sf.f_next = nf.f_next;
1099                 nf.f_next = &sf;
1100         }
1101
1102         /* Allocate IDL stack, plus 1 more for former tmp */
1103         if ( depth+1 > bdb->bi_search_stack_depth ) {
1104                 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1105         } else {
1106                 stack = search_stack( op );
1107         }
1108
1109         if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1110                 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1111         } else {
1112                 rc = bdb_dn2idl( op, e, ids, stack );
1113         }
1114
1115         if ( rc == LDAP_SUCCESS ) {
1116                 rc = bdb_filter_candidates( op, &f, ids,
1117                         stack, stack+BDB_IDL_UM_SIZE );
1118         }
1119
1120         if ( depth+1 > bdb->bi_search_stack_depth ) {
1121                 ch_free( stack );
1122         }
1123
1124         if( rc ) {
1125                 Debug(LDAP_DEBUG_TRACE,
1126                         "bdb_search_candidates: failed (rc=%d)\n",
1127                         rc, NULL, NULL );
1128
1129         } else {
1130                 Debug(LDAP_DEBUG_TRACE,
1131                         "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1132                         (long) ids[0],
1133                         (long) BDB_IDL_FIRST(ids),
1134                         (long) BDB_IDL_LAST(ids) );
1135         }
1136
1137         return rc;
1138 }
1139
1140 static int
1141 parse_paged_cookie( Operation *op, SlapReply *rs )
1142 {
1143         LDAPControl     **c;
1144         int             rc = LDAP_SUCCESS;
1145         ber_tag_t       tag;
1146         ber_int_t       size;
1147         BerElement      *ber;
1148         struct berval   cookie = BER_BVNULL;
1149         PagedResultsState *ps = op->o_pagedresults_state;
1150
1151         /* this function must be invoked only if the pagedResults
1152          * control has been detected, parsed and partially checked
1153          * by the frontend */
1154         assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1155
1156         /* look for the appropriate ctrl structure */
1157         for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
1158                 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
1159                 {
1160                         break;
1161                 }
1162         }
1163
1164         if ( c[0] == NULL ) {
1165                 rs->sr_text = "missing pagedResults control";
1166                 return LDAP_PROTOCOL_ERROR;
1167         }
1168
1169         /* Tested by frontend */
1170         assert( c[0]->ldctl_value.bv_len > 0 );
1171
1172         /* Parse the control value
1173          *      realSearchControlValue ::= SEQUENCE {
1174          *              size    INTEGER (0..maxInt),
1175          *                              -- requested page size from client
1176          *                              -- result set size estimate from server
1177          *              cookie  OCTET STRING
1178          * }
1179          */
1180         ber = ber_init( &c[0]->ldctl_value );
1181         if ( ber == NULL ) {
1182                 rs->sr_text = "internal error";
1183                 return LDAP_OTHER;
1184         }
1185
1186         tag = ber_scanf( ber, "{im}", &size, &cookie );
1187
1188         /* Tested by frontend */
1189         assert( tag != LBER_ERROR );
1190         assert( size >= 0 );
1191
1192         /* cookie decoding/checks deferred to backend... */
1193         if ( cookie.bv_len ) {
1194                 PagedResultsCookie reqcookie;
1195                 if( cookie.bv_len != sizeof( reqcookie ) ) {
1196                         /* bad cookie */
1197                         rs->sr_text = "paged results cookie is invalid";
1198                         rc = LDAP_PROTOCOL_ERROR;
1199                         goto done;
1200                 }
1201
1202                 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
1203
1204                 if ( reqcookie > ps->ps_cookie ) {
1205                         /* bad cookie */
1206                         rs->sr_text = "paged results cookie is invalid";
1207                         rc = LDAP_PROTOCOL_ERROR;
1208                         goto done;
1209
1210                 } else if ( reqcookie < ps->ps_cookie ) {
1211                         rs->sr_text = "paged results cookie is invalid or old";
1212                         rc = LDAP_UNWILLING_TO_PERFORM;
1213                         goto done;
1214                 }
1215
1216         } else {
1217                 /* Initial request.  Initialize state. */
1218 #if 0
1219                 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
1220                         /* There's another pagedResults control on the
1221                          * same connection; reject new pagedResults controls 
1222                          * (allowed by RFC2696) */
1223                         rs->sr_text = "paged results cookie unavailable; try later";
1224                         rc = LDAP_UNWILLING_TO_PERFORM;
1225                         goto done;
1226                 }
1227 #endif
1228                 ps->ps_cookie = 0;
1229                 ps->ps_count = 0;
1230         }
1231
1232 done:;
1233         (void)ber_free( ber, 1 );
1234
1235         return rc;
1236 }
1237
1238 static void
1239 send_paged_response( 
1240         Operation       *op,
1241         SlapReply       *rs,
1242         ID              *lastid,
1243         int             tentries )
1244 {
1245         LDAPControl     ctrl, *ctrls[2];
1246         BerElementBuffer berbuf;
1247         BerElement      *ber = (BerElement *)&berbuf;
1248         PagedResultsCookie respcookie;
1249         struct berval cookie;
1250
1251         Debug(LDAP_DEBUG_ARGS,
1252                 "send_paged_response: lastid=0x%08lx nentries=%d\n", 
1253                 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1254
1255         BER_BVZERO( &ctrl.ldctl_value );
1256         ctrls[0] = &ctrl;
1257         ctrls[1] = NULL;
1258
1259         ber_init2( ber, NULL, LBER_USE_DER );
1260
1261         if ( lastid ) {
1262                 respcookie = ( PagedResultsCookie )(*lastid);
1263                 cookie.bv_len = sizeof( respcookie );
1264                 cookie.bv_val = (char *)&respcookie;
1265
1266         } else {
1267                 respcookie = ( PagedResultsCookie )0;
1268                 BER_BVSTR( &cookie, "" );
1269         }
1270
1271         op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1272         op->o_conn->c_pagedresults_state.ps_count =
1273                 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1274                 rs->sr_nentries;
1275
1276         /* return size of 0 -- no estimate */
1277         ber_printf( ber, "{iO}", 0, &cookie ); 
1278
1279         if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1280                 goto done;
1281         }
1282
1283         ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1284         ctrls[0]->ldctl_iscritical = 0;
1285
1286         rs->sr_ctrls = ctrls;
1287         rs->sr_err = LDAP_SUCCESS;
1288         send_ldap_result( op, rs );
1289         rs->sr_ctrls = NULL;
1290
1291 done:
1292         (void) ber_free_buf( ber );
1293 }
1294