a9e0bbca645ba873a529cd1beeed5f2d8aaaa0c5
[kai/samba.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 "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"
36
37 #include "system/kerberos.h"
38 #include "auth/kerberos/kerberos.h"
39 #include "librpc/gen_ndr/krb5pac.h"
40 #include <gssapi/gssapi.h>
41 #include <gssapi/gssapi_krb5.h>
42 #include "auth/gensec/gensec_gssapi.h"
43
44 #define TEST_MACHINE_NAME "torturepactest"
45
46 /* Check to see if we can pass the PAC across to the NETLOGON server for validation */
47
48 /* Also happens to be a really good one-step verfication of our Kerberos stack */
49
50 static bool test_PACVerify(struct torture_context *tctx, 
51                            struct dcerpc_pipe *p,
52                            struct cli_credentials *credentials)
53 {
54         NTSTATUS status;
55
56         struct netr_LogonSamLogon r;
57         
58         struct netr_GenericInfo generic;
59         struct netr_Authenticator auth, auth2;
60         
61
62         struct creds_CredentialState *creds;
63         struct gensec_security *gensec_client_context;
64         struct gensec_security *gensec_server_context;
65         struct gensec_gssapi_state *gensec_gssapi_state;
66
67         struct messaging_context *msg_server_ctx;
68         DATA_BLOB client_to_server, server_to_client, pac_blob, pac_wrapped;
69         gss_buffer_desc pac;
70         struct PAC_Validate pac_wrapped_struct;
71         
72         enum ndr_err_code ndr_err;
73
74         struct auth_session_info *session_info;
75
76         char *tmp_dir;
77         OM_uint32 maj_stat, min_stat;
78
79         TALLOC_CTX *tmp_ctx = talloc_new(tctx);
80         
81         torture_assert(tctx, tmp_ctx != NULL, "talloc_new() failed");
82
83         if (!test_SetupCredentials2(p, tctx, NETLOGON_NEG_AUTH2_ADS_FLAGS, 
84                                     credentials, SEC_CHAN_BDC, 
85                                     &creds)) {
86                 return false;
87         }
88
89         status = torture_temp_dir(tctx, "PACVerify", &tmp_dir);
90         torture_assert_ntstatus_ok(tctx, status, "torture_temp_dir failed");
91
92         msg_server_ctx = messaging_init(tctx, 
93                                         tmp_dir,
94                                         cluster_id(0, 1), 
95                                         lp_iconv_convenience(tctx->lp_ctx),
96                                         tctx->ev);
97         
98         torture_assert(tctx, msg_server_ctx != NULL, "Failed to init messaging context");
99
100         status = gensec_client_start(tctx, &gensec_client_context, tctx->ev, tctx->lp_ctx);
101         torture_assert_ntstatus_ok(tctx, status, "gensec_client_start (client) failed");
102
103         status = gensec_set_target_hostname(gensec_client_context, TEST_MACHINE_NAME);
104
105         status = gensec_set_credentials(gensec_client_context, cmdline_credentials);
106         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (client) failed");
107
108         status = gensec_start_mech_by_sasl_name(gensec_client_context, "GSSAPI");
109         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (client) failed");
110
111         status = gensec_server_start(tctx, tctx->ev, tctx->lp_ctx, msg_server_ctx, &gensec_server_context);
112         torture_assert_ntstatus_ok(tctx, status, "gensec_server_start (server) failed");
113
114         status = gensec_set_credentials(gensec_server_context, credentials);
115         torture_assert_ntstatus_ok(tctx, status, "gensec_set_credentials (server) failed");
116
117         status = gensec_start_mech_by_sasl_name(gensec_server_context, "GSSAPI");
118         torture_assert_ntstatus_ok(tctx, status, "gensec_start_mech_by_sasl_name (server) failed");
119
120         server_to_client = data_blob(NULL, 0);
121         
122         do {
123                 /* Do a client-server update dance */
124                 status = gensec_update(gensec_client_context, tmp_ctx, server_to_client, &client_to_server);
125                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
126                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (client) failed");
127                 }
128
129                 if (client_to_server.length == 0) {
130                         break;
131                 }
132
133                 status = gensec_update(gensec_server_context, tmp_ctx, client_to_server, &server_to_client);
134                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {;
135                         torture_assert_ntstatus_ok(tctx, status, "gensec_update (server) failed");
136                 }
137
138                 if (server_to_client.length == 0) {
139                         break;
140                 }
141         } while (1);
142
143         /* Extract the PAC using Samba's code */
144
145         status = gensec_session_info(gensec_server_context, &session_info);
146         torture_assert_ntstatus_ok(tctx, status, "gensec_gssapi_session_info failed");
147         
148         /* Extract a raw PAC */
149
150         gensec_gssapi_state = talloc_get_type(gensec_server_context->private_data, struct gensec_gssapi_state);
151
152         maj_stat = gsskrb5_extract_authz_data_from_sec_context(&min_stat, 
153                                                                gensec_gssapi_state->gssapi_context, 
154                                                                KRB5_AUTHDATA_WIN2K_PAC,
155                                                                &pac);
156         
157         if (maj_stat == 0) {
158                 pac_blob = data_blob_talloc(tmp_ctx, pac.value, pac.length);
159                 gss_release_buffer(&min_stat, &pac);
160
161         } else {
162                 pac_blob = data_blob(NULL, 0);
163         }
164
165         pac_wrapped_struct.MessageType = 0x3;
166         pac_wrapped_struct.ChecksumLength = pac_blob.length;
167         pac_wrapped_struct.SignatureType = 0;
168         pac_wrapped_struct.SignatureLength = 0;
169         pac_wrapped_struct.data = pac_blob.data;
170
171         ndr_err = ndr_push_struct_blob(&pac_wrapped, tmp_ctx, lp_iconv_convenience(tctx->lp_ctx), &pac_wrapped_struct,
172                                        (ndr_push_flags_fn_t)ndr_push_PAC_Validate);
173         torture_assert(tctx, NDR_ERR_CODE_IS_SUCCESS(ndr_err), "ndr_push_struct_blob of PACValidate structure failed");
174                 
175         
176         /* Validate it over the netlogon pipe */
177
178         generic.identity_info.parameter_control = 0;
179         generic.identity_info.logon_id_high = 0;
180         generic.identity_info.logon_id_low = 0;
181         generic.identity_info.domain_name.string = session_info->server_info->domain_name;
182         generic.identity_info.account_name.string = session_info->server_info->account_name;
183         generic.identity_info.workstation.string = TEST_MACHINE_NAME;
184
185         generic.package_name.string = "Kerberos";
186         generic.length = pac_wrapped.length;
187         generic.data = pac_wrapped.data;
188
189         ZERO_STRUCT(auth2);
190         creds_client_authenticator(creds, &auth);
191         r.in.credential = &auth;
192         r.in.return_authenticator = &auth2;
193         r.in.logon_level = NetlogonGenericInformation;
194         r.in.logon.generic = &generic;
195         r.in.server_name = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
196         r.in.computer_name = cli_credentials_get_workstation(credentials);
197         r.in.validation_level = NetlogonValidationGenericInfo2;
198
199         status = dcerpc_netr_LogonSamLogon(p, tctx, &r);
200
201         torture_assert_ntstatus_ok(tctx, status, "LogonSamLogon failed");
202         
203         torture_assert(tctx, creds_client_check(creds, &r.out.return_authenticator->cred), 
204                        "Credential chaining failed");
205
206         return true;
207 }
208
209 struct torture_suite *torture_rpc_remote_pac(TALLOC_CTX *mem_ctx)
210 {
211         struct torture_suite *suite = torture_suite_create(mem_ctx, "PAC");
212         struct torture_rpc_tcase *tcase;
213         struct torture_test *test;
214
215         tcase = torture_suite_add_machine_rpc_iface_tcase(suite, "netlogon", 
216                                                   &ndr_table_netlogon, TEST_MACHINE_NAME);
217         torture_rpc_tcase_add_test_creds(tcase, "verify", test_PACVerify);
218
219         return suite;
220 }