Author: ekohl
Date: Sat Nov 2 14:45:26 2013
New Revision: 60834
URL:
http://svn.reactos.org/svn/reactos?rev=60834&view=rev
Log:
[USERENV]
Get the user name and domain name from the logon token and set the USERNAME and USERDOMAIN
environment variables accordingly.
Modified:
trunk/reactos/dll/win32/userenv/environment.c
trunk/reactos/dll/win32/userenv/internal.h
trunk/reactos/dll/win32/userenv/misc.c
trunk/reactos/dll/win32/userenv/profile.c
Modified: trunk/reactos/dll/win32/userenv/environment.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/userenv/environm…
==============================================================================
--- trunk/reactos/dll/win32/userenv/environment.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/userenv/environment.c [iso-8859-1] Sat Nov 2 14:45:26 2013
@@ -175,8 +175,8 @@
HKEY hKey;
LONG Error;
- if (!GetUserSidFromToken(hToken,
- &SidString))
+ if (!GetUserSidStringFromToken(hToken,
+ &SidString))
{
DPRINT1("GetUserSidFromToken() failed\n");
return NULL;
@@ -198,6 +198,89 @@
RtlFreeUnicodeString(&SidString);
return hKey;
+}
+
+
+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;
}
@@ -326,6 +409,8 @@
DWORD dwType;
HKEY hKey;
HKEY hKeyUser;
+ LPWSTR lpUserName = NULL;
+ LPWSTR lpDomainName = NULL;
NTSTATUS Status;
LONG lError;
@@ -431,15 +516,20 @@
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);
}
@@ -454,6 +544,12 @@
L"Volatile Environment");
RegCloseKey(hKeyUser);
+
+ if (lpUserName != NULL)
+ LocalFree(lpUserName);
+
+ if (lpDomainName != NULL)
+ LocalFree(lpDomainName);
return TRUE;
}
Modified: trunk/reactos/dll/win32/userenv/internal.h
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/userenv/internal…
==============================================================================
--- trunk/reactos/dll/win32/userenv/internal.h [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/userenv/internal.h [iso-8859-1] Sat Nov 2 14:45:26 2013
@@ -75,7 +75,11 @@
BOOL
GetUserSidFromToken(HANDLE hToken,
- PUNICODE_STRING SidString);
+ PSID *Sid);
+
+BOOL
+GetUserSidStringFromToken(HANDLE hToken,
+ PUNICODE_STRING SidString);
PSECURITY_DESCRIPTOR
CreateDefaultSecurityDescriptor(VOID);
Modified: trunk/reactos/dll/win32/userenv/misc.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/userenv/misc.c?r…
==============================================================================
--- trunk/reactos/dll/win32/userenv/misc.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/userenv/misc.c [iso-8859-1] Sat Nov 2 14:45:26 2013
@@ -53,56 +53,124 @@
BOOL
GetUserSidFromToken(HANDLE hToken,
- PUNICODE_STRING SidString)
-{
- PSID_AND_ATTRIBUTES SidBuffer, nsb;
+ PSID *Sid)
+{
+ PTOKEN_USER UserBuffer, nsb;
+ PSID pSid = NULL;
ULONG Length;
NTSTATUS Status;
Length = 256;
- SidBuffer = LocalAlloc(LMEM_FIXED,
- Length);
- if (SidBuffer == NULL)
- return FALSE;
+ UserBuffer = LocalAlloc(LPTR, Length);
+ if (UserBuffer == NULL)
+ {
+ return FALSE;
+ }
Status = NtQueryInformationToken(hToken,
TokenUser,
- (PVOID)SidBuffer,
+ (PVOID)UserBuffer,
Length,
&Length);
if (Status == STATUS_BUFFER_TOO_SMALL)
{
- nsb = LocalReAlloc(SidBuffer,
+ nsb = LocalReAlloc(UserBuffer, Length, LMEM_MOVEABLE);
+ if (nsb == NULL)
+ {
+ LocalFree(UserBuffer);
+ return FALSE;
+ }
+
+ UserBuffer = nsb;
+ Status = NtQueryInformationToken(hToken,
+ TokenUser,
+ (PVOID)UserBuffer,
+ Length,
+ &Length);
+ }
+
+ if (!NT_SUCCESS (Status))
+ {
+ LocalFree(UserBuffer);
+ return FALSE;
+ }
+
+ Length = RtlLengthSid(UserBuffer->User.Sid);
+
+ pSid = LocalAlloc(LPTR, Length);
+ if (pSid == NULL)
+ {
+ LocalFree(UserBuffer);
+ return FALSE;
+ }
+
+ Status = RtlCopySid(Length, pSid, UserBuffer->User.Sid);
+
+ LocalFree(UserBuffer);
+
+ if (!NT_SUCCESS (Status))
+ {
+ LocalFree(pSid);
+ return FALSE;
+ }
+
+ *Sid = pSid;
+
+ return TRUE;
+}
+
+
+BOOL
+GetUserSidStringFromToken(HANDLE hToken,
+ PUNICODE_STRING SidString)
+{
+ PTOKEN_USER UserBuffer, nsb;
+ ULONG Length;
+ NTSTATUS Status;
+
+ Length = 256;
+ UserBuffer = LocalAlloc(LPTR, Length);
+ if (UserBuffer == NULL)
+ return FALSE;
+
+ Status = NtQueryInformationToken(hToken,
+ TokenUser,
+ (PVOID)UserBuffer,
+ Length,
+ &Length);
+ if (Status == STATUS_BUFFER_TOO_SMALL)
+ {
+ nsb = LocalReAlloc(UserBuffer,
Length,
LMEM_MOVEABLE);
if (nsb == NULL)
{
- LocalFree((HLOCAL)SidBuffer);
+ LocalFree(UserBuffer);
return FALSE;
}
- SidBuffer = nsb;
+ UserBuffer = nsb;
Status = NtQueryInformationToken(hToken,
TokenUser,
- (PVOID)SidBuffer,
+ (PVOID)UserBuffer,
Length,
&Length);
}
if (!NT_SUCCESS (Status))
{
- LocalFree((HLOCAL)SidBuffer);
+ LocalFree(UserBuffer);
SetLastError(RtlNtStatusToDosError(Status));
return FALSE;
}
- DPRINT("SidLength: %lu\n", RtlLengthSid (SidBuffer[0].Sid));
+ DPRINT("SidLength: %lu\n", RtlLengthSid (UserBuffer->User.Sid));
Status = RtlConvertSidToUnicodeString(SidString,
- SidBuffer[0].Sid,
+ UserBuffer->User.Sid,
TRUE);
- LocalFree((HLOCAL)SidBuffer);
+ LocalFree(UserBuffer);
if (!NT_SUCCESS(Status))
{
@@ -116,6 +184,7 @@
return TRUE;
}
+
PSECURITY_DESCRIPTOR
CreateDefaultSecurityDescriptor(VOID)
Modified: trunk/reactos/dll/win32/userenv/profile.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/userenv/profile.…
==============================================================================
--- trunk/reactos/dll/win32/userenv/profile.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/userenv/profile.c [iso-8859-1] Sat Nov 2 14:45:26 2013
@@ -814,8 +814,8 @@
HKEY hKey;
LONG Error;
- if (!GetUserSidFromToken(hToken,
- &SidString))
+ if (!GetUserSidStringFromToken(hToken,
+ &SidString))
{
DPRINT1("GetUserSidFromToken() failed\n");
return FALSE;
@@ -898,8 +898,8 @@
DPRINT("CheckForLoadedProfile() called\n");
- if (!GetUserSidFromToken(hToken,
- &SidString))
+ if (!GetUserSidStringFromToken(hToken,
+ &SidString))
{
DPRINT1("GetUserSidFromToken() failed\n");
return FALSE;
@@ -1166,7 +1166,7 @@
}
/* Get user SID string */
- ret = GetUserSidFromToken(hToken, &SidString);
+ ret = GetUserSidStringFromToken(hToken, &SidString);
if (!ret)
{
DPRINT1("GetUserSidFromToken() failed\n");
@@ -1243,8 +1243,8 @@
RegCloseKey(hProfile);
- if (!GetUserSidFromToken(hToken,
- &SidString))
+ if (!GetUserSidStringFromToken(hToken,
+ &SidString))
{
DPRINT1("GetUserSidFromToken() failed\n");
return FALSE;