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