]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/search.c
ITS#7473 Setup for subtree entry counts
[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;
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;
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         scopes = scope_chunk_get( op );
359         isc.mt = ltid;
360         isc.mc = NULL;
361         isc.scopes = scopes;
362
363         if ( op->ors_deref & LDAP_DEREF_FINDING ) {
364                 MDB_IDL_ZERO(candidates);
365         }
366 dn2entry_retry:
367         /* get entry with reader lock */
368         rs->sr_err = mdb_dn2entry( op, ltid, NULL, &op->o_req_ndn, &e, &nsubs, 1 );
369
370         switch(rs->sr_err) {
371         case MDB_NOTFOUND:
372                 matched = e;
373                 e = NULL;
374                 break;
375         case 0:
376                 break;
377         case LDAP_BUSY:
378                 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
379                 goto done;
380         default:
381                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
382                 goto done;
383         }
384
385         if ( op->ors_deref & LDAP_DEREF_FINDING ) {
386                 if ( matched && is_entry_alias( matched )) {
387                         struct berval stub;
388
389                         stub.bv_val = op->o_req_ndn.bv_val;
390                         stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
391                         e = deref_base( op, rs, matched, &matched, ltid,
392                                 candidates, NULL );
393                         if ( e ) {
394                                 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
395                                         op->o_tmpmemctx );
396                                 mdb_entry_return(op, e);
397                                 matched = NULL;
398                                 goto dn2entry_retry;
399                         }
400                 } else if ( e && is_entry_alias( e )) {
401                         e = deref_base( op, rs, e, &matched, ltid,
402                                 candidates, NULL );
403                 }
404         }
405
406         if ( e == NULL ) {
407                 struct berval matched_dn = BER_BVNULL;
408
409                 if ( matched != NULL ) {
410                         BerVarray erefs = NULL;
411
412                         /* return referral only if "disclose"
413                          * is granted on the object */
414                         if ( ! access_allowed( op, matched,
415                                                 slap_schema.si_ad_entry,
416                                                 NULL, ACL_DISCLOSE, NULL ) )
417                         {
418                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
419
420                         } else {
421                                 ber_dupbv( &matched_dn, &matched->e_name );
422
423                                 erefs = is_entry_referral( matched )
424                                         ? get_entry_referrals( op, matched )
425                                         : NULL;
426                                 if ( rs->sr_err == MDB_NOTFOUND )
427                                         rs->sr_err = LDAP_REFERRAL;
428                                 rs->sr_matched = matched_dn.bv_val;
429                         }
430
431                         mdb_entry_return(op, matched);
432                         matched = NULL;
433
434                         if ( erefs ) {
435                                 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
436                                         &op->o_req_dn, op->oq_search.rs_scope );
437                                 ber_bvarray_free( erefs );
438                         }
439
440                 } else {
441                         rs->sr_ref = referral_rewrite( default_referral,
442                                 NULL, &op->o_req_dn, op->oq_search.rs_scope );
443                         rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
444                 }
445
446                 send_ldap_result( op, rs );
447
448                 if ( rs->sr_ref ) {
449                         ber_bvarray_free( rs->sr_ref );
450                         rs->sr_ref = NULL;
451                 }
452                 if ( !BER_BVISNULL( &matched_dn ) ) {
453                         ber_memfree( matched_dn.bv_val );
454                         rs->sr_matched = NULL;
455                 }
456                 goto done;
457         }
458
459         /* NOTE: __NEW__ "search" access is required
460          * on searchBase object */
461         if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
462                                 NULL, ACL_SEARCH, NULL, &mask ) )
463         {
464                 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
465                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
466                 } else {
467                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
468                 }
469
470                 mdb_entry_return( op,e);
471                 send_ldap_result( op, rs );
472                 goto done;
473         }
474
475         if ( !manageDSAit && is_entry_referral( e ) ) {
476                 /* entry is a referral */
477                 struct berval matched_dn = BER_BVNULL;
478                 BerVarray erefs = NULL;
479                 
480                 ber_dupbv( &matched_dn, &e->e_name );
481                 erefs = get_entry_referrals( op, e );
482
483                 rs->sr_err = LDAP_REFERRAL;
484
485                 mdb_entry_return( op, e );
486                 e = NULL;
487
488                 if ( erefs ) {
489                         rs->sr_ref = referral_rewrite( erefs, &matched_dn,
490                                 &op->o_req_dn, op->oq_search.rs_scope );
491                         ber_bvarray_free( erefs );
492
493                         if ( !rs->sr_ref ) {
494                                 rs->sr_text = "bad_referral object";
495                         }
496                 }
497
498                 Debug( LDAP_DEBUG_TRACE,
499                         LDAP_XSTRING(mdb_search) ": entry is referral\n",
500                         0, 0, 0 );
501
502                 rs->sr_matched = matched_dn.bv_val;
503                 send_ldap_result( op, rs );
504
505                 ber_bvarray_free( rs->sr_ref );
506                 rs->sr_ref = NULL;
507                 ber_memfree( matched_dn.bv_val );
508                 rs->sr_matched = NULL;
509                 goto done;
510         }
511
512         if ( get_assert( op ) &&
513                 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
514         {
515                 rs->sr_err = LDAP_ASSERTION_FAILED;
516                 mdb_entry_return( op,e);
517                 send_ldap_result( op, rs );
518                 goto done;
519         }
520
521         /* compute it anyway; root does not use it */
522         stoptime = op->o_time + op->ors_tlimit;
523
524         base = e;
525
526         e = NULL;
527
528         /* select candidates */
529         if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
530                 rs->sr_err = base_candidate( op->o_bd, base, candidates );
531
532         } else {
533                 MDB_IDL_ZERO( candidates );
534                 scopes[0].mid = 1;
535                 scopes[1].mid = base->e_id;
536                 scopes[1].mval.mv_data = NULL;
537                 rs->sr_err = search_candidates( op, rs, base,
538                         ltid, mci, candidates, scopes );
539         }
540
541         /* start cursor at beginning of candidates.
542          */
543         cursor = 0;
544
545         if ( candidates[0] == 0 ) {
546                 Debug( LDAP_DEBUG_TRACE,
547                         LDAP_XSTRING(mdb_search) ": no candidates\n",
548                         0, 0, 0 );
549
550                 goto nochange;
551         }
552
553         /* if not root and candidates exceed to-be-checked entries, abort */
554         if ( op->ors_limit      /* isroot == FALSE */ &&
555                 op->ors_limit->lms_s_unchecked != -1 &&
556                 MDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
557         {
558                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
559                 send_ldap_result( op, rs );
560                 rs->sr_err = LDAP_SUCCESS;
561                 goto done;
562         }
563
564         if ( op->ors_limit == NULL      /* isroot == TRUE */ ||
565                 !op->ors_limit->lms_s_pr_hide )
566         {
567                 tentries = MDB_IDL_N(candidates);
568         }
569
570         if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
571                 PagedResultsState *ps = op->o_pagedresults_state;
572                 /* deferred cookie parsing */
573                 rs->sr_err = parse_paged_cookie( op, rs );
574                 if ( rs->sr_err != LDAP_SUCCESS ) {
575                         send_ldap_result( op, rs );
576                         goto done;
577                 }
578
579                 cursor = (ID) ps->ps_cookie;
580                 if ( cursor && ps->ps_size == 0 ) {
581                         rs->sr_err = LDAP_SUCCESS;
582                         rs->sr_text = "search abandoned by pagedResult size=0";
583                         send_ldap_result( op, rs );
584                         goto done;
585                 }
586                 id = mdb_idl_first( candidates, &cursor );
587                 if ( id == NOID ) {
588                         Debug( LDAP_DEBUG_TRACE, 
589                                 LDAP_XSTRING(mdb_search)
590                                 ": no paged results candidates\n",
591                                 0, 0, 0 );
592                         send_paged_response( op, rs, &lastid, 0 );
593
594                         rs->sr_err = LDAP_OTHER;
595                         goto done;
596                 }
597                 if ( id == (ID)ps->ps_cookie )
598                         id = mdb_idl_next( candidates, &cursor );
599                 goto loop_begin;
600         }
601
602         for ( id = mdb_idl_first( candidates, &cursor );
603                   id != NOID ; id = mdb_idl_next( candidates, &cursor ) )
604         {
605                 int scopeok;
606                 MDB_val edata;
607
608 loop_begin:
609
610                 /* check for abandon */
611                 if ( op->o_abandon ) {
612                         rs->sr_err = SLAPD_ABANDON;
613                         send_ldap_result( op, rs );
614                         goto done;
615                 }
616
617                 /* mostly needed by internal searches,
618                  * e.g. related to syncrepl, for whom
619                  * abandon does not get set... */
620                 if ( slapd_shutdown ) {
621                         rs->sr_err = LDAP_UNAVAILABLE;
622                         send_ldap_disconnect( op, rs );
623                         goto done;
624                 }
625
626                 /* check time limit */
627                 if ( op->ors_tlimit != SLAP_NO_LIMIT
628                                 && slap_get_time() > stoptime )
629                 {
630                         rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
631                         rs->sr_ref = rs->sr_v2ref;
632                         send_ldap_result( op, rs );
633                         rs->sr_err = LDAP_SUCCESS;
634                         goto done;
635                 }
636
637
638                 /* Does this candidate actually satisfy the search scope?
639                  */
640                 scopeok = 0;
641                 isc.numrdns = 0;
642                 switch( op->ors_scope ) {
643                 case LDAP_SCOPE_BASE:
644                         /* This is always true, yes? */
645                         if ( id == base->e_id ) scopeok = 1;
646                         break;
647
648 #ifdef LDAP_SCOPE_CHILDREN
649                 case LDAP_SCOPE_CHILDREN:
650                         if ( id == base->e_id ) break;
651                         /* Fall-thru */
652 #endif
653                 case LDAP_SCOPE_SUBTREE:
654                         if ( id == base->e_id ) {
655                                 scopeok = 1;
656                                 break;
657                         }
658                         /* Fall-thru */
659                 case LDAP_SCOPE_ONELEVEL:
660                         isc.id = id;
661                         isc.nscope = 0;
662                         rs->sr_err = mdb_idscopes( op, &isc );
663                         if ( rs->sr_err == MDB_SUCCESS ) {
664                                 if ( isc.nscope )
665                                         scopeok = 1;
666                         } else {
667                                 if ( rs->sr_err == MDB_NOTFOUND )
668                                         goto notfound;
669                         }
670                         break;
671                 }
672
673                 /* Not in scope, ignore it */
674                 if ( !scopeok )
675                 {
676                         Debug( LDAP_DEBUG_TRACE,
677                                 LDAP_XSTRING(mdb_search)
678                                 ": %ld scope not okay\n",
679                                 (long) id, 0, 0 );
680                         goto loop_continue;
681                 }
682
683                 if ( id == base->e_id ) {
684                         e = base;
685                 } else {
686
687                         /* get the entry */
688                         rs->sr_err = mdb_id2edata( op, mci, id, &edata );
689                         if ( rs->sr_err == MDB_NOTFOUND ) {
690 notfound:
691                                 if( !MDB_IDL_IS_RANGE(candidates) ) {
692                                         /* only complain for non-range IDLs */
693                                         Debug( LDAP_DEBUG_TRACE,
694                                                 LDAP_XSTRING(mdb_search)
695                                                 ": candidate %ld not found\n",
696                                                 (long) id, 0, 0 );
697                                 } else {
698                                         /* get the next ID from the DB */
699                                         rs->sr_err = mdb_get_nextid( mci, &cursor );
700                                         if ( rs->sr_err == MDB_NOTFOUND ) {
701                                                 break;
702                                         }
703                                         if ( rs->sr_err ) {
704                                                 rs->sr_err = LDAP_OTHER;
705                                                 rs->sr_text = "internal error in get_nextid";
706                                                 send_ldap_result( op, rs );
707                                                 goto done;
708                                         }
709                                         cursor--;
710                                 }
711
712                                 goto loop_continue;
713                         } else if ( rs->sr_err ) {
714                                 rs->sr_err = LDAP_OTHER;
715                                 rs->sr_text = "internal error in mdb_id2edata";
716                                 send_ldap_result( op, rs );
717                                 goto done;
718                         }
719
720                         rs->sr_err = mdb_entry_decode( op, &edata, &e );
721                         if ( rs->sr_err ) {
722                                 rs->sr_err = LDAP_OTHER;
723                                 rs->sr_text = "internal error in mdb_entry_decode";
724                                 send_ldap_result( op, rs );
725                                 goto done;
726                         }
727                         e->e_id = id;
728                         e->e_name.bv_val = NULL;
729                         e->e_nname.bv_val = NULL;
730                 }
731
732                 if ( is_entry_subentry( e ) ) {
733                         if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
734                                 if(!get_subentries_visibility( op )) {
735                                         /* only subentries are visible */
736                                         goto loop_continue;
737                                 }
738
739                         } else if ( get_subentries( op ) &&
740                                 !get_subentries_visibility( op ))
741                         {
742                                 /* only subentries are visible */
743                                 goto loop_continue;
744                         }
745
746                 } else if ( get_subentries_visibility( op )) {
747                         /* only subentries are visible */
748                         goto loop_continue;
749                 }
750
751                 /* aliases were already dereferenced in candidate list */
752                 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
753                         /* but if the search base is an alias, and we didn't
754                          * deref it when finding, return it.
755                          */
756                         if ( is_entry_alias(e) &&
757                                 ((op->ors_deref & LDAP_DEREF_FINDING) ||
758                                         !bvmatch(&e->e_nname, &op->o_req_ndn)))
759                         {
760                                 goto loop_continue;
761                         }
762                 }
763
764                 if ( !manageDSAit && is_entry_glue( e )) {
765                         goto loop_continue;
766                 }
767
768                 if (e != base) {
769                         struct berval pdn, pndn;
770                         char *d, *n;
771                         int i;
772                         /* child of base, just append RDNs to base->e_name */
773                         if ( isc.nscope == 1 ) {
774                                 pdn = base->e_name;
775                                 pndn = base->e_nname;
776                         } else {
777                                 mdb_id2name( op, ltid, &isc.mc, scopes[isc.nscope].mid, &pdn, &pndn );
778                         }
779                         e->e_name.bv_len = pdn.bv_len;
780                         e->e_nname.bv_len = pndn.bv_len;
781                         for (i=0; i<isc.numrdns; i++) {
782                                 e->e_name.bv_len += isc.rdns[i].bv_len + 1;
783                                 e->e_nname.bv_len += isc.nrdns[i].bv_len + 1;
784                         }
785                         e->e_name.bv_val = op->o_tmpalloc(e->e_name.bv_len + 1, op->o_tmpmemctx);
786                         e->e_nname.bv_val = op->o_tmpalloc(e->e_nname.bv_len + 1, op->o_tmpmemctx);
787                         d = e->e_name.bv_val;
788                         n = e->e_nname.bv_val;
789                         for (i=0; i<isc.numrdns; i++) {
790                                 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
791                                 d += isc.rdns[i].bv_len;
792                                 *d++ = ',';
793                                 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
794                                 n += isc.nrdns[i].bv_len;
795                                 *n++ = ',';
796                         }
797                         if (pdn.bv_len) {
798                                 memcpy(d, pdn.bv_val, pdn.bv_len+1);
799                                 memcpy(n, pndn.bv_val, pndn.bv_len+1);
800                         } else {
801                                 *--d = '\0';
802                                 *--n = '\0';
803                                 e->e_name.bv_len--;
804                                 e->e_nname.bv_len--;
805                         }
806                         if (isc.nscope != 1) {
807                                 op->o_tmpfree(pndn.bv_val, op->o_tmpmemctx);
808                                 op->o_tmpfree(pdn.bv_val, op->o_tmpmemctx);
809                         }
810                 }
811
812                 /*
813                  * if it's a referral, add it to the list of referrals. only do
814                  * this for non-base searches, and don't check the filter
815                  * explicitly here since it's only a candidate anyway.
816                  */
817                 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
818                         && is_entry_referral( e ) )
819                 {
820                         BerVarray erefs = get_entry_referrals( op, e );
821                         rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
822                                 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
823                                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
824
825                         rs->sr_entry = e;
826                         rs->sr_flags = 0;
827
828                         send_search_reference( op, rs );
829
830                         mdb_entry_return( op, e );
831                         rs->sr_entry = NULL;
832                         e = NULL;
833
834                         ber_bvarray_free( rs->sr_ref );
835                         ber_bvarray_free( erefs );
836                         rs->sr_ref = NULL;
837
838                         goto loop_continue;
839                 }
840
841                 /* if it matches the filter and scope, send it */
842                 rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
843
844                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
845                         /* check size limit */
846                         if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
847                                 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
848                                         mdb_entry_return( op, e );
849                                         e = NULL;
850                                         send_paged_response( op, rs, &lastid, tentries );
851                                         goto done;
852                                 }
853                                 lastid = id;
854                         }
855
856                         if (e) {
857                                 /* safe default */
858                                 rs->sr_attrs = op->oq_search.rs_attrs;
859                                 rs->sr_operational_attrs = NULL;
860                                 rs->sr_ctrls = NULL;
861                                 rs->sr_entry = e;
862                                 RS_ASSERT( e->e_private != NULL );
863                                 rs->sr_flags = 0;
864                                 rs->sr_err = LDAP_SUCCESS;
865                                 rs->sr_err = send_search_entry( op, rs );
866                                 rs->sr_attrs = NULL;
867                                 rs->sr_entry = NULL;
868                                 if (e != base)
869                                         mdb_entry_return( op, e );
870                                 e = NULL;
871
872                                 switch ( rs->sr_err ) {
873                                 case LDAP_SUCCESS:      /* entry sent ok */
874                                         break;
875                                 default:                /* entry not sent */
876                                         break;
877                                 case LDAP_UNAVAILABLE:
878                                 case LDAP_SIZELIMIT_EXCEEDED:
879                                         if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
880                                                 rs->sr_ref = rs->sr_v2ref;
881                                                 send_ldap_result( op, rs );
882                                                 rs->sr_err = LDAP_SUCCESS;
883
884                                         } else {
885                                                 rs->sr_err = LDAP_OTHER;
886                                         }
887                                         goto done;
888                                 }
889                         }
890
891                 } else {
892                         Debug( LDAP_DEBUG_TRACE,
893                                 LDAP_XSTRING(mdb_search)
894                                 ": %ld does not match filter\n",
895                                 (long) id, 0, 0 );
896                 }
897
898 loop_continue:
899                 if( e != NULL ) {
900                         if ( e != base )
901                                 mdb_entry_return( op, e );
902                         RS_ASSERT( rs->sr_entry == NULL );
903                         e = NULL;
904                         rs->sr_entry = NULL;
905                 }
906         }
907
908 nochange:
909         rs->sr_ctrls = NULL;
910         rs->sr_ref = rs->sr_v2ref;
911         rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
912         rs->sr_rspoid = NULL;
913         if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
914                 send_paged_response( op, rs, NULL, 0 );
915         } else {
916                 send_ldap_result( op, rs );
917         }
918
919         rs->sr_err = LDAP_SUCCESS;
920
921 done:
922         if( isc.mc )
923                 mdb_cursor_close( isc.mc );
924         if (mci)
925                 mdb_cursor_close( mci );
926         if ( moi == &opinfo ) {
927                 mdb_txn_reset( moi->moi_txn );
928                 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
929         }
930         if( rs->sr_v2ref ) {
931                 ber_bvarray_free( rs->sr_v2ref );
932                 rs->sr_v2ref = NULL;
933         }
934         if (base)
935                 mdb_entry_return( op,base);
936         scope_chunk_ret( op, scopes );
937
938         return rs->sr_err;
939 }
940
941
942 static int base_candidate(
943         BackendDB       *be,
944         Entry   *e,
945         ID              *ids )
946 {
947         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
948                 e->e_nname.bv_val, (long) e->e_id, 0);
949
950         ids[0] = 1;
951         ids[1] = e->e_id;
952         return 0;
953 }
954
955 /* Look for "objectClass Present" in this filter.
956  * Also count depth of filter tree while we're at it.
957  */
958 static int oc_filter(
959         Filter *f,
960         int cur,
961         int *max )
962 {
963         int rc = 0;
964
965         assert( f != NULL );
966
967         if( cur > *max ) *max = cur;
968
969         switch( f->f_choice ) {
970         case LDAP_FILTER_PRESENT:
971                 if (f->f_desc == slap_schema.si_ad_objectClass) {
972                         rc = 1;
973                 }
974                 break;
975
976         case LDAP_FILTER_AND:
977         case LDAP_FILTER_OR:
978                 cur++;
979                 for ( f=f->f_and; f; f=f->f_next ) {
980                         (void) oc_filter(f, cur, max);
981                 }
982                 break;
983
984         default:
985                 break;
986         }
987         return rc;
988 }
989
990 static void search_stack_free( void *key, void *data )
991 {
992         ber_memfree_x(data, NULL);
993 }
994
995 static void *search_stack( Operation *op )
996 {
997         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
998         void *ret = NULL;
999
1000         if ( op->o_threadctx ) {
1001                 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
1002                         &ret, NULL );
1003         } else {
1004                 ret = mdb->mi_search_stack;
1005         }
1006
1007         if ( !ret ) {
1008                 ret = ch_malloc( mdb->mi_search_stack_depth * MDB_IDL_UM_SIZE
1009                         * sizeof( ID ) );
1010                 if ( op->o_threadctx ) {
1011                         ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1012                                 ret, search_stack_free, NULL, NULL );
1013                 } else {
1014                         mdb->mi_search_stack = ret;
1015                 }
1016         }
1017         return ret;
1018 }
1019
1020 static int search_candidates(
1021         Operation *op,
1022         SlapReply *rs,
1023         Entry *e,
1024         MDB_txn *txn,
1025         MDB_cursor *mci,
1026         ID      *ids,
1027         ID2L    scopes )
1028 {
1029         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1030         int rc, depth = 1;
1031         Filter          *f, rf, xf, nf, sf;
1032         ID              *stack;
1033         AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1034         AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1035
1036         /*
1037          * This routine takes as input a filter (user-filter)
1038          * and rewrites it as follows:
1039          *      (&(scope=DN)[(objectClass=subentry)]
1040          *              (|[(objectClass=referral)](user-filter))
1041          */
1042
1043         Debug(LDAP_DEBUG_TRACE,
1044                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1045                 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1046
1047         f = op->oq_search.rs_filter;
1048
1049         /* If the user's filter uses objectClass=*,
1050          * these clauses are redundant.
1051          */
1052         if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1053                 && !get_subentries_visibility(op)) {
1054                 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1055                         /* match referral objects */
1056                         struct berval bv_ref = BER_BVC( "referral" );
1057                         rf.f_choice = LDAP_FILTER_EQUALITY;
1058                         rf.f_ava = &aa_ref;
1059                         rf.f_av_desc = slap_schema.si_ad_objectClass;
1060                         rf.f_av_value = bv_ref;
1061                         rf.f_next = f;
1062                         xf.f_or = &rf;
1063                         xf.f_choice = LDAP_FILTER_OR;
1064                         xf.f_next = NULL;
1065                         f = &xf;
1066                         depth++;
1067                 }
1068         }
1069
1070         if( get_subentries_visibility( op ) ) {
1071                 struct berval bv_subentry = BER_BVC( "subentry" );
1072                 sf.f_choice = LDAP_FILTER_EQUALITY;
1073                 sf.f_ava = &aa_subentry;
1074                 sf.f_av_desc = slap_schema.si_ad_objectClass;
1075                 sf.f_av_value = bv_subentry;
1076                 sf.f_next = f;
1077                 nf.f_choice = LDAP_FILTER_AND;
1078                 nf.f_and = &sf;
1079                 nf.f_next = NULL;
1080                 f = &nf;
1081                 depth++;
1082         }
1083
1084         /* Allocate IDL stack, plus 1 more for former tmp */
1085         if ( depth+1 > mdb->mi_search_stack_depth ) {
1086                 stack = ch_malloc( (depth + 1) * MDB_IDL_UM_SIZE * sizeof( ID ) );
1087         } else {
1088                 stack = search_stack( op );
1089         }
1090
1091         if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1092                 rc = search_aliases( op, rs, e->e_id, txn, mci, scopes, stack );
1093         } else {
1094                 rc = LDAP_SUCCESS;
1095         }
1096
1097         if ( rc == LDAP_SUCCESS ) {
1098                 rc = mdb_filter_candidates( op, txn, f, ids,
1099                         stack, stack+MDB_IDL_UM_SIZE );
1100         }
1101
1102         if ( depth+1 > mdb->mi_search_stack_depth ) {
1103                 ch_free( stack );
1104         }
1105
1106         if( rc ) {
1107                 Debug(LDAP_DEBUG_TRACE,
1108                         "mdb_search_candidates: failed (rc=%d)\n",
1109                         rc, NULL, NULL );
1110
1111         } else {
1112                 Debug(LDAP_DEBUG_TRACE,
1113                         "mdb_search_candidates: id=%ld first=%ld last=%ld\n",
1114                         (long) ids[0],
1115                         (long) MDB_IDL_FIRST(ids),
1116                         (long) MDB_IDL_LAST(ids) );
1117         }
1118
1119         return rc;
1120 }
1121
1122 static int
1123 parse_paged_cookie( Operation *op, SlapReply *rs )
1124 {
1125         int             rc = LDAP_SUCCESS;
1126         PagedResultsState *ps = op->o_pagedresults_state;
1127
1128         /* this function must be invoked only if the pagedResults
1129          * control has been detected, parsed and partially checked
1130          * by the frontend */
1131         assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1132
1133         /* cookie decoding/checks deferred to backend... */
1134         if ( ps->ps_cookieval.bv_len ) {
1135                 PagedResultsCookie reqcookie;
1136                 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1137                         /* bad cookie */
1138                         rs->sr_text = "paged results cookie is invalid";
1139                         rc = LDAP_PROTOCOL_ERROR;
1140                         goto done;
1141                 }
1142
1143                 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1144
1145                 if ( reqcookie > ps->ps_cookie ) {
1146                         /* bad cookie */
1147                         rs->sr_text = "paged results cookie is invalid";
1148                         rc = LDAP_PROTOCOL_ERROR;
1149                         goto done;
1150
1151                 } else if ( reqcookie < ps->ps_cookie ) {
1152                         rs->sr_text = "paged results cookie is invalid or old";
1153                         rc = LDAP_UNWILLING_TO_PERFORM;
1154                         goto done;
1155                 }
1156
1157         } else {
1158                 /* we're going to use ps_cookie */
1159                 op->o_conn->c_pagedresults_state.ps_cookie = 0;
1160         }
1161
1162 done:;
1163
1164         return rc;
1165 }
1166
1167 static void
1168 send_paged_response( 
1169         Operation       *op,
1170         SlapReply       *rs,
1171         ID              *lastid,
1172         int             tentries )
1173 {
1174         LDAPControl     *ctrls[2];
1175         BerElementBuffer berbuf;
1176         BerElement      *ber = (BerElement *)&berbuf;
1177         PagedResultsCookie respcookie;
1178         struct berval cookie;
1179
1180         Debug(LDAP_DEBUG_ARGS,
1181                 "send_paged_response: lastid=0x%08lx nentries=%d\n", 
1182                 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1183
1184         ctrls[1] = NULL;
1185
1186         ber_init2( ber, NULL, LBER_USE_DER );
1187
1188         if ( lastid ) {
1189                 respcookie = ( PagedResultsCookie )(*lastid);
1190                 cookie.bv_len = sizeof( respcookie );
1191                 cookie.bv_val = (char *)&respcookie;
1192
1193         } else {
1194                 respcookie = ( PagedResultsCookie )0;
1195                 BER_BVSTR( &cookie, "" );
1196         }
1197
1198         op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1199         op->o_conn->c_pagedresults_state.ps_count =
1200                 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1201                 rs->sr_nentries;
1202
1203         /* return size of 0 -- no estimate */
1204         ber_printf( ber, "{iO}", 0, &cookie ); 
1205
1206         ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1207         if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1208                 goto done;
1209         }
1210
1211         ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1212         ctrls[0]->ldctl_iscritical = 0;
1213
1214         slap_add_ctrls( op, rs, ctrls );
1215         rs->sr_err = LDAP_SUCCESS;
1216         send_ldap_result( op, rs );
1217
1218 done:
1219         (void) ber_free_buf( ber );
1220 }