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/security/security.h"
35 #include "librpc/gen_ndr/ndr_netlogon.h"
36 #include "librpc/gen_ndr/ndr_netlogon_c.h"
37 #include "librpc/gen_ndr/ndr_lsa_c.h"
38 #include "librpc/gen_ndr/ndr_samr_c.h"
39 #include "librpc/gen_ndr/ndr_security.h"
40 #include "param/param.h"
42 #define TEST_MACHINE_NAME "samsynctest"
43 #define TEST_WKSTA_MACHINE_NAME "samsynctest2"
44 #define TEST_USER_NAME "samsynctestuser"
47 try a netlogon SamLogon
49 static NTSTATUS test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
50 struct creds_CredentialState *creds,
51 const char *domain, const char *account_name,
52 const char *workstation,
53 struct samr_Password *lm_hash,
54 struct samr_Password *nt_hash,
55 struct netr_SamInfo3 **info3)
58 struct netr_LogonSamLogon r;
59 struct netr_Authenticator auth, auth2;
60 struct netr_NetworkInfo ninfo;
61 union netr_LogonLevel logon;
62 union netr_Validation validation;
63 uint8_t authoritative;
65 ninfo.identity_info.domain_name.string = domain;
66 ninfo.identity_info.parameter_control = 0;
67 ninfo.identity_info.logon_id_low = 0;
68 ninfo.identity_info.logon_id_high = 0;
69 ninfo.identity_info.account_name.string = account_name;
70 ninfo.identity_info.workstation.string = workstation;
71 generate_random_buffer(ninfo.challenge,
72 sizeof(ninfo.challenge));
75 ninfo.nt.data = talloc_array(mem_ctx, uint8_t, 24);
76 SMBOWFencrypt(nt_hash->hash, ninfo.challenge, ninfo.nt.data);
84 ninfo.lm.data = talloc_array(mem_ctx, uint8_t, 24);
85 SMBOWFencrypt(lm_hash->hash, ninfo.challenge, ninfo.lm.data);
91 logon.network = &ninfo;
93 r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
94 r.in.computer_name = workstation;
95 r.in.credential = &auth;
96 r.in.return_authenticator = &auth2;
99 r.out.validation = &validation;
100 r.out.authoritative = &authoritative;
103 creds_client_authenticator(creds, &auth);
105 r.in.validation_level = 3;
107 status = dcerpc_netr_LogonSamLogon(p, mem_ctx, &r);
109 if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
110 printf("Credential chaining failed\n");
114 *info3 = validation.sam3;
120 struct samsync_state {
121 /* we remember the sequence numbers so we can easily do a DatabaseDelta */
123 const char *domain_name[2];
124 struct samsync_secret *secrets;
125 struct samsync_trusted_domain *trusted_domains;
126 struct creds_CredentialState *creds;
127 struct creds_CredentialState *creds_netlogon_wksta;
128 struct policy_handle *connect_handle;
129 struct policy_handle *domain_handle[2];
130 struct dom_sid *sid[2];
131 struct dcerpc_pipe *p;
132 struct dcerpc_pipe *p_netlogon_wksta;
133 struct dcerpc_pipe *p_samr;
134 struct dcerpc_pipe *p_lsa;
135 struct policy_handle *lsa_handle;
138 struct samsync_secret {
139 struct samsync_secret *prev, *next;
145 struct samsync_trusted_domain {
146 struct samsync_trusted_domain *prev, *next;
151 static struct policy_handle *samsync_open_domain(TALLOC_CTX *mem_ctx,
152 struct samsync_state *samsync_state,
154 struct dom_sid **sid)
156 struct lsa_String name;
157 struct samr_OpenDomain o;
158 struct samr_LookupDomain l;
159 struct policy_handle *domain_handle = talloc(mem_ctx, struct policy_handle);
162 name.string = domain;
163 l.in.connect_handle = samsync_state->connect_handle;
164 l.in.domain_name = &name;
166 nt_status = dcerpc_samr_LookupDomain(samsync_state->p_samr, mem_ctx, &l);
167 if (!NT_STATUS_IS_OK(nt_status)) {
168 printf("LookupDomain failed - %s\n", nt_errstr(nt_status));
172 o.in.connect_handle = samsync_state->connect_handle;
173 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
174 o.in.sid = l.out.sid;
175 o.out.domain_handle = domain_handle;
181 nt_status = dcerpc_samr_OpenDomain(samsync_state->p_samr, mem_ctx, &o);
182 if (!NT_STATUS_IS_OK(nt_status)) {
183 printf("OpenDomain failed - %s\n", nt_errstr(nt_status));
187 return domain_handle;
190 static struct sec_desc_buf *samsync_query_samr_sec_desc(TALLOC_CTX *mem_ctx,
191 struct samsync_state *samsync_state,
192 struct policy_handle *handle)
194 struct samr_QuerySecurity r;
195 struct sec_desc_buf *sdbuf = NULL;
198 r.in.handle = handle;
200 r.out.sdbuf = &sdbuf;
202 status = dcerpc_samr_QuerySecurity(samsync_state->p_samr, mem_ctx, &r);
203 if (!NT_STATUS_IS_OK(status)) {
204 printf("SAMR QuerySecurity failed - %s\n", nt_errstr(status));
211 static struct sec_desc_buf *samsync_query_lsa_sec_desc(TALLOC_CTX *mem_ctx,
212 struct samsync_state *samsync_state,
213 struct policy_handle *handle)
215 struct lsa_QuerySecurity r;
216 struct sec_desc_buf *sdbuf = NULL;
219 r.in.handle = handle;
221 r.out.sdbuf = &sdbuf;
223 status = dcerpc_lsa_QuerySecurity(samsync_state->p_lsa, mem_ctx, &r);
224 if (!NT_STATUS_IS_OK(status)) {
225 printf("LSA QuerySecurity failed - %s\n", nt_errstr(status));
232 #define TEST_UINT64_EQUAL(i1, i2) do {\
234 printf("%s: uint64 mismatch: " #i1 ": 0x%016llx (%lld) != " #i2 ": 0x%016llx (%lld)\n", \
236 (long long)i1, (long long)i1, \
237 (long long)i2, (long long)i2);\
241 #define TEST_INT_EQUAL(i1, i2) do {\
243 printf("%s: integer mismatch: " #i1 ": 0x%08x (%d) != " #i2 ": 0x%08x (%d)\n", \
244 __location__, i1, i1, i2, i2); \
248 #define TEST_TIME_EQUAL(t1, t2) do {\
250 printf("%s: NTTIME mismatch: " #t1 ":%s != " #t2 ": %s\n", \
251 __location__, nt_time_string(mem_ctx, t1), nt_time_string(mem_ctx, t2));\
256 #define TEST_STRING_EQUAL(s1, s2) do {\
257 if (!((!s1.string || s1.string[0]=='\0') && (!s2.string || s2.string[0]=='\0')) \
258 && strcmp_safe(s1.string, s2.string) != 0) {\
259 printf("%s: string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
260 __location__, s1.string, s2.string);\
265 #define TEST_BINARY_STRING_EQUAL(s1, s2) do {\
266 if (!((!s1.array || s1.array[0]=='\0') && (!s2.array || s2.array[0]=='\0')) \
267 && memcmp(s1.array, s2.array, s1.length * 2) != 0) {\
268 printf("%s: string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
269 __location__, (const char *)s1.array, (const char *)s2.array);\
274 #define TEST_SID_EQUAL(s1, s2) do {\
275 if (!dom_sid_equal(s1, s2)) {\
276 printf("%s: dom_sid mismatch: " #s1 ":%s != " #s2 ": %s\n", \
277 __location__, dom_sid_string(mem_ctx, s1), dom_sid_string(mem_ctx, s2));\
282 /* The ~SEC_DESC_SACL_PRESENT is because we don't, as administrator,
283 * get back the SACL part of the SD when we ask over SAMR */
285 #define TEST_SEC_DESC_EQUAL(sd1, pipe, handle) do {\
286 struct sec_desc_buf *sdbuf = samsync_query_ ##pipe## _sec_desc(mem_ctx, samsync_state, \
288 if (!sdbuf || !sdbuf->sd) { \
289 printf("Could not obtain security descriptor to match " #sd1 "\n");\
292 if (!security_descriptor_mask_equal(sd1.sd, sdbuf->sd, \
293 ~SEC_DESC_SACL_PRESENT)) {\
294 printf("Security Descriptor Mismatch for %s:\n", #sd1);\
295 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamSync", sd1.sd);\
296 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamR", sdbuf->sd);\
302 static bool samsync_handle_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
303 int database_id, struct netr_DELTA_ENUM *delta)
305 struct netr_DELTA_DOMAIN *domain = delta->delta_union.domain;
306 struct dom_sid *dom_sid;
307 struct samr_QueryDomainInfo q[14]; /* q[0] will be unused simple for clarity */
308 uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
313 samsync_state->seq_num[database_id] =
314 domain->sequence_num;
315 switch (database_id) {
316 case SAM_DATABASE_DOMAIN:
318 case SAM_DATABASE_BUILTIN:
319 if (strcasecmp_m("BUILTIN", domain->domain_name.string) != 0) {
320 printf("BUILTIN domain has different name: %s\n", domain->domain_name.string);
323 case SAM_DATABASE_PRIVS:
324 printf("DOMAIN entry on privs DB!\n");
329 if (!samsync_state->domain_name[database_id]) {
330 samsync_state->domain_name[database_id] =
331 talloc_reference(samsync_state, domain->domain_name.string);
333 if (strcasecmp_m(samsync_state->domain_name[database_id], domain->domain_name.string) != 0) {
334 printf("Domain has name varies!: %s != %s\n", samsync_state->domain_name[database_id],
335 domain->domain_name.string);
340 if (!samsync_state->domain_handle[database_id]) {
341 samsync_state->domain_handle[database_id]
342 = talloc_reference(samsync_state,
343 samsync_open_domain(mem_ctx, samsync_state, samsync_state->domain_name[database_id],
346 if (samsync_state->domain_handle[database_id]) {
347 samsync_state->sid[database_id] = talloc_reference(samsync_state, dom_sid);
350 printf("\tsequence_nums[%d/%s]=%llu\n",
351 database_id, domain->domain_name.string,
352 (long long)samsync_state->seq_num[database_id]);
354 for (i=0;i<ARRAY_SIZE(levels);i++) {
355 q[levels[i]].in.domain_handle = samsync_state->domain_handle[database_id];
356 q[levels[i]].in.level = levels[i];
358 nt_status = dcerpc_samr_QueryDomainInfo(samsync_state->p_samr, mem_ctx, &q[levels[i]]);
360 if (!NT_STATUS_IS_OK(nt_status)) {
361 printf("QueryDomainInfo level %u failed - %s\n",
362 q[levels[i]].in.level, nt_errstr(nt_status));
367 TEST_STRING_EQUAL(q[5].out.info->info5.domain_name, domain->domain_name);
369 TEST_STRING_EQUAL(q[2].out.info->general.oem_information, domain->oem_information);
370 TEST_STRING_EQUAL(q[4].out.info->oem.oem_information, domain->oem_information);
371 TEST_TIME_EQUAL(q[2].out.info->general.force_logoff_time, domain->force_logoff_time);
372 TEST_TIME_EQUAL(q[3].out.info->info3.force_logoff_time, domain->force_logoff_time);
374 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_length, domain->min_password_length);
375 TEST_TIME_EQUAL(q[1].out.info->info1.password_history_length, domain->password_history_length);
376 TEST_TIME_EQUAL(q[1].out.info->info1.max_password_age, domain->max_password_age);
377 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_age, domain->min_password_age);
379 TEST_UINT64_EQUAL(q[8].out.info->info8.sequence_num,
380 domain->sequence_num);
381 TEST_TIME_EQUAL(q[8].out.info->info8.domain_create_time,
382 domain->domain_create_time);
383 TEST_TIME_EQUAL(q[13].out.info->info13.domain_create_time,
384 domain->domain_create_time);
386 TEST_SEC_DESC_EQUAL(domain->sdbuf, samr, samsync_state->domain_handle[database_id]);
391 static bool samsync_handle_policy(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
392 int database_id, struct netr_DELTA_ENUM *delta)
394 struct netr_DELTA_POLICY *policy = delta->delta_union.policy;
396 samsync_state->seq_num[database_id] =
397 policy->sequence_num;
399 if (!samsync_state->domain_name[SAM_DATABASE_DOMAIN]) {
400 samsync_state->domain_name[SAM_DATABASE_DOMAIN] =
401 talloc_reference(samsync_state, policy->primary_domain_name.string);
403 if (strcasecmp_m(samsync_state->domain_name[SAM_DATABASE_DOMAIN], policy->primary_domain_name.string) != 0) {
404 printf("PRIMARY domain has name varies between DOMAIN and POLICY!: %s != %s\n", samsync_state->domain_name[SAM_DATABASE_DOMAIN],
405 policy->primary_domain_name.string);
410 if (!dom_sid_equal(samsync_state->sid[SAM_DATABASE_DOMAIN], policy->sid)) {
411 printf("Domain SID from POLICY (%s) does not match domain sid from SAMR (%s)\n",
412 dom_sid_string(mem_ctx, policy->sid), dom_sid_string(mem_ctx, samsync_state->sid[SAM_DATABASE_DOMAIN]));
416 printf("\tsequence_nums[%d/PRIVS]=%llu\n",
418 (long long)samsync_state->seq_num[database_id]);
422 static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
423 int database_id, struct netr_DELTA_ENUM *delta)
425 uint32_t rid = delta->delta_id_union.rid;
426 struct netr_DELTA_USER *user = delta->delta_union.user;
427 struct netr_SamInfo3 *info3;
428 struct samr_Password lm_hash;
429 struct samr_Password nt_hash;
430 struct samr_Password *lm_hash_p = NULL;
431 struct samr_Password *nt_hash_p = NULL;
432 const char *domain = samsync_state->domain_name[database_id];
433 const char *username = user->account_name.string;
437 struct samr_OpenUser r;
438 struct samr_QueryUserInfo q;
439 struct policy_handle user_handle;
441 struct samr_GetGroupsForUser getgroups;
442 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
443 printf("SamSync needs domain information before the users\n");
447 r.in.domain_handle = samsync_state->domain_handle[database_id];
448 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
450 r.out.user_handle = &user_handle;
452 nt_status = dcerpc_samr_OpenUser(samsync_state->p_samr, mem_ctx, &r);
453 if (!NT_STATUS_IS_OK(nt_status)) {
454 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
458 q.in.user_handle = &user_handle;
461 TEST_SEC_DESC_EQUAL(user->sdbuf, samr, &user_handle);
463 nt_status = dcerpc_samr_QueryUserInfo(samsync_state->p_samr, mem_ctx, &q);
464 if (!NT_STATUS_IS_OK(nt_status)) {
465 printf("QueryUserInfo level %u failed - %s\n",
466 q.in.level, nt_errstr(nt_status));
470 getgroups.in.user_handle = &user_handle;
472 nt_status = dcerpc_samr_GetGroupsForUser(samsync_state->p_samr, mem_ctx, &getgroups);
473 if (!NT_STATUS_IS_OK(nt_status)) {
474 printf("GetGroupsForUser failed - %s\n",
475 nt_errstr(nt_status));
479 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &user_handle)) {
480 printf("samr_handle_Close failed - %s\n",
481 nt_errstr(nt_status));
488 if (!NT_STATUS_IS_OK(nt_status)) {
489 printf("QueryUserInfo level %u failed - %s\n",
490 q.in.level, nt_errstr(nt_status));
494 TEST_STRING_EQUAL(q.out.info->info21.account_name, user->account_name);
495 TEST_STRING_EQUAL(q.out.info->info21.full_name, user->full_name);
496 TEST_INT_EQUAL(q.out.info->info21.rid, user->rid);
497 TEST_INT_EQUAL(q.out.info->info21.primary_gid, user->primary_gid);
498 TEST_STRING_EQUAL(q.out.info->info21.home_directory, user->home_directory);
499 TEST_STRING_EQUAL(q.out.info->info21.home_drive, user->home_drive);
500 TEST_STRING_EQUAL(q.out.info->info21.logon_script, user->logon_script);
501 TEST_STRING_EQUAL(q.out.info->info21.description, user->description);
502 TEST_STRING_EQUAL(q.out.info->info21.workstations, user->workstations);
504 TEST_TIME_EQUAL(q.out.info->info21.last_logon, user->last_logon);
505 TEST_TIME_EQUAL(q.out.info->info21.last_logoff, user->last_logoff);
508 TEST_INT_EQUAL(q.out.info->info21.logon_hours.units_per_week,
509 user->logon_hours.units_per_week);
511 if (memcmp(q.out.info->info21.logon_hours.bits, user->logon_hours.bits,
512 q.out.info->info21.logon_hours.units_per_week/8) != 0) {
513 printf("Logon hours mismatch\n");
518 TEST_INT_EQUAL(q.out.info->info21.bad_password_count,
519 user->bad_password_count);
520 TEST_INT_EQUAL(q.out.info->info21.logon_count,
523 TEST_TIME_EQUAL(q.out.info->info21.last_password_change,
524 user->last_password_change);
525 TEST_TIME_EQUAL(q.out.info->info21.acct_expiry,
528 TEST_INT_EQUAL((q.out.info->info21.acct_flags & ~ACB_PW_EXPIRED), user->acct_flags);
529 if (user->acct_flags & ACB_PWNOEXP) {
530 if (q.out.info->info21.acct_flags & ACB_PW_EXPIRED) {
531 printf("ACB flags mismatch: both expired and no expiry!\n");
534 if (q.out.info->info21.force_password_change != (NTTIME)0x7FFFFFFFFFFFFFFFULL) {
535 printf("ACB flags mismatch: no password expiry, but force password change 0x%016llx (%lld) != 0x%016llx (%lld)\n",
536 (unsigned long long)q.out.info->info21.force_password_change,
537 (unsigned long long)q.out.info->info21.force_password_change,
538 (unsigned long long)0x7FFFFFFFFFFFFFFFULL, (unsigned long long)0x7FFFFFFFFFFFFFFFULL
544 TEST_INT_EQUAL(q.out.info->info21.nt_password_set, user->nt_password_present);
545 TEST_INT_EQUAL(q.out.info->info21.lm_password_set, user->lm_password_present);
546 TEST_INT_EQUAL(q.out.info->info21.password_expired, user->password_expired);
548 TEST_STRING_EQUAL(q.out.info->info21.comment, user->comment);
549 TEST_BINARY_STRING_EQUAL(q.out.info->info21.parameters, user->parameters);
551 TEST_INT_EQUAL(q.out.info->info21.country_code, user->country_code);
552 TEST_INT_EQUAL(q.out.info->info21.code_page, user->code_page);
554 TEST_STRING_EQUAL(q.out.info->info21.profile_path, user->profile_path);
556 if (user->lm_password_present) {
557 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
558 lm_hash_p = &lm_hash;
560 if (user->nt_password_present) {
561 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
562 nt_hash_p = &nt_hash;
565 if (user->user_private_info.SensitiveData) {
567 struct netr_USER_KEYS keys;
568 enum ndr_err_code ndr_err;
569 data.data = user->user_private_info.SensitiveData;
570 data.length = user->user_private_info.DataLength;
571 creds_arcfour_crypt(samsync_state->creds, data.data, data.length);
572 ndr_err = ndr_pull_struct_blob(&data, mem_ctx, lp_iconv_convenience(tctx->lp_ctx), &keys, (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
573 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
574 if (keys.keys.keys2.lmpassword.length == 16) {
575 sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
576 lm_hash_p = &lm_hash;
578 if (keys.keys.keys2.ntpassword.length == 16) {
579 sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
580 nt_hash_p = &nt_hash;
583 printf("Failed to parse Sensitive Data for %s:\n", username);
585 dump_data(0, data.data, data.length);
592 DATA_BLOB nt_hash_blob = data_blob_const(nt_hash_p, 16);
593 DEBUG(100,("ACCOUNT [%s\\%-25s] NTHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string(mem_ctx, &nt_hash_blob)));
596 DATA_BLOB lm_hash_blob = data_blob_const(lm_hash_p, 16);
597 DEBUG(100,("ACCOUNT [%s\\%-25s] LMHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string(mem_ctx, &lm_hash_blob)));
600 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta,
603 TEST_WKSTA_MACHINE_NAME,
608 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED)) {
609 if (user->acct_flags & ACB_DISABLED) {
612 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)) {
613 if (user->acct_flags & ACB_WSTRUST) {
616 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT)) {
617 if (user->acct_flags & ACB_SVRTRUST) {
620 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
621 if (user->acct_flags & ACB_DOMTRUST) {
624 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
625 if (user->acct_flags & ACB_DOMTRUST) {
628 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT)) {
629 if (user->acct_flags & ACB_AUTOLOCK) {
632 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED)) {
633 if (q.out.info->info21.acct_flags & ACB_PW_EXPIRED) {
636 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
637 if (!lm_hash_p && !nt_hash_p) {
640 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE)) {
641 /* We would need to know the server's current time to test this properly */
643 } else if (NT_STATUS_IS_OK(nt_status)) {
644 TEST_INT_EQUAL(user->rid, info3->base.rid);
645 TEST_INT_EQUAL(user->primary_gid, info3->base.primary_gid);
646 /* this is 0x0 from NT4 sp6 */
647 if (info3->base.acct_flags) {
648 TEST_INT_EQUAL(user->acct_flags, info3->base.acct_flags);
650 /* this is NULL from NT4 sp6 */
651 if (info3->base.account_name.string) {
652 TEST_STRING_EQUAL(user->account_name, info3->base.account_name);
654 /* this is NULL from Win2k3 */
655 if (info3->base.full_name.string) {
656 TEST_STRING_EQUAL(user->full_name, info3->base.full_name);
658 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
659 TEST_STRING_EQUAL(user->profile_path, info3->base.profile_path);
660 TEST_STRING_EQUAL(user->home_directory, info3->base.home_directory);
661 TEST_STRING_EQUAL(user->home_drive, info3->base.home_drive);
662 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
665 TEST_TIME_EQUAL(user->last_logon, info3->base.last_logon);
666 TEST_TIME_EQUAL(user->acct_expiry, info3->base.acct_expiry);
667 TEST_TIME_EQUAL(user->last_password_change, info3->base.last_password_change);
668 TEST_TIME_EQUAL(q.out.info->info21.force_password_change, info3->base.force_password_change);
670 /* Does the concept of a logoff time ever really
671 * exist? (not in any sensible way, according to the
672 * doco I read -- abartlet) */
674 /* This copes with the two different versions of 0 I see */
675 /* with NT4 sp6 we have the || case */
676 if (!((user->last_logoff == 0)
677 || (info3->base.last_logoff == 0x7fffffffffffffffLL))) {
678 TEST_TIME_EQUAL(user->last_logoff, info3->base.last_logoff);
681 TEST_INT_EQUAL(getgroups.out.rids->count, info3->base.groups.count);
682 if (getgroups.out.rids->count == info3->base.groups.count) {
684 int count = getgroups.out.rids->count;
685 bool *matched = talloc_zero_array(mem_ctx, bool, getgroups.out.rids->count);
687 for (i = 0; i < count; i++) {
688 for (j = 0; j < count; j++) {
689 if ((getgroups.out.rids->rids[i].rid ==
690 info3->base.groups.rids[j].rid)
691 && (getgroups.out.rids->rids[i].attributes ==
692 info3->base.groups.rids[j].attributes)) {
698 for (i = 0; i < getgroups.out.rids->count; i++) {
699 if (matched[i] == false) {
701 printf("Could not find group RID %u found in getgroups in NETLOGON reply\n",
702 getgroups.out.rids->rids[i].rid);
708 printf("Could not validate password for user %s\\%s: %s\n",
709 domain, username, nt_errstr(nt_status));
715 static bool samsync_handle_alias(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
716 int database_id, struct netr_DELTA_ENUM *delta)
718 uint32_t rid = delta->delta_id_union.rid;
719 struct netr_DELTA_ALIAS *alias = delta->delta_union.alias;
723 struct samr_OpenAlias r;
724 struct samr_QueryAliasInfo q;
725 struct policy_handle alias_handle;
727 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
728 printf("SamSync needs domain information before the users\n");
732 r.in.domain_handle = samsync_state->domain_handle[database_id];
733 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
735 r.out.alias_handle = &alias_handle;
737 nt_status = dcerpc_samr_OpenAlias(samsync_state->p_samr, mem_ctx, &r);
738 if (!NT_STATUS_IS_OK(nt_status)) {
739 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
743 q.in.alias_handle = &alias_handle;
746 TEST_SEC_DESC_EQUAL(alias->sdbuf, samr, &alias_handle);
748 nt_status = dcerpc_samr_QueryAliasInfo(samsync_state->p_samr, mem_ctx, &q);
749 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &alias_handle)) {
753 if (!NT_STATUS_IS_OK(nt_status)) {
754 printf("QueryAliasInfo level %u failed - %s\n",
755 q.in.level, nt_errstr(nt_status));
759 TEST_STRING_EQUAL(q.out.info->all.name, alias->alias_name);
760 TEST_STRING_EQUAL(q.out.info->all.description, alias->description);
764 static bool samsync_handle_group(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
765 int database_id, struct netr_DELTA_ENUM *delta)
767 uint32_t rid = delta->delta_id_union.rid;
768 struct netr_DELTA_GROUP *group = delta->delta_union.group;
772 struct samr_OpenGroup r;
773 struct samr_QueryGroupInfo q;
774 struct policy_handle group_handle;
776 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
777 printf("SamSync needs domain information before the users\n");
781 r.in.domain_handle = samsync_state->domain_handle[database_id];
782 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
784 r.out.group_handle = &group_handle;
786 nt_status = dcerpc_samr_OpenGroup(samsync_state->p_samr, mem_ctx, &r);
787 if (!NT_STATUS_IS_OK(nt_status)) {
788 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
792 q.in.group_handle = &group_handle;
795 TEST_SEC_DESC_EQUAL(group->sdbuf, samr, &group_handle);
797 nt_status = dcerpc_samr_QueryGroupInfo(samsync_state->p_samr, mem_ctx, &q);
798 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &group_handle)) {
802 if (!NT_STATUS_IS_OK(nt_status)) {
803 printf("QueryGroupInfo level %u failed - %s\n",
804 q.in.level, nt_errstr(nt_status));
808 TEST_STRING_EQUAL(q.out.info->all.name, group->group_name);
809 TEST_INT_EQUAL(q.out.info->all.attributes, group->attributes);
810 TEST_STRING_EQUAL(q.out.info->all.description, group->description);
814 static bool samsync_handle_secret(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
815 int database_id, struct netr_DELTA_ENUM *delta)
817 struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
818 const char *name = delta->delta_id_union.name;
819 struct samsync_secret *new = talloc(samsync_state, struct samsync_secret);
820 struct samsync_secret *old = talloc(mem_ctx, struct samsync_secret);
821 struct lsa_QuerySecret q;
822 struct lsa_OpenSecret o;
823 struct policy_handle sec_handle;
824 struct lsa_DATA_BUF_PTR bufp1;
825 struct lsa_DATA_BUF_PTR bufp2;
829 DATA_BLOB lsa_blob1, lsa_blob_out, session_key;
832 creds_arcfour_crypt(samsync_state->creds, secret->current_cipher.cipher_data,
833 secret->current_cipher.maxlen);
835 creds_arcfour_crypt(samsync_state->creds, secret->old_cipher.cipher_data,
836 secret->old_cipher.maxlen);
838 new->name = talloc_reference(new, name);
839 new->secret = data_blob_talloc(new, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
840 new->mtime = secret->current_cipher_set_time;
842 new = talloc_reference(samsync_state, new);
843 DLIST_ADD(samsync_state->secrets, new);
845 old->name = talloc_reference(old, name);
846 old->secret = data_blob_const(secret->old_cipher.cipher_data, secret->old_cipher.maxlen);
847 old->mtime = secret->old_cipher_set_time;
849 o.in.handle = samsync_state->lsa_handle;
850 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
851 o.in.name.string = name;
852 o.out.sec_handle = &sec_handle;
854 status = dcerpc_lsa_OpenSecret(samsync_state->p_lsa, mem_ctx, &o);
855 if (!NT_STATUS_IS_OK(status)) {
856 printf("OpenSecret failed - %s\n", nt_errstr(status));
861 We would like to do this, but it is NOT_SUPPORTED on win2k3
862 TEST_SEC_DESC_EQUAL(secret->sdbuf, lsa, &sec_handle);
864 status = dcerpc_fetch_session_key(samsync_state->p_lsa, &session_key);
865 if (!NT_STATUS_IS_OK(status)) {
866 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
871 ZERO_STRUCT(new_mtime);
872 ZERO_STRUCT(old_mtime);
874 /* fetch the secret back again */
875 q.in.sec_handle = &sec_handle;
876 q.in.new_val = &bufp1;
877 q.in.new_mtime = &new_mtime;
878 q.in.old_val = &bufp2;
879 q.in.old_mtime = &old_mtime;
884 status = dcerpc_lsa_QuerySecret(samsync_state->p_lsa, mem_ctx, &q);
885 if (NT_STATUS_EQUAL(NT_STATUS_ACCESS_DENIED, status)) {
886 /* some things are just off limits */
888 } else if (!NT_STATUS_IS_OK(status)) {
889 printf("QuerySecret failed - %s\n", nt_errstr(status));
893 if (q.out.old_val->buf == NULL) {
894 /* probably just not available due to ACLs */
896 lsa_blob1.data = q.out.old_val->buf->data;
897 lsa_blob1.length = q.out.old_val->buf->length;
899 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
900 if (!NT_STATUS_IS_OK(status)) {
901 printf("Failed to decrypt secrets OLD blob: %s\n", nt_errstr(status));
905 if (!q.out.old_mtime) {
906 printf("OLD mtime not available on LSA for secret %s\n", old->name);
909 if (old->mtime != *q.out.old_mtime) {
910 printf("OLD mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
911 old->name, nt_time_string(mem_ctx, old->mtime),
912 nt_time_string(mem_ctx, *q.out.old_mtime));
916 if (old->secret.length != lsa_blob_out.length) {
917 printf("Returned secret %s doesn't match: %d != %d\n",
918 old->name, (int)old->secret.length, (int)lsa_blob_out.length);
920 } else if (memcmp(lsa_blob_out.data,
921 old->secret.data, old->secret.length) != 0) {
922 printf("Returned secret %s doesn't match: \n",
924 DEBUG(1, ("SamSync Secret:\n"));
925 dump_data(1, old->secret.data, old->secret.length);
926 DEBUG(1, ("LSA Secret:\n"));
927 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
933 if (q.out.new_val->buf == NULL) {
934 /* probably just not available due to ACLs */
936 lsa_blob1.data = q.out.new_val->buf->data;
937 lsa_blob1.length = q.out.new_val->buf->length;
939 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
940 if (!NT_STATUS_IS_OK(status)) {
941 printf("Failed to decrypt secrets OLD blob\n");
945 if (!q.out.new_mtime) {
946 printf("NEW mtime not available on LSA for secret %s\n", new->name);
949 if (new->mtime != *q.out.new_mtime) {
950 printf("NEW mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
951 new->name, nt_time_string(mem_ctx, new->mtime),
952 nt_time_string(mem_ctx, *q.out.new_mtime));
956 if (new->secret.length != lsa_blob_out.length) {
957 printf("Returned secret %s doesn't match: %d != %d\n",
958 new->name, (int)new->secret.length, (int)lsa_blob_out.length);
960 } else if (memcmp(lsa_blob_out.data,
961 new->secret.data, new->secret.length) != 0) {
962 printf("Returned secret %s doesn't match: \n",
964 DEBUG(1, ("SamSync Secret:\n"));
965 dump_data(1, new->secret.data, new->secret.length);
966 DEBUG(1, ("LSA Secret:\n"));
967 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
975 static bool samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
976 int database_id, struct netr_DELTA_ENUM *delta)
980 struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
981 struct dom_sid *dom_sid = delta->delta_id_union.sid;
983 struct samsync_trusted_domain *new = talloc(samsync_state, struct samsync_trusted_domain);
984 struct lsa_OpenTrustedDomain t;
985 struct policy_handle trustdom_handle;
986 struct lsa_QueryTrustedDomainInfo q;
987 union lsa_TrustedDomainInfo *info[9];
988 union lsa_TrustedDomainInfo *_info = NULL;
989 int levels [] = {1, 3, 8};
992 new->name = talloc_reference(new, trusted_domain->domain_name.string);
993 new->sid = talloc_reference(new, dom_sid);
995 t.in.handle = samsync_state->lsa_handle;
996 t.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
998 t.out.trustdom_handle = &trustdom_handle;
1000 status = dcerpc_lsa_OpenTrustedDomain(samsync_state->p_lsa, mem_ctx, &t);
1001 if (!NT_STATUS_IS_OK(status)) {
1002 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1006 for (i=0; i< ARRAY_SIZE(levels); i++) {
1007 q.in.trustdom_handle = &trustdom_handle;
1008 q.in.level = levels[i];
1009 q.out.info = &_info;
1010 status = dcerpc_lsa_QueryTrustedDomainInfo(samsync_state->p_lsa, mem_ctx, &q);
1011 if (!NT_STATUS_IS_OK(status)) {
1012 if (q.in.level == 8 && NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER)) {
1013 info[levels[i]] = NULL;
1016 printf("QueryInfoTrustedDomain level %d failed - %s\n",
1017 levels[i], nt_errstr(status));
1020 info[levels[i]] = _info;
1024 TEST_SID_EQUAL(info[8]->full_info.info_ex.sid, dom_sid);
1025 TEST_STRING_EQUAL(info[8]->full_info.info_ex.netbios_name, trusted_domain->domain_name);
1027 TEST_STRING_EQUAL(info[1]->name.netbios_name, trusted_domain->domain_name);
1028 TEST_INT_EQUAL(info[3]->posix_offset.posix_offset, trusted_domain->posix_offset);
1030 We would like to do this, but it is NOT_SUPPORTED on win2k3
1031 TEST_SEC_DESC_EQUAL(trusted_domain->sdbuf, lsa, &trustdom_handle);
1033 new = talloc_reference(samsync_state, new);
1034 DLIST_ADD(samsync_state->trusted_domains, new);
1039 static bool samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
1040 int database_id, struct netr_DELTA_ENUM *delta)
1044 struct netr_DELTA_ACCOUNT *account = delta->delta_union.account;
1045 struct dom_sid *dom_sid = delta->delta_id_union.sid;
1047 struct lsa_OpenAccount a;
1048 struct policy_handle acct_handle;
1049 struct lsa_EnumPrivsAccount e;
1050 struct lsa_PrivilegeSet *privs = NULL;
1051 struct lsa_LookupPrivName r;
1055 bool *found_priv_in_lsa;
1057 a.in.handle = samsync_state->lsa_handle;
1058 a.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1060 a.out.acct_handle = &acct_handle;
1062 status = dcerpc_lsa_OpenAccount(samsync_state->p_lsa, mem_ctx, &a);
1063 if (!NT_STATUS_IS_OK(status)) {
1064 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1068 TEST_SEC_DESC_EQUAL(account->sdbuf, lsa, &acct_handle);
1070 found_priv_in_lsa = talloc_zero_array(mem_ctx, bool, account->privilege_entries);
1072 e.in.handle = &acct_handle;
1073 e.out.privs = &privs;
1075 status = dcerpc_lsa_EnumPrivsAccount(samsync_state->p_lsa, mem_ctx, &e);
1076 if (!NT_STATUS_IS_OK(status)) {
1077 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
1081 if ((account->privilege_entries && !privs)) {
1082 printf("Account %s has privileges in SamSync, but not LSA\n",
1083 dom_sid_string(mem_ctx, dom_sid));
1087 if (!account->privilege_entries && privs && privs->count) {
1088 printf("Account %s has privileges in LSA, but not SamSync\n",
1089 dom_sid_string(mem_ctx, dom_sid));
1093 TEST_INT_EQUAL(account->privilege_entries, privs->count);
1095 for (i=0;i< privs->count; i++) {
1097 struct lsa_StringLarge *name = NULL;
1099 r.in.handle = samsync_state->lsa_handle;
1100 r.in.luid = &privs->set[i].luid;
1103 status = dcerpc_lsa_LookupPrivName(samsync_state->p_lsa, mem_ctx, &r);
1104 if (!NT_STATUS_IS_OK(status)) {
1105 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
1110 printf("\nLookupPrivName failed to return a name\n");
1113 for (j=0;j<account->privilege_entries; j++) {
1114 if (strcmp(name->string, account->privilege_name[j].string) == 0) {
1115 found_priv_in_lsa[j] = true;
1120 for (j=0;j<account->privilege_entries; j++) {
1121 if (!found_priv_in_lsa[j]) {
1122 printf("Privilage %s on account %s not found in LSA\n", account->privilege_name[j].string,
1123 dom_sid_string(mem_ctx, dom_sid));
1131 try a netlogon DatabaseSync
1133 static bool test_DatabaseSync(struct torture_context *tctx,
1134 struct samsync_state *samsync_state,
1135 TALLOC_CTX *mem_ctx)
1138 TALLOC_CTX *loop_ctx, *delta_ctx, *trustdom_ctx;
1139 struct netr_DatabaseSync r;
1140 const enum netr_SamDatabaseID database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS};
1143 struct samsync_trusted_domain *t;
1144 struct samsync_secret *s;
1145 struct netr_Authenticator return_authenticator, credential;
1146 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1148 const char *domain, *username;
1150 ZERO_STRUCT(return_authenticator);
1152 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1153 r.in.computername = TEST_MACHINE_NAME;
1154 r.in.preferredmaximumlength = (uint32_t)-1;
1155 r.in.return_authenticator = &return_authenticator;
1156 r.out.return_authenticator = &return_authenticator;
1157 r.out.delta_enum_array = &delta_enum_array;
1159 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1161 uint32_t sync_context = 0;
1163 r.in.database_id = database_ids[i];
1164 r.in.sync_context = &sync_context;
1165 r.out.sync_context = &sync_context;
1167 printf("Testing DatabaseSync of id %d\n", r.in.database_id);
1170 loop_ctx = talloc_named(mem_ctx, 0, "DatabaseSync loop context");
1171 creds_client_authenticator(samsync_state->creds, &credential);
1173 r.in.credential = &credential;
1175 status = dcerpc_netr_DatabaseSync(samsync_state->p, loop_ctx, &r);
1176 if (!NT_STATUS_IS_OK(status) &&
1177 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1178 printf("DatabaseSync - %s\n", nt_errstr(status));
1183 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator->cred)) {
1184 printf("Credential chaining failed\n");
1187 r.in.sync_context = r.out.sync_context;
1189 for (d=0; d < delta_enum_array->num_deltas; d++) {
1190 delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
1191 switch (delta_enum_array->delta_enum[d].delta_type) {
1192 case NETR_DELTA_DOMAIN:
1193 if (!samsync_handle_domain(delta_ctx, samsync_state,
1194 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1195 printf("Failed to handle DELTA_DOMAIN\n");
1199 case NETR_DELTA_GROUP:
1200 if (!samsync_handle_group(delta_ctx, samsync_state,
1201 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1202 printf("Failed to handle DELTA_USER\n");
1206 case NETR_DELTA_USER:
1207 if (!samsync_handle_user(tctx, delta_ctx, samsync_state,
1208 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1209 printf("Failed to handle DELTA_USER\n");
1213 case NETR_DELTA_ALIAS:
1214 if (!samsync_handle_alias(delta_ctx, samsync_state,
1215 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1216 printf("Failed to handle DELTA_ALIAS\n");
1220 case NETR_DELTA_POLICY:
1221 if (!samsync_handle_policy(delta_ctx, samsync_state,
1222 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1223 printf("Failed to handle DELTA_POLICY\n");
1227 case NETR_DELTA_TRUSTED_DOMAIN:
1228 if (!samsync_handle_trusted_domain(delta_ctx, samsync_state,
1229 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1230 printf("Failed to handle DELTA_TRUSTED_DOMAIN\n");
1234 case NETR_DELTA_ACCOUNT:
1235 if (!samsync_handle_account(delta_ctx, samsync_state,
1236 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1237 printf("Failed to handle DELTA_ACCOUNT\n");
1241 case NETR_DELTA_SECRET:
1242 if (!samsync_handle_secret(delta_ctx, samsync_state,
1243 r.in.database_id, &delta_enum_array->delta_enum[d])) {
1244 printf("Failed to handle DELTA_SECRET\n");
1248 case NETR_DELTA_GROUP_MEMBER:
1249 case NETR_DELTA_ALIAS_MEMBER:
1250 /* These are harder to cross-check, and we expect them */
1252 case NETR_DELTA_DELETE_GROUP:
1253 case NETR_DELTA_RENAME_GROUP:
1254 case NETR_DELTA_DELETE_USER:
1255 case NETR_DELTA_RENAME_USER:
1256 case NETR_DELTA_DELETE_ALIAS:
1257 case NETR_DELTA_RENAME_ALIAS:
1258 case NETR_DELTA_DELETE_TRUST:
1259 case NETR_DELTA_DELETE_ACCOUNT:
1260 case NETR_DELTA_DELETE_SECRET:
1261 case NETR_DELTA_DELETE_GROUP2:
1262 case NETR_DELTA_DELETE_USER2:
1263 case NETR_DELTA_MODIFY_COUNT:
1265 printf("Uxpected delta type %d\n", delta_enum_array->delta_enum[d].delta_type);
1269 talloc_free(delta_ctx);
1271 talloc_free(loop_ctx);
1272 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1276 domain = samsync_state->domain_name[SAM_DATABASE_DOMAIN];
1278 printf("Never got a DOMAIN object in samsync!\n");
1282 trustdom_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync Trusted domains context");
1284 username = talloc_asprintf(trustdom_ctx, "%s$", domain);
1285 for (t=samsync_state->trusted_domains; t; t=t->next) {
1286 char *secret_name = talloc_asprintf(trustdom_ctx, "G$$%s", t->name);
1287 for (s=samsync_state->secrets; s; s=s->next) {
1288 if (strcasecmp_m(s->name, secret_name) == 0) {
1290 struct samr_Password nt_hash;
1291 mdfour(nt_hash.hash, s->secret.data, s->secret.length);
1293 printf("Checking password for %s\\%s\n", t->name, username);
1294 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1297 TEST_WKSTA_MACHINE_NAME,
1301 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS)) {
1302 printf("Verifiction of trust password to %s failed: %s (the trusted domain is not available)\n",
1303 t->name, nt_errstr(nt_status));
1307 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1308 printf("Verifiction of trust password to %s: should have failed (nologon interdomain trust account), instead: %s\n",
1309 t->name, nt_errstr(nt_status));
1315 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1318 TEST_WKSTA_MACHINE_NAME,
1323 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
1324 printf("Verifiction of trust password to %s: should have failed (wrong password), instead: %s\n",
1325 t->name, nt_errstr(nt_status));
1333 talloc_free(trustdom_ctx);
1339 try a netlogon DatabaseDeltas
1341 static bool test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX *mem_ctx)
1344 TALLOC_CTX *loop_ctx;
1345 struct netr_DatabaseDeltas r;
1346 struct netr_Authenticator credential;
1347 struct netr_Authenticator return_authenticator;
1348 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1349 const uint32_t database_ids[] = {0, 1, 2};
1353 ZERO_STRUCT(return_authenticator);
1355 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1356 r.in.computername = TEST_MACHINE_NAME;
1357 r.in.credential = &credential;
1358 r.in.preferredmaximumlength = (uint32_t)-1;
1359 r.in.return_authenticator = &return_authenticator;
1360 r.out.return_authenticator = &return_authenticator;
1361 r.out.delta_enum_array = &delta_enum_array;
1363 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1365 uint64_t seq_num = samsync_state->seq_num[i];
1367 r.in.database_id = database_ids[i];
1368 r.in.sequence_num = &seq_num;
1369 r.out.sequence_num = &seq_num;
1371 if (seq_num == 0) continue;
1373 /* this shows that the bdc doesn't need to do a single call for
1374 * each seqnumber, and the pdc doesn't need to know about old values
1379 printf("Testing DatabaseDeltas of id %d at %llu\n",
1380 r.in.database_id, (long long)seq_num);
1383 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseDeltas loop context");
1384 creds_client_authenticator(samsync_state->creds, &credential);
1386 status = dcerpc_netr_DatabaseDeltas(samsync_state->p, loop_ctx, &r);
1387 if (!NT_STATUS_IS_OK(status) &&
1388 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) &&
1389 !NT_STATUS_EQUAL(status, NT_STATUS_SYNCHRONIZATION_REQUIRED)) {
1390 printf("DatabaseDeltas - %s\n", nt_errstr(status));
1394 if (!creds_client_check(samsync_state->creds, &return_authenticator.cred)) {
1395 printf("Credential chaining failed\n");
1399 talloc_free(loop_ctx);
1400 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1408 try a netlogon DatabaseSync2
1410 static bool test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1411 struct creds_CredentialState *creds)
1414 TALLOC_CTX *loop_ctx;
1415 struct netr_DatabaseSync2 r;
1416 const uint32_t database_ids[] = {0, 1, 2};
1419 struct netr_Authenticator return_authenticator, credential;
1420 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1422 ZERO_STRUCT(return_authenticator);
1424 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1425 r.in.computername = TEST_MACHINE_NAME;
1426 r.in.preferredmaximumlength = (uint32_t)-1;
1427 r.in.return_authenticator = &return_authenticator;
1428 r.out.return_authenticator = &return_authenticator;
1429 r.out.delta_enum_array = &delta_enum_array;
1431 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1433 uint32_t sync_context = 0;
1435 r.in.database_id = database_ids[i];
1436 r.in.sync_context = &sync_context;
1437 r.out.sync_context = &sync_context;
1438 r.in.restart_state = 0;
1440 printf("Testing DatabaseSync2 of id %d\n", r.in.database_id);
1443 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync2 loop context");
1444 creds_client_authenticator(creds, &credential);
1446 r.in.credential = &credential;
1448 status = dcerpc_netr_DatabaseSync2(p, loop_ctx, &r);
1449 if (!NT_STATUS_IS_OK(status) &&
1450 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1451 printf("DatabaseSync2 - %s\n", nt_errstr(status));
1455 if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
1456 printf("Credential chaining failed\n");
1459 talloc_free(loop_ctx);
1460 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1468 bool torture_rpc_samsync(struct torture_context *torture)
1471 TALLOC_CTX *mem_ctx;
1473 struct test_join *join_ctx;
1474 struct test_join *join_ctx2;
1475 struct test_join *user_ctx;
1476 const char *machine_password;
1477 const char *wksta_machine_password;
1478 struct dcerpc_binding *b;
1479 struct dcerpc_binding *b_netlogon_wksta;
1480 struct samr_Connect c;
1481 struct samr_SetDomainInfo s;
1482 struct policy_handle *domain_policy;
1484 struct lsa_ObjectAttribute attr;
1485 struct lsa_QosInfo qos;
1486 struct lsa_OpenPolicy2 r;
1487 struct cli_credentials *credentials;
1488 struct cli_credentials *credentials_wksta;
1490 struct samsync_state *samsync_state;
1492 char *test_machine_account;
1494 char *test_wksta_machine_account;
1496 mem_ctx = talloc_init("torture_rpc_netlogon");
1498 test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1499 join_ctx = torture_create_testuser(torture, test_machine_account,
1500 lp_workgroup(torture->lp_ctx), ACB_SVRTRUST,
1503 talloc_free(mem_ctx);
1504 printf("Failed to join as BDC\n");
1508 test_wksta_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_WKSTA_MACHINE_NAME);
1509 join_ctx2 = torture_create_testuser(torture, test_wksta_machine_account, lp_workgroup(torture->lp_ctx), ACB_WSTRUST, &wksta_machine_password);
1511 talloc_free(mem_ctx);
1512 printf("Failed to join as member\n");
1516 user_ctx = torture_create_testuser(torture, TEST_USER_NAME,
1517 lp_workgroup(torture->lp_ctx),
1520 talloc_free(mem_ctx);
1521 printf("Failed to create test account\n");
1525 samsync_state = talloc_zero(mem_ctx, struct samsync_state);
1527 samsync_state->p_samr = torture_join_samr_pipe(join_ctx);
1528 samsync_state->connect_handle = talloc_zero(samsync_state, struct policy_handle);
1529 samsync_state->lsa_handle = talloc_zero(samsync_state, struct policy_handle);
1530 c.in.system_name = NULL;
1531 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1532 c.out.connect_handle = samsync_state->connect_handle;
1534 status = dcerpc_samr_Connect(samsync_state->p_samr, mem_ctx, &c);
1535 if (!NT_STATUS_IS_OK(status)) {
1536 printf("samr_Connect failed\n");
1541 domain_policy = samsync_open_domain(mem_ctx, samsync_state, lp_workgroup(torture->lp_ctx), NULL);
1542 if (!domain_policy) {
1543 printf("samrsync_open_domain failed\n");
1548 s.in.domain_handle = domain_policy;
1550 s.in.info = talloc(mem_ctx, union samr_DomainInfo);
1552 s.in.info->oem.oem_information.string
1553 = talloc_asprintf(mem_ctx,
1554 "Tortured by Samba4: %s",
1555 timestring(mem_ctx, time(NULL)));
1556 status = dcerpc_samr_SetDomainInfo(samsync_state->p_samr, mem_ctx, &s);
1558 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, domain_policy)) {
1563 if (!NT_STATUS_IS_OK(status)) {
1564 printf("SetDomainInfo level %u failed - %s\n",
1565 s.in.level, nt_errstr(status));
1571 status = torture_rpc_connection(torture,
1572 &samsync_state->p_lsa,
1575 if (!NT_STATUS_IS_OK(status)) {
1581 qos.impersonation_level = 2;
1582 qos.context_mode = 1;
1583 qos.effective_only = 0;
1586 attr.root_dir = NULL;
1587 attr.object_name = NULL;
1588 attr.attributes = 0;
1589 attr.sec_desc = NULL;
1590 attr.sec_qos = &qos;
1592 r.in.system_name = "\\";
1594 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1595 r.out.handle = samsync_state->lsa_handle;
1597 status = dcerpc_lsa_OpenPolicy2(samsync_state->p_lsa, mem_ctx, &r);
1598 if (!NT_STATUS_IS_OK(status)) {
1599 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1604 status = torture_rpc_binding(torture, &b);
1605 if (!NT_STATUS_IS_OK(status)) {
1610 b->flags &= ~DCERPC_AUTH_OPTIONS;
1611 b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1613 credentials = cli_credentials_init(mem_ctx);
1615 cli_credentials_set_workstation(credentials, TEST_MACHINE_NAME, CRED_SPECIFIED);
1616 cli_credentials_set_domain(credentials, lp_workgroup(torture->lp_ctx), CRED_SPECIFIED);
1617 cli_credentials_set_username(credentials, test_machine_account, CRED_SPECIFIED);
1618 cli_credentials_set_password(credentials, machine_password, CRED_SPECIFIED);
1619 cli_credentials_set_secure_channel_type(credentials,
1622 status = dcerpc_pipe_connect_b(samsync_state,
1623 &samsync_state->p, b,
1624 &ndr_table_netlogon,
1625 credentials, torture->ev, torture->lp_ctx);
1627 if (!NT_STATUS_IS_OK(status)) {
1628 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
1633 status = dcerpc_schannel_creds(samsync_state->p->conn->security_state.generic_state,
1634 samsync_state, &samsync_state->creds);
1635 if (!NT_STATUS_IS_OK(status)) {
1641 status = torture_rpc_binding(torture, &b_netlogon_wksta);
1642 if (!NT_STATUS_IS_OK(status)) {
1647 b_netlogon_wksta->flags &= ~DCERPC_AUTH_OPTIONS;
1648 b_netlogon_wksta->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1650 credentials_wksta = cli_credentials_init(mem_ctx);
1652 cli_credentials_set_workstation(credentials_wksta, TEST_WKSTA_MACHINE_NAME, CRED_SPECIFIED);
1653 cli_credentials_set_domain(credentials_wksta, lp_workgroup(torture->lp_ctx), CRED_SPECIFIED);
1654 cli_credentials_set_username(credentials_wksta, test_wksta_machine_account, CRED_SPECIFIED);
1655 cli_credentials_set_password(credentials_wksta, wksta_machine_password, CRED_SPECIFIED);
1656 cli_credentials_set_secure_channel_type(credentials_wksta,
1659 status = dcerpc_pipe_connect_b(samsync_state,
1660 &samsync_state->p_netlogon_wksta,
1662 &ndr_table_netlogon,
1663 credentials_wksta, torture->ev, torture->lp_ctx);
1665 if (!NT_STATUS_IS_OK(status)) {
1666 printf("Failed to connect to server as a Workstation: %s\n", nt_errstr(status));
1671 status = dcerpc_schannel_creds(samsync_state->p_netlogon_wksta->conn->security_state.generic_state,
1672 samsync_state, &samsync_state->creds_netlogon_wksta);
1673 if (!NT_STATUS_IS_OK(status)) {
1674 printf("Failed to obtail schanel creds!\n");
1678 if (!test_DatabaseSync(torture, samsync_state, mem_ctx)) {
1679 printf("DatabaseSync failed\n");
1683 if (!test_DatabaseDeltas(samsync_state, mem_ctx)) {
1684 printf("DatabaseDeltas failed\n");
1688 if (!test_DatabaseSync2(samsync_state->p, mem_ctx, samsync_state->creds)) {
1689 printf("DatabaseSync2 failed\n");
1694 torture_leave_domain(torture, join_ctx);
1695 torture_leave_domain(torture, join_ctx2);
1696 torture_leave_domain(torture, user_ctx);
1698 talloc_free(mem_ctx);