summaryrefslogtreecommitdiff
path: root/source3/libads/kerberos_verify.c
blob: 4ae89aa01f3fdef53b4936d970e05a28720ed8ea (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
/* 
   Unix SMB/CIFS implementation.
   kerberos utility library
   Copyright (C) Andrew Tridgell 2001
   Copyright (C) Remus Koos 2001
   Copyright (C) Luke Howard 2003   
   Copyright (C) Guenther Deschner 2003
   Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003
   
   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"

#ifdef HAVE_KRB5

static void free_keytab(krb5_context context, krb5_keytab keytab)
{
	int ret=0;
	
	if (keytab) 
		ret = krb5_kt_close(context, keytab);
	if (ret) {
		DEBUG(3, ("krb5_kt_close failed (%s)\n",
			  error_message(ret)));
	}
}

#ifdef HAVE_MEMORY_KEYTAB
static krb5_error_code create_keytab(krb5_context context,
				     krb5_principal host_princ,
				     char *host_princ_s,
				     krb5_data password,
				     krb5_enctype *enctypes,
				     krb5_keytab *keytab,
				     char *keytab_name)
{
	krb5_keytab_entry entry;
	krb5_kvno kvno = 1;
	krb5_error_code ret;
	krb5_keyblock *key;
	int i;

	DEBUG(10,("creating keytab: %s\n", keytab_name));
	ret = krb5_kt_resolve(context, keytab_name, keytab);
	if (ret) 
		return ret;

	if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
		return ENOMEM;
	}
	
	/* add keytab entries for all encryption types */
	for ( i=0; enctypes[i]; i++ ) {
		
		if (create_kerberos_key_from_string(context, host_princ, &password, key, enctypes[i])) {
			continue;
		}

		entry.principal = host_princ;
		entry.vno       = kvno;
		/* this will have to be detected in configure...heimdal
		   calls it keyblock, MIT calls it key, but it does not
		   matter we are creating keytabs with MIT */
		entry.keyblock  = *key;

		DEBUG(10,("adding keytab-entry for (%s) with encryption type (%d)\n",
				host_princ_s, enctypes[i]));
		ret = krb5_kt_add_entry(context, *keytab, &entry);
		if (ret) {
			DEBUG(1,("adding entry to keytab failed (%s)\n", 
				 error_message(ret)));
			free_keytab(context, *keytab);
			return ret;
		}
	}
	krb5_free_keyblock(context, key);
	
	return 0;
}
#endif

static BOOL setup_keytab(krb5_context context,
			 krb5_principal host_princ,
			 char *host_princ_s,
			 krb5_data password,
			 krb5_enctype *enctypes,
			 krb5_keytab *keytab)
{
	char *keytab_name = NULL;
	krb5_error_code ret;

	/* check if we have to setup a keytab - not currently enabled
	   I've put this in so that the else block below functions 
	   the same way that it will when this code is turned on */
	if (0 /* will later be *lp_keytab() */) {

		/* use a file-keytab */
		asprintf(&keytab_name, "%s:%s", 
			 "" 
			 /* KRB5_KT_FILE_PREFIX, "FILE" or 
			    "WRFILE" depending on HEeimdal or MIT */, 
			 "" /* will later be lp_keytab() */);

	        DEBUG(10,("will use filebased keytab: %s\n", keytab_name));
	        ret = krb5_kt_resolve(context, keytab_name, keytab);
		if (ret) {
			DEBUG(3,("cannot resolve keytab name %s (%s)\n",
				 keytab_name, 
				 error_message(ret)));
			SAFE_FREE(keytab_name);
			return False;
		}

	}

#if defined(HAVE_MEMORY_KEYTAB)
	else {

		/* setup a in-memory-keytab */
		asprintf(&keytab_name, "MEMORY:");

  		ret = create_keytab(context, host_princ, host_princ_s, password, enctypes, 
			keytab, keytab_name);
		if (ret) {
			DEBUG(3,("unable to create MEMORY: keytab (%s)\n",
				 error_message(ret)));
			SAFE_FREE(keytab_name);
			return False;
		}
	}
#endif
	SAFE_FREE(keytab_name);
	return True;
}
	

/*
  verify an incoming ticket and parse out the principal name and 
  authorization_data if available 
*/
NTSTATUS ads_verify_ticket(const char *realm, const DATA_BLOB *ticket, 
			   char **principal, DATA_BLOB *auth_data,
			   DATA_BLOB *ap_rep,
			   uint8 session_key[16])
{
	NTSTATUS sret = NT_STATUS_LOGON_FAILURE;
	krb5_context context = NULL;
	krb5_auth_context auth_context = NULL;
	krb5_keytab keytab = NULL;
	krb5_data packet;
	krb5_ticket *tkt = NULL;
	krb5_rcache rcache = NULL;
	int ret, i;
	krb5_keyblock *key = NULL;
	krb5_principal host_princ;
	char *host_princ_s = NULL;
	fstring myname;
	char *password_s = NULL;
	krb5_data password;
	krb5_enctype *enctypes = NULL;
#if 0
	krb5_address local_addr;
	krb5_address remote_addr;
#endif
	BOOL auth_ok = False;

	ZERO_STRUCT(packet);
	ZERO_STRUCT(password);
	ZERO_STRUCTP(auth_data);
	ZERO_STRUCTP(ap_rep);

	if (!secrets_init()) {
		DEBUG(1,("ads_verify_ticket: secrets_init failed\n"));
		return NT_STATUS_LOGON_FAILURE;
	}

	password_s = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
	if (!password_s) {
		DEBUG(1,("ads_verify_ticket: failed to fetch machine password\n"));
		return NT_STATUS_LOGON_FAILURE;
	}

	password.data = password_s;
	password.length = strlen(password_s);

	ret = krb5_init_context(&context);
	if (ret) {
		DEBUG(1,("ads_verify_ticket: krb5_init_context failed (%s)\n", error_message(ret)));
		return NT_STATUS_LOGON_FAILURE;
	}

	ret = krb5_set_default_realm(context, realm);
	if (ret) {
		DEBUG(1,("ads_verify_ticket: krb5_set_default_realm failed (%s)\n", error_message(ret)));
		sret = NT_STATUS_LOGON_FAILURE;
		goto out;
	}

	/* This whole process is far more complex than I would
           like. We have to go through all this to allow us to store
           the secret internally, instead of using /etc/krb5.keytab */

	ret = krb5_auth_con_init(context, &auth_context);
	if (ret) {
		DEBUG(1,("ads_verify_ticket: krb5_auth_con_init failed (%s)\n", error_message(ret)));
		sret = NT_STATUS_LOGON_FAILURE;
		goto out;
	}

	fstrcpy(myname, global_myname());
	strlower_m(myname);
	asprintf(&host_princ_s, "HOST/%s@%s", myname, lp_realm());
	ret = krb5_parse_name(context, host_princ_s, &host_princ);
	if (ret) {
		DEBUG(1,("ads_verify_ticket: krb5_parse_name(%s) failed (%s)\n",
					host_princ_s, error_message(ret)));
		sret = NT_STATUS_LOGON_FAILURE;
		goto out;
	}

	/*
	 * JRA. We must set the rcache here. This will prevent replay attacks.
	 */

	ret = krb5_get_server_rcache(context, krb5_princ_component(context, host_princ, 0), &rcache);
	if (ret) {
		DEBUG(1,("ads_verify_ticket: krb5_get_server_rcache failed (%s)\n", error_message(ret)));
		sret = NT_STATUS_LOGON_FAILURE;
		goto out;
	}

	ret = krb5_auth_con_setrcache(context, auth_context, rcache);
	if (ret) {
		DEBUG(1,("ads_verify_ticket: krb5_auth_con_setrcache failed (%s)\n", error_message(ret)));
		sret = NT_STATUS_LOGON_FAILURE;
		goto out;
	}

	/* CIFS doesn't use addresses in tickets. This would breat NAT. JRA */

	if ((ret = get_kerberos_allowed_etypes(context, &enctypes))) {
		DEBUG(1,("ads_verify_ticket: krb5_get_permitted_enctypes failed (%s)\n", 
			 error_message(ret)));
		sret = NT_STATUS_LOGON_FAILURE;
		goto out;
	}

	/* Lock a mutex surrounding the replay as there is no locking in the MIT krb5
	 * code surrounding the replay cache... */

	if (!grab_server_mutex("replay cache mutex")) {
		DEBUG(1,("ads_verify_ticket: unable to protect replay cache with mutex.\n"));
		sret = NT_STATUS_LOGON_FAILURE;
		goto out;
	}

	if (!setup_keytab(context, host_princ, host_princ_s, password,
			  enctypes, &keytab)) {
		DEBUG(3,("ads_verify_ticket: unable to setup keytab\n"));
		sret = NT_STATUS_LOGON_FAILURE;
		goto out;
	}
	
	/* We need to setup a auth context with each possible encoding type in turn. */
	for (i=0;enctypes[i];i++) {
		if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
			sret = NT_STATUS_NO_MEMORY;
			goto out;
		}
	
		if (create_kerberos_key_from_string(context, host_princ, &password, key, enctypes[i])) {
			continue;
		}

		krb5_auth_con_setuseruserkey(context, auth_context, key);

		krb5_free_keyblock(context, key);

		packet.length = ticket->length;
		packet.data = (krb5_pointer)ticket->data;

		if (!(ret = krb5_rd_req(context, &auth_context, &packet, 
#ifdef HAVE_MEMORY_KEYTAB
					host_princ, 
#else
					NULL,
#endif
					keytab, NULL, &tkt))) {
			DEBUG(10,("ads_verify_ticket: enc type [%u] decrypted message !\n",
				(unsigned int)enctypes[i] ));
			auth_ok = True;
			break;
		}
	
		DEBUG((ret != KRB5_BAD_ENCTYPE) ? 3 : 10,
				("ads_verify_ticket: enc type [%u] failed to decrypt with error %s\n",
				(unsigned int)enctypes[i], error_message(ret)));
	}

	release_server_mutex();

	if (!auth_ok) {
		DEBUG(3,("ads_verify_ticket: krb5_rd_req with auth failed (%s)\n", 
			 error_message(ret)));
		sret = NT_STATUS_LOGON_FAILURE;
		goto out;
	}

	ret = krb5_mk_rep(context, auth_context, &packet);
	if (ret) {
		DEBUG(3,("ads_verify_ticket: Failed to generate mutual authentication reply (%s)\n",
			error_message(ret)));
		sret = NT_STATUS_LOGON_FAILURE;
		goto out;
	}

	*ap_rep = data_blob(packet.data, packet.length);
	free(packet.data);

	get_krb5_smb_session_key(context, auth_context, session_key, True);
#ifdef DEBUG_PASSWORD
	DEBUG(10,("SMB session key (from ticket) follows:\n"));
	dump_data(10, session_key, 16);
#endif

#if 0
	file_save("/tmp/ticket.dat", ticket->data, ticket->length);
#endif

	get_auth_data_from_tkt(auth_data, tkt);

	{
		TALLOC_CTX *ctx = talloc_init("pac data");
		decode_pac_data(auth_data, ctx);
		talloc_destroy(ctx);
	}

#if 0
	if (tkt->enc_part2) {
		file_save("/tmp/authdata.dat",
			  tkt->enc_part2->authorization_data[0]->contents,
			  tkt->enc_part2->authorization_data[0]->length);
	}
#endif

		
	/* get rid of all resources associated with the keytab */
	if (keytab) free_keytab(context, keytab);
		
	if ((ret = krb5_unparse_name(context, get_principal_from_tkt(tkt),
				     principal))) {
		DEBUG(3,("ads_verify_ticket: krb5_unparse_name failed (%s)\n", 
			 error_message(ret)));
		sret = NT_STATUS_LOGON_FAILURE;
		goto out;
	}

	sret = NT_STATUS_OK;

 out:

	if (!NT_STATUS_IS_OK(sret))
		data_blob_free(auth_data);

	if (!NT_STATUS_IS_OK(sret))
		data_blob_free(ap_rep);

	krb5_free_principal(context, host_princ);
	if (tkt != NULL)
		krb5_free_ticket(context, tkt);
	free_kerberos_etypes(context, enctypes);
	SAFE_FREE(password_s);
	SAFE_FREE(host_princ_s);

	if (auth_context)
		krb5_auth_con_free(context, auth_context);

	if (context)
		krb5_free_context(context);

	return sret;
}

#endif /* HAVE_KRB5 */