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