a9341d569b47da259c489366adaeda3ccd3d4083
[ira/wip.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 "../libcli/drsuapi/drsuapi.h"
34 #include "auth/credentials/credentials.h"
35 #include "libcli/auth/libcli_auth.h"
36 #include "auth/gensec/gensec.h"
37 #include "param/param.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "torture/rpc/rpc.h"
40 #include "torture/drs/proto.h"
41
42
43 struct DsSyncBindInfo {
44         struct dcerpc_pipe *pipe;
45         struct drsuapi_DsBind req;
46         struct GUID bind_guid;
47         struct drsuapi_DsBindInfoCtr our_bind_info_ctr;
48         struct drsuapi_DsBindInfo28 our_bind_info28;
49         struct drsuapi_DsBindInfo28 peer_bind_info28;
50         struct policy_handle bind_handle;
51 };
52
53 struct DsSyncLDAPInfo {
54         struct ldap_connection *conn;
55 };
56
57 struct DsSyncTest {
58         struct dcerpc_binding *drsuapi_binding;
59         
60         const char *ldap_url;
61         const char *site_name;
62         
63         const char *domain_dn;
64
65         /* what we need to do as 'Administrator' */
66         struct {
67                 struct cli_credentials *credentials;
68                 struct DsSyncBindInfo drsuapi;
69                 struct DsSyncLDAPInfo ldap;
70         } admin;
71
72         /* what we need to do as the new dc machine account */
73         struct {
74                 struct cli_credentials *credentials;
75                 struct DsSyncBindInfo drsuapi;
76                 struct drsuapi_DsGetDCInfo2 dc_info2;
77                 struct GUID invocation_id;
78                 struct GUID object_guid;
79         } new_dc;
80
81         /* info about the old dc */
82         struct {
83                 struct drsuapi_DsGetDomainControllerInfo dc_info;
84         } old_dc;
85 };
86
87 static struct DsSyncTest *test_create_context(struct torture_context *tctx)
88 {
89         NTSTATUS status;
90         struct DsSyncTest *ctx;
91         struct drsuapi_DsBindInfo28 *our_bind_info28;
92         struct drsuapi_DsBindInfoCtr *our_bind_info_ctr;
93         const char *binding = torture_setting_string(tctx, "binding", NULL);
94         ctx = talloc_zero(tctx, struct DsSyncTest);
95         if (!ctx) return NULL;
96
97         status = dcerpc_parse_binding(ctx, binding, &ctx->drsuapi_binding);
98         if (!NT_STATUS_IS_OK(status)) {
99                 printf("Bad binding string %s\n", binding);
100                 return NULL;
101         }
102         ctx->drsuapi_binding->flags |= DCERPC_SIGN | DCERPC_SEAL;
103
104         ctx->ldap_url = talloc_asprintf(ctx, "ldap://%s/", ctx->drsuapi_binding->host);
105
106         /* ctx->admin ...*/
107         ctx->admin.credentials                          = cmdline_credentials;
108
109         our_bind_info28                         = &ctx->admin.drsuapi.our_bind_info28;
110         our_bind_info28->supported_extensions   = 0xFFFFFFFF;
111         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
112         our_bind_info28->site_guid              = GUID_zero();
113         our_bind_info28->pid                    = 0;
114         our_bind_info28->repl_epoch             = 1;
115
116         our_bind_info_ctr                       = &ctx->admin.drsuapi.our_bind_info_ctr;
117         our_bind_info_ctr->length               = 28;
118         our_bind_info_ctr->info.info28          = *our_bind_info28;
119
120         GUID_from_string(DRSUAPI_DS_BIND_GUID, &ctx->admin.drsuapi.bind_guid);
121
122         ctx->admin.drsuapi.req.in.bind_guid             = &ctx->admin.drsuapi.bind_guid;
123         ctx->admin.drsuapi.req.in.bind_info             = our_bind_info_ctr;
124         ctx->admin.drsuapi.req.out.bind_handle          = &ctx->admin.drsuapi.bind_handle;
125
126         /* ctx->new_dc ...*/
127         ctx->new_dc.credentials                 = cmdline_credentials;
128
129         our_bind_info28                         = &ctx->new_dc.drsuapi.our_bind_info28;
130         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
131         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
132         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
133         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
134         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
135         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
136         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
137         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
138         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
139         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
140         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
141         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
142         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
143         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
144         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
145         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
146         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
147         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
148         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
149         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
150         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
151         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
152         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
153         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
154         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
155         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
156         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
157         our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
158         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "xpress", false)) {
159                 our_bind_info28->supported_extensions   |= DRSUAPI_SUPPORTED_EXTENSION_XPRESS_COMPRESS;
160         }
161         our_bind_info28->site_guid              = GUID_zero();
162         our_bind_info28->pid                    = 0;
163         our_bind_info28->repl_epoch             = 0;
164
165         our_bind_info_ctr                       = &ctx->new_dc.drsuapi.our_bind_info_ctr;
166         our_bind_info_ctr->length               = 28;
167         our_bind_info_ctr->info.info28          = *our_bind_info28;
168
169         GUID_from_string(DRSUAPI_DS_BIND_GUID_W2K3, &ctx->new_dc.drsuapi.bind_guid);
170
171         ctx->new_dc.drsuapi.req.in.bind_guid            = &ctx->new_dc.drsuapi.bind_guid;
172         ctx->new_dc.drsuapi.req.in.bind_info            = our_bind_info_ctr;
173         ctx->new_dc.drsuapi.req.out.bind_handle         = &ctx->new_dc.drsuapi.bind_handle;
174
175         ctx->new_dc.invocation_id                       = ctx->new_dc.drsuapi.bind_guid;
176
177         /* ctx->old_dc ...*/
178
179         return ctx;
180 }
181
182 static bool _test_DsBind(struct torture_context *tctx,
183                          struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
184 {
185         NTSTATUS status;
186         bool ret = true;
187
188         status = dcerpc_pipe_connect_b(ctx,
189                                        &b->pipe, ctx->drsuapi_binding, 
190                                        &ndr_table_drsuapi,
191                                        credentials, tctx->ev, tctx->lp_ctx);
192         
193         if (!NT_STATUS_IS_OK(status)) {
194                 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
195                 return false;
196         }
197
198         status = dcerpc_drsuapi_DsBind(b->pipe, ctx, &b->req);
199         if (!NT_STATUS_IS_OK(status)) {
200                 const char *errstr = nt_errstr(status);
201                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
202                         errstr = dcerpc_errstr(ctx, b->pipe->last_fault_code);
203                 }
204                 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
205                 ret = false;
206         } else if (!W_ERROR_IS_OK(b->req.out.result)) {
207                 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
208                 ret = false;
209         }
210
211         ZERO_STRUCT(b->peer_bind_info28);
212         if (b->req.out.bind_info) {
213                 switch (b->req.out.bind_info->length) {
214                 case 24: {
215                         struct drsuapi_DsBindInfo24 *info24;
216                         info24 = &b->req.out.bind_info->info.info24;
217                         b->peer_bind_info28.supported_extensions= info24->supported_extensions;
218                         b->peer_bind_info28.site_guid           = info24->site_guid;
219                         b->peer_bind_info28.pid                 = info24->pid;
220                         b->peer_bind_info28.repl_epoch          = 0;
221                         break;
222                 }
223                 case 48: {
224                         struct drsuapi_DsBindInfo48 *info48;
225                         info48 = &b->req.out.bind_info->info.info48;
226                         b->peer_bind_info28.supported_extensions= info48->supported_extensions;
227                         b->peer_bind_info28.site_guid           = info48->site_guid;
228                         b->peer_bind_info28.pid                 = info48->pid;
229                         b->peer_bind_info28.repl_epoch          = info48->repl_epoch;
230                         break;
231                 }
232                 case 28:
233                         b->peer_bind_info28 = b->req.out.bind_info->info.info28;
234                         break;
235                 default:
236                         printf("DsBind - warning: unknown BindInfo length: %u\n",
237                                b->req.out.bind_info->length);
238                 }
239         }
240
241         return ret;
242 }
243
244 static bool test_LDAPBind(struct torture_context *tctx, struct DsSyncTest *ctx, 
245                           struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
246 {
247         NTSTATUS status;
248         bool ret = true;
249
250         status = torture_ldap_connection(tctx, &l->conn, ctx->ldap_url);
251         if (!NT_STATUS_IS_OK(status)) {
252                 printf("failed to connect to LDAP: %s\n", ctx->ldap_url);
253                 return false;
254         }
255
256         printf("connected to LDAP: %s\n", ctx->ldap_url);
257
258         status = torture_ldap_bind_sasl(l->conn, credentials, tctx->lp_ctx);
259         if (!NT_STATUS_IS_OK(status)) {
260                 printf("failed to bind to LDAP:\n");
261                 return false;
262         }
263         printf("bound to LDAP.\n");
264
265         return ret;
266 }
267
268 static bool test_GetInfo(struct torture_context *tctx, struct DsSyncTest *ctx)
269 {
270         NTSTATUS status;
271         struct drsuapi_DsCrackNames r;
272         union drsuapi_DsNameRequest req;
273         union drsuapi_DsNameCtr ctr;
274         int32_t level_out = 0;
275         struct drsuapi_DsNameString names[1];
276         bool ret = true;
277         struct cldap_socket *cldap;
278         struct cldap_netlogon search;
279
280         status = cldap_socket_init(ctx, NULL, NULL, NULL, &cldap);
281         if (!NT_STATUS_IS_OK(status)) {
282                 printf("failed to setup cldap socket - %s\n",
283                         nt_errstr(status));
284                 return false;
285         }
286
287         r.in.bind_handle                = &ctx->admin.drsuapi.bind_handle;
288         r.in.level                      = 1;
289         r.in.req                        = &req;
290         r.in.req->req1.codepage         = 1252; /* western european */
291         r.in.req->req1.language         = 0x00000407; /* german */
292         r.in.req->req1.count            = 1;
293         r.in.req->req1.names            = names;
294         r.in.req->req1.format_flags     = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
295         r.in.req->req1.format_offered   = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
296         r.in.req->req1.format_desired   = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
297         names[0].str = talloc_asprintf(ctx, "%s\\", lp_workgroup(tctx->lp_ctx));
298
299         r.out.level_out                 = &level_out;
300         r.out.ctr                       = &ctr;
301
302         status = dcerpc_drsuapi_DsCrackNames(ctx->admin.drsuapi.pipe, ctx, &r);
303         if (!NT_STATUS_IS_OK(status)) {
304                 const char *errstr = nt_errstr(status);
305                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
306                         errstr = dcerpc_errstr(ctx, ctx->admin.drsuapi.pipe->last_fault_code);
307                 }
308                 printf("dcerpc_drsuapi_DsCrackNames failed - %s\n", errstr);
309                 return false;
310         } else if (!W_ERROR_IS_OK(r.out.result)) {
311                 printf("DsCrackNames failed - %s\n", win_errstr(r.out.result));
312                 return false;
313         }
314
315         ctx->domain_dn = r.out.ctr->ctr1->array[0].result_name;
316         
317         ZERO_STRUCT(search);
318         search.in.dest_address = ctx->drsuapi_binding->host;
319         search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
320         search.in.acct_control = -1;
321         search.in.version               = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
322         search.in.map_response = true;
323         status = cldap_netlogon(cldap, lp_iconv_convenience(tctx->lp_ctx), ctx, &search);
324         if (!NT_STATUS_IS_OK(status)) {
325                 const char *errstr = nt_errstr(status);
326                 ctx->site_name = talloc_asprintf(ctx, "%s", "Default-First-Site-Name");
327                 printf("cldap_netlogon() returned %s. Defaulting to Site-Name: %s\n", errstr, ctx->site_name);          
328         } else {
329                 ctx->site_name = talloc_steal(ctx, search.out.netlogon.data.nt5_ex.client_site);
330                 printf("cldap_netlogon() returned Client Site-Name: %s.\n",ctx->site_name);
331                 printf("cldap_netlogon() returned Server Site-Name: %s.\n",search.out.netlogon.data.nt5_ex.server_site);
332         }
333
334         if (!ctx->domain_dn) {
335                 struct ldb_context *ldb = ldb_init(ctx, tctx->ev);
336                 struct ldb_dn *dn = samdb_dns_domain_to_dn(ldb, ctx, search.out.netlogon.data.nt5_ex.dns_domain);
337                 ctx->domain_dn = ldb_dn_alloc_linearized(ctx, dn);
338                 talloc_free(dn);
339                 talloc_free(ldb);
340         }
341
342         return ret;
343 }
344
345 static void test_analyse_objects(struct torture_context *tctx, 
346                                  struct DsSyncTest *ctx,
347                                  const DATA_BLOB *gensec_skey,
348                                  struct drsuapi_DsReplicaObjectListItemEx *cur)
349 {
350         static uint32_t object_id;
351         const char *save_values_dir;
352
353         if (!lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
354                 return; 
355         }
356
357         save_values_dir = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "save_pwd_blobs_dir");
358
359         for (; cur; cur = cur->next_object) {
360                 const char *dn;
361                 struct dom_sid *sid = NULL;
362                 uint32_t rid = 0;
363                 bool dn_printed = false;
364                 uint32_t i;
365
366                 if (!cur->object.identifier) continue;
367
368                 dn = cur->object.identifier->dn;
369                 if (cur->object.identifier->sid.num_auths > 0) {
370                         sid = &cur->object.identifier->sid;
371                         rid = sid->sub_auths[sid->num_auths - 1];
372                 }
373
374                 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
375                         WERROR werr;
376                         const char *name = NULL;
377                         bool rcrypt = false;
378                         DATA_BLOB *enc_data = NULL;
379                         DATA_BLOB plain_data;
380                         struct drsuapi_DsReplicaAttribute *attr;
381                         ndr_pull_flags_fn_t pull_fn = NULL;
382                         ndr_print_fn_t print_fn = NULL;
383                         void *ptr = NULL;
384                         attr = &cur->object.attribute_ctr.attributes[i];
385
386                         switch (attr->attid) {
387                         case DRSUAPI_ATTRIBUTE_dBCSPwd:
388                                 name    = "dBCSPwd";
389                                 rcrypt  = true;
390                                 break;
391                         case DRSUAPI_ATTRIBUTE_unicodePwd:
392                                 name    = "unicodePwd";
393                                 rcrypt  = true;
394                                 break;
395                         case DRSUAPI_ATTRIBUTE_ntPwdHistory:
396                                 name    = "ntPwdHistory";
397                                 rcrypt  = true;
398                                 break;
399                         case DRSUAPI_ATTRIBUTE_lmPwdHistory:
400                                 name    = "lmPwdHistory";
401                                 rcrypt  = true;
402                                 break;
403                         case DRSUAPI_ATTRIBUTE_supplementalCredentials:
404                                 name    = "supplementalCredentials";
405                                 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob;
406                                 print_fn = (ndr_print_fn_t)ndr_print_supplementalCredentialsBlob;
407                                 ptr = talloc(ctx, struct supplementalCredentialsBlob);
408                                 break;
409                         case DRSUAPI_ATTRIBUTE_priorValue:
410                                 name    = "priorValue";
411                                 break;
412                         case DRSUAPI_ATTRIBUTE_currentValue:
413                                 name    = "currentValue";
414                                 break;
415                         case DRSUAPI_ATTRIBUTE_trustAuthOutgoing:
416                                 name    = "trustAuthOutgoing";
417                                 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob;
418                                 print_fn = (ndr_print_fn_t)ndr_print_trustAuthInOutBlob;
419                                 ptr = talloc(ctx, struct trustAuthInOutBlob);
420                                 break;
421                         case DRSUAPI_ATTRIBUTE_trustAuthIncoming:
422                                 name    = "trustAuthIncoming";
423                                 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob;
424                                 print_fn = (ndr_print_fn_t)ndr_print_trustAuthInOutBlob;
425                                 ptr = talloc(ctx, struct trustAuthInOutBlob);
426                                 break;
427                         case DRSUAPI_ATTRIBUTE_initialAuthOutgoing:
428                                 name    = "initialAuthOutgoing";
429                                 break;
430                         case DRSUAPI_ATTRIBUTE_initialAuthIncoming:
431                                 name    = "initialAuthIncoming";
432                                 break;
433                         default:
434                                 continue;
435                         }
436
437                         if (attr->value_ctr.num_values != 1) continue;
438
439                         if (!attr->value_ctr.values[0].blob) continue;
440
441                         enc_data = attr->value_ctr.values[0].blob;
442                         ZERO_STRUCT(plain_data);
443
444                         werr = drsuapi_decrypt_attribute_value(ctx, gensec_skey, rcrypt,
445                                                                rid,
446                                                                enc_data, &plain_data);
447                         if (!W_ERROR_IS_OK(werr)) {
448                                 DEBUG(0, ("Failed to decrypt %s\n", name));
449                                 continue;
450                         }
451                         if (!dn_printed) {
452                                 object_id++;
453                                 DEBUG(0,("DN[%u] %s\n", object_id, dn));
454                                 dn_printed = true;
455                         }
456                         DEBUGADD(0,("ATTR: %s enc.length=%lu plain.length=%lu\n",
457                                     name, (long)enc_data->length, (long)plain_data.length));
458                         if (plain_data.length) {
459                                 enum ndr_err_code ndr_err;
460                                 dump_data(0, plain_data.data, plain_data.length);
461                                 if (save_values_dir) {
462                                         char *fname;
463                                         fname = talloc_asprintf(ctx, "%s/%s%02d",
464                                                                 save_values_dir,
465                                                                 name, object_id);
466                                         if (fname) {
467                                                 bool ok;
468                                                 ok = file_save(fname, plain_data.data, plain_data.length);
469                                                 if (!ok) {
470                                                         DEBUGADD(0,("Failed to save '%s'\n", fname));
471                                                 }
472                                         }
473                                         talloc_free(fname);
474                                 }
475
476                                 if (pull_fn) {
477                                         /* Can't use '_all' because of PIDL bugs with relative pointers */
478                                         ndr_err = ndr_pull_struct_blob(&plain_data, ptr,
479                                                                        lp_iconv_convenience(tctx->lp_ctx), ptr,
480                                                                        pull_fn);
481                                         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
482                                                 ndr_print_debug(print_fn, name, ptr);
483                                         } else {
484                                                 DEBUG(0, ("Failed to decode %s\n", name));
485                                         }
486                                 }
487                         } else {
488                                 dump_data(0, enc_data->data, enc_data->length);
489                         }
490                         talloc_free(ptr);
491                 }
492         }
493 }
494
495 /**
496  * Fetch LDAP attribute name and DN by supplied OID
497  */
498 static bool _drs_ldap_attr_by_oid(struct torture_context *tctx,
499                                         struct DsSyncTest *ctx,
500                                         const char *oid,
501                                         const char **attr_dn,
502                                         const char **attr_name)
503 {
504         NTSTATUS status;
505         const char *config_dn;
506         const char *expression;
507         struct ldap_message **res_msg;
508         struct ldap_SearchResEntry *search_res;
509         TALLOC_CTX *tmp_ctx = NULL;
510         const char *search_attrs[] = {"lDAPDisplayName", NULL};
511
512         tmp_ctx = talloc_new(ctx);
513
514         config_dn = talloc_asprintf(tmp_ctx, "CN=Schema,CN=Configuration,%s", ctx->domain_dn);
515         expression = talloc_asprintf(tmp_ctx, "(attributeID=%s)", oid);
516
517         status = ildap_search(ctx->admin.ldap.conn,
518                                 config_dn, LDAP_SEARCH_SCOPE_SUB,
519                                 expression, search_attrs, false,
520                                 NULL, NULL, &res_msg);
521         torture_assert_ntstatus_ok(tctx, status, "LDAP search request failed");
522         torture_assert(tctx,
523                         ildap_count_entries(ctx->admin.ldap.conn, res_msg) == 1,
524                         talloc_asprintf(tmp_ctx, "Failed to find attribute with OID=%s", oid));
525
526         search_res = &res_msg[0]->r.SearchResultEntry;
527         torture_assert(tctx, search_res->num_attributes > 0, "No attributes returned!")
528         torture_assert(tctx, strequal(search_attrs[0], search_res->attributes[0].name),
529                         "Requested attributes for attribute class not returned");
530
531         if (attr_dn) {
532                 *attr_dn = search_res->dn;
533         }
534
535         if (attr_name) {
536                 *attr_name = (const char *)search_res->attributes[0].values[0].data;
537         }
538
539         talloc_free(tmp_ctx);
540
541         return true;
542 }
543
544 /**
545  * Make Attribute OID and verify such Attribute exists in schema
546  */
547 static bool _drs_util_verify_attids(struct torture_context *tctx,
548                                     struct DsSyncTest *ctx,
549                                     struct drsuapi_DsReplicaOIDMapping_Ctr *prefix_map,
550                                     struct drsuapi_DsReplicaObjectListItemEx *cur)
551 {
552         uint32_t i;
553
554         DEBUG(1,("drs_test_verify_attids:\n"));
555
556         for (; cur; cur = cur->next_object) {
557                 const char *attr_dn = NULL;
558                 const char *attr_name = NULL;
559                 struct drsuapi_DsReplicaObject *obj = &cur->object;
560
561                 DEBUG(1,("%3s %-10s: %s\n", "", "object_dn", obj->identifier->dn));
562
563                 for (i = 0; i < obj->attribute_ctr.num_attributes; i++) {
564                         int map_idx;
565                         const char *oid = NULL;
566                         struct drsuapi_DsReplicaAttribute *attr;
567
568                         attr = &obj->attribute_ctr.attributes[i];
569                         if (!drs_util_oid_from_attid(tctx, prefix_map, attr->attid, &oid, &map_idx)) {
570                                 return false;
571                         }
572
573                         if (!_drs_ldap_attr_by_oid(tctx, ctx, oid, &attr_dn, &attr_name)) {
574                                 return false;
575                         }
576
577                         DEBUG(1,("%7s attr[%2d]: %-22s {map_idx=%2d; attid=0x%06x; ldap_name=%-26s; idl_name=%s}\n", "",
578                                         i, oid, map_idx, attr->attid, attr_name,
579                                         drs_util_DsAttributeId_to_string(attr->attid)));
580                 }
581         }
582
583         return true;
584 }
585
586
587 static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
588 {
589         NTSTATUS status;
590         bool ret = true;
591         int i, y = 0;
592         uint64_t highest_usn = 0;
593         const char *partition = NULL;
594         struct drsuapi_DsGetNCChanges r;
595         union drsuapi_DsGetNCChangesRequest req;
596         struct drsuapi_DsReplicaObjectIdentifier nc;
597         struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
598         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
599         int32_t out_level = 0;
600         struct GUID null_guid;
601         struct dom_sid null_sid;
602         DATA_BLOB gensec_skey;
603         struct {
604                 int32_t level;
605         } array[] = {
606 /*              {
607                         5
608                 },
609 */              {
610                         8
611                 }
612         };
613
614         ZERO_STRUCT(null_guid);
615         ZERO_STRUCT(null_sid);
616
617         partition = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "partition");
618         if (partition == NULL) {
619                 partition = ctx->domain_dn;
620                 printf("dssync:partition not specified, defaulting to %s.\n", ctx->domain_dn);
621         }
622
623         highest_usn = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "highest_usn", 0);
624
625         array[0].level = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "get_nc_changes_level", array[0].level);
626
627         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
628                 const struct samr_Password *nthash;
629                 nthash = cli_credentials_get_nt_hash(ctx->new_dc.credentials, ctx);
630                 if (nthash) {
631                         dump_data_pw("CREDENTIALS nthash:", nthash->hash, sizeof(nthash->hash));
632                 }
633         }
634         status = gensec_session_key(ctx->new_dc.drsuapi.pipe->conn->security_state.generic_state,
635                                     &gensec_skey);
636         if (!NT_STATUS_IS_OK(status)) {
637                 printf("failed to get gensec session key: %s\n", nt_errstr(status));
638                 return false;
639         }
640
641         for (i=0; i < ARRAY_SIZE(array); i++) {
642                 printf("testing DsGetNCChanges level %d\n",
643                         array[i].level);
644
645                 r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
646                 r.in.level              = array[i].level;
647
648                 switch (r.in.level) {
649                 case 5:
650                         nc.guid = null_guid;
651                         nc.sid  = null_sid;
652                         nc.dn   = partition; 
653
654                         r.in.req                                        = &req;
655                         r.in.req->req5.destination_dsa_guid             = ctx->new_dc.invocation_id;
656                         r.in.req->req5.source_dsa_invocation_id         = null_guid;
657                         r.in.req->req5.naming_context                   = &nc;
658                         r.in.req->req5.highwatermark.tmp_highest_usn    = highest_usn;
659                         r.in.req->req5.highwatermark.reserved_usn       = 0;
660                         r.in.req->req5.highwatermark.highest_usn        = highest_usn;
661                         r.in.req->req5.uptodateness_vector              = NULL;
662                         r.in.req->req5.replica_flags                    = 0;
663                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
664                                 r.in.req->req5.replica_flags            |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
665                         }
666                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
667                                 r.in.req->req5.replica_flags            |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
668                         }
669                         r.in.req->req5.replica_flags                    |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
670                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
671                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
672                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
673                                                                         ;
674                         r.in.req->req5.max_object_count                 = 133;
675                         r.in.req->req5.max_ndr_size                     = 1336770;
676                         r.in.req->req5.extended_op                      = DRSUAPI_EXOP_NONE;
677                         r.in.req->req5.fsmo_info                        = 0;
678
679                         break;
680                 case 8:
681                         nc.guid = null_guid;
682                         nc.sid  = null_sid;
683                         nc.dn   = partition; 
684                         /* nc.dn can be set to any other ad partition */
685
686                         r.in.req                                        = &req;
687                         r.in.req->req8.destination_dsa_guid             = ctx->new_dc.invocation_id;
688                         r.in.req->req8.source_dsa_invocation_id         = null_guid;
689                         r.in.req->req8.naming_context                   = &nc;
690                         r.in.req->req8.highwatermark.tmp_highest_usn    = highest_usn;
691                         r.in.req->req8.highwatermark.reserved_usn       = 0;
692                         r.in.req->req8.highwatermark.highest_usn        = highest_usn;
693                         r.in.req->req8.uptodateness_vector              = NULL;
694                         r.in.req->req8.replica_flags                    = 0;
695                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
696                                 r.in.req->req8.replica_flags            |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
697                         }
698                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
699                                 r.in.req->req8.replica_flags            |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
700                         }
701                         r.in.req->req8.replica_flags                    |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
702                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
703                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
704                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
705                                                                         ;
706                         r.in.req->req8.max_object_count                 = 402;
707                         r.in.req->req8.max_ndr_size                     = 402116;
708
709                         r.in.req->req8.extended_op                      = DRSUAPI_EXOP_NONE;
710                         r.in.req->req8.fsmo_info                        = 0;
711                         r.in.req->req8.partial_attribute_set            = NULL;
712                         r.in.req->req8.partial_attribute_set_ex         = NULL;
713                         r.in.req->req8.mapping_ctr.num_mappings         = 0;
714                         r.in.req->req8.mapping_ctr.mappings             = NULL;
715
716                         break;
717                 }
718                 
719                 printf("Dumping AD partition: %s\n", nc.dn);
720                 for (y=0; ;y++) {
721                         int32_t _level = 0;
722                         union drsuapi_DsGetNCChangesCtr ctr;
723
724                         ZERO_STRUCT(r.out);
725
726                         r.out.level_out = &_level;
727                         r.out.ctr       = &ctr;
728
729                         if (r.in.level == 5) {
730                                 DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
731                                         (long long)r.in.req->req5.highwatermark.tmp_highest_usn,
732                                         (long long)r.in.req->req5.highwatermark.highest_usn));
733                         }
734
735                         if (r.in.level == 8) {
736                                 DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
737                                         (long long)r.in.req->req8.highwatermark.tmp_highest_usn,
738                                         (long long)r.in.req->req8.highwatermark.highest_usn));
739                         }
740
741                         status = dcerpc_drsuapi_DsGetNCChanges(ctx->new_dc.drsuapi.pipe, ctx, &r);
742                         if (!NT_STATUS_IS_OK(status)) {
743                                 const char *errstr = nt_errstr(status);
744                                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
745                                         errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
746                                 }
747                                 printf("dcerpc_drsuapi_DsGetNCChanges failed - %s\n", errstr);
748                                 ret = false;
749                         } else if (!W_ERROR_IS_OK(r.out.result)) {
750                                 printf("DsGetNCChanges failed - %s\n", win_errstr(r.out.result));
751                                 ret = false;
752                         }
753
754                         if (ret == true && *r.out.level_out == 1) {
755                                 out_level = 1;
756                                 ctr1 = &r.out.ctr->ctr1;
757                         } else if (ret == true && *r.out.level_out == 2 &&
758                                    r.out.ctr->ctr2.mszip1.ts) {
759                                 out_level = 1;
760                                 ctr1 = &r.out.ctr->ctr2.mszip1.ts->ctr1;
761                         }
762
763                         if (out_level == 1) {
764                                 DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
765                                         (long long)ctr1->new_highwatermark.tmp_highest_usn,
766                                         (long long)ctr1->new_highwatermark.highest_usn));
767
768                                 _drs_util_verify_attids(tctx, ctx, &ctr1->mapping_ctr, ctr1->first_object);
769
770                                 test_analyse_objects(tctx, ctx, &gensec_skey, ctr1->first_object);
771
772                                 if (ctr1->more_data) {
773                                         r.in.req->req5.highwatermark = ctr1->new_highwatermark;
774                                         continue;
775                                 }
776                         }
777
778                         if (ret == true && *r.out.level_out == 6) {
779                                 out_level = 6;
780                                 ctr6 = &r.out.ctr->ctr6;
781                         } else if (ret == true && *r.out.level_out == 7
782                                    && r.out.ctr->ctr7.level == 6
783                                    && r.out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP
784                                    && r.out.ctr->ctr7.ctr.mszip6.ts) {
785                                 out_level = 6;
786                                 ctr6 = &r.out.ctr->ctr7.ctr.mszip6.ts->ctr6;
787                         } else if (ret == true && *r.out.level_out == 7
788                                    && r.out.ctr->ctr7.level == 6
789                                    && r.out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS
790                                    && r.out.ctr->ctr7.ctr.xpress6.ts) {
791                                 out_level = 6;
792                                 ctr6 = &r.out.ctr->ctr7.ctr.xpress6.ts->ctr6;
793                         }
794
795                         if (out_level == 6) {
796                                 DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
797                                         (long long)ctr6->new_highwatermark.tmp_highest_usn,
798                                         (long long)ctr6->new_highwatermark.highest_usn));
799
800                                 _drs_util_verify_attids(tctx, ctx, &ctr6->mapping_ctr, ctr6->first_object);
801
802                                 test_analyse_objects(tctx, ctx, &gensec_skey, ctr6->first_object);
803
804                                 if (ctr6->more_data) {
805                                         r.in.req->req8.highwatermark = ctr6->new_highwatermark;
806                                         continue;
807                                 }
808                         }
809
810                         break;
811                 }
812         }
813
814         return ret;
815 }
816
817 static bool test_FetchNT4Data(struct torture_context *tctx, 
818                               struct DsSyncTest *ctx)
819 {
820         NTSTATUS status;
821         bool ret = true;
822         struct drsuapi_DsGetNT4ChangeLog r;
823         union drsuapi_DsGetNT4ChangeLogRequest req;
824         union drsuapi_DsGetNT4ChangeLogInfo info;
825         uint32_t level_out = 0;
826         struct GUID null_guid;
827         struct dom_sid null_sid;
828         DATA_BLOB cookie;
829
830         ZERO_STRUCT(null_guid);
831         ZERO_STRUCT(null_sid);
832         ZERO_STRUCT(cookie);
833
834         ZERO_STRUCT(r);
835         r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
836         r.in.level              = 1;
837         r.out.info              = &info;
838         r.out.level_out         = &level_out;
839
840         req.req1.unknown1       = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "nt4-1", 3);
841         req.req1.unknown2       = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "nt4-2", 0x00004000);
842
843         while (1) {
844                 req.req1.length = cookie.length;
845                 req.req1.data   = cookie.data;
846
847                 r.in.req = &req;
848
849                 status = dcerpc_drsuapi_DsGetNT4ChangeLog(ctx->new_dc.drsuapi.pipe, ctx, &r);
850                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
851                         printf("DsGetNT4ChangeLog not supported by target server\n");
852                         break;
853                 } else if (!NT_STATUS_IS_OK(status)) {
854                         const char *errstr = nt_errstr(status);
855                         if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
856                                 errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
857                         }
858                         printf("dcerpc_drsuapi_DsGetNT4ChangeLog failed - %s\n", errstr);
859                         ret = false;
860                 } else if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_DOMAIN_ROLE)) {
861                         printf("DsGetNT4ChangeLog not supported by target server\n");
862                         break;
863                 } else if (!W_ERROR_IS_OK(r.out.result)) {
864                         printf("DsGetNT4ChangeLog failed - %s\n", win_errstr(r.out.result));
865                         ret = false;
866                 } else if (*r.out.level_out != 1) {
867                         printf("DsGetNT4ChangeLog unknown level - %u\n", *r.out.level_out);
868                         ret = false;
869                 } else if (NT_STATUS_IS_OK(r.out.info->info1.status)) {
870                 } else if (NT_STATUS_EQUAL(r.out.info->info1.status, STATUS_MORE_ENTRIES)) {
871                         cookie.length   = r.out.info->info1.length1;
872                         cookie.data     = r.out.info->info1.data1;
873                         continue;
874                 } else {
875                         printf("DsGetNT4ChangeLog failed - %s\n", nt_errstr(r.out.info->info1.status));
876                         ret = false;
877                 }
878
879                 break;
880         }
881
882         return ret;
883 }
884
885 bool torture_rpc_dssync(struct torture_context *torture)
886 {
887         bool ret = true;
888         TALLOC_CTX *mem_ctx;
889         struct DsSyncTest *ctx;
890         
891         mem_ctx = talloc_init("torture_rpc_dssync");
892         ctx = test_create_context(torture);
893         
894         ret &= _test_DsBind(torture, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
895         if (!ret) {
896                 return ret;
897         }
898         ret &= test_LDAPBind(torture, ctx, ctx->admin.credentials, &ctx->admin.ldap);
899         if (!ret) {
900                 return ret;
901         }
902         ret &= test_GetInfo(torture, ctx);
903         ret &= _test_DsBind(torture, ctx, ctx->new_dc.credentials, &ctx->new_dc.drsuapi);
904         if (!ret) {
905                 return ret;
906         }
907         ret &= test_FetchData(torture, ctx);
908         ret &= test_FetchNT4Data(torture, ctx);
909
910         return ret;
911 }