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;
62 ninfo.identity_info.domain_name.string = domain;
63 ninfo.identity_info.parameter_control = 0;
64 ninfo.identity_info.logon_id_low = 0;
65 ninfo.identity_info.logon_id_high = 0;
66 ninfo.identity_info.account_name.string = account_name;
67 ninfo.identity_info.workstation.string = workstation;
68 generate_random_buffer(ninfo.challenge,
69 sizeof(ninfo.challenge));
72 ninfo.nt.data = talloc_array(mem_ctx, uint8_t, 24);
73 SMBOWFencrypt(nt_hash->hash, ninfo.challenge, ninfo.nt.data);
81 ninfo.lm.data = talloc_array(mem_ctx, uint8_t, 24);
82 SMBOWFencrypt(lm_hash->hash, ninfo.challenge, ninfo.lm.data);
88 r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
89 r.in.computer_name = workstation;
90 r.in.credential = &auth;
91 r.in.return_authenticator = &auth2;
93 r.in.logon.network = &ninfo;
96 creds_client_authenticator(creds, &auth);
98 r.in.validation_level = 3;
100 status = dcerpc_netr_LogonSamLogon(p, mem_ctx, &r);
102 if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
103 printf("Credential chaining failed\n");
107 *info3 = r.out.validation.sam3;
113 struct samsync_state {
114 /* we remember the sequence numbers so we can easily do a DatabaseDelta */
116 const char *domain_name[2];
117 struct samsync_secret *secrets;
118 struct samsync_trusted_domain *trusted_domains;
119 struct creds_CredentialState *creds;
120 struct creds_CredentialState *creds_netlogon_wksta;
121 struct policy_handle *connect_handle;
122 struct policy_handle *domain_handle[2];
123 struct dom_sid *sid[2];
124 struct dcerpc_pipe *p;
125 struct dcerpc_pipe *p_netlogon_wksta;
126 struct dcerpc_pipe *p_samr;
127 struct dcerpc_pipe *p_lsa;
128 struct policy_handle *lsa_handle;
131 struct samsync_secret {
132 struct samsync_secret *prev, *next;
138 struct samsync_trusted_domain {
139 struct samsync_trusted_domain *prev, *next;
144 static struct policy_handle *samsync_open_domain(TALLOC_CTX *mem_ctx,
145 struct samsync_state *samsync_state,
147 struct dom_sid **sid)
149 struct lsa_String name;
150 struct samr_OpenDomain o;
151 struct samr_LookupDomain l;
152 struct policy_handle *domain_handle = talloc(mem_ctx, struct policy_handle);
155 name.string = domain;
156 l.in.connect_handle = samsync_state->connect_handle;
157 l.in.domain_name = &name;
159 nt_status = dcerpc_samr_LookupDomain(samsync_state->p_samr, mem_ctx, &l);
160 if (!NT_STATUS_IS_OK(nt_status)) {
161 printf("LookupDomain failed - %s\n", nt_errstr(nt_status));
165 o.in.connect_handle = samsync_state->connect_handle;
166 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
167 o.in.sid = l.out.sid;
168 o.out.domain_handle = domain_handle;
174 nt_status = dcerpc_samr_OpenDomain(samsync_state->p_samr, mem_ctx, &o);
175 if (!NT_STATUS_IS_OK(nt_status)) {
176 printf("OpenDomain failed - %s\n", nt_errstr(nt_status));
180 return domain_handle;
183 static struct sec_desc_buf *samsync_query_samr_sec_desc(TALLOC_CTX *mem_ctx,
184 struct samsync_state *samsync_state,
185 struct policy_handle *handle)
187 struct samr_QuerySecurity r;
190 r.in.handle = handle;
193 status = dcerpc_samr_QuerySecurity(samsync_state->p_samr, mem_ctx, &r);
194 if (!NT_STATUS_IS_OK(status)) {
195 printf("SAMR QuerySecurity failed - %s\n", nt_errstr(status));
202 static struct sec_desc_buf *samsync_query_lsa_sec_desc(TALLOC_CTX *mem_ctx,
203 struct samsync_state *samsync_state,
204 struct policy_handle *handle)
206 struct lsa_QuerySecurity r;
207 struct sec_desc_buf *sdbuf = NULL;
210 r.in.handle = handle;
212 r.out.sdbuf = &sdbuf;
214 status = dcerpc_lsa_QuerySecurity(samsync_state->p_lsa, mem_ctx, &r);
215 if (!NT_STATUS_IS_OK(status)) {
216 printf("LSA QuerySecurity failed - %s\n", nt_errstr(status));
223 #define TEST_UINT64_EQUAL(i1, i2) do {\
225 printf("%s: uint64 mismatch: " #i1 ": 0x%016llx (%lld) != " #i2 ": 0x%016llx (%lld)\n", \
227 (long long)i1, (long long)i1, \
228 (long long)i2, (long long)i2);\
232 #define TEST_INT_EQUAL(i1, i2) do {\
234 printf("%s: integer mismatch: " #i1 ": 0x%08x (%d) != " #i2 ": 0x%08x (%d)\n", \
235 __location__, i1, i1, i2, i2); \
239 #define TEST_TIME_EQUAL(t1, t2) do {\
241 printf("%s: NTTIME mismatch: " #t1 ":%s != " #t2 ": %s\n", \
242 __location__, nt_time_string(mem_ctx, t1), nt_time_string(mem_ctx, t2));\
247 #define TEST_STRING_EQUAL(s1, s2) do {\
248 if (!((!s1.string || s1.string[0]=='\0') && (!s2.string || s2.string[0]=='\0')) \
249 && strcmp_safe(s1.string, s2.string) != 0) {\
250 printf("%s: string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
251 __location__, s1.string, s2.string);\
256 #define TEST_SID_EQUAL(s1, s2) do {\
257 if (!dom_sid_equal(s1, s2)) {\
258 printf("%s: dom_sid mismatch: " #s1 ":%s != " #s2 ": %s\n", \
259 __location__, dom_sid_string(mem_ctx, s1), dom_sid_string(mem_ctx, s2));\
264 /* The ~SEC_DESC_SACL_PRESENT is because we don't, as administrator,
265 * get back the SACL part of the SD when we ask over SAMR */
267 #define TEST_SEC_DESC_EQUAL(sd1, pipe, handle) do {\
268 struct sec_desc_buf *sdbuf = samsync_query_ ##pipe## _sec_desc(mem_ctx, samsync_state, \
270 if (!sdbuf || !sdbuf->sd) { \
271 printf("Could not obtain security descriptor to match " #sd1 "\n");\
274 if (!security_descriptor_mask_equal(sd1.sd, sdbuf->sd, \
275 ~SEC_DESC_SACL_PRESENT)) {\
276 printf("Security Descriptor Mismatch for %s:\n", #sd1);\
277 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamSync", sd1.sd);\
278 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamR", sdbuf->sd);\
284 static bool samsync_handle_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
285 int database_id, struct netr_DELTA_ENUM *delta)
287 struct netr_DELTA_DOMAIN *domain = delta->delta_union.domain;
288 struct dom_sid *dom_sid;
289 struct samr_QueryDomainInfo q[14]; /* q[0] will be unused simple for clarity */
290 uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
295 samsync_state->seq_num[database_id] =
296 domain->sequence_num;
297 switch (database_id) {
298 case SAM_DATABASE_DOMAIN:
300 case SAM_DATABASE_BUILTIN:
301 if (strcasecmp_m("BUILTIN", domain->domain_name.string) != 0) {
302 printf("BUILTIN domain has different name: %s\n", domain->domain_name.string);
305 case SAM_DATABASE_PRIVS:
306 printf("DOMAIN entry on privs DB!\n");
311 if (!samsync_state->domain_name[database_id]) {
312 samsync_state->domain_name[database_id] =
313 talloc_reference(samsync_state, domain->domain_name.string);
315 if (strcasecmp_m(samsync_state->domain_name[database_id], domain->domain_name.string) != 0) {
316 printf("Domain has name varies!: %s != %s\n", samsync_state->domain_name[database_id],
317 domain->domain_name.string);
322 if (!samsync_state->domain_handle[database_id]) {
323 samsync_state->domain_handle[database_id]
324 = talloc_reference(samsync_state,
325 samsync_open_domain(mem_ctx, samsync_state, samsync_state->domain_name[database_id],
328 if (samsync_state->domain_handle[database_id]) {
329 samsync_state->sid[database_id] = talloc_reference(samsync_state, dom_sid);
332 printf("\tsequence_nums[%d/%s]=%llu\n",
333 database_id, domain->domain_name.string,
334 (long long)samsync_state->seq_num[database_id]);
336 for (i=0;i<ARRAY_SIZE(levels);i++) {
337 q[levels[i]].in.domain_handle = samsync_state->domain_handle[database_id];
338 q[levels[i]].in.level = levels[i];
340 nt_status = dcerpc_samr_QueryDomainInfo(samsync_state->p_samr, mem_ctx, &q[levels[i]]);
342 if (!NT_STATUS_IS_OK(nt_status)) {
343 printf("QueryDomainInfo level %u failed - %s\n",
344 q[levels[i]].in.level, nt_errstr(nt_status));
349 TEST_STRING_EQUAL(q[5].out.info->info5.domain_name, domain->domain_name);
351 TEST_STRING_EQUAL(q[2].out.info->general.oem_information, domain->oem_information);
352 TEST_STRING_EQUAL(q[4].out.info->oem.oem_information, domain->oem_information);
353 TEST_TIME_EQUAL(q[2].out.info->general.force_logoff_time, domain->force_logoff_time);
354 TEST_TIME_EQUAL(q[3].out.info->info3.force_logoff_time, domain->force_logoff_time);
356 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_length, domain->min_password_length);
357 TEST_TIME_EQUAL(q[1].out.info->info1.password_history_length, domain->password_history_length);
358 TEST_TIME_EQUAL(q[1].out.info->info1.max_password_age, domain->max_password_age);
359 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_age, domain->min_password_age);
361 TEST_UINT64_EQUAL(q[8].out.info->info8.sequence_num,
362 domain->sequence_num);
363 TEST_TIME_EQUAL(q[8].out.info->info8.domain_create_time,
364 domain->domain_create_time);
365 TEST_TIME_EQUAL(q[13].out.info->info13.domain_create_time,
366 domain->domain_create_time);
368 TEST_SEC_DESC_EQUAL(domain->sdbuf, samr, samsync_state->domain_handle[database_id]);
373 static bool samsync_handle_policy(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
374 int database_id, struct netr_DELTA_ENUM *delta)
376 struct netr_DELTA_POLICY *policy = delta->delta_union.policy;
378 samsync_state->seq_num[database_id] =
379 policy->sequence_num;
381 if (!samsync_state->domain_name[SAM_DATABASE_DOMAIN]) {
382 samsync_state->domain_name[SAM_DATABASE_DOMAIN] =
383 talloc_reference(samsync_state, policy->primary_domain_name.string);
385 if (strcasecmp_m(samsync_state->domain_name[SAM_DATABASE_DOMAIN], policy->primary_domain_name.string) != 0) {
386 printf("PRIMARY domain has name varies between DOMAIN and POLICY!: %s != %s\n", samsync_state->domain_name[SAM_DATABASE_DOMAIN],
387 policy->primary_domain_name.string);
392 if (!dom_sid_equal(samsync_state->sid[SAM_DATABASE_DOMAIN], policy->sid)) {
393 printf("Domain SID from POLICY (%s) does not match domain sid from SAMR (%s)\n",
394 dom_sid_string(mem_ctx, policy->sid), dom_sid_string(mem_ctx, samsync_state->sid[SAM_DATABASE_DOMAIN]));
398 printf("\tsequence_nums[%d/PRIVS]=%llu\n",
400 (long long)samsync_state->seq_num[database_id]);
404 static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
405 int database_id, struct netr_DELTA_ENUM *delta)
407 uint32_t rid = delta->delta_id_union.rid;
408 struct netr_DELTA_USER *user = delta->delta_union.user;
409 struct netr_SamInfo3 *info3;
410 struct samr_Password lm_hash;
411 struct samr_Password nt_hash;
412 struct samr_Password *lm_hash_p = NULL;
413 struct samr_Password *nt_hash_p = NULL;
414 const char *domain = samsync_state->domain_name[database_id];
415 const char *username = user->account_name.string;
419 struct samr_OpenUser r;
420 struct samr_QueryUserInfo q;
421 struct policy_handle user_handle;
423 struct samr_GetGroupsForUser getgroups;
424 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
425 printf("SamSync needs domain information before the users\n");
429 r.in.domain_handle = samsync_state->domain_handle[database_id];
430 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
432 r.out.user_handle = &user_handle;
434 nt_status = dcerpc_samr_OpenUser(samsync_state->p_samr, mem_ctx, &r);
435 if (!NT_STATUS_IS_OK(nt_status)) {
436 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
440 q.in.user_handle = &user_handle;
443 TEST_SEC_DESC_EQUAL(user->sdbuf, samr, &user_handle);
445 nt_status = dcerpc_samr_QueryUserInfo(samsync_state->p_samr, mem_ctx, &q);
446 if (!NT_STATUS_IS_OK(nt_status)) {
447 printf("QueryUserInfo level %u failed - %s\n",
448 q.in.level, nt_errstr(nt_status));
452 getgroups.in.user_handle = &user_handle;
454 nt_status = dcerpc_samr_GetGroupsForUser(samsync_state->p_samr, mem_ctx, &getgroups);
455 if (!NT_STATUS_IS_OK(nt_status)) {
456 printf("GetGroupsForUser failed - %s\n",
457 nt_errstr(nt_status));
461 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &user_handle)) {
462 printf("samr_handle_Close failed - %s\n",
463 nt_errstr(nt_status));
470 if (!NT_STATUS_IS_OK(nt_status)) {
471 printf("QueryUserInfo level %u failed - %s\n",
472 q.in.level, nt_errstr(nt_status));
476 TEST_STRING_EQUAL(q.out.info->info21.account_name, user->account_name);
477 TEST_STRING_EQUAL(q.out.info->info21.full_name, user->full_name);
478 TEST_INT_EQUAL(q.out.info->info21.rid, user->rid);
479 TEST_INT_EQUAL(q.out.info->info21.primary_gid, user->primary_gid);
480 TEST_STRING_EQUAL(q.out.info->info21.home_directory, user->home_directory);
481 TEST_STRING_EQUAL(q.out.info->info21.home_drive, user->home_drive);
482 TEST_STRING_EQUAL(q.out.info->info21.logon_script, user->logon_script);
483 TEST_STRING_EQUAL(q.out.info->info21.description, user->description);
484 TEST_STRING_EQUAL(q.out.info->info21.workstations, user->workstations);
486 TEST_TIME_EQUAL(q.out.info->info21.last_logon, user->last_logon);
487 TEST_TIME_EQUAL(q.out.info->info21.last_logoff, user->last_logoff);
490 TEST_INT_EQUAL(q.out.info->info21.logon_hours.units_per_week,
491 user->logon_hours.units_per_week);
493 if (memcmp(q.out.info->info21.logon_hours.bits, user->logon_hours.bits,
494 q.out.info->info21.logon_hours.units_per_week/8) != 0) {
495 printf("Logon hours mismatch\n");
500 TEST_INT_EQUAL(q.out.info->info21.bad_password_count,
501 user->bad_password_count);
502 TEST_INT_EQUAL(q.out.info->info21.logon_count,
505 TEST_TIME_EQUAL(q.out.info->info21.last_password_change,
506 user->last_password_change);
507 TEST_TIME_EQUAL(q.out.info->info21.acct_expiry,
510 TEST_INT_EQUAL((q.out.info->info21.acct_flags & ~ACB_PW_EXPIRED), user->acct_flags);
511 if (user->acct_flags & ACB_PWNOEXP) {
512 if (q.out.info->info21.acct_flags & ACB_PW_EXPIRED) {
513 printf("ACB flags mismatch: both expired and no expiry!\n");
516 if (q.out.info->info21.force_password_change != (NTTIME)0x7FFFFFFFFFFFFFFFULL) {
517 printf("ACB flags mismatch: no password expiry, but force password change 0x%016llx (%lld) != 0x%016llx (%lld)\n",
518 (unsigned long long)q.out.info->info21.force_password_change,
519 (unsigned long long)q.out.info->info21.force_password_change,
520 (unsigned long long)0x7FFFFFFFFFFFFFFFULL, (unsigned long long)0x7FFFFFFFFFFFFFFFULL
526 TEST_INT_EQUAL(q.out.info->info21.nt_password_set, user->nt_password_present);
527 TEST_INT_EQUAL(q.out.info->info21.lm_password_set, user->lm_password_present);
528 TEST_INT_EQUAL(q.out.info->info21.password_expired, user->password_expired);
530 TEST_STRING_EQUAL(q.out.info->info21.comment, user->comment);
531 TEST_STRING_EQUAL(q.out.info->info21.parameters, user->parameters);
533 TEST_INT_EQUAL(q.out.info->info21.country_code, user->country_code);
534 TEST_INT_EQUAL(q.out.info->info21.code_page, user->code_page);
536 TEST_STRING_EQUAL(q.out.info->info21.profile_path, user->profile_path);
538 if (user->lm_password_present) {
539 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
540 lm_hash_p = &lm_hash;
542 if (user->nt_password_present) {
543 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
544 nt_hash_p = &nt_hash;
547 if (user->user_private_info.SensitiveData) {
549 struct netr_USER_KEYS keys;
550 enum ndr_err_code ndr_err;
551 data.data = user->user_private_info.SensitiveData;
552 data.length = user->user_private_info.DataLength;
553 creds_arcfour_crypt(samsync_state->creds, data.data, data.length);
554 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);
555 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
556 if (keys.keys.keys2.lmpassword.length == 16) {
557 sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
558 lm_hash_p = &lm_hash;
560 if (keys.keys.keys2.ntpassword.length == 16) {
561 sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
562 nt_hash_p = &nt_hash;
565 printf("Failed to parse Sensitive Data for %s:\n", username);
567 dump_data(0, data.data, data.length);
574 DATA_BLOB nt_hash_blob = data_blob_const(nt_hash_p, 16);
575 DEBUG(100,("ACCOUNT [%s\\%-25s] NTHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string(mem_ctx, &nt_hash_blob)));
578 DATA_BLOB lm_hash_blob = data_blob_const(lm_hash_p, 16);
579 DEBUG(100,("ACCOUNT [%s\\%-25s] LMHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string(mem_ctx, &lm_hash_blob)));
582 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta,
585 TEST_WKSTA_MACHINE_NAME,
590 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED)) {
591 if (user->acct_flags & ACB_DISABLED) {
594 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)) {
595 if (user->acct_flags & ACB_WSTRUST) {
598 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT)) {
599 if (user->acct_flags & ACB_SVRTRUST) {
602 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
603 if (user->acct_flags & ACB_DOMTRUST) {
606 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
607 if (user->acct_flags & ACB_DOMTRUST) {
610 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT)) {
611 if (user->acct_flags & ACB_AUTOLOCK) {
614 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED)) {
615 if (q.out.info->info21.acct_flags & ACB_PW_EXPIRED) {
618 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
619 if (!lm_hash_p && !nt_hash_p) {
622 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE)) {
623 /* We would need to know the server's current time to test this properly */
625 } else if (NT_STATUS_IS_OK(nt_status)) {
626 TEST_INT_EQUAL(user->rid, info3->base.rid);
627 TEST_INT_EQUAL(user->primary_gid, info3->base.primary_gid);
628 /* this is 0x0 from NT4 sp6 */
629 if (info3->base.acct_flags) {
630 TEST_INT_EQUAL(user->acct_flags, info3->base.acct_flags);
632 /* this is NULL from NT4 sp6 */
633 if (info3->base.account_name.string) {
634 TEST_STRING_EQUAL(user->account_name, info3->base.account_name);
636 /* this is NULL from Win2k3 */
637 if (info3->base.full_name.string) {
638 TEST_STRING_EQUAL(user->full_name, info3->base.full_name);
640 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
641 TEST_STRING_EQUAL(user->profile_path, info3->base.profile_path);
642 TEST_STRING_EQUAL(user->home_directory, info3->base.home_directory);
643 TEST_STRING_EQUAL(user->home_drive, info3->base.home_drive);
644 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
647 TEST_TIME_EQUAL(user->last_logon, info3->base.last_logon);
648 TEST_TIME_EQUAL(user->acct_expiry, info3->base.acct_expiry);
649 TEST_TIME_EQUAL(user->last_password_change, info3->base.last_password_change);
650 TEST_TIME_EQUAL(q.out.info->info21.force_password_change, info3->base.force_password_change);
652 /* Does the concept of a logoff time ever really
653 * exist? (not in any sensible way, according to the
654 * doco I read -- abartlet) */
656 /* This copes with the two different versions of 0 I see */
657 /* with NT4 sp6 we have the || case */
658 if (!((user->last_logoff == 0)
659 || (info3->base.last_logoff == 0x7fffffffffffffffLL))) {
660 TEST_TIME_EQUAL(user->last_logoff, info3->base.last_logoff);
663 TEST_INT_EQUAL(getgroups.out.rids->count, info3->base.groups.count);
664 if (getgroups.out.rids->count == info3->base.groups.count) {
666 int count = getgroups.out.rids->count;
667 bool *matched = talloc_zero_array(mem_ctx, bool, getgroups.out.rids->count);
669 for (i = 0; i < count; i++) {
670 for (j = 0; j < count; j++) {
671 if ((getgroups.out.rids->rids[i].rid ==
672 info3->base.groups.rids[j].rid)
673 && (getgroups.out.rids->rids[i].attributes ==
674 info3->base.groups.rids[j].attributes)) {
680 for (i = 0; i < getgroups.out.rids->count; i++) {
681 if (matched[i] == false) {
683 printf("Could not find group RID %u found in getgroups in NETLOGON reply\n",
684 getgroups.out.rids->rids[i].rid);
690 printf("Could not validate password for user %s\\%s: %s\n",
691 domain, username, nt_errstr(nt_status));
697 static bool samsync_handle_alias(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
698 int database_id, struct netr_DELTA_ENUM *delta)
700 uint32_t rid = delta->delta_id_union.rid;
701 struct netr_DELTA_ALIAS *alias = delta->delta_union.alias;
705 struct samr_OpenAlias r;
706 struct samr_QueryAliasInfo q;
707 struct policy_handle alias_handle;
709 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
710 printf("SamSync needs domain information before the users\n");
714 r.in.domain_handle = samsync_state->domain_handle[database_id];
715 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
717 r.out.alias_handle = &alias_handle;
719 nt_status = dcerpc_samr_OpenAlias(samsync_state->p_samr, mem_ctx, &r);
720 if (!NT_STATUS_IS_OK(nt_status)) {
721 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
725 q.in.alias_handle = &alias_handle;
728 TEST_SEC_DESC_EQUAL(alias->sdbuf, samr, &alias_handle);
730 nt_status = dcerpc_samr_QueryAliasInfo(samsync_state->p_samr, mem_ctx, &q);
731 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &alias_handle)) {
735 if (!NT_STATUS_IS_OK(nt_status)) {
736 printf("QueryAliasInfo level %u failed - %s\n",
737 q.in.level, nt_errstr(nt_status));
741 TEST_STRING_EQUAL(q.out.info->all.name, alias->alias_name);
742 TEST_STRING_EQUAL(q.out.info->all.description, alias->description);
746 static bool samsync_handle_group(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
747 int database_id, struct netr_DELTA_ENUM *delta)
749 uint32_t rid = delta->delta_id_union.rid;
750 struct netr_DELTA_GROUP *group = delta->delta_union.group;
754 struct samr_OpenGroup r;
755 struct samr_QueryGroupInfo q;
756 struct policy_handle group_handle;
758 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
759 printf("SamSync needs domain information before the users\n");
763 r.in.domain_handle = samsync_state->domain_handle[database_id];
764 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
766 r.out.group_handle = &group_handle;
768 nt_status = dcerpc_samr_OpenGroup(samsync_state->p_samr, mem_ctx, &r);
769 if (!NT_STATUS_IS_OK(nt_status)) {
770 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
774 q.in.group_handle = &group_handle;
777 TEST_SEC_DESC_EQUAL(group->sdbuf, samr, &group_handle);
779 nt_status = dcerpc_samr_QueryGroupInfo(samsync_state->p_samr, mem_ctx, &q);
780 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &group_handle)) {
784 if (!NT_STATUS_IS_OK(nt_status)) {
785 printf("QueryGroupInfo level %u failed - %s\n",
786 q.in.level, nt_errstr(nt_status));
790 TEST_STRING_EQUAL(q.out.info->all.name, group->group_name);
791 TEST_INT_EQUAL(q.out.info->all.attributes, group->attributes);
792 TEST_STRING_EQUAL(q.out.info->all.description, group->description);
796 static bool samsync_handle_secret(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
797 int database_id, struct netr_DELTA_ENUM *delta)
799 struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
800 const char *name = delta->delta_id_union.name;
801 struct samsync_secret *new = talloc(samsync_state, struct samsync_secret);
802 struct samsync_secret *old = talloc(mem_ctx, struct samsync_secret);
803 struct lsa_QuerySecret q;
804 struct lsa_OpenSecret o;
805 struct policy_handle sec_handle;
806 struct lsa_DATA_BUF_PTR bufp1;
807 struct lsa_DATA_BUF_PTR bufp2;
811 DATA_BLOB lsa_blob1, lsa_blob_out, session_key;
814 creds_arcfour_crypt(samsync_state->creds, secret->current_cipher.cipher_data,
815 secret->current_cipher.maxlen);
817 creds_arcfour_crypt(samsync_state->creds, secret->old_cipher.cipher_data,
818 secret->old_cipher.maxlen);
820 new->name = talloc_reference(new, name);
821 new->secret = data_blob_talloc(new, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
822 new->mtime = secret->current_cipher_set_time;
824 new = talloc_reference(samsync_state, new);
825 DLIST_ADD(samsync_state->secrets, new);
827 old->name = talloc_reference(old, name);
828 old->secret = data_blob_const(secret->old_cipher.cipher_data, secret->old_cipher.maxlen);
829 old->mtime = secret->old_cipher_set_time;
831 o.in.handle = samsync_state->lsa_handle;
832 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
833 o.in.name.string = name;
834 o.out.sec_handle = &sec_handle;
836 status = dcerpc_lsa_OpenSecret(samsync_state->p_lsa, mem_ctx, &o);
837 if (!NT_STATUS_IS_OK(status)) {
838 printf("OpenSecret failed - %s\n", nt_errstr(status));
843 We would like to do this, but it is NOT_SUPPORTED on win2k3
844 TEST_SEC_DESC_EQUAL(secret->sdbuf, lsa, &sec_handle);
846 status = dcerpc_fetch_session_key(samsync_state->p_lsa, &session_key);
847 if (!NT_STATUS_IS_OK(status)) {
848 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
853 ZERO_STRUCT(new_mtime);
854 ZERO_STRUCT(old_mtime);
856 /* fetch the secret back again */
857 q.in.sec_handle = &sec_handle;
858 q.in.new_val = &bufp1;
859 q.in.new_mtime = &new_mtime;
860 q.in.old_val = &bufp2;
861 q.in.old_mtime = &old_mtime;
866 status = dcerpc_lsa_QuerySecret(samsync_state->p_lsa, mem_ctx, &q);
867 if (NT_STATUS_EQUAL(NT_STATUS_ACCESS_DENIED, status)) {
868 /* some things are just off limits */
870 } else if (!NT_STATUS_IS_OK(status)) {
871 printf("QuerySecret failed - %s\n", nt_errstr(status));
875 if (q.out.old_val->buf == NULL) {
876 /* probably just not available due to ACLs */
878 lsa_blob1.data = q.out.old_val->buf->data;
879 lsa_blob1.length = q.out.old_val->buf->length;
881 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
882 if (!NT_STATUS_IS_OK(status)) {
883 printf("Failed to decrypt secrets OLD blob: %s\n", nt_errstr(status));
887 if (!q.out.old_mtime) {
888 printf("OLD mtime not available on LSA for secret %s\n", old->name);
891 if (old->mtime != *q.out.old_mtime) {
892 printf("OLD mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
893 old->name, nt_time_string(mem_ctx, old->mtime),
894 nt_time_string(mem_ctx, *q.out.old_mtime));
898 if (old->secret.length != lsa_blob_out.length) {
899 printf("Returned secret %s doesn't match: %d != %d\n",
900 old->name, (int)old->secret.length, (int)lsa_blob_out.length);
902 } else if (memcmp(lsa_blob_out.data,
903 old->secret.data, old->secret.length) != 0) {
904 printf("Returned secret %s doesn't match: \n",
906 DEBUG(1, ("SamSync Secret:\n"));
907 dump_data(1, old->secret.data, old->secret.length);
908 DEBUG(1, ("LSA Secret:\n"));
909 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
915 if (q.out.new_val->buf == NULL) {
916 /* probably just not available due to ACLs */
918 lsa_blob1.data = q.out.new_val->buf->data;
919 lsa_blob1.length = q.out.new_val->buf->length;
921 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
922 if (!NT_STATUS_IS_OK(status)) {
923 printf("Failed to decrypt secrets OLD blob\n");
927 if (!q.out.new_mtime) {
928 printf("NEW mtime not available on LSA for secret %s\n", new->name);
931 if (new->mtime != *q.out.new_mtime) {
932 printf("NEW mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
933 new->name, nt_time_string(mem_ctx, new->mtime),
934 nt_time_string(mem_ctx, *q.out.new_mtime));
938 if (new->secret.length != lsa_blob_out.length) {
939 printf("Returned secret %s doesn't match: %d != %d\n",
940 new->name, (int)new->secret.length, (int)lsa_blob_out.length);
942 } else if (memcmp(lsa_blob_out.data,
943 new->secret.data, new->secret.length) != 0) {
944 printf("Returned secret %s doesn't match: \n",
946 DEBUG(1, ("SamSync Secret:\n"));
947 dump_data(1, new->secret.data, new->secret.length);
948 DEBUG(1, ("LSA Secret:\n"));
949 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
957 static bool samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
958 int database_id, struct netr_DELTA_ENUM *delta)
962 struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
963 struct dom_sid *dom_sid = delta->delta_id_union.sid;
965 struct samsync_trusted_domain *new = talloc(samsync_state, struct samsync_trusted_domain);
966 struct lsa_OpenTrustedDomain t;
967 struct policy_handle trustdom_handle;
968 struct lsa_QueryTrustedDomainInfo q;
969 union lsa_TrustedDomainInfo *info[9];
970 union lsa_TrustedDomainInfo *_info = NULL;
971 int levels [] = {1, 3, 8};
974 new->name = talloc_reference(new, trusted_domain->domain_name.string);
975 new->sid = talloc_reference(new, dom_sid);
977 t.in.handle = samsync_state->lsa_handle;
978 t.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
980 t.out.trustdom_handle = &trustdom_handle;
982 status = dcerpc_lsa_OpenTrustedDomain(samsync_state->p_lsa, mem_ctx, &t);
983 if (!NT_STATUS_IS_OK(status)) {
984 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
988 for (i=0; i< ARRAY_SIZE(levels); i++) {
989 q.in.trustdom_handle = &trustdom_handle;
990 q.in.level = levels[i];
992 status = dcerpc_lsa_QueryTrustedDomainInfo(samsync_state->p_lsa, mem_ctx, &q);
993 if (!NT_STATUS_IS_OK(status)) {
994 if (q.in.level == 8 && NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER)) {
995 info[levels[i]] = NULL;
998 printf("QueryInfoTrustedDomain level %d failed - %s\n",
999 levels[i], nt_errstr(status));
1002 info[levels[i]] = _info;
1006 TEST_SID_EQUAL(info[8]->full_info.info_ex.sid, dom_sid);
1007 TEST_STRING_EQUAL(info[8]->full_info.info_ex.netbios_name, trusted_domain->domain_name);
1009 TEST_STRING_EQUAL(info[1]->name.netbios_name, trusted_domain->domain_name);
1010 TEST_INT_EQUAL(info[3]->posix_offset.posix_offset, trusted_domain->posix_offset);
1012 We would like to do this, but it is NOT_SUPPORTED on win2k3
1013 TEST_SEC_DESC_EQUAL(trusted_domain->sdbuf, lsa, &trustdom_handle);
1015 new = talloc_reference(samsync_state, new);
1016 DLIST_ADD(samsync_state->trusted_domains, new);
1021 static bool samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
1022 int database_id, struct netr_DELTA_ENUM *delta)
1026 struct netr_DELTA_ACCOUNT *account = delta->delta_union.account;
1027 struct dom_sid *dom_sid = delta->delta_id_union.sid;
1029 struct lsa_OpenAccount a;
1030 struct policy_handle acct_handle;
1031 struct lsa_EnumPrivsAccount e;
1032 struct lsa_PrivilegeSet *privs = NULL;
1033 struct lsa_LookupPrivName r;
1037 bool *found_priv_in_lsa;
1039 a.in.handle = samsync_state->lsa_handle;
1040 a.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1042 a.out.acct_handle = &acct_handle;
1044 status = dcerpc_lsa_OpenAccount(samsync_state->p_lsa, mem_ctx, &a);
1045 if (!NT_STATUS_IS_OK(status)) {
1046 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1050 TEST_SEC_DESC_EQUAL(account->sdbuf, lsa, &acct_handle);
1052 found_priv_in_lsa = talloc_zero_array(mem_ctx, bool, account->privilege_entries);
1054 e.in.handle = &acct_handle;
1055 e.out.privs = &privs;
1057 status = dcerpc_lsa_EnumPrivsAccount(samsync_state->p_lsa, mem_ctx, &e);
1058 if (!NT_STATUS_IS_OK(status)) {
1059 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
1063 if ((account->privilege_entries && !privs)) {
1064 printf("Account %s has privileges in SamSync, but not LSA\n",
1065 dom_sid_string(mem_ctx, dom_sid));
1069 if (!account->privilege_entries && privs && privs->count) {
1070 printf("Account %s has privileges in LSA, but not SamSync\n",
1071 dom_sid_string(mem_ctx, dom_sid));
1075 TEST_INT_EQUAL(account->privilege_entries, privs->count);
1077 for (i=0;i< privs->count; i++) {
1079 struct lsa_StringLarge *name = NULL;
1081 r.in.handle = samsync_state->lsa_handle;
1082 r.in.luid = &privs->set[i].luid;
1085 status = dcerpc_lsa_LookupPrivName(samsync_state->p_lsa, mem_ctx, &r);
1086 if (!NT_STATUS_IS_OK(status)) {
1087 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
1092 printf("\nLookupPrivName failed to return a name\n");
1095 for (j=0;j<account->privilege_entries; j++) {
1096 if (strcmp(name->string, account->privilege_name[j].string) == 0) {
1097 found_priv_in_lsa[j] = true;
1102 for (j=0;j<account->privilege_entries; j++) {
1103 if (!found_priv_in_lsa[j]) {
1104 printf("Privilage %s on account %s not found in LSA\n", account->privilege_name[j].string,
1105 dom_sid_string(mem_ctx, dom_sid));
1113 try a netlogon DatabaseSync
1115 static bool test_DatabaseSync(struct torture_context *tctx,
1116 struct samsync_state *samsync_state,
1117 TALLOC_CTX *mem_ctx)
1120 TALLOC_CTX *loop_ctx, *delta_ctx, *trustdom_ctx;
1121 struct netr_DatabaseSync r;
1122 const enum netr_SamDatabaseID database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS};
1125 struct samsync_trusted_domain *t;
1126 struct samsync_secret *s;
1128 const char *domain, *username;
1130 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1131 r.in.computername = TEST_MACHINE_NAME;
1132 r.in.preferredmaximumlength = (uint32_t)-1;
1133 ZERO_STRUCT(r.in.return_authenticator);
1135 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1136 r.in.sync_context = 0;
1137 r.in.database_id = database_ids[i];
1139 printf("Testing DatabaseSync of id %d\n", r.in.database_id);
1142 loop_ctx = talloc_named(mem_ctx, 0, "DatabaseSync loop context");
1143 creds_client_authenticator(samsync_state->creds, &r.in.credential);
1145 status = dcerpc_netr_DatabaseSync(samsync_state->p, loop_ctx, &r);
1146 if (!NT_STATUS_IS_OK(status) &&
1147 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1148 printf("DatabaseSync - %s\n", nt_errstr(status));
1153 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1154 printf("Credential chaining failed\n");
1157 r.in.sync_context = r.out.sync_context;
1159 for (d=0; d < r.out.delta_enum_array->num_deltas; d++) {
1160 delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
1161 switch (r.out.delta_enum_array->delta_enum[d].delta_type) {
1162 case NETR_DELTA_DOMAIN:
1163 if (!samsync_handle_domain(delta_ctx, samsync_state,
1164 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1165 printf("Failed to handle DELTA_DOMAIN\n");
1169 case NETR_DELTA_GROUP:
1170 if (!samsync_handle_group(delta_ctx, samsync_state,
1171 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1172 printf("Failed to handle DELTA_USER\n");
1176 case NETR_DELTA_USER:
1177 if (!samsync_handle_user(tctx, delta_ctx, samsync_state,
1178 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1179 printf("Failed to handle DELTA_USER\n");
1183 case NETR_DELTA_ALIAS:
1184 if (!samsync_handle_alias(delta_ctx, samsync_state,
1185 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1186 printf("Failed to handle DELTA_ALIAS\n");
1190 case NETR_DELTA_POLICY:
1191 if (!samsync_handle_policy(delta_ctx, samsync_state,
1192 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1193 printf("Failed to handle DELTA_POLICY\n");
1197 case NETR_DELTA_TRUSTED_DOMAIN:
1198 if (!samsync_handle_trusted_domain(delta_ctx, samsync_state,
1199 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1200 printf("Failed to handle DELTA_TRUSTED_DOMAIN\n");
1204 case NETR_DELTA_ACCOUNT:
1205 if (!samsync_handle_account(delta_ctx, samsync_state,
1206 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1207 printf("Failed to handle DELTA_ACCOUNT\n");
1211 case NETR_DELTA_SECRET:
1212 if (!samsync_handle_secret(delta_ctx, samsync_state,
1213 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1214 printf("Failed to handle DELTA_SECRET\n");
1218 case NETR_DELTA_GROUP_MEMBER:
1219 case NETR_DELTA_ALIAS_MEMBER:
1220 /* These are harder to cross-check, and we expect them */
1222 case NETR_DELTA_DELETE_GROUP:
1223 case NETR_DELTA_RENAME_GROUP:
1224 case NETR_DELTA_DELETE_USER:
1225 case NETR_DELTA_RENAME_USER:
1226 case NETR_DELTA_DELETE_ALIAS:
1227 case NETR_DELTA_RENAME_ALIAS:
1228 case NETR_DELTA_DELETE_TRUST:
1229 case NETR_DELTA_DELETE_ACCOUNT:
1230 case NETR_DELTA_DELETE_SECRET:
1231 case NETR_DELTA_DELETE_GROUP2:
1232 case NETR_DELTA_DELETE_USER2:
1233 case NETR_DELTA_MODIFY_COUNT:
1235 printf("Uxpected delta type %d\n", r.out.delta_enum_array->delta_enum[d].delta_type);
1239 talloc_free(delta_ctx);
1241 talloc_free(loop_ctx);
1242 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1246 domain = samsync_state->domain_name[SAM_DATABASE_DOMAIN];
1248 printf("Never got a DOMAIN object in samsync!\n");
1252 trustdom_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync Trusted domains context");
1254 username = talloc_asprintf(trustdom_ctx, "%s$", domain);
1255 for (t=samsync_state->trusted_domains; t; t=t->next) {
1256 char *secret_name = talloc_asprintf(trustdom_ctx, "G$$%s", t->name);
1257 for (s=samsync_state->secrets; s; s=s->next) {
1258 if (strcasecmp_m(s->name, secret_name) == 0) {
1260 struct samr_Password nt_hash;
1261 mdfour(nt_hash.hash, s->secret.data, s->secret.length);
1263 printf("Checking password for %s\\%s\n", t->name, username);
1264 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1267 TEST_WKSTA_MACHINE_NAME,
1271 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS)) {
1272 printf("Verifiction of trust password to %s failed: %s (the trusted domain is not available)\n",
1273 t->name, nt_errstr(nt_status));
1277 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1278 printf("Verifiction of trust password to %s: should have failed (nologon interdomain trust account), instead: %s\n",
1279 t->name, nt_errstr(nt_status));
1285 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1288 TEST_WKSTA_MACHINE_NAME,
1293 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
1294 printf("Verifiction of trust password to %s: should have failed (wrong password), instead: %s\n",
1295 t->name, nt_errstr(nt_status));
1303 talloc_free(trustdom_ctx);
1309 try a netlogon DatabaseDeltas
1311 static bool test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX *mem_ctx)
1314 TALLOC_CTX *loop_ctx;
1315 struct netr_DatabaseDeltas r;
1316 const uint32_t database_ids[] = {0, 1, 2};
1320 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1321 r.in.computername = TEST_MACHINE_NAME;
1322 r.in.preferredmaximumlength = (uint32_t)-1;
1323 ZERO_STRUCT(r.in.return_authenticator);
1325 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1326 r.in.database_id = database_ids[i];
1327 r.in.sequence_num = samsync_state->seq_num[i];
1329 if (r.in.sequence_num == 0) continue;
1331 /* this shows that the bdc doesn't need to do a single call for
1332 * each seqnumber, and the pdc doesn't need to know about old values
1335 r.in.sequence_num -= 10;
1338 printf("Testing DatabaseDeltas of id %d at %llu\n",
1339 r.in.database_id, (long long)r.in.sequence_num);
1342 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseDeltas loop context");
1343 creds_client_authenticator(samsync_state->creds, &r.in.credential);
1345 status = dcerpc_netr_DatabaseDeltas(samsync_state->p, loop_ctx, &r);
1346 if (!NT_STATUS_IS_OK(status) &&
1347 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) &&
1348 !NT_STATUS_EQUAL(status, NT_STATUS_SYNCHRONIZATION_REQUIRED)) {
1349 printf("DatabaseDeltas - %s\n", nt_errstr(status));
1353 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1354 printf("Credential chaining failed\n");
1357 r.in.sequence_num++;
1358 talloc_free(loop_ctx);
1359 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1367 try a netlogon DatabaseSync2
1369 static bool test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1370 struct creds_CredentialState *creds)
1373 TALLOC_CTX *loop_ctx;
1374 struct netr_DatabaseSync2 r;
1375 const uint32_t database_ids[] = {0, 1, 2};
1379 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1380 r.in.computername = TEST_MACHINE_NAME;
1381 r.in.preferredmaximumlength = (uint32_t)-1;
1382 ZERO_STRUCT(r.in.return_authenticator);
1384 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1385 r.in.sync_context = 0;
1386 r.in.database_id = database_ids[i];
1387 r.in.restart_state = 0;
1389 printf("Testing DatabaseSync2 of id %d\n", r.in.database_id);
1392 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync2 loop context");
1393 creds_client_authenticator(creds, &r.in.credential);
1395 status = dcerpc_netr_DatabaseSync2(p, loop_ctx, &r);
1396 if (!NT_STATUS_IS_OK(status) &&
1397 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1398 printf("DatabaseSync2 - %s\n", nt_errstr(status));
1402 if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
1403 printf("Credential chaining failed\n");
1406 r.in.sync_context = r.out.sync_context;
1407 talloc_free(loop_ctx);
1408 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1416 bool torture_rpc_samsync(struct torture_context *torture)
1419 TALLOC_CTX *mem_ctx;
1421 struct test_join *join_ctx;
1422 struct test_join *join_ctx2;
1423 struct test_join *user_ctx;
1424 const char *machine_password;
1425 const char *wksta_machine_password;
1426 struct dcerpc_binding *b;
1427 struct dcerpc_binding *b_netlogon_wksta;
1428 struct samr_Connect c;
1429 struct samr_SetDomainInfo s;
1430 struct policy_handle *domain_policy;
1432 struct lsa_ObjectAttribute attr;
1433 struct lsa_QosInfo qos;
1434 struct lsa_OpenPolicy2 r;
1435 struct cli_credentials *credentials;
1436 struct cli_credentials *credentials_wksta;
1438 struct samsync_state *samsync_state;
1440 char *test_machine_account;
1442 char *test_wksta_machine_account;
1444 mem_ctx = talloc_init("torture_rpc_netlogon");
1446 test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1447 join_ctx = torture_create_testuser(torture, test_machine_account,
1448 lp_workgroup(torture->lp_ctx), ACB_SVRTRUST,
1451 talloc_free(mem_ctx);
1452 printf("Failed to join as BDC\n");
1456 test_wksta_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_WKSTA_MACHINE_NAME);
1457 join_ctx2 = torture_create_testuser(torture, test_wksta_machine_account, lp_workgroup(torture->lp_ctx), ACB_WSTRUST, &wksta_machine_password);
1459 talloc_free(mem_ctx);
1460 printf("Failed to join as member\n");
1464 user_ctx = torture_create_testuser(torture, TEST_USER_NAME,
1465 lp_workgroup(torture->lp_ctx),
1468 talloc_free(mem_ctx);
1469 printf("Failed to create test account\n");
1473 samsync_state = talloc_zero(mem_ctx, struct samsync_state);
1475 samsync_state->p_samr = torture_join_samr_pipe(join_ctx);
1476 samsync_state->connect_handle = talloc_zero(samsync_state, struct policy_handle);
1477 samsync_state->lsa_handle = talloc_zero(samsync_state, struct policy_handle);
1478 c.in.system_name = NULL;
1479 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1480 c.out.connect_handle = samsync_state->connect_handle;
1482 status = dcerpc_samr_Connect(samsync_state->p_samr, mem_ctx, &c);
1483 if (!NT_STATUS_IS_OK(status)) {
1484 printf("samr_Connect failed\n");
1489 domain_policy = samsync_open_domain(mem_ctx, samsync_state, lp_workgroup(torture->lp_ctx), NULL);
1490 if (!domain_policy) {
1491 printf("samrsync_open_domain failed\n");
1496 s.in.domain_handle = domain_policy;
1498 s.in.info = talloc(mem_ctx, union samr_DomainInfo);
1500 s.in.info->oem.oem_information.string
1501 = talloc_asprintf(mem_ctx,
1502 "Tortured by Samba4: %s",
1503 timestring(mem_ctx, time(NULL)));
1504 status = dcerpc_samr_SetDomainInfo(samsync_state->p_samr, mem_ctx, &s);
1506 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, domain_policy)) {
1511 if (!NT_STATUS_IS_OK(status)) {
1512 printf("SetDomainInfo level %u failed - %s\n",
1513 s.in.level, nt_errstr(status));
1519 status = torture_rpc_connection(torture,
1520 &samsync_state->p_lsa,
1523 if (!NT_STATUS_IS_OK(status)) {
1529 qos.impersonation_level = 2;
1530 qos.context_mode = 1;
1531 qos.effective_only = 0;
1534 attr.root_dir = NULL;
1535 attr.object_name = NULL;
1536 attr.attributes = 0;
1537 attr.sec_desc = NULL;
1538 attr.sec_qos = &qos;
1540 r.in.system_name = "\\";
1542 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1543 r.out.handle = samsync_state->lsa_handle;
1545 status = dcerpc_lsa_OpenPolicy2(samsync_state->p_lsa, mem_ctx, &r);
1546 if (!NT_STATUS_IS_OK(status)) {
1547 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1552 status = torture_rpc_binding(torture, &b);
1553 if (!NT_STATUS_IS_OK(status)) {
1558 b->flags &= ~DCERPC_AUTH_OPTIONS;
1559 b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1561 credentials = cli_credentials_init(mem_ctx);
1563 cli_credentials_set_workstation(credentials, TEST_MACHINE_NAME, CRED_SPECIFIED);
1564 cli_credentials_set_domain(credentials, lp_workgroup(torture->lp_ctx), CRED_SPECIFIED);
1565 cli_credentials_set_username(credentials, test_machine_account, CRED_SPECIFIED);
1566 cli_credentials_set_password(credentials, machine_password, CRED_SPECIFIED);
1567 cli_credentials_set_secure_channel_type(credentials,
1570 status = dcerpc_pipe_connect_b(samsync_state,
1571 &samsync_state->p, b,
1572 &ndr_table_netlogon,
1573 credentials, torture->ev, torture->lp_ctx);
1575 if (!NT_STATUS_IS_OK(status)) {
1576 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
1581 status = dcerpc_schannel_creds(samsync_state->p->conn->security_state.generic_state,
1582 samsync_state, &samsync_state->creds);
1583 if (!NT_STATUS_IS_OK(status)) {
1589 status = torture_rpc_binding(torture, &b_netlogon_wksta);
1590 if (!NT_STATUS_IS_OK(status)) {
1595 b_netlogon_wksta->flags &= ~DCERPC_AUTH_OPTIONS;
1596 b_netlogon_wksta->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1598 credentials_wksta = cli_credentials_init(mem_ctx);
1600 cli_credentials_set_workstation(credentials_wksta, TEST_WKSTA_MACHINE_NAME, CRED_SPECIFIED);
1601 cli_credentials_set_domain(credentials_wksta, lp_workgroup(torture->lp_ctx), CRED_SPECIFIED);
1602 cli_credentials_set_username(credentials_wksta, test_wksta_machine_account, CRED_SPECIFIED);
1603 cli_credentials_set_password(credentials_wksta, wksta_machine_password, CRED_SPECIFIED);
1604 cli_credentials_set_secure_channel_type(credentials_wksta,
1607 status = dcerpc_pipe_connect_b(samsync_state,
1608 &samsync_state->p_netlogon_wksta,
1610 &ndr_table_netlogon,
1611 credentials_wksta, torture->ev, torture->lp_ctx);
1613 if (!NT_STATUS_IS_OK(status)) {
1614 printf("Failed to connect to server as a Workstation: %s\n", nt_errstr(status));
1619 status = dcerpc_schannel_creds(samsync_state->p_netlogon_wksta->conn->security_state.generic_state,
1620 samsync_state, &samsync_state->creds_netlogon_wksta);
1621 if (!NT_STATUS_IS_OK(status)) {
1622 printf("Failed to obtail schanel creds!\n");
1626 if (!test_DatabaseSync(torture, samsync_state, mem_ctx)) {
1627 printf("DatabaseSync failed\n");
1631 if (!test_DatabaseDeltas(samsync_state, mem_ctx)) {
1632 printf("DatabaseDeltas failed\n");
1636 if (!test_DatabaseSync2(samsync_state->p, mem_ctx, samsync_state->creds)) {
1637 printf("DatabaseSync2 failed\n");
1642 torture_leave_domain(torture, join_ctx);
1643 torture_leave_domain(torture, join_ctx2);
1644 torture_leave_domain(torture, user_ctx);
1646 talloc_free(mem_ctx);