From 8d1b32083eb9f35b5e3de3354480aa5e0e8a76d1 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Thu, 21 Sep 2006 06:14:32 +0000 Subject: r18777: add helper functions to create an ldb_request structure (This used to be commit bcbe82873f2f0a4e2552ed27eb171028de4560a7) --- source4/lib/ldb/common/ldb.c | 271 +++++++++++++++++++++++++++++++----------- source4/lib/ldb/include/ldb.h | 3 +- 2 files changed, 205 insertions(+), 69 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/ldb/common/ldb.c b/source4/lib/ldb/common/ldb.c index 473c6324ed..a68733244d 100644 --- a/source4/lib/ldb/common/ldb.c +++ b/source4/lib/ldb/common/ldb.c @@ -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 */ diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h index 124cba9b66..78d701689d 100644 --- a/source4/lib/ldb/include/ldb.h +++ b/source4/lib/ldb/include/ldb.h @@ -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; -- cgit