2003-08-13 05:53:07 +04:00
/*
Unix SMB / CIFS implementation .
simple kerberos5 routines for active directory
Copyright ( C ) Andrew Tridgell 2001
Copyright ( C ) Luke Howard 2002 - 2003
2005-06-04 15:17:05 +04:00
Copyright ( C ) Andrew Bartlett < abartlet @ samba . org > 2005
2005-10-20 14:15:31 +04:00
2003-08-13 05:53:07 +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
the Free Software Foundation ; either version 2 of the License , or
( 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
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
2004-11-02 01:48:25 +03:00
# include "system/network.h"
# include "system/kerberos.h"
2005-02-11 13:15:56 +03:00
# include "system/time.h"
2005-03-29 12:24:03 +04:00
# include "auth/kerberos/kerberos.h"
2003-08-13 05:53:07 +04:00
# ifdef HAVE_KRB5
# ifndef HAVE_KRB5_SET_REAL_TIME
/*
* This function is not in the Heimdal mainline .
*/
2004-05-25 22:12:55 +04:00
krb5_error_code krb5_set_real_time ( krb5_context context , int32_t seconds , int32_t microseconds )
2003-08-13 05:53:07 +04:00
{
krb5_error_code ret ;
2004-05-25 22:12:55 +04:00
int32_t sec , usec ;
2003-08-13 05:53:07 +04:00
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
# if defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES) && !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
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
# if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
/* HEIMDAL */
void setup_kaddr ( krb5_address * pkaddr , struct sockaddr * paddr )
{
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 ) ;
}
# elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
/* MIT */
void setup_kaddr ( krb5_address * pkaddr , struct sockaddr * paddr )
{
pkaddr - > addrtype = ADDRTYPE_INET ;
pkaddr - > length = sizeof ( ( ( struct sockaddr_in * ) paddr ) - > sin_addr ) ;
2003-08-15 21:50:16 +04:00
pkaddr - > contents = ( krb5_octet * ) & ( ( ( struct sockaddr_in * ) paddr ) - > sin_addr ) ;
2003-08-13 05:53:07 +04:00
}
# else
2004-12-24 12:54:23 +03:00
# error UNKNOWN_ADDRTYPE
2003-08-13 05:53:07 +04:00
# endif
2004-12-24 12:54:23 +03:00
# if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
2005-08-20 10:08:52 +04:00
int create_kerberos_key_from_string ( krb5_context context ,
2003-08-13 05:53:07 +04:00
krb5_principal host_princ ,
krb5_data * password ,
krb5_keyblock * key ,
krb5_enctype enctype )
{
int ret ;
krb5_data salt ;
krb5_encrypt_block eblock ;
ret = krb5_principal2salt ( context , host_princ , & salt ) ;
if ( ret ) {
DEBUG ( 1 , ( " krb5_principal2salt failed (%s) \n " , error_message ( ret ) ) ) ;
return ret ;
}
krb5_use_enctype ( context , & eblock , enctype ) ;
2004-02-01 14:26:25 +03:00
ret = krb5_string_to_key ( context , & eblock , key , password , & salt ) ;
SAFE_FREE ( salt . data ) ;
return ret ;
2003-08-13 05:53:07 +04:00
}
# elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
2005-08-20 10:08:52 +04:00
int create_kerberos_key_from_string ( krb5_context context ,
2003-08-13 05:53:07 +04:00
krb5_principal host_princ ,
krb5_data * password ,
krb5_keyblock * key ,
krb5_enctype enctype )
{
int ret ;
krb5_salt salt ;
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 ;
}
2005-10-13 02:24:43 +04:00
ret = krb5_string_to_key_salt ( context , enctype , password - > data ,
salt , key ) ;
krb5_free_salt ( context , salt ) ;
return ret ;
2003-08-13 05:53:07 +04:00
}
# else
2004-12-24 12:54:23 +03:00
# error UNKNOWN_CREATE_KEY_FUNCTIONS
2003-08-13 05:53:07 +04:00
# endif
# if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
2004-07-09 15:46:42 +04:00
krb5_error_code get_kerberos_allowed_etypes ( krb5_context context ,
2003-08-13 05:53:07 +04:00
krb5_enctype * * enctypes )
{
return krb5_get_permitted_enctypes ( context , enctypes ) ;
}
# elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
2004-07-09 15:46:42 +04:00
krb5_error_code get_kerberos_allowed_etypes ( krb5_context context ,
2003-08-13 05:53:07 +04:00
krb5_enctype * * enctypes )
{
return krb5_get_default_in_tkt_etypes ( context , enctypes ) ;
}
# else
# error UNKNOWN_GET_ENCTYPES_FUNCTIONS
# endif
void free_kerberos_etypes ( krb5_context context ,
krb5_enctype * enctypes )
{
# if defined(HAVE_KRB5_FREE_KTYPES)
krb5_free_ktypes ( context , enctypes ) ;
return ;
# else
SAFE_FREE ( enctypes ) ;
return ;
# endif
}
# 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
2004-06-19 12:15:41 +04:00
# if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
void krb5_free_unparsed_name ( krb5_context context , char * val )
{
SAFE_FREE ( val ) ;
}
# endif
2004-12-24 12:54:23 +03:00
void kerberos_free_data_contents ( krb5_context context , krb5_data * pdata )
{
# if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
if ( pdata - > data ) {
krb5_free_data_contents ( context , pdata ) ;
}
# else
SAFE_FREE ( pdata - > data ) ;
# endif
}
void kerberos_set_creds_enctype ( krb5_creds * pcreds , int enctype )
{
# 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
}
BOOL kerberos_compatible_enctypes ( krb5_context context ,
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 ) ;
return similar ? True : False ;
# elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
return krb5_enctypes_compatible_keys ( context , enctype1 , enctype2 ) ? True : False ;
# endif
}
2004-06-19 12:15:41 +04:00
static BOOL ads_cleanup_expired_creds ( krb5_context context ,
krb5_ccache ccache ,
krb5_creds * credsp )
{
krb5_error_code retval ;
TALLOC_CTX * mem_ctx = talloc_init ( " ticket expied time " ) ;
if ( ! mem_ctx ) {
return False ;
}
DEBUG ( 3 , ( " Ticket in ccache[%s] expiration %s \n " ,
krb5_cc_default_name ( context ) ,
http_timestring ( mem_ctx , credsp - > times . endtime ) ) ) ;
2005-01-27 10:08:20 +03:00
talloc_free ( mem_ctx ) ;
2004-06-19 12:15:41 +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
*/
2005-08-30 15:55:05 +04:00
if ( strcasecmp_m ( krb5_cc_get_type ( context , ccache ) , " FILE " ) = = 0 ) {
2004-12-24 12:54:23 +03:00
DEBUG ( 5 , ( " ads_cleanup_expired_creds: We do not remove creds from a FILE ccache \n " ) ) ;
2004-06-19 12:15:41 +04:00
return False ;
}
retval = krb5_cc_remove_cred ( context , ccache , 0 , credsp ) ;
if ( retval ) {
2004-12-24 12:54:23 +03:00
DEBUG ( 1 , ( " ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s \n " ,
2004-06-19 12:15:41 +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 ;
}
2003-08-13 05:53:07 +04:00
/*
we can ' t use krb5_mk_req because w2k wants the service to be in a particular format
*/
2004-12-24 12:54:23 +03:00
krb5_error_code ads_krb5_mk_req ( krb5_context context ,
2004-07-11 14:47:41 +04:00
krb5_auth_context * auth_context ,
const krb5_flags ap_req_options ,
const char * principal ,
krb5_ccache ccache ,
krb5_data * outbuf )
2003-08-13 05:53:07 +04:00
{
krb5_error_code retval ;
krb5_principal server ;
krb5_creds * credsp ;
krb5_creds creds ;
krb5_data in_data ;
2004-06-19 12:15:41 +04:00
BOOL creds_ready = False ;
2003-08-13 05:53:07 +04:00
2004-07-09 17:33:10 +04:00
TALLOC_CTX * mem_ctx = NULL ;
2004-06-19 12:15:41 +04:00
2003-08-13 05:53:07 +04:00
retval = krb5_parse_name ( context , principal , & server ) ;
if ( retval ) {
2004-12-24 12:54:23 +03:00
DEBUG ( 1 , ( " ads_krb5_mk_req: Failed to parse principal %s \n " , principal ) ) ;
2003-08-13 05:53:07 +04:00
return retval ;
}
/* obtain ticket & session key */
2004-02-01 14:26:25 +03:00
ZERO_STRUCT ( creds ) ;
2003-08-13 05:53:07 +04:00
if ( ( retval = krb5_copy_principal ( context , server , & creds . server ) ) ) {
DEBUG ( 1 , ( " krb5_copy_principal failed (%s) \n " ,
error_message ( retval ) ) ) ;
goto cleanup_princ ;
}
if ( ( retval = krb5_cc_get_principal ( context , ccache , & creds . client ) ) ) {
2004-12-24 12:54: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 " ,
2003-08-13 05:53:07 +04:00
error_message ( retval ) ) ) ;
goto cleanup_creds ;
}
2004-06-19 12:15:41 +04:00
while ( ! creds_ready ) {
if ( ( retval = krb5_get_credentials ( context , 0 , ccache ,
& creds , & credsp ) ) ) {
2004-12-24 12:54:23 +03:00
DEBUG ( 1 , ( " ads_krb5_mk_req: krb5_get_credentials failed for %s (%s) \n " ,
2004-06-19 12:15:41 +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 ) ;
int time_offset = ( unsigned ) credsp - > times . starttime - t ;
2004-12-24 12:54:23 +03:00
DEBUG ( 4 , ( " ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew \n " , time_offset ) ) ;
2004-06-19 12:15:41 +04:00
krb5_set_real_time ( context , t + time_offset + 1 , 0 ) ;
}
if ( ! ads_cleanup_expired_creds ( context , ccache , credsp ) )
creds_ready = True ;
2003-08-13 05:53:07 +04:00
}
2004-06-19 12:15:41 +04:00
mem_ctx = talloc_init ( " ticket expied time " ) ;
if ( ! mem_ctx ) {
retval = ENOMEM ;
goto cleanup_creds ;
2003-08-13 05:53:07 +04:00
}
2004-06-19 12:15:41 +04:00
DEBUG ( 10 , ( " Ticket (%s) in ccache (%s) is valid until: (%s - %d) \n " ,
principal , krb5_cc_default_name ( context ) ,
http_timestring ( mem_ctx , ( unsigned ) credsp - > times . endtime ) ,
( unsigned ) credsp - > times . endtime ) ) ;
2003-08-13 05:53:07 +04:00
in_data . length = 0 ;
retval = krb5_mk_req_extended ( context , auth_context , ap_req_options ,
& in_data , credsp , outbuf ) ;
if ( retval ) {
2004-12-24 12:54:23 +03:00
DEBUG ( 1 , ( " ads_krb5_mk_req: krb5_mk_req_extended failed (%s) \n " ,
2003-08-13 05:53:07 +04:00
error_message ( retval ) ) ) ;
}
krb5_free_creds ( context , credsp ) ;
cleanup_creds :
krb5_free_cred_contents ( context , & creds ) ;
cleanup_princ :
krb5_free_principal ( context , server ) ;
return retval ;
}
2004-07-09 15:46:42 +04:00
krb5_error_code smb_krb5_kt_free_entry ( krb5_context context , krb5_keytab_entry * kt_entry )
{
# 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
}
2005-03-28 10:40:18 +04:00
char * smb_get_krb5_error_message ( krb5_context context , krb5_error_code code , TALLOC_CTX * mem_ctx )
{
char * ret ;
# if defined(HAVE_KRB5_GET_ERROR_STRING) && defined(HAVE_KRB5_FREE_ERROR_STRING)
char * context_error = krb5_get_error_string ( context ) ;
r10066: This is the second in my patches to work on Samba4's kerberos support,
with an aim to make the code simpiler and more correct.
Gone is the old (since the very early Samba 3.0 krb5 days) 'iterate over
all keytypes)' code in gensec_krb5, we now follow the approach used in
gensec_gssapi, and use a keytab.
I have also done a lot of work in the GSSAPI code, to try and reduce
the diff between us and upstream heimdal. It was becoming hard to
track patches in this code, and I also want this patch (the DCE_STYLE
support) to be in a 'manageable' state for when lha considers it for
merging. (metze assures me it still has memory leak problems, but
I've started to address some of that).
This patch also includes a simple update of other code to current
heimdal, as well as changes we need for better PAC verification.
On the PAC side of things we now match windows member servers by
checking the name and authtime on an incoming PAC. Not generating these
right was the cause of the PAC pain, and so now both the main code and
torture test validate this behaviour.
One thing doesn't work with this patch:
- the sealing of RPC pipes with kerberos, Samba -> Samba seems
broken. I'm pretty sure this is related to AES, and the need to break
apart the gss_wrap interface.
Andrew Bartlett
(This used to be commit a3aba57c00a9c5318f4706db55d03f64e8bea60c)
2005-09-08 01:52:50 +04:00
if ( context_error ) {
ret = talloc_asprintf ( mem_ctx , " %s: %s " , error_message ( code ) , context_error ) ;
krb5_free_error_string ( context , context_error ) ;
return ret ;
}
2005-03-28 10:40:18 +04:00
# endif
r10066: This is the second in my patches to work on Samba4's kerberos support,
with an aim to make the code simpiler and more correct.
Gone is the old (since the very early Samba 3.0 krb5 days) 'iterate over
all keytypes)' code in gensec_krb5, we now follow the approach used in
gensec_gssapi, and use a keytab.
I have also done a lot of work in the GSSAPI code, to try and reduce
the diff between us and upstream heimdal. It was becoming hard to
track patches in this code, and I also want this patch (the DCE_STYLE
support) to be in a 'manageable' state for when lha considers it for
merging. (metze assures me it still has memory leak problems, but
I've started to address some of that).
This patch also includes a simple update of other code to current
heimdal, as well as changes we need for better PAC verification.
On the PAC side of things we now match windows member servers by
checking the name and authtime on an incoming PAC. Not generating these
right was the cause of the PAC pain, and so now both the main code and
torture test validate this behaviour.
One thing doesn't work with this patch:
- the sealing of RPC pipes with kerberos, Samba -> Samba seems
broken. I'm pretty sure this is related to AES, and the need to break
apart the gss_wrap interface.
Andrew Bartlett
(This used to be commit a3aba57c00a9c5318f4706db55d03f64e8bea60c)
2005-09-08 01:52:50 +04:00
ret = talloc_strdup ( mem_ctx , error_message ( code ) ) ;
2005-03-28 10:40:18 +04:00
return ret ;
}
2003-08-13 05:53:07 +04:00
# endif