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