s4-torture: Complete test for winbindd PAC parsing
[Samba/gbeck.git] / source4 / torture / winbind / winbind.c
blob5956834efac0dd37c61d616f45a6791fd93c83a4
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester
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/>.
22 #include "includes.h"
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"
34 #include "wbclient.h"
36 struct pac_data {
37 DATA_BLOB pac_blob;
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,
44 TALLOC_CTX *mem_ctx,
45 struct smb_krb5_context *smb_krb5_context,
46 DATA_BLOB *pac_blob,
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)
52 NTSTATUS nt_status;
53 struct auth_user_info_dc *user_info_dc;
54 TALLOC_CTX *tmp_ctx;
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,
66 *pac_blob,
67 smb_krb5_context->krb5_context,
68 &user_info_dc,
69 NULL, NULL);
70 if (!NT_STATUS_IS_OK(nt_status)) {
71 talloc_free(tmp_ctx);
72 return 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,
81 NULL,
82 NULL,
83 user_info_dc, session_info_flags,
84 session_info);
85 if (!NT_STATUS_IS_OK(nt_status)) {
86 talloc_free(tmp_ctx);
87 return nt_status;
90 talloc_free(tmp_ctx);
91 return nt_status;
94 static bool torture_decode_compare_pac(struct torture_context *tctx,
95 DATA_BLOB pac)
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;
103 wbcErr wbc_err;
104 NTSTATUS status;
105 int result, sid_idx, i;
106 char sid_str[50];
108 /* Let winbind decode the PAC */
109 memset(&params, 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(&params, &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,
119 &logon_info);
120 torture_assert(tctx, NT_STATUS_IS_OK(status), "pac_logon_info");
121 info3 = &logon_info->info3;
122 base = &info3->base;
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");
140 sid_idx = 0;
141 wbcSidToStringBuf(&info->sids[sid_idx].sid, sid_str, sizeof(sid_str));
142 torture_assert(tctx,
143 dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
144 dom_sid_add_rid(tctx, base->domain_sid, base->rid)),
145 sid_str);
147 sid_idx++;
148 wbcSidToStringBuf(&info->sids[sid_idx].sid, sid_str, sizeof(sid_str));
149 torture_assert(tctx,
150 dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
151 dom_sid_add_rid(tctx, base->domain_sid, base->primary_gid)),
152 sid_str);
154 for(i = 0; i < base->groups.count; i++ ) {
155 sid_idx++;
156 wbcSidToStringBuf(&info->sids[sid_idx].sid,
157 sid_str, sizeof(sid_str));
158 torture_assert(tctx,
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)),
162 sid_str);
165 for(i = 0; i < info3->sidcount; i++) {
166 sid_idx++;
167 wbcSidToStringBuf(&info->sids[sid_idx].sid,
168 sid_str, sizeof(sid_str));
169 torture_assert(tctx,
170 dom_sid_equal(dom_sid_parse_talloc(tctx, sid_str),
171 info3->sids[i].sid),
172 sid_str);
175 return true;
178 static bool torture_winbind_pac(struct torture_context *tctx)
180 NTSTATUS status;
182 struct gensec_security *gensec_client_context;
183 struct gensec_security *gensec_server_context;
185 DATA_BLOB client_to_server, server_to_client;
187 struct auth4_context *auth_context;
188 struct auth_session_info *session_info;
189 struct pac_data *pac_data;
191 TALLOC_CTX *tmp_ctx = talloc_new(tctx);
192 torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
194 auth_context = talloc_zero(tmp_ctx, struct auth4_context);
195 torture_assert(tctx, auth_context != NULL, "talloc_new() failed");
197 auth_context->generate_session_info_pac = test_generate_session_info_pac;
199 status = gensec_client_start(tctx, &gensec_client_context,
200 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
201 torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
203 status = gensec_set_target_hostname(gensec_client_context, cli_credentials_get_workstation(cmdline_credentials));
205 status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
206 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
208 status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
209 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
211 status = gensec_server_start(tctx,
212 lpcfg_gensec_settings(tctx, tctx->lp_ctx),
213 auth_context, &gensec_server_context);
214 torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
216 status = gensec_set_credentials(gensec_server_context, cmdline_credentials);
217 torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
219 status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
220 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
222 server_to_client = data_blob(NULL, 0);
224 do {
225 /* Do a client-server update dance */
226 status = gensec_update(gensec_client_context, tmp_ctx, tctx->ev, server_to_client, &client_to_server);
227 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
228 torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
231 status = gensec_update(gensec_server_context, tmp_ctx, tctx->ev, client_to_server, &server_to_client);
232 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
233 torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
236 if (NT_STATUS_IS_OK(status)) {
237 break;
239 } while (1);
241 /* Extract the PAC using Samba's code */
243 status = gensec_session_info(gensec_server_context, gensec_server_context, &session_info);
244 torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
246 pac_data = talloc_get_type(auth_context->private_data, struct pac_data);
248 torture_assert(tctx, pac_data != NULL, "gensec_update failed to fill in pac_data in auth_context");
249 torture_assert(tctx, pac_data->pac_blob.data != NULL, "pac_blob not present");
250 torture_decode_compare_pac(tctx, pac_data->pac_blob);
252 return true;
255 NTSTATUS torture_winbind_init(void)
257 struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "winbind");
259 torture_suite_add_suite(suite, torture_winbind_struct_init());
260 torture_suite_add_suite(suite, torture_wbclient());
261 torture_suite_add_simple_test(suite,
262 "pac", torture_winbind_pac);
264 suite->description = talloc_strdup(suite, "WINBIND tests");
266 torture_register_suite(suite);
268 return NT_STATUS_OK;