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