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