2001-11-24 14:16:41 +00:00
/*
2002-01-30 06:08:46 +00:00
Unix SMB / CIFS implementation .
2001-11-24 14:16:41 +00:00
kerberos utility library
Copyright ( C ) Andrew Tridgell 2001
2001-12-20 03:54:52 +00:00
Copyright ( C ) Remus Koos 2001
2005-01-14 19:26:13 +00:00
Copyright ( C ) Nalin Dahyabhai < nalin @ redhat . com > 2004.
2004-10-29 22:38:10 +00:00
Copyright ( C ) Jeremy Allison 2004.
2006-07-11 18:45:22 +00:00
Copyright ( C ) Gerald Carter 2006.
2004-10-29 22:38:10 +00:00
2001-11-24 14:16:41 +00: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 19:25:36 +00:00
the Free Software Foundation ; either version 3 of the License , or
2001-11-24 14:16:41 +00:00
( at your option ) any later version .
2011-02-26 12:36:19 +01:00
2001-11-24 14:16:41 +00:00
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 .
2011-02-26 12:36:19 +01:00
2001-11-24 14:16:41 +00:00
You should have received a copy of the GNU General Public License
2007-07-10 00:52:41 +00:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2001-11-24 14:16:41 +00:00
*/
# include "includes.h"
2018-03-10 15:31:11 +01:00
# include "libsmb/namequery.h"
2011-02-25 23:20:06 +01:00
# include "system/filesys.h"
2009-11-27 15:52:57 +01:00
# include "smb_krb5.h"
2010-05-28 02:20:02 +02:00
# include "../librpc/gen_ndr/ndr_misc.h"
2010-07-02 00:32:52 +02:00
# include "libads/kerberos_proto.h"
2011-10-12 19:41:45 +02:00
# include "libads/cldap.h"
2010-08-05 02:25:37 +02:00
# include "secrets.h"
2011-10-12 19:41:45 +02:00
# include "../lib/tsocket/tsocket.h"
2016-08-25 17:07:01 +02:00
# include "lib/util/asn1.h"
2001-11-24 14:16:41 +00:00
# ifdef HAVE_KRB5
2004-10-29 22:38:10 +00:00
# define LIBADS_CCACHE_NAME "MEMORY:libads"
2002-07-15 10:35:28 +00:00
/*
we use a prompter to avoid a crash bug in the kerberos libs when
dealing with empty passwords
this prompter is just a string copy . . .
*/
static krb5_error_code
kerb_prompter ( krb5_context ctx , void * data ,
const char * name ,
const char * banner ,
int num_prompts ,
krb5_prompt prompts [ ] )
{
if ( num_prompts = = 0 ) return 0 ;
2016-02-15 12:58:07 +01:00
if ( num_prompts = = 2 ) {
2015-08-28 12:33:13 +02:00
/*
2016-02-15 12:58:07 +01:00
* only heimdal has a prompt type and we need to deal with it here to
* avoid loops .
*
* removing the prompter completely is not an option as at least these
* versions would crash : heimdal - 1.0 .2 and heimdal - 1.1 . Later heimdal
* version have looping detection and return with a proper error code .
2015-08-28 12:33:13 +02:00
*/
2016-02-15 12:58:07 +01:00
# if HAVE_KRB5_PROMPT_TYPE /* Heimdal */
if ( prompts [ 0 ] . type = = KRB5_PROMPT_TYPE_NEW_PASSWORD & &
prompts [ 1 ] . type = = KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN ) {
/*
* We don ' t want to change passwords here . We ' re
* called from heimal when the KDC returns
* KRB5KDC_ERR_KEY_EXPIRED , but at this point we don ' t
* have the chance to ask the user for a new
* password . If we return 0 ( i . e . success ) , we will be
* spinning in the endless for - loop in
* change_password ( ) in
* source4 / heimdal / lib / krb5 / init_creds_pw . c : 526ff
*/
return KRB5KDC_ERR_KEY_EXPIRED ;
}
# elif defined(HAVE_KRB5_GET_PROMPT_TYPES) /* MIT */
krb5_prompt_type * prompt_types = NULL ;
prompt_types = krb5_get_prompt_types ( ctx ) ;
if ( prompt_types ! = NULL ) {
if ( prompt_types [ 0 ] = = KRB5_PROMPT_TYPE_NEW_PASSWORD & &
prompt_types [ 1 ] = = KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN ) {
return KRB5KDC_ERR_KEY_EXP ;
}
}
# endif
2015-08-28 12:33:13 +02:00
}
2016-02-15 12:58:07 +01:00
2004-10-29 22:38:10 +00:00
memset ( prompts [ 0 ] . reply - > data , ' \0 ' , prompts [ 0 ] . reply - > length ) ;
2002-07-15 10:35:28 +00:00
if ( prompts [ 0 ] . reply - > length > 0 ) {
2002-10-24 01:05:30 +00:00
if ( data ) {
2009-11-06 10:25:53 +01:00
strncpy ( ( char * ) prompts [ 0 ] . reply - > data , ( const char * ) data ,
2006-07-31 20:51:55 +00:00
prompts [ 0 ] . reply - > length - 1 ) ;
2009-11-06 10:25:53 +01:00
prompts [ 0 ] . reply - > length = strlen ( ( const char * ) prompts [ 0 ] . reply - > data ) ;
2002-10-24 01:05:30 +00:00
} else {
prompts [ 0 ] . reply - > length = 0 ;
}
2002-07-15 10:35:28 +00:00
}
return 0 ;
}
2001-12-06 05:41:53 +00:00
/*
2004-10-29 22:38:10 +00:00
simulate a kinit , putting the tgt in the given cache location . If cache_name = = NULL
place in default cache location .
2001-12-06 05:41:53 +00:00
remus @ snapserver . com
*/
2006-03-20 19:05:44 +00:00
int kerberos_kinit_password_ext ( const char * principal ,
2004-10-29 22:38:10 +00:00
const char * password ,
int time_offset ,
time_t * expire_time ,
2006-02-03 22:19:41 +00:00
time_t * renew_till_time ,
const char * cache_name ,
2007-10-18 17:40:25 -07:00
bool request_pac ,
bool add_netbios_addr ,
2007-05-04 10:21:39 +00:00
time_t renewable_time ,
NTSTATUS * ntstatus )
2001-12-06 05:41:53 +00:00
{
2004-05-07 02:48:03 +00:00
krb5_context ctx = NULL ;
2001-12-06 05:41:53 +00:00
krb5_error_code code = 0 ;
2004-05-07 02:48:03 +00:00
krb5_ccache cc = NULL ;
2007-05-04 09:46:17 +00:00
krb5_principal me = NULL ;
2016-02-15 12:09:59 +01:00
krb5_principal canon_princ = NULL ;
2001-12-06 05:41:53 +00:00
krb5_creds my_creds ;
2007-02-01 15:10:13 +00:00
krb5_get_init_creds_opt * opt = NULL ;
2006-04-25 12:24:25 +00:00
smb_krb5_addresses * addr = NULL ;
2001-12-13 11:30:13 +00:00
2007-05-04 09:46:17 +00:00
ZERO_STRUCT ( my_creds ) ;
2005-11-07 14:16:50 +00:00
initialize_krb5_error_table ( ) ;
2001-12-06 05:41:53 +00:00
if ( ( code = krb5_init_context ( & ctx ) ) )
2007-05-04 09:46:17 +00:00
goto out ;
2002-09-25 15:19:00 +00:00
if ( time_offset ! = 0 ) {
krb5_set_real_time ( ctx , time ( NULL ) + time_offset , 0 ) ;
}
2006-02-03 22:19:41 +00:00
2008-01-14 14:18:53 +01:00
DEBUG ( 10 , ( " kerberos_kinit_password: as %s using [%s] as ccache and config [%s] \n " ,
principal ,
2006-09-14 10:21:46 +00:00
cache_name ? cache_name : krb5_cc_default_name ( ctx ) ,
getenv ( " KRB5_CONFIG " ) ) ) ;
2006-02-03 22:19:41 +00:00
if ( ( code = krb5_cc_resolve ( ctx , cache_name ? cache_name : krb5_cc_default_name ( ctx ) , & cc ) ) ) {
2007-05-04 09:46:17 +00:00
goto out ;
2001-12-06 05:41:53 +00:00
}
2011-02-26 12:36:19 +01:00
2006-04-24 15:57:54 +00:00
if ( ( code = smb_krb5_parse_name ( ctx , principal , & me ) ) ) {
2007-05-04 09:46:17 +00:00
goto out ;
2001-12-06 05:41:53 +00:00
}
2006-02-03 22:19:41 +00:00
2016-08-29 09:12:38 +02:00
if ( ( code = krb5_get_init_creds_opt_alloc ( ctx , & opt ) ) ) {
2007-05-04 09:46:17 +00:00
goto out ;
2007-02-01 15:10:13 +00:00
}
krb5_get_init_creds_opt_set_renew_life ( opt , renewable_time ) ;
krb5_get_init_creds_opt_set_forwardable ( opt , True ) ;
2016-02-15 12:09:59 +01:00
/* Turn on canonicalization for lower case realm support */
# ifndef SAMBA4_USES_HEIMDAL /* MIT */
krb5_get_init_creds_opt_set_canonicalize ( opt , true ) ;
# endif /* MIT */
2007-02-08 17:02:39 +00:00
#if 0
/* insane testing */
krb5_get_init_creds_opt_set_tkt_life ( opt , 60 ) ;
# endif
2007-02-01 15:10:13 +00:00
2006-02-03 22:19:41 +00:00
# ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_SET_PAC_REQUEST
2007-02-01 15:10:13 +00:00
if ( request_pac ) {
2007-05-04 09:46:17 +00:00
if ( ( code = krb5_get_init_creds_opt_set_pac_request ( ctx , opt , ( krb5_boolean ) request_pac ) ) ) {
goto out ;
2006-04-25 12:24:25 +00:00
}
2006-02-03 22:19:41 +00:00
}
2007-02-01 15:10:13 +00:00
# endif
2006-04-25 12:24:25 +00:00
if ( add_netbios_addr ) {
2012-04-21 17:26:18 -04:00
if ( ( code = smb_krb5_gen_netbios_krb5_address ( & addr ,
lp_netbios_name ( ) ) ) ) {
2007-05-04 09:46:17 +00:00
goto out ;
2006-04-25 12:24:25 +00:00
}
2007-02-01 15:10:13 +00:00
krb5_get_init_creds_opt_set_address_list ( opt , addr - > addrs ) ;
2006-04-25 12:24:25 +00:00
}
2011-05-05 13:42:05 -07:00
if ( ( code = krb5_get_init_creds_password ( ctx , & my_creds , me , discard_const_p ( char , password ) ,
kerb_prompter , discard_const_p ( char , password ) ,
2007-05-25 22:04:03 +00:00
0 , NULL , opt ) ) ) {
2007-05-04 09:46:17 +00:00
goto out ;
2001-12-06 05:41:53 +00:00
}
2007-02-01 15:10:13 +00:00
2016-02-15 12:09:59 +01:00
canon_princ = me ;
# ifndef SAMBA4_USES_HEIMDAL /* MIT */
canon_princ = my_creds . client ;
# endif /* MIT */
if ( ( code = krb5_cc_initialize ( ctx , cc , canon_princ ) ) ) {
2007-05-04 09:46:17 +00:00
goto out ;
2001-12-06 05:41:53 +00:00
}
2011-02-26 12:36:19 +01:00
2001-12-06 05:41:53 +00:00
if ( ( code = krb5_cc_store_cred ( ctx , cc , & my_creds ) ) ) {
2007-05-04 09:46:17 +00:00
goto out ;
2001-12-06 05:41:53 +00:00
}
2006-02-03 22:19:41 +00:00
if ( expire_time ) {
2004-03-24 17:32:55 +00:00
* expire_time = ( time_t ) my_creds . times . endtime ;
2006-02-03 22:19:41 +00:00
}
if ( renew_till_time ) {
* renew_till_time = ( time_t ) my_creds . times . renew_till ;
}
2007-05-04 09:46:17 +00:00
out :
2007-05-04 10:21:39 +00:00
if ( ntstatus ) {
/* fast path */
if ( code = = 0 ) {
* ntstatus = NT_STATUS_OK ;
goto cleanup ;
}
/* fall back to self-made-mapping */
* ntstatus = krb5_to_nt_status ( code ) ;
}
cleanup :
2001-12-06 05:41:53 +00:00
krb5_free_cred_contents ( ctx , & my_creds ) ;
2007-05-04 09:46:17 +00:00
if ( me ) {
krb5_free_principal ( ctx , me ) ;
}
if ( addr ) {
smb_krb5_free_addresses ( ctx , addr ) ;
}
2016-08-29 09:13:51 +02:00
if ( opt ) {
krb5_get_init_creds_opt_free ( ctx , opt ) ;
2007-05-04 09:46:17 +00:00
}
if ( cc ) {
krb5_cc_close ( ctx , cc ) ;
}
if ( ctx ) {
krb5_free_context ( ctx ) ;
}
return code ;
2001-12-06 05:41:53 +00:00
}
2004-03-24 17:32:55 +00:00
int ads_kdestroy ( const char * cc_name )
{
krb5_error_code code ;
2004-05-07 02:48:03 +00:00
krb5_context ctx = NULL ;
krb5_ccache cc = NULL ;
2004-03-24 17:32:55 +00:00
2005-11-07 14:16:50 +00:00
initialize_krb5_error_table ( ) ;
2004-03-24 17:32:55 +00:00
if ( ( code = krb5_init_context ( & ctx ) ) ) {
2004-08-20 20:18:28 +00:00
DEBUG ( 3 , ( " ads_kdestroy: kdb5_init_context failed: %s \n " ,
error_message ( code ) ) ) ;
2004-03-24 17:32:55 +00:00
return code ;
}
2011-02-26 12:36:19 +01:00
2004-03-24 17:32:55 +00:00
if ( ! cc_name ) {
if ( ( code = krb5_cc_default ( ctx , & cc ) ) ) {
krb5_free_context ( ctx ) ;
return code ;
}
} else {
if ( ( code = krb5_cc_resolve ( ctx , cc_name , & cc ) ) ) {
2004-08-20 20:18:28 +00:00
DEBUG ( 3 , ( " ads_kdestroy: krb5_cc_resolve failed: %s \n " ,
error_message ( code ) ) ) ;
2004-03-24 17:32:55 +00:00
krb5_free_context ( ctx ) ;
return code ;
}
}
if ( ( code = krb5_cc_destroy ( ctx , cc ) ) ) {
2004-08-20 20:18:28 +00:00
DEBUG ( 3 , ( " ads_kdestroy: krb5_cc_destroy failed: %s \n " ,
error_message ( code ) ) ) ;
2004-03-24 17:32:55 +00:00
}
krb5_free_context ( ctx ) ;
return code ;
}
2001-11-24 14:16:41 +00:00
2012-04-21 17:26:18 -04:00
int create_kerberos_key_from_string ( krb5_context context ,
krb5_principal host_princ ,
2016-02-29 16:21:56 +01:00
krb5_principal salt_princ ,
2012-04-21 17:26:18 -04:00
krb5_data * password ,
krb5_keyblock * key ,
krb5_enctype enctype ,
bool no_salt )
{
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 .
*/
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 ;
}
2014-04-25 14:14:20 +02:00
ret = smb_krb5_create_key_from_string ( context ,
2015-03-26 11:21:06 +01:00
salt_princ ? salt_princ : host_princ ,
2014-04-25 14:14:20 +02:00
NULL ,
password ,
enctype ,
key ) ;
2012-04-21 17:26:18 -04:00
return ret ;
}
2004-10-29 22:38:10 +00:00
/************************************************************************
2006-07-11 18:45:22 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-03-20 19:05:44 +00:00
int kerberos_kinit_password ( const char * principal ,
const char * password ,
int time_offset ,
const char * cache_name )
{
return kerberos_kinit_password_ext ( principal ,
password ,
time_offset ,
0 ,
0 ,
cache_name ,
False ,
2006-04-25 12:24:25 +00:00
False ,
2007-05-04 10:21:39 +00:00
0 ,
NULL ) ;
2006-03-20 19:05:44 +00:00
}
2008-01-16 13:21:46 -08:00
/************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-09-02 19:27:44 +00:00
/************************************************************************
Create a string list of available kdc ' s , possibly searching by sitename .
Does DNS queries .
2008-05-19 02:48:09 +02:00
If " sitename " is given , the DC ' s in that site are listed first .
2006-09-02 19:27:44 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2017-12-07 17:38:21 +01:00
static void add_sockaddr_unique ( struct sockaddr_storage * addrs , size_t * num_addrs ,
2011-10-12 19:41:45 +02:00
const struct sockaddr_storage * addr )
{
2017-12-07 17:38:21 +01:00
size_t i ;
2011-10-12 19:41:45 +02:00
for ( i = 0 ; i < * num_addrs ; i + + ) {
if ( sockaddr_equal ( ( const struct sockaddr * ) & addrs [ i ] ,
( const struct sockaddr * ) addr ) ) {
return ;
}
}
addrs [ i ] = * addr ;
* num_addrs + = 1 ;
}
2014-04-02 19:37:34 +02:00
/* print_canonical_sockaddr prints an ipv6 addr in the form of
* [ ipv6 . addr ] . This string , when put in a generated krb5 . conf file is not
* always properly dealt with by some older krb5 libraries . Adding the hard - coded
* portnumber workarounds the issue . - gd */
static char * print_canonical_sockaddr_with_port ( TALLOC_CTX * mem_ctx ,
const struct sockaddr_storage * pss )
{
char * str = NULL ;
str = print_canonical_sockaddr ( mem_ctx , pss ) ;
if ( str = = NULL ) {
return NULL ;
}
if ( pss - > ss_family ! = AF_INET6 ) {
return str ;
}
# if defined(HAVE_IPV6)
str = talloc_asprintf_append ( str , " :88 " ) ;
# endif
return str ;
}
2007-10-24 14:16:54 -07:00
static char * get_kdc_ip_string ( char * mem_ctx ,
const char * realm ,
const char * sitename ,
2014-03-07 14:47:31 +01:00
const struct sockaddr_storage * pss )
2006-09-02 19:27:44 +00:00
{
2011-10-12 19:41:45 +02:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2017-12-07 17:38:21 +01:00
size_t i ;
2007-08-31 13:39:51 +00:00
struct ip_service * ip_srv_site = NULL ;
2008-01-16 13:21:46 -08:00
struct ip_service * ip_srv_nonsite = NULL ;
2007-08-31 13:39:51 +00:00
int count_site = 0 ;
2007-09-08 13:53:08 +00:00
int count_nonsite ;
2017-12-07 17:38:21 +01:00
size_t num_dcs ;
2011-10-12 19:41:45 +02:00
struct sockaddr_storage * dc_addrs ;
struct tsocket_address * * dc_addrs2 = NULL ;
const struct tsocket_address * const * dc_addrs3 = NULL ;
char * result = NULL ;
struct netlogon_samlogon_response * * responses = NULL ;
NTSTATUS status ;
2015-05-21 08:40:24 +03:00
char * kdc_str = talloc_asprintf ( mem_ctx , " %s \t \t kdc = %s \n " , " " ,
2014-04-02 19:37:34 +02:00
print_canonical_sockaddr_with_port ( mem_ctx , pss ) ) ;
2006-09-02 19:27:44 +00:00
if ( kdc_str = = NULL ) {
2012-11-05 09:46:49 +11:00
TALLOC_FREE ( frame ) ;
2006-09-02 19:27:44 +00:00
return NULL ;
}
2008-05-19 02:48:09 +02:00
/*
* First get the KDC ' s only in this site , the rest will be
* appended later
*/
2007-01-17 18:25:35 +00:00
2007-01-17 19:11:45 +00:00
if ( sitename ) {
get_kdc_list ( realm , sitename , & ip_srv_site , & count_site ) ;
2015-05-18 13:36:46 +03:00
DEBUG ( 10 , ( " got %d addresses from site %s search \n " , count_site ,
sitename ) ) ;
2006-09-02 19:27:44 +00:00
}
2007-01-17 18:25:35 +00:00
/* Get all KDC's. */
get_kdc_list ( realm , NULL , & ip_srv_nonsite , & count_nonsite ) ;
2015-05-18 13:36:46 +03:00
DEBUG ( 10 , ( " got %d addresses from site-less search \n " , count_nonsite ) ) ;
2007-01-17 18:25:35 +00:00
2011-10-12 19:41:45 +02:00
dc_addrs = talloc_array ( talloc_tos ( ) , struct sockaddr_storage ,
2015-05-18 13:36:46 +03:00
count_site + count_nonsite ) ;
2011-10-12 19:41:45 +02:00
if ( dc_addrs = = NULL ) {
2015-05-18 13:36:46 +03:00
goto out ;
2011-10-12 19:41:45 +02:00
}
2007-01-17 18:25:35 +00:00
2015-05-18 13:36:46 +03:00
num_dcs = 0 ;
2011-10-12 19:41:45 +02:00
2015-05-18 13:36:46 +03:00
for ( i = 0 ; i < count_site ; i + + ) {
if ( ! sockaddr_equal (
( const struct sockaddr * ) pss ,
( const struct sockaddr * ) & ip_srv_site [ i ] . ss ) ) {
add_sockaddr_unique ( dc_addrs , & num_dcs ,
& ip_srv_site [ i ] . ss ) ;
}
2011-10-12 19:41:45 +02:00
}
2015-05-18 13:36:46 +03:00
for ( i = 0 ; i < count_nonsite ; i + + ) {
if ( ! sockaddr_equal (
( const struct sockaddr * ) pss ,
( const struct sockaddr * ) & ip_srv_nonsite [ i ] . ss ) ) {
add_sockaddr_unique ( dc_addrs , & num_dcs ,
& ip_srv_nonsite [ i ] . ss ) ;
}
2011-10-12 19:41:45 +02:00
}
dc_addrs2 = talloc_zero_array ( talloc_tos ( ) ,
struct tsocket_address * ,
num_dcs ) ;
2015-05-18 13:36:46 +03:00
2017-12-07 17:38:21 +01:00
DBG_DEBUG ( " %zu additional KDCs to test \n " , num_dcs ) ;
2015-05-18 13:36:46 +03:00
if ( num_dcs = = 0 ) {
goto out ;
}
2011-10-12 19:41:45 +02:00
if ( dc_addrs2 = = NULL ) {
2015-05-18 13:36:46 +03:00
goto out ;
2011-10-12 19:41:45 +02:00
}
2007-01-17 18:25:35 +00:00
2011-10-12 19:41:45 +02:00
for ( i = 0 ; i < num_dcs ; i + + ) {
char addr [ INET6_ADDRSTRLEN ] ;
int ret ;
print_sockaddr ( addr , sizeof ( addr ) , & dc_addrs [ i ] ) ;
ret = tsocket_address_inet_from_strings ( dc_addrs2 , " ip " ,
addr , LDAP_PORT ,
& dc_addrs2 [ i ] ) ;
if ( ret ! = 0 ) {
status = map_nt_error_from_unix ( errno ) ;
DEBUG ( 2 , ( " Failed to create tsocket_address for %s - %s \n " ,
addr , nt_errstr ( status ) ) ) ;
2015-05-18 13:36:46 +03:00
goto out ;
2007-01-17 18:25:35 +00:00
}
2011-10-12 19:41:45 +02:00
}
dc_addrs3 = ( const struct tsocket_address * const * ) dc_addrs2 ;
status = cldap_multi_netlogon ( talloc_tos ( ) ,
dc_addrs3 , num_dcs ,
realm , lp_netbios_name ( ) ,
NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX ,
MIN ( num_dcs , 3 ) , timeval_current_ofs ( 3 , 0 ) , & responses ) ;
TALLOC_FREE ( dc_addrs2 ) ;
dc_addrs3 = NULL ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 10 , ( " get_kdc_ip_string: cldap_multi_netlogon failed: "
" %s \n " , nt_errstr ( status ) ) ) ;
2015-05-18 13:36:46 +03:00
goto out ;
2011-10-12 19:41:45 +02:00
}
for ( i = 0 ; i < num_dcs ; i + + ) {
char * new_kdc_str ;
if ( responses [ i ] = = NULL ) {
2007-01-17 18:25:35 +00:00
continue ;
}
2006-09-02 19:27:44 +00:00
/* Append to the string - inefficient but not done often. */
2015-05-21 08:40:24 +03:00
new_kdc_str = talloc_asprintf ( mem_ctx , " %s \t \t kdc = %s \n " ,
2014-01-13 15:59:26 +01:00
kdc_str ,
2014-04-02 19:37:34 +02:00
print_canonical_sockaddr_with_port ( mem_ctx , & dc_addrs [ i ] ) ) ;
2011-10-12 19:41:45 +02:00
if ( new_kdc_str = = NULL ) {
2015-05-18 13:36:46 +03:00
goto out ;
2006-09-02 19:27:44 +00:00
}
2011-10-12 19:41:45 +02:00
TALLOC_FREE ( kdc_str ) ;
kdc_str = new_kdc_str ;
2006-09-02 19:27:44 +00:00
}
2015-05-18 13:36:46 +03:00
out :
DEBUG ( 10 , ( " get_kdc_ip_string: Returning %s \n " , kdc_str ) ) ;
2006-09-02 19:27:44 +00:00
2011-10-12 19:41:45 +02:00
result = kdc_str ;
SAFE_FREE ( ip_srv_site ) ;
SAFE_FREE ( ip_srv_nonsite ) ;
TALLOC_FREE ( frame ) ;
return result ;
2006-09-02 19:27:44 +00:00
}
2006-08-31 01:20:21 +00:00
/************************************************************************
Create a specific krb5 . conf file in the private directory pointing
at a specific kdc for a realm . Keyed off domain name . Sets
KRB5_CONFIG environment variable to point to this file . Must be
run as root or will fail ( which is a good thing : - ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2016-05-30 21:21:41 +03:00
# if !defined(SAMBA4_USES_HEIMDAL) /* MIT version */
static char * get_enctypes ( TALLOC_CTX * mem_ctx )
{
char * aes_enctypes = NULL ;
const char * legacy_enctypes = " " ;
char * enctypes = NULL ;
aes_enctypes = talloc_strdup ( mem_ctx , " " ) ;
if ( aes_enctypes = = NULL ) {
goto done ;
}
if ( lp_kerberos_encryption_types ( ) = = KERBEROS_ETYPES_ALL | |
lp_kerberos_encryption_types ( ) = = KERBEROS_ETYPES_STRONG ) {
# ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
aes_enctypes = talloc_asprintf_append (
aes_enctypes , " %s " , " aes256-cts-hmac-sha1-96 " ) ;
if ( aes_enctypes = = NULL ) {
goto done ;
}
# endif
# ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
aes_enctypes = talloc_asprintf_append (
aes_enctypes , " %s " , " aes128-cts-hmac-sha1-96 " ) ;
if ( aes_enctypes = = NULL ) {
goto done ;
}
# endif
}
if ( lp_kerberos_encryption_types ( ) = = KERBEROS_ETYPES_ALL | |
lp_kerberos_encryption_types ( ) = = KERBEROS_ETYPES_LEGACY ) {
legacy_enctypes = " RC4-HMAC DES-CBC-CRC DES-CBC-MD5 " ;
}
enctypes =
talloc_asprintf ( mem_ctx , " \t default_tgs_enctypes = %s %s \n "
" \t default_tkt_enctypes = %s %s \n "
" \t preferred_enctypes = %s %s \n " ,
aes_enctypes , legacy_enctypes , aes_enctypes ,
legacy_enctypes , aes_enctypes , legacy_enctypes ) ;
done :
TALLOC_FREE ( aes_enctypes ) ;
return enctypes ;
}
# else /* Heimdal version */
static char * get_enctypes ( TALLOC_CTX * mem_ctx )
{
const char * aes_enctypes = " " ;
const char * legacy_enctypes = " " ;
char * enctypes = NULL ;
if ( lp_kerberos_encryption_types ( ) = = KERBEROS_ETYPES_ALL | |
lp_kerberos_encryption_types ( ) = = KERBEROS_ETYPES_STRONG ) {
aes_enctypes =
" aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 " ;
}
if ( lp_kerberos_encryption_types ( ) = = KERBEROS_ETYPES_ALL | |
lp_kerberos_encryption_types ( ) = = KERBEROS_ETYPES_LEGACY ) {
legacy_enctypes = " arcfour-hmac-md5 des-cbc-crc des-cbc-md5 " ;
}
enctypes = talloc_asprintf ( mem_ctx , " \t default_etypes = %s %s \n " ,
aes_enctypes , legacy_enctypes ) ;
return enctypes ;
}
# endif
2007-10-24 14:16:54 -07:00
bool create_local_private_krb5_conf_for_domain ( const char * realm ,
const char * domain ,
const char * sitename ,
2014-03-07 14:47:31 +01:00
const struct sockaddr_storage * pss )
2006-08-31 01:20:21 +00:00
{
2009-08-26 14:56:41 +02:00
char * dname ;
2006-09-07 04:01:11 +00:00
char * tmpname = NULL ;
2006-09-02 01:33:13 +00:00
char * fname = NULL ;
2006-08-31 01:20:21 +00:00
char * file_contents = NULL ;
2006-09-07 04:17:23 +00:00
char * kdc_ip_string = NULL ;
2006-08-31 01:20:21 +00:00
size_t flen = 0 ;
2006-09-07 04:01:11 +00:00
ssize_t ret ;
int fd ;
2006-08-31 04:14:08 +00:00
char * realm_upper = NULL ;
2009-01-16 00:10:29 +01:00
bool result = false ;
2016-05-30 21:21:41 +03:00
char * enctypes = NULL ;
2016-11-23 14:40:42 +01:00
const char * include_system_krb5 = " " ;
2013-02-25 17:34:21 +01:00
mode_t mask ;
2006-08-31 01:20:21 +00:00
2009-08-26 14:56:41 +02:00
if ( ! lp_create_krb5_conf ( ) ) {
return false ;
}
2013-08-05 09:25:11 +02:00
if ( realm = = NULL ) {
DEBUG ( 0 , ( " No realm has been specified! Do you really want to "
" join an Active Directory server? \n " ) ) ;
return false ;
}
2014-03-07 14:47:31 +01:00
if ( domain = = NULL | | pss = = NULL ) {
2013-05-17 15:14:35 +02:00
return false ;
}
2018-08-16 10:51:44 +02:00
dname = lock_path ( talloc_tos ( ) , " smb_krb5 " ) ;
2006-09-02 01:33:13 +00:00
if ( ! dname ) {
2009-01-16 00:10:29 +01:00
return false ;
2006-09-02 01:33:13 +00:00
}
2006-09-02 05:55:47 +00:00
if ( ( mkdir ( dname , 0755 ) = = - 1 ) & & ( errno ! = EEXIST ) ) {
2006-09-02 01:33:13 +00:00
DEBUG ( 0 , ( " create_local_private_krb5_conf_for_domain: "
" failed to create directory %s. Error was %s \n " ,
dname , strerror ( errno ) ) ) ;
2009-01-16 00:10:29 +01:00
goto done ;
2006-09-02 01:33:13 +00:00
}
2018-08-16 10:51:44 +02:00
tmpname = lock_path ( talloc_tos ( ) , " smb_tmp_krb5.XXXXXX " ) ;
2006-09-07 04:01:11 +00:00
if ( ! tmpname ) {
2009-01-16 00:10:29 +01:00
goto done ;
2006-09-07 04:01:11 +00:00
}
2006-09-02 01:33:13 +00:00
fname = talloc_asprintf ( dname , " %s/krb5.conf.%s " , dname , domain ) ;
2006-08-31 01:20:21 +00:00
if ( ! fname ) {
2009-01-16 00:10:29 +01:00
goto done ;
2006-08-31 01:20:21 +00:00
}
2006-09-02 01:23:08 +00:00
DEBUG ( 10 , ( " create_local_private_krb5_conf_for_domain: fname = %s, realm = %s, domain = %s \n " ,
fname , realm , domain ) ) ;
2006-08-31 04:14:08 +00:00
realm_upper = talloc_strdup ( fname , realm ) ;
2012-08-08 15:35:28 -07:00
if ( ! strupper_m ( realm_upper ) ) {
goto done ;
}
2006-08-31 04:14:08 +00:00
2014-03-07 14:47:31 +01:00
kdc_ip_string = get_kdc_ip_string ( dname , realm , sitename , pss ) ;
2006-09-02 19:27:44 +00:00
if ( ! kdc_ip_string ) {
2009-01-16 00:10:29 +01:00
goto done ;
2006-09-02 19:27:44 +00:00
}
2007-10-24 14:16:54 -07:00
2016-05-30 21:21:41 +03:00
enctypes = get_enctypes ( fname ) ;
if ( enctypes = = NULL ) {
2011-12-19 10:52:58 +01:00
goto done ;
}
2016-11-23 14:40:42 +01:00
# if !defined(SAMBA4_USES_HEIMDAL)
if ( lp_include_system_krb5_conf ( ) ) {
include_system_krb5 = " include /etc/krb5.conf " ;
}
# endif
2016-05-30 21:21:41 +03:00
file_contents =
2016-11-23 14:40:42 +01:00
talloc_asprintf ( fname ,
" [libdefaults] \n \t default_realm = %s \n "
" %s "
" \t dns_lookup_realm = false \n \n "
" [realms] \n \t %s = { \n "
" %s \t } \n "
" %s \n " ,
realm_upper ,
enctypes ,
realm_upper ,
kdc_ip_string ,
include_system_krb5 ) ;
2006-08-31 01:20:21 +00:00
if ( ! file_contents ) {
2009-01-16 00:10:29 +01:00
goto done ;
2006-08-31 01:20:21 +00:00
}
flen = strlen ( file_contents ) ;
2006-08-31 01:27:51 +00:00
2013-02-25 17:34:21 +01:00
mask = umask ( S_IRWXO | S_IRWXG ) ;
2009-04-20 23:58:26 +02:00
fd = mkstemp ( tmpname ) ;
2013-02-25 17:34:21 +01:00
umask ( mask ) ;
2006-09-07 04:01:11 +00:00
if ( fd = = - 1 ) {
DEBUG ( 0 , ( " create_local_private_krb5_conf_for_domain: smb_mkstemp failed, "
" for file %s. Errno %s \n " ,
tmpname , strerror ( errno ) ) ) ;
2009-01-16 00:10:29 +01:00
goto done ;
2006-08-31 01:20:21 +00:00
}
2006-09-08 01:16:01 +00:00
if ( fchmod ( fd , 0644 ) = = - 1 ) {
DEBUG ( 0 , ( " create_local_private_krb5_conf_for_domain: fchmod failed for %s. "
" Errno %s \n " ,
tmpname , strerror ( errno ) ) ) ;
unlink ( tmpname ) ;
close ( fd ) ;
2009-01-16 00:10:29 +01:00
goto done ;
2006-09-08 01:16:01 +00:00
}
2006-09-07 04:01:11 +00:00
ret = write ( fd , file_contents , flen ) ;
2006-09-02 04:50:08 +00:00
if ( flen ! = ret ) {
2006-09-07 04:01:11 +00:00
DEBUG ( 0 , ( " create_local_private_krb5_conf_for_domain: write failed, "
" returned %d (should be %u). Errno %s \n " ,
( int ) ret , ( unsigned int ) flen , strerror ( errno ) ) ) ;
unlink ( tmpname ) ;
close ( fd ) ;
2009-01-16 00:10:29 +01:00
goto done ;
2006-08-31 01:20:21 +00:00
}
2006-09-07 04:01:11 +00:00
if ( close ( fd ) = = - 1 ) {
DEBUG ( 0 , ( " create_local_private_krb5_conf_for_domain: close failed. "
2006-09-02 04:50:08 +00:00
" Errno %s \n " , strerror ( errno ) ) ) ;
2006-09-07 04:01:11 +00:00
unlink ( tmpname ) ;
2009-01-16 00:10:29 +01:00
goto done ;
2006-09-07 04:01:11 +00:00
}
if ( rename ( tmpname , fname ) = = - 1 ) {
DEBUG ( 0 , ( " create_local_private_krb5_conf_for_domain: rename "
" of %s to %s failed. Errno %s \n " ,
tmpname , fname , strerror ( errno ) ) ) ;
unlink ( tmpname ) ;
2009-01-16 00:10:29 +01:00
goto done ;
2006-08-31 01:20:21 +00:00
}
2006-08-31 04:14:08 +00:00
DEBUG ( 5 , ( " create_local_private_krb5_conf_for_domain: wrote "
2008-01-16 13:21:46 -08:00
" file %s with realm %s KDC list = %s \n " ,
fname , realm_upper , kdc_ip_string ) ) ;
2006-08-31 04:14:08 +00:00
2006-09-02 01:34:37 +00:00
/* Set the environment variable to this file. */
setenv ( " KRB5_CONFIG " , fname , 1 ) ;
2006-09-07 04:01:11 +00:00
2009-01-16 00:10:29 +01:00
result = true ;
2006-09-07 04:01:11 +00:00
# if defined(OVERWRITE_SYSTEM_KRB5_CONF)
2006-09-07 17:29:23 +00:00
# define SYSTEM_KRB5_CONF_PATH " / etc / krb5.conf"
2006-09-07 04:01:11 +00:00
/* Insanity, sheer insanity..... */
2006-09-07 18:34:02 +00:00
if ( strequal ( realm , lp_realm ( ) ) ) {
2011-06-01 16:40:05 -07:00
SMB_STRUCT_STAT sbuf ;
if ( sys_lstat ( SYSTEM_KRB5_CONF_PATH , & sbuf , false ) = = 0 ) {
if ( S_ISLNK ( sbuf . st_ex_mode ) & & sbuf . st_ex_size ) {
int lret ;
size_t alloc_size = sbuf . st_ex_size + 1 ;
2011-06-07 11:30:12 +10:00
char * linkpath = talloc_array ( talloc_tos ( ) , char ,
2011-06-01 16:40:05 -07:00
alloc_size ) ;
if ( ! linkpath ) {
goto done ;
}
lret = readlink ( SYSTEM_KRB5_CONF_PATH , linkpath ,
alloc_size - 1 ) ;
if ( lret = = - 1 ) {
TALLOC_FREE ( linkpath ) ;
goto done ;
}
linkpath [ lret ] = ' \0 ' ;
if ( strcmp ( linkpath , fname ) = = 0 ) {
/* Symlink already exists. */
TALLOC_FREE ( linkpath ) ;
goto done ;
}
TALLOC_FREE ( linkpath ) ;
}
2006-09-07 04:01:11 +00:00
}
2006-09-07 17:29:23 +00:00
/* Try and replace with a symlink. */
if ( symlink ( fname , SYSTEM_KRB5_CONF_PATH ) = = - 1 ) {
2011-06-01 16:40:05 -07:00
const char * newpath = SYSTEM_KRB5_CONF_PATH " .saved " ;
2006-09-07 17:29:23 +00:00
if ( errno ! = EEXIST ) {
DEBUG ( 0 , ( " create_local_private_krb5_conf_for_domain: symlink "
" of %s to %s failed. Errno %s \n " ,
fname , SYSTEM_KRB5_CONF_PATH , strerror ( errno ) ) ) ;
2009-01-16 00:10:29 +01:00
goto done ; /* Not a fatal error. */
2006-09-07 17:29:23 +00:00
}
/* Yes, this is a race conditon... too bad. */
2007-11-20 18:55:36 -08:00
if ( rename ( SYSTEM_KRB5_CONF_PATH , newpath ) = = - 1 ) {
2006-09-07 17:29:23 +00:00
DEBUG ( 0 , ( " create_local_private_krb5_conf_for_domain: rename "
" of %s to %s failed. Errno %s \n " ,
2007-11-20 18:55:36 -08:00
SYSTEM_KRB5_CONF_PATH , newpath ,
2006-09-07 17:29:23 +00:00
strerror ( errno ) ) ) ;
2009-01-16 00:10:29 +01:00
goto done ; /* Not a fatal error. */
2006-09-07 17:29:23 +00:00
}
2007-11-20 18:55:36 -08:00
if ( symlink ( fname , SYSTEM_KRB5_CONF_PATH ) = = - 1 ) {
2006-09-07 17:29:23 +00:00
DEBUG ( 0 , ( " create_local_private_krb5_conf_for_domain: "
" forced symlink of %s to /etc/krb5.conf failed. Errno %s \n " ,
fname , strerror ( errno ) ) ) ;
2009-01-16 00:10:29 +01:00
goto done ; /* Not a fatal error. */
2006-09-07 17:29:23 +00:00
}
2006-09-07 04:01:11 +00:00
}
}
# endif
2009-01-16 00:10:29 +01:00
done :
2009-01-16 00:17:51 +01:00
TALLOC_FREE ( tmpname ) ;
2006-09-02 01:34:37 +00:00
TALLOC_FREE ( dname ) ;
2009-01-16 00:10:29 +01:00
return result ;
2006-08-31 01:20:21 +00:00
}
2001-11-24 14:16:41 +00:00
# endif