s3-talloc Change TALLOC_ARRAY() to talloc_array()
[samba.git] / source3 / rpc_client / cli_lsarpc.c
index c42938d6e905112751747f7c56b6c4bb2a53cd21..66192bdf54f9ec8c8c51c121518731fb7e0aea33 100644 (file)
@@ -6,6 +6,7 @@
    Copyright (C) Rafal Szczesniak                       2002
    Copyright (C) Jeremy Allison                                2005.
    Copyright (C) Michael Adam                          2007.
+   Copyright (C) Guenther Deschner                     2008.
 
    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
 */
 
 #include "includes.h"
+#include "rpc_client/rpc_client.h"
+#include "../librpc/gen_ndr/ndr_lsa_c.h"
+#include "rpc_client/cli_lsarpc.h"
+#include "rpc_client/init_lsa.h"
+#include "../libcli/security/security.h"
 
 /** @defgroup lsa LSA - Local Security Architecture
  *  @ingroup rpc_client
  * security authority", which is half of a password database.
  **/
 
+NTSTATUS dcerpc_lsa_open_policy(struct dcerpc_binding_handle *h,
+                               TALLOC_CTX *mem_ctx,
+                               bool sec_qos,
+                               uint32_t des_access,
+                               struct policy_handle *pol,
+                               NTSTATUS *result)
+{
+       struct lsa_ObjectAttribute attr;
+       struct lsa_QosInfo qos;
+       uint16_t system_name = '\\';
+
+       ZERO_STRUCT(attr);
+
+       attr.len        = 0x18;
+
+       if (sec_qos) {
+               qos.len                 = 0xc;
+               qos.impersonation_level = 2;
+               qos.context_mode        = 1;
+               qos.effective_only      = 0;
+
+               attr.sec_qos            = &qos;
+       }
+
+       return dcerpc_lsa_OpenPolicy(h,
+                                    mem_ctx,
+                                    &system_name,
+                                    &attr,
+                                    des_access,
+                                    pol,
+                                    result);
+}
+
 /** Open a LSA policy handle
  *
  * @param cli Handle on an initialised SMB connection */
 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
                                TALLOC_CTX *mem_ctx,
                                bool sec_qos, uint32 des_access,
-                               POLICY_HND *pol)
+                               struct policy_handle *pol)
+{
+       NTSTATUS status;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+
+       status = dcerpc_lsa_open_policy(cli->binding_handle,
+                                       mem_ctx,
+                                       sec_qos,
+                                       des_access,
+                                       pol,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       return result;
+}
+
+NTSTATUS dcerpc_lsa_open_policy2(struct dcerpc_binding_handle *h,
+                                TALLOC_CTX *mem_ctx,
+                                const char *srv_name_slash,
+                                bool sec_qos,
+                                uint32_t des_access,
+                                struct policy_handle *pol,
+                                NTSTATUS *result)
 {
        struct lsa_ObjectAttribute attr;
        struct lsa_QosInfo qos;
-       uint16_t system_name = '\\';
+
+       ZERO_STRUCT(attr);
+
+       attr.len        = 0x18;
 
        if (sec_qos) {
-               init_lsa_sec_qos(&qos, 0xc, 2, 1, 0);
-               init_lsa_obj_attr(&attr,
-                                 0x18,
-                                 NULL,
-                                 NULL,
-                                 0,
-                                 NULL,
-                                 &qos);
-       } else {
-               init_lsa_obj_attr(&attr,
-                                 0x18,
-                                 NULL,
-                                 NULL,
-                                 0,
-                                 NULL,
-                                 NULL);
+               qos.len                 = 0xc;
+               qos.impersonation_level = 2;
+               qos.context_mode        = 1;
+               qos.effective_only      = 0;
+
+               attr.sec_qos            = &qos;
        }
 
-       return rpccli_lsa_OpenPolicy(cli, mem_ctx,
-                                    &system_name,
-                                    &attr,
-                                    des_access,
-                                    pol);
+       return dcerpc_lsa_OpenPolicy2(h,
+                                     mem_ctx,
+                                     srv_name_slash,
+                                     &attr,
+                                     des_access,
+                                     pol,
+                                     result);
 }
 
 /** Open a LSA policy handle
@@ -82,130 +140,169 @@ NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
 
 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
                                 TALLOC_CTX *mem_ctx, bool sec_qos,
-                                uint32 des_access, POLICY_HND *pol)
+                                uint32 des_access, struct policy_handle *pol)
 {
-       struct lsa_ObjectAttribute attr;
-       struct lsa_QosInfo qos;
-       char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
+       NTSTATUS status;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 
-       if (sec_qos) {
-               init_lsa_sec_qos(&qos, 0xc, 2, 1, 0);
-               init_lsa_obj_attr(&attr,
-                                 0x18,
-                                 NULL,
-                                 NULL,
-                                 0,
-                                 NULL,
-                                 &qos);
-       } else {
-               init_lsa_obj_attr(&attr,
-                                 0x18,
-                                 NULL,
-                                 NULL,
-                                 0,
-                                 NULL,
-                                 NULL);
+       status = dcerpc_lsa_open_policy2(cli->binding_handle,
+                                        mem_ctx,
+                                        cli->srv_name_slash,
+                                        sec_qos,
+                                        des_access,
+                                        pol,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
-       return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
-                                     srv_name_slash,
-                                     &attr,
-                                     des_access,
-                                     pol);
+       return result;
 }
 
 /* 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. */
+ * this assumes sufficiently sized arrays to store domains, names and types. */
 
-static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
+static NTSTATUS dcerpc_lsa_lookup_sids_noalloc(struct dcerpc_binding_handle *h,
                                               TALLOC_CTX *mem_ctx,
-                                              POLICY_HND *pol,
+                                              struct policy_handle *pol,
                                               int num_sids,
-                                              const DOM_SID *sids,
+                                              const struct dom_sid *sids,
                                               char **domains,
                                               char **names,
-                                              enum lsa_SidType *types)
+                                              enum lsa_SidType *types,
+                                              bool use_lookupsids3,
+                                              NTSTATUS *presult)
 {
-       prs_struct qbuf, rbuf;
-       LSA_Q_LOOKUP_SIDS q;
-       LSA_R_LOOKUP_SIDS r;
-       DOM_R_REF ref;
-       NTSTATUS result = NT_STATUS_OK;
-       TALLOC_CTX *tmp_ctx = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       struct lsa_SidArray sid_array;
+       struct lsa_RefDomainList *ref_domains = NULL;
+       struct lsa_TransNameArray lsa_names;
+       enum lsa_LookupNamesLevel level = LSA_LOOKUP_NAMES_ALL;
+       uint32_t count = 0;
        int i;
 
-       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;
+       ZERO_STRUCT(lsa_names);
+
+       sid_array.num_sids = num_sids;
+       sid_array.sids = talloc_array(mem_ctx, struct lsa_SidPtr, num_sids);
+       if (sid_array.sids == NULL) {
+               return NT_STATUS_NO_MEMORY;
        }
 
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
+       for (i = 0; i<num_sids; i++) {
+               sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[i]);
+               if (!sid_array.sids[i].sid) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
 
-       init_q_lookup_sids(tmp_ctx, &q, pol, num_sids, sids, 1);
+       if (use_lookupsids3) {
+               struct lsa_TransNameArray2 lsa_names2;
+               uint32_t n;
+
+               ZERO_STRUCT(lsa_names2);
+
+               status = dcerpc_lsa_LookupSids3(h,
+                                               mem_ctx,
+                                               &sid_array,
+                                               &ref_domains,
+                                               &lsa_names2,
+                                               level,
+                                               &count,
+                                               LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES,
+                                               LSA_CLIENT_REVISION_2,
+                                               &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
 
-       ZERO_STRUCT(ref);
+               if(!NT_STATUS_IS_ERR(result)) {
+                       lsa_names.count = lsa_names2.count;
+                       lsa_names.names = talloc_array(mem_ctx,
+                                                      struct lsa_TranslatedName,
+                                                      lsa_names.count);
+                       if (lsa_names.names == NULL) {
+                               return NT_STATUS_NO_MEMORY;
+                       }
+                       for (n=0; n < lsa_names.count; n++) {
+                               lsa_names.names[n].sid_type     = lsa_names2.names[n].sid_type;
+                               lsa_names.names[n].name         = lsa_names2.names[n].name;
+                               lsa_names.names[n].sid_index    = lsa_names2.names[n].sid_index;
+                       }
+               }
 
-       r.dom_ref = &ref;
+       } else {
+               status = dcerpc_lsa_LookupSids(h,
+                                              mem_ctx,
+                                              pol,
+                                              &sid_array,
+                                              &ref_domains,
+                                              &lsa_names,
+                                              level,
+                                              &count,
+                                              &result);
+       }
 
-       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 );
+       DEBUG(10, ("LSA_LOOKUPSIDS returned status: '%s', result: '%s', "
+                  "mapped count = %d'\n",
+                  nt_errstr(status), nt_errstr(result), count));
 
-       DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
-                  nt_errstr(r.status), r.mapped_count));
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
-       if (!NT_STATUS_IS_OK(r.status) &&
-           !NT_STATUS_EQUAL(r.status, NT_STATUS_NONE_MAPPED) &&
-           !NT_STATUS_EQUAL(r.status, STATUS_SOME_UNMAPPED))
+       if (!NT_STATUS_IS_OK(result) &&
+           !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
+           !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
        {
-               /* An actual error occured */
-               result = r.status;
-               goto done;
+               *presult = result;
+               return status;
        }
 
        /* Return output parameters */
-
-       if (NT_STATUS_EQUAL(r.status, NT_STATUS_NONE_MAPPED) ||
-           (r.mapped_count == 0))
+       if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_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;
+               *presult = NT_STATUS_NONE_MAPPED;
+               return status;
        }
 
        for (i = 0; i < num_sids; i++) {
-               fstring name, dom_name;
-               uint32 dom_idx = r.names.name[i].domain_idx;
+               const char *name, *dom_name;
+               uint32_t dom_idx = lsa_names.names[i].sid_index;
 
                /* Translate optimised name through domain index array */
 
                if (dom_idx != 0xffffffff) {
 
-                       rpcstr_pull_unistr2_fstring(
-                                dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
-                       rpcstr_pull_unistr2_fstring(
-                                name, &r.names.uni_name[i]);
-
-                       (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)) {
+                       dom_name = ref_domains->domains[dom_idx].name.string;
+                       name = lsa_names.names[i].name.string;
+
+                       if (name) {
+                               (names)[i] = talloc_strdup(names, name);
+                               if ((names)[i] == NULL) {
+                                       DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
+                                       *presult = NT_STATUS_UNSUCCESSFUL;
+                                       return status;
+                               }
+                       } else {
+                               (names)[i] = NULL;
+                       }
+                       domains[i] = talloc_strdup(domains,
+                                                  dom_name ? dom_name : "");
+                       (types)[i] = lsa_names.names[i].sid_type;
+                       if (((domains)[i] == NULL)) {
                                DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
-                               result = NT_STATUS_UNSUCCESSFUL;
-                               goto done;
+                               *presult = NT_STATUS_UNSUCCESSFUL;
+                               return status;
                        }
 
                } else {
@@ -215,9 +312,8 @@ static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
                }
        }
 
-done:
-       TALLOC_FREE(tmp_ctx);
-       return result;
+       *presult = NT_STATUS_OK;
+       return status;
 }
 
 /* Lookup a list of sids
@@ -232,55 +328,59 @@ done:
  * 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)
+static NTSTATUS dcerpc_lsa_lookup_sids_generic(struct dcerpc_binding_handle *h,
+                                              TALLOC_CTX *mem_ctx,
+                                              struct policy_handle *pol,
+                                              int num_sids,
+                                              const struct dom_sid *sids,
+                                              char ***pdomains,
+                                              char ***pnames,
+                                              enum lsa_SidType **ptypes,
+                                              bool use_lookupsids3,
+                                              NTSTATUS *presult)
 {
+       NTSTATUS status = NT_STATUS_OK;
        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;
+       const struct dom_sid *hunk_sids = sids;
+       char **hunk_domains;
+       char **hunk_names;
+       enum lsa_SidType *hunk_types;
+       char **domains = NULL;
+       char **names = NULL;
+       enum lsa_SidType *types = NULL;
+       bool have_mapped = false;
+       bool have_unmapped = false;
 
        if (num_sids) {
-               if (!((*domains) = TALLOC_ARRAY(mem_ctx, char *, 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;
+                       status = NT_STATUS_NO_MEMORY;
                        goto fail;
                }
 
-               if (!((*names) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
+               if (!(names = talloc_array(mem_ctx, char *, num_sids))) {
                        DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
-                       result = NT_STATUS_NO_MEMORY;
+                       status = NT_STATUS_NO_MEMORY;
                        goto fail;
                }
 
-               if (!((*types) = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
+               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;
+                       status = 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;
+       hunk_domains = domains;
+       hunk_names = names;
+       hunk_types = types;
 
        while (sids_left > 0) {
                int hunk_num_sids;
-               NTSTATUS hunk_result = NT_STATUS_OK;
+               NTSTATUS hunk_result = NT_STATUS_UNSUCCESSFUL;
 
                hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
                                ? LOOKUP_SIDS_HUNK_SIZE
@@ -292,32 +392,44 @@ NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
                           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);
+               status = dcerpc_lsa_lookup_sids_noalloc(h,
+                                                       mem_ctx,
+                                                       pol,
+                                                       hunk_num_sids,
+                                                       hunk_sids,
+                                                       hunk_domains,
+                                                       hunk_names,
+                                                       hunk_types,
+                                                       use_lookupsids3,
+                                                       &hunk_result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto fail;
+               }
 
                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;
+                       *presult = 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;
+               if (NT_STATUS_IS_OK(hunk_result)) {
+                       have_mapped = true;
+               }
+               if (NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)) {
+                       have_unmapped = true;
+               }
+               if (NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED)) {
+                       int i;
+                       for (i=0; i<hunk_num_sids; i++) {
+                               if (hunk_types[i] == SID_NAME_UNKNOWN) {
+                                       have_unmapped = true;
+                               } else {
+                                       have_mapped = true;
+                               }
+                       }
                }
 
                sids_left -= hunk_num_sids;
@@ -328,83 +440,222 @@ NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
                hunk_types += hunk_num_sids;
        }
 
-       return result;
+       *pdomains = domains;
+       *pnames = names;
+       *ptypes = types;
+
+       if (!have_mapped) {
+               result = NT_STATUS_NONE_MAPPED;
+       }
+       if (have_unmapped) {
+               result = STATUS_SOME_UNMAPPED;
+       }
+       *presult = result;
+
+       return status;
 
 fail:
-       TALLOC_FREE(*domains);
-       TALLOC_FREE(*names);
-       TALLOC_FREE(*types);
-       return result;
+       TALLOC_FREE(domains);
+       TALLOC_FREE(names);
+       TALLOC_FREE(types);
+
+       return status;
 }
 
-/** Lookup a list of names */
+NTSTATUS dcerpc_lsa_lookup_sids(struct dcerpc_binding_handle *h,
+                               TALLOC_CTX *mem_ctx,
+                               struct policy_handle *pol,
+                               int num_sids,
+                               const struct dom_sid *sids,
+                               char ***pdomains,
+                               char ***pnames,
+                               enum lsa_SidType **ptypes,
+                               NTSTATUS *result)
+{
+       return dcerpc_lsa_lookup_sids_generic(h,
+                                             mem_ctx,
+                                             pol,
+                                             num_sids,
+                                             sids,
+                                             pdomains,
+                                             pnames,
+                                             ptypes,
+                                             false,
+                                             result);
+}
 
-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)
+NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
+                               TALLOC_CTX *mem_ctx,
+                               struct policy_handle *pol,
+                               int num_sids,
+                               const struct dom_sid *sids,
+                               char ***pdomains,
+                               char ***pnames,
+                               enum lsa_SidType **ptypes)
 {
-       prs_struct qbuf, rbuf;
-       LSA_Q_LOOKUP_NAMES q;
-       LSA_R_LOOKUP_NAMES r;
-       DOM_R_REF ref;
-       NTSTATUS result;
-       int i;
+       NTSTATUS status;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+
+       status = dcerpc_lsa_lookup_sids_generic(cli->binding_handle,
+                                               mem_ctx,
+                                               pol,
+                                               num_sids,
+                                               sids,
+                                               pdomains,
+                                               pnames,
+                                               ptypes,
+                                               false,
+                                               &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
+       return result;
+}
 
-       ZERO_STRUCT(ref);
-       r.dom_ref = &ref;
+NTSTATUS dcerpc_lsa_lookup_sids3(struct dcerpc_binding_handle *h,
+                                TALLOC_CTX *mem_ctx,
+                                struct policy_handle *pol,
+                                int num_sids,
+                                const struct dom_sid *sids,
+                                char ***pdomains,
+                                char ***pnames,
+                                enum lsa_SidType **ptypes,
+                                NTSTATUS *result)
+{
+       return dcerpc_lsa_lookup_sids_generic(h,
+                                             mem_ctx,
+                                             pol,
+                                             num_sids,
+                                             sids,
+                                             pdomains,
+                                             pnames,
+                                             ptypes,
+                                             true,
+                                             result);
+}
 
-       init_q_lookup_names(mem_ctx, &q, pol, num_names, names, level);
+NTSTATUS rpccli_lsa_lookup_sids3(struct rpc_pipe_client *cli,
+                                TALLOC_CTX *mem_ctx,
+                                struct policy_handle *pol,
+                                int num_sids,
+                                const struct dom_sid *sids,
+                                char ***pdomains,
+                                char ***pnames,
+                                enum lsa_SidType **ptypes)
+{
+       NTSTATUS status;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+
+       status = dcerpc_lsa_lookup_sids_generic(cli->binding_handle,
+                                               mem_ctx,
+                                               pol,
+                                               num_sids,
+                                               sids,
+                                               pdomains,
+                                               pnames,
+                                               ptypes,
+                                               true,
+                                               &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
-       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);
+       return result;
+}
+
+/** Lookup a list of names */
 
-       result = r.status;
+static NTSTATUS dcerpc_lsa_lookup_names_generic(struct dcerpc_binding_handle *h,
+                                               TALLOC_CTX *mem_ctx,
+                                               struct policy_handle *pol,
+                                               uint32_t num_names,
+                                               const char **names,
+                                               const char ***dom_names,
+                                               enum lsa_LookupNamesLevel level,
+                                               struct dom_sid **sids,
+                                               enum lsa_SidType **types,
+                                               bool use_lookupnames4,
+                                               NTSTATUS *presult)
+{
+       NTSTATUS status;
+       struct lsa_String *lsa_names = NULL;
+       struct lsa_RefDomainList *domains = NULL;
+       struct lsa_TransSidArray sid_array;
+       struct lsa_TransSidArray3 sid_array3;
+       uint32_t count = 0;
+       uint32_t i;
+
+       ZERO_STRUCT(sid_array);
+       ZERO_STRUCT(sid_array3);
+
+       lsa_names = talloc_array(mem_ctx, struct lsa_String, num_names);
+       if (lsa_names == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
-       if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
-           NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
+       for (i = 0; i < num_names; i++) {
+               init_lsa_String(&lsa_names[i], names[i]);
+       }
+
+       if (use_lookupnames4) {
+               status = dcerpc_lsa_LookupNames4(h,
+                                                mem_ctx,
+                                                num_names,
+                                                lsa_names,
+                                                &domains,
+                                                &sid_array3,
+                                                level,
+                                                &count,
+                                                LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES,
+                                                LSA_CLIENT_REVISION_2,
+                                                presult);
+       } else {
+               status = dcerpc_lsa_LookupNames(h,
+                                               mem_ctx,
+                                               pol,
+                                               num_names,
+                                               lsa_names,
+                                               &domains,
+                                               &sid_array,
+                                               level,
+                                               &count,
+                                               presult);
+       }
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
 
+       if (!NT_STATUS_IS_OK(*presult) &&
+           !NT_STATUS_EQUAL(*presult, STATUS_SOME_UNMAPPED)) {
                /* An actual error occured */
-
                goto done;
        }
 
        /* Return output parameters */
-
-       if (r.mapped_count == 0) {
-               result = NT_STATUS_NONE_MAPPED;
+       if (count == 0) {
+               *presult = NT_STATUS_NONE_MAPPED;
                goto done;
        }
 
        if (num_names) {
-               if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
+               if (!((*sids = talloc_array(mem_ctx, struct dom_sid, num_names)))) {
                        DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
-                       result = NT_STATUS_NO_MEMORY;
+                       *presult = NT_STATUS_NO_MEMORY;
                        goto done;
                }
 
-               if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
+               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;
+                       *presult = NT_STATUS_NO_MEMORY;
                        goto done;
                }
 
                if (dom_names != NULL) {
-                       *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
+                       *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;
+                               *presult = NT_STATUS_NO_MEMORY;
                                goto done;
                        }
                }
@@ -417,10 +668,16 @@ NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
        }
 
        for (i = 0; i < num_names; i++) {
-               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];
+               uint32_t dom_idx;
+               struct dom_sid *sid = &(*sids)[i];
+
+               if (use_lookupnames4) {
+                       dom_idx         = sid_array3.sids[i].sid_index;
+                       (*types)[i]     = sid_array3.sids[i].sid_type;
+               } else {
+                       dom_idx         = sid_array.sids[i].sid_index;
+                       (*types)[i]     = sid_array.sids[i].sid_type;
+               }
 
                /* Translate optimised sid through domain index array */
 
@@ -431,418 +688,131 @@ NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
                        continue;
                }
 
-               sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
+               if (use_lookupnames4) {
+                       sid_copy(sid, sid_array3.sids[i].sid);
+               } else {
+                       sid_copy(sid, domains->domains[dom_idx].sid);
 
-               if (dom_rid != 0xffffffff) {
-                       sid_append_rid(sid, dom_rid);
+                       if (sid_array.sids[i].rid != 0xffffffff) {
+                               sid_append_rid(sid, sid_array.sids[i].rid);
+                       }
                }
 
-               (*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);
+               (*dom_names)[i] = domains->domains[dom_idx].name.string;
        }
 
  done:
-
-       return result;
+       return status;
 }
 
-/** Enumerate list of SIDs  */
-
-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;
-       LSA_Q_ENUM_ACCOUNTS q;
-       LSA_R_ENUM_ACCOUNTS r;
-       NTSTATUS result;
-       int i;
-
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
-
-        init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
-
-       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)) {
-               goto done;
-       }
-
-       if (r.sids.num_entries==0)
-               goto done;
-
-       /* Return output parameters */
-
-       *sids = TALLOC_ARRAY(mem_ctx, DOM_SID, r.sids.num_entries);
-       if (!*sids) {
-               DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Copy across names and sids */
-
-       for (i = 0; i < r.sids.num_entries; i++) {
-               sid_copy(&(*sids)[i], &r.sids.sid[i].sid);
-       }
-
-       *num_sids= r.sids.num_entries;
-       *enum_ctx = r.enum_context;
-
- done:
-
-       return result;
-}
-
-/** Enumerate user privileges
- *
- * @param cli Handle on an initialised SMB connection */
-
-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;
-       LSA_Q_ENUMPRIVSACCOUNT q;
-       LSA_R_ENUMPRIVSACCOUNT r;
-       NTSTATUS result;
-       int i;
-
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
-
-       /* Initialise input parameters */
-
-       init_lsa_q_enum_privsaccount(&q, pol);
-
-       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 */
-
-       result = r.status;
-
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
-
-       if (r.count == 0)
-               goto done;
-
-       if (!((*set = TALLOC_ARRAY(mem_ctx, LUID_ATTR, r.count)))) {
-               DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       for (i=0; i<r.count; i++) {
-               (*set)[i].luid.low = r.set.set[i].luid.low;
-               (*set)[i].luid.high = r.set.set[i].luid.high;
-               (*set)[i].attr = r.set.set[i].attr;
-       }
-
-       *count=r.count;
- done:
-
-       return result;
-}
-
-/** Get a privilege value given its name */
-
-NTSTATUS rpccli_lsa_lookup_priv_value(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
-                                POLICY_HND *pol, const char *name, LUID *luid)
+NTSTATUS dcerpc_lsa_lookup_names(struct dcerpc_binding_handle *h,
+                                TALLOC_CTX *mem_ctx,
+                                struct policy_handle *pol,
+                                uint32_t num_names,
+                                const char **names,
+                                const char ***dom_names,
+                                enum lsa_LookupNamesLevel level,
+                                struct dom_sid **sids,
+                                enum lsa_SidType **types,
+                                NTSTATUS *result)
 {
-       prs_struct qbuf, rbuf;
-       LSA_Q_LOOKUP_PRIV_VALUE q;
-       LSA_R_LOOKUP_PRIV_VALUE r;
-       NTSTATUS result;
-
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
-
-       /* Marshall data and send request */
-
-       init_lsa_q_lookup_priv_value(&q, pol, name);
-
-       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);
-
-       result = r.status;
-
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
-
-       /* Return output parameters */
-
-       (*luid).low=r.luid.low;
-       (*luid).high=r.luid.high;
-
- done:
-
-       return result;
+       return dcerpc_lsa_lookup_names_generic(h,
+                                              mem_ctx,
+                                              pol,
+                                              num_names,
+                                              names,
+                                              dom_names,
+                                              level,
+                                              sids,
+                                              types,
+                                              false,
+                                              result);
 }
 
-/* Enumerate account rights This is similar to enum_privileges but
-   takes a SID directly, avoiding the open_account call.
-*/
-
-NTSTATUS rpccli_lsa_enum_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
-                                    POLICY_HND *pol, DOM_SID *sid,
-                                    uint32 *count, char ***priv_names)
+NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
+                                TALLOC_CTX *mem_ctx,
+                                struct policy_handle *pol,
+                                int num_names,
+                                const char **names,
+                                const char ***dom_names,
+                                int level,
+                                struct dom_sid **sids,
+                                enum lsa_SidType **types)
 {
-       prs_struct qbuf, rbuf;
-       LSA_Q_ENUM_ACCT_RIGHTS q;
-       LSA_R_ENUM_ACCT_RIGHTS r;
-       NTSTATUS result;
-       int i;
-       fstring *privileges;
-       char **names;
-
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
-
-       /* Marshall data and send request */
-       init_q_enum_acct_rights(&q, pol, 2, sid);
-
-       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);
-
-       result = r.status;
-
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
-
-       *count = r.count;
-       if (! *count) {
-               goto done;
-       }
-
-
-       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++ ) {
-               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 */
-               names[i] = talloc_strdup( mem_ctx, privileges[i] );
+       NTSTATUS status;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+
+       status = dcerpc_lsa_lookup_names(cli->binding_handle,
+                                        mem_ctx,
+                                        pol,
+                                        num_names,
+                                        names,
+                                        dom_names,
+                                        level,
+                                        sids,
+                                        types,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
-       *priv_names = names;
-
-done:
-
        return result;
 }
 
-
-
-/* add account rights to an account. */
-
-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)
+NTSTATUS dcerpc_lsa_lookup_names4(struct dcerpc_binding_handle *h,
+                                 TALLOC_CTX *mem_ctx,
+                                 struct policy_handle *pol,
+                                 uint32_t num_names,
+                                 const char **names,
+                                 const char ***dom_names,
+                                 enum lsa_LookupNamesLevel level,
+                                 struct dom_sid **sids,
+                                 enum lsa_SidType **types,
+                                 NTSTATUS *result)
 {
-       prs_struct qbuf, rbuf;
-       LSA_Q_ADD_ACCT_RIGHTS q;
-       LSA_R_ADD_ACCT_RIGHTS r;
-       NTSTATUS result;
-
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
-
-       /* Marshall data and send request */
-       init_q_add_acct_rights(&q, pol, &sid, count, privs_name);
-
-       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);
-
-       result = r.status;
-
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
-done:
-
-       return result;
+       return dcerpc_lsa_lookup_names_generic(h,
+                                              mem_ctx,
+                                              pol,
+                                              num_names,
+                                              names,
+                                              dom_names,
+                                              level,
+                                              sids,
+                                              types,
+                                              true,
+                                              result);
 }
 
-
-/* remove account rights for an account. */
-
-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)
+NTSTATUS rpccli_lsa_lookup_names4(struct rpc_pipe_client *cli,
+                                 TALLOC_CTX *mem_ctx,
+                                 struct policy_handle *pol,
+                                 int num_names,
+                                 const char **names,
+                                 const char ***dom_names,
+                                 int level,
+                                 struct dom_sid **sids,
+                                 enum lsa_SidType **types)
 {
-       prs_struct qbuf, rbuf;
-       LSA_Q_REMOVE_ACCT_RIGHTS q;
-       LSA_R_REMOVE_ACCT_RIGHTS r;
-       NTSTATUS result;
-
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
-
-       /* Marshall data and send request */
-       init_q_remove_acct_rights(&q, pol, &sid, removeall?1:0, count, privs_name);
-
-       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);
-
-       result = r.status;
-
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
+       NTSTATUS status;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+
+       status = dcerpc_lsa_lookup_names4(cli->binding_handle,
+                                         mem_ctx,
+                                         pol,
+                                         num_names,
+                                         names,
+                                         dom_names,
+                                         level,
+                                         sids,
+                                         types,
+                                         &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
-done:
 
        return result;
 }
-
-
-#if 0
-
-/** 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)
-{
-       struct cli_state cli;
-       NTSTATUS result;
-       POLICY_HND lsa_pol;
-       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",
-                       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));
-               goto done;
-       }
-
-       if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
-               DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
-Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
-               goto done;
-       }
-
-       /* 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:
-
-       cli_shutdown(&cli);
-       return ret;
-}
-
-#endif