source: vendor/current/source3/libsmb/passchange.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 8.1 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 SMB client password change routine
4 Copyright (C) Andrew Tridgell 1994-1998
5
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.
10
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.
15
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/>.
18*/
19
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"
28
29/*************************************************************
30 Change a password on a remote machine using IPC calls.
31*************************************************************/
32
33NTSTATUS remote_password_change(const char *remote_machine, const char *user_name,
34 const char *old_passwd, const char *new_passwd,
35 char **err_str)
36{
37 struct cli_state *cli = NULL;
38 struct rpc_pipe_client *pipe_hnd = NULL;
39 char *user, *domain, *p;
40
41 NTSTATUS result;
42 bool pass_must_change = False;
43
44 user = talloc_strdup(talloc_tos(), user_name);
45 SMB_ASSERT(user != NULL);
46 domain = talloc_strdup(talloc_tos(), "");
47 SMB_ASSERT(domain != NULL);
48
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()))) {
52 *p = 0;
53 domain = user;
54 user = p+1;
55 }
56
57 *err_str = NULL;
58
59 result = cli_connect_nb(remote_machine, NULL, 0, 0x20, NULL,
60 SMB_SIGNING_IPC_DEFAULT, 0, &cli);
61 if (!NT_STATUS_IS_OK(result)) {
62 if (asprintf(err_str, "Unable to connect to SMB server on "
63 "machine %s. Error was : %s.\n",
64 remote_machine, nt_errstr(result))==-1) {
65 *err_str = NULL;
66 }
67 return result;
68 }
69
70 result = smbXcli_negprot(cli->conn, cli->timeout,
71 lp_client_ipc_min_protocol(),
72 lp_client_ipc_max_protocol());
73
74 if (!NT_STATUS_IS_OK(result)) {
75 if (asprintf(err_str, "machine %s rejected the negotiate "
76 "protocol. Error was : %s.\n",
77 remote_machine, nt_errstr(result)) == -1) {
78 *err_str = NULL;
79 }
80 cli_shutdown(cli);
81 return result;
82 }
83
84 /* Given things like SMB signing, restrict anonymous and the like,
85 try an authenticated connection first */
86 result = cli_session_setup(cli, user_name,
87 old_passwd, strlen(old_passwd)+1,
88 old_passwd, strlen(old_passwd)+1, "");
89
90 if (!NT_STATUS_IS_OK(result)) {
91
92 /* Password must change or Password expired are the only valid
93 * error conditions here from where we can proceed, the rest like
94 * account locked out or logon failure will lead to errors later
95 * anyway */
96
97 if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
98 !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
99 if (asprintf(err_str, "Could not connect to machine %s: "
100 "%s\n", remote_machine, nt_errstr(result)) == -1) {
101 *err_str = NULL;
102 }
103 cli_shutdown(cli);
104 return result;
105 }
106
107 pass_must_change = True;
108
109 /*
110 * We should connect as the anonymous user here, in case
111 * the server has "must change password" checked...
112 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
113 */
114
115 result = cli_session_setup(cli, "", "", 0, "", 0, "");
116
117 if (!NT_STATUS_IS_OK(result)) {
118 if (asprintf(err_str, "machine %s rejected the session "
119 "setup. Error was : %s.\n",
120 remote_machine, nt_errstr(result)) == -1) {
121 *err_str = NULL;
122 }
123 cli_shutdown(cli);
124 return result;
125 }
126 }
127
128 result = cli_tree_connect(cli, "IPC$", "IPC", "", 1);
129 if (!NT_STATUS_IS_OK(result)) {
130 if (asprintf(err_str, "machine %s rejected the tconX on the "
131 "IPC$ share. Error was : %s.\n",
132 remote_machine, nt_errstr(result))) {
133 *err_str = NULL;
134 }
135 cli_shutdown(cli);
136 return result;
137 }
138
139 /* Try not to give the password away too easily */
140
141 if (!pass_must_change) {
142 result = cli_rpc_pipe_open_generic_auth(cli,
143 &ndr_table_samr,
144 NCACN_NP,
145 CRED_DONT_USE_KERBEROS,
146 DCERPC_AUTH_TYPE_NTLMSSP,
147 DCERPC_AUTH_LEVEL_PRIVACY,
148 remote_machine,
149 domain, user,
150 old_passwd,
151 &pipe_hnd);
152 } else {
153 /*
154 * If the user password must be changed the ntlmssp bind will
155 * fail the same way as the session setup above did. The
156 * difference ist that with a pipe bind we don't get a good
157 * error message, the result will be that the rpc call below
158 * will just fail. So we do it anonymously, there's no other
159 * way.
160 */
161 result = cli_rpc_pipe_open_noauth(
162 cli, &ndr_table_samr, &pipe_hnd);
163 }
164
165 if (!NT_STATUS_IS_OK(result)) {
166 if (lp_client_lanman_auth()) {
167 /* Use the old RAP method. */
168 if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
169 result = cli_nt_error(cli);
170 if (asprintf(err_str, "machine %s rejected the "
171 "password change: Error was : %s.\n",
172 remote_machine, nt_errstr(result)) == -1) {
173 *err_str = NULL;
174 }
175 cli_shutdown(cli);
176 return result;
177 }
178 } else {
179 if (asprintf(err_str, "SAMR connection to machine %s "
180 "failed. Error was %s, but LANMAN password "
181 "changes are disabled\n",
182 remote_machine, nt_errstr(result)) == -1) {
183 *err_str = NULL;
184 }
185 cli_shutdown(cli);
186 return result;
187 }
188 }
189
190 result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
191 user_name, new_passwd, old_passwd);
192 if (NT_STATUS_IS_OK(result)) {
193 /* Great - it all worked! */
194 cli_shutdown(cli);
195 return NT_STATUS_OK;
196
197 } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
198 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
199 /* it failed, but for reasons such as wrong password, too short etc ... */
200
201 if (asprintf(err_str, "machine %s rejected the password change: "
202 "Error was : %s.\n",
203 remote_machine, get_friendly_nt_error_msg(result)) == -1) {
204 *err_str = NULL;
205 }
206 cli_shutdown(cli);
207 return result;
208 }
209
210 /* OK, that failed, so try again... */
211 TALLOC_FREE(pipe_hnd);
212
213 /* Try anonymous NTLMSSP... */
214 result = NT_STATUS_UNSUCCESSFUL;
215
216 /* OK, this is ugly, but... try an anonymous pipe. */
217 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
218 &pipe_hnd);
219
220 if ( NT_STATUS_IS_OK(result) &&
221 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
222 pipe_hnd, talloc_tos(), user_name,
223 new_passwd, old_passwd)))) {
224 /* Great - it all worked! */
225 cli_shutdown(cli);
226 return NT_STATUS_OK;
227 } else {
228 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
229 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
230 /* it failed, but again it was due to things like new password too short */
231
232 if (asprintf(err_str, "machine %s rejected the "
233 "(anonymous) password change: Error was : "
234 "%s.\n", remote_machine,
235 get_friendly_nt_error_msg(result)) == -1) {
236 *err_str = NULL;
237 }
238 cli_shutdown(cli);
239 return result;
240 }
241
242 /* We have failed to change the user's password, and we think the server
243 just might not support SAMR password changes, so fall back */
244
245 if (lp_client_lanman_auth()) {
246 /* Use the old RAP method. */
247 if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
248 /* SAMR failed, but the old LanMan protocol worked! */
249
250 cli_shutdown(cli);
251 return NT_STATUS_OK;
252 }
253
254 result = cli_nt_error(cli);
255 if (asprintf(err_str, "machine %s rejected the password "
256 "change: Error was : %s.\n",
257 remote_machine, nt_errstr(result)) == -1) {
258 *err_str = NULL;
259 }
260 cli_shutdown(cli);
261 return result;
262 } else {
263 if (asprintf(err_str, "SAMR connection to machine %s "
264 "failed. Error was %s, but LANMAN password "
265 "changes are disabled\n",
266 remote_machine, nt_errstr(result)) == -1) {
267 *err_str = NULL;
268 }
269 cli_shutdown(cli);
270 return NT_STATUS_UNSUCCESSFUL;
271 }
272 }
273}
Note: See TracBrowser for help on using the repository browser.