Make cli_negprot return NTSTATUS instead of bool
[jra/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)
29 {
30         struct nmb_name calling, called;
31         struct cli_state *cli;
32         struct rpc_pipe_client *pipe_hnd;
33         struct sockaddr_storage ss;
34
35         NTSTATUS result;
36         bool pass_must_change = False;
37
38         *err_str = NULL;
39
40         if(!resolve_name( remote_machine, &ss, 0x20)) {
41                 asprintf(err_str, "Unable to find an IP address for machine "
42                          "%s.\n", 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, &ss);
52         if (!NT_STATUS_IS_OK(result)) {
53                 asprintf(err_str, "Unable to connect to SMB server on "
54                          "machine %s. Error was : %s.\n",
55                          remote_machine, nt_errstr(result));
56                 cli_shutdown(cli);
57                 return result;
58         }
59
60         make_nmb_name(&calling, global_myname() , 0x0);
61         make_nmb_name(&called , remote_machine, 0x20);
62
63         if (!cli_session_request(cli, &calling, &called)) {
64                 asprintf(err_str, "machine %s rejected the session setup. "
65                          "Error was : %s.\n",
66                          remote_machine, cli_errstr(cli) );
67                 result = cli_nt_error(cli);
68                 cli_shutdown(cli);
69                 return result;
70         }
71
72         cli->protocol = PROTOCOL_NT1;
73
74         result = cli_negprot(cli);
75
76         if (!NT_STATUS_IS_OK(result)) {
77                 asprintf(err_str, "machine %s rejected the negotiate "
78                          "protocol. Error was : %s.\n",        
79                          remote_machine, nt_errstr(result));
80                 result = cli_nt_error(cli);
81                 cli_shutdown(cli);
82                 return result;
83         }
84
85         /* Given things like SMB signing, restrict anonymous and the like, 
86            try an authenticated connection first */
87         result = cli_session_setup(cli, user_name,
88                                    old_passwd, strlen(old_passwd)+1,
89                                    old_passwd, strlen(old_passwd)+1, "");
90
91         if (!NT_STATUS_IS_OK(result)) {
92
93                 /* Password must change or Password expired are the only valid
94                  * error conditions here from where we can proceed, the rest like
95                  * account locked out or logon failure will lead to errors later
96                  * anyway */
97
98                 if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
99                     !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
100                         asprintf(err_str, "Could not connect to machine %s: "
101                                  "%s\n", remote_machine, cli_errstr(cli));
102                         cli_shutdown(cli);
103                         return result;
104                 }
105
106                 pass_must_change = True;
107
108                 /*
109                  * We should connect as the anonymous user here, in case
110                  * the server has "must change password" checked...
111                  * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
112                  */
113
114                 result = cli_session_setup(cli, "", "", 0, "", 0, "");
115
116                 if (!NT_STATUS_IS_OK(result)) {
117                         asprintf(err_str, "machine %s rejected the session "
118                                  "setup. Error was : %s.\n",        
119                                  remote_machine, cli_errstr(cli) );
120                         cli_shutdown(cli);
121                         return result;
122                 }
123
124                 cli_init_creds(cli, "", "", NULL);
125         } else {
126                 cli_init_creds(cli, user_name, "", old_passwd);
127         }
128
129         if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
130                 asprintf(err_str, "machine %s rejected the tconX on the IPC$ "
131                          "share. Error was : %s.\n",
132                          remote_machine, cli_errstr(cli) );
133                 result = cli_nt_error(cli);
134                 cli_shutdown(cli);
135                 return result;
136         }
137
138         /* Try not to give the password away too easily */
139
140         if (!pass_must_change) {
141                 result = cli_rpc_pipe_open_ntlmssp(cli,
142                                                    &ndr_table_samr.syntax_id,
143                                                    PIPE_AUTH_LEVEL_PRIVACY,
144                                                    "", /* what domain... ? */
145                                                    user_name,
146                                                    old_passwd,
147                                                    &pipe_hnd);
148         } else {
149                 /*
150                  * If the user password must be changed the ntlmssp bind will
151                  * fail the same way as the session setup above did. The
152                  * difference ist that with a pipe bind we don't get a good
153                  * error message, the result will be that the rpc call below
154                  * will just fail. So we do it anonymously, there's no other
155                  * way.
156                  */
157                 result = cli_rpc_pipe_open_noauth(
158                         cli, &ndr_table_samr.syntax_id, &pipe_hnd);
159         }
160
161         if (!NT_STATUS_IS_OK(result)) {
162                 if (lp_client_lanman_auth()) {
163                         /* Use the old RAP method. */
164                         if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
165                                 asprintf(err_str, "machine %s rejected the "
166                                          "password change: Error was : %s.\n",
167                                          remote_machine, cli_errstr(cli) );
168                                 result = cli_nt_error(cli);
169                                 cli_shutdown(cli);
170                                 return result;
171                         }
172                 } else {
173                         asprintf(err_str, "SAMR connection to machine %s "
174                                  "failed. Error was %s, but LANMAN password "
175                                  "changed are disabled\n",
176                                  nt_errstr(result), remote_machine);
177                         result = cli_nt_error(cli);
178                         cli_shutdown(cli);
179                         return result;
180                 }
181         }
182
183         result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
184                                              user_name, new_passwd, old_passwd);
185         if (NT_STATUS_IS_OK(result)) {
186                 /* Great - it all worked! */
187                 cli_shutdown(cli);
188                 return NT_STATUS_OK;
189
190         } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) 
191                      || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
192                 /* it failed, but for reasons such as wrong password, too short etc ... */
193
194                 asprintf(err_str, "machine %s rejected the password change: "
195                          "Error was : %s.\n",
196                          remote_machine, get_friendly_nt_error_msg(result));
197                 cli_shutdown(cli);
198                 return result;
199         }
200
201         /* OK, that failed, so try again... */
202         TALLOC_FREE(pipe_hnd);
203
204         /* Try anonymous NTLMSSP... */
205         cli_init_creds(cli, "", "", NULL);
206
207         result = NT_STATUS_UNSUCCESSFUL;
208
209         /* OK, this is ugly, but... try an anonymous pipe. */
210         result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
211                                           &pipe_hnd);
212
213         if ( NT_STATUS_IS_OK(result) &&
214                 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
215                                          pipe_hnd, talloc_tos(), user_name,
216                                          new_passwd, old_passwd)))) {
217                 /* Great - it all worked! */
218                 cli_shutdown(cli);
219                 return NT_STATUS_OK;
220         } else {
221                 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) 
222                       || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
223                         /* it failed, but again it was due to things like new password too short */
224
225                         asprintf(err_str, "machine %s rejected the "
226                                  "(anonymous) password change: Error was : "
227                                  "%s.\n", remote_machine,
228                                  get_friendly_nt_error_msg(result));
229                         cli_shutdown(cli);
230                         return result;
231                 }
232
233                 /* We have failed to change the user's password, and we think the server
234                    just might not support SAMR password changes, so fall back */
235
236                 if (lp_client_lanman_auth()) {
237                         /* Use the old RAP method. */
238                         if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
239                                 /* SAMR failed, but the old LanMan protocol worked! */
240
241                                 cli_shutdown(cli);
242                                 return NT_STATUS_OK;
243                         }
244                         asprintf(err_str, "machine %s rejected the password "
245                                  "change: Error was : %s.\n",
246                                  remote_machine, cli_errstr(cli) );
247                         result = cli_nt_error(cli);
248                         cli_shutdown(cli);
249                         return result;
250                 } else {
251                         asprintf(err_str, "SAMR connection to machine %s "
252                                  "failed. Error was %s, but LANMAN password "
253                                  "changed are disabled\n",
254                                 nt_errstr(result), remote_machine);
255                         cli_shutdown(cli);
256                         return NT_STATUS_UNSUCCESSFUL;
257                 }
258         }
259 }