s3-libsmb: rename cli_query_secdesc() to cli_query_secdesc_old()
[kai/samba.git] / source3 / utils / net_rpc.c
index 6c98e4b73884cb9a623c88524484a912a59d2c89..947ae96c8896a079b34cc8a81946517043712d2e 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/cli_lsa.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 "lib/netapi/netapi_net.h"
 #include "rpc_client/init_lsa.h"
 #include "../libcli/security/security.h"
+#include "libsmb/libsmb.h"
+#include "libsmb/clirap.h"
+#include "nsswitch/libwbclient/wbclient.h"
+#include "passdb.h"
 
 static int net_mode_share;
 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
@@ -72,30 +77,40 @@ NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 {
        struct rpc_pipe_client *lsa_pipe = NULL;
        struct policy_handle pol;
-       NTSTATUS result = NT_STATUS_OK;
+       NTSTATUS status, result;
        union lsa_PolicyInformation *info = NULL;
+       struct dcerpc_binding_handle *b;
 
-       result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
+       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
                                          &lsa_pipe);
-       if (!NT_STATUS_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
-               return result;
+               return status;
        }
 
-       result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
+       b = lsa_pipe->binding_handle;
+
+       status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
                                     SEC_FLAG_MAXIMUM_ALLOWED,
                                     &pol);
-       if (!NT_STATUS_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, "open_policy %s: %s\n",
                          _("failed"),
-                         nt_errstr(result));
-               return result;
+                         nt_errstr(status));
+               return status;
        }
 
-       result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
+       status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
                                            &pol,
                                            LSA_POLICY_INFO_ACCOUNT_DOMAIN,
-                                           &info);
+                                           &info,
+                                           &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, "lsaquery %s: %s\n",
+                         _("failed"),
+                         nt_errstr(status));
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                d_fprintf(stderr, "lsaquery %s: %s\n",
                          _("failed"),
@@ -106,7 +121,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;
@@ -336,7 +351,7 @@ static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
                        "error was %s\n",
-                       cli->desthost,
+                       cli_state_remote_name(cli),
                        nt_errstr(result) ));
                return result;
        }
@@ -351,7 +366,7 @@ static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
                sec_channel_type = get_sec_channel_type(NULL);
        }
 
-       fstrcpy(trust_passwd, global_myname());
+       fstrcpy(trust_passwd, lp_netbios_name());
        strlower_m(trust_passwd);
 
        /*
@@ -364,7 +379,7 @@ static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
        E_md4hash(trust_passwd, orig_trust_passwd_hash);
 
        result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
-                                             global_myname(),
+                                             lp_netbios_name(),
                                              orig_trust_passwd_hash,
                                              sec_channel_type);
 
@@ -466,10 +481,10 @@ int net_rpc_join(struct net_context *c, int argc, const char **argv)
                return -1;
        }
 
-       if (strlen(global_myname()) > 15) {
+       if (strlen(lp_netbios_name()) > 15) {
                d_printf(_("Our netbios name can be at most 15 chars long, "
                           "\"%s\" is %u chars long\n"),
-                        global_myname(), (unsigned int)strlen(global_myname()));
+                        lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
                return -1;
        }
 
@@ -535,7 +550,7 @@ NTSTATUS rpc_info_internals(struct net_context *c,
        status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
+                                       discard_const_p(struct dom_sid2, domain_sid),
                                        &domain_pol,
                                        &result);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1608,7 +1623,7 @@ static NTSTATUS rpc_group_delete_internals(struct net_context *c,
        status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
+                                       discard_const_p(struct dom_sid2, domain_sid),
                                        &domain_pol,
                                        &result);
         if (!NT_STATUS_IS_OK(status)) {
@@ -1929,37 +1944,40 @@ static NTSTATUS get_sid_from_name(struct cli_state *cli,
        enum lsa_SidType *types = NULL;
        struct rpc_pipe_client *pipe_hnd = NULL;
        struct policy_handle lsa_pol;
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status, result;
+       struct dcerpc_binding_handle *b;
 
-       result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
+       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
                                          &pipe_hnd);
-       if (!NT_STATUS_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(status)) {
                goto done;
        }
 
-       result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
+       b = pipe_hnd->binding_handle;
+
+       status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
                                     SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
 
-       if (!NT_STATUS_IS_OK(result)) {
+       if (!NT_STATUS_IS_OK(status)) {
                goto done;
        }
 
-       result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
+       status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
                                      &name, NULL, 1, &sids, &types);
 
-       if (NT_STATUS_IS_OK(result)) {
+       if (NT_STATUS_IS_OK(status)) {
                sid_copy(sid, &sids[0]);
                *type = types[0];
        }
 
-       rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
+       dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
 
  done:
        if (pipe_hnd) {
                TALLOC_FREE(pipe_hnd);
        }
 
-       if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
+       if (!NT_STATUS_IS_OK(status) && (strncasecmp_m(name, "S-", 2) == 0)) {
 
                /* Try as S-1-5-whatever */
 
@@ -1968,11 +1986,11 @@ static NTSTATUS get_sid_from_name(struct cli_state *cli,
                if (string_to_sid(&tmp_sid, name)) {
                        sid_copy(sid, &tmp_sid);
                        *type = SID_NAME_UNKNOWN;
-                       result = NT_STATUS_OK;
+                       status = NT_STATUS_OK;
                }
        }
 
-       return result;
+       return status;
 }
 
 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
@@ -2567,7 +2585,7 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
        status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
+                                       discard_const_p(struct dom_sid2, domain_sid),
                                        &domain_pol,
                                        &result);
        if (!NT_STATUS_IS_OK(status)) {
@@ -2693,7 +2711,7 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
        status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
+                                       discard_const_p(struct dom_sid2, &global_sid_Builtin),
                                        &domain_pol,
                                        &result);
        if (!NT_STATUS_IS_OK(status)) {
@@ -2939,7 +2957,7 @@ static NTSTATUS rpc_list_alias_members(struct net_context *c,
                return result;
        }
 
-       alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_members);
+       alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
        if (!alias_sids) {
                d_fprintf(stderr, _("Out of memory\n"));
                TALLOC_FREE(lsa_pipe);
@@ -3015,7 +3033,7 @@ static NTSTATUS rpc_group_members_internals(struct net_context *c,
        status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
+                                       discard_const_p(struct dom_sid2, domain_sid),
                                        &domain_pol,
                                        &result);
        if (!NT_STATUS_IS_OK(status)) {
@@ -3413,7 +3431,7 @@ static WERROR get_share_info(struct net_context *c,
        {
                struct srvsvc_NetShareCtr1 *ctr1;
 
-               ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
+               ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
                W_ERROR_HAVE_NO_MEMORY(ctr1);
 
                ctr1->count = 1;
@@ -3427,7 +3445,7 @@ static WERROR get_share_info(struct net_context *c,
        {
                struct srvsvc_NetShareCtr2 *ctr2;
 
-               ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
+               ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
                W_ERROR_HAVE_NO_MEMORY(ctr2);
 
                ctr2->count = 1;
@@ -3441,7 +3459,7 @@ static WERROR get_share_info(struct net_context *c,
        {
                struct srvsvc_NetShareCtr502 *ctr502;
 
-               ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
+               ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
                W_ERROR_HAVE_NO_MEMORY(ctr502);
 
                ctr502->count = 1;
@@ -3713,7 +3731,7 @@ static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
        DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
 
        /* DIRECTORY */
-       if (f->mode & aDIR) {
+       if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
 
                DEBUG(3,("got dir: %s\n", f->name));
 
@@ -3746,8 +3764,8 @@ static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
                }
 
                /* search below that directory */
-               fstrcpy(new_mask, dir);
-               fstrcat(new_mask, "\\*");
+               strlcpy(new_mask, dir, sizeof(new_mask));
+               strlcat(new_mask, "\\*", sizeof(new_mask));
 
                old_dir = local_state->cwd;
                local_state->cwd = dir;
@@ -3809,12 +3827,14 @@ static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
 
        DEBUG(3,("calling cli_list with mask: %s\n", mask));
 
-       if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
-                               mask, &targetcli, &targetpath ) ) {
+       status = cli_resolve_path(talloc_tos(), "", NULL,
+                                 cp_clistate->cli_share_src,
+                                 mask, &targetcli, &targetpath);
+       if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
                                    "%s\n"),
-                       mask, cli_errstr(cp_clistate->cli_share_src));
-               return cli_nt_error(cp_clistate->cli_share_src);
+                       mask, nt_errstr(status));
+               return status;
        }
 
        status = cli_list(targetcli, targetpath, cp_clistate->attribute,
@@ -3951,12 +3971,13 @@ static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
                cp_clistate.cli_share_src = NULL;
                cp_clistate.cli_share_dst = NULL;
                cp_clistate.cwd = NULL;
-               cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
+               cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
                cp_clistate.c = c;
 
                /* open share source */
                nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
-                                              &cli->dest_ss, cli->desthost,
+                                              cli_state_remote_sockaddr(cli),
+                                              cli_state_remote_name(cli),
                                               info502.name, "A:");
                if (!NT_STATUS_IS_OK(nt_status))
                        goto done;
@@ -4301,7 +4322,7 @@ static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
        status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
+                                       discard_const_p(struct dom_sid2, domain_sid),
                                        &domain_pol,
                                        &result);
        if (!NT_STATUS_IS_OK(status)) {
@@ -4411,6 +4432,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,
@@ -4455,7 +4477,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;
 }
@@ -4602,7 +4624,7 @@ static bool get_user_sids(const char *domain, const char *user, struct security_
        enum wbcSidType type;
        fstring full_name;
        struct wbcDomainSid wsid;
-       char *sid_str = NULL;
+       char sid_str[WBC_SID_STRING_BUFLEN];
        struct dom_sid user_sid;
        uint32_t num_groups;
        gid_t *groups = NULL;
@@ -4621,13 +4643,9 @@ static bool get_user_sids(const char *domain, const char *user, struct security_
                return false;
        }
 
-       wbc_status = wbcSidToString(&wsid, &sid_str);
-       if (!WBC_ERROR_IS_OK(wbc_status)) {
-               return false;
-       }
+       wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
 
        if (type != WBC_SID_NAME_USER) {
-               wbcFreeMemory(sid_str);
                DEBUG(1, ("%s is not a user\n", full_name));
                return false;
        }
@@ -4637,9 +4655,6 @@ static bool get_user_sids(const char *domain, const char *user, struct security_
                return false;
        }
 
-       wbcFreeMemory(sid_str);
-       sid_str = NULL;
-
        init_user_token(token, &user_sid);
 
        /* And now the groups winbind knows about */
@@ -4663,18 +4678,11 @@ static bool get_user_sids(const char *domain, const char *user, struct security_
                        return false;
                }
 
-               wbc_status = wbcSidToString(&wsid, &sid_str);
-               if (!WBC_ERROR_IS_OK(wbc_status)) {
-                       wbcFreeMemory(groups);
-                       return false;
-               }
+               wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
 
                DEBUG(3, (" %s\n", sid_str));
 
                string_to_sid(&sid, sid_str);
-               wbcFreeMemory(sid_str);
-               sid_str = NULL;
-
                add_sid_to_token(token, &sid);
        }
        wbcFreeMemory(groups);
@@ -4798,7 +4806,7 @@ static bool get_user_tokens_from_file(FILE *f,
 
                token = &((*tokens)[*num_tokens-1]);
 
-               fstrcpy(token->name, line);
+               strlcpy(token->name, line, sizeof(token->name));
                token->token.num_sids = 0;
                token->token.sids = NULL;
                continue;
@@ -4848,7 +4856,7 @@ static void show_userlist(struct rpc_pipe_client *pipe_hnd,
                          netname));
        }
 
-       cnum = cli->cnum;
+       cnum = cli_state_get_tid(cli);
 
        if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
                return;
@@ -4856,7 +4864,7 @@ static void show_userlist(struct rpc_pipe_client *pipe_hnd,
 
        if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
                        FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
-               root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
+               root_sd = cli_query_secdesc_old(cli, fnum, mem_ctx);
        }
 
        for (i=0; i<num_tokens; i++) {
@@ -4892,7 +4900,7 @@ static void show_userlist(struct rpc_pipe_client *pipe_hnd,
        if (fnum != (uint16_t)-1)
                cli_close(cli, fnum);
        cli_tdis(cli);
-       cli->cnum = cnum;
+       cli_state_set_tid(cli, cnum);
        
        return;
 }
@@ -5750,7 +5758,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;
@@ -5759,6 +5767,7 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
        uint32_t access_granted = 0;
        union samr_UserInfo info;
        unsigned int orig_timeout;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 2) {
                d_printf("%s\n%s",
@@ -5781,21 +5790,31 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
        init_lsa_String(&lsa_acct_name, acct_name);
 
        /* Get samr policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
-                                       &domain_pol);
+                                       discard_const_p(struct dom_sid2, domain_sid),
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
@@ -5812,19 +5831,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;
@@ -5844,12 +5867,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;
@@ -5858,7 +5886,7 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
 
  done:
        SAFE_FREE(acct_name);
-       return result;
+       return status;
 }
 
 /**
@@ -5911,11 +5939,12 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
                                        const char **argv)
 {
        struct policy_handle connect_pol, domain_pol, user_pol;
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status, result;
        char *acct_name;
        struct dom_sid trust_acct_sid;
        struct samr_Ids user_rids, name_types;
        struct lsa_String lsa_acct_name;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 1) {
                d_printf("%s\n%s",
@@ -5935,47 +5964,72 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
        strupper_m(acct_name);
 
        /* Get samr policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
-                                       CONST_DISCARD(struct dom_sid2 *, domain_sid),
-                                       &domain_pol);
+                                       discard_const_p(struct dom_sid2, domain_sid),
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
        init_lsa_String(&lsa_acct_name, acct_name);
 
-       result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_LookupNames(b, mem_ctx,
                                         &domain_pol,
                                         1,
                                         &lsa_acct_name,
                                         &user_rids,
-                                        &name_types);
-
+                                        &name_types,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf(_("net rpc trustdom del: LookupNames on user %s "
+                          "failed %s\n"),
+                       acct_name, nt_errstr(status));
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_printf(_("net rpc trustdom del: LookupNames on user %s "
                           "failed %s\n"),
                        acct_name, nt_errstr(result) );
                goto done;
        }
 
-       result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenUser(b, mem_ctx,
                                      &domain_pol,
                                      MAXIMUM_ALLOWED_ACCESS,
                                      user_rids.ids[0],
-                                     &user_pol);
+                                     &user_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
+                          "%s\n"),
+                       acct_name, nt_errstr(status) );
+               goto done;
+       }
 
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
                           "%s\n"),
                        acct_name, nt_errstr(result) );
@@ -5989,22 +6043,39 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
 
        /* remove the sid */
 
-       result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
                                                           &user_pol,
-                                                          &trust_acct_sid);
+                                                          &trust_acct_sid,
+                                                          &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
+                          " on user %s failed %s\n"),
+                       acct_name, nt_errstr(status));
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
                           " on user %s failed %s\n"),
                        acct_name, nt_errstr(result) );
                goto done;
        }
 
+
        /* Delete user */
 
-       result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
-                                       &user_pol);
+       status = dcerpc_samr_DeleteUser(b, mem_ctx,
+                                       &user_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
+                          "%s\n"),
+                       acct_name, nt_errstr(status));
+               goto done;
+       }
 
        if (!NT_STATUS_IS_OK(result)) {
+               result = status;
                d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
                           "%s\n"),
                        acct_name, nt_errstr(result) );
@@ -6018,7 +6089,7 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
        }
 
  done:
-       return result;
+       return status;
 }
 
 /**
@@ -6076,7 +6147,7 @@ static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
        b = netr->binding_handle;
 
        status = dcerpc_netr_GetDcName(b, mem_ctx,
-                                      cli->desthost,
+                                      netr->desthost,
                                       domain_name,
                                       &buffer,
                                       &result);
@@ -6115,13 +6186,14 @@ static int rpc_trustdom_establish(struct net_context *c, int argc,
        struct rpc_pipe_client *pipe_hnd = NULL;
        struct policy_handle connect_hnd;
        TALLOC_CTX *mem_ctx;
-       NTSTATUS nt_status;
+       NTSTATUS nt_status, result;
        struct dom_sid *domain_sid;
 
        char* domain_name;
        char* acct_name;
        fstring pdc_name;
        union lsa_PolicyInformation *info = NULL;
+       struct dcerpc_binding_handle *b;
 
        /*
         * Connect to \\server\ipc$ as 'our domain' account with password
@@ -6215,6 +6287,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)) {
@@ -6227,10 +6301,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)));
@@ -6238,6 +6313,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;
 
@@ -6259,7 +6341,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)));
@@ -6351,23 +6433,31 @@ static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
                                      struct dom_sid dom_sid,
                                      const char *trusted_dom_name)
 {
-       NTSTATUS nt_status;
+       NTSTATUS nt_status, result;
        union lsa_TrustedDomainInfo *info = NULL;
        char *cleartextpwd = NULL;
        uint8_t session_key[16];
        DATA_BLOB session_key_blob;
        DATA_BLOB 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);
@@ -6412,9 +6502,8 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
        TALLOC_CTX* mem_ctx;
        struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_hnd = NULL;
-       NTSTATUS nt_status;
+       NTSTATUS nt_status, result;
        const char *domain_name = NULL;
-       struct dom_sid *queried_dom_sid;
        struct policy_handle connect_hnd;
        union lsa_PolicyInformation *info = NULL;
 
@@ -6423,6 +6512,7 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
        int i;
        struct lsa_DomainList dom_list;
        fstring pdc_name;
+       struct dcerpc_binding_handle *b;
 
        if (c->display_usage) {
                d_printf(  "%s\n"
@@ -6444,11 +6534,11 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
         * or to remote one given in command line
         */
 
-       if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
+       if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
                domain_name = c->opt_workgroup;
                c->opt_target_workgroup = c->opt_workgroup;
        } else {
-               fstrcpy(pdc_name, global_myname());
+               fstrcpy(pdc_name, lp_netbios_name());
                domain_name = talloc_strdup(mem_ctx, lp_workgroup());
                c->opt_target_workgroup = domain_name;
        };
@@ -6472,6 +6562,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)) {
@@ -6483,10 +6575,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",
@@ -6495,8 +6588,13 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
                talloc_destroy(mem_ctx);
                return -1;
        }
-
-       queried_dom_sid = info->account_domain.sid;
+       if (NT_STATUS_IS_ERR(result)) {
+               DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
+                       nt_errstr(result)));
+               cli_shutdown(cli);
+               talloc_destroy(mem_ctx);
+               return -1;
+       }
 
        /*
         * Keep calling LsaEnumTrustdom over opened pipe until
@@ -6506,11 +6604,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)));
@@ -6518,6 +6617,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++) {
 
@@ -6543,7 +6651,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)));
@@ -6565,12 +6673,13 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        TALLOC_CTX* mem_ctx;
        struct cli_state *cli = NULL, *remote_cli = NULL;
        struct rpc_pipe_client *pipe_hnd = NULL;
-       NTSTATUS nt_status;
+       NTSTATUS nt_status, result;
        const char *domain_name = NULL;
        struct dom_sid *queried_dom_sid;
        int ascii_dom_name_len;
        struct policy_handle connect_hnd;
        union lsa_PolicyInformation *info = NULL;
+       struct dcerpc_binding_handle *b = NULL;
 
        /* trusted domains listing variables */
        unsigned int num_domains, enum_ctx = 0;
@@ -6603,11 +6712,11 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
         * or to remote one given in command line
         */
 
-       if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
+       if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
                domain_name = c->opt_workgroup;
                c->opt_target_workgroup = c->opt_workgroup;
        } else {
-               fstrcpy(pdc_name, global_myname());
+               fstrcpy(pdc_name, lp_netbios_name());
                domain_name = talloc_strdup(mem_ctx, lp_workgroup());
                c->opt_target_workgroup = domain_name;
        };
@@ -6631,6 +6740,8 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                return -1;
        };
 
+       b = pipe_hnd->binding_handle;
+
        nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
                                        &connect_hnd);
        if (NT_STATUS_IS_ERR(nt_status)) {
@@ -6642,10 +6753,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",
@@ -6654,6 +6766,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;
 
@@ -6667,11 +6786,12 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        found_domain = false;
 
        do {
-               nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
+               nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
                                                    &connect_hnd,
                                                    &enum_ctx,
                                                    &dom_list,
-                                                   (uint32_t)-1);
+                                                   (uint32_t)-1,
+                                                   &result);
                if (NT_STATUS_IS_ERR(nt_status)) {
                        DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
                                nt_errstr(nt_status)));
@@ -6679,6 +6799,14 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                        talloc_destroy(mem_ctx);
                        return -1;
                };
+               if (NT_STATUS_IS_ERR(result)) {
+                       DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
+                               nt_errstr(result)));
+                       cli_shutdown(cli);
+                       talloc_destroy(mem_ctx);
+                       return -1;
+               };
+
 
                for (i = 0; i < dom_list.count; i++) {
                        print_trusted_domain(dom_list.domains[i].sid,
@@ -6698,7 +6826,7 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        }
 
        /* close this connection before doing next one */
-       nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
+       nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
        if (NT_STATUS_IS_ERR(nt_status)) {
                DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
                        nt_errstr(nt_status)));
@@ -6727,11 +6855,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)));
@@ -6739,14 +6870,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)));
@@ -6754,6 +6894,14 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                talloc_destroy(mem_ctx);
                return -1;
        };
+       if (!NT_STATUS_IS_OK(result)) {
+               nt_status = result;
+               DEBUG(0, ("Couldn't open domain object. Error was %s\n",
+                       nt_errstr(result)));
+               cli_shutdown(cli);
+               talloc_destroy(mem_ctx);
+               return -1;
+       };
 
        /*
         * perform actual enumeration
@@ -6764,13 +6912,14 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        enum_ctx = 0;   /* reset enumeration context from last enumeration */
        do {
 
-               nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
+               nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
                                                        &domain_hnd,
                                                        &enum_ctx,
                                                        ACB_DOMTRUST,
                                                        &trusts,
                                                        0xffff,
-                                                       &num_domains);
+                                                       &num_domains,
+                                                       &result);
                if (NT_STATUS_IS_ERR(nt_status)) {
                        DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
                                nt_errstr(nt_status)));
@@ -6778,10 +6927,18 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                        talloc_destroy(mem_ctx);
                        return -1;
                };
+               if (NT_STATUS_IS_ERR(result)) {
+                       nt_status = result;
+                       DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
+                               nt_errstr(result)));
+                       cli_shutdown(cli);
+                       talloc_destroy(mem_ctx);
+                       return -1;
+               };
 
                for (i = 0; i < num_domains; i++) {
 
-                       char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
+                       char *str = discard_const_p(char, trusts->entries[i].name.string);
 
                        found_domain = true;
 
@@ -6826,19 +6983,19 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                        }
                }
 
-       } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
+       } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
 
        if (!found_domain) {
                d_printf("none\n");
        }
 
        /* close opened samr and domain policy handles */
-       nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
+       nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
        };
 
-       nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
+       nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
        };
@@ -6934,16 +7091,11 @@ bool net_rpc_check(struct net_context *c, unsigned flags)
        if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
                return false;
 
-       if ((cli = cli_initialise()) == NULL) {
+       status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
+                               lp_netbios_name(), Undefined, &cli);
+       if (!NT_STATUS_IS_OK(status)) {
                return false;
        }
-
-       status = cli_connect(cli, server_name, &server_ss);
-       if (!NT_STATUS_IS_OK(status))
-               goto done;
-       if (!attempt_netbios_session_request(&cli, global_myname(),
-                                            server_name, &server_ss))
-               goto done;
        status = cli_negprot(cli);
        if (!NT_STATUS_IS_OK(status))
                goto done;
@@ -7016,10 +7168,7 @@ static int rpc_vampire(struct net_context *c, int argc, const char **argv)
                        return 0;
                }
 
-               return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
-                                      NET_FLAGS_ANONYMOUS,
-                                      rpc_vampire_internals,
-                                      argc, argv);
+               return rpc_vampire_passdb(c, argc, argv);
        }
 
        return net_run_function(c, argc, argv, "net rpc vampire", func);
@@ -7844,6 +7993,14 @@ int net_rpc(struct net_context *c, int argc, const char **argv)
                        N_("net rpc shell\n"
                           "    Open interactive shell on remote server")
                },
+               {
+                       "trust",
+                       net_rpc_trust,
+                       NET_TRANSPORT_RPC,
+                       N_("Manage trusts"),
+                       N_("net rpc trust\n"
+                          "    Manage trusts")
+               },
                {NULL, NULL, 0, NULL, NULL}
        };