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 = 0x1ffffb7f;
115 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
116 our_bind_info->info.info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
117 our_bind_info->info.info28.site_guid = GUID_zero();
118 our_bind_info->info.info28.u1 = 508;
119 our_bind_info->info.info28.repl_epoch = 0;
121 GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &ctx->new_dc.drsuapi.bind_guid);
123 ctx->new_dc.drsuapi.req.in.bind_guid = &ctx->new_dc.drsuapi.bind_guid;
124 ctx->new_dc.drsuapi.req.in.bind_info = our_bind_info;
125 ctx->new_dc.drsuapi.req.out.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
132 static BOOL _test_DsBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
136 struct event_context *event = NULL;
138 status = dcerpc_pipe_connect_b(ctx,
139 &b->pipe, ctx->drsuapi_binding,
141 DCERPC_DRSUAPI_VERSION,
144 if (!NT_STATUS_IS_OK(status)) {
145 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
149 status = dcerpc_drsuapi_DsBind(b->pipe, ctx, &b->req);
150 if (!NT_STATUS_IS_OK(status)) {
151 const char *errstr = nt_errstr(status);
152 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
153 errstr = dcerpc_errstr(ctx, b->pipe->last_fault_code);
155 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
157 } else if (!W_ERROR_IS_OK(b->req.out.result)) {
158 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
165 static BOOL test_LDAPBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
170 status = torture_ldap_connection(ctx, &l->conn, ctx->ldap_url);
171 if (!NT_STATUS_IS_OK(status)) {
172 printf("failed to connect to LDAP: %s\n", ctx->ldap_url);
176 printf("connected to LDAP: %s\n", ctx->ldap_url);
178 status = torture_ldap_bind_sasl(l->conn, credentials);
179 if (!NT_STATUS_IS_OK(status)) {
180 printf("failed to bind to LDAP:\n");
183 printf("bound to LDAP.\n");
188 static BOOL test_GetInfo(struct DsSyncTest *ctx)
191 struct drsuapi_DsCrackNames r;
192 struct drsuapi_DsNameString names[1];
195 struct cldap_socket *cldap = cldap_socket_init(ctx, NULL);
196 struct cldap_netlogon search;
198 r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
200 r.in.req.req1.unknown1 = 0x000004e4;
201 r.in.req.req1.unknown2 = 0x00000407;
202 r.in.req.req1.count = 1;
203 r.in.req.req1.names = names;
204 r.in.req.req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
205 r.in.req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
206 r.in.req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
207 names[0].str = talloc_asprintf(ctx, "%s\\", lp_workgroup());
209 status = dcerpc_drsuapi_DsCrackNames(ctx->admin.drsuapi.pipe, ctx, &r);
210 if (!NT_STATUS_IS_OK(status)) {
211 const char *errstr = nt_errstr(status);
212 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
213 errstr = dcerpc_errstr(ctx, ctx->admin.drsuapi.pipe->last_fault_code);
215 printf("dcerpc_drsuapi_DsCrackNames failed - %s\n", errstr);
217 } else if (!W_ERROR_IS_OK(r.out.result)) {
218 printf("DsCrackNames failed - %s\n", win_errstr(r.out.result));
222 ctx->domain_dn = r.out.ctr.ctr1->array[0].result_name;
225 search.in.dest_address = ctx->drsuapi_binding->host;
226 search.in.acct_control = -1;
227 search.in.version = 6;
228 status = cldap_netlogon(cldap, ctx, &search);
229 if (!NT_STATUS_IS_OK(status)) {
230 const char *errstr = nt_errstr(status);
231 ctx->site_name = talloc_asprintf(ctx, "%s", "Default-First-Site-Name");
232 printf("cldap_netlogon() returned %s. Defaulting to Site-Name: %s\n", errstr, ctx->site_name);
234 ctx->site_name = talloc_steal(ctx, search.out.netlogon.logon5.site_name);
235 printf("cldap_netlogon() returned Site-Name: %s.\n",ctx->site_name);
239 ctx->new_dc.invocation_id = GUID_random();
245 static BOOL test_FetchData(struct DsSyncTest *ctx)
250 uint64_t highest_usn = 0;
251 const char *partition = NULL;
252 struct drsuapi_DsGetNCChanges r;
253 struct drsuapi_DsReplicaObjectIdentifier nc;
254 struct GUID null_guid;
255 struct dom_sid null_sid;
267 ZERO_STRUCT(null_guid);
268 ZERO_STRUCT(null_sid);
270 partition = lp_parm_string(-1, "dssync", "partition");
271 if (partition == NULL) {
272 partition = ctx->domain_dn;
273 printf("dssync:partition not specified, defaulting to %s.\n", ctx->domain_dn);
276 highest_usn = lp_parm_int(-1, "dssync", "highest_usn", 0);
278 for (i=0; i < ARRAY_SIZE(array); i++) {
279 printf("testing DsGetNCChanges level %d\n",
282 r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
283 r.in.level = array[i].level;
285 switch (r.in.level) {
291 r.in.req.req5.destination_dsa_guid = ctx->new_dc.invocation_id;
292 r.in.req.req5.source_dsa_guid = null_guid;
293 r.in.req.req5.naming_context = &nc;
294 r.in.req.req5.highwatermark.tmp_highest_usn = highest_usn;
295 r.in.req.req5.highwatermark.reserved_usn = 0;
296 r.in.req.req5.highwatermark.highest_usn = highest_usn;
297 r.in.req.req5.uptodateness_vector = NULL;
298 r.in.req.req5.replica_flags = 0;
299 if (lp_parm_bool(-1,"dssync","compression",False)) {
300 r.in.req.req5.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
302 if (lp_parm_bool(-1,"dssync","neighbour_writeable",True)) {
303 r.in.req.req5.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
305 r.in.req.req5.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
306 | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
307 | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
308 | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
310 r.in.req.req5.unknown2 = 133;
311 r.in.req.req5.unknown3 = 1336770;
312 r.in.req.req5.unknown4 = 0;
313 r.in.req.req5.h1 = 0;
320 /* nc.dn can be set to any other ad partition */
322 r.in.req.req8.destination_dsa_guid = ctx->new_dc.invocation_id;
323 r.in.req.req8.source_dsa_guid = null_guid;
324 r.in.req.req8.naming_context = &nc;
325 r.in.req.req8.highwatermark.tmp_highest_usn = highest_usn;
326 r.in.req.req8.highwatermark.reserved_usn = 0;
327 r.in.req.req8.highwatermark.highest_usn = highest_usn;
328 r.in.req.req8.uptodateness_vector = NULL;
329 r.in.req.req8.replica_flags = 0;
330 if (lp_parm_bool(-1,"dssync","compression",False)) {
331 r.in.req.req8.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
333 if (lp_parm_bool(-1,"dssync","neighbour_writeable",True)) {
334 r.in.req.req8.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
336 r.in.req.req8.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
337 | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
338 | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
339 | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
341 r.in.req.req8.unknown2 = 402;
342 r.in.req.req8.unknown3 = 402116;
344 r.in.req.req8.unknown4 = 0;
345 r.in.req.req8.h1 = 0;
346 r.in.req.req8.unique_ptr1 = 0;
347 r.in.req.req8.unique_ptr2 = 0;
348 r.in.req.req8.ctr12.count = 0;
349 r.in.req.req8.ctr12.array = NULL;
354 printf("Dumping AD partition: %s\n", nc.dn);
358 if (r.in.level == 8) {
359 DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
360 r.in.req.req8.highwatermark.tmp_highest_usn,
361 r.in.req.req8.highwatermark.highest_usn));
364 status = dcerpc_drsuapi_DsGetNCChanges(ctx->new_dc.drsuapi.pipe, ctx, &r);
365 if (!NT_STATUS_IS_OK(status)) {
366 const char *errstr = nt_errstr(status);
367 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
368 errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
370 printf("dcerpc_drsuapi_DsGetNCChanges failed - %s\n", errstr);
372 } else if (!W_ERROR_IS_OK(r.out.result)) {
373 printf("DsGetNCChanges failed - %s\n", win_errstr(r.out.result));
377 if (ret == True && r.out.level == 6) {
378 DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
379 r.out.ctr.ctr6.new_highwatermark.tmp_highest_usn,
380 r.out.ctr.ctr6.new_highwatermark.highest_usn));
382 if (r.out.ctr.ctr6.new_highwatermark.tmp_highest_usn > r.out.ctr.ctr6.new_highwatermark.highest_usn) {
383 r.in.req.req8.highwatermark = r.out.ctr.ctr6.new_highwatermark;
394 BOOL torture_rpc_dssync(void)
398 struct DsSyncTest *ctx;
400 mem_ctx = talloc_init("torture_rpc_dssync");
401 ctx = test_create_context(mem_ctx);
403 ret &= _test_DsBind(ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
404 ret &= test_LDAPBind(ctx, ctx->admin.credentials, &ctx->admin.ldap);
405 ret &= test_GetInfo(ctx);
406 ret &= _test_DsBind(ctx, ctx->admin.credentials, &ctx->new_dc.drsuapi);
407 ret &= test_FetchData(ctx);