1998-11-12 07:06:48 +00:00
/*
2002-01-30 06:08:46 +00:00
Unix SMB / CIFS implementation .
1998-11-12 07:06:48 +00:00
SMB client password change routine
Copyright ( C ) Andrew Tridgell 1994 - 1998
2011-05-28 20:24:01 +02:00
1998-11-12 07:06:48 +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
1998-11-12 07:06:48 +00:00
( at your option ) any later version .
2011-05-28 20:24:01 +02:00
1998-11-12 07:06:48 +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-05-28 20:24:01 +02:00
1998-11-12 07:06:48 +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/>.
1998-11-12 07:06:48 +00:00
*/
# include "includes.h"
2010-05-28 02:18:21 +02:00
# include "../librpc/gen_ndr/ndr_samr.h"
2011-02-28 10:19:44 +01:00
# include "rpc_client/cli_pipe.h"
2010-08-02 22:52:00 +02:00
# include "rpc_client/cli_samr.h"
2011-05-06 11:47:43 +02:00
# include "libsmb/libsmb.h"
2011-02-24 10:46:55 +01:00
# include "libsmb/clirap.h"
2011-03-23 14:18:59 +01:00
# include "libsmb/nmblib.h"
2012-05-20 17:54:29 +02:00
# include "../libcli/smb/smbXcli_base.h"
1998-11-12 07:06:48 +00:00
/*************************************************************
2005-09-30 17:13:37 +00:00
Change a password on a remote machine using IPC calls .
1998-11-12 07:06:48 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-09-30 17:13:37 +00:00
2017-08-18 16:08:46 +02:00
NTSTATUS remote_password_change ( const char * remote_machine ,
const char * domain , const char * user_name ,
2007-11-23 12:04:35 +01:00
const char * old_passwd , const char * new_passwd ,
char * * err_str )
1998-11-12 07:06:48 +00:00
{
2009-11-12 13:56:33 -08:00
struct cli_state * cli = NULL ;
2016-10-28 13:48:23 +02:00
struct cli_credentials * creds = NULL ;
2009-11-12 13:56:33 -08:00
struct rpc_pipe_client * pipe_hnd = NULL ;
2004-01-26 08:45:02 +00:00
NTSTATUS result ;
2007-10-18 17:40:25 -07:00
bool pass_must_change = False ;
1998-11-12 07:06:48 +00:00
2007-11-23 12:04:35 +01:00
* err_str = NULL ;
1999-12-13 13:27:58 +00:00
2011-05-28 20:45:11 +02:00
result = cli_connect_nb ( remote_machine , NULL , 0 , 0x20 , NULL ,
2015-12-16 10:04:35 +01:00
SMB_SIGNING_IPC_DEFAULT , 0 , & cli ) ;
2007-06-20 17:38:42 +00:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
2018-12-17 14:57:59 -05:00
if ( NT_STATUS_EQUAL ( result , NT_STATUS_NOT_SUPPORTED ) ) {
if ( asprintf ( err_str , " Unable to connect to SMB server on "
" machine %s. NetBIOS support disabled \n " ,
remote_machine ) = = - 1 ) {
* err_str = NULL ;
}
} else {
if ( asprintf ( err_str , " Unable to connect to SMB server on "
" machine %s. Error was : %s. \n " ,
remote_machine , nt_errstr ( result ) ) = = - 1 ) {
* err_str = NULL ;
}
2008-12-31 16:30:11 -08:00
}
2006-07-11 18:01:26 +00:00
return result ;
1998-11-12 07:06:48 +00:00
}
2008-09-01 21:28:57 +02:00
2016-10-28 13:48:23 +02:00
creds = cli_session_creds_init ( cli ,
user_name ,
2017-08-18 16:08:46 +02:00
domain ,
2016-10-28 13:48:23 +02:00
NULL , /* realm */
old_passwd ,
2016-10-30 16:10:03 +01:00
false , /* use_kerberos */
false , /* fallback_after_kerberos */
false , /* use_ccache */
false ) ; /* password_is_nt_hash */
2016-10-28 13:48:23 +02:00
SMB_ASSERT ( creds ! = NULL ) ;
2015-12-16 10:04:35 +01:00
result = smbXcli_negprot ( cli - > conn , cli - > timeout ,
lp_client_ipc_min_protocol ( ) ,
lp_client_ipc_max_protocol ( ) ) ;
2008-09-11 18:57:49 +02:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
2008-12-31 16:30:11 -08:00
if ( asprintf ( err_str , " machine %s rejected the negotiate "
2007-11-23 12:04:35 +01:00
" protocol. Error was : %s. \n " ,
2008-12-31 16:30:11 -08:00
remote_machine , nt_errstr ( result ) ) = = - 1 ) {
* err_str = NULL ;
}
2006-07-11 18:01:26 +00:00
cli_shutdown ( cli ) ;
2006-02-03 22:19:41 +00:00
return result ;
1998-11-12 07:06:48 +00:00
}
2008-09-01 21:28:57 +02:00
2004-01-26 08:45:02 +00:00
/* Given things like SMB signing, restrict anonymous and the like,
try an authenticated connection first */
2016-10-28 13:48:23 +02:00
result = cli_session_setup_creds ( cli , creds ) ;
2006-01-28 22:49:25 +00:00
2006-08-16 17:14:16 +00:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
2006-01-28 22:49:25 +00:00
2006-11-06 19:21:44 +00: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-28 22:49:25 +00:00
2006-11-06 19:21:44 +00:00
if ( ! NT_STATUS_EQUAL ( result , NT_STATUS_PASSWORD_MUST_CHANGE ) & &
! NT_STATUS_EQUAL ( result , NT_STATUS_PASSWORD_EXPIRED ) ) {
2008-12-31 16:30:11 -08:00
if ( asprintf ( err_str , " Could not connect to machine %s: "
2011-02-04 14:25:52 +01:00
" %s \n " , remote_machine , nt_errstr ( result ) ) = = - 1 ) {
2008-12-31 16:30:11 -08:00
* err_str = NULL ;
}
2006-08-16 17:14:16 +00:00
cli_shutdown ( cli ) ;
return result ;
2006-01-28 22:49:25 +00:00
}
2006-08-16 17:14:16 +00:00
pass_must_change = True ;
2004-01-26 08:45:02 +00: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 07:06:48 +00:00
2016-10-28 12:15:20 +02:00
result = cli_session_setup_anon ( cli ) ;
2006-08-16 17:14:16 +00:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
2008-12-31 16:30:11 -08:00
if ( asprintf ( err_str , " machine %s rejected the session "
2007-11-23 12:04:35 +01:00
" setup. Error was : %s. \n " ,
2011-02-04 14:25:52 +01:00
remote_machine , nt_errstr ( result ) ) = = - 1 ) {
2008-12-31 16:30:11 -08:00
* err_str = NULL ;
}
2006-07-11 18:01:26 +00:00
cli_shutdown ( cli ) ;
2006-02-03 22:19:41 +00:00
return result ;
2004-01-26 08:45:02 +00:00
}
}
1998-11-12 07:06:48 +00:00
2016-12-08 07:13:57 +01:00
result = cli_tree_connect ( cli , " IPC$ " , " IPC " , NULL ) ;
2009-01-26 08:37:13 +01:00
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 ) ) ) {
2008-12-31 16:30:11 -08:00
* err_str = NULL ;
}
2006-07-11 18:01:26 +00:00
cli_shutdown ( cli ) ;
2006-02-03 22:19:41 +00:00
return result ;
1998-11-12 07:06:48 +00:00
}
2005-09-30 17:13:37 +00:00
/* Try not to give the password away too easily */
2004-01-26 08:45:02 +00:00
2006-01-28 22:49:25 +00:00
if ( ! pass_must_change ) {
2016-10-28 13:48:23 +02:00
result = cli_rpc_pipe_open_with_creds ( cli ,
& ndr_table_samr ,
NCACN_NP ,
DCERPC_AUTH_TYPE_NTLMSSP ,
DCERPC_AUTH_LEVEL_PRIVACY ,
remote_machine ,
creds ,
& pipe_hnd ) ;
2006-01-28 22:49:25 +00: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 11:04:31 +02:00
result = cli_rpc_pipe_open_noauth (
2013-05-24 13:29:28 +02:00
cli , & ndr_table_samr , & pipe_hnd ) ;
2006-01-28 22:49:25 +00:00
}
2005-09-30 17:13:37 +00:00
2008-07-20 11:04:31 +02:00
if ( ! NT_STATUS_IS_OK ( result ) ) {
2004-01-26 08:45:02 +00:00
if ( lp_client_lanman_auth ( ) ) {
2005-09-30 17:13:37 +00:00
/* Use the old RAP method. */
2006-07-11 18:01:26 +00:00
if ( ! cli_oem_change_password ( cli , user_name , new_passwd , old_passwd ) ) {
2011-02-04 14:25:52 +01:00
result = cli_nt_error ( cli ) ;
2008-12-31 16:30:11 -08:00
if ( asprintf ( err_str , " machine %s rejected the "
2007-11-23 12:04:35 +01:00
" password change: Error was : %s. \n " ,
2011-02-04 14:25:52 +01:00
remote_machine , nt_errstr ( result ) ) = = - 1 ) {
2008-12-31 16:30:11 -08:00
* err_str = NULL ;
}
2006-07-11 18:01:26 +00:00
cli_shutdown ( cli ) ;
2006-02-03 22:19:41 +00:00
return result ;
2004-01-26 08:45:02 +00:00
}
} else {
2008-12-31 16:30:11 -08:00
if ( asprintf ( err_str , " SAMR connection to machine %s "
2007-11-23 12:04:35 +01:00
" failed. Error was %s, but LANMAN password "
2009-06-17 10:23:21 +02:00
" changes are disabled \n " ,
2009-06-15 08:33:22 +02:00
remote_machine , nt_errstr ( result ) ) = = - 1 ) {
2008-12-31 16:30:11 -08:00
* err_str = NULL ;
}
2006-07-11 18:01:26 +00:00
cli_shutdown ( cli ) ;
2006-02-03 22:19:41 +00:00
return result ;
2004-01-26 08:45:02 +00:00
}
}
2008-06-25 10:35:59 +02:00
result = rpccli_samr_chgpasswd_user2 ( pipe_hnd , talloc_tos ( ) ,
user_name , new_passwd , old_passwd ) ;
2008-04-19 18:17:13 +02:00
if ( NT_STATUS_IS_OK ( result ) ) {
2004-04-12 11:18:32 +00:00
/* Great - it all worked! */
2006-07-11 18:01:26 +00:00
cli_shutdown ( cli ) ;
2006-02-03 22:19:41 +00:00
return NT_STATUS_OK ;
2004-04-12 11:18:32 +00: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 21:28:57 +02:00
2008-12-31 16:30:11 -08:00
if ( asprintf ( err_str , " machine %s rejected the password change: "
2007-11-23 12:04:35 +01:00
" Error was : %s. \n " ,
2008-12-31 16:30:11 -08:00
remote_machine , get_friendly_nt_error_msg ( result ) ) = = - 1 ) {
* err_str = NULL ;
}
2006-07-11 18:01:26 +00:00
cli_shutdown ( cli ) ;
2006-02-03 22:19:41 +00:00
return result ;
2004-04-12 11:18:32 +00:00
}
/* OK, that failed, so try again... */
2008-04-20 13:51:46 +02:00
TALLOC_FREE ( pipe_hnd ) ;
2008-09-01 21:28:57 +02:00
2004-04-12 11:18:32 +00:00
/* Try anonymous NTLMSSP... */
result = NT_STATUS_UNSUCCESSFUL ;
2008-09-01 21:28:57 +02:00
2005-09-30 17:13:37 +00:00
/* OK, this is ugly, but... try an anonymous pipe. */
2013-05-24 13:29:28 +02:00
result = cli_rpc_pipe_open_noauth ( cli , & ndr_table_samr ,
2008-07-20 11:04:31 +02:00
& pipe_hnd ) ;
2005-09-30 17:13:37 +00:00
2008-07-20 11:04:31 +02:00
if ( NT_STATUS_IS_OK ( result ) & &
2008-06-25 10:35:59 +02:00
( NT_STATUS_IS_OK ( result = rpccli_samr_chgpasswd_user2 (
2008-04-19 18:17:13 +02:00
pipe_hnd , talloc_tos ( ) , user_name ,
new_passwd , old_passwd ) ) ) ) {
2004-04-12 11:18:32 +00:00
/* Great - it all worked! */
2006-07-11 18:01:26 +00:00
cli_shutdown ( cli ) ;
2006-02-03 22:19:41 +00:00
return NT_STATUS_OK ;
2004-04-12 11:18:32 +00: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 */
2008-12-31 16:30:11 -08:00
if ( asprintf ( err_str , " machine %s rejected the "
2007-11-23 12:04:35 +01:00
" (anonymous) password change: Error was : "
" %s. \n " , remote_machine ,
2008-12-31 16:30:11 -08:00
get_friendly_nt_error_msg ( result ) ) = = - 1 ) {
* err_str = NULL ;
}
2006-07-11 18:01:26 +00:00
cli_shutdown ( cli ) ;
2006-02-03 22:19:41 +00:00
return result ;
2004-04-12 11:18:32 +00:00
}
2008-09-01 21:28:57 +02:00
2004-04-12 11:18:32 +00: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 21:28:57 +02:00
2004-04-12 11:18:32 +00:00
if ( lp_client_lanman_auth ( ) ) {
2005-09-30 17:13:37 +00:00
/* Use the old RAP method. */
2006-07-11 18:01:26 +00:00
if ( cli_oem_change_password ( cli , user_name , new_passwd , old_passwd ) ) {
2004-04-12 11:18:32 +00:00
/* SAMR failed, but the old LanMan protocol worked! */
2006-07-11 18:01:26 +00:00
cli_shutdown ( cli ) ;
2006-02-03 22:19:41 +00:00
return NT_STATUS_OK ;
2004-01-26 08:45:02 +00:00
}
2011-02-04 14:25:52 +01:00
result = cli_nt_error ( cli ) ;
2008-12-31 16:30:11 -08:00
if ( asprintf ( err_str , " machine %s rejected the password "
2007-11-23 12:04:35 +01:00
" change: Error was : %s. \n " ,
2011-02-04 14:25:52 +01:00
remote_machine , nt_errstr ( result ) ) = = - 1 ) {
2008-12-31 16:30:11 -08:00
* err_str = NULL ;
}
2006-07-11 18:01:26 +00:00
cli_shutdown ( cli ) ;
2006-02-03 22:19:41 +00:00
return result ;
2004-01-26 08:45:02 +00:00
} else {
2008-12-31 16:30:11 -08:00
if ( asprintf ( err_str , " SAMR connection to machine %s "
2007-11-23 12:04:35 +01:00
" failed. Error was %s, but LANMAN password "
2010-05-12 21:41:52 +02:00
" changes are disabled \n " ,
2015-04-14 16:59:13 +02:00
remote_machine , nt_errstr ( result ) ) = = - 1 ) {
2008-12-31 16:30:11 -08:00
* err_str = NULL ;
}
2006-07-11 18:01:26 +00:00
cli_shutdown ( cli ) ;
2006-02-03 22:19:41 +00:00
return NT_STATUS_UNSUCCESSFUL ;
2004-01-26 08:45:02 +00:00
}
}
1998-11-12 07:06:48 +00:00
}