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 cli_state
*cli
= NULL
;
37 struct rpc_pipe_client
*pipe_hnd
= NULL
;
38 char *user
, *domain
, *p
;
41 bool pass_must_change
= False
;
43 user
= talloc_strdup(talloc_tos(), user_name
);
44 SMB_ASSERT(user
!= NULL
);
45 domain
= talloc_strdup(talloc_tos(), "");
46 SMB_ASSERT(domain
!= NULL
);
48 /* allow usernames of the form domain\\user or domain/user */
49 if ((p
= strchr_m(user
,'\\')) || (p
= strchr_m(user
,'/')) ||
50 (p
= strchr_m(user
,*lp_winbind_separator()))) {
58 result
= cli_connect_nb(remote_machine
, NULL
, 0, 0x20, NULL
,
60 if (!NT_STATUS_IS_OK(result
)) {
61 if (asprintf(err_str
, "Unable to connect to SMB server on "
62 "machine %s. Error was : %s.\n",
63 remote_machine
, nt_errstr(result
))==-1) {
69 result
= cli_negprot(cli
, PROTOCOL_NT1
);
71 if (!NT_STATUS_IS_OK(result
)) {
72 if (asprintf(err_str
, "machine %s rejected the negotiate "
73 "protocol. Error was : %s.\n",
74 remote_machine
, nt_errstr(result
)) == -1) {
77 result
= cli_nt_error(cli
);
82 /* Given things like SMB signing, restrict anonymous and the like,
83 try an authenticated connection first */
84 result
= cli_session_setup(cli
, user_name
,
85 old_passwd
, strlen(old_passwd
)+1,
86 old_passwd
, strlen(old_passwd
)+1, "");
88 if (!NT_STATUS_IS_OK(result
)) {
90 /* Password must change or Password expired are the only valid
91 * error conditions here from where we can proceed, the rest like
92 * account locked out or logon failure will lead to errors later
95 if (!NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_MUST_CHANGE
) &&
96 !NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_EXPIRED
)) {
97 if (asprintf(err_str
, "Could not connect to machine %s: "
98 "%s\n", remote_machine
, nt_errstr(result
)) == -1) {
105 pass_must_change
= True
;
108 * We should connect as the anonymous user here, in case
109 * the server has "must change password" checked...
110 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
113 result
= cli_session_setup(cli
, "", "", 0, "", 0, "");
115 if (!NT_STATUS_IS_OK(result
)) {
116 if (asprintf(err_str
, "machine %s rejected the session "
117 "setup. Error was : %s.\n",
118 remote_machine
, nt_errstr(result
)) == -1) {
125 result
= cli_init_creds(cli
, "", "", NULL
);
126 if (!NT_STATUS_IS_OK(result
)) {
131 result
= cli_init_creds(cli
, user
, domain
, old_passwd
);
132 if (!NT_STATUS_IS_OK(result
)) {
138 result
= cli_tcon_andx(cli
, "IPC$", "IPC", "", 1);
139 if (!NT_STATUS_IS_OK(result
)) {
140 if (asprintf(err_str
, "machine %s rejected the tconX on the "
141 "IPC$ share. Error was : %s.\n",
142 remote_machine
, nt_errstr(result
))) {
149 /* Try not to give the password away too easily */
151 if (!pass_must_change
) {
152 result
= cli_rpc_pipe_open_ntlmssp(cli
,
153 &ndr_table_samr
.syntax_id
,
155 DCERPC_AUTH_LEVEL_PRIVACY
,
161 * If the user password must be changed the ntlmssp bind will
162 * fail the same way as the session setup above did. The
163 * difference ist that with a pipe bind we don't get a good
164 * error message, the result will be that the rpc call below
165 * will just fail. So we do it anonymously, there's no other
168 result
= cli_rpc_pipe_open_noauth(
169 cli
, &ndr_table_samr
.syntax_id
, &pipe_hnd
);
172 if (!NT_STATUS_IS_OK(result
)) {
173 if (lp_client_lanman_auth()) {
174 /* Use the old RAP method. */
175 if (!cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
176 result
= cli_nt_error(cli
);
177 if (asprintf(err_str
, "machine %s rejected the "
178 "password change: Error was : %s.\n",
179 remote_machine
, nt_errstr(result
)) == -1) {
186 if (asprintf(err_str
, "SAMR connection to machine %s "
187 "failed. Error was %s, but LANMAN password "
188 "changes are disabled\n",
189 remote_machine
, nt_errstr(result
)) == -1) {
192 result
= cli_nt_error(cli
);
198 result
= rpccli_samr_chgpasswd_user2(pipe_hnd
, talloc_tos(),
199 user_name
, new_passwd
, old_passwd
);
200 if (NT_STATUS_IS_OK(result
)) {
201 /* Great - it all worked! */
205 } else if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
206 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
207 /* it failed, but for reasons such as wrong password, too short etc ... */
209 if (asprintf(err_str
, "machine %s rejected the password change: "
211 remote_machine
, get_friendly_nt_error_msg(result
)) == -1) {
218 /* OK, that failed, so try again... */
219 TALLOC_FREE(pipe_hnd
);
221 /* Try anonymous NTLMSSP... */
222 result
= cli_init_creds(cli
, "", "", NULL
);
223 if (!NT_STATUS_IS_OK(result
)) {
228 result
= NT_STATUS_UNSUCCESSFUL
;
230 /* OK, this is ugly, but... try an anonymous pipe. */
231 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
234 if ( NT_STATUS_IS_OK(result
) &&
235 (NT_STATUS_IS_OK(result
= rpccli_samr_chgpasswd_user2(
236 pipe_hnd
, talloc_tos(), user_name
,
237 new_passwd
, old_passwd
)))) {
238 /* Great - it all worked! */
242 if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
243 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
244 /* it failed, but again it was due to things like new password too short */
246 if (asprintf(err_str
, "machine %s rejected the "
247 "(anonymous) password change: Error was : "
248 "%s.\n", remote_machine
,
249 get_friendly_nt_error_msg(result
)) == -1) {
256 /* We have failed to change the user's password, and we think the server
257 just might not support SAMR password changes, so fall back */
259 if (lp_client_lanman_auth()) {
260 /* Use the old RAP method. */
261 if (cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
262 /* SAMR failed, but the old LanMan protocol worked! */
268 result
= cli_nt_error(cli
);
269 if (asprintf(err_str
, "machine %s rejected the password "
270 "change: Error was : %s.\n",
271 remote_machine
, nt_errstr(result
)) == -1) {
277 if (asprintf(err_str
, "SAMR connection to machine %s "
278 "failed. Error was %s, but LANMAN password "
279 "changes are disabled\n",
280 nt_errstr(result
), remote_machine
) == -1) {
284 return NT_STATUS_UNSUCCESSFUL
;