From 3211fcf2d035574c0755db54bd37071439d46565 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Sat, 13 Mar 2004 20:49:07 +0000 Subject: [PATCH] Implement LoadUserProfileW() and UnloadUserProfile(). svn path=/trunk/; revision=8693 --- reactos/lib/userenv/internal.h | 14 ++-- reactos/lib/userenv/misc.c | 61 ++++++++++++++++- reactos/lib/userenv/profile.c | 115 +++++++++++++++++++++++++++++++- reactos/lib/userenv/setup.c | 3 +- reactos/lib/userenv/userenv.c | 3 +- reactos/lib/userenv/userenv.def | 1 + reactos/lib/userenv/userenv.edf | 1 + 7 files changed, 188 insertions(+), 10 deletions(-) diff --git a/reactos/lib/userenv/internal.h b/reactos/lib/userenv/internal.h index fe53f8990d2..4458b5fb8b6 100644 --- a/reactos/lib/userenv/internal.h +++ b/reactos/lib/userenv/internal.h @@ -1,4 +1,4 @@ -/* $Id: internal.h,v 1.4 2004/01/16 15:31:53 ekohl Exp $ +/* $Id: internal.h,v 1.5 2004/03/13 20:49:07 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -24,21 +24,25 @@ DebugPrint (char* fmt,...); #endif /* __GNUC__ */ #define CHECKPOINT -/* directory.h */ +/* directory.c */ BOOL CopyDirectory (LPCWSTR lpDestinationPath, LPCWSTR lpSourcePath); -/* misc.h */ +/* misc.c */ LPWSTR AppendBackslash (LPWSTR String); +BOOL +GetUserSidFromToken (HANDLE hToken, + PUNICODE_STRING SidString); + +/* profile.c */ BOOL AppendSystemPostfix (LPWSTR lpName, DWORD dwMaxLength); - -/* registry.h */ +/* registry.c */ BOOL CreateUserHive (LPCWSTR lpKeyName); diff --git a/reactos/lib/userenv/misc.c b/reactos/lib/userenv/misc.c index ca0861ea1ac..4548eaa98ff 100644 --- a/reactos/lib/userenv/misc.c +++ b/reactos/lib/userenv/misc.c @@ -1,4 +1,4 @@ -/* $Id: misc.c,v 1.1 2004/01/16 15:31:53 ekohl Exp $ +/* $Id: misc.c,v 1.2 2004/03/13 20:49:07 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -32,4 +32,63 @@ AppendBackslash (LPWSTR String) return &String[Length]; } + +BOOL +GetUserSidFromToken (HANDLE hToken, + PUNICODE_STRING SidString) +{ + PSID_AND_ATTRIBUTES SidBuffer; + ULONG Length; + NTSTATUS Status; + + Length = 256; + SidBuffer = LocalAlloc (0, + Length); + if (SidBuffer == NULL) + return FALSE; + + Status = NtQueryInformationToken (hToken, + TokenUser, + (PVOID)SidBuffer, + Length, + &Length); + if (Status == STATUS_BUFFER_TOO_SMALL) + { + SidBuffer = LocalReAlloc (SidBuffer, + Length, + 0); + if (SidBuffer == NULL) + return FALSE; + + Status = NtQueryInformationToken (hToken, + TokenUser, + (PVOID)SidBuffer, + Length, + &Length); + } + + if (!NT_SUCCESS (Status)) + { + LocalFree (SidBuffer); + return FALSE; + } + + DPRINT ("SidLength: %lu\n", RtlLengthSid (SidBuffer[0].Sid)); + + Status = RtlConvertSidToUnicodeString (SidString, + SidBuffer[0].Sid, + TRUE); + + LocalFree (SidBuffer); + + if (!NT_SUCCESS (Status)) + return FALSE; + + DPRINT ("SidString.Length: %lu\n", SidString->Length); + DPRINT ("SidString.MaximumLength: %lu\n", SidString->MaximumLength); + DPRINT ("SidString: '%wZ'\n", SidString); + + return TRUE; +} + /* EOF */ diff --git a/reactos/lib/userenv/profile.c b/reactos/lib/userenv/profile.c index d7d927a5a4c..564dc9fbdd3 100644 --- a/reactos/lib/userenv/profile.c +++ b/reactos/lib/userenv/profile.c @@ -1,4 +1,4 @@ -/* $Id: profile.c,v 1.5 2004/02/28 11:30:59 ekohl Exp $ +/* $Id: profile.c,v 1.6 2004/03/13 20:49:07 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -515,6 +515,14 @@ BOOL WINAPI LoadUserProfileW (HANDLE hToken, LPPROFILEINFOW lpProfileInfo) { + WCHAR szRawProfilesPath[MAX_PATH]; + WCHAR szProfilesPath[MAX_PATH]; + WCHAR szUserHivePath[MAX_PATH]; + UNICODE_STRING SidString; + DWORD dwLength; + HKEY hKey; + + DPRINT ("LoadUserProfileW() called\n"); /* Check profile info */ if (lpProfileInfo->dwSize != sizeof(PROFILEINFOW) || @@ -525,7 +533,110 @@ LoadUserProfileW (HANDLE hToken, return FALSE; } - /* FIXME: load the profile */ + if (RegOpenKeyExW (HKEY_LOCAL_MACHINE, + L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList", + 0, + KEY_ALL_ACCESS, + &hKey)) + { + DPRINT1("Error: %lu\n", GetLastError()); + return FALSE; + } + + /* Get profiles path */ + dwLength = MAX_PATH * sizeof(WCHAR); + if (RegQueryValueExW (hKey, + L"ProfilesDirectory", + NULL, + NULL, + (LPBYTE)szRawProfilesPath, + &dwLength)) + { + DPRINT1("Error: %lu\n", GetLastError()); + RegCloseKey (hKey); + return FALSE; + } + + /* Expand it */ + if (!ExpandEnvironmentStringsW (szRawProfilesPath, + szProfilesPath, + MAX_PATH)) + { + DPRINT1("Error: %lu\n", GetLastError()); + RegCloseKey (hKey); + return FALSE; + } + + RegCloseKey (hKey); + + wcscpy (szUserHivePath, szProfilesPath); + wcscat (szUserHivePath, L"\\"); + wcscat (szUserHivePath, lpProfileInfo->lpUserName); + if (!AppendSystemPostfix (szUserHivePath, MAX_PATH)) + { + DPRINT1("AppendSystemPostfix() failed\n", GetLastError()); + return FALSE; + } + + /* Create user hive name */ + wcscat (szUserHivePath, L"\\ntuser.dat"); + + DPRINT ("szUserHivePath: %S\n", szUserHivePath); + + if (!GetUserSidFromToken (hToken, + &SidString)) + { + DPRINT1 ("GetUserSidFromToken() failed\n"); + return FALSE; + } + + DPRINT ("SidString: '%wZ'\n", &SidString); + + if (RegLoadKeyW (HKEY_USERS, + SidString.Buffer, + szUserHivePath)) + { + DPRINT1 ("RegLoadKeyW() failed (Error %ld)\n", GetLastError()); + RtlFreeUnicodeString (&SidString); + return FALSE; + } + + RtlFreeUnicodeString (&SidString); + + DPRINT ("LoadUserProfileW() done\n"); + + return TRUE; +} + + +BOOL WINAPI +UnloadUserProfile (HANDLE hToken, + HANDLE hProfile) +{ + UNICODE_STRING SidString; + + DPRINT ("UnloadUserProfile() called\n"); + + if (!GetUserSidFromToken (hToken, + &SidString)) + { + DPRINT1 ("GetUserSidFromToken() failed\n"); + return FALSE; + } + + DPRINT ("SidString: '%wZ'\n", &SidString); + + if (RegUnLoadKeyW (HKEY_USERS, + SidString.Buffer)) + { + DPRINT1 ("RegUnLoadKeyW() failed (Error %ld)\n", GetLastError()); + RtlFreeUnicodeString (&SidString); + return FALSE; + } + + RtlFreeUnicodeString (&SidString); + + DPRINT ("UnloadUserProfile() done\n"); return TRUE; } diff --git a/reactos/lib/userenv/setup.c b/reactos/lib/userenv/setup.c index bd72482574f..835a8945124 100644 --- a/reactos/lib/userenv/setup.c +++ b/reactos/lib/userenv/setup.c @@ -1,4 +1,4 @@ -/* $Id: setup.c,v 1.3 2004/01/16 15:31:53 ekohl Exp $ +/* $Id: setup.c,v 1.4 2004/03/13 20:49:07 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -7,6 +7,7 @@ * PROGRAMMER: Eric Kohl */ +#include #include #include #include diff --git a/reactos/lib/userenv/userenv.c b/reactos/lib/userenv/userenv.c index 2b9993df49c..b2acad5143a 100644 --- a/reactos/lib/userenv/userenv.c +++ b/reactos/lib/userenv/userenv.c @@ -1,4 +1,4 @@ -/* $Id: userenv.c,v 1.1 2004/01/09 19:52:01 ekohl Exp $ +/* $Id: userenv.c,v 1.2 2004/03/13 20:49:07 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -7,6 +7,7 @@ * PROGRAMMER: Eric Kohl */ +#include #include #include diff --git a/reactos/lib/userenv/userenv.def b/reactos/lib/userenv/userenv.def index 7a44c804dea..3be07e7d780 100644 --- a/reactos/lib/userenv/userenv.def +++ b/reactos/lib/userenv/userenv.def @@ -7,4 +7,5 @@ GetProfilesDirectoryW@8 GetUserProfileDirectoryW@12 CreateUserProfileW@8 LoadUserProfileW@8 +UnloadUserProfile@8 ;EOF diff --git a/reactos/lib/userenv/userenv.edf b/reactos/lib/userenv/userenv.edf index 806a4469c11..e79f6d00eb1 100644 --- a/reactos/lib/userenv/userenv.edf +++ b/reactos/lib/userenv/userenv.edf @@ -7,4 +7,5 @@ GetProfilesDirectoryW=GetProfilesDirectoryW@8 GetUserProfileDirectoryW=GetUserProfileDirectoryW@12 CreateUserProfileW=CreateUserProfileW@8 LoadUserProfileW=LoadUserProfileW@8 +UnloadUserProfile=UnloadUserProfile@8 ;EOF