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