b59fda32e8955a8d70d7805019b5f6660c821e10
[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 * Startup (before the user logs in)
30 * - Services (NT, ?semi-synchronous?, not implemented yet)
31 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce (9x, asynch)
32 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices (9x, asynch)
33 *
34 * After log in
35 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce (all, synch)
36 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run (all, asynch)
37 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run (all, asynch)
38 * - Startup folders (all, ?asynch?, no imp)
39 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce (all, asynch)
40 *
41 * Somewhere in there is processing the RunOnceEx entries (also no imp)
42 */
43
44 #include "precomp.h"
45
46 EXTERN_C HRESULT WINAPI SHCreateSessionKey(REGSAM samDesired, PHKEY phKey);
47
48 #define INVALID_RUNCMD_RETURN -1
49 /**
50 * This function runs the specified command in the specified dir.
51 * [in,out] cmdline - the command line to run. The function may change the passed buffer.
52 * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
53 * [in] wait - whether to wait for the run program to finish before returning.
54 * [in] minimized - Whether to ask the program to run minimized.
55 *
56 * Returns:
57 * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
58 * If wait is FALSE - returns 0 if successful.
59 * If wait is TRUE - returns the program's return value.
60 */
61 static int runCmd(LPWSTR cmdline, LPCWSTR dir, BOOL wait, BOOL minimized)
62 {
63 STARTUPINFOW si;
64 PROCESS_INFORMATION info;
65 DWORD exit_code = 0;
66 WCHAR szCmdLineExp[MAX_PATH+1] = L"\0";
67
68 ExpandEnvironmentStringsW(cmdline, szCmdLineExp, sizeof(szCmdLineExp) / sizeof(WCHAR));
69
70 memset(&si, 0, sizeof(si));
71 si.cb = sizeof(si);
72 if (minimized)
73 {
74 si.dwFlags = STARTF_USESHOWWINDOW;
75 si.wShowWindow = SW_MINIMIZE;
76 }
77 memset(&info, 0, sizeof(info));
78
79 if (!CreateProcessW(NULL, szCmdLineExp, NULL, NULL, FALSE, 0, NULL, dir, &si, &info))
80 {
81 printf("Failed to run command (%ld)\n", GetLastError());
82
83 return INVALID_RUNCMD_RETURN;
84 }
85
86 printf("Successfully ran command\n");
87
88 if (wait)
89 { /* wait for the process to exit */
90 WaitForSingleObject(info.hProcess, INFINITE);
91 GetExitCodeProcess(info.hProcess, &exit_code);
92 }
93
94 CloseHandle(info.hThread);
95 CloseHandle(info.hProcess);
96
97 return exit_code;
98 }
99
100
101 /**
102 * Process a "Run" type registry key.
103 * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
104 * opened.
105 * szKeyName is the key holding the actual entries.
106 * bDelete tells whether we should delete each value right before executing it.
107 * bSynchronous tells whether we should wait for the prog to complete before
108 * going on to the next prog.
109 */
110 static BOOL ProcessRunKeys(HKEY hkRoot, LPCWSTR szKeyName, BOOL bDelete,
111 BOOL bSynchronous)
112 {
113 static const WCHAR WINKEY_NAME[]={'S','o','f','t','w','a','r','e','\\',
114 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
115 'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
116 HKEY hkWin = NULL, hkRun=NULL;
117 LONG res = ERROR_SUCCESS;
118 DWORD i, nMaxCmdLine = 0, nMaxValue = 0;
119 WCHAR *szCmdLine = NULL;
120 WCHAR *szValue = NULL;
121
122 if (hkRoot == HKEY_LOCAL_MACHINE)
123 wprintf(L"processing %s entries under HKLM\n", szKeyName);
124 else
125 wprintf(L"processing %s entries under HKCU\n", szKeyName);
126
127 if ((res = RegOpenKeyExW(hkRoot, WINKEY_NAME, 0, KEY_READ, &hkWin)) != ERROR_SUCCESS)
128 {
129 printf("RegOpenKey failed on Software\\Microsoft\\Windows\\CurrentVersion (%ld)\n",
130 res);
131
132 goto end;
133 }
134
135 if ((res = RegOpenKeyExW(hkWin, szKeyName, 0, bDelete?KEY_ALL_ACCESS:KEY_READ, &hkRun))!=
136 ERROR_SUCCESS)
137 {
138 if (res == ERROR_FILE_NOT_FOUND)
139 {
140 printf("Key doesn't exist - nothing to be done\n");
141
142 res = ERROR_SUCCESS;
143 }
144 else
145 printf("RegOpenKey failed on run key (%ld)\n", res);
146
147 goto end;
148 }
149
150 if ((res = RegQueryInfoKeyW(hkRun, NULL, NULL, NULL, NULL, NULL, NULL, &i, &nMaxValue,
151 &nMaxCmdLine, NULL, NULL)) != ERROR_SUCCESS)
152 {
153 printf("Couldn't query key info (%ld)\n", res);
154
155 goto end;
156 }
157
158 if (i == 0)
159 {
160 printf("No commands to execute.\n");
161
162 res = ERROR_SUCCESS;
163 goto end;
164 }
165
166 if ((szCmdLine = malloc(nMaxCmdLine)) == NULL)
167 {
168 printf("Couldn't allocate memory for the commands to be executed\n");
169
170 res = ERROR_NOT_ENOUGH_MEMORY;
171 goto end;
172 }
173
174 if ((szValue = malloc((++nMaxValue)*sizeof(*szValue))) == NULL)
175 {
176 printf("Couldn't allocate memory for the value names\n");
177
178 free(szCmdLine);
179 res = ERROR_NOT_ENOUGH_MEMORY;
180 goto end;
181 }
182
183 while(i > 0)
184 {
185 DWORD nValLength=nMaxValue, nDataLength=nMaxCmdLine;
186 DWORD type;
187
188 --i;
189
190 if ((res = RegEnumValueW(hkRun, i, szValue, &nValLength, 0, &type,
191 (LPBYTE)szCmdLine, &nDataLength)) != ERROR_SUCCESS)
192 {
193 printf("Couldn't read in value %ld - %ld\n", i, res);
194
195 continue;
196 }
197
198 /* safe mode - force to run if prefixed with asterisk */
199 if (GetSystemMetrics(SM_CLEANBOOT) && (szValue[0] != L'*')) continue;
200
201 if (bDelete && (res = RegDeleteValueW(hkRun, szValue)) != ERROR_SUCCESS)
202 {
203 printf("Couldn't delete value - %ld, %ld. Running command anyways.\n", i, res);
204 }
205
206 if (type != REG_SZ)
207 {
208 printf("Incorrect type of value #%ld (%ld)\n", i, type);
209
210 continue;
211 }
212
213 if ((res = runCmd(szCmdLine, NULL, bSynchronous, FALSE)) == INVALID_RUNCMD_RETURN)
214 {
215 printf("Error running cmd #%ld (%ld)\n", i, GetLastError());
216 }
217
218 printf("Done processing cmd #%ld\n", i);
219 }
220
221 free(szValue);
222 free(szCmdLine);
223 res = ERROR_SUCCESS;
224
225 end:
226 if (hkRun != NULL)
227 RegCloseKey(hkRun);
228 if (hkWin != NULL)
229 RegCloseKey(hkWin);
230
231 printf("done\n");
232
233 return res == ERROR_SUCCESS ? TRUE : FALSE;
234 }
235
236
237 int
238 ProcessStartupItems(VOID)
239 {
240 BOOL bNormalBoot = GetSystemMetrics(SM_CLEANBOOT) == 0; /* Perform the operations that are performed every boot */
241 /* First, set the current directory to SystemRoot */
242 TCHAR gen_path[MAX_PATH];
243 DWORD res;
244 HKEY hSessionKey, hKey;
245 HRESULT hr;
246
247 res = GetWindowsDirectory(gen_path, sizeof(gen_path));
248
249 if (res == 0)
250 {
251 printf("Couldn't get the windows directory - error %ld\n",
252 GetLastError());
253
254 return 100;
255 }
256
257 if (!SetCurrentDirectory(gen_path))
258 {
259 wprintf(L"Cannot set the dir to %s (%ld)\n", gen_path, GetLastError());
260
261 return 100;
262 }
263
264 hr = SHCreateSessionKey(KEY_WRITE, &hSessionKey);
265 if (SUCCEEDED(hr))
266 {
267 LONG Error;
268 DWORD dwDisp;
269
270 Error = RegCreateKeyEx(hSessionKey, L"StartupHasBeenRun", 0, NULL, REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisp);
271 RegCloseKey(hSessionKey);
272 if (Error == ERROR_SUCCESS)
273 {
274 RegCloseKey(hKey);
275 if (dwDisp == REG_OPENED_EXISTING_KEY)
276 {
277 /* Startup programs has already been run */
278 return 0;
279 }
280 }
281 }
282
283 /* Perform the operations by order checking if policy allows it, checking if this is not Safe Mode,
284 * stopping if one fails, skipping if necessary.
285 */
286 res = TRUE;
287 if (res && (SHRestricted(REST_NOLOCALMACHINERUNONCE) == 0))
288 res = ProcessRunKeys(HKEY_LOCAL_MACHINE, L"RunOnce", TRUE, TRUE);
289
290 if (res && bNormalBoot && (SHRestricted(REST_NOLOCALMACHINERUN) == 0))
291 res = ProcessRunKeys(HKEY_LOCAL_MACHINE, L"Run", FALSE, FALSE);
292
293 if (res && bNormalBoot && (SHRestricted(REST_NOCURRENTUSERRUNONCE) == 0))
294 res = ProcessRunKeys(HKEY_CURRENT_USER, L"Run", FALSE, FALSE);
295
296 if (res && bNormalBoot && (SHRestricted(REST_NOCURRENTUSERRUNONCE) == 0))
297 res = ProcessRunKeys(HKEY_CURRENT_USER, L"RunOnce", TRUE, FALSE);
298
299 printf("Operation done\n");
300
301 return res ? 0 : 101;
302 }