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