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 "krb5_samba.h"
25 #include "lib/util/asn1.h"
27 #ifndef KRB5_AUTHDATA_WIN2K_PAC
28 #define KRB5_AUTHDATA_WIN2K_PAC 128
31 #ifndef KRB5_AUTHDATA_IF_RELEVANT
32 #define KRB5_AUTHDATA_IF_RELEVANT 1
37 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
38 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
39 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
40 bind field, flags field. */
41 #define GSS_C_DELEG_FLAG 1
43 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
44 but still has the symbol */
45 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
46 krb5_error_code
krb5_auth_con_set_req_cksumtype(
48 krb5_auth_context auth_context
,
49 krb5_cksumtype cksumtype
);
52 #if !defined(SMB_MALLOC)
54 #define SMB_MALLOC(s) malloc((s))
58 #define SMB_STRDUP(s) strdup(s)
61 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
63 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
65 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
66 * to krb5_set_default_tgs_ktypes. See
67 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
69 * If the MIT libraries are not exporting internal symbols, we will end up in
70 * this branch, which is correct. Otherwise we will continue to use the
73 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
75 return krb5_set_default_tgs_enctypes(ctx
, enc
);
78 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
81 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
83 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
86 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
88 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
90 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
92 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
94 memset(pkaddr
, '\0', sizeof(krb5_address
));
95 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
96 if (paddr
->ss_family
== AF_INET6
) {
97 pkaddr
->addr_type
= KRB5_ADDRESS_INET6
;
98 pkaddr
->address
.length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
99 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
103 if (paddr
->ss_family
== AF_INET
) {
104 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
105 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
106 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
111 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
113 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
115 memset(pkaddr
, '\0', sizeof(krb5_address
));
116 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
117 if (paddr
->ss_family
== AF_INET6
) {
118 pkaddr
->addrtype
= ADDRTYPE_INET6
;
119 pkaddr
->length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
120 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
124 if (paddr
->ss_family
== AF_INET
) {
125 pkaddr
->addrtype
= ADDRTYPE_INET
;
126 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
127 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
133 #error UNKNOWN_ADDRTYPE
136 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_C_STRING_TO_KEY)
138 int create_kerberos_key_from_string_direct(krb5_context context
,
139 krb5_principal host_princ
,
142 krb5_enctype enctype
)
147 ret
= krb5_principal2salt(context
, host_princ
, &salt
);
149 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret
)));
152 ret
= krb5_c_string_to_key(context
, enctype
, password
, &salt
, key
);
153 SAFE_FREE(salt
.data
);
157 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
159 int create_kerberos_key_from_string_direct(krb5_context context
,
160 krb5_principal host_princ
,
163 krb5_enctype enctype
)
168 ret
= krb5_get_pw_salt(context
, host_princ
, &salt
);
170 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret
)));
174 ret
= krb5_string_to_key_salt(context
, enctype
, (const char *)password
->data
, salt
, key
);
175 krb5_free_salt(context
, salt
);
180 #error UNKNOWN_CREATE_KEY_FUNCTIONS
183 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
184 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
185 krb5_enctype
**enctypes
)
187 return krb5_get_permitted_enctypes(context
, enctypes
);
189 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
190 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
191 krb5_enctype
**enctypes
)
193 #ifdef HAVE_KRB5_PDU_NONE_DECL
194 return krb5_get_default_in_tkt_etypes(context
, KRB5_PDU_NONE
, enctypes
);
196 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
200 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
203 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
204 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
205 krb5_auth_context auth_context
,
206 krb5_keyblock
*keyblock
)
208 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
212 bool unwrap_edata_ntstatus(TALLOC_CTX
*mem_ctx
,
214 DATA_BLOB
*edata_out
)
216 DATA_BLOB edata_contents
;
220 if (!edata
->length
) {
224 data
= asn1_init(mem_ctx
);
229 asn1_load(data
, *edata
);
230 asn1_start_tag(data
, ASN1_SEQUENCE(0));
231 asn1_start_tag(data
, ASN1_CONTEXT(1));
232 asn1_read_Integer(data
, &edata_type
);
234 if (edata_type
!= KRB5_PADATA_PW_SALT
) {
235 DEBUG(0,("edata is not of required type %d but of type %d\n",
236 KRB5_PADATA_PW_SALT
, edata_type
));
241 asn1_start_tag(data
, ASN1_CONTEXT(2));
242 asn1_read_OctetString(data
, talloc_tos(), &edata_contents
);
248 *edata_out
= data_blob_talloc(mem_ctx
, edata_contents
.data
, edata_contents
.length
);
250 data_blob_free(&edata_contents
);
256 static bool ads_cleanup_expired_creds(krb5_context context
,
260 krb5_error_code retval
;
261 const char *cc_type
= krb5_cc_get_type(context
, ccache
);
263 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
264 cc_type
, krb5_cc_get_name(context
, ccache
),
265 http_timestring(talloc_tos(), credsp
->times
.endtime
)));
267 /* we will probably need new tickets if the current ones
268 will expire within 10 seconds.
270 if (credsp
->times
.endtime
>= (time(NULL
) + 10))
273 /* heimdal won't remove creds from a file ccache, and
274 perhaps we shouldn't anyway, since internally we
275 use memory ccaches, and a FILE one probably means that
276 we're using creds obtained outside of our exectuable
278 if (strequal(cc_type
, "FILE")) {
279 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type
));
283 retval
= krb5_cc_remove_cred(context
, ccache
, 0, credsp
);
285 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
286 error_message(retval
)));
287 /* If we have an error in this, we want to display it,
288 but continue as though we deleted it */
293 /* Allocate and setup the auth context into the state we need. */
295 static krb5_error_code
setup_auth_context(krb5_context context
,
296 krb5_auth_context
*auth_context
)
298 krb5_error_code retval
;
300 retval
= krb5_auth_con_init(context
, auth_context
);
302 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
303 error_message(retval
)));
307 /* Ensure this is an addressless ticket. */
308 retval
= krb5_auth_con_setaddrs(context
, *auth_context
, NULL
, NULL
);
310 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
311 error_message(retval
)));
317 #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)
318 static krb5_error_code
create_gss_checksum(krb5_data
*in_data
, /* [inout] */
321 unsigned int orig_length
= in_data
->length
;
322 unsigned int base_cksum_size
= GSSAPI_CHECKSUM_SIZE
;
323 char *gss_cksum
= NULL
;
326 /* Extra length field for delgated ticket. */
327 base_cksum_size
+= 4;
330 if ((unsigned int)base_cksum_size
+ orig_length
<
331 (unsigned int)base_cksum_size
) {
335 gss_cksum
= (char *)SMB_MALLOC(base_cksum_size
+ orig_length
);
336 if (gss_cksum
== NULL
) {
340 memset(gss_cksum
, '\0', base_cksum_size
+ orig_length
);
341 SIVAL(gss_cksum
, 0, GSSAPI_BNDLENGTH
);
344 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
345 * This matches the behavior of heimdal and mit.
347 * And it is needed to work against some closed source
352 memset(&gss_cksum
[4], 0x00, GSSAPI_BNDLENGTH
);
354 SIVAL(gss_cksum
, 20, gss_flags
);
357 SSVAL(gss_cksum
, 24, 1); /* The Delegation Option identifier */
358 SSVAL(gss_cksum
, 26, orig_length
);
359 /* Copy the kerberos KRB_CRED data */
360 memcpy(gss_cksum
+ 28, in_data
->data
, orig_length
);
362 in_data
->data
= NULL
;
365 in_data
->data
= gss_cksum
;
366 in_data
->length
= base_cksum_size
+ orig_length
;
371 /**************************************************************
372 krb5_parse_name that takes a UNIX charset.
373 **************************************************************/
375 krb5_error_code
smb_krb5_parse_name(krb5_context context
,
376 const char *name
, /* in unix charset */
377 krb5_principal
*principal
)
381 size_t converted_size
;
382 TALLOC_CTX
*frame
= talloc_stackframe();
384 if (!push_utf8_talloc(frame
, &utf8_name
, name
, &converted_size
)) {
389 ret
= krb5_parse_name(context
, utf8_name
, principal
);
394 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
395 void krb5_free_unparsed_name(krb5_context context
, char *val
)
401 /**************************************************************
402 krb5_parse_name that returns a UNIX charset name. Must
403 be freed with talloc_free() call.
404 **************************************************************/
406 krb5_error_code
smb_krb5_unparse_name(TALLOC_CTX
*mem_ctx
,
407 krb5_context context
,
408 krb5_const_principal principal
,
413 size_t converted_size
;
416 ret
= krb5_unparse_name(context
, principal
, &utf8_name
);
421 if (!pull_utf8_talloc(mem_ctx
, unix_name
, utf8_name
, &converted_size
)) {
422 krb5_free_unparsed_name(context
, utf8_name
);
425 krb5_free_unparsed_name(context
, utf8_name
);
429 krb5_error_code
smb_krb5_parse_name_norealm(krb5_context context
,
431 krb5_principal
*principal
)
433 /* we are cheating here because parse_name will in fact set the realm.
434 * We don't care as the only caller of smb_krb5_parse_name_norealm
435 * ignores the realm anyway when calling
436 * smb_krb5_principal_compare_any_realm later - Guenther */
438 return smb_krb5_parse_name(context
, name
, principal
);
441 bool smb_krb5_principal_compare_any_realm(krb5_context context
,
442 krb5_const_principal princ1
,
443 krb5_const_principal princ2
)
445 return krb5_principal_compare_any_realm(context
, princ1
, princ2
);
449 we can't use krb5_mk_req because w2k wants the service to be in a particular format
451 static krb5_error_code
ads_krb5_mk_req(krb5_context context
,
452 krb5_auth_context
*auth_context
,
453 const krb5_flags ap_req_options
,
454 const char *principal
,
458 const char *impersonate_princ_s
)
460 krb5_error_code retval
;
461 krb5_principal server
;
462 krb5_principal impersonate_princ
= NULL
;
466 bool creds_ready
= false;
467 int i
= 0, maxtries
= 3;
469 ZERO_STRUCT(in_data
);
471 retval
= smb_krb5_parse_name(context
, principal
, &server
);
473 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal
));
477 if (impersonate_princ_s
) {
478 retval
= smb_krb5_parse_name(context
, impersonate_princ_s
,
481 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s
));
486 /* obtain ticket & session key */
488 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
489 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
490 error_message(retval
)));
494 if ((retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
))) {
495 /* This can commonly fail on smbd startup with no ticket in the cache.
496 * Report at higher level than 1. */
497 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
498 error_message(retval
)));
502 while (!creds_ready
&& (i
< maxtries
)) {
504 if ((retval
= smb_krb5_get_credentials(context
, ccache
,
509 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
510 principal
, error_message(retval
)));
514 /* cope with ticket being in the future due to clock skew */
515 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
516 time_t t
= time(NULL
);
517 int time_offset
=(int)((unsigned)credsp
->times
.starttime
-t
);
518 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
519 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
522 if (!ads_cleanup_expired_creds(context
, ccache
, credsp
)) {
529 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
530 principal
, krb5_cc_get_type(context
, ccache
), krb5_cc_get_name(context
, ccache
),
531 http_timestring(talloc_tos(), (unsigned)credsp
->times
.endtime
),
532 (unsigned)credsp
->times
.endtime
));
535 *expire_time
= (time_t)credsp
->times
.endtime
;
538 /* Allocate the auth_context. */
539 retval
= setup_auth_context(context
, auth_context
);
541 DEBUG(1,("setup_auth_context failed (%s)\n",
542 error_message(retval
)));
546 #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)
548 uint32_t gss_flags
= 0;
550 if( credsp
->ticket_flags
& TKT_FLG_OK_AS_DELEGATE
) {
551 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
552 as part of the kerberos exchange. */
554 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
556 retval
= krb5_auth_con_setuseruserkey(context
,
560 DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
561 error_message(retval
)));
565 /* Must use a subkey for forwarded tickets. */
566 retval
= krb5_auth_con_setflags(context
,
568 KRB5_AUTH_CONTEXT_USE_SUBKEY
);
570 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
571 error_message(retval
)));
575 retval
= krb5_fwd_tgt_creds(context
,/* Krb5 context [in] */
576 *auth_context
, /* Authentication context [in] */
577 discard_const_p(char, KRB5_TGS_NAME
), /* Ticket service name ("krbtgt") [in] */
578 credsp
->client
, /* Client principal for the tgt [in] */
579 credsp
->server
, /* Server principal for the tgt [in] */
580 ccache
, /* Credential cache to use for storage [in] */
581 1, /* Turn on for "Forwardable ticket" [in] */
582 &in_data
); /* Resulting response [out] */
585 DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n",
586 error_message( retval
) ) );
589 * This is not fatal. Delete the *auth_context and continue
590 * with krb5_mk_req_extended to get a non-forwardable ticket.
594 free( in_data
.data
);
598 krb5_auth_con_free(context
, *auth_context
);
599 *auth_context
= NULL
;
600 retval
= setup_auth_context(context
, auth_context
);
602 DEBUG(1,("setup_auth_context failed (%s)\n",
603 error_message(retval
)));
607 /* We got a delegated ticket. */
608 gss_flags
|= GSS_C_DELEG_FLAG
;
612 /* Frees and reallocates in_data into a GSS checksum blob. */
613 retval
= create_gss_checksum(&in_data
, gss_flags
);
618 /* We always want GSS-checksum types. */
619 retval
= krb5_auth_con_set_req_cksumtype(context
, *auth_context
, GSSAPI_CHECKSUM
);
621 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
622 error_message(retval
)));
628 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
629 &in_data
, credsp
, outbuf
);
631 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
632 error_message(retval
)));
635 #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)
640 free( in_data
.data
);
644 krb5_free_creds(context
, credsp
);
647 krb5_free_cred_contents(context
, &creds
);
650 krb5_free_principal(context
, server
);
651 if (impersonate_princ
) {
652 krb5_free_principal(context
, impersonate_princ
);
658 void kerberos_free_data_contents(krb5_context context
, krb5_data
*pdata
)
660 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
662 krb5_free_data_contents(context
, pdata
);
664 #elif defined(HAVE_KRB5_DATA_FREE)
665 krb5_data_free(context
, pdata
);
667 SAFE_FREE(pdata
->data
);
672 get a kerberos5 ticket for the given service
674 int cli_krb5_get_ticket(TALLOC_CTX
*mem_ctx
,
675 const char *principal
, time_t time_offset
,
676 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
677 uint32_t extra_ap_opts
, const char *ccname
,
679 const char *impersonate_princ_s
)
682 krb5_error_code retval
;
684 krb5_context context
= NULL
;
685 krb5_ccache ccdef
= NULL
;
686 krb5_auth_context auth_context
= NULL
;
687 krb5_enctype enc_types
[] = {
688 ENCTYPE_ARCFOUR_HMAC
,
693 initialize_krb5_error_table();
694 retval
= krb5_init_context(&context
);
696 DEBUG(1, ("krb5_init_context failed (%s)\n",
697 error_message(retval
)));
701 if (time_offset
!= 0) {
702 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
705 if ((retval
= krb5_cc_resolve(context
, ccname
?
706 ccname
: krb5_cc_default_name(context
), &ccdef
))) {
707 DEBUG(1, ("krb5_cc_default failed (%s)\n",
708 error_message(retval
)));
712 if ((retval
= krb5_set_default_tgs_ktypes(context
, enc_types
))) {
713 DEBUG(1, ("krb5_set_default_tgs_ktypes failed (%s)\n",
714 error_message(retval
)));
718 retval
= ads_krb5_mk_req(context
, &auth_context
,
719 AP_OPTS_USE_SUBKEY
| (krb5_flags
)extra_ap_opts
,
720 principal
, ccdef
, &packet
,
721 tgs_expire
, impersonate_princ_s
);
726 get_krb5_smb_session_key(mem_ctx
, context
, auth_context
,
727 session_key_krb5
, false);
729 *ticket
= data_blob_talloc(mem_ctx
, packet
.data
, packet
.length
);
731 kerberos_free_data_contents(context
, &packet
);
737 krb5_cc_close(context
, ccdef
);
739 krb5_auth_con_free(context
, auth_context
);
740 krb5_free_context(context
);
746 bool get_krb5_smb_session_key(TALLOC_CTX
*mem_ctx
,
747 krb5_context context
,
748 krb5_auth_context auth_context
,
749 DATA_BLOB
*session_key
, bool remote
)
751 krb5_keyblock
*skey
= NULL
;
752 krb5_error_code err
= 0;
756 err
= krb5_auth_con_getremotesubkey(context
,
757 auth_context
, &skey
);
759 err
= krb5_auth_con_getlocalsubkey(context
,
760 auth_context
, &skey
);
763 if (err
|| skey
== NULL
) {
764 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
768 DEBUG(10, ("Got KRB5 session key of length %d\n",
769 (int)KRB5_KEY_LENGTH(skey
)));
771 *session_key
= data_blob_talloc(mem_ctx
,
773 KRB5_KEY_LENGTH(skey
));
774 dump_data_pw("KRB5 Session Key:\n",
776 session_key
->length
);
782 krb5_free_keyblock(context
, skey
);
789 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
790 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
);
792 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
)
794 static krb5_data kdata
;
796 kdata
.data
= discard_const_p(char, krb5_principal_get_comp_string(context
, principal
, i
));
797 kdata
.length
= strlen((const char *)kdata
.data
);
804 krb5_error_code
smb_krb5_renew_ticket(const char *ccache_string
, /* FILE:/tmp/krb5cc_0 */
805 const char *client_string
, /* gd@BER.SUSE.DE */
806 const char *service_string
, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
810 krb5_context context
= NULL
;
811 krb5_ccache ccache
= NULL
;
812 krb5_principal client
= NULL
;
813 krb5_creds creds
, creds_in
, *creds_out
= NULL
;
816 ZERO_STRUCT(creds_in
);
818 initialize_krb5_error_table();
819 ret
= krb5_init_context(&context
);
824 if (!ccache_string
) {
825 ccache_string
= krb5_cc_default_name(context
);
828 if (!ccache_string
) {
833 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string
));
835 /* FIXME: we should not fall back to defaults */
836 ret
= krb5_cc_resolve(context
, discard_const_p(char, ccache_string
), &ccache
);
842 ret
= smb_krb5_parse_name(context
, client_string
, &client
);
847 ret
= krb5_cc_get_principal(context
, ccache
, &client
);
853 ret
= krb5_get_renewed_creds(context
, &creds
, client
, ccache
, discard_const_p(char, service_string
));
855 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
859 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
860 ret
= krb5_cc_initialize(context
, ccache
, client
);
865 ret
= krb5_cc_store_cred(context
, ccache
, &creds
);
868 *expire_time
= (time_t) creds
.times
.endtime
;
872 krb5_free_cred_contents(context
, &creds_in
);
875 krb5_free_creds(context
, creds_out
);
877 krb5_free_cred_contents(context
, &creds
);
881 krb5_free_principal(context
, client
);
884 krb5_cc_close(context
, ccache
);
887 krb5_free_context(context
);
893 krb5_error_code
smb_krb5_free_addresses(krb5_context context
, smb_krb5_addresses
*addr
)
895 krb5_error_code ret
= 0;
899 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
900 krb5_free_addresses(context
, addr
->addrs
);
901 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
902 ret
= krb5_free_addresses(context
, addr
->addrs
);
903 SAFE_FREE(addr
->addrs
);
910 #define MAX_NETBIOSNAME_LEN 16
911 krb5_error_code
smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses
**kerb_addr
,
912 const char *netbios_name
)
914 krb5_error_code ret
= 0;
915 char buf
[MAX_NETBIOSNAME_LEN
];
917 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
918 krb5_address
**addrs
= NULL
;
919 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
920 krb5_addresses
*addrs
= NULL
;
923 *kerb_addr
= (smb_krb5_addresses
*)SMB_MALLOC(sizeof(smb_krb5_addresses
));
924 if (*kerb_addr
== NULL
) {
928 /* temporarily duplicate put_name() code here to avoid dependency
929 * issues for a 5 lines function */
930 len
= strlen(netbios_name
);
931 memcpy(buf
, netbios_name
,
932 (len
< MAX_NETBIOSNAME_LEN
) ? len
: MAX_NETBIOSNAME_LEN
- 1);
933 if (len
< MAX_NETBIOSNAME_LEN
- 1) {
934 memset(buf
+ len
, ' ', MAX_NETBIOSNAME_LEN
- 1 - len
);
936 buf
[MAX_NETBIOSNAME_LEN
- 1] = 0x20;
938 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
942 addrs
= (krb5_address
**)SMB_MALLOC(sizeof(krb5_address
*) * num_addr
);
944 SAFE_FREE(*kerb_addr
);
948 memset(addrs
, 0, sizeof(krb5_address
*) * num_addr
);
950 addrs
[0] = (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
951 if (addrs
[0] == NULL
) {
953 SAFE_FREE(*kerb_addr
);
957 addrs
[0]->magic
= KV5M_ADDRESS
;
958 addrs
[0]->addrtype
= KRB5_ADDR_NETBIOS
;
959 addrs
[0]->length
= MAX_NETBIOSNAME_LEN
;
960 addrs
[0]->contents
= (unsigned char *)SMB_MALLOC(addrs
[0]->length
);
961 if (addrs
[0]->contents
== NULL
) {
964 SAFE_FREE(*kerb_addr
);
968 memcpy(addrs
[0]->contents
, buf
, addrs
[0]->length
);
972 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
974 addrs
= (krb5_addresses
*)SMB_MALLOC(sizeof(krb5_addresses
));
976 SAFE_FREE(*kerb_addr
);
980 memset(addrs
, 0, sizeof(krb5_addresses
));
983 addrs
->val
= (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
984 if (addrs
->val
== NULL
) {
986 SAFE_FREE(kerb_addr
);
990 addrs
->val
[0].addr_type
= KRB5_ADDR_NETBIOS
;
991 addrs
->val
[0].address
.length
= MAX_NETBIOSNAME_LEN
;
992 addrs
->val
[0].address
.data
= (unsigned char *)SMB_MALLOC(addrs
->val
[0].address
.length
);
993 if (addrs
->val
[0].address
.data
== NULL
) {
994 SAFE_FREE(addrs
->val
);
996 SAFE_FREE(*kerb_addr
);
1000 memcpy(addrs
->val
[0].address
.data
, buf
, addrs
->val
[0].address
.length
);
1003 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1005 (*kerb_addr
)->addrs
= addrs
;
1010 void smb_krb5_free_error(krb5_context context
, krb5_error
*krberror
)
1012 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1013 krb5_free_error_contents(context
, krberror
);
1015 krb5_free_error(context
, krberror
);
1019 krb5_error_code
handle_krberror_packet(krb5_context context
,
1022 krb5_error_code ret
;
1023 bool got_error_code
= false;
1025 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1027 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1029 krb5_error krberror
;
1031 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1032 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1033 error_message(ret
)));
1037 if (krberror
.e_data
== NULL
|| krberror
.e_data
->data
== NULL
) {
1038 ret
= (krb5_error_code
) krberror
.error_code
;
1039 got_error_code
= true;
1042 smb_krb5_free_error(context
, &krberror
);
1046 krb5_error
*krberror
;
1048 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1049 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1050 error_message(ret
)));
1054 if (krberror
->e_data
.data
== NULL
) {
1055 #if defined(ERROR_TABLE_BASE_krb5)
1056 ret
= ERROR_TABLE_BASE_krb5
+ (krb5_error_code
) krberror
->error
;
1058 ret
= (krb5_error_code
)krberror
->error
;
1060 got_error_code
= true;
1062 smb_krb5_free_error(context
, krberror
);
1065 if (got_error_code
) {
1066 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1067 error_message(ret
), ret
));
1072 krb5_error_code
smb_krb5_get_init_creds_opt_alloc(krb5_context context
,
1073 krb5_get_init_creds_opt
**opt
)
1075 /* Heimdal or modern MIT version */
1076 return krb5_get_init_creds_opt_alloc(context
, opt
);
1079 void smb_krb5_get_init_creds_opt_free(krb5_context context
,
1080 krb5_get_init_creds_opt
*opt
)
1082 /* Modern MIT or Heimdal version */
1083 krb5_get_init_creds_opt_free(context
, opt
);
1086 krb5_enctype
smb_get_enctype_from_kt_entry(krb5_keytab_entry
*kt_entry
)
1088 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry
));
1091 krb5_error_code
smb_krb5_kt_free_entry(krb5_context context
,
1092 krb5_keytab_entry
*kt_entry
)
1094 /* Try krb5_free_keytab_entry_contents first, since
1095 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
1096 * krb5_kt_free_entry but only has a prototype for the first, while the
1097 * second is considered private.
1099 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
1100 return krb5_free_keytab_entry_contents(context
, kt_entry
);
1101 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
1102 return krb5_kt_free_entry(context
, kt_entry
);
1104 #error UNKNOWN_KT_FREE_FUNCTION
1109 /* caller needs to free etype_s */
1110 krb5_error_code
smb_krb5_enctype_to_string(krb5_context context
,
1111 krb5_enctype enctype
,
1114 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1115 return krb5_enctype_to_string(context
, enctype
, etype_s
); /* Heimdal */
1116 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1118 krb5_error_code ret
= krb5_enctype_to_string(enctype
, buf
, 256); /* MIT */
1122 *etype_s
= SMB_STRDUP(buf
);
1128 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1132 /**********************************************************************
1133 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1134 * allows to process non-default keytab names.
1135 * @param context krb5_context
1136 * @param keytab_name_req string
1137 * @param write_access bool if writable keytab is required
1138 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1139 * @return krb5_error_code
1140 **********************************************************************/
1142 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1143 #ifndef MAX_KEYTAB_NAME_LEN
1144 #define MAX_KEYTAB_NAME_LEN 1100
1147 krb5_error_code
smb_krb5_open_keytab(krb5_context context
,
1148 const char *keytab_name_req
,
1150 krb5_keytab
*keytab
)
1152 krb5_error_code ret
= 0;
1153 TALLOC_CTX
*mem_ctx
;
1154 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1155 char *kt_str
= NULL
;
1156 bool found_valid_name
= false;
1157 const char *pragma
= "FILE";
1158 const char *tmp
= NULL
;
1160 if (!write_access
&& !keytab_name_req
) {
1161 /* caller just wants to read the default keytab readonly, so be it */
1162 return krb5_kt_default(context
, keytab
);
1165 mem_ctx
= talloc_init("smb_krb5_open_keytab");
1170 #ifdef HAVE_WRFILE_KEYTAB
1176 if (keytab_name_req
) {
1178 if (strlen(keytab_name_req
) > MAX_KEYTAB_NAME_LEN
) {
1179 ret
= KRB5_CONFIG_NOTENUFSPACE
;
1183 if ((strncmp(keytab_name_req
, "WRFILE:/", 8) == 0) ||
1184 (strncmp(keytab_name_req
, "FILE:/", 6) == 0)) {
1185 tmp
= keytab_name_req
;
1189 if (keytab_name_req
[0] != '/') {
1190 ret
= KRB5_KT_BADNAME
;
1194 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, keytab_name_req
);
1203 /* we need to handle more complex keytab_strings, like:
1204 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1206 ret
= krb5_kt_default_name(context
, &keytab_string
[0], MAX_KEYTAB_NAME_LEN
- 2);
1211 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string
));
1213 tmp
= talloc_strdup(mem_ctx
, keytab_string
);
1219 if (strncmp(tmp
, "ANY:", 4) == 0) {
1223 memset(&keytab_string
, '\0', sizeof(keytab_string
));
1225 while (next_token_talloc(mem_ctx
, &tmp
, &kt_str
, ",")) {
1226 if (strncmp(kt_str
, "WRFILE:", 7) == 0) {
1227 found_valid_name
= true;
1232 if (strncmp(kt_str
, "FILE:", 5) == 0) {
1233 found_valid_name
= true;
1238 if (tmp
[0] == '/') {
1239 /* Treat as a FILE: keytab definition. */
1240 found_valid_name
= true;
1243 if (found_valid_name
) {
1244 if (tmp
[0] != '/') {
1245 ret
= KRB5_KT_BADNAME
;
1249 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, tmp
);
1258 if (!found_valid_name
) {
1259 ret
= KRB5_KT_UNKNOWN_TYPE
;
1264 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp
));
1265 ret
= krb5_kt_resolve(context
, tmp
, keytab
);
1268 TALLOC_FREE(mem_ctx
);
1272 krb5_error_code
smb_krb5_keytab_name(TALLOC_CTX
*mem_ctx
,
1273 krb5_context context
,
1275 const char **keytab_name
)
1277 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1278 krb5_error_code ret
= 0;
1280 ret
= krb5_kt_get_name(context
, keytab
,
1281 keytab_string
, MAX_KEYTAB_NAME_LEN
- 2);
1286 *keytab_name
= talloc_strdup(mem_ctx
, keytab_string
);
1287 if (!*keytab_name
) {
1294 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1295 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1296 defined(HAVE_KRB5_GET_CREDS)
1297 static krb5_error_code
smb_krb5_get_credentials_for_user_opt(krb5_context context
,
1300 krb5_principal server
,
1301 krb5_principal impersonate_princ
,
1302 krb5_creds
**out_creds
)
1304 krb5_error_code ret
;
1305 krb5_get_creds_opt opt
;
1307 ret
= krb5_get_creds_opt_alloc(context
, &opt
);
1311 krb5_get_creds_opt_add_options(context
, opt
, KRB5_GC_FORWARDABLE
);
1313 if (impersonate_princ
) {
1314 ret
= krb5_get_creds_opt_set_impersonate(context
, opt
,
1321 ret
= krb5_get_creds(context
, opt
, ccache
, server
, out_creds
);
1328 krb5_get_creds_opt_free(context
, opt
);
1332 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
1334 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
1335 static krb5_error_code
smb_krb5_get_credentials_for_user(krb5_context context
,
1338 krb5_principal server
,
1339 krb5_principal impersonate_princ
,
1340 krb5_creds
**out_creds
)
1342 krb5_error_code ret
;
1343 krb5_creds in_creds
;
1345 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1346 krb5_error_code KRB5_CALLCONV
1347 krb5_get_credentials_for_user(krb5_context context
, krb5_flags options
,
1348 krb5_ccache ccache
, krb5_creds
*in_creds
,
1349 krb5_data
*subject_cert
,
1350 krb5_creds
**out_creds
);
1351 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1353 ZERO_STRUCT(in_creds
);
1355 if (impersonate_princ
) {
1357 in_creds
.server
= me
;
1358 in_creds
.client
= impersonate_princ
;
1360 ret
= krb5_get_credentials_for_user(context
,
1361 0, /* krb5_flags options */
1364 NULL
, /* krb5_data *subject_cert */
1367 in_creds
.client
= me
;
1368 in_creds
.server
= server
;
1370 ret
= krb5_get_credentials(context
, 0, ccache
,
1371 &in_creds
, out_creds
);
1376 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1379 * smb_krb5_get_credentials
1381 * @brief Get krb5 credentials for a server
1383 * @param[in] context An initialized krb5_context
1384 * @param[in] ccache An initialized krb5_ccache
1385 * @param[in] me The krb5_principal of the caller
1386 * @param[in] server The krb5_principal of the requested service
1387 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1388 * @param[out] out_creds The returned krb5_creds structure
1389 * @return krb5_error_code
1392 krb5_error_code
smb_krb5_get_credentials(krb5_context context
,
1395 krb5_principal server
,
1396 krb5_principal impersonate_princ
,
1397 krb5_creds
**out_creds
)
1399 krb5_error_code ret
;
1400 krb5_creds
*creds
= NULL
;
1404 if (impersonate_princ
) {
1405 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
1406 ret
= smb_krb5_get_credentials_for_user_opt(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
1407 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
1408 ret
= smb_krb5_get_credentials_for_user(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
1413 krb5_creds in_creds
;
1415 ZERO_STRUCT(in_creds
);
1417 in_creds
.client
= me
;
1418 in_creds
.server
= server
;
1420 ret
= krb5_get_credentials(context
, 0, ccache
,
1433 krb5_free_creds(context
, creds
);
1439 krb5_error_code
smb_krb5_keyblock_init_contents(krb5_context context
,
1440 krb5_enctype enctype
,
1445 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
1446 return krb5_keyblock_init(context
, enctype
, data
, length
, key
);
1448 memset(key
, 0, sizeof(krb5_keyblock
));
1449 KRB5_KEY_DATA(key
) = SMB_MALLOC(length
);
1450 if (NULL
== KRB5_KEY_DATA(key
)) {
1453 memcpy(KRB5_KEY_DATA(key
), data
, length
);
1454 KRB5_KEY_LENGTH(key
) = length
;
1455 KRB5_KEY_TYPE(key
) = enctype
;
1461 simulate a kinit, putting the tgt in the given credentials cache.
1462 Orignally by remus@snapserver.com
1464 This version is built to use a keyblock, rather than needing the
1467 The impersonate_principal is the principal if NULL, or the principal
1470 The target_service defaults to the krbtgt if NULL, but could be
1471 kpasswd/realm or the local service (if we are doing s4u2self)
1473 krb5_error_code
kerberos_kinit_keyblock_cc(krb5_context ctx
, krb5_ccache cc
,
1474 krb5_principal principal
,
1475 krb5_keyblock
*keyblock
,
1476 const char *target_service
,
1477 krb5_get_init_creds_opt
*krb_options
,
1478 time_t *expire_time
,
1481 krb5_error_code code
= 0;
1482 krb5_creds my_creds
;
1484 #if defined(HAVE_KRB5_GET_INIT_CREDS_KEYBLOCK)
1485 code
= krb5_get_init_creds_keyblock(ctx
, &my_creds
, principal
,
1486 keyblock
, 0, target_service
,
1488 #elif defined(HAVE_KRB5_GET_INIT_CREDS_KEYTAB)
1490 #define SMB_CREDS_KEYTAB "MEMORY:tmp_smb_creds_XXXXXX"
1491 char tmp_name
[sizeof(SMB_CREDS_KEYTAB
)];
1492 krb5_keytab_entry entry
;
1495 memset(&entry
, 0, sizeof(entry
));
1496 entry
.principal
= principal
;
1497 *(KRB5_KT_KEY(&entry
)) = *keyblock
;
1499 memcpy(tmp_name
, SMB_CREDS_KEYTAB
, sizeof(SMB_CREDS_KEYTAB
));
1501 if (tmp_name
[0] == 0) {
1502 return KRB5_KT_BADNAME
;
1504 code
= krb5_kt_resolve(ctx
, tmp_name
, &keytab
);
1509 code
= krb5_kt_add_entry(ctx
, keytab
, &entry
);
1511 (void)krb5_kt_close(ctx
, keytab
);
1515 code
= krb5_get_init_creds_keytab(ctx
, &my_creds
, principal
,
1516 keytab
, 0, target_service
,
1518 (void)krb5_kt_close(ctx
, keytab
);
1521 #error krb5_get_init_creds_keyblock not available!
1527 code
= krb5_cc_initialize(ctx
, cc
, principal
);
1532 code
= krb5_cc_store_cred(ctx
, cc
, &my_creds
);
1538 *expire_time
= (time_t) my_creds
.times
.endtime
;
1542 *kdc_time
= (time_t) my_creds
.times
.starttime
;
1547 krb5_free_cred_contents(ctx
, &my_creds
);
1551 krb5_error_code
kerberos_kinit_password_cc(krb5_context ctx
, krb5_ccache cc
,
1552 krb5_principal principal
,
1553 const char *password
,
1554 const char *target_service
,
1555 krb5_get_init_creds_opt
*krb_options
,
1556 time_t *expire_time
,
1559 krb5_error_code code
= 0;
1560 krb5_creds my_creds
;
1562 code
= krb5_get_init_creds_password(ctx
, &my_creds
, principal
,
1563 password
, NULL
, NULL
, 0,
1564 target_service
, krb_options
);
1569 code
= krb5_cc_initialize(ctx
, cc
, principal
);
1574 code
= krb5_cc_store_cred(ctx
, cc
, &my_creds
);
1580 *expire_time
= (time_t) my_creds
.times
.endtime
;
1584 *kdc_time
= (time_t) my_creds
.times
.starttime
;
1589 krb5_free_cred_contents(ctx
, &my_creds
);
1593 #ifdef SAMBA4_USES_HEIMDAL
1595 simulate a kinit, putting the tgt in the given credentials cache.
1596 Orignally by remus@snapserver.com
1598 The impersonate_principal is the principal
1600 The self_service, should be the local service (for S4U2Self if
1601 impersonate_principal is given).
1603 The target_service defaults to the krbtgt if NULL, but could be
1604 kpasswd/realm or a remote service (for S4U2Proxy)
1607 krb5_error_code
kerberos_kinit_s4u2_cc(krb5_context ctx
,
1608 krb5_ccache store_cc
,
1609 krb5_principal init_principal
,
1610 const char *init_password
,
1611 krb5_principal impersonate_principal
,
1612 const char *self_service
,
1613 const char *target_service
,
1614 krb5_get_init_creds_opt
*krb_options
,
1615 time_t *expire_time
,
1618 krb5_error_code code
= 0;
1619 krb5_get_creds_opt options
;
1620 krb5_principal store_principal
;
1621 krb5_creds store_creds
;
1622 krb5_creds
*s4u2self_creds
;
1623 Ticket s4u2self_ticket
;
1624 size_t s4u2self_ticketlen
;
1625 krb5_creds
*s4u2proxy_creds
;
1626 krb5_principal self_princ
;
1628 krb5_principal target_princ
;
1630 const char *self_realm
;
1631 krb5_principal blacklist_principal
= NULL
;
1632 krb5_principal whitelist_principal
= NULL
;
1634 code
= krb5_get_init_creds_password(ctx
, &store_creds
,
1645 store_principal
= init_principal
;
1648 * We are trying S4U2Self now:
1650 * As we do not want to expose our TGT in the
1651 * krb5_ccache, which is also holds the impersonated creds.
1653 * Some low level krb5/gssapi function might use the TGT
1654 * identity and let the client act as our machine account.
1656 * We need to avoid that and use a temporary krb5_ccache
1657 * in order to pass our TGT to the krb5_get_creds() function.
1659 code
= krb5_cc_new_unique(ctx
, NULL
, NULL
, &tmp_cc
);
1661 krb5_free_cred_contents(ctx
, &store_creds
);
1665 code
= krb5_cc_initialize(ctx
, tmp_cc
, store_creds
.client
);
1667 krb5_cc_destroy(ctx
, tmp_cc
);
1668 krb5_free_cred_contents(ctx
, &store_creds
);
1672 code
= krb5_cc_store_cred(ctx
, tmp_cc
, &store_creds
);
1674 krb5_free_cred_contents(ctx
, &store_creds
);
1675 krb5_cc_destroy(ctx
, tmp_cc
);
1680 * we need to remember the client principal of our
1681 * TGT and make sure the KDC does not return this
1682 * in the impersonated tickets. This can happen
1683 * if the KDC does not support S4U2Self and S4U2Proxy.
1685 blacklist_principal
= store_creds
.client
;
1686 store_creds
.client
= NULL
;
1687 krb5_free_cred_contents(ctx
, &store_creds
);
1690 * Check if we also need S4U2Proxy or if S4U2Self is
1691 * enough in order to get a ticket for the target.
1693 if (target_service
== NULL
) {
1695 } else if (strcmp(target_service
, self_service
) == 0) {
1702 * For S4U2Self we need our own service principal,
1703 * which belongs to our own realm (available on
1704 * our client principal).
1706 self_realm
= krb5_principal_get_realm(ctx
, init_principal
);
1708 code
= krb5_parse_name(ctx
, self_service
, &self_princ
);
1710 krb5_free_principal(ctx
, blacklist_principal
);
1711 krb5_cc_destroy(ctx
, tmp_cc
);
1715 code
= krb5_principal_set_realm(ctx
, self_princ
, self_realm
);
1717 krb5_free_principal(ctx
, blacklist_principal
);
1718 krb5_free_principal(ctx
, self_princ
);
1719 krb5_cc_destroy(ctx
, tmp_cc
);
1723 code
= krb5_get_creds_opt_alloc(ctx
, &options
);
1725 krb5_free_principal(ctx
, blacklist_principal
);
1726 krb5_free_principal(ctx
, self_princ
);
1727 krb5_cc_destroy(ctx
, tmp_cc
);
1733 * If we want S4U2Proxy, we need the forwardable flag
1734 * on the S4U2Self ticket.
1736 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_FORWARDABLE
);
1739 code
= krb5_get_creds_opt_set_impersonate(ctx
, options
,
1740 impersonate_principal
);
1742 krb5_get_creds_opt_free(ctx
, options
);
1743 krb5_free_principal(ctx
, blacklist_principal
);
1744 krb5_free_principal(ctx
, self_princ
);
1745 krb5_cc_destroy(ctx
, tmp_cc
);
1749 code
= krb5_get_creds(ctx
, options
, tmp_cc
,
1750 self_princ
, &s4u2self_creds
);
1751 krb5_get_creds_opt_free(ctx
, options
);
1752 krb5_free_principal(ctx
, self_princ
);
1754 krb5_free_principal(ctx
, blacklist_principal
);
1755 krb5_cc_destroy(ctx
, tmp_cc
);
1760 krb5_cc_destroy(ctx
, tmp_cc
);
1763 * Now make sure we store the impersonated principal
1764 * and creds instead of the TGT related stuff
1765 * in the krb5_ccache of the caller.
1767 code
= krb5_copy_creds_contents(ctx
, s4u2self_creds
,
1769 krb5_free_creds(ctx
, s4u2self_creds
);
1775 * It's important to store the principal the KDC
1776 * returned, as otherwise the caller would not find
1777 * the S4U2Self ticket in the krb5_ccache lookup.
1779 store_principal
= store_creds
.client
;
1784 * We are trying S4U2Proxy:
1786 * We need the ticket from the S4U2Self step
1787 * and our TGT in order to get the delegated ticket.
1789 code
= decode_Ticket((const uint8_t *)s4u2self_creds
->ticket
.data
,
1790 s4u2self_creds
->ticket
.length
,
1792 &s4u2self_ticketlen
);
1794 krb5_free_creds(ctx
, s4u2self_creds
);
1795 krb5_free_principal(ctx
, blacklist_principal
);
1796 krb5_cc_destroy(ctx
, tmp_cc
);
1801 * we need to remember the client principal of the
1802 * S4U2Self stage and as it needs to match the one we
1803 * will get for the S4U2Proxy stage. We need this
1804 * in order to detect KDCs which does not support S4U2Proxy.
1806 whitelist_principal
= s4u2self_creds
->client
;
1807 s4u2self_creds
->client
= NULL
;
1808 krb5_free_creds(ctx
, s4u2self_creds
);
1811 * For S4U2Proxy we also got a target service principal,
1812 * which also belongs to our own realm (available on
1813 * our client principal).
1815 code
= krb5_parse_name(ctx
, target_service
, &target_princ
);
1817 free_Ticket(&s4u2self_ticket
);
1818 krb5_free_principal(ctx
, whitelist_principal
);
1819 krb5_free_principal(ctx
, blacklist_principal
);
1820 krb5_cc_destroy(ctx
, tmp_cc
);
1824 code
= krb5_principal_set_realm(ctx
, target_princ
, self_realm
);
1826 free_Ticket(&s4u2self_ticket
);
1827 krb5_free_principal(ctx
, target_princ
);
1828 krb5_free_principal(ctx
, whitelist_principal
);
1829 krb5_free_principal(ctx
, blacklist_principal
);
1830 krb5_cc_destroy(ctx
, tmp_cc
);
1834 code
= krb5_get_creds_opt_alloc(ctx
, &options
);
1836 free_Ticket(&s4u2self_ticket
);
1837 krb5_free_principal(ctx
, target_princ
);
1838 krb5_free_principal(ctx
, whitelist_principal
);
1839 krb5_free_principal(ctx
, blacklist_principal
);
1840 krb5_cc_destroy(ctx
, tmp_cc
);
1844 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_FORWARDABLE
);
1845 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_CONSTRAINED_DELEGATION
);
1847 code
= krb5_get_creds_opt_set_ticket(ctx
, options
, &s4u2self_ticket
);
1848 free_Ticket(&s4u2self_ticket
);
1850 krb5_get_creds_opt_free(ctx
, options
);
1851 krb5_free_principal(ctx
, target_princ
);
1852 krb5_free_principal(ctx
, whitelist_principal
);
1853 krb5_free_principal(ctx
, blacklist_principal
);
1854 krb5_cc_destroy(ctx
, tmp_cc
);
1858 code
= krb5_get_creds(ctx
, options
, tmp_cc
,
1859 target_princ
, &s4u2proxy_creds
);
1860 krb5_get_creds_opt_free(ctx
, options
);
1861 krb5_free_principal(ctx
, target_princ
);
1862 krb5_cc_destroy(ctx
, tmp_cc
);
1864 krb5_free_principal(ctx
, whitelist_principal
);
1865 krb5_free_principal(ctx
, blacklist_principal
);
1870 * Now make sure we store the impersonated principal
1871 * and creds instead of the TGT related stuff
1872 * in the krb5_ccache of the caller.
1874 code
= krb5_copy_creds_contents(ctx
, s4u2proxy_creds
,
1876 krb5_free_creds(ctx
, s4u2proxy_creds
);
1878 krb5_free_principal(ctx
, whitelist_principal
);
1879 krb5_free_principal(ctx
, blacklist_principal
);
1884 * It's important to store the principal the KDC
1885 * returned, as otherwise the caller would not find
1886 * the S4U2Self ticket in the krb5_ccache lookup.
1888 store_principal
= store_creds
.client
;
1891 if (blacklist_principal
&&
1892 krb5_principal_compare(ctx
, store_creds
.client
, blacklist_principal
)) {
1896 code
= krb5_unparse_name(ctx
, blacklist_principal
, &sp
);
1900 code
= krb5_unparse_name(ctx
, impersonate_principal
, &ip
);
1904 DEBUG(1, ("kerberos_kinit_password_cc: "
1905 "KDC returned self principal[%s] while impersonating [%s]\n",
1906 sp
?sp
:"<no memory>",
1907 ip
?ip
:"<no memory>"));
1912 krb5_free_principal(ctx
, whitelist_principal
);
1913 krb5_free_principal(ctx
, blacklist_principal
);
1914 krb5_free_cred_contents(ctx
, &store_creds
);
1915 return KRB5_FWD_BAD_PRINCIPAL
;
1917 if (blacklist_principal
) {
1918 krb5_free_principal(ctx
, blacklist_principal
);
1921 if (whitelist_principal
&&
1922 !krb5_principal_compare(ctx
, store_creds
.client
, whitelist_principal
)) {
1926 code
= krb5_unparse_name(ctx
, store_creds
.client
, &sp
);
1930 code
= krb5_unparse_name(ctx
, whitelist_principal
, &ep
);
1934 DEBUG(1, ("kerberos_kinit_password_cc: "
1935 "KDC returned wrong principal[%s] we expected [%s]\n",
1936 sp
?sp
:"<no memory>",
1937 ep
?ep
:"<no memory>"));
1942 krb5_free_principal(ctx
, whitelist_principal
);
1943 krb5_free_cred_contents(ctx
, &store_creds
);
1944 return KRB5_FWD_BAD_PRINCIPAL
;
1946 if (whitelist_principal
) {
1947 krb5_free_principal(ctx
, whitelist_principal
);
1950 code
= krb5_cc_initialize(ctx
, store_cc
, store_principal
);
1952 krb5_free_cred_contents(ctx
, &store_creds
);
1956 code
= krb5_cc_store_cred(ctx
, store_cc
, &store_creds
);
1958 krb5_free_cred_contents(ctx
, &store_creds
);
1963 *expire_time
= (time_t) store_creds
.times
.endtime
;
1967 *kdc_time
= (time_t) store_creds
.times
.starttime
;
1970 krb5_free_cred_contents(ctx
, &store_creds
);
1976 #if !defined(HAVE_KRB5_MAKE_PRINCIPAL) && defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
1977 krb5_error_code
smb_krb5_make_principal(krb5_context context
,
1978 krb5_principal
*principal
,
1979 const char *_realm
, ...)
1981 krb5_error_code code
;
1990 code
= krb5_get_default_realm(context
, &realm
);
1997 va_start(ap
, _realm
);
1998 code
= krb5_build_principal_alloc_va(context
, principal
,
1999 strlen(realm
), realm
,
2004 krb5_free_default_realm(context
, realm
);
2011 #if !defined(HAVE_KRB5_CC_GET_LIFETIME) && defined(HAVE_KRB5_CC_RETRIEVE_CRED)
2013 * @brief Get the lifetime of the initial ticket in the cache.
2015 * @param[in] context The kerberos context.
2017 * @param[in] id The credential cache to get the ticket lifetime.
2019 * @param[out] t A pointer to a time value to store the lifetime.
2021 * @return 0 on success, a krb5_error_code on error.
2023 krb5_error_code
smb_krb5_cc_get_lifetime(krb5_context context
,
2027 krb5_cc_cursor cursor
;
2028 krb5_error_code kerr
;
2034 kerr
= krb5_timeofday(context
, &now
);
2039 kerr
= krb5_cc_start_seq_get(context
, id
, &cursor
);
2044 while ((kerr
= krb5_cc_next_cred(context
, id
, &cursor
, &cred
)) == 0) {
2045 #ifndef HAVE_FLAGS_IN_KRB5_CREDS
2046 if (cred
.ticket_flags
& TKT_FLG_INITIAL
) {
2048 if (cred
.flags
.b
.initial
) {
2050 if (now
< cred
.times
.endtime
) {
2051 *t
= (time_t) (cred
.times
.endtime
- now
);
2053 krb5_free_cred_contents(context
, &cred
);
2056 krb5_free_cred_contents(context
, &cred
);
2059 krb5_cc_end_seq_get(context
, id
, &cursor
);
2063 #endif /* HAVE_KRB5_CC_GET_LIFETIME */
2065 #if !defined(HAVE_KRB5_FREE_CHECKSUM_CONTENTS) && defined(HAVE_FREE_CHECKSUM)
2066 void smb_krb5_free_checksum_contents(krb5_context ctx
, krb5_checksum
*cksum
)
2068 free_Checksum(cksum
);
2072 krb5_error_code
smb_krb5_make_pac_checksum(TALLOC_CTX
*mem_ctx
,
2073 DATA_BLOB
*pac_data
,
2074 krb5_context context
,
2075 const krb5_keyblock
*keyblock
,
2077 DATA_BLOB
*sig_blob
)
2079 krb5_error_code ret
;
2080 krb5_checksum cksum
;
2081 #if defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CREATE_CHECKSUM)
2085 ret
= krb5_crypto_init(context
,
2090 DEBUG(0,("krb5_crypto_init() failed: %s\n",
2091 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
2094 ret
= krb5_create_checksum(context
,
2096 KRB5_KU_OTHER_CKSUM
,
2102 DEBUG(2, ("PAC Verification failed: %s\n",
2103 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
2106 krb5_crypto_destroy(context
, crypto
);
2112 *sig_type
= cksum
.cksumtype
;
2113 *sig_blob
= data_blob_talloc(mem_ctx
,
2114 cksum
.checksum
.data
,
2115 cksum
.checksum
.length
);
2116 #elif defined(HAVE_KRB5_C_MAKE_CHECKSUM)
2119 input
.data
= (char *)pac_data
->data
;
2120 input
.length
= pac_data
->length
;
2122 ret
= krb5_c_make_checksum(context
,
2125 KRB5_KEYUSAGE_APP_DATA_CKSUM
,
2129 DEBUG(2, ("PAC Verification failed: %s\n",
2130 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
2134 *sig_type
= cksum
.checksum_type
;
2135 *sig_blob
= data_blob_talloc(mem_ctx
,
2140 #error krb5_create_checksum or krb5_c_make_checksum not available
2141 #endif /* HAVE_KRB5_C_MAKE_CHECKSUM */
2142 smb_krb5_free_checksum_contents(context
, &cksum
);
2149 * smb_krb5_principal_get_realm
2151 * @brief Get realm of a principal
2153 * @param[in] context The krb5_context
2154 * @param[in] principal The principal
2155 * @return pointer to the realm
2159 char *smb_krb5_principal_get_realm(krb5_context context
,
2160 krb5_principal principal
)
2162 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2163 return discard_const_p(char, krb5_principal_get_realm(context
, principal
));
2164 #elif defined(krb5_princ_realm) /* MIT */
2166 realm
= krb5_princ_realm(context
, principal
);
2167 return discard_const_p(char, realm
->data
);
2173 /************************************************************************
2174 Routine to get the default realm from the kerberos credentials cache.
2175 Caller must free if the return value is not NULL.
2176 ************************************************************************/
2178 static char *smb_krb5_get_default_realm_from_ccache(TALLOC_CTX
*mem_ctx
)
2181 krb5_context ctx
= NULL
;
2182 krb5_ccache cc
= NULL
;
2183 krb5_principal princ
= NULL
;
2185 initialize_krb5_error_table();
2186 if (krb5_init_context(&ctx
)) {
2190 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2191 "Trying to read krb5 cache: %s\n",
2192 krb5_cc_default_name(ctx
)));
2193 if (krb5_cc_default(ctx
, &cc
)) {
2194 DEBUG(0,("kerberos_get_default_realm_from_ccache: "
2195 "failed to read default cache\n"));
2198 if (krb5_cc_get_principal(ctx
, cc
, &princ
)) {
2199 DEBUG(0,("kerberos_get_default_realm_from_ccache: "
2200 "failed to get default principal\n"));
2204 #if defined(HAVE_KRB5_PRINCIPAL_GET_REALM)
2205 realm
= talloc_strdup(mem_ctx
, krb5_principal_get_realm(ctx
, princ
));
2206 #elif defined(HAVE_KRB5_PRINC_REALM)
2208 krb5_data
*realm_data
= krb5_princ_realm(ctx
, princ
);
2209 realm
= talloc_strndup(mem_ctx
, realm_data
->data
, realm_data
->length
);
2217 krb5_free_principal(ctx
, princ
);
2220 krb5_cc_close(ctx
, cc
);
2222 krb5_free_context(ctx
);
2228 /************************************************************************
2229 Routine to get the realm from a given DNS name.
2230 ************************************************************************/
2232 static char *smb_krb5_get_realm_from_hostname(TALLOC_CTX
*mem_ctx
,
2233 const char *hostname
)
2235 #if defined(HAVE_KRB5_REALM_TYPE)
2237 krb5_realm
*realm_list
= NULL
;
2240 char **realm_list
= NULL
;
2243 krb5_error_code kerr
;
2244 krb5_context ctx
= NULL
;
2246 initialize_krb5_error_table();
2247 if (krb5_init_context(&ctx
)) {
2251 kerr
= krb5_get_host_realm(ctx
, hostname
, &realm_list
);
2253 DEBUG(3,("kerberos_get_realm_from_hostname %s: "
2255 hostname
? hostname
: "(NULL)",
2256 error_message(kerr
) ));
2260 if (realm_list
&& realm_list
[0]) {
2261 realm
= talloc_strdup(mem_ctx
, realm_list
[0]);
2268 krb5_free_host_realm(ctx
, realm_list
);
2271 krb5_free_context(ctx
);
2277 char *kerberos_get_principal_from_service_hostname(TALLOC_CTX
*mem_ctx
,
2278 const char *service
,
2279 const char *remote_name
,
2280 const char *default_realm
)
2285 host
= strchr_m(remote_name
, '.');
2288 realm
= smb_krb5_get_realm_from_hostname(talloc_tos(),
2291 /* NetBIOS name - use our realm. */
2292 realm
= smb_krb5_get_default_realm_from_ccache(talloc_tos());
2295 if (realm
== NULL
|| *realm
== '\0') {
2296 realm
= talloc_strdup(talloc_tos(), default_realm
);
2300 DEBUG(3,("kerberos_get_principal_from_service_hostname: "
2301 "cannot get realm from, "
2302 "desthost %s or default ccache. Using default "
2303 "smb.conf realm %s\n",
2308 principal
= talloc_asprintf(mem_ctx
,
2310 service
, remote_name
,
2316 char *smb_get_krb5_error_message(krb5_context context
,
2317 krb5_error_code code
,
2318 TALLOC_CTX
*mem_ctx
)
2322 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
2323 const char *context_error
= krb5_get_error_message(context
, code
);
2324 if (context_error
) {
2325 ret
= talloc_asprintf(mem_ctx
, "%s: %s",
2326 error_message(code
), context_error
);
2327 krb5_free_error_message(context
, context_error
);
2331 ret
= talloc_strdup(mem_ctx
, error_message(code
));
2335 #else /* HAVE_KRB5 */
2336 /* this saves a few linking headaches */
2337 int cli_krb5_get_ticket(TALLOC_CTX
*mem_ctx
,
2338 const char *principal
, time_t time_offset
,
2339 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
2340 uint32_t extra_ap_opts
,
2341 const char *ccname
, time_t *tgs_expire
,
2342 const char *impersonate_princ_s
)
2344 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2348 #endif /* HAVE_KRB5 */