s4:lib: merge LDB_WRAP and LDBSAMBA and make LDBSAMBA a library.
[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                 new_msg = ldb_msg_diff(ldb, drs_msg, ldap_msg);
579                 talloc_steal(search_req, new_msg);
580                 if (new_msg->num_elements != 0) {
581                         char *s;
582                         struct ldb_ldif ldif;
583                         ldif.changetype = LDB_CHANGETYPE_MODIFY;
584                         ldif.msg = new_msg;
585                         s = ldb_ldif_write_string(ldb, new_msg, &ldif);
586                         s = talloc_asprintf(tctx, "\n# Difference in between DRS and LDAP objects: \n%s\n", s);
587
588                         ldif.msg = ldb_msg_diff(ldb, ldap_msg, drs_msg);
589                         s = talloc_asprintf_append(s,
590                                                    "\n# Difference in between LDAP and DRS objects: \n%s\n",
591                                                    ldb_ldif_write_string(ldb, new_msg, &ldif));
592
593                         s = talloc_asprintf_append(s,
594                                                    "# Should have no objects in 'difference' message. Diff elements: %d",
595                                                    new_msg->num_elements);
596                         torture_fail(tctx, s);
597                 }
598
599                 /* search_req is used as a tmp talloc context in the above */
600                 talloc_free(search_req);
601         }
602
603         if (!lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
604                 talloc_free(objs);
605                 return true;    
606         }
607
608         save_values_dir = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "save_pwd_blobs_dir");
609
610         for (cur = first_object; cur; cur = cur->next_object) {
611                 const char *dn;
612                 struct dom_sid *sid = NULL;
613                 uint32_t rid = 0;
614                 bool dn_printed = false;
615
616                 if (!cur->object.identifier) continue;
617
618                 dn = cur->object.identifier->dn;
619                 if (cur->object.identifier->sid.num_auths > 0) {
620                         sid = &cur->object.identifier->sid;
621                         rid = sid->sub_auths[sid->num_auths - 1];
622                 }
623
624                 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
625                         WERROR werr;
626                         const char *name = NULL;
627                         bool rcrypt = false;
628                         DATA_BLOB *enc_data = NULL;
629                         DATA_BLOB plain_data;
630                         struct drsuapi_DsReplicaAttribute *attr;
631                         ndr_pull_flags_fn_t pull_fn = NULL;
632                         ndr_print_fn_t print_fn = NULL;
633                         void *ptr = NULL;
634                         attr = &cur->object.attribute_ctr.attributes[i];
635
636                         switch (attr->attid) {
637                         case DRSUAPI_ATTRIBUTE_dBCSPwd:
638                                 name    = "dBCSPwd";
639                                 rcrypt  = true;
640                                 break;
641                         case DRSUAPI_ATTRIBUTE_unicodePwd:
642                                 name    = "unicodePwd";
643                                 rcrypt  = true;
644                                 break;
645                         case DRSUAPI_ATTRIBUTE_ntPwdHistory:
646                                 name    = "ntPwdHistory";
647                                 rcrypt  = true;
648                                 break;
649                         case DRSUAPI_ATTRIBUTE_lmPwdHistory:
650                                 name    = "lmPwdHistory";
651                                 rcrypt  = true;
652                                 break;
653                         case DRSUAPI_ATTRIBUTE_supplementalCredentials:
654                                 name    = "supplementalCredentials";
655                                 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob;
656                                 print_fn = (ndr_print_fn_t)ndr_print_supplementalCredentialsBlob;
657                                 ptr = talloc(ctx, struct supplementalCredentialsBlob);
658                                 break;
659                         case DRSUAPI_ATTRIBUTE_priorValue:
660                                 name    = "priorValue";
661                                 break;
662                         case DRSUAPI_ATTRIBUTE_currentValue:
663                                 name    = "currentValue";
664                                 break;
665                         case DRSUAPI_ATTRIBUTE_trustAuthOutgoing:
666                                 name    = "trustAuthOutgoing";
667                                 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob;
668                                 print_fn = (ndr_print_fn_t)ndr_print_trustAuthInOutBlob;
669                                 ptr = talloc(ctx, struct trustAuthInOutBlob);
670                                 break;
671                         case DRSUAPI_ATTRIBUTE_trustAuthIncoming:
672                                 name    = "trustAuthIncoming";
673                                 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob;
674                                 print_fn = (ndr_print_fn_t)ndr_print_trustAuthInOutBlob;
675                                 ptr = talloc(ctx, struct trustAuthInOutBlob);
676                                 break;
677                         case DRSUAPI_ATTRIBUTE_initialAuthOutgoing:
678                                 name    = "initialAuthOutgoing";
679                                 break;
680                         case DRSUAPI_ATTRIBUTE_initialAuthIncoming:
681                                 name    = "initialAuthIncoming";
682                                 break;
683                         default:
684                                 continue;
685                         }
686
687                         if (attr->value_ctr.num_values != 1) continue;
688
689                         if (!attr->value_ctr.values[0].blob) continue;
690
691                         enc_data = attr->value_ctr.values[0].blob;
692                         ZERO_STRUCT(plain_data);
693
694                         werr = drsuapi_decrypt_attribute_value(ctx, gensec_skey, rcrypt,
695                                                                rid,
696                                                                enc_data, &plain_data);
697                         if (!W_ERROR_IS_OK(werr)) {
698                                 DEBUG(0, ("Failed to decrypt %s\n", name));
699                                 continue;
700                         }
701                         if (!dn_printed) {
702                                 object_id++;
703                                 DEBUG(0,("DN[%u] %s\n", object_id, dn));
704                                 dn_printed = true;
705                         }
706                         DEBUGADD(0,("ATTR: %s enc.length=%lu plain.length=%lu\n",
707                                     name, (long)enc_data->length, (long)plain_data.length));
708                         if (plain_data.length) {
709                                 enum ndr_err_code ndr_err;
710                                 dump_data(0, plain_data.data, plain_data.length);
711                                 if (save_values_dir) {
712                                         char *fname;
713                                         fname = talloc_asprintf(ctx, "%s/%s%02d",
714                                                                 save_values_dir,
715                                                                 name, object_id);
716                                         if (fname) {
717                                                 bool ok;
718                                                 ok = file_save(fname, plain_data.data, plain_data.length);
719                                                 if (!ok) {
720                                                         DEBUGADD(0,("Failed to save '%s'\n", fname));
721                                                 }
722                                         }
723                                         talloc_free(fname);
724                                 }
725
726                                 if (pull_fn) {
727                                         /* Can't use '_all' because of PIDL bugs with relative pointers */
728                                         ndr_err = ndr_pull_struct_blob(&plain_data, ptr,
729                                                                        ptr, pull_fn);
730                                         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
731                                                 ndr_print_debug(print_fn, name, ptr);
732                                         } else {
733                                                 DEBUG(0, ("Failed to decode %s\n", name));
734                                         }
735                                 }
736                         } else {
737                                 dump_data(0, enc_data->data, enc_data->length);
738                         }
739                         talloc_free(ptr);
740                 }
741         }
742         talloc_free(objs);
743         return true;
744 }
745
746 static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
747 {
748         NTSTATUS status;
749         bool ret = true;
750         int i, y = 0;
751         uint64_t highest_usn = 0;
752         const char *partition = NULL;
753         struct drsuapi_DsGetNCChanges r;
754         union drsuapi_DsGetNCChangesRequest req;
755         struct drsuapi_DsReplicaObjectIdentifier nc;
756         struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
757         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
758         uint32_t out_level = 0;
759         struct GUID null_guid;
760         struct dom_sid null_sid;
761         DATA_BLOB gensec_skey;
762         struct {
763                 uint32_t level;
764         } array[] = {
765 /*              {
766                         5
767                 },
768 */              {
769                         8
770                 }
771         };
772
773         ZERO_STRUCT(null_guid);
774         ZERO_STRUCT(null_sid);
775
776         partition = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "partition");
777         if (partition == NULL) {
778                 partition = ctx->domain_dn;
779                 printf("dssync:partition not specified, defaulting to %s.\n", ctx->domain_dn);
780         }
781
782         highest_usn = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "highest_usn", 0);
783
784         array[0].level = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "get_nc_changes_level", array[0].level);
785
786         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
787                 const struct samr_Password *nthash;
788                 nthash = cli_credentials_get_nt_hash(ctx->new_dc.credentials, ctx);
789                 if (nthash) {
790                         dump_data_pw("CREDENTIALS nthash:", nthash->hash, sizeof(nthash->hash));
791                 }
792         }
793         status = gensec_session_key(ctx->new_dc.drsuapi.drs_pipe->conn->security_state.generic_state,
794                                     &gensec_skey);
795         if (!NT_STATUS_IS_OK(status)) {
796                 printf("failed to get gensec session key: %s\n", nt_errstr(status));
797                 return false;
798         }
799
800         for (i=0; i < ARRAY_SIZE(array); i++) {
801                 printf("Testing DsGetNCChanges level %d\n",
802                         array[i].level);
803
804                 r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
805                 r.in.level              = array[i].level;
806
807                 switch (r.in.level) {
808                 case 5:
809                         nc.guid = null_guid;
810                         nc.sid  = null_sid;
811                         nc.dn   = partition; 
812
813                         r.in.req                                        = &req;
814                         r.in.req->req5.destination_dsa_guid             = ctx->new_dc.invocation_id;
815                         r.in.req->req5.source_dsa_invocation_id         = null_guid;
816                         r.in.req->req5.naming_context                   = &nc;
817                         r.in.req->req5.highwatermark.tmp_highest_usn    = highest_usn;
818                         r.in.req->req5.highwatermark.reserved_usn       = 0;
819                         r.in.req->req5.highwatermark.highest_usn        = highest_usn;
820                         r.in.req->req5.uptodateness_vector              = NULL;
821                         r.in.req->req5.replica_flags                    = 0;
822                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
823                                 r.in.req->req5.replica_flags            |= DRSUAPI_DRS_USE_COMPRESSION;
824                         }
825                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
826                                 r.in.req->req5.replica_flags            |= DRSUAPI_DRS_WRIT_REP;
827                         }
828                         r.in.req->req5.replica_flags                    |= DRSUAPI_DRS_INIT_SYNC
829                                                                         | DRSUAPI_DRS_PER_SYNC
830                                                                         | DRSUAPI_DRS_GET_ANC
831                                                                         | DRSUAPI_DRS_NEVER_SYNCED
832                                                                         ;
833                         r.in.req->req5.max_object_count                 = 133;
834                         r.in.req->req5.max_ndr_size                     = 1336770;
835                         r.in.req->req5.extended_op                      = DRSUAPI_EXOP_NONE;
836                         r.in.req->req5.fsmo_info                        = 0;
837
838                         break;
839                 case 8:
840                         nc.guid = null_guid;
841                         nc.sid  = null_sid;
842                         nc.dn   = partition; 
843                         /* nc.dn can be set to any other ad partition */
844
845                         r.in.req                                        = &req;
846                         r.in.req->req8.destination_dsa_guid             = ctx->new_dc.invocation_id;
847                         r.in.req->req8.source_dsa_invocation_id         = null_guid;
848                         r.in.req->req8.naming_context                   = &nc;
849                         r.in.req->req8.highwatermark.tmp_highest_usn    = highest_usn;
850                         r.in.req->req8.highwatermark.reserved_usn       = 0;
851                         r.in.req->req8.highwatermark.highest_usn        = highest_usn;
852                         r.in.req->req8.uptodateness_vector              = NULL;
853                         r.in.req->req8.replica_flags                    = 0;
854                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
855                                 r.in.req->req8.replica_flags            |= DRSUAPI_DRS_USE_COMPRESSION;
856                         }
857                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
858                                 r.in.req->req8.replica_flags            |= DRSUAPI_DRS_WRIT_REP;
859                         }
860                         r.in.req->req8.replica_flags                    |= DRSUAPI_DRS_INIT_SYNC
861                                                                         | DRSUAPI_DRS_PER_SYNC
862                                                                         | DRSUAPI_DRS_GET_ANC
863                                                                         | DRSUAPI_DRS_NEVER_SYNCED
864                                                                         ;
865                         r.in.req->req8.max_object_count                 = 402;
866                         r.in.req->req8.max_ndr_size                     = 402116;
867
868                         r.in.req->req8.extended_op                      = DRSUAPI_EXOP_NONE;
869                         r.in.req->req8.fsmo_info                        = 0;
870                         r.in.req->req8.partial_attribute_set            = NULL;
871                         r.in.req->req8.partial_attribute_set_ex         = NULL;
872                         r.in.req->req8.mapping_ctr.num_mappings         = 0;
873                         r.in.req->req8.mapping_ctr.mappings             = NULL;
874
875                         break;
876                 }
877                 
878                 printf("Dumping AD partition: %s\n", nc.dn);
879                 for (y=0; ;y++) {
880                         uint32_t _level = 0;
881                         union drsuapi_DsGetNCChangesCtr ctr;
882
883                         ZERO_STRUCT(r.out);
884
885                         r.out.level_out = &_level;
886                         r.out.ctr       = &ctr;
887
888                         if (r.in.level == 5) {
889                                 torture_comment(tctx,
890                                                 "start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",
891                                                 y,
892                                                 r.in.req->req5.highwatermark.tmp_highest_usn,
893                                                 r.in.req->req5.highwatermark.highest_usn);
894                         }
895
896                         if (r.in.level == 8) {
897                                 torture_comment(tctx,
898                                                 "start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",
899                                                 y,
900                                                 r.in.req->req8.highwatermark.tmp_highest_usn,
901                                                 r.in.req->req8.highwatermark.highest_usn);
902                         }
903
904                         status = dcerpc_drsuapi_DsGetNCChanges_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
905                         torture_drsuapi_assert_call(tctx, ctx->new_dc.drsuapi.drs_pipe, status,
906                                                     &r, "dcerpc_drsuapi_DsGetNCChanges");
907
908                         if (ret == true && *r.out.level_out == 1) {
909                                 out_level = 1;
910                                 ctr1 = &r.out.ctr->ctr1;
911                         } else if (ret == true && *r.out.level_out == 2 &&
912                                    r.out.ctr->ctr2.mszip1.ts) {
913                                 out_level = 1;
914                                 ctr1 = &r.out.ctr->ctr2.mszip1.ts->ctr1;
915                         }
916
917                         if (out_level == 1) {
918                                 torture_comment(tctx,
919                                                 "end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",
920                                                 y,
921                                                 ctr1->new_highwatermark.tmp_highest_usn,
922                                                 ctr1->new_highwatermark.highest_usn);
923
924                                 if (!test_analyse_objects(tctx, ctx, partition, &ctr1->mapping_ctr,  ctr1->object_count, 
925                                                           ctr1->first_object, &gensec_skey)) {
926                                         return false;
927                                 }
928
929                                 if (ctr1->more_data) {
930                                         r.in.req->req5.highwatermark = ctr1->new_highwatermark;
931                                         continue;
932                                 }
933                         }
934
935                         if (ret == true && *r.out.level_out == 6) {
936                                 out_level = 6;
937                                 ctr6 = &r.out.ctr->ctr6;
938                         } else if (ret == true && *r.out.level_out == 7
939                                    && r.out.ctr->ctr7.level == 6
940                                    && r.out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP
941                                    && r.out.ctr->ctr7.ctr.mszip6.ts) {
942                                 out_level = 6;
943                                 ctr6 = &r.out.ctr->ctr7.ctr.mszip6.ts->ctr6;
944                         } else if (ret == true && *r.out.level_out == 7
945                                    && r.out.ctr->ctr7.level == 6
946                                    && r.out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS
947                                    && r.out.ctr->ctr7.ctr.xpress6.ts) {
948                                 out_level = 6;
949                                 ctr6 = &r.out.ctr->ctr7.ctr.xpress6.ts->ctr6;
950                         }
951
952                         if (out_level == 6) {
953                                 torture_comment(tctx,
954                                                 "end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",
955                                                 y,
956                                                 ctr6->new_highwatermark.tmp_highest_usn,
957                                                 ctr6->new_highwatermark.highest_usn);
958
959                                 if (!test_analyse_objects(tctx, ctx, partition, &ctr6->mapping_ctr,  ctr6->object_count, 
960                                                           ctr6->first_object, &gensec_skey)) {
961                                         return false;
962                                 }
963
964                                 if (ctr6->more_data) {
965                                         r.in.req->req8.highwatermark = ctr6->new_highwatermark;
966                                         continue;
967                                 }
968                         }
969
970                         break;
971                 }
972         }
973
974         return ret;
975 }
976
977 static bool test_FetchNT4Data(struct torture_context *tctx, 
978                               struct DsSyncTest *ctx)
979 {
980         NTSTATUS status;
981         struct drsuapi_DsGetNT4ChangeLog r;
982         union drsuapi_DsGetNT4ChangeLogRequest req;
983         union drsuapi_DsGetNT4ChangeLogInfo info;
984         uint32_t level_out = 0;
985         struct GUID null_guid;
986         struct dom_sid null_sid;
987         DATA_BLOB cookie;
988
989         ZERO_STRUCT(null_guid);
990         ZERO_STRUCT(null_sid);
991         ZERO_STRUCT(cookie);
992
993         ZERO_STRUCT(r);
994         r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
995         r.in.level              = 1;
996         r.out.info              = &info;
997         r.out.level_out         = &level_out;
998
999         req.req1.flags = lp_parm_int(tctx->lp_ctx, NULL,
1000                                      "dssync", "nt4changelog_flags",
1001                                      DRSUAPI_NT4_CHANGELOG_GET_CHANGELOG |
1002                                      DRSUAPI_NT4_CHANGELOG_GET_SERIAL_NUMBERS);
1003         req.req1.preferred_maximum_length = lp_parm_int(tctx->lp_ctx, NULL,
1004                                         "dssync", "nt4changelog_preferred_len",
1005                                         0x00004000);
1006
1007         while (1) {
1008                 req.req1.restart_length = cookie.length;
1009                 req.req1.restart_data = cookie.data;
1010
1011                 r.in.req = &req;
1012
1013                 status = dcerpc_drsuapi_DsGetNT4ChangeLog_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
1014                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1015                         torture_skip(tctx, "DsGetNT4ChangeLog not supported by target server");
1016                 } else if (!NT_STATUS_IS_OK(status)) {
1017                         const char *errstr = nt_errstr(status);
1018                         if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
1019                                 torture_skip(tctx, "DsGetNT4ChangeLog not supported by target server");
1020                         }
1021                         torture_fail(tctx,
1022                                      talloc_asprintf(tctx, "dcerpc_drsuapi_DsGetNT4ChangeLog failed - %s\n",
1023                                                      errstr));
1024                 } else if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_DOMAIN_ROLE)) {
1025                         torture_skip(tctx, "DsGetNT4ChangeLog not supported by target server");
1026                 } else if (!W_ERROR_IS_OK(r.out.result)) {
1027                         torture_fail(tctx,
1028                                      talloc_asprintf(tctx, "DsGetNT4ChangeLog failed - %s\n",
1029                                                      win_errstr(r.out.result)));
1030                 } else if (*r.out.level_out != 1) {
1031                         torture_fail(tctx,
1032                                      talloc_asprintf(tctx, "DsGetNT4ChangeLog unknown level - %u\n",
1033                                                      *r.out.level_out));
1034                 } else if (NT_STATUS_IS_OK(r.out.info->info1.status)) {
1035                 } else if (NT_STATUS_EQUAL(r.out.info->info1.status, STATUS_MORE_ENTRIES)) {
1036                         cookie.length   = r.out.info->info1.restart_length;
1037                         cookie.data     = r.out.info->info1.restart_data;
1038                         continue;
1039                 } else {
1040                         torture_fail(tctx,
1041                                      talloc_asprintf(tctx, "DsGetNT4ChangeLog failed - %s\n",
1042                                                      nt_errstr(r.out.info->info1.status)));
1043                 }
1044
1045                 break;
1046         }
1047
1048         return true;
1049 }
1050
1051 /**
1052  * DSSYNC test case setup
1053  */
1054 static bool torture_dssync_tcase_setup(struct torture_context *tctx, void **data)
1055 {
1056         bool bret;
1057         struct DsSyncTest *ctx;
1058
1059         *data = ctx = test_create_context(tctx);
1060         torture_assert(tctx, ctx, "test_create_context() failed");
1061
1062         bret = _test_DsBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
1063         torture_assert(tctx, bret, "_test_DsBind() failed");
1064
1065         bret = test_LDAPBind(tctx, ctx, ctx->admin.credentials, &ctx->admin.ldap);
1066         torture_assert(tctx, bret, "test_LDAPBind() failed");
1067
1068         bret = test_GetInfo(tctx, ctx);
1069         torture_assert(tctx, bret, "test_GetInfo() failed");
1070
1071         bret = _test_DsBind(tctx, ctx, ctx->new_dc.credentials, &ctx->new_dc.drsuapi);
1072         torture_assert(tctx, bret, "_test_DsBind() failed");
1073
1074         return true;
1075 }
1076
1077 /**
1078  * DSSYNC test case cleanup
1079  */
1080 static bool torture_dssync_tcase_teardown(struct torture_context *tctx, void *data)
1081 {
1082         struct DsSyncTest *ctx;
1083         struct drsuapi_DsUnbind r;
1084         struct policy_handle bind_handle;
1085
1086         ctx = talloc_get_type(data, struct DsSyncTest);
1087
1088         ZERO_STRUCT(r);
1089         r.out.bind_handle = &bind_handle;
1090
1091         /* Unbing admin handle */
1092         r.in.bind_handle = &ctx->admin.drsuapi.bind_handle;
1093         dcerpc_drsuapi_DsUnbind_r(ctx->admin.drsuapi.drs_handle, ctx, &r);
1094
1095         /* Unbing new_dc handle */
1096         r.in.bind_handle = &ctx->new_dc.drsuapi.bind_handle;
1097         dcerpc_drsuapi_DsUnbind_r(ctx->new_dc.drsuapi.drs_handle, ctx, &r);
1098
1099         talloc_free(ctx);
1100
1101         return true;
1102 }
1103
1104 /**
1105  * DSSYNC test case implementation
1106  */
1107 void torture_drs_rpc_dssync_tcase(struct torture_suite *suite)
1108 {
1109         typedef bool (*run_func) (struct torture_context *test, void *tcase_data);
1110
1111         struct torture_test *test;
1112         struct torture_tcase *tcase = torture_suite_add_tcase(suite, "DSSYNC");
1113
1114         torture_tcase_set_fixture(tcase,
1115                                   torture_dssync_tcase_setup,
1116                                   torture_dssync_tcase_teardown);
1117
1118         test = torture_tcase_add_simple_test(tcase, "DC_FetchData", (run_func)test_FetchData);
1119         test = torture_tcase_add_simple_test(tcase, "FetchNT4Data", (run_func)test_FetchNT4Data);
1120 }
1121