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