67fde7c2b6137247417fb0a68d81befc225fc912
[samba.git] / source4 / torture / drs / rpc / dssync.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    DsGetNCChanges replication test
5
6    Copyright (C) Stefan (metze) Metzmacher 2005
7    Copyright (C) Brad Henry 2005
8
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.
13
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.
18
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/>.
21 */
22
23 #include "includes.h"
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"
37 #include "lib/util/util_paths.h"
38
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;
48 };
49
50 struct DsSyncLDAPInfo {
51         struct ldb_context *ldb;
52 };
53
54 struct DsSyncTest {
55         struct dcerpc_binding *drsuapi_binding;
56
57         const char *ldap_url;
58         const char *dest_address;
59         const char *domain_dn;
60         const char *config_dn;
61         const char *schema_dn;
62
63         /* what we need to do as 'Administrator' */
64         struct {
65                 struct cli_credentials *credentials;
66                 struct DsSyncBindInfo drsuapi;
67                 struct DsSyncLDAPInfo ldap;
68         } admin;
69
70         /* what we need to do as the new dc machine account */
71         struct {
72                 struct cli_credentials *credentials;
73                 struct DsSyncBindInfo drsuapi;
74                 struct drsuapi_DsGetDCInfo2 dc_info2;
75                 struct GUID invocation_id;
76                 struct GUID object_guid;
77         } new_dc;
78
79         /* info about the old dc */
80         struct {
81                 struct drsuapi_DsGetDomainControllerInfo dc_info;
82         } old_dc;
83 };
84
85 static struct DsSyncTest *test_create_context(struct torture_context *tctx)
86 {
87         NTSTATUS status;
88         struct DsSyncTest *ctx;
89         struct drsuapi_DsBindInfo28 *our_bind_info28;
90         struct drsuapi_DsBindInfoCtr *our_bind_info_ctr;
91         const char *binding = torture_setting_string(tctx, "binding", NULL);
92         const char *host;
93         struct nbt_name name;
94
95         ctx = talloc_zero(tctx, struct DsSyncTest);
96         if (!ctx) return NULL;
97
98         status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
99         if (!NT_STATUS_IS_OK(status)) {
100                 printf("Bad binding string %s\n", binding);
101                 return NULL;
102         }
103         status = dcerpc_binding_set_flags(ctx->drsuapi_binding,
104                                           DCERPC_SIGN | DCERPC_SEAL, 0);
105         if (!NT_STATUS_IS_OK(status)) {
106                 printf("dcerpc_binding_set_flags - %s\n", nt_errstr(status));
107                 return NULL;
108         }
109
110         host = dcerpc_binding_get_string_option(ctx->drsuapi_binding, "host");
111
112         ctx->ldap_url = talloc_asprintf(ctx, "ldap://%s", host);
113
114         make_nbt_name_server(&name, host);
115
116         /* do an initial name resolution to find its IP */
117         status = resolve_name_ex(lpcfg_resolve_context(tctx->lp_ctx),
118                                  0, 0, &name, tctx,
119                                  &ctx->dest_address, tctx->ev);
120         if (!NT_STATUS_IS_OK(status)) {
121                 printf("Failed to resolve %s - %s\n",
122                        name.name, nt_errstr(status));
123                 return NULL;
124         }
125
126         /* ctx->admin ...*/
127         ctx->admin.credentials  = popt_get_cmdline_credentials();
128
129         our_bind_info28                         = &ctx->admin.drsuapi.our_bind_info28;
130         our_bind_info28->supported_extensions   = 0xFFFFFFFF;
131         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
132         our_bind_info28->site_guid              = GUID_zero();
133         our_bind_info28->pid                    = 0;
134         our_bind_info28->repl_epoch             = 1;
135
136         our_bind_info_ctr                       = &ctx->admin.drsuapi.our_bind_info_ctr;
137         our_bind_info_ctr->length               = 28;
138         our_bind_info_ctr->info.info28          = *our_bind_info28;
139
140         GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
141
142         ctx->admin.drsuapi.req.in.bind_guid             = &ctx->admin.drsuapi.bind_guid;
143         ctx->admin.drsuapi.req.in.bind_info             = our_bind_info_ctr;
144         ctx->admin.drsuapi.req.out.bind_handle          = &ctx->admin.drsuapi.bind_handle;
145
146         /* ctx->new_dc ...*/
147         ctx->new_dc.credentials = popt_get_cmdline_credentials();
148
149         our_bind_info28                         = &ctx->new_dc.drsuapi.our_bind_info28;
150         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
151         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
152         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
153         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
154         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
155         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
156         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
157         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
158         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
159         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
160         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
161         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
162         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
163         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
164         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
165         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
166         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
167         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
168         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
169         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
170         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
171         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
172         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
173         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
174         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
175         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
176         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
177         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
178         if (lpcfg_parm_bool(tctx->lp_ctx, NULL, "dssync", "xpress", false)) {
179                 our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
180         }
181         our_bind_info28->site_guid              = GUID_zero();
182         our_bind_info28->pid                    = 0;
183         our_bind_info28->repl_epoch             = 0;
184
185         our_bind_info_ctr                       = &ctx->new_dc.drsuapi.our_bind_info_ctr;
186         our_bind_info_ctr->length               = 28;
187         our_bind_info_ctr->info.info28          = *our_bind_info28;
188
189         GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &ctx->new_dc.drsuapi.bind_guid);
190
191         ctx->new_dc.drsuapi.req.in.bind_guid            = &ctx->new_dc.drsuapi.bind_guid;
192         ctx->new_dc.drsuapi.req.in.bind_info            = our_bind_info_ctr;
193         ctx->new_dc.drsuapi.req.out.bind_handle         = &ctx->new_dc.drsuapi.bind_handle;
194
195         ctx->new_dc.invocation_id                       = ctx->new_dc.drsuapi.bind_guid;
196
197         /* ctx->old_dc ...*/
198
199         return ctx;
200 }
201
202 static bool _test_DsBind(struct torture_context *tctx,
203                          struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
204 {
205         NTSTATUS status;
206         bool ret = true;
207
208         status = dcerpc_pipe_connect_b(ctx,
209                                        &b->drs_pipe, ctx->drsuapi_binding,
210                                        &ndr_table_drsuapi,
211                                        credentials, tctx->ev, tctx->lp_ctx);
212
213         if (!NT_STATUS_IS_OK(status)) {
214                 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
215                 return false;
216         }
217         b->drs_handle = b->drs_pipe->binding_handle;
218
219         status = dcerpc_drsuapi_DsBind_r(b->drs_handle, ctx, &b->req);
220         if (!NT_STATUS_IS_OK(status)) {
221                 const char *errstr = nt_errstr(status);
222                 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
223                 ret = false;
224         } else if (!W_ERROR_IS_OK(b->req.out.result)) {
225                 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
226                 ret = false;
227         }
228
229         ZERO_STRUCT(b->peer_bind_info28);
230         if (b->req.out.bind_info) {
231                 switch (b->req.out.bind_info->length) {
232                 case 24: {
233                         struct drsuapi_DsBindInfo24 *info24;
234                         info24 = &b->req.out.bind_info->info.info24;
235                         b->peer_bind_info28.supported_extensions= info24->supported_extensions;
236                         b->peer_bind_info28.site_guid           = info24->site_guid;
237                         b->peer_bind_info28.pid                 = info24->pid;
238                         b->peer_bind_info28.repl_epoch          = 0;
239                         break;
240                 }
241                 case 28: {
242                         b->peer_bind_info28 = b->req.out.bind_info->info.info28;
243                         break;
244                 }
245                 case 32: {
246                         struct drsuapi_DsBindInfo32 *info32;
247                         info32 = &b->req.out.bind_info->info.info32;
248                         b->peer_bind_info28.supported_extensions= info32->supported_extensions;
249                         b->peer_bind_info28.site_guid           = info32->site_guid;
250                         b->peer_bind_info28.pid                 = info32->pid;
251                         b->peer_bind_info28.repl_epoch          = info32->repl_epoch;
252                         break;
253                 }
254                 case 48: {
255                         struct drsuapi_DsBindInfo48 *info48;
256                         info48 = &b->req.out.bind_info->info.info48;
257                         b->peer_bind_info28.supported_extensions= info48->supported_extensions;
258                         b->peer_bind_info28.site_guid           = info48->site_guid;
259                         b->peer_bind_info28.pid                 = info48->pid;
260                         b->peer_bind_info28.repl_epoch          = info48->repl_epoch;
261                         break;
262                 }
263                 case 52: {
264                         struct drsuapi_DsBindInfo52 *info52;
265                         info52 = &b->req.out.bind_info->info.info52;
266                         b->peer_bind_info28.supported_extensions= info52->supported_extensions;
267                         b->peer_bind_info28.site_guid           = info52->site_guid;
268                         b->peer_bind_info28.pid                 = info52->pid;
269                         b->peer_bind_info28.repl_epoch          = info52->repl_epoch;
270                         break;
271                 }
272                 default:
273                         printf("DsBind - warning: unknown BindInfo length: %u\n",
274                                b->req.out.bind_info->length);
275                 }
276         }
277
278         return ret;
279 }
280
281 static bool test_LDAPBind(struct torture_context *tctx, struct DsSyncTest *ctx,
282                           struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
283 {
284         bool ret = true;
285
286         struct ldb_context *ldb;
287
288         const char *modules_option[] = { "modules:paged_searches", NULL };
289         ctx->admin.ldap.ldb = ldb = ldb_init(ctx, tctx->ev);
290         if (ldb == NULL) {
291                 return false;
292         }
293
294         /* Despite us loading the schema from the AD server, we need
295          * the samba handlers to get the extended DN syntax stuff */
296         ret = ldb_register_samba_handlers(ldb);
297         if (ret != LDB_SUCCESS) {
298                 talloc_free(ldb);
299                 return NULL;
300         }
301
302         ldb_set_modules_dir(ldb, modules_path(ldb, "ldb"));
303
304         if (ldb_set_opaque(ldb, "credentials", credentials)) {
305                 talloc_free(ldb);
306                 return NULL;
307         }
308
309         if (ldb_set_opaque(ldb, "loadparm", tctx->lp_ctx)) {
310                 talloc_free(ldb);
311                 return NULL;
312         }
313
314         ret = ldb_connect(ldb, ctx->ldap_url, 0, modules_option);
315         if (ret != LDB_SUCCESS) {
316                 talloc_free(ldb);
317                 torture_assert_int_equal(tctx, ret, LDB_SUCCESS, "Failed to make LDB connection to target");
318         }
319
320         printf("connected to LDAP: %s\n", ctx->ldap_url);
321
322         return true;
323 }
324
325 static bool test_GetInfo(struct torture_context *tctx, struct DsSyncTest *ctx)
326 {
327         struct ldb_context *ldb = ctx->admin.ldap.ldb;
328
329         /* We must have LDB connection ready by this time */
330         SMB_ASSERT(ldb != NULL);
331
332         ctx->domain_dn = ldb_dn_get_linearized(ldb_get_default_basedn(ldb));
333         torture_assert(tctx, ctx->domain_dn != NULL, "Failed to get Domain DN");
334
335         ctx->config_dn = ldb_dn_get_linearized(ldb_get_config_basedn(ldb));
336         torture_assert(tctx, ctx->config_dn != NULL, "Failed to get Domain DN");
337
338         ctx->schema_dn = ldb_dn_get_linearized(ldb_get_schema_basedn(ldb));
339         torture_assert(tctx, ctx->schema_dn != NULL, "Failed to get Domain DN");
340
341         return true;
342 }
343
344 static bool test_analyse_objects(struct torture_context *tctx,
345                                  struct DsSyncTest *ctx,
346                                  const char *partition,
347                                  const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
348                                  uint32_t object_count,
349                                  const struct drsuapi_DsReplicaObjectListItemEx *first_object,
350                                  const DATA_BLOB *gensec_skey)
351 {
352         static uint32_t object_id;
353         const char *save_values_dir;
354         const struct drsuapi_DsReplicaObjectListItemEx *cur;
355         struct ldb_context *ldb = ctx->admin.ldap.ldb;
356         struct ldb_dn *deleted_dn;
357         WERROR status;
358         int i, j, ret;
359         struct dsdb_extended_replicated_objects *objs;
360         struct ldb_extended_dn_control *extended_dn_ctrl;
361         struct dsdb_schema *ldap_schema;
362         struct ldb_dn *partition_dn = ldb_dn_new(tctx, ldb, partition);
363
364         torture_assert_not_null(tctx, partition_dn, "Failed to parse partition DN as as DN");
365
366         /* load dsdb_schema using remote prefixMap */
367         torture_assert(tctx,
368                        drs_util_dsdb_schema_load_ldb(tctx, ldb, mapping_ctr, false),
369                        "drs_util_dsdb_schema_load_ldb() failed");
370         ldap_schema = dsdb_get_schema(ldb, NULL);
371
372         status = dsdb_replicated_objects_convert(ldb,
373                                                  ldap_schema,
374                                                  partition_dn,
375                                                  mapping_ctr,
376                                                  object_count,
377                                                  first_object,
378                                                  0, NULL,
379                                                  NULL, NULL,
380                                                  gensec_skey,
381                                                  0,
382                                                  ctx, &objs);
383         torture_assert_werr_ok(tctx, status, "dsdb_extended_replicated_objects_convert() failed!");
384
385         extended_dn_ctrl = talloc(objs, struct ldb_extended_dn_control);
386         extended_dn_ctrl->type = 1;
387
388         deleted_dn = ldb_dn_new(objs, ldb, partition);
389         ldb_dn_add_child_fmt(deleted_dn, "CN=Deleted Objects");
390
391         for (i=0; i < objs->num_objects; i++) {
392                 struct ldb_request *search_req;
393                 struct ldb_result *res;
394                 struct ldb_message *new_msg, *drs_msg, *ldap_msg;
395                 size_t num_attrs = objs->objects[i].msg->num_elements+1;
396                 const char **attrs = talloc_array(objs, const char *, num_attrs);
397                 for (j=0; j < objs->objects[i].msg->num_elements; j++) {
398                         attrs[j] = objs->objects[i].msg->elements[j].name;
399                 }
400                 attrs[j] = NULL;
401                 res = talloc_zero(objs, struct ldb_result);
402                 if (!res) {
403                         return LDB_ERR_OPERATIONS_ERROR;
404                 }
405                 ret = ldb_build_search_req(&search_req, ldb, objs,
406                                            objs->objects[i].msg->dn,
407                                            LDB_SCOPE_BASE,
408                                            NULL,
409                                            attrs,
410                                            NULL,
411                                            res,
412                                            ldb_search_default_callback,
413                                            NULL);
414                 if (ret != LDB_SUCCESS) {
415                         return false;
416                 }
417                 talloc_steal(search_req, res);
418                 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_DELETED_OID, true, NULL);
419                 if (ret != LDB_SUCCESS) {
420                         return false;
421                 }
422
423                 ret = ldb_request_add_control(search_req, LDB_CONTROL_EXTENDED_DN_OID, true, extended_dn_ctrl);
424                 if (ret != LDB_SUCCESS) {
425                         return false;
426                 }
427
428                 ret = ldb_request(ldb, search_req);
429                 if (ret == LDB_SUCCESS) {
430                         ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
431                 }
432
433                 torture_assert_int_equal(tctx, ret, LDB_SUCCESS,
434                                          talloc_asprintf(tctx,
435                                                          "Could not re-fetch object just delivered over DRS: %s",
436                                                          ldb_errstring(ldb)));
437                 torture_assert_int_equal(tctx, res->count, 1, "Could not re-fetch object just delivered over DRS");
438                 ldap_msg = res->msgs[0];
439                 for (j=0; j < ldap_msg->num_elements; j++) {
440                         ldap_msg->elements[j].flags = LDB_FLAG_MOD_ADD;
441                         /* 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 */
442                         if ((ldb_attr_cmp(ldap_msg->elements[j].name, "nTSecurityDescriptor") == 0) &&
443                             (ldb_dn_compare(ldap_msg->dn, deleted_dn) == 0)) {
444                                 ldb_msg_remove_element(ldap_msg, &ldap_msg->elements[j]);
445                                 /* Don't skip one */
446                                 j--;
447                         }
448                 }
449
450                 ret = ldb_msg_normalize(ldb, search_req,
451                                         objs->objects[i].msg, &drs_msg);
452                 torture_assert(tctx, ret == LDB_SUCCESS,
453                                "ldb_msg_normalize() has failed");
454
455                 for (j=0; j < drs_msg->num_elements; j++) {
456                         if (drs_msg->elements[j].num_values == 0) {
457                                 ldb_msg_remove_element(drs_msg, &drs_msg->elements[j]);
458                                 /* Don't skip one */
459                                 j--;
460
461                                 /* For unknown reasons, there is no nTSecurityDescriptor on cn=deleted objects over LDAP, but there is over DRS! */
462                         } else if ((ldb_attr_cmp(drs_msg->elements[j].name, "nTSecurityDescriptor") == 0) &&
463                                    (ldb_dn_compare(drs_msg->dn, deleted_dn) == 0)) {
464                                 ldb_msg_remove_element(drs_msg, &drs_msg->elements[j]);
465                                 /* Don't skip one */
466                                 j--;
467                         } else if (ldb_attr_cmp(drs_msg->elements[j].name, "unicodePwd") == 0 ||
468                                    ldb_attr_cmp(drs_msg->elements[j].name, "dBCSPwd") == 0 ||
469                                    ldb_attr_cmp(drs_msg->elements[j].name, "userPassword") == 0 ||
470                                    ldb_attr_cmp(drs_msg->elements[j].name, "ntPwdHistory") == 0 ||
471                                    ldb_attr_cmp(drs_msg->elements[j].name, "lmPwdHistory") == 0 ||
472                                    ldb_attr_cmp(drs_msg->elements[j].name, "supplementalCredentials") == 0 ||
473                                    ldb_attr_cmp(drs_msg->elements[j].name, "priorValue") == 0 ||
474                                    ldb_attr_cmp(drs_msg->elements[j].name, "currentValue") == 0 ||
475                                    ldb_attr_cmp(drs_msg->elements[j].name, "trustAuthOutgoing") == 0 ||
476                                    ldb_attr_cmp(drs_msg->elements[j].name, "trustAuthIncoming") == 0 ||
477                                    ldb_attr_cmp(drs_msg->elements[j].name, "initialAuthOutgoing") == 0 ||
478                                    ldb_attr_cmp(drs_msg->elements[j].name, "initialAuthIncoming") == 0) {
479
480                                 /* These are not shown over LDAP, so we need to skip them for the comparison */
481                                 ldb_msg_remove_element(drs_msg, &drs_msg->elements[j]);
482                                 /* Don't skip one */
483                                 j--;
484                         } else {
485                                 drs_msg->elements[j].flags = LDB_FLAG_MOD_ADD;
486                         }
487                 }
488
489
490                 ret = ldb_msg_difference(ldb, search_req,
491                                          drs_msg, ldap_msg, &new_msg);
492                 torture_assert(tctx, ret == LDB_SUCCESS, "ldb_msg_difference() has failed");
493                 if (new_msg->num_elements != 0) {
494                         char *s;
495                         bool is_warning = true;
496                         unsigned int idx;
497                         struct ldb_message_element *el;
498                         const struct dsdb_attribute * a;
499                         struct ldb_ldif ldif;
500                         ldif.changetype = LDB_CHANGETYPE_MODIFY;
501                         ldif.msg = new_msg;
502                         s = ldb_ldif_write_string(ldb, new_msg, &ldif);
503                         s = talloc_asprintf(tctx, "\n# Difference in between DRS and LDAP objects: \n%s", s);
504
505                         ret = ldb_msg_difference(ldb, search_req,
506                                                  ldap_msg, drs_msg, &ldif.msg);
507                         torture_assert(tctx, ret == LDB_SUCCESS, "ldb_msg_difference() has failed");
508                         s = talloc_asprintf_append(s,
509                                                    "\n# Difference in between LDAP and DRS objects: \n%s",
510                                                    ldb_ldif_write_string(ldb, new_msg, &ldif));
511
512                         s = talloc_asprintf_append(s,
513                                                    "# Should have no objects in 'difference' message. Diff elements: %d",
514                                                    new_msg->num_elements);
515
516                         /*
517                          * In case differences in messages are:
518                          * 1. Attributes with different values, i.e. 'replace'
519                          * 2. Those attributes are forward-link attributes
520                          * then we just warn about those differences.
521                          * It turns out windows doesn't send all of those values
522                          * in replicated_object but in linked_attributes.
523                          */
524                         for (idx = 0; idx < new_msg->num_elements && is_warning; idx++) {
525                                 el = &new_msg->elements[idx];
526                                 a = dsdb_attribute_by_lDAPDisplayName(ldap_schema,
527                                                                       el->name);
528                                 if (!(el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE))) {
529                                         /* DRS only value */
530                                         is_warning = false;
531                                 } else if (a->linkID & 1) {
532                                         is_warning = false;
533                                 }
534                         }
535                         if (is_warning) {
536                                 torture_warning(tctx, "%s", s);
537                         } else {
538                                 torture_fail(tctx, s);
539                         }
540                 }
541
542                 /* search_req is used as a tmp talloc context in the above */
543                 talloc_free(search_req);
544         }
545
546         if (!lpcfg_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
547                 talloc_free(objs);
548                 return true;
549         }
550
551         save_values_dir = lpcfg_parm_string(tctx->lp_ctx, NULL, "dssync", "save_pwd_blobs_dir");
552
553         for (cur = first_object; cur; cur = cur->next_object) {
554                 const char *dn;
555                 bool dn_printed = false;
556
557                 if (!cur->object.identifier) continue;
558
559                 dn = cur->object.identifier->dn;
560
561                 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
562                         const char *name = NULL;
563                         DATA_BLOB plain_data;
564                         struct drsuapi_DsReplicaAttribute *attr;
565                         ndr_pull_flags_fn_t pull_fn = NULL;
566                         ndr_print_fn_t print_fn = NULL;
567                         void *ptr = NULL;
568                         attr = &cur->object.attribute_ctr.attributes[i];
569
570                         switch (attr->attid) {
571                         case DRSUAPI_ATTID_dBCSPwd:
572                                 name    = "dBCSPwd";
573                                 break;
574                         case DRSUAPI_ATTID_unicodePwd:
575                                 name    = "unicodePwd";
576                                 break;
577                         case DRSUAPI_ATTID_ntPwdHistory:
578                                 name    = "ntPwdHistory";
579                                 break;
580                         case DRSUAPI_ATTID_lmPwdHistory:
581                                 name    = "lmPwdHistory";
582                                 break;
583                         case DRSUAPI_ATTID_supplementalCredentials:
584                                 name    = "supplementalCredentials";
585                                 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob;
586                                 print_fn = (ndr_print_fn_t)ndr_print_supplementalCredentialsBlob;
587                                 ptr = talloc(ctx, struct supplementalCredentialsBlob);
588                                 break;
589                         case DRSUAPI_ATTID_priorValue:
590                                 name    = "priorValue";
591                                 break;
592                         case DRSUAPI_ATTID_currentValue:
593                                 name    = "currentValue";
594                                 break;
595                         case DRSUAPI_ATTID_trustAuthOutgoing:
596                                 name    = "trustAuthOutgoing";
597                                 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob;
598                                 print_fn = (ndr_print_fn_t)ndr_print_trustAuthInOutBlob;
599                                 ptr = talloc(ctx, struct trustAuthInOutBlob);
600                                 break;
601                         case DRSUAPI_ATTID_trustAuthIncoming:
602                                 name    = "trustAuthIncoming";
603                                 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob;
604                                 print_fn = (ndr_print_fn_t)ndr_print_trustAuthInOutBlob;
605                                 ptr = talloc(ctx, struct trustAuthInOutBlob);
606                                 break;
607                         case DRSUAPI_ATTID_initialAuthOutgoing:
608                                 name    = "initialAuthOutgoing";
609                                 break;
610                         case DRSUAPI_ATTID_initialAuthIncoming:
611                                 name    = "initialAuthIncoming";
612                                 break;
613                         default:
614                                 continue;
615                         }
616
617                         if (attr->value_ctr.num_values != 1) continue;
618
619                         if (!attr->value_ctr.values[0].blob) continue;
620
621                         plain_data = *attr->value_ctr.values[0].blob;
622
623                         if (!dn_printed) {
624                                 object_id++;
625                                 DEBUG(0,("DN[%u] %s\n", object_id, dn));
626                                 dn_printed = true;
627                         }
628                         DEBUGADD(0,("ATTR: %s plain.length=%lu\n",
629                                     name, (long)plain_data.length));
630                         if (plain_data.length) {
631                                 enum ndr_err_code ndr_err;
632                                 dump_data(0, plain_data.data, plain_data.length);
633                                 if (save_values_dir) {
634                                         char *fname;
635                                         fname = talloc_asprintf(ctx, "%s/%s%02d",
636                                                                 save_values_dir,
637                                                                 name, object_id);
638                                         if (fname) {
639                                                 bool ok;
640                                                 ok = file_save(fname, plain_data.data, plain_data.length);
641                                                 if (!ok) {
642                                                         DEBUGADD(0,("Failed to save '%s'\n", fname));
643                                                 }
644                                         }
645                                         talloc_free(fname);
646                                 }
647
648                                 if (pull_fn) {
649                                         /* Can't use '_all' because of PIDL bugs with relative pointers */
650                                         ndr_err = ndr_pull_struct_blob(&plain_data, ptr,
651                                                                        ptr, pull_fn);
652                                         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
653                                                 ndr_print_debug(print_fn, name, ptr);
654                                         } else {
655                                                 DEBUG(0, ("Failed to decode %s\n", name));
656                                         }
657                                 }
658                         }
659                         talloc_free(ptr);
660                 }
661         }
662         talloc_free(objs);
663         return true;
664 }
665
666 static bool test_GetNCChanges(struct torture_context *tctx,
667                               struct DsSyncTest *ctx,
668                               const char *nc_dn_str)
669 {
670         NTSTATUS status;
671         bool ret = true;
672         int i, y = 0;
673         uint64_t highest_usn = 0;
674         struct drsuapi_DsGetNCChanges r;
675         union drsuapi_DsGetNCChangesRequest req;
676         struct drsuapi_DsReplicaObjectIdentifier nc;
677         struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
678         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
679         uint32_t out_level = 0;
680         struct GUID null_guid;
681         struct dom_sid null_sid;
682         DATA_BLOB gensec_skey;
683         struct {
684                 uint32_t level;
685         } array[] = {
686 /*              {
687                         5
688                 },
689 */              {
690                         8
691                 }
692         };
693
694         ZERO_STRUCT(null_guid);
695         ZERO_STRUCT(null_sid);
696
697         highest_usn = lpcfg_parm_int(tctx->lp_ctx, NULL, "dssync", "highest_usn", 0);
698
699         array[0].level = lpcfg_parm_int(tctx->lp_ctx, NULL, "dssync", "get_nc_changes_level", array[0].level);
700
701         if (lpcfg_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
702                 const struct samr_Password *nthash;
703                 nthash = cli_credentials_get_nt_hash(ctx->new_dc.credentials, ctx);
704                 if (nthash) {
705                         dump_data_pw("CREDENTIALS nthash:", nthash->hash, sizeof(nthash->hash));
706                 }
707         }
708         status = gensec_session_key(ctx->new_dc.drsuapi.drs_pipe->conn->security_state.generic_state,
709                                     ctx, &gensec_skey);
710         if (!NT_STATUS_IS_OK(status)) {
711                 printf("failed to get gensec session key: %s\n", nt_errstr(status));
712                 return false;
713         }
714
715         for (i=0; i < ARRAY_SIZE(array); i++) {
716                 printf("Testing DsGetNCChanges level %d\n",
717                         array[i].level);
718
719                 r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
720                 r.in.level              = array[i].level;
721
722                 switch (r.in.level) {
723                 case 5:
724                         nc.guid = null_guid;
725                         nc.sid  = null_sid;
726                         nc.dn   = nc_dn_str;
727
728                         r.in.req                                        = &req;
729                         r.in.req->req5.destination_dsa_guid             = ctx->new_dc.invocation_id;
730                         r.in.req->req5.source_dsa_invocation_id         = null_guid;
731                         r.in.req->req5.naming_context                   = &nc;
732                         r.in.req->req5.highwatermark.tmp_highest_usn    = highest_usn;
733                         r.in.req->req5.highwatermark.reserved_usn       = 0;
734                         r.in.req->req5.highwatermark.highest_usn        = highest_usn;
735                         r.in.req->req5.uptodateness_vector              = NULL;
736                         r.in.req->req5.replica_flags                    = 0;
737                         if (lpcfg_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
738                                 r.in.req->req5.replica_flags            |= DRSUAPI_DRS_USE_COMPRESSION;
739                         }
740                         if (lpcfg_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
741                                 r.in.req->req5.replica_flags            |= DRSUAPI_DRS_WRIT_REP;
742                         }
743                         r.in.req->req5.replica_flags                    |= DRSUAPI_DRS_INIT_SYNC
744                                                                         | DRSUAPI_DRS_PER_SYNC
745                                                                         | DRSUAPI_DRS_GET_ANC
746                                                                         | DRSUAPI_DRS_NEVER_SYNCED
747                                                                         ;
748                         r.in.req->req5.max_object_count                 = 133;
749                         r.in.req->req5.max_ndr_size                     = 1336770;
750                         r.in.req->req5.extended_op                      = DRSUAPI_EXOP_NONE;
751                         r.in.req->req5.fsmo_info                        = 0;
752
753                         break;
754                 case 8:
755                         nc.guid = null_guid;
756                         nc.sid  = null_sid;
757                         nc.dn   = nc_dn_str;
758                         /* nc.dn can be set to any other ad partition */
759
760                         r.in.req                                        = &req;
761                         r.in.req->req8.destination_dsa_guid             = ctx->new_dc.invocation_id;
762                         r.in.req->req8.source_dsa_invocation_id         = null_guid;
763                         r.in.req->req8.naming_context                   = &nc;
764                         r.in.req->req8.highwatermark.tmp_highest_usn    = highest_usn;
765                         r.in.req->req8.highwatermark.reserved_usn       = 0;
766                         r.in.req->req8.highwatermark.highest_usn        = highest_usn;
767                         r.in.req->req8.uptodateness_vector              = NULL;
768                         r.in.req->req8.replica_flags                    = 0;
769                         if (lpcfg_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
770                                 r.in.req->req8.replica_flags            |= DRSUAPI_DRS_USE_COMPRESSION;
771                         }
772                         if (lpcfg_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
773                                 r.in.req->req8.replica_flags            |= DRSUAPI_DRS_WRIT_REP;
774                         }
775                         r.in.req->req8.replica_flags                    |= DRSUAPI_DRS_INIT_SYNC
776                                                                         | DRSUAPI_DRS_PER_SYNC
777                                                                         | DRSUAPI_DRS_GET_ANC
778                                                                         | DRSUAPI_DRS_NEVER_SYNCED
779                                                                         ;
780                         r.in.req->req8.max_object_count                 = 402;
781                         r.in.req->req8.max_ndr_size                     = 402116;
782
783                         r.in.req->req8.extended_op                      = DRSUAPI_EXOP_NONE;
784                         r.in.req->req8.fsmo_info                        = 0;
785                         r.in.req->req8.partial_attribute_set            = NULL;
786                         r.in.req->req8.partial_attribute_set_ex         = NULL;
787                         r.in.req->req8.mapping_ctr.num_mappings         = 0;
788                         r.in.req->req8.mapping_ctr.mappings             = NULL;
789
790                         break;
791                 }
792
793                 for (y=0; ;y++) {
794                         uint32_t _level = 0;
795                         union drsuapi_DsGetNCChangesCtr ctr;
796
797                         ZERO_STRUCT(r.out);
798
799                         r.out.level_out = &_level;
800                         r.out.ctr       = &ctr;
801
802                         if (r.in.level == 5) {
803                                 torture_comment(tctx,
804                                                 "start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",
805                                                 y,
806                                                 (unsigned long long) r.in.req->req5.highwatermark.tmp_highest_usn,
807                                                 (unsigned long long) r.in.req->req5.highwatermark.highest_usn);
808                         }
809
810                         if (r.in.level == 8) {
811                                 torture_comment(tctx,
812                                                 "start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",
813                                                 y,
814                                                 (unsigned long long) r.in.req->req8.highwatermark.tmp_highest_usn,
815                                                 (unsigned long long) r.in.req->req8.highwatermark.highest_usn);
816                         }
817
818                         status = dcerpc_drsuapi_DsGetNCChanges_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
819                         torture_drsuapi_assert_call(tctx, ctx->new_dc.drsuapi.drs_pipe, status,
820                                                     &r, "dcerpc_drsuapi_DsGetNCChanges");
821
822                         if (ret == true && *r.out.level_out == 1) {
823                                 out_level = 1;
824                                 ctr1 = &r.out.ctr->ctr1;
825                         } else if (ret == true && *r.out.level_out == 2 &&
826                                    r.out.ctr->ctr2.mszip1.ts) {
827                                 out_level = 1;
828                                 ctr1 = &r.out.ctr->ctr2.mszip1.ts->ctr1;
829                         }
830
831                         if (out_level == 1) {
832                                 torture_comment(tctx,
833                                                 "end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",
834                                                 y,
835                                                 (unsigned long long) ctr1->new_highwatermark.tmp_highest_usn,
836                                                 (unsigned long long) ctr1->new_highwatermark.highest_usn);
837
838                                 if (!test_analyse_objects(tctx, ctx, nc_dn_str, &ctr1->mapping_ctr,  ctr1->object_count,
839                                                           ctr1->first_object, &gensec_skey)) {
840                                         return false;
841                                 }
842
843                                 if (ctr1->more_data) {
844                                         r.in.req->req5.highwatermark = ctr1->new_highwatermark;
845                                         continue;
846                                 }
847                         }
848
849                         if (ret == true && *r.out.level_out == 6) {
850                                 out_level = 6;
851                                 ctr6 = &r.out.ctr->ctr6;
852                         } else if (ret == true && *r.out.level_out == 7
853                                    && r.out.ctr->ctr7.level == 6
854                                    && r.out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP
855                                    && r.out.ctr->ctr7.ctr.mszip6.ts) {
856                                 out_level = 6;
857                                 ctr6 = &r.out.ctr->ctr7.ctr.mszip6.ts->ctr6;
858                         } else if (ret == true && *r.out.level_out == 7
859                                    && r.out.ctr->ctr7.level == 6
860                                    && r.out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS
861                                    && r.out.ctr->ctr7.ctr.xpress6.ts) {
862                                 out_level = 6;
863                                 ctr6 = &r.out.ctr->ctr7.ctr.xpress6.ts->ctr6;
864                         }
865
866                         if (out_level == 6) {
867                                 torture_comment(tctx,
868                                                 "end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",
869                                                 y,
870                                                 (unsigned long long) ctr6->new_highwatermark.tmp_highest_usn,
871                                                 (unsigned long long) ctr6->new_highwatermark.highest_usn);
872
873                                 if (!test_analyse_objects(tctx, ctx, nc_dn_str, &ctr6->mapping_ctr,  ctr6->object_count,
874                                                           ctr6->first_object, &gensec_skey)) {
875                                         return false;
876                                 }
877
878                                 if (ctr6->more_data) {
879                                         r.in.req->req8.highwatermark = ctr6->new_highwatermark;
880                                         continue;
881                                 }
882                         }
883
884                         break;
885                 }
886         }
887
888         return ret;
889 }
890
891 /**
892  * Test DsGetNCChanges() DRSUAPI call against one
893  * or more Naming Contexts.
894  * Specific NC to test with may be supplied
895  * in lp_ctx configuration. If no NC is specified,
896  * it will test DsGetNCChanges() on all NCs on remote DC
897  */
898 static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
899 {
900         bool ret = true;
901         size_t i, count;
902         const char *nc_dn_str;
903         const char **nc_list;
904
905         nc_list = const_str_list(str_list_make_empty(ctx));
906         torture_assert(tctx, nc_list, "Not enough memory!");
907
908         /* make a list of partitions to test with */
909         nc_dn_str = lpcfg_parm_string(tctx->lp_ctx, NULL, "dssync", "partition");
910         if (nc_dn_str == NULL) {
911                 nc_list = str_list_add_const(nc_list, ctx->domain_dn);
912                 nc_list = str_list_add_const(nc_list, ctx->config_dn);
913                 nc_list = str_list_add_const(nc_list, ctx->schema_dn);
914         } else {
915                 nc_list = str_list_add_const(nc_list, nc_dn_str);
916         }
917
918         count = str_list_length(nc_list);
919         for (i = 0; i < count && ret; i++) {
920                 torture_comment(tctx, "\nNaming Context: %s\n", nc_list[i]);
921                 ret = test_GetNCChanges(tctx, ctx, nc_list[i]);
922         }
923
924         talloc_free(nc_list);
925         return ret;
926 }
927
928
929 static bool test_FetchNT4Data(struct torture_context *tctx,
930                               struct DsSyncTest *ctx)
931 {
932         NTSTATUS status;
933         struct drsuapi_DsGetNT4ChangeLog r;
934         union drsuapi_DsGetNT4ChangeLogRequest req;
935         union drsuapi_DsGetNT4ChangeLogInfo info;
936         uint32_t level_out = 0;
937         struct GUID null_guid;
938         DATA_BLOB cookie;
939
940         ZERO_STRUCT(null_guid);
941         ZERO_STRUCT(cookie);
942
943         ZERO_STRUCT(r);
944         r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
945         r.in.level              = 1;
946         r.out.info              = &info;
947         r.out.level_out         = &level_out;
948
949         req.req1.flags = lpcfg_parm_int(tctx->lp_ctx, NULL,
950                                      "dssync", "nt4changelog_flags",
951                                      DRSUAPI_NT4_CHANGELOG_GET_CHANGELOG |
952                                      DRSUAPI_NT4_CHANGELOG_GET_SERIAL_NUMBERS);
953         req.req1.preferred_maximum_length = lpcfg_parm_int(tctx->lp_ctx, NULL,
954                                         "dssync", "nt4changelog_preferred_len",
955                                         0x00004000);
956
957         while (1) {
958                 req.req1.restart_length = cookie.length;
959                 req.req1.restart_data = cookie.data;
960
961                 r.in.req = &req;
962
963                 status = dcerpc_drsuapi_DsGetNT4ChangeLog_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
964                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
965                         torture_skip(tctx,
966                                      "DsGetNT4ChangeLog not supported: NT_STATUS_NOT_IMPLEMENTED");
967                 } else if (!NT_STATUS_IS_OK(status)) {
968                         const char *errstr = nt_errstr(status);
969                         if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
970                                 torture_skip(tctx,
971                                              "DsGetNT4ChangeLog not supported: NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE");
972                         }
973                         torture_fail(tctx,
974                                      talloc_asprintf(tctx, "dcerpc_drsuapi_DsGetNT4ChangeLog failed - %s\n",
975                                                      errstr));
976                 } else if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_DOMAIN_ROLE)) {
977                         torture_skip(tctx,
978                                      "DsGetNT4ChangeLog not supported: WERR_INVALID_DOMAIN_ROLE");
979                 } else if (!W_ERROR_IS_OK(r.out.result)) {
980                         torture_fail(tctx,
981                                      talloc_asprintf(tctx, "DsGetNT4ChangeLog failed - %s\n",
982                                                      win_errstr(r.out.result)));
983                 } else if (*r.out.level_out != 1) {
984                         torture_fail(tctx,
985                                      talloc_asprintf(tctx, "DsGetNT4ChangeLog unknown level - %u\n",
986                                                      *r.out.level_out));
987                 } else if (NT_STATUS_IS_OK(r.out.info->info1.status)) {
988                 } else if (NT_STATUS_EQUAL(r.out.info->info1.status, STATUS_MORE_ENTRIES)) {
989                         cookie.length   = r.out.info->info1.restart_length;
990                         cookie.data     = r.out.info->info1.restart_data;
991                         continue;
992                 } else {
993                         torture_fail(tctx,
994                                      talloc_asprintf(tctx, "DsGetNT4ChangeLog failed - %s\n",
995                                                      nt_errstr(r.out.info->info1.status)));
996                 }
997
998                 break;
999         }
1000
1001         return true;
1002 }
1003
1004 /**
1005  * DSSYNC test case setup
1006  */
1007 static bool torture_dssync_tcase_setup(struct torture_context *tctx, void **data)
1008 {
1009         bool bret;
1010         struct DsSyncTest *ctx;
1011
1012         *data = ctx = test_create_context(tctx);
1013         torture_assert(tctx, ctx, "test_create_context() failed");
1014
1015         bret = _test_DsBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
1016         torture_assert(tctx, bret, "_test_DsBind() failed");
1017
1018         bret = test_LDAPBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.ldap);
1019         torture_assert(tctx, bret, "test_LDAPBind() failed");
1020
1021         bret = test_GetInfo(tctx, ctx);
1022         torture_assert(tctx, bret, "test_GetInfo() failed");
1023
1024         bret = _test_DsBind(tctx, ctx, ctx->new_dc.credentials, &ctx->new_dc.drsuapi);
1025         torture_assert(tctx, bret, "_test_DsBind() failed");
1026
1027         return true;
1028 }
1029
1030 /**
1031  * DSSYNC test case cleanup
1032  */
1033 static bool torture_dssync_tcase_teardown(struct torture_context *tctx, void *data)
1034 {
1035         struct DsSyncTest *ctx;
1036         struct drsuapi_DsUnbind r;
1037         struct policy_handle bind_handle;
1038
1039         ctx = talloc_get_type(data, struct DsSyncTest);
1040
1041         ZERO_STRUCT(r);
1042         r.out.bind_handle = &bind_handle;
1043
1044         /* Unbing admin handle */
1045         r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
1046         dcerpc_drsuapi_DsUnbind_r(ctx->admin.drsuapi.drs_handle, ctx, &r);
1047
1048         /* Unbing new_dc handle */
1049         r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
1050         dcerpc_drsuapi_DsUnbind_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
1051
1052         talloc_free(ctx);
1053
1054         return true;
1055 }
1056
1057 /**
1058  * DSSYNC test case implementation
1059  */
1060 void torture_drs_rpc_dssync_tcase(struct torture_suite *suite)
1061 {
1062         typedef bool (*run_func) (struct torture_context *test, void *tcase_data);
1063         struct torture_tcase *tcase = torture_suite_add_tcase(suite, "dssync");
1064
1065         torture_tcase_set_fixture(tcase,
1066                                   torture_dssync_tcase_setup,
1067                                   torture_dssync_tcase_teardown);
1068
1069         torture_tcase_add_simple_test(tcase, "DC_FetchData", (run_func)test_FetchData);
1070         torture_tcase_add_simple_test(tcase, "FetchNT4Data", (run_func)test_FetchNT4Data);
1071 }
1072