2 Unix SMB/CIFS implementation.
3 SMB client password change routine
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /*************************************************************
24 change a password on a remote machine using IPC calls
25 *************************************************************/
26 BOOL
remote_password_change(const char *remote_machine
, const char *user_name
,
27 const char *old_passwd
, const char *new_passwd
,
28 char *err_str
, size_t err_str_len
)
30 struct nmb_name calling
, called
;
33 struct ntuser_creds creds
;
39 if(!resolve_name( remote_machine
, &ip
, 0x20)) {
40 slprintf(err_str
, err_str_len
-1, "unable to find an IP address for machine %s.\n",
47 if (!cli_initialise(&cli
) || !cli_connect(&cli
, remote_machine
, &ip
)) {
48 slprintf(err_str
, err_str_len
-1, "unable to connect to SMB server on machine %s. Error was : %s.\n",
49 remote_machine
, cli_errstr(&cli
) );
53 make_nmb_name(&calling
, global_myname() , 0x0);
54 make_nmb_name(&called
, remote_machine
, 0x20);
56 if (!cli_session_request(&cli
, &calling
, &called
)) {
57 slprintf(err_str
, err_str_len
-1, "machine %s rejected the session setup. Error was : %s.\n",
58 remote_machine
, cli_errstr(&cli
) );
63 cli
.protocol
= PROTOCOL_NT1
;
65 if (!cli_negprot(&cli
)) {
66 slprintf(err_str
, err_str_len
-1, "machine %s rejected the negotiate protocol. Error was : %s.\n",
67 remote_machine
, cli_errstr(&cli
) );
72 /* Given things like SMB signing, restrict anonymous and the like,
73 try an authenticated connection first */
74 if (!cli_session_setup(&cli
, user_name
, old_passwd
, strlen(old_passwd
)+1, old_passwd
, strlen(old_passwd
)+1, "")) {
76 * We should connect as the anonymous user here, in case
77 * the server has "must change password" checked...
78 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
81 if (!cli_session_setup(&cli
, "", "", 0, "", 0, "")) {
82 slprintf(err_str
, err_str_len
-1, "machine %s rejected the session setup. Error was : %s.\n",
83 remote_machine
, cli_errstr(&cli
) );
88 init_creds(&creds
, "", "", NULL
);
89 cli_init_creds(&cli
, &creds
);
91 init_creds(&creds
, user_name
, "", old_passwd
);
92 cli_init_creds(&cli
, &creds
);
95 if (!cli_send_tconX(&cli
, "IPC$", "IPC", "", 1)) {
96 slprintf(err_str
, err_str_len
-1, "machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
97 remote_machine
, cli_errstr(&cli
) );
102 /* Try not to give the password away to easily */
104 cli
.pipe_auth_flags
= AUTH_PIPE_NTLMSSP
;
105 cli
.pipe_auth_flags
|= AUTH_PIPE_SIGN
;
106 cli
.pipe_auth_flags
|= AUTH_PIPE_SEAL
;
108 if ( !cli_nt_session_open( &cli
, PI_SAMR
) ) {
109 if (lp_client_lanman_auth()) {
110 if (!cli_oem_change_password(&cli
, user_name
, new_passwd
, old_passwd
)) {
111 slprintf(err_str
, err_str_len
-1, "machine %s rejected the password change: Error was : %s.\n",
112 remote_machine
, cli_errstr(&cli
) );
117 slprintf(err_str
, err_str_len
-1, "machine %s does not support SAMR connections, but LANMAN password changed are disabled\n",
124 if (NT_STATUS_IS_OK(result
= cli_samr_chgpasswd_user(&cli
, cli
.mem_ctx
, user_name
,
125 new_passwd
, old_passwd
))) {
126 /* Great - it all worked! */
130 } else if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
131 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
132 /* it failed, but for reasons such as wrong password, too short etc ... */
134 slprintf(err_str
, err_str_len
-1, "machine %s rejected the password change: Error was : %s.\n",
135 remote_machine
, get_friendly_nt_error_msg(result
));
140 /* OK, that failed, so try again... */
141 cli_nt_session_close(&cli
);
143 /* Try anonymous NTLMSSP... */
144 init_creds(&creds
, "", "", NULL
);
145 cli_init_creds(&cli
, &creds
);
147 result
= NT_STATUS_UNSUCCESSFUL
;
149 /* OK, this is ugly, but... */
150 if ( cli_nt_session_open( &cli
, PI_SAMR
)
151 && NT_STATUS_IS_OK(result
152 = cli_samr_chgpasswd_user(&cli
, cli
.mem_ctx
, user_name
,
153 new_passwd
, old_passwd
))) {
154 /* Great - it all worked! */
159 if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
160 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
161 /* it failed, but again it was due to things like new password too short */
163 slprintf(err_str
, err_str_len
-1,
164 "machine %s rejected the (anonymous) password change: Error was : %s.\n",
165 remote_machine
, get_friendly_nt_error_msg(result
));
170 /* We have failed to change the user's password, and we think the server
171 just might not support SAMR password changes, so fall back */
173 if (lp_client_lanman_auth()) {
174 if (cli_oem_change_password(&cli
, user_name
, new_passwd
, old_passwd
)) {
175 /* SAMR failed, but the old LanMan protocol worked! */
180 slprintf(err_str
, err_str_len
-1,
181 "machine %s rejected the password change: Error was : %s.\n",
182 remote_machine
, cli_errstr(&cli
) );
186 slprintf(err_str
, err_str_len
-1,
187 "machine %s does not support SAMR connections, but LANMAN password changed are disabled\n",