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/>.
21 #include "../librpc/gen_ndr/ndr_samr.h"
22 #include "rpc_client/cli_pipe.h"
23 #include "rpc_client/cli_samr.h"
24 #include "libsmb/libsmb.h"
25 #include "libsmb/clirap.h"
26 #include "libsmb/nmblib.h"
27 #include "../libcli/smb/smbXcli_base.h"
29 /*************************************************************
30 Change a password on a remote machine using IPC calls.
31 *************************************************************/
33 NTSTATUS
remote_password_change(const char *remote_machine
,
34 const char *domain
, const char *user_name
,
35 const char *old_passwd
, const char *new_passwd
,
38 struct cli_state
*cli
= NULL
;
39 struct cli_credentials
*creds
= NULL
;
40 struct rpc_pipe_client
*pipe_hnd
= NULL
;
43 bool pass_must_change
= False
;
47 result
= cli_connect_nb(talloc_tos(),
53 SMB_SIGNING_IPC_DEFAULT
,
56 if (!NT_STATUS_IS_OK(result
)) {
57 if (NT_STATUS_EQUAL(result
, NT_STATUS_NOT_SUPPORTED
)) {
58 if (asprintf(err_str
, "Unable to connect to SMB server on "
59 "machine %s. NetBIOS support disabled\n",
60 remote_machine
) == -1) {
64 if (asprintf(err_str
, "Unable to connect to SMB server on "
65 "machine %s. Error was : %s.\n",
66 remote_machine
, nt_errstr(result
))==-1) {
73 creds
= cli_session_creds_init(cli
,
78 false, /* use_kerberos */
79 false, /* fallback_after_kerberos */
80 false, /* use_ccache */
81 false); /* password_is_nt_hash */
82 SMB_ASSERT(creds
!= NULL
);
84 result
= smbXcli_negprot(cli
->conn
,
86 lp_client_ipc_min_protocol(),
87 lp_client_ipc_max_protocol(),
92 if (!NT_STATUS_IS_OK(result
)) {
93 if (asprintf(err_str
, "machine %s rejected the negotiate "
94 "protocol. Error was : %s.\n",
95 remote_machine
, nt_errstr(result
)) == -1) {
102 /* Given things like SMB signing, restrict anonymous and the like,
103 try an authenticated connection first */
104 result
= cli_session_setup_creds(cli
, creds
);
106 if (!NT_STATUS_IS_OK(result
)) {
108 /* Password must change or Password expired are the only valid
109 * error conditions here from where we can proceed, the rest like
110 * account locked out or logon failure will lead to errors later
113 if (!NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_MUST_CHANGE
) &&
114 !NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_EXPIRED
)) {
115 if (asprintf(err_str
, "Could not connect to machine %s: "
116 "%s\n", remote_machine
, nt_errstr(result
)) == -1) {
123 pass_must_change
= True
;
126 * We should connect as the anonymous user here, in case
127 * the server has "must change password" checked...
128 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
131 result
= cli_session_setup_anon(cli
);
133 if (!NT_STATUS_IS_OK(result
)) {
134 if (asprintf(err_str
, "machine %s rejected the session "
135 "setup. Error was : %s.\n",
136 remote_machine
, nt_errstr(result
)) == -1) {
144 result
= cli_tree_connect(cli
, "IPC$", "IPC", NULL
);
145 if (!NT_STATUS_IS_OK(result
)) {
146 if (asprintf(err_str
, "machine %s rejected the tconX on the "
147 "IPC$ share. Error was : %s.\n",
148 remote_machine
, nt_errstr(result
))) {
155 /* Try not to give the password away too easily */
157 if (!pass_must_change
) {
158 const struct sockaddr_storage
*remote_sockaddr
=
159 smbXcli_conn_remote_sockaddr(cli
->conn
);
161 result
= cli_rpc_pipe_open_with_creds(cli
,
164 DCERPC_AUTH_TYPE_NTLMSSP
,
165 DCERPC_AUTH_LEVEL_PRIVACY
,
172 * If the user password must be changed the ntlmssp bind will
173 * fail the same way as the session setup above did. The
174 * difference is that with a pipe bind we don't get a good
175 * error message, the result will be that the rpc call below
176 * will just fail. So we do it anonymously, there's no other
179 result
= cli_rpc_pipe_open_noauth(
180 cli
, &ndr_table_samr
, &pipe_hnd
);
183 if (!NT_STATUS_IS_OK(result
)) {
184 if (lp_client_lanman_auth()) {
185 /* Use the old RAP method. */
186 result
= cli_oem_change_password(cli
,
190 if (!NT_STATUS_IS_OK(result
)) {
191 if (asprintf(err_str
, "machine %s rejected the "
192 "password change: Error was : %s.\n",
193 remote_machine
, nt_errstr(result
)) == -1) {
200 if (asprintf(err_str
, "SAMR connection to machine %s "
201 "failed. Error was %s, but LANMAN password "
202 "changes are disabled\n",
203 remote_machine
, nt_errstr(result
)) == -1) {
211 status
= dcerpc_samr_chgpasswd_user4(pipe_hnd
->binding_handle
,
213 pipe_hnd
->srv_name_slash
,
218 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
219 /* All good, password successfully changed. */
223 if (!NT_STATUS_IS_OK(status
)) {
224 if (NT_STATUS_EQUAL(status
,
225 NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
) ||
226 NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
) ||
227 NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)) {
228 /* DO NOT FALLBACK TO RC4 */
229 if (lp_weak_crypto() == SAMBA_WEAK_CRYPTO_DISALLOWED
) {
231 return NT_STATUS_STRONG_CRYPTO_NOT_SUPPORTED
;
235 if (!NT_STATUS_IS_OK(result
)) {
238 "machine %s rejected to change the password"
241 get_friendly_nt_error_msg(result
));
250 result
= rpccli_samr_chgpasswd_user2(pipe_hnd
, talloc_tos(),
251 user_name
, new_passwd
, old_passwd
);
252 if (NT_STATUS_IS_OK(result
)) {
253 /* Great - it all worked! */
258 if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) ||
259 NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
)))
261 /* it failed, but for reasons such as wrong password, too short etc ... */
263 if (asprintf(err_str
, "machine %s rejected the password change: "
265 remote_machine
, get_friendly_nt_error_msg(result
)) == -1) {
272 /* OK, that failed, so try again... */
273 TALLOC_FREE(pipe_hnd
);
275 /* OK, this is ugly, but... try an anonymous pipe. */
276 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
,
279 if ( NT_STATUS_IS_OK(result
) &&
280 (NT_STATUS_IS_OK(result
= rpccli_samr_chgpasswd_user2(
281 pipe_hnd
, talloc_tos(), user_name
,
282 new_passwd
, old_passwd
)))) {
283 /* Great - it all worked! */
287 if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
288 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
289 /* it failed, but again it was due to things like new password too short */
291 if (asprintf(err_str
, "machine %s rejected the "
292 "(anonymous) password change: Error was : "
293 "%s.\n", remote_machine
,
294 get_friendly_nt_error_msg(result
)) == -1) {
301 /* We have failed to change the user's password, and we think the server
302 just might not support SAMR password changes, so fall back */
304 if (!lp_client_lanman_auth()) {
305 if (asprintf(err_str
, "SAMR connection to machine %s "
306 "failed. Error was %s, but LANMAN password "
307 "changes are disabled\n",
308 remote_machine
, nt_errstr(result
)) == -1) {
312 return NT_STATUS_UNSUCCESSFUL
;
315 /* Use the old RAP method. */
316 result
= cli_oem_change_password(cli
,
320 if (NT_STATUS_IS_OK(result
)) {
321 /* SAMR failed, but the old LanMan protocol worked! */
327 if (asprintf(err_str
,
328 "machine %s rejected the password "
329 "change: Error was : %s.\n",
331 nt_errstr(result
)) == -1)