s3-rpc_client: Move client pipe functions to own header.
[samba.git] / source3 / utils / net_rpc.c
index 896ea8cc653217646f1172edb86089c86c41ca63..ae7d5d0c1f0eda88f4378dfefb28c9c9e299d004 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/ndr_samr_c.h"
+#include "rpc_client/cli_samr.h"
+#include "rpc_client/init_samr.h"
+#include "../librpc/gen_ndr/ndr_lsa_c.h"
+#include "rpc_client/cli_lsarpc.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"
 
 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
@@ -52,36 +68,48 @@ static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
  **/
 
 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                                  DOM_SID **domain_sid,
+                                  struct dom_sid **domain_sid,
                                   const char **domain_name)
 {
        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)) {
-               d_fprintf(stderr, _("open_policy failed: %s\n"),
-                         nt_errstr(result));
-               return result;
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, "open_policy %s: %s\n",
+                         _("failed"),
+                         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 failed: %s\n"),
+               d_fprintf(stderr, "lsaquery %s: %s\n",
+                         _("failed"),
                          nt_errstr(result));
                return result;
        }
@@ -89,7 +117,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;
@@ -119,7 +147,7 @@ int run_rpc_command(struct net_context *c,
        struct rpc_pipe_client *pipe_hnd = NULL;
        TALLOC_CTX *mem_ctx;
        NTSTATUS nt_status;
-       DOM_SID *domain_sid;
+       struct dom_sid *domain_sid;
        const char *domain_name;
        int ret = -1;
 
@@ -182,7 +210,8 @@ int run_rpc_command(struct net_context *c,
                        }
                        if (!NT_STATUS_IS_OK(nt_status)) {
                                DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
-                                       get_pipe_name_from_iface(interface),
+                                         get_pipe_name_from_syntax(
+                                                 talloc_tos(), interface),
                                        nt_errstr(nt_status) ));
                                goto fail;
                        }
@@ -231,7 +260,7 @@ fail:
  **/
 
 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -264,9 +293,11 @@ static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc changetrustpw\n"
-                          "    Change the machine trust password\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Change the machine trust password"));
                return 0;
        }
 
@@ -297,7 +328,7 @@ int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
  **/
 
 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -309,7 +340,7 @@ static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
        fstring trust_passwd;
        unsigned char orig_trust_passwd_hash[16];
        NTSTATUS result;
-       uint32 sec_channel_type;
+       enum netr_SchannelType sec_channel_type;
 
        result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
                                          &pipe_hnd);
@@ -395,9 +426,11 @@ static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
        int rc = -1;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc oldjoin\n"
-                          "    Join a domain the old way\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Join a domain the old way"));
                return 0;
        }
 
@@ -425,8 +458,9 @@ static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
 int net_rpc_join(struct net_context *c, int argc, const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
-                          "net rpc join -U <username>[%%password] <type>\n"
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc join -U <username>[%%password] <type>\n"
                           "  Join a domain\n"
                           "    username\tName of the admin user"
                           "    password\tPassword of the admin user, will "
@@ -473,7 +507,7 @@ int net_rpc_join(struct net_context *c, int argc, const char **argv)
  **/
 
 NTSTATUS rpc_info_internals(struct net_context *c,
-                       const DOM_SID *domain_sid,
+                       const struct dom_sid *domain_sid,
                        const char *domain_name,
                        struct cli_state *cli,
                        struct rpc_pipe_client *pipe_hnd,
@@ -482,39 +516,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);
+                                       &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);
@@ -527,7 +582,7 @@ NTSTATUS rpc_info_internals(struct net_context *c,
        }
 
  done:
-       return result;
+       return status;
 }
 
 /**
@@ -540,9 +595,11 @@ NTSTATUS rpc_info_internals(struct net_context *c,
 int net_rpc_info(struct net_context *c, int argc, const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc info\n"
-                          "  Display information about the domain\n"));
+                          "  %s\n",
+                        _("Usage:"),
+                        _("Display information about the domain"));
                return 0;
        }
 
@@ -568,7 +625,7 @@ int net_rpc_info(struct net_context *c, int argc, const char **argv)
  **/
 
 static NTSTATUS rpc_getsid_internals(struct net_context *c,
-                       const DOM_SID *domain_sid,
+                       const struct dom_sid *domain_sid,
                        const char *domain_name,
                        struct cli_state *cli,
                        struct rpc_pipe_client *pipe_hnd,
@@ -599,15 +656,23 @@ static NTSTATUS rpc_getsid_internals(struct net_context *c,
 
 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
 {
+       int conn_flags = NET_FLAGS_PDC;
+
+       if (!c->opt_user_specified) {
+               conn_flags |= NET_FLAGS_ANONYMOUS;
+       }
+
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc getsid\n"
-                          "    Fetch domain SID into local secrets.tdb\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Fetch domain SID into local secrets.tdb"));
                return 0;
        }
 
        return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
-                              NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
+                              conn_flags,
                               rpc_getsid_internals,
                               argc, argv);
 }
@@ -705,6 +770,57 @@ static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
        return status;
 }
 
+/**
+ * Set a user's primary group
+ *
+ * @param argc  Standard main() style argc.
+ * @param argv  Standard main() style argv. Initial components are already
+ *              stripped.
+ *
+ * @return A shell status integer (0 for success).
+ **/
+
+static int rpc_user_setprimarygroup(struct net_context *c, int argc,
+                                   const char **argv)
+{
+       NET_API_STATUS status;
+       uint8_t *buffer;
+       struct GROUP_INFO_2 *g2;
+       struct USER_INFO_1051 u1051;
+       uint32_t parm_err = 0;
+
+       if (argc != 2 || c->display_usage) {
+               rpc_user_usage(c, argc, argv);
+               return 0;
+       }
+
+       status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
+       if (status) {
+               d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
+                         argv[1],
+                         libnetapi_get_error_string(c->netapi_ctx, status));
+               return status;
+       }
+       g2 = (struct GROUP_INFO_2 *)buffer;
+
+       u1051.usri1051_primary_group_id = g2->grpi2_group_id;
+
+       NetApiBufferFree(buffer);
+
+       status = NetUserSetInfo(c->opt_host, argv[0], 1051,
+                               (uint8_t *)&u1051, &parm_err);
+       if (status) {
+               d_fprintf(stderr,
+                         _("Failed to set user's primary group %s to %s - "
+                           "%s\n"), argv[0], argv[1],
+                         libnetapi_get_error_string(c->netapi_ctx, status));
+       } else {
+               d_printf(_("Set primary group of user %s to %s\n"), argv[0],
+                        argv[1]);
+       }
+       return status;
+}
+
 /**
  * Delete a user from a remote RPC server.
  *
@@ -769,8 +885,11 @@ static int rpc_user_password(struct net_context *c, int argc, const char **argv)
                if (ret == -1) {
                        return -1;
                }
-               u1003.usri1003_password = getpass(prompt);
+               u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
                SAFE_FREE(prompt);
+               if (u1003.usri1003_password == NULL) {
+                       return -1;
+               }
        }
 
        status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
@@ -865,7 +984,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,
@@ -927,7 +1046,7 @@ int net_rpc_user(struct net_context *c, int argc, const char **argv)
                        NET_TRANSPORT_RPC,
                        N_("List domain groups of user"),
                        N_("net rpc user info\n"
-                          "    Lis domain groups of user")
+                          "    List domain groups of user")
                },
                {
                        "delete",
@@ -953,10 +1072,18 @@ int net_rpc_user(struct net_context *c, int argc, const char **argv)
                        N_("net rpc user rename\n"
                           "    Rename specified user")
                },
+               {
+                       "setprimarygroup",
+                       rpc_user_setprimarygroup,
+                       NET_TRANSPORT_RPC,
+                       "Set a user's primary group",
+                       "net rpc user setprimarygroup\n"
+                       "    Set a user's primary group"
+               },
                {NULL, NULL, 0, NULL, NULL}
        };
 
-       status = libnetapi_init(&c->netapi_ctx);
+       status = libnetapi_net_init(&c->netapi_ctx);
        if (status != 0) {
                return -1;
        }
@@ -968,9 +1095,11 @@ int net_rpc_user(struct net_context *c, int argc, const char **argv)
 
        if (argc == 0) {
                if (c->display_usage) {
-                       d_printf(_("Usage:\n"));
-                       d_printf(_("net rpc user\n"
-                                  "    List all users\n"));
+                       d_printf(  "%s\n"
+                                  "net rpc user\n"
+                                  "    %s\n",
+                                _("Usage:"),
+                                _("List all users"));
                        net_display_usage_from_functable(func);
                        return 0;
                }
@@ -1013,13 +1142,15 @@ 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;
-       DOM_SID sid;
+       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, _("usage: %s <username>\n"), ctx->whoami);
+               d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
+                         ctx->whoami);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -1027,66 +1158,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,
@@ -1096,18 +1242,24 @@ 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, _("usage: %s show <username>\n"),ctx->whoami);
+               d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
+                         ctx->whoami);
                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;
        }
@@ -1147,21 +1299,26 @@ 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, _("usage: %s <username> [new value|NULL]\n"),
-                         ctx->whoami);
+               d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
+                         _("Usage:"), ctx->whoami);
                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;
        }
@@ -1193,17 +1350,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) \
@@ -1232,29 +1395,34 @@ 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") &&
             !strequal(argv[0], "no"))) {
                /* TRANSATORS: The yes|no here are program keywords. Please do
                   not translate. */
-               d_fprintf(stderr, _("usage: %s <username> [yes|no]\n"),
+               d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
                          ctx->whoami);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
        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;
        }
@@ -1279,11 +1447,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]);
@@ -1291,7 +1463,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,
@@ -1401,7 +1573,7 @@ static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
  **/
 
 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -1411,12 +1583,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;
@@ -1427,64 +1600,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);
+                                       &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) {
@@ -1496,31 +1703,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) {
@@ -1531,14 +1755,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;
                }
 
@@ -1548,59 +1772,81 @@ 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"));
                        } else {
                                if (c->opt_verbose)
-                                       d_printf(_("failed\n"));
+                                       d_printf("%s\n", _("failed"));
                                goto done;
                        }
                }
 
-               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;
+               }
+
+               status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
+                                                   &group_pol,
+                                                   &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       break;
                }
 
-               result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
-                                                   &group_pol);
+               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;
 
 }
 
@@ -1687,73 +1933,77 @@ static int rpc_group_add(struct net_context *c, int argc, const char **argv)
 static NTSTATUS get_sid_from_name(struct cli_state *cli,
                                TALLOC_CTX *mem_ctx,
                                const char *name,
-                               DOM_SID *sid,
+                               struct dom_sid *sid,
                                enum lsa_SidType *type)
 {
-       DOM_SID *sids = NULL;
+       struct dom_sid *sids = NULL;
        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(name, "S-", 2) == 0)) {
 
                /* Try as S-1-5-whatever */
 
-               DOM_SID tmp_sid;
+               struct dom_sid tmp_sid;
 
                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,
                                TALLOC_CTX *mem_ctx,
-                               const DOM_SID *group_sid,
+                               const struct dom_sid *group_sid,
                                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;
 
-       DOM_SID sid;
+       struct dom_sid sid;
 
        sid_copy(&sid, group_sid);
 
@@ -1762,73 +2012,100 @@ 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,
                                TALLOC_CTX *mem_ctx,
-                               const DOM_SID *alias_sid,
+                               const struct dom_sid *alias_sid,
                                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;
 
-       DOM_SID member_sid;
+       struct dom_sid member_sid;
        enum lsa_SidType member_type;
 
-       DOM_SID sid;
+       struct dom_sid sid;
 
        sid_copy(&sid, alias_sid);
 
@@ -1846,49 +2123,64 @@ 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,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -1896,12 +2188,13 @@ static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
                                        int argc,
                                        const char **argv)
 {
-       DOM_SID group_sid;
+       struct dom_sid group_sid;
        enum lsa_SidType group_type;
 
        if (argc != 2 || c->display_usage) {
-               d_printf(_("Usage:\n"
-                          "net rpc group addmem <group> <member>\n"
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc group addmem <group> <member>\n"
                           "  Add a member to a group\n"
                           "    group\tGroup to add member to\n"
                           "    member\tMember to add to group\n"));
@@ -1953,18 +2246,19 @@ static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
 static NTSTATUS rpc_del_groupmem(struct net_context *c,
                                struct rpc_pipe_client *pipe_hnd,
                                TALLOC_CTX *mem_ctx,
-                               const DOM_SID *group_sid,
+                               const struct dom_sid *group_sid,
                                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;
 
-       DOM_SID sid;
+       struct dom_sid sid;
 
        sid_copy(&sid, group_sid);
 
@@ -1972,68 +2266,98 @@ 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,
                                TALLOC_CTX *mem_ctx,
-                               const DOM_SID *alias_sid,
+                               const struct dom_sid *alias_sid,
                                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;
 
-       DOM_SID member_sid;
+       struct dom_sid member_sid;
        enum lsa_SidType member_type;
 
-       DOM_SID sid;
+       struct dom_sid sid;
 
        sid_copy(&sid, alias_sid);
 
@@ -2050,47 +2374,66 @@ 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,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -2098,12 +2441,13 @@ static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
                                        int argc,
                                        const char **argv)
 {
-       DOM_SID group_sid;
+       struct dom_sid group_sid;
        enum lsa_SidType group_type;
 
        if (argc != 2 || c->display_usage) {
-               d_printf(_("Usage:\n"
-                          "net rpc group delmem <group> <member>\n"
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc group delmem <group> <member>\n"
                           "  Delete a member from a group\n"
                           "    group\tGroup to delete member from\n"
                           "    member\tMember to delete from group\n"));
@@ -2169,7 +2513,7 @@ static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
  **/
 
 static NTSTATUS rpc_group_list_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -2178,16 +2522,18 @@ 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(_("Usage:\n"
-                          "net rpc group list [global] [local] [builtin]\n"
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc group list [global] [local] [builtin]\n"
                           "  List groups on RPC server\n"
                           "    global\tList global groups\n"
                           "    local\tList local groups\n"
@@ -2217,22 +2563,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);
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
@@ -2246,10 +2602,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,
@@ -2257,7 +2613,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;
 
@@ -2285,12 +2645,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;
@@ -2303,19 +2667,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;
+                                               }
+                                       }
                                }
                        }
 
@@ -2328,31 +2701,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);
+                                       &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++) {
 
@@ -2362,19 +2747,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;
+                                               }
+                                       }
                                }
                        }
 
@@ -2388,8 +2782,10 @@ 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)
@@ -2403,36 +2799,45 @@ static NTSTATUS rpc_list_group_members(struct net_context *c,
                                        struct rpc_pipe_client *pipe_hnd,
                                        TALLOC_CTX *mem_ctx,
                                        const char *domain_name,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        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;
@@ -2443,15 +2848,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 */
@@ -2482,30 +2891,39 @@ 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;
-       DOM_SID *alias_sids;
+       struct dom_sid *alias_sids;
        char **domains;
        char **names;
        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;
@@ -2535,7 +2953,7 @@ static NTSTATUS rpc_list_alias_members(struct net_context *c,
                return result;
        }
 
-       alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, 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);
@@ -2578,7 +2996,7 @@ static NTSTATUS rpc_list_alias_members(struct net_context *c,
 }
 
 static NTSTATUS rpc_group_members_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -2586,70 +3004,89 @@ 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))
+                                       &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)) {
 
                /* Ok, did not find it in the global sam, try with builtin */
 
-               DOM_SID sid_Builtin;
+               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]);
@@ -2695,7 +3132,8 @@ static int rpc_group_rename_internals(struct net_context *c, int argc, const cha
        uint32_t parm_err;
 
        if (argc != 2) {
-               d_printf(_("Usage: 'net rpc group rename group newname'\n"));
+               d_printf(_("Usage:\n"));
+               d_printf("net rpc group rename group newname\n");
                return -1;
        }
 
@@ -2797,7 +3235,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;
        }
@@ -2938,6 +3376,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) {
@@ -2948,25 +3387,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;
        }
 
@@ -2987,6 +3434,8 @@ static WERROR get_share_info(struct net_context *c,
                ctr1->array = info.info1;
 
                info_ctr->ctr.ctr1 = ctr1;
+
+               break;
        }
        case 2:
        {
@@ -2999,6 +3448,8 @@ static WERROR get_share_info(struct net_context *c,
                ctr2->array = info.info2;
 
                info_ctr->ctr.ctr2 = ctr2;
+
+               break;
        }
        case 502:
        {
@@ -3011,6 +3462,8 @@ static WERROR get_share_info(struct net_context *c,
                ctr502->array = info.info502;
 
                info_ctr->ctr.ctr502 = ctr502;
+
+               break;
        }
        } /* switch */
 done:
@@ -3033,9 +3486,11 @@ static int rpc_share_list(struct net_context *c, int argc, const char **argv)
        uint32_t i, level = 1;
 
        if (c->display_usage) {
-               d_printf(_("Usage\n"
+               d_printf(  "%s\n"
                           "net rpc share list\n"
-                          "    List shares on remote server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("List shares on remote server"));
                return 0;
        }
 
@@ -3066,13 +3521,19 @@ static int rpc_share_list(struct net_context *c, int argc, const char **argv)
 
 static bool check_share_availability(struct cli_state *cli, const char *netname)
 {
-       if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
+       NTSTATUS status;
+
+       status = cli_tcon_andx(cli, netname, "A:", "", 0);
+       if (!NT_STATUS_IS_OK(status)) {
                d_printf(_("skipping   [%s]: not a file share.\n"), netname);
                return false;
        }
 
-       if (!cli_tdis(cli))
+       status = cli_tdis(cli);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
                return false;
+       }
 
        return true;
 }
@@ -3118,7 +3579,7 @@ static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
  **/
 
 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -3134,6 +3595,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);
@@ -3146,6 +3608,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++) {
 
@@ -3167,21 +3630,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;
                }
 
@@ -3211,9 +3680,11 @@ static int rpc_share_migrate_shares(struct net_context *c, int argc,
                                    const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc share migrate shares\n"
-                          "    Migrate shares to local server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate shares to local server"));
                return 0;
        }
 
@@ -3235,7 +3706,7 @@ static int rpc_share_migrate_shares(struct net_context *c, int argc,
  * @param state        arg-pointer
  *
  **/
-static void copy_fn(const char *mnt, file_info *f,
+static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
                    const char *mask, void *state)
 {
        static NTSTATUS nt_status;
@@ -3251,7 +3722,7 @@ static void copy_fn(const char *mnt, 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));
 
@@ -3279,12 +3750,14 @@ static void copy_fn(const char *mnt, 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);
@@ -3292,11 +3765,13 @@ static void copy_fn(const char *mnt, file_info *f,
 
                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;
        }
 
 
@@ -3322,13 +3797,13 @@ static void copy_fn(const char *mnt, 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;
 }
 
 /**
@@ -3340,10 +3815,11 @@ static void copy_fn(const char *mnt, 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;
+       NTSTATUS status;
 
        DEBUG(3,("calling cli_list with mask: %s\n", mask));
 
@@ -3352,16 +3828,17 @@ static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
                d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
                                    "%s\n"),
                        mask, cli_errstr(cp_clistate->cli_share_src));
-               return false;
+               return cli_nt_error(cp_clistate->cli_share_src);
        }
 
-       if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
+       status = cli_list(targetcli, targetpath, cp_clistate->attribute,
+                         copy_fn, cp_clistate);
+       if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, _("listing %s failed with error: %s\n"),
-                       mask, cli_errstr(targetcli));
-               return false;
+                         mask, nt_errstr(status));
        }
 
-       return true;
+       return status;
 }
 
 
@@ -3421,7 +3898,7 @@ bool copy_top_level_perms(struct net_context *c,
  **/
 
 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -3518,10 +3995,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;
                }
        }
@@ -3544,9 +4021,11 @@ done:
 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net share migrate files\n"
-                          "    Migrate files to local server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate files to local server"));
                return 0;
        }
 
@@ -3577,7 +4056,7 @@ static int rpc_share_migrate_files(struct net_context *c, int argc, const char *
  **/
 
 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -3594,6 +4073,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);
@@ -3607,6 +4087,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++) {
 
@@ -3630,14 +4111,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;
@@ -3669,9 +4156,11 @@ static int rpc_share_migrate_security(struct net_context *c, int argc,
                                      const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc share migrate security\n"
-                          "    Migrate share-acls to local server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate share-acls to local server"));
                return 0;
        }
 
@@ -3702,9 +4191,11 @@ static int rpc_share_migrate_all(struct net_context *c, int argc,
        int ret;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc share migrate all\n"
-                          "    Migrates shares including all share settings\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrates shares including all share settings"));
                return 0;
        }
 
@@ -3783,9 +4274,9 @@ static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
 }
 
 struct full_alias {
-       DOM_SID sid;
+       struct dom_sid sid;
        uint32 num_members;
-       DOM_SID *members;
+       struct dom_sid *members;
 };
 
 static int num_server_aliases;
@@ -3811,83 +4302,111 @@ static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
                                        TALLOC_CTX *mem_ctx,
                                        struct policy_handle *connect_pol,
-                                       const DOM_SID *domain_sid)
+                                       const struct dom_sid *domain_sid)
 {
        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))
+                                       &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;
 
                        if (alias.num_members > 0) {
-                               alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
+                               alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
 
                                for (j = 0; j < alias.num_members; j++)
                                        sid_copy(&alias.members[j],
                                                 sid_array.sids[j].sid);
                        }
 
-                       sid_copy(&alias.sid, domain_sid);
-                       sid_append_rid(&alias.sid, groups->entries[i].idx);
+                       sid_compose(&alias.sid, domain_sid,
+                                   groups->entries[i].idx);
 
                        push_alias(mem_ctx, &alias);
                }
        } 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;
 }
 
 /*
@@ -3895,7 +4414,7 @@ static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
  */
 
 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
-                               const DOM_SID *domain_sid,
+                               const struct dom_sid *domain_sid,
                                const char *domain_name,
                                struct cli_state *cli,
                                struct rpc_pipe_client *pipe_hnd,
@@ -3906,6 +4425,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,
@@ -3950,7 +4470,7 @@ static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
                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;
 }
@@ -3961,7 +4481,7 @@ static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
  */
 
 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -3969,70 +4489,76 @@ 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(NT_USER_TOKEN *token, DOM_SID *user_sid)
+static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
 {
        token->num_sids = 4;
 
-       if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
-               d_fprintf(stderr, _("malloc failed\n"));
+       if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
+               d_fprintf(stderr, "malloc %s\n",_("failed"));
                token->num_sids = 0;
                return;
        }
 
-       token->user_sids[0] = *user_sid;
-       sid_copy(&token->user_sids[1], &global_sid_World);
-       sid_copy(&token->user_sids[2], &global_sid_Network);
-       sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
+       token->sids[0] = *user_sid;
+       sid_copy(&token->sids[1], &global_sid_World);
+       sid_copy(&token->sids[2], &global_sid_Network);
+       sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
 }
 
-static void free_user_token(NT_USER_TOKEN *token)
+static void free_user_token(struct security_token *token)
 {
-       SAFE_FREE(token->user_sids);
+       SAFE_FREE(token->sids);
 }
 
-static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
+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->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
-       if (!token->user_sids) {
+       token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
+       if (!token->sids) {
                return;
        }
 
-       sid_copy(&token->user_sids[token->num_sids], sid);
+       sid_copy(&token->sids[token->num_sids], sid);
 
        token->num_sids += 1;
 }
 
 struct user_token {
        fstring name;
-       NT_USER_TOKEN token;
+       struct security_token token;
 };
 
 static void dump_user_token(struct user_token *token)
@@ -4042,23 +4568,23 @@ static void dump_user_token(struct user_token *token)
        d_printf("%s\n", token->name);
 
        for (i=0; i<token->token.num_sids; i++) {
-               d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
+               d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
        }
 }
 
-static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
+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;
        }
 
        return false;
 }
 
-static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
+static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
 {
        int i;
 
@@ -4075,24 +4601,24 @@ static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
  * add them to the token.
  */
 
-static void collect_alias_memberships(NT_USER_TOKEN *token)
+static void collect_alias_memberships(struct security_token *token)
 {
        int num_global_sids = token->num_sids;
        int i;
 
        for (i=0; i<num_global_sids; i++) {
-               collect_sid_memberships(token, token->user_sids[i]);
+               collect_sid_memberships(token, token->sids[i]);
        }
 }
 
-static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
+static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        enum wbcSidType type;
        fstring full_name;
        struct wbcDomainSid wsid;
        char *sid_str = NULL;
-       DOM_SID user_sid;
+       struct dom_sid user_sid;
        uint32_t num_groups;
        gid_t *groups = NULL;
        uint32_t i;
@@ -4115,7 +4641,7 @@ static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *t
                return false;
        }
 
-       if (type != SID_NAME_USER) {
+       if (type != WBC_SID_NAME_USER) {
                wbcFreeMemory(sid_str);
                DEBUG(1, ("%s is not a user\n", full_name));
                return false;
@@ -4142,7 +4668,7 @@ static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *t
 
        for (i = 0; i < num_groups; i++) {
                gid_t gid = groups[i];
-               DOM_SID sid;
+               struct dom_sid sid;
 
                wbc_status = wbcGidToSid(gid, &wsid);
                if (!WBC_ERROR_IS_OK(wbc_status)) {
@@ -4230,7 +4756,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);
@@ -4254,13 +4779,14 @@ static bool get_user_tokens_from_file(FILE *f,
                        return true;
                }
 
-               if (line[strlen(line)-1] == '\n')
+               if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
                        line[strlen(line)-1] = '\0';
+               }
 
                if (line[0] == ' ') {
                        /* We have a SID */
 
-                       DOM_SID sid;
+                       struct dom_sid sid;
                        if(!string_to_sid(&sid, &line[1])) {
                                DEBUG(1,("get_user_tokens_from_file: Could "
                                        "not convert sid %s \n",&line[1]));
@@ -4289,7 +4815,7 @@ static bool get_user_tokens_from_file(FILE *f,
 
                fstrcpy(token->name, line);
                token->token.num_sids = 0;
-               token->token.user_sids = NULL;
+               token->token.sids = NULL;
                continue;
        }
        
@@ -4308,16 +4834,17 @@ static void show_userlist(struct rpc_pipe_client *pipe_hnd,
                        struct user_token *tokens)
 {
        uint16_t fnum;
-       SEC_DESC *share_sd = NULL;
-       SEC_DESC *root_sd = NULL;
+       struct security_descriptor *share_sd = NULL;
+       struct security_descriptor *root_sd = NULL;
        struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
        int i;
        union srvsvc_NetShareInfo info;
        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,
@@ -4424,7 +4951,7 @@ static void collect_share(const char *name, uint32 m,
  **/
 
 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -4504,9 +5031,11 @@ static int rpc_share_allowedusers(struct net_context *c, int argc,
        int result;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc share allowedusers\n"
-                           "    List allowed users\n"));
+                           "    %s\n",
+                         _("Usage:"),
+                         _("List allowed users"));
                return 0;
        }
 
@@ -4618,7 +5147,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;
        }
@@ -4630,8 +5159,9 @@ int net_rpc_share(struct net_context *c, int argc, const char **argv)
 
        if (argc == 0) {
                if (c->display_usage) {
-                       d_printf(_("Usage:\n"
-                                  "net rpc share\n"
+                       d_printf("%s\n%s",
+                                _("Usage:"),
+                                _("net rpc share\n"
                                   "    List shares\n"
                                   "    Alias for net rpc share list\n"));
                        net_display_usage_from_functable(func);
@@ -4665,7 +5195,7 @@ static NTSTATUS rpc_sh_share_add(struct net_context *c,
        struct SHARE_INFO_2 i2;
 
        if ((argc < 2) || (argc > 3)) {
-               d_fprintf(stderr, _("usage: %s <share> <path> [comment]\n"),
+               d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
                          ctx->whoami);
                return NT_STATUS_INVALID_PARAMETER;
        }
@@ -4694,7 +5224,7 @@ static NTSTATUS rpc_sh_share_delete(struct net_context *c,
                                    int argc, const char **argv)
 {
        if (argc != 1) {
-               d_fprintf(stderr, _("usage: %s <share>\n"), ctx->whoami);
+               d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -4710,19 +5240,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, _("usage: %s <share>\n"), ctx->whoami);
+               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;
        }
 
@@ -4893,7 +5428,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;
        }
@@ -4936,7 +5471,7 @@ int net_rpc_file(struct net_context *c, int argc, const char **argv)
  **/
 
 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -4944,17 +5479,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);
 }
 
 /**
@@ -4975,7 +5514,7 @@ static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
  **/
 
 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -4984,16 +5523,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);
 }
 
 /**
@@ -5012,9 +5557,11 @@ static int rpc_shutdown_abort(struct net_context *c, int argc,
        int rc = -1;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc abortshutdown\n"
-                          "    Abort a scheduled shutdown\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Abort a scheduled shutdown"));
                return 0;
        }
 
@@ -5049,7 +5596,7 @@ static int rpc_shutdown_abort(struct net_context *c, int argc,
  **/
 
 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
-                                    const DOM_SID *domain_sid,
+                                    const struct dom_sid *domain_sid,
                                     const char *domain_name,
                                     struct cli_state *cli,
                                     struct rpc_pipe_client *pipe_hnd,
@@ -5057,10 +5604,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;
@@ -5072,17 +5621,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);
 }
 
 /**
@@ -5103,7 +5654,7 @@ NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
  **/
 
 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
-                                   const DOM_SID *domain_sid,
+                                   const struct dom_sid *domain_sid,
                                    const char *domain_name,
                                    struct cli_state *cli,
                                    struct rpc_pipe_client *pipe_hnd,
@@ -5116,6 +5667,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;
@@ -5127,11 +5679,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");
@@ -5141,7 +5697,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);
 }
 
 /**
@@ -5159,9 +5715,11 @@ static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
        int rc =  -1;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc shutdown\n"
-                          "    Shut down a remote RPC server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Shut down a remote RPC server"));
                return 0;
        }
 
@@ -5198,7 +5756,7 @@ static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
  */
 
 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -5207,7 +5765,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;
@@ -5216,14 +5774,17 @@ 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(_("Usage: net rpc trustdom add <domain_name> "
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _(" net rpc trustdom add <domain_name> "
                           "<trust password>\n"));
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       /* 
+       /*
         * Make valid trusting domain account (ie. uppercased and with '$' appended)
         */
 
@@ -5236,21 +5797,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);
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
@@ -5267,19 +5838,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;
@@ -5299,12 +5874,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;
@@ -5313,7 +5893,7 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
 
  done:
        SAFE_FREE(acct_name);
-       return result;
+       return status;
 }
 
 /**
@@ -5331,8 +5911,9 @@ static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
                return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
                                       rpc_trustdom_add_internals, argc, argv);
        } else {
-               d_printf(_("Usage:\n"
-                          "net rpc trustdom add <domain_name> <trust "
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc trustdom add <domain_name> <trust "
                           "password>\n"));
                return -1;
        }
@@ -5356,7 +5937,7 @@ static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
  */
 
 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -5365,14 +5946,17 @@ 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;
-       DOM_SID trust_acct_sid;
+       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(_("Usage: net rpc trustdom del <domain_name>\n"));
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _(" net rpc trustdom del <domain_name>\n"));
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -5387,47 +5971,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);
+                                       &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) );
@@ -5435,29 +6044,45 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
        }
 
        /* append the rid to the domain sid */
-       sid_copy(&trust_acct_sid, domain_sid);
-       if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
+       if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
                goto done;
        }
 
        /* 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) );
@@ -5471,7 +6096,7 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
        }
 
  done:
-       return result;
+       return status;
 }
 
 /**
@@ -5489,8 +6114,9 @@ static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
                return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
                                       rpc_trustdom_del_internals, argc, argv);
        } else {
-               d_printf(_("Usage:\n"
-                          "net rpc trustdom del <domain>\n"));
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc trustdom del <domain>\n"));
                return -1;
        }
 }
@@ -5504,6 +6130,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 */
 
@@ -5523,21 +6151,27 @@ static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
                return status;
        }
 
-       status = rpccli_netr_GetDcName(netr, mem_ctx,
+       b = netr->binding_handle;
+
+       status = dcerpc_netr_GetDcName(b, mem_ctx,
                                       cli->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);
 }
 
 /**
@@ -5559,21 +6193,23 @@ 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;
-       DOM_SID *domain_sid;
+       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
         */
 
        if (argc != 1 || c->display_usage) {
-               d_printf(_("Usage:\n"
-                          "net rpc trustdom establish <domain_name>\n"));
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc trustdom establish <domain_name>\n"));
                return -1;
        }
 
@@ -5658,6 +6294,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)) {
@@ -5670,10 +6308,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)));
@@ -5681,6 +6320,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;
 
@@ -5702,7 +6348,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)));
@@ -5736,8 +6382,9 @@ static int rpc_trustdom_revoke(struct net_context *c, int argc,
        int rc = -1;
 
        if (argc < 1 || c->display_usage) {
-               d_printf(_("Usage:\n"
-                          "net rpc trustdom revoke <domain_name>\n"
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc trustdom revoke <domain_name>\n"
                           "  Revoke trust relationship\n"
                           "    domain_name\tName of domain to revoke trust\n"));
                return -1;
@@ -5761,7 +6408,7 @@ done:
 }
 
 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -5770,50 +6417,54 @@ static NTSTATUS rpc_query_domain_sid(struct net_context *c,
                                        const char **argv)
 {
        fstring str_sid;
-       sid_to_fstring(str_sid, domain_sid);
+       if (!sid_to_fstring(str_sid, domain_sid)) {
+               return NT_STATUS_UNSUCCESSFUL;
+       }
        d_printf("%s\n", str_sid);
        return NT_STATUS_OK;
 }
 
-static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
+static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
 {
-       fstring ascii_sid, padding;
-       int pad_len, col_len = 20;
+       fstring ascii_sid;
 
        /* convert sid into ascii string */
        sid_to_fstring(ascii_sid, dom_sid);
 
-       /* calculate padding space for d_printf to look nicer */
-       pad_len = col_len - strlen(trusted_dom_name);
-       padding[pad_len] = 0;
-       do padding[--pad_len] = ' '; while (pad_len);
-
-       d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
+       d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
 }
 
 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
                                      TALLOC_CTX *mem_ctx,
                                      struct policy_handle *pol,
-                                     DOM_SID dom_sid,
+                                     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 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);
@@ -5858,9 +6509,9 @@ 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;
-       DOM_SID *queried_dom_sid;
+       struct dom_sid *queried_dom_sid;
        struct policy_handle connect_hnd;
        union lsa_PolicyInformation *info = NULL;
 
@@ -5869,11 +6520,14 @@ 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(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc trustdom vampire\n"
-                          "  Vampire trust relationship from remote server\n"));
+                          "  %s\n",
+                        _("Usage:"),
+                        _("Vampire trust relationship from remote server"));
                return 0;
        }
 
@@ -5916,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)) {
@@ -5927,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",
@@ -5939,6 +6596,13 @@ static int rpc_trustdom_vampire(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;
+       }
 
        queried_dom_sid = info->account_domain.sid;
 
@@ -5950,11 +6614,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)));
@@ -5962,6 +6627,15 @@ 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++) {
 
@@ -5987,7 +6661,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)));
@@ -6009,28 +6683,31 @@ 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;
-       DOM_SID *queried_dom_sid;
-       fstring padding;
+       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;
-       int i, pad_len, col_len = 20;
+       int i;
        struct lsa_DomainList dom_list;
        fstring pdc_name;
+       bool found_domain;
 
        /* trusting domains listing variables */
        struct policy_handle domain_hnd;
        struct samr_SamArray *trusts = NULL;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc trustdom list\n"
-                          "    List in- and outgoing trust relationships\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("List incoming and outgoing trust relationships"));
                return 0;
        }
 
@@ -6084,10 +6761,11 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        };
        
        /* 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",
@@ -6096,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;
 
@@ -6103,15 +6788,18 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
         * Keep calling LsaEnumTrustdom over opened pipe until
         * the end of enumeration is reached
         */
-        
+
        d_printf(_("Trusted domains list:\n\n"));
 
+       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)));
@@ -6119,22 +6807,34 @@ 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,
                                             dom_list.domains[i].name.string);
+                       found_domain = true;
                };
 
-               /*
-                * in case of no trusted domains say something rather
-                * than just display blank line
-                */
-               if (!dom_list.count) d_printf(_("none\n"));
 
        } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
 
+       /*
+        * in case of no trusted domains say something rather
+        * than just display blank line
+        */
+       if (!found_domain) {
+               d_printf(_("none\n"));
+       }
+
        /* 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)));
@@ -6163,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)));
@@ -6175,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)));
@@ -6190,21 +6902,32 @@ 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
         */
 
+       found_domain = false;
+
        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)));
@@ -6212,11 +6935,21 @@ 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);
 
+                       found_domain = true;
+
                        /*
                         * get each single domain's sid (do we _really_ need this ?):
                         *  1) connect to domain's pdc
@@ -6228,17 +6961,12 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                        if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
                                str[ascii_dom_name_len - 1] = '\0';
 
-                       /* calculate padding space for d_printf to look nicer */
-                       pad_len = col_len - strlen(str);
-                       padding[pad_len] = 0;
-                       do padding[--pad_len] = ' '; while (pad_len);
-
                        /* set opt_* variables to remote domain */
                        strupper_m(str);
                        c->opt_workgroup = talloc_strdup(mem_ctx, str);
                        c->opt_target_workgroup = c->opt_workgroup;
 
-                       d_printf("%s%s", str, padding);
+                       d_printf("%-20s", str);
 
                        /* connect to remote domain controller */
                        nt_status = net_make_ipc_connection(c,
@@ -6251,8 +6979,7 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                                            &ndr_table_lsarpc.syntax_id, 0,
                                            rpc_query_domain_sid, argc,
                                            argv))
-                                       d_fprintf(stderr,
-                                             _("couldn't get domain's sid\n"));
+                                       d_printf(_("strange - couldn't get domain's sid\n"));
 
                                cli_shutdown(remote_cli);
 
@@ -6260,20 +6987,23 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                                d_fprintf(stderr, _("domain controller is not "
                                          "responding: %s\n"),
                                          nt_errstr(nt_status));
-                       };
-               };
+                               d_printf(_("couldn't get domain's sid\n"));
+                       }
+               }
 
-               if (!num_domains) d_printf("none\n");
+       } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
 
-       } while (NT_STATUS_EQUAL(nt_status, 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));
        };
@@ -6394,9 +7124,11 @@ bool net_rpc_check(struct net_context *c, unsigned flags)
 /* dump sam database via samsync rpc calls */
 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc samdump\n"
-                          "    Dump remote SAM database\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Dump remote SAM database"));
                return 0;
        }
 
@@ -6441,16 +7173,15 @@ static int rpc_vampire(struct net_context *c, int argc, const char **argv)
 
        if (argc == 0) {
                if (c->display_usage) {
-                       d_printf(_("Usage:\n"
+                       d_printf(  "%s\n"
                                   "net rpc vampire\n"
-                                  "    Vampire remote SAM database\n"));
+                                  "    %s\n",
+                                _("Usage:"),
+                                _("Vampire remote SAM database"));
                        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);
@@ -6477,9 +7208,11 @@ static int rpc_printer_migrate_all(struct net_context *c, int argc,
        int ret;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer migrate all\n"
-                          "    Migrate everything from a print server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate everything from a print server"));
                return 0;
        }
 
@@ -6531,9 +7264,11 @@ static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
                                       const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer migrate drivers\n"
-                          "     Migrate print-drivers from a print-server\n"));
+                          "     %s\n",
+                        _("Usage:"),
+                        _("Migrate print-drivers from a print-server"));
                return 0;
        }
 
@@ -6561,9 +7296,11 @@ static int rpc_printer_migrate_forms(struct net_context *c, int argc,
                                     const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer migrate forms\n"
-                          "    Migrate print-forms from a print-server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate print-forms from a print-server"));
                return 0;
        }
 
@@ -6591,9 +7328,11 @@ static int rpc_printer_migrate_printers(struct net_context *c, int argc,
                                        const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer migrate printers\n"
-                          "    Migrate printers from a print-server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate printers from a print-server"));
                return 0;
        }
 
@@ -6621,9 +7360,11 @@ static int rpc_printer_migrate_security(struct net_context *c, int argc,
                                        const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer migrate security\n"
-                          "    Migrate printer-ACLs from a print-server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate printer-ACLs from a print-server"));
                return 0;
        }
 
@@ -6651,10 +7392,12 @@ static int rpc_printer_migrate_settings(struct net_context *c, int argc,
                                        const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer migrate settings\n"
-                           "    Migrate printer-settings from a "
-                           "print-server\n"));
+                           "    %s\n",
+                         _("Usage:"),
+                         _("Migrate printer-settings from a "
+                           "print-server"));
                return 0;
        }
 
@@ -6753,9 +7496,11 @@ int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer list\n"
-                          "    List printers on a remote RPC server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("List printers on a remote RPC server"));
                return 0;
        }
 
@@ -6778,9 +7523,11 @@ static int rpc_printer_driver_list(struct net_context *c, int argc,
                                   const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer driver\n"
-                          "    List printer-drivers on a remote RPC server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("List printer-drivers on a remote RPC server"));
                return 0;
        }
 
@@ -6803,9 +7550,11 @@ static int rpc_printer_publish_publish(struct net_context *c, int argc,
                                       const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer publish publish\n"
-                          "     Publish printer in ADS via MSRPC\n"));
+                          "     %s\n",
+                        _("Usage:"),
+                        _("Publish printer in ADS via MSRPC"));
                return 0;
        }
 
@@ -6827,9 +7576,11 @@ static int rpc_printer_publish_publish(struct net_context *c, int argc,
 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer publish update\n"
-                          "    Update printer in ADS via MSRPC\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Update printer in ADS via MSRPC"));
                return 0;
        }
 
@@ -6852,9 +7603,11 @@ static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
                                         const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer publish unpublish\n"
-                          "    UnPublish printer in ADS via MSRPC\n"));
+                          "    %s\n",
+                        _("Usage:\n"),
+                        _("UnPublish printer in ADS via MSRPC"));
                return 0;
        }
 
@@ -6877,9 +7630,11 @@ static int rpc_printer_publish_list(struct net_context *c, int argc,
                                    const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer publish list\n"
-                          "    List published printers via MSRPC\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("List published printers via MSRPC"));
                return 0;
        }
 
@@ -7254,7 +8009,7 @@ int net_rpc(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;
        }
@@ -7263,6 +8018,9 @@ int net_rpc(struct net_context *c, int argc, const char **argv)
        if (c->opt_kerberos) {
                libnetapi_set_use_kerberos(c->netapi_ctx);
        }
+       if (c->opt_ccache) {
+               libnetapi_set_use_ccache(c->netapi_ctx);
+       }
 
        return net_run_function(c, argc, argv, "net rpc", func);
 }