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