[USERENV] LoadUserProfileW: If a user profile has already been loaded, just open...
authorEric Kohl <eric.kohl@reactos.org>
Thu, 14 Mar 2019 22:47:34 +0000 (23:47 +0100)
committerEric Kohl <eric.kohl@reactos.org>
Thu, 14 Mar 2019 22:49:16 +0000 (23:49 +0100)
dll/win32/userenv/profile.c

index 40d4b32..c250d88 100644 (file)
@@ -1436,100 +1436,102 @@ LoadUserProfileW(
 
     DPRINT("UserName: %S\n", lpProfileInfo->lpUserName);
 
-    /* Don't load a profile twice */
-    if (CheckForLoadedProfile(hToken))
+    /* Get the user SID string */
+    ret = GetUserSidStringFromToken(hToken, &SidString);
+    if (!ret)
     {
-        DPRINT ("Profile already loaded\n");
-        lpProfileInfo->hProfile = NULL;
-        return TRUE;
+        DPRINT1("GetUserSidStringFromToken() failed\n");
+        goto cleanup;
     }
+    ret = FALSE;
 
-    if (lpProfileInfo->lpProfilePath)
+    /* Don't load a profile twice */
+    if (CheckForLoadedProfile(hToken))
     {
-        /* Use the caller's specified roaming user profile path */
-        StringCbCopyW(szUserHivePath, sizeof(szUserHivePath), lpProfileInfo->lpProfilePath);
+        DPRINT1("Profile %S already loaded\n", SidString.Buffer);
     }
     else
     {
-        /* FIXME: check if MS Windows allows lpProfileInfo->lpProfilePath to be NULL */
-        if (!GetProfilesDirectoryW(szUserHivePath, &dwLength))
-        {
-            DPRINT1("GetProfilesDirectoryW() failed (error %ld)\n", GetLastError());
-            return FALSE;
-        }
-    }
+        DPRINT1("Loading profile %S\n", SidString.Buffer);
 
-    /* Create user hive name */
-    StringCbCatW(szUserHivePath, sizeof(szUserHivePath), L"\\");
-    StringCbCatW(szUserHivePath, sizeof(szUserHivePath), lpProfileInfo->lpUserName);
-    StringCbCatW(szUserHivePath, sizeof(szUserHivePath), L"\\ntuser.dat");
-    DPRINT("szUserHivePath: %S\n", szUserHivePath);
-
-    /* Create user profile directory if needed */
-    if (GetFileAttributesW(szUserHivePath) == INVALID_FILE_ATTRIBUTES)
-    {
-        /* Get user sid */
-        if (GetTokenInformation(hToken, TokenUser, NULL, 0, &dwLength) ||
-            GetLastError() != ERROR_INSUFFICIENT_BUFFER)
+        if (lpProfileInfo->lpProfilePath)
         {
-            DPRINT1 ("GetTokenInformation() failed\n");
-            return FALSE;
+            /* Use the caller's specified roaming user profile path */
+            StringCbCopyW(szUserHivePath, sizeof(szUserHivePath), lpProfileInfo->lpProfilePath);
         }
-
-        UserSid = (PTOKEN_USER)HeapAlloc(GetProcessHeap(), 0, dwLength);
-        if (!UserSid)
+        else
         {
-            DPRINT1("HeapAlloc() failed\n");
-            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-            goto cleanup;
+            /* FIXME: check if MS Windows allows lpProfileInfo->lpProfilePath to be NULL */
+            if (!GetProfilesDirectoryW(szUserHivePath, &dwLength))
+            {
+                DPRINT1("GetProfilesDirectoryW() failed (error %ld)\n", GetLastError());
+                goto cleanup;
+            }
         }
 
-        if (!GetTokenInformation(hToken, TokenUser, UserSid, dwLength, &dwLength))
+        /* Create user hive name */
+        StringCbCatW(szUserHivePath, sizeof(szUserHivePath), L"\\");
+        StringCbCatW(szUserHivePath, sizeof(szUserHivePath), lpProfileInfo->lpUserName);
+        StringCbCatW(szUserHivePath, sizeof(szUserHivePath), L"\\ntuser.dat");
+        DPRINT("szUserHivePath: %S\n", szUserHivePath);
+
+        /* Create user profile directory if needed */
+        if (GetFileAttributesW(szUserHivePath) == INVALID_FILE_ATTRIBUTES)
         {
-            DPRINT1("GetTokenInformation() failed\n");
-            goto cleanup;
+            /* Get user sid */
+            if (GetTokenInformation(hToken, TokenUser, NULL, 0, &dwLength) ||
+                GetLastError() != ERROR_INSUFFICIENT_BUFFER)
+            {
+                DPRINT1 ("GetTokenInformation() failed\n");
+                goto cleanup;
+            }
+
+            UserSid = (PTOKEN_USER)HeapAlloc(GetProcessHeap(), 0, dwLength);
+            if (!UserSid)
+            {
+                DPRINT1("HeapAlloc() failed\n");
+                SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                goto cleanup;
+            }
+
+            if (!GetTokenInformation(hToken, TokenUser, UserSid, dwLength, &dwLength))
+            {
+                DPRINT1("GetTokenInformation() failed\n");
+                goto cleanup;
+            }
+
+            /* Create profile */
+            ret = CreateUserProfileW(UserSid->User.Sid, lpProfileInfo->lpUserName);
+            if (!ret)
+            {
+                DPRINT1("CreateUserProfileW() failed\n");
+                goto cleanup;
+            }
         }
 
-        /* Create profile */
-        ret = CreateUserProfileW(UserSid->User.Sid, lpProfileInfo->lpUserName);
-        if (!ret)
+        /* Acquire restore privilege */
+        if (!AcquireRemoveRestorePrivilege(TRUE))
         {
-            DPRINT1("CreateUserProfileW() failed\n");
+            DPRINT1("AcquireRemoveRestorePrivilege() failed (Error %ld)\n", GetLastError());
             goto cleanup;
         }
-    }
-
-    /* Get the user SID string */
-    ret = GetUserSidStringFromToken(hToken, &SidString);
-    if (!ret)
-    {
-        DPRINT1("GetUserSidStringFromToken() failed\n");
-        goto cleanup;
-    }
-    ret = FALSE;
 
-    /* Acquire restore privilege */
-    if (!AcquireRemoveRestorePrivilege(TRUE))
-    {
-        DPRINT1("AcquireRemoveRestorePrivilege() failed (Error %ld)\n", GetLastError());
-        goto cleanup;
-    }
-
-    /* Load user registry hive */
-    Error = RegLoadKeyW(HKEY_USERS,
-                        SidString.Buffer,
-                        szUserHivePath);
-    AcquireRemoveRestorePrivilege(FALSE);
+        /* Load user registry hive */
+        Error = RegLoadKeyW(HKEY_USERS,
+                            SidString.Buffer,
+                            szUserHivePath);
+        AcquireRemoveRestorePrivilege(FALSE);
 
-    /* HACK: Do not fail if the profile has already been loaded! */
-    if (Error == ERROR_SHARING_VIOLATION)
-        Error = ERROR_SUCCESS;
+        /* HACK: Do not fail if the profile has already been loaded! */
+        if (Error == ERROR_SHARING_VIOLATION)
+            Error = ERROR_SUCCESS;
 
-    if (Error != ERROR_SUCCESS)
-    {
-        DPRINT1("RegLoadKeyW() failed (Error %ld)\n", Error);
-        SetLastError((DWORD)Error);
-        goto cleanup;
+        if (Error != ERROR_SUCCESS)
+        {
+            DPRINT1("RegLoadKeyW() failed (Error %ld)\n", Error);
+            SetLastError((DWORD)Error);
+            goto cleanup;
+        }
     }
 
     /* Open future HKEY_CURRENT_USER */
@@ -1548,7 +1550,8 @@ LoadUserProfileW(
     ret = TRUE;
 
 cleanup:
-    HeapFree(GetProcessHeap(), 0, UserSid);
+    if (UserSid != NULL)
+        HeapFree(GetProcessHeap(), 0, UserSid);
     RtlFreeUnicodeString(&SidString);
 
     DPRINT("LoadUserProfileW() done\n");