r23703: Start to get Samba4 to again work with LDAP backends, after I turned
[samba.git] / source4 / 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 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_searches
27  *
28  *  Component: ldb paged searches module
29  *
30  *  Description: this module detects if the remote ldap server supports
31  *               paged results and use them to transparently access all objects
32  *
33  *  Author: Simo Sorce
34  */
35
36 #include "includes.h"
37 #include "ldb_includes.h"
38
39 #define PS_DEFAULT_PAGE_SIZE 500
40 /* 500 objects per query seem to be a decent compromise
41  * the default AD limit per request is 1000 entries */
42
43 struct private_data {
44
45         bool paged_supported;
46 };
47
48 struct ps_context {
49         struct ldb_module *module;
50         void *up_context;
51         int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
52
53         struct ldb_request *orig_req;
54
55         struct ldb_request *new_req;
56
57         bool pending;
58
59         char **saved_referrals;
60         int num_referrals;
61 };
62
63 static struct ldb_handle *init_handle(void *mem_ctx, struct ldb_module *module,
64                                             void *context,
65                                             int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
66 {
67         struct ps_context *ac;
68         struct ldb_handle *h;
69
70         h = talloc_zero(mem_ctx, struct ldb_handle);
71         if (h == NULL) {
72                 ldb_set_errstring(module->ldb, "Out of Memory");
73                 return NULL;
74         }
75
76         h->module = module;
77
78         ac = talloc_zero(h, struct ps_context);
79         if (ac == NULL) {
80                 ldb_set_errstring(module->ldb, "Out of Memory");
81                 talloc_free(h);
82                 return NULL;
83         }
84
85         h->private_data = (void *)ac;
86
87         h->state = LDB_ASYNC_INIT;
88         h->status = LDB_SUCCESS;
89
90         ac->module = module;
91         ac->up_context = context;
92         ac->up_callback = callback;
93
94         ac->pending = False;
95         ac->saved_referrals = NULL;
96         ac->num_referrals = 0;
97
98         return h;
99 }
100
101 static int check_ps_continuation(struct ldb_reply *ares, struct ps_context *ac)
102 {
103         struct ldb_paged_control *rep_control, *req_control;
104
105         /* look up our paged control */
106         if (!ares->controls || strcmp(LDB_CONTROL_PAGED_RESULTS_OID, ares->controls[0]->oid) != 0) {
107                 /* something wrong here */
108                 return LDB_ERR_OPERATIONS_ERROR;
109         }
110
111         rep_control = talloc_get_type(ares->controls[0]->data, struct ldb_paged_control);
112         if (rep_control->cookie_len == 0) {
113                 /* we are done */
114                 ac->pending = False;
115                 return LDB_SUCCESS;
116         }
117
118         /* more processing required */
119         /* let's fill in the request control with the new cookie */
120         /* if there's a reply control we must find a request
121          * control matching it */
122
123         if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, ac->new_req->controls[0]->oid) != 0) {
124                 /* something wrong here */
125                 return LDB_ERR_OPERATIONS_ERROR;
126         }
127
128         req_control = talloc_get_type(ac->new_req->controls[0]->data, struct ldb_paged_control);
129
130         if (req_control->cookie) {
131                 talloc_free(req_control->cookie);
132         }
133
134         req_control->cookie = talloc_memdup(req_control,
135                                             rep_control->cookie,
136                                             rep_control->cookie_len);
137         req_control->cookie_len = rep_control->cookie_len;
138
139         ac->pending = True;
140         return LDB_SUCCESS;
141 }
142
143 static int store_referral(char *referral, struct ps_context *ac)
144 {
145         ac->saved_referrals = talloc_realloc(ac, ac->saved_referrals, char *, ac->num_referrals + 2);
146         if (!ac->saved_referrals) {
147                 return LDB_ERR_OPERATIONS_ERROR;
148         }
149
150         ac->saved_referrals[ac->num_referrals] = talloc_strdup(ac->saved_referrals, referral);
151         if (!ac->saved_referrals[ac->num_referrals]) {
152                 return LDB_ERR_OPERATIONS_ERROR;
153         }
154
155         ac->num_referrals++;
156         ac->saved_referrals[ac->num_referrals] = NULL;
157
158         return LDB_SUCCESS;
159 }
160
161 static int send_referrals(struct ldb_context *ldb, struct ps_context *ac)
162 {
163         struct ldb_reply *ares;
164         int i;
165
166         for (i = 0; i < ac->num_referrals; i++) {
167                 ares = talloc_zero(ac, struct ldb_reply);
168                 if (!ares) {
169                         return LDB_ERR_OPERATIONS_ERROR;
170                 }
171
172                 ares->type = LDB_REPLY_REFERRAL;
173                 ares->referral = ac->saved_referrals[i];
174
175                 ac->up_callback(ldb, ac->up_context, ares);
176         }
177
178         return LDB_SUCCESS;
179 }
180
181 static int ps_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
182 {
183         struct ps_context *ac = NULL;
184         int ret = LDB_ERR_OPERATIONS_ERROR;
185
186         if (!context || !ares) {
187                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
188                 goto error;
189         }
190
191         ac = talloc_get_type(context, struct ps_context);
192
193         switch (ares->type) {
194         case LDB_REPLY_ENTRY:
195                 ac->up_callback(ldb, ac->up_context, ares);
196                 break;
197
198         case LDB_REPLY_REFERRAL:
199                 ret = store_referral(ares->referral, ac);
200                 if (ret != LDB_SUCCESS) {
201                         goto error;
202                 }
203                 break;
204
205         case LDB_REPLY_DONE:
206                 ret = check_ps_continuation(ares, ac);
207                 if (ret != LDB_SUCCESS) {
208                         goto error;
209                 }
210                 if (!ac->pending) {
211                         /* send referrals */
212                         ret = send_referrals(ldb, ac);
213                         if (ret != LDB_SUCCESS) {
214                                 goto error;
215                         }
216
217                         /* send REPLY_DONE */
218                         ac->up_callback(ldb, ac->up_context, ares);
219                 }
220                 break;
221         default:
222                 goto error;
223         }
224
225         return LDB_SUCCESS;
226
227 error:
228         talloc_free(ares);
229         return ret;
230 }
231
232 static int ps_search(struct ldb_module *module, struct ldb_request *req)
233 {
234         struct private_data *private_data;
235         struct ldb_paged_control *control;
236         struct ps_context *ac;
237         struct ldb_handle *h;
238
239         private_data = talloc_get_type(module->private_data, struct private_data);
240
241         /* check if paging is supported and if there is a any control */
242         if (!private_data || !private_data->paged_supported || req->controls) {
243                 /* do not touch this request paged controls not
244                  * supported or explicit controls have been set or we
245                  * are just not setup yet */
246                 return ldb_next_request(module, req);
247         }
248
249         if (!req->callback || !req->context) {
250                 ldb_set_errstring(module->ldb,
251                                   "Async interface called with NULL callback function or NULL context");
252                 return LDB_ERR_OPERATIONS_ERROR;
253         }
254         
255         h = init_handle(req, module, req->context, req->callback);
256         if (!h) {
257                 return LDB_ERR_OPERATIONS_ERROR;
258         }
259         ac = talloc_get_type(h->private_data, struct ps_context);
260
261         ac->new_req = talloc(ac, struct ldb_request);
262         if (!ac->new_req) return LDB_ERR_OPERATIONS_ERROR;
263
264         ac->new_req->controls = talloc_array(ac->new_req, struct ldb_control *, 2);
265         if (!ac->new_req->controls) return LDB_ERR_OPERATIONS_ERROR;
266
267         ac->new_req->controls[0] = talloc(ac->new_req->controls, struct ldb_control);
268         if (!ac->new_req->controls[0]) return LDB_ERR_OPERATIONS_ERROR;
269
270         control = talloc(ac->new_req->controls[0], struct ldb_paged_control);
271         if (!control) return LDB_ERR_OPERATIONS_ERROR;
272
273         control->size = PS_DEFAULT_PAGE_SIZE;
274         control->cookie = NULL;
275         control->cookie_len = 0;
276
277         ac->new_req->controls[0]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
278         ac->new_req->controls[0]->critical = 1;
279         ac->new_req->controls[0]->data = control;
280
281         ac->new_req->controls[1] = NULL;
282
283         ac->new_req->operation = req->operation;
284         ac->new_req->op.search.base = req->op.search.base;
285         ac->new_req->op.search.scope = req->op.search.scope;
286         ac->new_req->op.search.tree = req->op.search.tree;
287         ac->new_req->op.search.attrs = req->op.search.attrs;
288         ac->new_req->context = ac;
289         ac->new_req->callback = ps_callback;
290         ldb_set_timeout_from_prev_req(module->ldb, req, ac->new_req);
291
292         req->handle = h;
293
294         return ldb_next_request(module, ac->new_req);
295 }
296
297 static int ps_continuation(struct ldb_handle *handle)
298 {
299         struct ps_context *ac;
300
301         if (!handle || !handle->private_data) {
302                 return LDB_ERR_OPERATIONS_ERROR;
303         }
304
305         ac = talloc_get_type(handle->private_data, struct ps_context);
306
307         /* reset the requests handle */
308         ac->new_req->handle = NULL;
309
310         return ldb_next_request(handle->module, ac->new_req);
311 }
312
313 static int ps_wait_none(struct ldb_handle *handle)
314 {
315         struct ps_context *ac;
316         int ret;
317     
318         if (!handle || !handle->private_data) {
319                 return LDB_ERR_OPERATIONS_ERROR;
320         }
321
322         if (handle->state == LDB_ASYNC_DONE) {
323                 return handle->status;
324         }
325
326         handle->state = LDB_ASYNC_PENDING;
327         handle->status = LDB_SUCCESS;
328
329         ac = talloc_get_type(handle->private_data, struct ps_context);
330
331         ret = ldb_wait(ac->new_req->handle, LDB_WAIT_NONE);
332
333         if (ret != LDB_SUCCESS) {
334                 handle->status = ret;
335                 goto done;
336         }
337
338         if (ac->new_req->handle->status != LDB_SUCCESS) {
339                 handle->status = ac->new_req->handle->status;
340                 goto done;
341         }
342
343         if (ac->new_req->handle->state != LDB_ASYNC_DONE) {
344                 return LDB_SUCCESS;
345         } 
346
347         /* see if we need to send another request for the next batch */
348         if (ac->pending) {
349                 ret = ps_continuation(handle);
350                 if (ret != LDB_SUCCESS) {
351                         handle->status = ret;
352                         goto done;
353                 }
354
355                 /* continue the search with the next request */
356                 return LDB_SUCCESS;
357         }
358
359         ret = LDB_SUCCESS;
360
361 done:
362         handle->state = LDB_ASYNC_DONE;
363         return ret;
364 }
365
366 static int ps_wait_all(struct ldb_handle *handle)
367 {
368         int ret;
369
370         while (handle->state != LDB_ASYNC_DONE) {
371                 ret = ps_wait_none(handle);
372                 if (ret != LDB_SUCCESS) {
373                         return ret;
374                 }
375         }
376
377         return handle->status;
378 }
379
380 static int ps_wait(struct ldb_handle *handle, enum ldb_wait_type type)
381 {
382         if (type == LDB_WAIT_ALL) {
383                 return ps_wait_all(handle);
384         } else {
385                 return ps_wait_none(handle);
386         }
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