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