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