Merge branch 'v3-2-test' of git://git.samba.org/samba into v3-2-test
[samba.git] / source3 / rpc_client / cli_lsarpc.c
index bf2c0353c04e57c6efd3f5f7b92e819aa38067d9..77ade5cba1b3f19aa02b22bd39818a9362082403 100644 (file)
@@ -1,26 +1,24 @@
-/* 
+/*
    Unix SMB/CIFS implementation.
    RPC pipe client
    Copyright (C) Tim Potter                        2000-2001,
    Copyright (C) Andrew Tridgell              1992-1997,2000,
-   Copyright (C) Luke Kenneth Casson Leighton 1996-1997,2000,
-   Copyright (C) Paul Ashton                       1997,2000,
-   Copyright (C) Elrond                                 2000,
    Copyright (C) Rafal Szczesniak                       2002
-   
+   Copyright (C) Jeremy Allison                                2005.
+   Copyright (C) Michael Adam                          2007.
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
  *
  * @param cli Handle on an initialised SMB connection */
 
-NTSTATUS cli_lsa_open_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                             BOOL sec_qos, uint32 des_access, POLICY_HND *pol)
+NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
+                               TALLOC_CTX *mem_ctx,
+                               bool sec_qos, uint32 des_access,
+                               POLICY_HND *pol)
 {
        prs_struct qbuf, rbuf;
        LSA_Q_OPEN_POL q;
@@ -54,11 +54,6 @@ NTSTATUS cli_lsa_open_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
        /* Initialise input parameters */
 
        if (sec_qos) {
@@ -70,233 +65,142 @@ NTSTATUS cli_lsa_open_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        /* Marshall data and send request */
 
-       if (!lsa_io_q_open_pol("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_OPENPOLICY, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Unmarshall response */
-
-       if (!lsa_io_r_open_pol("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENPOLICY,
+                       q, r,
+                       qbuf, rbuf,
+                       lsa_io_q_open_pol,
+                       lsa_io_r_open_pol,
+                       NT_STATUS_UNSUCCESSFUL );
 
        /* Return output parameters */
 
-       if (NT_STATUS_IS_OK(result = r.status)) {
+       result = r.status;
+
+       if (NT_STATUS_IS_OK(result)) {
                *pol = r.pol;
-#ifdef __INSURE__
-               pol->marker = MALLOC(1);
-#endif
        }
 
- done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
-
        return result;
 }
 
 /** Open a LSA policy handle
   *
-  * @param cli Handle on an initialised SMB connection 
+  * @param cli Handle on an initialised SMB connection
   */
 
-NTSTATUS cli_lsa_open_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                              BOOL sec_qos, uint32 des_access, POLICY_HND *pol)
+NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
+                                TALLOC_CTX *mem_ctx, bool sec_qos,
+                                uint32 des_access, POLICY_HND *pol)
 {
        prs_struct qbuf, rbuf;
        LSA_Q_OPEN_POL2 q;
        LSA_R_OPEN_POL2 r;
        LSA_SEC_QOS qos;
        NTSTATUS result;
+       char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
 
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
-       /* Initialise input parameters */
-
        if (sec_qos) {
                init_lsa_sec_qos(&qos, 2, 1, 0);
-               init_q_open_pol2(&q, cli->srv_name_slash, 0, des_access, 
-                                 &qos);
+               init_q_open_pol2(&q, srv_name_slash, 0, des_access, &qos);
        } else {
-               init_q_open_pol2(&q, cli->srv_name_slash, 0, des_access, 
-                                 NULL);
+               init_q_open_pol2(&q, srv_name_slash, 0, des_access, NULL);
        }
 
-       /* Marshall data and send request */
-
-       if (!lsa_io_q_open_pol2("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_OPENPOLICY2, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Unmarshall response */
-
-       if (!lsa_io_r_open_pol2("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENPOLICY2,
+                       q, r,
+                       qbuf, rbuf,
+                       lsa_io_q_open_pol2,
+                       lsa_io_r_open_pol2,
+                       NT_STATUS_UNSUCCESSFUL );
 
        /* Return output parameters */
 
-       if (NT_STATUS_IS_OK(result = r.status)) {
-               *pol = r.pol;
-#ifdef __INSURE__
-               pol->marker = (char *)malloc(1);
-#endif
-       }
-
- done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
-
-       return result;
-}
-
-/** Close a LSA policy handle */
-
-NTSTATUS cli_lsa_close(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
-                       POLICY_HND *pol)
-{
-       prs_struct qbuf, rbuf;
-       LSA_Q_CLOSE q;
-       LSA_R_CLOSE r;
-       NTSTATUS result;
-
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
-
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
-       /* Marshall data and send request */
-
-       init_lsa_q_close(&q, pol);
-
-       if (!lsa_io_q_close("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_CLOSE, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Unmarshall response */
-
-       if (!lsa_io_r_close("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Return output parameters */
+       result = r.status;
 
-       if (NT_STATUS_IS_OK(result = r.status)) {
-#ifdef __INSURE__
-               SAFE_FREE(pol->marker);
-#endif
+       if (NT_STATUS_IS_OK(result)) {
                *pol = r.pol;
        }
 
- done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
-
        return result;
 }
 
-/** Lookup a list of sids */
-
-NTSTATUS cli_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                             POLICY_HND *pol, int num_sids, const DOM_SID *sids, 
-                             char ***domains, char ***names, uint32 **types)
+/* Lookup a list of sids
+ *
+ * internal version withOUT memory allocation of the target arrays.
+ * this assumes suffciently sized arrays to store domains, names and types. */
+
+static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
+                                              TALLOC_CTX *mem_ctx,
+                                              POLICY_HND *pol,
+                                              int num_sids,
+                                              const DOM_SID *sids,
+                                              char **domains,
+                                              char **names,
+                                              enum lsa_SidType *types)
 {
        prs_struct qbuf, rbuf;
        LSA_Q_LOOKUP_SIDS q;
        LSA_R_LOOKUP_SIDS r;
        DOM_R_REF ref;
-       LSA_TRANS_NAME_ENUM t_names;
-       NTSTATUS result;
+       NTSTATUS result = NT_STATUS_OK;
+       TALLOC_CTX *tmp_ctx = NULL;
        int i;
 
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
-
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
-       /* Marshall data and send request */
-
-       init_q_lookup_sids(mem_ctx, &q, pol, num_sids, sids, 1);
-
-       if (!lsa_io_q_lookup_sids("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_LOOKUPSIDS, &qbuf, &rbuf)) {
+       tmp_ctx = talloc_new(mem_ctx);
+       if (!tmp_ctx) {
+               DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
                result = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
 
-       /* Unmarshall response */
+       ZERO_STRUCT(q);
+       ZERO_STRUCT(r);
+
+       init_q_lookup_sids(tmp_ctx, &q, pol, num_sids, sids, 1);
 
        ZERO_STRUCT(ref);
-       ZERO_STRUCT(t_names);
 
        r.dom_ref = &ref;
-       r.names = &t_names;
 
-       if (!lsa_io_r_lookup_sids("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       CLI_DO_RPC( cli, tmp_ctx, PI_LSARPC, LSA_LOOKUPSIDS,
+                       q, r,
+                       qbuf, rbuf,
+                       lsa_io_q_lookup_sids,
+                       lsa_io_r_lookup_sids,
+                       NT_STATUS_UNSUCCESSFUL );
 
-       result = r.status;
+       DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
+                  nt_errstr(r.status), r.mapped_count));
 
-       if (!NT_STATUS_IS_OK(result) &&
-           NT_STATUS_V(result) != NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
-         
+       if (!NT_STATUS_IS_OK(r.status) &&
+           !NT_STATUS_EQUAL(r.status, NT_STATUS_NONE_MAPPED) &&
+           !NT_STATUS_EQUAL(r.status, STATUS_SOME_UNMAPPED))
+       {
                /* An actual error occured */
-
+               result = r.status;
                goto done;
        }
 
        /* Return output parameters */
 
-       if (r.mapped_count == 0) {
+       if (NT_STATUS_EQUAL(r.status, NT_STATUS_NONE_MAPPED) ||
+           (r.mapped_count == 0))
+       {
+               for (i = 0; i < num_sids; i++) {
+                       (names)[i] = NULL;
+                       (domains)[i] = NULL;
+                       (types)[i] = SID_NAME_UNKNOWN;
+               }
                result = NT_STATUS_NONE_MAPPED;
                goto done;
        }
 
-       if (!((*domains) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
-               DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       if (!((*names) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
-               DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       if (!((*types) = TALLOC_ARRAY(mem_ctx, uint32, num_sids))) {
-               DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-               
        for (i = 0; i < num_sids; i++) {
                fstring name, dom_name;
-               uint32 dom_idx = t_names.name[i].domain_idx;
+               uint32 dom_idx = r.names.name[i].domain_idx;
 
                /* Translate optimised name through domain index array */
 
@@ -305,38 +209,157 @@ NTSTATUS cli_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                        rpcstr_pull_unistr2_fstring(
                                 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
                        rpcstr_pull_unistr2_fstring(
-                                name, &t_names.uni_name[i]);
+                                name, &r.names.uni_name[i]);
 
-                       (*names)[i] = talloc_strdup(mem_ctx, name);
-                       (*domains)[i] = talloc_strdup(mem_ctx, dom_name);
-                       (*types)[i] = t_names.name[i].sid_name_use;
-                       
-                       if (((*names)[i] == NULL) || ((*domains)[i] == NULL)) {
-                               DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
+                       (names)[i] = talloc_strdup(mem_ctx, name);
+                       (domains)[i] = talloc_strdup(mem_ctx, dom_name);
+                       (types)[i] = r.names.name[i].sid_name_use;
+
+                       if (((names)[i] == NULL) || ((domains)[i] == NULL)) {
+                               DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
                                result = NT_STATUS_UNSUCCESSFUL;
                                goto done;
                        }
 
                } else {
-                       (*names)[i] = NULL;
-                       (*domains)[i] = NULL;
-                       (*types)[i] = SID_NAME_UNKNOWN;
+                       (names)[i] = NULL;
+                       (domains)[i] = NULL;
+                       (types)[i] = SID_NAME_UNKNOWN;
                }
        }
 
- done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
+done:
+       TALLOC_FREE(tmp_ctx);
+       return result;
+}
+
+/* Lookup a list of sids
+ *
+ * do it the right way: there is a limit (of 20480 for w2k3) entries
+ * returned by this call. when the sids list contains more entries,
+ * empty lists are returned. This version of lsa_lookup_sids passes
+ * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
+
+/* This constant defines the limit of how many sids to look up
+ * in one call (maximum). the limit from the server side is
+ * at 20480 for win2k3, but we keep it at a save 1000 for now. */
+#define LOOKUP_SIDS_HUNK_SIZE 1000
+
+NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
+                               TALLOC_CTX *mem_ctx,
+                               POLICY_HND *pol,
+                               int num_sids,
+                               const DOM_SID *sids,
+                               char ***domains,
+                               char ***names,
+                               enum lsa_SidType **types)
+{
+       NTSTATUS result = NT_STATUS_OK;
+       int sids_left = 0;
+       int sids_processed = 0;
+       const DOM_SID *hunk_sids = sids;
+       char **hunk_domains = NULL;
+       char **hunk_names = NULL;
+       enum lsa_SidType *hunk_types = NULL;
+
+       if (num_sids) {
+               if (!((*domains) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
+                       DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
+                       result = NT_STATUS_NO_MEMORY;
+                       goto fail;
+               }
+
+               if (!((*names) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
+                       DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
+                       result = NT_STATUS_NO_MEMORY;
+                       goto fail;
+               }
+
+               if (!((*types) = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
+                       DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
+                       result = NT_STATUS_NO_MEMORY;
+                       goto fail;
+               }
+       } else {
+               (*domains) = NULL;
+               (*names) = NULL;
+               (*types) = NULL;
+       }
+
+       sids_left = num_sids;
+       hunk_domains = *domains;
+       hunk_names = *names;
+       hunk_types = *types;
+
+       while (sids_left > 0) {
+               int hunk_num_sids;
+               NTSTATUS hunk_result = NT_STATUS_OK;
+
+               hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
+                               ? LOOKUP_SIDS_HUNK_SIZE
+                               : sids_left);
+
+               DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
+                          "%d -- %d of %d.\n",
+                          sids_processed,
+                          sids_processed + hunk_num_sids - 1,
+                          num_sids));
+
+               hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
+                                                            mem_ctx,
+                                                            pol,
+                                                            hunk_num_sids,
+                                                            hunk_sids,
+                                                            hunk_domains,
+                                                            hunk_names,
+                                                            hunk_types);
+
+               if (!NT_STATUS_IS_OK(hunk_result) &&
+                   !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
+                   !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
+               {
+                       /* An actual error occured */
+                       result = hunk_result;
+                       goto fail;
+               }
+
+               /* adapt overall result */
+               if (( NT_STATUS_IS_OK(result) &&
+                    !NT_STATUS_IS_OK(hunk_result))
+                   ||
+                   ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
+                    !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
+               {
+                       result = STATUS_SOME_UNMAPPED;
+               }
+
+               sids_left -= hunk_num_sids;
+               sids_processed += hunk_num_sids; /* only used in DEBUG */
+               hunk_sids += hunk_num_sids;
+               hunk_domains += hunk_num_sids;
+               hunk_names += hunk_num_sids;
+               hunk_types += hunk_num_sids;
+       }
 
        return result;
+
+fail:
+       TALLOC_FREE(*domains);
+       TALLOC_FREE(*names);
+       TALLOC_FREE(*types);
+       return result;
 }
 
 /** Lookup a list of names */
 
-NTSTATUS cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                              POLICY_HND *pol, int num_names, 
-                             const char **names, DOM_SID **sids, 
-                             uint32 **types)
+NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
+                                TALLOC_CTX *mem_ctx,
+                                POLICY_HND *pol, int num_names,
+                                const char **names,
+                                const char ***dom_names,
+                                int level,
+                                DOM_SID **sids,
+                                enum lsa_SidType **types)
 {
        prs_struct qbuf, rbuf;
        LSA_Q_LOOKUP_NAMES q;
@@ -344,34 +367,21 @@ NTSTATUS cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        DOM_R_REF ref;
        NTSTATUS result;
        int i;
-       
+
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
-       /* Marshall data and send request */
-
-       init_q_lookup_names(mem_ctx, &q, pol, num_names, names);
-
-       if (!lsa_io_q_lookup_names("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_LOOKUPNAMES, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-       
-       /* Unmarshall response */
-
        ZERO_STRUCT(ref);
        r.dom_ref = &ref;
 
-       if (!lsa_io_r_lookup_names("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       init_q_lookup_names(mem_ctx, &q, pol, num_names, names, level);
+
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPNAMES,
+                       q, r,
+                       qbuf, rbuf,
+                       lsa_io_q_lookup_names,
+                       lsa_io_r_lookup_names,
+                       NT_STATUS_UNSUCCESSFUL);
 
        result = r.status;
 
@@ -390,55 +400,74 @@ NTSTATUS cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                goto done;
        }
 
-       if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
-               DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       if (num_names) {
+               if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
+                       DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
+                       result = NT_STATUS_NO_MEMORY;
+                       goto done;
+               }
 
-       if (!((*types = TALLOC_ARRAY(mem_ctx, uint32, num_names)))) {
-               DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
+               if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
+                       DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
+                       result = NT_STATUS_NO_MEMORY;
+                       goto done;
+               }
+
+               if (dom_names != NULL) {
+                       *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
+                       if (*dom_names == NULL) {
+                               DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
+                               result = NT_STATUS_NO_MEMORY;
+                               goto done;
+                       }
+               }
+       } else {
+               *sids = NULL;
+               *types = NULL;
+               if (dom_names != NULL) {
+                       *dom_names = NULL;
+               }
        }
 
        for (i = 0; i < num_names; i++) {
-               DOM_RID2 *t_rids = r.dom_rid;
+               DOM_RID *t_rids = r.dom_rid;
                uint32 dom_idx = t_rids[i].rid_idx;
                uint32 dom_rid = t_rids[i].rid;
                DOM_SID *sid = &(*sids)[i];
 
                /* Translate optimised sid through domain index array */
 
-               if (dom_idx != 0xffffffff) {
+               if (dom_idx == 0xffffffff) {
+                       /* Nothing to do, this is unknown */
+                       ZERO_STRUCTP(sid);
+                       (*types)[i] = SID_NAME_UNKNOWN;
+                       continue;
+               }
 
-                       sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
+               sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
 
-                       if (dom_rid != 0xffffffff) {
-                               sid_append_rid(sid, dom_rid);
-                       }
+               if (dom_rid != 0xffffffff) {
+                       sid_append_rid(sid, dom_rid);
+               }
 
-                       (*types)[i] = t_rids[i].type;
-               } else {
-                       ZERO_STRUCTP(sid);
-                       (*types)[i] = SID_NAME_UNKNOWN;
+               (*types)[i] = t_rids[i].type;
+
+               if (dom_names == NULL) {
+                       continue;
                }
+
+               (*dom_names)[i] = rpcstr_pull_unistr2_talloc(
+                       *dom_names, &ref.ref_dom[dom_idx].uni_dom_name);
        }
 
  done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
 
        return result;
 }
 
-/** Query info policy
- *
- *  @param domain_sid - returned remote server's domain sid */
-
-NTSTATUS cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                                   POLICY_HND *pol, uint16 info_class, 
-                                   char **domain_name, DOM_SID **domain_sid)
+NTSTATUS rpccli_lsa_query_info_policy_new(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
+                                         POLICY_HND *pol, uint16 info_class,
+                                         LSA_INFO_CTR *ctr)
 {
        prs_struct qbuf, rbuf;
        LSA_Q_QUERY_INFO q;
@@ -448,29 +477,93 @@ NTSTATUS cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
+       init_q_query(&q, pol, info_class);
 
-       /* Marshall data and send request */
+       CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFOPOLICY,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_query,
+               lsa_io_r_query,
+               NT_STATUS_UNSUCCESSFUL);
 
-       init_q_query(&q, pol, info_class);
+       result = r.status;
 
-       if (!lsa_io_q_query("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_QUERYINFOPOLICY, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
-       /* Unmarshall response */
+ done:
 
-       if (!lsa_io_r_query("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
+       *ctr = r.ctr;
+
+       return result;
+}
+
+NTSTATUS rpccli_lsa_query_info_policy2_new(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
+                                         POLICY_HND *pol, uint16 info_class,
+                                         LSA_INFO_CTR2 *ctr)
+{
+       prs_struct qbuf, rbuf;
+       LSA_Q_QUERY_INFO2 q;
+       LSA_R_QUERY_INFO2 r;
+       NTSTATUS result;
+
+       ZERO_STRUCT(q);
+       ZERO_STRUCT(r);
+
+       init_q_query2(&q, pol, info_class);
+
+       CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFO2,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_query_info2,
+               lsa_io_r_query_info2,
+               NT_STATUS_UNSUCCESSFUL);
+
+       result = r.status;
+
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
-       if (!NT_STATUS_IS_OK(result = r.status)) {
+ done:
+
+       *ctr = r.ctr;
+
+       return result;
+}
+
+
+
+/** Query info policy
+ *
+ *  @param domain_sid - returned remote server's domain sid */
+
+NTSTATUS rpccli_lsa_query_info_policy(struct rpc_pipe_client *cli,
+                                     TALLOC_CTX *mem_ctx,
+                                     POLICY_HND *pol, uint16 info_class,
+                                     char **domain_name, DOM_SID **domain_sid)
+{
+       prs_struct qbuf, rbuf;
+       LSA_Q_QUERY_INFO q;
+       LSA_R_QUERY_INFO r;
+       NTSTATUS result;
+
+       ZERO_STRUCT(q);
+       ZERO_STRUCT(r);
+
+       init_q_query(&q, pol, info_class);
+
+       CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFOPOLICY,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_query,
+               lsa_io_r_query,
+               NT_STATUS_UNSUCCESSFUL);
+
+       result = r.status;
+
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
@@ -479,45 +572,51 @@ NTSTATUS cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        switch (info_class) {
 
        case 3:
-               if (domain_name && (r.dom.id3.buffer_dom_name != 0)) {
-                       *domain_name = unistr2_tdup(mem_ctx, 
-                                                  &r.dom.id3.
+               if (domain_name && (r.ctr.info.id3.buffer_dom_name != 0)) {
+                       *domain_name = unistr2_to_ascii_talloc(mem_ctx,
+                                                  &r.ctr.info.id3.
                                                   uni_domain_name);
+                       if (!*domain_name) {
+                               return NT_STATUS_NO_MEMORY;
+                       }
                }
 
-               if (domain_sid && (r.dom.id3.buffer_dom_sid != 0)) {
+               if (domain_sid && (r.ctr.info.id3.buffer_dom_sid != 0)) {
                        *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
-                       if (*domain_sid) {
-                               sid_copy(*domain_sid, &r.dom.id3.dom_sid.sid);
+                       if (!*domain_sid) {
+                               return NT_STATUS_NO_MEMORY;
                        }
+                       sid_copy(*domain_sid, &r.ctr.info.id3.dom_sid.sid);
                }
 
                break;
 
        case 5:
-               
-               if (domain_name && (r.dom.id5.buffer_dom_name != 0)) {
-                       *domain_name = unistr2_tdup(mem_ctx, 
-                                                  &r.dom.id5.
+
+               if (domain_name && (r.ctr.info.id5.buffer_dom_name != 0)) {
+                       *domain_name = unistr2_to_ascii_talloc(mem_ctx,
+                                                  &r.ctr.info.id5.
                                                   uni_domain_name);
+                       if (!*domain_name) {
+                               return NT_STATUS_NO_MEMORY;
+                       }
                }
-                       
-               if (domain_sid && (r.dom.id5.buffer_dom_sid != 0)) {
+
+               if (domain_sid && (r.ctr.info.id5.buffer_dom_sid != 0)) {
                        *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
-                       if (*domain_sid) {
-                               sid_copy(*domain_sid, &r.dom.id5.dom_sid.sid);
+                       if (!*domain_sid) {
+                               return NT_STATUS_NO_MEMORY;
                        }
+                       sid_copy(*domain_sid, &r.ctr.info.id5.dom_sid.sid);
                }
                break;
-                       
+
        default:
                DEBUG(3, ("unknown info class %d\n", info_class));
-               break;                
+               break;
        }
-       
+
  done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
 
        return result;
 }
@@ -530,11 +629,13 @@ NTSTATUS cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
  *  @param domain_guid - returned remote server's domain guid
  *  @param domain_sid - returned remote server's domain sid */
 
-NTSTATUS cli_lsa_query_info_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                                   POLICY_HND *pol, uint16 info_class, 
-                                   char **domain_name, char **dns_name,
-                                   char **forest_name, struct uuid **domain_guid,
-                                   DOM_SID **domain_sid)
+NTSTATUS rpccli_lsa_query_info_policy2(struct rpc_pipe_client *cli,
+                                      TALLOC_CTX *mem_ctx,
+                                      POLICY_HND *pol, uint16 info_class,
+                                      char **domain_name, char **dns_name,
+                                      char **forest_name,
+                                      struct GUID **domain_guid,
+                                      DOM_SID **domain_sid)
 {
        prs_struct qbuf, rbuf;
        LSA_Q_QUERY_INFO2 q;
@@ -547,29 +648,18 @@ NTSTATUS cli_lsa_query_info_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
-       /* Marshall data and send request */
-
        init_q_query2(&q, pol, info_class);
 
-       if (!lsa_io_q_query_info2("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_QUERYINFO2, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Unmarshall response */
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYINFO2,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_query_info2,
+               lsa_io_r_query_info2,
+               NT_STATUS_UNSUCCESSFUL);
 
-       if (!lsa_io_r_query_info2("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       result = r.status;
 
-       if (!NT_STATUS_IS_OK(result = r.status)) {
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
@@ -577,40 +667,51 @@ NTSTATUS cli_lsa_query_info_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        ZERO_STRUCTP(domain_guid);
 
-       if (domain_name && r.info.dns_dom_info.hdr_nb_dom_name.buffer) {
-               *domain_name = unistr2_tdup(mem_ctx, 
-                                           &r.info.dns_dom_info
+       if (domain_name && r.ctr.info.id12.hdr_nb_dom_name.buffer) {
+               *domain_name = unistr2_to_ascii_talloc(mem_ctx,
+                                           &r.ctr.info.id12
                                            .uni_nb_dom_name);
+               if (!*domain_name) {
+                       return NT_STATUS_NO_MEMORY;
+               }
        }
-       if (dns_name && r.info.dns_dom_info.hdr_dns_dom_name.buffer) {
-               *dns_name = unistr2_tdup(mem_ctx, 
-                                        &r.info.dns_dom_info
+       if (dns_name && r.ctr.info.id12.hdr_dns_dom_name.buffer) {
+               *dns_name = unistr2_to_ascii_talloc(mem_ctx,
+                                        &r.ctr.info.id12
                                         .uni_dns_dom_name);
+               if (!*dns_name) {
+                       return NT_STATUS_NO_MEMORY;
+               }
        }
-       if (forest_name && r.info.dns_dom_info.hdr_forest_name.buffer) {
-               *forest_name = unistr2_tdup(mem_ctx, 
-                                           &r.info.dns_dom_info
+       if (forest_name && r.ctr.info.id12.hdr_forest_name.buffer) {
+               *forest_name = unistr2_to_ascii_talloc(mem_ctx,
+                                           &r.ctr.info.id12
                                            .uni_forest_name);
+               if (!*forest_name) {
+                       return NT_STATUS_NO_MEMORY;
+               }
        }
-       
+
        if (domain_guid) {
-               *domain_guid = TALLOC_P(mem_ctx, struct uuid);
-               memcpy(*domain_guid, 
-                      &r.info.dns_dom_info.dom_guid, 
-                      sizeof(struct uuid));
+               *domain_guid = TALLOC_P(mem_ctx, struct GUID);
+               if (!*domain_guid) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               memcpy(*domain_guid,
+                      &r.ctr.info.id12.dom_guid,
+                      sizeof(struct GUID));
        }
 
-       if (domain_sid && r.info.dns_dom_info.ptr_dom_sid != 0) {
+       if (domain_sid && r.ctr.info.id12.ptr_dom_sid != 0) {
                *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
-               if (*domain_sid) {
-                       sid_copy(*domain_sid, 
-                                &r.info.dns_dom_info.dom_sid.sid);
+               if (!*domain_sid) {
+                       return NT_STATUS_NO_MEMORY;
                }
+               sid_copy(*domain_sid,
+                        &r.ctr.info.id12.dom_sid.sid);
        }
-       
+
  done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
 
        return result;
 }
@@ -630,101 +731,79 @@ NTSTATUS cli_lsa_query_info_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
  * @return nt status code of response
  **/
 
-NTSTATUS cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                                POLICY_HND *pol, uint32 *enum_ctx, 
-                                uint32 *num_domains,
-                                char ***domain_names, DOM_SID **domain_sids)
+NTSTATUS rpccli_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
+                                  TALLOC_CTX *mem_ctx,
+                                  POLICY_HND *pol, uint32 *enum_ctx,
+                                  uint32 *num_domains,
+                                  char ***domain_names, DOM_SID **domain_sids)
 {
        prs_struct qbuf, rbuf;
-       LSA_Q_ENUM_TRUST_DOM q;
-       LSA_R_ENUM_TRUST_DOM r;
-       NTSTATUS result;
+       LSA_Q_ENUM_TRUST_DOM in;
+       LSA_R_ENUM_TRUST_DOM out;
        int i;
+       fstring tmp;
 
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
-
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
-       /* Marshall data and send request */
+       ZERO_STRUCT(in);
+       ZERO_STRUCT(out);
 
        /* 64k is enough for about 2000 trusted domains */
-        init_q_enum_trust_dom(&q, pol, *enum_ctx, 0x10000);
 
-       if (!lsa_io_q_enum_trust_dom("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_ENUMTRUSTDOM, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+        init_q_enum_trust_dom(&in, pol, *enum_ctx, 0x10000);
 
-       /* Unmarshall response */
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMTRUSTDOM,
+                   in, out,
+                   qbuf, rbuf,
+                   lsa_io_q_enum_trust_dom,
+                   lsa_io_r_enum_trust_dom,
+                   NT_STATUS_UNSUCCESSFUL );
 
-       if (!lsa_io_r_enum_trust_dom("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
 
-       result = r.status;
+       /* check for an actual error */
 
-       if (!NT_STATUS_IS_OK(result) &&
-           !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
-           !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
-
-               /* An actual error ocured */
-
-               goto done;
+       if ( !NT_STATUS_IS_OK(out.status)
+               && !NT_STATUS_EQUAL(out.status, NT_STATUS_NO_MORE_ENTRIES)
+               && !NT_STATUS_EQUAL(out.status, STATUS_MORE_ENTRIES) )
+       {
+               return out.status;
        }
 
        /* Return output parameters */
 
-       if (r.num_domains) {
+       *num_domains  = out.count;
+       *enum_ctx     = out.enum_context;
 
-               /* Allocate memory for trusted domain names and sids */
+       if ( out.count ) {
 
-               *domain_names = TALLOC_ARRAY(mem_ctx, char *, r.num_domains);
+               /* Allocate memory for trusted domain names and sids */
 
-               if (!*domain_names) {
+               if ( !(*domain_names = TALLOC_ARRAY(mem_ctx, char *, out.count)) ) {
                        DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
-                       result = NT_STATUS_NO_MEMORY;
-                       goto done;
+                       return NT_STATUS_NO_MEMORY;
                }
 
-               *domain_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, r.num_domains);
-               if (!domain_sids) {
+               if ( !(*domain_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, out.count)) ) {
                        DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
-                       result = NT_STATUS_NO_MEMORY;
-                       goto done;
+                       return NT_STATUS_NO_MEMORY;
                }
 
                /* Copy across names and sids */
 
-               for (i = 0; i < r.num_domains; i++) {
-                       fstring tmp;
+               for (i = 0; i < out.count; i++) {
 
-                       unistr2_to_ascii(tmp, &r.uni_domain_name[i], 
-                                        sizeof(tmp) - 1);
+                       rpcstr_pull( tmp, out.domlist->domains[i].name.string->buffer,
+                               sizeof(tmp), out.domlist->domains[i].name.length, 0);
                        (*domain_names)[i] = talloc_strdup(mem_ctx, tmp);
-                       sid_copy(&(*domain_sids)[i], &r.domain_sid[i].sid);
+
+                       sid_copy(&(*domain_sids)[i], &out.domlist->domains[i].sid->sid );
                }
        }
 
-       *num_domains = r.num_domains;
-       *enum_ctx = r.enum_context;
-
- done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
-
-       return result;
+       return out.status;
 }
 
-
 /** Enumerate privileges*/
 
-NTSTATUS cli_lsa_enum_privilege(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+NTSTATUS rpccli_lsa_enum_privilege(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
                                 POLICY_HND *pol, uint32 *enum_context, uint32 pref_max_length,
                                uint32 *count, char ***privs_name, uint32 **privs_high, uint32 **privs_low)
 {
@@ -737,29 +816,18 @@ NTSTATUS cli_lsa_enum_privilege(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
-       /* Marshall data and send request */
-
        init_q_enum_privs(&q, pol, *enum_context, pref_max_length);
 
-       if (!lsa_io_q_enum_privs("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_ENUM_PRIVS, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Unmarshall response */
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_PRIVS,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_enum_privs,
+               lsa_io_r_enum_privs,
+               NT_STATUS_UNSUCCESSFUL);
 
-       if (!lsa_io_r_enum_privs("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       result = r.status;
 
-       if (!NT_STATUS_IS_OK(result = r.status)) {
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
@@ -768,22 +836,28 @@ NTSTATUS cli_lsa_enum_privilege(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        *enum_context = r.enum_context;
        *count = r.count;
 
-       if (!((*privs_name = TALLOC_ARRAY(mem_ctx, char *, r.count)))) {
-               DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       if (r.count) {
+               if (!((*privs_name = TALLOC_ARRAY(mem_ctx, char *, r.count)))) {
+                       DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
+                       result = NT_STATUS_UNSUCCESSFUL;
+                       goto done;
+               }
 
-       if (!((*privs_high = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
-               DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+               if (!((*privs_high = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
+                       DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
+                       result = NT_STATUS_UNSUCCESSFUL;
+                       goto done;
+               }
 
-       if (!((*privs_low = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
-               DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
+               if (!((*privs_low = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
+                       DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
+                       result = NT_STATUS_UNSUCCESSFUL;
+                       goto done;
+               }
+       } else {
+               *privs_name = NULL;
+               *privs_high = NULL;
+               *privs_low = NULL;
        }
 
        for (i = 0; i < r.count; i++) {
@@ -798,16 +872,14 @@ NTSTATUS cli_lsa_enum_privilege(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        }
 
  done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
 
        return result;
 }
 
 /** Get privilege name */
 
-NTSTATUS cli_lsa_get_dispname(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                             POLICY_HND *pol, const char *name, 
+NTSTATUS rpccli_lsa_get_dispname(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
+                             POLICY_HND *pol, const char *name,
                              uint16 lang_id, uint16 lang_id_sys,
                              fstring description, uint16 *lang_id_desc)
 {
@@ -819,48 +891,35 @@ NTSTATUS cli_lsa_get_dispname(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
-       /* Marshall data and send request */
-
        init_lsa_priv_get_dispname(&q, pol, name, lang_id, lang_id_sys);
 
-       if (!lsa_io_q_priv_get_dispname("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_PRIV_GET_DISPNAME, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Unmarshall response */
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_PRIV_GET_DISPNAME,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_priv_get_dispname,
+               lsa_io_r_priv_get_dispname,
+               NT_STATUS_UNSUCCESSFUL);
 
-       if (!lsa_io_r_priv_get_dispname("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       result = r.status;
 
-       if (!NT_STATUS_IS_OK(result = r.status)) {
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
        /* Return output parameters */
-       
+
        rpcstr_pull_unistr2_fstring(description , &r.desc);
        *lang_id_desc = r.lang_id;
 
  done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
 
        return result;
 }
 
 /** Enumerate list of SIDs  */
 
-NTSTATUS cli_lsa_enum_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                                POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length, 
+NTSTATUS rpccli_lsa_enum_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
+                                POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length,
                                 uint32 *num_sids, DOM_SID **sids)
 {
        prs_struct qbuf, rbuf;
@@ -872,31 +931,18 @@ NTSTATUS cli_lsa_enum_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
-       /* Marshall data and send request */
-
         init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
 
-       if (!lsa_io_q_enum_accounts("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_ENUM_ACCOUNTS, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Unmarshall response */
-
-       if (!lsa_io_r_enum_accounts("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_ACCOUNTS,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_enum_accounts,
+               lsa_io_r_enum_accounts,
+               NT_STATUS_UNSUCCESSFUL);
 
        result = r.status;
 
-       if (!NT_STATUS_IS_OK(result = r.status)) {
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
@@ -922,8 +968,6 @@ NTSTATUS cli_lsa_enum_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        *enum_ctx = r.enum_context;
 
  done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
 
        return result;
 }
@@ -937,8 +981,8 @@ NTSTATUS cli_lsa_enum_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
  *
  * */
 
-NTSTATUS cli_lsa_create_account(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                             POLICY_HND *dom_pol, DOM_SID *sid, uint32 desired_access, 
+NTSTATUS rpccli_lsa_create_account(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
+                             POLICY_HND *dom_pol, DOM_SID *sid, uint32 desired_access,
                             POLICY_HND *user_pol)
 {
        prs_struct qbuf, rbuf;
@@ -949,40 +993,25 @@ NTSTATUS cli_lsa_create_account(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
        /* Initialise input parameters */
 
        init_lsa_q_create_account(&q, dom_pol, sid, desired_access);
 
-       /* Marshall data and send request */
-
-       if (!lsa_io_q_create_account("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_CREATEACCOUNT, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Unmarshall response */
-
-       if (!lsa_io_r_create_account("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_CREATEACCOUNT,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_create_account,
+               lsa_io_r_create_account,
+               NT_STATUS_UNSUCCESSFUL);
 
        /* Return output parameters */
 
-       if (NT_STATUS_IS_OK(result = r.status)) {
+       result = r.status;
+
+       if (NT_STATUS_IS_OK(result)) {
                *user_pol = r.pol;
        }
 
- done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
-
        return result;
 }
 
@@ -990,8 +1019,8 @@ NTSTATUS cli_lsa_create_account(struct cli_state *cli, TALLOC_CTX *mem_ctx,
  *
  * @param cli Handle on an initialised SMB connection */
 
-NTSTATUS cli_lsa_open_account(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                             POLICY_HND *dom_pol, DOM_SID *sid, uint32 des_access, 
+NTSTATUS rpccli_lsa_open_account(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
+                             POLICY_HND *dom_pol, DOM_SID *sid, uint32 des_access,
                             POLICY_HND *user_pol)
 {
        prs_struct qbuf, rbuf;
@@ -1002,40 +1031,25 @@ NTSTATUS cli_lsa_open_account(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
        /* Initialise input parameters */
 
        init_lsa_q_open_account(&q, dom_pol, sid, des_access);
 
-       /* Marshall data and send request */
-
-       if (!lsa_io_q_open_account("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_OPENACCOUNT, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Unmarshall response */
-
-       if (!lsa_io_r_open_account("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENACCOUNT,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_open_account,
+               lsa_io_r_open_account,
+               NT_STATUS_UNSUCCESSFUL);
 
        /* Return output parameters */
 
-       if (NT_STATUS_IS_OK(result = r.status)) {
+       result = r.status;
+
+       if (NT_STATUS_IS_OK(result)) {
                *user_pol = r.pol;
        }
 
- done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
-
        return result;
 }
 
@@ -1043,7 +1057,7 @@ NTSTATUS cli_lsa_open_account(struct cli_state *cli, TALLOC_CTX *mem_ctx,
  *
  * @param cli Handle on an initialised SMB connection */
 
-NTSTATUS cli_lsa_enum_privsaccount(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+NTSTATUS rpccli_lsa_enum_privsaccount(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
                              POLICY_HND *pol, uint32 *count, LUID_ATTR **set)
 {
        prs_struct qbuf, rbuf;
@@ -1055,33 +1069,22 @@ NTSTATUS cli_lsa_enum_privsaccount(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
        /* Initialise input parameters */
 
        init_lsa_q_enum_privsaccount(&q, pol);
 
-       /* Marshall data and send request */
-
-       if (!lsa_io_q_enum_privsaccount("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_ENUMPRIVSACCOUNT, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Unmarshall response */
-
-       if (!lsa_io_r_enum_privsaccount("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMPRIVSACCOUNT,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_enum_privsaccount,
+               lsa_io_r_enum_privsaccount,
+               NT_STATUS_UNSUCCESSFUL);
 
        /* Return output parameters */
 
-       if (!NT_STATUS_IS_OK(result = r.status)) {
+       result = r.status;
+
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
@@ -1102,48 +1105,37 @@ NTSTATUS cli_lsa_enum_privsaccount(struct cli_state *cli, TALLOC_CTX *mem_ctx,
 
        *count=r.count;
  done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
 
        return result;
 }
 
 /** Get a privilege value given its name */
 
-NTSTATUS cli_lsa_lookupprivvalue(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+NTSTATUS rpccli_lsa_lookup_priv_value(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
                                 POLICY_HND *pol, const char *name, LUID *luid)
 {
        prs_struct qbuf, rbuf;
-       LSA_Q_LOOKUPPRIVVALUE q;
-       LSA_R_LOOKUPPRIVVALUE r;
+       LSA_Q_LOOKUP_PRIV_VALUE q;
+       LSA_R_LOOKUP_PRIV_VALUE r;
        NTSTATUS result;
 
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
        /* Marshall data and send request */
 
-       init_lsa_q_lookupprivvalue(&q, pol, name);
+       init_lsa_q_lookup_priv_value(&q, pol, name);
 
-       if (!lsa_io_q_lookupprivvalue("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_LOOKUPPRIVVALUE, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPPRIVVALUE,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_lookup_priv_value,
+               lsa_io_r_lookup_priv_value,
+               NT_STATUS_UNSUCCESSFUL);
 
-       /* Unmarshall response */
-
-       if (!lsa_io_r_lookupprivvalue("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       result = r.status;
 
-       if (!NT_STATUS_IS_OK(result = r.status)) {
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
@@ -1153,16 +1145,14 @@ NTSTATUS cli_lsa_lookupprivvalue(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        (*luid).high=r.luid.high;
 
  done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
 
        return result;
 }
 
 /** Query LSA security object */
 
-NTSTATUS cli_lsa_query_secobj(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                             POLICY_HND *pol, uint32 sec_info, 
+NTSTATUS rpccli_lsa_query_secobj(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
+                             POLICY_HND *pol, uint32 sec_info,
                              SEC_DESC_BUF **psdb)
 {
        prs_struct qbuf, rbuf;
@@ -1173,29 +1163,20 @@ NTSTATUS cli_lsa_query_secobj(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
        /* Marshall data and send request */
 
        init_q_query_sec_obj(&q, pol, sec_info);
 
-       if (!lsa_io_q_query_sec_obj("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_QUERYSECOBJ, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYSECOBJ,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_query_sec_obj,
+               lsa_io_r_query_sec_obj,
+               NT_STATUS_UNSUCCESSFUL);
 
-       /* Unmarshall response */
-
-       if (!lsa_io_r_query_sec_obj("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       result = r.status;
 
-       if (!NT_STATUS_IS_OK(result = r.status)) {
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
@@ -1205,8 +1186,6 @@ NTSTATUS cli_lsa_query_secobj(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                *psdb = r.buf;
 
  done:
-       prs_mem_free(&qbuf);
-       prs_mem_free(&rbuf);
 
        return result;
 }
@@ -1216,9 +1195,9 @@ NTSTATUS cli_lsa_query_secobj(struct cli_state *cli, TALLOC_CTX *mem_ctx,
    takes a SID directly, avoiding the open_account call.
 */
 
-NTSTATUS cli_lsa_enum_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+NTSTATUS rpccli_lsa_enum_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
                                     POLICY_HND *pol, DOM_SID *sid,
-                                    uint32 *count, char ***privs_name)
+                                    uint32 *count, char ***priv_names)
 {
        prs_struct qbuf, rbuf;
        LSA_Q_ENUM_ACCT_RIGHTS q;
@@ -1226,30 +1205,24 @@ NTSTATUS cli_lsa_enum_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        NTSTATUS result;
        int i;
        fstring *privileges;
+       char **names;
 
        ZERO_STRUCT(q);
        ZERO_STRUCT(r);
 
-       /* Initialise parse structures */
-
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
-
        /* Marshall data and send request */
        init_q_enum_acct_rights(&q, pol, 2, sid);
 
-       if (!lsa_io_q_enum_acct_rights("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_ENUMACCTRIGHTS, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMACCTRIGHTS,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_enum_acct_rights,
+               lsa_io_r_enum_acct_rights,
+               NT_STATUS_UNSUCCESSFUL);
 
-       if (!lsa_io_r_enum_acct_rights("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       result = r.status;
 
-       if (!NT_STATUS_IS_OK(result = r.status)) {
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
@@ -1258,18 +1231,30 @@ NTSTATUS cli_lsa_enum_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                goto done;
        }
 
-       
-       privileges = TALLOC_ARRAY(mem_ctx, fstring, *count);
-       *privs_name = TALLOC_ARRAY(mem_ctx, char *, *count);
+
+       privileges = TALLOC_ARRAY( mem_ctx, fstring, *count );
+       names      = TALLOC_ARRAY( mem_ctx, char *, *count );
+
+       if ((privileges == NULL) || (names == NULL)) {
+               TALLOC_FREE(privileges);
+               TALLOC_FREE(names);
+               return NT_STATUS_NO_MEMORY;
+       }
+
        for ( i=0; i<*count; i++ ) {
-               /* ensure NULL termination ... what a hack */
-               pull_ucs2(NULL, privileges[i], r.rights.strings[i].string.buffer, 
-                       sizeof(fstring), r.rights.strings[i].string.uni_str_len*2 , 0);
-                       
+               UNISTR4 *uni_string = &r.rights->strings[i];
+
+               if ( !uni_string->string )
+                       continue;
+
+               rpcstr_pull( privileges[i], uni_string->string->buffer, sizeof(privileges[i]), -1, STR_TERMINATE );
+
                /* now copy to the return array */
-               *privs_name[i] = talloc_strdup( mem_ctx, privileges[i] );
+               names[i] = talloc_strdup( mem_ctx, privileges[i] );
        }
 
+       *priv_names = names;
+
 done:
 
        return result;
@@ -1279,9 +1264,9 @@ done:
 
 /* add account rights to an account. */
 
-NTSTATUS cli_lsa_add_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+NTSTATUS rpccli_lsa_add_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
                                    POLICY_HND *pol, DOM_SID sid,
-                                   uint32 count, const char **privs_name)
+                                       uint32 count, const char **privs_name)
 {
        prs_struct qbuf, rbuf;
        LSA_Q_ADD_ACCT_RIGHTS q;
@@ -1289,28 +1274,21 @@ NTSTATUS cli_lsa_add_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        NTSTATUS result;
 
        ZERO_STRUCT(q);
-
-       /* Initialise parse structures */
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
+       ZERO_STRUCT(r);
 
        /* Marshall data and send request */
        init_q_add_acct_rights(&q, pol, &sid, count, privs_name);
 
-       if (!lsa_io_q_add_acct_rights("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_ADDACCTRIGHTS, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ADDACCTRIGHTS,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_add_acct_rights,
+               lsa_io_r_add_acct_rights,
+               NT_STATUS_UNSUCCESSFUL);
 
-       /* Unmarshall response */
-
-       if (!lsa_io_r_add_acct_rights("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       result = r.status;
 
-       if (!NT_STATUS_IS_OK(result = r.status)) {
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 done:
@@ -1321,8 +1299,8 @@ done:
 
 /* remove account rights for an account. */
 
-NTSTATUS cli_lsa_remove_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                                      POLICY_HND *pol, DOM_SID sid, BOOL removeall,
+NTSTATUS rpccli_lsa_remove_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
+                                      POLICY_HND *pol, DOM_SID sid, bool removeall,
                                       uint32 count, const char **privs_name)
 {
        prs_struct qbuf, rbuf;
@@ -1331,28 +1309,21 @@ NTSTATUS cli_lsa_remove_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ct
        NTSTATUS result;
 
        ZERO_STRUCT(q);
-
-       /* Initialise parse structures */
-       prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
-       prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
+       ZERO_STRUCT(r);
 
        /* Marshall data and send request */
        init_q_remove_acct_rights(&q, pol, &sid, removeall?1:0, count, privs_name);
 
-       if (!lsa_io_q_remove_acct_rights("", &q, &qbuf, 0) ||
-           !rpc_api_pipe_req(cli, PI_LSARPC, LSA_REMOVEACCTRIGHTS, &qbuf, &rbuf)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Unmarshall response */
+       CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_REMOVEACCTRIGHTS,
+               q, r,
+               qbuf, rbuf,
+               lsa_io_q_remove_acct_rights,
+               lsa_io_r_remove_acct_rights,
+               NT_STATUS_UNSUCCESSFUL);
 
-       if (!lsa_io_r_remove_acct_rights("", &r, &rbuf, 0)) {
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
+       result = r.status;
 
-       if (!NT_STATUS_IS_OK(result = r.status)) {
+       if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 done:
@@ -1366,61 +1337,60 @@ done:
 /** An example of how to use the routines in this file.  Fetch a DOMAIN
     sid. Does complete cli setup / teardown anonymously. */
 
-BOOL fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
+bool fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
 {
-       extern pstring global_myname;
        struct cli_state cli;
        NTSTATUS result;
        POLICY_HND lsa_pol;
-       BOOL ret = False;
+       bool ret = False;
+
        ZERO_STRUCT(cli);
        if(cli_initialise(&cli) == False) {
                DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
                return False;
        }
+
        if(!resolve_name( remote_machine, &cli.dest_ip, 0x20)) {
                DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
                goto done;
        }
+
        if (!cli_connect(&cli, remote_machine, &cli.dest_ip)) {
                DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
                goto done;
        }
 
-       if (!attempt_netbios_session_request(&cli, global_myname, remote_machine, &cli.dest_ip)) {
-               DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n", 
+       if (!attempt_netbios_session_request(&cli, global_myname(), remote_machine, &cli.dest_ip)) {
+               DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
                        remote_machine));
                goto done;
        }
+
        cli.protocol = PROTOCOL_NT1;
+
        if (!cli_negprot(&cli)) {
                DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
                goto done;
        }
+
        if (cli.protocol != PROTOCOL_NT1) {
                DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
                        remote_machine));
                goto done;
        }
+
        /*
         * Do an anonymous session setup.
         */
+
        if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
                DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
                goto done;
        }
+
        if (!(cli.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
                DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
                        remote_machine));
@@ -1434,26 +1404,26 @@ Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
        }
 
        /* Fetch domain sid */
+
        if (!cli_nt_session_open(&cli, PI_LSARPC)) {
                DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
                goto done;
        }
+
        result = cli_lsa_open_policy(&cli, cli.mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
                        nt_errstr(result) ));
                goto done;
        }
+
        result = cli_lsa_query_info_policy(&cli, cli.mem_ctx, &lsa_pol, 5, domain, psid);
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
                        nt_errstr(result) ));
                goto done;
        }
+
        ret = True;
 
   done:
@@ -1463,5 +1433,3 @@ Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
 }
 
 #endif
-
-/** @} **/