[NETAPI32]
[reactos.git] / reactos / dll / win32 / netapi32 / user.c
index dcd313d..6057772 100644 (file)
@@ -20,7 +20,7 @@
 
 /*
  *  TODO:
- *    Implement NetUserGetGroups
+ *    Implement NetUserGetGroups (WIP)
  *    Implement NetUserSetGroups
  *    NetUserGetLocalGroups does not support LG_INCLUDE_INDIRECT yet.
  *    Add missing information levels.
 
 #include "netapi32.h"
 
-WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
+#include <ndk/kefuncs.h>
+#include <ndk/obfuncs.h>
 
+WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
 
 typedef struct _ENUM_CONTEXT
 {
@@ -65,11 +67,77 @@ DeltaTimeToSeconds(LARGE_INTEGER DeltaTime)
 }
 
 
+static
+NTSTATUS
+GetAllowedWorldAce(IN PACL Acl,
+                   OUT PACCESS_ALLOWED_ACE *Ace)
+{
+    SID_IDENTIFIER_AUTHORITY WorldAuthority = {SECURITY_WORLD_SID_AUTHORITY};
+    ULONG WorldSid[sizeof(SID) / sizeof(ULONG) + SID_MAX_SUB_AUTHORITIES];
+    ACL_SIZE_INFORMATION AclSize;
+    PVOID LocalAce = NULL;
+    ULONG i;
+    NTSTATUS Status;
+
+    *Ace = NULL;
+
+    RtlInitializeSid((PSID)WorldSid,
+                     &WorldAuthority,
+                     1);
+    *(RtlSubAuthoritySid((PSID)WorldSid, 0)) = SECURITY_WORLD_RID;
+
+    Status = RtlQueryInformationAcl(Acl,
+                                    &AclSize,
+                                    sizeof(AclSize),
+                                    AclSizeInformation);
+    if (!NT_SUCCESS(Status))
+        return Status;
+
+    for (i = 0; i < AclSize.AceCount; i++)
+    {
+        Status = RtlGetAce(Acl, i, &LocalAce);
+        if (!NT_SUCCESS(Status))
+            return Status;
+
+        if (((PACE_HEADER)LocalAce)->AceType != ACCESS_ALLOWED_ACE_TYPE)
+            continue;
+
+        if (RtlEqualSid((PSID)WorldSid,
+                        (PSID)&((PACCESS_ALLOWED_ACE)LocalAce)->SidStart))
+        {
+            *Ace = (PACCESS_ALLOWED_ACE)LocalAce;
+            return STATUS_SUCCESS;
+        }
+    }
+
+    return STATUS_SUCCESS;
+}
+
+
 static
 ULONG
-GetAccountFlags(ULONG AccountControl)
+GetAccountFlags(ULONG AccountControl,
+                PACL Dacl)
 {
+    PACCESS_ALLOWED_ACE Ace = NULL;
     ULONG Flags = UF_SCRIPT;
+    NTSTATUS Status;
+
+    if (Dacl != NULL)
+    {
+        Status = GetAllowedWorldAce(Dacl, &Ace);
+        if (NT_SUCCESS(Status))
+        {
+            if (Ace == NULL)
+            {
+                Flags |= UF_PASSWD_CANT_CHANGE;
+            }
+            else if ((Ace->Mask & USER_CHANGE_PASSWORD) == 0)
+            {
+                Flags |= UF_PASSWD_CANT_CHANGE;
+            }
+        }
+    }
 
     if (AccountControl & USER_ACCOUNT_DISABLED)
         Flags |= UF_ACCOUNTDISABLE;
@@ -80,8 +148,6 @@ GetAccountFlags(ULONG AccountControl)
     if (AccountControl & USER_PASSWORD_NOT_REQUIRED)
         Flags |= UF_PASSWD_NOTREQD;
 
-//    UF_PASSWD_CANT_CHANGE
-
     if (AccountControl & USER_ACCOUNT_AUTO_LOCKED)
         Flags |= UF_LOCKOUT;
 
@@ -184,15 +250,164 @@ GetPasswordAge(IN PLARGE_INTEGER PasswordLastSet)
 }
 
 
+static
+VOID
+ChangeUserDacl(IN PACL Dacl,
+               IN ULONG Flags)
+{
+    PACCESS_ALLOWED_ACE Ace = NULL;
+    NTSTATUS Status;
+
+    if (Dacl == NULL)
+        return;
+
+    Status = GetAllowedWorldAce(Dacl, &Ace);
+    if (!NT_SUCCESS(Status))
+        return;
+
+    if (Flags & UF_PASSWD_CANT_CHANGE)
+        Ace->Mask &= ~USER_CHANGE_PASSWORD;
+    else
+        Ace->Mask |= USER_CHANGE_PASSWORD;
+}
+
+
+static
+NET_API_STATUS
+GetUserDacl(IN SAM_HANDLE UserHandle,
+            OUT PACL *Dacl)
+{
+    PSECURITY_DESCRIPTOR SecurityDescriptor = NULL;
+    PACL SamDacl;
+    PACL LocalDacl;
+    BOOLEAN Defaulted;
+    BOOLEAN Present;
+    ACL_SIZE_INFORMATION AclSize;
+    NET_API_STATUS ApiStatus;
+    NTSTATUS Status;
+
+    TRACE("(%p %p)\n", UserHandle, Dacl);
+
+    *Dacl = NULL;
+
+    Status = SamQuerySecurityObject(UserHandle,
+                                    DACL_SECURITY_INFORMATION,
+                                    &SecurityDescriptor);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("SamQuerySecurityObject() failed (Status 0x%08lx)\n", Status);
+        ApiStatus = NetpNtStatusToApiStatus(Status);
+        goto done;
+    }
+
+    Status = RtlGetDaclSecurityDescriptor(SecurityDescriptor,
+                                          &Present,
+                                          &SamDacl,
+                                          &Defaulted);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("RtlGetDaclSecurityDescriptor() failed (Status 0x%08lx)\n", Status);
+        ApiStatus = NERR_InternalError;
+        goto done;
+    }
+
+    if (Present == FALSE)
+    {
+        TRACE("No DACL present\n");
+        ApiStatus = NERR_Success;
+        goto done;
+    }
+
+    Status = RtlQueryInformationAcl(SamDacl,
+                                    &AclSize,
+                                    sizeof(AclSize),
+                                    AclSizeInformation);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("RtlQueryInformationAcl() failed (Status 0x%08lx)\n", Status);
+        ApiStatus = NERR_InternalError;
+        goto done;
+    }
+
+    LocalDacl = HeapAlloc(GetProcessHeap(), 0, AclSize.AclBytesInUse);
+    if (LocalDacl == NULL)
+    {
+        TRACE("Memory allocation failed\n");
+        ApiStatus = ERROR_NOT_ENOUGH_MEMORY;
+        goto done;
+    }
+
+    RtlCopyMemory(LocalDacl, SamDacl, AclSize.AclBytesInUse);
+
+    *Dacl = LocalDacl;
+
+    ApiStatus = NERR_Success;
+
+done:
+    if (SecurityDescriptor != NULL)
+        SamFreeMemory(SecurityDescriptor);
+
+    TRACE("done (ApiStatus: 0x%08lx)\n", ApiStatus);
+
+    return ApiStatus;
+}
+
+
+static
+VOID
+FreeUserInfo(PUSER_ALL_INFORMATION UserInfo)
+{
+    if (UserInfo->UserName.Buffer != NULL)
+        SamFreeMemory(UserInfo->UserName.Buffer);
+
+    if (UserInfo->FullName.Buffer != NULL)
+        SamFreeMemory(UserInfo->FullName.Buffer);
+
+    if (UserInfo->HomeDirectory.Buffer != NULL)
+        SamFreeMemory(UserInfo->HomeDirectory.Buffer);
+
+    if (UserInfo->HomeDirectoryDrive.Buffer != NULL)
+        SamFreeMemory(UserInfo->HomeDirectoryDrive.Buffer);
+
+    if (UserInfo->ScriptPath.Buffer != NULL)
+        SamFreeMemory(UserInfo->ScriptPath.Buffer);
+
+    if (UserInfo->ProfilePath.Buffer != NULL)
+        SamFreeMemory(UserInfo->ProfilePath.Buffer);
+
+    if (UserInfo->AdminComment.Buffer != NULL)
+        SamFreeMemory(UserInfo->AdminComment.Buffer);
+
+    if (UserInfo->WorkStations.Buffer != NULL)
+        SamFreeMemory(UserInfo->WorkStations.Buffer);
+
+    if (UserInfo->UserComment.Buffer != NULL)
+        SamFreeMemory(UserInfo->UserComment.Buffer);
+
+    if (UserInfo->Parameters.Buffer != NULL)
+        SamFreeMemory(UserInfo->Parameters.Buffer);
+
+    if (UserInfo->PrivateData.Buffer != NULL)
+        SamFreeMemory(UserInfo->PrivateData.Buffer);
+
+    if (UserInfo->LogonHours.LogonHours != NULL)
+        SamFreeMemory(UserInfo->LogonHours.LogonHours);
+
+    SamFreeMemory(UserInfo);
+}
+
+
 static
 NET_API_STATUS
-BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
+BuildUserInfoBuffer(SAM_HANDLE UserHandle,
                     DWORD level,
                     ULONG RelativeId,
                     LPVOID *Buffer)
 {
     UNICODE_STRING LogonServer = RTL_CONSTANT_STRING(L"\\\\*");
+    PUSER_ALL_INFORMATION UserInfo = NULL;
     LPVOID LocalBuffer = NULL;
+    PACL Dacl = NULL;
     PUSER_INFO_0 UserInfo0;
     PUSER_INFO_1 UserInfo1;
     PUSER_INFO_2 UserInfo2;
@@ -204,10 +419,29 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
     PUSER_INFO_23 UserInfo23;
     LPWSTR Ptr;
     ULONG Size = 0;
+    NTSTATUS Status;
     NET_API_STATUS ApiStatus = NERR_Success;
 
     *Buffer = NULL;
 
+    Status = SamQueryInformationUser(UserHandle,
+                                     UserAllInformation,
+                                     (PVOID *)&UserInfo);
+    if (!NT_SUCCESS(Status))
+    {
+        ERR("SamQueryInformationUser failed (Status %08lx)\n", Status);
+        ApiStatus = NetpNtStatusToApiStatus(Status);
+        goto done;
+    }
+
+    if ((level == 1) || (level == 2) || (level == 3) ||
+        (level == 4) || (level == 20) || (level == 23))
+    {
+        ApiStatus = GetUserDacl(UserHandle, &Dacl);
+        if (ApiStatus != NERR_Success)
+            goto done;
+    }
+
     switch (level)
     {
         case 0:
@@ -217,166 +451,84 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
 
         case 1:
             Size = sizeof(USER_INFO_1) +
-                   UserInfo->UserName.Length + sizeof(WCHAR);
-
-            if (UserInfo->HomeDirectory.Length > 0)
-                Size += UserInfo->HomeDirectory.Length + sizeof(WCHAR);
-
-            if (UserInfo->AdminComment.Length > 0)
-                Size += UserInfo->AdminComment.Length + sizeof(WCHAR);
-
-            if (UserInfo->ScriptPath.Length > 0)
-                Size += UserInfo->ScriptPath.Length + sizeof(WCHAR);
+                   UserInfo->UserName.Length + sizeof(WCHAR) +
+                   UserInfo->HomeDirectory.Length + sizeof(WCHAR) +
+                   UserInfo->AdminComment.Length + sizeof(WCHAR) +
+                   UserInfo->ScriptPath.Length + sizeof(WCHAR);
             break;
 
         case 2:
             Size = sizeof(USER_INFO_2) +
-                   UserInfo->UserName.Length + sizeof(WCHAR);
-
-            if (UserInfo->HomeDirectory.Length > 0)
-                Size += UserInfo->HomeDirectory.Length + sizeof(WCHAR);
-
-            if (UserInfo->AdminComment.Length > 0)
-                Size += UserInfo->AdminComment.Length + sizeof(WCHAR);
-
-            if (UserInfo->ScriptPath.Length > 0)
-                Size += UserInfo->ScriptPath.Length + sizeof(WCHAR);
-
-            if (UserInfo->FullName.Length > 0)
-                Size += UserInfo->FullName.Length + sizeof(WCHAR);
-
-            if (UserInfo->UserComment.Length > 0)
-                Size += UserInfo->UserComment.Length + sizeof(WCHAR);
-
-            if (UserInfo->Parameters.Length > 0)
-                Size += UserInfo->Parameters.Length + sizeof(WCHAR);
-
-            if (UserInfo->WorkStations.Length > 0)
-                Size += UserInfo->WorkStations.Length + sizeof(WCHAR);
+                   UserInfo->UserName.Length + sizeof(WCHAR) +
+                   UserInfo->HomeDirectory.Length + sizeof(WCHAR) +
+                   UserInfo->AdminComment.Length + sizeof(WCHAR) +
+                   UserInfo->ScriptPath.Length + sizeof(WCHAR) +
+                   UserInfo->FullName.Length + sizeof(WCHAR) +
+                   UserInfo->UserComment.Length + sizeof(WCHAR) +
+                   UserInfo->Parameters.Length + sizeof(WCHAR) +
+                   UserInfo->WorkStations.Length + sizeof(WCHAR) +
+                   LogonServer.Length + sizeof(WCHAR);
 
             if (UserInfo->LogonHours.UnitsPerWeek > 0)
                 Size += (((ULONG)UserInfo->LogonHours.UnitsPerWeek) + 7) / 8;
-
-            if (LogonServer.Length > 0)
-                Size += LogonServer.Length + sizeof(WCHAR);
             break;
 
         case 3:
             Size = sizeof(USER_INFO_3) +
-                   UserInfo->UserName.Length + sizeof(WCHAR);
-
-            if (UserInfo->HomeDirectory.Length > 0)
-                Size += UserInfo->HomeDirectory.Length + sizeof(WCHAR);
-
-            if (UserInfo->AdminComment.Length > 0)
-                Size += UserInfo->AdminComment.Length + sizeof(WCHAR);
-
-            if (UserInfo->ScriptPath.Length > 0)
-                Size += UserInfo->ScriptPath.Length + sizeof(WCHAR);
-
-            if (UserInfo->FullName.Length > 0)
-                Size += UserInfo->FullName.Length + sizeof(WCHAR);
-
-            if (UserInfo->UserComment.Length > 0)
-                Size += UserInfo->UserComment.Length + sizeof(WCHAR);
-
-            if (UserInfo->Parameters.Length > 0)
-                Size += UserInfo->Parameters.Length + sizeof(WCHAR);
-
-            if (UserInfo->WorkStations.Length > 0)
-                Size += UserInfo->WorkStations.Length + sizeof(WCHAR);
+                   UserInfo->UserName.Length + sizeof(WCHAR) +
+                   UserInfo->HomeDirectory.Length + sizeof(WCHAR) +
+                   UserInfo->AdminComment.Length + sizeof(WCHAR) +
+                   UserInfo->ScriptPath.Length + sizeof(WCHAR) +
+                   UserInfo->FullName.Length + sizeof(WCHAR) +
+                   UserInfo->UserComment.Length + sizeof(WCHAR) +
+                   UserInfo->Parameters.Length + sizeof(WCHAR) +
+                   UserInfo->WorkStations.Length + sizeof(WCHAR) +
+                   LogonServer.Length + sizeof(WCHAR) +
+                   UserInfo->ProfilePath.Length + sizeof(WCHAR) +
+                   UserInfo->HomeDirectoryDrive.Length + sizeof(WCHAR);
 
             if (UserInfo->LogonHours.UnitsPerWeek > 0)
                 Size += (((ULONG)UserInfo->LogonHours.UnitsPerWeek) + 7) / 8;
-
-            if (LogonServer.Length > 0)
-                Size += LogonServer.Length + sizeof(WCHAR);
-
-            if (UserInfo->ProfilePath.Length > 0)
-                Size += UserInfo->ProfilePath.Length + sizeof(WCHAR);
-
-            if (UserInfo->HomeDirectoryDrive.Length > 0)
-                Size += UserInfo->HomeDirectoryDrive.Length + sizeof(WCHAR);
             break;
 
         case 4:
             Size = sizeof(USER_INFO_4) +
-                   UserInfo->UserName.Length + sizeof(WCHAR);
-
-            if (UserInfo->HomeDirectory.Length > 0)
-                Size += UserInfo->HomeDirectory.Length + sizeof(WCHAR);
-
-            if (UserInfo->AdminComment.Length > 0)
-                Size += UserInfo->AdminComment.Length + sizeof(WCHAR);
-
-            if (UserInfo->ScriptPath.Length > 0)
-                Size += UserInfo->ScriptPath.Length + sizeof(WCHAR);
-
-            if (UserInfo->FullName.Length > 0)
-                Size += UserInfo->FullName.Length + sizeof(WCHAR);
-
-            if (UserInfo->UserComment.Length > 0)
-                Size += UserInfo->UserComment.Length + sizeof(WCHAR);
-
-            if (UserInfo->Parameters.Length > 0)
-                Size += UserInfo->Parameters.Length + sizeof(WCHAR);
-
-            if (UserInfo->WorkStations.Length > 0)
-                Size += UserInfo->WorkStations.Length + sizeof(WCHAR);
+                   UserInfo->UserName.Length + sizeof(WCHAR) +
+                   UserInfo->HomeDirectory.Length + sizeof(WCHAR) +
+                   UserInfo->AdminComment.Length + sizeof(WCHAR) +
+                   UserInfo->ScriptPath.Length + sizeof(WCHAR) +
+                   UserInfo->FullName.Length + sizeof(WCHAR) +
+                   UserInfo->UserComment.Length + sizeof(WCHAR) +
+                   UserInfo->Parameters.Length + sizeof(WCHAR) +
+                   UserInfo->WorkStations.Length + sizeof(WCHAR) +
+                   LogonServer.Length + sizeof(WCHAR) +
+                   UserInfo->ProfilePath.Length + sizeof(WCHAR) +
+                   UserInfo->HomeDirectoryDrive.Length + sizeof(WCHAR);
 
             if (UserInfo->LogonHours.UnitsPerWeek > 0)
                 Size += (((ULONG)UserInfo->LogonHours.UnitsPerWeek) + 7) / 8;
 
-            if (LogonServer.Length > 0)
-                Size += LogonServer.Length + sizeof(WCHAR);
-
             /* FIXME: usri4_user_sid */
-
-            if (UserInfo->ProfilePath.Length > 0)
-                Size += UserInfo->ProfilePath.Length + sizeof(WCHAR);
-
-            if (UserInfo->HomeDirectoryDrive.Length > 0)
-                Size += UserInfo->HomeDirectoryDrive.Length + sizeof(WCHAR);
             break;
 
         case 10:
             Size = sizeof(USER_INFO_10) +
-                   UserInfo->UserName.Length + sizeof(WCHAR);
-
-            if (UserInfo->AdminComment.Length > 0)
-                Size += UserInfo->AdminComment.Length + sizeof(WCHAR);
-
-            if (UserInfo->UserComment.Length > 0)
-                Size += UserInfo->UserComment.Length + sizeof(WCHAR);
-
-            if (UserInfo->FullName.Length > 0)
-                Size += UserInfo->FullName.Length + sizeof(WCHAR);
+                   UserInfo->UserName.Length + sizeof(WCHAR) +
+                   UserInfo->AdminComment.Length + sizeof(WCHAR) +
+                   UserInfo->UserComment.Length + sizeof(WCHAR) +
+                   UserInfo->FullName.Length + sizeof(WCHAR);
             break;
 
         case 11:
             Size = sizeof(USER_INFO_11) +
-                   UserInfo->UserName.Length + sizeof(WCHAR);
-
-            if (UserInfo->AdminComment.Length > 0)
-                Size += UserInfo->AdminComment.Length + sizeof(WCHAR);
-
-            if (UserInfo->UserComment.Length > 0)
-                Size += UserInfo->UserComment.Length + sizeof(WCHAR);
-
-            if (UserInfo->FullName.Length > 0)
-                Size += UserInfo->FullName.Length + sizeof(WCHAR);
-
-            if (UserInfo->HomeDirectory.Length > 0)
-                Size += UserInfo->HomeDirectory.Length + sizeof(WCHAR);
-
-            if (UserInfo->Parameters.Length > 0)
-                Size += UserInfo->Parameters.Length + sizeof(WCHAR);
-
-            if (LogonServer.Length > 0)
-                Size += LogonServer.Length + sizeof(WCHAR);
-
-            if (UserInfo->WorkStations.Length > 0)
-                Size += UserInfo->WorkStations.Length + sizeof(WCHAR);
+                   UserInfo->UserName.Length + sizeof(WCHAR) +
+                   UserInfo->AdminComment.Length + sizeof(WCHAR) +
+                   UserInfo->UserComment.Length + sizeof(WCHAR) +
+                   UserInfo->FullName.Length + sizeof(WCHAR) +
+                   UserInfo->HomeDirectory.Length + sizeof(WCHAR) +
+                   UserInfo->Parameters.Length + sizeof(WCHAR) +
+                   LogonServer.Length + sizeof(WCHAR) +
+                   UserInfo->WorkStations.Length + sizeof(WCHAR);
 
             if (UserInfo->LogonHours.UnitsPerWeek > 0)
                 Size += (((ULONG)UserInfo->LogonHours.UnitsPerWeek) + 7) / 8;
@@ -384,24 +536,16 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
 
         case 20:
             Size = sizeof(USER_INFO_20) +
-                   UserInfo->UserName.Length + sizeof(WCHAR);
-
-            if (UserInfo->FullName.Length > 0)
-                Size += UserInfo->FullName.Length + sizeof(WCHAR);
-
-            if (UserInfo->AdminComment.Length > 0)
-                Size += UserInfo->AdminComment.Length + sizeof(WCHAR);
+                   UserInfo->UserName.Length + sizeof(WCHAR) +
+                   UserInfo->FullName.Length + sizeof(WCHAR) +
+                   UserInfo->AdminComment.Length + sizeof(WCHAR);
             break;
 
         case 23:
             Size = sizeof(USER_INFO_23) +
-                   UserInfo->UserName.Length + sizeof(WCHAR);
-
-            if (UserInfo->FullName.Length > 0)
-                Size += UserInfo->FullName.Length + sizeof(WCHAR);
-
-            if (UserInfo->AdminComment.Length > 0)
-                Size += UserInfo->AdminComment.Length + sizeof(WCHAR);
+                   UserInfo->UserName.Length + sizeof(WCHAR) +
+                   UserInfo->FullName.Length + sizeof(WCHAR) +
+                   UserInfo->AdminComment.Length + sizeof(WCHAR);
 
             /* FIXME: usri23_user_sid */
             break;
@@ -423,6 +567,7 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
             UserInfo0 = (PUSER_INFO_0)LocalBuffer;
 
             Ptr = (LPWSTR)((ULONG_PTR)UserInfo0 + sizeof(USER_INFO_0));
+
             UserInfo0->usri0_name = Ptr;
 
             memcpy(UserInfo0->usri0_name,
@@ -446,46 +591,32 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
             Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserName.Length + sizeof(WCHAR));
 
             UserInfo1->usri1_password = NULL;
-
             UserInfo1->usri1_password_age = GetPasswordAge(&UserInfo->PasswordLastSet);
 
-            /* FIXME: UserInfo1->usri1_priv */
-
-            if (UserInfo->HomeDirectory.Length > 0)
-            {
-                UserInfo1->usri1_home_dir = Ptr;
-
-                memcpy(UserInfo1->usri1_home_dir,
-                       UserInfo->HomeDirectory.Buffer,
-                       UserInfo->HomeDirectory.Length);
-                UserInfo1->usri1_home_dir[UserInfo->HomeDirectory.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectory.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->AdminComment.Length > 0)
-            {
-                UserInfo1->usri1_comment = Ptr;
-
-                memcpy(UserInfo1->usri1_comment,
-                       UserInfo->AdminComment.Buffer,
-                       UserInfo->AdminComment.Length);
-                UserInfo1->usri1_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
-            }
-
-            UserInfo1->usri1_flags = GetAccountFlags(UserInfo->UserAccountControl);
-
-            if (UserInfo->ScriptPath.Length > 0)
-            {
-                UserInfo1->usri1_script_path = Ptr;
-
-                memcpy(UserInfo1->usri1_script_path,
-                       UserInfo->ScriptPath.Buffer,
-                       UserInfo->ScriptPath.Length);
-                UserInfo1->usri1_script_path[UserInfo->ScriptPath.Length / sizeof(WCHAR)] = UNICODE_NULL;
-            }
+            /* FIXME: usri1_priv */
+
+            UserInfo1->usri1_home_dir = Ptr;
+            memcpy(UserInfo1->usri1_home_dir,
+                   UserInfo->HomeDirectory.Buffer,
+                   UserInfo->HomeDirectory.Length);
+            UserInfo1->usri1_home_dir[UserInfo->HomeDirectory.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectory.Length + sizeof(WCHAR));
+
+            UserInfo1->usri1_comment = Ptr;
+            memcpy(UserInfo1->usri1_comment,
+                   UserInfo->AdminComment.Buffer,
+                   UserInfo->AdminComment.Length);
+            UserInfo1->usri1_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
+
+            UserInfo1->usri1_flags = GetAccountFlags(UserInfo->UserAccountControl,
+                                                     Dacl);
+
+            UserInfo1->usri1_script_path = Ptr;
+            memcpy(UserInfo1->usri1_script_path,
+                   UserInfo->ScriptPath.Buffer,
+                   UserInfo->ScriptPath.Length);
+            UserInfo1->usri1_script_path[UserInfo->ScriptPath.Length / sizeof(WCHAR)] = UNICODE_NULL;
             break;
 
         case 2:
@@ -502,97 +633,64 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
 
             Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserName.Length + sizeof(WCHAR));
 
+            UserInfo2->usri2_password = NULL;
             UserInfo2->usri2_password_age = GetPasswordAge(&UserInfo->PasswordLastSet);
 
             /* FIXME: usri2_priv */
 
-            if (UserInfo->HomeDirectory.Length > 0)
-            {
-                UserInfo2->usri2_home_dir = Ptr;
-
-                memcpy(UserInfo2->usri2_home_dir,
-                       UserInfo->HomeDirectory.Buffer,
-                       UserInfo->HomeDirectory.Length);
-                UserInfo2->usri2_home_dir[UserInfo->HomeDirectory.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectory.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->AdminComment.Length > 0)
-            {
-                UserInfo2->usri2_comment = Ptr;
-
-                memcpy(UserInfo2->usri2_comment,
-                       UserInfo->AdminComment.Buffer,
-                       UserInfo->AdminComment.Length);
-                UserInfo2->usri2_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
-            }
-
-            UserInfo2->usri2_flags = GetAccountFlags(UserInfo->UserAccountControl);
-
-            if (UserInfo->ScriptPath.Length > 0)
-            {
-                UserInfo2->usri2_script_path = Ptr;
-
-                memcpy(UserInfo2->usri2_script_path,
-                       UserInfo->ScriptPath.Buffer,
-                       UserInfo->ScriptPath.Length);
-                UserInfo2->usri2_script_path[UserInfo->ScriptPath.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->ScriptPath.Length + sizeof(WCHAR));
-            }
+            UserInfo2->usri2_home_dir = Ptr;
+            memcpy(UserInfo2->usri2_home_dir,
+                   UserInfo->HomeDirectory.Buffer,
+                   UserInfo->HomeDirectory.Length);
+            UserInfo2->usri2_home_dir[UserInfo->HomeDirectory.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectory.Length + sizeof(WCHAR));
+
+            UserInfo2->usri2_comment = Ptr;
+            memcpy(UserInfo2->usri2_comment,
+                   UserInfo->AdminComment.Buffer,
+                   UserInfo->AdminComment.Length);
+            UserInfo2->usri2_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
+
+            UserInfo2->usri2_flags = GetAccountFlags(UserInfo->UserAccountControl,
+                                                     Dacl);
+
+            UserInfo2->usri2_script_path = Ptr;
+            memcpy(UserInfo2->usri2_script_path,
+                   UserInfo->ScriptPath.Buffer,
+                   UserInfo->ScriptPath.Length);
+            UserInfo2->usri2_script_path[UserInfo->ScriptPath.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->ScriptPath.Length + sizeof(WCHAR));
 
             /* FIXME: usri2_auth_flags */
 
-            if (UserInfo->FullName.Length > 0)
-            {
-                UserInfo2->usri2_full_name = Ptr;
-
-                memcpy(UserInfo2->usri2_full_name,
-                       UserInfo->FullName.Buffer,
-                       UserInfo->FullName.Length);
-                UserInfo2->usri2_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->UserComment.Length > 0)
-            {
-                UserInfo2->usri2_usr_comment = Ptr;
-
-                memcpy(UserInfo2->usri2_usr_comment,
-                       UserInfo->UserComment.Buffer,
-                       UserInfo->UserComment.Length);
-                UserInfo2->usri2_usr_comment[UserInfo->UserComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserComment.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->Parameters.Length > 0)
-            {
-                UserInfo2->usri2_parms = Ptr;
-
-                memcpy(UserInfo2->usri2_parms,
-                       UserInfo->Parameters.Buffer,
-                       UserInfo->Parameters.Length);
-                UserInfo2->usri2_parms[UserInfo->Parameters.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->Parameters.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->WorkStations.Length > 0)
-            {
-                UserInfo2->usri2_workstations = Ptr;
-
-                memcpy(UserInfo2->usri2_workstations,
-                       UserInfo->WorkStations.Buffer,
-                       UserInfo->WorkStations.Length);
-                UserInfo2->usri2_workstations[UserInfo->WorkStations.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->WorkStations.Length + sizeof(WCHAR));
-            }
+            UserInfo2->usri2_full_name = Ptr;
+            memcpy(UserInfo2->usri2_full_name,
+                   UserInfo->FullName.Buffer,
+                   UserInfo->FullName.Length);
+            UserInfo2->usri2_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
+
+            UserInfo2->usri2_usr_comment = Ptr;
+            memcpy(UserInfo2->usri2_usr_comment,
+                   UserInfo->UserComment.Buffer,
+                   UserInfo->UserComment.Length);
+            UserInfo2->usri2_usr_comment[UserInfo->UserComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserComment.Length + sizeof(WCHAR));
+
+            UserInfo2->usri2_parms = Ptr;
+            memcpy(UserInfo2->usri2_parms,
+                   UserInfo->Parameters.Buffer,
+                   UserInfo->Parameters.Length);
+            UserInfo2->usri2_parms[UserInfo->Parameters.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->Parameters.Length + sizeof(WCHAR));
+
+            UserInfo2->usri2_workstations = Ptr;
+            memcpy(UserInfo2->usri2_workstations,
+                   UserInfo->WorkStations.Buffer,
+                   UserInfo->WorkStations.Length);
+            UserInfo2->usri2_workstations[UserInfo->WorkStations.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->WorkStations.Length + sizeof(WCHAR));
 
             RtlTimeToSecondsSince1970(&UserInfo->LastLogon,
                                       &UserInfo2->usri2_last_logon);
@@ -620,17 +718,12 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
             UserInfo2->usri2_bad_pw_count = UserInfo->BadPasswordCount;
             UserInfo2->usri2_num_logons = UserInfo->LogonCount;
 
-            if (LogonServer.Length > 0)
-            {
-                UserInfo2->usri2_logon_server = Ptr;
-
-                memcpy(UserInfo2->usri2_logon_server,
-                       LogonServer.Buffer,
-                       LogonServer.Length);
-                UserInfo2->usri2_logon_server[LogonServer.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + LogonServer.Length + sizeof(WCHAR));
-            }
+            UserInfo2->usri2_logon_server = Ptr;
+            memcpy(UserInfo2->usri2_logon_server,
+                   LogonServer.Buffer,
+                   LogonServer.Length);
+            UserInfo2->usri2_logon_server[LogonServer.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + LogonServer.Length + sizeof(WCHAR));
 
             UserInfo2->usri2_country_code = UserInfo->CountryCode;
             UserInfo2->usri2_code_page = UserInfo->CodePage;
@@ -650,97 +743,64 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
 
             Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserName.Length + sizeof(WCHAR));
 
+            UserInfo3->usri3_password = NULL;
             UserInfo3->usri3_password_age = GetPasswordAge(&UserInfo->PasswordLastSet);
 
             /* FIXME: usri3_priv */
 
-            if (UserInfo->HomeDirectory.Length > 0)
-            {
-                UserInfo3->usri3_home_dir = Ptr;
-
-                memcpy(UserInfo3->usri3_home_dir,
-                       UserInfo->HomeDirectory.Buffer,
-                       UserInfo->HomeDirectory.Length);
-                UserInfo3->usri3_home_dir[UserInfo->HomeDirectory.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectory.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->AdminComment.Length > 0)
-            {
-                UserInfo3->usri3_comment = Ptr;
-
-                memcpy(UserInfo3->usri3_comment,
-                       UserInfo->AdminComment.Buffer,
-                       UserInfo->AdminComment.Length);
-                UserInfo3->usri3_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
-            }
-
-            UserInfo3->usri3_flags = GetAccountFlags(UserInfo->UserAccountControl);
-
-            if (UserInfo->ScriptPath.Length > 0)
-            {
-                UserInfo3->usri3_script_path = Ptr;
-
-                memcpy(UserInfo3->usri3_script_path,
-                       UserInfo->ScriptPath.Buffer,
-                       UserInfo->ScriptPath.Length);
-                UserInfo3->usri3_script_path[UserInfo->ScriptPath.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->ScriptPath.Length + sizeof(WCHAR));
-            }
+            UserInfo3->usri3_home_dir = Ptr;
+            memcpy(UserInfo3->usri3_home_dir,
+                   UserInfo->HomeDirectory.Buffer,
+                   UserInfo->HomeDirectory.Length);
+            UserInfo3->usri3_home_dir[UserInfo->HomeDirectory.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectory.Length + sizeof(WCHAR));
+
+            UserInfo3->usri3_comment = Ptr;
+            memcpy(UserInfo3->usri3_comment,
+                   UserInfo->AdminComment.Buffer,
+                   UserInfo->AdminComment.Length);
+            UserInfo3->usri3_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
+
+            UserInfo3->usri3_flags = GetAccountFlags(UserInfo->UserAccountControl,
+                                                     Dacl);
+
+            UserInfo3->usri3_script_path = Ptr;
+            memcpy(UserInfo3->usri3_script_path,
+                   UserInfo->ScriptPath.Buffer,
+                   UserInfo->ScriptPath.Length);
+            UserInfo3->usri3_script_path[UserInfo->ScriptPath.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->ScriptPath.Length + sizeof(WCHAR));
 
             /* FIXME: usri3_auth_flags */
 
-            if (UserInfo->FullName.Length > 0)
-            {
-                UserInfo3->usri3_full_name = Ptr;
-
-                memcpy(UserInfo3->usri3_full_name,
-                       UserInfo->FullName.Buffer,
-                       UserInfo->FullName.Length);
-                UserInfo3->usri3_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->UserComment.Length > 0)
-            {
-                UserInfo3->usri3_usr_comment = Ptr;
-
-                memcpy(UserInfo3->usri3_usr_comment,
-                       UserInfo->UserComment.Buffer,
-                       UserInfo->UserComment.Length);
-                UserInfo3->usri3_usr_comment[UserInfo->UserComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserComment.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->Parameters.Length > 0)
-            {
-                UserInfo3->usri3_parms = Ptr;
-
-                memcpy(UserInfo3->usri3_parms,
-                       UserInfo->Parameters.Buffer,
-                       UserInfo->Parameters.Length);
-                UserInfo3->usri3_parms[UserInfo->Parameters.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->Parameters.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->WorkStations.Length > 0)
-            {
-                UserInfo3->usri3_workstations = Ptr;
-
-                memcpy(UserInfo3->usri3_workstations,
-                       UserInfo->WorkStations.Buffer,
-                       UserInfo->WorkStations.Length);
-                UserInfo3->usri3_workstations[UserInfo->WorkStations.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->WorkStations.Length + sizeof(WCHAR));
-            }
+            UserInfo3->usri3_full_name = Ptr;
+            memcpy(UserInfo3->usri3_full_name,
+                   UserInfo->FullName.Buffer,
+                   UserInfo->FullName.Length);
+            UserInfo3->usri3_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
+
+            UserInfo3->usri3_usr_comment = Ptr;
+            memcpy(UserInfo3->usri3_usr_comment,
+                   UserInfo->UserComment.Buffer,
+                   UserInfo->UserComment.Length);
+            UserInfo3->usri3_usr_comment[UserInfo->UserComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserComment.Length + sizeof(WCHAR));
+
+            UserInfo3->usri3_parms = Ptr;
+            memcpy(UserInfo3->usri3_parms,
+                   UserInfo->Parameters.Buffer,
+                   UserInfo->Parameters.Length);
+            UserInfo3->usri3_parms[UserInfo->Parameters.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->Parameters.Length + sizeof(WCHAR));
+
+            UserInfo3->usri3_workstations = Ptr;
+            memcpy(UserInfo3->usri3_workstations,
+                   UserInfo->WorkStations.Buffer,
+                   UserInfo->WorkStations.Length);
+            UserInfo3->usri3_workstations[UserInfo->WorkStations.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->WorkStations.Length + sizeof(WCHAR));
 
             RtlTimeToSecondsSince1970(&UserInfo->LastLogon,
                                       &UserInfo3->usri3_last_logon);
@@ -768,46 +828,31 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
             UserInfo3->usri3_bad_pw_count = UserInfo->BadPasswordCount;
             UserInfo3->usri3_num_logons = UserInfo->LogonCount;
 
-            if (LogonServer.Length > 0)
-            {
-                UserInfo3->usri3_logon_server = Ptr;
-
-                memcpy(UserInfo3->usri3_logon_server,
-                       LogonServer.Buffer,
-                       LogonServer.Length);
-                UserInfo3->usri3_logon_server[LogonServer.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + LogonServer.Length + sizeof(WCHAR));
-            }
+            UserInfo3->usri3_logon_server = Ptr;
+            memcpy(UserInfo3->usri3_logon_server,
+                   LogonServer.Buffer,
+                   LogonServer.Length);
+            UserInfo3->usri3_logon_server[LogonServer.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + LogonServer.Length + sizeof(WCHAR));
 
             UserInfo3->usri3_country_code = UserInfo->CountryCode;
             UserInfo3->usri3_code_page = UserInfo->CodePage;
             UserInfo3->usri3_user_id = RelativeId;
             UserInfo3->usri3_primary_group_id = UserInfo->PrimaryGroupId;
 
-            if (UserInfo->ProfilePath.Length > 0)
-            {
-                UserInfo3->usri3_profile = Ptr;
-
-                memcpy(UserInfo3->usri3_profile,
-                       UserInfo->ProfilePath.Buffer,
-                       UserInfo->ProfilePath.Length);
-                UserInfo3->usri3_profile[UserInfo->ProfilePath.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            UserInfo3->usri3_profile = Ptr;
+            memcpy(UserInfo3->usri3_profile,
+                   UserInfo->ProfilePath.Buffer,
+                   UserInfo->ProfilePath.Length);
+            UserInfo3->usri3_profile[UserInfo->ProfilePath.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->ProfilePath.Length + sizeof(WCHAR));
 
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->ProfilePath.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->HomeDirectoryDrive.Length > 0)
-            {
-                UserInfo3->usri3_home_dir_drive = Ptr;
-
-                memcpy(UserInfo3->usri3_home_dir_drive,
-                       UserInfo->HomeDirectoryDrive.Buffer,
-                       UserInfo->HomeDirectoryDrive.Length);
-                UserInfo3->usri3_home_dir_drive[UserInfo->HomeDirectoryDrive.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectoryDrive.Length + sizeof(WCHAR));
-            }
+            UserInfo3->usri3_home_dir_drive = Ptr;
+            memcpy(UserInfo3->usri3_home_dir_drive,
+                   UserInfo->HomeDirectoryDrive.Buffer,
+                   UserInfo->HomeDirectoryDrive.Length);
+            UserInfo3->usri3_home_dir_drive[UserInfo->HomeDirectoryDrive.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectoryDrive.Length + sizeof(WCHAR));
 
             UserInfo3->usri3_password_expired = (UserInfo->UserAccountControl & USER_PASSWORD_EXPIRED);
             break;
@@ -831,93 +876,59 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
 
             /* FIXME: usri4_priv */
 
-            if (UserInfo->HomeDirectory.Length > 0)
-            {
-                UserInfo4->usri4_home_dir = Ptr;
-
-                memcpy(UserInfo4->usri4_home_dir,
-                       UserInfo->HomeDirectory.Buffer,
-                       UserInfo->HomeDirectory.Length);
-                UserInfo4->usri4_home_dir[UserInfo->HomeDirectory.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectory.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->AdminComment.Length > 0)
-            {
-                UserInfo4->usri4_comment = Ptr;
-
-                memcpy(UserInfo4->usri4_comment,
-                       UserInfo->AdminComment.Buffer,
-                       UserInfo->AdminComment.Length);
-                UserInfo4->usri4_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
-            }
-
-            UserInfo4->usri4_flags = GetAccountFlags(UserInfo->UserAccountControl);
-
-            if (UserInfo->ScriptPath.Length > 0)
-            {
-                UserInfo4->usri4_script_path = Ptr;
-
-                memcpy(UserInfo4->usri4_script_path,
-                       UserInfo->ScriptPath.Buffer,
-                       UserInfo->ScriptPath.Length);
-                UserInfo4->usri4_script_path[UserInfo->ScriptPath.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->ScriptPath.Length + sizeof(WCHAR));
-            }
+            UserInfo4->usri4_home_dir = Ptr;
+            memcpy(UserInfo4->usri4_home_dir,
+                   UserInfo->HomeDirectory.Buffer,
+                   UserInfo->HomeDirectory.Length);
+            UserInfo4->usri4_home_dir[UserInfo->HomeDirectory.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectory.Length + sizeof(WCHAR));
+
+            UserInfo4->usri4_comment = Ptr;
+            memcpy(UserInfo4->usri4_comment,
+                   UserInfo->AdminComment.Buffer,
+                   UserInfo->AdminComment.Length);
+            UserInfo4->usri4_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
+
+            UserInfo4->usri4_flags = GetAccountFlags(UserInfo->UserAccountControl,
+                                                     Dacl);
+
+            UserInfo4->usri4_script_path = Ptr;
+            memcpy(UserInfo4->usri4_script_path,
+                   UserInfo->ScriptPath.Buffer,
+                   UserInfo->ScriptPath.Length);
+            UserInfo4->usri4_script_path[UserInfo->ScriptPath.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->ScriptPath.Length + sizeof(WCHAR));
 
             /* FIXME: usri4_auth_flags */
 
-            if (UserInfo->FullName.Length > 0)
-            {
-                UserInfo4->usri4_full_name = Ptr;
-
-                memcpy(UserInfo4->usri4_full_name,
-                       UserInfo->FullName.Buffer,
-                       UserInfo->FullName.Length);
-                UserInfo4->usri4_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->UserComment.Length > 0)
-            {
-                UserInfo4->usri4_usr_comment = Ptr;
-
-                memcpy(UserInfo4->usri4_usr_comment,
-                       UserInfo->UserComment.Buffer,
-                       UserInfo->UserComment.Length);
-                UserInfo4->usri4_usr_comment[UserInfo->UserComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserComment.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->Parameters.Length > 0)
-            {
-                UserInfo4->usri4_parms = Ptr;
-
-                memcpy(UserInfo4->usri4_parms,
-                       UserInfo->Parameters.Buffer,
-                       UserInfo->Parameters.Length);
-                UserInfo4->usri4_parms[UserInfo->Parameters.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->Parameters.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->WorkStations.Length > 0)
-            {
-                UserInfo4->usri4_workstations = Ptr;
-
-                memcpy(UserInfo4->usri4_workstations,
-                       UserInfo->WorkStations.Buffer,
-                       UserInfo->WorkStations.Length);
-                UserInfo4->usri4_workstations[UserInfo->WorkStations.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->WorkStations.Length + sizeof(WCHAR));
-            }
+            UserInfo4->usri4_full_name = Ptr;
+            memcpy(UserInfo4->usri4_full_name,
+                   UserInfo->FullName.Buffer,
+                   UserInfo->FullName.Length);
+            UserInfo4->usri4_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
+
+            UserInfo4->usri4_usr_comment = Ptr;
+            memcpy(UserInfo4->usri4_usr_comment,
+                   UserInfo->UserComment.Buffer,
+                   UserInfo->UserComment.Length);
+            UserInfo4->usri4_usr_comment[UserInfo->UserComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserComment.Length + sizeof(WCHAR));
+
+            UserInfo4->usri4_parms = Ptr;
+            memcpy(UserInfo4->usri4_parms,
+                   UserInfo->Parameters.Buffer,
+                   UserInfo->Parameters.Length);
+            UserInfo4->usri4_parms[UserInfo->Parameters.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->Parameters.Length + sizeof(WCHAR));
+
+            UserInfo4->usri4_workstations = Ptr;
+            memcpy(UserInfo4->usri4_workstations,
+                   UserInfo->WorkStations.Buffer,
+                   UserInfo->WorkStations.Length);
+            UserInfo4->usri4_workstations[UserInfo->WorkStations.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->WorkStations.Length + sizeof(WCHAR));
 
             RtlTimeToSecondsSince1970(&UserInfo->LastLogon,
                                       &UserInfo4->usri4_last_logon);
@@ -945,17 +956,12 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
             UserInfo4->usri4_bad_pw_count = UserInfo->BadPasswordCount;
             UserInfo4->usri4_num_logons = UserInfo->LogonCount;
 
-            if (LogonServer.Length > 0)
-            {
-                UserInfo4->usri4_logon_server = Ptr;
-
-                memcpy(UserInfo4->usri4_logon_server,
-                       LogonServer.Buffer,
-                       LogonServer.Length);
-                UserInfo4->usri4_logon_server[LogonServer.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + LogonServer.Length + sizeof(WCHAR));
-            }
+            UserInfo4->usri4_logon_server = Ptr;
+            memcpy(UserInfo4->usri4_logon_server,
+                   LogonServer.Buffer,
+                   LogonServer.Length);
+            UserInfo4->usri4_logon_server[LogonServer.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + LogonServer.Length + sizeof(WCHAR));
 
             UserInfo4->usri4_country_code = UserInfo->CountryCode;
             UserInfo4->usri4_code_page = UserInfo->CodePage;
@@ -964,29 +970,19 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
 
             UserInfo4->usri4_primary_group_id = UserInfo->PrimaryGroupId;
 
-            if (UserInfo->ProfilePath.Length > 0)
-            {
-                UserInfo4->usri4_profile = Ptr;
-
-                memcpy(UserInfo4->usri4_profile,
-                       UserInfo->ProfilePath.Buffer,
-                       UserInfo->ProfilePath.Length);
-                UserInfo4->usri4_profile[UserInfo->ProfilePath.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->ProfilePath.Length + sizeof(WCHAR));
-            }
+            UserInfo4->usri4_profile = Ptr;
+            memcpy(UserInfo4->usri4_profile,
+                   UserInfo->ProfilePath.Buffer,
+                   UserInfo->ProfilePath.Length);
+            UserInfo4->usri4_profile[UserInfo->ProfilePath.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->ProfilePath.Length + sizeof(WCHAR));
 
-            if (UserInfo->HomeDirectoryDrive.Length > 0)
-            {
-                UserInfo4->usri4_home_dir_drive = Ptr;
-
-                memcpy(UserInfo4->usri4_home_dir_drive,
-                       UserInfo->HomeDirectoryDrive.Buffer,
-                       UserInfo->HomeDirectoryDrive.Length);
-                UserInfo4->usri4_home_dir_drive[UserInfo->HomeDirectoryDrive.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectoryDrive.Length + sizeof(WCHAR));
-            }
+            UserInfo4->usri4_home_dir_drive = Ptr;
+            memcpy(UserInfo4->usri4_home_dir_drive,
+                   UserInfo->HomeDirectoryDrive.Buffer,
+                   UserInfo->HomeDirectoryDrive.Length);
+            UserInfo4->usri4_home_dir_drive[UserInfo->HomeDirectoryDrive.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectoryDrive.Length + sizeof(WCHAR));
 
             UserInfo4->usri4_password_expired = (UserInfo->UserAccountControl & USER_PASSWORD_EXPIRED);
             break;
@@ -1005,41 +1001,25 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
 
             Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserName.Length + sizeof(WCHAR));
 
-            if (UserInfo->AdminComment.Length > 0)
-            {
-                UserInfo10->usri10_comment = Ptr;
-
-                memcpy(UserInfo10->usri10_comment,
-                       UserInfo->AdminComment.Buffer,
-                       UserInfo->AdminComment.Length);
-                UserInfo10->usri10_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->UserComment.Length > 0)
-            {
-                UserInfo10->usri10_usr_comment = Ptr;
-
-                memcpy(UserInfo10->usri10_usr_comment,
-                       UserInfo->UserComment.Buffer,
-                       UserInfo->UserComment.Length);
-                UserInfo10->usri10_usr_comment[UserInfo->UserComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserComment.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->FullName.Length > 0)
-            {
-                UserInfo10->usri10_full_name = Ptr;
-
-                memcpy(UserInfo10->usri10_full_name,
-                       UserInfo->FullName.Buffer,
-                       UserInfo->FullName.Length);
-                UserInfo10->usri10_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
-            }
+            UserInfo10->usri10_comment = Ptr;
+            memcpy(UserInfo10->usri10_comment,
+                   UserInfo->AdminComment.Buffer,
+                   UserInfo->AdminComment.Length);
+            UserInfo10->usri10_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
+
+            UserInfo10->usri10_usr_comment = Ptr;
+            memcpy(UserInfo10->usri10_usr_comment,
+                   UserInfo->UserComment.Buffer,
+                   UserInfo->UserComment.Length);
+            UserInfo10->usri10_usr_comment[UserInfo->UserComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserComment.Length + sizeof(WCHAR));
+
+            UserInfo10->usri10_full_name = Ptr;
+            memcpy(UserInfo10->usri10_full_name,
+                   UserInfo->FullName.Buffer,
+                   UserInfo->FullName.Length);
+            UserInfo10->usri10_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
             break;
 
         case 11:
@@ -1056,70 +1036,45 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
 
             Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserName.Length + sizeof(WCHAR));
 
-            if (UserInfo->AdminComment.Length > 0)
-            {
-                UserInfo11->usri11_comment = Ptr;
-
-                memcpy(UserInfo11->usri11_comment,
-                       UserInfo->AdminComment.Buffer,
-                       UserInfo->AdminComment.Length);
-                UserInfo11->usri11_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->UserComment.Length > 0)
-            {
-                UserInfo11->usri11_usr_comment = Ptr;
-
-                memcpy(UserInfo11->usri11_usr_comment,
-                       UserInfo->UserComment.Buffer,
-                       UserInfo->UserComment.Length);
-                UserInfo11->usri11_usr_comment[UserInfo->UserComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserComment.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->FullName.Length > 0)
-            {
-                UserInfo11->usri11_full_name = Ptr;
-
-                memcpy(UserInfo11->usri11_full_name,
-                       UserInfo->FullName.Buffer,
-                       UserInfo->FullName.Length);
-                UserInfo11->usri11_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
-            }
+            UserInfo11->usri11_comment = Ptr;
+            memcpy(UserInfo11->usri11_comment,
+                   UserInfo->AdminComment.Buffer,
+                   UserInfo->AdminComment.Length);
+            UserInfo11->usri11_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
+
+            UserInfo11->usri11_usr_comment = Ptr;
+            memcpy(UserInfo11->usri11_usr_comment,
+                   UserInfo->UserComment.Buffer,
+                   UserInfo->UserComment.Length);
+            UserInfo11->usri11_usr_comment[UserInfo->UserComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserComment.Length + sizeof(WCHAR));
+
+            UserInfo11->usri11_full_name = Ptr;
+            memcpy(UserInfo11->usri11_full_name,
+                   UserInfo->FullName.Buffer,
+                   UserInfo->FullName.Length);
+            UserInfo11->usri11_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
 
             /* FIXME: usri11_priv */
             /* FIXME: usri11_auth_flags */
 
             UserInfo11->usri11_password_age = GetPasswordAge(&UserInfo->PasswordLastSet);
 
-            if (UserInfo->HomeDirectory.Length > 0)
-            {
-                UserInfo11->usri11_home_dir = Ptr;
-
-                memcpy(UserInfo11->usri11_home_dir,
-                       UserInfo->HomeDirectory.Buffer,
-                       UserInfo->HomeDirectory.Length);
-                UserInfo11->usri11_home_dir[UserInfo->HomeDirectory.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectory.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->Parameters.Length > 0)
-            {
-                UserInfo11->usri11_parms = Ptr;
-
-                memcpy(UserInfo11->usri11_parms,
-                       UserInfo->Parameters.Buffer,
-                       UserInfo->Parameters.Length);
-                UserInfo11->usri11_parms[UserInfo->Parameters.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            UserInfo11->usri11_home_dir = Ptr;
+            memcpy(UserInfo11->usri11_home_dir,
+                   UserInfo->HomeDirectory.Buffer,
+                   UserInfo->HomeDirectory.Length);
+            UserInfo11->usri11_home_dir[UserInfo->HomeDirectory.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->HomeDirectory.Length + sizeof(WCHAR));
 
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->Parameters.Length + sizeof(WCHAR));
-            }
+            UserInfo11->usri11_parms = Ptr;
+            memcpy(UserInfo11->usri11_parms,
+                   UserInfo->Parameters.Buffer,
+                   UserInfo->Parameters.Length);
+            UserInfo11->usri11_parms[UserInfo->Parameters.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->Parameters.Length + sizeof(WCHAR));
 
             RtlTimeToSecondsSince1970(&UserInfo->LastLogon,
                                       &UserInfo11->usri11_last_logon);
@@ -1130,31 +1085,21 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
             UserInfo11->usri11_bad_pw_count = UserInfo->BadPasswordCount;
             UserInfo11->usri11_num_logons = UserInfo->LogonCount;
 
-            if (LogonServer.Length > 0)
-            {
-                UserInfo11->usri11_logon_server = Ptr;
-
-                memcpy(UserInfo11->usri11_logon_server,
-                       LogonServer.Buffer,
-                       LogonServer.Length);
-                UserInfo11->usri11_logon_server[LogonServer.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + LogonServer.Length + sizeof(WCHAR));
-            }
+            UserInfo11->usri11_logon_server = Ptr;
+            memcpy(UserInfo11->usri11_logon_server,
+                   LogonServer.Buffer,
+                   LogonServer.Length);
+            UserInfo11->usri11_logon_server[LogonServer.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + LogonServer.Length + sizeof(WCHAR));
 
             UserInfo11->usri11_country_code = UserInfo->CountryCode;
 
-            if (UserInfo->WorkStations.Length > 0)
-            {
-                UserInfo11->usri11_workstations = Ptr;
-
-                memcpy(UserInfo11->usri11_workstations,
-                       UserInfo->WorkStations.Buffer,
-                       UserInfo->WorkStations.Length);
-                UserInfo11->usri11_workstations[UserInfo->WorkStations.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->WorkStations.Length + sizeof(WCHAR));
-            }
+            UserInfo11->usri11_workstations = Ptr;
+            memcpy(UserInfo11->usri11_workstations,
+                   UserInfo->WorkStations.Buffer,
+                   UserInfo->WorkStations.Length);
+            UserInfo11->usri11_workstations[UserInfo->WorkStations.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->WorkStations.Length + sizeof(WCHAR));
 
             UserInfo11->usri11_max_storage = USER_MAXSTORAGE_UNLIMITED;
             UserInfo11->usri11_units_per_week = UserInfo->LogonHours.UnitsPerWeek;
@@ -1187,31 +1132,22 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
 
             Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserName.Length + sizeof(WCHAR));
 
-            if (UserInfo->FullName.Length > 0)
-            {
-                UserInfo20->usri20_full_name = Ptr;
-
-                memcpy(UserInfo20->usri20_full_name,
-                       UserInfo->FullName.Buffer,
-                       UserInfo->FullName.Length);
-                UserInfo20->usri20_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->AdminComment.Length > 0)
-            {
-                UserInfo20->usri20_comment = Ptr;
-
-                memcpy(UserInfo20->usri20_comment,
-                       UserInfo->AdminComment.Buffer,
-                       UserInfo->AdminComment.Length);
-                UserInfo20->usri20_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            UserInfo20->usri20_full_name = Ptr;
+            memcpy(UserInfo20->usri20_full_name,
+                   UserInfo->FullName.Buffer,
+                   UserInfo->FullName.Length);
+            UserInfo20->usri20_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
 
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
-            }
+            UserInfo20->usri20_comment = Ptr;
+            memcpy(UserInfo20->usri20_comment,
+                   UserInfo->AdminComment.Buffer,
+                   UserInfo->AdminComment.Length);
+            UserInfo20->usri20_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
 
-            UserInfo20->usri20_flags = GetAccountFlags(UserInfo->UserAccountControl);
+            UserInfo20->usri20_flags = GetAccountFlags(UserInfo->UserAccountControl,
+                                                       Dacl);
 
             UserInfo20->usri20_user_id = RelativeId;
             break;
@@ -1230,92 +1166,45 @@ BuildUserInfoBuffer(PUSER_ALL_INFORMATION UserInfo,
 
             Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->UserName.Length + sizeof(WCHAR));
 
-            if (UserInfo->FullName.Length > 0)
-            {
-                UserInfo23->usri23_full_name = Ptr;
-
-                memcpy(UserInfo23->usri23_full_name,
-                       UserInfo->FullName.Buffer,
-                       UserInfo->FullName.Length);
-                UserInfo23->usri23_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
-
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
-            }
-
-            if (UserInfo->AdminComment.Length > 0)
-            {
-                UserInfo23->usri23_comment = Ptr;
-
-                memcpy(UserInfo23->usri23_comment,
-                       UserInfo->AdminComment.Buffer,
-                       UserInfo->AdminComment.Length);
-                UserInfo23->usri23_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            UserInfo23->usri23_full_name = Ptr;
+            memcpy(UserInfo23->usri23_full_name,
+                   UserInfo->FullName.Buffer,
+                   UserInfo->FullName.Length);
+            UserInfo23->usri23_full_name[UserInfo->FullName.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->FullName.Length + sizeof(WCHAR));
 
-                Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
-            }
+            UserInfo23->usri23_comment = Ptr;
+            memcpy(UserInfo23->usri23_comment,
+                   UserInfo->AdminComment.Buffer,
+                   UserInfo->AdminComment.Length);
+            UserInfo23->usri23_comment[UserInfo->AdminComment.Length / sizeof(WCHAR)] = UNICODE_NULL;
+            Ptr = (LPWSTR)((ULONG_PTR)Ptr + UserInfo->AdminComment.Length + sizeof(WCHAR));
 
-            UserInfo23->usri23_flags = GetAccountFlags(UserInfo->UserAccountControl);
+            UserInfo23->usri23_flags = GetAccountFlags(UserInfo->UserAccountControl,
+                                                       Dacl);
 
             /* FIXME: usri23_user_sid */
-           break;
+            break;
     }
 
 done:
-    if (ApiStatus == NERR_Success)
-    {
-        *Buffer = LocalBuffer;
-    }
-    else
-    {
-        if (LocalBuffer != NULL)
-            NetApiBufferFree(LocalBuffer);
-    }
-
-    return ApiStatus;
-}
-
-
-static
-VOID
-FreeUserInfo(PUSER_ALL_INFORMATION UserInfo)
-{
-    if (UserInfo->UserName.Buffer != NULL)
-        SamFreeMemory(UserInfo->UserName.Buffer);
-
-    if (UserInfo->FullName.Buffer != NULL)
-        SamFreeMemory(UserInfo->FullName.Buffer);
-
-    if (UserInfo->HomeDirectory.Buffer != NULL)
-        SamFreeMemory(UserInfo->HomeDirectory.Buffer);
-
-    if (UserInfo->HomeDirectoryDrive.Buffer != NULL)
-        SamFreeMemory(UserInfo->HomeDirectoryDrive.Buffer);
-
-    if (UserInfo->ScriptPath.Buffer != NULL)
-        SamFreeMemory(UserInfo->ScriptPath.Buffer);
-
-    if (UserInfo->ProfilePath.Buffer != NULL)
-        SamFreeMemory(UserInfo->ProfilePath.Buffer);
-
-    if (UserInfo->AdminComment.Buffer != NULL)
-        SamFreeMemory(UserInfo->AdminComment.Buffer);
-
-    if (UserInfo->WorkStations.Buffer != NULL)
-        SamFreeMemory(UserInfo->WorkStations.Buffer);
-
-    if (UserInfo->UserComment.Buffer != NULL)
-        SamFreeMemory(UserInfo->UserComment.Buffer);
-
-    if (UserInfo->Parameters.Buffer != NULL)
-        SamFreeMemory(UserInfo->Parameters.Buffer);
+    if (UserInfo != NULL)
+        FreeUserInfo(UserInfo);
 
-    if (UserInfo->PrivateData.Buffer != NULL)
-        SamFreeMemory(UserInfo->PrivateData.Buffer);
+    if (Dacl != NULL)
+        HeapFree(GetProcessHeap(), 0, Dacl);
 
-    if (UserInfo->LogonHours.LogonHours != NULL)
-        SamFreeMemory(UserInfo->LogonHours.LogonHours);
+    if (ApiStatus == NERR_Success)
+    {
+        *Buffer = LocalBuffer;
+    }
+    else
+    {
+        if (LocalBuffer != NULL)
+            NetApiBufferFree(LocalBuffer);
+    }
 
-    SamFreeMemory(UserInfo);
+    return ApiStatus;
 }
 
 
@@ -1331,6 +1220,7 @@ SetUserInfo(SAM_HANDLE UserHandle,
     PUSER_INFO_2 UserInfo2;
     PUSER_INFO_3 UserInfo3;
     PUSER_INFO_4 UserInfo4;
+    PUSER_INFO_22 UserInfo22;
     PUSER_INFO_1003 UserInfo1003;
     PUSER_INFO_1006 UserInfo1006;
     PUSER_INFO_1007 UserInfo1007;
@@ -1347,11 +1237,20 @@ SetUserInfo(SAM_HANDLE UserHandle,
     PUSER_INFO_1051 UserInfo1051;
     PUSER_INFO_1052 UserInfo1052;
     PUSER_INFO_1053 UserInfo1053;
+    PACL Dacl = NULL;
     NET_API_STATUS ApiStatus = NERR_Success;
     NTSTATUS Status = STATUS_SUCCESS;
 
     ZeroMemory(&UserAllInfo, sizeof(USER_ALL_INFORMATION));
 
+    if ((Level == 1) || (Level == 2) || (Level == 3) ||
+        (Level == 4) || (Level == 22) || (Level == 1008))
+    {
+        ApiStatus = GetUserDacl(UserHandle, &Dacl);
+        if (ApiStatus != NERR_Success)
+            goto done;
+    }
+
     switch (Level)
     {
         case 0:
@@ -1394,6 +1293,7 @@ SetUserInfo(SAM_HANDLE UserHandle,
                 UserAllInfo.WhichFields |= USER_ALL_ADMINCOMMENT;
             }
 
+            ChangeUserDacl(Dacl, UserInfo1->usri1_flags);
             UserAllInfo.UserAccountControl = GetAccountControl(UserInfo1->usri1_flags);
             UserAllInfo.WhichFields |= USER_ALL_USERACCOUNTCONTROL;
 
@@ -1436,6 +1336,7 @@ SetUserInfo(SAM_HANDLE UserHandle,
                 UserAllInfo.WhichFields |= USER_ALL_ADMINCOMMENT;
             }
 
+            ChangeUserDacl(Dacl, UserInfo2->usri2_flags);
             UserAllInfo.UserAccountControl = GetAccountControl(UserInfo2->usri2_flags);
             UserAllInfo.WhichFields |= USER_ALL_USERACCOUNTCONTROL;
 
@@ -1538,6 +1439,7 @@ SetUserInfo(SAM_HANDLE UserHandle,
                 UserAllInfo.WhichFields |= USER_ALL_ADMINCOMMENT;
             }
 
+            ChangeUserDacl(Dacl, UserInfo3->usri3_flags);
             UserAllInfo.UserAccountControl = GetAccountControl(UserInfo3->usri3_flags);
             UserAllInfo.WhichFields |= USER_ALL_USERACCOUNTCONTROL;
 
@@ -1662,6 +1564,7 @@ SetUserInfo(SAM_HANDLE UserHandle,
                 UserAllInfo.WhichFields |= USER_ALL_ADMINCOMMENT;
             }
 
+            ChangeUserDacl(Dacl, UserInfo4->usri4_flags);
             UserAllInfo.UserAccountControl = GetAccountControl(UserInfo4->usri4_flags);
             UserAllInfo.WhichFields |= USER_ALL_USERACCOUNTCONTROL;
 
@@ -1719,8 +1622,8 @@ SetUserInfo(SAM_HANDLE UserHandle,
 
             // usri4_max_storage ignored
 
-//          UserInfo3->usri4_units_per_week;
-//          UserInfo3->usri4_logon_hours;
+//          UserInfo4->usri4_units_per_week;
+//          UserInfo4->usri4_logon_hours;
 
             // usri4_bad_pw_count ignored
             // usri4_num_logons ignored
@@ -1756,7 +1659,104 @@ SetUserInfo(SAM_HANDLE UserHandle,
             break;
 
 //        case 21:
-//        case 22:
+//            break;
+
+        case 22:
+            UserInfo22 = (PUSER_INFO_22)UserInfo;
+
+            // usri22_name ignored
+
+//          UserInfo22->usri22_password[ENCRYPTED_PWLEN];
+
+            // usri22_password_age ignored
+
+//          UserInfo3->usri3_priv;
+
+            if (UserInfo22->usri22_home_dir != NULL)
+            {
+                RtlInitUnicodeString(&UserAllInfo.HomeDirectory,
+                                     UserInfo22->usri22_home_dir);
+                UserAllInfo.WhichFields |= USER_ALL_HOMEDIRECTORY;
+            }
+
+            if (UserInfo22->usri22_comment != NULL)
+            {
+                RtlInitUnicodeString(&UserAllInfo.AdminComment,
+                                     UserInfo22->usri22_comment);
+                UserAllInfo.WhichFields |= USER_ALL_ADMINCOMMENT;
+            }
+
+            ChangeUserDacl(Dacl, UserInfo22->usri22_flags);
+            UserAllInfo.UserAccountControl = GetAccountControl(UserInfo22->usri22_flags);
+            UserAllInfo.WhichFields |= USER_ALL_USERACCOUNTCONTROL;
+
+            if (UserInfo22->usri22_script_path != NULL)
+            {
+                RtlInitUnicodeString(&UserAllInfo.ScriptPath,
+                                     UserInfo22->usri22_script_path);
+                UserAllInfo.WhichFields |= USER_ALL_SCRIPTPATH;
+            }
+
+//          UserInfo22->usri22_auth_flags;
+
+            if (UserInfo22->usri22_full_name != NULL)
+            {
+                RtlInitUnicodeString(&UserAllInfo.FullName,
+                                     UserInfo22->usri22_full_name);
+                UserAllInfo.WhichFields |= USER_ALL_FULLNAME;
+            }
+
+            if (UserInfo22->usri22_usr_comment != NULL)
+            {
+                RtlInitUnicodeString(&UserAllInfo.UserComment,
+                                     UserInfo22->usri22_usr_comment);
+                UserAllInfo.WhichFields |= USER_ALL_USERCOMMENT;
+            }
+
+            if (UserInfo22->usri22_parms != NULL)
+            {
+                RtlInitUnicodeString(&UserAllInfo.Parameters,
+                                     UserInfo22->usri22_parms);
+                UserAllInfo.WhichFields |= USER_ALL_PARAMETERS;
+            }
+
+            if (UserInfo22->usri22_workstations != NULL)
+            {
+                RtlInitUnicodeString(&UserAllInfo.WorkStations,
+                                     UserInfo22->usri22_workstations);
+                UserAllInfo.WhichFields |= USER_ALL_WORKSTATIONS;
+            }
+
+            // usri22_last_logon ignored
+            // usri22_last_logoff ignored
+
+            if (UserInfo22->usri22_acct_expires == TIMEQ_FOREVER)
+            {
+                UserAllInfo.AccountExpires.LowPart = 0;
+                UserAllInfo.AccountExpires.HighPart = 0;
+            }
+            else
+            {
+                RtlSecondsSince1970ToTime(UserInfo22->usri22_acct_expires,
+                                          &UserAllInfo.AccountExpires);
+            }
+            UserAllInfo.WhichFields |= USER_ALL_ACCOUNTEXPIRES;
+
+            // usri22_max_storage ignored
+
+//          UserInfo22->usri22_units_per_week;
+//          UserInfo22->usri22_logon_hours;
+
+            // usri22_bad_pw_count ignored
+            // usri22_num_logons ignored
+            // usri22_logon_server ignored
+
+            UserAllInfo.CountryCode = UserInfo22->usri22_country_code;
+            UserAllInfo.WhichFields |= USER_ALL_COUNTRYCODE;
+
+            UserAllInfo.CodePage = UserInfo22->usri22_code_page;
+            UserAllInfo.WhichFields |= USER_ALL_CODEPAGE;
+            break;
 
         case 1003:
             UserInfo1003 = (PUSER_INFO_1003)UserInfo;
@@ -1771,6 +1771,7 @@ SetUserInfo(SAM_HANDLE UserHandle,
             break;
 
 //        case 1005:
+//            break;
 
         case 1006:
             UserInfo1006 = (PUSER_INFO_1006)UserInfo;
@@ -1796,6 +1797,7 @@ SetUserInfo(SAM_HANDLE UserHandle,
 
         case 1008:
             UserInfo1008 = (PUSER_INFO_1008)UserInfo;
+            ChangeUserDacl(Dacl, UserInfo1008->usri1008_flags);
             UserAllInfo.UserAccountControl = GetAccountControl(UserInfo1008->usri1008_flags);
             UserAllInfo.WhichFields |= USER_ALL_USERACCOUNTCONTROL;
             break;
@@ -1812,6 +1814,7 @@ SetUserInfo(SAM_HANDLE UserHandle,
             break;
 
 //        case 1010:
+//            break;
 
         case 1011:
             UserInfo1011 = (PUSER_INFO_1011)UserInfo;
@@ -1884,6 +1887,7 @@ SetUserInfo(SAM_HANDLE UserHandle,
             break;
 
 //        case 1020:
+//            break;
 
         case 1024:
             UserInfo1024 = (PUSER_INFO_1024)UserInfo;
@@ -1944,6 +1948,9 @@ SetUserInfo(SAM_HANDLE UserHandle,
     }
 
 done:
+    if (Dacl != NULL)
+        HeapFree(GetProcessHeap(), 0, Dacl);
+
     return ApiStatus;
 }
 
@@ -2056,7 +2063,7 @@ NetUserAdd(LPCWSTR servername,
     /* Open the Account Domain */
     Status = OpenAccountDomain(ServerHandle,
                                (servername != NULL) ? &ServerName : NULL,
-                               DOMAIN_CREATE_USER | DOMAIN_LOOKUP,
+                               DOMAIN_CREATE_USER | DOMAIN_LOOKUP | DOMAIN_READ_PASSWORD_PARAMETERS,
                                &DomainHandle);
     if (!NT_SUCCESS(Status))
     {
@@ -2096,7 +2103,12 @@ NetUserAdd(LPCWSTR servername,
 
 done:
     if (UserHandle != NULL)
-        SamCloseHandle(UserHandle);
+    {
+        if (ApiStatus != NERR_Success)
+            SamDeleteUser(UserHandle);
+        else
+            SamCloseHandle(UserHandle);
+    }
 
     if (DomainHandle != NULL)
         SamCloseHandle(DomainHandle);
@@ -2350,7 +2362,7 @@ NetUserDel(LPCWSTR servername,
         {
             ERR("OpenUserByName failed (ApiStatus %lu)\n", ApiStatus);
             if (ApiStatus == ERROR_NONE_MAPPED)
-                ApiStatus = NERR_GroupNotFound;
+                ApiStatus = NERR_UserNotFound;
             goto done;
         }
     }
@@ -2364,6 +2376,9 @@ NetUserDel(LPCWSTR servername,
         goto done;
     }
 
+    /* A successful delete invalidates the handle */
+    UserHandle = NULL;
+
 done:
     if (UserHandle != NULL)
         SamCloseHandle(UserHandle);
@@ -2398,8 +2413,6 @@ NetUserEnum(LPCWSTR servername,
     LPVOID Buffer = NULL;
     ULONG i;
     SAM_HANDLE UserHandle = NULL;
-    PUSER_ALL_INFORMATION UserInfo = NULL;
-
     NET_API_STATUS ApiStatus = NERR_Success;
     NTSTATUS Status = STATUS_SUCCESS;
 
@@ -2512,7 +2525,7 @@ NetUserEnum(LPCWSTR servername,
         TRACE("RID: %lu\n", CurrentUser->RelativeId);
 
         Status = SamOpenUser(EnumContext->AccountDomainHandle, //BuiltinDomainHandle,
-                             USER_READ_GENERAL | USER_READ_PREFERENCES | USER_READ_LOGON | USER_READ_ACCOUNT,
+                             READ_CONTROL | USER_READ_GENERAL | USER_READ_PREFERENCES | USER_READ_LOGON | USER_READ_ACCOUNT,
                              CurrentUser->RelativeId,
                              &UserHandle);
         if (!NT_SUCCESS(Status))
@@ -2522,20 +2535,7 @@ NetUserEnum(LPCWSTR servername,
             goto done;
         }
 
-        Status = SamQueryInformationUser(UserHandle,
-                                         UserAllInformation,
-                                         (PVOID *)&UserInfo);
-        if (!NT_SUCCESS(Status))
-        {
-            ERR("SamQueryInformationUser failed (Status %08lx)\n", Status);
-            ApiStatus = NetpNtStatusToApiStatus(Status);
-            goto done;
-        }
-
-        SamCloseHandle(UserHandle);
-        UserHandle = NULL;
-
-        ApiStatus = BuildUserInfoBuffer(UserInfo,
+        ApiStatus = BuildUserInfoBuffer(UserHandle,
                                         level,
                                         CurrentUser->RelativeId,
                                         &Buffer);
@@ -2545,11 +2545,8 @@ NetUserEnum(LPCWSTR servername,
             goto done;
         }
 
-        if (UserInfo != NULL)
-        {
-            FreeUserInfo(UserInfo);
-            UserInfo = NULL;
-        }
+        SamCloseHandle(UserHandle);
+        UserHandle = NULL;
 
         EnumContext->Index++;
 
@@ -2557,7 +2554,7 @@ NetUserEnum(LPCWSTR servername,
 //    }
 
 done:
-    if (ApiStatus == NERR_Success && EnumContext->Index < EnumContext->Count)
+    if (ApiStatus == NERR_Success && EnumContext != NULL && EnumContext->Index < EnumContext->Count)
         ApiStatus = ERROR_MORE_DATA;
 
     if (EnumContext != NULL)
@@ -2594,9 +2591,6 @@ done:
     if (UserHandle != NULL)
         SamCloseHandle(UserHandle);
 
-    if (UserInfo != NULL)
-        FreeUserInfo(UserInfo);
-
     if (resume_handle != NULL)
         *resume_handle = (DWORD_PTR)EnumContext;
 
@@ -2621,15 +2615,155 @@ NetUserGetGroups(LPCWSTR servername,
                  LPDWORD entriesread,
                  LPDWORD totalentries)
 {
-    FIXME("%s %s %d %p %d %p %p stub\n", debugstr_w(servername),
+    UNICODE_STRING ServerName;
+    UNICODE_STRING UserName;
+    SAM_HANDLE ServerHandle = NULL;
+    SAM_HANDLE AccountDomainHandle = NULL;
+    SAM_HANDLE UserHandle = NULL;
+    PSID AccountDomainSid = NULL;
+    PULONG RelativeIds = NULL;
+    PSID_NAME_USE Use = NULL;
+    PGROUP_MEMBERSHIP GroupMembership = NULL;
+    ULONG GroupCount;
+
+    NET_API_STATUS ApiStatus = NERR_Success;
+    NTSTATUS Status = STATUS_SUCCESS;
+
+    TRACE("%s %s %d %p %d %p %p stub\n", debugstr_w(servername),
           debugstr_w(username), level, bufptr, prefixmaxlen, entriesread,
           totalentries);
 
-    *bufptr = NULL;
-    *entriesread = 0;
-    *totalentries = 0;
+    if (servername != NULL)
+        RtlInitUnicodeString(&ServerName, servername);
+
+    RtlInitUnicodeString(&UserName, username);
+
+    /* Connect to the SAM Server */
+    Status = SamConnect((servername != NULL) ? &ServerName : NULL,
+                        &ServerHandle,
+                        SAM_SERVER_CONNECT | SAM_SERVER_LOOKUP_DOMAIN,
+                        NULL);
+    if (!NT_SUCCESS(Status))
+    {
+        ERR("SamConnect failed (Status %08lx)\n", Status);
+        ApiStatus = NetpNtStatusToApiStatus(Status);
+        goto done;
+    }
+
+    /* Get the Account Domain SID */
+    Status = GetAccountDomainSid((servername != NULL) ? &ServerName : NULL,
+                                 &AccountDomainSid);
+    if (!NT_SUCCESS(Status))
+    {
+        ERR("GetAccountDomainSid failed (Status %08lx)\n", Status);
+        ApiStatus = NetpNtStatusToApiStatus(Status);
+        goto done;
+    }
+
+    /* Open the Account Domain */
+    Status = SamOpenDomain(ServerHandle,
+                           DOMAIN_LOOKUP | DOMAIN_GET_ALIAS_MEMBERSHIP,
+                           AccountDomainSid,
+                           &AccountDomainHandle);
+    if (!NT_SUCCESS(Status))
+    {
+        ERR("OpenAccountDomain failed (Status %08lx)\n", Status);
+        ApiStatus = NetpNtStatusToApiStatus(Status);
+        goto done;
+    }
+
+    /* Get the RID for the given user name */
+    Status = SamLookupNamesInDomain(AccountDomainHandle,
+                                    1,
+                                    &UserName,
+                                    &RelativeIds,
+                                    &Use);
+    if (!NT_SUCCESS(Status))
+    {
+        ERR("SamLookupNamesInDomain failed (Status %08lx)\n", Status);
+        if (Status == STATUS_NONE_MAPPED)
+            ApiStatus = NERR_UserNotFound;
+        else
+            ApiStatus = NetpNtStatusToApiStatus(Status);
+        goto done;
+    }
+
+    /* Fail, if it is not a user account */
+    if (Use[0] != SidTypeUser)
+    {
+        ERR("Account is not a User!\n");
+        ApiStatus = NERR_UserNotFound;
+        goto done;
+    }
+
+    /* Open the user object */
+    Status = SamOpenUser(AccountDomainHandle,
+                         USER_LIST_GROUPS,
+                         RelativeIds[0],
+                         &UserHandle);
+    if (!NT_SUCCESS(Status))
+    {
+        ERR("SamOpenUser failed (Status %08lx)\n", Status);
+        ApiStatus = NetpNtStatusToApiStatus(Status);
+        goto done;
+    }
+
+    /* Get the group memberships of this user */
+    Status = SamGetGroupsForUser(UserHandle,
+                                 &GroupMembership,
+                                 &GroupCount);
+    if (!NT_SUCCESS(Status))
+    {
+        ERR("SamGetGroupsForUser failed (Status %08lx)\n", Status);
+        ApiStatus = NetpNtStatusToApiStatus(Status);
+        goto done;
+    }
 
-    return ERROR_INVALID_LEVEL;
+    /* If there is no group membership, we're done */
+    if (GroupCount == 0)
+    {
+        ApiStatus = NERR_Success;
+        goto done;
+    }
+
+
+done:
+
+    if (GroupMembership != NULL)
+        SamFreeMemory(GroupMembership);
+
+    if (UserHandle != NULL)
+        SamCloseHandle(UserHandle);
+
+    if (RelativeIds != NULL)
+        SamFreeMemory(RelativeIds);
+
+    if (Use != NULL)
+        SamFreeMemory(Use);
+
+    if (AccountDomainSid != NULL)
+        RtlFreeHeap(RtlGetProcessHeap(), 0, AccountDomainSid);
+
+    if (AccountDomainHandle != NULL)
+        SamCloseHandle(AccountDomainHandle);
+
+    if (ServerHandle != NULL)
+        SamCloseHandle(ServerHandle);
+
+    if (ApiStatus != NERR_Success && ApiStatus != ERROR_MORE_DATA)
+    {
+        *entriesread = 0;
+        *totalentries = 0;
+    }
+    else
+    {
+//        *entriesread = Count;
+//        *totalentries = Count;
+    }
+
+//    *bufptr = (LPBYTE)Buffer;
+
+    return ApiStatus;
 }
 
 
@@ -2650,7 +2784,6 @@ NetUserGetInfo(LPCWSTR servername,
     SAM_HANDLE UserHandle = NULL;
     PULONG RelativeIds = NULL;
     PSID_NAME_USE Use = NULL;
-    PUSER_ALL_INFORMATION UserInfo = NULL;
     LPVOID Buffer = NULL;
     NET_API_STATUS ApiStatus = NERR_Success;
     NTSTATUS Status = STATUS_SUCCESS;
@@ -2696,7 +2829,10 @@ NetUserGetInfo(LPCWSTR servername,
     if (!NT_SUCCESS(Status))
     {
         ERR("SamOpenDomain failed (Status %08lx)\n", Status);
-        ApiStatus = NetpNtStatusToApiStatus(Status);
+        if (Status == STATUS_NONE_MAPPED)
+            ApiStatus = NERR_UserNotFound;
+        else
+            ApiStatus = NetpNtStatusToApiStatus(Status);
         goto done;
     }
 
@@ -2712,7 +2848,7 @@ NetUserGetInfo(LPCWSTR servername,
 
     /* Open the user object */
     Status = SamOpenUser(AccountDomainHandle,
-                         USER_READ_GENERAL | USER_READ_PREFERENCES | USER_READ_LOGON | USER_READ_ACCOUNT,
+                         READ_CONTROL | USER_READ_GENERAL | USER_READ_PREFERENCES | USER_READ_LOGON | USER_READ_ACCOUNT,
                          RelativeIds[0],
                          &UserHandle);
     if (!NT_SUCCESS(Status))
@@ -2722,17 +2858,7 @@ NetUserGetInfo(LPCWSTR servername,
         goto done;
     }
 
-    Status = SamQueryInformationUser(UserHandle,
-                                     UserAllInformation,
-                                     (PVOID *)&UserInfo);
-    if (!NT_SUCCESS(Status))
-    {
-        ERR("SamQueryInformationUser failed (Status %08lx)\n", Status);
-        ApiStatus = NetpNtStatusToApiStatus(Status);
-        goto done;
-    }
-
-    ApiStatus = BuildUserInfoBuffer(UserInfo,
+    ApiStatus = BuildUserInfoBuffer(UserHandle,
                                     level,
                                     RelativeIds[0],
                                     &Buffer);
@@ -2743,9 +2869,6 @@ NetUserGetInfo(LPCWSTR servername,
     }
 
 done:
-    if (UserInfo != NULL)
-        FreeUserInfo(UserInfo);
-
     if (UserHandle != NULL)
         SamCloseHandle(UserHandle);
 
@@ -2879,7 +3002,10 @@ NetUserGetLocalGroups(LPCWSTR servername,
     if (!NT_SUCCESS(Status))
     {
         ERR("SamLookupNamesInDomain failed (Status %08lx)\n", Status);
-        ApiStatus = NetpNtStatusToApiStatus(Status);
+        if (Status == STATUS_NONE_MAPPED)
+            ApiStatus = NERR_UserNotFound;
+        else
+            ApiStatus = NetpNtStatusToApiStatus(Status);
         goto done;
     }
 
@@ -2973,7 +3099,7 @@ NetUserGetLocalGroups(LPCWSTR servername,
 
     for (i = 0; i < AccountMemberCount; i++)
     {
-        if (BuiltinNames[i].Length > 0)
+        if (AccountNames[i].Length > 0)
         {
             Size += (sizeof(LOCALGROUP_USERS_INFO_0) + AccountNames[i].Length + sizeof(UNICODE_NULL));
             Count++;
@@ -3291,7 +3417,7 @@ NetUserModalsGet(LPCWSTR servername,
             umi0 = (PUSER_MODALS_INFO_0)*bufptr;
 
             umi0->usrmod0_min_passwd_len = PasswordInfo->MinPasswordLength;
-            umi0->usrmod0_max_passwd_age = (ULONG)(PasswordInfo->MaxPasswordAge.QuadPart / 10000000);
+            umi0->usrmod0_max_passwd_age = (ULONG)(-PasswordInfo->MaxPasswordAge.QuadPart / 10000000);
             umi0->usrmod0_min_passwd_age =
                 DeltaTimeToSeconds(PasswordInfo->MinPasswordAge);
             umi0->usrmod0_force_logoff =
@@ -3304,10 +3430,6 @@ NetUserModalsGet(LPCWSTR servername,
 
             switch (ServerRoleInfo->DomainServerRole)
             {
-
-                    umi1->usrmod1_role = UAS_ROLE_STANDALONE;
-                    umi1->usrmod1_role = UAS_ROLE_MEMBER;
-
                 case DomainServerRolePrimary:
                     umi1->usrmod1_role = UAS_ROLE_PRIMARY;
                     break;
@@ -3451,9 +3573,9 @@ NetUserSetInfo(LPCWSTR servername,
         case 1:
         case 2:
         case 3:
-//        case 4:
+        case 4:
 //        case 21:
-//        case 22:
+        case 22:
         case 1003:
 //        case 1005:
         case 1006:
@@ -3465,8 +3587,8 @@ NetUserSetInfo(LPCWSTR servername,
         case 1012:
         case 1013:
         case 1014:
-//        case 1017:
-//        case 1018:
+        case 1017:
+        case 1018:
 //        case 1020:
         case 1024:
         case 1025:
@@ -3499,7 +3621,7 @@ NetUserSetInfo(LPCWSTR servername,
     /* Open the Account Domain */
     Status = OpenAccountDomain(ServerHandle,
                                (servername != NULL) ? &ServerName : NULL,
-                               DOMAIN_LIST_ACCOUNTS | DOMAIN_LOOKUP,
+                               DOMAIN_LIST_ACCOUNTS | DOMAIN_LOOKUP | DOMAIN_READ_PASSWORD_PARAMETERS,
                                &AccountDomainHandle);
     if (!NT_SUCCESS(Status))
     {