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/>.
29 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
30 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
31 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
32 bind field, flags field. */
34 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
35 but still has the symbol */
36 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
37 krb5_error_code
krb5_auth_con_set_req_cksumtype(
39 krb5_auth_context auth_context
,
40 krb5_cksumtype cksumtype
);
43 /**************************************************************
44 Wrappers around kerberos string functions that convert from
45 utf8 -> unix charset and vica versa.
46 **************************************************************/
48 /**************************************************************
49 krb5_parse_name that takes a UNIX charset.
50 **************************************************************/
52 krb5_error_code
smb_krb5_parse_name(krb5_context context
,
53 const char *name
, /* in unix charset */
54 krb5_principal
*principal
)
58 size_t converted_size
;
60 if (!push_utf8_talloc(talloc_tos(), &utf8_name
, name
, &converted_size
)) {
64 ret
= krb5_parse_name(context
, utf8_name
, principal
);
65 TALLOC_FREE(utf8_name
);
69 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
70 /**************************************************************
71 krb5_parse_name_norealm that takes a UNIX charset.
72 **************************************************************/
74 static krb5_error_code
smb_krb5_parse_name_norealm_conv(krb5_context context
,
75 const char *name
, /* in unix charset */
76 krb5_principal
*principal
)
80 size_t converted_size
;
83 if (!push_utf8_talloc(talloc_tos(), &utf8_name
, name
, &converted_size
)) {
87 ret
= krb5_parse_name_norealm(context
, utf8_name
, principal
);
88 TALLOC_FREE(utf8_name
);
93 /**************************************************************
94 krb5_parse_name that returns a UNIX charset name. Must
95 be freed with talloc_free() call.
96 **************************************************************/
98 krb5_error_code
smb_krb5_unparse_name(TALLOC_CTX
*mem_ctx
,
100 krb5_const_principal principal
,
105 size_t converted_size
;
108 ret
= krb5_unparse_name(context
, principal
, &utf8_name
);
113 if (!pull_utf8_talloc(mem_ctx
, unix_name
, utf8_name
, &converted_size
)) {
114 krb5_free_unparsed_name(context
, utf8_name
);
117 krb5_free_unparsed_name(context
, utf8_name
);
121 #ifndef HAVE_KRB5_SET_REAL_TIME
123 * This function is not in the Heimdal mainline.
125 krb5_error_code
krb5_set_real_time(krb5_context context
, int32_t seconds
, int32_t microseconds
)
130 ret
= krb5_us_timeofday(context
, &sec
, &usec
);
134 context
->kdc_sec_offset
= seconds
- sec
;
135 context
->kdc_usec_offset
= microseconds
- usec
;
141 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
143 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
145 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
146 * to krb5_set_default_tgs_ktypes. See
147 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
149 * If the MIT libraries are not exporting internal symbols, we will end up in
150 * this branch, which is correct. Otherwise we will continue to use the
153 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
155 return krb5_set_default_tgs_enctypes(ctx
, enc
);
158 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
161 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
163 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
166 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
168 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
170 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
172 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
174 memset(pkaddr
, '\0', sizeof(krb5_address
));
175 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
176 if (paddr
->ss_family
== AF_INET6
) {
177 pkaddr
->addr_type
= KRB5_ADDRESS_INET6
;
178 pkaddr
->address
.length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
179 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
183 if (paddr
->ss_family
== AF_INET
) {
184 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
185 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
186 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
191 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
193 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
195 memset(pkaddr
, '\0', sizeof(krb5_address
));
196 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
197 if (paddr
->ss_family
== AF_INET6
) {
198 pkaddr
->addrtype
= ADDRTYPE_INET6
;
199 pkaddr
->length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
200 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
204 if (paddr
->ss_family
== AF_INET
) {
205 pkaddr
->addrtype
= ADDRTYPE_INET
;
206 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
207 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
213 #error UNKNOWN_ADDRTYPE
216 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
217 static int create_kerberos_key_from_string_direct(krb5_context context
,
218 krb5_principal host_princ
,
221 krb5_enctype enctype
)
225 krb5_encrypt_block eblock
;
227 ret
= krb5_principal2salt(context
, host_princ
, &salt
);
229 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret
)));
232 krb5_use_enctype(context
, &eblock
, enctype
);
233 ret
= krb5_string_to_key(context
, &eblock
, key
, password
, &salt
);
234 SAFE_FREE(salt
.data
);
238 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
239 static int create_kerberos_key_from_string_direct(krb5_context context
,
240 krb5_principal host_princ
,
243 krb5_enctype enctype
)
248 ret
= krb5_get_pw_salt(context
, host_princ
, &salt
);
250 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret
)));
254 ret
= krb5_string_to_key_salt(context
, enctype
, (const char *)password
->data
, salt
, key
);
255 krb5_free_salt(context
, salt
);
260 #error UNKNOWN_CREATE_KEY_FUNCTIONS
263 int create_kerberos_key_from_string(krb5_context context
,
264 krb5_principal host_princ
,
267 krb5_enctype enctype
,
270 krb5_principal salt_princ
= NULL
;
273 * Check if we've determined that the KDC is salting keys for this
274 * principal/enctype in a non-obvious way. If it is, try to match
278 KRB5_KEY_DATA(key
) = (KRB5_KEY_DATA_CAST
*)SMB_MALLOC(password
->length
);
279 if (!KRB5_KEY_DATA(key
)) {
282 memcpy(KRB5_KEY_DATA(key
), password
->data
, password
->length
);
283 KRB5_KEY_LENGTH(key
) = password
->length
;
284 KRB5_KEY_TYPE(key
) = enctype
;
287 salt_princ
= kerberos_fetch_salt_princ_for_host_princ(context
, host_princ
, enctype
);
288 ret
= create_kerberos_key_from_string_direct(context
, salt_princ
? salt_princ
: host_princ
, password
, key
, enctype
);
290 krb5_free_principal(context
, salt_princ
);
295 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
296 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
297 krb5_enctype
**enctypes
)
299 return krb5_get_permitted_enctypes(context
, enctypes
);
301 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
302 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
303 krb5_enctype
**enctypes
)
305 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
308 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
311 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
312 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
313 krb5_auth_context auth_context
,
314 krb5_keyblock
*keyblock
)
316 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
320 bool unwrap_edata_ntstatus(TALLOC_CTX
*mem_ctx
,
322 DATA_BLOB
*edata_out
)
324 DATA_BLOB edata_contents
;
328 if (!edata
->length
) {
332 data
= asn1_init(mem_ctx
);
337 asn1_load(data
, *edata
);
338 asn1_start_tag(data
, ASN1_SEQUENCE(0));
339 asn1_start_tag(data
, ASN1_CONTEXT(1));
340 asn1_read_Integer(data
, &edata_type
);
342 if (edata_type
!= KRB5_PADATA_PW_SALT
) {
343 DEBUG(0,("edata is not of required type %d but of type %d\n",
344 KRB5_PADATA_PW_SALT
, edata_type
));
349 asn1_start_tag(data
, ASN1_CONTEXT(2));
350 asn1_read_OctetString(data
, talloc_autofree_context(), &edata_contents
);
356 *edata_out
= data_blob_talloc(mem_ctx
, edata_contents
.data
, edata_contents
.length
);
358 data_blob_free(&edata_contents
);
364 bool unwrap_pac(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, DATA_BLOB
*unwrapped_pac_data
)
366 DATA_BLOB pac_contents
;
370 if (!auth_data
->length
) {
374 data
= asn1_init(mem_ctx
);
379 asn1_load(data
, *auth_data
);
380 asn1_start_tag(data
, ASN1_SEQUENCE(0));
381 asn1_start_tag(data
, ASN1_SEQUENCE(0));
382 asn1_start_tag(data
, ASN1_CONTEXT(0));
383 asn1_read_Integer(data
, &data_type
);
385 if (data_type
!= KRB5_AUTHDATA_WIN2K_PAC
) {
386 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type
));
392 asn1_start_tag(data
, ASN1_CONTEXT(1));
393 asn1_read_OctetString(data
, talloc_autofree_context(), &pac_contents
);
399 *unwrapped_pac_data
= data_blob_talloc(mem_ctx
, pac_contents
.data
, pac_contents
.length
);
401 data_blob_free(&pac_contents
);
406 bool get_auth_data_from_tkt(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, krb5_ticket
*tkt
)
408 DATA_BLOB auth_data_wrapped
;
409 bool got_auth_data_pac
= False
;
412 #if defined(HAVE_KRB5_TKT_ENC_PART2)
413 if (tkt
->enc_part2
&& tkt
->enc_part2
->authorization_data
&&
414 tkt
->enc_part2
->authorization_data
[0] &&
415 tkt
->enc_part2
->authorization_data
[0]->length
)
417 for (i
= 0; tkt
->enc_part2
->authorization_data
[i
] != NULL
; i
++) {
419 if (tkt
->enc_part2
->authorization_data
[i
]->ad_type
!=
420 KRB5_AUTHDATA_IF_RELEVANT
) {
421 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
422 tkt
->enc_part2
->authorization_data
[i
]->ad_type
));
426 auth_data_wrapped
= data_blob(tkt
->enc_part2
->authorization_data
[i
]->contents
,
427 tkt
->enc_part2
->authorization_data
[i
]->length
);
429 /* check if it is a PAC */
430 got_auth_data_pac
= unwrap_pac(mem_ctx
, &auth_data_wrapped
, auth_data
);
431 data_blob_free(&auth_data_wrapped
);
433 if (got_auth_data_pac
) {
438 return got_auth_data_pac
;
442 if (tkt
->ticket
.authorization_data
&&
443 tkt
->ticket
.authorization_data
->len
)
445 for (i
= 0; i
< tkt
->ticket
.authorization_data
->len
; i
++) {
447 if (tkt
->ticket
.authorization_data
->val
[i
].ad_type
!=
448 KRB5_AUTHDATA_IF_RELEVANT
) {
449 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
450 tkt
->ticket
.authorization_data
->val
[i
].ad_type
));
454 auth_data_wrapped
= data_blob(tkt
->ticket
.authorization_data
->val
[i
].ad_data
.data
,
455 tkt
->ticket
.authorization_data
->val
[i
].ad_data
.length
);
457 /* check if it is a PAC */
458 got_auth_data_pac
= unwrap_pac(mem_ctx
, &auth_data_wrapped
, auth_data
);
459 data_blob_free(&auth_data_wrapped
);
461 if (got_auth_data_pac
) {
466 return got_auth_data_pac
;
472 krb5_const_principal
get_principal_from_tkt(krb5_ticket
*tkt
)
474 #if defined(HAVE_KRB5_TKT_ENC_PART2)
475 return tkt
->enc_part2
->client
;
481 #if !defined(HAVE_KRB5_LOCATE_KDC)
483 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
484 * to a public interface for this functionality, so we have to be able to live
485 * without it if the MIT libraries are hiding their internal symbols.
488 #if defined(KRB5_KRBHST_INIT)
490 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
, struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
492 krb5_krbhst_handle hnd
;
493 krb5_krbhst_info
*hinfo
;
502 rc
= krb5_krbhst_init(ctx
, realm
->data
, KRB5_KRBHST_KDC
, &hnd
);
504 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc
)));
508 for ( num_kdcs
= 0; (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); num_kdcs
++)
511 krb5_krbhst_reset(ctx
, hnd
);
514 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
515 krb5_krbhst_free(ctx
, hnd
);
519 sa
= SMB_MALLOC_ARRAY( struct sockaddr
, num_kdcs
);
521 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
522 krb5_krbhst_free(ctx
, hnd
);
527 memset(sa
, '\0', sizeof(struct sockaddr
) * num_kdcs
);
529 for (i
= 0; i
< num_kdcs
&& (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); i
++) {
531 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
532 rc
= krb5_krbhst_get_addrinfo(ctx
, hinfo
, &ai
);
534 DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc
)));
538 if (hinfo
->ai
&& hinfo
->ai
->ai_family
== AF_INET
)
539 memcpy(&sa
[i
], hinfo
->ai
->ai_addr
, sizeof(struct sockaddr
));
542 krb5_krbhst_free(ctx
, hnd
);
549 #else /* ! defined(KRB5_KRBHST_INIT) */
551 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
,
552 struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
554 DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
555 return KRB5_KDC_UNREACH
;
558 #endif /* KRB5_KRBHST_INIT */
560 #else /* ! HAVE_KRB5_LOCATE_KDC */
562 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
,
563 struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
565 return krb5_locate_kdc(ctx
, realm
, addr_pp
, naddrs
, get_masters
);
568 #endif /* HAVE_KRB5_LOCATE_KDC */
570 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
571 void krb5_free_unparsed_name(krb5_context context
, char *val
)
577 void kerberos_free_data_contents(krb5_context context
, krb5_data
*pdata
)
579 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
581 krb5_free_data_contents(context
, pdata
);
584 SAFE_FREE(pdata
->data
);
588 void kerberos_set_creds_enctype(krb5_creds
*pcreds
, int enctype
)
590 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
591 KRB5_KEY_TYPE((&pcreds
->keyblock
)) = enctype
;
592 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
593 KRB5_KEY_TYPE((&pcreds
->session
)) = enctype
;
595 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
599 bool kerberos_compatible_enctypes(krb5_context context
,
600 krb5_enctype enctype1
,
601 krb5_enctype enctype2
)
603 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
604 krb5_boolean similar
= 0;
606 krb5_c_enctype_compare(context
, enctype1
, enctype2
, &similar
);
607 return similar
? True
: False
;
608 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
609 return krb5_enctypes_compatible_keys(context
, enctype1
, enctype2
) ? True
: False
;
613 static bool ads_cleanup_expired_creds(krb5_context context
,
617 krb5_error_code retval
;
618 const char *cc_type
= krb5_cc_get_type(context
, ccache
);
620 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
621 cc_type
, krb5_cc_get_name(context
, ccache
),
622 http_timestring(talloc_tos(), credsp
->times
.endtime
)));
624 /* we will probably need new tickets if the current ones
625 will expire within 10 seconds.
627 if (credsp
->times
.endtime
>= (time(NULL
) + 10))
630 /* heimdal won't remove creds from a file ccache, and
631 perhaps we shouldn't anyway, since internally we
632 use memory ccaches, and a FILE one probably means that
633 we're using creds obtained outside of our exectuable
635 if (strequal(cc_type
, "FILE")) {
636 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type
));
640 retval
= krb5_cc_remove_cred(context
, ccache
, 0, credsp
);
642 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
643 error_message(retval
)));
644 /* If we have an error in this, we want to display it,
645 but continue as though we deleted it */
650 /* Allocate and setup the auth context into the state we need. */
652 static krb5_error_code
setup_auth_context(krb5_context context
,
653 krb5_auth_context
*auth_context
)
655 krb5_error_code retval
;
657 retval
= krb5_auth_con_init(context
, auth_context
);
659 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
660 error_message(retval
)));
664 /* Ensure this is an addressless ticket. */
665 retval
= krb5_auth_con_setaddrs(context
, *auth_context
, NULL
, NULL
);
667 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
668 error_message(retval
)));
674 static krb5_error_code
create_gss_checksum(krb5_data
*in_data
, /* [inout] */
677 unsigned int orig_length
= in_data
->length
;
678 unsigned int base_cksum_size
= GSSAPI_CHECKSUM_SIZE
;
679 char *gss_cksum
= NULL
;
682 /* Extra length field for delgated ticket. */
683 base_cksum_size
+= 4;
686 if ((unsigned int)base_cksum_size
+ orig_length
<
687 (unsigned int)base_cksum_size
) {
691 gss_cksum
= (char *)SMB_MALLOC(base_cksum_size
+ orig_length
);
692 if (gss_cksum
== NULL
) {
696 memset(gss_cksum
, '\0', base_cksum_size
+ orig_length
);
697 SIVAL(gss_cksum
, 0, GSSAPI_BNDLENGTH
);
699 /* Precalculated MD5sum of NULL channel bindings (20 bytes) */
700 /* Channel bindings are: (all ints encoded as little endian)
702 [4 bytes] initiator_addrtype (255 for null bindings)
703 [4 bytes] initiator_address length
704 [n bytes] .. initiator_address data - not present
706 [4 bytes] acceptor_addrtype (255 for null bindings)
707 [4 bytes] acceptor_address length
708 [n bytes] .. acceptor_address data - not present
710 [4 bytes] application_data length
711 [n bytes] .. application_ data - not present
713 MD5 of this is ""\x14\x8f\x0c\xf7\xb1u\xdey*J\x9a%\xdfV\xc5\x18"
716 memcpy(&gss_cksum
[4],
717 "\x14\x8f\x0c\xf7\xb1u\xdey*J\x9a%\xdfV\xc5\x18",
720 SIVAL(gss_cksum
, 20, gss_flags
);
723 SSVAL(gss_cksum
, 24, 1); /* The Delegation Option identifier */
724 SSVAL(gss_cksum
, 26, orig_length
);
725 /* Copy the kerberos KRB_CRED data */
726 memcpy(gss_cksum
+ 28, in_data
->data
, orig_length
);
728 in_data
->data
= NULL
;
731 in_data
->data
= gss_cksum
;
732 in_data
->length
= base_cksum_size
+ orig_length
;
737 we can't use krb5_mk_req because w2k wants the service to be in a particular format
739 static krb5_error_code
ads_krb5_mk_req(krb5_context context
,
740 krb5_auth_context
*auth_context
,
741 const krb5_flags ap_req_options
,
742 const char *principal
,
746 const char *impersonate_princ_s
)
748 krb5_error_code retval
;
749 krb5_principal server
;
750 krb5_principal impersonate_princ
= NULL
;
754 bool creds_ready
= False
;
755 int i
= 0, maxtries
= 3;
756 uint32_t gss_flags
= 0;
758 ZERO_STRUCT(in_data
);
760 retval
= smb_krb5_parse_name(context
, principal
, &server
);
762 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal
));
766 if (impersonate_princ_s
) {
767 retval
= smb_krb5_parse_name(context
, impersonate_princ_s
,
770 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s
));
775 /* obtain ticket & session key */
777 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
778 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
779 error_message(retval
)));
783 if ((retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
))) {
784 /* This can commonly fail on smbd startup with no ticket in the cache.
785 * Report at higher level than 1. */
786 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
787 error_message(retval
)));
791 while (!creds_ready
&& (i
< maxtries
)) {
793 if ((retval
= smb_krb5_get_credentials(context
, ccache
,
798 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
799 principal
, error_message(retval
)));
803 /* cope with ticket being in the future due to clock skew */
804 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
805 time_t t
= time(NULL
);
806 int time_offset
=(int)((unsigned)credsp
->times
.starttime
-t
);
807 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
808 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
811 if (!ads_cleanup_expired_creds(context
, ccache
, credsp
)) {
818 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
819 principal
, krb5_cc_get_type(context
, ccache
), krb5_cc_get_name(context
, ccache
),
820 http_timestring(talloc_tos(), (unsigned)credsp
->times
.endtime
),
821 (unsigned)credsp
->times
.endtime
));
824 *expire_time
= (time_t)credsp
->times
.endtime
;
827 /* Allocate the auth_context. */
828 retval
= setup_auth_context(context
, auth_context
);
830 DEBUG(1,("setup_auth_context failed (%s)\n",
831 error_message(retval
)));
835 #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)
836 if( credsp
->ticket_flags
& TKT_FLG_OK_AS_DELEGATE
) {
837 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
838 as part of the kerberos exchange. */
840 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
842 retval
= krb5_auth_con_setuseruserkey(context
,
846 DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
847 error_message(retval
)));
851 /* Must use a subkey for forwarded tickets. */
852 retval
= krb5_auth_con_setflags(context
,
854 KRB5_AUTH_CONTEXT_USE_SUBKEY
);
856 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
857 error_message(retval
)));
861 retval
= krb5_fwd_tgt_creds(context
,/* Krb5 context [in] */
862 *auth_context
, /* Authentication context [in] */
863 CONST_DISCARD(char *, KRB5_TGS_NAME
), /* Ticket service name ("krbtgt") [in] */
864 credsp
->client
, /* Client principal for the tgt [in] */
865 credsp
->server
, /* Server principal for the tgt [in] */
866 ccache
, /* Credential cache to use for storage [in] */
867 1, /* Turn on for "Forwardable ticket" [in] */
868 &in_data
); /* Resulting response [out] */
871 DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n",
872 error_message( retval
) ) );
875 * This is not fatal. Delete the *auth_context and continue
876 * with krb5_mk_req_extended to get a non-forwardable ticket.
880 free( in_data
.data
);
884 krb5_auth_con_free(context
, *auth_context
);
885 *auth_context
= NULL
;
886 retval
= setup_auth_context(context
, auth_context
);
888 DEBUG(1,("setup_auth_context failed (%s)\n",
889 error_message(retval
)));
893 /* We got a delegated ticket. */
894 gss_flags
|= GSS_C_DELEG_FLAG
;
898 /* Frees and reallocates in_data into a GSS checksum blob. */
899 retval
= create_gss_checksum(&in_data
, gss_flags
);
904 /* We always want GSS-checksum types. */
905 retval
= krb5_auth_con_set_req_cksumtype(context
, *auth_context
, GSSAPI_CHECKSUM
);
907 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
908 error_message(retval
)));
913 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
914 &in_data
, credsp
, outbuf
);
916 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
917 error_message(retval
)));
922 free( in_data
.data
);
926 krb5_free_creds(context
, credsp
);
929 krb5_free_cred_contents(context
, &creds
);
932 krb5_free_principal(context
, server
);
933 if (impersonate_princ
) {
934 krb5_free_principal(context
, impersonate_princ
);
941 get a kerberos5 ticket for the given service
943 int cli_krb5_get_ticket(const char *principal
, time_t time_offset
,
944 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
945 uint32 extra_ap_opts
, const char *ccname
,
947 const char *impersonate_princ_s
)
950 krb5_error_code retval
;
952 krb5_context context
= NULL
;
953 krb5_ccache ccdef
= NULL
;
954 krb5_auth_context auth_context
= NULL
;
955 krb5_enctype enc_types
[] = {
956 #ifdef ENCTYPE_ARCFOUR_HMAC
957 ENCTYPE_ARCFOUR_HMAC
,
963 initialize_krb5_error_table();
964 retval
= krb5_init_context(&context
);
966 DEBUG(1,("cli_krb5_get_ticket: krb5_init_context failed (%s)\n",
967 error_message(retval
)));
971 if (time_offset
!= 0) {
972 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
975 if ((retval
= krb5_cc_resolve(context
, ccname
?
976 ccname
: krb5_cc_default_name(context
), &ccdef
))) {
977 DEBUG(1,("cli_krb5_get_ticket: krb5_cc_default failed (%s)\n",
978 error_message(retval
)));
982 if ((retval
= krb5_set_default_tgs_ktypes(context
, enc_types
))) {
983 DEBUG(1,("cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s)\n",
984 error_message(retval
)));
988 if ((retval
= ads_krb5_mk_req(context
,
990 AP_OPTS_USE_SUBKEY
| (krb5_flags
)extra_ap_opts
,
994 impersonate_princ_s
))) {
998 get_krb5_smb_session_key(context
, auth_context
, session_key_krb5
, False
);
1000 *ticket
= data_blob(packet
.data
, packet
.length
);
1002 kerberos_free_data_contents(context
, &packet
);
1008 krb5_cc_close(context
, ccdef
);
1010 krb5_auth_con_free(context
, auth_context
);
1011 krb5_free_context(context
);
1017 bool get_krb5_smb_session_key(krb5_context context
, krb5_auth_context auth_context
, DATA_BLOB
*session_key
, bool remote
)
1019 krb5_keyblock
*skey
= NULL
;
1020 krb5_error_code err
= 0;
1024 err
= krb5_auth_con_getremotesubkey(context
, auth_context
, &skey
);
1026 err
= krb5_auth_con_getlocalsubkey(context
, auth_context
, &skey
);
1029 if (err
|| skey
== NULL
) {
1030 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
1034 DEBUG(10, ("Got KRB5 session key of length %d\n", (int)KRB5_KEY_LENGTH(skey
)));
1035 *session_key
= data_blob(KRB5_KEY_DATA(skey
), KRB5_KEY_LENGTH(skey
));
1036 dump_data_pw("KRB5 Session Key:\n", session_key
->data
, session_key
->length
);
1042 krb5_free_keyblock(context
, skey
);
1049 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
1050 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
);
1052 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
)
1054 static krb5_data kdata
;
1056 kdata
.data
= (char *)krb5_principal_get_comp_string(context
, principal
, i
);
1057 kdata
.length
= strlen((const char *)kdata
.data
);
1062 krb5_error_code
smb_krb5_kt_free_entry(krb5_context context
, krb5_keytab_entry
*kt_entry
)
1064 /* Try krb5_free_keytab_entry_contents first, since
1065 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
1066 * krb5_kt_free_entry but only has a prototype for the first, while the
1067 * second is considered private.
1069 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
1070 return krb5_free_keytab_entry_contents(context
, kt_entry
);
1071 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
1072 return krb5_kt_free_entry(context
, kt_entry
);
1074 #error UNKNOWN_KT_FREE_FUNCTION
1078 void smb_krb5_checksum_from_pac_sig(krb5_checksum
*cksum
,
1079 struct PAC_SIGNATURE_DATA
*sig
)
1081 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
1082 cksum
->cksumtype
= (krb5_cksumtype
)sig
->type
;
1083 cksum
->checksum
.length
= sig
->signature
.length
;
1084 cksum
->checksum
.data
= sig
->signature
.data
;
1086 cksum
->checksum_type
= (krb5_cksumtype
)sig
->type
;
1087 cksum
->length
= sig
->signature
.length
;
1088 cksum
->contents
= sig
->signature
.data
;
1092 krb5_error_code
smb_krb5_verify_checksum(krb5_context context
,
1093 const krb5_keyblock
*keyblock
,
1094 krb5_keyusage usage
,
1095 krb5_checksum
*cksum
,
1099 krb5_error_code ret
;
1101 /* verify the checksum */
1103 /* welcome to the wonderful world of samba's kerberos abstraction layer:
1105 * function heimdal 0.6.1rc3 heimdal 0.7 MIT krb 1.4.2
1106 * -----------------------------------------------------------------------------
1107 * krb5_c_verify_checksum - works works
1108 * krb5_verify_checksum works (6 args) works (6 args) broken (7 args)
1111 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
1113 krb5_boolean checksum_valid
= False
;
1116 input
.data
= (char *)data
;
1117 input
.length
= length
;
1119 ret
= krb5_c_verify_checksum(context
,
1126 DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n",
1127 error_message(ret
)));
1131 if (!checksum_valid
)
1132 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
1135 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
1137 /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
1138 * without enctype and it ignores any key_usage types - Guenther */
1143 ret
= krb5_crypto_init(context
,
1148 DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n",
1149 error_message(ret
)));
1153 ret
= krb5_verify_checksum(context
,
1160 krb5_crypto_destroy(context
, crypto
);
1164 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
1170 time_t get_authtime_from_tkt(krb5_ticket
*tkt
)
1172 #if defined(HAVE_KRB5_TKT_ENC_PART2)
1173 return tkt
->enc_part2
->times
.authtime
;
1175 return tkt
->ticket
.authtime
;
1179 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1180 static int get_kvno_from_ap_req(krb5_ap_req
*ap_req
)
1182 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
1183 if (ap_req
->ticket
->enc_part
.kvno
)
1184 return ap_req
->ticket
->enc_part
.kvno
;
1186 if (ap_req
->ticket
.enc_part
.kvno
)
1187 return *ap_req
->ticket
.enc_part
.kvno
;
1192 static krb5_enctype
get_enctype_from_ap_req(krb5_ap_req
*ap_req
)
1194 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
1195 return ap_req
->ticket
.enc_part
.etype
;
1197 return ap_req
->ticket
->enc_part
.enctype
;
1200 #endif /* HAVE_KRB5_DECODE_AP_REQ */
1202 static krb5_error_code
1203 get_key_from_keytab(krb5_context context
,
1204 krb5_const_principal server
,
1205 krb5_enctype enctype
,
1207 krb5_keyblock
**out_key
)
1209 krb5_keytab_entry entry
;
1210 krb5_error_code ret
;
1213 krb5_keyblock
*keyp
;
1215 /* We have to open a new keytab handle here, as MIT does
1216 an implicit open/getnext/close on krb5_kt_get_entry. We
1217 may be in the middle of a keytab enumeration when this is
1220 ret
= smb_krb5_open_keytab(context
, NULL
, False
, &keytab
);
1222 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret
)));
1226 if ( DEBUGLEVEL
>= 10 ) {
1227 if (smb_krb5_unparse_name(talloc_tos(), context
, server
, &name
) == 0) {
1228 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
1229 kvno
, enctype
, name
));
1234 ret
= krb5_kt_get_entry(context
,
1242 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret
)));
1246 keyp
= KRB5_KT_KEY(&entry
);
1248 ret
= krb5_copy_keyblock(context
, keyp
, out_key
);
1250 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret
)));
1254 smb_krb5_kt_free_entry(context
, &entry
);
1257 krb5_kt_close(context
, keytab
);
1263 krb5_error_code
smb_krb5_get_keyinfo_from_ap_req(krb5_context context
,
1264 const krb5_data
*inbuf
,
1266 krb5_enctype
*enctype
)
1268 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1270 krb5_error_code ret
;
1273 ret
= krb5_decode_ap_req(context
, inbuf
, &ap_req
);
1277 *kvno
= get_kvno_from_ap_req(&ap_req
);
1278 *enctype
= get_enctype_from_ap_req(&ap_req
);
1280 free_AP_REQ(&ap_req
);
1285 /* Possibly not an appropriate error code. */
1286 return KRB5KDC_ERR_BADOPTION
;
1289 krb5_error_code
krb5_rd_req_return_keyblock_from_keytab(krb5_context context
,
1290 krb5_auth_context
*auth_context
,
1291 const krb5_data
*inbuf
,
1292 krb5_const_principal server
,
1294 krb5_flags
*ap_req_options
,
1295 krb5_ticket
**ticket
,
1296 krb5_keyblock
**keyblock
)
1298 krb5_error_code ret
;
1300 krb5_enctype enctype
;
1301 krb5_keyblock
*local_keyblock
;
1303 ret
= krb5_rd_req(context
,
1314 #ifdef KRB5_TICKET_HAS_KEYINFO
1315 enctype
= (*ticket
)->enc_part
.enctype
;
1316 kvno
= (*ticket
)->enc_part
.kvno
;
1318 ret
= smb_krb5_get_keyinfo_from_ap_req(context
, inbuf
, &kvno
, &enctype
);
1324 ret
= get_key_from_keytab(context
,
1330 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1335 if (ret
&& local_keyblock
!= NULL
) {
1336 krb5_free_keyblock(context
, local_keyblock
);
1338 *keyblock
= local_keyblock
;
1344 krb5_error_code
smb_krb5_parse_name_norealm(krb5_context context
,
1346 krb5_principal
*principal
)
1348 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1349 return smb_krb5_parse_name_norealm_conv(context
, name
, principal
);
1352 /* we are cheating here because parse_name will in fact set the realm.
1353 * We don't care as the only caller of smb_krb5_parse_name_norealm
1354 * ignores the realm anyway when calling
1355 * smb_krb5_principal_compare_any_realm later - Guenther */
1357 return smb_krb5_parse_name(context
, name
, principal
);
1360 bool smb_krb5_principal_compare_any_realm(krb5_context context
,
1361 krb5_const_principal princ1
,
1362 krb5_const_principal princ2
)
1364 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1366 return krb5_principal_compare_any_realm(context
, princ1
, princ2
);
1368 /* krb5_princ_size is a macro in MIT */
1369 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1372 const krb5_data
*p1
, *p2
;
1374 len1
= krb5_princ_size(context
, princ1
);
1375 len2
= krb5_princ_size(context
, princ2
);
1380 for (i
= 0; i
< len1
; i
++) {
1382 p1
= krb5_princ_component(context
, CONST_DISCARD(krb5_principal
, princ1
), i
);
1383 p2
= krb5_princ_component(context
, CONST_DISCARD(krb5_principal
, princ2
), i
);
1385 if (p1
->length
!= p2
->length
|| memcmp(p1
->data
, p2
->data
, p1
->length
))
1391 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1395 krb5_error_code
smb_krb5_renew_ticket(const char *ccache_string
, /* FILE:/tmp/krb5cc_0 */
1396 const char *client_string
, /* gd@BER.SUSE.DE */
1397 const char *service_string
, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1398 time_t *expire_time
)
1400 krb5_error_code ret
;
1401 krb5_context context
= NULL
;
1402 krb5_ccache ccache
= NULL
;
1403 krb5_principal client
= NULL
;
1404 krb5_creds creds
, creds_in
, *creds_out
= NULL
;
1407 ZERO_STRUCT(creds_in
);
1409 initialize_krb5_error_table();
1410 ret
= krb5_init_context(&context
);
1415 if (!ccache_string
) {
1416 ccache_string
= krb5_cc_default_name(context
);
1419 if (!ccache_string
) {
1424 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string
));
1426 /* FIXME: we should not fall back to defaults */
1427 ret
= krb5_cc_resolve(context
, CONST_DISCARD(char *, ccache_string
), &ccache
);
1432 if (client_string
) {
1433 ret
= smb_krb5_parse_name(context
, client_string
, &client
);
1438 ret
= krb5_cc_get_principal(context
, ccache
, &client
);
1444 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1446 ret
= krb5_get_renewed_creds(context
, &creds
, client
, ccache
, CONST_DISCARD(char *, service_string
));
1448 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1452 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1454 krb5_kdc_flags flags
;
1455 krb5_realm
*client_realm
= NULL
;
1457 ret
= krb5_copy_principal(context
, client
, &creds_in
.client
);
1462 if (service_string
) {
1463 ret
= smb_krb5_parse_name(context
, service_string
, &creds_in
.server
);
1468 /* build tgt service by default */
1469 client_realm
= krb5_princ_realm(context
, creds_in
.client
);
1470 if (!client_realm
) {
1474 ret
= krb5_make_principal(context
, &creds_in
.server
, *client_realm
, KRB5_TGS_NAME
, *client_realm
, NULL
);
1481 flags
.b
.renewable
= flags
.b
.renew
= True
;
1483 ret
= krb5_get_kdc_cred(context
, ccache
, flags
, NULL
, NULL
, &creds_in
, &creds_out
);
1485 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1492 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1495 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1496 ret
= krb5_cc_initialize(context
, ccache
, client
);
1501 ret
= krb5_cc_store_cred(context
, ccache
, &creds
);
1504 *expire_time
= (time_t) creds
.times
.endtime
;
1508 krb5_free_cred_contents(context
, &creds_in
);
1511 krb5_free_creds(context
, creds_out
);
1513 krb5_free_cred_contents(context
, &creds
);
1517 krb5_free_principal(context
, client
);
1520 krb5_cc_close(context
, ccache
);
1523 krb5_free_context(context
);
1529 krb5_error_code
smb_krb5_free_addresses(krb5_context context
, smb_krb5_addresses
*addr
)
1531 krb5_error_code ret
= 0;
1535 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1536 krb5_free_addresses(context
, addr
->addrs
);
1537 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1538 ret
= krb5_free_addresses(context
, addr
->addrs
);
1539 SAFE_FREE(addr
->addrs
);
1546 krb5_error_code
smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses
**kerb_addr
)
1548 krb5_error_code ret
= 0;
1550 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1551 krb5_address
**addrs
= NULL
;
1552 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1553 krb5_addresses
*addrs
= NULL
;
1556 *kerb_addr
= (smb_krb5_addresses
*)SMB_MALLOC(sizeof(smb_krb5_addresses
));
1557 if (*kerb_addr
== NULL
) {
1561 put_name(buf
, global_myname(), ' ', 0x20);
1563 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1567 addrs
= (krb5_address
**)SMB_MALLOC(sizeof(krb5_address
*) * num_addr
);
1568 if (addrs
== NULL
) {
1569 SAFE_FREE(*kerb_addr
);
1573 memset(addrs
, 0, sizeof(krb5_address
*) * num_addr
);
1575 addrs
[0] = (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1576 if (addrs
[0] == NULL
) {
1578 SAFE_FREE(*kerb_addr
);
1582 addrs
[0]->magic
= KV5M_ADDRESS
;
1583 addrs
[0]->addrtype
= KRB5_ADDR_NETBIOS
;
1584 addrs
[0]->length
= MAX_NETBIOSNAME_LEN
;
1585 addrs
[0]->contents
= (unsigned char *)SMB_MALLOC(addrs
[0]->length
);
1586 if (addrs
[0]->contents
== NULL
) {
1587 SAFE_FREE(addrs
[0]);
1589 SAFE_FREE(*kerb_addr
);
1593 memcpy(addrs
[0]->contents
, buf
, addrs
[0]->length
);
1597 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1599 addrs
= (krb5_addresses
*)SMB_MALLOC(sizeof(krb5_addresses
));
1600 if (addrs
== NULL
) {
1601 SAFE_FREE(*kerb_addr
);
1605 memset(addrs
, 0, sizeof(krb5_addresses
));
1608 addrs
->val
= (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1609 if (addrs
->val
== NULL
) {
1611 SAFE_FREE(kerb_addr
);
1615 addrs
->val
[0].addr_type
= KRB5_ADDR_NETBIOS
;
1616 addrs
->val
[0].address
.length
= MAX_NETBIOSNAME_LEN
;
1617 addrs
->val
[0].address
.data
= (unsigned char *)SMB_MALLOC(addrs
->val
[0].address
.length
);
1618 if (addrs
->val
[0].address
.data
== NULL
) {
1619 SAFE_FREE(addrs
->val
);
1621 SAFE_FREE(*kerb_addr
);
1625 memcpy(addrs
->val
[0].address
.data
, buf
, addrs
->val
[0].address
.length
);
1628 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1630 (*kerb_addr
)->addrs
= addrs
;
1635 void smb_krb5_free_error(krb5_context context
, krb5_error
*krberror
)
1637 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1638 krb5_free_error_contents(context
, krberror
);
1640 krb5_free_error(context
, krberror
);
1644 krb5_error_code
handle_krberror_packet(krb5_context context
,
1647 krb5_error_code ret
;
1648 bool got_error_code
= False
;
1650 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1652 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1654 krb5_error krberror
;
1656 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1657 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1658 error_message(ret
)));
1662 if (krberror
.e_data
== NULL
|| krberror
.e_data
->data
== NULL
) {
1663 ret
= (krb5_error_code
) krberror
.error_code
;
1664 got_error_code
= True
;
1667 smb_krb5_free_error(context
, &krberror
);
1671 krb5_error
*krberror
;
1673 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1674 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1675 error_message(ret
)));
1679 if (krberror
->e_data
.data
== NULL
) {
1680 #if defined(ERROR_TABLE_BASE_krb5)
1681 ret
= ERROR_TABLE_BASE_krb5
+ (krb5_error_code
) krberror
->error
;
1683 ret
= (krb5_error_code
)krberror
->error
;
1685 got_error_code
= True
;
1687 smb_krb5_free_error(context
, krberror
);
1690 if (got_error_code
) {
1691 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1692 error_message(ret
), ret
));
1697 krb5_error_code
smb_krb5_get_init_creds_opt_alloc(krb5_context context
,
1698 krb5_get_init_creds_opt
**opt
)
1700 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1701 /* Heimdal or modern MIT version */
1702 return krb5_get_init_creds_opt_alloc(context
, opt
);
1704 /* Historical MIT version */
1705 krb5_get_init_creds_opt
*my_opt
;
1709 if ((my_opt
= SMB_MALLOC_P(krb5_get_init_creds_opt
)) == NULL
) {
1713 krb5_get_init_creds_opt_init(my_opt
);
1717 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1720 void smb_krb5_get_init_creds_opt_free(krb5_context context
,
1721 krb5_get_init_creds_opt
*opt
)
1723 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1725 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1726 /* Modern MIT or Heimdal version */
1727 krb5_get_init_creds_opt_free(context
, opt
);
1729 /* Heimdal version */
1730 krb5_get_init_creds_opt_free(opt
);
1731 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1733 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1734 /* Historical MIT version */
1737 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1740 krb5_enctype
smb_get_enctype_from_kt_entry(krb5_keytab_entry
*kt_entry
)
1742 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry
));
1746 /* caller needs to free etype_s */
1747 krb5_error_code
smb_krb5_enctype_to_string(krb5_context context
,
1748 krb5_enctype enctype
,
1751 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1752 return krb5_enctype_to_string(context
, enctype
, etype_s
); /* Heimdal */
1753 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1755 krb5_error_code ret
= krb5_enctype_to_string(enctype
, buf
, 256); /* MIT */
1759 *etype_s
= SMB_STRDUP(buf
);
1765 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1769 krb5_error_code
smb_krb5_mk_error(krb5_context context
,
1770 krb5_error_code error_code
,
1771 const krb5_principal server
,
1774 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1776 * The MIT interface is *terrible*.
1777 * We have to construct this ourselves...
1781 memset(&e
, 0, sizeof(e
));
1782 krb5_us_timeofday(context
, &e
.stime
, &e
.susec
);
1784 #if defined(krb5_err_base)
1785 e
.error
= error_code
- krb5_err_base
;
1786 #elif defined(ERROR_TABLE_BASE_krb5)
1787 e
.error
= error_code
- ERROR_TABLE_BASE_krb5
;
1789 e
.error
= error_code
; /* Almost certainly wrong, but what can we do... ? */
1792 return krb5_mk_error(context
, &e
, reply
);
1793 #else /* Heimdal. */
1794 return krb5_mk_error(context
,
1806 /**********************************************************************
1807 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1808 * allows to process non-default keytab names.
1809 * @param context krb5_context
1810 * @param keytab_name_req string
1811 * @param write_access bool if writable keytab is required
1812 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1813 * @return krb5_error_code
1814 **********************************************************************/
1816 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1817 #ifndef MAX_KEYTAB_NAME_LEN
1818 #define MAX_KEYTAB_NAME_LEN 1100
1821 krb5_error_code
smb_krb5_open_keytab(krb5_context context
,
1822 const char *keytab_name_req
,
1824 krb5_keytab
*keytab
)
1826 krb5_error_code ret
= 0;
1827 TALLOC_CTX
*mem_ctx
;
1828 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1829 char *kt_str
= NULL
;
1830 bool found_valid_name
= False
;
1831 const char *pragma
= "FILE";
1832 const char *tmp
= NULL
;
1834 if (!write_access
&& !keytab_name_req
) {
1835 /* caller just wants to read the default keytab readonly, so be it */
1836 return krb5_kt_default(context
, keytab
);
1839 mem_ctx
= talloc_init("smb_krb5_open_keytab");
1844 #ifdef HAVE_WRFILE_KEYTAB
1850 if (keytab_name_req
) {
1852 if (strlen(keytab_name_req
) > MAX_KEYTAB_NAME_LEN
) {
1853 ret
= KRB5_CONFIG_NOTENUFSPACE
;
1857 if ((strncmp(keytab_name_req
, "WRFILE:/", 8) == 0) ||
1858 (strncmp(keytab_name_req
, "FILE:/", 6) == 0)) {
1859 tmp
= keytab_name_req
;
1863 if (keytab_name_req
[0] != '/') {
1864 ret
= KRB5_KT_BADNAME
;
1868 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, keytab_name_req
);
1877 /* we need to handle more complex keytab_strings, like:
1878 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1880 ret
= krb5_kt_default_name(context
, &keytab_string
[0], MAX_KEYTAB_NAME_LEN
- 2);
1885 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string
));
1887 tmp
= talloc_strdup(mem_ctx
, keytab_string
);
1893 if (strncmp(tmp
, "ANY:", 4) == 0) {
1897 memset(&keytab_string
, '\0', sizeof(keytab_string
));
1899 while (next_token_talloc(mem_ctx
, &tmp
, &kt_str
, ",")) {
1900 if (strncmp(kt_str
, "WRFILE:", 7) == 0) {
1901 found_valid_name
= True
;
1906 if (strncmp(kt_str
, "FILE:", 5) == 0) {
1907 found_valid_name
= True
;
1912 if (tmp
[0] == '/') {
1913 /* Treat as a FILE: keytab definition. */
1914 found_valid_name
= true;
1917 if (found_valid_name
) {
1918 if (tmp
[0] != '/') {
1919 ret
= KRB5_KT_BADNAME
;
1923 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, tmp
);
1932 if (!found_valid_name
) {
1933 ret
= KRB5_KT_UNKNOWN_TYPE
;
1938 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp
));
1939 ret
= krb5_kt_resolve(context
, tmp
, keytab
);
1942 TALLOC_FREE(mem_ctx
);
1946 krb5_error_code
smb_krb5_keytab_name(TALLOC_CTX
*mem_ctx
,
1947 krb5_context context
,
1949 const char **keytab_name
)
1951 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1952 krb5_error_code ret
= 0;
1954 ret
= krb5_kt_get_name(context
, keytab
,
1955 keytab_string
, MAX_KEYTAB_NAME_LEN
- 2);
1960 *keytab_name
= talloc_strdup(mem_ctx
, keytab_string
);
1961 if (!*keytab_name
) {
1968 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1969 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1970 defined(HAVE_KRB5_GET_CREDS)
1971 static krb5_error_code
smb_krb5_get_credentials_for_user_opt(krb5_context context
,
1974 krb5_principal server
,
1975 krb5_principal impersonate_princ
,
1976 krb5_creds
**out_creds
)
1978 krb5_error_code ret
;
1979 krb5_get_creds_opt opt
;
1981 ret
= krb5_get_creds_opt_alloc(context
, &opt
);
1985 krb5_get_creds_opt_add_options(context
, opt
, KRB5_GC_FORWARDABLE
);
1987 if (impersonate_princ
) {
1988 ret
= krb5_get_creds_opt_set_impersonate(context
, opt
,
1995 ret
= krb5_get_creds(context
, opt
, ccache
, server
, out_creds
);
2002 krb5_get_creds_opt_free(context
, opt
);
2006 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
2008 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
2009 static krb5_error_code
smb_krb5_get_credentials_for_user(krb5_context context
,
2012 krb5_principal server
,
2013 krb5_principal impersonate_princ
,
2014 krb5_creds
**out_creds
)
2016 krb5_error_code ret
;
2017 krb5_creds in_creds
;
2019 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
2020 krb5_error_code KRB5_CALLCONV
2021 krb5_get_credentials_for_user(krb5_context context
, krb5_flags options
,
2022 krb5_ccache ccache
, krb5_creds
*in_creds
,
2023 krb5_data
*subject_cert
,
2024 krb5_creds
**out_creds
);
2025 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
2027 ZERO_STRUCT(in_creds
);
2029 if (impersonate_princ
) {
2031 in_creds
.server
= me
;
2032 in_creds
.client
= impersonate_princ
;
2034 ret
= krb5_get_credentials_for_user(context
,
2035 0, /* krb5_flags options */
2038 NULL
, /* krb5_data *subject_cert */
2041 in_creds
.client
= me
;
2042 in_creds
.server
= server
;
2044 ret
= krb5_get_credentials(context
, 0, ccache
,
2045 &in_creds
, out_creds
);
2050 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
2053 * smb_krb5_get_credentials
2055 * @brief Get krb5 credentials for a server
2057 * @param[in] context An initialized krb5_context
2058 * @param[in] ccache An initialized krb5_ccache
2059 * @param[in] me The krb5_principal of the caller
2060 * @param[in] server The krb5_principal of the requested service
2061 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
2062 * @param[out] out_creds The returned krb5_creds structure
2063 * @return krb5_error_code
2066 krb5_error_code
smb_krb5_get_credentials(krb5_context context
,
2069 krb5_principal server
,
2070 krb5_principal impersonate_princ
,
2071 krb5_creds
**out_creds
)
2073 krb5_error_code ret
;
2074 krb5_creds
*creds
= NULL
;
2078 if (impersonate_princ
) {
2079 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
2080 ret
= smb_krb5_get_credentials_for_user_opt(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
2081 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
2082 ret
= smb_krb5_get_credentials_for_user(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
2087 krb5_creds in_creds
;
2089 ZERO_STRUCT(in_creds
);
2091 in_creds
.client
= me
;
2092 in_creds
.server
= server
;
2094 ret
= krb5_get_credentials(context
, 0, ccache
,
2101 ret
= krb5_cc_store_cred(context
, ccache
, creds
);
2112 krb5_free_creds(context
, creds
);
2119 * smb_krb5_get_creds
2121 * @brief Get krb5 credentials for a server
2123 * @param[in] server_s The string name of the service
2124 * @param[in] time_offset The offset to the KDCs time in seconds (optional)
2125 * @param[in] cc The krb5 credential cache string name (optional)
2126 * @param[in] impersonate_princ_s The string principal name to impersonate (optional)
2127 * @param[out] creds_p The returned krb5_creds structure
2128 * @return krb5_error_code
2131 krb5_error_code
smb_krb5_get_creds(const char *server_s
,
2134 const char *impersonate_princ_s
,
2135 krb5_creds
**creds_p
)
2137 krb5_error_code ret
;
2138 krb5_context context
= NULL
;
2139 krb5_principal me
= NULL
;
2140 krb5_principal server
= NULL
;
2141 krb5_principal impersonate_princ
= NULL
;
2142 krb5_creds
*creds
= NULL
;
2143 krb5_ccache ccache
= NULL
;
2147 initialize_krb5_error_table();
2148 ret
= krb5_init_context(&context
);
2153 if (time_offset
!= 0) {
2154 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
2157 ret
= krb5_cc_resolve(context
, cc
? cc
:
2158 krb5_cc_default_name(context
), &ccache
);
2163 ret
= krb5_cc_get_principal(context
, ccache
, &me
);
2168 ret
= smb_krb5_parse_name(context
, server_s
, &server
);
2173 if (impersonate_princ_s
) {
2174 ret
= smb_krb5_parse_name(context
, impersonate_princ_s
,
2175 &impersonate_princ
);
2181 ret
= smb_krb5_get_credentials(context
, ccache
,
2182 me
, server
, impersonate_princ
,
2188 ret
= krb5_cc_store_cred(context
, ccache
, creds
);
2197 DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n",
2200 if (impersonate_princ_s
) {
2201 char *client
= NULL
;
2203 ret
= smb_krb5_unparse_name(talloc_tos(), context
, creds
->client
, &client
);
2207 DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n",
2209 TALLOC_FREE(client
);
2218 krb5_free_creds(context
, creds
);
2221 krb5_free_principal(context
, server
);
2224 krb5_free_principal(context
, me
);
2226 if (impersonate_princ
) {
2227 krb5_free_principal(context
, impersonate_princ
);
2230 krb5_cc_close(context
, ccache
);
2232 krb5_free_context(context
);
2238 * smb_krb5_principal_get_realm
2240 * @brief Get realm of a principal
2242 * @param[in] context The krb5_context
2243 * @param[in] principal The principal
2244 * @return pointer to the realm
2248 char *smb_krb5_principal_get_realm(krb5_context context
,
2249 krb5_principal principal
)
2251 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2252 return krb5_principal_get_realm(context
, principal
);
2253 #elif defined(krb5_princ_realm) /* MIT */
2255 realm
= krb5_princ_realm(context
, principal
);
2256 return (char *)realm
->data
;
2262 #else /* HAVE_KRB5 */
2263 /* this saves a few linking headaches */
2264 int cli_krb5_get_ticket(const char *principal
, time_t time_offset
,
2265 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
, uint32 extra_ap_opts
,
2266 const char *ccname
, time_t *tgs_expire
,
2267 const char *impersonate_princ_s
)
2269 DEBUG(0,("NO KERBEROS SUPPORT\n"));