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