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, see <http://www.gnu.org/licenses/>.
27 * Component: ldb paged results control module
29 * Description: this module caches a complete search and sends back
30 * results in chunks as asked by the client
35 #include "ldb_includes.h"
37 struct message_store {
38 /* keep the whole ldb_reply as an optimization
39 * instead of freeing and talloc-ing the container
42 struct message_store *next;
47 struct results_store {
49 struct private_data *priv;
54 struct results_store *prev;
55 struct results_store *next;
57 struct message_store *first;
58 struct message_store *last;
61 struct message_store *first_ref;
62 struct message_store *last_ref;
64 struct ldb_control **controls;
66 struct ldb_request *req;
72 struct results_store *store;
76 int store_destructor(struct results_store *store)
79 store->prev->next = store->next;
82 store->next->prev = store->prev;
85 if (store == store->priv->store) {
86 store->priv->store = NULL;
92 static struct results_store *new_store(struct private_data *priv)
94 struct results_store *newr;
95 int new_id = priv->next_free_id++;
97 /* TODO: we should have a limit on the number of
98 * outstanding paged searches
101 newr = talloc(priv, struct results_store);
102 if (!newr) return NULL;
106 newr->cookie = talloc_asprintf(newr, "%d", new_id);
112 newr->timestamp = time(NULL);
115 newr->num_entries = 0;
116 newr->first_ref = NULL;
117 newr->controls = NULL;
119 /* put this entry as first */
121 newr->next = priv->store;
122 if (priv->store != NULL) priv->store->prev = newr;
125 talloc_set_destructor(newr, store_destructor);
130 struct paged_context {
131 struct ldb_module *module;
133 int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
137 struct results_store *store;
140 static struct ldb_handle *init_handle(void *mem_ctx, struct ldb_module *module,
142 int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
144 struct paged_context *ac;
145 struct ldb_handle *h;
147 h = talloc_zero(mem_ctx, struct ldb_handle);
149 ldb_set_errstring(module->ldb, "Out of Memory");
155 ac = talloc_zero(h, struct paged_context);
157 ldb_set_errstring(module->ldb, "Out of Memory");
162 h->private_data = (void *)ac;
164 h->state = LDB_ASYNC_INIT;
165 h->status = LDB_SUCCESS;
168 ac->up_context = context;
169 ac->up_callback = callback;
174 static int paged_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
176 struct paged_context *ac = NULL;
178 if (!context || !ares) {
179 ldb_set_errstring(ldb, "NULL Context or Result in callback");
183 ac = talloc_get_type(context, struct paged_context);
185 if (ares->type == LDB_REPLY_ENTRY) {
186 if (ac->store->first == NULL) {
187 ac->store->first = ac->store->last = talloc(ac->store, struct message_store);
189 ac->store->last->next = talloc(ac->store, struct message_store);
190 ac->store->last = ac->store->last->next;
192 if (ac->store->last == NULL) {
196 ac->store->num_entries++;
198 ac->store->last->r = talloc_steal(ac->store->last, ares);
199 ac->store->last->next = NULL;
202 if (ares->type == LDB_REPLY_REFERRAL) {
203 if (ac->store->first_ref == NULL) {
204 ac->store->first_ref = ac->store->last_ref = talloc(ac->store, struct message_store);
206 ac->store->last_ref->next = talloc(ac->store, struct message_store);
207 ac->store->last_ref = ac->store->last_ref->next;
209 if (ac->store->last_ref == NULL) {
213 ac->store->last_ref->r = talloc_steal(ac->store->last, ares);
214 ac->store->last_ref->next = NULL;
217 if (ares->type == LDB_REPLY_DONE) {
218 ac->store->controls = talloc_move(ac->store, &ares->controls);
226 return LDB_ERR_OPERATIONS_ERROR;
229 static int paged_search(struct ldb_module *module, struct ldb_request *req)
231 struct ldb_control *control;
232 struct private_data *private_data;
233 struct ldb_paged_control *paged_ctrl;
234 struct ldb_control **saved_controls;
235 struct paged_context *ac;
236 struct ldb_handle *h;
239 /* check if there's a paged request control */
240 control = ldb_request_get_control(req, LDB_CONTROL_PAGED_RESULTS_OID);
241 if (control == NULL) {
242 /* not found go on */
243 return ldb_next_request(module, req);
246 private_data = talloc_get_type(module->private_data, struct private_data);
250 if (!req->callback || !req->context) {
251 ldb_set_errstring(module->ldb,
252 "Async interface called with NULL callback function or NULL context");
253 return LDB_ERR_OPERATIONS_ERROR;
256 paged_ctrl = talloc_get_type(control->data, struct ldb_paged_control);
258 return LDB_ERR_PROTOCOL_ERROR;
261 h = init_handle(req, module, req->context, req->callback);
263 return LDB_ERR_OPERATIONS_ERROR;
265 ac = talloc_get_type(h->private_data, struct paged_context);
267 ac->size = paged_ctrl->size;
269 /* check if it is a continuation search the store */
270 if (paged_ctrl->cookie_len == 0) {
272 ac->store = new_store(private_data);
273 if (ac->store == NULL) {
275 return LDB_ERR_UNWILLING_TO_PERFORM;
278 ac->store->req = talloc(ac->store, struct ldb_request);
280 return LDB_ERR_OPERATIONS_ERROR;
282 ac->store->req->operation = req->operation;
283 ac->store->req->op.search.base = req->op.search.base;
284 ac->store->req->op.search.scope = req->op.search.scope;
285 ac->store->req->op.search.tree = req->op.search.tree;
286 ac->store->req->op.search.attrs = req->op.search.attrs;
287 ac->store->req->controls = req->controls;
289 /* save it locally and remove it from the list */
290 /* we do not need to replace them later as we
291 * are keeping the original req intact */
292 if (!save_controls(control, ac->store->req, &saved_controls)) {
293 return LDB_ERR_OPERATIONS_ERROR;
296 ac->store->req->context = ac;
297 ac->store->req->callback = paged_search_callback;
298 ldb_set_timeout_from_prev_req(module->ldb, req, ac->store->req);
300 ret = ldb_next_request(module, ac->store->req);
303 struct results_store *current = NULL;
305 for (current = private_data->store; current; current = current->next) {
306 if (strcmp(current->cookie, paged_ctrl->cookie) == 0) {
307 current->timestamp = time(NULL);
311 if (current == NULL) {
313 return LDB_ERR_UNWILLING_TO_PERFORM;
322 /* check if it is an abandon */
324 talloc_free(ac->store);
325 h->status = LDB_SUCCESS;
326 h->state = LDB_ASYNC_DONE;
330 /* TODO: age out old outstanding requests */
336 static int paged_results(struct ldb_handle *handle)
338 struct paged_context *ac;
339 struct ldb_paged_control *paged;
340 struct ldb_reply *ares;
341 struct message_store *msg;
342 int i, num_ctrls, ret;
344 ac = talloc_get_type(handle->private_data, struct paged_context);
346 if (ac->store == NULL)
347 return LDB_ERR_OPERATIONS_ERROR;
349 while (ac->store->num_entries > 0 && ac->size > 0) {
350 msg = ac->store->first;
351 ret = ac->up_callback(ac->module->ldb, ac->up_context, msg->r);
352 if (ret != LDB_SUCCESS) {
353 handle->status = ret;
354 handle->state = LDB_ASYNC_DONE;
358 ac->store->first = msg->next;
360 ac->store->num_entries--;
364 handle->state = LDB_ASYNC_DONE;
366 while (ac->store->first_ref != NULL) {
367 msg = ac->store->first_ref;
368 ret = ac->up_callback(ac->module->ldb, ac->up_context, msg->r);
369 if (ret != LDB_SUCCESS) {
370 handle->status = ret;
371 handle->state = LDB_ASYNC_DONE;
375 ac->store->first_ref = msg->next;
379 ares = talloc_zero(ac->store, struct ldb_reply);
381 handle->status = LDB_ERR_OPERATIONS_ERROR;
382 return handle->status;
387 if (ac->store->controls != NULL) {
388 ares->controls = ac->store->controls;
389 while (ares->controls[i]) i++; /* counting */
391 ares->controls = talloc_move(ares, &ac->store->controls);
395 ares->controls = talloc_realloc(ares, ares->controls, struct ldb_control *, num_ctrls);
396 if (ares->controls == NULL) {
397 handle->status = LDB_ERR_OPERATIONS_ERROR;
398 return handle->status;
401 ares->controls[i] = talloc(ares->controls, struct ldb_control);
402 if (ares->controls[i] == NULL) {
403 handle->status = LDB_ERR_OPERATIONS_ERROR;
404 return handle->status;
407 ares->controls[i]->oid = talloc_strdup(ares->controls[i], LDB_CONTROL_PAGED_RESULTS_OID);
408 if (ares->controls[i]->oid == NULL) {
409 handle->status = LDB_ERR_OPERATIONS_ERROR;
410 return handle->status;
413 ares->controls[i]->critical = 0;
414 ares->controls[i + 1] = NULL;
416 paged = talloc(ares->controls[i], struct ldb_paged_control);
418 handle->status = LDB_ERR_OPERATIONS_ERROR;
419 return handle->status;
422 ares->controls[i]->data = paged;
426 paged->cookie = NULL;
427 paged->cookie_len = 0;
429 paged->size = ac->store->num_entries;
430 paged->cookie = talloc_strdup(paged, ac->store->cookie);
431 paged->cookie_len = strlen(paged->cookie) + 1;
434 ares->type = LDB_REPLY_DONE;
436 ret = ac->up_callback(ac->module->ldb, ac->up_context, ares);
438 handle->status = ret;
443 static int paged_wait_once(struct ldb_handle *handle) {
444 struct paged_context *ac;
447 if (!handle || !handle->private_data) {
448 return LDB_ERR_OPERATIONS_ERROR;
451 if (handle->state == LDB_ASYNC_DONE) {
452 return handle->status;
455 handle->state = LDB_ASYNC_PENDING;
457 ac = talloc_get_type(handle->private_data, struct paged_context);
459 if (ac->store->req->handle->state == LDB_ASYNC_DONE) {
460 /* if lower level is finished we do not need to call it anymore */
461 /* return all we have until size == 0 or we empty storage */
462 ret = paged_results(handle);
464 /* we are done, if num_entries is zero free the storage
465 * as that mean we delivered the last batch */
466 if (ac->store->num_entries == 0) {
467 talloc_free(ac->store);
473 ret = ldb_wait(ac->store->req->handle, LDB_WAIT_NONE);
474 if (ret != LDB_SUCCESS) {
475 handle->state = LDB_ASYNC_DONE;
476 handle->status = ret;
480 handle->status = ret;
482 if (ac->store->num_entries >= ac->size ||
483 ac->store->req->handle->state == LDB_ASYNC_DONE) {
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);
497 static int paged_wait(struct ldb_handle *handle, enum ldb_wait_type type)
501 if (!handle || !handle->private_data) {
502 return LDB_ERR_OPERATIONS_ERROR;
505 if (type == LDB_WAIT_ALL) {
506 while (handle->state != LDB_ASYNC_DONE) {
507 ret = paged_wait_once(handle);
508 if (ret != LDB_SUCCESS) {
513 return handle->status;
516 return paged_wait_once(handle);
519 static int paged_request_init(struct ldb_module *module)
521 struct private_data *data;
522 struct ldb_request *req;
525 data = talloc(module, struct private_data);
527 return LDB_ERR_OTHER;
530 data->next_free_id = 1;
532 module->private_data = data;
534 req = talloc(module, struct ldb_request);
536 return LDB_ERR_OPERATIONS_ERROR;
539 req->operation = LDB_REQ_REGISTER_CONTROL;
540 req->op.reg_control.oid = LDB_CONTROL_PAGED_RESULTS_OID;
541 req->controls = NULL;
543 ret = ldb_request(module->ldb, req);
544 if (ret != LDB_SUCCESS) {
545 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "paged_request: Unable to register control with rootdse!\n");
549 return ldb_next_init(module);
552 const struct ldb_module_ops ldb_paged_results_module_ops = {
553 .name = "paged_results",
554 .search = paged_search,
556 .init_context = paged_request_init