r25000: Fix some more C++ compatibility warnings.
[abartlet/samba.git/.git] / source4 / lib / ldb / ldb_tdb / ldb_search.c
index d97444a6e87426b2380ef9ef83c44e452bea965f..a195a39391b55b2db2a37c4807e50860839630cf 100644 (file)
@@ -10,7 +10,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
@@ -18,8 +18,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/>.
 */
 
 /*
  *  Author: Andrew Tridgell
  */
 
-#include "includes.h"
-#include "ldb/ldb_tdb/ldb_tdb.h"
-#include "ldb/include/ldb_parse.h"
-
-/*
-  free a message that has all parts separately allocated
-*/
-static void msg_free_all_parts(struct ldb_context *ldb, struct ldb_message *msg)
-{
-       int i, j;
-       ldb_free(ldb, msg->dn);
-       for (i=0;i<msg->num_elements;i++) {
-               ldb_free(ldb, msg->elements[i].name);
-               for (j=0;j<msg->elements[i].num_values;j++) {
-                       ldb_free(ldb, msg->elements[i].values[j].data);
-               }
-               ldb_free(ldb, msg->elements[i].values);
-       }
-       ldb_free(ldb, msg->elements);
-       ldb_free(ldb, msg);
-}
-
-
-/*
-  duplicate a ldb_val structure
-*/
-struct ldb_val ldb_val_dup(struct ldb_context *ldb,
-                          const struct ldb_val *v)
-{
-       struct ldb_val v2;
-       v2.length = v->length;
-       if (v->length == 0) {
-               v2.data = NULL;
-               return v2;
-       }
-
-       /* the +1 is to cope with buggy C library routines like strndup
-          that look one byte beyond */
-       v2.data = ldb_malloc(ldb, v->length+1);
-       if (!v2.data) {
-               v2.length = 0;
-               return v2;
-       }
-
-       memcpy(v2.data, v->data, v->length);
-       ((char *)v2.data)[v->length] = 0;
-       return v2;
-}
-
+#include "ldb_includes.h"
 
+#include "ldb_tdb.h"
 
 /*
   add one element to a message
 */
-static int msg_add_element(struct ldb_context *ldb, 
-                          struct ldb_message *ret, const struct ldb_message_element *el)
+static int msg_add_element(struct ldb_message *ret, 
+                          const struct ldb_message_element *el,
+                          int check_duplicates)
 {
-       int i;
+       unsigned int i;
        struct ldb_message_element *e2, *elnew;
 
-       e2 = ldb_realloc_p(ldb, ret->elements, struct ldb_message_element, ret->num_elements+1);
+       if (check_duplicates && ldb_msg_find_element(ret, el->name)) {
+               /* its already there */
+               return 0;
+       }
+
+       e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
        if (!e2) {
                return -1;
        }
@@ -100,13 +58,13 @@ static int msg_add_element(struct ldb_context *ldb,
        
        elnew = &e2[ret->num_elements];
 
-       elnew->name = ldb_strdup(ldb, el->name);
+       elnew->name = talloc_strdup(ret->elements, el->name);
        if (!elnew->name) {
                return -1;
        }
 
        if (el->num_values) {
-               elnew->values = ldb_malloc_array_p(ldb, struct ldb_val, el->num_values);
+               elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
                if (!elnew->values) {
                        return -1;
                }
@@ -115,7 +73,7 @@ static int msg_add_element(struct ldb_context *ldb,
        }
 
        for (i=0;i<el->num_values;i++) {
-               elnew->values[i] = ldb_val_dup(ldb, &el->values[i]);
+               elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
                if (elnew->values[i].length != el->values[i].length) {
                        return -1;
                }
@@ -128,19 +86,48 @@ static int msg_add_element(struct ldb_context *ldb,
        return 0;
 }
 
+/*
+  add the special distinguishedName element
+*/
+static int msg_add_distinguished_name(struct ldb_message *msg)
+{
+       struct ldb_message_element el;
+       struct ldb_val val;
+       int ret;
+
+       el.flags = 0;
+       el.name = "distinguishedName";
+       el.num_values = 1;
+       el.values = &val;
+       val.data = (uint8_t *)ldb_dn_alloc_linearized(msg, msg->dn);
+       val.length = strlen((char *)val.data);
+       
+       ret = msg_add_element(msg, &el, 1);
+       return ret;
+}
+
 /*
   add all elements from one message into another
  */
-static int msg_add_all_elements(struct ldb_context *ldb, struct ldb_message *ret,
+static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *ret,
                                const struct ldb_message *msg)
 {
-       int i;
+       struct ldb_context *ldb = module->ldb;
+       unsigned int i;
+       int check_duplicates = (ret->num_elements != 0);
+
+       if (msg_add_distinguished_name(ret) != 0) {
+               return -1;
+       }
+
        for (i=0;i<msg->num_elements;i++) {
-               int flags = ltdb_attribute_flags(ldb, msg->elements[i].name);
-               if ((msg->dn[0] != '@') && (flags & LTDB_FLAG_HIDDEN)) {
+               const struct ldb_schema_attribute *a;
+               a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
+               if (a->flags & LDB_ATTR_FLAG_HIDDEN) {
                        continue;
                }
-               if (msg_add_element(ldb, ret, &msg->elements[i]) != 0) {
+               if (msg_add_element(ret, &msg->elements[i],
+                                   check_duplicates) != 0) {
                        return -1;
                }
        }
@@ -152,31 +139,31 @@ static int msg_add_all_elements(struct ldb_context *ldb, struct ldb_message *ret
 /*
   pull the specified list of attributes from a message
  */
-static struct ldb_message *ltdb_pull_attrs(struct ldb_context *ldb, 
+static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module, 
+                                          TALLOC_CTX *mem_ctx, 
                                           const struct ldb_message *msg, 
                                           const char * const *attrs)
 {
        struct ldb_message *ret;
        int i;
 
-       ret = ldb_malloc_p(ldb, struct ldb_message);
+       ret = talloc(mem_ctx, struct ldb_message);
        if (!ret) {
                return NULL;
        }
 
-       ret->dn = ldb_strdup(ldb, msg->dn);
+       ret->dn = ldb_dn_copy(ret, msg->dn);
        if (!ret->dn) {
-               ldb_free(ldb, ret);
+               talloc_free(ret);
                return NULL;
        }
 
        ret->num_elements = 0;
        ret->elements = NULL;
-       ret->private_data = NULL;
 
        if (!attrs) {
-               if (msg_add_all_elements(ldb, ret, msg) != 0) {
-                       msg_free_all_parts(ldb, ret);
+               if (msg_add_all_elements(module, ret, msg) != 0) {
+                       talloc_free(ret);
                        return NULL;
                }
                return ret;
@@ -186,35 +173,17 @@ static struct ldb_message *ltdb_pull_attrs(struct ldb_context *ldb,
                struct ldb_message_element *el;
 
                if (strcmp(attrs[i], "*") == 0) {
-                       if (msg_add_all_elements(ldb, ret, msg) != 0) {
-                               msg_free_all_parts(ldb, ret);
+                       if (msg_add_all_elements(module, ret, msg) != 0) {
+                               talloc_free(ret);
                                return NULL;
                        }
                        continue;
                }
 
-               if (ldb_attr_cmp(attrs[i], "dn") == 0) {
-                       struct ldb_message_element el2;
-                       struct ldb_val val;
-
-                       el2.flags = 0;
-                       el2.name = ldb_strdup(ldb, "dn");
-                       if (!el2.name) {
-                               msg_free_all_parts(ldb, ret);
-                               ldb_free(ldb, el2.name);
-                               return NULL;                            
-                       }
-                       el2.num_values = 1;
-                       el2.values = &val;
-                       val.data = ret->dn;
-                       val.length = strlen(ret->dn);
-
-                       if (msg_add_element(ldb, ret, &el2) != 0) {
-                               msg_free_all_parts(ldb, ret);
-                               ldb_free(ldb, el2.name);
-                               return NULL;                            
+               if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
+                       if (msg_add_distinguished_name(ret) != 0) {
+                               return NULL;
                        }
-                       ldb_free(ldb, el2.name);
                        continue;
                }
 
@@ -222,8 +191,8 @@ static struct ldb_message *ltdb_pull_attrs(struct ldb_context *ldb,
                if (!el) {
                        continue;
                }
-               if (msg_add_element(ldb, ret, el) != 0) {
-                       msg_free_all_parts(ldb, ret);
+               if (msg_add_element(ret, el, 1) != 0) {
+                       talloc_free(ret);
                        return NULL;                            
                }
        }
@@ -232,146 +201,77 @@ static struct ldb_message *ltdb_pull_attrs(struct ldb_context *ldb,
 }
 
 
-
-/*
-  see if a ldb_val is a wildcard
-  return 1 if yes, 0 if no
-*/
-int ltdb_has_wildcard(struct ldb_context *ldb, const char *attr_name, 
-                     const struct ldb_val *val)
-{
-       int flags;
-
-       /* all attribute types recognise the "*" wildcard */
-       if (val->length == 1 && strncmp((char *)val->data, "*", 1) == 0) {
-               return 1;
-       }
-
-       if (strpbrk(val->data, "*?") == NULL) {
-               return 0;
-       }
-
-       flags = ltdb_attribute_flags(ldb, attr_name);
-       if (flags & LTDB_FLAG_WILDCARD) {
-               return 1;
-       }
-
-       return 0;
-}
-
-
-/*
-  free the results of a ltdb_search_dn1 search
-*/
-void ltdb_search_dn1_free(struct ldb_context *ldb, struct ldb_message *msg)
-{
-       int i;
-       ldb_free(ldb, msg->private_data);
-       for (i=0;i<msg->num_elements;i++) {
-               ldb_free(ldb, msg->elements[i].values);
-       }
-       ldb_free(ldb, msg->elements);
-       memset(msg, 0, sizeof(*msg));
-}
-
-
 /*
   search the database for a single simple dn, returning all attributes
   in a single message
 
-  return 1 on success, 0 on record-not-found and -1 on error
+  return LDB_ERR_NO_SUCH_OBJECT on record-not-found
+  and LDB_SUCCESS on success
 */
-int ltdb_search_dn1(struct ldb_context *ldb, const char *dn, struct ldb_message *msg)
+int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg)
 {
-       struct ltdb_private *ltdb = ldb->private_data;
+       struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
        int ret;
-       TDB_DATA tdb_key, tdb_data, tdb_data2;
+       TDB_DATA tdb_key, tdb_data;
 
        memset(msg, 0, sizeof(*msg));
 
        /* form the key */
-       tdb_key = ltdb_key(ldb, dn);
+       tdb_key = ltdb_key(module, dn);
        if (!tdb_key.dptr) {
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
        tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
-       ldb_free(ldb, tdb_key.dptr);
+       talloc_free(tdb_key.dptr);
        if (!tdb_data.dptr) {
-               return 0;
-       }
-
-       tdb_data2.dptr = ldb_malloc(ldb, tdb_data.dsize);
-       if (!tdb_data2.dptr) {
-               free(tdb_data.dptr);
-               return -1;
+               return LDB_ERR_NO_SUCH_OBJECT;
        }
-       memcpy(tdb_data2.dptr, tdb_data.dptr, tdb_data.dsize);
-       free(tdb_data.dptr);
-       tdb_data2.dsize = tdb_data.dsize;
 
-       msg->private_data = tdb_data2.dptr;
        msg->num_elements = 0;
        msg->elements = NULL;
 
-       ret = ltdb_unpack_data(ldb, &tdb_data2, msg);
+       ret = ltdb_unpack_data(module, &tdb_data, msg);
+       free(tdb_data.dptr);
        if (ret == -1) {
-               ldb_free(ldb, tdb_data2.dptr);
-               return -1;              
+               return LDB_ERR_OPERATIONS_ERROR;                
        }
 
        if (!msg->dn) {
-               msg->dn = ldb_strdup(ldb, dn);
+               msg->dn = ldb_dn_copy(msg, dn);
        }
        if (!msg->dn) {
-               ldb_free(ldb, tdb_data2.dptr);
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       return 1;
+       return LDB_SUCCESS;
 }
 
-
 /*
-  search the database for a single simple dn
+  lock the database for read - use by ltdb_search
 */
-int ltdb_search_dn(struct ldb_context *ldb, char *dn,
-                  const char * const attrs[], struct ldb_message ***res)
+static int ltdb_lock_read(struct ldb_module *module)
 {
-       int ret;
-       struct ldb_message msg, *msg2;
-
-       ret = ltdb_search_dn1(ldb, dn, &msg);
-       if (ret != 1) {
-               return ret;
-       }
-
-       msg2 = ltdb_pull_attrs(ldb, &msg, attrs);
-
-       ltdb_search_dn1_free(ldb, &msg);
-
-       if (!msg2) {
-               return -1;              
-       }
-
-       *res = ldb_malloc_array_p(ldb, struct ldb_message *, 2);
-       if (! *res) {
-               msg_free_all_parts(ldb, msg2);
-               return -1;              
-       }
-
-       (*res)[0] = msg2;
-       (*res)[1] = NULL;
-
-       return 1;
+       struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
+       return tdb_lockall_read(ltdb->tdb);
 }
 
+/*
+  unlock the database after a ltdb_lock_read()
+*/
+static int ltdb_unlock_read(struct ldb_module *module)
+{
+       struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
+       return tdb_unlockall_read(ltdb->tdb);
+}
 
 /*
   add a set of attributes from a record to a set of results
   return 0 on success, -1 on failure
 */
-int ltdb_add_attr_results(struct ldb_context *ldb, struct ldb_message *msg,
+int ltdb_add_attr_results(struct ldb_module *module, 
+                         TALLOC_CTX *mem_ctx, 
+                         struct ldb_message *msg,
                          const char * const attrs[], 
                          unsigned int *count, 
                          struct ldb_message ***res)
@@ -380,21 +280,21 @@ int ltdb_add_attr_results(struct ldb_context *ldb, struct ldb_message *msg,
        struct ldb_message **res2;
 
        /* pull the attributes that the user wants */
-       msg2 = ltdb_pull_attrs(ldb, msg, attrs);
+       msg2 = ltdb_pull_attrs(module, mem_ctx, msg, attrs);
        if (!msg2) {
                return -1;
        }
 
        /* add to the results list */
-       res2 = ldb_realloc_p(ldb, *res, struct ldb_message *, (*count)+2);
+       res2 = talloc_realloc(mem_ctx, *res, struct ldb_message *, (*count)+2);
        if (!res2) {
-               msg_free_all_parts(ldb, msg2);
+               talloc_free(msg2);
                return -1;
        }
 
        (*res) = res2;
 
-       (*res)[*count] = msg2;
+       (*res)[*count] = talloc_move(*res, &msg2);
        (*res)[(*count)+1] = NULL;
        (*count)++;
 
@@ -402,162 +302,223 @@ int ltdb_add_attr_results(struct ldb_context *ldb, struct ldb_message *msg,
 }
 
 
+
 /*
-  internal search state during a full db search
-*/
-struct ltdb_search_info {
-       struct ldb_context *ldb;
-       struct ldb_parse_tree *tree;
-       const char *base;
-       enum ldb_scope scope;
-       const char * const *attrs;
-       struct ldb_message **msgs;
-       int failures;
-       int count;
-};
+  filter the specified list of attributes from a message
+  removing not requested attrs.
+ */
+int ltdb_filter_attrs(struct ldb_message *msg, const char * const *attrs)
+{
+       int i, keep_all = 0;
+
+       if (attrs) {
+               /* check for special attrs */
+               for (i = 0; attrs[i]; i++) {
+                       if (strcmp(attrs[i], "*") == 0) {
+                               keep_all = 1;
+                               break;
+                       }
+
+                       if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
+                               if (msg_add_distinguished_name(msg) != 0) {
+                                       return -1;
+                               }
+                       }
+               }
+       } else {
+               keep_all = 1;
+       }
+       
+       if (keep_all) {
+               if (msg_add_distinguished_name(msg) != 0) {
+                       return -1;
+               }
+               return 0;
+       }
+
+       for (i = 0; i < msg->num_elements; i++) {
+               int j, found;
+               
+               for (j = 0, found = 0; attrs[j]; j++) {
+                       if (ldb_attr_cmp(msg->elements[i].name, attrs[j]) == 0) {
+                               found = 1;
+                               break;
+                       }
+               }
 
+               if (!found) {
+                       ldb_msg_remove_attr(msg, msg->elements[i].name);
+                       i--;
+               }
+       }
+
+       return 0;
+}
 
 /*
   search function for a non-indexed search
  */
 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
 {
-       struct ltdb_search_info *sinfo = state;
-       struct ldb_message msg;
+       struct ldb_handle *handle = talloc_get_type(state, struct ldb_handle);
+       struct ltdb_context *ac = talloc_get_type(handle->private_data, struct ltdb_context);
+       struct ldb_reply *ares = NULL;
        int ret;
 
        if (key.dsize < 4 || 
-           strncmp(key.dptr, "DN=", 3) != 0) {
+           strncmp((char *)key.dptr, "DN=", 3) != 0) {
                return 0;
        }
 
+       ares = talloc_zero(ac, struct ldb_reply);
+       if (!ares) {
+               handle->status = LDB_ERR_OPERATIONS_ERROR;
+               handle->state = LDB_ASYNC_DONE;
+               return -1;
+       }
+
+       ares->message = ldb_msg_new(ares);
+       if (!ares->message) {
+               handle->status = LDB_ERR_OPERATIONS_ERROR;
+               handle->state = LDB_ASYNC_DONE;
+               talloc_free(ares);
+               return -1;
+       }
+
        /* unpack the record */
-       ret = ltdb_unpack_data(sinfo->ldb, &data, &msg);
+       ret = ltdb_unpack_data(ac->module, &data, ares->message);
        if (ret == -1) {
-               sinfo->failures++;
-               return 0;
+               talloc_free(ares);
+               return -1;
        }
 
-       if (!msg.dn) {
-               msg.dn = key.dptr + 3;
+       if (!ares->message->dn) {
+               ares->message->dn = ldb_dn_new(ares->message, ac->module->ldb, (char *)key.dptr + 3);
+               if (ares->message->dn == NULL) {
+                       handle->status = LDB_ERR_OPERATIONS_ERROR;
+                       handle->state = LDB_ASYNC_DONE;
+                       talloc_free(ares);
+                       return -1;
+               }
        }
 
        /* see if it matches the given expression */
-       if (!ldb_message_match(sinfo->ldb, &msg, sinfo->tree, 
-                              sinfo->base, sinfo->scope)) {
-               ltdb_unpack_data_free(sinfo->ldb, &msg);
+       if (!ldb_match_msg(ac->module->ldb, ares->message, ac->tree, 
+                              ac->base, ac->scope)) {
+               talloc_free(ares);
                return 0;
        }
 
-       ret = ltdb_add_attr_results(sinfo->ldb, &msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
+       /* filter the attributes that the user wants */
+       ret = ltdb_filter_attrs(ares->message, ac->attrs);
 
        if (ret == -1) {
-               sinfo->failures++;
+               handle->status = LDB_ERR_OPERATIONS_ERROR;
+               handle->state = LDB_ASYNC_DONE;
+               talloc_free(ares);
+               return -1;
        }
 
-       ltdb_unpack_data_free(sinfo->ldb, &msg);
-
-       return ret;
-}
-
+       ares->type = LDB_REPLY_ENTRY;
+        handle->state = LDB_ASYNC_PENDING;
+       handle->status = ac->callback(ac->module->ldb, ac->context, ares);
 
-/*
-  free a set of search results
-*/
-int ltdb_search_free(struct ldb_context *ldb, struct ldb_message **msgs)
-{
-       struct ltdb_private *ltdb = ldb->private_data;
-       int i;
-
-       ltdb->last_err_string = NULL;
-       
-       if (!msgs) return 0;
-
-       for (i=0;msgs[i];i++) {
-               msg_free_all_parts(ldb, msgs[i]);
-       }
-
-       ldb_free(ldb, msgs);
+       if (handle->status != LDB_SUCCESS) {
+               /* don't try to free ares here, the callback is in charge of that */
+               return -1;
+       }       
 
        return 0;
 }
 
+
 /*
   search the database with a LDAP-like expression.
-  this is the "full search" non-indexed varient
+  this is the "full search" non-indexed variant
 */
-static int ltdb_search_full(struct ldb_context *ldb, 
-                           const char *base,
-                           enum ldb_scope scope,
-                           struct ldb_parse_tree *tree,
-                           const char * const attrs[], struct ldb_message ***res)
+static int ltdb_search_full(struct ldb_handle *handle)
 {
-       struct ltdb_private *ltdb = ldb->private_data;
+       struct ltdb_context *ac = talloc_get_type(handle->private_data, struct ltdb_context);
+       struct ltdb_private *ltdb = talloc_get_type(ac->module->private_data, struct ltdb_private);
        int ret;
-       struct ltdb_search_info sinfo;
-
-       sinfo.tree = tree;
-       sinfo.ldb = ldb;
-       sinfo.scope = scope;
-       sinfo.base = base;
-       sinfo.attrs = attrs;
-       sinfo.msgs = NULL;
-       sinfo.count = 0;
-       sinfo.failures = 0;
 
-       ret = tdb_traverse(ltdb->tdb, search_func, &sinfo);
+       ret = tdb_traverse_read(ltdb->tdb, search_func, handle);
 
        if (ret == -1) {
-               ltdb_search_free(ldb, sinfo.msgs);
-               return -1;
+               handle->status = LDB_ERR_OPERATIONS_ERROR;
        }
 
-       *res = sinfo.msgs;
-       return sinfo.count;
+       handle->state = LDB_ASYNC_DONE;
+       return LDB_SUCCESS;
 }
 
-
 /*
   search the database with a LDAP-like expression.
   choses a search method
 */
-int ltdb_search(struct ldb_context *ldb, const char *base,
-               enum ldb_scope scope, const char *expression,
-               const char * const attrs[], struct ldb_message ***res)
+int ltdb_search(struct ldb_module *module, struct ldb_request *req)
 {
-       struct ltdb_private *ltdb = ldb->private_data;
-       struct ldb_parse_tree *tree;
+       struct ltdb_private *ltdb = talloc_get_type(module->private_data, struct ltdb_private);
+       struct ltdb_context *ltdb_ac;
+       struct ldb_reply *ares;
        int ret;
 
-       ltdb->last_err_string = NULL;
+       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 (ltdb_cache_load(ldb) != 0) {
-               return -1;
+       if (ltdb_lock_read(module) != 0) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       *res = NULL;
+       if (ltdb_cache_load(module) != 0) {
+               ltdb_unlock_read(module);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
-       /* form a parse tree for the expression */
-       tree = ldb_parse_tree(ldb, expression);
-       if (!tree) {
-               ltdb->last_err_string = "expression parse failed";
-               return -1;
+       if (req->op.search.tree == NULL) {
+               ltdb_unlock_read(module);
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       if (tree->operation == LDB_OP_SIMPLE && 
-           ldb_attr_cmp(tree->u.simple.attr, "dn") == 0 &&
-           !ltdb_has_wildcard(ldb, tree->u.simple.attr, &tree->u.simple.value)) {
-               /* yay! its a nice simple one */
-               ret = ltdb_search_dn(ldb, tree->u.simple.value.data, attrs, res);
-       } else {
-               ret = ltdb_search_indexed(ldb, base, scope, tree, attrs, res);
-               if (ret == -1) {
-                       ret = ltdb_search_full(ldb, base, scope, tree, attrs, res);
-               }
+       req->handle = init_ltdb_handle(ltdb, module, req);
+       if (req->handle == NULL) {
+               ltdb_unlock_read(module);
+               return LDB_ERR_OPERATIONS_ERROR;
        }
+       ltdb_ac = talloc_get_type(req->handle->private_data, struct ltdb_context);
 
-       ldb_parse_tree_free(ldb, tree);
+       ltdb_ac->tree = req->op.search.tree;
+       ltdb_ac->scope = req->op.search.scope;
+       ltdb_ac->base = req->op.search.base;
+       ltdb_ac->attrs = req->op.search.attrs;
 
-       return ret;
+       ret = ltdb_search_indexed(req->handle);
+       if (ret == LDB_ERR_OPERATIONS_ERROR) {
+               ret = ltdb_search_full(req->handle);
+       }
+       if (ret != LDB_SUCCESS) {
+               ldb_set_errstring(module->ldb, "Indexed and full searches both failed!\n");
+               req->handle->state = LDB_ASYNC_DONE;
+               req->handle->status = ret;
+       }
+
+       /* Finally send an LDB_REPLY_DONE packet when searching is finished */
+
+       ares = talloc_zero(req, struct ldb_reply);
+       if (!ares) {
+               ltdb_unlock_read(module);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       req->handle->state = LDB_ASYNC_DONE;
+       ares->type = LDB_REPLY_DONE;
+
+       ret = req->callback(module->ldb, req->context, ares);
+       req->handle->status = ret;
+
+       ltdb_unlock_read(module);
+
+       return LDB_SUCCESS;
 }