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