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-2007
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/>.
23 #define KRB5_PRIVATE 1 /* this file uses PRIVATE interfaces! */
24 #define KRB5_DEPRECATED 1 /* this file uses DEPRECATED interfaces! */
30 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
31 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
32 #define GSSAPI_CHECKSUM_SIZE (12+GSSAPI_BNDLENGTH)
34 #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)
35 static krb5_error_code
ads_krb5_get_fwd_ticket( krb5_context context
,
36 krb5_auth_context
*auth_context
,
39 krb5_data
*authenticator
);
42 /**************************************************************
43 Wrappers around kerberos string functions that convert from
44 utf8 -> unix charset and vica versa.
45 **************************************************************/
47 /**************************************************************
48 krb5_parse_name that takes a UNIX charset.
49 **************************************************************/
51 krb5_error_code
smb_krb5_parse_name(krb5_context context
,
52 const char *name
, /* in unix charset */
53 krb5_principal
*principal
)
57 size_t converted_size
;
59 if (!push_utf8_allocate(&utf8_name
, name
, &converted_size
)) {
63 ret
= krb5_parse_name(context
, utf8_name
, principal
);
68 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
69 /**************************************************************
70 krb5_parse_name_norealm that takes a UNIX charset.
71 **************************************************************/
73 static krb5_error_code
smb_krb5_parse_name_norealm_conv(krb5_context context
,
74 const char *name
, /* in unix charset */
75 krb5_principal
*principal
)
79 size_t converted_size
;
82 if (!push_utf8_allocate(&utf8_name
, name
, &converted_size
)) {
86 ret
= krb5_parse_name_norealm(context
, utf8_name
, principal
);
92 /**************************************************************
93 krb5_parse_name that returns a UNIX charset name. Must
94 be freed with normal free() call.
95 **************************************************************/
97 krb5_error_code
smb_krb5_unparse_name(krb5_context context
,
98 krb5_const_principal principal
,
103 size_t converted_size
;
106 ret
= krb5_unparse_name(context
, principal
, &utf8_name
);
111 if (!pull_utf8_allocate(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
, NULL
, &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
, NULL
, &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
,
659 krb5_error_code retval
;
660 krb5_principal server
;
664 bool creds_ready
= False
;
665 int i
= 0, maxtries
= 3;
667 ZERO_STRUCT(in_data
);
669 retval
= smb_krb5_parse_name(context
, principal
, &server
);
671 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal
));
675 /* obtain ticket & session key */
677 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
678 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
679 error_message(retval
)));
683 if ((retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
))) {
684 /* This can commonly fail on smbd startup with no ticket in the cache.
685 * Report at higher level than 1. */
686 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
687 error_message(retval
)));
691 while (!creds_ready
&& (i
< maxtries
)) {
693 if ((retval
= krb5_get_credentials(context
, 0, ccache
,
695 DEBUG(1,("ads_krb5_mk_req: krb5_get_credentials failed for %s (%s)\n",
696 principal
, error_message(retval
)));
700 /* cope with ticket being in the future due to clock skew */
701 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
702 time_t t
= time(NULL
);
703 int time_offset
=(int)((unsigned)credsp
->times
.starttime
-t
);
704 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
705 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
708 if (!ads_cleanup_expired_creds(context
, ccache
, credsp
)) {
715 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
716 principal
, krb5_cc_get_type(context
, ccache
), krb5_cc_get_name(context
, ccache
),
717 http_timestring(talloc_tos(), (unsigned)credsp
->times
.endtime
),
718 (unsigned)credsp
->times
.endtime
));
721 *expire_time
= (time_t)credsp
->times
.endtime
;
724 #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)
725 if( credsp
->ticket_flags
& TKT_FLG_OK_AS_DELEGATE
) {
726 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
727 as part of the kerberos exchange. */
729 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
731 if( *auth_context
== NULL
) {
732 /* Allocate if it has not yet been allocated. */
733 retval
= krb5_auth_con_init( context
, auth_context
);
735 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_init failed (%s)\n",
736 error_message(retval
)));
741 retval
= krb5_auth_con_setuseruserkey( context
, *auth_context
, &credsp
->keyblock
);
743 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_setuseruserkey failed (%s)\n",
744 error_message(retval
)));
748 /* Must use a subkey for forwarded tickets. */
749 retval
= krb5_auth_con_setflags( context
, *auth_context
, KRB5_AUTH_CONTEXT_USE_SUBKEY
);
751 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_setflags failed (%s)\n",
752 error_message(retval
)));
756 retval
= ads_krb5_get_fwd_ticket( context
,
762 DEBUG( 3, ("ads_krb5_get_fwd_ticket failed (%s)\n",
763 error_message( retval
) ) );
766 * This is not fatal. Delete the *auth_context and continue
767 * with krb5_mk_req_extended to get a non-forwardable ticket.
771 free( in_data
.data
);
775 krb5_auth_con_free(context
, *auth_context
);
776 *auth_context
= NULL
;
781 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
782 &in_data
, credsp
, outbuf
);
784 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
785 error_message(retval
)));
789 free( in_data
.data
);
793 krb5_free_creds(context
, credsp
);
796 krb5_free_cred_contents(context
, &creds
);
799 krb5_free_principal(context
, server
);
805 get a kerberos5 ticket for the given service
807 int cli_krb5_get_ticket(const char *principal
, time_t time_offset
,
808 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
809 uint32 extra_ap_opts
, const char *ccname
,
813 krb5_error_code retval
;
815 krb5_context context
= NULL
;
816 krb5_ccache ccdef
= NULL
;
817 krb5_auth_context auth_context
= NULL
;
818 krb5_enctype enc_types
[] = {
819 #ifdef ENCTYPE_ARCFOUR_HMAC
820 ENCTYPE_ARCFOUR_HMAC
,
826 initialize_krb5_error_table();
827 retval
= krb5_init_context(&context
);
829 DEBUG(1,("cli_krb5_get_ticket: krb5_init_context failed (%s)\n",
830 error_message(retval
)));
834 if (time_offset
!= 0) {
835 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
838 if ((retval
= krb5_cc_resolve(context
, ccname
?
839 ccname
: krb5_cc_default_name(context
), &ccdef
))) {
840 DEBUG(1,("cli_krb5_get_ticket: krb5_cc_default failed (%s)\n",
841 error_message(retval
)));
845 if ((retval
= krb5_set_default_tgs_ktypes(context
, enc_types
))) {
846 DEBUG(1,("cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s)\n",
847 error_message(retval
)));
851 if ((retval
= ads_krb5_mk_req(context
,
853 AP_OPTS_USE_SUBKEY
| (krb5_flags
)extra_ap_opts
,
860 get_krb5_smb_session_key(context
, auth_context
, session_key_krb5
, False
);
862 *ticket
= data_blob(packet
.data
, packet
.length
);
864 kerberos_free_data_contents(context
, &packet
);
870 krb5_cc_close(context
, ccdef
);
872 krb5_auth_con_free(context
, auth_context
);
873 krb5_free_context(context
);
879 bool get_krb5_smb_session_key(krb5_context context
, krb5_auth_context auth_context
, DATA_BLOB
*session_key
, bool remote
)
886 err
= krb5_auth_con_getremotesubkey(context
, auth_context
, &skey
);
888 err
= krb5_auth_con_getlocalsubkey(context
, auth_context
, &skey
);
889 if (err
== 0 && skey
!= NULL
) {
890 DEBUG(10, ("Got KRB5 session key of length %d\n", (int)KRB5_KEY_LENGTH(skey
)));
891 *session_key
= data_blob(KRB5_KEY_DATA(skey
), KRB5_KEY_LENGTH(skey
));
892 dump_data_pw("KRB5 Session Key:\n", session_key
->data
, session_key
->length
);
896 krb5_free_keyblock(context
, skey
);
898 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
905 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
906 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
);
908 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
)
910 static krb5_data kdata
;
912 kdata
.data
= (char *)krb5_principal_get_comp_string(context
, principal
, i
);
913 kdata
.length
= strlen((const char *)kdata
.data
);
918 krb5_error_code
smb_krb5_kt_free_entry(krb5_context context
, krb5_keytab_entry
*kt_entry
)
920 #if defined(HAVE_KRB5_KT_FREE_ENTRY)
921 return krb5_kt_free_entry(context
, kt_entry
);
922 #elif defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
923 return krb5_free_keytab_entry_contents(context
, kt_entry
);
925 #error UNKNOWN_KT_FREE_FUNCTION
929 void smb_krb5_checksum_from_pac_sig(krb5_checksum
*cksum
,
930 struct PAC_SIGNATURE_DATA
*sig
)
932 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
933 cksum
->cksumtype
= (krb5_cksumtype
)sig
->type
;
934 cksum
->checksum
.length
= sig
->signature
.length
;
935 cksum
->checksum
.data
= sig
->signature
.data
;
937 cksum
->checksum_type
= (krb5_cksumtype
)sig
->type
;
938 cksum
->length
= sig
->signature
.length
;
939 cksum
->contents
= sig
->signature
.data
;
943 krb5_error_code
smb_krb5_verify_checksum(krb5_context context
,
944 const krb5_keyblock
*keyblock
,
946 krb5_checksum
*cksum
,
952 /* verify the checksum */
954 /* welcome to the wonderful world of samba's kerberos abstraction layer:
956 * function heimdal 0.6.1rc3 heimdal 0.7 MIT krb 1.4.2
957 * -----------------------------------------------------------------------------
958 * krb5_c_verify_checksum - works works
959 * krb5_verify_checksum works (6 args) works (6 args) broken (7 args)
962 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
964 krb5_boolean checksum_valid
= False
;
967 input
.data
= (char *)data
;
968 input
.length
= length
;
970 ret
= krb5_c_verify_checksum(context
,
977 DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n",
978 error_message(ret
)));
983 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
986 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
988 /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
989 * without enctype and it ignores any key_usage types - Guenther */
994 ret
= krb5_crypto_init(context
,
999 DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n",
1000 error_message(ret
)));
1004 ret
= krb5_verify_checksum(context
,
1011 krb5_crypto_destroy(context
, crypto
);
1015 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
1021 time_t get_authtime_from_tkt(krb5_ticket
*tkt
)
1023 #if defined(HAVE_KRB5_TKT_ENC_PART2)
1024 return tkt
->enc_part2
->times
.authtime
;
1026 return tkt
->ticket
.authtime
;
1030 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1031 static int get_kvno_from_ap_req(krb5_ap_req
*ap_req
)
1033 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
1034 if (ap_req
->ticket
->enc_part
.kvno
)
1035 return ap_req
->ticket
->enc_part
.kvno
;
1037 if (ap_req
->ticket
.enc_part
.kvno
)
1038 return *ap_req
->ticket
.enc_part
.kvno
;
1043 static krb5_enctype
get_enctype_from_ap_req(krb5_ap_req
*ap_req
)
1045 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
1046 return ap_req
->ticket
.enc_part
.etype
;
1048 return ap_req
->ticket
->enc_part
.enctype
;
1051 #endif /* HAVE_KRB5_DECODE_AP_REQ */
1053 static krb5_error_code
1054 get_key_from_keytab(krb5_context context
,
1055 krb5_const_principal server
,
1056 krb5_enctype enctype
,
1058 krb5_keyblock
**out_key
)
1060 krb5_keytab_entry entry
;
1061 krb5_error_code ret
;
1064 krb5_keyblock
*keyp
;
1066 /* We have to open a new keytab handle here, as MIT does
1067 an implicit open/getnext/close on krb5_kt_get_entry. We
1068 may be in the middle of a keytab enumeration when this is
1071 ret
= smb_krb5_open_keytab(context
, NULL
, False
, &keytab
);
1073 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret
)));
1077 if ( DEBUGLEVEL
>= 10 ) {
1078 if (smb_krb5_unparse_name(context
, server
, &name
) == 0) {
1079 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
1080 kvno
, enctype
, name
));
1085 ret
= krb5_kt_get_entry(context
,
1093 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret
)));
1097 keyp
= KRB5_KT_KEY(&entry
);
1099 ret
= krb5_copy_keyblock(context
, keyp
, out_key
);
1101 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret
)));
1105 smb_krb5_kt_free_entry(context
, &entry
);
1108 krb5_kt_close(context
, keytab
);
1114 krb5_error_code
smb_krb5_get_keyinfo_from_ap_req(krb5_context context
,
1115 const krb5_data
*inbuf
,
1117 krb5_enctype
*enctype
)
1119 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1121 krb5_error_code ret
;
1124 ret
= krb5_decode_ap_req(context
, inbuf
, &ap_req
);
1128 *kvno
= get_kvno_from_ap_req(&ap_req
);
1129 *enctype
= get_enctype_from_ap_req(&ap_req
);
1131 free_AP_REQ(&ap_req
);
1136 /* Possibly not an appropriate error code. */
1137 return KRB5KDC_ERR_BADOPTION
;
1140 krb5_error_code
krb5_rd_req_return_keyblock_from_keytab(krb5_context context
,
1141 krb5_auth_context
*auth_context
,
1142 const krb5_data
*inbuf
,
1143 krb5_const_principal server
,
1145 krb5_flags
*ap_req_options
,
1146 krb5_ticket
**ticket
,
1147 krb5_keyblock
**keyblock
)
1149 krb5_error_code ret
;
1151 krb5_enctype enctype
;
1152 krb5_keyblock
*local_keyblock
;
1154 ret
= krb5_rd_req(context
,
1165 #ifdef KRB5_TICKET_HAS_KEYINFO
1166 enctype
= (*ticket
)->enc_part
.enctype
;
1167 kvno
= (*ticket
)->enc_part
.kvno
;
1169 ret
= smb_krb5_get_keyinfo_from_ap_req(context
, inbuf
, &kvno
, &enctype
);
1175 ret
= get_key_from_keytab(context
,
1181 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1186 if (ret
&& local_keyblock
!= NULL
) {
1187 krb5_free_keyblock(context
, local_keyblock
);
1189 *keyblock
= local_keyblock
;
1195 krb5_error_code
smb_krb5_parse_name_norealm(krb5_context context
,
1197 krb5_principal
*principal
)
1199 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1200 return smb_krb5_parse_name_norealm_conv(context
, name
, principal
);
1203 /* we are cheating here because parse_name will in fact set the realm.
1204 * We don't care as the only caller of smb_krb5_parse_name_norealm
1205 * ignores the realm anyway when calling
1206 * smb_krb5_principal_compare_any_realm later - Guenther */
1208 return smb_krb5_parse_name(context
, name
, principal
);
1211 bool smb_krb5_principal_compare_any_realm(krb5_context context
,
1212 krb5_const_principal princ1
,
1213 krb5_const_principal princ2
)
1215 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1217 return krb5_principal_compare_any_realm(context
, princ1
, princ2
);
1219 /* krb5_princ_size is a macro in MIT */
1220 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1223 const krb5_data
*p1
, *p2
;
1225 len1
= krb5_princ_size(context
, princ1
);
1226 len2
= krb5_princ_size(context
, princ2
);
1231 for (i
= 0; i
< len1
; i
++) {
1233 p1
= krb5_princ_component(context
, CONST_DISCARD(krb5_principal
, princ1
), i
);
1234 p2
= krb5_princ_component(context
, CONST_DISCARD(krb5_principal
, princ2
), i
);
1236 if (p1
->length
!= p2
->length
|| memcmp(p1
->data
, p2
->data
, p1
->length
))
1242 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1246 krb5_error_code
smb_krb5_renew_ticket(const char *ccache_string
, /* FILE:/tmp/krb5cc_0 */
1247 const char *client_string
, /* gd@BER.SUSE.DE */
1248 const char *service_string
, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1249 time_t *expire_time
)
1251 krb5_error_code ret
;
1252 krb5_context context
= NULL
;
1253 krb5_ccache ccache
= NULL
;
1254 krb5_principal client
= NULL
;
1255 krb5_creds creds
, creds_in
, *creds_out
= NULL
;
1258 ZERO_STRUCT(creds_in
);
1260 initialize_krb5_error_table();
1261 ret
= krb5_init_context(&context
);
1266 if (!ccache_string
) {
1267 ccache_string
= krb5_cc_default_name(context
);
1270 if (!ccache_string
) {
1275 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string
));
1277 /* FIXME: we should not fall back to defaults */
1278 ret
= krb5_cc_resolve(context
, CONST_DISCARD(char *, ccache_string
), &ccache
);
1283 if (client_string
) {
1284 ret
= smb_krb5_parse_name(context
, client_string
, &client
);
1289 ret
= krb5_cc_get_principal(context
, ccache
, &client
);
1295 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1297 ret
= krb5_get_renewed_creds(context
, &creds
, client
, ccache
, CONST_DISCARD(char *, service_string
));
1299 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1303 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1305 krb5_kdc_flags flags
;
1306 krb5_realm
*client_realm
= NULL
;
1308 ret
= krb5_copy_principal(context
, client
, &creds_in
.client
);
1313 if (service_string
) {
1314 ret
= smb_krb5_parse_name(context
, service_string
, &creds_in
.server
);
1319 /* build tgt service by default */
1320 client_realm
= krb5_princ_realm(context
, creds_in
.client
);
1321 if (!client_realm
) {
1325 ret
= krb5_make_principal(context
, &creds_in
.server
, *client_realm
, KRB5_TGS_NAME
, *client_realm
, NULL
);
1332 flags
.b
.renewable
= flags
.b
.renew
= True
;
1334 ret
= krb5_get_kdc_cred(context
, ccache
, flags
, NULL
, NULL
, &creds_in
, &creds_out
);
1336 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1343 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1346 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1347 ret
= krb5_cc_initialize(context
, ccache
, client
);
1352 ret
= krb5_cc_store_cred(context
, ccache
, &creds
);
1355 *expire_time
= (time_t) creds
.times
.endtime
;
1359 krb5_free_cred_contents(context
, &creds_in
);
1362 krb5_free_creds(context
, creds_out
);
1364 krb5_free_cred_contents(context
, &creds
);
1368 krb5_free_principal(context
, client
);
1371 krb5_cc_close(context
, ccache
);
1374 krb5_free_context(context
);
1380 krb5_error_code
smb_krb5_free_addresses(krb5_context context
, smb_krb5_addresses
*addr
)
1382 krb5_error_code ret
= 0;
1386 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1387 krb5_free_addresses(context
, addr
->addrs
);
1388 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1389 ret
= krb5_free_addresses(context
, addr
->addrs
);
1390 SAFE_FREE(addr
->addrs
);
1397 krb5_error_code
smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses
**kerb_addr
)
1399 krb5_error_code ret
= 0;
1401 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1402 krb5_address
**addrs
= NULL
;
1403 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1404 krb5_addresses
*addrs
= NULL
;
1407 *kerb_addr
= (smb_krb5_addresses
*)SMB_MALLOC(sizeof(smb_krb5_addresses
));
1408 if (*kerb_addr
== NULL
) {
1412 put_name(buf
, global_myname(), ' ', 0x20);
1414 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1418 addrs
= (krb5_address
**)SMB_MALLOC(sizeof(krb5_address
*) * num_addr
);
1419 if (addrs
== NULL
) {
1420 SAFE_FREE(kerb_addr
);
1424 memset(addrs
, 0, sizeof(krb5_address
*) * num_addr
);
1426 addrs
[0] = (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1427 if (addrs
[0] == NULL
) {
1429 SAFE_FREE(kerb_addr
);
1433 addrs
[0]->magic
= KV5M_ADDRESS
;
1434 addrs
[0]->addrtype
= KRB5_ADDR_NETBIOS
;
1435 addrs
[0]->length
= MAX_NETBIOSNAME_LEN
;
1436 addrs
[0]->contents
= (unsigned char *)SMB_MALLOC(addrs
[0]->length
);
1437 if (addrs
[0]->contents
== NULL
) {
1438 SAFE_FREE(addrs
[0]);
1440 SAFE_FREE(kerb_addr
);
1444 memcpy(addrs
[0]->contents
, buf
, addrs
[0]->length
);
1448 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1450 addrs
= (krb5_addresses
*)SMB_MALLOC(sizeof(krb5_addresses
));
1451 if (addrs
== NULL
) {
1452 SAFE_FREE(kerb_addr
);
1456 memset(addrs
, 0, sizeof(krb5_addresses
));
1459 addrs
->val
= (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1460 if (addrs
->val
== NULL
) {
1462 SAFE_FREE(kerb_addr
);
1466 addrs
->val
[0].addr_type
= KRB5_ADDR_NETBIOS
;
1467 addrs
->val
[0].address
.length
= MAX_NETBIOSNAME_LEN
;
1468 addrs
->val
[0].address
.data
= (unsigned char *)SMB_MALLOC(addrs
->val
[0].address
.length
);
1469 if (addrs
->val
[0].address
.data
== NULL
) {
1470 SAFE_FREE(addrs
->val
);
1472 SAFE_FREE(kerb_addr
);
1476 memcpy(addrs
->val
[0].address
.data
, buf
, addrs
->val
[0].address
.length
);
1479 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1481 (*kerb_addr
)->addrs
= addrs
;
1486 void smb_krb5_free_error(krb5_context context
, krb5_error
*krberror
)
1488 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1489 krb5_free_error_contents(context
, krberror
);
1491 krb5_free_error(context
, krberror
);
1495 krb5_error_code
handle_krberror_packet(krb5_context context
,
1498 krb5_error_code ret
;
1499 bool got_error_code
= False
;
1501 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1503 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1505 krb5_error krberror
;
1507 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1508 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1509 error_message(ret
)));
1513 if (krberror
.e_data
== NULL
|| krberror
.e_data
->data
== NULL
) {
1514 ret
= (krb5_error_code
) krberror
.error_code
;
1515 got_error_code
= True
;
1518 smb_krb5_free_error(context
, &krberror
);
1522 krb5_error
*krberror
;
1524 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1525 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1526 error_message(ret
)));
1530 if (krberror
->e_data
.data
== NULL
) {
1531 ret
= ERROR_TABLE_BASE_krb5
+ (krb5_error_code
) krberror
->error
;
1532 got_error_code
= True
;
1534 smb_krb5_free_error(context
, krberror
);
1537 if (got_error_code
) {
1538 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1539 error_message(ret
), ret
));
1544 krb5_error_code
smb_krb5_get_init_creds_opt_alloc(krb5_context context
,
1545 krb5_get_init_creds_opt
**opt
)
1547 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1548 /* Heimdal or modern MIT version */
1549 return krb5_get_init_creds_opt_alloc(context
, opt
);
1551 /* Historical MIT version */
1552 krb5_get_init_creds_opt
*my_opt
;
1556 if ((my_opt
= SMB_MALLOC_P(krb5_get_init_creds_opt
)) == NULL
) {
1560 krb5_get_init_creds_opt_init(my_opt
);
1564 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1567 void smb_krb5_get_init_creds_opt_free(krb5_context context
,
1568 krb5_get_init_creds_opt
*opt
)
1570 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1572 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1573 /* Modern MIT or Heimdal version */
1574 krb5_get_init_creds_opt_free(context
, opt
);
1576 /* Heimdal version */
1577 krb5_get_init_creds_opt_free(opt
);
1578 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1580 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1581 /* Historical MIT version */
1584 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1587 krb5_enctype
smb_get_enctype_from_kt_entry(krb5_keytab_entry
*kt_entry
)
1589 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry
));
1593 /* caller needs to free etype_s */
1594 krb5_error_code
smb_krb5_enctype_to_string(krb5_context context
,
1595 krb5_enctype enctype
,
1598 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1599 return krb5_enctype_to_string(context
, enctype
, etype_s
); /* Heimdal */
1600 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1602 krb5_error_code ret
= krb5_enctype_to_string(enctype
, buf
, 256); /* MIT */
1606 *etype_s
= SMB_STRDUP(buf
);
1612 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1616 krb5_error_code
smb_krb5_mk_error(krb5_context context
,
1617 krb5_error_code error_code
,
1618 const krb5_principal server
,
1621 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1623 * The MIT interface is *terrible*.
1624 * We have to construct this ourselves...
1628 memset(&e
, 0, sizeof(e
));
1629 krb5_us_timeofday(context
, &e
.stime
, &e
.susec
);
1631 #if defined(krb5_err_base)
1632 e
.error
= error_code
- krb5_err_base
;
1633 #elif defined(ERROR_TABLE_BASE_krb5)
1634 e
.error
= error_code
- ERROR_TABLE_BASE_krb5
;
1636 e
.error
= error_code
; /* Almost certainly wrong, but what can we do... ? */
1639 return krb5_mk_error(context
, &e
, reply
);
1640 #else /* Heimdal. */
1641 return krb5_mk_error(context
,
1653 /**********************************************************************
1654 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1655 * allows to process non-default keytab names.
1656 * @param context krb5_context
1657 * @param keytab_name_req string
1658 * @param write_access bool if writable keytab is required
1659 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1660 * @return krb5_error_code
1661 **********************************************************************/
1663 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1664 #ifndef MAX_KEYTAB_NAME_LEN
1665 #define MAX_KEYTAB_NAME_LEN 1100
1668 krb5_error_code
smb_krb5_open_keytab(krb5_context context
,
1669 const char *keytab_name_req
,
1671 krb5_keytab
*keytab
)
1673 krb5_error_code ret
= 0;
1674 TALLOC_CTX
*mem_ctx
;
1675 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1676 char *kt_str
= NULL
;
1677 bool found_valid_name
= False
;
1678 const char *pragma
= "FILE";
1679 const char *tmp
= NULL
;
1681 if (!write_access
&& !keytab_name_req
) {
1682 /* caller just wants to read the default keytab readonly, so be it */
1683 return krb5_kt_default(context
, keytab
);
1686 mem_ctx
= talloc_init("smb_krb5_open_keytab");
1691 #ifdef HAVE_WRFILE_KEYTAB
1697 if (keytab_name_req
) {
1699 if (strlen(keytab_name_req
) > MAX_KEYTAB_NAME_LEN
) {
1700 ret
= KRB5_CONFIG_NOTENUFSPACE
;
1704 if ((strncmp(keytab_name_req
, "WRFILE:/", 8) == 0) ||
1705 (strncmp(keytab_name_req
, "FILE:/", 6) == 0)) {
1706 tmp
= keytab_name_req
;
1710 if (keytab_name_req
[0] != '/') {
1711 ret
= KRB5_KT_BADNAME
;
1715 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, keytab_name_req
);
1724 /* we need to handle more complex keytab_strings, like:
1725 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1727 ret
= krb5_kt_default_name(context
, &keytab_string
[0], MAX_KEYTAB_NAME_LEN
- 2);
1732 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string
));
1734 tmp
= talloc_strdup(mem_ctx
, keytab_string
);
1740 if (strncmp(tmp
, "ANY:", 4) == 0) {
1744 memset(&keytab_string
, '\0', sizeof(keytab_string
));
1746 while (next_token_talloc(mem_ctx
, &tmp
, &kt_str
, ",")) {
1747 if (strncmp(kt_str
, "WRFILE:", 7) == 0) {
1748 found_valid_name
= True
;
1753 if (strncmp(kt_str
, "FILE:", 5) == 0) {
1754 found_valid_name
= True
;
1759 if (found_valid_name
) {
1760 if (tmp
[0] != '/') {
1761 ret
= KRB5_KT_BADNAME
;
1765 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, tmp
);
1774 if (!found_valid_name
) {
1775 ret
= KRB5_KT_UNKNOWN_TYPE
;
1780 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp
));
1781 ret
= krb5_kt_resolve(context
, tmp
, keytab
);
1784 TALLOC_FREE(mem_ctx
);
1788 krb5_error_code
smb_krb5_keytab_name(TALLOC_CTX
*mem_ctx
,
1789 krb5_context context
,
1791 const char **keytab_name
)
1793 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1794 krb5_error_code ret
= 0;
1796 ret
= krb5_kt_get_name(context
, keytab
,
1797 keytab_string
, MAX_KEYTAB_NAME_LEN
- 2);
1802 *keytab_name
= talloc_strdup(mem_ctx
, keytab_string
);
1803 if (!*keytab_name
) {
1810 #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)
1811 /**************************************************************
1812 Routine: ads_krb5_get_fwd_ticket
1814 When a service ticket is flagged as trusted
1815 for delegation we should provide a forwardable
1816 ticket so that the remote host can act on our
1817 behalf. This is done by taking the 2nd forwardable
1818 TGT and storing it in the GSS-API authenticator
1819 "checksum". This routine will populate
1820 the krb5_data authenticator with this TGT.
1822 krb5_context context: The kerberos context for this authentication.
1823 krb5_auth_context: The authentication context.
1824 krb5_creds *credsp: The ticket credentials (AS-REP).
1825 krb5_ccache ccache: The credentials cache.
1826 krb5_data &authenticator: The checksum field that will store the TGT, and
1827 authenticator.data must be freed by the caller.
1830 krb5_error_code: 0 if no errors, otherwise set.
1831 **************************************************************/
1833 static krb5_error_code
ads_krb5_get_fwd_ticket( krb5_context context
,
1834 krb5_auth_context
*auth_context
,
1837 krb5_data
*authenticator
)
1840 krb5_error_code retval
= 0;
1841 char *pChksum
= NULL
;
1844 ZERO_STRUCT(fwdData
);
1845 ZERO_STRUCTP(authenticator
);
1847 retval
= krb5_fwd_tgt_creds(context
,/* Krb5 context [in] */
1848 *auth_context
, /* Authentication context [in] */
1849 CONST_DISCARD(char *, KRB5_TGS_NAME
), /* Ticket service name ("krbtgt") [in] */
1850 credsp
->client
, /* Client principal for the tgt [in] */
1851 credsp
->server
, /* Server principal for the tgt [in] */
1852 ccache
, /* Credential cache to use for storage [in] */
1853 1, /* Turn on for "Forwardable ticket" [in] */
1854 &fwdData
); /* Resulting response [out] */
1858 DEBUG(1,("ads_krb5_get_fwd_ticket: krb5_fwd_tgt_creds failed (%s)\n",
1859 error_message(retval
)));
1863 if ((unsigned int)GSSAPI_CHECKSUM_SIZE
+ (unsigned int)fwdData
.length
<
1864 (unsigned int)GSSAPI_CHECKSUM_SIZE
) {
1869 /* We're going to allocate a gssChecksum structure with a little
1870 extra data the length of the kerberos credentials length
1871 (APPLICATION 22) so that we can pack it on the end of the structure.
1874 pChksum
= (char *)SMB_MALLOC(GSSAPI_CHECKSUM_SIZE
+ fwdData
.length
);
1882 SIVAL(p
, 0, GSSAPI_BNDLENGTH
);
1885 /* Zero out the bindings fields */
1886 memset(p
, '\0', GSSAPI_BNDLENGTH
);
1887 p
+= GSSAPI_BNDLENGTH
;
1889 SIVAL(p
, 0, GSS_C_DELEG_FLAG
);
1893 SSVAL(p
, 0, fwdData
.length
);
1896 /* Migrate the kerberos KRB_CRED data to the checksum delegation */
1897 memcpy(p
, fwdData
.data
, fwdData
.length
);
1898 p
+= fwdData
.length
;
1900 /* We need to do this in order to allow our GSS-API */
1901 retval
= krb5_auth_con_set_req_cksumtype( context
, *auth_context
, GSSAPI_CHECKSUM
);
1906 /* We now have a service ticket, now turn it into an AP-REQ. */
1907 authenticator
->length
= fwdData
.length
+ GSSAPI_CHECKSUM_SIZE
;
1909 /* Caller should call free() when they're done with this. */
1910 authenticator
->data
= (char *)pChksum
;
1914 /* Remove that input data, we never needed it anyway. */
1915 if (fwdData
.length
> 0) {
1916 krb5_free_data_contents( context
, &fwdData
);
1923 #else /* HAVE_KRB5 */
1924 /* this saves a few linking headaches */
1925 int cli_krb5_get_ticket(const char *principal
, time_t time_offset
,
1926 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
, uint32 extra_ap_opts
,
1927 const char *ccname
, time_t *tgs_expire
)
1929 DEBUG(0,("NO KERBEROS SUPPORT\n"));