s4/drs(tort): _drs_ldap_attr_by_oid() implementation
[amitay/samba.git] / source4 / torture / rpc / dssync.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    DsGetNCChanges replication test
5
6    Copyright (C) Stefan (metze) Metzmacher 2005
7    Copyright (C) Brad Henry 2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
26 #include "librpc/gen_ndr/ndr_drsblobs.h"
27 #include "libcli/cldap/cldap.h"
28 #include "libcli/ldap/ldap_client.h"
29 #include "torture/torture.h"
30 #include "torture/ldap/proto.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "../lib/crypto/crypto.h"
33 #include "../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
40 struct DsSyncBindInfo {
41         struct dcerpc_pipe *pipe;
42         struct drsuapi_DsBind req;
43         struct GUID bind_guid;
44         struct drsuapi_DsBindInfoCtr our_bind_info_ctr;
45         struct drsuapi_DsBindInfo28 our_bind_info28;
46         struct drsuapi_DsBindInfo28 peer_bind_info28;
47         struct policy_handle bind_handle;
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(struct torture_context *tctx)
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 = torture_setting_string(tctx, "binding", NULL);
91         ctx = talloc_zero(tctx, 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->pid                    = 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(tctx->lp_ctx, NULL, "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->pid                    = 0;
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 torture_context *tctx,
180                          struct DsSyncTest *ctx, struct cli_credentials *credentials, struct DsSyncBindInfo *b)
181 {
182         NTSTATUS status;
183         bool ret = true;
184
185         status = dcerpc_pipe_connect_b(ctx,
186                                        &b->pipe, ctx->drsuapi_binding, 
187                                        &ndr_table_drsuapi,
188                                        credentials, tctx->ev, tctx->lp_ctx);
189         
190         if (!NT_STATUS_IS_OK(status)) {
191                 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
192                 return false;
193         }
194
195         status = dcerpc_drsuapi_DsBind(b->pipe, ctx, &b->req);
196         if (!NT_STATUS_IS_OK(status)) {
197                 const char *errstr = nt_errstr(status);
198                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
199                         errstr = dcerpc_errstr(ctx, b->pipe->last_fault_code);
200                 }
201                 printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
202                 ret = false;
203         } else if (!W_ERROR_IS_OK(b->req.out.result)) {
204                 printf("DsBind failed - %s\n", win_errstr(b->req.out.result));
205                 ret = false;
206         }
207
208         ZERO_STRUCT(b->peer_bind_info28);
209         if (b->req.out.bind_info) {
210                 switch (b->req.out.bind_info->length) {
211                 case 24: {
212                         struct drsuapi_DsBindInfo24 *info24;
213                         info24 = &b->req.out.bind_info->info.info24;
214                         b->peer_bind_info28.supported_extensions= info24->supported_extensions;
215                         b->peer_bind_info28.site_guid           = info24->site_guid;
216                         b->peer_bind_info28.pid                 = info24->pid;
217                         b->peer_bind_info28.repl_epoch          = 0;
218                         break;
219                 }
220                 case 48: {
221                         struct drsuapi_DsBindInfo48 *info48;
222                         info48 = &b->req.out.bind_info->info.info48;
223                         b->peer_bind_info28.supported_extensions= info48->supported_extensions;
224                         b->peer_bind_info28.site_guid           = info48->site_guid;
225                         b->peer_bind_info28.pid                 = info48->pid;
226                         b->peer_bind_info28.repl_epoch          = info48->repl_epoch;
227                         break;
228                 }
229                 case 28:
230                         b->peer_bind_info28 = b->req.out.bind_info->info.info28;
231                         break;
232                 default:
233                         printf("DsBind - warning: unknown BindInfo length: %u\n",
234                                b->req.out.bind_info->length);
235                 }
236         }
237
238         return ret;
239 }
240
241 static bool test_LDAPBind(struct torture_context *tctx, struct DsSyncTest *ctx, 
242                           struct cli_credentials *credentials, struct DsSyncLDAPInfo *l)
243 {
244         NTSTATUS status;
245         bool ret = true;
246
247         status = torture_ldap_connection(tctx, &l->conn, ctx->ldap_url);
248         if (!NT_STATUS_IS_OK(status)) {
249                 printf("failed to connect to LDAP: %s\n", ctx->ldap_url);
250                 return false;
251         }
252
253         printf("connected to LDAP: %s\n", ctx->ldap_url);
254
255         status = torture_ldap_bind_sasl(l->conn, credentials, tctx->lp_ctx);
256         if (!NT_STATUS_IS_OK(status)) {
257                 printf("failed to bind to LDAP:\n");
258                 return false;
259         }
260         printf("bound to LDAP.\n");
261
262         return ret;
263 }
264
265 static bool test_GetInfo(struct torture_context *tctx, struct DsSyncTest *ctx)
266 {
267         NTSTATUS status;
268         struct drsuapi_DsCrackNames r;
269         union drsuapi_DsNameRequest req;
270         union drsuapi_DsNameCtr ctr;
271         int32_t level_out = 0;
272         struct drsuapi_DsNameString names[1];
273         bool ret = true;
274         struct cldap_socket *cldap;
275         struct cldap_netlogon search;
276
277         status = cldap_socket_init(ctx, NULL, NULL, NULL, &cldap);
278         if (!NT_STATUS_IS_OK(status)) {
279                 printf("failed to setup cldap socket - %s\n",
280                         nt_errstr(status));
281                 return false;
282         }
283
284         r.in.bind_handle                = &ctx->admin.drsuapi.bind_handle;
285         r.in.level                      = 1;
286         r.in.req                        = &req;
287         r.in.req->req1.codepage         = 1252; /* western european */
288         r.in.req->req1.language         = 0x00000407; /* german */
289         r.in.req->req1.count            = 1;
290         r.in.req->req1.names            = names;
291         r.in.req->req1.format_flags     = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
292         r.in.req->req1.format_offered   = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
293         r.in.req->req1.format_desired   = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
294         names[0].str = talloc_asprintf(ctx, "%s\\", lp_workgroup(tctx->lp_ctx));
295
296         r.out.level_out                 = &level_out;
297         r.out.ctr                       = &ctr;
298
299         status = dcerpc_drsuapi_DsCrackNames(ctx->admin.drsuapi.pipe, ctx, &r);
300         if (!NT_STATUS_IS_OK(status)) {
301                 const char *errstr = nt_errstr(status);
302                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
303                         errstr = dcerpc_errstr(ctx, ctx->admin.drsuapi.pipe->last_fault_code);
304                 }
305                 printf("dcerpc_drsuapi_DsCrackNames failed - %s\n", errstr);
306                 return false;
307         } else if (!W_ERROR_IS_OK(r.out.result)) {
308                 printf("DsCrackNames failed - %s\n", win_errstr(r.out.result));
309                 return false;
310         }
311
312         ctx->domain_dn = r.out.ctr->ctr1->array[0].result_name;
313         
314         ZERO_STRUCT(search);
315         search.in.dest_address = ctx->drsuapi_binding->host;
316         search.in.dest_port = lp_cldap_port(tctx->lp_ctx);
317         search.in.acct_control = -1;
318         search.in.version               = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
319         search.in.map_response = true;
320         status = cldap_netlogon(cldap, lp_iconv_convenience(tctx->lp_ctx), ctx, &search);
321         if (!NT_STATUS_IS_OK(status)) {
322                 const char *errstr = nt_errstr(status);
323                 ctx->site_name = talloc_asprintf(ctx, "%s", "Default-First-Site-Name");
324                 printf("cldap_netlogon() returned %s. Defaulting to Site-Name: %s\n", errstr, ctx->site_name);          
325         } else {
326                 ctx->site_name = talloc_steal(ctx, search.out.netlogon.data.nt5_ex.client_site);
327                 printf("cldap_netlogon() returned Client Site-Name: %s.\n",ctx->site_name);
328                 printf("cldap_netlogon() returned Server Site-Name: %s.\n",search.out.netlogon.data.nt5_ex.server_site);
329         }
330
331         if (!ctx->domain_dn) {
332                 struct ldb_context *ldb = ldb_init(ctx, tctx->ev);
333                 struct ldb_dn *dn = samdb_dns_domain_to_dn(ldb, ctx, search.out.netlogon.data.nt5_ex.dns_domain);
334                 ctx->domain_dn = ldb_dn_alloc_linearized(ctx, dn);
335                 talloc_free(dn);
336                 talloc_free(ldb);
337         }
338
339         return ret;
340 }
341
342 static void test_analyse_objects(struct torture_context *tctx, 
343                                  struct DsSyncTest *ctx,
344                                  const DATA_BLOB *gensec_skey,
345                                  struct drsuapi_DsReplicaObjectListItemEx *cur)
346 {
347         static uint32_t object_id;
348         const char *save_values_dir;
349
350         if (!lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
351                 return; 
352         }
353
354         save_values_dir = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "save_pwd_blobs_dir");
355
356         for (; cur; cur = cur->next_object) {
357                 const char *dn;
358                 struct dom_sid *sid = NULL;
359                 uint32_t rid = 0;
360                 bool dn_printed = false;
361                 uint32_t i;
362
363                 if (!cur->object.identifier) continue;
364
365                 dn = cur->object.identifier->dn;
366                 if (cur->object.identifier->sid.num_auths > 0) {
367                         sid = &cur->object.identifier->sid;
368                         rid = sid->sub_auths[sid->num_auths - 1];
369                 }
370
371                 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
372                         WERROR werr;
373                         const char *name = NULL;
374                         bool rcrypt = false;
375                         DATA_BLOB *enc_data = NULL;
376                         DATA_BLOB plain_data;
377                         struct drsuapi_DsReplicaAttribute *attr;
378                         ndr_pull_flags_fn_t pull_fn = NULL;
379                         ndr_print_fn_t print_fn = NULL;
380                         void *ptr = NULL;
381                         attr = &cur->object.attribute_ctr.attributes[i];
382
383                         switch (attr->attid) {
384                         case DRSUAPI_ATTRIBUTE_dBCSPwd:
385                                 name    = "dBCSPwd";
386                                 rcrypt  = true;
387                                 break;
388                         case DRSUAPI_ATTRIBUTE_unicodePwd:
389                                 name    = "unicodePwd";
390                                 rcrypt  = true;
391                                 break;
392                         case DRSUAPI_ATTRIBUTE_ntPwdHistory:
393                                 name    = "ntPwdHistory";
394                                 rcrypt  = true;
395                                 break;
396                         case DRSUAPI_ATTRIBUTE_lmPwdHistory:
397                                 name    = "lmPwdHistory";
398                                 rcrypt  = true;
399                                 break;
400                         case DRSUAPI_ATTRIBUTE_supplementalCredentials:
401                                 name    = "supplementalCredentials";
402                                 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob;
403                                 print_fn = (ndr_print_fn_t)ndr_print_supplementalCredentialsBlob;
404                                 ptr = talloc(ctx, struct supplementalCredentialsBlob);
405                                 break;
406                         case DRSUAPI_ATTRIBUTE_priorValue:
407                                 name    = "priorValue";
408                                 break;
409                         case DRSUAPI_ATTRIBUTE_currentValue:
410                                 name    = "currentValue";
411                                 break;
412                         case DRSUAPI_ATTRIBUTE_trustAuthOutgoing:
413                                 name    = "trustAuthOutgoing";
414                                 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob;
415                                 print_fn = (ndr_print_fn_t)ndr_print_trustAuthInOutBlob;
416                                 ptr = talloc(ctx, struct trustAuthInOutBlob);
417                                 break;
418                         case DRSUAPI_ATTRIBUTE_trustAuthIncoming:
419                                 name    = "trustAuthIncoming";
420                                 pull_fn = (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob;
421                                 print_fn = (ndr_print_fn_t)ndr_print_trustAuthInOutBlob;
422                                 ptr = talloc(ctx, struct trustAuthInOutBlob);
423                                 break;
424                         case DRSUAPI_ATTRIBUTE_initialAuthOutgoing:
425                                 name    = "initialAuthOutgoing";
426                                 break;
427                         case DRSUAPI_ATTRIBUTE_initialAuthIncoming:
428                                 name    = "initialAuthIncoming";
429                                 break;
430                         default:
431                                 continue;
432                         }
433
434                         if (attr->value_ctr.num_values != 1) continue;
435
436                         if (!attr->value_ctr.values[0].blob) continue;
437
438                         enc_data = attr->value_ctr.values[0].blob;
439                         ZERO_STRUCT(plain_data);
440
441                         werr = drsuapi_decrypt_attribute_value(ctx, gensec_skey, rcrypt,
442                                                                rid,
443                                                                enc_data, &plain_data);
444                         if (!W_ERROR_IS_OK(werr)) {
445                                 DEBUG(0, ("Failed to decrypt %s\n", name));
446                                 continue;
447                         }
448                         if (!dn_printed) {
449                                 object_id++;
450                                 DEBUG(0,("DN[%u] %s\n", object_id, dn));
451                                 dn_printed = true;
452                         }
453                         DEBUGADD(0,("ATTR: %s enc.length=%lu plain.length=%lu\n",
454                                     name, (long)enc_data->length, (long)plain_data.length));
455                         if (plain_data.length) {
456                                 enum ndr_err_code ndr_err;
457                                 dump_data(0, plain_data.data, plain_data.length);
458                                 if (save_values_dir) {
459                                         char *fname;
460                                         fname = talloc_asprintf(ctx, "%s/%s%02d",
461                                                                 save_values_dir,
462                                                                 name, object_id);
463                                         if (fname) {
464                                                 bool ok;
465                                                 ok = file_save(fname, plain_data.data, plain_data.length);
466                                                 if (!ok) {
467                                                         DEBUGADD(0,("Failed to save '%s'\n", fname));
468                                                 }
469                                         }
470                                         talloc_free(fname);
471                                 }
472
473                                 if (pull_fn) {
474                                         /* Can't use '_all' because of PIDL bugs with relative pointers */
475                                         ndr_err = ndr_pull_struct_blob(&plain_data, ptr,
476                                                                        lp_iconv_convenience(tctx->lp_ctx), ptr,
477                                                                        pull_fn);
478                                         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
479                                                 ndr_print_debug(print_fn, name, ptr);
480                                         } else {
481                                                 DEBUG(0, ("Failed to decode %s\n", name));
482                                         }
483                                 }
484                         } else {
485                                 dump_data(0, enc_data->data, enc_data->length);
486                         }
487                         talloc_free(ptr);
488                 }
489         }
490 }
491
492 /**
493  * Fetch LDAP attribute name and DN by supplied OID
494  */
495 static bool _drs_ldap_attr_by_oid(struct torture_context *tctx,
496                                         struct DsSyncTest *ctx,
497                                         const char *oid,
498                                         const char **attr_dn,
499                                         const char **attr_name)
500 {
501         NTSTATUS status;
502         const char *config_dn;
503         const char *expression;
504         struct ldap_message **res_msg;
505         struct ldap_SearchResEntry *search_res;
506         TALLOC_CTX *tmp_ctx = NULL;
507         const char *search_attrs[] = {"lDAPDisplayName", NULL};
508
509         tmp_ctx = talloc_new(ctx);
510
511         config_dn = talloc_asprintf(tmp_ctx, "CN=Schema,CN=Configuration,%s", ctx->domain_dn);
512         expression = talloc_asprintf(tmp_ctx, "(attributeID=%s)", oid);
513
514         status = ildap_search(ctx->admin.ldap.conn,
515                                 config_dn, LDAP_SEARCH_SCOPE_SUB,
516                                 expression, search_attrs, false,
517                                 NULL, NULL, &res_msg);
518         torture_assert_ntstatus_ok(tctx, status, "LDAP search request failed");
519         torture_assert(tctx,
520                         ildap_count_entries(ctx->admin.ldap.conn, res_msg) == 1,
521                         talloc_asprintf(tmp_ctx, "Failed to find attribute with OID=%s", oid));
522
523         search_res = &res_msg[0]->r.SearchResultEntry;
524         torture_assert(tctx, search_res->num_attributes > 0, "No attributes returned!")
525         torture_assert(tctx, strequal(search_attrs[0], search_res->attributes[0].name),
526                         "Requested attributes for attribute class not returned");
527
528         if (attr_dn) {
529                 *attr_dn = search_res->dn;
530         }
531
532         if (attr_name) {
533                 *attr_name = (const char *)search_res->attributes[0].values[0].data;
534         }
535
536         talloc_free(tmp_ctx);
537
538         return true;
539 }
540
541 static bool test_FetchData(struct torture_context *tctx, struct DsSyncTest *ctx)
542 {
543         NTSTATUS status;
544         bool ret = true;
545         int i, y = 0;
546         uint64_t highest_usn = 0;
547         const char *partition = NULL;
548         struct drsuapi_DsGetNCChanges r;
549         union drsuapi_DsGetNCChangesRequest req;
550         struct drsuapi_DsReplicaObjectIdentifier nc;
551         struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
552         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
553         int32_t out_level = 0;
554         struct GUID null_guid;
555         struct dom_sid null_sid;
556         DATA_BLOB gensec_skey;
557         struct {
558                 int32_t level;
559         } array[] = {
560 /*              {
561                         5
562                 },
563 */              {
564                         8
565                 }
566         };
567
568         ZERO_STRUCT(null_guid);
569         ZERO_STRUCT(null_sid);
570
571         partition = lp_parm_string(tctx->lp_ctx, NULL, "dssync", "partition");
572         if (partition == NULL) {
573                 partition = ctx->domain_dn;
574                 printf("dssync:partition not specified, defaulting to %s.\n", ctx->domain_dn);
575         }
576
577         highest_usn = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "highest_usn", 0);
578
579         array[0].level = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "get_nc_changes_level", array[0].level);
580
581         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "print_pwd_blobs", false)) {
582                 const struct samr_Password *nthash;
583                 nthash = cli_credentials_get_nt_hash(ctx->new_dc.credentials, ctx);
584                 if (nthash) {
585                         dump_data_pw("CREDENTIALS nthash:", nthash->hash, sizeof(nthash->hash));
586                 }
587         }
588         status = gensec_session_key(ctx->new_dc.drsuapi.pipe->conn->security_state.generic_state,
589                                     &gensec_skey);
590         if (!NT_STATUS_IS_OK(status)) {
591                 printf("failed to get gensec session key: %s\n", nt_errstr(status));
592                 return false;
593         }
594
595         for (i=0; i < ARRAY_SIZE(array); i++) {
596                 printf("testing DsGetNCChanges level %d\n",
597                         array[i].level);
598
599                 r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
600                 r.in.level              = array[i].level;
601
602                 switch (r.in.level) {
603                 case 5:
604                         nc.guid = null_guid;
605                         nc.sid  = null_sid;
606                         nc.dn   = partition; 
607
608                         r.in.req                                        = &req;
609                         r.in.req->req5.destination_dsa_guid             = ctx->new_dc.invocation_id;
610                         r.in.req->req5.source_dsa_invocation_id         = null_guid;
611                         r.in.req->req5.naming_context                   = &nc;
612                         r.in.req->req5.highwatermark.tmp_highest_usn    = highest_usn;
613                         r.in.req->req5.highwatermark.reserved_usn       = 0;
614                         r.in.req->req5.highwatermark.highest_usn        = highest_usn;
615                         r.in.req->req5.uptodateness_vector              = NULL;
616                         r.in.req->req5.replica_flags                    = 0;
617                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
618                                 r.in.req->req5.replica_flags            |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
619                         }
620                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
621                                 r.in.req->req5.replica_flags            |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
622                         }
623                         r.in.req->req5.replica_flags                    |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
624                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
625                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
626                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
627                                                                         ;
628                         r.in.req->req5.max_object_count                 = 133;
629                         r.in.req->req5.max_ndr_size                     = 1336770;
630                         r.in.req->req5.extended_op                      = DRSUAPI_EXOP_NONE;
631                         r.in.req->req5.fsmo_info                        = 0;
632
633                         break;
634                 case 8:
635                         nc.guid = null_guid;
636                         nc.sid  = null_sid;
637                         nc.dn   = partition; 
638                         /* nc.dn can be set to any other ad partition */
639
640                         r.in.req                                        = &req;
641                         r.in.req->req8.destination_dsa_guid             = ctx->new_dc.invocation_id;
642                         r.in.req->req8.source_dsa_invocation_id         = null_guid;
643                         r.in.req->req8.naming_context                   = &nc;
644                         r.in.req->req8.highwatermark.tmp_highest_usn    = highest_usn;
645                         r.in.req->req8.highwatermark.reserved_usn       = 0;
646                         r.in.req->req8.highwatermark.highest_usn        = highest_usn;
647                         r.in.req->req8.uptodateness_vector              = NULL;
648                         r.in.req->req8.replica_flags                    = 0;
649                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "compression", false)) {
650                                 r.in.req->req8.replica_flags            |= DRSUAPI_DS_REPLICA_NEIGHBOUR_COMPRESS_CHANGES;
651                         }
652                         if (lp_parm_bool(tctx->lp_ctx, NULL, "dssync", "neighbour_writeable", true)) {
653                                 r.in.req->req8.replica_flags            |= DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE;
654                         }
655                         r.in.req->req8.replica_flags                    |= DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP
656                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS
657                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS
658                                                                         | DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED
659                                                                         ;
660                         r.in.req->req8.max_object_count                 = 402;
661                         r.in.req->req8.max_ndr_size                     = 402116;
662
663                         r.in.req->req8.extended_op                      = DRSUAPI_EXOP_NONE;
664                         r.in.req->req8.fsmo_info                        = 0;
665                         r.in.req->req8.partial_attribute_set            = NULL;
666                         r.in.req->req8.partial_attribute_set_ex         = NULL;
667                         r.in.req->req8.mapping_ctr.num_mappings         = 0;
668                         r.in.req->req8.mapping_ctr.mappings             = NULL;
669
670                         break;
671                 }
672                 
673                 printf("Dumping AD partition: %s\n", nc.dn);
674                 for (y=0; ;y++) {
675                         int32_t _level = 0;
676                         union drsuapi_DsGetNCChangesCtr ctr;
677
678                         ZERO_STRUCT(r.out);
679
680                         r.out.level_out = &_level;
681                         r.out.ctr       = &ctr;
682
683                         if (r.in.level == 5) {
684                                 DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
685                                         (long long)r.in.req->req5.highwatermark.tmp_highest_usn,
686                                         (long long)r.in.req->req5.highwatermark.highest_usn));
687                         }
688
689                         if (r.in.level == 8) {
690                                 DEBUG(0,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
691                                         (long long)r.in.req->req8.highwatermark.tmp_highest_usn,
692                                         (long long)r.in.req->req8.highwatermark.highest_usn));
693                         }
694
695                         status = dcerpc_drsuapi_DsGetNCChanges(ctx->new_dc.drsuapi.pipe, ctx, &r);
696                         if (!NT_STATUS_IS_OK(status)) {
697                                 const char *errstr = nt_errstr(status);
698                                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
699                                         errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
700                                 }
701                                 printf("dcerpc_drsuapi_DsGetNCChanges failed - %s\n", errstr);
702                                 ret = false;
703                         } else if (!W_ERROR_IS_OK(r.out.result)) {
704                                 printf("DsGetNCChanges failed - %s\n", win_errstr(r.out.result));
705                                 ret = false;
706                         }
707
708                         if (ret == true && *r.out.level_out == 1) {
709                                 out_level = 1;
710                                 ctr1 = &r.out.ctr->ctr1;
711                         } else if (ret == true && *r.out.level_out == 2 &&
712                                    r.out.ctr->ctr2.mszip1.ts) {
713                                 out_level = 1;
714                                 ctr1 = &r.out.ctr->ctr2.mszip1.ts->ctr1;
715                         }
716
717                         if (out_level == 1) {
718                                 DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
719                                         (long long)ctr1->new_highwatermark.tmp_highest_usn,
720                                         (long long)ctr1->new_highwatermark.highest_usn));
721
722                                 test_analyse_objects(tctx, ctx, &gensec_skey, ctr1->first_object);
723
724                                 if (ctr1->more_data) {
725                                         r.in.req->req5.highwatermark = ctr1->new_highwatermark;
726                                         continue;
727                                 }
728                         }
729
730                         if (ret == true && *r.out.level_out == 6) {
731                                 out_level = 6;
732                                 ctr6 = &r.out.ctr->ctr6;
733                         } else if (ret == true && *r.out.level_out == 7
734                                    && r.out.ctr->ctr7.level == 6
735                                    && r.out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP
736                                    && r.out.ctr->ctr7.ctr.mszip6.ts) {
737                                 out_level = 6;
738                                 ctr6 = &r.out.ctr->ctr7.ctr.mszip6.ts->ctr6;
739                         } else if (ret == true && *r.out.level_out == 7
740                                    && r.out.ctr->ctr7.level == 6
741                                    && r.out.ctr->ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS
742                                    && r.out.ctr->ctr7.ctr.xpress6.ts) {
743                                 out_level = 6;
744                                 ctr6 = &r.out.ctr->ctr7.ctr.xpress6.ts->ctr6;
745                         }
746
747                         if (out_level == 6) {
748                                 DEBUG(0,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
749                                         (long long)ctr6->new_highwatermark.tmp_highest_usn,
750                                         (long long)ctr6->new_highwatermark.highest_usn));
751
752                                 test_analyse_objects(tctx, ctx, &gensec_skey, ctr6->first_object);
753
754                                 if (ctr6->more_data) {
755                                         r.in.req->req8.highwatermark = ctr6->new_highwatermark;
756                                         continue;
757                                 }
758                         }
759
760                         break;
761                 }
762         }
763
764         return ret;
765 }
766
767 static bool test_FetchNT4Data(struct torture_context *tctx, 
768                               struct DsSyncTest *ctx)
769 {
770         NTSTATUS status;
771         bool ret = true;
772         struct drsuapi_DsGetNT4ChangeLog r;
773         union drsuapi_DsGetNT4ChangeLogRequest req;
774         union drsuapi_DsGetNT4ChangeLogInfo info;
775         uint32_t level_out = 0;
776         struct GUID null_guid;
777         struct dom_sid null_sid;
778         DATA_BLOB cookie;
779
780         ZERO_STRUCT(null_guid);
781         ZERO_STRUCT(null_sid);
782         ZERO_STRUCT(cookie);
783
784         ZERO_STRUCT(r);
785         r.in.bind_handle        = &ctx->new_dc.drsuapi.bind_handle;
786         r.in.level              = 1;
787         r.out.info              = &info;
788         r.out.level_out         = &level_out;
789
790         req.req1.unknown1       = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "nt4-1", 3);
791         req.req1.unknown2       = lp_parm_int(tctx->lp_ctx, NULL, "dssync", "nt4-2", 0x00004000);
792
793         while (1) {
794                 req.req1.length = cookie.length;
795                 req.req1.data   = cookie.data;
796
797                 r.in.req = &req;
798
799                 status = dcerpc_drsuapi_DsGetNT4ChangeLog(ctx->new_dc.drsuapi.pipe, ctx, &r);
800                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
801                         printf("DsGetNT4ChangeLog not supported by target server\n");
802                         break;
803                 } else if (!NT_STATUS_IS_OK(status)) {
804                         const char *errstr = nt_errstr(status);
805                         if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
806                                 errstr = dcerpc_errstr(ctx, ctx->new_dc.drsuapi.pipe->last_fault_code);
807                         }
808                         printf("dcerpc_drsuapi_DsGetNT4ChangeLog failed - %s\n", errstr);
809                         ret = false;
810                 } else if (W_ERROR_EQUAL(r.out.result, WERR_INVALID_DOMAIN_ROLE)) {
811                         printf("DsGetNT4ChangeLog not supported by target server\n");
812                         break;
813                 } else if (!W_ERROR_IS_OK(r.out.result)) {
814                         printf("DsGetNT4ChangeLog failed - %s\n", win_errstr(r.out.result));
815                         ret = false;
816                 } else if (*r.out.level_out != 1) {
817                         printf("DsGetNT4ChangeLog unknown level - %u\n", *r.out.level_out);
818                         ret = false;
819                 } else if (NT_STATUS_IS_OK(r.out.info->info1.status)) {
820                 } else if (NT_STATUS_EQUAL(r.out.info->info1.status, STATUS_MORE_ENTRIES)) {
821                         cookie.length   = r.out.info->info1.length1;
822                         cookie.data     = r.out.info->info1.data1;
823                         continue;
824                 } else {
825                         printf("DsGetNT4ChangeLog failed - %s\n", nt_errstr(r.out.info->info1.status));
826                         ret = false;
827                 }
828
829                 break;
830         }
831
832         return ret;
833 }
834
835 bool torture_rpc_dssync(struct torture_context *torture)
836 {
837         bool ret = true;
838         TALLOC_CTX *mem_ctx;
839         struct DsSyncTest *ctx;
840         
841         mem_ctx = talloc_init("torture_rpc_dssync");
842         ctx = test_create_context(torture);
843         
844         ret &= _test_DsBind(torture, ctx, ctx->admin.credentials, &ctx->admin.drsuapi);
845         if (!ret) {
846                 return ret;
847         }
848         ret &= test_LDAPBind(torture, ctx, ctx->admin.credentials, &ctx->admin.ldap);
849         if (!ret) {
850                 return ret;
851         }
852         ret &= test_GetInfo(torture, ctx);
853         ret &= _test_DsBind(torture, ctx, ctx->new_dc.credentials, &ctx->new_dc.drsuapi);
854         if (!ret) {
855                 return ret;
856         }
857         ret &= test_FetchData(torture, ctx);
858         ret &= test_FetchNT4Data(torture, ctx);
859
860         return ret;
861 }