Sync up with trunk r61578.
[reactos.git] / base / system / winlogon / environment.c
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
17
18 /* FUNCTIONS ****************************************************************/
19
20 static
21 VOID
22 BuildVolatileEnvironment(
23 IN PWLSESSION Session,
24 IN HKEY hKeyCurrentUser)
25 {
26 WCHAR szPath[MAX_PATH + 1];
27 LPCWSTR wstr;
28 SIZE_T size;
29 WCHAR szEnvKey[MAX_PATH];
30 WCHAR szEnvValue[1024];
31 SIZE_T length;
32 LPWSTR eqptr, endptr;
33 DWORD dwDisp;
34 LONG lError;
35 HKEY hKeyVolatileEnv;
36 HKEY hKeyShellFolders;
37 DWORD dwType;
38 DWORD dwSize;
39
40 /* Create the 'Volatile Environment' key */
41 lError = RegCreateKeyExW(hKeyCurrentUser,
42 L"Volatile Environment",
43 0,
44 NULL,
45 REG_OPTION_VOLATILE,
46 KEY_WRITE,
47 NULL,
48 &hKeyVolatileEnv,
49 &dwDisp);
50 if (lError != ERROR_SUCCESS)
51 {
52 WARN("WL: RegCreateKeyExW() failed to create the volatile environment key (Error: %ld)\n", lError);
53 return;
54 }
55
56 /* Parse the environment variables and add them to the volatile environment key */
57 if (Session->Profile->dwType == WLX_PROFILE_TYPE_V2_0 &&
58 Session->Profile->pszEnvironment != NULL)
59 {
60 wstr = Session->Profile->pszEnvironment;
61 while (*wstr != UNICODE_NULL)
62 {
63 size = wcslen(wstr) + 1;
64
65 eqptr = wcschr(wstr, L'=');
66
67 if (eqptr != NULL)
68 {
69 endptr = eqptr;
70
71 endptr--;
72 while (iswspace(*endptr))
73 endptr--;
74
75 length = (SIZE_T)(endptr - wstr + 1);
76
77 wcsncpy(szEnvKey, wstr, length);
78 szEnvKey[length] = 0;
79
80 eqptr++;
81 while (iswspace(*eqptr))
82 eqptr++;
83 wcscpy(szEnvValue, eqptr);
84
85 RegSetValueExW(hKeyVolatileEnv,
86 szEnvKey,
87 0,
88 REG_SZ,
89 (LPBYTE)szEnvValue,
90 (wcslen(szEnvValue) + 1) * sizeof(WCHAR));
91 }
92
93 wstr += size;
94 }
95 }
96
97 /* Set the 'APPDATA' environment variable */
98 lError = RegOpenKeyExW(hKeyCurrentUser,
99 L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
100 0,
101 KEY_READ,
102 &hKeyShellFolders);
103 if (lError == ERROR_SUCCESS)
104 {
105 dwSize = (MAX_PATH + 1) * sizeof(WCHAR);
106 lError = RegQueryValueExW(hKeyShellFolders,
107 L"AppData",
108 NULL,
109 &dwType,
110 (LPBYTE)szPath,
111 &dwSize);
112 if (lError == ERROR_SUCCESS)
113 {
114 TRACE("APPDATA path: %S\n", szPath);
115 RegSetValueExW(hKeyVolatileEnv,
116 L"APPDATA",
117 0,
118 REG_SZ,
119 (LPBYTE)szPath,
120 (wcslen(szPath) + 1) * sizeof(WCHAR));
121 }
122
123 RegCloseKey(hKeyShellFolders);
124 }
125
126 RegCloseKey(hKeyVolatileEnv);
127 }
128
129
130 BOOL
131 CreateUserEnvironment(
132 IN PWLSESSION Session)
133 {
134 HKEY hKeyCurrentUser;
135 LONG lError;
136
137 TRACE("WL: CreateUserEnvironment called\n");
138
139 /* Impersonate the new user */
140 ImpersonateLoggedOnUser(Session->UserToken);
141
142 /* Open the new users HKCU key */
143 lError = RegOpenCurrentUser(KEY_CREATE_SUB_KEY,
144 &hKeyCurrentUser);
145 if (lError == ERROR_SUCCESS)
146 {
147 BuildVolatileEnvironment(Session,
148 hKeyCurrentUser);
149 RegCloseKey(hKeyCurrentUser);
150 }
151
152 /* Revert the impersonation */
153 RevertToSelf();
154
155 TRACE("WL: CreateUserEnvironment done\n");
156
157 return TRUE;
158 }