r13679: Commiting the rm_primary_group.patch posted on samba-technical
[Samba.git] / source / libads / kerberos_verify.c
blob220bf14e32c0dc777a51668ed659186e3ee6a38e
1 /*
2 Unix SMB/CIFS implementation.
3 kerberos utility library
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Remus Koos 2001
6 Copyright (C) Luke Howard 2003
7 Copyright (C) Guenther Deschner 2003, 2005
8 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003
9 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
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 2 of the License, or
14 (at your option) any later version.
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.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "includes.h"
28 #ifdef HAVE_KRB5
30 #if !defined(HAVE_KRB5_PRINC_COMPONENT)
31 const krb5_data *krb5_princ_component(krb5_context, krb5_principal, int );
32 #endif
34 /**********************************************************************************
35 Try to verify a ticket using the system keytab... the system keytab has kvno -1 entries, so
36 it's more like what microsoft does... see comment in utils/net_ads.c in the
37 ads_keytab_add_entry function for details.
38 ***********************************************************************************/
40 static BOOL ads_keytab_verify_ticket(krb5_context context, krb5_auth_context auth_context,
41 const DATA_BLOB *ticket, krb5_data *p_packet, krb5_ticket **pp_tkt,
42 krb5_keyblock **keyblock)
44 krb5_error_code ret = 0;
45 BOOL auth_ok = False;
46 krb5_keytab keytab = NULL;
47 krb5_kt_cursor kt_cursor;
48 krb5_keytab_entry kt_entry;
49 char *valid_princ_formats[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
50 char *entry_princ_s = NULL;
51 fstring my_name, my_fqdn;
52 int i;
53 int number_matched_principals = 0;
55 /* Generate the list of principal names which we expect
56 * clients might want to use for authenticating to the file
57 * service. We allow name$,{host,cifs}/{name,fqdn,name.REALM}. */
59 fstrcpy(my_name, global_myname());
61 my_fqdn[0] = '\0';
62 name_to_fqdn(my_fqdn, global_myname());
64 asprintf(&valid_princ_formats[0], "%s$@%s", my_name, lp_realm());
65 asprintf(&valid_princ_formats[1], "host/%s@%s", my_name, lp_realm());
66 asprintf(&valid_princ_formats[2], "host/%s@%s", my_fqdn, lp_realm());
67 asprintf(&valid_princ_formats[3], "host/%s.%s@%s", my_name, lp_realm(), lp_realm());
68 asprintf(&valid_princ_formats[4], "cifs/%s@%s", my_name, lp_realm());
69 asprintf(&valid_princ_formats[5], "cifs/%s@%s", my_fqdn, lp_realm());
70 asprintf(&valid_princ_formats[6], "cifs/%s.%s@%s", my_name, lp_realm(), lp_realm());
72 ZERO_STRUCT(kt_entry);
73 ZERO_STRUCT(kt_cursor);
75 ret = krb5_kt_default(context, &keytab);
76 if (ret) {
77 DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_default failed (%s)\n", error_message(ret)));
78 goto out;
81 /* Iterate through the keytab. For each key, if the principal
82 * name case-insensitively matches one of the allowed formats,
83 * try verifying the ticket using that principal. */
85 ret = krb5_kt_start_seq_get(context, keytab, &kt_cursor);
86 if (ret) {
87 DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_start_seq_get failed (%s)\n", error_message(ret)));
88 goto out;
91 if (ret != KRB5_KT_END && ret != ENOENT ) {
92 while (!auth_ok && (krb5_kt_next_entry(context, keytab, &kt_entry, &kt_cursor) == 0)) {
93 ret = krb5_unparse_name(context, kt_entry.principal, &entry_princ_s);
94 if (ret) {
95 DEBUG(1, ("ads_keytab_verify_ticket: krb5_unparse_name failed (%s)\n", error_message(ret)));
96 goto out;
99 for (i = 0; i < sizeof(valid_princ_formats) / sizeof(valid_princ_formats[0]); i++) {
100 if (strequal(entry_princ_s, valid_princ_formats[i])) {
101 number_matched_principals++;
102 p_packet->length = ticket->length;
103 p_packet->data = (krb5_pointer)ticket->data;
104 *pp_tkt = NULL;
106 ret = krb5_rd_req_return_keyblock_from_keytab(context, &auth_context, p_packet,
107 kt_entry.principal, keytab,
108 NULL, pp_tkt, keyblock);
110 if (ret) {
111 DEBUG(10,("ads_keytab_verify_ticket: "
112 "krb5_rd_req_return_keyblock_from_keytab(%s) failed: %s\n",
113 entry_princ_s, error_message(ret)));
114 } else {
115 DEBUG(3,("ads_keytab_verify_ticket: "
116 "krb5_rd_req_return_keyblock_from_keytab succeeded for principal %s\n",
117 entry_princ_s));
118 auth_ok = True;
119 break;
124 /* Free the name we parsed. */
125 krb5_free_unparsed_name(context, entry_princ_s);
126 entry_princ_s = NULL;
128 /* Free the entry we just read. */
129 smb_krb5_kt_free_entry(context, &kt_entry);
130 ZERO_STRUCT(kt_entry);
132 krb5_kt_end_seq_get(context, keytab, &kt_cursor);
135 ZERO_STRUCT(kt_cursor);
137 out:
139 for (i = 0; i < sizeof(valid_princ_formats) / sizeof(valid_princ_formats[0]); i++) {
140 SAFE_FREE(valid_princ_formats[i]);
143 if (!auth_ok) {
144 if (!number_matched_principals) {
145 DEBUG(3, ("ads_keytab_verify_ticket: no keytab principals matched expected file service name.\n"));
146 } else {
147 DEBUG(3, ("ads_keytab_verify_ticket: krb5_rd_req failed for all %d matched keytab principals\n",
148 number_matched_principals));
152 if (entry_princ_s) {
153 krb5_free_unparsed_name(context, entry_princ_s);
157 krb5_keytab_entry zero_kt_entry;
158 ZERO_STRUCT(zero_kt_entry);
159 if (memcmp(&zero_kt_entry, &kt_entry, sizeof(krb5_keytab_entry))) {
160 smb_krb5_kt_free_entry(context, &kt_entry);
165 krb5_kt_cursor zero_csr;
166 ZERO_STRUCT(zero_csr);
167 if ((memcmp(&kt_cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) && keytab) {
168 krb5_kt_end_seq_get(context, keytab, &kt_cursor);
172 if (keytab) {
173 krb5_kt_close(context, keytab);
175 return auth_ok;
178 /**********************************************************************************
179 Try to verify a ticket using the secrets.tdb.
180 ***********************************************************************************/
182 static BOOL ads_secrets_verify_ticket(krb5_context context, krb5_auth_context auth_context,
183 krb5_principal host_princ,
184 const DATA_BLOB *ticket, krb5_data *p_packet, krb5_ticket **pp_tkt,
185 krb5_keyblock **keyblock)
187 krb5_error_code ret = 0;
188 BOOL auth_ok = False;
189 char *password_s = NULL;
190 krb5_data password;
191 krb5_enctype *enctypes = NULL;
192 int i;
194 ZERO_STRUCTP(keyblock);
196 if (!secrets_init()) {
197 DEBUG(1,("ads_secrets_verify_ticket: secrets_init failed\n"));
198 return False;
201 password_s = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
202 if (!password_s) {
203 DEBUG(1,("ads_secrets_verify_ticket: failed to fetch machine password\n"));
204 return False;
207 password.data = password_s;
208 password.length = strlen(password_s);
210 /* CIFS doesn't use addresses in tickets. This would break NAT. JRA */
212 if ((ret = get_kerberos_allowed_etypes(context, &enctypes))) {
213 DEBUG(1,("ads_secrets_verify_ticket: krb5_get_permitted_enctypes failed (%s)\n",
214 error_message(ret)));
215 goto out;
218 p_packet->length = ticket->length;
219 p_packet->data = (krb5_pointer)ticket->data;
221 /* We need to setup a auth context with each possible encoding type in turn. */
222 for (i=0;enctypes[i];i++) {
223 krb5_keyblock *key = NULL;
225 if (!(key = SMB_MALLOC_P(krb5_keyblock))) {
226 goto out;
229 if (create_kerberos_key_from_string(context, host_princ, &password, key, enctypes[i])) {
230 SAFE_FREE(key);
231 continue;
234 krb5_auth_con_setuseruserkey(context, auth_context, key);
236 if (!(ret = krb5_rd_req(context, &auth_context, p_packet,
237 NULL,
238 NULL, NULL, pp_tkt))) {
239 DEBUG(10,("ads_secrets_verify_ticket: enc type [%u] decrypted message !\n",
240 (unsigned int)enctypes[i] ));
241 auth_ok = True;
242 krb5_copy_keyblock(context, key, keyblock);
243 krb5_free_keyblock(context, key);
244 break;
247 DEBUG((ret != KRB5_BAD_ENCTYPE) ? 3 : 10,
248 ("ads_secrets_verify_ticket: enc type [%u] failed to decrypt with error %s\n",
249 (unsigned int)enctypes[i], error_message(ret)));
251 krb5_free_keyblock(context, key);
255 out:
257 free_kerberos_etypes(context, enctypes);
258 SAFE_FREE(password_s);
260 return auth_ok;
263 /**********************************************************************************
264 Verify an incoming ticket and parse out the principal name and
265 authorization_data if available.
266 ***********************************************************************************/
268 NTSTATUS ads_verify_ticket(TALLOC_CTX *mem_ctx,
269 const char *realm, const DATA_BLOB *ticket,
270 char **principal, PAC_DATA **pac_data,
271 DATA_BLOB *ap_rep,
272 DATA_BLOB *session_key)
274 NTSTATUS sret = NT_STATUS_LOGON_FAILURE;
275 NTSTATUS pac_ret;
276 DATA_BLOB auth_data;
277 krb5_context context = NULL;
278 krb5_auth_context auth_context = NULL;
279 krb5_data packet;
280 krb5_ticket *tkt = NULL;
281 krb5_rcache rcache = NULL;
282 krb5_keyblock *keyblock = NULL;
283 time_t authtime;
284 int ret;
286 krb5_principal host_princ = NULL;
287 krb5_const_principal client_principal = NULL;
288 char *host_princ_s = NULL;
289 BOOL got_replay_mutex = False;
291 BOOL auth_ok = False;
292 BOOL got_auth_data = False;
294 ZERO_STRUCT(packet);
295 ZERO_STRUCT(auth_data);
296 ZERO_STRUCTP(ap_rep);
297 ZERO_STRUCTP(session_key);
299 initialize_krb5_error_table();
300 ret = krb5_init_context(&context);
301 if (ret) {
302 DEBUG(1,("ads_verify_ticket: krb5_init_context failed (%s)\n", error_message(ret)));
303 return NT_STATUS_LOGON_FAILURE;
306 ret = krb5_set_default_realm(context, realm);
307 if (ret) {
308 DEBUG(1,("ads_verify_ticket: krb5_set_default_realm failed (%s)\n", error_message(ret)));
309 goto out;
312 /* This whole process is far more complex than I would
313 like. We have to go through all this to allow us to store
314 the secret internally, instead of using /etc/krb5.keytab */
316 ret = krb5_auth_con_init(context, &auth_context);
317 if (ret) {
318 DEBUG(1,("ads_verify_ticket: krb5_auth_con_init failed (%s)\n", error_message(ret)));
319 goto out;
322 asprintf(&host_princ_s, "%s$", global_myname());
323 strlower_m(host_princ_s);
324 ret = krb5_parse_name(context, host_princ_s, &host_princ);
325 if (ret) {
326 DEBUG(1,("ads_verify_ticket: krb5_parse_name(%s) failed (%s)\n",
327 host_princ_s, error_message(ret)));
328 goto out;
332 /* Lock a mutex surrounding the replay as there is no locking in the MIT krb5
333 * code surrounding the replay cache... */
335 if (!grab_server_mutex("replay cache mutex")) {
336 DEBUG(1,("ads_verify_ticket: unable to protect replay cache with mutex.\n"));
337 goto out;
340 got_replay_mutex = True;
343 * JRA. We must set the rcache here. This will prevent replay attacks.
346 ret = krb5_get_server_rcache(context, krb5_princ_component(context, host_princ, 0), &rcache);
347 if (ret) {
348 DEBUG(1,("ads_verify_ticket: krb5_get_server_rcache failed (%s)\n", error_message(ret)));
349 goto out;
352 ret = krb5_auth_con_setrcache(context, auth_context, rcache);
353 if (ret) {
354 DEBUG(1,("ads_verify_ticket: krb5_auth_con_setrcache failed (%s)\n", error_message(ret)));
355 goto out;
358 if (lp_use_kerberos_keytab()) {
359 auth_ok = ads_keytab_verify_ticket(context, auth_context, ticket, &packet, &tkt, &keyblock);
361 if (!auth_ok) {
362 auth_ok = ads_secrets_verify_ticket(context, auth_context, host_princ,
363 ticket, &packet, &tkt, &keyblock);
366 release_server_mutex();
367 got_replay_mutex = False;
369 #if 0
370 /* Heimdal leaks here, if we fix the leak, MIT crashes */
371 if (rcache) {
372 krb5_rc_close(context, rcache);
374 #endif
376 if (!auth_ok) {
377 DEBUG(3,("ads_verify_ticket: krb5_rd_req with auth failed (%s)\n",
378 error_message(ret)));
379 goto out;
380 } else {
381 authtime = get_authtime_from_tkt(tkt);
382 client_principal = get_principal_from_tkt(tkt);
385 ret = krb5_mk_rep(context, auth_context, &packet);
386 if (ret) {
387 DEBUG(3,("ads_verify_ticket: Failed to generate mutual authentication reply (%s)\n",
388 error_message(ret)));
389 goto out;
392 *ap_rep = data_blob(packet.data, packet.length);
393 SAFE_FREE(packet.data);
394 packet.length = 0;
396 get_krb5_smb_session_key(context, auth_context, session_key, True);
397 dump_data_pw("SMB session key (from ticket)\n", session_key->data, session_key->length);
399 #if 0
400 file_save("/tmp/ticket.dat", ticket->data, ticket->length);
401 #endif
403 /* continue when no PAC is retrieved or we couldn't decode the PAC
404 (like accounts that have the UF_NO_AUTH_DATA_REQUIRED flag set, or
405 Kerberos tickets encrypted using a DES key) - Guenther */
407 got_auth_data = get_auth_data_from_tkt(mem_ctx, &auth_data, tkt);
408 if (!got_auth_data) {
409 DEBUG(3,("ads_verify_ticket: did not retrieve auth data. continuing without PAC\n"));
412 if (got_auth_data && pac_data != NULL) {
414 pac_ret = decode_pac_data(mem_ctx, &auth_data, context, keyblock, client_principal, authtime, pac_data);
415 if (!NT_STATUS_IS_OK(pac_ret)) {
416 DEBUG(3,("ads_verify_ticket: failed to decode PAC_DATA: %s\n", nt_errstr(pac_ret)));
417 *pac_data = NULL;
419 data_blob_free(&auth_data);
422 #if 0
423 #if defined(HAVE_KRB5_TKT_ENC_PART2)
424 /* MIT */
425 if (tkt->enc_part2) {
426 file_save("/tmp/authdata.dat",
427 tkt->enc_part2->authorization_data[0]->contents,
428 tkt->enc_part2->authorization_data[0]->length);
430 #else
431 /* Heimdal */
432 if (tkt->ticket.authorization_data) {
433 file_save("/tmp/authdata.dat",
434 tkt->ticket.authorization_data->val->ad_data.data,
435 tkt->ticket.authorization_data->val->ad_data.length);
437 #endif
438 #endif
440 if ((ret = krb5_unparse_name(context, get_principal_from_tkt(tkt),
441 principal))) {
442 DEBUG(3,("ads_verify_ticket: krb5_unparse_name failed (%s)\n",
443 error_message(ret)));
444 sret = NT_STATUS_LOGON_FAILURE;
445 goto out;
448 sret = NT_STATUS_OK;
450 out:
452 if (got_replay_mutex) {
453 release_server_mutex();
456 if (!NT_STATUS_IS_OK(sret)) {
457 data_blob_free(&auth_data);
460 if (!NT_STATUS_IS_OK(sret)) {
461 data_blob_free(ap_rep);
464 if (host_princ) {
465 krb5_free_principal(context, host_princ);
468 if (keyblock) {
469 krb5_free_keyblock(context, keyblock);
472 if (tkt != NULL) {
473 krb5_free_ticket(context, tkt);
476 SAFE_FREE(host_princ_s);
478 if (auth_context) {
479 krb5_auth_con_free(context, auth_context);
482 if (context) {
483 krb5_free_context(context);
486 return sret;
489 #endif /* HAVE_KRB5 */