[USERENV] Implement CopySystemProfile Part 1 (Add a new profile subkey and values...
authorEric Kohl <eric.kohl@reactos.org>
Sun, 27 May 2018 13:29:31 +0000 (15:29 +0200)
committerEric Kohl <eric.kohl@reactos.org>
Sun, 27 May 2018 13:31:43 +0000 (15:31 +0200)
CORE-12541

dll/win32/userenv/profile.c
dll/win32/userenv/userenv.spec
sdk/include/psdk/userenv.h

index 975a71f..8acbb90 100644 (file)
@@ -106,6 +106,111 @@ AcquireRemoveRestorePrivilege(IN BOOL bAcquire)
 }
 
 
+BOOL
+WINAPI
+CopySystemProfile(
+    IN ULONG Unused)
+{
+    WCHAR szKeyName[MAX_PATH];
+    WCHAR szImagePath[MAX_PATH];
+    UNICODE_STRING SidString = {0, 0, NULL};
+    HANDLE hToken = NULL;
+    PSID pUserSid = NULL;
+    HKEY hProfileKey = NULL;
+    DWORD dwDisposition;
+    BOOL bResult = FALSE;
+    DWORD dwError;
+
+    DPRINT1("CopySystemProfile()\n");
+
+    if (!OpenProcessToken(GetCurrentProcess(),
+                          TOKEN_QUERY | TOKEN_IMPERSONATE,
+                          &hToken))
+    {
+        DPRINT1("Failed to open the process token (Error %lu)\n", GetLastError());
+        return FALSE;
+    }
+
+    pUserSid = GetUserSid(hToken);
+    if (pUserSid == NULL)
+    {
+        DPRINT1("Failed to get the users SID (Error %lu)\n", GetLastError());
+        goto done;
+    }
+
+    /* Get the user SID string */
+    if (!GetUserSidStringFromToken(hToken, &SidString))
+    {
+        DPRINT1("GetUserSidStringFromToken() failed\n");
+        goto done;
+    }
+
+    StringCbCopyW(szKeyName, sizeof(szKeyName),
+                  L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
+    StringCbCatW(szKeyName, sizeof(szKeyName), SidString.Buffer);
+
+    RtlFreeUnicodeString(&SidString);
+
+    dwError = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
+                              szKeyName,
+                              0, NULL, 0,
+                              KEY_WRITE,
+                              NULL,
+                              &hProfileKey,
+                              &dwDisposition);
+    if (dwError != ERROR_SUCCESS)
+    {
+        DPRINT1("Failed to create the profile key for the %s profile (Error %lu)\n",
+                SidString.Buffer, dwError);
+        goto done;
+    }
+
+    dwError = RegSetValueExW(hProfileKey,
+                             L"Sid",
+                             0,
+                             REG_BINARY,
+                             (PBYTE)pUserSid,
+                             RtlLengthSid(pUserSid));
+    if (dwError != ERROR_SUCCESS)
+    {
+        DPRINT1("Failed to set the SID value (Error %lu)\n", dwError);
+        goto done;
+    }
+
+    wcscpy(szImagePath,
+           L"%systemroot%\\system32\\config\\systemprofile");
+
+    dwError = RegSetValueExW(hProfileKey,
+                             L"ProfileImagePath",
+                             0,
+                             REG_EXPAND_SZ,
+                             (PBYTE)szImagePath,
+                             (wcslen(szImagePath) + 1) * sizeof(WCHAR));
+    if (dwError != ERROR_SUCCESS)
+    {
+        DPRINT1("Failed to set the ProfileImagePath value (Error %lu)\n", dwError);
+        goto done;
+    }
+
+
+    bResult = TRUE;
+
+done:
+    if (hProfileKey != NULL)
+        RegCloseKey(hProfileKey);
+
+    RtlFreeUnicodeString(&SidString);
+
+    if (pUserSid != NULL)
+        LocalFree(pUserSid);
+
+    if (hToken != NULL)
+        CloseHandle(hToken);
+
+    return bResult;
+}
+
+
 BOOL
 WINAPI
 CreateUserProfileA(
@@ -1030,7 +1135,7 @@ GetUserProfileDirectoryW(
                                    szImagePath,
                                    ARRAYSIZE(szImagePath)))
     {
-        DPRINT1 ("Error: %lu\n", GetLastError());
+        DPRINT1("Error: %lu\n", GetLastError());
         return FALSE;
     }
 
index 0de19c7..b8826bc 100644 (file)
@@ -98,7 +98,7 @@
 152 stub -noname IsSyncForegroundPolicyRefresh                        #stdcall -noname IsSyncForegroundPolicyRefresh(8)
 153 stdcall -noname CreateUserProfileExA(ptr str str str long long)
 154 stdcall -noname CreateUserProfileExW(ptr wstr wstr wstr long long)
-155 stub -noname CopySystemProfile                                    #stdcall -noname CopySystemProfile(4)
+155 stdcall -noname CopySystemProfile(long)
 156 stub -noname GetUserProfileDirFromSidA                            #stdcall -noname GetUserProfileDirFromSidA(12)
 157 stub -noname GetUserProfileDirFromSidW                            #stdcall -noname GetUserProfileDirFromSidW(12)
 158 stub -noname SetNextFgPolicyRefreshInfo                           #stdcall -noname SetNextFgPolicyRefreshInfo(12)
index 836de5c..3b5054e 100644 (file)
@@ -106,6 +106,7 @@ BOOL WINAPI DeleteItemW (LPCWSTR, BOOL, LPCWSTR, BOOL);
 BOOL WINAPI CopyProfileDirectoryA(LPCSTR, LPCSTR, DWORD);
 BOOL WINAPI CopyProfileDirectoryW(LPCWSTR, LPCWSTR, DWORD);
 PSID WINAPI GetUserSid(HANDLE);
+BOOL WINAPI CopySystemProfile(ULONG);
 /* end private */
 BOOL WINAPI LoadUserProfileA (HANDLE, LPPROFILEINFOA);
 BOOL WINAPI LoadUserProfileW (HANDLE, LPPROFILEINFOW);