2 Unix SMB/CIFS implementation.
4 test suite for schannel operations
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "librpc/gen_ndr/ndr_samr.h"
25 #include "librpc/gen_ndr/ndr_netlogon.h"
26 #include "lib/cmdline/popt_common.h"
28 #define TEST_MACHINE_NAME "schanneltest"
31 do some samr ops using the schannel connection
33 static BOOL test_samr_ops(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
36 struct samr_GetDomPwInfo r;
37 struct samr_Connect connect;
38 struct samr_OpenDomain opendom;
40 struct lsa_String name;
41 struct policy_handle handle;
42 struct policy_handle domain_handle;
44 name.string = lp_workgroup();
45 r.in.domain_name = &name;
47 connect.in.system_name = 0;
48 connect.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
49 connect.out.connect_handle = &handle;
51 printf("Testing Connect and OpenDomain on BUILTIN\n");
53 status = dcerpc_samr_Connect(p, mem_ctx, &connect);
54 if (!NT_STATUS_IS_OK(status)) {
55 printf("Connect failed - %s\n", nt_errstr(status));
59 opendom.in.connect_handle = &handle;
60 opendom.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
61 opendom.in.sid = dom_sid_parse_talloc(mem_ctx, "S-1-5-32");
62 opendom.out.domain_handle = &domain_handle;
64 status = dcerpc_samr_OpenDomain(p, mem_ctx, &opendom);
65 if (!NT_STATUS_IS_OK(status)) {
66 printf("OpenDomain failed - %s\n", nt_errstr(status));
70 printf("Testing GetDomPwInfo with name %s\n", r.in.domain_name->string);
72 /* do several ops to test credential chaining */
74 status = dcerpc_samr_GetDomPwInfo(p, mem_ctx, &r);
75 if (!NT_STATUS_IS_OK(status)) {
76 printf("GetDomPwInfo op %d failed - %s\n", i, nt_errstr(status));
86 try a netlogon SamLogon
88 static BOOL test_netlogon_ops(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
89 struct creds_CredentialState *creds)
92 struct netr_LogonSamLogon r;
93 struct netr_Authenticator auth, auth2;
94 struct netr_NetworkInfo ninfo;
95 const char *username = cli_credentials_get_username(cmdline_credentials);
96 const char *password = cli_credentials_get_password(cmdline_credentials);
100 ninfo.identity_info.domain_name.string = lp_workgroup();
101 ninfo.identity_info.parameter_control = 0;
102 ninfo.identity_info.logon_id_low = 0;
103 ninfo.identity_info.logon_id_high = 0;
104 ninfo.identity_info.account_name.string = username;
105 ninfo.identity_info.workstation.string = TEST_MACHINE_NAME;
106 generate_random_buffer(ninfo.challenge,
107 sizeof(ninfo.challenge));
108 ninfo.nt.length = 24;
109 ninfo.nt.data = talloc_size(mem_ctx, 24);
110 SMBNTencrypt(password, ninfo.challenge, ninfo.nt.data);
111 ninfo.lm.length = 24;
112 ninfo.lm.data = talloc_size(mem_ctx, 24);
113 SMBencrypt(password, ninfo.challenge, ninfo.lm.data);
116 r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
117 r.in.workstation = TEST_MACHINE_NAME;
118 r.in.credential = &auth;
119 r.in.return_authenticator = &auth2;
120 r.in.logon_level = 2;
121 r.in.logon.network = &ninfo;
123 printf("Testing LogonSamLogon with name %s\n", username);
127 creds_client_authenticator(creds, &auth);
129 r.in.validation_level = i;
131 status = dcerpc_netr_LogonSamLogon(p, mem_ctx, &r);
133 if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
134 printf("Credential chaining failed\n");
143 test a schannel connection with the given flags
145 static BOOL test_schannel(TALLOC_CTX *mem_ctx,
146 uint16_t acct_flags, uint32_t dcerpc_flags,
147 uint32_t schannel_type)
150 const char *machine_password;
152 const char *binding = lp_parm_string(-1, "torture", "binding");
153 struct dcerpc_binding *b;
154 struct dcerpc_pipe *p = NULL;
155 struct dcerpc_pipe *p_netlogon = NULL;
156 struct creds_CredentialState *creds;
157 struct cli_credentials *credentials;
159 TALLOC_CTX *test_ctx = talloc_named(mem_ctx, 0, "test_schannel context");
160 char *test_machine_account = talloc_asprintf(NULL, "%s$", TEST_MACHINE_NAME);
162 join_ctx = torture_create_testuser(test_machine_account, lp_workgroup(),
163 acct_flags, &machine_password);
165 printf("Failed to join domain with acct_flags=0x%x\n", acct_flags);
166 talloc_free(test_ctx);
170 status = dcerpc_parse_binding(test_ctx, binding, &b);
171 if (!NT_STATUS_IS_OK(status)) {
172 printf("Bad binding string %s\n", binding);
176 b->flags &= ~DCERPC_AUTH_OPTIONS;
177 b->flags |= dcerpc_flags;
179 credentials = cli_credentials_init(mem_ctx);
180 cli_credentials_set_conf(credentials);
182 cli_credentials_set_domain(credentials, lp_workgroup(), CRED_SPECIFIED);
183 cli_credentials_set_workstation(credentials, TEST_MACHINE_NAME, CRED_SPECIFIED);
184 cli_credentials_set_username(credentials, test_machine_account, CRED_SPECIFIED);
185 cli_credentials_set_password(credentials, machine_password, CRED_SPECIFIED);
187 if (acct_flags == ACB_WSTRUST) {
188 cli_credentials_set_secure_channel_type(credentials,
190 } else if (acct_flags == ACB_SVRTRUST) {
191 cli_credentials_set_secure_channel_type(credentials,
197 status = dcerpc_pipe_connect_b(test_ctx,
202 if (!NT_STATUS_IS_OK(status)) {
203 printf("Failed to connect with schannel: %s\n", nt_errstr(status));
207 if (!test_samr_ops(p, test_ctx)) {
208 printf("Failed to process schannel secured ops\n");
212 status = dcerpc_schannel_creds(p->conn->security_state.generic_state, test_ctx, &creds);
213 if (!NT_STATUS_IS_OK(status)) {
217 /* Also test that when we connect to the netlogon pipe, that
218 * the credentials we setup on the first pipe are valid for
221 /* Swap the binding details from SAMR to NETLOGON */
222 status = dcerpc_epm_map_binding(test_ctx, b, DCERPC_NETLOGON_UUID,
223 DCERPC_NETLOGON_VERSION, NULL);
224 if (!NT_STATUS_IS_OK(status)) {
228 status = dcerpc_secondary_connection(p, &p_netlogon,
231 if (!NT_STATUS_IS_OK(status)) {
235 status = dcerpc_bind_auth_password(p_netlogon,
236 DCERPC_NETLOGON_UUID,
237 DCERPC_NETLOGON_VERSION,
238 credentials, DCERPC_AUTH_TYPE_SCHANNEL,
241 if (!NT_STATUS_IS_OK(status)) {
245 status = dcerpc_schannel_creds(p_netlogon->conn->security_state.generic_state, test_ctx, &creds);
246 if (!NT_STATUS_IS_OK(status)) {
250 /* do a couple of logins */
251 if (!test_netlogon_ops(p_netlogon, test_ctx, creds)) {
252 printf("Failed to process schannel secured ops\n");
256 torture_leave_domain(join_ctx);
257 talloc_free(test_ctx);
261 torture_leave_domain(join_ctx);
262 talloc_free(test_ctx);
267 a schannel test suite
269 BOOL torture_rpc_schannel(void)
275 uint32_t dcerpc_flags;
276 uint32_t schannel_type;
278 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SIGN, 3 },
279 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SEAL, 3 },
280 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128, 3 },
281 { ACB_WSTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128, 3 },
282 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SIGN, 3 },
283 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SEAL, 3 },
284 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128, 3 },
285 { ACB_SVRTRUST, DCERPC_SCHANNEL | DCERPC_SEAL | DCERPC_SCHANNEL_128, 3 }
289 mem_ctx = talloc_init("torture_rpc_schannel");
291 for (i=0;i<ARRAY_SIZE(tests);i++) {
292 if (!test_schannel(mem_ctx,
293 tests[i].acct_flags, tests[i].dcerpc_flags, tests[i].schannel_type)) {
294 printf("Failed with acct_flags=0x%x dcerpc_flags=0x%x schannel_type=%d\n",
295 tests[i].acct_flags, tests[i].dcerpc_flags, tests[i].schannel_type);
301 talloc_free(mem_ctx);