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
10 Copyright (C) Jeremy Allison 2007
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
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/>.
28 #include "libads/kerberos_proto.h"
33 #if !defined(HAVE_KRB5_PRINC_COMPONENT)
34 const krb5_data *krb5_princ_component(krb5_context, krb5_principal, int );
37 static bool ads_dedicated_keytab_verify_ticket(krb5_context context,
38 krb5_auth_context auth_context,
39 const DATA_BLOB *ticket,
41 krb5_keyblock **keyblock,
42 krb5_error_code *perr)
44 krb5_error_code ret = 0;
46 krb5_keytab keytab = NULL;
47 krb5_keytab_entry kt_entry;
48 krb5_ticket *dec_ticket = NULL;
58 ZERO_STRUCT(kt_entry);
60 ret = smb_krb5_open_keytab(context, lp_dedicated_keytab_file(), true,
63 DEBUG(1, ("smb_krb5_open_keytab failed (%s)\n",
68 packet.length = ticket->length;
69 packet.data = (char *)ticket->data;
71 ret = krb5_rd_req(context, &auth_context, &packet, NULL, keytab,
74 DEBUG(0, ("krb5_rd_req failed (%s)\n", error_message(ret)));
78 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
79 enctype = dec_ticket->ticket.key.keytype;
81 enctype = dec_ticket->enc_part.enctype;
82 kvno = dec_ticket->enc_part.kvno;
85 /* Get the key for checking the pac signature */
86 ret = krb5_kt_get_entry(context, keytab, dec_ticket->server,
87 kvno, enctype, &kt_entry);
89 DEBUG(0, ("krb5_kt_get_entry failed (%s)\n",
94 ret = krb5_copy_keyblock(context, KRB5_KT_KEY(&kt_entry), keyblock);
95 smb_krb5_kt_free_entry(context, &kt_entry);
98 DEBUG(0, ("failed to copy key: %s\n",
104 *pp_tkt = dec_ticket;
109 krb5_free_ticket(context, dec_ticket);
112 krb5_kt_close(context, keytab);
118 /******************************************************************************
119 Try to verify a ticket using the system keytab... the system keytab has
120 kvno -1 entries, so it's more like what microsoft does... see comment in
121 utils/net_ads.c in the ads_keytab_add_entry function for details.
122 ******************************************************************************/
124 static bool ads_keytab_verify_ticket(krb5_context context,
125 krb5_auth_context auth_context,
126 const DATA_BLOB *ticket,
127 krb5_ticket **pp_tkt,
128 krb5_keyblock **keyblock,
129 krb5_error_code *perr)
131 krb5_error_code ret = 0;
132 bool auth_ok = False;
133 krb5_keytab keytab = NULL;
134 krb5_kt_cursor kt_cursor;
135 krb5_keytab_entry kt_entry;
136 char *valid_princ_formats[7] = { NULL, NULL, NULL,
137 NULL, NULL, NULL, NULL };
138 char *entry_princ_s = NULL;
139 fstring my_name, my_fqdn;
141 int number_matched_principals = 0;
149 /* Generate the list of principal names which we expect
150 * clients might want to use for authenticating to the file
151 * service. We allow name$,{host,cifs}/{name,fqdn,name.REALM}. */
153 fstrcpy(my_name, global_myname());
156 name_to_fqdn(my_fqdn, global_myname());
158 err = asprintf(&valid_princ_formats[0],
159 "%s$@%s", my_name, lp_realm());
163 err = asprintf(&valid_princ_formats[1],
164 "host/%s@%s", my_name, lp_realm());
168 err = asprintf(&valid_princ_formats[2],
169 "host/%s@%s", my_fqdn, lp_realm());
173 err = asprintf(&valid_princ_formats[3],
174 "host/%s.%s@%s", my_name, lp_realm(), lp_realm());
178 err = asprintf(&valid_princ_formats[4],
179 "cifs/%s@%s", my_name, lp_realm());
183 err = asprintf(&valid_princ_formats[5],
184 "cifs/%s@%s", my_fqdn, lp_realm());
188 err = asprintf(&valid_princ_formats[6],
189 "cifs/%s.%s@%s", my_name, lp_realm(), lp_realm());
194 ZERO_STRUCT(kt_entry);
195 ZERO_STRUCT(kt_cursor);
197 ret = smb_krb5_open_keytab(context, NULL, False, &keytab);
199 DEBUG(1, (__location__ ": smb_krb5_open_keytab failed (%s)\n",
200 error_message(ret)));
204 /* Iterate through the keytab. For each key, if the principal
205 * name case-insensitively matches one of the allowed formats,
206 * try verifying the ticket using that principal. */
208 ret = krb5_kt_start_seq_get(context, keytab, &kt_cursor);
210 DEBUG(1, (__location__ ": krb5_kt_start_seq_get failed (%s)\n",
211 error_message(ret)));
216 (krb5_kt_next_entry(context, keytab,
217 &kt_entry, &kt_cursor) == 0)) {
218 ret = smb_krb5_unparse_name(talloc_tos(), context,
222 DEBUG(1, (__location__ ": smb_krb5_unparse_name "
223 "failed (%s)\n", error_message(ret)));
227 for (i = 0; i < ARRAY_SIZE(valid_princ_formats); i++) {
229 if (!strequal(entry_princ_s, valid_princ_formats[i])) {
233 number_matched_principals++;
234 packet.length = ticket->length;
235 packet.data = (char *)ticket->data;
238 ret = krb5_rd_req_return_keyblock_from_keytab(context,
239 &auth_context, &packet,
240 kt_entry.principal, keytab,
241 NULL, pp_tkt, keyblock);
244 DEBUG(10, (__location__ ": krb5_rd_req_return"
245 "_keyblock_from_keytab(%s) "
246 "failed: %s\n", entry_princ_s,
247 error_message(ret)));
249 /* workaround for MIT:
250 * as krb5_ktfile_get_entry will explicitly
251 * close the krb5_keytab as soon as krb5_rd_req
252 * has successfully decrypted the ticket but the
253 * ticket is not valid yet (due to clockskew)
254 * there is no point in querying more keytab
255 * entries - Guenther */
257 if (ret == KRB5KRB_AP_ERR_TKT_NYV ||
258 ret == KRB5KRB_AP_ERR_TKT_EXPIRED ||
259 ret == KRB5KRB_AP_ERR_SKEW) {
263 DEBUG(3, (__location__ ": krb5_rd_req_return"
264 "_keyblock_from_keytab succeeded "
265 "for principal %s\n",
272 /* Free the name we parsed. */
273 TALLOC_FREE(entry_princ_s);
275 /* Free the entry we just read. */
276 smb_krb5_kt_free_entry(context, &kt_entry);
277 ZERO_STRUCT(kt_entry);
279 krb5_kt_end_seq_get(context, keytab, &kt_cursor);
281 ZERO_STRUCT(kt_cursor);
285 for (i = 0; i < ARRAY_SIZE(valid_princ_formats); i++) {
286 SAFE_FREE(valid_princ_formats[i]);
290 if (!number_matched_principals) {
291 DEBUG(3, (__location__ ": no keytab principals "
292 "matched expected file service name.\n"));
294 DEBUG(3, (__location__ ": krb5_rd_req failed for "
295 "all %d matched keytab principals\n",
296 number_matched_principals));
300 SAFE_FREE(entry_princ_s);
303 krb5_keytab_entry zero_kt_entry;
304 ZERO_STRUCT(zero_kt_entry);
305 if (memcmp(&zero_kt_entry, &kt_entry,
306 sizeof(krb5_keytab_entry))) {
307 smb_krb5_kt_free_entry(context, &kt_entry);
312 krb5_kt_cursor zero_csr;
313 ZERO_STRUCT(zero_csr);
314 if ((memcmp(&kt_cursor, &zero_csr,
315 sizeof(krb5_kt_cursor)) != 0) && keytab) {
316 krb5_kt_end_seq_get(context, keytab, &kt_cursor);
321 krb5_kt_close(context, keytab);
327 /*****************************************************************************
328 Try to verify a ticket using the secrets.tdb.
329 ******************************************************************************/
331 static krb5_error_code ads_secrets_verify_ticket(krb5_context context,
332 krb5_auth_context auth_context,
333 krb5_principal host_princ,
334 const DATA_BLOB *ticket,
335 krb5_ticket **pp_tkt,
336 krb5_keyblock **keyblock,
337 krb5_error_code *perr)
339 krb5_error_code ret = 0;
340 bool auth_ok = False;
342 char *password_s = NULL;
343 /* Let's make some room for 2 password (old and new)*/
344 krb5_data passwords[2];
345 krb5_enctype enctypes[] = {
346 #if defined(ENCTYPE_ARCFOUR_HMAC)
347 ENCTYPE_ARCFOUR_HMAC,
360 ZERO_STRUCT(passwords);
362 if (!secrets_init()) {
363 DEBUG(1,("ads_secrets_verify_ticket: secrets_init failed\n"));
364 *perr = KRB5_CONFIG_CANTOPEN;
368 password_s = secrets_fetch_machine_password(lp_workgroup(),
371 DEBUG(1,(__location__ ": failed to fetch machine password\n"));
372 *perr = KRB5_LIBOS_CANTREADPWD;
376 passwords[0].data = password_s;
377 passwords[0].length = strlen(password_s);
379 password_s = secrets_fetch_prev_machine_password(lp_workgroup());
381 DEBUG(10, (__location__ ": found previous password\n"));
382 passwords[1].data = password_s;
383 passwords[1].length = strlen(password_s);
386 /* CIFS doesn't use addresses in tickets. This would break NAT. JRA */
388 packet.length = ticket->length;
389 packet.data = (char *)ticket->data;
391 /* We need to setup a auth context with each possible encoding type
393 for (j=0; j<2 && passwords[j].length; j++) {
395 for (i=0;enctypes[i];i++) {
396 krb5_keyblock *key = NULL;
398 if (!(key = SMB_MALLOC_P(krb5_keyblock))) {
403 if (create_kerberos_key_from_string(context,
404 host_princ, &passwords[j],
405 key, enctypes[i], false)) {
410 krb5_auth_con_setuseruserkey(context,
413 if (!(ret = krb5_rd_req(context, &auth_context,
416 DEBUG(10, (__location__ ": enc type [%u] "
417 "decrypted message !\n",
418 (unsigned int)enctypes[i]));
421 krb5_copy_keyblock(context, key, keyblock);
422 krb5_free_keyblock(context, key);
426 DEBUG((ret != KRB5_BAD_ENCTYPE) ? 3 : 10,
427 (__location__ ": enc type [%u] failed to "
428 "decrypt with error %s\n",
429 (unsigned int)enctypes[i],
430 error_message(ret)));
432 /* successfully decrypted but ticket is just not
433 * valid at the moment */
434 if (ret == KRB5KRB_AP_ERR_TKT_NYV ||
435 ret == KRB5KRB_AP_ERR_TKT_EXPIRED ||
436 ret == KRB5KRB_AP_ERR_SKEW) {
437 krb5_free_keyblock(context, key);
442 krb5_free_keyblock(context, key);
445 /* If we found a valid pass then no need to try
446 * the next one or we have invalid ticket so no need
447 * to try next password*/
453 SAFE_FREE(passwords[0].data);
454 SAFE_FREE(passwords[1].data);
459 /*****************************************************************************
460 Verify an incoming ticket and parse out the principal name and
461 authorization_data if available.
462 ******************************************************************************/
464 NTSTATUS ads_verify_ticket(TALLOC_CTX *mem_ctx,
467 const DATA_BLOB *ticket,
469 struct PAC_LOGON_INFO **logon_info,
471 DATA_BLOB *session_key,
472 bool use_replay_cache)
474 NTSTATUS sret = NT_STATUS_LOGON_FAILURE;
477 krb5_context context = NULL;
478 krb5_auth_context auth_context = NULL;
480 krb5_ticket *tkt = NULL;
481 krb5_rcache rcache = NULL;
482 krb5_keyblock *keyblock = NULL;
484 krb5_error_code ret = 0;
486 krb5_principal host_princ = NULL;
487 krb5_const_principal client_principal = NULL;
488 char *host_princ_s = NULL;
489 bool auth_ok = False;
490 bool got_auth_data = False;
491 struct named_mutex *mutex = NULL;
494 ZERO_STRUCT(auth_data);
498 *ap_rep = data_blob_null;
499 *session_key = data_blob_null;
501 initialize_krb5_error_table();
502 ret = krb5_init_context(&context);
504 DEBUG(1, (__location__ ": krb5_init_context failed (%s)\n",
505 error_message(ret)));
506 return NT_STATUS_LOGON_FAILURE;
509 if (time_offset != 0) {
510 krb5_set_real_time(context, time(NULL) + time_offset, 0);
513 ret = krb5_set_default_realm(context, realm);
515 DEBUG(1, (__location__ ": krb5_set_default_realm "
516 "failed (%s)\n", error_message(ret)));
520 /* This whole process is far more complex than I would
521 like. We have to go through all this to allow us to store
522 the secret internally, instead of using /etc/krb5.keytab */
524 ret = krb5_auth_con_init(context, &auth_context);
526 DEBUG(1, (__location__ ": krb5_auth_con_init failed (%s)\n",
527 error_message(ret)));
531 krb5_auth_con_getflags( context, auth_context, &flags );
532 if ( !use_replay_cache ) {
533 /* Disable default use of a replay cache */
534 flags &= ~KRB5_AUTH_CONTEXT_DO_TIME;
535 krb5_auth_con_setflags( context, auth_context, flags );
538 if (asprintf(&host_princ_s, "%s$", global_myname()) == -1) {
542 strlower_m(host_princ_s);
543 ret = smb_krb5_parse_name(context, host_princ_s, &host_princ);
545 DEBUG(1, (__location__ ": smb_krb5_parse_name(%s) "
546 "failed (%s)\n", host_princ_s, error_message(ret)));
551 if (use_replay_cache) {
553 /* Lock a mutex surrounding the replay as there is no
554 locking in the MIT krb5 code surrounding the replay
557 mutex = grab_named_mutex(talloc_tos(),
558 "replay cache mutex", 10);
560 DEBUG(1, (__location__ ": unable to protect replay "
561 "cache with mutex.\n"));
566 /* JRA. We must set the rcache here. This will prevent
569 ret = krb5_get_server_rcache(
571 krb5_princ_component(context, host_princ, 0),
574 DEBUG(1, (__location__ ": krb5_get_server_rcache "
575 "failed (%s)\n", error_message(ret)));
579 ret = krb5_auth_con_setrcache(context, auth_context, rcache);
581 DEBUG(1, (__location__ ": krb5_auth_con_setrcache "
582 "failed (%s)\n", error_message(ret)));
587 switch (lp_kerberos_method()) {
589 case KERBEROS_VERIFY_SECRETS:
590 auth_ok = ads_secrets_verify_ticket(context, auth_context,
591 host_princ, ticket, &tkt, &keyblock, &ret);
593 case KERBEROS_VERIFY_SYSTEM_KEYTAB:
594 auth_ok = ads_keytab_verify_ticket(context, auth_context,
595 ticket, &tkt, &keyblock, &ret);
597 case KERBEROS_VERIFY_DEDICATED_KEYTAB:
598 auth_ok = ads_dedicated_keytab_verify_ticket(context,
599 auth_context, ticket, &tkt, &keyblock, &ret);
601 case KERBEROS_VERIFY_SECRETS_AND_KEYTAB:
602 /* First try secrets.tdb and fallback to the krb5.keytab if
603 necessary. This is the pre 3.4 behavior when
604 "use kerberos keytab" was true.*/
605 auth_ok = ads_secrets_verify_ticket(context, auth_context,
606 host_princ, ticket, &tkt, &keyblock, &ret);
609 /* Only fallback if we failed to decrypt the ticket */
610 if (ret != KRB5KRB_AP_ERR_TKT_NYV &&
611 ret != KRB5KRB_AP_ERR_TKT_EXPIRED &&
612 ret != KRB5KRB_AP_ERR_SKEW) {
613 auth_ok = ads_keytab_verify_ticket(context,
614 auth_context, ticket, &tkt, &keyblock,
621 if (use_replay_cache) {
624 /* Heimdal leaks here, if we fix the leak, MIT crashes */
626 krb5_rc_close(context, rcache);
632 DEBUG(3, (__location__ ": krb5_rd_req with auth "
633 "failed (%s)\n", error_message(ret)));
634 /* Try map the error return in case it's something like
635 * a clock skew error.
637 sret = krb5_to_nt_status(ret);
638 if (NT_STATUS_IS_OK(sret) ||
639 NT_STATUS_EQUAL(sret,NT_STATUS_UNSUCCESSFUL)) {
640 sret = NT_STATUS_LOGON_FAILURE;
642 DEBUG(10, (__location__ ": returning error %s\n",
647 authtime = get_authtime_from_tkt(tkt);
648 client_principal = get_principal_from_tkt(tkt);
650 ret = krb5_mk_rep(context, auth_context, &packet);
652 DEBUG(3, (__location__ ": Failed to generate mutual "
653 "authentication reply (%s)\n", error_message(ret)));
657 *ap_rep = data_blob(packet.data, packet.length);
659 kerberos_free_data_contents(context, &packet);
663 get_krb5_smb_session_key(mem_ctx, context,
664 auth_context, session_key, true);
665 dump_data_pw("SMB session key (from ticket)\n",
666 session_key->data, session_key->length);
669 file_save("/tmp/ticket.dat", ticket->data, ticket->length);
672 /* continue when no PAC is retrieved or we couldn't decode the PAC
673 (like accounts that have the UF_NO_AUTH_DATA_REQUIRED flag set, or
674 Kerberos tickets encrypted using a DES key) - Guenther */
676 got_auth_data = get_auth_data_from_tkt(mem_ctx, &auth_data, tkt);
677 if (!got_auth_data) {
678 DEBUG(3, (__location__ ": did not retrieve auth data. "
679 "continuing without PAC\n"));
683 struct PAC_DATA *pac_data;
684 pac_ret = decode_pac_data(mem_ctx, &auth_data, context,
685 keyblock, client_principal,
686 authtime, &pac_data);
687 data_blob_free(&auth_data);
688 if (!NT_STATUS_IS_OK(pac_ret)) {
689 DEBUG(3, (__location__ ": failed to decode "
690 "PAC_DATA: %s\n", nt_errstr(pac_ret)));
693 for (i = 0; i < pac_data->num_buffers; i++) {
695 if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
699 *logon_info = pac_data->buffers[i].info->logon_info.info;
703 DEBUG(1, ("correctly decoded PAC but found "
705 "This should not happen\n"));
706 return NT_STATUS_INVALID_USER_BUFFER;
712 #if defined(HAVE_KRB5_TKT_ENC_PART2)
714 if (tkt->enc_part2) {
715 file_save("/tmp/authdata.dat",
716 tkt->enc_part2->authorization_data[0]->contents,
717 tkt->enc_part2->authorization_data[0]->length);
721 if (tkt->ticket.authorization_data) {
722 file_save("/tmp/authdata.dat",
723 tkt->ticket.authorization_data->val->ad_data.data,
724 tkt->ticket.authorization_data->val->ad_data.length);
729 ret = smb_krb5_unparse_name(mem_ctx, context,
730 client_principal, principal);
732 DEBUG(3, (__location__ ": smb_krb5_unparse_name "
733 "failed (%s)\n", error_message(ret)));
734 sret = NT_STATUS_LOGON_FAILURE;
744 if (!NT_STATUS_IS_OK(sret)) {
745 data_blob_free(&auth_data);
748 if (!NT_STATUS_IS_OK(sret)) {
749 data_blob_free(ap_rep);
753 krb5_free_principal(context, host_princ);
757 krb5_free_keyblock(context, keyblock);
761 krb5_free_ticket(context, tkt);
764 SAFE_FREE(host_princ_s);
767 krb5_auth_con_free(context, auth_context);
771 krb5_free_context(context);
777 #endif /* HAVE_KRB5 */