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_samr.h"
24 /*************************************************************
25 Change a password on a remote machine using IPC calls.
26 *************************************************************/
28 NTSTATUS
remote_password_change(const char *remote_machine
, const char *user_name
,
29 const char *old_passwd
, const char *new_passwd
,
32 struct nmb_name calling
, called
;
33 struct cli_state
*cli
= NULL
;
34 struct rpc_pipe_client
*pipe_hnd
= NULL
;
35 struct sockaddr_storage ss
;
36 char *user
, *domain
, *p
;
39 bool pass_must_change
= False
;
41 user
= talloc_strdup(talloc_tos(), user_name
);
42 SMB_ASSERT(user
!= NULL
);
43 domain
= talloc_strdup(talloc_tos(), "");
44 SMB_ASSERT(domain
!= NULL
);
46 /* allow usernames of the form domain\\user or domain/user */
47 if ((p
= strchr_m(user
,'\\')) || (p
= strchr_m(user
,'/')) ||
48 (p
= strchr_m(user
,*lp_winbind_separator()))) {
56 if(!resolve_name( remote_machine
, &ss
, 0x20, false)) {
57 if (asprintf(err_str
, "Unable to find an IP address for machine "
58 "%s.\n", remote_machine
) == -1) {
61 return NT_STATUS_UNSUCCESSFUL
;
64 cli
= cli_initialise();
66 return NT_STATUS_NO_MEMORY
;
69 result
= cli_connect(cli
, remote_machine
, &ss
);
70 if (!NT_STATUS_IS_OK(result
)) {
71 if (asprintf(err_str
, "Unable to connect to SMB server on "
72 "machine %s. Error was : %s.\n",
73 remote_machine
, nt_errstr(result
))==-1) {
80 make_nmb_name(&calling
, global_myname() , 0x0);
81 make_nmb_name(&called
, remote_machine
, 0x20);
83 if (!cli_session_request(cli
, &calling
, &called
)) {
84 if (asprintf(err_str
, "machine %s rejected the session setup. "
86 remote_machine
, cli_errstr(cli
)) == -1) {
89 result
= cli_nt_error(cli
);
94 cli
->protocol
= PROTOCOL_NT1
;
96 result
= cli_negprot(cli
);
98 if (!NT_STATUS_IS_OK(result
)) {
99 if (asprintf(err_str
, "machine %s rejected the negotiate "
100 "protocol. Error was : %s.\n",
101 remote_machine
, nt_errstr(result
)) == -1) {
104 result
= cli_nt_error(cli
);
109 /* Given things like SMB signing, restrict anonymous and the like,
110 try an authenticated connection first */
111 result
= cli_session_setup(cli
, user_name
,
112 old_passwd
, strlen(old_passwd
)+1,
113 old_passwd
, strlen(old_passwd
)+1, "");
115 if (!NT_STATUS_IS_OK(result
)) {
117 /* Password must change or Password expired are the only valid
118 * error conditions here from where we can proceed, the rest like
119 * account locked out or logon failure will lead to errors later
122 if (!NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_MUST_CHANGE
) &&
123 !NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_EXPIRED
)) {
124 if (asprintf(err_str
, "Could not connect to machine %s: "
125 "%s\n", remote_machine
, cli_errstr(cli
)) == -1) {
132 pass_must_change
= True
;
135 * We should connect as the anonymous user here, in case
136 * the server has "must change password" checked...
137 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
140 result
= cli_session_setup(cli
, "", "", 0, "", 0, "");
142 if (!NT_STATUS_IS_OK(result
)) {
143 if (asprintf(err_str
, "machine %s rejected the session "
144 "setup. Error was : %s.\n",
145 remote_machine
, cli_errstr(cli
)) == -1) {
152 result
= cli_init_creds(cli
, "", "", NULL
);
153 if (!NT_STATUS_IS_OK(result
)) {
158 result
= cli_init_creds(cli
, user
, domain
, old_passwd
);
159 if (!NT_STATUS_IS_OK(result
)) {
165 result
= cli_tcon_andx(cli
, "IPC$", "IPC", "", 1);
166 if (!NT_STATUS_IS_OK(result
)) {
167 if (asprintf(err_str
, "machine %s rejected the tconX on the "
168 "IPC$ share. Error was : %s.\n",
169 remote_machine
, nt_errstr(result
))) {
176 /* Try not to give the password away too easily */
178 if (!pass_must_change
) {
179 result
= cli_rpc_pipe_open_ntlmssp(cli
,
180 &ndr_table_samr
.syntax_id
,
182 DCERPC_AUTH_LEVEL_PRIVACY
,
188 * If the user password must be changed the ntlmssp bind will
189 * fail the same way as the session setup above did. The
190 * difference ist that with a pipe bind we don't get a good
191 * error message, the result will be that the rpc call below
192 * will just fail. So we do it anonymously, there's no other
195 result
= cli_rpc_pipe_open_noauth(
196 cli
, &ndr_table_samr
.syntax_id
, &pipe_hnd
);
199 if (!NT_STATUS_IS_OK(result
)) {
200 if (lp_client_lanman_auth()) {
201 /* Use the old RAP method. */
202 if (!cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
203 if (asprintf(err_str
, "machine %s rejected the "
204 "password change: Error was : %s.\n",
205 remote_machine
, cli_errstr(cli
)) == -1) {
208 result
= cli_nt_error(cli
);
213 if (asprintf(err_str
, "SAMR connection to machine %s "
214 "failed. Error was %s, but LANMAN password "
215 "changes are disabled\n",
216 remote_machine
, nt_errstr(result
)) == -1) {
219 result
= cli_nt_error(cli
);
225 result
= rpccli_samr_chgpasswd_user2(pipe_hnd
, talloc_tos(),
226 user_name
, new_passwd
, old_passwd
);
227 if (NT_STATUS_IS_OK(result
)) {
228 /* Great - it all worked! */
232 } else if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
233 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
234 /* it failed, but for reasons such as wrong password, too short etc ... */
236 if (asprintf(err_str
, "machine %s rejected the password change: "
238 remote_machine
, get_friendly_nt_error_msg(result
)) == -1) {
245 /* OK, that failed, so try again... */
246 TALLOC_FREE(pipe_hnd
);
248 /* Try anonymous NTLMSSP... */
249 result
= cli_init_creds(cli
, "", "", NULL
);
250 if (!NT_STATUS_IS_OK(result
)) {
255 result
= NT_STATUS_UNSUCCESSFUL
;
257 /* OK, this is ugly, but... try an anonymous pipe. */
258 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
261 if ( NT_STATUS_IS_OK(result
) &&
262 (NT_STATUS_IS_OK(result
= rpccli_samr_chgpasswd_user2(
263 pipe_hnd
, talloc_tos(), user_name
,
264 new_passwd
, old_passwd
)))) {
265 /* Great - it all worked! */
269 if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
270 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
271 /* it failed, but again it was due to things like new password too short */
273 if (asprintf(err_str
, "machine %s rejected the "
274 "(anonymous) password change: Error was : "
275 "%s.\n", remote_machine
,
276 get_friendly_nt_error_msg(result
)) == -1) {
283 /* We have failed to change the user's password, and we think the server
284 just might not support SAMR password changes, so fall back */
286 if (lp_client_lanman_auth()) {
287 /* Use the old RAP method. */
288 if (cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
289 /* SAMR failed, but the old LanMan protocol worked! */
294 if (asprintf(err_str
, "machine %s rejected the password "
295 "change: Error was : %s.\n",
296 remote_machine
, cli_errstr(cli
)) == -1) {
299 result
= cli_nt_error(cli
);
303 if (asprintf(err_str
, "SAMR connection to machine %s "
304 "failed. Error was %s, but LANMAN password "
305 "changes are disabled\n",
306 nt_errstr(result
), remote_machine
) == -1) {
310 return NT_STATUS_UNSUCCESSFUL
;