Fix the mess with ldb includes.
[metze/samba/wip.git] / source4 / lib / ldb / modules / operational.c
index 716f34d660eb69d1c1809101ab416542267d53ce..d862638389ca6302f8c7d4fd4afda52976a336cc 100644 (file)
@@ -2,7 +2,7 @@
    ldb database library
 
    Copyright (C) Andrew Tridgell 2005
-   Copyright (C) Simo Sorce 2006
+   Copyright (C) Simo Sorce 2006-2008
 
      ** NOTE! The following LGPL license applies to the ldb
      ** library. This does NOT imply that all of Samba is released
@@ -73,7 +73,7 @@
   modifiersName: not supported by w2k3?
 */
 
-#include "ldb_includes.h"
+#include "ldb_module.h"
 
 /*
   construct a canonical name from a message
@@ -126,8 +126,11 @@ static int operational_search_post_process(struct ldb_module *module,
                                           struct ldb_message *msg, 
                                           const char * const *attrs)
 {
+       struct ldb_context *ldb;
        int i, a=0;
 
+       ldb = ldb_module_get_ctx(module);
+
        for (a=0;attrs && attrs[a];a++) {
                for (i=0;i<ARRAY_SIZE(search_sub);i++) {
                        if (ldb_attr_cmp(attrs[a], search_sub[i].attr) != 0) {
@@ -158,13 +161,13 @@ static int operational_search_post_process(struct ldb_module *module,
                }
        }
 
-       return LDB_SUCCESS;
+       return 0;
 
 failed:
-       ldb_debug_set(module->ldb, LDB_DEBUG_WARNING, 
+       ldb_debug_set(ldb, LDB_DEBUG_WARNING, 
                      "operational_search_post_process failed for attribute '%s'\n", 
                      attrs[a]);
-       return LDB_ERR_OPERATIONS_ERROR;
+       return -1;
 }
 
 
@@ -173,48 +176,65 @@ failed:
 */
 
 struct operational_context {
-
        struct ldb_module *module;
-       void *up_context;
-       int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
+       struct ldb_request *req;
 
        const char * const *attrs;
 };
 
-static int operational_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
+static int operational_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
        struct operational_context *ac;
+       int ret;
 
-       if (!context || !ares) {
-               ldb_set_errstring(ldb, "NULL Context or Result in callback");
-               goto error;
-       }
+       ac = talloc_get_type(req->context, struct operational_context);
 
-       ac = talloc_get_type(context, struct operational_context);
+       if (!ares) {
+               return ldb_module_done(ac->req, NULL, NULL,
+                                       LDB_ERR_OPERATIONS_ERROR);
+       }
+       if (ares->error != LDB_SUCCESS) {
+               return ldb_module_done(ac->req, ares->controls,
+                                       ares->response, ares->error);
+       }
 
-       if (ares->type == LDB_REPLY_ENTRY) {
+       switch (ares->type) {
+       case LDB_REPLY_ENTRY:
                /* for each record returned post-process to add any derived
                   attributes that have been asked for */
-               if (operational_search_post_process(ac->module, ares->message, ac->attrs) != 0) {
-                       goto error;
+               ret = operational_search_post_process(ac->module,
+                                                       ares->message,
+                                                       ac->attrs);
+               if (ret != 0) {
+                       return ldb_module_done(ac->req, NULL, NULL,
+                                               LDB_ERR_OPERATIONS_ERROR);
                }
-       }
+               return ldb_module_send_entry(ac->req, ares->message, ares->controls);
+
+       case LDB_REPLY_REFERRAL:
+               /* ignore referrals */
+               break;
 
-       return ac->up_callback(ldb, ac->up_context, ares);
+       case LDB_REPLY_DONE:
+
+               return ldb_module_done(ac->req, ares->controls,
+                                       ares->response, LDB_SUCCESS);
+       }
 
-error:
        talloc_free(ares);
-       return LDB_ERR_OPERATIONS_ERROR;
+       return LDB_SUCCESS;
 }
 
 static int operational_search(struct ldb_module *module, struct ldb_request *req)
 {
+       struct ldb_context *ldb;
        struct operational_context *ac;
        struct ldb_request *down_req;
        const char **search_attrs = NULL;
-       int i, a, ret;
+       int i, a;
+       int ret;
 
-       req->handle = NULL;
+       ldb = ldb_module_get_ctx(module);
 
        ac = talloc(req, struct operational_context);
        if (ac == NULL) {
@@ -222,21 +242,10 @@ static int operational_search(struct ldb_module *module, struct ldb_request *req
        }
 
        ac->module = module;
-       ac->up_context = req->context;
-       ac->up_callback = req->callback;
+       ac->req = req;
        ac->attrs = req->op.search.attrs;
 
-       down_req = talloc_zero(req, struct ldb_request);
-       if (down_req == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       down_req->operation = req->operation;
-       down_req->op.search.base = req->op.search.base;
-       down_req->op.search.scope = req->op.search.scope;
-       down_req->op.search.tree = req->op.search.tree;
-
-       /*  FIXME: I hink we should copy the tree and keep the original
+       /*  FIXME: We must copy the tree and keep the original
         *  unmodified. SSS */
        /* replace any attributes in the parse tree that are
           searchable, but are stored using a different name in the
@@ -264,27 +273,22 @@ static int operational_search(struct ldb_module *module, struct ldb_request *req
                        }
                }
        }
-       
-       /* use new set of attrs if any */
-       if (search_attrs) down_req->op.search.attrs = search_attrs;
-       else down_req->op.search.attrs = req->op.search.attrs;
-       
-       down_req->controls = req->controls;
 
-       down_req->context = ac;
-       down_req->callback = operational_callback;
-       ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
-
-       /* perform the search */
-       ret = ldb_next_request(module, down_req);
-
-       /* do not free down_req as the call results may be linked to it,
-        * it will be freed when the upper level request get freed */
-       if (ret == LDB_SUCCESS) {
-               req->handle = down_req->handle;
+       ret = ldb_build_search_req_ex(&down_req, ldb, ac,
+                                       req->op.search.base,
+                                       req->op.search.scope,
+                                       req->op.search.tree,
+                                       /* use new set of attrs if any */
+                                       search_attrs == NULL?req->op.search.attrs:search_attrs,
+                                       req->controls,
+                                       ac, operational_callback,
+                                       req);
+       if (ret != LDB_SUCCESS) {
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       return ret;
+       /* perform the search */
+       return ldb_next_request(module, down_req);
 }
 
 static int operational_init(struct ldb_module *ctx)