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/>.
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
)
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
));
76 ninfo
.nt
.data
= talloc_array(mem_ctx
, uint8_t, 24);
77 SMBOWFencrypt(nt_hash
->hash
, ninfo
.challenge
, ninfo
.nt
.data
);
85 ninfo
.lm
.data
= talloc_array(mem_ctx
, uint8_t, 24);
86 SMBOWFencrypt(lm_hash
->hash
, ninfo
.challenge
, ninfo
.lm
.data
);
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
;
100 r
.out
.validation
= &validation
;
101 r
.out
.authoritative
= &authoritative
;
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");
115 *info3
= validation
.sam3
;
121 struct samsync_state
{
122 /* we remember the sequence numbers so we can easily do a DatabaseDelta */
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
;
146 struct samsync_trusted_domain
{
147 struct samsync_trusted_domain
*prev
, *next
;
152 static struct policy_handle
*samsync_open_domain(TALLOC_CTX
*mem_ctx
,
153 struct samsync_state
*samsync_state
,
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
);
164 name
.string
= domain
;
165 l
.in
.connect_handle
= samsync_state
->connect_handle
;
166 l
.in
.domain_name
= &name
;
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
));
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
;
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
));
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
;
201 r
.in
.handle
= handle
;
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
));
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
;
222 r
.in
.handle
= handle
;
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
));
235 #define TEST_UINT64_EQUAL(i1, i2) do {\
237 printf("%s: uint64 mismatch: " #i1 ": 0x%016llx (%lld) != " #i2 ": 0x%016llx (%lld)\n", \
239 (long long)i1, (long long)i1, \
240 (long long)i2, (long long)i2);\
244 #define TEST_INT_EQUAL(i1, i2) do {\
246 printf("%s: integer mismatch: " #i1 ": 0x%08x (%d) != " #i2 ": 0x%08x (%d)\n", \
247 __location__, i1, i1, i2, i2); \
251 #define TEST_TIME_EQUAL(t1, t2) do {\
253 printf("%s: NTTIME mismatch: " #t1 ":%s != " #t2 ": %s\n", \
254 __location__, nt_time_string(mem_ctx, t1), nt_time_string(mem_ctx, t2));\
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);\
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);\
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));\
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, \
291 if (!sdbuf || !sdbuf->sd) { \
292 printf("Could not obtain security descriptor to match " #sd1 "\n");\
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);\
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};
317 samsync_state
->seq_num
[database_id
] =
318 domain
->sequence_num
;
319 switch (database_id
) {
320 case SAM_DATABASE_DOMAIN
:
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
);
327 case SAM_DATABASE_PRIVS
:
328 printf("DOMAIN entry on privs DB!\n");
333 if (!samsync_state
->domain_name
[database_id
]) {
334 samsync_state
->domain_name
[database_id
] =
335 talloc_reference(samsync_state
, domain
->domain_name
.string
);
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
);
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
],
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
));
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
]);
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
);
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
);
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
]));
422 printf("\tsequence_nums[%d/PRIVS]=%llu\n",
424 (long long)samsync_state
->seq_num
[database_id
]);
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
;
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");
456 r
.in
.domain_handle
= samsync_state
->domain_handle
[database_id
];
457 r
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
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
));
467 q
.in
.user_handle
= &user_handle
;
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
));
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
));
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
));
499 if (!NT_STATUS_IS_OK(nt_status
)) {
500 printf("QueryUserInfo level %u failed - %s\n",
501 q
.in
.level
, nt_errstr(nt_status
));
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
);
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");
529 TEST_INT_EQUAL(info
->info21
.bad_password_count
,
530 user
->bad_password_count
);
531 TEST_INT_EQUAL(info
->info21
.logon_count
,
534 TEST_TIME_EQUAL(info
->info21
.last_password_change
,
535 user
->last_password_change
);
536 TEST_TIME_EQUAL(info
->info21
.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");
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
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
) {
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
;
589 printf("Failed to parse Sensitive Data for %s:\n", username
);
591 dump_data(0, data
.data
, data
.length
);
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
)));
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
,
609 TEST_WKSTA_MACHINE_NAME
,
614 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCOUNT_DISABLED
)) {
615 if (user
->acct_flags
& ACB_DISABLED
) {
618 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
)) {
619 if (user
->acct_flags
& ACB_WSTRUST
) {
622 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT
)) {
623 if (user
->acct_flags
& ACB_SVRTRUST
) {
626 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
627 if (user
->acct_flags
& ACB_DOMTRUST
) {
630 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
631 if (user
->acct_flags
& ACB_DOMTRUST
) {
634 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCOUNT_LOCKED_OUT
)) {
635 if (user
->acct_flags
& ACB_AUTOLOCK
) {
638 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_PASSWORD_EXPIRED
)) {
639 if (info
->info21
.acct_flags
& ACB_PW_EXPIRED
) {
642 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_WRONG_PASSWORD
)) {
643 if (!lm_hash_p
&& !nt_hash_p
) {
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 */
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
) {
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
)) {
704 for (i
= 0; i
< rids
->count
; i
++) {
705 if (matched
[i
] == false) {
707 printf("Could not find group RID %u found in getgroups in NETLOGON reply\n",
714 printf("Could not validate password for user %s\\%s: %s\n",
715 domain
, username
, nt_errstr(nt_status
));
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
;
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");
739 r
.in
.domain_handle
= samsync_state
->domain_handle
[database_id
];
740 r
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
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
));
750 q
.in
.alias_handle
= &alias_handle
;
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
)) {
761 if (!NT_STATUS_IS_OK(nt_status
)) {
762 printf("QueryAliasInfo level %u failed - %s\n",
763 q
.in
.level
, nt_errstr(nt_status
));
767 TEST_STRING_EQUAL(info
->all
.name
, alias
->alias_name
);
768 TEST_STRING_EQUAL(info
->all
.description
, alias
->description
);
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
;
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");
790 r
.in
.domain_handle
= samsync_state
->domain_handle
[database_id
];
791 r
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
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
));
801 q
.in
.group_handle
= &group_handle
;
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
)) {
812 if (!NT_STATUS_IS_OK(nt_status
)) {
813 printf("QueryGroupInfo level %u failed - %s\n",
814 q
.in
.level
, nt_errstr(nt_status
));
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
);
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
;
839 DATA_BLOB lsa_blob1
, lsa_blob_out
, session_key
;
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
));
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
));
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
;
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 */
892 } else if (!NT_STATUS_IS_OK(status
)) {
893 printf("QuerySecret failed - %s\n", nt_errstr(status
));
897 if (q
.out
.old_val
->buf
== NULL
) {
898 /* probably just not available due to ACLs */
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
));
909 if (!q
.out
.old_mtime
) {
910 printf("OLD mtime not available on LSA for secret %s\n", old
->name
);
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
));
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
);
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",
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
);
937 if (q
.out
.new_val
->buf
== NULL
) {
938 /* probably just not available due to ACLs */
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");
949 if (!q
.out
.new_mtime
) {
950 printf("NEW mtime not available on LSA for secret %s\n", nsec
->name
);
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
));
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
);
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",
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
);
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
)
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};
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
;
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
));
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
;
1020 printf("QueryInfoTrustedDomain level %d failed - %s\n",
1021 levels
[i
], nt_errstr(status
));
1024 info
[levels
[i
]] = _info
;
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
);
1043 static bool samsync_handle_account(TALLOC_CTX
*mem_ctx
, struct samsync_state
*samsync_state
,
1044 int database_id
, struct netr_DELTA_ENUM
*delta
)
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
;
1059 bool *found_priv_in_lsa
;
1061 a
.in
.handle
= samsync_state
->lsa_handle
;
1062 a
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
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
));
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
));
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
));
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
));
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
;
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
));
1114 printf("\nLookupPrivName failed to return a name\n");
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;
1124 for (j
=0;j
<account
->privilege_entries
; j
++) {
1125 if (!found_priv_in_lsa
[j
]) {
1126 printf("Privilege %s on account %s not found in LSA\n", account
->privilege_name
[j
].string
,
1127 dom_sid_string(mem_ctx
, dom_sid
));
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
)
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
};
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
);
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
));
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
,
1198 &delta_enum_array
->delta_enum
[d
]))) {
1199 printf("Failed to decrypt delta\n");
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");
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");
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");
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");
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");
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");
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");
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");
1260 case NETR_DELTA_GROUP_MEMBER
:
1261 case NETR_DELTA_ALIAS_MEMBER
:
1262 /* These are harder to cross-check, and we expect them */
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
:
1277 printf("Uxpected delta type %d\n", delta_enum_array
->delta_enum
[d
].delta_type
);
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
];
1290 printf("Never got a DOMAIN object in samsync!\n");
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) {
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
,
1309 TEST_WKSTA_MACHINE_NAME
,
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
));
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
));
1327 nt_status
= test_SamLogon(samsync_state
->p_netlogon_wksta
, trustdom_ctx
, samsync_state
->creds_netlogon_wksta
,
1330 TEST_WKSTA_MACHINE_NAME
,
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
));
1345 talloc_free(trustdom_ctx
);
1351 try a netlogon DatabaseDeltas
1353 static bool test_DatabaseDeltas(struct samsync_state
*samsync_state
, TALLOC_CTX
*mem_ctx
)
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};
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
1391 printf("Testing DatabaseDeltas of id %d at %llu\n",
1392 r
.in
.database_id
, (long long)seq_num
);
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
));
1406 if (!netlogon_creds_client_check(samsync_state
->creds
, &return_authenticator
.cred
)) {
1407 printf("Credential chaining failed\n");
1411 talloc_free(loop_ctx
);
1412 } while (NT_STATUS_EQUAL(status
, STATUS_MORE_ENTRIES
));
1420 try a netlogon DatabaseSync2
1422 static bool test_DatabaseSync2(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
1423 struct netlogon_creds_CredentialState
*creds
)
1426 TALLOC_CTX
*loop_ctx
;
1427 struct netr_DatabaseSync2 r
;
1428 const uint32_t database_ids
[] = {0, 1, 2};
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
);
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
));
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
));
1480 bool torture_rpc_samsync(struct torture_context
*torture
)
1483 TALLOC_CTX
*mem_ctx
;
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
,
1515 talloc_free(mem_ctx
);
1516 printf("Failed to join as BDC\n");
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
);
1523 talloc_free(mem_ctx
);
1524 printf("Failed to join as member\n");
1528 user_ctx
= torture_create_testuser(torture
, TEST_USER_NAME
,
1529 lp_workgroup(torture
->lp_ctx
),
1532 talloc_free(mem_ctx
);
1533 printf("Failed to create test account\n");
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");
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");
1560 s
.in
.domain_handle
= domain_policy
;
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
)) {
1575 if (!NT_STATUS_IS_OK(status
)) {
1576 printf("SetDomainInfo level %u failed - %s\n",
1577 s
.in
.level
, nt_errstr(status
));
1583 status
= torture_rpc_connection(torture
,
1584 &samsync_state
->p_lsa
,
1587 if (!NT_STATUS_IS_OK(status
)) {
1593 qos
.impersonation_level
= 2;
1594 qos
.context_mode
= 1;
1595 qos
.effective_only
= 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
= "\\";
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
));
1616 status
= torture_rpc_binding(torture
, &b
);
1617 if (!NT_STATUS_IS_OK(status
)) {
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
,
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
));
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
)) {
1653 status
= torture_rpc_binding(torture
, &b_netlogon_wksta
);
1654 if (!NT_STATUS_IS_OK(status
)) {
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
,
1671 status
= dcerpc_pipe_connect_b(samsync_state
,
1672 &samsync_state
->p_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
));
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");
1690 if (!test_DatabaseSync(torture
, samsync_state
, mem_ctx
)) {
1691 printf("DatabaseSync failed\n");
1695 if (!test_DatabaseDeltas(samsync_state
, mem_ctx
)) {
1696 printf("DatabaseDeltas failed\n");
1700 if (!test_DatabaseSync2(samsync_state
->p
, mem_ctx
, samsync_state
->creds
)) {
1701 printf("DatabaseSync2 failed\n");
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
);