Bug 4061: Italian resources updates by Paolo Devoti
[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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS system libraries
22 * PURPOSE: System setup
23 * FILE: lib/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 = OpenSCManager(NULL, NULL, 0);
481 if (hSCManager == NULL)
482 goto cleanup;
483
484 hService = OpenServiceW(hSCManager, L"PlugPlay", SERVICE_CHANGE_CONFIG | SERVICE_START);
485 if (hService == NULL)
486 goto cleanup;
487
488 ret = ChangeServiceConfigW(
489 hService,
490 SERVICE_NO_CHANGE, SERVICE_AUTO_START, SERVICE_NO_CHANGE,
491 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
492 if (!ret)
493 goto cleanup;
494
495 ret = StartServiceW(hService, 0, NULL);
496 if (!ret)
497 goto cleanup;
498
499 ret = TRUE;
500
501 cleanup:
502 if (hSCManager != NULL)
503 CloseServiceHandle(hSCManager);
504 if (hService != NULL)
505 CloseServiceHandle(hService);
506 return ret;
507 }
508
509
510 static BOOL CALLBACK
511 StatusMessageWindowProc(
512 IN HWND hwndDlg,
513 IN UINT uMsg,
514 IN WPARAM wParam,
515 IN LPARAM lParam)
516 {
517 UNREFERENCED_PARAMETER(wParam);
518
519 switch (uMsg)
520 {
521 case WM_INITDIALOG:
522 {
523 WCHAR szMsg[256];
524
525 if (!LoadStringW(hDllInstance, IDS_STATUS_INSTALL_DEV, szMsg, sizeof(szMsg)/sizeof(szMsg[0])))
526 return FALSE;
527 SetDlgItemTextW(hwndDlg, IDC_STATUSLABEL, szMsg);
528 return TRUE;
529 }
530 }
531 return FALSE;
532 }
533
534
535 static DWORD WINAPI
536 ShowStatusMessageThread(
537 IN LPVOID lpParameter)
538 {
539 HWND *phWnd = (HWND *)lpParameter;
540 HWND hWnd;
541 MSG Msg;
542
543 hWnd = CreateDialogParam(
544 hDllInstance,
545 MAKEINTRESOURCE(IDD_STATUSWINDOW_DLG),
546 GetDesktopWindow(),
547 StatusMessageWindowProc,
548 (LPARAM)NULL);
549 if (!hWnd)
550 return 0;
551 *phWnd = hWnd;
552
553 ShowWindow(hWnd, SW_SHOW);
554
555 /* Message loop for the Status window */
556 while (GetMessage(&Msg, NULL, 0, 0))
557 {
558 TranslateMessage(&Msg);
559 DispatchMessage(&Msg);
560 }
561
562 return 0;
563 }
564
565 static LONG
566 ReadRegSzKey(
567 IN HKEY hKey,
568 IN LPCWSTR pszKey,
569 OUT LPWSTR* pValue)
570 {
571 LONG rc;
572 DWORD dwType;
573 DWORD cbData = 0;
574 LPWSTR Value;
575
576 if (!pValue)
577 return ERROR_INVALID_PARAMETER;
578
579 *pValue = NULL;
580 rc = RegQueryValueExW(hKey, pszKey, NULL, &dwType, NULL, &cbData);
581 if (rc != ERROR_SUCCESS)
582 return rc;
583 if (dwType != REG_SZ)
584 return ERROR_FILE_NOT_FOUND;
585 Value = HeapAlloc(GetProcessHeap(), 0, cbData + sizeof(WCHAR));
586 if (!Value)
587 return ERROR_NOT_ENOUGH_MEMORY;
588 rc = RegQueryValueExW(hKey, pszKey, NULL, NULL, (LPBYTE)Value, &cbData);
589 if (rc != ERROR_SUCCESS)
590 {
591 HeapFree(GetProcessHeap(), 0, Value);
592 return rc;
593 }
594 /* NULL-terminate the string */
595 Value[cbData / sizeof(WCHAR)] = '\0';
596
597 *pValue = Value;
598 return ERROR_SUCCESS;
599 }
600
601 static BOOL
602 IsConsoleBoot(VOID)
603 {
604 HKEY ControlKey = NULL;
605 LPWSTR SystemStartOptions = NULL;
606 LPWSTR CurrentOption, NextOption; /* Pointers into SystemStartOptions */
607 BOOL ConsoleBoot = FALSE;
608 LONG rc;
609
610 rc = RegOpenKeyExW(
611 HKEY_LOCAL_MACHINE,
612 L"SYSTEM\\CurrentControlSet\\Control",
613 0,
614 KEY_QUERY_VALUE,
615 &ControlKey);
616
617 rc = ReadRegSzKey(ControlKey, L"SystemStartOptions", &SystemStartOptions);
618 if (rc != ERROR_SUCCESS)
619 goto cleanup;
620
621 /* Check for CMDCONS in SystemStartOptions */
622 CurrentOption = SystemStartOptions;
623 while (CurrentOption)
624 {
625 NextOption = wcschr(CurrentOption, L' ');
626 if (NextOption)
627 *NextOption = L'\0';
628 if (wcsicmp(CurrentOption, L"CONSOLE") == 0)
629 {
630 DPRINT("Found %S. Switching to console boot\n", CurrentOption);
631 ConsoleBoot = TRUE;
632 goto cleanup;
633 }
634 CurrentOption = NextOption ? NextOption + 1 : NULL;
635 }
636
637 cleanup:
638 if (ControlKey != NULL)
639 RegCloseKey(ControlKey);
640 HeapFree(GetProcessHeap(), 0, SystemStartOptions);
641 return ConsoleBoot;
642 }
643
644 static BOOL
645 CommonInstall(VOID)
646 {
647 HWND hWnd = NULL;
648
649 hSysSetupInf = SetupOpenInfFileW(
650 L"syssetup.inf",
651 NULL,
652 INF_STYLE_WIN4,
653 NULL);
654 if (hSysSetupInf == INVALID_HANDLE_VALUE)
655 {
656 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
657 return FALSE;
658 }
659
660 if (!InstallSysSetupInfDevices())
661 {
662 DebugPrint("InstallSysSetupInfDevices() failed!\n");
663 SetupCloseInfFile(hSysSetupInf);
664 return FALSE;
665 }
666
667 if(!InstallSysSetupInfComponents())
668 {
669 DebugPrint("InstallSysSetupInfComponents() failed!\n");
670 SetupCloseInfFile(hSysSetupInf);
671 return FALSE;
672 }
673
674 if (!IsConsoleBoot())
675 {
676 CreateThread(
677 NULL,
678 0,
679 ShowStatusMessageThread,
680 (LPVOID)&hWnd,
681 0,
682 NULL);
683 }
684
685 if (!EnableUserModePnpManager())
686 {
687 DebugPrint("EnableUserModePnpManager() failed!\n");
688 SetupCloseInfFile(hSysSetupInf);
689 EndDialog(hWnd, 0);
690 return FALSE;
691 }
692
693 if (CMP_WaitNoPendingInstallEvents(INFINITE) != WAIT_OBJECT_0)
694 {
695 DebugPrint("CMP_WaitNoPendingInstallEvents() failed!\n");
696 SetupCloseInfFile(hSysSetupInf);
697 EndDialog(hWnd, 0);
698 return FALSE;
699 }
700
701 EndDialog(hWnd, 0);
702 return TRUE;
703 }
704
705 DWORD WINAPI
706 InstallLiveCD(IN HINSTANCE hInstance)
707 {
708 STARTUPINFOW StartupInfo;
709 PROCESS_INFORMATION ProcessInformation;
710 BOOL res;
711
712 if (!CommonInstall())
713 goto cleanup;
714 SetupCloseInfFile(hSysSetupInf);
715
716 /* Run the shell */
717 StartupInfo.cb = sizeof(STARTUPINFOW);
718 StartupInfo.lpReserved = NULL;
719 StartupInfo.lpDesktop = NULL;
720 StartupInfo.lpTitle = NULL;
721 StartupInfo.dwFlags = 0;
722 StartupInfo.cbReserved2 = 0;
723 StartupInfo.lpReserved2 = 0;
724 res = CreateProcessW(
725 L"userinit.exe",
726 NULL,
727 NULL,
728 NULL,
729 FALSE,
730 0,
731 NULL,
732 NULL,
733 &StartupInfo,
734 &ProcessInformation);
735 if (!res)
736 goto cleanup;
737
738 return 0;
739
740 cleanup:
741 MessageBoxW(
742 NULL,
743 L"You can shutdown your computer, or press ENTER to reboot",
744 L"ReactOS LiveCD",
745 MB_OK);
746 return 0;
747 }
748
749
750 static BOOL
751 CreateShortcuts(VOID)
752 {
753 TCHAR szFolder[256];
754
755 CoInitialize(NULL);
756
757 /* Create desktop shortcuts */
758 CreateShortcut(CSIDL_DESKTOP, NULL, IDS_SHORT_CMD, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD, TRUE);
759
760 /* Create program startmenu shortcuts */
761 CreateShortcut(CSIDL_PROGRAMS, NULL, IDS_SHORT_EXPLORER, _T("%SystemRoot%\\explorer.exe"), IDS_CMT_EXPLORER, TRUE);
762 CreateShortcut(CSIDL_PROGRAMS, NULL, IDS_SHORT_DOWNLOADER, _T("%SystemRoot%\\system32\\downloader.exe"), IDS_CMT_DOWNLOADER, TRUE);
763
764 /* Create administrative tools startmenu shortcuts */
765 CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_SERVICE, _T("%SystemRoot%\\system32\\servman.exe"), IDS_CMT_SERVMAN, TRUE);
766 CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_DEVICE, _T("%SystemRoot%\\system32\\devmgmt.exe"), IDS_CMT_DEVMGMT, TRUE);
767 CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_EVENTVIEW, _T("%SystemRoot%\\system32\\eventvwr.exe"), IDS_CMT_EVENTVIEW, TRUE);
768 CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_MSCONFIG, _T("%SystemRoot%\\system32\\msconfig.exe"), IDS_CMT_MSCONFIG, TRUE);
769
770 /* Create and fill Accessories subfolder */
771 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_ACCESSORIES, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
772 {
773 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_CALC, _T("%SystemRoot%\\system32\\calc.exe"), IDS_CMT_CALC, TRUE);
774 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_CMD, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD, TRUE);
775 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_NOTEPAD, _T("%SystemRoot%\\system32\\notepad.exe"), IDS_CMT_NOTEPAD, TRUE);
776 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_WORDPAD, _T("%SystemRoot%\\system32\\wordpad.exe"), IDS_CMT_WORDPAD, TRUE);
777 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SNAP, _T("%SystemRoot%\\system32\\screenshot.exe"), IDS_CMT_SCREENSHOT, TRUE);
778 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_RDESKTOP, _T("%SystemRoot%\\system32\\mstsc.exe"), IDS_CMT_RDESKTOP, TRUE);
779 }
780
781 /* Create System Tools subfolder and fill if the exe is available */
782 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_SYS_TOOLS, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
783 {
784 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_CHARMAP, _T("%SystemRoot%\\system32\\charmap.exe"), IDS_CMT_CHARMAP, TRUE);
785 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_KBSWITCH, _T("%SystemRoot%\\system32\\kbswitch.exe"), IDS_CMT_KBSWITCH, TRUE);
786 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_REGEDIT, _T("%SystemRoot%\\regedit.exe"), IDS_CMT_REGEDIT, TRUE);
787 }
788
789 /* Create Accessibility subfolder and fill if the exe is available */
790 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_SYS_ACCESSIBILITY, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
791 {
792 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_MAGNIFY, _T("%SystemRoot%\\system32\\magnify.exe"), IDS_CMT_MAGNIFY, TRUE);
793 }
794
795 /* Create Games subfolder and fill if the exe is available */
796 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_GAMES, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
797 {
798 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SOLITAIRE, _T("%SystemRoot%\\system32\\sol.exe"), IDS_CMT_SOLITAIRE, TRUE);
799 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_WINEMINE, _T("%SystemRoot%\\system32\\winemine.exe"), IDS_CMT_WINEMINE, TRUE);
800 }
801
802 CoUninitialize();
803
804 return TRUE;
805 }
806
807 static BOOL
808 SetSetupType(DWORD dwSetupType)
809 {
810 DWORD dwError;
811 HKEY hKey;
812
813 dwError = RegOpenKeyExW(
814 HKEY_LOCAL_MACHINE,
815 L"SYSTEM\\Setup",
816 0,
817 KEY_SET_VALUE,
818 &hKey);
819 if (dwError != ERROR_SUCCESS)
820 return FALSE;
821
822 dwError = RegSetValueExW(
823 hKey,
824 L"SetupType",
825 0,
826 REG_DWORD,
827 (LPBYTE)&dwSetupType,
828 sizeof(DWORD));
829 RegCloseKey(hKey);
830 if (dwError != ERROR_SUCCESS)
831 return FALSE;
832
833 return TRUE;
834 }
835
836 DWORD WINAPI
837 InstallReactOS(HINSTANCE hInstance)
838 {
839 TCHAR szBuffer[MAX_PATH];
840 DWORD LastError;
841 HANDLE token;
842 TOKEN_PRIVILEGES privs;
843
844 InitializeSetupActionLog(FALSE);
845 LogItem(SYSSETUP_SEVERITY_INFORMATION, L"Installing ReactOS");
846
847 if (!InitializeProfiles())
848 {
849 DebugPrint("InitializeProfiles() failed");
850 return 0;
851 }
852
853 if (!CreateShortcuts())
854 {
855 DebugPrint("InitializeProfiles() failed");
856 return 0;
857 }
858
859 /* Initialize the Security Account Manager (SAM) */
860 if (!SamInitializeSAM())
861 {
862 DebugPrint("SamInitializeSAM() failed!");
863 return 0;
864 }
865
866 /* Create the semi-random Domain-SID */
867 if (!CreateRandomSid(&DomainSid))
868 {
869 DebugPrint("Domain-SID creation failed!");
870 return 0;
871 }
872
873 /* Set the Domain SID (aka Computer SID) */
874 if (!SamSetDomainSid(DomainSid))
875 {
876 DebugPrint("SamSetDomainSid() failed!");
877 RtlFreeSid(DomainSid);
878 return 0;
879 }
880
881 /* Append the Admin-RID */
882 AppendRidToSid(&AdminSid, DomainSid, DOMAIN_USER_RID_ADMIN);
883
884 /* Create the Administrator account */
885 if (!SamCreateUser(L"Administrator", L"", AdminSid))
886 {
887 /* Check what the error was.
888 * If the Admin Account already exists, then it means Setup
889 * wasn't allowed to finish properly. Instead of rebooting
890 * and not completing it, let it restart instead
891 */
892 LastError = GetLastError();
893 if (LastError != ERROR_USER_EXISTS)
894 {
895 DebugPrint("SamCreateUser() failed!");
896 RtlFreeSid(AdminSid);
897 RtlFreeSid(DomainSid);
898 return 0;
899 }
900 }
901
902 RtlFreeSid(AdminSid);
903 RtlFreeSid(DomainSid);
904
905 /* ROS HACK, as long as NtUnloadKey is not implemented */
906 {
907 NTSTATUS Status = NtUnloadKey(NULL);
908 if (Status == STATUS_NOT_IMPLEMENTED)
909 {
910 /* Create the Administrator profile */
911 PROFILEINFOW ProfileInfo;
912 HANDLE hToken;
913 BOOL ret;
914 #define LOGON32_LOGON_NETWORK 3
915 ret = LogonUserW(L"Administrator", L"", L"", LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &hToken);
916 if (!ret)
917 {
918 DebugPrint("LogonUserW() failed!");
919 return 0;
920 }
921 ZeroMemory(&ProfileInfo, sizeof(PROFILEINFOW));
922 ProfileInfo.dwSize = sizeof(PROFILEINFOW);
923 ProfileInfo.lpUserName = L"Administrator";
924 ProfileInfo.dwFlags = PI_NOUI;
925 LoadUserProfileW(hToken, &ProfileInfo);
926 CloseHandle(hToken);
927 }
928 else
929 {
930 DPRINT1("ROS HACK not needed anymore. Please remove it\n");
931 }
932 }
933 /* END OF ROS HACK */
934
935 CreateTempDir(L"TEMP");
936 CreateTempDir(L"TMP");
937
938 if (GetWindowsDirectory(szBuffer, sizeof(szBuffer) / sizeof(TCHAR)))
939 {
940 PathAddBackslash(szBuffer);
941 _tcscat(szBuffer, _T("system"));
942 CreateDirectory(szBuffer, NULL);
943 }
944
945 if (!CommonInstall())
946 return 0;
947
948 InstallWizard();
949
950 SetupCloseInfFile(hSysSetupInf);
951 SetSetupType(0);
952
953 LogItem(SYSSETUP_SEVERITY_INFORMATION, L"Installing ReactOS done");
954 TerminateSetupActionLog();
955
956 /* Get shutdown privilege */
957 if (! OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
958 {
959 DebugPrint("OpenProcessToken() failed!");
960 return 0;
961 }
962 if (!LookupPrivilegeValue(
963 NULL,
964 SE_SHUTDOWN_NAME,
965 &privs.Privileges[0].Luid))
966 {
967 DebugPrint("LookupPrivilegeValue() failed!");
968 return 0;
969 }
970 privs.PrivilegeCount = 1;
971 privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
972 if (AdjustTokenPrivileges(
973 token,
974 FALSE,
975 &privs,
976 0,
977 (PTOKEN_PRIVILEGES)NULL,
978 NULL) == 0)
979 {
980 DebugPrint("AdjustTokenPrivileges() failed!");
981 return 0;
982 }
983
984 ExitWindowsEx(EWX_REBOOT, 0);
985 return 0;
986 }
987
988
989 /*
990 * @unimplemented
991 */
992 DWORD WINAPI
993 SetupChangeFontSize(
994 IN HANDLE hWnd,
995 IN LPCWSTR lpszFontSize)
996 {
997 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
998 return FALSE;
999 }