[WINLOGON]
[reactos.git] / reactos / base / system / winlogon / environment.c
index 4d349fe..972d09c 100644 (file)
@@ -11,6 +11,7 @@
 /* INCLUDES *****************************************************************/
 
 #include "winlogon.h"
+#include <shlobj.h>
 
 #include <wine/debug.h>
 
@@ -21,72 +22,122 @@ WINE_DEFAULT_DEBUG_CHANNEL(winlogon);
 
 /* FUNCTIONS ****************************************************************/
 
-BOOL
-CreateUserEnvironment(IN PWLSESSION Session,
-                      IN LPVOID *lpEnvironment,
-                      IN LPWSTR *lpFullEnv)
+static VOID
+BuildVolatileEnvironment(IN PWLSESSION Session,
+                         IN HKEY hKeyCurrentUser)
 {
+    WCHAR szPath[MAX_PATH + 1];
     LPCWSTR wstr;
-    SIZE_T EnvBlockSize = 0, ProfileSize = 0;
-    LPVOID lpEnviron = NULL;
-    LPWSTR lpFullEnviron = NULL;
-    HKEY hKey;
+    SIZE_T size;
+    WCHAR szEnvKey[MAX_PATH];
+    WCHAR szEnvValue[1024];
+    SIZE_T length;
+    LPWSTR eqptr, endptr;
     DWORD dwDisp;
     LONG lError;
-    HKEY hKeyCurrentUser;
-
-    TRACE("WL: CreateUserEnvironment called\n");
-
-    /* Create environment block for the user */
-    if (!CreateEnvironmentBlock(&lpEnviron,
-                                Session->UserToken,
-                                TRUE))
+    HKEY hKeyVolatileEnv;
+    HKEY hKeyShellFolders;
+    DWORD dwType;
+    DWORD dwSize;
+
+    /* Create the 'Volatile Environment' key */
+    lError = RegCreateKeyExW(hKeyCurrentUser,
+                             L"Volatile Environment",
+                             0,
+                             NULL,
+                             REG_OPTION_VOLATILE,
+                             KEY_WRITE,
+                             NULL,
+                             &hKeyVolatileEnv,
+                             &dwDisp);
+    if (lError != ERROR_SUCCESS)
     {
-        WARN("WL: CreateEnvironmentBlock() failed\n");
-        return FALSE;
+        WARN("WL: RegCreateKeyExW() failed to create the volatile environment key (Error: %ld)\n", lError);
+        return;
     }
 
-    if (Session->Profile->dwType == WLX_PROFILE_TYPE_V2_0 && Session->Profile->pszEnvironment)
+    /* Parse the environment variables and add them to the volatile environment key */
+    if (Session->Profile->dwType == WLX_PROFILE_TYPE_V2_0 &&
+        Session->Profile->pszEnvironment != NULL)
     {
-        /* Count required size for full environment */
-        wstr = (LPCWSTR)lpEnviron;
-        while (*wstr != UNICODE_NULL)
-        {
-            SIZE_T size = wcslen(wstr) + 1;
-            wstr += size;
-            EnvBlockSize += size;
-        }
-
         wstr = Session->Profile->pszEnvironment;
         while (*wstr != UNICODE_NULL)
         {
-            SIZE_T size = wcslen(wstr) + 1;
+            size = wcslen(wstr) + 1;
+
+            eqptr = wcschr(wstr, L'=');
+
+            if (eqptr != NULL)
+            {
+                endptr = eqptr;
+
+                endptr--;
+                while (iswspace(*endptr))
+                    endptr--;
+
+                length = (SIZE_T)(endptr - wstr + 1);
+
+                wcsncpy(szEnvKey, wstr, length);
+                szEnvKey[length] = 0;
+
+                eqptr++;
+                while (iswspace(*eqptr))
+                    eqptr++;
+                wcscpy(szEnvValue, eqptr);
+
+                RegSetValueExW(hKeyVolatileEnv,
+                               szEnvKey,
+                               0,
+                               REG_SZ,
+                               (LPBYTE)szEnvValue,
+                               (wcslen(szEnvValue) + 1) * sizeof(WCHAR));
+            }
+
             wstr += size;
-            ProfileSize += size;
         }
+    }
 
-        /* Allocate enough memory */
-        lpFullEnviron = HeapAlloc(GetProcessHeap(), 0, (EnvBlockSize + ProfileSize + 1) * sizeof(WCHAR));
-        if (!lpFullEnviron)
+    /* Set the 'APPDATA' environment variable */
+    lError = RegOpenKeyExW(hKeyCurrentUser,
+                           L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
+                           0,
+                           KEY_READ,
+                           &hKeyShellFolders);
+    if (lError == ERROR_SUCCESS)
+    {
+        dwSize = (MAX_PATH + 1) * sizeof(WCHAR);
+        lError = RegQueryValueExW(hKeyShellFolders,
+                                  L"AppData",
+                                  NULL,
+                                  &dwType,
+                                  (LPBYTE)szPath,
+                                  &dwSize);
+        if (lError == ERROR_SUCCESS)
         {
-            TRACE("HeapAlloc() failed\n");
-            return FALSE;
+            TRACE("APPDATA path: %S\n", szPath);
+            RegSetValueExW(hKeyVolatileEnv,
+                           L"APPDATA",
+                           0,
+                           REG_SZ,
+                           (LPBYTE)szPath,
+                           (wcslen(szPath) + 1) * sizeof(WCHAR));
         }
 
-        /* Fill user environment block */
-        CopyMemory(lpFullEnviron,
-                   lpEnviron,
-                   EnvBlockSize * sizeof(WCHAR));
-        CopyMemory(&lpFullEnviron[EnvBlockSize],
-                   Session->Profile->pszEnvironment,
-                   ProfileSize * sizeof(WCHAR));
-        lpFullEnviron[EnvBlockSize + ProfileSize] = UNICODE_NULL;
-    }
-    else
-    {
-        lpFullEnviron = (LPWSTR)lpEnviron;
+        RegCloseKey(hKeyShellFolders);
     }
 
+    RegCloseKey(hKeyVolatileEnv);
+}
+
+
+BOOL
+CreateUserEnvironment(IN PWLSESSION Session)
+{
+    HKEY hKeyCurrentUser;
+    LONG lError;
+
+    TRACE("WL: CreateUserEnvironment called\n");
+
     /* Impersonate the new user */
     ImpersonateLoggedOnUser(Session->UserToken);
 
@@ -95,34 +146,14 @@ CreateUserEnvironment(IN PWLSESSION Session,
                                 &hKeyCurrentUser);
     if (lError == ERROR_SUCCESS)
     {
-        /* Create the 'Volatile Environment' key */
-        lError = RegCreateKeyExW(hKeyCurrentUser,
-                                 L"Volatile Environment",
-                                 0,
-                                 NULL,
-                                 REG_OPTION_VOLATILE,
-                                 KEY_WRITE,
-                                 NULL,
-                                 &hKey,
-                                 &dwDisp);
-        if (lError == ERROR_SUCCESS)
-        {
-            RegCloseKey(hKey);
-        }
-        else
-        {
-            WARN("WL: RegCreateKeyExW() failed (Error: %ld)\n", lError);
-        }
-
+        BuildVolatileEnvironment(Session,
+                                 hKeyCurrentUser);
         RegCloseKey(hKeyCurrentUser);
     }
 
     /* Revert the impersonation */
     RevertToSelf();
 
-    *lpEnvironment = lpEnviron;
-    *lpFullEnv = lpFullEnviron;
-
     TRACE("WL: CreateUserEnvironment done\n");
 
     return TRUE;