s3-libsmb/passchange.c: remove some cli_nt_error() calls
[gd/samba-autobuild/.git] / source3 / libsmb / passchange.c
index 7747062d4b3342bd35ee19c5b5cd76894bfcfea5..83fd9ebf1fc79aec32c4385152a8515f7b271015 100644 (file)
@@ -2,22 +2,28 @@
    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"
 
 /*************************************************************
  Change a password on a remote machine using IPC calls.
@@ -27,57 +33,40 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
                                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 rpc_pipe_client *pipe_hnd = NULL;
+       char *user, *domain, *p;
 
        NTSTATUS result;
        bool pass_must_change = False;
 
-       *err_str = NULL;
-
-       if(!resolve_name( remote_machine, &ss, 0x20)) {
-               if (asprintf(err_str, "Unable to find an IP address for machine "
-                        "%s.\n", remote_machine) == -1) {
-                       *err_str = NULL;
-               }
-               return NT_STATUS_UNSUCCESSFUL;
+       user = talloc_strdup(talloc_tos(), user_name);
+       SMB_ASSERT(user != NULL);
+       domain = talloc_strdup(talloc_tos(), "");
+       SMB_ASSERT(domain != NULL);
+
+       /* allow usernames of the form domain\\user or domain/user */
+       if ((p = strchr_m(user,'\\')) || (p = strchr_m(user,'/')) ||
+           (p = strchr_m(user,*lp_winbind_separator()))) {
+               *p = 0;
+               domain = user;
+               user = p+1;
        }
 
-       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_DEFAULT, 0, &cli);
        if (!NT_STATUS_IS_OK(result)) {
                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;
                }
-               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)) {
-               if (asprintf(err_str, "machine %s rejected the session setup. "
-                        "Error was : %s.\n",
-                        remote_machine, cli_errstr(cli)) == -1) {
-                       *err_str = NULL;
-               }
-               result = cli_nt_error(cli);
-               cli_shutdown(cli);
-               return result;
-       }
-
-       cli->protocol = PROTOCOL_NT1;
-
-       result = cli_negprot(cli);
+       result = cli_negprot(cli, PROTOCOL_NT1);
 
        if (!NT_STATUS_IS_OK(result)) {
                if (asprintf(err_str, "machine %s rejected the negotiate "
@@ -85,7 +74,6 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
                         remote_machine, nt_errstr(result)) == -1) {
                        *err_str = NULL;
                }
-               result = cli_nt_error(cli);
                cli_shutdown(cli);
                return result;
        }
@@ -106,7 +94,7 @@ 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)) {
                        if (asprintf(err_str, "Could not connect to machine %s: "
-                                "%s\n", remote_machine, cli_errstr(cli)) == -1) {
+                                "%s\n", remote_machine, nt_errstr(result)) == -1) {
                                *err_str = NULL;
                        }
                        cli_shutdown(cli);
@@ -126,7 +114,7 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
                if (!NT_STATUS_IS_OK(result)) {
                        if (asprintf(err_str, "machine %s rejected the session "
                                 "setup. Error was : %s.\n",        
-                                remote_machine, cli_errstr(cli)) == -1) {
+                                remote_machine, nt_errstr(result)) == -1) {
                                *err_str = NULL;
                        }
                        cli_shutdown(cli);
@@ -139,7 +127,7 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
                        return result;
                }
        } else {
-               result = cli_init_creds(cli, user_name, "", old_passwd);
+               result = cli_init_creds(cli, user, domain, old_passwd);
                if (!NT_STATUS_IS_OK(result)) {
                        cli_shutdown(cli);
                        return result;
@@ -162,9 +150,9 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
        if (!pass_must_change) {
                result = cli_rpc_pipe_open_ntlmssp(cli,
                                                   &ndr_table_samr.syntax_id,
-                                                  PIPE_AUTH_LEVEL_PRIVACY,
-                                                  "", /* what domain... ? */
-                                                  user_name,
+                                                  NCACN_NP,
+                                                  DCERPC_AUTH_LEVEL_PRIVACY,
+                                                  domain, user,
                                                   old_passwd,
                                                   &pipe_hnd);
        } else {
@@ -184,23 +172,22 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
                if (lp_client_lanman_auth()) {
                        /* Use the old RAP method. */
                        if (!cli_oem_change_password(cli, user_name, new_passwd, old_passwd)) {
+                               result = cli_nt_error(cli);
                                if (asprintf(err_str, "machine %s rejected the "
                                         "password change: Error was : %s.\n",
-                                        remote_machine, cli_errstr(cli)) == -1) {
+                                        remote_machine, nt_errstr(result)) == -1) {
                                        *err_str = NULL;
                                }
-                               result = cli_nt_error(cli);
                                cli_shutdown(cli);
                                return result;
                        }
                } else {
                        if (asprintf(err_str, "SAMR connection to machine %s "
                                 "failed. Error was %s, but LANMAN password "
-                                "changed are disabled\n",
+                                "changes are disabled\n",
                                 remote_machine, nt_errstr(result)) == -1) {
                                *err_str = NULL;
                        }
-                       result = cli_nt_error(cli);
                        cli_shutdown(cli);
                        return result;
                }
@@ -275,18 +262,19 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
                                cli_shutdown(cli);
                                return NT_STATUS_OK;
                        }
+
+                       result = cli_nt_error(cli);
                        if (asprintf(err_str, "machine %s rejected the password "
                                 "change: Error was : %s.\n",
-                                remote_machine, cli_errstr(cli)) == -1) {
+                                remote_machine, nt_errstr(result)) == -1) {
                                *err_str = NULL;
                        }
-                       result = cli_nt_error(cli);
                        cli_shutdown(cli);
                        return result;
                } else {
                        if (asprintf(err_str, "SAMR connection to machine %s "
                                 "failed. Error was %s, but LANMAN password "
-                                "changed are disabled\n",
+                                "changes are disabled\n",
                                nt_errstr(result), remote_machine) == -1) {
                                *err_str = NULL;
                        }