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
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/>.
*/
/*
* author: Simo Sorce
*/
-#include "includes.h"
-#include "ldb/include/includes.h"
+#include "ldb_includes.h"
+#define LDAP_DEPRECATED 1
#include <ldap.h>
struct lldb_private {
LDAP *ldap;
- int timeout;
};
-struct lldb_async_context {
+struct lldb_context {
struct ldb_module *module;
+ struct ldb_request *req;
+
+ struct lldb_private *lldb;
+
+ struct ldb_control **controls;
int msgid;
- uint32_t timeout;
- void *context;
- int (*callback)(struct ldb_context *, void *, struct ldb_async_result *);
};
static int lldb_ldap_to_ldb(int err) {
return err;
}
-static struct ldb_async_handle *init_handle(struct lldb_private *lldb, struct ldb_module *module,
- void *context,
- int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
- int timeout)
-{
- struct lldb_async_context *ac;
- struct ldb_async_handle *h;
-
- h = talloc_zero(lldb, struct ldb_async_handle);
- if (h == NULL) {
- ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
- return NULL;
- }
-
- ac = talloc(h, struct lldb_async_context);
- if (ac == NULL) {
- ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
- talloc_free(h);
- return NULL;
- }
-
- h->private_data = (void *)ac;
-
- ac->module = module;
- ac->context = context;
- ac->callback = callback;
- ac->timeout = timeout;
- ac->msgid = 0;
-
- return h;
-}
/*
convert a ldb_message structure to a list of LDAPMod structures
ready for ldap_add() or ldap_modify()
}
for (i=0;i<count;i++) {
- el->values[i].data = talloc_memdup(el->values, bval[i]->bv_val, bval[i]->bv_len);
+ /* we have to ensure this is null terminated so that
+ ldb_msg_find_attr_as_string() can work */
+ el->values[i].data = talloc_size(el->values, bval[i]->bv_len+1);
if (!el->values[i].data) {
+ errno = ENOMEM;
return -1;
}
+ memcpy(el->values[i].data, bval[i]->bv_val, bval[i]->bv_len);
+ el->values[i].data[bval[i]->bv_len] = 0;
el->values[i].length = bval[i]->bv_len;
el->num_values++;
}
/*
search for matching records
*/
-static int lldb_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 lldb_search(struct lldb_context *lldb_ac)
{
- struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
- struct lldb_async_context *lldb_ac;
+ struct lldb_private *lldb = lldb_ac->lldb;
+ struct ldb_module *module = lldb_ac->module;
+ struct ldb_request *req = lldb_ac->req;
struct timeval tv;
int ldap_scope;
char *search_base;
char *expression;
int ret;
- 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(module->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(module->ldb, "Invalid expression parse tree");
return LDB_ERR_OPERATIONS_ERROR;
}
- if (control_req != NULL) {
+ if (req->controls != NULL) {
ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls are not yet supported by ldb_ldap backend!\n");
}
- *handle = init_handle(lldb, module, context, callback, timeout);
- if (*handle == NULL) {
- talloc_free(*handle);
- return LDB_ERR_OPERATIONS_ERROR;
- }
-
- lldb_ac = talloc_get_type((*handle)->private_data, struct lldb_async_context);
+ req->handle->state = LDB_ASYNC_PENDING;
- search_base = ldb_dn_linearize(lldb_ac, base);
- if (base == NULL) {
+ search_base = ldb_dn_alloc_linearized(lldb_ac, req->op.search.base);
+ if (req->op.search.base == NULL) {
search_base = talloc_strdup(lldb_ac, "");
}
if (search_base == NULL) {
- talloc_free(*handle);
return LDB_ERR_OPERATIONS_ERROR;
}
- expression = ldb_filter_from_tree(lldb_ac, tree);
+ expression = ldb_filter_from_tree(lldb_ac, req->op.search.tree);
if (expression == NULL) {
- talloc_free(*handle);
return LDB_ERR_OPERATIONS_ERROR;
}
- switch (scope) {
+ switch (req->op.search.scope) {
case LDB_SCOPE_BASE:
ldap_scope = LDAP_SCOPE_BASE;
break;
break;
}
- tv.tv_sec = timeout;
+ tv.tv_sec = req->timeout;
tv.tv_usec = 0;
ret = ldap_search_ext(lldb->ldap, search_base, ldap_scope,
expression,
- discard_const_p(char *, attrs),
+ discard_const_p(char *, req->op.search.attrs),
0,
NULL,
NULL,
&lldb_ac->msgid);
if (ret != LDAP_SUCCESS) {
- ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(ret)));
- talloc_free(*handle);
- *handle = NULL;
+ ldb_set_errstring(module->ldb, ldap_err2string(ret));
}
return lldb_ldap_to_ldb(ret);
}
-static int lldb_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->type == LDB_REPLY_DONE) {
- 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 lldb_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 lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
- struct ldb_async_handle *handle;
- int ret;
-
- *res = talloc_zero(lldb, struct ldb_result);
- if (! *res) {
- return LDB_ERR_OPERATIONS_ERROR;
- }
-
- ret = lldb_search_async(module, base, scope, tree, attrs, control_req,
- res, &lldb_search_sync_callback, lldb->timeout, &handle);
-
- if (ret != LDB_SUCCESS)
- return ret;
-
- return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
-}
-
/*
add a record
*/
-static int lldb_add_async(struct ldb_module *module, const struct ldb_message *msg,
- void *context,
- int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
- int timeout,
- struct ldb_async_handle **handle)
+static int lldb_add(struct lldb_context *lldb_ac)
{
- struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
- struct lldb_async_context *lldb_ac;
+ struct lldb_private *lldb = lldb_ac->lldb;
+ struct ldb_module *module = lldb_ac->module;
+ struct ldb_request *req = lldb_ac->req;
LDAPMod **mods;
char *dn;
int ret;
- /* ltdb specials should not reach this point */
- if (ldb_dn_is_special(msg->dn)) {
- return LDB_ERR_INVALID_DN_SYNTAX;
- }
-
- *handle = init_handle(lldb, module, context, callback, timeout);
- if (*handle == NULL) {
- return LDB_ERR_OPERATIONS_ERROR;
- }
-
- lldb_ac = talloc_get_type((*handle)->private_data, struct lldb_async_context);
+ req->handle->state = LDB_ASYNC_PENDING;
- mods = lldb_msg_to_mods(lldb_ac, msg, 0);
+ mods = lldb_msg_to_mods(lldb_ac, req->op.add.message, 0);
if (mods == NULL) {
- talloc_free(*handle);
return LDB_ERR_OPERATIONS_ERROR;
}
- dn = ldb_dn_linearize(lldb_ac, msg->dn);
+ dn = ldb_dn_alloc_linearized(lldb_ac, req->op.add.message->dn);
if (dn == NULL) {
- talloc_free(*handle);
return LDB_ERR_OPERATIONS_ERROR;
}
&lldb_ac->msgid);
if (ret != LDAP_SUCCESS) {
- ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(ret)));
- talloc_free(*handle);
+ ldb_set_errstring(module->ldb, ldap_err2string(ret));
}
return lldb_ldap_to_ldb(ret);
}
-static int lldb_add(struct ldb_module *module, const struct ldb_message *msg)
-{
- struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
- struct ldb_async_handle *handle;
- int ret;
-
- /* ldap does not understand ltdb specials */
- if (ldb_dn_is_special(msg->dn)) {
- return LDB_SUCCESS;
- }
-
- ret = lldb_add_async(module, msg, NULL, NULL, lldb->timeout, &handle);
-
- if (ret != LDB_SUCCESS)
- return ret;
-
- return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
-}
-
-
/*
modify a record
*/
-static int lldb_modify_async(struct ldb_module *module, const struct ldb_message *msg,
- void *context,
- int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
- int timeout,
- struct ldb_async_handle **handle)
+static int lldb_modify(struct lldb_context *lldb_ac)
{
- struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
- struct lldb_async_context *lldb_ac;
+ struct lldb_private *lldb = lldb_ac->lldb;
+ struct ldb_module *module = lldb_ac->module;
+ struct ldb_request *req = lldb_ac->req;
LDAPMod **mods;
char *dn;
int ret;
- /* ltdb specials should not reach this point */
- if (ldb_dn_is_special(msg->dn)) {
- return LDB_ERR_INVALID_DN_SYNTAX;
- }
-
- *handle = init_handle(lldb, module, context, callback, timeout);
- if (*handle == NULL) {
- return LDB_ERR_OPERATIONS_ERROR;
- }
-
- lldb_ac = talloc_get_type((*handle)->private_data, struct lldb_async_context);
+ req->handle->state = LDB_ASYNC_PENDING;
- mods = lldb_msg_to_mods(lldb_ac, msg, 1);
+ mods = lldb_msg_to_mods(lldb_ac, req->op.mod.message, 1);
if (mods == NULL) {
- talloc_free(*handle);
return LDB_ERR_OPERATIONS_ERROR;
}
- dn = ldb_dn_linearize(lldb_ac, msg->dn);
+ dn = ldb_dn_alloc_linearized(lldb_ac, req->op.mod.message->dn);
if (dn == NULL) {
- talloc_free(*handle);
return LDB_ERR_OPERATIONS_ERROR;
}
&lldb_ac->msgid);
if (ret != LDAP_SUCCESS) {
- ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(ret)));
- talloc_free(*handle);
+ ldb_set_errstring(module->ldb, ldap_err2string(ret));
}
return lldb_ldap_to_ldb(ret);
}
-static int lldb_modify(struct ldb_module *module, const struct ldb_message *msg)
-{
- struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
- struct ldb_async_handle *handle;
- int ret;
-
- /* ldap does not understand ltdb specials */
- if (ldb_dn_is_special(msg->dn)) {
- return LDB_SUCCESS;
- }
-
- ret = lldb_modify_async(module, msg, NULL, NULL, lldb->timeout, &handle);
-
- if (ret != LDB_SUCCESS)
- return ret;
-
- return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
-}
-
/*
delete a record
*/
-static int lldb_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 lldb_delete(struct lldb_context *lldb_ac)
{
- struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
- struct lldb_async_context *lldb_ac;
+ struct lldb_private *lldb = lldb_ac->lldb;
+ struct ldb_module *module = lldb_ac->module;
+ struct ldb_request *req = lldb_ac->req;
char *dnstr;
int ret;
-
- /* ltdb specials should not reach this point */
- if (ldb_dn_is_special(dn)) {
- return LDB_ERR_INVALID_DN_SYNTAX;
- }
- *handle = init_handle(lldb, module, context, callback, timeout);
- if (*handle == NULL) {
- return LDB_ERR_OPERATIONS_ERROR;
- }
-
- lldb_ac = talloc_get_type((*handle)->private_data, struct lldb_async_context);
+ req->handle->state = LDB_ASYNC_PENDING;
- dnstr = ldb_dn_linearize(lldb_ac, dn);
+ dnstr = ldb_dn_alloc_linearized(lldb_ac, req->op.del.dn);
ret = ldap_delete_ext(lldb->ldap, dnstr,
NULL,
&lldb_ac->msgid);
if (ret != LDAP_SUCCESS) {
- ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(ret)));
- talloc_free(*handle);
+ ldb_set_errstring(module->ldb, ldap_err2string(ret));
}
return lldb_ldap_to_ldb(ret);
}
-static int lldb_delete(struct ldb_module *module, const struct ldb_dn *dn)
-{
- struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
- struct ldb_async_handle *handle;
- int ret;
-
- /* ignore ltdb specials */
- if (ldb_dn_is_special(dn)) {
- return LDB_SUCCESS;
- }
-
- ret = lldb_delete_async(module, dn, NULL, NULL, lldb->timeout, &handle);
-
- if (ret != LDB_SUCCESS)
- return ret;
-
- return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
-}
-
/*
rename a record
*/
-static int lldb_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 lldb_rename(struct lldb_context *lldb_ac)
{
- struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
- struct lldb_async_context *lldb_ac;
+ struct lldb_private *lldb = lldb_ac->lldb;
+ struct ldb_module *module = lldb_ac->module;
+ struct ldb_request *req = lldb_ac->req;
char *old_dn;
char *newrdn;
char *parentdn;
int ret;
-
- /* ltdb specials should not reach this point */
- if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
- return LDB_ERR_INVALID_DN_SYNTAX;
- }
- *handle = init_handle(lldb, module, context, callback, timeout);
- if (*handle == NULL) {
- return LDB_ERR_OPERATIONS_ERROR;
- }
+ req->handle->state = LDB_ASYNC_PENDING;
- lldb_ac = talloc_get_type((*handle)->private_data, struct lldb_async_context);
-
- old_dn = ldb_dn_linearize(lldb_ac, olddn);
+ old_dn = ldb_dn_alloc_linearized(lldb_ac, req->op.rename.olddn);
if (old_dn == NULL) {
- talloc_free(*handle);
return LDB_ERR_OPERATIONS_ERROR;
}
newrdn = talloc_asprintf(lldb_ac, "%s=%s",
- newdn->components[0].name,
- ldb_dn_escape_value(lldb, newdn->components[0].value));
+ ldb_dn_get_rdn_name(req->op.rename.newdn),
+ ldb_dn_escape_value(lldb, *(ldb_dn_get_rdn_val(req->op.rename.newdn))));
if (!newrdn) {
- talloc_free(*handle);
return LDB_ERR_OPERATIONS_ERROR;
}
- parentdn = ldb_dn_linearize(lldb_ac, ldb_dn_get_parent(lldb_ac, newdn));
+ parentdn = ldb_dn_alloc_linearized(lldb_ac, ldb_dn_get_parent(lldb_ac, req->op.rename.newdn));
if (!parentdn) {
- talloc_free(*handle);
return LDB_ERR_OPERATIONS_ERROR;
}
&lldb_ac->msgid);
if (ret != LDAP_SUCCESS) {
- ldb_set_errstring(module->ldb, talloc_strdup(module, ldap_err2string(ret)));
- talloc_free(*handle);
+ ldb_set_errstring(module->ldb, ldap_err2string(ret));
}
return lldb_ldap_to_ldb(ret);
}
-static int lldb_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
+static int lldb_start_trans(struct ldb_module *module)
{
- struct lldb_private *lldb = talloc_get_type(module->private_data, struct lldb_private);
- struct ldb_async_handle *handle;
- int ret;
+ /* TODO implement a local transaction mechanism here */
- /* ignore ltdb specials */
- if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
- return LDB_SUCCESS;
- }
+ return LDB_SUCCESS;
+}
- ret = lldb_rename_async(module, olddn, newdn, NULL, NULL, lldb->timeout, &handle);
+static int lldb_end_trans(struct ldb_module *module)
+{
+ /* TODO implement a local transaction mechanism here */
- if (ret != LDB_SUCCESS)
- return ret;
+ return LDB_SUCCESS;
+}
+
+static int lldb_del_trans(struct ldb_module *module)
+{
+ /* TODO implement a local transaction mechanism here */
- return ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
+ return LDB_SUCCESS;
}
-static int lldb_parse_result(struct ldb_async_handle *handle, LDAPMessage *result)
+void lldb_request_done(struct ldb_request *req,
+ struct ldb_control **ctrls, int error)
{
- struct lldb_async_context *ac = talloc_get_type(handle->private_data, struct lldb_async_context);
- struct lldb_private *lldb = talloc_get_type(ac->module->private_data, struct lldb_private);
- struct ldb_async_result *ares = NULL;
- LDAPMessage *msg;
- int type;
+ struct ldb_reply *ares;
+
+ ares = talloc_zero(req, struct ldb_reply);
+ if (!ares) {
+ ldb_oom(req->handle->ldb);
+ req->callback(req, NULL);
+ return;
+ }
+ ares->type = LDB_REPLY_DONE;
+ ares->controls = talloc_steal(ares, ctrls);
+ ares->error = error;
+
+ req->callback(req, ares);
+}
+
+/* return false if the request is still in progress
+ * return true if the request is completed
+ */
+static bool lldb_parse_result(struct lldb_context *ac, LDAPMessage *result)
+{
+ struct lldb_private *lldb = ac->lldb;
+ LDAPControl **serverctrlsp = NULL;
+ char **referralsp = NULL;
char *matcheddnp = NULL;
char *errmsgp = NULL;
- char **referralsp = NULL;
- LDAPControl **serverctrlsp = NULL;
+ LDAPMessage *msg;
+ int type;
+ struct ldb_message *ldbmsg;
+ char *referral;
+ bool callback_failed;
+ bool request_done;
+ bool lret;
+ int ret;
+ int i;
type = ldap_msgtype(result);
+ callback_failed = false;
+ request_done = false;
switch (type) {
-
case LDAP_RES_SEARCH_ENTRY:
+
msg = ldap_first_entry(lldb->ldap, result);
if (msg != NULL) {
BerElement *berptr = NULL;
char *attr, *dn;
- ares = talloc_zero(ac, struct ldb_async_result);
- if (!ares) {
- handle->status = LDB_ERR_OPERATIONS_ERROR;
- goto error;
- }
-
- ares->message = ldb_msg_new(ares);
- if (!ares->message) {
- handle->status = LDB_ERR_OPERATIONS_ERROR;
- goto error;
+ ldbmsg = ldb_msg_new(ac);
+ if (!ldbmsg) {
+ ret = LDB_ERR_OPERATIONS_ERROR;
+ break;
}
dn = ldap_get_dn(lldb->ldap, msg);
if (!dn) {
- handle->status = LDB_ERR_OPERATIONS_ERROR;
- goto error;
+ talloc_free(ldbmsg);
+ ret = LDB_ERR_OPERATIONS_ERROR;
+ break;
}
- ares->message->dn = ldb_dn_explode_or_special(ares->message, dn);
- if (ares->message->dn == NULL) {
- handle->status = LDB_ERR_OPERATIONS_ERROR;
- goto error;
+ ldbmsg->dn = ldb_dn_new(ldbmsg, ac->module->ldb, dn);
+ if ( ! ldb_dn_validate(ldbmsg->dn)) {
+ talloc_free(ldbmsg);
+ ret = LDB_ERR_OPERATIONS_ERROR;
+ break;
}
ldap_memfree(dn);
- ares->message->num_elements = 0;
- ares->message->elements = NULL;
- ares->message->private_data = NULL;
+ ldbmsg->num_elements = 0;
+ ldbmsg->elements = NULL;
/* loop over all attributes */
for (attr=ldap_first_attribute(lldb->ldap, msg, &berptr);
bval = ldap_get_values_len(lldb->ldap, msg, attr);
if (bval) {
- lldb_add_msg_attr(ac->module->ldb, ares->message, attr, bval);
+ lldb_add_msg_attr(ac->module->ldb, ldbmsg, attr, bval);
ldap_value_free_len(bval);
- }
-
- ldap_memfree(attr);
+ }
}
if (berptr) ber_free(berptr, 0);
+ ret = ldb_module_send_entry(ac->req, ldbmsg);
+ if (ret != LDB_SUCCESS) {
- ares->type = LDB_REPLY_ENTRY;
- handle->state = LDB_ASYNC_PENDING;
- handle->status = ac->callback(ac->module->ldb, ac->context, ares);
-
- ldap_msgfree(result);
+ callback_failed = true;
+ }
+ } else {
+ ret = LDB_ERR_OPERATIONS_ERROR;
}
break;
case LDAP_RES_SEARCH_REFERENCE:
- if (ldap_parse_result(lldb->ldap, result, &handle->status,
+
+ if (ldap_parse_result(lldb->ldap, result, &ret,
&matcheddnp, &errmsgp,
- &referralsp, &serverctrlsp, 1) != LDAP_SUCCESS) {
- handle->status = LDB_ERR_OPERATIONS_ERROR;
- goto error;
+ &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
+ ret = LDB_ERR_OPERATIONS_ERROR;
}
- if (referralsp == NULL) {
- handle->status = LDB_ERR_PROTOCOL_ERROR;
- goto error;
+ if (ret != LDB_SUCCESS) {
+ break;
}
-
- ares = talloc_zero(ac, struct ldb_async_result);
- if (!ares) {
- handle->status = LDB_ERR_OPERATIONS_ERROR;
- goto error;
+ if (referralsp == NULL) {
+ ret = LDB_ERR_PROTOCOL_ERROR;
+ break;
}
- ares->referral = talloc_strdup(ares, *referralsp);
- ares->type = LDB_REPLY_REFERRAL;
- handle->state = LDB_ASYNC_PENDING;
- handle->status = ac->callback(ac->module->ldb, ac->context, ares);
+ for (i = 0; referralsp[i]; i++) {
+ referral = talloc_strdup(ac, referralsp[i]);
+ ret = ldb_module_send_referral(ac->req, referral);
+ if (ret != LDB_SUCCESS) {
+ callback_failed = true;
+ break;
+ }
+ }
break;
case LDAP_RES_SEARCH_RESULT:
- if (ldap_parse_result(lldb->ldap, result, &handle->status,
+ case LDAP_RES_MODIFY:
+ case LDAP_RES_ADD:
+ case LDAP_RES_DELETE:
+ case LDAP_RES_MODDN:
+
+ if (ldap_parse_result(lldb->ldap, result, &ret,
&matcheddnp, &errmsgp,
- &referralsp, &serverctrlsp, 1) != LDAP_SUCCESS) {
- handle->status = LDB_ERR_OPERATIONS_ERROR;
- goto error;
+ &referralsp, &serverctrlsp, 0) != LDAP_SUCCESS) {
+ ret = LDB_ERR_OPERATIONS_ERROR;
}
-
- ares = talloc_zero(ac, struct ldb_async_result);
- if (!ares) {
- handle->status = LDB_ERR_OPERATIONS_ERROR;
- goto error;
+ if (ret != LDB_SUCCESS) {
+ break;
}
if (serverctrlsp != NULL) {
/* FIXME: transform the LDAPControl list into an ldb_control one */
- ares->controls = NULL;
+ ac->controls = NULL;
}
-
- ares->type = LDB_REPLY_DONE;
- handle->state = LDB_ASYNC_DONE;
- handle->status = ac->callback(ac->module->ldb, ac->context, ares);
+ request_done = true;
break;
- case LDAP_RES_MODIFY:
- case LDAP_RES_ADD:
- case LDAP_RES_DELETE:
- case LDAP_RES_MODDN:
- if (ldap_parse_result(lldb->ldap, result, &handle->status,
- &matcheddnp, &errmsgp,
- &referralsp, &serverctrlsp, 1) != LDAP_SUCCESS) {
- handle->status = LDB_ERR_OPERATIONS_ERROR;
- goto error;
- }
- if (ac->callback && handle->status == LDB_SUCCESS) {
- ares = NULL; /* FIXME: build a corresponding ares to pass on */
- handle->status = ac->callback(ac->module->ldb, ac->context, ares);
- }
- handle->state = LDB_ASYNC_DONE;
+ default:
+ ret = LDB_ERR_PROTOCOL_ERROR;
break;
+ }
- default:
- handle->state = LDB_ERR_PROTOCOL_ERROR;
+ 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) {
+
+ /* tell lldb_wait to remove the request from the
+ * queue */
+ lret = true;
+ goto free_and_return;
+ }
+
+ request_done = true;
}
+ if (request_done) {
+ lldb_request_done(ac->req, ac->controls, ret);
+ lret = true;
+ goto free_and_return;
+ }
+
+ lret = false;
+
+free_and_return:
+
if (matcheddnp) ldap_memfree(matcheddnp);
+ if (errmsgp && *errmsgp) {
+ ldb_set_errstring(ac->module->ldb, errmsgp);
+ }
if (errmsgp) {
- ldb_set_errstring(ac->module->ldb, talloc_strdup(ac->module, errmsgp));
ldap_memfree(errmsgp);
}
if (referralsp) ldap_value_free(referralsp);
if (serverctrlsp) ldap_controls_free(serverctrlsp);
- return handle->status;
+ ldap_msgfree(result);
+
+ return lret;
+}
+
+static void lldb_timeout(struct event_context *ev,
+ struct timed_event *te,
+ struct timeval t,
+ void *private_data)
+{
+ struct lldb_context *ac;
+ ac = talloc_get_type(private_data, struct lldb_context);
-error:
- handle->state = LDB_ASYNC_DONE;
- return handle->status;
+ lldb_request_done(ac->req, NULL, LDB_ERR_TIME_LIMIT_EXCEEDED);
}
-static int lldb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
+static void lldb_callback(struct event_context *ev,
+ struct timed_event *te,
+ struct timeval t,
+ void *private_data)
{
- struct lldb_async_context *ac = talloc_get_type(handle->private_data, struct lldb_async_context);
- struct lldb_private *lldb = talloc_get_type(ac->module->private_data, struct lldb_private);
- struct timeval timeout;
+ struct lldb_context *ac;
+ struct timed_event *lte;
+ struct timeval tv;
LDAPMessage *result;
- int ret;
+ int lret;
+
+ ac = talloc_get_type(private_data, struct lldb_context);
if (!ac->msgid) {
- return LDB_ERR_OPERATIONS_ERROR;
+ lldb_request_done(ac->req, NULL, LDB_ERR_OPERATIONS_ERROR);
+ return;
}
- handle->status = LDB_SUCCESS;
- handle->state = LDB_ASYNC_INIT;
-
- switch(type) {
- case LDB_WAIT_NONE:
- timeout.tv_sec = 0;
- timeout.tv_usec = 0;
- ret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
- if (ret == -1) {
- handle->status = LDB_ERR_OPERATIONS_ERROR;
- return handle->status;
- }
- if (ret == 0) {
- handle->status = LDB_SUCCESS;
- return handle->status;
- }
- ret = lldb_parse_result(handle, result);
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ lret = ldap_result(ac->lldb->ldap, ac->msgid, 0, &tv, &result);
+ if (lret == 0) {
+ goto respin;
+ }
+ if (lret == -1) {
+ lldb_request_done(ac->req, NULL, LDB_ERR_OPERATIONS_ERROR);
+ return;
+ }
+
+ if ( ! lldb_parse_result(ac, result)) {
+ goto respin;
+ }
+
+ return;
+
+respin:
+ tv.tv_sec = 0;
+ tv.tv_usec = 100;
+ lte = event_add_timed(ev, ac, tv, lldb_callback, ac);
+ if (NULL == lte) {
+ lldb_request_done(ac->req, NULL, LDB_ERR_OPERATIONS_ERROR);
+ }
+}
+
+static bool lldb_dn_is_special(struct ldb_request *req)
+{
+ struct ldb_dn *dn = NULL;
+
+ switch (req->operation) {
+ case LDB_ADD:
+ dn = req->op.add.message->dn;
break;
- case LDB_WAIT_ONCE:
- timeout.tv_sec = ac->timeout;
- timeout.tv_usec = 0;
- ret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
- if (ret == -1 || ret == 0) {
- handle->status = LDB_ERR_OPERATIONS_ERROR;
- return handle->status;
- }
- ret = lldb_parse_result(handle, result);
+ case LDB_MODIFY:
+ dn = req->op.mod.message->dn;
break;
- case LDB_WAIT_ALL:
- timeout.tv_sec = ac->timeout;
- timeout.tv_usec = 0;
- while (handle->status == LDB_SUCCESS && handle->state != LDB_ASYNC_DONE) {
- ret = ldap_result(lldb->ldap, ac->msgid, 0, &timeout, &result);
- if (ret == -1 || ret == 0) {
- handle->status = LDB_ERR_OPERATIONS_ERROR;
- return handle->status;
- }
- ret = lldb_parse_result(handle, result);
- if (ret != LDB_SUCCESS) {
- return ret;
- }
- }
+ case LDB_DELETE:
+ dn = req->op.del.dn;
+ break;
+ case LDB_RENAME:
+ dn = req->op.rename.olddn;
break;
default:
- ret = LDB_ERR_OPERATIONS_ERROR;
+ break;
}
- return ret;
+ if (dn && ldb_dn_is_special(dn)) {
+ return true;
+ }
+ return false;
}
-static int lldb_start_trans(struct ldb_module *module)
+static void lldb_auto_done_callback(struct event_context *ev,
+ struct timed_event *te,
+ struct timeval t,
+ void *private_data)
{
- /* TODO implement a local transaction mechanism here */
+ struct lldb_context *ac;
- return 0;
+ ac = talloc_get_type(private_data, struct lldb_context);
+ lldb_request_done(ac->req, NULL, LDB_SUCCESS);
}
-static int lldb_end_trans(struct ldb_module *module)
+static int lldb_handle_request(struct ldb_module *module, struct ldb_request *req)
{
- /* TODO implement a local transaction mechanism here */
+ struct lldb_private *lldb;
+ struct lldb_context *ac;
+ struct event_context *ev;
+ struct timed_event *te;
+ struct timeval tv;
+ int ret;
- return 0;
-}
+ lldb = talloc_get_type(module->private_data, struct lldb_private);
-static int lldb_del_trans(struct ldb_module *module)
-{
- /* TODO implement a local transaction mechanism here */
+ if (req->starttime == 0 || req->timeout == 0) {
+ ldb_set_errstring(module->ldb, "Invalid timeout settings");
+ return LDB_ERR_TIME_LIMIT_EXCEEDED;
+ }
- return 0;
-}
+ ev = ldb_get_event_context(module->ldb);
+ if (NULL == ev) {
+ return LDB_ERR_OPERATIONS_ERROR;
+ }
-static int lldb_request(struct ldb_module *module, struct ldb_request *req)
-{
- switch (req->operation) {
+ ac = talloc_zero(module->ldb, struct lldb_context);
+ if (ac == NULL) {
+ ldb_set_errstring(module->ldb, "Out of Memory");
+ return LDB_ERR_OPERATIONS_ERROR;
+ }
- case LDB_REQ_SEARCH:
- return lldb_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 lldb_add(module, req->op.add.message);
-
- case LDB_REQ_MODIFY:
- return lldb_modify(module, req->op.mod.message);
-
- case LDB_REQ_DELETE:
- return lldb_delete(module, req->op.del.dn);
-
- case LDB_REQ_RENAME:
- return lldb_rename(module,
- req->op.rename.olddn,
- req->op.rename.newdn);
-
- case LDB_ASYNC_SEARCH:
- return lldb_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 lldb_add_async(module,
- req->op.add.message,
- req->async.context,
- req->async.callback,
- req->async.timeout,
- &req->async.handle);
-
- case LDB_ASYNC_MODIFY:
- return lldb_modify_async(module,
- req->op.mod.message,
- req->async.context,
- req->async.callback,
- req->async.timeout,
- &req->async.handle);
-
- case LDB_ASYNC_DELETE:
- return lldb_delete_async(module,
- req->op.del.dn,
- req->async.context,
- req->async.callback,
- req->async.timeout,
- &req->async.handle);
-
- case LDB_ASYNC_RENAME:
- return lldb_rename_async(module,
- req->op.rename.olddn,
- req->op.rename.newdn,
- req->async.context,
- req->async.callback,
- req->async.timeout,
- &req->async.handle);
+ ac->module = module;
+ ac->req = req;
+ ac->lldb = lldb;
+ ac->msgid = 0;
+
+ if (lldb_dn_is_special(req)) {
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ te = event_add_timed(ev, ac, tv,
+ lldb_auto_done_callback, ac);
+ if (NULL == te) {
+ return LDB_ERR_OPERATIONS_ERROR;
+ }
+ return LDB_SUCCESS;
+ }
+
+ switch (ac->req->operation) {
+ case LDB_SEARCH:
+ ret = lldb_search(ac);
+ break;
+ case LDB_ADD:
+ ret = lldb_add(ac);
+ break;
+ case LDB_MODIFY:
+ ret = lldb_modify(ac);
+ break;
+ case LDB_DELETE:
+ ret = lldb_delete(ac);
+ break;
+ case LDB_RENAME:
+ ret = lldb_rename(ac);
+ break;
default:
- return -1;
+ /* no other op supported */
+ ret = LDB_ERR_OPERATIONS_ERROR;
+ break;
+ }
+
+ if (ret != LDB_SUCCESS) {
+ lldb_request_done(req, NULL, ret);
+ return ret;
+ }
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ te = event_add_timed(ev, ac, tv, lldb_callback, ac);
+ if (NULL == te) {
+ return LDB_ERR_OPERATIONS_ERROR;
+ }
+
+
+ tv.tv_sec = req->starttime + req->timeout;
+ tv.tv_usec = 0;
+ te = event_add_timed(ev, ac, tv, lldb_timeout, ac);
+ if (NULL == te) {
+ return LDB_ERR_OPERATIONS_ERROR;
}
-}
-static int lldb_init_2(struct ldb_module *module)
-{
return LDB_SUCCESS;
}
static const struct ldb_module_ops lldb_ops = {
.name = "ldap",
- .request = lldb_request,
+ .search = lldb_handle_request,
+ .add = lldb_handle_request,
+ .modify = lldb_handle_request,
+ .del = lldb_handle_request,
+ .rename = lldb_handle_request,
+ .request = lldb_handle_request,
.start_transaction = lldb_start_trans,
.end_transaction = lldb_end_trans,
.del_transaction = lldb_del_trans,
- .second_stage_init = lldb_init_2
};
-static int lldb_destructor(void *p)
+static int lldb_destructor(struct lldb_private *lldb)
{
- struct lldb_private *lldb = p;
ldap_unbind(lldb->ldap);
return 0;
}
/*
connect to the database
*/
-int lldb_connect(struct ldb_context *ldb,
- const char *url,
- unsigned int flags,
- const char *options[])
+static int lldb_connect(struct ldb_context *ldb,
+ const char *url,
+ unsigned int flags,
+ const char *options[],
+ struct ldb_module **_module)
{
- struct lldb_private *lldb = NULL;
+ struct ldb_module *module;
+ struct lldb_private *lldb;
int version = 3;
int ret;
- lldb = talloc(ldb, struct lldb_private);
+ module = talloc(ldb, struct ldb_module);
+ if (module == NULL) {
+ ldb_oom(ldb);
+ talloc_free(lldb);
+ return -1;
+ }
+ talloc_set_name_const(module, "ldb_ldap backend");
+ module->ldb = ldb;
+ module->prev = module->next = NULL;
+ module->ops = &lldb_ops;
+
+ lldb = talloc(module, struct lldb_private);
if (!lldb) {
ldb_oom(ldb);
goto failed;
}
-
- lldb->ldap = NULL;
- lldb->timeout = 120; /* TODO: get timeout from options ? */
+ module->private_data = lldb;
+ lldb->ldap = NULL;
ret = ldap_initialize(&lldb->ldap, url);
if (ret != LDAP_SUCCESS) {
goto failed;
}
- ldb->modules = talloc(ldb, struct ldb_module);
- if (!ldb->modules) {
- ldb_oom(ldb);
- goto failed;
- }
- ldb->modules->ldb = ldb;
- ldb->modules->prev = ldb->modules->next = NULL;
- ldb->modules->private_data = lldb;
- ldb->modules->ops = &lldb_ops;
-
- ldb->async_wait = &lldb_async_wait;
-
+ *_module = module;
return 0;
failed:
- talloc_free(lldb);
+ talloc_free(module);
return -1;
}
+const struct ldb_backend_ops ldb_ldap_backend_ops = {
+ .name = "ldap",
+ .connect_fn = lldb_connect
+};
+
+const struct ldb_backend_ops ldb_ldapi_backend_ops = {
+ .name = "ldapi",
+ .connect_fn = lldb_connect
+};
+
+const struct ldb_backend_ops ldb_ldaps_backend_ops = {
+ .name = "ldaps",
+ .connect_fn = lldb_connect
+};