subunit: Support formatting compatible with upstream subunit, for consistency.
[amitay/samba.git] / source4 / torture / rpc / dssync.c
index 67993bd8636a4929810025905f7bf2e0beb9d8ec..793ab14dbf044cf902225fccfb255475576aaf11 100644 (file)
 #include "lib/ldb_wrap.h"
 #include "torture/rpc/rpc.h"
 #include "torture/drs/proto.h"
-
+#include "lib/tsocket/tsocket.h"
+#include "libcli/resolve/resolve.h"
 
 struct DsSyncBindInfo {
        struct dcerpc_pipe *drs_pipe;
+       struct dcerpc_binding_handle *drs_handle;
        struct drsuapi_DsBind req;
        struct GUID bind_guid;
        struct drsuapi_DsBindInfoCtr our_bind_info_ctr;
@@ -54,7 +56,7 @@ struct DsSyncTest {
        
        const char *ldap_url;
        const char *site_name;
-       
+       const char *dest_address;
        const char *domain_dn;
 
        /* what we need to do as 'Administrator' */
@@ -86,6 +88,8 @@ static struct DsSyncTest *test_create_context(struct torture_context *tctx)
        struct drsuapi_DsBindInfo28 *our_bind_info28;
        struct drsuapi_DsBindInfoCtr *our_bind_info_ctr;
        const char *binding = torture_setting_string(tctx, "binding", NULL);
+       struct nbt_name name;
+
        ctx = talloc_zero(tctx, struct DsSyncTest);
        if (!ctx) return NULL;
 
@@ -98,6 +102,17 @@ static struct DsSyncTest *test_create_context(struct torture_context *tctx)
 
        ctx->ldap_url = talloc_asprintf(ctx, "ldap://%s", ctx->drsuapi_binding->host);
 
+       make_nbt_name_server(&name, ctx->drsuapi_binding->host);
+
+       /* do an initial name resolution to find its IP */
+       status = resolve_name(lp_resolve_context(tctx->lp_ctx), &name, tctx,
+                             &ctx->dest_address, tctx->ev);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("Failed to resolve %s - %s\n",
+                      name.name, nt_errstr(status));
+               return NULL;
+       }
+
        /* ctx->admin ...*/
        ctx->admin.credentials                          = cmdline_credentials;
 
@@ -189,8 +204,9 @@ static bool _test_DsBind(struct torture_context *tctx,
                printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
                return false;
        }
+       b->drs_handle = b->drs_pipe->binding_handle;
 
-       status = dcerpc_drsuapi_DsBind(b->drs_pipe, ctx, &b->req);
+       status = dcerpc_drsuapi_DsBind_r(b->drs_handle, ctx, &b->req);
        if (!NT_STATUS_IS_OK(status)) {
                const char *errstr = nt_errstr(status);
                if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
@@ -289,13 +305,26 @@ static bool test_GetInfo(struct torture_context *tctx, struct DsSyncTest *ctx)
        struct drsuapi_DsCrackNames r;
        union drsuapi_DsNameRequest req;
        union drsuapi_DsNameCtr ctr;
-       int32_t level_out = 0;
+       uint32_t level_out = 0;
        struct drsuapi_DsNameString names[1];
        bool ret = true;
        struct cldap_socket *cldap;
        struct cldap_netlogon search;
+       struct tsocket_address *dest_addr;
+       int ret2;
+
+       ret2 = tsocket_address_inet_from_strings(tctx, "ip",
+                                                ctx->dest_address,
+                                                lp_cldap_port(tctx->lp_ctx),
+                                                &dest_addr);
+       if (ret2 != 0) {
+               printf("failed to create tsocket_address for '%s' port %u - %s\n",
+                       ctx->drsuapi_binding->host, lp_cldap_port(tctx->lp_ctx),
+                       strerror(errno));
+               return false;
+       }
 
-       status = cldap_socket_init(ctx, NULL, NULL, NULL, &cldap);
+       status = cldap_socket_init(ctx, NULL, NULL, dest_addr, &cldap);
        if (!NT_STATUS_IS_OK(status)) {
                printf("failed to setup cldap socket - %s\n",
                        nt_errstr(status));
@@ -317,7 +346,7 @@ static bool test_GetInfo(struct torture_context *tctx, struct DsSyncTest *ctx)
        r.out.level_out                 = &level_out;
        r.out.ctr                       = &ctr;
 
-       status = dcerpc_drsuapi_DsCrackNames(ctx->admin.drsuapi.drs_pipe, ctx, &r);
+       status = dcerpc_drsuapi_DsCrackNames_r(ctx->admin.drsuapi.drs_handle, ctx, &r);
        if (!NT_STATUS_IS_OK(status)) {
                const char *errstr = nt_errstr(status);
                if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
@@ -333,8 +362,8 @@ static bool test_GetInfo(struct torture_context *tctx, struct DsSyncTest *ctx)
        ctx->domain_dn = r.out.ctr->ctr1->array[0].result_name;
        
        ZERO_STRUCT(search);
-       search.in.dest_address = ctx->drsuapi_binding->host;
-       search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
+       search.in.dest_address = NULL;
+       search.in.dest_port = 0;
        search.in.acct_control = -1;
        search.in.version               = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
        search.in.map_response = true;
@@ -379,7 +408,7 @@ static bool test_analyse_objects(struct torture_context *tctx,
        struct ldb_extended_dn_control *extended_dn_ctrl;
        const char *err_msg;
        
-       if (!dsdb_get_schema(ldb)) {
+       if (!dsdb_get_schema(ldb, NULL)) {
                struct dsdb_schema *ldap_schema;
                struct ldb_result *a_res;
                struct ldb_result *c_res;
@@ -561,7 +590,13 @@ static bool test_analyse_objects(struct torture_context *tctx,
                        ldif.changetype = LDB_CHANGETYPE_MODIFY;
                        ldif.msg = new_msg;
                        s = ldb_ldif_write_string(ldb, new_msg, &ldif);
-                       s = talloc_asprintf(tctx, "\n# Difference in between DRS and LDAP objects: %s\n", s);
+                       s = talloc_asprintf(tctx, "\n# Difference in between DRS and LDAP objects: \n%s\n", s);
+
+                       ldif.msg = ldb_msg_diff(ldb, ldap_msg, drs_msg);
+                       s = talloc_asprintf_append(s,
+                                                  "\n# Difference in between LDAP and DRS objects: \n%s\n",
+                                                  ldb_ldif_write_string(ldb, new_msg, &ldif));
+
                        s = talloc_asprintf_append(s,
                                                   "# Should have no objects in 'difference' message. Diff elements: %d",
                                                   new_msg->num_elements);
@@ -728,12 +763,12 @@ static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
        struct drsuapi_DsReplicaObjectIdentifier nc;
        struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
        struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
-       int32_t out_level = 0;
+       uint32_t out_level = 0;
        struct GUID null_guid;
        struct dom_sid null_sid;
        DATA_BLOB gensec_skey;
        struct {
-               int32_t level;
+               uint32_t level;
        } array[] = {
 /*             {
                        5
@@ -771,7 +806,7 @@ static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
        }
 
        for (i=0; i < ARRAY_SIZE(array); i++) {
-               printf("testing DsGetNCChanges level %d\n",
+               printf("Testing DsGetNCChanges level %d\n",
                        array[i].level);
 
                r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
@@ -793,15 +828,15 @@ static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
                        r.in.req->req5.uptodateness_vector              = NULL;
                        r.in.req->req5.replica_flags                    = 0;
                        if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
-                               r.in.req->req5.replica_flags            |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
+                               r.in.req->req5.replica_flags            |= DRSUAPI_DRS_USE_COMPRESSION;
                        }
                        if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
-                               r.in.req->req5.replica_flags            |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
+                               r.in.req->req5.replica_flags            |= DRSUAPI_DRS_WRIT_REP;
                        }
-                       r.in.req->req5.replica_flags                    |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
-                                                                       | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
-                                                                       | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
-                                                                       | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
+                       r.in.req->req5.replica_flags                    |= DRSUAPI_DRS_INIT_SYNC
+                                                                       | DRSUAPI_DRS_PER_SYNC
+                                                                       | DRSUAPI_DRS_GET_ANC
+                                                                       | DRSUAPI_DRS_NEVER_SYNCED
                                                                        ;
                        r.in.req->req5.max_object_count                 = 133;
                        r.in.req->req5.max_ndr_size                     = 1336770;
@@ -825,15 +860,15 @@ static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
                        r.in.req->req8.uptodateness_vector              = NULL;
                        r.in.req->req8.replica_flags                    = 0;
                        if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
-                               r.in.req->req8.replica_flags            |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
+                               r.in.req->req8.replica_flags            |= DRSUAPI_DRS_USE_COMPRESSION;
                        }
                        if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
-                               r.in.req->req8.replica_flags            |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
+                               r.in.req->req8.replica_flags            |= DRSUAPI_DRS_WRIT_REP;
                        }
-                       r.in.req->req8.replica_flags                    |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
-                                                                       | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
-                                                                       | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
-                                                                       | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
+                       r.in.req->req8.replica_flags                    |= DRSUAPI_DRS_INIT_SYNC
+                                                                       | DRSUAPI_DRS_PER_SYNC
+                                                                       | DRSUAPI_DRS_GET_ANC
+                                                                       | DRSUAPI_DRS_NEVER_SYNCED
                                                                        ;
                        r.in.req->req8.max_object_count                 = 402;
                        r.in.req->req8.max_ndr_size                     = 402116;
@@ -850,7 +885,7 @@ static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
                
                printf("Dumping AD partition: %s\n", nc.dn);
                for (y=0; ;y++) {
-                       int32_t _level = 0;
+                       uint32_t _level = 0;
                        union drsuapi_DsGetNCChangesCtr ctr;
 
                        ZERO_STRUCT(r.out);
@@ -874,7 +909,7 @@ static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
                                                r.in.req->req8.highwatermark.highest_usn);
                        }
 
-                       status = dcerpc_drsuapi_DsGetNCChanges(ctx->new_dc.drsuapi.drs_pipe, ctx, &r);
+                       status = dcerpc_drsuapi_DsGetNCChanges_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
                        torture_drsuapi_assert_call(tctx, ctx->new_dc.drsuapi.drs_pipe, status,
                                                    &r, "dcerpc_drsuapi_DsGetNCChanges");
 
@@ -969,22 +1004,31 @@ static bool test_FetchNT4Data(struct torture_context *tctx,
        r.out.info              = &info;
        r.out.level_out         = &level_out;
 
-       req.req1.unknown1       = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "nt4-1", 3);
-       req.req1.unknown2       = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "nt4-2", 0x00004000);
+       req.req1.flags = lp_parm_int(tctx->lp_ctx, NULL,
+                                    "dssync", "nt4changelog_flags",
+                                    DRSUAPI_NT4_CHANGELOG_GET_CHANGELOG |
+                                    DRSUAPI_NT4_CHANGELOG_GET_SERIAL_NUMBERS);
+       req.req1.preferred_maximum_length = lp_parm_int(tctx->lp_ctx, NULL,
+                                       "dssync", "nt4changelog_preferred_len",
+                                       0x00004000);
 
        while (1) {
-               req.req1.length = cookie.length;
-               req.req1.data   = cookie.data;
+               req.req1.restart_length = cookie.length;
+               req.req1.restart_data = cookie.data;
 
                r.in.req = &req;
 
-               status = dcerpc_drsuapi_DsGetNT4ChangeLog(ctx->new_dc.drsuapi.drs_pipe, ctx, &r);
+               status = dcerpc_drsuapi_DsGetNT4ChangeLog_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
                if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
                        torture_skip(tctx, "DsGetNT4ChangeLog not supported by target server");
                } else if (!NT_STATUS_IS_OK(status)) {
                        const char *errstr = nt_errstr(status);
                        if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
-                               errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.drs_pipe->last_fault_code);
+                               struct dcerpc_pipe *p = ctx->new_dc.drsuapi.drs_pipe;
+                               if (p->last_fault_code == DCERPC_FAULT_OP_RNG_ERROR) {
+                                       torture_skip(tctx, "DsGetNT4ChangeLog not supported by target server");
+                               }
+                               errstr = dcerpc_errstr(ctx, p->last_fault_code);
                        }
                        torture_fail(tctx,
                                     talloc_asprintf(tctx, "dcerpc_drsuapi_DsGetNT4ChangeLog failed - %s\n",
@@ -1001,8 +1045,8 @@ static bool test_FetchNT4Data(struct torture_context *tctx,
                                                     *r.out.level_out));
                } else if (NT_STATUS_IS_OK(r.out.info->info1.status)) {
                } else if (NT_STATUS_EQUAL(r.out.info->info1.status, STATUS_MORE_ENTRIES)) {
-                       cookie.length   = r.out.info->info1.length1;
-                       cookie.data     = r.out.info->info1.data1;
+                       cookie.length   = r.out.info->info1.restart_length;
+                       cookie.data     = r.out.info->info1.restart_data;
                        continue;
                } else {
                        torture_fail(tctx,
@@ -1016,30 +1060,74 @@ static bool test_FetchNT4Data(struct torture_context *tctx,
        return true;
 }
 
-bool torture_rpc_dssync(struct torture_context *torture)
+/**
+ * DSSYNC test case setup
+ */
+static bool torture_dssync_tcase_setup(struct torture_context *tctx, void **data)
 {
-       bool ret = true;
-       TALLOC_CTX *mem_ctx;
+       bool bret;
        struct DsSyncTest *ctx;
-       
-       mem_ctx = talloc_init("torture_rpc_dssync");
-       ctx = test_create_context(torture);
-       
-       ret &= _test_DsBind(torture, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
-       if (!ret) {
-               return ret;
-       }
-       ret &= test_LDAPBind(torture, ctx, ctx->admin.credentials, &ctx->admin.ldap);
-       if (!ret) {
-               return ret;
-       }
-       ret &= test_GetInfo(torture, ctx);
-       ret &= _test_DsBind(torture, ctx, ctx->new_dc.credentials, &ctx->new_dc.drsuapi);
-       if (!ret) {
-               return ret;
-       }
-       ret &= test_FetchData(torture, ctx);
-       ret &= test_FetchNT4Data(torture, ctx);
 
-       return ret;
+       *data = ctx = test_create_context(tctx);
+       torture_assert(tctx, ctx, "test_create_context() failed");
+
+       bret = _test_DsBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
+       torture_assert(tctx, bret, "_test_DsBind() failed");
+
+       bret = test_LDAPBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.ldap);
+       torture_assert(tctx, bret, "test_LDAPBind() failed");
+
+       bret = test_GetInfo(tctx, ctx);
+       torture_assert(tctx, bret, "test_GetInfo() failed");
+
+       bret = _test_DsBind(tctx, ctx, ctx->new_dc.credentials, &ctx->new_dc.drsuapi);
+       torture_assert(tctx, bret, "_test_DsBind() failed");
+
+       return true;
+}
+
+/**
+ * DSSYNC test case cleanup
+ */
+static bool torture_dssync_tcase_teardown(struct torture_context *tctx, void *data)
+{
+       struct DsSyncTest *ctx;
+       struct drsuapi_DsUnbind r;
+       struct policy_handle bind_handle;
+
+       ctx = talloc_get_type(data, struct DsSyncTest);
+
+       ZERO_STRUCT(r);
+       r.out.bind_handle = &bind_handle;
+
+       /* Unbing admin handle */
+       r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
+       dcerpc_drsuapi_DsUnbind_r(ctx->admin.drsuapi.drs_handle, ctx, &r);
+
+       /* Unbing new_dc handle */
+       r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
+       dcerpc_drsuapi_DsUnbind_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
+
+       talloc_free(ctx);
+
+       return true;
+}
+
+/**
+ * DSSYNC test case implementation
+ */
+void torture_drs_rpc_dssync_tcase(struct torture_suite *suite)
+{
+       typedef bool (*run_func) (struct torture_context *test, void *tcase_data);
+
+       struct torture_test *test;
+       struct torture_tcase *tcase = torture_suite_add_tcase(suite, "DSSYNC");
+
+       torture_tcase_set_fixture(tcase,
+                                 torture_dssync_tcase_setup,
+                                 torture_dssync_tcase_teardown);
+
+       test = torture_tcase_add_simple_test(tcase, "DC_FetchData", (run_func)test_FetchData);
+       test = torture_tcase_add_simple_test(tcase, "FetchNT4Data", (run_func)test_FetchNT4Data);
 }
+