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/>.
22 /*************************************************************
23 Change a password on a remote machine using IPC calls.
24 *************************************************************/
26 NTSTATUS
remote_password_change(const char *remote_machine
, const char *user_name
,
27 const char *old_passwd
, const char *new_passwd
,
30 struct nmb_name calling
, called
;
31 struct cli_state
*cli
;
32 struct rpc_pipe_client
*pipe_hnd
;
33 struct sockaddr_storage ss
;
34 char *user
, *domain
, *p
;
37 bool pass_must_change
= False
;
39 user
= talloc_strdup(talloc_tos(), user_name
);
40 SMB_ASSERT(user
!= NULL
);
41 domain
= talloc_strdup(talloc_tos(), "");
42 SMB_ASSERT(domain
!= NULL
);
44 /* allow usernames of the form domain\\user or domain/user */
45 if ((p
= strchr_m(user
,'\\')) || (p
= strchr_m(user
,'/')) ||
46 (p
= strchr_m(user
,*lp_winbind_separator()))) {
54 if(!resolve_name( remote_machine
, &ss
, 0x20)) {
55 if (asprintf(err_str
, "Unable to find an IP address for machine "
56 "%s.\n", remote_machine
) == -1) {
59 return NT_STATUS_UNSUCCESSFUL
;
62 cli
= cli_initialise();
64 return NT_STATUS_NO_MEMORY
;
67 result
= cli_connect(cli
, remote_machine
, &ss
);
68 if (!NT_STATUS_IS_OK(result
)) {
69 if (asprintf(err_str
, "Unable to connect to SMB server on "
70 "machine %s. Error was : %s.\n",
71 remote_machine
, nt_errstr(result
))==-1) {
78 make_nmb_name(&calling
, global_myname() , 0x0);
79 make_nmb_name(&called
, remote_machine
, 0x20);
81 if (!cli_session_request(cli
, &calling
, &called
)) {
82 if (asprintf(err_str
, "machine %s rejected the session setup. "
84 remote_machine
, cli_errstr(cli
)) == -1) {
87 result
= cli_nt_error(cli
);
92 cli
->protocol
= PROTOCOL_NT1
;
94 result
= cli_negprot(cli
);
96 if (!NT_STATUS_IS_OK(result
)) {
97 if (asprintf(err_str
, "machine %s rejected the negotiate "
98 "protocol. Error was : %s.\n",
99 remote_machine
, nt_errstr(result
)) == -1) {
102 result
= cli_nt_error(cli
);
107 /* Given things like SMB signing, restrict anonymous and the like,
108 try an authenticated connection first */
109 result
= cli_session_setup(cli
, user_name
,
110 old_passwd
, strlen(old_passwd
)+1,
111 old_passwd
, strlen(old_passwd
)+1, "");
113 if (!NT_STATUS_IS_OK(result
)) {
115 /* Password must change or Password expired are the only valid
116 * error conditions here from where we can proceed, the rest like
117 * account locked out or logon failure will lead to errors later
120 if (!NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_MUST_CHANGE
) &&
121 !NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_EXPIRED
)) {
122 if (asprintf(err_str
, "Could not connect to machine %s: "
123 "%s\n", remote_machine
, cli_errstr(cli
)) == -1) {
130 pass_must_change
= True
;
133 * We should connect as the anonymous user here, in case
134 * the server has "must change password" checked...
135 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
138 result
= cli_session_setup(cli
, "", "", 0, "", 0, "");
140 if (!NT_STATUS_IS_OK(result
)) {
141 if (asprintf(err_str
, "machine %s rejected the session "
142 "setup. Error was : %s.\n",
143 remote_machine
, cli_errstr(cli
)) == -1) {
150 result
= cli_init_creds(cli
, "", "", NULL
);
151 if (!NT_STATUS_IS_OK(result
)) {
156 result
= cli_init_creds(cli
, user
, domain
, old_passwd
);
157 if (!NT_STATUS_IS_OK(result
)) {
163 result
= cli_tcon_andx(cli
, "IPC$", "IPC", "", 1);
164 if (!NT_STATUS_IS_OK(result
)) {
165 if (asprintf(err_str
, "machine %s rejected the tconX on the "
166 "IPC$ share. Error was : %s.\n",
167 remote_machine
, nt_errstr(result
))) {
174 /* Try not to give the password away too easily */
176 if (!pass_must_change
) {
177 result
= cli_rpc_pipe_open_ntlmssp(cli
,
178 &ndr_table_samr
.syntax_id
,
179 PIPE_AUTH_LEVEL_PRIVACY
,
185 * If the user password must be changed the ntlmssp bind will
186 * fail the same way as the session setup above did. The
187 * difference ist that with a pipe bind we don't get a good
188 * error message, the result will be that the rpc call below
189 * will just fail. So we do it anonymously, there's no other
192 result
= cli_rpc_pipe_open_noauth(
193 cli
, &ndr_table_samr
.syntax_id
, &pipe_hnd
);
196 if (!NT_STATUS_IS_OK(result
)) {
197 if (lp_client_lanman_auth()) {
198 /* Use the old RAP method. */
199 if (!cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
200 if (asprintf(err_str
, "machine %s rejected the "
201 "password change: Error was : %s.\n",
202 remote_machine
, cli_errstr(cli
)) == -1) {
205 result
= cli_nt_error(cli
);
210 if (asprintf(err_str
, "SAMR connection to machine %s "
211 "failed. Error was %s, but LANMAN password "
212 "changes are disabled\n",
213 remote_machine
, nt_errstr(result
)) == -1) {
216 result
= cli_nt_error(cli
);
222 result
= rpccli_samr_chgpasswd_user2(pipe_hnd
, talloc_tos(),
223 user_name
, new_passwd
, old_passwd
);
224 if (NT_STATUS_IS_OK(result
)) {
225 /* Great - it all worked! */
229 } else if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
230 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
231 /* it failed, but for reasons such as wrong password, too short etc ... */
233 if (asprintf(err_str
, "machine %s rejected the password change: "
235 remote_machine
, get_friendly_nt_error_msg(result
)) == -1) {
242 /* OK, that failed, so try again... */
243 TALLOC_FREE(pipe_hnd
);
245 /* Try anonymous NTLMSSP... */
246 result
= cli_init_creds(cli
, "", "", NULL
);
247 if (!NT_STATUS_IS_OK(result
)) {
252 result
= NT_STATUS_UNSUCCESSFUL
;
254 /* OK, this is ugly, but... try an anonymous pipe. */
255 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
258 if ( NT_STATUS_IS_OK(result
) &&
259 (NT_STATUS_IS_OK(result
= rpccli_samr_chgpasswd_user2(
260 pipe_hnd
, talloc_tos(), user_name
,
261 new_passwd
, old_passwd
)))) {
262 /* Great - it all worked! */
266 if (!(NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)
267 || NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
))) {
268 /* it failed, but again it was due to things like new password too short */
270 if (asprintf(err_str
, "machine %s rejected the "
271 "(anonymous) password change: Error was : "
272 "%s.\n", remote_machine
,
273 get_friendly_nt_error_msg(result
)) == -1) {
280 /* We have failed to change the user's password, and we think the server
281 just might not support SAMR password changes, so fall back */
283 if (lp_client_lanman_auth()) {
284 /* Use the old RAP method. */
285 if (cli_oem_change_password(cli
, user_name
, new_passwd
, old_passwd
)) {
286 /* SAMR failed, but the old LanMan protocol worked! */
291 if (asprintf(err_str
, "machine %s rejected the password "
292 "change: Error was : %s.\n",
293 remote_machine
, cli_errstr(cli
)) == -1) {
296 result
= cli_nt_error(cli
);
300 if (asprintf(err_str
, "SAMR connection to machine %s "
301 "failed. Error was %s, but LANMAN password "
302 "changed are disabled\n",
303 nt_errstr(result
), remote_machine
) == -1) {
307 return NT_STATUS_UNSUCCESSFUL
;