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 (12+GSSAPI_BNDLENGTH)
33 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY)
34 static krb5_error_code
ads_krb5_get_fwd_ticket( krb5_context context
,
35 krb5_auth_context
*auth_context
,
38 krb5_data
*authenticator
);
41 /**************************************************************
42 Wrappers around kerberos string functions that convert from
43 utf8 -> unix charset and vica versa.
44 **************************************************************/
46 /**************************************************************
47 krb5_parse_name that takes a UNIX charset.
48 **************************************************************/
50 krb5_error_code
smb_krb5_parse_name(krb5_context context
,
51 const char *name
, /* in unix charset */
52 krb5_principal
*principal
)
56 size_t converted_size
;
58 if (!push_utf8_talloc(talloc_tos(), &utf8_name
, name
, &converted_size
)) {
62 ret
= krb5_parse_name(context
, utf8_name
, principal
);
63 TALLOC_FREE(utf8_name
);
67 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
68 /**************************************************************
69 krb5_parse_name_norealm that takes a UNIX charset.
70 **************************************************************/
72 static krb5_error_code
smb_krb5_parse_name_norealm_conv(krb5_context context
,
73 const char *name
, /* in unix charset */
74 krb5_principal
*principal
)
78 size_t converted_size
;
81 if (!push_utf8_talloc(talloc_tos(), &utf8_name
, name
, &converted_size
)) {
85 ret
= krb5_parse_name_norealm(context
, utf8_name
, principal
);
86 TALLOC_FREE(utf8_name
);
91 /**************************************************************
92 krb5_parse_name that returns a UNIX charset name. Must
93 be freed with talloc_free() call.
94 **************************************************************/
96 krb5_error_code
smb_krb5_unparse_name(TALLOC_CTX
*mem_ctx
,
98 krb5_const_principal principal
,
103 size_t converted_size
;
106 ret
= krb5_unparse_name(context
, principal
, &utf8_name
);
111 if (!pull_utf8_talloc(mem_ctx
, unix_name
, utf8_name
, &converted_size
)) {
112 krb5_free_unparsed_name(context
, utf8_name
);
115 krb5_free_unparsed_name(context
, utf8_name
);
119 #ifndef HAVE_KRB5_SET_REAL_TIME
121 * This function is not in the Heimdal mainline.
123 krb5_error_code
krb5_set_real_time(krb5_context context
, int32_t seconds
, int32_t microseconds
)
128 ret
= krb5_us_timeofday(context
, &sec
, &usec
);
132 context
->kdc_sec_offset
= seconds
- sec
;
133 context
->kdc_usec_offset
= microseconds
- usec
;
139 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
141 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
143 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
144 * to krb5_set_default_tgs_ktypes. See
145 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
147 * If the MIT libraries are not exporting internal symbols, we will end up in
148 * this branch, which is correct. Otherwise we will continue to use the
151 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
153 return krb5_set_default_tgs_enctypes(ctx
, enc
);
156 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
159 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
161 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
164 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
166 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
168 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
170 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
172 memset(pkaddr
, '\0', sizeof(krb5_address
));
173 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
174 if (paddr
->ss_family
== AF_INET6
) {
175 pkaddr
->addr_type
= KRB5_ADDRESS_INET6
;
176 pkaddr
->address
.length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
177 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
181 if (paddr
->ss_family
== AF_INET
) {
182 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
183 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
184 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
189 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
191 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
193 memset(pkaddr
, '\0', sizeof(krb5_address
));
194 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
195 if (paddr
->ss_family
== AF_INET6
) {
196 pkaddr
->addrtype
= ADDRTYPE_INET6
;
197 pkaddr
->length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
198 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
202 if (paddr
->ss_family
== AF_INET
) {
203 pkaddr
->addrtype
= ADDRTYPE_INET
;
204 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
205 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
211 #error UNKNOWN_ADDRTYPE
214 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
215 static int create_kerberos_key_from_string_direct(krb5_context context
,
216 krb5_principal host_princ
,
219 krb5_enctype enctype
)
223 krb5_encrypt_block eblock
;
225 ret
= krb5_principal2salt(context
, host_princ
, &salt
);
227 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret
)));
230 krb5_use_enctype(context
, &eblock
, enctype
);
231 ret
= krb5_string_to_key(context
, &eblock
, key
, password
, &salt
);
232 SAFE_FREE(salt
.data
);
236 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
237 static int create_kerberos_key_from_string_direct(krb5_context context
,
238 krb5_principal host_princ
,
241 krb5_enctype enctype
)
246 ret
= krb5_get_pw_salt(context
, host_princ
, &salt
);
248 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret
)));
252 ret
= krb5_string_to_key_salt(context
, enctype
, (const char *)password
->data
, salt
, key
);
253 krb5_free_salt(context
, salt
);
258 #error UNKNOWN_CREATE_KEY_FUNCTIONS
261 int create_kerberos_key_from_string(krb5_context context
,
262 krb5_principal host_princ
,
265 krb5_enctype enctype
,
268 krb5_principal salt_princ
= NULL
;
271 * Check if we've determined that the KDC is salting keys for this
272 * principal/enctype in a non-obvious way. If it is, try to match
276 KRB5_KEY_DATA(key
) = (KRB5_KEY_DATA_CAST
*)SMB_MALLOC(password
->length
);
277 if (!KRB5_KEY_DATA(key
)) {
280 memcpy(KRB5_KEY_DATA(key
), password
->data
, password
->length
);
281 KRB5_KEY_LENGTH(key
) = password
->length
;
282 KRB5_KEY_TYPE(key
) = enctype
;
285 salt_princ
= kerberos_fetch_salt_princ_for_host_princ(context
, host_princ
, enctype
);
286 ret
= create_kerberos_key_from_string_direct(context
, salt_princ
? salt_princ
: host_princ
, password
, key
, enctype
);
288 krb5_free_principal(context
, salt_princ
);
293 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
294 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
295 krb5_enctype
**enctypes
)
297 return krb5_get_permitted_enctypes(context
, enctypes
);
299 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
300 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
301 krb5_enctype
**enctypes
)
303 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
306 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
309 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
310 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
311 krb5_auth_context auth_context
,
312 krb5_keyblock
*keyblock
)
314 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
318 bool unwrap_edata_ntstatus(TALLOC_CTX
*mem_ctx
,
320 DATA_BLOB
*edata_out
)
322 DATA_BLOB edata_contents
;
326 if (!edata
->length
) {
330 data
= asn1_init(mem_ctx
);
335 asn1_load(data
, *edata
);
336 asn1_start_tag(data
, ASN1_SEQUENCE(0));
337 asn1_start_tag(data
, ASN1_CONTEXT(1));
338 asn1_read_Integer(data
, &edata_type
);
340 if (edata_type
!= KRB5_PADATA_PW_SALT
) {
341 DEBUG(0,("edata is not of required type %d but of type %d\n",
342 KRB5_PADATA_PW_SALT
, edata_type
));
347 asn1_start_tag(data
, ASN1_CONTEXT(2));
348 asn1_read_OctetString(data
, talloc_autofree_context(), &edata_contents
);
354 *edata_out
= data_blob_talloc(mem_ctx
, edata_contents
.data
, edata_contents
.length
);
356 data_blob_free(&edata_contents
);
362 bool unwrap_pac(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, DATA_BLOB
*unwrapped_pac_data
)
364 DATA_BLOB pac_contents
;
368 if (!auth_data
->length
) {
372 data
= asn1_init(mem_ctx
);
377 asn1_load(data
, *auth_data
);
378 asn1_start_tag(data
, ASN1_SEQUENCE(0));
379 asn1_start_tag(data
, ASN1_SEQUENCE(0));
380 asn1_start_tag(data
, ASN1_CONTEXT(0));
381 asn1_read_Integer(data
, &data_type
);
383 if (data_type
!= KRB5_AUTHDATA_WIN2K_PAC
) {
384 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type
));
390 asn1_start_tag(data
, ASN1_CONTEXT(1));
391 asn1_read_OctetString(data
, talloc_autofree_context(), &pac_contents
);
397 *unwrapped_pac_data
= data_blob_talloc(mem_ctx
, pac_contents
.data
, pac_contents
.length
);
399 data_blob_free(&pac_contents
);
404 bool get_auth_data_from_tkt(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, krb5_ticket
*tkt
)
406 DATA_BLOB auth_data_wrapped
;
407 bool got_auth_data_pac
= False
;
410 #if defined(HAVE_KRB5_TKT_ENC_PART2)
411 if (tkt
->enc_part2
&& tkt
->enc_part2
->authorization_data
&&
412 tkt
->enc_part2
->authorization_data
[0] &&
413 tkt
->enc_part2
->authorization_data
[0]->length
)
415 for (i
= 0; tkt
->enc_part2
->authorization_data
[i
] != NULL
; i
++) {
417 if (tkt
->enc_part2
->authorization_data
[i
]->ad_type
!=
418 KRB5_AUTHDATA_IF_RELEVANT
) {
419 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
420 tkt
->enc_part2
->authorization_data
[i
]->ad_type
));
424 auth_data_wrapped
= data_blob(tkt
->enc_part2
->authorization_data
[i
]->contents
,
425 tkt
->enc_part2
->authorization_data
[i
]->length
);
427 /* check if it is a PAC */
428 got_auth_data_pac
= unwrap_pac(mem_ctx
, &auth_data_wrapped
, auth_data
);
429 data_blob_free(&auth_data_wrapped
);
431 if (got_auth_data_pac
) {
436 return got_auth_data_pac
;
440 if (tkt
->ticket
.authorization_data
&&
441 tkt
->ticket
.authorization_data
->len
)
443 for (i
= 0; i
< tkt
->ticket
.authorization_data
->len
; i
++) {
445 if (tkt
->ticket
.authorization_data
->val
[i
].ad_type
!=
446 KRB5_AUTHDATA_IF_RELEVANT
) {
447 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
448 tkt
->ticket
.authorization_data
->val
[i
].ad_type
));
452 auth_data_wrapped
= data_blob(tkt
->ticket
.authorization_data
->val
[i
].ad_data
.data
,
453 tkt
->ticket
.authorization_data
->val
[i
].ad_data
.length
);
455 /* check if it is a PAC */
456 got_auth_data_pac
= unwrap_pac(mem_ctx
, &auth_data_wrapped
, auth_data
);
457 data_blob_free(&auth_data_wrapped
);
459 if (got_auth_data_pac
) {
464 return got_auth_data_pac
;
470 krb5_const_principal
get_principal_from_tkt(krb5_ticket
*tkt
)
472 #if defined(HAVE_KRB5_TKT_ENC_PART2)
473 return tkt
->enc_part2
->client
;
479 #if !defined(HAVE_KRB5_LOCATE_KDC)
481 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
482 * to a public interface for this functionality, so we have to be able to live
483 * without it if the MIT libraries are hiding their internal symbols.
486 #if defined(KRB5_KRBHST_INIT)
488 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
, struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
490 krb5_krbhst_handle hnd
;
491 krb5_krbhst_info
*hinfo
;
500 rc
= krb5_krbhst_init(ctx
, realm
->data
, KRB5_KRBHST_KDC
, &hnd
);
502 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc
)));
506 for ( num_kdcs
= 0; (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); num_kdcs
++)
509 krb5_krbhst_reset(ctx
, hnd
);
512 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
513 krb5_krbhst_free(ctx
, hnd
);
517 sa
= SMB_MALLOC_ARRAY( struct sockaddr
, num_kdcs
);
519 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
520 krb5_krbhst_free(ctx
, hnd
);
525 memset(sa
, '\0', sizeof(struct sockaddr
) * num_kdcs
);
527 for (i
= 0; i
< num_kdcs
&& (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); i
++) {
529 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
530 rc
= krb5_krbhst_get_addrinfo(ctx
, hinfo
, &ai
);
532 DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc
)));
536 if (hinfo
->ai
&& hinfo
->ai
->ai_family
== AF_INET
)
537 memcpy(&sa
[i
], hinfo
->ai
->ai_addr
, sizeof(struct sockaddr
));
540 krb5_krbhst_free(ctx
, hnd
);
547 #else /* ! defined(KRB5_KRBHST_INIT) */
549 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
,
550 struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
552 DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
553 return KRB5_KDC_UNREACH
;
556 #endif /* KRB5_KRBHST_INIT */
558 #else /* ! HAVE_KRB5_LOCATE_KDC */
560 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
,
561 struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
563 return krb5_locate_kdc(ctx
, realm
, addr_pp
, naddrs
, get_masters
);
566 #endif /* HAVE_KRB5_LOCATE_KDC */
568 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
569 void krb5_free_unparsed_name(krb5_context context
, char *val
)
575 void kerberos_free_data_contents(krb5_context context
, krb5_data
*pdata
)
577 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
579 krb5_free_data_contents(context
, pdata
);
582 SAFE_FREE(pdata
->data
);
586 void kerberos_set_creds_enctype(krb5_creds
*pcreds
, int enctype
)
588 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
589 KRB5_KEY_TYPE((&pcreds
->keyblock
)) = enctype
;
590 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
591 KRB5_KEY_TYPE((&pcreds
->session
)) = enctype
;
593 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
597 bool kerberos_compatible_enctypes(krb5_context context
,
598 krb5_enctype enctype1
,
599 krb5_enctype enctype2
)
601 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
602 krb5_boolean similar
= 0;
604 krb5_c_enctype_compare(context
, enctype1
, enctype2
, &similar
);
605 return similar
? True
: False
;
606 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
607 return krb5_enctypes_compatible_keys(context
, enctype1
, enctype2
) ? True
: False
;
611 static bool ads_cleanup_expired_creds(krb5_context context
,
615 krb5_error_code retval
;
616 const char *cc_type
= krb5_cc_get_type(context
, ccache
);
618 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
619 cc_type
, krb5_cc_get_name(context
, ccache
),
620 http_timestring(talloc_tos(), credsp
->times
.endtime
)));
622 /* we will probably need new tickets if the current ones
623 will expire within 10 seconds.
625 if (credsp
->times
.endtime
>= (time(NULL
) + 10))
628 /* heimdal won't remove creds from a file ccache, and
629 perhaps we shouldn't anyway, since internally we
630 use memory ccaches, and a FILE one probably means that
631 we're using creds obtained outside of our exectuable
633 if (strequal(cc_type
, "FILE")) {
634 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type
));
638 retval
= krb5_cc_remove_cred(context
, ccache
, 0, credsp
);
640 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
641 error_message(retval
)));
642 /* If we have an error in this, we want to display it,
643 but continue as though we deleted it */
649 we can't use krb5_mk_req because w2k wants the service to be in a particular format
651 static krb5_error_code
ads_krb5_mk_req(krb5_context context
,
652 krb5_auth_context
*auth_context
,
653 const krb5_flags ap_req_options
,
654 const char *principal
,
658 const char *impersonate_princ_s
)
660 krb5_error_code retval
;
661 krb5_principal server
;
662 krb5_principal impersonate_princ
= NULL
;
666 bool creds_ready
= False
;
667 int i
= 0, maxtries
= 3;
669 ZERO_STRUCT(in_data
);
671 retval
= smb_krb5_parse_name(context
, principal
, &server
);
673 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal
));
677 if (impersonate_princ_s
) {
678 retval
= smb_krb5_parse_name(context
, impersonate_princ_s
,
681 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s
));
686 /* obtain ticket & session key */
688 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
689 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
690 error_message(retval
)));
694 if ((retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
))) {
695 /* This can commonly fail on smbd startup with no ticket in the cache.
696 * Report at higher level than 1. */
697 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
698 error_message(retval
)));
702 while (!creds_ready
&& (i
< maxtries
)) {
704 if ((retval
= smb_krb5_get_credentials(context
, ccache
,
709 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
710 principal
, error_message(retval
)));
714 /* cope with ticket being in the future due to clock skew */
715 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
716 time_t t
= time(NULL
);
717 int time_offset
=(int)((unsigned)credsp
->times
.starttime
-t
);
718 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
719 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
722 if (!ads_cleanup_expired_creds(context
, ccache
, credsp
)) {
729 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
730 principal
, krb5_cc_get_type(context
, ccache
), krb5_cc_get_name(context
, ccache
),
731 http_timestring(talloc_tos(), (unsigned)credsp
->times
.endtime
),
732 (unsigned)credsp
->times
.endtime
));
735 *expire_time
= (time_t)credsp
->times
.endtime
;
738 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY)
739 if( credsp
->ticket_flags
& TKT_FLG_OK_AS_DELEGATE
) {
740 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
741 as part of the kerberos exchange. */
743 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
745 if( *auth_context
== NULL
) {
746 /* Allocate if it has not yet been allocated. */
747 retval
= krb5_auth_con_init( context
, auth_context
);
749 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_init failed (%s)\n",
750 error_message(retval
)));
755 retval
= krb5_auth_con_setuseruserkey( context
, *auth_context
, &credsp
->keyblock
);
757 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_setuseruserkey failed (%s)\n",
758 error_message(retval
)));
762 /* Must use a subkey for forwarded tickets. */
763 retval
= krb5_auth_con_setflags( context
, *auth_context
, KRB5_AUTH_CONTEXT_USE_SUBKEY
);
765 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_setflags failed (%s)\n",
766 error_message(retval
)));
770 retval
= ads_krb5_get_fwd_ticket( context
,
776 DEBUG( 3, ("ads_krb5_get_fwd_ticket failed (%s)\n",
777 error_message( retval
) ) );
780 * This is not fatal. Delete the *auth_context and continue
781 * with krb5_mk_req_extended to get a non-forwardable ticket.
785 free( in_data
.data
);
789 krb5_auth_con_free(context
, *auth_context
);
790 *auth_context
= NULL
;
795 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
796 &in_data
, credsp
, outbuf
);
798 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
799 error_message(retval
)));
803 free( in_data
.data
);
807 krb5_free_creds(context
, credsp
);
810 krb5_free_cred_contents(context
, &creds
);
813 krb5_free_principal(context
, server
);
814 if (impersonate_princ
) {
815 krb5_free_principal(context
, impersonate_princ
);
822 get a kerberos5 ticket for the given service
824 int cli_krb5_get_ticket(const char *principal
, time_t time_offset
,
825 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
826 uint32 extra_ap_opts
, const char *ccname
,
828 const char *impersonate_princ_s
)
831 krb5_error_code retval
;
833 krb5_context context
= NULL
;
834 krb5_ccache ccdef
= NULL
;
835 krb5_auth_context auth_context
= NULL
;
836 krb5_enctype enc_types
[] = {
837 #ifdef ENCTYPE_ARCFOUR_HMAC
838 ENCTYPE_ARCFOUR_HMAC
,
844 initialize_krb5_error_table();
845 retval
= krb5_init_context(&context
);
847 DEBUG(1,("cli_krb5_get_ticket: krb5_init_context failed (%s)\n",
848 error_message(retval
)));
852 if (time_offset
!= 0) {
853 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
856 if ((retval
= krb5_cc_resolve(context
, ccname
?
857 ccname
: krb5_cc_default_name(context
), &ccdef
))) {
858 DEBUG(1,("cli_krb5_get_ticket: krb5_cc_default failed (%s)\n",
859 error_message(retval
)));
863 if ((retval
= krb5_set_default_tgs_ktypes(context
, enc_types
))) {
864 DEBUG(1,("cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s)\n",
865 error_message(retval
)));
869 if ((retval
= ads_krb5_mk_req(context
,
871 AP_OPTS_USE_SUBKEY
| (krb5_flags
)extra_ap_opts
,
875 impersonate_princ_s
))) {
879 get_krb5_smb_session_key(context
, auth_context
, session_key_krb5
, False
);
881 *ticket
= data_blob(packet
.data
, packet
.length
);
883 kerberos_free_data_contents(context
, &packet
);
889 krb5_cc_close(context
, ccdef
);
891 krb5_auth_con_free(context
, auth_context
);
892 krb5_free_context(context
);
898 bool get_krb5_smb_session_key(krb5_context context
, krb5_auth_context auth_context
, DATA_BLOB
*session_key
, bool remote
)
900 krb5_keyblock
*skey
= NULL
;
901 krb5_error_code err
= 0;
905 err
= krb5_auth_con_getremotesubkey(context
, auth_context
, &skey
);
907 err
= krb5_auth_con_getlocalsubkey(context
, auth_context
, &skey
);
910 if (err
|| skey
== NULL
) {
911 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
915 DEBUG(10, ("Got KRB5 session key of length %d\n", (int)KRB5_KEY_LENGTH(skey
)));
916 *session_key
= data_blob(KRB5_KEY_DATA(skey
), KRB5_KEY_LENGTH(skey
));
917 dump_data_pw("KRB5 Session Key:\n", session_key
->data
, session_key
->length
);
923 krb5_free_keyblock(context
, skey
);
930 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
931 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
);
933 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
)
935 static krb5_data kdata
;
937 kdata
.data
= (char *)krb5_principal_get_comp_string(context
, principal
, i
);
938 kdata
.length
= strlen((const char *)kdata
.data
);
943 krb5_error_code
smb_krb5_kt_free_entry(krb5_context context
, krb5_keytab_entry
*kt_entry
)
945 /* Try krb5_free_keytab_entry_contents first, since
946 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
947 * krb5_kt_free_entry but only has a prototype for the first, while the
948 * second is considered private.
950 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
951 return krb5_free_keytab_entry_contents(context
, kt_entry
);
952 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
953 return krb5_kt_free_entry(context
, kt_entry
);
955 #error UNKNOWN_KT_FREE_FUNCTION
959 void smb_krb5_checksum_from_pac_sig(krb5_checksum
*cksum
,
960 struct PAC_SIGNATURE_DATA
*sig
)
962 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
963 cksum
->cksumtype
= (krb5_cksumtype
)sig
->type
;
964 cksum
->checksum
.length
= sig
->signature
.length
;
965 cksum
->checksum
.data
= sig
->signature
.data
;
967 cksum
->checksum_type
= (krb5_cksumtype
)sig
->type
;
968 cksum
->length
= sig
->signature
.length
;
969 cksum
->contents
= sig
->signature
.data
;
973 krb5_error_code
smb_krb5_verify_checksum(krb5_context context
,
974 const krb5_keyblock
*keyblock
,
976 krb5_checksum
*cksum
,
982 /* verify the checksum */
984 /* welcome to the wonderful world of samba's kerberos abstraction layer:
986 * function heimdal 0.6.1rc3 heimdal 0.7 MIT krb 1.4.2
987 * -----------------------------------------------------------------------------
988 * krb5_c_verify_checksum - works works
989 * krb5_verify_checksum works (6 args) works (6 args) broken (7 args)
992 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
994 krb5_boolean checksum_valid
= False
;
997 input
.data
= (char *)data
;
998 input
.length
= length
;
1000 ret
= krb5_c_verify_checksum(context
,
1007 DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n",
1008 error_message(ret
)));
1012 if (!checksum_valid
)
1013 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
1016 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
1018 /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
1019 * without enctype and it ignores any key_usage types - Guenther */
1024 ret
= krb5_crypto_init(context
,
1029 DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n",
1030 error_message(ret
)));
1034 ret
= krb5_verify_checksum(context
,
1041 krb5_crypto_destroy(context
, crypto
);
1045 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
1051 time_t get_authtime_from_tkt(krb5_ticket
*tkt
)
1053 #if defined(HAVE_KRB5_TKT_ENC_PART2)
1054 return tkt
->enc_part2
->times
.authtime
;
1056 return tkt
->ticket
.authtime
;
1060 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1061 static int get_kvno_from_ap_req(krb5_ap_req
*ap_req
)
1063 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
1064 if (ap_req
->ticket
->enc_part
.kvno
)
1065 return ap_req
->ticket
->enc_part
.kvno
;
1067 if (ap_req
->ticket
.enc_part
.kvno
)
1068 return *ap_req
->ticket
.enc_part
.kvno
;
1073 static krb5_enctype
get_enctype_from_ap_req(krb5_ap_req
*ap_req
)
1075 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
1076 return ap_req
->ticket
.enc_part
.etype
;
1078 return ap_req
->ticket
->enc_part
.enctype
;
1081 #endif /* HAVE_KRB5_DECODE_AP_REQ */
1083 static krb5_error_code
1084 get_key_from_keytab(krb5_context context
,
1085 krb5_const_principal server
,
1086 krb5_enctype enctype
,
1088 krb5_keyblock
**out_key
)
1090 krb5_keytab_entry entry
;
1091 krb5_error_code ret
;
1094 krb5_keyblock
*keyp
;
1096 /* We have to open a new keytab handle here, as MIT does
1097 an implicit open/getnext/close on krb5_kt_get_entry. We
1098 may be in the middle of a keytab enumeration when this is
1101 ret
= smb_krb5_open_keytab(context
, NULL
, False
, &keytab
);
1103 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret
)));
1107 if ( DEBUGLEVEL
>= 10 ) {
1108 if (smb_krb5_unparse_name(talloc_tos(), context
, server
, &name
) == 0) {
1109 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
1110 kvno
, enctype
, name
));
1115 ret
= krb5_kt_get_entry(context
,
1123 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret
)));
1127 keyp
= KRB5_KT_KEY(&entry
);
1129 ret
= krb5_copy_keyblock(context
, keyp
, out_key
);
1131 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret
)));
1135 smb_krb5_kt_free_entry(context
, &entry
);
1138 krb5_kt_close(context
, keytab
);
1144 krb5_error_code
smb_krb5_get_keyinfo_from_ap_req(krb5_context context
,
1145 const krb5_data
*inbuf
,
1147 krb5_enctype
*enctype
)
1149 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1151 krb5_error_code ret
;
1154 ret
= krb5_decode_ap_req(context
, inbuf
, &ap_req
);
1158 *kvno
= get_kvno_from_ap_req(&ap_req
);
1159 *enctype
= get_enctype_from_ap_req(&ap_req
);
1161 free_AP_REQ(&ap_req
);
1166 /* Possibly not an appropriate error code. */
1167 return KRB5KDC_ERR_BADOPTION
;
1170 krb5_error_code
krb5_rd_req_return_keyblock_from_keytab(krb5_context context
,
1171 krb5_auth_context
*auth_context
,
1172 const krb5_data
*inbuf
,
1173 krb5_const_principal server
,
1175 krb5_flags
*ap_req_options
,
1176 krb5_ticket
**ticket
,
1177 krb5_keyblock
**keyblock
)
1179 krb5_error_code ret
;
1181 krb5_enctype enctype
;
1182 krb5_keyblock
*local_keyblock
;
1184 ret
= krb5_rd_req(context
,
1195 #ifdef KRB5_TICKET_HAS_KEYINFO
1196 enctype
= (*ticket
)->enc_part
.enctype
;
1197 kvno
= (*ticket
)->enc_part
.kvno
;
1199 ret
= smb_krb5_get_keyinfo_from_ap_req(context
, inbuf
, &kvno
, &enctype
);
1205 ret
= get_key_from_keytab(context
,
1211 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1216 if (ret
&& local_keyblock
!= NULL
) {
1217 krb5_free_keyblock(context
, local_keyblock
);
1219 *keyblock
= local_keyblock
;
1225 krb5_error_code
smb_krb5_parse_name_norealm(krb5_context context
,
1227 krb5_principal
*principal
)
1229 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1230 return smb_krb5_parse_name_norealm_conv(context
, name
, principal
);
1233 /* we are cheating here because parse_name will in fact set the realm.
1234 * We don't care as the only caller of smb_krb5_parse_name_norealm
1235 * ignores the realm anyway when calling
1236 * smb_krb5_principal_compare_any_realm later - Guenther */
1238 return smb_krb5_parse_name(context
, name
, principal
);
1241 bool smb_krb5_principal_compare_any_realm(krb5_context context
,
1242 krb5_const_principal princ1
,
1243 krb5_const_principal princ2
)
1245 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1247 return krb5_principal_compare_any_realm(context
, princ1
, princ2
);
1249 /* krb5_princ_size is a macro in MIT */
1250 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1253 const krb5_data
*p1
, *p2
;
1255 len1
= krb5_princ_size(context
, princ1
);
1256 len2
= krb5_princ_size(context
, princ2
);
1261 for (i
= 0; i
< len1
; i
++) {
1263 p1
= krb5_princ_component(context
, CONST_DISCARD(krb5_principal
, princ1
), i
);
1264 p2
= krb5_princ_component(context
, CONST_DISCARD(krb5_principal
, princ2
), i
);
1266 if (p1
->length
!= p2
->length
|| memcmp(p1
->data
, p2
->data
, p1
->length
))
1272 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1276 krb5_error_code
smb_krb5_renew_ticket(const char *ccache_string
, /* FILE:/tmp/krb5cc_0 */
1277 const char *client_string
, /* gd@BER.SUSE.DE */
1278 const char *service_string
, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1279 time_t *expire_time
)
1281 krb5_error_code ret
;
1282 krb5_context context
= NULL
;
1283 krb5_ccache ccache
= NULL
;
1284 krb5_principal client
= NULL
;
1285 krb5_creds creds
, creds_in
, *creds_out
= NULL
;
1288 ZERO_STRUCT(creds_in
);
1290 initialize_krb5_error_table();
1291 ret
= krb5_init_context(&context
);
1296 if (!ccache_string
) {
1297 ccache_string
= krb5_cc_default_name(context
);
1300 if (!ccache_string
) {
1305 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string
));
1307 /* FIXME: we should not fall back to defaults */
1308 ret
= krb5_cc_resolve(context
, CONST_DISCARD(char *, ccache_string
), &ccache
);
1313 if (client_string
) {
1314 ret
= smb_krb5_parse_name(context
, client_string
, &client
);
1319 ret
= krb5_cc_get_principal(context
, ccache
, &client
);
1325 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1327 ret
= krb5_get_renewed_creds(context
, &creds
, client
, ccache
, CONST_DISCARD(char *, service_string
));
1329 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1333 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1335 krb5_kdc_flags flags
;
1336 krb5_realm
*client_realm
= NULL
;
1338 ret
= krb5_copy_principal(context
, client
, &creds_in
.client
);
1343 if (service_string
) {
1344 ret
= smb_krb5_parse_name(context
, service_string
, &creds_in
.server
);
1349 /* build tgt service by default */
1350 client_realm
= krb5_princ_realm(context
, creds_in
.client
);
1351 if (!client_realm
) {
1355 ret
= krb5_make_principal(context
, &creds_in
.server
, *client_realm
, KRB5_TGS_NAME
, *client_realm
, NULL
);
1362 flags
.b
.renewable
= flags
.b
.renew
= True
;
1364 ret
= krb5_get_kdc_cred(context
, ccache
, flags
, NULL
, NULL
, &creds_in
, &creds_out
);
1366 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1373 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1376 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1377 ret
= krb5_cc_initialize(context
, ccache
, client
);
1382 ret
= krb5_cc_store_cred(context
, ccache
, &creds
);
1385 *expire_time
= (time_t) creds
.times
.endtime
;
1389 krb5_free_cred_contents(context
, &creds_in
);
1392 krb5_free_creds(context
, creds_out
);
1394 krb5_free_cred_contents(context
, &creds
);
1398 krb5_free_principal(context
, client
);
1401 krb5_cc_close(context
, ccache
);
1404 krb5_free_context(context
);
1410 krb5_error_code
smb_krb5_free_addresses(krb5_context context
, smb_krb5_addresses
*addr
)
1412 krb5_error_code ret
= 0;
1416 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1417 krb5_free_addresses(context
, addr
->addrs
);
1418 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1419 ret
= krb5_free_addresses(context
, addr
->addrs
);
1420 SAFE_FREE(addr
->addrs
);
1427 krb5_error_code
smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses
**kerb_addr
)
1429 krb5_error_code ret
= 0;
1431 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1432 krb5_address
**addrs
= NULL
;
1433 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1434 krb5_addresses
*addrs
= NULL
;
1437 *kerb_addr
= (smb_krb5_addresses
*)SMB_MALLOC(sizeof(smb_krb5_addresses
));
1438 if (*kerb_addr
== NULL
) {
1442 put_name(buf
, global_myname(), ' ', 0x20);
1444 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1448 addrs
= (krb5_address
**)SMB_MALLOC(sizeof(krb5_address
*) * num_addr
);
1449 if (addrs
== NULL
) {
1450 SAFE_FREE(*kerb_addr
);
1454 memset(addrs
, 0, sizeof(krb5_address
*) * num_addr
);
1456 addrs
[0] = (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1457 if (addrs
[0] == NULL
) {
1459 SAFE_FREE(*kerb_addr
);
1463 addrs
[0]->magic
= KV5M_ADDRESS
;
1464 addrs
[0]->addrtype
= KRB5_ADDR_NETBIOS
;
1465 addrs
[0]->length
= MAX_NETBIOSNAME_LEN
;
1466 addrs
[0]->contents
= (unsigned char *)SMB_MALLOC(addrs
[0]->length
);
1467 if (addrs
[0]->contents
== NULL
) {
1468 SAFE_FREE(addrs
[0]);
1470 SAFE_FREE(*kerb_addr
);
1474 memcpy(addrs
[0]->contents
, buf
, addrs
[0]->length
);
1478 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1480 addrs
= (krb5_addresses
*)SMB_MALLOC(sizeof(krb5_addresses
));
1481 if (addrs
== NULL
) {
1482 SAFE_FREE(*kerb_addr
);
1486 memset(addrs
, 0, sizeof(krb5_addresses
));
1489 addrs
->val
= (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1490 if (addrs
->val
== NULL
) {
1492 SAFE_FREE(kerb_addr
);
1496 addrs
->val
[0].addr_type
= KRB5_ADDR_NETBIOS
;
1497 addrs
->val
[0].address
.length
= MAX_NETBIOSNAME_LEN
;
1498 addrs
->val
[0].address
.data
= (unsigned char *)SMB_MALLOC(addrs
->val
[0].address
.length
);
1499 if (addrs
->val
[0].address
.data
== NULL
) {
1500 SAFE_FREE(addrs
->val
);
1502 SAFE_FREE(*kerb_addr
);
1506 memcpy(addrs
->val
[0].address
.data
, buf
, addrs
->val
[0].address
.length
);
1509 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1511 (*kerb_addr
)->addrs
= addrs
;
1516 void smb_krb5_free_error(krb5_context context
, krb5_error
*krberror
)
1518 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1519 krb5_free_error_contents(context
, krberror
);
1521 krb5_free_error(context
, krberror
);
1525 krb5_error_code
handle_krberror_packet(krb5_context context
,
1528 krb5_error_code ret
;
1529 bool got_error_code
= False
;
1531 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1533 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1535 krb5_error krberror
;
1537 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1538 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1539 error_message(ret
)));
1543 if (krberror
.e_data
== NULL
|| krberror
.e_data
->data
== NULL
) {
1544 ret
= (krb5_error_code
) krberror
.error_code
;
1545 got_error_code
= True
;
1548 smb_krb5_free_error(context
, &krberror
);
1552 krb5_error
*krberror
;
1554 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1555 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1556 error_message(ret
)));
1560 if (krberror
->e_data
.data
== NULL
) {
1561 #if defined(ERROR_TABLE_BASE_krb5)
1562 ret
= ERROR_TABLE_BASE_krb5
+ (krb5_error_code
) krberror
->error
;
1564 ret
= (krb5_error_code
)krberror
->error
;
1566 got_error_code
= True
;
1568 smb_krb5_free_error(context
, krberror
);
1571 if (got_error_code
) {
1572 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1573 error_message(ret
), ret
));
1578 krb5_error_code
smb_krb5_get_init_creds_opt_alloc(krb5_context context
,
1579 krb5_get_init_creds_opt
**opt
)
1581 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1582 /* Heimdal or modern MIT version */
1583 return krb5_get_init_creds_opt_alloc(context
, opt
);
1585 /* Historical MIT version */
1586 krb5_get_init_creds_opt
*my_opt
;
1590 if ((my_opt
= SMB_MALLOC_P(krb5_get_init_creds_opt
)) == NULL
) {
1594 krb5_get_init_creds_opt_init(my_opt
);
1598 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1601 void smb_krb5_get_init_creds_opt_free(krb5_context context
,
1602 krb5_get_init_creds_opt
*opt
)
1604 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1606 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1607 /* Modern MIT or Heimdal version */
1608 krb5_get_init_creds_opt_free(context
, opt
);
1610 /* Heimdal version */
1611 krb5_get_init_creds_opt_free(opt
);
1612 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1614 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1615 /* Historical MIT version */
1618 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1621 krb5_enctype
smb_get_enctype_from_kt_entry(krb5_keytab_entry
*kt_entry
)
1623 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry
));
1627 /* caller needs to free etype_s */
1628 krb5_error_code
smb_krb5_enctype_to_string(krb5_context context
,
1629 krb5_enctype enctype
,
1632 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1633 return krb5_enctype_to_string(context
, enctype
, etype_s
); /* Heimdal */
1634 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1636 krb5_error_code ret
= krb5_enctype_to_string(enctype
, buf
, 256); /* MIT */
1640 *etype_s
= SMB_STRDUP(buf
);
1646 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1650 krb5_error_code
smb_krb5_mk_error(krb5_context context
,
1651 krb5_error_code error_code
,
1652 const krb5_principal server
,
1655 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1657 * The MIT interface is *terrible*.
1658 * We have to construct this ourselves...
1662 memset(&e
, 0, sizeof(e
));
1663 krb5_us_timeofday(context
, &e
.stime
, &e
.susec
);
1665 #if defined(krb5_err_base)
1666 e
.error
= error_code
- krb5_err_base
;
1667 #elif defined(ERROR_TABLE_BASE_krb5)
1668 e
.error
= error_code
- ERROR_TABLE_BASE_krb5
;
1670 e
.error
= error_code
; /* Almost certainly wrong, but what can we do... ? */
1673 return krb5_mk_error(context
, &e
, reply
);
1674 #else /* Heimdal. */
1675 return krb5_mk_error(context
,
1687 /**********************************************************************
1688 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1689 * allows to process non-default keytab names.
1690 * @param context krb5_context
1691 * @param keytab_name_req string
1692 * @param write_access bool if writable keytab is required
1693 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1694 * @return krb5_error_code
1695 **********************************************************************/
1697 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1698 #ifndef MAX_KEYTAB_NAME_LEN
1699 #define MAX_KEYTAB_NAME_LEN 1100
1702 krb5_error_code
smb_krb5_open_keytab(krb5_context context
,
1703 const char *keytab_name_req
,
1705 krb5_keytab
*keytab
)
1707 krb5_error_code ret
= 0;
1708 TALLOC_CTX
*mem_ctx
;
1709 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1710 char *kt_str
= NULL
;
1711 bool found_valid_name
= False
;
1712 const char *pragma
= "FILE";
1713 const char *tmp
= NULL
;
1715 if (!write_access
&& !keytab_name_req
) {
1716 /* caller just wants to read the default keytab readonly, so be it */
1717 return krb5_kt_default(context
, keytab
);
1720 mem_ctx
= talloc_init("smb_krb5_open_keytab");
1725 #ifdef HAVE_WRFILE_KEYTAB
1731 if (keytab_name_req
) {
1733 if (strlen(keytab_name_req
) > MAX_KEYTAB_NAME_LEN
) {
1734 ret
= KRB5_CONFIG_NOTENUFSPACE
;
1738 if ((strncmp(keytab_name_req
, "WRFILE:/", 8) == 0) ||
1739 (strncmp(keytab_name_req
, "FILE:/", 6) == 0)) {
1740 tmp
= keytab_name_req
;
1744 if (keytab_name_req
[0] != '/') {
1745 ret
= KRB5_KT_BADNAME
;
1749 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, keytab_name_req
);
1758 /* we need to handle more complex keytab_strings, like:
1759 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1761 ret
= krb5_kt_default_name(context
, &keytab_string
[0], MAX_KEYTAB_NAME_LEN
- 2);
1766 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string
));
1768 tmp
= talloc_strdup(mem_ctx
, keytab_string
);
1774 if (strncmp(tmp
, "ANY:", 4) == 0) {
1778 memset(&keytab_string
, '\0', sizeof(keytab_string
));
1780 while (next_token_talloc(mem_ctx
, &tmp
, &kt_str
, ",")) {
1781 if (strncmp(kt_str
, "WRFILE:", 7) == 0) {
1782 found_valid_name
= True
;
1787 if (strncmp(kt_str
, "FILE:", 5) == 0) {
1788 found_valid_name
= True
;
1793 if (tmp
[0] == '/') {
1794 /* Treat as a FILE: keytab definition. */
1795 found_valid_name
= true;
1798 if (found_valid_name
) {
1799 if (tmp
[0] != '/') {
1800 ret
= KRB5_KT_BADNAME
;
1804 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, tmp
);
1813 if (!found_valid_name
) {
1814 ret
= KRB5_KT_UNKNOWN_TYPE
;
1819 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp
));
1820 ret
= krb5_kt_resolve(context
, tmp
, keytab
);
1823 TALLOC_FREE(mem_ctx
);
1827 krb5_error_code
smb_krb5_keytab_name(TALLOC_CTX
*mem_ctx
,
1828 krb5_context context
,
1830 const char **keytab_name
)
1832 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1833 krb5_error_code ret
= 0;
1835 ret
= krb5_kt_get_name(context
, keytab
,
1836 keytab_string
, MAX_KEYTAB_NAME_LEN
- 2);
1841 *keytab_name
= talloc_strdup(mem_ctx
, keytab_string
);
1842 if (!*keytab_name
) {
1849 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY)
1850 /**************************************************************
1851 Routine: ads_krb5_get_fwd_ticket
1853 When a service ticket is flagged as trusted
1854 for delegation we should provide a forwardable
1855 ticket so that the remote host can act on our
1856 behalf. This is done by taking the 2nd forwardable
1857 TGT and storing it in the GSS-API authenticator
1858 "checksum". This routine will populate
1859 the krb5_data authenticator with this TGT.
1861 krb5_context context: The kerberos context for this authentication.
1862 krb5_auth_context: The authentication context.
1863 krb5_creds *credsp: The ticket credentials (AS-REP).
1864 krb5_ccache ccache: The credentials cache.
1865 krb5_data &authenticator: The checksum field that will store the TGT, and
1866 authenticator.data must be freed by the caller.
1869 krb5_error_code: 0 if no errors, otherwise set.
1870 **************************************************************/
1872 static krb5_error_code
ads_krb5_get_fwd_ticket( krb5_context context
,
1873 krb5_auth_context
*auth_context
,
1876 krb5_data
*authenticator
)
1879 krb5_error_code retval
= 0;
1880 char *pChksum
= NULL
;
1883 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
1884 but still has the symbol */
1885 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
1886 krb5_error_code
krb5_auth_con_set_req_cksumtype(
1887 krb5_context context
,
1888 krb5_auth_context auth_context
,
1889 krb5_cksumtype cksumtype
);
1892 ZERO_STRUCT(fwdData
);
1893 ZERO_STRUCTP(authenticator
);
1895 retval
= krb5_fwd_tgt_creds(context
,/* Krb5 context [in] */
1896 *auth_context
, /* Authentication context [in] */
1897 CONST_DISCARD(char *, KRB5_TGS_NAME
), /* Ticket service name ("krbtgt") [in] */
1898 credsp
->client
, /* Client principal for the tgt [in] */
1899 credsp
->server
, /* Server principal for the tgt [in] */
1900 ccache
, /* Credential cache to use for storage [in] */
1901 1, /* Turn on for "Forwardable ticket" [in] */
1902 &fwdData
); /* Resulting response [out] */
1906 DEBUG(1,("ads_krb5_get_fwd_ticket: krb5_fwd_tgt_creds failed (%s)\n",
1907 error_message(retval
)));
1911 if ((unsigned int)GSSAPI_CHECKSUM_SIZE
+ (unsigned int)fwdData
.length
<
1912 (unsigned int)GSSAPI_CHECKSUM_SIZE
) {
1917 /* We're going to allocate a gssChecksum structure with a little
1918 extra data the length of the kerberos credentials length
1919 (APPLICATION 22) so that we can pack it on the end of the structure.
1922 pChksum
= (char *)SMB_MALLOC(GSSAPI_CHECKSUM_SIZE
+ fwdData
.length
);
1930 SIVAL(p
, 0, GSSAPI_BNDLENGTH
);
1933 /* Zero out the bindings fields */
1934 memset(p
, '\0', GSSAPI_BNDLENGTH
);
1935 p
+= GSSAPI_BNDLENGTH
;
1937 SIVAL(p
, 0, GSS_C_DELEG_FLAG
);
1941 SSVAL(p
, 0, fwdData
.length
);
1944 /* Migrate the kerberos KRB_CRED data to the checksum delegation */
1945 memcpy(p
, fwdData
.data
, fwdData
.length
);
1946 p
+= fwdData
.length
;
1948 /* We need to do this in order to allow our GSS-API */
1949 retval
= krb5_auth_con_set_req_cksumtype( context
, *auth_context
, GSSAPI_CHECKSUM
);
1954 /* We now have a service ticket, now turn it into an AP-REQ. */
1955 authenticator
->length
= fwdData
.length
+ GSSAPI_CHECKSUM_SIZE
;
1957 /* Caller should call free() when they're done with this. */
1958 authenticator
->data
= (char *)pChksum
;
1962 /* Remove that input data, we never needed it anyway. */
1963 if (fwdData
.length
> 0) {
1964 krb5_free_data_contents( context
, &fwdData
);
1971 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1972 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1973 defined(HAVE_KRB5_GET_CREDS)
1974 static krb5_error_code
smb_krb5_get_credentials_for_user_opt(krb5_context context
,
1977 krb5_principal server
,
1978 krb5_principal impersonate_princ
,
1979 krb5_creds
**out_creds
)
1981 krb5_error_code ret
;
1982 krb5_get_creds_opt opt
;
1984 ret
= krb5_get_creds_opt_alloc(context
, &opt
);
1988 krb5_get_creds_opt_add_options(context
, opt
, KRB5_GC_FORWARDABLE
);
1990 if (impersonate_princ
) {
1991 ret
= krb5_get_creds_opt_set_impersonate(context
, opt
,
1998 ret
= krb5_get_creds(context
, opt
, ccache
, server
, out_creds
);
2005 krb5_get_creds_opt_free(context
, opt
);
2009 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
2011 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
2012 static krb5_error_code
smb_krb5_get_credentials_for_user(krb5_context context
,
2015 krb5_principal server
,
2016 krb5_principal impersonate_princ
,
2017 krb5_creds
**out_creds
)
2019 krb5_error_code ret
;
2020 krb5_creds in_creds
;
2022 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
2023 krb5_error_code KRB5_CALLCONV
2024 krb5_get_credentials_for_user(krb5_context context
, krb5_flags options
,
2025 krb5_ccache ccache
, krb5_creds
*in_creds
,
2026 krb5_data
*subject_cert
,
2027 krb5_creds
**out_creds
);
2028 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
2030 ZERO_STRUCT(in_creds
);
2032 if (impersonate_princ
) {
2034 in_creds
.server
= me
;
2035 in_creds
.client
= impersonate_princ
;
2037 ret
= krb5_get_credentials_for_user(context
,
2038 0, /* krb5_flags options */
2041 NULL
, /* krb5_data *subject_cert */
2044 in_creds
.client
= me
;
2045 in_creds
.server
= server
;
2047 ret
= krb5_get_credentials(context
, 0, ccache
,
2048 &in_creds
, out_creds
);
2053 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
2056 * smb_krb5_get_credentials
2058 * @brief Get krb5 credentials for a server
2060 * @param[in] context An initialized krb5_context
2061 * @param[in] ccache An initialized krb5_ccache
2062 * @param[in] me The krb5_principal of the caller
2063 * @param[in] server The krb5_principal of the requested service
2064 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
2065 * @param[out] out_creds The returned krb5_creds structure
2066 * @return krb5_error_code
2069 krb5_error_code
smb_krb5_get_credentials(krb5_context context
,
2072 krb5_principal server
,
2073 krb5_principal impersonate_princ
,
2074 krb5_creds
**out_creds
)
2076 krb5_error_code ret
;
2077 krb5_creds
*creds
= NULL
;
2081 if (impersonate_princ
) {
2082 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
2083 ret
= smb_krb5_get_credentials_for_user_opt(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
2084 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
2085 ret
= smb_krb5_get_credentials_for_user(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
2090 krb5_creds in_creds
;
2092 ZERO_STRUCT(in_creds
);
2094 in_creds
.client
= me
;
2095 in_creds
.server
= server
;
2097 ret
= krb5_get_credentials(context
, 0, ccache
,
2104 ret
= krb5_cc_store_cred(context
, ccache
, creds
);
2115 krb5_free_creds(context
, creds
);
2122 * smb_krb5_get_creds
2124 * @brief Get krb5 credentials for a server
2126 * @param[in] server_s The string name of the service
2127 * @param[in] time_offset The offset to the KDCs time in seconds (optional)
2128 * @param[in] cc The krb5 credential cache string name (optional)
2129 * @param[in] impersonate_princ_s The string principal name to impersonate (optional)
2130 * @param[out] creds_p The returned krb5_creds structure
2131 * @return krb5_error_code
2134 krb5_error_code
smb_krb5_get_creds(const char *server_s
,
2137 const char *impersonate_princ_s
,
2138 krb5_creds
**creds_p
)
2140 krb5_error_code ret
;
2141 krb5_context context
= NULL
;
2142 krb5_principal me
= NULL
;
2143 krb5_principal server
= NULL
;
2144 krb5_principal impersonate_princ
= NULL
;
2145 krb5_creds
*creds
= NULL
;
2146 krb5_ccache ccache
= NULL
;
2150 initialize_krb5_error_table();
2151 ret
= krb5_init_context(&context
);
2156 if (time_offset
!= 0) {
2157 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
2160 ret
= krb5_cc_resolve(context
, cc
? cc
:
2161 krb5_cc_default_name(context
), &ccache
);
2166 ret
= krb5_cc_get_principal(context
, ccache
, &me
);
2171 ret
= smb_krb5_parse_name(context
, server_s
, &server
);
2176 if (impersonate_princ_s
) {
2177 ret
= smb_krb5_parse_name(context
, impersonate_princ_s
,
2178 &impersonate_princ
);
2184 ret
= smb_krb5_get_credentials(context
, ccache
,
2185 me
, server
, impersonate_princ
,
2191 ret
= krb5_cc_store_cred(context
, ccache
, creds
);
2200 DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n",
2203 if (impersonate_princ_s
) {
2204 char *client
= NULL
;
2206 ret
= smb_krb5_unparse_name(talloc_tos(), context
, creds
->client
, &client
);
2210 DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n",
2212 TALLOC_FREE(client
);
2221 krb5_free_creds(context
, creds
);
2224 krb5_free_principal(context
, server
);
2227 krb5_free_principal(context
, me
);
2229 if (impersonate_princ
) {
2230 krb5_free_principal(context
, impersonate_princ
);
2233 krb5_cc_close(context
, ccache
);
2235 krb5_free_context(context
);
2241 * smb_krb5_principal_get_realm
2243 * @brief Get realm of a principal
2245 * @param[in] context The krb5_context
2246 * @param[in] principal The principal
2247 * @return pointer to the realm
2251 char *smb_krb5_principal_get_realm(krb5_context context
,
2252 krb5_principal principal
)
2254 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2255 return krb5_principal_get_realm(context
, principal
);
2256 #elif defined(krb5_princ_realm) /* MIT */
2258 realm
= krb5_princ_realm(context
, principal
);
2259 return (char *)realm
->data
;
2265 #else /* HAVE_KRB5 */
2266 /* this saves a few linking headaches */
2267 int cli_krb5_get_ticket(const char *principal
, time_t time_offset
,
2268 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
, uint32 extra_ap_opts
,
2269 const char *ccname
, time_t *tgs_expire
,
2270 const char *impersonate_princ_s
)
2272 DEBUG(0,("NO KERBEROS SUPPORT\n"));