r23795: more v2->v3 conversion
[samba.git] / source / lib / ldb / ldb_sqlite3 / ldb_sqlite3.c
index a8dc8fe3a2e00d05d07634fdc8285f972d7da83d..efb5c1da3f3009205f39e1a0afff9ffaf6acc642 100644 (file)
@@ -2,7 +2,7 @@
    ldb database library
    
    Copyright (C) Derrell Lipman  2005
-   Copyright (C) Simo Sorce 2005
+   Copyright (C) Simo Sorce 2005-2006
    
    ** NOTE! The following LGPL license applies to the ldb
    ** library. This does NOT imply that all of Samba is released
@@ -11,7 +11,7 @@
    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
@@ -33,8 +33,7 @@
  *  Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
  */
 
-#include "includes.h"
-#include "ldb/include/includes.h"
+#include "ldb_includes.h"
 
 #include <sqlite3.h>
 
@@ -44,46 +43,49 @@ struct lsqlite3_private {
         sqlite3 *sqlite;
 };
 
-struct lsql_async_context {
+struct lsql_context {
        struct ldb_module *module;
 
        /* search stuff */
        long long current_eid;
        const char * const * attrs;
-       struct ldb_async_result *ares;
+       struct ldb_reply *ares;
 
        /* async stuff */
        void *context;
-       int (*callback)(struct ldb_context *, void *, struct ldb_async_result *);
+       int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
 };
 
-static struct ldb_async_handle *init_lsql_handle(struct lsqlite3_private *lsqlite3, struct ldb_module *module,
-                                                void *context,
-                                                int (*callback)(struct ldb_context *, void *, struct ldb_async_result *))
+static struct ldb_handle *init_handle(struct lsqlite3_private *lsqlite3,
+                                       struct ldb_module *module,
+                                       struct ldb_request *req)
 {
-       struct lsql_async_context *ac;
-       struct ldb_async_handle *h;
+       struct lsql_context *ac;
+       struct ldb_handle *h;
 
-       h = talloc_zero(lsqlite3, struct ldb_async_handle);
+       h = talloc_zero(lsqlite3, struct ldb_handle);
        if (h == NULL) {
-               ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
+               ldb_set_errstring(module->ldb, "Out of Memory");
                return NULL;
        }
 
        h->module = module;
 
-       ac = talloc(h, struct lsql_async_context);
+       ac = talloc(h, struct lsql_context);
        if (ac == NULL) {
-               ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
+               ldb_set_errstring(module->ldb, "Out of Memory");
                talloc_free(h);
                return NULL;
        }
 
        h->private_data = (void *)ac;
 
+       h->state = LDB_ASYNC_INIT;
+       h->status = LDB_SUCCESS;
+
        ac->module = module;
-       ac->context = context;
-       ac->callback = callback;
+       ac->context = req->context;
+       ac->callback = req->callback;
 
        return h;
 }
@@ -280,7 +282,7 @@ static char *parsetree_to_sql(struct ldb_module *module,
                              void *mem_ctx,
                              const struct ldb_parse_tree *t)
 {
-       const struct ldb_attrib_handler *h;
+       const struct ldb_schema_attribute *a;
        struct ldb_val value, subval;
        char *wild_card_string;
        char *child, *tmp;
@@ -340,10 +342,10 @@ static char *parsetree_to_sql(struct ldb_module *module,
                */
                attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
                if (attr == NULL) return NULL;
-               h = ldb_attrib_handler(module->ldb, attr);
+               a = ldb_schema_attribute_by_name(module->ldb, attr);
 
                /* Get a canonicalised copy of the data */
-               h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
+               a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
                if (value.data == NULL) {
                        return NULL;
                }
@@ -366,8 +368,8 @@ static char *parsetree_to_sql(struct ldb_module *module,
 
                } else if (strcasecmp(t->u.equality.attr, "dn") == 0) {
                        /* DN query is a special ldb case */
-                       char *cdn = ldb_dn_linearize_casefold(module->ldb,
-                                                             ldb_dn_explode(module->ldb,
+                       const char *cdn = ldb_dn_get_casefold(
+                                               ldb_dn_new(mem_ctx, module->ldb,
                                                              (const char *)value.data));
 
                        return lsqlite3_tprintf(mem_ctx,
@@ -404,13 +406,13 @@ static char *parsetree_to_sql(struct ldb_module *module,
 
                attr = ldb_attr_casefold(mem_ctx, t->u.substring.attr);
                if (attr == NULL) return NULL;
-               h = ldb_attrib_handler(module->ldb, attr);
+               a = ldb_schema_attribute_by_name(module->ldb, attr);
 
                subval.data = (void *)wild_card_string;
                subval.length = strlen(wild_card_string) + 1;
 
                /* Get a canonicalised copy of the data */
-               h->canonicalise_fn(module->ldb, mem_ctx, &(subval), &value);
+               a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(subval), &value);
                if (value.data == NULL) {
                        return NULL;
                }
@@ -425,10 +427,10 @@ static char *parsetree_to_sql(struct ldb_module *module,
        case LDB_OP_GREATER:
                attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
                if (attr == NULL) return NULL;
-               h = ldb_attrib_handler(module->ldb, attr);
+               a = ldb_schema_attribute_by_name(module->ldb, attr);
 
                /* Get a canonicalised copy of the data */
-               h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
+               a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
                if (value.data == NULL) {
                        return NULL;
                }
@@ -444,10 +446,10 @@ static char *parsetree_to_sql(struct ldb_module *module,
        case LDB_OP_LESS:
                attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
                if (attr == NULL) return NULL;
-               h = ldb_attrib_handler(module->ldb, attr);
+               a = ldb_schema_attribute_by_name(module->ldb, attr);
 
                /* Get a canonicalised copy of the data */
-               h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
+               a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
                if (value.data == NULL) {
                        return NULL;
                }
@@ -476,10 +478,10 @@ static char *parsetree_to_sql(struct ldb_module *module,
        case LDB_OP_APPROX:
                attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
                if (attr == NULL) return NULL;
-               h = ldb_attrib_handler(module->ldb, attr);
+               a = ldb_schema_attribute_by_name(module->ldb, attr);
 
                /* Get a canonicalised copy of the data */
-               h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
+               a->syntax->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
                if (value.data == NULL) {
                        return NULL;
                }
@@ -617,7 +619,7 @@ static void lsqlite3_compare(sqlite3_context *ctx, int argc,
        const char *func = (const char *)sqlite3_value_text(argv[1]);
        const char *cmp = (const char *)sqlite3_value_text(argv[2]);
        const char *attr = (const char *)sqlite3_value_text(argv[3]);
-       const struct ldb_attrib_handler *h;
+       const struct ldb_schema_attribute *a;
        struct ldb_val valX;
        struct ldb_val valY;
        int ret;
@@ -625,12 +627,12 @@ static void lsqlite3_compare(sqlite3_context *ctx, int argc,
        switch (func[0]) {
        /* greater */
        case '>': /* >= */
-               h = ldb_attrib_handler(ldb, attr);
+               a = ldb_schema_attribute_by_name(ldb, attr);
                valX.data = (void *)cmp;
                valX.length = strlen(cmp);
                valY.data = (void *)val;
                valY.length = strlen(val);
-               ret = h->comparison_fn(ldb, ldb, &valY, &valX);
+               ret = a->syntax->comparison_fn(ldb, ldb, &valY, &valX);
                if (ret >= 0)
                        sqlite3_result_int(ctx, 1);
                else
@@ -639,12 +641,12 @@ static void lsqlite3_compare(sqlite3_context *ctx, int argc,
 
        /* lesser */
        case '<': /* <= */
-               h = ldb_attrib_handler(ldb, attr);
+               a = ldb_schema_attribute_by_name(ldb, attr);
                valX.data = (void *)cmp;
                valX.length = strlen(cmp);
                valY.data = (void *)val;
                valY.length = strlen(val);
-               ret = h->comparison_fn(ldb, ldb, &valY, &valX);
+               ret = a->syntax->comparison_fn(ldb, ldb, &valY, &valX);
                if (ret <= 0)
                        sqlite3_result_int(ctx, 1);
                else
@@ -708,8 +710,8 @@ static int lsqlite3_eid_callback(void *result, int col_num, char **cols, char **
  */
 static int lsqlite3_search_callback(void *result, int col_num, char **cols, char **names)
 {
-       struct ldb_async_handle *handle = talloc_get_type(result, struct ldb_async_handle);
-       struct lsql_async_context *ac = talloc_get_type(handle->private_data, struct lsql_async_context);
+       struct ldb_handle *handle = talloc_get_type(result, struct ldb_handle);
+       struct lsql_context *ac = talloc_get_type(handle->private_data, struct lsql_context);
        struct ldb_message *msg;
        long long eid;
        int i;
@@ -735,7 +737,7 @@ static int lsqlite3_search_callback(void *result, int col_num, char **cols, char
                }
 
                /* start over */
-               ac->ares = talloc_zero(ac, struct ldb_async_result);
+               ac->ares = talloc_zero(ac, struct ldb_reply);
                if (!ac->ares)
                        return SQLITE_ABORT;
 
@@ -750,7 +752,7 @@ static int lsqlite3_search_callback(void *result, int col_num, char **cols, char
        msg = ac->ares->message;
 
        if (msg->dn == NULL) {
-               msg->dn = ldb_dn_explode(msg, cols[1]);
+               msg->dn = ldb_dn_new(msg, ac->module->ldb, cols[1]);
                if (msg->dn == NULL)
                        return SQLITE_ABORT;
        }
@@ -807,7 +809,7 @@ static long long lsqlite3_get_eid_ndn(sqlite3 *sqlite, void *mem_ctx, const char
        return eid;
 }
 
-static long long lsqlite3_get_eid(struct ldb_module *module, const struct ldb_dn *dn)
+static long long lsqlite3_get_eid(struct ldb_module *module, struct ldb_dn *dn)
 {
        TALLOC_CTX *local_ctx;
        struct lsqlite3_private *lsqlite3 = module->private_data;
@@ -825,7 +827,7 @@ static long long lsqlite3_get_eid(struct ldb_module *module, const struct ldb_dn
                return -1;
        }
 
-       cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, dn));
+       cdn = ldb_dn_alloc_casefold(local_ctx, dn);
        if (!cdn) goto done;
 
        eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, cdn);
@@ -839,92 +841,40 @@ done:
  * Interface functions referenced by lsqlite3_ops
  */
 
-static int lsql_search_sync_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
-{
-       struct ldb_result *res = NULL;
-       
-       if (!context) {
-               ldb_set_errstring(ldb, talloc_strdup(ldb, "NULL Context in callback"));
-               goto 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++;
-       } else {
-               ldb_debug(ldb, LDB_DEBUG_ERROR, "unrecognized async reply in ltdb_search_sync_callback!\n");
-               goto error;
-       }
-
-       talloc_free(ares);
-       return LDB_SUCCESS;
-
-error:
-       if (ares) talloc_free(ares);
-       if (res) talloc_free(res);
-       if (context) *((struct ldb_result **)context) = NULL;
-       return LDB_ERR_OPERATIONS_ERROR;
-}
-
 /* search for matching records, by tree */
-int lsql_search_async(struct ldb_module *module, const struct ldb_dn *base,
-                     enum ldb_scope scope, struct ldb_parse_tree *tree,
-                     const char * const *attrs,
-                     void *context,
-                     int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
-                     struct ldb_async_handle **handle)
+int lsql_search(struct ldb_module *module, struct ldb_request *req)
 {
        struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
-       struct lsql_async_context *lsql_ac;
+       struct lsql_context *lsql_ac;
        char *norm_basedn;
        char *sqlfilter;
        char *errmsg;
        char *query = NULL;
         int ret;
 
-       *handle = init_lsql_handle(lsqlite3, module, context, callback);
-       if (*handle == NULL) {
-               talloc_free(*handle);
+       req->handle = init_handle(lsqlite3, module, req);
+       if (req->handle == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
+       lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
 
-       if (base) {
-               norm_basedn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, base));
+       if ((( ! ldb_dn_is_valid(req->op.search.base)) || ldb_dn_is_null(req->op.search.base)) &&
+           (req->op.search.scope == LDB_SCOPE_BASE || req->op.search.scope == LDB_SCOPE_ONELEVEL))
+               return LDB_ERR_OPERATIONS_ERROR;
+
+       if (req->op.search.base) {
+               norm_basedn = ldb_dn_alloc_casefold(lsql_ac, req->op.search.base);
                if (norm_basedn == NULL) {
                        ret = LDB_ERR_INVALID_DN_SYNTAX;
                        goto failed;
                }
        } else norm_basedn = talloc_strdup(lsql_ac, "");
 
-       if (*norm_basedn == '\0' &&
-               (scope == LDB_SCOPE_BASE || scope == LDB_SCOPE_ONELEVEL)) {
-                       ret = LDB_ERR_UNWILLING_TO_PERFORM;
-                       goto failed;
-               }
-
         /* Convert filter into a series of SQL conditions (constraints) */
-       sqlfilter = parsetree_to_sql(module, lsql_ac, tree);
+       sqlfilter = parsetree_to_sql(module, lsql_ac, req->op.search.tree);
         
-        switch(scope) {
+        switch(req->op.search.scope) {
         case LDB_SCOPE_DEFAULT:
         case LDB_SCOPE_SUBTREE:
                if (*norm_basedn != '\0') {
@@ -1034,15 +984,15 @@ int lsql_search_async(struct ldb_module *module, const struct ldb_dn *base,
        / * */
 
        lsql_ac->current_eid = 0;
-       lsql_ac->attrs = attrs;
+       lsql_ac->attrs = req->op.search.attrs;
        lsql_ac->ares = NULL;
 
-       (*handle)->state = LDB_ASYNC_PENDING;
+       req->handle->state = LDB_ASYNC_PENDING;
 
-       ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, *handle, &errmsg);
+       ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, req->handle, &errmsg);
        if (ret != SQLITE_OK) {
                if (errmsg) {
-                       ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
+                       ldb_set_errstring(module->ldb, errmsg);
                        free(errmsg);
                }
                goto failed;
@@ -1054,99 +1004,70 @@ int lsql_search_async(struct ldb_module *module, const struct ldb_dn *base,
                if (lsql_ac->ares->message == NULL)
                        goto failed;
                        
-               (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, lsql_ac->ares);
-               if ((*handle)->status != LDB_SUCCESS)
+               req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, lsql_ac->ares);
+               if (req->handle->status != LDB_SUCCESS)
                        goto failed;
        }
 
-       (*handle)->state = LDB_ASYNC_DONE;
+       req->handle->state = LDB_ASYNC_DONE;
 
        return LDB_SUCCESS;
 
 failed:
-        talloc_free(*handle);
        return LDB_ERR_OPERATIONS_ERROR;
 }
 
-static int lsql_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_result ** res)
-{
-       struct ldb_async_handle *handle;
-       int ret;
-
-       *res = talloc_zero(module, struct ldb_result);
-       if (! *res) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       ret = lsql_search_async(module, base, scope, tree, attrs,
-                               res, &lsql_search_sync_callback,
-                               &handle);
-
-       if (ret == LDB_SUCCESS) {
-               ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
-               talloc_free(handle);
-       }
-
-       if (ret != LDB_SUCCESS) {
-               talloc_free(*res);
-       }
-
-       return ret;
-}
-
 /* add a record */
-static int lsql_add_async(struct ldb_module *module, struct ldb_message *msg,
-                         void *context,
-                         int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
-                         struct ldb_async_handle **handle)
+static int lsql_add(struct ldb_module *module, struct ldb_request *req)
 {
        struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
-       struct lsql_async_context *lsql_ac;
+       struct lsql_context *lsql_ac;
+       struct ldb_message *msg = req->op.add.message;
         long long eid;
        char *dn, *ndn;
        char *errmsg;
        char *query;
        int i;
-       int ret = LDB_ERR_OPERATIONS_ERROR;
+       int ret = LDB_SUCCESS;
 
-       *handle = init_lsql_handle(lsqlite3, module, context, callback);
-       if (*handle == NULL) {
-               goto failed;
+       req->handle = init_handle(lsqlite3, module, req);
+       if (req->handle == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
-       lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
-       (*handle)->state = LDB_ASYNC_DONE;
-       (*handle)->status = LDB_SUCCESS;
+       lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
+       req->handle->state = LDB_ASYNC_DONE;
+       req->handle->status = LDB_SUCCESS;
 
         /* See if this is an ltdb special */
        if (ldb_dn_is_special(msg->dn)) {
                struct ldb_dn *c;
 
-               c = ldb_dn_explode(lsql_ac, "@SUBCLASSES");
-               if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
+               c = ldb_dn_new(lsql_ac, module->ldb, "@SUBCLASSES");
+               if (ldb_dn_compare(msg->dn, c) == 0) {
 #warning "insert subclasses into object class tree"
                        ret = LDB_ERR_UNWILLING_TO_PERFORM;
-                       goto failed;
+                       goto done;
                }
 
 /*
-               c = ldb_dn_explode(local_ctx, "@INDEXLIST");
+               c = ldb_dn_new(local_ctx, module->ldb, "@INDEXLIST");
                if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
 #warning "should we handle indexes somehow ?"
-                       goto failed;
+                       ret = LDB_ERR_UNWILLING_TO_PERFORM;
+                       goto done;
                }
 */
-                /* Others are implicitly ignored */
-                return LDB_SUCCESS;
+                /* Others return an error */
+               ret = LDB_ERR_UNWILLING_TO_PERFORM;
+               goto done;
        }
 
        /* create linearized and normalized dns */
-       dn = ldb_dn_linearize(lsql_ac, msg->dn);
-       ndn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, msg->dn));
+       dn = ldb_dn_alloc_linearized(lsql_ac, msg->dn);
+       ndn = ldb_dn_alloc_casefold(lsql_ac, msg->dn);
        if (dn == NULL || ndn == NULL) {
                ret = LDB_ERR_OTHER;
-               goto failed;
+               goto done;
        }
 
        query = lsqlite3_tprintf(lsql_ac,
@@ -1157,28 +1078,28 @@ static int lsql_add_async(struct ldb_module *module, struct ldb_message *msg,
                                dn, ndn);
        if (query == NULL) {
                ret = LDB_ERR_OTHER;
-               goto failed;
+               goto done;
        }
 
        ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
        if (ret != SQLITE_OK) {
                if (errmsg) {
-                       ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
+                       ldb_set_errstring(module->ldb, errmsg);
                        free(errmsg);
                }
                ret = LDB_ERR_OTHER;
-               goto failed;
+               goto done;
        }
 
        eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, lsql_ac, ndn);
        if (eid == -1) {
                ret = LDB_ERR_OTHER;
-               goto failed;
+               goto done;
        }
 
        for (i = 0; i < msg->num_elements; i++) {
                const struct ldb_message_element *el = &msg->elements[i];
-               const struct ldb_attrib_handler *h;
+               const struct ldb_schema_attribute *a;
                char *attr;
                int j;
 
@@ -1186,10 +1107,10 @@ static int lsql_add_async(struct ldb_module *module, struct ldb_message *msg,
                attr = ldb_attr_casefold(lsql_ac, el->name);
                if (attr == NULL) {
                        ret = LDB_ERR_OTHER;
-                       goto failed;
+                       goto done;
                }
 
-               h = ldb_attrib_handler(module->ldb, el->name);
+               a = ldb_schema_attribute_by_name(module->ldb, el->name);
 
                /* For each value of the specified attribute name... */
                for (j = 0; j < el->num_values; j++) {
@@ -1197,10 +1118,10 @@ static int lsql_add_async(struct ldb_module *module, struct ldb_message *msg,
                        char *insert;
 
                        /* Get a canonicalised copy of the data */
-                       h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
+                       a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
                        if (value.data == NULL) {
                                ret = LDB_ERR_OTHER;
-                               goto failed;
+                               goto done;
                        }
 
                        insert = lsqlite3_tprintf(lsql_ac,
@@ -1212,93 +1133,74 @@ static int lsql_add_async(struct ldb_module *module, struct ldb_message *msg,
                                        el->values[j].data, value.data);
                        if (insert == NULL) {
                                ret = LDB_ERR_OTHER;
-                               goto failed;
+                               goto done;
                        }
 
                        ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg);
                        if (ret != SQLITE_OK) {
                                if (errmsg) {
-                                       ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
+                                       ldb_set_errstring(module->ldb, errmsg);
                                        free(errmsg);
                                }
                                ret = LDB_ERR_OTHER;
-                               goto failed;
+                               goto done;
                        }
                }
        }
 
-       if (lsql_ac->callback)
-               (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
+       if (lsql_ac->callback) {
+               req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
+       }
        
-        return LDB_SUCCESS;
-
-failed:
-       talloc_free(*handle);
-       return ret;
-}
-
-static int lsql_add(struct ldb_module *module, const struct ldb_message *msg)
-{
-       struct ldb_async_handle *handle;
-       int ret;
-
-       ret = lsql_add_async(module, msg, NULL, NULL, &handle);
-
-       if (ret != LDB_SUCCESS)
-               return ret;
-
-       ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
-
-       talloc_free(handle);
+done:
+       req->handle->state = LDB_ASYNC_DONE;
        return ret;
 }
 
-
 /* modify a record */
-static int lsql_modify_async(struct ldb_module *module, const struct ldb_message *msg,
-                            void *context,
-                            int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
-                            struct ldb_async_handle **handle)
+static int lsql_modify(struct ldb_module *module, struct ldb_request *req)
 {
        struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
-       struct lsql_async_context *lsql_ac;
+       struct lsql_context *lsql_ac;
+       struct ldb_message *msg = req->op.mod.message;
         long long eid;
        char *errmsg;
        int i;
-       int ret = LDB_ERR_OPERATIONS_ERROR;
+       int ret = LDB_SUCCESS;
 
-       *handle = init_lsql_handle(lsqlite3, module, context, callback);
-       if (*handle == NULL) {
-               goto failed;
+       req->handle = init_handle(lsqlite3, module, req);
+       if (req->handle == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
-       lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
-       (*handle)->state = LDB_ASYNC_DONE;
-       (*handle)->status = LDB_SUCCESS;
+       lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
+       req->handle->state = LDB_ASYNC_DONE;
+       req->handle->status = LDB_SUCCESS;
 
         /* See if this is an ltdb special */
        if (ldb_dn_is_special(msg->dn)) {
                struct ldb_dn *c;
 
-               c = ldb_dn_explode(lsql_ac, "@SUBCLASSES");
-               if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
+               c = ldb_dn_new(lsql_ac, module->ldb, "@SUBCLASSES");
+               if (ldb_dn_compare(msg->dn, c) == 0) {
 #warning "modify subclasses into object class tree"
                        ret = LDB_ERR_UNWILLING_TO_PERFORM;
-                       goto failed;
+                       goto done;
                }
 
-                /* Others are implicitly ignored */
-                return LDB_SUCCESS;
+                /* Others return an error */
+               ret = LDB_ERR_UNWILLING_TO_PERFORM;
+                goto done;
        }
 
        eid = lsqlite3_get_eid(module, msg->dn);
        if (eid == -1) {
                ret = LDB_ERR_OTHER;
-               goto failed;
+               goto done;
        }
 
        for (i = 0; i < msg->num_elements; i++) {
                const struct ldb_message_element *el = &msg->elements[i];
-               const struct ldb_attrib_handler *h;
+               const struct ldb_schema_attribute *a;
                int flags = el->flags & LDB_FLAG_MOD_MASK;
                char *attr;
                char *mod;
@@ -1308,10 +1210,10 @@ static int lsql_modify_async(struct ldb_module *module, const struct ldb_message
                attr = ldb_attr_casefold(lsql_ac, el->name);
                if (attr == NULL) {
                        ret = LDB_ERR_OTHER;
-                       goto failed;
+                       goto done;
                }
 
-               h = ldb_attrib_handler(module->ldb, el->name);
+               a = ldb_schema_attribute_by_name(module->ldb, el->name);
 
                switch (flags) {
 
@@ -1325,17 +1227,17 @@ static int lsql_modify_async(struct ldb_module *module, const struct ldb_message
                                                eid, attr);
                        if (mod == NULL) {
                                ret = LDB_ERR_OTHER;
-                               goto failed;
+                               goto done;
                        }
 
                        ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
                        if (ret != SQLITE_OK) {
                                if (errmsg) {
-                                       ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
+                                       ldb_set_errstring(module->ldb, errmsg);
                                        free(errmsg);
                                }
                                ret = LDB_ERR_OTHER;
-                               goto failed;
+                               goto done;
                         }
 
                        /* MISSING break is INTENTIONAL */
@@ -1347,10 +1249,10 @@ static int lsql_modify_async(struct ldb_module *module, const struct ldb_message
                                struct ldb_val value;
 
                                /* Get a canonicalised copy of the data */
-                               h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
+                               a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
                                if (value.data == NULL) {
                                        ret = LDB_ERR_OTHER;
-                                       goto failed;
+                                       goto done;
                                }
 
                                mod = lsqlite3_tprintf(lsql_ac,
@@ -1363,17 +1265,17 @@ static int lsql_modify_async(struct ldb_module *module, const struct ldb_message
 
                                if (mod == NULL) {
                                        ret = LDB_ERR_OTHER;
-                                       goto failed;
+                                       goto done;
                                }
 
                                ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
                                if (ret != SQLITE_OK) {
                                        if (errmsg) {
-                                               ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
+                                               ldb_set_errstring(module->ldb, errmsg);
                                                free(errmsg);
                                        }
                                        ret = LDB_ERR_OTHER;
-                                       goto failed;
+                                       goto done;
                                }
                        }
 
@@ -1389,17 +1291,17 @@ static int lsql_modify_async(struct ldb_module *module, const struct ldb_message
                                                        eid, attr);
                                if (mod == NULL) {
                                        ret = LDB_ERR_OTHER;
-                                       goto failed;
+                                       goto done;
                                }
 
                                ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
                                if (ret != SQLITE_OK) {
                                        if (errmsg) {
-                                               ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
+                                               ldb_set_errstring(module->ldb, errmsg);
                                                free(errmsg);
                                        }
                                        ret = LDB_ERR_OTHER;
-                                       goto failed;
+                                       goto done;
                                }
                        }
 
@@ -1408,10 +1310,10 @@ static int lsql_modify_async(struct ldb_module *module, const struct ldb_message
                                struct ldb_val value;
 
                                /* Get a canonicalised copy of the data */
-                               h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
+                               a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
                                if (value.data == NULL) {
                                        ret = LDB_ERR_OTHER;
-                                       goto failed;
+                                       goto done;
                                }
 
                                mod = lsqlite3_tprintf(lsql_ac,
@@ -1423,17 +1325,17 @@ static int lsql_modify_async(struct ldb_module *module, const struct ldb_message
 
                                if (mod == NULL) {
                                        ret = LDB_ERR_OTHER;
-                                       goto failed;
+                                       goto done;
                                }
 
                                ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
                                if (ret != SQLITE_OK) {
                                        if (errmsg) {
-                                               ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
+                                               ldb_set_errstring(module->ldb, errmsg);
                                                free(errmsg);
                                        }
                                        ret = LDB_ERR_OTHER;
-                                       goto failed;
+                                       goto done;
                                }
                        }
 
@@ -1441,57 +1343,37 @@ static int lsql_modify_async(struct ldb_module *module, const struct ldb_message
                }
        }
 
-       if (lsql_ac->callback)
-               (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
+       if (lsql_ac->callback) {
+               req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
+       }
        
-        return LDB_SUCCESS;
-
-failed:
-       talloc_free(*handle);
-       return ret;
-}
-
-static int lsql_modify(struct ldb_module *module, const struct ldb_message *msg)
-{
-       struct ldb_async_handle *handle;
-       int ret;
-
-       ret = lsql_modify_async(module, msg, NULL, NULL, &handle);
-
-       if (ret != LDB_SUCCESS)
-               return ret;
-
-       ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
-
-       talloc_free(handle);
+done:
+       req->handle->state = LDB_ASYNC_DONE;
        return ret;
 }
 
 /* delete a record */
-static int lsql_delete_async(struct ldb_module *module, const struct ldb_dn *dn,
-                            void *context,
-                            int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
-                            struct ldb_async_handle **handle)
+static int lsql_delete(struct ldb_module *module, struct ldb_request *req)
 {
        struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
-       struct lsql_async_context *lsql_ac;
+       struct lsql_context *lsql_ac;
         long long eid;
        char *errmsg;
        char *query;
-       int ret = LDB_ERR_OPERATIONS_ERROR;
+       int ret = LDB_SUCCESS;
 
 
-       *handle = init_lsql_handle(lsqlite3, module, context, callback);
-       if (*handle == NULL) {
-               goto failed;
+       req->handle = init_handle(lsqlite3, module, req);
+       if (req->handle == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
-       lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
-       (*handle)->state = LDB_ASYNC_DONE;
-       (*handle)->status = LDB_SUCCESS;
+       lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
+       req->handle->state = LDB_ASYNC_DONE;
+       req->handle->status = LDB_SUCCESS;
 
-       eid = lsqlite3_get_eid(module, dn);
+       eid = lsqlite3_get_eid(module, req->op.del.dn);
        if (eid == -1) {
-               goto failed;
+               goto done;
        }
 
        query = lsqlite3_tprintf(lsql_ac,
@@ -1502,77 +1384,52 @@ static int lsql_delete_async(struct ldb_module *module, const struct ldb_dn *dn,
                                eid, eid);
        if (query == NULL) {
                ret = LDB_ERR_OTHER;
-               goto failed;
+               goto done;
        }
 
        ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
        if (ret != SQLITE_OK) {
                if (errmsg) {
-                       ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
+                       ldb_set_errstring(module->ldb, errmsg);
                        free(errmsg);
                }
-               ret = LDB_ERR_OPERATIONS_ERROR;
-               goto failed;
+               req->handle->status = LDB_ERR_OPERATIONS_ERROR;
+               goto done;
        }
 
-       if (lsql_ac->callback)
-               (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
-       
-        return LDB_SUCCESS;
-
-failed:
-       talloc_free(*handle);
-       return ret;
-}
-
-static int lsql_delete(struct ldb_module *module, const struct ldb_dn *dn)
-{
-       struct ldb_async_handle *handle;
-       int ret;
-
-       /* ignore ltdb specials */
-       if (ldb_dn_is_special(dn)) {
-               return LDB_SUCCESS;
+       if (lsql_ac->callback) {
+               ret = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
        }
-
-       ret = lsql_delete_async(module, dn, NULL, NULL, &handle);
-
-       if (ret != LDB_SUCCESS)
-               return ret;
-
-       ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
-
-       talloc_free(handle);
+       
+done:
+       req->handle->state = LDB_ASYNC_DONE;
        return ret;
 }
 
 /* rename a record */
-static int lsql_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 *),
-                            struct ldb_async_handle **handle)
+static int lsql_rename(struct ldb_module *module, struct ldb_request *req)
 {
        struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
-       struct lsql_async_context *lsql_ac;
+       struct lsql_context *lsql_ac;
        char *new_dn, *new_cdn, *old_cdn;
        char *errmsg;
        char *query;
-       int ret = LDB_ERR_OPERATIONS_ERROR;
+       int ret = LDB_SUCCESS;
 
-       *handle = init_lsql_handle(lsqlite3, module, context, callback);
-       if (*handle == NULL) {
-               goto failed;
+       req->handle = init_handle(lsqlite3, module, req);
+       if (req->handle == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
-       lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
-       (*handle)->state = LDB_ASYNC_DONE;
-       (*handle)->status = LDB_SUCCESS;
+       lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
+       req->handle->state = LDB_ASYNC_DONE;
+       req->handle->status = LDB_SUCCESS;
 
        /* create linearized and normalized dns */
-       old_cdn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, olddn));
-       new_cdn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, newdn));
-       new_dn = ldb_dn_linearize(lsql_ac, newdn);
+       old_cdn = ldb_dn_alloc_casefold(lsql_ac, req->op.rename.olddn);
+       new_cdn = ldb_dn_alloc_casefold(lsql_ac, req->op.rename.newdn);
+       new_dn = ldb_dn_alloc_linearized(lsql_ac, req->op.rename.newdn);
        if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) {
-               goto failed;
+               goto done;
        }
 
        /* build the SQL query */
@@ -1581,49 +1438,26 @@ static int lsql_rename_async(struct ldb_module *module, const struct ldb_dn *old
                                 "WHERE norm_dn = '%q';",
                                 new_dn, new_cdn, old_cdn);
        if (query == NULL) {
-               goto failed;
+               goto done;
        }
 
        /* execute */
        ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
        if (ret != SQLITE_OK) {
                if (errmsg) {
-                       ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
+                       ldb_set_errstring(module->ldb, errmsg);
                        free(errmsg);
                }
                ret = LDB_ERR_OPERATIONS_ERROR;
-               goto failed;
+               goto done;
        }
 
-       if (lsql_ac->callback)
-               (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
-       
-        return LDB_SUCCESS;
-
-failed:
-       talloc_free(*handle);
-       return ret;
-}
-
-static int lsql_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
-{
-       struct ldb_async_handle *handle;
-       int ret;
-
-       /* ignore ltdb specials */
-       if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
-               return LDB_SUCCESS;
+       if (lsql_ac->callback) {
+               ret = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
        }
 
-
-       ret = lsql_rename_async(module, olddn, newdn, NULL, NULL, &handle);
-
-       if (ret != LDB_SUCCESS)
-               return ret;
-
-       ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
-
-       talloc_free(handle);
+done:
+       req->handle->state = LDB_ASYNC_DONE;
        return ret;
 }
 
@@ -1688,6 +1522,41 @@ static int lsql_del_trans(struct ldb_module *module)
        return -1;
 }
 
+static int destructor(struct lsqlite3_private *lsqlite3)
+{        
+       if (lsqlite3->sqlite) {
+               sqlite3_close(lsqlite3->sqlite);
+       }
+       return 0;
+}
+
+static int lsql_request(struct ldb_module *module, struct ldb_request *req)
+{
+       return LDB_ERR_OPERATIONS_ERROR;
+}
+
+static int lsql_wait(struct ldb_handle *handle, enum ldb_wait_type type)
+{
+       return handle->status;
+}
+
+/*
+ * Table of operations for the sqlite3 backend
+ */
+static const struct ldb_module_ops lsqlite3_ops = {
+       .name              = "sqlite",
+       .search            = lsql_search,
+       .add               = lsql_add,
+        .modify            = lsql_modify,
+        .del               = lsql_delete,
+        .rename            = lsql_rename,
+       .request           = lsql_request,
+       .start_transaction = lsql_start_trans,
+       .end_transaction   = lsql_end_trans,
+       .del_transaction   = lsql_del_trans,
+       .wait              = lsql_wait,
+};
+
 /*
  * Static functions
  */
@@ -1797,12 +1666,12 @@ static int initialize(struct lsqlite3_private *lsqlite3,
                 "    ('TOP', '0001');");
         
         /* Skip protocol indicator of url  */
-        if (strncmp(url, "sqlite://", 9) != 0) {
+        if (strncmp(url, "sqlite3://", 10) != 0) {
                 return SQLITE_MISUSE;
         }
         
         /* Update pointer to just after the protocol indicator */
-        url += 9;
+        url += 10;
         
         /* Try to open the (possibly empty/non-existent) database */
         if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
@@ -1960,122 +1829,14 @@ failed:
        return -1;
 }
 
-static int
-destructor(void *p)
-{
-       struct lsqlite3_private *lsqlite3 = p;
-        
-       if (lsqlite3->sqlite) {
-               sqlite3_close(lsqlite3->sqlite);
-       }
-       return 0;
-}
-
-static int lsql_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
-{
-       return handle->status;
-}
-
-static int lsql_request(struct ldb_module *module, struct ldb_request *req)
-{
-       /* check for oustanding critical controls and return an error if found */
-
-       if (req->controls != NULL) {
-               ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls should not reach the ldb_sqlite3 backend!\n");
-       }
-
-       if (check_critical_controls(req->controls)) {
-               return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
-       }
-       
-       switch (req->operation) {
-
-       case LDB_REQ_SEARCH:
-               return lsql_search_bytree(module,
-                                         req->op.search.base,
-                                         req->op.search.scope, 
-                                         req->op.search.tree, 
-                                         req->op.search.attrs, 
-                                         &req->op.search.res);
-
-       case LDB_REQ_ADD:
-               return lsql_add(module, req->op.add.message);
-
-       case LDB_REQ_MODIFY:
-               return lsql_modify(module, req->op.mod.message);
-
-       case LDB_REQ_DELETE:
-               return lsql_delete(module, req->op.del.dn);
-
-       case LDB_REQ_RENAME:
-               return lsql_rename(module,
-                                       req->op.rename.olddn,
-                                       req->op.rename.newdn);
-
-       case LDB_ASYNC_SEARCH:
-               return lsql_search_async(module,
-                                       req->op.search.base,
-                                       req->op.search.scope, 
-                                       req->op.search.tree, 
-                                       req->op.search.attrs,
-                                       req->async.context,
-                                       req->async.callback,
-                                       &req->async.handle);
-/*
-       case LDB_ASYNC_ADD:
-               return lsql_add_async(module,
-                                       req->op.add.message,
-                                       req->async.context,
-                                       req->async.callback,
-                                       &req->async.handle);
-
-       case LDB_ASYNC_MODIFY:
-               return lsql_modify_async(module,
-                                       req->op.mod.message,
-                                       req->async.context,
-                                       req->async.callback,
-                                       &req->async.handle);
-*/
-       case LDB_ASYNC_DELETE:
-               return lsql_delete_async(module,
-                                       req->op.del.dn,
-                                       req->async.context,
-                                       req->async.callback,
-                                       &req->async.handle);
-
-       case LDB_ASYNC_RENAME:
-               return lsql_rename_async(module,
-                                       req->op.rename.olddn,
-                                       req->op.rename.newdn,
-                                       req->async.context,
-                                       req->async.callback,
-                                       &req->async.handle);
-
-       default:
-               return LDB_ERR_OPERATIONS_ERROR;
-
-       }
-}
-
-/*
- * Table of operations for the sqlite3 backend
- */
-static const struct ldb_module_ops lsqlite3_ops = {
-       .name              = "sqlite",
-       .request           = lsql_request,
-       .start_transaction = lsql_start_trans,
-       .end_transaction   = lsql_end_trans,
-       .del_transaction   = lsql_del_trans,
-       .async_wait        = lsql_async_wait,
-};
-
 /*
  * connect to the database
  */
 static int lsqlite3_connect(struct ldb_context *ldb,
-                    const char *url, 
-                    unsigned int flags, 
-                    const char *options[])
+                           const char *url, 
+                           unsigned int flags, 
+                           const char *options[],
+                           struct ldb_module **module)
 {
        int                         i;
         int                         ret;
@@ -2097,15 +1858,19 @@ static int lsqlite3_connect(struct ldb_context *ldb,
         
        talloc_set_destructor(lsqlite3, destructor);
         
-       ldb->modules = talloc(ldb, struct ldb_module);
-       if (!ldb->modules) {
+
+
+       *module = talloc(ldb, struct ldb_module);
+       if (!module) {
+               ldb_oom(ldb);
                goto failed;
        }
-       ldb->modules->ldb = ldb;
-       ldb->modules->prev = ldb->modules->next = NULL;
-       ldb->modules->private_data = lsqlite3;
-       ldb->modules->ops = &lsqlite3_ops;
-        
+       talloc_set_name_const(*module, "ldb_sqlite3 backend");
+       (*module)->ldb = ldb;
+       (*module)->prev = (*module)->next = NULL;
+       (*module)->private_data = lsqlite3;
+       (*module)->ops = &lsqlite3_ops;
+
        if (options) {
                /*
                  * take a copy of the options array, so we don't have to rely