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
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define KRB5_PRIVATE 1 /* this file uses PRIVATE interfaces! */
25 #define KRB5_DEPRECATED 1 /* this file uses DEPRECATED interfaces! */
31 #ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE /* Heimdal */
32 #define KRB5_KEY_TYPE(k) ((k)->keytype)
33 #define KRB5_KEY_LENGTH(k) ((k)->keyvalue.length)
34 #define KRB5_KEY_DATA(k) ((k)->keyvalue.data)
36 #define KRB5_KEY_TYPE(k) ((k)->enctype)
37 #define KRB5_KEY_LENGTH(k) ((k)->length)
38 #define KRB5_KEY_DATA(k) ((k)->contents)
39 #endif /* HAVE_KRB5_KEYBLOCK_KEYVALUE */
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
)
57 if (push_utf8_allocate(&utf8_name
, name
) == (size_t)-1) {
61 ret
= krb5_parse_name(context
, utf8_name
, principal
);
66 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
67 /**************************************************************
68 krb5_parse_name_norealm that takes a UNIX charset.
69 **************************************************************/
71 static krb5_error_code
smb_krb5_parse_name_norealm_conv(krb5_context context
,
72 const char *name
, /* in unix charset */
73 krb5_principal
*principal
)
79 if (push_utf8_allocate(&utf8_name
, name
) == (size_t)-1) {
83 ret
= krb5_parse_name_norealm(context
, utf8_name
, principal
);
89 /**************************************************************
90 krb5_parse_name that returns a UNIX charset name. Must
91 be freed with normal free() call.
92 **************************************************************/
94 krb5_error_code
smb_krb5_unparse_name(krb5_context context
,
95 krb5_const_principal principal
,
102 ret
= krb5_unparse_name(context
, principal
, &utf8_name
);
107 if (pull_utf8_allocate(unix_name
, utf8_name
)==-1) {
108 krb5_free_unparsed_name(context
, utf8_name
);
111 krb5_free_unparsed_name(context
, utf8_name
);
115 #ifndef HAVE_KRB5_SET_REAL_TIME
117 * This function is not in the Heimdal mainline.
119 krb5_error_code
krb5_set_real_time(krb5_context context
, int32_t seconds
, int32_t microseconds
)
124 ret
= krb5_us_timeofday(context
, &sec
, &usec
);
128 context
->kdc_sec_offset
= seconds
- sec
;
129 context
->kdc_usec_offset
= microseconds
- usec
;
135 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
137 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
139 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
140 * to krb5_set_default_tgs_ktypes. See
141 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
143 * If the MIT libraries are not exporting internal symbols, we will end up in
144 * this branch, which is correct. Otherwise we will continue to use the
147 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
149 return krb5_set_default_tgs_enctypes(ctx
, enc
);
152 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
155 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
157 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
160 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
162 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
164 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
166 void setup_kaddr( krb5_address
*pkaddr
, struct sockaddr
*paddr
)
168 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
169 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
170 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
172 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
174 void setup_kaddr( krb5_address
*pkaddr
, struct sockaddr
*paddr
)
176 pkaddr
->addrtype
= ADDRTYPE_INET
;
177 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
178 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
181 #error UNKNOWN_ADDRTYPE
184 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
185 int create_kerberos_key_from_string_direct(krb5_context context
,
186 krb5_principal host_princ
,
189 krb5_enctype enctype
)
193 krb5_encrypt_block eblock
;
195 ret
= krb5_principal2salt(context
, host_princ
, &salt
);
197 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret
)));
200 krb5_use_enctype(context
, &eblock
, enctype
);
201 ret
= krb5_string_to_key(context
, &eblock
, key
, password
, &salt
);
202 SAFE_FREE(salt
.data
);
205 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
206 int create_kerberos_key_from_string_direct(krb5_context context
,
207 krb5_principal host_princ
,
210 krb5_enctype enctype
)
215 ret
= krb5_get_pw_salt(context
, host_princ
, &salt
);
217 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret
)));
221 ret
= krb5_string_to_key_salt(context
, enctype
, (const char *)password
->data
, salt
, key
);
222 krb5_free_salt(context
, salt
);
226 #error UNKNOWN_CREATE_KEY_FUNCTIONS
229 int create_kerberos_key_from_string(krb5_context context
,
230 krb5_principal host_princ
,
233 krb5_enctype enctype
)
235 krb5_principal salt_princ
= NULL
;
238 * Check if we've determined that the KDC is salting keys for this
239 * principal/enctype in a non-obvious way. If it is, try to match
242 salt_princ
= kerberos_fetch_salt_princ_for_host_princ(context
, host_princ
, enctype
);
243 ret
= create_kerberos_key_from_string_direct(context
, salt_princ
? salt_princ
: host_princ
, password
, key
, enctype
);
245 krb5_free_principal(context
, salt_princ
);
250 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
251 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
252 krb5_enctype
**enctypes
)
254 return krb5_get_permitted_enctypes(context
, enctypes
);
256 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
257 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
258 krb5_enctype
**enctypes
)
260 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
263 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
266 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
267 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
268 krb5_auth_context auth_context
,
269 krb5_keyblock
*keyblock
)
271 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
275 BOOL
unwrap_pac(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, DATA_BLOB
*unwrapped_pac_data
)
277 DATA_BLOB pac_contents
;
281 if (!auth_data
->length
) {
285 asn1_load(&data
, *auth_data
);
286 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
287 asn1_start_tag(&data
, ASN1_SEQUENCE(0));
288 asn1_start_tag(&data
, ASN1_CONTEXT(0));
289 asn1_read_Integer(&data
, &data_type
);
291 if (data_type
!= KRB5_AUTHDATA_WIN2K_PAC
) {
292 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type
));
298 asn1_start_tag(&data
, ASN1_CONTEXT(1));
299 asn1_read_OctetString(&data
, &pac_contents
);
305 *unwrapped_pac_data
= data_blob_talloc(mem_ctx
, pac_contents
.data
, pac_contents
.length
);
307 data_blob_free(&pac_contents
);
312 BOOL
get_auth_data_from_tkt(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*auth_data
, krb5_ticket
*tkt
)
314 DATA_BLOB auth_data_wrapped
;
315 BOOL got_auth_data_pac
= False
;
318 #if defined(HAVE_KRB5_TKT_ENC_PART2)
319 if (tkt
->enc_part2
&& tkt
->enc_part2
->authorization_data
&&
320 tkt
->enc_part2
->authorization_data
[0] &&
321 tkt
->enc_part2
->authorization_data
[0]->length
)
323 for (i
= 0; tkt
->enc_part2
->authorization_data
[i
] != NULL
; i
++) {
325 if (tkt
->enc_part2
->authorization_data
[i
]->ad_type
!=
326 KRB5_AUTHDATA_IF_RELEVANT
) {
327 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
328 tkt
->enc_part2
->authorization_data
[i
]->ad_type
));
332 auth_data_wrapped
= data_blob(tkt
->enc_part2
->authorization_data
[i
]->contents
,
333 tkt
->enc_part2
->authorization_data
[i
]->length
);
335 /* check if it is a PAC */
336 got_auth_data_pac
= unwrap_pac(mem_ctx
, &auth_data_wrapped
, auth_data
);
337 data_blob_free(&auth_data_wrapped
);
339 if (!got_auth_data_pac
) {
344 return got_auth_data_pac
;
348 if (tkt
->ticket
.authorization_data
&&
349 tkt
->ticket
.authorization_data
->len
)
351 for (i
= 0; i
< tkt
->ticket
.authorization_data
->len
; i
++) {
353 if (tkt
->ticket
.authorization_data
->val
[i
].ad_type
!=
354 KRB5_AUTHDATA_IF_RELEVANT
) {
355 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
356 tkt
->ticket
.authorization_data
->val
[i
].ad_type
));
360 auth_data_wrapped
= data_blob(tkt
->ticket
.authorization_data
->val
[i
].ad_data
.data
,
361 tkt
->ticket
.authorization_data
->val
[i
].ad_data
.length
);
363 /* check if it is a PAC */
364 got_auth_data_pac
= unwrap_pac(mem_ctx
, &auth_data_wrapped
, auth_data
);
365 data_blob_free(&auth_data_wrapped
);
367 if (!got_auth_data_pac
) {
372 return got_auth_data_pac
;
378 krb5_const_principal
get_principal_from_tkt(krb5_ticket
*tkt
)
380 #if defined(HAVE_KRB5_TKT_ENC_PART2)
381 return tkt
->enc_part2
->client
;
387 #if !defined(HAVE_KRB5_LOCATE_KDC)
389 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
390 * to a public interface for this functionality, so we have to be able to live
391 * without it if the MIT libraries are hiding their internal symbols.
394 #if defined(KRB5_KRBHST_INIT)
396 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
, struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
398 krb5_krbhst_handle hnd
;
399 krb5_krbhst_info
*hinfo
;
408 rc
= krb5_krbhst_init(ctx
, realm
->data
, KRB5_KRBHST_KDC
, &hnd
);
410 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc
)));
414 for ( num_kdcs
= 0; (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); num_kdcs
++)
417 krb5_krbhst_reset(ctx
, hnd
);
420 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
421 krb5_krbhst_free(ctx
, hnd
);
425 sa
= SMB_MALLOC_ARRAY( struct sockaddr
, num_kdcs
);
427 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
428 krb5_krbhst_free(ctx
, hnd
);
433 memset(sa
, '\0', sizeof(struct sockaddr
) * num_kdcs
);
435 for (i
= 0; i
< num_kdcs
&& (rc
= krb5_krbhst_next(ctx
, hnd
, &hinfo
) == 0); i
++) {
437 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
438 rc
= krb5_krbhst_get_addrinfo(ctx
, hinfo
, &ai
);
440 DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc
)));
444 if (hinfo
->ai
&& hinfo
->ai
->ai_family
== AF_INET
)
445 memcpy(&sa
[i
], hinfo
->ai
->ai_addr
, sizeof(struct sockaddr
));
448 krb5_krbhst_free(ctx
, hnd
);
455 #else /* ! defined(KRB5_KRBHST_INIT) */
457 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
,
458 struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
460 DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
461 return KRB5_KDC_UNREACH
;
464 #endif /* KRB5_KRBHST_INIT */
466 #else /* ! HAVE_KRB5_LOCATE_KDC */
468 krb5_error_code
smb_krb5_locate_kdc(krb5_context ctx
, const krb5_data
*realm
,
469 struct sockaddr
**addr_pp
, int *naddrs
, int get_masters
)
471 return krb5_locate_kdc(ctx
, realm
, addr_pp
, naddrs
, get_masters
);
474 #endif /* HAVE_KRB5_LOCATE_KDC */
476 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
477 void krb5_free_unparsed_name(krb5_context context
, char *val
)
483 void kerberos_free_data_contents(krb5_context context
, krb5_data
*pdata
)
485 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
487 krb5_free_data_contents(context
, pdata
);
490 SAFE_FREE(pdata
->data
);
494 void kerberos_set_creds_enctype(krb5_creds
*pcreds
, int enctype
)
496 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
497 KRB5_KEY_TYPE((&pcreds
->keyblock
)) = enctype
;
498 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
499 KRB5_KEY_TYPE((&pcreds
->session
)) = enctype
;
501 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
505 BOOL
kerberos_compatible_enctypes(krb5_context context
,
506 krb5_enctype enctype1
,
507 krb5_enctype enctype2
)
509 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
510 krb5_boolean similar
= 0;
512 krb5_c_enctype_compare(context
, enctype1
, enctype2
, &similar
);
513 return similar
? True
: False
;
514 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
515 return krb5_enctypes_compatible_keys(context
, enctype1
, enctype2
) ? True
: False
;
519 static BOOL
ads_cleanup_expired_creds(krb5_context context
,
523 krb5_error_code retval
;
524 const char *cc_type
= krb5_cc_get_type(context
, ccache
);
526 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
527 cc_type
, krb5_cc_get_name(context
, ccache
),
528 http_timestring(credsp
->times
.endtime
)));
530 /* we will probably need new tickets if the current ones
531 will expire within 10 seconds.
533 if (credsp
->times
.endtime
>= (time(NULL
) + 10))
536 /* heimdal won't remove creds from a file ccache, and
537 perhaps we shouldn't anyway, since internally we
538 use memory ccaches, and a FILE one probably means that
539 we're using creds obtained outside of our exectuable
541 if (strequal(cc_type
, "FILE")) {
542 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type
));
546 retval
= krb5_cc_remove_cred(context
, ccache
, 0, credsp
);
548 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
549 error_message(retval
)));
550 /* If we have an error in this, we want to display it,
551 but continue as though we deleted it */
557 we can't use krb5_mk_req because w2k wants the service to be in a particular format
559 static krb5_error_code
ads_krb5_mk_req(krb5_context context
,
560 krb5_auth_context
*auth_context
,
561 const krb5_flags ap_req_options
,
562 const char *principal
,
567 krb5_error_code retval
;
568 krb5_principal server
;
572 BOOL creds_ready
= False
;
573 int i
= 0, maxtries
= 3;
575 retval
= smb_krb5_parse_name(context
, principal
, &server
);
577 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal
));
581 /* obtain ticket & session key */
583 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
584 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
585 error_message(retval
)));
589 if ((retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
))) {
590 /* This can commonly fail on smbd startup with no ticket in the cache.
591 * Report at higher level than 1. */
592 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
593 error_message(retval
)));
597 while (!creds_ready
&& (i
< maxtries
)) {
599 if ((retval
= krb5_get_credentials(context
, 0, ccache
,
601 DEBUG(1,("ads_krb5_mk_req: krb5_get_credentials failed for %s (%s)\n",
602 principal
, error_message(retval
)));
606 /* cope with ticket being in the future due to clock skew */
607 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
608 time_t t
= time(NULL
);
609 int time_offset
=(int)((unsigned)credsp
->times
.starttime
-t
);
610 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
611 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
614 if (!ads_cleanup_expired_creds(context
, ccache
, credsp
)) {
621 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
622 principal
, krb5_cc_get_type(context
, ccache
), krb5_cc_get_name(context
, ccache
),
623 http_timestring((unsigned)credsp
->times
.endtime
),
624 (unsigned)credsp
->times
.endtime
));
627 *expire_time
= (time_t)credsp
->times
.endtime
;
631 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
632 &in_data
, credsp
, outbuf
);
634 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
635 error_message(retval
)));
638 krb5_free_creds(context
, credsp
);
641 krb5_free_cred_contents(context
, &creds
);
644 krb5_free_principal(context
, server
);
650 get a kerberos5 ticket for the given service
652 int cli_krb5_get_ticket(const char *principal
, time_t time_offset
,
653 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
654 uint32 extra_ap_opts
, const char *ccname
,
658 krb5_error_code retval
;
660 krb5_context context
= NULL
;
661 krb5_ccache ccdef
= NULL
;
662 krb5_auth_context auth_context
= NULL
;
663 krb5_enctype enc_types
[] = {
664 #ifdef ENCTYPE_ARCFOUR_HMAC
665 ENCTYPE_ARCFOUR_HMAC
,
671 initialize_krb5_error_table();
672 retval
= krb5_init_context(&context
);
674 DEBUG(1,("cli_krb5_get_ticket: krb5_init_context failed (%s)\n",
675 error_message(retval
)));
679 if (time_offset
!= 0) {
680 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
683 if ((retval
= krb5_cc_resolve(context
, ccname
?
684 ccname
: krb5_cc_default_name(context
), &ccdef
))) {
685 DEBUG(1,("cli_krb5_get_ticket: krb5_cc_default failed (%s)\n",
686 error_message(retval
)));
690 if ((retval
= krb5_set_default_tgs_ktypes(context
, enc_types
))) {
691 DEBUG(1,("cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s)\n",
692 error_message(retval
)));
696 if ((retval
= ads_krb5_mk_req(context
,
698 AP_OPTS_USE_SUBKEY
| (krb5_flags
)extra_ap_opts
,
705 get_krb5_smb_session_key(context
, auth_context
, session_key_krb5
, False
);
707 *ticket
= data_blob(packet
.data
, packet
.length
);
709 kerberos_free_data_contents(context
, &packet
);
715 krb5_cc_close(context
, ccdef
);
717 krb5_auth_con_free(context
, auth_context
);
718 krb5_free_context(context
);
724 BOOL
get_krb5_smb_session_key(krb5_context context
, krb5_auth_context auth_context
, DATA_BLOB
*session_key
, BOOL remote
)
731 err
= krb5_auth_con_getremotesubkey(context
, auth_context
, &skey
);
733 err
= krb5_auth_con_getlocalsubkey(context
, auth_context
, &skey
);
734 if (err
== 0 && skey
!= NULL
) {
735 DEBUG(10, ("Got KRB5 session key of length %d\n", (int)KRB5_KEY_LENGTH(skey
)));
736 *session_key
= data_blob(KRB5_KEY_DATA(skey
), KRB5_KEY_LENGTH(skey
));
737 dump_data_pw("KRB5 Session Key:\n", session_key
->data
, session_key
->length
);
741 krb5_free_keyblock(context
, skey
);
743 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
750 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
751 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
);
753 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
)
755 static krb5_data kdata
;
757 kdata
.data
= (char *)krb5_principal_get_comp_string(context
, principal
, i
);
758 kdata
.length
= strlen((const char *)kdata
.data
);
763 krb5_error_code
smb_krb5_kt_free_entry(krb5_context context
, krb5_keytab_entry
*kt_entry
)
765 #if defined(HAVE_KRB5_KT_FREE_ENTRY)
766 return krb5_kt_free_entry(context
, kt_entry
);
767 #elif defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
768 return krb5_free_keytab_entry_contents(context
, kt_entry
);
770 #error UNKNOWN_KT_FREE_FUNCTION
774 void smb_krb5_checksum_from_pac_sig(krb5_checksum
*cksum
,
775 PAC_SIGNATURE_DATA
*sig
)
777 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
778 cksum
->cksumtype
= (krb5_cksumtype
)sig
->type
;
779 cksum
->checksum
.length
= sig
->signature
.buf_len
;
780 cksum
->checksum
.data
= sig
->signature
.buffer
;
782 cksum
->checksum_type
= (krb5_cksumtype
)sig
->type
;
783 cksum
->length
= sig
->signature
.buf_len
;
784 cksum
->contents
= sig
->signature
.buffer
;
788 krb5_error_code
smb_krb5_verify_checksum(krb5_context context
,
789 krb5_keyblock
*keyblock
,
791 krb5_checksum
*cksum
,
797 /* verify the checksum */
799 /* welcome to the wonderful world of samba's kerberos abstraction layer:
801 * function heimdal 0.6.1rc3 heimdal 0.7 MIT krb 1.4.2
802 * -----------------------------------------------------------------------------
803 * krb5_c_verify_checksum - works works
804 * krb5_verify_checksum works (6 args) works (6 args) broken (7 args)
807 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
809 krb5_boolean checksum_valid
= False
;
812 input
.data
= (char *)data
;
813 input
.length
= length
;
815 ret
= krb5_c_verify_checksum(context
,
822 DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n",
823 error_message(ret
)));
828 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
831 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
833 /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
834 * without enctype and it ignores any key_usage types - Guenther */
839 ret
= krb5_crypto_init(context
,
844 DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n",
845 error_message(ret
)));
849 ret
= krb5_verify_checksum(context
,
856 krb5_crypto_destroy(context
, crypto
);
860 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
866 time_t get_authtime_from_tkt(krb5_ticket
*tkt
)
868 #if defined(HAVE_KRB5_TKT_ENC_PART2)
869 return tkt
->enc_part2
->times
.authtime
;
871 return tkt
->ticket
.authtime
;
875 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
876 static int get_kvno_from_ap_req(krb5_ap_req
*ap_req
)
878 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
879 if (ap_req
->ticket
->enc_part
.kvno
)
880 return ap_req
->ticket
->enc_part
.kvno
;
882 if (ap_req
->ticket
.enc_part
.kvno
)
883 return *ap_req
->ticket
.enc_part
.kvno
;
888 static krb5_enctype
get_enctype_from_ap_req(krb5_ap_req
*ap_req
)
890 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
891 return ap_req
->ticket
.enc_part
.etype
;
893 return ap_req
->ticket
->enc_part
.enctype
;
896 #endif /* HAVE_KRB5_DECODE_AP_REQ */
898 static krb5_error_code
899 get_key_from_keytab(krb5_context context
,
900 krb5_const_principal server
,
901 krb5_enctype enctype
,
903 krb5_keyblock
**out_key
)
905 krb5_keytab_entry entry
;
910 /* We have to open a new keytab handle here, as MIT does
911 an implicit open/getnext/close on krb5_kt_get_entry. We
912 may be in the middle of a keytab enumeration when this is
915 ret
= krb5_kt_default(context
, &keytab
);
917 DEBUG(0,("get_key_from_keytab: failed to open keytab: %s\n", error_message(ret
)));
921 if ( DEBUGLEVEL
>= 10 ) {
922 if (smb_krb5_unparse_name(context
, server
, &name
) == 0) {
923 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
924 kvno
, enctype
, name
));
929 ret
= krb5_kt_get_entry(context
,
937 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret
)));
941 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK /* Heimdal */
942 ret
= krb5_copy_keyblock(context
, &entry
.keyblock
, out_key
);
943 #elif defined(HAVE_KRB5_KEYTAB_ENTRY_KEY) /* MIT */
944 ret
= krb5_copy_keyblock(context
, &entry
.key
, out_key
);
946 #error UNKNOWN_KRB5_KEYTAB_ENTRY_FORMAT
950 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret
)));
954 smb_krb5_kt_free_entry(context
, &entry
);
957 krb5_kt_close(context
, keytab
);
963 krb5_error_code
smb_krb5_get_keyinfo_from_ap_req(krb5_context context
,
964 const krb5_data
*inbuf
,
966 krb5_enctype
*enctype
)
968 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
973 ret
= krb5_decode_ap_req(context
, inbuf
, &ap_req
);
977 *kvno
= get_kvno_from_ap_req(&ap_req
);
978 *enctype
= get_enctype_from_ap_req(&ap_req
);
980 free_AP_REQ(&ap_req
);
985 /* Possibly not an appropriate error code. */
986 return KRB5KDC_ERR_BADOPTION
;
989 krb5_error_code
krb5_rd_req_return_keyblock_from_keytab(krb5_context context
,
990 krb5_auth_context
*auth_context
,
991 const krb5_data
*inbuf
,
992 krb5_const_principal server
,
994 krb5_flags
*ap_req_options
,
995 krb5_ticket
**ticket
,
996 krb5_keyblock
**keyblock
)
1000 krb5_enctype enctype
;
1001 krb5_keyblock
*local_keyblock
;
1003 ret
= krb5_rd_req(context
,
1014 #ifdef KRB5_TICKET_HAS_KEYINFO
1015 enctype
= (*ticket
)->enc_part
.enctype
;
1016 kvno
= (*ticket
)->enc_part
.kvno
;
1018 ret
= smb_krb5_get_keyinfo_from_ap_req(context
, inbuf
, &kvno
, &enctype
);
1024 ret
= get_key_from_keytab(context
,
1030 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1035 if (ret
&& local_keyblock
!= NULL
) {
1036 krb5_free_keyblock(context
, local_keyblock
);
1038 *keyblock
= local_keyblock
;
1044 krb5_error_code
smb_krb5_parse_name_norealm(krb5_context context
,
1046 krb5_principal
*principal
)
1048 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1049 return smb_krb5_parse_name_norealm_conv(context
, name
, principal
);
1052 /* we are cheating here because parse_name will in fact set the realm.
1053 * We don't care as the only caller of smb_krb5_parse_name_norealm
1054 * ignores the realm anyway when calling
1055 * smb_krb5_principal_compare_any_realm later - Guenther */
1057 return smb_krb5_parse_name(context
, name
, principal
);
1060 BOOL
smb_krb5_principal_compare_any_realm(krb5_context context
,
1061 krb5_const_principal princ1
,
1062 krb5_const_principal princ2
)
1064 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1066 return krb5_principal_compare_any_realm(context
, princ1
, princ2
);
1068 /* krb5_princ_size is a macro in MIT */
1069 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1072 const krb5_data
*p1
, *p2
;
1074 len1
= krb5_princ_size(context
, princ1
);
1075 len2
= krb5_princ_size(context
, princ2
);
1080 for (i
= 0; i
< len1
; i
++) {
1082 p1
= krb5_princ_component(context
, CONST_DISCARD(krb5_principal
, princ1
), i
);
1083 p2
= krb5_princ_component(context
, CONST_DISCARD(krb5_principal
, princ2
), i
);
1085 if (p1
->length
!= p2
->length
|| memcmp(p1
->data
, p2
->data
, p1
->length
))
1091 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1095 krb5_error_code
smb_krb5_renew_ticket(const char *ccache_string
, /* FILE:/tmp/krb5cc_0 */
1096 const char *client_string
, /* gd@BER.SUSE.DE */
1097 const char *service_string
, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1098 time_t *new_start_time
)
1100 krb5_error_code ret
;
1101 krb5_context context
= NULL
;
1102 krb5_ccache ccache
= NULL
;
1103 krb5_principal client
= NULL
;
1105 initialize_krb5_error_table();
1106 ret
= krb5_init_context(&context
);
1111 if (!ccache_string
) {
1112 ccache_string
= krb5_cc_default_name(context
);
1115 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string
));
1117 /* FIXME: we should not fall back to defaults */
1118 ret
= krb5_cc_resolve(context
, CONST_DISCARD(char *, ccache_string
), &ccache
);
1123 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1127 if (client_string
) {
1128 ret
= smb_krb5_parse_name(context
, client_string
, &client
);
1133 ret
= krb5_cc_get_principal(context
, ccache
, &client
);
1139 ret
= krb5_get_renewed_creds(context
, &creds
, client
, ccache
, CONST_DISCARD(char *, service_string
));
1141 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1145 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1146 ret
= krb5_cc_initialize(context
, ccache
, client
);
1151 ret
= krb5_cc_store_cred(context
, ccache
, &creds
);
1153 if (new_start_time
) {
1154 *new_start_time
= (time_t) creds
.times
.renew_till
;
1157 krb5_free_cred_contents(context
, &creds
);
1159 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1161 krb5_kdc_flags flags
;
1162 krb5_creds creds_in
;
1163 krb5_realm
*client_realm
;
1166 memset(&creds_in
, 0, sizeof(creds_in
));
1168 if (client_string
) {
1169 ret
= smb_krb5_parse_name(context
, client_string
, &creds_in
.client
);
1174 ret
= krb5_cc_get_principal(context
, ccache
, &creds_in
.client
);
1180 if (service_string
) {
1181 ret
= smb_krb5_parse_name(context
, service_string
, &creds_in
.server
);
1186 /* build tgt service by default */
1187 client_realm
= krb5_princ_realm(context
, client
);
1188 ret
= krb5_make_principal(context
, &creds_in
.server
, *client_realm
, KRB5_TGS_NAME
, *client_realm
, NULL
);
1195 flags
.b
.renewable
= flags
.b
.renew
= True
;
1197 ret
= krb5_get_kdc_cred(context
, ccache
, flags
, NULL
, NULL
, &creds_in
, &creds
);
1199 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1203 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1204 ret
= krb5_cc_initialize(context
, ccache
, creds_in
.client
);
1209 ret
= krb5_cc_store_cred(context
, ccache
, creds
);
1211 if (new_start_time
) {
1212 *new_start_time
= (time_t) creds
->times
.renew_till
;
1215 krb5_free_cred_contents(context
, &creds_in
);
1216 krb5_free_creds(context
, creds
);
1219 #error NO_SUITABKE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1225 krb5_free_principal(context
, client
);
1228 krb5_free_context(context
);
1231 krb5_cc_close(context
, ccache
);
1238 krb5_error_code
smb_krb5_free_addresses(krb5_context context
, smb_krb5_addresses
*addr
)
1240 krb5_error_code ret
= 0;
1244 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1245 krb5_free_addresses(context
, addr
->addrs
);
1246 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1247 ret
= krb5_free_addresses(context
, addr
->addrs
);
1248 SAFE_FREE(addr
->addrs
);
1255 krb5_error_code
smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses
**kerb_addr
)
1257 krb5_error_code ret
= 0;
1259 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1260 krb5_address
**addrs
= NULL
;
1261 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1262 krb5_addresses
*addrs
= NULL
;
1265 *kerb_addr
= (smb_krb5_addresses
*)SMB_MALLOC(sizeof(smb_krb5_addresses
));
1266 if (*kerb_addr
== NULL
) {
1270 put_name(buf
, global_myname(), ' ', 0x20);
1272 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1276 addrs
= (krb5_address
**)SMB_MALLOC(sizeof(krb5_address
*) * num_addr
);
1277 if (addrs
== NULL
) {
1278 SAFE_FREE(kerb_addr
);
1282 memset(addrs
, 0, sizeof(krb5_address
*) * num_addr
);
1284 addrs
[0] = (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1285 if (addrs
[0] == NULL
) {
1287 SAFE_FREE(kerb_addr
);
1291 addrs
[0]->magic
= KV5M_ADDRESS
;
1292 addrs
[0]->addrtype
= KRB5_ADDR_NETBIOS
;
1293 addrs
[0]->length
= MAX_NETBIOSNAME_LEN
;
1294 addrs
[0]->contents
= (unsigned char *)SMB_MALLOC(addrs
[0]->length
);
1295 if (addrs
[0]->contents
== NULL
) {
1296 SAFE_FREE(addrs
[0]);
1298 SAFE_FREE(kerb_addr
);
1302 memcpy(addrs
[0]->contents
, buf
, addrs
[0]->length
);
1306 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1308 addrs
= (krb5_addresses
*)SMB_MALLOC(sizeof(krb5_addresses
));
1309 if (addrs
== NULL
) {
1310 SAFE_FREE(kerb_addr
);
1314 memset(addrs
, 0, sizeof(krb5_addresses
));
1317 addrs
->val
= (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1318 if (addrs
->val
== NULL
) {
1320 SAFE_FREE(kerb_addr
);
1324 addrs
->val
[0].addr_type
= KRB5_ADDR_NETBIOS
;
1325 addrs
->val
[0].address
.length
= MAX_NETBIOSNAME_LEN
;
1326 addrs
->val
[0].address
.data
= (unsigned char *)SMB_MALLOC(addrs
->val
[0].address
.length
);
1327 if (addrs
->val
[0].address
.data
== NULL
) {
1328 SAFE_FREE(addrs
->val
);
1330 SAFE_FREE(kerb_addr
);
1334 memcpy(addrs
->val
[0].address
.data
, buf
, addrs
->val
[0].address
.length
);
1337 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1339 (*kerb_addr
)->addrs
= addrs
;
1344 void smb_krb5_free_error(krb5_context context
, krb5_error
*krberror
)
1346 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1347 krb5_free_error_contents(context
, krberror
);
1349 krb5_free_error(context
, krberror
);
1353 krb5_error_code
handle_krberror_packet(krb5_context context
,
1356 krb5_error_code ret
;
1357 BOOL got_error_code
= False
;
1359 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1361 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1363 krb5_error krberror
;
1365 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1366 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1367 error_message(ret
)));
1371 if (krberror
.e_data
== NULL
|| krberror
.e_data
->data
== NULL
) {
1372 ret
= (krb5_error_code
) krberror
.error_code
;
1373 got_error_code
= True
;
1376 smb_krb5_free_error(context
, &krberror
);
1380 krb5_error
*krberror
;
1382 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1383 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1384 error_message(ret
)));
1388 if (krberror
->e_data
.data
== NULL
) {
1389 ret
= ERROR_TABLE_BASE_krb5
+ (krb5_error_code
) krberror
->error
;
1390 got_error_code
= True
;
1392 smb_krb5_free_error(context
, krberror
);
1395 if (got_error_code
) {
1396 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1397 error_message(ret
), ret
));
1402 krb5_error_code
smb_krb5_get_init_creds_opt_alloc(krb5_context context
,
1403 krb5_get_init_creds_opt
**opt
)
1405 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1406 /* Heimdal or modern MIT version */
1407 return krb5_get_init_creds_opt_alloc(context
, opt
);
1409 /* Historical MIT version */
1410 krb5_get_init_creds_opt
*my_opt
;
1414 if ((my_opt
= SMB_MALLOC(sizeof(krb5_get_init_creds_opt
))) == NULL
) {
1418 krb5_get_init_creds_opt_init(my_opt
);
1422 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1425 void smb_krb5_get_init_creds_opt_free(krb5_context context
,
1426 krb5_get_init_creds_opt
*opt
)
1428 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1430 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1431 /* Modern MIT or Heimdal version */
1432 krb5_get_init_creds_opt_free(context
, opt
);
1434 /* Heimdal version */
1435 krb5_get_init_creds_opt_free(opt
);
1436 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1438 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1439 /* Historical MIT version */
1442 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1445 krb5_enctype
smb_get_enctype_from_kt_entry(const krb5_keytab_entry
*kt_entry
)
1447 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEY /* MIT */
1448 return kt_entry
->key
.enctype
;
1449 #elif defined(HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK) /* Heimdal */
1450 return kt_entry
->keyblock
.keytype
;
1452 #error UNKNOWN_KRB5_KEYTAB_ENTRY_KEYBLOCK_FORMAT
1457 /* caller needs to free etype_s */
1458 krb5_error_code
smb_krb5_enctype_to_string(krb5_context context
,
1459 krb5_enctype enctype
,
1462 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1463 return krb5_enctype_to_string(context
, enctype
, etype_s
); /* Heimdal */
1464 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1466 krb5_error_code ret
= krb5_enctype_to_string(enctype
, buf
, 256); /* MIT */
1470 *etype_s
= SMB_STRDUP(buf
);
1476 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1480 krb5_error_code
smb_krb5_mk_error(krb5_context context
,
1481 krb5_error_code error_code
,
1482 const krb5_principal server
,
1485 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1487 * The MIT interface is *terrible*.
1488 * We have to construct this ourselves...
1492 memset(&e
, 0, sizeof(e
));
1493 krb5_us_timeofday(context
, &e
.stime
, &e
.susec
);
1495 #if defined(krb5_err_base)
1496 e
.error
= error_code
- krb5_err_base
;
1497 #elif defined(ERROR_TABLE_BASE_krb5)
1498 e
.error
= error_code
- ERROR_TABLE_BASE_krb5
;
1500 e
.error
= error_code
; /* Almost certainly wrong, but what can we do... ? */
1503 return krb5_mk_error(context
, &e
, reply
);
1504 #else /* Heimdal. */
1505 return krb5_mk_error(context
,
1517 #else /* HAVE_KRB5 */
1518 /* this saves a few linking headaches */
1519 int cli_krb5_get_ticket(const char *principal
, time_t time_offset
,
1520 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
, uint32 extra_ap_opts
,
1521 const char *ccname
, time_t *tgs_expire
)
1523 DEBUG(0,("NO KERBEROS SUPPORT\n"));