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"
28 /*************************************************************
29 Change a password on a remote machine using IPC calls.
30 *************************************************************/
32 NTSTATUS
remote_password_change(const char *remote_machine
, const char *user_name
,
33 const char *old_passwd
, const char *new_passwd
,
36 struct nmb_name calling
, called
;
37 struct cli_state
*cli
= NULL
;
38 struct rpc_pipe_client
*pipe_hnd
= NULL
;
39 struct sockaddr_storage ss
;
40 char *user
, *domain
, *p
;
43 bool pass_must_change
= False
;
45 user
= talloc_strdup(talloc_tos(), user_name
);
46 SMB_ASSERT(user
!= NULL
);
47 domain
= talloc_strdup(talloc_tos(), "");
48 SMB_ASSERT(domain
!= NULL
);
50 /* allow usernames of the form domain\\user or domain/user */
51 if ((p
= strchr_m(user
,'\\')) || (p
= strchr_m(user
,'/')) ||
52 (p
= strchr_m(user
,*lp_winbind_separator()))) {
60 if(!resolve_name( remote_machine
, &ss
, 0x20, false)) {
61 if (asprintf(err_str
, "Unable to find an IP address for machine "
62 "%s.\n", remote_machine
) == -1) {
65 return NT_STATUS_UNSUCCESSFUL
;
68 cli
= cli_initialise();
70 return NT_STATUS_NO_MEMORY
;
73 result
= cli_connect(cli
, remote_machine
, &ss
);
74 if (!NT_STATUS_IS_OK(result
)) {
75 if (asprintf(err_str
, "Unable to connect to SMB server on "
76 "machine %s. Error was : %s.\n",
77 remote_machine
, nt_errstr(result
))==-1) {
84 make_nmb_name(&calling
, global_myname() , 0x0);
85 make_nmb_name(&called
, remote_machine
, 0x20);
87 if (!cli_session_request(cli
, &calling
, &called
)) {
88 if (asprintf(err_str
, "machine %s rejected the session setup. "
90 remote_machine
, cli_errstr(cli
)) == -1) {
93 result
= cli_nt_error(cli
);
98 cli
->protocol
= PROTOCOL_NT1
;
100 result
= cli_negprot(cli
);
102 if (!NT_STATUS_IS_OK(result
)) {
103 if (asprintf(err_str
, "machine %s rejected the negotiate "
104 "protocol. Error was : %s.\n",
105 remote_machine
, nt_errstr(result
)) == -1) {
108 result
= cli_nt_error(cli
);
113 /* Given things like SMB signing, restrict anonymous and the like,
114 try an authenticated connection first */
115 result
= cli_session_setup(cli
, user_name
,
116 old_passwd
, strlen(old_passwd
)+1,
117 old_passwd
, strlen(old_passwd
)+1, "");
119 if (!NT_STATUS_IS_OK(result
)) {
121 /* Password must change or Password expired are the only valid
122 * error conditions here from where we can proceed, the rest like
123 * account locked out or logon failure will lead to errors later
126 if (!NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_MUST_CHANGE
) &&
127 !NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_EXPIRED
)) {
128 if (asprintf(err_str
, "Could not connect to machine %s: "
129 "%s\n", remote_machine
, cli_errstr(cli
)) == -1) {
136 pass_must_change
= True
;
139 * We should connect as the anonymous user here, in case
140 * the server has "must change password" checked...
141 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
144 result
= cli_session_setup(cli
, "", "", 0, "", 0, "");
146 if (!NT_STATUS_IS_OK(result
)) {
147 if (asprintf(err_str
, "machine %s rejected the session "
148 "setup. Error was : %s.\n",
149 remote_machine
, cli_errstr(cli
)) == -1) {
156 result
= cli_init_creds(cli
, "", "", NULL
);
157 if (!NT_STATUS_IS_OK(result
)) {
162 result
= cli_init_creds(cli
, user
, domain
, old_passwd
);
163 if (!NT_STATUS_IS_OK(result
)) {
169 result
= cli_tcon_andx(cli
, "IPC$", "IPC", "", 1);
170 if (!NT_STATUS_IS_OK(result
)) {
171 if (asprintf(err_str
, "machine %s rejected the tconX on the "
172 "IPC$ share. Error was : %s.\n",
173 remote_machine
, nt_errstr(result
))) {
180 /* Try not to give the password away too easily */
182 if (!pass_must_change
) {
183 result
= cli_rpc_pipe_open_ntlmssp(cli
,
184 &ndr_table_samr
.syntax_id
,
186 DCERPC_AUTH_LEVEL_PRIVACY
,
192 * If the user password must be changed the ntlmssp bind will
193 * fail the same way as the session setup above did. The
194 * difference ist that with a pipe bind we don't get a good
195 * error message, the result will be that the rpc call below
196 * will just fail. So we do it anonymously, there's no other
199 result
= cli_rpc_pipe_open_noauth(
200 cli
, &ndr_table_samr
.syntax_id
, &pipe_hnd
);
203 if (!NT_STATUS_IS_OK(result
)) {
204 if (lp_client_lanman_auth()) {
205 /* Use the old RAP method. */
206 if (!cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
207 if (asprintf(err_str
, "machine %s rejected the "
208 "password change: Error was : %s.\n",
209 remote_machine
, cli_errstr(cli
)) == -1) {
212 result
= cli_nt_error(cli
);
217 if (asprintf(err_str
, "SAMR connection to machine %s "
218 "failed. Error was %s, but LANMAN password "
219 "changes are disabled\n",
220 remote_machine
, nt_errstr(result
)) == -1) {
223 result
= cli_nt_error(cli
);
229 result
= rpccli_samr_chgpasswd_user2(pipe_hnd
, talloc_tos(),
230 user_name
, new_passwd
, old_passwd
);
231 if (NT_STATUS_IS_OK(result
)) {
232 /* Great - it all worked! */
236 } else if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
237 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
238 /* it failed, but for reasons such as wrong password, too short etc ... */
240 if (asprintf(err_str
, "machine %s rejected the password change: "
242 remote_machine
, get_friendly_nt_error_msg(result
)) == -1) {
249 /* OK, that failed, so try again... */
250 TALLOC_FREE(pipe_hnd
);
252 /* Try anonymous NTLMSSP... */
253 result
= cli_init_creds(cli
, "", "", NULL
);
254 if (!NT_STATUS_IS_OK(result
)) {
259 result
= NT_STATUS_UNSUCCESSFUL
;
261 /* OK, this is ugly, but... try an anonymous pipe. */
262 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
265 if ( NT_STATUS_IS_OK(result
) &&
266 (NT_STATUS_IS_OK(result
= rpccli_samr_chgpasswd_user2(
267 pipe_hnd
, talloc_tos(), user_name
,
268 new_passwd
, old_passwd
)))) {
269 /* Great - it all worked! */
273 if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
274 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
275 /* it failed, but again it was due to things like new password too short */
277 if (asprintf(err_str
, "machine %s rejected the "
278 "(anonymous) password change: Error was : "
279 "%s.\n", remote_machine
,
280 get_friendly_nt_error_msg(result
)) == -1) {
287 /* We have failed to change the user's password, and we think the server
288 just might not support SAMR password changes, so fall back */
290 if (lp_client_lanman_auth()) {
291 /* Use the old RAP method. */
292 if (cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
293 /* SAMR failed, but the old LanMan protocol worked! */
298 if (asprintf(err_str
, "machine %s rejected the password "
299 "change: Error was : %s.\n",
300 remote_machine
, cli_errstr(cli
)) == -1) {
303 result
= cli_nt_error(cli
);
307 if (asprintf(err_str
, "SAMR connection to machine %s "
308 "failed. Error was %s, but LANMAN password "
309 "changes are disabled\n",
310 nt_errstr(result
), remote_machine
) == -1) {
314 return NT_STATUS_UNSUCCESSFUL
;