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/>.
25 #include "../librpc/gen_ndr/krb5pac.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. */
42 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
43 but still has the symbol */
44 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
45 krb5_error_code
krb5_auth_con_set_req_cksumtype(
47 krb5_auth_context auth_context
,
48 krb5_cksumtype cksumtype
);
51 /**************************************************************
52 Wrappers around kerberos string functions that convert from
53 utf8 -> unix charset and vica versa.
54 **************************************************************/
56 /**************************************************************
57 krb5_parse_name that takes a UNIX charset.
58 **************************************************************/
60 krb5_error_code
smb_krb5_parse_name(krb5_context context
,
61 const char *name
, /* in unix charset */
62 krb5_principal
*principal
)
66 size_t converted_size
;
68 if (!push_utf8_talloc(talloc_tos(), &utf8_name
, name
, &converted_size
)) {
72 ret
= krb5_parse_name(context
, utf8_name
, principal
);
73 TALLOC_FREE(utf8_name
);
77 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
78 /**************************************************************
79 krb5_parse_name_norealm that takes a UNIX charset.
80 **************************************************************/
82 static krb5_error_code
smb_krb5_parse_name_norealm_conv(krb5_context context
,
83 const char *name
, /* in unix charset */
84 krb5_principal
*principal
)
88 size_t converted_size
;
91 if (!push_utf8_talloc(talloc_tos(), &utf8_name
, name
, &converted_size
)) {
95 ret
= krb5_parse_name_norealm(context
, utf8_name
, principal
);
96 TALLOC_FREE(utf8_name
);
101 /**************************************************************
102 krb5_parse_name that returns a UNIX charset name. Must
103 be freed with talloc_free() call.
104 **************************************************************/
106 krb5_error_code
smb_krb5_unparse_name(TALLOC_CTX
*mem_ctx
,
107 krb5_context context
,
108 krb5_const_principal principal
,
113 size_t converted_size
;
116 ret
= krb5_unparse_name(context
, principal
, &utf8_name
);
121 if (!pull_utf8_talloc(mem_ctx
, unix_name
, utf8_name
, &converted_size
)) {
122 krb5_free_unparsed_name(context
, utf8_name
);
125 krb5_free_unparsed_name(context
, utf8_name
);
129 #ifndef HAVE_KRB5_SET_REAL_TIME
131 * This function is not in the Heimdal mainline.
133 krb5_error_code
krb5_set_real_time(krb5_context context
, int32_t seconds
, int32_t microseconds
)
138 ret
= krb5_us_timeofday(context
, &sec
, &usec
);
142 context
->kdc_sec_offset
= seconds
- sec
;
143 context
->kdc_usec_offset
= microseconds
- usec
;
149 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
151 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
153 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
154 * to krb5_set_default_tgs_ktypes. See
155 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
157 * If the MIT libraries are not exporting internal symbols, we will end up in
158 * this branch, which is correct. Otherwise we will continue to use the
161 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
163 return krb5_set_default_tgs_enctypes(ctx
, enc
);
166 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
169 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
171 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
174 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
176 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
178 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
180 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
182 memset(pkaddr
, '\0', sizeof(krb5_address
));
183 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
184 if (paddr
->ss_family
== AF_INET6
) {
185 pkaddr
->addr_type
= KRB5_ADDRESS_INET6
;
186 pkaddr
->address
.length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
187 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
191 if (paddr
->ss_family
== AF_INET
) {
192 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
193 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
194 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
199 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
201 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
203 memset(pkaddr
, '\0', sizeof(krb5_address
));
204 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
205 if (paddr
->ss_family
== AF_INET6
) {
206 pkaddr
->addrtype
= ADDRTYPE_INET6
;
207 pkaddr
->length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
208 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
212 if (paddr
->ss_family
== AF_INET
) {
213 pkaddr
->addrtype
= ADDRTYPE_INET
;
214 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
215 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
221 #error UNKNOWN_ADDRTYPE
224 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
225 static int create_kerberos_key_from_string_direct(krb5_context context
,
226 krb5_principal host_princ
,
229 krb5_enctype enctype
)
233 krb5_encrypt_block eblock
;
235 ret
= krb5_principal2salt(context
, host_princ
, &salt
);
237 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret
)));
240 krb5_use_enctype(context
, &eblock
, enctype
);
241 ret
= krb5_string_to_key(context
, &eblock
, key
, password
, &salt
);
242 SAFE_FREE(salt
.data
);
246 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
247 static int create_kerberos_key_from_string_direct(krb5_context context
,
248 krb5_principal host_princ
,
251 krb5_enctype enctype
)
256 ret
= krb5_get_pw_salt(context
, host_princ
, &salt
);
258 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret
)));
262 ret
= krb5_string_to_key_salt(context
, enctype
, (const char *)password
->data
, salt
, key
);
263 krb5_free_salt(context
, salt
);
268 #error UNKNOWN_CREATE_KEY_FUNCTIONS
271 int create_kerberos_key_from_string(krb5_context context
,
272 krb5_principal host_princ
,
275 krb5_enctype enctype
,
278 krb5_principal salt_princ
= NULL
;
281 * Check if we've determined that the KDC is salting keys for this
282 * principal/enctype in a non-obvious way. If it is, try to match
286 KRB5_KEY_DATA(key
) = (KRB5_KEY_DATA_CAST
*)SMB_MALLOC(password
->length
);
287 if (!KRB5_KEY_DATA(key
)) {
290 memcpy(KRB5_KEY_DATA(key
), password
->data
, password
->length
);
291 KRB5_KEY_LENGTH(key
) = password
->length
;
292 KRB5_KEY_TYPE(key
) = enctype
;
295 salt_princ
= kerberos_fetch_salt_princ_for_host_princ(context
, host_princ
, enctype
);
296 ret
= create_kerberos_key_from_string_direct(context
, salt_princ
? salt_princ
: host_princ
, password
, key
, enctype
);
298 krb5_free_principal(context
, salt_princ
);
303 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
304 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
305 krb5_enctype
**enctypes
)
307 return krb5_get_permitted_enctypes(context
, enctypes
);
309 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
310 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
311 krb5_enctype
**enctypes
)
313 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
316 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
319 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
320 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
321 krb5_auth_context auth_context
,
322 krb5_keyblock
*keyblock
)
324 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
328 bool unwrap_edata_ntstatus(TALLOC_CTX
*mem_ctx
,
330 DATA_BLOB
*edata_out
)
332 DATA_BLOB edata_contents
;
336 if (!edata
->length
) {
340 data
= asn1_init(mem_ctx
);
345 asn1_load(data
, *edata
);
346 asn1_start_tag(data
, ASN1_SEQUENCE(0));
347 asn1_start_tag(data
, ASN1_CONTEXT(1));
348 asn1_read_Integer(data
, &edata_type
);
350 if (edata_type
!= KRB5_PADATA_PW_SALT
) {
351 DEBUG(0,("edata is not of required type %d but of type %d\n",
352 KRB5_PADATA_PW_SALT
, edata_type
));
357 asn1_start_tag(data
, ASN1_CONTEXT(2));
358 asn1_read_OctetString(data
, talloc_tos(), &edata_contents
);
364 *edata_out
= data_blob_talloc(mem_ctx
, edata_contents
.data
, edata_contents
.length
);
366 data_blob_free(&edata_contents
);
372 bool unwrap_pac(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, DATA_BLOB
*unwrapped_pac_data
)
374 DATA_BLOB pac_contents
;
378 if (!auth_data
->length
) {
382 data
= asn1_init(mem_ctx
);
387 asn1_load(data
, *auth_data
);
388 asn1_start_tag(data
, ASN1_SEQUENCE(0));
389 asn1_start_tag(data
, ASN1_SEQUENCE(0));
390 asn1_start_tag(data
, ASN1_CONTEXT(0));
391 asn1_read_Integer(data
, &data_type
);
393 if (data_type
!= KRB5_AUTHDATA_WIN2K_PAC
) {
394 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type
));
400 asn1_start_tag(data
, ASN1_CONTEXT(1));
401 asn1_read_OctetString(data
, talloc_tos(), &pac_contents
);
407 *unwrapped_pac_data
= data_blob_talloc(mem_ctx
, pac_contents
.data
, pac_contents
.length
);
409 data_blob_free(&pac_contents
);
414 bool get_auth_data_from_tkt(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, krb5_ticket
*tkt
)
416 DATA_BLOB auth_data_wrapped
;
417 bool got_auth_data_pac
= False
;
420 #if defined(HAVE_KRB5_TKT_ENC_PART2)
421 if (tkt
->enc_part2
&& tkt
->enc_part2
->authorization_data
&&
422 tkt
->enc_part2
->authorization_data
[0] &&
423 tkt
->enc_part2
->authorization_data
[0]->length
)
425 for (i
= 0; tkt
->enc_part2
->authorization_data
[i
] != NULL
; i
++) {
427 if (tkt
->enc_part2
->authorization_data
[i
]->ad_type
!=
428 KRB5_AUTHDATA_IF_RELEVANT
) {
429 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
430 tkt
->enc_part2
->authorization_data
[i
]->ad_type
));
434 auth_data_wrapped
= data_blob(tkt
->enc_part2
->authorization_data
[i
]->contents
,
435 tkt
->enc_part2
->authorization_data
[i
]->length
);
437 /* check if it is a PAC */
438 got_auth_data_pac
= unwrap_pac(mem_ctx
, &auth_data_wrapped
, auth_data
);
439 data_blob_free(&auth_data_wrapped
);
441 if (got_auth_data_pac
) {
446 return got_auth_data_pac
;
450 if (tkt
->ticket
.authorization_data
&&
451 tkt
->ticket
.authorization_data
->len
)
453 for (i
= 0; i
< tkt
->ticket
.authorization_data
->len
; i
++) {
455 if (tkt
->ticket
.authorization_data
->val
[i
].ad_type
!=
456 KRB5_AUTHDATA_IF_RELEVANT
) {
457 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
458 tkt
->ticket
.authorization_data
->val
[i
].ad_type
));
462 auth_data_wrapped
= data_blob(tkt
->ticket
.authorization_data
->val
[i
].ad_data
.data
,
463 tkt
->ticket
.authorization_data
->val
[i
].ad_data
.length
);
465 /* check if it is a PAC */
466 got_auth_data_pac
= unwrap_pac(mem_ctx
, &auth_data_wrapped
, auth_data
);
467 data_blob_free(&auth_data_wrapped
);
469 if (got_auth_data_pac
) {
474 return got_auth_data_pac
;
480 krb5_const_principal
get_principal_from_tkt(krb5_ticket
*tkt
)
482 #if defined(HAVE_KRB5_TKT_ENC_PART2)
483 return tkt
->enc_part2
->client
;
489 #if !defined(HAVE_KRB5_LOCATE_KDC)
491 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
492 * to a public interface for this functionality, so we have to be able to live
493 * without it if the MIT libraries are hiding their internal symbols.
496 #if defined(KRB5_KRBHST_INIT)
498 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
, struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
500 krb5_krbhst_handle hnd
;
501 krb5_krbhst_info
*hinfo
;
510 rc
= krb5_krbhst_init(ctx
, realm
->data
, KRB5_KRBHST_KDC
, &hnd
);
512 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc
)));
516 for ( num_kdcs
= 0; (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); num_kdcs
++)
519 krb5_krbhst_reset(ctx
, hnd
);
522 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
523 krb5_krbhst_free(ctx
, hnd
);
527 sa
= SMB_MALLOC_ARRAY( struct sockaddr
, num_kdcs
);
529 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
530 krb5_krbhst_free(ctx
, hnd
);
535 memset(sa
, '\0', sizeof(struct sockaddr
) * num_kdcs
);
537 for (i
= 0; i
< num_kdcs
&& (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); i
++) {
539 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
540 rc
= krb5_krbhst_get_addrinfo(ctx
, hinfo
, &ai
);
542 DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc
)));
546 if (hinfo
->ai
&& hinfo
->ai
->ai_family
== AF_INET
)
547 memcpy(&sa
[i
], hinfo
->ai
->ai_addr
, sizeof(struct sockaddr
));
550 krb5_krbhst_free(ctx
, hnd
);
557 #else /* ! defined(KRB5_KRBHST_INIT) */
559 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
,
560 struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
562 DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
563 return KRB5_KDC_UNREACH
;
566 #endif /* KRB5_KRBHST_INIT */
568 #else /* ! HAVE_KRB5_LOCATE_KDC */
570 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
,
571 struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
573 return krb5_locate_kdc(ctx
, realm
, addr_pp
, naddrs
, get_masters
);
576 #endif /* HAVE_KRB5_LOCATE_KDC */
578 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
579 void krb5_free_unparsed_name(krb5_context context
, char *val
)
585 void kerberos_free_data_contents(krb5_context context
, krb5_data
*pdata
)
587 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
589 krb5_free_data_contents(context
, pdata
);
592 SAFE_FREE(pdata
->data
);
596 void kerberos_set_creds_enctype(krb5_creds
*pcreds
, int enctype
)
598 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
599 KRB5_KEY_TYPE((&pcreds
->keyblock
)) = enctype
;
600 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
601 KRB5_KEY_TYPE((&pcreds
->session
)) = enctype
;
603 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
607 bool kerberos_compatible_enctypes(krb5_context context
,
608 krb5_enctype enctype1
,
609 krb5_enctype enctype2
)
611 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
612 krb5_boolean similar
= 0;
614 krb5_c_enctype_compare(context
, enctype1
, enctype2
, &similar
);
615 return similar
? True
: False
;
616 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
617 return krb5_enctypes_compatible_keys(context
, enctype1
, enctype2
) ? True
: False
;
621 static bool ads_cleanup_expired_creds(krb5_context context
,
625 krb5_error_code retval
;
626 const char *cc_type
= krb5_cc_get_type(context
, ccache
);
628 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
629 cc_type
, krb5_cc_get_name(context
, ccache
),
630 http_timestring(talloc_tos(), credsp
->times
.endtime
)));
632 /* we will probably need new tickets if the current ones
633 will expire within 10 seconds.
635 if (credsp
->times
.endtime
>= (time(NULL
) + 10))
638 /* heimdal won't remove creds from a file ccache, and
639 perhaps we shouldn't anyway, since internally we
640 use memory ccaches, and a FILE one probably means that
641 we're using creds obtained outside of our exectuable
643 if (strequal(cc_type
, "FILE")) {
644 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type
));
648 retval
= krb5_cc_remove_cred(context
, ccache
, 0, credsp
);
650 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
651 error_message(retval
)));
652 /* If we have an error in this, we want to display it,
653 but continue as though we deleted it */
658 /* Allocate and setup the auth context into the state we need. */
660 static krb5_error_code
setup_auth_context(krb5_context context
,
661 krb5_auth_context
*auth_context
)
663 krb5_error_code retval
;
665 retval
= krb5_auth_con_init(context
, auth_context
);
667 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
668 error_message(retval
)));
672 /* Ensure this is an addressless ticket. */
673 retval
= krb5_auth_con_setaddrs(context
, *auth_context
, NULL
, NULL
);
675 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
676 error_message(retval
)));
682 static krb5_error_code
create_gss_checksum(krb5_data
*in_data
, /* [inout] */
685 unsigned int orig_length
= in_data
->length
;
686 unsigned int base_cksum_size
= GSSAPI_CHECKSUM_SIZE
;
687 char *gss_cksum
= NULL
;
690 /* Extra length field for delgated ticket. */
691 base_cksum_size
+= 4;
694 if ((unsigned int)base_cksum_size
+ orig_length
<
695 (unsigned int)base_cksum_size
) {
699 gss_cksum
= (char *)SMB_MALLOC(base_cksum_size
+ orig_length
);
700 if (gss_cksum
== NULL
) {
704 memset(gss_cksum
, '\0', base_cksum_size
+ orig_length
);
705 SIVAL(gss_cksum
, 0, GSSAPI_BNDLENGTH
);
708 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
709 * This matches the behavior of heimdal and mit.
711 * And it is needed to work against some closed source
716 memset(&gss_cksum
[4], 0x00, GSSAPI_BNDLENGTH
);
718 SIVAL(gss_cksum
, 20, gss_flags
);
721 SSVAL(gss_cksum
, 24, 1); /* The Delegation Option identifier */
722 SSVAL(gss_cksum
, 26, orig_length
);
723 /* Copy the kerberos KRB_CRED data */
724 memcpy(gss_cksum
+ 28, in_data
->data
, orig_length
);
726 in_data
->data
= NULL
;
729 in_data
->data
= gss_cksum
;
730 in_data
->length
= base_cksum_size
+ orig_length
;
735 we can't use krb5_mk_req because w2k wants the service to be in a particular format
737 static krb5_error_code
ads_krb5_mk_req(krb5_context context
,
738 krb5_auth_context
*auth_context
,
739 const krb5_flags ap_req_options
,
740 const char *principal
,
744 const char *impersonate_princ_s
)
746 krb5_error_code retval
;
747 krb5_principal server
;
748 krb5_principal impersonate_princ
= NULL
;
752 bool creds_ready
= False
;
753 int i
= 0, maxtries
= 3;
754 uint32_t gss_flags
= 0;
756 ZERO_STRUCT(in_data
);
758 retval
= smb_krb5_parse_name(context
, principal
, &server
);
760 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal
));
764 if (impersonate_princ_s
) {
765 retval
= smb_krb5_parse_name(context
, impersonate_princ_s
,
768 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s
));
773 /* obtain ticket & session key */
775 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
776 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
777 error_message(retval
)));
781 if ((retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
))) {
782 /* This can commonly fail on smbd startup with no ticket in the cache.
783 * Report at higher level than 1. */
784 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
785 error_message(retval
)));
789 while (!creds_ready
&& (i
< maxtries
)) {
791 if ((retval
= smb_krb5_get_credentials(context
, ccache
,
796 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
797 principal
, error_message(retval
)));
801 /* cope with ticket being in the future due to clock skew */
802 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
803 time_t t
= time(NULL
);
804 int time_offset
=(int)((unsigned)credsp
->times
.starttime
-t
);
805 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
806 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
809 if (!ads_cleanup_expired_creds(context
, ccache
, credsp
)) {
816 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
817 principal
, krb5_cc_get_type(context
, ccache
), krb5_cc_get_name(context
, ccache
),
818 http_timestring(talloc_tos(), (unsigned)credsp
->times
.endtime
),
819 (unsigned)credsp
->times
.endtime
));
822 *expire_time
= (time_t)credsp
->times
.endtime
;
825 /* Allocate the auth_context. */
826 retval
= setup_auth_context(context
, auth_context
);
828 DEBUG(1,("setup_auth_context failed (%s)\n",
829 error_message(retval
)));
833 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
834 if( credsp
->ticket_flags
& TKT_FLG_OK_AS_DELEGATE
) {
835 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
836 as part of the kerberos exchange. */
838 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
840 retval
= krb5_auth_con_setuseruserkey(context
,
844 DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
845 error_message(retval
)));
849 /* Must use a subkey for forwarded tickets. */
850 retval
= krb5_auth_con_setflags(context
,
852 KRB5_AUTH_CONTEXT_USE_SUBKEY
);
854 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
855 error_message(retval
)));
859 retval
= krb5_fwd_tgt_creds(context
,/* Krb5 context [in] */
860 *auth_context
, /* Authentication context [in] */
861 CONST_DISCARD(char *, KRB5_TGS_NAME
), /* Ticket service name ("krbtgt") [in] */
862 credsp
->client
, /* Client principal for the tgt [in] */
863 credsp
->server
, /* Server principal for the tgt [in] */
864 ccache
, /* Credential cache to use for storage [in] */
865 1, /* Turn on for "Forwardable ticket" [in] */
866 &in_data
); /* Resulting response [out] */
869 DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n",
870 error_message( retval
) ) );
873 * This is not fatal. Delete the *auth_context and continue
874 * with krb5_mk_req_extended to get a non-forwardable ticket.
878 free( in_data
.data
);
882 krb5_auth_con_free(context
, *auth_context
);
883 *auth_context
= NULL
;
884 retval
= setup_auth_context(context
, auth_context
);
886 DEBUG(1,("setup_auth_context failed (%s)\n",
887 error_message(retval
)));
891 /* We got a delegated ticket. */
892 gss_flags
|= GSS_C_DELEG_FLAG
;
896 /* Frees and reallocates in_data into a GSS checksum blob. */
897 retval
= create_gss_checksum(&in_data
, gss_flags
);
902 /* We always want GSS-checksum types. */
903 retval
= krb5_auth_con_set_req_cksumtype(context
, *auth_context
, GSSAPI_CHECKSUM
);
905 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
906 error_message(retval
)));
911 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
912 &in_data
, credsp
, outbuf
);
914 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
915 error_message(retval
)));
920 free( in_data
.data
);
924 krb5_free_creds(context
, credsp
);
927 krb5_free_cred_contents(context
, &creds
);
930 krb5_free_principal(context
, server
);
931 if (impersonate_princ
) {
932 krb5_free_principal(context
, impersonate_princ
);
939 get a kerberos5 ticket for the given service
941 int cli_krb5_get_ticket(TALLOC_CTX
*mem_ctx
,
942 const char *principal
, time_t time_offset
,
943 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
944 uint32_t extra_ap_opts
, const char *ccname
,
946 const char *impersonate_princ_s
)
949 krb5_error_code retval
;
951 krb5_context context
= NULL
;
952 krb5_ccache ccdef
= NULL
;
953 krb5_auth_context auth_context
= NULL
;
954 krb5_enctype enc_types
[] = {
955 ENCTYPE_ARCFOUR_HMAC
,
960 initialize_krb5_error_table();
961 retval
= krb5_init_context(&context
);
963 DEBUG(1, ("krb5_init_context failed (%s)\n",
964 error_message(retval
)));
968 if (time_offset
!= 0) {
969 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
972 if ((retval
= krb5_cc_resolve(context
, ccname
?
973 ccname
: krb5_cc_default_name(context
), &ccdef
))) {
974 DEBUG(1, ("krb5_cc_default failed (%s)\n",
975 error_message(retval
)));
979 if ((retval
= krb5_set_default_tgs_ktypes(context
, enc_types
))) {
980 DEBUG(1, ("krb5_set_default_tgs_ktypes failed (%s)\n",
981 error_message(retval
)));
985 retval
= ads_krb5_mk_req(context
, &auth_context
,
986 AP_OPTS_USE_SUBKEY
| (krb5_flags
)extra_ap_opts
,
987 principal
, ccdef
, &packet
,
988 tgs_expire
, impersonate_princ_s
);
993 get_krb5_smb_session_key(mem_ctx
, context
, auth_context
,
994 session_key_krb5
, false);
996 *ticket
= data_blob_talloc(mem_ctx
, packet
.data
, packet
.length
);
998 kerberos_free_data_contents(context
, &packet
);
1004 krb5_cc_close(context
, ccdef
);
1006 krb5_auth_con_free(context
, auth_context
);
1007 krb5_free_context(context
);
1013 bool get_krb5_smb_session_key(TALLOC_CTX
*mem_ctx
,
1014 krb5_context context
,
1015 krb5_auth_context auth_context
,
1016 DATA_BLOB
*session_key
, bool remote
)
1018 krb5_keyblock
*skey
= NULL
;
1019 krb5_error_code err
= 0;
1023 err
= krb5_auth_con_getremotesubkey(context
,
1024 auth_context
, &skey
);
1026 err
= krb5_auth_con_getlocalsubkey(context
,
1027 auth_context
, &skey
);
1030 if (err
|| skey
== NULL
) {
1031 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
1035 DEBUG(10, ("Got KRB5 session key of length %d\n",
1036 (int)KRB5_KEY_LENGTH(skey
)));
1038 *session_key
= data_blob_talloc(mem_ctx
,
1039 KRB5_KEY_DATA(skey
),
1040 KRB5_KEY_LENGTH(skey
));
1041 dump_data_pw("KRB5 Session Key:\n",
1043 session_key
->length
);
1049 krb5_free_keyblock(context
, skey
);
1056 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
1057 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
);
1059 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
)
1061 static krb5_data kdata
;
1063 kdata
.data
= (char *)krb5_principal_get_comp_string(context
, principal
, i
);
1064 kdata
.length
= strlen((const char *)kdata
.data
);
1069 krb5_error_code
smb_krb5_kt_free_entry(krb5_context context
, krb5_keytab_entry
*kt_entry
)
1071 /* Try krb5_free_keytab_entry_contents first, since
1072 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
1073 * krb5_kt_free_entry but only has a prototype for the first, while the
1074 * second is considered private.
1076 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
1077 return krb5_free_keytab_entry_contents(context
, kt_entry
);
1078 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
1079 return krb5_kt_free_entry(context
, kt_entry
);
1081 #error UNKNOWN_KT_FREE_FUNCTION
1085 void smb_krb5_checksum_from_pac_sig(krb5_checksum
*cksum
,
1086 struct PAC_SIGNATURE_DATA
*sig
)
1088 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
1089 cksum
->cksumtype
= (krb5_cksumtype
)sig
->type
;
1090 cksum
->checksum
.length
= sig
->signature
.length
;
1091 cksum
->checksum
.data
= sig
->signature
.data
;
1093 cksum
->checksum_type
= (krb5_cksumtype
)sig
->type
;
1094 cksum
->length
= sig
->signature
.length
;
1095 cksum
->contents
= sig
->signature
.data
;
1099 krb5_error_code
smb_krb5_verify_checksum(krb5_context context
,
1100 const krb5_keyblock
*keyblock
,
1101 krb5_keyusage usage
,
1102 krb5_checksum
*cksum
,
1106 krb5_error_code ret
;
1108 /* verify the checksum */
1110 /* welcome to the wonderful world of samba's kerberos abstraction layer:
1112 * function heimdal 0.6.1rc3 heimdal 0.7 MIT krb 1.4.2
1113 * -----------------------------------------------------------------------------
1114 * krb5_c_verify_checksum - works works
1115 * krb5_verify_checksum works (6 args) works (6 args) broken (7 args)
1118 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
1120 krb5_boolean checksum_valid
= False
;
1123 input
.data
= (char *)data
;
1124 input
.length
= length
;
1126 ret
= krb5_c_verify_checksum(context
,
1133 DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n",
1134 error_message(ret
)));
1138 if (!checksum_valid
)
1139 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
1142 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
1144 /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
1145 * without enctype and it ignores any key_usage types - Guenther */
1150 ret
= krb5_crypto_init(context
,
1155 DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n",
1156 error_message(ret
)));
1160 ret
= krb5_verify_checksum(context
,
1167 krb5_crypto_destroy(context
, crypto
);
1171 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
1177 time_t get_authtime_from_tkt(krb5_ticket
*tkt
)
1179 #if defined(HAVE_KRB5_TKT_ENC_PART2)
1180 return tkt
->enc_part2
->times
.authtime
;
1182 return tkt
->ticket
.authtime
;
1186 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1187 static int get_kvno_from_ap_req(krb5_ap_req
*ap_req
)
1189 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
1190 if (ap_req
->ticket
->enc_part
.kvno
)
1191 return ap_req
->ticket
->enc_part
.kvno
;
1193 if (ap_req
->ticket
.enc_part
.kvno
)
1194 return *ap_req
->ticket
.enc_part
.kvno
;
1199 static krb5_enctype
get_enctype_from_ap_req(krb5_ap_req
*ap_req
)
1201 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
1202 return ap_req
->ticket
.enc_part
.etype
;
1204 return ap_req
->ticket
->enc_part
.enctype
;
1207 #endif /* HAVE_KRB5_DECODE_AP_REQ */
1209 static krb5_error_code
1210 get_key_from_keytab(krb5_context context
,
1211 krb5_const_principal server
,
1212 krb5_enctype enctype
,
1214 krb5_keyblock
**out_key
)
1216 krb5_keytab_entry entry
;
1217 krb5_error_code ret
;
1220 krb5_keyblock
*keyp
;
1222 /* We have to open a new keytab handle here, as MIT does
1223 an implicit open/getnext/close on krb5_kt_get_entry. We
1224 may be in the middle of a keytab enumeration when this is
1227 ret
= smb_krb5_open_keytab(context
, NULL
, False
, &keytab
);
1229 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret
)));
1233 if ( DEBUGLEVEL
>= 10 ) {
1234 if (smb_krb5_unparse_name(talloc_tos(), context
, server
, &name
) == 0) {
1235 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
1236 kvno
, enctype
, name
));
1241 ret
= krb5_kt_get_entry(context
,
1249 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret
)));
1253 keyp
= KRB5_KT_KEY(&entry
);
1255 ret
= krb5_copy_keyblock(context
, keyp
, out_key
);
1257 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret
)));
1261 smb_krb5_kt_free_entry(context
, &entry
);
1264 krb5_kt_close(context
, keytab
);
1270 krb5_error_code
smb_krb5_get_keyinfo_from_ap_req(krb5_context context
,
1271 const krb5_data
*inbuf
,
1273 krb5_enctype
*enctype
)
1275 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1277 krb5_error_code ret
;
1280 ret
= krb5_decode_ap_req(context
, inbuf
, &ap_req
);
1284 *kvno
= get_kvno_from_ap_req(&ap_req
);
1285 *enctype
= get_enctype_from_ap_req(&ap_req
);
1287 free_AP_REQ(&ap_req
);
1292 /* Possibly not an appropriate error code. */
1293 return KRB5KDC_ERR_BADOPTION
;
1296 krb5_error_code
krb5_rd_req_return_keyblock_from_keytab(krb5_context context
,
1297 krb5_auth_context
*auth_context
,
1298 const krb5_data
*inbuf
,
1299 krb5_const_principal server
,
1301 krb5_flags
*ap_req_options
,
1302 krb5_ticket
**ticket
,
1303 krb5_keyblock
**keyblock
)
1305 krb5_error_code ret
;
1307 krb5_enctype enctype
;
1308 krb5_keyblock
*local_keyblock
;
1310 ret
= krb5_rd_req(context
,
1321 #ifdef KRB5_TICKET_HAS_KEYINFO
1322 enctype
= (*ticket
)->enc_part
.enctype
;
1323 kvno
= (*ticket
)->enc_part
.kvno
;
1325 ret
= smb_krb5_get_keyinfo_from_ap_req(context
, inbuf
, &kvno
, &enctype
);
1331 ret
= get_key_from_keytab(context
,
1337 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1342 if (ret
&& local_keyblock
!= NULL
) {
1343 krb5_free_keyblock(context
, local_keyblock
);
1345 *keyblock
= local_keyblock
;
1351 krb5_error_code
smb_krb5_parse_name_norealm(krb5_context context
,
1353 krb5_principal
*principal
)
1355 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1356 return smb_krb5_parse_name_norealm_conv(context
, name
, principal
);
1359 /* we are cheating here because parse_name will in fact set the realm.
1360 * We don't care as the only caller of smb_krb5_parse_name_norealm
1361 * ignores the realm anyway when calling
1362 * smb_krb5_principal_compare_any_realm later - Guenther */
1364 return smb_krb5_parse_name(context
, name
, principal
);
1367 bool smb_krb5_principal_compare_any_realm(krb5_context context
,
1368 krb5_const_principal princ1
,
1369 krb5_const_principal princ2
)
1371 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1373 return krb5_principal_compare_any_realm(context
, princ1
, princ2
);
1375 /* krb5_princ_size is a macro in MIT */
1376 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1379 const krb5_data
*p1
, *p2
;
1381 len1
= krb5_princ_size(context
, princ1
);
1382 len2
= krb5_princ_size(context
, princ2
);
1387 for (i
= 0; i
< len1
; i
++) {
1389 p1
= krb5_princ_component(context
, CONST_DISCARD(krb5_principal
, princ1
), i
);
1390 p2
= krb5_princ_component(context
, CONST_DISCARD(krb5_principal
, princ2
), i
);
1392 if (p1
->length
!= p2
->length
|| memcmp(p1
->data
, p2
->data
, p1
->length
))
1398 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1402 krb5_error_code
smb_krb5_renew_ticket(const char *ccache_string
, /* FILE:/tmp/krb5cc_0 */
1403 const char *client_string
, /* gd@BER.SUSE.DE */
1404 const char *service_string
, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1405 time_t *expire_time
)
1407 krb5_error_code ret
;
1408 krb5_context context
= NULL
;
1409 krb5_ccache ccache
= NULL
;
1410 krb5_principal client
= NULL
;
1411 krb5_creds creds
, creds_in
, *creds_out
= NULL
;
1414 ZERO_STRUCT(creds_in
);
1416 initialize_krb5_error_table();
1417 ret
= krb5_init_context(&context
);
1422 if (!ccache_string
) {
1423 ccache_string
= krb5_cc_default_name(context
);
1426 if (!ccache_string
) {
1431 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string
));
1433 /* FIXME: we should not fall back to defaults */
1434 ret
= krb5_cc_resolve(context
, CONST_DISCARD(char *, ccache_string
), &ccache
);
1439 if (client_string
) {
1440 ret
= smb_krb5_parse_name(context
, client_string
, &client
);
1445 ret
= krb5_cc_get_principal(context
, ccache
, &client
);
1451 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1453 ret
= krb5_get_renewed_creds(context
, &creds
, client
, ccache
, CONST_DISCARD(char *, service_string
));
1455 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1459 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1461 krb5_kdc_flags flags
;
1462 krb5_realm
*client_realm
= NULL
;
1464 ret
= krb5_copy_principal(context
, client
, &creds_in
.client
);
1469 if (service_string
) {
1470 ret
= smb_krb5_parse_name(context
, service_string
, &creds_in
.server
);
1475 /* build tgt service by default */
1476 client_realm
= krb5_princ_realm(context
, creds_in
.client
);
1477 if (!client_realm
) {
1481 ret
= krb5_make_principal(context
, &creds_in
.server
, *client_realm
, KRB5_TGS_NAME
, *client_realm
, NULL
);
1488 flags
.b
.renewable
= flags
.b
.renew
= True
;
1490 ret
= krb5_get_kdc_cred(context
, ccache
, flags
, NULL
, NULL
, &creds_in
, &creds_out
);
1492 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1499 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1502 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1503 ret
= krb5_cc_initialize(context
, ccache
, client
);
1508 ret
= krb5_cc_store_cred(context
, ccache
, &creds
);
1511 *expire_time
= (time_t) creds
.times
.endtime
;
1515 krb5_free_cred_contents(context
, &creds_in
);
1518 krb5_free_creds(context
, creds_out
);
1520 krb5_free_cred_contents(context
, &creds
);
1524 krb5_free_principal(context
, client
);
1527 krb5_cc_close(context
, ccache
);
1530 krb5_free_context(context
);
1536 krb5_error_code
smb_krb5_free_addresses(krb5_context context
, smb_krb5_addresses
*addr
)
1538 krb5_error_code ret
= 0;
1542 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1543 krb5_free_addresses(context
, addr
->addrs
);
1544 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1545 ret
= krb5_free_addresses(context
, addr
->addrs
);
1546 SAFE_FREE(addr
->addrs
);
1553 krb5_error_code
smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses
**kerb_addr
)
1555 krb5_error_code ret
= 0;
1557 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1558 krb5_address
**addrs
= NULL
;
1559 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1560 krb5_addresses
*addrs
= NULL
;
1563 *kerb_addr
= (smb_krb5_addresses
*)SMB_MALLOC(sizeof(smb_krb5_addresses
));
1564 if (*kerb_addr
== NULL
) {
1568 put_name(buf
, global_myname(), ' ', 0x20);
1570 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1574 addrs
= (krb5_address
**)SMB_MALLOC(sizeof(krb5_address
*) * num_addr
);
1575 if (addrs
== NULL
) {
1576 SAFE_FREE(*kerb_addr
);
1580 memset(addrs
, 0, sizeof(krb5_address
*) * num_addr
);
1582 addrs
[0] = (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1583 if (addrs
[0] == NULL
) {
1585 SAFE_FREE(*kerb_addr
);
1589 addrs
[0]->magic
= KV5M_ADDRESS
;
1590 addrs
[0]->addrtype
= KRB5_ADDR_NETBIOS
;
1591 addrs
[0]->length
= MAX_NETBIOSNAME_LEN
;
1592 addrs
[0]->contents
= (unsigned char *)SMB_MALLOC(addrs
[0]->length
);
1593 if (addrs
[0]->contents
== NULL
) {
1594 SAFE_FREE(addrs
[0]);
1596 SAFE_FREE(*kerb_addr
);
1600 memcpy(addrs
[0]->contents
, buf
, addrs
[0]->length
);
1604 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1606 addrs
= (krb5_addresses
*)SMB_MALLOC(sizeof(krb5_addresses
));
1607 if (addrs
== NULL
) {
1608 SAFE_FREE(*kerb_addr
);
1612 memset(addrs
, 0, sizeof(krb5_addresses
));
1615 addrs
->val
= (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1616 if (addrs
->val
== NULL
) {
1618 SAFE_FREE(kerb_addr
);
1622 addrs
->val
[0].addr_type
= KRB5_ADDR_NETBIOS
;
1623 addrs
->val
[0].address
.length
= MAX_NETBIOSNAME_LEN
;
1624 addrs
->val
[0].address
.data
= (unsigned char *)SMB_MALLOC(addrs
->val
[0].address
.length
);
1625 if (addrs
->val
[0].address
.data
== NULL
) {
1626 SAFE_FREE(addrs
->val
);
1628 SAFE_FREE(*kerb_addr
);
1632 memcpy(addrs
->val
[0].address
.data
, buf
, addrs
->val
[0].address
.length
);
1635 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1637 (*kerb_addr
)->addrs
= addrs
;
1642 void smb_krb5_free_error(krb5_context context
, krb5_error
*krberror
)
1644 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1645 krb5_free_error_contents(context
, krberror
);
1647 krb5_free_error(context
, krberror
);
1651 krb5_error_code
handle_krberror_packet(krb5_context context
,
1654 krb5_error_code ret
;
1655 bool got_error_code
= False
;
1657 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1659 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1661 krb5_error krberror
;
1663 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1664 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1665 error_message(ret
)));
1669 if (krberror
.e_data
== NULL
|| krberror
.e_data
->data
== NULL
) {
1670 ret
= (krb5_error_code
) krberror
.error_code
;
1671 got_error_code
= True
;
1674 smb_krb5_free_error(context
, &krberror
);
1678 krb5_error
*krberror
;
1680 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1681 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1682 error_message(ret
)));
1686 if (krberror
->e_data
.data
== NULL
) {
1687 #if defined(ERROR_TABLE_BASE_krb5)
1688 ret
= ERROR_TABLE_BASE_krb5
+ (krb5_error_code
) krberror
->error
;
1690 ret
= (krb5_error_code
)krberror
->error
;
1692 got_error_code
= True
;
1694 smb_krb5_free_error(context
, krberror
);
1697 if (got_error_code
) {
1698 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1699 error_message(ret
), ret
));
1704 krb5_error_code
smb_krb5_get_init_creds_opt_alloc(krb5_context context
,
1705 krb5_get_init_creds_opt
**opt
)
1707 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1708 /* Heimdal or modern MIT version */
1709 return krb5_get_init_creds_opt_alloc(context
, opt
);
1711 /* Historical MIT version */
1712 krb5_get_init_creds_opt
*my_opt
;
1716 if ((my_opt
= SMB_MALLOC_P(krb5_get_init_creds_opt
)) == NULL
) {
1720 krb5_get_init_creds_opt_init(my_opt
);
1724 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1727 void smb_krb5_get_init_creds_opt_free(krb5_context context
,
1728 krb5_get_init_creds_opt
*opt
)
1730 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1732 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1733 /* Modern MIT or Heimdal version */
1734 krb5_get_init_creds_opt_free(context
, opt
);
1736 /* Heimdal version */
1737 krb5_get_init_creds_opt_free(opt
);
1738 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1740 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1741 /* Historical MIT version */
1744 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1747 krb5_enctype
smb_get_enctype_from_kt_entry(krb5_keytab_entry
*kt_entry
)
1749 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry
));
1753 /* caller needs to free etype_s */
1754 krb5_error_code
smb_krb5_enctype_to_string(krb5_context context
,
1755 krb5_enctype enctype
,
1758 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1759 return krb5_enctype_to_string(context
, enctype
, etype_s
); /* Heimdal */
1760 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1762 krb5_error_code ret
= krb5_enctype_to_string(enctype
, buf
, 256); /* MIT */
1766 *etype_s
= SMB_STRDUP(buf
);
1772 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1776 krb5_error_code
smb_krb5_mk_error(krb5_context context
,
1777 krb5_error_code error_code
,
1778 const krb5_principal server
,
1781 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1783 * The MIT interface is *terrible*.
1784 * We have to construct this ourselves...
1788 memset(&e
, 0, sizeof(e
));
1789 krb5_us_timeofday(context
, &e
.stime
, &e
.susec
);
1791 #if defined(krb5_err_base)
1792 e
.error
= error_code
- krb5_err_base
;
1793 #elif defined(ERROR_TABLE_BASE_krb5)
1794 e
.error
= error_code
- ERROR_TABLE_BASE_krb5
;
1796 e
.error
= error_code
; /* Almost certainly wrong, but what can we do... ? */
1799 return krb5_mk_error(context
, &e
, reply
);
1800 #else /* Heimdal. */
1801 return krb5_mk_error(context
,
1813 /**********************************************************************
1814 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1815 * allows to process non-default keytab names.
1816 * @param context krb5_context
1817 * @param keytab_name_req string
1818 * @param write_access bool if writable keytab is required
1819 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1820 * @return krb5_error_code
1821 **********************************************************************/
1823 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1824 #ifndef MAX_KEYTAB_NAME_LEN
1825 #define MAX_KEYTAB_NAME_LEN 1100
1828 krb5_error_code
smb_krb5_open_keytab(krb5_context context
,
1829 const char *keytab_name_req
,
1831 krb5_keytab
*keytab
)
1833 krb5_error_code ret
= 0;
1834 TALLOC_CTX
*mem_ctx
;
1835 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1836 char *kt_str
= NULL
;
1837 bool found_valid_name
= False
;
1838 const char *pragma
= "FILE";
1839 const char *tmp
= NULL
;
1841 if (!write_access
&& !keytab_name_req
) {
1842 /* caller just wants to read the default keytab readonly, so be it */
1843 return krb5_kt_default(context
, keytab
);
1846 mem_ctx
= talloc_init("smb_krb5_open_keytab");
1851 #ifdef HAVE_WRFILE_KEYTAB
1857 if (keytab_name_req
) {
1859 if (strlen(keytab_name_req
) > MAX_KEYTAB_NAME_LEN
) {
1860 ret
= KRB5_CONFIG_NOTENUFSPACE
;
1864 if ((strncmp(keytab_name_req
, "WRFILE:/", 8) == 0) ||
1865 (strncmp(keytab_name_req
, "FILE:/", 6) == 0)) {
1866 tmp
= keytab_name_req
;
1870 if (keytab_name_req
[0] != '/') {
1871 ret
= KRB5_KT_BADNAME
;
1875 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, keytab_name_req
);
1884 /* we need to handle more complex keytab_strings, like:
1885 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1887 ret
= krb5_kt_default_name(context
, &keytab_string
[0], MAX_KEYTAB_NAME_LEN
- 2);
1892 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string
));
1894 tmp
= talloc_strdup(mem_ctx
, keytab_string
);
1900 if (strncmp(tmp
, "ANY:", 4) == 0) {
1904 memset(&keytab_string
, '\0', sizeof(keytab_string
));
1906 while (next_token_talloc(mem_ctx
, &tmp
, &kt_str
, ",")) {
1907 if (strncmp(kt_str
, "WRFILE:", 7) == 0) {
1908 found_valid_name
= True
;
1913 if (strncmp(kt_str
, "FILE:", 5) == 0) {
1914 found_valid_name
= True
;
1919 if (tmp
[0] == '/') {
1920 /* Treat as a FILE: keytab definition. */
1921 found_valid_name
= true;
1924 if (found_valid_name
) {
1925 if (tmp
[0] != '/') {
1926 ret
= KRB5_KT_BADNAME
;
1930 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, tmp
);
1939 if (!found_valid_name
) {
1940 ret
= KRB5_KT_UNKNOWN_TYPE
;
1945 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp
));
1946 ret
= krb5_kt_resolve(context
, tmp
, keytab
);
1949 TALLOC_FREE(mem_ctx
);
1953 krb5_error_code
smb_krb5_keytab_name(TALLOC_CTX
*mem_ctx
,
1954 krb5_context context
,
1956 const char **keytab_name
)
1958 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1959 krb5_error_code ret
= 0;
1961 ret
= krb5_kt_get_name(context
, keytab
,
1962 keytab_string
, MAX_KEYTAB_NAME_LEN
- 2);
1967 *keytab_name
= talloc_strdup(mem_ctx
, keytab_string
);
1968 if (!*keytab_name
) {
1975 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1976 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1977 defined(HAVE_KRB5_GET_CREDS)
1978 static krb5_error_code
smb_krb5_get_credentials_for_user_opt(krb5_context context
,
1981 krb5_principal server
,
1982 krb5_principal impersonate_princ
,
1983 krb5_creds
**out_creds
)
1985 krb5_error_code ret
;
1986 krb5_get_creds_opt opt
;
1988 ret
= krb5_get_creds_opt_alloc(context
, &opt
);
1992 krb5_get_creds_opt_add_options(context
, opt
, KRB5_GC_FORWARDABLE
);
1994 if (impersonate_princ
) {
1995 ret
= krb5_get_creds_opt_set_impersonate(context
, opt
,
2002 ret
= krb5_get_creds(context
, opt
, ccache
, server
, out_creds
);
2009 krb5_get_creds_opt_free(context
, opt
);
2013 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
2015 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
2016 static krb5_error_code
smb_krb5_get_credentials_for_user(krb5_context context
,
2019 krb5_principal server
,
2020 krb5_principal impersonate_princ
,
2021 krb5_creds
**out_creds
)
2023 krb5_error_code ret
;
2024 krb5_creds in_creds
;
2026 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
2027 krb5_error_code KRB5_CALLCONV
2028 krb5_get_credentials_for_user(krb5_context context
, krb5_flags options
,
2029 krb5_ccache ccache
, krb5_creds
*in_creds
,
2030 krb5_data
*subject_cert
,
2031 krb5_creds
**out_creds
);
2032 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
2034 ZERO_STRUCT(in_creds
);
2036 if (impersonate_princ
) {
2038 in_creds
.server
= me
;
2039 in_creds
.client
= impersonate_princ
;
2041 ret
= krb5_get_credentials_for_user(context
,
2042 0, /* krb5_flags options */
2045 NULL
, /* krb5_data *subject_cert */
2048 in_creds
.client
= me
;
2049 in_creds
.server
= server
;
2051 ret
= krb5_get_credentials(context
, 0, ccache
,
2052 &in_creds
, out_creds
);
2057 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
2060 * smb_krb5_get_credentials
2062 * @brief Get krb5 credentials for a server
2064 * @param[in] context An initialized krb5_context
2065 * @param[in] ccache An initialized krb5_ccache
2066 * @param[in] me The krb5_principal of the caller
2067 * @param[in] server The krb5_principal of the requested service
2068 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
2069 * @param[out] out_creds The returned krb5_creds structure
2070 * @return krb5_error_code
2073 krb5_error_code
smb_krb5_get_credentials(krb5_context context
,
2076 krb5_principal server
,
2077 krb5_principal impersonate_princ
,
2078 krb5_creds
**out_creds
)
2080 krb5_error_code ret
;
2081 krb5_creds
*creds
= NULL
;
2085 if (impersonate_princ
) {
2086 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
2087 ret
= smb_krb5_get_credentials_for_user_opt(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
2088 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
2089 ret
= smb_krb5_get_credentials_for_user(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
2094 krb5_creds in_creds
;
2096 ZERO_STRUCT(in_creds
);
2098 in_creds
.client
= me
;
2099 in_creds
.server
= server
;
2101 ret
= krb5_get_credentials(context
, 0, ccache
,
2108 ret
= krb5_cc_store_cred(context
, ccache
, creds
);
2119 krb5_free_creds(context
, creds
);
2126 * smb_krb5_get_creds
2128 * @brief Get krb5 credentials for a server
2130 * @param[in] server_s The string name of the service
2131 * @param[in] time_offset The offset to the KDCs time in seconds (optional)
2132 * @param[in] cc The krb5 credential cache string name (optional)
2133 * @param[in] impersonate_princ_s The string principal name to impersonate (optional)
2134 * @param[out] creds_p The returned krb5_creds structure
2135 * @return krb5_error_code
2138 krb5_error_code
smb_krb5_get_creds(const char *server_s
,
2141 const char *impersonate_princ_s
,
2142 krb5_creds
**creds_p
)
2144 krb5_error_code ret
;
2145 krb5_context context
= NULL
;
2146 krb5_principal me
= NULL
;
2147 krb5_principal server
= NULL
;
2148 krb5_principal impersonate_princ
= NULL
;
2149 krb5_creds
*creds
= NULL
;
2150 krb5_ccache ccache
= NULL
;
2154 initialize_krb5_error_table();
2155 ret
= krb5_init_context(&context
);
2160 if (time_offset
!= 0) {
2161 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
2164 ret
= krb5_cc_resolve(context
, cc
? cc
:
2165 krb5_cc_default_name(context
), &ccache
);
2170 ret
= krb5_cc_get_principal(context
, ccache
, &me
);
2175 ret
= smb_krb5_parse_name(context
, server_s
, &server
);
2180 if (impersonate_princ_s
) {
2181 ret
= smb_krb5_parse_name(context
, impersonate_princ_s
,
2182 &impersonate_princ
);
2188 ret
= smb_krb5_get_credentials(context
, ccache
,
2189 me
, server
, impersonate_princ
,
2195 ret
= krb5_cc_store_cred(context
, ccache
, creds
);
2204 DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n",
2207 if (impersonate_princ_s
) {
2208 char *client
= NULL
;
2210 ret
= smb_krb5_unparse_name(talloc_tos(), context
, creds
->client
, &client
);
2214 DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n",
2216 TALLOC_FREE(client
);
2225 krb5_free_creds(context
, creds
);
2228 krb5_free_principal(context
, server
);
2231 krb5_free_principal(context
, me
);
2233 if (impersonate_princ
) {
2234 krb5_free_principal(context
, impersonate_princ
);
2237 krb5_cc_close(context
, ccache
);
2239 krb5_free_context(context
);
2245 * smb_krb5_principal_get_realm
2247 * @brief Get realm of a principal
2249 * @param[in] context The krb5_context
2250 * @param[in] principal The principal
2251 * @return pointer to the realm
2255 char *smb_krb5_principal_get_realm(krb5_context context
,
2256 krb5_principal principal
)
2258 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2259 return krb5_principal_get_realm(context
, principal
);
2260 #elif defined(krb5_princ_realm) /* MIT */
2262 realm
= krb5_princ_realm(context
, principal
);
2263 return (char *)realm
->data
;
2269 #else /* HAVE_KRB5 */
2270 /* this saves a few linking headaches */
2271 int cli_krb5_get_ticket(TALLOC_CTX
*mem_ctx
,
2272 const char *principal
, time_t time_offset
,
2273 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
2274 uint32_t extra_ap_opts
,
2275 const char *ccname
, time_t *tgs_expire
,
2276 const char *impersonate_princ_s
)
2278 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2282 bool unwrap_pac(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, DATA_BLOB
*unwrapped_pac_data
)
2284 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2288 #endif /* HAVE_KRB5 */