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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "lib/cmdline/popt_common.h"
25 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
26 #include "librpc/gen_ndr/ndr_drsblobs.h"
27 #include "libcli/cldap/cldap.h"
28 #include "torture/torture.h"
29 #include "../libcli/drsuapi/drsuapi.h"
30 #include "auth/gensec/gensec.h"
31 #include "param/param.h"
32 #include "dsdb/samdb/samdb.h"
33 #include "torture/rpc/torture_rpc.h"
34 #include "torture/drs/proto.h"
35 #include "lib/tsocket/tsocket.h"
36 #include "libcli/resolve/resolve.h"
38 struct DsSyncBindInfo {
39 struct dcerpc_pipe *drs_pipe;
40 struct dcerpc_binding_handle *drs_handle;
41 struct drsuapi_DsBind req;
42 struct GUID bind_guid;
43 struct drsuapi_DsBindInfoCtr our_bind_info_ctr;
44 struct drsuapi_DsBindInfo28 our_bind_info28;
45 struct drsuapi_DsBindInfo28 peer_bind_info28;
46 struct policy_handle bind_handle;
49 struct DsSyncLDAPInfo {
50 struct ldb_context *ldb;
54 struct dcerpc_binding *drsuapi_binding;
57 const char *site_name;
58 const char *dest_address;
59 const char *domain_dn;
61 /* what we need to do as 'Administrator' */
63 struct cli_credentials *credentials;
64 struct DsSyncBindInfo drsuapi;
65 struct DsSyncLDAPInfo ldap;
68 /* what we need to do as the new dc machine account */
70 struct cli_credentials *credentials;
71 struct DsSyncBindInfo drsuapi;
72 struct drsuapi_DsGetDCInfo2 dc_info2;
73 struct GUID invocation_id;
74 struct GUID object_guid;
77 /* info about the old dc */
79 struct drsuapi_DsGetDomainControllerInfo dc_info;
83 static struct DsSyncTest *test_create_context(struct torture_context *tctx)
86 struct DsSyncTest *ctx;
87 struct drsuapi_DsBindInfo28 *our_bind_info28;
88 struct drsuapi_DsBindInfoCtr *our_bind_info_ctr;
89 const char *binding = torture_setting_string(tctx, "binding", NULL);
92 ctx = talloc_zero(tctx, struct DsSyncTest);
93 if (!ctx) return NULL;
95 status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
96 if (!NT_STATUS_IS_OK(status)) {
97 printf("Bad binding string %s\n", binding);
100 ctx->drsuapi_binding->flags |= DCERPC_SIGN | DCERPC_SEAL;
102 ctx->ldap_url = talloc_asprintf(ctx, "ldap://%s", ctx->drsuapi_binding->host);
104 make_nbt_name_server(&name, ctx->drsuapi_binding->host);
106 /* do an initial name resolution to find its IP */
107 status = resolve_name(lp_resolve_context(tctx->lp_ctx), &name, tctx,
108 &ctx->dest_address, tctx->ev);
109 if (!NT_STATUS_IS_OK(status)) {
110 printf("Failed to resolve %s - %s\n",
111 name.name, nt_errstr(status));
116 ctx->admin.credentials = cmdline_credentials;
118 our_bind_info28 = &ctx->admin.drsuapi.our_bind_info28;
119 our_bind_info28->supported_extensions = 0xFFFFFFFF;
120 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
121 our_bind_info28->site_guid = GUID_zero();
122 our_bind_info28->pid = 0;
123 our_bind_info28->repl_epoch = 1;
125 our_bind_info_ctr = &ctx->admin.drsuapi.our_bind_info_ctr;
126 our_bind_info_ctr->length = 28;
127 our_bind_info_ctr->info.info28 = *our_bind_info28;
129 GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
131 ctx->admin.drsuapi.req.in.bind_guid = &ctx->admin.drsuapi.bind_guid;
132 ctx->admin.drsuapi.req.in.bind_info = our_bind_info_ctr;
133 ctx->admin.drsuapi.req.out.bind_handle = &ctx->admin.drsuapi.bind_handle;
136 ctx->new_dc.credentials = cmdline_credentials;
138 our_bind_info28 = &ctx->new_dc.drsuapi.our_bind_info28;
139 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
140 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
141 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
142 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
143 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
144 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
145 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
146 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
147 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
148 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
149 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
150 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
151 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
152 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
153 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
154 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
155 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
156 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
157 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
158 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
159 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
160 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
161 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
162 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
163 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
164 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
165 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
166 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
167 if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "xpress", false)) {
168 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
170 our_bind_info28->site_guid = GUID_zero();
171 our_bind_info28->pid = 0;
172 our_bind_info28->repl_epoch = 0;
174 our_bind_info_ctr = &ctx->new_dc.drsuapi.our_bind_info_ctr;
175 our_bind_info_ctr->length = 28;
176 our_bind_info_ctr->info.info28 = *our_bind_info28;
178 GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &ctx->new_dc.drsuapi.bind_guid);
180 ctx->new_dc.drsuapi.req.in.bind_guid = &ctx->new_dc.drsuapi.bind_guid;
181 ctx->new_dc.drsuapi.req.in.bind_info = our_bind_info_ctr;
182 ctx->new_dc.drsuapi.req.out.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
184 ctx->new_dc.invocation_id = ctx->new_dc.drsuapi.bind_guid;
191 static bool _test_DsBind(struct torture_context *tctx,
192 struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
197 status = dcerpc_pipe_connect_b(ctx,
198 &b->drs_pipe, ctx->drsuapi_binding,
200 credentials, tctx->ev, tctx->lp_ctx);
202 if (!NT_STATUS_IS_OK(status)) {
203 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
206 b->drs_handle = b->drs_pipe->binding_handle;
208 status = dcerpc_drsuapi_DsBind_r(b->drs_handle, ctx, &b->req);
209 if (!NT_STATUS_IS_OK(status)) {
210 const char *errstr = nt_errstr(status);
211 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
213 } else if (!W_ERROR_IS_OK(b->req.out.result)) {
214 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
218 ZERO_STRUCT(b->peer_bind_info28);
219 if (b->req.out.bind_info) {
220 switch (b->req.out.bind_info->length) {
222 struct drsuapi_DsBindInfo24 *info24;
223 info24 = &b->req.out.bind_info->info.info24;
224 b->peer_bind_info28.supported_extensions= info24->supported_extensions;
225 b->peer_bind_info28.site_guid = info24->site_guid;
226 b->peer_bind_info28.pid = info24->pid;
227 b->peer_bind_info28.repl_epoch = 0;
231 struct drsuapi_DsBindInfo48 *info48;
232 info48 = &b->req.out.bind_info->info.info48;
233 b->peer_bind_info28.supported_extensions= info48->supported_extensions;
234 b->peer_bind_info28.site_guid = info48->site_guid;
235 b->peer_bind_info28.pid = info48->pid;
236 b->peer_bind_info28.repl_epoch = info48->repl_epoch;
240 b->peer_bind_info28 = b->req.out.bind_info->info.info28;
243 printf("DsBind - warning: unknown BindInfo length: %u\n",
244 b->req.out.bind_info->length);
251 static bool test_LDAPBind(struct torture_context *tctx, struct DsSyncTest *ctx,
252 struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
256 struct ldb_context *ldb;
258 const char *modules_option[] = { "modules:paged_searches", NULL };
259 ctx->admin.ldap.ldb = ldb = ldb_init(ctx, tctx->ev);
264 /* Despite us loading the schema from the AD server, we need
265 * the samba handlers to get the extended DN syntax stuff */
266 ret = ldb_register_samba_handlers(ldb);
272 ldb_set_modules_dir(ldb,
275 lp_modulesdir(tctx->lp_ctx)));
277 if (ldb_set_opaque(ldb, "credentials", credentials)) {
282 if (ldb_set_opaque(ldb, "loadparm", tctx->lp_ctx)) {
287 ret = ldb_connect(ldb, ctx->ldap_url, 0, modules_option);
288 if (ret != LDB_SUCCESS) {
290 torture_assert_int_equal(tctx, ret, LDB_SUCCESS, "Failed to make LDB connection to target");
293 printf("connected to LDAP: %s\n", ctx->ldap_url);
298 static bool test_GetInfo(struct torture_context *tctx, struct DsSyncTest *ctx)
301 struct drsuapi_DsCrackNames r;
302 union drsuapi_DsNameRequest req;
303 union drsuapi_DsNameCtr ctr;
304 uint32_t level_out = 0;
305 struct drsuapi_DsNameString names[1];
307 struct cldap_socket *cldap;
308 struct cldap_netlogon search;
309 struct tsocket_address *dest_addr;
312 ret2 = tsocket_address_inet_from_strings(tctx, "ip",
314 lp_cldap_port(tctx->lp_ctx),
317 printf("failed to create tsocket_address for '%s' port %u - %s\n",
318 ctx->drsuapi_binding->host, lp_cldap_port(tctx->lp_ctx),
323 status = cldap_socket_init(ctx, NULL, NULL, dest_addr, &cldap);
324 if (!NT_STATUS_IS_OK(status)) {
325 printf("failed to setup cldap socket - %s\n",
330 r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
333 r.in.req->req1.codepage = 1252; /* western european */
334 r.in.req->req1.language = 0x00000407; /* german */
335 r.in.req->req1.count = 1;
336 r.in.req->req1.names = names;
337 r.in.req->req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
338 r.in.req->req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
339 r.in.req->req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
340 names[0].str = talloc_asprintf(ctx, "%s\\", lp_workgroup(tctx->lp_ctx));
342 r.out.level_out = &level_out;
345 status = dcerpc_drsuapi_DsCrackNames_r(ctx->admin.drsuapi.drs_handle, ctx, &r);
346 if (!NT_STATUS_IS_OK(status)) {
347 const char *errstr = nt_errstr(status);
348 printf("dcerpc_drsuapi_DsCrackNames failed - %s\n", errstr);
350 } else if (!W_ERROR_IS_OK(r.out.result)) {
351 printf("DsCrackNames failed - %s\n", win_errstr(r.out.result));
355 ctx->domain_dn = r.out.ctr->ctr1->array[0].result_name;
358 search.in.dest_address = NULL;
359 search.in.dest_port = 0;
360 search.in.acct_control = -1;
361 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
362 search.in.map_response = true;
363 status = cldap_netlogon(cldap, ctx, &search);
364 if (!NT_STATUS_IS_OK(status)) {
365 const char *errstr = nt_errstr(status);
366 ctx->site_name = talloc_asprintf(ctx, "%s", "Default-First-Site-Name");
367 printf("cldap_netlogon() returned %s. Defaulting to Site-Name: %s\n", errstr, ctx->site_name);
369 ctx->site_name = talloc_steal(ctx, search.out.netlogon.data.nt5_ex.client_site);
370 printf("cldap_netlogon() returned Client Site-Name: %s.\n",ctx->site_name);
371 printf("cldap_netlogon() returned Server Site-Name: %s.\n",search.out.netlogon.data.nt5_ex.server_site);
374 if (!ctx->domain_dn) {
375 struct ldb_context *ldb = ldb_init(ctx, tctx->ev);
376 struct ldb_dn *dn = samdb_dns_domain_to_dn(ldb, ctx, search.out.netlogon.data.nt5_ex.dns_domain);
377 ctx->domain_dn = ldb_dn_alloc_linearized(ctx, dn);
385 static bool test_analyse_objects(struct torture_context *tctx,
386 struct DsSyncTest *ctx,
387 const char *partition,
388 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
389 uint32_t object_count,
390 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
391 const DATA_BLOB *gensec_skey)
393 static uint32_t object_id;
394 const char *save_values_dir;
395 const struct drsuapi_DsReplicaObjectListItemEx *cur;
396 struct ldb_context *ldb = ctx->admin.ldap.ldb;
397 struct ldb_dn *deleted_dn;
400 struct dsdb_extended_replicated_objects *objs;
401 struct ldb_extended_dn_control *extended_dn_ctrl;
404 if (!dsdb_get_schema(ldb, NULL)) {
405 struct dsdb_schema *ldap_schema;
406 struct ldb_result *a_res;
407 struct ldb_result *c_res;
408 struct ldb_dn *schema_dn = ldb_get_schema_basedn(ldb);
409 ldap_schema = dsdb_new_schema(ctx);
413 status = dsdb_load_prefixmap_from_drsuapi(ldap_schema, mapping_ctr);
416 * load the attribute definitions
418 ret = ldb_search(ldb, ldap_schema, &a_res,
419 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
420 "(objectClass=attributeSchema)");
421 if (ret != LDB_SUCCESS) {
422 err_msg = talloc_asprintf(tctx,
423 "failed to search attributeSchema objects: %s",
425 torture_fail(tctx, err_msg);
429 * load the objectClass definitions
431 ret = ldb_search(ldb, ldap_schema, &c_res,
432 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
433 "(objectClass=classSchema)");
434 if (ret != LDB_SUCCESS) {
435 err_msg = talloc_asprintf(tctx,
436 "failed to search classSchema objects: %s",
438 torture_fail(tctx, err_msg);
442 for (i=0; i < a_res->count; i++) {
443 status = dsdb_attribute_from_ldb(ldb, ldap_schema, a_res->msgs[i]);
444 torture_assert_werr_ok(tctx, status,
445 talloc_asprintf(tctx,
446 "dsdb_attribute_from_ldb() failed for: %s",
447 ldb_dn_get_linearized(a_res->msgs[i]->dn)));
450 for (i=0; i < c_res->count; i++) {
451 status = dsdb_class_from_ldb(ldap_schema, c_res->msgs[i]);
452 torture_assert_werr_ok(tctx, status,
453 talloc_asprintf(tctx,
454 "dsdb_class_from_ldb() failed for: %s",
455 ldb_dn_get_linearized(c_res->msgs[i]->dn)));
459 ret = dsdb_set_schema(ldb, ldap_schema);
460 if (ret != LDB_SUCCESS) {
462 talloc_asprintf(tctx, "dsdb_set_schema() failed: %s", ldb_strerror(ret)));
466 status = dsdb_extended_replicated_objects_convert(ldb,
475 torture_assert_werr_ok(tctx, status, "dsdb_extended_replicated_objects_convert() failed!");
477 extended_dn_ctrl = talloc(objs, struct ldb_extended_dn_control);
478 extended_dn_ctrl->type = 1;
480 deleted_dn = ldb_dn_new(objs, ldb, partition);
481 ldb_dn_add_child_fmt(deleted_dn, "CN=Deleted Objects");
483 for (i=0; i < object_count; i++) {
484 struct ldb_request *search_req;
485 struct ldb_result *res;
486 struct ldb_message *new_msg, *drs_msg, *ldap_msg;
487 const char **attrs = talloc_array(objs, const char *, objs->objects[i].msg->num_elements+1);
488 for (j=0; j < objs->objects[i].msg->num_elements; j++) {
489 attrs[j] = objs->objects[i].msg->elements[j].name;
492 res = talloc_zero(objs, struct ldb_result);
494 return LDB_ERR_OPERATIONS_ERROR;
496 ret = ldb_build_search_req(&search_req, ldb, objs,
497 objs->objects[i].msg->dn,
503 ldb_search_default_callback,
505 if (ret != LDB_SUCCESS) {
508 talloc_steal(search_req, res);
509 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
510 if (ret != LDB_SUCCESS) {
514 ret = ldb_request_add_control(search_req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_dn_ctrl);
515 if (ret != LDB_SUCCESS) {
519 ret = ldb_request(ldb, search_req);
520 if (ret == LDB_SUCCESS) {
521 ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
524 torture_assert_int_equal(tctx, ret, LDB_SUCCESS,
525 talloc_asprintf(tctx,
526 "Could not re-fetch object just delivered over DRS: %s",
527 ldb_errstring(ldb)));
528 torture_assert_int_equal(tctx, res->count, 1, "Could not re-fetch object just delivered over DRS");
529 ldap_msg = res->msgs[0];
530 for (j=0; j < ldap_msg->num_elements; j++) {
531 ldap_msg->elements[j].flags = LDB_FLAG_MOD_ADD;
532 /* For unknown reasons, there is no nTSecurityDescriptor on cn=deleted objects over LDAP, but there is over DRS! Skip it on both transports for now here so */
533 if ((ldb_attr_cmp(ldap_msg->elements[j].name, "nTSecurityDescriptor") == 0) &&
534 (ldb_dn_compare(ldap_msg->dn, deleted_dn) == 0)) {
535 ldb_msg_remove_element(ldap_msg, &ldap_msg->elements[j]);
541 drs_msg = ldb_msg_canonicalize(ldb, objs->objects[i].msg);
542 talloc_steal(search_req, drs_msg);
544 for (j=0; j < drs_msg->num_elements; j++) {
545 if (drs_msg->elements[j].num_values == 0) {
546 ldb_msg_remove_element(drs_msg, &drs_msg->elements[j]);
550 /* For unknown reasons, there is no nTSecurityDescriptor on cn=deleted objects over LDAP, but there is over DRS! */
551 } else if ((ldb_attr_cmp(drs_msg->elements[j].name, "nTSecurityDescriptor") == 0) &&
552 (ldb_dn_compare(drs_msg->dn, deleted_dn) == 0)) {
553 ldb_msg_remove_element(drs_msg, &drs_msg->elements[j]);
556 } else if (ldb_attr_cmp(drs_msg->elements[j].name, "unicodePwd") == 0 ||
557 ldb_attr_cmp(drs_msg->elements[j].name, "dBCSPwd") == 0 ||
558 ldb_attr_cmp(drs_msg->elements[j].name, "ntPwdHistory") == 0 ||
559 ldb_attr_cmp(drs_msg->elements[j].name, "lmPwdHistory") == 0 ||
560 ldb_attr_cmp(drs_msg->elements[j].name, "supplementalCredentials") == 0 ||
561 ldb_attr_cmp(drs_msg->elements[j].name, "priorValue") == 0 ||
562 ldb_attr_cmp(drs_msg->elements[j].name, "currentValue") == 0 ||
563 ldb_attr_cmp(drs_msg->elements[j].name, "trustAuthOutgoing") == 0 ||
564 ldb_attr_cmp(drs_msg->elements[j].name, "trustAuthIncoming") == 0 ||
565 ldb_attr_cmp(drs_msg->elements[j].name, "initialAuthOutgoing") == 0 ||
566 ldb_attr_cmp(drs_msg->elements[j].name, "initialAuthIncoming") == 0) {
568 /* These are not shown over LDAP, so we need to skip them for the comparison */
569 ldb_msg_remove_element(drs_msg, &drs_msg->elements[j]);
573 drs_msg->elements[j].flags = LDB_FLAG_MOD_ADD;
578 ret = ldb_msg_diff_ex(ldb, drs_msg, ldap_msg,
579 (TALLOC_CTX*)search_req, &new_msg);
580 torture_assert(tctx, ret == LDB_SUCCESS, "ldb_msg_diff_ex() has failed");
581 if (new_msg->num_elements != 0) {
583 struct ldb_ldif ldif;
584 ldif.changetype = LDB_CHANGETYPE_MODIFY;
586 s = ldb_ldif_write_string(ldb, new_msg, &ldif);
587 s = talloc_asprintf(tctx, "\n# Difference in between DRS and LDAP objects: \n%s\n", s);
589 ret = ldb_msg_diff_ex(ldb, ldap_msg, drs_msg,
590 (TALLOC_CTX*)search_req, &ldif.msg);
591 torture_assert(tctx, ret == LDB_SUCCESS, "ldb_msg_diff_ex() has failed");
592 s = talloc_asprintf_append(s,
593 "\n# Difference in between LDAP and DRS objects: \n%s\n",
594 ldb_ldif_write_string(ldb, new_msg, &ldif));
596 s = talloc_asprintf_append(s,
597 "# Should have no objects in 'difference' message. Diff elements: %d",
598 new_msg->num_elements);
599 torture_fail(tctx, s);
602 /* search_req is used as a tmp talloc context in the above */
603 talloc_free(search_req);
606 if (!lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
611 save_values_dir = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "save_pwd_blobs_dir");
613 for (cur = first_object; cur; cur = cur->next_object) {
615 struct dom_sid *sid = NULL;
617 bool dn_printed = false;
619 if (!cur->object.identifier) continue;
621 dn = cur->object.identifier->dn;
622 if (cur->object.identifier->sid.num_auths > 0) {
623 sid = &cur->object.identifier->sid;
624 rid = sid->sub_auths[sid->num_auths - 1];
627 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
629 const char *name = NULL;
631 DATA_BLOB *enc_data = NULL;
632 DATA_BLOB plain_data;
633 struct drsuapi_DsReplicaAttribute *attr;
634 ndr_pull_flags_fn_t pull_fn = NULL;
635 ndr_print_fn_t print_fn = NULL;
637 attr = &cur->object.attribute_ctr.attributes[i];
639 switch (attr->attid) {
640 case DRSUAPI_ATTRIBUTE_dBCSPwd:
644 case DRSUAPI_ATTRIBUTE_unicodePwd:
648 case DRSUAPI_ATTRIBUTE_ntPwdHistory:
649 name = "ntPwdHistory";
652 case DRSUAPI_ATTRIBUTE_lmPwdHistory:
653 name = "lmPwdHistory";
656 case DRSUAPI_ATTRIBUTE_supplementalCredentials:
657 name = "supplementalCredentials";
658 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob;
659 print_fn = (ndr_print_fn_t)ndr_print_supplementalCredentialsBlob;
660 ptr = talloc(ctx, struct supplementalCredentialsBlob);
662 case DRSUAPI_ATTRIBUTE_priorValue:
665 case DRSUAPI_ATTRIBUTE_currentValue:
666 name = "currentValue";
668 case DRSUAPI_ATTRIBUTE_trustAuthOutgoing:
669 name = "trustAuthOutgoing";
670 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob;
671 print_fn = (ndr_print_fn_t)ndr_print_trustAuthInOutBlob;
672 ptr = talloc(ctx, struct trustAuthInOutBlob);
674 case DRSUAPI_ATTRIBUTE_trustAuthIncoming:
675 name = "trustAuthIncoming";
676 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob;
677 print_fn = (ndr_print_fn_t)ndr_print_trustAuthInOutBlob;
678 ptr = talloc(ctx, struct trustAuthInOutBlob);
680 case DRSUAPI_ATTRIBUTE_initialAuthOutgoing:
681 name = "initialAuthOutgoing";
683 case DRSUAPI_ATTRIBUTE_initialAuthIncoming:
684 name = "initialAuthIncoming";
690 if (attr->value_ctr.num_values != 1) continue;
692 if (!attr->value_ctr.values[0].blob) continue;
694 enc_data = attr->value_ctr.values[0].blob;
695 ZERO_STRUCT(plain_data);
697 werr = drsuapi_decrypt_attribute_value(ctx, gensec_skey, rcrypt,
699 enc_data, &plain_data);
700 if (!W_ERROR_IS_OK(werr)) {
701 DEBUG(0, ("Failed to decrypt %s\n", name));
706 DEBUG(0,("DN[%u] %s\n", object_id, dn));
709 DEBUGADD(0,("ATTR: %s enc.length=%lu plain.length=%lu\n",
710 name, (long)enc_data->length, (long)plain_data.length));
711 if (plain_data.length) {
712 enum ndr_err_code ndr_err;
713 dump_data(0, plain_data.data, plain_data.length);
714 if (save_values_dir) {
716 fname = talloc_asprintf(ctx, "%s/%s%02d",
721 ok = file_save(fname, plain_data.data, plain_data.length);
723 DEBUGADD(0,("Failed to save '%s'\n", fname));
730 /* Can't use '_all' because of PIDL bugs with relative pointers */
731 ndr_err = ndr_pull_struct_blob(&plain_data, ptr,
733 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
734 ndr_print_debug(print_fn, name, ptr);
736 DEBUG(0, ("Failed to decode %s\n", name));
740 dump_data(0, enc_data->data, enc_data->length);
749 static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
754 uint64_t highest_usn = 0;
755 const char *partition = NULL;
756 struct drsuapi_DsGetNCChanges r;
757 union drsuapi_DsGetNCChangesRequest req;
758 struct drsuapi_DsReplicaObjectIdentifier nc;
759 struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
760 struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
761 uint32_t out_level = 0;
762 struct GUID null_guid;
763 struct dom_sid null_sid;
764 DATA_BLOB gensec_skey;
776 ZERO_STRUCT(null_guid);
777 ZERO_STRUCT(null_sid);
779 partition = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "partition");
780 if (partition == NULL) {
781 partition = ctx->domain_dn;
782 printf("dssync:partition not specified, defaulting to %s.\n", ctx->domain_dn);
785 highest_usn = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "highest_usn", 0);
787 array[0].level = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "get_nc_changes_level", array[0].level);
789 if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
790 const struct samr_Password *nthash;
791 nthash = cli_credentials_get_nt_hash(ctx->new_dc.credentials, ctx);
793 dump_data_pw("CREDENTIALS nthash:", nthash->hash, sizeof(nthash->hash));
796 status = gensec_session_key(ctx->new_dc.drsuapi.drs_pipe->conn->security_state.generic_state,
798 if (!NT_STATUS_IS_OK(status)) {
799 printf("failed to get gensec session key: %s\n", nt_errstr(status));
803 for (i=0; i < ARRAY_SIZE(array); i++) {
804 printf("Testing DsGetNCChanges level %d\n",
807 r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
808 r.in.level = array[i].level;
810 switch (r.in.level) {
817 r.in.req->req5.destination_dsa_guid = ctx->new_dc.invocation_id;
818 r.in.req->req5.source_dsa_invocation_id = null_guid;
819 r.in.req->req5.naming_context = &nc;
820 r.in.req->req5.highwatermark.tmp_highest_usn = highest_usn;
821 r.in.req->req5.highwatermark.reserved_usn = 0;
822 r.in.req->req5.highwatermark.highest_usn = highest_usn;
823 r.in.req->req5.uptodateness_vector = NULL;
824 r.in.req->req5.replica_flags = 0;
825 if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
826 r.in.req->req5.replica_flags |= DRSUAPI_DRS_USE_COMPRESSION;
828 if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
829 r.in.req->req5.replica_flags |= DRSUAPI_DRS_WRIT_REP;
831 r.in.req->req5.replica_flags |= DRSUAPI_DRS_INIT_SYNC
832 | DRSUAPI_DRS_PER_SYNC
833 | DRSUAPI_DRS_GET_ANC
834 | DRSUAPI_DRS_NEVER_SYNCED
836 r.in.req->req5.max_object_count = 133;
837 r.in.req->req5.max_ndr_size = 1336770;
838 r.in.req->req5.extended_op = DRSUAPI_EXOP_NONE;
839 r.in.req->req5.fsmo_info = 0;
846 /* nc.dn can be set to any other ad partition */
849 r.in.req->req8.destination_dsa_guid = ctx->new_dc.invocation_id;
850 r.in.req->req8.source_dsa_invocation_id = null_guid;
851 r.in.req->req8.naming_context = &nc;
852 r.in.req->req8.highwatermark.tmp_highest_usn = highest_usn;
853 r.in.req->req8.highwatermark.reserved_usn = 0;
854 r.in.req->req8.highwatermark.highest_usn = highest_usn;
855 r.in.req->req8.uptodateness_vector = NULL;
856 r.in.req->req8.replica_flags = 0;
857 if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
858 r.in.req->req8.replica_flags |= DRSUAPI_DRS_USE_COMPRESSION;
860 if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
861 r.in.req->req8.replica_flags |= DRSUAPI_DRS_WRIT_REP;
863 r.in.req->req8.replica_flags |= DRSUAPI_DRS_INIT_SYNC
864 | DRSUAPI_DRS_PER_SYNC
865 | DRSUAPI_DRS_GET_ANC
866 | DRSUAPI_DRS_NEVER_SYNCED
868 r.in.req->req8.max_object_count = 402;
869 r.in.req->req8.max_ndr_size = 402116;
871 r.in.req->req8.extended_op = DRSUAPI_EXOP_NONE;
872 r.in.req->req8.fsmo_info = 0;
873 r.in.req->req8.partial_attribute_set = NULL;
874 r.in.req->req8.partial_attribute_set_ex = NULL;
875 r.in.req->req8.mapping_ctr.num_mappings = 0;
876 r.in.req->req8.mapping_ctr.mappings = NULL;
881 printf("Dumping AD partition: %s\n", nc.dn);
884 union drsuapi_DsGetNCChangesCtr ctr;
888 r.out.level_out = &_level;
891 if (r.in.level == 5) {
892 torture_comment(tctx,
893 "start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",
895 r.in.req->req5.highwatermark.tmp_highest_usn,
896 r.in.req->req5.highwatermark.highest_usn);
899 if (r.in.level == 8) {
900 torture_comment(tctx,
901 "start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",
903 r.in.req->req8.highwatermark.tmp_highest_usn,
904 r.in.req->req8.highwatermark.highest_usn);
907 status = dcerpc_drsuapi_DsGetNCChanges_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
908 torture_drsuapi_assert_call(tctx, ctx->new_dc.drsuapi.drs_pipe, status,
909 &r, "dcerpc_drsuapi_DsGetNCChanges");
911 if (ret == true && *r.out.level_out == 1) {
913 ctr1 = &r.out.ctr->ctr1;
914 } else if (ret == true && *r.out.level_out == 2 &&
915 r.out.ctr->ctr2.mszip1.ts) {
917 ctr1 = &r.out.ctr->ctr2.mszip1.ts->ctr1;
920 if (out_level == 1) {
921 torture_comment(tctx,
922 "end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",
924 ctr1->new_highwatermark.tmp_highest_usn,
925 ctr1->new_highwatermark.highest_usn);
927 if (!test_analyse_objects(tctx, ctx, partition, &ctr1->mapping_ctr, ctr1->object_count,
928 ctr1->first_object, &gensec_skey)) {
932 if (ctr1->more_data) {
933 r.in.req->req5.highwatermark = ctr1->new_highwatermark;
938 if (ret == true && *r.out.level_out == 6) {
940 ctr6 = &r.out.ctr->ctr6;
941 } else if (ret == true && *r.out.level_out == 7
942 && r.out.ctr->ctr7.level == 6
943 && r.out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP
944 && r.out.ctr->ctr7.ctr.mszip6.ts) {
946 ctr6 = &r.out.ctr->ctr7.ctr.mszip6.ts->ctr6;
947 } else if (ret == true && *r.out.level_out == 7
948 && r.out.ctr->ctr7.level == 6
949 && r.out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS
950 && r.out.ctr->ctr7.ctr.xpress6.ts) {
952 ctr6 = &r.out.ctr->ctr7.ctr.xpress6.ts->ctr6;
955 if (out_level == 6) {
956 torture_comment(tctx,
957 "end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",
959 ctr6->new_highwatermark.tmp_highest_usn,
960 ctr6->new_highwatermark.highest_usn);
962 if (!test_analyse_objects(tctx, ctx, partition, &ctr6->mapping_ctr, ctr6->object_count,
963 ctr6->first_object, &gensec_skey)) {
967 if (ctr6->more_data) {
968 r.in.req->req8.highwatermark = ctr6->new_highwatermark;
980 static bool test_FetchNT4Data(struct torture_context *tctx,
981 struct DsSyncTest *ctx)
984 struct drsuapi_DsGetNT4ChangeLog r;
985 union drsuapi_DsGetNT4ChangeLogRequest req;
986 union drsuapi_DsGetNT4ChangeLogInfo info;
987 uint32_t level_out = 0;
988 struct GUID null_guid;
989 struct dom_sid null_sid;
992 ZERO_STRUCT(null_guid);
993 ZERO_STRUCT(null_sid);
997 r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
1000 r.out.level_out = &level_out;
1002 req.req1.flags = lp_parm_int(tctx->lp_ctx, NULL,
1003 "dssync", "nt4changelog_flags",
1004 DRSUAPI_NT4_CHANGELOG_GET_CHANGELOG |
1005 DRSUAPI_NT4_CHANGELOG_GET_SERIAL_NUMBERS);
1006 req.req1.preferred_maximum_length = lp_parm_int(tctx->lp_ctx, NULL,
1007 "dssync", "nt4changelog_preferred_len",
1011 req.req1.restart_length = cookie.length;
1012 req.req1.restart_data = cookie.data;
1016 status = dcerpc_drsuapi_DsGetNT4ChangeLog_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
1017 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1018 torture_skip(tctx, "DsGetNT4ChangeLog not supported by target server");
1019 } else if (!NT_STATUS_IS_OK(status)) {
1020 const char *errstr = nt_errstr(status);
1021 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
1022 torture_skip(tctx, "DsGetNT4ChangeLog not supported by target server");
1025 talloc_asprintf(tctx, "dcerpc_drsuapi_DsGetNT4ChangeLog failed - %s\n",
1027 } else if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_DOMAIN_ROLE)) {
1028 torture_skip(tctx, "DsGetNT4ChangeLog not supported by target server");
1029 } else if (!W_ERROR_IS_OK(r.out.result)) {
1031 talloc_asprintf(tctx, "DsGetNT4ChangeLog failed - %s\n",
1032 win_errstr(r.out.result)));
1033 } else if (*r.out.level_out != 1) {
1035 talloc_asprintf(tctx, "DsGetNT4ChangeLog unknown level - %u\n",
1037 } else if (NT_STATUS_IS_OK(r.out.info->info1.status)) {
1038 } else if (NT_STATUS_EQUAL(r.out.info->info1.status, STATUS_MORE_ENTRIES)) {
1039 cookie.length = r.out.info->info1.restart_length;
1040 cookie.data = r.out.info->info1.restart_data;
1044 talloc_asprintf(tctx, "DsGetNT4ChangeLog failed - %s\n",
1045 nt_errstr(r.out.info->info1.status)));
1055 * DSSYNC test case setup
1057 static bool torture_dssync_tcase_setup(struct torture_context *tctx, void **data)
1060 struct DsSyncTest *ctx;
1062 *data = ctx = test_create_context(tctx);
1063 torture_assert(tctx, ctx, "test_create_context() failed");
1065 bret = _test_DsBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
1066 torture_assert(tctx, bret, "_test_DsBind() failed");
1068 bret = test_LDAPBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.ldap);
1069 torture_assert(tctx, bret, "test_LDAPBind() failed");
1071 bret = test_GetInfo(tctx, ctx);
1072 torture_assert(tctx, bret, "test_GetInfo() failed");
1074 bret = _test_DsBind(tctx, ctx, ctx->new_dc.credentials, &ctx->new_dc.drsuapi);
1075 torture_assert(tctx, bret, "_test_DsBind() failed");
1081 * DSSYNC test case cleanup
1083 static bool torture_dssync_tcase_teardown(struct torture_context *tctx, void *data)
1085 struct DsSyncTest *ctx;
1086 struct drsuapi_DsUnbind r;
1087 struct policy_handle bind_handle;
1089 ctx = talloc_get_type(data, struct DsSyncTest);
1092 r.out.bind_handle = &bind_handle;
1094 /* Unbing admin handle */
1095 r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
1096 dcerpc_drsuapi_DsUnbind_r(ctx->admin.drsuapi.drs_handle, ctx, &r);
1098 /* Unbing new_dc handle */
1099 r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
1100 dcerpc_drsuapi_DsUnbind_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
1108 * DSSYNC test case implementation
1110 void torture_drs_rpc_dssync_tcase(struct torture_suite *suite)
1112 typedef bool (*run_func) (struct torture_context *test, void *tcase_data);
1114 struct torture_test *test;
1115 struct torture_tcase *tcase = torture_suite_add_tcase(suite, "DSSYNC");
1117 torture_tcase_set_fixture(tcase,
1118 torture_dssync_tcase_setup,
1119 torture_dssync_tcase_teardown);
1121 test = torture_tcase_add_simple_test(tcase, "DC_FetchData", (run_func)test_FetchData);
1122 test = torture_tcase_add_simple_test(tcase, "FetchNT4Data", (run_func)test_FetchNT4Data);