Return per-entry controls in ldb_module_send_entry()
[abartlet/samba.git/.git] / source4 / lib / ldb / ldb_tdb / ldb_search.c
index 515503105504c5f3dde0819c05ebc125f21d119e..35149c4b77f3e4612d8884ac64bc37aa00330d62 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/includes.h"
+#include "ldb_includes.h"
 
-#include "ldb/ldb_tdb/ldb_tdb.h"
+#include "ldb_tdb.h"
 
 /*
   add one element to a message
@@ -101,7 +99,7 @@ static int msg_add_distinguished_name(struct ldb_message *msg)
        el.name = "distinguishedName";
        el.num_values = 1;
        el.values = &val;
-       val.data = (uint8_t *)ldb_dn_linearize(msg, msg->dn);
+       val.data = (uint8_t *)ldb_dn_alloc_linearized(msg, msg->dn);
        val.length = strlen((char *)val.data);
        
        ret = msg_add_element(msg, &el, 1);
@@ -123,9 +121,9 @@ static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *r
        }
 
        for (i=0;i<msg->num_elements;i++) {
-               const struct ldb_attrib_handler *h;
-               h = ldb_attrib_handler(ldb, msg->elements[i].name);
-               if (h->flags & LDB_ATTR_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(ret, &msg->elements[i],
@@ -202,16 +200,46 @@ static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module,
        return ret;
 }
 
+/*
+  search the database for a single simple dn.
+  return LDB_ERR_NO_SUCH_OBJECT on record-not-found
+  and LDB_SUCCESS on success
+*/
+static int ltdb_search_base(struct ldb_module *module, struct ldb_dn *dn)
+{
+       struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
+       TDB_DATA tdb_key, tdb_data;
+
+       if (ldb_dn_is_null(dn)) {
+               return LDB_ERR_NO_SUCH_OBJECT;
+       }
+
+       /* form the key */
+       tdb_key = ltdb_key(module, dn);
+       if (!tdb_key.dptr) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
+       talloc_free(tdb_key.dptr);
+       if (!tdb_data.dptr) {
+               return LDB_ERR_NO_SUCH_OBJECT;
+       }
+       
+       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 struct ldb_dn *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;
+       struct ltdb_private *ltdb = (struct ltdb_private *)module->private_data;
        int ret;
        TDB_DATA tdb_key, tdb_data;
 
@@ -220,67 +248,32 @@ int ltdb_search_dn1(struct ldb_module *module, const struct ldb_dn *dn, struct l
        /* 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;
        }
-
+       
        msg->num_elements = 0;
        msg->elements = NULL;
 
        ret = ltdb_unpack_data(module, &tdb_data, msg);
        free(tdb_data.dptr);
        if (ret == -1) {
-               return -1;              
+               return LDB_ERR_OPERATIONS_ERROR;                
        }
 
        if (!msg->dn) {
                msg->dn = ldb_dn_copy(msg, dn);
        }
        if (!msg->dn) {
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       return 1;
-}
-
-/* the lock key for search locking. Note that this is not a DN, its
-   just an arbitrary key to give to tdb. Also note that as we and
-   using transactions for all write operations and transactions take
-   care of their own locks, we don't need to do any locking anywhere
-   other than in ldb_search() */
-#define LDBLOCK        "INT_LDBLOCK"
-
-/*
-  lock the database for read - use by ltdb_search
-*/
-static int ltdb_lock_read(struct ldb_module *module)
-{
-       struct ltdb_private *ltdb = module->private_data;
-       TDB_DATA key;
-
-       key.dptr = discard_const(LDBLOCK);
-       key.dsize = strlen(LDBLOCK);
-
-       return tdb_chainlock_read(ltdb->tdb, key);
-}
-
-/*
-  unlock the database after a ltdb_lock_read()
-*/
-static int ltdb_unlock_read(struct ldb_module *module)
-{
-       struct ltdb_private *ltdb = module->private_data;
-       TDB_DATA key;
-
-       key.dptr = discard_const(LDBLOCK);
-       key.dsize = strlen(LDBLOCK);
-
-       return tdb_chainunlock_read(ltdb->tdb, key);
+       return LDB_SUCCESS;
 }
 
 /*
@@ -312,7 +305,7 @@ int ltdb_add_attr_results(struct ldb_module *module,
 
        (*res) = res2;
 
-       (*res)[*count] = talloc_steal(*res, msg2);
+       (*res)[*count] = talloc_move(*res, &msg2);
        (*res)[(*count)+1] = NULL;
        (*count)++;
 
@@ -378,71 +371,57 @@ int ltdb_filter_attrs(struct ldb_message *msg, const char * const *attrs)
  */
 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
 {
-       struct ldb_async_handle *handle = talloc_get_type(state, struct ldb_async_handle);
-       struct ltdb_async_context *ac = talloc_get_type(handle->private_data, struct ltdb_async_context);
-       struct ldb_async_result *ares = NULL;
+       struct ltdb_context *ac;
+       struct ldb_message *msg;
        int ret;
 
+       ac = talloc_get_type(state, struct ltdb_context);
+
        if (key.dsize < 4 || 
            strncmp((char *)key.dptr, "DN=", 3) != 0) {
                return 0;
        }
 
-       ares = talloc_zero(ac, struct ldb_async_result);
-       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);
+       msg = ldb_msg_new(ac);
+       if (!msg) {
                return -1;
        }
 
        /* unpack the record */
-       ret = ltdb_unpack_data(ac->module, &data, ares->message);
+       ret = ltdb_unpack_data(ac->module, &data, msg);
        if (ret == -1) {
-               talloc_free(ares);
+               talloc_free(msg);
                return -1;
        }
 
-       if (!ares->message->dn) {
-               ares->message->dn = ldb_dn_explode(ares->message, (char *)key.dptr + 3);
-               if (ares->message->dn == NULL) {
-                       handle->status = LDB_ERR_OPERATIONS_ERROR;
-                       handle->state = LDB_ASYNC_DONE;
-                       talloc_free(ares);
+       if (!msg->dn) {
+               msg->dn = ldb_dn_new(msg, ac->module->ldb,
+                                    (char *)key.dptr + 3);
+               if (msg->dn == NULL) {
+                       talloc_free(msg);
                        return -1;
                }
        }
 
        /* see if it matches the given expression */
-       if (!ldb_match_msg(ac->module->ldb, ares->message, ac->tree, 
-                              ac->base, ac->scope)) {
-               talloc_free(ares);
+       if (!ldb_match_msg(ac->module->ldb, msg,
+                          ac->tree, ac->base, ac->scope)) {
+               talloc_free(msg);
                return 0;
        }
 
        /* filter the attributes that the user wants */
-       ret = ltdb_filter_attrs(ares->message, ac->attrs);
+       ret = ltdb_filter_attrs(msg, ac->attrs);
 
        if (ret == -1) {
-               handle->status = LDB_ERR_OPERATIONS_ERROR;
-               handle->state = LDB_ASYNC_DONE;
-               talloc_free(ares);
+               talloc_free(msg);
                return -1;
        }
 
-       ares->type = LDB_REPLY_ENTRY;
-        handle->state = LDB_ASYNC_PENDING;
-       handle->status = ac->callback(ac->module->ldb, ac->context, ares);
-
-       if (handle->status != LDB_SUCCESS) {
-               /* don't try to free ares here, the callback is in charge of that */
+       ret = ldb_module_send_entry(ac->req, msg, NULL);
+       if (ret != LDB_SUCCESS) {
+               ac->callback_failed = true;
+               /* the callback failed, abort the operation */
                return -1;
        }       
 
@@ -454,83 +433,36 @@ 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_async_handle *handle)
+static int ltdb_search_full(struct ltdb_context *ctx)
 {
-       struct ltdb_async_context *ac = talloc_get_type(handle->private_data, struct ltdb_async_context);
-       struct ltdb_private *ltdb = talloc_get_type(ac->module->private_data, struct ltdb_private);
+       struct ltdb_private *ltdb = talloc_get_type(ctx->module->private_data, struct ltdb_private);
        int ret;
 
-       ret = tdb_traverse_read(ltdb->tdb, search_func, handle);
-
-       handle->state = LDB_ASYNC_DONE;
-
-       if (ret == -1) {
-               handle->status = LDB_ERR_OPERATIONS_ERROR;
-               return handle->status;
-       }
-
-       handle->status = LDB_SUCCESS;
-       return handle->status;
-}
-
-static int ltdb_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 (ltdb->in_transaction != 0) {
+               ret = tdb_traverse(ltdb->tdb, search_func, ctx);
+       } else {
+               ret = tdb_traverse_read(ltdb->tdb, search_func, ctx);
        }
 
-       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;
+       if (ret == -1) {
+               return LDB_ERR_OPERATIONS_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;
 }
 
-int ltdb_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)
+/*
+  search the database with a LDAP-like expression.
+  choses a search method
+*/
+int ltdb_search(struct ltdb_context *ctx)
 {
+       struct ldb_module *module = ctx->module;
+       struct ldb_request *req = ctx->req;
        struct ltdb_private *ltdb = talloc_get_type(module->private_data, struct ltdb_private);
-       struct ltdb_async_context *ltdb_ac;
        int ret;
 
-       if ((base == NULL || base->comp_num == 0) &&
-           (scope == LDB_SCOPE_BASE || scope == LDB_SCOPE_ONELEVEL))
-               return LDB_ERR_OPERATIONS_ERROR;
+       req->handle->state = LDB_ASYNC_PENDING;
 
        if (ltdb_lock_read(module) != 0) {
                return LDB_ERR_OPERATIONS_ERROR;
@@ -541,70 +473,79 @@ int ltdb_search_async(struct ldb_module *module, const struct ldb_dn *base,
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       if (tree == NULL) {
+       if (req->op.search.tree == NULL) {
                ltdb_unlock_read(module);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       *handle = init_ltdb_handle(ltdb, module, context, callback);
-       if (*handle == NULL) {
-               talloc_free(*handle);
-               ltdb_unlock_read(module);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       ltdb_ac = talloc_get_type((*handle)->private_data, struct ltdb_async_context);
-
-       ltdb_ac->tree = tree;
-       ltdb_ac->scope = scope;
-       ltdb_ac->base = base;
-       ltdb_ac->attrs = attrs;
-
-       ret = ltdb_search_indexed(*handle);
-       if (ret == -1) {
-               ret = ltdb_search_full(*handle);
-       }
-       if (ret != LDB_SUCCESS) {
-               ldb_set_errstring(module->ldb, talloc_strdup(module->ldb, "Indexed and full searches both failed!\n"));
-               talloc_free(*handle);
-               *handle = NULL;
-       }
-
-       ltdb_unlock_read(module);
+       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(module->ldb, 
+                                              "NULL Base DN invalid for a base search");
+                       ret = LDB_ERR_INVALID_DN_SYNTAX;
+                       break;
+               case LDB_SCOPE_ONELEVEL:
+                       ldb_asprintf_errstring(module->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) {
 
-       return ret;
-}
+               /* We don't want invalid base DNs here */
+               ldb_asprintf_errstring(module->ldb, 
+                                      "Invalid Base DN: %s", 
+                                      ldb_dn_get_linearized(req->op.search.base));
+               ret = LDB_ERR_INVALID_DN_SYNTAX;
 
-/*
-  search the database with a LDAP-like expression.
-  choses a search method
-*/
-int ltdb_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;
+       } 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(module->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;
        }
 
-       ret = ltdb_search_async(module, base, scope, tree, attrs,
-                               res, &ltdb_search_sync_callback,
-                               &handle);
+       ctx->tree = req->op.search.tree;
+       ctx->scope = req->op.search.scope;
+       ctx->base = req->op.search.base;
+       ctx->attrs = req->op.search.attrs;
 
        if (ret == LDB_SUCCESS) {
-               ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
-               talloc_free(handle);
+               ret = ltdb_search_indexed(ctx);
+               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->callback_failed && ret != LDB_SUCCESS) {
+                       /* Not indexed, so we need to do a full scan */
+                       ret = ltdb_search_full(ctx);
+                       if (ret != LDB_SUCCESS) {
+                               ldb_set_errstring(module->ldb, "Indexed and full searches both failed!\n");
+                       }
+               }
        }
 
-       if (ret != LDB_SUCCESS) {
-               talloc_free(*res);
-       }
+       ltdb_unlock_read(module);
 
        return ret;
 }
 
-