Finish removal of iconv_convenience in public API's.
[bbaumbach/samba-autobuild/.git] / source4 / auth / gensec / gensec_krb5.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Kerberos backend for GENSEC
5    
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004
7    Copyright (C) Andrew Tridgell 2001
8    Copyright (C) Luke Howard 2002-2003
9    Copyright (C) Stefan Metzmacher 2004-2005
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "includes.h"
27 #include "system/kerberos.h"
28 #include "auth/kerberos/kerberos.h"
29 #include "auth/auth.h"
30 #include "lib/socket/socket.h"
31 #include "lib/tsocket/tsocket.h"
32 #include "librpc/rpc/dcerpc.h"
33 #include "auth/credentials/credentials.h"
34 #include "auth/credentials/credentials_krb5.h"
35 #include "auth/kerberos/kerberos_credentials.h"
36 #include "auth/gensec/gensec.h"
37 #include "auth/gensec/gensec_proto.h"
38 #include "param/param.h"
39 #include "auth/auth_sam_reply.h"
40 #include "lib/util/util_net.h"
41
42 enum GENSEC_KRB5_STATE {
43         GENSEC_KRB5_SERVER_START,
44         GENSEC_KRB5_CLIENT_START,
45         GENSEC_KRB5_CLIENT_MUTUAL_AUTH,
46         GENSEC_KRB5_DONE
47 };
48
49 struct gensec_krb5_state {
50         DATA_BLOB session_key;
51         DATA_BLOB pac;
52         enum GENSEC_KRB5_STATE state_position;
53         struct smb_krb5_context *smb_krb5_context;
54         krb5_auth_context auth_context;
55         krb5_data enc_ticket;
56         krb5_keyblock *keyblock;
57         krb5_ticket *ticket;
58         bool gssapi;
59         krb5_flags ap_req_options;
60 };
61
62 static int gensec_krb5_destroy(struct gensec_krb5_state *gensec_krb5_state)
63 {
64         if (!gensec_krb5_state->smb_krb5_context) {
65                 /* We can't clean anything else up unless we started up this far */
66                 return 0;
67         }
68         if (gensec_krb5_state->enc_ticket.length) { 
69                 kerberos_free_data_contents(gensec_krb5_state->smb_krb5_context->krb5_context, 
70                                             &gensec_krb5_state->enc_ticket); 
71         }
72
73         if (gensec_krb5_state->ticket) {
74                 krb5_free_ticket(gensec_krb5_state->smb_krb5_context->krb5_context, 
75                                  gensec_krb5_state->ticket);
76         }
77
78         /* ccache freed in a child destructor */
79
80         krb5_free_keyblock(gensec_krb5_state->smb_krb5_context->krb5_context, 
81                            gensec_krb5_state->keyblock);
82                 
83         if (gensec_krb5_state->auth_context) {
84                 krb5_auth_con_free(gensec_krb5_state->smb_krb5_context->krb5_context, 
85                                    gensec_krb5_state->auth_context);
86         }
87
88         return 0;
89 }
90
91 static NTSTATUS gensec_krb5_start(struct gensec_security *gensec_security, bool gssapi)
92 {
93         krb5_error_code ret;
94         struct gensec_krb5_state *gensec_krb5_state;
95         struct cli_credentials *creds;
96         const struct tsocket_address *tlocal_addr, *tremote_addr;
97         krb5_address my_krb5_addr, peer_krb5_addr;
98         
99         creds = gensec_get_credentials(gensec_security);
100         if (!creds) {
101                 return NT_STATUS_INVALID_PARAMETER;
102         }
103
104         gensec_krb5_state = talloc(gensec_security, struct gensec_krb5_state);
105         if (!gensec_krb5_state) {
106                 return NT_STATUS_NO_MEMORY;
107         }
108
109         gensec_security->private_data = gensec_krb5_state;
110         gensec_krb5_state->smb_krb5_context = NULL;
111         gensec_krb5_state->auth_context = NULL;
112         gensec_krb5_state->ticket = NULL;
113         ZERO_STRUCT(gensec_krb5_state->enc_ticket);
114         gensec_krb5_state->keyblock = NULL;
115         gensec_krb5_state->session_key = data_blob(NULL, 0);
116         gensec_krb5_state->pac = data_blob(NULL, 0);
117         gensec_krb5_state->gssapi = gssapi;
118
119         talloc_set_destructor(gensec_krb5_state, gensec_krb5_destroy); 
120
121         if (cli_credentials_get_krb5_context(creds, 
122                                              gensec_security->event_ctx, 
123                                              gensec_security->settings->lp_ctx, &gensec_krb5_state->smb_krb5_context)) {
124                 talloc_free(gensec_krb5_state);
125                 return NT_STATUS_INTERNAL_ERROR;
126         }
127
128         ret = krb5_auth_con_init(gensec_krb5_state->smb_krb5_context->krb5_context, &gensec_krb5_state->auth_context);
129         if (ret) {
130                 DEBUG(1,("gensec_krb5_start: krb5_auth_con_init failed (%s)\n", 
131                          smb_get_krb5_error_message(gensec_krb5_state->smb_krb5_context->krb5_context, 
132                                                     ret, gensec_krb5_state)));
133                 talloc_free(gensec_krb5_state);
134                 return NT_STATUS_INTERNAL_ERROR;
135         }
136
137         ret = krb5_auth_con_setflags(gensec_krb5_state->smb_krb5_context->krb5_context, 
138                                      gensec_krb5_state->auth_context,
139                                      KRB5_AUTH_CONTEXT_DO_SEQUENCE);
140         if (ret) {
141                 DEBUG(1,("gensec_krb5_start: krb5_auth_con_setflags failed (%s)\n", 
142                          smb_get_krb5_error_message(gensec_krb5_state->smb_krb5_context->krb5_context, 
143                                                     ret, gensec_krb5_state)));
144                 talloc_free(gensec_krb5_state);
145                 return NT_STATUS_INTERNAL_ERROR;
146         }
147
148         tlocal_addr = gensec_get_local_address(gensec_security);
149         if (tlocal_addr) {
150                 ssize_t socklen;
151                 struct sockaddr_storage ss;
152
153                 socklen = tsocket_address_bsd_sockaddr(tlocal_addr,
154                                 (struct sockaddr *) &ss,
155                                 sizeof(struct sockaddr_storage));
156                 if (socklen < 0) {
157                         return NT_STATUS_INTERNAL_ERROR;
158                 }
159                 ret = krb5_sockaddr2address(gensec_krb5_state->smb_krb5_context->krb5_context,
160                                 (const struct sockaddr *) &ss, &my_krb5_addr);
161                 if (ret) {
162                         DEBUG(1,("gensec_krb5_start: krb5_sockaddr2address (local) failed (%s)\n", 
163                                  smb_get_krb5_error_message(gensec_krb5_state->smb_krb5_context->krb5_context, 
164                                                             ret, gensec_krb5_state)));
165                         talloc_free(gensec_krb5_state);
166                         return NT_STATUS_INTERNAL_ERROR;
167                 }
168         }
169
170         tremote_addr = gensec_get_remote_address(gensec_security);
171         if (tremote_addr) {
172                 ssize_t socklen;
173                 struct sockaddr_storage ss;
174
175                 socklen = tsocket_address_bsd_sockaddr(tremote_addr,
176                                 (struct sockaddr *) &ss,
177                                 sizeof(struct sockaddr_storage));
178                 if (socklen < 0) {
179                         return NT_STATUS_INTERNAL_ERROR;
180                 }
181                 ret = krb5_sockaddr2address(gensec_krb5_state->smb_krb5_context->krb5_context,
182                                 (const struct sockaddr *) &ss, &peer_krb5_addr);
183                 if (ret) {
184                         DEBUG(1,("gensec_krb5_start: krb5_sockaddr2address (local) failed (%s)\n", 
185                                  smb_get_krb5_error_message(gensec_krb5_state->smb_krb5_context->krb5_context, 
186                                                             ret, gensec_krb5_state)));
187                         talloc_free(gensec_krb5_state);
188                         return NT_STATUS_INTERNAL_ERROR;
189                 }
190         }
191
192         ret = krb5_auth_con_setaddrs(gensec_krb5_state->smb_krb5_context->krb5_context, 
193                                      gensec_krb5_state->auth_context,
194                                      tlocal_addr ? &my_krb5_addr : NULL,
195                                      tremote_addr ? &peer_krb5_addr : NULL);
196         if (ret) {
197                 DEBUG(1,("gensec_krb5_start: krb5_auth_con_setaddrs failed (%s)\n", 
198                          smb_get_krb5_error_message(gensec_krb5_state->smb_krb5_context->krb5_context, 
199                                                     ret, gensec_krb5_state)));
200                 talloc_free(gensec_krb5_state);
201                 return NT_STATUS_INTERNAL_ERROR;
202         }
203
204         return NT_STATUS_OK;
205 }
206
207 static NTSTATUS gensec_krb5_common_server_start(struct gensec_security *gensec_security, bool gssapi)
208 {
209         NTSTATUS nt_status;
210         struct gensec_krb5_state *gensec_krb5_state;
211
212         nt_status = gensec_krb5_start(gensec_security, gssapi);
213         if (!NT_STATUS_IS_OK(nt_status)) {
214                 return nt_status;
215         }
216         
217         gensec_krb5_state = (struct gensec_krb5_state *)gensec_security->private_data;
218         gensec_krb5_state->state_position = GENSEC_KRB5_SERVER_START;
219
220         return NT_STATUS_OK;
221 }
222
223 static NTSTATUS gensec_krb5_server_start(struct gensec_security *gensec_security)
224 {
225         return gensec_krb5_common_server_start(gensec_security, false);
226 }
227
228 static NTSTATUS gensec_fake_gssapi_krb5_server_start(struct gensec_security *gensec_security)
229 {
230         return gensec_krb5_common_server_start(gensec_security, true);
231 }
232
233 static NTSTATUS gensec_krb5_common_client_start(struct gensec_security *gensec_security, bool gssapi)
234 {
235         struct gensec_krb5_state *gensec_krb5_state;
236         krb5_error_code ret;
237         NTSTATUS nt_status;
238         struct ccache_container *ccache_container;
239         const char *hostname;
240         const char *error_string;
241         const char *principal;
242         krb5_data in_data;
243
244         hostname = gensec_get_target_hostname(gensec_security);
245         if (!hostname) {
246                 DEBUG(1, ("Could not determine hostname for target computer, cannot use kerberos\n"));
247                 return NT_STATUS_INVALID_PARAMETER;
248         }
249         if (is_ipaddress(hostname)) {
250                 DEBUG(2, ("Cannot do krb5 to an IP address"));
251                 return NT_STATUS_INVALID_PARAMETER;
252         }
253         if (strcmp(hostname, "localhost") == 0) {
254                 DEBUG(2, ("krb5 to 'localhost' does not make sense"));
255                 return NT_STATUS_INVALID_PARAMETER;
256         }
257                         
258         nt_status = gensec_krb5_start(gensec_security, gssapi);
259         if (!NT_STATUS_IS_OK(nt_status)) {
260                 return nt_status;
261         }
262
263         gensec_krb5_state = (struct gensec_krb5_state *)gensec_security->private_data;
264         gensec_krb5_state->state_position = GENSEC_KRB5_CLIENT_START;
265         gensec_krb5_state->ap_req_options = AP_OPTS_USE_SUBKEY;
266
267         if (gensec_krb5_state->gssapi) {
268                 /* The Fake GSSAPI modal emulates Samba3, which does not do mutual authentication */
269                 if (gensec_setting_bool(gensec_security->settings, "gensec_fake_gssapi_krb5", "mutual", false)) {
270                         gensec_krb5_state->ap_req_options |= AP_OPTS_MUTUAL_REQUIRED;
271                 }
272         } else {
273                 /* The wrapping for KPASSWD (a user of the raw KRB5 API) should be mutually authenticated */
274                 if (gensec_setting_bool(gensec_security->settings, "gensec_krb5", "mutual", true)) {
275                         gensec_krb5_state->ap_req_options |= AP_OPTS_MUTUAL_REQUIRED;
276                 }
277         }
278
279         principal = gensec_get_target_principal(gensec_security);
280
281         ret = cli_credentials_get_ccache(gensec_get_credentials(gensec_security), 
282                                          gensec_security->event_ctx, 
283                                          gensec_security->settings->lp_ctx, &ccache_container, &error_string);
284         switch (ret) {
285         case 0:
286                 break;
287         case KRB5KDC_ERR_PREAUTH_FAILED:
288                 return NT_STATUS_LOGON_FAILURE;
289         case KRB5_KDC_UNREACH:
290                 DEBUG(3, ("Cannot reach a KDC we require to contact %s: %s\n", principal, error_string));
291                 return NT_STATUS_INVALID_PARAMETER; /* Make SPNEGO ignore us, we can't go any further here */
292         case KRB5_CC_NOTFOUND:
293         case KRB5_CC_END:
294                 DEBUG(3, ("Error preparing credentials we require to contact %s : %s\n", principal, error_string));
295                 return NT_STATUS_INVALID_PARAMETER; /* Make SPNEGO ignore us, we can't go any further here */
296         default:
297                 DEBUG(1, ("gensec_krb5_start: Aquiring initiator credentials failed: %s\n", error_string));
298                 return NT_STATUS_UNSUCCESSFUL;
299         }
300         in_data.length = 0;
301         
302         if (principal) {
303                 krb5_principal target_principal;
304                 ret = krb5_parse_name(gensec_krb5_state->smb_krb5_context->krb5_context, principal,
305                                       &target_principal);
306                 if (ret == 0) {
307                         ret = krb5_mk_req_exact(gensec_krb5_state->smb_krb5_context->krb5_context, 
308                                                 &gensec_krb5_state->auth_context,
309                                                 gensec_krb5_state->ap_req_options, 
310                                                 target_principal,
311                                                 &in_data, ccache_container->ccache, 
312                                                 &gensec_krb5_state->enc_ticket);
313                         krb5_free_principal(gensec_krb5_state->smb_krb5_context->krb5_context, 
314                                             target_principal);
315                 }
316         } else {
317                 ret = krb5_mk_req(gensec_krb5_state->smb_krb5_context->krb5_context, 
318                                   &gensec_krb5_state->auth_context,
319                                   gensec_krb5_state->ap_req_options,
320                                   gensec_get_target_service(gensec_security),
321                                   hostname,
322                                   &in_data, ccache_container->ccache, 
323                                   &gensec_krb5_state->enc_ticket);
324         }
325         switch (ret) {
326         case 0:
327                 return NT_STATUS_OK;
328         case KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN:
329                 DEBUG(3, ("Server [%s] is not registered with our KDC: %s\n", 
330                           hostname, smb_get_krb5_error_message(gensec_krb5_state->smb_krb5_context->krb5_context, ret, gensec_krb5_state)));
331                 return NT_STATUS_INVALID_PARAMETER; /* Make SPNEGO ignore us, we can't go any further here */
332         case KRB5_KDC_UNREACH:
333                 DEBUG(3, ("Cannot reach a KDC we require to contact host [%s]: %s\n",
334                           hostname, smb_get_krb5_error_message(gensec_krb5_state->smb_krb5_context->krb5_context, ret, gensec_krb5_state)));
335                 return NT_STATUS_INVALID_PARAMETER; /* Make SPNEGO ignore us, we can't go any further here */
336         case KRB5KDC_ERR_PREAUTH_FAILED:
337         case KRB5KRB_AP_ERR_TKT_EXPIRED:
338         case KRB5_CC_END:
339                 /* Too much clock skew - we will need to kinit to re-skew the clock */
340         case KRB5KRB_AP_ERR_SKEW:
341         case KRB5_KDCREP_SKEW:
342         {
343                 DEBUG(3, ("kerberos (mk_req) failed: %s\n", 
344                           smb_get_krb5_error_message(gensec_krb5_state->smb_krb5_context->krb5_context, ret, gensec_krb5_state)));
345                 /*fall through*/
346         }
347         
348         /* just don't print a message for these really ordinary messages */
349         case KRB5_FCC_NOFILE:
350         case KRB5_CC_NOTFOUND:
351         case ENOENT:
352                 
353                 return NT_STATUS_UNSUCCESSFUL;
354                 break;
355                 
356         default:
357                 DEBUG(0, ("kerberos: %s\n", 
358                           smb_get_krb5_error_message(gensec_krb5_state->smb_krb5_context->krb5_context, ret, gensec_krb5_state)));
359                 return NT_STATUS_UNSUCCESSFUL;
360         }
361 }
362
363 static NTSTATUS gensec_krb5_client_start(struct gensec_security *gensec_security)
364 {
365         return gensec_krb5_common_client_start(gensec_security, false);
366 }
367
368 static NTSTATUS gensec_fake_gssapi_krb5_client_start(struct gensec_security *gensec_security)
369 {
370         return gensec_krb5_common_client_start(gensec_security, true);
371 }
372
373 /**
374  * Check if the packet is one for this mechansim
375  * 
376  * @param gensec_security GENSEC state
377  * @param in The request, as a DATA_BLOB
378  * @return Error, INVALID_PARAMETER if it's not a packet for us
379  *                or NT_STATUS_OK if the packet is ok. 
380  */
381
382 static NTSTATUS gensec_fake_gssapi_krb5_magic(struct gensec_security *gensec_security, 
383                                   const DATA_BLOB *in) 
384 {
385         if (gensec_gssapi_check_oid(in, GENSEC_OID_KERBEROS5)) {
386                 return NT_STATUS_OK;
387         } else {
388                 return NT_STATUS_INVALID_PARAMETER;
389         }
390 }
391
392
393 /**
394  * Next state function for the Krb5 GENSEC mechanism
395  * 
396  * @param gensec_krb5_state KRB5 State
397  * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
398  * @param in The request, as a DATA_BLOB
399  * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
400  * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent, 
401  *                or NT_STATUS_OK if the user is authenticated. 
402  */
403
404 static NTSTATUS gensec_krb5_update(struct gensec_security *gensec_security, 
405                                    TALLOC_CTX *out_mem_ctx, 
406                                    const DATA_BLOB in, DATA_BLOB *out) 
407 {
408         struct gensec_krb5_state *gensec_krb5_state = (struct gensec_krb5_state *)gensec_security->private_data;
409         krb5_error_code ret = 0;
410         NTSTATUS nt_status;
411
412         switch (gensec_krb5_state->state_position) {
413         case GENSEC_KRB5_CLIENT_START:
414         {
415                 DATA_BLOB unwrapped_out;
416                 
417                 if (gensec_krb5_state->gssapi) {
418                         unwrapped_out = data_blob_talloc(out_mem_ctx, gensec_krb5_state->enc_ticket.data, gensec_krb5_state->enc_ticket.length);
419                         
420                         /* wrap that up in a nice GSS-API wrapping */
421                         *out = gensec_gssapi_gen_krb5_wrap(out_mem_ctx, &unwrapped_out, TOK_ID_KRB_AP_REQ);
422                 } else {
423                         *out = data_blob_talloc(out_mem_ctx, gensec_krb5_state->enc_ticket.data, gensec_krb5_state->enc_ticket.length);
424                 }
425                 if (gensec_krb5_state->ap_req_options & AP_OPTS_MUTUAL_REQUIRED) {
426                         gensec_krb5_state->state_position = GENSEC_KRB5_CLIENT_MUTUAL_AUTH;
427                         nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
428                 } else {
429                         gensec_krb5_state->state_position = GENSEC_KRB5_DONE;
430                         nt_status = NT_STATUS_OK;
431                 }
432                 return nt_status;
433         }
434                 
435         case GENSEC_KRB5_CLIENT_MUTUAL_AUTH:
436         {
437                 DATA_BLOB unwrapped_in;
438                 krb5_data inbuf;
439                 krb5_ap_rep_enc_part *repl = NULL;
440                 uint8_t tok_id[2];
441
442                 if (gensec_krb5_state->gssapi) {
443                         if (!gensec_gssapi_parse_krb5_wrap(out_mem_ctx, &in, &unwrapped_in, tok_id)) {
444                                 DEBUG(1,("gensec_gssapi_parse_krb5_wrap(mutual authentication) failed to parse\n"));
445                                 dump_data_pw("Mutual authentication message:\n", in.data, in.length);
446                                 return NT_STATUS_INVALID_PARAMETER;
447                         }
448                 } else {
449                         unwrapped_in = in;
450                 }
451                 /* TODO: check the tok_id */
452
453                 inbuf.data = unwrapped_in.data;
454                 inbuf.length = unwrapped_in.length;
455                 ret = krb5_rd_rep(gensec_krb5_state->smb_krb5_context->krb5_context, 
456                                   gensec_krb5_state->auth_context,
457                                   &inbuf, &repl);
458                 if (ret) {
459                         DEBUG(1,("krb5_rd_rep (mutual authentication) failed (%s)\n",
460                                  smb_get_krb5_error_message(gensec_krb5_state->smb_krb5_context->krb5_context, ret, out_mem_ctx)));
461                         dump_data_pw("Mutual authentication message:\n", (uint8_t *)inbuf.data, inbuf.length);
462                         nt_status = NT_STATUS_ACCESS_DENIED;
463                 } else {
464                         *out = data_blob(NULL, 0);
465                         nt_status = NT_STATUS_OK;
466                         gensec_krb5_state->state_position = GENSEC_KRB5_DONE;
467                 }
468                 if (repl) {
469                         krb5_free_ap_rep_enc_part(gensec_krb5_state->smb_krb5_context->krb5_context, repl);
470                 }
471                 return nt_status;
472         }
473
474         case GENSEC_KRB5_SERVER_START:
475         {
476                 DATA_BLOB unwrapped_in;
477                 DATA_BLOB unwrapped_out = data_blob(NULL, 0);
478                 krb5_data inbuf, outbuf;
479                 uint8_t tok_id[2];
480                 struct keytab_container *keytab;
481                 krb5_principal server_in_keytab;
482                 const char *error_string;
483                 enum credentials_obtained obtained;
484
485                 if (!in.data) {
486                         return NT_STATUS_INVALID_PARAMETER;
487                 }       
488
489                 /* Grab the keytab, however generated */
490                 ret = cli_credentials_get_keytab(gensec_get_credentials(gensec_security), 
491                                                  gensec_security->event_ctx, 
492                                                  gensec_security->settings->lp_ctx, &keytab);
493                 if (ret) {
494                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
495                 }
496                 
497                 /* This ensures we lookup the correct entry in that keytab */
498                 ret = principal_from_credentials(out_mem_ctx, gensec_get_credentials(gensec_security), 
499                                                  gensec_krb5_state->smb_krb5_context, 
500                                                  &server_in_keytab, &obtained, &error_string);
501
502                 if (ret) {
503                         DEBUG(2,("Failed to make credentials from principal: %s\n", error_string));
504                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
505                 }
506
507                 /* Parse the GSSAPI wrapping, if it's there... (win2k3 allows it to be omited) */
508                 if (gensec_krb5_state->gssapi
509                     && gensec_gssapi_parse_krb5_wrap(out_mem_ctx, &in, &unwrapped_in, tok_id)) {
510                         inbuf.data = unwrapped_in.data;
511                         inbuf.length = unwrapped_in.length;
512                 } else {
513                         inbuf.data = in.data;
514                         inbuf.length = in.length;
515                 }
516
517                 ret = smb_rd_req_return_stuff(gensec_krb5_state->smb_krb5_context->krb5_context,
518                                               &gensec_krb5_state->auth_context, 
519                                               &inbuf, keytab->keytab, server_in_keytab,  
520                                               &outbuf, 
521                                               &gensec_krb5_state->ticket, 
522                                               &gensec_krb5_state->keyblock);
523
524                 if (ret) {
525                         return NT_STATUS_LOGON_FAILURE;
526                 }
527                 unwrapped_out.data = (uint8_t *)outbuf.data;
528                 unwrapped_out.length = outbuf.length;
529                 gensec_krb5_state->state_position = GENSEC_KRB5_DONE;
530                 /* wrap that up in a nice GSS-API wrapping */
531                 if (gensec_krb5_state->gssapi) {
532                         *out = gensec_gssapi_gen_krb5_wrap(out_mem_ctx, &unwrapped_out, TOK_ID_KRB_AP_REP);
533                 } else {
534                         *out = data_blob_talloc(out_mem_ctx, outbuf.data, outbuf.length);
535                 }
536                 krb5_data_free(&outbuf);
537                 return NT_STATUS_OK;
538         }
539
540         case GENSEC_KRB5_DONE:
541         default:
542                 /* Asking too many times... */
543                 return NT_STATUS_INVALID_PARAMETER;
544         }
545 }
546
547 static NTSTATUS gensec_krb5_session_key(struct gensec_security *gensec_security, 
548                                         DATA_BLOB *session_key) 
549 {
550         struct gensec_krb5_state *gensec_krb5_state = (struct gensec_krb5_state *)gensec_security->private_data;
551         krb5_context context = gensec_krb5_state->smb_krb5_context->krb5_context;
552         krb5_auth_context auth_context = gensec_krb5_state->auth_context;
553         krb5_keyblock *skey;
554         krb5_error_code err = -1;
555
556         if (gensec_krb5_state->state_position != GENSEC_KRB5_DONE) {
557                 return NT_STATUS_NO_USER_SESSION_KEY;
558         }
559
560         if (gensec_krb5_state->session_key.data) {
561                 *session_key = gensec_krb5_state->session_key;
562                 return NT_STATUS_OK;
563         }
564
565         switch (gensec_security->gensec_role) {
566         case GENSEC_CLIENT:
567                 err = krb5_auth_con_getlocalsubkey(context, auth_context, &skey);
568                 break;
569         case GENSEC_SERVER:
570                 err = krb5_auth_con_getremotesubkey(context, auth_context, &skey);
571                 break;
572         }
573         if (err == 0 && skey != NULL) {
574                 DEBUG(10, ("Got KRB5 session key of length %d\n",  
575                            (int)KRB5_KEY_LENGTH(skey)));
576                 gensec_krb5_state->session_key = data_blob_talloc(gensec_krb5_state, 
577                                                 KRB5_KEY_DATA(skey), KRB5_KEY_LENGTH(skey));
578                 *session_key = gensec_krb5_state->session_key;
579                 dump_data_pw("KRB5 Session Key:\n", session_key->data, session_key->length);
580
581                 krb5_free_keyblock(context, skey);
582                 return NT_STATUS_OK;
583         } else {
584                 DEBUG(10, ("KRB5 error getting session key %d\n", err));
585                 return NT_STATUS_NO_USER_SESSION_KEY;
586         }
587 }
588
589 static NTSTATUS gensec_krb5_session_info(struct gensec_security *gensec_security,
590                                          struct auth_session_info **_session_info) 
591 {
592         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
593         struct gensec_krb5_state *gensec_krb5_state = (struct gensec_krb5_state *)gensec_security->private_data;
594         krb5_context context = gensec_krb5_state->smb_krb5_context->krb5_context;
595         struct auth_serversupplied_info *server_info = NULL;
596         struct auth_session_info *session_info = NULL;
597         struct PAC_LOGON_INFO *logon_info;
598
599         krb5_principal client_principal;
600         char *principal_string;
601         
602         DATA_BLOB pac;
603         krb5_data pac_data;
604
605         krb5_error_code ret;
606
607         TALLOC_CTX *mem_ctx = talloc_new(gensec_security);
608         if (!mem_ctx) {
609                 return NT_STATUS_NO_MEMORY;
610         }
611         
612         ret = krb5_ticket_get_client(context, gensec_krb5_state->ticket, &client_principal);
613         if (ret) {
614                 DEBUG(5, ("krb5_ticket_get_client failed to get cleint principal: %s\n", 
615                           smb_get_krb5_error_message(context, 
616                                                      ret, mem_ctx)));
617                 talloc_free(mem_ctx);
618                 return NT_STATUS_NO_MEMORY;
619         }
620         
621         ret = krb5_unparse_name(gensec_krb5_state->smb_krb5_context->krb5_context, 
622                                 client_principal, &principal_string);
623         if (ret) {
624                 DEBUG(1, ("Unable to parse client principal: %s\n",
625                           smb_get_krb5_error_message(context, 
626                                                      ret, mem_ctx)));
627                 talloc_free(mem_ctx);
628                 return NT_STATUS_NO_MEMORY;
629         }
630
631         ret = krb5_ticket_get_authorization_data_type(context, gensec_krb5_state->ticket, 
632                                                       KRB5_AUTHDATA_WIN2K_PAC, 
633                                                       &pac_data);
634         
635         if (ret && gensec_setting_bool(gensec_security->settings, "gensec", "require_pac", false)) {
636                 DEBUG(1, ("Unable to find PAC in ticket from %s, failing to allow access: %s \n",
637                           principal_string,
638                           smb_get_krb5_error_message(context, 
639                                                      ret, mem_ctx)));
640                 krb5_free_principal(context, client_principal);
641                 free(principal_string);
642                 return NT_STATUS_ACCESS_DENIED;
643         } else if (ret) {
644                 /* NO pac */
645                 DEBUG(5, ("krb5_ticket_get_authorization_data_type failed to find PAC: %s\n", 
646                           smb_get_krb5_error_message(context, 
647                                                      ret, mem_ctx)));
648                 if (gensec_security->auth_context && 
649                     !gensec_setting_bool(gensec_security->settings, "gensec", "require_pac", false)) {
650                         DEBUG(1, ("Unable to find PAC for %s, resorting to local user lookup: %s",
651                                   principal_string, smb_get_krb5_error_message(context, 
652                                                      ret, mem_ctx)));
653                         nt_status = gensec_security->auth_context->get_server_info_principal(mem_ctx, 
654                                                                                              gensec_security->auth_context, 
655                                                                                              principal_string,
656                                                                                              &server_info);
657                         if (!NT_STATUS_IS_OK(nt_status)) {
658                                 talloc_free(mem_ctx);
659                                 return nt_status;
660                         }
661                 } else {
662                         DEBUG(1, ("Unable to find PAC in ticket from %s, failing to allow access\n",
663                                   principal_string));
664                         return NT_STATUS_ACCESS_DENIED;
665                 }
666
667                 krb5_free_principal(context, client_principal);
668                 free(principal_string);
669                 
670                 if (!NT_STATUS_IS_OK(nt_status)) {
671                         talloc_free(mem_ctx);
672                         return nt_status;
673                 }
674         } else {
675                 /* Found pac */
676                 union netr_Validation validation;
677                 free(principal_string);
678
679                 pac = data_blob_talloc(mem_ctx, pac_data.data, pac_data.length);
680                 if (!pac.data) {
681                         krb5_free_principal(context, client_principal);
682                         talloc_free(mem_ctx);
683                         return NT_STATUS_NO_MEMORY;
684                 }
685
686                 /* decode and verify the pac */
687                 nt_status = kerberos_pac_logon_info(gensec_krb5_state, 
688                                                     &logon_info, pac,
689                                                     gensec_krb5_state->smb_krb5_context->krb5_context,
690                                                     NULL, gensec_krb5_state->keyblock,
691                                                     client_principal,
692                                                     gensec_krb5_state->ticket->ticket.authtime, NULL);
693                 krb5_free_principal(context, client_principal);
694
695                 if (!NT_STATUS_IS_OK(nt_status)) {
696                         talloc_free(mem_ctx);
697                         return nt_status;
698                 }
699
700                 validation.sam3 = &logon_info->info3;
701                 nt_status = make_server_info_netlogon_validation(mem_ctx, 
702                                                                  NULL,
703                                                                  3, &validation,
704                                                                  &server_info); 
705                 if (!NT_STATUS_IS_OK(nt_status)) {
706                         talloc_free(mem_ctx);
707                         return nt_status;
708                 }
709         }
710
711         /* references the server_info into the session_info */
712         nt_status = gensec_generate_session_info(mem_ctx, gensec_security, server_info, &session_info);
713
714         if (!NT_STATUS_IS_OK(nt_status)) {
715                 talloc_free(mem_ctx);
716                 return nt_status;
717         }
718
719         nt_status = gensec_krb5_session_key(gensec_security, &session_info->session_key);
720
721         if (!NT_STATUS_IS_OK(nt_status)) {
722                 talloc_free(mem_ctx);
723                 return nt_status;
724         }
725
726         *_session_info = session_info;
727
728         talloc_steal(gensec_krb5_state, session_info);
729         talloc_free(mem_ctx);
730         return NT_STATUS_OK;
731 }
732
733 static NTSTATUS gensec_krb5_wrap(struct gensec_security *gensec_security, 
734                                    TALLOC_CTX *mem_ctx, 
735                                    const DATA_BLOB *in, 
736                                    DATA_BLOB *out)
737 {
738         struct gensec_krb5_state *gensec_krb5_state = (struct gensec_krb5_state *)gensec_security->private_data;
739         krb5_context context = gensec_krb5_state->smb_krb5_context->krb5_context;
740         krb5_auth_context auth_context = gensec_krb5_state->auth_context;
741         krb5_error_code ret;
742         krb5_data input, output;
743         input.length = in->length;
744         input.data = in->data;
745         
746         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
747                 ret = krb5_mk_priv(context, auth_context, &input, &output, NULL);
748                 if (ret) {
749                         DEBUG(1, ("krb5_mk_priv failed: %s\n", 
750                                   smb_get_krb5_error_message(gensec_krb5_state->smb_krb5_context->krb5_context, 
751                                                              ret, mem_ctx)));
752                         return NT_STATUS_ACCESS_DENIED;
753                 }
754                 *out = data_blob_talloc(mem_ctx, output.data, output.length);
755                 
756                 krb5_data_free(&output);
757         } else {
758                 return NT_STATUS_ACCESS_DENIED;
759         }
760         return NT_STATUS_OK;
761 }
762
763 static NTSTATUS gensec_krb5_unwrap(struct gensec_security *gensec_security, 
764                                      TALLOC_CTX *mem_ctx, 
765                                      const DATA_BLOB *in, 
766                                      DATA_BLOB *out)
767 {
768         struct gensec_krb5_state *gensec_krb5_state = (struct gensec_krb5_state *)gensec_security->private_data;
769         krb5_context context = gensec_krb5_state->smb_krb5_context->krb5_context;
770         krb5_auth_context auth_context = gensec_krb5_state->auth_context;
771         krb5_error_code ret;
772         krb5_data input, output;
773         krb5_replay_data replay;
774         input.length = in->length;
775         input.data = in->data;
776         
777         if (gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
778                 ret = krb5_rd_priv(context, auth_context, &input, &output, &replay);
779                 if (ret) {
780                         DEBUG(1, ("krb5_rd_priv failed: %s\n", 
781                                   smb_get_krb5_error_message(gensec_krb5_state->smb_krb5_context->krb5_context, 
782                                                              ret, mem_ctx)));
783                         return NT_STATUS_ACCESS_DENIED;
784                 }
785                 *out = data_blob_talloc(mem_ctx, output.data, output.length);
786                 
787                 krb5_data_free(&output);
788         } else {
789                 return NT_STATUS_ACCESS_DENIED;
790         }
791         return NT_STATUS_OK;
792 }
793
794 static bool gensec_krb5_have_feature(struct gensec_security *gensec_security,
795                                      uint32_t feature)
796 {
797         struct gensec_krb5_state *gensec_krb5_state = (struct gensec_krb5_state *)gensec_security->private_data;
798         if (feature & GENSEC_FEATURE_SESSION_KEY) {
799                 return true;
800         } 
801         if (!gensec_krb5_state->gssapi && 
802             (feature & GENSEC_FEATURE_SEAL)) {
803                 return true;
804         } 
805         
806         return false;
807 }
808
809 static const char *gensec_krb5_oids[] = { 
810         GENSEC_OID_KERBEROS5,
811         GENSEC_OID_KERBEROS5_OLD,
812         NULL 
813 };
814
815 static const struct gensec_security_ops gensec_fake_gssapi_krb5_security_ops = {
816         .name           = "fake_gssapi_krb5",
817         .auth_type      = DCERPC_AUTH_TYPE_KRB5,
818         .oid            = gensec_krb5_oids,
819         .client_start   = gensec_fake_gssapi_krb5_client_start,
820         .server_start   = gensec_fake_gssapi_krb5_server_start,
821         .update         = gensec_krb5_update,
822         .magic          = gensec_fake_gssapi_krb5_magic,
823         .session_key    = gensec_krb5_session_key,
824         .session_info   = gensec_krb5_session_info,
825         .have_feature   = gensec_krb5_have_feature,
826         .enabled        = false,
827         .kerberos       = true,
828         .priority       = GENSEC_KRB5
829 };
830
831 static const struct gensec_security_ops gensec_krb5_security_ops = {
832         .name           = "krb5",
833         .client_start   = gensec_krb5_client_start,
834         .server_start   = gensec_krb5_server_start,
835         .update         = gensec_krb5_update,
836         .session_key    = gensec_krb5_session_key,
837         .session_info   = gensec_krb5_session_info,
838         .have_feature   = gensec_krb5_have_feature,
839         .wrap           = gensec_krb5_wrap,
840         .unwrap         = gensec_krb5_unwrap,
841         .enabled        = true,
842         .kerberos       = true,
843         .priority       = GENSEC_KRB5
844 };
845
846 _PUBLIC_ NTSTATUS gensec_krb5_init(void)
847 {
848         NTSTATUS ret;
849
850         ret = gensec_register(&gensec_krb5_security_ops);
851         if (!NT_STATUS_IS_OK(ret)) {
852                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
853                         gensec_krb5_security_ops.name));
854                 return ret;
855         }
856
857         ret = gensec_register(&gensec_fake_gssapi_krb5_security_ops);
858         if (!NT_STATUS_IS_OK(ret)) {
859                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
860                         gensec_krb5_security_ops.name));
861                 return ret;
862         }
863
864         return ret;
865 }