4d4295bb5dfea60b48b112a1822543d44f89581b
[reactos.git] / base / shell / explorer-new / startup.c
1 /*
2 * Copyright (C) 2002 Andreas Mohr
3 * Copyright (C) 2002 Shachar Shemesh
4 * Copyright (C) 2013 Edijs Kolesnikovics
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /* Based on the Wine "bootup" handler application
22 *
23 * This app handles the various "hooks" windows allows for applications to perform
24 * as part of the bootstrap process. Theses are roughly devided into three types.
25 * Knowledge base articles that explain this are 137367, 179365, 232487 and 232509.
26 * Also, 119941 has some info on grpconv.exe
27 * The operations performed are (by order of execution):
28 *
29 * After log in
30 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnceEx (synch, no imp)
31 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce (synch)
32 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run (asynch)
33 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run (asynch)
34 * - All users Startup folder "%ALLUSERSPROFILE%\Start Menu\Programs\Startup" (asynch, no imp)
35 * - Current user Startup folder "%USERPROFILE%\Start Menu\Programs\Startup" (asynch, no imp)
36 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce (asynch)
37 *
38 * None is processed in Safe Mode // FIXME: Check RunOnceEx in Safe Mode
39 */
40
41 #include "precomp.h"
42
43 EXTERN_C HRESULT WINAPI SHCreateSessionKey(REGSAM samDesired, PHKEY phKey);
44
45 #define INVALID_RUNCMD_RETURN -1
46 /**
47 * This function runs the specified command in the specified dir.
48 * [in,out] cmdline - the command line to run. The function may change the passed buffer.
49 * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
50 * [in] wait - whether to wait for the run program to finish before returning.
51 * [in] minimized - Whether to ask the program to run minimized.
52 *
53 * Returns:
54 * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
55 * If wait is FALSE - returns 0 if successful.
56 * If wait is TRUE - returns the program's return value.
57 */
58 static int runCmd(LPWSTR cmdline, LPCWSTR dir, BOOL wait, BOOL minimized)
59 {
60 STARTUPINFOW si;
61 PROCESS_INFORMATION info;
62 DWORD exit_code = 0;
63 WCHAR szCmdLineExp[MAX_PATH+1] = L"\0";
64
65 ExpandEnvironmentStringsW(cmdline, szCmdLineExp, sizeof(szCmdLineExp) / sizeof(WCHAR));
66
67 memset(&si, 0, sizeof(si));
68 si.cb = sizeof(si);
69 if (minimized)
70 {
71 si.dwFlags = STARTF_USESHOWWINDOW;
72 si.wShowWindow = SW_MINIMIZE;
73 }
74 memset(&info, 0, sizeof(info));
75
76 if (!CreateProcessW(NULL, szCmdLineExp, NULL, NULL, FALSE, 0, NULL, dir, &si, &info))
77 {
78 DbgPrint("Failed to run command (%lu)\n", GetLastError());
79
80 return INVALID_RUNCMD_RETURN;
81 }
82
83 DbgPrint("Successfully ran command\n");
84
85 if (wait)
86 { /* wait for the process to exit */
87 WaitForSingleObject(info.hProcess, INFINITE);
88 GetExitCodeProcess(info.hProcess, &exit_code);
89 }
90
91 CloseHandle(info.hThread);
92 CloseHandle(info.hProcess);
93
94 return exit_code;
95 }
96
97
98 /**
99 * Process a "Run" type registry key.
100 * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
101 * opened.
102 * szKeyName is the key holding the actual entries.
103 * bDelete tells whether we should delete each value right before executing it.
104 * bSynchronous tells whether we should wait for the prog to complete before
105 * going on to the next prog.
106 */
107 static BOOL ProcessRunKeys(HKEY hkRoot, LPCWSTR szKeyName, BOOL bDelete,
108 BOOL bSynchronous)
109 {
110 HKEY hkWin = NULL, hkRun = NULL;
111 LONG res = ERROR_SUCCESS;
112 DWORD i, cbMaxCmdLine = 0, cchMaxValue = 0;
113 WCHAR *szCmdLine = NULL;
114 WCHAR *szValue = NULL;
115
116 if (hkRoot == HKEY_LOCAL_MACHINE)
117 DbgPrint("processing %ls entries under HKLM\n", szKeyName);
118 else
119 DbgPrint("processing %ls entries under HKCU\n", szKeyName);
120
121 res = RegOpenKeyExW(hkRoot,
122 L"Software\\Microsoft\\Windows\\CurrentVersion",
123 0,
124 KEY_READ,
125 &hkWin);
126 if (res != ERROR_SUCCESS)
127 {
128 DbgPrint("RegOpenKey failed on Software\\Microsoft\\Windows\\CurrentVersion (%ld)\n", res);
129
130 goto end;
131 }
132
133 res = RegOpenKeyExW(hkWin,
134 szKeyName,
135 0,
136 bDelete ? KEY_ALL_ACCESS : KEY_READ,
137 &hkRun);
138 if (res != ERROR_SUCCESS)
139 {
140 if (res == ERROR_FILE_NOT_FOUND)
141 {
142 DbgPrint("Key doesn't exist - nothing to be done\n");
143
144 res = ERROR_SUCCESS;
145 }
146 else
147 DbgPrint("RegOpenKeyEx failed on run key (%ld)\n", res);
148
149 goto end;
150 }
151
152 res = RegQueryInfoKeyW(hkRun,
153 NULL,
154 NULL,
155 NULL,
156 NULL,
157 NULL,
158 NULL,
159 &i,
160 &cchMaxValue,
161 &cbMaxCmdLine,
162 NULL,
163 NULL);
164 if (res != ERROR_SUCCESS)
165 {
166 DbgPrint("Couldn't query key info (%ld)\n", res);
167
168 goto end;
169 }
170
171 if (i == 0)
172 {
173 DbgPrint("No commands to execute.\n");
174
175 res = ERROR_SUCCESS;
176 goto end;
177 }
178
179 szCmdLine = HeapAlloc(hProcessHeap,
180 0,
181 cbMaxCmdLine);
182 if (szCmdLine == NULL)
183 {
184 DbgPrint("Couldn't allocate memory for the commands to be executed\n");
185
186 res = ERROR_NOT_ENOUGH_MEMORY;
187 goto end;
188 }
189
190 ++cchMaxValue;
191 szValue = HeapAlloc(hProcessHeap,
192 0,
193 cchMaxValue * sizeof(*szValue));
194 if (szValue == NULL)
195 {
196 DbgPrint("Couldn't allocate memory for the value names\n");
197
198 res = ERROR_NOT_ENOUGH_MEMORY;
199 goto end;
200 }
201
202 while (i > 0)
203 {
204 DWORD cchValLength = cchMaxValue, cbDataLength = cbMaxCmdLine;
205 DWORD type;
206
207 --i;
208
209 res = RegEnumValueW(hkRun,
210 i,
211 szValue,
212 &cchValLength,
213 0,
214 &type,
215 (PBYTE)szCmdLine,
216 &cbDataLength);
217 if (res != ERROR_SUCCESS)
218 {
219 DbgPrint("Couldn't read in value %lu - %ld\n", i, res);
220
221 continue;
222 }
223
224 /* safe mode - force to run if prefixed with asterisk */
225 if (GetSystemMetrics(SM_CLEANBOOT) && (szValue[0] != L'*')) continue;
226
227 if (bDelete && (res = RegDeleteValueW(hkRun, szValue)) != ERROR_SUCCESS)
228 {
229 DbgPrint("Couldn't delete value - %lu, %ld. Running command anyways.\n", i, res);
230 }
231
232 if (type != REG_SZ)
233 {
234 DbgPrint("Incorrect type of value #%lu (%lu)\n", i, type);
235
236 continue;
237 }
238
239 res = runCmd(szCmdLine, NULL, bSynchronous, FALSE);
240 if (res == INVALID_RUNCMD_RETURN)
241 {
242 DbgPrint("Error running cmd #%lu (%lu)\n", i, GetLastError());
243 }
244
245 DbgPrint("Done processing cmd #%lu\n", i);
246 }
247
248 res = ERROR_SUCCESS;
249 end:
250 if (szValue != NULL)
251 HeapFree(hProcessHeap, 0, szValue);
252 if (szCmdLine != NULL)
253 HeapFree(hProcessHeap, 0, szCmdLine);
254 if (hkRun != NULL)
255 RegCloseKey(hkRun);
256 if (hkWin != NULL)
257 RegCloseKey(hkWin);
258
259 DbgPrint("done\n");
260
261 return res == ERROR_SUCCESS ? TRUE : FALSE;
262 }
263
264
265 int
266 ProcessStartupItems(VOID)
267 {
268 /* TODO: ProcessRunKeys already checks SM_CLEANBOOT -- items prefixed with * should probably run even in safe mode */
269 BOOL bNormalBoot = GetSystemMetrics(SM_CLEANBOOT) == 0; /* Perform the operations that are performed every boot */
270 /* First, set the current directory to SystemRoot */
271 WCHAR gen_path[MAX_PATH];
272 DWORD res;
273 HKEY hSessionKey, hKey;
274 HRESULT hr;
275
276 res = GetWindowsDirectoryW(gen_path, sizeof(gen_path) / sizeof(gen_path[0]));
277 if (res == 0)
278 {
279 DbgPrint("Couldn't get the windows directory - error %lu\n", GetLastError());
280
281 return 100;
282 }
283
284 if (!SetCurrentDirectoryW(gen_path))
285 {
286 DbgPrint("Cannot set the dir to %ls (%lu)\n", gen_path, GetLastError());
287
288 return 100;
289 }
290
291 hr = SHCreateSessionKey(KEY_WRITE, &hSessionKey);
292 if (SUCCEEDED(hr))
293 {
294 LONG Error;
295 DWORD dwDisp;
296
297 Error = RegCreateKeyExW(hSessionKey, L"StartupHasBeenRun", 0, NULL, REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisp);
298 RegCloseKey(hSessionKey);
299 if (Error == ERROR_SUCCESS)
300 {
301 RegCloseKey(hKey);
302 if (dwDisp == REG_OPENED_EXISTING_KEY)
303 {
304 /* Startup programs has already been run */
305 return 0;
306 }
307 }
308 }
309
310 /* Perform the operations by order checking if policy allows it, checking if this is not Safe Mode,
311 * stopping if one fails, skipping if necessary.
312 */
313 res = TRUE;
314 /* TODO: RunOnceEx */
315
316 if (res && (SHRestricted(REST_NOLOCALMACHINERUNONCE) == 0))
317 res = ProcessRunKeys(HKEY_LOCAL_MACHINE, L"RunOnce", TRUE, TRUE);
318
319 if (res && bNormalBoot && (SHRestricted(REST_NOLOCALMACHINERUN) == 0))
320 res = ProcessRunKeys(HKEY_LOCAL_MACHINE, L"Run", FALSE, FALSE);
321
322 if (res && bNormalBoot && (SHRestricted(REST_NOCURRENTUSERRUNONCE) == 0))
323 res = ProcessRunKeys(HKEY_CURRENT_USER, L"Run", FALSE, FALSE);
324
325 /* TODO: All users Startup folder */
326
327 /* TODO: Current user Startup folder */
328
329 /* TODO: HKCU\RunOnce runs even if StartupHasBeenRun exists */
330 if (res && bNormalBoot && (SHRestricted(REST_NOCURRENTUSERRUNONCE) == 0))
331 res = ProcessRunKeys(HKEY_CURRENT_USER, L"RunOnce", TRUE, FALSE);
332
333 DbgPrint("Operation done\n");
334
335 return res ? 0 : 101;
336 }