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