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;
209 r.in.handle = handle;
212 status = dcerpc_lsa_QuerySecurity(samsync_state->p_lsa, mem_ctx, &r);
213 if (!NT_STATUS_IS_OK(status)) {
214 printf("LSA QuerySecurity failed - %s\n", nt_errstr(status));
221 #define TEST_UINT64_EQUAL(i1, i2) do {\
223 printf("%s: uint64 mismatch: " #i1 ": 0x%016llx (%lld) != " #i2 ": 0x%016llx (%lld)\n", \
225 (long long)i1, (long long)i1, \
226 (long long)i2, (long long)i2);\
230 #define TEST_INT_EQUAL(i1, i2) do {\
232 printf("%s: integer mismatch: " #i1 ": 0x%08x (%d) != " #i2 ": 0x%08x (%d)\n", \
233 __location__, i1, i1, i2, i2); \
237 #define TEST_TIME_EQUAL(t1, t2) do {\
239 printf("%s: NTTIME mismatch: " #t1 ":%s != " #t2 ": %s\n", \
240 __location__, nt_time_string(mem_ctx, t1), nt_time_string(mem_ctx, t2));\
245 #define TEST_STRING_EQUAL(s1, s2) do {\
246 if (!((!s1.string || s1.string[0]=='\0') && (!s2.string || s2.string[0]=='\0')) \
247 && strcmp_safe(s1.string, s2.string) != 0) {\
248 printf("%s: string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
249 __location__, s1.string, s2.string);\
254 #define TEST_SID_EQUAL(s1, s2) do {\
255 if (!dom_sid_equal(s1, s2)) {\
256 printf("%s: dom_sid mismatch: " #s1 ":%s != " #s2 ": %s\n", \
257 __location__, dom_sid_string(mem_ctx, s1), dom_sid_string(mem_ctx, s2));\
262 /* The ~SEC_DESC_SACL_PRESENT is because we don't, as administrator,
263 * get back the SACL part of the SD when we ask over SAMR */
265 #define TEST_SEC_DESC_EQUAL(sd1, pipe, handle) do {\
266 struct sec_desc_buf *sdbuf = samsync_query_ ##pipe## _sec_desc(mem_ctx, samsync_state, \
268 if (!sdbuf || !sdbuf->sd) { \
269 printf("Could not obtain security descriptor to match " #sd1 "\n");\
272 if (!security_descriptor_mask_equal(sd1.sd, sdbuf->sd, \
273 ~SEC_DESC_SACL_PRESENT)) {\
274 printf("Security Descriptor Mismatch for %s:\n", #sd1);\
275 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamSync", sd1.sd);\
276 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamR", sdbuf->sd);\
282 static bool samsync_handle_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
283 int database_id, struct netr_DELTA_ENUM *delta)
285 struct netr_DELTA_DOMAIN *domain = delta->delta_union.domain;
286 struct dom_sid *dom_sid;
287 struct samr_QueryDomainInfo q[14]; /* q[0] will be unused simple for clarity */
288 uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
293 samsync_state->seq_num[database_id] =
294 domain->sequence_num;
295 switch (database_id) {
296 case SAM_DATABASE_DOMAIN:
298 case SAM_DATABASE_BUILTIN:
299 if (strcasecmp_m("BUILTIN", domain->domain_name.string) != 0) {
300 printf("BUILTIN domain has different name: %s\n", domain->domain_name.string);
303 case SAM_DATABASE_PRIVS:
304 printf("DOMAIN entry on privs DB!\n");
309 if (!samsync_state->domain_name[database_id]) {
310 samsync_state->domain_name[database_id] =
311 talloc_reference(samsync_state, domain->domain_name.string);
313 if (strcasecmp_m(samsync_state->domain_name[database_id], domain->domain_name.string) != 0) {
314 printf("Domain has name varies!: %s != %s\n", samsync_state->domain_name[database_id],
315 domain->domain_name.string);
320 if (!samsync_state->domain_handle[database_id]) {
321 samsync_state->domain_handle[database_id]
322 = talloc_reference(samsync_state,
323 samsync_open_domain(mem_ctx, samsync_state, samsync_state->domain_name[database_id],
326 if (samsync_state->domain_handle[database_id]) {
327 samsync_state->sid[database_id] = talloc_reference(samsync_state, dom_sid);
330 printf("\tsequence_nums[%d/%s]=%llu\n",
331 database_id, domain->domain_name.string,
332 (long long)samsync_state->seq_num[database_id]);
334 for (i=0;i<ARRAY_SIZE(levels);i++) {
335 q[levels[i]].in.domain_handle = samsync_state->domain_handle[database_id];
336 q[levels[i]].in.level = levels[i];
338 nt_status = dcerpc_samr_QueryDomainInfo(samsync_state->p_samr, mem_ctx, &q[levels[i]]);
340 if (!NT_STATUS_IS_OK(nt_status)) {
341 printf("QueryDomainInfo level %u failed - %s\n",
342 q[levels[i]].in.level, nt_errstr(nt_status));
347 TEST_STRING_EQUAL(q[5].out.info->info5.domain_name, domain->domain_name);
349 TEST_STRING_EQUAL(q[2].out.info->info2.comment, domain->comment);
350 TEST_STRING_EQUAL(q[4].out.info->info4.comment, domain->comment);
351 TEST_TIME_EQUAL(q[2].out.info->info2.force_logoff_time, domain->force_logoff_time);
352 TEST_TIME_EQUAL(q[3].out.info->info3.force_logoff_time, domain->force_logoff_time);
354 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_length, domain->min_password_length);
355 TEST_TIME_EQUAL(q[1].out.info->info1.password_history_length, domain->password_history_length);
356 TEST_TIME_EQUAL(q[1].out.info->info1.max_password_age, domain->max_password_age);
357 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_age, domain->min_password_age);
359 TEST_UINT64_EQUAL(q[8].out.info->info8.sequence_num,
360 domain->sequence_num);
361 TEST_TIME_EQUAL(q[8].out.info->info8.domain_create_time,
362 domain->domain_create_time);
363 TEST_TIME_EQUAL(q[13].out.info->info13.domain_create_time,
364 domain->domain_create_time);
366 TEST_SEC_DESC_EQUAL(domain->sdbuf, samr, samsync_state->domain_handle[database_id]);
371 static bool samsync_handle_policy(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
372 int database_id, struct netr_DELTA_ENUM *delta)
374 struct netr_DELTA_POLICY *policy = delta->delta_union.policy;
376 samsync_state->seq_num[database_id] =
377 policy->sequence_num;
379 if (!samsync_state->domain_name[SAM_DATABASE_DOMAIN]) {
380 samsync_state->domain_name[SAM_DATABASE_DOMAIN] =
381 talloc_reference(samsync_state, policy->primary_domain_name.string);
383 if (strcasecmp_m(samsync_state->domain_name[SAM_DATABASE_DOMAIN], policy->primary_domain_name.string) != 0) {
384 printf("PRIMARY domain has name varies between DOMAIN and POLICY!: %s != %s\n", samsync_state->domain_name[SAM_DATABASE_DOMAIN],
385 policy->primary_domain_name.string);
390 if (!dom_sid_equal(samsync_state->sid[SAM_DATABASE_DOMAIN], policy->sid)) {
391 printf("Domain SID from POLICY (%s) does not match domain sid from SAMR (%s)\n",
392 dom_sid_string(mem_ctx, policy->sid), dom_sid_string(mem_ctx, samsync_state->sid[SAM_DATABASE_DOMAIN]));
396 printf("\tsequence_nums[%d/PRIVS]=%llu\n",
398 (long long)samsync_state->seq_num[database_id]);
402 static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
403 int database_id, struct netr_DELTA_ENUM *delta)
405 uint32_t rid = delta->delta_id_union.rid;
406 struct netr_DELTA_USER *user = delta->delta_union.user;
407 struct netr_SamInfo3 *info3;
408 struct samr_Password lm_hash;
409 struct samr_Password nt_hash;
410 struct samr_Password *lm_hash_p = NULL;
411 struct samr_Password *nt_hash_p = NULL;
412 const char *domain = samsync_state->domain_name[database_id];
413 const char *username = user->account_name.string;
417 struct samr_OpenUser r;
418 struct samr_QueryUserInfo q;
419 struct policy_handle user_handle;
421 struct samr_GetGroupsForUser getgroups;
422 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
423 printf("SamSync needs domain information before the users\n");
427 r.in.domain_handle = samsync_state->domain_handle[database_id];
428 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
430 r.out.user_handle = &user_handle;
432 nt_status = dcerpc_samr_OpenUser(samsync_state->p_samr, mem_ctx, &r);
433 if (!NT_STATUS_IS_OK(nt_status)) {
434 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
438 q.in.user_handle = &user_handle;
441 TEST_SEC_DESC_EQUAL(user->sdbuf, samr, &user_handle);
443 nt_status = dcerpc_samr_QueryUserInfo(samsync_state->p_samr, mem_ctx, &q);
444 if (!NT_STATUS_IS_OK(nt_status)) {
445 printf("QueryUserInfo level %u failed - %s\n",
446 q.in.level, nt_errstr(nt_status));
450 getgroups.in.user_handle = &user_handle;
452 nt_status = dcerpc_samr_GetGroupsForUser(samsync_state->p_samr, mem_ctx, &getgroups);
453 if (!NT_STATUS_IS_OK(nt_status)) {
454 printf("GetGroupsForUser failed - %s\n",
455 nt_errstr(nt_status));
459 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &user_handle)) {
460 printf("samr_handle_Close failed - %s\n",
461 nt_errstr(nt_status));
468 if (!NT_STATUS_IS_OK(nt_status)) {
469 printf("QueryUserInfo level %u failed - %s\n",
470 q.in.level, nt_errstr(nt_status));
474 TEST_STRING_EQUAL(q.out.info->info21.account_name, user->account_name);
475 TEST_STRING_EQUAL(q.out.info->info21.full_name, user->full_name);
476 TEST_INT_EQUAL(q.out.info->info21.rid, user->rid);
477 TEST_INT_EQUAL(q.out.info->info21.primary_gid, user->primary_gid);
478 TEST_STRING_EQUAL(q.out.info->info21.home_directory, user->home_directory);
479 TEST_STRING_EQUAL(q.out.info->info21.home_drive, user->home_drive);
480 TEST_STRING_EQUAL(q.out.info->info21.logon_script, user->logon_script);
481 TEST_STRING_EQUAL(q.out.info->info21.description, user->description);
482 TEST_STRING_EQUAL(q.out.info->info21.workstations, user->workstations);
484 TEST_TIME_EQUAL(q.out.info->info21.last_logon, user->last_logon);
485 TEST_TIME_EQUAL(q.out.info->info21.last_logoff, user->last_logoff);
488 TEST_INT_EQUAL(q.out.info->info21.logon_hours.units_per_week,
489 user->logon_hours.units_per_week);
491 if (memcmp(q.out.info->info21.logon_hours.bits, user->logon_hours.bits,
492 q.out.info->info21.logon_hours.units_per_week/8) != 0) {
493 printf("Logon hours mismatch\n");
498 TEST_INT_EQUAL(q.out.info->info21.bad_password_count,
499 user->bad_password_count);
500 TEST_INT_EQUAL(q.out.info->info21.logon_count,
503 TEST_TIME_EQUAL(q.out.info->info21.last_password_change,
504 user->last_password_change);
505 TEST_TIME_EQUAL(q.out.info->info21.acct_expiry,
508 TEST_INT_EQUAL((q.out.info->info21.acct_flags & ~ACB_PW_EXPIRED), user->acct_flags);
509 if (user->acct_flags & ACB_PWNOEXP) {
510 if (q.out.info->info21.acct_flags & ACB_PW_EXPIRED) {
511 printf("ACB flags mismatch: both expired and no expiry!\n");
514 if (q.out.info->info21.force_password_change != (NTTIME)0x7FFFFFFFFFFFFFFFULL) {
515 printf("ACB flags mismatch: no password expiry, but force password change 0x%016llx (%lld) != 0x%016llx (%lld)\n",
516 (unsigned long long)q.out.info->info21.force_password_change,
517 (unsigned long long)q.out.info->info21.force_password_change,
518 (unsigned long long)0x7FFFFFFFFFFFFFFFULL, (unsigned long long)0x7FFFFFFFFFFFFFFFULL
524 TEST_INT_EQUAL(q.out.info->info21.nt_password_set, user->nt_password_present);
525 TEST_INT_EQUAL(q.out.info->info21.lm_password_set, user->lm_password_present);
526 TEST_INT_EQUAL(q.out.info->info21.password_expired, user->password_expired);
528 TEST_STRING_EQUAL(q.out.info->info21.comment, user->comment);
529 TEST_STRING_EQUAL(q.out.info->info21.parameters, user->parameters);
531 TEST_INT_EQUAL(q.out.info->info21.country_code, user->country_code);
532 TEST_INT_EQUAL(q.out.info->info21.code_page, user->code_page);
534 TEST_STRING_EQUAL(q.out.info->info21.profile_path, user->profile_path);
536 if (user->lm_password_present) {
537 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
538 lm_hash_p = &lm_hash;
540 if (user->nt_password_present) {
541 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
542 nt_hash_p = &nt_hash;
545 if (user->user_private_info.SensitiveData) {
547 struct netr_USER_KEYS keys;
548 enum ndr_err_code ndr_err;
549 data.data = user->user_private_info.SensitiveData;
550 data.length = user->user_private_info.DataLength;
551 creds_arcfour_crypt(samsync_state->creds, data.data, data.length);
552 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);
553 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
554 if (keys.keys.keys2.lmpassword.length == 16) {
555 sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
556 lm_hash_p = &lm_hash;
558 if (keys.keys.keys2.ntpassword.length == 16) {
559 sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
560 nt_hash_p = &nt_hash;
563 printf("Failed to parse Sensitive Data for %s:\n", username);
565 dump_data(0, data.data, data.length);
572 DATA_BLOB nt_hash_blob = data_blob_const(nt_hash_p, 16);
573 DEBUG(100,("ACCOUNT [%s\\%-25s] NTHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string(mem_ctx, &nt_hash_blob)));
576 DATA_BLOB lm_hash_blob = data_blob_const(lm_hash_p, 16);
577 DEBUG(100,("ACCOUNT [%s\\%-25s] LMHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string(mem_ctx, &lm_hash_blob)));
580 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta,
583 TEST_WKSTA_MACHINE_NAME,
588 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED)) {
589 if (user->acct_flags & ACB_DISABLED) {
592 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)) {
593 if (user->acct_flags & ACB_WSTRUST) {
596 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT)) {
597 if (user->acct_flags & ACB_SVRTRUST) {
600 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
601 if (user->acct_flags & ACB_DOMTRUST) {
604 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
605 if (user->acct_flags & ACB_DOMTRUST) {
608 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT)) {
609 if (user->acct_flags & ACB_AUTOLOCK) {
612 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED)) {
613 if (q.out.info->info21.acct_flags & ACB_PW_EXPIRED) {
616 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
617 if (!lm_hash_p && !nt_hash_p) {
620 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE)) {
621 /* We would need to know the server's current time to test this properly */
623 } else if (NT_STATUS_IS_OK(nt_status)) {
624 TEST_INT_EQUAL(user->rid, info3->base.rid);
625 TEST_INT_EQUAL(user->primary_gid, info3->base.primary_gid);
626 /* this is 0x0 from NT4 sp6 */
627 if (info3->base.acct_flags) {
628 TEST_INT_EQUAL(user->acct_flags, info3->base.acct_flags);
630 /* this is NULL from NT4 sp6 */
631 if (info3->base.account_name.string) {
632 TEST_STRING_EQUAL(user->account_name, info3->base.account_name);
634 /* this is NULL from Win2k3 */
635 if (info3->base.full_name.string) {
636 TEST_STRING_EQUAL(user->full_name, info3->base.full_name);
638 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
639 TEST_STRING_EQUAL(user->profile_path, info3->base.profile_path);
640 TEST_STRING_EQUAL(user->home_directory, info3->base.home_directory);
641 TEST_STRING_EQUAL(user->home_drive, info3->base.home_drive);
642 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
645 TEST_TIME_EQUAL(user->last_logon, info3->base.last_logon);
646 TEST_TIME_EQUAL(user->acct_expiry, info3->base.acct_expiry);
647 TEST_TIME_EQUAL(user->last_password_change, info3->base.last_password_change);
648 TEST_TIME_EQUAL(q.out.info->info21.force_password_change, info3->base.force_password_change);
650 /* Does the concept of a logoff time ever really
651 * exist? (not in any sensible way, according to the
652 * doco I read -- abartlet) */
654 /* This copes with the two different versions of 0 I see */
655 /* with NT4 sp6 we have the || case */
656 if (!((user->last_logoff == 0)
657 || (info3->base.last_logoff == 0x7fffffffffffffffLL))) {
658 TEST_TIME_EQUAL(user->last_logoff, info3->base.last_logoff);
661 TEST_INT_EQUAL(getgroups.out.rids->count, info3->base.groups.count);
662 if (getgroups.out.rids->count == info3->base.groups.count) {
664 int count = getgroups.out.rids->count;
665 bool *matched = talloc_zero_array(mem_ctx, bool, getgroups.out.rids->count);
667 for (i = 0; i < count; i++) {
668 for (j = 0; j < count; j++) {
669 if ((getgroups.out.rids->rids[i].rid ==
670 info3->base.groups.rids[j].rid)
671 && (getgroups.out.rids->rids[i].attributes ==
672 info3->base.groups.rids[j].attributes)) {
678 for (i = 0; i < getgroups.out.rids->count; i++) {
679 if (matched[i] == false) {
681 printf("Could not find group RID %u found in getgroups in NETLOGON reply\n",
682 getgroups.out.rids->rids[i].rid);
688 printf("Could not validate password for user %s\\%s: %s\n",
689 domain, username, nt_errstr(nt_status));
695 static bool samsync_handle_alias(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
696 int database_id, struct netr_DELTA_ENUM *delta)
698 uint32_t rid = delta->delta_id_union.rid;
699 struct netr_DELTA_ALIAS *alias = delta->delta_union.alias;
703 struct samr_OpenAlias r;
704 struct samr_QueryAliasInfo q;
705 struct policy_handle alias_handle;
707 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
708 printf("SamSync needs domain information before the users\n");
712 r.in.domain_handle = samsync_state->domain_handle[database_id];
713 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
715 r.out.alias_handle = &alias_handle;
717 nt_status = dcerpc_samr_OpenAlias(samsync_state->p_samr, mem_ctx, &r);
718 if (!NT_STATUS_IS_OK(nt_status)) {
719 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
723 q.in.alias_handle = &alias_handle;
726 TEST_SEC_DESC_EQUAL(alias->sdbuf, samr, &alias_handle);
728 nt_status = dcerpc_samr_QueryAliasInfo(samsync_state->p_samr, mem_ctx, &q);
729 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &alias_handle)) {
733 if (!NT_STATUS_IS_OK(nt_status)) {
734 printf("QueryAliasInfo level %u failed - %s\n",
735 q.in.level, nt_errstr(nt_status));
739 TEST_STRING_EQUAL(q.out.info->all.name, alias->alias_name);
740 TEST_STRING_EQUAL(q.out.info->all.description, alias->description);
744 static bool samsync_handle_group(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
745 int database_id, struct netr_DELTA_ENUM *delta)
747 uint32_t rid = delta->delta_id_union.rid;
748 struct netr_DELTA_GROUP *group = delta->delta_union.group;
752 struct samr_OpenGroup r;
753 struct samr_QueryGroupInfo q;
754 struct policy_handle group_handle;
756 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
757 printf("SamSync needs domain information before the users\n");
761 r.in.domain_handle = samsync_state->domain_handle[database_id];
762 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
764 r.out.group_handle = &group_handle;
766 nt_status = dcerpc_samr_OpenGroup(samsync_state->p_samr, mem_ctx, &r);
767 if (!NT_STATUS_IS_OK(nt_status)) {
768 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
772 q.in.group_handle = &group_handle;
775 TEST_SEC_DESC_EQUAL(group->sdbuf, samr, &group_handle);
777 nt_status = dcerpc_samr_QueryGroupInfo(samsync_state->p_samr, mem_ctx, &q);
778 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &group_handle)) {
782 if (!NT_STATUS_IS_OK(nt_status)) {
783 printf("QueryGroupInfo level %u failed - %s\n",
784 q.in.level, nt_errstr(nt_status));
788 TEST_STRING_EQUAL(q.out.info->all.name, group->group_name);
789 TEST_INT_EQUAL(q.out.info->all.attributes, group->attributes);
790 TEST_STRING_EQUAL(q.out.info->all.description, group->description);
794 static bool samsync_handle_secret(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
795 int database_id, struct netr_DELTA_ENUM *delta)
797 struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
798 const char *name = delta->delta_id_union.name;
799 struct samsync_secret *new = talloc(samsync_state, struct samsync_secret);
800 struct samsync_secret *old = talloc(mem_ctx, struct samsync_secret);
801 struct lsa_QuerySecret q;
802 struct lsa_OpenSecret o;
803 struct policy_handle sec_handle;
804 struct lsa_DATA_BUF_PTR bufp1;
805 struct lsa_DATA_BUF_PTR bufp2;
809 DATA_BLOB lsa_blob1, lsa_blob_out, session_key;
812 creds_arcfour_crypt(samsync_state->creds, secret->current_cipher.cipher_data,
813 secret->current_cipher.maxlen);
815 creds_arcfour_crypt(samsync_state->creds, secret->old_cipher.cipher_data,
816 secret->old_cipher.maxlen);
818 new->name = talloc_reference(new, name);
819 new->secret = data_blob_talloc(new, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
820 new->mtime = secret->current_cipher_set_time;
822 new = talloc_reference(samsync_state, new);
823 DLIST_ADD(samsync_state->secrets, new);
825 old->name = talloc_reference(old, name);
826 old->secret = data_blob_const(secret->old_cipher.cipher_data, secret->old_cipher.maxlen);
827 old->mtime = secret->old_cipher_set_time;
829 o.in.handle = samsync_state->lsa_handle;
830 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
831 o.in.name.string = name;
832 o.out.sec_handle = &sec_handle;
834 status = dcerpc_lsa_OpenSecret(samsync_state->p_lsa, mem_ctx, &o);
835 if (!NT_STATUS_IS_OK(status)) {
836 printf("OpenSecret failed - %s\n", nt_errstr(status));
841 We would like to do this, but it is NOT_SUPPORTED on win2k3
842 TEST_SEC_DESC_EQUAL(secret->sdbuf, lsa, &sec_handle);
844 status = dcerpc_fetch_session_key(samsync_state->p_lsa, &session_key);
845 if (!NT_STATUS_IS_OK(status)) {
846 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
851 ZERO_STRUCT(new_mtime);
852 ZERO_STRUCT(old_mtime);
854 /* fetch the secret back again */
855 q.in.sec_handle = &sec_handle;
856 q.in.new_val = &bufp1;
857 q.in.new_mtime = &new_mtime;
858 q.in.old_val = &bufp2;
859 q.in.old_mtime = &old_mtime;
864 status = dcerpc_lsa_QuerySecret(samsync_state->p_lsa, mem_ctx, &q);
865 if (NT_STATUS_EQUAL(NT_STATUS_ACCESS_DENIED, status)) {
866 /* some things are just off limits */
868 } else if (!NT_STATUS_IS_OK(status)) {
869 printf("QuerySecret failed - %s\n", nt_errstr(status));
873 if (q.out.old_val->buf == NULL) {
874 /* probably just not available due to ACLs */
876 lsa_blob1.data = q.out.old_val->buf->data;
877 lsa_blob1.length = q.out.old_val->buf->length;
879 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
880 if (!NT_STATUS_IS_OK(status)) {
881 printf("Failed to decrypt secrets OLD blob: %s\n", nt_errstr(status));
885 if (!q.out.old_mtime) {
886 printf("OLD mtime not available on LSA for secret %s\n", old->name);
889 if (old->mtime != *q.out.old_mtime) {
890 printf("OLD mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
891 old->name, nt_time_string(mem_ctx, old->mtime),
892 nt_time_string(mem_ctx, *q.out.old_mtime));
896 if (old->secret.length != lsa_blob_out.length) {
897 printf("Returned secret %s doesn't match: %d != %d\n",
898 old->name, (int)old->secret.length, (int)lsa_blob_out.length);
900 } else if (memcmp(lsa_blob_out.data,
901 old->secret.data, old->secret.length) != 0) {
902 printf("Returned secret %s doesn't match: \n",
904 DEBUG(1, ("SamSync Secret:\n"));
905 dump_data(1, old->secret.data, old->secret.length);
906 DEBUG(1, ("LSA Secret:\n"));
907 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
913 if (q.out.new_val->buf == NULL) {
914 /* probably just not available due to ACLs */
916 lsa_blob1.data = q.out.new_val->buf->data;
917 lsa_blob1.length = q.out.new_val->buf->length;
919 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
920 if (!NT_STATUS_IS_OK(status)) {
921 printf("Failed to decrypt secrets OLD blob\n");
925 if (!q.out.new_mtime) {
926 printf("NEW mtime not available on LSA for secret %s\n", new->name);
929 if (new->mtime != *q.out.new_mtime) {
930 printf("NEW mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
931 new->name, nt_time_string(mem_ctx, new->mtime),
932 nt_time_string(mem_ctx, *q.out.new_mtime));
936 if (new->secret.length != lsa_blob_out.length) {
937 printf("Returned secret %s doesn't match: %d != %d\n",
938 new->name, (int)new->secret.length, (int)lsa_blob_out.length);
940 } else if (memcmp(lsa_blob_out.data,
941 new->secret.data, new->secret.length) != 0) {
942 printf("Returned secret %s doesn't match: \n",
944 DEBUG(1, ("SamSync Secret:\n"));
945 dump_data(1, new->secret.data, new->secret.length);
946 DEBUG(1, ("LSA Secret:\n"));
947 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
955 static bool samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
956 int database_id, struct netr_DELTA_ENUM *delta)
960 struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
961 struct dom_sid *dom_sid = delta->delta_id_union.sid;
963 struct samsync_trusted_domain *new = talloc(samsync_state, struct samsync_trusted_domain);
964 struct lsa_OpenTrustedDomain t;
965 struct policy_handle trustdom_handle;
966 struct lsa_QueryTrustedDomainInfo q;
967 union lsa_TrustedDomainInfo *info[9];
968 int levels [] = {1, 3, 8};
971 new->name = talloc_reference(new, trusted_domain->domain_name.string);
972 new->sid = talloc_reference(new, dom_sid);
974 t.in.handle = samsync_state->lsa_handle;
975 t.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
977 t.out.trustdom_handle = &trustdom_handle;
979 status = dcerpc_lsa_OpenTrustedDomain(samsync_state->p_lsa, mem_ctx, &t);
980 if (!NT_STATUS_IS_OK(status)) {
981 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
985 for (i=0; i< ARRAY_SIZE(levels); i++) {
986 q.in.trustdom_handle = &trustdom_handle;
987 q.in.level = levels[i];
988 status = dcerpc_lsa_QueryTrustedDomainInfo(samsync_state->p_lsa, mem_ctx, &q);
989 if (!NT_STATUS_IS_OK(status)) {
990 if (q.in.level == 8 && NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER)) {
991 info[levels[i]] = NULL;
994 printf("QueryInfoTrustedDomain level %d failed - %s\n",
995 levels[i], nt_errstr(status));
998 info[levels[i]] = q.out.info;
1002 TEST_SID_EQUAL(info[8]->full_info.info_ex.sid, dom_sid);
1003 TEST_STRING_EQUAL(info[8]->full_info.info_ex.netbios_name, trusted_domain->domain_name);
1005 TEST_STRING_EQUAL(info[1]->name.netbios_name, trusted_domain->domain_name);
1006 TEST_INT_EQUAL(info[3]->posix_offset.posix_offset, trusted_domain->posix_offset);
1008 We would like to do this, but it is NOT_SUPPORTED on win2k3
1009 TEST_SEC_DESC_EQUAL(trusted_domain->sdbuf, lsa, &trustdom_handle);
1011 new = talloc_reference(samsync_state, new);
1012 DLIST_ADD(samsync_state->trusted_domains, new);
1017 static bool samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
1018 int database_id, struct netr_DELTA_ENUM *delta)
1022 struct netr_DELTA_ACCOUNT *account = delta->delta_union.account;
1023 struct dom_sid *dom_sid = delta->delta_id_union.sid;
1025 struct lsa_OpenAccount a;
1026 struct policy_handle acct_handle;
1027 struct lsa_EnumPrivsAccount e;
1028 struct lsa_LookupPrivName r;
1032 bool *found_priv_in_lsa;
1034 a.in.handle = samsync_state->lsa_handle;
1035 a.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1037 a.out.acct_handle = &acct_handle;
1039 status = dcerpc_lsa_OpenAccount(samsync_state->p_lsa, mem_ctx, &a);
1040 if (!NT_STATUS_IS_OK(status)) {
1041 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1045 TEST_SEC_DESC_EQUAL(account->sdbuf, lsa, &acct_handle);
1047 found_priv_in_lsa = talloc_zero_array(mem_ctx, bool, account->privilege_entries);
1049 e.in.handle = &acct_handle;
1051 status = dcerpc_lsa_EnumPrivsAccount(samsync_state->p_lsa, mem_ctx, &e);
1052 if (!NT_STATUS_IS_OK(status)) {
1053 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
1057 if ((account->privilege_entries && !e.out.privs)) {
1058 printf("Account %s has privileges in SamSync, but not LSA\n",
1059 dom_sid_string(mem_ctx, dom_sid));
1063 if (!account->privilege_entries && e.out.privs && e.out.privs->count) {
1064 printf("Account %s has privileges in LSA, but not SamSync\n",
1065 dom_sid_string(mem_ctx, dom_sid));
1069 TEST_INT_EQUAL(account->privilege_entries, e.out.privs->count);
1071 for (i=0;i< e.out.privs->count; i++) {
1072 r.in.handle = samsync_state->lsa_handle;
1073 r.in.luid = &e.out.privs->set[i].luid;
1075 status = dcerpc_lsa_LookupPrivName(samsync_state->p_lsa, mem_ctx, &r);
1076 if (!NT_STATUS_IS_OK(status)) {
1077 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
1082 printf("\nLookupPrivName failed to return a name\n");
1085 for (j=0;j<account->privilege_entries; j++) {
1086 if (strcmp(r.out.name->string, account->privilege_name[j].string) == 0) {
1087 found_priv_in_lsa[j] = true;
1092 for (j=0;j<account->privilege_entries; j++) {
1093 if (!found_priv_in_lsa[j]) {
1094 printf("Privilage %s on account %s not found in LSA\n", account->privilege_name[j].string,
1095 dom_sid_string(mem_ctx, dom_sid));
1103 try a netlogon DatabaseSync
1105 static bool test_DatabaseSync(struct torture_context *tctx,
1106 struct samsync_state *samsync_state,
1107 TALLOC_CTX *mem_ctx)
1110 TALLOC_CTX *loop_ctx, *delta_ctx, *trustdom_ctx;
1111 struct netr_DatabaseSync r;
1112 const enum netr_SamDatabaseID database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS};
1115 struct samsync_trusted_domain *t;
1116 struct samsync_secret *s;
1118 const char *domain, *username;
1120 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1121 r.in.computername = TEST_MACHINE_NAME;
1122 r.in.preferredmaximumlength = (uint32_t)-1;
1123 ZERO_STRUCT(r.in.return_authenticator);
1125 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1126 r.in.sync_context = 0;
1127 r.in.database_id = database_ids[i];
1129 printf("Testing DatabaseSync of id %d\n", r.in.database_id);
1132 loop_ctx = talloc_named(mem_ctx, 0, "DatabaseSync loop context");
1133 creds_client_authenticator(samsync_state->creds, &r.in.credential);
1135 status = dcerpc_netr_DatabaseSync(samsync_state->p, loop_ctx, &r);
1136 if (!NT_STATUS_IS_OK(status) &&
1137 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1138 printf("DatabaseSync - %s\n", nt_errstr(status));
1143 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1144 printf("Credential chaining failed\n");
1147 r.in.sync_context = r.out.sync_context;
1149 for (d=0; d < r.out.delta_enum_array->num_deltas; d++) {
1150 delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
1151 switch (r.out.delta_enum_array->delta_enum[d].delta_type) {
1152 case NETR_DELTA_DOMAIN:
1153 if (!samsync_handle_domain(delta_ctx, samsync_state,
1154 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1155 printf("Failed to handle DELTA_DOMAIN\n");
1159 case NETR_DELTA_GROUP:
1160 if (!samsync_handle_group(delta_ctx, samsync_state,
1161 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1162 printf("Failed to handle DELTA_USER\n");
1166 case NETR_DELTA_USER:
1167 if (!samsync_handle_user(tctx, delta_ctx, samsync_state,
1168 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1169 printf("Failed to handle DELTA_USER\n");
1173 case NETR_DELTA_ALIAS:
1174 if (!samsync_handle_alias(delta_ctx, samsync_state,
1175 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1176 printf("Failed to handle DELTA_ALIAS\n");
1180 case NETR_DELTA_POLICY:
1181 if (!samsync_handle_policy(delta_ctx, samsync_state,
1182 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1183 printf("Failed to handle DELTA_POLICY\n");
1187 case NETR_DELTA_TRUSTED_DOMAIN:
1188 if (!samsync_handle_trusted_domain(delta_ctx, samsync_state,
1189 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1190 printf("Failed to handle DELTA_TRUSTED_DOMAIN\n");
1194 case NETR_DELTA_ACCOUNT:
1195 if (!samsync_handle_account(delta_ctx, samsync_state,
1196 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1197 printf("Failed to handle DELTA_ACCOUNT\n");
1201 case NETR_DELTA_SECRET:
1202 if (!samsync_handle_secret(delta_ctx, samsync_state,
1203 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1204 printf("Failed to handle DELTA_SECRET\n");
1208 case NETR_DELTA_GROUP_MEMBER:
1209 case NETR_DELTA_ALIAS_MEMBER:
1210 /* These are harder to cross-check, and we expect them */
1212 case NETR_DELTA_DELETE_GROUP:
1213 case NETR_DELTA_RENAME_GROUP:
1214 case NETR_DELTA_DELETE_USER:
1215 case NETR_DELTA_RENAME_USER:
1216 case NETR_DELTA_DELETE_ALIAS:
1217 case NETR_DELTA_RENAME_ALIAS:
1218 case NETR_DELTA_DELETE_TRUST:
1219 case NETR_DELTA_DELETE_ACCOUNT:
1220 case NETR_DELTA_DELETE_SECRET:
1221 case NETR_DELTA_DELETE_GROUP2:
1222 case NETR_DELTA_DELETE_USER2:
1223 case NETR_DELTA_MODIFY_COUNT:
1225 printf("Uxpected delta type %d\n", r.out.delta_enum_array->delta_enum[d].delta_type);
1229 talloc_free(delta_ctx);
1231 talloc_free(loop_ctx);
1232 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1236 domain = samsync_state->domain_name[SAM_DATABASE_DOMAIN];
1238 printf("Never got a DOMAIN object in samsync!\n");
1242 trustdom_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync Trusted domains context");
1244 username = talloc_asprintf(trustdom_ctx, "%s$", domain);
1245 for (t=samsync_state->trusted_domains; t; t=t->next) {
1246 char *secret_name = talloc_asprintf(trustdom_ctx, "G$$%s", t->name);
1247 for (s=samsync_state->secrets; s; s=s->next) {
1248 if (strcasecmp_m(s->name, secret_name) == 0) {
1250 struct samr_Password nt_hash;
1251 mdfour(nt_hash.hash, s->secret.data, s->secret.length);
1253 printf("Checking password for %s\\%s\n", t->name, username);
1254 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1257 TEST_WKSTA_MACHINE_NAME,
1261 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS)) {
1262 printf("Verifiction of trust password to %s failed: %s (the trusted domain is not available)\n",
1263 t->name, nt_errstr(nt_status));
1267 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1268 printf("Verifiction of trust password to %s: should have failed (nologon interdomain trust account), instead: %s\n",
1269 t->name, nt_errstr(nt_status));
1275 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1278 TEST_WKSTA_MACHINE_NAME,
1283 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
1284 printf("Verifiction of trust password to %s: should have failed (wrong password), instead: %s\n",
1285 t->name, nt_errstr(nt_status));
1293 talloc_free(trustdom_ctx);
1299 try a netlogon DatabaseDeltas
1301 static bool test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX *mem_ctx)
1304 TALLOC_CTX *loop_ctx;
1305 struct netr_DatabaseDeltas r;
1306 const uint32_t database_ids[] = {0, 1, 2};
1310 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1311 r.in.computername = TEST_MACHINE_NAME;
1312 r.in.preferredmaximumlength = (uint32_t)-1;
1313 ZERO_STRUCT(r.in.return_authenticator);
1315 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1316 r.in.database_id = database_ids[i];
1317 r.in.sequence_num = samsync_state->seq_num[i];
1319 if (r.in.sequence_num == 0) continue;
1321 /* this shows that the bdc doesn't need to do a single call for
1322 * each seqnumber, and the pdc doesn't need to know about old values
1325 r.in.sequence_num -= 10;
1328 printf("Testing DatabaseDeltas of id %d at %llu\n",
1329 r.in.database_id, (long long)r.in.sequence_num);
1332 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseDeltas loop context");
1333 creds_client_authenticator(samsync_state->creds, &r.in.credential);
1335 status = dcerpc_netr_DatabaseDeltas(samsync_state->p, loop_ctx, &r);
1336 if (!NT_STATUS_IS_OK(status) &&
1337 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) &&
1338 !NT_STATUS_EQUAL(status, NT_STATUS_SYNCHRONIZATION_REQUIRED)) {
1339 printf("DatabaseDeltas - %s\n", nt_errstr(status));
1343 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1344 printf("Credential chaining failed\n");
1347 r.in.sequence_num++;
1348 talloc_free(loop_ctx);
1349 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1357 try a netlogon DatabaseSync2
1359 static bool test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1360 struct creds_CredentialState *creds)
1363 TALLOC_CTX *loop_ctx;
1364 struct netr_DatabaseSync2 r;
1365 const uint32_t database_ids[] = {0, 1, 2};
1369 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1370 r.in.computername = TEST_MACHINE_NAME;
1371 r.in.preferredmaximumlength = (uint32_t)-1;
1372 ZERO_STRUCT(r.in.return_authenticator);
1374 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1375 r.in.sync_context = 0;
1376 r.in.database_id = database_ids[i];
1377 r.in.restart_state = 0;
1379 printf("Testing DatabaseSync2 of id %d\n", r.in.database_id);
1382 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync2 loop context");
1383 creds_client_authenticator(creds, &r.in.credential);
1385 status = dcerpc_netr_DatabaseSync2(p, loop_ctx, &r);
1386 if (!NT_STATUS_IS_OK(status) &&
1387 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1388 printf("DatabaseSync2 - %s\n", nt_errstr(status));
1392 if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
1393 printf("Credential chaining failed\n");
1396 r.in.sync_context = r.out.sync_context;
1397 talloc_free(loop_ctx);
1398 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1406 bool torture_rpc_samsync(struct torture_context *torture)
1409 TALLOC_CTX *mem_ctx;
1411 struct test_join *join_ctx;
1412 struct test_join *join_ctx2;
1413 struct test_join *user_ctx;
1414 const char *machine_password;
1415 const char *wksta_machine_password;
1416 struct dcerpc_binding *b;
1417 struct dcerpc_binding *b_netlogon_wksta;
1418 struct samr_Connect c;
1419 struct samr_SetDomainInfo s;
1420 struct policy_handle *domain_policy;
1422 struct lsa_ObjectAttribute attr;
1423 struct lsa_QosInfo qos;
1424 struct lsa_OpenPolicy2 r;
1425 struct cli_credentials *credentials;
1426 struct cli_credentials *credentials_wksta;
1428 struct samsync_state *samsync_state;
1430 char *test_machine_account;
1432 char *test_wksta_machine_account;
1434 mem_ctx = talloc_init("torture_rpc_netlogon");
1436 test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1437 join_ctx = torture_create_testuser(torture, test_machine_account,
1438 lp_workgroup(torture->lp_ctx), ACB_SVRTRUST,
1441 talloc_free(mem_ctx);
1442 printf("Failed to join as BDC\n");
1446 test_wksta_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_WKSTA_MACHINE_NAME);
1447 join_ctx2 = torture_create_testuser(torture, test_wksta_machine_account, lp_workgroup(torture->lp_ctx), ACB_WSTRUST, &wksta_machine_password);
1449 talloc_free(mem_ctx);
1450 printf("Failed to join as member\n");
1454 user_ctx = torture_create_testuser(torture, TEST_USER_NAME,
1455 lp_workgroup(torture->lp_ctx),
1458 talloc_free(mem_ctx);
1459 printf("Failed to create test account\n");
1463 samsync_state = talloc_zero(mem_ctx, struct samsync_state);
1465 samsync_state->p_samr = torture_join_samr_pipe(join_ctx);
1466 samsync_state->connect_handle = talloc_zero(samsync_state, struct policy_handle);
1467 samsync_state->lsa_handle = talloc_zero(samsync_state, struct policy_handle);
1468 c.in.system_name = NULL;
1469 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1470 c.out.connect_handle = samsync_state->connect_handle;
1472 status = dcerpc_samr_Connect(samsync_state->p_samr, mem_ctx, &c);
1473 if (!NT_STATUS_IS_OK(status)) {
1474 printf("samr_Connect failed\n");
1479 domain_policy = samsync_open_domain(mem_ctx, samsync_state, lp_workgroup(torture->lp_ctx), NULL);
1480 if (!domain_policy) {
1481 printf("samrsync_open_domain failed\n");
1486 s.in.domain_handle = domain_policy;
1488 s.in.info = talloc(mem_ctx, union samr_DomainInfo);
1490 s.in.info->info4.comment.string
1491 = talloc_asprintf(mem_ctx,
1492 "Tortured by Samba4: %s",
1493 timestring(mem_ctx, time(NULL)));
1494 status = dcerpc_samr_SetDomainInfo(samsync_state->p_samr, mem_ctx, &s);
1496 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, domain_policy)) {
1501 if (!NT_STATUS_IS_OK(status)) {
1502 printf("SetDomainInfo level %u failed - %s\n",
1503 s.in.level, nt_errstr(status));
1509 status = torture_rpc_connection(torture,
1510 &samsync_state->p_lsa,
1513 if (!NT_STATUS_IS_OK(status)) {
1519 qos.impersonation_level = 2;
1520 qos.context_mode = 1;
1521 qos.effective_only = 0;
1524 attr.root_dir = NULL;
1525 attr.object_name = NULL;
1526 attr.attributes = 0;
1527 attr.sec_desc = NULL;
1528 attr.sec_qos = &qos;
1530 r.in.system_name = "\\";
1532 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1533 r.out.handle = samsync_state->lsa_handle;
1535 status = dcerpc_lsa_OpenPolicy2(samsync_state->p_lsa, mem_ctx, &r);
1536 if (!NT_STATUS_IS_OK(status)) {
1537 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1542 status = torture_rpc_binding(torture, &b);
1543 if (!NT_STATUS_IS_OK(status)) {
1548 b->flags &= ~DCERPC_AUTH_OPTIONS;
1549 b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1551 credentials = cli_credentials_init(mem_ctx);
1553 cli_credentials_set_workstation(credentials, TEST_MACHINE_NAME, CRED_SPECIFIED);
1554 cli_credentials_set_domain(credentials, lp_workgroup(torture->lp_ctx), CRED_SPECIFIED);
1555 cli_credentials_set_username(credentials, test_machine_account, CRED_SPECIFIED);
1556 cli_credentials_set_password(credentials, machine_password, CRED_SPECIFIED);
1557 cli_credentials_set_secure_channel_type(credentials,
1560 status = dcerpc_pipe_connect_b(samsync_state,
1561 &samsync_state->p, b,
1562 &ndr_table_netlogon,
1563 credentials, torture->ev, torture->lp_ctx);
1565 if (!NT_STATUS_IS_OK(status)) {
1566 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
1571 status = dcerpc_schannel_creds(samsync_state->p->conn->security_state.generic_state,
1572 samsync_state, &samsync_state->creds);
1573 if (!NT_STATUS_IS_OK(status)) {
1579 status = torture_rpc_binding(torture, &b_netlogon_wksta);
1580 if (!NT_STATUS_IS_OK(status)) {
1585 b_netlogon_wksta->flags &= ~DCERPC_AUTH_OPTIONS;
1586 b_netlogon_wksta->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1588 credentials_wksta = cli_credentials_init(mem_ctx);
1590 cli_credentials_set_workstation(credentials_wksta, TEST_WKSTA_MACHINE_NAME, CRED_SPECIFIED);
1591 cli_credentials_set_domain(credentials_wksta, lp_workgroup(torture->lp_ctx), CRED_SPECIFIED);
1592 cli_credentials_set_username(credentials_wksta, test_wksta_machine_account, CRED_SPECIFIED);
1593 cli_credentials_set_password(credentials_wksta, wksta_machine_password, CRED_SPECIFIED);
1594 cli_credentials_set_secure_channel_type(credentials_wksta,
1597 status = dcerpc_pipe_connect_b(samsync_state,
1598 &samsync_state->p_netlogon_wksta,
1600 &ndr_table_netlogon,
1601 credentials_wksta, torture->ev, torture->lp_ctx);
1603 if (!NT_STATUS_IS_OK(status)) {
1604 printf("Failed to connect to server as a Workstation: %s\n", nt_errstr(status));
1609 status = dcerpc_schannel_creds(samsync_state->p_netlogon_wksta->conn->security_state.generic_state,
1610 samsync_state, &samsync_state->creds_netlogon_wksta);
1611 if (!NT_STATUS_IS_OK(status)) {
1612 printf("Failed to obtail schanel creds!\n");
1616 if (!test_DatabaseSync(torture, samsync_state, mem_ctx)) {
1617 printf("DatabaseSync failed\n");
1621 if (!test_DatabaseDeltas(samsync_state, mem_ctx)) {
1622 printf("DatabaseDeltas failed\n");
1626 if (!test_DatabaseSync2(samsync_state->p, mem_ctx, samsync_state->creds)) {
1627 printf("DatabaseSync2 failed\n");
1632 torture_leave_domain(join_ctx);
1633 torture_leave_domain(join_ctx2);
1634 torture_leave_domain(user_ctx);
1636 talloc_free(mem_ctx);