2 Unix SMB/CIFS implementation.
4 DsGetNCChanges replication test
6 Copyright (C) Stefan (metze) Metzmacher 2005
7 Copyright (C) Brad Henry 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "lib/cmdline/popt_common.h"
26 #include "libnet/libnet.h"
27 #include "librpc/gen_ndr/ndr_drsuapi.h"
28 #include "librpc/gen_ndr/ndr_samr.h"
29 #include "libcli/ldap/ldap.h"
30 #include "libcli/cldap/cldap.h"
33 struct DsSyncBindInfo {
34 struct dcerpc_pipe *pipe;
35 struct drsuapi_DsBind req;
36 struct GUID bind_guid;
37 struct drsuapi_DsBindInfoCtr our_bind_info;
38 struct policy_handle bind_handle;
41 struct DsSyncLDAPInfo {
42 struct ldap_connection *conn;
46 struct dcerpc_binding *drsuapi_binding;
49 const char *site_name;
51 const char *domain_dn;
53 /* what we need to do as 'Administrator' */
55 struct cli_credentials *credentials;
56 struct DsSyncBindInfo drsuapi;
57 struct DsSyncLDAPInfo ldap;
60 /* what we need to do as the new dc machine account */
62 struct cli_credentials *credentials;
63 struct DsSyncBindInfo drsuapi;
64 struct drsuapi_DsGetDCInfo2 dc_info2;
65 struct GUID invocation_id;
66 struct GUID object_guid;
69 /* info about the old dc */
71 struct drsuapi_DsGetDomainControllerInfo dc_info;
75 static struct DsSyncTest *test_create_context(TALLOC_CTX *mem_ctx)
78 struct DsSyncTest *ctx;
79 struct drsuapi_DsBindInfoCtr *our_bind_info;
80 const char *binding = lp_parm_string(-1, "torture", "binding");
81 ctx = talloc_zero(mem_ctx, struct DsSyncTest);
82 if (!ctx) return NULL;
84 status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
85 if (!NT_STATUS_IS_OK(status)) {
86 printf("Bad binding string %s\n", binding);
89 ctx->drsuapi_binding->flags |= DCERPC_SIGN | DCERPC_SEAL;
91 ctx->ldap_url = talloc_asprintf(ctx, "ldap://%s/", ctx->drsuapi_binding->host);
94 ctx->admin.credentials = cmdline_credentials;
96 our_bind_info = &ctx->admin.drsuapi.our_bind_info;
97 our_bind_info->length = 28;
98 our_bind_info->info.info28.supported_extensions = 0xFFFFFFFF;
99 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
100 our_bind_info->info.info28.site_guid = GUID_zero();
101 our_bind_info->info.info28.u1 = 0;
102 our_bind_info->info.info28.repl_epoch = 1;
104 GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
106 ctx->admin.drsuapi.req.in.bind_guid = &ctx->admin.drsuapi.bind_guid;
107 ctx->admin.drsuapi.req.in.bind_info = our_bind_info;
108 ctx->admin.drsuapi.req.out.bind_handle = &ctx->admin.drsuapi.bind_handle;
112 our_bind_info = &ctx->new_dc.drsuapi.our_bind_info;
113 our_bind_info->length = 28;
114 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
115 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
116 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
117 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
118 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
119 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
120 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
121 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
122 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
123 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
124 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
125 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
126 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
127 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
128 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
129 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
130 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
131 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
132 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
133 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
134 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
135 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
136 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
137 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
138 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
139 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
140 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
141 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
142 if (lp_parm_bool(-1,"dssync","xpress",False)) {
143 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
145 our_bind_info->info.info28.site_guid = GUID_zero();
146 our_bind_info->info.info28.u1 = 508;
147 our_bind_info->info.info28.repl_epoch = 0;
149 GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &ctx->new_dc.drsuapi.bind_guid);
151 ctx->new_dc.drsuapi.req.in.bind_guid = &ctx->new_dc.drsuapi.bind_guid;
152 ctx->new_dc.drsuapi.req.in.bind_info = our_bind_info;
153 ctx->new_dc.drsuapi.req.out.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
160 static BOOL _test_DsBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
164 struct event_context *event = NULL;
166 status = dcerpc_pipe_connect_b(ctx,
167 &b->pipe, ctx->drsuapi_binding,
169 DCERPC_DRSUAPI_VERSION,
172 if (!NT_STATUS_IS_OK(status)) {
173 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
177 status = dcerpc_drsuapi_DsBind(b->pipe, ctx, &b->req);
178 if (!NT_STATUS_IS_OK(status)) {
179 const char *errstr = nt_errstr(status);
180 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
181 errstr = dcerpc_errstr(ctx, b->pipe->last_fault_code);
183 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
185 } else if (!W_ERROR_IS_OK(b->req.out.result)) {
186 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
193 static BOOL test_LDAPBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
198 status = torture_ldap_connection(ctx, &l->conn, ctx->ldap_url);
199 if (!NT_STATUS_IS_OK(status)) {
200 printf("failed to connect to LDAP: %s\n", ctx->ldap_url);
204 printf("connected to LDAP: %s\n", ctx->ldap_url);
206 status = torture_ldap_bind_sasl(l->conn, credentials);
207 if (!NT_STATUS_IS_OK(status)) {
208 printf("failed to bind to LDAP:\n");
211 printf("bound to LDAP.\n");
216 static BOOL test_GetInfo(struct DsSyncTest *ctx)
219 struct drsuapi_DsCrackNames r;
220 struct drsuapi_DsNameString names[1];
223 struct cldap_socket *cldap = cldap_socket_init(ctx, NULL);
224 struct cldap_netlogon search;
226 r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
228 r.in.req.req1.unknown1 = 0x000004e4;
229 r.in.req.req1.unknown2 = 0x00000407;
230 r.in.req.req1.count = 1;
231 r.in.req.req1.names = names;
232 r.in.req.req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
233 r.in.req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
234 r.in.req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
235 names[0].str = talloc_asprintf(ctx, "%s\\", lp_workgroup());
237 status = dcerpc_drsuapi_DsCrackNames(ctx->admin.drsuapi.pipe, ctx, &r);
238 if (!NT_STATUS_IS_OK(status)) {
239 const char *errstr = nt_errstr(status);
240 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
241 errstr = dcerpc_errstr(ctx, ctx->admin.drsuapi.pipe->last_fault_code);
243 printf("dcerpc_drsuapi_DsCrackNames failed - %s\n", errstr);
245 } else if (!W_ERROR_IS_OK(r.out.result)) {
246 printf("DsCrackNames failed - %s\n", win_errstr(r.out.result));
250 ctx->domain_dn = r.out.ctr.ctr1->array[0].result_name;
253 search.in.dest_address = ctx->drsuapi_binding->host;
254 search.in.acct_control = -1;
255 search.in.version = 6;
256 status = cldap_netlogon(cldap, ctx, &search);
257 if (!NT_STATUS_IS_OK(status)) {
258 const char *errstr = nt_errstr(status);
259 ctx->site_name = talloc_asprintf(ctx, "%s", "Default-First-Site-Name");
260 printf("cldap_netlogon() returned %s. Defaulting to Site-Name: %s\n", errstr, ctx->site_name);
262 ctx->site_name = talloc_steal(ctx, search.out.netlogon.logon5.site_name);
263 printf("cldap_netlogon() returned Site-Name: %s.\n",ctx->site_name);
267 ctx->new_dc.invocation_id = GUID_random();
273 static BOOL test_FetchData(struct DsSyncTest *ctx)
278 uint64_t highest_usn = 0;
279 const char *partition = NULL;
280 struct drsuapi_DsGetNCChanges r;
281 struct drsuapi_DsReplicaObjectIdentifier nc;
282 struct GUID null_guid;
283 struct dom_sid null_sid;
295 ZERO_STRUCT(null_guid);
296 ZERO_STRUCT(null_sid);
298 partition = lp_parm_string(-1, "dssync", "partition");
299 if (partition == NULL) {
300 partition = ctx->domain_dn;
301 printf("dssync:partition not specified, defaulting to %s.\n", ctx->domain_dn);
304 highest_usn = lp_parm_int(-1, "dssync", "highest_usn", 0);
306 for (i=0; i < ARRAY_SIZE(array); i++) {
307 printf("testing DsGetNCChanges level %d\n",
310 r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
311 r.in.level = array[i].level;
313 switch (r.in.level) {
319 r.in.req.req5.destination_dsa_guid = ctx->new_dc.invocation_id;
320 r.in.req.req5.source_dsa_guid = null_guid;
321 r.in.req.req5.naming_context = &nc;
322 r.in.req.req5.highwatermark.tmp_highest_usn = highest_usn;
323 r.in.req.req5.highwatermark.reserved_usn = 0;
324 r.in.req.req5.highwatermark.highest_usn = highest_usn;
325 r.in.req.req5.uptodateness_vector = NULL;
326 r.in.req.req5.replica_flags = 0;
327 if (lp_parm_bool(-1,"dssync","compression",False)) {
328 r.in.req.req5.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
330 if (lp_parm_bool(-1,"dssync","neighbour_writeable",True)) {
331 r.in.req.req5.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
333 r.in.req.req5.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
334 | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
335 | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
336 | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
338 r.in.req.req5.unknown2 = 133;
339 r.in.req.req5.unknown3 = 1336770;
340 r.in.req.req5.unknown4 = 0;
341 r.in.req.req5.h1 = 0;
348 /* nc.dn can be set to any other ad partition */
350 r.in.req.req8.destination_dsa_guid = ctx->new_dc.invocation_id;
351 r.in.req.req8.source_dsa_guid = null_guid;
352 r.in.req.req8.naming_context = &nc;
353 r.in.req.req8.highwatermark.tmp_highest_usn = highest_usn;
354 r.in.req.req8.highwatermark.reserved_usn = 0;
355 r.in.req.req8.highwatermark.highest_usn = highest_usn;
356 r.in.req.req8.uptodateness_vector = NULL;
357 r.in.req.req8.replica_flags = 0;
358 if (lp_parm_bool(-1,"dssync","compression",False)) {
359 r.in.req.req8.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
361 if (lp_parm_bool(-1,"dssync","neighbour_writeable",True)) {
362 r.in.req.req8.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
364 r.in.req.req8.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
365 | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
366 | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
367 | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
369 r.in.req.req8.unknown2 = 402;
370 r.in.req.req8.unknown3 = 402116;
372 r.in.req.req8.unknown4 = 0;
373 r.in.req.req8.h1 = 0;
374 r.in.req.req8.unique_ptr1 = 0;
375 r.in.req.req8.unique_ptr2 = 0;
376 r.in.req.req8.ctr12.count = 0;
377 r.in.req.req8.ctr12.array = NULL;
382 printf("Dumping AD partition: %s\n", nc.dn);
386 if (r.in.level == 8) {
387 DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
388 r.in.req.req8.highwatermark.tmp_highest_usn,
389 r.in.req.req8.highwatermark.highest_usn));
392 status = dcerpc_drsuapi_DsGetNCChanges(ctx->new_dc.drsuapi.pipe, ctx, &r);
393 if (!NT_STATUS_IS_OK(status)) {
394 const char *errstr = nt_errstr(status);
395 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
396 errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
398 printf("dcerpc_drsuapi_DsGetNCChanges failed - %s\n", errstr);
400 } else if (!W_ERROR_IS_OK(r.out.result)) {
401 printf("DsGetNCChanges failed - %s\n", win_errstr(r.out.result));
405 if (ret == True && r.out.level == 6) {
406 DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
407 r.out.ctr.ctr6.new_highwatermark.tmp_highest_usn,
408 r.out.ctr.ctr6.new_highwatermark.highest_usn));
410 if (r.out.ctr.ctr6.new_highwatermark.tmp_highest_usn > r.out.ctr.ctr6.new_highwatermark.highest_usn) {
411 r.in.req.req8.highwatermark = r.out.ctr.ctr6.new_highwatermark;
422 BOOL torture_rpc_dssync(void)
426 struct DsSyncTest *ctx;
428 mem_ctx = talloc_init("torture_rpc_dssync");
429 ctx = test_create_context(mem_ctx);
431 ret &= _test_DsBind(ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
432 ret &= test_LDAPBind(ctx, ctx->admin.credentials, &ctx->admin.ldap);
433 ret &= test_GetInfo(ctx);
434 ret &= _test_DsBind(ctx, ctx->admin.credentials, &ctx->new_dc.drsuapi);
435 ret &= test_FetchData(ctx);