s3:smbd: use PROTOCOL_SMB2_02 instead PROTOCOL_SMB2
[Samba/gebeck_regimport.git] / source3 / libsmb / passchange.c
blobbf2103db6845179a80e5266d7cacf7db46191ebb
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 "../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"
28 /*************************************************************
29 Change a password on a remote machine using IPC calls.
30 *************************************************************/
32 NTSTATUS remote_password_change(const char *remote_machine, const char *user_name,
33 const char *old_passwd, const char *new_passwd,
34 char **err_str)
36 struct cli_state *cli = NULL;
37 struct rpc_pipe_client *pipe_hnd = NULL;
38 char *user, *domain, *p;
40 NTSTATUS result;
41 bool pass_must_change = False;
43 user = talloc_strdup(talloc_tos(), user_name);
44 SMB_ASSERT(user != NULL);
45 domain = talloc_strdup(talloc_tos(), "");
46 SMB_ASSERT(domain != NULL);
48 /* allow usernames of the form domain\\user or domain/user */
49 if ((p = strchr_m(user,'\\')) || (p = strchr_m(user,'/')) ||
50 (p = strchr_m(user,*lp_winbind_separator()))) {
51 *p = 0;
52 domain = user;
53 user = p+1;
56 *err_str = NULL;
58 result = cli_connect_nb(remote_machine, NULL, 0, 0x20, NULL,
59 Undefined, &cli);
60 if (!NT_STATUS_IS_OK(result)) {
61 if (asprintf(err_str, "Unable to connect to SMB server on "
62 "machine %s. Error was : %s.\n",
63 remote_machine, nt_errstr(result))==-1) {
64 *err_str = NULL;
66 return result;
69 cli->protocol = PROTOCOL_NT1;
71 result = cli_negprot(cli);
73 if (!NT_STATUS_IS_OK(result)) {
74 if (asprintf(err_str, "machine %s rejected the negotiate "
75 "protocol. Error was : %s.\n",
76 remote_machine, nt_errstr(result)) == -1) {
77 *err_str = NULL;
79 result = cli_nt_error(cli);
80 cli_shutdown(cli);
81 return result;
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, "");
90 if (!NT_STATUS_IS_OK(result)) {
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 */
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;
103 cli_shutdown(cli);
104 return result;
107 pass_must_change = True;
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.
115 result = cli_session_setup(cli, "", "", 0, "", 0, "");
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;
123 cli_shutdown(cli);
124 return result;
127 result = cli_init_creds(cli, "", "", NULL);
128 if (!NT_STATUS_IS_OK(result)) {
129 cli_shutdown(cli);
130 return result;
132 } else {
133 result = cli_init_creds(cli, user, domain, old_passwd);
134 if (!NT_STATUS_IS_OK(result)) {
135 cli_shutdown(cli);
136 return result;
140 result = cli_tcon_andx(cli, "IPC$", "IPC", "", 1);
141 if (!NT_STATUS_IS_OK(result)) {
142 if (asprintf(err_str, "machine %s rejected the tconX on the "
143 "IPC$ share. Error was : %s.\n",
144 remote_machine, nt_errstr(result))) {
145 *err_str = NULL;
147 cli_shutdown(cli);
148 return result;
151 /* Try not to give the password away too easily */
153 if (!pass_must_change) {
154 result = cli_rpc_pipe_open_ntlmssp(cli,
155 &ndr_table_samr.syntax_id,
156 NCACN_NP,
157 DCERPC_AUTH_LEVEL_PRIVACY,
158 domain, user,
159 old_passwd,
160 &pipe_hnd);
161 } else {
163 * If the user password must be changed the ntlmssp bind will
164 * fail the same way as the session setup above did. The
165 * difference ist that with a pipe bind we don't get a good
166 * error message, the result will be that the rpc call below
167 * will just fail. So we do it anonymously, there's no other
168 * way.
170 result = cli_rpc_pipe_open_noauth(
171 cli, &ndr_table_samr.syntax_id, &pipe_hnd);
174 if (!NT_STATUS_IS_OK(result)) {
175 if (lp_client_lanman_auth()) {
176 /* Use the old RAP method. */
177 if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
178 result = cli_nt_error(cli);
179 if (asprintf(err_str, "machine %s rejected the "
180 "password change: Error was : %s.\n",
181 remote_machine, nt_errstr(result)) == -1) {
182 *err_str = NULL;
184 cli_shutdown(cli);
185 return result;
187 } else {
188 if (asprintf(err_str, "SAMR connection to machine %s "
189 "failed. Error was %s, but LANMAN password "
190 "changes are disabled\n",
191 remote_machine, nt_errstr(result)) == -1) {
192 *err_str = NULL;
194 result = cli_nt_error(cli);
195 cli_shutdown(cli);
196 return result;
200 result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
201 user_name, new_passwd, old_passwd);
202 if (NT_STATUS_IS_OK(result)) {
203 /* Great - it all worked! */
204 cli_shutdown(cli);
205 return NT_STATUS_OK;
207 } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
208 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
209 /* it failed, but for reasons such as wrong password, too short etc ... */
211 if (asprintf(err_str, "machine %s rejected the password change: "
212 "Error was : %s.\n",
213 remote_machine, get_friendly_nt_error_msg(result)) == -1) {
214 *err_str = NULL;
216 cli_shutdown(cli);
217 return result;
220 /* OK, that failed, so try again... */
221 TALLOC_FREE(pipe_hnd);
223 /* Try anonymous NTLMSSP... */
224 result = cli_init_creds(cli, "", "", NULL);
225 if (!NT_STATUS_IS_OK(result)) {
226 cli_shutdown(cli);
227 return result;
230 result = NT_STATUS_UNSUCCESSFUL;
232 /* OK, this is ugly, but... try an anonymous pipe. */
233 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
234 &pipe_hnd);
236 if ( NT_STATUS_IS_OK(result) &&
237 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
238 pipe_hnd, talloc_tos(), user_name,
239 new_passwd, old_passwd)))) {
240 /* Great - it all worked! */
241 cli_shutdown(cli);
242 return NT_STATUS_OK;
243 } else {
244 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
245 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
246 /* it failed, but again it was due to things like new password too short */
248 if (asprintf(err_str, "machine %s rejected the "
249 "(anonymous) password change: Error was : "
250 "%s.\n", remote_machine,
251 get_friendly_nt_error_msg(result)) == -1) {
252 *err_str = NULL;
254 cli_shutdown(cli);
255 return result;
258 /* We have failed to change the user's password, and we think the server
259 just might not support SAMR password changes, so fall back */
261 if (lp_client_lanman_auth()) {
262 /* Use the old RAP method. */
263 if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
264 /* SAMR failed, but the old LanMan protocol worked! */
266 cli_shutdown(cli);
267 return NT_STATUS_OK;
270 result = cli_nt_error(cli);
271 if (asprintf(err_str, "machine %s rejected the password "
272 "change: Error was : %s.\n",
273 remote_machine, nt_errstr(result)) == -1) {
274 *err_str = NULL;
276 cli_shutdown(cli);
277 return result;
278 } else {
279 if (asprintf(err_str, "SAMR connection to machine %s "
280 "failed. Error was %s, but LANMAN password "
281 "changes are disabled\n",
282 nt_errstr(result), remote_machine) == -1) {
283 *err_str = NULL;
285 cli_shutdown(cli);
286 return NT_STATUS_UNSUCCESSFUL;