s3-samr: add support for setting password hashes via samr_SetUserInfo level 21.
[ira/wip.git] / source3 / rpc_server / srv_samr_nt.c
index 386e081d4aa7de11a06f8fa76687fe8df583ec45..a4bfe73f1c7eb514fa483dcdf4acc04c8da32bba 100644 (file)
 
 #define SAMR_USR_RIGHTS_WRITE_PW \
                ( READ_CONTROL_ACCESS           | \
-                 SA_RIGHT_USER_CHANGE_PASSWORD | \
-                 SA_RIGHT_USER_SET_LOC_COM )
+                 SAMR_USER_ACCESS_CHANGE_PASSWORD      | \
+                 SAMR_USER_ACCESS_SET_LOC_COM)
 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
-               ( READ_CONTROL_ACCESS | SA_RIGHT_USER_SET_LOC_COM )
+               ( READ_CONTROL_ACCESS | SAMR_USER_ACCESS_SET_LOC_COM )
 
 #define DISP_INFO_CACHE_TIMEOUT 10
 
+#define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
+#define MAX_SAM_ENTRIES_W95 50
+
 typedef struct disp_info {
        DOM_SID sid; /* identify which domain this is. */
        bool builtin_domain; /* Quick flag to check if this is the builtin domain. */
@@ -70,7 +73,6 @@ struct samr_info {
        uint32 status; /* some sort of flag.  best to record it.  comes from opnum 0x39 */
        uint32 acc_granted;
        DISP_INFO *disp_info;
-       TALLOC_CTX *mem_ctx;
 };
 
 static const struct generic_mapping sam_generic_mapping = {
@@ -91,7 +93,7 @@ static const struct generic_mapping usr_generic_mapping = {
 static const struct generic_mapping usr_nopwchange_generic_mapping = {
        GENERIC_RIGHTS_USER_READ,
        GENERIC_RIGHTS_USER_WRITE,
-       GENERIC_RIGHTS_USER_EXECUTE & ~SA_RIGHT_USER_CHANGE_PASSWORD,
+       GENERIC_RIGHTS_USER_EXECUTE & ~SAMR_USER_ACCESS_CHANGE_PASSWORD,
        GENERIC_RIGHTS_USER_ALL_ACCESS};
 static const struct generic_mapping grp_generic_mapping = {
        GENERIC_RIGHTS_GROUP_READ,
@@ -186,8 +188,10 @@ static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token,
 
        /* check the security descriptor first */
 
-       if ( se_access_check(psd, token, des_access, acc_granted, &status) )
+       status = se_access_check(psd, token, des_access, acc_granted);
+       if (NT_STATUS_IS_OK(status)) {
                goto done;
+       }
 
        /* give root a free pass */
 
@@ -315,8 +319,8 @@ static DISP_INFO *get_samr_dispinfo_by_sid(DOM_SID *psid)
         * enumerate stuff, so just cache 2 entries.
         */
 
-       static struct disp_info builtin_dispinfo;
-       static struct disp_info domain_dispinfo;
+       static struct disp_info *builtin_dispinfo;
+       static struct disp_info *domain_dispinfo;
 
        /* There are two cases to consider here:
           1) The SID is a domain SID and we look for an equality match, or
@@ -331,18 +335,32 @@ static DISP_INFO *get_samr_dispinfo_by_sid(DOM_SID *psid)
                /*
                 * Necessary only once, but it does not really hurt.
                 */
-               sid_copy(&builtin_dispinfo.sid, &global_sid_Builtin);
+               if (builtin_dispinfo == NULL) {
+                       builtin_dispinfo = talloc_zero(
+                               talloc_autofree_context(), struct disp_info);
+                       if (builtin_dispinfo == NULL) {
+                               return NULL;
+                       }
+               }
+               sid_copy(&builtin_dispinfo->sid, &global_sid_Builtin);
 
-               return &builtin_dispinfo;
+               return builtin_dispinfo;
        }
 
        if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
                /*
                 * Necessary only once, but it does not really hurt.
                 */
-               sid_copy(&domain_dispinfo.sid, get_global_sam_sid());
+               if (domain_dispinfo == NULL) {
+                       domain_dispinfo = talloc_zero(
+                               talloc_autofree_context(), struct disp_info);
+                       if (domain_dispinfo == NULL) {
+                               return NULL;
+                       }
+               }
+               sid_copy(&domain_dispinfo->sid, get_global_sam_sid());
 
-               return &domain_dispinfo;
+               return domain_dispinfo;
        }
 
        return NULL;
@@ -352,11 +370,13 @@ static DISP_INFO *get_samr_dispinfo_by_sid(DOM_SID *psid)
  Create a samr_info struct.
 ********************************************************************/
 
-static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
+static int samr_info_destructor(struct samr_info *info);
+
+static struct samr_info *get_samr_info_by_sid(TALLOC_CTX *mem_ctx,
+                                             DOM_SID *psid)
 {
        struct samr_info *info;
        fstring sid_str;
-       TALLOC_CTX *mem_ctx;
 
        if (psid) {
                sid_to_fstring(sid_str, psid);
@@ -364,10 +384,10 @@ static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
                fstrcpy(sid_str,"(NULL)");
        }
 
-       mem_ctx = talloc_init("samr_info for domain sid %s", sid_str);
-
-       if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL)
+       if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL) {
                return NULL;
+       }
+       talloc_set_destructor(info, samr_info_destructor);
 
        DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_str));
        if (psid) {
@@ -377,7 +397,6 @@ static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
                DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
                info->builtin_domain = False;
        }
-       info->mem_ctx = mem_ctx;
 
        info->disp_info = get_samr_dispinfo_by_sid(psid);
 
@@ -398,52 +417,24 @@ static void free_samr_cache(DISP_INFO *disp_info)
 
        become_root();
 
-       if (disp_info->users) {
-               DEBUG(10,("free_samr_cache: deleting users cache\n"));
-               pdb_search_destroy(disp_info->users);
-               disp_info->users = NULL;
-       }
-       if (disp_info->machines) {
-               DEBUG(10,("free_samr_cache: deleting machines cache\n"));
-               pdb_search_destroy(disp_info->machines);
-               disp_info->machines = NULL;
-       }
-       if (disp_info->groups) {
-               DEBUG(10,("free_samr_cache: deleting groups cache\n"));
-               pdb_search_destroy(disp_info->groups);
-               disp_info->groups = NULL;
-       }
-       if (disp_info->aliases) {
-               DEBUG(10,("free_samr_cache: deleting aliases cache\n"));
-               pdb_search_destroy(disp_info->aliases);
-               disp_info->aliases = NULL;
-       }
-       if (disp_info->enum_users) {
-               DEBUG(10,("free_samr_cache: deleting enum_users cache\n"));
-               pdb_search_destroy(disp_info->enum_users);
-               disp_info->enum_users = NULL;
-       }
-       disp_info->enum_acb_mask = 0;
+       TALLOC_FREE(disp_info->users);
+       TALLOC_FREE(disp_info->machines);
+       TALLOC_FREE(disp_info->groups);
+       TALLOC_FREE(disp_info->aliases);
+       TALLOC_FREE(disp_info->enum_users);
 
        unbecome_root();
 }
 
-/*******************************************************************
- Function to free the per handle data.
- ********************************************************************/
-
-static void free_samr_info(void *ptr)
+static int samr_info_destructor(struct samr_info *info)
 {
-       struct samr_info *info=(struct samr_info *) ptr;
-
        /* Only free the dispinfo cache if no one bothered to set up
           a timeout. */
 
        if (info->disp_info && info->disp_info->cache_timeout_event == NULL) {
                free_samr_cache(info->disp_info);
        }
-
-       talloc_destroy(info->mem_ctx);
+       return 0;
 }
 
 /*******************************************************************
@@ -452,7 +443,7 @@ static void free_samr_info(void *ptr)
 
 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
                                                 struct timed_event *te,
-                                                const struct timeval *now,
+                                                struct timeval now,
                                                 void *private_data)
 {
        DISP_INFO *disp_info = (DISP_INFO *)private_data;
@@ -481,7 +472,6 @@ static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromno
        disp_info->cache_timeout_event = event_add_timed(
                smbd_event_context(), NULL,
                timeval_current_ofs(secs_fromnow, 0),
-               "disp_info_cache_idle_timeout_handler",
                disp_info_cache_idle_timeout_handler, (void *)disp_info);
 }
 
@@ -527,7 +517,7 @@ static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
        }
 
        if (info->users == NULL) {
-               info->users = pdb_search_users(acct_flags);
+               info->users = pdb_search_users(info, acct_flags);
                if (info->users == NULL) {
                        return 0;
                }
@@ -551,7 +541,7 @@ static uint32 count_sam_groups(struct disp_info *info)
        }
 
        if (info->groups == NULL) {
-               info->groups = pdb_search_groups();
+               info->groups = pdb_search_groups(info);
                if (info->groups == NULL) {
                        return 0;
                }
@@ -570,7 +560,7 @@ static uint32 count_sam_aliases(struct disp_info *info)
        struct samr_displayentry *entry;
 
        if (info->aliases == NULL) {
-               info->aliases = pdb_search_aliases(&info->sid);
+               info->aliases = pdb_search_aliases(info, &info->sid);
                if (info->aliases == NULL) {
                        return 0;
                }
@@ -620,14 +610,14 @@ NTSTATUS _samr_OpenDomain(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(info->acc_granted,
-                                           SA_RIGHT_SAM_OPEN_DOMAIN,
+                                           SAMR_ACCESS_OPEN_DOMAIN,
                                            "_samr_OpenDomain" );
 
        if ( !NT_STATUS_IS_OK(status) )
                return status;
 
        /*check if access can be granted as requested by client. */
-       map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
+       map_max_allowed_access(p->server_info->ptok, &des_access);
 
        make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
        se_map_generic( &des_access, &dom_generic_mapping );
@@ -635,7 +625,7 @@ NTSTATUS _samr_OpenDomain(pipes_struct *p,
        se_priv_copy( &se_rights, &se_machine_account );
        se_priv_add( &se_rights, &se_add_users );
 
-       status = access_check_samr_object( psd, p->pipe_user.nt_user_token,
+       status = access_check_samr_object( psd, p->server_info->ptok,
                &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access,
                &acc_granted, "_samr_OpenDomain" );
 
@@ -648,12 +638,12 @@ NTSTATUS _samr_OpenDomain(pipes_struct *p,
        }
 
        /* associate the domain SID with the (unique) handle. */
-       if ((info = get_samr_info_by_sid(r->in.sid))==NULL)
+       if ((info = get_samr_info_by_sid(p->mem_ctx, r->in.sid))==NULL)
                return NT_STATUS_NO_MEMORY;
        info->acc_granted = acc_granted;
 
        /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, r->out.domain_handle, free_samr_info, (void *)info))
+       if (!create_policy_hnd(p, r->out.domain_handle, info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
        DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__));
@@ -729,7 +719,7 @@ NTSTATUS _samr_GetUserPwInfo(pipes_struct *p,
 /*******************************************************************
 ********************************************************************/
 
-static bool get_lsa_policy_samr_sid( pipes_struct *p, POLICY_HND *pol,
+static bool get_lsa_policy_samr_sid( pipes_struct *p, struct policy_handle *pol,
                                        DOM_SID *sid, uint32 *acc_granted,
                                        DISP_INFO **ppdisp_info)
 {
@@ -789,7 +779,7 @@ NTSTATUS _samr_SetSecurity(pipes_struct *p,
                if (sid_equal(&pol_sid, &dacl->aces[i].trustee)) {
                        ret = pdb_set_pass_can_change(sampass,
                                (dacl->aces[i].access_mask &
-                                SA_RIGHT_USER_CHANGE_PASSWORD) ?
+                                SAMR_USER_ACCESS_CHANGE_PASSWORD) ?
                                                      True: False);
                        break;
                }
@@ -801,7 +791,7 @@ NTSTATUS _samr_SetSecurity(pipes_struct *p,
        }
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_USER_SET_ATTRIBUTES,
+                                           SAMR_USER_ACCESS_SET_ATTRIBUTES,
                                            "_samr_SetSecurity");
        if (NT_STATUS_IS_OK(status)) {
                become_root();
@@ -988,7 +978,7 @@ NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(info->acc_granted,
-                                           SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
+                                           SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
                                            "_samr_EnumDomainUsers");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -1007,6 +997,7 @@ NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
        if (!samr_array) {
                return NT_STATUS_NO_MEMORY;
        }
+       *r->out.sam = samr_array;
 
        become_root();
 
@@ -1014,12 +1005,12 @@ NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
 
        if ((info->disp_info->enum_users != NULL) &&
            (info->disp_info->enum_acb_mask != r->in.acct_flags)) {
-               pdb_search_destroy(info->disp_info->enum_users);
-               info->disp_info->enum_users = NULL;
+               TALLOC_FREE(info->disp_info->enum_users);
        }
 
        if (info->disp_info->enum_users == NULL) {
-               info->disp_info->enum_users = pdb_search_users(r->in.acct_flags);
+               info->disp_info->enum_users = pdb_search_users(
+                       info->disp_info, r->in.acct_flags);
                info->disp_info->enum_acb_mask = r->in.acct_flags;
        }
 
@@ -1066,7 +1057,6 @@ NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
        samr_array->entries = samr_entries;
 
        *r->out.resume_handle = *r->in.resume_handle + num_account;
-       *r->out.sam = samr_array;
        *r->out.num_entries = num_account;
 
        DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
@@ -1127,7 +1117,7 @@ NTSTATUS _samr_EnumDomainGroups(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(info->acc_granted,
-                                           SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
+                                           SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
                                            "_samr_EnumDomainGroups");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -1152,7 +1142,7 @@ NTSTATUS _samr_EnumDomainGroups(pipes_struct *p,
        become_root();
 
        if (info->disp_info->groups == NULL) {
-               info->disp_info->groups = pdb_search_groups();
+               info->disp_info->groups = pdb_search_groups(info->disp_info);
 
                if (info->disp_info->groups == NULL) {
                        unbecome_root();
@@ -1176,9 +1166,7 @@ NTSTATUS _samr_EnumDomainGroups(pipes_struct *p,
 
        *r->out.sam = samr_array;
        *r->out.num_entries = num_groups;
-       /* this was missing, IMHO:
        *r->out.resume_handle = num_groups + *r->in.resume_handle;
-       */
 
        DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
 
@@ -1207,7 +1195,7 @@ NTSTATUS _samr_EnumDomainAliases(pipes_struct *p,
                 sid_string_dbg(&info->sid)));
 
        status = access_check_samr_function(info->acc_granted,
-                                           SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
+                                           SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
                                            "_samr_EnumDomainAliases");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -1221,7 +1209,8 @@ NTSTATUS _samr_EnumDomainAliases(pipes_struct *p,
        become_root();
 
        if (info->disp_info->aliases == NULL) {
-               info->disp_info->aliases = pdb_search_aliases(&info->sid);
+               info->disp_info->aliases = pdb_search_aliases(
+                       info->disp_info, &info->sid);
                if (info->disp_info->aliases == NULL) {
                        unbecome_root();
                        return NT_STATUS_ACCESS_DENIED;
@@ -1479,8 +1468,13 @@ NTSTATUS _samr_QueryDisplayInfo(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
                return NT_STATUS_INVALID_HANDLE;
 
+       if (info->builtin_domain) {
+               DEBUG(5,("_samr_QueryDisplayInfo: Nothing in BUILTIN\n"));
+               return NT_STATUS_OK;
+       }
+
        status = access_check_samr_function(info->acc_granted,
-                                           SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
+                                           SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
                                            "_samr_QueryDisplayInfo");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -1547,7 +1541,8 @@ NTSTATUS _samr_QueryDisplayInfo(pipes_struct *p,
        case 0x1:
        case 0x4:
                if (info->disp_info->users == NULL) {
-                       info->disp_info->users = pdb_search_users(ACB_NORMAL);
+                       info->disp_info->users = pdb_search_users(
+                               info->disp_info, ACB_NORMAL);
                        if (info->disp_info->users == NULL) {
                                unbecome_root();
                                return NT_STATUS_ACCESS_DENIED;
@@ -1565,8 +1560,8 @@ NTSTATUS _samr_QueryDisplayInfo(pipes_struct *p,
                break;
        case 0x2:
                if (info->disp_info->machines == NULL) {
-                       info->disp_info->machines =
-                               pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
+                       info->disp_info->machines = pdb_search_users(
+                               info->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
                        if (info->disp_info->machines == NULL) {
                                unbecome_root();
                                return NT_STATUS_ACCESS_DENIED;
@@ -1585,7 +1580,8 @@ NTSTATUS _samr_QueryDisplayInfo(pipes_struct *p,
        case 0x3:
        case 0x5:
                if (info->disp_info->groups == NULL) {
-                       info->disp_info->groups = pdb_search_groups();
+                       info->disp_info->groups = pdb_search_groups(
+                               info->disp_info);
                        if (info->disp_info->groups == NULL) {
                                unbecome_root();
                                return NT_STATUS_ACCESS_DENIED;
@@ -1735,7 +1731,7 @@ NTSTATUS _samr_QueryAliasInfo(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_ALIAS_LOOKUP_INFO,
+                                           SAMR_ALIAS_ACCESS_LOOKUP_INFO,
                                            "_samr_QueryAliasInfo");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -1754,14 +1750,12 @@ NTSTATUS _samr_QueryAliasInfo(pipes_struct *p,
 
        switch (r->in.level) {
        case ALIASINFOALL:
-               init_samr_alias_info1(&alias_info->all,
-                                     alias_name,
-                                     1,
-                                     alias_description);
+               alias_info->all.name.string             = alias_name;
+               alias_info->all.num_members             = 1; /* ??? */
+               alias_info->all.description.string      = alias_description;
                break;
        case ALIASINFODESCRIPTION:
-               init_samr_alias_info3(&alias_info->description,
-                                     alias_description);
+               alias_info->description.string          = alias_description;
                break;
        default:
                return NT_STATUS_INVALID_INFO_CLASS;
@@ -1789,6 +1783,7 @@ NTSTATUS _samr_LookupNames(pipes_struct *p,
        DOM_SID pol_sid;
        uint32  acc_granted;
        struct samr_Ids rids, types;
+       uint32_t num_mapped = 0;
 
        DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
 
@@ -1836,10 +1831,18 @@ NTSTATUS _samr_LookupNames(pipes_struct *p,
                }
 
                if (type[i] != SID_NAME_UNKNOWN) {
-                       status = NT_STATUS_OK;
+                       num_mapped++;
                }
        }
 
+       if (num_mapped == num_rids) {
+               status = NT_STATUS_OK;
+       } else if (num_mapped == 0) {
+               status = NT_STATUS_NONE_MAPPED;
+       } else {
+               status = STATUS_SOME_UNMAPPED;
+       }
+
        rids.count = num_rids;
        rids.ids = rid;
 
@@ -1909,6 +1912,7 @@ NTSTATUS _samr_ChangePasswordUser3(pipes_struct *p,
        uint32 reject_reason;
        struct samr_DomInfo1 *dominfo = NULL;
        struct samr_ChangeReject *reject = NULL;
+       uint32_t tmp;
 
        DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
 
@@ -1941,9 +1945,7 @@ NTSTATUS _samr_ChangePasswordUser3(pipes_struct *p,
        if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION) ||
            NT_STATUS_EQUAL(status, NT_STATUS_ACCOUNT_RESTRICTION)) {
 
-               uint32 min_pass_len,pass_hist,password_properties;
                time_t u_expire, u_min_age;
-               NTTIME nt_expire, nt_min_age;
                uint32 account_policy_temp;
 
                dominfo = TALLOC_ZERO_P(p->mem_ctx, struct samr_DomInfo1);
@@ -1960,14 +1962,14 @@ NTSTATUS _samr_ChangePasswordUser3(pipes_struct *p,
 
                /* AS ROOT !!! */
 
-               pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
-               min_pass_len = account_policy_temp;
+               pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &tmp);
+               dominfo->min_password_length = tmp;
 
-               pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
-               pass_hist = account_policy_temp;
+               pdb_get_account_policy(AP_PASSWORD_HISTORY, &tmp);
+               dominfo->password_history_length = tmp;
 
-               pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
-               password_properties = account_policy_temp;
+               pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
+                                      &dominfo->password_properties);
 
                pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
                u_expire = account_policy_temp;
@@ -1979,20 +1981,13 @@ NTSTATUS _samr_ChangePasswordUser3(pipes_struct *p,
 
                unbecome_root();
 
-               unix_to_nt_time_abs(&nt_expire, u_expire);
-               unix_to_nt_time_abs(&nt_min_age, u_min_age);
+               unix_to_nt_time_abs((NTTIME *)&dominfo->max_password_age, u_expire);
+               unix_to_nt_time_abs((NTTIME *)&dominfo->min_password_age, u_min_age);
 
                if (lp_check_password_script() && *lp_check_password_script()) {
-                       password_properties |= DOMAIN_PASSWORD_COMPLEX;
+                       dominfo->password_properties |= DOMAIN_PASSWORD_COMPLEX;
                }
 
-               init_samr_DomInfo1(dominfo,
-                                  min_pass_len,
-                                  pass_hist,
-                                  password_properties,
-                                  u_expire,
-                                  u_min_age);
-
                reject->reason = reject_reason;
 
                *r->out.dominfo = dominfo;
@@ -2060,8 +2055,8 @@ NTSTATUS _samr_LookupRids(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
-                                           "_samr__LookupRids");
+                                           0, /* Don't know the acc_bits yet */
+                                           "_samr_LookupRids");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -2127,8 +2122,6 @@ NTSTATUS _samr_OpenUser(pipes_struct *p,
 {
        struct samu *sampass=NULL;
        DOM_SID sid;
-       POLICY_HND domain_pol = *r->in.domain_handle;
-       POLICY_HND *user_pol = r->out.user_handle;
        struct samr_info *info = NULL;
        SEC_DESC *psd = NULL;
        uint32    acc_granted;
@@ -2140,11 +2133,11 @@ NTSTATUS _samr_OpenUser(pipes_struct *p,
 
        /* find the domain policy handle and get domain SID / access bits in the domain policy. */
 
-       if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
+       if ( !get_lsa_policy_samr_sid(p, r->in.domain_handle, &sid, &acc_granted, NULL) )
                return NT_STATUS_INVALID_HANDLE;
 
        nt_status = access_check_samr_function(acc_granted,
-                                              SA_RIGHT_DOMAIN_OPEN_ACCOUNT,
+                                              SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
                                               "_samr_OpenUser" );
 
        if ( !NT_STATUS_IS_OK(nt_status) )
@@ -2161,7 +2154,7 @@ NTSTATUS _samr_OpenUser(pipes_struct *p,
 
        /* check if access can be granted as requested by client. */
 
-       map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
+       map_max_allowed_access(p->server_info->ptok, &des_access);
 
        make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
        se_map_generic(&des_access, &usr_generic_mapping);
@@ -2169,7 +2162,7 @@ NTSTATUS _samr_OpenUser(pipes_struct *p,
        se_priv_copy( &se_rights, &se_machine_account );
        se_priv_add( &se_rights, &se_add_users );
 
-       nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
+       nt_status = access_check_samr_object(psd, p->server_info->ptok,
                &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
                &acc_granted, "_samr_OpenUser");
 
@@ -2188,12 +2181,12 @@ NTSTATUS _samr_OpenUser(pipes_struct *p,
        TALLOC_FREE(sampass);
 
        /* associate the user's SID and access bits with the new handle. */
-       if ((info = get_samr_info_by_sid(&sid)) == NULL)
+       if ((info = get_samr_info_by_sid(p->mem_ctx, &sid)) == NULL)
                return NT_STATUS_NO_MEMORY;
        info->acc_granted = acc_granted;
 
        /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
+       if (!create_policy_hnd(p, r->out.user_handle, info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
        return NT_STATUS_OK;
@@ -2234,79 +2227,85 @@ static NTSTATUS init_samr_parameters_string(TALLOC_CTX *mem_ctx,
        return NT_STATUS_OK;
 }
 
-/*************************************************************************
- get_user_info_7. Safe. Only gives out account_name.
- *************************************************************************/
-
-static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
-                               struct samr_UserInfo7 *r,
-                               DOM_SID *user_sid)
+static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
+                               struct samr_UserInfo5 *r,
+                               struct samu *pw,
+                               DOM_SID *domain_sid)
 {
-       struct samu *smbpass=NULL;
-       bool ret;
-       const char *account_name = NULL;
+       const DOM_SID *sid_user, *sid_group;
+       uint32_t rid, primary_gid;
 
-       ZERO_STRUCTP(r);
+       sid_user = pdb_get_user_sid(pw);
 
-       if ( !(smbpass = samu_new( mem_ctx )) ) {
-               return NT_STATUS_NO_MEMORY;
+       if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
+               DEBUG(0, ("get_user_info_5: User %s has SID %s, \nwhich conflicts with "
+                         "the domain sid %s.  Failing operation.\n",
+                         pdb_get_username(pw), sid_string_dbg(sid_user),
+                         sid_string_dbg(domain_sid)));
+               return NT_STATUS_UNSUCCESSFUL;
        }
 
        become_root();
-       ret = pdb_getsampwsid(smbpass, user_sid);
+       sid_group = pdb_get_group_sid(pw);
        unbecome_root();
 
-       if ( !ret ) {
-               DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
-               return NT_STATUS_NO_SUCH_USER;
+       if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
+               DEBUG(0, ("get_user_info_5: User %s has Primary Group SID %s, \n"
+                         "which conflicts with the domain sid %s.  Failing operation.\n",
+                         pdb_get_username(pw), sid_string_dbg(sid_group),
+                         sid_string_dbg(domain_sid)));
+               return NT_STATUS_UNSUCCESSFUL;
        }
 
-       account_name = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
-       if (!account_name) {
-               TALLOC_FREE(smbpass);
-               return NT_STATUS_NO_MEMORY;
-       }
-       TALLOC_FREE(smbpass);
+       unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
+       unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
+       unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
+       unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
 
-       DEBUG(3,("User:[%s]\n", account_name));
+       r->account_name.string  = talloc_strdup(mem_ctx, pdb_get_username(pw));
+       r->full_name.string     = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
+       r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
+       r->home_drive.string    = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
+       r->logon_script.string  = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
+       r->profile_path.string  = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
+       r->description.string   = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
+       r->workstations.string  = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
 
-       init_samr_user_info7(r, account_name);
+       r->logon_hours          = get_logon_hours_from_pdb(mem_ctx, pw);
+       r->rid                  = rid;
+       r->primary_gid          = primary_gid;
+       r->acct_flags           = pdb_get_acct_ctrl(pw);
+       r->bad_password_count   = pdb_get_bad_password_count(pw);
+       r->logon_count          = pdb_get_logon_count(pw);
 
        return NT_STATUS_OK;
 }
 
 /*************************************************************************
- get_user_info_9. Only gives out primary group SID.
+ get_user_info_7. Safe. Only gives out account_name.
  *************************************************************************/
 
-static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
-                               struct samr_UserInfo9 *r,
-                               DOM_SID *user_sid)
+static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
+                               struct samr_UserInfo7 *r,
+                               struct samu *smbpass)
 {
-       struct samu *smbpass=NULL;
-       bool ret;
-
-       ZERO_STRUCTP(r);
-
-       if ( !(smbpass = samu_new( mem_ctx )) ) {
+       r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
+       if (!r->account_name.string) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       become_root();
-       ret = pdb_getsampwsid(smbpass, user_sid);
-       unbecome_root();
-
-       if (ret==False) {
-               DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
-               TALLOC_FREE(smbpass);
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
-       DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
+       return NT_STATUS_OK;
+}
 
-       init_samr_user_info9(r, pdb_get_group_rid(smbpass));
+/*************************************************************************
+ get_user_info_9. Only gives out primary group SID.
+ *************************************************************************/
 
-       TALLOC_FREE(smbpass);
+static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
+                               struct samr_UserInfo9 *r,
+                               struct samu *smbpass)
+{
+       r->primary_gid = pdb_get_group_rid(smbpass);
 
        return NT_STATUS_OK;
 }
@@ -2317,32 +2316,9 @@ static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
 
 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
                                 struct samr_UserInfo16 *r,
-                                DOM_SID *user_sid)
+                                struct samu *smbpass)
 {
-       struct samu *smbpass=NULL;
-       bool ret;
-
-       ZERO_STRUCTP(r);
-
-       if ( !(smbpass = samu_new( mem_ctx )) ) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       become_root();
-       ret = pdb_getsampwsid(smbpass, user_sid);
-       unbecome_root();
-
-       if (ret==False) {
-               DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
-               TALLOC_FREE(smbpass);
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
-       DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
-
-       init_samr_user_info16(r, pdb_get_acct_ctrl(smbpass));
-
-       TALLOC_FREE(smbpass);
+       r->acct_flags = pdb_get_acct_ctrl(smbpass);
 
        return NT_STATUS_OK;
 }
@@ -2394,8 +2370,11 @@ static NTSTATUS get_user_info_18(pipes_struct *p,
                return NT_STATUS_ACCOUNT_DISABLED;
        }
 
-       init_samr_user_info18(r, pdb_get_lanman_passwd(smbpass),
-                             pdb_get_nt_passwd(smbpass));
+       r->lm_pwd_active = true;
+       r->nt_pwd_active = true;
+       memcpy(r->lm_pwd.hash, pdb_get_lanman_passwd(smbpass), 16);
+       memcpy(r->nt_pwd.hash, pdb_get_nt_passwd(smbpass), 16);
+       r->password_expired = 0; /* FIXME */
 
        TALLOC_FREE(smbpass);
 
@@ -2408,10 +2387,8 @@ static NTSTATUS get_user_info_18(pipes_struct *p,
 
 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
                                 struct samr_UserInfo20 *r,
-                                DOM_SID *user_sid)
+                                struct samu *sampass)
 {
-       struct samu *sampass=NULL;
-       bool ret;
        const char *munged_dial = NULL;
        DATA_BLOB blob;
        NTSTATUS status;
@@ -2419,24 +2396,8 @@ static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
 
        ZERO_STRUCTP(r);
 
-       if ( !(sampass = samu_new( mem_ctx )) ) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       become_root();
-       ret = pdb_getsampwsid(sampass, user_sid);
-       unbecome_root();
-
-       if (ret == False) {
-               DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
-               TALLOC_FREE(sampass);
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
        munged_dial = pdb_get_munged_dial(sampass);
 
-       samr_clear_sam_passwd(sampass);
-
        DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass),
                munged_dial, (int)strlen(munged_dial)));
 
@@ -2448,12 +2409,11 @@ static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
 
        status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
        data_blob_free(&blob);
-       TALLOC_FREE(sampass);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       init_samr_user_info20(r, parameters);
+       r->parameters = *parameters;
 
        return NT_STATUS_OK;
 }
@@ -2465,46 +2425,20 @@ static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
 
 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
                                 struct samr_UserInfo21 *r,
-                                DOM_SID *user_sid,
+                                struct samu *pw,
                                 DOM_SID *domain_sid)
 {
        NTSTATUS status;
-       struct samu *pw = NULL;
-       bool ret;
        const DOM_SID *sid_user, *sid_group;
        uint32_t rid, primary_gid;
-       NTTIME last_logon, last_logoff, last_password_change,
-              acct_expiry, allow_password_change, force_password_change;
+       NTTIME force_password_change;
        time_t must_change_time;
-       uint8_t password_expired;
-       const char *account_name, *full_name, *home_directory, *home_drive,
-                  *logon_script, *profile_path, *description,
-                  *workstations, *comment;
-       struct samr_LogonHours logon_hours;
        struct lsa_BinaryString *parameters = NULL;
        const char *munged_dial = NULL;
        DATA_BLOB blob;
 
        ZERO_STRUCTP(r);
 
-       if (!(pw = samu_new(mem_ctx))) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       become_root();
-       ret = pdb_getsampwsid(pw, user_sid);
-       unbecome_root();
-
-       if (ret == False) {
-               DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
-               TALLOC_FREE(pw);
-               return NT_STATUS_NO_SUCH_USER;
-       }
-
-       samr_clear_sam_passwd(pw);
-
-       DEBUG(3,("User:[%s]\n", pdb_get_username(pw)));
-
        sid_user = pdb_get_user_sid(pw);
 
        if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
@@ -2512,7 +2446,6 @@ static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
                          "the domain sid %s.  Failing operation.\n",
                          pdb_get_username(pw), sid_string_dbg(sid_user),
                          sid_string_dbg(domain_sid)));
-               TALLOC_FREE(pw);
                return NT_STATUS_UNSUCCESSFUL;
        }
 
@@ -2525,15 +2458,14 @@ static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
                          "which conflicts with the domain sid %s.  Failing operation.\n",
                          pdb_get_username(pw), sid_string_dbg(sid_group),
                          sid_string_dbg(domain_sid)));
-               TALLOC_FREE(pw);
                return NT_STATUS_UNSUCCESSFUL;
        }
 
-       unix_to_nt_time(&last_logon, pdb_get_logon_time(pw));
-       unix_to_nt_time(&last_logoff, pdb_get_logoff_time(pw));
-       unix_to_nt_time(&acct_expiry, pdb_get_kickoff_time(pw));
-       unix_to_nt_time(&last_password_change, pdb_get_pass_last_set_time(pw));
-       unix_to_nt_time(&allow_password_change, pdb_get_pass_can_change_time(pw));
+       unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
+       unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
+       unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
+       unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
+       unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
 
        must_change_time = pdb_get_pass_must_change_time(pw);
        if (must_change_time == get_time_t_max()) {
@@ -2542,12 +2474,6 @@ static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
                unix_to_nt_time(&force_password_change, must_change_time);
        }
 
-       if (pdb_get_pass_must_change_time(pw) == 0) {
-               password_expired = PASS_MUST_CHANGE_AT_NEXT_LOGON;
-       } else {
-               password_expired = 0;
-       }
-
        munged_dial = pdb_get_munged_dial(pw);
        if (munged_dial) {
                blob = base64_decode_data_blob(munged_dial);
@@ -2558,21 +2484,36 @@ static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
        status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
        data_blob_free(&blob);
        if (!NT_STATUS_IS_OK(status)) {
-               TALLOC_FREE(pw);
                return status;
        }
 
-       account_name = talloc_strdup(mem_ctx, pdb_get_username(pw));
-       full_name = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
-       home_directory = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
-       home_drive = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
-       logon_script = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
-       profile_path = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
-       description = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
-       workstations = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
-       comment = talloc_strdup(mem_ctx, pdb_get_comment(pw));
+       r->force_password_change        = force_password_change;
+
+       r->account_name.string          = talloc_strdup(mem_ctx, pdb_get_username(pw));
+       r->full_name.string             = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
+       r->home_directory.string        = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
+       r->home_drive.string            = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
+       r->logon_script.string          = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
+       r->profile_path.string          = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
+       r->description.string           = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
+       r->workstations.string          = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
+       r->comment.string               = talloc_strdup(mem_ctx, pdb_get_comment(pw));
+
+       r->logon_hours                  = get_logon_hours_from_pdb(mem_ctx, pw);
+       r->parameters                   = *parameters;
+       r->rid                          = rid;
+       r->primary_gid                  = primary_gid;
+       r->acct_flags                   = pdb_get_acct_ctrl(pw);
+       r->bad_password_count           = pdb_get_bad_password_count(pw);
+       r->logon_count                  = pdb_get_logon_count(pw);
+       r->fields_present               = pdb_build_fields_present(pw);
+       r->password_expired             = (pdb_get_pass_must_change_time(pw) == 0) ?
+                                               PASS_MUST_CHANGE_AT_NEXT_LOGON : 0;
+       r->country_code                 = 0;
+       r->code_page                    = 0;
+       r->lm_password_set              = 0;
+       r->nt_password_set              = 0;
 
-       logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
 #if 0
 
        /*
@@ -2587,36 +2528,6 @@ static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
 
 #endif
 
-       init_samr_user_info21(r,
-                             last_logon,
-                             last_logoff,
-                             last_password_change,
-                             acct_expiry,
-                             allow_password_change,
-                             force_password_change,
-                             account_name,
-                             full_name,
-                             home_directory,
-                             home_drive,
-                             logon_script,
-                             profile_path,
-                             description,
-                             workstations,
-                             comment,
-                             parameters,
-                             rid,
-                             primary_gid,
-                             pdb_get_acct_ctrl(pw),
-                             pdb_build_fields_present(pw),
-                             logon_hours,
-                             pdb_get_bad_password_count(pw),
-                             pdb_get_logon_count(pw),
-                             0, /* country_code */
-                             0, /* code_page */
-                             0, /* nt_password_set */
-                             0, /* lm_password_set */
-                             password_expired);
-       TALLOC_FREE(pw);
 
        return NT_STATUS_OK;
 }
@@ -2633,13 +2544,15 @@ NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
        struct samr_info *info = NULL;
        DOM_SID domain_sid;
        uint32 rid;
+       bool ret = false;
+       struct samu *pwd = NULL;
 
        /* search for the handle */
        if (!find_policy_by_hnd(p, r->in.user_handle, (void **)(void *)&info))
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(info->acc_granted,
-                                           SA_RIGHT_DOMAIN_OPEN_ACCOUNT,
+                                           SAMR_USER_ACCESS_GET_ATTRIBUTES,
                                            "_samr_QueryUserInfo");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -2662,52 +2575,54 @@ NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
 
        DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
 
+       if (!(pwd = samu_new(p->mem_ctx))) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       become_root();
+       ret = pdb_getsampwsid(pwd, &info->sid);
+       unbecome_root();
+
+       if (ret == false) {
+               DEBUG(4,("User %s not found\n", sid_string_dbg(&info->sid)));
+               TALLOC_FREE(pwd);
+               return NT_STATUS_NO_SUCH_USER;
+       }
+
+       DEBUG(3,("User:[%s]\n", pdb_get_username(pwd)));
+
+       samr_clear_sam_passwd(pwd);
+
        switch (r->in.level) {
+       case 5:
+               status = get_user_info_5(p->mem_ctx, &user_info->info5, pwd, &domain_sid);
+               break;
        case 7:
-               status = get_user_info_7(p->mem_ctx, &user_info->info7, &info->sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
+               status = get_user_info_7(p->mem_ctx, &user_info->info7, pwd);
                break;
        case 9:
-               status = get_user_info_9(p->mem_ctx, &user_info->info9, &info->sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
+               status = get_user_info_9(p->mem_ctx, &user_info->info9, pwd);
                break;
        case 16:
-               status = get_user_info_16(p->mem_ctx, &user_info->info16, &info->sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
+               status = get_user_info_16(p->mem_ctx, &user_info->info16, pwd);
                break;
-
        case 18:
+               /* level 18 is special */
                status = get_user_info_18(p, p->mem_ctx, &user_info->info18, &info->sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
                break;
-
        case 20:
-               status = get_user_info_20(p->mem_ctx, &user_info->info20, &info->sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
+               status = get_user_info_20(p->mem_ctx, &user_info->info20, pwd);
                break;
-
        case 21:
-               status = get_user_info_21(p->mem_ctx, &user_info->info21,
-                                         &info->sid, &domain_sid);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
+               status = get_user_info_21(p->mem_ctx, &user_info->info21, pwd, &domain_sid);
                break;
-
        default:
-               return NT_STATUS_INVALID_INFO_CLASS;
+               status = NT_STATUS_INVALID_INFO_CLASS;
+               break;
        }
 
+       TALLOC_FREE(pwd);
+
        *r->out.info = user_info;
 
        DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
@@ -2715,6 +2630,21 @@ NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
        return status;
 }
 
+/****************************************************************
+****************************************************************/
+
+NTSTATUS _samr_QueryUserInfo2(pipes_struct *p,
+                             struct samr_QueryUserInfo2 *r)
+{
+       struct samr_QueryUserInfo u;
+
+       u.in.user_handle        = r->in.user_handle;
+       u.in.level              = r->in.level;
+       u.out.info              = r->out.info;
+
+       return _samr_QueryUserInfo(p, &u);
+}
+
 /*******************************************************************
  _samr_GetGroupsForUser
  ********************************************************************/
@@ -2762,7 +2692,7 @@ NTSTATUS _samr_GetGroupsForUser(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        result = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_USER_GET_GROUPS,
+                                           SAMR_USER_ACCESS_GET_GROUPS,
                                            "_samr_GetGroupsForUser");
        if (!NT_STATUS_IS_OK(result)) {
                return result;
@@ -2859,23 +2789,16 @@ NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
        NTSTATUS status = NT_STATUS_OK;
        struct samr_info *info = NULL;
        union samr_DomainInfo *dom_info;
-       uint32 min_pass_len,pass_hist,password_properties;
        time_t u_expire, u_min_age;
-       NTTIME nt_expire, nt_min_age;
 
        time_t u_lock_duration, u_reset_time;
-       NTTIME nt_lock_duration, nt_reset_time;
-       uint32 lockout;
-       time_t u_logout;
-       NTTIME nt_logout;
+       uint32_t u_logout;
 
        uint32 account_policy_temp;
 
        time_t seq_num;
        uint32 server_role;
 
-       uint32 num_users=0, num_groups=0, num_aliases=0;
-
        DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
 
        dom_info = TALLOC_ZERO_P(p->mem_ctx, union samr_DomainInfo);
@@ -2889,7 +2812,7 @@ NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
        }
 
        status = access_check_samr_function(info->acc_granted,
-                                           SA_RIGHT_SAM_OPEN_DOMAIN,
+                                           SAMR_ACCESS_OPEN_DOMAIN,
                                            "_samr_QueryDomainInfo" );
 
        if ( !NT_STATUS_IS_OK(status) )
@@ -2902,14 +2825,15 @@ NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
 
                        /* AS ROOT !!! */
 
-                       pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
-                       min_pass_len = account_policy_temp;
+                       pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
+                                              &account_policy_temp);
+                       dom_info->info1.min_password_length = account_policy_temp;
 
                        pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
-                       pass_hist = account_policy_temp;
+                       dom_info->info1.password_history_length = account_policy_temp;
 
-                       pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
-                       password_properties = account_policy_temp;
+                       pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
+                               &dom_info->info1.password_properties);
 
                        pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
                        u_expire = account_policy_temp;
@@ -2921,19 +2845,13 @@ NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
 
                        unbecome_root();
 
-                       unix_to_nt_time_abs(&nt_expire, u_expire);
-                       unix_to_nt_time_abs(&nt_min_age, u_min_age);
+                       unix_to_nt_time_abs((NTTIME *)&dom_info->info1.max_password_age, u_expire);
+                       unix_to_nt_time_abs((NTTIME *)&dom_info->info1.min_password_age, u_min_age);
 
                        if (lp_check_password_script() && *lp_check_password_script()) {
-                               password_properties |= DOMAIN_PASSWORD_COMPLEX;
+                               dom_info->info1.password_properties |= DOMAIN_PASSWORD_COMPLEX;
                        }
 
-                       init_samr_DomInfo1(&dom_info->info1,
-                                          (uint16)min_pass_len,
-                                          (uint16)pass_hist,
-                                          password_properties,
-                                          nt_expire,
-                                          nt_min_age);
                        break;
                case 0x02:
 
@@ -2941,14 +2859,13 @@ NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
 
                        /* AS ROOT !!! */
 
-                       num_users = count_sam_users(info->disp_info, ACB_NORMAL);
-                       num_groups = count_sam_groups(info->disp_info);
-                       num_aliases = count_sam_aliases(info->disp_info);
+                       dom_info->general.num_users     = count_sam_users(info->disp_info, ACB_NORMAL);
+                       dom_info->general.num_groups    = count_sam_groups(info->disp_info);
+                       dom_info->general.num_aliases   = count_sam_aliases(info->disp_info);
 
-                       pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
-                       u_logout = account_policy_temp;
+                       pdb_get_account_policy(AP_TIME_TO_LOGOUT, &u_logout);
 
-                       unix_to_nt_time_abs(&nt_logout, u_logout);
+                       unix_to_nt_time_abs(&dom_info->general.force_logoff_time, u_logout);
 
                        if (!pdb_get_seq_num(&seq_num))
                                seq_num = time(NULL);
@@ -2961,18 +2878,14 @@ NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
                        if (lp_server_role() == ROLE_DOMAIN_BDC)
                                server_role = ROLE_DOMAIN_BDC;
 
-                       init_samr_DomGeneralInformation(&dom_info->general,
-                                                       nt_logout,
-                                                       lp_serverstring(),
-                                                       lp_workgroup(),
-                                                       global_myname(),
-                                                       seq_num,
-                                                       1,
-                                                       server_role,
-                                                       1,
-                                                       num_users,
-                                                       num_groups,
-                                                       num_aliases);
+                       dom_info->general.oem_information.string        = lp_serverstring();
+                       dom_info->general.domain_name.string            = lp_workgroup();
+                       dom_info->general.primary.string                = global_myname();
+                       dom_info->general.sequence_num                  = seq_num;
+                       dom_info->general.domain_server_state           = DOMAIN_SERVER_ENABLED;
+                       dom_info->general.role                          = server_role;
+                       dom_info->general.unknown3                      = 1;
+
                        break;
                case 0x03:
 
@@ -2990,34 +2903,27 @@ NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
 
                        unbecome_root();
 
-                       unix_to_nt_time_abs(&nt_logout, u_logout);
-
-                       init_samr_DomInfo3(&dom_info->info3,
-                                          nt_logout);
+                       unix_to_nt_time_abs(&dom_info->info3.force_logoff_time, u_logout);
 
                        break;
                case 0x04:
-                       init_samr_DomOEMInformation(&dom_info->oem,
-                                                   lp_serverstring());
+                       dom_info->oem.oem_information.string = lp_serverstring();
                        break;
                case 0x05:
-                       init_samr_DomInfo5(&dom_info->info5,
-                                          get_global_sam_name());
+                       dom_info->info5.domain_name.string = get_global_sam_name();
                        break;
                case 0x06:
                        /* NT returns its own name when a PDC. win2k and later
                         * only the name of the PDC if itself is a BDC (samba4
                         * idl) */
-                       init_samr_DomInfo6(&dom_info->info6,
-                                          global_myname());
+                       dom_info->info6.primary.string = global_myname();
                        break;
                case 0x07:
                        server_role = ROLE_DOMAIN_PDC;
                        if (lp_server_role() == ROLE_DOMAIN_BDC)
                                server_role = ROLE_DOMAIN_BDC;
 
-                       init_samr_DomInfo7(&dom_info->info7,
-                                          server_role);
+                       dom_info->info7.role = server_role;
                        break;
                case 0x08:
 
@@ -3033,9 +2939,9 @@ NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
 
                        unbecome_root();
 
-                       init_samr_DomInfo8(&dom_info->info8,
-                                          seq_num,
-                                          0);
+                       dom_info->info8.sequence_num = seq_num;
+                       dom_info->info8.domain_create_time = 0;
+
                        break;
                case 0x0c:
 
@@ -3052,21 +2958,20 @@ NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
                        pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
                        u_reset_time = account_policy_temp * 60;
 
-                       pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
-                       lockout = account_policy_temp;
+                       pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT,
+                                              &account_policy_temp);
+                       dom_info->info12.lockout_threshold = account_policy_temp;
 
                        /* !AS ROOT */
 
                        unbecome_root();
 
-                       unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
-                       unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
+                       unix_to_nt_time_abs(&dom_info->info12.lockout_duration,
+                                           u_lock_duration);
+                       unix_to_nt_time_abs(&dom_info->info12.lockout_window,
+                                           u_reset_time);
 
-                       init_samr_DomInfo12(&dom_info->info12,
-                                           nt_lock_duration,
-                                           nt_reset_time,
-                                           (uint16)lockout);
-                       break;
+                       break;
                default:
                        return NT_STATUS_INVALID_INFO_CLASS;
        }
@@ -3125,9 +3030,7 @@ NTSTATUS _samr_CreateUser2(pipes_struct *p,
 {
        const char *account = NULL;
        DOM_SID sid;
-       POLICY_HND dom_pol = *r->in.domain_handle;
        uint32_t acb_info = r->in.acct_flags;
-       POLICY_HND *user_pol = r->out.user_handle;
        struct samr_info *info = NULL;
        NTSTATUS nt_status;
        uint32 acc_granted;
@@ -3140,12 +3043,17 @@ NTSTATUS _samr_CreateUser2(pipes_struct *p,
        DISP_INFO *disp_info = NULL;
 
        /* Get the domain SID stored in the domain policy */
-       if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted,
+       if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &sid, &acc_granted,
                                     &disp_info))
                return NT_STATUS_INVALID_HANDLE;
 
+       if (disp_info->builtin_domain) {
+               DEBUG(5,("_samr_CreateUser2: Refusing user create in BUILTIN\n"));
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
        nt_status = access_check_samr_function(acc_granted,
-                                              SA_RIGHT_DOMAIN_CREATE_USER,
+                                              SAMR_DOMAIN_ACCESS_CREATE_USER,
                                               "_samr_CreateUser2");
        if (!NT_STATUS_IS_OK(nt_status)) {
                return nt_status;
@@ -3174,7 +3082,7 @@ NTSTATUS _samr_CreateUser2(pipes_struct *p,
        {
                se_priv_copy( &se_rights, &se_machine_account );
                can_add_account = user_has_privileges(
-                       p->pipe_user.nt_user_token, &se_rights );
+                       p->server_info->ptok, &se_rights );
        }
        /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
           account for domain trusts and changes the ACB flags later */
@@ -3183,7 +3091,7 @@ NTSTATUS _samr_CreateUser2(pipes_struct *p,
        {
                se_priv_copy( &se_rights, &se_add_users );
                can_add_account = user_has_privileges(
-                       p->pipe_user.nt_user_token, &se_rights );
+                       p->server_info->ptok, &se_rights );
        }
        else    /* implicit assumption of a BDC or domain trust account here
                 * (we already check the flags earlier) */
@@ -3192,13 +3100,13 @@ NTSTATUS _samr_CreateUser2(pipes_struct *p,
                        /* only Domain Admins can add a BDC or domain trust */
                        se_priv_copy( &se_rights, &se_priv_none );
                        can_add_account = nt_token_check_domain_rid(
-                               p->pipe_user.nt_user_token,
+                               p->server_info->ptok,
                                DOMAIN_GROUP_RID_ADMINS );
                }
        }
 
        DEBUG(5, ("_samr_CreateUser2: %s can add this account : %s\n",
-                 uidtoname(p->pipe_user.ut.uid),
+                 uidtoname(p->server_info->utok.uid),
                  can_add_account ? "True":"False" ));
 
        /********** BEGIN Admin BLOCK **********/
@@ -3223,13 +3131,13 @@ NTSTATUS _samr_CreateUser2(pipes_struct *p,
 
        sid_compose(&sid, get_global_sam_sid(), *r->out.rid);
 
-       map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
+       map_max_allowed_access(p->server_info->ptok, &des_access);
 
        make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
                            &sid, SAMR_USR_RIGHTS_WRITE_PW);
        se_map_generic(&des_access, &usr_generic_mapping);
 
-       nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
+       nt_status = access_check_samr_object(psd, p->server_info->ptok,
                &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
                &acc_granted, "_samr_CreateUser2");
 
@@ -3238,7 +3146,7 @@ NTSTATUS _samr_CreateUser2(pipes_struct *p,
        }
 
        /* associate the user's SID with the new handle. */
-       if ((info = get_samr_info_by_sid(&sid)) == NULL) {
+       if ((info = get_samr_info_by_sid(p->mem_ctx, &sid)) == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -3247,7 +3155,7 @@ NTSTATUS _samr_CreateUser2(pipes_struct *p,
        info->acc_granted = acc_granted;
 
        /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
+       if (!create_policy_hnd(p, r->out.user_handle, info)) {
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
@@ -3259,6 +3167,26 @@ NTSTATUS _samr_CreateUser2(pipes_struct *p,
        return NT_STATUS_OK;
 }
 
+/****************************************************************
+****************************************************************/
+
+NTSTATUS _samr_CreateUser(pipes_struct *p,
+                         struct samr_CreateUser *r)
+{
+       struct samr_CreateUser2 c;
+       uint32_t access_granted;
+
+       c.in.domain_handle      = r->in.domain_handle;
+       c.in.account_name       = r->in.account_name;
+       c.in.acct_flags         = ACB_NORMAL;
+       c.in.access_mask        = r->in.access_mask;
+       c.out.user_handle       = r->out.user_handle;
+       c.out.access_granted    = &access_granted;
+       c.out.rid               = r->out.rid;
+
+       return _samr_CreateUser2(p, &c);
+}
+
 /*******************************************************************
  _samr_Connect
  ********************************************************************/
@@ -3279,20 +3207,20 @@ NTSTATUS _samr_Connect(pipes_struct *p,
        /* set up the SAMR connect_anon response */
 
        /* associate the user's SID with the new handle. */
-       if ((info = get_samr_info_by_sid(NULL)) == NULL)
+       if ((info = get_samr_info_by_sid(p->mem_ctx, NULL)) == NULL)
                return NT_STATUS_NO_MEMORY;
 
-       /* don't give away the farm but this is probably ok.  The SA_RIGHT_SAM_ENUM_DOMAINS
+       /* don't give away the farm but this is probably ok.  The SAMR_ACCESS_ENUM_DOMAINS
           was observed from a win98 client trying to enumerate users (when configured
           user level access control on shares)   --jerry */
 
-       map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
+       map_max_allowed_access(p->server_info->ptok, &des_access);
 
        se_map_generic( &des_access, &sam_generic_mapping );
-       info->acc_granted = des_access & (SA_RIGHT_SAM_ENUM_DOMAINS|SA_RIGHT_SAM_OPEN_DOMAIN);
+       info->acc_granted = des_access & (SAMR_ACCESS_ENUM_DOMAINS|SAMR_ACCESS_OPEN_DOMAIN);
 
        /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
+       if (!create_policy_hnd(p, r->out.connect_handle, info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
        return NT_STATUS_OK;
@@ -3311,40 +3239,52 @@ NTSTATUS _samr_Connect2(pipes_struct *p,
        uint32    des_access = r->in.access_mask;
        NTSTATUS  nt_status;
        size_t    sd_size;
+       const char *fn = "_samr_Connect2";
 
+       switch (p->hdr_req.opnum) {
+       case NDR_SAMR_CONNECT2:
+               fn = "_samr_Connect2";
+               break;
+       case NDR_SAMR_CONNECT4:
+               fn = "_samr_Connect4";
+               break;
+       case NDR_SAMR_CONNECT5:
+               fn = "_samr_Connect5";
+               break;
+       }
 
-       DEBUG(5,("_samr_Connect2: %d\n", __LINE__));
+       DEBUG(5,("%s: %d\n", fn, __LINE__));
 
        /* Access check */
 
        if (!pipe_access_check(p)) {
-               DEBUG(3, ("access denied to _samr_Connect2\n"));
+               DEBUG(3, ("access denied to %s\n", fn));
                return NT_STATUS_ACCESS_DENIED;
        }
 
-       map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
+       map_max_allowed_access(p->server_info->ptok, &des_access);
 
        make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
        se_map_generic(&des_access, &sam_generic_mapping);
 
-       nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
-               NULL, 0, des_access, &acc_granted, "_samr_Connect2");
+       nt_status = access_check_samr_object(psd, p->server_info->ptok,
+               NULL, 0, des_access, &acc_granted, fn);
 
        if ( !NT_STATUS_IS_OK(nt_status) )
                return nt_status;
 
        /* associate the user's SID and access granted with the new handle. */
-       if ((info = get_samr_info_by_sid(NULL)) == NULL)
+       if ((info = get_samr_info_by_sid(p->mem_ctx, NULL)) == NULL)
                return NT_STATUS_NO_MEMORY;
 
        info->acc_granted = acc_granted;
        info->status = r->in.access_mask; /* this looks so wrong... - gd */
 
        /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
+       if (!create_policy_hnd(p, r->out.connect_handle, info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
-       DEBUG(5,("_samr_Connect2: %d\n", __LINE__));
+       DEBUG(5,("%s: %d\n", fn, __LINE__));
 
        return nt_status;
 }
@@ -3356,48 +3296,13 @@ NTSTATUS _samr_Connect2(pipes_struct *p,
 NTSTATUS _samr_Connect4(pipes_struct *p,
                        struct samr_Connect4 *r)
 {
-       struct samr_info *info = NULL;
-       SEC_DESC *psd = NULL;
-       uint32    acc_granted;
-       uint32    des_access = r->in.access_mask;
-       NTSTATUS  nt_status;
-       size_t    sd_size;
-
-
-       DEBUG(5,("_samr_Connect4: %d\n", __LINE__));
-
-       /* Access check */
-
-       if (!pipe_access_check(p)) {
-               DEBUG(3, ("access denied to samr_Connect4\n"));
-               return NT_STATUS_ACCESS_DENIED;
-       }
-
-       map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
-
-       make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
-       se_map_generic(&des_access, &sam_generic_mapping);
-
-       nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
-               NULL, 0, des_access, &acc_granted, "_samr_Connect4");
-
-       if ( !NT_STATUS_IS_OK(nt_status) )
-               return nt_status;
-
-       /* associate the user's SID and access granted with the new handle. */
-       if ((info = get_samr_info_by_sid(NULL)) == NULL)
-               return NT_STATUS_NO_MEMORY;
+       struct samr_Connect2 c;
 
-       info->acc_granted = acc_granted;
-       info->status = r->in.access_mask; /* ??? */
-
-       /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
-               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       c.in.system_name        = r->in.system_name;
+       c.in.access_mask        = r->in.access_mask;
+       c.out.connect_handle    = r->out.connect_handle;
 
-       DEBUG(5,("_samr_Connect4: %d\n", __LINE__));
-
-       return NT_STATUS_OK;
+       return _samr_Connect2(p, &c);
 }
 
 /*******************************************************************
@@ -3407,50 +3312,22 @@ NTSTATUS _samr_Connect4(pipes_struct *p,
 NTSTATUS _samr_Connect5(pipes_struct *p,
                        struct samr_Connect5 *r)
 {
-       struct samr_info *info = NULL;
-       SEC_DESC *psd = NULL;
-       uint32    acc_granted;
-       uint32    des_access = r->in.access_mask;
-       NTSTATUS  nt_status;
-       size_t    sd_size;
+       NTSTATUS status;
+       struct samr_Connect2 c;
        struct samr_ConnectInfo1 info1;
 
-       DEBUG(5,("_samr_Connect5: %d\n", __LINE__));
+       info1.client_version = SAMR_CONNECT_AFTER_W2K;
+       info1.unknown2 = 0;
 
-       /* Access check */
+       c.in.system_name        = r->in.system_name;
+       c.in.access_mask        = r->in.access_mask;
+       c.out.connect_handle    = r->out.connect_handle;
 
-       if (!pipe_access_check(p)) {
-               DEBUG(3, ("access denied to samr_Connect5\n"));
-               return NT_STATUS_ACCESS_DENIED;
+       status = _samr_Connect2(p, &c);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
-       map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
-
-       make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
-       se_map_generic(&des_access, &sam_generic_mapping);
-
-       nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
-               NULL, 0, des_access, &acc_granted, "_samr_Connect5");
-
-       if ( !NT_STATUS_IS_OK(nt_status) )
-               return nt_status;
-
-       /* associate the user's SID and access granted with the new handle. */
-       if ((info = get_samr_info_by_sid(NULL)) == NULL)
-               return NT_STATUS_NO_MEMORY;
-
-       info->acc_granted = acc_granted;
-       info->status = r->in.access_mask; /* ??? */
-
-       /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
-               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
-
-       DEBUG(5,("_samr_Connect5: %d\n", __LINE__));
-
-       info1.client_version = SAMR_CONNECT_AFTER_W2K;
-       info1.unknown2 = 0;
-
        *r->out.level_out = 1;
        r->out.info_out->info1 = info1;
 
@@ -3472,17 +3349,20 @@ NTSTATUS _samr_LookupDomain(pipes_struct *p,
        if (!find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info))
                return NT_STATUS_INVALID_HANDLE;
 
-       /* win9x user manager likes to use SA_RIGHT_SAM_ENUM_DOMAINS here.
+       /* win9x user manager likes to use SAMR_ACCESS_ENUM_DOMAINS here.
           Reverted that change so we will work with RAS servers again */
 
        status = access_check_samr_function(info->acc_granted,
-                                           SA_RIGHT_SAM_OPEN_DOMAIN,
+                                           SAMR_ACCESS_OPEN_DOMAIN,
                                            "_samr_LookupDomain");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
        domain_name = r->in.domain_name->string;
+       if (!domain_name) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
        sid = TALLOC_ZERO_P(p->mem_ctx, struct dom_sid2);
        if (!sid) {
@@ -3522,7 +3402,7 @@ NTSTATUS _samr_EnumDomains(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(info->acc_granted,
-                                           SA_RIGHT_SAM_ENUM_DOMAINS,
+                                           SAMR_ACCESS_ENUM_DOMAINS,
                                            "_samr_EnumDomains");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -3563,9 +3443,7 @@ NTSTATUS _samr_OpenAlias(pipes_struct *p,
                         struct samr_OpenAlias *r)
 {
        DOM_SID sid;
-       POLICY_HND domain_pol = *r->in.domain_handle;
        uint32 alias_rid = r->in.rid;
-       POLICY_HND *alias_pol = r->out.alias_handle;
        struct    samr_info *info = NULL;
        SEC_DESC *psd = NULL;
        uint32    acc_granted;
@@ -3576,11 +3454,11 @@ NTSTATUS _samr_OpenAlias(pipes_struct *p,
 
        /* find the domain policy and get the SID / access bits stored in the domain policy */
 
-       if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
+       if ( !get_lsa_policy_samr_sid(p, r->in.domain_handle, &sid, &acc_granted, NULL) )
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_DOMAIN_OPEN_ACCOUNT,
+                                           SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
                                            "_samr_OpenAlias");
 
        if ( !NT_STATUS_IS_OK(status) )
@@ -3593,7 +3471,7 @@ NTSTATUS _samr_OpenAlias(pipes_struct *p,
 
        /*check if access can be granted as requested by client. */
 
-       map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
+       map_max_allowed_access(p->server_info->ptok, &des_access);
 
        make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
        se_map_generic(&des_access,&ali_generic_mapping);
@@ -3601,7 +3479,7 @@ NTSTATUS _samr_OpenAlias(pipes_struct *p,
        se_priv_copy( &se_rights, &se_add_users );
 
 
-       status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
+       status = access_check_samr_object(psd, p->server_info->ptok,
                &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access,
                &acc_granted, "_samr_OpenAlias");
 
@@ -3631,13 +3509,13 @@ NTSTATUS _samr_OpenAlias(pipes_struct *p,
        }
 
        /* associate the alias SID with the new handle. */
-       if ((info = get_samr_info_by_sid(&sid)) == NULL)
+       if ((info = get_samr_info_by_sid(p->mem_ctx, &sid)) == NULL)
                return NT_STATUS_NO_MEMORY;
 
        info->acc_granted = acc_granted;
 
        /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
+       if (!create_policy_hnd(p, r->out.alias_handle, info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
        return NT_STATUS_OK;
@@ -3655,13 +3533,11 @@ static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
 
        if (id7 == NULL) {
                DEBUG(5, ("set_user_info_7: NULL id7\n"));
-               TALLOC_FREE(pwd);
                return NT_STATUS_ACCESS_DENIED;
        }
 
        if (!id7->account_name.string) {
                DEBUG(5, ("set_user_info_7: failed to get new username\n"));
-               TALLOC_FREE(pwd);
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -3681,7 +3557,6 @@ static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
 
        rc = pdb_rename_sam_account(pwd, id7->account_name.string);
 
-       TALLOC_FREE(pwd);
        return rc;
 }
 
@@ -3694,23 +3569,18 @@ static bool set_user_info_16(struct samr_UserInfo16 *id16,
 {
        if (id16 == NULL) {
                DEBUG(5, ("set_user_info_16: NULL id16\n"));
-               TALLOC_FREE(pwd);
                return False;
        }
 
        /* FIX ME: check if the value is really changed --metze */
        if (!pdb_set_acct_ctrl(pwd, id16->acct_flags, PDB_CHANGED)) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
        if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
-       TALLOC_FREE(pwd);
-
        return True;
 }
 
@@ -3718,35 +3588,57 @@ static bool set_user_info_16(struct samr_UserInfo16 *id16,
  set_user_info_18
  ********************************************************************/
 
-static bool set_user_info_18(struct samr_UserInfo18 *id18,
-                            struct samu *pwd)
+static NTSTATUS set_user_info_18(struct samr_UserInfo18 *id18,
+                                TALLOC_CTX *mem_ctx,
+                                DATA_BLOB *session_key,
+                                struct samu *pwd)
 {
        if (id18 == NULL) {
                DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
-               TALLOC_FREE(pwd);
-               return False;
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd.hash, PDB_CHANGED)) {
-               TALLOC_FREE(pwd);
-               return False;
+       if (id18->nt_pwd_active || id18->lm_pwd_active) {
+               if (!session_key->length) {
+                       return NT_STATUS_NO_USER_SESSION_KEY;
+               }
        }
-       if (!pdb_set_nt_passwd     (pwd, id18->nt_pwd.hash, PDB_CHANGED)) {
-               TALLOC_FREE(pwd);
-               return False;
+
+       if (id18->nt_pwd_active) {
+
+               DATA_BLOB in, out;
+
+               in = data_blob_const(id18->nt_pwd.hash, 16);
+               out = data_blob_talloc_zero(mem_ctx, 16);
+
+               sess_crypt_blob(&out, &in, session_key, false);
+
+               if (!pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED)) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+
+               pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
        }
-       if (!pdb_set_pass_last_set_time (pwd, time(NULL), PDB_CHANGED)) {
-               TALLOC_FREE(pwd);
-               return False;
+
+       if (id18->lm_pwd_active) {
+
+               DATA_BLOB in, out;
+
+               in = data_blob_const(id18->lm_pwd.hash, 16);
+               out = data_blob_talloc_zero(mem_ctx, 16);
+
+               sess_crypt_blob(&out, &in, session_key, false);
+
+               if (!pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED)) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+
+               pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
        }
 
-       if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
-               return False;
-       }
+       copy_id18_to_sam_passwd(pwd, id18);
 
-       TALLOC_FREE(pwd);
-       return True;
+       return pdb_update_sam_account(pwd);
 }
 
 /*******************************************************************
@@ -3765,12 +3657,9 @@ static bool set_user_info_20(struct samr_UserInfo20 *id20,
 
        /* write the change out */
        if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
-       TALLOC_FREE(pwd);
-
        return True;
 }
 
@@ -3778,8 +3667,9 @@ static bool set_user_info_20(struct samr_UserInfo20 *id20,
  set_user_info_21
  ********************************************************************/
 
-static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx,
-                                struct samr_UserInfo21 *id21,
+static NTSTATUS set_user_info_21(struct samr_UserInfo21 *id21,
+                                TALLOC_CTX *mem_ctx,
+                                DATA_BLOB *session_key,
                                 struct samu *pwd)
 {
        NTSTATUS status;
@@ -3789,6 +3679,60 @@ static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       if (id21->fields_present == 0) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (id21->fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       if (id21->fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) {
+               if (id21->nt_password_set) {
+                       DATA_BLOB in, out;
+
+                       if ((id21->nt_owf_password.length != 16) ||
+                           (id21->nt_owf_password.size != 16)) {
+                               return NT_STATUS_INVALID_PARAMETER;
+                       }
+
+                       if (!session_key->length) {
+                               return NT_STATUS_NO_USER_SESSION_KEY;
+                       }
+
+                       in = data_blob_const(id21->nt_owf_password.array, 16);
+                       out = data_blob_talloc_zero(mem_ctx, 16);
+
+                       sess_crypt_blob(&out, &in, session_key, false);
+
+                       pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED);
+                       pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
+               }
+       }
+
+       if (id21->fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT) {
+               if (id21->lm_password_set) {
+                       DATA_BLOB in, out;
+
+                       if ((id21->lm_owf_password.length != 16) ||
+                           (id21->lm_owf_password.size != 16)) {
+                               return NT_STATUS_INVALID_PARAMETER;
+                       }
+
+                       if (!session_key->length) {
+                               return NT_STATUS_NO_USER_SESSION_KEY;
+                       }
+
+                       in = data_blob_const(id21->lm_owf_password.array, 16);
+                       out = data_blob_talloc_zero(mem_ctx, 16);
+
+                       sess_crypt_blob(&out, &in, session_key, false);
+
+                       pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED);
+                       pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
+               }
+       }
+
        /* we need to separately check for an account rename first */
 
        if (id21->account_name.string &&
@@ -3814,7 +3758,6 @@ static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx,
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
                                nt_errstr(status)));
-                       TALLOC_FREE(pwd);
                        return status;
                }
 
@@ -3845,12 +3788,9 @@ static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx,
 
        /* write the change out */
        if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
                return status;
        }
 
-       TALLOC_FREE(pwd);
-
        return NT_STATUS_OK;
 }
 
@@ -3864,7 +3804,7 @@ static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
 {
        char *plaintext_buf = NULL;
        uint32 len = 0;
-       uint16 acct_ctrl;
+       uint32_t acct_ctrl;
        NTSTATUS status;
 
        if (id23 == NULL) {
@@ -3872,39 +3812,48 @@ static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
-                 pdb_get_username(pwd)));
-
-       acct_ctrl = pdb_get_acct_ctrl(pwd);
-
-       if (!decode_pw_buffer(mem_ctx,
-                               id23->password.data,
-                               &plaintext_buf,
-                               &len,
-                               STR_UNICODE)) {
-               TALLOC_FREE(pwd);
+       if (id23->info.fields_present == 0) {
                return NT_STATUS_INVALID_PARAMETER;
-       }
+       }
 
-       if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
-               TALLOC_FREE(pwd);
+       if (id23->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
                return NT_STATUS_ACCESS_DENIED;
        }
 
+       if ((id23->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
+           (id23->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
+
+               DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
+                         pdb_get_username(pwd)));
+
+               if (!decode_pw_buffer(mem_ctx,
+                                     id23->password.data,
+                                     &plaintext_buf,
+                                     &len,
+                                     STR_UNICODE)) {
+                       return NT_STATUS_WRONG_PASSWORD;
+               }
+
+               if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+       }
+
        copy_id23_to_sam_passwd(pwd, id23);
 
+       acct_ctrl = pdb_get_acct_ctrl(pwd);
+
        /* if it's a trust account, don't update /etc/passwd */
        if (    ( (acct_ctrl &  ACB_DOMTRUST) == ACB_DOMTRUST ) ||
                ( (acct_ctrl &  ACB_WSTRUST) ==  ACB_WSTRUST) ||
                ( (acct_ctrl &  ACB_SVRTRUST) ==  ACB_SVRTRUST) ) {
                DEBUG(5, ("Changing trust account.  Not updating /etc/passwd\n"));
-       } else  {
+       } else if (plaintext_buf) {
                /* update the UNIX password */
                if (lp_unix_password_sync() ) {
                        struct passwd *passwd;
                        if (pdb_get_username(pwd) == NULL) {
                                DEBUG(1, ("chgpasswd: User without name???\n"));
-                               TALLOC_FREE(pwd);
                                return NT_STATUS_ACCESS_DENIED;
                        }
 
@@ -3914,29 +3863,26 @@ static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
                        }
 
                        if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
-                               TALLOC_FREE(pwd);
                                return NT_STATUS_ACCESS_DENIED;
                        }
                        TALLOC_FREE(passwd);
                }
        }
 
-       memset(plaintext_buf, '\0', strlen(plaintext_buf));
+       if (plaintext_buf) {
+               memset(plaintext_buf, '\0', strlen(plaintext_buf));
+       }
 
        if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
            (!NT_STATUS_IS_OK(status =  pdb_set_unix_primary_group(mem_ctx,
                                                                   pwd)))) {
-               TALLOC_FREE(pwd);
                return status;
        }
 
        if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
                return status;
        }
 
-       TALLOC_FREE(pwd);
-
        return NT_STATUS_OK;
 }
 
@@ -3944,35 +3890,26 @@ static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
  set_user_info_pw
  ********************************************************************/
 
-static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
-                            int level)
+static bool set_user_info_pw(uint8 *pass, struct samu *pwd)
 {
        uint32 len = 0;
        char *plaintext_buf = NULL;
        uint32 acct_ctrl;
-       time_t last_set_time;
-       enum pdb_value_state last_set_state;
 
        DEBUG(5, ("Attempting administrator password change for user %s\n",
                  pdb_get_username(pwd)));
 
        acct_ctrl = pdb_get_acct_ctrl(pwd);
-       /* we need to know if it's expired, because this is an admin change, not a
-          user change, so it's still expired when we're done */
-       last_set_state = pdb_get_init_flags(pwd, PDB_PASSLASTSET);
-       last_set_time = pdb_get_pass_last_set_time(pwd);
 
        if (!decode_pw_buffer(talloc_tos(),
                                pass,
                                &plaintext_buf,
                                &len,
                                STR_UNICODE)) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
        if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
-               TALLOC_FREE(pwd);
                return False;
        }
 
@@ -3988,7 +3925,6 @@ static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
 
                        if (pdb_get_username(pwd) == NULL) {
                                DEBUG(1, ("chgpasswd: User without name???\n"));
-                               TALLOC_FREE(pwd);
                                return False;
                        }
 
@@ -3998,7 +3934,6 @@ static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
                        }
 
                        if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
-                               TALLOC_FREE(pwd);
                                return False;
                        }
                        TALLOC_FREE(passwd);
@@ -4007,32 +3942,38 @@ static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
 
        memset(plaintext_buf, '\0', strlen(plaintext_buf));
 
-       /*
-        * A level 25 change does reset the pwdlastset field, a level 24
-        * change does not. I know this is probably not the full story, but
-        * it is needed to make XP join LDAP correctly, without it the later
-        * auth2 check can fail with PWD_MUST_CHANGE.
-        */
-       if (level != 25) {
-               /*
-                * restore last set time as this is an admin change, not a
-                * user pw change
-                */
-               pdb_set_pass_last_set_time (pwd, last_set_time,
-                                           last_set_state);
+       DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
+
+       return True;
+}
+
+/*******************************************************************
+ set_user_info_24
+ ********************************************************************/
+
+static NTSTATUS set_user_info_24(TALLOC_CTX *mem_ctx,
+                                struct samr_UserInfo24 *id24,
+                                struct samu *pwd)
+{
+       NTSTATUS status;
+
+       if (id24 == NULL) {
+               DEBUG(5, ("set_user_info_24: NULL id24\n"));
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
+       if (!set_user_info_pw(id24->password.data, pwd)) {
+               return NT_STATUS_WRONG_PASSWORD;
+       }
 
-       /* update the SAMBA password */
-       if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
-               return False;
-       }
+       copy_id24_to_sam_passwd(pwd, id24);
 
-       TALLOC_FREE(pwd);
+       status = pdb_update_sam_account(pwd);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
-       return True;
+       return NT_STATUS_OK;
 }
 
 /*******************************************************************
@@ -4050,11 +3991,26 @@ static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       if (id25->info.fields_present == 0) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (id25->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       if ((id25->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
+           (id25->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
+
+               if (!set_user_info_pw(id25->password.data, pwd)) {
+                       return NT_STATUS_WRONG_PASSWORD;
+               }
+       }
+
        copy_id25_to_sam_passwd(pwd, id25);
 
        /* write the change out */
        if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
-               TALLOC_FREE(pwd);
                return status;
        }
 
@@ -4073,12 +4029,39 @@ static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
                }
        }
 
-       /* WARNING: No TALLOC_FREE(pwd), we are about to set the password
-        * hereafter! */
+       return NT_STATUS_OK;
+}
+
+/*******************************************************************
+ set_user_info_26
+ ********************************************************************/
+
+static NTSTATUS set_user_info_26(TALLOC_CTX *mem_ctx,
+                                struct samr_UserInfo26 *id26,
+                                struct samu *pwd)
+{
+       NTSTATUS status;
+
+       if (id26 == NULL) {
+               DEBUG(5, ("set_user_info_26: NULL id26\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (!set_user_info_pw(id26->password.data, pwd)) {
+               return NT_STATUS_WRONG_PASSWORD;
+       }
+
+       copy_id26_to_sam_passwd(pwd, id26);
+
+       status = pdb_update_sam_account(pwd);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
        return NT_STATUS_OK;
 }
 
+
 /*******************************************************************
  samr_SetUserInfo
  ********************************************************************/
@@ -4089,7 +4072,6 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
        NTSTATUS status;
        struct samu *pwd = NULL;
        DOM_SID sid;
-       POLICY_HND *pol = r->in.user_handle;
        union samr_UserInfo *info = r->in.info;
        uint16_t switch_value = r->in.level;
        uint32_t acc_granted;
@@ -4102,14 +4084,14 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
        DEBUG(5,("_samr_SetUserInfo: %d\n", __LINE__));
 
        /* find the policy handle.  open a policy on it. */
-       if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info)) {
+       if (!get_lsa_policy_samr_sid(p, r->in.user_handle, &sid, &acc_granted, &disp_info)) {
                return NT_STATUS_INVALID_HANDLE;
        }
 
        /* This is tricky.  A WinXP domain join sets
-         (SA_RIGHT_USER_SET_PASSWORD|SA_RIGHT_USER_SET_ATTRIBUTES|SA_RIGHT_USER_ACCT_FLAGS_EXPIRY)
+         (SAMR_USER_ACCESS_SET_PASSWORD|SAMR_USER_ACCESS_SET_ATTRIBUTES|SAMR_USER_ACCESS_GET_ATTRIBUTES)
          The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser().  But the
-         standard Win32 API calls just ask for SA_RIGHT_USER_SET_PASSWORD in the SamrOpenUser().
+         standard Win32 API calls just ask for SAMR_USER_ACCESS_SET_PASSWORD in the SamrOpenUser().
          This should be enough for levels 18, 24, 25,& 26.  Info level 23 can set more so
          we'll use the set from the WinXP join as the basis. */
 
@@ -4118,12 +4100,12 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
        case 24:
        case 25:
        case 26:
-               acc_required = SA_RIGHT_USER_SET_PASSWORD;
+               acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
                break;
        default:
-               acc_required = SA_RIGHT_USER_SET_PASSWORD |
-                              SA_RIGHT_USER_SET_ATTRIBUTES |
-                              SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
+               acc_required = SAMR_USER_ACCESS_SET_PASSWORD |
+                              SAMR_USER_ACCESS_SET_ATTRIBUTES |
+                              SAMR_USER_ACCESS_GET_ATTRIBUTES;
                break;
        }
 
@@ -4160,20 +4142,20 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
 
        acb_info = pdb_get_acct_ctrl(pwd);
        if (acb_info & ACB_WSTRUST)
-               has_enough_rights = user_has_privileges(p->pipe_user.nt_user_token,
+               has_enough_rights = user_has_privileges(p->server_info->ptok,
                                                        &se_machine_account);
        else if (acb_info & ACB_NORMAL)
-               has_enough_rights = user_has_privileges(p->pipe_user.nt_user_token,
+               has_enough_rights = user_has_privileges(p->server_info->ptok,
                                                        &se_add_users);
        else if (acb_info & (ACB_SVRTRUST|ACB_DOMTRUST)) {
                if (lp_enable_privileges()) {
-                       has_enough_rights = nt_token_check_domain_rid(p->pipe_user.nt_user_token,
+                       has_enough_rights = nt_token_check_domain_rid(p->server_info->ptok,
                                                                      DOMAIN_GROUP_RID_ADMINS);
                }
        }
 
        DEBUG(5, ("_samr_SetUserInfo: %s does%s possess sufficient rights\n",
-                 uidtoname(p->pipe_user.ut.uid),
+                 uidtoname(p->server_info->utok.uid),
                  has_enough_rights ? "" : " not"));
 
        /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
@@ -4199,9 +4181,10 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
 
                case 18:
                        /* Used by AS/U JRA. */
-                       if (!set_user_info_18(&info->info18, pwd)) {
-                               status = NT_STATUS_ACCESS_DENIED;
-                       }
+                       status = set_user_info_18(&info->info18,
+                                                 p->mem_ctx,
+                                                 &p->server_info->user_session_key,
+                                                 pwd);
                        break;
 
                case 20:
@@ -4211,8 +4194,10 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
                        break;
 
                case 21:
-                       status = set_user_info_21(p->mem_ctx,
-                                                 &info->info21, pwd);
+                       status = set_user_info_21(&info->info21,
+                                                 p->mem_ctx,
+                                                 &p->server_info->user_session_key,
+                                                 pwd);
                        break;
 
                case 23:
@@ -4238,10 +4223,8 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
 
                        dump_data(100, info->info24.password.data, 516);
 
-                       if (!set_user_info_pw(info->info24.password.data, pwd,
-                                             switch_value)) {
-                               status = NT_STATUS_ACCESS_DENIED;
-                       }
+                       status = set_user_info_24(p->mem_ctx,
+                                                 &info->info24, pwd);
                        break;
 
                case 25:
@@ -4256,13 +4239,6 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
 
                        status = set_user_info_25(p->mem_ctx,
                                                  &info->info25, pwd);
-                       if (!NT_STATUS_IS_OK(status)) {
-                               goto done;
-                       }
-                       if (!set_user_info_pw(info->info25.password.data, pwd,
-                                             switch_value)) {
-                               status = NT_STATUS_ACCESS_DENIED;
-                       }
                        break;
 
                case 26:
@@ -4275,17 +4251,15 @@ NTSTATUS _samr_SetUserInfo(pipes_struct *p,
 
                        dump_data(100, info->info26.password.data, 516);
 
-                       if (!set_user_info_pw(info->info26.password.data, pwd,
-                                             switch_value)) {
-                               status = NT_STATUS_ACCESS_DENIED;
-                       }
+                       status = set_user_info_26(p->mem_ctx,
+                                                 &info->info26, pwd);
                        break;
 
                default:
                        status = NT_STATUS_INVALID_INFO_CLASS;
        }
 
- done:
+       TALLOC_FREE(pwd);
 
        if (has_enough_rights) {
                unbecome_root();
@@ -4340,10 +4314,10 @@ NTSTATUS _samr_GetAliasMembership(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        ntstatus1 = access_check_samr_function(info->acc_granted,
-                                              SA_RIGHT_DOMAIN_LOOKUP_ALIAS_BY_MEM,
+                                              SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS,
                                               "_samr_GetAliasMembership");
        ntstatus2 = access_check_samr_function(info->acc_granted,
-                                              SA_RIGHT_DOMAIN_OPEN_ACCOUNT,
+                                              SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
                                               "_samr_GetAliasMembership");
 
        if (!NT_STATUS_IS_OK(ntstatus1) || !NT_STATUS_IS_OK(ntstatus2)) {
@@ -4410,7 +4384,7 @@ NTSTATUS _samr_GetMembersInAlias(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_ALIAS_GET_MEMBERS,
+                                           SAMR_ALIAS_ACCESS_GET_MEMBERS,
                                            "_samr_GetMembersInAlias");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -4478,7 +4452,7 @@ NTSTATUS _samr_QueryGroupMember(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_GROUP_GET_MEMBERS,
+                                           SAMR_GROUP_ACCESS_GET_MEMBERS,
                                            "_samr_QueryGroupMember");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -4542,7 +4516,7 @@ NTSTATUS _samr_AddAliasMember(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_ALIAS_ADD_MEMBER,
+                                           SAMR_ALIAS_ACCESS_ADD_MEMBER,
                                            "_samr_AddAliasMember");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -4551,7 +4525,7 @@ NTSTATUS _samr_AddAliasMember(pipes_struct *p,
        DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid)));
 
        se_priv_copy( &se_rights, &se_add_users );
-       can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
+       can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
 
        /******** BEGIN SeAddUsers BLOCK *********/
 
@@ -4591,7 +4565,7 @@ NTSTATUS _samr_DeleteAliasMember(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_ALIAS_REMOVE_MEMBER,
+                                           SAMR_ALIAS_ACCESS_REMOVE_MEMBER,
                                            "_samr_DeleteAliasMember");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -4601,7 +4575,7 @@ NTSTATUS _samr_DeleteAliasMember(pipes_struct *p,
                   sid_string_dbg(&alias_sid)));
 
        se_priv_copy( &se_rights, &se_add_users );
-       can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
+       can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
 
        /******** BEGIN SeAddUsers BLOCK *********/
 
@@ -4642,7 +4616,7 @@ NTSTATUS _samr_AddGroupMember(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_GROUP_ADD_MEMBER,
+                                           SAMR_GROUP_ACCESS_ADD_MEMBER,
                                            "_samr_AddGroupMember");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -4656,7 +4630,7 @@ NTSTATUS _samr_AddGroupMember(pipes_struct *p,
        }
 
        se_priv_copy( &se_rights, &se_add_users );
-       can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
+       can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
 
        /******** BEGIN SeAddUsers BLOCK *********/
 
@@ -4702,7 +4676,7 @@ NTSTATUS _samr_DeleteGroupMember(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_GROUP_REMOVE_MEMBER,
+                                           SAMR_GROUP_ACCESS_REMOVE_MEMBER,
                                            "_samr_DeleteGroupMember");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -4714,7 +4688,7 @@ NTSTATUS _samr_DeleteGroupMember(pipes_struct *p,
        }
 
        se_priv_copy( &se_rights, &se_add_users );
-       can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
+       can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
 
        /******** BEGIN SeAddUsers BLOCK *********/
 
@@ -4785,9 +4759,9 @@ NTSTATUS _samr_DeleteUser(pipes_struct *p,
 
        /* For machine accounts it's the SeMachineAccountPrivilege that counts. */
        if ( acb_info & ACB_WSTRUST ) {
-               can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account );
+               can_add_accounts = user_has_privileges( p->server_info->ptok, &se_machine_account );
        } else {
-               can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
+               can_add_accounts = user_has_privileges( p->server_info->ptok, &se_add_users );
        }
 
        /******** BEGIN SeAddUsers BLOCK *********/
@@ -4859,7 +4833,7 @@ NTSTATUS _samr_DeleteDomainGroup(pipes_struct *p,
        }
 
        se_priv_copy( &se_rights, &se_add_users );
-       can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
+       can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
 
        /******** BEGIN SeAddUsers BLOCK *********/
 
@@ -4934,7 +4908,7 @@ NTSTATUS _samr_DeleteDomAlias(pipes_struct *p,
        DEBUG(10, ("lookup on Local SID\n"));
 
        se_priv_copy( &se_rights, &se_add_users );
-       can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
+       can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
 
        /******** BEGIN SeAddUsers BLOCK *********/
 
@@ -4983,7 +4957,7 @@ NTSTATUS _samr_CreateDomainGroup(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_DOMAIN_CREATE_GROUP,
+                                           SAMR_DOMAIN_ACCESS_CREATE_GROUP,
                                            "_samr_CreateDomainGroup");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -5003,7 +4977,7 @@ NTSTATUS _samr_CreateDomainGroup(pipes_struct *p,
        }
 
        se_priv_copy( &se_rights, &se_add_users );
-       can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
+       can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
 
        /******** BEGIN SeAddUsers BLOCK *********/
 
@@ -5026,7 +5000,7 @@ NTSTATUS _samr_CreateDomainGroup(pipes_struct *p,
 
        sid_compose(&info_sid, get_global_sam_sid(), *r->out.rid);
 
-       if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
+       if ((info = get_samr_info_by_sid(p->mem_ctx, &info_sid)) == NULL)
                return NT_STATUS_NO_MEMORY;
 
        /* they created it; let the user do what he wants with it */
@@ -5034,7 +5008,7 @@ NTSTATUS _samr_CreateDomainGroup(pipes_struct *p,
        info->acc_granted = GENERIC_RIGHTS_GROUP_ALL_ACCESS;
 
        /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, r->out.group_handle, free_samr_info, (void *)info))
+       if (!create_policy_hnd(p, r->out.group_handle, info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
        force_flush_samr_cache(disp_info);
@@ -5065,7 +5039,7 @@ NTSTATUS _samr_CreateDomAlias(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        result = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_DOMAIN_CREATE_ALIAS,
+                                           SAMR_DOMAIN_ACCESS_CREATE_ALIAS,
                                            "_samr_CreateDomAlias");
        if (!NT_STATUS_IS_OK(result)) {
                return result;
@@ -5077,7 +5051,7 @@ NTSTATUS _samr_CreateDomAlias(pipes_struct *p,
        name = r->in.alias_name->string;
 
        se_priv_copy( &se_rights, &se_add_users );
-       can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
+       can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
 
        result = can_create(p->mem_ctx, name);
        if (!NT_STATUS_IS_OK(result)) {
@@ -5118,7 +5092,7 @@ NTSTATUS _samr_CreateDomAlias(pipes_struct *p,
                return NT_STATUS_ACCESS_DENIED;
        }
 
-       if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
+       if ((info = get_samr_info_by_sid(p->mem_ctx, &info_sid)) == NULL)
                return NT_STATUS_NO_MEMORY;
 
        /* they created it; let the user do what he wants with it */
@@ -5126,7 +5100,7 @@ NTSTATUS _samr_CreateDomAlias(pipes_struct *p,
        info->acc_granted = GENERIC_RIGHTS_ALIAS_ALL_ACCESS;
 
        /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, r->out.alias_handle, free_samr_info, (void *)info))
+       if (!create_policy_hnd(p, r->out.alias_handle, info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
        force_flush_samr_cache(disp_info);
@@ -5157,7 +5131,7 @@ NTSTATUS _samr_QueryGroupInfo(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_GROUP_LOOKUP_INFO,
+                                           SAMR_GROUP_ACCESS_LOOKUP_INFO,
                                            "_samr_QueryGroupInfo");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -5192,24 +5166,20 @@ NTSTATUS _samr_QueryGroupInfo(pipes_struct *p,
                                return status;
                        }
 
-                       init_samr_group_info1(&info->all,
-                                             group_name,
-                                             attributes,
-                                             num_members,
-                                             group_description);
+                       info->all.name.string           = group_name;
+                       info->all.attributes            = attributes;
+                       info->all.num_members           = num_members;
+                       info->all.description.string    = group_description;
                        break;
                }
                case 2:
-                       init_samr_group_info2(&info->name,
-                                             group_name);
+                       info->name.string = group_name;
                        break;
                case 3:
-                       init_samr_group_info3(&info->attributes,
-                                             attributes);
+                       info->attributes.attributes = attributes;
                        break;
                case 4:
-                       init_samr_group_info4(&info->description,
-                                             group_description);
+                       info->description.string = group_description;
                        break;
                case 5: {
                        /*
@@ -5227,11 +5197,10 @@ NTSTATUS _samr_QueryGroupInfo(pipes_struct *p,
                                return status;
                        }
                        */
-                       init_samr_group_info5(&info->all2,
-                                             group_name,
-                                             attributes,
-                                             0, /* num_members - in w2k3 this is always 0 */
-                                             group_description);
+                       info->all2.name.string          = group_name;
+                       info->all2.attributes           = attributes;
+                       info->all2.num_members          = 0; /* num_members - in w2k3 this is always 0 */
+                       info->all2.description.string   = group_description;
 
                        break;
                }
@@ -5263,7 +5232,7 @@ NTSTATUS _samr_SetGroupInfo(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_GROUP_SET_INFO,
+                                           SAMR_GROUP_ACCESS_SET_INFO,
                                            "_samr_SetGroupInfo");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -5289,7 +5258,7 @@ NTSTATUS _samr_SetGroupInfo(pipes_struct *p,
                        return NT_STATUS_INVALID_INFO_CLASS;
        }
 
-       can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
+       can_mod_accounts = user_has_privileges( p->server_info->ptok, &se_add_users );
 
        /******** BEGIN SeAddUsers BLOCK *********/
 
@@ -5328,7 +5297,7 @@ NTSTATUS _samr_SetAliasInfo(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_ALIAS_SET_INFO,
+                                           SAMR_ALIAS_ACCESS_SET_INFO,
                                            "_samr_SetAliasInfo");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -5391,7 +5360,7 @@ NTSTATUS _samr_SetAliasInfo(pipes_struct *p,
                        return NT_STATUS_INVALID_INFO_CLASS;
        }
 
-        can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
+        can_mod_accounts = user_has_privileges( p->server_info->ptok, &se_add_users );
 
         /******** BEGIN SeAddUsers BLOCK *********/
 
@@ -5472,21 +5441,21 @@ NTSTATUS _samr_OpenGroup(pipes_struct *p,
                return NT_STATUS_INVALID_HANDLE;
 
        status = access_check_samr_function(acc_granted,
-                                           SA_RIGHT_DOMAIN_OPEN_ACCOUNT,
+                                           SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
                                            "_samr_OpenGroup");
 
        if ( !NT_STATUS_IS_OK(status) )
                return status;
 
        /*check if access can be granted as requested by client. */
-       map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
+       map_max_allowed_access(p->server_info->ptok, &des_access);
 
        make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
        se_map_generic(&des_access,&grp_generic_mapping);
 
        se_priv_copy( &se_rights, &se_add_users );
 
-       status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
+       status = access_check_samr_object(psd, p->server_info->ptok,
                &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access,
                &acc_granted, "_samr_OpenGroup");
 
@@ -5502,7 +5471,7 @@ NTSTATUS _samr_OpenGroup(pipes_struct *p,
        sid_append_rid(&info_sid, r->in.rid);
        sid_to_fstring(sid_string, &info_sid);
 
-       if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
+       if ((info = get_samr_info_by_sid(p->mem_ctx, &info_sid)) == NULL)
                return NT_STATUS_NO_MEMORY;
 
        info->acc_granted = acc_granted;
@@ -5517,7 +5486,7 @@ NTSTATUS _samr_OpenGroup(pipes_struct *p,
                return NT_STATUS_NO_SUCH_GROUP;
 
        /* get a (unique) handle.  open a policy on it. */
-       if (!create_policy_hnd(p, r->out.group_handle, free_samr_info, (void *)info))
+       if (!create_policy_hnd(p, r->out.group_handle, info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
        return NT_STATUS_OK;
@@ -5626,11 +5595,11 @@ NTSTATUS _samr_SetDomainInfo(pipes_struct *p,
         * levels here, but we're really just looking for
         * GENERIC_RIGHTS_DOMAIN_WRITE access. Unfortunately
         * this maps to different specific bits. So
-        * assume if we have SA_RIGHT_DOMAIN_SET_INFO_1
+        * assume if we have SAMR_DOMAIN_ACCESS_SET_INFO_1
         * set we are ok. */
 
        result = access_check_samr_function(info->acc_granted,
-                                           SA_RIGHT_DOMAIN_SET_INFO_1,
+                                           SAMR_DOMAIN_ACCESS_SET_INFO_1,
                                            "_samr_SetDomainInfo");
 
        if (!NT_STATUS_IS_OK(result))
@@ -5703,7 +5672,7 @@ NTSTATUS _samr_GetDisplayEnumerationIndex(pipes_struct *p,
        }
 
        status = access_check_samr_function(info->acc_granted,
-                                           SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
+                                           SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
                                            "_samr_GetDisplayEnumerationIndex");
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -5723,7 +5692,8 @@ NTSTATUS _samr_GetDisplayEnumerationIndex(pipes_struct *p,
        switch (r->in.level) {
        case 1:
                if (info->disp_info->users == NULL) {
-                       info->disp_info->users = pdb_search_users(ACB_NORMAL);
+                       info->disp_info->users = pdb_search_users(
+                               info->disp_info, ACB_NORMAL);
                        if (info->disp_info->users == NULL) {
                                unbecome_root();
                                return NT_STATUS_ACCESS_DENIED;
@@ -5742,8 +5712,8 @@ NTSTATUS _samr_GetDisplayEnumerationIndex(pipes_struct *p,
                break;
        case 2:
                if (info->disp_info->machines == NULL) {
-                       info->disp_info->machines =
-                               pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
+                       info->disp_info->machines = pdb_search_users(
+                               info->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
                        if (info->disp_info->machines == NULL) {
                                unbecome_root();
                                return NT_STATUS_ACCESS_DENIED;
@@ -5762,7 +5732,8 @@ NTSTATUS _samr_GetDisplayEnumerationIndex(pipes_struct *p,
                break;
        case 3:
                if (info->disp_info->groups == NULL) {
-                       info->disp_info->groups = pdb_search_groups();
+                       info->disp_info->groups = pdb_search_groups(
+                               info->disp_info);
                        if (info->disp_info->groups == NULL) {
                                unbecome_root();
                                return NT_STATUS_ACCESS_DENIED;
@@ -5840,16 +5811,6 @@ NTSTATUS _samr_Shutdown(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _samr_CreateUser(pipes_struct *p,
-                         struct samr_CreateUser *r)
-{
-       p->rng_fault_state = true;
-       return NT_STATUS_NOT_IMPLEMENTED;
-}
-
-/****************************************************************
-****************************************************************/
-
 NTSTATUS _samr_SetMemberAttributesOfGroup(pipes_struct *p,
                                          struct samr_SetMemberAttributesOfGroup *r)
 {
@@ -5890,16 +5851,6 @@ NTSTATUS _samr_TestPrivateFunctionsUser(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _samr_QueryUserInfo2(pipes_struct *p,
-                             struct samr_QueryUserInfo2 *r)
-{
-       p->rng_fault_state = true;
-       return NT_STATUS_NOT_IMPLEMENTED;
-}
-
-/****************************************************************
-****************************************************************/
-
 NTSTATUS _samr_AddMultipleMembersToAlias(pipes_struct *p,
                                         struct samr_AddMultipleMembersToAlias *r)
 {