s3:libsmb: get rid of cli_state_*_sockaddr
[samba.git] / source3 / utils / net_rpc.c
index 671f7e81e9cd83adf76d1bb9f205ddeaa7ffd473..3196b4f8719c9c28228da9ce6ced43bab64d526e 100644 (file)
 
 #include "includes.h"
 #include "utils/net.h"
+#include "rpc_client/cli_pipe.h"
 #include "../libcli/auth/libcli_auth.h"
-#include "../librpc/gen_ndr/cli_samr.h"
+#include "../librpc/gen_ndr/ndr_samr_c.h"
 #include "rpc_client/cli_samr.h"
 #include "rpc_client/init_samr.h"
-#include "../librpc/gen_ndr/cli_lsa.h"
+#include "../librpc/gen_ndr/ndr_lsa_c.h"
 #include "rpc_client/cli_lsarpc.h"
-#include "../librpc/gen_ndr/cli_netlogon.h"
-#include "../librpc/gen_ndr/cli_srvsvc.h"
-#include "../librpc/gen_ndr/cli_spoolss.h"
-#include "../librpc/gen_ndr/cli_initshutdown.h"
-#include "../librpc/gen_ndr/cli_winreg.h"
+#include "../librpc/gen_ndr/ndr_netlogon_c.h"
+#include "../librpc/gen_ndr/ndr_srvsvc_c.h"
+#include "../librpc/gen_ndr/ndr_spoolss.h"
+#include "../librpc/gen_ndr/ndr_initshutdown_c.h"
+#include "../librpc/gen_ndr/ndr_winreg_c.h"
 #include "secrets.h"
 #include "lib/netapi/netapi.h"
+#include "lib/netapi/netapi_net.h"
 #include "rpc_client/init_lsa.h"
+#include "../libcli/security/security.h"
+#include "libsmb/libsmb.h"
+#include "libsmb/clirap.h"
+#include "nsswitch/libwbclient/wbclient.h"
+#include "passdb.h"
+#include "libcli/smb/smbXcli_base.h"
 
 static int net_mode_share;
-static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
+static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
 
 /**
  * @file net_rpc.c
@@ -70,30 +78,40 @@ NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 {
        struct rpc_pipe_client *lsa_pipe = NULL;
        struct policy_handle pol;
-       NTSTATUS result = NT_STATUS_OK;
+       NTSTATUS status, result;
        union lsa_PolicyInformation *info = NULL;
+       struct dcerpc_binding_handle *b;
 
-       result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
+       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
                                          &lsa_pipe);
-       if (!NT_STATUS_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
-               return result;
+               return status;
        }
 
-       result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
+       b = lsa_pipe->binding_handle;
+
+       status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
                                     SEC_FLAG_MAXIMUM_ALLOWED,
                                     &pol);
-       if (!NT_STATUS_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, "open_policy %s: %s\n",
                          _("failed"),
-                         nt_errstr(result));
-               return result;
+                         nt_errstr(status));
+               return status;
        }
 
-       result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
+       status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
                                            &pol,
                                            LSA_POLICY_INFO_ACCOUNT_DOMAIN,
-                                           &info);
+                                           &info,
+                                           &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, "lsaquery %s: %s\n",
+                         _("failed"),
+                         nt_errstr(status));
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                d_fprintf(stderr, "lsaquery %s: %s\n",
                          _("failed"),
@@ -104,7 +122,7 @@ NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        *domain_name = info->account_domain.name.string;
        *domain_sid = info->account_domain.sid;
 
-       rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
+       dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
        TALLOC_FREE(lsa_pipe);
 
        return NT_STATUS_OK;
@@ -124,7 +142,7 @@ NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
 int run_rpc_command(struct net_context *c,
                        struct cli_state *cli_arg,
-                       const struct ndr_syntax_id *interface,
+                       const struct ndr_interface_table *table,
                        int conn_flags,
                        rpc_command_fn fn,
                        int argc,
@@ -169,11 +187,11 @@ int run_rpc_command(struct net_context *c,
 
        if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
                if (lp_client_schannel()
-                   && (ndr_syntax_id_equal(interface,
+                   && (ndr_syntax_id_equal(&table->syntax_id,
                                            &ndr_table_netlogon.syntax_id))) {
                        /* Always try and create an schannel netlogon pipe. */
                        nt_status = cli_rpc_pipe_open_schannel(
-                               cli, interface, NCACN_NP,
+                               cli, &table->syntax_id, NCACN_NP,
                                DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
                                &pipe_hnd);
                        if (!NT_STATUS_IS_OK(nt_status)) {
@@ -183,22 +201,23 @@ int run_rpc_command(struct net_context *c,
                        }
                } else {
                        if (conn_flags & NET_FLAGS_SEAL) {
-                               nt_status = cli_rpc_pipe_open_ntlmssp(
-                                       cli, interface,
+                               nt_status = cli_rpc_pipe_open_generic_auth(
+                                       cli, table,
                                        (conn_flags & NET_FLAGS_TCP) ?
                                        NCACN_IP_TCP : NCACN_NP,
+                                       DCERPC_AUTH_TYPE_NTLMSSP,
                                        DCERPC_AUTH_LEVEL_PRIVACY,
+                                       cli_state_remote_name(cli),
                                        lp_workgroup(), c->opt_user_name,
                                        c->opt_password, &pipe_hnd);
                        } else {
                                nt_status = cli_rpc_pipe_open_noauth(
-                                       cli, interface,
+                                       cli, &table->syntax_id,
                                        &pipe_hnd);
                        }
                        if (!NT_STATUS_IS_OK(nt_status)) {
                                DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
-                                         get_pipe_name_from_syntax(
-                                                 talloc_tos(), interface),
+                                         table->name,
                                        nt_errstr(nt_status) ));
                                goto fail;
                        }
@@ -288,7 +307,7 @@ int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
+       return run_rpc_command(c, NULL, &ndr_table_netlogon,
                               NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
                               rpc_changetrustpw_internals,
                               argc, argv);
@@ -334,7 +353,7 @@ static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
                        "error was %s\n",
-                       cli->desthost,
+                       cli_state_remote_name(cli),
                        nt_errstr(result) ));
                return result;
        }
@@ -349,7 +368,7 @@ static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
                sec_channel_type = get_sec_channel_type(NULL);
        }
 
-       fstrcpy(trust_passwd, global_myname());
+       fstrcpy(trust_passwd, lp_netbios_name());
        strlower_m(trust_passwd);
 
        /*
@@ -362,7 +381,7 @@ static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
        E_md4hash(trust_passwd, orig_trust_passwd_hash);
 
        result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
-                                             global_myname(),
+                                             lp_netbios_name(),
                                              orig_trust_passwd_hash,
                                              sec_channel_type);
 
@@ -390,7 +409,7 @@ static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
 
 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
 {
-       return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
+       return run_rpc_command(c, NULL, &ndr_table_netlogon,
                               NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
                               rpc_oldjoin_internals,
                               argc, argv);
@@ -464,10 +483,10 @@ int net_rpc_join(struct net_context *c, int argc, const char **argv)
                return -1;
        }
 
-       if (strlen(global_myname()) > 15) {
+       if (strlen(lp_netbios_name()) > 15) {
                d_printf(_("Our netbios name can be at most 15 chars long, "
                           "\"%s\" is %u chars long\n"),
-                        global_myname(), (unsigned int)strlen(global_myname()));
+                        lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
                return -1;
        }
 
@@ -503,39 +522,60 @@ NTSTATUS rpc_info_internals(struct net_context *c,
                        const char **argv)
 {
        struct policy_handle connect_pol, domain_pol;
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status, result;
        union samr_DomainInfo *info = NULL;
        fstring sid_str;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        sid_to_fstring(sid_str, domain_sid);
 
        /* Get sam policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
+                         nt_errstr(status));
+               goto done;
+       }
+
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
                          nt_errstr(result));
                goto done;
        }
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
-                                       &domain_pol);
+                                       discard_const_p(struct dom_sid2, domain_sid),
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Could not open domain: %s\n"),
+                         nt_errstr(status));
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_fprintf(stderr, _("Could not open domain: %s\n"),
                          nt_errstr(result));
                goto done;
        }
 
-       result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
                                             &domain_pol,
                                             2,
-                                            &info);
+                                            &info,
+                                            &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
+       status = result;
        if (NT_STATUS_IS_OK(result)) {
                d_printf(_("Domain Name: %s\n"),
                         info->general.domain_name.string);
@@ -548,7 +588,7 @@ NTSTATUS rpc_info_internals(struct net_context *c,
        }
 
  done:
-       return result;
+       return status;
 }
 
 /**
@@ -569,7 +609,7 @@ int net_rpc_info(struct net_context *c, int argc, const char **argv)
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
+       return run_rpc_command(c, NULL, &ndr_table_samr,
                               NET_FLAGS_PDC, rpc_info_internals,
                               argc, argv);
 }
@@ -637,7 +677,7 @@ int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
+       return run_rpc_command(c, NULL, &ndr_table_samr,
                               conn_flags,
                               rpc_getsid_internals,
                               argc, argv);
@@ -950,7 +990,7 @@ static int rpc_user_list(struct net_context *c, int argc, const char **argv)
        do {
                uint32_t max_entries, max_size;
 
-               get_query_dispinfo_params(
+               dcerpc_get_query_dispinfo_params(
                        loop_count, &max_entries, &max_size);
 
                status = NetQueryDisplayInformation(c->opt_host,
@@ -1049,7 +1089,7 @@ int net_rpc_user(struct net_context *c, int argc, const char **argv)
                {NULL, NULL, 0, NULL, NULL}
        };
 
-       status = libnetapi_init(&c->netapi_ctx);
+       status = libnetapi_net_init(&c->netapi_ctx);
        if (status != 0) {
                return -1;
        }
@@ -1108,10 +1148,11 @@ static NTSTATUS rpc_sh_handle_user(struct net_context *c,
                                           int argc, const char **argv))
 {
        struct policy_handle connect_pol, domain_pol, user_pol;
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status, result;
        struct dom_sid sid;
        uint32 rid;
        enum lsa_SidType type;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc == 0) {
                d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
@@ -1123,66 +1164,81 @@ static NTSTATUS rpc_sh_handle_user(struct net_context *c,
        ZERO_STRUCT(domain_pol);
        ZERO_STRUCT(user_pol);
 
-       result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
+       status = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
                                     argv[0], NULL, NULL, &sid, &type);
-       if (!NT_STATUS_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
-                         nt_errstr(result));
+                         nt_errstr(status));
                goto done;
        }
 
        if (type != SID_NAME_USER) {
                d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
                          sid_type_lookup(type));
-               result = NT_STATUS_NO_SUCH_USER;
+               status = NT_STATUS_NO_SUCH_USER;
                goto done;
        }
 
        if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
                d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
-               result = NT_STATUS_NO_SUCH_USER;
+               status = NT_STATUS_NO_SUCH_USER;
                goto done;
        }
 
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
                                        ctx->domain_sid,
-                                       &domain_pol);
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
-       result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenUser(b, mem_ctx,
                                      &domain_pol,
                                      MAXIMUM_ALLOWED_ACCESS,
                                      rid,
-                                     &user_pol);
+                                     &user_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
-       result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
+       status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
 
  done:
        if (is_valid_policy_hnd(&user_pol)) {
-               rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
+               dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
        }
        if (is_valid_policy_hnd(&domain_pol)) {
-               rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
+               dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
        }
        if (is_valid_policy_hnd(&connect_pol)) {
-               rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
+               dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
        }
-       return result;
+       return status;
 }
 
 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
@@ -1192,8 +1248,9 @@ static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
                                           struct policy_handle *user_hnd,
                                           int argc, const char **argv)
 {
-       NTSTATUS result;
+       NTSTATUS status, result;
        union samr_UserInfo *info = NULL;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 0) {
                d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
@@ -1201,10 +1258,14 @@ static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
                                           user_hnd,
                                           21,
-                                          &info);
+                                          &info,
+                                          &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
@@ -1244,10 +1305,11 @@ static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
                                               struct policy_handle *user_hnd,
                                               int argc, const char **argv)
 {
-       NTSTATUS result;
+       NTSTATUS status, result;
        const char *username;
        const char *oldval = "";
        union samr_UserInfo *info = NULL;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc > 1) {
                d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
@@ -1255,10 +1317,14 @@ static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
                                           user_hnd,
                                           21,
-                                          &info);
+                                          &info,
+                                          &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
@@ -1290,17 +1356,23 @@ static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
        SETSTR("profilepath", profile_path, PROFILE_PATH);
        SETSTR("description", description, DESCRIPTION);
 
-       result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_SetUserInfo(b, mem_ctx,
                                         user_hnd,
                                         21,
-                                        info);
+                                        info,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       status = result;
 
        d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
                 ctx->thiscmd, oldval, argv[0]);
 
  done:
 
-       return result;
+       return status;
 }
 
 #define HANDLEFLG(name, rec) \
@@ -1329,12 +1401,13 @@ static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
                                                struct policy_handle *user_hnd,
                                                int argc, const char **argv)
 {
-       NTSTATUS result;
+       NTSTATUS status, result;
        const char *username;
        const char *oldval = "unknown";
        uint32 oldflags, newflags;
        bool newval;
        union samr_UserInfo *info = NULL;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if ((argc > 1) ||
            ((argc == 1) && !strequal(argv[0], "yes") &&
@@ -1348,10 +1421,14 @@ static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
 
        newval = strequal(argv[0], "yes");
 
-       result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
                                           user_hnd,
                                           21,
-                                          &info);
+                                          &info,
+                                          &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
@@ -1376,11 +1453,15 @@ static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
        info->info21.acct_flags = newflags;
        info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
 
-       result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_SetUserInfo(b, mem_ctx,
                                         user_hnd,
                                         21,
-                                        info);
-
+                                        info,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
+       status = result;
        if (NT_STATUS_IS_OK(result)) {
                d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
                         ctx->thiscmd, oldval, argv[0]);
@@ -1388,7 +1469,7 @@ static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
 
  done:
 
-       return result;
+       return status;
 }
 
 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
@@ -1407,34 +1488,34 @@ struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
 {
        static struct rpc_sh_cmd cmds[] = {
 
-               { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
+               { "fullname", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
                  N_("Show/Set a user's full name") },
 
-               { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
+               { "homedir", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
                  N_("Show/Set a user's home directory") },
 
-               { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
+               { "homedrive", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
                  N_("Show/Set a user's home drive") },
 
-               { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
+               { "logonscript", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
                  N_("Show/Set a user's logon script") },
 
-               { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
+               { "profilepath", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
                  N_("Show/Set a user's profile path") },
 
-               { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
+               { "description", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
                  N_("Show/Set a user's description") },
 
-               { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
+               { "disabled", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
                  N_("Show/Set whether a user is disabled") },
 
-               { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
+               { "autolock", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
                  N_("Show/Set whether a user locked out") },
 
-               { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
+               { "pwnotreq", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
                  N_("Show/Set whether a user does not need a password") },
 
-               { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
+               { "pwnoexp", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
                  N_("Show/Set whether a user's password does not expire") },
 
                { NULL, NULL, 0, NULL, NULL }
@@ -1449,13 +1530,13 @@ struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
 {
        static struct rpc_sh_cmd cmds[] = {
 
-               { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
+               { "list", NULL, &ndr_table_samr, rpc_sh_user_list,
                  N_("List available users") },
 
-               { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
+               { "info", NULL, &ndr_table_samr, rpc_sh_user_info,
                  N_("List the domain groups a user is member of") },
 
-               { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
+               { "show", NULL, &ndr_table_samr, rpc_sh_user_show,
                  N_("Show info about a user") },
 
                { "edit", net_rpc_user_edit_cmds, 0, NULL,
@@ -1508,12 +1589,13 @@ static NTSTATUS rpc_group_delete_internals(struct net_context *c,
 {
        struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
        bool group_is_primary = false;
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status, result;
        uint32_t group_rid;
-       struct samr_RidTypeArray *rids = NULL;
+       struct samr_RidAttrArray *rids = NULL;
        /* char **names; */
        int i;
        /* struct samr_RidWithAttribute *user_gids; */
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        struct samr_Ids group_rids, name_types;
        struct lsa_String lsa_acct_name;
@@ -1524,64 +1606,98 @@ static NTSTATUS rpc_group_delete_internals(struct net_context *c,
                return NT_STATUS_OK; /* ok? */
        }
 
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+        if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
+               goto done;
+        }
 
         if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
-               goto done;
+               goto done;
         }
 
-        result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
-                                       &domain_pol);
+                                       discard_const_p(struct dom_sid2, domain_sid),
+                                       &domain_pol,
+                                       &result);
+        if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Request open_domain failed\n"));
+               goto done;
+        }
 
         if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_fprintf(stderr, _("Request open_domain failed\n"));
-               goto done;
+               goto done;
         }
 
        init_lsa_String(&lsa_acct_name, argv[0]);
 
-       result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_LookupNames(b, mem_ctx,
                                         &domain_pol,
                                         1,
                                         &lsa_acct_name,
                                         &group_rids,
-                                        &name_types);
+                                        &name_types,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
+               goto done;
+       }
+
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
-               goto done;
+               goto done;
        }
 
        switch (name_types.ids[0])
        {
        case SID_NAME_DOM_GRP:
-               result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_OpenGroup(b, mem_ctx,
                                               &domain_pol,
                                               MAXIMUM_ALLOWED_ACCESS,
                                               group_rids.ids[0],
-                                              &group_pol);
+                                              &group_pol,
+                                              &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_fprintf(stderr, _("Request open_group failed"));
+                       goto done;
+               }
+
                if (!NT_STATUS_IS_OK(result)) {
+                       status = result;
                        d_fprintf(stderr, _("Request open_group failed"));
-                       goto done;
+                       goto done;
                }
 
                group_rid = group_rids.ids[0];
 
-               result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
                                                      &group_pol,
-                                                     &rids);
+                                                     &rids,
+                                                     &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_fprintf(stderr,
+                                 _("Unable to query group members of %s"),
+                                 argv[0]);
+                       goto done;
+               }
 
                if (!NT_STATUS_IS_OK(result)) {
+                       status = result;
                        d_fprintf(stderr,
                                  _("Unable to query group members of %s"),
                                  argv[0]);
-                       goto done;
+                       goto done;
                }
 
                if (c->opt_verbose) {
@@ -1593,31 +1709,48 @@ static NTSTATUS rpc_group_delete_internals(struct net_context *c,
                /* Check if group is anyone's primary group */
                 for (i = 0; i < rids->count; i++)
                {
-                       result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
+                       status = dcerpc_samr_OpenUser(b, mem_ctx,
                                                      &domain_pol,
                                                      MAXIMUM_ALLOWED_ACCESS,
                                                      rids->rids[i],
-                                                     &user_pol);
+                                                     &user_pol,
+                                                     &result);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               d_fprintf(stderr,
+                                       _("Unable to open group member %d\n"),
+                                       rids->rids[i]);
+                               goto done;
+                       }
 
-                       if (!NT_STATUS_IS_OK(result)) {
+                       if (!NT_STATUS_IS_OK(result)) {
+                               status = result;
                                d_fprintf(stderr,
                                        _("Unable to open group member %d\n"),
                                        rids->rids[i]);
-                               goto done;
-                       }
+                               goto done;
+                       }
 
-                       result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
+                       status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
                                                           &user_pol,
                                                           21,
-                                                          &info);
+                                                          &info,
+                                                          &result);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               d_fprintf(stderr,
+                                       _("Unable to lookup userinfo for group "
+                                         "member %d\n"),
+                                       rids->rids[i]);
+                               goto done;
+                       }
 
-                       if (!NT_STATUS_IS_OK(result)) {
+                       if (!NT_STATUS_IS_OK(result)) {
+                               status = result;
                                d_fprintf(stderr,
                                        _("Unable to lookup userinfo for group "
                                          "member %d\n"),
                                        rids->rids[i]);
-                               goto done;
-                       }
+                               goto done;
+                       }
 
                        if (info->info21.primary_gid == group_rid) {
                                if (c->opt_verbose) {
@@ -1628,14 +1761,14 @@ static NTSTATUS rpc_group_delete_internals(struct net_context *c,
                                group_is_primary = true;
                         }
 
-                       rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
+                       dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
                }
 
                if (group_is_primary) {
                        d_fprintf(stderr, _("Unable to delete group because "
                                 "some of it's members have it as primary "
                                 "group\n"));
-                       result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
+                       status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
                        goto done;
                }
 
@@ -1645,10 +1778,14 @@ static NTSTATUS rpc_group_delete_internals(struct net_context *c,
                        if (c->opt_verbose)
                                d_printf(_("Remove group member %d..."),
                                        rids->rids[i]);
-                       result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
+                       status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
                                                               &group_pol,
-                                                              rids->rids[i]);
-
+                                                              rids->rids[i],
+                                                              &result);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               goto done;
+                       }
+                       status = result;
                        if (NT_STATUS_IS_OK(result)) {
                                if (c->opt_verbose)
                                        d_printf(_("ok\n"));
@@ -1659,51 +1796,69 @@ static NTSTATUS rpc_group_delete_internals(struct net_context *c,
                        }
                }
 
-               result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
-                                                      &group_pol);
+               status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
+                                                      &group_pol,
+                                                      &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       break;
+               }
+
+               status = result;
 
                break;
        /* removing a local group is easier... */
        case SID_NAME_ALIAS:
-               result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_OpenAlias(b, mem_ctx,
                                               &domain_pol,
                                               MAXIMUM_ALLOWED_ACCESS,
                                               group_rids.ids[0],
-                                              &group_pol);
-
+                                              &group_pol,
+                                              &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_fprintf(stderr, _("Request open_alias failed\n"));
+                       goto done;
+               }
                if (!NT_STATUS_IS_OK(result)) {
+                       status = result;
                        d_fprintf(stderr, _("Request open_alias failed\n"));
-                       goto done;
+                       goto done;
                }
 
-               result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
-                                                   &group_pol);
+               status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
+                                                   &group_pol,
+                                                   &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       break;
+               }
+
+               status = result;
+
                break;
        default:
                d_fprintf(stderr, _("%s is of type %s. This command is only "
                                    "for deleting local or global groups\n"),
                        argv[0],sid_type_lookup(name_types.ids[0]));
-               result = NT_STATUS_UNSUCCESSFUL;
+               status = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
 
-       if (NT_STATUS_IS_OK(result)) {
+       if (NT_STATUS_IS_OK(status)) {
                if (c->opt_verbose)
                        d_printf(_("Deleted %s '%s'\n"),
                                 sid_type_lookup(name_types.ids[0]), argv[0]);
        } else {
                d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
-                       get_friendly_nt_error_msg(result));
+                       get_friendly_nt_error_msg(status));
        }
 
  done:
-       return result;
+       return status;
 
 }
 
 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
 {
-       return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_samr, 0,
                               rpc_group_delete_internals, argc,argv);
 }
 
@@ -1791,37 +1946,40 @@ static NTSTATUS get_sid_from_name(struct cli_state *cli,
        enum lsa_SidType *types = NULL;
        struct rpc_pipe_client *pipe_hnd = NULL;
        struct policy_handle lsa_pol;
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status, result;
+       struct dcerpc_binding_handle *b;
 
-       result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
+       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
                                          &pipe_hnd);
-       if (!NT_STATUS_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(status)) {
                goto done;
        }
 
-       result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
+       b = pipe_hnd->binding_handle;
+
+       status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
                                     SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
 
-       if (!NT_STATUS_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(status)) {
                goto done;
        }
 
-       result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
+       status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
                                      &name, NULL, 1, &sids, &types);
 
-       if (NT_STATUS_IS_OK(result)) {
+       if (NT_STATUS_IS_OK(status)) {
                sid_copy(sid, &sids[0]);
                *type = types[0];
        }
 
-       rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
+       dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
 
  done:
        if (pipe_hnd) {
                TALLOC_FREE(pipe_hnd);
        }
 
-       if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
+       if (!NT_STATUS_IS_OK(status) && (strncasecmp_m(name, "S-", 2) == 0)) {
 
                /* Try as S-1-5-whatever */
 
@@ -1830,11 +1988,11 @@ static NTSTATUS get_sid_from_name(struct cli_state *cli,
                if (string_to_sid(&tmp_sid, name)) {
                        sid_copy(sid, &tmp_sid);
                        *type = SID_NAME_UNKNOWN;
-                       result = NT_STATUS_OK;
+                       status = NT_STATUS_OK;
                }
        }
 
-       return result;
+       return status;
 }
 
 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
@@ -1843,9 +2001,10 @@ static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
                                const char *member)
 {
        struct policy_handle connect_pol, domain_pol;
-       NTSTATUS result;
+       NTSTATUS status, result;
        uint32 group_rid;
        struct policy_handle group_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        struct samr_Ids rids, rid_types;
        struct lsa_String lsa_acct_name;
@@ -1859,57 +2018,83 @@ static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
        }
 
        /* Get sam policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
                                        &sid,
-                                       &domain_pol);
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
 
        init_lsa_String(&lsa_acct_name, member);
 
-       result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_LookupNames(b, mem_ctx,
                                         &domain_pol,
                                         1,
                                         &lsa_acct_name,
                                         &rids,
-                                        &rid_types);
+                                        &rid_types,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Could not lookup up group member %s\n"),
+                         member);
+               goto done;
+       }
 
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_fprintf(stderr, _("Could not lookup up group member %s\n"),
                          member);
                goto done;
        }
 
-       result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenGroup(b, mem_ctx,
                                       &domain_pol,
                                       MAXIMUM_ALLOWED_ACCESS,
                                       group_rid,
-                                      &group_pol);
+                                      &group_pol,
+                                      &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
 
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
-       result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_AddGroupMember(b, mem_ctx,
                                            &group_pol,
                                            rids.ids[0],
-                                           0x0005); /* unknown flags */
+                                           0x0005, /* unknown flags */
+                                           &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
+
+       status = result;
 
  done:
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
-       return result;
+       dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
+       return status;
 }
 
 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
@@ -1918,9 +2103,10 @@ static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
                                const char *member)
 {
        struct policy_handle connect_pol, domain_pol;
-       NTSTATUS result;
+       NTSTATUS status, result;
        uint32 alias_rid;
        struct policy_handle alias_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        struct dom_sid member_sid;
        enum lsa_SidType member_type;
@@ -1943,45 +2129,60 @@ static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
        }
 
        /* Get sam policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
                                        &sid,
-                                       &domain_pol);
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
-       result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenAlias(b, mem_ctx,
                                       &domain_pol,
                                       MAXIMUM_ALLOWED_ACCESS,
                                       alias_rid,
-                                      &alias_pol);
-
+                                      &alias_pol,
+                                      &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
 
-       result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_AddAliasMember(b, mem_ctx,
                                            &alias_pol,
-                                           &member_sid);
-
-       if (!NT_STATUS_IS_OK(result)) {
-               return result;
+                                           &member_sid,
+                                           &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
+       status = result;
+
  done:
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
-       return result;
+       dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
+       return status;
 }
 
 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
@@ -2043,7 +2244,7 @@ static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
 
 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
 {
-       return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_samr, 0,
                               rpc_group_addmem_internals,
                               argc, argv);
 }
@@ -2055,9 +2256,10 @@ static NTSTATUS rpc_del_groupmem(struct net_context *c,
                                const char *member)
 {
        struct policy_handle connect_pol, domain_pol;
-       NTSTATUS result;
+       NTSTATUS status, result;
        uint32 group_rid;
        struct policy_handle group_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        struct samr_Ids rids, rid_types;
        struct lsa_String lsa_acct_name;
@@ -2070,52 +2272,81 @@ static NTSTATUS rpc_del_groupmem(struct net_context *c,
                return NT_STATUS_UNSUCCESSFUL;
 
        /* Get sam policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
-       if (!NT_STATUS_IS_OK(result))
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
+
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
                                        &sid,
-                                       &domain_pol);
-       if (!NT_STATUS_IS_OK(result))
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
 
        init_lsa_String(&lsa_acct_name, member);
 
-       result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_LookupNames(b, mem_ctx,
                                         &domain_pol,
                                         1,
                                         &lsa_acct_name,
                                         &rids,
-                                        &rid_types);
+                                        &rid_types,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Could not lookup up group member %s\n"),
+                         member);
+               goto done;
+       }
+
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_fprintf(stderr, _("Could not lookup up group member %s\n"),
                          member);
                goto done;
        }
 
-       result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenGroup(b, mem_ctx,
                                       &domain_pol,
                                       MAXIMUM_ALLOWED_ACCESS,
                                       group_rid,
-                                      &group_pol);
-
-       if (!NT_STATUS_IS_OK(result))
+                                      &group_pol,
+                                      &result);
+       if (!NT_STATUS_IS_OK(status)) {
                goto done;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               status = result;
+               goto done;
+       }
 
-       result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
                                               &group_pol,
-                                              rids.ids[0]);
+                                              rids.ids[0],
+                                              &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
 
+       status = result;
  done:
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
-       return result;
+       dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
+       return status;
 }
 
 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
@@ -2124,9 +2355,10 @@ static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
                                const char *member)
 {
        struct policy_handle connect_pol, domain_pol;
-       NTSTATUS result;
+       NTSTATUS status, result;
        uint32 alias_rid;
        struct policy_handle alias_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        struct dom_sid member_sid;
        enum lsa_SidType member_type;
@@ -2148,43 +2380,62 @@ static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
        }
 
        /* Get sam policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
                                        &sid,
-                                       &domain_pol);
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
-       result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenAlias(b, mem_ctx,
                                       &domain_pol,
                                       MAXIMUM_ALLOWED_ACCESS,
                                       alias_rid,
-                                      &alias_pol);
+                                      &alias_pol,
+                                      &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
-       if (!NT_STATUS_IS_OK(result))
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
 
-       result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
                                               &alias_pol,
-                                              &member_sid);
+                                              &member_sid,
+                                              &result);
 
-       if (!NT_STATUS_IS_OK(result))
-               return result;
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       status = result;
 
  done:
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
-       return result;
+       dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
+       return status;
 }
 
 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
@@ -2246,7 +2497,7 @@ static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
 
 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
 {
-       return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_samr, 0,
                               rpc_group_delmem_internals,
                               argc, argv);
 }
@@ -2277,12 +2528,13 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
                                        const char **argv)
 {
        struct policy_handle connect_pol, domain_pol;
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status, result;
        uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
        struct samr_SamArray *groups = NULL;
        bool global = false;
        bool local = false;
        bool builtin = false;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (c->display_usage) {
                d_printf("%s\n%s",
@@ -2317,22 +2569,32 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
 
        /* Get sam policy handle */
 
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
        /* Get domain policy handle */
 
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
-                                       &domain_pol);
+                                       discard_const_p(struct dom_sid2, domain_sid),
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
@@ -2346,10 +2608,10 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
 
                if (!global) break;
 
-               get_query_dispinfo_params(
+               dcerpc_get_query_dispinfo_params(
                        loop_count, &max_entries, &max_size);
 
-               result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
                                                      &domain_pol,
                                                      3,
                                                      start_idx,
@@ -2357,7 +2619,11 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
                                                      max_size,
                                                      &total_size,
                                                      &returned_size,
-                                                     &info);
+                                                     &info,
+                                                     &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto done;
+               }
                num_entries = info.info3.count;
                start_idx += info.info3.count;
 
@@ -2385,12 +2651,16 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
        do {
                if (!local) break;
 
-               result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
                                                       &domain_pol,
                                                       &start_idx,
                                                       &groups,
                                                       0xffff,
-                                                      &num_entries);
+                                                      &num_entries,
+                                                      &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto done;
+               }
                if (!NT_STATUS_IS_OK(result) &&
                    !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
                        break;
@@ -2403,19 +2673,28 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
 
                                struct policy_handle alias_pol;
                                union samr_AliasInfo *info = NULL;
+                               NTSTATUS _result;
 
-                               if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
-                                                                          &domain_pol,
-                                                                          0x8,
-                                                                          groups->entries[i].idx,
-                                                                          &alias_pol))) &&
-                                   (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
-                                                                               &alias_pol,
-                                                                               3,
-                                                                               &info))) &&
-                                   (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
-                                                                   &alias_pol)))) {
-                                       description = info->description.string;
+                               status = dcerpc_samr_OpenAlias(b, mem_ctx,
+                                                              &domain_pol,
+                                                              0x8,
+                                                              groups->entries[i].idx,
+                                                              &alias_pol,
+                                                              &_result);
+                               if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
+                                       status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
+                                                                           &alias_pol,
+                                                                           3,
+                                                                           &info,
+                                                                           &_result);
+                                       if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
+                                               status = dcerpc_samr_Close(b, mem_ctx,
+                                                                          &alias_pol,
+                                                                          &_result);
+                                               if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
+                                                       description = info->description.string;
+                                               }
+                                       }
                                }
                        }
 
@@ -2428,31 +2707,43 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
                        }
                }
        } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
+       dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
        /* Get builtin policy handle */
 
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
-                                       &domain_pol);
+                                       discard_const_p(struct dom_sid2, &global_sid_Builtin),
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
+
        /* query builtin aliases */
        start_idx = 0;
        do {
                if (!builtin) break;
 
-               result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
                                                       &domain_pol,
                                                       &start_idx,
                                                       &groups,
                                                       max_entries,
-                                                      &num_entries);
+                                                      &num_entries,
+                                                      &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       break;
+               }
                if (!NT_STATUS_IS_OK(result) &&
-                   !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
+                   !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
+                       status = result;
                        break;
+               }
 
                for (i = 0; i < num_entries; i++) {
 
@@ -2462,19 +2753,28 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
 
                                struct policy_handle alias_pol;
                                union samr_AliasInfo *info = NULL;
+                               NTSTATUS _result;
 
-                               if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
-                                                                          &domain_pol,
-                                                                          0x8,
-                                                                          groups->entries[i].idx,
-                                                                          &alias_pol))) &&
-                                   (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
-                                                                               &alias_pol,
-                                                                               3,
-                                                                               &info))) &&
-                                   (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
-                                                                   &alias_pol)))) {
-                                       description = info->description.string;
+                               status = dcerpc_samr_OpenAlias(b, mem_ctx,
+                                                              &domain_pol,
+                                                              0x8,
+                                                              groups->entries[i].idx,
+                                                              &alias_pol,
+                                                              &_result);
+                               if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
+                                       status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
+                                                                           &alias_pol,
+                                                                           3,
+                                                                           &info,
+                                                                           &_result);
+                                       if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
+                                               status = dcerpc_samr_Close(b, mem_ctx,
+                                                                          &alias_pol,
+                                                                          &_result);
+                                               if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
+                                                       description = info->description.string;
+                                               }
+                                       }
                                }
                        }
 
@@ -2488,13 +2788,15 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
                }
        } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
 
+       status = result;
+
  done:
-       return result;
+       return status;
 }
 
 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
 {
-       return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_samr, 0,
                               rpc_group_list_internals,
                               argc, argv);
 }
@@ -2507,32 +2809,41 @@ static NTSTATUS rpc_list_group_members(struct net_context *c,
                                        struct policy_handle *domain_pol,
                                        uint32 rid)
 {
-       NTSTATUS result;
+       NTSTATUS result, status;
        struct policy_handle group_pol;
        uint32 num_members, *group_rids;
        int i;
-       struct samr_RidTypeArray *rids = NULL;
+       struct samr_RidAttrArray *rids = NULL;
        struct lsa_Strings names;
        struct samr_Ids types;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        fstring sid_str;
        sid_to_fstring(sid_str, domain_sid);
 
-       result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenGroup(b, mem_ctx,
                                       domain_pol,
                                       MAXIMUM_ALLOWED_ACCESS,
                                       rid,
-                                      &group_pol);
-
-       if (!NT_STATUS_IS_OK(result))
+                                      &group_pol,
+                                      &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
 
-       result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
                                              &group_pol,
-                                             &rids);
-
-       if (!NT_STATUS_IS_OK(result))
+                                             &rids,
+                                             &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
 
        num_members = rids->count;
        group_rids = rids->rids;
@@ -2543,15 +2854,19 @@ static NTSTATUS rpc_list_group_members(struct net_context *c,
                if (num_members < this_time)
                        this_time = num_members;
 
-               result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_LookupRids(b, mem_ctx,
                                                domain_pol,
                                                this_time,
                                                group_rids,
                                                &names,
-                                               &types);
-
-               if (!NT_STATUS_IS_OK(result))
+                                               &types,
+                                               &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+               if (!NT_STATUS_IS_OK(result)) {
                        return result;
+               }
 
                /* We only have users as members, but make the output
                   the same as the output of alias members */
@@ -2582,7 +2897,7 @@ static NTSTATUS rpc_list_alias_members(struct net_context *c,
                                        struct policy_handle *domain_pol,
                                        uint32 rid)
 {
-       NTSTATUS result;
+       NTSTATUS result, status;
        struct rpc_pipe_client *lsa_pipe;
        struct policy_handle alias_pol, lsa_pol;
        uint32 num_members;
@@ -2592,20 +2907,29 @@ static NTSTATUS rpc_list_alias_members(struct net_context *c,
        enum lsa_SidType *types;
        int i;
        struct lsa_SidArray sid_array;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
-       result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenAlias(b, mem_ctx,
                                       domain_pol,
                                       MAXIMUM_ALLOWED_ACCESS,
                                       rid,
-                                      &alias_pol);
-
-       if (!NT_STATUS_IS_OK(result))
+                                      &alias_pol,
+                                      &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
 
-       result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
                                               &alias_pol,
-                                              &sid_array);
-
+                                              &sid_array,
+                                              &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Couldn't list alias members\n"));
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                d_fprintf(stderr, _("Couldn't list alias members\n"));
                return result;
@@ -2635,7 +2959,7 @@ static NTSTATUS rpc_list_alias_members(struct net_context *c,
                return result;
        }
 
-       alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_members);
+       alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
        if (!alias_sids) {
                d_fprintf(stderr, _("Out of memory\n"));
                TALLOC_FREE(lsa_pipe);
@@ -2686,40 +3010,53 @@ static NTSTATUS rpc_group_members_internals(struct net_context *c,
                                        int argc,
                                        const char **argv)
 {
-       NTSTATUS result;
+       NTSTATUS result, status;
        struct policy_handle connect_pol, domain_pol;
        struct samr_Ids rids, rid_types;
        struct lsa_String lsa_acct_name;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        /* Get sam policy handle */
 
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
-
-       if (!NT_STATUS_IS_OK(result))
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
 
        /* Get domain policy handle */
 
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
-                                       &domain_pol);
-
-       if (!NT_STATUS_IS_OK(result))
+                                       discard_const_p(struct dom_sid2, domain_sid),
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
 
        init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
 
-       result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_LookupNames(b, mem_ctx,
                                         &domain_pol,
                                         1,
                                         &lsa_acct_name,
                                         &rids,
-                                        &rid_types);
+                                        &rid_types,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
        if (!NT_STATUS_IS_OK(result)) {
 
@@ -2727,29 +3064,35 @@ static NTSTATUS rpc_group_members_internals(struct net_context *c,
 
                struct dom_sid sid_Builtin;
 
-               rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
+               dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
 
                sid_copy(&sid_Builtin, &global_sid_Builtin);
 
-               result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                                &connect_pol,
                                                MAXIMUM_ALLOWED_ACCESS,
                                                &sid_Builtin,
-                                               &domain_pol);
-
+                                               &domain_pol,
+                                               &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
                if (!NT_STATUS_IS_OK(result)) {
                        d_fprintf(stderr, _("Couldn't find group %s\n"),
                                  argv[0]);
                        return result;
                }
 
-               result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_LookupNames(b, mem_ctx,
                                                 &domain_pol,
                                                 1,
                                                 &lsa_acct_name,
                                                 &rids,
-                                                &rid_types);
-
+                                                &rid_types,
+                                                &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
                if (!NT_STATUS_IS_OK(result)) {
                        d_fprintf(stderr, _("Couldn't find group %s\n"),
                                  argv[0]);
@@ -2783,7 +3126,7 @@ static int rpc_group_members(struct net_context *c, int argc, const char **argv)
                return rpc_group_usage(c, argc, argv);
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_samr, 0,
                               rpc_group_members_internals,
                               argc, argv);
 }
@@ -2898,7 +3241,7 @@ int net_rpc_group(struct net_context *c, int argc, const char **argv)
                {NULL, NULL, 0, NULL, NULL}
        };
 
-       status = libnetapi_init(&c->netapi_ctx);
+       status = libnetapi_net_init(&c->netapi_ctx);
        if (status != 0) {
                return -1;
        }
@@ -2918,7 +3261,7 @@ int net_rpc_group(struct net_context *c, int argc, const char **argv)
                        return 0;
                }
 
-               return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
+               return run_rpc_command(c, NULL, &ndr_table_samr, 0,
                                       rpc_group_list_internals,
                                       argc, argv);
        }
@@ -3039,6 +3382,7 @@ static WERROR get_share_info(struct net_context *c,
        WERROR result;
        NTSTATUS status;
        union srvsvc_NetShareInfo info;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        /* no specific share requested, enumerate all */
        if (argc == 0) {
@@ -3049,25 +3393,33 @@ static WERROR get_share_info(struct net_context *c,
 
                info_ctr->level = level;
 
-               status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
+               status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
                                                       pipe_hnd->desthost,
                                                       info_ctr,
                                                       preferred_len,
                                                       &total_entries,
                                                       &resume_handle,
                                                       &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return ntstatus_to_werror(status);
+               }
                return result;
        }
 
        /* request just one share */
-       status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
+       status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
                                               pipe_hnd->desthost,
                                               argv[0],
                                               level,
                                               &info,
                                               &result);
 
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(status)) {
+               result = ntstatus_to_werror(status);
+               goto done;
+       }
+
+       if (!W_ERROR_IS_OK(result)) {
                goto done;
        }
 
@@ -3081,37 +3433,43 @@ static WERROR get_share_info(struct net_context *c,
        {
                struct srvsvc_NetShareCtr1 *ctr1;
 
-               ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
+               ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
                W_ERROR_HAVE_NO_MEMORY(ctr1);
 
                ctr1->count = 1;
                ctr1->array = info.info1;
 
                info_ctr->ctr.ctr1 = ctr1;
+
+               break;
        }
        case 2:
        {
                struct srvsvc_NetShareCtr2 *ctr2;
 
-               ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
+               ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
                W_ERROR_HAVE_NO_MEMORY(ctr2);
 
                ctr2->count = 1;
                ctr2->array = info.info2;
 
                info_ctr->ctr.ctr2 = ctr2;
+
+               break;
        }
        case 502:
        {
                struct srvsvc_NetShareCtr502 *ctr502;
 
-               ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
+               ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
                W_ERROR_HAVE_NO_MEMORY(ctr502);
 
                ctr502->count = 1;
                ctr502->array = info.info502;
 
                info_ctr->ctr.ctr502 = ctr502;
+
+               break;
        }
        } /* switch */
 done:
@@ -3171,7 +3529,7 @@ static bool check_share_availability(struct cli_state *cli, const char *netname)
 {
        NTSTATUS status;
 
-       status = cli_tcon_andx(cli, netname, "A:", "", 0);
+       status = cli_tree_connect(cli, netname, "A:", "", 0);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf(_("skipping   [%s]: not a file share.\n"), netname);
                return false;
@@ -3243,6 +3601,7 @@ static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
        struct cli_state *cli_dst = NULL;
        uint32 level = 502; /* includes secdesc */
        uint32_t parm_error = 0;
+       struct dcerpc_binding_handle *b;
 
        result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
                                &ctr_src);
@@ -3255,6 +3614,7 @@ static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
         if (!NT_STATUS_IS_OK(nt_status))
                 return nt_status;
 
+       b = srvsvc_pipe->binding_handle;
 
        for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
 
@@ -3276,21 +3636,27 @@ static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
 
                info.info502 = &info502;
 
-               nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
+               nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
                                                      srvsvc_pipe->desthost,
                                                      502,
                                                      &info,
                                                      &parm_error,
                                                      &result);
-
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       printf(_("cannot add share: %s\n"),
+                               nt_errstr(nt_status));
+                       goto done;
+               }
                 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
                        printf(_("           [%s] does already exist\n"),
                                info502.name);
                        continue;
                }
 
-               if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
-                       printf(_("cannot add share: %s\n"), win_errstr(result));
+               if (!W_ERROR_IS_OK(result)) {
+                       nt_status = werror_to_ntstatus(result);
+                       printf(_("cannot add share: %s\n"),
+                               win_errstr(result));
                        goto done;
                }
 
@@ -3333,7 +3699,7 @@ static int rpc_share_migrate_shares(struct net_context *c, int argc,
                return -1;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
                               rpc_share_migrate_shares_internals,
                               argc, argv);
 }
@@ -3346,7 +3712,7 @@ static int rpc_share_migrate_shares(struct net_context *c, int argc,
  * @param state        arg-pointer
  *
  **/
-static void copy_fn(const char *mnt, struct file_info *f,
+static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
                    const char *mask, void *state)
 {
        static NTSTATUS nt_status;
@@ -3362,12 +3728,12 @@ static void copy_fn(const char *mnt, struct file_info *f,
        c = local_state->c;
 
        if (strequal(f->name, ".") || strequal(f->name, ".."))
-               return;
+               return NT_STATUS_OK;
 
        DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
 
        /* DIRECTORY */
-       if (f->mode & aDIR) {
+       if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
 
                DEBUG(3,("got dir: %s\n", f->name));
 
@@ -3390,24 +3756,32 @@ static void copy_fn(const char *mnt, struct file_info *f,
                        break;
                default:
                        d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
-                       return;
+                       return NT_STATUS_INTERNAL_ERROR;
                }
 
-               if (!NT_STATUS_IS_OK(nt_status))
+               if (!NT_STATUS_IS_OK(nt_status)) {
                        printf(_("could not handle dir %s: %s\n"),
                                dir, nt_errstr(nt_status));
+                       return nt_status;
+               }
 
                /* search below that directory */
-               fstrcpy(new_mask, dir);
-               fstrcat(new_mask, "\\*");
+               if (strlcpy(new_mask, dir, sizeof(new_mask)) >= sizeof(new_mask)) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               if (strlcat(new_mask, "\\*", sizeof(new_mask)) >= sizeof(new_mask)) {
+                       return NT_STATUS_NO_MEMORY;
+               }
 
                old_dir = local_state->cwd;
                local_state->cwd = dir;
-               if (!sync_files(local_state, new_mask))
+               nt_status = sync_files(local_state, new_mask);
+               if (!NT_STATUS_IS_OK(nt_status)) {
                        printf(_("could not handle files\n"));
+               }
                local_state->cwd = old_dir;
 
-               return;
+               return nt_status;
        }
 
 
@@ -3433,13 +3807,13 @@ static void copy_fn(const char *mnt, struct file_info *f,
        default:
                d_fprintf(stderr, _("Unsupported file mode %d\n"),
                          net_mode_share);
-               return;
+               return NT_STATUS_INTERNAL_ERROR;
        }
 
        if (!NT_STATUS_IS_OK(nt_status))
                printf(_("could not handle file %s: %s\n"),
                        filename, nt_errstr(nt_status));
-
+       return nt_status;
 }
 
 /**
@@ -3451,7 +3825,7 @@ static void copy_fn(const char *mnt, struct file_info *f,
  *
  * @return             Boolean result
  **/
-static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
+static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
 {
        struct cli_state *targetcli;
        char *targetpath = NULL;
@@ -3459,12 +3833,14 @@ static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
 
        DEBUG(3,("calling cli_list with mask: %s\n", mask));
 
-       if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
-                               mask, &targetcli, &targetpath ) ) {
+       status = cli_resolve_path(talloc_tos(), "", NULL,
+                                 cp_clistate->cli_share_src,
+                                 mask, &targetcli, &targetpath);
+       if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
                                    "%s\n"),
-                       mask, cli_errstr(cp_clistate->cli_share_src));
-               return false;
+                       mask, nt_errstr(status));
+               return status;
        }
 
        status = cli_list(targetcli, targetpath, cp_clistate->attribute,
@@ -3472,10 +3848,9 @@ static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
        if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, _("listing %s failed with error: %s\n"),
                          mask, nt_errstr(status));
-               return false;
        }
 
-       return true;
+       return status;
 }
 
 
@@ -3602,12 +3977,13 @@ static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
                cp_clistate.cli_share_src = NULL;
                cp_clistate.cli_share_dst = NULL;
                cp_clistate.cwd = NULL;
-               cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
+               cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
                cp_clistate.c = c;
 
                /* open share source */
                nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
-                                              &cli->dest_ss, cli->desthost,
+                                              smbXcli_conn_remote_sockaddr(cli->conn),
+                                              cli_state_remote_name(cli),
                                               info502.name, "A:");
                if (!NT_STATUS_IS_OK(nt_status))
                        goto done;
@@ -3632,10 +4008,10 @@ static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
                        goto done;
                }
 
-               if (!sync_files(&cp_clistate, mask)) {
+               nt_status = sync_files(&cp_clistate, mask);
+               if (!NT_STATUS_IS_OK(nt_status)) {
                        d_fprintf(stderr, _("could not handle files for share: "
                                            "%s\n"), info502.name);
-                       nt_status = NT_STATUS_UNSUCCESSFUL;
                        goto done;
                }
        }
@@ -3671,7 +4047,7 @@ static int rpc_share_migrate_files(struct net_context *c, int argc, const char *
                return -1;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
                               rpc_share_migrate_files_internals,
                               argc, argv);
 }
@@ -3710,6 +4086,7 @@ static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
        struct cli_state *cli_dst = NULL;
        uint32 level = 502; /* includes secdesc */
        uint32_t parm_error = 0;
+       struct dcerpc_binding_handle *b;
 
        result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
                                &ctr_src);
@@ -3723,6 +4100,7 @@ static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
         if (!NT_STATUS_IS_OK(nt_status))
                 return nt_status;
 
+       b = srvsvc_pipe->binding_handle;
 
        for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
 
@@ -3746,14 +4124,20 @@ static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
                info.info502 = &info502;
 
                /* finally modify the share on the dst server */
-               nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
+               nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
                                                          srvsvc_pipe->desthost,
                                                          info502.name,
                                                          level,
                                                          &info,
                                                          &parm_error,
                                                          &result);
-               if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       printf(_("cannot set share-acl: %s\n"),
+                              nt_errstr(nt_status));
+                       goto done;
+               }
+               if (!W_ERROR_IS_OK(result)) {
+                       nt_status = werror_to_ntstatus(result);
                        printf(_("cannot set share-acl: %s\n"),
                               win_errstr(result));
                        goto done;
@@ -3798,7 +4182,7 @@ static int rpc_share_migrate_security(struct net_context *c, int argc,
                return -1;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
                               rpc_share_migrate_security_internals,
                               argc, argv);
 }
@@ -3836,17 +4220,17 @@ static int rpc_share_migrate_all(struct net_context *c, int argc,
        /* order is important. we don't want to be locked out by the share-acl
         * before copying files - gd */
 
-       ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
+       ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
                              rpc_share_migrate_shares_internals, argc, argv);
        if (ret)
                return ret;
 
-       ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
+       ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
                              rpc_share_migrate_files_internals, argc, argv);
        if (ret)
                return ret;
 
-       return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
                               rpc_share_migrate_security_internals, argc,
                               argv);
 }
@@ -3935,55 +4319,83 @@ static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
 {
        uint32 start_idx, max_entries, num_entries, i;
        struct samr_SamArray *groups = NULL;
-       NTSTATUS result;
+       NTSTATUS result, status;
        struct policy_handle domain_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        /* Get domain policy handle */
 
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
-                                       &domain_pol);
-       if (!NT_STATUS_IS_OK(result))
+                                       discard_const_p(struct dom_sid2, domain_sid),
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
 
        start_idx = 0;
        max_entries = 250;
 
        do {
-               result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
                                                       &domain_pol,
                                                       &start_idx,
                                                       &groups,
                                                       max_entries,
-                                                      &num_entries);
+                                                      &num_entries,
+                                                      &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto done;
+               }
                for (i = 0; i < num_entries; i++) {
 
                        struct policy_handle alias_pol;
                        struct full_alias alias;
                        struct lsa_SidArray sid_array;
                        int j;
+                       NTSTATUS _result;
 
-                       result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
+                       status = dcerpc_samr_OpenAlias(b, mem_ctx,
                                                       &domain_pol,
                                                       MAXIMUM_ALLOWED_ACCESS,
                                                       groups->entries[i].idx,
-                                                      &alias_pol);
-                       if (!NT_STATUS_IS_OK(result))
+                                                      &alias_pol,
+                                                      &_result);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               goto done;
+                       }
+                       if (!NT_STATUS_IS_OK(_result)) {
+                               status = _result;
                                goto done;
+                       }
 
-                       result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
+                       status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
                                                               &alias_pol,
-                                                              &sid_array);
-                       if (!NT_STATUS_IS_OK(result))
+                                                              &sid_array,
+                                                              &_result);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               goto done;
+                       }
+                       if (!NT_STATUS_IS_OK(_result)) {
+                               status = _result;
                                goto done;
+                       }
 
                        alias.num_members = sid_array.num_sids;
 
-                       result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
-                       if (!NT_STATUS_IS_OK(result))
+                       status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               goto done;
+                       }
+                       if (!NT_STATUS_IS_OK(_result)) {
+                               status = _result;
                                goto done;
+                       }
 
                        alias.members = NULL;
 
@@ -4002,12 +4414,12 @@ static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
                }
        } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
 
-       result = NT_STATUS_OK;
+       status = NT_STATUS_OK;
 
  done:
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
+       dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
 
-       return result;
+       return status;
 }
 
 /*
@@ -4026,6 +4438,7 @@ static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
        int i;
        NTSTATUS result;
        struct policy_handle lsa_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
                                     SEC_FLAG_MAXIMUM_ALLOWED,
@@ -4065,12 +4478,12 @@ static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
 
                for (j=0; j<alias->num_members; j++)
                        DEBUG(1, ("%s\\%s (%d); ",
-                                 domains[j] ? domains[j] : "*unknown*", 
+                                 domains[j] ? domains[j] : "*unknown*",
                                  names[j] ? names[j] : "*unknown*",types[j]));
                DEBUG(1, ("\n"));
        }
 
-       rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
+       dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
 
        return NT_STATUS_OK;
 }
@@ -4089,29 +4502,35 @@ static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
                                        int argc,
                                        const char **argv)
 {
-       NTSTATUS result;
+       NTSTATUS result, status;
        struct policy_handle connect_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
-
-       if (!NT_STATUS_IS_OK(result))
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
                goto done;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               status = result;
+               goto done;
+       }
 
-       result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
+       status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
                                          &global_sid_Builtin);
-
-       if (!NT_STATUS_IS_OK(result))
+       if (!NT_STATUS_IS_OK(status)) {
                goto done;
+       }
 
-       result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
+       status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
                                          domain_sid);
 
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
+       dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
  done:
-       return result;
+       return status;
 }
 
 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
@@ -4137,7 +4556,7 @@ static void free_user_token(struct security_token *token)
 
 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
 {
-       if (is_sid_in_token(token, sid))
+       if (security_token_has_sid(token, sid))
                return;
 
        token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
@@ -4171,7 +4590,7 @@ static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
        int i;
 
        for (i=0; i<alias->num_members; i++) {
-               if (sid_compare(sid, &alias->members[i]) == 0)
+               if (dom_sid_compare(sid, &alias->members[i]) == 0)
                        return true;
        }
 
@@ -4211,7 +4630,7 @@ static bool get_user_sids(const char *domain, const char *user, struct security_
        enum wbcSidType type;
        fstring full_name;
        struct wbcDomainSid wsid;
-       char *sid_str = NULL;
+       char sid_str[WBC_SID_STRING_BUFLEN];
        struct dom_sid user_sid;
        uint32_t num_groups;
        gid_t *groups = NULL;
@@ -4230,13 +4649,9 @@ static bool get_user_sids(const char *domain, const char *user, struct security_
                return false;
        }
 
-       wbc_status = wbcSidToString(&wsid, &sid_str);
-       if (!WBC_ERROR_IS_OK(wbc_status)) {
-               return false;
-       }
+       wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
 
-       if (type != SID_NAME_USER) {
-               wbcFreeMemory(sid_str);
+       if (type != WBC_SID_NAME_USER) {
                DEBUG(1, ("%s is not a user\n", full_name));
                return false;
        }
@@ -4246,9 +4661,6 @@ static bool get_user_sids(const char *domain, const char *user, struct security_
                return false;
        }
 
-       wbcFreeMemory(sid_str);
-       sid_str = NULL;
-
        init_user_token(token, &user_sid);
 
        /* And now the groups winbind knows about */
@@ -4272,18 +4684,11 @@ static bool get_user_sids(const char *domain, const char *user, struct security_
                        return false;
                }
 
-               wbc_status = wbcSidToString(&wsid, &sid_str);
-               if (!WBC_ERROR_IS_OK(wbc_status)) {
-                       wbcFreeMemory(groups);
-                       return false;
-               }
+               wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
 
                DEBUG(3, (" %s\n", sid_str));
 
                string_to_sid(&sid, sid_str);
-               wbcFreeMemory(sid_str);
-               sid_str = NULL;
-
                add_sid_to_token(token, &sid);
        }
        wbcFreeMemory(groups);
@@ -4350,7 +4755,6 @@ static bool get_user_tokens(struct net_context *c, int *num_tokens,
                }
 
                get_user_sids(domain, user, &(result[i].token));
-               i+=1;
        }
        TALLOC_FREE(frame);
        wbcFreeMemory(users);
@@ -4408,12 +4812,14 @@ static bool get_user_tokens_from_file(FILE *f,
 
                token = &((*tokens)[*num_tokens-1]);
 
-               fstrcpy(token->name, line);
+               if (strlcpy(token->name, line, sizeof(token->name)) >= sizeof(token->name)) {
+                       return false;
+               }
                token->token.num_sids = 0;
                token->token.sids = NULL;
                continue;
        }
-       
+
        return false;
 }
 
@@ -4437,8 +4843,9 @@ static void show_userlist(struct rpc_pipe_client *pipe_hnd,
        WERROR result;
        NTSTATUS status;
        uint16 cnum;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
-       status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
+       status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
                                               pipe_hnd->desthost,
                                               netname,
                                               502,
@@ -4457,15 +4864,15 @@ static void show_userlist(struct rpc_pipe_client *pipe_hnd,
                          netname));
        }
 
-       cnum = cli->cnum;
+       cnum = cli_state_get_tid(cli);
 
-       if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
+       if (!NT_STATUS_IS_OK(cli_tree_connect(cli, netname, "A:", "", 0))) {
                return;
        }
 
        if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
                        FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
-               root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
+               cli_query_secdesc(cli, fnum, mem_ctx, &root_sd);
        }
 
        for (i=0; i<num_tokens; i++) {
@@ -4501,8 +4908,8 @@ static void show_userlist(struct rpc_pipe_client *pipe_hnd,
        if (fnum != (uint16_t)-1)
                cli_close(cli, fnum);
        cli_tdis(cli);
-       cli->cnum = cnum;
-       
+       cli_state_set_tid(cli, cnum);
+
        return;
 }
 
@@ -4633,19 +5040,19 @@ static int rpc_share_allowedusers(struct net_context *c, int argc,
                return 0;
        }
 
-       result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
+       result = run_rpc_command(c, NULL, &ndr_table_samr, 0,
                                 rpc_aliaslist_internals,
                                 argc, argv);
        if (result != 0)
                return result;
 
-       result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
+       result = run_rpc_command(c, NULL, &ndr_table_lsarpc, 0,
                                 rpc_aliaslist_dump,
                                 argc, argv);
        if (result != 0)
                return result;
 
-       return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
                               rpc_share_allowedusers_internals,
                               argc, argv);
 }
@@ -4663,7 +5070,7 @@ int net_usersidlist(struct net_context *c, int argc, const char **argv)
 
        if (!get_user_tokens(c, &num_tokens, &tokens)) {
                DEBUG(0, ("Could not get the user/sid list\n"));
-               return 0;
+               return -1;
        }
 
        for (i=0; i<num_tokens; i++) {
@@ -4672,7 +5079,7 @@ int net_usersidlist(struct net_context *c, int argc, const char **argv)
        }
 
        SAFE_FREE(tokens);
-       return 1;
+       return 0;
 }
 
 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
@@ -4741,7 +5148,7 @@ int net_rpc_share(struct net_context *c, int argc, const char **argv)
                {NULL, NULL, 0, NULL, NULL}
        };
 
-       status = libnetapi_init(&c->netapi_ctx);
+       status = libnetapi_net_init(&c->netapi_ctx);
        if (status != 0) {
                return -1;
        }
@@ -4834,19 +5241,24 @@ static NTSTATUS rpc_sh_share_info(struct net_context *c,
        union srvsvc_NetShareInfo info;
        WERROR result;
        NTSTATUS status;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 1) {
                d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
+       status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
                                               pipe_hnd->desthost,
                                               argv[0],
                                               2,
                                               &info,
                                               &result);
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(status)) {
+               result = ntstatus_to_werror(status);
+               goto done;
+       }
+       if (!W_ERROR_IS_OK(result)) {
                goto done;
        }
 
@@ -4864,16 +5276,16 @@ struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx
 {
        static struct rpc_sh_cmd cmds[] = {
 
-       { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
+       { "list", NULL, &ndr_table_srvsvc, rpc_sh_share_list,
          N_("List available shares") },
 
-       { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
+       { "add", NULL, &ndr_table_srvsvc, rpc_sh_share_add,
          N_("Add a share") },
 
-       { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
+       { "delete", NULL, &ndr_table_srvsvc, rpc_sh_share_delete,
          N_("Delete a share") },
 
-       { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
+       { "info", NULL, &ndr_table_srvsvc, rpc_sh_share_info,
          N_("Get information about a share") },
 
        { NULL, NULL, 0, NULL, NULL }
@@ -5017,7 +5429,7 @@ int net_rpc_file(struct net_context *c, int argc, const char **argv)
                {NULL, NULL, 0, NULL, NULL}
        };
 
-       status = libnetapi_init(&c->netapi_ctx);
+       status = libnetapi_net_init(&c->netapi_ctx);
        if (status != 0) {
                return -1;
        }
@@ -5068,17 +5480,21 @@ static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
                                        int argc,
                                        const char **argv)
 {
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-
-       result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       WERROR result;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
-       if (NT_STATUS_IS_OK(result)) {
+       status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (W_ERROR_IS_OK(result)) {
                d_printf(_("\nShutdown successfully aborted\n"));
                DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
        } else
                DEBUG(5,("cmd_shutdown_abort: query failed\n"));
 
-       return result;
+       return werror_to_ntstatus(result);
 }
 
 /**
@@ -5108,16 +5524,22 @@ static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
                                                const char **argv)
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       WERROR werr;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
-       result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
+       result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
 
-       if (NT_STATUS_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
+               return result;
+       }
+       if (W_ERROR_IS_OK(werr)) {
                d_printf(_("\nShutdown successfully aborted\n"));
                DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
        } else
                DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
 
-       return result;
+       return werror_to_ntstatus(werr);
 }
 
 /**
@@ -5144,7 +5566,7 @@ static int rpc_shutdown_abort(struct net_context *c, int argc,
                return 0;
        }
 
-       rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
+       rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
                             rpc_shutdown_abort_internals, argc, argv);
 
        if (rc == 0)
@@ -5152,7 +5574,7 @@ static int rpc_shutdown_abort(struct net_context *c, int argc,
 
        DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
 
-       return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
                               rpc_reg_shutdown_abort_internals,
                               argc, argv);
 }
@@ -5183,10 +5605,12 @@ NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
                                     int argc,
                                     const char **argv)
 {
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       WERROR result;
         const char *msg = N_("This machine will be shutdown shortly");
        uint32 timeout = 20;
        struct lsa_StringLarge msg_string;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (c->opt_comment) {
                msg = c->opt_comment;
@@ -5198,17 +5622,19 @@ NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
        msg_string.string = msg;
 
        /* create an entry */
-       result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
+       status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
                        &msg_string, timeout, c->opt_force, c->opt_reboot,
-                       NULL);
-
-       if (NT_STATUS_IS_OK(result)) {
+                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (W_ERROR_IS_OK(result)) {
                d_printf(_("\nShutdown of remote machine succeeded\n"));
                DEBUG(5,("Shutdown of remote machine succeeded\n"));
        } else {
                DEBUG(1,("Shutdown of remote machine failed!\n"));
        }
-       return result;
+       return werror_to_ntstatus(result);
 }
 
 /**
@@ -5242,6 +5668,7 @@ NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
        struct lsa_StringLarge msg_string;
        NTSTATUS result;
        WERROR werr;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (c->opt_comment) {
                msg = c->opt_comment;
@@ -5253,11 +5680,15 @@ NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
        }
 
        /* create an entry */
-       result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
+       result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
                        &msg_string, timeout, c->opt_force, c->opt_reboot,
                        &werr);
+       if (!NT_STATUS_IS_OK(result)) {
+               d_fprintf(stderr, "\nShutdown of remote machine failed\n");
+               return result;
+       }
 
-       if (NT_STATUS_IS_OK(result)) {
+       if (W_ERROR_IS_OK(werr)) {
                d_printf(_("\nShutdown of remote machine succeeded\n"));
        } else {
                d_fprintf(stderr, "\nShutdown of remote machine failed\n");
@@ -5267,7 +5698,7 @@ NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
                        d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
        }
 
-       return result;
+       return werror_to_ntstatus(werr);
 }
 
 /**
@@ -5293,12 +5724,12 @@ static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
                return 0;
        }
 
-       rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
+       rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
                             rpc_init_shutdown_internals, argc, argv);
 
        if (rc) {
                DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
-               rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
+               rc = run_rpc_command(c, NULL, &ndr_table_winreg, 0,
                                     rpc_reg_shutdown_internals, argc, argv);
        }
 
@@ -5335,7 +5766,7 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
                                                const char **argv)
 {
        struct policy_handle connect_pol, domain_pol, user_pol;
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status, result;
        char *acct_name;
        struct lsa_String lsa_acct_name;
        uint32 acb_info;
@@ -5344,6 +5775,7 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
        uint32_t access_granted = 0;
        union samr_UserInfo info;
        unsigned int orig_timeout;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 2) {
                d_printf("%s\n%s",
@@ -5366,21 +5798,31 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
        init_lsa_String(&lsa_acct_name, acct_name);
 
        /* Get samr policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
-                                       &domain_pol);
+                                       discard_const_p(struct dom_sid2, domain_sid),
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
@@ -5397,19 +5839,23 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
                     SAMR_USER_ACCESS_GET_ATTRIBUTES |
                     SAMR_USER_ACCESS_SET_ATTRIBUTES;
 
-       result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_CreateUser2(b, mem_ctx,
                                         &domain_pol,
                                         &lsa_acct_name,
                                         acb_info,
                                         acct_flags,
                                         &user_pol,
                                         &access_granted,
-                                        &user_rid);
-
+                                        &user_rid,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        /* And restore our original timeout. */
        rpccli_set_timeout(pipe_hnd, orig_timeout);
 
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
                        acct_name, nt_errstr(result));
                goto done;
@@ -5429,12 +5875,17 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
                info.info23.info.acct_flags = ACB_DOMTRUST;
                info.info23.password = crypt_pwd;
 
-               result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
                                                  &user_pol,
                                                  23,
-                                                 &info);
+                                                 &info,
+                                                 &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto done;
+               }
 
                if (!NT_STATUS_IS_OK(result)) {
+                       status = result;
                        DEBUG(0,("Could not set trust account password: %s\n",
                                 nt_errstr(result)));
                        goto done;
@@ -5443,7 +5894,7 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
 
  done:
        SAFE_FREE(acct_name);
-       return result;
+       return status;
 }
 
 /**
@@ -5458,7 +5909,7 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
 {
        if (argc > 0 && !c->display_usage) {
-               return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
+               return run_rpc_command(c, NULL, &ndr_table_samr, 0,
                                       rpc_trustdom_add_internals, argc, argv);
        } else {
                d_printf("%s\n%s",
@@ -5496,11 +5947,12 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
                                        const char **argv)
 {
        struct policy_handle connect_pol, domain_pol, user_pol;
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status, result;
        char *acct_name;
        struct dom_sid trust_acct_sid;
        struct samr_Ids user_rids, name_types;
        struct lsa_String lsa_acct_name;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 1) {
                d_printf("%s\n%s",
@@ -5520,47 +5972,72 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
        strupper_m(acct_name);
 
        /* Get samr policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
-                                       &domain_pol);
+                                       discard_const_p(struct dom_sid2, domain_sid),
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
        init_lsa_String(&lsa_acct_name, acct_name);
 
-       result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_LookupNames(b, mem_ctx,
                                         &domain_pol,
                                         1,
                                         &lsa_acct_name,
                                         &user_rids,
-                                        &name_types);
-
+                                        &name_types,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf(_("net rpc trustdom del: LookupNames on user %s "
+                          "failed %s\n"),
+                       acct_name, nt_errstr(status));
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_printf(_("net rpc trustdom del: LookupNames on user %s "
                           "failed %s\n"),
                        acct_name, nt_errstr(result) );
                goto done;
        }
 
-       result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenUser(b, mem_ctx,
                                      &domain_pol,
                                      MAXIMUM_ALLOWED_ACCESS,
                                      user_rids.ids[0],
-                                     &user_pol);
+                                     &user_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
+                          "%s\n"),
+                       acct_name, nt_errstr(status) );
+               goto done;
+       }
 
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
                           "%s\n"),
                        acct_name, nt_errstr(result) );
@@ -5574,22 +6051,39 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
 
        /* remove the sid */
 
-       result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
                                                           &user_pol,
-                                                          &trust_acct_sid);
+                                                          &trust_acct_sid,
+                                                          &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
+                          " on user %s failed %s\n"),
+                       acct_name, nt_errstr(status));
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
                           " on user %s failed %s\n"),
                        acct_name, nt_errstr(result) );
                goto done;
        }
 
+
        /* Delete user */
 
-       result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
-                                       &user_pol);
+       status = dcerpc_samr_DeleteUser(b, mem_ctx,
+                                       &user_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
+                          "%s\n"),
+                       acct_name, nt_errstr(status));
+               goto done;
+       }
 
        if (!NT_STATUS_IS_OK(result)) {
+               result = status;
                d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
                           "%s\n"),
                        acct_name, nt_errstr(result) );
@@ -5603,7 +6097,7 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
        }
 
  done:
-       return result;
+       return status;
 }
 
 /**
@@ -5618,7 +6112,7 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
 {
        if (argc > 0 && !c->display_usage) {
-               return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
+               return run_rpc_command(c, NULL, &ndr_table_samr, 0,
                                       rpc_trustdom_del_internals, argc, argv);
        } else {
                d_printf("%s\n%s",
@@ -5637,6 +6131,8 @@ static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
        const char *buffer = NULL;
        struct rpc_pipe_client *netr;
        NTSTATUS status;
+       WERROR result;
+       struct dcerpc_binding_handle *b;
 
        /* Use NetServerEnum2 */
 
@@ -5656,21 +6152,27 @@ static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
                return status;
        }
 
-       status = rpccli_netr_GetDcName(netr, mem_ctx,
-                                      cli->desthost,
+       b = netr->binding_handle;
+
+       status = dcerpc_netr_GetDcName(b, mem_ctx,
+                                      netr->desthost,
                                       domain_name,
                                       &buffer,
-                                      NULL);
+                                      &result);
        TALLOC_FREE(netr);
 
-       if (NT_STATUS_IS_OK(status)) {
+       if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
                return status;
        }
 
        DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
                 for domain %s\n", domain_name));
 
-       return status;
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       return werror_to_ntstatus(result);
 }
 
 /**
@@ -5692,13 +6194,14 @@ static int rpc_trustdom_establish(struct net_context *c, int argc,
        struct rpc_pipe_client *pipe_hnd = NULL;
        struct policy_handle connect_hnd;
        TALLOC_CTX *mem_ctx;
-       NTSTATUS nt_status;
+       NTSTATUS nt_status, result;
        struct dom_sid *domain_sid;
 
        char* domain_name;
        char* acct_name;
        fstring pdc_name;
        union lsa_PolicyInformation *info = NULL;
+       struct dcerpc_binding_handle *b;
 
        /*
         * Connect to \\server\ipc$ as 'our domain' account with password
@@ -5792,6 +6295,8 @@ static int rpc_trustdom_establish(struct net_context *c, int argc,
                return -1;
        }
 
+       b = pipe_hnd->binding_handle;
+
        nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
                                         &connect_hnd);
        if (NT_STATUS_IS_ERR(nt_status)) {
@@ -5804,10 +6309,11 @@ static int rpc_trustdom_establish(struct net_context *c, int argc,
 
        /* Querying info level 5 */
 
-       nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
+       nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
                                               &connect_hnd,
                                               LSA_POLICY_INFO_ACCOUNT_DOMAIN,
-                                              &info);
+                                              &info,
+                                              &result);
        if (NT_STATUS_IS_ERR(nt_status)) {
                DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
                        nt_errstr(nt_status)));
@@ -5815,6 +6321,13 @@ static int rpc_trustdom_establish(struct net_context *c, int argc,
                talloc_destroy(mem_ctx);
                return -1;
        }
+       if (NT_STATUS_IS_ERR(result)) {
+               DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
+                       nt_errstr(result)));
+               cli_shutdown(cli);
+               talloc_destroy(mem_ctx);
+               return -1;
+       }
 
        domain_sid = info->account_domain.sid;
 
@@ -5836,7 +6349,7 @@ static int rpc_trustdom_establish(struct net_context *c, int argc,
         * Close the pipes and clean up
         */
 
-       nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
+       nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
        if (NT_STATUS_IS_ERR(nt_status)) {
                DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
                        nt_errstr(nt_status)));
@@ -5928,34 +6441,42 @@ static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
                                      struct dom_sid dom_sid,
                                      const char *trusted_dom_name)
 {
-       NTSTATUS nt_status;
+       NTSTATUS nt_status, result;
        union lsa_TrustedDomainInfo *info = NULL;
        char *cleartextpwd = NULL;
-       uint8_t session_key[16];
-       DATA_BLOB session_key_blob;
+       DATA_BLOB session_key;
        DATA_BLOB data = data_blob_null;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
-       nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
+       nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
                                                           pol,
                                                           &dom_sid,
                                                           LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
-                                                          &info);
+                                                          &info,
+                                                          &result);
        if (NT_STATUS_IS_ERR(nt_status)) {
                DEBUG(0,("Could not query trusted domain info. Error was %s\n",
                nt_errstr(nt_status)));
                goto done;
        }
+       if (NT_STATUS_IS_ERR(result)) {
+               nt_status = result;
+               DEBUG(0,("Could not query trusted domain info. Error was %s\n",
+               nt_errstr(result)));
+               goto done;
+       }
 
        data = data_blob(info->password.password->data,
                         info->password.password->length);
 
-       if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
-               DEBUG(0, ("Could not retrieve password hash\n"));
+       nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
                goto done;
        }
 
-       session_key_blob = data_blob_const(session_key, sizeof(session_key));
-       cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
+       cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
+       data_blob_free(&session_key);
 
        if (cleartextpwd == NULL) {
                DEBUG(0,("retrieved NULL password\n"));
@@ -5989,9 +6510,8 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
        TALLOC_CTX* mem_ctx;
        struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_hnd = NULL;
-       NTSTATUS nt_status;
+       NTSTATUS nt_status, result;
        const char *domain_name = NULL;
-       struct dom_sid *queried_dom_sid;
        struct policy_handle connect_hnd;
        union lsa_PolicyInformation *info = NULL;
 
@@ -6000,6 +6520,7 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
        int i;
        struct lsa_DomainList dom_list;
        fstring pdc_name;
+       struct dcerpc_binding_handle *b;
 
        if (c->display_usage) {
                d_printf(  "%s\n"
@@ -6021,11 +6542,11 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
         * or to remote one given in command line
         */
 
-       if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
+       if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
                domain_name = c->opt_workgroup;
                c->opt_target_workgroup = c->opt_workgroup;
        } else {
-               fstrcpy(pdc_name, global_myname());
+               fstrcpy(pdc_name, lp_netbios_name());
                domain_name = talloc_strdup(mem_ctx, lp_workgroup());
                c->opt_target_workgroup = domain_name;
        };
@@ -6049,6 +6570,8 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
                return -1;
        };
 
+       b = pipe_hnd->binding_handle;
+
        nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
                                        &connect_hnd);
        if (NT_STATUS_IS_ERR(nt_status)) {
@@ -6060,10 +6583,11 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
        };
 
        /* query info level 5 to obtain sid of a domain being queried */
-       nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
+       nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
                                               &connect_hnd,
                                               LSA_POLICY_INFO_ACCOUNT_DOMAIN,
-                                              &info);
+                                              &info,
+                                              &result);
 
        if (NT_STATUS_IS_ERR(nt_status)) {
                DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
@@ -6072,8 +6596,13 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
                talloc_destroy(mem_ctx);
                return -1;
        }
-
-       queried_dom_sid = info->account_domain.sid;
+       if (NT_STATUS_IS_ERR(result)) {
+               DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
+                       nt_errstr(result)));
+               cli_shutdown(cli);
+               talloc_destroy(mem_ctx);
+               return -1;
+       }
 
        /*
         * Keep calling LsaEnumTrustdom over opened pipe until
@@ -6083,11 +6612,12 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
        d_printf(_("Vampire trusted domains:\n\n"));
 
        do {
-               nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
+               nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
                                                    &connect_hnd,
                                                    &enum_ctx,
                                                    &dom_list,
-                                                   (uint32_t)-1);
+                                                   (uint32_t)-1,
+                                                   &result);
                if (NT_STATUS_IS_ERR(nt_status)) {
                        DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
                                nt_errstr(nt_status)));
@@ -6095,13 +6625,22 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
                        talloc_destroy(mem_ctx);
                        return -1;
                };
+               if (NT_STATUS_IS_ERR(result)) {
+                       nt_status = result;
+                       DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
+                               nt_errstr(result)));
+                       cli_shutdown(cli);
+                       talloc_destroy(mem_ctx);
+                       return -1;
+               };
+
 
                for (i = 0; i < dom_list.count; i++) {
 
                        print_trusted_domain(dom_list.domains[i].sid,
                                             dom_list.domains[i].name.string);
 
-                       nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd, 
+                       nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
                                                           *dom_list.domains[i].sid,
                                                           dom_list.domains[i].name.string);
                        if (!NT_STATUS_IS_OK(nt_status)) {
@@ -6120,7 +6659,7 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
        } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
 
        /* close this connection before doing next one */
-       nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
+       nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
        if (NT_STATUS_IS_ERR(nt_status)) {
                DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
                        nt_errstr(nt_status)));
@@ -6142,12 +6681,13 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        TALLOC_CTX* mem_ctx;
        struct cli_state *cli = NULL, *remote_cli = NULL;
        struct rpc_pipe_client *pipe_hnd = NULL;
-       NTSTATUS nt_status;
+       NTSTATUS nt_status, result;
        const char *domain_name = NULL;
        struct dom_sid *queried_dom_sid;
        int ascii_dom_name_len;
        struct policy_handle connect_hnd;
        union lsa_PolicyInformation *info = NULL;
+       struct dcerpc_binding_handle *b = NULL;
 
        /* trusted domains listing variables */
        unsigned int num_domains, enum_ctx = 0;
@@ -6180,11 +6720,11 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
         * or to remote one given in command line
         */
 
-       if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
+       if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
                domain_name = c->opt_workgroup;
                c->opt_target_workgroup = c->opt_workgroup;
        } else {
-               fstrcpy(pdc_name, global_myname());
+               fstrcpy(pdc_name, lp_netbios_name());
                domain_name = talloc_strdup(mem_ctx, lp_workgroup());
                c->opt_target_workgroup = domain_name;
        };
@@ -6208,6 +6748,8 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                return -1;
        };
 
+       b = pipe_hnd->binding_handle;
+
        nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
                                        &connect_hnd);
        if (NT_STATUS_IS_ERR(nt_status)) {
@@ -6217,12 +6759,13 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                talloc_destroy(mem_ctx);
                return -1;
        };
-       
+
        /* query info level 5 to obtain sid of a domain being queried */
-       nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
+       nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
                                               &connect_hnd,
                                               LSA_POLICY_INFO_ACCOUNT_DOMAIN,
-                                              &info);
+                                              &info,
+                                              &result);
 
        if (NT_STATUS_IS_ERR(nt_status)) {
                DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
@@ -6231,6 +6774,13 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                talloc_destroy(mem_ctx);
                return -1;
        }
+       if (NT_STATUS_IS_ERR(result)) {
+               DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
+                       nt_errstr(result)));
+               cli_shutdown(cli);
+               talloc_destroy(mem_ctx);
+               return -1;
+       }
 
        queried_dom_sid = info->account_domain.sid;
 
@@ -6244,11 +6794,12 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        found_domain = false;
 
        do {
-               nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
+               nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
                                                    &connect_hnd,
                                                    &enum_ctx,
                                                    &dom_list,
-                                                   (uint32_t)-1);
+                                                   (uint32_t)-1,
+                                                   &result);
                if (NT_STATUS_IS_ERR(nt_status)) {
                        DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
                                nt_errstr(nt_status)));
@@ -6256,6 +6807,14 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                        talloc_destroy(mem_ctx);
                        return -1;
                };
+               if (NT_STATUS_IS_ERR(result)) {
+                       DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
+                               nt_errstr(result)));
+                       cli_shutdown(cli);
+                       talloc_destroy(mem_ctx);
+                       return -1;
+               };
+
 
                for (i = 0; i < dom_list.count; i++) {
                        print_trusted_domain(dom_list.domains[i].sid,
@@ -6275,7 +6834,7 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        }
 
        /* close this connection before doing next one */
-       nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
+       nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
        if (NT_STATUS_IS_ERR(nt_status)) {
                DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
                        nt_errstr(nt_status)));
@@ -6283,13 +6842,13 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                talloc_destroy(mem_ctx);
                return -1;
        };
-       
+
        TALLOC_FREE(pipe_hnd);
 
        /*
         * Listing trusting domains (stored in passdb backend, if local)
         */
-       
+
        d_printf(_("\nTrusting domains list:\n\n"));
 
        /*
@@ -6304,11 +6863,14 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                return -1;
        };
 
+       b = pipe_hnd->binding_handle;
+
        /* SamrConnect2 */
-       nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       nt_status = dcerpc_samr_Connect2(b, mem_ctx,
                                         pipe_hnd->desthost,
                                         SAMR_ACCESS_LOOKUP_DOMAIN,
-                                        &connect_hnd);
+                                        &connect_hnd,
+                                        &result);
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
                        nt_errstr(nt_status)));
@@ -6316,14 +6878,23 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                talloc_destroy(mem_ctx);
                return -1;
        };
+       if (!NT_STATUS_IS_OK(result)) {
+               nt_status = result;
+               DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
+                       nt_errstr(result)));
+               cli_shutdown(cli);
+               talloc_destroy(mem_ctx);
+               return -1;
+       };
 
        /* SamrOpenDomain - we have to open domain policy handle in order to be
           able to enumerate accounts*/
-       nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                           &connect_hnd,
                                           SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
                                           queried_dom_sid,
-                                          &domain_hnd);
+                                          &domain_hnd,
+                                          &result);
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0, ("Couldn't open domain object. Error was %s\n",
                        nt_errstr(nt_status)));
@@ -6331,6 +6902,14 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                talloc_destroy(mem_ctx);
                return -1;
        };
+       if (!NT_STATUS_IS_OK(result)) {
+               nt_status = result;
+               DEBUG(0, ("Couldn't open domain object. Error was %s\n",
+                       nt_errstr(result)));
+               cli_shutdown(cli);
+               talloc_destroy(mem_ctx);
+               return -1;
+       };
 
        /*
         * perform actual enumeration
@@ -6341,13 +6920,14 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        enum_ctx = 0;   /* reset enumeration context from last enumeration */
        do {
 
-               nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
+               nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
                                                        &domain_hnd,
                                                        &enum_ctx,
                                                        ACB_DOMTRUST,
                                                        &trusts,
                                                        0xffff,
-                                                       &num_domains);
+                                                       &num_domains,
+                                                       &result);
                if (NT_STATUS_IS_ERR(nt_status)) {
                        DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
                                nt_errstr(nt_status)));
@@ -6355,10 +6935,18 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                        talloc_destroy(mem_ctx);
                        return -1;
                };
+               if (NT_STATUS_IS_ERR(result)) {
+                       nt_status = result;
+                       DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
+                               nt_errstr(result)));
+                       cli_shutdown(cli);
+                       talloc_destroy(mem_ctx);
+                       return -1;
+               };
 
                for (i = 0; i < num_domains; i++) {
 
-                       char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
+                       char *str = discard_const_p(char, trusts->entries[i].name.string);
 
                        found_domain = true;
 
@@ -6388,7 +6976,7 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                                /* query for domain's sid */
                                if (run_rpc_command(
                                            c, remote_cli,
-                                           &ndr_table_lsarpc.syntax_id, 0,
+                                           &ndr_table_lsarpc, 0,
                                            rpc_query_domain_sid, argc,
                                            argv))
                                        d_printf(_("strange - couldn't get domain's sid\n"));
@@ -6403,19 +6991,19 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                        }
                }
 
-       } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
+       } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
 
        if (!found_domain) {
                d_printf("none\n");
        }
 
        /* close opened samr and domain policy handles */
-       nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
+       nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
        };
 
-       nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
+       nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
        };
@@ -6511,20 +7099,16 @@ bool net_rpc_check(struct net_context *c, unsigned flags)
        if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
                return false;
 
-       if ((cli = cli_initialise()) == NULL) {
+       status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
+                               lp_netbios_name(), SMB_SIGNING_DEFAULT,
+                               0, &cli);
+       if (!NT_STATUS_IS_OK(status)) {
                return false;
        }
-
-       status = cli_connect(cli, server_name, &server_ss);
-       if (!NT_STATUS_IS_OK(status))
-               goto done;
-       if (!attempt_netbios_session_request(&cli, global_myname(),
-                                            server_name, &server_ss))
-               goto done;
-       status = cli_negprot(cli);
+       status = cli_negprot(cli, PROTOCOL_NT1);
        if (!NT_STATUS_IS_OK(status))
                goto done;
-       if (cli->protocol < PROTOCOL_NT1)
+       if (cli_state_protocol(cli) < PROTOCOL_NT1)
                goto done;
 
        ret = true;
@@ -6544,7 +7128,7 @@ static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
+       return run_rpc_command(c, NULL, &ndr_table_netlogon,
                               NET_FLAGS_ANONYMOUS,
                               rpc_samdump_internals, argc, argv);
 }
@@ -6593,10 +7177,7 @@ static int rpc_vampire(struct net_context *c, int argc, const char **argv)
                        return 0;
                }
 
-               return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
-                                      NET_FLAGS_ANONYMOUS,
-                                      rpc_vampire_internals,
-                                      argc, argv);
+               return rpc_vampire_passdb(c, argc, argv);
        }
 
        return net_run_function(c, argc, argv, "net rpc vampire", func);
@@ -6636,30 +7217,30 @@ static int rpc_printer_migrate_all(struct net_context *c, int argc,
                return -1;
        }
 
-       ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                              rpc_printer_migrate_printers_internals, argc,
                              argv);
        if (ret)
                return ret;
 
-       ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                              rpc_printer_migrate_drivers_internals, argc,
                              argv);
        if (ret)
                return ret;
 
-       ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                              rpc_printer_migrate_forms_internals, argc, argv);
        if (ret)
                return ret;
 
-       ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                              rpc_printer_migrate_settings_internals, argc,
                              argv);
        if (ret)
                return ret;
 
-       return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_migrate_security_internals, argc,
                               argv);
 
@@ -6692,7 +7273,7 @@ static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
                return -1;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_migrate_drivers_internals,
                               argc, argv);
 }
@@ -6724,7 +7305,7 @@ static int rpc_printer_migrate_forms(struct net_context *c, int argc,
                return -1;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_migrate_forms_internals,
                               argc, argv);
 }
@@ -6756,7 +7337,7 @@ static int rpc_printer_migrate_printers(struct net_context *c, int argc,
                return -1;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_migrate_printers_internals,
                               argc, argv);
 }
@@ -6788,7 +7369,7 @@ static int rpc_printer_migrate_security(struct net_context *c, int argc,
                return -1;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_migrate_security_internals,
                               argc, argv);
 }
@@ -6821,7 +7402,7 @@ static int rpc_printer_migrate_settings(struct net_context *c, int argc,
                return -1;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_migrate_settings_internals,
                               argc, argv);
 }
@@ -6919,7 +7500,7 @@ static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_list_internals,
                               argc, argv);
 }
@@ -6946,7 +7527,7 @@ static int rpc_printer_driver_list(struct net_context *c, int argc,
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_driver_list_internals,
                               argc, argv);
 }
@@ -6973,7 +7554,7 @@ static int rpc_printer_publish_publish(struct net_context *c, int argc,
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_publish_publish_internals,
                               argc, argv);
 }
@@ -6999,7 +7580,7 @@ static int rpc_printer_publish_update(struct net_context *c, int argc, const cha
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_publish_update_internals,
                               argc, argv);
 }
@@ -7026,7 +7607,7 @@ static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_publish_unpublish_internals,
                               argc, argv);
 }
@@ -7053,7 +7634,7 @@ static int rpc_printer_publish_list(struct net_context *c, int argc,
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_publish_list_internals,
                               argc, argv);
 }
@@ -7119,7 +7700,7 @@ static int rpc_printer_publish(struct net_context *c, int argc,
                        net_display_usage_from_functable(func);
                        return 0;
                }
-               return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+               return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_publish_list_internals,
                               argc, argv);
        }
@@ -7222,7 +7803,7 @@ int net_rpc_printer(struct net_context *c, int argc, const char **argv)
                        net_display_usage_from_functable(func);
                        return 0;
                }
-               return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
+               return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
                               rpc_printer_list_internals,
                               argc, argv);
        }
@@ -7421,10 +8002,26 @@ int net_rpc(struct net_context *c, int argc, const char **argv)
                        N_("net rpc shell\n"
                           "    Open interactive shell on remote server")
                },
+               {
+                       "trust",
+                       net_rpc_trust,
+                       NET_TRANSPORT_RPC,
+                       N_("Manage trusts"),
+                       N_("net rpc trust\n"
+                          "    Manage trusts")
+               },
+               {
+                       "conf",
+                       net_rpc_conf,
+                       NET_TRANSPORT_RPC,
+                       N_("Configure a remote samba server"),
+                       N_("net rpc conf\n"
+                          "    Configure a remote samba server")
+               },
                {NULL, NULL, 0, NULL, NULL}
        };
 
-       status = libnetapi_init(&c->netapi_ctx);
+       status = libnetapi_net_init(&c->netapi_ctx);
        if (status != 0) {
                return -1;
        }