r5037: got rid of all of the TALLOC_DEPRECATED stuff. My apologies for the
[kai/samba.git] / source4 / torture / rpc / samsync.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test suite for netlogon rpc operations
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8    Copyright (C) Tim Potter      2003
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "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(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                 printf("samr_handle_Close failed - %s\n", 
432                        nt_errstr(nt_status));
433                 return False;
434         }
435
436         if (!NT_STATUS_IS_OK(nt_status)) {
437                 printf("QueryUserInfo level %u failed - %s\n", 
438                        q.in.level, nt_errstr(nt_status));
439                 return False;
440         }
441
442         TEST_STRING_EQUAL(q.out.info->info21.account_name, user->account_name);
443         TEST_STRING_EQUAL(q.out.info->info21.full_name, user->full_name);
444         TEST_INT_EQUAL(q.out.info->info21.rid, user->rid);
445         TEST_INT_EQUAL(q.out.info->info21.primary_gid, user->primary_gid);
446         TEST_STRING_EQUAL(q.out.info->info21.home_directory, user->home_directory);
447         TEST_STRING_EQUAL(q.out.info->info21.home_drive, user->home_drive);
448         TEST_STRING_EQUAL(q.out.info->info21.logon_script, user->logon_script);
449         TEST_STRING_EQUAL(q.out.info->info21.description, user->description);
450         TEST_STRING_EQUAL(q.out.info->info21.workstations, user->workstations);
451
452         TEST_TIME_EQUAL(q.out.info->info21.last_logon, user->last_logon);
453         TEST_TIME_EQUAL(q.out.info->info21.last_logoff, user->last_logoff);
454
455
456         TEST_INT_EQUAL(q.out.info->info21.logon_hours.units_per_week, 
457                        user->logon_hours.units_per_week);
458         if (ret) {
459                 if (memcmp(q.out.info->info21.logon_hours.bits, user->logon_hours.bits, 
460                            q.out.info->info21.logon_hours.units_per_week/8) != 0) {
461                         printf("Logon hours mismatch\n");
462                         ret = False;
463                 }
464         }
465
466         TEST_INT_EQUAL(q.out.info->info21.bad_password_count,
467                        user->bad_password_count);
468         TEST_INT_EQUAL(q.out.info->info21.logon_count,
469                        user->logon_count);
470
471         TEST_TIME_EQUAL(q.out.info->info21.last_password_change,
472                        user->last_password_change);
473         TEST_TIME_EQUAL(q.out.info->info21.acct_expiry,
474                        user->acct_expiry);
475
476         TEST_INT_EQUAL(q.out.info->info21.acct_flags, user->acct_flags);
477         TEST_INT_EQUAL(q.out.info->info21.nt_password_set, user->nt_password_present);
478         TEST_INT_EQUAL(q.out.info->info21.lm_password_set, user->lm_password_present);
479         TEST_INT_EQUAL(q.out.info->info21.password_expired, user->password_expired);
480
481         TEST_STRING_EQUAL(q.out.info->info21.comment, user->comment);
482         TEST_STRING_EQUAL(q.out.info->info21.parameters, user->parameters);
483
484         TEST_INT_EQUAL(q.out.info->info21.country_code, user->country_code);
485         TEST_INT_EQUAL(q.out.info->info21.code_page, user->code_page);
486
487         TEST_STRING_EQUAL(q.out.info->info21.profile_path, user->profile_path);
488
489         if (user->lm_password_present) {
490                 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
491                 lm_hash_p = &lm_hash;
492         }
493         if (user->nt_password_present) {
494                 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
495                 nt_hash_p = &nt_hash;
496         }
497
498         if (user->user_private_info.SensitiveData) {
499                 DATA_BLOB data;
500                 struct netr_USER_KEYS keys;
501                 data.data = user->user_private_info.SensitiveData;
502                 data.length = user->user_private_info.DataLength;
503                 creds_arcfour_crypt(samsync_state->creds, data.data, data.length);
504                 nt_status = ndr_pull_struct_blob(&data, mem_ctx, &keys, (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
505                 if (NT_STATUS_IS_OK(nt_status)) {
506                         if (keys.keys.keys2.lmpassword.length == 16) {
507                                 sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
508                                 lm_hash_p = &lm_hash;
509                         }
510                         if (keys.keys.keys2.ntpassword.length == 16) {
511                                 sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
512                                 nt_hash_p = &nt_hash;
513                         }
514                 } else {
515                         printf("Failed to parse Sensitive Data for %s:\n", username);
516 #if 0
517                         dump_data(0, data.data, data.length);
518 #endif
519                         return False;
520                 }
521         }
522
523         nt_status = test_SamLogon(samsync_state->p, mem_ctx, samsync_state->creds, 
524                                   domain,
525                                   username, 
526                                   TEST_MACHINE_NAME,
527                                   lm_hash_p,
528                                   nt_hash_p,
529                                   &info3);
530
531         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED)) {
532                 if (user->acct_flags & ACB_DISABLED) {
533                         return True;
534                 }
535         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)) {
536                 if (user->acct_flags & ACB_WSTRUST) {
537                         return True;
538                 }
539         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT)) {
540                 if (user->acct_flags & ACB_SVRTRUST) {
541                         return True;
542                 }
543         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
544                 if (user->acct_flags & ACB_DOMTRUST) {
545                         return True;
546                 }
547         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
548                 if (user->acct_flags & ACB_DOMTRUST) {
549                         return True;
550                 }
551         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT)) {
552                 if (user->acct_flags & ACB_AUTOLOCK) {
553                         return True;
554                 }
555         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
556                 if (!lm_hash_p && !nt_hash_p) {
557                         return True;
558                 }
559         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE)) {
560                 /* We would need to know the server's current time to test this properly */
561                 return True;
562         } else if (NT_STATUS_IS_OK(nt_status)) {
563                 TEST_INT_EQUAL(user->rid, info3->base.rid);
564                 TEST_INT_EQUAL(user->primary_gid, info3->base.primary_gid);
565                 /* this is 0x0 from NT4 sp6 */
566                 if (info3->base.acct_flags) {
567                         TEST_INT_EQUAL(user->acct_flags, info3->base.acct_flags);
568                 }
569                 /* this is NULL from NT4 sp6 */
570                 if (info3->base.account_name.string) {
571                         TEST_STRING_EQUAL(user->account_name, info3->base.account_name);
572                 }
573                 TEST_STRING_EQUAL(user->full_name, info3->base.full_name);
574                 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
575                 TEST_STRING_EQUAL(user->profile_path, info3->base.profile_path);
576                 TEST_STRING_EQUAL(user->home_directory, info3->base.home_directory);
577                 TEST_STRING_EQUAL(user->home_drive, info3->base.home_drive);
578                 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
579
580
581                 TEST_TIME_EQUAL(user->last_logon, info3->base.last_logon);
582                 TEST_TIME_EQUAL(user->acct_expiry, info3->base.acct_expiry);
583                 TEST_TIME_EQUAL(user->last_password_change, info3->base.last_password_change);
584
585                 /* Does the concept of a logoff time ever really
586                  * exist? (not in any sensible way, according to the
587                  * doco I read -- abartlet) */
588
589                 /* This copes with the two different versions of 0 I see */
590                 /* with NT4 sp6 we have the || case */
591                 if (!((user->last_logoff == 0) 
592                       || (info3->base.last_logoff == 0x7fffffffffffffffLL))) {
593                         TEST_TIME_EQUAL(user->last_logoff, info3->base.last_logoff);
594                 }
595                 return ret;
596         } else {
597                 printf("Could not validate password for user %s\\%s: %s\n",
598                        domain, username, nt_errstr(nt_status));
599                 return False;
600         } 
601         return False;
602 }
603
604 static BOOL samsync_handle_alias(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
605                                  int database_id, struct netr_DELTA_ENUM *delta) 
606 {
607         uint32 rid = delta->delta_id_union.rid;
608         struct netr_DELTA_ALIAS *alias = delta->delta_union.alias;
609         NTSTATUS nt_status;
610         BOOL ret = True;
611
612         struct samr_OpenAlias r;
613         struct samr_QueryAliasInfo q;
614         struct policy_handle alias_handle;
615
616         if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
617                 printf("SamSync needs domain information before the users\n");
618                 return False;
619         }
620
621         r.in.domain_handle = samsync_state->domain_handle[database_id];
622         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
623         r.in.rid = rid;
624         r.out.alias_handle = &alias_handle;
625
626         nt_status = dcerpc_samr_OpenAlias(samsync_state->p_samr, mem_ctx, &r);
627         if (!NT_STATUS_IS_OK(nt_status)) {
628                 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
629                 return False;
630         }
631
632         q.in.alias_handle = &alias_handle;
633         q.in.level = 1;
634
635         TEST_SEC_DESC_EQUAL(alias->sdbuf, samr, &alias_handle);
636
637         nt_status = dcerpc_samr_QueryAliasInfo(samsync_state->p_samr, mem_ctx, &q);
638         if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &alias_handle)) {
639                 return False;
640         }
641
642         if (!NT_STATUS_IS_OK(nt_status)) {
643                 printf("QueryAliasInfo level %u failed - %s\n", 
644                        q.in.level, nt_errstr(nt_status));
645                 return False;
646         }
647
648         TEST_STRING_EQUAL(q.out.info->all.name, alias->alias_name);
649         TEST_STRING_EQUAL(q.out.info->all.description, alias->description);
650         return ret;
651 }
652
653 static BOOL samsync_handle_group(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
654                                  int database_id, struct netr_DELTA_ENUM *delta) 
655 {
656         uint32 rid = delta->delta_id_union.rid;
657         struct netr_DELTA_GROUP *group = delta->delta_union.group;
658         NTSTATUS nt_status;
659         BOOL ret = True;
660
661         struct samr_OpenGroup r;
662         struct samr_QueryGroupInfo q;
663         struct policy_handle group_handle;
664
665         if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
666                 printf("SamSync needs domain information before the users\n");
667                 return False;
668         }
669
670         r.in.domain_handle = samsync_state->domain_handle[database_id];
671         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
672         r.in.rid = rid;
673         r.out.group_handle = &group_handle;
674
675         nt_status = dcerpc_samr_OpenGroup(samsync_state->p_samr, mem_ctx, &r);
676         if (!NT_STATUS_IS_OK(nt_status)) {
677                 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
678                 return False;
679         }
680
681         q.in.group_handle = &group_handle;
682         q.in.level = 1;
683
684         TEST_SEC_DESC_EQUAL(group->sdbuf, samr, &group_handle);
685
686         nt_status = dcerpc_samr_QueryGroupInfo(samsync_state->p_samr, mem_ctx, &q);
687         if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &group_handle)) {
688                 return False;
689         }
690
691         if (!NT_STATUS_IS_OK(nt_status)) {
692                 printf("QueryGroupInfo level %u failed - %s\n", 
693                        q.in.level, nt_errstr(nt_status));
694                 return False;
695         }
696
697         TEST_STRING_EQUAL(q.out.info->all.name, group->group_name);
698         TEST_INT_EQUAL(q.out.info->all.attributes, group->attributes);
699         TEST_STRING_EQUAL(q.out.info->all.description, group->description);
700         return ret;
701 }
702
703 static BOOL samsync_handle_secret(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
704                                   int database_id, struct netr_DELTA_ENUM *delta) 
705 {
706         struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
707         const char *name = delta->delta_id_union.name;
708         struct samsync_secret *new = talloc(samsync_state, struct samsync_secret);
709         struct samsync_secret *old = talloc(mem_ctx, struct samsync_secret);
710         struct lsa_QuerySecret q;
711         struct lsa_OpenSecret o;
712         struct policy_handle sec_handle;
713         struct lsa_DATA_BUF_PTR bufp1;
714         struct lsa_DATA_BUF_PTR bufp2;
715         NTTIME new_mtime;
716         NTTIME old_mtime;
717         BOOL ret = True;
718         DATA_BLOB lsa_blob1, lsa_blob_out, session_key;
719         NTSTATUS status;
720
721         creds_arcfour_crypt(samsync_state->creds, secret->current_cipher.cipher_data, 
722                             secret->current_cipher.maxlen); 
723
724         creds_arcfour_crypt(samsync_state->creds, secret->old_cipher.cipher_data, 
725                             secret->old_cipher.maxlen); 
726
727         new->name = talloc_reference(new, name);
728         new->secret = data_blob_talloc(new, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
729         new->mtime = secret->current_cipher_set_time;
730
731         DLIST_ADD(samsync_state->secrets, new);
732
733         old->name = talloc_reference(old, name);
734         old->secret = data_blob_const(secret->old_cipher.cipher_data, secret->old_cipher.maxlen);
735         old->mtime = secret->old_cipher_set_time;
736
737         o.in.handle = samsync_state->lsa_handle;
738         o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
739         o.in.name.string = name;
740         o.out.sec_handle = &sec_handle;
741
742         status = dcerpc_lsa_OpenSecret(samsync_state->p_lsa, mem_ctx, &o);
743         if (!NT_STATUS_IS_OK(status)) {
744                 printf("OpenSecret failed - %s\n", nt_errstr(status));
745                 return False;
746         }
747
748 /*
749   We would like to do this, but it is NOT_SUPPORTED on win2k3
750   TEST_SEC_DESC_EQUAL(secret->sdbuf, lsa, &sec_handle);
751 */
752         status = dcerpc_fetch_session_key(samsync_state->p_lsa, &session_key);
753         if (!NT_STATUS_IS_OK(status)) {
754                 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
755                 return False;
756         }
757
758
759         ZERO_STRUCT(new_mtime);
760         ZERO_STRUCT(old_mtime);
761
762         /* fetch the secret back again */
763         q.in.sec_handle = &sec_handle;
764         q.in.new_val = &bufp1;
765         q.in.new_mtime = &new_mtime;
766         q.in.old_val = &bufp2;
767         q.in.old_mtime = &old_mtime;
768
769         bufp1.buf = NULL;
770         bufp2.buf = NULL;
771
772         status = dcerpc_lsa_QuerySecret(samsync_state->p_lsa, mem_ctx, &q);
773         if (NT_STATUS_EQUAL(NT_STATUS_ACCESS_DENIED, status)) {
774                 /* some things are just off limits */
775                 return True;
776         } else if (!NT_STATUS_IS_OK(status)) {
777                 printf("QuerySecret failed - %s\n", nt_errstr(status));
778                 return False;
779         }
780
781         if (q.out.old_val->buf == NULL) {
782                 /* probably just not available due to ACLs */
783         } else {
784                 lsa_blob1.data = q.out.old_val->buf->data;
785                 lsa_blob1.length = q.out.old_val->buf->length;
786
787                 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
788                 if (!NT_STATUS_IS_OK(status)) {
789                         printf("Failed to decrypt secrets OLD blob\n");
790                         return False;
791                 }
792
793                 if (!q.out.old_mtime) {
794                         printf("OLD mtime not available on LSA for secret %s\n", old->name);
795                         ret = False;
796                 }
797                 if (old->mtime != *q.out.old_mtime) {
798                         printf("OLD mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n", 
799                                old->name, nt_time_string(mem_ctx, old->mtime), 
800                                nt_time_string(mem_ctx, *q.out.old_mtime)); 
801                         ret = False;
802                 }
803
804                 if (old->secret.length != lsa_blob_out.length) {
805                         printf("Returned secret %s doesn't match: %d != %d\n",
806                                old->name, old->secret.length, lsa_blob_out.length);
807                         ret = False;
808                 } else if (memcmp(lsa_blob_out.data, 
809                            old->secret.data, old->secret.length) != 0) {
810                         printf("Returned secret %s doesn't match: \n",
811                                old->name);
812                         DEBUG(1, ("SamSync Secret:\n"));
813                         dump_data(1, old->secret.data, old->secret.length);
814                         DEBUG(1, ("LSA Secret:\n"));
815                         dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
816                         ret = False;
817                 }
818
819         }
820
821         if (q.out.new_val->buf == NULL) {
822                 /* probably just not available due to ACLs */
823         } else {
824                 lsa_blob1.data = q.out.new_val->buf->data;
825                 lsa_blob1.length = q.out.new_val->buf->length;
826
827                 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
828                 if (!NT_STATUS_IS_OK(status)) {
829                         printf("Failed to decrypt secrets OLD blob\n");
830                         return False;
831                 }
832                 
833                 if (!q.out.new_mtime) {
834                         printf("NEW mtime not available on LSA for secret %s\n", new->name);
835                         ret = False;
836                 }
837                 if (new->mtime != *q.out.new_mtime) {
838                         printf("NEW mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n", 
839                                new->name, nt_time_string(mem_ctx, new->mtime), 
840                                nt_time_string(mem_ctx, *q.out.new_mtime)); 
841                         ret = False;
842                 }
843
844                 if (new->secret.length != lsa_blob_out.length) {
845                         printf("Returned secret %s doesn't match: %d != %d\n",
846                                new->name, new->secret.length, lsa_blob_out.length);
847                         ret = False;
848                 } else if (memcmp(lsa_blob_out.data, 
849                            new->secret.data, new->secret.length) != 0) {
850                         printf("Returned secret %s doesn't match: \n",
851                                new->name);
852                         DEBUG(1, ("SamSync Secret:\n"));
853                         dump_data(1, new->secret.data, new->secret.length);
854                         DEBUG(1, ("LSA Secret:\n"));
855                         dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
856                         ret = False;
857                 }
858         }
859
860         return ret;
861 }
862
863 static BOOL samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
864                                           int database_id, struct netr_DELTA_ENUM *delta) 
865 {
866         NTSTATUS status;
867         BOOL ret = True;
868         struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
869         struct dom_sid *dom_sid = delta->delta_id_union.sid;
870
871         struct samsync_trusted_domain *new = talloc(samsync_state, struct samsync_trusted_domain);
872         struct lsa_OpenTrustedDomain t;
873         struct policy_handle trustdom_handle;
874         struct lsa_QueryTrustedDomainInfo q;
875         union lsa_TrustedDomainInfo *info[9];
876         int levels [] = {1, 3, 8};
877         int i;
878
879         new->name = talloc_reference(new, trusted_domain->domain_name.string);
880         new->sid = talloc_reference(new, dom_sid);
881
882         t.in.handle = samsync_state->lsa_handle;
883         t.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
884         t.in.sid = dom_sid;
885         t.out.trustdom_handle = &trustdom_handle;
886
887         status = dcerpc_lsa_OpenTrustedDomain(samsync_state->p_lsa, mem_ctx, &t);
888         if (!NT_STATUS_IS_OK(status)) {
889                 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
890                 return False;
891         }
892         
893         for (i=0; i< ARRAY_SIZE(levels); i++) {
894                 q.in.trustdom_handle = &trustdom_handle;
895                 q.in.level = levels[i];
896                 status = dcerpc_lsa_QueryTrustedDomainInfo(samsync_state->p_lsa, mem_ctx, &q);
897                 if (!NT_STATUS_IS_OK(status)) {
898                         if (q.in.level == 8 && NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER)) {
899                                 info[levels[i]] = NULL;
900                                 continue;
901                         }
902                         printf("QueryInfoTrustedDomain level %d failed - %s\n", 
903                                levels[i], nt_errstr(status));
904                         return False;
905                 }
906                 info[levels[i]]  = q.out.info;
907         }
908
909         if (info[8]) {
910                 TEST_SID_EQUAL(info[8]->full_info.info_ex.sid, dom_sid);
911                 TEST_STRING_EQUAL(info[8]->full_info.info_ex.netbios_name, trusted_domain->domain_name);
912         }
913         TEST_STRING_EQUAL(info[1]->name.netbios_name, trusted_domain->domain_name);
914         TEST_INT_EQUAL(info[3]->posix_offset.posix_offset, trusted_domain->posix_offset);
915 /*
916   We would like to do this, but it is NOT_SUPPORTED on win2k3
917         TEST_SEC_DESC_EQUAL(trusted_domain->sdbuf, lsa, &trustdom_handle);
918 */
919         DLIST_ADD(samsync_state->trusted_domains, new);
920
921         return ret;
922 }
923
924 static BOOL samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
925                                           int database_id, struct netr_DELTA_ENUM *delta) 
926 {
927         NTSTATUS status;
928         BOOL ret = True;
929         struct netr_DELTA_ACCOUNT *account = delta->delta_union.account;
930         struct dom_sid *dom_sid = delta->delta_id_union.sid;
931
932         struct lsa_OpenAccount a;
933         struct policy_handle acct_handle;
934         struct lsa_EnumPrivsAccount e;
935         struct lsa_LookupPrivName r;
936
937         int i, j;
938
939         BOOL *found_priv_in_lsa;
940
941         a.in.handle = samsync_state->lsa_handle;
942         a.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
943         a.in.sid = dom_sid;
944         a.out.acct_handle = &acct_handle;
945
946         status = dcerpc_lsa_OpenAccount(samsync_state->p_lsa, mem_ctx, &a);
947         if (!NT_STATUS_IS_OK(status)) {
948                 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
949                 return False;
950         }
951
952         TEST_SEC_DESC_EQUAL(account->sdbuf, lsa, &acct_handle);
953
954         found_priv_in_lsa = talloc_zero_array(mem_ctx, BOOL, account->privilege_entries);
955
956         e.in.handle = &acct_handle;
957
958         status = dcerpc_lsa_EnumPrivsAccount(samsync_state->p_lsa, mem_ctx, &e);
959         if (!NT_STATUS_IS_OK(status)) {
960                 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
961                 return False;
962         }
963
964         if ((account->privilege_entries && !e.out.privs)) {
965                 printf("Account %s has privilages in SamSync, but not LSA\n",
966                        dom_sid_string(mem_ctx, dom_sid));
967                 return False;
968         }
969
970         if (!account->privilege_entries && e.out.privs && e.out.privs->count) {
971                 printf("Account %s has privilages in LSA, but not SamSync\n",
972                        dom_sid_string(mem_ctx, dom_sid));
973                 return False;
974         }
975
976         TEST_INT_EQUAL(account->privilege_entries, e.out.privs->count);
977         
978         for (i=0;i< e.out.privs->count; i++) {
979                 r.in.handle = samsync_state->lsa_handle;
980                 r.in.luid = &e.out.privs->set[i].luid;
981                 
982                 status = dcerpc_lsa_LookupPrivName(samsync_state->p_lsa, mem_ctx, &r);
983                 if (!NT_STATUS_IS_OK(status)) {
984                         printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
985                         return False;
986                 }
987                 
988                 if (!r.out.name) {
989                         printf("\nLookupPrivName failed to return a name\n");
990                         return False;
991                 }
992                 for (j=0;j<account->privilege_entries; j++) {
993                         if (strcmp(r.out.name->string, account->privilege_name[j].string) == 0) {
994                                 found_priv_in_lsa[j] = True;
995                                 break;
996                         }
997                 }
998         }
999         for (j=0;j<account->privilege_entries; j++) {
1000                 if (!found_priv_in_lsa[j]) {
1001                         printf("Privilage %s on account %s not found in LSA\n", account->privilege_name[j].string, 
1002                                dom_sid_string(mem_ctx, dom_sid));
1003                         ret = False;
1004                 }
1005         }
1006         return ret;
1007 }
1008
1009 /*
1010   try a netlogon DatabaseSync
1011 */
1012 static BOOL test_DatabaseSync(struct samsync_state *samsync_state,
1013                               TALLOC_CTX *mem_ctx)
1014 {
1015         NTSTATUS status;
1016         struct netr_DatabaseSync r;
1017         const uint32_t database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS}; 
1018         int i, d;
1019         BOOL ret = True;
1020         struct samsync_trusted_domain *t;
1021         struct samsync_secret *s;
1022         
1023         const char *domain, *username;
1024
1025         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1026         r.in.computername = TEST_MACHINE_NAME;
1027         r.in.preferredmaximumlength = (uint32_t)-1;
1028         ZERO_STRUCT(r.in.return_authenticator);
1029
1030         for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1031                 r.in.sync_context = 0;
1032                 r.in.database_id = database_ids[i];
1033
1034                 printf("Testing DatabaseSync of id %d\n", r.in.database_id);
1035
1036                 do {
1037                         creds_client_authenticator(samsync_state->creds, &r.in.credential);
1038
1039                         status = dcerpc_netr_DatabaseSync(samsync_state->p, mem_ctx, &r);
1040                         if (!NT_STATUS_IS_OK(status) &&
1041                             !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1042                                 printf("DatabaseSync - %s\n", nt_errstr(status));
1043                                 ret = False;
1044                                 break;
1045                         }
1046
1047                         if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1048                                 printf("Credential chaining failed\n");
1049                         }
1050
1051                         r.in.sync_context = r.out.sync_context;
1052
1053                         for (d=0; d < r.out.delta_enum_array->num_deltas; d++) {
1054                                 switch (r.out.delta_enum_array->delta_enum[d].delta_type) {
1055                                 case NETR_DELTA_DOMAIN:
1056                                         if (!samsync_handle_domain(mem_ctx, samsync_state, 
1057                                                                    r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1058                                                 printf("Failed to handle DELTA_DOMAIN\n");
1059                                                 ret = False;
1060                                         }
1061                                         break;
1062                                 case NETR_DELTA_GROUP:
1063                                         if (!samsync_handle_group(mem_ctx, samsync_state, 
1064                                                                   r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1065                                                 printf("Failed to handle DELTA_USER\n");
1066                                                 ret = False;
1067                                         }
1068                                         break;
1069                                 case NETR_DELTA_USER:
1070                                         if (!samsync_handle_user(mem_ctx, samsync_state, 
1071                                                                  r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1072                                                 printf("Failed to handle DELTA_USER\n");
1073                                                 ret = False;
1074                                         }
1075                                         break;
1076                                 case NETR_DELTA_ALIAS:
1077                                         if (!samsync_handle_alias(mem_ctx, samsync_state, 
1078                                                                   r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1079                                                 printf("Failed to handle DELTA_ALIAS\n");
1080                                                 ret = False;
1081                                         }
1082                                         break;
1083                                 case NETR_DELTA_POLICY:
1084                                         if (!samsync_handle_policy(mem_ctx, samsync_state, 
1085                                                                    r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1086                                                 printf("Failed to handle DELTA_POLICY\n");
1087                                                 ret = False;
1088                                         }
1089                                         break;
1090                                 case NETR_DELTA_TRUSTED_DOMAIN:
1091                                         if (!samsync_handle_trusted_domain(mem_ctx, samsync_state, 
1092                                                                            r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1093                                                 printf("Failed to handle DELTA_TRUSTED_DOMAIN\n");
1094                                                 ret = False;
1095                                         }
1096                                         break;
1097                                 case NETR_DELTA_ACCOUNT:
1098                                         if (!samsync_handle_account(mem_ctx, samsync_state, 
1099                                                                     r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1100                                                 printf("Failed to handle DELTA_ACCOUNT\n");
1101                                                 ret = False;
1102                                         }
1103                                         break;
1104                                 case NETR_DELTA_SECRET:
1105                                         if (!samsync_handle_secret(mem_ctx, samsync_state, 
1106                                                                    r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1107                                                 printf("Failed to handle DELTA_SECRET\n");
1108                                                 ret = False;
1109                                         }
1110                                         break;
1111                                 }
1112                         }
1113                 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1114                 
1115         }
1116
1117         domain = samsync_state->domain_name[SAM_DATABASE_DOMAIN];
1118         if (!domain) {
1119                 printf("Never got a DOMAIN object in samsync!\n");
1120                 return False;
1121         }
1122         
1123         username = talloc_asprintf(mem_ctx, "%s$", domain);
1124         for (t=samsync_state->trusted_domains; t; t=t->next) {
1125                 char *secret_name = talloc_asprintf(mem_ctx, "G$$%s", t->name);
1126                 for (s=samsync_state->secrets; s; s=s->next) {
1127                         if (StrCaseCmp(s->name, secret_name) == 0) {
1128                                 NTSTATUS nt_status;
1129                                 struct samr_Password nt_hash;
1130                                 mdfour(nt_hash.hash, s->secret.data, s->secret.length);
1131                                 
1132                                 printf("Checking password for %s\\%s\n", t->name, username);
1133                                 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta, 
1134                                                           t->name,
1135                                                           username, 
1136                                                           TEST_MACHINE_NAME2,
1137                                                           NULL, 
1138                                                           &nt_hash,
1139                                                           NULL);
1140                                 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS)) {
1141                                         printf("Verifiction of trust password to %s failed: %s (the trusted domain is not available)\n", 
1142                                                t->name, nt_errstr(nt_status));
1143                                         
1144                                         break;
1145                                 }
1146                                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1147                                         printf("Verifiction of trust password to %s: should have failed (nologon interdomain trust account), instead: %s\n", 
1148                                                t->name, nt_errstr(nt_status));
1149                                         ret = False;
1150                                 }
1151                                 
1152                                 /* break it */
1153                                 nt_hash.hash[0]++;
1154                                 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta, 
1155                                                           t->name,
1156                                                           username, 
1157                                                           TEST_MACHINE_NAME2,
1158                                                           NULL,
1159                                                           &nt_hash,
1160                                                           NULL);
1161                                 
1162                                 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
1163                                         printf("Verifiction of trust password to %s: should have failed (wrong password), instead: %s\n", 
1164                                                t->name, nt_errstr(nt_status));
1165                                         ret = False;
1166                                 }
1167                                 
1168                                 break;
1169                         }
1170                 }
1171         }
1172         return ret;
1173 }
1174
1175
1176 /*
1177   try a netlogon DatabaseDeltas
1178 */
1179 static BOOL test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX *mem_ctx)
1180 {
1181         NTSTATUS status;
1182         struct netr_DatabaseDeltas r;
1183         const uint32_t database_ids[] = {0, 1, 2}; 
1184         int i;
1185         BOOL ret = True;
1186
1187         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1188         r.in.computername = TEST_MACHINE_NAME;
1189         r.in.preferredmaximumlength = (uint32_t)-1;
1190         ZERO_STRUCT(r.in.return_authenticator);
1191
1192         for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1193                 r.in.database_id = database_ids[i];
1194                 r.in.sequence_num = samsync_state->seq_num[i];
1195
1196                 if (r.in.sequence_num == 0) continue;
1197
1198                 r.in.sequence_num -= 1;
1199
1200
1201                 printf("Testing DatabaseDeltas of id %d at %llu\n", 
1202                        r.in.database_id, r.in.sequence_num);
1203
1204                 do {
1205                         creds_client_authenticator(samsync_state->creds, &r.in.credential);
1206
1207                         status = dcerpc_netr_DatabaseDeltas(samsync_state->p, mem_ctx, &r);
1208                         if (!NT_STATUS_IS_OK(status) &&
1209                             !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) &&
1210                             !NT_STATUS_EQUAL(status, NT_STATUS_SYNCHRONIZATION_REQUIRED)) {
1211                                 printf("DatabaseDeltas - %s\n", nt_errstr(status));
1212                                 ret = False;
1213                                 break;
1214                         }
1215
1216                         if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1217                                 printf("Credential chaining failed\n");
1218                         }
1219
1220                         r.in.sequence_num++;
1221                 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1222         }
1223
1224         return ret;
1225 }
1226
1227
1228 /*
1229   try a netlogon DatabaseSync2
1230 */
1231 static BOOL test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
1232                                struct creds_CredentialState *creds)
1233 {
1234         NTSTATUS status;
1235         struct netr_DatabaseSync2 r;
1236         const uint32_t database_ids[] = {0, 1, 2}; 
1237         int i;
1238         BOOL ret = True;
1239
1240         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1241         r.in.computername = TEST_MACHINE_NAME;
1242         r.in.preferredmaximumlength = (uint32_t)-1;
1243         ZERO_STRUCT(r.in.return_authenticator);
1244
1245         for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1246                 r.in.sync_context = 0;
1247                 r.in.database_id = database_ids[i];
1248                 r.in.restart_state = 0;
1249
1250                 printf("Testing DatabaseSync2 of id %d\n", r.in.database_id);
1251
1252                 do {
1253                         creds_client_authenticator(creds, &r.in.credential);
1254
1255                         status = dcerpc_netr_DatabaseSync2(p, mem_ctx, &r);
1256                         if (!NT_STATUS_IS_OK(status) &&
1257                             !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1258                                 printf("DatabaseSync2 - %s\n", nt_errstr(status));
1259                                 ret = False;
1260                                 break;
1261                         }
1262
1263                         if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
1264                                 printf("Credential chaining failed\n");
1265                         }
1266
1267                         r.in.sync_context = r.out.sync_context;
1268                 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1269         }
1270
1271         return ret;
1272 }
1273
1274
1275
1276 BOOL torture_rpc_samsync(void)
1277 {
1278         NTSTATUS status;
1279         TALLOC_CTX *mem_ctx;
1280         BOOL ret = True;
1281         struct test_join *join_ctx;
1282         struct test_join *join_ctx2;
1283         const char *machine_password;
1284         const char *machine_password2;
1285         const char *binding = lp_parm_string(-1, "torture", "binding");
1286         struct dcerpc_binding b;
1287         struct dcerpc_binding b_netlogon_wksta;
1288         struct samr_Connect c;
1289         struct samr_SetDomainInfo s;
1290         struct policy_handle *domain_policy;
1291
1292         struct lsa_ObjectAttribute attr;
1293         struct lsa_QosInfo qos;
1294         struct lsa_OpenPolicy2 r;
1295
1296         struct samsync_state *samsync_state;
1297
1298         mem_ctx = talloc_init("torture_rpc_netlogon");
1299
1300         join_ctx = torture_join_domain(TEST_MACHINE_NAME, lp_workgroup(), ACB_SVRTRUST, 
1301                                        &machine_password);
1302         if (!join_ctx) {
1303                 printf("Failed to join as BDC\n");
1304                 return False;
1305         }
1306         
1307         join_ctx2 = torture_join_domain(TEST_MACHINE_NAME2, lp_workgroup(), ACB_WSTRUST, 
1308                                        &machine_password2);
1309         if (!join_ctx2) {
1310                 printf("Failed to join as member\n");
1311                 return False;
1312         }
1313         
1314         samsync_state = talloc_zero(mem_ctx, struct samsync_state);
1315
1316         samsync_state->p_samr = torture_join_samr_pipe(join_ctx);
1317         samsync_state->connect_handle = talloc_zero(samsync_state, struct policy_handle);
1318         samsync_state->lsa_handle = talloc_zero(samsync_state, struct policy_handle);
1319         c.in.system_name = NULL;
1320         c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1321         c.out.connect_handle = samsync_state->connect_handle;
1322
1323         status = dcerpc_samr_Connect(samsync_state->p_samr, mem_ctx, &c);
1324         if (!NT_STATUS_IS_OK(status)) {
1325                 printf("samr_Connect failed\n");
1326                 ret = False;
1327                 goto failed;
1328         }
1329
1330         domain_policy = samsync_open_domain(mem_ctx, samsync_state, lp_workgroup(), NULL);
1331         if (!domain_policy) {
1332                 printf("samrsync_open_domain failed\n");
1333                 ret = False;
1334                 goto failed;
1335         }
1336         
1337         s.in.domain_handle = domain_policy;
1338         s.in.level = 4;
1339         s.in.info = talloc(mem_ctx, union samr_DomainInfo);
1340         
1341         s.in.info->info4.comment.string
1342                 = talloc_asprintf(mem_ctx, 
1343                                   "Tortured by Samba4: %s", 
1344                                   timestring(mem_ctx, time(NULL)));
1345         status = dcerpc_samr_SetDomainInfo(samsync_state->p_samr, mem_ctx, &s);
1346
1347         if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, domain_policy)) {
1348                 ret = False;
1349                 goto failed;
1350         }
1351
1352         if (!NT_STATUS_IS_OK(status)) {
1353                 printf("SetDomainInfo level %u failed - %s\n", 
1354                        s.in.level, nt_errstr(status));
1355                 ret = False;
1356                 goto failed;
1357         }
1358         
1359
1360         status = torture_rpc_connection(&samsync_state->p_lsa, 
1361                                         DCERPC_LSARPC_NAME,
1362                                         DCERPC_LSARPC_UUID,
1363                                         DCERPC_LSARPC_VERSION);
1364
1365         if (!NT_STATUS_IS_OK(status)) {
1366                 ret = False;
1367                 goto failed;
1368         }
1369
1370         qos.len = 0;
1371         qos.impersonation_level = 2;
1372         qos.context_mode = 1;
1373         qos.effective_only = 0;
1374
1375         attr.len = 0;
1376         attr.root_dir = NULL;
1377         attr.object_name = NULL;
1378         attr.attributes = 0;
1379         attr.sec_desc = NULL;
1380         attr.sec_qos = &qos;
1381
1382         r.in.system_name = "\\";
1383         r.in.attr = &attr;
1384         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1385         r.out.handle = samsync_state->lsa_handle;
1386
1387         status = dcerpc_lsa_OpenPolicy2(samsync_state->p_lsa, mem_ctx, &r);
1388         if (!NT_STATUS_IS_OK(status)) {
1389                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1390                 ret = False;
1391                 goto failed;
1392         }
1393
1394         status = dcerpc_parse_binding(mem_ctx, binding, &b);
1395         if (!NT_STATUS_IS_OK(status)) {
1396                 printf("Bad binding string %s\n", binding);
1397                 ret = False;
1398                 goto failed;
1399         }
1400
1401         b.flags &= ~DCERPC_AUTH_OPTIONS;
1402         b.flags |= DCERPC_SCHANNEL_BDC | DCERPC_SIGN;
1403
1404         status = dcerpc_pipe_connect_b(&samsync_state->p, &b, 
1405                                        DCERPC_NETLOGON_UUID,
1406                                        DCERPC_NETLOGON_VERSION,
1407                                        lp_workgroup(), 
1408                                        TEST_MACHINE_NAME,
1409                                        machine_password);
1410
1411         if (!NT_STATUS_IS_OK(status)) {
1412                 ret = False;
1413                 goto failed;
1414         }
1415
1416         status = dcerpc_schannel_creds(samsync_state->p->conn->security_state.generic_state, mem_ctx, &samsync_state->creds);
1417         if (!NT_STATUS_IS_OK(status)) {
1418                 ret = False;
1419         }
1420
1421
1422
1423         status = dcerpc_parse_binding(mem_ctx, binding, &b_netlogon_wksta);
1424         if (!NT_STATUS_IS_OK(status)) {
1425                 printf("Bad binding string %s\n", binding);
1426                 ret = False;
1427                 goto failed;
1428         }
1429
1430         b_netlogon_wksta.flags &= ~DCERPC_AUTH_OPTIONS;
1431         b_netlogon_wksta.flags |= DCERPC_SCHANNEL_WORKSTATION | DCERPC_SIGN;
1432
1433         status = dcerpc_pipe_connect_b(&samsync_state->p_netlogon_wksta, &b_netlogon_wksta, 
1434                                        DCERPC_NETLOGON_UUID,
1435                                        DCERPC_NETLOGON_VERSION,
1436                                        lp_workgroup(), 
1437                                        TEST_MACHINE_NAME2,
1438                                        machine_password2);
1439
1440         if (!NT_STATUS_IS_OK(status)) {
1441                 ret = False;
1442                 goto failed;
1443         }
1444
1445         status = dcerpc_schannel_creds(samsync_state->p_netlogon_wksta->conn->security_state.generic_state, 
1446                                        mem_ctx, &samsync_state->creds_netlogon_wksta);
1447         if (!NT_STATUS_IS_OK(status)) {
1448                 printf("Failed to obtail schanel creds!\n");
1449                 ret = False;
1450         }
1451
1452         if (!test_DatabaseSync(samsync_state, mem_ctx)) {
1453                 printf("DatabaseSync failed\n");
1454                 ret = False;
1455         }
1456
1457         if (!test_DatabaseDeltas(samsync_state, mem_ctx)) {
1458                 printf("DatabaseDeltas failed\n");
1459                 ret = False;
1460         }
1461
1462         if (!test_DatabaseSync2(samsync_state->p, mem_ctx, samsync_state->creds)) {
1463                 printf("DatabaseSync2 failed\n");
1464                 ret = False;
1465         }
1466 failed:
1467         torture_rpc_close(samsync_state->p);
1468
1469         torture_leave_domain(join_ctx);
1470         torture_leave_domain(join_ctx2);
1471
1472         talloc_free(mem_ctx);
1473
1474         return ret;
1475 }