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