X-Git-Url: http://git.samba.org/samba.git/?p=ira%2Fwip.git;a=blobdiff_plain;f=source4%2Flib%2Fldb%2Fldb_ildap%2Fldb_ildap.c;h=352b7696d698fa47492bfb6b93e2de4f54e1368f;hp=cc5416fc5a30bda28d5e95fcbb8e5de56ea778f3;hb=b45f56d4ba103f5ef04084285466f7e52d3de959;hpb=f7312bf37367ab9576842ac06de0f170455d08a8 diff --git a/source4/lib/ldb/ldb_ildap/ldb_ildap.c b/source4/lib/ldb/ldb_ildap/ldb_ildap.c index cc5416fc5a3..352b7696d69 100644 --- a/source4/lib/ldb/ldb_ildap/ldb_ildap.c +++ b/source4/lib/ldb/ldb_ildap/ldb_ildap.c @@ -1,17 +1,17 @@ -/* +/* ldb database library - ildap backend Copyright (C) Andrew Tridgell 2005 - Copyright (C) Simo Sorce 2006 + Copyright (C) Simo Sorce 2008 ** NOTE! The following LGPL license applies to the ldb ** library. This does NOT imply that all of Samba is released ** under the LGPL - + 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 @@ -19,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 . */ /* @@ -41,35 +40,78 @@ * author: Simo Sorce */ - #include "includes.h" -#include "ldb/include/includes.h" +#include "ldb_module.h" +#include "dlinklist.h" -#include "lib/events/events.h" #include "libcli/ldap/ldap.h" #include "libcli/ldap/ldap_client.h" -#include "lib/cmdline/popt_common.h" #include "auth/auth.h" +#include "auth/credentials/credentials.h" struct ildb_private { struct ldap_connection *ldap; - struct ldb_message *rootDSE; - struct ldb_context *ldb; + struct tevent_context *event_ctx; }; -struct ildb_async_context { +struct ildb_context { struct ldb_module *module; - struct ldap_request *req; - void *context; - int (*callback)(struct ldb_context *, void *, struct ldb_async_result *); + struct ldb_request *req; + + struct ildb_private *ildb; + struct ldap_request *ireq; + + bool done; + + struct ildb_destructor_ctx *dc; }; +static void ildb_request_done(struct ildb_context *ctx, + struct ldb_control **ctrls, int error) +{ + struct ldb_context *ldb; + struct ldb_reply *ares; + + ldb = ldb_module_get_ctx(ctx->module); + + ctx->done = true; + + if (ctx->req == NULL) { + /* if the req has been freed already just return */ + return; + } + + ares = talloc_zero(ctx->req, struct ldb_reply); + if (!ares) { + ldb_oom(ldb); + ctx->req->callback(ctx->req, NULL); + return; + } + ares->type = LDB_REPLY_DONE; + ares->controls = talloc_steal(ares, ctrls); + ares->error = error; + + ctx->req->callback(ctx->req, ares); +} + +static void ildb_auto_done_callback(struct tevent_context *ev, + struct tevent_timer *te, + struct timeval t, + void *private_data) +{ + struct ildb_context *ac; + + ac = talloc_get_type(private_data, struct ildb_context); + ildb_request_done(ac, NULL, LDB_SUCCESS); +} + /* convert a ldb_message structure to a list of ldap_mod structures ready for ildap_add() or ildap_modify() */ static struct ldap_mod **ildb_msg_to_mods(void *mem_ctx, int *num_mods, - const struct ldb_message *msg, int use_flags) + const struct ldb_message *msg, + int use_flags) { struct ldap_mod **mods; unsigned int i; @@ -121,194 +163,190 @@ failed: /* map an ildap NTSTATUS to a ldb error code */ -static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status) +static int ildb_map_error(struct ldb_module *module, NTSTATUS status) { + struct ildb_private *ildb; + struct ldb_context *ldb; + TALLOC_CTX *mem_ctx; + + ildb = talloc_get_type(ldb_module_get_private(module), struct ildb_private); + ldb = ldb_module_get_ctx(module); + if (NT_STATUS_IS_OK(status)) { return LDB_SUCCESS; } - talloc_free(ildb->ldb->err_string); - ildb->ldb->err_string = talloc_strdup(ildb, ldap_errstr(ildb->ldap, status)); + + mem_ctx = talloc_new(ildb); + if (!mem_ctx) { + ldb_oom(ldb); + return LDB_ERR_OPERATIONS_ERROR; + } + ldb_set_errstring(ldb, + ldap_errstr(ildb->ldap, mem_ctx, status)); + talloc_free(mem_ctx); if (NT_STATUS_IS_LDAP(status)) { return NT_STATUS_LDAP_CODE(status); } return LDB_ERR_OPERATIONS_ERROR; } -static void ildb_request_timeout(struct event_context *ev, struct timed_event *te, +static void ildb_request_timeout(struct tevent_context *ev, struct tevent_timer *te, struct timeval t, void *private_data) { - struct ldb_async_handle *handle = talloc_get_type(private_data, struct ldb_async_handle); - struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context); + struct ildb_context *ac = talloc_get_type(private_data, struct ildb_context); - if (ac->req->state == LDAP_REQUEST_PENDING) { - DLIST_REMOVE(ac->req->conn->pending, ac->req); + if (ac->ireq->state == LDAP_REQUEST_PENDING) { + DLIST_REMOVE(ac->ireq->conn->pending, ac->ireq); } - handle->status = LDB_ERR_OPERATIONS_ERROR; - - return; + ildb_request_done(ac, NULL, LDB_ERR_TIME_LIMIT_EXCEEDED); } -static void ildb_async_callback(struct ldap_request *req) +static void ildb_callback(struct ldap_request *req) { - struct ldb_async_handle *handle = talloc_get_type(req->async.private_data, struct ldb_async_handle); - struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context); - struct ildb_private *ildb = talloc_get_type(ac->module->private_data, struct ildb_private); + struct ldb_context *ldb; + struct ildb_context *ac; NTSTATUS status; + struct ldap_SearchResEntry *search; + struct ldap_message *msg; + struct ldb_control **controls; + struct ldb_message *ldbmsg; + char *referral; + bool callback_failed; + bool request_done; + int ret; int i; - handle->status = LDB_SUCCESS; + ac = talloc_get_type(req->async.private_data, struct ildb_context); + ldb = ldb_module_get_ctx(ac->module); + callback_failed = false; + request_done = false; + controls = NULL; if (!NT_STATUS_IS_OK(req->status)) { - handle->status = ildb_map_error(ildb, req->status); + ret = ildb_map_error(ac->module, req->status); + ildb_request_done(ac, NULL, ret); return; } if (req->num_replies < 1) { - handle->status = LDB_ERR_OPERATIONS_ERROR; + ret = LDB_ERR_OPERATIONS_ERROR; + ildb_request_done(ac, NULL, ret); return; - } - + } + switch (req->type) { case LDAP_TAG_ModifyRequest: if (req->replies[0]->type != LDAP_TAG_ModifyResponse) { - handle->status = LDB_ERR_PROTOCOL_ERROR; - return; + ret = LDB_ERR_PROTOCOL_ERROR; + break; } - status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult); - handle->status = ildb_map_error(ildb, status); - if (ac->callback && handle->status == LDB_SUCCESS) { - /* FIXME: build a corresponding ares to pass on */ - handle->status = ac->callback(ac->module->ldb, ac->context, NULL); - } - handle->state = LDB_ASYNC_DONE; + status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult); + ret = ildb_map_error(ac->module, status); + request_done = true; break; case LDAP_TAG_AddRequest: if (req->replies[0]->type != LDAP_TAG_AddResponse) { - handle->status = LDB_ERR_PROTOCOL_ERROR; + ret = LDB_ERR_PROTOCOL_ERROR; return; } - status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult); - handle->status = ildb_map_error(ildb, status); - if (ac->callback && handle->status == LDB_SUCCESS) { - /* FIXME: build a corresponding ares to pass on */ - handle->status = ac->callback(ac->module->ldb, ac->context, NULL); - } - handle->state = LDB_ASYNC_DONE; + status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult); + ret = ildb_map_error(ac->module, status); + request_done = true; break; case LDAP_TAG_DelRequest: if (req->replies[0]->type != LDAP_TAG_DelResponse) { - handle->status = LDB_ERR_PROTOCOL_ERROR; + ret = LDB_ERR_PROTOCOL_ERROR; return; } - status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult); - handle->status = ildb_map_error(ildb, status); - if (ac->callback && handle->status == LDB_SUCCESS) { - /* FIXME: build a corresponding ares to pass on */ - handle->status = ac->callback(ac->module->ldb, ac->context, NULL); - } - handle->state = LDB_ASYNC_DONE; + status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult); + ret = ildb_map_error(ac->module, status); + request_done = true; break; case LDAP_TAG_ModifyDNRequest: if (req->replies[0]->type != LDAP_TAG_ModifyDNResponse) { - handle->status = LDB_ERR_PROTOCOL_ERROR; + ret = LDB_ERR_PROTOCOL_ERROR; return; } - status = ldap_check_response(req->conn, &req->replies[0]->r.GeneralResult); - handle->status = ildb_map_error(ildb, status); - if (ac->callback && handle->status == LDB_SUCCESS) { - /* FIXME: build a corresponding ares to pass on */ - handle->status = ac->callback(ac->module->ldb, ac->context, NULL); - } - handle->state = LDB_ASYNC_DONE; + status = ldap_check_response(ac->ireq->conn, &req->replies[0]->r.GeneralResult); + ret = ildb_map_error(ac->module, status); + request_done = true; break; case LDAP_TAG_SearchRequest: /* loop over all messages */ for (i = 0; i < req->num_replies; i++) { - struct ldap_SearchResEntry *search; - struct ldb_async_result *ares = NULL; - struct ldap_message *msg; - int ret; - - ares = talloc_zero(ac, struct ldb_async_result); - if (!ares) { - handle->status = LDB_ERR_OPERATIONS_ERROR; - return; - } msg = req->replies[i]; switch (msg->type) { case LDAP_TAG_SearchResultDone: - status = ldap_check_response(req->conn, &msg->r.GeneralResult); + status = ldap_check_response(ac->ireq->conn, &msg->r.GeneralResult); if (!NT_STATUS_IS_OK(status)) { - ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Error: %s\n" ,ldap_errstr(req->conn, status)); - handle->status = ildb_map_error(ildb, status); - return; - } - - if (msg->controls) { - ares->controls = talloc_steal(ares, msg->controls); + ret = ildb_map_error(ac->module, status); + break; } + + controls = talloc_steal(ac, msg->controls); if (msg->r.SearchResultDone.resultcode) { if (msg->r.SearchResultDone.errormessage) { - ldb_set_errstring(ac->module->ldb, talloc_strdup(ac->module, msg->r.SearchResultDone.errormessage)); + ldb_set_errstring(ldb, msg->r.SearchResultDone.errormessage); } } - handle->status = msg->r.SearchResultDone.resultcode; - handle->state = LDB_ASYNC_DONE; - ares->type = LDB_REPLY_DONE; + ret = msg->r.SearchResultDone.resultcode; + request_done = true; break; case LDAP_TAG_SearchResultEntry: - - ares->message = ldb_msg_new(ares); - if (!ares->message) { - handle->status = LDB_ERR_OPERATIONS_ERROR;; - return; + ldbmsg = ldb_msg_new(ac); + if (!ldbmsg) { + ret = LDB_ERR_OPERATIONS_ERROR; + break; } search = &(msg->r.SearchResultEntry); - - ares->message->dn = ldb_dn_explode_or_special(ares->message, search->dn); - if (ares->message->dn == NULL) { - handle->status = LDB_ERR_OPERATIONS_ERROR; - return; + + ldbmsg->dn = ldb_dn_new(ldbmsg, ldb, search->dn); + if ( ! ldb_dn_validate(ldbmsg->dn)) { + ret = LDB_ERR_OPERATIONS_ERROR; + break; } - ares->message->num_elements = search->num_attributes; - ares->message->elements = talloc_steal(ares->message, search->attributes); + ldbmsg->num_elements = search->num_attributes; + ldbmsg->elements = talloc_move(ldbmsg, &search->attributes); - handle->status = LDB_SUCCESS; - handle->state = LDB_ASYNC_PENDING; - ares->type = LDB_REPLY_ENTRY; + controls = talloc_steal(ac, msg->controls); + + ret = ldb_module_send_entry(ac->req, ldbmsg, controls); + if (ret != LDB_SUCCESS) { + callback_failed = true; + } break; case LDAP_TAG_SearchResultReference: - ares->referral = talloc_strdup(ares, msg->r.SearchResultReference.referral); - - handle->status = LDB_SUCCESS; - handle->state = LDB_ASYNC_PENDING; - ares->type = LDB_REPLY_REFERRAL; + referral = talloc_strdup(ac, msg->r.SearchResultReference.referral); + + ret = ldb_module_send_referral(ac->req, referral); + if (ret != LDB_SUCCESS) { + callback_failed = true; + } break; default: /* TAG not handled, fail ! */ - handle->status = LDB_ERR_PROTOCOL_ERROR; - return; + ret = LDB_ERR_PROTOCOL_ERROR; + break; } - ret = ac->callback(ac->module->ldb, ac->context, ares); - if (ret) { - handle->status = ret; + if (ret != LDB_SUCCESS) { + break; } } @@ -317,277 +355,149 @@ static void ildb_async_callback(struct ldap_request *req) req->num_replies = 0; break; - + default: - handle->status = LDB_ERR_PROTOCOL_ERROR; - return; + ret = LDB_ERR_PROTOCOL_ERROR; + break; } + + if (ret != LDB_SUCCESS) { + + /* if the callback failed the caller will have freed the + * request. Just return and don't try to use it */ + if ( ! callback_failed) { + request_done = true; + } + } + + if (request_done) { + ildb_request_done(ac, controls, ret); + } + return; } -static int ildb_request_send(struct ldb_module *module, struct ldap_message *msg, - void *context, - int (*callback)(struct ldb_context *, void *, struct ldb_async_result *), - int timeout, - struct ldb_async_handle **handle) +static int ildb_request_send(struct ildb_context *ac, struct ldap_message *msg) { - struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); - struct ildb_async_context *ildb_ac; - struct ldb_async_handle *h; + struct ldb_context *ldb; struct ldap_request *req; - h = talloc_zero(ildb->ldap, struct ldb_async_handle); - if (h == NULL) { - ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory")); + if (!ac) { return LDB_ERR_OPERATIONS_ERROR; } - h->module = module; + ldb = ldb_module_get_ctx(ac->module); - ildb_ac = talloc(h, struct ildb_async_context); - if (ildb_ac == NULL) { - ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory")); - talloc_free(h); + req = ldap_request_send(ac->ildb->ldap, msg); + if (req == NULL) { + ldb_set_errstring(ldb, "async send request failed"); return LDB_ERR_OPERATIONS_ERROR; } + ac->ireq = talloc_steal(ac, req); - h->private_data = (void *)ildb_ac; - - h->state = LDB_ASYNC_INIT; - h->status = LDB_SUCCESS; - - req = ldap_request_send(ildb->ldap, msg); - if (req == NULL) { - ldb_set_errstring(module->ldb, talloc_asprintf(module, "async send request failed")); + if (!ac->ireq->conn) { + ldb_set_errstring(ldb, "connection to remote LDAP server dropped?"); return LDB_ERR_OPERATIONS_ERROR; } - ildb_ac->req = talloc_steal(ildb_ac, req); - ildb_ac->module = module; - ildb_ac->context = context; - ildb_ac->callback = callback; - - req->async.fn = ildb_async_callback; - req->async.private_data = (void *)h; - talloc_free(req->time_event); req->time_event = NULL; - if (timeout) { - req->time_event = event_add_timed(req->conn->event.event_ctx, h, - timeval_current_ofs(timeout, 0), - ildb_request_timeout, h); + if (ac->req->timeout) { + req->time_event = tevent_add_timer(ac->ildb->event_ctx, ac, + timeval_current_ofs(ac->req->timeout, 0), + ildb_request_timeout, ac); } - *handle = h; + req->async.fn = ildb_callback; + req->async.private_data = ac; return LDB_SUCCESS; - } /* search for matching records using an asynchronous function */ -static int ildb_search_async(struct ldb_module *module, const struct ldb_dn *base, - enum ldb_scope scope, struct ldb_parse_tree *tree, - const char * const *attrs, - struct ldb_control **control_req, - void *context, - int (*callback)(struct ldb_context *, void *, struct ldb_async_result *), - int timeout, - struct ldb_async_handle **handle) +static int ildb_search(struct ildb_context *ac) { - struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); + struct ldb_context *ldb; + struct ldb_request *req = ac->req; struct ldap_message *msg; int n; - *handle = NULL; + ldb = ldb_module_get_ctx(ac->module); - if (!callback || !context) { - ldb_set_errstring(module->ldb, talloc_asprintf(module, "Async interface called with NULL callback function or NULL context")); + if (!req->callback || !req->context) { + ldb_set_errstring(ldb, "Async interface called with NULL callback function or NULL context"); return LDB_ERR_OPERATIONS_ERROR; } - - if (tree == NULL) { - ldb_set_errstring(module->ldb, talloc_asprintf(module, "Invalid expression parse tree")); + + if (req->op.search.tree == NULL) { + ldb_set_errstring(ldb, "Invalid expression parse tree"); return LDB_ERR_OPERATIONS_ERROR; } - msg = new_ldap_message(ildb); + msg = new_ldap_message(req); if (msg == NULL) { - ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory")); + ldb_set_errstring(ldb, "Out of Memory"); return LDB_ERR_OPERATIONS_ERROR; } msg->type = LDAP_TAG_SearchRequest; - if (base == NULL) { - if (ildb->rootDSE != NULL) { - msg->r.SearchRequest.basedn = - talloc_strdup(msg, ldb_msg_find_string(ildb->rootDSE, "defaultNamingContext", "")); - } else { - msg->r.SearchRequest.basedn = talloc_strdup(msg, ""); - } + if (req->op.search.base == NULL) { + msg->r.SearchRequest.basedn = talloc_strdup(msg, ""); } else { - msg->r.SearchRequest.basedn = ldb_dn_linearize(msg, base); + msg->r.SearchRequest.basedn = ldb_dn_get_extended_linearized(msg, req->op.search.base, 0); } if (msg->r.SearchRequest.basedn == NULL) { - ldb_set_errstring(module->ldb, talloc_asprintf(module, "Unable to determine baseDN")); + ldb_set_errstring(ldb, "Unable to determine baseDN"); talloc_free(msg); return LDB_ERR_OPERATIONS_ERROR; } - if (scope == LDB_SCOPE_DEFAULT) { + if (req->op.search.scope == LDB_SCOPE_DEFAULT) { msg->r.SearchRequest.scope = LDB_SCOPE_SUBTREE; } else { - msg->r.SearchRequest.scope = scope; + msg->r.SearchRequest.scope = req->op.search.scope; } - + msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER; msg->r.SearchRequest.timelimit = 0; msg->r.SearchRequest.sizelimit = 0; msg->r.SearchRequest.attributesonly = 0; - msg->r.SearchRequest.tree = tree; - - for (n = 0; attrs && attrs[n]; n++) /* noop */ ; - msg->r.SearchRequest.num_attributes = n; - msg->r.SearchRequest.attributes = discard_const(attrs); - msg->controls = control_req; - - return ildb_request_send(module, msg, context, callback, timeout, handle); -} - -static int ildb_search_sync_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares) -{ - struct ldb_result *res; - int n; - - if (!context) { - ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context in callback")); - return LDB_ERR_OPERATIONS_ERROR; - } - - res = *((struct ldb_result **)context); - - if (!res || !ares) { - goto error; - } - - if (ares->type == LDB_REPLY_ENTRY) { - res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2); - if (! res->msgs) { - goto error; - } - - res->msgs[res->count + 1] = NULL; - - res->msgs[res->count] = talloc_steal(res->msgs, ares->message); - if (! res->msgs[res->count]) { - goto error; - } - - res->count++; - } - - if (ares->type == LDB_REPLY_REFERRAL) { - if (res->refs) { - for (n = 0; res->refs[n]; n++) /*noop*/ ; - } else { - n = 0; - } - - res->refs = talloc_realloc(res, res->refs, char *, n + 2); - if (! res->refs) { - goto error; - } - - res->refs[n] = talloc_steal(res->refs, ares->referral); - res->refs[n + 1] = NULL; - } - - if (ares->controls) { - res->controls = talloc_steal(res, ares->controls); - if (! res->controls) { - goto error; - } - } - - talloc_free(ares); - return LDB_SUCCESS; - -error: - talloc_free(ares); - talloc_free(res); - *((struct ldb_result **)context) = NULL; - return LDB_ERR_OPERATIONS_ERROR; -} - -/* - search for matching records using a synchronous function - */ -static int ildb_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_control **control_req, - struct ldb_result **res) -{ - struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); - struct ldb_async_handle *handle; - int ret; - - *res = talloc_zero(ildb, struct ldb_result); - if (! *res) { - return LDB_ERR_OPERATIONS_ERROR; - } - - ret = ildb_search_async(module, base, scope, tree, attrs, control_req, - res, &ildb_search_sync_callback, ildb->ldap->timeout, &handle); - - if (ret == LDB_SUCCESS) { - ret = ldb_async_wait(handle, LDB_WAIT_ALL); - talloc_free(handle); - } + msg->r.SearchRequest.tree = discard_const(req->op.search.tree); - if (ret != LDB_SUCCESS) { - talloc_free(*res); - } + for (n = 0; req->op.search.attrs && req->op.search.attrs[n]; n++) /* noop */ ; + msg->r.SearchRequest.num_attributes = n; + msg->r.SearchRequest.attributes = req->op.search.attrs; + msg->controls = req->controls; - return ret; + return ildb_request_send(ac, msg); } /* add a record */ -static int ildb_add_async(struct ldb_module *module, const struct ldb_message *ldb_msg, - void *context, - int (*callback)(struct ldb_context *, void *, struct ldb_async_result *), - int timeout, - struct ldb_async_handle **handle) +static int ildb_add(struct ildb_context *ac) { - struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); + struct ldb_request *req = ac->req; struct ldap_message *msg; struct ldap_mod **mods; int i,n; - *handle = NULL; - - /* ignore ltdb specials */ - if (ldb_dn_is_special(ldb_msg->dn)) { - return LDB_SUCCESS; - } - - msg = new_ldap_message(ildb->ldap); + msg = new_ldap_message(req); if (msg == NULL) { return LDB_ERR_OPERATIONS_ERROR; } msg->type = LDAP_TAG_AddRequest; - msg->r.AddRequest.dn = ldb_dn_linearize(msg, ldb_msg->dn); + msg->r.AddRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.add.message->dn, 0); if (msg->r.AddRequest.dn == NULL) { talloc_free(msg); return LDB_ERR_INVALID_DN_SYNTAX; } - mods = ildb_msg_to_mods(msg, &n, ldb_msg, 0); + mods = ildb_msg_to_mods(msg, &n, req->op.add.message, 0); if (mods == NULL) { talloc_free(msg); return LDB_ERR_OPERATIONS_ERROR; @@ -604,62 +514,33 @@ static int ildb_add_async(struct ldb_module *module, const struct ldb_message *l msg->r.AddRequest.attributes[i] = mods[i]->attrib; } - return ildb_request_send(module, msg, context, callback, timeout, handle); -} - -static int ildb_add(struct ldb_module *module, const struct ldb_message *msg) -{ - struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); - struct ldb_async_handle *handle; - int ret; - - ret = ildb_add_async(module, msg, - NULL, NULL, ildb->ldap->timeout, &handle); - - if (ret != LDB_SUCCESS) - return ret; - - ret = ldb_async_wait(handle, LDB_WAIT_ALL); - - talloc_free(handle); - return ret; + return ildb_request_send(ac, msg); } /* modify a record */ -static int ildb_modify_async(struct ldb_module *module, const struct ldb_message *ldb_msg, - void *context, - int (*callback)(struct ldb_context *, void *, struct ldb_async_result *), - int timeout, - struct ldb_async_handle **handle) +static int ildb_modify(struct ildb_context *ac) { - struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); + struct ldb_request *req = ac->req; struct ldap_message *msg; struct ldap_mod **mods; int i,n; - *handle = NULL; - - /* ignore ltdb specials */ - if (ldb_dn_is_special(ldb_msg->dn)) { - return LDB_SUCCESS; - } - - msg = new_ldap_message(ildb->ldap); + msg = new_ldap_message(req); if (msg == NULL) { return LDB_ERR_OPERATIONS_ERROR; } msg->type = LDAP_TAG_ModifyRequest; - msg->r.ModifyRequest.dn = ldb_dn_linearize(msg, ldb_msg->dn); + msg->r.ModifyRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.mod.message->dn, 0); if (msg->r.ModifyRequest.dn == NULL) { talloc_free(msg); return LDB_ERR_INVALID_DN_SYNTAX; } - mods = ildb_msg_to_mods(msg, &n, ldb_msg, 1); + mods = ildb_msg_to_mods(msg, &n, req->op.mod.message, 1); if (mods == NULL) { talloc_free(msg); return LDB_ERR_OPERATIONS_ERROR; @@ -676,361 +557,251 @@ static int ildb_modify_async(struct ldb_module *module, const struct ldb_message msg->r.ModifyRequest.mods[i] = *mods[i]; } - return ildb_request_send(module, msg, context, callback, timeout, handle); -} - -static int ildb_modify(struct ldb_module *module, const struct ldb_message *msg) -{ - struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); - struct ldb_async_handle *handle; - int ret; - - ret = ildb_modify_async(module, msg, - NULL, NULL, ildb->ldap->timeout, &handle); - - if (ret != LDB_SUCCESS) - return ret; - - ret = ldb_async_wait(handle, LDB_WAIT_ALL); - - talloc_free(handle); - return ret; + return ildb_request_send(ac, msg); } /* delete a record */ -static int ildb_delete_async(struct ldb_module *module, const struct ldb_dn *dn, - void *context, - int (*callback)(struct ldb_context *, void *, struct ldb_async_result *), - int timeout, - struct ldb_async_handle **handle) +static int ildb_delete(struct ildb_context *ac) { - struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); + struct ldb_request *req = ac->req; struct ldap_message *msg; - *handle = NULL; - - /* ignore ltdb specials */ - if (ldb_dn_is_special(dn)) { - return LDB_SUCCESS; - } - - msg = new_ldap_message(ildb->ldap); + msg = new_ldap_message(req); if (msg == NULL) { return LDB_ERR_OPERATIONS_ERROR; } msg->type = LDAP_TAG_DelRequest; - - msg->r.DelRequest.dn = ldb_dn_linearize(msg, dn); + + msg->r.DelRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.del.dn, 0); if (msg->r.DelRequest.dn == NULL) { talloc_free(msg); return LDB_ERR_INVALID_DN_SYNTAX; } - return ildb_request_send(module, msg, context, callback, timeout, handle); -} - -static int ildb_delete(struct ldb_module *module, const struct ldb_dn *dn) -{ - struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); - struct ldb_async_handle *handle; - int ret; - - ret = ildb_delete_async(module, dn, - NULL, NULL, ildb->ldap->timeout, &handle); - - if (ret != LDB_SUCCESS) - return ret; - - ret = ldb_async_wait(handle, LDB_WAIT_ALL); - - talloc_free(handle); - return ret; + return ildb_request_send(ac, msg); } /* rename a record */ -static int ildb_rename_async(struct ldb_module *module, - const struct ldb_dn *olddn, const struct ldb_dn *newdn, - void *context, - int (*callback)(struct ldb_context *, void *, struct ldb_async_result *), - int timeout, - struct ldb_async_handle **handle) +static int ildb_rename(struct ildb_context *ac) { - struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); + struct ldb_request *req = ac->req; struct ldap_message *msg; + const char *rdn_name; + const struct ldb_val *rdn_val; - *handle = NULL; - - /* ignore ltdb specials */ - if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) { - return LDB_SUCCESS; - } - - msg = new_ldap_message(ildb->ldap); + msg = new_ldap_message(req); if (msg == NULL) { return LDB_ERR_OPERATIONS_ERROR; } msg->type = LDAP_TAG_ModifyDNRequest; - msg->r.ModifyDNRequest.dn = ldb_dn_linearize(msg, olddn); + msg->r.ModifyDNRequest.dn = ldb_dn_get_extended_linearized(msg, req->op.rename.olddn, 0); if (msg->r.ModifyDNRequest.dn == NULL) { talloc_free(msg); return LDB_ERR_INVALID_DN_SYNTAX; } - msg->r.ModifyDNRequest.newrdn = - talloc_asprintf(msg, "%s=%s", - newdn->components[0].name, - ldb_dn_escape_value(msg, newdn->components[0].value)); + rdn_name = ldb_dn_get_rdn_name(req->op.rename.newdn); + rdn_val = ldb_dn_get_rdn_val(req->op.rename.newdn); + + if ((rdn_name != NULL) && (rdn_val != NULL)) { + msg->r.ModifyDNRequest.newrdn = + talloc_asprintf(msg, "%s=%s", rdn_name, + ldb_dn_escape_value(msg, *rdn_val)); + } else { + msg->r.ModifyDNRequest.newrdn = talloc_strdup(msg, ""); + } if (msg->r.ModifyDNRequest.newrdn == NULL) { talloc_free(msg); return LDB_ERR_OPERATIONS_ERROR; } msg->r.ModifyDNRequest.newsuperior = - ldb_dn_linearize(msg, - ldb_dn_get_parent(msg, newdn)); + ldb_dn_alloc_linearized(msg, ldb_dn_get_parent(msg, req->op.rename.newdn)); if (msg->r.ModifyDNRequest.newsuperior == NULL) { talloc_free(msg); return LDB_ERR_INVALID_DN_SYNTAX; } - msg->r.ModifyDNRequest.deleteolddn = True; + msg->r.ModifyDNRequest.deleteolddn = true; - return ildb_request_send(module, msg, context, callback, timeout, handle); -} - -static int ildb_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn) -{ - struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); - struct ldb_async_handle *handle; - int ret; - - ret = ildb_rename_async(module, olddn, newdn, - NULL, NULL, ildb->ldap->timeout, &handle); - - if (ret != LDB_SUCCESS) - return ret; - - ret = ldb_async_wait(handle, LDB_WAIT_ALL); - - talloc_free(handle); - return ret; + return ildb_request_send(ac, msg); } static int ildb_start_trans(struct ldb_module *module) { /* TODO implement a local locking mechanism here */ - return 0; + return LDB_SUCCESS; } static int ildb_end_trans(struct ldb_module *module) { /* TODO implement a local transaction mechanism here */ - return 0; + return LDB_SUCCESS; } static int ildb_del_trans(struct ldb_module *module) { /* TODO implement a local locking mechanism here */ - return 0; + return LDB_SUCCESS; } -static int ildb_request(struct ldb_module *module, struct ldb_request *req) +static bool ildb_dn_is_special(struct ldb_request *req) { - switch (req->operation) { - - case LDB_REQ_SEARCH: - return ildb_search_bytree(module, - req->op.search.base, - req->op.search.scope, - req->op.search.tree, - req->op.search.attrs, - req->controls, - &req->op.search.res); - - case LDB_REQ_ADD: - return ildb_add(module, req->op.add.message); - - case LDB_REQ_MODIFY: - return ildb_modify(module, req->op.mod.message); - - case LDB_REQ_DELETE: - return ildb_delete(module, req->op.del.dn); - - case LDB_REQ_RENAME: - return ildb_rename(module, - req->op.rename.olddn, - req->op.rename.newdn); - - case LDB_ASYNC_SEARCH: - return ildb_search_async(module, - req->op.search.base, - req->op.search.scope, - req->op.search.tree, - req->op.search.attrs, - req->controls, - req->async.context, - req->async.callback, - req->async.timeout, - &req->async.handle); - - case LDB_ASYNC_ADD: - return ildb_add_async(module, - req->op.add.message, - req->async.context, - req->async.callback, - req->async.timeout, - &req->async.handle); - - case LDB_ASYNC_MODIFY: - return ildb_modify_async(module, - req->op.mod.message, - req->async.context, - req->async.callback, - req->async.timeout, - &req->async.handle); - - case LDB_ASYNC_DELETE: - return ildb_delete_async(module, - req->op.del.dn, - req->async.context, - req->async.callback, - req->async.timeout, - &req->async.handle); - - case LDB_ASYNC_RENAME: - return ildb_rename_async(module, - req->op.rename.olddn, - req->op.rename.newdn, - req->async.context, - req->async.callback, - req->async.timeout, - &req->async.handle); + struct ldb_dn *dn = NULL; + switch (req->operation) { + case LDB_ADD: + dn = req->op.add.message->dn; + break; + case LDB_MODIFY: + dn = req->op.mod.message->dn; + break; + case LDB_DELETE: + dn = req->op.del.dn; + break; + case LDB_RENAME: + dn = req->op.rename.olddn; + break; default: - return -1; + break; + } + if (dn && ldb_dn_is_special(dn)) { + return true; } + return false; } -static int ildb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type) +static int ildb_handle_request(struct ldb_module *module, struct ldb_request *req) { - struct ildb_async_context *ac = talloc_get_type(handle->private_data, struct ildb_async_context); + struct ldb_context *ldb; + struct ildb_private *ildb; + struct ildb_context *ac; + struct tevent_timer *te; + int ret; - if (handle->state == LDB_ASYNC_DONE) { - return handle->status; + ildb = talloc_get_type(ldb_module_get_private(module), struct ildb_private); + ldb = ldb_module_get_ctx(module); + + if (req->starttime == 0 || req->timeout == 0) { + ldb_set_errstring(ldb, "Invalid timeout settings"); + return LDB_ERR_TIME_LIMIT_EXCEEDED; } - if (!ac) { + ac = talloc_zero(req, struct ildb_context); + if (ac == NULL) { + ldb_set_errstring(ldb, "Out of Memory"); return LDB_ERR_OPERATIONS_ERROR; } - handle->state = LDB_ASYNC_INIT; + ac->module = module; + ac->req = req; + ac->ildb = ildb; - switch(type) { - case LDB_WAIT_NONE: - if (event_loop_once(ac->req->conn->event.event_ctx) != 0) { - return LDB_ERR_OTHER; + if (ildb_dn_is_special(req)) { + + te = tevent_add_timer(ac->ildb->event_ctx, + ac, timeval_zero(), + ildb_auto_done_callback, ac); + if (NULL == te) { + return LDB_ERR_OPERATIONS_ERROR; } + + return LDB_SUCCESS; + } + + switch (ac->req->operation) { + case LDB_SEARCH: + ret = ildb_search(ac); break; - case LDB_WAIT_ALL: - while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) { - if (event_loop_once(ac->req->conn->event.event_ctx) != 0) { - return LDB_ERR_OTHER; - } - } + case LDB_ADD: + ret = ildb_add(ac); + break; + case LDB_MODIFY: + ret = ildb_modify(ac); + break; + case LDB_DELETE: + ret = ildb_delete(ac); + break; + case LDB_RENAME: + ret = ildb_rename(ac); break; default: - return LDB_ERR_OPERATIONS_ERROR; - } - - return handle->status; -} - -/* - fetch the rootDSE for later use -*/ -static int ildb_init(struct ldb_module *module) -{ - struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private); - struct ldb_result *res = NULL; - struct ldb_dn *empty_dn = ldb_dn_new(ildb); - int ret; - ret = ildb_search_bytree(module, empty_dn, LDB_SCOPE_BASE, - ldb_parse_tree(empty_dn, "dn=dc=rootDSE"), - NULL, NULL, &res); - if (ret == LDB_SUCCESS && res->count == 1) { - ildb->rootDSE = talloc_steal(ildb, res->msgs[0]); + /* no other op supported */ + ret = LDB_ERR_OPERATIONS_ERROR; + break; } - if (ret == LDB_SUCCESS) talloc_free(res); - talloc_free(empty_dn); - return LDB_SUCCESS; + return ret; } static const struct ldb_module_ops ildb_ops = { .name = "ldap", - .request = ildb_request, + .search = ildb_handle_request, + .add = ildb_handle_request, + .modify = ildb_handle_request, + .del = ildb_handle_request, + .rename = ildb_handle_request, +/* .request = ildb_handle_request, */ .start_transaction = ildb_start_trans, .end_transaction = ildb_end_trans, .del_transaction = ildb_del_trans, - .async_wait = ildb_async_wait, - .init_context = ildb_init }; /* connect to the database */ -static int ildb_connect(struct ldb_context *ldb, const char *url, - unsigned int flags, const char *options[]) +static int ildb_connect(struct ldb_context *ldb, const char *url, + unsigned int flags, const char *options[], + struct ldb_module **_module) { - struct ildb_private *ildb = NULL; + struct ldb_module *module; + struct ildb_private *ildb; NTSTATUS status; struct cli_credentials *creds; + struct loadparm_context *lp_ctx; - ildb = talloc(ldb, struct ildb_private); + module = ldb_module_new(ldb, ldb, "ldb_ildap backend", &ildb_ops); + if (!module) return -1; + + ildb = talloc(module, struct ildb_private); if (!ildb) { ldb_oom(ldb); goto failed; } + ldb_module_set_private(module, ildb); + + ildb->event_ctx = ldb_get_event_context(ldb); - ildb->rootDSE = NULL; - ildb->ldb = ldb; + lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"), + struct loadparm_context); - ildb->ldap = ldap_new_connection(ildb, ldb_get_opaque(ldb, "EventContext")); + ildb->ldap = ldap4_new_connection(ildb, lp_ctx, + ildb->event_ctx); if (!ildb->ldap) { ldb_oom(ldb); goto failed; } - if (flags == LDB_FLG_RECONNECT) { + if (flags & LDB_FLG_RECONNECT) { ldap_set_reconn_params(ildb->ldap, 10); } status = ldap_connect(ildb->ldap, url); if (!NT_STATUS_IS_OK(status)) { - ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n", - url, ldap_errstr(ildb->ldap, status)); - goto failed; - } - - ldb->modules = talloc(ldb, struct ldb_module); - if (!ldb->modules) { - ldb_oom(ldb); + ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s", + url, ldap_errstr(ildb->ldap, module, status)); goto failed; } - ldb->modules->ldb = ldb; - ldb->modules->prev = ldb->modules->next = NULL; - ldb->modules->private_data = ildb; - ldb->modules->ops = &ildb_ops; /* caller can optionally setup credentials using the opaque token 'credentials' */ creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials); @@ -1047,33 +818,40 @@ static int ildb_connect(struct ldb_context *ldb, const char *url, const char *password = cli_credentials_get_password(creds); status = ldap_bind_simple(ildb->ldap, bind_dn, password); if (!NT_STATUS_IS_OK(status)) { - ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n", - ldap_errstr(ildb->ldap, status)); + ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s", + ldap_errstr(ildb->ldap, module, status)); goto failed; } } else { - status = ldap_bind_sasl(ildb->ldap, creds); + status = ldap_bind_sasl(ildb->ldap, creds, lp_ctx); if (!NT_STATUS_IS_OK(status)) { - ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n", - ldap_errstr(ildb->ldap, status)); + ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s", + ldap_errstr(ildb->ldap, module, status)); goto failed; } } } + *_module = module; return 0; failed: - if (ldb->modules) { - ldb->modules->private_data = NULL; - } - talloc_free(ildb); + talloc_free(module); return -1; } -int ldb_ildap_init(void) -{ - return ldb_register_backend("ldap", ildb_connect) + - ldb_register_backend("ldapi", ildb_connect) + - ldb_register_backend("ldaps", ildb_connect); -} +_PUBLIC_ const struct ldb_backend_ops ldb_ldap_backend_ops = { + .name = "ldap", + .connect_fn = ildb_connect +}; + +_PUBLIC_ const struct ldb_backend_ops ldb_ldapi_backend_ops = { + .name = "ldapi", + .connect_fn = ildb_connect +}; + +_PUBLIC_ const struct ldb_backend_ops ldb_ldaps_backend_ops = { + .name = "ldaps", + .connect_fn = ildb_connect +}; +