2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2007
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2012
6 Copyright (C) Christof Schmit <christof.schmitt@us.ibm.com> 2012
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/smbtorture.h"
24 #include "torture/winbind/proto.h"
25 #include "auth/auth.h"
26 #include "auth/auth_sam_reply.h"
27 #include "auth/gensec/gensec.h"
28 #include "system/kerberos.h"
29 #include "auth/kerberos/kerberos.h"
30 #include "auth/credentials/credentials.h"
31 #include "param/param.h"
32 #include "lib/cmdline/popt_common.h"
33 #include "auth/kerberos/pac_utils.h"
40 /* A helper function which avoids touching the local databases to
41 * generate the session info, as we just want to verify the PAC
42 * details, not the full local token */
43 static NTSTATUS test_generate_session_info_pac(struct auth4_context *auth_ctx,
45 struct smb_krb5_context *smb_krb5_context,
47 const char *principal_name,
48 const struct tsocket_address *remote_address,
49 uint32_t session_info_flags,
50 struct auth_session_info **session_info)
53 struct auth_user_info_dc *user_info_dc;
55 struct pac_data *pac_data;
57 tmp_ctx = talloc_named(mem_ctx, 0, "gensec_gssapi_session_info context");
58 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
60 auth_ctx->private_data = pac_data = talloc_zero(auth_ctx, struct pac_data);
62 pac_data->pac_blob = *pac_blob;
64 talloc_steal(pac_data, pac_data->pac_blob.data);
65 nt_status = kerberos_pac_blob_to_user_info_dc(tmp_ctx,
67 smb_krb5_context->krb5_context,
70 if (!NT_STATUS_IS_OK(nt_status)) {
75 if (user_info_dc->info->authenticated) {
76 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
79 session_info_flags |= AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
80 nt_status = auth_generate_session_info(mem_ctx,
83 user_info_dc, session_info_flags,
85 if (!NT_STATUS_IS_OK(nt_status)) {
94 static bool torture_decode_compare_pac(struct torture_context *tctx,
97 struct wbcAuthUserParams params;
98 struct wbcAuthUserInfo *info;
99 struct wbcAuthErrorInfo *error;
100 struct PAC_LOGON_INFO *logon_info;
101 struct netr_SamInfo3 *info3;
102 struct netr_SamBaseInfo *base;
108 /* Let winbind decode the PAC */
109 memset(¶ms, 0, sizeof(params));
110 params.level = WBC_AUTH_USER_LEVEL_PAC;
111 params.password.pac.data = pac.data;
112 params.password.pac.length = pac.length;
114 wbc_err = wbcAuthenticateUserEx(¶ms, &info, &error);
115 torture_assert(tctx, WBC_ERROR_IS_OK(wbc_err), wbcErrorString(wbc_err));
117 /* Decode the PAC internally */
118 status = kerberos_pac_logon_info(tctx, pac, NULL, NULL, NULL, NULL, 0,
120 torture_assert(tctx, NT_STATUS_IS_OK(status), "pac_logon_info");
121 info3 = &logon_info->info3;
124 /* Compare the decoded data from winbind and from internal call */
125 torture_assert(tctx, info->user_flags == base->user_flags, "user_flags");
126 torture_assert_str_equal(tctx, info->account_name, base->account_name.string, "account_name");
127 torture_assert_str_equal(tctx, info->full_name, base->full_name.string, "full_name");
128 torture_assert_str_equal(tctx, info->domain_name, base->logon_domain.string, "domain_name");
129 torture_assert(tctx, info->acct_flags == base->acct_flags, "acct_flags");
130 torture_assert(tctx, info->logon_count == base->logon_count, "logon_count");
131 torture_assert(tctx, info->bad_password_count == base->bad_password_count, "bad_password_count");
132 torture_assert(tctx, info->logon_time == nt_time_to_unix(base->logon_time), "logon_time");
133 torture_assert(tctx, info->logoff_time == nt_time_to_unix(base->logoff_time), "logoff_time");
134 torture_assert(tctx, info->kickoff_time == nt_time_to_unix(base->kickoff_time), "kickoff_time");
135 torture_assert(tctx, info->pass_last_set_time == nt_time_to_unix(base->last_password_change), "last_password_change");
136 torture_assert(tctx, info->pass_can_change_time == nt_time_to_unix(base->allow_password_change), "allow_password_change");
137 torture_assert(tctx, info->pass_must_change_time == nt_time_to_unix(base->force_password_change), "force_password_change");
138 torture_assert(tctx, info->num_sids == 2 + base->groups.count + info3->sidcount, "num_sids");
141 wbcSidToStringBuf(&info->sids[sid_idx].sid, sid_str, sizeof(sid_str));
143 dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
144 dom_sid_add_rid(tctx, base->domain_sid, base->rid)),
148 wbcSidToStringBuf(&info->sids[sid_idx].sid, sid_str, sizeof(sid_str));
150 dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
151 dom_sid_add_rid(tctx, base->domain_sid, base->primary_gid)),
154 for(i = 0; i < base->groups.count; i++ ) {
156 wbcSidToStringBuf(&info->sids[sid_idx].sid,
157 sid_str, sizeof(sid_str));
159 dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
160 dom_sid_add_rid(tctx, base->domain_sid,
161 base->groups.rids[i].rid)),
165 for(i = 0; i < info3->sidcount; i++) {
167 wbcSidToStringBuf(&info->sids[sid_idx].sid,
168 sid_str, sizeof(sid_str));
170 dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
178 static bool torture_winbind_pac(struct torture_context *tctx,
179 const char *sasl_mech,
184 struct gensec_security *gensec_client_context;
185 struct gensec_security *gensec_server_context;
186 struct cli_credentials *machine_credentials;
188 DATA_BLOB client_to_server, server_to_client;
190 struct auth4_context *auth_context;
191 struct auth_session_info *session_info;
192 struct pac_data *pac_data;
194 TALLOC_CTX *tmp_ctx = talloc_new(tctx);
195 torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
197 machine_credentials = cli_credentials_init(tmp_ctx);
198 torture_assert(tctx, machine_credentials != NULL, "cli_credentials_init() failed");
200 cli_credentials_set_conf(machine_credentials, tctx->lp_ctx);
202 status = cli_credentials_set_machine_account(machine_credentials, tctx->lp_ctx);
203 torture_assert_ntstatus_ok(tctx, status, " cli_credentials_set_machine_account() (for server) failed");
205 auth_context = talloc_zero(tmp_ctx, struct auth4_context);
206 torture_assert(tctx, auth_context != NULL, "talloc_new() failed");
208 auth_context->generate_session_info_pac = test_generate_session_info_pac;
210 status = gensec_client_start(tctx, &gensec_client_context,
211 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
212 torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
214 status = gensec_set_target_hostname(gensec_client_context, cli_credentials_get_workstation(machine_credentials));
215 torture_assert_ntstatus_ok(tctx, status, "gensec_set_target_hostname (client) failed");
217 status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
218 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
221 status = gensec_start_mech_by_sasl_name(gensec_client_context, sasl_mech);
222 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
224 status = gensec_start_mech_by_name(gensec_client_context, mech);
225 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_name (client) failed");
229 status = gensec_server_start(tctx,
230 lpcfg_gensec_settings(tctx, tctx->lp_ctx),
231 auth_context, &gensec_server_context);
232 torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
234 status = gensec_set_credentials(gensec_server_context, machine_credentials);
235 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
238 status = gensec_start_mech_by_sasl_name(gensec_server_context, sasl_mech);
239 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
241 status = gensec_start_mech_by_name(gensec_server_context, mech);
242 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_name (server) failed");
245 server_to_client = data_blob(NULL, 0);
248 /* Do a client-server update dance */
249 status = gensec_update(gensec_client_context, tmp_ctx, server_to_client, &client_to_server);
250 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
251 torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
254 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
255 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
256 torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
259 if (NT_STATUS_IS_OK(status)) {
264 /* Extract the PAC using Samba's code */
266 status = gensec_session_info(gensec_server_context, gensec_server_context, &session_info);
267 torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
269 pac_data = talloc_get_type(auth_context->private_data, struct pac_data);
271 torture_assert(tctx, pac_data != NULL, "gensec_update failed to fill in pac_data in auth_context");
272 torture_assert(tctx, pac_data->pac_blob.data != NULL, "pac_blob not present");
273 torture_decode_compare_pac(tctx, pac_data->pac_blob);
278 static bool torture_winbind_pac_gssapi(struct torture_context *tctx)
280 return torture_winbind_pac(tctx, "GSSAPI", NULL);
283 static bool torture_winbind_pac_gss_spnego(struct torture_context *tctx)
285 return torture_winbind_pac(tctx, "GSS-SPNEGO", NULL);
288 static bool torture_winbind_pac_krb5(struct torture_context *tctx)
290 return torture_winbind_pac(tctx, NULL, "krb5");
293 NTSTATUS torture_winbind_init(TALLOC_CTX *ctx)
295 struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "winbind");
296 struct torture_suite *pac_suite;
297 torture_suite_add_suite(suite, torture_winbind_struct_init());
298 torture_suite_add_suite(suite, torture_wbclient());
300 pac_suite = torture_suite_create(talloc_autofree_context(), "pac");
301 torture_suite_add_simple_test(pac_suite,
302 "GSSAPI", torture_winbind_pac_gssapi);
303 torture_suite_add_simple_test(pac_suite,
304 "GSS-SPNEGO", torture_winbind_pac_gss_spnego);
305 torture_suite_add_simple_test(pac_suite,
306 "krb5", torture_winbind_pac_krb5);
308 pac_suite->description = talloc_strdup(suite, "Winbind Kerberos PAC tests");
310 torture_suite_add_suite(suite, pac_suite);
312 suite->description = talloc_strdup(suite, "WINBIND tests");
314 torture_register_suite(suite);