4 Copyright (C) Simo Sorce 2005-2006
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
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.
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.
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/>.
25 * Name: paged_searches
27 * Component: ldb paged searches module
29 * Description: this module detects if the remote ldap server supports
30 * paged results and use them to transparently access all objects
36 #include "ldb_includes.h"
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 */
48 struct ldb_module *module;
50 int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
52 struct ldb_request *orig_req;
54 struct ldb_request *new_req;
58 char **saved_referrals;
62 static struct ldb_handle *init_handle(void *mem_ctx, struct ldb_module *module,
64 int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
66 struct ps_context *ac;
69 h = talloc_zero(mem_ctx, struct ldb_handle);
71 ldb_set_errstring(module->ldb, "Out of Memory");
77 ac = talloc_zero(h, struct ps_context);
79 ldb_set_errstring(module->ldb, "Out of Memory");
84 h->private_data = (void *)ac;
86 h->state = LDB_ASYNC_INIT;
87 h->status = LDB_SUCCESS;
90 ac->up_context = context;
91 ac->up_callback = callback;
97 ac->saved_referrals = NULL;
98 ac->num_referrals = 0;
103 static int check_ps_continuation(struct ldb_reply *ares, struct ps_context *ac)
105 struct ldb_paged_control *rep_control, *req_control;
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;
113 rep_control = talloc_get_type(ares->controls[0]->data, struct ldb_paged_control);
114 if (rep_control->cookie_len == 0) {
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 */
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;
130 req_control = talloc_get_type(ac->new_req->controls[0]->data, struct ldb_paged_control);
132 if (req_control->cookie) {
133 talloc_free(req_control->cookie);
136 req_control->cookie = talloc_memdup(req_control,
138 rep_control->cookie_len);
139 req_control->cookie_len = rep_control->cookie_len;
145 static int store_referral(char *referral, struct ps_context *ac)
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;
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;
158 ac->saved_referrals[ac->num_referrals] = NULL;
163 static int send_referrals(struct ldb_context *ldb, struct ps_context *ac)
165 struct ldb_reply *ares;
168 for (i = 0; i < ac->num_referrals; i++) {
169 ares = talloc_zero(ac, struct ldb_reply);
171 return LDB_ERR_OPERATIONS_ERROR;
174 ares->type = LDB_REPLY_REFERRAL;
175 ares->referral = ac->saved_referrals[i];
177 ac->up_callback(ldb, ac->up_context, ares);
183 static int ps_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
185 struct ps_context *ac = NULL;
186 int ret = LDB_ERR_OPERATIONS_ERROR;
188 if (!context || !ares) {
189 ldb_set_errstring(ldb, "NULL Context or Result in callback");
193 ac = talloc_get_type(context, struct ps_context);
195 switch (ares->type) {
196 case LDB_REPLY_ENTRY:
197 ac->up_callback(ldb, ac->up_context, ares);
200 case LDB_REPLY_REFERRAL:
201 ret = store_referral(ares->referral, ac);
202 if (ret != LDB_SUCCESS) {
208 ret = check_ps_continuation(ares, ac);
209 if (ret != LDB_SUCCESS) {
214 ret = send_referrals(ldb, ac);
215 if (ret != LDB_SUCCESS) {
219 /* send REPLY_DONE */
220 ac->up_callback(ldb, ac->up_context, ares);
234 static int ps_search(struct ldb_module *module, struct ldb_request *req)
236 struct private_data *private_data;
237 struct ldb_paged_control *control;
238 struct ps_context *ac;
239 struct ldb_handle *h;
241 private_data = talloc_get_type(module->private_data, struct private_data);
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);
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;
257 h = init_handle(req, module, req->context, req->callback);
259 return LDB_ERR_OPERATIONS_ERROR;
261 ac = talloc_get_type(h->private_data, struct ps_context);
263 ac->new_req = talloc(ac, struct ldb_request);
264 if (!ac->new_req) return LDB_ERR_OPERATIONS_ERROR;
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;
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;
272 control = talloc(ac->new_req->controls[0], struct ldb_paged_control);
273 if (!control) return LDB_ERR_OPERATIONS_ERROR;
275 control->size = PS_DEFAULT_PAGE_SIZE;
276 control->cookie = NULL;
277 control->cookie_len = 0;
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;
283 ac->new_req->controls[1] = NULL;
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);
296 return ldb_next_request(module, ac->new_req);
299 static int ps_continuation(struct ldb_handle *handle)
301 struct ps_context *ac;
303 if (!handle || !handle->private_data) {
304 return LDB_ERR_OPERATIONS_ERROR;
307 ac = talloc_get_type(handle->private_data, struct ps_context);
309 /* reset the requests handle */
310 ac->new_req->handle = NULL;
312 return ldb_next_request(handle->module, ac->new_req);
315 static int ps_wait_once(struct ldb_handle *handle)
317 struct ps_context *ac;
320 if (!handle || !handle->private_data) {
321 return LDB_ERR_OPERATIONS_ERROR;
324 if (handle->state == LDB_ASYNC_DONE) {
325 return handle->status;
328 handle->state = LDB_ASYNC_PENDING;
329 handle->status = LDB_SUCCESS;
331 ac = talloc_get_type(handle->private_data, struct ps_context);
333 ret = ldb_wait(ac->new_req->handle, LDB_WAIT_NONE);
335 if (ret != LDB_SUCCESS) {
336 handle->status = ret;
340 if (ac->new_req->handle->status != LDB_SUCCESS) {
341 handle->status = ac->new_req->handle->status;
345 if (ac->new_req->handle->state != LDB_ASYNC_DONE) {
349 /* see if we need to send another request for the next batch */
351 ret = ps_continuation(handle);
352 if (ret != LDB_SUCCESS) {
353 handle->status = ret;
357 /* continue the search with the next request */
364 handle->state = LDB_ASYNC_DONE;
368 static int ps_wait(struct ldb_handle *handle, enum ldb_wait_type type)
372 if (!handle || !handle->private_data) {
373 return LDB_ERR_OPERATIONS_ERROR;
376 if (type == LDB_WAIT_ALL) {
377 while (handle->state != LDB_ASYNC_DONE) {
378 ret = ps_wait_once(handle);
379 if (ret != LDB_SUCCESS) {
384 return handle->status;
386 return ps_wait_once(handle);
389 static int check_supported_paged(struct ldb_context *ldb, void *context,
390 struct ldb_reply *ares)
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,
398 LDB_CONTROL_PAGED_RESULTS_OID)) {
399 data->paged_supported = true;
406 static int ps_init(struct ldb_module *module)
408 static const char *attrs[] = { "supportedControl", NULL };
409 struct private_data *data;
411 struct ldb_request *req;
413 data = talloc(module, struct private_data);
415 ldb_set_errstring(module->ldb, "Out of Memory");
416 return LDB_ERR_OTHER;
418 module->private_data = data;
419 data->paged_supported = false;
421 req = talloc(module, struct ldb_request);
423 ldb_set_errstring(module->ldb, "Out of Memory");
424 return LDB_ERR_OPERATIONS_ERROR;
427 req->operation = LDB_SEARCH;
428 req->op.search.base = ldb_dn_new(req, module->ldb, "");
429 req->op.search.scope = LDB_SCOPE_BASE;
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");
435 return LDB_ERR_OPERATIONS_ERROR;
438 req->op.search.attrs = attrs;
439 req->controls = NULL;
441 req->callback = check_supported_paged;
442 ldb_set_timeout(module->ldb, req, 0); /* use default timeout */
444 ret = ldb_next_request(module, req);
446 if (ret == LDB_SUCCESS) {
447 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
451 if (ret != LDB_SUCCESS) {
455 return ldb_next_init(module);
458 _PUBLIC_ const struct ldb_module_ops ldb_paged_searches_module_ops = {
459 .name = "paged_searches",
462 .init_context = ps_init