summaryrefslogtreecommitdiff
path: root/source4/libcli/auth/gensec_krb5.c
blob: e5bfd3865a4bf88e0295b998d87e9bc43830e322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/* 
   Unix SMB/CIFS implementation.

   Kerberos backend for GENSEC
   
   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004
   Copyright (C) Andrew Tridgell 2001
   Copyright (C) Luke Howard 2002-2003
   Copyright (C) Stefan Metzmacher 2004-2005

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "system/kerberos.h"
#include "system/time.h"
#include "libcli/auth/kerberos.h"
#include "librpc/gen_ndr/ndr_krb5pac.h"
#include "auth/auth.h"

enum GENSEC_KRB5_STATE {
	GENSEC_KRB5_SERVER_START,
	GENSEC_KRB5_CLIENT_START,
	GENSEC_KRB5_CLIENT_MUTUAL_AUTH,
	GENSEC_KRB5_DONE
};

struct gensec_krb5_state {
	DATA_BLOB session_key;
	DATA_BLOB pac;
	enum GENSEC_KRB5_STATE state_position;
	krb5_context krb5_context;
	krb5_auth_context krb5_auth_context;
	krb5_ccache krb5_ccache;
	krb5_data ticket;
	krb5_keyblock krb5_keyblock;
	char *peer_principal;
};

#ifdef KRB5_DO_VERIFY_PAC
static NTSTATUS gensec_krb5_pac_checksum(DATA_BLOB pac_data,
					    struct PAC_SIGNATURE_DATA *sig,
					    struct gensec_krb5_state *gensec_krb5_state,
					    uint32 keyusage)
{
	krb5_error_code ret;
	krb5_crypto crypto;
	Checksum cksum;
	int i;

	cksum.cksumtype		= (CKSUMTYPE)sig->type;
	cksum.checksum.length	= sizeof(sig->signature);
	cksum.checksum.data	= sig->signature;


	ret = krb5_crypto_init(gensec_krb5_state->krb5_context,
				&gensec_krb5_state->krb5_keyblock,
				0,
				&crypto);
	if (ret) {
		DEBUG(0,("krb5_crypto_init() failed\n"));
		return NT_STATUS_FOOBAR;
	}
	for (i=0; i < 40; i++) {
		keyusage = i;
		ret = krb5_verify_checksum(gensec_krb5_state->krb5_context,
					   crypto,
					   keyusage,
					   pac_data.data,
					   pac_data.length,
					   &cksum);
		if (!ret) {
			DEBUG(0,("PAC Verified: keyusage: %d\n", keyusage));
			break;
		}
	}
	krb5_crypto_destroy(gensec_krb5_state->krb5_context, crypto);

	if (ret) {
		DEBUG(0,("NOT verifying PAC checksums yet!\n"));
		//return NT_STATUS_LOGON_FAILURE;
	} else {
		DEBUG(0,("PAC checksums verified!\n"));
	}

	return NT_STATUS_OK;
}
#endif

static NTSTATUS gensec_krb5_decode_pac(TALLOC_CTX *mem_ctx,
				struct PAC_LOGON_INFO **logon_info_out,
				DATA_BLOB blob,
				struct gensec_krb5_state *gensec_krb5_state)
{
	NTSTATUS status;
	struct PAC_SIGNATURE_DATA srv_sig;
	struct PAC_SIGNATURE_DATA *srv_sig_ptr;
	struct PAC_SIGNATURE_DATA kdc_sig;
	struct PAC_SIGNATURE_DATA *kdc_sig_ptr;
	struct PAC_LOGON_INFO *logon_info = NULL;
	struct PAC_DATA pac_data;
#ifdef KRB5_DO_VERIFY_PAC
	DATA_BLOB tmp_blob = data_blob(NULL, 0);
#endif
	int i;

	status = ndr_pull_struct_blob(&blob, mem_ctx, &pac_data,
					(ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0,("can't parse the PAC\n"));
		return status;
	}
	NDR_PRINT_DEBUG(PAC_DATA, &pac_data);

	if (pac_data.num_buffers < 3) {
		/* we need logon_ingo, service_key and kdc_key */
		DEBUG(0,("less than 3 PAC buffers\n"));
		return NT_STATUS_FOOBAR;
	}

	for (i=0; i < pac_data.num_buffers; i++) {
		switch (pac_data.buffers[i].type) {
			case PAC_TYPE_LOGON_INFO:
				if (!pac_data.buffers[i].info) {
					break;
				}
				logon_info = &pac_data.buffers[i].info->logon_info;
				break;
			case PAC_TYPE_SRV_CHECKSUM:
				if (!pac_data.buffers[i].info) {
					break;
				}
				srv_sig_ptr = &pac_data.buffers[i].info->srv_cksum;
				srv_sig = pac_data.buffers[i].info->srv_cksum;
				break;
			case PAC_TYPE_KDC_CHECKSUM:
				if (!pac_data.buffers[i].info) {
					break;
				}
				kdc_sig_ptr = &pac_data.buffers[i].info->kdc_cksum;
				kdc_sig = pac_data.buffers[i].info->kdc_cksum;
				break;
			case PAC_TYPE_UNKNOWN_10:
				break;
			default:
				break;
		}
	}

	if (!logon_info) {
		DEBUG(0,("PAC no logon_info\n"));
		return NT_STATUS_FOOBAR;
	}

	if (!srv_sig_ptr) {
		DEBUG(0,("PAC no srv_key\n"));
		return NT_STATUS_FOOBAR;
	}

	if (!kdc_sig_ptr) {
		DEBUG(0,("PAC no kdc_key\n"));
		return NT_STATUS_FOOBAR;
	}
#ifdef KRB5_DO_VERIFY_PAC
	/* clear the kdc_key */
/*	memset((void *)kdc_sig_ptr , '\0', sizeof(*kdc_sig_ptr));*/

	status = ndr_push_struct_blob(&tmp_blob, mem_ctx, &pac_data,
					      (ndr_push_flags_fn_t)ndr_push_PAC_DATA);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}
	status = ndr_pull_struct_blob(&tmp_blob, mem_ctx, &pac_data,
					(ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0,("can't parse the PAC\n"));
		return status;
	}
	/*NDR_PRINT_DEBUG(PAC_DATA, &pac_data);*/

	/* verify by kdc_key */
	status = gensec_krb5_pac_checksum(tmp_blob, &kdc_sig, gensec_krb5_state, 0);

	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* clear the service_key */
/*	memset((void *)srv_sig_ptr , '\0', sizeof(*srv_sig_ptr));*/

	status = ndr_push_struct_blob(&tmp_blob, mem_ctx, &pac_data,
					      (ndr_push_flags_fn_t)ndr_push_PAC_DATA);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}
	status = ndr_pull_struct_blob(&tmp_blob, mem_ctx, &pac_data,
					(ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0,("can't parse the PAC\n"));
		return status;
	}
	NDR_PRINT_DEBUG(PAC_DATA, &pac_data);

	/* verify by servie_key */
	status = gensec_krb5_pac_checksum(tmp_blob, &srv_sig, gensec_krb5_state, 0);

	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}
#endif
	DEBUG(0,("account_name: %s [%s]\n",logon_info->info3.base.account_name.string, logon_info->info3.base.full_name.string));
	*logon_info_out = logon_info;

	return status;
}

static int gensec_krb5_destory(void *ptr) 
{
	struct gensec_krb5_state *gensec_krb5_state = ptr;

	if (gensec_krb5_state->ticket.length) { 
		kerberos_free_data_contents(gensec_krb5_state->krb5_context, &gensec_krb5_state->ticket); 
	}
	if (gensec_krb5_state->krb5_ccache) {
		/* current heimdal - 0.6.3, which we need anyway, fixes segfaults here */
		krb5_cc_close(gensec_krb5_state->krb5_context, gensec_krb5_state->krb5_ccache);
	}

	krb5_free_keyblock_contents(gensec_krb5_state->krb5_context, 
				    &gensec_krb5_state->krb5_keyblock);
		
	if (gensec_krb5_state->krb5_auth_context) {
		krb5_auth_con_free(gensec_krb5_state->krb5_context, 
				   gensec_krb5_state->krb5_auth_context);
	}

	if (gensec_krb5_state->krb5_context) {
		krb5_free_context(gensec_krb5_state->krb5_context);
	}
	return 0;
}

static NTSTATUS gensec_krb5_start(struct gensec_security *gensec_security)
{
	struct gensec_krb5_state *gensec_krb5_state;
	krb5_error_code ret = 0;

	gensec_krb5_state = talloc(gensec_security, struct gensec_krb5_state);
	if (!gensec_krb5_state) {
		return NT_STATUS_NO_MEMORY;
	}

	gensec_security->private_data = gensec_krb5_state;

	initialize_krb5_error_table();
	gensec_krb5_state->krb5_context = NULL;
	gensec_krb5_state->krb5_auth_context = NULL;
	gensec_krb5_state->krb5_ccache = NULL;
	ZERO_STRUCT(gensec_krb5_state->ticket);
	ZERO_STRUCT(gensec_krb5_state->krb5_keyblock);
	gensec_krb5_state->session_key = data_blob(NULL, 0);
	gensec_krb5_state->pac = data_blob(NULL, 0);

	talloc_set_destructor(gensec_krb5_state, gensec_krb5_destory); 

	ret = krb5_init_context(&gensec_krb5_state->krb5_context);
	if (ret) {
		DEBUG(1,("gensec_krb5_start: krb5_init_context failed (%s)\n", error_message(ret)));
		return NT_STATUS_INTERNAL_ERROR;
	}

	if (lp_realm() && *lp_realm()) {
		ret = krb5_set_default_realm(gensec_krb5_state->krb5_context, lp_realm());
		if (ret) {
			DEBUG(1,("gensec_krb5_start: krb5_set_default_realm failed (%s)\n", error_message(ret)));
			return NT_STATUS_INTERNAL_ERROR;
		}
	}

	ret = krb5_auth_con_init(gensec_krb5_state->krb5_context, &gensec_krb5_state->krb5_auth_context);
	if (ret) {
		DEBUG(1,("gensec_krb5_start: krb5_auth_con_init failed (%s)\n", error_message(ret)));
		return NT_STATUS_INTERNAL_ERROR;
	}

	return NT_STATUS_OK;
}

static NTSTATUS gensec_krb5_server_start(struct gensec_security *gensec_security)
{
	NTSTATUS nt_status;
	struct gensec_krb5_state *gensec_krb5_state;

	nt_status = gensec_krb5_start(gensec_security);
	if (!NT_STATUS_IS_OK(nt_status)) {
		return nt_status;
	}

	gensec_krb5_state = gensec_security->private_data;
	gensec_krb5_state->state_position = GENSEC_KRB5_SERVER_START;

	return NT_STATUS_OK;
}

static NTSTATUS gensec_krb5_client_start(struct gensec_security *gensec_security)
{
	struct gensec_krb5_state *gensec_krb5_state;
	krb5_error_code ret;
	NTSTATUS nt_status;

	nt_status = gensec_krb5_start(gensec_security);
	if (!NT_STATUS_IS_OK(nt_status)) {
		return nt_status;
	}

	gensec_krb5_state = gensec_security->private_data;
	gensec_krb5_state->state_position = GENSEC_KRB5_CLIENT_START;

	/* TODO: This is effecivly a static/global variable... 
	 
	   TODO: If the user set a username, we should use an in-memory CCACHE (see below)
	*/ 
	ret = krb5_cc_default(gensec_krb5_state->krb5_context, &gensec_krb5_state->krb5_ccache);
	if (ret) {
		DEBUG(1,("krb5_cc_default failed (%s)\n",
			 error_message(ret)));
		return NT_STATUS_INTERNAL_ERROR;
	}
	
	while (1) {
		if (gensec_security->target.principal) {
			DEBUG(5, ("Finding ticket for target [%s]\n", gensec_security->target.principal));
			ret = ads_krb5_mk_req(gensec_krb5_state->krb5_context, 
					      &gensec_krb5_state->krb5_auth_context,
					      AP_OPTS_USE_SUBKEY | AP_OPTS_MUTUAL_REQUIRED,
					      gensec_security->target.principal,
					      gensec_krb5_state->krb5_ccache, 
					      &gensec_krb5_state->ticket);
		} else {
			krb5_data in_data;
			const char *hostname = gensec_get_target_hostname(gensec_security);
			if (!hostname) {
				DEBUG(1, ("Could not determine hostname for target computer, cannot use kerberos\n"));
				return NT_STATUS_ACCESS_DENIED;
			}
			
			in_data.length = 0;

			ret = krb5_mk_req(gensec_krb5_state->krb5_context, 
					  &gensec_krb5_state->krb5_auth_context,
					  AP_OPTS_USE_SUBKEY | AP_OPTS_MUTUAL_REQUIRED,
					  gensec_get_target_service(gensec_security),
					  hostname,
					  &in_data, gensec_krb5_state->krb5_ccache, 
					  &gensec_krb5_state->ticket);
			
		}
		switch (ret) {
		case 0:
			return NT_STATUS_OK;
		case KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN:
			DEBUG(3, ("Server is not registered with our KDC: %s\n", 
				  error_message(ret)));
			return NT_STATUS_ACCESS_DENIED;
		case KRB5KDC_ERR_PREAUTH_FAILED:
		case KRB5KRB_AP_ERR_TKT_EXPIRED:
		case KRB5_CC_END:
		{
			DEBUG(3, ("kerberos (mk_req) failed: %s\n", 
				  error_message(ret)));
			/* fall down to remaining code */
		}
		/* just don't print a message for these really ordinary messages */
		case KRB5_FCC_NOFILE:
		case KRB5_CC_NOTFOUND:
		case ENOENT:
		{
			char *password;
			char *ccache_string;
			time_t kdc_time = 0;
			nt_status = gensec_get_password(gensec_security, 
							gensec_security, 
							&password);
			if (!NT_STATUS_IS_OK(nt_status)) {
				return nt_status;
			}
			
			/* this string should be unique */
			ccache_string = talloc_asprintf(gensec_krb5_state, "MEMORY:%s:%s:%s", 
							gensec_get_client_principal(gensec_security, gensec_krb5_state), 
							gensec_get_target_principal(gensec_security),
							generate_random_str(gensec_krb5_state, 16));

			ret = krb5_cc_resolve(gensec_krb5_state->krb5_context, ccache_string, &gensec_krb5_state->krb5_ccache);
			if (ret) {
				DEBUG(1,("failed to generate a new krb5 keytab (%s): %s\n", 
					 ccache_string,
					 error_message(ret)));
				return NT_STATUS_INTERNAL_ERROR;
			}

			ret = kerberos_kinit_password_cc(gensec_krb5_state->krb5_context, gensec_krb5_state->krb5_ccache, 
						      gensec_get_client_principal(gensec_security, gensec_krb5_state), 
						      password, NULL, &kdc_time);

			/* cope with ticket being in the future due to clock skew */
			if ((unsigned)kdc_time > time(NULL)) {
				time_t t = time(NULL);
				int time_offset =(unsigned)kdc_time-t;
				DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
				krb5_set_real_time(gensec_krb5_state->krb5_context, t + time_offset + 1, 0);
			}
	
			if (ret) {
				DEBUG(1,("kinit failed (%s)\n", 
					 error_message(ret)));
				return NT_STATUS_WRONG_PASSWORD;
			}
			break;
		}
		default:
			DEBUG(0, ("kerberos: %s\n", 
				  error_message(ret)));
			return NT_STATUS_UNSUCCESSFUL;
		}
	}
}


/**
 * Next state function for the Krb5 GENSEC mechanism
 * 
 * @param gensec_krb5_state KRB5 State
 * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
 * @param in The request, as a DATA_BLOB
 * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
 * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent, 
 *                or NT_STATUS_OK if the user is authenticated. 
 */

static NTSTATUS gensec_krb5_update(struct gensec_security *gensec_security, 
				   TALLOC_CTX *out_mem_ctx, 
				   const DATA_BLOB in, DATA_BLOB *out) 
{
	struct gensec_krb5_state *gensec_krb5_state = gensec_security->private_data;
	krb5_error_code ret = 0;
	DATA_BLOB pac;
	NTSTATUS nt_status;

	switch (gensec_krb5_state->state_position) {
	case GENSEC_KRB5_CLIENT_START:
	{
		if (ret) {
			DEBUG(1,("ads_krb5_mk_req (request ticket) failed (%s)\n",
				 error_message(ret)));
			nt_status = NT_STATUS_LOGON_FAILURE;
		} else {
			DATA_BLOB unwrapped_out;

#ifndef GENSEC_SEND_UNWRAPPED_KRB5 /* This should be a switch for the torture code to set */
			unwrapped_out = data_blob_talloc(out_mem_ctx, gensec_krb5_state->ticket.data, gensec_krb5_state->ticket.length);
			
			/* wrap that up in a nice GSS-API wrapping */
			*out = gensec_gssapi_gen_krb5_wrap(out_mem_ctx, &unwrapped_out, TOK_ID_KRB_AP_REQ);
#else
			*out = data_blob_talloc(out_mem_ctx, gensec_krb5_state->ticket.data, gensec_krb5_state->ticket.length);
#endif
			gensec_krb5_state->state_position = GENSEC_KRB5_CLIENT_MUTUAL_AUTH;
			nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
		}
		
		return nt_status;
	}
		
	case GENSEC_KRB5_CLIENT_MUTUAL_AUTH:
	{
		krb5_data inbuf;
		krb5_ap_rep_enc_part *repl = NULL;
		uint8 tok_id[2];
		DATA_BLOB unwrapped_in;

		if (!gensec_gssapi_parse_krb5_wrap(out_mem_ctx, &in, &unwrapped_in, tok_id)) {
			DEBUG(1,("gensec_gssapi_parse_krb5_wrap(mutual authentication) failed to parse\n"));
			dump_data_pw("Mutual authentication message:\n", in.data, in.length);
			return NT_STATUS_INVALID_PARAMETER;
		}
		/* TODO: check the tok_id */

		inbuf.data = unwrapped_in.data;
		inbuf.length = unwrapped_in.length;
		ret = krb5_rd_rep(gensec_krb5_state->krb5_context, 
				  gensec_krb5_state->krb5_auth_context,
				  &inbuf, &repl);
		if (ret) {
			DEBUG(1,("krb5_rd_rep (mutual authentication) failed (%s)\n",
				 error_message(ret)));
			dump_data_pw("Mutual authentication message:\n", inbuf.data, inbuf.length);
			nt_status = NT_STATUS_ACCESS_DENIED;
		} else {
			*out = data_blob(NULL, 0);
			nt_status = NT_STATUS_OK;
			gensec_krb5_state->state_position = GENSEC_KRB5_DONE;
		}
		if (repl) {
			krb5_free_ap_rep_enc_part(gensec_krb5_state->krb5_context, repl);
		}
		return nt_status;
	}

	case GENSEC_KRB5_SERVER_START:
	{
		char *principal;
		DATA_BLOB unwrapped_in;
		DATA_BLOB unwrapped_out = data_blob(NULL, 0);
		uint8 tok_id[2];

		if (!in.data) {
			*out = unwrapped_out;
			return NT_STATUS_MORE_PROCESSING_REQUIRED;
		}	

		/* Parse the GSSAPI wrapping, if it's there... (win2k3 allows it to be omited) */
		if (!gensec_gssapi_parse_krb5_wrap(out_mem_ctx, &in, &unwrapped_in, tok_id)) {
			nt_status = ads_verify_ticket(out_mem_ctx, 
						      gensec_krb5_state->krb5_context, 
						      gensec_krb5_state->krb5_auth_context, 
						      lp_realm(), 
						      gensec_get_target_service(gensec_security), &in, 
						      &principal, &pac, &unwrapped_out,
						      &gensec_krb5_state->krb5_keyblock);
		} else {
			/* TODO: check the tok_id */
			nt_status = ads_verify_ticket(out_mem_ctx, 
						      gensec_krb5_state->krb5_context, 
						      gensec_krb5_state->krb5_auth_context, 
						      lp_realm(), 
						      gensec_get_target_service(gensec_security), 
						      &unwrapped_in, 
						      &principal, &pac, &unwrapped_out,
						      &gensec_krb5_state->krb5_keyblock);
		}

		if (!NT_STATUS_IS_OK(nt_status)) {
			return nt_status;
		}

		if (pac.data) {
			gensec_krb5_state->pac = data_blob_talloc_reference(gensec_krb5_state, &pac);
		}

		if (NT_STATUS_IS_OK(nt_status)) {
			gensec_krb5_state->state_position = GENSEC_KRB5_DONE;
			/* wrap that up in a nice GSS-API wrapping */
#ifndef GENSEC_SEND_UNWRAPPED_KRB5
			*out = gensec_gssapi_gen_krb5_wrap(out_mem_ctx, &unwrapped_out, TOK_ID_KRB_AP_REP);
#else
			*out = unwrapped_out;
#endif
			gensec_krb5_state->peer_principal = talloc_steal(gensec_krb5_state, principal);
		}
		return nt_status;
	}
	case GENSEC_KRB5_DONE:
		return NT_STATUS_OK;
	}
	
	return NT_STATUS_INVALID_PARAMETER;
}

static NTSTATUS gensec_krb5_session_key(struct gensec_security *gensec_security, 
					   DATA_BLOB *session_key) 
{
	struct gensec_krb5_state *gensec_krb5_state = gensec_security->private_data;
	krb5_context context = gensec_krb5_state->krb5_context;
	krb5_auth_context auth_context = gensec_krb5_state->krb5_auth_context;
	krb5_keyblock *skey;
	krb5_error_code err;

	if (gensec_krb5_state->session_key.data) {
		*session_key = gensec_krb5_state->session_key;
		return NT_STATUS_OK;
	}

	switch (gensec_security->gensec_role) {
	case GENSEC_CLIENT:
		err = krb5_auth_con_getlocalsubkey(context, auth_context, &skey);
		break;
	case GENSEC_SERVER:
		err = krb5_auth_con_getremotesubkey(context, auth_context, &skey);
		break;
	}
	if (err == 0 && skey != NULL) {
		DEBUG(10, ("Got KRB5 session key of length %d\n",  KRB5_KEY_LENGTH(skey)));
		gensec_krb5_state->session_key = data_blob_talloc(gensec_krb5_state, 
						KRB5_KEY_DATA(skey), KRB5_KEY_LENGTH(skey));
		*session_key = gensec_krb5_state->session_key;
		dump_data_pw("KRB5 Session Key:\n", session_key->data, session_key->length);

		krb5_free_keyblock(context, skey);
		return NT_STATUS_OK;
	} else {
		DEBUG(10, ("KRB5 error getting session key %d\n", err));
		return NT_STATUS_NO_USER_SESSION_KEY;
	}
}

static NTSTATUS gensec_krb5_session_info(struct gensec_security *gensec_security,
					 struct auth_session_info **_session_info) 
{
	NTSTATUS nt_status;
	struct gensec_krb5_state *gensec_krb5_state = gensec_security->private_data;
	struct auth_serversupplied_info *server_info = NULL;
	struct auth_session_info *session_info = NULL;
	struct PAC_LOGON_INFO *logon_info;
	char *p;
	char *principal;
	const char *account_name;
	const char *realm;

	principal = talloc_strdup(gensec_krb5_state, gensec_krb5_state->peer_principal);
	NT_STATUS_HAVE_NO_MEMORY(principal);

	p = strchr(principal, '@');
	if (p) {
		*p = '\0';
		p++;
		realm = p;
	} else {
		realm = lp_realm();
	}
	account_name = principal;

	/* decode and verify the pac */
	nt_status = gensec_krb5_decode_pac(gensec_krb5_state, &logon_info, gensec_krb5_state->pac,
					   gensec_krb5_state);

	/* IF we have the PAC - otherwise we need to get this
	 * data from elsewere - local ldb, or (TODO) lookup of some
	 * kind... 
	 *
	 * when heimdal can generate the PAC, we should fail if there's
	 * no PAC present
	 */

	if (NT_STATUS_IS_OK(nt_status)) {
		union netr_Validation validation;
		validation.sam3 = &logon_info->info3;
		nt_status = make_server_info_netlogon_validation(gensec_krb5_state, 
								 account_name,
								 3, &validation,
								 &server_info); 
		talloc_free(principal);
		NT_STATUS_NOT_OK_RETURN(nt_status);
	} else {
		DATA_BLOB user_sess_key = data_blob(NULL, 0);
		DATA_BLOB lm_sess_key = data_blob(NULL, 0);
		/* TODO: should we pass the krb5 session key in here? */
		nt_status = sam_get_server_info(gensec_krb5_state, account_name, realm,
						user_sess_key, lm_sess_key,
						&server_info);
		talloc_free(principal);
		NT_STATUS_NOT_OK_RETURN(nt_status);
	}

	/* references the server_info into the session_info */
	nt_status = auth_generate_session_info(gensec_krb5_state, server_info, &session_info);
	talloc_free(server_info);
	NT_STATUS_NOT_OK_RETURN(nt_status);

	nt_status = gensec_krb5_session_key(gensec_security, &session_info->session_key);
	NT_STATUS_NOT_OK_RETURN(nt_status);

	*_session_info = session_info;

	return NT_STATUS_OK;
}

static BOOL gensec_krb5_have_feature(struct gensec_security *gensec_security,
				     uint32 feature)
{
	if (feature & GENSEC_FEATURE_SESSION_KEY) {
		return True;
	} 
	
	return False;
}


static const struct gensec_security_ops gensec_krb5_security_ops = {
	.name		= "krb5",
	.auth_type	= DCERPC_AUTH_TYPE_KRB5,
	.oid            = GENSEC_OID_KERBEROS5,
	.client_start   = gensec_krb5_client_start,
	.server_start   = gensec_krb5_server_start,
	.update 	= gensec_krb5_update,
	.session_key	= gensec_krb5_session_key,
	.session_info	= gensec_krb5_session_info,
	.have_feature   = gensec_krb5_have_feature,
	.enabled        = False
};

static const struct gensec_security_ops gensec_ms_krb5_security_ops = {
	.name		= "ms_krb5",
	.auth_type	= DCERPC_AUTH_TYPE_KRB5,
	.oid            = GENSEC_OID_KERBEROS5_OLD,
	.client_start   = gensec_krb5_client_start,
	.server_start   = gensec_krb5_server_start,
	.update 	= gensec_krb5_update,
	.session_key	= gensec_krb5_session_key,
	.session_info	= gensec_krb5_session_info,
	.have_feature   = gensec_krb5_have_feature,
	.enabled        = False
};


NTSTATUS gensec_krb5_init(void)
{
	NTSTATUS ret;

	ret = gensec_register(&gensec_krb5_security_ops);
	if (!NT_STATUS_IS_OK(ret)) {
		DEBUG(0,("Failed to register '%s' gensec backend!\n",
			gensec_krb5_security_ops.name));
		return ret;
	}

	ret = gensec_register(&gensec_ms_krb5_security_ops);
	if (!NT_STATUS_IS_OK(ret)) {
		DEBUG(0,("Failed to register '%s' gensec backend!\n",
			gensec_krb5_security_ops.name));
		return ret;
	}

	return ret;
}