r21236: store and orint the dcerpc session key, gensec session key and user nthash
[metze/samba/wip.git] / source / 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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
27 #include "libcli/cldap/cldap.h"
28 #include "libcli/ldap/ldap_client.h"
29 #include "torture/torture.h"
30 #include "torture/ldap/proto.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "lib/crypto/crypto.h"
33 #include "auth/credentials/credentials.h"
34 #include "libcli/auth/libcli_auth.h"
35 #include "auth/gensec/gensec.h"
36
37 struct DsSyncBindInfo {
38         struct dcerpc_pipe *pipe;
39         struct drsuapi_DsBind req;
40         struct GUID bind_guid;
41         struct drsuapi_DsBindInfoCtr our_bind_info_ctr;
42         struct drsuapi_DsBindInfo28 our_bind_info28;
43         struct drsuapi_DsBindInfo28 peer_bind_info28;
44         struct policy_handle bind_handle;
45         DATA_BLOB dce_key;
46         DATA_BLOB gen_key;
47         struct samr_Password nthash;
48 };
49
50 struct DsSyncLDAPInfo {
51         struct ldap_connection *conn;
52 };
53
54 struct DsSyncTest {
55         struct dcerpc_binding *drsuapi_binding;
56         
57         const char *ldap_url;
58         const char *site_name;
59         
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(TALLOC_CTX *mem_ctx)
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 = lp_parm_string(-1, "torture", "binding");
91         ctx = talloc_zero(mem_ctx, struct DsSyncTest);
92         if (!ctx) return NULL;
93
94         status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
95         if (!NT_STATUS_IS_OK(status)) {
96                 printf("Bad binding string %s\n", binding);
97                 return NULL;
98         }
99         ctx->drsuapi_binding->flags |= DCERPC_SIGN | DCERPC_SEAL;
100
101         ctx->ldap_url = talloc_asprintf(ctx, "ldap://%s/", ctx->drsuapi_binding->host);
102
103         /* ctx->admin ...*/
104         ctx->admin.credentials                          = cmdline_credentials;
105
106         our_bind_info28                         = &ctx->admin.drsuapi.our_bind_info28;
107         our_bind_info28->supported_extensions   = 0xFFFFFFFF;
108         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
109         our_bind_info28->site_guid              = GUID_zero();
110         our_bind_info28->u1                     = 0;
111         our_bind_info28->repl_epoch             = 1;
112
113         our_bind_info_ctr                       = &ctx->admin.drsuapi.our_bind_info_ctr;
114         our_bind_info_ctr->length               = 28;
115         our_bind_info_ctr->info.info28          = *our_bind_info28;
116
117         GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
118
119         ctx->admin.drsuapi.req.in.bind_guid             = &ctx->admin.drsuapi.bind_guid;
120         ctx->admin.drsuapi.req.in.bind_info             = our_bind_info_ctr;
121         ctx->admin.drsuapi.req.out.bind_handle          = &ctx->admin.drsuapi.bind_handle;
122
123         /* ctx->new_dc ...*/
124         ctx->new_dc.credentials                 = cmdline_credentials;
125
126         our_bind_info28                         = &ctx->new_dc.drsuapi.our_bind_info28;
127         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
128         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
129         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
130         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
131         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
132         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
133         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
134         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
135         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
136         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
137         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
138         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
139         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
140         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
141         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
142         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
143         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
144         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
145         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
146         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
147         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
148         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
149         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
150         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
151         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
152         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
153         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
154         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
155         if (lp_parm_bool(-1,"dssync","xpress",False)) {
156                 our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
157         }
158         our_bind_info28->site_guid              = GUID_zero();
159         our_bind_info28->u1                     = 508;
160         our_bind_info28->repl_epoch             = 0;
161
162         our_bind_info_ctr                       = &ctx->new_dc.drsuapi.our_bind_info_ctr;
163         our_bind_info_ctr->length               = 28;
164         our_bind_info_ctr->info.info28          = *our_bind_info28;
165
166         GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &ctx->new_dc.drsuapi.bind_guid);
167
168         ctx->new_dc.drsuapi.req.in.bind_guid            = &ctx->new_dc.drsuapi.bind_guid;
169         ctx->new_dc.drsuapi.req.in.bind_info            = our_bind_info_ctr;
170         ctx->new_dc.drsuapi.req.out.bind_handle         = &ctx->new_dc.drsuapi.bind_handle;
171
172         ctx->new_dc.invocation_id                       = ctx->new_dc.drsuapi.bind_guid;
173
174         /* ctx->old_dc ...*/
175
176         return ctx;
177 }
178
179 static BOOL _test_DsBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
180 {
181         NTSTATUS status;
182         BOOL ret = True;
183         struct event_context *event = NULL;
184         const struct samr_Password *nthash;
185
186         status = dcerpc_pipe_connect_b(ctx,
187                                        &b->pipe, ctx->drsuapi_binding, 
188                                            &dcerpc_table_drsuapi,
189                                        credentials, event);
190         
191         if (!NT_STATUS_IS_OK(status)) {
192                 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
193                 return False;
194         }
195
196         status = dcerpc_drsuapi_DsBind(b->pipe, ctx, &b->req);
197         if (!NT_STATUS_IS_OK(status)) {
198                 const char *errstr = nt_errstr(status);
199                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
200                         errstr = dcerpc_errstr(ctx, b->pipe->last_fault_code);
201                 }
202                 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
203                 ret = False;
204         } else if (!W_ERROR_IS_OK(b->req.out.result)) {
205                 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
206                 ret = False;
207         }
208
209         ZERO_STRUCT(b->peer_bind_info28);
210         if (b->req.out.bind_info) {
211                 switch (b->req.out.bind_info->length) {
212                 case 24: {
213                         struct drsuapi_DsBindInfo24 *info24;
214                         info24 = &b->req.out.bind_info->info.info24;
215                         b->peer_bind_info28.supported_extensions= info24->supported_extensions;
216                         b->peer_bind_info28.site_guid           = info24->site_guid;
217                         b->peer_bind_info28.u1                  = info24->u1;
218                         b->peer_bind_info28.repl_epoch          = 0;
219                         break;
220                 }
221                 case 28:
222                         b->peer_bind_info28 = b->req.out.bind_info->info.info28;
223                         break;
224                 }
225         }
226
227         dcerpc_fetch_session_key(b->pipe, &b->dce_key);
228         gensec_session_key(b->pipe->conn->security_state.generic_state, &b->gen_key);
229         nthash = cli_credentials_get_nt_hash(credentials, NULL);
230         if (nthash) b->nthash = *nthash;
231
232         if (lp_parm_bool(-1,"dssync","print_pwd_blobs",False)) {
233                 DEBUG(0,("DCERPC session key:\n"));
234                 dump_data(0, b->dce_key.data, b->dce_key.length);
235                 DEBUG(0,("GENSEC session key:\n"));
236                 dump_data(0, b->gen_key.data, b->gen_key.length);
237                 DEBUG(0,("CREDENTIALS nthash:\n"));
238                 dump_data(0, b->nthash.hash, sizeof(b->nthash.hash));
239         }
240
241         return ret;
242 }
243
244 static BOOL test_LDAPBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
245 {
246         NTSTATUS status;
247         BOOL ret = True;
248
249         status = torture_ldap_connection(ctx, &l->conn, ctx->ldap_url);
250         if (!NT_STATUS_IS_OK(status)) {
251                 printf("failed to connect to LDAP: %s\n", ctx->ldap_url);
252                 return False;
253         }
254
255         printf("connected to LDAP: %s\n", ctx->ldap_url);
256
257         status = torture_ldap_bind_sasl(l->conn, credentials);
258         if (!NT_STATUS_IS_OK(status)) {
259                 printf("failed to bind to LDAP:\n");
260                 return False;
261         }
262         printf("bound to LDAP.\n");
263
264         return ret;
265 }
266
267 static BOOL test_GetInfo(struct DsSyncTest *ctx)
268 {
269         NTSTATUS status;
270         struct drsuapi_DsCrackNames r;
271         struct drsuapi_DsNameString names[1];
272         BOOL ret = True;
273
274         struct cldap_socket *cldap = cldap_socket_init(ctx, NULL);
275         struct cldap_netlogon search;
276         
277         r.in.bind_handle                = &ctx->admin.drsuapi.bind_handle;
278         r.in.level                      = 1;
279         r.in.req.req1.codepage          = 1252; /* western european */
280         r.in.req.req1.language          = 0x00000407; /* german */
281         r.in.req.req1.count             = 1;
282         r.in.req.req1.names             = names;
283         r.in.req.req1.format_flags      = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;                
284         r.in.req.req1.format_offered    = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
285         r.in.req.req1.format_desired    = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
286         names[0].str = talloc_asprintf(ctx, "%s\\", lp_workgroup());
287
288         status = dcerpc_drsuapi_DsCrackNames(ctx->admin.drsuapi.pipe, ctx, &r);
289         if (!NT_STATUS_IS_OK(status)) {
290                 const char *errstr = nt_errstr(status);
291                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
292                         errstr = dcerpc_errstr(ctx, ctx->admin.drsuapi.pipe->last_fault_code);
293                 }
294                 printf("dcerpc_drsuapi_DsCrackNames failed - %s\n", errstr);
295                 return False;
296         } else if (!W_ERROR_IS_OK(r.out.result)) {
297                 printf("DsCrackNames failed - %s\n", win_errstr(r.out.result));
298                 return False;
299         }
300
301         ctx->domain_dn = r.out.ctr.ctr1->array[0].result_name;
302         
303         ZERO_STRUCT(search);
304         search.in.dest_address = ctx->drsuapi_binding->host;
305         search.in.acct_control = -1;
306         search.in.version = 6;
307         status = cldap_netlogon(cldap, ctx, &search);
308         if (!NT_STATUS_IS_OK(status)) {
309                 const char *errstr = nt_errstr(status);
310                 ctx->site_name = talloc_asprintf(ctx, "%s", "Default-First-Site-Name");
311                 printf("cldap_netlogon() returned %s. Defaulting to Site-Name: %s\n", errstr, ctx->site_name);          
312         } else {
313                 ctx->site_name = talloc_steal(ctx, search.out.netlogon.logon5.client_site);
314                 printf("cldap_netlogon() returned Client Site-Name: %s.\n",ctx->site_name);
315                 printf("cldap_netlogon() returned Server Site-Name: %s.\n",search.out.netlogon.logon5.server_site);
316         }
317
318         return ret;
319 }
320
321 static void test_analyse_objects(struct DsSyncTest *ctx,
322                                  struct drsuapi_DsReplicaObjectListItemEx *cur)
323 {
324         if (!lp_parm_bool(-1,"dssync","print_pwd_blobs",False)) {
325                 return; 
326         }
327
328         for (; cur; cur = cur->next_object) {
329                 const char *dn;
330                 BOOL dn_printed = False;
331                 uint32_t i;
332
333                 if (!cur->object.identifier) continue;
334
335                 dn = cur->object.identifier->dn;
336
337                 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
338                         const char *name = NULL;
339                         DATA_BLOB *data = NULL;
340                         struct drsuapi_DsReplicaAttribute *attr;
341                         attr = &cur->object.attribute_ctr.attributes[i];
342
343                         switch (attr->attid) {
344                         case DRSUAPI_ATTRIBUTE_dBCSPwd:
345                                 name    = "dBCSPwd";
346                                 break;
347                         case DRSUAPI_ATTRIBUTE_unicodePwd:
348                                 name    = "unicodePwd";
349                                 break;
350                         case DRSUAPI_ATTRIBUTE_ntPwdHistory:
351                                 name    = "ntPwdHistory";
352                                 break;
353                         case DRSUAPI_ATTRIBUTE_lmPwdHistory:
354                                 name    = "lmPwdHistory";
355                                 break;
356                         case DRSUAPI_ATTRIBUTE_supplementalCredentials:
357                                 name    = "supplementalCredentials";
358                                 break;
359                         case DRSUAPI_ATTRIBUTE_priorValue:
360                                 name    = "priorValue";
361                                 break;
362                         case DRSUAPI_ATTRIBUTE_currentValue:
363                                 name    = "currentValue";
364                                 break;
365                         case DRSUAPI_ATTRIBUTE_trustAuthOutgoing:
366                                 name    = "trustAuthOutgoing";
367                                 break;
368                         case DRSUAPI_ATTRIBUTE_trustAuthIncoming:
369                                 name    = "trustAuthIncoming";
370                                 break;
371                         case DRSUAPI_ATTRIBUTE_initialAuthOutgoing:
372                                 name    = "initialAuthOutgoing";
373                                 break;
374                         case DRSUAPI_ATTRIBUTE_initialAuthIncoming:
375                                 name    = "initialAuthIncoming";
376                                 break;
377                         default:
378                                 continue;
379                         }
380
381                         if (attr->value_ctr.num_values != 1) continue;
382
383                         if (!attr->value_ctr.values[0].blob) continue;
384
385                         data = attr->value_ctr.values[0].blob;
386
387                         if (!dn_printed) {
388                                 DEBUG(0,("DN: %s\n", dn));
389                                 dn_printed = True;
390                         }
391                         DEBUGADD(0,("ATTR: %s data_blob.length=%lu\n",
392                                     name, (long)data->length));
393                         dump_data(0,data->data, data->length);
394                 }
395         }
396 }
397
398 static BOOL test_FetchData(struct DsSyncTest *ctx)
399 {
400         NTSTATUS status;
401         BOOL ret = True;
402         int i, y = 0;
403         uint64_t highest_usn = 0;
404         const char *partition = NULL;
405         struct drsuapi_DsGetNCChanges r;
406         struct drsuapi_DsReplicaObjectIdentifier nc;
407         struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
408         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
409         int32_t out_level = 0;
410         struct GUID null_guid;
411         struct dom_sid null_sid;
412         struct {
413                 int32_t level;
414         } array[] = {
415 /*              {
416                         5
417                 },
418 */              {
419                         8
420                 }
421         };
422
423         ZERO_STRUCT(null_guid);
424         ZERO_STRUCT(null_sid);
425
426         partition = lp_parm_string(-1, "dssync", "partition");
427         if (partition == NULL) {
428                 partition = ctx->domain_dn;
429                 printf("dssync:partition not specified, defaulting to %s.\n", ctx->domain_dn);
430         }
431
432         highest_usn = lp_parm_int(-1, "dssync", "highest_usn", 0);
433
434         for (i=0; i < ARRAY_SIZE(array); i++) {
435                 printf("testing DsGetNCChanges level %d\n",
436                         array[i].level);
437
438                 r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
439                 r.in.level              = &array[i].level;
440
441                 switch (*r.in.level) {
442                 case 5:
443                         nc.guid = null_guid;
444                         nc.sid  = null_sid;
445                         nc.dn   = partition; 
446
447                         r.in.req.req5.destination_dsa_guid              = ctx->new_dc.invocation_id;
448                         r.in.req.req5.source_dsa_invocation_id          = null_guid;
449                         r.in.req.req5.naming_context                    = &nc;
450                         r.in.req.req5.highwatermark.tmp_highest_usn     = highest_usn;
451                         r.in.req.req5.highwatermark.reserved_usn        = 0;
452                         r.in.req.req5.highwatermark.highest_usn         = highest_usn;
453                         r.in.req.req5.uptodateness_vector               = NULL;
454                         r.in.req.req5.replica_flags                     = 0;
455                         if (lp_parm_bool(-1,"dssync","compression",False)) {
456                                 r.in.req.req5.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
457                         }
458                         if (lp_parm_bool(-1,"dssync","neighbour_writeable",True)) {
459                                 r.in.req.req5.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
460                         }
461                         r.in.req.req5.replica_flags                     |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
462                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
463                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
464                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
465                                                                         ;
466                         r.in.req.req5.max_object_count                  = 133;
467                         r.in.req.req5.max_ndr_size                      = 1336770;
468                         r.in.req.req5.unknown4                          = 0;
469                         r.in.req.req5.h1                                = 0;
470
471                         break;
472                 case 8:
473                         nc.guid = null_guid;
474                         nc.sid  = null_sid;
475                         nc.dn   = partition; 
476                         /* nc.dn can be set to any other ad partition */
477                         
478                         r.in.req.req8.destination_dsa_guid              = ctx->new_dc.invocation_id;
479                         r.in.req.req8.source_dsa_invocation_id          = null_guid;
480                         r.in.req.req8.naming_context                    = &nc;
481                         r.in.req.req8.highwatermark.tmp_highest_usn     = highest_usn;
482                         r.in.req.req8.highwatermark.reserved_usn        = 0;
483                         r.in.req.req8.highwatermark.highest_usn         = highest_usn;
484                         r.in.req.req8.uptodateness_vector               = NULL;
485                         r.in.req.req8.replica_flags                     = 0;
486                         if (lp_parm_bool(-1,"dssync","compression",False)) {
487                                 r.in.req.req8.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
488                         }
489                         if (lp_parm_bool(-1,"dssync","neighbour_writeable",True)) {
490                                 r.in.req.req8.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
491                         }
492                         r.in.req.req8.replica_flags                     |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
493                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
494                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
495                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
496                                                                         ;
497                         r.in.req.req8.max_object_count                  = 402;
498                         r.in.req.req8.max_ndr_size                      = 402116;
499
500                         r.in.req.req8.unknown4                          = 0;
501                         r.in.req.req8.h1                                = 0;
502                         r.in.req.req8.unique_ptr1                       = 0;
503                         r.in.req.req8.unique_ptr2                       = 0;
504                         r.in.req.req8.mapping_ctr.num_mappings          = 0;
505                         r.in.req.req8.mapping_ctr.mappings              = NULL;
506
507                         break;
508                 }
509                 
510                 printf("Dumping AD partition: %s\n", nc.dn);
511                 for (y=0; ;y++) {
512                         int32_t _level = 0;
513                         ZERO_STRUCT(r.out);
514                         r.out.level = &_level;
515
516                         if (*r.in.level == 5) {
517                                 DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
518                                         (long long)r.in.req.req5.highwatermark.tmp_highest_usn,
519                                         (long long)r.in.req.req5.highwatermark.highest_usn));
520                         }
521
522                         if (*r.in.level == 8) {
523                                 DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
524                                         (long long)r.in.req.req8.highwatermark.tmp_highest_usn,
525                                         (long long)r.in.req.req8.highwatermark.highest_usn));
526                         }
527
528                         status = dcerpc_drsuapi_DsGetNCChanges(ctx->new_dc.drsuapi.pipe, ctx, &r);
529                         if (!NT_STATUS_IS_OK(status)) {
530                                 const char *errstr = nt_errstr(status);
531                                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
532                                         errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
533                                 }
534                                 printf("dcerpc_drsuapi_DsGetNCChanges failed - %s\n", errstr);
535                                 ret = False;
536                         } else if (!W_ERROR_IS_OK(r.out.result)) {
537                                 printf("DsGetNCChanges failed - %s\n", win_errstr(r.out.result));
538                                 ret = False;
539                         }
540
541                         if (ret == True && *r.out.level == 1) {
542                                 out_level = 1;
543                                 ctr1 = &r.out.ctr.ctr1;
544                         } else if (ret == True && *r.out.level == 2) {
545                                 out_level = 1;
546                                 ctr1 = r.out.ctr.ctr2.ctr.mszip1.ctr1;
547                         }
548
549                         if (out_level == 1) {
550                                 DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
551                                         (long long)ctr1->new_highwatermark.tmp_highest_usn,
552                                         (long long)ctr1->new_highwatermark.highest_usn));
553
554                                 test_analyse_objects(ctx, ctr1->first_object);
555
556                                 if (ctr1->new_highwatermark.tmp_highest_usn > ctr1->new_highwatermark.highest_usn) {
557                                         r.in.req.req5.highwatermark = ctr1->new_highwatermark;
558                                         continue;
559                                 }
560                         }
561
562                         if (ret == True && *r.out.level == 6) {
563                                 out_level = 6;
564                                 ctr6 = &r.out.ctr.ctr6;
565                         } else if (ret == True && *r.out.level == 7
566                                    && r.out.ctr.ctr7.level == 6
567                                    && r.out.ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP) {
568                                 out_level = 6;
569                                 ctr6 = r.out.ctr.ctr7.ctr.mszip6.ctr6;
570                         }
571
572                         if (out_level == 6) {
573                                 DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
574                                         (long long)ctr6->new_highwatermark.tmp_highest_usn,
575                                         (long long)ctr6->new_highwatermark.highest_usn));
576
577                                 test_analyse_objects(ctx, ctr6->first_object);
578
579                                 if (ctr6->new_highwatermark.tmp_highest_usn > ctr6->new_highwatermark.highest_usn) {
580                                         r.in.req.req8.highwatermark = ctr6->new_highwatermark;
581                                         continue;
582                                 }
583                         }
584
585                         break;
586                 }
587         }
588
589         return ret;
590 }
591
592 static BOOL test_FetchNT4Data(struct DsSyncTest *ctx)
593 {
594         NTSTATUS status;
595         BOOL ret = True;
596         int i, y = 0;
597         uint64_t highest_usn = 0;
598         const char *partition = NULL;
599         struct drsuapi_DsGetNT4ChangeLog r;
600         int32_t out_level = 0;
601         struct GUID null_guid;
602         struct dom_sid null_sid;
603         DATA_BLOB cookie;
604
605         ZERO_STRUCT(null_guid);
606         ZERO_STRUCT(null_sid);
607         ZERO_STRUCT(cookie);
608
609         ZERO_STRUCT(r);
610         r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
611         r.in.level              = 1;
612
613         r.in.req.req1.unknown1  = lp_parm_int(-1, "dssync", "nt4-1", 3);
614         r.in.req.req1.unknown2  = lp_parm_int(-1, "dssync", "nt4-2", 0x00004000);
615
616         while (1) {
617                 r.in.req.req1.length    = cookie.length;
618                 r.in.req.req1.data      = cookie.data;
619
620                 status = dcerpc_drsuapi_DsGetNT4ChangeLog(ctx->new_dc.drsuapi.pipe, ctx, &r);
621                 if (!NT_STATUS_IS_OK(status)) {
622                         const char *errstr = nt_errstr(status);
623                         if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
624                                 errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
625                         }
626                         printf("dcerpc_drsuapi_DsGetNT4ChangeLog failed - %s\n", errstr);
627                         ret = False;
628                 } else if (!W_ERROR_IS_OK(r.out.result)) {
629                         printf("DsGetNT4ChangeLog failed - %s\n", win_errstr(r.out.result));
630                         ret = False;
631                 } else if (r.out.level != 1) {
632                         printf("DsGetNT4ChangeLog unknown level - %u\n", r.out.level);
633                         ret = False;
634                 } else if (NT_STATUS_IS_OK(r.out.info.info1.status)) {
635                 } else if (NT_STATUS_EQUAL(r.out.info.info1.status, STATUS_MORE_ENTRIES)) {
636                         cookie.length   = r.out.info.info1.length1;
637                         cookie.data     = r.out.info.info1.data1;
638                         continue;
639                 } else {
640                         printf("DsGetNT4ChangeLog failed - %s\n", nt_errstr(r.out.info.info1.status));
641                         ret = False;
642                 }
643
644                 break;
645         }
646
647         return ret;
648 }
649
650 BOOL torture_rpc_dssync(struct torture_context *torture)
651 {
652         BOOL ret = True;
653         TALLOC_CTX *mem_ctx;
654         struct DsSyncTest *ctx;
655         
656         mem_ctx = talloc_init("torture_rpc_dssync");
657         ctx = test_create_context(mem_ctx);
658         
659         ret &= _test_DsBind(ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
660         ret &= test_LDAPBind(ctx, ctx->admin.credentials, &ctx->admin.ldap);
661         ret &= test_GetInfo(ctx);
662         ret &= _test_DsBind(ctx, ctx->new_dc.credentials, &ctx->new_dc.drsuapi);
663         ret &= test_FetchData(ctx);
664         ret &= test_FetchNT4Data(ctx);
665
666         return ret;
667 }