2001-10-05 04:20:06 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2001-10-05 04:20:06 +04:00
Winbind daemon connection manager
Copyright ( C ) Tim Potter 2001
2002-07-15 14:35:28 +04:00
Copyright ( C ) Andrew Bartlett 2002
2001-10-05 04:20:06 +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
2001-11-28 02:48:44 +03:00
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2001-10-05 04:20:06 +04:00
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 .
*/
/*
We need to manage connections to domain controllers without having to
mess up the main winbindd code with other issues . The aim of the
connection manager is to :
- make connections to domain controllers and cache them
- re - establish connections when networks or servers go down
- centralise the policy on connection timeouts , domain controller
2001-11-28 02:48:44 +03:00
selection etc
2001-10-05 04:20:06 +04:00
- manage re - entrancy for when winbindd becomes able to handle
2001-11-28 02:48:44 +03:00
multiple outstanding rpc requests
2001-10-05 04:20:06 +04:00
Why not have connection management as part of the rpc layer like tng ?
Good question . This code may morph into libsmb / rpc_cache . c or something
2001-11-28 02:48:44 +03:00
like that but at the moment it ' s simply staying as part of winbind . I
2001-10-05 04:20:06 +04:00
think the TNG architecture of forcing every user of the rpc layer to use
2001-11-28 02:48:44 +03:00
the connection caching system is a bad idea . It should be an optional
2001-11-15 09:55:56 +03:00
method of using the routines .
2001-10-05 04:20:06 +04:00
The TNG design is quite good but I disagree with some aspects of the
implementation . - tpot
*/
/*
TODO :
- I ' m pretty annoyed by all the make_nmb_name ( ) stuff . It should be
moved down into another function .
2001-10-08 04:34:14 +04:00
- Take care when destroying cli_structs as they can be shared between
various sam handles .
2001-10-05 04:20:06 +04:00
*/
2003-11-12 04:51:10 +03:00
# include "includes.h"
2001-10-05 04:20:06 +04:00
# include "winbindd.h"
2002-07-15 14:35:28 +04:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_WINBIND
2001-11-28 02:48:44 +03:00
/* Global list of connections. Initially a DLIST but can become a hash
2001-11-14 09:18:13 +03:00
table or whatever later . */
2001-10-05 04:20:06 +04:00
struct winbindd_cm_conn {
2001-11-15 22:40:00 +03:00
struct winbindd_cm_conn * prev , * next ;
fstring domain ;
fstring controller ;
fstring pipe_name ;
2003-01-16 23:08:26 +03:00
size_t mutex_ref_count ;
2001-11-15 22:40:00 +03:00
struct cli_state * cli ;
POLICY_HND pol ;
2001-10-05 04:20:06 +04:00
} ;
2001-12-01 15:31:43 +03:00
static struct winbindd_cm_conn * cm_conns = NULL ;
2001-10-05 04:20:06 +04:00
2001-12-11 08:19:15 +03:00
/* Choose between anonymous or authenticated connections. We need to use
an authenticated connection if DCs have the RestrictAnonymous registry
entry set > 0 , or the " Additional restrictions for anonymous
2002-02-15 16:28:59 +03:00
connections " set in the win2k Local Security Policy.
Caller to free ( ) result in domain , username , password
*/
2001-12-11 08:19:15 +03:00
2002-02-15 16:28:59 +03:00
static void cm_get_ipc_userpass ( char * * username , char * * domain , char * * password )
2001-12-11 08:19:15 +03:00
{
2002-02-15 16:28:59 +03:00
* username = secrets_fetch ( SECRETS_AUTH_USER , NULL ) ;
* domain = secrets_fetch ( SECRETS_AUTH_DOMAIN , NULL ) ;
* password = secrets_fetch ( SECRETS_AUTH_PASSWORD , NULL ) ;
if ( * username & & * * username ) {
2002-11-02 04:06:10 +03:00
if ( ! * domain | | ! * * domain )
2002-02-15 16:28:59 +03:00
* domain = smb_xstrdup ( lp_workgroup ( ) ) ;
2002-11-02 04:06:10 +03:00
if ( ! * password | | ! * * password )
* password = smb_xstrdup ( " " ) ;
DEBUG ( 3 , ( " IPC$ connections done by user %s \\ %s \n " ,
* domain , * username ) ) ;
2002-02-15 16:28:59 +03:00
} else {
2001-12-11 08:19:15 +03:00
DEBUG ( 3 , ( " IPC$ connections done anonymously \n " ) ) ;
2002-02-15 16:28:59 +03:00
* username = smb_xstrdup ( " " ) ;
* domain = smb_xstrdup ( " " ) ;
* password = smb_xstrdup ( " " ) ;
}
2001-12-11 08:19:15 +03:00
}
2002-02-15 16:28:59 +03:00
/* Open a connction to the remote server, cache failures for 30 seconds */
2004-01-05 07:10:28 +03:00
static NTSTATUS cm_open_connection ( const struct winbindd_domain * domain , const int pipe_index ,
struct winbindd_cm_conn * new_conn )
2001-10-05 04:20:06 +04:00
{
2002-02-15 16:28:59 +03:00
NTSTATUS result ;
2003-08-20 02:47:10 +04:00
char * machine_password ;
char * machine_krb5_principal , * ipc_username , * ipc_domain , * ipc_password ;
2002-02-15 16:28:59 +03:00
struct in_addr dc_ip ;
2002-10-17 21:10:24 +04:00
int i ;
BOOL retry = True ;
2002-02-15 16:28:59 +03:00
ZERO_STRUCT ( dc_ip ) ;
2001-11-15 22:40:00 +03:00
2004-01-05 07:10:28 +03:00
fstrcpy ( new_conn - > domain , domain - > name ) ;
2001-11-28 02:48:44 +03:00
2003-07-01 00:45:14 +04:00
/* connection failure cache has been moved inside of get_dc_name
2003-06-06 18:11:14 +04:00
so we can deal with half dead DC ' s - - jerry */
2002-10-02 03:07:12 +04:00
2004-01-05 07:10:28 +03:00
if ( ! get_dc_name ( domain - > name , domain - > alt_name [ 0 ] ? domain - > alt_name : NULL ,
new_conn - > controller , & dc_ip ) ) {
2002-10-02 03:07:12 +04:00
result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND ;
2004-01-05 07:10:28 +03:00
add_failed_connection_entry ( domain - > name , " " , result ) ;
2002-10-02 03:07:12 +04:00
return result ;
}
2001-11-15 22:40:00 +03:00
/* Initialise SMB connection */
2004-01-05 07:10:28 +03:00
fstrcpy ( new_conn - > pipe_name , get_pipe_name_from_index ( pipe_index ) ) ;
2001-10-05 04:20:06 +04:00
2004-01-05 07:10:28 +03:00
/* grab stored passwords */
2003-08-20 02:47:10 +04:00
machine_password = secrets_fetch_machine_password ( lp_workgroup ( ) , NULL , NULL ) ;
if ( asprintf ( & machine_krb5_principal , " %s$@%s " , global_myname ( ) , lp_realm ( ) ) = = - 1 ) {
SAFE_FREE ( machine_password ) ;
return NT_STATUS_NO_MEMORY ;
}
2001-10-05 04:20:06 +04:00
2003-08-20 02:47:10 +04:00
cm_get_ipc_userpass ( & ipc_username , & ipc_domain , & ipc_password ) ;
2001-10-05 04:20:06 +04:00
2002-10-17 21:10:24 +04:00
for ( i = 0 ; retry & & ( i < 3 ) ; i + + ) {
2003-03-18 02:06:12 +03:00
BOOL got_mutex ;
if ( ! ( got_mutex = secrets_named_mutex ( new_conn - > controller , WINBIND_SERVER_MUTEX_WAIT_TIME ) ) ) {
2003-01-16 23:08:26 +03:00
DEBUG ( 0 , ( " cm_open_connection: mutex grab failed for %s \n " , new_conn - > controller ) ) ;
result = NT_STATUS_POSSIBLE_DEADLOCK ;
continue ;
}
2003-04-02 10:04:51 +04:00
2003-08-20 02:47:10 +04:00
new_conn - > cli = NULL ;
result = cli_start_connection ( & new_conn - > cli , global_myname ( ) ,
new_conn - > controller ,
& dc_ip , 0 , Undefined ,
CLI_FULL_CONNECTION_USE_KERBEROS ,
& retry ) ;
if ( NT_STATUS_IS_OK ( result ) ) {
2003-08-20 08:25:09 +04:00
/* reset the error code */
result = NT_STATUS_UNSUCCESSFUL ;
/* Krb5 session */
2003-08-20 02:47:10 +04:00
if ( ( lp_security ( ) = = SEC_ADS )
& & ( new_conn - > cli - > protocol > = PROTOCOL_NT1 & & new_conn - > cli - > capabilities & CAP_EXTENDED_SECURITY ) ) {
2004-01-08 11:19:18 +03:00
ADS_STATUS ads_status ;
2003-08-20 02:47:10 +04:00
new_conn - > cli - > use_kerberos = True ;
DEBUG ( 5 , ( " connecting to %s from %s with kerberos principal [%s] \n " ,
new_conn - > controller , global_myname ( ) , machine_krb5_principal ) ) ;
2004-01-08 11:19:18 +03:00
ads_status = cli_session_setup_spnego ( new_conn - > cli , machine_krb5_principal ,
machine_password ,
lp_workgroup ( ) ) ;
if ( ! ADS_ERR_OK ( ads_status ) ) {
DEBUG ( 4 , ( " failed kerberos session setup with %s \n " , ads_errstr ( ads_status ) ) ) ;
result = ads_ntstatus ( ads_status ) ;
} else {
result = NT_STATUS_OK ;
2003-08-20 02:47:10 +04:00
}
}
new_conn - > cli - > use_kerberos = False ;
2003-08-20 08:25:09 +04:00
/* only do this is we have a username/password for thr IPC$ connection */
if ( ! NT_STATUS_IS_OK ( result )
& & new_conn - > cli - > sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
& & strlen ( ipc_username ) )
{
2003-08-20 02:47:10 +04:00
DEBUG ( 5 , ( " connecting to %s from %s with username [%s] \\ [%s] \n " ,
new_conn - > controller , global_myname ( ) , ipc_domain , ipc_username ) ) ;
2003-08-20 08:25:09 +04:00
result = NT_STATUS_OK ;
2003-08-20 02:47:10 +04:00
if ( ! cli_session_setup ( new_conn - > cli , ipc_username ,
ipc_password , strlen ( ipc_password ) + 1 ,
ipc_password , strlen ( ipc_password ) + 1 ,
2004-01-06 03:32:24 +03:00
ipc_domain ) ) {
2003-08-20 02:47:10 +04:00
result = cli_nt_error ( new_conn - > cli ) ;
2003-08-20 08:25:09 +04:00
DEBUG ( 4 , ( " failed authenticated session setup with %s \n " , nt_errstr ( result ) ) ) ;
2003-08-20 02:47:10 +04:00
if ( NT_STATUS_IS_OK ( result ) )
result = NT_STATUS_UNSUCCESSFUL ;
}
}
2003-08-20 08:25:09 +04:00
/* anonymous is all that is left if we get to here */
2003-08-20 02:47:10 +04:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
2003-08-20 08:25:09 +04:00
DEBUG ( 5 , ( " anonymous connection attempt to %s from %s \n " ,
new_conn - > controller , global_myname ( ) ) ) ;
result = NT_STATUS_OK ;
if ( ! cli_session_setup ( new_conn - > cli , " " , NULL , 0 , NULL , 0 , " " ) )
{
2003-08-20 02:47:10 +04:00
result = cli_nt_error ( new_conn - > cli ) ;
2003-08-20 08:25:09 +04:00
DEBUG ( 4 , ( " failed anonymous session setup with %s \n " , nt_errstr ( result ) ) ) ;
2003-08-20 02:47:10 +04:00
if ( NT_STATUS_IS_OK ( result ) )
result = NT_STATUS_UNSUCCESSFUL ;
}
}
2003-08-20 08:25:09 +04:00
2003-08-20 02:47:10 +04:00
if ( NT_STATUS_IS_OK ( result ) & & ! cli_send_tconX ( new_conn - > cli , " IPC$ " , " IPC " ,
" " , 0 ) ) {
result = cli_nt_error ( new_conn - > cli ) ;
DEBUG ( 1 , ( " failed tcon_X with %s \n " , nt_errstr ( result ) ) ) ;
cli_shutdown ( new_conn - > cli ) ;
if ( NT_STATUS_IS_OK ( result ) ) {
result = NT_STATUS_UNSUCCESSFUL ;
}
}
}
if ( NT_STATUS_IS_OK ( result ) ) {
struct ntuser_creds creds ;
init_creds ( & creds , ipc_username , ipc_domain , ipc_password ) ;
cli_init_creds ( new_conn - > cli , & creds ) ;
}
if ( got_mutex )
secrets_named_mutex_release ( new_conn - > controller ) ;
2002-10-17 21:10:24 +04:00
if ( NT_STATUS_IS_OK ( result ) )
break ;
}
2001-10-05 04:20:06 +04:00
2002-02-15 16:28:59 +03:00
SAFE_FREE ( ipc_username ) ;
SAFE_FREE ( ipc_domain ) ;
SAFE_FREE ( ipc_password ) ;
2003-08-20 02:47:10 +04:00
SAFE_FREE ( machine_password ) ;
2001-11-15 06:33:12 +03:00
2002-02-15 16:28:59 +03:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
2004-01-05 07:10:28 +03:00
add_failed_connection_entry ( domain - > name , new_conn - > controller , result ) ;
2002-02-15 16:28:59 +03:00
return result ;
2001-11-15 22:40:00 +03:00
}
2002-02-15 16:28:59 +03:00
2003-07-25 22:00:57 +04:00
/* set the domain if empty; needed for schannel connections */
if ( ! * new_conn - > cli - > domain )
2004-01-05 07:10:28 +03:00
fstrcpy ( new_conn - > cli - > domain , domain - > name ) ;
2003-07-25 22:00:57 +04:00
2002-10-05 01:42:04 +04:00
if ( ! cli_nt_session_open ( new_conn - > cli , pipe_index ) ) {
2002-02-15 16:28:59 +03:00
result = NT_STATUS_PIPE_NOT_AVAILABLE ;
2002-10-08 22:32:42 +04:00
/*
* only cache a failure if we are not trying to open the
* * * win2k * * specific lsarpc UUID . This could be an NT PDC
* and therefore a failure is normal . This should probably
* be abstracted to a check for 2 k specific pipes and wondering
* if the PDC is an NT4 box . but since there is only one 2 k
* specific UUID right now , i ' m not going to bother . - - jerry
*/
if ( ! is_win2k_pipe ( pipe_index ) )
2004-01-05 07:10:28 +03:00
add_failed_connection_entry ( domain - > name , new_conn - > controller , result ) ;
2001-11-15 22:40:00 +03:00
cli_shutdown ( new_conn - > cli ) ;
2002-02-15 16:28:59 +03:00
return result ;
}
2001-11-15 22:40:00 +03:00
2002-02-15 16:28:59 +03:00
return NT_STATUS_OK ;
2001-10-05 04:20:06 +04:00
}
2003-07-31 09:43:47 +04:00
/************************************************************************
Wrapper around statuc cm_open_connection to retreive a freshly
setup cli_state struct
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-01-05 07:10:28 +03:00
NTSTATUS cm_fresh_connection ( struct winbindd_domain * domain , const int pipe_index ,
2003-07-31 09:43:47 +04:00
struct cli_state * * cli )
{
NTSTATUS result ;
struct winbindd_cm_conn conn ;
result = cm_open_connection ( domain , pipe_index , & conn ) ;
if ( NT_STATUS_IS_OK ( result ) )
* cli = conn . cli ;
return result ;
}
2001-10-27 08:48:22 +04:00
/* Return true if a connection is still alive */
static BOOL connection_ok ( struct winbindd_cm_conn * conn )
{
2002-02-15 16:28:59 +03:00
if ( ! conn ) {
2003-05-08 12:02:52 +04:00
smb_panic ( " Invalid parameter passed to connection_ok(): conn was NULL! \n " ) ;
2002-02-15 16:28:59 +03:00
return False ;
}
2002-03-18 13:53:02 +03:00
if ( ! conn - > cli ) {
2003-05-08 12:02:52 +04:00
DEBUG ( 3 , ( " Connection to %s for domain %s (pipe %s) has NULL conn->cli! \n " ,
2002-03-18 13:53:02 +03:00
conn - > controller , conn - > domain , conn - > pipe_name ) ) ;
return False ;
}
if ( ! conn - > cli - > initialised ) {
2003-05-08 12:02:52 +04:00
DEBUG ( 3 , ( " Connection to %s for domain %s (pipe %s) was never initialised! \n " ,
2002-02-11 04:29:07 +03:00
conn - > controller , conn - > domain , conn - > pipe_name ) ) ;
2001-11-15 22:40:00 +03:00
return False ;
2002-02-11 04:29:07 +03:00
}
2001-10-27 08:48:22 +04:00
2002-02-11 04:29:07 +03:00
if ( conn - > cli - > fd = = - 1 ) {
DEBUG ( 3 , ( " Connection to %s for domain %s (pipe %s) has died or was never started (fd == -1) \n " ,
conn - > controller , conn - > domain , conn - > pipe_name ) ) ;
2001-11-15 22:40:00 +03:00
return False ;
2002-02-11 04:29:07 +03:00
}
2001-10-27 08:48:22 +04:00
2001-11-15 22:40:00 +03:00
return True ;
2001-10-27 08:48:22 +04:00
}
2003-05-08 12:02:52 +04:00
/* Search the cache for a connection. If there is a broken one,
shut it down properly and return NULL . */
2001-10-05 04:20:06 +04:00
2004-01-05 07:10:28 +03:00
static void find_cm_connection ( struct winbindd_domain * domain , const char * pipe_name ,
2003-05-08 12:02:52 +04:00
struct winbindd_cm_conn * * conn_out )
2001-10-05 04:20:06 +04:00
{
2003-05-15 01:28:54 +04:00
struct winbindd_cm_conn * conn ;
2003-05-15 00:48:48 +04:00
2003-05-15 01:28:54 +04:00
for ( conn = cm_conns ; conn ; ) {
2004-01-05 07:10:28 +03:00
if ( strequal ( conn - > domain , domain - > name ) & &
2002-02-15 16:28:59 +03:00
strequal ( conn - > pipe_name , pipe_name ) ) {
2001-11-15 22:40:00 +03:00
if ( ! connection_ok ( conn ) ) {
2003-05-15 01:28:54 +04:00
/* Dead connection - remove it. */
struct winbindd_cm_conn * conn_temp = conn - > next ;
2003-01-16 03:27:27 +03:00
if ( conn - > cli )
2002-02-15 16:28:59 +03:00
cli_shutdown ( conn - > cli ) ;
2001-11-15 22:40:00 +03:00
DLIST_REMOVE ( cm_conns , conn ) ;
2002-02-11 04:29:07 +03:00
SAFE_FREE ( conn ) ;
2003-05-15 01:28:54 +04:00
conn = conn_temp ; /* Keep the loop moving */
continue ;
2002-02-15 16:28:59 +03:00
} else {
break ;
2001-11-15 22:40:00 +03:00
}
}
2003-05-15 01:28:54 +04:00
conn = conn - > next ;
2001-11-15 22:40:00 +03:00
}
2003-05-08 12:02:52 +04:00
* conn_out = conn ;
}
/* Initialize a new connection up to the RPC BIND. */
2004-01-05 07:10:28 +03:00
static NTSTATUS new_cm_connection ( struct winbindd_domain * domain , const char * pipe_name ,
2003-05-08 12:02:52 +04:00
struct winbindd_cm_conn * * conn_out )
{
struct winbindd_cm_conn * conn ;
NTSTATUS result ;
if ( ! ( conn = malloc ( sizeof ( * conn ) ) ) )
return NT_STATUS_NO_MEMORY ;
2002-02-15 16:28:59 +03:00
2003-05-08 12:02:52 +04:00
ZERO_STRUCTP ( conn ) ;
2002-02-15 16:28:59 +03:00
2003-05-08 12:02:52 +04:00
if ( ! NT_STATUS_IS_OK ( result = cm_open_connection ( domain , get_pipe_index ( pipe_name ) , conn ) ) ) {
DEBUG ( 3 , ( " Could not open a connection to %s for %s (%s) \n " ,
2004-01-05 07:10:28 +03:00
domain - > name , pipe_name , nt_errstr ( result ) ) ) ;
2003-05-08 12:02:52 +04:00
SAFE_FREE ( conn ) ;
return result ;
2002-02-15 16:28:59 +03:00
}
2003-05-08 12:02:52 +04:00
DLIST_ADD ( cm_conns , conn ) ;
2002-02-15 16:28:59 +03:00
* conn_out = conn ;
return NT_STATUS_OK ;
}
2001-10-05 04:20:06 +04:00
2003-05-08 12:02:52 +04:00
/* Get a connection to the remote DC and open the pipe. If there is already a connection, use that */
2004-01-05 07:10:28 +03:00
static NTSTATUS get_connection_from_cache ( struct winbindd_domain * domain , const char * pipe_name ,
2003-05-08 12:02:52 +04:00
struct winbindd_cm_conn * * conn_out )
{
find_cm_connection ( domain , pipe_name , conn_out ) ;
2003-05-15 22:25:03 +04:00
if ( * conn_out ! = NULL )
2003-05-08 12:02:52 +04:00
return NT_STATUS_OK ;
return new_cm_connection ( domain , pipe_name , conn_out ) ;
}
2002-10-05 01:42:04 +04:00
/**********************************************************************************
2004-01-08 11:19:18 +03:00
We can ' sense ' certain things about the DC by it ' s replies to certain questions .
This tells us if this particular remote server is Active Directory , and if it is
native mode .
2002-10-05 01:42:04 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-01-08 11:19:18 +03:00
void set_dc_type_and_flags ( struct winbindd_domain * domain )
2002-10-05 01:42:04 +04:00
{
NTSTATUS result ;
struct winbindd_cm_conn conn ;
DS_DOMINFO_CTR ctr ;
2004-01-08 11:19:18 +03:00
TALLOC_CTX * mem_ctx ;
2002-10-05 01:42:04 +04:00
ZERO_STRUCT ( conn ) ;
ZERO_STRUCT ( ctr ) ;
2004-01-08 11:19:18 +03:00
domain - > native_mode = False ;
domain - > active_directory = False ;
2002-10-05 01:42:04 +04:00
2003-03-18 02:06:12 +03:00
if ( ! NT_STATUS_IS_OK ( result = cm_open_connection ( domain , PI_LSARPC_DS , & conn ) ) ) {
2004-01-08 11:19:18 +03:00
DEBUG ( 5 , ( " set_dc_type_and_flags: Could not open a connection to %s for PIPE_LSARPC (%s) \n " ,
2004-01-05 07:10:28 +03:00
domain - > name , nt_errstr ( result ) ) ) ;
2004-01-08 11:19:18 +03:00
return ;
2002-10-05 01:42:04 +04:00
}
if ( conn . cli ) {
if ( ! NT_STATUS_IS_OK ( cli_ds_getprimarydominfo ( conn . cli ,
2003-01-16 03:27:27 +03:00
conn . cli - > mem_ctx , DsRolePrimaryDomainInfoBasic , & ctr ) ) ) {
2002-10-05 01:42:04 +04:00
goto done ;
}
}
if ( ( ctr . basic - > flags & DSROLE_PRIMARY_DS_RUNNING )
2003-01-16 03:27:27 +03:00
& & ! ( ctr . basic - > flags & DSROLE_PRIMARY_DS_MIXED_MODE ) )
2004-01-08 11:19:18 +03:00
domain - > native_mode = True ;
2002-10-05 01:42:04 +04:00
2004-01-08 11:19:18 +03:00
/* Cheat - shut down the DS pipe, and open LSA */
cli_nt_session_close ( conn . cli ) ;
if ( cli_nt_session_open ( conn . cli , PI_LSARPC ) ) {
char * domain_name = NULL ;
char * dns_name = NULL ;
DOM_SID * dom_sid = NULL ;
mem_ctx = talloc_init ( " set_dc_type_and_flags on domain %s \n " , domain - > name ) ;
if ( ! mem_ctx ) {
DEBUG ( 1 , ( " set_dc_type_and_flags: talloc_init() failed \n " ) ) ;
return ;
}
result = cli_lsa_open_policy2 ( conn . cli , mem_ctx , True ,
SEC_RIGHTS_MAXIMUM_ALLOWED ,
& conn . pol ) ;
if ( NT_STATUS_IS_OK ( result ) ) {
/* This particular query is exactly what Win2k clients use
to determine that the DC is active directory */
result = cli_lsa_query_info_policy2 ( conn . cli , mem_ctx ,
& conn . pol ,
12 , & domain_name ,
& dns_name , NULL ,
NULL , & dom_sid ) ;
}
if ( NT_STATUS_IS_OK ( result ) ) {
if ( domain_name )
fstrcpy ( domain - > name , domain_name ) ;
if ( dns_name )
fstrcpy ( domain - > alt_name , dns_name ) ;
2003-06-21 08:05:01 +04:00
2004-01-08 11:19:18 +03:00
if ( dom_sid )
sid_copy ( & domain - > sid , dom_sid ) ;
domain - > active_directory = True ;
} else {
result = cli_lsa_open_policy ( conn . cli , mem_ctx , True ,
SEC_RIGHTS_MAXIMUM_ALLOWED ,
& conn . pol ) ;
if ( ! NT_STATUS_IS_OK ( result ) )
goto done ;
result = cli_lsa_query_info_policy ( conn . cli , mem_ctx ,
& conn . pol , 5 , & domain_name ,
& dom_sid ) ;
if ( NT_STATUS_IS_OK ( result ) ) {
if ( domain_name )
fstrcpy ( domain - > name , domain_name ) ;
if ( dom_sid )
sid_copy ( & domain - > sid , dom_sid ) ;
}
}
}
done :
2003-07-31 09:43:47 +04:00
/* close the connection; no other cals use this pipe and it is called only
on reestablishing the domain list - - jerry */
2004-01-08 11:19:18 +03:00
2002-10-05 01:42:04 +04:00
if ( conn . cli )
cli_shutdown ( conn . cli ) ;
2004-01-08 11:19:18 +03:00
talloc_destroy ( mem_ctx ) ;
return ;
2002-10-05 01:42:04 +04:00
}
2002-02-15 16:28:59 +03:00
/* Return a LSA policy handle on a domain */
2001-10-05 04:20:06 +04:00
2004-01-05 07:10:28 +03:00
NTSTATUS cm_get_lsa_handle ( struct winbindd_domain * domain , CLI_POLICY_HND * * return_hnd )
2002-02-15 16:28:59 +03:00
{
struct winbindd_cm_conn * conn ;
uint32 des_access = SEC_RIGHTS_MAXIMUM_ALLOWED ;
NTSTATUS result ;
static CLI_POLICY_HND hnd ;
2001-10-05 04:20:06 +04:00
2002-02-15 16:28:59 +03:00
/* Look for existing connections */
2001-10-08 04:34:14 +04:00
2003-03-18 02:06:12 +03:00
if ( ! NT_STATUS_IS_OK ( result = get_connection_from_cache ( domain , PIPE_LSARPC , & conn ) ) )
2003-06-21 08:05:01 +04:00
return result ;
2002-04-04 06:39:57 +04:00
/* This *shitty* code needs scrapping ! JRA */
2003-06-21 08:05:01 +04:00
2002-04-04 06:39:57 +04:00
if ( policy_handle_is_valid ( & conn - > pol ) ) {
hnd . pol = conn - > pol ;
hnd . cli = conn - > cli ;
2003-06-21 08:05:01 +04:00
* return_hnd = & hnd ;
return NT_STATUS_OK ;
2002-04-04 06:39:57 +04:00
}
2002-02-15 16:28:59 +03:00
2001-11-28 02:48:44 +03:00
result = cli_lsa_open_policy ( conn - > cli , conn - > cli - > mem_ctx , False ,
des_access , & conn - > pol ) ;
2001-10-05 04:20:06 +04:00
2002-02-15 16:28:59 +03:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
/* Hit the cache code again. This cleans out the old connection and gets a new one */
if ( conn - > cli - > fd = = - 1 ) { /* Try again, if the remote host disapeared */
2003-03-18 02:06:12 +03:00
if ( ! NT_STATUS_IS_OK ( result = get_connection_from_cache ( domain , PIPE_LSARPC , & conn ) ) )
2003-06-21 08:05:01 +04:00
return result ;
2001-10-05 04:20:06 +04:00
2002-02-15 16:28:59 +03:00
result = cli_lsa_open_policy ( conn - > cli , conn - > cli - > mem_ctx , False ,
des_access , & conn - > pol ) ;
}
2001-10-05 04:20:06 +04:00
2002-02-15 16:28:59 +03:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
cli_shutdown ( conn - > cli ) ;
DLIST_REMOVE ( cm_conns , conn ) ;
SAFE_FREE ( conn ) ;
2003-06-21 08:05:01 +04:00
return result ;
2002-02-15 16:28:59 +03:00
}
}
2001-10-05 04:20:06 +04:00
2001-11-15 22:40:00 +03:00
hnd . pol = conn - > pol ;
hnd . cli = conn - > cli ;
2001-10-05 04:20:06 +04:00
2003-06-21 08:05:01 +04:00
* return_hnd = & hnd ;
return NT_STATUS_OK ;
2001-10-05 04:20:06 +04:00
}
/* Return a SAM policy handle on a domain */
2004-01-05 07:10:28 +03:00
NTSTATUS cm_get_sam_handle ( struct winbindd_domain * domain , CLI_POLICY_HND * * return_hnd )
2001-10-05 04:20:06 +04:00
{
2001-11-15 22:40:00 +03:00
struct winbindd_cm_conn * conn ;
uint32 des_access = SEC_RIGHTS_MAXIMUM_ALLOWED ;
NTSTATUS result ;
static CLI_POLICY_HND hnd ;
2001-10-08 04:34:14 +04:00
2001-11-15 22:40:00 +03:00
/* Look for existing connections */
2001-10-08 04:34:14 +04:00
2003-03-18 02:06:12 +03:00
if ( ! NT_STATUS_IS_OK ( result = get_connection_from_cache ( domain , PIPE_SAMR , & conn ) ) )
2003-06-21 08:05:01 +04:00
return result ;
2002-02-15 16:28:59 +03:00
2002-04-04 06:39:57 +04:00
/* This *shitty* code needs scrapping ! JRA */
2003-06-21 08:05:01 +04:00
2002-04-04 06:39:57 +04:00
if ( policy_handle_is_valid ( & conn - > pol ) ) {
hnd . pol = conn - > pol ;
hnd . cli = conn - > cli ;
2003-06-21 08:05:01 +04:00
* return_hnd = & hnd ;
return NT_STATUS_OK ;
2002-04-04 06:39:57 +04:00
}
2003-06-21 08:05:01 +04:00
2001-11-28 02:48:44 +03:00
result = cli_samr_connect ( conn - > cli , conn - > cli - > mem_ctx ,
des_access , & conn - > pol ) ;
2001-10-08 04:34:14 +04:00
2002-02-15 16:28:59 +03:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
/* Hit the cache code again. This cleans out the old connection and gets a new one */
if ( conn - > cli - > fd = = - 1 ) { /* Try again, if the remote host disapeared */
2003-06-21 08:05:01 +04:00
2003-03-18 02:06:12 +03:00
if ( ! NT_STATUS_IS_OK ( result = get_connection_from_cache ( domain , PIPE_SAMR , & conn ) ) )
2003-06-21 08:05:01 +04:00
return result ;
2001-10-08 04:34:14 +04:00
2002-02-15 16:28:59 +03:00
result = cli_samr_connect ( conn - > cli , conn - > cli - > mem_ctx ,
des_access , & conn - > pol ) ;
}
2001-10-08 04:34:14 +04:00
2002-02-15 16:28:59 +03:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
2003-06-21 08:05:01 +04:00
2002-02-15 16:28:59 +03:00
cli_shutdown ( conn - > cli ) ;
DLIST_REMOVE ( cm_conns , conn ) ;
SAFE_FREE ( conn ) ;
2003-06-21 08:05:01 +04:00
return result ;
2002-02-15 16:28:59 +03:00
}
}
2001-10-08 04:34:14 +04:00
2001-11-15 22:40:00 +03:00
hnd . pol = conn - > pol ;
hnd . cli = conn - > cli ;
2001-10-08 04:34:14 +04:00
2003-06-21 08:05:01 +04:00
* return_hnd = & hnd ;
return NT_STATUS_OK ;
2001-10-05 04:20:06 +04:00
}
2001-11-15 09:55:56 +03:00
/* Get a handle on a netlogon pipe. This is a bit of a hack to re-use the
netlogon pipe as no handle is returned . */
2001-11-05 03:21:17 +03:00
2004-01-05 07:10:28 +03:00
NTSTATUS cm_get_netlogon_cli ( struct winbindd_domain * domain ,
2003-04-21 18:09:03 +04:00
const unsigned char * trust_passwd ,
uint32 sec_channel_type ,
2003-05-08 12:02:52 +04:00
BOOL fresh ,
2001-11-28 02:48:44 +03:00
struct cli_state * * cli )
2001-11-05 03:21:17 +03:00
{
2002-02-11 04:29:07 +03:00
NTSTATUS result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND ;
2002-02-15 16:28:59 +03:00
struct winbindd_cm_conn * conn ;
2003-03-18 02:06:12 +03:00
fstring lock_name ;
BOOL got_mutex ;
2001-11-05 03:21:17 +03:00
2003-01-16 03:27:27 +03:00
if ( ! cli )
2002-02-15 16:28:59 +03:00
return NT_STATUS_INVALID_PARAMETER ;
2003-01-16 03:27:27 +03:00
2003-01-16 23:08:26 +03:00
/* Open an initial conection - keep the mutex. */
2003-01-16 03:27:27 +03:00
2003-05-08 12:02:52 +04:00
find_cm_connection ( domain , PIPE_NETLOGON , & conn ) ;
if ( fresh & & ( conn ! = NULL ) ) {
cli_shutdown ( conn - > cli ) ;
conn - > cli = NULL ;
conn = NULL ;
/* purge connection from cache */
find_cm_connection ( domain , PIPE_NETLOGON , & conn ) ;
if ( conn ! = NULL ) {
DEBUG ( 0 , ( " Could not purge connection \n " ) ) ;
return NT_STATUS_UNSUCCESSFUL ;
}
}
if ( conn ! = NULL ) {
* cli = conn - > cli ;
return NT_STATUS_OK ;
}
result = new_cm_connection ( domain , PIPE_NETLOGON , & conn ) ;
if ( ! NT_STATUS_IS_OK ( result ) )
2002-02-15 16:28:59 +03:00
return result ;
2002-02-11 04:29:07 +03:00
2003-07-23 16:33:59 +04:00
fstr_sprintf ( lock_name , " NETLOGON \\ %s " , conn - > controller ) ;
2003-03-18 02:06:12 +03:00
if ( ! ( got_mutex = secrets_named_mutex ( lock_name , WINBIND_SERVER_MUTEX_WAIT_TIME ) ) ) {
DEBUG ( 0 , ( " cm_get_netlogon_cli: mutex grab failed for %s \n " , conn - > controller ) ) ;
}
Here's the code to make winbindd work on a Samba DC
to handle domain trusts. Jeremy and I talked about this
and it's going in as working code. It keeps winbind clean
and solves the trust problem with minimal changes.
To summarize, there are 2 basic cases where the deadlock would
occur. (1) lookuping up secondary groups for a user, and
(2) get[gr|pw]nam() calls that fall through the NSS layer because
they don't exist anywhere.
o To handle case #1, we bypass winbindd in sys_getgrouplist() unless
the username includes the 'winbind separator'.
o Case #2 is handled by adding checks in winbindd to return failure
if we are a DC and the domain matches our own.
This code has been tested using basic share connections, domain
logons, and with pam_winbind (both with and without 'winbind
use default domain'). The 'trustdomain' auth module should work
as well if an admin wants to manually create UNIX users for
acounts in the trusted domains.
Other misc fixes:
* we need to fix check_ntlm_password() to be able to determine
if an auth module is authoritative over a user (NT_STATUS_WRONG_PASSWORD,
etc...). I worked around my specific situation, but this needs to be
fixed. the winbindd auth module was causing delays.
* fix named server mutex deadlock between trust domain auth module
and winbindd looking up a uid
* make sure SAM_ACCOUNT gets stored in the server_info struct for the
_net_sam_logon() reply.
Configuration details:
The recommended method for supporting trusts is to use winbind.
The gets us around some of the server mutex issues as well.
* set 'files winbind' for passwd: and group: in /etc/nsswitch.conf
* create domain trusts like normal
* join winbind on the pdc to the Samba domain using 'net rpc join'
* add normal parameters to smb.conf for winbind
* set 'auth method = guest sam winbind'
* start smbd, nmbd, & winbindd
Problems that remain:
* join a Windows 2k/XP box to a Samba domain.
* create a 2-way trust between the Samba domain
and an NT domain
* logon to the windows client as a user from theh trusted
domain
* try to browse server in the trusted domain (or other
workstations). an NT client seems to work ok, but 2k
and XP either prompt for passwords or fail with errors.
apparanently this never got tested since no one has ever been
able to logon as a trusted user to a Samba domain from a Windows
client.
(This used to be commit f804b590f9dbf1f0147c06a0a2f12e221ae6fc3b)
2003-06-29 07:39:50 +04:00
if ( sec_channel_type = = SEC_CHAN_DOMAIN )
2003-07-23 16:33:59 +04:00
fstr_sprintf ( conn - > cli - > mach_acct , " %s$ " , lp_workgroup ( ) ) ;
2003-03-18 02:06:12 +03:00
2004-01-05 07:10:28 +03:00
fstrcpy ( conn - > cli - > domain , domain - > name ) ;
2003-09-05 21:57:45 +04:00
2003-05-08 12:02:52 +04:00
result = cli_nt_establish_netlogon ( conn - > cli , sec_channel_type , trust_passwd ) ;
2003-04-02 10:04:51 +04:00
2003-03-18 02:06:12 +03:00
if ( got_mutex )
secrets_named_mutex_release ( lock_name ) ;
2003-05-08 12:02:52 +04:00
2001-11-15 22:40:00 +03:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
2003-05-08 12:02:52 +04:00
cli_shutdown ( conn - > cli ) ;
DLIST_REMOVE ( cm_conns , conn ) ;
SAFE_FREE ( conn ) ;
return result ;
2001-11-15 22:40:00 +03:00
}
2001-11-05 03:21:17 +03:00
2002-02-15 16:28:59 +03:00
* cli = conn - > cli ;
2001-11-05 03:21:17 +03:00
2001-11-28 02:48:44 +03:00
return result ;
2001-11-05 03:21:17 +03:00
}
2001-11-14 09:18:13 +03:00
/* Dump the current connection status */
static void dump_conn_list ( void )
{
2001-11-15 22:40:00 +03:00
struct winbindd_cm_conn * con ;
2001-11-14 09:18:13 +03:00
2001-11-28 02:48:44 +03:00
DEBUG ( 0 , ( " \t Domain Controller Pipe \n " ) ) ;
2001-11-14 09:18:13 +03:00
2001-11-15 22:40:00 +03:00
for ( con = cm_conns ; con ; con = con - > next ) {
char * msg ;
2001-11-14 09:18:13 +03:00
2001-11-15 22:40:00 +03:00
/* Display pipe info */
2001-11-28 02:48:44 +03:00
2002-01-19 20:29:32 +03:00
if ( asprintf ( & msg , " \t %-15s %-15s %-16s " , con - > domain , con - > controller , con - > pipe_name ) < 0 ) {
DEBUG ( 0 , ( " Error: not enough memory! \n " ) ) ;
} else {
DEBUG ( 0 , ( " %s \n " , msg ) ) ;
SAFE_FREE ( msg ) ;
}
2001-11-15 22:40:00 +03:00
}
2001-11-14 09:18:13 +03:00
}
void winbindd_cm_status ( void )
{
2001-11-15 22:40:00 +03:00
/* List open connections */
2001-11-14 09:18:13 +03:00
2001-11-15 22:40:00 +03:00
DEBUG ( 0 , ( " winbindd connection manager status: \n " ) ) ;
2001-11-14 09:18:13 +03:00
2001-11-15 22:40:00 +03:00
if ( cm_conns )
dump_conn_list ( ) ;
else
DEBUG ( 0 , ( " \t No active connections \n " ) ) ;
2001-11-14 09:18:13 +03:00
}
2003-06-11 02:11:30 +04:00
/* Close all cached connections */
void winbindd_cm_flush ( void )
{
struct winbindd_cm_conn * conn , tmp ;
/* Flush connection cache */
for ( conn = cm_conns ; conn ; conn = conn - > next ) {
if ( ! connection_ok ( conn ) )
continue ;
DEBUG ( 10 , ( " Closing connection to %s on %s \n " ,
conn - > pipe_name , conn - > controller ) ) ;
if ( conn - > cli )
cli_shutdown ( conn - > cli ) ;
tmp . next = conn - > next ;
DLIST_REMOVE ( cm_conns , conn ) ;
SAFE_FREE ( conn ) ;
conn = & tmp ;
}
/* Flush failed connection cache */
flush_negative_conn_cache ( ) ;
}