4 Copyright (C) Simo Sorce 2005-2008
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
36 #include "system/filesys.h"
37 #include "system/time.h"
38 #include "dlinklist.h"
40 #include "ldb_module.h"
42 struct message_store {
43 /* keep the whole ldb_reply as an optimization
44 * instead of freeing and talloc-ing the container
47 struct message_store *next;
52 struct results_store {
53 struct results_store *prev, *next;
55 struct private_data *priv;
60 struct message_store *first;
61 struct message_store *last;
64 struct message_store *first_ref;
65 struct message_store *last_ref;
67 struct ldb_control **controls;
71 uint32_t next_free_id;
73 struct results_store *store;
77 static int store_destructor(struct results_store *del)
79 struct private_data *priv = del->priv;
80 DLIST_REMOVE(priv->store, del);
82 assert(priv->num_stores > 0);
83 priv->num_stores -= 1;
88 static struct results_store *new_store(struct private_data *priv)
90 struct results_store *newr;
91 uint32_t new_id = priv->next_free_id++;
93 /* TODO: we should have a limit on the number of
94 * outstanding paged searches
97 newr = talloc(priv, struct results_store);
98 if (!newr) return NULL;
102 newr->cookie = talloc_asprintf(newr, "%d", new_id);
108 newr->timestamp = time(NULL);
111 newr->num_entries = 0;
112 newr->first_ref = NULL;
113 newr->controls = NULL;
115 DLIST_ADD(priv->store, newr);
117 assert(priv->num_stores < SIZE_MAX);
118 priv->num_stores += 1;
120 talloc_set_destructor(newr, store_destructor);
122 if (priv->num_stores > 10) {
123 struct results_store *last;
125 * 10 is the default for MaxResultSetsPerConn --
126 * possibly need to parameterize it.
128 last = DLIST_TAIL(priv->store);
135 struct paged_context {
136 struct ldb_module *module;
137 struct ldb_request *req;
139 struct results_store *store;
141 struct ldb_control **controls;
144 static int paged_results(struct paged_context *ac)
146 struct ldb_paged_control *paged;
147 struct message_store *msg;
148 unsigned int i, num_ctrls;
151 if (ac->store == NULL) {
152 return LDB_ERR_OPERATIONS_ERROR;
155 while (ac->store->num_entries > 0 && ac->size > 0) {
156 msg = ac->store->first;
157 ret = ldb_module_send_entry(ac->req, msg->r->message, msg->r->controls);
158 if (ret != LDB_SUCCESS) {
162 ac->store->first = msg->next;
164 ac->store->num_entries--;
168 while (ac->store->first_ref != NULL) {
169 msg = ac->store->first_ref;
170 ret = ldb_module_send_referral(ac->req, msg->r->referral);
171 if (ret != LDB_SUCCESS) {
175 ac->store->first_ref = msg->next;
179 /* return result done */
183 if (ac->store->controls != NULL) {
184 while (ac->store->controls[i]) i++; /* counting */
189 ac->controls = talloc_array(ac, struct ldb_control *, num_ctrls +1);
190 if (ac->controls == NULL) {
191 return LDB_ERR_OPERATIONS_ERROR;
193 ac->controls[num_ctrls] = NULL;
195 for (i = 0; i < (num_ctrls -1); i++) {
196 ac->controls[i] = talloc_reference(ac->controls, ac->store->controls[i]);
199 ac->controls[i] = talloc(ac->controls, struct ldb_control);
200 if (ac->controls[i] == NULL) {
201 return LDB_ERR_OPERATIONS_ERROR;
204 ac->controls[i]->oid = talloc_strdup(ac->controls[i],
205 LDB_CONTROL_PAGED_RESULTS_OID);
206 if (ac->controls[i]->oid == NULL) {
207 return LDB_ERR_OPERATIONS_ERROR;
210 ac->controls[i]->critical = 0;
212 paged = talloc(ac->controls[i], struct ldb_paged_control);
214 return LDB_ERR_OPERATIONS_ERROR;
217 ac->controls[i]->data = paged;
221 paged->cookie = NULL;
222 paged->cookie_len = 0;
224 paged->size = ac->store->num_entries;
225 paged->cookie = talloc_strdup(paged, ac->store->cookie);
226 paged->cookie_len = strlen(paged->cookie) + 1;
232 static int paged_search_callback(struct ldb_request *req, struct ldb_reply *ares)
234 struct paged_context *ac ;
235 struct message_store *msg_store;
238 ac = talloc_get_type(req->context, struct paged_context);
241 return ldb_module_done(ac->req, NULL, NULL,
242 LDB_ERR_OPERATIONS_ERROR);
244 if (ares->error != LDB_SUCCESS) {
245 return ldb_module_done(ac->req, ares->controls,
246 ares->response, ares->error);
249 switch (ares->type) {
250 case LDB_REPLY_ENTRY:
251 msg_store = talloc(ac->store, struct message_store);
252 if (msg_store == NULL) {
253 return ldb_module_done(ac->req, NULL, NULL,
254 LDB_ERR_OPERATIONS_ERROR);
256 msg_store->next = NULL;
257 msg_store->r = talloc_steal(msg_store, ares);
259 if (ac->store->first == NULL) {
260 ac->store->first = msg_store;
262 ac->store->last->next = msg_store;
264 ac->store->last = msg_store;
266 ac->store->num_entries++;
270 case LDB_REPLY_REFERRAL:
271 msg_store = talloc(ac->store, struct message_store);
272 if (msg_store == NULL) {
273 return ldb_module_done(ac->req, NULL, NULL,
274 LDB_ERR_OPERATIONS_ERROR);
276 msg_store->next = NULL;
277 msg_store->r = talloc_steal(msg_store, ares);
279 if (ac->store->first_ref == NULL) {
280 ac->store->first_ref = msg_store;
282 ac->store->last_ref->next = msg_store;
284 ac->store->last_ref = msg_store;
289 ac->store->controls = talloc_move(ac->store, &ares->controls);
290 ret = paged_results(ac);
291 return ldb_module_done(ac->req, ac->controls,
292 ares->response, ret);
298 static int paged_search(struct ldb_module *module, struct ldb_request *req)
300 struct ldb_context *ldb;
301 struct ldb_control *control;
302 struct private_data *private_data;
303 struct ldb_paged_control *paged_ctrl;
304 struct ldb_control **saved_controls;
305 struct ldb_request *search_req;
306 struct paged_context *ac;
309 ldb = ldb_module_get_ctx(module);
311 /* check if there's a paged request control */
312 control = ldb_request_get_control(req, LDB_CONTROL_PAGED_RESULTS_OID);
313 if (control == NULL) {
314 /* not found go on */
315 return ldb_next_request(module, req);
318 paged_ctrl = talloc_get_type(control->data, struct ldb_paged_control);
320 return LDB_ERR_PROTOCOL_ERROR;
323 private_data = talloc_get_type(ldb_module_get_private(module),
324 struct private_data);
326 ac = talloc_zero(req, struct paged_context);
328 ldb_set_errstring(ldb, "Out of Memory");
329 return LDB_ERR_OPERATIONS_ERROR;
334 ac->size = paged_ctrl->size;
336 /* apparently some clients send more than 2^31. This
337 violates the ldap standard, but we need to cope */
338 ac->size = 0x7FFFFFFF;
341 /* check if it is a continuation search the store */
342 if (paged_ctrl->cookie_len == 0) {
343 if (paged_ctrl->size == 0) {
344 return LDB_ERR_OPERATIONS_ERROR;
347 ac->store = new_store(private_data);
348 if (ac->store == NULL) {
349 return LDB_ERR_OPERATIONS_ERROR;
352 ret = ldb_build_search_req_ex(&search_req, ldb, ac,
354 req->op.search.scope,
356 req->op.search.attrs,
359 paged_search_callback,
361 if (ret != LDB_SUCCESS) {
365 /* save it locally and remove it from the list */
366 /* we do not need to replace them later as we
367 * are keeping the original req intact */
368 if (!ldb_save_controls(control, search_req, &saved_controls)) {
369 return LDB_ERR_OPERATIONS_ERROR;
372 return ldb_next_request(module, search_req);
375 struct results_store *current = NULL;
377 /* TODO: age out old outstanding requests */
378 for (current = private_data->store; current; current = current->next) {
379 if (strcmp(current->cookie, paged_ctrl->cookie) == 0) {
380 current->timestamp = time(NULL);
384 if (current == NULL) {
385 return LDB_ERR_UNWILLING_TO_PERFORM;
388 DLIST_PROMOTE(private_data->store, current);
392 /* check if it is an abandon */
394 return ldb_module_done(req, NULL, NULL,
398 ret = paged_results(ac);
399 if (ret != LDB_SUCCESS) {
400 return ldb_module_done(req, NULL, NULL, ret);
402 return ldb_module_done(req, ac->controls, NULL,
407 static int paged_request_init(struct ldb_module *module)
409 struct ldb_context *ldb;
410 struct private_data *data;
413 ldb = ldb_module_get_ctx(module);
415 data = talloc(module, struct private_data);
417 return LDB_ERR_OTHER;
420 data->next_free_id = 1;
421 data->num_stores = 0;
423 ldb_module_set_private(module, data);
425 ret = ldb_mod_register_control(module, LDB_CONTROL_PAGED_RESULTS_OID);
426 if (ret != LDB_SUCCESS) {
427 ldb_debug(ldb, LDB_DEBUG_WARNING,
429 "Unable to register control with rootdse!");
432 return ldb_next_init(module);
435 static const struct ldb_module_ops ldb_paged_results_module_ops = {
436 .name = "paged_results",
437 .search = paged_search,
438 .init_context = paged_request_init
441 int ldb_paged_results_init(const char *version)
443 LDB_MODULE_CHECK_VERSION(version);
444 return ldb_register_module(&ldb_paged_results_module_ops);