diff options
Diffstat (limited to 'source4/lib/ldb/common')
-rw-r--r-- | source4/lib/ldb/common/ldb.c | 192 | ||||
-rw-r--r-- | source4/lib/ldb/common/ldb_modules.c | 73 |
2 files changed, 113 insertions, 152 deletions
diff --git a/source4/lib/ldb/common/ldb.c b/source4/lib/ldb/common/ldb.c index 791e66ea51..48911cad6b 100644 --- a/source4/lib/ldb/common/ldb.c +++ b/source4/lib/ldb/common/ldb.c @@ -172,6 +172,52 @@ int ldb_transaction_cancel(struct ldb_context *ldb) } /* + check for an error return from an op + if an op fails, but has not setup an error string, then setup one now +*/ +static int ldb_op_finish(struct ldb_context *ldb, int status) +{ + if (status == LDB_SUCCESS) { + return ldb_transaction_commit(ldb); + } + if (ldb->err_string == NULL) { + /* no error string was setup by the backend */ + ldb_set_errstring(ldb->modules, + talloc_asprintf(ldb, "ldb error %d", status)); + } + ldb_transaction_cancel(ldb); + return status; +} + +/* + start an ldb request + autostarts a transacion if none active and the operation is not a search + returns -1 on errors. +*/ + +int ldb_request(struct ldb_context *ldb, struct ldb_request *request) +{ + int status; + + ldb_reset_err_string(ldb); + + if ((!ldb->transaction_active) && + (request->operation == LDB_REQ_ADD || + request->operation == LDB_REQ_MODIFY || + request->operation == LDB_REQ_DELETE || + request->operation == LDB_REQ_RENAME)) { + + status = ldb_transaction_start(ldb); + if (status != LDB_SUCCESS) return status; + + status = ldb->modules->ops->request(ldb->modules, request); + return ldb_op_finish(ldb, status); + } + + return ldb->modules->ops->request(ldb->modules, request); +} + +/* search the database given a LDAP-like search expression return the number of records found, or -1 on error @@ -183,61 +229,36 @@ 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_message ***res) + const char * const *attrs, struct ldb_result **res) { + struct ldb_request *request; struct ldb_parse_tree *tree; int ret; + request = talloc(ldb, struct ldb_request); + if (request == NULL) { + ldb_set_errstring(ldb->modules, talloc_strdup(ldb, "Not Enough memory")); + return -1; + } + tree = ldb_parse_tree(ldb, expression); if (tree == NULL) { ldb_set_errstring(ldb->modules, talloc_strdup(ldb, "Unable to parse search expression")); return -1; } - ret = ldb_search_bytree(ldb, base, scope, tree, attrs, res); - talloc_free(tree); - - return ret; -} - -/* - search the database given a search tree - - return the number of records found, or -1 on error - - Use talloc_free to free the ldb_message returned in 'res' - -*/ -int ldb_search_bytree(struct ldb_context *ldb, - const struct ldb_dn *base, - enum ldb_scope scope, - struct ldb_parse_tree *tree, - const char * const *attrs, struct ldb_message ***res) -{ - struct ldb_module *module; - FIRST_OP(ldb, search_bytree); + request->operation = LDB_REQ_SEARCH; + request->op.search.base = base; + request->op.search.scope = scope; + request->op.search.tree = tree; + request->op.search.attrs = attrs; + request->op.search.res = res; - ldb_reset_err_string(ldb); + ret = ldb_request(ldb, request); - return module->ops->search_bytree(module, base, scope, tree, attrs, res); -} + talloc_free(tree); -/* - check for an error return from an op - if an op fails, but has not setup an error string, then setup one now -*/ -static int ldb_op_finish(struct ldb_context *ldb, int status) -{ - if (status == LDB_SUCCESS) { - return ldb_transaction_commit(ldb); - } - if (ldb->err_string == NULL) { - /* no error string was setup by the backend */ - ldb_set_errstring(ldb->modules, - talloc_asprintf(ldb, "ldb error %d", status)); - } - ldb_transaction_cancel(ldb); - return status; + return ret; } /* @@ -247,25 +268,22 @@ static int ldb_op_finish(struct ldb_context *ldb, int status) int ldb_add(struct ldb_context *ldb, const struct ldb_message *message) { - struct ldb_module *module; + struct ldb_request *request; int status; - FIRST_OP(ldb, add_record); - - ldb_reset_err_string(ldb); - status = ldb_msg_sanity_check(message); if (status != LDB_SUCCESS) return status; - if (! ldb->transaction_active) { - status = ldb_transaction_start(ldb); - if (status != LDB_SUCCESS) return status; - - status = module->ops->add_record(module, message); - return ldb_op_finish(ldb, status); + request = talloc(ldb, struct ldb_request); + if (request == NULL) { + ldb_set_errstring(ldb->modules, talloc_strdup(ldb, "Not Enough memory")); + return -1; } - return module->ops->add_record(module, message); + request->operation = LDB_REQ_ADD; + request->op.add.message = message; + + return ldb_request(ldb, request); } /* @@ -274,25 +292,22 @@ int ldb_add(struct ldb_context *ldb, int ldb_modify(struct ldb_context *ldb, const struct ldb_message *message) { - struct ldb_module *module; + struct ldb_request *request; int status; - FIRST_OP(ldb, modify_record); - - ldb_reset_err_string(ldb); - status = ldb_msg_sanity_check(message); if (status != LDB_SUCCESS) return status; - if (! ldb->transaction_active) { - status = ldb_transaction_start(ldb); - if (status != LDB_SUCCESS) return status; - - status = module->ops->modify_record(module, message); - return ldb_op_finish(ldb, status); + request = talloc(ldb, struct ldb_request); + if (request == NULL) { + ldb_set_errstring(ldb->modules, talloc_strdup(ldb, "Not Enough memory")); + return -1; } - return module->ops->modify_record(module, message); + request->operation = LDB_REQ_MODIFY; + request->op.mod.message = message; + + return ldb_request(ldb, request); } @@ -301,22 +316,18 @@ int ldb_modify(struct ldb_context *ldb, */ int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn) { - struct ldb_module *module; - int status; - - FIRST_OP(ldb, delete_record); - - ldb_reset_err_string(ldb); - - if (! ldb->transaction_active) { - status = ldb_transaction_start(ldb); - if (status != LDB_SUCCESS) return status; + struct ldb_request *request; - status = module->ops->delete_record(module, dn); - return ldb_op_finish(ldb, status); + request = talloc(ldb, struct ldb_request); + if (request == NULL) { + ldb_set_errstring(ldb->modules, talloc_strdup(ldb, "Not Enough memory")); + return -1; } - return module->ops->delete_record(module, dn); + request->operation = LDB_REQ_DELETE; + request->op.del.dn = dn; + + return ldb_request(ldb, request); } /* @@ -324,22 +335,19 @@ int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn) */ int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn) { - struct ldb_module *module; - int status; - - FIRST_OP(ldb, rename_record); - - ldb_reset_err_string(ldb); - - if (! ldb->transaction_active) { - status = ldb_transaction_start(ldb); - if (status != LDB_SUCCESS) return status; + struct ldb_request *request; - status = module->ops->rename_record(module, olddn, newdn); - return ldb_op_finish(ldb, status); + request = talloc(ldb, struct ldb_request); + if (request == NULL) { + ldb_set_errstring(ldb->modules, talloc_strdup(ldb, "Not Enough memory")); + return -1; } - return module->ops->rename_record(module, olddn, newdn); + request->operation = LDB_REQ_RENAME; + request->op.rename.olddn = olddn; + request->op.rename.newdn = newdn; + + return ldb_request(ldb, request); } diff --git a/source4/lib/ldb/common/ldb_modules.c b/source4/lib/ldb/common/ldb_modules.c index f3ca4df9b4..91338c2e10 100644 --- a/source4/lib/ldb/common/ldb_modules.c +++ b/source4/lib/ldb/common/ldb_modules.c @@ -35,6 +35,7 @@ #include "includes.h" #include "ldb/include/ldb.h" +#include "ldb/include/ldb_errors.h" #include "ldb/include/ldb_private.h" #include "dlinklist.h" #include <sys/types.h> @@ -153,7 +154,7 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[]) if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) { int ret; const char * const attrs[] = { "@LIST" , NULL}; - struct ldb_message **msg = NULL; + struct ldb_result *res = NULL; struct ldb_dn *mods; mods = ldb_dn_explode(ldb, "@MODULES"); @@ -161,27 +162,27 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[]) return -1; } - ret = ldb_search(ldb, mods, LDB_SCOPE_BASE, "", attrs, &msg); + ret = ldb_search(ldb, mods, LDB_SCOPE_BASE, "", attrs, &res); talloc_free(mods); - if (ret == 0 || (ret == 1 && msg[0]->num_elements == 0)) { + if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) { ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n"); } else { - if (ret < 0) { + if (ret != LDB_SUCCESS) { ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb)); return -1; } - if (ret > 1) { - ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", ret); - talloc_free(msg); + if (res->count > 1) { + ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count); + talloc_free(res); return -1; } modules = ldb_modules_list_from_string(ldb, - (const char *)msg[0]->elements[0].values[0].data); + (const char *)res->msgs[0]->elements[0].values[0].data); } - talloc_free(msg); + talloc_free(res); } if (modules == NULL) { @@ -228,58 +229,10 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[]) /* helper functions to call the next module in chain */ -int ldb_next_search_bytree(struct ldb_module *module, - const struct ldb_dn *base, - enum ldb_scope scope, - struct ldb_parse_tree *tree, - const char * const *attrs, struct ldb_message ***res) +int ldb_next_request(struct ldb_module *module, struct ldb_request *request) { - FIND_OP(module, search_bytree); - return module->ops->search_bytree(module, base, scope, tree, attrs, res); -} - -int ldb_next_search(struct ldb_module *module, - const struct ldb_dn *base, - enum ldb_scope scope, - const char *expression, - const char * const *attrs, struct ldb_message ***res) -{ - struct ldb_parse_tree *tree; - int ret; - FIND_OP(module, search_bytree); - tree = ldb_parse_tree(module, expression); - if (tree == NULL) { - ldb_set_errstring(module, talloc_strdup(module, "Unable to parse search expression")); - return -1; - } - ret = module->ops->search_bytree(module, base, scope, tree, attrs, res); - talloc_free(tree); - return ret; -} - - -int ldb_next_add_record(struct ldb_module *module, const struct ldb_message *message) -{ - FIND_OP(module, add_record); - return module->ops->add_record(module, message); -} - -int ldb_next_modify_record(struct ldb_module *module, const struct ldb_message *message) -{ - FIND_OP(module, modify_record); - return module->ops->modify_record(module, message); -} - -int ldb_next_delete_record(struct ldb_module *module, const struct ldb_dn *dn) -{ - FIND_OP(module, delete_record); - return module->ops->delete_record(module, dn); -} - -int ldb_next_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn) -{ - FIND_OP(module, rename_record); - return module->ops->rename_record(module, olddn, newdn); + FIND_OP(module, request); + return module->ops->request(module, request); } int ldb_next_start_trans(struct ldb_module *module) |