2001-10-11 11:42:52 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2001-10-12 08:49:42 +04:00
simple kerberos5 routines for active directory
2001-10-11 11:42:52 +04:00
Copyright ( C ) Andrew Tridgell 2001
2003-03-18 01:45:16 +03:00
Copyright ( C ) Luke Howard 2002 - 2003
2005-09-30 21:13:37 +04:00
Copyright ( C ) Andrew Bartlett < abartlet @ samba . org > 2005
2007-06-29 13:42:14 +04:00
Copyright ( C ) Guenther Deschner 2005 - 2007
2001-10-11 11:42:52 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2001-10-11 11:42:52 +04:00
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2001-10-11 11:42:52 +04:00
*/
2005-03-31 09:06:04 +04:00
# define KRB5_PRIVATE 1 /* this file uses PRIVATE interfaces! */
# define KRB5_DEPRECATED 1 /* this file uses DEPRECATED interfaces! */
2001-10-11 11:42:52 +04:00
# include "includes.h"
2001-11-29 02:54:07 +03:00
# ifdef HAVE_KRB5
2003-01-20 01:27:32 +03:00
2008-08-09 01:32:15 +04:00
# define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
# define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
# define GSSAPI_CHECKSUM_SIZE (12+GSSAPI_BNDLENGTH)
2008-08-09 03:08:11 +04:00
# 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)
2008-08-09 01:32:15 +04:00
static krb5_error_code ads_krb5_get_fwd_ticket ( krb5_context context ,
krb5_auth_context * auth_context ,
krb5_creds * credsp ,
krb5_ccache ccache ,
krb5_data * authenticator ) ;
# endif
2006-04-24 19:57:54 +04:00
/**************************************************************
Wrappers around kerberos string functions that convert from
utf8 - > unix charset and vica versa .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**************************************************************
krb5_parse_name that takes a UNIX charset .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-04-24 20:29:55 +04:00
krb5_error_code smb_krb5_parse_name ( krb5_context context ,
2006-04-24 19:57:54 +04:00
const char * name , /* in unix charset */
krb5_principal * principal )
{
krb5_error_code ret ;
char * utf8_name ;
2008-04-30 01:36:24 +04:00
size_t converted_size ;
2006-04-24 19:57:54 +04:00
2009-03-18 08:23:27 +03:00
if ( ! push_utf8_talloc ( talloc_tos ( ) , & utf8_name , name , & converted_size ) ) {
2006-04-24 19:57:54 +04:00
return ENOMEM ;
}
ret = krb5_parse_name ( context , utf8_name , principal ) ;
2009-03-18 08:23:27 +03:00
TALLOC_FREE ( utf8_name ) ;
2006-04-24 19:57:54 +04:00
return ret ;
}
# ifdef HAVE_KRB5_PARSE_NAME_NOREALM
/**************************************************************
krb5_parse_name_norealm that takes a UNIX charset .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static krb5_error_code smb_krb5_parse_name_norealm_conv ( krb5_context context ,
const char * name , /* in unix charset */
krb5_principal * principal )
{
krb5_error_code ret ;
char * utf8_name ;
2008-04-30 01:36:24 +04:00
size_t converted_size ;
2006-04-24 19:57:54 +04:00
2007-03-15 22:18:18 +03:00
* principal = NULL ;
2009-03-18 08:23:27 +03:00
if ( ! push_utf8_talloc ( talloc_tos ( ) , & utf8_name , name , & converted_size ) ) {
2006-04-24 19:57:54 +04:00
return ENOMEM ;
}
ret = krb5_parse_name_norealm ( context , utf8_name , principal ) ;
2009-03-18 08:23:27 +03:00
TALLOC_FREE ( utf8_name ) ;
2006-04-24 19:57:54 +04:00
return ret ;
}
# endif
/**************************************************************
krb5_parse_name that returns a UNIX charset name . Must
2009-03-18 08:23:27 +03:00
be freed with talloc_free ( ) call .
2006-04-24 19:57:54 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-03-18 08:23:27 +03:00
krb5_error_code smb_krb5_unparse_name ( TALLOC_CTX * mem_ctx ,
krb5_context context ,
krb5_const_principal principal ,
char * * unix_name )
2006-04-24 19:57:54 +04:00
{
krb5_error_code ret ;
char * utf8_name ;
2008-04-30 01:36:24 +04:00
size_t converted_size ;
2006-04-24 19:57:54 +04:00
2007-03-15 22:18:18 +03:00
* unix_name = NULL ;
2006-04-24 19:57:54 +04:00
ret = krb5_unparse_name ( context , principal , & utf8_name ) ;
if ( ret ) {
return ret ;
}
2009-03-18 08:23:27 +03:00
if ( ! pull_utf8_talloc ( mem_ctx , unix_name , utf8_name , & converted_size ) ) {
2006-04-24 19:57:54 +04:00
krb5_free_unparsed_name ( context , utf8_name ) ;
return ENOMEM ;
}
krb5_free_unparsed_name ( context , utf8_name ) ;
return 0 ;
}
2003-01-20 22:37:11 +03:00
# ifndef HAVE_KRB5_SET_REAL_TIME
2003-01-20 01:27:32 +03:00
/*
2006-07-21 00:23:04 +04:00
* This function is not in the Heimdal mainline .
2003-01-20 01:27:32 +03:00
*/
2003-01-21 16:33:31 +03:00
krb5_error_code krb5_set_real_time ( krb5_context context , int32_t seconds , int32_t microseconds )
2003-01-20 01:27:32 +03:00
{
krb5_error_code ret ;
int32_t sec , usec ;
ret = krb5_us_timeofday ( context , & sec , & usec ) ;
if ( ret )
return ret ;
context - > kdc_sec_offset = seconds - sec ;
context - > kdc_usec_offset = microseconds - usec ;
return 0 ;
}
# endif
2007-01-30 00:15:25 +03:00
# if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
# if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
/* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
* to krb5_set_default_tgs_ktypes . See
* http : //lists.samba.org/archive/samba-technical/2006-July/048271.html
*
* If the MIT libraries are not exporting internal symbols , we will end up in
* this branch , which is correct . Otherwise we will continue to use the
* internal symbol
*/
krb5_error_code krb5_set_default_tgs_ktypes ( krb5_context ctx , const krb5_enctype * enc )
{
return krb5_set_default_tgs_enctypes ( ctx , enc ) ;
}
# elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
/* Heimdal */
krb5_error_code krb5_set_default_tgs_ktypes ( krb5_context ctx , const krb5_enctype * enc )
{
return krb5_set_default_in_tkt_etypes ( ctx , enc ) ;
}
# endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
# endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
2003-01-21 09:23:49 +03:00
# if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
/* HEIMDAL */
2007-10-30 01:03:36 +03:00
bool setup_kaddr ( krb5_address * pkaddr , struct sockaddr_storage * paddr )
2003-01-21 09:23:49 +03:00
{
2007-10-30 01:03:36 +03:00
memset ( pkaddr , ' \0 ' , sizeof ( krb5_address ) ) ;
# if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
if ( paddr - > ss_family = = AF_INET6 ) {
pkaddr - > addr_type = KRB5_ADDRESS_INET6 ;
pkaddr - > address . length = sizeof ( ( ( struct sockaddr_in6 * ) paddr ) - > sin6_addr ) ;
pkaddr - > address . data = ( char * ) & ( ( ( struct sockaddr_in6 * ) paddr ) - > sin6_addr ) ;
return true ;
}
# endif
if ( paddr - > ss_family = = AF_INET ) {
pkaddr - > addr_type = KRB5_ADDRESS_INET ;
pkaddr - > address . length = sizeof ( ( ( struct sockaddr_in * ) paddr ) - > sin_addr ) ;
pkaddr - > address . data = ( char * ) & ( ( ( struct sockaddr_in * ) paddr ) - > sin_addr ) ;
return true ;
}
return false ;
2003-01-21 09:23:49 +03:00
}
# elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
/* MIT */
2007-10-30 01:03:36 +03:00
bool setup_kaddr ( krb5_address * pkaddr , struct sockaddr_storage * paddr )
2003-01-21 09:23:49 +03:00
{
2007-10-30 01:03:36 +03:00
memset ( pkaddr , ' \0 ' , sizeof ( krb5_address ) ) ;
# if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
if ( paddr - > ss_family = = AF_INET6 ) {
pkaddr - > addrtype = ADDRTYPE_INET6 ;
pkaddr - > length = sizeof ( ( ( struct sockaddr_in6 * ) paddr ) - > sin6_addr ) ;
pkaddr - > contents = ( krb5_octet * ) & ( ( ( struct sockaddr_in6 * ) paddr ) - > sin6_addr ) ;
return true ;
}
# endif
if ( paddr - > ss_family = = AF_INET ) {
pkaddr - > addrtype = ADDRTYPE_INET ;
pkaddr - > length = sizeof ( ( ( struct sockaddr_in * ) paddr ) - > sin_addr ) ;
pkaddr - > contents = ( krb5_octet * ) & ( ( ( struct sockaddr_in * ) paddr ) - > sin_addr ) ;
return true ;
}
return false ;
2003-01-21 09:23:49 +03:00
}
# else
2004-10-30 04:34:58 +04:00
# error UNKNOWN_ADDRTYPE
2003-01-21 09:23:49 +03:00
# endif
2004-11-05 03:57:29 +03:00
# if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
2008-06-17 19:58:38 +04:00
static int create_kerberos_key_from_string_direct ( krb5_context context ,
krb5_principal host_princ ,
krb5_data * password ,
krb5_keyblock * key ,
2008-08-04 15:52:18 +04:00
krb5_enctype enctype )
2003-01-30 21:01:03 +03:00
{
2008-06-27 00:19:40 +04:00
int ret = 0 ;
2003-01-30 21:01:03 +03:00
krb5_data salt ;
krb5_encrypt_block eblock ;
2008-08-04 15:52:18 +04:00
ret = krb5_principal2salt ( context , host_princ , & salt ) ;
if ( ret ) {
DEBUG ( 1 , ( " krb5_principal2salt failed (%s) \n " , error_message ( ret ) ) ) ;
return ret ;
2003-01-30 21:01:03 +03:00
}
2008-08-04 15:52:18 +04:00
krb5_use_enctype ( context , & eblock , enctype ) ;
ret = krb5_string_to_key ( context , & eblock , key , password , & salt ) ;
SAFE_FREE ( salt . data ) ;
2003-08-15 05:46:09 +04:00
return ret ;
2003-01-30 21:01:03 +03:00
}
# elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
2008-06-17 19:58:38 +04:00
static int create_kerberos_key_from_string_direct ( krb5_context context ,
krb5_principal host_princ ,
krb5_data * password ,
krb5_keyblock * key ,
2008-08-04 15:52:18 +04:00
krb5_enctype enctype )
2003-01-30 21:01:03 +03:00
{
int ret ;
krb5_salt salt ;
2008-08-04 15:52:18 +04:00
ret = krb5_get_pw_salt ( context , host_princ , & salt ) ;
if ( ret ) {
DEBUG ( 1 , ( " krb5_get_pw_salt failed (%s) \n " , error_message ( ret ) ) ) ;
return ret ;
2003-01-30 21:01:03 +03:00
}
2008-06-18 14:45:57 +04:00
2008-08-04 15:52:18 +04:00
ret = krb5_string_to_key_salt ( context , enctype , ( const char * ) password - > data , salt , key ) ;
krb5_free_salt ( context , salt ) ;
2005-09-14 01:21:43 +04:00
return ret ;
2003-01-30 21:01:03 +03:00
}
# else
2004-10-30 04:34:58 +04:00
# error UNKNOWN_CREATE_KEY_FUNCTIONS
2003-01-30 21:01:03 +03:00
# endif
2004-11-01 22:35:55 +03:00
int create_kerberos_key_from_string ( krb5_context context ,
2004-10-30 02:38:10 +04:00
krb5_principal host_princ ,
krb5_data * password ,
krb5_keyblock * key ,
2008-06-18 14:45:57 +04:00
krb5_enctype enctype ,
bool no_salt )
2004-10-30 02:38:10 +04:00
{
krb5_principal salt_princ = NULL ;
int ret ;
/*
* Check if we ' ve determined that the KDC is salting keys for this
* principal / enctype in a non - obvious way . If it is , try to match
* its behavior .
*/
2008-08-04 15:52:18 +04:00
if ( no_salt ) {
KRB5_KEY_DATA ( key ) = ( KRB5_KEY_DATA_CAST * ) SMB_MALLOC ( password - > length ) ;
if ( ! KRB5_KEY_DATA ( key ) ) {
return ENOMEM ;
}
memcpy ( KRB5_KEY_DATA ( key ) , password - > data , password - > length ) ;
KRB5_KEY_LENGTH ( key ) = password - > length ;
KRB5_KEY_TYPE ( key ) = enctype ;
return 0 ;
}
2004-10-30 02:38:10 +04:00
salt_princ = kerberos_fetch_salt_princ_for_host_princ ( context , host_princ , enctype ) ;
2008-08-04 15:52:18 +04:00
ret = create_kerberos_key_from_string_direct ( context , salt_princ ? salt_princ : host_princ , password , key , enctype ) ;
2004-10-30 02:38:10 +04:00
if ( salt_princ ) {
krb5_free_principal ( context , salt_princ ) ;
}
return ret ;
}
2003-02-19 18:48:12 +03:00
# if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
2004-06-28 15:12:43 +04:00
krb5_error_code get_kerberos_allowed_etypes ( krb5_context context ,
2003-02-19 18:48:12 +03:00
krb5_enctype * * enctypes )
{
return krb5_get_permitted_enctypes ( context , enctypes ) ;
}
# elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
2004-06-28 15:12:43 +04:00
krb5_error_code get_kerberos_allowed_etypes ( krb5_context context ,
2003-02-19 18:48:12 +03:00
krb5_enctype * * enctypes )
{
return krb5_get_default_in_tkt_etypes ( context , enctypes ) ;
}
# else
2003-03-18 01:45:16 +03:00
# error UNKNOWN_GET_ENCTYPES_FUNCTIONS
2003-02-19 18:48:12 +03:00
# endif
2003-01-30 21:01:03 +03:00
# if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
krb5_error_code krb5_auth_con_setuseruserkey ( krb5_context context ,
krb5_auth_context auth_context ,
krb5_keyblock * keyblock )
{
return krb5_auth_con_setkey ( context , auth_context , keyblock ) ;
}
# endif
2007-10-19 04:40:25 +04:00
bool unwrap_edata_ntstatus ( TALLOC_CTX * mem_ctx ,
2007-05-04 13:55:40 +04:00
DATA_BLOB * edata ,
DATA_BLOB * edata_out )
{
DATA_BLOB edata_contents ;
2008-10-22 21:23:49 +04:00
ASN1_DATA * data ;
2007-05-04 13:55:40 +04:00
int edata_type ;
if ( ! edata - > length ) {
return False ;
}
2008-10-22 21:23:49 +04:00
data = asn1_init ( mem_ctx ) ;
if ( data = = NULL ) {
return false ;
}
asn1_load ( data , * edata ) ;
asn1_start_tag ( data , ASN1_SEQUENCE ( 0 ) ) ;
asn1_start_tag ( data , ASN1_CONTEXT ( 1 ) ) ;
asn1_read_Integer ( data , & edata_type ) ;
2007-05-04 13:55:40 +04:00
if ( edata_type ! = KRB5_PADATA_PW_SALT ) {
DEBUG ( 0 , ( " edata is not of required type %d but of type %d \n " ,
KRB5_PADATA_PW_SALT , edata_type ) ) ;
2008-10-22 21:23:49 +04:00
asn1_free ( data ) ;
2007-05-04 13:55:40 +04:00
return False ;
}
2008-10-22 21:23:49 +04:00
asn1_start_tag ( data , ASN1_CONTEXT ( 2 ) ) ;
asn1_read_OctetString ( data , NULL , & edata_contents ) ;
asn1_end_tag ( data ) ;
asn1_end_tag ( data ) ;
asn1_end_tag ( data ) ;
asn1_free ( data ) ;
2007-05-04 13:55:40 +04:00
* edata_out = data_blob_talloc ( mem_ctx , edata_contents . data , edata_contents . length ) ;
data_blob_free ( & edata_contents ) ;
return True ;
}
2007-10-19 04:40:25 +04:00
bool unwrap_pac ( TALLOC_CTX * mem_ctx , DATA_BLOB * auth_data , DATA_BLOB * unwrapped_pac_data )
2003-01-30 21:01:03 +03:00
{
2005-09-30 21:13:37 +04:00
DATA_BLOB pac_contents ;
2008-10-22 21:23:49 +04:00
ASN1_DATA * data ;
2005-09-30 21:13:37 +04:00
int data_type ;
if ( ! auth_data - > length ) {
return False ;
}
2008-10-22 21:23:49 +04:00
data = asn1_init ( mem_ctx ) ;
if ( data = = NULL ) {
return false ;
}
asn1_load ( data , * auth_data ) ;
asn1_start_tag ( data , ASN1_SEQUENCE ( 0 ) ) ;
asn1_start_tag ( data , ASN1_SEQUENCE ( 0 ) ) ;
asn1_start_tag ( data , ASN1_CONTEXT ( 0 ) ) ;
asn1_read_Integer ( data , & data_type ) ;
2005-09-30 21:13:37 +04:00
if ( data_type ! = KRB5_AUTHDATA_WIN2K_PAC ) {
DEBUG ( 10 , ( " authorization data is not a Windows PAC (type: %d) \n " , data_type ) ) ;
2008-10-22 21:23:49 +04:00
asn1_free ( data ) ;
2005-09-30 21:13:37 +04:00
return False ;
}
2008-10-22 21:23:49 +04:00
asn1_end_tag ( data ) ;
asn1_start_tag ( data , ASN1_CONTEXT ( 1 ) ) ;
asn1_read_OctetString ( data , NULL , & pac_contents ) ;
asn1_end_tag ( data ) ;
asn1_end_tag ( data ) ;
asn1_end_tag ( data ) ;
asn1_free ( data ) ;
2005-09-30 21:13:37 +04:00
* unwrapped_pac_data = data_blob_talloc ( mem_ctx , pac_contents . data , pac_contents . length ) ;
data_blob_free ( & pac_contents ) ;
return True ;
}
2007-10-19 04:40:25 +04:00
bool get_auth_data_from_tkt ( TALLOC_CTX * mem_ctx , DATA_BLOB * auth_data , krb5_ticket * tkt )
2005-09-30 21:13:37 +04:00
{
DATA_BLOB auth_data_wrapped ;
2007-10-19 04:40:25 +04:00
bool got_auth_data_pac = False ;
2005-09-30 21:13:37 +04:00
int i ;
2003-01-30 21:01:03 +03:00
# if defined(HAVE_KRB5_TKT_ENC_PART2)
2005-09-30 21:13:37 +04:00
if ( tkt - > enc_part2 & & tkt - > enc_part2 - > authorization_data & &
tkt - > enc_part2 - > authorization_data [ 0 ] & &
tkt - > enc_part2 - > authorization_data [ 0 ] - > length )
{
for ( i = 0 ; tkt - > enc_part2 - > authorization_data [ i ] ! = NULL ; i + + ) {
if ( tkt - > enc_part2 - > authorization_data [ i ] - > ad_type ! =
KRB5_AUTHDATA_IF_RELEVANT ) {
DEBUG ( 10 , ( " get_auth_data_from_tkt: ad_type is %d \n " ,
tkt - > enc_part2 - > authorization_data [ i ] - > ad_type ) ) ;
continue ;
}
auth_data_wrapped = data_blob ( tkt - > enc_part2 - > authorization_data [ i ] - > contents ,
tkt - > enc_part2 - > authorization_data [ i ] - > length ) ;
/* check if it is a PAC */
got_auth_data_pac = unwrap_pac ( mem_ctx , & auth_data_wrapped , auth_data ) ;
data_blob_free ( & auth_data_wrapped ) ;
2007-12-12 20:57:45 +03:00
if ( got_auth_data_pac ) {
return true ;
2005-09-30 21:13:37 +04:00
}
}
return got_auth_data_pac ;
}
2003-01-30 21:01:03 +03:00
# else
2005-09-30 21:13:37 +04:00
if ( tkt - > ticket . authorization_data & &
tkt - > ticket . authorization_data - > len )
{
for ( i = 0 ; i < tkt - > ticket . authorization_data - > len ; i + + ) {
if ( tkt - > ticket . authorization_data - > val [ i ] . ad_type ! =
KRB5_AUTHDATA_IF_RELEVANT ) {
DEBUG ( 10 , ( " get_auth_data_from_tkt: ad_type is %d \n " ,
tkt - > ticket . authorization_data - > val [ i ] . ad_type ) ) ;
continue ;
}
auth_data_wrapped = data_blob ( tkt - > ticket . authorization_data - > val [ i ] . ad_data . data ,
tkt - > ticket . authorization_data - > val [ i ] . ad_data . length ) ;
/* check if it is a PAC */
got_auth_data_pac = unwrap_pac ( mem_ctx , & auth_data_wrapped , auth_data ) ;
data_blob_free ( & auth_data_wrapped ) ;
2007-12-12 15:38:28 +03:00
if ( got_auth_data_pac ) {
return true ;
2005-09-30 21:13:37 +04:00
}
}
return got_auth_data_pac ;
}
2003-01-30 21:01:03 +03:00
# endif
2005-09-30 21:13:37 +04:00
return False ;
2003-01-30 21:01:03 +03:00
}
krb5_const_principal get_principal_from_tkt ( krb5_ticket * tkt )
{
# if defined(HAVE_KRB5_TKT_ENC_PART2)
return tkt - > enc_part2 - > client ;
# else
return tkt - > client ;
# endif
}
# if !defined(HAVE_KRB5_LOCATE_KDC)
2006-10-14 03:43:27 +04:00
/* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
* to a public interface for this functionality , so we have to be able to live
* without it if the MIT libraries are hiding their internal symbols .
*/
# if defined(KRB5_KRBHST_INIT)
/* Heimdal */
2007-03-28 23:47:59 +04:00
krb5_error_code smb_krb5_locate_kdc ( krb5_context ctx , const krb5_data * realm , struct sockaddr * * addr_pp , int * naddrs , int get_masters )
2003-01-30 21:01:03 +03:00
{
krb5_krbhst_handle hnd ;
krb5_krbhst_info * hinfo ;
krb5_error_code rc ;
int num_kdcs , i ;
struct sockaddr * sa ;
2004-09-21 14:40:45 +04:00
struct addrinfo * ai ;
2003-01-30 21:01:03 +03:00
* addr_pp = NULL ;
* naddrs = 0 ;
rc = krb5_krbhst_init ( ctx , realm - > data , KRB5_KRBHST_KDC , & hnd ) ;
if ( rc ) {
2007-03-28 23:47:59 +04:00
DEBUG ( 0 , ( " smb_krb5_locate_kdc: krb5_krbhst_init failed (%s) \n " , error_message ( rc ) ) ) ;
2003-01-30 21:01:03 +03:00
return rc ;
}
for ( num_kdcs = 0 ; ( rc = krb5_krbhst_next ( ctx , hnd , & hinfo ) = = 0 ) ; num_kdcs + + )
;
krb5_krbhst_reset ( ctx , hnd ) ;
if ( ! num_kdcs ) {
2007-03-28 23:47:59 +04:00
DEBUG ( 0 , ( " smb_krb5_locate_kdc: zero kdcs found ! \n " ) ) ;
2003-01-30 21:01:03 +03:00
krb5_krbhst_free ( ctx , hnd ) ;
return - 1 ;
}
2004-12-21 00:14:28 +03:00
sa = SMB_MALLOC_ARRAY ( struct sockaddr , num_kdcs ) ;
2003-01-30 21:01:03 +03:00
if ( ! sa ) {
2007-03-28 23:47:59 +04:00
DEBUG ( 0 , ( " smb_krb5_locate_kdc: malloc failed \n " ) ) ;
2003-01-30 21:01:03 +03:00
krb5_krbhst_free ( ctx , hnd ) ;
naddrs = 0 ;
return - 1 ;
}
2004-12-21 00:14:28 +03:00
memset ( sa , ' \0 ' , sizeof ( struct sockaddr ) * num_kdcs ) ;
2003-01-30 21:01:03 +03:00
for ( i = 0 ; i < num_kdcs & & ( rc = krb5_krbhst_next ( ctx , hnd , & hinfo ) = = 0 ) ; i + + ) {
2004-08-25 11:52:23 +04:00
# if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
2004-09-21 14:40:45 +04:00
rc = krb5_krbhst_get_addrinfo ( ctx , hinfo , & ai ) ;
2004-08-25 11:52:23 +04:00
if ( rc ) {
DEBUG ( 0 , ( " krb5_krbhst_get_addrinfo failed: %s \n " , error_message ( rc ) ) ) ;
2004-09-21 14:40:45 +04:00
continue ;
2004-08-25 11:52:23 +04:00
}
# endif
if ( hinfo - > ai & & hinfo - > ai - > ai_family = = AF_INET )
2003-01-30 21:01:03 +03:00
memcpy ( & sa [ i ] , hinfo - > ai - > ai_addr , sizeof ( struct sockaddr ) ) ;
}
krb5_krbhst_free ( ctx , hnd ) ;
* naddrs = num_kdcs ;
* addr_pp = sa ;
return 0 ;
}
2006-10-14 03:43:27 +04:00
# else /* ! defined(KRB5_KRBHST_INIT) */
2007-03-28 23:47:59 +04:00
krb5_error_code smb_krb5_locate_kdc ( krb5_context ctx , const krb5_data * realm ,
2006-10-14 03:43:27 +04:00
struct sockaddr * * addr_pp , int * naddrs , int get_masters )
{
DEBUG ( 0 , ( " unable to explicitly locate the KDC on this platform \n " ) ) ;
return KRB5_KDC_UNREACH ;
}
# endif /* KRB5_KRBHST_INIT */
2007-03-28 23:47:59 +04:00
# else /* ! HAVE_KRB5_LOCATE_KDC */
krb5_error_code smb_krb5_locate_kdc ( krb5_context ctx , const krb5_data * realm ,
struct sockaddr * * addr_pp , int * naddrs , int get_masters )
{
return krb5_locate_kdc ( ctx , realm , addr_pp , naddrs , get_masters ) ;
}
2006-10-14 03:43:27 +04:00
# endif /* HAVE_KRB5_LOCATE_KDC */
2003-01-30 21:01:03 +03:00
2004-06-19 04:55:29 +04:00
# if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
void krb5_free_unparsed_name ( krb5_context context , char * val )
{
SAFE_FREE ( val ) ;
}
# endif
2004-11-01 22:35:55 +03:00
void kerberos_free_data_contents ( krb5_context context , krb5_data * pdata )
2004-10-30 02:38:10 +04:00
{
2004-10-31 14:20:07 +03:00
# if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
2004-10-30 02:38:10 +04:00
if ( pdata - > data ) {
krb5_free_data_contents ( context , pdata ) ;
}
# else
SAFE_FREE ( pdata - > data ) ;
# endif
}
2004-11-01 22:35:55 +03:00
void kerberos_set_creds_enctype ( krb5_creds * pcreds , int enctype )
2004-10-29 03:50:14 +04:00
{
# if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
KRB5_KEY_TYPE ( ( & pcreds - > keyblock ) ) = enctype ;
# elif defined(HAVE_KRB5_SESSION_IN_CREDS)
KRB5_KEY_TYPE ( ( & pcreds - > session ) ) = enctype ;
# else
# error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
# endif
}
2007-10-19 04:40:25 +04:00
bool kerberos_compatible_enctypes ( krb5_context context ,
2004-10-29 04:02:32 +04:00
krb5_enctype enctype1 ,
krb5_enctype enctype2 )
{
# if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
krb5_boolean similar = 0 ;
krb5_c_enctype_compare ( context , enctype1 , enctype2 , & similar ) ;
2004-10-30 02:38:10 +04:00
return similar ? True : False ;
2004-10-29 04:02:32 +04:00
# elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
2004-10-30 02:38:10 +04:00
return krb5_enctypes_compatible_keys ( context , enctype1 , enctype2 ) ? True : False ;
2004-10-29 04:02:32 +04:00
# endif
}
2007-10-19 04:40:25 +04:00
static bool ads_cleanup_expired_creds ( krb5_context context ,
2004-04-14 23:06:45 +04:00
krb5_ccache ccache ,
krb5_creds * credsp )
{
krb5_error_code retval ;
2006-02-04 01:19:41 +03:00
const char * cc_type = krb5_cc_get_type ( context , ccache ) ;
2004-04-14 23:06:45 +04:00
2006-02-04 01:19:41 +03:00
DEBUG ( 3 , ( " ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s \n " ,
cc_type , krb5_cc_get_name ( context , ccache ) ,
2008-10-12 01:57:44 +04:00
http_timestring ( talloc_tos ( ) , credsp - > times . endtime ) ) ) ;
2004-04-14 23:06:45 +04:00
/* we will probably need new tickets if the current ones
will expire within 10 seconds .
*/
if ( credsp - > times . endtime > = ( time ( NULL ) + 10 ) )
return False ;
/* heimdal won't remove creds from a file ccache, and
perhaps we shouldn ' t anyway , since internally we
use memory ccaches , and a FILE one probably means that
we ' re using creds obtained outside of our exectuable
*/
2006-03-17 13:22:13 +03:00
if ( strequal ( cc_type , " FILE " ) ) {
2006-02-04 01:19:41 +03:00
DEBUG ( 5 , ( " ads_cleanup_expired_creds: We do not remove creds from a %s ccache \n " , cc_type ) ) ;
2004-04-14 23:06:45 +04:00
return False ;
}
2006-02-04 01:19:41 +03:00
2004-04-14 23:06:45 +04:00
retval = krb5_cc_remove_cred ( context , ccache , 0 , credsp ) ;
if ( retval ) {
2004-11-05 02:56:23 +03:00
DEBUG ( 1 , ( " ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s \n " ,
2004-04-14 23:06:45 +04:00
error_message ( retval ) ) ) ;
/* If we have an error in this, we want to display it,
but continue as though we deleted it */
}
return True ;
}
2001-10-11 14:29:17 +04:00
/*
we can ' t use krb5_mk_req because w2k wants the service to be in a particular format
*/
2003-05-31 00:11:34 +04:00
static krb5_error_code ads_krb5_mk_req ( krb5_context context ,
krb5_auth_context * auth_context ,
const krb5_flags ap_req_options ,
const char * principal ,
krb5_ccache ccache ,
2007-02-08 20:02:39 +03:00
krb5_data * outbuf ,
time_t * expire_time )
2001-10-11 11:42:52 +04:00
{
2001-10-11 17:13:06 +04:00
krb5_error_code retval ;
krb5_principal server ;
krb5_creds * credsp ;
krb5_creds creds ;
krb5_data in_data ;
2007-10-19 04:40:25 +04:00
bool creds_ready = False ;
2006-01-19 01:40:00 +03:00
int i = 0 , maxtries = 3 ;
2001-10-11 17:13:06 +04:00
2008-08-09 01:32:15 +04:00
ZERO_STRUCT ( in_data ) ;
2006-04-24 19:57:54 +04:00
retval = smb_krb5_parse_name ( context , principal , & server ) ;
2001-10-11 17:13:06 +04:00
if ( retval ) {
2004-11-05 02:56:23 +03:00
DEBUG ( 1 , ( " ads_krb5_mk_req: Failed to parse principal %s \n " , principal ) ) ;
2001-10-11 17:13:06 +04:00
return retval ;
}
/* obtain ticket & session key */
2003-06-10 07:48:09 +04:00
ZERO_STRUCT ( creds ) ;
2001-10-20 10:50:24 +04:00
if ( ( retval = krb5_copy_principal ( context , server , & creds . server ) ) ) {
2006-02-04 01:19:41 +03:00
DEBUG ( 1 , ( " ads_krb5_mk_req: krb5_copy_principal failed (%s) \n " ,
2001-10-20 10:50:24 +04:00
error_message ( retval ) ) ) ;
2001-10-11 17:13:06 +04:00
goto cleanup_princ ;
2001-10-20 10:50:24 +04:00
}
2001-10-11 17:13:06 +04:00
2001-10-20 10:50:24 +04:00
if ( ( retval = krb5_cc_get_principal ( context , ccache , & creds . client ) ) ) {
2004-11-05 02:56:23 +03:00
/* This can commonly fail on smbd startup with no ticket in the cache.
* Report at higher level than 1. */
DEBUG ( 3 , ( " ads_krb5_mk_req: krb5_cc_get_principal failed (%s) \n " ,
2001-10-20 10:50:24 +04:00
error_message ( retval ) ) ) ;
2001-10-11 17:13:06 +04:00
goto cleanup_creds ;
2001-10-20 10:50:24 +04:00
}
2001-10-11 17:13:06 +04:00
2006-01-19 01:40:00 +03:00
while ( ! creds_ready & & ( i < maxtries ) ) {
2007-02-08 20:02:39 +03:00
2004-04-14 21:34:48 +04:00
if ( ( retval = krb5_get_credentials ( context , 0 , ccache ,
& creds , & credsp ) ) ) {
2004-11-05 02:56:23 +03:00
DEBUG ( 1 , ( " ads_krb5_mk_req: krb5_get_credentials failed for %s (%s) \n " ,
2004-04-14 21:34:48 +04:00
principal , error_message ( retval ) ) ) ;
goto cleanup_creds ;
}
/* cope with ticket being in the future due to clock skew */
if ( ( unsigned ) credsp - > times . starttime > time ( NULL ) ) {
time_t t = time ( NULL ) ;
2005-04-19 23:23:49 +04:00
int time_offset = ( int ) ( ( unsigned ) credsp - > times . starttime - t ) ;
2004-11-05 02:56:23 +03:00
DEBUG ( 4 , ( " ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew \n " , time_offset ) ) ;
2004-04-14 21:34:48 +04:00
krb5_set_real_time ( context , t + time_offset + 1 , 0 ) ;
}
2007-02-08 20:02:39 +03:00
if ( ! ads_cleanup_expired_creds ( context , ccache , credsp ) ) {
2004-04-14 23:06:45 +04:00
creds_ready = True ;
2007-02-08 20:02:39 +03:00
}
2006-01-19 01:40:00 +03:00
i + + ;
2001-10-11 17:13:06 +04:00
}
2001-10-11 11:42:52 +04:00
2006-02-04 01:19:41 +03:00
DEBUG ( 10 , ( " ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u) \n " ,
principal , krb5_cc_get_type ( context , ccache ) , krb5_cc_get_name ( context , ccache ) ,
2008-10-12 01:57:44 +04:00
http_timestring ( talloc_tos ( ) , ( unsigned ) credsp - > times . endtime ) ,
2004-04-14 21:34:48 +04:00
( unsigned ) credsp - > times . endtime ) ) ;
2002-09-25 19:19:00 +04:00
2007-02-08 20:02:39 +03:00
if ( expire_time ) {
* expire_time = ( time_t ) credsp - > times . endtime ;
}
2008-08-09 03:08:11 +04:00
# 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)
2008-08-09 01:32:15 +04:00
if ( credsp - > ticket_flags & TKT_FLG_OK_AS_DELEGATE ) {
/* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
as part of the kerberos exchange . */
DEBUG ( 3 , ( " ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT \n " ) ) ;
if ( * auth_context = = NULL ) {
/* Allocate if it has not yet been allocated. */
retval = krb5_auth_con_init ( context , auth_context ) ;
if ( retval ) {
DEBUG ( 1 , ( " ads_krb5_mk_req: krb5_auth_con_init failed (%s) \n " ,
error_message ( retval ) ) ) ;
goto cleanup_creds ;
}
}
retval = krb5_auth_con_setuseruserkey ( context , * auth_context , & credsp - > keyblock ) ;
if ( retval ) {
DEBUG ( 1 , ( " ads_krb5_mk_req: krb5_auth_con_setuseruserkey failed (%s) \n " ,
error_message ( retval ) ) ) ;
goto cleanup_creds ;
}
/* Must use a subkey for forwarded tickets. */
retval = krb5_auth_con_setflags ( context , * auth_context , KRB5_AUTH_CONTEXT_USE_SUBKEY ) ;
if ( retval ) {
DEBUG ( 1 , ( " ads_krb5_mk_req: krb5_auth_con_setflags failed (%s) \n " ,
error_message ( retval ) ) ) ;
goto cleanup_creds ;
}
retval = ads_krb5_get_fwd_ticket ( context ,
auth_context ,
credsp ,
ccache ,
& in_data ) ;
if ( retval ) {
2008-09-10 21:18:02 +04:00
DEBUG ( 3 , ( " ads_krb5_get_fwd_ticket failed (%s) \n " ,
2008-08-31 13:45:12 +04:00
error_message ( retval ) ) ) ;
2008-09-10 21:18:02 +04:00
/*
* This is not fatal . Delete the * auth_context and continue
* with krb5_mk_req_extended to get a non - forwardable ticket .
*/
if ( in_data . data ) {
free ( in_data . data ) ;
in_data . data = NULL ;
in_data . length = 0 ;
}
krb5_auth_con_free ( context , * auth_context ) ;
* auth_context = NULL ;
2008-08-09 01:32:15 +04:00
}
}
# endif
2001-10-11 17:13:06 +04:00
retval = krb5_mk_req_extended ( context , auth_context , ap_req_options ,
& in_data , credsp , outbuf ) ;
if ( retval ) {
2004-11-05 02:56:23 +03:00
DEBUG ( 1 , ( " ads_krb5_mk_req: krb5_mk_req_extended failed (%s) \n " ,
2001-10-20 10:50:24 +04:00
error_message ( retval ) ) ) ;
2001-10-11 17:13:06 +04:00
}
2008-08-09 01:32:15 +04:00
if ( in_data . data ) {
free ( in_data . data ) ;
in_data . length = 0 ;
}
2001-10-11 17:13:06 +04:00
krb5_free_creds ( context , credsp ) ;
2001-10-11 11:42:52 +04:00
cleanup_creds :
2001-10-11 17:13:06 +04:00
krb5_free_cred_contents ( context , & creds ) ;
2001-10-11 11:42:52 +04:00
cleanup_princ :
2001-10-11 17:13:06 +04:00
krb5_free_principal ( context , server ) ;
2001-10-11 11:42:52 +04:00
2001-10-11 17:13:06 +04:00
return retval ;
2001-10-11 11:42:52 +04:00
}
/*
get a kerberos5 ticket for the given service
*/
2004-01-08 11:19:18 +03:00
int cli_krb5_get_ticket ( const char * principal , time_t time_offset ,
2006-02-04 01:19:41 +03:00
DATA_BLOB * ticket , DATA_BLOB * session_key_krb5 ,
2007-02-08 20:02:39 +03:00
uint32 extra_ap_opts , const char * ccname ,
time_t * tgs_expire )
2001-10-11 11:42:52 +04:00
{
krb5_error_code retval ;
2001-10-11 17:13:06 +04:00
krb5_data packet ;
2004-05-07 06:48:03 +04:00
krb5_context context = NULL ;
2004-05-06 22:23:01 +04:00
krb5_ccache ccdef = NULL ;
2001-10-11 11:42:52 +04:00
krb5_auth_context auth_context = NULL ;
2007-01-30 00:15:25 +03:00
krb5_enctype enc_types [ ] = {
# ifdef ENCTYPE_ARCFOUR_HMAC
ENCTYPE_ARCFOUR_HMAC ,
# endif
ENCTYPE_DES_CBC_MD5 ,
ENCTYPE_DES_CBC_CRC ,
ENCTYPE_NULL } ;
2006-02-04 01:19:41 +03:00
2005-11-07 17:16:50 +03:00
initialize_krb5_error_table ( ) ;
2001-10-11 11:42:52 +04:00
retval = krb5_init_context ( & context ) ;
if ( retval ) {
2004-11-05 02:56:23 +03:00
DEBUG ( 1 , ( " cli_krb5_get_ticket: krb5_init_context failed (%s) \n " ,
2001-10-20 10:50:24 +04:00
error_message ( retval ) ) ) ;
2001-10-11 11:42:52 +04:00
goto failed ;
}
2002-09-25 19:19:00 +04:00
if ( time_offset ! = 0 ) {
krb5_set_real_time ( context , time ( NULL ) + time_offset , 0 ) ;
}
2006-02-04 01:19:41 +03:00
if ( ( retval = krb5_cc_resolve ( context , ccname ?
ccname : krb5_cc_default_name ( context ) , & ccdef ) ) ) {
2004-11-05 02:56:23 +03:00
DEBUG ( 1 , ( " cli_krb5_get_ticket: krb5_cc_default failed (%s) \n " ,
2001-10-20 10:50:24 +04:00
error_message ( retval ) ) ) ;
2001-10-11 11:42:52 +04:00
goto failed ;
}
2007-01-30 00:15:25 +03:00
if ( ( retval = krb5_set_default_tgs_ktypes ( context , enc_types ) ) ) {
DEBUG ( 1 , ( " cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s) \n " ,
error_message ( retval ) ) ) ;
goto failed ;
}
2003-05-31 00:11:34 +04:00
if ( ( retval = ads_krb5_mk_req ( context ,
2003-07-26 03:15:30 +04:00
& auth_context ,
2005-09-30 21:13:37 +04:00
AP_OPTS_USE_SUBKEY | ( krb5_flags ) extra_ap_opts ,
2003-07-26 03:15:30 +04:00
principal ,
2007-02-08 20:02:39 +03:00
ccdef , & packet ,
tgs_expire ) ) ) {
2001-10-11 11:42:52 +04:00
goto failed ;
}
2003-07-26 03:15:30 +04:00
get_krb5_smb_session_key ( context , auth_context , session_key_krb5 , False ) ;
2004-01-08 11:19:18 +03:00
* ticket = data_blob ( packet . data , packet . length ) ;
2004-10-30 02:38:10 +04:00
kerberos_free_data_contents ( context , & packet ) ;
2001-10-11 11:42:52 +04:00
failed :
2004-05-06 22:23:01 +04:00
if ( context ) {
if ( ccdef )
krb5_cc_close ( context , ccdef ) ;
if ( auth_context )
krb5_auth_con_free ( context , auth_context ) ;
2002-11-15 20:57:25 +03:00
krb5_free_context ( context ) ;
2004-05-06 22:23:01 +04:00
}
2002-11-15 20:57:25 +03:00
2004-01-08 11:19:18 +03:00
return retval ;
2001-10-11 11:42:52 +04:00
}
2007-10-19 04:40:25 +04:00
bool get_krb5_smb_session_key ( krb5_context context , krb5_auth_context auth_context , DATA_BLOB * session_key , bool remote )
2003-03-18 01:45:16 +03:00
{
2009-03-19 18:42:54 +03:00
krb5_keyblock * skey = NULL ;
krb5_error_code err = 0 ;
bool ret = false ;
2003-03-18 01:45:16 +03:00
2009-03-19 18:42:54 +03:00
if ( remote ) {
2003-07-26 03:15:30 +04:00
err = krb5_auth_con_getremotesubkey ( context , auth_context , & skey ) ;
2009-03-19 18:42:54 +03:00
} else {
2003-07-26 03:15:30 +04:00
err = krb5_auth_con_getlocalsubkey ( context , auth_context , & skey ) ;
2009-03-19 18:42:54 +03:00
}
if ( err | | skey = = NULL ) {
DEBUG ( 10 , ( " KRB5 error getting session key %d \n " , err ) ) ;
goto done ;
}
DEBUG ( 10 , ( " Got KRB5 session key of length %d \n " , ( int ) KRB5_KEY_LENGTH ( skey ) ) ) ;
* session_key = data_blob ( KRB5_KEY_DATA ( skey ) , KRB5_KEY_LENGTH ( skey ) ) ;
dump_data_pw ( " KRB5 Session Key: \n " , session_key - > data , session_key - > length ) ;
Changes all over the shop, but all towards:
- NTLM2 support in the server
- KEY_EXCH support in the server
- variable length session keys.
In detail:
- NTLM2 is an extension of NTLMv1, that is compatible with existing
domain controllers (unlike NTLMv2, which requires a DC upgrade).
* This is known as 'NTLMv2 session security' *
(This is not yet implemented on the RPC pipes however, so there may
well still be issues for PDC setups, particuarly around password
changes. We do not fully understand the sign/seal implications of
NTLM2 on RPC pipes.)
This requires modifications to our authentication subsystem, as we
must handle the 'challege' input into the challenge-response algorithm
being changed. This also needs to be turned off for
'security=server', which does not support this.
- KEY_EXCH is another 'security' mechanism, whereby the session key
actually used by the server is sent by the client, rather than being
the shared-secret directly or indirectly.
- As both these methods change the session key, the auth subsystem
needed to be changed, to 'override' session keys provided by the
backend.
- There has also been a major overhaul of the NTLMSSP subsystem, to merge the 'client' and 'server' functions, so they both operate on a single structure. This should help the SPNEGO implementation.
- The 'names blob' in NTLMSSP is always in unicode - never in ascii.
Don't make an ascii version ever.
- The other big change is to allow variable length session keys. We
have always assumed that session keys are 16 bytes long - and padded
to this length if shorter. However, Kerberos session keys are 8 bytes
long, when the krb5 login uses DES.
* This fix allows SMB signging on machines not yet running MIT KRB5 1.3.1. *
- Add better DEBUG() messages to ntlm_auth, warning administrators of
misconfigurations that prevent access to the privileged pipe. This
should help reduce some of the 'it just doesn't work' issues.
- Fix data_blob_talloc() to behave the same way data_blob() does when
passed a NULL data pointer. (just allocate)
REMEMBER to make clean after this commit - I have changed plenty of data structures...
(This used to be commit f3bbc87b0dac63426cda6fac7a295d3aad810ecc)
2003-11-22 16:19:38 +03:00
2009-03-19 18:42:54 +03:00
ret = true ;
Changes all over the shop, but all towards:
- NTLM2 support in the server
- KEY_EXCH support in the server
- variable length session keys.
In detail:
- NTLM2 is an extension of NTLMv1, that is compatible with existing
domain controllers (unlike NTLMv2, which requires a DC upgrade).
* This is known as 'NTLMv2 session security' *
(This is not yet implemented on the RPC pipes however, so there may
well still be issues for PDC setups, particuarly around password
changes. We do not fully understand the sign/seal implications of
NTLM2 on RPC pipes.)
This requires modifications to our authentication subsystem, as we
must handle the 'challege' input into the challenge-response algorithm
being changed. This also needs to be turned off for
'security=server', which does not support this.
- KEY_EXCH is another 'security' mechanism, whereby the session key
actually used by the server is sent by the client, rather than being
the shared-secret directly or indirectly.
- As both these methods change the session key, the auth subsystem
needed to be changed, to 'override' session keys provided by the
backend.
- There has also been a major overhaul of the NTLMSSP subsystem, to merge the 'client' and 'server' functions, so they both operate on a single structure. This should help the SPNEGO implementation.
- The 'names blob' in NTLMSSP is always in unicode - never in ascii.
Don't make an ascii version ever.
- The other big change is to allow variable length session keys. We
have always assumed that session keys are 16 bytes long - and padded
to this length if shorter. However, Kerberos session keys are 8 bytes
long, when the krb5 login uses DES.
* This fix allows SMB signging on machines not yet running MIT KRB5 1.3.1. *
- Add better DEBUG() messages to ntlm_auth, warning administrators of
misconfigurations that prevent access to the privileged pipe. This
should help reduce some of the 'it just doesn't work' issues.
- Fix data_blob_talloc() to behave the same way data_blob() does when
passed a NULL data pointer. (just allocate)
REMEMBER to make clean after this commit - I have changed plenty of data structures...
(This used to be commit f3bbc87b0dac63426cda6fac7a295d3aad810ecc)
2003-11-22 16:19:38 +03:00
2009-03-19 18:42:54 +03:00
done :
if ( skey ) {
2003-03-18 01:45:16 +03:00
krb5_free_keyblock ( context , skey ) ;
}
return ret ;
}
2003-07-16 23:17:33 +04:00
# if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
2005-05-02 21:49:43 +04:00
const krb5_data * krb5_princ_component ( krb5_context context , krb5_principal principal , int i ) ;
2003-07-16 23:17:33 +04:00
const krb5_data * krb5_princ_component ( krb5_context context , krb5_principal principal , int i )
{
static krb5_data kdata ;
2005-05-02 21:49:43 +04:00
kdata . data = ( char * ) krb5_principal_get_comp_string ( context , principal , i ) ;
2006-10-20 01:47:11 +04:00
kdata . length = strlen ( ( const char * ) kdata . data ) ;
2003-07-16 23:17:33 +04:00
return & kdata ;
}
# endif
2004-07-08 22:14:07 +04:00
krb5_error_code smb_krb5_kt_free_entry ( krb5_context context , krb5_keytab_entry * kt_entry )
2004-06-24 09:56:44 +04:00
{
# if defined(HAVE_KRB5_KT_FREE_ENTRY)
return krb5_kt_free_entry ( context , kt_entry ) ;
# elif defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
return krb5_free_keytab_entry_contents ( context , kt_entry ) ;
# else
# error UNKNOWN_KT_FREE_FUNCTION
# endif
}
2008-02-17 03:59:02 +03:00
void smb_krb5_checksum_from_pac_sig ( krb5_checksum * cksum ,
struct PAC_SIGNATURE_DATA * sig )
2005-09-30 21:13:37 +04:00
{
# ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
cksum - > cksumtype = ( krb5_cksumtype ) sig - > type ;
2008-02-17 03:59:02 +03:00
cksum - > checksum . length = sig - > signature . length ;
cksum - > checksum . data = sig - > signature . data ;
2005-09-30 21:13:37 +04:00
# else
cksum - > checksum_type = ( krb5_cksumtype ) sig - > type ;
2008-02-17 03:59:02 +03:00
cksum - > length = sig - > signature . length ;
cksum - > contents = sig - > signature . data ;
2005-09-30 21:13:37 +04:00
# endif
}
2005-10-01 13:43:53 +04:00
krb5_error_code smb_krb5_verify_checksum ( krb5_context context ,
2008-02-17 03:59:02 +03:00
const krb5_keyblock * keyblock ,
2005-09-30 21:13:37 +04:00
krb5_keyusage usage ,
krb5_checksum * cksum ,
uint8 * data ,
size_t length )
{
krb5_error_code ret ;
/* verify the checksum */
/* welcome to the wonderful world of samba's kerberos abstraction layer:
*
* function heimdal 0.6 .1 rc3 heimdal 0.7 MIT krb 1.4 .2
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* krb5_c_verify_checksum - works works
* krb5_verify_checksum works ( 6 args ) works ( 6 args ) broken ( 7 args )
*/
# if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
{
krb5_boolean checksum_valid = False ;
krb5_data input ;
input . data = ( char * ) data ;
input . length = length ;
ret = krb5_c_verify_checksum ( context ,
keyblock ,
usage ,
& input ,
cksum ,
& checksum_valid ) ;
2005-10-11 20:27:05 +04:00
if ( ret ) {
DEBUG ( 3 , ( " smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s \n " ,
error_message ( ret ) ) ) ;
return ret ;
}
2005-09-30 21:13:37 +04:00
if ( ! checksum_valid )
ret = KRB5KRB_AP_ERR_BAD_INTEGRITY ;
}
# elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
/* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
* without enctype and it ignores any key_usage types - Guenther */
{
krb5_crypto crypto ;
ret = krb5_crypto_init ( context ,
keyblock ,
0 ,
& crypto ) ;
if ( ret ) {
DEBUG ( 0 , ( " smb_krb5_verify_checksum: krb5_crypto_init() failed: %s \n " ,
error_message ( ret ) ) ) ;
return ret ;
}
ret = krb5_verify_checksum ( context ,
crypto ,
usage ,
data ,
length ,
cksum ) ;
krb5_crypto_destroy ( context , crypto ) ;
}
# else
# error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
# endif
return ret ;
}
2005-10-01 13:43:53 +04:00
time_t get_authtime_from_tkt ( krb5_ticket * tkt )
2005-09-30 21:13:37 +04:00
{
# if defined(HAVE_KRB5_TKT_ENC_PART2)
return tkt - > enc_part2 - > times . authtime ;
# else
return tkt - > ticket . authtime ;
# endif
}
2006-11-29 00:11:20 +03:00
# ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
2005-09-30 21:13:37 +04:00
static int get_kvno_from_ap_req ( krb5_ap_req * ap_req )
{
# ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
if ( ap_req - > ticket - > enc_part . kvno )
return ap_req - > ticket - > enc_part . kvno ;
# else /* Heimdal */
if ( ap_req - > ticket . enc_part . kvno )
return * ap_req - > ticket . enc_part . kvno ;
# endif
return 0 ;
}
static krb5_enctype get_enctype_from_ap_req ( krb5_ap_req * ap_req )
{
# ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
return ap_req - > ticket . enc_part . etype ;
# else /* MIT */
return ap_req - > ticket - > enc_part . enctype ;
# endif
}
2006-11-29 00:11:20 +03:00
# endif /* HAVE_KRB5_DECODE_AP_REQ */
2005-09-30 21:13:37 +04:00
static krb5_error_code
get_key_from_keytab ( krb5_context context ,
krb5_const_principal server ,
krb5_enctype enctype ,
krb5_kvno kvno ,
krb5_keyblock * * out_key )
{
krb5_keytab_entry entry ;
krb5_error_code ret ;
2006-01-18 22:25:18 +03:00
krb5_keytab keytab ;
2005-09-30 21:13:37 +04:00
char * name = NULL ;
2008-08-22 16:58:01 +04:00
krb5_keyblock * keyp ;
2005-09-30 21:13:37 +04:00
2006-01-18 22:25:18 +03:00
/* We have to open a new keytab handle here, as MIT does
an implicit open / getnext / close on krb5_kt_get_entry . We
may be in the middle of a keytab enumeration when this is
called . JRA . */
2007-06-29 13:54:39 +04:00
ret = smb_krb5_open_keytab ( context , NULL , False , & keytab ) ;
2006-01-18 22:25:18 +03:00
if ( ret ) {
2007-06-29 13:54:39 +04:00
DEBUG ( 1 , ( " get_key_from_keytab: smb_krb5_open_keytab failed (%s) \n " , error_message ( ret ) ) ) ;
2007-06-29 13:58:11 +04:00
return ret ;
2005-09-30 21:13:37 +04:00
}
if ( DEBUGLEVEL > = 10 ) {
2009-03-18 08:23:27 +03:00
if ( smb_krb5_unparse_name ( talloc_tos ( ) , context , server , & name ) = = 0 ) {
2006-04-24 19:57:54 +04:00
DEBUG ( 10 , ( " get_key_from_keytab: will look for kvno %d, enctype %d and name: %s \n " ,
kvno , enctype , name ) ) ;
2009-03-18 08:23:27 +03:00
TALLOC_FREE ( name ) ;
2006-04-24 19:57:54 +04:00
}
2005-09-30 21:13:37 +04:00
}
ret = krb5_kt_get_entry ( context ,
2006-01-18 22:25:18 +03:00
keytab ,
2005-09-30 21:13:37 +04:00
server ,
kvno ,
enctype ,
& entry ) ;
if ( ret ) {
DEBUG ( 0 , ( " get_key_from_keytab: failed to retrieve key: %s \n " , error_message ( ret ) ) ) ;
goto out ;
}
2008-08-22 16:58:01 +04:00
keyp = KRB5_KT_KEY ( & entry ) ;
2005-09-30 21:13:37 +04:00
2008-08-22 16:58:01 +04:00
ret = krb5_copy_keyblock ( context , keyp , out_key ) ;
2005-09-30 21:13:37 +04:00
if ( ret ) {
DEBUG ( 0 , ( " get_key_from_keytab: failed to copy key: %s \n " , error_message ( ret ) ) ) ;
goto out ;
}
smb_krb5_kt_free_entry ( context , & entry ) ;
out :
2006-01-18 22:25:18 +03:00
krb5_kt_close ( context , keytab ) ;
2005-09-30 21:13:37 +04:00
return ret ;
}
/* Prototypes */
2005-10-01 13:43:53 +04:00
krb5_error_code smb_krb5_get_keyinfo_from_ap_req ( krb5_context context ,
2005-09-30 21:13:37 +04:00
const krb5_data * inbuf ,
krb5_kvno * kvno ,
krb5_enctype * enctype )
{
# ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
{
2006-10-14 03:43:27 +04:00
krb5_error_code ret ;
2005-09-30 21:13:37 +04:00
krb5_ap_req ap_req ;
ret = krb5_decode_ap_req ( context , inbuf , & ap_req ) ;
if ( ret )
return ret ;
* kvno = get_kvno_from_ap_req ( & ap_req ) ;
* enctype = get_enctype_from_ap_req ( & ap_req ) ;
2006-10-17 19:57:00 +04:00
free_AP_REQ ( & ap_req ) ;
2006-10-14 03:43:27 +04:00
return 0 ;
2005-09-30 21:13:37 +04:00
}
# endif
2006-10-14 03:43:27 +04:00
/* Possibly not an appropriate error code. */
return KRB5KDC_ERR_BADOPTION ;
2005-09-30 21:13:37 +04:00
}
2005-10-01 13:43:53 +04:00
krb5_error_code krb5_rd_req_return_keyblock_from_keytab ( krb5_context context ,
2005-09-30 21:13:37 +04:00
krb5_auth_context * auth_context ,
const krb5_data * inbuf ,
krb5_const_principal server ,
krb5_keytab keytab ,
krb5_flags * ap_req_options ,
krb5_ticket * * ticket ,
krb5_keyblock * * keyblock )
{
krb5_error_code ret ;
krb5_kvno kvno ;
krb5_enctype enctype ;
krb5_keyblock * local_keyblock ;
ret = krb5_rd_req ( context ,
auth_context ,
inbuf ,
server ,
keytab ,
ap_req_options ,
ticket ) ;
if ( ret ) {
return ret ;
}
2006-10-14 03:43:27 +04:00
# ifdef KRB5_TICKET_HAS_KEYINFO
enctype = ( * ticket ) - > enc_part . enctype ;
kvno = ( * ticket ) - > enc_part . kvno ;
# else
2005-09-30 21:13:37 +04:00
ret = smb_krb5_get_keyinfo_from_ap_req ( context , inbuf , & kvno , & enctype ) ;
if ( ret ) {
return ret ;
}
2006-10-14 03:43:27 +04:00
# endif
2005-09-30 21:13:37 +04:00
ret = get_key_from_keytab ( context ,
server ,
enctype ,
kvno ,
& local_keyblock ) ;
if ( ret ) {
DEBUG ( 0 , ( " krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab \n " ) ) ;
goto out ;
}
out :
if ( ret & & local_keyblock ! = NULL ) {
krb5_free_keyblock ( context , local_keyblock ) ;
} else {
* keyblock = local_keyblock ;
}
return ret ;
}
2005-10-01 13:43:53 +04:00
krb5_error_code smb_krb5_parse_name_norealm ( krb5_context context ,
2005-09-30 21:13:37 +04:00
const char * name ,
krb5_principal * principal )
{
# ifdef HAVE_KRB5_PARSE_NAME_NOREALM
2006-04-24 19:57:54 +04:00
return smb_krb5_parse_name_norealm_conv ( context , name , principal ) ;
2005-09-30 21:13:37 +04:00
# endif
/* we are cheating here because parse_name will in fact set the realm.
* We don ' t care as the only caller of smb_krb5_parse_name_norealm
* ignores the realm anyway when calling
* smb_krb5_principal_compare_any_realm later - Guenther */
2006-04-24 19:57:54 +04:00
return smb_krb5_parse_name ( context , name , principal ) ;
2005-09-30 21:13:37 +04:00
}
2007-10-19 04:40:25 +04:00
bool smb_krb5_principal_compare_any_realm ( krb5_context context ,
2005-09-30 21:13:37 +04:00
krb5_const_principal princ1 ,
krb5_const_principal princ2 )
{
# ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
return krb5_principal_compare_any_realm ( context , princ1 , princ2 ) ;
/* krb5_princ_size is a macro in MIT */
# elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
int i , len1 , len2 ;
const krb5_data * p1 , * p2 ;
len1 = krb5_princ_size ( context , princ1 ) ;
len2 = krb5_princ_size ( context , princ2 ) ;
if ( len1 ! = len2 )
return False ;
for ( i = 0 ; i < len1 ; i + + ) {
p1 = krb5_princ_component ( context , CONST_DISCARD ( krb5_principal , princ1 ) , i ) ;
p2 = krb5_princ_component ( context , CONST_DISCARD ( krb5_principal , princ2 ) , i ) ;
if ( p1 - > length ! = p2 - > length | | memcmp ( p1 - > data , p2 - > data , p1 - > length ) )
return False ;
}
return True ;
# else
# error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
# endif
2006-02-04 01:19:41 +03:00
}
krb5_error_code smb_krb5_renew_ticket ( const char * ccache_string , /* FILE:/tmp/krb5cc_0 */
const char * client_string , /* gd@BER.SUSE.DE */
const char * service_string , /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
2007-06-22 15:20:37 +04:00
time_t * expire_time )
2006-02-04 01:19:41 +03:00
{
krb5_error_code ret ;
krb5_context context = NULL ;
krb5_ccache ccache = NULL ;
krb5_principal client = NULL ;
2007-06-26 14:19:06 +04:00
krb5_creds creds , creds_in , * creds_out = NULL ;
ZERO_STRUCT ( creds ) ;
ZERO_STRUCT ( creds_in ) ;
2006-02-04 01:19:41 +03:00
initialize_krb5_error_table ( ) ;
ret = krb5_init_context ( & context ) ;
if ( ret ) {
goto done ;
}
if ( ! ccache_string ) {
ccache_string = krb5_cc_default_name ( context ) ;
}
2007-06-22 18:54:39 +04:00
if ( ! ccache_string ) {
ret = EINVAL ;
goto done ;
}
2006-02-04 01:19:41 +03:00
DEBUG ( 10 , ( " smb_krb5_renew_ticket: using %s as ccache \n " , ccache_string ) ) ;
/* FIXME: we should not fall back to defaults */
ret = krb5_cc_resolve ( context , CONST_DISCARD ( char * , ccache_string ) , & ccache ) ;
if ( ret ) {
goto done ;
}
2007-06-22 18:50:15 +04:00
if ( client_string ) {
ret = smb_krb5_parse_name ( context , client_string , & client ) ;
if ( ret ) {
goto done ;
}
} else {
ret = krb5_cc_get_principal ( context , ccache , & client ) ;
if ( ret ) {
goto done ;
}
}
2006-02-04 01:19:41 +03:00
# ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
{
ret = krb5_get_renewed_creds ( context , & creds , client , ccache , CONST_DISCARD ( char * , service_string ) ) ;
if ( ret ) {
DEBUG ( 10 , ( " smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s \n " , error_message ( ret ) ) ) ;
goto done ;
}
}
# elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
{
krb5_kdc_flags flags ;
2007-06-26 14:19:06 +04:00
krb5_realm * client_realm = NULL ;
2006-02-04 01:19:41 +03:00
2007-06-22 18:50:15 +04:00
ret = krb5_copy_principal ( context , client , & creds_in . client ) ;
if ( ret ) {
goto done ;
2006-02-04 01:19:41 +03:00
}
if ( service_string ) {
2006-04-24 19:57:54 +04:00
ret = smb_krb5_parse_name ( context , service_string , & creds_in . server ) ;
2006-02-04 01:19:41 +03:00
if ( ret ) {
goto done ;
}
} else {
/* build tgt service by default */
2007-06-22 18:43:42 +04:00
client_realm = krb5_princ_realm ( context , creds_in . client ) ;
if ( ! client_realm ) {
ret = ENOMEM ;
goto done ;
}
2006-02-04 01:19:41 +03:00
ret = krb5_make_principal ( context , & creds_in . server , * client_realm , KRB5_TGS_NAME , * client_realm , NULL ) ;
if ( ret ) {
goto done ;
}
}
flags . i = 0 ;
flags . b . renewable = flags . b . renew = True ;
2007-06-26 14:19:06 +04:00
ret = krb5_get_kdc_cred ( context , ccache , flags , NULL , NULL , & creds_in , & creds_out ) ;
2006-02-04 01:19:41 +03:00
if ( ret ) {
DEBUG ( 10 , ( " smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s \n " , error_message ( ret ) ) ) ;
goto done ;
}
2007-06-26 14:19:06 +04:00
creds = * creds_out ;
2006-02-04 01:19:41 +03:00
}
# else
2007-06-22 18:54:39 +04:00
# error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
2006-02-04 01:19:41 +03:00
# endif
2007-06-26 14:19:06 +04:00
/* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
ret = krb5_cc_initialize ( context , ccache , client ) ;
if ( ret ) {
goto done ;
}
ret = krb5_cc_store_cred ( context , ccache , & creds ) ;
if ( expire_time ) {
* expire_time = ( time_t ) creds . times . endtime ;
}
2006-02-04 01:19:41 +03:00
done :
2007-06-26 14:19:06 +04:00
krb5_free_cred_contents ( context , & creds_in ) ;
if ( creds_out ) {
krb5_free_creds ( context , creds_out ) ;
} else {
krb5_free_cred_contents ( context , & creds ) ;
}
2006-02-04 01:19:41 +03:00
if ( client ) {
krb5_free_principal ( context , client ) ;
}
if ( ccache ) {
krb5_cc_close ( context , ccache ) ;
}
2007-06-22 18:54:39 +04:00
if ( context ) {
krb5_free_context ( context ) ;
}
2006-02-04 01:19:41 +03:00
return ret ;
2005-09-30 21:13:37 +04:00
}
2006-04-25 16:24:25 +04:00
krb5_error_code smb_krb5_free_addresses ( krb5_context context , smb_krb5_addresses * addr )
{
krb5_error_code ret = 0 ;
2006-04-25 16:53:38 +04:00
if ( addr = = NULL ) {
return ret ;
}
2006-04-25 16:24:25 +04:00
# if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
krb5_free_addresses ( context , addr - > addrs ) ;
# elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
ret = krb5_free_addresses ( context , addr - > addrs ) ;
SAFE_FREE ( addr - > addrs ) ;
# endif
SAFE_FREE ( addr ) ;
addr = NULL ;
return ret ;
}
krb5_error_code smb_krb5_gen_netbios_krb5_address ( smb_krb5_addresses * * kerb_addr )
{
krb5_error_code ret = 0 ;
nstring buf ;
# if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
krb5_address * * addrs = NULL ;
# elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
krb5_addresses * addrs = NULL ;
# endif
* kerb_addr = ( smb_krb5_addresses * ) SMB_MALLOC ( sizeof ( smb_krb5_addresses ) ) ;
if ( * kerb_addr = = NULL ) {
return ENOMEM ;
}
put_name ( buf , global_myname ( ) , ' ' , 0x20 ) ;
# if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
{
int num_addr = 2 ;
addrs = ( krb5_address * * ) SMB_MALLOC ( sizeof ( krb5_address * ) * num_addr ) ;
if ( addrs = = NULL ) {
2009-01-20 02:09:51 +03:00
SAFE_FREE ( * kerb_addr ) ;
2006-04-25 16:24:25 +04:00
return ENOMEM ;
}
memset ( addrs , 0 , sizeof ( krb5_address * ) * num_addr ) ;
addrs [ 0 ] = ( krb5_address * ) SMB_MALLOC ( sizeof ( krb5_address ) ) ;
if ( addrs [ 0 ] = = NULL ) {
SAFE_FREE ( addrs ) ;
2009-01-20 02:09:51 +03:00
SAFE_FREE ( * kerb_addr ) ;
2006-04-25 16:24:25 +04:00
return ENOMEM ;
}
addrs [ 0 ] - > magic = KV5M_ADDRESS ;
addrs [ 0 ] - > addrtype = KRB5_ADDR_NETBIOS ;
addrs [ 0 ] - > length = MAX_NETBIOSNAME_LEN ;
addrs [ 0 ] - > contents = ( unsigned char * ) SMB_MALLOC ( addrs [ 0 ] - > length ) ;
if ( addrs [ 0 ] - > contents = = NULL ) {
SAFE_FREE ( addrs [ 0 ] ) ;
SAFE_FREE ( addrs ) ;
2009-01-20 02:09:51 +03:00
SAFE_FREE ( * kerb_addr ) ;
2006-04-25 16:24:25 +04:00
return ENOMEM ;
}
memcpy ( addrs [ 0 ] - > contents , buf , addrs [ 0 ] - > length ) ;
addrs [ 1 ] = NULL ;
}
# elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
{
addrs = ( krb5_addresses * ) SMB_MALLOC ( sizeof ( krb5_addresses ) ) ;
if ( addrs = = NULL ) {
2009-01-20 02:09:51 +03:00
SAFE_FREE ( * kerb_addr ) ;
2006-04-25 16:24:25 +04:00
return ENOMEM ;
}
memset ( addrs , 0 , sizeof ( krb5_addresses ) ) ;
addrs - > len = 1 ;
addrs - > val = ( krb5_address * ) SMB_MALLOC ( sizeof ( krb5_address ) ) ;
if ( addrs - > val = = NULL ) {
SAFE_FREE ( addrs ) ;
2006-04-25 16:53:38 +04:00
SAFE_FREE ( kerb_addr ) ;
2006-04-25 16:24:25 +04:00
return ENOMEM ;
}
addrs - > val [ 0 ] . addr_type = KRB5_ADDR_NETBIOS ;
addrs - > val [ 0 ] . address . length = MAX_NETBIOSNAME_LEN ;
addrs - > val [ 0 ] . address . data = ( unsigned char * ) SMB_MALLOC ( addrs - > val [ 0 ] . address . length ) ;
if ( addrs - > val [ 0 ] . address . data = = NULL ) {
SAFE_FREE ( addrs - > val ) ;
SAFE_FREE ( addrs ) ;
2009-01-20 02:09:51 +03:00
SAFE_FREE ( * kerb_addr ) ;
2006-04-25 16:24:25 +04:00
return ENOMEM ;
}
memcpy ( addrs - > val [ 0 ] . address . data , buf , addrs - > val [ 0 ] . address . length ) ;
}
# else
# error UNKNOWN_KRB5_ADDRESS_FORMAT
# endif
( * kerb_addr ) - > addrs = addrs ;
return ret ;
}
2006-06-16 01:25:57 +04:00
2006-06-16 01:45:10 +04:00
void smb_krb5_free_error ( krb5_context context , krb5_error * krberror )
2006-06-16 01:25:57 +04:00
{
# ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
krb5_free_error_contents ( context , krberror ) ;
# else /* MIT */
krb5_free_error ( context , krberror ) ;
# endif
}
2006-06-16 01:45:10 +04:00
krb5_error_code handle_krberror_packet ( krb5_context context ,
krb5_data * packet )
2006-06-16 01:25:57 +04:00
{
krb5_error_code ret ;
2007-10-19 04:40:25 +04:00
bool got_error_code = False ;
2006-06-16 01:25:57 +04:00
DEBUG ( 10 , ( " handle_krberror_packet: got error packet \n " ) ) ;
# ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
{
krb5_error krberror ;
if ( ( ret = krb5_rd_error ( context , packet , & krberror ) ) ) {
DEBUG ( 10 , ( " handle_krberror_packet: krb5_rd_error failed with: %s \n " ,
error_message ( ret ) ) ) ;
return ret ;
}
if ( krberror . e_data = = NULL | | krberror . e_data - > data = = NULL ) {
ret = ( krb5_error_code ) krberror . error_code ;
got_error_code = True ;
}
smb_krb5_free_error ( context , & krberror ) ;
}
# else /* MIT */
{
krb5_error * krberror ;
if ( ( ret = krb5_rd_error ( context , packet , & krberror ) ) ) {
DEBUG ( 10 , ( " handle_krberror_packet: krb5_rd_error failed with: %s \n " ,
error_message ( ret ) ) ) ;
return ret ;
}
if ( krberror - > e_data . data = = NULL ) {
ret = ERROR_TABLE_BASE_krb5 + ( krb5_error_code ) krberror - > error ;
got_error_code = True ;
}
smb_krb5_free_error ( context , krberror ) ;
}
# endif
if ( got_error_code ) {
DEBUG ( 5 , ( " handle_krberror_packet: got KERBERR from kpasswd: %s (%d) \n " ,
error_message ( ret ) , ret ) ) ;
}
return ret ;
}
2006-04-25 16:24:25 +04:00
2007-03-09 21:51:48 +03:00
krb5_error_code smb_krb5_get_init_creds_opt_alloc ( krb5_context context ,
krb5_get_init_creds_opt * * opt )
2007-02-01 18:10:13 +03:00
{
2007-03-09 21:51:48 +03:00
# ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
/* Heimdal or modern MIT version */
return krb5_get_init_creds_opt_alloc ( context , opt ) ;
# else
/* Historical MIT version */
2007-02-01 18:10:13 +03:00
krb5_get_init_creds_opt * my_opt ;
* opt = NULL ;
2007-05-07 19:07:49 +04:00
if ( ( my_opt = SMB_MALLOC_P ( krb5_get_init_creds_opt ) ) = = NULL ) {
2007-02-01 18:10:13 +03:00
return ENOMEM ;
}
krb5_get_init_creds_opt_init ( my_opt ) ;
* opt = my_opt ;
return 0 ;
2007-03-09 21:51:48 +03:00
# endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
2007-02-01 18:10:13 +03:00
}
2007-03-09 21:51:48 +03:00
void smb_krb5_get_init_creds_opt_free ( krb5_context context ,
krb5_get_init_creds_opt * opt )
2007-02-01 18:10:13 +03:00
{
2007-03-09 21:51:48 +03:00
# ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
# ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
2007-04-23 12:40:54 +04:00
/* Modern MIT or Heimdal version */
2007-03-09 21:51:48 +03:00
krb5_get_init_creds_opt_free ( context , opt ) ;
# else
/* Heimdal version */
krb5_get_init_creds_opt_free ( opt ) ;
2007-04-23 12:40:54 +04:00
# endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
2007-03-09 21:51:48 +03:00
# else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
/* Historical MIT version */
2007-02-01 18:10:13 +03:00
SAFE_FREE ( opt ) ;
opt = NULL ;
2007-03-09 21:51:48 +03:00
# endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
2007-04-23 12:40:54 +04:00
}
2008-08-22 16:58:01 +04:00
krb5_enctype smb_get_enctype_from_kt_entry ( krb5_keytab_entry * kt_entry )
2007-04-23 12:40:54 +04:00
{
2008-08-22 16:58:01 +04:00
return KRB5_KEY_TYPE ( KRB5_KT_KEY ( kt_entry ) ) ;
2007-04-23 12:40:54 +04:00
}
/* caller needs to free etype_s */
krb5_error_code smb_krb5_enctype_to_string ( krb5_context context ,
krb5_enctype enctype ,
char * * etype_s )
{
# ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
return krb5_enctype_to_string ( context , enctype , etype_s ) ; /* Heimdal */
# elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
char buf [ 256 ] ;
krb5_error_code ret = krb5_enctype_to_string ( enctype , buf , 256 ) ; /* MIT */
if ( ret ) {
return ret ;
}
* etype_s = SMB_STRDUP ( buf ) ;
if ( ! * etype_s ) {
return ENOMEM ;
}
return ret ;
# else
# error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
# endif
2007-03-15 22:18:18 +03:00
}
krb5_error_code smb_krb5_mk_error ( krb5_context context ,
krb5_error_code error_code ,
const krb5_principal server ,
krb5_data * reply )
{
# ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
/*
* The MIT interface is * terrible * .
* We have to construct this ourselves . . .
*/
krb5_error e ;
memset ( & e , 0 , sizeof ( e ) ) ;
krb5_us_timeofday ( context , & e . stime , & e . susec ) ;
e . server = server ;
2007-03-15 23:45:27 +03:00
# if defined(krb5_err_base)
2007-03-15 22:18:18 +03:00
e . error = error_code - krb5_err_base ;
2007-03-15 23:45:27 +03:00
# elif defined(ERROR_TABLE_BASE_krb5)
e . error = error_code - ERROR_TABLE_BASE_krb5 ;
# else
e . error = error_code ; /* Almost certainly wrong, but what can we do... ? */
# endif
2007-03-15 22:18:18 +03:00
return krb5_mk_error ( context , & e , reply ) ;
# else /* Heimdal. */
return krb5_mk_error ( context ,
error_code ,
NULL ,
NULL , /* e_data */
NULL ,
server ,
NULL ,
NULL ,
reply ) ;
# endif
2007-02-01 18:10:13 +03:00
}
2007-06-29 13:42:14 +04:00
/**********************************************************************
* Open a krb5 keytab with flags , handles readonly or readwrite access and
* allows to process non - default keytab names .
* @ param context krb5_context
* @ param keytab_name_req string
2007-10-19 04:40:25 +04:00
* @ param write_access bool if writable keytab is required
2007-06-29 13:42:14 +04:00
* @ param krb5_keytab pointer to krb5_keytab ( close with krb5_kt_close ( ) )
* @ return krb5_error_code
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* This MAX_NAME_LEN is a constant defined in krb5.h */
# ifndef MAX_KEYTAB_NAME_LEN
# define MAX_KEYTAB_NAME_LEN 1100
# endif
krb5_error_code smb_krb5_open_keytab ( krb5_context context ,
const char * keytab_name_req ,
2007-10-19 04:40:25 +04:00
bool write_access ,
2007-06-29 13:42:14 +04:00
krb5_keytab * keytab )
{
krb5_error_code ret = 0 ;
TALLOC_CTX * mem_ctx ;
char keytab_string [ MAX_KEYTAB_NAME_LEN ] ;
2007-12-08 04:32:32 +03:00
char * kt_str = NULL ;
2007-10-19 04:40:25 +04:00
bool found_valid_name = False ;
2007-06-29 13:42:14 +04:00
const char * pragma = " FILE " ;
const char * tmp = NULL ;
if ( ! write_access & & ! keytab_name_req ) {
/* caller just wants to read the default keytab readonly, so be it */
return krb5_kt_default ( context , keytab ) ;
}
mem_ctx = talloc_init ( " smb_krb5_open_keytab " ) ;
if ( ! mem_ctx ) {
return ENOMEM ;
}
# ifdef HAVE_WRFILE_KEYTAB
if ( write_access ) {
pragma = " WRFILE " ;
}
# endif
if ( keytab_name_req ) {
if ( strlen ( keytab_name_req ) > MAX_KEYTAB_NAME_LEN ) {
ret = KRB5_CONFIG_NOTENUFSPACE ;
goto out ;
}
if ( ( strncmp ( keytab_name_req , " WRFILE:/ " , 8 ) = = 0 ) | |
( strncmp ( keytab_name_req , " FILE:/ " , 6 ) = = 0 ) ) {
tmp = keytab_name_req ;
goto resolve ;
}
if ( keytab_name_req [ 0 ] ! = ' / ' ) {
ret = KRB5_KT_BADNAME ;
goto out ;
}
tmp = talloc_asprintf ( mem_ctx , " %s:%s " , pragma , keytab_name_req ) ;
if ( ! tmp ) {
ret = ENOMEM ;
goto out ;
}
goto resolve ;
}
/* we need to handle more complex keytab_strings, like:
* " ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab " */
ret = krb5_kt_default_name ( context , & keytab_string [ 0 ] , MAX_KEYTAB_NAME_LEN - 2 ) ;
if ( ret ) {
goto out ;
}
DEBUG ( 10 , ( " smb_krb5_open_keytab: krb5_kt_default_name returned %s \n " , keytab_string ) ) ;
tmp = talloc_strdup ( mem_ctx , keytab_string ) ;
if ( ! tmp ) {
ret = ENOMEM ;
goto out ;
}
2007-12-08 04:32:32 +03:00
2007-06-29 13:42:14 +04:00
if ( strncmp ( tmp , " ANY: " , 4 ) = = 0 ) {
tmp + = 4 ;
}
memset ( & keytab_string , ' \0 ' , sizeof ( keytab_string ) ) ;
2007-12-08 04:32:32 +03:00
while ( next_token_talloc ( mem_ctx , & tmp , & kt_str , " , " ) ) {
if ( strncmp ( kt_str , " WRFILE: " , 7 ) = = 0 ) {
2007-06-29 13:42:14 +04:00
found_valid_name = True ;
2007-12-08 04:32:32 +03:00
tmp = kt_str ;
2007-06-29 13:42:14 +04:00
tmp + = 7 ;
}
2007-12-08 04:32:32 +03:00
if ( strncmp ( kt_str , " FILE: " , 5 ) = = 0 ) {
2007-06-29 13:42:14 +04:00
found_valid_name = True ;
2007-12-08 04:32:32 +03:00
tmp = kt_str ;
2007-06-29 13:42:14 +04:00
tmp + = 5 ;
}
2009-02-18 02:54:33 +03:00
if ( tmp [ 0 ] = = ' / ' ) {
/* Treat as a FILE: keytab definition. */
found_valid_name = true ;
}
2007-06-29 13:42:14 +04:00
if ( found_valid_name ) {
if ( tmp [ 0 ] ! = ' / ' ) {
ret = KRB5_KT_BADNAME ;
goto out ;
}
tmp = talloc_asprintf ( mem_ctx , " %s:%s " , pragma , tmp ) ;
if ( ! tmp ) {
ret = ENOMEM ;
goto out ;
}
break ;
}
}
2007-12-08 04:32:32 +03:00
2007-06-29 13:42:14 +04:00
if ( ! found_valid_name ) {
ret = KRB5_KT_UNKNOWN_TYPE ;
goto out ;
}
resolve :
DEBUG ( 10 , ( " smb_krb5_open_keytab: resolving: %s \n " , tmp ) ) ;
ret = krb5_kt_resolve ( context , tmp , keytab ) ;
out :
TALLOC_FREE ( mem_ctx ) ;
return ret ;
}
2008-06-18 14:48:35 +04:00
krb5_error_code smb_krb5_keytab_name ( TALLOC_CTX * mem_ctx ,
krb5_context context ,
krb5_keytab keytab ,
const char * * keytab_name )
{
char keytab_string [ MAX_KEYTAB_NAME_LEN ] ;
krb5_error_code ret = 0 ;
ret = krb5_kt_get_name ( context , keytab ,
keytab_string , MAX_KEYTAB_NAME_LEN - 2 ) ;
if ( ret ) {
return ret ;
}
* keytab_name = talloc_strdup ( mem_ctx , keytab_string ) ;
if ( ! * keytab_name ) {
return ENOMEM ;
}
return ret ;
}
2008-08-09 03:08:11 +04:00
# 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)
2008-08-09 01:32:15 +04:00
/**************************************************************
Routine : ads_krb5_get_fwd_ticket
Description :
When a service ticket is flagged as trusted
for delegation we should provide a forwardable
ticket so that the remote host can act on our
behalf . This is done by taking the 2 nd forwardable
TGT and storing it in the GSS - API authenticator
" checksum " . This routine will populate
the krb5_data authenticator with this TGT .
Parameters :
krb5_context context : The kerberos context for this authentication .
krb5_auth_context : The authentication context .
krb5_creds * credsp : The ticket credentials ( AS - REP ) .
krb5_ccache ccache : The credentials cache .
krb5_data & authenticator : The checksum field that will store the TGT , and
authenticator . data must be freed by the caller .
Returns :
krb5_error_code : 0 if no errors , otherwise set .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static krb5_error_code ads_krb5_get_fwd_ticket ( krb5_context context ,
krb5_auth_context * auth_context ,
krb5_creds * credsp ,
krb5_ccache ccache ,
krb5_data * authenticator )
{
krb5_data fwdData ;
krb5_error_code retval = 0 ;
char * pChksum = NULL ;
char * p = NULL ;
ZERO_STRUCT ( fwdData ) ;
ZERO_STRUCTP ( authenticator ) ;
retval = krb5_fwd_tgt_creds ( context , /* Krb5 context [in] */
* auth_context , /* Authentication context [in] */
CONST_DISCARD ( char * , KRB5_TGS_NAME ) , /* Ticket service name ("krbtgt") [in] */
credsp - > client , /* Client principal for the tgt [in] */
credsp - > server , /* Server principal for the tgt [in] */
ccache , /* Credential cache to use for storage [in] */
1 , /* Turn on for "Forwardable ticket" [in] */
& fwdData ) ; /* Resulting response [out] */
if ( retval ) {
DEBUG ( 1 , ( " ads_krb5_get_fwd_ticket: krb5_fwd_tgt_creds failed (%s) \n " ,
error_message ( retval ) ) ) ;
goto out ;
}
if ( ( unsigned int ) GSSAPI_CHECKSUM_SIZE + ( unsigned int ) fwdData . length <
( unsigned int ) GSSAPI_CHECKSUM_SIZE ) {
retval = EINVAL ;
goto out ;
}
/* We're going to allocate a gssChecksum structure with a little
extra data the length of the kerberos credentials length
( APPLICATION 22 ) so that we can pack it on the end of the structure .
*/
2008-08-11 13:20:38 +04:00
pChksum = ( char * ) SMB_MALLOC ( GSSAPI_CHECKSUM_SIZE + fwdData . length ) ;
2008-08-09 01:32:15 +04:00
if ( ! pChksum ) {
retval = ENOMEM ;
goto out ;
}
p = pChksum ;
SIVAL ( p , 0 , GSSAPI_BNDLENGTH ) ;
p + = 4 ;
/* Zero out the bindings fields */
memset ( p , ' \0 ' , GSSAPI_BNDLENGTH ) ;
p + = GSSAPI_BNDLENGTH ;
SIVAL ( p , 0 , GSS_C_DELEG_FLAG ) ;
p + = 4 ;
SSVAL ( p , 0 , 1 ) ;
p + = 2 ;
SSVAL ( p , 0 , fwdData . length ) ;
p + = 2 ;
/* Migrate the kerberos KRB_CRED data to the checksum delegation */
memcpy ( p , fwdData . data , fwdData . length ) ;
p + = fwdData . length ;
/* We need to do this in order to allow our GSS-API */
retval = krb5_auth_con_set_req_cksumtype ( context , * auth_context , GSSAPI_CHECKSUM ) ;
if ( retval ) {
goto out ;
}
/* We now have a service ticket, now turn it into an AP-REQ. */
2008-08-18 20:55:11 +04:00
authenticator - > length = fwdData . length + GSSAPI_CHECKSUM_SIZE ;
2008-08-09 01:32:15 +04:00
/* Caller should call free() when they're done with this. */
authenticator - > data = ( char * ) pChksum ;
out :
/* Remove that input data, we never needed it anyway. */
if ( fwdData . length > 0 ) {
krb5_free_data_contents ( context , & fwdData ) ;
}
return retval ;
}
# endif
2001-10-11 11:42:52 +04:00
# else /* HAVE_KRB5 */
2001-10-12 08:49:42 +04:00
/* this saves a few linking headaches */
2005-10-01 13:43:53 +04:00
int cli_krb5_get_ticket ( const char * principal , time_t time_offset ,
2006-02-04 01:19:41 +03:00
DATA_BLOB * ticket , DATA_BLOB * session_key_krb5 , uint32 extra_ap_opts ,
2007-02-08 20:02:39 +03:00
const char * ccname , time_t * tgs_expire )
2004-01-08 11:19:18 +03:00
{
2001-10-12 08:49:42 +04:00
DEBUG ( 0 , ( " NO KERBEROS SUPPORT \n " ) ) ;
2004-01-08 11:19:18 +03:00
return 1 ;
}
2003-03-18 01:45:16 +03:00
2001-10-11 11:42:52 +04:00
# endif