WHATSNEW: Start release notes for Samba 3.6.14.
[Samba.git] / source3 / libsmb / passchange.c
blobfc68e41bd91b303598454e817c55bbc919da33d6
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"
28 /*************************************************************
29 Change a password on a remote machine using IPC calls.
30 *************************************************************/
32 NTSTATUS remote_password_change(const char *remote_machine, const char *user_name,
33 const char *old_passwd, const char *new_passwd,
34 char **err_str)
36 struct nmb_name calling, called;
37 struct cli_state *cli = NULL;
38 struct rpc_pipe_client *pipe_hnd = NULL;
39 struct sockaddr_storage ss;
40 char *user, *domain, *p;
42 NTSTATUS result;
43 bool pass_must_change = False;
45 user = talloc_strdup(talloc_tos(), user_name);
46 SMB_ASSERT(user != NULL);
47 domain = talloc_strdup(talloc_tos(), "");
48 SMB_ASSERT(domain != NULL);
50 /* allow usernames of the form domain\\user or domain/user */
51 if ((p = strchr_m(user,'\\')) || (p = strchr_m(user,'/')) ||
52 (p = strchr_m(user,*lp_winbind_separator()))) {
53 *p = 0;
54 domain = user;
55 user = p+1;
58 *err_str = NULL;
60 if(!resolve_name( remote_machine, &ss, 0x20, false)) {
61 if (asprintf(err_str, "Unable to find an IP address for machine "
62 "%s.\n", remote_machine) == -1) {
63 *err_str = NULL;
65 return NT_STATUS_UNSUCCESSFUL;
68 cli = cli_initialise();
69 if (!cli) {
70 return NT_STATUS_NO_MEMORY;
73 result = cli_connect(cli, remote_machine, &ss);
74 if (!NT_STATUS_IS_OK(result)) {
75 if (asprintf(err_str, "Unable to connect to SMB server on "
76 "machine %s. Error was : %s.\n",
77 remote_machine, nt_errstr(result))==-1) {
78 *err_str = NULL;
80 cli_shutdown(cli);
81 return result;
84 make_nmb_name(&calling, global_myname() , 0x0);
85 make_nmb_name(&called , remote_machine, 0x20);
87 if (!cli_session_request(cli, &calling, &called)) {
88 if (asprintf(err_str, "machine %s rejected the session setup. "
89 "Error was : %s.\n",
90 remote_machine, cli_errstr(cli)) == -1) {
91 *err_str = NULL;
93 result = cli_nt_error(cli);
94 cli_shutdown(cli);
95 return result;
98 cli->protocol = PROTOCOL_NT1;
100 result = cli_negprot(cli);
102 if (!NT_STATUS_IS_OK(result)) {
103 if (asprintf(err_str, "machine %s rejected the negotiate "
104 "protocol. Error was : %s.\n",
105 remote_machine, nt_errstr(result)) == -1) {
106 *err_str = NULL;
108 result = cli_nt_error(cli);
109 cli_shutdown(cli);
110 return result;
113 /* Given things like SMB signing, restrict anonymous and the like,
114 try an authenticated connection first */
115 result = cli_session_setup(cli, user_name,
116 old_passwd, strlen(old_passwd)+1,
117 old_passwd, strlen(old_passwd)+1, "");
119 if (!NT_STATUS_IS_OK(result)) {
121 /* Password must change or Password expired are the only valid
122 * error conditions here from where we can proceed, the rest like
123 * account locked out or logon failure will lead to errors later
124 * anyway */
126 if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
127 !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
128 if (asprintf(err_str, "Could not connect to machine %s: "
129 "%s\n", remote_machine, cli_errstr(cli)) == -1) {
130 *err_str = NULL;
132 cli_shutdown(cli);
133 return result;
136 pass_must_change = True;
139 * We should connect as the anonymous user here, in case
140 * the server has "must change password" checked...
141 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
144 result = cli_session_setup(cli, "", "", 0, "", 0, "");
146 if (!NT_STATUS_IS_OK(result)) {
147 if (asprintf(err_str, "machine %s rejected the session "
148 "setup. Error was : %s.\n",
149 remote_machine, cli_errstr(cli)) == -1) {
150 *err_str = NULL;
152 cli_shutdown(cli);
153 return result;
156 result = cli_init_creds(cli, "", "", NULL);
157 if (!NT_STATUS_IS_OK(result)) {
158 cli_shutdown(cli);
159 return result;
161 } else {
162 result = cli_init_creds(cli, user, domain, old_passwd);
163 if (!NT_STATUS_IS_OK(result)) {
164 cli_shutdown(cli);
165 return result;
169 result = cli_tcon_andx(cli, "IPC$", "IPC", "", 1);
170 if (!NT_STATUS_IS_OK(result)) {
171 if (asprintf(err_str, "machine %s rejected the tconX on the "
172 "IPC$ share. Error was : %s.\n",
173 remote_machine, nt_errstr(result))) {
174 *err_str = NULL;
176 cli_shutdown(cli);
177 return result;
180 /* Try not to give the password away too easily */
182 if (!pass_must_change) {
183 result = cli_rpc_pipe_open_ntlmssp(cli,
184 &ndr_table_samr.syntax_id,
185 NCACN_NP,
186 DCERPC_AUTH_LEVEL_PRIVACY,
187 domain, user,
188 old_passwd,
189 &pipe_hnd);
190 } else {
192 * If the user password must be changed the ntlmssp bind will
193 * fail the same way as the session setup above did. The
194 * difference ist that with a pipe bind we don't get a good
195 * error message, the result will be that the rpc call below
196 * will just fail. So we do it anonymously, there's no other
197 * way.
199 result = cli_rpc_pipe_open_noauth(
200 cli, &ndr_table_samr.syntax_id, &pipe_hnd);
203 if (!NT_STATUS_IS_OK(result)) {
204 if (lp_client_lanman_auth()) {
205 /* Use the old RAP method. */
206 if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
207 if (asprintf(err_str, "machine %s rejected the "
208 "password change: Error was : %s.\n",
209 remote_machine, cli_errstr(cli)) == -1) {
210 *err_str = NULL;
212 result = cli_nt_error(cli);
213 cli_shutdown(cli);
214 return result;
216 } else {
217 if (asprintf(err_str, "SAMR connection to machine %s "
218 "failed. Error was %s, but LANMAN password "
219 "changes are disabled\n",
220 remote_machine, nt_errstr(result)) == -1) {
221 *err_str = NULL;
223 result = cli_nt_error(cli);
224 cli_shutdown(cli);
225 return result;
229 result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
230 user_name, new_passwd, old_passwd);
231 if (NT_STATUS_IS_OK(result)) {
232 /* Great - it all worked! */
233 cli_shutdown(cli);
234 return NT_STATUS_OK;
236 } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
237 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
238 /* it failed, but for reasons such as wrong password, too short etc ... */
240 if (asprintf(err_str, "machine %s rejected the password change: "
241 "Error was : %s.\n",
242 remote_machine, get_friendly_nt_error_msg(result)) == -1) {
243 *err_str = NULL;
245 cli_shutdown(cli);
246 return result;
249 /* OK, that failed, so try again... */
250 TALLOC_FREE(pipe_hnd);
252 /* Try anonymous NTLMSSP... */
253 result = cli_init_creds(cli, "", "", NULL);
254 if (!NT_STATUS_IS_OK(result)) {
255 cli_shutdown(cli);
256 return result;
259 result = NT_STATUS_UNSUCCESSFUL;
261 /* OK, this is ugly, but... try an anonymous pipe. */
262 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
263 &pipe_hnd);
265 if ( NT_STATUS_IS_OK(result) &&
266 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
267 pipe_hnd, talloc_tos(), user_name,
268 new_passwd, old_passwd)))) {
269 /* Great - it all worked! */
270 cli_shutdown(cli);
271 return NT_STATUS_OK;
272 } else {
273 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
274 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
275 /* it failed, but again it was due to things like new password too short */
277 if (asprintf(err_str, "machine %s rejected the "
278 "(anonymous) password change: Error was : "
279 "%s.\n", remote_machine,
280 get_friendly_nt_error_msg(result)) == -1) {
281 *err_str = NULL;
283 cli_shutdown(cli);
284 return result;
287 /* We have failed to change the user's password, and we think the server
288 just might not support SAMR password changes, so fall back */
290 if (lp_client_lanman_auth()) {
291 /* Use the old RAP method. */
292 if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
293 /* SAMR failed, but the old LanMan protocol worked! */
295 cli_shutdown(cli);
296 return NT_STATUS_OK;
298 if (asprintf(err_str, "machine %s rejected the password "
299 "change: Error was : %s.\n",
300 remote_machine, cli_errstr(cli)) == -1) {
301 *err_str = NULL;
303 result = cli_nt_error(cli);
304 cli_shutdown(cli);
305 return result;
306 } else {
307 if (asprintf(err_str, "SAMR connection to machine %s "
308 "failed. Error was %s, but LANMAN password "
309 "changes are disabled\n",
310 nt_errstr(result), remote_machine) == -1) {
311 *err_str = NULL;
313 cli_shutdown(cli);
314 return NT_STATUS_UNSUCCESSFUL;