r16945: Sync trunk -> 3.0 for 3.0.24 code. Still need
[samba.git] / source3 / rpc_server / srv_lsa_nt.c
index 78461008a8be2e10cd44dd0079ebcf63e4bbde2d..2cc5ef6110492ed592d1c0cf42ae3f89b0c4a4f0 100644 (file)
@@ -4,7 +4,7 @@
  *  Copyright (C) Andrew Tridgell              1992-1997,
  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
  *  Copyright (C) Paul Ashton                       1997,
- *  Copyright (C) Jeremy Allison                    2001,
+ *  Copyright (C) Jeremy Allison                    2001, 2006.
  *  Copyright (C) Rafal Szczesniak                  2002,
  *  Copyright (C) Jim McDonough <jmcd@us.ibm.com>   2002,
  *  Copyright (C) Simo Sorce                        2003.
@@ -62,7 +62,7 @@ static void free_lsa_info(void *ptr)
 Init dom_query
  ***************************************************************************/
 
-static void init_dom_query(DOM_QUERY *d_q, const char *dom_name, DOM_SID *dom_sid)
+static void init_dom_query_3(DOM_QUERY_3 *d_q, const char *dom_name, DOM_SID *dom_sid)
 {
        d_q->buffer_dom_name = (dom_name != NULL) ? 1 : 0; /* domain buffer pointer */
        d_q->buffer_dom_sid = (dom_sid != NULL) ? 1 : 0;  /* domain sid pointer */
@@ -93,6 +93,15 @@ static void init_dom_query(DOM_QUERY *d_q, const char *dom_name, DOM_SID *dom_si
                init_dom_sid2(&d_q->dom_sid, dom_sid);
 }
 
+/***************************************************************************
+Init dom_query
+ ***************************************************************************/
+
+static void init_dom_query_5(DOM_QUERY_5 *d_q, const char *dom_name, DOM_SID *dom_sid)
+{
+       init_dom_query_3(d_q, dom_name, dom_sid);
+}
+
 /***************************************************************************
  init_dom_ref - adds a domain if it's not already in, returns the index.
 ***************************************************************************/
@@ -120,7 +129,7 @@ static int init_dom_ref(DOM_R_REF *ref, const char *dom_name, DOM_SID *dom_sid)
        ref->max_entries = MAX_REF_DOMAINS;
        ref->num_ref_doms_2 = num+1;
 
-       ref->hdr_ref_dom[num].ptr_dom_sid = dom_sid != NULL ? 1 : 0;
+       ref->hdr_ref_dom[num].ptr_dom_sid = 1; /* dom sid cannot be NULL. */
 
        init_unistr2(&ref->ref_dom[num].uni_dom_name, dom_name, UNI_FLAGS_NONE);
        init_uni_hdr(&ref->hdr_ref_dom[num].hdr_dom_name, &ref->ref_dom[num].uni_dom_name);
@@ -131,21 +140,23 @@ static int init_dom_ref(DOM_R_REF *ref, const char *dom_name, DOM_SID *dom_sid)
 }
 
 /***************************************************************************
- init_lsa_rids
+ lookup_lsa_rids. Must be called as root for lookup_name to work.
  ***************************************************************************/
 
-static int init_lsa_rids(TALLOC_CTX *mem_ctx,
-                         DOM_R_REF *ref, DOM_RID *prid,
-                         int num_entries, UNISTR2 *name,
-                         int flags)
+static NTSTATUS lookup_lsa_rids(TALLOC_CTX *mem_ctx,
+                       DOM_R_REF *ref,
+                       DOM_RID *prid,
+                       uint32 num_entries,
+                       const UNISTR2 *name,
+                       int flags,
+                       uint32 *pmapped_count)
 {
-       int mapped_count, i;
+       uint32 mapped_count, i;
 
        SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
 
        mapped_count = 0;
-
-       become_root(); /* lookup_name can require root privs */
+       *pmapped_count = 0;
 
        for (i = 0; i < num_entries; i++) {
                DOM_SID sid;
@@ -160,10 +171,10 @@ static int init_lsa_rids(TALLOC_CTX *mem_ctx,
                full_name = rpcstr_pull_unistr2_talloc(mem_ctx, &name[i]);
                if (full_name == NULL) {
                        DEBUG(0, ("pull_ucs2_talloc failed\n"));
-                       return 0;
+                       return NT_STATUS_NO_MEMORY;
                }
 
-               DEBUG(5, ("init_lsa_rids: looking up name %s\n", full_name));
+               DEBUG(5, ("lookup_lsa_rids: looking up name %s\n", full_name));
 
                /* We can ignore the result of lookup_name, it will not touch
                   "type" if it's not successful */
@@ -171,7 +182,6 @@ static int init_lsa_rids(TALLOC_CTX *mem_ctx,
                lookup_name(mem_ctx, full_name, flags, &domain, NULL,
                            &sid, &type);
 
-
                switch (type) {
                case SID_NAME_USER:
                case SID_NAME_DOM_GRP:
@@ -200,9 +210,92 @@ static int init_lsa_rids(TALLOC_CTX *mem_ctx,
                init_dom_rid(&prid[i], rid, type, dom_idx);
        }
 
-       unbecome_root();
+       *pmapped_count = mapped_count;
+       return NT_STATUS_OK;
+}
+
+/***************************************************************************
+ lookup_lsa_sids. Must be called as root for lookup_name to work.
+ ***************************************************************************/
+
+static NTSTATUS lookup_lsa_sids(TALLOC_CTX *mem_ctx,
+                       DOM_R_REF *ref,
+                       LSA_TRANSLATED_SID3 *trans_sids,
+                       uint32 num_entries,
+                       const UNISTR2 *name,
+                       int flags,
+                       uint32 *pmapped_count)
+{
+       uint32 mapped_count, i;
+
+       SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
+
+       mapped_count = 0;
+       *pmapped_count = 0;
+
+       for (i = 0; i < num_entries; i++) {
+               DOM_SID sid;
+               uint32 rid;
+               int dom_idx;
+               char *full_name;
+               const char *domain;
+               enum SID_NAME_USE type = SID_NAME_UNKNOWN;
+
+               /* Split name into domain and user component */
+
+               full_name = rpcstr_pull_unistr2_talloc(mem_ctx, &name[i]);
+               if (full_name == NULL) {
+                       DEBUG(0, ("pull_ucs2_talloc failed\n"));
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               DEBUG(5, ("init_lsa_sids: looking up name %s\n", full_name));
+
+               /* We can ignore the result of lookup_name, it will not touch
+                  "type" if it's not successful */
+
+               lookup_name(mem_ctx, full_name, flags, &domain, NULL,
+                           &sid, &type);
+
+               switch (type) {
+               case SID_NAME_USER:
+               case SID_NAME_DOM_GRP:
+               case SID_NAME_DOMAIN:
+               case SID_NAME_ALIAS:
+               case SID_NAME_WKN_GRP:
+                       DEBUG(5, ("init_lsa_sids: %s found\n", full_name));
+                       /* Leave these unchanged */
+                       break;
+               default:
+                       /* Don't hand out anything but the list above */
+                       DEBUG(5, ("init_lsa_sids: %s not found\n", full_name));
+                       type = SID_NAME_UNKNOWN;
+                       break;
+               }
+
+               rid = 0;
+               dom_idx = -1;
+
+               if (type != SID_NAME_UNKNOWN) {
+                       DOM_SID domain_sid;
+                       sid_copy(&domain_sid, &sid);
+                       sid_split_rid(&domain_sid, &rid);
+                       dom_idx = init_dom_ref(ref, domain, &domain_sid);
+                       mapped_count++;
+               }
+
+               /* Initialize the LSA_TRANSLATED_SID3 return. */
+               trans_sids[i].sid_type = type;
+               trans_sids[i].sid2 = TALLOC_P(mem_ctx, DOM_SID2);
+               if (trans_sids[i].sid2 == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               init_dom_sid2(trans_sids[i].sid2, &sid);
+               trans_sids[i].sid_idx = dom_idx;
+       }
 
-       return mapped_count;
+       *pmapped_count = mapped_count;
+       return NT_STATUS_OK;
 }
 
 /***************************************************************************
@@ -224,6 +317,63 @@ static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
        r_l->mapped_count = mapped_count;
 }
 
+/***************************************************************************
+ init_reply_lookup_names2
+ ***************************************************************************/
+
+static void init_reply_lookup_names2(LSA_R_LOOKUP_NAMES2 *r_l,
+                DOM_R_REF *ref, uint32 num_entries,
+                DOM_RID2 *rid, uint32 mapped_count)
+{
+       r_l->ptr_dom_ref  = 1;
+       r_l->dom_ref      = ref;
+
+       r_l->num_entries  = num_entries;
+       r_l->ptr_entries  = 1;
+       r_l->num_entries2 = num_entries;
+       r_l->dom_rid      = rid;
+
+       r_l->mapped_count = mapped_count;
+}
+
+/***************************************************************************
+ init_reply_lookup_names3
+ ***************************************************************************/
+
+static void init_reply_lookup_names3(LSA_R_LOOKUP_NAMES3 *r_l,
+                DOM_R_REF *ref, uint32 num_entries,
+                LSA_TRANSLATED_SID3 *trans_sids, uint32 mapped_count)
+{
+       r_l->ptr_dom_ref  = 1;
+       r_l->dom_ref      = ref;
+
+       r_l->num_entries  = num_entries;
+       r_l->ptr_entries  = 1;
+       r_l->num_entries2 = num_entries;
+       r_l->trans_sids   = trans_sids;
+
+       r_l->mapped_count = mapped_count;
+}
+
+/***************************************************************************
+ init_reply_lookup_names4
+ ***************************************************************************/
+
+static void init_reply_lookup_names4(LSA_R_LOOKUP_NAMES4 *r_l,
+                DOM_R_REF *ref, uint32 num_entries,
+                LSA_TRANSLATED_SID3 *trans_sids, uint32 mapped_count)
+{
+       r_l->ptr_dom_ref  = 1;
+       r_l->dom_ref      = ref;
+
+       r_l->num_entries  = num_entries;
+       r_l->ptr_entries  = 1;
+       r_l->num_entries2 = num_entries;
+       r_l->trans_sids   = trans_sids;
+
+       r_l->mapped_count = mapped_count;
+}
+
 /***************************************************************************
  Init_reply_lookup_sids.
  ***************************************************************************/
@@ -537,7 +687,7 @@ NTSTATUS _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u,
 NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
 {
        struct lsa_info *handle;
-       LSA_INFO_UNION *info = &r_u->dom;
+       LSA_INFO_CTR *ctr = &r_u->ctr;
        DOM_SID domain_sid;
        const char *name;
        DOM_SID *sid = NULL;
@@ -550,19 +700,31 @@ NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INF
        switch (q_u->info_class) {
        case 0x02:
                {
-               unsigned int i;
+
+               uint32 policy_def = LSA_AUDIT_POLICY_ALL;
+               
                /* check if the user have enough rights */
-               if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION))
+               if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION)) {
+                       DEBUG(10,("_lsa_query_info: insufficient access rights\n"));
                        return NT_STATUS_ACCESS_DENIED;
+               }
 
                /* fake info: We audit everything. ;) */
-               info->id2.auditing_enabled = 1;
-               info->id2.count1 = 7;
-               info->id2.count2 = 7;
-               if ((info->id2.auditsettings = TALLOC_ARRAY(p->mem_ctx,uint32, 7)) == NULL)
+               ctr->info.id2.ptr = 1;
+               ctr->info.id2.auditing_enabled = True;
+               ctr->info.id2.count1 = ctr->info.id2.count2 = LSA_AUDIT_NUM_CATEGORIES;
+
+               if ((ctr->info.id2.auditsettings = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, LSA_AUDIT_NUM_CATEGORIES)) == NULL)
                        return NT_STATUS_NO_MEMORY;
-               for (i = 0; i < 7; i++)
-                       info->id2.auditsettings[i] = 3;
+
+               ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_ACCOUNT_MANAGEMENT] = policy_def;
+               ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_FILE_AND_OBJECT_ACCESS] = policy_def; 
+               ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_LOGON] = policy_def; 
+               ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_PROCCESS_TRACKING] = policy_def; 
+               ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_SECURITY_POLICY_CHANGES] = policy_def;
+               ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_SYSTEM] = policy_def;
+               ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_USE_OF_USER_RIGHTS] = policy_def; 
+
                break;
                }
        case 0x03:
@@ -592,7 +754,7 @@ NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INF
                        default:
                                return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
                }
-               init_dom_query(&r_u->dom.id3, name, sid);
+               init_dom_query_3(&r_u->ctr.info.id3, name, sid);
                break;
        case 0x05:
                /* check if the user have enough rights */
@@ -602,7 +764,7 @@ NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INF
                /* Request PolicyAccountDomainInformation. */
                name = get_global_sam_name();
                sid = get_global_sam_sid();
-               init_dom_query(&r_u->dom.id5, name, sid);
+               init_dom_query_5(&r_u->ctr.info.id5, name, sid);
                break;
        case 0x06:
                /* check if the user have enough rights */
@@ -615,14 +777,14 @@ NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INF
                                 * only a BDC is a backup controller
                                 * of the domain, it controls.
                                 */
-                               info->id6.server_role = 2;
+                               ctr->info.id6.server_role = 2;
                                break;
                        default:
                                /*
                                 * any other role is a primary
                                 * of the domain, it controls.
                                 */
-                               info->id6.server_role = 3;
+                               ctr->info.id6.server_role = 3;
                                break; 
                }
                break;
@@ -633,8 +795,8 @@ NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INF
        }
 
        if (NT_STATUS_IS_OK(r_u->status)) {
-               r_u->undoc_buffer = 0x22000000; /* bizarre */
-               r_u->info_class = q_u->info_class;
+               r_u->dom_ptr = 0x22000000; /* bizarre */
+               ctr->info_class = q_u->info_class;
        }
 
        return r_u->status;
@@ -889,7 +1051,7 @@ NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP
 {
        struct lsa_info *handle;
        UNISTR2 *names = q_u->uni_name;
-       int num_entries = q_u->num_entries;
+       uint32 num_entries = q_u->num_entries;
        DOM_R_REF *ref;
        DOM_RID *rids;
        uint32 mapped_count = 0;
@@ -923,18 +1085,212 @@ NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP
                return NT_STATUS_NO_MEMORY;
 
        /* set up the LSA Lookup RIDs response */
-       mapped_count = init_lsa_rids(p->mem_ctx, ref, rids, num_entries,
-                                     names, flags);
+       become_root(); /* lookup_name can require root privs */
+       r_u->status = lookup_lsa_rids(p->mem_ctx, ref, rids, num_entries,
+                                     names, flags, &mapped_count);
+       unbecome_root();
+
 done:
 
-       if (NT_STATUS_IS_OK(r_u->status)) {
+       if (NT_STATUS_IS_OK(r_u->status) && (num_entries != 0) ) {
                if (mapped_count == 0)
                        r_u->status = NT_STATUS_NONE_MAPPED;
                else if (mapped_count != num_entries)
                        r_u->status = STATUS_SOME_UNMAPPED;
        }
+
        init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
+       return r_u->status;
+}
 
+/***************************************************************************
+lsa_reply_lookup_names2
+ ***************************************************************************/
+
+NTSTATUS _lsa_lookup_names2(pipes_struct *p, LSA_Q_LOOKUP_NAMES2 *q_u, LSA_R_LOOKUP_NAMES2 *r_u)
+{
+       struct lsa_info *handle;
+       UNISTR2 *names = q_u->uni_name;
+       uint32 num_entries = q_u->num_entries;
+       DOM_R_REF *ref;
+       DOM_RID *rids;
+       DOM_RID2 *rids2;
+       int i;
+       uint32 mapped_count = 0;
+       int flags = 0;
+
+       if (num_entries >  MAX_LOOKUP_SIDS) {
+               num_entries = MAX_LOOKUP_SIDS;
+               DEBUG(5,("_lsa_lookup_names2: truncating name lookup list to %d\n", num_entries));
+       }
+               
+       /* Probably the lookup_level is some sort of bitmask. */
+       if (q_u->lookup_level == 1) {
+               flags = LOOKUP_NAME_ALL;
+       }
+
+       ref = TALLOC_ZERO_P(p->mem_ctx, DOM_R_REF);
+       rids = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_RID, num_entries);
+       rids2 = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_RID2, num_entries);
+
+       if ((ref == NULL) || (rids == NULL) || (rids2 == NULL)) {
+               r_u->status = NT_STATUS_NO_MEMORY;
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle)) {
+               r_u->status = NT_STATUS_INVALID_HANDLE;
+               goto done;
+       }
+
+       /* check if the user have enough rights */
+       if (!(handle->access & POLICY_LOOKUP_NAMES)) {
+               r_u->status = NT_STATUS_ACCESS_DENIED;
+               goto done;
+       }
+
+       /* set up the LSA Lookup RIDs response */
+       become_root(); /* lookup_name can require root privs */
+       r_u->status = lookup_lsa_rids(p->mem_ctx, ref, rids, num_entries,
+                                     names, flags, &mapped_count);
+       unbecome_root();
+
+done:
+
+       if (NT_STATUS_IS_OK(r_u->status)) {
+               if (mapped_count == 0) {
+                       r_u->status = NT_STATUS_NONE_MAPPED;
+               } else if (mapped_count != num_entries) {
+                       r_u->status = STATUS_SOME_UNMAPPED;
+               }
+       }
+
+       /* Convert the rids array to rids2. */
+       for (i = 0; i < num_entries; i++) {
+               rids2[i].type = rids[i].type;
+               rids2[i].rid = rids[i].rid;
+               rids2[i].rid_idx = rids[i].rid_idx;
+               rids2[i].unknown = 0;
+       }
+
+       init_reply_lookup_names2(r_u, ref, num_entries, rids2, mapped_count);
+       return r_u->status;
+}
+
+/***************************************************************************
+lsa_reply_lookup_names3.
+ ***************************************************************************/
+
+NTSTATUS _lsa_lookup_names3(pipes_struct *p, LSA_Q_LOOKUP_NAMES3 *q_u, LSA_R_LOOKUP_NAMES3 *r_u)
+{
+       struct lsa_info *handle;
+       UNISTR2 *names = q_u->uni_name;
+       uint32 num_entries = q_u->num_entries;
+       DOM_R_REF *ref = NULL;
+       LSA_TRANSLATED_SID3 *trans_sids = NULL;
+       uint32 mapped_count = 0;
+       int flags = 0;
+
+       if (num_entries >  MAX_LOOKUP_SIDS) {
+               num_entries = MAX_LOOKUP_SIDS;
+               DEBUG(5,("_lsa_lookup_names3: truncating name lookup list to %d\n", num_entries));
+       }
+               
+       /* Probably the lookup_level is some sort of bitmask. */
+       if (q_u->lookup_level == 1) {
+               flags = LOOKUP_NAME_ALL;
+       }
+
+       ref = TALLOC_ZERO_P(p->mem_ctx, DOM_R_REF);
+       trans_sids = TALLOC_ZERO_ARRAY(p->mem_ctx, LSA_TRANSLATED_SID3, num_entries);
+
+       if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle)) {
+               r_u->status = NT_STATUS_INVALID_HANDLE;
+               goto done;
+       }
+
+       /* check if the user have enough rights */
+       if (!(handle->access & POLICY_LOOKUP_NAMES)) {
+               r_u->status = NT_STATUS_ACCESS_DENIED;
+               goto done;
+       }
+
+       if (!ref || !trans_sids) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       /* set up the LSA Lookup SIDs response */
+       become_root(); /* lookup_name can require root privs */
+       r_u->status = lookup_lsa_sids(p->mem_ctx, ref, trans_sids, num_entries,
+                                     names, flags, &mapped_count);
+       unbecome_root();
+
+done:
+
+       if (NT_STATUS_IS_OK(r_u->status)) {
+               if (mapped_count == 0) {
+                       r_u->status = NT_STATUS_NONE_MAPPED;
+               } else if (mapped_count != num_entries) {
+                       r_u->status = STATUS_SOME_UNMAPPED;
+               }
+       }
+
+       init_reply_lookup_names3(r_u, ref, num_entries, trans_sids, mapped_count);
+       return r_u->status;
+}
+
+/***************************************************************************
+lsa_reply_lookup_names4.
+ ***************************************************************************/
+
+NTSTATUS _lsa_lookup_names4(pipes_struct *p, LSA_Q_LOOKUP_NAMES4 *q_u, LSA_R_LOOKUP_NAMES4 *r_u)
+{
+       UNISTR2 *names = q_u->uni_name;
+       uint32 num_entries = q_u->num_entries;
+       DOM_R_REF *ref = NULL;
+       LSA_TRANSLATED_SID3 *trans_sids = NULL;
+       uint32 mapped_count = 0;
+       int flags = 0;
+
+       if (num_entries >  MAX_LOOKUP_SIDS) {
+               num_entries = MAX_LOOKUP_SIDS;
+               DEBUG(5,("_lsa_lookup_names4: truncating name lookup list to %d\n", num_entries));
+       }
+               
+       /* Probably the lookup_level is some sort of bitmask. */
+       if (q_u->lookup_level == 1) {
+               flags = LOOKUP_NAME_ALL;
+       }
+
+       /* No policy handle on this call. Restrict to crypto connections. */
+       if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
+               DEBUG(0,("_lsa_lookup_names4: client %s not using schannel for netlogon\n",
+                       get_remote_machine_name() ));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       ref = TALLOC_ZERO_P(p->mem_ctx, DOM_R_REF);
+       trans_sids = TALLOC_ZERO_ARRAY(p->mem_ctx, LSA_TRANSLATED_SID3, num_entries);
+
+       if (!ref || !trans_sids) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       /* set up the LSA Lookup SIDs response */
+       become_root(); /* lookup_name can require root privs */
+       r_u->status = lookup_lsa_sids(p->mem_ctx, ref, trans_sids, num_entries,
+                                     names, flags, &mapped_count);
+       unbecome_root();
+
+       if (NT_STATUS_IS_OK(r_u->status)) {
+               if (mapped_count == 0) {
+                       r_u->status = NT_STATUS_NONE_MAPPED;
+               } else if (mapped_count != num_entries) {
+                       r_u->status = STATUS_SOME_UNMAPPED;
+               }
+       }
+
+       init_reply_lookup_names4(r_u, ref, num_entries, trans_sids, mapped_count);
        return r_u->status;
 }
 
@@ -944,8 +1300,9 @@ done:
 
 NTSTATUS _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
 {
-       if (!find_policy_by_hnd(p, &q_u->pol, NULL))
+       if (!find_policy_by_hnd(p, &q_u->pol, NULL)) {
                return NT_STATUS_INVALID_HANDLE;
+       }
 
        close_policy_hnd(p, &q_u->pol);
        return NT_STATUS_OK;
@@ -1200,17 +1557,23 @@ NTSTATUS _lsa_create_account(pipes_struct *p, LSA_Q_CREATEACCOUNT *q_u, LSA_R_CR
         * I don't know if it's the right one. not documented.
         * but guessed with rpcclient.
         */
-       if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
+       if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION)) {
+               DEBUG(10, ("_lsa_create_account: No POLICY_GET_PRIVATE_INFORMATION access right!\n"));
                return NT_STATUS_ACCESS_DENIED;
+       }
 
        /* check to see if the pipe_user is a Domain Admin since 
           account_pol.tdb was already opened as root, this is all we have */
           
-       if ( !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
+       if ( !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) ) {
+               DEBUG(10, ("_lsa_create_account: The use is not a Domain Admin, deny access!\n"));
                return NT_STATUS_ACCESS_DENIED;
+       }
                
-       if ( is_privileged_sid( &q_u->sid.sid ) )
+       if ( is_privileged_sid( &q_u->sid.sid ) ) {
+               DEBUG(10, ("_lsa_create_account: Policy account already exists!\n"));
                return NT_STATUS_OBJECT_NAME_COLLISION;
+       }
 
        /* associate the user/group SID with the (unique) handle. */
        
@@ -1225,6 +1588,7 @@ NTSTATUS _lsa_create_account(pipes_struct *p, LSA_Q_CREATEACCOUNT *q_u, LSA_R_CR
        if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
+       DEBUG(10, ("_lsa_create_account: call privileges code to create an account\n"));
        return privilege_create_account( &info->sid );
 }
 
@@ -1319,7 +1683,7 @@ NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA
                return NT_STATUS_INVALID_HANDLE;
 
        if (!lookup_sid(p->mem_ctx, &info->sid, NULL, NULL, NULL))
-               return NT_STATUS_ACCESS_DENIED;
+               return NT_STATUS_OK;
 
        /*
          0x01 -> Log on locally
@@ -1370,7 +1734,6 @@ NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u
        struct lsa_info *info = NULL;
        SE_PRIV mask;
        PRIVILEGE_SET *set = NULL;
-       struct current_user user;
 
        /* find the connection policy handle. */
        if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
@@ -1379,8 +1742,7 @@ NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u
        /* check to see if the pipe_user is root or a Domain Admin since 
           account_pol.tdb was already opened as root, this is all we have */
           
-       get_current_user( &user, p );
-       if ( user.ut.uid != sec_initial_uid() 
+       if ( p->pipe_user.ut.uid != sec_initial_uid() 
                && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
        {
                return NT_STATUS_ACCESS_DENIED;
@@ -1411,7 +1773,6 @@ NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEP
        struct lsa_info *info = NULL;
        SE_PRIV mask;
        PRIVILEGE_SET *set = NULL;
-       struct current_user user;
 
        /* find the connection policy handle. */
        if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
@@ -1420,8 +1781,7 @@ NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEP
        /* check to see if the pipe_user is root or a Domain Admin since 
           account_pol.tdb was already opened as root, this is all we have */
           
-       get_current_user( &user, p );
-       if ( user.ut.uid != sec_initial_uid()
+       if ( p->pipe_user.ut.uid != sec_initial_uid()
                && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) ) 
        {
                return NT_STATUS_ACCESS_DENIED;
@@ -1572,7 +1932,6 @@ NTSTATUS _lsa_add_acct_rights(pipes_struct *p, LSA_Q_ADD_ACCT_RIGHTS *q_u, LSA_R
        DOM_SID sid;
        fstring privname;
        UNISTR4_ARRAY *uni_privnames = q_u->rights;
-       struct current_user user;
        
 
        /* find the connection policy handle. */
@@ -1582,8 +1941,7 @@ NTSTATUS _lsa_add_acct_rights(pipes_struct *p, LSA_Q_ADD_ACCT_RIGHTS *q_u, LSA_R
        /* check to see if the pipe_user is a Domain Admin since 
           account_pol.tdb was already opened as root, this is all we have */
           
-       get_current_user( &user, p );
-       if ( user.ut.uid != sec_initial_uid()
+       if ( p->pipe_user.ut.uid != sec_initial_uid()
                && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) ) 
        {
                return NT_STATUS_ACCESS_DENIED;
@@ -1630,7 +1988,6 @@ NTSTATUS _lsa_remove_acct_rights(pipes_struct *p, LSA_Q_REMOVE_ACCT_RIGHTS *q_u,
        DOM_SID sid;
        fstring privname;
        UNISTR4_ARRAY *uni_privnames = q_u->rights;
-       struct current_user user;
        
 
        /* find the connection policy handle. */
@@ -1640,8 +1997,7 @@ NTSTATUS _lsa_remove_acct_rights(pipes_struct *p, LSA_Q_REMOVE_ACCT_RIGHTS *q_u,
        /* check to see if the pipe_user is a Domain Admin since 
           account_pol.tdb was already opened as root, this is all we have */
           
-       get_current_user( &user, p );
-       if ( user.ut.uid != sec_initial_uid()
+       if ( p->pipe_user.ut.uid != sec_initial_uid()
                && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
        {
                return NT_STATUS_ACCESS_DENIED;