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