s3/libsmb: Fix typo in error message.
[Samba/fernandojvsilva.git] / source3 / libsmb / passchange.c
blob7f0389f132dccbf216ec9d5d4162f807421450b4
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"
22 /*************************************************************
23 Change a password on a remote machine using IPC calls.
24 *************************************************************/
26 NTSTATUS remote_password_change(const char *remote_machine, const char *user_name,
27 const char *old_passwd, const char *new_passwd,
28 char **err_str)
30 struct nmb_name calling, called;
31 struct cli_state *cli;
32 struct rpc_pipe_client *pipe_hnd;
33 struct sockaddr_storage ss;
35 NTSTATUS result;
36 bool pass_must_change = False;
38 *err_str = NULL;
40 if(!resolve_name( remote_machine, &ss, 0x20)) {
41 if (asprintf(err_str, "Unable to find an IP address for machine "
42 "%s.\n", remote_machine) == -1) {
43 *err_str = NULL;
45 return NT_STATUS_UNSUCCESSFUL;
48 cli = cli_initialise();
49 if (!cli) {
50 return NT_STATUS_NO_MEMORY;
53 result = cli_connect(cli, remote_machine, &ss);
54 if (!NT_STATUS_IS_OK(result)) {
55 if (asprintf(err_str, "Unable to connect to SMB server on "
56 "machine %s. Error was : %s.\n",
57 remote_machine, nt_errstr(result))==-1) {
58 *err_str = NULL;
60 cli_shutdown(cli);
61 return result;
64 make_nmb_name(&calling, global_myname() , 0x0);
65 make_nmb_name(&called , remote_machine, 0x20);
67 if (!cli_session_request(cli, &calling, &called)) {
68 if (asprintf(err_str, "machine %s rejected the session setup. "
69 "Error was : %s.\n",
70 remote_machine, cli_errstr(cli)) == -1) {
71 *err_str = NULL;
73 result = cli_nt_error(cli);
74 cli_shutdown(cli);
75 return result;
78 cli->protocol = PROTOCOL_NT1;
80 result = cli_negprot(cli);
82 if (!NT_STATUS_IS_OK(result)) {
83 if (asprintf(err_str, "machine %s rejected the negotiate "
84 "protocol. Error was : %s.\n",
85 remote_machine, nt_errstr(result)) == -1) {
86 *err_str = NULL;
88 result = cli_nt_error(cli);
89 cli_shutdown(cli);
90 return result;
93 /* Given things like SMB signing, restrict anonymous and the like,
94 try an authenticated connection first */
95 result = cli_session_setup(cli, user_name,
96 old_passwd, strlen(old_passwd)+1,
97 old_passwd, strlen(old_passwd)+1, "");
99 if (!NT_STATUS_IS_OK(result)) {
101 /* Password must change or Password expired are the only valid
102 * error conditions here from where we can proceed, the rest like
103 * account locked out or logon failure will lead to errors later
104 * anyway */
106 if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
107 !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
108 if (asprintf(err_str, "Could not connect to machine %s: "
109 "%s\n", remote_machine, cli_errstr(cli)) == -1) {
110 *err_str = NULL;
112 cli_shutdown(cli);
113 return result;
116 pass_must_change = True;
119 * We should connect as the anonymous user here, in case
120 * the server has "must change password" checked...
121 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
124 result = cli_session_setup(cli, "", "", 0, "", 0, "");
126 if (!NT_STATUS_IS_OK(result)) {
127 if (asprintf(err_str, "machine %s rejected the session "
128 "setup. Error was : %s.\n",
129 remote_machine, cli_errstr(cli)) == -1) {
130 *err_str = NULL;
132 cli_shutdown(cli);
133 return result;
136 result = cli_init_creds(cli, "", "", NULL);
137 if (!NT_STATUS_IS_OK(result)) {
138 cli_shutdown(cli);
139 return result;
141 } else {
142 result = cli_init_creds(cli, user_name, "", old_passwd);
143 if (!NT_STATUS_IS_OK(result)) {
144 cli_shutdown(cli);
145 return result;
149 result = cli_tcon_andx(cli, "IPC$", "IPC", "", 1);
150 if (!NT_STATUS_IS_OK(result)) {
151 if (asprintf(err_str, "machine %s rejected the tconX on the "
152 "IPC$ share. Error was : %s.\n",
153 remote_machine, nt_errstr(result))) {
154 *err_str = NULL;
156 cli_shutdown(cli);
157 return result;
160 /* Try not to give the password away too easily */
162 if (!pass_must_change) {
163 result = cli_rpc_pipe_open_ntlmssp(cli,
164 &ndr_table_samr.syntax_id,
165 PIPE_AUTH_LEVEL_PRIVACY,
166 "", /* what domain... ? */
167 user_name,
168 old_passwd,
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 ist 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.syntax_id, &pipe_hnd);
183 if (!NT_STATUS_IS_OK(result)) {
184 if (lp_client_lanman_auth()) {
185 /* Use the old RAP method. */
186 if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
187 if (asprintf(err_str, "machine %s rejected the "
188 "password change: Error was : %s.\n",
189 remote_machine, cli_errstr(cli)) == -1) {
190 *err_str = NULL;
192 result = cli_nt_error(cli);
193 cli_shutdown(cli);
194 return result;
196 } else {
197 if (asprintf(err_str, "SAMR connection to machine %s "
198 "failed. Error was %s, but LANMAN password "
199 "changes are disabled\n",
200 remote_machine, nt_errstr(result)) == -1) {
201 *err_str = NULL;
203 result = cli_nt_error(cli);
204 cli_shutdown(cli);
205 return result;
209 result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
210 user_name, new_passwd, old_passwd);
211 if (NT_STATUS_IS_OK(result)) {
212 /* Great - it all worked! */
213 cli_shutdown(cli);
214 return NT_STATUS_OK;
216 } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
217 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
218 /* it failed, but for reasons such as wrong password, too short etc ... */
220 if (asprintf(err_str, "machine %s rejected the password change: "
221 "Error was : %s.\n",
222 remote_machine, get_friendly_nt_error_msg(result)) == -1) {
223 *err_str = NULL;
225 cli_shutdown(cli);
226 return result;
229 /* OK, that failed, so try again... */
230 TALLOC_FREE(pipe_hnd);
232 /* Try anonymous NTLMSSP... */
233 result = cli_init_creds(cli, "", "", NULL);
234 if (!NT_STATUS_IS_OK(result)) {
235 cli_shutdown(cli);
236 return result;
239 result = NT_STATUS_UNSUCCESSFUL;
241 /* OK, this is ugly, but... try an anonymous pipe. */
242 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
243 &pipe_hnd);
245 if ( NT_STATUS_IS_OK(result) &&
246 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
247 pipe_hnd, talloc_tos(), user_name,
248 new_passwd, old_passwd)))) {
249 /* Great - it all worked! */
250 cli_shutdown(cli);
251 return NT_STATUS_OK;
252 } else {
253 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
254 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
255 /* it failed, but again it was due to things like new password too short */
257 if (asprintf(err_str, "machine %s rejected the "
258 "(anonymous) password change: Error was : "
259 "%s.\n", remote_machine,
260 get_friendly_nt_error_msg(result)) == -1) {
261 *err_str = NULL;
263 cli_shutdown(cli);
264 return result;
267 /* We have failed to change the user's password, and we think the server
268 just might not support SAMR password changes, so fall back */
270 if (lp_client_lanman_auth()) {
271 /* Use the old RAP method. */
272 if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
273 /* SAMR failed, but the old LanMan protocol worked! */
275 cli_shutdown(cli);
276 return NT_STATUS_OK;
278 if (asprintf(err_str, "machine %s rejected the password "
279 "change: Error was : %s.\n",
280 remote_machine, cli_errstr(cli)) == -1) {
281 *err_str = NULL;
283 result = cli_nt_error(cli);
284 cli_shutdown(cli);
285 return result;
286 } else {
287 if (asprintf(err_str, "SAMR connection to machine %s "
288 "failed. Error was %s, but LANMAN password "
289 "changed are disabled\n",
290 nt_errstr(result), remote_machine) == -1) {
291 *err_str = NULL;
293 cli_shutdown(cli);
294 return NT_STATUS_UNSUCCESSFUL;