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