4 Copyright (C) Simo Sorce 2005-2006
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb paged results control module
30 * Description: this module caches a complete search and sends back
31 * results in chunks as asked by the client
36 #include "ldb_includes.h"
38 struct message_store {
39 /* keep the whole ldb_reply as an optimization
40 * instead of freeing and talloc-ing the container
43 struct message_store *next;
48 struct results_store {
50 struct private_data *priv;
55 struct results_store *prev;
56 struct results_store *next;
58 struct message_store *first;
59 struct message_store *last;
62 struct message_store *first_ref;
63 struct message_store *last_ref;
65 struct ldb_control **controls;
67 struct ldb_request *req;
73 struct results_store *store;
77 int store_destructor(struct results_store *store)
80 store->prev->next = store->next;
83 store->next->prev = store->prev;
86 if (store == store->priv->store) {
87 store->priv->store = NULL;
93 static struct results_store *new_store(struct private_data *priv)
95 struct results_store *newr;
96 int new_id = priv->next_free_id++;
98 /* TODO: we should have a limit on the number of
99 * outstanding paged searches
102 newr = talloc(priv, struct results_store);
103 if (!newr) return NULL;
107 newr->cookie = talloc_asprintf(newr, "%d", new_id);
113 newr->timestamp = time(NULL);
116 newr->num_entries = 0;
117 newr->first_ref = NULL;
118 newr->controls = NULL;
120 /* put this entry as first */
122 newr->next = priv->store;
123 if (priv->store != NULL) priv->store->prev = newr;
126 talloc_set_destructor(newr, store_destructor);
131 struct paged_context {
132 struct ldb_module *module;
134 int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
138 struct results_store *store;
141 static struct ldb_handle *init_handle(void *mem_ctx, struct ldb_module *module,
143 int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
145 struct paged_context *ac;
146 struct ldb_handle *h;
148 h = talloc_zero(mem_ctx, struct ldb_handle);
150 ldb_set_errstring(module->ldb, "Out of Memory");
156 ac = talloc_zero(h, struct paged_context);
158 ldb_set_errstring(module->ldb, "Out of Memory");
163 h->private_data = (void *)ac;
165 h->state = LDB_ASYNC_INIT;
166 h->status = LDB_SUCCESS;
169 ac->up_context = context;
170 ac->up_callback = callback;
175 static int paged_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
177 struct paged_context *ac = NULL;
179 if (!context || !ares) {
180 ldb_set_errstring(ldb, "NULL Context or Result in callback");
184 ac = talloc_get_type(context, struct paged_context);
186 if (ares->type == LDB_REPLY_ENTRY) {
187 if (ac->store->first == NULL) {
188 ac->store->first = ac->store->last = talloc(ac->store, struct message_store);
190 ac->store->last->next = talloc(ac->store, struct message_store);
191 ac->store->last = ac->store->last->next;
193 if (ac->store->last == NULL) {
197 ac->store->num_entries++;
199 ac->store->last->r = talloc_steal(ac->store->last, ares);
200 ac->store->last->next = NULL;
203 if (ares->type == LDB_REPLY_REFERRAL) {
204 if (ac->store->first_ref == NULL) {
205 ac->store->first_ref = ac->store->last_ref = talloc(ac->store, struct message_store);
207 ac->store->last_ref->next = talloc(ac->store, struct message_store);
208 ac->store->last_ref = ac->store->last_ref->next;
210 if (ac->store->last_ref == NULL) {
214 ac->store->last_ref->r = talloc_steal(ac->store->last, ares);
215 ac->store->last_ref->next = NULL;
218 if (ares->type == LDB_REPLY_DONE) {
219 ac->store->controls = talloc_move(ac->store, &ares->controls);
227 return LDB_ERR_OPERATIONS_ERROR;
230 static int paged_search(struct ldb_module *module, struct ldb_request *req)
232 struct ldb_control *control;
233 struct private_data *private_data;
234 struct ldb_paged_control *paged_ctrl;
235 struct ldb_control **saved_controls;
236 struct paged_context *ac;
237 struct ldb_handle *h;
240 /* check if there's a paged request control */
241 control = ldb_request_get_control(req, LDB_CONTROL_PAGED_RESULTS_OID);
242 if (control == NULL) {
243 /* not found go on */
244 return ldb_next_request(module, req);
247 private_data = talloc_get_type(module->private_data, struct private_data);
251 if (!req->callback || !req->context) {
252 ldb_set_errstring(module->ldb,
253 "Async interface called with NULL callback function or NULL context");
254 return LDB_ERR_OPERATIONS_ERROR;
257 paged_ctrl = talloc_get_type(control->data, struct ldb_paged_control);
259 return LDB_ERR_PROTOCOL_ERROR;
262 h = init_handle(req, module, req->context, req->callback);
264 return LDB_ERR_OPERATIONS_ERROR;
266 ac = talloc_get_type(h->private_data, struct paged_context);
268 ac->size = paged_ctrl->size;
270 /* check if it is a continuation search the store */
271 if (paged_ctrl->cookie_len == 0) {
273 ac->store = new_store(private_data);
274 if (ac->store == NULL) {
276 return LDB_ERR_UNWILLING_TO_PERFORM;
279 ac->store->req = talloc(ac->store, struct ldb_request);
281 return LDB_ERR_OPERATIONS_ERROR;
283 ac->store->req->operation = req->operation;
284 ac->store->req->op.search.base = req->op.search.base;
285 ac->store->req->op.search.scope = req->op.search.scope;
286 ac->store->req->op.search.tree = req->op.search.tree;
287 ac->store->req->op.search.attrs = req->op.search.attrs;
288 ac->store->req->controls = req->controls;
290 /* save it locally and remove it from the list */
291 /* we do not need to replace them later as we
292 * are keeping the original req intact */
293 if (!save_controls(control, ac->store->req, &saved_controls)) {
294 return LDB_ERR_OPERATIONS_ERROR;
297 ac->store->req->context = ac;
298 ac->store->req->callback = paged_search_callback;
299 ldb_set_timeout_from_prev_req(module->ldb, req, ac->store->req);
301 ret = ldb_next_request(module, ac->store->req);
304 struct results_store *current = NULL;
306 for (current = private_data->store; current; current = current->next) {
307 if (strcmp(current->cookie, paged_ctrl->cookie) == 0) {
308 current->timestamp = time(NULL);
312 if (current == NULL) {
314 return LDB_ERR_UNWILLING_TO_PERFORM;
323 /* check if it is an abandon */
325 talloc_free(ac->store);
326 h->status = LDB_SUCCESS;
327 h->state = LDB_ASYNC_DONE;
331 /* TODO: age out old outstanding requests */
337 static int paged_results(struct ldb_handle *handle)
339 struct paged_context *ac;
340 struct ldb_paged_control *paged;
341 struct ldb_reply *ares;
342 struct message_store *msg;
343 int i, num_ctrls, ret;
345 ac = talloc_get_type(handle->private_data, struct paged_context);
347 if (ac->store == NULL)
348 return LDB_ERR_OPERATIONS_ERROR;
350 while (ac->store->num_entries > 0 && ac->size > 0) {
351 msg = ac->store->first;
352 ret = ac->up_callback(ac->module->ldb, ac->up_context, msg->r);
353 if (ret != LDB_SUCCESS) {
354 handle->status = ret;
355 handle->state = LDB_ASYNC_DONE;
359 ac->store->first = msg->next;
361 ac->store->num_entries--;
365 handle->state = LDB_ASYNC_DONE;
367 while (ac->store->first_ref != NULL) {
368 msg = ac->store->first_ref;
369 ret = ac->up_callback(ac->module->ldb, ac->up_context, msg->r);
370 if (ret != LDB_SUCCESS) {
371 handle->status = ret;
372 handle->state = LDB_ASYNC_DONE;
376 ac->store->first_ref = msg->next;
380 ares = talloc_zero(ac->store, struct ldb_reply);
382 handle->status = LDB_ERR_OPERATIONS_ERROR;
383 return handle->status;
388 if (ac->store->controls != NULL) {
389 ares->controls = ac->store->controls;
390 while (ares->controls[i]) i++; /* counting */
392 ares->controls = talloc_move(ares, &ac->store->controls);
396 ares->controls = talloc_realloc(ares, ares->controls, struct ldb_control *, num_ctrls);
397 if (ares->controls == NULL) {
398 handle->status = LDB_ERR_OPERATIONS_ERROR;
399 return handle->status;
402 ares->controls[i] = talloc(ares->controls, struct ldb_control);
403 if (ares->controls[i] == NULL) {
404 handle->status = LDB_ERR_OPERATIONS_ERROR;
405 return handle->status;
408 ares->controls[i]->oid = talloc_strdup(ares->controls[i], LDB_CONTROL_PAGED_RESULTS_OID);
409 if (ares->controls[i]->oid == NULL) {
410 handle->status = LDB_ERR_OPERATIONS_ERROR;
411 return handle->status;
414 ares->controls[i]->critical = 0;
415 ares->controls[i + 1] = NULL;
417 paged = talloc(ares->controls[i], struct ldb_paged_control);
419 handle->status = LDB_ERR_OPERATIONS_ERROR;
420 return handle->status;
423 ares->controls[i]->data = paged;
427 paged->cookie = NULL;
428 paged->cookie_len = 0;
430 paged->size = ac->store->num_entries;
431 paged->cookie = talloc_strdup(paged, ac->store->cookie);
432 paged->cookie_len = strlen(paged->cookie) + 1;
435 ares->type = LDB_REPLY_DONE;
437 ret = ac->up_callback(ac->module->ldb, ac->up_context, ares);
439 handle->status = ret;
444 static int paged_wait(struct ldb_handle *handle, enum ldb_wait_type type)
446 struct paged_context *ac;
449 if (!handle || !handle->private_data) {
450 return LDB_ERR_OPERATIONS_ERROR;
453 if (handle->state == LDB_ASYNC_DONE) {
454 return handle->status;
457 handle->state = LDB_ASYNC_PENDING;
459 ac = talloc_get_type(handle->private_data, struct paged_context);
461 if (ac->store->req->handle->state == LDB_ASYNC_DONE) {
462 /* if lower level is finished we do not need to call it anymore */
463 /* return all we have until size == 0 or we empty storage */
464 ret = paged_results(handle);
466 /* we are done, if num_entries is zero free the storage
467 * as that mean we delivered the last batch */
468 if (ac->store->num_entries == 0) {
469 talloc_free(ac->store);
475 if (type == LDB_WAIT_ALL) {
476 while (ac->store->req->handle->state != LDB_ASYNC_DONE) {
477 ret = ldb_wait(ac->store->req->handle, type);
478 if (ret != LDB_SUCCESS) {
479 handle->state = LDB_ASYNC_DONE;
480 handle->status = ret;
485 ret = paged_results(handle);
487 /* we are done, if num_entries is zero free the storage
488 * as that mean we delivered the last batch */
489 if (ac->store->num_entries == 0) {
490 talloc_free(ac->store);
496 ret = ldb_wait(ac->store->req->handle, type);
497 if (ret != LDB_SUCCESS) {
498 handle->state = LDB_ASYNC_DONE;
499 handle->status = ret;
503 handle->status = ret;
505 if (ac->store->num_entries >= ac->size ||
506 ac->store->req->handle->state == LDB_ASYNC_DONE) {
508 ret = paged_results(handle);
510 /* we are done, if num_entries is zero free the storage
511 * as that mean we delivered the last batch */
512 if (ac->store->num_entries == 0) {
513 talloc_free(ac->store);
520 static int paged_request_init(struct ldb_module *module)
522 struct private_data *data;
523 struct ldb_request *req;
526 data = talloc(module, struct private_data);
528 return LDB_ERR_OTHER;
531 data->next_free_id = 1;
533 module->private_data = data;
535 req = talloc(module, struct ldb_request);
537 return LDB_ERR_OPERATIONS_ERROR;
540 req->operation = LDB_REQ_REGISTER_CONTROL;
541 req->op.reg_control.oid = LDB_CONTROL_PAGED_RESULTS_OID;
542 req->controls = NULL;
544 ret = ldb_request(module->ldb, req);
545 if (ret != LDB_SUCCESS) {
546 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "paged_request: Unable to register control with rootdse!\n");
550 return ldb_next_init(module);
553 static const struct ldb_module_ops paged_ops = {
554 .name = "paged_results",
555 .search = paged_search,
557 .init_context = paged_request_init
560 int ldb_paged_results_init(void)
562 return ldb_register_module(&paged_ops);