s3-net: display full value of "msDS-SupportedEncryptionTypes".
[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, const char *user_name, 
34                                 const char *old_passwd, const char *new_passwd,
35                                 char **err_str)
36 {
37         struct cli_state *cli = NULL;
38         struct rpc_pipe_client *pipe_hnd = NULL;
39         char *user, *domain, *p;
40
41         NTSTATUS result;
42         bool pass_must_change = False;
43
44         user = talloc_strdup(talloc_tos(), user_name);
45         SMB_ASSERT(user != NULL);
46         domain = talloc_strdup(talloc_tos(), "");
47         SMB_ASSERT(domain != NULL);
48
49         /* allow usernames of the form domain\\user or domain/user */
50         if ((p = strchr_m(user,'\\')) || (p = strchr_m(user,'/')) ||
51             (p = strchr_m(user,*lp_winbind_separator()))) {
52                 *p = 0;
53                 domain = user;
54                 user = p+1;
55         }
56
57         *err_str = NULL;
58
59         result = cli_connect_nb(remote_machine, NULL, 0, 0x20, NULL,
60                                 SMB_SIGNING_DEFAULT, 0, &cli);
61         if (!NT_STATUS_IS_OK(result)) {
62                 if (asprintf(err_str, "Unable to connect to SMB server on "
63                          "machine %s. Error was : %s.\n",
64                          remote_machine, nt_errstr(result))==-1) {
65                         *err_str = NULL;
66                 }
67                 return result;
68         }
69
70         result = smbXcli_negprot(cli->conn, cli->timeout, PROTOCOL_CORE,
71                                  PROTOCOL_NT1);
72
73         if (!NT_STATUS_IS_OK(result)) {
74                 if (asprintf(err_str, "machine %s rejected the negotiate "
75                          "protocol. Error was : %s.\n",        
76                          remote_machine, nt_errstr(result)) == -1) {
77                         *err_str = NULL;
78                 }
79                 cli_shutdown(cli);
80                 return result;
81         }
82
83         /* Given things like SMB signing, restrict anonymous and the like, 
84            try an authenticated connection first */
85         result = cli_session_setup(cli, user_name,
86                                    old_passwd, strlen(old_passwd)+1,
87                                    old_passwd, strlen(old_passwd)+1, "");
88
89         if (!NT_STATUS_IS_OK(result)) {
90
91                 /* Password must change or Password expired are the only valid
92                  * error conditions here from where we can proceed, the rest like
93                  * account locked out or logon failure will lead to errors later
94                  * anyway */
95
96                 if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
97                     !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
98                         if (asprintf(err_str, "Could not connect to machine %s: "
99                                  "%s\n", remote_machine, nt_errstr(result)) == -1) {
100                                 *err_str = NULL;
101                         }
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                         if (asprintf(err_str, "machine %s rejected the session "
118                                  "setup. Error was : %s.\n",        
119                                  remote_machine, nt_errstr(result)) == -1) {
120                                 *err_str = NULL;
121                         }
122                         cli_shutdown(cli);
123                         return result;
124                 }
125
126                 result = cli_init_creds(cli, "", "", NULL);
127                 if (!NT_STATUS_IS_OK(result)) {
128                         cli_shutdown(cli);
129                         return result;
130                 }
131         } else {
132                 result = cli_init_creds(cli, user, domain, old_passwd);
133                 if (!NT_STATUS_IS_OK(result)) {
134                         cli_shutdown(cli);
135                         return result;
136                 }
137         }
138
139         result = cli_tree_connect(cli, "IPC$", "IPC", "", 1);
140         if (!NT_STATUS_IS_OK(result)) {
141                 if (asprintf(err_str, "machine %s rejected the tconX on the "
142                              "IPC$ share. Error was : %s.\n",
143                              remote_machine, nt_errstr(result))) {
144                         *err_str = NULL;
145                 }
146                 cli_shutdown(cli);
147                 return result;
148         }
149
150         /* Try not to give the password away too easily */
151
152         if (!pass_must_change) {
153                 result = cli_rpc_pipe_open_generic_auth(cli,
154                                                         &ndr_table_samr,
155                                                         NCACN_NP,
156                                                         CRED_DONT_USE_KERBEROS,
157                                                         DCERPC_AUTH_TYPE_NTLMSSP,
158                                                         DCERPC_AUTH_LEVEL_PRIVACY,
159                                                         remote_machine,
160                                                         domain, user,
161                                                         old_passwd,
162                                                         &pipe_hnd);
163         } else {
164                 /*
165                  * If the user password must be changed the ntlmssp bind will
166                  * fail the same way as the session setup above did. The
167                  * difference ist that with a pipe bind we don't get a good
168                  * error message, the result will be that the rpc call below
169                  * will just fail. So we do it anonymously, there's no other
170                  * way.
171                  */
172                 result = cli_rpc_pipe_open_noauth(
173                         cli, &ndr_table_samr, &pipe_hnd);
174         }
175
176         if (!NT_STATUS_IS_OK(result)) {
177                 if (lp_client_lanman_auth()) {
178                         /* Use the old RAP method. */
179                         if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
180                                 result = cli_nt_error(cli);
181                                 if (asprintf(err_str, "machine %s rejected the "
182                                          "password change: Error was : %s.\n",
183                                          remote_machine, nt_errstr(result)) == -1) {
184                                         *err_str = NULL;
185                                 }
186                                 cli_shutdown(cli);
187                                 return result;
188                         }
189                 } else {
190                         if (asprintf(err_str, "SAMR connection to machine %s "
191                                  "failed. Error was %s, but LANMAN password "
192                                  "changes are disabled\n",
193                                  remote_machine, nt_errstr(result)) == -1) {
194                                 *err_str = NULL;
195                         }
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         result = cli_init_creds(cli, "", "", NULL);
226         if (!NT_STATUS_IS_OK(result)) {
227                 cli_shutdown(cli);
228                 return result;
229         }
230
231         result = NT_STATUS_UNSUCCESSFUL;
232
233         /* OK, this is ugly, but... try an anonymous pipe. */
234         result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
235                                           &pipe_hnd);
236
237         if ( NT_STATUS_IS_OK(result) &&
238                 (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
239                                          pipe_hnd, talloc_tos(), user_name,
240                                          new_passwd, old_passwd)))) {
241                 /* Great - it all worked! */
242                 cli_shutdown(cli);
243                 return NT_STATUS_OK;
244         } else {
245                 if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) 
246                       || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
247                         /* it failed, but again it was due to things like new password too short */
248
249                         if (asprintf(err_str, "machine %s rejected the "
250                                  "(anonymous) password change: Error was : "
251                                  "%s.\n", remote_machine,
252                                  get_friendly_nt_error_msg(result)) == -1) {
253                                 *err_str = NULL;
254                         }
255                         cli_shutdown(cli);
256                         return result;
257                 }
258
259                 /* We have failed to change the user's password, and we think the server
260                    just might not support SAMR password changes, so fall back */
261
262                 if (lp_client_lanman_auth()) {
263                         /* Use the old RAP method. */
264                         if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
265                                 /* SAMR failed, but the old LanMan protocol worked! */
266
267                                 cli_shutdown(cli);
268                                 return NT_STATUS_OK;
269                         }
270
271                         result = cli_nt_error(cli);
272                         if (asprintf(err_str, "machine %s rejected the password "
273                                  "change: Error was : %s.\n",
274                                  remote_machine, nt_errstr(result)) == -1) {
275                                 *err_str = NULL;
276                         }
277                         cli_shutdown(cli);
278                         return result;
279                 } else {
280                         if (asprintf(err_str, "SAMR connection to machine %s "
281                                  "failed. Error was %s, but LANMAN password "
282                                  "changes are disabled\n",
283                                 nt_errstr(result), remote_machine) == -1) {
284                                 *err_str = NULL;
285                         }
286                         cli_shutdown(cli);
287                         return NT_STATUS_UNSUCCESSFUL;
288                 }
289         }
290 }