ReactOS 0.4.17-dev-1190-g4c812f4
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/* GLOBALS ******************************************************************/
16
17static const struct
18{
22{
23 {L"APPDATA", L"AppData"},
24 {L"LOCALAPPDATA", L"Local AppData"},
25};
26
27
28/* FUNCTIONS ****************************************************************/
29
30static
31VOID
33 IN PWLSESSION Session,
34 IN HKEY hKeyCurrentUser)
35{
37 LPCWSTR wstr;
39 WCHAR szEnvKey[MAX_PATH];
40 WCHAR szEnvValue[1024];
42 LPWSTR eqptr, endptr;
43 DWORD dwDisp;
44 LONG lError;
45 HKEY hKeyVolatileEnv;
46 HKEY hKeyShellFolders;
47 DWORD dwType;
49
50 /* Create the 'Volatile Environment' key */
51 lError = RegCreateKeyExW(hKeyCurrentUser,
52 L"Volatile Environment",
53 0,
54 NULL,
57 NULL,
58 &hKeyVolatileEnv,
59 &dwDisp);
60 if (lError != ERROR_SUCCESS)
61 {
62 WARN("WL: RegCreateKeyExW() failed to create the volatile environment key (Error: %ld)\n", lError);
63 return;
64 }
65
66 /* Parse the environment variables and add them to the volatile environment key */
67 if (Session->Profile->dwType == WLX_PROFILE_TYPE_V2_0 &&
68 Session->Profile->pszEnvironment != NULL)
69 {
70 wstr = Session->Profile->pszEnvironment;
71 while (*wstr != UNICODE_NULL)
72 {
73 size = wcslen(wstr) + 1;
74
75 eqptr = wcschr(wstr, L'=');
76
77 if (eqptr != NULL)
78 {
79 endptr = eqptr;
80
81 endptr--;
82 while (iswspace(*endptr))
83 endptr--;
84
85 length = (SIZE_T)(endptr - wstr + 1);
86
87 wcsncpy(szEnvKey, wstr, length);
88 szEnvKey[length] = 0;
89
90 eqptr++;
91 while (iswspace(*eqptr))
92 eqptr++;
93 wcscpy(szEnvValue, eqptr);
94
95 RegSetValueExW(hKeyVolatileEnv,
96 szEnvKey,
97 0,
98 REG_SZ,
99 (LPBYTE)szEnvValue,
100 (wcslen(szEnvValue) + 1) * sizeof(WCHAR));
101 }
102
103 wstr += size;
104 }
105 }
106
107 /* Set the 'APPDATA' and 'LOCALAPPDATA' environment variables */
108 lError = RegOpenKeyExW(hKeyCurrentUser,
109 L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
110 0,
111 KEY_READ,
112 &hKeyShellFolders);
113 if (lError == ERROR_SUCCESS)
114 {
115 ULONG i;
116
117 for (i = 0; i < ARRAYSIZE(AppDataVariables); i++)
118 {
119 dwSize = (MAX_PATH + 1) * sizeof(WCHAR);
120 lError = RegQueryValueExW(hKeyShellFolders,
122 NULL,
123 &dwType,
124 (LPBYTE)szPath,
125 &dwSize);
126 if (lError != ERROR_SUCCESS)
127 {
128 WARN("WL: No '%S' shell folder (Error: %ld)\n",
130 continue;
131 }
132
133 TRACE("%S path: %S\n", AppDataVariables[i].pszVariable, szPath);
134 RegSetValueExW(hKeyVolatileEnv,
136 0,
137 REG_SZ,
138 (LPBYTE)szPath,
139 (wcslen(szPath) + 1) * sizeof(WCHAR));
140 }
141
142 RegCloseKey(hKeyShellFolders);
143 }
144
145 RegCloseKey(hKeyVolatileEnv);
146}
147
148
149BOOL
151 IN PWLSESSION Session)
152{
153 HKEY hKeyCurrentUser;
154 LONG lError;
155
156 TRACE("WL: CreateUserEnvironment called\n");
157
158 /* Impersonate the new user */
159 if (!ImpersonateLoggedOnUser(Session->UserToken))
160 {
161 ERR("ImpersonateLoggedOnUser() failed with error %lu\n", GetLastError());
162 return FALSE;
163 }
164
165 /* Open the new user HKCU key */
167 &hKeyCurrentUser);
168 if (lError == ERROR_SUCCESS)
169 {
171 hKeyCurrentUser);
172 RegCloseKey(hKeyCurrentUser);
173 }
174
175 /* Revert the impersonation */
176 RevertToSelf();
177
178 TRACE("WL: CreateUserEnvironment done\n");
179
180 return TRUE;
181}
#define REG_SZ
Definition: layer.c:22
#define WARN(fmt,...)
Definition: precomp.h:61
#define ERR(fmt,...)
Definition: precomp.h:57
static const struct @134 AppDataVariables[]
PCWSTR pszShellFolder
Definition: environment.c:20
PCWSTR pszVariable
Definition: environment.c:19
BOOL CreateUserEnvironment(IN PWLSESSION Session)
Definition: environment.c:150
static VOID BuildVolatileEnvironment(IN PWLSESSION Session, IN HKEY hKeyCurrentUser)
Definition: environment.c:32
#define RegCloseKey(hKey)
Definition: registry.h:49
#define ARRAYSIZE(a)
Definition: cat.c:25
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
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:1096
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3333
LONG WINAPI RegOpenCurrentUser(IN REGSAM samDesired, OUT PHKEY phkResult)
Definition: reg.c:3209
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:4882
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4103
BOOL WINAPI ImpersonateLoggedOnUser(HANDLE hToken)
Definition: misc.c:152
#define wcschr
Definition: compat.h:17
#define MAX_PATH
Definition: compat.h:34
BOOL WINAPI RevertToSelf(void)
Definition: security.c:855
_ACRTIMP size_t __cdecl wcslen(const wchar_t *)
Definition: wcs.c:2988
#define L(x)
Definition: resources.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLsizeiptr size
Definition: glext.h:5919
GLuint GLsizei GLsizei * length
Definition: glext.h:6040
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
LPCWSTR szPath
Definition: env.c:37
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
#define KEY_READ
Definition: nt_native.h:1026
#define KEY_CREATE_SUB_KEY
Definition: nt_native.h:1021
#define KEY_WRITE
Definition: nt_native.h:1034
#define REG_OPTION_VOLATILE
Definition: nt_native.h:1063
#define UNICODE_NULL
short WCHAR
Definition: pedump.c:58
long LONG
Definition: pedump.c:60
#define iswspace(_c)
Definition: ctype.h:669
wcsncpy
wcscpy
#define TRACE(s)
Definition: solgame.cpp:4
const uint16_t * PCWSTR
Definition: typedefs.h:57
const uint16_t * LPCWSTR
Definition: typedefs.h:57
unsigned char * LPBYTE
Definition: typedefs.h:53
uint16_t * LPWSTR
Definition: typedefs.h:56
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define WLX_PROFILE_TYPE_V2_0
Definition: winwlx.h:51