r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / libsmb / passchange.c
blobcec150d8b20a08f0af9bfeacc0b3dd00e881a3f5
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, size_t err_str_len)
30 struct nmb_name calling, called;
31 struct cli_state *cli;
32 struct rpc_pipe_client *pipe_hnd;
33 struct in_addr ip;
35 NTSTATUS result;
36 BOOL pass_must_change = False;
38 *err_str = '\0';
40 if(!resolve_name( remote_machine, &ip, 0x20)) {
41 slprintf(err_str, err_str_len-1, "Unable to find an IP address for machine %s.\n",
42 remote_machine );
43 return NT_STATUS_UNSUCCESSFUL;
46 cli = cli_initialise();
47 if (!cli) {
48 return NT_STATUS_NO_MEMORY;
51 result = cli_connect(cli, remote_machine, &ip);
52 if (!NT_STATUS_IS_OK(result)) {
53 slprintf(err_str, err_str_len-1, "Unable to connect to SMB server on machine %s. Error was : %s.\n",
54 remote_machine, nt_errstr(result) );
55 cli_shutdown(cli);
56 return result;
59 make_nmb_name(&calling, global_myname() , 0x0);
60 make_nmb_name(&called , remote_machine, 0x20);
62 if (!cli_session_request(cli, &calling, &called)) {
63 slprintf(err_str, err_str_len-1, "machine %s rejected the session setup. Error was : %s.\n",
64 remote_machine, cli_errstr(cli) );
65 result = cli_nt_error(cli);
66 cli_shutdown(cli);
67 return result;
70 cli->protocol = PROTOCOL_NT1;
72 if (!cli_negprot(cli)) {
73 slprintf(err_str, err_str_len-1, "machine %s rejected the negotiate protocol. Error was : %s.\n",
74 remote_machine, cli_errstr(cli) );
75 result = cli_nt_error(cli);
76 cli_shutdown(cli);
77 return result;
80 /* Given things like SMB signing, restrict anonymous and the like,
81 try an authenticated connection first */
82 result = cli_session_setup(cli, user_name,
83 old_passwd, strlen(old_passwd)+1,
84 old_passwd, strlen(old_passwd)+1, "");
86 if (!NT_STATUS_IS_OK(result)) {
88 /* Password must change or Password expired are the only valid
89 * error conditions here from where we can proceed, the rest like
90 * account locked out or logon failure will lead to errors later
91 * anyway */
93 if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
94 !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
95 slprintf(err_str, err_str_len-1, "Could not "
96 "connect to machine %s: %s\n",
97 remote_machine, cli_errstr(cli));
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(cli, "", "", 0, "", 0, "");
112 if (!NT_STATUS_IS_OK(result)) {
113 slprintf(err_str, err_str_len-1, "machine %s rejected the session setup. Error was : %s.\n",
114 remote_machine, cli_errstr(cli) );
115 cli_shutdown(cli);
116 return result;
119 cli_init_creds(cli, "", "", NULL);
120 } else {
121 cli_init_creds(cli, user_name, "", old_passwd);
124 if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
125 slprintf(err_str, err_str_len-1, "machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
126 remote_machine, cli_errstr(cli) );
127 result = cli_nt_error(cli);
128 cli_shutdown(cli);
129 return result;
132 /* Try not to give the password away too easily */
134 if (!pass_must_change) {
135 pipe_hnd = cli_rpc_pipe_open_ntlmssp(cli,
136 PI_SAMR,
137 PIPE_AUTH_LEVEL_PRIVACY,
138 "", /* what domain... ? */
139 user_name,
140 old_passwd,
141 &result);
142 } else {
144 * If the user password must be changed the ntlmssp bind will
145 * fail the same way as the session setup above did. The
146 * difference ist that with a pipe bind we don't get a good
147 * error message, the result will be that the rpc call below
148 * will just fail. So we do it anonymously, there's no other
149 * way.
151 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &result);
154 if (!pipe_hnd) {
155 if (lp_client_lanman_auth()) {
156 /* Use the old RAP method. */
157 if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
158 slprintf(err_str, err_str_len-1, "machine %s rejected the password change: Error was : %s.\n",
159 remote_machine, cli_errstr(cli) );
160 result = cli_nt_error(cli);
161 cli_shutdown(cli);
162 return result;
164 } else {
165 slprintf(err_str, err_str_len-1,
166 "SAMR connection to machine %s failed. Error was %s, "
167 "but LANMAN password changed are disabled\n",
168 nt_errstr(result), remote_machine);
169 result = cli_nt_error(cli);
170 cli_shutdown(cli);
171 return result;
175 if (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user(pipe_hnd, cli->mem_ctx, user_name,
176 new_passwd, old_passwd))) {
177 /* Great - it all worked! */
178 cli_shutdown(cli);
179 return NT_STATUS_OK;
181 } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
182 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
183 /* it failed, but for reasons such as wrong password, too short etc ... */
185 slprintf(err_str, err_str_len-1, "machine %s rejected the password change: Error was : %s.\n",
186 remote_machine, get_friendly_nt_error_msg(result));
187 cli_shutdown(cli);
188 return result;
191 /* OK, that failed, so try again... */
192 cli_rpc_pipe_close(pipe_hnd);
194 /* Try anonymous NTLMSSP... */
195 cli_init_creds(cli, "", "", NULL);
197 result = NT_STATUS_UNSUCCESSFUL;
199 /* OK, this is ugly, but... try an anonymous pipe. */
200 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &result);
202 if ( pipe_hnd &&
203 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user(pipe_hnd,
204 cli->mem_ctx,
205 user_name,
206 new_passwd,
207 old_passwd)))) {
208 /* Great - it all worked! */
209 cli_shutdown(cli);
210 return NT_STATUS_OK;
211 } else {
212 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
213 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
214 /* it failed, but again it was due to things like new password too short */
216 slprintf(err_str, err_str_len-1,
217 "machine %s rejected the (anonymous) password change: Error was : %s.\n",
218 remote_machine, get_friendly_nt_error_msg(result));
219 cli_shutdown(cli);
220 return result;
223 /* We have failed to change the user's password, and we think the server
224 just might not support SAMR password changes, so fall back */
226 if (lp_client_lanman_auth()) {
227 /* Use the old RAP method. */
228 if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
229 /* SAMR failed, but the old LanMan protocol worked! */
231 cli_shutdown(cli);
232 return NT_STATUS_OK;
234 slprintf(err_str, err_str_len-1,
235 "machine %s rejected the password change: Error was : %s.\n",
236 remote_machine, cli_errstr(cli) );
237 result = cli_nt_error(cli);
238 cli_shutdown(cli);
239 return result;
240 } else {
241 slprintf(err_str, err_str_len-1,
242 "SAMR connection to machine %s failed. Error was %s, "
243 "but LANMAN password changed are disabled\n",
244 nt_errstr(result), remote_machine);
245 cli_shutdown(cli);
246 return NT_STATUS_UNSUCCESSFUL;