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"
40 #define TEST_MACHINE_NAME "samsynctest"
41 #define TEST_WKSTA_MACHINE_NAME "samsynctest2"
42 #define TEST_USER_NAME "samsynctestuser"
45 try a netlogon SamLogon
47 static NTSTATUS test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
48 struct creds_CredentialState *creds,
49 const char *domain, const char *account_name,
50 const char *workstation,
51 struct samr_Password *lm_hash,
52 struct samr_Password *nt_hash,
53 struct netr_SamInfo3 **info3)
56 struct netr_LogonSamLogon r;
57 struct netr_Authenticator auth, auth2;
58 struct netr_NetworkInfo ninfo;
60 ninfo.identity_info.domain_name.string = domain;
61 ninfo.identity_info.parameter_control = 0;
62 ninfo.identity_info.logon_id_low = 0;
63 ninfo.identity_info.logon_id_high = 0;
64 ninfo.identity_info.account_name.string = account_name;
65 ninfo.identity_info.workstation.string = workstation;
66 generate_random_buffer(ninfo.challenge,
67 sizeof(ninfo.challenge));
70 ninfo.nt.data = talloc_size(mem_ctx, 24);
71 SMBOWFencrypt(nt_hash->hash, ninfo.challenge, ninfo.nt.data);
79 ninfo.lm.data = talloc_size(mem_ctx, 24);
80 SMBOWFencrypt(lm_hash->hash, ninfo.challenge, ninfo.lm.data);
86 r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
87 r.in.computer_name = workstation;
88 r.in.credential = &auth;
89 r.in.return_authenticator = &auth2;
91 r.in.logon.network = &ninfo;
94 creds_client_authenticator(creds, &auth);
96 r.in.validation_level = 3;
98 status = dcerpc_netr_LogonSamLogon(p, mem_ctx, &r);
100 if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
101 printf("Credential chaining failed\n");
105 *info3 = r.out.validation.sam3;
111 struct samsync_state {
112 /* we remember the sequence numbers so we can easily do a DatabaseDelta */
114 const char *domain_name[2];
115 struct samsync_secret *secrets;
116 struct samsync_trusted_domain *trusted_domains;
117 struct creds_CredentialState *creds;
118 struct creds_CredentialState *creds_netlogon_wksta;
119 struct policy_handle *connect_handle;
120 struct policy_handle *domain_handle[2];
121 struct dom_sid *sid[2];
122 struct dcerpc_pipe *p;
123 struct dcerpc_pipe *p_netlogon_wksta;
124 struct dcerpc_pipe *p_samr;
125 struct dcerpc_pipe *p_lsa;
126 struct policy_handle *lsa_handle;
129 struct samsync_secret {
130 struct samsync_secret *prev, *next;
136 struct samsync_trusted_domain {
137 struct samsync_trusted_domain *prev, *next;
142 static struct policy_handle *samsync_open_domain(TALLOC_CTX *mem_ctx,
143 struct samsync_state *samsync_state,
145 struct dom_sid **sid)
147 struct lsa_String name;
148 struct samr_OpenDomain o;
149 struct samr_LookupDomain l;
150 struct policy_handle *domain_handle = talloc(mem_ctx, struct policy_handle);
153 name.string = domain;
154 l.in.connect_handle = samsync_state->connect_handle;
155 l.in.domain_name = &name;
157 nt_status = dcerpc_samr_LookupDomain(samsync_state->p_samr, mem_ctx, &l);
158 if (!NT_STATUS_IS_OK(nt_status)) {
159 printf("LookupDomain failed - %s\n", nt_errstr(nt_status));
163 o.in.connect_handle = samsync_state->connect_handle;
164 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
165 o.in.sid = l.out.sid;
166 o.out.domain_handle = domain_handle;
172 nt_status = dcerpc_samr_OpenDomain(samsync_state->p_samr, mem_ctx, &o);
173 if (!NT_STATUS_IS_OK(nt_status)) {
174 printf("OpenDomain failed - %s\n", nt_errstr(nt_status));
178 return domain_handle;
181 static struct sec_desc_buf *samsync_query_samr_sec_desc(TALLOC_CTX *mem_ctx,
182 struct samsync_state *samsync_state,
183 struct policy_handle *handle)
185 struct samr_QuerySecurity r;
188 r.in.handle = handle;
191 status = dcerpc_samr_QuerySecurity(samsync_state->p_samr, mem_ctx, &r);
192 if (!NT_STATUS_IS_OK(status)) {
193 printf("SAMR QuerySecurity failed - %s\n", nt_errstr(status));
200 static struct sec_desc_buf *samsync_query_lsa_sec_desc(TALLOC_CTX *mem_ctx,
201 struct samsync_state *samsync_state,
202 struct policy_handle *handle)
204 struct lsa_QuerySecurity r;
207 r.in.handle = handle;
210 status = dcerpc_lsa_QuerySecurity(samsync_state->p_lsa, mem_ctx, &r);
211 if (!NT_STATUS_IS_OK(status)) {
212 printf("LSA QuerySecurity failed - %s\n", nt_errstr(status));
219 #define TEST_UINT64_EQUAL(i1, i2) do {\
221 printf("%s: uint64 mismatch: " #i1 ": 0x%016llx (%lld) != " #i2 ": 0x%016llx (%lld)\n", \
223 (long long)i1, (long long)i1, \
224 (long long)i2, (long long)i2);\
228 #define TEST_INT_EQUAL(i1, i2) do {\
230 printf("%s: integer mismatch: " #i1 ": 0x%08x (%d) != " #i2 ": 0x%08x (%d)\n", \
231 __location__, i1, i1, i2, i2); \
235 #define TEST_TIME_EQUAL(t1, t2) do {\
237 printf("%s: NTTIME mismatch: " #t1 ":%s != " #t2 ": %s\n", \
238 __location__, nt_time_string(mem_ctx, t1), nt_time_string(mem_ctx, t2));\
243 #define TEST_STRING_EQUAL(s1, s2) do {\
244 if (!((!s1.string || s1.string[0]=='\0') && (!s2.string || s2.string[0]=='\0')) \
245 && strcmp_safe(s1.string, s2.string) != 0) {\
246 printf("%s: string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
247 __location__, s1.string, s2.string);\
252 #define TEST_SID_EQUAL(s1, s2) do {\
253 if (!dom_sid_equal(s1, s2)) {\
254 printf("%s: dom_sid mismatch: " #s1 ":%s != " #s2 ": %s\n", \
255 __location__, dom_sid_string(mem_ctx, s1), dom_sid_string(mem_ctx, s2));\
260 /* The ~SEC_DESC_SACL_PRESENT is because we don't, as administrator,
261 * get back the SACL part of the SD when we ask over SAMR */
263 #define TEST_SEC_DESC_EQUAL(sd1, pipe, handle) do {\
264 struct sec_desc_buf *sdbuf = samsync_query_ ##pipe## _sec_desc(mem_ctx, samsync_state, \
266 if (!sdbuf || !sdbuf->sd) { \
267 printf("Could not obtain security descriptor to match " #sd1 "\n");\
270 if (!security_descriptor_mask_equal(sd1.sd, sdbuf->sd, \
271 ~SEC_DESC_SACL_PRESENT)) {\
272 printf("Security Descriptor Mismatch for %s:\n", #sd1);\
273 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamSync", sd1.sd);\
274 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamR", sdbuf->sd);\
280 static BOOL samsync_handle_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
281 int database_id, struct netr_DELTA_ENUM *delta)
283 struct netr_DELTA_DOMAIN *domain = delta->delta_union.domain;
284 struct dom_sid *dom_sid;
285 struct samr_QueryDomainInfo q[14]; /* q[0] will be unused simple for clarity */
286 uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
291 samsync_state->seq_num[database_id] =
292 domain->sequence_num;
293 switch (database_id) {
294 case SAM_DATABASE_DOMAIN:
296 case SAM_DATABASE_BUILTIN:
297 if (strcasecmp_m("BUILTIN", domain->domain_name.string) != 0) {
298 printf("BUILTIN domain has different name: %s\n", domain->domain_name.string);
301 case SAM_DATABASE_PRIVS:
302 printf("DOMAIN entry on privs DB!\n");
307 if (!samsync_state->domain_name[database_id]) {
308 samsync_state->domain_name[database_id] =
309 talloc_reference(samsync_state, domain->domain_name.string);
311 if (strcasecmp_m(samsync_state->domain_name[database_id], domain->domain_name.string) != 0) {
312 printf("Domain has name varies!: %s != %s\n", samsync_state->domain_name[database_id],
313 domain->domain_name.string);
318 if (!samsync_state->domain_handle[database_id]) {
319 samsync_state->domain_handle[database_id]
320 = talloc_reference(samsync_state,
321 samsync_open_domain(mem_ctx, samsync_state, samsync_state->domain_name[database_id],
324 if (samsync_state->domain_handle[database_id]) {
325 samsync_state->sid[database_id] = talloc_reference(samsync_state, dom_sid);
328 printf("\tsequence_nums[%d/%s]=%llu\n",
329 database_id, domain->domain_name.string,
330 (long long)samsync_state->seq_num[database_id]);
332 for (i=0;i<ARRAY_SIZE(levels);i++) {
333 q[levels[i]].in.domain_handle = samsync_state->domain_handle[database_id];
334 q[levels[i]].in.level = levels[i];
336 nt_status = dcerpc_samr_QueryDomainInfo(samsync_state->p_samr, mem_ctx, &q[levels[i]]);
338 if (!NT_STATUS_IS_OK(nt_status)) {
339 printf("QueryDomainInfo level %u failed - %s\n",
340 q[levels[i]].in.level, nt_errstr(nt_status));
345 TEST_STRING_EQUAL(q[5].out.info->info5.domain_name, domain->domain_name);
347 TEST_STRING_EQUAL(q[2].out.info->info2.comment, domain->comment);
348 TEST_STRING_EQUAL(q[4].out.info->info4.comment, domain->comment);
349 TEST_TIME_EQUAL(q[2].out.info->info2.force_logoff_time, domain->force_logoff_time);
350 TEST_TIME_EQUAL(q[3].out.info->info3.force_logoff_time, domain->force_logoff_time);
352 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_length, domain->min_password_length);
353 TEST_TIME_EQUAL(q[1].out.info->info1.password_history_length, domain->password_history_length);
354 TEST_TIME_EQUAL(q[1].out.info->info1.max_password_age, domain->max_password_age);
355 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_age, domain->min_password_age);
357 TEST_UINT64_EQUAL(q[8].out.info->info8.sequence_num,
358 domain->sequence_num);
359 TEST_TIME_EQUAL(q[8].out.info->info8.domain_create_time,
360 domain->domain_create_time);
361 TEST_TIME_EQUAL(q[13].out.info->info13.domain_create_time,
362 domain->domain_create_time);
364 TEST_SEC_DESC_EQUAL(domain->sdbuf, samr, samsync_state->domain_handle[database_id]);
369 static BOOL samsync_handle_policy(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
370 int database_id, struct netr_DELTA_ENUM *delta)
372 struct netr_DELTA_POLICY *policy = delta->delta_union.policy;
374 samsync_state->seq_num[database_id] =
375 policy->sequence_num;
377 if (!samsync_state->domain_name[SAM_DATABASE_DOMAIN]) {
378 samsync_state->domain_name[SAM_DATABASE_DOMAIN] =
379 talloc_reference(samsync_state, policy->primary_domain_name.string);
381 if (strcasecmp_m(samsync_state->domain_name[SAM_DATABASE_DOMAIN], policy->primary_domain_name.string) != 0) {
382 printf("PRIMARY domain has name varies between DOMAIN and POLICY!: %s != %s\n", samsync_state->domain_name[SAM_DATABASE_DOMAIN],
383 policy->primary_domain_name.string);
388 if (!dom_sid_equal(samsync_state->sid[SAM_DATABASE_DOMAIN], policy->sid)) {
389 printf("Domain SID from POLICY (%s) does not match domain sid from SAMR (%s)\n",
390 dom_sid_string(mem_ctx, policy->sid), dom_sid_string(mem_ctx, samsync_state->sid[SAM_DATABASE_DOMAIN]));
394 printf("\tsequence_nums[%d/PRIVS]=%llu\n",
396 (long long)samsync_state->seq_num[database_id]);
400 static BOOL samsync_handle_user(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
401 int database_id, struct netr_DELTA_ENUM *delta)
403 uint32_t rid = delta->delta_id_union.rid;
404 struct netr_DELTA_USER *user = delta->delta_union.user;
405 struct netr_SamInfo3 *info3;
406 struct samr_Password lm_hash;
407 struct samr_Password nt_hash;
408 struct samr_Password *lm_hash_p = NULL;
409 struct samr_Password *nt_hash_p = NULL;
410 const char *domain = samsync_state->domain_name[database_id];
411 const char *username = user->account_name.string;
415 struct samr_OpenUser r;
416 struct samr_QueryUserInfo q;
417 struct policy_handle user_handle;
419 struct samr_GetGroupsForUser getgroups;
420 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
421 printf("SamSync needs domain information before the users\n");
425 r.in.domain_handle = samsync_state->domain_handle[database_id];
426 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
428 r.out.user_handle = &user_handle;
430 nt_status = dcerpc_samr_OpenUser(samsync_state->p_samr, mem_ctx, &r);
431 if (!NT_STATUS_IS_OK(nt_status)) {
432 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
436 q.in.user_handle = &user_handle;
439 TEST_SEC_DESC_EQUAL(user->sdbuf, samr, &user_handle);
441 nt_status = dcerpc_samr_QueryUserInfo(samsync_state->p_samr, mem_ctx, &q);
442 if (!NT_STATUS_IS_OK(nt_status)) {
443 printf("QueryUserInfo level %u failed - %s\n",
444 q.in.level, nt_errstr(nt_status));
448 getgroups.in.user_handle = &user_handle;
450 nt_status = dcerpc_samr_GetGroupsForUser(samsync_state->p_samr, mem_ctx, &getgroups);
451 if (!NT_STATUS_IS_OK(nt_status)) {
452 printf("GetGroupsForUser failed - %s\n",
453 nt_errstr(nt_status));
457 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &user_handle)) {
458 printf("samr_handle_Close failed - %s\n",
459 nt_errstr(nt_status));
466 if (!NT_STATUS_IS_OK(nt_status)) {
467 printf("QueryUserInfo level %u failed - %s\n",
468 q.in.level, nt_errstr(nt_status));
472 TEST_STRING_EQUAL(q.out.info->info21.account_name, user->account_name);
473 TEST_STRING_EQUAL(q.out.info->info21.full_name, user->full_name);
474 TEST_INT_EQUAL(q.out.info->info21.rid, user->rid);
475 TEST_INT_EQUAL(q.out.info->info21.primary_gid, user->primary_gid);
476 TEST_STRING_EQUAL(q.out.info->info21.home_directory, user->home_directory);
477 TEST_STRING_EQUAL(q.out.info->info21.home_drive, user->home_drive);
478 TEST_STRING_EQUAL(q.out.info->info21.logon_script, user->logon_script);
479 TEST_STRING_EQUAL(q.out.info->info21.description, user->description);
480 TEST_STRING_EQUAL(q.out.info->info21.workstations, user->workstations);
482 TEST_TIME_EQUAL(q.out.info->info21.last_logon, user->last_logon);
483 TEST_TIME_EQUAL(q.out.info->info21.last_logoff, user->last_logoff);
486 TEST_INT_EQUAL(q.out.info->info21.logon_hours.units_per_week,
487 user->logon_hours.units_per_week);
489 if (memcmp(q.out.info->info21.logon_hours.bits, user->logon_hours.bits,
490 q.out.info->info21.logon_hours.units_per_week/8) != 0) {
491 printf("Logon hours mismatch\n");
496 TEST_INT_EQUAL(q.out.info->info21.bad_password_count,
497 user->bad_password_count);
498 TEST_INT_EQUAL(q.out.info->info21.logon_count,
501 TEST_TIME_EQUAL(q.out.info->info21.last_password_change,
502 user->last_password_change);
503 TEST_TIME_EQUAL(q.out.info->info21.acct_expiry,
506 TEST_INT_EQUAL((q.out.info->info21.acct_flags & ~ACB_PW_EXPIRED), user->acct_flags);
507 if (user->acct_flags & ACB_PWNOEXP) {
508 if (q.out.info->info21.acct_flags & ACB_PW_EXPIRED) {
509 printf("ACB flags mismatch: both expired and no expiry!\n");
512 if (q.out.info->info21.force_password_change != (NTTIME)0x7FFFFFFFFFFFFFFFULL) {
513 printf("ACB flags mismatch: no password expiry, but force password change 0x%016llx (%lld) != 0x%016llx (%lld)\n",
514 (unsigned long long)q.out.info->info21.force_password_change,
515 (unsigned long long)q.out.info->info21.force_password_change,
516 (unsigned long long)0x7FFFFFFFFFFFFFFFULL, (unsigned long long)0x7FFFFFFFFFFFFFFFULL
522 TEST_INT_EQUAL(q.out.info->info21.nt_password_set, user->nt_password_present);
523 TEST_INT_EQUAL(q.out.info->info21.lm_password_set, user->lm_password_present);
524 TEST_INT_EQUAL(q.out.info->info21.password_expired, user->password_expired);
526 TEST_STRING_EQUAL(q.out.info->info21.comment, user->comment);
527 TEST_STRING_EQUAL(q.out.info->info21.parameters, user->parameters);
529 TEST_INT_EQUAL(q.out.info->info21.country_code, user->country_code);
530 TEST_INT_EQUAL(q.out.info->info21.code_page, user->code_page);
532 TEST_STRING_EQUAL(q.out.info->info21.profile_path, user->profile_path);
534 if (user->lm_password_present) {
535 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
536 lm_hash_p = &lm_hash;
538 if (user->nt_password_present) {
539 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
540 nt_hash_p = &nt_hash;
543 if (user->user_private_info.SensitiveData) {
545 struct netr_USER_KEYS keys;
546 data.data = user->user_private_info.SensitiveData;
547 data.length = user->user_private_info.DataLength;
548 creds_arcfour_crypt(samsync_state->creds, data.data, data.length);
549 nt_status = ndr_pull_struct_blob(&data, mem_ctx, &keys, (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
550 if (NT_STATUS_IS_OK(nt_status)) {
551 if (keys.keys.keys2.lmpassword.length == 16) {
552 sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
553 lm_hash_p = &lm_hash;
555 if (keys.keys.keys2.ntpassword.length == 16) {
556 sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
557 nt_hash_p = &nt_hash;
560 printf("Failed to parse Sensitive Data for %s:\n", username);
562 dump_data(0, data.data, data.length);
569 DATA_BLOB nt_hash_blob = data_blob_const(nt_hash_p, 16);
570 DEBUG(100,("ACCOUNT [%s\\%-25s] NTHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string(mem_ctx, &nt_hash_blob)));
573 DATA_BLOB lm_hash_blob = data_blob_const(lm_hash_p, 16);
574 DEBUG(100,("ACCOUNT [%s\\%-25s] LMHASH %s\n", samsync_state->domain_name[0], username, data_blob_hex_string(mem_ctx, &lm_hash_blob)));
577 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta,
580 TEST_WKSTA_MACHINE_NAME,
585 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED)) {
586 if (user->acct_flags & ACB_DISABLED) {
589 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)) {
590 if (user->acct_flags & ACB_WSTRUST) {
593 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT)) {
594 if (user->acct_flags & ACB_SVRTRUST) {
597 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
598 if (user->acct_flags & ACB_DOMTRUST) {
601 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
602 if (user->acct_flags & ACB_DOMTRUST) {
605 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT)) {
606 if (user->acct_flags & ACB_AUTOLOCK) {
609 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED)) {
610 if (q.out.info->info21.acct_flags & ACB_PW_EXPIRED) {
613 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
614 if (!lm_hash_p && !nt_hash_p) {
617 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE)) {
618 /* We would need to know the server's current time to test this properly */
620 } else if (NT_STATUS_IS_OK(nt_status)) {
621 TEST_INT_EQUAL(user->rid, info3->base.rid);
622 TEST_INT_EQUAL(user->primary_gid, info3->base.primary_gid);
623 /* this is 0x0 from NT4 sp6 */
624 if (info3->base.acct_flags) {
625 TEST_INT_EQUAL(user->acct_flags, info3->base.acct_flags);
627 /* this is NULL from NT4 sp6 */
628 if (info3->base.account_name.string) {
629 TEST_STRING_EQUAL(user->account_name, info3->base.account_name);
631 /* this is NULL from Win2k3 */
632 if (info3->base.full_name.string) {
633 TEST_STRING_EQUAL(user->full_name, info3->base.full_name);
635 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
636 TEST_STRING_EQUAL(user->profile_path, info3->base.profile_path);
637 TEST_STRING_EQUAL(user->home_directory, info3->base.home_directory);
638 TEST_STRING_EQUAL(user->home_drive, info3->base.home_drive);
639 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
642 TEST_TIME_EQUAL(user->last_logon, info3->base.last_logon);
643 TEST_TIME_EQUAL(user->acct_expiry, info3->base.acct_expiry);
644 TEST_TIME_EQUAL(user->last_password_change, info3->base.last_password_change);
645 TEST_TIME_EQUAL(q.out.info->info21.force_password_change, info3->base.force_password_change);
647 /* Does the concept of a logoff time ever really
648 * exist? (not in any sensible way, according to the
649 * doco I read -- abartlet) */
651 /* This copes with the two different versions of 0 I see */
652 /* with NT4 sp6 we have the || case */
653 if (!((user->last_logoff == 0)
654 || (info3->base.last_logoff == 0x7fffffffffffffffLL))) {
655 TEST_TIME_EQUAL(user->last_logoff, info3->base.last_logoff);
658 TEST_INT_EQUAL(getgroups.out.rids->count, info3->base.groups.count);
659 if (getgroups.out.rids->count == info3->base.groups.count) {
661 int count = getgroups.out.rids->count;
662 BOOL *matched = talloc_zero_array(mem_ctx, BOOL, getgroups.out.rids->count);
664 for (i = 0; i < count; i++) {
665 for (j = 0; j < count; j++) {
666 if ((getgroups.out.rids->rids[i].rid ==
667 info3->base.groups.rids[j].rid)
668 && (getgroups.out.rids->rids[i].attributes ==
669 info3->base.groups.rids[j].attributes)) {
675 for (i = 0; i < getgroups.out.rids->count; i++) {
676 if (matched[i] == False) {
678 printf("Could not find group RID %u found in getgroups in NETLOGON reply\n",
679 getgroups.out.rids->rids[i].rid);
685 printf("Could not validate password for user %s\\%s: %s\n",
686 domain, username, nt_errstr(nt_status));
692 static BOOL samsync_handle_alias(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
693 int database_id, struct netr_DELTA_ENUM *delta)
695 uint32_t rid = delta->delta_id_union.rid;
696 struct netr_DELTA_ALIAS *alias = delta->delta_union.alias;
700 struct samr_OpenAlias r;
701 struct samr_QueryAliasInfo q;
702 struct policy_handle alias_handle;
704 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
705 printf("SamSync needs domain information before the users\n");
709 r.in.domain_handle = samsync_state->domain_handle[database_id];
710 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
712 r.out.alias_handle = &alias_handle;
714 nt_status = dcerpc_samr_OpenAlias(samsync_state->p_samr, mem_ctx, &r);
715 if (!NT_STATUS_IS_OK(nt_status)) {
716 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
720 q.in.alias_handle = &alias_handle;
723 TEST_SEC_DESC_EQUAL(alias->sdbuf, samr, &alias_handle);
725 nt_status = dcerpc_samr_QueryAliasInfo(samsync_state->p_samr, mem_ctx, &q);
726 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &alias_handle)) {
730 if (!NT_STATUS_IS_OK(nt_status)) {
731 printf("QueryAliasInfo level %u failed - %s\n",
732 q.in.level, nt_errstr(nt_status));
736 TEST_STRING_EQUAL(q.out.info->all.name, alias->alias_name);
737 TEST_STRING_EQUAL(q.out.info->all.description, alias->description);
741 static BOOL samsync_handle_group(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
742 int database_id, struct netr_DELTA_ENUM *delta)
744 uint32_t rid = delta->delta_id_union.rid;
745 struct netr_DELTA_GROUP *group = delta->delta_union.group;
749 struct samr_OpenGroup r;
750 struct samr_QueryGroupInfo q;
751 struct policy_handle group_handle;
753 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
754 printf("SamSync needs domain information before the users\n");
758 r.in.domain_handle = samsync_state->domain_handle[database_id];
759 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
761 r.out.group_handle = &group_handle;
763 nt_status = dcerpc_samr_OpenGroup(samsync_state->p_samr, mem_ctx, &r);
764 if (!NT_STATUS_IS_OK(nt_status)) {
765 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
769 q.in.group_handle = &group_handle;
772 TEST_SEC_DESC_EQUAL(group->sdbuf, samr, &group_handle);
774 nt_status = dcerpc_samr_QueryGroupInfo(samsync_state->p_samr, mem_ctx, &q);
775 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &group_handle)) {
779 if (!NT_STATUS_IS_OK(nt_status)) {
780 printf("QueryGroupInfo level %u failed - %s\n",
781 q.in.level, nt_errstr(nt_status));
785 TEST_STRING_EQUAL(q.out.info->all.name, group->group_name);
786 TEST_INT_EQUAL(q.out.info->all.attributes, group->attributes);
787 TEST_STRING_EQUAL(q.out.info->all.description, group->description);
791 static BOOL samsync_handle_secret(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
792 int database_id, struct netr_DELTA_ENUM *delta)
794 struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
795 const char *name = delta->delta_id_union.name;
796 struct samsync_secret *new = talloc(samsync_state, struct samsync_secret);
797 struct samsync_secret *old = talloc(mem_ctx, struct samsync_secret);
798 struct lsa_QuerySecret q;
799 struct lsa_OpenSecret o;
800 struct policy_handle sec_handle;
801 struct lsa_DATA_BUF_PTR bufp1;
802 struct lsa_DATA_BUF_PTR bufp2;
806 DATA_BLOB lsa_blob1, lsa_blob_out, session_key;
809 creds_arcfour_crypt(samsync_state->creds, secret->current_cipher.cipher_data,
810 secret->current_cipher.maxlen);
812 creds_arcfour_crypt(samsync_state->creds, secret->old_cipher.cipher_data,
813 secret->old_cipher.maxlen);
815 new->name = talloc_reference(new, name);
816 new->secret = data_blob_talloc(new, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
817 new->mtime = secret->current_cipher_set_time;
819 new = talloc_reference(samsync_state, new);
820 DLIST_ADD(samsync_state->secrets, new);
822 old->name = talloc_reference(old, name);
823 old->secret = data_blob_const(secret->old_cipher.cipher_data, secret->old_cipher.maxlen);
824 old->mtime = secret->old_cipher_set_time;
826 o.in.handle = samsync_state->lsa_handle;
827 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
828 o.in.name.string = name;
829 o.out.sec_handle = &sec_handle;
831 status = dcerpc_lsa_OpenSecret(samsync_state->p_lsa, mem_ctx, &o);
832 if (!NT_STATUS_IS_OK(status)) {
833 printf("OpenSecret failed - %s\n", nt_errstr(status));
838 We would like to do this, but it is NOT_SUPPORTED on win2k3
839 TEST_SEC_DESC_EQUAL(secret->sdbuf, lsa, &sec_handle);
841 status = dcerpc_fetch_session_key(samsync_state->p_lsa, &session_key);
842 if (!NT_STATUS_IS_OK(status)) {
843 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
848 ZERO_STRUCT(new_mtime);
849 ZERO_STRUCT(old_mtime);
851 /* fetch the secret back again */
852 q.in.sec_handle = &sec_handle;
853 q.in.new_val = &bufp1;
854 q.in.new_mtime = &new_mtime;
855 q.in.old_val = &bufp2;
856 q.in.old_mtime = &old_mtime;
861 status = dcerpc_lsa_QuerySecret(samsync_state->p_lsa, mem_ctx, &q);
862 if (NT_STATUS_EQUAL(NT_STATUS_ACCESS_DENIED, status)) {
863 /* some things are just off limits */
865 } else if (!NT_STATUS_IS_OK(status)) {
866 printf("QuerySecret failed - %s\n", nt_errstr(status));
870 if (q.out.old_val->buf == NULL) {
871 /* probably just not available due to ACLs */
873 lsa_blob1.data = q.out.old_val->buf->data;
874 lsa_blob1.length = q.out.old_val->buf->length;
876 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
877 if (!NT_STATUS_IS_OK(status)) {
878 printf("Failed to decrypt secrets OLD blob: %s\n", nt_errstr(status));
882 if (!q.out.old_mtime) {
883 printf("OLD mtime not available on LSA for secret %s\n", old->name);
886 if (old->mtime != *q.out.old_mtime) {
887 printf("OLD mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
888 old->name, nt_time_string(mem_ctx, old->mtime),
889 nt_time_string(mem_ctx, *q.out.old_mtime));
893 if (old->secret.length != lsa_blob_out.length) {
894 printf("Returned secret %s doesn't match: %d != %d\n",
895 old->name, (int)old->secret.length, (int)lsa_blob_out.length);
897 } else if (memcmp(lsa_blob_out.data,
898 old->secret.data, old->secret.length) != 0) {
899 printf("Returned secret %s doesn't match: \n",
901 DEBUG(1, ("SamSync Secret:\n"));
902 dump_data(1, old->secret.data, old->secret.length);
903 DEBUG(1, ("LSA Secret:\n"));
904 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
910 if (q.out.new_val->buf == NULL) {
911 /* probably just not available due to ACLs */
913 lsa_blob1.data = q.out.new_val->buf->data;
914 lsa_blob1.length = q.out.new_val->buf->length;
916 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
917 if (!NT_STATUS_IS_OK(status)) {
918 printf("Failed to decrypt secrets OLD blob\n");
922 if (!q.out.new_mtime) {
923 printf("NEW mtime not available on LSA for secret %s\n", new->name);
926 if (new->mtime != *q.out.new_mtime) {
927 printf("NEW mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
928 new->name, nt_time_string(mem_ctx, new->mtime),
929 nt_time_string(mem_ctx, *q.out.new_mtime));
933 if (new->secret.length != lsa_blob_out.length) {
934 printf("Returned secret %s doesn't match: %d != %d\n",
935 new->name, (int)new->secret.length, (int)lsa_blob_out.length);
937 } else if (memcmp(lsa_blob_out.data,
938 new->secret.data, new->secret.length) != 0) {
939 printf("Returned secret %s doesn't match: \n",
941 DEBUG(1, ("SamSync Secret:\n"));
942 dump_data(1, new->secret.data, new->secret.length);
943 DEBUG(1, ("LSA Secret:\n"));
944 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
952 static BOOL samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
953 int database_id, struct netr_DELTA_ENUM *delta)
957 struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
958 struct dom_sid *dom_sid = delta->delta_id_union.sid;
960 struct samsync_trusted_domain *new = talloc(samsync_state, struct samsync_trusted_domain);
961 struct lsa_OpenTrustedDomain t;
962 struct policy_handle trustdom_handle;
963 struct lsa_QueryTrustedDomainInfo q;
964 union lsa_TrustedDomainInfo *info[9];
965 int levels [] = {1, 3, 8};
968 new->name = talloc_reference(new, trusted_domain->domain_name.string);
969 new->sid = talloc_reference(new, dom_sid);
971 t.in.handle = samsync_state->lsa_handle;
972 t.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
974 t.out.trustdom_handle = &trustdom_handle;
976 status = dcerpc_lsa_OpenTrustedDomain(samsync_state->p_lsa, mem_ctx, &t);
977 if (!NT_STATUS_IS_OK(status)) {
978 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
982 for (i=0; i< ARRAY_SIZE(levels); i++) {
983 q.in.trustdom_handle = &trustdom_handle;
984 q.in.level = levels[i];
985 status = dcerpc_lsa_QueryTrustedDomainInfo(samsync_state->p_lsa, mem_ctx, &q);
986 if (!NT_STATUS_IS_OK(status)) {
987 if (q.in.level == 8 && NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER)) {
988 info[levels[i]] = NULL;
991 printf("QueryInfoTrustedDomain level %d failed - %s\n",
992 levels[i], nt_errstr(status));
995 info[levels[i]] = q.out.info;
999 TEST_SID_EQUAL(info[8]->full_info.info_ex.sid, dom_sid);
1000 TEST_STRING_EQUAL(info[8]->full_info.info_ex.netbios_name, trusted_domain->domain_name);
1002 TEST_STRING_EQUAL(info[1]->name.netbios_name, trusted_domain->domain_name);
1003 TEST_INT_EQUAL(info[3]->posix_offset.posix_offset, trusted_domain->posix_offset);
1005 We would like to do this, but it is NOT_SUPPORTED on win2k3
1006 TEST_SEC_DESC_EQUAL(trusted_domain->sdbuf, lsa, &trustdom_handle);
1008 new = talloc_reference(samsync_state, new);
1009 DLIST_ADD(samsync_state->trusted_domains, new);
1014 static BOOL samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
1015 int database_id, struct netr_DELTA_ENUM *delta)
1019 struct netr_DELTA_ACCOUNT *account = delta->delta_union.account;
1020 struct dom_sid *dom_sid = delta->delta_id_union.sid;
1022 struct lsa_OpenAccount a;
1023 struct policy_handle acct_handle;
1024 struct lsa_EnumPrivsAccount e;
1025 struct lsa_LookupPrivName r;
1029 BOOL *found_priv_in_lsa;
1031 a.in.handle = samsync_state->lsa_handle;
1032 a.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1034 a.out.acct_handle = &acct_handle;
1036 status = dcerpc_lsa_OpenAccount(samsync_state->p_lsa, mem_ctx, &a);
1037 if (!NT_STATUS_IS_OK(status)) {
1038 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1042 TEST_SEC_DESC_EQUAL(account->sdbuf, lsa, &acct_handle);
1044 found_priv_in_lsa = talloc_zero_array(mem_ctx, BOOL, account->privilege_entries);
1046 e.in.handle = &acct_handle;
1048 status = dcerpc_lsa_EnumPrivsAccount(samsync_state->p_lsa, mem_ctx, &e);
1049 if (!NT_STATUS_IS_OK(status)) {
1050 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
1054 if ((account->privilege_entries && !e.out.privs)) {
1055 printf("Account %s has privileges in SamSync, but not LSA\n",
1056 dom_sid_string(mem_ctx, dom_sid));
1060 if (!account->privilege_entries && e.out.privs && e.out.privs->count) {
1061 printf("Account %s has privileges in LSA, but not SamSync\n",
1062 dom_sid_string(mem_ctx, dom_sid));
1066 TEST_INT_EQUAL(account->privilege_entries, e.out.privs->count);
1068 for (i=0;i< e.out.privs->count; i++) {
1069 r.in.handle = samsync_state->lsa_handle;
1070 r.in.luid = &e.out.privs->set[i].luid;
1072 status = dcerpc_lsa_LookupPrivName(samsync_state->p_lsa, mem_ctx, &r);
1073 if (!NT_STATUS_IS_OK(status)) {
1074 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
1079 printf("\nLookupPrivName failed to return a name\n");
1082 for (j=0;j<account->privilege_entries; j++) {
1083 if (strcmp(r.out.name->string, account->privilege_name[j].string) == 0) {
1084 found_priv_in_lsa[j] = True;
1089 for (j=0;j<account->privilege_entries; j++) {
1090 if (!found_priv_in_lsa[j]) {
1091 printf("Privilage %s on account %s not found in LSA\n", account->privilege_name[j].string,
1092 dom_sid_string(mem_ctx, dom_sid));
1100 try a netlogon DatabaseSync
1102 static BOOL test_DatabaseSync(struct samsync_state *samsync_state,
1103 TALLOC_CTX *mem_ctx)
1106 TALLOC_CTX *loop_ctx, *delta_ctx, *trustdom_ctx;
1107 struct netr_DatabaseSync r;
1108 const enum netr_SamDatabaseID database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS};
1111 struct samsync_trusted_domain *t;
1112 struct samsync_secret *s;
1114 const char *domain, *username;
1116 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1117 r.in.computername = TEST_MACHINE_NAME;
1118 r.in.preferredmaximumlength = (uint32_t)-1;
1119 ZERO_STRUCT(r.in.return_authenticator);
1121 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1122 r.in.sync_context = 0;
1123 r.in.database_id = database_ids[i];
1125 printf("Testing DatabaseSync of id %d\n", r.in.database_id);
1128 loop_ctx = talloc_named(mem_ctx, 0, "DatabaseSync loop context");
1129 creds_client_authenticator(samsync_state->creds, &r.in.credential);
1131 status = dcerpc_netr_DatabaseSync(samsync_state->p, loop_ctx, &r);
1132 if (!NT_STATUS_IS_OK(status) &&
1133 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1134 printf("DatabaseSync - %s\n", nt_errstr(status));
1139 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1140 printf("Credential chaining failed\n");
1143 r.in.sync_context = r.out.sync_context;
1145 for (d=0; d < r.out.delta_enum_array->num_deltas; d++) {
1146 delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
1147 switch (r.out.delta_enum_array->delta_enum[d].delta_type) {
1148 case NETR_DELTA_DOMAIN:
1149 if (!samsync_handle_domain(delta_ctx, samsync_state,
1150 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1151 printf("Failed to handle DELTA_DOMAIN\n");
1155 case NETR_DELTA_GROUP:
1156 if (!samsync_handle_group(delta_ctx, samsync_state,
1157 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1158 printf("Failed to handle DELTA_USER\n");
1162 case NETR_DELTA_USER:
1163 if (!samsync_handle_user(delta_ctx, samsync_state,
1164 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1165 printf("Failed to handle DELTA_USER\n");
1169 case NETR_DELTA_ALIAS:
1170 if (!samsync_handle_alias(delta_ctx, samsync_state,
1171 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1172 printf("Failed to handle DELTA_ALIAS\n");
1176 case NETR_DELTA_POLICY:
1177 if (!samsync_handle_policy(delta_ctx, samsync_state,
1178 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1179 printf("Failed to handle DELTA_POLICY\n");
1183 case NETR_DELTA_TRUSTED_DOMAIN:
1184 if (!samsync_handle_trusted_domain(delta_ctx, samsync_state,
1185 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1186 printf("Failed to handle DELTA_TRUSTED_DOMAIN\n");
1190 case NETR_DELTA_ACCOUNT:
1191 if (!samsync_handle_account(delta_ctx, samsync_state,
1192 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1193 printf("Failed to handle DELTA_ACCOUNT\n");
1197 case NETR_DELTA_SECRET:
1198 if (!samsync_handle_secret(delta_ctx, samsync_state,
1199 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1200 printf("Failed to handle DELTA_SECRET\n");
1204 case NETR_DELTA_GROUP_MEMBER:
1205 case NETR_DELTA_ALIAS_MEMBER:
1206 /* These are harder to cross-check, and we expect them */
1208 case NETR_DELTA_DELETE_GROUP:
1209 case NETR_DELTA_RENAME_GROUP:
1210 case NETR_DELTA_DELETE_USER:
1211 case NETR_DELTA_RENAME_USER:
1212 case NETR_DELTA_DELETE_ALIAS:
1213 case NETR_DELTA_RENAME_ALIAS:
1214 case NETR_DELTA_DELETE_TRUST:
1215 case NETR_DELTA_DELETE_ACCOUNT:
1216 case NETR_DELTA_DELETE_SECRET:
1217 case NETR_DELTA_DELETE_GROUP2:
1218 case NETR_DELTA_DELETE_USER2:
1219 case NETR_DELTA_MODIFY_COUNT:
1221 printf("Uxpected delta type %d\n", r.out.delta_enum_array->delta_enum[d].delta_type);
1225 talloc_free(delta_ctx);
1227 talloc_free(loop_ctx);
1228 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1232 domain = samsync_state->domain_name[SAM_DATABASE_DOMAIN];
1234 printf("Never got a DOMAIN object in samsync!\n");
1238 trustdom_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync Trusted domains context");
1240 username = talloc_asprintf(trustdom_ctx, "%s$", domain);
1241 for (t=samsync_state->trusted_domains; t; t=t->next) {
1242 char *secret_name = talloc_asprintf(trustdom_ctx, "G$$%s", t->name);
1243 for (s=samsync_state->secrets; s; s=s->next) {
1244 if (strcasecmp_m(s->name, secret_name) == 0) {
1246 struct samr_Password nt_hash;
1247 mdfour(nt_hash.hash, s->secret.data, s->secret.length);
1249 printf("Checking password for %s\\%s\n", t->name, username);
1250 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1253 TEST_WKSTA_MACHINE_NAME,
1257 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS)) {
1258 printf("Verifiction of trust password to %s failed: %s (the trusted domain is not available)\n",
1259 t->name, nt_errstr(nt_status));
1263 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1264 printf("Verifiction of trust password to %s: should have failed (nologon interdomain trust account), instead: %s\n",
1265 t->name, nt_errstr(nt_status));
1271 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1274 TEST_WKSTA_MACHINE_NAME,
1279 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
1280 printf("Verifiction of trust password to %s: should have failed (wrong password), instead: %s\n",
1281 t->name, nt_errstr(nt_status));
1289 talloc_free(trustdom_ctx);
1295 try a netlogon DatabaseDeltas
1297 static BOOL test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX *mem_ctx)
1300 TALLOC_CTX *loop_ctx;
1301 struct netr_DatabaseDeltas r;
1302 const uint32_t database_ids[] = {0, 1, 2};
1306 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1307 r.in.computername = TEST_MACHINE_NAME;
1308 r.in.preferredmaximumlength = (uint32_t)-1;
1309 ZERO_STRUCT(r.in.return_authenticator);
1311 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1312 r.in.database_id = database_ids[i];
1313 r.in.sequence_num = samsync_state->seq_num[i];
1315 if (r.in.sequence_num == 0) continue;
1317 /* this shows that the bdc doesn't need to do a single call for
1318 * each seqnumber, and the pdc doesn't need to know about old values
1321 r.in.sequence_num -= 10;
1324 printf("Testing DatabaseDeltas of id %d at %llu\n",
1325 r.in.database_id, (long long)r.in.sequence_num);
1328 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseDeltas loop context");
1329 creds_client_authenticator(samsync_state->creds, &r.in.credential);
1331 status = dcerpc_netr_DatabaseDeltas(samsync_state->p, loop_ctx, &r);
1332 if (!NT_STATUS_IS_OK(status) &&
1333 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) &&
1334 !NT_STATUS_EQUAL(status, NT_STATUS_SYNCHRONIZATION_REQUIRED)) {
1335 printf("DatabaseDeltas - %s\n", nt_errstr(status));
1339 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1340 printf("Credential chaining failed\n");
1343 r.in.sequence_num++;
1344 talloc_free(loop_ctx);
1345 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1353 try a netlogon DatabaseSync2
1355 static BOOL test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1356 struct creds_CredentialState *creds)
1359 TALLOC_CTX *loop_ctx;
1360 struct netr_DatabaseSync2 r;
1361 const uint32_t database_ids[] = {0, 1, 2};
1365 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1366 r.in.computername = TEST_MACHINE_NAME;
1367 r.in.preferredmaximumlength = (uint32_t)-1;
1368 ZERO_STRUCT(r.in.return_authenticator);
1370 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1371 r.in.sync_context = 0;
1372 r.in.database_id = database_ids[i];
1373 r.in.restart_state = 0;
1375 printf("Testing DatabaseSync2 of id %d\n", r.in.database_id);
1378 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync2 loop context");
1379 creds_client_authenticator(creds, &r.in.credential);
1381 status = dcerpc_netr_DatabaseSync2(p, loop_ctx, &r);
1382 if (!NT_STATUS_IS_OK(status) &&
1383 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1384 printf("DatabaseSync2 - %s\n", nt_errstr(status));
1388 if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
1389 printf("Credential chaining failed\n");
1392 r.in.sync_context = r.out.sync_context;
1393 talloc_free(loop_ctx);
1394 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1402 BOOL torture_rpc_samsync(struct torture_context *torture)
1405 TALLOC_CTX *mem_ctx;
1407 struct test_join *join_ctx;
1408 struct test_join *join_ctx2;
1409 struct test_join *user_ctx;
1410 const char *machine_password;
1411 const char *wksta_machine_password;
1412 struct dcerpc_binding *b;
1413 struct dcerpc_binding *b_netlogon_wksta;
1414 struct samr_Connect c;
1415 struct samr_SetDomainInfo s;
1416 struct policy_handle *domain_policy;
1418 struct lsa_ObjectAttribute attr;
1419 struct lsa_QosInfo qos;
1420 struct lsa_OpenPolicy2 r;
1421 struct cli_credentials *credentials;
1422 struct cli_credentials *credentials_wksta;
1424 struct samsync_state *samsync_state;
1426 char *test_machine_account;
1428 char *test_wksta_machine_account;
1430 mem_ctx = talloc_init("torture_rpc_netlogon");
1432 test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1433 join_ctx = torture_create_testuser(torture, test_machine_account, lp_workgroup(), ACB_SVRTRUST,
1436 talloc_free(mem_ctx);
1437 printf("Failed to join as BDC\n");
1441 test_wksta_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_WKSTA_MACHINE_NAME);
1442 join_ctx2 = torture_create_testuser(torture, test_wksta_machine_account, lp_workgroup(), ACB_WSTRUST,
1443 &wksta_machine_password);
1445 talloc_free(mem_ctx);
1446 printf("Failed to join as member\n");
1450 user_ctx = torture_create_testuser(torture, TEST_USER_NAME,
1454 talloc_free(mem_ctx);
1455 printf("Failed to create test account\n");
1459 samsync_state = talloc_zero(mem_ctx, struct samsync_state);
1461 samsync_state->p_samr = torture_join_samr_pipe(join_ctx);
1462 samsync_state->connect_handle = talloc_zero(samsync_state, struct policy_handle);
1463 samsync_state->lsa_handle = talloc_zero(samsync_state, struct policy_handle);
1464 c.in.system_name = NULL;
1465 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1466 c.out.connect_handle = samsync_state->connect_handle;
1468 status = dcerpc_samr_Connect(samsync_state->p_samr, mem_ctx, &c);
1469 if (!NT_STATUS_IS_OK(status)) {
1470 printf("samr_Connect failed\n");
1475 domain_policy = samsync_open_domain(mem_ctx, samsync_state, lp_workgroup(), NULL);
1476 if (!domain_policy) {
1477 printf("samrsync_open_domain failed\n");
1482 s.in.domain_handle = domain_policy;
1484 s.in.info = talloc(mem_ctx, union samr_DomainInfo);
1486 s.in.info->info4.comment.string
1487 = talloc_asprintf(mem_ctx,
1488 "Tortured by Samba4: %s",
1489 timestring(mem_ctx, time(NULL)));
1490 status = dcerpc_samr_SetDomainInfo(samsync_state->p_samr, mem_ctx, &s);
1492 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, domain_policy)) {
1497 if (!NT_STATUS_IS_OK(status)) {
1498 printf("SetDomainInfo level %u failed - %s\n",
1499 s.in.level, nt_errstr(status));
1505 status = torture_rpc_connection(torture,
1506 &samsync_state->p_lsa,
1509 if (!NT_STATUS_IS_OK(status)) {
1515 qos.impersonation_level = 2;
1516 qos.context_mode = 1;
1517 qos.effective_only = 0;
1520 attr.root_dir = NULL;
1521 attr.object_name = NULL;
1522 attr.attributes = 0;
1523 attr.sec_desc = NULL;
1524 attr.sec_qos = &qos;
1526 r.in.system_name = "\\";
1528 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1529 r.out.handle = samsync_state->lsa_handle;
1531 status = dcerpc_lsa_OpenPolicy2(samsync_state->p_lsa, mem_ctx, &r);
1532 if (!NT_STATUS_IS_OK(status)) {
1533 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1538 status = torture_rpc_binding(torture, &b);
1539 if (!NT_STATUS_IS_OK(status)) {
1544 b->flags &= ~DCERPC_AUTH_OPTIONS;
1545 b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1547 credentials = cli_credentials_init(mem_ctx);
1549 cli_credentials_set_workstation(credentials, TEST_MACHINE_NAME, CRED_SPECIFIED);
1550 cli_credentials_set_domain(credentials, lp_workgroup(), CRED_SPECIFIED);
1551 cli_credentials_set_username(credentials, test_machine_account, CRED_SPECIFIED);
1552 cli_credentials_set_password(credentials, machine_password, CRED_SPECIFIED);
1553 cli_credentials_set_secure_channel_type(credentials,
1556 status = dcerpc_pipe_connect_b(samsync_state,
1557 &samsync_state->p, b,
1558 &ndr_table_netlogon,
1561 if (!NT_STATUS_IS_OK(status)) {
1562 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
1567 status = dcerpc_schannel_creds(samsync_state->p->conn->security_state.generic_state,
1568 samsync_state, &samsync_state->creds);
1569 if (!NT_STATUS_IS_OK(status)) {
1575 status = torture_rpc_binding(torture, &b_netlogon_wksta);
1576 if (!NT_STATUS_IS_OK(status)) {
1581 b_netlogon_wksta->flags &= ~DCERPC_AUTH_OPTIONS;
1582 b_netlogon_wksta->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1584 credentials_wksta = cli_credentials_init(mem_ctx);
1586 cli_credentials_set_workstation(credentials_wksta, TEST_WKSTA_MACHINE_NAME, CRED_SPECIFIED);
1587 cli_credentials_set_domain(credentials_wksta, lp_workgroup(), CRED_SPECIFIED);
1588 cli_credentials_set_username(credentials_wksta, test_wksta_machine_account, CRED_SPECIFIED);
1589 cli_credentials_set_password(credentials_wksta, wksta_machine_password, CRED_SPECIFIED);
1590 cli_credentials_set_secure_channel_type(credentials_wksta,
1593 status = dcerpc_pipe_connect_b(samsync_state,
1594 &samsync_state->p_netlogon_wksta,
1596 &ndr_table_netlogon,
1597 credentials_wksta, NULL);
1599 if (!NT_STATUS_IS_OK(status)) {
1600 printf("Failed to connect to server as a Workstation: %s\n", nt_errstr(status));
1605 status = dcerpc_schannel_creds(samsync_state->p_netlogon_wksta->conn->security_state.generic_state,
1606 samsync_state, &samsync_state->creds_netlogon_wksta);
1607 if (!NT_STATUS_IS_OK(status)) {
1608 printf("Failed to obtail schanel creds!\n");
1612 if (!test_DatabaseSync(samsync_state, mem_ctx)) {
1613 printf("DatabaseSync failed\n");
1617 if (!test_DatabaseDeltas(samsync_state, mem_ctx)) {
1618 printf("DatabaseDeltas failed\n");
1622 if (!test_DatabaseSync2(samsync_state->p, mem_ctx, samsync_state->creds)) {
1623 printf("DatabaseSync2 failed\n");
1628 torture_leave_domain(join_ctx);
1629 torture_leave_domain(join_ctx2);
1630 torture_leave_domain(user_ctx);
1632 talloc_free(mem_ctx);