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