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