56f9b1cac3d6cbffaad59c736dc8fd7de19a0266
[metze/samba/wip.git] / source4 / lib / ldb / modules / paged_searches.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2005-2008
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         struct ldb_request *req;
50
51         bool pending;
52
53         char **saved_referrals;
54         int num_referrals;
55 };
56
57 static int check_ps_continuation(struct ldb_request *req, struct ldb_reply *ares)
58 {
59         struct ps_context *ac;
60         struct ldb_paged_control *rep_control, *req_control;
61
62         ac = talloc_get_type(req->context, struct ps_context);
63
64         /* look up our paged control */
65         if (!ares->controls || strcmp(LDB_CONTROL_PAGED_RESULTS_OID, ares->controls[0]->oid) != 0) {
66                 /* something wrong here */
67                 return LDB_ERR_OPERATIONS_ERROR;
68         }
69
70         rep_control = talloc_get_type(ares->controls[0]->data, struct ldb_paged_control);
71         if (rep_control->cookie_len == 0) {
72                 /* we are done */
73                 ac->pending = false;
74                 return LDB_SUCCESS;
75         }
76
77         /* more processing required */
78         /* let's fill in the request control with the new cookie */
79         /* if there's a reply control we must find a request
80          * control matching it */
81
82         if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, req->controls[0]->oid) != 0) {
83                 /* something wrong here */
84                 return LDB_ERR_OPERATIONS_ERROR;
85         }
86
87         req_control = talloc_get_type(req->controls[0]->data, struct ldb_paged_control);
88
89         if (req_control->cookie) {
90                 talloc_free(req_control->cookie);
91         }
92
93         req_control->cookie = talloc_memdup(req_control,
94                                             rep_control->cookie,
95                                             rep_control->cookie_len);
96         req_control->cookie_len = rep_control->cookie_len;
97
98         ac->pending = true;
99         return LDB_SUCCESS;
100 }
101
102 static int store_referral(struct ps_context *ac, char *referral)
103 {
104         ac->saved_referrals = talloc_realloc(ac, ac->saved_referrals, char *, ac->num_referrals + 2);
105         if (!ac->saved_referrals) {
106                 return LDB_ERR_OPERATIONS_ERROR;
107         }
108
109         ac->saved_referrals[ac->num_referrals] = talloc_strdup(ac->saved_referrals, referral);
110         if (!ac->saved_referrals[ac->num_referrals]) {
111                 return LDB_ERR_OPERATIONS_ERROR;
112         }
113
114         ac->num_referrals++;
115         ac->saved_referrals[ac->num_referrals] = NULL;
116
117         return LDB_SUCCESS;
118 }
119
120 static int send_referrals(struct ps_context *ac)
121 {
122         struct ldb_reply *ares;
123         int ret;
124         int i;
125
126         for (i = 0; i < ac->num_referrals; i++) {
127                 ares = talloc_zero(ac->req, struct ldb_reply);
128                 if (!ares) {
129                         return LDB_ERR_OPERATIONS_ERROR;
130                 }
131
132                 ares->type = LDB_REPLY_REFERRAL;
133                 ares->referral = ac->saved_referrals[i];
134
135                 ret = ldb_module_send_referral(ac->req, ares->referral);
136                 if (ret != LDB_SUCCESS) {
137                         return ret;
138                 }
139         }
140
141         return LDB_SUCCESS;
142 }
143
144 static int ps_next_request(struct ps_context *ac);
145
146 static int ps_callback(struct ldb_request *req, struct ldb_reply *ares)
147 {
148         struct ps_context *ac;
149         int ret;
150
151         ac = talloc_get_type(req->context, struct ps_context);
152
153         if (!ares) {
154                 return ldb_module_done(ac->req, NULL, NULL,
155                                         LDB_ERR_OPERATIONS_ERROR);
156         }
157         if (ares->error != LDB_SUCCESS) {
158                 return ldb_module_done(ac->req, ares->controls,
159                                         ares->response, ares->error);
160         }
161
162         switch (ares->type) {
163         case LDB_REPLY_ENTRY:
164                 ret = ldb_module_send_entry(ac->req, ares->message, ares->controls);
165                 if (ret != LDB_SUCCESS) {
166                         return ldb_module_done(ac->req, NULL, NULL, ret);
167                 }
168                 break;
169
170         case LDB_REPLY_REFERRAL:
171                 ret = store_referral(ac, ares->referral);
172                 if (ret != LDB_SUCCESS) {
173                         return ldb_module_done(ac->req, NULL, NULL, ret);
174                 }
175                 break;
176
177         case LDB_REPLY_DONE:
178
179                 ret = check_ps_continuation(req, ares);
180                 if (ret != LDB_SUCCESS) {
181                         return ldb_module_done(ac->req, NULL, NULL, ret);
182                 }
183
184                 if (ac->pending) {
185
186                         ret = ps_next_request(ac);
187                         if (ret != LDB_SUCCESS) {
188                                 return ldb_module_done(ac->req,
189                                                         NULL, NULL, ret);
190                         }
191
192                 } else {
193
194                         /* send referrals */
195                         ret = send_referrals(ac);
196                         if (ret != LDB_SUCCESS) {
197                                 return ldb_module_done(ac->req,
198                                                         NULL, NULL, ret);
199                         }
200
201                         /* send REPLY_DONE */
202                         return ldb_module_done(ac->req, ares->controls,
203                                                 ares->response, LDB_SUCCESS);
204                 }
205                 break;
206         }
207
208         talloc_free(ares);
209         return LDB_SUCCESS;
210 }
211
212 static int ps_search(struct ldb_module *module, struct ldb_request *req)
213 {
214         struct private_data *private_data;
215         struct ps_context *ac;
216
217         private_data = talloc_get_type(module->private_data, struct private_data);
218
219         /* check if paging is supported and if there is a any control */
220         if (!private_data || !private_data->paged_supported || req->controls) {
221                 /* do not touch this request paged controls not
222                  * supported or explicit controls have been set or we
223                  * are just not setup yet */
224                 return ldb_next_request(module, req);
225         }
226
227         ac = talloc_zero(req, struct ps_context);
228         if (ac == NULL) {
229                 ldb_oom(module->ldb);
230                 return LDB_ERR_OPERATIONS_ERROR;
231         }
232
233         ac->module = module;
234         ac->req = req;
235         ac->pending = false;
236         ac->saved_referrals = NULL;
237         ac->num_referrals = 0;
238
239         return ps_next_request(ac);
240 }
241
242 static int ps_next_request(struct ps_context *ac) {
243
244         struct ldb_paged_control *control;
245         struct ldb_control **controls;
246         struct ldb_request *new_req;
247         int ret;
248
249         controls = talloc_array(ac, struct ldb_control *, 2);
250         if (!controls) {
251                 return LDB_ERR_OPERATIONS_ERROR;
252         }
253
254         controls[0] = talloc(controls, struct ldb_control);
255         if (!controls[0]) {
256                 return LDB_ERR_OPERATIONS_ERROR;
257         }
258
259         control = talloc(controls[0], struct ldb_paged_control);
260         if (!control) {
261                 return LDB_ERR_OPERATIONS_ERROR;
262         }
263
264         control->size = PS_DEFAULT_PAGE_SIZE;
265         control->cookie = NULL;
266         control->cookie_len = 0;
267
268         controls[0]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
269         controls[0]->critical = 1;
270         controls[0]->data = control;
271         controls[1] = NULL;
272
273         ret = ldb_build_search_req_ex(&new_req, ac->module->ldb, ac,
274                                         ac->req->op.search.base,
275                                         ac->req->op.search.scope,
276                                         ac->req->op.search.tree,
277                                         ac->req->op.search.attrs,
278                                         controls,
279                                         ac,
280                                         ps_callback,
281                                         ac->req);
282         if (ret != LDB_SUCCESS) {
283                 return ret;
284         }
285         talloc_steal(new_req, controls);
286
287         return ldb_next_request(ac->module, new_req);
288 }
289
290 static int check_supported_paged(struct ldb_request *req,
291                                  struct ldb_reply *ares)
292 {
293         struct private_data *data;
294
295         data = talloc_get_type(req->context, struct private_data);
296
297         if (!ares) {
298                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
299         }
300         if (ares->error != LDB_SUCCESS) {
301                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
302         }
303
304         switch (ares->type) {
305         case LDB_REPLY_ENTRY:
306                 if (ldb_msg_check_string_attribute(ares->message,
307                                                    "supportedControl",
308                                                    LDB_CONTROL_PAGED_RESULTS_OID)) {
309                         data->paged_supported = true;
310                 }
311                 break;
312
313         case LDB_REPLY_REFERRAL:
314                 /* ignore */
315                 break;
316
317         case LDB_REPLY_DONE:
318                 return ldb_request_done(req, LDB_SUCCESS);
319         }
320
321         talloc_free(ares);
322         return LDB_SUCCESS;
323 }
324
325 static int ps_init(struct ldb_module *module)
326 {
327         static const char *attrs[] = { "supportedControl", NULL };
328         struct private_data *data;
329         struct ldb_dn *base;
330         int ret;
331         struct ldb_request *req;
332
333         data = talloc(module, struct private_data);
334         if (data == NULL) {
335                 ldb_oom(module->ldb);
336                 return LDB_ERR_OPERATIONS_ERROR;
337         }
338         module->private_data = data;
339         data->paged_supported = false;
340
341         base = ldb_dn_new(module, module->ldb, "");
342         if (base == NULL) {
343                 ldb_oom(module->ldb);
344                 return LDB_ERR_OPERATIONS_ERROR;
345         }
346         ret = ldb_build_search_req(&req, module->ldb, module,
347                                    base, LDB_SCOPE_BASE,
348                                    "(objectClass=*)",
349                                    attrs, NULL,
350                                    data, check_supported_paged,
351                                    NULL);
352         if (ret != LDB_SUCCESS) {
353                 return ret;
354         }
355
356         ret = ldb_next_request(module, req);
357         if (ret == LDB_SUCCESS) {
358                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
359         }
360         if (ret != LDB_SUCCESS) {
361                 return ret;
362         }
363
364         talloc_free(base);
365         talloc_free(req);
366
367         return ldb_next_init(module);
368 }
369
370 _PUBLIC_ const struct ldb_module_ops ldb_paged_searches_module_ops = {
371         .name           = "paged_searches",
372         .search         = ps_search,
373         .init_context   = ps_init
374 };