c41c3fb2bccaf1f63b76e8c7b94668d695d354fa
[samba.git] / source4 / libcli / auth / gensec_gssapi.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Kerberos backend for GENSEC
5    
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004
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 2 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    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "system/kerberos.h"
26 #include "system/time.h"
27 #include "libcli/auth/kerberos.h"
28 #include "auth/auth.h"
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_AUTH
32
33 struct gensec_gssapi_state {
34         gss_ctx_id_t gssapi_context;
35         struct gss_channel_bindings_struct *input_chan_bindings;
36         gss_name_t server_name;
37         gss_name_t client_name;
38         int want_flags, got_flags;
39 };
40 static int gensec_gssapi_destory(void *ptr) 
41 {
42         struct gensec_gssapi_state *gensec_gssapi_state = ptr;
43         OM_uint32 maj_stat, min_stat;
44
45         if (gensec_gssapi_state->gssapi_context != GSS_C_NO_CONTEXT) {
46                 maj_stat = gss_delete_sec_context (&min_stat,
47                                                    &gensec_gssapi_state->gssapi_context,
48                                                    GSS_C_NO_BUFFER);
49         }
50
51         if (gensec_gssapi_state->server_name != GSS_C_NO_NAME) {
52                 maj_stat = gss_release_name(&min_stat, &gensec_gssapi_state->server_name);
53         }
54         if (gensec_gssapi_state->client_name != GSS_C_NO_NAME) {
55                 maj_stat = gss_release_name(&min_stat, &gensec_gssapi_state->client_name);
56         }
57         return 0;
58 }
59
60 static NTSTATUS gensec_gssapi_start(struct gensec_security *gensec_security)
61 {
62         struct gensec_gssapi_state *gensec_gssapi_state;
63
64         gensec_gssapi_state = talloc_p(gensec_security, struct gensec_gssapi_state);
65         if (!gensec_gssapi_state) {
66                 return NT_STATUS_NO_MEMORY;
67         }
68
69         gensec_security->private_data = gensec_gssapi_state;
70
71         gensec_gssapi_state->gssapi_context = GSS_C_NO_CONTEXT;
72         gensec_gssapi_state->server_name = GSS_C_NO_NAME;
73         gensec_gssapi_state->client_name = GSS_C_NO_NAME;
74
75         talloc_set_destructor(gensec_gssapi_state, gensec_gssapi_destory); 
76
77         /* TODO: Fill in channel bindings */
78         gensec_gssapi_state->input_chan_bindings = GSS_C_NO_CHANNEL_BINDINGS;
79         
80         gensec_gssapi_state->want_flags = 0;
81         gensec_gssapi_state->got_flags = 0;
82
83         if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
84                 /* GSSAPI won't give us the session keys */
85                 return NT_STATUS_INVALID_PARAMETER;
86         }
87         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
88                 gensec_gssapi_state->want_flags |= GSS_C_INTEG_FLAG;
89         }
90         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
91                 gensec_gssapi_state->want_flags |= GSS_C_CONF_FLAG;
92         }
93
94         return NT_STATUS_OK;
95 }
96
97 static NTSTATUS gensec_gssapi_server_start(struct gensec_security *gensec_security)
98 {
99         NTSTATUS nt_status;
100         struct gensec_gssapi_state *gensec_gssapi_state;
101
102         nt_status = gensec_gssapi_start(gensec_security);
103         if (!NT_STATUS_IS_OK(nt_status)) {
104                 return nt_status;
105         }
106
107         gensec_gssapi_state = gensec_security->private_data;
108
109         return NT_STATUS_OK;
110 }
111
112 static NTSTATUS gensec_gssapi_client_start(struct gensec_security *gensec_security)
113 {
114         struct gensec_gssapi_state *gensec_gssapi_state;
115         NTSTATUS nt_status;
116         gss_buffer_desc name_token;
117         OM_uint32 maj_stat, min_stat;
118
119         nt_status = gensec_gssapi_start(gensec_security);
120         if (!NT_STATUS_IS_OK(nt_status)) {
121                 return nt_status;
122         }
123
124         gensec_gssapi_state = gensec_security->private_data;
125
126         name_token.value = talloc_asprintf(gensec_gssapi_state, "%s@%s", gensec_get_target_service(gensec_security), 
127                                            gensec_get_target_hostname(gensec_security));
128         DEBUG(0, ("name: %s\n", (char *)name_token.value));
129         name_token.length = strlen(name_token.value);
130
131         maj_stat = gss_import_name (&min_stat,
132                                     &name_token,
133                                     GSS_C_NT_HOSTBASED_SERVICE,
134                                     &gensec_gssapi_state->server_name);
135
136
137         if (maj_stat) {
138                 return NT_STATUS_UNSUCCESSFUL;
139         }
140         return NT_STATUS_OK;
141 }
142
143
144
145 /**
146  * Next state function for the GSSAPI GENSEC mechanism
147  * 
148  * @param gensec_gssapi_state GSSAPI State
149  * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
150  * @param in The request, as a DATA_BLOB
151  * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
152  * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent, 
153  *                or NT_STATUS_OK if the user is authenticated. 
154  */
155
156 static NTSTATUS gensec_gssapi_update(struct gensec_security *gensec_security, 
157                                    TALLOC_CTX *out_mem_ctx, 
158                                    const DATA_BLOB in, DATA_BLOB *out) 
159 {
160         struct gensec_gssapi_state *gensec_gssapi_state = gensec_security->private_data;
161         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
162         OM_uint32 maj_stat, min_stat;
163         OM_uint32 min_stat2;
164         gss_buffer_desc input_token, output_token;
165
166         input_token.length = in.length;
167         input_token.value = in.data;
168
169         switch (gensec_security->gensec_role) {
170         case GENSEC_CLIENT:
171         {
172                 maj_stat = gss_init_sec_context(&min_stat, 
173                                                 GSS_C_NO_CREDENTIAL, 
174                                                 &gensec_gssapi_state->gssapi_context, 
175                                                 gensec_gssapi_state->server_name, 
176                                                 GSS_C_NO_OID, 
177                                                 gensec_gssapi_state->want_flags, 
178                                                 0, 
179                                                 gensec_gssapi_state->input_chan_bindings,
180                                                 &input_token, 
181                                                 NULL, 
182                                                 &output_token, 
183                                                 &gensec_gssapi_state->got_flags, /* ret flags */
184                                                 NULL);
185                 break;
186         }
187         case GENSEC_SERVER:
188         {
189                 maj_stat = gss_accept_sec_context(&min_stat, 
190                                                   gensec_gssapi_state->gssapi_context, 
191                                                   GSS_C_NO_CREDENTIAL, 
192                                                   &input_token, 
193                                                   gensec_gssapi_state->input_chan_bindings,
194                                                   &gensec_gssapi_state->client_name, 
195                                                   NULL /* mech oid */,
196                                                   &output_token, 
197                                                   &gensec_gssapi_state->got_flags, 
198                                                   NULL, 
199                                                   NULL);
200                 break;
201         }
202         default:
203                 return NT_STATUS_INVALID_PARAMETER;
204                 
205         }
206
207         *out = data_blob_talloc(out_mem_ctx, output_token.value, output_token.length);
208         gss_release_buffer(&min_stat2, &output_token);
209
210         if (maj_stat == GSS_S_COMPLETE) {
211                 return NT_STATUS_OK;
212         } else if (maj_stat == GSS_S_CONTINUE_NEEDED) {
213                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
214         } else {
215                 gss_buffer_desc msg1, msg2;
216                 OM_uint32 msg_ctx = 0;
217                 
218                 msg1.value = NULL;
219                 msg2.value = NULL;
220                 gss_display_status(&min_stat2, maj_stat, GSS_C_GSS_CODE,
221                                    GSS_C_NULL_OID, &msg_ctx, &msg1);
222                 gss_display_status(&min_stat2, min_stat, GSS_C_MECH_CODE,
223                                    GSS_C_NULL_OID, &msg_ctx, &msg2);
224                 DEBUG(1, ("gensec_gssapi_update: %s : %s\n", (char *)msg1.value, (char *)msg2.value));
225                 gss_release_buffer(&min_stat2, &msg1);
226                 gss_release_buffer(&min_stat2, &msg2);
227
228                 return nt_status;
229         }
230         
231 }
232
233 static NTSTATUS gensec_gssapi_wrap(struct gensec_security *gensec_security, 
234                                    TALLOC_CTX *mem_ctx, 
235                                    const DATA_BLOB *in, 
236                                    DATA_BLOB *out)
237 {
238         struct gensec_gssapi_state *gensec_gssapi_state = gensec_security->private_data;
239         OM_uint32 maj_stat, min_stat;
240         gss_buffer_desc input_token, output_token;
241         int conf_state;
242         input_token.length = in->length;
243         input_token.value = in->data;
244         
245         maj_stat = gss_wrap(&min_stat, 
246                             gensec_gssapi_state->gssapi_context, 
247                             gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL),
248                             GSS_C_QOP_DEFAULT,
249                             &input_token,
250                             &conf_state,
251                             &output_token);
252         if (GSS_ERROR(maj_stat)) {
253                 return NT_STATUS_ACCESS_DENIED;
254         }
255         *out = data_blob_talloc(mem_ctx, output_token.value, output_token.length);
256
257         gss_release_buffer(&min_stat, &output_token);
258
259         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)
260             && !conf_state) {
261                 return NT_STATUS_ACCESS_DENIED;
262         }
263         return NT_STATUS_OK;
264 }
265
266 static NTSTATUS gensec_gssapi_unwrap(struct gensec_security *gensec_security, 
267                                      TALLOC_CTX *mem_ctx, 
268                                      const DATA_BLOB *in, 
269                                      DATA_BLOB *out)
270 {
271         struct gensec_gssapi_state *gensec_gssapi_state = gensec_security->private_data;
272         OM_uint32 maj_stat, min_stat;
273         gss_buffer_desc input_token, output_token;
274         int conf_state;
275         gss_qop_t qop_state;
276         input_token.length = in->length;
277         input_token.value = in->data;
278         
279         maj_stat = gss_unwrap(&min_stat, 
280                               gensec_gssapi_state->gssapi_context, 
281                               &input_token,
282                               &output_token, 
283                               &conf_state,
284                               &qop_state);
285         if (GSS_ERROR(maj_stat)) {
286                 return NT_STATUS_ACCESS_DENIED;
287         }
288         *out = data_blob_talloc(mem_ctx, output_token.value, output_token.length);
289
290         gss_release_buffer(&min_stat, &output_token);
291         
292         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)
293             && !conf_state) {
294                 return NT_STATUS_ACCESS_DENIED;
295         }
296         return NT_STATUS_OK;
297 }
298
299 static BOOL gensec_gssapi_have_feature(struct gensec_security *gensec_security, 
300                                        uint32 feature) 
301 {
302         struct gensec_gssapi_state *gensec_gssapi_state = gensec_security->private_data;
303         if (feature & GENSEC_FEATURE_SIGN) {
304                 return gensec_gssapi_state->got_flags & GSS_C_INTEG_FLAG;
305         }
306         if (feature & GENSEC_FEATURE_SEAL) {
307                 return gensec_gssapi_state->got_flags & GSS_C_CONF_FLAG;
308         }
309         return False;
310 }
311
312 static const struct gensec_security_ops gensec_gssapi_security_ops = {
313         .name           = "gssapi",
314         .oid            = GENSEC_OID_KERBEROS5,
315         .client_start   = gensec_gssapi_client_start,
316         .server_start   = gensec_gssapi_server_start,
317         .update         = gensec_gssapi_update,
318         .wrap           = gensec_gssapi_wrap,
319         .unwrap         = gensec_gssapi_unwrap,
320         .have_feature   = gensec_gssapi_have_feature
321
322 };
323
324 NTSTATUS gensec_gssapi_init(void)
325 {
326         NTSTATUS ret;
327
328         ret = gensec_register(&gensec_gssapi_security_ops);
329         if (!NT_STATUS_IS_OK(ret)) {
330                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
331                         gensec_gssapi_security_ops.name));
332                 return ret;
333         }
334
335         return ret;
336 }