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 "libcli/auth/libcli_auth.h"
33 #include "libcli/security/security.h"
34 #include "librpc/gen_ndr/ndr_netlogon.h"
35 #include "librpc/gen_ndr/ndr_netlogon_c.h"
36 #include "librpc/gen_ndr/ndr_lsa_c.h"
37 #include "librpc/gen_ndr/ndr_samr_c.h"
38 #include "librpc/gen_ndr/ndr_security.h"
39 #include "param/param.h"
41 #define TEST_MACHINE_NAME "samsynctest"
42 #define TEST_WKSTA_MACHINE_NAME "samsynctest2"
43 #define TEST_USER_NAME "samsynctestuser"
46 try a netlogon SamLogon
48 static NTSTATUS test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
49 struct creds_CredentialState *creds,
50 const char *domain, const char *account_name,
51 const char *workstation,
52 struct samr_Password *lm_hash,
53 struct samr_Password *nt_hash,
54 struct netr_SamInfo3 **info3)
57 struct netr_LogonSamLogon r;
58 struct netr_Authenticator auth, auth2;
59 struct netr_NetworkInfo ninfo;
61 ninfo.identity_info.domain_name.string = domain;
62 ninfo.identity_info.parameter_control = 0;
63 ninfo.identity_info.logon_id_low = 0;
64 ninfo.identity_info.logon_id_high = 0;
65 ninfo.identity_info.account_name.string = account_name;
66 ninfo.identity_info.workstation.string = workstation;
67 generate_random_buffer(ninfo.challenge,
68 sizeof(ninfo.challenge));
71 ninfo.nt.data = talloc_array(mem_ctx, uint8_t, 24);
72 SMBOWFencrypt(nt_hash->hash, ninfo.challenge, ninfo.nt.data);
80 ninfo.lm.data = talloc_array(mem_ctx, uint8_t, 24);
81 SMBOWFencrypt(lm_hash->hash, ninfo.challenge, ninfo.lm.data);
87 r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
88 r.in.computer_name = workstation;
89 r.in.credential = &auth;
90 r.in.return_authenticator = &auth2;
92 r.in.logon.network = &ninfo;
95 creds_client_authenticator(creds, &auth);
97 r.in.validation_level = 3;
99 status = dcerpc_netr_LogonSamLogon(p, mem_ctx, &r);
101 if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
102 printf("Credential chaining failed\n");
106 *info3 = r.out.validation.sam3;
112 struct samsync_state {
113 /* we remember the sequence numbers so we can easily do a DatabaseDelta */
115 const char *domain_name[2];
116 struct samsync_secret *secrets;
117 struct samsync_trusted_domain *trusted_domains;
118 struct creds_CredentialState *creds;
119 struct creds_CredentialState *creds_netlogon_wksta;
120 struct policy_handle *connect_handle;
121 struct policy_handle *domain_handle[2];
122 struct dom_sid *sid[2];
123 struct dcerpc_pipe *p;
124 struct dcerpc_pipe *p_netlogon_wksta;
125 struct dcerpc_pipe *p_samr;
126 struct dcerpc_pipe *p_lsa;
127 struct policy_handle *lsa_handle;
130 struct samsync_secret {
131 struct samsync_secret *prev, *next;
137 struct samsync_trusted_domain {
138 struct samsync_trusted_domain *prev, *next;
143 static struct policy_handle *samsync_open_domain(TALLOC_CTX *mem_ctx,
144 struct samsync_state *samsync_state,
146 struct dom_sid **sid)
148 struct lsa_String name;
149 struct samr_OpenDomain o;
150 struct samr_LookupDomain l;
151 struct policy_handle *domain_handle = talloc(mem_ctx, struct policy_handle);
154 name.string = domain;
155 l.in.connect_handle = samsync_state->connect_handle;
156 l.in.domain_name = &name;
158 nt_status = dcerpc_samr_LookupDomain(samsync_state->p_samr, mem_ctx, &l);
159 if (!NT_STATUS_IS_OK(nt_status)) {
160 printf("LookupDomain failed - %s\n", nt_errstr(nt_status));
164 o.in.connect_handle = samsync_state->connect_handle;
165 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
166 o.in.sid = l.out.sid;
167 o.out.domain_handle = domain_handle;
173 nt_status = dcerpc_samr_OpenDomain(samsync_state->p_samr, mem_ctx, &o);
174 if (!NT_STATUS_IS_OK(nt_status)) {
175 printf("OpenDomain failed - %s\n", nt_errstr(nt_status));
179 return domain_handle;
182 static struct sec_desc_buf *samsync_query_samr_sec_desc(TALLOC_CTX *mem_ctx,
183 struct samsync_state *samsync_state,
184 struct policy_handle *handle)
186 struct samr_QuerySecurity r;
189 r.in.handle = handle;
192 status = dcerpc_samr_QuerySecurity(samsync_state->p_samr, mem_ctx, &r);
193 if (!NT_STATUS_IS_OK(status)) {
194 printf("SAMR QuerySecurity failed - %s\n", nt_errstr(status));
201 static struct sec_desc_buf *samsync_query_lsa_sec_desc(TALLOC_CTX *mem_ctx,
202 struct samsync_state *samsync_state,
203 struct policy_handle *handle)
205 struct lsa_QuerySecurity r;
208 r.in.handle = handle;
211 status = dcerpc_lsa_QuerySecurity(samsync_state->p_lsa, mem_ctx, &r);
212 if (!NT_STATUS_IS_OK(status)) {
213 printf("LSA QuerySecurity failed - %s\n", nt_errstr(status));
220 #define TEST_UINT64_EQUAL(i1, i2) do {\
222 printf("%s: uint64 mismatch: " #i1 ": 0x%016llx (%lld) != " #i2 ": 0x%016llx (%lld)\n", \
224 (long long)i1, (long long)i1, \
225 (long long)i2, (long long)i2);\
229 #define TEST_INT_EQUAL(i1, i2) do {\
231 printf("%s: integer mismatch: " #i1 ": 0x%08x (%d) != " #i2 ": 0x%08x (%d)\n", \
232 __location__, i1, i1, i2, i2); \
236 #define TEST_TIME_EQUAL(t1, t2) do {\
238 printf("%s: NTTIME mismatch: " #t1 ":%s != " #t2 ": %s\n", \
239 __location__, nt_time_string(mem_ctx, t1), nt_time_string(mem_ctx, t2));\
244 #define TEST_STRING_EQUAL(s1, s2) do {\
245 if (!((!s1.string || s1.string[0]=='\0') && (!s2.string || s2.string[0]=='\0')) \
246 && strcmp_safe(s1.string, s2.string) != 0) {\
247 printf("%s: string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
248 __location__, s1.string, s2.string);\
253 #define TEST_SID_EQUAL(s1, s2) do {\
254 if (!dom_sid_equal(s1, s2)) {\
255 printf("%s: dom_sid mismatch: " #s1 ":%s != " #s2 ": %s\n", \
256 __location__, dom_sid_string(mem_ctx, s1), dom_sid_string(mem_ctx, s2));\
261 /* The ~SEC_DESC_SACL_PRESENT is because we don't, as administrator,
262 * get back the SACL part of the SD when we ask over SAMR */
264 #define TEST_SEC_DESC_EQUAL(sd1, pipe, handle) do {\
265 struct sec_desc_buf *sdbuf = samsync_query_ ##pipe## _sec_desc(mem_ctx, samsync_state, \
267 if (!sdbuf || !sdbuf->sd) { \
268 printf("Could not obtain security descriptor to match " #sd1 "\n");\
271 if (!security_descriptor_mask_equal(sd1.sd, sdbuf->sd, \
272 ~SEC_DESC_SACL_PRESENT)) {\
273 printf("Security Descriptor Mismatch for %s:\n", #sd1);\
274 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamSync", sd1.sd);\
275 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamR", sdbuf->sd);\
281 static bool samsync_handle_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
282 int database_id, struct netr_DELTA_ENUM *delta)
284 struct netr_DELTA_DOMAIN *domain = delta->delta_union.domain;
285 struct dom_sid *dom_sid;
286 struct samr_QueryDomainInfo q[14]; /* q[0] will be unused simple for clarity */
287 uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
292 samsync_state->seq_num[database_id] =
293 domain->sequence_num;
294 switch (database_id) {
295 case SAM_DATABASE_DOMAIN:
297 case SAM_DATABASE_BUILTIN:
298 if (strcasecmp_m("BUILTIN", domain->domain_name.string) != 0) {
299 printf("BUILTIN domain has different name: %s\n", domain->domain_name.string);
302 case SAM_DATABASE_PRIVS:
303 printf("DOMAIN entry on privs DB!\n");
308 if (!samsync_state->domain_name[database_id]) {
309 samsync_state->domain_name[database_id] =
310 talloc_reference(samsync_state, domain->domain_name.string);
312 if (strcasecmp_m(samsync_state->domain_name[database_id], domain->domain_name.string) != 0) {
313 printf("Domain has name varies!: %s != %s\n", samsync_state->domain_name[database_id],
314 domain->domain_name.string);
319 if (!samsync_state->domain_handle[database_id]) {
320 samsync_state->domain_handle[database_id]
321 = talloc_reference(samsync_state,
322 samsync_open_domain(mem_ctx, samsync_state, samsync_state->domain_name[database_id],
325 if (samsync_state->domain_handle[database_id]) {
326 samsync_state->sid[database_id] = talloc_reference(samsync_state, dom_sid);
329 printf("\tsequence_nums[%d/%s]=%llu\n",
330 database_id, domain->domain_name.string,
331 (long long)samsync_state->seq_num[database_id]);
333 for (i=0;i<ARRAY_SIZE(levels);i++) {
334 q[levels[i]].in.domain_handle = samsync_state->domain_handle[database_id];
335 q[levels[i]].in.level = levels[i];
337 nt_status = dcerpc_samr_QueryDomainInfo(samsync_state->p_samr, mem_ctx, &q[levels[i]]);
339 if (!NT_STATUS_IS_OK(nt_status)) {
340 printf("QueryDomainInfo level %u failed - %s\n",
341 q[levels[i]].in.level, nt_errstr(nt_status));
346 TEST_STRING_EQUAL(q[5].out.info->info5.domain_name, domain->domain_name);
348 TEST_STRING_EQUAL(q[2].out.info->info2.comment, domain->comment);
349 TEST_STRING_EQUAL(q[4].out.info->info4.comment, domain->comment);
350 TEST_TIME_EQUAL(q[2].out.info->info2.force_logoff_time, domain->force_logoff_time);
351 TEST_TIME_EQUAL(q[3].out.info->info3.force_logoff_time, domain->force_logoff_time);
353 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_length, domain->min_password_length);
354 TEST_TIME_EQUAL(q[1].out.info->info1.password_history_length, domain->password_history_length);
355 TEST_TIME_EQUAL(q[1].out.info->info1.max_password_age, domain->max_password_age);
356 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_age, domain->min_password_age);
358 TEST_UINT64_EQUAL(q[8].out.info->info8.sequence_num,
359 domain->sequence_num);
360 TEST_TIME_EQUAL(q[8].out.info->info8.domain_create_time,
361 domain->domain_create_time);
362 TEST_TIME_EQUAL(q[13].out.info->info13.domain_create_time,
363 domain->domain_create_time);
365 TEST_SEC_DESC_EQUAL(domain->sdbuf, samr, samsync_state->domain_handle[database_id]);
370 static bool samsync_handle_policy(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
371 int database_id, struct netr_DELTA_ENUM *delta)
373 struct netr_DELTA_POLICY *policy = delta->delta_union.policy;
375 samsync_state->seq_num[database_id] =
376 policy->sequence_num;
378 if (!samsync_state->domain_name[SAM_DATABASE_DOMAIN]) {
379 samsync_state->domain_name[SAM_DATABASE_DOMAIN] =
380 talloc_reference(samsync_state, policy->primary_domain_name.string);
382 if (strcasecmp_m(samsync_state->domain_name[SAM_DATABASE_DOMAIN], policy->primary_domain_name.string) != 0) {
383 printf("PRIMARY domain has name varies between DOMAIN and POLICY!: %s != %s\n", samsync_state->domain_name[SAM_DATABASE_DOMAIN],
384 policy->primary_domain_name.string);
389 if (!dom_sid_equal(samsync_state->sid[SAM_DATABASE_DOMAIN], policy->sid)) {
390 printf("Domain SID from POLICY (%s) does not match domain sid from SAMR (%s)\n",
391 dom_sid_string(mem_ctx, policy->sid), dom_sid_string(mem_ctx, samsync_state->sid[SAM_DATABASE_DOMAIN]));
395 printf("\tsequence_nums[%d/PRIVS]=%llu\n",
397 (long long)samsync_state->seq_num[database_id]);
401 static bool samsync_handle_user(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
402 int database_id, struct netr_DELTA_ENUM *delta)
404 uint32_t rid = delta->delta_id_union.rid;
405 struct netr_DELTA_USER *user = delta->delta_union.user;
406 struct netr_SamInfo3 *info3;
407 struct samr_Password lm_hash;
408 struct samr_Password nt_hash;
409 struct samr_Password *lm_hash_p = NULL;
410 struct samr_Password *nt_hash_p = NULL;
411 const char *domain = samsync_state->domain_name[database_id];
412 const char *username = user->account_name.string;
416 struct samr_OpenUser r;
417 struct samr_QueryUserInfo q;
418 struct policy_handle user_handle;
420 struct samr_GetGroupsForUser getgroups;
421 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
422 printf("SamSync needs domain information before the users\n");
426 r.in.domain_handle = samsync_state->domain_handle[database_id];
427 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
429 r.out.user_handle = &user_handle;
431 nt_status = dcerpc_samr_OpenUser(samsync_state->p_samr, mem_ctx, &r);
432 if (!NT_STATUS_IS_OK(nt_status)) {
433 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
437 q.in.user_handle = &user_handle;
440 TEST_SEC_DESC_EQUAL(user->sdbuf, samr, &user_handle);
442 nt_status = dcerpc_samr_QueryUserInfo(samsync_state->p_samr, mem_ctx, &q);
443 if (!NT_STATUS_IS_OK(nt_status)) {
444 printf("QueryUserInfo level %u failed - %s\n",
445 q.in.level, nt_errstr(nt_status));
449 getgroups.in.user_handle = &user_handle;
451 nt_status = dcerpc_samr_GetGroupsForUser(samsync_state->p_samr, mem_ctx, &getgroups);
452 if (!NT_STATUS_IS_OK(nt_status)) {
453 printf("GetGroupsForUser failed - %s\n",
454 nt_errstr(nt_status));
458 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &user_handle)) {
459 printf("samr_handle_Close failed - %s\n",
460 nt_errstr(nt_status));
467 if (!NT_STATUS_IS_OK(nt_status)) {
468 printf("QueryUserInfo level %u failed - %s\n",
469 q.in.level, nt_errstr(nt_status));
473 TEST_STRING_EQUAL(q.out.info->info21.account_name, user->account_name);
474 TEST_STRING_EQUAL(q.out.info->info21.full_name, user->full_name);
475 TEST_INT_EQUAL(q.out.info->info21.rid, user->rid);
476 TEST_INT_EQUAL(q.out.info->info21.primary_gid, user->primary_gid);
477 TEST_STRING_EQUAL(q.out.info->info21.home_directory, user->home_directory);
478 TEST_STRING_EQUAL(q.out.info->info21.home_drive, user->home_drive);
479 TEST_STRING_EQUAL(q.out.info->info21.logon_script, user->logon_script);
480 TEST_STRING_EQUAL(q.out.info->info21.description, user->description);
481 TEST_STRING_EQUAL(q.out.info->info21.workstations, user->workstations);
483 TEST_TIME_EQUAL(q.out.info->info21.last_logon, user->last_logon);
484 TEST_TIME_EQUAL(q.out.info->info21.last_logoff, user->last_logoff);
487 TEST_INT_EQUAL(q.out.info->info21.logon_hours.units_per_week,
488 user->logon_hours.units_per_week);
490 if (memcmp(q.out.info->info21.logon_hours.bits, user->logon_hours.bits,
491 q.out.info->info21.logon_hours.units_per_week/8) != 0) {
492 printf("Logon hours mismatch\n");
497 TEST_INT_EQUAL(q.out.info->info21.bad_password_count,
498 user->bad_password_count);
499 TEST_INT_EQUAL(q.out.info->info21.logon_count,
502 TEST_TIME_EQUAL(q.out.info->info21.last_password_change,
503 user->last_password_change);
504 TEST_TIME_EQUAL(q.out.info->info21.acct_expiry,
507 TEST_INT_EQUAL((q.out.info->info21.acct_flags & ~ACB_PW_EXPIRED), user->acct_flags);
508 if (user->acct_flags & ACB_PWNOEXP) {
509 if (q.out.info->info21.acct_flags & ACB_PW_EXPIRED) {
510 printf("ACB flags mismatch: both expired and no expiry!\n");
513 if (q.out.info->info21.force_password_change != (NTTIME)0x7FFFFFFFFFFFFFFFULL) {
514 printf("ACB flags mismatch: no password expiry, but force password change 0x%016llx (%lld) != 0x%016llx (%lld)\n",
515 (unsigned long long)q.out.info->info21.force_password_change,
516 (unsigned long long)q.out.info->info21.force_password_change,
517 (unsigned long long)0x7FFFFFFFFFFFFFFFULL, (unsigned long long)0x7FFFFFFFFFFFFFFFULL
523 TEST_INT_EQUAL(q.out.info->info21.nt_password_set, user->nt_password_present);
524 TEST_INT_EQUAL(q.out.info->info21.lm_password_set, user->lm_password_present);
525 TEST_INT_EQUAL(q.out.info->info21.password_expired, user->password_expired);
527 TEST_STRING_EQUAL(q.out.info->info21.comment, user->comment);
528 TEST_STRING_EQUAL(q.out.info->info21.parameters, user->parameters);
530 TEST_INT_EQUAL(q.out.info->info21.country_code, user->country_code);
531 TEST_INT_EQUAL(q.out.info->info21.code_page, user->code_page);
533 TEST_STRING_EQUAL(q.out.info->info21.profile_path, user->profile_path);
535 if (user->lm_password_present) {
536 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
537 lm_hash_p = &lm_hash;
539 if (user->nt_password_present) {
540 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
541 nt_hash_p = &nt_hash;
544 if (user->user_private_info.SensitiveData) {
546 struct netr_USER_KEYS keys;
547 enum ndr_err_code ndr_err;
548 data.data = user->user_private_info.SensitiveData;
549 data.length = user->user_private_info.DataLength;
550 creds_arcfour_crypt(samsync_state->creds, data.data, data.length);
551 ndr_err = ndr_pull_struct_blob(&data, mem_ctx, &keys, (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
552 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
553 if (keys.keys.keys2.lmpassword.length == 16) {
554 sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
555 lm_hash_p = &lm_hash;
557 if (keys.keys.keys2.ntpassword.length == 16) {
558 sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
559 nt_hash_p = &nt_hash;
562 printf("Failed to parse Sensitive Data for %s:\n", username);
564 dump_data(0, data.data, data.length);
571 DATA_BLOB nt_hash_blob = data_blob_const(nt_hash_p, 16);
572 DEBUG(100,("ACCOUNT [%s\\%-25s] NTHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string(mem_ctx, &nt_hash_blob)));
575 DATA_BLOB lm_hash_blob = data_blob_const(lm_hash_p, 16);
576 DEBUG(100,("ACCOUNT [%s\\%-25s] LMHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string(mem_ctx, &lm_hash_blob)));
579 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta,
582 TEST_WKSTA_MACHINE_NAME,
587 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED)) {
588 if (user->acct_flags & ACB_DISABLED) {
591 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)) {
592 if (user->acct_flags & ACB_WSTRUST) {
595 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT)) {
596 if (user->acct_flags & ACB_SVRTRUST) {
599 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
600 if (user->acct_flags & ACB_DOMTRUST) {
603 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
604 if (user->acct_flags & ACB_DOMTRUST) {
607 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT)) {
608 if (user->acct_flags & ACB_AUTOLOCK) {
611 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED)) {
612 if (q.out.info->info21.acct_flags & ACB_PW_EXPIRED) {
615 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
616 if (!lm_hash_p && !nt_hash_p) {
619 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE)) {
620 /* We would need to know the server's current time to test this properly */
622 } else if (NT_STATUS_IS_OK(nt_status)) {
623 TEST_INT_EQUAL(user->rid, info3->base.rid);
624 TEST_INT_EQUAL(user->primary_gid, info3->base.primary_gid);
625 /* this is 0x0 from NT4 sp6 */
626 if (info3->base.acct_flags) {
627 TEST_INT_EQUAL(user->acct_flags, info3->base.acct_flags);
629 /* this is NULL from NT4 sp6 */
630 if (info3->base.account_name.string) {
631 TEST_STRING_EQUAL(user->account_name, info3->base.account_name);
633 /* this is NULL from Win2k3 */
634 if (info3->base.full_name.string) {
635 TEST_STRING_EQUAL(user->full_name, info3->base.full_name);
637 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
638 TEST_STRING_EQUAL(user->profile_path, info3->base.profile_path);
639 TEST_STRING_EQUAL(user->home_directory, info3->base.home_directory);
640 TEST_STRING_EQUAL(user->home_drive, info3->base.home_drive);
641 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
644 TEST_TIME_EQUAL(user->last_logon, info3->base.last_logon);
645 TEST_TIME_EQUAL(user->acct_expiry, info3->base.acct_expiry);
646 TEST_TIME_EQUAL(user->last_password_change, info3->base.last_password_change);
647 TEST_TIME_EQUAL(q.out.info->info21.force_password_change, info3->base.force_password_change);
649 /* Does the concept of a logoff time ever really
650 * exist? (not in any sensible way, according to the
651 * doco I read -- abartlet) */
653 /* This copes with the two different versions of 0 I see */
654 /* with NT4 sp6 we have the || case */
655 if (!((user->last_logoff == 0)
656 || (info3->base.last_logoff == 0x7fffffffffffffffLL))) {
657 TEST_TIME_EQUAL(user->last_logoff, info3->base.last_logoff);
660 TEST_INT_EQUAL(getgroups.out.rids->count, info3->base.groups.count);
661 if (getgroups.out.rids->count == info3->base.groups.count) {
663 int count = getgroups.out.rids->count;
664 bool *matched = talloc_zero_array(mem_ctx, bool, getgroups.out.rids->count);
666 for (i = 0; i < count; i++) {
667 for (j = 0; j < count; j++) {
668 if ((getgroups.out.rids->rids[i].rid ==
669 info3->base.groups.rids[j].rid)
670 && (getgroups.out.rids->rids[i].attributes ==
671 info3->base.groups.rids[j].attributes)) {
677 for (i = 0; i < getgroups.out.rids->count; i++) {
678 if (matched[i] == false) {
680 printf("Could not find group RID %u found in getgroups in NETLOGON reply\n",
681 getgroups.out.rids->rids[i].rid);
687 printf("Could not validate password for user %s\\%s: %s\n",
688 domain, username, nt_errstr(nt_status));
694 static bool samsync_handle_alias(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
695 int database_id, struct netr_DELTA_ENUM *delta)
697 uint32_t rid = delta->delta_id_union.rid;
698 struct netr_DELTA_ALIAS *alias = delta->delta_union.alias;
702 struct samr_OpenAlias r;
703 struct samr_QueryAliasInfo q;
704 struct policy_handle alias_handle;
706 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
707 printf("SamSync needs domain information before the users\n");
711 r.in.domain_handle = samsync_state->domain_handle[database_id];
712 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
714 r.out.alias_handle = &alias_handle;
716 nt_status = dcerpc_samr_OpenAlias(samsync_state->p_samr, mem_ctx, &r);
717 if (!NT_STATUS_IS_OK(nt_status)) {
718 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
722 q.in.alias_handle = &alias_handle;
725 TEST_SEC_DESC_EQUAL(alias->sdbuf, samr, &alias_handle);
727 nt_status = dcerpc_samr_QueryAliasInfo(samsync_state->p_samr, mem_ctx, &q);
728 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &alias_handle)) {
732 if (!NT_STATUS_IS_OK(nt_status)) {
733 printf("QueryAliasInfo level %u failed - %s\n",
734 q.in.level, nt_errstr(nt_status));
738 TEST_STRING_EQUAL(q.out.info->all.name, alias->alias_name);
739 TEST_STRING_EQUAL(q.out.info->all.description, alias->description);
743 static bool samsync_handle_group(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
744 int database_id, struct netr_DELTA_ENUM *delta)
746 uint32_t rid = delta->delta_id_union.rid;
747 struct netr_DELTA_GROUP *group = delta->delta_union.group;
751 struct samr_OpenGroup r;
752 struct samr_QueryGroupInfo q;
753 struct policy_handle group_handle;
755 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
756 printf("SamSync needs domain information before the users\n");
760 r.in.domain_handle = samsync_state->domain_handle[database_id];
761 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
763 r.out.group_handle = &group_handle;
765 nt_status = dcerpc_samr_OpenGroup(samsync_state->p_samr, mem_ctx, &r);
766 if (!NT_STATUS_IS_OK(nt_status)) {
767 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
771 q.in.group_handle = &group_handle;
774 TEST_SEC_DESC_EQUAL(group->sdbuf, samr, &group_handle);
776 nt_status = dcerpc_samr_QueryGroupInfo(samsync_state->p_samr, mem_ctx, &q);
777 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &group_handle)) {
781 if (!NT_STATUS_IS_OK(nt_status)) {
782 printf("QueryGroupInfo level %u failed - %s\n",
783 q.in.level, nt_errstr(nt_status));
787 TEST_STRING_EQUAL(q.out.info->all.name, group->group_name);
788 TEST_INT_EQUAL(q.out.info->all.attributes, group->attributes);
789 TEST_STRING_EQUAL(q.out.info->all.description, group->description);
793 static bool samsync_handle_secret(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
794 int database_id, struct netr_DELTA_ENUM *delta)
796 struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
797 const char *name = delta->delta_id_union.name;
798 struct samsync_secret *new = talloc(samsync_state, struct samsync_secret);
799 struct samsync_secret *old = talloc(mem_ctx, struct samsync_secret);
800 struct lsa_QuerySecret q;
801 struct lsa_OpenSecret o;
802 struct policy_handle sec_handle;
803 struct lsa_DATA_BUF_PTR bufp1;
804 struct lsa_DATA_BUF_PTR bufp2;
808 DATA_BLOB lsa_blob1, lsa_blob_out, session_key;
811 creds_arcfour_crypt(samsync_state->creds, secret->current_cipher.cipher_data,
812 secret->current_cipher.maxlen);
814 creds_arcfour_crypt(samsync_state->creds, secret->old_cipher.cipher_data,
815 secret->old_cipher.maxlen);
817 new->name = talloc_reference(new, name);
818 new->secret = data_blob_talloc(new, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
819 new->mtime = secret->current_cipher_set_time;
821 new = talloc_reference(samsync_state, new);
822 DLIST_ADD(samsync_state->secrets, new);
824 old->name = talloc_reference(old, name);
825 old->secret = data_blob_const(secret->old_cipher.cipher_data, secret->old_cipher.maxlen);
826 old->mtime = secret->old_cipher_set_time;
828 o.in.handle = samsync_state->lsa_handle;
829 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
830 o.in.name.string = name;
831 o.out.sec_handle = &sec_handle;
833 status = dcerpc_lsa_OpenSecret(samsync_state->p_lsa, mem_ctx, &o);
834 if (!NT_STATUS_IS_OK(status)) {
835 printf("OpenSecret failed - %s\n", nt_errstr(status));
840 We would like to do this, but it is NOT_SUPPORTED on win2k3
841 TEST_SEC_DESC_EQUAL(secret->sdbuf, lsa, &sec_handle);
843 status = dcerpc_fetch_session_key(samsync_state->p_lsa, &session_key);
844 if (!NT_STATUS_IS_OK(status)) {
845 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
850 ZERO_STRUCT(new_mtime);
851 ZERO_STRUCT(old_mtime);
853 /* fetch the secret back again */
854 q.in.sec_handle = &sec_handle;
855 q.in.new_val = &bufp1;
856 q.in.new_mtime = &new_mtime;
857 q.in.old_val = &bufp2;
858 q.in.old_mtime = &old_mtime;
863 status = dcerpc_lsa_QuerySecret(samsync_state->p_lsa, mem_ctx, &q);
864 if (NT_STATUS_EQUAL(NT_STATUS_ACCESS_DENIED, status)) {
865 /* some things are just off limits */
867 } else if (!NT_STATUS_IS_OK(status)) {
868 printf("QuerySecret failed - %s\n", nt_errstr(status));
872 if (q.out.old_val->buf == NULL) {
873 /* probably just not available due to ACLs */
875 lsa_blob1.data = q.out.old_val->buf->data;
876 lsa_blob1.length = q.out.old_val->buf->length;
878 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
879 if (!NT_STATUS_IS_OK(status)) {
880 printf("Failed to decrypt secrets OLD blob: %s\n", nt_errstr(status));
884 if (!q.out.old_mtime) {
885 printf("OLD mtime not available on LSA for secret %s\n", old->name);
888 if (old->mtime != *q.out.old_mtime) {
889 printf("OLD mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
890 old->name, nt_time_string(mem_ctx, old->mtime),
891 nt_time_string(mem_ctx, *q.out.old_mtime));
895 if (old->secret.length != lsa_blob_out.length) {
896 printf("Returned secret %s doesn't match: %d != %d\n",
897 old->name, (int)old->secret.length, (int)lsa_blob_out.length);
899 } else if (memcmp(lsa_blob_out.data,
900 old->secret.data, old->secret.length) != 0) {
901 printf("Returned secret %s doesn't match: \n",
903 DEBUG(1, ("SamSync Secret:\n"));
904 dump_data(1, old->secret.data, old->secret.length);
905 DEBUG(1, ("LSA Secret:\n"));
906 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
912 if (q.out.new_val->buf == NULL) {
913 /* probably just not available due to ACLs */
915 lsa_blob1.data = q.out.new_val->buf->data;
916 lsa_blob1.length = q.out.new_val->buf->length;
918 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
919 if (!NT_STATUS_IS_OK(status)) {
920 printf("Failed to decrypt secrets OLD blob\n");
924 if (!q.out.new_mtime) {
925 printf("NEW mtime not available on LSA for secret %s\n", new->name);
928 if (new->mtime != *q.out.new_mtime) {
929 printf("NEW mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
930 new->name, nt_time_string(mem_ctx, new->mtime),
931 nt_time_string(mem_ctx, *q.out.new_mtime));
935 if (new->secret.length != lsa_blob_out.length) {
936 printf("Returned secret %s doesn't match: %d != %d\n",
937 new->name, (int)new->secret.length, (int)lsa_blob_out.length);
939 } else if (memcmp(lsa_blob_out.data,
940 new->secret.data, new->secret.length) != 0) {
941 printf("Returned secret %s doesn't match: \n",
943 DEBUG(1, ("SamSync Secret:\n"));
944 dump_data(1, new->secret.data, new->secret.length);
945 DEBUG(1, ("LSA Secret:\n"));
946 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
954 static bool samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
955 int database_id, struct netr_DELTA_ENUM *delta)
959 struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
960 struct dom_sid *dom_sid = delta->delta_id_union.sid;
962 struct samsync_trusted_domain *new = talloc(samsync_state, struct samsync_trusted_domain);
963 struct lsa_OpenTrustedDomain t;
964 struct policy_handle trustdom_handle;
965 struct lsa_QueryTrustedDomainInfo q;
966 union lsa_TrustedDomainInfo *info[9];
967 int levels [] = {1, 3, 8};
970 new->name = talloc_reference(new, trusted_domain->domain_name.string);
971 new->sid = talloc_reference(new, dom_sid);
973 t.in.handle = samsync_state->lsa_handle;
974 t.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
976 t.out.trustdom_handle = &trustdom_handle;
978 status = dcerpc_lsa_OpenTrustedDomain(samsync_state->p_lsa, mem_ctx, &t);
979 if (!NT_STATUS_IS_OK(status)) {
980 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
984 for (i=0; i< ARRAY_SIZE(levels); i++) {
985 q.in.trustdom_handle = &trustdom_handle;
986 q.in.level = levels[i];
987 status = dcerpc_lsa_QueryTrustedDomainInfo(samsync_state->p_lsa, mem_ctx, &q);
988 if (!NT_STATUS_IS_OK(status)) {
989 if (q.in.level == 8 && NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER)) {
990 info[levels[i]] = NULL;
993 printf("QueryInfoTrustedDomain level %d failed - %s\n",
994 levels[i], nt_errstr(status));
997 info[levels[i]] = q.out.info;
1001 TEST_SID_EQUAL(info[8]->full_info.info_ex.sid, dom_sid);
1002 TEST_STRING_EQUAL(info[8]->full_info.info_ex.netbios_name, trusted_domain->domain_name);
1004 TEST_STRING_EQUAL(info[1]->name.netbios_name, trusted_domain->domain_name);
1005 TEST_INT_EQUAL(info[3]->posix_offset.posix_offset, trusted_domain->posix_offset);
1007 We would like to do this, but it is NOT_SUPPORTED on win2k3
1008 TEST_SEC_DESC_EQUAL(trusted_domain->sdbuf, lsa, &trustdom_handle);
1010 new = talloc_reference(samsync_state, new);
1011 DLIST_ADD(samsync_state->trusted_domains, new);
1016 static bool samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
1017 int database_id, struct netr_DELTA_ENUM *delta)
1021 struct netr_DELTA_ACCOUNT *account = delta->delta_union.account;
1022 struct dom_sid *dom_sid = delta->delta_id_union.sid;
1024 struct lsa_OpenAccount a;
1025 struct policy_handle acct_handle;
1026 struct lsa_EnumPrivsAccount e;
1027 struct lsa_LookupPrivName r;
1031 bool *found_priv_in_lsa;
1033 a.in.handle = samsync_state->lsa_handle;
1034 a.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1036 a.out.acct_handle = &acct_handle;
1038 status = dcerpc_lsa_OpenAccount(samsync_state->p_lsa, mem_ctx, &a);
1039 if (!NT_STATUS_IS_OK(status)) {
1040 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1044 TEST_SEC_DESC_EQUAL(account->sdbuf, lsa, &acct_handle);
1046 found_priv_in_lsa = talloc_zero_array(mem_ctx, bool, account->privilege_entries);
1048 e.in.handle = &acct_handle;
1050 status = dcerpc_lsa_EnumPrivsAccount(samsync_state->p_lsa, mem_ctx, &e);
1051 if (!NT_STATUS_IS_OK(status)) {
1052 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
1056 if ((account->privilege_entries && !e.out.privs)) {
1057 printf("Account %s has privileges in SamSync, but not LSA\n",
1058 dom_sid_string(mem_ctx, dom_sid));
1062 if (!account->privilege_entries && e.out.privs && e.out.privs->count) {
1063 printf("Account %s has privileges in LSA, but not SamSync\n",
1064 dom_sid_string(mem_ctx, dom_sid));
1068 TEST_INT_EQUAL(account->privilege_entries, e.out.privs->count);
1070 for (i=0;i< e.out.privs->count; i++) {
1071 r.in.handle = samsync_state->lsa_handle;
1072 r.in.luid = &e.out.privs->set[i].luid;
1074 status = dcerpc_lsa_LookupPrivName(samsync_state->p_lsa, mem_ctx, &r);
1075 if (!NT_STATUS_IS_OK(status)) {
1076 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
1081 printf("\nLookupPrivName failed to return a name\n");
1084 for (j=0;j<account->privilege_entries; j++) {
1085 if (strcmp(r.out.name->string, account->privilege_name[j].string) == 0) {
1086 found_priv_in_lsa[j] = true;
1091 for (j=0;j<account->privilege_entries; j++) {
1092 if (!found_priv_in_lsa[j]) {
1093 printf("Privilage %s on account %s not found in LSA\n", account->privilege_name[j].string,
1094 dom_sid_string(mem_ctx, dom_sid));
1102 try a netlogon DatabaseSync
1104 static bool test_DatabaseSync(struct samsync_state *samsync_state,
1105 TALLOC_CTX *mem_ctx)
1108 TALLOC_CTX *loop_ctx, *delta_ctx, *trustdom_ctx;
1109 struct netr_DatabaseSync r;
1110 const enum netr_SamDatabaseID database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS};
1113 struct samsync_trusted_domain *t;
1114 struct samsync_secret *s;
1116 const char *domain, *username;
1118 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1119 r.in.computername = TEST_MACHINE_NAME;
1120 r.in.preferredmaximumlength = (uint32_t)-1;
1121 ZERO_STRUCT(r.in.return_authenticator);
1123 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1124 r.in.sync_context = 0;
1125 r.in.database_id = database_ids[i];
1127 printf("Testing DatabaseSync of id %d\n", r.in.database_id);
1130 loop_ctx = talloc_named(mem_ctx, 0, "DatabaseSync loop context");
1131 creds_client_authenticator(samsync_state->creds, &r.in.credential);
1133 status = dcerpc_netr_DatabaseSync(samsync_state->p, loop_ctx, &r);
1134 if (!NT_STATUS_IS_OK(status) &&
1135 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1136 printf("DatabaseSync - %s\n", nt_errstr(status));
1141 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1142 printf("Credential chaining failed\n");
1145 r.in.sync_context = r.out.sync_context;
1147 for (d=0; d < r.out.delta_enum_array->num_deltas; d++) {
1148 delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
1149 switch (r.out.delta_enum_array->delta_enum[d].delta_type) {
1150 case NETR_DELTA_DOMAIN:
1151 if (!samsync_handle_domain(delta_ctx, samsync_state,
1152 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1153 printf("Failed to handle DELTA_DOMAIN\n");
1157 case NETR_DELTA_GROUP:
1158 if (!samsync_handle_group(delta_ctx, samsync_state,
1159 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1160 printf("Failed to handle DELTA_USER\n");
1164 case NETR_DELTA_USER:
1165 if (!samsync_handle_user(delta_ctx, samsync_state,
1166 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1167 printf("Failed to handle DELTA_USER\n");
1171 case NETR_DELTA_ALIAS:
1172 if (!samsync_handle_alias(delta_ctx, samsync_state,
1173 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1174 printf("Failed to handle DELTA_ALIAS\n");
1178 case NETR_DELTA_POLICY:
1179 if (!samsync_handle_policy(delta_ctx, samsync_state,
1180 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1181 printf("Failed to handle DELTA_POLICY\n");
1185 case NETR_DELTA_TRUSTED_DOMAIN:
1186 if (!samsync_handle_trusted_domain(delta_ctx, samsync_state,
1187 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1188 printf("Failed to handle DELTA_TRUSTED_DOMAIN\n");
1192 case NETR_DELTA_ACCOUNT:
1193 if (!samsync_handle_account(delta_ctx, samsync_state,
1194 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1195 printf("Failed to handle DELTA_ACCOUNT\n");
1199 case NETR_DELTA_SECRET:
1200 if (!samsync_handle_secret(delta_ctx, samsync_state,
1201 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1202 printf("Failed to handle DELTA_SECRET\n");
1206 case NETR_DELTA_GROUP_MEMBER:
1207 case NETR_DELTA_ALIAS_MEMBER:
1208 /* These are harder to cross-check, and we expect them */
1210 case NETR_DELTA_DELETE_GROUP:
1211 case NETR_DELTA_RENAME_GROUP:
1212 case NETR_DELTA_DELETE_USER:
1213 case NETR_DELTA_RENAME_USER:
1214 case NETR_DELTA_DELETE_ALIAS:
1215 case NETR_DELTA_RENAME_ALIAS:
1216 case NETR_DELTA_DELETE_TRUST:
1217 case NETR_DELTA_DELETE_ACCOUNT:
1218 case NETR_DELTA_DELETE_SECRET:
1219 case NETR_DELTA_DELETE_GROUP2:
1220 case NETR_DELTA_DELETE_USER2:
1221 case NETR_DELTA_MODIFY_COUNT:
1223 printf("Uxpected delta type %d\n", r.out.delta_enum_array->delta_enum[d].delta_type);
1227 talloc_free(delta_ctx);
1229 talloc_free(loop_ctx);
1230 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1234 domain = samsync_state->domain_name[SAM_DATABASE_DOMAIN];
1236 printf("Never got a DOMAIN object in samsync!\n");
1240 trustdom_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync Trusted domains context");
1242 username = talloc_asprintf(trustdom_ctx, "%s$", domain);
1243 for (t=samsync_state->trusted_domains; t; t=t->next) {
1244 char *secret_name = talloc_asprintf(trustdom_ctx, "G$$%s", t->name);
1245 for (s=samsync_state->secrets; s; s=s->next) {
1246 if (strcasecmp_m(s->name, secret_name) == 0) {
1248 struct samr_Password nt_hash;
1249 mdfour(nt_hash.hash, s->secret.data, s->secret.length);
1251 printf("Checking password for %s\\%s\n", t->name, username);
1252 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1255 TEST_WKSTA_MACHINE_NAME,
1259 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS)) {
1260 printf("Verifiction of trust password to %s failed: %s (the trusted domain is not available)\n",
1261 t->name, nt_errstr(nt_status));
1265 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1266 printf("Verifiction of trust password to %s: should have failed (nologon interdomain trust account), instead: %s\n",
1267 t->name, nt_errstr(nt_status));
1273 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1276 TEST_WKSTA_MACHINE_NAME,
1281 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
1282 printf("Verifiction of trust password to %s: should have failed (wrong password), instead: %s\n",
1283 t->name, nt_errstr(nt_status));
1291 talloc_free(trustdom_ctx);
1297 try a netlogon DatabaseDeltas
1299 static bool test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX *mem_ctx)
1302 TALLOC_CTX *loop_ctx;
1303 struct netr_DatabaseDeltas r;
1304 const uint32_t database_ids[] = {0, 1, 2};
1308 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1309 r.in.computername = TEST_MACHINE_NAME;
1310 r.in.preferredmaximumlength = (uint32_t)-1;
1311 ZERO_STRUCT(r.in.return_authenticator);
1313 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1314 r.in.database_id = database_ids[i];
1315 r.in.sequence_num = samsync_state->seq_num[i];
1317 if (r.in.sequence_num == 0) continue;
1319 /* this shows that the bdc doesn't need to do a single call for
1320 * each seqnumber, and the pdc doesn't need to know about old values
1323 r.in.sequence_num -= 10;
1326 printf("Testing DatabaseDeltas of id %d at %llu\n",
1327 r.in.database_id, (long long)r.in.sequence_num);
1330 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseDeltas loop context");
1331 creds_client_authenticator(samsync_state->creds, &r.in.credential);
1333 status = dcerpc_netr_DatabaseDeltas(samsync_state->p, loop_ctx, &r);
1334 if (!NT_STATUS_IS_OK(status) &&
1335 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) &&
1336 !NT_STATUS_EQUAL(status, NT_STATUS_SYNCHRONIZATION_REQUIRED)) {
1337 printf("DatabaseDeltas - %s\n", nt_errstr(status));
1341 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1342 printf("Credential chaining failed\n");
1345 r.in.sequence_num++;
1346 talloc_free(loop_ctx);
1347 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1355 try a netlogon DatabaseSync2
1357 static bool test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1358 struct creds_CredentialState *creds)
1361 TALLOC_CTX *loop_ctx;
1362 struct netr_DatabaseSync2 r;
1363 const uint32_t database_ids[] = {0, 1, 2};
1367 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1368 r.in.computername = TEST_MACHINE_NAME;
1369 r.in.preferredmaximumlength = (uint32_t)-1;
1370 ZERO_STRUCT(r.in.return_authenticator);
1372 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1373 r.in.sync_context = 0;
1374 r.in.database_id = database_ids[i];
1375 r.in.restart_state = 0;
1377 printf("Testing DatabaseSync2 of id %d\n", r.in.database_id);
1380 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync2 loop context");
1381 creds_client_authenticator(creds, &r.in.credential);
1383 status = dcerpc_netr_DatabaseSync2(p, loop_ctx, &r);
1384 if (!NT_STATUS_IS_OK(status) &&
1385 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1386 printf("DatabaseSync2 - %s\n", nt_errstr(status));
1390 if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
1391 printf("Credential chaining failed\n");
1394 r.in.sync_context = r.out.sync_context;
1395 talloc_free(loop_ctx);
1396 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1404 bool torture_rpc_samsync(struct torture_context *torture)
1407 TALLOC_CTX *mem_ctx;
1409 struct test_join *join_ctx;
1410 struct test_join *join_ctx2;
1411 struct test_join *user_ctx;
1412 const char *machine_password;
1413 const char *wksta_machine_password;
1414 struct dcerpc_binding *b;
1415 struct dcerpc_binding *b_netlogon_wksta;
1416 struct samr_Connect c;
1417 struct samr_SetDomainInfo s;
1418 struct policy_handle *domain_policy;
1420 struct lsa_ObjectAttribute attr;
1421 struct lsa_QosInfo qos;
1422 struct lsa_OpenPolicy2 r;
1423 struct cli_credentials *credentials;
1424 struct cli_credentials *credentials_wksta;
1426 struct samsync_state *samsync_state;
1428 char *test_machine_account;
1430 char *test_wksta_machine_account;
1432 mem_ctx = talloc_init("torture_rpc_netlogon");
1434 test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1435 join_ctx = torture_create_testuser(torture, test_machine_account,
1436 lp_workgroup(torture->lp_ctx), ACB_SVRTRUST,
1439 talloc_free(mem_ctx);
1440 printf("Failed to join as BDC\n");
1444 test_wksta_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_WKSTA_MACHINE_NAME);
1445 join_ctx2 = torture_create_testuser(torture, test_wksta_machine_account, lp_workgroup(torture->lp_ctx), ACB_WSTRUST, &wksta_machine_password);
1447 talloc_free(mem_ctx);
1448 printf("Failed to join as member\n");
1452 user_ctx = torture_create_testuser(torture, TEST_USER_NAME,
1453 lp_workgroup(torture->lp_ctx),
1456 talloc_free(mem_ctx);
1457 printf("Failed to create test account\n");
1461 samsync_state = talloc_zero(mem_ctx, struct samsync_state);
1463 samsync_state->p_samr = torture_join_samr_pipe(join_ctx);
1464 samsync_state->connect_handle = talloc_zero(samsync_state, struct policy_handle);
1465 samsync_state->lsa_handle = talloc_zero(samsync_state, struct policy_handle);
1466 c.in.system_name = NULL;
1467 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1468 c.out.connect_handle = samsync_state->connect_handle;
1470 status = dcerpc_samr_Connect(samsync_state->p_samr, mem_ctx, &c);
1471 if (!NT_STATUS_IS_OK(status)) {
1472 printf("samr_Connect failed\n");
1477 domain_policy = samsync_open_domain(mem_ctx, samsync_state, lp_workgroup(torture->lp_ctx), NULL);
1478 if (!domain_policy) {
1479 printf("samrsync_open_domain failed\n");
1484 s.in.domain_handle = domain_policy;
1486 s.in.info = talloc(mem_ctx, union samr_DomainInfo);
1488 s.in.info->info4.comment.string
1489 = talloc_asprintf(mem_ctx,
1490 "Tortured by Samba4: %s",
1491 timestring(mem_ctx, time(NULL)));
1492 status = dcerpc_samr_SetDomainInfo(samsync_state->p_samr, mem_ctx, &s);
1494 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, domain_policy)) {
1499 if (!NT_STATUS_IS_OK(status)) {
1500 printf("SetDomainInfo level %u failed - %s\n",
1501 s.in.level, nt_errstr(status));
1507 status = torture_rpc_connection(torture,
1508 &samsync_state->p_lsa,
1511 if (!NT_STATUS_IS_OK(status)) {
1517 qos.impersonation_level = 2;
1518 qos.context_mode = 1;
1519 qos.effective_only = 0;
1522 attr.root_dir = NULL;
1523 attr.object_name = NULL;
1524 attr.attributes = 0;
1525 attr.sec_desc = NULL;
1526 attr.sec_qos = &qos;
1528 r.in.system_name = "\\";
1530 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1531 r.out.handle = samsync_state->lsa_handle;
1533 status = dcerpc_lsa_OpenPolicy2(samsync_state->p_lsa, mem_ctx, &r);
1534 if (!NT_STATUS_IS_OK(status)) {
1535 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1540 status = torture_rpc_binding(torture, &b);
1541 if (!NT_STATUS_IS_OK(status)) {
1546 b->flags &= ~DCERPC_AUTH_OPTIONS;
1547 b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1549 credentials = cli_credentials_init(mem_ctx);
1551 cli_credentials_set_workstation(credentials, TEST_MACHINE_NAME, CRED_SPECIFIED);
1552 cli_credentials_set_domain(credentials, lp_workgroup(torture->lp_ctx), CRED_SPECIFIED);
1553 cli_credentials_set_username(credentials, test_machine_account, CRED_SPECIFIED);
1554 cli_credentials_set_password(credentials, machine_password, CRED_SPECIFIED);
1555 cli_credentials_set_secure_channel_type(credentials,
1558 status = dcerpc_pipe_connect_b(samsync_state,
1559 &samsync_state->p, b,
1560 &ndr_table_netlogon,
1561 credentials, NULL, torture->lp_ctx);
1563 if (!NT_STATUS_IS_OK(status)) {
1564 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
1569 status = dcerpc_schannel_creds(samsync_state->p->conn->security_state.generic_state,
1570 samsync_state, &samsync_state->creds);
1571 if (!NT_STATUS_IS_OK(status)) {
1577 status = torture_rpc_binding(torture, &b_netlogon_wksta);
1578 if (!NT_STATUS_IS_OK(status)) {
1583 b_netlogon_wksta->flags &= ~DCERPC_AUTH_OPTIONS;
1584 b_netlogon_wksta->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1586 credentials_wksta = cli_credentials_init(mem_ctx);
1588 cli_credentials_set_workstation(credentials_wksta, TEST_WKSTA_MACHINE_NAME, CRED_SPECIFIED);
1589 cli_credentials_set_domain(credentials_wksta, lp_workgroup(torture->lp_ctx), CRED_SPECIFIED);
1590 cli_credentials_set_username(credentials_wksta, test_wksta_machine_account, CRED_SPECIFIED);
1591 cli_credentials_set_password(credentials_wksta, wksta_machine_password, CRED_SPECIFIED);
1592 cli_credentials_set_secure_channel_type(credentials_wksta,
1595 status = dcerpc_pipe_connect_b(samsync_state,
1596 &samsync_state->p_netlogon_wksta,
1598 &ndr_table_netlogon,
1599 credentials_wksta, NULL, torture->lp_ctx);
1601 if (!NT_STATUS_IS_OK(status)) {
1602 printf("Failed to connect to server as a Workstation: %s\n", nt_errstr(status));
1607 status = dcerpc_schannel_creds(samsync_state->p_netlogon_wksta->conn->security_state.generic_state,
1608 samsync_state, &samsync_state->creds_netlogon_wksta);
1609 if (!NT_STATUS_IS_OK(status)) {
1610 printf("Failed to obtail schanel creds!\n");
1614 if (!test_DatabaseSync(samsync_state, mem_ctx)) {
1615 printf("DatabaseSync failed\n");
1619 if (!test_DatabaseDeltas(samsync_state, mem_ctx)) {
1620 printf("DatabaseDeltas failed\n");
1624 if (!test_DatabaseSync2(samsync_state->p, mem_ctx, samsync_state->creds)) {
1625 printf("DatabaseSync2 failed\n");
1630 torture_leave_domain(join_ctx);
1631 torture_leave_domain(join_ctx2);
1632 torture_leave_domain(user_ctx);
1634 talloc_free(mem_ctx);