| 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 | if (!cli_negprot(cli)) {
|
|---|
| 75 | asprintf(err_str, "machine %s rejected the negotiate "
|
|---|
| 76 | "protocol. Error was : %s.\n",
|
|---|
| 77 | remote_machine, cli_errstr(cli) );
|
|---|
| 78 | result = cli_nt_error(cli);
|
|---|
| 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 | asprintf(err_str, "Could not connect to machine %s: "
|
|---|
| 99 | "%s\n", remote_machine, cli_errstr(cli));
|
|---|
| 100 | cli_shutdown(cli);
|
|---|
| 101 | return result;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | pass_must_change = True;
|
|---|
| 105 |
|
|---|
| 106 | /*
|
|---|
| 107 | * We should connect as the anonymous user here, in case
|
|---|
| 108 | * the server has "must change password" checked...
|
|---|
| 109 | * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
|
|---|
| 110 | */
|
|---|
| 111 |
|
|---|
| 112 | result = cli_session_setup(cli, "", "", 0, "", 0, "");
|
|---|
| 113 |
|
|---|
| 114 | if (!NT_STATUS_IS_OK(result)) {
|
|---|
| 115 | asprintf(err_str, "machine %s rejected the session "
|
|---|
| 116 | "setup. Error was : %s.\n",
|
|---|
| 117 | remote_machine, cli_errstr(cli) );
|
|---|
| 118 | cli_shutdown(cli);
|
|---|
| 119 | return result;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | cli_init_creds(cli, "", "", NULL);
|
|---|
| 123 | } else {
|
|---|
| 124 | cli_init_creds(cli, user_name, "", old_passwd);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
|
|---|
| 128 | asprintf(err_str, "machine %s rejected the tconX on the IPC$ "
|
|---|
| 129 | "share. Error was : %s.\n",
|
|---|
| 130 | remote_machine, cli_errstr(cli) );
|
|---|
| 131 | result = cli_nt_error(cli);
|
|---|
| 132 | cli_shutdown(cli);
|
|---|
| 133 | return result;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /* Try not to give the password away too easily */
|
|---|
| 137 |
|
|---|
| 138 | if (!pass_must_change) {
|
|---|
| 139 | pipe_hnd = cli_rpc_pipe_open_ntlmssp(cli,
|
|---|
| 140 | PI_SAMR,
|
|---|
| 141 | PIPE_AUTH_LEVEL_PRIVACY,
|
|---|
| 142 | "", /* what domain... ? */
|
|---|
| 143 | user_name,
|
|---|
| 144 | old_passwd,
|
|---|
| 145 | &result);
|
|---|
| 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 | pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &result);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | if (!pipe_hnd) {
|
|---|
| 159 | if (lp_client_lanman_auth()) {
|
|---|
| 160 | /* Use the old RAP method. */
|
|---|
| 161 | if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
|
|---|
| 162 | asprintf(err_str, "machine %s rejected the "
|
|---|
| 163 | "password change: Error was : %s.\n",
|
|---|
| 164 | remote_machine, cli_errstr(cli) );
|
|---|
| 165 | result = cli_nt_error(cli);
|
|---|
| 166 | cli_shutdown(cli);
|
|---|
| 167 | return result;
|
|---|
| 168 | }
|
|---|
| 169 | } else {
|
|---|
| 170 | asprintf(err_str, "SAMR connection to machine %s "
|
|---|
| 171 | "failed. Error was %s, but LANMAN password "
|
|---|
| 172 | "changes are disabled\n",
|
|---|
| 173 | nt_errstr(result), remote_machine);
|
|---|
| 174 | result = cli_nt_error(cli);
|
|---|
| 175 | cli_shutdown(cli);
|
|---|
| 176 | return result;
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | if (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user(pipe_hnd, pipe_hnd->mem_ctx, user_name,
|
|---|
| 181 | new_passwd, old_passwd))) {
|
|---|
| 182 | /* Great - it all worked! */
|
|---|
| 183 | cli_shutdown(cli);
|
|---|
| 184 | return NT_STATUS_OK;
|
|---|
| 185 |
|
|---|
| 186 | } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
|
|---|
| 187 | || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
|
|---|
| 188 | /* it failed, but for reasons such as wrong password, too short etc ... */
|
|---|
| 189 |
|
|---|
| 190 | asprintf(err_str, "machine %s rejected the password change: "
|
|---|
| 191 | "Error was : %s.\n",
|
|---|
| 192 | remote_machine, get_friendly_nt_error_msg(result));
|
|---|
| 193 | cli_shutdown(cli);
|
|---|
| 194 | return result;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | /* OK, that failed, so try again... */
|
|---|
| 198 | cli_rpc_pipe_close(pipe_hnd);
|
|---|
| 199 |
|
|---|
| 200 | /* Try anonymous NTLMSSP... */
|
|---|
| 201 | cli_init_creds(cli, "", "", NULL);
|
|---|
| 202 |
|
|---|
| 203 | result = NT_STATUS_UNSUCCESSFUL;
|
|---|
| 204 |
|
|---|
| 205 | /* OK, this is ugly, but... try an anonymous pipe. */
|
|---|
| 206 | pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &result);
|
|---|
| 207 |
|
|---|
| 208 | if ( pipe_hnd &&
|
|---|
| 209 | (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user(pipe_hnd,
|
|---|
| 210 | pipe_hnd->mem_ctx,
|
|---|
| 211 | user_name,
|
|---|
| 212 | new_passwd,
|
|---|
| 213 | old_passwd)))) {
|
|---|
| 214 | /* Great - it all worked! */
|
|---|
| 215 | cli_shutdown(cli);
|
|---|
| 216 | return NT_STATUS_OK;
|
|---|
| 217 | } else {
|
|---|
| 218 | if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)
|
|---|
| 219 | || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
|
|---|
| 220 | /* it failed, but again it was due to things like new password too short */
|
|---|
| 221 |
|
|---|
| 222 | asprintf(err_str, "machine %s rejected the "
|
|---|
| 223 | "(anonymous) password change: Error was : "
|
|---|
| 224 | "%s.\n", remote_machine,
|
|---|
| 225 | get_friendly_nt_error_msg(result));
|
|---|
| 226 | cli_shutdown(cli);
|
|---|
| 227 | return result;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | /* We have failed to change the user's password, and we think the server
|
|---|
| 231 | just might not support SAMR password changes, so fall back */
|
|---|
| 232 |
|
|---|
| 233 | if (lp_client_lanman_auth()) {
|
|---|
| 234 | /* Use the old RAP method. */
|
|---|
| 235 | if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
|
|---|
| 236 | /* SAMR failed, but the old LanMan protocol worked! */
|
|---|
| 237 |
|
|---|
| 238 | cli_shutdown(cli);
|
|---|
| 239 | return NT_STATUS_OK;
|
|---|
| 240 | }
|
|---|
| 241 | asprintf(err_str, "machine %s rejected the password "
|
|---|
| 242 | "change: Error was : %s.\n",
|
|---|
| 243 | remote_machine, cli_errstr(cli) );
|
|---|
| 244 | result = cli_nt_error(cli);
|
|---|
| 245 | cli_shutdown(cli);
|
|---|
| 246 | return result;
|
|---|
| 247 | } else {
|
|---|
| 248 | asprintf(err_str, "SAMR connection to machine %s "
|
|---|
| 249 | "failed. Error was %s, but LANMAN password "
|
|---|
| 250 | "changed are disabled\n",
|
|---|
| 251 | nt_errstr(result), remote_machine);
|
|---|
| 252 | cli_shutdown(cli);
|
|---|
| 253 | return NT_STATUS_UNSUCCESSFUL;
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 | }
|
|---|