s3-waf: ndr string functions moved to top level
[Samba/ekacnet.git] / source3 / libsmb / passchange.c
blob4a81a2141774cbfcb2ee14438482677cdbdac20a
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 "rpc_client/cli_samr.h"
23 /*************************************************************
24 Change a password on a remote machine using IPC calls.
25 *************************************************************/
27 NTSTATUS remote_password_change(const char *remote_machine, const char *user_name,
28 const char *old_passwd, const char *new_passwd,
29 char **err_str)
31 struct nmb_name calling, called;
32 struct cli_state *cli = NULL;
33 struct rpc_pipe_client *pipe_hnd = NULL;
34 struct sockaddr_storage ss;
35 char *user, *domain, *p;
37 NTSTATUS result;
38 bool pass_must_change = False;
40 user = talloc_strdup(talloc_tos(), user_name);
41 SMB_ASSERT(user != NULL);
42 domain = talloc_strdup(talloc_tos(), "");
43 SMB_ASSERT(domain != NULL);
45 /* allow usernames of the form domain\\user or domain/user */
46 if ((p = strchr_m(user,'\\')) || (p = strchr_m(user,'/')) ||
47 (p = strchr_m(user,*lp_winbind_separator()))) {
48 *p = 0;
49 domain = user;
50 user = p+1;
53 *err_str = NULL;
55 if(!resolve_name( remote_machine, &ss, 0x20, false)) {
56 if (asprintf(err_str, "Unable to find an IP address for machine "
57 "%s.\n", remote_machine) == -1) {
58 *err_str = NULL;
60 return NT_STATUS_UNSUCCESSFUL;
63 cli = cli_initialise();
64 if (!cli) {
65 return NT_STATUS_NO_MEMORY;
68 result = cli_connect(cli, remote_machine, &ss);
69 if (!NT_STATUS_IS_OK(result)) {
70 if (asprintf(err_str, "Unable to connect to SMB server on "
71 "machine %s. Error was : %s.\n",
72 remote_machine, nt_errstr(result))==-1) {
73 *err_str = NULL;
75 cli_shutdown(cli);
76 return result;
79 make_nmb_name(&calling, global_myname() , 0x0);
80 make_nmb_name(&called , remote_machine, 0x20);
82 if (!cli_session_request(cli, &calling, &called)) {
83 if (asprintf(err_str, "machine %s rejected the session setup. "
84 "Error was : %s.\n",
85 remote_machine, cli_errstr(cli)) == -1) {
86 *err_str = NULL;
88 result = cli_nt_error(cli);
89 cli_shutdown(cli);
90 return result;
93 cli->protocol = PROTOCOL_NT1;
95 result = cli_negprot(cli);
97 if (!NT_STATUS_IS_OK(result)) {
98 if (asprintf(err_str, "machine %s rejected the negotiate "
99 "protocol. Error was : %s.\n",
100 remote_machine, nt_errstr(result)) == -1) {
101 *err_str = NULL;
103 result = cli_nt_error(cli);
104 cli_shutdown(cli);
105 return result;
108 /* Given things like SMB signing, restrict anonymous and the like,
109 try an authenticated connection first */
110 result = cli_session_setup(cli, user_name,
111 old_passwd, strlen(old_passwd)+1,
112 old_passwd, strlen(old_passwd)+1, "");
114 if (!NT_STATUS_IS_OK(result)) {
116 /* Password must change or Password expired are the only valid
117 * error conditions here from where we can proceed, the rest like
118 * account locked out or logon failure will lead to errors later
119 * anyway */
121 if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
122 !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
123 if (asprintf(err_str, "Could not connect to machine %s: "
124 "%s\n", remote_machine, cli_errstr(cli)) == -1) {
125 *err_str = NULL;
127 cli_shutdown(cli);
128 return result;
131 pass_must_change = True;
134 * We should connect as the anonymous user here, in case
135 * the server has "must change password" checked...
136 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
139 result = cli_session_setup(cli, "", "", 0, "", 0, "");
141 if (!NT_STATUS_IS_OK(result)) {
142 if (asprintf(err_str, "machine %s rejected the session "
143 "setup. Error was : %s.\n",
144 remote_machine, cli_errstr(cli)) == -1) {
145 *err_str = NULL;
147 cli_shutdown(cli);
148 return result;
151 result = cli_init_creds(cli, "", "", NULL);
152 if (!NT_STATUS_IS_OK(result)) {
153 cli_shutdown(cli);
154 return result;
156 } else {
157 result = cli_init_creds(cli, user, domain, old_passwd);
158 if (!NT_STATUS_IS_OK(result)) {
159 cli_shutdown(cli);
160 return result;
164 result = cli_tcon_andx(cli, "IPC$", "IPC", "", 1);
165 if (!NT_STATUS_IS_OK(result)) {
166 if (asprintf(err_str, "machine %s rejected the tconX on the "
167 "IPC$ share. Error was : %s.\n",
168 remote_machine, nt_errstr(result))) {
169 *err_str = NULL;
171 cli_shutdown(cli);
172 return result;
175 /* Try not to give the password away too easily */
177 if (!pass_must_change) {
178 result = cli_rpc_pipe_open_ntlmssp(cli,
179 &ndr_table_samr.syntax_id,
180 NCACN_NP,
181 DCERPC_AUTH_LEVEL_PRIVACY,
182 domain, user,
183 old_passwd,
184 &pipe_hnd);
185 } else {
187 * If the user password must be changed the ntlmssp bind will
188 * fail the same way as the session setup above did. The
189 * difference ist that with a pipe bind we don't get a good
190 * error message, the result will be that the rpc call below
191 * will just fail. So we do it anonymously, there's no other
192 * way.
194 result = cli_rpc_pipe_open_noauth(
195 cli, &ndr_table_samr.syntax_id, &pipe_hnd);
198 if (!NT_STATUS_IS_OK(result)) {
199 if (lp_client_lanman_auth()) {
200 /* Use the old RAP method. */
201 if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
202 if (asprintf(err_str, "machine %s rejected the "
203 "password change: Error was : %s.\n",
204 remote_machine, cli_errstr(cli)) == -1) {
205 *err_str = NULL;
207 result = cli_nt_error(cli);
208 cli_shutdown(cli);
209 return result;
211 } else {
212 if (asprintf(err_str, "SAMR connection to machine %s "
213 "failed. Error was %s, but LANMAN password "
214 "changes are disabled\n",
215 remote_machine, nt_errstr(result)) == -1) {
216 *err_str = NULL;
218 result = cli_nt_error(cli);
219 cli_shutdown(cli);
220 return result;
224 result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
225 user_name, new_passwd, old_passwd);
226 if (NT_STATUS_IS_OK(result)) {
227 /* Great - it all worked! */
228 cli_shutdown(cli);
229 return NT_STATUS_OK;
231 } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
232 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
233 /* it failed, but for reasons such as wrong password, too short etc ... */
235 if (asprintf(err_str, "machine %s rejected the password change: "
236 "Error was : %s.\n",
237 remote_machine, get_friendly_nt_error_msg(result)) == -1) {
238 *err_str = NULL;
240 cli_shutdown(cli);
241 return result;
244 /* OK, that failed, so try again... */
245 TALLOC_FREE(pipe_hnd);
247 /* Try anonymous NTLMSSP... */
248 result = cli_init_creds(cli, "", "", NULL);
249 if (!NT_STATUS_IS_OK(result)) {
250 cli_shutdown(cli);
251 return result;
254 result = NT_STATUS_UNSUCCESSFUL;
256 /* OK, this is ugly, but... try an anonymous pipe. */
257 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
258 &pipe_hnd);
260 if ( NT_STATUS_IS_OK(result) &&
261 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
262 pipe_hnd, talloc_tos(), user_name,
263 new_passwd, old_passwd)))) {
264 /* Great - it all worked! */
265 cli_shutdown(cli);
266 return NT_STATUS_OK;
267 } else {
268 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
269 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
270 /* it failed, but again it was due to things like new password too short */
272 if (asprintf(err_str, "machine %s rejected the "
273 "(anonymous) password change: Error was : "
274 "%s.\n", remote_machine,
275 get_friendly_nt_error_msg(result)) == -1) {
276 *err_str = NULL;
278 cli_shutdown(cli);
279 return result;
282 /* We have failed to change the user's password, and we think the server
283 just might not support SAMR password changes, so fall back */
285 if (lp_client_lanman_auth()) {
286 /* Use the old RAP method. */
287 if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
288 /* SAMR failed, but the old LanMan protocol worked! */
290 cli_shutdown(cli);
291 return NT_STATUS_OK;
293 if (asprintf(err_str, "machine %s rejected the password "
294 "change: Error was : %s.\n",
295 remote_machine, cli_errstr(cli)) == -1) {
296 *err_str = NULL;
298 result = cli_nt_error(cli);
299 cli_shutdown(cli);
300 return result;
301 } else {
302 if (asprintf(err_str, "SAMR connection to machine %s "
303 "failed. Error was %s, but LANMAN password "
304 "changes are disabled\n",
305 nt_errstr(result), remote_machine) == -1) {
306 *err_str = NULL;
308 cli_shutdown(cli);
309 return NT_STATUS_UNSUCCESSFUL;