tests: Check symlinks are readable as reparse points
[samba.git] / source3 / libsmb / passchange.c
blobe1b4a9824551309284c6a55c1a02bc3c259024c0
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 status;
42 NTSTATUS result;
43 bool pass_must_change = False;
45 *err_str = NULL;
47 result = cli_connect_nb(talloc_tos(),
48 remote_machine,
49 NULL,
51 0x20,
52 NULL,
53 SMB_SIGNING_IPC_DEFAULT,
55 &cli);
56 if (!NT_STATUS_IS_OK(result)) {
57 if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED)) {
58 if (asprintf(err_str, "Unable to connect to SMB server on "
59 "machine %s. NetBIOS support disabled\n",
60 remote_machine) == -1) {
61 *err_str = NULL;
63 } else {
64 if (asprintf(err_str, "Unable to connect to SMB server on "
65 "machine %s. Error was : %s.\n",
66 remote_machine, nt_errstr(result))==-1) {
67 *err_str = NULL;
70 return result;
73 creds = cli_session_creds_init(cli,
74 user_name,
75 domain,
76 NULL, /* realm */
77 old_passwd,
78 false, /* use_kerberos */
79 false, /* fallback_after_kerberos */
80 false, /* use_ccache */
81 false); /* password_is_nt_hash */
82 SMB_ASSERT(creds != NULL);
84 result = smbXcli_negprot(cli->conn,
85 cli->timeout,
86 lp_client_ipc_min_protocol(),
87 lp_client_ipc_max_protocol(),
88 NULL,
89 NULL,
90 NULL);
92 if (!NT_STATUS_IS_OK(result)) {
93 if (asprintf(err_str, "machine %s rejected the negotiate "
94 "protocol. Error was : %s.\n",
95 remote_machine, nt_errstr(result)) == -1) {
96 *err_str = NULL;
98 cli_shutdown(cli);
99 return result;
102 /* Given things like SMB signing, restrict anonymous and the like,
103 try an authenticated connection first */
104 result = cli_session_setup_creds(cli, creds);
106 if (!NT_STATUS_IS_OK(result)) {
108 /* Password must change or Password expired are the only valid
109 * error conditions here from where we can proceed, the rest like
110 * account locked out or logon failure will lead to errors later
111 * anyway */
113 if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
114 !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
115 if (asprintf(err_str, "Could not connect to machine %s: "
116 "%s\n", remote_machine, nt_errstr(result)) == -1) {
117 *err_str = NULL;
119 cli_shutdown(cli);
120 return result;
123 pass_must_change = True;
126 * We should connect as the anonymous user here, in case
127 * the server has "must change password" checked...
128 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
131 result = cli_session_setup_anon(cli);
133 if (!NT_STATUS_IS_OK(result)) {
134 if (asprintf(err_str, "machine %s rejected the session "
135 "setup. Error was : %s.\n",
136 remote_machine, nt_errstr(result)) == -1) {
137 *err_str = NULL;
139 cli_shutdown(cli);
140 return result;
144 result = cli_tree_connect(cli, "IPC$", "IPC", NULL);
145 if (!NT_STATUS_IS_OK(result)) {
146 if (asprintf(err_str, "machine %s rejected the tconX on the "
147 "IPC$ share. Error was : %s.\n",
148 remote_machine, nt_errstr(result))) {
149 *err_str = NULL;
151 cli_shutdown(cli);
152 return result;
155 /* Try not to give the password away too easily */
157 if (!pass_must_change) {
158 const struct sockaddr_storage *remote_sockaddr =
159 smbXcli_conn_remote_sockaddr(cli->conn);
161 result = cli_rpc_pipe_open_with_creds(cli,
162 &ndr_table_samr,
163 NCACN_NP,
164 DCERPC_AUTH_TYPE_NTLMSSP,
165 DCERPC_AUTH_LEVEL_PRIVACY,
166 remote_machine,
167 remote_sockaddr,
168 creds,
169 &pipe_hnd);
170 } else {
172 * If the user password must be changed the ntlmssp bind will
173 * fail the same way as the session setup above did. The
174 * difference is that with a pipe bind we don't get a good
175 * error message, the result will be that the rpc call below
176 * will just fail. So we do it anonymously, there's no other
177 * way.
179 result = cli_rpc_pipe_open_noauth(
180 cli, &ndr_table_samr, &pipe_hnd);
183 if (!NT_STATUS_IS_OK(result)) {
184 if (lp_client_lanman_auth()) {
185 /* Use the old RAP method. */
186 result = cli_oem_change_password(cli,
187 user_name,
188 new_passwd,
189 old_passwd);
190 if (!NT_STATUS_IS_OK(result)) {
191 if (asprintf(err_str, "machine %s rejected the "
192 "password change: Error was : %s.\n",
193 remote_machine, nt_errstr(result)) == -1) {
194 *err_str = NULL;
196 cli_shutdown(cli);
197 return result;
199 } else {
200 if (asprintf(err_str, "SAMR connection to machine %s "
201 "failed. Error was %s, but LANMAN password "
202 "changes are disabled\n",
203 remote_machine, nt_errstr(result)) == -1) {
204 *err_str = NULL;
206 cli_shutdown(cli);
207 return result;
211 status = dcerpc_samr_chgpasswd_user4(pipe_hnd->binding_handle,
212 talloc_tos(),
213 pipe_hnd->srv_name_slash,
214 user_name,
215 old_passwd,
216 new_passwd,
217 &result);
218 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result)) {
219 /* All good, password successfully changed. */
220 cli_shutdown(cli);
221 return NT_STATUS_OK;
223 if (!NT_STATUS_IS_OK(status)) {
224 if (NT_STATUS_EQUAL(status,
225 NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE) ||
226 NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
227 NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
228 /* DO NOT FALLBACK TO RC4 */
229 if (lp_weak_crypto() == SAMBA_WEAK_CRYPTO_DISALLOWED) {
230 cli_shutdown(cli);
231 return NT_STATUS_STRONG_CRYPTO_NOT_SUPPORTED;
234 } else {
235 if (!NT_STATUS_IS_OK(result)) {
236 int rc = asprintf(
237 err_str,
238 "machine %s rejected to change the password"
239 "with error: %s",
240 remote_machine,
241 get_friendly_nt_error_msg(result));
242 if (rc <= 0) {
243 *err_str = NULL;
245 cli_shutdown(cli);
246 return result;
250 result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
251 user_name, new_passwd, old_passwd);
252 if (NT_STATUS_IS_OK(result)) {
253 /* Great - it all worked! */
254 cli_shutdown(cli);
255 return NT_STATUS_OK;
258 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ||
259 NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))
261 /* it failed, but for reasons such as wrong password, too short etc ... */
263 if (asprintf(err_str, "machine %s rejected the password change: "
264 "Error was : %s.\n",
265 remote_machine, get_friendly_nt_error_msg(result)) == -1) {
266 *err_str = NULL;
268 cli_shutdown(cli);
269 return result;
272 /* OK, that failed, so try again... */
273 TALLOC_FREE(pipe_hnd);
275 /* OK, this is ugly, but... try an anonymous pipe. */
276 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
277 &pipe_hnd);
279 if ( NT_STATUS_IS_OK(result) &&
280 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
281 pipe_hnd, talloc_tos(), user_name,
282 new_passwd, old_passwd)))) {
283 /* Great - it all worked! */
284 cli_shutdown(cli);
285 return NT_STATUS_OK;
286 } else {
287 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
288 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
289 /* it failed, but again it was due to things like new password too short */
291 if (asprintf(err_str, "machine %s rejected the "
292 "(anonymous) password change: Error was : "
293 "%s.\n", remote_machine,
294 get_friendly_nt_error_msg(result)) == -1) {
295 *err_str = NULL;
297 cli_shutdown(cli);
298 return result;
301 /* We have failed to change the user's password, and we think the server
302 just might not support SAMR password changes, so fall back */
304 if (!lp_client_lanman_auth()) {
305 if (asprintf(err_str, "SAMR connection to machine %s "
306 "failed. Error was %s, but LANMAN password "
307 "changes are disabled\n",
308 remote_machine, nt_errstr(result)) == -1) {
309 *err_str = NULL;
311 cli_shutdown(cli);
312 return NT_STATUS_UNSUCCESSFUL;
315 /* Use the old RAP method. */
316 result = cli_oem_change_password(cli,
317 user_name,
318 new_passwd,
319 old_passwd);
320 if (NT_STATUS_IS_OK(result)) {
321 /* SAMR failed, but the old LanMan protocol worked! */
323 cli_shutdown(cli);
324 return NT_STATUS_OK;
327 if (asprintf(err_str,
328 "machine %s rejected the password "
329 "change: Error was : %s.\n",
330 remote_machine,
331 nt_errstr(result)) == -1)
333 *err_str = NULL;
335 cli_shutdown(cli);
336 return result;