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>
56 CMP_WaitNoPendingInstallEvents(DWORD dwTimeout
);
58 /* GLOBALS ******************************************************************/
60 PSID DomainSid
= NULL
;
63 HINF hSysSetupInf
= INVALID_HANDLE_VALUE
;
65 /* FUNCTIONS ****************************************************************/
68 DebugPrint(char* fmt
,...)
74 vsprintf(buffer
, fmt
, ap
);
77 LogItem(SYSSETUP_SEVERITY_FATAL_ERROR
, L
"Failed");
79 strcat(buffer
, "\nRebooting now!");
87 HRESULT
CreateShellLink(LPCTSTR linkPath
, LPCTSTR cmd
, LPCTSTR arg
, LPCTSTR dir
, LPCTSTR iconPath
, int icon_nr
, LPCTSTR comment
)
92 WCHAR buffer
[MAX_PATH
];
95 HRESULT hr
= CoCreateInstance(&CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IShellLink
, (LPVOID
*)&psl
);
99 hr
= psl
->lpVtbl
->SetPath(psl
, cmd
);
103 hr
= psl
->lpVtbl
->SetArguments(psl
, arg
);
108 hr
= psl
->lpVtbl
->SetWorkingDirectory(psl
, dir
);
113 hr
= psl
->lpVtbl
->SetIconLocation(psl
, iconPath
, icon_nr
);
118 hr
= psl
->lpVtbl
->SetDescription(psl
, comment
);
121 hr
= psl
->lpVtbl
->QueryInterface(psl
, &IID_IPersistFile
, (LPVOID
*)&ppf
);
126 hr
= ppf
->lpVtbl
->Save(ppf
, linkPath
, TRUE
);
128 MultiByteToWideChar(CP_ACP
, 0, linkPath
, -1, buffer
, MAX_PATH
);
130 hr
= ppf
->lpVtbl
->Save(ppf
, buffer
, TRUE
);
131 #endif /* _UNICODE */
133 ppf
->lpVtbl
->Release(ppf
);
136 psl
->lpVtbl
->Release(psl
);
144 CreateShortcut(int csidl
, LPCTSTR folder
, UINT nIdName
, LPCTSTR command
, UINT nIdTitle
, BOOL bCheckExistence
)
146 TCHAR path
[MAX_PATH
];
147 TCHAR exeName
[MAX_PATH
];
151 TCHAR szWorkingDir
[MAX_PATH
];
152 LPTSTR lpWorkingDir
= NULL
;
156 if (ExpandEnvironmentStrings(command
,
158 sizeof(path
) / sizeof(path
[0])) == 0)
166 if ((_taccess(path
, 0 )) == -1)
167 /* Expected error, don't return FALSE */
171 dwLen
= GetFullPathName(path
,
172 sizeof(szWorkingDir
) / sizeof(szWorkingDir
[0]),
175 if (dwLen
!= 0 && dwLen
<= sizeof(szWorkingDir
) / sizeof(szWorkingDir
[0]))
177 /* Save the file name */
178 _tcscpy(exeName
, lpFilePart
);
180 if (lpFilePart
!= NULL
)
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('\\')))
189 *lpFilePart
= _T('\0');
193 lpWorkingDir
= szWorkingDir
;
197 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
202 p
= PathAddBackslash(p
);
206 p
= PathAddBackslash(p
);
208 if (!LoadString(hDllInstance
, nIdName
, name
, sizeof(name
)/sizeof(name
[0])))
212 if (!LoadString(hDllInstance
, nIdTitle
, title
, sizeof(title
)/sizeof(title
[0])))
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
));
221 CreateShortcutFolder(int csidl
, UINT nID
, LPTSTR name
, int nameLen
)
223 TCHAR path
[MAX_PATH
];
226 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
229 if (!LoadString(hDllInstance
, nID
, name
, nameLen
))
232 p
= PathAddBackslash(path
);
235 return CreateDirectory(path
, NULL
) || GetLastError()==ERROR_ALREADY_EXISTS
;
243 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
244 LARGE_INTEGER SystemTime
;
248 NtQuerySystemTime(&SystemTime
);
249 Seed
= &SystemTime
.u
.LowPart
;
251 Status
= RtlAllocateAndInitializeSid(
254 SECURITY_NT_NON_UNIQUE
,
263 return NT_SUCCESS(Status
);
273 ULONG Rid
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
277 RidCount
= *RtlSubAuthorityCountSid (Src
);
279 for (i
= 0; i
< RidCount
; i
++)
280 Rid
[i
] = *RtlSubAuthoritySid (Src
, i
);
284 Rid
[RidCount
] = NewRid
;
288 RtlAllocateAndInitializeSid(
289 RtlIdentifierAuthoritySid(Src
),
307 TCHAR szTempDir
[MAX_PATH
];
308 TCHAR szBuffer
[MAX_PATH
];
312 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
313 _T("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"),
318 DebugPrint("Error: %lu\n", GetLastError());
323 dwLength
= MAX_PATH
* sizeof(TCHAR
);
324 if (RegQueryValueEx(hKey
,
331 DebugPrint("Error: %lu\n", GetLastError());
337 if (!ExpandEnvironmentStrings(szBuffer
,
341 DebugPrint("Error: %lu\n", GetLastError());
346 /* Create profiles directory */
347 if (!CreateDirectory(szTempDir
, NULL
))
349 if (GetLastError() != ERROR_ALREADY_EXISTS
)
351 DebugPrint("Error: %lu\n", GetLastError());
362 InstallSysSetupInfDevices(VOID
)
364 INFCONTEXT InfContext
;
365 TCHAR LineBuffer
[256];
368 if (!SetupFindFirstLine(hSysSetupInf
,
369 _T("DeviceInfsToInstall"),
378 if (!SetupGetStringField(&InfContext
,
381 sizeof(LineBuffer
)/sizeof(LineBuffer
[0]),
387 if (!SetupDiInstallClass(NULL
, LineBuffer
, DI_QUIETINSTALL
, NULL
))
392 while (SetupFindNextLine(&InfContext
, &InfContext
));
397 InstallSysSetupInfComponents(VOID
)
399 INFCONTEXT InfContext
;
400 TCHAR NameBuffer
[256];
401 TCHAR SectionBuffer
[256];
403 if (!SetupFindFirstLine(hSysSetupInf
,
408 DPRINT("No Inf.Always section found\n");
414 if (!SetupGetStringField(&InfContext
,
415 1, // Get the component name
417 sizeof(NameBuffer
)/sizeof(NameBuffer
[0]),
420 DebugPrint("Error while trying to get component name \n");
424 if (!SetupGetStringField(&InfContext
,
425 2, // Get the component install section
427 sizeof(SectionBuffer
)/sizeof(SectionBuffer
[0]),
430 DebugPrint("Error while trying to get component install section \n");
434 DPRINT("Trying to execute install section '%S' from '%S' \n", SectionBuffer
, NameBuffer
);
436 if (!SetupInstallFromInfSection(NULL
,
448 DebugPrint("Error while trying to install : %S (Error: %lu) \n", NameBuffer
, GetLastError());
452 while (SetupFindNextLine(&InfContext
, &InfContext
));
459 EnableUserModePnpManager(VOID
)
461 SC_HANDLE hSCManager
= NULL
;
462 SC_HANDLE hService
= NULL
;
465 hSCManager
= OpenSCManager(NULL
, NULL
, 0);
466 if (hSCManager
== NULL
)
469 hService
= OpenService(hSCManager
, _T("PlugPlay"), SERVICE_CHANGE_CONFIG
| SERVICE_START
);
470 if (hService
== NULL
)
473 ret
= ChangeServiceConfig(
475 SERVICE_NO_CHANGE
, SERVICE_AUTO_START
, SERVICE_NO_CHANGE
,
476 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
480 ret
= StartService(hService
, 0, NULL
);
487 if (hSCManager
!= NULL
)
488 CloseServiceHandle(hSCManager
);
489 if (hService
!= NULL
)
490 CloseServiceHandle(hService
);
496 StatusMessageWindowProc(
502 UNREFERENCED_PARAMETER(wParam
);
510 if (!LoadString(hDllInstance
, IDS_STATUS_INSTALL_DEV
, szMsg
, sizeof(szMsg
)/sizeof(szMsg
[0])))
512 SetDlgItemText(hwndDlg
, IDC_STATUSLABEL
, szMsg
);
521 ShowStatusMessageThread(
522 IN LPVOID lpParameter
)
524 HWND
*phWnd
= (HWND
*)lpParameter
;
528 hWnd
= CreateDialogParam(
530 MAKEINTRESOURCE(IDD_STATUSWINDOW_DLG
),
532 StatusMessageWindowProc
,
538 ShowWindow(hWnd
, SW_SHOW
);
540 /* Message loop for the Status window */
541 while (GetMessage(&Msg
, NULL
, 0, 0))
543 TranslateMessage(&Msg
);
544 DispatchMessage(&Msg
);
562 return ERROR_INVALID_PARAMETER
;
565 rc
= RegQueryValueExW(hKey
, pszKey
, NULL
, &dwType
, NULL
, &cbData
);
566 if (rc
!= ERROR_SUCCESS
)
568 if (dwType
!= REG_SZ
)
569 return ERROR_FILE_NOT_FOUND
;
570 Value
= HeapAlloc(GetProcessHeap(), 0, cbData
+ sizeof(WCHAR
));
572 return ERROR_NOT_ENOUGH_MEMORY
;
573 rc
= RegQueryValueExW(hKey
, pszKey
, NULL
, NULL
, (LPBYTE
)Value
, &cbData
);
574 if (rc
!= ERROR_SUCCESS
)
576 HeapFree(GetProcessHeap(), 0, Value
);
579 /* NULL-terminate the string */
580 Value
[cbData
/ sizeof(WCHAR
)] = '\0';
583 return ERROR_SUCCESS
;
589 HKEY ControlKey
= NULL
;
590 LPWSTR SystemStartOptions
= NULL
;
591 LPWSTR CurrentOption
, NextOption
; /* Pointers into SystemStartOptions */
592 BOOL ConsoleBoot
= FALSE
;
597 L
"SYSTEM\\CurrentControlSet\\Control",
602 rc
= ReadRegSzKey(ControlKey
, L
"SystemStartOptions", &SystemStartOptions
);
603 if (rc
!= ERROR_SUCCESS
)
606 /* Check for CMDCONS in SystemStartOptions */
607 CurrentOption
= SystemStartOptions
;
608 while (CurrentOption
)
610 NextOption
= wcschr(CurrentOption
, L
' ');
613 if (wcsicmp(CurrentOption
, L
"CONSOLE") == 0)
615 DPRINT("Found %S. Switching to console boot\n", CurrentOption
);
619 CurrentOption
= NextOption
? NextOption
+ 1 : NULL
;
623 if (ControlKey
!= NULL
)
624 RegCloseKey(ControlKey
);
625 HeapFree(GetProcessHeap(), 0, SystemStartOptions
);
634 hSysSetupInf
= SetupOpenInfFileW(
639 if (hSysSetupInf
== INVALID_HANDLE_VALUE
)
641 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
645 if (!InstallSysSetupInfDevices())
647 DebugPrint("InstallSysSetupInfDevices() failed!\n");
648 SetupCloseInfFile(hSysSetupInf
);
652 if(!InstallSysSetupInfComponents())
654 DebugPrint("InstallSysSetupInfComponents() failed!\n");
655 SetupCloseInfFile(hSysSetupInf
);
659 if (!IsConsoleBoot())
664 ShowStatusMessageThread
,
670 if (!EnableUserModePnpManager())
672 DebugPrint("EnableUserModePnpManager() failed!\n");
673 SetupCloseInfFile(hSysSetupInf
);
678 if (CMP_WaitNoPendingInstallEvents(INFINITE
) != WAIT_OBJECT_0
)
680 DebugPrint("CMP_WaitNoPendingInstallEvents() failed!\n");
681 SetupCloseInfFile(hSysSetupInf
);
691 InstallLiveCD(IN HINSTANCE hInstance
)
693 STARTUPINFO StartupInfo
;
694 PROCESS_INFORMATION ProcessInformation
;
697 if (!CommonInstall())
699 SetupCloseInfFile(hSysSetupInf
);
702 StartupInfo
.cb
= sizeof(StartupInfo
);
703 StartupInfo
.lpReserved
= NULL
;
704 StartupInfo
.lpDesktop
= NULL
;
705 StartupInfo
.lpTitle
= NULL
;
706 StartupInfo
.dwFlags
= 0;
707 StartupInfo
.cbReserved2
= 0;
708 StartupInfo
.lpReserved2
= 0;
719 &ProcessInformation
);
728 "You can shutdown your computer, or press ENTER to reboot",
736 CreateShortcuts(VOID
)
742 /* Create desktop shortcuts */
743 CreateShortcut(CSIDL_DESKTOP
, NULL
, IDS_SHORT_CMD
, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD
, FALSE
);
745 /* Create program startmenu shortcuts */
746 CreateShortcut(CSIDL_PROGRAMS
, NULL
, IDS_SHORT_EXPLORER
, _T("%SystemRoot%\\explorer.exe"), IDS_CMT_EXPLORER
, FALSE
);
747 CreateShortcut(CSIDL_PROGRAMS
, NULL
, IDS_SHORT_DOWNLOADER
, _T("%SystemRoot%\\system32\\downloader.exe"), IDS_CMT_DOWNLOADER
, TRUE
);
749 /* Create administrative tools startmenu shortcuts */
750 CreateShortcut(CSIDL_COMMON_ADMINTOOLS
, NULL
, IDS_SHORT_SERVICE
, _T("%SystemRoot%\\system32\\servman.exe"), IDS_CMT_SERVMAN
, FALSE
);
751 CreateShortcut(CSIDL_COMMON_ADMINTOOLS
, NULL
, IDS_SHORT_DEVICE
, _T("%SystemRoot%\\system32\\devmgmt.exe"), IDS_CMT_DEVMGMT
, FALSE
);
753 /* Create and fill Accessories subfolder */
754 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_ACCESSORIES
, szFolder
, sizeof(szFolder
)/sizeof(szFolder
[0])))
756 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_CALC
, _T("%SystemRoot%\\system32\\calc.exe"), IDS_CMT_CALC
, FALSE
);
757 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_CMD
, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD
, FALSE
);
758 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_NOTEPAD
, _T("%SystemRoot%\\system32\\notepad.exe"), IDS_CMT_NOTEPAD
, FALSE
);
759 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_REGEDIT
, _T("%SystemRoot%\\regedit.exe"), IDS_CMT_REGEDIT
, FALSE
);
760 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_WORDPAD
, _T("%SystemRoot%\\system32\\wordpad.exe"), IDS_CMT_WORDPAD
, FALSE
);
761 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_SNAP
, _T("%SystemRoot%\\system32\\screenshot.exe"), IDS_CMT_SCREENSHOT
, TRUE
);
762 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_RDESKTOP
, _T("%SystemRoot%\\system32\\mstsc.exe"), IDS_CMT_RDESKTOP
, FALSE
);
765 /* Creacte System Tools subfolder and fill if the exe is available */
766 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_SYS_TOOLS
, szFolder
, sizeof(szFolder
)/sizeof(szFolder
[0])))
768 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_CHARMAP
, _T("%SystemRoot%\\system32\\charmap.exe"), IDS_CMT_CHARMAP
, FALSE
);
771 /* Creacte Accessibility subfolder and fill if the exe is available */
772 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_SYS_ACCESSIBILITY
, szFolder
, sizeof(szFolder
)/sizeof(szFolder
[0])))
774 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_MAGNIFY
, _T("%SystemRoot%\\system32\\magnify.exe"), IDS_CMT_MAGNIFY
, TRUE
);
777 /* Create Games subfolder and fill if the exe is available */
778 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_GAMES
, szFolder
, sizeof(szFolder
)/sizeof(szFolder
[0])))
780 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_SOLITAIRE
, _T("%SystemRoot%\\system32\\sol.exe"), IDS_CMT_SOLITAIRE
, FALSE
);
781 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_WINEMINE
, _T("%SystemRoot%\\system32\\winemine.exe"), IDS_CMT_WINEMINE
, FALSE
);
790 SetSetupType(DWORD dwSetupType
)
795 dwError
= RegOpenKeyExW(
801 if (dwError
!= ERROR_SUCCESS
)
804 dwError
= RegSetValueExW(
809 (LPBYTE
)&dwSetupType
,
812 if (dwError
!= ERROR_SUCCESS
)
819 InstallReactOS(HINSTANCE hInstance
)
821 TCHAR szBuffer
[MAX_PATH
];
824 TOKEN_PRIVILEGES privs
;
826 InitializeSetupActionLog(FALSE
);
827 LogItem(SYSSETUP_SEVERITY_INFORMATION
, L
"Installing ReactOS");
829 if (!InitializeProfiles())
831 DebugPrint("InitializeProfiles() failed");
835 if (!CreateShortcuts())
837 DebugPrint("InitializeProfiles() failed");
841 /* Initialize the Security Account Manager (SAM) */
842 if (!SamInitializeSAM())
844 DebugPrint("SamInitializeSAM() failed!");
848 /* Create the semi-random Domain-SID */
849 if (!CreateRandomSid(&DomainSid
))
851 DebugPrint("Domain-SID creation failed!");
855 /* Set the Domain SID (aka Computer SID) */
856 if (!SamSetDomainSid(DomainSid
))
858 DebugPrint("SamSetDomainSid() failed!");
859 RtlFreeSid(DomainSid
);
863 /* Append the Admin-RID */
864 AppendRidToSid(&AdminSid
, DomainSid
, DOMAIN_USER_RID_ADMIN
);
866 /* Create the Administrator account */
867 if (!SamCreateUser(L
"Administrator", L
"", AdminSid
))
869 /* Check what the error was.
870 * If the Admin Account already exists, then it means Setup
871 * wasn't allowed to finish properly. Instead of rebooting
872 * and not completing it, let it restart instead
874 LastError
= GetLastError();
875 if (LastError
!= ERROR_USER_EXISTS
)
877 DebugPrint("SamCreateUser() failed!");
878 RtlFreeSid(AdminSid
);
879 RtlFreeSid(DomainSid
);
884 RtlFreeSid(AdminSid
);
885 RtlFreeSid(DomainSid
);
887 /* ROS HACK, as long as NtUnloadKey is not implemented */
889 NTSTATUS Status
= NtUnloadKey(NULL
);
890 if (Status
== STATUS_NOT_IMPLEMENTED
)
892 /* Create the Administrator profile */
893 PROFILEINFOW ProfileInfo
;
896 #define LOGON32_LOGON_NETWORK 3
897 ret
= LogonUserW(L
"Administrator", L
"", L
"", LOGON32_LOGON_NETWORK
, LOGON32_PROVIDER_DEFAULT
, &hToken
);
900 DebugPrint("LogonUserW() failed!");
903 ZeroMemory(&ProfileInfo
, sizeof(PROFILEINFOW
));
904 ProfileInfo
.dwSize
= sizeof(PROFILEINFOW
);
905 ProfileInfo
.lpUserName
= L
"Administrator";
906 ProfileInfo
.dwFlags
= PI_NOUI
;
907 LoadUserProfileW(hToken
, &ProfileInfo
);
912 DPRINT1("ROS HACK not needed anymore. Please remove it\n");
915 /* END OF ROS HACK */
917 CreateTempDir(L
"TEMP");
918 CreateTempDir(L
"TMP");
920 if (GetWindowsDirectory(szBuffer
, sizeof(szBuffer
) / sizeof(TCHAR
)))
922 PathAddBackslash(szBuffer
);
923 _tcscat(szBuffer
, _T("system"));
924 CreateDirectory(szBuffer
, NULL
);
927 if (!CommonInstall())
932 SetupCloseInfFile(hSysSetupInf
);
935 LogItem(SYSSETUP_SEVERITY_INFORMATION
, L
"Installing ReactOS done");
936 TerminateSetupActionLog();
938 /* Get shutdown privilege */
939 if (! OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &token
))
941 DebugPrint("OpenProcessToken() failed!");
944 if (!LookupPrivilegeValue(
947 &privs
.Privileges
[0].Luid
))
949 DebugPrint("LookupPrivilegeValue() failed!");
952 privs
.PrivilegeCount
= 1;
953 privs
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
954 if (AdjustTokenPrivileges(
959 (PTOKEN_PRIVILEGES
)NULL
,
962 DebugPrint("AdjustTokenPrivileges() failed!");
966 if (SetupData
.BootCDRegtestActive
)
968 /// THE FOLLOWING DPRINT IS FOR THE SYSTEM REGRESSION TOOL
970 DbgPrint("SYSREG_CHECKPOINT:SYSSETUP_COMPLETE\n");
973 ExitWindowsEx(EWX_REBOOT
, 0);
984 IN LPCWSTR lpszFontSize
)
986 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);