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