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/clirap.h"
25 #include "libsmb/nmblib.h"
27 /*************************************************************
28 Change a password on a remote machine using IPC calls.
29 *************************************************************/
31 NTSTATUS remote_password_change(const char *remote_machine, const char *user_name,
32 const char *old_passwd, const char *new_passwd,
35 struct nmb_name calling, called;
36 struct cli_state *cli = NULL;
37 struct rpc_pipe_client *pipe_hnd = NULL;
38 struct sockaddr_storage ss;
39 char *user, *domain, *p;
42 bool pass_must_change = False;
44 user = talloc_strdup(talloc_tos(), user_name);
45 SMB_ASSERT(user != NULL);
46 domain = talloc_strdup(talloc_tos(), "");
47 SMB_ASSERT(domain != NULL);
49 /* allow usernames of the form domain\\user or domain/user */
50 if ((p = strchr_m(user,'\\')) || (p = strchr_m(user,'/')) ||
51 (p = strchr_m(user,*lp_winbind_separator()))) {
59 if(!resolve_name( remote_machine, &ss, 0x20, false)) {
60 if (asprintf(err_str, "Unable to find an IP address for machine "
61 "%s.\n", remote_machine) == -1) {
64 return NT_STATUS_UNSUCCESSFUL;
67 cli = cli_initialise();
69 return NT_STATUS_NO_MEMORY;
72 result = cli_connect(cli, remote_machine, &ss);
73 if (!NT_STATUS_IS_OK(result)) {
74 if (asprintf(err_str, "Unable to connect to SMB server on "
75 "machine %s. Error was : %s.\n",
76 remote_machine, nt_errstr(result))==-1) {
83 make_nmb_name(&calling, global_myname() , 0x0);
84 make_nmb_name(&called , remote_machine, 0x20);
86 if (!cli_session_request(cli, &calling, &called)) {
87 if (asprintf(err_str, "machine %s rejected the session setup. "
89 remote_machine, cli_errstr(cli)) == -1) {
92 result = cli_nt_error(cli);
97 cli->protocol = PROTOCOL_NT1;
99 result = cli_negprot(cli);
101 if (!NT_STATUS_IS_OK(result)) {
102 if (asprintf(err_str, "machine %s rejected the negotiate "
103 "protocol. Error was : %s.\n",
104 remote_machine, nt_errstr(result)) == -1) {
107 result = cli_nt_error(cli);
112 /* Given things like SMB signing, restrict anonymous and the like,
113 try an authenticated connection first */
114 result = cli_session_setup(cli, user_name,
115 old_passwd, strlen(old_passwd)+1,
116 old_passwd, strlen(old_passwd)+1, "");
118 if (!NT_STATUS_IS_OK(result)) {
120 /* Password must change or Password expired are the only valid
121 * error conditions here from where we can proceed, the rest like
122 * account locked out or logon failure will lead to errors later
125 if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
126 !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
127 if (asprintf(err_str, "Could not connect to machine %s: "
128 "%s\n", remote_machine, cli_errstr(cli)) == -1) {
135 pass_must_change = True;
138 * We should connect as the anonymous user here, in case
139 * the server has "must change password" checked...
140 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
143 result = cli_session_setup(cli, "", "", 0, "", 0, "");
145 if (!NT_STATUS_IS_OK(result)) {
146 if (asprintf(err_str, "machine %s rejected the session "
147 "setup. Error was : %s.\n",
148 remote_machine, cli_errstr(cli)) == -1) {
155 result = cli_init_creds(cli, "", "", NULL);
156 if (!NT_STATUS_IS_OK(result)) {
161 result = cli_init_creds(cli, user, domain, old_passwd);
162 if (!NT_STATUS_IS_OK(result)) {
168 result = cli_tcon_andx(cli, "IPC$", "IPC", "", 1);
169 if (!NT_STATUS_IS_OK(result)) {
170 if (asprintf(err_str, "machine %s rejected the tconX on the "
171 "IPC$ share. Error was : %s.\n",
172 remote_machine, nt_errstr(result))) {
179 /* Try not to give the password away too easily */
181 if (!pass_must_change) {
182 result = cli_rpc_pipe_open_ntlmssp(cli,
183 &ndr_table_samr.syntax_id,
185 DCERPC_AUTH_LEVEL_PRIVACY,
191 * If the user password must be changed the ntlmssp bind will
192 * fail the same way as the session setup above did. The
193 * difference ist that with a pipe bind we don't get a good
194 * error message, the result will be that the rpc call below
195 * will just fail. So we do it anonymously, there's no other
198 result = cli_rpc_pipe_open_noauth(
199 cli, &ndr_table_samr.syntax_id, &pipe_hnd);
202 if (!NT_STATUS_IS_OK(result)) {
203 if (lp_client_lanman_auth()) {
204 /* Use the old RAP method. */
205 if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
206 if (asprintf(err_str, "machine %s rejected the "
207 "password change: Error was : %s.\n",
208 remote_machine, cli_errstr(cli)) == -1) {
211 result = cli_nt_error(cli);
216 if (asprintf(err_str, "SAMR connection to machine %s "
217 "failed. Error was %s, but LANMAN password "
218 "changes are disabled\n",
219 remote_machine, nt_errstr(result)) == -1) {
222 result = cli_nt_error(cli);
228 result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
229 user_name, new_passwd, old_passwd);
230 if (NT_STATUS_IS_OK(result)) {
231 /* Great - it all worked! */
235 } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
236 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
237 /* it failed, but for reasons such as wrong password, too short etc ... */
239 if (asprintf(err_str, "machine %s rejected the password change: "
241 remote_machine, get_friendly_nt_error_msg(result)) == -1) {
248 /* OK, that failed, so try again... */
249 TALLOC_FREE(pipe_hnd);
251 /* Try anonymous NTLMSSP... */
252 result = cli_init_creds(cli, "", "", NULL);
253 if (!NT_STATUS_IS_OK(result)) {
258 result = NT_STATUS_UNSUCCESSFUL;
260 /* OK, this is ugly, but... try an anonymous pipe. */
261 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
264 if ( NT_STATUS_IS_OK(result) &&
265 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
266 pipe_hnd, talloc_tos(), user_name,
267 new_passwd, old_passwd)))) {
268 /* Great - it all worked! */
272 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
273 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
274 /* it failed, but again it was due to things like new password too short */
276 if (asprintf(err_str, "machine %s rejected the "
277 "(anonymous) password change: Error was : "
278 "%s.\n", remote_machine,
279 get_friendly_nt_error_msg(result)) == -1) {
286 /* We have failed to change the user's password, and we think the server
287 just might not support SAMR password changes, so fall back */
289 if (lp_client_lanman_auth()) {
290 /* Use the old RAP method. */
291 if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
292 /* SAMR failed, but the old LanMan protocol worked! */
297 if (asprintf(err_str, "machine %s rejected the password "
298 "change: Error was : %s.\n",
299 remote_machine, cli_errstr(cli)) == -1) {
302 result = cli_nt_error(cli);
306 if (asprintf(err_str, "SAMR connection to machine %s "
307 "failed. Error was %s, but LANMAN password "
308 "changes are disabled\n",
309 nt_errstr(result), remote_machine) == -1) {
313 return NT_STATUS_UNSUCCESSFUL;