ReactOS  0.4.13-dev-687-g023794c
environment.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS Winlogon
4  * FILE: base/system/winlogon/environment.c
5  * PURPOSE: User environment routines
6  * PROGRAMMERS: Thomas Weidenmueller (w3seek@users.sourceforge.net)
7  * Hervé Poussineau (hpoussin@reactos.org)
8  * Eric Kohl
9  */
10 
11 /* INCLUDES *****************************************************************/
12 
13 #include "winlogon.h"
14 
15 /* FUNCTIONS ****************************************************************/
16 
17 static
18 VOID
20  IN PWLSESSION Session,
21  IN HKEY hKeyCurrentUser)
22 {
23  WCHAR szPath[MAX_PATH + 1];
24  LPCWSTR wstr;
25  SIZE_T size;
26  WCHAR szEnvKey[MAX_PATH];
27  WCHAR szEnvValue[1024];
28  SIZE_T length;
29  LPWSTR eqptr, endptr;
30  DWORD dwDisp;
31  LONG lError;
32  HKEY hKeyVolatileEnv;
33  HKEY hKeyShellFolders;
34  DWORD dwType;
35  DWORD dwSize;
36 
37  /* Create the 'Volatile Environment' key */
38  lError = RegCreateKeyExW(hKeyCurrentUser,
39  L"Volatile Environment",
40  0,
41  NULL,
43  KEY_WRITE,
44  NULL,
45  &hKeyVolatileEnv,
46  &dwDisp);
47  if (lError != ERROR_SUCCESS)
48  {
49  WARN("WL: RegCreateKeyExW() failed to create the volatile environment key (Error: %ld)\n", lError);
50  return;
51  }
52 
53  /* Parse the environment variables and add them to the volatile environment key */
54  if (Session->Profile->dwType == WLX_PROFILE_TYPE_V2_0 &&
55  Session->Profile->pszEnvironment != NULL)
56  {
57  wstr = Session->Profile->pszEnvironment;
58  while (*wstr != UNICODE_NULL)
59  {
60  size = wcslen(wstr) + 1;
61 
62  eqptr = wcschr(wstr, L'=');
63 
64  if (eqptr != NULL)
65  {
66  endptr = eqptr;
67 
68  endptr--;
69  while (iswspace(*endptr))
70  endptr--;
71 
72  length = (SIZE_T)(endptr - wstr + 1);
73 
74  wcsncpy(szEnvKey, wstr, length);
75  szEnvKey[length] = 0;
76 
77  eqptr++;
78  while (iswspace(*eqptr))
79  eqptr++;
80  wcscpy(szEnvValue, eqptr);
81 
82  RegSetValueExW(hKeyVolatileEnv,
83  szEnvKey,
84  0,
85  REG_SZ,
86  (LPBYTE)szEnvValue,
87  (wcslen(szEnvValue) + 1) * sizeof(WCHAR));
88  }
89 
90  wstr += size;
91  }
92  }
93 
94  /* Set the 'APPDATA' environment variable */
95  lError = RegOpenKeyExW(hKeyCurrentUser,
96  L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
97  0,
98  KEY_READ,
99  &hKeyShellFolders);
100  if (lError == ERROR_SUCCESS)
101  {
102  dwSize = (MAX_PATH + 1) * sizeof(WCHAR);
103  lError = RegQueryValueExW(hKeyShellFolders,
104  L"AppData",
105  NULL,
106  &dwType,
107  (LPBYTE)szPath,
108  &dwSize);
109  if (lError == ERROR_SUCCESS)
110  {
111  TRACE("APPDATA path: %S\n", szPath);
112  RegSetValueExW(hKeyVolatileEnv,
113  L"APPDATA",
114  0,
115  REG_SZ,
116  (LPBYTE)szPath,
117  (wcslen(szPath) + 1) * sizeof(WCHAR));
118  }
119 
120  RegCloseKey(hKeyShellFolders);
121  }
122 
123  RegCloseKey(hKeyVolatileEnv);
124 }
125 
126 
127 BOOL
129  IN PWLSESSION Session)
130 {
131  HKEY hKeyCurrentUser;
132  LONG lError;
133 
134  TRACE("WL: CreateUserEnvironment called\n");
135 
136  /* Impersonate the new user */
137  if (!ImpersonateLoggedOnUser(Session->UserToken))
138  {
139  ERR("ImpersonateLoggedOnUser() failed with error %lu\n", GetLastError());
140  return FALSE;
141  }
142 
143  /* Open the new user HKCU key */
145  &hKeyCurrentUser);
146  if (lError == ERROR_SUCCESS)
147  {
148  BuildVolatileEnvironment(Session,
149  hKeyCurrentUser);
150  RegCloseKey(hKeyCurrentUser);
151  }
152 
153  /* Revert the impersonation */
154  RevertToSelf();
155 
156  TRACE("WL: CreateUserEnvironment done\n");
157 
158  return TRUE;
159 }
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
#define ERROR_SUCCESS
Definition: deptool.c:10
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define KEY_READ
Definition: nt_native.h:1023
BOOL WINAPI RevertToSelf(VOID)
Definition: security.c:1487
#define WARN(fmt,...)
Definition: debug.h:111
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
BOOL CreateUserEnvironment(IN PWLSESSION Session)
Definition: environment.c:128
LONG WINAPI RegCreateKeyExW(_In_ HKEY hKey, _In_ LPCWSTR lpSubKey, _In_ DWORD Reserved, _In_opt_ LPWSTR lpClass, _In_ DWORD dwOptions, _In_ REGSAM samDesired, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _Out_ PHKEY phkResult, _Out_opt_ LPDWORD lpdwDisposition)
Definition: reg.c:1091
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
#define UNICODE_NULL
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
static VOID BuildVolatileEnvironment(IN PWLSESSION Session, IN HKEY hKeyCurrentUser)
Definition: environment.c:19
smooth NULL
Definition: ftsmooth.c:416
BOOL WINAPI ImpersonateLoggedOnUser(HANDLE hToken)
Definition: misc.c:152
#define KEY_WRITE
Definition: nt_native.h:1031
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4895
#define TRACE(s)
Definition: solgame.cpp:4
GLsizeiptr size
Definition: glext.h:5919
_CONST_RETURN wchar_t *__cdecl wcschr(_In_z_ const wchar_t *_Str, wchar_t _Ch)
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4116
__wchar_t WCHAR
Definition: xmlstorage.h:180
GLenum GLuint GLenum GLsizei length
Definition: glext.h:5579
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
#define iswspace(_c)
Definition: ctype.h:669
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
static const WCHAR L[]
Definition: oid.c:1250
#define ERR(fmt,...)
Definition: debug.h:109
ULONG_PTR SIZE_T
Definition: typedefs.h:78
_CRTIMP wchar_t *__cdecl wcsncpy(wchar_t *_Dest, const wchar_t *_Source, size_t _Count)
LPCWSTR szPath
Definition: env.c:35
LONG WINAPI RegOpenCurrentUser(IN REGSAM samDesired, OUT PHKEY phkResult)
Definition: reg.c:3232
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
#define REG_OPTION_VOLATILE
Definition: nt_native.h:1060
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define KEY_CREATE_SUB_KEY
Definition: nt_native.h:1018
#define WLX_PROFILE_TYPE_V2_0
Definition: winwlx.h:51
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
#define REG_SZ
Definition: layer.c:22