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 "librpc/gen_ndr/ndr_drsuapi_c.h"
27 #include "libcli/cldap/cldap.h"
28 #include "libcli/ldap/ldap_client.h"
29 #include "torture/torture.h"
30 #include "torture/ldap/proto.h"
32 struct DsSyncBindInfo {
33 struct dcerpc_pipe *pipe;
34 struct drsuapi_DsBind req;
35 struct GUID bind_guid;
36 struct drsuapi_DsBindInfoCtr our_bind_info_ctr;
37 struct drsuapi_DsBindInfo28 our_bind_info28;
38 struct drsuapi_DsBindInfo28 peer_bind_info28;
39 struct policy_handle bind_handle;
42 struct DsSyncLDAPInfo {
43 struct ldap_connection *conn;
47 struct dcerpc_binding *drsuapi_binding;
50 const char *site_name;
52 const char *domain_dn;
54 /* what we need to do as 'Administrator' */
56 struct cli_credentials *credentials;
57 struct DsSyncBindInfo drsuapi;
58 struct DsSyncLDAPInfo ldap;
61 /* what we need to do as the new dc machine account */
63 struct cli_credentials *credentials;
64 struct DsSyncBindInfo drsuapi;
65 struct drsuapi_DsGetDCInfo2 dc_info2;
66 struct GUID invocation_id;
67 struct GUID object_guid;
70 /* info about the old dc */
72 struct drsuapi_DsGetDomainControllerInfo dc_info;
76 static struct DsSyncTest *test_create_context(TALLOC_CTX *mem_ctx)
79 struct DsSyncTest *ctx;
80 struct drsuapi_DsBindInfo28 *our_bind_info28;
81 struct drsuapi_DsBindInfoCtr *our_bind_info_ctr;
82 const char *binding = lp_parm_string(-1, "torture", "binding");
83 ctx = talloc_zero(mem_ctx, struct DsSyncTest);
84 if (!ctx) return NULL;
86 status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
87 if (!NT_STATUS_IS_OK(status)) {
88 printf("Bad binding string %s\n", binding);
91 ctx->drsuapi_binding->flags |= DCERPC_SIGN | DCERPC_SEAL;
93 ctx->ldap_url = talloc_asprintf(ctx, "ldap://%s/", ctx->drsuapi_binding->host);
96 ctx->admin.credentials = cmdline_credentials;
98 our_bind_info28 = &ctx->admin.drsuapi.our_bind_info28;
99 our_bind_info28->supported_extensions = 0xFFFFFFFF;
100 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
101 our_bind_info28->site_guid = GUID_zero();
102 our_bind_info28->u1 = 0;
103 our_bind_info28->repl_epoch = 1;
105 our_bind_info_ctr = &ctx->admin.drsuapi.our_bind_info_ctr;
106 our_bind_info_ctr->length = 28;
107 our_bind_info_ctr->info.info28 = *our_bind_info28;
109 GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
111 ctx->admin.drsuapi.req.in.bind_guid = &ctx->admin.drsuapi.bind_guid;
112 ctx->admin.drsuapi.req.in.bind_info = our_bind_info_ctr;
113 ctx->admin.drsuapi.req.out.bind_handle = &ctx->admin.drsuapi.bind_handle;
117 our_bind_info28 = &ctx->new_dc.drsuapi.our_bind_info28;
118 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
119 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
120 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
121 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
122 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
123 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
124 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
125 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
126 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
127 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
128 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
129 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
130 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
131 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
132 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
133 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
134 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
135 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
136 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
137 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
138 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
139 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
140 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
141 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
142 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
143 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
144 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
145 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
146 if (lp_parm_bool(-1,"dssync","xpress",False)) {
147 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
149 our_bind_info28->site_guid = GUID_zero();
150 our_bind_info28->u1 = 508;
151 our_bind_info28->repl_epoch = 0;
153 our_bind_info_ctr = &ctx->new_dc.drsuapi.our_bind_info_ctr;
154 our_bind_info_ctr->length = 28;
155 our_bind_info_ctr->info.info28 = *our_bind_info28;
157 GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &ctx->new_dc.drsuapi.bind_guid);
159 ctx->new_dc.drsuapi.req.in.bind_guid = &ctx->new_dc.drsuapi.bind_guid;
160 ctx->new_dc.drsuapi.req.in.bind_info = our_bind_info_ctr;
161 ctx->new_dc.drsuapi.req.out.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
168 static BOOL _test_DsBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
172 struct event_context *event = NULL;
174 status = dcerpc_pipe_connect_b(ctx,
175 &b->pipe, ctx->drsuapi_binding,
176 &dcerpc_table_drsuapi,
179 if (!NT_STATUS_IS_OK(status)) {
180 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
184 status = dcerpc_drsuapi_DsBind(b->pipe, ctx, &b->req);
185 if (!NT_STATUS_IS_OK(status)) {
186 const char *errstr = nt_errstr(status);
187 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
188 errstr = dcerpc_errstr(ctx, b->pipe->last_fault_code);
190 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
192 } else if (!W_ERROR_IS_OK(b->req.out.result)) {
193 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
197 ZERO_STRUCT(b->peer_bind_info28);
198 if (b->req.out.bind_info) {
199 switch (b->req.out.bind_info->length) {
201 struct drsuapi_DsBindInfo24 *info24;
202 info24 = &b->req.out.bind_info->info.info24;
203 b->peer_bind_info28.supported_extensions= info24->supported_extensions;
204 b->peer_bind_info28.site_guid = info24->site_guid;
205 b->peer_bind_info28.u1 = info24->u1;
206 b->peer_bind_info28.repl_epoch = 0;
210 b->peer_bind_info28 = b->req.out.bind_info->info.info28;
218 static BOOL test_LDAPBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
223 status = torture_ldap_connection(ctx, &l->conn, ctx->ldap_url);
224 if (!NT_STATUS_IS_OK(status)) {
225 printf("failed to connect to LDAP: %s\n", ctx->ldap_url);
229 printf("connected to LDAP: %s\n", ctx->ldap_url);
231 status = torture_ldap_bind_sasl(l->conn, credentials);
232 if (!NT_STATUS_IS_OK(status)) {
233 printf("failed to bind to LDAP:\n");
236 printf("bound to LDAP.\n");
241 static BOOL test_GetInfo(struct DsSyncTest *ctx)
244 struct drsuapi_DsCrackNames r;
245 struct drsuapi_DsNameString names[1];
248 struct cldap_socket *cldap = cldap_socket_init(ctx, NULL);
249 struct cldap_netlogon search;
251 r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
253 r.in.req.req1.unknown1 = 0x000004e4;
254 r.in.req.req1.unknown2 = 0x00000407;
255 r.in.req.req1.count = 1;
256 r.in.req.req1.names = names;
257 r.in.req.req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
258 r.in.req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
259 r.in.req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
260 names[0].str = talloc_asprintf(ctx, "%s\\", lp_workgroup());
262 status = dcerpc_drsuapi_DsCrackNames(ctx->admin.drsuapi.pipe, ctx, &r);
263 if (!NT_STATUS_IS_OK(status)) {
264 const char *errstr = nt_errstr(status);
265 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
266 errstr = dcerpc_errstr(ctx, ctx->admin.drsuapi.pipe->last_fault_code);
268 printf("dcerpc_drsuapi_DsCrackNames failed - %s\n", errstr);
270 } else if (!W_ERROR_IS_OK(r.out.result)) {
271 printf("DsCrackNames failed - %s\n", win_errstr(r.out.result));
275 ctx->domain_dn = r.out.ctr.ctr1->array[0].result_name;
278 search.in.dest_address = ctx->drsuapi_binding->host;
279 search.in.acct_control = -1;
280 search.in.version = 6;
281 status = cldap_netlogon(cldap, ctx, &search);
282 if (!NT_STATUS_IS_OK(status)) {
283 const char *errstr = nt_errstr(status);
284 ctx->site_name = talloc_asprintf(ctx, "%s", "Default-First-Site-Name");
285 printf("cldap_netlogon() returned %s. Defaulting to Site-Name: %s\n", errstr, ctx->site_name);
287 ctx->site_name = talloc_steal(ctx, search.out.netlogon.logon5.site_name);
288 printf("cldap_netlogon() returned Site-Name: %s.\n",ctx->site_name);
292 ctx->new_dc.invocation_id = GUID_random();
297 static void test_analyse_objects(struct DsSyncTest *ctx,
298 struct drsuapi_DsReplicaObjectListItemEx *cur)
300 if (!lp_parm_bool(-1,"dssync","print_pwd_blobs",False)) {
304 for (; cur; cur = cur->next_object) {
306 BOOL dn_printed = False;
309 if (!cur->object.identifier) continue;
311 dn = cur->object.identifier->dn;
313 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
314 const char *name = NULL;
315 DATA_BLOB *data = NULL;
316 struct drsuapi_DsReplicaAttribute *attr;
317 attr = &cur->object.attribute_ctr.attributes[i];
319 switch (attr->attid) {
320 case DRSUAPI_ATTRIBUTE_dBCSPwd:
323 case DRSUAPI_ATTRIBUTE_unicodePwd:
326 case DRSUAPI_ATTRIBUTE_ntPwdHistory:
327 name = "ntPwdHistory";
329 case DRSUAPI_ATTRIBUTE_lmPwdHistory:
330 name = "lmPwdHistory";
332 case DRSUAPI_ATTRIBUTE_supplementalCredentials:
333 name = "supplementalCredentials";
335 case DRSUAPI_ATTRIBUTE_msDS_KeyVersionNumber:
336 name = "msDS-KeyVersionNumber";
342 if (attr->value_ctr.data_blob.num_values != 1) continue;
344 if (!attr->value_ctr.data_blob.values[0].data) continue;
346 data = attr->value_ctr.data_blob.values[0].data;
349 DEBUG(0,("DN: %s\n", dn));
352 DEBUGADD(0,("ATTR: %s data_blob.length=%lu\n",
353 name, (long)data->length));
354 dump_data(0,data->data, data->length);
359 static BOOL test_FetchData(struct DsSyncTest *ctx)
364 uint64_t highest_usn = 0;
365 const char *partition = NULL;
366 struct drsuapi_DsGetNCChanges r;
367 struct drsuapi_DsReplicaObjectIdentifier nc;
368 struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
369 struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
370 int32_t out_level = 0;
371 struct GUID null_guid;
372 struct dom_sid null_sid;
384 ZERO_STRUCT(null_guid);
385 ZERO_STRUCT(null_sid);
387 partition = lp_parm_string(-1, "dssync", "partition");
388 if (partition == NULL) {
389 partition = ctx->domain_dn;
390 printf("dssync:partition not specified, defaulting to %s.\n", ctx->domain_dn);
393 highest_usn = lp_parm_int(-1, "dssync", "highest_usn", 0);
395 for (i=0; i < ARRAY_SIZE(array); i++) {
396 printf("testing DsGetNCChanges level %d\n",
399 r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
400 r.in.level = array[i].level;
402 switch (r.in.level) {
408 r.in.req.req5.destination_dsa_guid = ctx->new_dc.invocation_id;
409 r.in.req.req5.source_dsa_guid = null_guid;
410 r.in.req.req5.naming_context = &nc;
411 r.in.req.req5.highwatermark.tmp_highest_usn = highest_usn;
412 r.in.req.req5.highwatermark.reserved_usn = 0;
413 r.in.req.req5.highwatermark.highest_usn = highest_usn;
414 r.in.req.req5.uptodateness_vector = NULL;
415 r.in.req.req5.replica_flags = 0;
416 if (lp_parm_bool(-1,"dssync","compression",False)) {
417 r.in.req.req5.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
419 if (lp_parm_bool(-1,"dssync","neighbour_writeable",True)) {
420 r.in.req.req5.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
422 r.in.req.req5.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
423 | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
424 | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
425 | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
427 r.in.req.req5.unknown2 = 133;
428 r.in.req.req5.unknown3 = 1336770;
429 r.in.req.req5.unknown4 = 0;
430 r.in.req.req5.h1 = 0;
437 /* nc.dn can be set to any other ad partition */
439 r.in.req.req8.destination_dsa_guid = ctx->new_dc.invocation_id;
440 r.in.req.req8.source_dsa_guid = null_guid;
441 r.in.req.req8.naming_context = &nc;
442 r.in.req.req8.highwatermark.tmp_highest_usn = highest_usn;
443 r.in.req.req8.highwatermark.reserved_usn = 0;
444 r.in.req.req8.highwatermark.highest_usn = highest_usn;
445 r.in.req.req8.uptodateness_vector = NULL;
446 r.in.req.req8.replica_flags = 0;
447 if (lp_parm_bool(-1,"dssync","compression",False)) {
448 r.in.req.req8.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
450 if (lp_parm_bool(-1,"dssync","neighbour_writeable",True)) {
451 r.in.req.req8.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
453 r.in.req.req8.replica_flags |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
454 | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
455 | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
456 | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
458 r.in.req.req8.unknown2 = 402;
459 r.in.req.req8.unknown3 = 402116;
461 r.in.req.req8.unknown4 = 0;
462 r.in.req.req8.h1 = 0;
463 r.in.req.req8.unique_ptr1 = 0;
464 r.in.req.req8.unique_ptr2 = 0;
465 r.in.req.req8.ctr12.count = 0;
466 r.in.req.req8.ctr12.array = NULL;
471 printf("Dumping AD partition: %s\n", nc.dn);
475 if (r.in.level == 5) {
476 DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
477 (long long)r.in.req.req5.highwatermark.tmp_highest_usn,
478 (long long)r.in.req.req5.highwatermark.highest_usn));
481 if (r.in.level == 8) {
482 DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
483 (long long)r.in.req.req8.highwatermark.tmp_highest_usn,
484 (long long)r.in.req.req8.highwatermark.highest_usn));
487 status = dcerpc_drsuapi_DsGetNCChanges(ctx->new_dc.drsuapi.pipe, ctx, &r);
488 if (!NT_STATUS_IS_OK(status)) {
489 const char *errstr = nt_errstr(status);
490 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
491 errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
493 printf("dcerpc_drsuapi_DsGetNCChanges failed - %s\n", errstr);
495 } else if (!W_ERROR_IS_OK(r.out.result)) {
496 printf("DsGetNCChanges failed - %s\n", win_errstr(r.out.result));
500 if (ret == True && r.out.level == 1) {
502 ctr1 = &r.out.ctr.ctr1;
503 } else if (ret == True && r.out.level == 2) {
505 ctr1 = r.out.ctr.ctr2.ctr.mszip1.ctr1;
508 if (out_level == 1) {
509 DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
510 (long long)ctr1->new_highwatermark.tmp_highest_usn,
511 (long long)ctr1->new_highwatermark.highest_usn));
513 test_analyse_objects(ctx, ctr1->first_object);
515 if (ctr1->new_highwatermark.tmp_highest_usn > ctr1->new_highwatermark.highest_usn) {
516 r.in.req.req5.highwatermark = ctr1->new_highwatermark;
521 if (ret == True && r.out.level == 6) {
523 ctr6 = &r.out.ctr.ctr6;
524 } else if (ret == True && r.out.level == 7
525 && r.out.ctr.ctr7.level == 6
526 && r.out.ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP) {
528 ctr6 = r.out.ctr.ctr7.ctr.mszip6.ctr6;
531 if (out_level == 6) {
532 DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
533 (long long)ctr6->new_highwatermark.tmp_highest_usn,
534 (long long)ctr6->new_highwatermark.highest_usn));
536 test_analyse_objects(ctx, ctr6->first_object);
538 if (ctr6->new_highwatermark.tmp_highest_usn > ctr6->new_highwatermark.highest_usn) {
539 r.in.req.req8.highwatermark = ctr6->new_highwatermark;
551 BOOL torture_rpc_dssync(struct torture_context *torture)
555 struct DsSyncTest *ctx;
557 mem_ctx = talloc_init("torture_rpc_dssync");
558 ctx = test_create_context(mem_ctx);
560 ret &= _test_DsBind(ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
561 ret &= test_LDAPBind(ctx, ctx->admin.credentials, &ctx->admin.ldap);
562 ret &= test_GetInfo(ctx);
563 ret &= _test_DsBind(ctx, ctx->admin.credentials, &ctx->new_dc.drsuapi);
564 ret &= test_FetchData(ctx);