[WINLOGON]
[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
23 VOID
24 BuildVolatileEnvironment(
25 IN PWLSESSION Session,
26 IN HKEY hKeyCurrentUser)
27 {
28 WCHAR szPath[MAX_PATH + 1];
29 LPCWSTR wstr;
30 SIZE_T size;
31 WCHAR szEnvKey[MAX_PATH];
32 WCHAR szEnvValue[1024];
33 SIZE_T length;
34 LPWSTR eqptr, endptr;
35 DWORD dwDisp;
36 LONG lError;
37 HKEY hKeyVolatileEnv;
38 HKEY hKeyShellFolders;
39 DWORD dwType;
40 DWORD dwSize;
41
42 /* Create the 'Volatile Environment' key */
43 lError = RegCreateKeyExW(hKeyCurrentUser,
44 L"Volatile Environment",
45 0,
46 NULL,
47 REG_OPTION_VOLATILE,
48 KEY_WRITE,
49 NULL,
50 &hKeyVolatileEnv,
51 &dwDisp);
52 if (lError != ERROR_SUCCESS)
53 {
54 WARN("WL: RegCreateKeyExW() failed to create the volatile environment key (Error: %ld)\n", lError);
55 return;
56 }
57
58 /* Parse the environment variables and add them to the volatile environment key */
59 if (Session->Profile->dwType == WLX_PROFILE_TYPE_V2_0 &&
60 Session->Profile->pszEnvironment != NULL)
61 {
62 wstr = Session->Profile->pszEnvironment;
63 while (*wstr != UNICODE_NULL)
64 {
65 size = wcslen(wstr) + 1;
66
67 eqptr = wcschr(wstr, L'=');
68
69 if (eqptr != NULL)
70 {
71 endptr = eqptr;
72
73 endptr--;
74 while (iswspace(*endptr))
75 endptr--;
76
77 length = (SIZE_T)(endptr - wstr + 1);
78
79 wcsncpy(szEnvKey, wstr, length);
80 szEnvKey[length] = 0;
81
82 eqptr++;
83 while (iswspace(*eqptr))
84 eqptr++;
85 wcscpy(szEnvValue, eqptr);
86
87 RegSetValueExW(hKeyVolatileEnv,
88 szEnvKey,
89 0,
90 REG_SZ,
91 (LPBYTE)szEnvValue,
92 (wcslen(szEnvValue) + 1) * sizeof(WCHAR));
93 }
94
95 wstr += size;
96 }
97 }
98
99 /* Set the 'APPDATA' environment variable */
100 lError = RegOpenKeyExW(hKeyCurrentUser,
101 L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
102 0,
103 KEY_READ,
104 &hKeyShellFolders);
105 if (lError == ERROR_SUCCESS)
106 {
107 dwSize = (MAX_PATH + 1) * sizeof(WCHAR);
108 lError = RegQueryValueExW(hKeyShellFolders,
109 L"AppData",
110 NULL,
111 &dwType,
112 (LPBYTE)szPath,
113 &dwSize);
114 if (lError == ERROR_SUCCESS)
115 {
116 TRACE("APPDATA path: %S\n", szPath);
117 RegSetValueExW(hKeyVolatileEnv,
118 L"APPDATA",
119 0,
120 REG_SZ,
121 (LPBYTE)szPath,
122 (wcslen(szPath) + 1) * sizeof(WCHAR));
123 }
124
125 RegCloseKey(hKeyShellFolders);
126 }
127
128 RegCloseKey(hKeyVolatileEnv);
129 }
130
131
132 BOOL
133 CreateUserEnvironment(
134 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 }