7bb3f380113bd81be1c043b032364f797497f28a
[reactos.git] / reactos / 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 WINE_DEFAULT_DEBUG_CHANNEL(winlogon);
16
17 /* GLOBALS ******************************************************************/
18
19
20 /* FUNCTIONS ****************************************************************/
21
22 static VOID
23 BuildVolatileEnvironment(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(IN PWLSESSION Session)
132 {
133 HKEY hKeyCurrentUser;
134 LONG lError;
135
136 TRACE("WL: CreateUserEnvironment called\n");
137
138 /* Impersonate the new user */
139 ImpersonateLoggedOnUser(Session->UserToken);
140
141 /* Open the new users HKCU key */
142 lError = RegOpenCurrentUser(KEY_CREATE_SUB_KEY,
143 &hKeyCurrentUser);
144 if (lError == ERROR_SUCCESS)
145 {
146 BuildVolatileEnvironment(Session,
147 hKeyCurrentUser);
148 RegCloseKey(hKeyCurrentUser);
149 }
150
151 /* Revert the impersonation */
152 RevertToSelf();
153
154 TRACE("WL: CreateUserEnvironment done\n");
155
156 return TRUE;
157 }