1998-11-12 10:06:48 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1998-11-12 10:06:48 +03:00
SMB client password change routine
Copyright ( C ) Andrew Tridgell 1994 - 1998
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
1998-11-12 10:06:48 +03: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/>.
1998-11-12 10:06:48 +03:00
*/
# include "includes.h"
/*************************************************************
2005-09-30 21:13:37 +04:00
Change a password on a remote machine using IPC calls .
1998-11-12 10:06:48 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-09-30 21:13:37 +04:00
2006-02-04 01:19:41 +03:00
NTSTATUS remote_password_change ( const char * remote_machine , const char * user_name ,
2007-11-23 14:04:35 +03:00
const char * old_passwd , const char * new_passwd ,
char * * err_str )
1998-11-12 10:06:48 +03:00
{
struct nmb_name calling , called ;
2009-11-13 00:56:33 +03:00
struct cli_state * cli = NULL ;
struct rpc_pipe_client * pipe_hnd = NULL ;
2007-10-25 01:16:54 +04:00
struct sockaddr_storage ss ;
2009-08-30 03:31:02 +04:00
char * user , * domain , * p ;
2004-01-26 11:45:02 +03:00
NTSTATUS result ;
2007-10-19 04:40:25 +04:00
bool pass_must_change = False ;
1998-11-12 10:06:48 +03:00
2009-08-30 03:31:02 +04:00
user = talloc_strdup ( talloc_tos ( ) , user_name ) ;
SMB_ASSERT ( user ! = NULL ) ;
domain = talloc_strdup ( talloc_tos ( ) , " " ) ;
SMB_ASSERT ( domain ! = NULL ) ;
/* allow usernames of the form domain\\user or domain/user */
if ( ( p = strchr_m ( user , ' \\ ' ) ) | | ( p = strchr_m ( user , ' / ' ) ) | |
( p = strchr_m ( user , * lp_winbind_separator ( ) ) ) ) {
* p = 0 ;
domain = user ;
user = p + 1 ;
}
2007-11-23 14:04:35 +03:00
* err_str = NULL ;
1999-12-13 16:27:58 +03:00
2009-07-28 22:51:58 +04:00
if ( ! resolve_name ( remote_machine , & ss , 0x20 , false ) ) {
2009-01-01 03:30:11 +03:00
if ( asprintf ( err_str , " Unable to find an IP address for machine "
" %s. \n " , remote_machine ) = = - 1 ) {
* err_str = NULL ;
}
2006-02-04 01:19:41 +03:00
return NT_STATUS_UNSUCCESSFUL ;
1998-11-12 10:06:48 +03:00
}
2008-09-01 23:28:57 +04:00
2006-07-11 22:01:26 +04:00
cli = cli_initialise ( ) ;
if ( ! cli ) {
return NT_STATUS_NO_MEMORY ;
}
2007-10-25 01:16:54 +04:00
result = cli_connect ( cli , remote_machine , & ss ) ;
2007-06-20 21:38:42 +04:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
2009-01-01 03:30:11 +03:00
if ( asprintf ( err_str , " Unable to connect to SMB server on "
2007-11-23 14:04:35 +03:00
" machine %s. Error was : %s. \n " ,
2009-01-01 03:30:11 +03:00
remote_machine , nt_errstr ( result ) ) = = - 1 ) {
* err_str = NULL ;
}
2006-07-11 22:01:26 +04:00
cli_shutdown ( cli ) ;
return result ;
1998-11-12 10:06:48 +03:00
}
2008-09-01 23:28:57 +04:00
2002-11-13 02:20:50 +03:00
make_nmb_name ( & calling , global_myname ( ) , 0x0 ) ;
2000-01-07 09:55:36 +03:00
make_nmb_name ( & called , remote_machine , 0x20 ) ;
2008-09-01 23:28:57 +04:00
2006-07-11 22:01:26 +04:00
if ( ! cli_session_request ( cli , & calling , & called ) ) {
2009-01-01 03:30:11 +03:00
if ( asprintf ( err_str , " machine %s rejected the session setup. "
2007-11-23 14:04:35 +03:00
" Error was : %s. \n " ,
2009-01-01 03:30:11 +03:00
remote_machine , cli_errstr ( cli ) ) = = - 1 ) {
* err_str = NULL ;
}
2006-07-11 22:01:26 +04:00
result = cli_nt_error ( cli ) ;
cli_shutdown ( cli ) ;
return result ;
1998-11-12 10:06:48 +03:00
}
2008-09-01 23:28:57 +04:00
2006-07-11 22:01:26 +04:00
cli - > protocol = PROTOCOL_NT1 ;
1998-11-12 10:06:48 +03:00
2008-09-11 20:57:49 +04:00
result = cli_negprot ( cli ) ;
if ( ! NT_STATUS_IS_OK ( result ) ) {
2009-01-01 03:30:11 +03:00
if ( asprintf ( err_str , " machine %s rejected the negotiate "
2007-11-23 14:04:35 +03:00
" protocol. Error was : %s. \n " ,
2009-01-01 03:30:11 +03:00
remote_machine , nt_errstr ( result ) ) = = - 1 ) {
* err_str = NULL ;
}
2006-07-11 22:01:26 +04:00
result = cli_nt_error ( cli ) ;
cli_shutdown ( cli ) ;
2006-02-04 01:19:41 +03:00
return result ;
1998-11-12 10:06:48 +03:00
}
2008-09-01 23:28:57 +04:00
2004-01-26 11:45:02 +03:00
/* Given things like SMB signing, restrict anonymous and the like,
try an authenticated connection first */
2006-08-16 21:14:16 +04:00
result = cli_session_setup ( cli , user_name ,
old_passwd , strlen ( old_passwd ) + 1 ,
old_passwd , strlen ( old_passwd ) + 1 , " " ) ;
2006-01-29 01:49:25 +03:00
2006-08-16 21:14:16 +04:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
2006-01-29 01:49:25 +03:00
2006-11-06 22:21:44 +03:00
/* Password must change or Password expired are the only valid
* error conditions here from where we can proceed , the rest like
* account locked out or logon failure will lead to errors later
* anyway */
2006-01-29 01:49:25 +03:00
2006-11-06 22:21:44 +03:00
if ( ! NT_STATUS_EQUAL ( result , NT_STATUS_PASSWORD_MUST_CHANGE ) & &
! NT_STATUS_EQUAL ( result , NT_STATUS_PASSWORD_EXPIRED ) ) {
2009-01-01 03:30:11 +03:00
if ( asprintf ( err_str , " Could not connect to machine %s: "
" %s \n " , remote_machine , cli_errstr ( cli ) ) = = - 1 ) {
* err_str = NULL ;
}
2006-08-16 21:14:16 +04:00
cli_shutdown ( cli ) ;
return result ;
2006-01-29 01:49:25 +03:00
}
2006-08-16 21:14:16 +04:00
pass_must_change = True ;
2004-01-26 11:45:02 +03:00
/*
* We should connect as the anonymous user here , in case
* the server has " must change password " checked . . .
* Thanks to < Nicholas . S . Jenkins @ cdc . com > for this fix .
*/
1998-11-12 10:06:48 +03:00
2006-08-16 21:14:16 +04:00
result = cli_session_setup ( cli , " " , " " , 0 , " " , 0 , " " ) ;
if ( ! NT_STATUS_IS_OK ( result ) ) {
2009-01-01 03:30:11 +03:00
if ( asprintf ( err_str , " machine %s rejected the session "
2007-11-23 14:04:35 +03:00
" setup. Error was : %s. \n " ,
2009-01-01 03:30:11 +03:00
remote_machine , cli_errstr ( cli ) ) = = - 1 ) {
* err_str = NULL ;
}
2006-07-11 22:01:26 +04:00
cli_shutdown ( cli ) ;
2006-02-04 01:19:41 +03:00
return result ;
2004-01-26 11:45:02 +03:00
}
2009-03-14 03:49:24 +03:00
result = cli_init_creds ( cli , " " , " " , NULL ) ;
if ( ! NT_STATUS_IS_OK ( result ) ) {
cli_shutdown ( cli ) ;
return result ;
}
2004-01-26 11:45:02 +03:00
} else {
2009-08-30 03:31:02 +04:00
result = cli_init_creds ( cli , user , domain , old_passwd ) ;
2009-03-14 03:49:24 +03:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
cli_shutdown ( cli ) ;
return result ;
}
2004-01-26 11:45:02 +03:00
}
1998-11-12 10:06:48 +03:00
2009-01-26 10:37:13 +03:00
result = cli_tcon_andx ( cli , " IPC$ " , " IPC " , " " , 1 ) ;
if ( ! NT_STATUS_IS_OK ( result ) ) {
if ( asprintf ( err_str , " machine %s rejected the tconX on the "
" IPC$ share. Error was : %s. \n " ,
remote_machine , nt_errstr ( result ) ) ) {
2009-01-01 03:30:11 +03:00
* err_str = NULL ;
}
2006-07-11 22:01:26 +04:00
cli_shutdown ( cli ) ;
2006-02-04 01:19:41 +03:00
return result ;
1998-11-12 10:06:48 +03:00
}
2005-09-30 21:13:37 +04:00
/* Try not to give the password away too easily */
2004-01-26 11:45:02 +03:00
2006-01-29 01:49:25 +03:00
if ( ! pass_must_change ) {
2008-07-20 13:04:31 +04:00
result = cli_rpc_pipe_open_ntlmssp ( cli ,
& ndr_table_samr . syntax_id ,
2009-09-11 00:23:21 +04:00
NCACN_NP ,
2009-09-14 22:39:54 +04:00
DCERPC_AUTH_LEVEL_PRIVACY ,
2009-08-30 03:31:02 +04:00
domain , user ,
2008-07-20 13:04:31 +04:00
old_passwd ,
& pipe_hnd ) ;
2006-01-29 01:49:25 +03:00
} else {
/*
* If the user password must be changed the ntlmssp bind will
* fail the same way as the session setup above did . The
* difference ist that with a pipe bind we don ' t get a good
* error message , the result will be that the rpc call below
* will just fail . So we do it anonymously , there ' s no other
* way .
*/
2008-07-20 13:04:31 +04:00
result = cli_rpc_pipe_open_noauth (
cli , & ndr_table_samr . syntax_id , & pipe_hnd ) ;
2006-01-29 01:49:25 +03:00
}
2005-09-30 21:13:37 +04:00
2008-07-20 13:04:31 +04:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
2004-01-26 11:45:02 +03:00
if ( lp_client_lanman_auth ( ) ) {
2005-09-30 21:13:37 +04:00
/* Use the old RAP method. */
2006-07-11 22:01:26 +04:00
if ( ! cli_oem_change_password ( cli , user_name , new_passwd , old_passwd ) ) {
2009-01-01 03:30:11 +03:00
if ( asprintf ( err_str , " machine %s rejected the "
2007-11-23 14:04:35 +03:00
" password change: Error was : %s. \n " ,
2009-01-01 03:30:11 +03:00
remote_machine , cli_errstr ( cli ) ) = = - 1 ) {
* err_str = NULL ;
}
2006-07-11 22:01:26 +04:00
result = cli_nt_error ( cli ) ;
cli_shutdown ( cli ) ;
2006-02-04 01:19:41 +03:00
return result ;
2004-01-26 11:45:02 +03:00
}
} else {
2009-01-01 03:30:11 +03:00
if ( asprintf ( err_str , " SAMR connection to machine %s "
2007-11-23 14:04:35 +03:00
" failed. Error was %s, but LANMAN password "
2009-06-17 12:23:21 +04:00
" changes are disabled \n " ,
2009-06-15 10:33:22 +04:00
remote_machine , nt_errstr ( result ) ) = = - 1 ) {
2009-01-01 03:30:11 +03:00
* err_str = NULL ;
}
2006-07-11 22:01:26 +04:00
result = cli_nt_error ( cli ) ;
cli_shutdown ( cli ) ;
2006-02-04 01:19:41 +03:00
return result ;
2004-01-26 11:45:02 +03:00
}
}
2008-06-25 12:35:59 +04:00
result = rpccli_samr_chgpasswd_user2 ( pipe_hnd , talloc_tos ( ) ,
user_name , new_passwd , old_passwd ) ;
2008-04-19 20:17:13 +04:00
if ( NT_STATUS_IS_OK ( result ) ) {
2004-04-12 15:18:32 +04:00
/* Great - it all worked! */
2006-07-11 22:01:26 +04:00
cli_shutdown ( cli ) ;
2006-02-04 01:19:41 +03:00
return NT_STATUS_OK ;
2004-04-12 15:18:32 +04:00
} else if ( ! ( NT_STATUS_EQUAL ( result , NT_STATUS_ACCESS_DENIED )
| | NT_STATUS_EQUAL ( result , NT_STATUS_UNSUCCESSFUL ) ) ) {
/* it failed, but for reasons such as wrong password, too short etc ... */
2008-09-01 23:28:57 +04:00
2009-01-01 03:30:11 +03:00
if ( asprintf ( err_str , " machine %s rejected the password change: "
2007-11-23 14:04:35 +03:00
" Error was : %s. \n " ,
2009-01-01 03:30:11 +03:00
remote_machine , get_friendly_nt_error_msg ( result ) ) = = - 1 ) {
* err_str = NULL ;
}
2006-07-11 22:01:26 +04:00
cli_shutdown ( cli ) ;
2006-02-04 01:19:41 +03:00
return result ;
2004-04-12 15:18:32 +04:00
}
/* OK, that failed, so try again... */
2008-04-20 15:51:46 +04:00
TALLOC_FREE ( pipe_hnd ) ;
2008-09-01 23:28:57 +04:00
2004-04-12 15:18:32 +04:00
/* Try anonymous NTLMSSP... */
2009-03-14 03:49:24 +03:00
result = cli_init_creds ( cli , " " , " " , NULL ) ;
if ( ! NT_STATUS_IS_OK ( result ) ) {
cli_shutdown ( cli ) ;
return result ;
}
2008-09-01 23:28:57 +04:00
2004-04-12 15:18:32 +04:00
result = NT_STATUS_UNSUCCESSFUL ;
2008-09-01 23:28:57 +04:00
2005-09-30 21:13:37 +04:00
/* OK, this is ugly, but... try an anonymous pipe. */
2008-07-20 13:04:31 +04:00
result = cli_rpc_pipe_open_noauth ( cli , & ndr_table_samr . syntax_id ,
& pipe_hnd ) ;
2005-09-30 21:13:37 +04:00
2008-07-20 13:04:31 +04:00
if ( NT_STATUS_IS_OK ( result ) & &
2008-06-25 12:35:59 +04:00
( NT_STATUS_IS_OK ( result = rpccli_samr_chgpasswd_user2 (
2008-04-19 20:17:13 +04:00
pipe_hnd , talloc_tos ( ) , user_name ,
new_passwd , old_passwd ) ) ) ) {
2004-04-12 15:18:32 +04:00
/* Great - it all worked! */
2006-07-11 22:01:26 +04:00
cli_shutdown ( cli ) ;
2006-02-04 01:19:41 +03:00
return NT_STATUS_OK ;
2004-04-12 15:18:32 +04:00
} else {
if ( ! ( NT_STATUS_EQUAL ( result , NT_STATUS_ACCESS_DENIED )
| | NT_STATUS_EQUAL ( result , NT_STATUS_UNSUCCESSFUL ) ) ) {
/* it failed, but again it was due to things like new password too short */
2009-01-01 03:30:11 +03:00
if ( asprintf ( err_str , " machine %s rejected the "
2007-11-23 14:04:35 +03:00
" (anonymous) password change: Error was : "
" %s. \n " , remote_machine ,
2009-01-01 03:30:11 +03:00
get_friendly_nt_error_msg ( result ) ) = = - 1 ) {
* err_str = NULL ;
}
2006-07-11 22:01:26 +04:00
cli_shutdown ( cli ) ;
2006-02-04 01:19:41 +03:00
return result ;
2004-04-12 15:18:32 +04:00
}
2008-09-01 23:28:57 +04:00
2004-04-12 15:18:32 +04:00
/* We have failed to change the user's password, and we think the server
just might not support SAMR password changes , so fall back */
2008-09-01 23:28:57 +04:00
2004-04-12 15:18:32 +04:00
if ( lp_client_lanman_auth ( ) ) {
2005-09-30 21:13:37 +04:00
/* Use the old RAP method. */
2006-07-11 22:01:26 +04:00
if ( cli_oem_change_password ( cli , user_name , new_passwd , old_passwd ) ) {
2004-04-12 15:18:32 +04:00
/* SAMR failed, but the old LanMan protocol worked! */
2006-07-11 22:01:26 +04:00
cli_shutdown ( cli ) ;
2006-02-04 01:19:41 +03:00
return NT_STATUS_OK ;
2004-01-26 11:45:02 +03:00
}
2009-01-01 03:30:11 +03:00
if ( asprintf ( err_str , " machine %s rejected the password "
2007-11-23 14:04:35 +03:00
" change: Error was : %s. \n " ,
2009-01-01 03:30:11 +03:00
remote_machine , cli_errstr ( cli ) ) = = - 1 ) {
* err_str = NULL ;
}
2006-07-11 22:01:26 +04:00
result = cli_nt_error ( cli ) ;
cli_shutdown ( cli ) ;
2006-02-04 01:19:41 +03:00
return result ;
2004-01-26 11:45:02 +03:00
} else {
2009-01-01 03:30:11 +03:00
if ( asprintf ( err_str , " SAMR connection to machine %s "
2007-11-23 14:04:35 +03:00
" failed. Error was %s, but LANMAN password "
" changed are disabled \n " ,
2009-01-01 03:30:11 +03:00
nt_errstr ( result ) , remote_machine ) = = - 1 ) {
* err_str = NULL ;
}
2006-07-11 22:01:26 +04:00
cli_shutdown ( cli ) ;
2006-02-04 01:19:41 +03:00
return NT_STATUS_UNSUCCESSFUL ;
2004-01-26 11:45:02 +03:00
}
}
1998-11-12 10:06:48 +03:00
}