s3-rpc_client: Move client pipe functions to own header.
[samba.git] / source3 / utils / net_rpc.c
index 77eb4e722072b7c6b4353bdb02434c66b7efaa55..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/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"
@@ -72,30 +73,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 +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;
@@ -1288,10 +1299,11 @@ static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
                                               struct policy_handle *user_hnd,
                                               int argc, const char **argv)
 {
-       NTSTATUS result;
+       NTSTATUS status, result;
        const char *username;
        const char *oldval = "";
        union samr_UserInfo *info = NULL;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc > 1) {
                d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
@@ -1299,10 +1311,14 @@ static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
                                           user_hnd,
                                           21,
-                                          &info);
+                                          &info,
+                                          &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
@@ -1334,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) \
@@ -1373,12 +1395,13 @@ static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
                                                struct policy_handle *user_hnd,
                                                int argc, const char **argv)
 {
-       NTSTATUS result;
+       NTSTATUS status, result;
        const char *username;
        const char *oldval = "unknown";
        uint32 oldflags, newflags;
        bool newval;
        union samr_UserInfo *info = NULL;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if ((argc > 1) ||
            ((argc == 1) && !strequal(argv[0], "yes") &&
@@ -1392,10 +1415,14 @@ static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
 
        newval = strequal(argv[0], "yes");
 
-       result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
                                           user_hnd,
                                           21,
-                                          &info);
+                                          &info,
+                                          &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
@@ -1420,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]);
@@ -1432,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,
@@ -1552,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_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;
@@ -1568,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) {
@@ -1637,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) {
@@ -1672,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;
                }
 
@@ -1689,10 +1772,14 @@ static NTSTATUS rpc_group_delete_internals(struct net_context *c,
                        if (c->opt_verbose)
                                d_printf(_("Remove group member %d..."),
                                        rids->rids[i]);
-                       result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
+                       status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
                                                               &group_pol,
-                                                              rids->rids[i]);
-
+                                                              rids->rids[i],
+                                                              &result);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               goto done;
+                       }
+                       status = result;
                        if (NT_STATUS_IS_OK(result)) {
                                if (c->opt_verbose)
                                        d_printf(_("ok\n"));
@@ -1703,45 +1790,63 @@ static NTSTATUS rpc_group_delete_internals(struct net_context *c,
                        }
                }
 
-               result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
-                                                      &group_pol);
+               status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
+                                                      &group_pol,
+                                                      &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       break;
+               }
+
+               status = result;
 
                break;
        /* removing a local group is easier... */
        case SID_NAME_ALIAS:
-               result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_OpenAlias(b, mem_ctx,
                                               &domain_pol,
                                               MAXIMUM_ALLOWED_ACCESS,
                                               group_rids.ids[0],
-                                              &group_pol);
-
+                                              &group_pol,
+                                              &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_fprintf(stderr, _("Request open_alias failed\n"));
+                       goto done;
+               }
                if (!NT_STATUS_IS_OK(result)) {
+                       status = result;
                        d_fprintf(stderr, _("Request open_alias failed\n"));
-                       goto done;
+                       goto done;
                }
 
-               result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
-                                                   &group_pol);
+               status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
+                                                   &group_pol,
+                                                   &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       break;
+               }
+
+               status = result;
+
                break;
        default:
                d_fprintf(stderr, _("%s is of type %s. This command is only "
                                    "for deleting local or global groups\n"),
                        argv[0],sid_type_lookup(name_types.ids[0]));
-               result = NT_STATUS_UNSUCCESSFUL;
+               status = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
 
-       if (NT_STATUS_IS_OK(result)) {
+       if (NT_STATUS_IS_OK(status)) {
                if (c->opt_verbose)
                        d_printf(_("Deleted %s '%s'\n"),
                                 sid_type_lookup(name_types.ids[0]), argv[0]);
        } else {
                d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
-                       get_friendly_nt_error_msg(result));
+                       get_friendly_nt_error_msg(status));
        }
 
  done:
-       return result;
+       return status;
 
 }
 
@@ -1835,37 +1940,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(name, "S-", 2) == 0)) {
 
                /* Try as S-1-5-whatever */
 
@@ -1874,11 +1982,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,
@@ -1887,9 +1995,10 @@ static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
                                const char *member)
 {
        struct policy_handle connect_pol, domain_pol;
-       NTSTATUS result;
+       NTSTATUS status, result;
        uint32 group_rid;
        struct policy_handle group_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        struct samr_Ids rids, rid_types;
        struct lsa_String lsa_acct_name;
@@ -1903,57 +2012,83 @@ static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
        }
 
        /* Get sam policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
                                        &sid,
-                                       &domain_pol);
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
 
        init_lsa_String(&lsa_acct_name, member);
 
-       result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_LookupNames(b, mem_ctx,
                                         &domain_pol,
                                         1,
                                         &lsa_acct_name,
                                         &rids,
-                                        &rid_types);
+                                        &rid_types,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Could not lookup up group member %s\n"),
+                         member);
+               goto done;
+       }
 
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_fprintf(stderr, _("Could not lookup up group member %s\n"),
                          member);
                goto done;
        }
 
-       result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenGroup(b, mem_ctx,
                                       &domain_pol,
                                       MAXIMUM_ALLOWED_ACCESS,
                                       group_rid,
-                                      &group_pol);
+                                      &group_pol,
+                                      &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
 
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
-       result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_AddGroupMember(b, mem_ctx,
                                            &group_pol,
                                            rids.ids[0],
-                                           0x0005); /* unknown flags */
+                                           0x0005, /* unknown flags */
+                                           &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
+
+       status = result;
 
  done:
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
-       return result;
+       dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
+       return status;
 }
 
 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
@@ -1962,9 +2097,10 @@ static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
                                const char *member)
 {
        struct policy_handle connect_pol, domain_pol;
-       NTSTATUS result;
+       NTSTATUS status, result;
        uint32 alias_rid;
        struct policy_handle alias_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        struct dom_sid member_sid;
        enum lsa_SidType member_type;
@@ -1987,45 +2123,60 @@ static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
        }
 
        /* Get sam policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
                                        &sid,
-                                       &domain_pol);
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
-       result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenAlias(b, mem_ctx,
                                       &domain_pol,
                                       MAXIMUM_ALLOWED_ACCESS,
                                       alias_rid,
-                                      &alias_pol);
-
+                                      &alias_pol,
+                                      &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
 
-       result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_AddAliasMember(b, mem_ctx,
                                            &alias_pol,
-                                           &member_sid);
-
-       if (!NT_STATUS_IS_OK(result)) {
-               return result;
+                                           &member_sid,
+                                           &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
+       status = result;
+
  done:
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
-       return result;
+       dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
+       return status;
 }
 
 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
@@ -2099,9 +2250,10 @@ static NTSTATUS rpc_del_groupmem(struct net_context *c,
                                const char *member)
 {
        struct policy_handle connect_pol, domain_pol;
-       NTSTATUS result;
+       NTSTATUS status, result;
        uint32 group_rid;
        struct policy_handle group_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        struct samr_Ids rids, rid_types;
        struct lsa_String lsa_acct_name;
@@ -2114,52 +2266,81 @@ static NTSTATUS rpc_del_groupmem(struct net_context *c,
                return NT_STATUS_UNSUCCESSFUL;
 
        /* Get sam policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
-       if (!NT_STATUS_IS_OK(result))
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
+
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
                                        &sid,
-                                       &domain_pol);
-       if (!NT_STATUS_IS_OK(result))
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
 
        init_lsa_String(&lsa_acct_name, member);
 
-       result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_LookupNames(b, mem_ctx,
                                         &domain_pol,
                                         1,
                                         &lsa_acct_name,
                                         &rids,
-                                        &rid_types);
+                                        &rid_types,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Could not lookup up group member %s\n"),
+                         member);
+               goto done;
+       }
+
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                d_fprintf(stderr, _("Could not lookup up group member %s\n"),
                          member);
                goto done;
        }
 
-       result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenGroup(b, mem_ctx,
                                       &domain_pol,
                                       MAXIMUM_ALLOWED_ACCESS,
                                       group_rid,
-                                      &group_pol);
-
-       if (!NT_STATUS_IS_OK(result))
+                                      &group_pol,
+                                      &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
+       }
 
-       result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
                                               &group_pol,
-                                              rids.ids[0]);
+                                              rids.ids[0],
+                                              &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
 
+       status = result;
  done:
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
-       return result;
+       dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
+       return status;
 }
 
 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
@@ -2168,9 +2349,10 @@ static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
                                const char *member)
 {
        struct policy_handle connect_pol, domain_pol;
-       NTSTATUS result;
+       NTSTATUS status, result;
        uint32 alias_rid;
        struct policy_handle alias_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        struct dom_sid member_sid;
        enum lsa_SidType member_type;
@@ -2192,43 +2374,62 @@ static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
        }
 
        /* Get sam policy handle */
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
        /* Get domain policy handle */
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
                                        &sid,
-                                       &domain_pol);
+                                       &domain_pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
        if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
        }
 
-       result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenAlias(b, mem_ctx,
                                       &domain_pol,
                                       MAXIMUM_ALLOWED_ACCESS,
                                       alias_rid,
-                                      &alias_pol);
+                                      &alias_pol,
+                                      &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
-       if (!NT_STATUS_IS_OK(result))
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
 
-       result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
                                               &alias_pol,
-                                              &member_sid);
+                                              &member_sid,
+                                              &result);
 
-       if (!NT_STATUS_IS_OK(result))
-               return result;
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       status = result;
 
  done:
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
-       return result;
+       dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
+       return status;
 }
 
 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
@@ -2321,12 +2522,13 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
                                        const char **argv)
 {
        struct policy_handle connect_pol, domain_pol;
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status, result;
        uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
        struct samr_SamArray *groups = NULL;
        bool global = false;
        bool local = false;
        bool builtin = false;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (c->display_usage) {
                d_printf("%s\n%s",
@@ -2361,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;
        }
 
@@ -2393,7 +2605,7 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
                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,
@@ -2401,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;
 
@@ -2429,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;
@@ -2447,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;
+                                               }
+                                       }
                                }
                        }
 
@@ -2472,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++) {
 
@@ -2506,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;
+                                               }
+                                       }
                                }
                        }
 
@@ -2532,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)
@@ -2551,32 +2803,41 @@ static NTSTATUS rpc_list_group_members(struct net_context *c,
                                        struct policy_handle *domain_pol,
                                        uint32 rid)
 {
-       NTSTATUS result;
+       NTSTATUS result, status;
        struct policy_handle group_pol;
        uint32 num_members, *group_rids;
        int i;
        struct samr_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;
@@ -2587,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 */
@@ -2626,7 +2891,7 @@ static NTSTATUS rpc_list_alias_members(struct net_context *c,
                                        struct policy_handle *domain_pol,
                                        uint32 rid)
 {
-       NTSTATUS result;
+       NTSTATUS result, status;
        struct rpc_pipe_client *lsa_pipe;
        struct policy_handle alias_pol, lsa_pol;
        uint32 num_members;
@@ -2636,20 +2901,29 @@ static NTSTATUS rpc_list_alias_members(struct net_context *c,
        enum lsa_SidType *types;
        int i;
        struct lsa_SidArray sid_array;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
-       result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenAlias(b, mem_ctx,
                                       domain_pol,
                                       MAXIMUM_ALLOWED_ACCESS,
                                       rid,
-                                      &alias_pol);
-
-       if (!NT_STATUS_IS_OK(result))
+                                      &alias_pol,
+                                      &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
 
-       result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
                                               &alias_pol,
-                                              &sid_array);
-
+                                              &sid_array,
+                                              &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Couldn't list alias members\n"));
+               return status;
+       }
        if (!NT_STATUS_IS_OK(result)) {
                d_fprintf(stderr, _("Couldn't list alias members\n"));
                return result;
@@ -2730,40 +3004,53 @@ static NTSTATUS rpc_group_members_internals(struct net_context *c,
                                        int argc,
                                        const char **argv)
 {
-       NTSTATUS result;
+       NTSTATUS result, status;
        struct policy_handle connect_pol, domain_pol;
        struct samr_Ids rids, rid_types;
        struct lsa_String lsa_acct_name;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        /* Get sam policy handle */
 
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
-
-       if (!NT_STATUS_IS_OK(result))
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                return result;
+       }
 
        /* Get domain policy handle */
 
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        &connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
                                        CONST_DISCARD(struct dom_sid2 *, domain_sid),
-                                       &domain_pol);
-
-       if (!NT_STATUS_IS_OK(result))
+                                       &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)) {
 
@@ -2771,29 +3058,35 @@ static NTSTATUS rpc_group_members_internals(struct net_context *c,
 
                struct dom_sid sid_Builtin;
 
-               rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
+               dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
 
                sid_copy(&sid_Builtin, &global_sid_Builtin);
 
-               result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                                &connect_pol,
                                                MAXIMUM_ALLOWED_ACCESS,
                                                &sid_Builtin,
-                                               &domain_pol);
-
+                                               &domain_pol,
+                                               &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
                if (!NT_STATUS_IS_OK(result)) {
                        d_fprintf(stderr, _("Couldn't find group %s\n"),
                                  argv[0]);
                        return result;
                }
 
-               result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
+               status = dcerpc_samr_LookupNames(b, mem_ctx,
                                                 &domain_pol,
                                                 1,
                                                 &lsa_acct_name,
                                                 &rids,
-                                                &rid_types);
-
+                                                &rid_types,
+                                                &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
                if (!NT_STATUS_IS_OK(result)) {
                        d_fprintf(stderr, _("Couldn't find group %s\n"),
                                  argv[0]);
@@ -4013,55 +4306,83 @@ static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
 {
        uint32 start_idx, max_entries, num_entries, i;
        struct samr_SamArray *groups = NULL;
-       NTSTATUS result;
+       NTSTATUS result, status;
        struct policy_handle domain_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        /* Get domain policy handle */
 
-       result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
                                        connect_pol,
                                        MAXIMUM_ALLOWED_ACCESS,
                                        CONST_DISCARD(struct dom_sid2 *, domain_sid),
-                                       &domain_pol);
-       if (!NT_STATUS_IS_OK(result))
+                                       &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;
 
@@ -4080,12 +4401,12 @@ static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
                }
        } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
 
-       result = NT_STATUS_OK;
+       status = NT_STATUS_OK;
 
  done:
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
+       dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
 
-       return result;
+       return status;
 }
 
 /*
@@ -4104,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,
@@ -4148,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;
 }
@@ -4167,29 +4489,35 @@ static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
                                        int argc,
                                        const char **argv)
 {
-       NTSTATUS result;
+       NTSTATUS result, status;
        struct policy_handle connect_pol;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
-       result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
+       status = dcerpc_samr_Connect2(b, mem_ctx,
                                      pipe_hnd->desthost,
                                      MAXIMUM_ALLOWED_ACCESS,
-                                     &connect_pol);
-
-       if (!NT_STATUS_IS_OK(result))
+                                     &connect_pol,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               status = result;
                goto done;
+       }
 
-       result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
+       status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
                                          &global_sid_Builtin);
-
-       if (!NT_STATUS_IS_OK(result))
+       if (!NT_STATUS_IS_OK(status)) {
                goto done;
+       }
 
-       result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
+       status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
                                          domain_sid);
 
-       rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
+       dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
  done:
-       return result;
+       return status;
 }
 
 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
@@ -5437,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;
@@ -5446,6 +5774,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",
@@ -5468,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;
        }
 
@@ -5499,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;
@@ -5531,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;
@@ -5545,7 +5893,7 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
 
  done:
        SAFE_FREE(acct_name);
-       return result;
+       return status;
 }
 
 /**
@@ -5598,11 +5946,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",
@@ -5622,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) );
@@ -5676,22 +6050,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) );
@@ -5705,7 +6096,7 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
        }
 
  done:
-       return result;
+       return status;
 }
 
 /**
@@ -5802,13 +6193,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
@@ -5902,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)) {
@@ -5914,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)));
@@ -5925,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;
 
@@ -5946,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)));
@@ -6038,23 +6440,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);
@@ -6099,7 +6509,7 @@ 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;
@@ -6110,6 +6520,7 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
        int i;
        struct lsa_DomainList dom_list;
        fstring pdc_name;
+       struct dcerpc_binding_handle *b;
 
        if (c->display_usage) {
                d_printf(  "%s\n"
@@ -6159,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)) {
@@ -6170,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",
@@ -6182,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;
 
@@ -6193,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)));
@@ -6205,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++) {
 
@@ -6230,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)));
@@ -6252,12 +6683,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;
@@ -6329,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",
@@ -6341,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;
 
@@ -6354,11 +6794,12 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        found_domain = false;
 
        do {
-               nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
+               nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
                                                    &connect_hnd,
                                                    &enum_ctx,
                                                    &dom_list,
-                                                   (uint32_t)-1);
+                                                   (uint32_t)-1,
+                                                   &result);
                if (NT_STATUS_IS_ERR(nt_status)) {
                        DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
                                nt_errstr(nt_status)));
@@ -6366,6 +6807,14 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                        talloc_destroy(mem_ctx);
                        return -1;
                };
+               if (NT_STATUS_IS_ERR(result)) {
+                       DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
+                               nt_errstr(result)));
+                       cli_shutdown(cli);
+                       talloc_destroy(mem_ctx);
+                       return -1;
+               };
+
 
                for (i = 0; i < dom_list.count; i++) {
                        print_trusted_domain(dom_list.domains[i].sid,
@@ -6385,7 +6834,7 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        }
 
        /* close this connection before doing next one */
-       nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
+       nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
        if (NT_STATUS_IS_ERR(nt_status)) {
                DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
                        nt_errstr(nt_status)));
@@ -6414,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)));
@@ -6426,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)));
@@ -6441,6 +6902,14 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                talloc_destroy(mem_ctx);
                return -1;
        };
+       if (!NT_STATUS_IS_OK(result)) {
+               nt_status = result;
+               DEBUG(0, ("Couldn't open domain object. Error was %s\n",
+                       nt_errstr(result)));
+               cli_shutdown(cli);
+               talloc_destroy(mem_ctx);
+               return -1;
+       };
 
        /*
         * perform actual enumeration
@@ -6451,13 +6920,14 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        enum_ctx = 0;   /* reset enumeration context from last enumeration */
        do {
 
-               nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
+               nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
                                                        &domain_hnd,
                                                        &enum_ctx,
                                                        ACB_DOMTRUST,
                                                        &trusts,
                                                        0xffff,
-                                                       &num_domains);
+                                                       &num_domains,
+                                                       &result);
                if (NT_STATUS_IS_ERR(nt_status)) {
                        DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
                                nt_errstr(nt_status)));
@@ -6465,6 +6935,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)) {
+                       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++) {
 
@@ -6513,19 +6991,19 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                        }
                }
 
-       } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
+       } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
 
        if (!found_domain) {
                d_printf("none\n");
        }
 
        /* close opened samr and domain policy handles */
-       nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
+       nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
        };
 
-       nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
+       nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
        };
@@ -6703,10 +7181,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);