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