ctdb-scripts: Ignore shellcheck SC2181 warning (use of $?)
[metze/samba/wip.git] / source3 / libsmb / passchange.c
index f5057ec73b8032fae1945796a56177dfe791baa4..c89b7ca85d19f0ec80ed0aec0ba9fd883c3eb837 100644 (file)
@@ -2,86 +2,85 @@
    Unix SMB/CIFS implementation.
    SMB client password change routine
    Copyright (C) Andrew Tridgell 1994-1998
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "../librpc/gen_ndr/ndr_samr.h"
+#include "rpc_client/cli_pipe.h"
+#include "rpc_client/cli_samr.h"
+#include "libsmb/libsmb.h"
+#include "libsmb/clirap.h"
+#include "libsmb/nmblib.h"
+#include "../libcli/smb/smbXcli_base.h"
 
 /*************************************************************
  Change a password on a remote machine using IPC calls.
 *************************************************************/
 
 NTSTATUS remote_password_change(const char *remote_machine, const char *user_name, 
-                           const char *old_passwd, const char *new_passwd,
-                           char *err_str, size_t err_str_len)
+                               const char *old_passwd, const char *new_passwd,
+                               char **err_str)
 {
-       struct nmb_name calling, called;
-       struct cli_state *cli;
-       struct rpc_pipe_client *pipe_hnd;
-       struct sockaddr_storage ss;
-
+       struct cli_state *cli = NULL;
+       struct cli_credentials *creds = NULL;
+       struct rpc_pipe_client *pipe_hnd = NULL;
        NTSTATUS result;
        bool pass_must_change = False;
 
-       *err_str = '\0';
-
-       if(!resolve_name( remote_machine, &ss, 0x20)) {
-               slprintf(err_str, err_str_len-1, "Unable to find an IP address for machine %s.\n",
-                       remote_machine );
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-       cli = cli_initialise();
-       if (!cli) {
-               return NT_STATUS_NO_MEMORY;
-       }
+       *err_str = NULL;
 
-       result = cli_connect(cli, remote_machine, &ss);
+       result = cli_connect_nb(remote_machine, NULL, 0, 0x20, NULL,
+                               SMB_SIGNING_IPC_DEFAULT, 0, &cli);
        if (!NT_STATUS_IS_OK(result)) {
-               slprintf(err_str, err_str_len-1, "Unable to connect to SMB server on machine %s. Error was : %s.\n",
-                       remote_machine, nt_errstr(result) );
-               cli_shutdown(cli);
-               return result;
-       }
-  
-       make_nmb_name(&calling, global_myname() , 0x0);
-       make_nmb_name(&called , remote_machine, 0x20);
-       
-       if (!cli_session_request(cli, &calling, &called)) {
-               slprintf(err_str, err_str_len-1, "machine %s rejected the session setup. Error was : %s.\n",
-                       remote_machine, cli_errstr(cli) );
-               result = cli_nt_error(cli);
-               cli_shutdown(cli);
+               if (asprintf(err_str, "Unable to connect to SMB server on "
+                        "machine %s. Error was : %s.\n",
+                        remote_machine, nt_errstr(result))==-1) {
+                       *err_str = NULL;
+               }
                return result;
        }
-  
-       cli->protocol = PROTOCOL_NT1;
 
-       if (!cli_negprot(cli)) {
-               slprintf(err_str, err_str_len-1, "machine %s rejected the negotiate protocol. Error was : %s.\n",        
-                       remote_machine, cli_errstr(cli) );
-               result = cli_nt_error(cli);
+       creds = cli_session_creds_init(cli,
+                                      user_name,
+                                      NULL, /* domain */
+                                      NULL, /* realm */
+                                      old_passwd,
+                                      false, /* use_kerberos */
+                                      false, /* fallback_after_kerberos */
+                                      false, /* use_ccache */
+                                      false); /* password_is_nt_hash */
+       SMB_ASSERT(creds != NULL);
+
+       result = smbXcli_negprot(cli->conn, cli->timeout,
+                                lp_client_ipc_min_protocol(),
+                                lp_client_ipc_max_protocol());
+
+       if (!NT_STATUS_IS_OK(result)) {
+               if (asprintf(err_str, "machine %s rejected the negotiate "
+                        "protocol. Error was : %s.\n",        
+                        remote_machine, nt_errstr(result)) == -1) {
+                       *err_str = NULL;
+               }
                cli_shutdown(cli);
                return result;
        }
-  
+
        /* Given things like SMB signing, restrict anonymous and the like, 
           try an authenticated connection first */
-       result = cli_session_setup(cli, user_name,
-                                  old_passwd, strlen(old_passwd)+1,
-                                  old_passwd, strlen(old_passwd)+1, "");
+       result = cli_session_setup_creds(cli, creds);
 
        if (!NT_STATUS_IS_OK(result)) {
 
@@ -92,9 +91,10 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
 
                if (!NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) &&
                    !NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED)) {
-                       slprintf(err_str, err_str_len-1, "Could not "
-                                "connect to machine %s: %s\n",
-                                remote_machine, cli_errstr(cli));
+                       if (asprintf(err_str, "Could not connect to machine %s: "
+                                "%s\n", remote_machine, nt_errstr(result)) == -1) {
+                               *err_str = NULL;
+                       }
                        cli_shutdown(cli);
                        return result;
                }
@@ -107,24 +107,26 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
                 * Thanks to <Nicholas.S.Jenkins@cdc.com> for this fix.
                 */
 
-               result = cli_session_setup(cli, "", "", 0, "", 0, "");
+               result = cli_session_setup_anon(cli);
 
                if (!NT_STATUS_IS_OK(result)) {
-                       slprintf(err_str, err_str_len-1, "machine %s rejected the session setup. Error was : %s.\n",        
-                                remote_machine, cli_errstr(cli) );
+                       if (asprintf(err_str, "machine %s rejected the session "
+                                "setup. Error was : %s.\n",        
+                                remote_machine, nt_errstr(result)) == -1) {
+                               *err_str = NULL;
+                       }
                        cli_shutdown(cli);
                        return result;
                }
-
-               cli_init_creds(cli, "", "", NULL);
-       } else {
-               cli_init_creds(cli, user_name, "", old_passwd);
        }
 
-       if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
-               slprintf(err_str, err_str_len-1, "machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
-                       remote_machine, cli_errstr(cli) );
-               result = cli_nt_error(cli);
+       result = cli_tree_connect(cli, "IPC$", "IPC", NULL);
+       if (!NT_STATUS_IS_OK(result)) {
+               if (asprintf(err_str, "machine %s rejected the tconX on the "
+                            "IPC$ share. Error was : %s.\n",
+                            remote_machine, nt_errstr(result))) {
+                       *err_str = NULL;
+               }
                cli_shutdown(cli);
                return result;
        }
@@ -132,13 +134,14 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
        /* Try not to give the password away too easily */
 
        if (!pass_must_change) {
-               pipe_hnd = cli_rpc_pipe_open_ntlmssp(cli,
-                                               PI_SAMR,
-                                               PIPE_AUTH_LEVEL_PRIVACY,
-                                               "", /* what domain... ? */
-                                               user_name,
-                                               old_passwd,
-                                               &result);
+               result = cli_rpc_pipe_open_with_creds(cli,
+                                                     &ndr_table_samr,
+                                                     NCACN_NP,
+                                                     DCERPC_AUTH_TYPE_NTLMSSP,
+                                                     DCERPC_AUTH_LEVEL_PRIVACY,
+                                                     remote_machine,
+                                                     creds,
+                                                     &pipe_hnd);
        } else {
                /*
                 * If the user password must be changed the ntlmssp bind will
@@ -148,32 +151,38 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
                 * will just fail. So we do it anonymously, there's no other
                 * way.
                 */
-               pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &result);
+               result = cli_rpc_pipe_open_noauth(
+                       cli, &ndr_table_samr, &pipe_hnd);
        }
 
-       if (!pipe_hnd) {
+       if (!NT_STATUS_IS_OK(result)) {
                if (lp_client_lanman_auth()) {
                        /* Use the old RAP method. */
                        if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
-                               slprintf(err_str, err_str_len-1, "machine %s rejected the password change: Error was : %s.\n",
-                                        remote_machine, cli_errstr(cli) );
                                result = cli_nt_error(cli);
+                               if (asprintf(err_str, "machine %s rejected the "
+                                        "password change: Error was : %s.\n",
+                                        remote_machine, nt_errstr(result)) == -1) {
+                                       *err_str = NULL;
+                               }
                                cli_shutdown(cli);
                                return result;
                        }
                } else {
-                       slprintf(err_str, err_str_len-1,
-                               "SAMR connection to machine %s failed. Error was %s, "
-                               "but LANMAN password changed are disabled\n",
-                               nt_errstr(result), remote_machine);
-                       result = cli_nt_error(cli);
+                       if (asprintf(err_str, "SAMR connection to machine %s "
+                                "failed. Error was %s, but LANMAN password "
+                                "changes are disabled\n",
+                                remote_machine, nt_errstr(result)) == -1) {
+                               *err_str = NULL;
+                       }
                        cli_shutdown(cli);
                        return result;
                }
        }
 
-       if (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user(pipe_hnd, cli->mem_ctx, user_name, 
-                                                            new_passwd, old_passwd))) {
+       result = rpccli_samr_chgpasswd_user2(pipe_hnd, talloc_tos(),
+                                            user_name, new_passwd, old_passwd);
+       if (NT_STATUS_IS_OK(result)) {
                /* Great - it all worked! */
                cli_shutdown(cli);
                return NT_STATUS_OK;
@@ -181,30 +190,30 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
        } else if (!(NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) 
                     || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
                /* it failed, but for reasons such as wrong password, too short etc ... */
-               
-               slprintf(err_str, err_str_len-1, "machine %s rejected the password change: Error was : %s.\n",
-                        remote_machine, get_friendly_nt_error_msg(result));
+
+               if (asprintf(err_str, "machine %s rejected the password change: "
+                        "Error was : %s.\n",
+                        remote_machine, get_friendly_nt_error_msg(result)) == -1) {
+                       *err_str = NULL;
+               }
                cli_shutdown(cli);
                return result;
        }
 
        /* OK, that failed, so try again... */
-       cli_rpc_pipe_close(pipe_hnd);
-       
+       TALLOC_FREE(pipe_hnd);
+
        /* Try anonymous NTLMSSP... */
-       cli_init_creds(cli, "", "", NULL);
-       
        result = NT_STATUS_UNSUCCESSFUL;
-       
+
        /* OK, this is ugly, but... try an anonymous pipe. */
-       pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &result);
-
-       if ( pipe_hnd &&
-               (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user(pipe_hnd,
-                                               cli->mem_ctx,
-                                               user_name, 
-                                               new_passwd,
-                                               old_passwd)))) {
+       result = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr,
+                                         &pipe_hnd);
+
+       if ( NT_STATUS_IS_OK(result) &&
+               (NT_STATUS_IS_OK(result = rpccli_samr_chgpasswd_user2(
+                                        pipe_hnd, talloc_tos(), user_name,
+                                        new_passwd, old_passwd)))) {
                /* Great - it all worked! */
                cli_shutdown(cli);
                return NT_STATUS_OK;
@@ -213,16 +222,19 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
                      || NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL))) {
                        /* it failed, but again it was due to things like new password too short */
 
-                       slprintf(err_str, err_str_len-1, 
-                                "machine %s rejected the (anonymous) password change: Error was : %s.\n",
-                                remote_machine, get_friendly_nt_error_msg(result));
+                       if (asprintf(err_str, "machine %s rejected the "
+                                "(anonymous) password change: Error was : "
+                                "%s.\n", remote_machine,
+                                get_friendly_nt_error_msg(result)) == -1) {
+                               *err_str = NULL;
+                       }
                        cli_shutdown(cli);
                        return result;
                }
-               
+
                /* We have failed to change the user's password, and we think the server
                   just might not support SAMR password changes, so fall back */
-               
+
                if (lp_client_lanman_auth()) {
                        /* Use the old RAP method. */
                        if (cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
@@ -231,17 +243,22 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
                                cli_shutdown(cli);
                                return NT_STATUS_OK;
                        }
-                       slprintf(err_str, err_str_len-1, 
-                                "machine %s rejected the password change: Error was : %s.\n",
-                                remote_machine, cli_errstr(cli) );
+
                        result = cli_nt_error(cli);
+                       if (asprintf(err_str, "machine %s rejected the password "
+                                "change: Error was : %s.\n",
+                                remote_machine, nt_errstr(result)) == -1) {
+                               *err_str = NULL;
+                       }
                        cli_shutdown(cli);
                        return result;
                } else {
-                       slprintf(err_str, err_str_len-1,
-                               "SAMR connection to machine %s failed. Error was %s, "
-                               "but LANMAN password changed are disabled\n",
-                               nt_errstr(result), remote_machine);
+                       if (asprintf(err_str, "SAMR connection to machine %s "
+                                "failed. Error was %s, but LANMAN password "
+                                "changes are disabled\n",
+                               remote_machine, nt_errstr(result)) == -1) {
+                               *err_str = NULL;
+                       }
                        cli_shutdown(cli);
                        return NT_STATUS_UNSUCCESSFUL;
                }