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