s3-lsa: Fix _lsa_LookupNames2() server implementation which always returned a NULL...
[ira/wip.git] / source3 / rpc_server / srv_lsa_nt.c
index f43258d5e5d17bf4a94793ec86773c456ff78d57..fb5117cdd3b18b09df96b8ab5e24a221a5993e6f 100644 (file)
@@ -10,6 +10,7 @@
  *  Copyright (C) Simo Sorce                        2003.
  *  Copyright (C) Gerald (Jerry) Carter             2005.
  *  Copyright (C) Volker Lendecke                   2005.
+ *  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
@@ -32,6 +33,8 @@
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
 
+#define MAX_LOOKUP_SIDS 0x5000 /* 20480 */
+
 extern PRIVS privs[];
 
 struct lsa_info {
@@ -40,10 +43,10 @@ struct lsa_info {
 };
 
 const struct generic_mapping lsa_generic_mapping = {
-       POLICY_READ,
-       POLICY_WRITE,
-       POLICY_EXECUTE,
-       POLICY_ALL_ACCESS
+       LSA_POLICY_READ,
+       LSA_POLICY_WRITE,
+       LSA_POLICY_EXECUTE,
+       LSA_POLICY_ALL_ACCESS
 };
 
 /***************************************************************************
@@ -67,13 +70,13 @@ static int init_lsa_ref_domain_list(TALLOC_CTX *mem_ctx,
                num = ref->count;
        }
 
-       if (num >= MAX_REF_DOMAINS) {
+       if (num >= LSA_REF_DOMAIN_LIST_MULTIPLIER) {
                /* index not found, already at maximum domain limit */
                return -1;
        }
 
        ref->count = num + 1;
-       ref->max_size = MAX_REF_DOMAINS;
+       ref->max_size = LSA_REF_DOMAIN_LIST_MULTIPLIER;
 
        ref->domains = TALLOC_REALLOC_ARRAY(mem_ctx, ref->domains,
                                            struct lsa_DomainInfo, ref->count);
@@ -81,6 +84,8 @@ static int init_lsa_ref_domain_list(TALLOC_CTX *mem_ctx,
                return -1;
        }
 
+       ZERO_STRUCT(ref->domains[num]);
+
        init_lsa_StringLarge(&ref->domains[num].name, dom_name);
        ref->domains[num].sid = sid_dup_talloc(mem_ctx, dom_sid);
        if (!ref->domains[num].sid) {
@@ -91,17 +96,6 @@ static int init_lsa_ref_domain_list(TALLOC_CTX *mem_ctx,
 }
 
 
-/*******************************************************************
- Function to free the per handle data.
- ********************************************************************/
-
-static void free_lsa_info(void *ptr)
-{
-       struct lsa_info *lsa = (struct lsa_info *)ptr;
-
-       SAFE_FREE(lsa);
-}
-
 /***************************************************************************
  initialize a lsa_DomainInfo structure.
  ***************************************************************************/
@@ -193,7 +187,9 @@ static NTSTATUS lookup_lsa_rids(TALLOC_CTX *mem_ctx,
                        mapped_count++;
                }
 
-               init_lsa_translated_sid(&prid[i], type, rid, dom_idx);
+               prid[i].sid_type        = type;
+               prid[i].rid             = rid;
+               prid[i].sid_index       = dom_idx;
        }
 
        *pmapped_count = mapped_count;
@@ -227,6 +223,8 @@ static NTSTATUS lookup_lsa_sids(TALLOC_CTX *mem_ctx,
                const char *domain;
                enum lsa_SidType type = SID_NAME_UNKNOWN;
 
+               ZERO_STRUCT(sid);
+
                /* Split name into domain and user component */
 
                full_name = name[i].string;
@@ -285,22 +283,18 @@ static NTSTATUS lsa_get_generic_sd(TALLOC_CTX *mem_ctx, SEC_DESC **sd, size_t *s
        DOM_SID adm_sid;
 
        SEC_ACE ace[3];
-       SEC_ACCESS mask;
 
        SEC_ACL *psa = NULL;
 
-       init_sec_access(&mask, POLICY_EXECUTE);
-       init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
+       init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, LSA_POLICY_EXECUTE, 0);
 
        sid_copy(&adm_sid, get_global_sam_sid());
        sid_append_rid(&adm_sid, DOMAIN_GROUP_RID_ADMINS);
-       init_sec_access(&mask, POLICY_ALL_ACCESS);
-       init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
+       init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, LSA_POLICY_ALL_ACCESS, 0);
 
        sid_copy(&local_adm_sid, &global_sid_Builtin);
        sid_append_rid(&local_adm_sid, BUILTIN_ALIAS_RID_ADMINS);
-       init_sec_access(&mask, POLICY_ALL_ACCESS);
-       init_sec_ace(&ace[2], &local_adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
+       init_sec_ace(&ace[2], &local_adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, LSA_POLICY_ALL_ACCESS, 0);
 
        if((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
                return NT_STATUS_NO_MEMORY;
@@ -378,8 +372,10 @@ NTSTATUS _lsa_OpenPolicy2(pipes_struct *p,
        /* get the generic lsa policy SD until we store it */
        lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
 
-       if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status)) {
-               if (p->pipe_user.ut.uid != sec_initial_uid()) {
+       status = se_access_check(psd, p->server_info->ptok, des_access,
+                                &acc_granted);
+       if (!NT_STATUS_IS_OK(status)) {
+               if (p->server_info->utok.uid != sec_initial_uid()) {
                        return status;
                }
                DEBUG(4,("ACCESS should be DENIED (granted: %#010x;  required: %#010x)\n",
@@ -389,19 +385,20 @@ NTSTATUS _lsa_OpenPolicy2(pipes_struct *p,
 
        /* This is needed for lsa_open_account and rpcclient .... :-) */
 
-       if (p->pipe_user.ut.uid == sec_initial_uid())
-               acc_granted = POLICY_ALL_ACCESS;
+       if (p->server_info->utok.uid == sec_initial_uid())
+               acc_granted = LSA_POLICY_ALL_ACCESS;
 
        /* associate the domain SID with the (unique) handle. */
-       if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
+       info = TALLOC_ZERO_P(p->mem_ctx, struct lsa_info);
+       if (info == NULL) {
                return NT_STATUS_NO_MEMORY;
+       }
 
-       ZERO_STRUCTP(info);
        sid_copy(&info->sid,get_global_sam_sid());
        info->access = acc_granted;
 
        /* set up the LSA QUERY INFO response */
-       if (!create_policy_hnd(p, r->out.handle, free_lsa_info, (void *)info))
+       if (!create_policy_hnd(p, r->out.handle, info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
        return NT_STATUS_OK;
@@ -428,8 +425,10 @@ NTSTATUS _lsa_OpenPolicy(pipes_struct *p,
        /* get the generic lsa policy SD until we store it */
        lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
 
-       if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status)) {
-               if (geteuid() != 0) {
+       status = se_access_check(psd, p->server_info->ptok, des_access,
+                                &acc_granted);
+       if (!NT_STATUS_IS_OK(status)) {
+               if (p->server_info->utok.uid != sec_initial_uid()) {
                        return status;
                }
                DEBUG(4,("ACCESS should be DENIED (granted: %#010x;  required: %#010x)\n",
@@ -439,15 +438,16 @@ NTSTATUS _lsa_OpenPolicy(pipes_struct *p,
        }
 
        /* associate the domain SID with the (unique) handle. */
-       if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
+       info = TALLOC_ZERO_P(p->mem_ctx, struct lsa_info);
+       if (info == NULL) {
                return NT_STATUS_NO_MEMORY;
+       }
 
-       ZERO_STRUCTP(info);
        sid_copy(&info->sid,get_global_sam_sid());
        info->access = acc_granted;
 
        /* set up the LSA QUERY INFO response */
-       if (!create_policy_hnd(p, r->out.handle, free_lsa_info, (void *)info))
+       if (!create_policy_hnd(p, r->out.handle, info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
        return NT_STATUS_OK;
@@ -482,11 +482,13 @@ NTSTATUS _lsa_EnumTrustDom(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
                return NT_STATUS_INVALID_HANDLE;
 
-       /* check if the user have enough rights */
-       if (!(info->access & POLICY_VIEW_LOCAL_INFORMATION))
+       /* check if the user has enough rights */
+       if (!(info->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
                return NT_STATUS_ACCESS_DENIED;
 
+       become_root();
        nt_status = pdb_enum_trusteddoms(p->mem_ctx, &num_domains, &domains);
+       unbecome_root();
 
        if (!NT_STATUS_IS_OK(nt_status)) {
                return nt_status;
@@ -529,6 +531,10 @@ NTSTATUS _lsa_EnumTrustDom(pipes_struct *p,
        return nt_status;
 }
 
+#define LSA_AUDIT_NUM_CATEGORIES_NT4   7
+#define LSA_AUDIT_NUM_CATEGORIES_WIN2K 9
+#define LSA_AUDIT_NUM_CATEGORIES LSA_AUDIT_NUM_CATEGORIES_NT4
+
 /***************************************************************************
  _lsa_QueryInfoPolicy
  ***************************************************************************/
@@ -557,8 +563,8 @@ NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p,
 
                uint32 policy_def = LSA_AUDIT_POLICY_ALL;
 
-               /* check if the user have enough rights */
-               if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION)) {
+               /* check if the user has enough rights */
+               if (!(handle->access & LSA_POLICY_VIEW_AUDIT_INFORMATION)) {
                        DEBUG(10,("_lsa_QueryInfoPolicy: insufficient access rights\n"));
                        return NT_STATUS_ACCESS_DENIED;
                }
@@ -585,8 +591,8 @@ NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p,
                break;
                }
        case 0x03:
-               /* check if the user have enough rights */
-               if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
+               /* check if the user has enough rights */
+               if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
                        return NT_STATUS_ACCESS_DENIED;
 
                /* Request PolicyPrimaryDomainInformation. */
@@ -594,15 +600,22 @@ NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p,
                        case ROLE_DOMAIN_PDC:
                        case ROLE_DOMAIN_BDC:
                                name = get_global_sam_name();
-                               sid = get_global_sam_sid();
+                               sid = sid_dup_talloc(p->mem_ctx, get_global_sam_sid());
+                               if (!sid) {
+                                       return NT_STATUS_NO_MEMORY;
+                               }
                                break;
                        case ROLE_DOMAIN_MEMBER:
                                name = lp_workgroup();
                                /* We need to return the Domain SID here. */
-                               if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid))
-                                       sid = &domain_sid;
-                               else
+                               if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) {
+                                       sid = sid_dup_talloc(p->mem_ctx, &domain_sid);
+                                       if (!sid) {
+                                               return NT_STATUS_NO_MEMORY;
+                                       }
+                               } else {
                                        return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
+                               }
                                break;
                        case ROLE_STANDALONE:
                                name = lp_workgroup();
@@ -614,8 +627,8 @@ NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p,
                init_dom_query_3(&info->domain, name, sid);
                break;
        case 0x05:
-               /* check if the user have enough rights */
-               if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
+               /* check if the user has enough rights */
+               if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
                        return NT_STATUS_ACCESS_DENIED;
 
                /* Request PolicyAccountDomainInformation. */
@@ -625,8 +638,8 @@ NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p,
                init_dom_query_5(&info->account_domain, name, sid);
                break;
        case 0x06:
-               /* check if the user have enough rights */
-               if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
+               /* check if the user has enough rights */
+               if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
                        return NT_STATUS_ACCESS_DENIED;
 
                switch (lp_server_role()) {
@@ -635,14 +648,14 @@ NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p,
                                 * only a BDC is a backup controller
                                 * of the domain, it controls.
                                 */
-                               info->role.role = 2;
+                               info->role.role = LSA_ROLE_BACKUP;
                                break;
                        default:
                                /*
                                 * any other role is a primary
                                 * of the domain, it controls.
                                 */
-                               info->role.role = 3;
+                               info->role.role = LSA_ROLE_PRIMARY;
                                break;
                }
                break;
@@ -711,7 +724,7 @@ static NTSTATUS _lsa_lookup_sids_internal(pipes_struct *p,
                return NT_STATUS_NO_MEMORY;
        }
 
-       for (i=0; i<MAX_REF_DOMAINS; i++) {
+       for (i=0; i<LSA_REF_DOMAIN_LIST_MULTIPLIER; i++) {
 
                if (!dom_infos[i].valid) {
                        break;
@@ -748,8 +761,10 @@ static NTSTATUS _lsa_lookup_sids_internal(pipes_struct *p,
                        mapped_count += 1;
                }
 
-               init_lsa_translated_name2(&names[i], name->type,
-                                         name->name, name->dom_idx, 0);
+               names[i].sid_type       = name->type;
+               names[i].name.string    = name->name;
+               names[i].sid_index      = name->dom_idx;
+               names[i].unknown        = 0;
        }
 
        status = NT_STATUS_NONE_MAPPED;
@@ -793,7 +808,7 @@ NTSTATUS _lsa_LookupSids(pipes_struct *p,
        }
 
        /* check if the user has enough rights */
-       if (!(handle->access & POLICY_LOOKUP_NAMES)) {
+       if (!(handle->access & LSA_POLICY_LOOKUP_NAMES)) {
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -812,6 +827,18 @@ NTSTATUS _lsa_LookupSids(pipes_struct *p,
                                           &names,
                                           &mapped_count);
 
+       /* Only return here when there is a real error.
+          NT_STATUS_NONE_MAPPED is a special case as it indicates that none of
+          the requested sids could be resolved. Older versions of XP (pre SP3)
+          rely that we return with the string representations of those SIDs in
+          that case. If we don't, XP crashes - Guenther
+          */
+
+       if (NT_STATUS_IS_ERR(status) &&
+           !NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
+               return status;
+       }
+
        /* Convert from lsa_TranslatedName2 to lsa_TranslatedName */
        names_out = TALLOC_ARRAY(p->mem_ctx, struct lsa_TranslatedName,
                                 num_sids);
@@ -866,8 +893,8 @@ NTSTATUS _lsa_LookupSids2(pipes_struct *p,
                        return NT_STATUS_INVALID_HANDLE;
                }
 
-               /* check if the user have enough rights */
-               if (!(handle->access & POLICY_LOOKUP_NAMES)) {
+               /* check if the user has enough rights */
+               if (!(handle->access & LSA_POLICY_LOOKUP_NAMES)) {
                        return NT_STATUS_ACCESS_DENIED;
                }
        }
@@ -998,8 +1025,8 @@ NTSTATUS _lsa_LookupNames(pipes_struct *p,
                goto done;
        }
 
-       /* check if the user have enough rights */
-       if (!(handle->access & POLICY_LOOKUP_NAMES)) {
+       /* check if the user has enough rights */
+       if (!(handle->access & LSA_POLICY_LOOKUP_NAMES)) {
                status = NT_STATUS_ACCESS_DENIED;
                goto done;
        }
@@ -1020,10 +1047,10 @@ done:
                }
        }
 
-       *r->out.count = num_entries;
+       *r->out.count = mapped_count;
        *r->out.domains = domains;
        r->out.sids->sids = rids;
-       r->out.sids->count = mapped_count;
+       r->out.sids->count = num_entries;
 
        return status;
 }
@@ -1062,6 +1089,7 @@ NTSTATUS _lsa_LookupNames2(pipes_struct *p,
 
        status = _lsa_LookupNames(p, &q);
 
+       sid_array2->count = sid_array->count;
        sid_array2->sids = TALLOC_ARRAY(p->mem_ctx, struct lsa_TranslatedSid2, sid_array->count);
        if (!sid_array2->sids) {
                return NT_STATUS_NO_MEMORY;
@@ -1137,8 +1165,8 @@ NTSTATUS _lsa_LookupNames3(pipes_struct *p,
                        goto done;
                }
 
-               /* check if the user have enough rights */
-               if (!(handle->access & POLICY_LOOKUP_NAMES)) {
+               /* check if the user has enough rights */
+               if (!(handle->access & LSA_POLICY_LOOKUP_NAMES)) {
                        status = NT_STATUS_ACCESS_DENIED;
                        goto done;
                }
@@ -1160,10 +1188,10 @@ done:
                }
        }
 
-       *r->out.count = num_entries;
+       *r->out.count = mapped_count;
        *r->out.domains = domains;
        r->out.sids->sids = trans_sids;
-       r->out.sids->count = mapped_count;
+       r->out.sids->count = num_entries;
 
        return status;
 }
@@ -1188,8 +1216,8 @@ NTSTATUS _lsa_LookupNames4(pipes_struct *p,
        q.in.num_names          = r->in.num_names;
        q.in.names              = r->in.names;
        q.in.level              = r->in.level;
-       q.in.unknown1           = r->in.unknown1;
-       q.in.unknown2           = r->in.unknown2;
+       q.in.lookup_options     = r->in.lookup_options;
+       q.in.client_revision    = r->in.client_revision;
        q.in.sids               = r->in.sids;
        q.in.count              = r->in.count;
 
@@ -1211,6 +1239,7 @@ NTSTATUS _lsa_Close(pipes_struct *p, struct lsa_Close *r)
        }
 
        close_policy_hnd(p, r->in.handle);
+       ZERO_STRUCTP(r->out.handle);
        return NT_STATUS_OK;
 }
 
@@ -1289,10 +1318,10 @@ NTSTATUS _lsa_EnumPrivs(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
                return NT_STATUS_INVALID_HANDLE;
 
-       /* check if the user have enough rights
+       /* check if the user has enough rights
           I don't know if it's the right one. not documented.  */
 
-       if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
+       if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
                return NT_STATUS_ACCESS_DENIED;
 
        if (num_privs) {
@@ -1345,12 +1374,12 @@ NTSTATUS _lsa_LookupPrivDisplayName(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
                return NT_STATUS_INVALID_HANDLE;
 
-       /* check if the user have enough rights */
+       /* check if the user has enough rights */
 
        /*
         * I don't know if it's the right one. not documented.
         */
-       if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
+       if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
                return NT_STATUS_ACCESS_DENIED;
 
        DEBUG(10,("_lsa_LookupPrivDisplayName: name = %s\n", r->in.name->string));
@@ -1392,7 +1421,7 @@ NTSTATUS _lsa_EnumAccounts(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
                return NT_STATUS_INVALID_HANDLE;
 
-       if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
+       if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
                return NT_STATUS_ACCESS_DENIED;
 
        sid_list = NULL;
@@ -1414,14 +1443,14 @@ NTSTATUS _lsa_EnumAccounts(pipes_struct *p,
                sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_SidPtr,
                                         num_entries - *r->in.resume_handle);
                if (!sids) {
-                       SAFE_FREE(sid_list);
+                       talloc_free(sid_list);
                        return NT_STATUS_NO_MEMORY;
                }
 
                for (i = *r->in.resume_handle, j = 0; i < num_entries; i++, j++) {
                        sids[j].sid = sid_dup_talloc(p->mem_ctx, &sid_list[i]);
                        if (!sids[j].sid) {
-                               SAFE_FREE(sid_list);
+                               talloc_free(sid_list);
                                return NT_STATUS_NO_MEMORY;
                        }
                }
@@ -1444,14 +1473,20 @@ NTSTATUS _lsa_GetUserName(pipes_struct *p,
                          struct lsa_GetUserName *r)
 {
        const char *username, *domname;
-       user_struct *vuser = get_valid_user_struct(p->vuid);
        struct lsa_String *account_name = NULL;
        struct lsa_String *authority_name = NULL;
 
-       if (vuser == NULL)
-               return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
+       if (r->in.account_name &&
+          *r->in.account_name) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
-       if (vuser->guest) {
+       if (r->in.authority_name &&
+          *r->in.authority_name) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (p->server_info->guest) {
                /*
                 * I'm 99% sure this is not the right place to do this,
                 * global_sid_Anonymous should probably be put into the token
@@ -1462,25 +1497,28 @@ NTSTATUS _lsa_GetUserName(pipes_struct *p,
                        return NT_STATUS_NO_MEMORY;
                }
        } else {
-               username = vuser->user.smb_name;
-               domname = vuser->user.domain;
+               username = p->server_info->sanitized_username;
+               domname = pdb_get_domain(p->server_info->sam_account);
        }
 
-       account_name = TALLOC_ZERO_P(p->mem_ctx, struct lsa_String);
+       account_name = TALLOC_P(p->mem_ctx, struct lsa_String);
        if (!account_name) {
                return NT_STATUS_NO_MEMORY;
        }
+       init_lsa_String(account_name, username);
 
-       authority_name = TALLOC_ZERO_P(p->mem_ctx, struct lsa_String);
-       if (!authority_name) {
-               return NT_STATUS_NO_MEMORY;
+       if (r->out.authority_name) {
+               authority_name = TALLOC_P(p->mem_ctx, struct lsa_String);
+               if (!authority_name) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               init_lsa_String(authority_name, domname);
        }
 
-       init_lsa_String(account_name, username);
-       init_lsa_String(authority_name, domname);
-
        *r->out.account_name = account_name;
-       *r->out.authority_name = authority_name;
+       if (r->out.authority_name) {
+               *r->out.authority_name = authority_name;
+       }
 
        return NT_STATUS_OK;
 }
@@ -1499,19 +1537,21 @@ NTSTATUS _lsa_CreateAccount(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
                return NT_STATUS_INVALID_HANDLE;
 
-       /* check if the user have enough rights */
+       /* check if the user has enough rights */
 
        /*
         * 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 & LSA_POLICY_GET_PRIVATE_INFORMATION))
                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 ( p->server_info->utok.uid != sec_initial_uid()
+               && !nt_token_check_domain_rid( p->server_info->ptok,
+                                              DOMAIN_GROUP_RID_ADMINS ) )
                return NT_STATUS_ACCESS_DENIED;
 
        if ( is_privileged_sid( r->in.sid ) )
@@ -1519,15 +1559,16 @@ NTSTATUS _lsa_CreateAccount(pipes_struct *p,
 
        /* associate the user/group SID with the (unique) handle. */
 
-       if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
+       info = TALLOC_ZERO_P(p->mem_ctx, struct lsa_info);
+       if (info == NULL) {
                return NT_STATUS_NO_MEMORY;
+       }
 
-       ZERO_STRUCTP(info);
        info->sid = *r->in.sid;
        info->access = r->in.access_mask;
 
        /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, r->out.acct_handle, free_lsa_info, (void *)info))
+       if (!create_policy_hnd(p, r->out.acct_handle, info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
        return privilege_create_account( &info->sid );
@@ -1548,13 +1589,13 @@ NTSTATUS _lsa_OpenAccount(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
                return NT_STATUS_INVALID_HANDLE;
 
-       /* check if the user have enough rights */
+       /* check if the user has enough rights */
 
        /*
         * 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 & LSA_POLICY_GET_PRIVATE_INFORMATION))
                return NT_STATUS_ACCESS_DENIED;
 
        /* TODO: Fis the parsing routine before reenabling this check! */
@@ -1563,15 +1604,16 @@ NTSTATUS _lsa_OpenAccount(pipes_struct *p,
                return NT_STATUS_ACCESS_DENIED;
        #endif
        /* associate the user/group SID with the (unique) handle. */
-       if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
+       info = TALLOC_ZERO_P(p->mem_ctx, struct lsa_info);
+       if (info == NULL) {
                return NT_STATUS_NO_MEMORY;
+       }
 
-       ZERO_STRUCTP(info);
        info->sid = *r->in.sid;
        info->access = r->in.access_mask;
 
        /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, r->out.acct_handle, free_lsa_info, (void *)info))
+       if (!create_policy_hnd(p, r->out.acct_handle, info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
        return NT_STATUS_OK;
@@ -1597,6 +1639,9 @@ NTSTATUS _lsa_EnumPrivsAccount(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
                return NT_STATUS_INVALID_HANDLE;
 
+       if (!(info->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
+               return NT_STATUS_ACCESS_DENIED;
+
        if ( !get_privileges_for_sids( &mask, &info->sid, 1 ) )
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
@@ -1657,6 +1702,9 @@ NTSTATUS _lsa_GetSystemAccessAccount(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
                return NT_STATUS_INVALID_HANDLE;
 
+       if (!(info->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
+               return NT_STATUS_ACCESS_DENIED;
+
        if (!lookup_sid(p->mem_ctx, &info->sid, NULL, NULL, NULL))
                return NT_STATUS_ACCESS_DENIED;
 
@@ -1669,7 +1717,8 @@ NTSTATUS _lsa_GetSystemAccessAccount(pipes_struct *p,
          they can be ORed together
        */
 
-       *r->out.access_mask = PR_LOG_ON_LOCALLY | PR_ACCESS_FROM_NETWORK;
+       *r->out.access_mask = LSA_POLICY_MODE_INTERACTIVE |
+                             LSA_POLICY_MODE_NETWORK;
 
        return NT_STATUS_OK;
 }
@@ -1691,7 +1740,9 @@ NTSTATUS _lsa_SetSystemAccessAccount(pipes_struct *p,
        /* 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 ( p->server_info->utok.uid != sec_initial_uid()
+               && !nt_token_check_domain_rid( p->server_info->ptok,
+                                              DOMAIN_GROUP_RID_ADMINS ) )
                return NT_STATUS_ACCESS_DENIED;
 
        if (!pdb_getgrsid(&map, info->sid))
@@ -1719,8 +1770,9 @@ NTSTATUS _lsa_AddPrivilegesToAccount(pipes_struct *p,
        /* 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 */
 
-       if ( p->pipe_user.ut.uid != sec_initial_uid()
-               && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
+       if ( p->server_info->utok.uid != sec_initial_uid()
+               && !nt_token_check_domain_rid( p->server_info->ptok,
+                                              DOMAIN_GROUP_RID_ADMINS ) )
        {
                return NT_STATUS_ACCESS_DENIED;
        }
@@ -1759,8 +1811,9 @@ NTSTATUS _lsa_RemovePrivilegesFromAccount(pipes_struct *p,
        /* 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 */
 
-       if ( p->pipe_user.ut.uid != sec_initial_uid()
-               && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
+       if ( p->server_info->utok.uid != sec_initial_uid()
+               && !nt_token_check_domain_rid( p->server_info->ptok,
+                                              DOMAIN_GROUP_RID_ADMINS ) )
        {
                return NT_STATUS_ACCESS_DENIED;
        }
@@ -1797,11 +1850,10 @@ NTSTATUS _lsa_QuerySecurity(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
                return NT_STATUS_INVALID_HANDLE;
 
-       /* check if the user have enough rights */
-       if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
+       /* check if the user has enough rights */
+       if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
                return NT_STATUS_ACCESS_DENIED;
 
-
        switch (r->in.sec_info) {
        case 1:
                /* SD contains only the owner */
@@ -1854,8 +1906,8 @@ NTSTATUS _lsa_QuerySecurity(pipes_struct *p,
 
        switch (q_u->info_class) {
        case 0x0c:
-               /* check if the user have enough rights */
-               if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
+               /* check if the user has enough rights */
+               if (!(handle->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
                        return NT_STATUS_ACCESS_DENIED;
 
                /* Request PolicyPrimaryDomainInformation. */
@@ -1917,8 +1969,9 @@ NTSTATUS _lsa_AddAccountRights(pipes_struct *p,
        /* 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 ( p->pipe_user.ut.uid != sec_initial_uid()
-               && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
+       if ( p->server_info->utok.uid != sec_initial_uid()
+               && !nt_token_check_domain_rid( p->server_info->ptok,
+                                              DOMAIN_GROUP_RID_ADMINS ) )
        {
                return NT_STATUS_ACCESS_DENIED;
        }
@@ -1966,8 +2019,9 @@ NTSTATUS _lsa_RemoveAccountRights(pipes_struct *p,
        /* 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 ( p->pipe_user.ut.uid != sec_initial_uid()
-               && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
+       if ( p->server_info->utok.uid != sec_initial_uid()
+               && !nt_token_check_domain_rid( p->server_info->ptok,
+                                              DOMAIN_GROUP_RID_ADMINS ) )
        {
                return NT_STATUS_ACCESS_DENIED;
        }
@@ -2059,6 +2113,9 @@ NTSTATUS _lsa_EnumAccountRights(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
                return NT_STATUS_INVALID_HANDLE;
 
+       if (!(info->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
+               return NT_STATUS_ACCESS_DENIED;
+
        /* according to an NT4 PDC, you can add privileges to SIDs even without
           call_lsa_create_account() first.  And you can use any arbitrary SID. */
 
@@ -2101,6 +2158,9 @@ NTSTATUS _lsa_LookupPrivValue(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
                return NT_STATUS_INVALID_HANDLE;
 
+       if (!(info->access & LSA_POLICY_VIEW_LOCAL_INFORMATION))
+               return NT_STATUS_ACCESS_DENIED;
+
        name = r->in.name->string;
 
        DEBUG(10,("_lsa_lookup_priv_value: name = %s\n", name));