r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[kai/samba.git] / source3 / libsmb / passchange.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB client password change routine
4    Copyright (C) Andrew Tridgell 1994-1998
5    
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.
10    
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.
15    
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/>.
18 */
19
20 #include "includes.h"
21
22 /*************************************************************
23  Change a password on a remote machine using IPC calls.
24 *************************************************************/
25
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)
29 {
30         struct nmb_name calling, called;
31         struct cli_state *cli;
32         struct rpc_pipe_client *pipe_hnd;
33         struct in_addr ip;
34
35         NTSTATUS result;
36         BOOL pass_must_change = False;
37
38         *err_str = '\0';
39
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;
44         }
45  
46         cli = cli_initialise();
47         if (!cli) {
48                 return NT_STATUS_NO_MEMORY;
49         }
50
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;
57         }
58   
59         make_nmb_name(&calling, global_myname() , 0x0);
60         make_nmb_name(&called , remote_machine, 0x20);
61         
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;
68         }
69   
70         cli->protocol = PROTOCOL_NT1;
71
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;
78         }
79   
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, "");
85
86         if (!NT_STATUS_IS_OK(result)) {
87
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 */
92
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;
100                 }
101
102                 pass_must_change = True;
103
104                 /*
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.
108                  */
109
110                 result = cli_session_setup(cli, "", "", 0, "", 0, "");
111
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;
117                 }
118
119                 cli_init_creds(cli, "", "", NULL);
120         } else {
121                 cli_init_creds(cli, user_name, "", old_passwd);
122         }
123
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;
130         }
131
132         /* Try not to give the password away too easily */
133
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 {
143                 /*
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.
150                  */
151                 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &result);
152         }
153
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;
163                         }
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;
172                 }
173         }
174
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;
180
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 ... */
184                 
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;
189         }
190
191         /* OK, that failed, so try again... */
192         cli_rpc_pipe_close(pipe_hnd);
193         
194         /* Try anonymous NTLMSSP... */
195         cli_init_creds(cli, "", "", NULL);
196         
197         result = NT_STATUS_UNSUCCESSFUL;
198         
199         /* OK, this is ugly, but... try an anonymous pipe. */
200         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &result);
201
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 */
215
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;
221                 }
222                 
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 */
225                 
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! */
230
231                                 cli_shutdown(cli);
232                                 return NT_STATUS_OK;
233                         }
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;
247                 }
248         }
249 }