r17186: "async" word abuse clean-up part 2
[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 2 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 "includes.h"
37 #include "ldb/include/includes.h"
38
39 struct message_store {
40         /* keep the whole ldb_reply as an optimization
41          * instead of freeing and talloc-ing the container
42          * on each result */
43         struct ldb_reply *r;
44         struct message_store *next;
45 };
46
47 struct results_store {
48         char *cookie;
49         time_t timestamp;
50         int num_sent; /* To be removed */
51         struct ldb_result *result; /* To be removed */
52         struct results_store *prev;
53         struct results_store *next;
54         
55         struct message_store *first;
56         struct message_store *last;
57         int num_entries;
58
59         struct message_store *first_ref;
60         struct message_store *last_ref;
61
62         struct ldb_control **controls;
63
64         struct ldb_request *req;
65 };
66
67 struct private_data {
68
69         int next_free_id;
70         struct results_store *store;
71         
72 };
73
74 int store_destructor(struct results_store *store)
75 {
76         if (store->prev) {
77                 store->prev->next = store->next;
78         }
79         if (store->next) {
80                 store->next->prev = store->prev;
81         }
82         
83         return 0;
84 }
85
86 static struct results_store *new_store(struct private_data *priv)
87 {
88         struct results_store *new;
89         int new_id = priv->next_free_id++;
90
91         /* TODO: we should have a limit on the number of
92          * outstanding paged searches
93          */
94
95         new = talloc(priv, struct results_store);
96         if (!new) return NULL;
97
98         new->cookie = talloc_asprintf(new, "%d", new_id);
99         if (!new->cookie) {
100                 talloc_free(new);
101                 return NULL;
102         }
103
104         new->timestamp = time(NULL);
105
106         new->num_sent = 0; /* To be removed */
107         new->result = NULL; /* To be removed */
108
109         new->first = NULL;
110         new->num_entries = 0;
111         new->first_ref = NULL;
112         new->controls = NULL;
113
114         /* put this entry as first */
115         new->prev = NULL;
116         new->next = priv->store;
117         if (priv->store != NULL) priv->store->prev = new;
118         priv->store = new;
119
120         talloc_set_destructor(new, store_destructor);
121
122         return new;
123 }
124
125 struct paged_context {
126         struct ldb_module *module;
127         void *up_context;
128         int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
129
130         int size;
131
132         struct results_store *store;
133 };
134
135 static struct ldb_handle *init_handle(void *mem_ctx, struct ldb_module *module,
136                                             void *context,
137                                             int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
138 {
139         struct paged_context *ac;
140         struct ldb_handle *h;
141
142         h = talloc_zero(mem_ctx, struct ldb_handle);
143         if (h == NULL) {
144                 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
145                 return NULL;
146         }
147
148         h->module = module;
149
150         ac = talloc_zero(h, struct paged_context);
151         if (ac == NULL) {
152                 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
153                 talloc_free(h);
154                 return NULL;
155         }
156
157         h->private_data = (void *)ac;
158
159         h->state = LDB_ASYNC_INIT;
160         h->status = LDB_SUCCESS;
161
162         ac->module = module;
163         ac->up_context = context;
164         ac->up_callback = callback;
165
166         return h;
167 }
168
169 static int paged_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
170 {
171         struct paged_context *ac = NULL;
172
173         if (!context || !ares) {
174                 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
175                 goto error;
176         }
177
178         ac = talloc_get_type(context, struct paged_context);
179
180         if (ares->type == LDB_REPLY_ENTRY) {
181                 if (ac->store->first == NULL) {
182                         ac->store->first = ac->store->last = talloc(ac->store, struct message_store);
183                 } else {
184                         ac->store->last->next = talloc(ac->store, struct message_store);
185                         ac->store->last = ac->store->last->next;
186                 }
187                 if (ac->store->last == NULL) {
188                         goto error;
189                 }
190
191                 ac->store->num_entries++;
192
193                 ac->store->last->r = talloc_steal(ac->store->last, ares);
194                 if (ac->store->last->r == NULL) {
195                         goto error;
196                 }
197                 ac->store->last->next = NULL;
198         }
199
200         if (ares->type == LDB_REPLY_REFERRAL) {
201                 if (ac->store->first_ref == NULL) {
202                         ac->store->first_ref = ac->store->last_ref = talloc(ac->store, struct message_store);
203                 } else {
204                         ac->store->last_ref->next = talloc(ac->store, struct message_store);
205                         ac->store->last_ref = ac->store->last_ref->next;
206                 }
207                 if (ac->store->last_ref == NULL) {
208                         goto error;
209                 }
210
211                 ac->store->last_ref->r = talloc_steal(ac->store->last, ares);
212                 if (ac->store->last_ref->r == NULL) {
213                         goto error;
214                 }
215                 ac->store->last_ref->next = NULL;
216         }
217
218         if (ares->type == LDB_REPLY_DONE) {
219                 if (ares->controls) {
220                         ac->store->controls = talloc_steal(ac->store, ares->controls);
221                         if (! ac->store->controls) {
222                                 goto error;
223                         }
224                 }
225                 talloc_free(ares);
226         }
227
228         return LDB_SUCCESS;
229
230 error:
231         talloc_free(ares);
232         return LDB_ERR_OPERATIONS_ERROR;
233 }
234
235 static int paged_search(struct ldb_module *module, struct ldb_request *req)
236 {
237         struct ldb_control *control;
238         struct private_data *private_data;
239         struct ldb_paged_control *paged_ctrl;
240         struct ldb_control **saved_controls;
241         struct paged_context *ac;
242         struct ldb_handle *h;
243         int ret;
244
245         /* check if there's a paged request control */
246         control = get_control_from_list(req->controls, LDB_CONTROL_PAGED_RESULTS_OID);
247         if (control == NULL) {
248                 /* not found go on */
249                 return ldb_next_request(module, req);
250         }
251
252         private_data = talloc_get_type(module->private_data, struct private_data);
253
254         req->handle = NULL;
255
256         if (!req->callback || !req->context) {
257                 ldb_set_errstring(module->ldb, talloc_asprintf(module,
258                                   "Async interface called with NULL callback function or NULL context"));
259                 return LDB_ERR_OPERATIONS_ERROR;
260         }
261         
262         paged_ctrl = talloc_get_type(control->data, struct ldb_paged_control);
263         if (!paged_ctrl) {
264                 return LDB_ERR_PROTOCOL_ERROR;
265         }
266
267         h = init_handle(req, module, req->context, req->callback);
268         if (!h) {
269                 return LDB_ERR_OPERATIONS_ERROR;
270         }
271         ac = talloc_get_type(h->private_data, struct paged_context);
272
273         ac->size = paged_ctrl->size;
274
275         /* check if it is a continuation search the store */
276         if (paged_ctrl->cookie_len == 0) {
277                 
278                 ac->store = new_store(private_data);
279                 if (ac->store == NULL) {
280                         talloc_free(h);
281                         return LDB_ERR_UNWILLING_TO_PERFORM;
282                 }
283
284                 ac->store->req = talloc(ac->store, struct ldb_request);
285                 if (!ac->store->req)
286                         return LDB_ERR_OPERATIONS_ERROR;
287
288                 ac->store->req->operation = req->operation;
289                 ac->store->req->op.search.base = req->op.search.base;
290                 ac->store->req->op.search.scope = req->op.search.scope;
291                 ac->store->req->op.search.tree = req->op.search.tree;
292                 ac->store->req->op.search.attrs = req->op.search.attrs;
293                 ac->store->req->controls = req->controls;
294
295                 /* save it locally and remove it from the list */
296                 /* we do not need to replace them later as we
297                  * are keeping the original req intact */
298                 if (!save_controls(control, ac->store->req, &saved_controls)) {
299                         return LDB_ERR_OPERATIONS_ERROR;
300                 }
301
302                 ac->store->req->context = ac;
303                 ac->store->req->callback = paged_search_callback;
304                 ldb_set_timeout_from_prev_req(module->ldb, req, ac->store->req);
305
306                 ret = ldb_next_request(module, ac->store->req);
307
308         } else {
309                 struct results_store *current = NULL;
310
311                 for (current = private_data->store; current; current = current->next) {
312                         if (strcmp(current->cookie, paged_ctrl->cookie) == 0) {
313                                 current->timestamp = time(NULL);
314                                 break;
315                         }
316                 }
317                 if (current == NULL) {
318                         talloc_free(h);
319                         return LDB_ERR_UNWILLING_TO_PERFORM;
320                 }
321
322                 ac->store = current;
323                 ret = LDB_SUCCESS;
324         }
325
326         req->handle = h;
327
328         /* check if it is an abandon */
329         if (ac->size == 0) {
330                 talloc_free(ac->store);
331                 h->status = LDB_SUCCESS;
332                 h->state = LDB_ASYNC_DONE;
333                 return LDB_SUCCESS;
334         }
335
336         /* TODO: age out old outstanding requests */
337
338         return ret;
339
340 }
341
342 static int paged_results(struct ldb_handle *handle)
343 {
344         struct paged_context *ac;
345         struct ldb_paged_control *paged;
346         struct ldb_reply *ares;
347         struct message_store *msg;
348         int i, num_ctrls, ret;
349
350         ac = talloc_get_type(handle->private_data, struct paged_context);
351
352         if (ac->store == NULL)
353                 return LDB_ERR_OPERATIONS_ERROR;
354
355         while (ac->store->num_entries > 0 && ac->size > 0) {
356                 msg = ac->store->first;
357                 ret = ac->up_callback(ac->module->ldb, ac->up_context, msg->r);
358                 if (ret != LDB_SUCCESS) {
359                         handle->status = ret;
360                         handle->state = LDB_ASYNC_DONE;
361                         return ret;
362                 }
363
364                 ac->store->first = msg->next;
365                 talloc_free(msg);
366                 ac->store->num_entries--;
367                 ac->size--;
368         }
369
370         handle->state = LDB_ASYNC_DONE;
371
372         while (ac->store->first_ref != NULL) {
373                 msg = ac->store->first_ref;
374                 ret = ac->up_callback(ac->module->ldb, ac->up_context, msg->r);
375                 if (ret != LDB_SUCCESS) {
376                         handle->status = ret;
377                         handle->state = LDB_ASYNC_DONE;
378                         return ret;
379                 }
380
381                 ac->store->first_ref = msg->next;
382                 talloc_free(msg);
383         }
384
385         ares = talloc_zero(ac->store, struct ldb_reply);
386         if (ares == NULL) {
387                 handle->status = LDB_ERR_OPERATIONS_ERROR;
388                 return handle->status;
389         }
390         num_ctrls = 2;
391         i = 0;
392
393         if (ac->store->controls != NULL) {
394                 ares->controls = ac->store->controls;
395                 while (ares->controls[i]) i++; /* counting */
396
397                 ares->controls = talloc_steal(ares, ac->store->controls);
398                 num_ctrls += i;
399         }
400
401         ares->controls = talloc_realloc(ares, ares->controls, struct ldb_control *, num_ctrls);
402         if (ares->controls == NULL) {
403                 handle->status = LDB_ERR_OPERATIONS_ERROR;
404                 return handle->status;
405         }
406
407         ares->controls[i] = talloc(ares->controls, struct ldb_control);
408         if (ares->controls[i] == NULL) {
409                 handle->status = LDB_ERR_OPERATIONS_ERROR;
410                 return handle->status;
411         }
412
413         ares->controls[i]->oid = talloc_strdup(ares->controls[i], LDB_CONTROL_PAGED_RESULTS_OID);
414         if (ares->controls[i]->oid == NULL) {
415                 handle->status = LDB_ERR_OPERATIONS_ERROR;
416                 return handle->status;
417         }
418                 
419         ares->controls[i]->critical = 0;
420         ares->controls[i + 1] = NULL;
421
422         paged = talloc(ares->controls[i], struct ldb_paged_control);
423         if (paged == NULL) {
424                 handle->status = LDB_ERR_OPERATIONS_ERROR;
425                 return handle->status;
426         }
427         
428         ares->controls[i]->data = paged;
429
430         if (ac->size > 0) {
431                 paged->size = 0;
432                 paged->cookie = NULL;
433                 paged->cookie_len = 0;
434         } else {
435                 paged->size = ac->store->num_entries;
436                 paged->cookie = talloc_strdup(paged, ac->store->cookie);
437                 paged->cookie_len = strlen(paged->cookie) + 1;
438         }
439
440         ares->type = LDB_REPLY_DONE;
441
442         ret = ac->up_callback(ac->module->ldb, ac->up_context, ares);
443
444         handle->status = ret;
445
446         return ret;
447 }
448
449 static int paged_wait(struct ldb_handle *handle, enum ldb_wait_type type)
450 {
451         struct paged_context *ac;
452         int ret;
453     
454         if (!handle || !handle->private_data) {
455                 return LDB_ERR_OPERATIONS_ERROR;
456         }
457
458         if (handle->state == LDB_ASYNC_DONE) {
459                 return handle->status;
460         }
461
462         handle->state = LDB_ASYNC_PENDING;
463
464         ac = talloc_get_type(handle->private_data, struct paged_context);
465
466         if (ac->store->req->handle->state == LDB_ASYNC_DONE) {
467                 /* if lower level is finished we do not need to call it anymore */
468                 /* return all we have until size == 0 or we empty storage */
469                 ret = paged_results(handle);
470
471                 /* we are done, if num_entries is zero free the storage
472                  * as that mean we delivered the last batch */
473                 if (ac->store->num_entries == 0) {
474                         talloc_free(ac->store);
475                 }
476
477                 return ret;
478         }
479
480         if (type == LDB_WAIT_ALL) {
481                 while (ac->store->req->handle->state != LDB_ASYNC_DONE) {
482                         ret = ldb_wait(ac->store->req->handle, type);
483                         if (ret != LDB_SUCCESS) {
484                                 handle->state = LDB_ASYNC_DONE;
485                                 handle->status = ret;
486                                 return ret;
487                         }
488                 }
489
490                 ret = paged_results(handle);
491
492                 /* we are done, if num_entries is zero free the storage
493                  * as that mean we delivered the last batch */
494                 if (ac->store->num_entries == 0) {
495                         talloc_free(ac->store);
496                 }
497
498                 return ret;
499         }
500
501         ret = ldb_wait(ac->store->req->handle, type);
502         if (ret != LDB_SUCCESS) {
503                 handle->state = LDB_ASYNC_DONE;
504                 handle->status = ret;
505                 return ret;
506         }
507
508         handle->status = ret;
509
510         if (ac->store->num_entries >= ac->size ||
511             ac->store->req->handle->state == LDB_ASYNC_DONE) {
512
513                 ret = paged_results(handle);
514
515                 /* we are done, if num_entries is zero free the storage
516                  * as that mean we delivered the last batch */
517                 if (ac->store->num_entries == 0) {
518                         talloc_free(ac->store);
519                 }
520         }
521
522         return ret;
523 }
524
525 static int paged_request_init(struct ldb_module *module)
526 {
527         struct private_data *data;
528         struct ldb_request *req;
529         int ret;
530
531         data = talloc(module, struct private_data);
532         if (data == NULL) {
533                 return LDB_ERR_OTHER;
534         }
535         
536         data->next_free_id = 1;
537         data->store = NULL;
538         module->private_data = data;
539
540         req = talloc(module, struct ldb_request);
541         if (req == NULL) {
542                 return LDB_ERR_OPERATIONS_ERROR;
543         }
544
545         req->operation = LDB_REQ_REGISTER_CONTROL;
546         req->op.reg_control.oid = LDB_CONTROL_PAGED_RESULTS_OID;
547         req->controls = NULL;
548
549         ret = ldb_request(module->ldb, req);
550         if (ret != LDB_SUCCESS) {
551                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "paged_request: Unable to register control with rootdse!\n");
552                 talloc_free(req);
553                 return LDB_ERR_OTHER;
554         }
555
556         talloc_free(req);
557         return ldb_next_init(module);
558 }
559
560 static const struct ldb_module_ops paged_ops = {
561         .name           = "paged_results",
562         .search         = paged_search,
563         .wait           = paged_wait,
564         .init_context   = paged_request_init
565 };
566
567 int ldb_paged_results_init(void)
568 {
569         return ldb_register_module(&paged_ops);
570 }
571