s4:dsdb/schema Simplify schema loading from ldb messages
[samba.git] / source4 / torture / ldap / schema.c
index 60b27f6c7243f1bfe3bcbc1f09424e88e536fc46..c9423409a8675bba0a594a9b79d2a46a56538a69 100644 (file)
@@ -6,7 +6,7 @@
     
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
 */
 
 #include "includes.h"
 #include "libcli/ldap/ldap_client.h"
 #include "lib/cmdline/popt_common.h"
-#include "db_wrap.h"
-#include "lib/ldb/include/ldb.h"
-#include "lib/ldb/include/ldb_errors.h"
+#include "ldb_wrap.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"
+
 
 struct test_rootDSE {
        const char *defaultdn;
@@ -40,15 +37,17 @@ struct test_rootDSE {
 };
 
 struct test_schema_ctx {
+       struct ldb_context *ldb;
+
        struct ldb_paged_control *ctrl;
        uint32_t count;
-       BOOL pending;
+       bool pending;
 
        int (*callback)(void *, struct ldb_context *ldb, struct ldb_message *);
        void *private_data;
 };
 
-static BOOL test_search_rootDSE(struct ldb_context *ldb, struct test_rootDSE *root)
+static bool test_search_rootDSE(struct ldb_context *ldb, struct test_rootDSE *root)
 {
        int ret;
        struct ldb_message *msg;
@@ -56,13 +55,13 @@ 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;
+               return false;
        } else if (r->count != 1) {
                talloc_free(r);
-               return False;
+               return false;
        }
 
        msg = r->msgs[0];
@@ -78,18 +77,27 @@ static BOOL test_search_rootDSE(struct ldb_context *ldb, struct test_rootDSE *ro
 
        talloc_free(r);
 
-       return True;
+       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:
@@ -114,30 +122,31 @@ static int test_schema_search_callback(struct ldb_context *ldb, void *context, s
                        actx->ctrl->cookie_len = ctrl->cookie_len;
 
                        if (actx->ctrl->cookie_len > 0) {
-                               actx->pending = True;
+                               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;
 }
 
-static BOOL test_create_schema_type(struct ldb_context *ldb, struct test_rootDSE *root,
+static bool test_create_schema_type(struct ldb_context *ldb, struct test_rootDSE *root,
                                    const char *filter,
                                    int (*callback)(void *, struct ldb_context *ldb, struct ldb_message *),
                                    void *private_data)
@@ -148,13 +157,15 @@ 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;
+       ctrl[0]->critical = true;
        control = talloc(ctrl[0], struct ldb_paged_control);
        control->size = 1000;
        control->cookie = NULL;
@@ -162,91 +173,74 @@ 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;
+       actx->pending           = false;
 
        ret = ldb_request(ldb, req);
        if (ret != LDB_SUCCESS) {
                d_printf("search failed - %s\n", ldb_errstring(ldb));
-               return False;
+               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));
-               return False;
+               talloc_free(actx);
+               return false;
        }
 
        if (actx->pending)
                goto again;
 
        d_printf("filter[%s] count[%u]\n", filter, actx->count);
-       return True;
+       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(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(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;
 }
 
-static BOOL test_create_schema(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema **_schema)
+static bool test_create_schema(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema **_schema)
 {
-       BOOL ret = True;
+       bool ret = true;
        struct dsdb_schema *schema;
 
        schema = talloc_zero(ldb, struct dsdb_schema);
@@ -258,13 +252,13 @@ static BOOL test_create_schema(struct ldb_context *ldb, struct test_rootDSE *roo
        ret &= test_create_schema_type(ldb, root, "(objectClass=classSchema)",
                                       test_add_class, schema);
 
-       if (ret == True) {
+       if (ret == true) {
                *_schema = schema;
        }
        return ret;
 }
 
-static BOOL test_dump_not_replicated(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
+static bool test_dump_not_replicated(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
 {
        struct dsdb_attribute *a;
        uint32_t a_i = 1;
@@ -277,10 +271,10 @@ static BOOL test_dump_not_replicated(struct ldb_context *ldb, struct test_rootDS
                         a->lDAPDisplayName);
        }
 
-       return True;
+       return true;
 }
 
-static BOOL test_dump_partial(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
+static bool test_dump_partial(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
 {
        struct dsdb_attribute *a;
        uint32_t a_i = 1;
@@ -294,10 +288,10 @@ static BOOL test_dump_partial(struct ldb_context *ldb, struct test_rootDSE *root
                         a->lDAPDisplayName);
        }
 
-       return True;
+       return true;
 }
 
-static BOOL test_dump_contructed(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
+static bool test_dump_contructed(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
 {
        struct dsdb_attribute *a;
        uint32_t a_i = 1;
@@ -310,10 +304,10 @@ static BOOL test_dump_contructed(struct ldb_context *ldb, struct test_rootDSE *r
                         a->lDAPDisplayName);
        }
 
-       return True;
+       return true;
 }
 
-static BOOL test_dump_sorted_syntax(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
+static bool test_dump_sorted_syntax(struct ldb_context *ldb, struct test_rootDSE *root, struct dsdb_schema *schema)
 {
        struct dsdb_attribute *a;
        uint32_t a_i = 1;
@@ -339,103 +333,33 @@ static BOOL test_dump_sorted_syntax(struct ldb_context *ldb, struct test_rootDSE
                "2.5.5.17"
        };
 
+       d_printf("Dumping attribute syntaxes\n");
+
        for (i=0; i < ARRAY_SIZE(syntaxes); i++) {
                for (a=schema->attributes; a; a = a->next) {
-                       if (strcmp(syntaxes[i], a->attributeSyntax_oid) != 0) continue;
-                       d_printf("attr[%4u]: %s %u '%s'\n", a_i++,
-                                a->attributeSyntax_oid, a->oMSyntax,
-                                a->lDAPDisplayName);
-               }
-       }
-
-       return True;
-}
-
+                       char *om_hex;
 
-static BOOL test_dsdb_map(struct torture_context *torture)
-{
-       BOOL ret = true;
-       WERROR status;
-       const char *oid;
-       uint32_t id;
-
-       oid = "1.2.840.113556.1.4.1716";
-       status = dsdb_map_oid2int(oid, &id);
-       if (!W_ERROR_IS_OK(status)) {
-               DEBUG(0,("%s => %s\n", oid, win_errstr(status)));
-               ret = False;
-       } else {
-               DEBUG(0,("%s => 0x%08X\n", oid, id));
-       }
-
-       status = dsdb_map_int2oid(id, torture, &oid);
-       if (!W_ERROR_IS_OK(status)) {
-               DEBUG(0,("0x%08X => %s\n", id, win_errstr(status)));
-               ret = False;
-       } else {
-               DEBUG(0,("0x%08X => %s\n", id, oid));
-       }
-
-       oid = "1.2.840.113556.1.4.65535";
-       status = dsdb_map_oid2int(oid, &id);
-       if (!W_ERROR_IS_OK(status)) {
-               DEBUG(0,("%s => %s\n", oid, win_errstr(status)));
-               ret = False;
-       } else {
-               DEBUG(0,("%s => 0x%08X\n", oid, id));
-       }
-
-       status = dsdb_map_int2oid(id, torture, &oid);
-       if (!W_ERROR_IS_OK(status)) {
-               DEBUG(0,("0x%08X => %s\n", id, win_errstr(status)));
-               ret = False;
-       } else {
-               DEBUG(0,("0x%08X => %s\n", id, oid));
-       }
-
-       oid = "1.2.840.113556.1.4.1716.";
-       status = dsdb_map_oid2int(oid, &id);
-       if (!W_ERROR_EQUAL(status, WERR_INVALID_PARAM)) {
-               DEBUG(0,("%s => %s\n", oid, win_errstr(status)));
-               ret = False;
-       } else {
-               DEBUG(0,("%s => %s (ok!)\n", oid, win_errstr(status)));
-       }
-
-       oid = "1.2.840.113556.1.4.1716.65536";
-       status = dsdb_map_oid2int(oid, &id);
-       if (!W_ERROR_EQUAL(status, WERR_INVALID_PARAM)) {
-               DEBUG(0,("%s => %s\n", oid, win_errstr(status)));
-               ret = False;
-       } else {
-               DEBUG(0,("%s => %s (ok!)\n", oid, win_errstr(status)));
-       }
+                       if (strcmp(syntaxes[i], a->attributeSyntax_oid) != 0) continue;
 
-       oid = "5435.1.2.840.113556.1.4.1716.";
-       status = dsdb_map_oid2int(oid, &id);
-       if (!W_ERROR_EQUAL(status, WERR_DS_NO_MSDS_INTID)) {
-               DEBUG(0,("%s => %s\n", oid, win_errstr(status)));
-               ret = False;
-       } else {
-               DEBUG(0,("%s => %s (ok!)\n", oid, win_errstr(status)));
-       }
+                       om_hex = data_blob_hex_string_upper(ldb, &a->oMObjectClass);
+                       if (!om_hex) {
+                               return false;
+                       }
 
-       id = 0xEF001234;
-       status = dsdb_map_int2oid(id, torture, &oid);
-       if (!W_ERROR_EQUAL(status, WERR_DS_NO_MSDS_INTID)) {
-               DEBUG(0,("0x%08X => %s\n", id, win_errstr(status)));
-               ret = False;
-       } else {
-               DEBUG(0,("0x%08X => %s (ok!)\n", id, win_errstr(status)));
+                       d_printf("attr[%4u]: %s %u '%s' '%s'\n", a_i++,
+                                a->attributeSyntax_oid, a->oMSyntax,
+                                om_hex, a->lDAPDisplayName);
+                       talloc_free(om_hex);
+               }
        }
 
-       return ret;
+       return true;
 }
 
-BOOL torture_ldap_schema(struct torture_context *torture)
+bool torture_ldap_schema(struct torture_context *torture)
 {
        struct ldb_context *ldb;
-       BOOL ret = True;
+       bool ret = true;
        const char *host = torture_setting_string(torture, "host", NULL);
        char *url;
        struct test_rootDSE rootDSE;
@@ -445,10 +369,10 @@ BOOL torture_ldap_schema(struct torture_context *torture)
 
        url = talloc_asprintf(torture, "ldap://%s/", host);
 
-       ldb = ldb_wrap_connect(torture, url,
+       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);
@@ -461,8 +385,6 @@ BOOL torture_ldap_schema(struct torture_context *torture)
        ret &= test_dump_contructed(ldb, &rootDSE, schema);
        ret &= test_dump_sorted_syntax(ldb, &rootDSE, schema);
 
-       ret &= test_dsdb_map(torture);
-
 failed:
        return ret;
 }