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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "librpc/gen_ndr/ndr_netlogon.h"
27 #include "auth/auth.h"
28 #include "dlinklist.h"
29 #include "lib/crypto/crypto.h"
30 #include "system/time.h"
32 #define TEST_MACHINE_NAME "samsynctest"
33 #define TEST_WKSTA_MACHINE_NAME "samsynctest2"
34 #define TEST_USER_NAME "samsynctestuser"
37 try a netlogon SamLogon
39 static NTSTATUS test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
40 struct creds_CredentialState *creds,
41 const char *domain, const char *account_name,
42 const char *workstation,
43 struct samr_Password *lm_hash,
44 struct samr_Password *nt_hash,
45 struct netr_SamInfo3 **info3)
48 struct netr_LogonSamLogon r;
49 struct netr_Authenticator auth, auth2;
50 struct netr_NetworkInfo ninfo;
52 ninfo.identity_info.domain_name.string = domain;
53 ninfo.identity_info.parameter_control = 0;
54 ninfo.identity_info.logon_id_low = 0;
55 ninfo.identity_info.logon_id_high = 0;
56 ninfo.identity_info.account_name.string = account_name;
57 ninfo.identity_info.workstation.string = workstation;
58 generate_random_buffer(ninfo.challenge,
59 sizeof(ninfo.challenge));
62 ninfo.nt.data = talloc_size(mem_ctx, 24);
63 SMBOWFencrypt(nt_hash->hash, ninfo.challenge, ninfo.nt.data);
71 ninfo.lm.data = talloc_size(mem_ctx, 24);
72 SMBOWFencrypt(lm_hash->hash, ninfo.challenge, ninfo.lm.data);
78 r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
79 r.in.workstation = workstation;
80 r.in.credential = &auth;
81 r.in.return_authenticator = &auth2;
83 r.in.logon.network = &ninfo;
86 creds_client_authenticator(creds, &auth);
88 r.in.validation_level = 3;
90 status = dcerpc_netr_LogonSamLogon(p, mem_ctx, &r);
92 if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
93 printf("Credential chaining failed\n");
97 *info3 = r.out.validation.sam3;
103 struct samsync_state {
104 /* we remember the sequence numbers so we can easily do a DatabaseDelta */
106 char *domain_name[2];
107 struct samsync_secret *secrets;
108 struct samsync_trusted_domain *trusted_domains;
109 struct creds_CredentialState *creds;
110 struct creds_CredentialState *creds_netlogon_wksta;
111 struct policy_handle *connect_handle;
112 struct policy_handle *domain_handle[2];
113 struct dom_sid *sid[2];
114 struct dcerpc_pipe *p;
115 struct dcerpc_pipe *p_netlogon_wksta;
116 struct dcerpc_pipe *p_samr;
117 struct dcerpc_pipe *p_lsa;
118 struct policy_handle *lsa_handle;
121 struct samsync_secret {
122 struct samsync_secret *prev, *next;
128 struct samsync_trusted_domain {
129 struct samsync_trusted_domain *prev, *next;
134 static struct policy_handle *samsync_open_domain(TALLOC_CTX *mem_ctx,
135 struct samsync_state *samsync_state,
137 struct dom_sid **sid)
139 struct samr_String name;
140 struct samr_OpenDomain o;
141 struct samr_LookupDomain l;
142 struct policy_handle *domain_handle = talloc(mem_ctx, struct policy_handle);
145 name.string = domain;
146 l.in.connect_handle = samsync_state->connect_handle;
147 l.in.domain_name = &name;
149 nt_status = dcerpc_samr_LookupDomain(samsync_state->p_samr, mem_ctx, &l);
150 if (!NT_STATUS_IS_OK(nt_status)) {
151 printf("LookupDomain failed - %s\n", nt_errstr(nt_status));
155 o.in.connect_handle = samsync_state->connect_handle;
156 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
157 o.in.sid = l.out.sid;
158 o.out.domain_handle = domain_handle;
164 nt_status = dcerpc_samr_OpenDomain(samsync_state->p_samr, mem_ctx, &o);
165 if (!NT_STATUS_IS_OK(nt_status)) {
166 printf("OpenDomain failed - %s\n", nt_errstr(nt_status));
170 return domain_handle;
173 static struct sec_desc_buf *samsync_query_samr_sec_desc(TALLOC_CTX *mem_ctx,
174 struct samsync_state *samsync_state,
175 struct policy_handle *handle)
177 struct samr_QuerySecurity r;
180 r.in.handle = handle;
183 status = dcerpc_samr_QuerySecurity(samsync_state->p_samr, mem_ctx, &r);
184 if (!NT_STATUS_IS_OK(status)) {
185 printf("SAMR QuerySecurity failed - %s\n", nt_errstr(status));
192 static struct sec_desc_buf *samsync_query_lsa_sec_desc(TALLOC_CTX *mem_ctx,
193 struct samsync_state *samsync_state,
194 struct policy_handle *handle)
196 struct lsa_QuerySecurity r;
199 r.in.handle = handle;
202 status = dcerpc_lsa_QuerySecurity(samsync_state->p_lsa, mem_ctx, &r);
203 if (!NT_STATUS_IS_OK(status)) {
204 printf("LSA QuerySecurity failed - %s\n", nt_errstr(status));
211 #define TEST_UINT64_EQUAL(i1, i2) do {\
213 printf("%s: uint64 mismatch: " #i1 ": 0x%08x%08x (%lld) != " #i2 ": 0x%08x%08x (%lld)\n", \
215 (uint32_t)(i1 >> 32), (uint32_t)(i1 & 0xFFFFFFFF), i1, \
216 (uint32_t)(i2 >> 32), (uint32_t)(i2 & 0xFFFFFFFF), i2);\
220 #define TEST_INT_EQUAL(i1, i2) do {\
222 printf("%s: integer mismatch: " #i1 ":%d != " #i2 ": %d\n", \
223 __location__, i1, i2);\
227 #define TEST_TIME_EQUAL(t1, t2) do {\
229 printf("%s: NTTIME mismatch: " #t1 ":%s != " #t2 ": %s\n", \
230 __location__, nt_time_string(mem_ctx, t1), nt_time_string(mem_ctx, t2));\
235 #define TEST_STRING_EQUAL(s1, s2) do {\
236 if (!((!s1.string || s1.string[0]=='\0') && (!s2.string || s2.string[0]=='\0')) \
237 && strcmp_safe(s1.string, s2.string) != 0) {\
238 printf("%s: string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
239 __location__, s1.string, s2.string);\
244 #define TEST_SID_EQUAL(s1, s2) do {\
245 if (!dom_sid_equal(s1, s2)) {\
246 printf("%s: dom_sid mismatch: " #s1 ":%s != " #s2 ": %s\n", \
247 __location__, dom_sid_string(mem_ctx, s1), dom_sid_string(mem_ctx, s2));\
252 /* The ~SEC_DESC_SACL_PRESENT is because we don't, as administrator,
253 * get back the SACL part of the SD when we ask over SAMR */
255 #define TEST_SEC_DESC_EQUAL(sd1, pipe, handle) do {\
256 struct sec_desc_buf *sdbuf = samsync_query_ ##pipe## _sec_desc(mem_ctx, samsync_state, \
258 if (!sdbuf || !sdbuf->sd) { \
259 printf("Could not obtain security descriptor to match " #sd1 "\n");\
262 if (!security_descriptor_mask_equal(sd1.sd, sdbuf->sd, \
263 ~SEC_DESC_SACL_PRESENT)) {\
264 printf("Security Descriptor Mismatch for %s:\n", #sd1);\
265 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamSync", sd1.sd);\
266 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "SamR", sdbuf->sd);\
272 static BOOL samsync_handle_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
273 int database_id, struct netr_DELTA_ENUM *delta)
275 struct netr_DELTA_DOMAIN *domain = delta->delta_union.domain;
276 struct dom_sid *dom_sid;
277 struct samr_QueryDomainInfo q[14]; /* q[0] will be unused simple for clarity */
278 uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
283 samsync_state->seq_num[database_id] =
284 domain->sequence_num;
285 switch (database_id) {
286 case SAM_DATABASE_DOMAIN:
288 case SAM_DATABASE_BUILTIN:
289 if (StrCaseCmp("BUILTIN", domain->domain_name.string) != 0) {
290 printf("BUILTIN domain has different name: %s\n", domain->domain_name.string);
293 case SAM_DATABASE_PRIVS:
294 printf("DOMAIN entry on privs DB!\n");
299 if (!samsync_state->domain_name[database_id]) {
300 samsync_state->domain_name[database_id] =
301 talloc_reference(samsync_state, domain->domain_name.string);
303 if (StrCaseCmp(samsync_state->domain_name[database_id], domain->domain_name.string) != 0) {
304 printf("Domain has name varies!: %s != %s\n", samsync_state->domain_name[database_id],
305 domain->domain_name.string);
310 if (!samsync_state->domain_handle[database_id]) {
311 samsync_state->domain_handle[database_id]
312 = talloc_reference(samsync_state,
313 samsync_open_domain(mem_ctx, samsync_state, samsync_state->domain_name[database_id],
316 if (samsync_state->domain_handle[database_id]) {
317 samsync_state->sid[database_id] = talloc_reference(samsync_state, dom_sid);
318 talloc_reference(dom_sid, dom_sid->sub_auths);
321 printf("\tsequence_nums[%d/%s]=%llu\n",
322 database_id, domain->domain_name.string,
323 samsync_state->seq_num[database_id]);
325 for (i=0;i<ARRAY_SIZE(levels);i++) {
326 q[levels[i]].in.domain_handle = samsync_state->domain_handle[database_id];
327 q[levels[i]].in.level = levels[i];
329 nt_status = dcerpc_samr_QueryDomainInfo(samsync_state->p_samr, mem_ctx, &q[levels[i]]);
331 if (!NT_STATUS_IS_OK(nt_status)) {
332 printf("QueryDomainInfo level %u failed - %s\n",
333 q[levels[i]].in.level, nt_errstr(nt_status));
338 TEST_STRING_EQUAL(q[5].out.info->info5.domain_name, domain->domain_name);
340 TEST_STRING_EQUAL(q[2].out.info->info2.comment, domain->comment);
341 TEST_STRING_EQUAL(q[4].out.info->info4.comment, domain->comment);
342 TEST_TIME_EQUAL(q[2].out.info->info2.force_logoff_time, domain->force_logoff_time);
343 TEST_TIME_EQUAL(q[3].out.info->info3.force_logoff_time, domain->force_logoff_time);
345 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_length, domain->min_password_length);
346 TEST_TIME_EQUAL(q[1].out.info->info1.password_history_length, domain->password_history_length);
347 TEST_TIME_EQUAL(q[1].out.info->info1.max_password_age, domain->max_password_age);
348 TEST_TIME_EQUAL(q[1].out.info->info1.min_password_age, domain->min_password_age);
350 TEST_UINT64_EQUAL(q[8].out.info->info8.sequence_num,
351 domain->sequence_num);
352 TEST_TIME_EQUAL(q[8].out.info->info8.domain_create_time,
353 domain->domain_create_time);
354 TEST_TIME_EQUAL(q[13].out.info->info13.domain_create_time,
355 domain->domain_create_time);
357 TEST_SEC_DESC_EQUAL(domain->sdbuf, samr, samsync_state->domain_handle[database_id]);
362 static BOOL samsync_handle_policy(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
363 int database_id, struct netr_DELTA_ENUM *delta)
365 struct netr_DELTA_POLICY *policy = delta->delta_union.policy;
367 samsync_state->seq_num[database_id] =
368 policy->sequence_num;
370 if (!samsync_state->domain_name[SAM_DATABASE_DOMAIN]) {
371 samsync_state->domain_name[SAM_DATABASE_DOMAIN] =
372 talloc_reference(samsync_state, policy->primary_domain_name.string);
374 if (StrCaseCmp(samsync_state->domain_name[SAM_DATABASE_DOMAIN], policy->primary_domain_name.string) != 0) {
375 printf("PRIMARY domain has name varies between DOMAIN and POLICY!: %s != %s\n", samsync_state->domain_name[SAM_DATABASE_DOMAIN],
376 policy->primary_domain_name.string);
381 if (!dom_sid_equal(samsync_state->sid[SAM_DATABASE_DOMAIN], policy->sid)) {
382 printf("Domain SID from POLICY (%s) does not match domain sid from SAMR (%s)\n",
383 dom_sid_string(mem_ctx, policy->sid), dom_sid_string(mem_ctx, samsync_state->sid[SAM_DATABASE_DOMAIN]));
387 printf("\tsequence_nums[%d/PRIVS]=%llu\n",
389 samsync_state->seq_num[database_id]);
393 static BOOL samsync_handle_user(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
394 int database_id, struct netr_DELTA_ENUM *delta)
396 uint32_t rid = delta->delta_id_union.rid;
397 struct netr_DELTA_USER *user = delta->delta_union.user;
398 struct netr_SamInfo3 *info3;
399 struct samr_Password lm_hash;
400 struct samr_Password nt_hash;
401 struct samr_Password *lm_hash_p = NULL;
402 struct samr_Password *nt_hash_p = NULL;
403 const char *domain = samsync_state->domain_name[database_id];
404 const char *username = user->account_name.string;
408 struct samr_OpenUser r;
409 struct samr_QueryUserInfo q;
410 struct policy_handle user_handle;
412 struct samr_GetGroupsForUser getgroups;
413 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
414 printf("SamSync needs domain information before the users\n");
418 r.in.domain_handle = samsync_state->domain_handle[database_id];
419 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
421 r.out.user_handle = &user_handle;
423 nt_status = dcerpc_samr_OpenUser(samsync_state->p_samr, mem_ctx, &r);
424 if (!NT_STATUS_IS_OK(nt_status)) {
425 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
429 q.in.user_handle = &user_handle;
432 TEST_SEC_DESC_EQUAL(user->sdbuf, samr, &user_handle);
434 nt_status = dcerpc_samr_QueryUserInfo(samsync_state->p_samr, mem_ctx, &q);
435 if (!NT_STATUS_IS_OK(nt_status)) {
436 printf("QueryUserInfo level %u failed - %s\n",
437 q.in.level, nt_errstr(nt_status));
441 getgroups.in.user_handle = &user_handle;
443 nt_status = dcerpc_samr_GetGroupsForUser(samsync_state->p_samr, mem_ctx, &getgroups);
444 if (!NT_STATUS_IS_OK(nt_status)) {
445 printf("GetGroupsForUser failed - %s\n",
446 nt_errstr(nt_status));
450 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &user_handle)) {
451 printf("samr_handle_Close failed - %s\n",
452 nt_errstr(nt_status));
459 if (!NT_STATUS_IS_OK(nt_status)) {
460 printf("QueryUserInfo level %u failed - %s\n",
461 q.in.level, nt_errstr(nt_status));
465 TEST_STRING_EQUAL(q.out.info->info21.account_name, user->account_name);
466 TEST_STRING_EQUAL(q.out.info->info21.full_name, user->full_name);
467 TEST_INT_EQUAL(q.out.info->info21.rid, user->rid);
468 TEST_INT_EQUAL(q.out.info->info21.primary_gid, user->primary_gid);
469 TEST_STRING_EQUAL(q.out.info->info21.home_directory, user->home_directory);
470 TEST_STRING_EQUAL(q.out.info->info21.home_drive, user->home_drive);
471 TEST_STRING_EQUAL(q.out.info->info21.logon_script, user->logon_script);
472 TEST_STRING_EQUAL(q.out.info->info21.description, user->description);
473 TEST_STRING_EQUAL(q.out.info->info21.workstations, user->workstations);
475 TEST_TIME_EQUAL(q.out.info->info21.last_logon, user->last_logon);
476 TEST_TIME_EQUAL(q.out.info->info21.last_logoff, user->last_logoff);
479 TEST_INT_EQUAL(q.out.info->info21.logon_hours.units_per_week,
480 user->logon_hours.units_per_week);
482 if (memcmp(q.out.info->info21.logon_hours.bits, user->logon_hours.bits,
483 q.out.info->info21.logon_hours.units_per_week/8) != 0) {
484 printf("Logon hours mismatch\n");
489 TEST_INT_EQUAL(q.out.info->info21.bad_password_count,
490 user->bad_password_count);
491 TEST_INT_EQUAL(q.out.info->info21.logon_count,
494 TEST_TIME_EQUAL(q.out.info->info21.last_password_change,
495 user->last_password_change);
496 TEST_TIME_EQUAL(q.out.info->info21.acct_expiry,
499 TEST_INT_EQUAL(q.out.info->info21.acct_flags, user->acct_flags);
500 TEST_INT_EQUAL(q.out.info->info21.nt_password_set, user->nt_password_present);
501 TEST_INT_EQUAL(q.out.info->info21.lm_password_set, user->lm_password_present);
502 TEST_INT_EQUAL(q.out.info->info21.password_expired, user->password_expired);
504 TEST_STRING_EQUAL(q.out.info->info21.comment, user->comment);
505 TEST_STRING_EQUAL(q.out.info->info21.parameters, user->parameters);
507 TEST_INT_EQUAL(q.out.info->info21.country_code, user->country_code);
508 TEST_INT_EQUAL(q.out.info->info21.code_page, user->code_page);
510 TEST_STRING_EQUAL(q.out.info->info21.profile_path, user->profile_path);
512 if (user->lm_password_present) {
513 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
514 lm_hash_p = &lm_hash;
516 if (user->nt_password_present) {
517 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
518 nt_hash_p = &nt_hash;
521 if (user->user_private_info.SensitiveData) {
523 struct netr_USER_KEYS keys;
524 data.data = user->user_private_info.SensitiveData;
525 data.length = user->user_private_info.DataLength;
526 creds_arcfour_crypt(samsync_state->creds, data.data, data.length);
527 nt_status = ndr_pull_struct_blob(&data, mem_ctx, &keys, (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
528 if (NT_STATUS_IS_OK(nt_status)) {
529 if (keys.keys.keys2.lmpassword.length == 16) {
530 sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
531 lm_hash_p = &lm_hash;
533 if (keys.keys.keys2.ntpassword.length == 16) {
534 sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
535 nt_hash_p = &nt_hash;
538 printf("Failed to parse Sensitive Data for %s:\n", username);
540 dump_data(0, data.data, data.length);
546 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, mem_ctx, samsync_state->creds_netlogon_wksta,
549 TEST_WKSTA_MACHINE_NAME,
554 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED)) {
555 if (user->acct_flags & ACB_DISABLED) {
558 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT)) {
559 if (user->acct_flags & ACB_WSTRUST) {
562 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT)) {
563 if (user->acct_flags & ACB_SVRTRUST) {
566 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
567 if (user->acct_flags & ACB_DOMTRUST) {
570 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
571 if (user->acct_flags & ACB_DOMTRUST) {
574 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT)) {
575 if (user->acct_flags & ACB_AUTOLOCK) {
578 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
579 if (!lm_hash_p && !nt_hash_p) {
582 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE)) {
583 /* We would need to know the server's current time to test this properly */
585 } else if (NT_STATUS_IS_OK(nt_status)) {
586 TEST_INT_EQUAL(user->rid, info3->base.rid);
587 TEST_INT_EQUAL(user->primary_gid, info3->base.primary_gid);
588 /* this is 0x0 from NT4 sp6 */
589 if (info3->base.acct_flags) {
590 TEST_INT_EQUAL(user->acct_flags, info3->base.acct_flags);
592 /* this is NULL from NT4 sp6 */
593 if (info3->base.account_name.string) {
594 TEST_STRING_EQUAL(user->account_name, info3->base.account_name);
596 /* this is NULL from Win2k3 */
597 if (info3->base.full_name.string) {
598 TEST_STRING_EQUAL(user->full_name, info3->base.full_name);
600 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
601 TEST_STRING_EQUAL(user->profile_path, info3->base.profile_path);
602 TEST_STRING_EQUAL(user->home_directory, info3->base.home_directory);
603 TEST_STRING_EQUAL(user->home_drive, info3->base.home_drive);
604 TEST_STRING_EQUAL(user->logon_script, info3->base.logon_script);
607 TEST_TIME_EQUAL(user->last_logon, info3->base.last_logon);
608 TEST_TIME_EQUAL(user->acct_expiry, info3->base.acct_expiry);
609 TEST_TIME_EQUAL(user->last_password_change, info3->base.last_password_change);
611 /* Does the concept of a logoff time ever really
612 * exist? (not in any sensible way, according to the
613 * doco I read -- abartlet) */
615 /* This copes with the two different versions of 0 I see */
616 /* with NT4 sp6 we have the || case */
617 if (!((user->last_logoff == 0)
618 || (info3->base.last_logoff == 0x7fffffffffffffffLL))) {
619 TEST_TIME_EQUAL(user->last_logoff, info3->base.last_logoff);
622 TEST_INT_EQUAL(getgroups.out.rids->count, info3->base.groups.count);
623 if (getgroups.out.rids->count == info3->base.groups.count) {
625 int count = getgroups.out.rids->count;
626 BOOL *matched = talloc_zero_array(mem_ctx, BOOL, getgroups.out.rids->count);
628 for (i = 0; i < count; i++) {
629 for (j = 0; j < count; j++) {
630 if ((getgroups.out.rids->rids[i].rid ==
631 info3->base.groups.rids[j].rid)
632 && (getgroups.out.rids->rids[i].type ==
633 info3->base.groups.rids[j].type)) {
639 for (i = 0; i < getgroups.out.rids->count; i++) {
640 if (matched[i] == False) {
642 printf("Could not find group RID %u found in getgroups in NETLOGON reply\n",
643 getgroups.out.rids->rids[i].rid);
649 printf("Could not validate password for user %s\\%s: %s\n",
650 domain, username, nt_errstr(nt_status));
656 static BOOL samsync_handle_alias(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
657 int database_id, struct netr_DELTA_ENUM *delta)
659 uint32_t rid = delta->delta_id_union.rid;
660 struct netr_DELTA_ALIAS *alias = delta->delta_union.alias;
664 struct samr_OpenAlias r;
665 struct samr_QueryAliasInfo q;
666 struct policy_handle alias_handle;
668 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
669 printf("SamSync needs domain information before the users\n");
673 r.in.domain_handle = samsync_state->domain_handle[database_id];
674 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
676 r.out.alias_handle = &alias_handle;
678 nt_status = dcerpc_samr_OpenAlias(samsync_state->p_samr, mem_ctx, &r);
679 if (!NT_STATUS_IS_OK(nt_status)) {
680 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
684 q.in.alias_handle = &alias_handle;
687 TEST_SEC_DESC_EQUAL(alias->sdbuf, samr, &alias_handle);
689 nt_status = dcerpc_samr_QueryAliasInfo(samsync_state->p_samr, mem_ctx, &q);
690 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &alias_handle)) {
694 if (!NT_STATUS_IS_OK(nt_status)) {
695 printf("QueryAliasInfo level %u failed - %s\n",
696 q.in.level, nt_errstr(nt_status));
700 TEST_STRING_EQUAL(q.out.info->all.name, alias->alias_name);
701 TEST_STRING_EQUAL(q.out.info->all.description, alias->description);
705 static BOOL samsync_handle_group(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
706 int database_id, struct netr_DELTA_ENUM *delta)
708 uint32_t rid = delta->delta_id_union.rid;
709 struct netr_DELTA_GROUP *group = delta->delta_union.group;
713 struct samr_OpenGroup r;
714 struct samr_QueryGroupInfo q;
715 struct policy_handle group_handle;
717 if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
718 printf("SamSync needs domain information before the users\n");
722 r.in.domain_handle = samsync_state->domain_handle[database_id];
723 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
725 r.out.group_handle = &group_handle;
727 nt_status = dcerpc_samr_OpenGroup(samsync_state->p_samr, mem_ctx, &r);
728 if (!NT_STATUS_IS_OK(nt_status)) {
729 printf("OpenUser(%u) failed - %s\n", rid, nt_errstr(nt_status));
733 q.in.group_handle = &group_handle;
736 TEST_SEC_DESC_EQUAL(group->sdbuf, samr, &group_handle);
738 nt_status = dcerpc_samr_QueryGroupInfo(samsync_state->p_samr, mem_ctx, &q);
739 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, &group_handle)) {
743 if (!NT_STATUS_IS_OK(nt_status)) {
744 printf("QueryGroupInfo level %u failed - %s\n",
745 q.in.level, nt_errstr(nt_status));
749 TEST_STRING_EQUAL(q.out.info->all.name, group->group_name);
750 TEST_INT_EQUAL(q.out.info->all.attributes, group->attributes);
751 TEST_STRING_EQUAL(q.out.info->all.description, group->description);
755 static BOOL samsync_handle_secret(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
756 int database_id, struct netr_DELTA_ENUM *delta)
758 struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
759 const char *name = delta->delta_id_union.name;
760 struct samsync_secret *new = talloc(samsync_state, struct samsync_secret);
761 struct samsync_secret *old = talloc(mem_ctx, struct samsync_secret);
762 struct lsa_QuerySecret q;
763 struct lsa_OpenSecret o;
764 struct policy_handle sec_handle;
765 struct lsa_DATA_BUF_PTR bufp1;
766 struct lsa_DATA_BUF_PTR bufp2;
770 DATA_BLOB lsa_blob1, lsa_blob_out, session_key;
773 creds_arcfour_crypt(samsync_state->creds, secret->current_cipher.cipher_data,
774 secret->current_cipher.maxlen);
776 creds_arcfour_crypt(samsync_state->creds, secret->old_cipher.cipher_data,
777 secret->old_cipher.maxlen);
779 new->name = talloc_reference(new, name);
780 new->secret = data_blob_talloc(new, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
781 new->mtime = secret->current_cipher_set_time;
783 new = talloc_reference(samsync_state, new);
784 DLIST_ADD(samsync_state->secrets, new);
786 old->name = talloc_reference(old, name);
787 old->secret = data_blob_const(secret->old_cipher.cipher_data, secret->old_cipher.maxlen);
788 old->mtime = secret->old_cipher_set_time;
790 o.in.handle = samsync_state->lsa_handle;
791 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
792 o.in.name.string = name;
793 o.out.sec_handle = &sec_handle;
795 status = dcerpc_lsa_OpenSecret(samsync_state->p_lsa, mem_ctx, &o);
796 if (!NT_STATUS_IS_OK(status)) {
797 printf("OpenSecret failed - %s\n", nt_errstr(status));
802 We would like to do this, but it is NOT_SUPPORTED on win2k3
803 TEST_SEC_DESC_EQUAL(secret->sdbuf, lsa, &sec_handle);
805 status = dcerpc_fetch_session_key(samsync_state->p_lsa, &session_key);
806 if (!NT_STATUS_IS_OK(status)) {
807 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
812 ZERO_STRUCT(new_mtime);
813 ZERO_STRUCT(old_mtime);
815 /* fetch the secret back again */
816 q.in.sec_handle = &sec_handle;
817 q.in.new_val = &bufp1;
818 q.in.new_mtime = &new_mtime;
819 q.in.old_val = &bufp2;
820 q.in.old_mtime = &old_mtime;
825 status = dcerpc_lsa_QuerySecret(samsync_state->p_lsa, mem_ctx, &q);
826 if (NT_STATUS_EQUAL(NT_STATUS_ACCESS_DENIED, status)) {
827 /* some things are just off limits */
829 } else if (!NT_STATUS_IS_OK(status)) {
830 printf("QuerySecret failed - %s\n", nt_errstr(status));
834 if (q.out.old_val->buf == NULL) {
835 /* probably just not available due to ACLs */
837 lsa_blob1.data = q.out.old_val->buf->data;
838 lsa_blob1.length = q.out.old_val->buf->length;
840 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
841 if (!NT_STATUS_IS_OK(status)) {
842 printf("Failed to decrypt secrets OLD blob: %s\n", nt_errstr(status));
846 if (!q.out.old_mtime) {
847 printf("OLD mtime not available on LSA for secret %s\n", old->name);
850 if (old->mtime != *q.out.old_mtime) {
851 printf("OLD mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
852 old->name, nt_time_string(mem_ctx, old->mtime),
853 nt_time_string(mem_ctx, *q.out.old_mtime));
857 if (old->secret.length != lsa_blob_out.length) {
858 printf("Returned secret %s doesn't match: %d != %d\n",
859 old->name, old->secret.length, lsa_blob_out.length);
861 } else if (memcmp(lsa_blob_out.data,
862 old->secret.data, old->secret.length) != 0) {
863 printf("Returned secret %s doesn't match: \n",
865 DEBUG(1, ("SamSync Secret:\n"));
866 dump_data(1, old->secret.data, old->secret.length);
867 DEBUG(1, ("LSA Secret:\n"));
868 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
874 if (q.out.new_val->buf == NULL) {
875 /* probably just not available due to ACLs */
877 lsa_blob1.data = q.out.new_val->buf->data;
878 lsa_blob1.length = q.out.new_val->buf->length;
880 status = sess_decrypt_blob(mem_ctx, &lsa_blob1, &session_key, &lsa_blob_out);
881 if (!NT_STATUS_IS_OK(status)) {
882 printf("Failed to decrypt secrets OLD blob\n");
886 if (!q.out.new_mtime) {
887 printf("NEW mtime not available on LSA for secret %s\n", new->name);
890 if (new->mtime != *q.out.new_mtime) {
891 printf("NEW mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n",
892 new->name, nt_time_string(mem_ctx, new->mtime),
893 nt_time_string(mem_ctx, *q.out.new_mtime));
897 if (new->secret.length != lsa_blob_out.length) {
898 printf("Returned secret %s doesn't match: %d != %d\n",
899 new->name, new->secret.length, lsa_blob_out.length);
901 } else if (memcmp(lsa_blob_out.data,
902 new->secret.data, new->secret.length) != 0) {
903 printf("Returned secret %s doesn't match: \n",
905 DEBUG(1, ("SamSync Secret:\n"));
906 dump_data(1, new->secret.data, new->secret.length);
907 DEBUG(1, ("LSA Secret:\n"));
908 dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
916 static BOOL samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
917 int database_id, struct netr_DELTA_ENUM *delta)
921 struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
922 struct dom_sid *dom_sid = delta->delta_id_union.sid;
924 struct samsync_trusted_domain *new = talloc(samsync_state, struct samsync_trusted_domain);
925 struct lsa_OpenTrustedDomain t;
926 struct policy_handle trustdom_handle;
927 struct lsa_QueryTrustedDomainInfo q;
928 union lsa_TrustedDomainInfo *info[9];
929 int levels [] = {1, 3, 8};
932 new->name = talloc_reference(new, trusted_domain->domain_name.string);
933 new->sid = talloc_reference(new, dom_sid);
935 t.in.handle = samsync_state->lsa_handle;
936 t.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
938 t.out.trustdom_handle = &trustdom_handle;
940 status = dcerpc_lsa_OpenTrustedDomain(samsync_state->p_lsa, mem_ctx, &t);
941 if (!NT_STATUS_IS_OK(status)) {
942 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
946 for (i=0; i< ARRAY_SIZE(levels); i++) {
947 q.in.trustdom_handle = &trustdom_handle;
948 q.in.level = levels[i];
949 status = dcerpc_lsa_QueryTrustedDomainInfo(samsync_state->p_lsa, mem_ctx, &q);
950 if (!NT_STATUS_IS_OK(status)) {
951 if (q.in.level == 8 && NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER)) {
952 info[levels[i]] = NULL;
955 printf("QueryInfoTrustedDomain level %d failed - %s\n",
956 levels[i], nt_errstr(status));
959 info[levels[i]] = q.out.info;
963 TEST_SID_EQUAL(info[8]->full_info.info_ex.sid, dom_sid);
964 TEST_STRING_EQUAL(info[8]->full_info.info_ex.netbios_name, trusted_domain->domain_name);
966 TEST_STRING_EQUAL(info[1]->name.netbios_name, trusted_domain->domain_name);
967 TEST_INT_EQUAL(info[3]->posix_offset.posix_offset, trusted_domain->posix_offset);
969 We would like to do this, but it is NOT_SUPPORTED on win2k3
970 TEST_SEC_DESC_EQUAL(trusted_domain->sdbuf, lsa, &trustdom_handle);
972 new = talloc_reference(samsync_state, new);
973 DLIST_ADD(samsync_state->trusted_domains, new);
978 static BOOL samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *samsync_state,
979 int database_id, struct netr_DELTA_ENUM *delta)
983 struct netr_DELTA_ACCOUNT *account = delta->delta_union.account;
984 struct dom_sid *dom_sid = delta->delta_id_union.sid;
986 struct lsa_OpenAccount a;
987 struct policy_handle acct_handle;
988 struct lsa_EnumPrivsAccount e;
989 struct lsa_LookupPrivName r;
993 BOOL *found_priv_in_lsa;
995 a.in.handle = samsync_state->lsa_handle;
996 a.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
998 a.out.acct_handle = &acct_handle;
1000 status = dcerpc_lsa_OpenAccount(samsync_state->p_lsa, mem_ctx, &a);
1001 if (!NT_STATUS_IS_OK(status)) {
1002 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1006 TEST_SEC_DESC_EQUAL(account->sdbuf, lsa, &acct_handle);
1008 found_priv_in_lsa = talloc_zero_array(mem_ctx, BOOL, account->privilege_entries);
1010 e.in.handle = &acct_handle;
1012 status = dcerpc_lsa_EnumPrivsAccount(samsync_state->p_lsa, mem_ctx, &e);
1013 if (!NT_STATUS_IS_OK(status)) {
1014 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
1018 if ((account->privilege_entries && !e.out.privs)) {
1019 printf("Account %s has privilages in SamSync, but not LSA\n",
1020 dom_sid_string(mem_ctx, dom_sid));
1024 if (!account->privilege_entries && e.out.privs && e.out.privs->count) {
1025 printf("Account %s has privilages in LSA, but not SamSync\n",
1026 dom_sid_string(mem_ctx, dom_sid));
1030 TEST_INT_EQUAL(account->privilege_entries, e.out.privs->count);
1032 for (i=0;i< e.out.privs->count; i++) {
1033 r.in.handle = samsync_state->lsa_handle;
1034 r.in.luid = &e.out.privs->set[i].luid;
1036 status = dcerpc_lsa_LookupPrivName(samsync_state->p_lsa, mem_ctx, &r);
1037 if (!NT_STATUS_IS_OK(status)) {
1038 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
1043 printf("\nLookupPrivName failed to return a name\n");
1046 for (j=0;j<account->privilege_entries; j++) {
1047 if (strcmp(r.out.name->string, account->privilege_name[j].string) == 0) {
1048 found_priv_in_lsa[j] = True;
1053 for (j=0;j<account->privilege_entries; j++) {
1054 if (!found_priv_in_lsa[j]) {
1055 printf("Privilage %s on account %s not found in LSA\n", account->privilege_name[j].string,
1056 dom_sid_string(mem_ctx, dom_sid));
1064 try a netlogon DatabaseSync
1066 static BOOL test_DatabaseSync(struct samsync_state *samsync_state,
1067 TALLOC_CTX *mem_ctx)
1070 TALLOC_CTX *loop_ctx, *delta_ctx, *trustdom_ctx;
1071 struct netr_DatabaseSync r;
1072 const enum netr_SamDatabaseID database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS};
1075 struct samsync_trusted_domain *t;
1076 struct samsync_secret *s;
1078 const char *domain, *username;
1080 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1081 r.in.computername = TEST_MACHINE_NAME;
1082 r.in.preferredmaximumlength = (uint32_t)-1;
1083 ZERO_STRUCT(r.in.return_authenticator);
1085 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1086 r.in.sync_context = 0;
1087 r.in.database_id = database_ids[i];
1089 printf("Testing DatabaseSync of id %d\n", r.in.database_id);
1092 loop_ctx = talloc_named(mem_ctx, 0, "DatabaseSync loop context");
1093 creds_client_authenticator(samsync_state->creds, &r.in.credential);
1095 status = dcerpc_netr_DatabaseSync(samsync_state->p, loop_ctx, &r);
1096 if (!NT_STATUS_IS_OK(status) &&
1097 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1098 printf("DatabaseSync - %s\n", nt_errstr(status));
1103 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1104 printf("Credential chaining failed\n");
1107 r.in.sync_context = r.out.sync_context;
1109 for (d=0; d < r.out.delta_enum_array->num_deltas; d++) {
1110 delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
1111 switch (r.out.delta_enum_array->delta_enum[d].delta_type) {
1112 case NETR_DELTA_DOMAIN:
1113 if (!samsync_handle_domain(delta_ctx, samsync_state,
1114 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1115 printf("Failed to handle DELTA_DOMAIN\n");
1119 case NETR_DELTA_GROUP:
1120 if (!samsync_handle_group(delta_ctx, samsync_state,
1121 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1122 printf("Failed to handle DELTA_USER\n");
1126 case NETR_DELTA_USER:
1127 if (!samsync_handle_user(delta_ctx, samsync_state,
1128 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1129 printf("Failed to handle DELTA_USER\n");
1133 case NETR_DELTA_ALIAS:
1134 if (!samsync_handle_alias(delta_ctx, samsync_state,
1135 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1136 printf("Failed to handle DELTA_ALIAS\n");
1140 case NETR_DELTA_POLICY:
1141 if (!samsync_handle_policy(delta_ctx, samsync_state,
1142 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1143 printf("Failed to handle DELTA_POLICY\n");
1147 case NETR_DELTA_TRUSTED_DOMAIN:
1148 if (!samsync_handle_trusted_domain(delta_ctx, samsync_state,
1149 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1150 printf("Failed to handle DELTA_TRUSTED_DOMAIN\n");
1154 case NETR_DELTA_ACCOUNT:
1155 if (!samsync_handle_account(delta_ctx, samsync_state,
1156 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1157 printf("Failed to handle DELTA_ACCOUNT\n");
1161 case NETR_DELTA_SECRET:
1162 if (!samsync_handle_secret(delta_ctx, samsync_state,
1163 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
1164 printf("Failed to handle DELTA_SECRET\n");
1169 talloc_free(delta_ctx);
1171 talloc_free(loop_ctx);
1172 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1176 domain = samsync_state->domain_name[SAM_DATABASE_DOMAIN];
1178 printf("Never got a DOMAIN object in samsync!\n");
1182 trustdom_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync Trusted domains context");
1184 username = talloc_asprintf(trustdom_ctx, "%s$", domain);
1185 for (t=samsync_state->trusted_domains; t; t=t->next) {
1186 char *secret_name = talloc_asprintf(trustdom_ctx, "G$$%s", t->name);
1187 for (s=samsync_state->secrets; s; s=s->next) {
1188 if (StrCaseCmp(s->name, secret_name) == 0) {
1190 struct samr_Password nt_hash;
1191 mdfour(nt_hash.hash, s->secret.data, s->secret.length);
1193 printf("Checking password for %s\\%s\n", t->name, username);
1194 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1197 TEST_WKSTA_MACHINE_NAME,
1201 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_LOGON_SERVERS)) {
1202 printf("Verifiction of trust password to %s failed: %s (the trusted domain is not available)\n",
1203 t->name, nt_errstr(nt_status));
1207 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1208 printf("Verifiction of trust password to %s: should have failed (nologon interdomain trust account), instead: %s\n",
1209 t->name, nt_errstr(nt_status));
1215 nt_status = test_SamLogon(samsync_state->p_netlogon_wksta, trustdom_ctx, samsync_state->creds_netlogon_wksta,
1218 TEST_WKSTA_MACHINE_NAME,
1223 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
1224 printf("Verifiction of trust password to %s: should have failed (wrong password), instead: %s\n",
1225 t->name, nt_errstr(nt_status));
1233 talloc_free(trustdom_ctx);
1239 try a netlogon DatabaseDeltas
1241 static BOOL test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX *mem_ctx)
1244 TALLOC_CTX *loop_ctx;
1245 struct netr_DatabaseDeltas r;
1246 const uint32_t database_ids[] = {0, 1, 2};
1250 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
1251 r.in.computername = TEST_MACHINE_NAME;
1252 r.in.preferredmaximumlength = (uint32_t)-1;
1253 ZERO_STRUCT(r.in.return_authenticator);
1255 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1256 r.in.database_id = database_ids[i];
1257 r.in.sequence_num = samsync_state->seq_num[i];
1259 if (r.in.sequence_num == 0) continue;
1261 /* this shows that the bdc doesn't need to do a single call for
1262 * each seqnumber, and the pdc doesn't need to know about old values
1265 r.in.sequence_num -= 10;
1268 printf("Testing DatabaseDeltas of id %d at %llu\n",
1269 r.in.database_id, r.in.sequence_num);
1272 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseDeltas loop context");
1273 creds_client_authenticator(samsync_state->creds, &r.in.credential);
1275 status = dcerpc_netr_DatabaseDeltas(samsync_state->p, loop_ctx, &r);
1276 if (!NT_STATUS_IS_OK(status) &&
1277 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) &&
1278 !NT_STATUS_EQUAL(status, NT_STATUS_SYNCHRONIZATION_REQUIRED)) {
1279 printf("DatabaseDeltas - %s\n", nt_errstr(status));
1283 if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
1284 printf("Credential chaining failed\n");
1287 r.in.sequence_num++;
1288 talloc_free(loop_ctx);
1289 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1297 try a netlogon DatabaseSync2
1299 static BOOL test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1300 struct creds_CredentialState *creds)
1303 TALLOC_CTX *loop_ctx;
1304 struct netr_DatabaseSync2 r;
1305 const uint32_t database_ids[] = {0, 1, 2};
1309 r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1310 r.in.computername = TEST_MACHINE_NAME;
1311 r.in.preferredmaximumlength = (uint32_t)-1;
1312 ZERO_STRUCT(r.in.return_authenticator);
1314 for (i=0;i<ARRAY_SIZE(database_ids);i++) {
1315 r.in.sync_context = 0;
1316 r.in.database_id = database_ids[i];
1317 r.in.restart_state = 0;
1319 printf("Testing DatabaseSync2 of id %d\n", r.in.database_id);
1322 loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync2 loop context");
1323 creds_client_authenticator(creds, &r.in.credential);
1325 status = dcerpc_netr_DatabaseSync2(p, loop_ctx, &r);
1326 if (!NT_STATUS_IS_OK(status) &&
1327 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
1328 printf("DatabaseSync2 - %s\n", nt_errstr(status));
1332 if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
1333 printf("Credential chaining failed\n");
1336 r.in.sync_context = r.out.sync_context;
1337 talloc_free(loop_ctx);
1338 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
1346 BOOL torture_rpc_samsync(void)
1349 TALLOC_CTX *mem_ctx;
1351 struct test_join *join_ctx;
1352 struct test_join *join_ctx2;
1353 struct test_join *user_ctx;
1354 const char *machine_password;
1355 const char *wksta_machine_password;
1356 const char *binding = lp_parm_string(-1, "torture", "binding");
1357 struct dcerpc_binding *b;
1358 struct dcerpc_binding *b_netlogon_wksta;
1359 struct samr_Connect c;
1360 struct samr_SetDomainInfo s;
1361 struct policy_handle *domain_policy;
1363 struct lsa_ObjectAttribute attr;
1364 struct lsa_QosInfo qos;
1365 struct lsa_OpenPolicy2 r;
1366 struct cli_credentials *credentials;
1367 struct cli_credentials *credentials_wksta;
1369 struct samsync_state *samsync_state;
1371 char *test_machine_account;
1373 char *test_wksta_machine_account;
1375 mem_ctx = talloc_init("torture_rpc_netlogon");
1377 test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1378 join_ctx = torture_create_testuser(test_machine_account, lp_workgroup(), ACB_SVRTRUST,
1381 talloc_free(mem_ctx);
1382 printf("Failed to join as BDC\n");
1386 test_wksta_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_WKSTA_MACHINE_NAME);
1387 join_ctx2 = torture_create_testuser(test_wksta_machine_account, lp_workgroup(), ACB_WSTRUST,
1388 &wksta_machine_password);
1390 talloc_free(mem_ctx);
1391 printf("Failed to join as member\n");
1395 user_ctx = torture_create_testuser(TEST_USER_NAME,
1399 talloc_free(mem_ctx);
1400 printf("Failed to create test account\n");
1404 samsync_state = talloc_zero(mem_ctx, struct samsync_state);
1406 samsync_state->p_samr = torture_join_samr_pipe(join_ctx);
1407 samsync_state->connect_handle = talloc_zero(samsync_state, struct policy_handle);
1408 samsync_state->lsa_handle = talloc_zero(samsync_state, struct policy_handle);
1409 c.in.system_name = NULL;
1410 c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1411 c.out.connect_handle = samsync_state->connect_handle;
1413 status = dcerpc_samr_Connect(samsync_state->p_samr, mem_ctx, &c);
1414 if (!NT_STATUS_IS_OK(status)) {
1415 printf("samr_Connect failed\n");
1420 domain_policy = samsync_open_domain(mem_ctx, samsync_state, lp_workgroup(), NULL);
1421 if (!domain_policy) {
1422 printf("samrsync_open_domain failed\n");
1427 s.in.domain_handle = domain_policy;
1429 s.in.info = talloc(mem_ctx, union samr_DomainInfo);
1431 s.in.info->info4.comment.string
1432 = talloc_asprintf(mem_ctx,
1433 "Tortured by Samba4: %s",
1434 timestring(mem_ctx, time(NULL)));
1435 status = dcerpc_samr_SetDomainInfo(samsync_state->p_samr, mem_ctx, &s);
1437 if (!test_samr_handle_Close(samsync_state->p_samr, mem_ctx, domain_policy)) {
1442 if (!NT_STATUS_IS_OK(status)) {
1443 printf("SetDomainInfo level %u failed - %s\n",
1444 s.in.level, nt_errstr(status));
1450 status = torture_rpc_connection(samsync_state,
1451 &samsync_state->p_lsa,
1454 DCERPC_LSARPC_VERSION);
1456 if (!NT_STATUS_IS_OK(status)) {
1462 qos.impersonation_level = 2;
1463 qos.context_mode = 1;
1464 qos.effective_only = 0;
1467 attr.root_dir = NULL;
1468 attr.object_name = NULL;
1469 attr.attributes = 0;
1470 attr.sec_desc = NULL;
1471 attr.sec_qos = &qos;
1473 r.in.system_name = "\\";
1475 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1476 r.out.handle = samsync_state->lsa_handle;
1478 status = dcerpc_lsa_OpenPolicy2(samsync_state->p_lsa, mem_ctx, &r);
1479 if (!NT_STATUS_IS_OK(status)) {
1480 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1485 status = dcerpc_parse_binding(mem_ctx, binding, &b);
1486 if (!NT_STATUS_IS_OK(status)) {
1487 printf("Bad binding string %s\n", binding);
1492 b->flags &= ~DCERPC_AUTH_OPTIONS;
1493 b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1495 credentials = cli_credentials_init(mem_ctx);
1497 cli_credentials_set_workstation(credentials, TEST_MACHINE_NAME, CRED_SPECIFIED);
1498 cli_credentials_set_domain(credentials, lp_workgroup(), CRED_SPECIFIED);
1499 cli_credentials_set_username(credentials, test_machine_account, CRED_SPECIFIED);
1500 cli_credentials_set_password(credentials, machine_password, CRED_SPECIFIED);
1501 cli_credentials_set_secure_channel_type(credentials,
1504 status = dcerpc_pipe_connect_b(samsync_state,
1505 &samsync_state->p, b,
1506 DCERPC_NETLOGON_UUID,
1507 DCERPC_NETLOGON_VERSION,
1510 if (!NT_STATUS_IS_OK(status)) {
1511 printf("Failed to connect to server as a BDC: %s\n", nt_errstr(status));
1516 status = dcerpc_schannel_creds(samsync_state->p->conn->security_state.generic_state,
1517 samsync_state, &samsync_state->creds);
1518 if (!NT_STATUS_IS_OK(status)) {
1524 status = dcerpc_parse_binding(mem_ctx, binding, &b_netlogon_wksta);
1525 if (!NT_STATUS_IS_OK(status)) {
1526 printf("Bad binding string %s\n", binding);
1531 b_netlogon_wksta->flags &= ~DCERPC_AUTH_OPTIONS;
1532 b_netlogon_wksta->flags |= DCERPC_SCHANNEL | DCERPC_SIGN;
1534 credentials_wksta = cli_credentials_init(mem_ctx);
1536 cli_credentials_set_workstation(credentials_wksta, TEST_WKSTA_MACHINE_NAME, CRED_SPECIFIED);
1537 cli_credentials_set_domain(credentials_wksta, lp_workgroup(), CRED_SPECIFIED);
1538 cli_credentials_set_username(credentials_wksta, test_wksta_machine_account, CRED_SPECIFIED);
1539 cli_credentials_set_password(credentials_wksta, wksta_machine_password, CRED_SPECIFIED);
1540 cli_credentials_set_secure_channel_type(credentials_wksta,
1543 status = dcerpc_pipe_connect_b(samsync_state,
1544 &samsync_state->p_netlogon_wksta,
1546 DCERPC_NETLOGON_UUID,
1547 DCERPC_NETLOGON_VERSION,
1550 if (!NT_STATUS_IS_OK(status)) {
1551 printf("Failed to connect to server as a Workstation: %s\n", nt_errstr(status));
1556 status = dcerpc_schannel_creds(samsync_state->p_netlogon_wksta->conn->security_state.generic_state,
1557 samsync_state, &samsync_state->creds_netlogon_wksta);
1558 if (!NT_STATUS_IS_OK(status)) {
1559 printf("Failed to obtail schanel creds!\n");
1563 if (!test_DatabaseSync(samsync_state, mem_ctx)) {
1564 printf("DatabaseSync failed\n");
1568 if (!test_DatabaseDeltas(samsync_state, mem_ctx)) {
1569 printf("DatabaseDeltas failed\n");
1573 if (!test_DatabaseSync2(samsync_state->p, mem_ctx, samsync_state->creds)) {
1574 printf("DatabaseSync2 failed\n");
1579 torture_leave_domain(join_ctx);
1580 torture_leave_domain(join_ctx2);
1581 torture_leave_domain(user_ctx);
1583 talloc_free(mem_ctx);