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