s4:ldb Fix the paged_searches module
[kai/samba.git] / source4 / lib / ldb / modules / paged_searches.c
index f4a06e841f037605f363c3854ab1d8c513d5c3c3..70b880e2dde7c56340c33174bae8c24503dc00f2 100644 (file)
@@ -1,7 +1,8 @@
 /* 
    ldb database library
 
-   Copyright (C) Simo Sorce  2005-2006
+   Copyright (C) Simo Sorce  2005-2008
+   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
 
      ** NOTE! The following LGPL license applies to the ldb
      ** library. This does NOT imply that all of Samba is released
@@ -10,7 +11,7 @@
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
+   version 3 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -18,8 +19,7 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -34,7 +34,7 @@
  */
 
 #include "includes.h"
-#include "ldb_includes.h"
+#include "ldb_module.h"
 
 #define PS_DEFAULT_PAGE_SIZE 500
 /* 500 objects per query seem to be a decent compromise
@@ -47,71 +47,48 @@ struct private_data {
 
 struct ps_context {
        struct ldb_module *module;
-       void *up_context;
-       int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
-
-       struct ldb_request *orig_req;
-
-       struct ldb_request *new_req;
+       struct ldb_request *req;
 
        bool pending;
 
        char **saved_referrals;
        int num_referrals;
+
+       struct ldb_request *down_req;
 };
 
-static struct ldb_handle *init_handle(void *mem_ctx, struct ldb_module *module,
-                                           void *context,
-                                           int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
+static int check_ps_continuation(struct ps_context *ac, struct ldb_request *req, struct ldb_reply *ares)
 {
-       struct ps_context *ac;
-       struct ldb_handle *h;
-
-       h = talloc_zero(mem_ctx, struct ldb_handle);
-       if (h == NULL) {
-               ldb_set_errstring(module->ldb, "Out of Memory");
-               return NULL;
+       struct ldb_context *ldb;
+       struct ldb_control *rep_control, *req_control;
+       struct ldb_paged_control *paged_rep_control = NULL, *paged_req_control = NULL;
+       ldb = ldb_module_get_ctx(ac->module);
+
+       rep_control = ldb_reply_get_control(ares, LDB_CONTROL_PAGED_RESULTS_OID);
+       if (rep_control) {
+               paged_rep_control = talloc_get_type(rep_control->data, struct ldb_paged_control);
        }
 
-       h->module = module;
-
-       ac = talloc_zero(h, struct ps_context);
-       if (ac == NULL) {
-               ldb_set_errstring(module->ldb, "Out of Memory");
-               talloc_free(h);
-               return NULL;
-       }
-
-       h->private_data = (void *)ac;
-
-       h->state = LDB_ASYNC_INIT;
-       h->status = LDB_SUCCESS;
-
-       ac->module = module;
-       ac->up_context = context;
-       ac->up_callback = callback;
-
-       ac->pending = False;
-       ac->saved_referrals = NULL;
-       ac->num_referrals = 0;
-
-       return h;
-}
+       req_control = ldb_request_get_control(req, LDB_CONTROL_PAGED_RESULTS_OID);
+       paged_req_control = talloc_get_type(req_control->data, struct ldb_paged_control);
 
-static int check_ps_continuation(struct ldb_reply *ares, struct ps_context *ac)
-{
-       struct ldb_paged_control *rep_control, *req_control;
+       if (!rep_control || !paged_rep_control) {
+               if (paged_req_control->cookie) {
+                       /* something wrong here - why give us a control back befre, but not one now? */
+                       ldb_set_errstring(ldb, "paged_searches:  ERROR: We got back a control from a previous page, but this time no control was returned!");
+                       return LDB_ERR_OPERATIONS_ERROR;
+               } else {
+                       /* No cookie recived yet, valid to just return the full data set */
 
-       /* look up our paged control */
-       if (!ares->controls || strcmp(LDB_CONTROL_PAGED_RESULTS_OID, ares->controls[0]->oid) != 0) {
-               /* something wrong here */
-               return LDB_ERR_OPERATIONS_ERROR;
+                       /* we are done */
+                       ac->pending = false;
+                       return LDB_SUCCESS;
+               }
        }
 
-       rep_control = talloc_get_type(ares->controls[0]->data, struct ldb_paged_control);
-       if (rep_control->cookie_len == 0) {
+       if (paged_rep_control->cookie_len == 0) {
                /* we are done */
-               ac->pending = False;
+               ac->pending = false;
                return LDB_SUCCESS;
        }
 
@@ -120,27 +97,20 @@ static int check_ps_continuation(struct ldb_reply *ares, struct ps_context *ac)
        /* if there's a reply control we must find a request
         * control matching it */
 
-       if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, ac->new_req->controls[0]->oid) != 0) {
-               /* something wrong here */
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       req_control = talloc_get_type(ac->new_req->controls[0]->data, struct ldb_paged_control);
-
-       if (req_control->cookie) {
-               talloc_free(req_control->cookie);
+       if (paged_req_control->cookie) {
+               talloc_free(paged_req_control->cookie);
        }
 
-       req_control->cookie = talloc_memdup(req_control,
-                                           rep_control->cookie,
-                                           rep_control->cookie_len);
-       req_control->cookie_len = rep_control->cookie_len;
+       paged_req_control->cookie = talloc_memdup(req_control,
+                                                 paged_rep_control->cookie,
+                                                 paged_rep_control->cookie_len);
+       paged_req_control->cookie_len = paged_rep_control->cookie_len;
 
-       ac->pending = True;
+       ac->pending = true;
        return LDB_SUCCESS;
 }
 
-static int store_referral(char *referral, struct ps_context *ac)
+static int store_referral(struct ps_context *ac, char *referral)
 {
        ac->saved_referrals = talloc_realloc(ac, ac->saved_referrals, char *, ac->num_referrals + 2);
        if (!ac->saved_referrals) {
@@ -158,13 +128,14 @@ static int store_referral(char *referral, struct ps_context *ac)
        return LDB_SUCCESS;
 }
 
-static int send_referrals(struct ldb_context *ldb, struct ps_context *ac)
+static int send_referrals(struct ps_context *ac)
 {
        struct ldb_reply *ares;
+       int ret;
        int i;
 
        for (i = 0; i < ac->num_referrals; i++) {
-               ares = talloc_zero(ac, struct ldb_reply);
+               ares = talloc_zero(ac->req, struct ldb_reply);
                if (!ares) {
                        return LDB_ERR_OPERATIONS_ERROR;
                }
@@ -172,71 +143,92 @@ static int send_referrals(struct ldb_context *ldb, struct ps_context *ac)
                ares->type = LDB_REPLY_REFERRAL;
                ares->referral = ac->saved_referrals[i];
 
-               ac->up_callback(ldb, ac->up_context, ares);
+               ret = ldb_module_send_referral(ac->req, ares->referral);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
        }
 
        return LDB_SUCCESS;
 }
 
-static int ps_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
+static int ps_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
-       struct ps_context *ac = NULL;
-       int ret = LDB_ERR_OPERATIONS_ERROR;
+       struct ps_context *ac;
+       int ret;
 
-       if (!context || !ares) {
-               ldb_set_errstring(ldb, "NULL Context or Result in callback");
-               goto error;
-       }
+       ac = talloc_get_type(req->context, struct ps_context);
 
-       ac = talloc_get_type(context, struct ps_context);
+       if (!ares) {
+               return ldb_module_done(ac->req, NULL, NULL,
+                                       LDB_ERR_OPERATIONS_ERROR);
+       }
+       if (ares->error != LDB_SUCCESS) {
+               return ldb_module_done(ac->req, ares->controls,
+                                       ares->response, ares->error);
+       }
 
        switch (ares->type) {
        case LDB_REPLY_ENTRY:
-               ac->up_callback(ldb, ac->up_context, ares);
+               ret = ldb_module_send_entry(ac->req, ares->message, ares->controls);
+               if (ret != LDB_SUCCESS) {
+                       return ldb_module_done(ac->req, NULL, NULL, ret);
+               }
                break;
 
        case LDB_REPLY_REFERRAL:
-               ret = store_referral(ares->referral, ac);
+               ret = store_referral(ac, ares->referral);
                if (ret != LDB_SUCCESS) {
-                       goto error;
+                       return ldb_module_done(ac->req, NULL, NULL, ret);
                }
                break;
 
        case LDB_REPLY_DONE:
-               ret = check_ps_continuation(ares, ac);
+
+               ret = check_ps_continuation(ac, req, ares);
                if (ret != LDB_SUCCESS) {
-                       goto error;
+                       return ldb_module_done(ac->req, NULL, NULL, ret);
                }
-               if (!ac->pending) {
+
+               if (ac->pending) {
+
+                       ret = ldb_next_request(ac->module, ac->down_req);
+
+                       if (ret != LDB_SUCCESS) {
+                               return ldb_module_done(ac->req,
+                                                       NULL, NULL, ret);
+                       }
+
+               } else {
+
                        /* send referrals */
-                       ret = send_referrals(ldb, ac);
+                       ret = send_referrals(ac);
                        if (ret != LDB_SUCCESS) {
-                               goto error;
+                               return ldb_module_done(ac->req,
+                                                       NULL, NULL, ret);
                        }
 
                        /* send REPLY_DONE */
-                       ac->up_callback(ldb, ac->up_context, ares);
+                       return ldb_module_done(ac->req, ares->controls,
+                                               ares->response, LDB_SUCCESS);
                }
                break;
-       default:
-               goto error;
        }
 
-       return LDB_SUCCESS;
-
-error:
        talloc_free(ares);
-       return ret;
+       return LDB_SUCCESS;
 }
 
 static int ps_search(struct ldb_module *module, struct ldb_request *req)
 {
+       struct ldb_context *ldb;
        struct private_data *private_data;
-       struct ldb_paged_control *control;
        struct ps_context *ac;
-       struct ldb_handle *h;
+       struct ldb_paged_control *control;
+       int ret;
 
-       private_data = talloc_get_type(module->private_data, struct private_data);
+       private_data = talloc_get_type(ldb_module_get_private(module), struct private_data);
+       ldb = ldb_module_get_ctx(module);
 
        /* check if paging is supported and if there is a any control */
        if (!private_data || !private_data->paged_supported || req->controls) {
@@ -246,223 +238,139 @@ static int ps_search(struct ldb_module *module, struct ldb_request *req)
                return ldb_next_request(module, req);
        }
 
-       if (!req->callback || !req->context) {
-               ldb_set_errstring(module->ldb,
-                                 "Async interface called with NULL callback function or NULL context");
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       
-       h = init_handle(req, module, req->context, req->callback);
-       if (!h) {
+       ac = talloc_zero(req, struct ps_context);
+       if (ac == NULL) {
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
-       ac = talloc_get_type(h->private_data, struct ps_context);
 
-       ac->new_req = talloc(ac, struct ldb_request);
-       if (!ac->new_req) return LDB_ERR_OPERATIONS_ERROR;
-
-       ac->new_req->controls = talloc_array(ac->new_req, struct ldb_control *, 2);
-       if (!ac->new_req->controls) return LDB_ERR_OPERATIONS_ERROR;
+       ac->module = module;
+       ac->req = req;
+       ac->pending = false;
+       ac->saved_referrals = NULL;
+       ac->num_referrals = 0;
 
-       ac->new_req->controls[0] = talloc(ac->new_req->controls, struct ldb_control);
-       if (!ac->new_req->controls[0]) return LDB_ERR_OPERATIONS_ERROR;
+       ldb = ldb_module_get_ctx(ac->module);
 
-       control = talloc(ac->new_req->controls[0], struct ldb_paged_control);
-       if (!control) return LDB_ERR_OPERATIONS_ERROR;
+       control = talloc(ac, struct ldb_paged_control);
+       if (!control) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
        control->size = PS_DEFAULT_PAGE_SIZE;
        control->cookie = NULL;
        control->cookie_len = 0;
 
-       ac->new_req->controls[0]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
-       ac->new_req->controls[0]->critical = 1;
-       ac->new_req->controls[0]->data = control;
-
-       ac->new_req->controls[1] = NULL;
-
-       ac->new_req->operation = req->operation;
-       ac->new_req->op.search.base = req->op.search.base;
-       ac->new_req->op.search.scope = req->op.search.scope;
-       ac->new_req->op.search.tree = req->op.search.tree;
-       ac->new_req->op.search.attrs = req->op.search.attrs;
-       ac->new_req->context = ac;
-       ac->new_req->callback = ps_callback;
-       ldb_set_timeout_from_prev_req(module->ldb, req, ac->new_req);
-
-       req->handle = h;
-
-       return ldb_next_request(module, ac->new_req);
-}
-
-static int ps_continuation(struct ldb_handle *handle)
-{
-       struct ps_context *ac;
-
-       if (!handle || !handle->private_data) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       ac = talloc_get_type(handle->private_data, struct ps_context);
-
-       /* reset the requests handle */
-       ac->new_req->handle = NULL;
-
-       return ldb_next_request(handle->module, ac->new_req);
-}
-
-static int ps_wait_none(struct ldb_handle *handle)
-{
-       struct ps_context *ac;
-       int ret;
-    
-       if (!handle || !handle->private_data) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       if (handle->state == LDB_ASYNC_DONE) {
-               return handle->status;
-       }
-
-       handle->state = LDB_ASYNC_PENDING;
-       handle->status = LDB_SUCCESS;
-
-       ac = talloc_get_type(handle->private_data, struct ps_context);
-
-       ret = ldb_wait(ac->new_req->handle, LDB_WAIT_NONE);
-
+       ret = ldb_build_search_req_ex(&ac->down_req, ldb, ac,
+                                       ac->req->op.search.base,
+                                       ac->req->op.search.scope,
+                                       ac->req->op.search.tree,
+                                       ac->req->op.search.attrs,
+                                       ac->req->controls,
+                                       ac,
+                                       ps_callback,
+                                       ac->req);
        if (ret != LDB_SUCCESS) {
-               handle->status = ret;
-               goto done;
-       }
-
-       if (ac->new_req->handle->status != LDB_SUCCESS) {
-               handle->status = ac->new_req->handle->status;
-               goto done;
+               return ret;
        }
 
-       if (ac->new_req->handle->state != LDB_ASYNC_DONE) {
-               return LDB_SUCCESS;
-       } 
-
-       /* see if we need to send another request for the next batch */
-       if (ac->pending) {
-               ret = ps_continuation(handle);
-               if (ret != LDB_SUCCESS) {
-                       handle->status = ret;
-                       goto done;
-               }
-
-               /* continue the search with the next request */
-               return LDB_SUCCESS;
+       ret = ldb_request_add_control(ac->down_req, LDB_CONTROL_PAGED_RESULTS_OID,
+                                     true, control);
+       if (ret != LDB_SUCCESS) {
+               return ret;
        }
 
-       ret = LDB_SUCCESS;
+       talloc_steal(ac->down_req, control);
 
-done:
-       handle->state = LDB_ASYNC_DONE;
-       return ret;
+       return ldb_next_request(ac->module, ac->down_req);
 }
 
-static int ps_wait_all(struct ldb_handle *handle)
+static int check_supported_paged(struct ldb_request *req,
+                                struct ldb_reply *ares)
 {
-       int ret;
-
-       while (handle->state != LDB_ASYNC_DONE) {
-               ret = ps_wait_none(handle);
-               if (ret != LDB_SUCCESS) {
-                       return ret;
-               }
-       }
+       struct private_data *data;
 
-       return handle->status;
-}
+       data = talloc_get_type(req->context, struct private_data);
 
-static int ps_wait(struct ldb_handle *handle, enum ldb_wait_type type)
-{
-       if (type == LDB_WAIT_ALL) {
-               return ps_wait_all(handle);
-       } else {
-               return ps_wait_none(handle);
+       if (!ares) {
+               return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
+       }
+       if (ares->error != LDB_SUCCESS) {
+               return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
        }
-}
 
-static int check_supported_paged(struct ldb_context *ldb, void *context, 
-                                struct ldb_reply *ares) 
-{
-       struct private_data *data;
-       data = talloc_get_type(context,
-                              struct private_data);
-       if (ares->type == LDB_REPLY_ENTRY) {
+       switch (ares->type) {
+       case LDB_REPLY_ENTRY:
                if (ldb_msg_check_string_attribute(ares->message,
                                                   "supportedControl",
                                                   LDB_CONTROL_PAGED_RESULTS_OID)) {
-                       data->paged_supported = True;
+                       data->paged_supported = true;
                }
+               break;
+
+       case LDB_REPLY_REFERRAL:
+               /* ignore */
+               break;
+
+       case LDB_REPLY_DONE:
+               return ldb_request_done(req, LDB_SUCCESS);
        }
+
+       talloc_free(ares);
        return LDB_SUCCESS;
 }
 
-
 static int ps_init(struct ldb_module *module)
 {
+       struct ldb_context *ldb;
        static const char *attrs[] = { "supportedControl", NULL };
        struct private_data *data;
+       struct ldb_dn *base;
        int ret;
        struct ldb_request *req;
 
+       ldb = ldb_module_get_ctx(module);
+
        data = talloc(module, struct private_data);
        if (data == NULL) {
-               return LDB_ERR_OTHER;
-       }
-       module->private_data = data;
-       data->paged_supported = False;
-
-       req = talloc(module, struct ldb_request);
-       if (req == NULL) {
-               ldb_set_errstring(module->ldb, "Out of Memory");
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
+       data->paged_supported = false;
 
-       req->operation = LDB_SEARCH;
-       req->op.search.base = ldb_dn_new(req, module->ldb, NULL);
-       req->op.search.scope = LDB_SCOPE_BASE;
+       ldb_module_set_private(module, data);
 
-       req->op.search.tree = ldb_parse_tree(req, "objectClass=*");
-       if (req->op.search.tree == NULL) {
-               ldb_set_errstring(module->ldb, "Unable to parse search expression");
-               talloc_free(req);
+       base = ldb_dn_new(module, ldb, "");
+       if (base == NULL) {
+               ldb_oom(ldb);
                return LDB_ERR_OPERATIONS_ERROR;
        }
-
-       req->op.search.attrs = attrs;
-       req->controls = NULL;
-       req->context = data;
-       req->callback = check_supported_paged;
-       ldb_set_timeout(module->ldb, req, 0); /* use default timeout */
+       ret = ldb_build_search_req(&req, ldb, module,
+                                  base, LDB_SCOPE_BASE,
+                                  "(objectClass=*)",
+                                  attrs, NULL,
+                                  data, check_supported_paged,
+                                  NULL);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
 
        ret = ldb_next_request(module, req);
-       
        if (ret == LDB_SUCCESS) {
                ret = ldb_wait(req->handle, LDB_WAIT_ALL);
        }
-       
-       talloc_free(req);
        if (ret != LDB_SUCCESS) {
                return ret;
        }
 
+       talloc_free(base);
+       talloc_free(req);
+
        return ldb_next_init(module);
 }
 
-static const struct ldb_module_ops ps_ops = {
+_PUBLIC_ const struct ldb_module_ops ldb_paged_searches_module_ops = {
        .name           = "paged_searches",
        .search         = ps_search,
-       .wait           = ps_wait,
        .init_context   = ps_init
 };
-
-int ldb_paged_searches_init(void)
-{
-       return ldb_register_module(&ps_ops);
-}
-