Merge r51043 and r51044
[reactos.git] / reactos / dll / win32 / syssetup / install.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2003 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS system libraries
22 * PURPOSE: System setup
23 * FILE: dll/win32/syssetup/install.c
24 * PROGRAMER: Eric Kohl
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #define WIN32_NO_STATUS
30 #include <windows.h>
31 #define NTOS_MODE_USER
32 #include <ndk/ntndk.h>
33
34 #include <commctrl.h>
35 #include <stdio.h>
36 #include <io.h>
37 #include <tchar.h>
38 #include <stdlib.h>
39
40 #include <samlib/samlib.h>
41 #include <syssetup/syssetup.h>
42 #include <userenv.h>
43 #include <setupapi.h>
44
45 #include <shlobj.h>
46 #include <objidl.h>
47 #include <shlwapi.h>
48
49 #include "globals.h"
50 #include "resource.h"
51
52 #define NDEBUG
53 #include <debug.h>
54
55 DWORD WINAPI
56 CMP_WaitNoPendingInstallEvents(DWORD dwTimeout);
57
58 /* GLOBALS ******************************************************************/
59
60 PSID DomainSid = NULL;
61 PSID AdminSid = NULL;
62
63 HINF hSysSetupInf = INVALID_HANDLE_VALUE;
64
65 /* FUNCTIONS ****************************************************************/
66
67 static VOID
68 DebugPrint(char* fmt,...)
69 {
70 char buffer[512];
71 va_list ap;
72
73 va_start(ap, fmt);
74 vsprintf(buffer, fmt, ap);
75 va_end(ap);
76
77 LogItem(SYSSETUP_SEVERITY_FATAL_ERROR, L"Failed");
78
79 strcat(buffer, "\nRebooting now!");
80 MessageBoxA(NULL,
81 buffer,
82 "ReactOS Setup",
83 MB_OK);
84 }
85
86
87 HRESULT CreateShellLink(LPCTSTR linkPath, LPCTSTR cmd, LPCTSTR arg, LPCTSTR dir, LPCTSTR iconPath, int icon_nr, LPCTSTR comment)
88 {
89 IShellLink* psl;
90 IPersistFile* ppf;
91 #ifndef _UNICODE
92 WCHAR buffer[MAX_PATH];
93 #endif /* _UNICODE */
94
95 HRESULT hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (LPVOID*)&psl);
96
97 if (SUCCEEDED(hr))
98 {
99 hr = psl->lpVtbl->SetPath(psl, cmd);
100
101 if (arg)
102 {
103 hr = psl->lpVtbl->SetArguments(psl, arg);
104 }
105
106 if (dir)
107 {
108 hr = psl->lpVtbl->SetWorkingDirectory(psl, dir);
109 }
110
111 if (iconPath)
112 {
113 hr = psl->lpVtbl->SetIconLocation(psl, iconPath, icon_nr);
114 }
115
116 if (comment)
117 {
118 hr = psl->lpVtbl->SetDescription(psl, comment);
119 }
120
121 hr = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
122
123 if (SUCCEEDED(hr))
124 {
125 #ifdef _UNICODE
126 hr = ppf->lpVtbl->Save(ppf, linkPath, TRUE);
127 #else /* _UNICODE */
128 MultiByteToWideChar(CP_ACP, 0, linkPath, -1, buffer, MAX_PATH);
129
130 hr = ppf->lpVtbl->Save(ppf, buffer, TRUE);
131 #endif /* _UNICODE */
132
133 ppf->lpVtbl->Release(ppf);
134 }
135
136 psl->lpVtbl->Release(psl);
137 }
138
139 return hr;
140 }
141
142
143 static BOOL
144 CreateShortcut(int csidl, LPCTSTR folder, UINT nIdName, LPCTSTR command, UINT nIdTitle, BOOL bCheckExistence)
145 {
146 TCHAR path[MAX_PATH];
147 TCHAR exeName[MAX_PATH];
148 TCHAR title[256];
149 TCHAR name[256];
150 LPTSTR p = path;
151 TCHAR szWorkingDir[MAX_PATH];
152 LPTSTR lpWorkingDir = NULL;
153 LPTSTR lpFilePart;
154 DWORD dwLen;
155
156 if (ExpandEnvironmentStrings(command,
157 path,
158 sizeof(path) / sizeof(path[0])) == 0)
159 {
160 _tcscpy(path,
161 command);
162 }
163
164 if (bCheckExistence)
165 {
166 if ((_taccess(path, 0 )) == -1)
167 /* Expected error, don't return FALSE */
168 return TRUE;
169 }
170
171 dwLen = GetFullPathName(path,
172 sizeof(szWorkingDir) / sizeof(szWorkingDir[0]),
173 szWorkingDir,
174 &lpFilePart);
175 if (dwLen != 0 && dwLen <= sizeof(szWorkingDir) / sizeof(szWorkingDir[0]))
176 {
177 /* Save the file name */
178 _tcscpy(exeName, lpFilePart);
179
180 if (lpFilePart != NULL)
181 {
182 /* We're only interested in the path. Cut the file name off.
183 Also remove the trailing backslash unless the working directory
184 is only going to be a drive, ie. C:\ */
185 *(lpFilePart--) = _T('\0');
186 if (!(lpFilePart - szWorkingDir == 2 && szWorkingDir[1] == _T(':') &&
187 szWorkingDir[2] == _T('\\')))
188 {
189 *lpFilePart = _T('\0');
190 }
191 }
192
193 lpWorkingDir = szWorkingDir;
194 }
195
196
197 if (!SHGetSpecialFolderPath(0, path, csidl, TRUE))
198 return FALSE;
199
200 if (folder)
201 {
202 p = PathAddBackslash(p);
203 _tcscpy(p, folder);
204 }
205
206 p = PathAddBackslash(p);
207
208 if (!LoadString(hDllInstance, nIdName, name, sizeof(name)/sizeof(name[0])))
209 return FALSE;
210 _tcscpy(p, name);
211
212 if (!LoadString(hDllInstance, nIdTitle, title, sizeof(title)/sizeof(title[0])))
213 return FALSE;
214
215 // FIXME: we should pass 'command' straight in here, but shell32 doesn't expand it
216 return SUCCEEDED(CreateShellLink(path, exeName, _T(""), lpWorkingDir, NULL, 0, title));
217 }
218
219
220 static BOOL
221 CreateShortcutFolder(int csidl, UINT nID, LPTSTR name, int nameLen)
222 {
223 TCHAR path[MAX_PATH];
224 LPTSTR p;
225
226 if (!SHGetSpecialFolderPath(0, path, csidl, TRUE))
227 return FALSE;
228
229 if (!LoadString(hDllInstance, nID, name, nameLen))
230 return FALSE;
231
232 p = PathAddBackslash(path);
233 _tcscpy(p, name);
234
235 return CreateDirectory(path, NULL) || GetLastError()==ERROR_ALREADY_EXISTS;
236 }
237
238
239 static BOOL
240 CreateRandomSid(
241 OUT PSID *Sid)
242 {
243 SID_IDENTIFIER_AUTHORITY SystemAuthority = {SECURITY_NT_AUTHORITY};
244 LARGE_INTEGER SystemTime;
245 PULONG Seed;
246 NTSTATUS Status;
247
248 NtQuerySystemTime(&SystemTime);
249 Seed = &SystemTime.u.LowPart;
250
251 Status = RtlAllocateAndInitializeSid(
252 &SystemAuthority,
253 4,
254 SECURITY_NT_NON_UNIQUE,
255 RtlUniform(Seed),
256 RtlUniform(Seed),
257 RtlUniform(Seed),
258 SECURITY_NULL_RID,
259 SECURITY_NULL_RID,
260 SECURITY_NULL_RID,
261 SECURITY_NULL_RID,
262 Sid);
263 return NT_SUCCESS(Status);
264 }
265
266
267 static VOID
268 AppendRidToSid(
269 OUT PSID *Dst,
270 IN PSID Src,
271 IN ULONG NewRid)
272 {
273 ULONG Rid[8] = {0, 0, 0, 0, 0, 0, 0, 0};
274 UCHAR RidCount;
275 ULONG i;
276
277 RidCount = *RtlSubAuthorityCountSid (Src);
278
279 for (i = 0; i < RidCount; i++)
280 Rid[i] = *RtlSubAuthoritySid (Src, i);
281
282 if (RidCount < 8)
283 {
284 Rid[RidCount] = NewRid;
285 RidCount++;
286 }
287
288 RtlAllocateAndInitializeSid(
289 RtlIdentifierAuthoritySid(Src),
290 RidCount,
291 Rid[0],
292 Rid[1],
293 Rid[2],
294 Rid[3],
295 Rid[4],
296 Rid[5],
297 Rid[6],
298 Rid[7],
299 Dst);
300 }
301
302
303 static VOID
304 CreateTempDir(
305 IN LPCWSTR VarName)
306 {
307 WCHAR szTempDir[MAX_PATH];
308 WCHAR szBuffer[MAX_PATH];
309 DWORD dwLength;
310 HKEY hKey;
311
312 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
313 L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
314 0,
315 KEY_QUERY_VALUE,
316 &hKey))
317 {
318 DebugPrint("Error: %lu\n", GetLastError());
319 return;
320 }
321
322 /* Get temp dir */
323 dwLength = MAX_PATH * sizeof(WCHAR);
324 if (RegQueryValueExW(hKey,
325 VarName,
326 NULL,
327 NULL,
328 (LPBYTE)szBuffer,
329 &dwLength))
330 {
331 DebugPrint("Error: %lu\n", GetLastError());
332 RegCloseKey(hKey);
333 return;
334 }
335
336 /* Expand it */
337 if (!ExpandEnvironmentStringsW(szBuffer,
338 szTempDir,
339 MAX_PATH))
340 {
341 DebugPrint("Error: %lu\n", GetLastError());
342 RegCloseKey(hKey);
343 return;
344 }
345
346 /* Create profiles directory */
347 if (!CreateDirectoryW(szTempDir, NULL))
348 {
349 if (GetLastError() != ERROR_ALREADY_EXISTS)
350 {
351 DebugPrint("Error: %lu\n", GetLastError());
352 RegCloseKey(hKey);
353 return;
354 }
355 }
356
357 RegCloseKey(hKey);
358 }
359
360
361 BOOL
362 InstallSysSetupInfDevices(VOID)
363 {
364 INFCONTEXT InfContext;
365 WCHAR LineBuffer[256];
366 DWORD LineLength;
367
368 if (!SetupFindFirstLineW(hSysSetupInf,
369 L"DeviceInfsToInstall",
370 NULL,
371 &InfContext))
372 {
373 return FALSE;
374 }
375
376 do
377 {
378 if (!SetupGetStringFieldW(&InfContext,
379 0,
380 LineBuffer,
381 sizeof(LineBuffer)/sizeof(LineBuffer[0]),
382 &LineLength))
383 {
384 return FALSE;
385 }
386
387 if (!SetupDiInstallClassW(NULL, LineBuffer, DI_QUIETINSTALL, NULL))
388 {
389 return FALSE;
390 }
391 }
392 while (SetupFindNextLine(&InfContext, &InfContext));
393
394 return TRUE;
395 }
396 BOOL
397 InstallSysSetupInfComponents(VOID)
398 {
399 INFCONTEXT InfContext;
400 WCHAR NameBuffer[256];
401 WCHAR SectionBuffer[256];
402 HINF hComponentInf = INVALID_HANDLE_VALUE;
403
404 if (!SetupFindFirstLineW(hSysSetupInf,
405 L"Infs.Always",
406 NULL,
407 &InfContext))
408 {
409 DPRINT("No Inf.Always section found\n");
410 }
411 else
412 {
413 do
414 {
415 if (!SetupGetStringFieldW(&InfContext,
416 1, // Get the component name
417 NameBuffer,
418 sizeof(NameBuffer)/sizeof(NameBuffer[0]),
419 NULL))
420 {
421 DebugPrint("Error while trying to get component name \n");
422 return FALSE;
423 }
424
425 if (!SetupGetStringFieldW(&InfContext,
426 2, // Get the component install section
427 SectionBuffer,
428 sizeof(SectionBuffer)/sizeof(SectionBuffer[0]),
429 NULL))
430 {
431 DebugPrint("Error while trying to get component install section \n");
432 return FALSE;
433 }
434
435 DPRINT("Trying to execute install section '%S' from '%S' \n", SectionBuffer , NameBuffer);
436
437 hComponentInf = SetupOpenInfFileW(NameBuffer,
438 NULL,
439 INF_STYLE_WIN4,
440 NULL);
441
442 if (hComponentInf == INVALID_HANDLE_VALUE)
443 {
444 DebugPrint("SetupOpenInfFileW() failed to open '%S' (Error: %lu)\n", NameBuffer ,GetLastError());
445 return FALSE;
446 }
447
448 if (!SetupInstallFromInfSectionW(NULL,
449 hComponentInf,
450 SectionBuffer,
451 SPINST_ALL,
452 NULL,
453 NULL,
454 SP_COPY_NEWER,
455 SetupDefaultQueueCallbackW,
456 NULL,
457 NULL,
458 NULL))
459 {
460 DebugPrint("Error while trying to install : %S (Error: %lu)\n", NameBuffer, GetLastError());
461 SetupCloseInfFile(hComponentInf);
462 return FALSE;
463 }
464
465 SetupCloseInfFile(hComponentInf);
466 }
467 while (SetupFindNextLine(&InfContext, &InfContext));
468 }
469
470 return TRUE;
471 }
472
473 static BOOL
474 EnableUserModePnpManager(VOID)
475 {
476 SC_HANDLE hSCManager = NULL;
477 SC_HANDLE hService = NULL;
478 BOOL ret = FALSE;
479
480 hSCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
481 if (hSCManager == NULL)
482 {
483 DPRINT1("Unable to open the service control manager.\n");
484 DPRINT1("Last Error %d\n", GetLastError());
485 goto cleanup;
486 }
487
488 hService = OpenServiceW(hSCManager,
489 L"PlugPlay",
490 SERVICE_CHANGE_CONFIG | SERVICE_START);
491 if (hService == NULL)
492 {
493 DPRINT1("Unable to open PlugPlay service\n");
494 goto cleanup;
495 }
496
497 ret = ChangeServiceConfigW(hService,
498 SERVICE_NO_CHANGE,
499 SERVICE_AUTO_START,
500 SERVICE_NO_CHANGE,
501 NULL, NULL, NULL,
502 NULL, NULL, NULL, NULL);
503 if (!ret)
504 {
505 DPRINT1("Unable to change the service configuration\n");
506 goto cleanup;
507 }
508
509 ret = StartServiceW(hService, 0, NULL);
510 if ((!ret) && (GetLastError() != ERROR_SERVICE_ALREADY_RUNNING))
511 {
512 DPRINT1("Unable to start service\n");
513 goto cleanup;
514 }
515
516 ret = TRUE;
517
518 cleanup:
519 if (hSCManager != NULL)
520 CloseServiceHandle(hSCManager);
521 if (hService != NULL)
522 CloseServiceHandle(hService);
523 return ret;
524 }
525
526
527 static INT_PTR CALLBACK
528 StatusMessageWindowProc(
529 IN HWND hwndDlg,
530 IN UINT uMsg,
531 IN WPARAM wParam,
532 IN LPARAM lParam)
533 {
534 UNREFERENCED_PARAMETER(wParam);
535
536 switch (uMsg)
537 {
538 case WM_INITDIALOG:
539 {
540 WCHAR szMsg[256];
541
542 if (!LoadStringW(hDllInstance, IDS_STATUS_INSTALL_DEV, szMsg, sizeof(szMsg)/sizeof(szMsg[0])))
543 return FALSE;
544 SetDlgItemTextW(hwndDlg, IDC_STATUSLABEL, szMsg);
545 return TRUE;
546 }
547 }
548 return FALSE;
549 }
550
551
552 static DWORD WINAPI
553 ShowStatusMessageThread(
554 IN LPVOID lpParameter)
555 {
556 HWND *phWnd = (HWND *)lpParameter;
557 HWND hWnd;
558 MSG Msg;
559
560 hWnd = CreateDialogParam(
561 hDllInstance,
562 MAKEINTRESOURCE(IDD_STATUSWINDOW_DLG),
563 GetDesktopWindow(),
564 StatusMessageWindowProc,
565 (LPARAM)NULL);
566 if (!hWnd)
567 return 0;
568 *phWnd = hWnd;
569
570 ShowWindow(hWnd, SW_SHOW);
571
572 /* Message loop for the Status window */
573 while (GetMessage(&Msg, NULL, 0, 0))
574 {
575 TranslateMessage(&Msg);
576 DispatchMessage(&Msg);
577 }
578
579 return 0;
580 }
581
582 static LONG
583 ReadRegSzKey(
584 IN HKEY hKey,
585 IN LPCWSTR pszKey,
586 OUT LPWSTR* pValue)
587 {
588 LONG rc;
589 DWORD dwType;
590 DWORD cbData = 0;
591 LPWSTR Value;
592
593 if (!pValue)
594 return ERROR_INVALID_PARAMETER;
595
596 *pValue = NULL;
597 rc = RegQueryValueExW(hKey, pszKey, NULL, &dwType, NULL, &cbData);
598 if (rc != ERROR_SUCCESS)
599 return rc;
600 if (dwType != REG_SZ)
601 return ERROR_FILE_NOT_FOUND;
602 Value = HeapAlloc(GetProcessHeap(), 0, cbData + sizeof(WCHAR));
603 if (!Value)
604 return ERROR_NOT_ENOUGH_MEMORY;
605 rc = RegQueryValueExW(hKey, pszKey, NULL, NULL, (LPBYTE)Value, &cbData);
606 if (rc != ERROR_SUCCESS)
607 {
608 HeapFree(GetProcessHeap(), 0, Value);
609 return rc;
610 }
611 /* NULL-terminate the string */
612 Value[cbData / sizeof(WCHAR)] = '\0';
613
614 *pValue = Value;
615 return ERROR_SUCCESS;
616 }
617
618 static BOOL
619 IsConsoleBoot(VOID)
620 {
621 HKEY ControlKey = NULL;
622 LPWSTR SystemStartOptions = NULL;
623 LPWSTR CurrentOption, NextOption; /* Pointers into SystemStartOptions */
624 BOOL ConsoleBoot = FALSE;
625 LONG rc;
626
627 rc = RegOpenKeyExW(
628 HKEY_LOCAL_MACHINE,
629 L"SYSTEM\\CurrentControlSet\\Control",
630 0,
631 KEY_QUERY_VALUE,
632 &ControlKey);
633
634 rc = ReadRegSzKey(ControlKey, L"SystemStartOptions", &SystemStartOptions);
635 if (rc != ERROR_SUCCESS)
636 goto cleanup;
637
638 /* Check for CMDCONS in SystemStartOptions */
639 CurrentOption = SystemStartOptions;
640 while (CurrentOption)
641 {
642 NextOption = wcschr(CurrentOption, L' ');
643 if (NextOption)
644 *NextOption = L'\0';
645 if (wcsicmp(CurrentOption, L"CONSOLE") == 0)
646 {
647 DPRINT("Found %S. Switching to console boot\n", CurrentOption);
648 ConsoleBoot = TRUE;
649 goto cleanup;
650 }
651 CurrentOption = NextOption ? NextOption + 1 : NULL;
652 }
653
654 cleanup:
655 if (ControlKey != NULL)
656 RegCloseKey(ControlKey);
657 HeapFree(GetProcessHeap(), 0, SystemStartOptions);
658 return ConsoleBoot;
659 }
660
661 static BOOL
662 CommonInstall(VOID)
663 {
664 HWND hWnd = NULL;
665
666 hSysSetupInf = SetupOpenInfFileW(
667 L"syssetup.inf",
668 NULL,
669 INF_STYLE_WIN4,
670 NULL);
671 if (hSysSetupInf == INVALID_HANDLE_VALUE)
672 {
673 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
674 return FALSE;
675 }
676
677 if (!InstallSysSetupInfDevices())
678 {
679 DebugPrint("InstallSysSetupInfDevices() failed!\n");
680 SetupCloseInfFile(hSysSetupInf);
681 return FALSE;
682 }
683
684 if(!InstallSysSetupInfComponents())
685 {
686 DebugPrint("InstallSysSetupInfComponents() failed!\n");
687 SetupCloseInfFile(hSysSetupInf);
688 return FALSE;
689 }
690
691 if (!IsConsoleBoot())
692 {
693 CreateThread(
694 NULL,
695 0,
696 ShowStatusMessageThread,
697 (LPVOID)&hWnd,
698 0,
699 NULL);
700 }
701
702 if (!EnableUserModePnpManager())
703 {
704 DebugPrint("EnableUserModePnpManager() failed!\n");
705 SetupCloseInfFile(hSysSetupInf);
706 EndDialog(hWnd, 0);
707 return FALSE;
708 }
709
710 if (CMP_WaitNoPendingInstallEvents(INFINITE) != WAIT_OBJECT_0)
711 {
712 DebugPrint("CMP_WaitNoPendingInstallEvents() failed!\n");
713 SetupCloseInfFile(hSysSetupInf);
714 EndDialog(hWnd, 0);
715 return FALSE;
716 }
717
718 EndDialog(hWnd, 0);
719 return TRUE;
720 }
721
722 DWORD WINAPI
723 InstallLiveCD(IN HINSTANCE hInstance)
724 {
725 STARTUPINFOW StartupInfo;
726 PROCESS_INFORMATION ProcessInformation;
727 BOOL res;
728
729 if (!CommonInstall())
730 goto cleanup;
731 SetupCloseInfFile(hSysSetupInf);
732
733 /* Run the shell */
734 StartupInfo.cb = sizeof(STARTUPINFOW);
735 StartupInfo.lpReserved = NULL;
736 StartupInfo.lpDesktop = NULL;
737 StartupInfo.lpTitle = NULL;
738 StartupInfo.dwFlags = 0;
739 StartupInfo.cbReserved2 = 0;
740 StartupInfo.lpReserved2 = 0;
741 res = CreateProcessW(
742 L"userinit.exe",
743 NULL,
744 NULL,
745 NULL,
746 FALSE,
747 0,
748 NULL,
749 NULL,
750 &StartupInfo,
751 &ProcessInformation);
752 if (!res)
753 goto cleanup;
754
755 return 0;
756
757 cleanup:
758 MessageBoxW(
759 NULL,
760 L"You can shutdown your computer, or press ENTER to reboot",
761 L"ReactOS LiveCD",
762 MB_OK);
763 return 0;
764 }
765
766
767 static BOOL
768 CreateShortcuts(VOID)
769 {
770 TCHAR szFolder[256];
771
772 CoInitialize(NULL);
773
774 /* Create desktop shortcuts */
775 CreateShortcut(CSIDL_DESKTOP, NULL, IDS_SHORT_CMD, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD, TRUE);
776 CreateShortcut(CSIDL_DESKTOP, NULL, IDS_SHORT_DOWNLOADER, _T("%SystemRoot%\\system32\\rapps.exe"), IDS_CMT_DOWNLOADER, TRUE);
777
778 /* Create program startmenu shortcuts */
779 CreateShortcut(CSIDL_PROGRAMS, NULL, IDS_SHORT_EXPLORER, _T("%SystemRoot%\\explorer.exe"), IDS_CMT_EXPLORER, TRUE);
780 CreateShortcut(CSIDL_PROGRAMS, NULL, IDS_SHORT_DOWNLOADER, _T("%SystemRoot%\\system32\\rapps.exe"), IDS_CMT_DOWNLOADER, TRUE);
781
782 /* Create administrative tools startmenu shortcuts */
783 CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_SERVICE, _T("%SystemRoot%\\system32\\servman.exe"), IDS_CMT_SERVMAN, TRUE);
784 CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_DEVICE, _T("%SystemRoot%\\system32\\devmgmt.exe"), IDS_CMT_DEVMGMT, TRUE);
785 CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_EVENTVIEW, _T("%SystemRoot%\\system32\\eventvwr.exe"), IDS_CMT_EVENTVIEW, TRUE);
786 CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_MSCONFIG, _T("%SystemRoot%\\system32\\msconfig.exe"), IDS_CMT_MSCONFIG, TRUE);
787
788 /* Create and fill Accessories subfolder */
789 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_ACCESSORIES, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
790 {
791 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_CALC, _T("%SystemRoot%\\system32\\calc.exe"), IDS_CMT_CALC, TRUE);
792 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_CMD, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD, TRUE);
793 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_NOTEPAD, _T("%SystemRoot%\\system32\\notepad.exe"), IDS_CMT_NOTEPAD, TRUE);
794 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_RDESKTOP, _T("%SystemRoot%\\system32\\mstsc.exe"), IDS_CMT_RDESKTOP, TRUE);
795 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SNAP, _T("%SystemRoot%\\system32\\screenshot.exe"), IDS_CMT_SCREENSHOT, TRUE);
796 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_WORDPAD, _T("%SystemRoot%\\system32\\wordpad.exe"), IDS_CMT_WORDPAD, TRUE);
797 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_PAINT, _T("%SystemRoot%\\system32\\paint.exe"), IDS_CMT_PAINT, TRUE);
798 }
799
800 /* Create System Tools subfolder and fill if the exe is available */
801 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_SYS_TOOLS, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
802 {
803 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_CHARMAP, _T("%SystemRoot%\\system32\\charmap.exe"), IDS_CMT_CHARMAP, TRUE);
804 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_KBSWITCH, _T("%SystemRoot%\\system32\\kbswitch.exe"), IDS_CMT_KBSWITCH, TRUE);
805 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_REGEDIT, _T("%SystemRoot%\\regedit.exe"), IDS_CMT_REGEDIT, TRUE);
806 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_DXDIAG, _T("%SystemRoot%\\system32\\dxdiag.exe"), IDS_CMT_DXDIAG, TRUE);
807 }
808
809 /* Create Accessibility subfolder and fill if the exe is available */
810 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_SYS_ACCESSIBILITY, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
811 {
812 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_MAGNIFY, _T("%SystemRoot%\\system32\\magnify.exe"), IDS_CMT_MAGNIFY, TRUE);
813 }
814
815 /* Create Entertainment subfolder and fill if the exe is available */
816 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_SYS_ENTERTAINMENT, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
817 {
818 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_MPLAY32, _T("%SystemRoot%\\system32\\mplay32.exe"), IDS_CMT_MPLAY32, TRUE);
819 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SNDVOL32, _T("%SystemRoot%\\system32\\sndvol32.exe"), IDS_CMT_SNDVOL32, TRUE);
820 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SNDREC32, _T("%SystemRoot%\\system32\\sndrec32.exe"), IDS_CMT_SNDREC32, TRUE);
821 }
822
823 /* Create Games subfolder and fill if the exe is available */
824 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_GAMES, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
825 {
826 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SOLITAIRE, _T("%SystemRoot%\\system32\\sol.exe"), IDS_CMT_SOLITAIRE, TRUE);
827 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_WINEMINE, _T("%SystemRoot%\\system32\\winmine.exe"), IDS_CMT_WINEMINE, TRUE);
828 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SPIDER, _T("%SystemRoot%\\system32\\spider.exe"), IDS_CMT_SPIDER, TRUE);
829 }
830
831 CoUninitialize();
832
833 return TRUE;
834 }
835
836 static BOOL
837 SetSetupType(DWORD dwSetupType)
838 {
839 DWORD dwError;
840 HKEY hKey;
841
842 dwError = RegOpenKeyExW(
843 HKEY_LOCAL_MACHINE,
844 L"SYSTEM\\Setup",
845 0,
846 KEY_SET_VALUE,
847 &hKey);
848 if (dwError != ERROR_SUCCESS)
849 return FALSE;
850
851 dwError = RegSetValueExW(
852 hKey,
853 L"SetupType",
854 0,
855 REG_DWORD,
856 (LPBYTE)&dwSetupType,
857 sizeof(DWORD));
858 RegCloseKey(hKey);
859 if (dwError != ERROR_SUCCESS)
860 return FALSE;
861
862 return TRUE;
863 }
864
865 DWORD WINAPI
866 InstallReactOS(HINSTANCE hInstance)
867 {
868 TCHAR szBuffer[MAX_PATH];
869 DWORD LastError;
870 HANDLE token;
871 TOKEN_PRIVILEGES privs;
872 HKEY hKey;
873
874 InitializeSetupActionLog(FALSE);
875 LogItem(SYSSETUP_SEVERITY_INFORMATION, L"Installing ReactOS");
876
877 if (!InitializeProfiles())
878 {
879 DebugPrint("InitializeProfiles() failed");
880 return 0;
881 }
882
883 if (!CreateShortcuts())
884 {
885 DebugPrint("InitializeProfiles() failed");
886 return 0;
887 }
888
889 /* Initialize the Security Account Manager (SAM) */
890 if (!SamInitializeSAM())
891 {
892 DebugPrint("SamInitializeSAM() failed!");
893 return 0;
894 }
895
896 /* Create the semi-random Domain-SID */
897 if (!CreateRandomSid(&DomainSid))
898 {
899 DebugPrint("Domain-SID creation failed!");
900 return 0;
901 }
902
903 /* Set the Domain SID (aka Computer SID) */
904 if (!SamSetDomainSid(DomainSid))
905 {
906 DebugPrint("SamSetDomainSid() failed!");
907 RtlFreeSid(DomainSid);
908 return 0;
909 }
910
911 /* Append the Admin-RID */
912 AppendRidToSid(&AdminSid, DomainSid, DOMAIN_USER_RID_ADMIN);
913
914 CreateTempDir(L"TEMP");
915 CreateTempDir(L"TMP");
916
917 if (GetWindowsDirectory(szBuffer, sizeof(szBuffer) / sizeof(TCHAR)))
918 {
919 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
920 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
921 0,
922 KEY_WRITE,
923 &hKey) == ERROR_SUCCESS)
924 {
925 RegSetValueExW(hKey,
926 L"PathName",
927 0,
928 REG_SZ,
929 (LPBYTE)szBuffer,
930 (wcslen(szBuffer) + 1) * sizeof(WCHAR));
931
932 RegSetValueExW(hKey,
933 L"SystemRoot",
934 0,
935 REG_SZ,
936 (LPBYTE)szBuffer,
937 (wcslen(szBuffer) + 1) * sizeof(WCHAR));
938
939 RegCloseKey(hKey);
940 }
941
942 PathAddBackslash(szBuffer);
943 _tcscat(szBuffer, _T("system"));
944 CreateDirectory(szBuffer, NULL);
945 }
946
947 if (!CommonInstall())
948 return 0;
949
950 InstallWizard();
951
952 /* Create the Administrator account */
953 if (!SamCreateUser(L"Administrator", L"", AdminSid))
954 {
955 /* Check what the error was.
956 * If the Admin Account already exists, then it means Setup
957 * wasn't allowed to finish properly. Instead of rebooting
958 * and not completing it, let it restart instead
959 */
960 LastError = GetLastError();
961 if (LastError != ERROR_USER_EXISTS)
962 {
963 DebugPrint("SamCreateUser() failed!");
964 RtlFreeSid(AdminSid);
965 RtlFreeSid(DomainSid);
966 return 0;
967 }
968 }
969
970 RtlFreeSid(AdminSid);
971 RtlFreeSid(DomainSid);
972
973 /* ROS HACK, as long as NtUnloadKey is not implemented */
974 {
975 NTSTATUS Status = NtUnloadKey(NULL);
976 if (Status == STATUS_NOT_IMPLEMENTED)
977 {
978 /* Create the Administrator profile */
979 PROFILEINFOW ProfileInfo;
980 HANDLE hToken;
981 BOOL ret;
982 #define LOGON32_LOGON_NETWORK 3
983 ret = LogonUserW(L"Administrator", L"", L"", LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &hToken);
984 if (!ret)
985 {
986 DebugPrint("LogonUserW() failed!");
987 return 0;
988 }
989 ZeroMemory(&ProfileInfo, sizeof(PROFILEINFOW));
990 ProfileInfo.dwSize = sizeof(PROFILEINFOW);
991 ProfileInfo.lpUserName = L"Administrator";
992 ProfileInfo.dwFlags = PI_NOUI;
993 LoadUserProfileW(hToken, &ProfileInfo);
994 CloseHandle(hToken);
995 }
996 else
997 {
998 DPRINT1("ROS HACK not needed anymore. Please remove it\n");
999 }
1000 }
1001 /* END OF ROS HACK */
1002
1003 SetupCloseInfFile(hSysSetupInf);
1004 SetSetupType(0);
1005
1006 LogItem(SYSSETUP_SEVERITY_INFORMATION, L"Installing ReactOS done");
1007 TerminateSetupActionLog();
1008
1009 /* Get shutdown privilege */
1010 if (! OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
1011 {
1012 DebugPrint("OpenProcessToken() failed!");
1013 return 0;
1014 }
1015 if (!LookupPrivilegeValue(
1016 NULL,
1017 SE_SHUTDOWN_NAME,
1018 &privs.Privileges[0].Luid))
1019 {
1020 DebugPrint("LookupPrivilegeValue() failed!");
1021 return 0;
1022 }
1023 privs.PrivilegeCount = 1;
1024 privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1025 if (AdjustTokenPrivileges(
1026 token,
1027 FALSE,
1028 &privs,
1029 0,
1030 (PTOKEN_PRIVILEGES)NULL,
1031 NULL) == 0)
1032 {
1033 DebugPrint("AdjustTokenPrivileges() failed!");
1034 return 0;
1035 }
1036
1037 ExitWindowsEx(EWX_REBOOT, 0);
1038 return 0;
1039 }
1040
1041
1042 /*
1043 * @unimplemented
1044 */
1045 DWORD WINAPI
1046 SetupChangeFontSize(
1047 IN HANDLE hWnd,
1048 IN LPCWSTR lpszFontSize)
1049 {
1050 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1051 return FALSE;
1052 }
1053
1054 /*
1055 * @unimplemented
1056 */
1057 DWORD WINAPI
1058 SetupChangeLocaleEx(HWND hWnd,
1059 LCID Lcid,
1060 LPCWSTR lpSrcRootPath,
1061 char Unknown,
1062 DWORD dwUnused1,
1063 DWORD dwUnused2)
1064 {
1065 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1066 return FALSE;
1067 }
1068
1069 /*
1070 * @implemented
1071 */
1072 DWORD WINAPI
1073 SetupChangeLocale(HWND hWnd, LCID Lcid)
1074 {
1075 return SetupChangeLocaleEx(hWnd, Lcid, NULL, 0, 0, 0);
1076 }