3 * Copyright (C) 2003 ReactOS Team
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.
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.
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.
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
27 /* INCLUDES *****************************************************************/
29 #define WIN32_NO_STATUS
31 #define NTOS_MODE_USER
32 #include <ndk/ntndk.h>
40 #include <samlib/samlib.h>
41 #include <syssetup/syssetup.h>
55 CMP_WaitNoPendingInstallEvents(DWORD dwTimeout
);
57 /* GLOBALS ******************************************************************/
59 PSID DomainSid
= NULL
;
62 HINF hSysSetupInf
= INVALID_HANDLE_VALUE
;
64 /* FUNCTIONS ****************************************************************/
67 DebugPrint(char* fmt
,...)
73 vsprintf(buffer
, fmt
, ap
);
76 LogItem(SYSSETUP_SEVERITY_FATAL_ERROR
, L
"Failed");
78 strcat(buffer
, "\nRebooting now!");
86 HRESULT
CreateShellLink(LPCTSTR linkPath
, LPCTSTR cmd
, LPCTSTR arg
, LPCTSTR dir
, LPCTSTR iconPath
, int icon_nr
, LPCTSTR comment
)
91 WCHAR buffer
[MAX_PATH
];
94 HRESULT hr
= CoCreateInstance(&CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IShellLink
, (LPVOID
*)&psl
);
98 hr
= psl
->lpVtbl
->SetPath(psl
, cmd
);
102 hr
= psl
->lpVtbl
->SetArguments(psl
, arg
);
107 hr
= psl
->lpVtbl
->SetWorkingDirectory(psl
, dir
);
112 hr
= psl
->lpVtbl
->SetIconLocation(psl
, iconPath
, icon_nr
);
117 hr
= psl
->lpVtbl
->SetDescription(psl
, comment
);
120 hr
= psl
->lpVtbl
->QueryInterface(psl
, &IID_IPersistFile
, (LPVOID
*)&ppf
);
125 hr
= ppf
->lpVtbl
->Save(ppf
, linkPath
, TRUE
);
127 MultiByteToWideChar(CP_ACP
, 0, linkPath
, -1, buffer
, MAX_PATH
);
129 hr
= ppf
->lpVtbl
->Save(ppf
, buffer
, TRUE
);
130 #endif /* _UNICODE */
132 ppf
->lpVtbl
->Release(ppf
);
135 psl
->lpVtbl
->Release(psl
);
143 CreateShortcut(int csidl
, LPCTSTR folder
, UINT nIdName
, LPCTSTR command
, UINT nIdTitle
, BOOL bCheckExistence
)
145 TCHAR path
[MAX_PATH
];
146 TCHAR exeName
[MAX_PATH
];
150 TCHAR szWorkingDir
[MAX_PATH
];
151 LPTSTR lpWorkingDir
= NULL
;
155 if (ExpandEnvironmentStrings(command
,
157 sizeof(path
) / sizeof(path
[0])) == 0)
165 if ((_taccess(path
, 0 )) == -1)
166 /* Expected error, don't return FALSE */
170 dwLen
= GetFullPathName(path
,
171 sizeof(szWorkingDir
) / sizeof(szWorkingDir
[0]),
174 if (dwLen
!= 0 && dwLen
<= sizeof(szWorkingDir
) / sizeof(szWorkingDir
[0]))
176 /* Save the file name */
177 _tcscpy(exeName
, lpFilePart
);
179 if (lpFilePart
!= NULL
)
181 /* We're only interested in the path. Cut the file name off.
182 Also remove the trailing backslash unless the working directory
183 is only going to be a drive, ie. C:\ */
184 *(lpFilePart
--) = _T('\0');
185 if (!(lpFilePart
- szWorkingDir
== 2 && szWorkingDir
[1] == _T(':') &&
186 szWorkingDir
[2] == _T('\\')))
188 *lpFilePart
= _T('\0');
192 lpWorkingDir
= szWorkingDir
;
196 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
201 p
= PathAddBackslash(p
);
205 p
= PathAddBackslash(p
);
207 if (!LoadString(hDllInstance
, nIdName
, name
, sizeof(name
)/sizeof(name
[0])))
211 if (!LoadString(hDllInstance
, nIdTitle
, title
, sizeof(title
)/sizeof(title
[0])))
214 return SUCCEEDED(CreateShellLink(path
, exeName
, _T(""), lpWorkingDir
, NULL
, 0, title
));
219 CreateShortcutFolder(int csidl
, UINT nID
, LPTSTR name
, int nameLen
)
221 TCHAR path
[MAX_PATH
];
224 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
227 if (!LoadString(hDllInstance
, nID
, name
, nameLen
))
230 p
= PathAddBackslash(path
);
233 return CreateDirectory(path
, NULL
) || GetLastError()==ERROR_ALREADY_EXISTS
;
241 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
242 LARGE_INTEGER SystemTime
;
246 NtQuerySystemTime(&SystemTime
);
247 Seed
= &SystemTime
.u
.LowPart
;
249 Status
= RtlAllocateAndInitializeSid(
252 SECURITY_NT_NON_UNIQUE
,
261 return NT_SUCCESS(Status
);
271 ULONG Rid
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
275 RidCount
= *RtlSubAuthorityCountSid (Src
);
277 for (i
= 0; i
< RidCount
; i
++)
278 Rid
[i
] = *RtlSubAuthoritySid (Src
, i
);
282 Rid
[RidCount
] = NewRid
;
286 RtlAllocateAndInitializeSid(
287 RtlIdentifierAuthoritySid(Src
),
305 TCHAR szTempDir
[MAX_PATH
];
306 TCHAR szBuffer
[MAX_PATH
];
310 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
311 _T("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"),
316 DebugPrint("Error: %lu\n", GetLastError());
321 dwLength
= MAX_PATH
* sizeof(TCHAR
);
322 if (RegQueryValueEx(hKey
,
329 DebugPrint("Error: %lu\n", GetLastError());
335 if (!ExpandEnvironmentStrings(szBuffer
,
339 DebugPrint("Error: %lu\n", GetLastError());
344 /* Create profiles directory */
345 if (!CreateDirectory(szTempDir
, NULL
))
347 if (GetLastError() != ERROR_ALREADY_EXISTS
)
349 DebugPrint("Error: %lu\n", GetLastError());
360 ProcessSysSetupInf(VOID
)
362 INFCONTEXT InfContext
;
363 TCHAR LineBuffer
[256];
366 if (!SetupFindFirstLine(hSysSetupInf
,
367 _T("DeviceInfsToInstall"),
376 if (!SetupGetStringField(&InfContext
,
379 sizeof(LineBuffer
)/sizeof(LineBuffer
[0]),
385 if (!SetupDiInstallClass(NULL
, LineBuffer
, DI_QUIETINSTALL
, NULL
))
390 while (SetupFindNextLine(&InfContext
, &InfContext
));
397 EnableUserModePnpManager(VOID
)
399 SC_HANDLE hSCManager
= NULL
;
400 SC_HANDLE hService
= NULL
;
403 hSCManager
= OpenSCManager(NULL
, NULL
, 0);
404 if (hSCManager
== NULL
)
407 hService
= OpenService(hSCManager
, _T("PlugPlay"), SERVICE_CHANGE_CONFIG
| SERVICE_START
);
408 if (hService
== NULL
)
411 ret
= ChangeServiceConfig(
413 SERVICE_NO_CHANGE
, SERVICE_AUTO_START
, SERVICE_NO_CHANGE
,
414 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
418 ret
= StartService(hService
, 0, NULL
);
425 if (hSCManager
!= NULL
)
426 CloseServiceHandle(hSCManager
);
427 if (hService
!= NULL
)
428 CloseServiceHandle(hService
);
434 StatusMessageWindowProc(
440 UNREFERENCED_PARAMETER(wParam
);
448 if (!LoadString(hDllInstance
, IDS_STATUS_INSTALL_DEV
, szMsg
, sizeof(szMsg
)/sizeof(szMsg
[0])))
450 SetDlgItemText(hwndDlg
, IDC_STATUSLABEL
, szMsg
);
459 ShowStatusMessageThread(
460 IN LPVOID lpParameter
)
462 HWND
*phWnd
= (HWND
*)lpParameter
;
466 hWnd
= CreateDialogParam(
468 MAKEINTRESOURCE(IDD_STATUSWINDOW_DLG
),
470 StatusMessageWindowProc
,
476 ShowWindow(hWnd
, SW_SHOW
);
478 /* Message loop for the Status window */
479 while (GetMessage(&Msg
, NULL
, 0, 0))
481 TranslateMessage(&Msg
);
482 DispatchMessage(&Msg
);
493 hSysSetupInf
= SetupOpenInfFileW(
498 if (hSysSetupInf
== INVALID_HANDLE_VALUE
)
500 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
504 if (!ProcessSysSetupInf())
506 DebugPrint("ProcessSysSetupInf() failed!\n");
507 SetupCloseInfFile(hSysSetupInf
);
514 ShowStatusMessageThread
,
519 if (!EnableUserModePnpManager())
521 DebugPrint("EnableUserModePnpManager() failed!\n");
522 SetupCloseInfFile(hSysSetupInf
);
527 if (CMP_WaitNoPendingInstallEvents(INFINITE
) != WAIT_OBJECT_0
)
529 DebugPrint("CMP_WaitNoPendingInstallEvents() failed!\n");
530 SetupCloseInfFile(hSysSetupInf
);
540 InstallLiveCD(IN HINSTANCE hInstance
)
542 STARTUPINFO StartupInfo
;
543 PROCESS_INFORMATION ProcessInformation
;
546 if (!CommonInstall())
548 SetupCloseInfFile(hSysSetupInf
);
551 StartupInfo
.cb
= sizeof(StartupInfo
);
552 StartupInfo
.lpReserved
= NULL
;
553 StartupInfo
.lpDesktop
= NULL
;
554 StartupInfo
.lpTitle
= NULL
;
555 StartupInfo
.dwFlags
= 0;
556 StartupInfo
.cbReserved2
= 0;
557 StartupInfo
.lpReserved2
= 0;
568 &ProcessInformation
);
572 /* Wait for process termination */
573 WaitForSingleObject(ProcessInformation
.hProcess
, INFINITE
);
578 "You can shutdown your computer, or press ENTER to reboot",
586 CreateShortcuts(VOID
)
592 /* Create desktop shortcuts */
593 CreateShortcut(CSIDL_DESKTOP
, NULL
, IDS_SHORT_CMD
, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD
, FALSE
);
595 /* Create program startmenu shortcuts */
596 CreateShortcut(CSIDL_PROGRAMS
, NULL
, IDS_SHORT_EXPLORER
, _T("%SystemRoot%\\explorer.exe"), IDS_CMT_EXPLORER
, FALSE
);
597 CreateShortcut(CSIDL_PROGRAMS
, NULL
, IDS_SHORT_DOWNLOADER
, _T("%SystemRoot%\\system32\\downloader.exe"), IDS_CMT_DOWNLOADER
, TRUE
);
599 /* Create administrative tools startmenu shortcuts */
600 CreateShortcut(CSIDL_COMMON_ADMINTOOLS
, NULL
, IDS_SHORT_SERVICE
, _T("%SystemRoot%\\system32\\servman.exe"), IDS_CMT_SERVMAN
, FALSE
);
601 CreateShortcut(CSIDL_COMMON_ADMINTOOLS
, NULL
, IDS_SHORT_DEVICE
, _T("%SystemRoot%\\system32\\devmgmt.exe"), IDS_CMT_DEVMGMT
, FALSE
);
603 /* Create and fill Accessories subfolder */
604 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_ACCESSORIES
, szFolder
, sizeof(szFolder
)/sizeof(szFolder
[0])))
606 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_CALC
, _T("%SystemRoot%\\system32\\calc.exe"), IDS_CMT_CALC
, FALSE
);
607 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_CMD
, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD
, FALSE
);
608 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_NOTEPAD
, _T("%SystemRoot%\\system32\\notepad.exe"), IDS_CMT_NOTEPAD
, FALSE
);
609 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_REGEDIT
, _T("%SystemRoot%\\regedit.exe"), IDS_CMT_REGEDIT
, FALSE
);
610 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_WORDPAD
, _T("%SystemRoot%\\system32\\wordpad.exe"), IDS_CMT_WORDPAD
, FALSE
);
611 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_SNAP
, _T("%SystemRoot%\\system32\\screenshot.exe"), IDS_CMT_SCREENSHOT
, TRUE
);
614 /* Create Games subfolder and fill if the exe is available */
615 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_GAMES
, szFolder
, sizeof(szFolder
)/sizeof(szFolder
[0])))
617 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_SOLITAIRE
, _T("%SystemRoot%\\system32\\sol.exe"), IDS_CMT_SOLITAIRE
, FALSE
);
618 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_WINEMINE
, _T("%SystemRoot%\\system32\\winemine.exe"), IDS_CMT_WINEMINE
, FALSE
);
627 InstallReactOS(HINSTANCE hInstance
)
629 TCHAR szBuffer
[MAX_PATH
];
632 InitializeSetupActionLog(FALSE
);
633 LogItem(SYSSETUP_SEVERITY_INFORMATION
, L
"Installing ReactOS");
635 /* Set user langage to the system language */
636 SetUserDefaultLCID(GetSystemDefaultLCID());
637 SetThreadLocale(GetSystemDefaultLCID());
639 if (!InitializeProfiles())
641 DebugPrint("InitializeProfiles() failed");
645 if (!CreateShortcuts())
647 DebugPrint("InitializeProfiles() failed");
651 /* Initialize the Security Account Manager (SAM) */
652 if (!SamInitializeSAM())
654 DebugPrint("SamInitializeSAM() failed!");
658 /* Create the semi-random Domain-SID */
659 if (!CreateRandomSid(&DomainSid
))
661 DebugPrint("Domain-SID creation failed!");
665 /* Set the Domain SID (aka Computer SID) */
666 if (!SamSetDomainSid(DomainSid
))
668 DebugPrint("SamSetDomainSid() failed!");
669 RtlFreeSid(DomainSid
);
673 /* Append the Admin-RID */
674 AppendRidToSid(&AdminSid
, DomainSid
, DOMAIN_USER_RID_ADMIN
);
676 /* Create the Administrator account */
677 if (!SamCreateUser(L
"Administrator", L
"", AdminSid
))
679 /* Check what the error was.
680 * If the Admin Account already exists, then it means Setup
681 * wasn't allowed to finish properly. Instead of rebooting
682 * and not completing it, let it restart instead
684 LastError
= GetLastError();
685 if (LastError
!= ERROR_USER_EXISTS
)
687 DebugPrint("SamCreateUser() failed!");
688 RtlFreeSid(AdminSid
);
689 RtlFreeSid(DomainSid
);
694 /* Create the Administrator profile */
695 if (!CreateUserProfileW(AdminSid
, L
"Administrator"))
697 DebugPrint("CreateUserProfileW() failed!");
698 RtlFreeSid(AdminSid
);
699 RtlFreeSid(DomainSid
);
703 RtlFreeSid(AdminSid
);
704 RtlFreeSid(DomainSid
);
706 CreateTempDir(L
"TEMP");
707 CreateTempDir(L
"TMP");
709 if (GetWindowsDirectory(szBuffer
, sizeof(szBuffer
) / sizeof(TCHAR
)))
711 PathAddBackslash(szBuffer
);
712 _tcscat(szBuffer
, _T("system"));
713 CreateDirectory(szBuffer
, NULL
);
716 if (!CommonInstall())
721 SetupCloseInfFile(hSysSetupInf
);
723 LogItem(SYSSETUP_SEVERITY_INFORMATION
, L
"Installing ReactOS done");
724 TerminateSetupActionLog();
726 /// THE FOLLOWING DPRINT IS FOR THE SYSTEM REGRESSION TOOL
728 DbgPrint("SYSREG_CHECKPOINT:SYSSETUP_COMPLETE\n");
740 IN LPCWSTR lpszFontSize
)
742 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);