2 Unix SMB/CIFS implementation.
4 test suite for netlogon PAC operations
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "torture/torture.h"
24 #include "lib/events/events.h"
25 #include "auth/auth.h"
26 #include "auth/gensec/gensec.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "torture/rpc/rpc.h"
29 #include "torture/rpc/netlogon.h"
30 #include "libcli/auth/libcli_auth.h"
31 #include "librpc/gen_ndr/ndr_netlogon_c.h"
32 #include "librpc/gen_ndr/ndr_krb5pac.h"
33 #include "param/param.h"
34 #include "lib/messaging/irpc.h"
35 #include "cluster/cluster.h"
37 #define TEST_MACHINE_NAME "torturepactest"
39 /* Check to see if we can pass the PAC across to the NETLOGON server for validation */
41 /* Also happens to be a really good one-step verfication of our Kerberos stack */
43 static bool test_PACVerify(struct torture_context *tctx,
44 struct dcerpc_pipe *p,
45 struct cli_credentials *credentials)
49 struct netr_LogonSamLogon r;
51 struct netr_GenericInfo generic;
52 struct netr_Authenticator auth, auth2;
55 struct creds_CredentialState *creds;
56 struct gensec_security *gensec_client_context;
57 struct gensec_security *gensec_server_context;
59 struct messaging_context *msg_server_ctx;
60 DATA_BLOB client_to_server, server_to_client, pac_wrapped, payload;
61 struct PAC_Validate pac_wrapped_struct;
63 enum ndr_err_code ndr_err;
65 struct auth_session_info *session_info;
69 TALLOC_CTX *tmp_ctx = talloc_new(tctx);
73 torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
75 if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS,
76 credentials, SEC_CHAN_BDC,
81 status = torture_temp_dir(tctx, "PACVerify", &tmp_dir);
82 torture_assert_ntstatus_ok(tctx, status, "torture_temp_dir failed");
84 msg_server_ctx = messaging_init(tctx,
87 lp_iconv_convenience(tctx->lp_ctx),
90 torture_assert(tctx, msg_server_ctx != NULL, "Failed to init messaging context");
92 status = gensec_client_start(tctx, &gensec_client_context, tctx->ev, tctx->lp_ctx);
93 torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
95 status = gensec_set_target_hostname(gensec_client_context, TEST_MACHINE_NAME);
97 status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
98 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
100 status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
101 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
103 status = gensec_server_start(tctx, tctx->ev, tctx->lp_ctx, msg_server_ctx, &gensec_server_context);
104 torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
106 status = gensec_set_credentials(gensec_server_context, credentials);
107 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
109 status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
110 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
112 server_to_client = data_blob(NULL, 0);
115 /* Do a client-server update dance */
116 status = gensec_update(gensec_client_context, tmp_ctx, server_to_client, &client_to_server);
117 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
118 torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
121 if (client_to_server.length == 0) {
125 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
126 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
127 torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
130 if (server_to_client.length == 0) {
135 /* Extract the PAC using Samba's code */
137 status = gensec_session_info(gensec_server_context, &session_info);
138 torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
140 pac_wrapped_struct.MessageType = 0x3;
141 pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
142 pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
143 pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
144 pac_wrapped_struct.ChecksumAndSignature = payload
145 = data_blob_talloc(tmp_ctx, NULL,
146 pac_wrapped_struct.ChecksumLength
147 + pac_wrapped_struct.SignatureLength);
148 memcpy(&payload.data[0],
149 session_info->server_info->pac_srv_sig.signature.data,
150 pac_wrapped_struct.ChecksumLength);
151 memcpy(&payload.data[pac_wrapped_struct.ChecksumLength],
152 session_info->server_info->pac_kdc_sig.signature.data,
153 pac_wrapped_struct.SignatureLength);
155 ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
156 (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
157 torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
159 torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
160 creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
162 generic.length = pac_wrapped.length;
163 generic.data = pac_wrapped.data;
165 /* Validate it over the netlogon pipe */
167 generic.identity_info.parameter_control = 0;
168 generic.identity_info.logon_id_high = 0;
169 generic.identity_info.logon_id_low = 0;
170 generic.identity_info.domain_name.string = session_info->server_info->domain_name;
171 generic.identity_info.account_name.string = session_info->server_info->account_name;
172 generic.identity_info.workstation.string = TEST_MACHINE_NAME;
174 generic.package_name.string = "Kerberos";
177 creds_client_authenticator(creds, &auth);
178 r.in.credential = &auth;
179 r.in.return_authenticator = &auth2;
180 r.in.logon_level = NetlogonGenericInformation;
181 r.in.logon.generic = &generic;
182 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
183 r.in.computer_name = cli_credentials_get_workstation(credentials);
184 r.in.validation_level = NetlogonValidationGenericInfo2;
186 status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
188 torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
190 /* This will break the signature nicely (even in the crypto wrapping), check we get a logon failure */
191 generic.data[generic.length-1]++;
194 creds_client_authenticator(creds, &auth);
195 r.in.credential = &auth;
196 r.in.return_authenticator = &auth2;
197 r.in.logon_level = NetlogonGenericInformation;
198 r.in.logon.generic = &generic;
199 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
200 r.in.computer_name = cli_credentials_get_workstation(credentials);
201 r.in.validation_level = NetlogonValidationGenericInfo2;
203 status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
205 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
207 torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred),
208 "Credential chaining failed");
210 /* This will break message type, check that however we still get NT_STATUS_OK */
211 for (i=0; i < 256; i++) {
212 pac_wrapped_struct.MessageType = i;
213 pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
214 pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
215 pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
216 pac_wrapped_struct.ChecksumAndSignature = payload
217 = data_blob_talloc(tmp_ctx, NULL,
218 pac_wrapped_struct.ChecksumLength
219 + pac_wrapped_struct.SignatureLength);
220 memcpy(&payload.data[0],
221 session_info->server_info->pac_srv_sig.signature.data,
222 pac_wrapped_struct.ChecksumLength);
223 memcpy(&payload.data[pac_wrapped_struct.ChecksumLength],
224 session_info->server_info->pac_kdc_sig.signature.data,
225 pac_wrapped_struct.SignatureLength);
227 ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
228 (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
229 torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
231 torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
232 creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
234 generic.length = pac_wrapped.length;
235 generic.data = pac_wrapped.data;
238 creds_client_authenticator(creds, &auth);
239 r.in.credential = &auth;
240 r.in.return_authenticator = &auth2;
241 r.in.logon_level = NetlogonGenericInformation;
242 r.in.logon.generic = &generic;
243 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
244 r.in.computer_name = cli_credentials_get_workstation(credentials);
245 r.in.validation_level = NetlogonValidationGenericInfo2;
247 status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
249 torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
251 torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred),
252 "Credential chaining failed");
255 /* This will break the parsing nicely (even in the crypto wrapping), check we get INVALID_PARAMETER */
259 creds_client_authenticator(creds, &auth);
260 r.in.credential = &auth;
261 r.in.return_authenticator = &auth2;
262 r.in.logon_level = NetlogonGenericInformation;
263 r.in.logon.generic = &generic;
264 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
265 r.in.computer_name = cli_credentials_get_workstation(credentials);
266 r.in.validation_level = NetlogonValidationGenericInfo2;
268 status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
270 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
272 torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred),
273 "Credential chaining failed");
275 pac_wrapped_struct.MessageType = 0x3;
276 pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
277 pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
279 /* Break the SignatureType */
280 pac_wrapped_struct.SignatureType++;
282 pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
283 pac_wrapped_struct.ChecksumAndSignature = payload
284 = data_blob_talloc(tmp_ctx, NULL,
285 pac_wrapped_struct.ChecksumLength
286 + pac_wrapped_struct.SignatureLength);
287 memcpy(&payload.data[0],
288 session_info->server_info->pac_srv_sig.signature.data,
289 pac_wrapped_struct.ChecksumLength);
290 memcpy(&payload.data[pac_wrapped_struct.ChecksumLength],
291 session_info->server_info->pac_kdc_sig.signature.data,
292 pac_wrapped_struct.SignatureLength);
294 ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
295 (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
296 torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
298 torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
299 creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
301 generic.length = pac_wrapped.length;
302 generic.data = pac_wrapped.data;
305 creds_client_authenticator(creds, &auth);
306 r.in.credential = &auth;
307 r.in.return_authenticator = &auth2;
308 r.in.logon_level = NetlogonGenericInformation;
309 r.in.logon.generic = &generic;
310 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
311 r.in.computer_name = cli_credentials_get_workstation(credentials);
312 r.in.validation_level = NetlogonValidationGenericInfo2;
314 status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
316 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
318 torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred),
319 "Credential chaining failed");
322 pac_wrapped_struct.MessageType = 0x3;
323 pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
324 pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
325 pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
327 pac_wrapped_struct.ChecksumAndSignature = payload
328 = data_blob_talloc(tmp_ctx, NULL,
329 pac_wrapped_struct.ChecksumLength
330 + pac_wrapped_struct.SignatureLength);
331 memcpy(&payload.data[0],
332 session_info->server_info->pac_srv_sig.signature.data,
333 pac_wrapped_struct.ChecksumLength);
334 memcpy(&payload.data[pac_wrapped_struct.ChecksumLength],
335 session_info->server_info->pac_kdc_sig.signature.data,
336 pac_wrapped_struct.SignatureLength);
338 /* Break the signature length */
339 pac_wrapped_struct.SignatureLength++;
341 ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
342 (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
343 torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
345 torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
346 creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
348 generic.length = pac_wrapped.length;
349 generic.data = pac_wrapped.data;
352 creds_client_authenticator(creds, &auth);
353 r.in.credential = &auth;
354 r.in.return_authenticator = &auth2;
355 r.in.logon_level = NetlogonGenericInformation;
356 r.in.logon.generic = &generic;
357 r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
358 r.in.computer_name = cli_credentials_get_workstation(credentials);
359 r.in.validation_level = NetlogonValidationGenericInfo2;
361 status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
363 torture_assert_ntstatus_equal(tctx, status, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
365 torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred),
366 "Credential chaining failed");
370 struct torture_suite *torture_rpc_remote_pac(TALLOC_CTX *mem_ctx)
372 struct torture_suite *suite = torture_suite_create(mem_ctx, "PAC");
373 struct torture_rpc_tcase *tcase;
375 tcase = torture_suite_add_machine_rpc_iface_tcase(suite, "netlogon",
376 &ndr_table_netlogon, TEST_MACHINE_NAME);
377 torture_rpc_tcase_add_test_creds(tcase, "verify", test_PACVerify);