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