]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/search.c
Merge remote branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / servers / slapd / back-mdb / 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-2013 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-mdb.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         MDB_txn *txn,
35         MDB_cursor *mci,
36         ID      *ids,
37         ID2L    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 /* Dereference aliases for a single alias entry. Return the final
48  * dereferenced entry on success, NULL on any failure.
49  */
50 static Entry * deref_base (
51         Operation *op,
52         SlapReply *rs,
53         Entry *e,
54         Entry **matched,
55         MDB_txn *txn,
56         ID      *tmp,
57         ID      *visited )
58 {
59         struct berval ndn;
60
61         rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
62         rs->sr_text = "maximum deref depth exceeded";
63
64         for (;;) {
65                 /* Remember the last entry we looked at, so we can
66                  * report broken links
67                  */
68                 *matched = e;
69
70                 if (MDB_IDL_N(tmp) >= op->o_bd->be_max_deref_depth) {
71                         e = NULL;
72                         break;
73                 }
74
75                 /* If this is part of a subtree or onelevel search,
76                  * have we seen this ID before? If so, quit.
77                  */
78                 if ( visited && mdb_idl_insert( visited, e->e_id ) ) {
79                         e = NULL;
80                         break;
81                 }
82
83                 /* If we've seen this ID during this deref iteration,
84                  * we've hit a loop.
85                  */
86                 if ( mdb_idl_insert( tmp, e->e_id ) ) {
87                         rs->sr_err = LDAP_ALIAS_PROBLEM;
88                         rs->sr_text = "circular alias";
89                         e = NULL;
90                         break;
91                 }
92
93                 /* If there was a problem getting the aliasedObjectName,
94                  * get_alias_dn will have set the error status.
95                  */
96                 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
97                         e = NULL;
98                         break;
99                 }
100
101                 rs->sr_err = mdb_dn2entry( op, txn, NULL, &ndn, &e, NULL, 0 );
102                 if (rs->sr_err) {
103                         rs->sr_err = LDAP_ALIAS_PROBLEM;
104                         rs->sr_text = "aliasedObject not found";
105                         break;
106                 }
107
108                 /* Free the previous entry, continue to work with the
109                  * one we just retrieved.
110                  */
111                 mdb_entry_return( op, *matched );
112
113                 /* We found a regular entry. Return this to the caller.
114                  */
115                 if (!is_entry_alias(e)) {
116                         rs->sr_err = LDAP_SUCCESS;
117                         rs->sr_text = NULL;
118                         break;
119                 }
120         }
121         return e;
122 }
123
124 /* Look for and dereference all aliases within the search scope.
125  * Requires "stack" to be able to hold 6 levels of DB_SIZE IDLs.
126  * Of course we're hardcoded to require a minimum of 8 UM_SIZE
127  * IDLs so this is never a problem.
128  */
129 static int search_aliases(
130         Operation *op,
131         SlapReply *rs,
132         ID e_id,
133         MDB_txn *txn,
134         MDB_cursor *mci,
135         ID2L scopes,
136         ID *stack )
137 {
138         ID *aliases, *curscop, *visited, *newsubs, *oldsubs, *tmp;
139         ID cursora, ida, cursoro, ido;
140         Entry *matched, *a;
141         struct berval bv_alias = BER_BVC( "alias" );
142         AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
143         Filter  af;
144         int first = 1;
145
146         aliases = stack;        /* IDL of all aliases in the database */
147         curscop = aliases + MDB_IDL_DB_SIZE;    /* Aliases in the current scope */
148         visited = curscop + MDB_IDL_DB_SIZE;    /* IDs we've seen in this search */
149         newsubs = visited + MDB_IDL_DB_SIZE;    /* New subtrees we've added */
150         oldsubs = newsubs + MDB_IDL_DB_SIZE;    /* Subtrees added previously */
151         tmp = oldsubs + MDB_IDL_DB_SIZE;        /* Scratch space for deref_base() */
152
153         af.f_choice = LDAP_FILTER_EQUALITY;
154         af.f_ava = &aa_alias;
155         af.f_av_desc = slap_schema.si_ad_objectClass;
156         af.f_av_value = bv_alias;
157         af.f_next = NULL;
158
159         /* Find all aliases in database */
160         MDB_IDL_ZERO( aliases );
161         rs->sr_err = mdb_filter_candidates( op, txn, &af, aliases,
162                 curscop, visited );
163         if (rs->sr_err != LDAP_SUCCESS || MDB_IDL_IS_ZERO( aliases )) {
164                 return rs->sr_err;
165         }
166         oldsubs[0] = 1;
167         oldsubs[1] = e_id;
168
169         MDB_IDL_ZERO( visited );
170         MDB_IDL_ZERO( newsubs );
171
172         cursoro = 0;
173         ido = mdb_idl_first( oldsubs, &cursoro );
174
175         for (;;) {
176                 /* Set curscop to only the aliases in the current scope. Start with
177                  * all the aliases, then get the intersection with the scope.
178                  */
179                 rs->sr_err = mdb_idscope( op, txn, e_id, aliases, curscop );
180
181                 /* Dereference all of the aliases in the current scope. */
182                 cursora = 0;
183                 for (ida = mdb_idl_first(curscop, &cursora); ida != NOID;
184                         ida = mdb_idl_next(curscop, &cursora))
185                 {
186                         rs->sr_err = mdb_id2entry(op, mci, ida, &a);
187                         if (rs->sr_err != LDAP_SUCCESS) {
188                                 continue;
189                         }
190
191                         /* This should only happen if the curscop IDL has maxed out and
192                          * turned into a range that spans IDs indiscriminately
193                          */
194                         if (!is_entry_alias(a)) {
195                                 mdb_entry_return(op, a);
196                                 continue;
197                         }
198
199                         /* Actually dereference the alias */
200                         MDB_IDL_ZERO(tmp);
201                         a = deref_base( op, rs, a, &matched, txn,
202                                 tmp, visited );
203                         if (a) {
204                                 /* If the target was not already in our current scopes,
205                                  * make note of it in the newsubs list.
206                                  */
207                                 ID2 mid;
208                                 mid.mid = a->e_id;
209                                 mid.mval.mv_data = NULL;
210                                 if (mdb_id2l_insert(scopes, &mid) == 0) {
211                                         mdb_idl_insert(newsubs, a->e_id);
212                                 }
213                                 mdb_entry_return( op, a );
214
215                         } else if (matched) {
216                                 /* Alias could not be dereferenced, or it deref'd to
217                                  * an ID we've already seen. Ignore it.
218                                  */
219                                 mdb_entry_return( op, matched );
220                                 rs->sr_text = NULL;
221                         }
222                 }
223                 /* If this is a OneLevel search, we're done; oldsubs only had one
224                  * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
225                  */
226                 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
227 nextido:
228                 ido = mdb_idl_next( oldsubs, &cursoro );
229                 
230                 /* If we're done processing the old scopes, did we add any new
231                  * scopes in this iteration? If so, go back and do those now.
232                  */
233                 if (ido == NOID) {
234                         if (MDB_IDL_IS_ZERO(newsubs)) break;
235                         MDB_IDL_CPY(oldsubs, newsubs);
236                         MDB_IDL_ZERO(newsubs);
237                         cursoro = 0;
238                         ido = mdb_idl_first( oldsubs, &cursoro );
239                 }
240
241                 /* Find the entry corresponding to the next scope. If it can't
242                  * be found, ignore it and move on. This should never happen;
243                  * we should never see the ID of an entry that doesn't exist.
244                  */
245                 {
246                         MDB_val edata;
247                         rs->sr_err = mdb_id2edata(op, mci, ido, &edata);
248                         if ( rs->sr_err != MDB_SUCCESS ) {
249                                 goto nextido;
250                         }
251                         e_id = ido;
252                 }
253         }
254         return rs->sr_err;
255 }
256
257 /* Get the next ID from the DB. Used if the candidate list is
258  * a range and simple iteration hits missing entryIDs
259  */
260 static int
261 mdb_get_nextid(MDB_cursor *mci, ID *cursor)
262 {
263         MDB_val key;
264         ID id;
265         int rc;
266
267         id = *cursor + 1;
268         key.mv_data = &id;
269         key.mv_size = sizeof(ID);
270         rc = mdb_cursor_get( mci, &key, NULL, MDB_SET_RANGE );
271         if ( rc )
272                 return rc;
273         memcpy( cursor, key.mv_data, sizeof(ID));
274         return 0;
275 }
276
277 static void scope_chunk_free( void *key, void *data )
278 {
279         ID2 *p1, *p2;
280         for (p1 = data; p1; p1 = p2) {
281                 p2 = p1[0].mval.mv_data;
282                 ber_memfree_x(p1, NULL);
283         }
284 }
285
286 static ID2 *scope_chunk_get( Operation *op )
287 {
288         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
289         ID2 *ret = NULL;
290
291         ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
292                         (void *)&ret, NULL );
293         if ( !ret ) {
294                 ret = ch_malloc( MDB_IDL_UM_SIZE * sizeof( ID2 ));
295         } else {
296                 void *r2 = ret[0].mval.mv_data;
297                 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
298                         r2, scope_chunk_free, NULL, NULL );
299         }
300         return ret;
301 }
302
303 static void scope_chunk_ret( Operation *op, ID2 *scopes )
304 {
305         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
306         void *ret = NULL;
307
308         ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
309                         &ret, NULL );
310         scopes[0].mval.mv_data = ret;
311         ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
312                         (void *)scopes, scope_chunk_free, NULL, NULL );
313 }
314
315 int
316 mdb_search( Operation *op, SlapReply *rs )
317 {
318         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
319         ID              id, cursor, nsubs, ncand;
320         ID              lastid = NOID;
321         ID              candidates[MDB_IDL_UM_SIZE];
322         ID2             *scopes;
323         Entry           *e = NULL, *base = NULL;
324         Entry           *matched = NULL;
325         AttributeName   *attrs;
326         slap_mask_t     mask;
327         time_t          stoptime;
328         int             manageDSAit;
329         int             tentries = 0;
330         IdScopes        isc;
331         MDB_cursor      *mci, *mcd;
332
333         mdb_op_info     opinfo = {{{0}}}, *moi = &opinfo;
334         MDB_txn                 *ltid = NULL;
335
336         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_search) "\n", 0, 0, 0);
337         attrs = op->oq_search.rs_attrs;
338
339         manageDSAit = get_manageDSAit( op );
340
341         rs->sr_err = mdb_opinfo_get( op, mdb, 1, &moi );
342         switch(rs->sr_err) {
343         case 0:
344                 break;
345         default:
346                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
347                 return rs->sr_err;
348         }
349
350         ltid = moi->moi_txn;
351
352         rs->sr_err = mdb_cursor_open( ltid, mdb->mi_id2entry, &mci );
353         if ( rs->sr_err ) {
354                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
355                 return rs->sr_err;
356         }
357
358         rs->sr_err = mdb_cursor_open( ltid, mdb->mi_dn2id, &mcd );
359         if ( rs->sr_err ) {
360                 mdb_cursor_close( mci );
361                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
362                 return rs->sr_err;
363         }
364
365         scopes = scope_chunk_get( op );
366         isc.mt = ltid;
367         isc.mc = mcd;
368         isc.scopes = scopes;
369
370         if ( op->ors_deref & LDAP_DEREF_FINDING ) {
371                 MDB_IDL_ZERO(candidates);
372         }
373 dn2entry_retry:
374         /* get entry with reader lock */
375         rs->sr_err = mdb_dn2entry( op, ltid, mcd, &op->o_req_ndn, &e, &nsubs, 1 );
376
377         switch(rs->sr_err) {
378         case MDB_NOTFOUND:
379                 matched = e;
380                 e = NULL;
381                 break;
382         case 0:
383                 break;
384         case LDAP_BUSY:
385                 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
386                 goto done;
387         default:
388                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
389                 goto done;
390         }
391
392         if ( op->ors_deref & LDAP_DEREF_FINDING ) {
393                 if ( matched && is_entry_alias( matched )) {
394                         struct berval stub;
395
396                         stub.bv_val = op->o_req_ndn.bv_val;
397                         stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
398                         e = deref_base( op, rs, matched, &matched, ltid,
399                                 candidates, NULL );
400                         if ( e ) {
401                                 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
402                                         op->o_tmpmemctx );
403                                 mdb_entry_return(op, e);
404                                 matched = NULL;
405                                 goto dn2entry_retry;
406                         }
407                 } else if ( e && is_entry_alias( e )) {
408                         e = deref_base( op, rs, e, &matched, ltid,
409                                 candidates, NULL );
410                 }
411         }
412
413         if ( e == NULL ) {
414                 struct berval matched_dn = BER_BVNULL;
415
416                 if ( matched != NULL ) {
417                         BerVarray erefs = NULL;
418
419                         /* return referral only if "disclose"
420                          * is granted on the object */
421                         if ( ! access_allowed( op, matched,
422                                                 slap_schema.si_ad_entry,
423                                                 NULL, ACL_DISCLOSE, NULL ) )
424                         {
425                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
426
427                         } else {
428                                 ber_dupbv( &matched_dn, &matched->e_name );
429
430                                 erefs = is_entry_referral( matched )
431                                         ? get_entry_referrals( op, matched )
432                                         : NULL;
433                                 if ( rs->sr_err == MDB_NOTFOUND )
434                                         rs->sr_err = LDAP_REFERRAL;
435                                 rs->sr_matched = matched_dn.bv_val;
436                         }
437
438                         mdb_entry_return(op, matched);
439                         matched = NULL;
440
441                         if ( erefs ) {
442                                 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
443                                         &op->o_req_dn, op->oq_search.rs_scope );
444                                 ber_bvarray_free( erefs );
445                         }
446
447                 } else {
448                         rs->sr_ref = referral_rewrite( default_referral,
449                                 NULL, &op->o_req_dn, op->oq_search.rs_scope );
450                         rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
451                 }
452
453                 send_ldap_result( op, rs );
454
455                 if ( rs->sr_ref ) {
456                         ber_bvarray_free( rs->sr_ref );
457                         rs->sr_ref = NULL;
458                 }
459                 if ( !BER_BVISNULL( &matched_dn ) ) {
460                         ber_memfree( matched_dn.bv_val );
461                         rs->sr_matched = NULL;
462                 }
463                 goto done;
464         }
465
466         /* NOTE: __NEW__ "search" access is required
467          * on searchBase object */
468         if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
469                                 NULL, ACL_SEARCH, NULL, &mask ) )
470         {
471                 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
472                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
473                 } else {
474                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
475                 }
476
477                 mdb_entry_return( op,e);
478                 send_ldap_result( op, rs );
479                 goto done;
480         }
481
482         if ( !manageDSAit && is_entry_referral( e ) ) {
483                 /* entry is a referral */
484                 struct berval matched_dn = BER_BVNULL;
485                 BerVarray erefs = NULL;
486                 
487                 ber_dupbv( &matched_dn, &e->e_name );
488                 erefs = get_entry_referrals( op, e );
489
490                 rs->sr_err = LDAP_REFERRAL;
491
492                 mdb_entry_return( op, e );
493                 e = NULL;
494
495                 if ( erefs ) {
496                         rs->sr_ref = referral_rewrite( erefs, &matched_dn,
497                                 &op->o_req_dn, op->oq_search.rs_scope );
498                         ber_bvarray_free( erefs );
499
500                         if ( !rs->sr_ref ) {
501                                 rs->sr_text = "bad_referral object";
502                         }
503                 }
504
505                 Debug( LDAP_DEBUG_TRACE,
506                         LDAP_XSTRING(mdb_search) ": entry is referral\n",
507                         0, 0, 0 );
508
509                 rs->sr_matched = matched_dn.bv_val;
510                 send_ldap_result( op, rs );
511
512                 ber_bvarray_free( rs->sr_ref );
513                 rs->sr_ref = NULL;
514                 ber_memfree( matched_dn.bv_val );
515                 rs->sr_matched = NULL;
516                 goto done;
517         }
518
519         if ( get_assert( op ) &&
520                 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
521         {
522                 rs->sr_err = LDAP_ASSERTION_FAILED;
523                 mdb_entry_return( op,e);
524                 send_ldap_result( op, rs );
525                 goto done;
526         }
527
528         /* compute it anyway; root does not use it */
529         stoptime = op->o_time + op->ors_tlimit;
530
531         base = e;
532
533         e = NULL;
534
535         /* select candidates */
536         if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
537                 rs->sr_err = base_candidate( op->o_bd, base, candidates );
538                 ncand = 1;
539         } else {
540                 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
541                         size_t nkids;
542                         MDB_val key, data;
543                         key.mv_data = &base->e_id;
544                         key.mv_size = sizeof( ID );
545                         mdb_cursor_get( mcd, &key, &data, MDB_SET );
546                         mdb_cursor_count( mcd, &nkids );
547                         nsubs = nkids - 1;
548                 } else if ( !base->e_id ) {
549                         /* we don't maintain nsubs for entryID 0.
550                          * just grab entry count from id2entry stat
551                          */
552                         MDB_stat ms;
553                         mdb_stat( ltid, mdb->mi_id2entry, &ms );
554                         nsubs = ms.ms_entries;
555                 }
556                 MDB_IDL_ZERO( candidates );
557                 scopes[0].mid = 1;
558                 scopes[1].mid = base->e_id;
559                 scopes[1].mval.mv_data = NULL;
560                 rs->sr_err = search_candidates( op, rs, base,
561                         ltid, mci, candidates, scopes );
562                 ncand = MDB_IDL_N( candidates );
563                 if ( !base->e_id || ncand == NOID ) {
564                         /* grab entry count from id2entry stat
565                          */
566                         MDB_stat ms;
567                         mdb_stat( ltid, mdb->mi_id2entry, &ms );
568                         if ( !base->e_id )
569                                 nsubs = ms.ms_entries;
570                         if ( ncand == NOID )
571                                 ncand = ms.ms_entries;
572                 }
573         }
574
575         /* start cursor at beginning of candidates.
576          */
577         cursor = 0;
578
579         if ( candidates[0] == 0 ) {
580                 Debug( LDAP_DEBUG_TRACE,
581                         LDAP_XSTRING(mdb_search) ": no candidates\n",
582                         0, 0, 0 );
583
584                 goto nochange;
585         }
586
587         /* if not root and candidates exceed to-be-checked entries, abort */
588         if ( op->ors_limit      /* isroot == FALSE */ &&
589                 op->ors_limit->lms_s_unchecked != -1 &&
590                 ncand > (unsigned) op->ors_limit->lms_s_unchecked )
591         {
592                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
593                 send_ldap_result( op, rs );
594                 rs->sr_err = LDAP_SUCCESS;
595                 goto done;
596         }
597
598         if ( op->ors_limit == NULL      /* isroot == TRUE */ ||
599                 !op->ors_limit->lms_s_pr_hide )
600         {
601                 tentries = ncand;
602         }
603
604         if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
605                 PagedResultsState *ps = op->o_pagedresults_state;
606                 /* deferred cookie parsing */
607                 rs->sr_err = parse_paged_cookie( op, rs );
608                 if ( rs->sr_err != LDAP_SUCCESS ) {
609                         send_ldap_result( op, rs );
610                         goto done;
611                 }
612
613                 cursor = (ID) ps->ps_cookie;
614                 if ( cursor && ps->ps_size == 0 ) {
615                         rs->sr_err = LDAP_SUCCESS;
616                         rs->sr_text = "search abandoned by pagedResult size=0";
617                         send_ldap_result( op, rs );
618                         goto done;
619                 }
620                 id = mdb_idl_first( candidates, &cursor );
621                 if ( id == NOID ) {
622                         Debug( LDAP_DEBUG_TRACE, 
623                                 LDAP_XSTRING(mdb_search)
624                                 ": no paged results candidates\n",
625                                 0, 0, 0 );
626                         send_paged_response( op, rs, &lastid, 0 );
627
628                         rs->sr_err = LDAP_OTHER;
629                         goto done;
630                 }
631                 if ( id == (ID)ps->ps_cookie )
632                         id = mdb_idl_next( candidates, &cursor );
633                 nsubs = ncand;  /* always bypass scope'd search */
634                 goto loop_begin;
635         }
636         if ( nsubs < ncand ) {
637                 int rc;
638                 /* Do scope-based search */
639                 isc.id = base->e_id;
640                 isc.numrdns = 0;
641                 rc = mdb_dn2id_walk( op, &isc );
642                 if ( rc )
643                         id = NOID;
644                 else
645                         id = isc.id;
646         } else {
647                 id = mdb_idl_first( candidates, &cursor );
648         }
649
650         while (id != NOID)
651         {
652                 int scopeok;
653                 MDB_val edata;
654
655 loop_begin:
656
657                 /* check for abandon */
658                 if ( op->o_abandon ) {
659                         rs->sr_err = SLAPD_ABANDON;
660                         send_ldap_result( op, rs );
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
685                 if ( nsubs < ncand ) {
686                         unsigned i;
687                         /* Is this entry in the candidate list? */
688                         scopeok = 0;
689                         if (MDB_IDL_IS_RANGE( candidates )) {
690                                 if ( id >= MDB_IDL_RANGE_FIRST( candidates ) &&
691                                         id <= MDB_IDL_RANGE_LAST( candidates ))
692                                         scopeok = 1;
693                         } else {
694                                 i = mdb_idl_search( candidates, id );
695                                 if ( candidates[i] == id )
696                                         scopeok = 1;
697                         }
698                         if ( scopeok )
699                                 goto scopeok;
700                         goto loop_continue;
701                 }
702
703                 /* Does this candidate actually satisfy the search scope?
704                  */
705                 scopeok = 0;
706                 isc.numrdns = 0;
707                 switch( op->ors_scope ) {
708                 case LDAP_SCOPE_BASE:
709                         /* This is always true, yes? */
710                         if ( id == base->e_id ) scopeok = 1;
711                         break;
712
713 #ifdef LDAP_SCOPE_CHILDREN
714                 case LDAP_SCOPE_CHILDREN:
715                         if ( id == base->e_id ) break;
716                         /* Fall-thru */
717 #endif
718                 case LDAP_SCOPE_SUBTREE:
719                         if ( id == base->e_id ) {
720                                 scopeok = 1;
721                                 break;
722                         }
723                         /* Fall-thru */
724                 case LDAP_SCOPE_ONELEVEL:
725                         isc.id = id;
726                         isc.nscope = 0;
727                         rs->sr_err = mdb_idscopes( op, &isc );
728                         if ( rs->sr_err == MDB_SUCCESS ) {
729                                 if ( isc.nscope )
730                                         scopeok = 1;
731                         } else {
732                                 if ( rs->sr_err == MDB_NOTFOUND )
733                                         goto notfound;
734                         }
735                         break;
736                 }
737
738                 /* Not in scope, ignore it */
739                 if ( !scopeok )
740                 {
741                         Debug( LDAP_DEBUG_TRACE,
742                                 LDAP_XSTRING(mdb_search)
743                                 ": %ld scope not okay\n",
744                                 (long) id, 0, 0 );
745                         goto loop_continue;
746                 }
747
748 scopeok:
749                 if ( id == base->e_id ) {
750                         e = base;
751                 } else {
752
753                         /* get the entry */
754                         rs->sr_err = mdb_id2edata( op, mci, id, &edata );
755                         if ( rs->sr_err == MDB_NOTFOUND ) {
756 notfound:
757                                 if( nsubs < ncand )
758                                         goto loop_continue;
759
760                                 if( !MDB_IDL_IS_RANGE(candidates) ) {
761                                         /* only complain for non-range IDLs */
762                                         Debug( LDAP_DEBUG_TRACE,
763                                                 LDAP_XSTRING(mdb_search)
764                                                 ": candidate %ld not found\n",
765                                                 (long) id, 0, 0 );
766                                 } else {
767                                         /* get the next ID from the DB */
768                                         rs->sr_err = mdb_get_nextid( mci, &cursor );
769                                         if ( rs->sr_err == MDB_NOTFOUND ) {
770                                                 break;
771                                         }
772                                         if ( rs->sr_err ) {
773                                                 rs->sr_err = LDAP_OTHER;
774                                                 rs->sr_text = "internal error in get_nextid";
775                                                 send_ldap_result( op, rs );
776                                                 goto done;
777                                         }
778                                         cursor--;
779                                 }
780
781                                 goto loop_continue;
782                         } else if ( rs->sr_err ) {
783                                 rs->sr_err = LDAP_OTHER;
784                                 rs->sr_text = "internal error in mdb_id2edata";
785                                 send_ldap_result( op, rs );
786                                 goto done;
787                         }
788
789                         rs->sr_err = mdb_entry_decode( op, &edata, &e );
790                         if ( rs->sr_err ) {
791                                 rs->sr_err = LDAP_OTHER;
792                                 rs->sr_text = "internal error in mdb_entry_decode";
793                                 send_ldap_result( op, rs );
794                                 goto done;
795                         }
796                         e->e_id = id;
797                         e->e_name.bv_val = NULL;
798                         e->e_nname.bv_val = NULL;
799                 }
800
801                 if ( is_entry_subentry( e ) ) {
802                         if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
803                                 if(!get_subentries_visibility( op )) {
804                                         /* only subentries are visible */
805                                         goto loop_continue;
806                                 }
807
808                         } else if ( get_subentries( op ) &&
809                                 !get_subentries_visibility( op ))
810                         {
811                                 /* only subentries are visible */
812                                 goto loop_continue;
813                         }
814
815                 } else if ( get_subentries_visibility( op )) {
816                         /* only subentries are visible */
817                         goto loop_continue;
818                 }
819
820                 /* aliases were already dereferenced in candidate list */
821                 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
822                         /* but if the search base is an alias, and we didn't
823                          * deref it when finding, return it.
824                          */
825                         if ( is_entry_alias(e) &&
826                                 ((op->ors_deref & LDAP_DEREF_FINDING) || e != base ))
827                         {
828                                 goto loop_continue;
829                         }
830                 }
831
832                 if ( !manageDSAit && is_entry_glue( e )) {
833                         goto loop_continue;
834                 }
835
836                 if (e != base) {
837                         struct berval pdn, pndn;
838                         char *d, *n;
839                         int i;
840
841                         /* child of base, just append RDNs to base->e_name */
842                         if ( nsubs < ncand || isc.nscope == 1 ) {
843                                 pdn = base->e_name;
844                                 pndn = base->e_nname;
845                         } else {
846                                 mdb_id2name( op, ltid, &isc.mc, scopes[isc.nscope].mid, &pdn, &pndn );
847                         }
848                         e->e_name.bv_len = pdn.bv_len;
849                         e->e_nname.bv_len = pndn.bv_len;
850                         for (i=0; i<isc.numrdns; i++) {
851                                 e->e_name.bv_len += isc.rdns[i].bv_len + 1;
852                                 e->e_nname.bv_len += isc.nrdns[i].bv_len + 1;
853                         }
854                         e->e_name.bv_val = op->o_tmpalloc(e->e_name.bv_len + 1, op->o_tmpmemctx);
855                         e->e_nname.bv_val = op->o_tmpalloc(e->e_nname.bv_len + 1, op->o_tmpmemctx);
856                         d = e->e_name.bv_val;
857                         n = e->e_nname.bv_val;
858                         if (nsubs < ncand) {
859                                 /* RDNs are in top-down order */
860                                 for (i=isc.numrdns-1; i>=0; i--) {
861                                         memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
862                                         d += isc.rdns[i].bv_len;
863                                         *d++ = ',';
864                                         memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
865                                         n += isc.nrdns[i].bv_len;
866                                         *n++ = ',';
867                                 }
868                         } else {
869                                 /* RDNs are in bottom-up order */
870                                 for (i=0; i<isc.numrdns; i++) {
871                                         memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
872                                         d += isc.rdns[i].bv_len;
873                                         *d++ = ',';
874                                         memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
875                                         n += isc.nrdns[i].bv_len;
876                                         *n++ = ',';
877                                 }
878                         }
879
880                         if (pdn.bv_len) {
881                                 memcpy(d, pdn.bv_val, pdn.bv_len+1);
882                                 memcpy(n, pndn.bv_val, pndn.bv_len+1);
883                         } else {
884                                 *--d = '\0';
885                                 *--n = '\0';
886                                 e->e_name.bv_len--;
887                                 e->e_nname.bv_len--;
888                         }
889                         if (pndn.bv_val != base->e_nname.bv_val) {
890                                 op->o_tmpfree(pndn.bv_val, op->o_tmpmemctx);
891                                 op->o_tmpfree(pdn.bv_val, op->o_tmpmemctx);
892                         }
893                 }
894
895                 /*
896                  * if it's a referral, add it to the list of referrals. only do
897                  * this for non-base searches, and don't check the filter
898                  * explicitly here since it's only a candidate anyway.
899                  */
900                 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
901                         && is_entry_referral( e ) )
902                 {
903                         BerVarray erefs = get_entry_referrals( op, e );
904                         rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
905                                 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
906                                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
907
908                         rs->sr_entry = e;
909                         rs->sr_flags = 0;
910
911                         send_search_reference( op, rs );
912
913                         mdb_entry_return( op, e );
914                         rs->sr_entry = NULL;
915                         e = NULL;
916
917                         ber_bvarray_free( rs->sr_ref );
918                         ber_bvarray_free( erefs );
919                         rs->sr_ref = NULL;
920
921                         goto loop_continue;
922                 }
923
924                 /* if it matches the filter and scope, send it */
925                 rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
926
927                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
928                         /* check size limit */
929                         if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
930                                 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
931                                         mdb_entry_return( op, e );
932                                         e = NULL;
933                                         send_paged_response( op, rs, &lastid, tentries );
934                                         goto done;
935                                 }
936                                 lastid = id;
937                         }
938
939                         if (e) {
940                                 /* safe default */
941                                 rs->sr_attrs = op->oq_search.rs_attrs;
942                                 rs->sr_operational_attrs = NULL;
943                                 rs->sr_ctrls = NULL;
944                                 rs->sr_entry = e;
945                                 RS_ASSERT( e->e_private != NULL );
946                                 rs->sr_flags = 0;
947                                 rs->sr_err = LDAP_SUCCESS;
948                                 rs->sr_err = send_search_entry( op, rs );
949                                 rs->sr_attrs = NULL;
950                                 rs->sr_entry = NULL;
951                                 if (e != base)
952                                         mdb_entry_return( op, e );
953                                 e = NULL;
954
955                                 switch ( rs->sr_err ) {
956                                 case LDAP_SUCCESS:      /* entry sent ok */
957                                         break;
958                                 default:                /* entry not sent */
959                                         break;
960                                 case LDAP_UNAVAILABLE:
961                                 case LDAP_SIZELIMIT_EXCEEDED:
962                                         if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
963                                                 rs->sr_ref = rs->sr_v2ref;
964                                                 send_ldap_result( op, rs );
965                                                 rs->sr_err = LDAP_SUCCESS;
966
967                                         } else {
968                                                 rs->sr_err = LDAP_OTHER;
969                                         }
970                                         goto done;
971                                 }
972                         }
973
974                 } else {
975                         Debug( LDAP_DEBUG_TRACE,
976                                 LDAP_XSTRING(mdb_search)
977                                 ": %ld does not match filter\n",
978                                 (long) id, 0, 0 );
979                 }
980
981 loop_continue:
982                 if( e != NULL ) {
983                         if ( e != base )
984                                 mdb_entry_return( op, e );
985                         RS_ASSERT( rs->sr_entry == NULL );
986                         e = NULL;
987                         rs->sr_entry = NULL;
988                 }
989
990                 if ( nsubs < ncand ) {
991                         int rc = mdb_dn2id_walk( op, &isc );
992                         if (rc)
993                                 id = NOID;
994                         else
995                                 id = isc.id;
996                 } else {
997                         id = mdb_idl_next( candidates, &cursor );
998                 }
999         }
1000
1001 nochange:
1002         rs->sr_ctrls = NULL;
1003         rs->sr_ref = rs->sr_v2ref;
1004         rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1005         rs->sr_rspoid = NULL;
1006         if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
1007                 send_paged_response( op, rs, NULL, 0 );
1008         } else {
1009                 send_ldap_result( op, rs );
1010         }
1011
1012         rs->sr_err = LDAP_SUCCESS;
1013
1014 done:
1015         mdb_cursor_close( mcd );
1016         mdb_cursor_close( mci );
1017         if ( moi == &opinfo ) {
1018                 mdb_txn_reset( moi->moi_txn );
1019                 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
1020         }
1021         if( rs->sr_v2ref ) {
1022                 ber_bvarray_free( rs->sr_v2ref );
1023                 rs->sr_v2ref = NULL;
1024         }
1025         if (base)
1026                 mdb_entry_return( op,base);
1027         scope_chunk_ret( op, scopes );
1028
1029         return rs->sr_err;
1030 }
1031
1032
1033 static int base_candidate(
1034         BackendDB       *be,
1035         Entry   *e,
1036         ID              *ids )
1037 {
1038         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1039                 e->e_nname.bv_val, (long) e->e_id, 0);
1040
1041         ids[0] = 1;
1042         ids[1] = e->e_id;
1043         return 0;
1044 }
1045
1046 /* Look for "objectClass Present" in this filter.
1047  * Also count depth of filter tree while we're at it.
1048  */
1049 static int oc_filter(
1050         Filter *f,
1051         int cur,
1052         int *max )
1053 {
1054         int rc = 0;
1055
1056         assert( f != NULL );
1057
1058         if( cur > *max ) *max = cur;
1059
1060         switch( f->f_choice ) {
1061         case LDAP_FILTER_PRESENT:
1062                 if (f->f_desc == slap_schema.si_ad_objectClass) {
1063                         rc = 1;
1064                 }
1065                 break;
1066
1067         case LDAP_FILTER_AND:
1068         case LDAP_FILTER_OR:
1069                 cur++;
1070                 for ( f=f->f_and; f; f=f->f_next ) {
1071                         (void) oc_filter(f, cur, max);
1072                 }
1073                 break;
1074
1075         default:
1076                 break;
1077         }
1078         return rc;
1079 }
1080
1081 static void search_stack_free( void *key, void *data )
1082 {
1083         ber_memfree_x(data, NULL);
1084 }
1085
1086 static void *search_stack( Operation *op )
1087 {
1088         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1089         void *ret = NULL;
1090
1091         if ( op->o_threadctx ) {
1092                 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
1093                         &ret, NULL );
1094         } else {
1095                 ret = mdb->mi_search_stack;
1096         }
1097
1098         if ( !ret ) {
1099                 ret = ch_malloc( mdb->mi_search_stack_depth * MDB_IDL_UM_SIZE
1100                         * sizeof( ID ) );
1101                 if ( op->o_threadctx ) {
1102                         ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1103                                 ret, search_stack_free, NULL, NULL );
1104                 } else {
1105                         mdb->mi_search_stack = ret;
1106                 }
1107         }
1108         return ret;
1109 }
1110
1111 static int search_candidates(
1112         Operation *op,
1113         SlapReply *rs,
1114         Entry *e,
1115         MDB_txn *txn,
1116         MDB_cursor *mci,
1117         ID      *ids,
1118         ID2L    scopes )
1119 {
1120         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1121         int rc, depth = 1;
1122         Filter          *f, rf, xf, nf, sf;
1123         ID              *stack;
1124         AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1125         AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1126
1127         /*
1128          * This routine takes as input a filter (user-filter)
1129          * and rewrites it as follows:
1130          *      (&(scope=DN)[(objectClass=subentry)]
1131          *              (|[(objectClass=referral)](user-filter))
1132          */
1133
1134         Debug(LDAP_DEBUG_TRACE,
1135                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1136                 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1137
1138         f = op->oq_search.rs_filter;
1139
1140         /* If the user's filter uses objectClass=*,
1141          * these clauses are redundant.
1142          */
1143         if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1144                 && !get_subentries_visibility(op)) {
1145                 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1146                         /* match referral objects */
1147                         struct berval bv_ref = BER_BVC( "referral" );
1148                         rf.f_choice = LDAP_FILTER_EQUALITY;
1149                         rf.f_ava = &aa_ref;
1150                         rf.f_av_desc = slap_schema.si_ad_objectClass;
1151                         rf.f_av_value = bv_ref;
1152                         rf.f_next = f;
1153                         xf.f_or = &rf;
1154                         xf.f_choice = LDAP_FILTER_OR;
1155                         xf.f_next = NULL;
1156                         f = &xf;
1157                         depth++;
1158                 }
1159         }
1160
1161         if( get_subentries_visibility( op ) ) {
1162                 struct berval bv_subentry = BER_BVC( "subentry" );
1163                 sf.f_choice = LDAP_FILTER_EQUALITY;
1164                 sf.f_ava = &aa_subentry;
1165                 sf.f_av_desc = slap_schema.si_ad_objectClass;
1166                 sf.f_av_value = bv_subentry;
1167                 sf.f_next = f;
1168                 nf.f_choice = LDAP_FILTER_AND;
1169                 nf.f_and = &sf;
1170                 nf.f_next = NULL;
1171                 f = &nf;
1172                 depth++;
1173         }
1174
1175         /* Allocate IDL stack, plus 1 more for former tmp */
1176         if ( depth+1 > mdb->mi_search_stack_depth ) {
1177                 stack = ch_malloc( (depth + 1) * MDB_IDL_UM_SIZE * sizeof( ID ) );
1178         } else {
1179                 stack = search_stack( op );
1180         }
1181
1182         if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1183                 rc = search_aliases( op, rs, e->e_id, txn, mci, scopes, stack );
1184         } else {
1185                 rc = LDAP_SUCCESS;
1186         }
1187
1188         if ( rc == LDAP_SUCCESS ) {
1189                 rc = mdb_filter_candidates( op, txn, f, ids,
1190                         stack, stack+MDB_IDL_UM_SIZE );
1191         }
1192
1193         if ( depth+1 > mdb->mi_search_stack_depth ) {
1194                 ch_free( stack );
1195         }
1196
1197         if( rc ) {
1198                 Debug(LDAP_DEBUG_TRACE,
1199                         "mdb_search_candidates: failed (rc=%d)\n",
1200                         rc, NULL, NULL );
1201
1202         } else {
1203                 Debug(LDAP_DEBUG_TRACE,
1204                         "mdb_search_candidates: id=%ld first=%ld last=%ld\n",
1205                         (long) ids[0],
1206                         (long) MDB_IDL_FIRST(ids),
1207                         (long) MDB_IDL_LAST(ids) );
1208         }
1209
1210         return rc;
1211 }
1212
1213 static int
1214 parse_paged_cookie( Operation *op, SlapReply *rs )
1215 {
1216         int             rc = LDAP_SUCCESS;
1217         PagedResultsState *ps = op->o_pagedresults_state;
1218
1219         /* this function must be invoked only if the pagedResults
1220          * control has been detected, parsed and partially checked
1221          * by the frontend */
1222         assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1223
1224         /* cookie decoding/checks deferred to backend... */
1225         if ( ps->ps_cookieval.bv_len ) {
1226                 PagedResultsCookie reqcookie;
1227                 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1228                         /* bad cookie */
1229                         rs->sr_text = "paged results cookie is invalid";
1230                         rc = LDAP_PROTOCOL_ERROR;
1231                         goto done;
1232                 }
1233
1234                 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1235
1236                 if ( reqcookie > ps->ps_cookie ) {
1237                         /* bad cookie */
1238                         rs->sr_text = "paged results cookie is invalid";
1239                         rc = LDAP_PROTOCOL_ERROR;
1240                         goto done;
1241
1242                 } else if ( reqcookie < ps->ps_cookie ) {
1243                         rs->sr_text = "paged results cookie is invalid or old";
1244                         rc = LDAP_UNWILLING_TO_PERFORM;
1245                         goto done;
1246                 }
1247
1248         } else {
1249                 /* we're going to use ps_cookie */
1250                 op->o_conn->c_pagedresults_state.ps_cookie = 0;
1251         }
1252
1253 done:;
1254
1255         return rc;
1256 }
1257
1258 static void
1259 send_paged_response( 
1260         Operation       *op,
1261         SlapReply       *rs,
1262         ID              *lastid,
1263         int             tentries )
1264 {
1265         LDAPControl     *ctrls[2];
1266         BerElementBuffer berbuf;
1267         BerElement      *ber = (BerElement *)&berbuf;
1268         PagedResultsCookie respcookie;
1269         struct berval cookie;
1270
1271         Debug(LDAP_DEBUG_ARGS,
1272                 "send_paged_response: lastid=0x%08lx nentries=%d\n", 
1273                 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1274
1275         ctrls[1] = NULL;
1276
1277         ber_init2( ber, NULL, LBER_USE_DER );
1278
1279         if ( lastid ) {
1280                 respcookie = ( PagedResultsCookie )(*lastid);
1281                 cookie.bv_len = sizeof( respcookie );
1282                 cookie.bv_val = (char *)&respcookie;
1283
1284         } else {
1285                 respcookie = ( PagedResultsCookie )0;
1286                 BER_BVSTR( &cookie, "" );
1287         }
1288
1289         op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1290         op->o_conn->c_pagedresults_state.ps_count =
1291                 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1292                 rs->sr_nentries;
1293
1294         /* return size of 0 -- no estimate */
1295         ber_printf( ber, "{iO}", 0, &cookie ); 
1296
1297         ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1298         if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1299                 goto done;
1300         }
1301
1302         ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1303         ctrls[0]->ldctl_iscritical = 0;
1304
1305         slap_add_ctrls( op, rs, ctrls );
1306         rs->sr_err = LDAP_SUCCESS;
1307         send_ldap_result( op, rs );
1308
1309 done:
1310         (void) ber_free_buf( ber );
1311 }