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