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