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 *************************************************************/
27 NTSTATUS
remote_password_change(const char *remote_machine
, const char *user_name
,
28 const char *old_passwd
, const char *new_passwd
,
29 char *err_str
, size_t err_str_len
)
31 struct nmb_name calling
, called
;
32 struct cli_state
*cli
;
33 struct rpc_pipe_client
*pipe_hnd
;
37 BOOL pass_must_change
= False
;
41 if(!resolve_name( remote_machine
, &ip
, 0x20)) {
42 slprintf(err_str
, err_str_len
-1, "unable to find an IP address for machine %s.\n",
44 return NT_STATUS_UNSUCCESSFUL
;
47 cli
= cli_initialise();
49 return NT_STATUS_NO_MEMORY
;
52 if (!cli_connect(cli
, remote_machine
, &ip
)) {
53 slprintf(err_str
, err_str_len
-1, "unable to connect to SMB server on machine %s. Error was : %s.\n",
54 remote_machine
, cli_errstr(cli
) );
55 result
= cli_nt_error(cli
);
60 make_nmb_name(&calling
, global_myname() , 0x0);
61 make_nmb_name(&called
, remote_machine
, 0x20);
63 if (!cli_session_request(cli
, &calling
, &called
)) {
64 slprintf(err_str
, err_str_len
-1, "machine %s rejected the session setup. Error was : %s.\n",
65 remote_machine
, cli_errstr(cli
) );
66 result
= cli_nt_error(cli
);
71 cli
->protocol
= PROTOCOL_NT1
;
73 if (!cli_negprot(cli
)) {
74 slprintf(err_str
, err_str_len
-1, "machine %s rejected the negotiate protocol. Error was : %s.\n",
75 remote_machine
, cli_errstr(cli
) );
76 result
= cli_nt_error(cli
);
81 /* Given things like SMB signing, restrict anonymous and the like,
82 try an authenticated connection first */
83 if (!cli_session_setup(cli
, user_name
, old_passwd
, strlen(old_passwd
)+1, old_passwd
, strlen(old_passwd
)+1, "")) {
85 result
= cli_nt_error(cli
);
87 if (!NT_STATUS_IS_OK(result
)) {
89 /* Password must change is the only valid error
90 * condition here from where we can proceed, the rest
91 * like account locked out or logon failure will lead
92 * to errors later anyway */
94 if (!NT_STATUS_EQUAL(result
,
95 NT_STATUS_PASSWORD_MUST_CHANGE
)) {
96 slprintf(err_str
, err_str_len
-1, "Could not "
97 "connect to machine %s: %s\n",
98 remote_machine
, cli_errstr(cli
));
103 pass_must_change
= True
;
107 * We should connect as the anonymous user here, in case
108 * the server has "must change password" checked...
109 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
112 if (!cli_session_setup(cli
, "", "", 0, "", 0, "")) {
113 slprintf(err_str
, err_str_len
-1, "machine %s rejected the session setup. Error was : %s.\n",
114 remote_machine
, cli_errstr(cli
) );
115 result
= cli_nt_error(cli
);
120 cli_init_creds(cli
, "", "", NULL
);
122 cli_init_creds(cli
, user_name
, "", old_passwd
);
125 if (!cli_send_tconX(cli
, "IPC$", "IPC", "", 1)) {
126 slprintf(err_str
, err_str_len
-1, "machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
127 remote_machine
, cli_errstr(cli
) );
128 result
= cli_nt_error(cli
);
133 /* Try not to give the password away too easily */
135 if (!pass_must_change
) {
136 pipe_hnd
= cli_rpc_pipe_open_ntlmssp(cli
,
138 PIPE_AUTH_LEVEL_PRIVACY
,
139 "", /* what domain... ? */
145 * If the user password must be changed the ntlmssp bind will
146 * fail the same way as the session setup above did. The
147 * difference ist that with a pipe bind we don't get a good
148 * error message, the result will be that the rpc call below
149 * will just fail. So we do it anonymously, there's no other
152 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SAMR
, &result
);
156 if (lp_client_lanman_auth()) {
157 /* Use the old RAP method. */
158 if (!cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
159 slprintf(err_str
, err_str_len
-1, "machine %s rejected the password change: Error was : %s.\n",
160 remote_machine
, cli_errstr(cli
) );
161 result
= cli_nt_error(cli
);
166 slprintf(err_str
, err_str_len
-1,
167 "SAMR connection to machine %s failed. Error was %s, "
168 "but LANMAN password changed are disabled\n",
169 nt_errstr(result
), remote_machine
);
170 result
= cli_nt_error(cli
);
176 if (NT_STATUS_IS_OK(result
= rpccli_samr_chgpasswd_user(pipe_hnd
, cli
->mem_ctx
, user_name
,
177 new_passwd
, old_passwd
))) {
178 /* Great - it all worked! */
182 } else if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
183 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
184 /* it failed, but for reasons such as wrong password, too short etc ... */
186 slprintf(err_str
, err_str_len
-1, "machine %s rejected the password change: Error was : %s.\n",
187 remote_machine
, get_friendly_nt_error_msg(result
));
192 /* OK, that failed, so try again... */
193 cli_rpc_pipe_close(pipe_hnd
);
195 /* Try anonymous NTLMSSP... */
196 cli_init_creds(cli
, "", "", NULL
);
198 result
= NT_STATUS_UNSUCCESSFUL
;
200 /* OK, this is ugly, but... try an anonymous pipe. */
201 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SAMR
, &result
);
204 (NT_STATUS_IS_OK(result
= rpccli_samr_chgpasswd_user(pipe_hnd
,
209 /* Great - it all worked! */
213 if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
214 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
215 /* it failed, but again it was due to things like new password too short */
217 slprintf(err_str
, err_str_len
-1,
218 "machine %s rejected the (anonymous) password change: Error was : %s.\n",
219 remote_machine
, get_friendly_nt_error_msg(result
));
224 /* We have failed to change the user's password, and we think the server
225 just might not support SAMR password changes, so fall back */
227 if (lp_client_lanman_auth()) {
228 /* Use the old RAP method. */
229 if (cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
230 /* SAMR failed, but the old LanMan protocol worked! */
235 slprintf(err_str
, err_str_len
-1,
236 "machine %s rejected the password change: Error was : %s.\n",
237 remote_machine
, cli_errstr(cli
) );
238 result
= cli_nt_error(cli
);
242 slprintf(err_str
, err_str_len
-1,
243 "SAMR connection to machine %s failed. Error was %s, "
244 "but LANMAN password changed are disabled\n",
245 nt_errstr(result
), remote_machine
);
247 return NT_STATUS_UNSUCCESSFUL
;