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