r18777: add helper functions to create an ldb_request structure
authorSimo Sorce <idra@samba.org>
Thu, 21 Sep 2006 06:14:32 +0000 (06:14 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 19:19:11 +0000 (14:19 -0500)
(This used to be commit bcbe82873f2f0a4e2552ed27eb171028de4560a7)

source4/lib/ldb/common/ldb.c
source4/lib/ldb/include/ldb.h

index 473c6324edb43677b9c906f0d853fe1afa7d82fc..a68733244d9bd7c9d00d8de780743df56f8babe8 100644 (file)
@@ -559,34 +559,33 @@ error:
        return LDB_ERR_OPERATIONS_ERROR;
 }
 
-/*
-  note that ldb_search() will automatically replace a NULL 'base' value with the 
-  defaultNamingContext from the rootDSE if available.
-*/
-int ldb_search(struct ldb_context *ldb, 
-              const struct ldb_dn *base,
-              enum ldb_scope scope,
-              const char *expression,
-              const char * const *attrs, 
-              struct ldb_result **res)
+int ldb_build_search_req(struct ldb_request **ret_req,
+                       struct ldb_context *ldb,
+                       void *mem_ctx,
+                       const struct ldb_dn *base,
+                       enum ldb_scope scope,
+                       const char *expression,
+                       const char * const *attrs,
+                       struct ldb_control **controls,
+                       void *context,
+                       ldb_request_callback_t callback)
 {
        struct ldb_request *req;
-       int ret;
 
-       *res = NULL;
-       
-       req = talloc(ldb, struct ldb_request);
+       *ret_req = NULL;
+
+       req = talloc(mem_ctx, struct ldb_request);
        if (req == NULL) {
                ldb_set_errstring(ldb, "Out of Memory");
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
+       req->operation = LDB_SEARCH;
        if (base == NULL) {
-               base = ldb_get_default_basedn(ldb);
+               req->op.search.base = ldb_dn_new(req);
+       } else {
+               req->op.search.base = base;
        }
-
-       req->operation = LDB_SEARCH;
-       req->op.search.base = base;
        req->op.search.scope = scope;
 
        req->op.search.tree = ldb_parse_tree(req, expression);
@@ -596,16 +595,163 @@ int ldb_search(struct ldb_context *ldb,
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
+       req->op.search.attrs = attrs;
+       req->controls = controls;
+       req->context = context;
+       req->callback = callback;
+
+       *ret_req = req;
+       return LDB_SUCCESS;
+}
+
+int ldb_build_add_req(struct ldb_request **ret_req,
+                       struct ldb_context *ldb,
+                       void *mem_ctx,
+                       struct ldb_message *message,
+                       struct ldb_control **controls,
+                       void *context,
+                       ldb_request_callback_t callback)
+{
+       struct ldb_request *req;
+
+       *ret_req = NULL;
+
+       req = talloc(mem_ctx, struct ldb_request);
+       if (req == NULL) {
+               ldb_set_errstring(ldb, "Out of Memory");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       req->operation = LDB_ADD;
+       req->op.add.message = message;
+       req->controls = controls;
+       req->context = context;
+       req->callback = callback;
+
+       *ret_req = req;
+
+       return LDB_SUCCESS;
+}
+
+int ldb_build_mod_req(struct ldb_request **ret_req,
+                       struct ldb_context *ldb,
+                       void *mem_ctx,
+                       struct ldb_message *message,
+                       struct ldb_control **controls,
+                       void *context,
+                       ldb_request_callback_t callback)
+{
+       struct ldb_request *req;
+
+       *ret_req = NULL;
+
+       req = talloc(mem_ctx, struct ldb_request);
+       if (req == NULL) {
+               ldb_set_errstring(ldb, "Out of Memory");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       req->operation = LDB_MODIFY;
+       req->op.mod.message = message;
+       req->controls = controls;
+       req->context = context;
+       req->callback = callback;
+
+       *ret_req = req;
+
+       return LDB_SUCCESS;
+}
+
+int ldb_build_del_req(struct ldb_request **ret_req,
+                       struct ldb_context *ldb,
+                       void *mem_ctx,
+                       struct ldb_dn *dn,
+                       struct ldb_control **controls,
+                       void *context,
+                       ldb_request_callback_t callback)
+{
+       struct ldb_request *req;
+
+       *ret_req = NULL;
+
+       req = talloc(mem_ctx, struct ldb_request);
+       if (req == NULL) {
+               ldb_set_errstring(ldb, "Out of Memory");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       req->operation = LDB_DELETE;
+       req->op.del.dn = dn;
+       req->controls = controls;
+       req->context = context;
+       req->callback = callback;
+
+       *ret_req = req;
+
+       return LDB_SUCCESS;
+}
+
+int ldb_build_rename_req(struct ldb_request **ret_req,
+                       struct ldb_context *ldb,
+                       void *mem_ctx,
+                       struct ldb_dn *olddn,
+                       struct ldb_dn *newdn,
+                       struct ldb_control **controls,
+                       void *context,
+                       ldb_request_callback_t callback)
+{
+       struct ldb_request *req;
+
+       *ret_req = NULL;
+
+       req = talloc(mem_ctx, struct ldb_request);
+       if (req == NULL) {
+               ldb_set_errstring(ldb, "Out of Memory");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       req->operation = LDB_RENAME;
+       req->op.rename.olddn = olddn;
+       req->op.rename.newdn = newdn;
+       req->controls = controls;
+       req->context = context;
+       req->callback = callback;
+
+       *ret_req = req;
+
+       return LDB_SUCCESS;
+}
+
+/*
+  note that ldb_search() will automatically replace a NULL 'base' value with the 
+  defaultNamingContext from the rootDSE if available.
+*/
+int ldb_search(struct ldb_context *ldb, 
+              const struct ldb_dn *base,
+              enum ldb_scope scope,
+              const char *expression,
+              const char * const *attrs, 
+              struct ldb_result **res)
+{
+       struct ldb_request *req;
+       int ret;
+
        *res = talloc_zero(ldb, struct ldb_result);
        if (! *res) {
-               talloc_free(req);
                return LDB_ERR_OPERATIONS_ERROR;
        }
+       
+       ret = ldb_build_search_req(&req, ldb, ldb,
+                                       base?base:ldb_get_default_basedn(ldb),
+                                       scope,
+                                       expression,
+                                       attrs,
+                                       NULL,
+                                       res,
+                                       ldb_search_callback);
+
+       if (ret != LDB_SUCCESS) goto done;
 
-       req->op.search.attrs = attrs;
-       req->controls = NULL;
-       req->context = res;
-       req->callback = ldb_search_callback;
        ldb_set_timeout(ldb, req, 0); /* use default timeout */
 
        ret = ldb_request(ldb, req);
@@ -613,17 +759,18 @@ int ldb_search(struct ldb_context *ldb,
        if (ret == LDB_SUCCESS) {
                ret = ldb_wait(req->handle, LDB_WAIT_ALL);
        }
-       
+
+       talloc_free(req);
+
+done:
        if (ret != LDB_SUCCESS) {
                talloc_free(*res);
                *res = NULL;
        }
 
-       talloc_free(req);
        return ret;
 }
 
-
 /*
   add a record to the database. Will fail if a record with the given class and key
   already exists
@@ -638,18 +785,15 @@ int ldb_add(struct ldb_context *ldb,
        if (ret != LDB_SUCCESS) {
                return ret;
        }
-               
-       req = talloc(ldb, struct ldb_request);
-       if (req == NULL) {
-               ldb_set_errstring(ldb, "Out of Memory");
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
 
-       req->operation = LDB_ADD;
-       req->op.add.message = message;
-       req->controls = NULL;
-       req->context = NULL;
-       req->callback = NULL;
+       ret = ldb_build_add_req(&req, ldb, ldb,
+                                       message,
+                                       NULL,
+                                       NULL,
+                                       NULL);
+
+       if (ret != LDB_SUCCESS) return ret;
+
        ldb_set_timeout(ldb, req, 0); /* use default timeout */
 
        /* do request and autostart a transaction */
@@ -671,17 +815,14 @@ int ldb_modify(struct ldb_context *ldb,
        ret = ldb_msg_sanity_check(ldb, message);
        if (ret != LDB_SUCCESS) return ret;
 
-       req = talloc(ldb, struct ldb_request);
-       if (req == NULL) {
-               ldb_set_errstring(ldb, "Out of Memory!");
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
+       ret = ldb_build_mod_req(&req, ldb, ldb,
+                                       message,
+                                       NULL,
+                                       NULL,
+                                       NULL);
+
+       if (ret != LDB_SUCCESS) return ret;
 
-       req->operation = LDB_MODIFY;
-       req->op.add.message = message;
-       req->controls = NULL;
-       req->context = NULL;
-       req->callback = NULL;
        ldb_set_timeout(ldb, req, 0); /* use default timeout */
 
        /* do request and autostart a transaction */
@@ -700,17 +841,14 @@ int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
        struct ldb_request *req;
        int ret;
 
-       req = talloc(ldb, struct ldb_request);
-       if (req == NULL) {
-               ldb_set_errstring(ldb, "Out of Memory!");
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
+       ret = ldb_build_del_req(&req, ldb, ldb,
+                                       dn,
+                                       NULL,
+                                       NULL,
+                                       NULL);
+
+       if (ret != LDB_SUCCESS) return ret;
 
-       req->operation = LDB_DELETE;
-       req->op.del.dn = dn;
-       req->controls = NULL;
-       req->context = NULL;
-       req->callback = NULL;
        ldb_set_timeout(ldb, req, 0); /* use default timeout */
 
        /* do request and autostart a transaction */
@@ -728,18 +866,15 @@ int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct
        struct ldb_request *req;
        int ret;
 
-       req = talloc(ldb, struct ldb_request);
-       if (req == NULL) {
-               ldb_set_errstring(ldb, "Out of Memory!");
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
+       ret = ldb_build_rename_req(&req, ldb, ldb,
+                                       olddn,
+                                       newdn,
+                                       NULL,
+                                       NULL,
+                                       NULL);
+
+       if (ret != LDB_SUCCESS) return ret;
 
-       req->operation = LDB_RENAME;
-       req->op.rename.olddn = olddn;
-       req->op.rename.newdn = newdn;
-       req->controls = NULL;
-       req->context = NULL;
-       req->callback = NULL;
        ldb_set_timeout(ldb, req, 0); /* use default timeout */
 
        /* do request and autostart a transaction */
index 124cba9b66a137ec5efdaeac6f49c761e9f855af..78d701689d55c5c6437fa79d3a04572c81dd2483 100644 (file)
@@ -743,6 +743,7 @@ struct ldb_sequence_number {
        uint64_t seq_num;
 };
 
+typedef int (*ldb_request_callback_t)(struct ldb_context *, void *, struct ldb_reply *);
 struct ldb_request {
 
        enum ldb_request_type operation;
@@ -761,7 +762,7 @@ struct ldb_request {
        struct ldb_control **controls;
 
        void *context;
-       int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
+       ldb_request_callback_t callback;
 
        int timeout;
        time_t starttime;