[EXPLORER_NEW]
[reactos.git] / reactos / 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 printf("Failed to run command (%ld)\n", GetLastError());
79
80 return INVALID_RUNCMD_RETURN;
81 }
82
83 printf("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, nMaxCmdLine = 0, nMaxValue = 0;
113 WCHAR *szCmdLine = NULL;
114 WCHAR *szValue = NULL;
115
116 if (hkRoot == HKEY_LOCAL_MACHINE)
117 wprintf(L"processing %s entries under HKLM\n", szKeyName);
118 else
119 wprintf(L"processing %s entries under HKCU\n", szKeyName);
120
121 if ((res = RegOpenKeyExW(hkRoot, L"Software\\Microsoft\\Windows\\CurrentVersion", 0, KEY_READ, &hkWin)) != ERROR_SUCCESS)
122 {
123 printf("RegOpenKey failed on Software\\Microsoft\\Windows\\CurrentVersion (%ld)\n",
124 res);
125
126 goto end;
127 }
128
129 if ((res = RegOpenKeyExW(hkWin, szKeyName, 0, bDelete?KEY_ALL_ACCESS:KEY_READ, &hkRun))!=
130 ERROR_SUCCESS)
131 {
132 if (res == ERROR_FILE_NOT_FOUND)
133 {
134 printf("Key doesn't exist - nothing to be done\n");
135
136 res = ERROR_SUCCESS;
137 }
138 else
139 printf("RegOpenKey failed on run key (%ld)\n", res);
140
141 goto end;
142 }
143
144 if ((res = RegQueryInfoKeyW(hkRun, NULL, NULL, NULL, NULL, NULL, NULL, &i, &nMaxValue,
145 &nMaxCmdLine, NULL, NULL)) != ERROR_SUCCESS)
146 {
147 printf("Couldn't query key info (%ld)\n", res);
148
149 goto end;
150 }
151
152 if (i == 0)
153 {
154 printf("No commands to execute.\n");
155
156 res = ERROR_SUCCESS;
157 goto end;
158 }
159
160 if ((szCmdLine = malloc(nMaxCmdLine)) == NULL)
161 {
162 printf("Couldn't allocate memory for the commands to be executed\n");
163
164 res = ERROR_NOT_ENOUGH_MEMORY;
165 goto end;
166 }
167
168 if ((szValue = malloc((++nMaxValue)*sizeof(*szValue))) == NULL)
169 {
170 printf("Couldn't allocate memory for the value names\n");
171
172 free(szCmdLine);
173 res = ERROR_NOT_ENOUGH_MEMORY;
174 goto end;
175 }
176
177 while (i > 0)
178 {
179 DWORD nValLength = nMaxValue, nDataLength = nMaxCmdLine;
180 DWORD type;
181
182 --i;
183
184 if ((res = RegEnumValueW(hkRun, i, szValue, &nValLength, 0, &type,
185 (LPBYTE)szCmdLine, &nDataLength)) != ERROR_SUCCESS)
186 {
187 printf("Couldn't read in value %ld - %ld\n", i, res);
188
189 continue;
190 }
191
192 /* safe mode - force to run if prefixed with asterisk */
193 if (GetSystemMetrics(SM_CLEANBOOT) && (szValue[0] != L'*')) continue;
194
195 if (bDelete && (res = RegDeleteValueW(hkRun, szValue)) != ERROR_SUCCESS)
196 {
197 printf("Couldn't delete value - %ld, %ld. Running command anyways.\n", i, res);
198 }
199
200 if (type != REG_SZ)
201 {
202 printf("Incorrect type of value #%ld (%ld)\n", i, type);
203
204 continue;
205 }
206
207 if ((res = runCmd(szCmdLine, NULL, bSynchronous, FALSE)) == INVALID_RUNCMD_RETURN)
208 {
209 printf("Error running cmd #%ld (%ld)\n", i, GetLastError());
210 }
211
212 printf("Done processing cmd #%ld\n", i);
213 }
214
215 free(szValue);
216 free(szCmdLine);
217 res = ERROR_SUCCESS;
218
219 end:
220 if (hkRun != NULL)
221 RegCloseKey(hkRun);
222 if (hkWin != NULL)
223 RegCloseKey(hkWin);
224
225 printf("done\n");
226
227 return res == ERROR_SUCCESS ? TRUE : FALSE;
228 }
229
230
231 int
232 ProcessStartupItems(VOID)
233 {
234 /* TODO: ProcessRunKeys already checks SM_CLEANBOOT -- items prefixed with * should probably run even in safe mode */
235 BOOL bNormalBoot = GetSystemMetrics(SM_CLEANBOOT) == 0; /* Perform the operations that are performed every boot */
236 /* First, set the current directory to SystemRoot */
237 TCHAR gen_path[MAX_PATH];
238 DWORD res;
239 HKEY hSessionKey, hKey;
240 HRESULT hr;
241
242 res = GetWindowsDirectory(gen_path, sizeof(gen_path));
243
244 if (res == 0)
245 {
246 printf("Couldn't get the windows directory - error %ld\n",
247 GetLastError());
248
249 return 100;
250 }
251
252 if (!SetCurrentDirectory(gen_path))
253 {
254 wprintf(L"Cannot set the dir to %s (%ld)\n", gen_path, GetLastError());
255
256 return 100;
257 }
258
259 hr = SHCreateSessionKey(KEY_WRITE, &hSessionKey);
260 if (SUCCEEDED(hr))
261 {
262 LONG Error;
263 DWORD dwDisp;
264
265 Error = RegCreateKeyEx(hSessionKey, L"StartupHasBeenRun", 0, NULL, REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisp);
266 RegCloseKey(hSessionKey);
267 if (Error == ERROR_SUCCESS)
268 {
269 RegCloseKey(hKey);
270 if (dwDisp == REG_OPENED_EXISTING_KEY)
271 {
272 /* Startup programs has already been run */
273 return 0;
274 }
275 }
276 }
277
278 /* Perform the operations by order checking if policy allows it, checking if this is not Safe Mode,
279 * stopping if one fails, skipping if necessary.
280 */
281 res = TRUE;
282 /* TODO: RunOnceEx */
283
284 if (res && (SHRestricted(REST_NOLOCALMACHINERUNONCE) == 0))
285 res = ProcessRunKeys(HKEY_LOCAL_MACHINE, L"RunOnce", TRUE, TRUE);
286
287 if (res && bNormalBoot && (SHRestricted(REST_NOLOCALMACHINERUN) == 0))
288 res = ProcessRunKeys(HKEY_LOCAL_MACHINE, L"Run", FALSE, FALSE);
289
290 if (res && bNormalBoot && (SHRestricted(REST_NOCURRENTUSERRUNONCE) == 0))
291 res = ProcessRunKeys(HKEY_CURRENT_USER, L"Run", FALSE, FALSE);
292
293 /* TODO: All users Startup folder */
294
295 /* TODO: Current user Startup folder */
296
297 /* TODO: HKCU\RunOnce runs even if StartupHasBeenRun exists */
298 if (res && bNormalBoot && (SHRestricted(REST_NOCURRENTUSERRUNONCE) == 0))
299 res = ProcessRunKeys(HKEY_CURRENT_USER, L"RunOnce", TRUE, FALSE);
300
301 printf("Operation done\n");
302
303 return res ? 0 : 101;
304 }