r299: updating release notes
[Samba/gebeck_regimport.git] / source3 / libsmb / passchange.c
blob9f46c131fee54b423f8a4a7e13d9e640593c9cc6
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 /*************************************************************
24 change a password on a remote machine using IPC calls
25 *************************************************************/
26 BOOL 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 in_addr ip;
33 struct ntuser_creds creds;
35 NTSTATUS result;
37 *err_str = '\0';
39 if(!resolve_name( remote_machine, &ip, 0x20)) {
40 slprintf(err_str, err_str_len-1, "unable to find an IP address for machine %s.\n",
41 remote_machine );
42 return False;
45 ZERO_STRUCT(cli);
47 if (!cli_initialise(&cli) || !cli_connect(&cli, remote_machine, &ip)) {
48 slprintf(err_str, err_str_len-1, "unable to connect to SMB server on machine %s. Error was : %s.\n",
49 remote_machine, cli_errstr(&cli) );
50 return False;
53 make_nmb_name(&calling, global_myname() , 0x0);
54 make_nmb_name(&called , remote_machine, 0x20);
56 if (!cli_session_request(&cli, &calling, &called)) {
57 slprintf(err_str, err_str_len-1, "machine %s rejected the session setup. Error was : %s.\n",
58 remote_machine, cli_errstr(&cli) );
59 cli_shutdown(&cli);
60 return False;
63 cli.protocol = PROTOCOL_NT1;
65 if (!cli_negprot(&cli)) {
66 slprintf(err_str, err_str_len-1, "machine %s rejected the negotiate protocol. Error was : %s.\n",
67 remote_machine, cli_errstr(&cli) );
68 cli_shutdown(&cli);
69 return False;
72 /* Given things like SMB signing, restrict anonymous and the like,
73 try an authenticated connection first */
74 if (!cli_session_setup(&cli, user_name, old_passwd, strlen(old_passwd)+1, old_passwd, strlen(old_passwd)+1, "")) {
76 * We should connect as the anonymous user here, in case
77 * the server has "must change password" checked...
78 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
81 if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
82 slprintf(err_str, err_str_len-1, "machine %s rejected the session setup. Error was : %s.\n",
83 remote_machine, cli_errstr(&cli) );
84 cli_shutdown(&cli);
85 return False;
88 init_creds(&creds, "", "", NULL);
89 cli_init_creds(&cli, &creds);
90 } else {
91 init_creds(&creds, user_name, "", old_passwd);
92 cli_init_creds(&cli, &creds);
95 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
96 slprintf(err_str, err_str_len-1, "machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
97 remote_machine, cli_errstr(&cli) );
98 cli_shutdown(&cli);
99 return False;
102 /* Try not to give the password away to easily */
104 cli.pipe_auth_flags = AUTH_PIPE_NTLMSSP;
105 cli.pipe_auth_flags |= AUTH_PIPE_SIGN;
106 cli.pipe_auth_flags |= AUTH_PIPE_SEAL;
108 if ( !cli_nt_session_open( &cli, PI_SAMR ) ) {
109 if (lp_client_lanman_auth()) {
110 if (!cli_oem_change_password(&cli, user_name, new_passwd, old_passwd)) {
111 slprintf(err_str, err_str_len-1, "machine %s rejected the password change: Error was : %s.\n",
112 remote_machine, cli_errstr(&cli) );
113 cli_shutdown(&cli);
114 return False;
116 } else {
117 slprintf(err_str, err_str_len-1, "machine %s does not support SAMR connections, but LANMAN password changed are disabled\n",
118 remote_machine);
119 cli_shutdown(&cli);
120 return False;
124 if (NT_STATUS_IS_OK(result = cli_samr_chgpasswd_user(&cli, cli.mem_ctx, user_name,
125 new_passwd, old_passwd))) {
126 /* Great - it all worked! */
127 cli_shutdown(&cli);
128 return True;
130 } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
131 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
132 /* it failed, but for reasons such as wrong password, too short etc ... */
134 slprintf(err_str, err_str_len-1, "machine %s rejected the password change: Error was : %s.\n",
135 remote_machine, get_friendly_nt_error_msg(result));
136 cli_shutdown(&cli);
137 return False;
140 /* OK, that failed, so try again... */
141 cli_nt_session_close(&cli);
143 /* Try anonymous NTLMSSP... */
144 init_creds(&creds, "", "", NULL);
145 cli_init_creds(&cli, &creds);
147 result = NT_STATUS_UNSUCCESSFUL;
149 /* OK, this is ugly, but... */
150 if ( cli_nt_session_open( &cli, PI_SAMR )
151 && NT_STATUS_IS_OK(result
152 = cli_samr_chgpasswd_user(&cli, cli.mem_ctx, user_name,
153 new_passwd, old_passwd))) {
154 /* Great - it all worked! */
155 cli_shutdown(&cli);
156 return True;
158 } else {
159 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
160 || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
161 /* it failed, but again it was due to things like new password too short */
163 slprintf(err_str, err_str_len-1,
164 "machine %s rejected the (anonymous) password change: Error was : %s.\n",
165 remote_machine, get_friendly_nt_error_msg(result));
166 cli_shutdown(&cli);
167 return False;
170 /* We have failed to change the user's password, and we think the server
171 just might not support SAMR password changes, so fall back */
173 if (lp_client_lanman_auth()) {
174 if (cli_oem_change_password(&cli, user_name, new_passwd, old_passwd)) {
175 /* SAMR failed, but the old LanMan protocol worked! */
177 cli_shutdown(&cli);
178 return True;
180 slprintf(err_str, err_str_len-1,
181 "machine %s rejected the password change: Error was : %s.\n",
182 remote_machine, cli_errstr(&cli) );
183 cli_shutdown(&cli);
184 return False;
185 } else {
186 slprintf(err_str, err_str_len-1,
187 "machine %s does not support SAMR connections, but LANMAN password changed are disabled\n",
188 remote_machine);
189 cli_shutdown(&cli);
190 return False;