Until the new ldb changes land, make ldb_wait set the error string.
[kai/samba.git] / source / lib / ldb / modules / paged_searches.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, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: paged_searches
26  *
27  *  Component: ldb paged searches module
28  *
29  *  Description: this module detects if the remote ldap server supports
30  *               paged results and use them to transparently access all objects
31  *
32  *  Author: Simo Sorce
33  */
34
35 #include "includes.h"
36 #include "ldb_includes.h"
37
38 #define PS_DEFAULT_PAGE_SIZE 500
39 /* 500 objects per query seem to be a decent compromise
40  * the default AD limit per request is 1000 entries */
41
42 struct private_data {
43
44         bool paged_supported;
45 };
46
47 struct ps_context {
48         struct ldb_module *module;
49         void *up_context;
50         int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
51
52         struct ldb_request *orig_req;
53
54         struct ldb_request *new_req;
55
56         bool pending;
57
58         char **saved_referrals;
59         int num_referrals;
60 };
61
62 static struct ldb_handle *init_handle(void *mem_ctx, struct ldb_module *module,
63                                             void *context,
64                                             int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
65 {
66         struct ps_context *ac;
67         struct ldb_handle *h;
68
69         h = talloc_zero(mem_ctx, struct ldb_handle);
70         if (h == NULL) {
71                 ldb_set_errstring(module->ldb, "Out of Memory");
72                 return NULL;
73         }
74
75         h->module = module;
76
77         ac = talloc_zero(h, struct ps_context);
78         if (ac == NULL) {
79                 ldb_set_errstring(module->ldb, "Out of Memory");
80                 talloc_free(h);
81                 return NULL;
82         }
83
84         h->private_data = (void *)ac;
85
86         h->state = LDB_ASYNC_INIT;
87         h->status = LDB_SUCCESS;
88
89         ac->module = module;
90         ac->up_context = context;
91         ac->up_callback = callback;
92
93         ac->pending = false;
94
95
96
97         ac->saved_referrals = NULL;
98         ac->num_referrals = 0;
99
100         return h;
101 }
102
103 static int check_ps_continuation(struct ldb_reply *ares, struct ps_context *ac)
104 {
105         struct ldb_paged_control *rep_control, *req_control;
106
107         /* look up our paged control */
108         if (!ares->controls || strcmp(LDB_CONTROL_PAGED_RESULTS_OID, ares->controls[0]->oid) != 0) {
109                 /* something wrong here */
110                 return LDB_ERR_OPERATIONS_ERROR;
111         }
112
113         rep_control = talloc_get_type(ares->controls[0]->data, struct ldb_paged_control);
114         if (rep_control->cookie_len == 0) {
115                 /* we are done */
116                 ac->pending = false;
117                 return LDB_SUCCESS;
118         }
119
120         /* more processing required */
121         /* let's fill in the request control with the new cookie */
122         /* if there's a reply control we must find a request
123          * control matching it */
124
125         if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, ac->new_req->controls[0]->oid) != 0) {
126                 /* something wrong here */
127                 return LDB_ERR_OPERATIONS_ERROR;
128         }
129
130         req_control = talloc_get_type(ac->new_req->controls[0]->data, struct ldb_paged_control);
131
132         if (req_control->cookie) {
133                 talloc_free(req_control->cookie);
134         }
135
136         req_control->cookie = talloc_memdup(req_control,
137                                             rep_control->cookie,
138                                             rep_control->cookie_len);
139         req_control->cookie_len = rep_control->cookie_len;
140
141         ac->pending = true;
142         return LDB_SUCCESS;
143 }
144
145 static int store_referral(char *referral, struct ps_context *ac)
146 {
147         ac->saved_referrals = talloc_realloc(ac, ac->saved_referrals, char *, ac->num_referrals + 2);
148         if (!ac->saved_referrals) {
149                 return LDB_ERR_OPERATIONS_ERROR;
150         }
151
152         ac->saved_referrals[ac->num_referrals] = talloc_strdup(ac->saved_referrals, referral);
153         if (!ac->saved_referrals[ac->num_referrals]) {
154                 return LDB_ERR_OPERATIONS_ERROR;
155         }
156
157         ac->num_referrals++;
158         ac->saved_referrals[ac->num_referrals] = NULL;
159
160         return LDB_SUCCESS;
161 }
162
163 static int send_referrals(struct ldb_context *ldb, struct ps_context *ac)
164 {
165         struct ldb_reply *ares;
166         int i;
167
168         for (i = 0; i < ac->num_referrals; i++) {
169                 ares = talloc_zero(ac, struct ldb_reply);
170                 if (!ares) {
171                         return LDB_ERR_OPERATIONS_ERROR;
172                 }
173
174                 ares->type = LDB_REPLY_REFERRAL;
175                 ares->referral = ac->saved_referrals[i];
176
177                 ac->up_callback(ldb, ac->up_context, ares);
178         }
179
180         return LDB_SUCCESS;
181 }
182
183 static int ps_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
184 {
185         struct ps_context *ac = NULL;
186         int ret = LDB_ERR_OPERATIONS_ERROR;
187
188         if (!context || !ares) {
189                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
190                 goto error;
191         }
192
193         ac = talloc_get_type(context, struct ps_context);
194
195         switch (ares->type) {
196         case LDB_REPLY_ENTRY:
197                 ac->up_callback(ldb, ac->up_context, ares);
198                 break;
199
200         case LDB_REPLY_REFERRAL:
201                 ret = store_referral(ares->referral, ac);
202                 if (ret != LDB_SUCCESS) {
203                         goto error;
204                 }
205                 break;
206
207         case LDB_REPLY_DONE:
208                 ret = check_ps_continuation(ares, ac);
209                 if (ret != LDB_SUCCESS) {
210                         goto error;
211                 }
212                 if (!ac->pending) {
213                         /* send referrals */
214                         ret = send_referrals(ldb, ac);
215                         if (ret != LDB_SUCCESS) {
216                                 goto error;
217                         }
218
219                         /* send REPLY_DONE */
220                         ac->up_callback(ldb, ac->up_context, ares);
221                 }
222                 break;
223         default:
224                 goto error;
225         }
226
227         return LDB_SUCCESS;
228
229 error:
230         talloc_free(ares);
231         return ret;
232 }
233
234 static int ps_search(struct ldb_module *module, struct ldb_request *req)
235 {
236         struct private_data *private_data;
237         struct ldb_paged_control *control;
238         struct ps_context *ac;
239         struct ldb_handle *h;
240
241         private_data = talloc_get_type(module->private_data, struct private_data);
242
243         /* check if paging is supported and if there is a any control */
244         if (!private_data || !private_data->paged_supported || req->controls) {
245                 /* do not touch this request paged controls not
246                  * supported or explicit controls have been set or we
247                  * are just not setup yet */
248                 return ldb_next_request(module, req);
249         }
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         h = init_handle(req, module, req->context, req->callback);
258         if (!h) {
259                 return LDB_ERR_OPERATIONS_ERROR;
260         }
261         ac = talloc_get_type(h->private_data, struct ps_context);
262
263         ac->new_req = talloc(ac, struct ldb_request);
264         if (!ac->new_req) return LDB_ERR_OPERATIONS_ERROR;
265
266         ac->new_req->controls = talloc_array(ac->new_req, struct ldb_control *, 2);
267         if (!ac->new_req->controls) return LDB_ERR_OPERATIONS_ERROR;
268
269         ac->new_req->controls[0] = talloc(ac->new_req->controls, struct ldb_control);
270         if (!ac->new_req->controls[0]) return LDB_ERR_OPERATIONS_ERROR;
271
272         control = talloc(ac->new_req->controls[0], struct ldb_paged_control);
273         if (!control) return LDB_ERR_OPERATIONS_ERROR;
274
275         control->size = PS_DEFAULT_PAGE_SIZE;
276         control->cookie = NULL;
277         control->cookie_len = 0;
278
279         ac->new_req->controls[0]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
280         ac->new_req->controls[0]->critical = 1;
281         ac->new_req->controls[0]->data = control;
282
283         ac->new_req->controls[1] = NULL;
284
285         ac->new_req->operation = req->operation;
286         ac->new_req->op.search.base = req->op.search.base;
287         ac->new_req->op.search.scope = req->op.search.scope;
288         ac->new_req->op.search.tree = req->op.search.tree;
289         ac->new_req->op.search.attrs = req->op.search.attrs;
290         ac->new_req->context = ac;
291         ac->new_req->callback = ps_callback;
292         ldb_set_timeout_from_prev_req(module->ldb, req, ac->new_req);
293
294         req->handle = h;
295
296         return ldb_next_request(module, ac->new_req);
297 }
298
299 static int ps_continuation(struct ldb_handle *handle)
300 {
301         struct ps_context *ac;
302
303         if (!handle || !handle->private_data) {
304                 return LDB_ERR_OPERATIONS_ERROR;
305         }
306
307         ac = talloc_get_type(handle->private_data, struct ps_context);
308
309         /* reset the requests handle */
310         ac->new_req->handle = NULL;
311
312         return ldb_next_request(handle->module, ac->new_req);
313 }
314
315 static int ps_wait_once(struct ldb_handle *handle)
316 {
317         struct ps_context *ac;
318         int ret;
319     
320         if (!handle || !handle->private_data) {
321                 return LDB_ERR_OPERATIONS_ERROR;
322         }
323
324         if (handle->state == LDB_ASYNC_DONE) {
325                 return handle->status;
326         }
327
328         handle->state = LDB_ASYNC_PENDING;
329         handle->status = LDB_SUCCESS;
330
331         ac = talloc_get_type(handle->private_data, struct ps_context);
332
333         ret = ldb_wait(ac->new_req->handle, LDB_WAIT_NONE);
334
335         if (ret != LDB_SUCCESS) {
336                 handle->status = ret;
337                 goto done;
338         }
339
340         if (ac->new_req->handle->status != LDB_SUCCESS) {
341                 handle->status = ac->new_req->handle->status;
342                 goto done;
343         }
344
345         if (ac->new_req->handle->state != LDB_ASYNC_DONE) {
346                 return LDB_SUCCESS;
347         } 
348
349         /* see if we need to send another request for the next batch */
350         if (ac->pending) {
351                 ret = ps_continuation(handle);
352                 if (ret != LDB_SUCCESS) {
353                         handle->status = ret;
354                         goto done;
355                 }
356
357                 /* continue the search with the next request */
358                 return LDB_SUCCESS;
359         }
360
361         ret = LDB_SUCCESS;
362
363 done:
364         handle->state = LDB_ASYNC_DONE;
365         return ret;
366 }
367
368 static int ps_wait(struct ldb_handle *handle, enum ldb_wait_type type)
369 {
370         int ret;
371
372         if (!handle || !handle->private_data) {
373                 return LDB_ERR_OPERATIONS_ERROR;
374         }
375
376         if (type == LDB_WAIT_ALL) {
377                 while (handle->state != LDB_ASYNC_DONE) {
378                         ret = ps_wait_once(handle);
379                         if (ret != LDB_SUCCESS) {
380                                 return ret;
381                         }
382                 }
383
384                 return handle->status;
385         }
386         return ps_wait_once(handle);
387 }
388
389 static int check_supported_paged(struct ldb_context *ldb, void *context, 
390                                  struct ldb_reply *ares) 
391 {
392         struct private_data *data;
393         data = talloc_get_type(context,
394                                struct private_data);
395         if (ares->type == LDB_REPLY_ENTRY) {
396                 if (ldb_msg_check_string_attribute(ares->message,
397                                                    "supportedControl",
398                                                    LDB_CONTROL_PAGED_RESULTS_OID)) {
399                         data->paged_supported = true;
400                 }
401         }
402         return LDB_SUCCESS;
403 }
404
405
406 static int ps_init(struct ldb_module *module)
407 {
408         static const char *attrs[] = { "supportedControl", NULL };
409         struct private_data *data;
410         int ret;
411         struct ldb_request *req;
412
413         data = talloc(module, struct private_data);
414         if (data == NULL) {
415                 ldb_set_errstring(module->ldb, "Out of Memory");
416                 return LDB_ERR_OTHER;
417         }
418         module->private_data = data;
419         data->paged_supported = false;
420
421         req = talloc(module, struct ldb_request);
422         if (req == NULL) {
423                 ldb_set_errstring(module->ldb, "Out of Memory");
424                 return LDB_ERR_OPERATIONS_ERROR;
425         }
426
427         req->operation = LDB_SEARCH;
428         req->op.search.base = ldb_dn_new(req, module->ldb, "");
429         req->op.search.scope = LDB_SCOPE_BASE;
430
431         req->op.search.tree = ldb_parse_tree(req, "objectClass=*");
432         if (req->op.search.tree == NULL) {
433                 ldb_set_errstring(module->ldb, "Unable to parse search expression");
434                 talloc_free(req);
435                 return LDB_ERR_OPERATIONS_ERROR;
436         }
437
438         req->op.search.attrs = attrs;
439         req->controls = NULL;
440         req->context = data;
441         req->callback = check_supported_paged;
442         ldb_set_timeout(module->ldb, req, 0); /* use default timeout */
443
444         ret = ldb_next_request(module, req);
445         
446         if (ret == LDB_SUCCESS) {
447                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
448         }
449         
450         talloc_free(req);
451         if (ret != LDB_SUCCESS) {
452                 return ret;
453         }
454
455         return ldb_next_init(module);
456 }
457
458 static const struct ldb_module_ops ps_ops = {
459         .name           = "paged_searches",
460         .search         = ps_search,
461         .wait           = ps_wait,
462         .init_context   = ps_init
463 };
464
465 int ldb_paged_searches_init(void)
466 {
467         return ldb_register_module(&ps_ops);
468 }
469