dcb1c4b0f78ec8699b8bf6159b732a4a9e36565d
[gd/samba-autobuild/.git] / source4 / torture / rpc / samsync.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    test suite for netlogon rpc operations
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8    Copyright (C) Tim Potter      2003
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "../lib/util/dlinklist.h"
26 #include "../lib/crypto/crypto.h"
27 #include "system/time.h"
28 #include "torture/rpc/rpc.h"
29 #include "auth/gensec/gensec.h"
30 #include "libcli/auth/libcli_auth.h"
31 #include "libcli/samsync/samsync.h"
32 #include "libcli/security/security.h"
33 #include "librpc/gen_ndr/ndr_netlogon.h"
34 #include "librpc/gen_ndr/ndr_netlogon_c.h"
35 #include "librpc/gen_ndr/ndr_lsa_c.h"
36 #include "librpc/gen_ndr/ndr_samr_c.h"
37 #include "librpc/gen_ndr/ndr_security.h"
38 #include "param/param.h"
39
40 #define TEST_MACHINE_NAME "samsynctest"
41 #define TEST_WKSTA_MACHINE_NAME "samsynctest2"
42 #define TEST_USER_NAME "samsynctestuser"
43
44 /*
45   try a netlogon SamLogon
46 */
47 static NTSTATUS test_SamLogon(struct torture_context *tctx,
48                               struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
49                               struct netlogon_creds_CredentialState *creds,
50                               const char *domain, const char *account_name,
51                               const char *workstation,
52                               struct samr_Password *lm_hash,
53                               struct samr_Password *nt_hash,
54                               struct netr_SamInfo3 **info3)
55 {
56         NTSTATUS status;
57         struct netr_LogonSamLogon r;
58         struct netr_Authenticator auth, auth2;
59         struct netr_NetworkInfo ninfo;
60         union netr_LogonLevel logon;
61         union netr_Validation validation;
62         uint8_t authoritative;
63         struct dcerpc_binding_handle *b = p->binding_handle;
64
65         ninfo.identity_info.domain_name.string = domain;
66         ninfo.identity_info.parameter_control = 0;
67         ninfo.identity_info.logon_id_low = 0;
68         ninfo.identity_info.logon_id_high = 0;
69         ninfo.identity_info.account_name.string = account_name;
70         ninfo.identity_info.workstation.string = workstation;
71         generate_random_buffer(ninfo.challenge,
72                                sizeof(ninfo.challenge));
73         if (nt_hash) {
74                 ninfo.nt.length = 24;
75                 ninfo.nt.data = talloc_array(mem_ctx, uint8_t, 24);
76                 SMBOWFencrypt(nt_hash->hash, ninfo.challenge, ninfo.nt.data);
77         } else {
78                 ninfo.nt.length = 0;
79                 ninfo.nt.data = NULL;
80         }
81
82         if (lm_hash) {
83                 ninfo.lm.length = 24;
84                 ninfo.lm.data = talloc_array(mem_ctx, uint8_t, 24);
85                 SMBOWFencrypt(lm_hash->hash, ninfo.challenge, ninfo.lm.data);
86         } else {
87                 ninfo.lm.length = 0;
88                 ninfo.lm.data = NULL;
89         }
90
91         logon.network = &ninfo;
92
93         r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
94         r.in.computer_name = workstation;
95         r.in.credential = &auth;
96         r.in.return_authenticator = &auth2;
97         r.in.logon_level = 2;
98         r.in.logon = &logon;
99         r.out.validation = &validation;
100         r.out.authoritative = &authoritative;
101
102         ZERO_STRUCT(auth2);
103         netlogon_creds_client_authenticator(creds, &auth);
104
105         r.in.validation_level = 3;
106
107         status = dcerpc_netr_LogonSamLogon_r(b, mem_ctx, &r);
108         if (!NT_STATUS_IS_OK(status)) {
109                 return status;
110         }
111
112         if (!netlogon_creds_client_check(creds, &r.out.return_authenticator->cred)) {
113                 torture_comment(tctx, "Credential chaining failed\n");
114         }
115
116         if (info3) {
117                 *info3 = validation.sam3;
118         }
119
120         return r.out.result;
121 }
122
123 struct samsync_state {
124 /* we remember the sequence numbers so we can easily do a DatabaseDelta */
125         uint64_t seq_num[3];
126         const char *domain_name[2];
127         struct samsync_secret *secrets;
128         struct samsync_trusted_domain *trusted_domains;
129         struct netlogon_creds_CredentialState *creds;
130         struct netlogon_creds_CredentialState *creds_netlogon_wksta;
131         struct policy_handle *connect_handle;
132         struct policy_handle *domain_handle[2];
133         struct dom_sid *sid[2];
134         struct dcerpc_pipe *p;
135         struct dcerpc_binding_handle *b;
136         struct dcerpc_pipe *p_netlogon_wksta;
137         struct dcerpc_pipe *p_samr;
138         struct dcerpc_binding_handle *b_samr;
139         struct dcerpc_pipe *p_lsa;
140         struct dcerpc_binding_handle *b_lsa;
141         struct policy_handle *lsa_handle;
142 };
143
144 struct samsync_secret {
145         struct samsync_secret *prev, *next;
146         DATA_BLOB secret;
147         const char *name;
148         NTTIME mtime;
149 };
150
151 struct samsync_trusted_domain {
152         struct samsync_trusted_domain *prev, *next;
153         struct dom_sid *sid;
154         const char *name;
155 };
156
157 static struct policy_handle *samsync_open_domain(struct torture_context *tctx,
158                                                  TALLOC_CTX *mem_ctx,
159                                                  struct samsync_state *samsync_state,
160                                                  const char *domain,
161                                                  struct dom_sid **sid_p)
162 {
163         struct lsa_String name;
164         struct samr_OpenDomain o;
165         struct samr_LookupDomain l;
166         struct dom_sid2 *sid = NULL;
167         struct policy_handle *domain_handle = talloc(mem_ctx, struct policy_handle);
168         NTSTATUS nt_status;
169
170         name.string = domain;
171         l.in.connect_handle = samsync_state->connect_handle;
172         l.in.domain_name = &name;
173         l.out.sid = &sid;
174
175         nt_status = dcerpc_samr_LookupDomain_r(samsync_state->b_samr, mem_ctx, &l);
176         if (!NT_STATUS_IS_OK(nt_status)) {
177                 torture_comment(tctx, "LookupDomain failed - %s\n", nt_errstr(nt_status));
178                 return NULL;
179         }
180         if (!NT_STATUS_IS_OK(l.out.result)) {
181                 torture_comment(tctx, "LookupDomain failed - %s\n", nt_errstr(l.out.result));
182                 return NULL;
183         }
184
185         o.in.connect_handle = samsync_state->connect_handle;
186         o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
187         o.in.sid = *l.out.sid;
188         o.out.domain_handle = domain_handle;
189
190         if (sid_p) {
191                 *sid_p = *l.out.sid;
192         }
193
194         nt_status = dcerpc_samr_OpenDomain_r(samsync_state->b_samr, mem_ctx, &o);
195         if (!NT_STATUS_IS_OK(nt_status)) {
196                 torture_comment(tctx, "OpenDomain failed - %s\n", nt_errstr(nt_status));
197                 return NULL;
198         }
199         if (!NT_STATUS_IS_OK(o.out.result)) {
200                 torture_comment(tctx, "OpenDomain failed - %s\n", nt_errstr(o.out.result));
201                 return NULL;
202         }
203
204         return domain_handle;
205 }
206
207 static struct sec_desc_buf *samsync_query_samr_sec_desc(struct torture_context *tctx,
208                                                         TALLOC_CTX *mem_ctx,
209                                                         struct samsync_state *samsync_state,
210                                                         struct policy_handle *handle)
211 {
212         struct samr_QuerySecurity r;
213         struct sec_desc_buf *sdbuf = NULL;
214         NTSTATUS status;
215
216         r.in.handle = handle;
217         r.in.sec_info = 0x7;
218         r.out.sdbuf = &sdbuf;
219
220         status = dcerpc_samr_QuerySecurity_r(samsync_state->b_samr, mem_ctx, &r);
221         if (!NT_STATUS_IS_OK(status)) {
222                 torture_comment(tctx, "SAMR QuerySecurity failed - %s\n", nt_errstr(status));
223                 return NULL;
224         }
225         if (!NT_STATUS_IS_OK(r.out.result)) {
226                 torture_comment(tctx, "SAMR QuerySecurity failed - %s\n", nt_errstr(r.out.result));
227                 return NULL;
228         }
229
230         return sdbuf;
231 }
232
233 static struct sec_desc_buf *samsync_query_lsa_sec_desc(struct torture_context *tctx,
234                                                        TALLOC_CTX *mem_ctx,
235                                                        struct samsync_state *samsync_state,
236                                                        struct policy_handle *handle)
237 {
238         struct lsa_QuerySecurity r;
239         struct sec_desc_buf *sdbuf = NULL;
240         NTSTATUS status;
241
242         r.in.handle = handle;
243         r.in.sec_info = 0x7;
244         r.out.sdbuf = &sdbuf;
245
246         status = dcerpc_lsa_QuerySecurity_r(samsync_state->b_lsa, mem_ctx, &r);
247         if (!NT_STATUS_IS_OK(status)) {
248                 torture_comment(tctx, "LSA QuerySecurity failed - %s\n", nt_errstr(status));
249                 return NULL;
250         }
251         if (!NT_STATUS_IS_OK(r.out.result)) {
252                 torture_comment(tctx, "LSA QuerySecurity failed - %s\n", nt_errstr(r.out.result));
253                 return NULL;
254         }
255
256         return sdbuf;
257 }
258
259 #define TEST_UINT64_EQUAL(i1, i2) do {\
260         if (i1 != i2) {\
261               torture_comment(tctx, "%s: uint64 mismatch: " #i1 ": 0x%016llx (%lld) != " #i2 ": 0x%016llx (%lld)\n", \
262                      __location__, \
263                      (long long)i1, (long long)i1, \
264                      (long long)i2, (long long)i2);\
265               ret = false;\
266         } \
267 } while (0)
268 #define TEST_INT_EQUAL(i1, i2) do {\
269         if (i1 != i2) {\
270               torture_comment(tctx, "%s: integer mismatch: " #i1 ": 0x%08x (%d) != " #i2 ": 0x%08x (%d)\n", \
271                      __location__, i1, i1, i2, i2);                     \
272               ret = false;\
273         } \
274 } while (0)
275 #define TEST_TIME_EQUAL(t1, t2) do {\
276         if (t1 != t2) {\
277               torture_comment(tctx, "%s: NTTIME mismatch: " #t1 ":%s != " #t2 ": %s\n", \
278                      __location__, nt_time_string(mem_ctx, t1),  nt_time_string(mem_ctx, t2));\
279               ret = false;\
280         } \
281 } while (0)
282
283 #define TEST_STRING_EQUAL(s1, s2) do {\
284         if (!((!s1.string || s1.string[0]=='\0') && (!s2.string || s2.string[0]=='\0')) \
285             && strcmp_safe(s1.string, s2.string) != 0) {\
286               torture_comment(tctx, "%s: string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
287                      __location__, s1.string, s2.string);\
288               ret = false;\
289         } \
290 } while (0)
291
292 #define TEST_BINARY_STRING_EQUAL(s1, s2) do {\
293         if (!((!s1.array || s1.array[0]=='\0') && (!s2.array || s2.array[0]=='\0')) \
294             && memcmp(s1.array, s2.array, s1.length * 2) != 0) {\
295               torture_comment(tctx, "%s: string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
296                      __location__, (const char *)s1.array, (const char *)s2.array);\
297               ret = false;\
298         } \
299 } while (0)
300
301 #define TEST_SID_EQUAL(s1, s2) do {\
302         if (!dom_sid_equal(s1, s2)) {\
303               torture_comment(tctx, "%s: dom_sid mismatch: " #s1 ":%s != " #s2 ": %s\n", \
304                      __location__, dom_sid_string(mem_ctx, s1), dom_sid_string(mem_ctx, s2));\
305               ret = false;\
306         } \
307 } while (0)
308
309 /* The ~SEC_DESC_SACL_PRESENT is because we don't, as administrator,
310  * get back the SACL part of the SD when we ask over SAMR */
311
312 #define TEST_SEC_DESC_EQUAL(sd1, pipe, handle) do {\
313         struct sec_desc_buf *sdbuf = samsync_query_ ##pipe## _sec_desc(tctx, mem_ctx, samsync_state, \
314                                                             handle); \
315         if (!sdbuf || !sdbuf->sd) { \
316                 torture_comment(tctx, "Could not obtain security descriptor to match " #sd1 "\n");\
317                 ret = false; \
318         } else {\
319                 if (!security_descriptor_mask_equal(sd1.sd, sdbuf->sd, \
320                             ~SEC_DESC_SACL_PRESENT)) {\
321                         torture_comment(tctx, "Security Descriptor Mismatch for %s:\n", #sd1);\
322                         ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamSync", sd1.sd);\
323                         ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamR", sdbuf->sd);\
324                         ret = false;\
325                 }\
326         }\
327 } while (0)
328
329 static bool samsync_handle_domain(struct torture_context *tctx, TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
330                            int database_id, struct netr_DELTA_ENUM *delta)
331 {
332         struct netr_DELTA_DOMAIN *domain = delta->delta_union.domain;
333         struct dom_sid *dom_sid;
334         struct samr_QueryDomainInfo q[14]; /* q[0] will be unused simple for clarity */
335         union samr_DomainInfo *info[14];
336         uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
337         int i;
338         bool ret = true;
339
340         samsync_state->seq_num[database_id] =
341                 domain->sequence_num;
342         switch (database_id) {
343         case SAM_DATABASE_DOMAIN:
344                 break;
345         case SAM_DATABASE_BUILTIN:
346                 if (strcasecmp_m("BUILTIN", domain->domain_name.string) != 0) {
347                         torture_comment(tctx, "BUILTIN domain has different name: %s\n", domain->domain_name.string);
348                 }
349                 break;
350         case SAM_DATABASE_PRIVS:
351                 torture_comment(tctx, "DOMAIN entry on privs DB!\n");
352                 return false;
353                 break;
354         }
355
356         if (!samsync_state->domain_name[database_id]) {
357                 samsync_state->domain_name[database_id] =
358                         talloc_reference(samsync_state, domain->domain_name.string);
359         } else {
360                 if (strcasecmp_m(samsync_state->domain_name[database_id], domain->domain_name.string) != 0) {
361                         torture_comment(tctx, "Domain has name varies!: %s != %s\n", samsync_state->domain_name[database_id],
362                                domain->domain_name.string);
363                         return false;
364                 }
365         }
366
367         if (!samsync_state->domain_handle[database_id]) {
368                 samsync_state->domain_handle[database_id]
369                         = talloc_reference(samsync_state,
370                                            samsync_open_domain(tctx,
371                                                                mem_ctx, samsync_state, samsync_state->domain_name[database_id],
372                                                                &dom_sid));
373         }
374         if (samsync_state->domain_handle[database_id]) {
375                 samsync_state->sid[database_id] = talloc_reference(samsync_state, dom_sid);
376         }
377
378         torture_comment(tctx, "\tsequence_nums[%d/%s]=%llu\n",
379                database_id, domain->domain_name.string,
380                (long long)samsync_state->seq_num[database_id]);
381
382         for (i=0;i<ARRAY_SIZE(levels);i++) {
383
384                 q[levels[i]].in.domain_handle = samsync_state->domain_handle[database_id];
385                 q[levels[i]].in.level = levels[i];
386                 q[levels[i]].out.info = &info[levels[i]];
387
388                 torture_assert_ntstatus_ok(tctx,
389                         dcerpc_samr_QueryDomainInfo_r(samsync_state->b_samr, mem_ctx, &q[levels[i]]),
390                         talloc_asprintf(tctx, "QueryDomainInfo level %u failed", q[levels[i]].in.level));
391                 torture_assert_ntstatus_ok(tctx, q[levels[i]].out.result,
392                         talloc_asprintf(tctx, "QueryDomainInfo level %u failed", q[levels[i]].in.level));
393         }
394
395         TEST_STRING_EQUAL(info[5]->info5.domain_name, domain->domain_name);
396
397         TEST_STRING_EQUAL(info[2]->general.oem_information, domain->oem_information);
398         TEST_STRING_EQUAL(info[4]->oem.oem_information, domain->oem_information);
399         TEST_TIME_EQUAL(info[2]->general.force_logoff_time, domain->force_logoff_time);
400         TEST_TIME_EQUAL(info[3]->info3.force_logoff_time, domain->force_logoff_time);
401
402         TEST_TIME_EQUAL(info[1]->info1.min_password_length, domain->min_password_length);
403         TEST_TIME_EQUAL(info[1]->info1.password_history_length, domain->password_history_length);
404         TEST_TIME_EQUAL(info[1]->info1.max_password_age, domain->max_password_age);
405         TEST_TIME_EQUAL(info[1]->info1.min_password_age, domain->min_password_age);
406
407         TEST_UINT64_EQUAL(info[8]->info8.sequence_num,
408                         domain->sequence_num);
409         TEST_TIME_EQUAL(info[8]->info8.domain_create_time,
410                         domain->domain_create_time);
411         TEST_TIME_EQUAL(info[13]->info13.domain_create_time,
412                         domain->domain_create_time);
413
414         TEST_SEC_DESC_EQUAL(domain->sdbuf, samr, samsync_state->domain_handle[database_id]);
415
416         return ret;
417 }
418
419 static bool samsync_handle_policy(struct torture_context *tctx,
420                                   TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
421                            int database_id, struct netr_DELTA_ENUM *delta)
422 {
423         struct netr_DELTA_POLICY *policy = delta->delta_union.policy;
424
425         samsync_state->seq_num[database_id] =
426                 policy->sequence_num;
427
428         if (!samsync_state->domain_name[SAM_DATABASE_DOMAIN]) {
429                 samsync_state->domain_name[SAM_DATABASE_DOMAIN] =
430                         talloc_reference(samsync_state, policy->primary_domain_name.string);
431         } else {
432                 if (strcasecmp_m(samsync_state->domain_name[SAM_DATABASE_DOMAIN], policy->primary_domain_name.string) != 0) {
433                         torture_comment(tctx, "PRIMARY domain has name varies between DOMAIN and POLICY!: %s != %s\n", samsync_state->domain_name[SAM_DATABASE_DOMAIN],
434                                policy->primary_domain_name.string);
435                         return false;
436                 }
437         }
438
439         if (!dom_sid_equal(samsync_state->sid[SAM_DATABASE_DOMAIN], policy->sid)) {
440                 torture_comment(tctx, "Domain SID from POLICY (%s) does not match domain sid from SAMR (%s)\n",
441                        dom_sid_string(mem_ctx, policy->sid), dom_sid_string(mem_ctx, samsync_state->sid[SAM_DATABASE_DOMAIN]));
442                 return false;
443         }
444
445         torture_comment(tctx, "\tsequence_nums[%d/PRIVS]=%llu\n",
446                database_id,
447                (long long)samsync_state->seq_num[database_id]);
448         return true;
449 }
450
451 static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
452                                 int database_id, struct netr_DELTA_ENUM *delta)
453 {
454         uint32_t rid = delta->delta_id_union.rid;
455         struct netr_DELTA_USER *user = delta->delta_union.user;
456         struct netr_SamInfo3 *info3;
457         struct samr_Password lm_hash;
458         struct samr_Password nt_hash;
459         struct samr_Password *lm_hash_p = NULL;
460         struct samr_Password *nt_hash_p = NULL;
461         const char *domain = samsync_state->domain_name[database_id];
462         const char *username = user->account_name.string;
463         NTSTATUS nt_status;
464         bool ret = true;
465
466         struct samr_OpenUser r;
467         struct samr_QueryUserInfo q;
468         union samr_UserInfo *info;
469         struct policy_handle user_handle;
470
471         struct samr_GetGroupsForUser getgroups;
472         struct samr_RidWithAttributeArray *rids;
473
474         if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
475                 torture_comment(tctx, "SamSync needs domain information before the users\n");
476                 return false;
477         }
478
479         r.in.domain_handle = samsync_state->domain_handle[database_id];
480         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
481         r.in.rid = rid;
482         r.out.user_handle = &user_handle;
483
484         torture_assert_ntstatus_ok(tctx,
485                 dcerpc_samr_OpenUser_r(samsync_state->b_samr, mem_ctx, &r),
486                 talloc_asprintf(tctx, "OpenUser(%u) failed", rid));
487         torture_assert_ntstatus_ok(tctx, r.out.result,
488                 talloc_asprintf(tctx, "OpenUser(%u) failed", rid));
489
490         q.in.user_handle = &user_handle;
491         q.in.level = 21;
492         q.out.info = &info;
493
494         TEST_SEC_DESC_EQUAL(user->sdbuf, samr, &user_handle);
495
496         torture_assert_ntstatus_ok(tctx,
497                 dcerpc_samr_QueryUserInfo_r(samsync_state->b_samr, mem_ctx, &q),
498                 talloc_asprintf(tctx, "OpenUserInfo level %u failed", q.in.level));
499         torture_assert_ntstatus_ok(tctx, q.out.result,
500                 talloc_asprintf(tctx, "OpenUserInfo level %u failed", q.in.level));
501
502         getgroups.in.user_handle = &user_handle;
503         getgroups.out.rids = &rids;
504
505         torture_assert_ntstatus_ok(tctx,
506                 dcerpc_samr_GetGroupsForUser_r(samsync_state->b_samr, mem_ctx, &getgroups),
507                 "GetGroupsForUser failed");
508         torture_assert_ntstatus_ok(tctx, getgroups.out.result,
509                 "GetGroupsForUser failed");
510
511         if (!test_samr_handle_Close(samsync_state->b_samr, tctx, &user_handle)) {
512                 torture_comment(tctx, "samr_handle_Close failed - %s\n",
513                        nt_errstr(nt_status));
514                 ret = false;
515         }
516         if (!ret) {
517                 return false;
518         }
519
520         if (!NT_STATUS_IS_OK(nt_status)) {
521                 torture_comment(tctx, "QueryUserInfo level %u failed - %s\n",
522                        q.in.level, nt_errstr(nt_status));
523                 return false;
524         }
525
526         TEST_STRING_EQUAL(info->info21.account_name, user->account_name);
527         TEST_STRING_EQUAL(info->info21.full_name, user->full_name);
528         TEST_INT_EQUAL(info->info21.rid, user->rid);
529         TEST_INT_EQUAL(info->info21.primary_gid, user->primary_gid);
530         TEST_STRING_EQUAL(info->info21.home_directory, user->home_directory);
531         TEST_STRING_EQUAL(info->info21.home_drive, user->home_drive);
532         TEST_STRING_EQUAL(info->info21.logon_script, user->logon_script);
533         TEST_STRING_EQUAL(info->info21.description, user->description);
534         TEST_STRING_EQUAL(info->info21.workstations, user->workstations);
535
536         TEST_TIME_EQUAL(info->info21.last_logon, user->last_logon);
537         TEST_TIME_EQUAL(info->info21.last_logoff, user->last_logoff);
538
539
540         TEST_INT_EQUAL(info->info21.logon_hours.units_per_week,
541                        user->logon_hours.units_per_week);
542         if (ret) {
543                 if (memcmp(info->info21.logon_hours.bits, user->logon_hours.bits,
544                            info->info21.logon_hours.units_per_week/8) != 0) {
545                         torture_comment(tctx, "Logon hours mismatch\n");
546                         ret = false;
547                 }
548         }
549
550         TEST_INT_EQUAL(info->info21.bad_password_count,
551                        user->bad_password_count);
552         TEST_INT_EQUAL(info->info21.logon_count,
553                        user->logon_count);
554
555         TEST_TIME_EQUAL(info->info21.last_password_change,
556                        user->last_password_change);
557         TEST_TIME_EQUAL(info->info21.acct_expiry,
558                        user->acct_expiry);
559
560         TEST_INT_EQUAL((info->info21.acct_flags & ~ACB_PW_EXPIRED), user->acct_flags);
561         if (user->acct_flags & ACB_PWNOEXP) {
562                 if (info->info21.acct_flags & ACB_PW_EXPIRED) {
563                         torture_comment(tctx, "ACB flags mismatch: both expired and no expiry!\n");
564                         ret = false;
565                 }
566                 if (info->info21.force_password_change != (NTTIME)0x7FFFFFFFFFFFFFFFULL) {
567                         torture_comment(tctx, "ACB flags mismatch: no password expiry, but force password change 0x%016llx (%lld) != 0x%016llx (%lld)\n",
568                                (unsigned long long)info->info21.force_password_change,
569                                (unsigned long long)info->info21.force_password_change,
570                                (unsigned long long)0x7FFFFFFFFFFFFFFFULL, (unsigned long long)0x7FFFFFFFFFFFFFFFULL
571                                 );
572                         ret = false;
573                 }
574         }
575
576         TEST_INT_EQUAL(info->info21.nt_password_set, user->nt_password_present);
577         TEST_INT_EQUAL(info->info21.lm_password_set, user->lm_password_present);
578         TEST_INT_EQUAL(info->info21.password_expired, user->password_expired);
579
580         TEST_STRING_EQUAL(info->info21.comment, user->comment);
581         TEST_BINARY_STRING_EQUAL(info->info21.parameters, user->parameters);
582
583         TEST_INT_EQUAL(info->info21.country_code, user->country_code);
584         TEST_INT_EQUAL(info->info21.code_page, user->code_page);
585
586         TEST_STRING_EQUAL(info->info21.profile_path, user->profile_path);
587
588         if (user->lm_password_present) {
589                 lm_hash_p = &lm_hash;
590         }
591         if (user->nt_password_present) {
592                 nt_hash_p = &nt_hash;
593         }
594
595         if (user->user_private_info.SensitiveData) {
596                 DATA_BLOB data;
597                 struct netr_USER_KEYS keys;
598                 enum ndr_err_code ndr_err;
599                 data.data = user->user_private_info.SensitiveData;
600                 data.length = user->user_private_info.DataLength;
601                 ndr_err = ndr_pull_struct_blob(&data, mem_ctx, lp_iconv_convenience(tctx->lp_ctx), &keys, (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
602                 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
603                         if (keys.keys.keys2.lmpassword.length == 16) {
604                                 lm_hash_p = &lm_hash;
605                         }
606                         if (keys.keys.keys2.ntpassword.length == 16) {
607                                 nt_hash_p = &nt_hash;
608                         }
609                 } else {
610                         torture_comment(tctx, "Failed to parse Sensitive Data for %s:\n", username);
611 #if 0
612                         dump_data(0, data.data, data.length);
613 #endif
614                         return false;
615                 }
616         }
617
618         if (nt_hash_p) {
619                 DATA_BLOB nt_hash_blob = data_blob_const(nt_hash_p, 16);
620                 DEBUG(100,("ACCOUNT [%s\\%-25s] NTHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string_upper(mem_ctx, &nt_hash_blob)));
621         }
622         if (lm_hash_p) {
623                 DATA_BLOB lm_hash_blob = data_blob_const(lm_hash_p, 16);
624                 DEBUG(100,("ACCOUNT [%s\\%-25s] LMHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string_upper(mem_ctx, &lm_hash_blob)));
625         }
626
627         nt_status = test_SamLogon(tctx,
628                                   samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta,
629                                   domain,
630                                   username,
631                                   TEST_WKSTA_MACHINE_NAME,
632                                   lm_hash_p,
633                                   nt_hash_p,
634                                   &info3);
635
636         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED)) {
637                 if (user->acct_flags & ACB_DISABLED) {
638                         return true;
639                 }
640         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)) {
641                 if (user->acct_flags & ACB_WSTRUST) {
642                         return true;
643                 }
644         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT)) {
645                 if (user->acct_flags & ACB_SVRTRUST) {
646                         return true;
647                 }
648         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
649                 if (user->acct_flags & ACB_DOMTRUST) {
650                         return true;
651                 }
652         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
653                 if (user->acct_flags & ACB_DOMTRUST) {
654                         return true;
655                 }
656         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT)) {
657                 if (user->acct_flags & ACB_AUTOLOCK) {
658                         return true;
659                 }
660         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED)) {
661                 if (info->info21.acct_flags & ACB_PW_EXPIRED) {
662                         return true;
663                 }
664         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
665                 if (!lm_hash_p && !nt_hash_p) {
666                         return true;
667                 }
668         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE)) {
669                 /* We would need to know the server's current time to test this properly */
670                 return true;
671         } else if (NT_STATUS_IS_OK(nt_status)) {
672                 TEST_INT_EQUAL(user->rid, info3->base.rid);
673                 TEST_INT_EQUAL(user->primary_gid, info3->base.primary_gid);
674                 /* this is 0x0 from NT4 sp6 */
675                 if (info3->base.acct_flags) {
676                         TEST_INT_EQUAL(user->acct_flags, info3->base.acct_flags);
677                 }
678                 /* this is NULL from NT4 sp6 */
679                 if (info3->base.account_name.string) {
680                         TEST_STRING_EQUAL(user->account_name, info3->base.account_name);
681                 }
682                 /* this is NULL from Win2k3 */
683                 if (info3->base.full_name.string) {
684                         TEST_STRING_EQUAL(user->full_name, info3->base.full_name);
685                 }
686                 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
687                 TEST_STRING_EQUAL(user->profile_path, info3->base.profile_path);
688                 TEST_STRING_EQUAL(user->home_directory, info3->base.home_directory);
689                 TEST_STRING_EQUAL(user->home_drive, info3->base.home_drive);
690                 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
691
692
693                 TEST_TIME_EQUAL(user->last_logon, info3->base.last_logon);
694                 TEST_TIME_EQUAL(user->acct_expiry, info3->base.acct_expiry);
695                 TEST_TIME_EQUAL(user->last_password_change, info3->base.last_password_change);
696                 TEST_TIME_EQUAL(info->info21.force_password_change, info3->base.force_password_change);
697
698                 /* Does the concept of a logoff time ever really
699                  * exist? (not in any sensible way, according to the
700                  * doco I read -- abartlet) */
701
702                 /* This copes with the two different versions of 0 I see */
703                 /* with NT4 sp6 we have the || case */
704                 if (!((user->last_logoff == 0)
705                       || (info3->base.last_logoff == 0x7fffffffffffffffLL))) {
706                         TEST_TIME_EQUAL(user->last_logoff, info3->base.last_logoff);
707                 }
708
709                 TEST_INT_EQUAL(rids->count, info3->base.groups.count);
710                 if (rids->count == info3->base.groups.count) {
711                         int i, j;
712                         int count = rids->count;
713                         bool *matched = talloc_zero_array(mem_ctx, bool, rids->count);
714
715                         for (i = 0; i < count; i++) {
716                                 for (j = 0; j < count; j++) {
717                                         if ((rids->rids[i].rid ==
718                                              info3->base.groups.rids[j].rid)
719                                             && (rids->rids[i].attributes ==
720                                                 info3->base.groups.rids[j].attributes)) {
721                                                         matched[i] = true;
722                                                 }
723                                 }
724                         }
725
726                         for (i = 0; i < rids->count; i++) {
727                                 if (matched[i] == false) {
728                                         ret = false;
729                                         torture_comment(tctx, "Could not find group RID %u found in getgroups in NETLOGON reply\n",
730                                                rids->rids[i].rid);
731                                 }
732                         }
733                 }
734                 return ret;
735         } else {
736                 torture_comment(tctx, "Could not validate password for user %s\\%s: %s\n",
737                        domain, username, nt_errstr(nt_status));
738                 return false;
739         }
740         return false;
741 }
742
743 static bool samsync_handle_alias(struct torture_context *tctx,
744                                  TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
745                                  int database_id, struct netr_DELTA_ENUM *delta)
746 {
747         uint32_t rid = delta->delta_id_union.rid;
748         struct netr_DELTA_ALIAS *alias = delta->delta_union.alias;
749         bool ret = true;
750
751         struct samr_OpenAlias r;
752         struct samr_QueryAliasInfo q;
753         union samr_AliasInfo *info;
754         struct policy_handle alias_handle;
755
756         if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
757                 torture_comment(tctx, "SamSync needs domain information before the users\n");
758                 return false;
759         }
760
761         r.in.domain_handle = samsync_state->domain_handle[database_id];
762         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
763         r.in.rid = rid;
764         r.out.alias_handle = &alias_handle;
765
766         torture_assert_ntstatus_ok(tctx,
767                 dcerpc_samr_OpenAlias_r(samsync_state->b_samr, mem_ctx, &r),
768                 talloc_asprintf(tctx, "OpenUser(%u) failed", rid));
769         torture_assert_ntstatus_ok(tctx, r.out.result,
770                 talloc_asprintf(tctx, "OpenUser(%u) failed", rid));
771
772         q.in.alias_handle = &alias_handle;
773         q.in.level = 1;
774         q.out.info = &info;
775
776         TEST_SEC_DESC_EQUAL(alias->sdbuf, samr, &alias_handle);
777
778         torture_assert_ntstatus_ok(tctx,
779                 dcerpc_samr_QueryAliasInfo_r(samsync_state->b_samr, mem_ctx, &q),
780                 "QueryAliasInfo failed");
781         if (!test_samr_handle_Close(samsync_state->b_samr, tctx, &alias_handle)) {
782                 return false;
783         }
784
785         if (!NT_STATUS_IS_OK(q.out.result)) {
786                 torture_comment(tctx, "QueryAliasInfo level %u failed - %s\n",
787                        q.in.level, nt_errstr(q.out.result));
788                 return false;
789         }
790
791         TEST_STRING_EQUAL(info->all.name, alias->alias_name);
792         TEST_STRING_EQUAL(info->all.description, alias->description);
793         return ret;
794 }
795
796 static bool samsync_handle_group(struct torture_context *tctx,
797                                  TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
798                                  int database_id, struct netr_DELTA_ENUM *delta)
799 {
800         uint32_t rid = delta->delta_id_union.rid;
801         struct netr_DELTA_GROUP *group = delta->delta_union.group;
802         bool ret = true;
803
804         struct samr_OpenGroup r;
805         struct samr_QueryGroupInfo q;
806         union samr_GroupInfo *info;
807         struct policy_handle group_handle;
808
809         if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
810                 torture_comment(tctx, "SamSync needs domain information before the users\n");
811                 return false;
812         }
813
814         r.in.domain_handle = samsync_state->domain_handle[database_id];
815         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
816         r.in.rid = rid;
817         r.out.group_handle = &group_handle;
818
819         torture_assert_ntstatus_ok(tctx,
820                 dcerpc_samr_OpenGroup_r(samsync_state->b_samr, mem_ctx, &r),
821                 talloc_asprintf(tctx, "OpenUser(%u) failed", rid));
822         torture_assert_ntstatus_ok(tctx, r.out.result,
823                 talloc_asprintf(tctx, "OpenUser(%u) failed", rid));
824
825         q.in.group_handle = &group_handle;
826         q.in.level = 1;
827         q.out.info = &info;
828
829         TEST_SEC_DESC_EQUAL(group->sdbuf, samr, &group_handle);
830
831         torture_assert_ntstatus_ok(tctx,
832                 dcerpc_samr_QueryGroupInfo_r(samsync_state->b_samr, mem_ctx, &q),
833                 "QueryGroupInfo failed");
834         if (!test_samr_handle_Close(samsync_state->b_samr, tctx, &group_handle)) {
835                 return false;
836         }
837
838         if (!NT_STATUS_IS_OK(q.out.result)) {
839                 torture_comment(tctx, "QueryGroupInfo level %u failed - %s\n",
840                        q.in.level, nt_errstr(q.out.result));
841                 return false;
842         }
843
844         TEST_STRING_EQUAL(info->all.name, group->group_name);
845         TEST_INT_EQUAL(info->all.attributes, group->attributes);
846         TEST_STRING_EQUAL(info->all.description, group->description);
847         return ret;
848 }
849
850 static bool samsync_handle_secret(struct torture_context *tctx,
851                                   TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
852                                   int database_id, struct netr_DELTA_ENUM *delta)
853 {
854         struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
855         const char *name = delta->delta_id_union.name;
856         struct samsync_secret *nsec = talloc(samsync_state, struct samsync_secret);
857         struct samsync_secret *old = talloc(mem_ctx, struct samsync_secret);
858         struct lsa_QuerySecret q;
859         struct lsa_OpenSecret o;
860         struct policy_handle sec_handle;
861         struct lsa_DATA_BUF_PTR bufp1;
862         struct lsa_DATA_BUF_PTR bufp2;
863         NTTIME nsec_mtime;
864         NTTIME old_mtime;
865         bool ret = true;
866         DATA_BLOB lsa_blob1, lsa_blob_out, session_key;
867         NTSTATUS status;
868
869         nsec->name = talloc_reference(nsec, name);
870         nsec->secret = data_blob_talloc(nsec, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
871         nsec->mtime = secret->current_cipher_set_time;
872
873         nsec = talloc_reference(samsync_state, nsec);
874         DLIST_ADD(samsync_state->secrets, nsec);
875
876         old->name = talloc_reference(old, name);
877         old->secret = data_blob_const(secret->old_cipher.cipher_data, secret->old_cipher.maxlen);
878         old->mtime = secret->old_cipher_set_time;
879
880         o.in.handle = samsync_state->lsa_handle;
881         o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
882         o.in.name.string = name;
883         o.out.sec_handle = &sec_handle;
884
885         torture_assert_ntstatus_ok(tctx,
886                 dcerpc_lsa_OpenSecret_r(samsync_state->b_lsa, mem_ctx, &o),
887                 "OpenSecret failed");
888         torture_assert_ntstatus_ok(tctx, o.out.result,
889                 "OpenSecret failed");
890
891 /*
892   We would like to do this, but it is NOT_SUPPORTED on win2k3
893   TEST_SEC_DESC_EQUAL(secret->sdbuf, lsa, &sec_handle);
894 */
895         status = dcerpc_fetch_session_key(samsync_state->p_lsa, &session_key);
896         if (!NT_STATUS_IS_OK(status)) {
897                 torture_comment(tctx, "dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
898                 return false;
899         }
900
901
902         ZERO_STRUCT(nsec_mtime);
903         ZERO_STRUCT(old_mtime);
904
905         /* fetch the secret back again */
906         q.in.sec_handle = &sec_handle;
907         q.in.new_val = &bufp1;
908         q.in.new_mtime = &nsec_mtime;
909         q.in.old_val = &bufp2;
910         q.in.old_mtime = &old_mtime;
911
912         bufp1.buf = NULL;
913         bufp2.buf = NULL;
914
915         torture_assert_ntstatus_ok(tctx,
916                 dcerpc_lsa_QuerySecret_r(samsync_state->b_lsa, mem_ctx, &q),
917                 "QuerySecret failed");
918         if (NT_STATUS_EQUAL(NT_STATUS_ACCESS_DENIED, q.out.result)) {
919                 /* some things are just off limits */
920                 return true;
921         } else if (!NT_STATUS_IS_OK(q.out.result)) {
922                 torture_comment(tctx, "QuerySecret failed - %s\n", nt_errstr(q.out.result));
923                 return false;
924         }
925
926         if (q.out.old_val->buf == NULL) {
927                 /* probably just not available due to ACLs */
928         } else {
929                 lsa_blob1.data = q.out.old_val->buf->data;
930                 lsa_blob1.length = q.out.old_val->buf->length;
931
932                 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
933                 if (!NT_STATUS_IS_OK(status)) {
934                         torture_comment(tctx, "Failed to decrypt secrets OLD blob: %s\n", nt_errstr(status));
935                         return false;
936                 }
937
938                 if (!q.out.old_mtime) {
939                         torture_comment(tctx, "OLD mtime not available on LSA for secret %s\n", old->name);
940                         ret = false;
941                 }
942                 if (old->mtime != *q.out.old_mtime) {
943                         torture_comment(tctx, "OLD mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
944                                old->name, nt_time_string(mem_ctx, old->mtime),
945                                nt_time_string(mem_ctx, *q.out.old_mtime));
946                         ret = false;
947                 }
948
949                 if (old->secret.length != lsa_blob_out.length) {
950                         torture_comment(tctx, "Returned secret %s doesn't match: %d != %d\n",
951                                old->name, (int)old->secret.length, (int)lsa_blob_out.length);
952                         ret = false;
953                 } else if (memcmp(lsa_blob_out.data,
954                            old->secret.data, old->secret.length) != 0) {
955                         torture_comment(tctx, "Returned secret %s doesn't match: \n",
956                                old->name);
957                         DEBUG(1, ("SamSync Secret:\n"));
958                         dump_data(1, old->secret.data, old->secret.length);
959                         DEBUG(1, ("LSA Secret:\n"));
960                         dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
961                         ret = false;
962                 }
963
964         }
965
966         if (q.out.new_val->buf == NULL) {
967                 /* probably just not available due to ACLs */
968         } else {
969                 lsa_blob1.data = q.out.new_val->buf->data;
970                 lsa_blob1.length = q.out.new_val->buf->length;
971
972                 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
973                 if (!NT_STATUS_IS_OK(status)) {
974                         torture_comment(tctx, "Failed to decrypt secrets OLD blob\n");
975                         return false;
976                 }
977
978                 if (!q.out.new_mtime) {
979                         torture_comment(tctx, "NEW mtime not available on LSA for secret %s\n", nsec->name);
980                         ret = false;
981                 }
982                 if (nsec->mtime != *q.out.new_mtime) {
983                         torture_comment(tctx, "NEW mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
984                                nsec->name, nt_time_string(mem_ctx, nsec->mtime),
985                                nt_time_string(mem_ctx, *q.out.new_mtime));
986                         ret = false;
987                 }
988
989                 if (nsec->secret.length != lsa_blob_out.length) {
990                         torture_comment(tctx, "Returned secret %s doesn't match: %d != %d\n",
991                                nsec->name, (int)nsec->secret.length, (int)lsa_blob_out.length);
992                         ret = false;
993                 } else if (memcmp(lsa_blob_out.data,
994                            nsec->secret.data, nsec->secret.length) != 0) {
995                         torture_comment(tctx, "Returned secret %s doesn't match: \n",
996                                nsec->name);
997                         DEBUG(1, ("SamSync Secret:\n"));
998                         dump_data(1, nsec->secret.data, nsec->secret.length);
999                         DEBUG(1, ("LSA Secret:\n"));
1000                         dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
1001                         ret = false;
1002                 }
1003         }
1004
1005         return ret;
1006 }
1007
1008 static bool samsync_handle_trusted_domain(struct torture_context *tctx,
1009                                           TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
1010                                           int database_id, struct netr_DELTA_ENUM *delta)
1011 {
1012         bool ret = true;
1013         struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
1014         struct dom_sid *dom_sid = delta->delta_id_union.sid;
1015
1016         struct samsync_trusted_domain *ndom = talloc(samsync_state, struct samsync_trusted_domain);
1017         struct lsa_OpenTrustedDomain t;
1018         struct policy_handle trustdom_handle;
1019         struct lsa_QueryTrustedDomainInfo q;
1020         union lsa_TrustedDomainInfo *info[9];
1021         union lsa_TrustedDomainInfo *_info = NULL;
1022         int levels [] = {1, 3, 8};
1023         int i;
1024
1025         ndom->name = talloc_reference(ndom, trusted_domain->domain_name.string);
1026         ndom->sid = talloc_reference(ndom, dom_sid);
1027
1028         t.in.handle = samsync_state->lsa_handle;
1029         t.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1030         t.in.sid = dom_sid;
1031         t.out.trustdom_handle = &trustdom_handle;
1032
1033         torture_assert_ntstatus_ok(tctx,
1034                 dcerpc_lsa_OpenTrustedDomain_r(samsync_state->b_lsa, mem_ctx, &t),
1035                 "OpenTrustedDomain failed");
1036         torture_assert_ntstatus_ok(tctx, t.out.result,
1037                 "OpenTrustedDomain failed");
1038
1039         for (i=0; i< ARRAY_SIZE(levels); i++) {
1040                 q.in.trustdom_handle = &trustdom_handle;
1041                 q.in.level = levels[i];
1042                 q.out.info = &_info;
1043                 torture_assert_ntstatus_ok(tctx,
1044                         dcerpc_lsa_QueryTrustedDomainInfo_r(samsync_state->b_lsa, mem_ctx, &q),
1045                         "QueryTrustedDomainInfo failed");
1046                 if (!NT_STATUS_IS_OK(q.out.result)) {
1047                         if (q.in.level == 8 && NT_STATUS_EQUAL(q.out.result, NT_STATUS_INVALID_PARAMETER)) {
1048                                 info[levels[i]] = NULL;
1049                                 continue;
1050                         }
1051                         torture_comment(tctx, "QueryInfoTrustedDomain level %d failed - %s\n",
1052                                levels[i], nt_errstr(q.out.result));
1053                         return false;
1054                 }
1055                 info[levels[i]]  = _info;
1056         }
1057
1058         if (info[8]) {
1059                 TEST_SID_EQUAL(info[8]->full_info.info_ex.sid, dom_sid);
1060                 TEST_STRING_EQUAL(info[8]->full_info.info_ex.netbios_name, trusted_domain->domain_name);
1061         }
1062         TEST_STRING_EQUAL(info[1]->name.netbios_name, trusted_domain->domain_name);
1063         TEST_INT_EQUAL(info[3]->posix_offset.posix_offset, trusted_domain->posix_offset);
1064 /*
1065   We would like to do this, but it is NOT_SUPPORTED on win2k3
1066         TEST_SEC_DESC_EQUAL(trusted_domain->sdbuf, lsa, &trustdom_handle);
1067 */
1068         ndom = talloc_reference(samsync_state, ndom);
1069         DLIST_ADD(samsync_state->trusted_domains, ndom);
1070
1071         return ret;
1072 }
1073
1074 static bool samsync_handle_account(struct torture_context *tctx,
1075                                    TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
1076                                           int database_id, struct netr_DELTA_ENUM *delta)
1077 {
1078         bool ret = true;
1079         struct netr_DELTA_ACCOUNT *account = delta->delta_union.account;
1080         struct dom_sid *dom_sid = delta->delta_id_union.sid;
1081
1082         struct lsa_OpenAccount a;
1083         struct policy_handle acct_handle;
1084         struct lsa_EnumPrivsAccount e;
1085         struct lsa_PrivilegeSet *privs = NULL;
1086         struct lsa_LookupPrivName r;
1087
1088         int i, j;
1089
1090         bool *found_priv_in_lsa;
1091
1092         a.in.handle = samsync_state->lsa_handle;
1093         a.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1094         a.in.sid = dom_sid;
1095         a.out.acct_handle = &acct_handle;
1096
1097         torture_assert_ntstatus_ok(tctx,
1098                 dcerpc_lsa_OpenAccount_r(samsync_state->b_lsa, mem_ctx, &a),
1099                 "OpenAccount failed");
1100         torture_assert_ntstatus_ok(tctx, a.out.result,
1101                 "OpenAccount failed");
1102
1103         TEST_SEC_DESC_EQUAL(account->sdbuf, lsa, &acct_handle);
1104
1105         found_priv_in_lsa = talloc_zero_array(mem_ctx, bool, account->privilege_entries);
1106
1107         e.in.handle = &acct_handle;
1108         e.out.privs = &privs;
1109
1110         torture_assert_ntstatus_ok(tctx,
1111                 dcerpc_lsa_EnumPrivsAccount_r(samsync_state->b_lsa, mem_ctx, &e),
1112                 "EnumPrivsAccount failed");
1113         torture_assert_ntstatus_ok(tctx, e.out.result,
1114                 "EnumPrivsAccount failed");
1115
1116         if ((account->privilege_entries && !privs)) {
1117                 torture_comment(tctx, "Account %s has privileges in SamSync, but not LSA\n",
1118                        dom_sid_string(mem_ctx, dom_sid));
1119                 return false;
1120         }
1121
1122         if (!account->privilege_entries && privs && privs->count) {
1123                 torture_comment(tctx, "Account %s has privileges in LSA, but not SamSync\n",
1124                        dom_sid_string(mem_ctx, dom_sid));
1125                 return false;
1126         }
1127
1128         TEST_INT_EQUAL(account->privilege_entries, privs->count);
1129
1130         for (i=0;i< privs->count; i++) {
1131
1132                 struct lsa_StringLarge *name = NULL;
1133
1134                 r.in.handle = samsync_state->lsa_handle;
1135                 r.in.luid = &privs->set[i].luid;
1136                 r.out.name = &name;
1137
1138                 torture_assert_ntstatus_ok(tctx,
1139                         dcerpc_lsa_LookupPrivName_r(samsync_state->b_lsa, mem_ctx, &r),
1140                         "\nLookupPrivName failed");
1141                 torture_assert_ntstatus_ok(tctx, r.out.result,
1142                         "\nLookupPrivName failed");
1143
1144                 if (!r.out.name) {
1145                         torture_comment(tctx, "\nLookupPrivName failed to return a name\n");
1146                         return false;
1147                 }
1148                 for (j=0;j<account->privilege_entries; j++) {
1149                         if (strcmp(name->string, account->privilege_name[j].string) == 0) {
1150                                 found_priv_in_lsa[j] = true;
1151                                 break;
1152                         }
1153                 }
1154         }
1155         for (j=0;j<account->privilege_entries; j++) {
1156                 if (!found_priv_in_lsa[j]) {
1157                         torture_comment(tctx, "Privilege %s on account %s not found in LSA\n", account->privilege_name[j].string,
1158                                dom_sid_string(mem_ctx, dom_sid));
1159                         ret = false;
1160                 }
1161         }
1162         return ret;
1163 }
1164
1165 /*
1166   try a netlogon DatabaseSync
1167 */
1168 static bool test_DatabaseSync(struct torture_context *tctx,
1169                                                           struct samsync_state *samsync_state,
1170                                                           TALLOC_CTX *mem_ctx)
1171 {
1172         TALLOC_CTX *loop_ctx, *delta_ctx, *trustdom_ctx;
1173         struct netr_DatabaseSync r;
1174         const enum netr_SamDatabaseID database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS};
1175         int i, d;
1176         bool ret = true;
1177         struct samsync_trusted_domain *t;
1178         struct samsync_secret *s;
1179         struct netr_Authenticator return_authenticator, credential;
1180         struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1181
1182         const char *domain, *username;
1183
1184         ZERO_STRUCT(return_authenticator);
1185
1186         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1187         r.in.computername = TEST_MACHINE_NAME;
1188         r.in.preferredmaximumlength = (uint32_t)-1;
1189         r.in.return_authenticator = &return_authenticator;
1190         r.out.return_authenticator = &return_authenticator;
1191         r.out.delta_enum_array = &delta_enum_array;
1192
1193         for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1194
1195                 uint32_t sync_context = 0;
1196
1197                 r.in.database_id = database_ids[i];
1198                 r.in.sync_context = &sync_context;
1199                 r.out.sync_context = &sync_context;
1200
1201                 torture_comment(tctx, "Testing DatabaseSync of id %d\n", r.in.database_id);
1202
1203                 do {
1204                         loop_ctx = talloc_named(mem_ctx, 0, "DatabaseSync loop context");
1205                         netlogon_creds_client_authenticator(samsync_state->creds, &credential);
1206
1207                         r.in.credential = &credential;
1208
1209                         torture_assert_ntstatus_ok(tctx,
1210                                 dcerpc_netr_DatabaseSync_r(samsync_state->b, loop_ctx, &r),
1211                                 "DatabaseSync failed");
1212                         if (!NT_STATUS_IS_OK(r.out.result) &&
1213                             !NT_STATUS_EQUAL(r.out.result, STATUS_MORE_ENTRIES)) {
1214                                 torture_comment(tctx, "DatabaseSync - %s\n", nt_errstr(r.out.result));
1215                                 ret = false;
1216                                 break;
1217                         }
1218
1219                         if (!netlogon_creds_client_check(samsync_state->creds, &r.out.return_authenticator->cred)) {
1220                                 torture_comment(tctx, "Credential chaining failed\n");
1221                         }
1222
1223                         r.in.sync_context = r.out.sync_context;
1224
1225                         for (d=0; d < delta_enum_array->num_deltas; d++) {
1226                                 delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
1227
1228                                 if (!NT_STATUS_IS_OK(samsync_fix_delta(delta_ctx, samsync_state->creds,
1229                                                                        r.in.database_id,
1230                                                                        &delta_enum_array->delta_enum[d]))) {
1231                                         torture_comment(tctx, "Failed to decrypt delta\n");
1232                                         ret = false;
1233                                 }
1234
1235                                 switch (delta_enum_array->delta_enum[d].delta_type) {
1236                                 case NETR_DELTA_DOMAIN:
1237                                         if (!samsync_handle_domain(tctx, delta_ctx, samsync_state,
1238                                                                    r.in.database_id, &delta_enum_array->delta_enum[d])) {
1239                                                 torture_comment(tctx, "Failed to handle DELTA_DOMAIN\n");
1240                                                 ret = false;
1241                                         }
1242                                         break;
1243                                 case NETR_DELTA_GROUP:
1244                                         if (!samsync_handle_group(tctx, delta_ctx, samsync_state,
1245                                                                   r.in.database_id, &delta_enum_array->delta_enum[d])) {
1246                                                 torture_comment(tctx, "Failed to handle DELTA_USER\n");
1247                                                 ret = false;
1248                                         }
1249                                         break;
1250                                 case NETR_DELTA_USER:
1251                                         if (!samsync_handle_user(tctx, delta_ctx, samsync_state,
1252                                                                  r.in.database_id, &delta_enum_array->delta_enum[d])) {
1253                                                 torture_comment(tctx, "Failed to handle DELTA_USER\n");
1254                                                 ret = false;
1255                                         }
1256                                         break;
1257                                 case NETR_DELTA_ALIAS:
1258                                         if (!samsync_handle_alias(tctx, delta_ctx, samsync_state,
1259                                                                   r.in.database_id, &delta_enum_array->delta_enum[d])) {
1260                                                 torture_comment(tctx, "Failed to handle DELTA_ALIAS\n");
1261                                                 ret = false;
1262                                         }
1263                                         break;
1264                                 case NETR_DELTA_POLICY:
1265                                         if (!samsync_handle_policy(tctx, delta_ctx, samsync_state,
1266                                                                    r.in.database_id, &delta_enum_array->delta_enum[d])) {
1267                                                 torture_comment(tctx, "Failed to handle DELTA_POLICY\n");
1268                                                 ret = false;
1269                                         }
1270                                         break;
1271                                 case NETR_DELTA_TRUSTED_DOMAIN:
1272                                         if (!samsync_handle_trusted_domain(tctx, delta_ctx, samsync_state,
1273                                                                            r.in.database_id, &delta_enum_array->delta_enum[d])) {
1274                                                 torture_comment(tctx, "Failed to handle DELTA_TRUSTED_DOMAIN\n");
1275                                                 ret = false;
1276                                         }
1277                                         break;
1278                                 case NETR_DELTA_ACCOUNT:
1279                                         if (!samsync_handle_account(tctx, delta_ctx, samsync_state,
1280                                                                     r.in.database_id, &delta_enum_array->delta_enum[d])) {
1281                                                 torture_comment(tctx, "Failed to handle DELTA_ACCOUNT\n");
1282                                                 ret = false;
1283                                         }
1284                                         break;
1285                                 case NETR_DELTA_SECRET:
1286                                         if (!samsync_handle_secret(tctx, delta_ctx, samsync_state,
1287                                                                    r.in.database_id, &delta_enum_array->delta_enum[d])) {
1288                                                 torture_comment(tctx, "Failed to handle DELTA_SECRET\n");
1289                                                 ret = false;
1290                                         }
1291                                         break;
1292                                 case NETR_DELTA_GROUP_MEMBER:
1293                                 case NETR_DELTA_ALIAS_MEMBER:
1294                                         /* These are harder to cross-check, and we expect them */
1295                                         break;
1296                                 case NETR_DELTA_DELETE_GROUP:
1297                                 case NETR_DELTA_RENAME_GROUP:
1298                                 case NETR_DELTA_DELETE_USER:
1299                                 case NETR_DELTA_RENAME_USER:
1300                                 case NETR_DELTA_DELETE_ALIAS:
1301                                 case NETR_DELTA_RENAME_ALIAS:
1302                                 case NETR_DELTA_DELETE_TRUST:
1303                                 case NETR_DELTA_DELETE_ACCOUNT:
1304                                 case NETR_DELTA_DELETE_SECRET:
1305                                 case NETR_DELTA_DELETE_GROUP2:
1306                                 case NETR_DELTA_DELETE_USER2:
1307                                 case NETR_DELTA_MODIFY_COUNT:
1308                                 default:
1309                                         torture_comment(tctx, "Uxpected delta type %d\n", delta_enum_array->delta_enum[d].delta_type);
1310                                         ret = false;
1311                                         break;
1312                                 }
1313                                 talloc_free(delta_ctx);
1314                         }
1315                         talloc_free(loop_ctx);
1316                 } while (NT_STATUS_EQUAL(r.out.result, STATUS_MORE_ENTRIES));
1317
1318         }
1319
1320         domain = samsync_state->domain_name[SAM_DATABASE_DOMAIN];
1321         if (!domain) {
1322                 torture_comment(tctx, "Never got a DOMAIN object in samsync!\n");
1323                 return false;
1324         }
1325
1326         trustdom_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync Trusted domains context");
1327
1328         username = talloc_asprintf(trustdom_ctx, "%s$", domain);
1329         for (t=samsync_state->trusted_domains; t; t=t->next) {
1330                 char *secret_name = talloc_asprintf(trustdom_ctx, "G$$%s", t->name);
1331                 for (s=samsync_state->secrets; s; s=s->next) {
1332                         if (strcasecmp_m(s->name, secret_name) == 0) {
1333                                 NTSTATUS nt_status;
1334                                 struct samr_Password nt_hash;
1335                                 mdfour(nt_hash.hash, s->secret.data, s->secret.length);
1336
1337                                 torture_comment(tctx, "Checking password for %s\\%s\n", t->name, username);
1338                                 nt_status = test_SamLogon(tctx,
1339                                                           samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1340                                                           t->name,
1341                                                           username,
1342                                                           TEST_WKSTA_MACHINE_NAME,
1343                                                           NULL,
1344                                                           &nt_hash,
1345                                                           NULL);
1346                                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS)) {
1347                                         torture_comment(tctx, "Verifiction of trust password to %s failed: %s (the trusted domain is not available)\n",
1348                                                t->name, nt_errstr(nt_status));
1349
1350                                         break;
1351                                 }
1352                                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1353                                         torture_comment(tctx, "Verifiction of trust password to %s: should have failed (nologon interdomain trust account), instead: %s\n",
1354                                                t->name, nt_errstr(nt_status));
1355                                         ret = false;
1356                                 }
1357
1358                                 /* break it */
1359                                 nt_hash.hash[0]++;
1360                                 nt_status = test_SamLogon(tctx,
1361                                                           samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1362                                                           t->name,
1363                                                           username,
1364                                                           TEST_WKSTA_MACHINE_NAME,
1365                                                           NULL,
1366                                                           &nt_hash,
1367                                                           NULL);
1368
1369                                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
1370                                         torture_comment(tctx, "Verifiction of trust password to %s: should have failed (wrong password), instead: %s\n",
1371                                                t->name, nt_errstr(nt_status));
1372                                         ret = false;
1373                                 }
1374
1375                                 break;
1376                         }
1377                 }
1378         }
1379         talloc_free(trustdom_ctx);
1380         return ret;
1381 }
1382
1383
1384 /*
1385   try a netlogon DatabaseDeltas
1386 */
1387 static bool test_DatabaseDeltas(struct torture_context *tctx,
1388                                 struct samsync_state *samsync_state, TALLOC_CTX *mem_ctx)
1389 {
1390         TALLOC_CTX *loop_ctx;
1391         struct netr_DatabaseDeltas r;
1392         struct netr_Authenticator credential;
1393         struct netr_Authenticator return_authenticator;
1394         struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1395         const uint32_t database_ids[] = {0, 1, 2};
1396         int i;
1397         bool ret = true;
1398
1399         ZERO_STRUCT(return_authenticator);
1400
1401         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1402         r.in.computername = TEST_MACHINE_NAME;
1403         r.in.credential = &credential;
1404         r.in.preferredmaximumlength = (uint32_t)-1;
1405         r.in.return_authenticator = &return_authenticator;
1406         r.out.return_authenticator = &return_authenticator;
1407         r.out.delta_enum_array = &delta_enum_array;
1408
1409         for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1410
1411                 uint64_t seq_num = samsync_state->seq_num[i];
1412
1413                 r.in.database_id = database_ids[i];
1414                 r.in.sequence_num = &seq_num;
1415                 r.out.sequence_num = &seq_num;
1416
1417                 if (seq_num == 0) continue;
1418
1419                 /* this shows that the bdc doesn't need to do a single call for
1420                  * each seqnumber, and the pdc doesn't need to know about old values
1421                  * -- metze
1422                  */
1423                 seq_num -= 10;
1424
1425                 torture_comment(tctx, "Testing DatabaseDeltas of id %d at %llu\n",
1426                        r.in.database_id, (long long)seq_num);
1427
1428                 do {
1429                         loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseDeltas loop context");
1430                         netlogon_creds_client_authenticator(samsync_state->creds, &credential);
1431
1432                         torture_assert_ntstatus_ok(tctx,
1433                                 dcerpc_netr_DatabaseDeltas_r(samsync_state->b, loop_ctx, &r),
1434                                 "DatabaseDeltas failed");
1435                         if (!NT_STATUS_IS_OK(r.out.result) &&
1436                             !NT_STATUS_EQUAL(r.out.result, STATUS_MORE_ENTRIES) &&
1437                             !NT_STATUS_EQUAL(r.out.result, NT_STATUS_SYNCHRONIZATION_REQUIRED)) {
1438                                 torture_comment(tctx, "DatabaseDeltas - %s\n", nt_errstr(r.out.result));
1439                                 ret = false;
1440                         }
1441
1442                         if (!netlogon_creds_client_check(samsync_state->creds, &return_authenticator.cred)) {
1443                                 torture_comment(tctx, "Credential chaining failed\n");
1444                         }
1445
1446                         seq_num++;
1447                         talloc_free(loop_ctx);
1448                 } while (NT_STATUS_EQUAL(r.out.result, STATUS_MORE_ENTRIES));
1449         }
1450
1451         return ret;
1452 }
1453
1454
1455 /*
1456   try a netlogon DatabaseSync2
1457 */
1458 static bool test_DatabaseSync2(struct torture_context *tctx,
1459                                struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1460                                struct netlogon_creds_CredentialState *creds)
1461 {
1462         TALLOC_CTX *loop_ctx;
1463         struct netr_DatabaseSync2 r;
1464         const uint32_t database_ids[] = {0, 1, 2};
1465         int i;
1466         bool ret = true;
1467         struct netr_Authenticator return_authenticator, credential;
1468         struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1469         struct dcerpc_binding_handle *b = p->binding_handle;
1470
1471         ZERO_STRUCT(return_authenticator);
1472
1473         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1474         r.in.computername = TEST_MACHINE_NAME;
1475         r.in.preferredmaximumlength = (uint32_t)-1;
1476         r.in.return_authenticator = &return_authenticator;
1477         r.out.return_authenticator = &return_authenticator;
1478         r.out.delta_enum_array = &delta_enum_array;
1479
1480         for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1481
1482                 uint32_t sync_context = 0;
1483
1484                 r.in.database_id = database_ids[i];
1485                 r.in.sync_context = &sync_context;
1486                 r.out.sync_context = &sync_context;
1487                 r.in.restart_state = 0;
1488
1489                 torture_comment(tctx, "Testing DatabaseSync2 of id %d\n", r.in.database_id);
1490
1491                 do {
1492                         loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync2 loop context");
1493                         netlogon_creds_client_authenticator(creds, &credential);
1494
1495                         r.in.credential = &credential;
1496
1497                         torture_assert_ntstatus_ok(tctx,
1498                                 dcerpc_netr_DatabaseSync2_r(b, loop_ctx, &r),
1499                                 "DatabaseSync2 failed");
1500                         if (!NT_STATUS_IS_OK(r.out.result) &&
1501                             !NT_STATUS_EQUAL(r.out.result, STATUS_MORE_ENTRIES)) {
1502                                 torture_comment(tctx, "DatabaseSync2 - %s\n", nt_errstr(r.out.result));
1503                                 ret = false;
1504                         }
1505
1506                         if (!netlogon_creds_client_check(creds, &r.out.return_authenticator->cred)) {
1507                                 torture_comment(tctx, "Credential chaining failed\n");
1508                         }
1509
1510                         talloc_free(loop_ctx);
1511                 } while (NT_STATUS_EQUAL(r.out.result, STATUS_MORE_ENTRIES));
1512         }
1513
1514         return ret;
1515 }
1516
1517
1518
1519 bool torture_rpc_samsync(struct torture_context *torture)
1520 {
1521         NTSTATUS status;
1522         TALLOC_CTX *mem_ctx;
1523         bool ret = true;
1524         struct test_join *join_ctx;
1525         struct test_join *join_ctx2;
1526         struct test_join *user_ctx;
1527         const char *machine_password;
1528         const char *wksta_machine_password;
1529         struct dcerpc_binding *b;
1530         struct dcerpc_binding *b_netlogon_wksta;
1531         struct samr_Connect c;
1532         struct samr_SetDomainInfo s;
1533         struct policy_handle *domain_policy;
1534
1535         struct lsa_ObjectAttribute attr;
1536         struct lsa_QosInfo qos;
1537         struct lsa_OpenPolicy2 r;
1538         struct cli_credentials *credentials;
1539         struct cli_credentials *credentials_wksta;
1540
1541         struct samsync_state *samsync_state;
1542
1543         char *test_machine_account;
1544
1545         char *test_wksta_machine_account;
1546
1547         mem_ctx = talloc_init("torture_rpc_netlogon");
1548
1549         test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1550         join_ctx = torture_create_testuser(torture, test_machine_account,
1551                                            lp_workgroup(torture->lp_ctx), ACB_SVRTRUST,
1552                                            &machine_password);
1553         if (!join_ctx) {
1554                 talloc_free(mem_ctx);
1555                 torture_comment(torture, "Failed to join as BDC\n");
1556                 return false;
1557         }
1558
1559         test_wksta_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_WKSTA_MACHINE_NAME);
1560         join_ctx2 = torture_create_testuser(torture, test_wksta_machine_account, lp_workgroup(torture->lp_ctx), ACB_WSTRUST, &wksta_machine_password);
1561         if (!join_ctx2) {
1562                 talloc_free(mem_ctx);
1563                 torture_comment(torture, "Failed to join as member\n");
1564                 return false;
1565         }
1566
1567         user_ctx = torture_create_testuser(torture, TEST_USER_NAME,
1568                                            lp_workgroup(torture->lp_ctx),
1569                                            ACB_NORMAL, NULL);
1570         if (!user_ctx) {
1571                 talloc_free(mem_ctx);
1572                 torture_comment(torture, "Failed to create test account\n");
1573                 return false;
1574         }
1575
1576         samsync_state = talloc_zero(mem_ctx, struct samsync_state);
1577
1578         samsync_state->p_samr = torture_join_samr_pipe(join_ctx);
1579         samsync_state->b_samr = samsync_state->p_samr->binding_handle;
1580         samsync_state->connect_handle = talloc_zero(samsync_state, struct policy_handle);
1581         samsync_state->lsa_handle = talloc_zero(samsync_state, struct policy_handle);
1582         c.in.system_name = NULL;
1583         c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1584         c.out.connect_handle = samsync_state->connect_handle;
1585
1586         torture_assert_ntstatus_ok_goto(torture,
1587                 dcerpc_samr_Connect_r(samsync_state->b_samr, mem_ctx, &c),
1588                 ret, failed,
1589                 "samr_Connect failed");
1590         torture_assert_ntstatus_ok_goto(torture, c.out.result,
1591                 ret, failed,
1592                 "samr_Connect failed");
1593
1594         domain_policy = samsync_open_domain(torture, mem_ctx, samsync_state, lp_workgroup(torture->lp_ctx), NULL);
1595         if (!domain_policy) {
1596                 torture_comment(torture, "samrsync_open_domain failed\n");
1597                 ret = false;
1598                 goto failed;
1599         }
1600
1601         s.in.domain_handle = domain_policy;
1602         s.in.level = 4;
1603         s.in.info = talloc(mem_ctx, union samr_DomainInfo);
1604
1605         s.in.info->oem.oem_information.string
1606                 = talloc_asprintf(mem_ctx,
1607                                   "Tortured by Samba4: %s",
1608                                   timestring(mem_ctx, time(NULL)));
1609         torture_assert_ntstatus_ok_goto(torture,
1610                 dcerpc_samr_SetDomainInfo_r(samsync_state->b_samr, mem_ctx, &s),
1611                 ret, failed,
1612                 "SetDomainInfo failed");
1613
1614         if (!test_samr_handle_Close(samsync_state->b_samr, torture, domain_policy)) {
1615                 ret = false;
1616                 goto failed;
1617         }
1618
1619         torture_assert_ntstatus_ok_goto(torture, s.out.result,
1620                 ret, failed,
1621                 talloc_asprintf(torture, "SetDomainInfo level %u failed", s.in.level));
1622
1623         status = torture_rpc_connection(torture,
1624                                         &samsync_state->p_lsa,
1625                                         &ndr_table_lsarpc);
1626
1627         if (!NT_STATUS_IS_OK(status)) {
1628                 ret = false;
1629                 goto failed;
1630         }
1631         samsync_state->b_lsa = samsync_state->p_lsa->binding_handle;
1632
1633         qos.len = 0;
1634         qos.impersonation_level = 2;
1635         qos.context_mode = 1;
1636         qos.effective_only = 0;
1637
1638         attr.len = 0;
1639         attr.root_dir = NULL;
1640         attr.object_name = NULL;
1641         attr.attributes = 0;
1642         attr.sec_desc = NULL;
1643         attr.sec_qos = &qos;
1644
1645         r.in.system_name = "\\";
1646         r.in.attr = &attr;
1647         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1648         r.out.handle = samsync_state->lsa_handle;
1649
1650         torture_assert_ntstatus_ok_goto(torture,
1651                 dcerpc_lsa_OpenPolicy2_r(samsync_state->b_lsa, mem_ctx, &r),
1652                 ret, failed,
1653                 "OpenPolicy2 failed");
1654         torture_assert_ntstatus_ok_goto(torture, r.out.result,
1655                 ret, failed,
1656                 "OpenPolicy2 failed");
1657
1658         status = torture_rpc_binding(torture, &b);
1659         if (!NT_STATUS_IS_OK(status)) {
1660                 ret = false;
1661                 goto failed;
1662         }
1663
1664         b->flags &= ~DCERPC_AUTH_OPTIONS;
1665         b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1666
1667         credentials = cli_credentials_init(mem_ctx);
1668
1669         cli_credentials_set_workstation(credentials, TEST_MACHINE_NAME, CRED_SPECIFIED);
1670         cli_credentials_set_domain(credentials, lp_workgroup(torture->lp_ctx), CRED_SPECIFIED);
1671         cli_credentials_set_username(credentials, test_machine_account, CRED_SPECIFIED);
1672         cli_credentials_set_password(credentials, machine_password, CRED_SPECIFIED);
1673         cli_credentials_set_secure_channel_type(credentials,
1674                                                 SEC_CHAN_BDC);
1675
1676         status = dcerpc_pipe_connect_b(samsync_state,
1677                                        &samsync_state->p, b,
1678                                            &ndr_table_netlogon,
1679                                        credentials, torture->ev, torture->lp_ctx);
1680
1681         if (!NT_STATUS_IS_OK(status)) {
1682                 torture_comment(torture, "Failed to connect to server as a BDC: %s\n", nt_errstr(status));
1683                 ret = false;
1684                 goto failed;
1685         }
1686         samsync_state->b = samsync_state->p->binding_handle;
1687
1688         status = dcerpc_schannel_creds(samsync_state->p->conn->security_state.generic_state,
1689                                        samsync_state, &samsync_state->creds);
1690         if (!NT_STATUS_IS_OK(status)) {
1691                 ret = false;
1692         }
1693
1694
1695
1696         status = torture_rpc_binding(torture, &b_netlogon_wksta);
1697         if (!NT_STATUS_IS_OK(status)) {
1698                 ret = false;
1699                 goto failed;
1700         }
1701
1702         b_netlogon_wksta->flags &= ~DCERPC_AUTH_OPTIONS;
1703         b_netlogon_wksta->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1704
1705         credentials_wksta = cli_credentials_init(mem_ctx);
1706
1707         cli_credentials_set_workstation(credentials_wksta, TEST_WKSTA_MACHINE_NAME, CRED_SPECIFIED);
1708         cli_credentials_set_domain(credentials_wksta, lp_workgroup(torture->lp_ctx), CRED_SPECIFIED);
1709         cli_credentials_set_username(credentials_wksta, test_wksta_machine_account, CRED_SPECIFIED);
1710         cli_credentials_set_password(credentials_wksta, wksta_machine_password, CRED_SPECIFIED);
1711         cli_credentials_set_secure_channel_type(credentials_wksta,
1712                                                 SEC_CHAN_WKSTA);
1713
1714         status = dcerpc_pipe_connect_b(samsync_state,
1715                                        &samsync_state->p_netlogon_wksta,
1716                                        b_netlogon_wksta,
1717                                            &ndr_table_netlogon,
1718                                        credentials_wksta, torture->ev, torture->lp_ctx);
1719
1720         if (!NT_STATUS_IS_OK(status)) {
1721                 torture_comment(torture, "Failed to connect to server as a Workstation: %s\n", nt_errstr(status));
1722                 ret = false;
1723                 goto failed;
1724         }
1725
1726         status = dcerpc_schannel_creds(samsync_state->p_netlogon_wksta->conn->security_state.generic_state,
1727                                        samsync_state, &samsync_state->creds_netlogon_wksta);
1728         if (!NT_STATUS_IS_OK(status)) {
1729                 torture_comment(torture, "Failed to obtail schanel creds!\n");
1730                 ret = false;
1731         }
1732
1733         if (!test_DatabaseSync(torture, samsync_state, mem_ctx)) {
1734                 torture_comment(torture, "DatabaseSync failed\n");
1735                 ret = false;
1736         }
1737
1738         if (!test_DatabaseDeltas(torture, samsync_state, mem_ctx)) {
1739                 torture_comment(torture, "DatabaseDeltas failed\n");
1740                 ret = false;
1741         }
1742
1743         if (!test_DatabaseSync2(torture, samsync_state->p, mem_ctx, samsync_state->creds)) {
1744                 torture_comment(torture, "DatabaseSync2 failed\n");
1745                 ret = false;
1746         }
1747 failed:
1748
1749         torture_leave_domain(torture, join_ctx);
1750         torture_leave_domain(torture, join_ctx2);
1751         torture_leave_domain(torture, user_ctx);
1752
1753         talloc_free(mem_ctx);
1754
1755         return ret;
1756 }