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