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