s3:libsmb: Pass domain to remote_password_change()
[sfrench/samba-autobuild/.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 #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"
27 #include "../libcli/smb/smbXcli_base.h"
28
29 /*************************************************************
30  Change a password on a remote machine using IPC calls.
31 *************************************************************/
32
33 NTSTATUS remote_password_change(const char *remote_machine,
34                                 const char *domain, const char *user_name,
35                                 const char *old_passwd, const char *new_passwd,
36                                 char **err_str)
37 {
38         struct cli_state *cli = NULL;
39         struct cli_credentials *creds = NULL;
40         struct rpc_pipe_client *pipe_hnd = NULL;
41         NTSTATUS result;
42         bool pass_must_change = False;
43
44         *err_str = NULL;
45
46         result = cli_connect_nb(remote_machine, NULL, 0, 0x20, NULL,
47                                 SMB_SIGNING_IPC_DEFAULT, 0, &cli);
48         if (!NT_STATUS_IS_OK(result)) {
49                 if (asprintf(err_str, "Unable to connect to SMB server on "
50                          "machine %s. Error was : %s.\n",
51                          remote_machine, nt_errstr(result))==-1) {
52                         *err_str = NULL;
53                 }
54                 return result;
55         }
56
57         creds = cli_session_creds_init(cli,
58                                        user_name,
59                                        domain,
60                                        NULL, /* realm */
61                                        old_passwd,
62                                        false, /* use_kerberos */
63                                        false, /* fallback_after_kerberos */
64                                        false, /* use_ccache */
65                                        false); /* password_is_nt_hash */
66         SMB_ASSERT(creds != NULL);
67
68         result = smbXcli_negprot(cli->conn, cli->timeout,
69                                  lp_client_ipc_min_protocol(),
70                                  lp_client_ipc_max_protocol());
71
72         if (!NT_STATUS_IS_OK(result)) {
73                 if (asprintf(err_str, "machine %s rejected the negotiate "
74                          "protocol. Error was : %s.\n",        
75                          remote_machine, nt_errstr(result)) == -1) {
76                         *err_str = NULL;
77                 }
78                 cli_shutdown(cli);
79                 return result;
80         }
81
82         /* Given things like SMB signing, restrict anonymous and the like, 
83            try an authenticated connection first */
84         result = cli_session_setup_creds(cli, creds);
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                         if (asprintf(err_str, "Could not connect to machine %s: "
96                                  "%s\n", remote_machine, nt_errstr(result)) == -1) {
97                                 *err_str = NULL;
98                         }
99                         cli_shutdown(cli);
100                         return result;
101                 }
102
103                 pass_must_change = True;
104
105                 /*
106                  * We should connect as the anonymous user here, in case
107                  * the server has "must change password" checked...
108                  * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
109                  */
110
111                 result = cli_session_setup_anon(cli);
112
113                 if (!NT_STATUS_IS_OK(result)) {
114                         if (asprintf(err_str, "machine %s rejected the session "
115                                  "setup. Error was : %s.\n",        
116                                  remote_machine, nt_errstr(result)) == -1) {
117                                 *err_str = NULL;
118                         }
119                         cli_shutdown(cli);
120                         return result;
121                 }
122         }
123
124         result = cli_tree_connect(cli, "IPC$", "IPC", NULL);
125         if (!NT_STATUS_IS_OK(result)) {
126                 if (asprintf(err_str, "machine %s rejected the tconX on the "
127                              "IPC$ share. Error was : %s.\n",
128                              remote_machine, nt_errstr(result))) {
129                         *err_str = NULL;
130                 }
131                 cli_shutdown(cli);
132                 return result;
133         }
134
135         /* Try not to give the password away too easily */
136
137         if (!pass_must_change) {
138                 result = cli_rpc_pipe_open_with_creds(cli,
139                                                       &ndr_table_samr,
140                                                       NCACN_NP,
141                                                       DCERPC_AUTH_TYPE_NTLMSSP,
142                                                       DCERPC_AUTH_LEVEL_PRIVACY,
143                                                       remote_machine,
144                                                       creds,
145                                                       &pipe_hnd);
146         } else {
147                 /*
148                  * If the user password must be changed the ntlmssp bind will
149                  * fail the same way as the session setup above did. The
150                  * difference ist that with a pipe bind we don't get a good
151                  * error message, the result will be that the rpc call below
152                  * will just fail. So we do it anonymously, there's no other
153                  * way.
154                  */
155                 result = cli_rpc_pipe_open_noauth(
156                         cli, &ndr_table_samr, &pipe_hnd);
157         }
158
159         if (!NT_STATUS_IS_OK(result)) {
160                 if (lp_client_lanman_auth()) {
161                         /* Use the old RAP method. */
162                         if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
163                                 result = cli_nt_error(cli);
164                                 if (asprintf(err_str, "machine %s rejected the "
165                                          "password change: Error was : %s.\n",
166                                          remote_machine, nt_errstr(result)) == -1) {
167                                         *err_str = NULL;
168                                 }
169                                 cli_shutdown(cli);
170                                 return result;
171                         }
172                 } else {
173                         if (asprintf(err_str, "SAMR connection to machine %s "
174                                  "failed. Error was %s, but LANMAN password "
175                                  "changes are disabled\n",
176                                  remote_machine, nt_errstr(result)) == -1) {
177                                 *err_str = NULL;
178                         }
179                         cli_shutdown(cli);
180                         return result;
181                 }
182         }
183
184         result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
185                                              user_name, new_passwd, old_passwd);
186         if (NT_STATUS_IS_OK(result)) {
187                 /* Great - it all worked! */
188                 cli_shutdown(cli);
189                 return NT_STATUS_OK;
190
191         } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) 
192                      || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
193                 /* it failed, but for reasons such as wrong password, too short etc ... */
194
195                 if (asprintf(err_str, "machine %s rejected the password change: "
196                          "Error was : %s.\n",
197                          remote_machine, get_friendly_nt_error_msg(result)) == -1) {
198                         *err_str = NULL;
199                 }
200                 cli_shutdown(cli);
201                 return result;
202         }
203
204         /* OK, that failed, so try again... */
205         TALLOC_FREE(pipe_hnd);
206
207         /* Try anonymous NTLMSSP... */
208         result = NT_STATUS_UNSUCCESSFUL;
209
210         /* OK, this is ugly, but... try an anonymous pipe. */
211         result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
212                                           &pipe_hnd);
213
214         if ( NT_STATUS_IS_OK(result) &&
215                 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
216                                          pipe_hnd, talloc_tos(), user_name,
217                                          new_passwd, old_passwd)))) {
218                 /* Great - it all worked! */
219                 cli_shutdown(cli);
220                 return NT_STATUS_OK;
221         } else {
222                 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) 
223                       || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
224                         /* it failed, but again it was due to things like new password too short */
225
226                         if (asprintf(err_str, "machine %s rejected the "
227                                  "(anonymous) password change: Error was : "
228                                  "%s.\n", remote_machine,
229                                  get_friendly_nt_error_msg(result)) == -1) {
230                                 *err_str = NULL;
231                         }
232                         cli_shutdown(cli);
233                         return result;
234                 }
235
236                 /* We have failed to change the user's password, and we think the server
237                    just might not support SAMR password changes, so fall back */
238
239                 if (lp_client_lanman_auth()) {
240                         /* Use the old RAP method. */
241                         if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
242                                 /* SAMR failed, but the old LanMan protocol worked! */
243
244                                 cli_shutdown(cli);
245                                 return NT_STATUS_OK;
246                         }
247
248                         result = cli_nt_error(cli);
249                         if (asprintf(err_str, "machine %s rejected the password "
250                                  "change: Error was : %s.\n",
251                                  remote_machine, nt_errstr(result)) == -1) {
252                                 *err_str = NULL;
253                         }
254                         cli_shutdown(cli);
255                         return result;
256                 } else {
257                         if (asprintf(err_str, "SAMR connection to machine %s "
258                                  "failed. Error was %s, but LANMAN password "
259                                  "changes are disabled\n",
260                                 remote_machine, nt_errstr(result)) == -1) {
261                                 *err_str = NULL;
262                         }
263                         cli_shutdown(cli);
264                         return NT_STATUS_UNSUCCESSFUL;
265                 }
266         }
267 }