r23795: more v2->v3 conversion
[samba.git] / source4 / lib / ldb / modules / paged_results.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2005-2006
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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.
14
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.
19
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
23 */
24
25 /*
26  *  Name: paged_result
27  *
28  *  Component: ldb paged results control module
29  *
30  *  Description: this module caches a complete search and sends back
31  *               results in chunks as asked by the client
32  *
33  *  Author: Simo Sorce
34  */
35
36 #include "ldb_includes.h"
37
38 struct message_store {
39         /* keep the whole ldb_reply as an optimization
40          * instead of freeing and talloc-ing the container
41          * on each result */
42         struct ldb_reply *r;
43         struct message_store *next;
44 };
45
46 struct private_data;
47
48 struct results_store {
49
50         struct private_data *priv;
51
52         char *cookie;
53         time_t timestamp;
54
55         struct results_store *prev;
56         struct results_store *next;
57         
58         struct message_store *first;
59         struct message_store *last;
60         int num_entries;
61
62         struct message_store *first_ref;
63         struct message_store *last_ref;
64
65         struct ldb_control **controls;
66
67         struct ldb_request *req;
68 };
69
70 struct private_data {
71
72         int next_free_id;
73         struct results_store *store;
74         
75 };
76
77 int store_destructor(struct results_store *store)
78 {
79         if (store->prev) {
80                 store->prev->next = store->next;
81         }
82         if (store->next) {
83                 store->next->prev = store->prev;
84         }
85
86         if (store == store->priv->store) {
87                 store->priv->store = NULL;
88         }
89
90         return 0;
91 }
92
93 static struct results_store *new_store(struct private_data *priv)
94 {
95         struct results_store *newr;
96         int new_id = priv->next_free_id++;
97
98         /* TODO: we should have a limit on the number of
99          * outstanding paged searches
100          */
101
102         newr = talloc(priv, struct results_store);
103         if (!newr) return NULL;
104
105         newr->priv = priv;
106
107         newr->cookie = talloc_asprintf(newr, "%d", new_id);
108         if (!newr->cookie) {
109                 talloc_free(newr);
110                 return NULL;
111         }
112
113         newr->timestamp = time(NULL);
114
115         newr->first = NULL;
116         newr->num_entries = 0;
117         newr->first_ref = NULL;
118         newr->controls = NULL;
119
120         /* put this entry as first */
121         newr->prev = NULL;
122         newr->next = priv->store;
123         if (priv->store != NULL) priv->store->prev = newr;
124         priv->store = newr;
125
126         talloc_set_destructor(newr, store_destructor);
127
128         return newr;
129 }
130
131 struct paged_context {
132         struct ldb_module *module;
133         void *up_context;
134         int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
135
136         int size;
137
138         struct results_store *store;
139 };
140
141 static struct ldb_handle *init_handle(void *mem_ctx, struct ldb_module *module,
142                                             void *context,
143                                             int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
144 {
145         struct paged_context *ac;
146         struct ldb_handle *h;
147
148         h = talloc_zero(mem_ctx, struct ldb_handle);
149         if (h == NULL) {
150                 ldb_set_errstring(module->ldb, "Out of Memory");
151                 return NULL;
152         }
153
154         h->module = module;
155
156         ac = talloc_zero(h, struct paged_context);
157         if (ac == NULL) {
158                 ldb_set_errstring(module->ldb, "Out of Memory");
159                 talloc_free(h);
160                 return NULL;
161         }
162
163         h->private_data = (void *)ac;
164
165         h->state = LDB_ASYNC_INIT;
166         h->status = LDB_SUCCESS;
167
168         ac->module = module;
169         ac->up_context = context;
170         ac->up_callback = callback;
171
172         return h;
173 }
174
175 static int paged_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
176 {
177         struct paged_context *ac = NULL;
178
179         if (!context || !ares) {
180                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
181                 goto error;
182         }
183
184         ac = talloc_get_type(context, struct paged_context);
185
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);
189                 } else {
190                         ac->store->last->next = talloc(ac->store, struct message_store);
191                         ac->store->last = ac->store->last->next;
192                 }
193                 if (ac->store->last == NULL) {
194                         goto error;
195                 }
196
197                 ac->store->num_entries++;
198
199                 ac->store->last->r = talloc_steal(ac->store->last, ares);
200                 ac->store->last->next = NULL;
201         }
202
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);
206                 } else {
207                         ac->store->last_ref->next = talloc(ac->store, struct message_store);
208                         ac->store->last_ref = ac->store->last_ref->next;
209                 }
210                 if (ac->store->last_ref == NULL) {
211                         goto error;
212                 }
213
214                 ac->store->last_ref->r = talloc_steal(ac->store->last, ares);
215                 ac->store->last_ref->next = NULL;
216         }
217
218         if (ares->type == LDB_REPLY_DONE) {
219                 ac->store->controls = talloc_move(ac->store, &ares->controls);
220                 talloc_free(ares);
221         }
222
223         return LDB_SUCCESS;
224
225 error:
226         talloc_free(ares);
227         return LDB_ERR_OPERATIONS_ERROR;
228 }
229
230 static int paged_search(struct ldb_module *module, struct ldb_request *req)
231 {
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;
238         int ret;
239
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);
245         }
246
247         private_data = talloc_get_type(module->private_data, struct private_data);
248
249         req->handle = NULL;
250
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;
255         }
256         
257         paged_ctrl = talloc_get_type(control->data, struct ldb_paged_control);
258         if (!paged_ctrl) {
259                 return LDB_ERR_PROTOCOL_ERROR;
260         }
261
262         h = init_handle(req, module, req->context, req->callback);
263         if (!h) {
264                 return LDB_ERR_OPERATIONS_ERROR;
265         }
266         ac = talloc_get_type(h->private_data, struct paged_context);
267
268         ac->size = paged_ctrl->size;
269
270         /* check if it is a continuation search the store */
271         if (paged_ctrl->cookie_len == 0) {
272                 
273                 ac->store = new_store(private_data);
274                 if (ac->store == NULL) {
275                         talloc_free(h);
276                         return LDB_ERR_UNWILLING_TO_PERFORM;
277                 }
278
279                 ac->store->req = talloc(ac->store, struct ldb_request);
280                 if (!ac->store->req)
281                         return LDB_ERR_OPERATIONS_ERROR;
282
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;
289
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;
295                 }
296
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);
300
301                 ret = ldb_next_request(module, ac->store->req);
302
303         } else {
304                 struct results_store *current = NULL;
305
306                 for (current = private_data->store; current; current = current->next) {
307                         if (strcmp(current->cookie, paged_ctrl->cookie) == 0) {
308                                 current->timestamp = time(NULL);
309                                 break;
310                         }
311                 }
312                 if (current == NULL) {
313                         talloc_free(h);
314                         return LDB_ERR_UNWILLING_TO_PERFORM;
315                 }
316
317                 ac->store = current;
318                 ret = LDB_SUCCESS;
319         }
320
321         req->handle = h;
322
323         /* check if it is an abandon */
324         if (ac->size == 0) {
325                 talloc_free(ac->store);
326                 h->status = LDB_SUCCESS;
327                 h->state = LDB_ASYNC_DONE;
328                 return LDB_SUCCESS;
329         }
330
331         /* TODO: age out old outstanding requests */
332
333         return ret;
334
335 }
336
337 static int paged_results(struct ldb_handle *handle)
338 {
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;
344
345         ac = talloc_get_type(handle->private_data, struct paged_context);
346
347         if (ac->store == NULL)
348                 return LDB_ERR_OPERATIONS_ERROR;
349
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;
356                         return ret;
357                 }
358
359                 ac->store->first = msg->next;
360                 talloc_free(msg);
361                 ac->store->num_entries--;
362                 ac->size--;
363         }
364
365         handle->state = LDB_ASYNC_DONE;
366
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;
373                         return ret;
374                 }
375
376                 ac->store->first_ref = msg->next;
377                 talloc_free(msg);
378         }
379
380         ares = talloc_zero(ac->store, struct ldb_reply);
381         if (ares == NULL) {
382                 handle->status = LDB_ERR_OPERATIONS_ERROR;
383                 return handle->status;
384         }
385         num_ctrls = 2;
386         i = 0;
387
388         if (ac->store->controls != NULL) {
389                 ares->controls = ac->store->controls;
390                 while (ares->controls[i]) i++; /* counting */
391
392                 ares->controls = talloc_move(ares, &ac->store->controls);
393                 num_ctrls += i;
394         }
395
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;
400         }
401
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;
406         }
407
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;
412         }
413                 
414         ares->controls[i]->critical = 0;
415         ares->controls[i + 1] = NULL;
416
417         paged = talloc(ares->controls[i], struct ldb_paged_control);
418         if (paged == NULL) {
419                 handle->status = LDB_ERR_OPERATIONS_ERROR;
420                 return handle->status;
421         }
422         
423         ares->controls[i]->data = paged;
424
425         if (ac->size > 0) {
426                 paged->size = 0;
427                 paged->cookie = NULL;
428                 paged->cookie_len = 0;
429         } else {
430                 paged->size = ac->store->num_entries;
431                 paged->cookie = talloc_strdup(paged, ac->store->cookie);
432                 paged->cookie_len = strlen(paged->cookie) + 1;
433         }
434
435         ares->type = LDB_REPLY_DONE;
436
437         ret = ac->up_callback(ac->module->ldb, ac->up_context, ares);
438
439         handle->status = ret;
440
441         return ret;
442 }
443
444 static int paged_wait(struct ldb_handle *handle, enum ldb_wait_type type)
445 {
446         struct paged_context *ac;
447         int ret;
448     
449         if (!handle || !handle->private_data) {
450                 return LDB_ERR_OPERATIONS_ERROR;
451         }
452
453         if (handle->state == LDB_ASYNC_DONE) {
454                 return handle->status;
455         }
456
457         handle->state = LDB_ASYNC_PENDING;
458
459         ac = talloc_get_type(handle->private_data, struct paged_context);
460
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);
465
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);
470                 }
471
472                 return ret;
473         }
474
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;
481                                 return ret;
482                         }
483                 }
484
485                 ret = paged_results(handle);
486
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);
491                 }
492
493                 return ret;
494         }
495
496         ret = ldb_wait(ac->store->req->handle, type);
497         if (ret != LDB_SUCCESS) {
498                 handle->state = LDB_ASYNC_DONE;
499                 handle->status = ret;
500                 return ret;
501         }
502
503         handle->status = ret;
504
505         if (ac->store->num_entries >= ac->size ||
506             ac->store->req->handle->state == LDB_ASYNC_DONE) {
507
508                 ret = paged_results(handle);
509
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);
514                 }
515         }
516
517         return ret;
518 }
519
520 static int paged_request_init(struct ldb_module *module)
521 {
522         struct private_data *data;
523         struct ldb_request *req;
524         int ret;
525
526         data = talloc(module, struct private_data);
527         if (data == NULL) {
528                 return LDB_ERR_OTHER;
529         }
530         
531         data->next_free_id = 1;
532         data->store = NULL;
533         module->private_data = data;
534
535         req = talloc(module, struct ldb_request);
536         if (req == NULL) {
537                 return LDB_ERR_OPERATIONS_ERROR;
538         }
539
540         req->operation = LDB_REQ_REGISTER_CONTROL;
541         req->op.reg_control.oid = LDB_CONTROL_PAGED_RESULTS_OID;
542         req->controls = NULL;
543
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");
547         }
548
549         talloc_free(req);
550         return ldb_next_init(module);
551 }
552
553 static const struct ldb_module_ops paged_ops = {
554         .name           = "paged_results",
555         .search         = paged_search,
556         .wait           = paged_wait,
557         .init_context   = paged_request_init
558 };
559
560 int ldb_paged_results_init(void)
561 {
562         return ldb_register_module(&paged_ops);
563 }
564