8f15f6f096cb0fc9353bc6ab867ced584cb8037f
[ambi/samba-autobuild/.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 "libcli/cldap/cldap.h"
27 #include "libcli/ldap/ldap_client.h"
28 #include "torture/torture.h"
29 #include "torture/ldap/proto.h"
30 #include "libcli/auth/libcli_auth.h"
31 #include "lib/crypto/crypto.h"
32 #include "auth/credentials/credentials.h"
33 #include "libcli/auth/libcli_auth.h"
34 #include "auth/gensec/gensec.h"
35 #include "param/param.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 };
46
47 struct DsSyncLDAPInfo {
48         struct ldap_connection *conn;
49 };
50
51 struct DsSyncTest {
52         struct dcerpc_binding *drsuapi_binding;
53         
54         const char *ldap_url;
55         const char *site_name;
56         
57         const char *domain_dn;
58
59         /* what we need to do as 'Administrator' */
60         struct {
61                 struct cli_credentials *credentials;
62                 struct DsSyncBindInfo drsuapi;
63                 struct DsSyncLDAPInfo ldap;
64         } admin;
65
66         /* what we need to do as the new dc machine account */
67         struct {
68                 struct cli_credentials *credentials;
69                 struct DsSyncBindInfo drsuapi;
70                 struct drsuapi_DsGetDCInfo2 dc_info2;
71                 struct GUID invocation_id;
72                 struct GUID object_guid;
73         } new_dc;
74
75         /* info about the old dc */
76         struct {
77                 struct drsuapi_DsGetDomainControllerInfo dc_info;
78         } old_dc;
79 };
80
81 static struct DsSyncTest *test_create_context(struct torture_context *tctx)
82 {
83         NTSTATUS status;
84         struct DsSyncTest *ctx;
85         struct drsuapi_DsBindInfo28 *our_bind_info28;
86         struct drsuapi_DsBindInfoCtr *our_bind_info_ctr;
87         const char *binding = torture_setting_string(tctx, "binding", NULL);
88         ctx = talloc_zero(tctx, struct DsSyncTest);
89         if (!ctx) return NULL;
90
91         status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
92         if (!NT_STATUS_IS_OK(status)) {
93                 printf("Bad binding string %s\n", binding);
94                 return NULL;
95         }
96         ctx->drsuapi_binding->flags |= DCERPC_SIGN | DCERPC_SEAL;
97
98         ctx->ldap_url = talloc_asprintf(ctx, "ldap://%s/", ctx->drsuapi_binding->host);
99
100         /* ctx->admin ...*/
101         ctx->admin.credentials                          = cmdline_credentials;
102
103         our_bind_info28                         = &ctx->admin.drsuapi.our_bind_info28;
104         our_bind_info28->supported_extensions   = 0xFFFFFFFF;
105         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
106         our_bind_info28->site_guid              = GUID_zero();
107         our_bind_info28->u1                     = 0;
108         our_bind_info28->repl_epoch             = 1;
109
110         our_bind_info_ctr                       = &ctx->admin.drsuapi.our_bind_info_ctr;
111         our_bind_info_ctr->length               = 28;
112         our_bind_info_ctr->info.info28          = *our_bind_info28;
113
114         GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
115
116         ctx->admin.drsuapi.req.in.bind_guid             = &ctx->admin.drsuapi.bind_guid;
117         ctx->admin.drsuapi.req.in.bind_info             = our_bind_info_ctr;
118         ctx->admin.drsuapi.req.out.bind_handle          = &ctx->admin.drsuapi.bind_handle;
119
120         /* ctx->new_dc ...*/
121         ctx->new_dc.credentials                 = cmdline_credentials;
122
123         our_bind_info28                         = &ctx->new_dc.drsuapi.our_bind_info28;
124         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
125         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
126         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
127         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
128         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
129         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
130         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
131         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
132         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
133         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
134         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
135         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
136         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
137         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
138         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
139         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
140         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
141         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
142         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
143         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
144         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
145         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
146         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
147         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
148         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
149         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
150         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
151         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
152         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "xpress", false)) {
153                 our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
154         }
155         our_bind_info28->site_guid              = GUID_zero();
156         our_bind_info28->u1                     = 508;
157         our_bind_info28->repl_epoch             = 0;
158
159         our_bind_info_ctr                       = &ctx->new_dc.drsuapi.our_bind_info_ctr;
160         our_bind_info_ctr->length               = 28;
161         our_bind_info_ctr->info.info28          = *our_bind_info28;
162
163         GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &ctx->new_dc.drsuapi.bind_guid);
164
165         ctx->new_dc.drsuapi.req.in.bind_guid            = &ctx->new_dc.drsuapi.bind_guid;
166         ctx->new_dc.drsuapi.req.in.bind_info            = our_bind_info_ctr;
167         ctx->new_dc.drsuapi.req.out.bind_handle         = &ctx->new_dc.drsuapi.bind_handle;
168
169         ctx->new_dc.invocation_id                       = ctx->new_dc.drsuapi.bind_guid;
170
171         /* ctx->old_dc ...*/
172
173         return ctx;
174 }
175
176 static bool _test_DsBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
177 {
178         NTSTATUS status;
179         bool ret = true;
180         struct event_context *event = NULL;
181
182         status = dcerpc_pipe_connect_b(ctx,
183                                        &b->pipe, ctx->drsuapi_binding, 
184                                        &ndr_table_drsuapi,
185                                        credentials, event, global_loadparm);
186         
187         if (!NT_STATUS_IS_OK(status)) {
188                 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
189                 return false;
190         }
191
192         status = dcerpc_drsuapi_DsBind(b->pipe, ctx, &b->req);
193         if (!NT_STATUS_IS_OK(status)) {
194                 const char *errstr = nt_errstr(status);
195                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
196                         errstr = dcerpc_errstr(ctx, b->pipe->last_fault_code);
197                 }
198                 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
199                 ret = false;
200         } else if (!W_ERROR_IS_OK(b->req.out.result)) {
201                 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
202                 ret = false;
203         }
204
205         ZERO_STRUCT(b->peer_bind_info28);
206         if (b->req.out.bind_info) {
207                 switch (b->req.out.bind_info->length) {
208                 case 24: {
209                         struct drsuapi_DsBindInfo24 *info24;
210                         info24 = &b->req.out.bind_info->info.info24;
211                         b->peer_bind_info28.supported_extensions= info24->supported_extensions;
212                         b->peer_bind_info28.site_guid           = info24->site_guid;
213                         b->peer_bind_info28.u1                  = info24->u1;
214                         b->peer_bind_info28.repl_epoch          = 0;
215                         break;
216                 }
217                 case 28:
218                         b->peer_bind_info28 = b->req.out.bind_info->info.info28;
219                         break;
220                 }
221         }
222
223         return ret;
224 }
225
226 static bool test_LDAPBind(struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
227 {
228         NTSTATUS status;
229         bool ret = true;
230
231         status = torture_ldap_connection(ctx, &l->conn, ctx->ldap_url);
232         if (!NT_STATUS_IS_OK(status)) {
233                 printf("failed to connect to LDAP: %s\n", ctx->ldap_url);
234                 return false;
235         }
236
237         printf("connected to LDAP: %s\n", ctx->ldap_url);
238
239         status = torture_ldap_bind_sasl(l->conn, credentials, global_loadparm);
240         if (!NT_STATUS_IS_OK(status)) {
241                 printf("failed to bind to LDAP:\n");
242                 return false;
243         }
244         printf("bound to LDAP.\n");
245
246         return ret;
247 }
248
249 static bool test_GetInfo(struct torture_context *tctx, struct DsSyncTest *ctx)
250 {
251         NTSTATUS status;
252         struct drsuapi_DsCrackNames r;
253         struct drsuapi_DsNameString names[1];
254         bool ret = true;
255
256         struct cldap_socket *cldap = cldap_socket_init(ctx, NULL);
257         struct cldap_netlogon search;
258         
259         r.in.bind_handle                = &ctx->admin.drsuapi.bind_handle;
260         r.in.level                      = 1;
261         r.in.req.req1.codepage          = 1252; /* western european */
262         r.in.req.req1.language          = 0x00000407; /* german */
263         r.in.req.req1.count             = 1;
264         r.in.req.req1.names             = names;
265         r.in.req.req1.format_flags      = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;                
266         r.in.req.req1.format_offered    = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
267         r.in.req.req1.format_desired    = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
268         names[0].str = talloc_asprintf(ctx, "%s\\", lp_workgroup(tctx->lp_ctx));
269
270         status = dcerpc_drsuapi_DsCrackNames(ctx->admin.drsuapi.pipe, ctx, &r);
271         if (!NT_STATUS_IS_OK(status)) {
272                 const char *errstr = nt_errstr(status);
273                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
274                         errstr = dcerpc_errstr(ctx, ctx->admin.drsuapi.pipe->last_fault_code);
275                 }
276                 printf("dcerpc_drsuapi_DsCrackNames failed - %s\n", errstr);
277                 return false;
278         } else if (!W_ERROR_IS_OK(r.out.result)) {
279                 printf("DsCrackNames failed - %s\n", win_errstr(r.out.result));
280                 return false;
281         }
282
283         ctx->domain_dn = r.out.ctr.ctr1->array[0].result_name;
284         
285         ZERO_STRUCT(search);
286         search.in.dest_address = ctx->drsuapi_binding->host;
287         search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
288         search.in.acct_control = -1;
289         search.in.version = 6;
290         status = cldap_netlogon(cldap, ctx, &search);
291         if (!NT_STATUS_IS_OK(status)) {
292                 const char *errstr = nt_errstr(status);
293                 ctx->site_name = talloc_asprintf(ctx, "%s", "Default-First-Site-Name");
294                 printf("cldap_netlogon() returned %s. Defaulting to Site-Name: %s\n", errstr, ctx->site_name);          
295         } else {
296                 ctx->site_name = talloc_steal(ctx, search.out.netlogon.logon5.client_site);
297                 printf("cldap_netlogon() returned Client Site-Name: %s.\n",ctx->site_name);
298                 printf("cldap_netlogon() returned Server Site-Name: %s.\n",search.out.netlogon.logon5.server_site);
299         }
300
301         return ret;
302 }
303
304 static DATA_BLOB decrypt_blob(TALLOC_CTX *mem_ctx,
305                               const DATA_BLOB *gensec_skey,
306                               bool rcrypt,
307                               struct drsuapi_DsReplicaObjectIdentifier *id,
308                               uint32_t rid,
309                               const DATA_BLOB *buffer)
310 {
311         DATA_BLOB confounder;
312         DATA_BLOB enc_buffer;
313
314         struct MD5Context md5;
315         uint8_t _enc_key[16];
316         DATA_BLOB enc_key;
317
318         DATA_BLOB dec_buffer;
319
320         uint32_t crc32_given;
321         uint32_t crc32_calc;
322         DATA_BLOB checked_buffer;
323
324         DATA_BLOB plain_buffer;
325
326         /*
327          * the combination "c[3] s[1] e[1] d[0]..."
328          * was successful!!!!!!!!!!!!!!!!!!!!!!!!!!
329          */
330
331         /* 
332          * the first 16 bytes at the beginning are the confounder
333          * followed by the 4 byte crc32 checksum
334          */
335         if (buffer->length < 20) {
336                 return data_blob_const(NULL, 0);
337         }
338         confounder = data_blob_const(buffer->data, 16);
339         enc_buffer = data_blob_const(buffer->data + 16, buffer->length - 16);
340
341         /* 
342          * build the encryption key md5 over the session key followed
343          * by the confounder
344          * 
345          * here the gensec session key is used and
346          * not the dcerpc ncacn_ip_tcp "SystemLibraryDTC" key!
347          */
348         enc_key = data_blob_const(_enc_key, sizeof(_enc_key));
349         MD5Init(&md5);
350         MD5Update(&md5, gensec_skey->data, gensec_skey->length);
351         MD5Update(&md5, confounder.data, confounder.length);
352         MD5Final(enc_key.data, &md5);
353
354         /*
355          * copy the encrypted buffer part and 
356          * decrypt it using the created encryption key using arcfour
357          */
358         dec_buffer = data_blob_talloc(mem_ctx, enc_buffer.data, enc_buffer.length);
359         if (!dec_buffer.data) {
360                 return data_blob_const(NULL, 0);
361         }
362         arcfour_crypt_blob(dec_buffer.data, dec_buffer.length, &enc_key);
363
364         /* 
365          * the first 4 byte are the crc32 checksum
366          * of the remaining bytes
367          */
368         crc32_given = IVAL(dec_buffer.data, 0);
369         crc32_calc = crc32_calc_buffer(dec_buffer.data + 4 , dec_buffer.length - 4);
370         if (crc32_given != crc32_calc) {
371                 DEBUG(0,("CRC32: given[0x%08X] calc[0x%08X]\n",
372                       crc32_given, crc32_calc));
373                 return data_blob_const(NULL, 0);
374         }
375         checked_buffer = data_blob_talloc(mem_ctx, dec_buffer.data + 4, dec_buffer.length - 4);
376         if (!checked_buffer.data) {
377                 return data_blob_const(NULL, 0);
378         }
379
380         /*
381          * some attributes seem to be in a usable form after this decryption
382          * (supplementalCredentials, priorValue, currentValue, trustAuthOutgoing,
383          *  trustAuthIncoming, initialAuthOutgoing, initialAuthIncoming)
384          * At least supplementalCredentials contains plaintext
385          * like "Primary:Kerberos" (in unicode form)
386          *
387          * some attributes seem to have some additional encryption
388          * dBCSPwd, unicodePwd, ntPwdHistory, lmPwdHistory
389          *
390          * it's the sam_rid_crypt() function, as the value is constant,
391          * so it doesn't depend on sessionkeys.
392          */
393         if (rcrypt) {
394                 uint32_t i, num_hashes;
395
396                 if ((checked_buffer.length % 16) != 0) {
397                         return data_blob_const(NULL, 0);
398                 }
399
400                 plain_buffer = data_blob_talloc(mem_ctx, checked_buffer.data, checked_buffer.length);
401                 if (!plain_buffer.data) {
402                         return data_blob_const(NULL, 0);
403                 }
404                         
405                 num_hashes = plain_buffer.length / 16;
406                 for (i = 0; i < num_hashes; i++) {
407                         uint32_t offset = i * 16;
408                         sam_rid_crypt(rid, checked_buffer.data + offset, plain_buffer.data + offset, 0);
409                 }
410         } else {
411                 plain_buffer = checked_buffer;
412         }
413
414         return plain_buffer;
415 }
416
417 static void test_analyse_objects(struct torture_context *tctx, 
418                                  struct DsSyncTest *ctx,
419                                  const DATA_BLOB *gensec_skey,
420                                  struct drsuapi_DsReplicaObjectListItemEx *cur)
421 {
422         static uint32_t object_id;
423         const char *save_values_dir;
424
425         if (!lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
426                 return; 
427         }
428
429         save_values_dir = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "save_pwd_blobs_dir");
430
431         for (; cur; cur = cur->next_object) {
432                 const char *dn;
433                 struct dom_sid *sid = NULL;
434                 uint32_t rid = 0;
435                 bool dn_printed = false;
436                 uint32_t i;
437
438                 if (!cur->object.identifier) continue;
439
440                 dn = cur->object.identifier->dn;
441                 if (cur->object.identifier->sid.num_auths > 0) {
442                         sid = &cur->object.identifier->sid;
443                         rid = sid->sub_auths[sid->num_auths - 1];
444                 }
445
446                 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
447                         const char *name = NULL;
448                         bool rcrypt = false;
449                         DATA_BLOB *enc_data = NULL;
450                         DATA_BLOB plain_data;
451                         struct drsuapi_DsReplicaAttribute *attr;
452                         attr = &cur->object.attribute_ctr.attributes[i];
453
454                         switch (attr->attid) {
455                         case DRSUAPI_ATTRIBUTE_dBCSPwd:
456                                 name    = "dBCSPwd";
457                                 rcrypt  = true;
458                                 break;
459                         case DRSUAPI_ATTRIBUTE_unicodePwd:
460                                 name    = "unicodePwd";
461                                 rcrypt  = true;
462                                 break;
463                         case DRSUAPI_ATTRIBUTE_ntPwdHistory:
464                                 name    = "ntPwdHistory";
465                                 rcrypt  = true;
466                                 break;
467                         case DRSUAPI_ATTRIBUTE_lmPwdHistory:
468                                 name    = "lmPwdHistory";
469                                 rcrypt  = true;
470                                 break;
471                         case DRSUAPI_ATTRIBUTE_supplementalCredentials:
472                                 name    = "supplementalCredentials";
473                                 break;
474                         case DRSUAPI_ATTRIBUTE_priorValue:
475                                 name    = "priorValue";
476                                 break;
477                         case DRSUAPI_ATTRIBUTE_currentValue:
478                                 name    = "currentValue";
479                                 break;
480                         case DRSUAPI_ATTRIBUTE_trustAuthOutgoing:
481                                 name    = "trustAuthOutgoing";
482                                 break;
483                         case DRSUAPI_ATTRIBUTE_trustAuthIncoming:
484                                 name    = "trustAuthIncoming";
485                                 break;
486                         case DRSUAPI_ATTRIBUTE_initialAuthOutgoing:
487                                 name    = "initialAuthOutgoing";
488                                 break;
489                         case DRSUAPI_ATTRIBUTE_initialAuthIncoming:
490                                 name    = "initialAuthIncoming";
491                                 break;
492                         default:
493                                 continue;
494                         }
495
496                         if (attr->value_ctr.num_values != 1) continue;
497
498                         if (!attr->value_ctr.values[0].blob) continue;
499
500                         enc_data = attr->value_ctr.values[0].blob;
501                         ZERO_STRUCT(plain_data);
502
503                         plain_data = decrypt_blob(ctx, gensec_skey, rcrypt,
504                                                   cur->object.identifier, rid,
505                                                   enc_data);
506                         if (!dn_printed) {
507                                 object_id++;
508                                 DEBUG(0,("DN[%u] %s\n", object_id, dn));
509                                 dn_printed = true;
510                         }
511                         DEBUGADD(0,("ATTR: %s enc.length=%lu plain.length=%lu\n",
512                                     name, (long)enc_data->length, (long)plain_data.length));
513                         if (plain_data.length) {
514                                 dump_data(0, plain_data.data, plain_data.length);
515                                 if (save_values_dir) {
516                                         char *fname;
517                                         fname = talloc_asprintf(ctx, "%s/%s%02d",
518                                                                 save_values_dir,
519                                                                 name, object_id);
520                                         if (fname) {
521                                                 bool ok;
522                                                 ok = file_save(fname, plain_data.data, plain_data.length);
523                                                 if (!ok) {
524                                                         DEBUGADD(0,("Failed to save '%s'\n", fname));
525                                                 }
526                                         }
527                                         talloc_free(fname);
528                                 }
529                         } else {
530                                 dump_data(0, enc_data->data, enc_data->length);
531                         }
532                 }
533         }
534 }
535
536 static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
537 {
538         NTSTATUS status;
539         bool ret = true;
540         int i, y = 0;
541         uint64_t highest_usn = 0;
542         const char *partition = NULL;
543         struct drsuapi_DsGetNCChanges r;
544         struct drsuapi_DsReplicaObjectIdentifier nc;
545         struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
546         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
547         int32_t out_level = 0;
548         struct GUID null_guid;
549         struct dom_sid null_sid;
550         DATA_BLOB gensec_skey;
551         struct {
552                 int32_t level;
553         } array[] = {
554 /*              {
555                         5
556                 },
557 */              {
558                         8
559                 }
560         };
561
562         ZERO_STRUCT(null_guid);
563         ZERO_STRUCT(null_sid);
564
565         partition = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "partition");
566         if (partition == NULL) {
567                 partition = ctx->domain_dn;
568                 printf("dssync:partition not specified, defaulting to %s.\n", ctx->domain_dn);
569         }
570
571         highest_usn = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "highest_usn", 0);
572
573         array[0].level = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "get_nc_changes_level", array[0].level);
574
575         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
576                 const struct samr_Password *nthash;
577                 nthash = cli_credentials_get_nt_hash(ctx->new_dc.credentials, ctx);
578                 if (nthash) {
579                         dump_data_pw("CREDENTIALS nthash:", nthash->hash, sizeof(nthash->hash));
580                 }
581         }
582         status = gensec_session_key(ctx->new_dc.drsuapi.pipe->conn->security_state.generic_state,
583                                     &gensec_skey);
584         if (!NT_STATUS_IS_OK(status)) {
585                 printf("failed to get gensec session key: %s\n", nt_errstr(status));
586                 return false;
587         }
588
589         for (i=0; i < ARRAY_SIZE(array); i++) {
590                 printf("testing DsGetNCChanges level %d\n",
591                         array[i].level);
592
593                 r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
594                 r.in.level              = &array[i].level;
595
596                 switch (*r.in.level) {
597                 case 5:
598                         nc.guid = null_guid;
599                         nc.sid  = null_sid;
600                         nc.dn   = partition; 
601
602                         r.in.req.req5.destination_dsa_guid              = ctx->new_dc.invocation_id;
603                         r.in.req.req5.source_dsa_invocation_id          = null_guid;
604                         r.in.req.req5.naming_context                    = &nc;
605                         r.in.req.req5.highwatermark.tmp_highest_usn     = highest_usn;
606                         r.in.req.req5.highwatermark.reserved_usn        = 0;
607                         r.in.req.req5.highwatermark.highest_usn         = highest_usn;
608                         r.in.req.req5.uptodateness_vector               = NULL;
609                         r.in.req.req5.replica_flags                     = 0;
610                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
611                                 r.in.req.req5.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
612                         }
613                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
614                                 r.in.req.req5.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
615                         }
616                         r.in.req.req5.replica_flags                     |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
617                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
618                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
619                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
620                                                                         ;
621                         r.in.req.req5.max_object_count                  = 133;
622                         r.in.req.req5.max_ndr_size                      = 1336770;
623                         r.in.req.req5.unknown4                          = 0;
624                         r.in.req.req5.h1                                = 0;
625
626                         break;
627                 case 8:
628                         nc.guid = null_guid;
629                         nc.sid  = null_sid;
630                         nc.dn   = partition; 
631                         /* nc.dn can be set to any other ad partition */
632                         
633                         r.in.req.req8.destination_dsa_guid              = ctx->new_dc.invocation_id;
634                         r.in.req.req8.source_dsa_invocation_id          = null_guid;
635                         r.in.req.req8.naming_context                    = &nc;
636                         r.in.req.req8.highwatermark.tmp_highest_usn     = highest_usn;
637                         r.in.req.req8.highwatermark.reserved_usn        = 0;
638                         r.in.req.req8.highwatermark.highest_usn         = highest_usn;
639                         r.in.req.req8.uptodateness_vector               = NULL;
640                         r.in.req.req8.replica_flags                     = 0;
641                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
642                                 r.in.req.req8.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
643                         }
644                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
645                                 r.in.req.req8.replica_flags             |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
646                         }
647                         r.in.req.req8.replica_flags                     |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
648                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
649                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
650                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
651                                                                         ;
652                         r.in.req.req8.max_object_count                  = 402;
653                         r.in.req.req8.max_ndr_size                      = 402116;
654
655                         r.in.req.req8.unknown4                          = 0;
656                         r.in.req.req8.h1                                = 0;
657                         r.in.req.req8.unique_ptr1                       = 0;
658                         r.in.req.req8.unique_ptr2                       = 0;
659                         r.in.req.req8.mapping_ctr.num_mappings          = 0;
660                         r.in.req.req8.mapping_ctr.mappings              = NULL;
661
662                         break;
663                 }
664                 
665                 printf("Dumping AD partition: %s\n", nc.dn);
666                 for (y=0; ;y++) {
667                         int32_t _level = 0;
668                         ZERO_STRUCT(r.out);
669                         r.out.level = &_level;
670
671                         if (*r.in.level == 5) {
672                                 DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
673                                         (long long)r.in.req.req5.highwatermark.tmp_highest_usn,
674                                         (long long)r.in.req.req5.highwatermark.highest_usn));
675                         }
676
677                         if (*r.in.level == 8) {
678                                 DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
679                                         (long long)r.in.req.req8.highwatermark.tmp_highest_usn,
680                                         (long long)r.in.req.req8.highwatermark.highest_usn));
681                         }
682
683                         status = dcerpc_drsuapi_DsGetNCChanges(ctx->new_dc.drsuapi.pipe, ctx, &r);
684                         if (!NT_STATUS_IS_OK(status)) {
685                                 const char *errstr = nt_errstr(status);
686                                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
687                                         errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
688                                 }
689                                 printf("dcerpc_drsuapi_DsGetNCChanges failed - %s\n", errstr);
690                                 ret = false;
691                         } else if (!W_ERROR_IS_OK(r.out.result)) {
692                                 printf("DsGetNCChanges failed - %s\n", win_errstr(r.out.result));
693                                 ret = false;
694                         }
695
696                         if (ret == true && *r.out.level == 1) {
697                                 out_level = 1;
698                                 ctr1 = &r.out.ctr.ctr1;
699                         } else if (ret == true && *r.out.level == 2) {
700                                 out_level = 1;
701                                 ctr1 = r.out.ctr.ctr2.ctr.mszip1.ctr1;
702                         }
703
704                         if (out_level == 1) {
705                                 DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
706                                         (long long)ctr1->new_highwatermark.tmp_highest_usn,
707                                         (long long)ctr1->new_highwatermark.highest_usn));
708
709                                 test_analyse_objects(tctx, ctx, &gensec_skey, ctr1->first_object);
710
711                                 if (ctr1->new_highwatermark.tmp_highest_usn > ctr1->new_highwatermark.highest_usn) {
712                                         r.in.req.req5.highwatermark = ctr1->new_highwatermark;
713                                         continue;
714                                 }
715                         }
716
717                         if (ret == true && *r.out.level == 6) {
718                                 out_level = 6;
719                                 ctr6 = &r.out.ctr.ctr6;
720                         } else if (ret == true && *r.out.level == 7
721                                    && r.out.ctr.ctr7.level == 6
722                                    && r.out.ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP) {
723                                 out_level = 6;
724                                 ctr6 = r.out.ctr.ctr7.ctr.mszip6.ctr6;
725                         }
726
727                         if (out_level == 6) {
728                                 DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
729                                         (long long)ctr6->new_highwatermark.tmp_highest_usn,
730                                         (long long)ctr6->new_highwatermark.highest_usn));
731
732                                 test_analyse_objects(tctx, ctx, &gensec_skey, ctr6->first_object);
733
734                                 if (ctr6->new_highwatermark.tmp_highest_usn > ctr6->new_highwatermark.highest_usn) {
735                                         r.in.req.req8.highwatermark = ctr6->new_highwatermark;
736                                         continue;
737                                 }
738                         }
739
740                         break;
741                 }
742         }
743
744         return ret;
745 }
746
747 static bool test_FetchNT4Data(struct torture_context *tctx, 
748                               struct DsSyncTest *ctx)
749 {
750         NTSTATUS status;
751         bool ret = true;
752         struct drsuapi_DsGetNT4ChangeLog r;
753         struct GUID null_guid;
754         struct dom_sid null_sid;
755         DATA_BLOB cookie;
756
757         ZERO_STRUCT(null_guid);
758         ZERO_STRUCT(null_sid);
759         ZERO_STRUCT(cookie);
760
761         ZERO_STRUCT(r);
762         r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
763         r.in.level              = 1;
764
765         r.in.req.req1.unknown1  = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "nt4-1", 3);
766         r.in.req.req1.unknown2  = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "nt4-2", 0x00004000);
767
768         while (1) {
769                 r.in.req.req1.length    = cookie.length;
770                 r.in.req.req1.data      = cookie.data;
771
772                 status = dcerpc_drsuapi_DsGetNT4ChangeLog(ctx->new_dc.drsuapi.pipe, ctx, &r);
773                 if (!NT_STATUS_IS_OK(status)) {
774                         const char *errstr = nt_errstr(status);
775                         if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
776                                 errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
777                         }
778                         printf("dcerpc_drsuapi_DsGetNT4ChangeLog failed - %s\n", errstr);
779                         ret = false;
780                 } else if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_DOMAIN_ROLE)) {
781                         printf("DsGetNT4ChangeLog not supported by target server\n");
782                         break;
783                 } else if (!W_ERROR_IS_OK(r.out.result)) {
784                         printf("DsGetNT4ChangeLog failed - %s\n", win_errstr(r.out.result));
785                         ret = false;
786                 } else if (r.out.level != 1) {
787                         printf("DsGetNT4ChangeLog unknown level - %u\n", r.out.level);
788                         ret = false;
789                 } else if (NT_STATUS_IS_OK(r.out.info.info1.status)) {
790                 } else if (NT_STATUS_EQUAL(r.out.info.info1.status, STATUS_MORE_ENTRIES)) {
791                         cookie.length   = r.out.info.info1.length1;
792                         cookie.data     = r.out.info.info1.data1;
793                         continue;
794                 } else {
795                         printf("DsGetNT4ChangeLog failed - %s\n", nt_errstr(r.out.info.info1.status));
796                         ret = false;
797                 }
798
799                 break;
800         }
801
802         return ret;
803 }
804
805 bool torture_rpc_dssync(struct torture_context *torture)
806 {
807         bool ret = true;
808         TALLOC_CTX *mem_ctx;
809         struct DsSyncTest *ctx;
810         
811         mem_ctx = talloc_init("torture_rpc_dssync");
812         ctx = test_create_context(torture);
813         
814         ret &= _test_DsBind(ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
815         if (!ret) {
816                 return ret;
817         }
818         ret &= test_LDAPBind(ctx, ctx->admin.credentials, &ctx->admin.ldap);
819         if (!ret) {
820                 return ret;
821         }
822         ret &= test_GetInfo(torture, ctx);
823         ret &= _test_DsBind(ctx, ctx->new_dc.credentials, &ctx->new_dc.drsuapi);
824         if (!ret) {
825                 return ret;
826         }
827         ret &= test_FetchData(torture, ctx);
828         ret &= test_FetchNT4Data(torture, ctx);
829
830         return ret;
831 }