s3-smbpasswd: Fix Bug #6584: allow DOM\user when changing passwords remotely.
[Samba.git] / source3 / libsmb / passchange.c
blobf3cb9d69d6ea869dff25890d5b2f1af7cab4ad80
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;
34 char *user, *domain, *p;
36 NTSTATUS result;
37 bool pass_must_change = False;
39 user = talloc_strdup(talloc_tos(), user_name);
40 SMB_ASSERT(user != NULL);
41 domain = talloc_strdup(talloc_tos(), "");
42 SMB_ASSERT(domain != NULL);
44 /* allow usernames of the form domain\\user or domain/user */
45 if ((p = strchr_m(user,'\\')) || (p = strchr_m(user,'/')) ||
46 (p = strchr_m(user,*lp_winbind_separator()))) {
47 *p = 0;
48 domain = user;
49 user = p+1;
52 *err_str = NULL;
54 if(!resolve_name( remote_machine, &ss, 0x20)) {
55 if (asprintf(err_str, "Unable to find an IP address for machine "
56 "%s.\n", remote_machine) == -1) {
57 *err_str = NULL;
59 return NT_STATUS_UNSUCCESSFUL;
62 cli = cli_initialise();
63 if (!cli) {
64 return NT_STATUS_NO_MEMORY;
67 result = cli_connect(cli, remote_machine, &ss);
68 if (!NT_STATUS_IS_OK(result)) {
69 if (asprintf(err_str, "Unable to connect to SMB server on "
70 "machine %s. Error was : %s.\n",
71 remote_machine, nt_errstr(result))==-1) {
72 *err_str = NULL;
74 cli_shutdown(cli);
75 return result;
78 make_nmb_name(&calling, global_myname() , 0x0);
79 make_nmb_name(&called , remote_machine, 0x20);
81 if (!cli_session_request(cli, &calling, &called)) {
82 if (asprintf(err_str, "machine %s rejected the session setup. "
83 "Error was : %s.\n",
84 remote_machine, cli_errstr(cli)) == -1) {
85 *err_str = NULL;
87 result = cli_nt_error(cli);
88 cli_shutdown(cli);
89 return result;
92 cli->protocol = PROTOCOL_NT1;
94 result = cli_negprot(cli);
96 if (!NT_STATUS_IS_OK(result)) {
97 if (asprintf(err_str, "machine %s rejected the negotiate "
98 "protocol. Error was : %s.\n",
99 remote_machine, nt_errstr(result)) == -1) {
100 *err_str = NULL;
102 result = cli_nt_error(cli);
103 cli_shutdown(cli);
104 return result;
107 /* Given things like SMB signing, restrict anonymous and the like,
108 try an authenticated connection first */
109 result = cli_session_setup(cli, user_name,
110 old_passwd, strlen(old_passwd)+1,
111 old_passwd, strlen(old_passwd)+1, "");
113 if (!NT_STATUS_IS_OK(result)) {
115 /* Password must change or Password expired are the only valid
116 * error conditions here from where we can proceed, the rest like
117 * account locked out or logon failure will lead to errors later
118 * anyway */
120 if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
121 !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
122 if (asprintf(err_str, "Could not connect to machine %s: "
123 "%s\n", remote_machine, cli_errstr(cli)) == -1) {
124 *err_str = NULL;
126 cli_shutdown(cli);
127 return result;
130 pass_must_change = True;
133 * We should connect as the anonymous user here, in case
134 * the server has "must change password" checked...
135 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
138 result = cli_session_setup(cli, "", "", 0, "", 0, "");
140 if (!NT_STATUS_IS_OK(result)) {
141 if (asprintf(err_str, "machine %s rejected the session "
142 "setup. Error was : %s.\n",
143 remote_machine, cli_errstr(cli)) == -1) {
144 *err_str = NULL;
146 cli_shutdown(cli);
147 return result;
150 result = cli_init_creds(cli, "", "", NULL);
151 if (!NT_STATUS_IS_OK(result)) {
152 cli_shutdown(cli);
153 return result;
155 } else {
156 result = cli_init_creds(cli, user, domain, old_passwd);
157 if (!NT_STATUS_IS_OK(result)) {
158 cli_shutdown(cli);
159 return result;
163 result = cli_tcon_andx(cli, "IPC$", "IPC", "", 1);
164 if (!NT_STATUS_IS_OK(result)) {
165 if (asprintf(err_str, "machine %s rejected the tconX on the "
166 "IPC$ share. Error was : %s.\n",
167 remote_machine, nt_errstr(result))) {
168 *err_str = NULL;
170 cli_shutdown(cli);
171 return result;
174 /* Try not to give the password away too easily */
176 if (!pass_must_change) {
177 result = cli_rpc_pipe_open_ntlmssp(cli,
178 &ndr_table_samr.syntax_id,
179 PIPE_AUTH_LEVEL_PRIVACY,
180 domain, user,
181 old_passwd,
182 &pipe_hnd);
183 } else {
185 * If the user password must be changed the ntlmssp bind will
186 * fail the same way as the session setup above did. The
187 * difference ist that with a pipe bind we don't get a good
188 * error message, the result will be that the rpc call below
189 * will just fail. So we do it anonymously, there's no other
190 * way.
192 result = cli_rpc_pipe_open_noauth(
193 cli, &ndr_table_samr.syntax_id, &pipe_hnd);
196 if (!NT_STATUS_IS_OK(result)) {
197 if (lp_client_lanman_auth()) {
198 /* Use the old RAP method. */
199 if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
200 if (asprintf(err_str, "machine %s rejected the "
201 "password change: Error was : %s.\n",
202 remote_machine, cli_errstr(cli)) == -1) {
203 *err_str = NULL;
205 result = cli_nt_error(cli);
206 cli_shutdown(cli);
207 return result;
209 } else {
210 if (asprintf(err_str, "SAMR connection to machine %s "
211 "failed. Error was %s, but LANMAN password "
212 "changes are disabled\n",
213 remote_machine, nt_errstr(result)) == -1) {
214 *err_str = NULL;
216 result = cli_nt_error(cli);
217 cli_shutdown(cli);
218 return result;
222 result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
223 user_name, new_passwd, old_passwd);
224 if (NT_STATUS_IS_OK(result)) {
225 /* Great - it all worked! */
226 cli_shutdown(cli);
227 return NT_STATUS_OK;
229 } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
230 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
231 /* it failed, but for reasons such as wrong password, too short etc ... */
233 if (asprintf(err_str, "machine %s rejected the password change: "
234 "Error was : %s.\n",
235 remote_machine, get_friendly_nt_error_msg(result)) == -1) {
236 *err_str = NULL;
238 cli_shutdown(cli);
239 return result;
242 /* OK, that failed, so try again... */
243 TALLOC_FREE(pipe_hnd);
245 /* Try anonymous NTLMSSP... */
246 result = cli_init_creds(cli, "", "", NULL);
247 if (!NT_STATUS_IS_OK(result)) {
248 cli_shutdown(cli);
249 return result;
252 result = NT_STATUS_UNSUCCESSFUL;
254 /* OK, this is ugly, but... try an anonymous pipe. */
255 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
256 &pipe_hnd);
258 if ( NT_STATUS_IS_OK(result) &&
259 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
260 pipe_hnd, talloc_tos(), user_name,
261 new_passwd, old_passwd)))) {
262 /* Great - it all worked! */
263 cli_shutdown(cli);
264 return NT_STATUS_OK;
265 } else {
266 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
267 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
268 /* it failed, but again it was due to things like new password too short */
270 if (asprintf(err_str, "machine %s rejected the "
271 "(anonymous) password change: Error was : "
272 "%s.\n", remote_machine,
273 get_friendly_nt_error_msg(result)) == -1) {
274 *err_str = NULL;
276 cli_shutdown(cli);
277 return result;
280 /* We have failed to change the user's password, and we think the server
281 just might not support SAMR password changes, so fall back */
283 if (lp_client_lanman_auth()) {
284 /* Use the old RAP method. */
285 if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
286 /* SAMR failed, but the old LanMan protocol worked! */
288 cli_shutdown(cli);
289 return NT_STATUS_OK;
291 if (asprintf(err_str, "machine %s rejected the password "
292 "change: Error was : %s.\n",
293 remote_machine, cli_errstr(cli)) == -1) {
294 *err_str = NULL;
296 result = cli_nt_error(cli);
297 cli_shutdown(cli);
298 return result;
299 } else {
300 if (asprintf(err_str, "SAMR connection to machine %s "
301 "failed. Error was %s, but LANMAN password "
302 "changed are disabled\n",
303 nt_errstr(result), remote_machine) == -1) {
304 *err_str = NULL;
306 cli_shutdown(cli);
307 return NT_STATUS_UNSUCCESSFUL;