s4:dsdb/schema Simplify schema loading from ldb messages
[samba.git] / source4 / torture / ldap / schema.c
index 8437e7f79db13af5652b04815df1e48fa7b8eae8..c9423409a8675bba0a594a9b79d2a46a56538a69 100644 (file)
 #include "libcli/ldap/ldap_client.h"
 #include "lib/cmdline/popt_common.h"
 #include "ldb_wrap.h"
-#include "lib/ldb/include/ldb.h"
-#include "lib/ldb/include/ldb_errors.h"
 #include "dsdb/samdb/samdb.h"
-#include "lib/util/dlinklist.h"
+#include "../lib/util/dlinklist.h"
 
 #include "torture/torture.h"
-#include "torture/ldap/proto.h"
 
-#include "param/param.h"
 
 struct test_rootDSE {
        const char *defaultdn;
@@ -41,6 +37,8 @@ struct test_rootDSE {
 };
 
 struct test_schema_ctx {
+       struct ldb_context *ldb;
+
        struct ldb_paged_control *ctrl;
        uint32_t count;
        bool pending;
@@ -57,8 +55,8 @@ static bool test_search_rootDSE(struct ldb_context *ldb, struct test_rootDSE *ro
 
        d_printf("Testing RootDSE Search\n");
 
-       ret = ldb_search(ldb, ldb_dn_new(ldb, ldb, NULL), LDB_SCOPE_BASE, 
-                        NULL, NULL, &r);
+       ret = ldb_search(ldb, ldb, &r, ldb_dn_new(ldb, ldb, NULL),
+                        LDB_SCOPE_BASE, NULL, NULL);
        if (ret != LDB_SUCCESS) {
                return false;
        } else if (r->count != 1) {
@@ -82,15 +80,24 @@ static bool test_search_rootDSE(struct ldb_context *ldb, struct test_rootDSE *ro
        return true;
 }
 
-static int test_schema_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
+static int test_schema_search_callback(struct ldb_request *req, struct ldb_reply *ares)
 {
-       struct test_schema_ctx *actx = talloc_get_type(context, struct test_schema_ctx);
+       struct test_schema_ctx *actx;
        int ret = LDB_SUCCESS;
 
+       actx = talloc_get_type(req->context, struct test_schema_ctx);
+
+       if (!ares) {
+               return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
+       }
+       if (ares->error != LDB_SUCCESS) {
+               return ldb_request_done(req, ares->error);
+       }
+
        switch (ares->type) {
        case LDB_REPLY_ENTRY:
                actx->count++;
-               ret = actx->callback(actx->private_data, ldb, ares->message);
+               ret = actx->callback(actx->private_data, actx->ldb, ares->message);
                break;
 
        case LDB_REPLY_REFERRAL:
@@ -118,21 +125,22 @@ static int test_schema_search_callback(struct ldb_context *ldb, void *context, s
                                actx->pending = true;
                        }
                }
-               break;
-               
+               talloc_free(ares);
+               return ldb_request_done(req, LDB_SUCCESS);
+
        default:
                d_printf("%s: unknown Reply Type %u\n", __location__, ares->type);
-               return LDB_ERR_OTHER;
+               return ldb_request_done(req, LDB_ERR_OTHER);
        }
 
        if (talloc_free(ares) == -1) {
                d_printf("talloc_free failed\n");
                actx->pending = 0;
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
        }
 
        if (ret) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
        }
 
        return LDB_SUCCESS;
@@ -149,10 +157,12 @@ static bool test_create_schema_type(struct ldb_context *ldb, struct test_rootDSE
        int ret;
        struct test_schema_ctx *actx;
 
-       req = talloc(ldb, struct ldb_request);
-       actx = talloc(req, struct test_schema_ctx);
+       actx = talloc(ldb, struct test_schema_ctx);
+       actx->ldb = ldb;
+       actx->private_data = private_data;
+       actx->callback= callback;
 
-       ctrl = talloc_array(req, struct ldb_control *, 2);
+       ctrl = talloc_array(actx, struct ldb_control *, 2);
        ctrl[0] = talloc(ctrl, struct ldb_control);
        ctrl[0]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
        ctrl[0]->critical = true;
@@ -163,33 +173,30 @@ static bool test_create_schema_type(struct ldb_context *ldb, struct test_rootDSE
        ctrl[0]->data = control;
        ctrl[1] = NULL;
 
-       req->operation = LDB_SEARCH;
-       req->op.search.base = ldb_dn_new(req, ldb, root->schemadn);
-       req->op.search.scope = LDB_SCOPE_SUBTREE;
-       req->op.search.tree = ldb_parse_tree(req, filter);
-       if (req->op.search.tree == NULL) return -1;
-       req->op.search.attrs = NULL;
-       req->controls = ctrl;
-       req->context = actx;
-       req->callback = test_schema_search_callback;
-       ldb_set_timeout(ldb, req, 0);
-
-       actx->count             = 0;
-       actx->ctrl              = control;
-       actx->callback          = callback;
-       actx->private_data      = private_data;
+       ret = ldb_build_search_req(&req, ldb, actx,
+                                  ldb_dn_new(actx, ldb, root->schemadn),
+                                  LDB_SCOPE_SUBTREE,
+                                  filter, NULL,
+                                  ctrl,
+                                  actx, test_schema_search_callback,
+                                  NULL);
+
+       actx->ctrl = control;
+       actx->count = 0;
 again:
        actx->pending           = false;
 
        ret = ldb_request(ldb, req);
        if (ret != LDB_SUCCESS) {
                d_printf("search failed - %s\n", ldb_errstring(ldb));
+               talloc_free(actx);
                return false;
        }
 
        ret = ldb_wait(req->handle, LDB_WAIT_ALL);
                if (ret != LDB_SUCCESS) {
                d_printf("search error - %s\n", ldb_errstring(ldb));
+               talloc_free(actx);
                return false;
        }
 
@@ -197,49 +204,35 @@ again:
                goto again;
 
        d_printf("filter[%s] count[%u]\n", filter, actx->count);
+       talloc_free(actx);
        return true;
 }
 
 static int test_add_attribute(void *ptr, struct ldb_context *ldb, struct ldb_message *msg)
 {
        struct dsdb_schema *schema = talloc_get_type(ptr, struct dsdb_schema);
-       struct dsdb_attribute *attr = NULL;
        WERROR status;
 
-       attr = talloc_zero(schema, struct dsdb_attribute);
-       if (!attr) {
-               goto failed;
-       }
-
-       status = dsdb_attribute_from_ldb(schema, msg, attr, attr);
+       status = dsdb_attribute_from_ldb(ldb, schema, msg);
        if (!W_ERROR_IS_OK(status)) {
                goto failed;
        }
 
-       DLIST_ADD_END(schema->attributes, attr, struct dsdb_attribute *);
        return LDB_SUCCESS;
 failed:
-       talloc_free(attr);
        return LDB_ERR_OTHER;
 }
 
 static int test_add_class(void *ptr, struct ldb_context *ldb, struct ldb_message *msg)
 {
        struct dsdb_schema *schema = talloc_get_type(ptr, struct dsdb_schema);
-       struct dsdb_class *obj;
        WERROR status;
 
-       obj = talloc_zero(schema, struct dsdb_class);
-       if (!obj) {
-               goto failed;
-       }
-
-       status = dsdb_class_from_ldb(schema, msg, obj, obj);
+       status = dsdb_class_from_ldb(schema, msg);
        if (!W_ERROR_IS_OK(status)) {
                goto failed;
        }
 
-       DLIST_ADD_END(schema->classes, obj, struct dsdb_class *);
        return LDB_SUCCESS;
 failed:
        return LDB_ERR_OTHER;
@@ -348,7 +341,7 @@ static bool test_dump_sorted_syntax(struct ldb_context *ldb, struct test_rootDSE
 
                        if (strcmp(syntaxes[i], a->attributeSyntax_oid) != 0) continue;
 
-                       om_hex = data_blob_hex_string(ldb, &a->oMObjectClass);
+                       om_hex = data_blob_hex_string_upper(ldb, &a->oMObjectClass);
                        if (!om_hex) {
                                return false;
                        }
@@ -379,7 +372,7 @@ bool torture_ldap_schema(struct torture_context *torture)
        ldb = ldb_wrap_connect(torture, torture->ev, torture->lp_ctx, url,
                               NULL,
                               cmdline_credentials,
-                              0, NULL);
+                              0);
        if (!ldb) goto failed;
 
        ret &= test_search_rootDSE(ldb, &rootDSE);