selftest/Samba3.pm: use "winbind use krb5 enterprise principals = yes" for ad_member
[gd/samba-autobuild/.git] / source4 / torture / winbind / winbind.c
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
7
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.
12
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.
17
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/>.
20 */
21
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"
35
36 struct pac_data {
37         DATA_BLOB pac_blob;
38 };
39
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)
51 {
52         NTSTATUS nt_status;
53         struct auth_user_info_dc *user_info_dc;
54         TALLOC_CTX *tmp_ctx;
55         struct pac_data *pac_data;
56
57         tmp_ctx = talloc_named(mem_ctx, 0, "gensec_gssapi_session_info context");
58         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
59
60         auth_ctx->private_data = pac_data = talloc_zero(auth_ctx, struct pac_data); 
61
62         pac_data->pac_blob = *pac_blob;
63
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;
73         }
74
75         if (user_info_dc->info->authenticated) {
76                 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
77         }
78
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;
88         }
89
90         talloc_free(tmp_ctx);
91         return nt_status;
92 }
93
94 static bool torture_decode_compare_pac(struct torture_context *tctx,
95                                        DATA_BLOB pac)
96 {
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 sid_idx, i;
106         char sid_str[50];
107
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;
113
114         wbc_err = wbcAuthenticateUserEx(&params, &info, &error);
115         torture_assert(tctx, WBC_ERROR_IS_OK(wbc_err), wbcErrorString(wbc_err));
116
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;
123
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");
139
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);
146
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);
153
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);
163         }
164
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);
173         }
174
175         return true;
176 }
177
178 static bool torture_winbind_pac(struct torture_context *tctx,
179                                 const char *sasl_mech,
180                                 const char *mech)
181 {
182         NTSTATUS status;
183
184         struct gensec_security *gensec_client_context;
185         struct gensec_security *gensec_server_context;
186         struct cli_credentials *machine_credentials;
187
188         DATA_BLOB client_to_server, server_to_client;   
189
190         struct auth4_context *auth_context;
191         struct auth_session_info *session_info;
192         struct pac_data *pac_data;
193
194         TALLOC_CTX *tmp_ctx = talloc_new(tctx);
195         torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
196
197         machine_credentials = cli_credentials_init(tmp_ctx);
198         torture_assert(tctx, machine_credentials != NULL, "cli_credentials_init() failed");
199
200         cli_credentials_set_conf(machine_credentials, tctx->lp_ctx);
201
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");
204         
205         auth_context = talloc_zero(tmp_ctx, struct auth4_context);
206         torture_assert(tctx, auth_context != NULL, "talloc_new() failed");
207
208         auth_context->generate_session_info_pac = test_generate_session_info_pac;
209
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");
213
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");
216
217         status = gensec_set_credentials(gensec_client_context,
218                         popt_get_cmdline_credentials());
219         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
220
221         if (sasl_mech) {
222                 status = gensec_start_mech_by_sasl_name(gensec_client_context, sasl_mech);
223                 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
224         } else {
225                 status = gensec_start_mech_by_name(gensec_client_context, mech);
226                 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_name (client) failed");
227         }
228
229
230         status = gensec_server_start(tctx,
231                                      lpcfg_gensec_settings(tctx, tctx->lp_ctx),
232                                      auth_context, &gensec_server_context);
233         torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
234
235         status = gensec_set_credentials(gensec_server_context, machine_credentials);
236         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
237
238         if (sasl_mech) {
239                 status = gensec_start_mech_by_sasl_name(gensec_server_context, sasl_mech);
240                 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
241         } else {
242                 status = gensec_start_mech_by_name(gensec_server_context, mech);
243                 torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_name (server) failed");
244         }
245
246         server_to_client = data_blob(NULL, 0);
247         
248         do {
249                 /* Do a client-server update dance */
250                 status = gensec_update(gensec_client_context, tmp_ctx, server_to_client, &client_to_server);
251                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
252                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
253                 }
254
255                 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
256                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
257                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
258                 }
259
260                 if (NT_STATUS_IS_OK(status)) {
261                         break;
262                 }
263         } while (1);
264
265         /* Extract the PAC using Samba's code */
266
267         status = gensec_session_info(gensec_server_context, gensec_server_context, &session_info);
268         torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
269
270         pac_data = talloc_get_type(auth_context->private_data, struct pac_data);
271
272         torture_assert(tctx, pac_data != NULL, "gensec_update failed to fill in pac_data in auth_context");
273         torture_assert(tctx, pac_data->pac_blob.data != NULL, "pac_blob not present");
274         torture_decode_compare_pac(tctx, pac_data->pac_blob);
275
276         return true;
277 }
278
279 static bool torture_winbind_pac_gssapi(struct torture_context *tctx)
280 {
281         return torture_winbind_pac(tctx, "GSSAPI", NULL);
282 }       
283
284 static bool torture_winbind_pac_gss_spnego(struct torture_context *tctx)
285 {
286         return torture_winbind_pac(tctx, "GSS-SPNEGO", NULL);
287 }       
288
289 static bool torture_winbind_pac_krb5(struct torture_context *tctx)
290 {
291         return torture_winbind_pac(tctx, NULL, "krb5");
292 }       
293
294 NTSTATUS torture_winbind_init(TALLOC_CTX *ctx)
295 {
296         struct torture_suite *suite = torture_suite_create(ctx, "winbind");
297         struct torture_suite *pac_suite;
298         torture_suite_add_suite(suite, torture_winbind_struct_init(suite));
299         torture_suite_add_suite(suite, torture_wbclient(suite));
300
301         pac_suite = torture_suite_create(ctx, "pac");
302         torture_suite_add_simple_test(pac_suite,
303                                       "GSSAPI", torture_winbind_pac_gssapi);
304         torture_suite_add_simple_test(pac_suite,
305                                       "GSS-SPNEGO", torture_winbind_pac_gss_spnego);
306         torture_suite_add_simple_test(pac_suite,
307                                       "krb5", torture_winbind_pac_krb5);
308
309         pac_suite->description = talloc_strdup(suite, "Winbind Kerberos PAC tests");
310
311         torture_suite_add_suite(suite, pac_suite);
312
313         suite->description = talloc_strdup(suite, "WINBIND tests");
314
315         torture_register_suite(ctx, suite);
316
317         return NT_STATUS_OK;
318 }