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