Finish removal of iconv_convenience in public API's.
[gd/samba-autobuild/.git] / source4 / torture / rpc / remote_pac.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test suite for netlogon PAC operations
5
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
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 "auth/auth.h"
24 #include "auth/gensec/gensec.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "torture/rpc/torture_rpc.h"
27 #include "libcli/auth/libcli_auth.h"
28 #include "librpc/gen_ndr/ndr_netlogon_c.h"
29 #include "param/param.h"
30
31 #define TEST_MACHINE_NAME "torturepactest"
32
33 /* Check to see if we can pass the PAC across to the NETLOGON server for validation */
34
35 /* Also happens to be a really good one-step verfication of our Kerberos stack */
36
37 static bool test_PACVerify(struct torture_context *tctx, 
38                            struct dcerpc_pipe *p,
39                            struct cli_credentials *credentials)
40 {
41         NTSTATUS status;
42
43         struct netr_LogonSamLogon r;
44
45         union netr_LogonLevel logon;
46         union netr_Validation validation;
47         uint8_t authoritative;
48         struct netr_Authenticator return_authenticator;
49
50         struct netr_GenericInfo generic;
51         struct netr_Authenticator auth, auth2;
52         
53
54         struct netlogon_creds_CredentialState *creds;
55         struct gensec_security *gensec_client_context;
56         struct gensec_security *gensec_server_context;
57
58         DATA_BLOB client_to_server, server_to_client, pac_wrapped, payload;
59         struct PAC_Validate pac_wrapped_struct;
60         
61         enum ndr_err_code ndr_err;
62
63         struct auth_session_info *session_info;
64
65         char *tmp_dir;
66         struct dcerpc_binding_handle *b = p->binding_handle;
67
68         TALLOC_CTX *tmp_ctx = talloc_new(tctx);
69         
70         torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
71
72         if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS, 
73                                     credentials, SEC_CHAN_BDC, 
74                                     &creds)) {
75                 return false;
76         }
77
78         status = torture_temp_dir(tctx, "PACVerify", &tmp_dir);
79         torture_assert_ntstatus_ok(tctx, status, "torture_temp_dir failed");
80
81         status = gensec_client_start(tctx, &gensec_client_context, tctx->ev, 
82                                      lp_gensec_settings(tctx, tctx->lp_ctx));
83         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
84
85         status = gensec_set_target_hostname(gensec_client_context, TEST_MACHINE_NAME);
86
87         status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
88         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
89
90         status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
91         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
92
93         status = gensec_server_start(tctx, tctx->ev, 
94                                      lp_gensec_settings(tctx, tctx->lp_ctx), 
95                                      NULL, &gensec_server_context);
96         torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
97
98         status = gensec_set_credentials(gensec_server_context, credentials);
99         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
100
101         status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
102         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
103
104         server_to_client = data_blob(NULL, 0);
105         
106         do {
107                 /* Do a client-server update dance */
108                 status = gensec_update(gensec_client_context, tmp_ctx, server_to_client, &client_to_server);
109                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
110                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
111                 }
112
113                 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
114                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
115                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
116                 }
117
118                 if (NT_STATUS_IS_OK(status)) {
119                         break;
120                 }
121         } while (1);
122
123         /* Extract the PAC using Samba's code */
124
125         status = gensec_session_info(gensec_server_context, &session_info);
126         torture_assert_ntstatus_ok(tctx, status, "gensec_session_info failed");
127         
128         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
129         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
130         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
131         pac_wrapped_struct.ChecksumAndSignature = payload
132                 = data_blob_talloc(tmp_ctx, NULL, 
133                                    pac_wrapped_struct.ChecksumLength
134                                    + pac_wrapped_struct.SignatureLength);
135         memcpy(&payload.data[0], 
136                session_info->server_info->pac_srv_sig.signature.data, 
137                pac_wrapped_struct.ChecksumLength);
138         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
139                session_info->server_info->pac_kdc_sig.signature.data, 
140                pac_wrapped_struct.SignatureLength);
141
142         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, &pac_wrapped_struct,
143                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
144         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
145                 
146         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
147         netlogon_creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
148
149         generic.length = pac_wrapped.length;
150         generic.data = pac_wrapped.data;
151
152         /* Validate it over the netlogon pipe */
153
154         generic.identity_info.parameter_control = 0;
155         generic.identity_info.logon_id_high = 0;
156         generic.identity_info.logon_id_low = 0;
157         generic.identity_info.domain_name.string = session_info->server_info->domain_name;
158         generic.identity_info.account_name.string = session_info->server_info->account_name;
159         generic.identity_info.workstation.string = TEST_MACHINE_NAME;
160
161         generic.package_name.string = "Kerberos";
162
163         logon.generic = &generic;
164
165         ZERO_STRUCT(auth2);
166         netlogon_creds_client_authenticator(creds, &auth);
167         r.in.credential = &auth;
168         r.in.return_authenticator = &auth2;
169         r.in.logon = &logon;
170         r.in.logon_level = NetlogonGenericInformation;
171         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
172         r.in.computer_name = cli_credentials_get_workstation(credentials);
173         r.in.validation_level = NetlogonValidationGenericInfo2;
174         r.out.validation = &validation;
175         r.out.authoritative = &authoritative;
176         r.out.return_authenticator = &return_authenticator;
177
178         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
179                 "LogonSamLogon failed");
180
181         torture_assert_ntstatus_ok(tctx, r.out.result, "LogonSamLogon failed");
182         
183         /* This will break the signature nicely (even in the crypto wrapping), check we get a logon failure */
184         generic.data[generic.length-1]++;
185
186         logon.generic = &generic;
187
188         ZERO_STRUCT(auth2);
189         netlogon_creds_client_authenticator(creds, &auth);
190         r.in.credential = &auth;
191         r.in.return_authenticator = &auth2;
192         r.in.logon_level = NetlogonGenericInformation;
193         r.in.logon = &logon;
194         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
195         r.in.computer_name = cli_credentials_get_workstation(credentials);
196         r.in.validation_level = NetlogonValidationGenericInfo2;
197
198         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
199                 "LogonSamLogon failed");
200
201         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
202         
203         torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred), 
204                        "Credential chaining failed");
205
206         /* This will break the parsing nicely (even in the crypto wrapping), check we get INVALID_PARAMETER */
207         generic.length--;
208
209         logon.generic = &generic;
210
211         ZERO_STRUCT(auth2);
212         netlogon_creds_client_authenticator(creds, &auth);
213         r.in.credential = &auth;
214         r.in.return_authenticator = &auth2;
215         r.in.logon_level = NetlogonGenericInformation;
216         r.in.logon = &logon;
217         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
218         r.in.computer_name = cli_credentials_get_workstation(credentials);
219         r.in.validation_level = NetlogonValidationGenericInfo2;
220
221         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
222                 "LogonSamLogon failed");
223
224         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
225         
226         torture_assert(tctx, netlogon_creds_client_check(creds, 
227                                                          &r.out.return_authenticator->cred), 
228                        "Credential chaining failed");
229
230         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
231         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
232         
233         /* Break the SignatureType */
234         pac_wrapped_struct.SignatureType++;
235
236         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
237         pac_wrapped_struct.ChecksumAndSignature = payload
238                 = data_blob_talloc(tmp_ctx, NULL, 
239                                    pac_wrapped_struct.ChecksumLength
240                                    + pac_wrapped_struct.SignatureLength);
241         memcpy(&payload.data[0], 
242                session_info->server_info->pac_srv_sig.signature.data, 
243                pac_wrapped_struct.ChecksumLength);
244         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
245                session_info->server_info->pac_kdc_sig.signature.data, 
246                pac_wrapped_struct.SignatureLength);
247         
248         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, &pac_wrapped_struct,
249                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
250         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
251         
252         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
253         netlogon_creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
254         
255         generic.length = pac_wrapped.length;
256         generic.data = pac_wrapped.data;
257
258         logon.generic = &generic;
259
260         ZERO_STRUCT(auth2);
261         netlogon_creds_client_authenticator(creds, &auth);
262         r.in.credential = &auth;
263         r.in.return_authenticator = &auth2;
264         r.in.logon_level = NetlogonGenericInformation;
265         r.in.logon = &logon;
266         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
267         r.in.computer_name = cli_credentials_get_workstation(credentials);
268         r.in.validation_level = NetlogonValidationGenericInfo2;
269         
270         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
271                 "LogonSamLogon failed");
272         
273         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_LOGON_FAILURE, "LogonSamLogon failed");
274         
275         torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred), 
276                        "Credential chaining failed");
277
278         pac_wrapped_struct.ChecksumLength = session_info->server_info->pac_srv_sig.signature.length;
279         pac_wrapped_struct.SignatureType = session_info->server_info->pac_kdc_sig.type;
280         pac_wrapped_struct.SignatureLength = session_info->server_info->pac_kdc_sig.signature.length;
281
282         pac_wrapped_struct.ChecksumAndSignature = payload
283                 = data_blob_talloc(tmp_ctx, NULL, 
284                                    pac_wrapped_struct.ChecksumLength
285                                    + pac_wrapped_struct.SignatureLength);
286         memcpy(&payload.data[0], 
287                session_info->server_info->pac_srv_sig.signature.data, 
288                pac_wrapped_struct.ChecksumLength);
289         memcpy(&payload.data[pac_wrapped_struct.ChecksumLength], 
290                session_info->server_info->pac_kdc_sig.signature.data, 
291                pac_wrapped_struct.SignatureLength);
292         
293         /* Break the signature length */
294         pac_wrapped_struct.SignatureLength++;
295
296         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, &pac_wrapped_struct,
297                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
298         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
299         
300         torture_assert(tctx, (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR), "not willing to even try a PACValidate without RC4 encryption");
301         netlogon_creds_arcfour_crypt(creds, pac_wrapped.data, pac_wrapped.length);
302         
303         generic.length = pac_wrapped.length;
304         generic.data = pac_wrapped.data;
305
306         logon.generic = &generic;
307
308         ZERO_STRUCT(auth2);
309         netlogon_creds_client_authenticator(creds, &auth);
310         r.in.credential = &auth;
311         r.in.return_authenticator = &auth2;
312         r.in.logon_level = NetlogonGenericInformation;
313         r.in.logon = &logon;
314         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
315         r.in.computer_name = cli_credentials_get_workstation(credentials);
316         r.in.validation_level = NetlogonValidationGenericInfo2;
317         
318         torture_assert_ntstatus_ok(tctx, dcerpc_netr_LogonSamLogon_r(b, tctx, &r),
319                 "LogonSamLogon failed");
320         
321         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_INVALID_PARAMETER, "LogonSamLogon failed");
322         
323         torture_assert(tctx, netlogon_creds_client_check(creds, &r.out.return_authenticator->cred), 
324                        "Credential chaining failed");
325         return true;
326 }
327
328 struct torture_suite *torture_rpc_remote_pac(TALLOC_CTX *mem_ctx)
329 {
330         struct torture_suite *suite = torture_suite_create(mem_ctx, "PAC");
331         struct torture_rpc_tcase *tcase;
332
333         tcase = torture_suite_add_machine_bdc_rpc_iface_tcase(suite, "netlogon",
334                                                   &ndr_table_netlogon, TEST_MACHINE_NAME);
335         torture_rpc_tcase_add_test_creds(tcase, "verify", test_PACVerify);
336
337         return suite;
338 }