2 Unix SMB/CIFS implementation.
3 simple kerberos5 routines for active directory
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Luke Howard 2002-2003
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 Copyright (C) Guenther Deschner 2005-2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "system/filesys.h"
25 #include "krb5_samba.h"
26 #include "lib/util/asn1.h"
28 #ifndef KRB5_AUTHDATA_WIN2K_PAC
29 #define KRB5_AUTHDATA_WIN2K_PAC 128
32 #ifndef KRB5_AUTHDATA_IF_RELEVANT
33 #define KRB5_AUTHDATA_IF_RELEVANT 1
38 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
39 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
40 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
41 bind field, flags field. */
42 #define GSS_C_DELEG_FLAG 1
44 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
45 but still has the symbol */
46 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
47 krb5_error_code
krb5_auth_con_set_req_cksumtype(
49 krb5_auth_context auth_context
,
50 krb5_cksumtype cksumtype
);
53 #if !defined(SMB_MALLOC)
55 #define SMB_MALLOC(s) malloc((s))
59 #define SMB_STRDUP(s) strdup(s)
62 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
64 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
66 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
67 * to krb5_set_default_tgs_ktypes. See
68 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
70 * If the MIT libraries are not exporting internal symbols, we will end up in
71 * this branch, which is correct. Otherwise we will continue to use the
74 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
76 return krb5_set_default_tgs_enctypes(ctx
, enc
);
79 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
82 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
84 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
87 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
89 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
91 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
93 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
95 memset(pkaddr
, '\0', sizeof(krb5_address
));
96 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
97 if (paddr
->ss_family
== AF_INET6
) {
98 pkaddr
->addr_type
= KRB5_ADDRESS_INET6
;
99 pkaddr
->address
.length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
100 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
104 if (paddr
->ss_family
== AF_INET
) {
105 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
106 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
107 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
112 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
114 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
116 memset(pkaddr
, '\0', sizeof(krb5_address
));
117 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
118 if (paddr
->ss_family
== AF_INET6
) {
119 pkaddr
->addrtype
= ADDRTYPE_INET6
;
120 pkaddr
->length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
121 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
125 if (paddr
->ss_family
== AF_INET
) {
126 pkaddr
->addrtype
= ADDRTYPE_INET
;
127 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
128 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
134 #error UNKNOWN_ADDRTYPE
137 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_C_STRING_TO_KEY)
139 int create_kerberos_key_from_string_direct(krb5_context context
,
140 krb5_principal host_princ
,
143 krb5_enctype enctype
)
148 ret
= krb5_principal2salt(context
, host_princ
, &salt
);
150 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret
)));
153 ret
= krb5_c_string_to_key(context
, enctype
, password
, &salt
, key
);
154 SAFE_FREE(salt
.data
);
158 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
160 int create_kerberos_key_from_string_direct(krb5_context context
,
161 krb5_principal host_princ
,
164 krb5_enctype enctype
)
169 ret
= krb5_get_pw_salt(context
, host_princ
, &salt
);
171 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret
)));
175 ret
= krb5_string_to_key_salt(context
, enctype
, (const char *)password
->data
, salt
, key
);
176 krb5_free_salt(context
, salt
);
181 #error UNKNOWN_CREATE_KEY_FUNCTIONS
184 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
185 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
186 krb5_enctype
**enctypes
)
188 return krb5_get_permitted_enctypes(context
, enctypes
);
190 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
191 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
192 krb5_enctype
**enctypes
)
194 #ifdef HAVE_KRB5_PDU_NONE_DECL
195 return krb5_get_default_in_tkt_etypes(context
, KRB5_PDU_NONE
, enctypes
);
197 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
201 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
204 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
205 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
206 krb5_auth_context auth_context
,
207 krb5_keyblock
*keyblock
)
209 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
213 bool unwrap_edata_ntstatus(TALLOC_CTX
*mem_ctx
,
215 DATA_BLOB
*edata_out
)
217 DATA_BLOB edata_contents
;
221 if (!edata
->length
) {
225 data
= asn1_init(mem_ctx
);
230 asn1_load(data
, *edata
);
231 asn1_start_tag(data
, ASN1_SEQUENCE(0));
232 asn1_start_tag(data
, ASN1_CONTEXT(1));
233 asn1_read_Integer(data
, &edata_type
);
235 if (edata_type
!= KRB5_PADATA_PW_SALT
) {
236 DEBUG(0,("edata is not of required type %d but of type %d\n",
237 KRB5_PADATA_PW_SALT
, edata_type
));
242 asn1_start_tag(data
, ASN1_CONTEXT(2));
243 asn1_read_OctetString(data
, talloc_tos(), &edata_contents
);
249 *edata_out
= data_blob_talloc(mem_ctx
, edata_contents
.data
, edata_contents
.length
);
251 data_blob_free(&edata_contents
);
257 static bool ads_cleanup_expired_creds(krb5_context context
,
261 krb5_error_code retval
;
262 const char *cc_type
= krb5_cc_get_type(context
, ccache
);
264 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
265 cc_type
, krb5_cc_get_name(context
, ccache
),
266 http_timestring(talloc_tos(), credsp
->times
.endtime
)));
268 /* we will probably need new tickets if the current ones
269 will expire within 10 seconds.
271 if (credsp
->times
.endtime
>= (time(NULL
) + 10))
274 /* heimdal won't remove creds from a file ccache, and
275 perhaps we shouldn't anyway, since internally we
276 use memory ccaches, and a FILE one probably means that
277 we're using creds obtained outside of our exectuable
279 if (strequal(cc_type
, "FILE")) {
280 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type
));
284 retval
= krb5_cc_remove_cred(context
, ccache
, 0, credsp
);
286 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
287 error_message(retval
)));
288 /* If we have an error in this, we want to display it,
289 but continue as though we deleted it */
294 /* Allocate and setup the auth context into the state we need. */
296 static krb5_error_code
setup_auth_context(krb5_context context
,
297 krb5_auth_context
*auth_context
)
299 krb5_error_code retval
;
301 retval
= krb5_auth_con_init(context
, auth_context
);
303 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
304 error_message(retval
)));
308 /* Ensure this is an addressless ticket. */
309 retval
= krb5_auth_con_setaddrs(context
, *auth_context
, NULL
, NULL
);
311 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
312 error_message(retval
)));
318 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
319 static krb5_error_code
create_gss_checksum(krb5_data
*in_data
, /* [inout] */
322 unsigned int orig_length
= in_data
->length
;
323 unsigned int base_cksum_size
= GSSAPI_CHECKSUM_SIZE
;
324 char *gss_cksum
= NULL
;
327 /* Extra length field for delgated ticket. */
328 base_cksum_size
+= 4;
331 if ((unsigned int)base_cksum_size
+ orig_length
<
332 (unsigned int)base_cksum_size
) {
336 gss_cksum
= (char *)SMB_MALLOC(base_cksum_size
+ orig_length
);
337 if (gss_cksum
== NULL
) {
341 memset(gss_cksum
, '\0', base_cksum_size
+ orig_length
);
342 SIVAL(gss_cksum
, 0, GSSAPI_BNDLENGTH
);
345 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
346 * This matches the behavior of heimdal and mit.
348 * And it is needed to work against some closed source
353 memset(&gss_cksum
[4], 0x00, GSSAPI_BNDLENGTH
);
355 SIVAL(gss_cksum
, 20, gss_flags
);
358 SSVAL(gss_cksum
, 24, 1); /* The Delegation Option identifier */
359 SSVAL(gss_cksum
, 26, orig_length
);
360 /* Copy the kerberos KRB_CRED data */
361 memcpy(gss_cksum
+ 28, in_data
->data
, orig_length
);
363 in_data
->data
= NULL
;
366 in_data
->data
= gss_cksum
;
367 in_data
->length
= base_cksum_size
+ orig_length
;
372 /**************************************************************
373 krb5_parse_name that takes a UNIX charset.
374 **************************************************************/
376 krb5_error_code
smb_krb5_parse_name(krb5_context context
,
377 const char *name
, /* in unix charset */
378 krb5_principal
*principal
)
382 size_t converted_size
;
383 TALLOC_CTX
*frame
= talloc_stackframe();
385 if (!push_utf8_talloc(frame
, &utf8_name
, name
, &converted_size
)) {
390 ret
= krb5_parse_name(context
, utf8_name
, principal
);
395 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
396 void krb5_free_unparsed_name(krb5_context context
, char *val
)
402 /**************************************************************
403 krb5_parse_name that returns a UNIX charset name. Must
404 be freed with talloc_free() call.
405 **************************************************************/
407 krb5_error_code
smb_krb5_unparse_name(TALLOC_CTX
*mem_ctx
,
408 krb5_context context
,
409 krb5_const_principal principal
,
414 size_t converted_size
;
417 ret
= krb5_unparse_name(context
, principal
, &utf8_name
);
422 if (!pull_utf8_talloc(mem_ctx
, unix_name
, utf8_name
, &converted_size
)) {
423 krb5_free_unparsed_name(context
, utf8_name
);
426 krb5_free_unparsed_name(context
, utf8_name
);
430 krb5_error_code
smb_krb5_parse_name_norealm(krb5_context context
,
432 krb5_principal
*principal
)
434 /* we are cheating here because parse_name will in fact set the realm.
435 * We don't care as the only caller of smb_krb5_parse_name_norealm
436 * ignores the realm anyway when calling
437 * smb_krb5_principal_compare_any_realm later - Guenther */
439 return smb_krb5_parse_name(context
, name
, principal
);
442 bool smb_krb5_principal_compare_any_realm(krb5_context context
,
443 krb5_const_principal princ1
,
444 krb5_const_principal princ2
)
446 return krb5_principal_compare_any_realm(context
, princ1
, princ2
);
450 we can't use krb5_mk_req because w2k wants the service to be in a particular format
452 static krb5_error_code
ads_krb5_mk_req(krb5_context context
,
453 krb5_auth_context
*auth_context
,
454 const krb5_flags ap_req_options
,
455 const char *principal
,
459 const char *impersonate_princ_s
)
461 krb5_error_code retval
;
462 krb5_principal server
;
463 krb5_principal impersonate_princ
= NULL
;
467 bool creds_ready
= false;
468 int i
= 0, maxtries
= 3;
470 ZERO_STRUCT(in_data
);
472 retval
= smb_krb5_parse_name(context
, principal
, &server
);
474 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal
));
478 if (impersonate_princ_s
) {
479 retval
= smb_krb5_parse_name(context
, impersonate_princ_s
,
482 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s
));
487 /* obtain ticket & session key */
489 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
490 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
491 error_message(retval
)));
495 if ((retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
))) {
496 /* This can commonly fail on smbd startup with no ticket in the cache.
497 * Report at higher level than 1. */
498 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
499 error_message(retval
)));
503 while (!creds_ready
&& (i
< maxtries
)) {
505 if ((retval
= smb_krb5_get_credentials(context
, ccache
,
510 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
511 principal
, error_message(retval
)));
515 /* cope with ticket being in the future due to clock skew */
516 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
517 time_t t
= time(NULL
);
518 int time_offset
=(int)((unsigned)credsp
->times
.starttime
-t
);
519 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
520 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
523 if (!ads_cleanup_expired_creds(context
, ccache
, credsp
)) {
530 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
531 principal
, krb5_cc_get_type(context
, ccache
), krb5_cc_get_name(context
, ccache
),
532 http_timestring(talloc_tos(), (unsigned)credsp
->times
.endtime
),
533 (unsigned)credsp
->times
.endtime
));
536 *expire_time
= (time_t)credsp
->times
.endtime
;
539 /* Allocate the auth_context. */
540 retval
= setup_auth_context(context
, auth_context
);
542 DEBUG(1,("setup_auth_context failed (%s)\n",
543 error_message(retval
)));
547 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
549 uint32_t gss_flags
= 0;
551 if( credsp
->ticket_flags
& TKT_FLG_OK_AS_DELEGATE
) {
552 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
553 as part of the kerberos exchange. */
555 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
557 retval
= krb5_auth_con_setuseruserkey(context
,
561 DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
562 error_message(retval
)));
566 /* Must use a subkey for forwarded tickets. */
567 retval
= krb5_auth_con_setflags(context
,
569 KRB5_AUTH_CONTEXT_USE_SUBKEY
);
571 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
572 error_message(retval
)));
576 retval
= krb5_fwd_tgt_creds(context
,/* Krb5 context [in] */
577 *auth_context
, /* Authentication context [in] */
578 discard_const_p(char, KRB5_TGS_NAME
), /* Ticket service name ("krbtgt") [in] */
579 credsp
->client
, /* Client principal for the tgt [in] */
580 credsp
->server
, /* Server principal for the tgt [in] */
581 ccache
, /* Credential cache to use for storage [in] */
582 1, /* Turn on for "Forwardable ticket" [in] */
583 &in_data
); /* Resulting response [out] */
586 DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n",
587 error_message( retval
) ) );
590 * This is not fatal. Delete the *auth_context and continue
591 * with krb5_mk_req_extended to get a non-forwardable ticket.
595 free( in_data
.data
);
599 krb5_auth_con_free(context
, *auth_context
);
600 *auth_context
= NULL
;
601 retval
= setup_auth_context(context
, auth_context
);
603 DEBUG(1,("setup_auth_context failed (%s)\n",
604 error_message(retval
)));
608 /* We got a delegated ticket. */
609 gss_flags
|= GSS_C_DELEG_FLAG
;
613 /* Frees and reallocates in_data into a GSS checksum blob. */
614 retval
= create_gss_checksum(&in_data
, gss_flags
);
619 /* We always want GSS-checksum types. */
620 retval
= krb5_auth_con_set_req_cksumtype(context
, *auth_context
, GSSAPI_CHECKSUM
);
622 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
623 error_message(retval
)));
629 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
630 &in_data
, credsp
, outbuf
);
632 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
633 error_message(retval
)));
636 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
641 free( in_data
.data
);
645 krb5_free_creds(context
, credsp
);
648 krb5_free_cred_contents(context
, &creds
);
651 krb5_free_principal(context
, server
);
652 if (impersonate_princ
) {
653 krb5_free_principal(context
, impersonate_princ
);
659 void kerberos_free_data_contents(krb5_context context
, krb5_data
*pdata
)
661 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
663 krb5_free_data_contents(context
, pdata
);
665 #elif defined(HAVE_KRB5_DATA_FREE)
666 krb5_data_free(context
, pdata
);
668 SAFE_FREE(pdata
->data
);
673 get a kerberos5 ticket for the given service
675 int cli_krb5_get_ticket(TALLOC_CTX
*mem_ctx
,
676 const char *principal
, time_t time_offset
,
677 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
678 uint32_t extra_ap_opts
, const char *ccname
,
680 const char *impersonate_princ_s
)
683 krb5_error_code retval
;
685 krb5_context context
= NULL
;
686 krb5_ccache ccdef
= NULL
;
687 krb5_auth_context auth_context
= NULL
;
688 krb5_enctype enc_types
[] = {
689 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
690 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
692 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
693 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
695 ENCTYPE_ARCFOUR_HMAC
,
700 initialize_krb5_error_table();
701 retval
= krb5_init_context(&context
);
703 DEBUG(1, ("krb5_init_context failed (%s)\n",
704 error_message(retval
)));
708 if (time_offset
!= 0) {
709 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
712 if ((retval
= krb5_cc_resolve(context
, ccname
?
713 ccname
: krb5_cc_default_name(context
), &ccdef
))) {
714 DEBUG(1, ("krb5_cc_default failed (%s)\n",
715 error_message(retval
)));
719 if ((retval
= krb5_set_default_tgs_ktypes(context
, enc_types
))) {
720 DEBUG(1, ("krb5_set_default_tgs_ktypes failed (%s)\n",
721 error_message(retval
)));
725 retval
= ads_krb5_mk_req(context
, &auth_context
,
726 AP_OPTS_USE_SUBKEY
| (krb5_flags
)extra_ap_opts
,
727 principal
, ccdef
, &packet
,
728 tgs_expire
, impersonate_princ_s
);
733 get_krb5_smb_session_key(mem_ctx
, context
, auth_context
,
734 session_key_krb5
, false);
736 *ticket
= data_blob_talloc(mem_ctx
, packet
.data
, packet
.length
);
738 kerberos_free_data_contents(context
, &packet
);
744 krb5_cc_close(context
, ccdef
);
746 krb5_auth_con_free(context
, auth_context
);
747 krb5_free_context(context
);
753 bool get_krb5_smb_session_key(TALLOC_CTX
*mem_ctx
,
754 krb5_context context
,
755 krb5_auth_context auth_context
,
756 DATA_BLOB
*session_key
, bool remote
)
758 krb5_keyblock
*skey
= NULL
;
759 krb5_error_code err
= 0;
763 err
= krb5_auth_con_getremotesubkey(context
,
764 auth_context
, &skey
);
766 err
= krb5_auth_con_getlocalsubkey(context
,
767 auth_context
, &skey
);
770 if (err
|| skey
== NULL
) {
771 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
775 DEBUG(10, ("Got KRB5 session key of length %d\n",
776 (int)KRB5_KEY_LENGTH(skey
)));
778 *session_key
= data_blob_talloc(mem_ctx
,
780 KRB5_KEY_LENGTH(skey
));
781 dump_data_pw("KRB5 Session Key:\n",
783 session_key
->length
);
789 krb5_free_keyblock(context
, skey
);
796 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
797 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
);
799 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
)
801 static krb5_data kdata
;
803 kdata
.data
= discard_const_p(char, krb5_principal_get_comp_string(context
, principal
, i
));
804 kdata
.length
= strlen((const char *)kdata
.data
);
811 krb5_error_code
smb_krb5_renew_ticket(const char *ccache_string
, /* FILE:/tmp/krb5cc_0 */
812 const char *client_string
, /* gd@BER.SUSE.DE */
813 const char *service_string
, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
817 krb5_context context
= NULL
;
818 krb5_ccache ccache
= NULL
;
819 krb5_principal client
= NULL
;
820 krb5_creds creds
, creds_in
;
823 ZERO_STRUCT(creds_in
);
825 initialize_krb5_error_table();
826 ret
= krb5_init_context(&context
);
831 if (!ccache_string
) {
832 ccache_string
= krb5_cc_default_name(context
);
835 if (!ccache_string
) {
840 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string
));
842 /* FIXME: we should not fall back to defaults */
843 ret
= krb5_cc_resolve(context
, discard_const_p(char, ccache_string
), &ccache
);
849 ret
= smb_krb5_parse_name(context
, client_string
, &client
);
854 ret
= krb5_cc_get_principal(context
, ccache
, &client
);
860 ret
= krb5_get_renewed_creds(context
, &creds
, client
, ccache
, discard_const_p(char, service_string
));
862 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
866 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
867 ret
= krb5_cc_initialize(context
, ccache
, client
);
872 ret
= krb5_cc_store_cred(context
, ccache
, &creds
);
875 *expire_time
= (time_t) creds
.times
.endtime
;
879 krb5_free_cred_contents(context
, &creds_in
);
880 krb5_free_cred_contents(context
, &creds
);
883 krb5_free_principal(context
, client
);
886 krb5_cc_close(context
, ccache
);
889 krb5_free_context(context
);
895 krb5_error_code
smb_krb5_free_addresses(krb5_context context
, smb_krb5_addresses
*addr
)
897 krb5_error_code ret
= 0;
901 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
902 krb5_free_addresses(context
, addr
->addrs
);
903 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
904 ret
= krb5_free_addresses(context
, addr
->addrs
);
905 SAFE_FREE(addr
->addrs
);
912 #define MAX_NETBIOSNAME_LEN 16
913 krb5_error_code
smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses
**kerb_addr
,
914 const char *netbios_name
)
916 krb5_error_code ret
= 0;
917 char buf
[MAX_NETBIOSNAME_LEN
];
919 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
920 krb5_address
**addrs
= NULL
;
921 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
922 krb5_addresses
*addrs
= NULL
;
925 *kerb_addr
= (smb_krb5_addresses
*)SMB_MALLOC(sizeof(smb_krb5_addresses
));
926 if (*kerb_addr
== NULL
) {
930 /* temporarily duplicate put_name() code here to avoid dependency
931 * issues for a 5 lines function */
932 len
= strlen(netbios_name
);
933 memcpy(buf
, netbios_name
,
934 (len
< MAX_NETBIOSNAME_LEN
) ? len
: MAX_NETBIOSNAME_LEN
- 1);
935 if (len
< MAX_NETBIOSNAME_LEN
- 1) {
936 memset(buf
+ len
, ' ', MAX_NETBIOSNAME_LEN
- 1 - len
);
938 buf
[MAX_NETBIOSNAME_LEN
- 1] = 0x20;
940 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
944 addrs
= (krb5_address
**)SMB_MALLOC(sizeof(krb5_address
*) * num_addr
);
946 SAFE_FREE(*kerb_addr
);
950 memset(addrs
, 0, sizeof(krb5_address
*) * num_addr
);
952 addrs
[0] = (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
953 if (addrs
[0] == NULL
) {
955 SAFE_FREE(*kerb_addr
);
959 addrs
[0]->magic
= KV5M_ADDRESS
;
960 addrs
[0]->addrtype
= KRB5_ADDR_NETBIOS
;
961 addrs
[0]->length
= MAX_NETBIOSNAME_LEN
;
962 addrs
[0]->contents
= (unsigned char *)SMB_MALLOC(addrs
[0]->length
);
963 if (addrs
[0]->contents
== NULL
) {
966 SAFE_FREE(*kerb_addr
);
970 memcpy(addrs
[0]->contents
, buf
, addrs
[0]->length
);
974 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
976 addrs
= (krb5_addresses
*)SMB_MALLOC(sizeof(krb5_addresses
));
978 SAFE_FREE(*kerb_addr
);
982 memset(addrs
, 0, sizeof(krb5_addresses
));
985 addrs
->val
= (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
986 if (addrs
->val
== NULL
) {
988 SAFE_FREE(kerb_addr
);
992 addrs
->val
[0].addr_type
= KRB5_ADDR_NETBIOS
;
993 addrs
->val
[0].address
.length
= MAX_NETBIOSNAME_LEN
;
994 addrs
->val
[0].address
.data
= (unsigned char *)SMB_MALLOC(addrs
->val
[0].address
.length
);
995 if (addrs
->val
[0].address
.data
== NULL
) {
996 SAFE_FREE(addrs
->val
);
998 SAFE_FREE(*kerb_addr
);
1002 memcpy(addrs
->val
[0].address
.data
, buf
, addrs
->val
[0].address
.length
);
1005 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1007 (*kerb_addr
)->addrs
= addrs
;
1012 void smb_krb5_free_error(krb5_context context
, krb5_error
*krberror
)
1014 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1015 krb5_free_error_contents(context
, krberror
);
1017 krb5_free_error(context
, krberror
);
1021 krb5_error_code
handle_krberror_packet(krb5_context context
,
1024 krb5_error_code ret
;
1025 bool got_error_code
= false;
1027 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1029 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1031 krb5_error krberror
;
1033 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1034 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1035 error_message(ret
)));
1039 if (krberror
.e_data
== NULL
|| krberror
.e_data
->data
== NULL
) {
1040 ret
= (krb5_error_code
) krberror
.error_code
;
1041 got_error_code
= true;
1044 smb_krb5_free_error(context
, &krberror
);
1048 krb5_error
*krberror
;
1050 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1051 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1052 error_message(ret
)));
1056 if (krberror
->e_data
.data
== NULL
) {
1057 #if defined(ERROR_TABLE_BASE_krb5)
1058 ret
= ERROR_TABLE_BASE_krb5
+ (krb5_error_code
) krberror
->error
;
1060 ret
= (krb5_error_code
)krberror
->error
;
1062 got_error_code
= true;
1064 smb_krb5_free_error(context
, krberror
);
1067 if (got_error_code
) {
1068 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1069 error_message(ret
), ret
));
1074 krb5_error_code
smb_krb5_get_init_creds_opt_alloc(krb5_context context
,
1075 krb5_get_init_creds_opt
**opt
)
1077 /* Heimdal or modern MIT version */
1078 return krb5_get_init_creds_opt_alloc(context
, opt
);
1081 void smb_krb5_get_init_creds_opt_free(krb5_context context
,
1082 krb5_get_init_creds_opt
*opt
)
1084 /* Modern MIT or Heimdal version */
1085 krb5_get_init_creds_opt_free(context
, opt
);
1088 krb5_enctype
smb_get_enctype_from_kt_entry(krb5_keytab_entry
*kt_entry
)
1090 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry
));
1093 krb5_error_code
smb_krb5_kt_free_entry(krb5_context context
,
1094 krb5_keytab_entry
*kt_entry
)
1096 /* Try krb5_free_keytab_entry_contents first, since
1097 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
1098 * krb5_kt_free_entry but only has a prototype for the first, while the
1099 * second is considered private.
1101 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
1102 return krb5_free_keytab_entry_contents(context
, kt_entry
);
1103 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
1104 return krb5_kt_free_entry(context
, kt_entry
);
1106 #error UNKNOWN_KT_FREE_FUNCTION
1111 /* caller needs to free etype_s */
1112 krb5_error_code
smb_krb5_enctype_to_string(krb5_context context
,
1113 krb5_enctype enctype
,
1116 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1117 return krb5_enctype_to_string(context
, enctype
, etype_s
); /* Heimdal */
1118 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1120 krb5_error_code ret
= krb5_enctype_to_string(enctype
, buf
, 256); /* MIT */
1124 *etype_s
= SMB_STRDUP(buf
);
1130 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1134 /**********************************************************************
1135 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1136 * allows to process non-default keytab names.
1137 * @param context krb5_context
1138 * @param keytab_name_req string
1139 * @param write_access bool if writable keytab is required
1140 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1141 * @return krb5_error_code
1142 **********************************************************************/
1144 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1145 #ifndef MAX_KEYTAB_NAME_LEN
1146 #define MAX_KEYTAB_NAME_LEN 1100
1149 krb5_error_code
smb_krb5_open_keytab(krb5_context context
,
1150 const char *keytab_name_req
,
1152 krb5_keytab
*keytab
)
1154 krb5_error_code ret
= 0;
1155 TALLOC_CTX
*mem_ctx
;
1156 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1157 char *kt_str
= NULL
;
1158 bool found_valid_name
= false;
1159 const char *pragma
= "FILE";
1160 const char *tmp
= NULL
;
1162 if (!write_access
&& !keytab_name_req
) {
1163 /* caller just wants to read the default keytab readonly, so be it */
1164 return krb5_kt_default(context
, keytab
);
1167 mem_ctx
= talloc_init("smb_krb5_open_keytab");
1172 #ifdef HAVE_WRFILE_KEYTAB
1178 if (keytab_name_req
) {
1180 if (strlen(keytab_name_req
) > MAX_KEYTAB_NAME_LEN
) {
1181 ret
= KRB5_CONFIG_NOTENUFSPACE
;
1185 if ((strncmp(keytab_name_req
, "WRFILE:/", 8) == 0) ||
1186 (strncmp(keytab_name_req
, "FILE:/", 6) == 0)) {
1187 tmp
= keytab_name_req
;
1191 if (keytab_name_req
[0] != '/') {
1192 ret
= KRB5_KT_BADNAME
;
1196 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, keytab_name_req
);
1205 /* we need to handle more complex keytab_strings, like:
1206 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1208 ret
= krb5_kt_default_name(context
, &keytab_string
[0], MAX_KEYTAB_NAME_LEN
- 2);
1213 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string
));
1215 tmp
= talloc_strdup(mem_ctx
, keytab_string
);
1221 if (strncmp(tmp
, "ANY:", 4) == 0) {
1225 memset(&keytab_string
, '\0', sizeof(keytab_string
));
1227 while (next_token_talloc(mem_ctx
, &tmp
, &kt_str
, ",")) {
1228 if (strncmp(kt_str
, "WRFILE:", 7) == 0) {
1229 found_valid_name
= true;
1234 if (strncmp(kt_str
, "FILE:", 5) == 0) {
1235 found_valid_name
= true;
1240 if (tmp
[0] == '/') {
1241 /* Treat as a FILE: keytab definition. */
1242 found_valid_name
= true;
1245 if (found_valid_name
) {
1246 if (tmp
[0] != '/') {
1247 ret
= KRB5_KT_BADNAME
;
1251 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, tmp
);
1260 if (!found_valid_name
) {
1261 ret
= KRB5_KT_UNKNOWN_TYPE
;
1266 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp
));
1267 ret
= krb5_kt_resolve(context
, tmp
, keytab
);
1270 TALLOC_FREE(mem_ctx
);
1274 krb5_error_code
smb_krb5_keytab_name(TALLOC_CTX
*mem_ctx
,
1275 krb5_context context
,
1277 const char **keytab_name
)
1279 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1280 krb5_error_code ret
= 0;
1282 ret
= krb5_kt_get_name(context
, keytab
,
1283 keytab_string
, MAX_KEYTAB_NAME_LEN
- 2);
1288 *keytab_name
= talloc_strdup(mem_ctx
, keytab_string
);
1289 if (!*keytab_name
) {
1296 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1297 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1298 defined(HAVE_KRB5_GET_CREDS)
1299 static krb5_error_code
smb_krb5_get_credentials_for_user_opt(krb5_context context
,
1302 krb5_principal server
,
1303 krb5_principal impersonate_princ
,
1304 krb5_creds
**out_creds
)
1306 krb5_error_code ret
;
1307 krb5_get_creds_opt opt
;
1309 ret
= krb5_get_creds_opt_alloc(context
, &opt
);
1313 krb5_get_creds_opt_add_options(context
, opt
, KRB5_GC_FORWARDABLE
);
1315 if (impersonate_princ
) {
1316 ret
= krb5_get_creds_opt_set_impersonate(context
, opt
,
1323 ret
= krb5_get_creds(context
, opt
, ccache
, server
, out_creds
);
1330 krb5_get_creds_opt_free(context
, opt
);
1334 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
1336 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
1338 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1339 krb5_error_code KRB5_CALLCONV
1340 krb5_get_credentials_for_user(krb5_context context
, krb5_flags options
,
1341 krb5_ccache ccache
, krb5_creds
*in_creds
,
1342 krb5_data
*subject_cert
,
1343 krb5_creds
**out_creds
);
1344 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1346 static krb5_error_code
smb_krb5_get_credentials_for_user(krb5_context context
,
1349 krb5_principal server
,
1350 krb5_principal impersonate_princ
,
1351 krb5_creds
**out_creds
)
1353 krb5_error_code ret
;
1354 krb5_creds in_creds
;
1356 ZERO_STRUCT(in_creds
);
1358 if (impersonate_princ
) {
1360 in_creds
.server
= me
;
1361 in_creds
.client
= impersonate_princ
;
1363 ret
= krb5_get_credentials_for_user(context
,
1364 0, /* krb5_flags options */
1367 NULL
, /* krb5_data *subject_cert */
1370 in_creds
.client
= me
;
1371 in_creds
.server
= server
;
1373 ret
= krb5_get_credentials(context
, 0, ccache
,
1374 &in_creds
, out_creds
);
1379 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1382 * smb_krb5_get_credentials
1384 * @brief Get krb5 credentials for a server
1386 * @param[in] context An initialized krb5_context
1387 * @param[in] ccache An initialized krb5_ccache
1388 * @param[in] me The krb5_principal of the caller
1389 * @param[in] server The krb5_principal of the requested service
1390 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1391 * @param[out] out_creds The returned krb5_creds structure
1392 * @return krb5_error_code
1395 krb5_error_code
smb_krb5_get_credentials(krb5_context context
,
1398 krb5_principal server
,
1399 krb5_principal impersonate_princ
,
1400 krb5_creds
**out_creds
)
1402 krb5_error_code ret
;
1403 krb5_creds
*creds
= NULL
;
1405 if (out_creds
!= NULL
) {
1409 if (impersonate_princ
) {
1410 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
1411 ret
= smb_krb5_get_credentials_for_user_opt(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
1412 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
1413 ret
= smb_krb5_get_credentials_for_user(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
1418 krb5_creds in_creds
;
1420 ZERO_STRUCT(in_creds
);
1422 in_creds
.client
= me
;
1423 in_creds
.server
= server
;
1425 ret
= krb5_get_credentials(context
, 0, ccache
,
1438 krb5_free_creds(context
, creds
);
1444 krb5_error_code
smb_krb5_keyblock_init_contents(krb5_context context
,
1445 krb5_enctype enctype
,
1450 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
1451 return krb5_keyblock_init(context
, enctype
, data
, length
, key
);
1453 memset(key
, 0, sizeof(krb5_keyblock
));
1454 KRB5_KEY_DATA(key
) = SMB_MALLOC(length
);
1455 if (NULL
== KRB5_KEY_DATA(key
)) {
1458 memcpy(KRB5_KEY_DATA(key
), data
, length
);
1459 KRB5_KEY_LENGTH(key
) = length
;
1460 KRB5_KEY_TYPE(key
) = enctype
;
1466 simulate a kinit, putting the tgt in the given credentials cache.
1467 Orignally by remus@snapserver.com
1469 This version is built to use a keyblock, rather than needing the
1472 The impersonate_principal is the principal if NULL, or the principal
1475 The target_service defaults to the krbtgt if NULL, but could be
1476 kpasswd/realm or the local service (if we are doing s4u2self)
1478 krb5_error_code
kerberos_kinit_keyblock_cc(krb5_context ctx
, krb5_ccache cc
,
1479 krb5_principal principal
,
1480 krb5_keyblock
*keyblock
,
1481 const char *target_service
,
1482 krb5_get_init_creds_opt
*krb_options
,
1483 time_t *expire_time
,
1486 krb5_error_code code
= 0;
1487 krb5_creds my_creds
;
1489 #if defined(HAVE_KRB5_GET_INIT_CREDS_KEYBLOCK)
1490 code
= krb5_get_init_creds_keyblock(ctx
, &my_creds
, principal
,
1491 keyblock
, 0, target_service
,
1493 #elif defined(HAVE_KRB5_GET_INIT_CREDS_KEYTAB)
1495 #define SMB_CREDS_KEYTAB "MEMORY:tmp_smb_creds_XXXXXX"
1496 char tmp_name
[sizeof(SMB_CREDS_KEYTAB
)];
1497 krb5_keytab_entry entry
;
1501 memset(&entry
, 0, sizeof(entry
));
1502 entry
.principal
= principal
;
1503 *(KRB5_KT_KEY(&entry
)) = *keyblock
;
1505 memcpy(tmp_name
, SMB_CREDS_KEYTAB
, sizeof(SMB_CREDS_KEYTAB
));
1506 mask
= umask(S_IRWXO
| S_IRWXG
);
1509 if (tmp_name
[0] == 0) {
1510 return KRB5_KT_BADNAME
;
1512 code
= krb5_kt_resolve(ctx
, tmp_name
, &keytab
);
1517 code
= krb5_kt_add_entry(ctx
, keytab
, &entry
);
1519 (void)krb5_kt_close(ctx
, keytab
);
1523 code
= krb5_get_init_creds_keytab(ctx
, &my_creds
, principal
,
1524 keytab
, 0, target_service
,
1526 (void)krb5_kt_close(ctx
, keytab
);
1529 #error krb5_get_init_creds_keyblock not available!
1535 code
= krb5_cc_initialize(ctx
, cc
, principal
);
1540 code
= krb5_cc_store_cred(ctx
, cc
, &my_creds
);
1546 *expire_time
= (time_t) my_creds
.times
.endtime
;
1550 *kdc_time
= (time_t) my_creds
.times
.starttime
;
1555 krb5_free_cred_contents(ctx
, &my_creds
);
1559 krb5_error_code
kerberos_kinit_password_cc(krb5_context ctx
, krb5_ccache cc
,
1560 krb5_principal principal
,
1561 const char *password
,
1562 const char *target_service
,
1563 krb5_get_init_creds_opt
*krb_options
,
1564 time_t *expire_time
,
1567 krb5_error_code code
= 0;
1568 krb5_creds my_creds
;
1570 code
= krb5_get_init_creds_password(ctx
, &my_creds
, principal
,
1571 password
, NULL
, NULL
, 0,
1572 target_service
, krb_options
);
1577 code
= krb5_cc_initialize(ctx
, cc
, principal
);
1582 code
= krb5_cc_store_cred(ctx
, cc
, &my_creds
);
1588 *expire_time
= (time_t) my_creds
.times
.endtime
;
1592 *kdc_time
= (time_t) my_creds
.times
.starttime
;
1597 krb5_free_cred_contents(ctx
, &my_creds
);
1601 #ifdef SAMBA4_USES_HEIMDAL
1603 simulate a kinit, putting the tgt in the given credentials cache.
1604 Orignally by remus@snapserver.com
1606 The impersonate_principal is the principal
1608 The self_service, should be the local service (for S4U2Self if
1609 impersonate_principal is given).
1611 The target_service defaults to the krbtgt if NULL, but could be
1612 kpasswd/realm or a remote service (for S4U2Proxy)
1615 krb5_error_code
kerberos_kinit_s4u2_cc(krb5_context ctx
,
1616 krb5_ccache store_cc
,
1617 krb5_principal init_principal
,
1618 const char *init_password
,
1619 krb5_principal impersonate_principal
,
1620 const char *self_service
,
1621 const char *target_service
,
1622 krb5_get_init_creds_opt
*krb_options
,
1623 time_t *expire_time
,
1626 krb5_error_code code
= 0;
1627 krb5_get_creds_opt options
;
1628 krb5_principal store_principal
;
1629 krb5_creds store_creds
;
1630 krb5_creds
*s4u2self_creds
;
1631 Ticket s4u2self_ticket
;
1632 size_t s4u2self_ticketlen
;
1633 krb5_creds
*s4u2proxy_creds
;
1634 krb5_principal self_princ
;
1636 krb5_principal target_princ
;
1638 const char *self_realm
;
1639 krb5_principal blacklist_principal
= NULL
;
1640 krb5_principal whitelist_principal
= NULL
;
1642 code
= krb5_get_init_creds_password(ctx
, &store_creds
,
1653 store_principal
= init_principal
;
1656 * We are trying S4U2Self now:
1658 * As we do not want to expose our TGT in the
1659 * krb5_ccache, which is also holds the impersonated creds.
1661 * Some low level krb5/gssapi function might use the TGT
1662 * identity and let the client act as our machine account.
1664 * We need to avoid that and use a temporary krb5_ccache
1665 * in order to pass our TGT to the krb5_get_creds() function.
1667 code
= krb5_cc_new_unique(ctx
, NULL
, NULL
, &tmp_cc
);
1669 krb5_free_cred_contents(ctx
, &store_creds
);
1673 code
= krb5_cc_initialize(ctx
, tmp_cc
, store_creds
.client
);
1675 krb5_cc_destroy(ctx
, tmp_cc
);
1676 krb5_free_cred_contents(ctx
, &store_creds
);
1680 code
= krb5_cc_store_cred(ctx
, tmp_cc
, &store_creds
);
1682 krb5_free_cred_contents(ctx
, &store_creds
);
1683 krb5_cc_destroy(ctx
, tmp_cc
);
1688 * we need to remember the client principal of our
1689 * TGT and make sure the KDC does not return this
1690 * in the impersonated tickets. This can happen
1691 * if the KDC does not support S4U2Self and S4U2Proxy.
1693 blacklist_principal
= store_creds
.client
;
1694 store_creds
.client
= NULL
;
1695 krb5_free_cred_contents(ctx
, &store_creds
);
1698 * Check if we also need S4U2Proxy or if S4U2Self is
1699 * enough in order to get a ticket for the target.
1701 if (target_service
== NULL
) {
1703 } else if (strcmp(target_service
, self_service
) == 0) {
1710 * For S4U2Self we need our own service principal,
1711 * which belongs to our own realm (available on
1712 * our client principal).
1714 self_realm
= krb5_principal_get_realm(ctx
, init_principal
);
1716 code
= krb5_parse_name(ctx
, self_service
, &self_princ
);
1718 krb5_free_principal(ctx
, blacklist_principal
);
1719 krb5_cc_destroy(ctx
, tmp_cc
);
1723 code
= krb5_principal_set_realm(ctx
, self_princ
, self_realm
);
1725 krb5_free_principal(ctx
, blacklist_principal
);
1726 krb5_free_principal(ctx
, self_princ
);
1727 krb5_cc_destroy(ctx
, tmp_cc
);
1731 code
= krb5_get_creds_opt_alloc(ctx
, &options
);
1733 krb5_free_principal(ctx
, blacklist_principal
);
1734 krb5_free_principal(ctx
, self_princ
);
1735 krb5_cc_destroy(ctx
, tmp_cc
);
1741 * If we want S4U2Proxy, we need the forwardable flag
1742 * on the S4U2Self ticket.
1744 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_FORWARDABLE
);
1747 code
= krb5_get_creds_opt_set_impersonate(ctx
, options
,
1748 impersonate_principal
);
1750 krb5_get_creds_opt_free(ctx
, options
);
1751 krb5_free_principal(ctx
, blacklist_principal
);
1752 krb5_free_principal(ctx
, self_princ
);
1753 krb5_cc_destroy(ctx
, tmp_cc
);
1757 code
= krb5_get_creds(ctx
, options
, tmp_cc
,
1758 self_princ
, &s4u2self_creds
);
1759 krb5_get_creds_opt_free(ctx
, options
);
1760 krb5_free_principal(ctx
, self_princ
);
1762 krb5_free_principal(ctx
, blacklist_principal
);
1763 krb5_cc_destroy(ctx
, tmp_cc
);
1768 krb5_cc_destroy(ctx
, tmp_cc
);
1771 * Now make sure we store the impersonated principal
1772 * and creds instead of the TGT related stuff
1773 * in the krb5_ccache of the caller.
1775 code
= krb5_copy_creds_contents(ctx
, s4u2self_creds
,
1777 krb5_free_creds(ctx
, s4u2self_creds
);
1783 * It's important to store the principal the KDC
1784 * returned, as otherwise the caller would not find
1785 * the S4U2Self ticket in the krb5_ccache lookup.
1787 store_principal
= store_creds
.client
;
1792 * We are trying S4U2Proxy:
1794 * We need the ticket from the S4U2Self step
1795 * and our TGT in order to get the delegated ticket.
1797 code
= decode_Ticket((const uint8_t *)s4u2self_creds
->ticket
.data
,
1798 s4u2self_creds
->ticket
.length
,
1800 &s4u2self_ticketlen
);
1802 krb5_free_creds(ctx
, s4u2self_creds
);
1803 krb5_free_principal(ctx
, blacklist_principal
);
1804 krb5_cc_destroy(ctx
, tmp_cc
);
1809 * we need to remember the client principal of the
1810 * S4U2Self stage and as it needs to match the one we
1811 * will get for the S4U2Proxy stage. We need this
1812 * in order to detect KDCs which does not support S4U2Proxy.
1814 whitelist_principal
= s4u2self_creds
->client
;
1815 s4u2self_creds
->client
= NULL
;
1816 krb5_free_creds(ctx
, s4u2self_creds
);
1819 * For S4U2Proxy we also got a target service principal,
1820 * which also belongs to our own realm (available on
1821 * our client principal).
1823 code
= krb5_parse_name(ctx
, target_service
, &target_princ
);
1825 free_Ticket(&s4u2self_ticket
);
1826 krb5_free_principal(ctx
, whitelist_principal
);
1827 krb5_free_principal(ctx
, blacklist_principal
);
1828 krb5_cc_destroy(ctx
, tmp_cc
);
1832 code
= krb5_principal_set_realm(ctx
, target_princ
, self_realm
);
1834 free_Ticket(&s4u2self_ticket
);
1835 krb5_free_principal(ctx
, target_princ
);
1836 krb5_free_principal(ctx
, whitelist_principal
);
1837 krb5_free_principal(ctx
, blacklist_principal
);
1838 krb5_cc_destroy(ctx
, tmp_cc
);
1842 code
= krb5_get_creds_opt_alloc(ctx
, &options
);
1844 free_Ticket(&s4u2self_ticket
);
1845 krb5_free_principal(ctx
, target_princ
);
1846 krb5_free_principal(ctx
, whitelist_principal
);
1847 krb5_free_principal(ctx
, blacklist_principal
);
1848 krb5_cc_destroy(ctx
, tmp_cc
);
1852 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_FORWARDABLE
);
1853 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_CONSTRAINED_DELEGATION
);
1855 code
= krb5_get_creds_opt_set_ticket(ctx
, options
, &s4u2self_ticket
);
1856 free_Ticket(&s4u2self_ticket
);
1858 krb5_get_creds_opt_free(ctx
, options
);
1859 krb5_free_principal(ctx
, target_princ
);
1860 krb5_free_principal(ctx
, whitelist_principal
);
1861 krb5_free_principal(ctx
, blacklist_principal
);
1862 krb5_cc_destroy(ctx
, tmp_cc
);
1866 code
= krb5_get_creds(ctx
, options
, tmp_cc
,
1867 target_princ
, &s4u2proxy_creds
);
1868 krb5_get_creds_opt_free(ctx
, options
);
1869 krb5_free_principal(ctx
, target_princ
);
1870 krb5_cc_destroy(ctx
, tmp_cc
);
1872 krb5_free_principal(ctx
, whitelist_principal
);
1873 krb5_free_principal(ctx
, blacklist_principal
);
1878 * Now make sure we store the impersonated principal
1879 * and creds instead of the TGT related stuff
1880 * in the krb5_ccache of the caller.
1882 code
= krb5_copy_creds_contents(ctx
, s4u2proxy_creds
,
1884 krb5_free_creds(ctx
, s4u2proxy_creds
);
1886 krb5_free_principal(ctx
, whitelist_principal
);
1887 krb5_free_principal(ctx
, blacklist_principal
);
1892 * It's important to store the principal the KDC
1893 * returned, as otherwise the caller would not find
1894 * the S4U2Self ticket in the krb5_ccache lookup.
1896 store_principal
= store_creds
.client
;
1899 if (blacklist_principal
&&
1900 krb5_principal_compare(ctx
, store_creds
.client
, blacklist_principal
)) {
1904 code
= krb5_unparse_name(ctx
, blacklist_principal
, &sp
);
1908 code
= krb5_unparse_name(ctx
, impersonate_principal
, &ip
);
1912 DEBUG(1, ("kerberos_kinit_password_cc: "
1913 "KDC returned self principal[%s] while impersonating [%s]\n",
1914 sp
?sp
:"<no memory>",
1915 ip
?ip
:"<no memory>"));
1920 krb5_free_principal(ctx
, whitelist_principal
);
1921 krb5_free_principal(ctx
, blacklist_principal
);
1922 krb5_free_cred_contents(ctx
, &store_creds
);
1923 return KRB5_FWD_BAD_PRINCIPAL
;
1925 if (blacklist_principal
) {
1926 krb5_free_principal(ctx
, blacklist_principal
);
1929 if (whitelist_principal
&&
1930 !krb5_principal_compare(ctx
, store_creds
.client
, whitelist_principal
)) {
1934 code
= krb5_unparse_name(ctx
, store_creds
.client
, &sp
);
1938 code
= krb5_unparse_name(ctx
, whitelist_principal
, &ep
);
1942 DEBUG(1, ("kerberos_kinit_password_cc: "
1943 "KDC returned wrong principal[%s] we expected [%s]\n",
1944 sp
?sp
:"<no memory>",
1945 ep
?ep
:"<no memory>"));
1950 krb5_free_principal(ctx
, whitelist_principal
);
1951 krb5_free_cred_contents(ctx
, &store_creds
);
1952 return KRB5_FWD_BAD_PRINCIPAL
;
1954 if (whitelist_principal
) {
1955 krb5_free_principal(ctx
, whitelist_principal
);
1958 code
= krb5_cc_initialize(ctx
, store_cc
, store_principal
);
1960 krb5_free_cred_contents(ctx
, &store_creds
);
1964 code
= krb5_cc_store_cred(ctx
, store_cc
, &store_creds
);
1966 krb5_free_cred_contents(ctx
, &store_creds
);
1971 *expire_time
= (time_t) store_creds
.times
.endtime
;
1975 *kdc_time
= (time_t) store_creds
.times
.starttime
;
1978 krb5_free_cred_contents(ctx
, &store_creds
);
1984 #if !defined(HAVE_KRB5_MAKE_PRINCIPAL) && defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
1985 krb5_error_code
smb_krb5_make_principal(krb5_context context
,
1986 krb5_principal
*principal
,
1987 const char *_realm
, ...)
1989 krb5_error_code code
;
1998 code
= krb5_get_default_realm(context
, &realm
);
2005 va_start(ap
, _realm
);
2006 code
= krb5_build_principal_alloc_va(context
, principal
,
2007 strlen(realm
), realm
,
2012 krb5_free_default_realm(context
, realm
);
2019 #if !defined(HAVE_KRB5_CC_GET_LIFETIME) && defined(HAVE_KRB5_CC_RETRIEVE_CRED)
2021 * @brief Get the lifetime of the initial ticket in the cache.
2023 * @param[in] context The kerberos context.
2025 * @param[in] id The credential cache to get the ticket lifetime.
2027 * @param[out] t A pointer to a time value to store the lifetime.
2029 * @return 0 on success, a krb5_error_code on error.
2031 krb5_error_code
smb_krb5_cc_get_lifetime(krb5_context context
,
2035 krb5_cc_cursor cursor
;
2036 krb5_error_code kerr
;
2042 kerr
= krb5_timeofday(context
, &now
);
2047 kerr
= krb5_cc_start_seq_get(context
, id
, &cursor
);
2052 while ((kerr
= krb5_cc_next_cred(context
, id
, &cursor
, &cred
)) == 0) {
2053 #ifndef HAVE_FLAGS_IN_KRB5_CREDS
2054 if (cred
.ticket_flags
& TKT_FLG_INITIAL
) {
2056 if (cred
.flags
.b
.initial
) {
2058 if (now
< cred
.times
.endtime
) {
2059 *t
= (time_t) (cred
.times
.endtime
- now
);
2061 krb5_free_cred_contents(context
, &cred
);
2064 krb5_free_cred_contents(context
, &cred
);
2067 krb5_cc_end_seq_get(context
, id
, &cursor
);
2071 #endif /* HAVE_KRB5_CC_GET_LIFETIME */
2073 #if !defined(HAVE_KRB5_FREE_CHECKSUM_CONTENTS) && defined(HAVE_FREE_CHECKSUM)
2074 void smb_krb5_free_checksum_contents(krb5_context ctx
, krb5_checksum
*cksum
)
2076 free_Checksum(cksum
);
2080 krb5_error_code
smb_krb5_make_pac_checksum(TALLOC_CTX
*mem_ctx
,
2081 DATA_BLOB
*pac_data
,
2082 krb5_context context
,
2083 const krb5_keyblock
*keyblock
,
2085 DATA_BLOB
*sig_blob
)
2087 krb5_error_code ret
;
2088 krb5_checksum cksum
;
2089 #if defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CREATE_CHECKSUM)
2093 ret
= krb5_crypto_init(context
,
2098 DEBUG(0,("krb5_crypto_init() failed: %s\n",
2099 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
2102 ret
= krb5_create_checksum(context
,
2104 KRB5_KU_OTHER_CKSUM
,
2110 DEBUG(2, ("PAC Verification failed: %s\n",
2111 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
2114 krb5_crypto_destroy(context
, crypto
);
2120 *sig_type
= cksum
.cksumtype
;
2121 *sig_blob
= data_blob_talloc(mem_ctx
,
2122 cksum
.checksum
.data
,
2123 cksum
.checksum
.length
);
2124 #elif defined(HAVE_KRB5_C_MAKE_CHECKSUM)
2127 input
.data
= (char *)pac_data
->data
;
2128 input
.length
= pac_data
->length
;
2130 ret
= krb5_c_make_checksum(context
,
2133 KRB5_KEYUSAGE_APP_DATA_CKSUM
,
2137 DEBUG(2, ("PAC Verification failed: %s\n",
2138 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
2142 *sig_type
= cksum
.checksum_type
;
2143 *sig_blob
= data_blob_talloc(mem_ctx
,
2148 #error krb5_create_checksum or krb5_c_make_checksum not available
2149 #endif /* HAVE_KRB5_C_MAKE_CHECKSUM */
2150 smb_krb5_free_checksum_contents(context
, &cksum
);
2157 * smb_krb5_principal_get_realm
2159 * @brief Get realm of a principal
2161 * @param[in] context The krb5_context
2162 * @param[in] principal The principal
2163 * @return pointer to the realm
2167 char *smb_krb5_principal_get_realm(krb5_context context
,
2168 krb5_principal principal
)
2170 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2171 return discard_const_p(char, krb5_principal_get_realm(context
, principal
));
2172 #elif defined(krb5_princ_realm) /* MIT */
2174 realm
= krb5_princ_realm(context
, principal
);
2175 return discard_const_p(char, realm
->data
);
2181 /************************************************************************
2182 Routine to get the default realm from the kerberos credentials cache.
2183 Caller must free if the return value is not NULL.
2184 ************************************************************************/
2186 static char *smb_krb5_get_default_realm_from_ccache(TALLOC_CTX
*mem_ctx
)
2189 krb5_context ctx
= NULL
;
2190 krb5_ccache cc
= NULL
;
2191 krb5_principal princ
= NULL
;
2193 initialize_krb5_error_table();
2194 if (krb5_init_context(&ctx
)) {
2198 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2199 "Trying to read krb5 cache: %s\n",
2200 krb5_cc_default_name(ctx
)));
2201 if (krb5_cc_default(ctx
, &cc
)) {
2202 DEBUG(0,("kerberos_get_default_realm_from_ccache: "
2203 "failed to read default cache\n"));
2206 if (krb5_cc_get_principal(ctx
, cc
, &princ
)) {
2207 DEBUG(0,("kerberos_get_default_realm_from_ccache: "
2208 "failed to get default principal\n"));
2212 #if defined(HAVE_KRB5_PRINCIPAL_GET_REALM)
2213 realm
= talloc_strdup(mem_ctx
, krb5_principal_get_realm(ctx
, princ
));
2214 #elif defined(HAVE_KRB5_PRINC_REALM)
2216 krb5_data
*realm_data
= krb5_princ_realm(ctx
, princ
);
2217 realm
= talloc_strndup(mem_ctx
, realm_data
->data
, realm_data
->length
);
2225 krb5_free_principal(ctx
, princ
);
2228 krb5_cc_close(ctx
, cc
);
2230 krb5_free_context(ctx
);
2236 /************************************************************************
2237 Routine to get the realm from a given DNS name.
2238 ************************************************************************/
2240 static char *smb_krb5_get_realm_from_hostname(TALLOC_CTX
*mem_ctx
,
2241 const char *hostname
)
2243 #if defined(HAVE_KRB5_REALM_TYPE)
2245 krb5_realm
*realm_list
= NULL
;
2248 char **realm_list
= NULL
;
2251 krb5_error_code kerr
;
2252 krb5_context ctx
= NULL
;
2254 initialize_krb5_error_table();
2255 if (krb5_init_context(&ctx
)) {
2259 kerr
= krb5_get_host_realm(ctx
, hostname
, &realm_list
);
2261 DEBUG(3,("kerberos_get_realm_from_hostname %s: "
2263 hostname
? hostname
: "(NULL)",
2264 error_message(kerr
) ));
2268 if (realm_list
&& realm_list
[0]) {
2269 realm
= talloc_strdup(mem_ctx
, realm_list
[0]);
2276 krb5_free_host_realm(ctx
, realm_list
);
2279 krb5_free_context(ctx
);
2285 char *kerberos_get_principal_from_service_hostname(TALLOC_CTX
*mem_ctx
,
2286 const char *service
,
2287 const char *remote_name
,
2288 const char *default_realm
)
2293 host
= strchr_m(remote_name
, '.');
2296 realm
= smb_krb5_get_realm_from_hostname(talloc_tos(),
2299 /* NetBIOS name - use our realm. */
2300 realm
= smb_krb5_get_default_realm_from_ccache(talloc_tos());
2303 if (realm
== NULL
|| *realm
== '\0') {
2304 realm
= talloc_strdup(talloc_tos(), default_realm
);
2308 DEBUG(3,("kerberos_get_principal_from_service_hostname: "
2309 "cannot get realm from, "
2310 "desthost %s or default ccache. Using default "
2311 "smb.conf realm %s\n",
2316 principal
= talloc_asprintf(mem_ctx
,
2318 service
, remote_name
,
2324 char *smb_get_krb5_error_message(krb5_context context
,
2325 krb5_error_code code
,
2326 TALLOC_CTX
*mem_ctx
)
2330 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
2331 const char *context_error
= krb5_get_error_message(context
, code
);
2332 if (context_error
) {
2333 ret
= talloc_asprintf(mem_ctx
, "%s: %s",
2334 error_message(code
), context_error
);
2335 krb5_free_error_message(context
, context_error
);
2339 ret
= talloc_strdup(mem_ctx
, error_message(code
));
2343 #else /* HAVE_KRB5 */
2344 /* this saves a few linking headaches */
2345 int cli_krb5_get_ticket(TALLOC_CTX
*mem_ctx
,
2346 const char *principal
, time_t time_offset
,
2347 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
2348 uint32_t extra_ap_opts
,
2349 const char *ccname
, time_t *tgs_expire
,
2350 const char *impersonate_princ_s
)
2352 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2356 #endif /* HAVE_KRB5 */