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