Fix the mess with ldb includes.
[metze/samba/wip.git] / source4 / lib / ldb / ldb_sqlite3 / ldb_sqlite3.c
index bf6cf0951b3f84d019dbbb50e300b81fa9b76587..39a77a5505797b374e2106edc083a0748970e225 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
@@ -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 <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -33,8 +32,7 @@
  *  Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
  */
 
-#include "includes.h"
-#include "ldb/include/includes.h"
+#include "ldb_module.h"
 
 #include <sqlite3.h>
 
@@ -44,48 +42,16 @@ struct lsqlite3_private {
         sqlite3 *sqlite;
 };
 
-struct lsql_async_context {
+struct lsql_context {
        struct ldb_module *module;
+       struct ldb_request *req;
 
        /* search stuff */
        long long current_eid;
        const char * const * attrs;
-       struct ldb_async_result *ares;
-
-       /* async stuff */
-       void *context;
-       int (*callback)(struct ldb_context *, void *, struct ldb_async_result *);
+       struct ldb_reply *ares;
 };
 
-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 *))
-{
-       struct lsql_async_context *ac;
-       struct ldb_async_handle *h;
-
-       h = talloc_zero(lsqlite3, struct ldb_async_handle);
-       if (h == NULL) {
-               ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
-               return NULL;
-       }
-
-       ac = talloc(h, struct lsql_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;
-
-       return h;
-}
-
 /*
  * Macros used throughout
  */
@@ -278,7 +244,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;
@@ -338,34 +304,18 @@ 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;
                }
 
-               if (strcasecmp(t->u.equality.attr, "objectclass") == 0) {
-               /*
-                * For object classes, we want to search for all objectclasses
-                * that are subclasses as well.
-               */
-                       return lsqlite3_tprintf(mem_ctx,
-                                       "SELECT eid  FROM ldb_attribute_values\n"
-                                       "WHERE norm_attr_name = 'OBJECTCLASS' "
-                                       "AND norm_attr_value IN\n"
-                                       "  (SELECT class_name FROM ldb_object_classes\n"
-                                       "   WHERE tree_key GLOB\n"
-                                       "     (SELECT tree_key FROM ldb_object_classes\n"
-                                       "      WHERE class_name = '%q'\n"
-                                       "     ) || '*'\n"
-                                       "  )\n", value.data);
-
-               } else if (strcasecmp(t->u.equality.attr, "dn") == 0) {
+               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,
@@ -402,13 +352,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;
                }
@@ -423,10 +373,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;
                }
@@ -442,10 +392,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;
                }
@@ -474,10 +424,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;
                }
@@ -615,7 +565,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;
@@ -623,12 +573,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
@@ -637,12 +587,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
@@ -706,8 +656,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;
@@ -733,7 +683,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;
 
@@ -748,7 +698,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;
        }
@@ -805,7 +755,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;
@@ -823,7 +773,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);
@@ -837,92 +787,35 @@ 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);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
+       lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
 
-       lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
+       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 (base) {
-               norm_basedn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, base));
+       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') {
@@ -1032,15 +925,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;
@@ -1052,99 +945,58 @@ 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;
-       }
-       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) {
-#warning "insert subclasses into object class tree"
-                       ret = LDB_ERR_UNWILLING_TO_PERFORM;
-                       goto failed;
-               }
-
 /*
-               c = ldb_dn_explode(local_ctx, "@INDEXLIST");
+               struct ldb_dn *c;
+               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,
@@ -1155,28 +1007,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;
 
@@ -1184,10 +1036,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++) {
@@ -1195,10 +1047,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,
@@ -1210,93 +1062,61 @@ 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;
-       }
-       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) {
-#warning "modify subclasses into object class tree"
-                       ret = LDB_ERR_UNWILLING_TO_PERFORM;
-                       goto failed;
-               }
-
-                /* 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;
@@ -1306,10 +1126,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) {
 
@@ -1323,17 +1143,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 */
@@ -1345,10 +1165,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,
@@ -1361,17 +1181,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;
                                }
                        }
 
@@ -1387,17 +1207,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;
                                }
                        }
 
@@ -1406,10 +1226,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,
@@ -1421,17 +1241,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;
                                }
                        }
 
@@ -1439,57 +1259,33 @@ 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;
-       }
-       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,
@@ -1500,77 +1296,48 @@ 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;
-       }
-       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 */
@@ -1579,49 +1346,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;
 }
 
@@ -1686,6 +1430,93 @@ 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_run_request(struct ldb_module *module, struct ldb_request *req)
+{
+       switch (req->operation) {
+       case LDB_SEARCH:
+               return lsql_search(module, req);
+               break;
+       case LDB_ADD:
+               return lsql_add(module, req);
+               break;
+       case LDB_MODIFY:
+               return lsql_modify(module, req);
+               break;
+       case LDB_DELETE:
+               return lsql_delete(module, req);
+               break;
+       case LDB_RENAME:
+               return lsql_rename(module, req);
+               break;
+/* TODO:
+       case LDB_SEQUENCE_NUMBER:
+               return lsql_sequence_number(module, req);
+               break;
+ */
+       default:
+               return lsql_request(module, req);
+               break;
+       }
+
+       return LDB_ERR_OPERATIONS_ERROR;
+}
+
+static int lsql_handle_request(struct ldb_module *module, struct ldb_request *req)
+{
+       struct lsql_context *ac;
+
+       if (check_critical_controls(req->controls)) {
+               return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
+       }
+
+       ac = talloc_zero(req, struct lsql_context);
+       if (ac == NULL) {
+               ldb_set_errstring(module->ldb, "Out of Memory");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       ac->module = module;
+       ac->req = req;
+
+       req->handle = ldb_handle_new(req, lsql_run_request, ac);
+       if (req->handle == NULL) {
+               talloc_free(ac);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       return LDB_SUCCESS;
+}
+
+/*
+ * Table of operations for the sqlite3 backend
+ */
+static const struct ldb_module_ops lsqlite3_ops = {
+       .name              = "sqlite",
+       .search            = lsql_handle_request,
+       .add               = lsql_handle_request,
+        .modify            = lsql_handle_request,
+        .del               = lsql_handle_request,
+        .rename            = lsql_handle_request,
+       .request           = lsql_handle_request,
+       .start_transaction = lsql_start_trans,
+       .end_transaction   = lsql_end_trans,
+       .del_transaction   = lsql_del_trans,
+       /* TODO: .sequence_number   = lsql_handle_request */
+};
+
 /*
  * Static functions
  */
@@ -1795,12 +1626,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) {
@@ -1958,122 +1789,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_module *module, 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
  */
-int lsqlite3_connect(struct ldb_context *ldb,
-                    const char *url, 
-                    unsigned int flags, 
-                    const char *options[])
+static int lsqlite3_connect(struct ldb_context *ldb,
+                           const char *url, 
+                           unsigned int flags, 
+                           const char *options[],
+                           struct ldb_module **module)
 {
        int                         i;
         int                         ret;
@@ -2095,15 +1818,19 @@ 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
@@ -2137,3 +1864,7 @@ failed:
        return -1;
 }
 
+const struct ldb_backend_ops ldb_sqlite3_backend_ops = {
+       .name = "sqlite3",
+       .connect_fn = lsqlite3_connect
+};