s4:torture/rpc/samsync.c: make use of dcerpc_binding_handle stubs
[Samba/gebeck_regimport.git] / source4 / torture / rpc / samsync.c
blob1cfb9dc5cf6803c03d0e0377038a5da8e1d01258
1 /*
2 Unix SMB/CIFS implementation.
4 test suite for netlogon rpc operations
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Tim Potter 2003
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.
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.
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/>.
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"
40 #define TEST_MACHINE_NAME "samsynctest"
41 #define TEST_WKSTA_MACHINE_NAME "samsynctest2"
42 #define TEST_USER_NAME "samsynctestuser"
45 try a netlogon SamLogon
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)
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 struct dcerpc_binding_handle *b = p->binding_handle;
64 ninfo.identity_info.domain_name.string = domain;
65 ninfo.identity_info.parameter_control = 0;
66 ninfo.identity_info.logon_id_low = 0;
67 ninfo.identity_info.logon_id_high = 0;
68 ninfo.identity_info.account_name.string = account_name;
69 ninfo.identity_info.workstation.string = workstation;
70 generate_random_buffer(ninfo.challenge,
71 sizeof(ninfo.challenge));
72 if (nt_hash) {
73 ninfo.nt.length = 24;
74 ninfo.nt.data = talloc_array(mem_ctx, uint8_t, 24);
75 SMBOWFencrypt(nt_hash->hash, ninfo.challenge, ninfo.nt.data);
76 } else {
77 ninfo.nt.length = 0;
78 ninfo.nt.data = NULL;
81 if (lm_hash) {
82 ninfo.lm.length = 24;
83 ninfo.lm.data = talloc_array(mem_ctx, uint8_t, 24);
84 SMBOWFencrypt(lm_hash->hash, ninfo.challenge, ninfo.lm.data);
85 } else {
86 ninfo.lm.length = 0;
87 ninfo.lm.data = NULL;
90 logon.network = &ninfo;
92 r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
93 r.in.computer_name = workstation;
94 r.in.credential = &auth;
95 r.in.return_authenticator = &auth2;
96 r.in.logon_level = 2;
97 r.in.logon = &logon;
98 r.out.validation = &validation;
99 r.out.authoritative = &authoritative;
101 ZERO_STRUCT(auth2);
102 netlogon_creds_client_authenticator(creds, &auth);
104 r.in.validation_level = 3;
106 status = dcerpc_netr_LogonSamLogon_r(b, mem_ctx, &r);
108 if (!netlogon_creds_client_check(creds, &r.out.return_authenticator->cred)) {
109 printf("Credential chaining failed\n");
112 if (info3) {
113 *info3 = validation.sam3;
116 return status;
119 struct samsync_state {
120 /* we remember the sequence numbers so we can easily do a DatabaseDelta */
121 uint64_t seq_num[3];
122 const char *domain_name[2];
123 struct samsync_secret *secrets;
124 struct samsync_trusted_domain *trusted_domains;
125 struct netlogon_creds_CredentialState *creds;
126 struct netlogon_creds_CredentialState *creds_netlogon_wksta;
127 struct policy_handle *connect_handle;
128 struct policy_handle *domain_handle[2];
129 struct dom_sid *sid[2];
130 struct dcerpc_pipe *p;
131 struct dcerpc_binding_handle *b;
132 struct dcerpc_pipe *p_netlogon_wksta;
133 struct dcerpc_pipe *p_samr;
134 struct dcerpc_binding_handle *b_samr;
135 struct dcerpc_pipe *p_lsa;
136 struct dcerpc_binding_handle *b_lsa;
137 struct policy_handle *lsa_handle;
140 struct samsync_secret {
141 struct samsync_secret *prev, *next;
142 DATA_BLOB secret;
143 const char *name;
144 NTTIME mtime;
147 struct samsync_trusted_domain {
148 struct samsync_trusted_domain *prev, *next;
149 struct dom_sid *sid;
150 const char *name;
153 static struct policy_handle *samsync_open_domain(TALLOC_CTX *mem_ctx,
154 struct samsync_state *samsync_state,
155 const char *domain,
156 struct dom_sid **sid_p)
158 struct lsa_String name;
159 struct samr_OpenDomain o;
160 struct samr_LookupDomain l;
161 struct dom_sid2 *sid = NULL;
162 struct policy_handle *domain_handle = talloc(mem_ctx, struct policy_handle);
163 NTSTATUS nt_status;
165 name.string = domain;
166 l.in.connect_handle = samsync_state->connect_handle;
167 l.in.domain_name = &name;
168 l.out.sid = &sid;
170 nt_status = dcerpc_samr_LookupDomain_r(samsync_state->b_samr, mem_ctx, &l);
171 if (!NT_STATUS_IS_OK(nt_status)) {
172 printf("LookupDomain failed - %s\n", nt_errstr(nt_status));
173 return NULL;
176 o.in.connect_handle = samsync_state->connect_handle;
177 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
178 o.in.sid = *l.out.sid;
179 o.out.domain_handle = domain_handle;
181 if (sid_p) {
182 *sid_p = *l.out.sid;
185 nt_status = dcerpc_samr_OpenDomain_r(samsync_state->b_samr, mem_ctx, &o);
186 if (!NT_STATUS_IS_OK(nt_status)) {
187 printf("OpenDomain failed - %s\n", nt_errstr(nt_status));
188 return NULL;
191 return domain_handle;
194 static struct sec_desc_buf *samsync_query_samr_sec_desc(TALLOC_CTX *mem_ctx,
195 struct samsync_state *samsync_state,
196 struct policy_handle *handle)
198 struct samr_QuerySecurity r;
199 struct sec_desc_buf *sdbuf = NULL;
200 NTSTATUS status;
202 r.in.handle = handle;
203 r.in.sec_info = 0x7;
204 r.out.sdbuf = &sdbuf;
206 status = dcerpc_samr_QuerySecurity_r(samsync_state->b_samr, mem_ctx, &r);
207 if (!NT_STATUS_IS_OK(status)) {
208 printf("SAMR QuerySecurity failed - %s\n", nt_errstr(status));
209 return NULL;
212 return sdbuf;
215 static struct sec_desc_buf *samsync_query_lsa_sec_desc(TALLOC_CTX *mem_ctx,
216 struct samsync_state *samsync_state,
217 struct policy_handle *handle)
219 struct lsa_QuerySecurity r;
220 struct sec_desc_buf *sdbuf = NULL;
221 NTSTATUS status;
223 r.in.handle = handle;
224 r.in.sec_info = 0x7;
225 r.out.sdbuf = &sdbuf;
227 status = dcerpc_lsa_QuerySecurity_r(samsync_state->b_lsa, mem_ctx, &r);
228 if (!NT_STATUS_IS_OK(status)) {
229 printf("LSA QuerySecurity failed - %s\n", nt_errstr(status));
230 return NULL;
233 return sdbuf;
236 #define TEST_UINT64_EQUAL(i1, i2) do {\
237 if (i1 != i2) {\
238 printf("%s: uint64 mismatch: " #i1 ": 0x%016llx (%lld) != " #i2 ": 0x%016llx (%lld)\n", \
239 __location__, \
240 (long long)i1, (long long)i1, \
241 (long long)i2, (long long)i2);\
242 ret = false;\
244 } while (0)
245 #define TEST_INT_EQUAL(i1, i2) do {\
246 if (i1 != i2) {\
247 printf("%s: integer mismatch: " #i1 ": 0x%08x (%d) != " #i2 ": 0x%08x (%d)\n", \
248 __location__, i1, i1, i2, i2); \
249 ret = false;\
251 } while (0)
252 #define TEST_TIME_EQUAL(t1, t2) do {\
253 if (t1 != t2) {\
254 printf("%s: NTTIME mismatch: " #t1 ":%s != " #t2 ": %s\n", \
255 __location__, nt_time_string(mem_ctx, t1), nt_time_string(mem_ctx, t2));\
256 ret = false;\
258 } while (0)
260 #define TEST_STRING_EQUAL(s1, s2) do {\
261 if (!((!s1.string || s1.string[0]=='\0') && (!s2.string || s2.string[0]=='\0')) \
262 && strcmp_safe(s1.string, s2.string) != 0) {\
263 printf("%s: string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
264 __location__, s1.string, s2.string);\
265 ret = false;\
267 } while (0)
269 #define TEST_BINARY_STRING_EQUAL(s1, s2) do {\
270 if (!((!s1.array || s1.array[0]=='\0') && (!s2.array || s2.array[0]=='\0')) \
271 && memcmp(s1.array, s2.array, s1.length * 2) != 0) {\
272 printf("%s: string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
273 __location__, (const char *)s1.array, (const char *)s2.array);\
274 ret = false;\
276 } while (0)
278 #define TEST_SID_EQUAL(s1, s2) do {\
279 if (!dom_sid_equal(s1, s2)) {\
280 printf("%s: dom_sid mismatch: " #s1 ":%s != " #s2 ": %s\n", \
281 __location__, dom_sid_string(mem_ctx, s1), dom_sid_string(mem_ctx, s2));\
282 ret = false;\
284 } while (0)
286 /* The ~SEC_DESC_SACL_PRESENT is because we don't, as administrator,
287 * get back the SACL part of the SD when we ask over SAMR */
289 #define TEST_SEC_DESC_EQUAL(sd1, pipe, handle) do {\
290 struct sec_desc_buf *sdbuf = samsync_query_ ##pipe## _sec_desc(mem_ctx, samsync_state, \
291 handle); \
292 if (!sdbuf || !sdbuf->sd) { \
293 printf("Could not obtain security descriptor to match " #sd1 "\n");\
294 ret = false; \
295 } else {\
296 if (!security_descriptor_mask_equal(sd1.sd, sdbuf->sd, \
297 ~SEC_DESC_SACL_PRESENT)) {\
298 printf("Security Descriptor Mismatch for %s:\n", #sd1);\
299 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamSync", sd1.sd);\
300 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamR", sdbuf->sd);\
301 ret = false;\
304 } while (0)
306 static bool samsync_handle_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
307 int database_id, struct netr_DELTA_ENUM *delta)
309 struct netr_DELTA_DOMAIN *domain = delta->delta_union.domain;
310 struct dom_sid *dom_sid;
311 struct samr_QueryDomainInfo q[14]; /* q[0] will be unused simple for clarity */
312 union samr_DomainInfo *info[14];
313 uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
314 NTSTATUS nt_status;
315 int i;
316 bool ret = true;
318 samsync_state->seq_num[database_id] =
319 domain->sequence_num;
320 switch (database_id) {
321 case SAM_DATABASE_DOMAIN:
322 break;
323 case SAM_DATABASE_BUILTIN:
324 if (strcasecmp_m("BUILTIN", domain->domain_name.string) != 0) {
325 printf("BUILTIN domain has different name: %s\n", domain->domain_name.string);
327 break;
328 case SAM_DATABASE_PRIVS:
329 printf("DOMAIN entry on privs DB!\n");
330 return false;
331 break;
334 if (!samsync_state->domain_name[database_id]) {
335 samsync_state->domain_name[database_id] =
336 talloc_reference(samsync_state, domain->domain_name.string);
337 } else {
338 if (strcasecmp_m(samsync_state->domain_name[database_id], domain->domain_name.string) != 0) {
339 printf("Domain has name varies!: %s != %s\n", samsync_state->domain_name[database_id],
340 domain->domain_name.string);
341 return false;
345 if (!samsync_state->domain_handle[database_id]) {
346 samsync_state->domain_handle[database_id]
347 = talloc_reference(samsync_state,
348 samsync_open_domain(mem_ctx, samsync_state, samsync_state->domain_name[database_id],
349 &dom_sid));
351 if (samsync_state->domain_handle[database_id]) {
352 samsync_state->sid[database_id] = talloc_reference(samsync_state, dom_sid);
355 printf("\tsequence_nums[%d/%s]=%llu\n",
356 database_id, domain->domain_name.string,
357 (long long)samsync_state->seq_num[database_id]);
359 for (i=0;i<ARRAY_SIZE(levels);i++) {
361 q[levels[i]].in.domain_handle = samsync_state->domain_handle[database_id];
362 q[levels[i]].in.level = levels[i];
363 q[levels[i]].out.info = &info[levels[i]];
365 nt_status = dcerpc_samr_QueryDomainInfo_r(samsync_state->b_samr, mem_ctx, &q[levels[i]]);
367 if (!NT_STATUS_IS_OK(nt_status)) {
368 printf("QueryDomainInfo level %u failed - %s\n",
369 q[levels[i]].in.level, nt_errstr(nt_status));
370 return false;
374 TEST_STRING_EQUAL(info[5]->info5.domain_name, domain->domain_name);
376 TEST_STRING_EQUAL(info[2]->general.oem_information, domain->oem_information);
377 TEST_STRING_EQUAL(info[4]->oem.oem_information, domain->oem_information);
378 TEST_TIME_EQUAL(info[2]->general.force_logoff_time, domain->force_logoff_time);
379 TEST_TIME_EQUAL(info[3]->info3.force_logoff_time, domain->force_logoff_time);
381 TEST_TIME_EQUAL(info[1]->info1.min_password_length, domain->min_password_length);
382 TEST_TIME_EQUAL(info[1]->info1.password_history_length, domain->password_history_length);
383 TEST_TIME_EQUAL(info[1]->info1.max_password_age, domain->max_password_age);
384 TEST_TIME_EQUAL(info[1]->info1.min_password_age, domain->min_password_age);
386 TEST_UINT64_EQUAL(info[8]->info8.sequence_num,
387 domain->sequence_num);
388 TEST_TIME_EQUAL(info[8]->info8.domain_create_time,
389 domain->domain_create_time);
390 TEST_TIME_EQUAL(info[13]->info13.domain_create_time,
391 domain->domain_create_time);
393 TEST_SEC_DESC_EQUAL(domain->sdbuf, samr, samsync_state->domain_handle[database_id]);
395 return ret;
398 static bool samsync_handle_policy(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
399 int database_id, struct netr_DELTA_ENUM *delta)
401 struct netr_DELTA_POLICY *policy = delta->delta_union.policy;
403 samsync_state->seq_num[database_id] =
404 policy->sequence_num;
406 if (!samsync_state->domain_name[SAM_DATABASE_DOMAIN]) {
407 samsync_state->domain_name[SAM_DATABASE_DOMAIN] =
408 talloc_reference(samsync_state, policy->primary_domain_name.string);
409 } else {
410 if (strcasecmp_m(samsync_state->domain_name[SAM_DATABASE_DOMAIN], policy->primary_domain_name.string) != 0) {
411 printf("PRIMARY domain has name varies between DOMAIN and POLICY!: %s != %s\n", samsync_state->domain_name[SAM_DATABASE_DOMAIN],
412 policy->primary_domain_name.string);
413 return false;
417 if (!dom_sid_equal(samsync_state->sid[SAM_DATABASE_DOMAIN], policy->sid)) {
418 printf("Domain SID from POLICY (%s) does not match domain sid from SAMR (%s)\n",
419 dom_sid_string(mem_ctx, policy->sid), dom_sid_string(mem_ctx, samsync_state->sid[SAM_DATABASE_DOMAIN]));
420 return false;
423 printf("\tsequence_nums[%d/PRIVS]=%llu\n",
424 database_id,
425 (long long)samsync_state->seq_num[database_id]);
426 return true;
429 static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
430 int database_id, struct netr_DELTA_ENUM *delta)
432 uint32_t rid = delta->delta_id_union.rid;
433 struct netr_DELTA_USER *user = delta->delta_union.user;
434 struct netr_SamInfo3 *info3;
435 struct samr_Password lm_hash;
436 struct samr_Password nt_hash;
437 struct samr_Password *lm_hash_p = NULL;
438 struct samr_Password *nt_hash_p = NULL;
439 const char *domain = samsync_state->domain_name[database_id];
440 const char *username = user->account_name.string;
441 NTSTATUS nt_status;
442 bool ret = true;
444 struct samr_OpenUser r;
445 struct samr_QueryUserInfo q;
446 union samr_UserInfo *info;
447 struct policy_handle user_handle;
449 struct samr_GetGroupsForUser getgroups;
450 struct samr_RidWithAttributeArray *rids;
452 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
453 printf("SamSync needs domain information before the users\n");
454 return false;
457 r.in.domain_handle = samsync_state->domain_handle[database_id];
458 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
459 r.in.rid = rid;
460 r.out.user_handle = &user_handle;
462 nt_status = dcerpc_samr_OpenUser_r(samsync_state->b_samr, mem_ctx, &r);
463 if (!NT_STATUS_IS_OK(nt_status)) {
464 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
465 return false;
468 q.in.user_handle = &user_handle;
469 q.in.level = 21;
470 q.out.info = &info;
472 TEST_SEC_DESC_EQUAL(user->sdbuf, samr, &user_handle);
474 nt_status = dcerpc_samr_QueryUserInfo_r(samsync_state->b_samr, mem_ctx, &q);
475 if (!NT_STATUS_IS_OK(nt_status)) {
476 printf("QueryUserInfo level %u failed - %s\n",
477 q.in.level, nt_errstr(nt_status));
478 ret = false;
481 getgroups.in.user_handle = &user_handle;
482 getgroups.out.rids = &rids;
484 nt_status = dcerpc_samr_GetGroupsForUser_r(samsync_state->b_samr, mem_ctx, &getgroups);
485 if (!NT_STATUS_IS_OK(nt_status)) {
486 printf("GetGroupsForUser failed - %s\n",
487 nt_errstr(nt_status));
488 ret = false;
491 if (!test_samr_handle_Close(samsync_state->b_samr, mem_ctx, &user_handle)) {
492 printf("samr_handle_Close failed - %s\n",
493 nt_errstr(nt_status));
494 ret = false;
496 if (!ret) {
497 return false;
500 if (!NT_STATUS_IS_OK(nt_status)) {
501 printf("QueryUserInfo level %u failed - %s\n",
502 q.in.level, nt_errstr(nt_status));
503 return false;
506 TEST_STRING_EQUAL(info->info21.account_name, user->account_name);
507 TEST_STRING_EQUAL(info->info21.full_name, user->full_name);
508 TEST_INT_EQUAL(info->info21.rid, user->rid);
509 TEST_INT_EQUAL(info->info21.primary_gid, user->primary_gid);
510 TEST_STRING_EQUAL(info->info21.home_directory, user->home_directory);
511 TEST_STRING_EQUAL(info->info21.home_drive, user->home_drive);
512 TEST_STRING_EQUAL(info->info21.logon_script, user->logon_script);
513 TEST_STRING_EQUAL(info->info21.description, user->description);
514 TEST_STRING_EQUAL(info->info21.workstations, user->workstations);
516 TEST_TIME_EQUAL(info->info21.last_logon, user->last_logon);
517 TEST_TIME_EQUAL(info->info21.last_logoff, user->last_logoff);
520 TEST_INT_EQUAL(info->info21.logon_hours.units_per_week,
521 user->logon_hours.units_per_week);
522 if (ret) {
523 if (memcmp(info->info21.logon_hours.bits, user->logon_hours.bits,
524 info->info21.logon_hours.units_per_week/8) != 0) {
525 printf("Logon hours mismatch\n");
526 ret = false;
530 TEST_INT_EQUAL(info->info21.bad_password_count,
531 user->bad_password_count);
532 TEST_INT_EQUAL(info->info21.logon_count,
533 user->logon_count);
535 TEST_TIME_EQUAL(info->info21.last_password_change,
536 user->last_password_change);
537 TEST_TIME_EQUAL(info->info21.acct_expiry,
538 user->acct_expiry);
540 TEST_INT_EQUAL((info->info21.acct_flags & ~ACB_PW_EXPIRED), user->acct_flags);
541 if (user->acct_flags & ACB_PWNOEXP) {
542 if (info->info21.acct_flags & ACB_PW_EXPIRED) {
543 printf("ACB flags mismatch: both expired and no expiry!\n");
544 ret = false;
546 if (info->info21.force_password_change != (NTTIME)0x7FFFFFFFFFFFFFFFULL) {
547 printf("ACB flags mismatch: no password expiry, but force password change 0x%016llx (%lld) != 0x%016llx (%lld)\n",
548 (unsigned long long)info->info21.force_password_change,
549 (unsigned long long)info->info21.force_password_change,
550 (unsigned long long)0x7FFFFFFFFFFFFFFFULL, (unsigned long long)0x7FFFFFFFFFFFFFFFULL
552 ret = false;
556 TEST_INT_EQUAL(info->info21.nt_password_set, user->nt_password_present);
557 TEST_INT_EQUAL(info->info21.lm_password_set, user->lm_password_present);
558 TEST_INT_EQUAL(info->info21.password_expired, user->password_expired);
560 TEST_STRING_EQUAL(info->info21.comment, user->comment);
561 TEST_BINARY_STRING_EQUAL(info->info21.parameters, user->parameters);
563 TEST_INT_EQUAL(info->info21.country_code, user->country_code);
564 TEST_INT_EQUAL(info->info21.code_page, user->code_page);
566 TEST_STRING_EQUAL(info->info21.profile_path, user->profile_path);
568 if (user->lm_password_present) {
569 lm_hash_p = &lm_hash;
571 if (user->nt_password_present) {
572 nt_hash_p = &nt_hash;
575 if (user->user_private_info.SensitiveData) {
576 DATA_BLOB data;
577 struct netr_USER_KEYS keys;
578 enum ndr_err_code ndr_err;
579 data.data = user->user_private_info.SensitiveData;
580 data.length = user->user_private_info.DataLength;
581 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);
582 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
583 if (keys.keys.keys2.lmpassword.length == 16) {
584 lm_hash_p = &lm_hash;
586 if (keys.keys.keys2.ntpassword.length == 16) {
587 nt_hash_p = &nt_hash;
589 } else {
590 printf("Failed to parse Sensitive Data for %s:\n", username);
591 #if 0
592 dump_data(0, data.data, data.length);
593 #endif
594 return false;
598 if (nt_hash_p) {
599 DATA_BLOB nt_hash_blob = data_blob_const(nt_hash_p, 16);
600 DEBUG(100,("ACCOUNT [%s\\%-25s] NTHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string_upper(mem_ctx, &nt_hash_blob)));
602 if (lm_hash_p) {
603 DATA_BLOB lm_hash_blob = data_blob_const(lm_hash_p, 16);
604 DEBUG(100,("ACCOUNT [%s\\%-25s] LMHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string_upper(mem_ctx, &lm_hash_blob)));
607 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta,
608 domain,
609 username,
610 TEST_WKSTA_MACHINE_NAME,
611 lm_hash_p,
612 nt_hash_p,
613 &info3);
615 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED)) {
616 if (user->acct_flags & ACB_DISABLED) {
617 return true;
619 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)) {
620 if (user->acct_flags & ACB_WSTRUST) {
621 return true;
623 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT)) {
624 if (user->acct_flags & ACB_SVRTRUST) {
625 return true;
627 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
628 if (user->acct_flags & ACB_DOMTRUST) {
629 return true;
631 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
632 if (user->acct_flags & ACB_DOMTRUST) {
633 return true;
635 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT)) {
636 if (user->acct_flags & ACB_AUTOLOCK) {
637 return true;
639 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED)) {
640 if (info->info21.acct_flags & ACB_PW_EXPIRED) {
641 return true;
643 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
644 if (!lm_hash_p && !nt_hash_p) {
645 return true;
647 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE)) {
648 /* We would need to know the server's current time to test this properly */
649 return true;
650 } else if (NT_STATUS_IS_OK(nt_status)) {
651 TEST_INT_EQUAL(user->rid, info3->base.rid);
652 TEST_INT_EQUAL(user->primary_gid, info3->base.primary_gid);
653 /* this is 0x0 from NT4 sp6 */
654 if (info3->base.acct_flags) {
655 TEST_INT_EQUAL(user->acct_flags, info3->base.acct_flags);
657 /* this is NULL from NT4 sp6 */
658 if (info3->base.account_name.string) {
659 TEST_STRING_EQUAL(user->account_name, info3->base.account_name);
661 /* this is NULL from Win2k3 */
662 if (info3->base.full_name.string) {
663 TEST_STRING_EQUAL(user->full_name, info3->base.full_name);
665 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
666 TEST_STRING_EQUAL(user->profile_path, info3->base.profile_path);
667 TEST_STRING_EQUAL(user->home_directory, info3->base.home_directory);
668 TEST_STRING_EQUAL(user->home_drive, info3->base.home_drive);
669 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
672 TEST_TIME_EQUAL(user->last_logon, info3->base.last_logon);
673 TEST_TIME_EQUAL(user->acct_expiry, info3->base.acct_expiry);
674 TEST_TIME_EQUAL(user->last_password_change, info3->base.last_password_change);
675 TEST_TIME_EQUAL(info->info21.force_password_change, info3->base.force_password_change);
677 /* Does the concept of a logoff time ever really
678 * exist? (not in any sensible way, according to the
679 * doco I read -- abartlet) */
681 /* This copes with the two different versions of 0 I see */
682 /* with NT4 sp6 we have the || case */
683 if (!((user->last_logoff == 0)
684 || (info3->base.last_logoff == 0x7fffffffffffffffLL))) {
685 TEST_TIME_EQUAL(user->last_logoff, info3->base.last_logoff);
688 TEST_INT_EQUAL(rids->count, info3->base.groups.count);
689 if (rids->count == info3->base.groups.count) {
690 int i, j;
691 int count = rids->count;
692 bool *matched = talloc_zero_array(mem_ctx, bool, rids->count);
694 for (i = 0; i < count; i++) {
695 for (j = 0; j < count; j++) {
696 if ((rids->rids[i].rid ==
697 info3->base.groups.rids[j].rid)
698 && (rids->rids[i].attributes ==
699 info3->base.groups.rids[j].attributes)) {
700 matched[i] = true;
705 for (i = 0; i < rids->count; i++) {
706 if (matched[i] == false) {
707 ret = false;
708 printf("Could not find group RID %u found in getgroups in NETLOGON reply\n",
709 rids->rids[i].rid);
713 return ret;
714 } else {
715 printf("Could not validate password for user %s\\%s: %s\n",
716 domain, username, nt_errstr(nt_status));
717 return false;
719 return false;
722 static bool samsync_handle_alias(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
723 int database_id, struct netr_DELTA_ENUM *delta)
725 uint32_t rid = delta->delta_id_union.rid;
726 struct netr_DELTA_ALIAS *alias = delta->delta_union.alias;
727 NTSTATUS nt_status;
728 bool ret = true;
730 struct samr_OpenAlias r;
731 struct samr_QueryAliasInfo q;
732 union samr_AliasInfo *info;
733 struct policy_handle alias_handle;
735 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
736 printf("SamSync needs domain information before the users\n");
737 return false;
740 r.in.domain_handle = samsync_state->domain_handle[database_id];
741 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
742 r.in.rid = rid;
743 r.out.alias_handle = &alias_handle;
745 nt_status = dcerpc_samr_OpenAlias_r(samsync_state->b_samr, mem_ctx, &r);
746 if (!NT_STATUS_IS_OK(nt_status)) {
747 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
748 return false;
751 q.in.alias_handle = &alias_handle;
752 q.in.level = 1;
753 q.out.info = &info;
755 TEST_SEC_DESC_EQUAL(alias->sdbuf, samr, &alias_handle);
757 nt_status = dcerpc_samr_QueryAliasInfo_r(samsync_state->b_samr, mem_ctx, &q);
758 if (!test_samr_handle_Close(samsync_state->b_samr, mem_ctx, &alias_handle)) {
759 return false;
762 if (!NT_STATUS_IS_OK(nt_status)) {
763 printf("QueryAliasInfo level %u failed - %s\n",
764 q.in.level, nt_errstr(nt_status));
765 return false;
768 TEST_STRING_EQUAL(info->all.name, alias->alias_name);
769 TEST_STRING_EQUAL(info->all.description, alias->description);
770 return ret;
773 static bool samsync_handle_group(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
774 int database_id, struct netr_DELTA_ENUM *delta)
776 uint32_t rid = delta->delta_id_union.rid;
777 struct netr_DELTA_GROUP *group = delta->delta_union.group;
778 NTSTATUS nt_status;
779 bool ret = true;
781 struct samr_OpenGroup r;
782 struct samr_QueryGroupInfo q;
783 union samr_GroupInfo *info;
784 struct policy_handle group_handle;
786 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
787 printf("SamSync needs domain information before the users\n");
788 return false;
791 r.in.domain_handle = samsync_state->domain_handle[database_id];
792 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
793 r.in.rid = rid;
794 r.out.group_handle = &group_handle;
796 nt_status = dcerpc_samr_OpenGroup_r(samsync_state->b_samr, mem_ctx, &r);
797 if (!NT_STATUS_IS_OK(nt_status)) {
798 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
799 return false;
802 q.in.group_handle = &group_handle;
803 q.in.level = 1;
804 q.out.info = &info;
806 TEST_SEC_DESC_EQUAL(group->sdbuf, samr, &group_handle);
808 nt_status = dcerpc_samr_QueryGroupInfo_r(samsync_state->b_samr, mem_ctx, &q);
809 if (!test_samr_handle_Close(samsync_state->b_samr, mem_ctx, &group_handle)) {
810 return false;
813 if (!NT_STATUS_IS_OK(nt_status)) {
814 printf("QueryGroupInfo level %u failed - %s\n",
815 q.in.level, nt_errstr(nt_status));
816 return false;
819 TEST_STRING_EQUAL(info->all.name, group->group_name);
820 TEST_INT_EQUAL(info->all.attributes, group->attributes);
821 TEST_STRING_EQUAL(info->all.description, group->description);
822 return ret;
825 static bool samsync_handle_secret(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
826 int database_id, struct netr_DELTA_ENUM *delta)
828 struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
829 const char *name = delta->delta_id_union.name;
830 struct samsync_secret *nsec = talloc(samsync_state, struct samsync_secret);
831 struct samsync_secret *old = talloc(mem_ctx, struct samsync_secret);
832 struct lsa_QuerySecret q;
833 struct lsa_OpenSecret o;
834 struct policy_handle sec_handle;
835 struct lsa_DATA_BUF_PTR bufp1;
836 struct lsa_DATA_BUF_PTR bufp2;
837 NTTIME nsec_mtime;
838 NTTIME old_mtime;
839 bool ret = true;
840 DATA_BLOB lsa_blob1, lsa_blob_out, session_key;
841 NTSTATUS status;
843 nsec->name = talloc_reference(nsec, name);
844 nsec->secret = data_blob_talloc(nsec, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
845 nsec->mtime = secret->current_cipher_set_time;
847 nsec = talloc_reference(samsync_state, nsec);
848 DLIST_ADD(samsync_state->secrets, nsec);
850 old->name = talloc_reference(old, name);
851 old->secret = data_blob_const(secret->old_cipher.cipher_data, secret->old_cipher.maxlen);
852 old->mtime = secret->old_cipher_set_time;
854 o.in.handle = samsync_state->lsa_handle;
855 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
856 o.in.name.string = name;
857 o.out.sec_handle = &sec_handle;
859 status = dcerpc_lsa_OpenSecret_r(samsync_state->b_lsa, mem_ctx, &o);
860 if (!NT_STATUS_IS_OK(status)) {
861 printf("OpenSecret failed - %s\n", nt_errstr(status));
862 return false;
866 We would like to do this, but it is NOT_SUPPORTED on win2k3
867 TEST_SEC_DESC_EQUAL(secret->sdbuf, lsa, &sec_handle);
869 status = dcerpc_fetch_session_key(samsync_state->p_lsa, &session_key);
870 if (!NT_STATUS_IS_OK(status)) {
871 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
872 return false;
876 ZERO_STRUCT(nsec_mtime);
877 ZERO_STRUCT(old_mtime);
879 /* fetch the secret back again */
880 q.in.sec_handle = &sec_handle;
881 q.in.new_val = &bufp1;
882 q.in.new_mtime = &nsec_mtime;
883 q.in.old_val = &bufp2;
884 q.in.old_mtime = &old_mtime;
886 bufp1.buf = NULL;
887 bufp2.buf = NULL;
889 status = dcerpc_lsa_QuerySecret_r(samsync_state->b_lsa, mem_ctx, &q);
890 if (NT_STATUS_EQUAL(NT_STATUS_ACCESS_DENIED, status)) {
891 /* some things are just off limits */
892 return true;
893 } else if (!NT_STATUS_IS_OK(status)) {
894 printf("QuerySecret failed - %s\n", nt_errstr(status));
895 return false;
898 if (q.out.old_val->buf == NULL) {
899 /* probably just not available due to ACLs */
900 } else {
901 lsa_blob1.data = q.out.old_val->buf->data;
902 lsa_blob1.length = q.out.old_val->buf->length;
904 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
905 if (!NT_STATUS_IS_OK(status)) {
906 printf("Failed to decrypt secrets OLD blob: %s\n", nt_errstr(status));
907 return false;
910 if (!q.out.old_mtime) {
911 printf("OLD mtime not available on LSA for secret %s\n", old->name);
912 ret = false;
914 if (old->mtime != *q.out.old_mtime) {
915 printf("OLD mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
916 old->name, nt_time_string(mem_ctx, old->mtime),
917 nt_time_string(mem_ctx, *q.out.old_mtime));
918 ret = false;
921 if (old->secret.length != lsa_blob_out.length) {
922 printf("Returned secret %s doesn't match: %d != %d\n",
923 old->name, (int)old->secret.length, (int)lsa_blob_out.length);
924 ret = false;
925 } else if (memcmp(lsa_blob_out.data,
926 old->secret.data, old->secret.length) != 0) {
927 printf("Returned secret %s doesn't match: \n",
928 old->name);
929 DEBUG(1, ("SamSync Secret:\n"));
930 dump_data(1, old->secret.data, old->secret.length);
931 DEBUG(1, ("LSA Secret:\n"));
932 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
933 ret = false;
938 if (q.out.new_val->buf == NULL) {
939 /* probably just not available due to ACLs */
940 } else {
941 lsa_blob1.data = q.out.new_val->buf->data;
942 lsa_blob1.length = q.out.new_val->buf->length;
944 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
945 if (!NT_STATUS_IS_OK(status)) {
946 printf("Failed to decrypt secrets OLD blob\n");
947 return false;
950 if (!q.out.new_mtime) {
951 printf("NEW mtime not available on LSA for secret %s\n", nsec->name);
952 ret = false;
954 if (nsec->mtime != *q.out.new_mtime) {
955 printf("NEW mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
956 nsec->name, nt_time_string(mem_ctx, nsec->mtime),
957 nt_time_string(mem_ctx, *q.out.new_mtime));
958 ret = false;
961 if (nsec->secret.length != lsa_blob_out.length) {
962 printf("Returned secret %s doesn't match: %d != %d\n",
963 nsec->name, (int)nsec->secret.length, (int)lsa_blob_out.length);
964 ret = false;
965 } else if (memcmp(lsa_blob_out.data,
966 nsec->secret.data, nsec->secret.length) != 0) {
967 printf("Returned secret %s doesn't match: \n",
968 nsec->name);
969 DEBUG(1, ("SamSync Secret:\n"));
970 dump_data(1, nsec->secret.data, nsec->secret.length);
971 DEBUG(1, ("LSA Secret:\n"));
972 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
973 ret = false;
977 return ret;
980 static bool samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
981 int database_id, struct netr_DELTA_ENUM *delta)
983 NTSTATUS status;
984 bool ret = true;
985 struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
986 struct dom_sid *dom_sid = delta->delta_id_union.sid;
988 struct samsync_trusted_domain *ndom = talloc(samsync_state, struct samsync_trusted_domain);
989 struct lsa_OpenTrustedDomain t;
990 struct policy_handle trustdom_handle;
991 struct lsa_QueryTrustedDomainInfo q;
992 union lsa_TrustedDomainInfo *info[9];
993 union lsa_TrustedDomainInfo *_info = NULL;
994 int levels [] = {1, 3, 8};
995 int i;
997 ndom->name = talloc_reference(ndom, trusted_domain->domain_name.string);
998 ndom->sid = talloc_reference(ndom, dom_sid);
1000 t.in.handle = samsync_state->lsa_handle;
1001 t.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1002 t.in.sid = dom_sid;
1003 t.out.trustdom_handle = &trustdom_handle;
1005 status = dcerpc_lsa_OpenTrustedDomain_r(samsync_state->b_lsa, mem_ctx, &t);
1006 if (!NT_STATUS_IS_OK(status)) {
1007 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1008 return false;
1011 for (i=0; i< ARRAY_SIZE(levels); i++) {
1012 q.in.trustdom_handle = &trustdom_handle;
1013 q.in.level = levels[i];
1014 q.out.info = &_info;
1015 status = dcerpc_lsa_QueryTrustedDomainInfo_r(samsync_state->b_lsa, mem_ctx, &q);
1016 if (!NT_STATUS_IS_OK(status)) {
1017 if (q.in.level == 8 && NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER)) {
1018 info[levels[i]] = NULL;
1019 continue;
1021 printf("QueryInfoTrustedDomain level %d failed - %s\n",
1022 levels[i], nt_errstr(status));
1023 return false;
1025 info[levels[i]] = _info;
1028 if (info[8]) {
1029 TEST_SID_EQUAL(info[8]->full_info.info_ex.sid, dom_sid);
1030 TEST_STRING_EQUAL(info[8]->full_info.info_ex.netbios_name, trusted_domain->domain_name);
1032 TEST_STRING_EQUAL(info[1]->name.netbios_name, trusted_domain->domain_name);
1033 TEST_INT_EQUAL(info[3]->posix_offset.posix_offset, trusted_domain->posix_offset);
1035 We would like to do this, but it is NOT_SUPPORTED on win2k3
1036 TEST_SEC_DESC_EQUAL(trusted_domain->sdbuf, lsa, &trustdom_handle);
1038 ndom = talloc_reference(samsync_state, ndom);
1039 DLIST_ADD(samsync_state->trusted_domains, ndom);
1041 return ret;
1044 static bool samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
1045 int database_id, struct netr_DELTA_ENUM *delta)
1047 NTSTATUS status;
1048 bool ret = true;
1049 struct netr_DELTA_ACCOUNT *account = delta->delta_union.account;
1050 struct dom_sid *dom_sid = delta->delta_id_union.sid;
1052 struct lsa_OpenAccount a;
1053 struct policy_handle acct_handle;
1054 struct lsa_EnumPrivsAccount e;
1055 struct lsa_PrivilegeSet *privs = NULL;
1056 struct lsa_LookupPrivName r;
1058 int i, j;
1060 bool *found_priv_in_lsa;
1062 a.in.handle = samsync_state->lsa_handle;
1063 a.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1064 a.in.sid = dom_sid;
1065 a.out.acct_handle = &acct_handle;
1067 status = dcerpc_lsa_OpenAccount_r(samsync_state->b_lsa, mem_ctx, &a);
1068 if (!NT_STATUS_IS_OK(status)) {
1069 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1070 return false;
1073 TEST_SEC_DESC_EQUAL(account->sdbuf, lsa, &acct_handle);
1075 found_priv_in_lsa = talloc_zero_array(mem_ctx, bool, account->privilege_entries);
1077 e.in.handle = &acct_handle;
1078 e.out.privs = &privs;
1080 status = dcerpc_lsa_EnumPrivsAccount_r(samsync_state->b_lsa, mem_ctx, &e);
1081 if (!NT_STATUS_IS_OK(status)) {
1082 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
1083 return false;
1086 if ((account->privilege_entries && !privs)) {
1087 printf("Account %s has privileges in SamSync, but not LSA\n",
1088 dom_sid_string(mem_ctx, dom_sid));
1089 return false;
1092 if (!account->privilege_entries && privs && privs->count) {
1093 printf("Account %s has privileges in LSA, but not SamSync\n",
1094 dom_sid_string(mem_ctx, dom_sid));
1095 return false;
1098 TEST_INT_EQUAL(account->privilege_entries, privs->count);
1100 for (i=0;i< privs->count; i++) {
1102 struct lsa_StringLarge *name = NULL;
1104 r.in.handle = samsync_state->lsa_handle;
1105 r.in.luid = &privs->set[i].luid;
1106 r.out.name = &name;
1108 status = dcerpc_lsa_LookupPrivName_r(samsync_state->b_lsa, mem_ctx, &r);
1109 if (!NT_STATUS_IS_OK(status)) {
1110 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
1111 return false;
1114 if (!r.out.name) {
1115 printf("\nLookupPrivName failed to return a name\n");
1116 return false;
1118 for (j=0;j<account->privilege_entries; j++) {
1119 if (strcmp(name->string, account->privilege_name[j].string) == 0) {
1120 found_priv_in_lsa[j] = true;
1121 break;
1125 for (j=0;j<account->privilege_entries; j++) {
1126 if (!found_priv_in_lsa[j]) {
1127 printf("Privilege %s on account %s not found in LSA\n", account->privilege_name[j].string,
1128 dom_sid_string(mem_ctx, dom_sid));
1129 ret = false;
1132 return ret;
1136 try a netlogon DatabaseSync
1138 static bool test_DatabaseSync(struct torture_context *tctx,
1139 struct samsync_state *samsync_state,
1140 TALLOC_CTX *mem_ctx)
1142 NTSTATUS status;
1143 TALLOC_CTX *loop_ctx, *delta_ctx, *trustdom_ctx;
1144 struct netr_DatabaseSync r;
1145 const enum netr_SamDatabaseID database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS};
1146 int i, d;
1147 bool ret = true;
1148 struct samsync_trusted_domain *t;
1149 struct samsync_secret *s;
1150 struct netr_Authenticator return_authenticator, credential;
1151 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1153 const char *domain, *username;
1155 ZERO_STRUCT(return_authenticator);
1157 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1158 r.in.computername = TEST_MACHINE_NAME;
1159 r.in.preferredmaximumlength = (uint32_t)-1;
1160 r.in.return_authenticator = &return_authenticator;
1161 r.out.return_authenticator = &return_authenticator;
1162 r.out.delta_enum_array = &delta_enum_array;
1164 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1166 uint32_t sync_context = 0;
1168 r.in.database_id = database_ids[i];
1169 r.in.sync_context = &sync_context;
1170 r.out.sync_context = &sync_context;
1172 printf("Testing DatabaseSync of id %d\n", r.in.database_id);
1174 do {
1175 loop_ctx = talloc_named(mem_ctx, 0, "DatabaseSync loop context");
1176 netlogon_creds_client_authenticator(samsync_state->creds, &credential);
1178 r.in.credential = &credential;
1180 status = dcerpc_netr_DatabaseSync_r(samsync_state->b, loop_ctx, &r);
1181 if (!NT_STATUS_IS_OK(status) &&
1182 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1183 printf("DatabaseSync - %s\n", nt_errstr(status));
1184 ret = false;
1185 break;
1188 if (!netlogon_creds_client_check(samsync_state->creds, &r.out.return_authenticator->cred)) {
1189 printf("Credential chaining failed\n");
1192 r.in.sync_context = r.out.sync_context;
1194 for (d=0; d < delta_enum_array->num_deltas; d++) {
1195 delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
1197 if (!NT_STATUS_IS_OK(samsync_fix_delta(delta_ctx, samsync_state->creds,
1198 r.in.database_id,
1199 &delta_enum_array->delta_enum[d]))) {
1200 printf("Failed to decrypt delta\n");
1201 ret = false;
1204 switch (delta_enum_array->delta_enum[d].delta_type) {
1205 case NETR_DELTA_DOMAIN:
1206 if (!samsync_handle_domain(delta_ctx, samsync_state,
1207 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1208 printf("Failed to handle DELTA_DOMAIN\n");
1209 ret = false;
1211 break;
1212 case NETR_DELTA_GROUP:
1213 if (!samsync_handle_group(delta_ctx, samsync_state,
1214 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1215 printf("Failed to handle DELTA_USER\n");
1216 ret = false;
1218 break;
1219 case NETR_DELTA_USER:
1220 if (!samsync_handle_user(tctx, delta_ctx, samsync_state,
1221 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1222 printf("Failed to handle DELTA_USER\n");
1223 ret = false;
1225 break;
1226 case NETR_DELTA_ALIAS:
1227 if (!samsync_handle_alias(delta_ctx, samsync_state,
1228 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1229 printf("Failed to handle DELTA_ALIAS\n");
1230 ret = false;
1232 break;
1233 case NETR_DELTA_POLICY:
1234 if (!samsync_handle_policy(delta_ctx, samsync_state,
1235 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1236 printf("Failed to handle DELTA_POLICY\n");
1237 ret = false;
1239 break;
1240 case NETR_DELTA_TRUSTED_DOMAIN:
1241 if (!samsync_handle_trusted_domain(delta_ctx, samsync_state,
1242 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1243 printf("Failed to handle DELTA_TRUSTED_DOMAIN\n");
1244 ret = false;
1246 break;
1247 case NETR_DELTA_ACCOUNT:
1248 if (!samsync_handle_account(delta_ctx, samsync_state,
1249 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1250 printf("Failed to handle DELTA_ACCOUNT\n");
1251 ret = false;
1253 break;
1254 case NETR_DELTA_SECRET:
1255 if (!samsync_handle_secret(delta_ctx, samsync_state,
1256 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1257 printf("Failed to handle DELTA_SECRET\n");
1258 ret = false;
1260 break;
1261 case NETR_DELTA_GROUP_MEMBER:
1262 case NETR_DELTA_ALIAS_MEMBER:
1263 /* These are harder to cross-check, and we expect them */
1264 break;
1265 case NETR_DELTA_DELETE_GROUP:
1266 case NETR_DELTA_RENAME_GROUP:
1267 case NETR_DELTA_DELETE_USER:
1268 case NETR_DELTA_RENAME_USER:
1269 case NETR_DELTA_DELETE_ALIAS:
1270 case NETR_DELTA_RENAME_ALIAS:
1271 case NETR_DELTA_DELETE_TRUST:
1272 case NETR_DELTA_DELETE_ACCOUNT:
1273 case NETR_DELTA_DELETE_SECRET:
1274 case NETR_DELTA_DELETE_GROUP2:
1275 case NETR_DELTA_DELETE_USER2:
1276 case NETR_DELTA_MODIFY_COUNT:
1277 default:
1278 printf("Uxpected delta type %d\n", delta_enum_array->delta_enum[d].delta_type);
1279 ret = false;
1280 break;
1282 talloc_free(delta_ctx);
1284 talloc_free(loop_ctx);
1285 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1289 domain = samsync_state->domain_name[SAM_DATABASE_DOMAIN];
1290 if (!domain) {
1291 printf("Never got a DOMAIN object in samsync!\n");
1292 return false;
1295 trustdom_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync Trusted domains context");
1297 username = talloc_asprintf(trustdom_ctx, "%s$", domain);
1298 for (t=samsync_state->trusted_domains; t; t=t->next) {
1299 char *secret_name = talloc_asprintf(trustdom_ctx, "G$$%s", t->name);
1300 for (s=samsync_state->secrets; s; s=s->next) {
1301 if (strcasecmp_m(s->name, secret_name) == 0) {
1302 NTSTATUS nt_status;
1303 struct samr_Password nt_hash;
1304 mdfour(nt_hash.hash, s->secret.data, s->secret.length);
1306 printf("Checking password for %s\\%s\n", t->name, username);
1307 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1308 t->name,
1309 username,
1310 TEST_WKSTA_MACHINE_NAME,
1311 NULL,
1312 &nt_hash,
1313 NULL);
1314 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS)) {
1315 printf("Verifiction of trust password to %s failed: %s (the trusted domain is not available)\n",
1316 t->name, nt_errstr(nt_status));
1318 break;
1320 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1321 printf("Verifiction of trust password to %s: should have failed (nologon interdomain trust account), instead: %s\n",
1322 t->name, nt_errstr(nt_status));
1323 ret = false;
1326 /* break it */
1327 nt_hash.hash[0]++;
1328 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1329 t->name,
1330 username,
1331 TEST_WKSTA_MACHINE_NAME,
1332 NULL,
1333 &nt_hash,
1334 NULL);
1336 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
1337 printf("Verifiction of trust password to %s: should have failed (wrong password), instead: %s\n",
1338 t->name, nt_errstr(nt_status));
1339 ret = false;
1342 break;
1346 talloc_free(trustdom_ctx);
1347 return ret;
1352 try a netlogon DatabaseDeltas
1354 static bool test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX *mem_ctx)
1356 NTSTATUS status;
1357 TALLOC_CTX *loop_ctx;
1358 struct netr_DatabaseDeltas r;
1359 struct netr_Authenticator credential;
1360 struct netr_Authenticator return_authenticator;
1361 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1362 const uint32_t database_ids[] = {0, 1, 2};
1363 int i;
1364 bool ret = true;
1366 ZERO_STRUCT(return_authenticator);
1368 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1369 r.in.computername = TEST_MACHINE_NAME;
1370 r.in.credential = &credential;
1371 r.in.preferredmaximumlength = (uint32_t)-1;
1372 r.in.return_authenticator = &return_authenticator;
1373 r.out.return_authenticator = &return_authenticator;
1374 r.out.delta_enum_array = &delta_enum_array;
1376 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1378 uint64_t seq_num = samsync_state->seq_num[i];
1380 r.in.database_id = database_ids[i];
1381 r.in.sequence_num = &seq_num;
1382 r.out.sequence_num = &seq_num;
1384 if (seq_num == 0) continue;
1386 /* this shows that the bdc doesn't need to do a single call for
1387 * each seqnumber, and the pdc doesn't need to know about old values
1388 * -- metze
1390 seq_num -= 10;
1392 printf("Testing DatabaseDeltas of id %d at %llu\n",
1393 r.in.database_id, (long long)seq_num);
1395 do {
1396 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseDeltas loop context");
1397 netlogon_creds_client_authenticator(samsync_state->creds, &credential);
1399 status = dcerpc_netr_DatabaseDeltas_r(samsync_state->b, loop_ctx, &r);
1400 if (!NT_STATUS_IS_OK(status) &&
1401 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) &&
1402 !NT_STATUS_EQUAL(status, NT_STATUS_SYNCHRONIZATION_REQUIRED)) {
1403 printf("DatabaseDeltas - %s\n", nt_errstr(status));
1404 ret = false;
1407 if (!netlogon_creds_client_check(samsync_state->creds, &return_authenticator.cred)) {
1408 printf("Credential chaining failed\n");
1411 seq_num++;
1412 talloc_free(loop_ctx);
1413 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1416 return ret;
1421 try a netlogon DatabaseSync2
1423 static bool test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1424 struct netlogon_creds_CredentialState *creds)
1426 NTSTATUS status;
1427 TALLOC_CTX *loop_ctx;
1428 struct netr_DatabaseSync2 r;
1429 const uint32_t database_ids[] = {0, 1, 2};
1430 int i;
1431 bool ret = true;
1432 struct netr_Authenticator return_authenticator, credential;
1433 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1434 struct dcerpc_binding_handle *b = p->binding_handle;
1436 ZERO_STRUCT(return_authenticator);
1438 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1439 r.in.computername = TEST_MACHINE_NAME;
1440 r.in.preferredmaximumlength = (uint32_t)-1;
1441 r.in.return_authenticator = &return_authenticator;
1442 r.out.return_authenticator = &return_authenticator;
1443 r.out.delta_enum_array = &delta_enum_array;
1445 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1447 uint32_t sync_context = 0;
1449 r.in.database_id = database_ids[i];
1450 r.in.sync_context = &sync_context;
1451 r.out.sync_context = &sync_context;
1452 r.in.restart_state = 0;
1454 printf("Testing DatabaseSync2 of id %d\n", r.in.database_id);
1456 do {
1457 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync2 loop context");
1458 netlogon_creds_client_authenticator(creds, &credential);
1460 r.in.credential = &credential;
1462 status = dcerpc_netr_DatabaseSync2_r(b, loop_ctx, &r);
1463 if (!NT_STATUS_IS_OK(status) &&
1464 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1465 printf("DatabaseSync2 - %s\n", nt_errstr(status));
1466 ret = false;
1469 if (!netlogon_creds_client_check(creds, &r.out.return_authenticator->cred)) {
1470 printf("Credential chaining failed\n");
1473 talloc_free(loop_ctx);
1474 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1477 return ret;
1482 bool torture_rpc_samsync(struct torture_context *torture)
1484 NTSTATUS status;
1485 TALLOC_CTX *mem_ctx;
1486 bool ret = true;
1487 struct test_join *join_ctx;
1488 struct test_join *join_ctx2;
1489 struct test_join *user_ctx;
1490 const char *machine_password;
1491 const char *wksta_machine_password;
1492 struct dcerpc_binding *b;
1493 struct dcerpc_binding *b_netlogon_wksta;
1494 struct samr_Connect c;
1495 struct samr_SetDomainInfo s;
1496 struct policy_handle *domain_policy;
1498 struct lsa_ObjectAttribute attr;
1499 struct lsa_QosInfo qos;
1500 struct lsa_OpenPolicy2 r;
1501 struct cli_credentials *credentials;
1502 struct cli_credentials *credentials_wksta;
1504 struct samsync_state *samsync_state;
1506 char *test_machine_account;
1508 char *test_wksta_machine_account;
1510 mem_ctx = talloc_init("torture_rpc_netlogon");
1512 test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1513 join_ctx = torture_create_testuser(torture, test_machine_account,
1514 lp_workgroup(torture->lp_ctx), ACB_SVRTRUST,
1515 &machine_password);
1516 if (!join_ctx) {
1517 talloc_free(mem_ctx);
1518 printf("Failed to join as BDC\n");
1519 return false;
1522 test_wksta_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_WKSTA_MACHINE_NAME);
1523 join_ctx2 = torture_create_testuser(torture, test_wksta_machine_account, lp_workgroup(torture->lp_ctx), ACB_WSTRUST, &wksta_machine_password);
1524 if (!join_ctx2) {
1525 talloc_free(mem_ctx);
1526 printf("Failed to join as member\n");
1527 return false;
1530 user_ctx = torture_create_testuser(torture, TEST_USER_NAME,
1531 lp_workgroup(torture->lp_ctx),
1532 ACB_NORMAL, NULL);
1533 if (!user_ctx) {
1534 talloc_free(mem_ctx);
1535 printf("Failed to create test account\n");
1536 return false;
1539 samsync_state = talloc_zero(mem_ctx, struct samsync_state);
1541 samsync_state->p_samr = torture_join_samr_pipe(join_ctx);
1542 samsync_state->b_samr = samsync_state->p_samr->binding_handle;
1543 samsync_state->connect_handle = talloc_zero(samsync_state, struct policy_handle);
1544 samsync_state->lsa_handle = talloc_zero(samsync_state, struct policy_handle);
1545 c.in.system_name = NULL;
1546 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1547 c.out.connect_handle = samsync_state->connect_handle;
1549 status = dcerpc_samr_Connect_r(samsync_state->b_samr, mem_ctx, &c);
1550 if (!NT_STATUS_IS_OK(status)) {
1551 printf("samr_Connect failed\n");
1552 ret = false;
1553 goto failed;
1556 domain_policy = samsync_open_domain(mem_ctx, samsync_state, lp_workgroup(torture->lp_ctx), NULL);
1557 if (!domain_policy) {
1558 printf("samrsync_open_domain failed\n");
1559 ret = false;
1560 goto failed;
1563 s.in.domain_handle = domain_policy;
1564 s.in.level = 4;
1565 s.in.info = talloc(mem_ctx, union samr_DomainInfo);
1567 s.in.info->oem.oem_information.string
1568 = talloc_asprintf(mem_ctx,
1569 "Tortured by Samba4: %s",
1570 timestring(mem_ctx, time(NULL)));
1571 status = dcerpc_samr_SetDomainInfo_r(samsync_state->b_samr, mem_ctx, &s);
1573 if (!test_samr_handle_Close(samsync_state->b_samr, mem_ctx, domain_policy)) {
1574 ret = false;
1575 goto failed;
1578 if (!NT_STATUS_IS_OK(status)) {
1579 printf("SetDomainInfo level %u failed - %s\n",
1580 s.in.level, nt_errstr(status));
1581 ret = false;
1582 goto failed;
1586 status = torture_rpc_connection(torture,
1587 &samsync_state->p_lsa,
1588 &ndr_table_lsarpc);
1590 if (!NT_STATUS_IS_OK(status)) {
1591 ret = false;
1592 goto failed;
1594 samsync_state->b_lsa = samsync_state->p_lsa->binding_handle;
1596 qos.len = 0;
1597 qos.impersonation_level = 2;
1598 qos.context_mode = 1;
1599 qos.effective_only = 0;
1601 attr.len = 0;
1602 attr.root_dir = NULL;
1603 attr.object_name = NULL;
1604 attr.attributes = 0;
1605 attr.sec_desc = NULL;
1606 attr.sec_qos = &qos;
1608 r.in.system_name = "\\";
1609 r.in.attr = &attr;
1610 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1611 r.out.handle = samsync_state->lsa_handle;
1613 status = dcerpc_lsa_OpenPolicy2_r(samsync_state->b_lsa, mem_ctx, &r);
1614 if (!NT_STATUS_IS_OK(status)) {
1615 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1616 ret = false;
1617 goto failed;
1620 status = torture_rpc_binding(torture, &b);
1621 if (!NT_STATUS_IS_OK(status)) {
1622 ret = false;
1623 goto failed;
1626 b->flags &= ~DCERPC_AUTH_OPTIONS;
1627 b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1629 credentials = cli_credentials_init(mem_ctx);
1631 cli_credentials_set_workstation(credentials, TEST_MACHINE_NAME, CRED_SPECIFIED);
1632 cli_credentials_set_domain(credentials, lp_workgroup(torture->lp_ctx), CRED_SPECIFIED);
1633 cli_credentials_set_username(credentials, test_machine_account, CRED_SPECIFIED);
1634 cli_credentials_set_password(credentials, machine_password, CRED_SPECIFIED);
1635 cli_credentials_set_secure_channel_type(credentials,
1636 SEC_CHAN_BDC);
1638 status = dcerpc_pipe_connect_b(samsync_state,
1639 &samsync_state->p, b,
1640 &ndr_table_netlogon,
1641 credentials, torture->ev, torture->lp_ctx);
1643 if (!NT_STATUS_IS_OK(status)) {
1644 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
1645 ret = false;
1646 goto failed;
1648 samsync_state->b = samsync_state->p->binding_handle;
1650 status = dcerpc_schannel_creds(samsync_state->p->conn->security_state.generic_state,
1651 samsync_state, &samsync_state->creds);
1652 if (!NT_STATUS_IS_OK(status)) {
1653 ret = false;
1658 status = torture_rpc_binding(torture, &b_netlogon_wksta);
1659 if (!NT_STATUS_IS_OK(status)) {
1660 ret = false;
1661 goto failed;
1664 b_netlogon_wksta->flags &= ~DCERPC_AUTH_OPTIONS;
1665 b_netlogon_wksta->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1667 credentials_wksta = cli_credentials_init(mem_ctx);
1669 cli_credentials_set_workstation(credentials_wksta, TEST_WKSTA_MACHINE_NAME, CRED_SPECIFIED);
1670 cli_credentials_set_domain(credentials_wksta, lp_workgroup(torture->lp_ctx), CRED_SPECIFIED);
1671 cli_credentials_set_username(credentials_wksta, test_wksta_machine_account, CRED_SPECIFIED);
1672 cli_credentials_set_password(credentials_wksta, wksta_machine_password, CRED_SPECIFIED);
1673 cli_credentials_set_secure_channel_type(credentials_wksta,
1674 SEC_CHAN_WKSTA);
1676 status = dcerpc_pipe_connect_b(samsync_state,
1677 &samsync_state->p_netlogon_wksta,
1678 b_netlogon_wksta,
1679 &ndr_table_netlogon,
1680 credentials_wksta, torture->ev, torture->lp_ctx);
1682 if (!NT_STATUS_IS_OK(status)) {
1683 printf("Failed to connect to server as a Workstation: %s\n", nt_errstr(status));
1684 ret = false;
1685 goto failed;
1688 status = dcerpc_schannel_creds(samsync_state->p_netlogon_wksta->conn->security_state.generic_state,
1689 samsync_state, &samsync_state->creds_netlogon_wksta);
1690 if (!NT_STATUS_IS_OK(status)) {
1691 printf("Failed to obtail schanel creds!\n");
1692 ret = false;
1695 if (!test_DatabaseSync(torture, samsync_state, mem_ctx)) {
1696 printf("DatabaseSync failed\n");
1697 ret = false;
1700 if (!test_DatabaseDeltas(samsync_state, mem_ctx)) {
1701 printf("DatabaseDeltas failed\n");
1702 ret = false;
1705 if (!test_DatabaseSync2(samsync_state->p, mem_ctx, samsync_state->creds)) {
1706 printf("DatabaseSync2 failed\n");
1707 ret = false;
1709 failed:
1711 torture_leave_domain(torture, join_ctx);
1712 torture_leave_domain(torture, join_ctx2);
1713 torture_leave_domain(torture, user_ctx);
1715 talloc_free(mem_ctx);
1717 return ret;