s4: popt: Global replace of cmdline_credentials -> popt_get_cmdline_credentials().
[Samba.git] / source3 / libsmb / passchange.c
blobc89b7ca85d19f0ec80ed0aec0ba9fd883c3eb837
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, const char *user_name,
34 const char *old_passwd, const char *new_passwd,
35 char **err_str)
37 struct cli_state *cli = NULL;
38 struct cli_credentials *creds = NULL;
39 struct rpc_pipe_client *pipe_hnd = NULL;
40 NTSTATUS result;
41 bool pass_must_change = False;
43 *err_str = NULL;
45 result = cli_connect_nb(remote_machine, NULL, 0, 0x20, NULL,
46 SMB_SIGNING_IPC_DEFAULT, 0, &cli);
47 if (!NT_STATUS_IS_OK(result)) {
48 if (asprintf(err_str, "Unable to connect to SMB server on "
49 "machine %s. Error was : %s.\n",
50 remote_machine, nt_errstr(result))==-1) {
51 *err_str = NULL;
53 return result;
56 creds = cli_session_creds_init(cli,
57 user_name,
58 NULL, /* domain */
59 NULL, /* realm */
60 old_passwd,
61 false, /* use_kerberos */
62 false, /* fallback_after_kerberos */
63 false, /* use_ccache */
64 false); /* password_is_nt_hash */
65 SMB_ASSERT(creds != NULL);
67 result = smbXcli_negprot(cli->conn, cli->timeout,
68 lp_client_ipc_min_protocol(),
69 lp_client_ipc_max_protocol());
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) {
75 *err_str = NULL;
77 cli_shutdown(cli);
78 return result;
81 /* Given things like SMB signing, restrict anonymous and the like,
82 try an authenticated connection first */
83 result = cli_session_setup_creds(cli, creds);
85 if (!NT_STATUS_IS_OK(result)) {
87 /* Password must change or Password expired are the only valid
88 * error conditions here from where we can proceed, the rest like
89 * account locked out or logon failure will lead to errors later
90 * anyway */
92 if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
93 !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
94 if (asprintf(err_str, "Could not connect to machine %s: "
95 "%s\n", remote_machine, nt_errstr(result)) == -1) {
96 *err_str = NULL;
98 cli_shutdown(cli);
99 return result;
102 pass_must_change = True;
105 * We should connect as the anonymous user here, in case
106 * the server has "must change password" checked...
107 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
110 result = cli_session_setup_anon(cli);
112 if (!NT_STATUS_IS_OK(result)) {
113 if (asprintf(err_str, "machine %s rejected the session "
114 "setup. Error was : %s.\n",
115 remote_machine, nt_errstr(result)) == -1) {
116 *err_str = NULL;
118 cli_shutdown(cli);
119 return result;
123 result = cli_tree_connect(cli, "IPC$", "IPC", NULL);
124 if (!NT_STATUS_IS_OK(result)) {
125 if (asprintf(err_str, "machine %s rejected the tconX on the "
126 "IPC$ share. Error was : %s.\n",
127 remote_machine, nt_errstr(result))) {
128 *err_str = NULL;
130 cli_shutdown(cli);
131 return result;
134 /* Try not to give the password away too easily */
136 if (!pass_must_change) {
137 result = cli_rpc_pipe_open_with_creds(cli,
138 &ndr_table_samr,
139 NCACN_NP,
140 DCERPC_AUTH_TYPE_NTLMSSP,
141 DCERPC_AUTH_LEVEL_PRIVACY,
142 remote_machine,
143 creds,
144 &pipe_hnd);
145 } else {
147 * If the user password must be changed the ntlmssp bind will
148 * fail the same way as the session setup above did. The
149 * difference ist that with a pipe bind we don't get a good
150 * error message, the result will be that the rpc call below
151 * will just fail. So we do it anonymously, there's no other
152 * way.
154 result = cli_rpc_pipe_open_noauth(
155 cli, &ndr_table_samr, &pipe_hnd);
158 if (!NT_STATUS_IS_OK(result)) {
159 if (lp_client_lanman_auth()) {
160 /* Use the old RAP method. */
161 if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
162 result = cli_nt_error(cli);
163 if (asprintf(err_str, "machine %s rejected the "
164 "password change: Error was : %s.\n",
165 remote_machine, nt_errstr(result)) == -1) {
166 *err_str = NULL;
168 cli_shutdown(cli);
169 return result;
171 } else {
172 if (asprintf(err_str, "SAMR connection to machine %s "
173 "failed. Error was %s, but LANMAN password "
174 "changes are disabled\n",
175 remote_machine, nt_errstr(result)) == -1) {
176 *err_str = NULL;
178 cli_shutdown(cli);
179 return result;
183 result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
184 user_name, new_passwd, old_passwd);
185 if (NT_STATUS_IS_OK(result)) {
186 /* Great - it all worked! */
187 cli_shutdown(cli);
188 return NT_STATUS_OK;
190 } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
191 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
192 /* it failed, but for reasons such as wrong password, too short etc ... */
194 if (asprintf(err_str, "machine %s rejected the password change: "
195 "Error was : %s.\n",
196 remote_machine, get_friendly_nt_error_msg(result)) == -1) {
197 *err_str = NULL;
199 cli_shutdown(cli);
200 return result;
203 /* OK, that failed, so try again... */
204 TALLOC_FREE(pipe_hnd);
206 /* Try anonymous NTLMSSP... */
207 result = NT_STATUS_UNSUCCESSFUL;
209 /* OK, this is ugly, but... try an anonymous pipe. */
210 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
211 &pipe_hnd);
213 if ( NT_STATUS_IS_OK(result) &&
214 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
215 pipe_hnd, talloc_tos(), user_name,
216 new_passwd, old_passwd)))) {
217 /* Great - it all worked! */
218 cli_shutdown(cli);
219 return NT_STATUS_OK;
220 } else {
221 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
222 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
223 /* it failed, but again it was due to things like new password too short */
225 if (asprintf(err_str, "machine %s rejected the "
226 "(anonymous) password change: Error was : "
227 "%s.\n", remote_machine,
228 get_friendly_nt_error_msg(result)) == -1) {
229 *err_str = NULL;
231 cli_shutdown(cli);
232 return result;
235 /* We have failed to change the user's password, and we think the server
236 just might not support SAMR password changes, so fall back */
238 if (lp_client_lanman_auth()) {
239 /* Use the old RAP method. */
240 if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
241 /* SAMR failed, but the old LanMan protocol worked! */
243 cli_shutdown(cli);
244 return NT_STATUS_OK;
247 result = cli_nt_error(cli);
248 if (asprintf(err_str, "machine %s rejected the password "
249 "change: Error was : %s.\n",
250 remote_machine, nt_errstr(result)) == -1) {
251 *err_str = NULL;
253 cli_shutdown(cli);
254 return result;
255 } else {
256 if (asprintf(err_str, "SAMR connection to machine %s "
257 "failed. Error was %s, but LANMAN password "
258 "changes are disabled\n",
259 remote_machine, nt_errstr(result)) == -1) {
260 *err_str = NULL;
262 cli_shutdown(cli);
263 return NT_STATUS_UNSUCCESSFUL;