Use the cldap reply to avoid segfaulting in RPC-DSSYNC
[metze/samba/wip.git] / source / torture / rpc / dssync.c
index 3d9accf4fe44088ccb613deee477fefc0ecff95d..e8c67b46c107c1d60807874a5d46eb6b4fed3040 100644 (file)
@@ -8,7 +8,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 "lib/cmdline/popt_common.h"
 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
+#include "librpc/gen_ndr/ndr_drsblobs.h"
 #include "libcli/cldap/cldap.h"
 #include "libcli/ldap/ldap_client.h"
 #include "torture/torture.h"
@@ -33,6 +33,8 @@
 #include "auth/credentials/credentials.h"
 #include "libcli/auth/libcli_auth.h"
 #include "auth/gensec/gensec.h"
+#include "param/param.h"
+#include "dsdb/samdb/samdb.h"
 
 struct DsSyncBindInfo {
        struct dcerpc_pipe *pipe;
@@ -78,14 +80,14 @@ struct DsSyncTest {
        } old_dc;
 };
 
-static struct DsSyncTest *test_create_context(TALLOC_CTX *mem_ctx)
+static struct DsSyncTest *test_create_context(struct torture_context *tctx)
 {
        NTSTATUS status;
        struct DsSyncTest *ctx;
        struct drsuapi_DsBindInfo28 *our_bind_info28;
        struct drsuapi_DsBindInfoCtr *our_bind_info_ctr;
-       const char *binding = lp_parm_string(-1, "torture", "binding");
-       ctx = talloc_zero(mem_ctx, struct DsSyncTest);
+       const char *binding = torture_setting_string(tctx, "binding", NULL);
+       ctx = talloc_zero(tctx, struct DsSyncTest);
        if (!ctx) return NULL;
 
        status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
@@ -104,7 +106,7 @@ static struct DsSyncTest *test_create_context(TALLOC_CTX *mem_ctx)
        our_bind_info28->supported_extensions   = 0xFFFFFFFF;
        our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
        our_bind_info28->site_guid              = GUID_zero();
-       our_bind_info28->u1                     = 0;
+       our_bind_info28->pid                    = 0;
        our_bind_info28->repl_epoch             = 1;
 
        our_bind_info_ctr                       = &ctx->admin.drsuapi.our_bind_info_ctr;
@@ -149,11 +151,11 @@ static struct DsSyncTest *test_create_context(TALLOC_CTX *mem_ctx)
        our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
        our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
        our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
-       if (lp_parm_bool(-1,"dssync","xpress",False)) {
+       if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "xpress", false)) {
                our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
        }
        our_bind_info28->site_guid              = GUID_zero();
-       our_bind_info28->u1                     = 508;
+       our_bind_info28->pid                    = 0;
        our_bind_info28->repl_epoch             = 0;
 
        our_bind_info_ctr                       = &ctx->new_dc.drsuapi.our_bind_info_ctr;
@@ -173,20 +175,20 @@ static struct DsSyncTest *test_create_context(TALLOC_CTX *mem_ctx)
        return ctx;
 }
 
-static BOOL _test_DsBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
+static bool _test_DsBind(struct torture_context *tctx,
+                        struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
 {
        NTSTATUS status;
-       BOOL ret = True;
-       struct event_context *event = NULL;
+       bool ret = true;
 
        status = dcerpc_pipe_connect_b(ctx,
                                       &b->pipe, ctx->drsuapi_binding, 
-                                      &dcerpc_table_drsuapi,
-                                      credentials, event);
+                                      &ndr_table_drsuapi,
+                                      credentials, tctx->ev, tctx->lp_ctx);
        
        if (!NT_STATUS_IS_OK(status)) {
                printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
-               return False;
+               return false;
        }
 
        status = dcerpc_drsuapi_DsBind(b->pipe, ctx, &b->req);
@@ -196,10 +198,10 @@ static BOOL _test_DsBind(struct DsSyncTest *ctx, struct cli_credentials *credent
                        errstr = dcerpc_errstr(ctx, b->pipe->last_fault_code);
                }
                printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
-               ret = False;
+               ret = false;
        } else if (!W_ERROR_IS_OK(b->req.out.result)) {
                printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
-               ret = False;
+               ret = false;
        }
 
        ZERO_STRUCT(b->peer_bind_info28);
@@ -210,52 +212,66 @@ static BOOL _test_DsBind(struct DsSyncTest *ctx, struct cli_credentials *credent
                        info24 = &b->req.out.bind_info->info.info24;
                        b->peer_bind_info28.supported_extensions= info24->supported_extensions;
                        b->peer_bind_info28.site_guid           = info24->site_guid;
-                       b->peer_bind_info28.u1                  = info24->u1;
+                       b->peer_bind_info28.pid                 = info24->pid;
                        b->peer_bind_info28.repl_epoch          = 0;
                        break;
                }
+               case 48: {
+                       struct drsuapi_DsBindInfo48 *info48;
+                       info48 = &b->req.out.bind_info->info.info48;
+                       b->peer_bind_info28.supported_extensions= info48->supported_extensions;
+                       b->peer_bind_info28.site_guid           = info48->site_guid;
+                       b->peer_bind_info28.pid                 = info48->pid;
+                       b->peer_bind_info28.repl_epoch          = info48->repl_epoch;
+                       break;
+               }
                case 28:
                        b->peer_bind_info28 = b->req.out.bind_info->info.info28;
                        break;
+               default:
+                       printf("DsBind - warning: unknown BindInfo length: %u\n",
+                              b->req.out.bind_info->length);
                }
        }
 
        return ret;
 }
 
-static BOOL test_LDAPBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
+static bool test_LDAPBind(struct torture_context *tctx, struct DsSyncTest *ctx, 
+                         struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
 {
        NTSTATUS status;
-       BOOL ret = True;
+       bool ret = true;
 
-       status = torture_ldap_connection(ctx, &l->conn, ctx->ldap_url);
+       status = torture_ldap_connection(tctx, &l->conn, ctx->ldap_url);
        if (!NT_STATUS_IS_OK(status)) {
                printf("failed to connect to LDAP: %s\n", ctx->ldap_url);
-               return False;
+               return false;
        }
 
        printf("connected to LDAP: %s\n", ctx->ldap_url);
 
-       status = torture_ldap_bind_sasl(l->conn, credentials);
+       status = torture_ldap_bind_sasl(l->conn, credentials, tctx->lp_ctx);
        if (!NT_STATUS_IS_OK(status)) {
                printf("failed to bind to LDAP:\n");
-               return False;
+               return false;
        }
        printf("bound to LDAP.\n");
 
        return ret;
 }
 
-static BOOL test_GetInfo(struct DsSyncTest *ctx)
+static bool test_GetInfo(struct torture_context *tctx, struct DsSyncTest *ctx)
 {
        NTSTATUS status;
        struct drsuapi_DsCrackNames r;
        struct drsuapi_DsNameString names[1];
-       BOOL ret = True;
-
-       struct cldap_socket *cldap = cldap_socket_init(ctx, NULL);
+       bool ret = true;
+       struct cldap_socket *cldap;
        struct cldap_netlogon search;
-       
+
+       cldap = cldap_socket_init(ctx, tctx->ev, lp_iconv_convenience(tctx->lp_ctx));
+
        r.in.bind_handle                = &ctx->admin.drsuapi.bind_handle;
        r.in.level                      = 1;
        r.in.req.req1.codepage          = 1252; /* western european */
@@ -265,7 +281,7 @@ static BOOL test_GetInfo(struct DsSyncTest *ctx)
        r.in.req.req1.format_flags      = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;                
        r.in.req.req1.format_offered    = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
        r.in.req.req1.format_desired    = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
-       names[0].str = talloc_asprintf(ctx, "%s\\", lp_workgroup());
+       names[0].str = talloc_asprintf(ctx, "%s\\", lp_workgroup(tctx->lp_ctx));
 
        status = dcerpc_drsuapi_DsCrackNames(ctx->admin.drsuapi.pipe, ctx, &r);
        if (!NT_STATUS_IS_OK(status)) {
@@ -274,27 +290,37 @@ static BOOL test_GetInfo(struct DsSyncTest *ctx)
                        errstr = dcerpc_errstr(ctx, ctx->admin.drsuapi.pipe->last_fault_code);
                }
                printf("dcerpc_drsuapi_DsCrackNames failed - %s\n", errstr);
-               return False;
+               return false;
        } else if (!W_ERROR_IS_OK(r.out.result)) {
                printf("DsCrackNames failed - %s\n", win_errstr(r.out.result));
-               return False;
+               return false;
        }
 
        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.acct_control = -1;
-       search.in.version = 6;
+       search.in.version               = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
+       search.in.map_response = true;
        status = cldap_netlogon(cldap, ctx, &search);
        if (!NT_STATUS_IS_OK(status)) {
                const char *errstr = nt_errstr(status);
                ctx->site_name = talloc_asprintf(ctx, "%s", "Default-First-Site-Name");
                printf("cldap_netlogon() returned %s. Defaulting to Site-Name: %s\n", errstr, ctx->site_name);          
        } else {
-               ctx->site_name = talloc_steal(ctx, search.out.netlogon.logon5.client_site);
+               ctx->site_name = talloc_steal(ctx, search.out.netlogon.nt5_ex.client_site);
                printf("cldap_netlogon() returned Client Site-Name: %s.\n",ctx->site_name);
-               printf("cldap_netlogon() returned Server Site-Name: %s.\n",search.out.netlogon.logon5.server_site);
+               printf("cldap_netlogon() returned Server Site-Name: %s.\n",search.out.netlogon.nt5_ex.server_site);
+       }
+
+       if (!ctx->domain_dn) {
+               struct ldb_context *ldb = ldb_init(ctx, tctx->ev);
+               struct ldb_dn *dn = samdb_dns_domain_to_dn(ldb, ctx, search.out.netlogon.nt5_ex.dns_domain);
+               ctx->domain_dn = ldb_dn_alloc_linearized(ctx, dn);
+               talloc_free(dn);
+               talloc_free(ldb);
        }
 
        return ret;
@@ -413,24 +439,25 @@ static DATA_BLOB decrypt_blob(TALLOC_CTX *mem_ctx,
        return plain_buffer;
 }
 
-static void test_analyse_objects(struct DsSyncTest *ctx,
+static void test_analyse_objects(struct torture_context *tctx, 
+                                struct DsSyncTest *ctx,
                                 const DATA_BLOB *gensec_skey,
                                 struct drsuapi_DsReplicaObjectListItemEx *cur)
 {
        static uint32_t object_id;
        const char *save_values_dir;
 
-       if (!lp_parm_bool(-1,"dssync","print_pwd_blobs", false)) {
+       if (!lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
                return; 
        }
 
-       save_values_dir = lp_parm_string(-1,"dssync","save_pwd_blobs_dir");
+       save_values_dir = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "save_pwd_blobs_dir");
 
        for (; cur; cur = cur->next_object) {
                const char *dn;
                struct dom_sid *sid = NULL;
                uint32_t rid = 0;
-               BOOL dn_printed = False;
+               bool dn_printed = false;
                uint32_t i;
 
                if (!cur->object.identifier) continue;
@@ -504,11 +531,13 @@ static void test_analyse_objects(struct DsSyncTest *ctx,
                        if (!dn_printed) {
                                object_id++;
                                DEBUG(0,("DN[%u] %s\n", object_id, dn));
-                               dn_printed = True;
+                               dn_printed = true;
                        }
                        DEBUGADD(0,("ATTR: %s enc.length=%lu plain.length=%lu\n",
                                    name, (long)enc_data->length, (long)plain_data.length));
                        if (plain_data.length) {
+                               enum ndr_err_code ndr_err;
+                               struct supplementalCredentialsBlob scb;
                                dump_data(0, plain_data.data, plain_data.length);
                                if (save_values_dir) {
                                        char *fname;
@@ -524,6 +553,13 @@ static void test_analyse_objects(struct DsSyncTest *ctx,
                                        }
                                        talloc_free(fname);
                                }
+
+                               ndr_err = ndr_pull_struct_blob_all(&plain_data, tctx,
+                                          lp_iconv_convenience(tctx->lp_ctx), &scb,
+                                          (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
+                               if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+                                       NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
+                               }
                        } else {
                                dump_data(0, enc_data->data, enc_data->length);
                        }
@@ -531,10 +567,10 @@ static void test_analyse_objects(struct DsSyncTest *ctx,
        }
 }
 
-static BOOL test_FetchData(struct DsSyncTest *ctx)
+static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
 {
        NTSTATUS status;
-       BOOL ret = True;
+       bool ret = true;
        int i, y = 0;
        uint64_t highest_usn = 0;
        const char *partition = NULL;
@@ -560,29 +596,28 @@ static BOOL test_FetchData(struct DsSyncTest *ctx)
        ZERO_STRUCT(null_guid);
        ZERO_STRUCT(null_sid);
 
-       partition = lp_parm_string(-1, "dssync", "partition");
+       partition = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "partition");
        if (partition == NULL) {
                partition = ctx->domain_dn;
                printf("dssync:partition not specified, defaulting to %s.\n", ctx->domain_dn);
        }
 
-       highest_usn = lp_parm_int(-1, "dssync", "highest_usn", 0);
+       highest_usn = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "highest_usn", 0);
 
-       array[0].level = lp_parm_int(-1, "dssync", "get_nc_changes_level", array[0].level);
+       array[0].level = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "get_nc_changes_level", array[0].level);
 
-       if (lp_parm_bool(-1,"dssync","print_pwd_blobs",False)) {
+       if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
                const struct samr_Password *nthash;
                nthash = cli_credentials_get_nt_hash(ctx->new_dc.credentials, ctx);
                if (nthash) {
-                       DEBUG(0,("CREDENTIALS nthash:\n"));
-                       dump_data(0, nthash->hash, sizeof(nthash->hash));
+                       dump_data_pw("CREDENTIALS nthash:", nthash->hash, sizeof(nthash->hash));
                }
        }
        status = gensec_session_key(ctx->new_dc.drsuapi.pipe->conn->security_state.generic_state,
                                    &gensec_skey);
        if (!NT_STATUS_IS_OK(status)) {
                printf("failed to get gensec session key: %s\n", nt_errstr(status));
-               return False;
+               return false;
        }
 
        for (i=0; i < ARRAY_SIZE(array); i++) {
@@ -606,10 +641,10 @@ static BOOL test_FetchData(struct DsSyncTest *ctx)
                        r.in.req.req5.highwatermark.highest_usn         = highest_usn;
                        r.in.req.req5.uptodateness_vector               = NULL;
                        r.in.req.req5.replica_flags                     = 0;
-                       if (lp_parm_bool(-1,"dssync","compression",False)) {
+                       if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
                                r.in.req.req5.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
                        }
-                       if (lp_parm_bool(-1,"dssync","neighbour_writeable",True)) {
+                       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_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
@@ -619,8 +654,8 @@ static BOOL test_FetchData(struct DsSyncTest *ctx)
                                                                        ;
                        r.in.req.req5.max_object_count                  = 133;
                        r.in.req.req5.max_ndr_size                      = 1336770;
-                       r.in.req.req5.unknown4                          = 0;
-                       r.in.req.req5.h1                                = 0;
+                       r.in.req.req5.extended_op                       = DRSUAPI_EXOP_NONE;
+                       r.in.req.req5.fsmo_info                         = 0;
 
                        break;
                case 8:
@@ -637,10 +672,10 @@ static BOOL test_FetchData(struct DsSyncTest *ctx)
                        r.in.req.req8.highwatermark.highest_usn         = highest_usn;
                        r.in.req.req8.uptodateness_vector               = NULL;
                        r.in.req.req8.replica_flags                     = 0;
-                       if (lp_parm_bool(-1,"dssync","compression",False)) {
+                       if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
                                r.in.req.req8.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
                        }
-                       if (lp_parm_bool(-1,"dssync","neighbour_writeable",True)) {
+                       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_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
@@ -651,10 +686,10 @@ static BOOL test_FetchData(struct DsSyncTest *ctx)
                        r.in.req.req8.max_object_count                  = 402;
                        r.in.req.req8.max_ndr_size                      = 402116;
 
-                       r.in.req.req8.unknown4                          = 0;
-                       r.in.req.req8.h1                                = 0;
-                       r.in.req.req8.unique_ptr1                       = 0;
-                       r.in.req.req8.unique_ptr2                       = 0;
+                       r.in.req.req8.extended_op                       = DRSUAPI_EXOP_NONE;
+                       r.in.req.req8.fsmo_info                         = 0;
+                       r.in.req.req8.partial_attribute_set             = NULL;
+                       r.in.req.req8.partial_attribute_set_ex          = NULL;
                        r.in.req.req8.mapping_ctr.num_mappings          = 0;
                        r.in.req.req8.mapping_ctr.mappings              = NULL;
 
@@ -686,16 +721,16 @@ static BOOL test_FetchData(struct DsSyncTest *ctx)
                                        errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
                                }
                                printf("dcerpc_drsuapi_DsGetNCChanges failed - %s\n", errstr);
-                               ret = False;
+                               ret = false;
                        } else if (!W_ERROR_IS_OK(r.out.result)) {
                                printf("DsGetNCChanges failed - %s\n", win_errstr(r.out.result));
-                               ret = False;
+                               ret = false;
                        }
 
-                       if (ret == True && *r.out.level == 1) {
+                       if (ret == true && *r.out.level == 1) {
                                out_level = 1;
                                ctr1 = &r.out.ctr.ctr1;
-                       } else if (ret == True && *r.out.level == 2) {
+                       } else if (ret == true && *r.out.level == 2) {
                                out_level = 1;
                                ctr1 = r.out.ctr.ctr2.ctr.mszip1.ctr1;
                        }
@@ -705,22 +740,27 @@ static BOOL test_FetchData(struct DsSyncTest *ctx)
                                        (long long)ctr1->new_highwatermark.tmp_highest_usn,
                                        (long long)ctr1->new_highwatermark.highest_usn));
 
-                               test_analyse_objects(ctx, &gensec_skey, ctr1->first_object);
+                               test_analyse_objects(tctx, ctx, &gensec_skey, ctr1->first_object);
 
-                               if (ctr1->new_highwatermark.tmp_highest_usn > ctr1->new_highwatermark.highest_usn) {
+                               if (ctr1->more_data) {
                                        r.in.req.req5.highwatermark = ctr1->new_highwatermark;
                                        continue;
                                }
                        }
 
-                       if (ret == True && *r.out.level == 6) {
+                       if (ret == true && *r.out.level == 6) {
                                out_level = 6;
                                ctr6 = &r.out.ctr.ctr6;
-                       } else if (ret == True && *r.out.level == 7
+                       } else if (ret == true && *r.out.level == 7
                                   && r.out.ctr.ctr7.level == 6
                                   && r.out.ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP) {
                                out_level = 6;
                                ctr6 = r.out.ctr.ctr7.ctr.mszip6.ctr6;
+                       } else if (ret == true && *r.out.level == 7
+                                  && r.out.ctr.ctr7.level == 6
+                                  && r.out.ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS) {
+                               out_level = 6;
+                               ctr6 = r.out.ctr.ctr7.ctr.xpress6.ctr6;
                        }
 
                        if (out_level == 6) {
@@ -728,9 +768,9 @@ static BOOL test_FetchData(struct DsSyncTest *ctx)
                                        (long long)ctr6->new_highwatermark.tmp_highest_usn,
                                        (long long)ctr6->new_highwatermark.highest_usn));
 
-                               test_analyse_objects(ctx, &gensec_skey, ctr6->first_object);
+                               test_analyse_objects(tctx, ctx, &gensec_skey, ctr6->first_object);
 
-                               if (ctr6->new_highwatermark.tmp_highest_usn > ctr6->new_highwatermark.highest_usn) {
+                               if (ctr6->more_data) {
                                        r.in.req.req8.highwatermark = ctr6->new_highwatermark;
                                        continue;
                                }
@@ -743,15 +783,12 @@ static BOOL test_FetchData(struct DsSyncTest *ctx)
        return ret;
 }
 
-static BOOL test_FetchNT4Data(struct DsSyncTest *ctx)
+static bool test_FetchNT4Data(struct torture_context *tctx, 
+                             struct DsSyncTest *ctx)
 {
        NTSTATUS status;
-       BOOL ret = True;
-       int i, y = 0;
-       uint64_t highest_usn = 0;
-       const char *partition = NULL;
+       bool ret = true;
        struct drsuapi_DsGetNT4ChangeLog r;
-       int32_t out_level = 0;
        struct GUID null_guid;
        struct dom_sid null_sid;
        DATA_BLOB cookie;
@@ -764,27 +801,33 @@ static BOOL test_FetchNT4Data(struct DsSyncTest *ctx)
        r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
        r.in.level              = 1;
 
-       r.in.req.req1.unknown1  = lp_parm_int(-1, "dssync", "nt4-1", 3);
-       r.in.req.req1.unknown2  = lp_parm_int(-1, "dssync", "nt4-2", 0x00004000);
+       r.in.req.req1.unknown1  = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "nt4-1", 3);
+       r.in.req.req1.unknown2  = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "nt4-2", 0x00004000);
 
        while (1) {
                r.in.req.req1.length    = cookie.length;
                r.in.req.req1.data      = cookie.data;
 
                status = dcerpc_drsuapi_DsGetNT4ChangeLog(ctx->new_dc.drsuapi.pipe, ctx, &r);
-               if (!NT_STATUS_IS_OK(status)) {
+               if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
+                       printf("DsGetNT4ChangeLog not supported by target server\n");
+                       break;
+               } 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.pipe->last_fault_code);
                        }
                        printf("dcerpc_drsuapi_DsGetNT4ChangeLog failed - %s\n", errstr);
-                       ret = False;
+                       ret = false;
+               } else if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_DOMAIN_ROLE)) {
+                       printf("DsGetNT4ChangeLog not supported by target server\n");
+                       break;
                } else if (!W_ERROR_IS_OK(r.out.result)) {
                        printf("DsGetNT4ChangeLog failed - %s\n", win_errstr(r.out.result));
-                       ret = False;
+                       ret = false;
                } else if (r.out.level != 1) {
                        printf("DsGetNT4ChangeLog unknown level - %u\n", r.out.level);
-                       ret = False;
+                       ret = false;
                } 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;
@@ -792,7 +835,7 @@ static BOOL test_FetchNT4Data(struct DsSyncTest *ctx)
                        continue;
                } else {
                        printf("DsGetNT4ChangeLog failed - %s\n", nt_errstr(r.out.info.info1.status));
-                       ret = False;
+                       ret = false;
                }
 
                break;
@@ -801,30 +844,30 @@ static BOOL test_FetchNT4Data(struct DsSyncTest *ctx)
        return ret;
 }
 
-BOOL torture_rpc_dssync(struct torture_context *torture)
+bool torture_rpc_dssync(struct torture_context *torture)
 {
-       BOOL ret = True;
+       bool ret = true;
        TALLOC_CTX *mem_ctx;
        struct DsSyncTest *ctx;
        
        mem_ctx = talloc_init("torture_rpc_dssync");
-       ctx = test_create_context(mem_ctx);
+       ctx = test_create_context(torture);
        
-       ret &= _test_DsBind(ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
+       ret &= _test_DsBind(torture, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
        if (!ret) {
                return ret;
        }
-       ret &= test_LDAPBind(ctx, ctx->admin.credentials, &ctx->admin.ldap);
+       ret &= test_LDAPBind(torture, ctx, ctx->admin.credentials, &ctx->admin.ldap);
        if (!ret) {
                return ret;
        }
-       ret &= test_GetInfo(ctx);
-       ret &= _test_DsBind(ctx, ctx->new_dc.credentials, &ctx->new_dc.drsuapi);
+       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(ctx);
-       ret &= test_FetchNT4Data(ctx);
+       ret &= test_FetchData(torture, ctx);
+       ret &= test_FetchNT4Data(torture, ctx);
 
        return ret;
 }