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