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