CVE-2020-10730: selftest: Add test to confirm VLV interaction with ASQ
[Samba.git] / source3 / libsmb / passchange.c
blobf60e307997574303dc20dcaae3ed3fe90c37eb2e
1 /*
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/>.
20 #include "includes.h"
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"
27 #include "../libcli/smb/smbXcli_base.h"
29 /*************************************************************
30 Change a password on a remote machine using IPC calls.
31 *************************************************************/
33 NTSTATUS remote_password_change(const char *remote_machine,
34 const char *domain, const char *user_name,
35 const char *old_passwd, const char *new_passwd,
36 char **err_str)
38 struct cli_state *cli = NULL;
39 struct cli_credentials *creds = NULL;
40 struct rpc_pipe_client *pipe_hnd = NULL;
41 NTSTATUS result;
42 bool pass_must_change = False;
44 *err_str = NULL;
46 result = cli_connect_nb(remote_machine, NULL, 0, 0x20, NULL,
47 SMB_SIGNING_IPC_DEFAULT, 0, &cli);
48 if (!NT_STATUS_IS_OK(result)) {
49 if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED)) {
50 if (asprintf(err_str, "Unable to connect to SMB server on "
51 "machine %s. NetBIOS support disabled\n",
52 remote_machine) == -1) {
53 *err_str = NULL;
55 } else {
56 if (asprintf(err_str, "Unable to connect to SMB server on "
57 "machine %s. Error was : %s.\n",
58 remote_machine, nt_errstr(result))==-1) {
59 *err_str = NULL;
62 return result;
65 creds = cli_session_creds_init(cli,
66 user_name,
67 domain,
68 NULL, /* realm */
69 old_passwd,
70 false, /* use_kerberos */
71 false, /* fallback_after_kerberos */
72 false, /* use_ccache */
73 false); /* password_is_nt_hash */
74 SMB_ASSERT(creds != NULL);
76 result = smbXcli_negprot(cli->conn, cli->timeout,
77 lp_client_ipc_min_protocol(),
78 lp_client_ipc_max_protocol());
80 if (!NT_STATUS_IS_OK(result)) {
81 if (asprintf(err_str, "machine %s rejected the negotiate "
82 "protocol. Error was : %s.\n",
83 remote_machine, nt_errstr(result)) == -1) {
84 *err_str = NULL;
86 cli_shutdown(cli);
87 return result;
90 /* Given things like SMB signing, restrict anonymous and the like,
91 try an authenticated connection first */
92 result = cli_session_setup_creds(cli, creds);
94 if (!NT_STATUS_IS_OK(result)) {
96 /* Password must change or Password expired are the only valid
97 * error conditions here from where we can proceed, the rest like
98 * account locked out or logon failure will lead to errors later
99 * anyway */
101 if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
102 !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
103 if (asprintf(err_str, "Could not connect to machine %s: "
104 "%s\n", remote_machine, nt_errstr(result)) == -1) {
105 *err_str = NULL;
107 cli_shutdown(cli);
108 return result;
111 pass_must_change = True;
114 * We should connect as the anonymous user here, in case
115 * the server has "must change password" checked...
116 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
119 result = cli_session_setup_anon(cli);
121 if (!NT_STATUS_IS_OK(result)) {
122 if (asprintf(err_str, "machine %s rejected the session "
123 "setup. Error was : %s.\n",
124 remote_machine, nt_errstr(result)) == -1) {
125 *err_str = NULL;
127 cli_shutdown(cli);
128 return result;
132 result = cli_tree_connect(cli, "IPC$", "IPC", NULL);
133 if (!NT_STATUS_IS_OK(result)) {
134 if (asprintf(err_str, "machine %s rejected the tconX on the "
135 "IPC$ share. Error was : %s.\n",
136 remote_machine, nt_errstr(result))) {
137 *err_str = NULL;
139 cli_shutdown(cli);
140 return result;
143 /* Try not to give the password away too easily */
145 if (!pass_must_change) {
146 result = cli_rpc_pipe_open_with_creds(cli,
147 &ndr_table_samr,
148 NCACN_NP,
149 DCERPC_AUTH_TYPE_NTLMSSP,
150 DCERPC_AUTH_LEVEL_PRIVACY,
151 remote_machine,
152 creds,
153 &pipe_hnd);
154 } else {
156 * If the user password must be changed the ntlmssp bind will
157 * fail the same way as the session setup above did. The
158 * difference ist that with a pipe bind we don't get a good
159 * error message, the result will be that the rpc call below
160 * will just fail. So we do it anonymously, there's no other
161 * way.
163 result = cli_rpc_pipe_open_noauth(
164 cli, &ndr_table_samr, &pipe_hnd);
167 if (!NT_STATUS_IS_OK(result)) {
168 if (lp_client_lanman_auth()) {
169 /* Use the old RAP method. */
170 if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
171 result = cli_nt_error(cli);
172 if (asprintf(err_str, "machine %s rejected the "
173 "password change: Error was : %s.\n",
174 remote_machine, nt_errstr(result)) == -1) {
175 *err_str = NULL;
177 cli_shutdown(cli);
178 return result;
180 } else {
181 if (asprintf(err_str, "SAMR connection to machine %s "
182 "failed. Error was %s, but LANMAN password "
183 "changes are disabled\n",
184 remote_machine, nt_errstr(result)) == -1) {
185 *err_str = NULL;
187 cli_shutdown(cli);
188 return result;
192 result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
193 user_name, new_passwd, old_passwd);
194 if (NT_STATUS_IS_OK(result)) {
195 /* Great - it all worked! */
196 cli_shutdown(cli);
197 return NT_STATUS_OK;
199 } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
200 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
201 /* it failed, but for reasons such as wrong password, too short etc ... */
203 if (asprintf(err_str, "machine %s rejected the password change: "
204 "Error was : %s.\n",
205 remote_machine, get_friendly_nt_error_msg(result)) == -1) {
206 *err_str = NULL;
208 cli_shutdown(cli);
209 return result;
212 /* OK, that failed, so try again... */
213 TALLOC_FREE(pipe_hnd);
215 /* Try anonymous NTLMSSP... */
216 result = NT_STATUS_UNSUCCESSFUL;
218 /* OK, this is ugly, but... try an anonymous pipe. */
219 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
220 &pipe_hnd);
222 if ( NT_STATUS_IS_OK(result) &&
223 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
224 pipe_hnd, talloc_tos(), user_name,
225 new_passwd, old_passwd)))) {
226 /* Great - it all worked! */
227 cli_shutdown(cli);
228 return NT_STATUS_OK;
229 } else {
230 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
231 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
232 /* it failed, but again it was due to things like new password too short */
234 if (asprintf(err_str, "machine %s rejected the "
235 "(anonymous) password change: Error was : "
236 "%s.\n", remote_machine,
237 get_friendly_nt_error_msg(result)) == -1) {
238 *err_str = NULL;
240 cli_shutdown(cli);
241 return result;
244 /* We have failed to change the user's password, and we think the server
245 just might not support SAMR password changes, so fall back */
247 if (lp_client_lanman_auth()) {
248 /* Use the old RAP method. */
249 if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
250 /* SAMR failed, but the old LanMan protocol worked! */
252 cli_shutdown(cli);
253 return NT_STATUS_OK;
256 result = cli_nt_error(cli);
257 if (asprintf(err_str, "machine %s rejected the password "
258 "change: Error was : %s.\n",
259 remote_machine, nt_errstr(result)) == -1) {
260 *err_str = NULL;
262 cli_shutdown(cli);
263 return result;
264 } else {
265 if (asprintf(err_str, "SAMR connection to machine %s "
266 "failed. Error was %s, but LANMAN password "
267 "changes are disabled\n",
268 remote_machine, nt_errstr(result)) == -1) {
269 *err_str = NULL;
271 cli_shutdown(cli);
272 return NT_STATUS_UNSUCCESSFUL;