[USERENV]
[reactos.git] / reactos / dll / win32 / userenv / environment.c
index 918bce4..baf2a80 100644 (file)
@@ -175,8 +175,8 @@ GetCurrentUserKey(HANDLE hToken)
     HKEY hKey;
     LONG Error;
 
-    if (!GetUserSidFromToken(hToken,
-                             &SidString))
+    if (!GetUserSidStringFromToken(hToken,
+                                   &SidString))
     {
         DPRINT1("GetUserSidFromToken() failed\n");
         return NULL;
@@ -201,6 +201,89 @@ GetCurrentUserKey(HANDLE hToken)
 }
 
 
+static
+BOOL
+GetUserAndDomainName(IN HANDLE hToken,
+                     OUT LPWSTR *UserName,
+                     OUT LPWSTR *DomainName)
+{
+    PSID Sid = NULL;
+    LPWSTR lpUserName = NULL;
+    LPWSTR lpDomainName = NULL;
+    DWORD cbUserName = 0;
+    DWORD cbDomainName = 0;
+    SID_NAME_USE SidNameUse;
+    BOOL bRet = TRUE;
+
+    if (!GetUserSidFromToken(hToken,
+                             &Sid))
+    {
+        DPRINT1("GetUserSidFromToken() failed\n");
+        return FALSE;
+    }
+
+    if (!LookupAccountSidW(NULL,
+                           Sid,
+                           NULL,
+                           &cbUserName,
+                           NULL,
+                           &cbDomainName,
+                           &SidNameUse))
+    {
+        if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
+        {
+            bRet = FALSE;
+            goto done;
+        }
+    }
+
+    lpUserName = LocalAlloc(LPTR,
+                            cbUserName * sizeof(WCHAR));
+    if (lpUserName == NULL)
+    {
+        bRet = FALSE;
+        goto done;
+    }
+
+    lpDomainName = LocalAlloc(LPTR,
+                              cbDomainName * sizeof(WCHAR));
+    if (lpDomainName == NULL)
+    {
+        bRet = FALSE;
+        goto done;
+    }
+
+    if (!LookupAccountSidW(NULL,
+                           Sid,
+                           lpUserName,
+                           &cbUserName,
+                           lpDomainName,
+                           &cbDomainName,
+                           &SidNameUse))
+    {
+        bRet = FALSE;
+        goto done;
+    }
+
+    *UserName = lpUserName;
+    *DomainName = lpDomainName;
+
+done:
+    if (bRet == FALSE)
+    {
+        if (lpUserName != NULL)
+            LocalFree(lpUserName);
+
+        if (lpDomainName != NULL)
+            LocalFree(lpDomainName);
+    }
+
+    LocalFree(Sid);
+
+    return bRet;
+}
+
+
 static
 BOOL
 SetUserEnvironment(LPVOID *lpEnvironment,
@@ -326,6 +409,8 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment,
     DWORD dwType;
     HKEY hKey;
     HKEY hKeyUser;
+    LPWSTR lpUserName = NULL;
+    LPWSTR lpDomainName = NULL;
     NTSTATUS Status;
     LONG lError;
 
@@ -431,15 +516,20 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment,
                                    FALSE);
     }
 
-    /* FIXME: Set 'USERDOMAIN' variable */
-
-    Length = MAX_PATH;
-    if (GetUserNameW(Buffer,
-                     &Length))
+    if (GetUserAndDomainName(hToken,
+                             &lpUserName,
+                             &lpDomainName))
     {
+        /* Set 'USERDOMAIN' variable */
+        SetUserEnvironmentVariable(lpEnvironment,
+                                   L"USERDOMAIN",
+                                   lpDomainName,
+                                   FALSE);
+
+        /* Set 'USERNAME' variable */
         SetUserEnvironmentVariable(lpEnvironment,
                                    L"USERNAME",
-                                   Buffer,
+                                   lpUserName,
                                    FALSE);
     }
 
@@ -455,6 +545,12 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment,
 
     RegCloseKey(hKeyUser);
 
+    if (lpUserName != NULL)
+        LocalFree(lpUserName);
+
+    if (lpDomainName != NULL)
+        LocalFree(lpDomainName);
+
     return TRUE;
 }