s4:ldb Remove LTDB_PACKING_FORMAT_NODN
[ira/wip.git] / source4 / lib / ldb / ldb_tdb / ldb_search.c
index 3644d046e095c3df3253ba484d5f1b0d8b103598..a089a2f82695aed7d5622a4796f2d282da871c13 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/include/ldb.h"
-#include "ldb/include/ldb_private.h"
-#include "ldb/ldb_tdb/ldb_tdb.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)
 {
        unsigned int i;
        struct ldb_message_element *e2, *elnew;
 
+       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;
@@ -82,21 +84,50 @@ 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_module *module, struct ldb_message *ret,
                                const struct ldb_message *msg)
 {
-       struct ldb_context *ldb = module->ldb;
+       struct ldb_context *ldb;
        unsigned int i;
+       int check_duplicates = (ret->num_elements != 0);
+
+       ldb = ldb_module_get_ctx(module);
+
+       if (msg_add_distinguished_name(ret) != 0) {
+               return -1;
+       }
 
        for (i=0;i<msg->num_elements;i++) {
-               int flags = ltdb_attribute_flags(module, 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;
                }
        }
@@ -109,19 +140,19 @@ static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *r
   pull the specified list of attributes from a message
  */
 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_context *ldb = module->ldb;
        struct ldb_message *ret;
        int i;
 
-       ret = talloc(ldb, struct ldb_message);
+       ret = talloc(mem_ctx, struct ldb_message);
        if (!ret) {
                return NULL;
        }
 
-       ret->dn = talloc_strdup(ret, msg->dn);
+       ret->dn = ldb_dn_copy(ret, msg->dn);
        if (!ret->dn) {
                talloc_free(ret);
                return NULL;
@@ -149,27 +180,10 @@ static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module,
                        continue;
                }
 
-               if (ldb_attr_cmp(attrs[i], "dn") == 0 ||
-                   ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
-                       struct ldb_message_element el2;
-                       struct ldb_val val;
-
-                       el2.flags = 0;
-                       el2.name = talloc_strdup(ret, attrs[i]);
-                       if (!el2.name) {
-                               talloc_free(ret);
-                               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) {
-                               talloc_free(ret);
-                               return NULL;                            
+               if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
+                       if (msg_add_distinguished_name(ret) != 0) {
+                               return NULL;
                        }
-                       talloc_free(discard_const_p(char, el2.name));
                        continue;
                }
 
@@ -177,7 +191,7 @@ static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module,
                if (!el) {
                        continue;
                }
-               if (msg_add_element(ldb, ret, el) != 0) {
+               if (msg_add_element(ret, el, 1) != 0) {
                        talloc_free(ret);
                        return NULL;                            
                }
@@ -186,154 +200,109 @@ static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module,
        return ret;
 }
 
-
-
 /*
-  see if a ldb_val is a wildcard
-  return 1 if yes, 0 if no
+  search the database for a single simple dn.
+  return LDB_ERR_NO_SUCH_OBJECT on record-not-found
+  and LDB_SUCCESS on success
 */
-int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name, 
-                     const struct ldb_val *val)
+static int ltdb_search_base(struct ldb_module *module, struct ldb_dn *dn)
 {
-       int flags;
+       void *data = ldb_module_get_private(module);
+       struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+       TDB_DATA tdb_key, tdb_data;
 
-       /* all attribute types recognise the "*" wildcard */
-       if (val->length == 1 && strncmp((char *)val->data, "*", 1) == 0) {
-               return 1;
+       if (ldb_dn_is_null(dn)) {
+               return LDB_ERR_NO_SUCH_OBJECT;
        }
 
-       if (strpbrk(val->data, "*?") == NULL) {
-               return 0;
+       /* form the key */
+       tdb_key = ltdb_key(module, dn);
+       if (!tdb_key.dptr) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       flags = ltdb_attribute_flags(module, attr_name);
-       if (flags & LTDB_FLAG_WILDCARD) {
-               return 1;
+       tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
+       talloc_free(tdb_key.dptr);
+       if (!tdb_data.dptr) {
+               return LDB_ERR_NO_SUCH_OBJECT;
        }
-
-       return 0;
+       
+       free(tdb_data.dptr);
+       return LDB_SUCCESS;
 }
 
-
 /*
   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_module *module, 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 = module->private_data;
+       void *data = ldb_module_get_private(module);
+       struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
        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(module, dn);
        if (!tdb_key.dptr) {
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
        tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
        talloc_free(tdb_key.dptr);
        if (!tdb_data.dptr) {
-               return 0;
+               return LDB_ERR_NO_SUCH_OBJECT;
        }
-
-       tdb_data2.dptr = talloc_memdup(msg, tdb_data.dptr, tdb_data.dsize);
-       free(tdb_data.dptr);
-       if (!tdb_data2.dptr) {
-               return -1;
-       }
-       tdb_data2.dsize = tdb_data.dsize;
-
+       
        msg->num_elements = 0;
        msg->elements = NULL;
 
-       ret = ltdb_unpack_data(module, &tdb_data2, msg);
+       ret = ltdb_unpack_data(module, &tdb_data, msg);
+       free(tdb_data.dptr);
        if (ret == -1) {
-               talloc_free(tdb_data2.dptr);
-               return -1;              
+               struct ldb_context *ldb = ldb_module_get_ctx(module);
+               ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %s\n",
+                         ldb_dn_get_linearized(msg->dn));
+               return LDB_ERR_OPERATIONS_ERROR;                
        }
 
        if (!msg->dn) {
-               msg->dn = talloc_strdup(tdb_data2.dptr, dn);
+               msg->dn = ldb_dn_copy(msg, dn);
        }
        if (!msg->dn) {
-               talloc_free(tdb_data2.dptr);
-               return -1;
-       }
-
-       return 1;
-}
-
-
-/*
-  search the database for a single simple dn
-*/
-int ltdb_search_dn(struct ldb_module *module, const char *dn,
-                  const char * const attrs[], struct ldb_message ***res)
-{
-       struct ldb_context *ldb = module->ldb;
-       int ret;
-       struct ldb_message *msg, *msg2;
-
-       *res = talloc_array(ldb, struct ldb_message *, 2);
-       if (! *res) {
-               return -1;              
-       }
-
-       msg = talloc(*res, struct ldb_message);
-       if (msg == NULL) {
-               talloc_free(*res);
-               *res = NULL;
-               return -1;
-       }
-
-       ret = ltdb_search_dn1(module, dn, msg);
-       if (ret != 1) {
-               talloc_free(*res);
-               *res = NULL;
-               return ret;
-       }
-
-       msg2 = ltdb_pull_attrs(module, msg, attrs);
-
-       talloc_free(msg);
-
-       if (!msg2) {
-               return -1;              
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       (*res)[0] = talloc_steal(*res, msg2);
-       (*res)[1] = NULL;
-
-       return 1;
+       return LDB_SUCCESS;
 }
 
-
 /*
   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_module *module, 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[], 
-                         int *count, 
+                         unsigned int *count, 
                          struct ldb_message ***res)
 {
-       struct ldb_context *ldb = module->ldb;
        struct ldb_message *msg2;
        struct ldb_message **res2;
 
        /* pull the attributes that the user wants */
-       msg2 = ltdb_pull_attrs(module, msg, attrs);
+       msg2 = ltdb_pull_attrs(module, mem_ctx, msg, attrs);
        if (!msg2) {
                return -1;
        }
 
        /* add to the results list */
-       res2 = talloc_realloc(ldb, *res, struct ldb_message *, (*count)+2);
+       res2 = talloc_realloc(mem_ctx, *res, struct ldb_message *, (*count)+2);
        if (!res2) {
                talloc_free(msg2);
                return -1;
@@ -341,7 +310,7 @@ int ltdb_add_attr_results(struct ldb_module *module, struct ldb_message *msg,
 
        (*res) = res2;
 
-       (*res)[*count] = talloc_steal(*res, msg2);
+       (*res)[*count] = talloc_move(*res, &msg2);
        (*res)[(*count)+1] = NULL;
        (*count)++;
 
@@ -349,68 +318,112 @@ int ltdb_add_attr_results(struct ldb_module *module, struct ldb_message *msg,
 }
 
 
+
 /*
-  internal search state during a full db search
-*/
-struct ltdb_search_info {
-       struct ldb_module *module;
-       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_context *ldb;
+       struct ltdb_context *ac;
        struct ldb_message *msg;
        int ret;
 
+       ac = talloc_get_type(state, struct ltdb_context);
+       ldb = ldb_module_get_ctx(ac->module);
+
        if (key.dsize < 4 || 
-           strncmp(key.dptr, "DN=", 3) != 0) {
+           strncmp((char *)key.dptr, "DN=", 3) != 0) {
                return 0;
        }
 
-       msg = talloc(sinfo, struct ldb_message);
-       if (msg == NULL) {
+       msg = ldb_msg_new(ac);
+       if (!msg) {
                return -1;
        }
 
        /* unpack the record */
-       ret = ltdb_unpack_data(sinfo->module, &data, msg);
+       ret = ltdb_unpack_data(ac->module, &data, msg);
        if (ret == -1) {
-               sinfo->failures++;
                talloc_free(msg);
-               return 0;
-       }
-
-       if (!msg->dn) {
-               msg->dn = key.dptr + 3;
+               return -1;
        }
 
        /* see if it matches the given expression */
-       if (!ltdb_message_match(sinfo->module, msg, sinfo->tree, 
-                               sinfo->base, sinfo->scope)) {
+       if (!ldb_match_msg(ldb, msg,
+                          ac->tree, ac->base, ac->scope)) {
                talloc_free(msg);
                return 0;
        }
 
-       ret = ltdb_add_attr_results(sinfo->module, msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
+       /* filter the attributes that the user wants */
+       ret = ltdb_filter_attrs(msg, ac->attrs);
 
        if (ret == -1) {
-               sinfo->failures++;
+               talloc_free(msg);
+               return -1;
        }
 
-       talloc_free(msg);
+       ret = ldb_module_send_entry(ac->req, msg, NULL);
+       if (ret != LDB_SUCCESS) {
+               ac->request_terminated = true;
+               /* the callback failed, abort the operation */
+               return -1;
+       }
 
-       return ret;
+       return 0;
 }
 
 
@@ -418,108 +431,144 @@ static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, voi
   search the database with a LDAP-like expression.
   this is the "full search" non-indexed variant
 */
-static int ltdb_search_full(struct ldb_module *module, 
-                           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 ltdb_context *ctx)
 {
-       struct ltdb_private *ltdb = module->private_data;
-       int ret, count;
-       struct ltdb_search_info *sinfo;
+       void *data = ldb_module_get_private(ctx->module);
+       struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+       int ret;
 
-       sinfo = talloc(ltdb, struct ltdb_search_info);
-       if (sinfo == NULL) {
-               return -1;
+       if (ltdb->in_transaction != 0) {
+               ret = tdb_traverse(ltdb->tdb, search_func, ctx);
+       } else {
+               ret = tdb_traverse_read(ltdb->tdb, search_func, ctx);
        }
 
-       sinfo->tree = tree;
-       sinfo->module = module;
-       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);
-
        if (ret == -1) {
-               talloc_free(sinfo);
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       *res = talloc_steal(ltdb, sinfo->msgs);
-       count = sinfo->count;
-
-       talloc_free(sinfo);
-
-       return count;
+       return LDB_SUCCESS;
 }
 
-
 /*
   search the database with a LDAP-like expression.
   choses a search method
 */
-int ltdb_search_bytree(struct ldb_module *module, const char *base,
-                      enum ldb_scope scope, struct ldb_parse_tree *tree,
-                      const char * const attrs[], struct ldb_message ***res)
+int ltdb_search(struct ltdb_context *ctx)
 {
-       struct ltdb_private *ltdb = module->private_data;
+       struct ldb_context *ldb;
+       struct ldb_module *module = ctx->module;
+       struct ldb_request *req = ctx->req;
+       void *data = ldb_module_get_private(module);
+       struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
        int ret;
 
+       ldb = ldb_module_get_ctx(module);
+
+       ldb_request_set_state(req, LDB_ASYNC_PENDING);
+
        if (ltdb_lock_read(module) != 0) {
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       ltdb->last_err_string = NULL;
-
        if (ltdb_cache_load(module) != 0) {
                ltdb_unlock_read(module);
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       *res = NULL;
-
-       ret = ltdb_search_indexed(module, base, scope, tree, attrs, res);
-       if (ret == -1) {
-               ret = ltdb_search_full(module, base, scope, tree, attrs, res);
+       if (req->op.search.tree == NULL) {
+               ltdb_unlock_read(module);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       if ((req->op.search.base == NULL) || (ldb_dn_is_null(req->op.search.base) == true)) {
+
+               /* Check what we should do with a NULL dn */
+               switch (req->op.search.scope) {
+               case LDB_SCOPE_BASE:
+                       ldb_asprintf_errstring(ldb, 
+                                              "NULL Base DN invalid for a base search");
+                       ret = LDB_ERR_INVALID_DN_SYNTAX;
+                       break;
+               case LDB_SCOPE_ONELEVEL:
+                       ldb_asprintf_errstring(ldb, 
+                                              "NULL Base DN invalid for a one-level search");
+                       ret = LDB_ERR_INVALID_DN_SYNTAX;        
+                       break;
+               case LDB_SCOPE_SUBTREE:
+               default:
+                       /* We accept subtree searches from a NULL base DN, ie over the whole DB */
+                       ret = LDB_SUCCESS;
+               }
+       } else if (ldb_dn_is_valid(req->op.search.base) == false) {
+
+               /* We don't want invalid base DNs here */
+               ldb_asprintf_errstring(ldb, 
+                                      "Invalid Base DN: %s", 
+                                      ldb_dn_get_linearized(req->op.search.base));
+               ret = LDB_ERR_INVALID_DN_SYNTAX;
+
+       } else if (ltdb->check_base) {
+               /* This database has been marked as 'checkBaseOnSearch', so do a spot check of the base dn */
+               ret = ltdb_search_base(module, req->op.search.base);
+               
+               if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+                       ldb_asprintf_errstring(ldb, 
+                                              "No such Base DN: %s", 
+                                              ldb_dn_get_linearized(req->op.search.base));
+               }
+                       
+       } else {
+               /* If we are not checking the base DN life is easy */
+               ret = LDB_SUCCESS;
        }
 
-       ltdb_unlock_read(module);
-
-       return ret;
-}
-
+       ctx->tree = req->op.search.tree;
+       ctx->scope = req->op.search.scope;
+       ctx->base = req->op.search.base;
+       ctx->attrs = req->op.search.attrs;
 
-/*
-  search the database with a LDAP-like expression.
-  choses a search method
-*/
-int ltdb_search(struct ldb_module *module, const char *base,
-               enum ldb_scope scope, const char *expression,
-               const char * const attrs[], struct ldb_message ***res)
-{
-       struct ltdb_private *ltdb = module->private_data;
-       struct ldb_parse_tree *tree;
-       int ret;
+       if (ret == LDB_SUCCESS) {
+               uint32_t match_count = 0;
 
-       /* check if we are looking for a simple dn */
-       if (scope == LDB_SCOPE_BASE && (expression == NULL || expression[0] == '\0')) {
-               ret = ltdb_search_dn(module, base, attrs, res);
-               ltdb_unlock_read(module);
-               return ret;
+               ret = ltdb_search_indexed(ctx, &match_count);
+               if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+                       /* Not in the index, therefore OK! */
+                       ret = LDB_SUCCESS;
+                       
+               }
+               /* Check if we got just a normal error.
+                * In that case proceed to a full search unless we got a
+                * callback error */
+               if ( ! ctx->request_terminated && ret != LDB_SUCCESS) {
+                       /* Not indexed, so we need to do a full scan */
+#if 0
+                       /* useful for debugging when slow performance
+                        * is caused by unindexed searches */
+                       char *expression = ldb_filter_from_tree(ctx, ctx->tree);
+                       printf("FULL SEARCH: %s\n", expression);
+                       talloc_free(expression);
+#endif
+                       if (match_count != 0) {
+                               /* the indexing code gave an error
+                                * after having returned at least one
+                                * entry. This means the indexes are
+                                * corrupt or a database record is
+                                * corrupt. We cannot continue with a
+                                * full search or we may return
+                                * duplicate entries
+                                */
+                               return LDB_ERR_OPERATIONS_ERROR;
+                       }
+                       ret = ltdb_search_full(ctx);
+                       if (ret != LDB_SUCCESS) {
+                               ldb_set_errstring(ldb, "Indexed and full searches both failed!\n");
+                       }
+               }
        }
 
-       tree = ldb_parse_tree(ltdb, expression);
-       if (tree == NULL) {
-               ltdb->last_err_string = "expression parse failed";
-               return -1;
-       }
+       ltdb_unlock_read(module);
 
-       ret = ltdb_search_bytree(module, base, scope, tree, attrs, res);
-       talloc_free(tree);
        return ret;
 }