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 3 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, see <http://www.gnu.org/licenses/>.
22 /*************************************************************
23 Change a password on a remote machine using IPC calls.
24 *************************************************************/
26 NTSTATUS
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
;
31 struct cli_state
*cli
;
32 struct rpc_pipe_client
*pipe_hnd
;
36 BOOL pass_must_change
= False
;
40 if(!resolve_name( remote_machine
, &ip
, 0x20)) {
41 slprintf(err_str
, err_str_len
-1, "Unable to find an IP address for machine %s.\n",
43 return NT_STATUS_UNSUCCESSFUL
;
46 cli
= cli_initialise();
48 return NT_STATUS_NO_MEMORY
;
51 result
= cli_connect(cli
, remote_machine
, &ip
);
52 if (!NT_STATUS_IS_OK(result
)) {
53 slprintf(err_str
, err_str_len
-1, "Unable to connect to SMB server on machine %s. Error was : %s.\n",
54 remote_machine
, nt_errstr(result
) );
59 make_nmb_name(&calling
, global_myname() , 0x0);
60 make_nmb_name(&called
, remote_machine
, 0x20);
62 if (!cli_session_request(cli
, &calling
, &called
)) {
63 slprintf(err_str
, err_str_len
-1, "machine %s rejected the session setup. Error was : %s.\n",
64 remote_machine
, cli_errstr(cli
) );
65 result
= cli_nt_error(cli
);
70 cli
->protocol
= PROTOCOL_NT1
;
72 if (!cli_negprot(cli
)) {
73 slprintf(err_str
, err_str_len
-1, "machine %s rejected the negotiate protocol. Error was : %s.\n",
74 remote_machine
, cli_errstr(cli
) );
75 result
= cli_nt_error(cli
);
80 /* Given things like SMB signing, restrict anonymous and the like,
81 try an authenticated connection first */
82 result
= cli_session_setup(cli
, user_name
,
83 old_passwd
, strlen(old_passwd
)+1,
84 old_passwd
, strlen(old_passwd
)+1, "");
86 if (!NT_STATUS_IS_OK(result
)) {
88 /* Password must change or Password expired are the only valid
89 * error conditions here from where we can proceed, the rest like
90 * account locked out or logon failure will lead to errors later
93 if (!NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_MUST_CHANGE
) &&
94 !NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_EXPIRED
)) {
95 slprintf(err_str
, err_str_len
-1, "Could not "
96 "connect to machine %s: %s\n",
97 remote_machine
, cli_errstr(cli
));
102 pass_must_change
= True
;
105 * We should connect as the anonymous user here, in case
106 * the server has "must change password" checked...
107 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
110 result
= cli_session_setup(cli
, "", "", 0, "", 0, "");
112 if (!NT_STATUS_IS_OK(result
)) {
113 slprintf(err_str
, err_str_len
-1, "machine %s rejected the session setup. Error was : %s.\n",
114 remote_machine
, cli_errstr(cli
) );
119 cli_init_creds(cli
, "", "", NULL
);
121 cli_init_creds(cli
, user_name
, "", old_passwd
);
124 if (!cli_send_tconX(cli
, "IPC$", "IPC", "", 1)) {
125 slprintf(err_str
, err_str_len
-1, "machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
126 remote_machine
, cli_errstr(cli
) );
127 result
= cli_nt_error(cli
);
132 /* Try not to give the password away too easily */
134 if (!pass_must_change
) {
135 pipe_hnd
= cli_rpc_pipe_open_ntlmssp(cli
,
137 PIPE_AUTH_LEVEL_PRIVACY
,
138 "", /* what domain... ? */
144 * If the user password must be changed the ntlmssp bind will
145 * fail the same way as the session setup above did. The
146 * difference ist that with a pipe bind we don't get a good
147 * error message, the result will be that the rpc call below
148 * will just fail. So we do it anonymously, there's no other
151 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SAMR
, &result
);
155 if (lp_client_lanman_auth()) {
156 /* Use the old RAP method. */
157 if (!cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
158 slprintf(err_str
, err_str_len
-1, "machine %s rejected the password change: Error was : %s.\n",
159 remote_machine
, cli_errstr(cli
) );
160 result
= cli_nt_error(cli
);
165 slprintf(err_str
, err_str_len
-1,
166 "SAMR connection to machine %s failed. Error was %s, "
167 "but LANMAN password changed are disabled\n",
168 nt_errstr(result
), remote_machine
);
169 result
= cli_nt_error(cli
);
175 if (NT_STATUS_IS_OK(result
= rpccli_samr_chgpasswd_user(pipe_hnd
, cli
->mem_ctx
, user_name
,
176 new_passwd
, old_passwd
))) {
177 /* Great - it all worked! */
181 } else if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
182 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
183 /* it failed, but for reasons such as wrong password, too short etc ... */
185 slprintf(err_str
, err_str_len
-1, "machine %s rejected the password change: Error was : %s.\n",
186 remote_machine
, get_friendly_nt_error_msg(result
));
191 /* OK, that failed, so try again... */
192 cli_rpc_pipe_close(pipe_hnd
);
194 /* Try anonymous NTLMSSP... */
195 cli_init_creds(cli
, "", "", NULL
);
197 result
= NT_STATUS_UNSUCCESSFUL
;
199 /* OK, this is ugly, but... try an anonymous pipe. */
200 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SAMR
, &result
);
203 (NT_STATUS_IS_OK(result
= rpccli_samr_chgpasswd_user(pipe_hnd
,
208 /* Great - it all worked! */
212 if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
213 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
214 /* it failed, but again it was due to things like new password too short */
216 slprintf(err_str
, err_str_len
-1,
217 "machine %s rejected the (anonymous) password change: Error was : %s.\n",
218 remote_machine
, get_friendly_nt_error_msg(result
));
223 /* We have failed to change the user's password, and we think the server
224 just might not support SAMR password changes, so fall back */
226 if (lp_client_lanman_auth()) {
227 /* Use the old RAP method. */
228 if (cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
229 /* SAMR failed, but the old LanMan protocol worked! */
234 slprintf(err_str
, err_str_len
-1,
235 "machine %s rejected the password change: Error was : %s.\n",
236 remote_machine
, cli_errstr(cli
) );
237 result
= cli_nt_error(cli
);
241 slprintf(err_str
, err_str_len
-1,
242 "SAMR connection to machine %s failed. Error was %s, "
243 "but LANMAN password changed are disabled\n",
244 nt_errstr(result
), remote_machine
);
246 return NT_STATUS_UNSUCCESSFUL
;