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 "lib/ldb_wrap.h"
34 #include "torture/rpc/torture_rpc.h"
35 #include "torture/drs/proto.h"
36 #include "lib/tsocket/tsocket.h"
37 #include "libcli/resolve/resolve.h"
39 struct DsSyncBindInfo {
40 struct dcerpc_pipe *drs_pipe;
41 struct dcerpc_binding_handle *drs_handle;
42 struct drsuapi_DsBind req;
43 struct GUID bind_guid;
44 struct drsuapi_DsBindInfoCtr our_bind_info_ctr;
45 struct drsuapi_DsBindInfo28 our_bind_info28;
46 struct drsuapi_DsBindInfo28 peer_bind_info28;
47 struct policy_handle bind_handle;
50 struct DsSyncLDAPInfo {
51 struct ldb_context *ldb;
55 struct dcerpc_binding *drsuapi_binding;
58 const char *site_name;
59 const char *dest_address;
60 const char *domain_dn;
62 /* what we need to do as 'Administrator' */
64 struct cli_credentials *credentials;
65 struct DsSyncBindInfo drsuapi;
66 struct DsSyncLDAPInfo ldap;
69 /* what we need to do as the new dc machine account */
71 struct cli_credentials *credentials;
72 struct DsSyncBindInfo drsuapi;
73 struct drsuapi_DsGetDCInfo2 dc_info2;
74 struct GUID invocation_id;
75 struct GUID object_guid;
78 /* info about the old dc */
80 struct drsuapi_DsGetDomainControllerInfo dc_info;
84 static struct DsSyncTest *test_create_context(struct torture_context *tctx)
87 struct DsSyncTest *ctx;
88 struct drsuapi_DsBindInfo28 *our_bind_info28;
89 struct drsuapi_DsBindInfoCtr *our_bind_info_ctr;
90 const char *binding = torture_setting_string(tctx, "binding", NULL);
93 ctx = talloc_zero(tctx, struct DsSyncTest);
94 if (!ctx) return NULL;
96 status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
97 if (!NT_STATUS_IS_OK(status)) {
98 printf("Bad binding string %s\n", binding);
101 ctx->drsuapi_binding->flags |= DCERPC_SIGN | DCERPC_SEAL;
103 ctx->ldap_url = talloc_asprintf(ctx, "ldap://%s", ctx->drsuapi_binding->host);
105 make_nbt_name_server(&name, ctx->drsuapi_binding->host);
107 /* do an initial name resolution to find its IP */
108 status = resolve_name(lp_resolve_context(tctx->lp_ctx), &name, tctx,
109 &ctx->dest_address, tctx->ev);
110 if (!NT_STATUS_IS_OK(status)) {
111 printf("Failed to resolve %s - %s\n",
112 name.name, nt_errstr(status));
117 ctx->admin.credentials = cmdline_credentials;
119 our_bind_info28 = &ctx->admin.drsuapi.our_bind_info28;
120 our_bind_info28->supported_extensions = 0xFFFFFFFF;
121 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
122 our_bind_info28->site_guid = GUID_zero();
123 our_bind_info28->pid = 0;
124 our_bind_info28->repl_epoch = 1;
126 our_bind_info_ctr = &ctx->admin.drsuapi.our_bind_info_ctr;
127 our_bind_info_ctr->length = 28;
128 our_bind_info_ctr->info.info28 = *our_bind_info28;
130 GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
132 ctx->admin.drsuapi.req.in.bind_guid = &ctx->admin.drsuapi.bind_guid;
133 ctx->admin.drsuapi.req.in.bind_info = our_bind_info_ctr;
134 ctx->admin.drsuapi.req.out.bind_handle = &ctx->admin.drsuapi.bind_handle;
137 ctx->new_dc.credentials = cmdline_credentials;
139 our_bind_info28 = &ctx->new_dc.drsuapi.our_bind_info28;
140 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
141 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
142 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
143 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
144 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
145 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
146 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
147 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
148 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
149 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
150 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
151 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
152 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
153 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
154 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
155 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
156 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
157 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
158 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
159 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
160 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
161 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
162 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
163 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
164 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
165 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
166 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
167 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
168 if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "xpress", false)) {
169 our_bind_info28->supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
171 our_bind_info28->site_guid = GUID_zero();
172 our_bind_info28->pid = 0;
173 our_bind_info28->repl_epoch = 0;
175 our_bind_info_ctr = &ctx->new_dc.drsuapi.our_bind_info_ctr;
176 our_bind_info_ctr->length = 28;
177 our_bind_info_ctr->info.info28 = *our_bind_info28;
179 GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &ctx->new_dc.drsuapi.bind_guid);
181 ctx->new_dc.drsuapi.req.in.bind_guid = &ctx->new_dc.drsuapi.bind_guid;
182 ctx->new_dc.drsuapi.req.in.bind_info = our_bind_info_ctr;
183 ctx->new_dc.drsuapi.req.out.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
185 ctx->new_dc.invocation_id = ctx->new_dc.drsuapi.bind_guid;
192 static bool _test_DsBind(struct torture_context *tctx,
193 struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
198 status = dcerpc_pipe_connect_b(ctx,
199 &b->drs_pipe, ctx->drsuapi_binding,
201 credentials, tctx->ev, tctx->lp_ctx);
203 if (!NT_STATUS_IS_OK(status)) {
204 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
207 b->drs_handle = b->drs_pipe->binding_handle;
209 status = dcerpc_drsuapi_DsBind_r(b->drs_handle, ctx, &b->req);
210 if (!NT_STATUS_IS_OK(status)) {
211 const char *errstr = nt_errstr(status);
212 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
214 } else if (!W_ERROR_IS_OK(b->req.out.result)) {
215 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
219 ZERO_STRUCT(b->peer_bind_info28);
220 if (b->req.out.bind_info) {
221 switch (b->req.out.bind_info->length) {
223 struct drsuapi_DsBindInfo24 *info24;
224 info24 = &b->req.out.bind_info->info.info24;
225 b->peer_bind_info28.supported_extensions= info24->supported_extensions;
226 b->peer_bind_info28.site_guid = info24->site_guid;
227 b->peer_bind_info28.pid = info24->pid;
228 b->peer_bind_info28.repl_epoch = 0;
232 struct drsuapi_DsBindInfo48 *info48;
233 info48 = &b->req.out.bind_info->info.info48;
234 b->peer_bind_info28.supported_extensions= info48->supported_extensions;
235 b->peer_bind_info28.site_guid = info48->site_guid;
236 b->peer_bind_info28.pid = info48->pid;
237 b->peer_bind_info28.repl_epoch = info48->repl_epoch;
241 b->peer_bind_info28 = b->req.out.bind_info->info.info28;
244 printf("DsBind - warning: unknown BindInfo length: %u\n",
245 b->req.out.bind_info->length);
252 static bool test_LDAPBind(struct torture_context *tctx, struct DsSyncTest *ctx,
253 struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
257 struct ldb_context *ldb;
259 const char *modules_option[] = { "modules:paged_searches", NULL };
260 ctx->admin.ldap.ldb = ldb = ldb_init(ctx, tctx->ev);
265 /* Despite us loading the schema from the AD server, we need
266 * the samba handlers to get the extended DN syntax stuff */
267 ret = ldb_register_samba_handlers(ldb);
273 ldb_set_modules_dir(ldb,
276 lp_modulesdir(tctx->lp_ctx)));
278 if (ldb_set_opaque(ldb, "credentials", credentials)) {
283 if (ldb_set_opaque(ldb, "loadparm", tctx->lp_ctx)) {
288 ret = ldb_connect(ldb, ctx->ldap_url, 0, modules_option);
289 if (ret != LDB_SUCCESS) {
291 torture_assert_int_equal(tctx, ret, LDB_SUCCESS, "Failed to make LDB connection to target");
294 printf("connected to LDAP: %s\n", ctx->ldap_url);
299 static bool test_GetInfo(struct torture_context *tctx, struct DsSyncTest *ctx)
302 struct drsuapi_DsCrackNames r;
303 union drsuapi_DsNameRequest req;
304 union drsuapi_DsNameCtr ctr;
305 uint32_t level_out = 0;
306 struct drsuapi_DsNameString names[1];
308 struct cldap_socket *cldap;
309 struct cldap_netlogon search;
310 struct tsocket_address *dest_addr;
313 ret2 = tsocket_address_inet_from_strings(tctx, "ip",
315 lp_cldap_port(tctx->lp_ctx),
318 printf("failed to create tsocket_address for '%s' port %u - %s\n",
319 ctx->drsuapi_binding->host, lp_cldap_port(tctx->lp_ctx),
324 status = cldap_socket_init(ctx, NULL, NULL, dest_addr, &cldap);
325 if (!NT_STATUS_IS_OK(status)) {
326 printf("failed to setup cldap socket - %s\n",
331 r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
334 r.in.req->req1.codepage = 1252; /* western european */
335 r.in.req->req1.language = 0x00000407; /* german */
336 r.in.req->req1.count = 1;
337 r.in.req->req1.names = names;
338 r.in.req->req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
339 r.in.req->req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
340 r.in.req->req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
341 names[0].str = talloc_asprintf(ctx, "%s\\", lp_workgroup(tctx->lp_ctx));
343 r.out.level_out = &level_out;
346 status = dcerpc_drsuapi_DsCrackNames_r(ctx->admin.drsuapi.drs_handle, ctx, &r);
347 if (!NT_STATUS_IS_OK(status)) {
348 const char *errstr = nt_errstr(status);
349 printf("dcerpc_drsuapi_DsCrackNames failed - %s\n", errstr);
351 } else if (!W_ERROR_IS_OK(r.out.result)) {
352 printf("DsCrackNames failed - %s\n", win_errstr(r.out.result));
356 ctx->domain_dn = r.out.ctr->ctr1->array[0].result_name;
359 search.in.dest_address = NULL;
360 search.in.dest_port = 0;
361 search.in.acct_control = -1;
362 search.in.version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
363 search.in.map_response = true;
364 status = cldap_netlogon(cldap, ctx, &search);
365 if (!NT_STATUS_IS_OK(status)) {
366 const char *errstr = nt_errstr(status);
367 ctx->site_name = talloc_asprintf(ctx, "%s", "Default-First-Site-Name");
368 printf("cldap_netlogon() returned %s. Defaulting to Site-Name: %s\n", errstr, ctx->site_name);
370 ctx->site_name = talloc_steal(ctx, search.out.netlogon.data.nt5_ex.client_site);
371 printf("cldap_netlogon() returned Client Site-Name: %s.\n",ctx->site_name);
372 printf("cldap_netlogon() returned Server Site-Name: %s.\n",search.out.netlogon.data.nt5_ex.server_site);
375 if (!ctx->domain_dn) {
376 struct ldb_context *ldb = ldb_init(ctx, tctx->ev);
377 struct ldb_dn *dn = samdb_dns_domain_to_dn(ldb, ctx, search.out.netlogon.data.nt5_ex.dns_domain);
378 ctx->domain_dn = ldb_dn_alloc_linearized(ctx, dn);
386 static bool test_analyse_objects(struct torture_context *tctx,
387 struct DsSyncTest *ctx,
388 const char *partition,
389 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
390 uint32_t object_count,
391 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
392 const DATA_BLOB *gensec_skey)
394 static uint32_t object_id;
395 const char *save_values_dir;
396 const struct drsuapi_DsReplicaObjectListItemEx *cur;
397 struct ldb_context *ldb = ctx->admin.ldap.ldb;
398 struct ldb_dn *deleted_dn;
401 struct dsdb_extended_replicated_objects *objs;
402 struct ldb_extended_dn_control *extended_dn_ctrl;
405 if (!dsdb_get_schema(ldb, NULL)) {
406 struct dsdb_schema *ldap_schema;
407 struct ldb_result *a_res;
408 struct ldb_result *c_res;
409 struct ldb_dn *schema_dn = ldb_get_schema_basedn(ldb);
410 ldap_schema = dsdb_new_schema(ctx);
414 status = dsdb_load_prefixmap_from_drsuapi(ldap_schema, mapping_ctr);
417 * load the attribute definitions
419 ret = ldb_search(ldb, ldap_schema, &a_res,
420 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
421 "(objectClass=attributeSchema)");
422 if (ret != LDB_SUCCESS) {
423 err_msg = talloc_asprintf(tctx,
424 "failed to search attributeSchema objects: %s",
426 torture_fail(tctx, err_msg);
430 * load the objectClass definitions
432 ret = ldb_search(ldb, ldap_schema, &c_res,
433 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
434 "(objectClass=classSchema)");
435 if (ret != LDB_SUCCESS) {
436 err_msg = talloc_asprintf(tctx,
437 "failed to search classSchema objects: %s",
439 torture_fail(tctx, err_msg);
443 for (i=0; i < a_res->count; i++) {
444 status = dsdb_attribute_from_ldb(ldb, ldap_schema, a_res->msgs[i]);
445 torture_assert_werr_ok(tctx, status,
446 talloc_asprintf(tctx,
447 "dsdb_attribute_from_ldb() failed for: %s",
448 ldb_dn_get_linearized(a_res->msgs[i]->dn)));
451 for (i=0; i < c_res->count; i++) {
452 status = dsdb_class_from_ldb(ldap_schema, c_res->msgs[i]);
453 torture_assert_werr_ok(tctx, status,
454 talloc_asprintf(tctx,
455 "dsdb_class_from_ldb() failed for: %s",
456 ldb_dn_get_linearized(c_res->msgs[i]->dn)));
460 ret = dsdb_set_schema(ldb, ldap_schema);
461 if (ret != LDB_SUCCESS) {
463 talloc_asprintf(tctx, "dsdb_set_schema() failed: %s", ldb_strerror(ret)));
467 status = dsdb_extended_replicated_objects_convert(ldb,
476 torture_assert_werr_ok(tctx, status, "dsdb_extended_replicated_objects_convert() failed!");
478 extended_dn_ctrl = talloc(objs, struct ldb_extended_dn_control);
479 extended_dn_ctrl->type = 1;
481 deleted_dn = ldb_dn_new(objs, ldb, partition);
482 ldb_dn_add_child_fmt(deleted_dn, "CN=Deleted Objects");
484 for (i=0; i < object_count; i++) {
485 struct ldb_request *search_req;
486 struct ldb_result *res;
487 struct ldb_message *new_msg, *drs_msg, *ldap_msg;
488 const char **attrs = talloc_array(objs, const char *, objs->objects[i].msg->num_elements+1);
489 for (j=0; j < objs->objects[i].msg->num_elements; j++) {
490 attrs[j] = objs->objects[i].msg->elements[j].name;
493 res = talloc_zero(objs, struct ldb_result);
495 return LDB_ERR_OPERATIONS_ERROR;
497 ret = ldb_build_search_req(&search_req, ldb, objs,
498 objs->objects[i].msg->dn,
504 ldb_search_default_callback,
506 if (ret != LDB_SUCCESS) {
509 talloc_steal(search_req, res);
510 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
511 if (ret != LDB_SUCCESS) {
515 ret = ldb_request_add_control(search_req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_dn_ctrl);
516 if (ret != LDB_SUCCESS) {
520 ret = ldb_request(ldb, search_req);
521 if (ret == LDB_SUCCESS) {
522 ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
525 torture_assert_int_equal(tctx, ret, LDB_SUCCESS,
526 talloc_asprintf(tctx,
527 "Could not re-fetch object just delivered over DRS: %s",
528 ldb_errstring(ldb)));
529 torture_assert_int_equal(tctx, res->count, 1, "Could not re-fetch object just delivered over DRS");
530 ldap_msg = res->msgs[0];
531 for (j=0; j < ldap_msg->num_elements; j++) {
532 ldap_msg->elements[j].flags = LDB_FLAG_MOD_ADD;
533 /* 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 */
534 if ((ldb_attr_cmp(ldap_msg->elements[j].name, "nTSecurityDescriptor") == 0) &&
535 (ldb_dn_compare(ldap_msg->dn, deleted_dn) == 0)) {
536 ldb_msg_remove_element(ldap_msg, &ldap_msg->elements[j]);
542 drs_msg = ldb_msg_canonicalize(ldb, objs->objects[i].msg);
543 talloc_steal(search_req, drs_msg);
545 for (j=0; j < drs_msg->num_elements; j++) {
546 if (drs_msg->elements[j].num_values == 0) {
547 ldb_msg_remove_element(drs_msg, &drs_msg->elements[j]);
551 /* For unknown reasons, there is no nTSecurityDescriptor on cn=deleted objects over LDAP, but there is over DRS! */
552 } else if ((ldb_attr_cmp(drs_msg->elements[j].name, "nTSecurityDescriptor") == 0) &&
553 (ldb_dn_compare(drs_msg->dn, deleted_dn) == 0)) {
554 ldb_msg_remove_element(drs_msg, &drs_msg->elements[j]);
557 } else if (ldb_attr_cmp(drs_msg->elements[j].name, "unicodePwd") == 0 ||
558 ldb_attr_cmp(drs_msg->elements[j].name, "dBCSPwd") == 0 ||
559 ldb_attr_cmp(drs_msg->elements[j].name, "ntPwdHistory") == 0 ||
560 ldb_attr_cmp(drs_msg->elements[j].name, "lmPwdHistory") == 0 ||
561 ldb_attr_cmp(drs_msg->elements[j].name, "supplementalCredentials") == 0 ||
562 ldb_attr_cmp(drs_msg->elements[j].name, "priorValue") == 0 ||
563 ldb_attr_cmp(drs_msg->elements[j].name, "currentValue") == 0 ||
564 ldb_attr_cmp(drs_msg->elements[j].name, "trustAuthOutgoing") == 0 ||
565 ldb_attr_cmp(drs_msg->elements[j].name, "trustAuthIncoming") == 0 ||
566 ldb_attr_cmp(drs_msg->elements[j].name, "initialAuthOutgoing") == 0 ||
567 ldb_attr_cmp(drs_msg->elements[j].name, "initialAuthIncoming") == 0) {
569 /* These are not shown over LDAP, so we need to skip them for the comparison */
570 ldb_msg_remove_element(drs_msg, &drs_msg->elements[j]);
574 drs_msg->elements[j].flags = LDB_FLAG_MOD_ADD;
579 new_msg = ldb_msg_diff(ldb, drs_msg, ldap_msg);
580 talloc_steal(search_req, new_msg);
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 ldif.msg = ldb_msg_diff(ldb, ldap_msg, drs_msg);
590 s = talloc_asprintf_append(s,
591 "\n# Difference in between LDAP and DRS objects: \n%s\n",
592 ldb_ldif_write_string(ldb, new_msg, &ldif));
594 s = talloc_asprintf_append(s,
595 "# Should have no objects in 'difference' message. Diff elements: %d",
596 new_msg->num_elements);
597 torture_fail(tctx, s);
600 /* search_req is used as a tmp talloc context in the above */
601 talloc_free(search_req);
604 if (!lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
609 save_values_dir = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "save_pwd_blobs_dir");
611 for (cur = first_object; cur; cur = cur->next_object) {
613 struct dom_sid *sid = NULL;
615 bool dn_printed = false;
617 if (!cur->object.identifier) continue;
619 dn = cur->object.identifier->dn;
620 if (cur->object.identifier->sid.num_auths > 0) {
621 sid = &cur->object.identifier->sid;
622 rid = sid->sub_auths[sid->num_auths - 1];
625 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
627 const char *name = NULL;
629 DATA_BLOB *enc_data = NULL;
630 DATA_BLOB plain_data;
631 struct drsuapi_DsReplicaAttribute *attr;
632 ndr_pull_flags_fn_t pull_fn = NULL;
633 ndr_print_fn_t print_fn = NULL;
635 attr = &cur->object.attribute_ctr.attributes[i];
637 switch (attr->attid) {
638 case DRSUAPI_ATTRIBUTE_dBCSPwd:
642 case DRSUAPI_ATTRIBUTE_unicodePwd:
646 case DRSUAPI_ATTRIBUTE_ntPwdHistory:
647 name = "ntPwdHistory";
650 case DRSUAPI_ATTRIBUTE_lmPwdHistory:
651 name = "lmPwdHistory";
654 case DRSUAPI_ATTRIBUTE_supplementalCredentials:
655 name = "supplementalCredentials";
656 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob;
657 print_fn = (ndr_print_fn_t)ndr_print_supplementalCredentialsBlob;
658 ptr = talloc(ctx, struct supplementalCredentialsBlob);
660 case DRSUAPI_ATTRIBUTE_priorValue:
663 case DRSUAPI_ATTRIBUTE_currentValue:
664 name = "currentValue";
666 case DRSUAPI_ATTRIBUTE_trustAuthOutgoing:
667 name = "trustAuthOutgoing";
668 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob;
669 print_fn = (ndr_print_fn_t)ndr_print_trustAuthInOutBlob;
670 ptr = talloc(ctx, struct trustAuthInOutBlob);
672 case DRSUAPI_ATTRIBUTE_trustAuthIncoming:
673 name = "trustAuthIncoming";
674 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob;
675 print_fn = (ndr_print_fn_t)ndr_print_trustAuthInOutBlob;
676 ptr = talloc(ctx, struct trustAuthInOutBlob);
678 case DRSUAPI_ATTRIBUTE_initialAuthOutgoing:
679 name = "initialAuthOutgoing";
681 case DRSUAPI_ATTRIBUTE_initialAuthIncoming:
682 name = "initialAuthIncoming";
688 if (attr->value_ctr.num_values != 1) continue;
690 if (!attr->value_ctr.values[0].blob) continue;
692 enc_data = attr->value_ctr.values[0].blob;
693 ZERO_STRUCT(plain_data);
695 werr = drsuapi_decrypt_attribute_value(ctx, gensec_skey, rcrypt,
697 enc_data, &plain_data);
698 if (!W_ERROR_IS_OK(werr)) {
699 DEBUG(0, ("Failed to decrypt %s\n", name));
704 DEBUG(0,("DN[%u] %s\n", object_id, dn));
707 DEBUGADD(0,("ATTR: %s enc.length=%lu plain.length=%lu\n",
708 name, (long)enc_data->length, (long)plain_data.length));
709 if (plain_data.length) {
710 enum ndr_err_code ndr_err;
711 dump_data(0, plain_data.data, plain_data.length);
712 if (save_values_dir) {
714 fname = talloc_asprintf(ctx, "%s/%s%02d",
719 ok = file_save(fname, plain_data.data, plain_data.length);
721 DEBUGADD(0,("Failed to save '%s'\n", fname));
728 /* Can't use '_all' because of PIDL bugs with relative pointers */
729 ndr_err = ndr_pull_struct_blob(&plain_data, ptr,
731 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
732 ndr_print_debug(print_fn, name, ptr);
734 DEBUG(0, ("Failed to decode %s\n", name));
738 dump_data(0, enc_data->data, enc_data->length);
747 static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
752 uint64_t highest_usn = 0;
753 const char *partition = NULL;
754 struct drsuapi_DsGetNCChanges r;
755 union drsuapi_DsGetNCChangesRequest req;
756 struct drsuapi_DsReplicaObjectIdentifier nc;
757 struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
758 struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
759 uint32_t out_level = 0;
760 struct GUID null_guid;
761 struct dom_sid null_sid;
762 DATA_BLOB gensec_skey;
774 ZERO_STRUCT(null_guid);
775 ZERO_STRUCT(null_sid);
777 partition = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "partition");
778 if (partition == NULL) {
779 partition = ctx->domain_dn;
780 printf("dssync:partition not specified, defaulting to %s.\n", ctx->domain_dn);
783 highest_usn = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "highest_usn", 0);
785 array[0].level = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "get_nc_changes_level", array[0].level);
787 if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
788 const struct samr_Password *nthash;
789 nthash = cli_credentials_get_nt_hash(ctx->new_dc.credentials, ctx);
791 dump_data_pw("CREDENTIALS nthash:", nthash->hash, sizeof(nthash->hash));
794 status = gensec_session_key(ctx->new_dc.drsuapi.drs_pipe->conn->security_state.generic_state,
796 if (!NT_STATUS_IS_OK(status)) {
797 printf("failed to get gensec session key: %s\n", nt_errstr(status));
801 for (i=0; i < ARRAY_SIZE(array); i++) {
802 printf("Testing DsGetNCChanges level %d\n",
805 r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
806 r.in.level = array[i].level;
808 switch (r.in.level) {
815 r.in.req->req5.destination_dsa_guid = ctx->new_dc.invocation_id;
816 r.in.req->req5.source_dsa_invocation_id = null_guid;
817 r.in.req->req5.naming_context = &nc;
818 r.in.req->req5.highwatermark.tmp_highest_usn = highest_usn;
819 r.in.req->req5.highwatermark.reserved_usn = 0;
820 r.in.req->req5.highwatermark.highest_usn = highest_usn;
821 r.in.req->req5.uptodateness_vector = NULL;
822 r.in.req->req5.replica_flags = 0;
823 if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
824 r.in.req->req5.replica_flags |= DRSUAPI_DRS_USE_COMPRESSION;
826 if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
827 r.in.req->req5.replica_flags |= DRSUAPI_DRS_WRIT_REP;
829 r.in.req->req5.replica_flags |= DRSUAPI_DRS_INIT_SYNC
830 | DRSUAPI_DRS_PER_SYNC
831 | DRSUAPI_DRS_GET_ANC
832 | DRSUAPI_DRS_NEVER_SYNCED
834 r.in.req->req5.max_object_count = 133;
835 r.in.req->req5.max_ndr_size = 1336770;
836 r.in.req->req5.extended_op = DRSUAPI_EXOP_NONE;
837 r.in.req->req5.fsmo_info = 0;
844 /* nc.dn can be set to any other ad partition */
847 r.in.req->req8.destination_dsa_guid = ctx->new_dc.invocation_id;
848 r.in.req->req8.source_dsa_invocation_id = null_guid;
849 r.in.req->req8.naming_context = &nc;
850 r.in.req->req8.highwatermark.tmp_highest_usn = highest_usn;
851 r.in.req->req8.highwatermark.reserved_usn = 0;
852 r.in.req->req8.highwatermark.highest_usn = highest_usn;
853 r.in.req->req8.uptodateness_vector = NULL;
854 r.in.req->req8.replica_flags = 0;
855 if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
856 r.in.req->req8.replica_flags |= DRSUAPI_DRS_USE_COMPRESSION;
858 if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
859 r.in.req->req8.replica_flags |= DRSUAPI_DRS_WRIT_REP;
861 r.in.req->req8.replica_flags |= DRSUAPI_DRS_INIT_SYNC
862 | DRSUAPI_DRS_PER_SYNC
863 | DRSUAPI_DRS_GET_ANC
864 | DRSUAPI_DRS_NEVER_SYNCED
866 r.in.req->req8.max_object_count = 402;
867 r.in.req->req8.max_ndr_size = 402116;
869 r.in.req->req8.extended_op = DRSUAPI_EXOP_NONE;
870 r.in.req->req8.fsmo_info = 0;
871 r.in.req->req8.partial_attribute_set = NULL;
872 r.in.req->req8.partial_attribute_set_ex = NULL;
873 r.in.req->req8.mapping_ctr.num_mappings = 0;
874 r.in.req->req8.mapping_ctr.mappings = NULL;
879 printf("Dumping AD partition: %s\n", nc.dn);
882 union drsuapi_DsGetNCChangesCtr ctr;
886 r.out.level_out = &_level;
889 if (r.in.level == 5) {
890 torture_comment(tctx,
891 "start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",
893 r.in.req->req5.highwatermark.tmp_highest_usn,
894 r.in.req->req5.highwatermark.highest_usn);
897 if (r.in.level == 8) {
898 torture_comment(tctx,
899 "start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",
901 r.in.req->req8.highwatermark.tmp_highest_usn,
902 r.in.req->req8.highwatermark.highest_usn);
905 status = dcerpc_drsuapi_DsGetNCChanges_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
906 torture_drsuapi_assert_call(tctx, ctx->new_dc.drsuapi.drs_pipe, status,
907 &r, "dcerpc_drsuapi_DsGetNCChanges");
909 if (ret == true && *r.out.level_out == 1) {
911 ctr1 = &r.out.ctr->ctr1;
912 } else if (ret == true && *r.out.level_out == 2 &&
913 r.out.ctr->ctr2.mszip1.ts) {
915 ctr1 = &r.out.ctr->ctr2.mszip1.ts->ctr1;
918 if (out_level == 1) {
919 torture_comment(tctx,
920 "end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",
922 ctr1->new_highwatermark.tmp_highest_usn,
923 ctr1->new_highwatermark.highest_usn);
925 if (!test_analyse_objects(tctx, ctx, partition, &ctr1->mapping_ctr, ctr1->object_count,
926 ctr1->first_object, &gensec_skey)) {
930 if (ctr1->more_data) {
931 r.in.req->req5.highwatermark = ctr1->new_highwatermark;
936 if (ret == true && *r.out.level_out == 6) {
938 ctr6 = &r.out.ctr->ctr6;
939 } else if (ret == true && *r.out.level_out == 7
940 && r.out.ctr->ctr7.level == 6
941 && r.out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP
942 && r.out.ctr->ctr7.ctr.mszip6.ts) {
944 ctr6 = &r.out.ctr->ctr7.ctr.mszip6.ts->ctr6;
945 } else if (ret == true && *r.out.level_out == 7
946 && r.out.ctr->ctr7.level == 6
947 && r.out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS
948 && r.out.ctr->ctr7.ctr.xpress6.ts) {
950 ctr6 = &r.out.ctr->ctr7.ctr.xpress6.ts->ctr6;
953 if (out_level == 6) {
954 torture_comment(tctx,
955 "end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",
957 ctr6->new_highwatermark.tmp_highest_usn,
958 ctr6->new_highwatermark.highest_usn);
960 if (!test_analyse_objects(tctx, ctx, partition, &ctr6->mapping_ctr, ctr6->object_count,
961 ctr6->first_object, &gensec_skey)) {
965 if (ctr6->more_data) {
966 r.in.req->req8.highwatermark = ctr6->new_highwatermark;
978 static bool test_FetchNT4Data(struct torture_context *tctx,
979 struct DsSyncTest *ctx)
982 struct drsuapi_DsGetNT4ChangeLog r;
983 union drsuapi_DsGetNT4ChangeLogRequest req;
984 union drsuapi_DsGetNT4ChangeLogInfo info;
985 uint32_t level_out = 0;
986 struct GUID null_guid;
987 struct dom_sid null_sid;
990 ZERO_STRUCT(null_guid);
991 ZERO_STRUCT(null_sid);
995 r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
998 r.out.level_out = &level_out;
1000 req.req1.flags = lp_parm_int(tctx->lp_ctx, NULL,
1001 "dssync", "nt4changelog_flags",
1002 DRSUAPI_NT4_CHANGELOG_GET_CHANGELOG |
1003 DRSUAPI_NT4_CHANGELOG_GET_SERIAL_NUMBERS);
1004 req.req1.preferred_maximum_length = lp_parm_int(tctx->lp_ctx, NULL,
1005 "dssync", "nt4changelog_preferred_len",
1009 req.req1.restart_length = cookie.length;
1010 req.req1.restart_data = cookie.data;
1014 status = dcerpc_drsuapi_DsGetNT4ChangeLog_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
1015 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1016 torture_skip(tctx, "DsGetNT4ChangeLog not supported by target server");
1017 } else if (!NT_STATUS_IS_OK(status)) {
1018 const char *errstr = nt_errstr(status);
1019 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
1020 torture_skip(tctx, "DsGetNT4ChangeLog not supported by target server");
1023 talloc_asprintf(tctx, "dcerpc_drsuapi_DsGetNT4ChangeLog failed - %s\n",
1025 } else if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_DOMAIN_ROLE)) {
1026 torture_skip(tctx, "DsGetNT4ChangeLog not supported by target server");
1027 } else if (!W_ERROR_IS_OK(r.out.result)) {
1029 talloc_asprintf(tctx, "DsGetNT4ChangeLog failed - %s\n",
1030 win_errstr(r.out.result)));
1031 } else if (*r.out.level_out != 1) {
1033 talloc_asprintf(tctx, "DsGetNT4ChangeLog unknown level - %u\n",
1035 } else if (NT_STATUS_IS_OK(r.out.info->info1.status)) {
1036 } else if (NT_STATUS_EQUAL(r.out.info->info1.status, STATUS_MORE_ENTRIES)) {
1037 cookie.length = r.out.info->info1.restart_length;
1038 cookie.data = r.out.info->info1.restart_data;
1042 talloc_asprintf(tctx, "DsGetNT4ChangeLog failed - %s\n",
1043 nt_errstr(r.out.info->info1.status)));
1053 * DSSYNC test case setup
1055 static bool torture_dssync_tcase_setup(struct torture_context *tctx, void **data)
1058 struct DsSyncTest *ctx;
1060 *data = ctx = test_create_context(tctx);
1061 torture_assert(tctx, ctx, "test_create_context() failed");
1063 bret = _test_DsBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
1064 torture_assert(tctx, bret, "_test_DsBind() failed");
1066 bret = test_LDAPBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.ldap);
1067 torture_assert(tctx, bret, "test_LDAPBind() failed");
1069 bret = test_GetInfo(tctx, ctx);
1070 torture_assert(tctx, bret, "test_GetInfo() failed");
1072 bret = _test_DsBind(tctx, ctx, ctx->new_dc.credentials, &ctx->new_dc.drsuapi);
1073 torture_assert(tctx, bret, "_test_DsBind() failed");
1079 * DSSYNC test case cleanup
1081 static bool torture_dssync_tcase_teardown(struct torture_context *tctx, void *data)
1083 struct DsSyncTest *ctx;
1084 struct drsuapi_DsUnbind r;
1085 struct policy_handle bind_handle;
1087 ctx = talloc_get_type(data, struct DsSyncTest);
1090 r.out.bind_handle = &bind_handle;
1092 /* Unbing admin handle */
1093 r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
1094 dcerpc_drsuapi_DsUnbind_r(ctx->admin.drsuapi.drs_handle, ctx, &r);
1096 /* Unbing new_dc handle */
1097 r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
1098 dcerpc_drsuapi_DsUnbind_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
1106 * DSSYNC test case implementation
1108 void torture_drs_rpc_dssync_tcase(struct torture_suite *suite)
1110 typedef bool (*run_func) (struct torture_context *test, void *tcase_data);
1112 struct torture_test *test;
1113 struct torture_tcase *tcase = torture_suite_add_tcase(suite, "DSSYNC");
1115 torture_tcase_set_fixture(tcase,
1116 torture_dssync_tcase_setup,
1117 torture_dssync_tcase_teardown);
1119 test = torture_tcase_add_simple_test(tcase, "DC_FetchData", (run_func)test_FetchData);
1120 test = torture_tcase_add_simple_test(tcase, "FetchNT4Data", (run_func)test_FetchNT4Data);