r4682: A LDB-based secrets implementation in Samba4.
[ira/wip.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                 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
782                 if (!NT_STATUS_IS_OK(status)) {
783                         return False;
784                 }
785
786                 if (!q.out.old_mtime) {
787                         printf("OLD mtime not available on LSA for secret %s\n", old->name);
788                         ret = False;
789                 }
790                 if (old->mtime != *q.out.old_mtime) {
791                         printf("OLD mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n", 
792                                old->name, nt_time_string(mem_ctx, old->mtime), 
793                                nt_time_string(mem_ctx, *q.out.old_mtime)); 
794                         ret = False;
795                 }
796
797                 if (old->secret.length != lsa_blob_out.length) {
798                         printf("Returned secret %s doesn't match: %d != %d\n",
799                                old->name, old->secret.length, lsa_blob_out.length);
800                         ret = False;
801                 } else if (memcmp(lsa_blob_out.data, 
802                            old->secret.data, old->secret.length) != 0) {
803                         printf("Returned secret %s doesn't match: \n",
804                                old->name);
805                         DEBUG(1, ("SamSync Secret:\n"));
806                         dump_data(1, old->secret.data, old->secret.length);
807                         DEBUG(1, ("LSA Secret:\n"));
808                         dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
809                         ret = False;
810                 }
811
812         }
813
814         if (q.out.new_val->buf == NULL) {
815                 /* probably just not available due to ACLs */
816         } else {
817                 lsa_blob1.data = q.out.new_val->buf->data;
818                 lsa_blob1.length = q.out.new_val->buf->length;
819
820                 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
821                 if (!NT_STATUS_IS_OK(status)) {
822                         return False;
823                 }
824                 
825                 if (!q.out.new_mtime) {
826                         printf("NEW mtime not available on LSA for secret %s\n", new->name);
827                         ret = False;
828                 }
829                 if (new->mtime != *q.out.new_mtime) {
830                         printf("NEW mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n", 
831                                new->name, nt_time_string(mem_ctx, new->mtime), 
832                                nt_time_string(mem_ctx, *q.out.new_mtime)); 
833                         ret = False;
834                 }
835
836                 if (new->secret.length != lsa_blob_out.length) {
837                         printf("Returned secret %s doesn't match: %d != %d\n",
838                                new->name, new->secret.length, lsa_blob_out.length);
839                         ret = False;
840                 } else if (memcmp(lsa_blob_out.data, 
841                            new->secret.data, new->secret.length) != 0) {
842                         printf("Returned secret %s doesn't match: \n",
843                                new->name);
844                         DEBUG(1, ("SamSync Secret:\n"));
845                         dump_data(1, new->secret.data, new->secret.length);
846                         DEBUG(1, ("LSA Secret:\n"));
847                         dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
848                         ret = False;
849                 }
850         }
851
852         return ret;
853 }
854
855 static BOOL samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
856                                           int database_id, struct netr_DELTA_ENUM *delta) 
857 {
858         NTSTATUS status;
859         BOOL ret = True;
860         struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
861         struct dom_sid *dom_sid = delta->delta_id_union.sid;
862
863         struct samsync_trusted_domain *new = talloc_p(samsync_state, struct samsync_trusted_domain);
864         struct lsa_OpenTrustedDomain t;
865         struct policy_handle trustdom_handle;
866         struct lsa_QueryTrustedDomainInfo q;
867         union lsa_TrustedDomainInfo *info[9];
868         int levels [] = {1, 3, 8};
869         int i;
870
871         new->name = talloc_reference(new, trusted_domain->domain_name.string);
872         new->sid = talloc_reference(new, dom_sid);
873
874         t.in.handle = samsync_state->lsa_handle;
875         t.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
876         t.in.sid = dom_sid;
877         t.out.trustdom_handle = &trustdom_handle;
878
879         status = dcerpc_lsa_OpenTrustedDomain(samsync_state->p_lsa, mem_ctx, &t);
880         if (!NT_STATUS_IS_OK(status)) {
881                 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
882                 return False;
883         }
884         
885         for (i=0; i< ARRAY_SIZE(levels); i++) {
886                 q.in.trustdom_handle = &trustdom_handle;
887                 q.in.level = levels[i];
888                 status = dcerpc_lsa_QueryTrustedDomainInfo(samsync_state->p_lsa, mem_ctx, &q);
889                 if (!NT_STATUS_IS_OK(status)) {
890                         if (q.in.level == 8 && NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER)) {
891                                 info[levels[i]] = NULL;
892                                 continue;
893                         }
894                         printf("QueryInfoTrustedDomain level %d failed - %s\n", 
895                                levels[i], nt_errstr(status));
896                         return False;
897                 }
898                 info[levels[i]]  = q.out.info;
899         }
900
901         if (info[8]) {
902                 TEST_SID_EQUAL(info[8]->full_info.info_ex.sid, dom_sid);
903                 TEST_STRING_EQUAL(info[8]->full_info.info_ex.netbios_name, trusted_domain->domain_name);
904         }
905         TEST_STRING_EQUAL(info[1]->name.netbios_name, trusted_domain->domain_name);
906         TEST_INT_EQUAL(info[3]->flags.flags, trusted_domain->flags);
907 /*
908   We would like to do this, but it is NOT_SUPPORTED on win2k3
909         TEST_SEC_DESC_EQUAL(trusted_domain->sdbuf, lsa, &trustdom_handle);
910 */
911         DLIST_ADD(samsync_state->trusted_domains, new);
912
913         return ret;
914 }
915
916 static BOOL samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
917                                           int database_id, struct netr_DELTA_ENUM *delta) 
918 {
919         NTSTATUS status;
920         BOOL ret = True;
921         struct netr_DELTA_ACCOUNT *account = delta->delta_union.account;
922         struct dom_sid *dom_sid = delta->delta_id_union.sid;
923
924         struct lsa_OpenAccount a;
925         struct policy_handle acct_handle;
926         struct lsa_EnumPrivsAccount e;
927         struct lsa_LookupPrivName r;
928
929         int i, j;
930
931         BOOL *found_priv_in_lsa;
932
933         a.in.handle = samsync_state->lsa_handle;
934         a.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
935         a.in.sid = dom_sid;
936         a.out.acct_handle = &acct_handle;
937
938         status = dcerpc_lsa_OpenAccount(samsync_state->p_lsa, mem_ctx, &a);
939         if (!NT_STATUS_IS_OK(status)) {
940                 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
941                 return False;
942         }
943
944         TEST_SEC_DESC_EQUAL(account->sdbuf, lsa, &acct_handle);
945
946         found_priv_in_lsa = talloc_zero_array(mem_ctx, BOOL, account->privilege_entries);
947
948         e.in.handle = &acct_handle;
949
950         status = dcerpc_lsa_EnumPrivsAccount(samsync_state->p_lsa, mem_ctx, &e);
951         if (!NT_STATUS_IS_OK(status)) {
952                 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
953                 return False;
954         }
955
956         if (!e.out.privs) {
957                 return account->privilege_entries != 0;
958         }
959
960         if ((account->privilege_entries && !e.out.privs)) {
961                 return False;
962         }
963
964         TEST_INT_EQUAL(account->privilege_entries, e.out.privs->count);
965         
966         for (i=0;i< e.out.privs->count; i++) {
967                 r.in.handle = samsync_state->lsa_handle;
968                 r.in.luid = &e.out.privs->set[i].luid;
969                 
970                 status = dcerpc_lsa_LookupPrivName(samsync_state->p_lsa, mem_ctx, &r);
971                 if (!NT_STATUS_IS_OK(status)) {
972                         printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
973                         return False;
974                 }
975                 
976                 if (!r.out.name) {
977                         printf("\nLookupPrivName failed to return a name\n");
978                         return False;
979                 }
980                 for (j=0;j<account->privilege_entries; j++) {
981                         if (strcmp(r.out.name->string, account->privilege_name[j].string) == 0) {
982                                 found_priv_in_lsa[j] = True;
983                                 break;
984                         }
985                 }
986         }
987         for (j=0;j<account->privilege_entries; j++) {
988                 if (!found_priv_in_lsa[j]) {
989                         printf("Privilage %s on account %s not found in LSA\n", account->privilege_name[j].string, 
990                                dom_sid_string(mem_ctx, dom_sid));
991                         ret = False;
992                 }
993         }
994         return ret;
995 }
996
997 /*
998   try a netlogon DatabaseSync
999 */
1000 static BOOL test_DatabaseSync(struct samsync_state *samsync_state,
1001                               TALLOC_CTX *mem_ctx)
1002 {
1003         NTSTATUS status;
1004         struct netr_DatabaseSync r;
1005         const uint32_t database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS}; 
1006         int i, d;
1007         BOOL ret = True;
1008         struct samsync_trusted_domain *t;
1009         struct samsync_secret *s;
1010         
1011         const char *domain, *username;
1012
1013         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1014         r.in.computername = TEST_MACHINE_NAME;
1015         r.in.preferredmaximumlength = (uint32_t)-1;
1016         ZERO_STRUCT(r.in.return_authenticator);
1017
1018         for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1019                 r.in.sync_context = 0;
1020                 r.in.database_id = database_ids[i];
1021
1022                 printf("Testing DatabaseSync of id %d\n", r.in.database_id);
1023
1024                 do {
1025                         creds_client_authenticator(samsync_state->creds, &r.in.credential);
1026
1027                         status = dcerpc_netr_DatabaseSync(samsync_state->p, mem_ctx, &r);
1028                         if (!NT_STATUS_IS_OK(status) &&
1029                             !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1030                                 printf("DatabaseSync - %s\n", nt_errstr(status));
1031                                 ret = False;
1032                                 break;
1033                         }
1034
1035                         if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1036                                 printf("Credential chaining failed\n");
1037                         }
1038
1039                         r.in.sync_context = r.out.sync_context;
1040
1041                         for (d=0; d < r.out.delta_enum_array->num_deltas; d++) {
1042                                 switch (r.out.delta_enum_array->delta_enum[d].delta_type) {
1043                                 case NETR_DELTA_DOMAIN:
1044                                         ret &= samsync_handle_domain(mem_ctx, samsync_state, 
1045                                                                      r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
1046                                         break;
1047                                 case NETR_DELTA_GROUP:
1048                                         ret &= samsync_handle_group(mem_ctx, samsync_state, 
1049                                                                     r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
1050                                         break;
1051                                 case NETR_DELTA_USER:
1052                                         ret &= samsync_handle_user(mem_ctx, samsync_state, 
1053                                                                    r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
1054                                         break;
1055                                 case NETR_DELTA_ALIAS:
1056                                         ret &= samsync_handle_alias(mem_ctx, samsync_state, 
1057                                                                     r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
1058                                         break;
1059                                 case NETR_DELTA_POLICY:
1060                                         ret &= samsync_handle_policy(mem_ctx, samsync_state, 
1061                                                                      r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
1062                                         break;
1063                                 case NETR_DELTA_TRUSTED_DOMAIN:
1064                                         ret &= samsync_handle_trusted_domain(mem_ctx, samsync_state, 
1065                                                                              r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
1066                                         break;
1067                                 case NETR_DELTA_ACCOUNT:
1068                                         ret &= samsync_handle_account(mem_ctx, samsync_state, 
1069                                                                      r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
1070                                         break;
1071                                 case NETR_DELTA_SECRET:
1072                                         ret &= samsync_handle_secret(mem_ctx, samsync_state, 
1073                                                                      r.in.database_id, &r.out.delta_enum_array->delta_enum[d]);
1074                                         break;
1075                                 }
1076                         }
1077                 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1078                 
1079         }
1080
1081         domain = samsync_state->domain_name[SAM_DATABASE_DOMAIN];
1082         if (!domain) {
1083                 printf("Never got a DOMAIN object in samsync!\n");
1084                 return False;
1085         }
1086         
1087         username = talloc_asprintf(mem_ctx, "%s$", domain);
1088         for (t=samsync_state->trusted_domains; t; t=t->next) {
1089                 char *secret_name = talloc_asprintf(mem_ctx, "G$$%s", t->name);
1090                 for (s=samsync_state->secrets; s; s=s->next) {
1091                         if (StrCaseCmp(s->name, secret_name) == 0) {
1092                                 NTSTATUS nt_status;
1093                                 struct samr_Password nt_hash;
1094                                 mdfour(nt_hash.hash, s->secret.data, s->secret.length);
1095                                 
1096                                 printf("Checking password for %s\\%s\n", t->name, username);
1097                                 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta, 
1098                                                           t->name,
1099                                                           username, 
1100                                                           TEST_MACHINE_NAME2,
1101                                                           NULL, 
1102                                                           &nt_hash,
1103                                                           NULL);
1104                                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS)) {
1105                                         printf("Verifiction of trust password to %s failed: %s (the trusted domain is not available)\n", 
1106                                                t->name, nt_errstr(nt_status));
1107                                         
1108                                         break;
1109                                 }
1110                                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1111                                         printf("Verifiction of trust password to %s: should have failed (nologon interdomain trust account), instead: %s\n", 
1112                                                t->name, nt_errstr(nt_status));
1113                                         ret = False;
1114                                 }
1115                                 
1116                                 /* break it */
1117                                 nt_hash.hash[0]++;
1118                                 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta, 
1119                                                           t->name,
1120                                                           username, 
1121                                                           TEST_MACHINE_NAME2,
1122                                                           NULL,
1123                                                           &nt_hash,
1124                                                           NULL);
1125                                 
1126                                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
1127                                         printf("Verifiction of trust password to %s: should have failed (wrong password), instead: %s\n", 
1128                                                t->name, nt_errstr(nt_status));
1129                                         ret = False;
1130                                         ret = False;
1131                                 }
1132                                 
1133                                 break;
1134                         }
1135                 }
1136         }
1137         return ret;
1138 }
1139
1140
1141 /*
1142   try a netlogon DatabaseDeltas
1143 */
1144 static BOOL test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX *mem_ctx)
1145 {
1146         NTSTATUS status;
1147         struct netr_DatabaseDeltas r;
1148         const uint32_t database_ids[] = {0, 1, 2}; 
1149         int i;
1150         BOOL ret = True;
1151
1152         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1153         r.in.computername = TEST_MACHINE_NAME;
1154         r.in.preferredmaximumlength = (uint32_t)-1;
1155         ZERO_STRUCT(r.in.return_authenticator);
1156
1157         for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1158                 r.in.database_id = database_ids[i];
1159                 r.in.sequence_num = samsync_state->seq_num[i];
1160
1161                 if (r.in.sequence_num == 0) continue;
1162
1163                 r.in.sequence_num -= 1;
1164
1165
1166                 printf("Testing DatabaseDeltas of id %d at %llu\n", 
1167                        r.in.database_id, r.in.sequence_num);
1168
1169                 do {
1170                         creds_client_authenticator(samsync_state->creds, &r.in.credential);
1171
1172                         status = dcerpc_netr_DatabaseDeltas(samsync_state->p, mem_ctx, &r);
1173                         if (!NT_STATUS_IS_OK(status) &&
1174                             !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) &&
1175                             !NT_STATUS_EQUAL(status, NT_STATUS_SYNCHRONIZATION_REQUIRED)) {
1176                                 printf("DatabaseDeltas - %s\n", nt_errstr(status));
1177                                 ret = False;
1178                                 break;
1179                         }
1180
1181                         if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1182                                 printf("Credential chaining failed\n");
1183                         }
1184
1185                         r.in.sequence_num++;
1186                 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1187         }
1188
1189         return ret;
1190 }
1191
1192
1193 /*
1194   try a netlogon DatabaseSync2
1195 */
1196 static BOOL test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
1197                                struct creds_CredentialState *creds)
1198 {
1199         NTSTATUS status;
1200         struct netr_DatabaseSync2 r;
1201         const uint32_t database_ids[] = {0, 1, 2}; 
1202         int i;
1203         BOOL ret = True;
1204
1205         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1206         r.in.computername = TEST_MACHINE_NAME;
1207         r.in.preferredmaximumlength = (uint32_t)-1;
1208         ZERO_STRUCT(r.in.return_authenticator);
1209
1210         for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1211                 r.in.sync_context = 0;
1212                 r.in.database_id = database_ids[i];
1213                 r.in.restart_state = 0;
1214
1215                 printf("Testing DatabaseSync2 of id %d\n", r.in.database_id);
1216
1217                 do {
1218                         creds_client_authenticator(creds, &r.in.credential);
1219
1220                         status = dcerpc_netr_DatabaseSync2(p, mem_ctx, &r);
1221                         if (!NT_STATUS_IS_OK(status) &&
1222                             !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1223                                 printf("DatabaseSync2 - %s\n", nt_errstr(status));
1224                                 ret = False;
1225                                 break;
1226                         }
1227
1228                         if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
1229                                 printf("Credential chaining failed\n");
1230                         }
1231
1232                         r.in.sync_context = r.out.sync_context;
1233                 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1234         }
1235
1236         return ret;
1237 }
1238
1239
1240
1241 BOOL torture_rpc_samsync(void)
1242 {
1243         NTSTATUS status;
1244         TALLOC_CTX *mem_ctx;
1245         BOOL ret = True;
1246         struct test_join *join_ctx;
1247         struct test_join *join_ctx2;
1248         const char *machine_password;
1249         const char *machine_password2;
1250         const char *binding = lp_parm_string(-1, "torture", "binding");
1251         struct dcerpc_binding b;
1252         struct dcerpc_binding b_netlogon_wksta;
1253         struct samr_Connect c;
1254         struct samr_SetDomainInfo s;
1255         struct policy_handle *domain_policy;
1256
1257         struct lsa_ObjectAttribute attr;
1258         struct lsa_QosInfo qos;
1259         struct lsa_OpenPolicy2 r;
1260
1261         struct samsync_state *samsync_state;
1262
1263         mem_ctx = talloc_init("torture_rpc_netlogon");
1264
1265         join_ctx = torture_join_domain(TEST_MACHINE_NAME, lp_workgroup(), ACB_SVRTRUST, 
1266                                        &machine_password);
1267         if (!join_ctx) {
1268                 printf("Failed to join as BDC\n");
1269                 return False;
1270         }
1271         
1272         join_ctx2 = torture_join_domain(TEST_MACHINE_NAME2, lp_workgroup(), ACB_WSTRUST, 
1273                                        &machine_password2);
1274         if (!join_ctx2) {
1275                 printf("Failed to join as member\n");
1276                 return False;
1277         }
1278         
1279         samsync_state = talloc_zero_p(mem_ctx, struct samsync_state);
1280
1281         samsync_state->p_samr = torture_join_samr_pipe(join_ctx);
1282         samsync_state->connect_handle = talloc_zero_p(samsync_state, struct policy_handle);
1283         samsync_state->lsa_handle = talloc_zero_p(samsync_state, struct policy_handle);
1284         c.in.system_name = NULL;
1285         c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1286         c.out.connect_handle = samsync_state->connect_handle;
1287
1288         status = dcerpc_samr_Connect(samsync_state->p_samr, mem_ctx, &c);
1289         if (!NT_STATUS_IS_OK(status)) {
1290                 printf("samr_Connect failed\n");
1291                 ret = False;
1292                 goto failed;
1293         }
1294
1295         domain_policy = samsync_open_domain(mem_ctx, samsync_state, lp_workgroup(), NULL);
1296         if (!domain_policy) {
1297                 printf("samrsync_open_domain failed\n");
1298                 ret = False;
1299                 goto failed;
1300         }
1301         
1302         s.in.domain_handle = domain_policy;
1303         s.in.level = 4;
1304         s.in.info = talloc_p(mem_ctx, union samr_DomainInfo);
1305         
1306         s.in.info->info4.comment.string
1307                 = talloc_asprintf(mem_ctx, 
1308                                   "Tortured by Samba4: %s", 
1309                                   timestring(mem_ctx, time(NULL)));
1310         status = dcerpc_samr_SetDomainInfo(samsync_state->p_samr, mem_ctx, &s);
1311
1312         if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, domain_policy)) {
1313                 ret = False;
1314                 goto failed;
1315         }
1316
1317         if (!NT_STATUS_IS_OK(status)) {
1318                 printf("SetDomainInfo level %u failed - %s\n", 
1319                        s.in.level, nt_errstr(status));
1320                 ret = False;
1321                 goto failed;
1322         }
1323         
1324
1325         status = torture_rpc_connection(&samsync_state->p_lsa, 
1326                                         DCERPC_LSARPC_NAME,
1327                                         DCERPC_LSARPC_UUID,
1328                                         DCERPC_LSARPC_VERSION);
1329
1330         if (!NT_STATUS_IS_OK(status)) {
1331                 ret = False;
1332                 goto failed;
1333         }
1334
1335         qos.len = 0;
1336         qos.impersonation_level = 2;
1337         qos.context_mode = 1;
1338         qos.effective_only = 0;
1339
1340         attr.len = 0;
1341         attr.root_dir = NULL;
1342         attr.object_name = NULL;
1343         attr.attributes = 0;
1344         attr.sec_desc = NULL;
1345         attr.sec_qos = &qos;
1346
1347         r.in.system_name = "\\";
1348         r.in.attr = &attr;
1349         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1350         r.out.handle = samsync_state->lsa_handle;
1351
1352         status = dcerpc_lsa_OpenPolicy2(samsync_state->p_lsa, mem_ctx, &r);
1353         if (!NT_STATUS_IS_OK(status)) {
1354                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1355                 ret = False;
1356                 goto failed;
1357         }
1358
1359         status = dcerpc_parse_binding(mem_ctx, binding, &b);
1360         if (!NT_STATUS_IS_OK(status)) {
1361                 printf("Bad binding string %s\n", binding);
1362                 ret = False;
1363                 goto failed;
1364         }
1365
1366         b.flags &= ~DCERPC_AUTH_OPTIONS;
1367         b.flags |= DCERPC_SCHANNEL_BDC | DCERPC_SIGN;
1368
1369         status = dcerpc_pipe_connect_b(&samsync_state->p, &b, 
1370                                        DCERPC_NETLOGON_UUID,
1371                                        DCERPC_NETLOGON_VERSION,
1372                                        lp_workgroup(), 
1373                                        TEST_MACHINE_NAME,
1374                                        machine_password);
1375
1376         if (!NT_STATUS_IS_OK(status)) {
1377                 ret = False;
1378                 goto failed;
1379         }
1380
1381         status = dcerpc_schannel_creds(samsync_state->p->conn->security_state.generic_state, mem_ctx, &samsync_state->creds);
1382         if (!NT_STATUS_IS_OK(status)) {
1383                 ret = False;
1384         }
1385
1386
1387
1388         status = dcerpc_parse_binding(mem_ctx, binding, &b_netlogon_wksta);
1389         if (!NT_STATUS_IS_OK(status)) {
1390                 printf("Bad binding string %s\n", binding);
1391                 ret = False;
1392                 goto failed;
1393         }
1394
1395         b_netlogon_wksta.flags &= ~DCERPC_AUTH_OPTIONS;
1396         b_netlogon_wksta.flags |= DCERPC_SCHANNEL_WORKSTATION | DCERPC_SIGN;
1397
1398         status = dcerpc_pipe_connect_b(&samsync_state->p_netlogon_wksta, &b_netlogon_wksta, 
1399                                        DCERPC_NETLOGON_UUID,
1400                                        DCERPC_NETLOGON_VERSION,
1401                                        lp_workgroup(), 
1402                                        TEST_MACHINE_NAME2,
1403                                        machine_password2);
1404
1405         if (!NT_STATUS_IS_OK(status)) {
1406                 ret = False;
1407                 goto failed;
1408         }
1409
1410         status = dcerpc_schannel_creds(samsync_state->p_netlogon_wksta->conn->security_state.generic_state, 
1411                                        mem_ctx, &samsync_state->creds_netlogon_wksta);
1412         if (!NT_STATUS_IS_OK(status)) {
1413                 ret = False;
1414         }
1415
1416         if (!test_DatabaseSync(samsync_state, mem_ctx)) {
1417                 ret = False;
1418         }
1419
1420         if (!test_DatabaseDeltas(samsync_state, mem_ctx)) {
1421                 ret = False;
1422         }
1423
1424         if (!test_DatabaseSync2(samsync_state->p, mem_ctx, samsync_state->creds)) {
1425                 ret = False;
1426         }
1427 failed:
1428         torture_rpc_close(samsync_state->p);
1429
1430         torture_leave_domain(join_ctx);
1431         torture_leave_domain(join_ctx2);
1432
1433         talloc_destroy(mem_ctx);
1434
1435         return ret;
1436 }