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