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 "rpc_client/cli_samr.h"
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
,
31 struct nmb_name calling
, called
;
32 struct cli_state
*cli
= NULL
;
33 struct rpc_pipe_client
*pipe_hnd
= NULL
;
34 struct sockaddr_storage ss
;
35 char *user
, *domain
, *p
;
38 bool pass_must_change
= False
;
40 user
= talloc_strdup(talloc_tos(), user_name
);
41 SMB_ASSERT(user
!= NULL
);
42 domain
= talloc_strdup(talloc_tos(), "");
43 SMB_ASSERT(domain
!= NULL
);
45 /* allow usernames of the form domain\\user or domain/user */
46 if ((p
= strchr_m(user
,'\\')) || (p
= strchr_m(user
,'/')) ||
47 (p
= strchr_m(user
,*lp_winbind_separator()))) {
55 if(!resolve_name( remote_machine
, &ss
, 0x20, false)) {
56 if (asprintf(err_str
, "Unable to find an IP address for machine "
57 "%s.\n", remote_machine
) == -1) {
60 return NT_STATUS_UNSUCCESSFUL
;
63 cli
= cli_initialise();
65 return NT_STATUS_NO_MEMORY
;
68 result
= cli_connect(cli
, remote_machine
, &ss
);
69 if (!NT_STATUS_IS_OK(result
)) {
70 if (asprintf(err_str
, "Unable to connect to SMB server on "
71 "machine %s. Error was : %s.\n",
72 remote_machine
, nt_errstr(result
))==-1) {
79 make_nmb_name(&calling
, global_myname() , 0x0);
80 make_nmb_name(&called
, remote_machine
, 0x20);
82 if (!cli_session_request(cli
, &calling
, &called
)) {
83 if (asprintf(err_str
, "machine %s rejected the session setup. "
85 remote_machine
, cli_errstr(cli
)) == -1) {
88 result
= cli_nt_error(cli
);
93 cli
->protocol
= PROTOCOL_NT1
;
95 result
= cli_negprot(cli
);
97 if (!NT_STATUS_IS_OK(result
)) {
98 if (asprintf(err_str
, "machine %s rejected the negotiate "
99 "protocol. Error was : %s.\n",
100 remote_machine
, nt_errstr(result
)) == -1) {
103 result
= cli_nt_error(cli
);
108 /* Given things like SMB signing, restrict anonymous and the like,
109 try an authenticated connection first */
110 result
= cli_session_setup(cli
, user_name
,
111 old_passwd
, strlen(old_passwd
)+1,
112 old_passwd
, strlen(old_passwd
)+1, "");
114 if (!NT_STATUS_IS_OK(result
)) {
116 /* Password must change or Password expired are the only valid
117 * error conditions here from where we can proceed, the rest like
118 * account locked out or logon failure will lead to errors later
121 if (!NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_MUST_CHANGE
) &&
122 !NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_EXPIRED
)) {
123 if (asprintf(err_str
, "Could not connect to machine %s: "
124 "%s\n", remote_machine
, cli_errstr(cli
)) == -1) {
131 pass_must_change
= True
;
134 * We should connect as the anonymous user here, in case
135 * the server has "must change password" checked...
136 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
139 result
= cli_session_setup(cli
, "", "", 0, "", 0, "");
141 if (!NT_STATUS_IS_OK(result
)) {
142 if (asprintf(err_str
, "machine %s rejected the session "
143 "setup. Error was : %s.\n",
144 remote_machine
, cli_errstr(cli
)) == -1) {
151 result
= cli_init_creds(cli
, "", "", NULL
);
152 if (!NT_STATUS_IS_OK(result
)) {
157 result
= cli_init_creds(cli
, user
, domain
, old_passwd
);
158 if (!NT_STATUS_IS_OK(result
)) {
164 result
= cli_tcon_andx(cli
, "IPC$", "IPC", "", 1);
165 if (!NT_STATUS_IS_OK(result
)) {
166 if (asprintf(err_str
, "machine %s rejected the tconX on the "
167 "IPC$ share. Error was : %s.\n",
168 remote_machine
, nt_errstr(result
))) {
175 /* Try not to give the password away too easily */
177 if (!pass_must_change
) {
178 result
= cli_rpc_pipe_open_ntlmssp(cli
,
179 &ndr_table_samr
.syntax_id
,
181 DCERPC_AUTH_LEVEL_PRIVACY
,
187 * If the user password must be changed the ntlmssp bind will
188 * fail the same way as the session setup above did. The
189 * difference ist that with a pipe bind we don't get a good
190 * error message, the result will be that the rpc call below
191 * will just fail. So we do it anonymously, there's no other
194 result
= cli_rpc_pipe_open_noauth(
195 cli
, &ndr_table_samr
.syntax_id
, &pipe_hnd
);
198 if (!NT_STATUS_IS_OK(result
)) {
199 if (lp_client_lanman_auth()) {
200 /* Use the old RAP method. */
201 if (!cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
202 if (asprintf(err_str
, "machine %s rejected the "
203 "password change: Error was : %s.\n",
204 remote_machine
, cli_errstr(cli
)) == -1) {
207 result
= cli_nt_error(cli
);
212 if (asprintf(err_str
, "SAMR connection to machine %s "
213 "failed. Error was %s, but LANMAN password "
214 "changes are disabled\n",
215 remote_machine
, nt_errstr(result
)) == -1) {
218 result
= cli_nt_error(cli
);
224 result
= rpccli_samr_chgpasswd_user2(pipe_hnd
, talloc_tos(),
225 user_name
, new_passwd
, old_passwd
);
226 if (NT_STATUS_IS_OK(result
)) {
227 /* Great - it all worked! */
231 } else if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
232 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
233 /* it failed, but for reasons such as wrong password, too short etc ... */
235 if (asprintf(err_str
, "machine %s rejected the password change: "
237 remote_machine
, get_friendly_nt_error_msg(result
)) == -1) {
244 /* OK, that failed, so try again... */
245 TALLOC_FREE(pipe_hnd
);
247 /* Try anonymous NTLMSSP... */
248 result
= cli_init_creds(cli
, "", "", NULL
);
249 if (!NT_STATUS_IS_OK(result
)) {
254 result
= NT_STATUS_UNSUCCESSFUL
;
256 /* OK, this is ugly, but... try an anonymous pipe. */
257 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
260 if ( NT_STATUS_IS_OK(result
) &&
261 (NT_STATUS_IS_OK(result
= rpccli_samr_chgpasswd_user2(
262 pipe_hnd
, talloc_tos(), user_name
,
263 new_passwd
, old_passwd
)))) {
264 /* Great - it all worked! */
268 if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
269 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
270 /* it failed, but again it was due to things like new password too short */
272 if (asprintf(err_str
, "machine %s rejected the "
273 "(anonymous) password change: Error was : "
274 "%s.\n", remote_machine
,
275 get_friendly_nt_error_msg(result
)) == -1) {
282 /* We have failed to change the user's password, and we think the server
283 just might not support SAMR password changes, so fall back */
285 if (lp_client_lanman_auth()) {
286 /* Use the old RAP method. */
287 if (cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
288 /* SAMR failed, but the old LanMan protocol worked! */
293 if (asprintf(err_str
, "machine %s rejected the password "
294 "change: Error was : %s.\n",
295 remote_machine
, cli_errstr(cli
)) == -1) {
298 result
= cli_nt_error(cli
);
302 if (asprintf(err_str
, "SAMR connection to machine %s "
303 "failed. Error was %s, but LANMAN password "
304 "changes are disabled\n",
305 nt_errstr(result
), remote_machine
) == -1) {
309 return NT_STATUS_UNSUCCESSFUL
;