Debian Codegraph @

servers/slapd/back-mdb/search.c

raw
1 /* search.c - search operation */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2000-2024 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 IdScopes *isc,
35 MDB_cursor *mci,
36 ID *ids,
37 ID *stack );
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 IdScopes *isc,
134 MDB_cursor *mci,
135 ID *stack )
136 {
137 ID *aliases, *curscop, *visited, *newsubs, *oldsubs, *tmp;
138 ID cursora, ida, cursoro, ido;
139 Entry *matched, *a;
140 struct berval bv_alias = BER_BVC( "alias" );
141 AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
142 Filter af;
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, isc->mt, &af, aliases,
160 curscop, visited );
161 if (rs->sr_err != LDAP_SUCCESS || MDB_IDL_IS_ZERO( aliases )) {
162 return rs->sr_err;
163 }
164 if ( op->ors_limit /* isroot == FALSE */ &&
165 op->ors_limit->lms_s_unchecked != -1 &&
166 MDB_IDL_N( aliases ) > (unsigned) op->ors_limit->lms_s_unchecked )
167 {
168 return LDAP_ADMINLIMIT_EXCEEDED;
169 }
170 oldsubs[0] = 1;
171 oldsubs[1] = e_id;
172
173 MDB_IDL_ZERO( visited );
174 MDB_IDL_ZERO( newsubs );
175
176 cursoro = 0;
177 ido = mdb_idl_first( oldsubs, &cursoro );
178
179 for (;;) {
180 /* Set curscop to only the aliases in the current scope. Start with
181 * all the aliases, then get the intersection with the scope.
182 */
183 rs->sr_err = mdb_idscope( op, isc->mt, e_id, aliases, curscop );
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, mci, 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, isc->mt,
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 ID2 mid;
212 mid.mid = a->e_id;
213 mid.mval.mv_data = NULL;
214 if (op->ors_scope == LDAP_SCOPE_SUBTREE) {
215 isc->id = a->e_id;
216 /* if ID is a child of any of our current scopes,
217 * ignore it, it's already included.
218 */
219 if (mdb_idscopechk(op, isc))
220 goto skip;
221 }
222 if (mdb_id2l_insert(isc->scopes, &mid) == 0) {
223 mdb_idl_insert(newsubs, a->e_id);
224 }
225 skip: mdb_entry_return( op, a );
226
227 } else if (matched) {
228 /* Alias could not be dereferenced, or it deref'd to
229 * an ID we've already seen. Ignore it.
230 */
231 mdb_entry_return( op, matched );
232 rs->sr_text = NULL;
233 rs->sr_err = 0;
234 }
235 }
236 /* If this is a OneLevel search, we're done; oldsubs only had one
237 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
238 */
239 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
240 nextido:
241 ido = mdb_idl_next( oldsubs, &cursoro );
242
243 /* If we're done processing the old scopes, did we add any new
244 * scopes in this iteration? If so, go back and do those now.
245 */
246 if (ido == NOID) {
247 if (MDB_IDL_IS_ZERO(newsubs)) break;
248 MDB_IDL_CPY(oldsubs, newsubs);
249 MDB_IDL_ZERO(newsubs);
250 cursoro = 0;
251 ido = mdb_idl_first( oldsubs, &cursoro );
252 }
253
254 /* Find the entry corresponding to the next scope. If it can't
255 * be found, ignore it and move on. This should never happen;
256 * we should never see the ID of an entry that doesn't exist.
257 */
258 {
259 MDB_val edata;
260 rs->sr_err = mdb_id2edata(op, mci, ido, &edata);
261 if ( rs->sr_err != MDB_SUCCESS ) {
262 goto nextido;
263 }
264 e_id = ido;
265 }
266 }
267 return rs->sr_err;
268 }
269
270 /* Get the next ID from the DB. Used if the candidate list is
271 * a range and simple iteration hits missing entryIDs
272 */
273 static int
274 mdb_get_nextid(MDB_cursor *mci, ID *cursor)
275 {
276 MDB_val key;
277 ID id;
278 int rc;
279
280 id = *cursor + 1;
281 key.mv_data = &id;
282 key.mv_size = sizeof(ID);
283 rc = mdb_cursor_get( mci, &key, NULL, MDB_SET_RANGE );
284 if ( rc )
285 return rc;
286 memcpy( cursor, key.mv_data, sizeof(ID));
287 return 0;
288 }
289
290 static void scope_chunk_free( void *key, void *data )
291 {
292 ID2 *p1, *p2;
293 for (p1 = data; p1; p1 = p2) {
294 p2 = p1[0].mval.mv_data;
295 ber_memfree_x(p1, NULL);
296 }
297 }
298
299 static ID2 *scope_chunk_get( Operation *op )
300 {
301 ID2 *ret = NULL;
302
303 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
304 (void *)&ret, NULL );
305 if ( !ret ) {
306 ret = ch_malloc( MDB_idl_um_size * sizeof( ID2 ));
307 } else {
308 void *r2 = ret[0].mval.mv_data;
309 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
310 r2, scope_chunk_free, NULL, NULL );
311 }
312 return ret;
313 }
314
315 static void scope_chunk_ret( Operation *op, ID2 *scopes )
316 {
317 void *ret = NULL;
318
319 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
320 &ret, NULL );
321 scopes[0].mval.mv_data = ret;
322 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
323 (void *)scopes, scope_chunk_free, NULL, NULL );
324 }
325
326 static void *search_stack( Operation *op );
327
328 typedef struct ww_ctx {
329 MDB_txn *txn;
330 MDB_cursor *mcd; /* if set, save cursor context */
331 ID key;
332 MDB_val data;
333 int flag;
334 unsigned nentries;
335 } ww_ctx;
336
337 /* ITS#7904 if we get blocked while writing results to client,
338 * release the current reader txn and reacquire it after we
339 * unblock.
340 * Slight problem - if we're doing a scope-based walk (mdb_dn2id_walk)
341 * to return results, we need to remember the state of the mcd cursor.
342 * If the node that cursor was pointing to gets deleted while we're
343 * blocked, we may be unable to restore the cursor position. In that
344 * case return an LDAP_BUSY error - let the client know this search
345 * couldn't succeed, but might succeed on a retry.
346 */
347 static void
348 mdb_rtxn_snap( Operation *op, ww_ctx *ww )
349 {
350 /* save cursor position and release read txn */
351 if ( ww->mcd ) {
352 MDB_val key, data;
353 mdb_cursor_get( ww->mcd, &key, &data, MDB_GET_CURRENT );
354 memcpy( &ww->key, key.mv_data, sizeof(ID) );
355 ww->data.mv_size = data.mv_size;
356 ww->data.mv_data = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
357 memcpy(ww->data.mv_data, data.mv_data, data.mv_size);
358 }
359 mdb_txn_reset( ww->txn );
360 ww->flag = 1;
361 }
362
363 static void
364 mdb_writewait( Operation *op, slap_callback *sc )
365 {
366 ww_ctx *ww = sc->sc_private;
367 if ( !ww->flag ) {
368 mdb_rtxn_snap( op, ww );
369 }
370 }
371
372 static int
373 mdb_waitfixup( Operation *op, ww_ctx *ww, MDB_cursor *mci, MDB_cursor *mcd, IdScopes *isc )
374 {
375 MDB_val key;
376 int rc = 0;
377 ww->flag = 0;
378 mdb_txn_renew( ww->txn );
379 mdb_cursor_renew( ww->txn, mci );
380 mdb_cursor_renew( ww->txn, mcd );
381
382 key.mv_size = sizeof(ID);
383 if ( ww->mcd ) { /* scope-based search using dn2id_walk */
384 MDB_val data;
385
386 if ( isc->numrdns )
387 mdb_dn2id_wrestore( op, isc );
388
389 key.mv_data = &ww->key;
390 data = ww->data;
391 rc = mdb_cursor_get( mcd, &key, &data, MDB_GET_BOTH );
392 if ( rc == MDB_NOTFOUND ) {
393 data = ww->data;
394 rc = mdb_cursor_get( mcd, &key, &data, MDB_GET_BOTH_RANGE );
395 /* the loop will skip this node using NEXT_DUP but we want it
396 * sent, so go back one space first
397 */
398 if ( rc == MDB_SUCCESS )
399 mdb_cursor_get( mcd, &key, &data, MDB_PREV_DUP );
400 else
401 rc = LDAP_BUSY;
402 } else if ( rc ) {
403 rc = LDAP_OTHER;
404 }
405 op->o_tmpfree( ww->data.mv_data, op->o_tmpmemctx );
406 ww->data.mv_data = NULL;
407 } else if ( isc->scopes[0].mid > 1 ) { /* candidate-based search */
408 int i;
409 for ( i=1; i<=isc->scopes[0].mid; i++ ) {
410 if ( !isc->scopes[i].mval.mv_data )
411 continue;
412 key.mv_data = &isc->scopes[i].mid;
413 mdb_cursor_get( mcd, &key, &isc->scopes[i].mval, MDB_SET );
414 }
415 }
416 return rc;
417 }
418
419 int
420 mdb_search( Operation *op, SlapReply *rs )
421 {
422 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
423 ID id, cursor, nsubs, ncand, cscope;
424 ID lastid = NOID;
425 ID *candidates, *iscopes, *c0;
426 ID2 *scopes;
427 void *stack;
428 Entry *e = NULL, *base = NULL;
429 Entry *matched = NULL;
430 AttributeName *attrs;
431 slap_mask_t mask;
432 time_t stoptime;
433 int manageDSAit;
434 int tentries = 0;
435 int admincheck = 0;
436 IdScopes isc;
437 MDB_cursor *mci, *mcd;
438 ww_ctx wwctx;
439 slap_callback cb = { 0 };
440
441 mdb_op_info opinfo = {{{0}}}, *moi = &opinfo;
442 MDB_txn *ltid = NULL;
443
444 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_search) "\n" );
445 attrs = op->oq_search.rs_attrs;
446
447 manageDSAit = get_manageDSAit( op );
448
449 rs->sr_err = mdb_opinfo_get( op, mdb, 1, &moi );
450 switch(rs->sr_err) {
451 case 0:
452 break;
453 default:
454 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
455 return rs->sr_err;
456 }
457
458 ltid = moi->moi_txn;
459
460 rs->sr_err = mdb_cursor_open( ltid, mdb->mi_id2entry, &mci );
461 if ( rs->sr_err ) {
462 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
463 return rs->sr_err;
464 }
465
466 rs->sr_err = mdb_cursor_open( ltid, mdb->mi_dn2id, &mcd );
467 if ( rs->sr_err ) {
468 mdb_cursor_close( mci );
469 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
470 return rs->sr_err;
471 }
472
473 scopes = scope_chunk_get( op );
474 candidates = c0 = search_stack( op );
475 iscopes = candidates + MDB_idl_um_size;
476 stack = iscopes + MDB_idl_db_size;
477 /* if candidates already in use, alloc a new array */
478 if ( c0[0] ) {
479 candidates = ch_malloc(( MDB_idl_um_size + MDB_idl_db_size ) * sizeof ( ID ));
480 iscopes = candidates + MDB_idl_um_size;
481 }
482 isc.mt = ltid;
483 isc.mc = mcd;
484 isc.scopes = scopes;
485 isc.oscope = op->ors_scope;
486 isc.sctmp = stack;
487
488 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
489 MDB_IDL_ZERO(candidates);
490 }
491 dn2entry_retry:
492 /* get entry with reader lock */
493 rs->sr_err = mdb_dn2entry( op, ltid, mcd, &op->o_req_ndn, &e, &nsubs, 1 );
494
495 switch(rs->sr_err) {
496 case MDB_NOTFOUND:
497 matched = e;
498 e = NULL;
499 break;
500 case 0:
501 break;
502 case LDAP_BUSY:
503 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
504 goto done;
505 default:
506 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
507 goto done;
508 }
509
510 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
511 if ( matched && is_entry_alias( matched )) {
512 struct berval stub;
513
514 stub.bv_val = op->o_req_ndn.bv_val;
515 stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
516 e = deref_base( op, rs, matched, &matched, ltid,
517 candidates, NULL );
518 if ( e ) {
519 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
520 op->o_tmpmemctx );
521 mdb_entry_return(op, e);
522 matched = NULL;
523 goto dn2entry_retry;
524 }
525 } else if ( e && is_entry_alias( e )) {
526 e = deref_base( op, rs, e, &matched, ltid,
527 candidates, NULL );
528 }
529 }
530
531 if ( e == NULL ) {
532 struct berval matched_dn = BER_BVNULL;
533
534 if ( matched != NULL ) {
535 BerVarray erefs = NULL;
536
537 /* return referral only if "disclose"
538 * is granted on the object */
539 if ( ! access_allowed( op, matched,
540 slap_schema.si_ad_entry,
541 NULL, ACL_DISCLOSE, NULL ) )
542 {
543 rs->sr_err = LDAP_NO_SUCH_OBJECT;
544
545 } else {
546 ber_dupbv( &matched_dn, &matched->e_name );
547
548 erefs = is_entry_referral( matched )
549 ? get_entry_referrals( op, matched )
550 : NULL;
551 if ( rs->sr_err == MDB_NOTFOUND )
552 rs->sr_err = LDAP_REFERRAL;
553 rs->sr_matched = matched_dn.bv_val;
554 }
555
556 mdb_entry_return(op, matched);
557 matched = NULL;
558
559 if ( erefs ) {
560 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
561 &op->o_req_dn, op->oq_search.rs_scope );
562 ber_bvarray_free( erefs );
563 }
564
565 } else {
566 rs->sr_ref = referral_rewrite( default_referral,
567 NULL, &op->o_req_dn, op->oq_search.rs_scope );
568 rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
569 }
570
571 send_ldap_result( op, rs );
572
573 if ( rs->sr_ref ) {
574 ber_bvarray_free( rs->sr_ref );
575 rs->sr_ref = NULL;
576 }
577 if ( !BER_BVISNULL( &matched_dn ) ) {
578 ber_memfree( matched_dn.bv_val );
579 rs->sr_matched = NULL;
580 }
581 goto done;
582 }
583
584 /* NOTE: __NEW__ "search" access is required
585 * on searchBase object */
586 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
587 NULL, ACL_SEARCH, NULL, &mask ) )
588 {
589 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
590 rs->sr_err = LDAP_NO_SUCH_OBJECT;
591 } else {
592 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
593 }
594
595 mdb_entry_return( op,e);
596 send_ldap_result( op, rs );
597 goto done;
598 }
599
600 if ( !manageDSAit && is_entry_referral( e ) ) {
601 /* entry is a referral */
602 struct berval matched_dn = BER_BVNULL;
603 BerVarray erefs = NULL;
604
605 ber_dupbv( &matched_dn, &e->e_name );
606 erefs = get_entry_referrals( op, e );
607
608 rs->sr_err = LDAP_REFERRAL;
609
610 mdb_entry_return( op, e );
611 e = NULL;
612
613 if ( erefs ) {
614 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
615 &op->o_req_dn, op->oq_search.rs_scope );
616 ber_bvarray_free( erefs );
617
618 if ( !rs->sr_ref ) {
619 rs->sr_text = "bad_referral object";
620 }
621 }
622
623 Debug( LDAP_DEBUG_TRACE,
624 LDAP_XSTRING(mdb_search) ": entry is referral\n" );
625
626 rs->sr_matched = matched_dn.bv_val;
627 send_ldap_result( op, rs );
628
629 ber_bvarray_free( rs->sr_ref );
630 rs->sr_ref = NULL;
631 ber_memfree( matched_dn.bv_val );
632 rs->sr_matched = NULL;
633 goto done;
634 }
635
636 if ( get_assert( op ) &&
637 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
638 {
639 rs->sr_err = LDAP_ASSERTION_FAILED;
640 mdb_entry_return( op,e);
641 send_ldap_result( op, rs );
642 goto done;
643 }
644
645 /* compute it anyway; root does not use it */
646 stoptime = op->o_time + op->ors_tlimit;
647
648 base = e;
649
650 e = NULL;
651
652 /* select candidates */
653 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
654 rs->sr_err = base_candidate( op->o_bd, base, candidates );
655 scopes[0].mid = 0;
656 ncand = 1;
657 } else {
658 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
659 size_t nkids;
660 MDB_val key, data;
661 key.mv_data = &base->e_id;
662 key.mv_size = sizeof( ID );
663 mdb_cursor_get( mcd, &key, &data, MDB_SET );
664 mdb_cursor_count( mcd, &nkids );
665 nsubs = nkids - 1;
666 } else if ( !base->e_id ) {
667 /* we don't maintain nsubs for entryID 0.
668 * just grab entry count from id2entry stat
669 */
670 MDB_stat ms;
671 mdb_stat( ltid, mdb->mi_id2entry, &ms );
672 nsubs = ms.ms_entries;
673 }
674 MDB_IDL_ZERO( candidates );
675 scopes[0].mid = 1;
676 scopes[1].mid = base->e_id;
677 scopes[1].mval.mv_data = NULL;
678 rs->sr_err = search_candidates( op, rs, base,
679 &isc, mci, candidates, stack );
680
681 if ( rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ) {
682 adminlimit:
683 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
684 send_ldap_result( op, rs );
685 rs->sr_err = LDAP_SUCCESS;
686 goto done;
687 }
688
689 ncand = MDB_IDL_N( candidates );
690 if ( !base->e_id || ncand == NOID ) {
691 /* grab entry count from id2entry stat
692 */
693 MDB_stat ms;
694 mdb_stat( ltid, mdb->mi_id2entry, &ms );
695 if ( !base->e_id )
696 nsubs = ms.ms_entries;
697 if ( ncand == NOID )
698 ncand = ms.ms_entries;
699 }
700 }
701
702 /* start cursor at beginning of candidates.
703 */
704 cursor = 0;
705
706 if ( candidates[0] == 0 ) {
707 Debug( LDAP_DEBUG_TRACE,
708 LDAP_XSTRING(mdb_search) ": no candidates\n" );
709
710 goto nochange;
711 }
712
713 /* if not root and candidates exceed to-be-checked entries, abort */
714 if ( op->ors_limit /* isroot == FALSE */ &&
715 op->ors_limit->lms_s_unchecked != -1 &&
716 ncand > (unsigned) op->ors_limit->lms_s_unchecked )
717 {
718 admincheck = 1;
719 }
720
721 if ( op->ors_limit == NULL /* isroot == TRUE */ ||
722 !op->ors_limit->lms_s_pr_hide )
723 {
724 tentries = ncand;
725 }
726
727 wwctx.flag = 0;
728 wwctx.nentries = 0;
729 /* If we're running in our own read txn */
730 if ( moi == &opinfo ) {
731 cb.sc_writewait = mdb_writewait;
732 cb.sc_private = &wwctx;
733 wwctx.txn = ltid;
734 wwctx.mcd = NULL;
735 cb.sc_next = op->o_callback;
736 op->o_callback = &cb;
737 }
738
739 if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
740 PagedResultsState *ps = op->o_pagedresults_state;
741 /* deferred cookie parsing */
742 rs->sr_err = parse_paged_cookie( op, rs );
743 if ( rs->sr_err != LDAP_SUCCESS ) {
744 send_ldap_result( op, rs );
745 goto done;
746 }
747
748 cursor = (ID) ps->ps_cookie;
749 if ( cursor && ps->ps_size == 0 ) {
750 rs->sr_err = LDAP_SUCCESS;
751 rs->sr_text = "search abandoned by pagedResult size=0";
752 send_ldap_result( op, rs );
753 goto done;
754 }
755
756 if ( admincheck )
757 goto adminlimit;
758
759 id = mdb_idl_first( candidates, &cursor );
760 if ( id == NOID ) {
761 Debug( LDAP_DEBUG_TRACE,
762 LDAP_XSTRING(mdb_search)
763 ": no paged results candidates\n" );
764 send_paged_response( op, rs, &lastid, 0 );
765
766 rs->sr_err = LDAP_OTHER;
767 goto done;
768 }
769 if ( id == (ID)ps->ps_cookie )
770 id = mdb_idl_next( candidates, &cursor );
771 nsubs = ncand; /* always bypass scope'd search */
772 goto loop_begin;
773 }
774 if ( nsubs < ncand ) {
775 int rc;
776 /* Do scope-based search */
777 if ( admincheck && nsubs > (unsigned) op->ors_limit->lms_s_unchecked )
778 goto adminlimit;
779
780 /* if any alias scopes were set, save them */
781 if (scopes[0].mid > 1) {
782 cursor = 1;
783 for (cscope = 1; cscope <= scopes[0].mid; cscope++) {
784 /* Ignore the original base */
785 if (scopes[cscope].mid == base->e_id)
786 continue;
787 iscopes[cursor++] = scopes[cscope].mid;
788 }
789 iscopes[0] = scopes[0].mid - 1;
790 } else {
791 iscopes[0] = 0;
792 }
793
794 wwctx.mcd = mcd;
795 isc.id = base->e_id;
796 isc.numrdns = 0;
797 rc = mdb_dn2id_walk( op, &isc );
798 if ( rc )
799 id = NOID;
800 else
801 id = isc.id;
802 cscope = 0;
803 } else {
804 if ( admincheck )
805 goto adminlimit;
806 id = mdb_idl_first( candidates, &cursor );
807 }
808
809 while (id != NOID)
810 {
811 int scopeok;
812 MDB_val edata;
813
814 loop_begin:
815
816 /* check for abandon */
817 if ( op->o_abandon ) {
818 rs->sr_err = SLAPD_ABANDON;
819 send_ldap_result( op, rs );
820 goto done;
821 }
822
823 /* mostly needed by internal searches,
824 * e.g. related to syncrepl, for whom
825 * abandon does not get set... */
826 if ( slapd_shutdown ) {
827 rs->sr_err = LDAP_UNAVAILABLE;
828 send_ldap_disconnect( op, rs );
829 goto done;
830 }
831
832 /* check time limit */
833 if ( op->ors_tlimit != SLAP_NO_LIMIT
834 && slap_get_time() > stoptime )
835 {
836 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
837 rs->sr_ref = rs->sr_v2ref;
838 send_ldap_result( op, rs );
839 rs->sr_err = LDAP_SUCCESS;
840 goto done;
841 }
842
843
844 if ( nsubs < ncand ) {
845 unsigned i;
846 /* Is this entry in the candidate list? */
847 scopeok = 0;
848 if (MDB_IDL_IS_RANGE( candidates )) {
849 if ( id >= MDB_IDL_RANGE_FIRST( candidates ) &&
850 id <= MDB_IDL_RANGE_LAST( candidates ))
851 scopeok = 1;
852 } else {
853 i = mdb_idl_search( candidates, id );
854 if (i <= candidates[0] && candidates[i] == id )
855 scopeok = 1;
856 }
857 if ( scopeok )
858 goto scopeok;
859 goto loop_continue;
860 }
861
862 /* Does this candidate actually satisfy the search scope?
863 */
864 scopeok = 0;
865 isc.numrdns = 0;
866 switch( op->ors_scope ) {
867 case LDAP_SCOPE_BASE:
868 /* This is always true, yes? */
869 if ( id == base->e_id ) scopeok = 1;
870 break;
871
872 #ifdef LDAP_SCOPE_CHILDREN
873 case LDAP_SCOPE_CHILDREN:
874 if ( id == base->e_id ) break;
875 /* Fall-thru */
876 #endif
877 case LDAP_SCOPE_SUBTREE:
878 if ( id == base->e_id ) {
879 scopeok = 1;
880 break;
881 }
882 /* Fall-thru */
883 case LDAP_SCOPE_ONELEVEL:
884 if ( id == base->e_id ) break;
885 isc.id = id;
886 isc.nscope = 0;
887 rs->sr_err = mdb_idscopes( op, &isc );
888 if ( rs->sr_err == MDB_SUCCESS ) {
889 if ( isc.nscope )
890 scopeok = 1;
891 } else {
892 if ( rs->sr_err == MDB_NOTFOUND )
893 goto notfound;
894 }
895 break;
896 }
897
898 /* Not in scope, ignore it */
899 if ( !scopeok )
900 {
901 Debug( LDAP_DEBUG_TRACE,
902 LDAP_XSTRING(mdb_search)
903 ": %ld scope not okay\n",
904 (long) id );
905 goto loop_continue;
906 }
907
908 scopeok:
909 if ( id == base->e_id ) {
910 e = base;
911 } else {
912
913 /* get the entry */
914 rs->sr_err = mdb_id2edata( op, mci, id, &edata );
915 if ( rs->sr_err == MDB_NOTFOUND ) {
916 notfound:
917 if( nsubs < ncand )
918 goto loop_continue;
919
920 if( !MDB_IDL_IS_RANGE(candidates) ) {
921 /* only complain for non-range IDLs */
922 Debug( LDAP_DEBUG_TRACE,
923 LDAP_XSTRING(mdb_search)
924 ": candidate %ld not found\n",
925 (long) id );
926 } else {
927 /* get the next ID from the DB */
928 rs->sr_err = mdb_get_nextid( mci, &cursor );
929 if ( rs->sr_err == MDB_NOTFOUND ) {
930 break;
931 }
932 if ( rs->sr_err ) {
933 rs->sr_err = LDAP_OTHER;
934 rs->sr_text = "internal error in get_nextid";
935 send_ldap_result( op, rs );
936 goto done;
937 }
938 cursor--;
939 }
940
941 goto loop_continue;
942 } else if ( rs->sr_err ) {
943 rs->sr_err = LDAP_OTHER;
944 rs->sr_text = "internal error in mdb_id2edata";
945 send_ldap_result( op, rs );
946 goto done;
947 }
948
949 rs->sr_err = mdb_entry_decode( op, ltid, &edata, id, &e );
950 if ( rs->sr_err ) {
951 rs->sr_err = LDAP_OTHER;
952 rs->sr_text = "internal error in mdb_entry_decode";
953 send_ldap_result( op, rs );
954 goto done;
955 }
956 e->e_id = id;
957 e->e_name.bv_val = NULL;
958 e->e_nname.bv_val = NULL;
959 }
960
961 if ( is_entry_subentry( e ) ) {
962 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
963 if(!get_subentries_visibility( op )) {
964 /* only subentries are visible */
965 goto loop_continue;
966 }
967
968 } else if ( get_subentries( op ) &&
969 !get_subentries_visibility( op ))
970 {
971 /* only subentries are visible */
972 goto loop_continue;
973 }
974
975 } else if ( get_subentries_visibility( op )) {
976 /* only subentries are visible */
977 goto loop_continue;
978 }
979
980 /* aliases were already dereferenced in candidate list */
981 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
982 /* but if the search base is an alias, and we didn't
983 * deref it when finding, return it.
984 */
985 if ( is_entry_alias(e) &&
986 ((op->ors_deref & LDAP_DEREF_FINDING) || e != base ))
987 {
988 goto loop_continue;
989 }
990 }
991
992 if ( !manageDSAit && is_entry_glue( e )) {
993 goto loop_continue;
994 }
995
996 if (e != base) {
997 struct berval pdn, pndn;
998 char *d, *n;
999 int i;
1000
1001 /* child of base, just append RDNs to base->e_name */
1002 if ( nsubs < ncand || isc.scopes[isc.nscope].mid == base->e_id ) {
1003 pdn = base->e_name;
1004 pndn = base->e_nname;
1005 } else {
1006 mdb_id2name( op, ltid, &isc.mc, scopes[isc.nscope].mid, &pdn, &pndn );
1007 }
1008 e->e_name.bv_len = pdn.bv_len;
1009 e->e_nname.bv_len = pndn.bv_len;
1010 for (i=0; i<isc.numrdns; i++) {
1011 e->e_name.bv_len += isc.rdns[i].bv_len + 1;
1012 e->e_nname.bv_len += isc.nrdns[i].bv_len + 1;
1013 }
1014 e->e_name.bv_val = op->o_tmpalloc(e->e_name.bv_len + 1, op->o_tmpmemctx);
1015 e->e_nname.bv_val = op->o_tmpalloc(e->e_nname.bv_len + 1, op->o_tmpmemctx);
1016 d = e->e_name.bv_val;
1017 n = e->e_nname.bv_val;
1018 if (nsubs < ncand) {
1019 /* RDNs are in top-down order */
1020 for (i=isc.numrdns-1; i>=0; i--) {
1021 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
1022 d += isc.rdns[i].bv_len;
1023 *d++ = ',';
1024 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
1025 n += isc.nrdns[i].bv_len;
1026 *n++ = ',';
1027 }
1028 } else {
1029 /* RDNs are in bottom-up order */
1030 for (i=0; i<isc.numrdns; i++) {
1031 memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
1032 d += isc.rdns[i].bv_len;
1033 *d++ = ',';
1034 memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
1035 n += isc.nrdns[i].bv_len;
1036 *n++ = ',';
1037 }
1038 }
1039
1040 if (pdn.bv_len) {
1041 memcpy(d, pdn.bv_val, pdn.bv_len+1);
1042 memcpy(n, pndn.bv_val, pndn.bv_len+1);
1043 } else {
1044 *--d = '\0';
1045 *--n = '\0';
1046 e->e_name.bv_len--;
1047 e->e_nname.bv_len--;
1048 }
1049 if (pndn.bv_val != base->e_nname.bv_val) {
1050 op->o_tmpfree(pndn.bv_val, op->o_tmpmemctx);
1051 op->o_tmpfree(pdn.bv_val, op->o_tmpmemctx);
1052 }
1053 }
1054
1055 /*
1056 * if it's a referral, add it to the list of referrals. only do
1057 * this for non-base searches, and don't check the filter
1058 * explicitly here since it's only a candidate anyway.
1059 */
1060 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
1061 && is_entry_referral( e ) )
1062 {
1063 BerVarray erefs = get_entry_referrals( op, e );
1064 rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1065 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1066 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1067
1068 rs->sr_entry = e;
1069 rs->sr_flags = 0;
1070
1071 send_search_reference( op, rs );
1072
1073 if (e != base)
1074 mdb_entry_return( op, e );
1075 rs->sr_entry = NULL;
1076 e = NULL;
1077
1078 ber_bvarray_free( rs->sr_ref );
1079 ber_bvarray_free( erefs );
1080 rs->sr_ref = NULL;
1081
1082 goto loop_continue;
1083 }
1084
1085 /* if it matches the filter and scope, send it */
1086 rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
1087
1088 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1089 /* check size limit */
1090 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
1091 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
1092 if (e != base)
1093 mdb_entry_return( op, e );
1094 e = NULL;
1095 send_paged_response( op, rs, &lastid, tentries );
1096 goto done;
1097 }
1098 lastid = id;
1099 }
1100
1101 if (e) {
1102 /* safe default */
1103 rs->sr_attrs = op->oq_search.rs_attrs;
1104 rs->sr_operational_attrs = NULL;
1105 rs->sr_ctrls = NULL;
1106 rs->sr_entry = e;
1107 RS_ASSERT( e->e_private != NULL );
1108 rs->sr_flags = 0;
1109 rs->sr_err = LDAP_SUCCESS;
1110 rs->sr_err = send_search_entry( op, rs );
1111 rs->sr_attrs = NULL;
1112 rs->sr_entry = NULL;
1113 if (e != base)
1114 mdb_entry_return( op, e );
1115 e = NULL;
1116
1117 switch ( rs->sr_err ) {
1118 case LDAP_SUCCESS: /* entry sent ok */
1119 break;
1120 default: /* entry not sent */
1121 break;
1122 case LDAP_BUSY:
1123 send_ldap_result( op, rs );
1124 goto done;
1125 case LDAP_UNAVAILABLE:
1126 case LDAP_SIZELIMIT_EXCEEDED:
1127 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1128 rs->sr_ref = rs->sr_v2ref;
1129 send_ldap_result( op, rs );
1130 rs->sr_err = LDAP_SUCCESS;
1131
1132 } else {
1133 rs->sr_err = LDAP_OTHER;
1134 }
1135 goto done;
1136 }
1137 }
1138
1139 } else {
1140 Debug( LDAP_DEBUG_TRACE,
1141 LDAP_XSTRING(mdb_search)
1142 ": %ld does not match filter\n",
1143 (long) id );
1144 }
1145
1146 loop_continue:
1147 if ( moi == &opinfo && !wwctx.flag && mdb->mi_rtxn_size ) {
1148 wwctx.nentries++;
1149 if ( wwctx.nentries >= mdb->mi_rtxn_size ) {
1150 MDB_envinfo ei;
1151 wwctx.nentries = 0;
1152 mdb_env_info(mdb->mi_dbenv, &ei);
1153 if ( ei.me_last_txnid > mdb_txn_id( ltid ))
1154 mdb_rtxn_snap( op, &wwctx );
1155 }
1156 }
1157 if ( wwctx.flag ) {
1158 rs->sr_err = mdb_waitfixup( op, &wwctx, mci, mcd, &isc );
1159 if ( rs->sr_err ) {
1160 send_ldap_result( op, rs );
1161 goto done;
1162 }
1163 }
1164
1165 if( e != NULL ) {
1166 if ( e != base )
1167 mdb_entry_return( op, e );
1168 RS_ASSERT( rs->sr_entry == NULL );
1169 e = NULL;
1170 rs->sr_entry = NULL;
1171 }
1172
1173 if ( nsubs < ncand ) {
1174 int rc = mdb_dn2id_walk( op, &isc );
1175 if (rc) {
1176 id = NOID;
1177 /* We got to the end of a subtree. If there are any
1178 * alias scopes left, search them too.
1179 */
1180 while (iscopes[0] && cscope < iscopes[0]) {
1181 cscope++;
1182 isc.id = iscopes[cscope];
1183 if ( base )
1184 mdb_entry_return( op, base );
1185 rs->sr_err = mdb_id2entry(op, mci, isc.id, &base);
1186 if ( !rs->sr_err ) {
1187 mdb_id2name( op, ltid, &isc.mc, isc.id, &base->e_name, &base->e_nname );
1188 isc.numrdns = 0;
1189 if (isc.oscope == LDAP_SCOPE_ONELEVEL)
1190 isc.oscope = LDAP_SCOPE_BASE;
1191 rc = mdb_dn2id_walk( op, &isc );
1192 if ( !rc ) {
1193 id = isc.id;
1194 break;
1195 }
1196 }
1197 }
1198 } else
1199 id = isc.id;
1200 } else {
1201 id = mdb_idl_next( candidates, &cursor );
1202 }
1203 }
1204
1205 nochange:
1206 rs->sr_ctrls = NULL;
1207 rs->sr_ref = rs->sr_v2ref;
1208 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1209 rs->sr_rspoid = NULL;
1210 if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
1211 send_paged_response( op, rs, NULL, 0 );
1212 } else {
1213 send_ldap_result( op, rs );
1214 }
1215
1216 done:
1217 if ( cb.sc_private ) {
1218 /* remove our writewait callback */
1219 slap_callback **scp = &op->o_callback;
1220 while ( *scp ) {
1221 if ( *scp == &cb ) {
1222 *scp = cb.sc_next;
1223 cb.sc_private = NULL;
1224 break;
1225 }
1226 }
1227 }
1228 mdb_cursor_close( mcd );
1229 mdb_cursor_close( mci );
1230 if ( moi == &opinfo ) {
1231 mdb_txn_reset( moi->moi_txn );
1232 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
1233 } else {
1234 moi->moi_ref--;
1235 }
1236 if( rs->sr_v2ref ) {
1237 ber_bvarray_free( rs->sr_v2ref );
1238 rs->sr_v2ref = NULL;
1239 }
1240 if (base)
1241 mdb_entry_return( op, base );
1242 scope_chunk_ret( op, scopes );
1243 if ( candidates != c0 ) {
1244 ch_free( candidates );
1245 } else {
1246 MDB_IDL_ZERO( candidates );
1247 }
1248
1249 return rs->sr_err;
1250 }
1251
1252
1253 static int base_candidate(
1254 BackendDB *be,
1255 Entry *e,
1256 ID *ids )
1257 {
1258 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1259 e->e_nname.bv_val, (long) e->e_id );
1260
1261 ids[0] = 1;
1262 ids[1] = e->e_id;
1263 return 0;
1264 }
1265
1266 /* Look for "objectClass Present" in this filter.
1267 * Also count depth of filter tree while we're at it.
1268 */
1269 static int oc_filter(
1270 Filter *f,
1271 int cur,
1272 int *max )
1273 {
1274 int rc = 0;
1275
1276 assert( f != NULL );
1277
1278 if( cur > *max ) *max = cur;
1279
1280 switch( f->f_choice ) {
1281 case LDAP_FILTER_PRESENT:
1282 if (f->f_desc == slap_schema.si_ad_objectClass) {
1283 rc = 1;
1284 }
1285 break;
1286
1287 case LDAP_FILTER_AND:
1288 case LDAP_FILTER_OR:
1289 cur++;
1290 for ( f=f->f_and; f; f=f->f_next ) {
1291 (void) oc_filter(f, cur, max);
1292 }
1293 break;
1294
1295 default:
1296 break;
1297 }
1298 return rc;
1299 }
1300
1301 typedef struct IDLchunk {
1302 unsigned int logn;
1303 unsigned int pad;
1304 } IDLchunk;
1305
1306 static void search_stack_free( void *key, void *data )
1307 {
1308 ber_memfree_x(data, NULL);
1309 }
1310
1311 static void *search_stack( Operation *op )
1312 {
1313 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1314 IDLchunk *ic = NULL;
1315
1316 if ( op->o_threadctx ) {
1317 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
1318 (void **)&ic, NULL );
1319 } else {
1320 ic = mdb->mi_search_stack;
1321 }
1322
1323 if ( ic && ic->logn != MDB_idl_logn ) {
1324 ber_memfree_x( ic, NULL );
1325 ic = NULL;
1326 }
1327
1328 if ( !ic ) {
1329 ic = ch_malloc(( mdb->mi_search_stack_depth + 2 ) * (size_t)MDB_idl_um_size
1330 * sizeof( ID ) + sizeof( IDLchunk ) );
1331 ic->logn = MDB_idl_logn;
1332 if ( op->o_threadctx ) {
1333 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1334 ic, search_stack_free, NULL, NULL );
1335 } else {
1336 mdb->mi_search_stack = ic;
1337 }
1338 ID *idl = (ID *)( ic+1 );
1339 MDB_IDL_ZERO( idl );
1340 }
1341 return ic+1;
1342 }
1343
1344 static int search_candidates(
1345 Operation *op,
1346 SlapReply *rs,
1347 Entry *e,
1348 IdScopes *isc,
1349 MDB_cursor *mci,
1350 ID *ids,
1351 ID *stack )
1352 {
1353 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1354 int rc, depth = 1;
1355 Filter *f, rf, xf, nf, sf;
1356 AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1357 AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1358
1359 /*
1360 * This routine takes as input a filter (user-filter)
1361 * and rewrites it as follows:
1362 * (&(scope=DN)[(objectClass=subentry)]
1363 * (|[(objectClass=referral)](user-filter))
1364 */
1365
1366 Debug(LDAP_DEBUG_TRACE,
1367 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1368 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1369
1370 f = op->oq_search.rs_filter;
1371
1372 /* If the user's filter uses objectClass=*,
1373 * these clauses are redundant.
1374 */
1375 if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1376 && !get_subentries_visibility(op)) {
1377 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1378 /* match referral objects */
1379 struct berval bv_ref = BER_BVC( "referral" );
1380 rf.f_choice = LDAP_FILTER_EQUALITY;
1381 rf.f_ava = &aa_ref;
1382 rf.f_av_desc = slap_schema.si_ad_objectClass;
1383 rf.f_av_value = bv_ref;
1384 rf.f_next = f;
1385 xf.f_or = &rf;
1386 xf.f_choice = LDAP_FILTER_OR;
1387 xf.f_next = NULL;
1388 f = &xf;
1389 depth++;
1390 }
1391 }
1392
1393 if( get_subentries_visibility( op ) ) {
1394 struct berval bv_subentry = BER_BVC( "subentry" );
1395 sf.f_choice = LDAP_FILTER_EQUALITY;
1396 sf.f_ava = &aa_subentry;
1397 sf.f_av_desc = slap_schema.si_ad_objectClass;
1398 sf.f_av_value = bv_subentry;
1399 sf.f_next = f;
1400 nf.f_choice = LDAP_FILTER_AND;
1401 nf.f_and = &sf;
1402 nf.f_next = NULL;
1403 f = &nf;
1404 depth++;
1405 }
1406
1407 /* Allocate IDL stack, plus 1 more for former tmp */
1408 if ( depth+1 > mdb->mi_search_stack_depth ) {
1409 stack = ch_malloc( (depth + 1) * MDB_idl_um_size * sizeof( ID ) );
1410 }
1411
1412 if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1413 rc = search_aliases( op, rs, e->e_id, isc, mci, stack );
1414 } else {
1415 rc = LDAP_SUCCESS;
1416 }
1417
1418 if ( rc == LDAP_SUCCESS ) {
1419 rc = mdb_filter_candidates( op, isc->mt, f, ids,
1420 stack, stack+MDB_idl_um_size );
1421 }
1422
1423 if ( depth+1 > mdb->mi_search_stack_depth ) {
1424 ch_free( stack );
1425 }
1426
1427 if( rc ) {
1428 Debug(LDAP_DEBUG_TRACE,
1429 "mdb_search_candidates: failed (rc=%d)\n",
1430 rc );
1431
1432 } else {
1433 Debug(LDAP_DEBUG_TRACE,
1434 "mdb_search_candidates: id=%ld first=%ld last=%ld\n",
1435 (long) ids[0],
1436 (long) MDB_IDL_FIRST(ids),
1437 (long) MDB_IDL_LAST(ids) );
1438 }
1439
1440 return rc;
1441 }
1442
1443 static int
1444 parse_paged_cookie( Operation *op, SlapReply *rs )
1445 {
1446 int rc = LDAP_SUCCESS;
1447 PagedResultsState *ps = op->o_pagedresults_state;
1448
1449 /* this function must be invoked only if the pagedResults
1450 * control has been detected, parsed and partially checked
1451 * by the frontend */
1452 assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1453
1454 /* cookie decoding/checks deferred to backend... */
1455 if ( ps->ps_cookieval.bv_len ) {
1456 PagedResultsCookie reqcookie;
1457 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1458 /* bad cookie */
1459 rs->sr_text = "paged results cookie is invalid";
1460 rc = LDAP_PROTOCOL_ERROR;
1461 goto done;
1462 }
1463
1464 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1465
1466 if ( reqcookie > ps->ps_cookie ) {
1467 /* bad cookie */
1468 rs->sr_text = "paged results cookie is invalid";
1469 rc = LDAP_PROTOCOL_ERROR;
1470 goto done;
1471
1472 } else if ( reqcookie < ps->ps_cookie ) {
1473 rs->sr_text = "paged results cookie is invalid or old";
1474 rc = LDAP_UNWILLING_TO_PERFORM;
1475 goto done;
1476 }
1477
1478 } else {
1479 /* we're going to use ps_cookie */
1480 op->o_conn->c_pagedresults_state.ps_cookie = 0;
1481 }
1482
1483 done:;
1484
1485 return rc;
1486 }
1487
1488 static void
1489 send_paged_response(
1490 Operation *op,
1491 SlapReply *rs,
1492 ID *lastid,
1493 int tentries )
1494 {
1495 LDAPControl *ctrls[2];
1496 BerElementBuffer berbuf;
1497 BerElement *ber = (BerElement *)&berbuf;
1498 PagedResultsCookie respcookie;
1499 struct berval cookie;
1500
1501 Debug(LDAP_DEBUG_ARGS,
1502 "send_paged_response: lastid=0x%08lx nentries=%d\n",
1503 lastid ? *lastid : 0, rs->sr_nentries );
1504
1505 ctrls[1] = NULL;
1506
1507 ber_init2( ber, NULL, LBER_USE_DER );
1508
1509 if ( lastid ) {
1510 respcookie = ( PagedResultsCookie )(*lastid);
1511 cookie.bv_len = sizeof( respcookie );
1512 cookie.bv_val = (char *)&respcookie;
1513
1514 } else {
1515 respcookie = ( PagedResultsCookie )0;
1516 BER_BVSTR( &cookie, "" );
1517 }
1518
1519 op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1520 op->o_conn->c_pagedresults_state.ps_count =
1521 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1522 rs->sr_nentries;
1523
1524 /* return size of 0 -- no estimate */
1525 ber_printf( ber, "{iO}", 0, &cookie );
1526
1527 ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1528 if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1529 goto done;
1530 }
1531
1532 ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1533 ctrls[0]->ldctl_iscritical = 0;
1534
1535 slap_add_ctrls( op, rs, ctrls );
1536 rs->sr_err = LDAP_SUCCESS;
1537 send_ldap_result( op, rs );
1538
1539 done:
1540 (void) ber_free_buf( ber );
1541 }