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 // FIXME: we should pass 'command' straight in here, but shell32 doesn't expand it
215 return SUCCEEDED(CreateShellLink(path
, exeName
, _T(""), lpWorkingDir
, NULL
, 0, title
));
220 CreateShortcutFolder(int csidl
, UINT nID
, LPTSTR name
, int nameLen
)
222 TCHAR path
[MAX_PATH
];
225 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
228 if (!LoadString(hDllInstance
, nID
, name
, nameLen
))
231 p
= PathAddBackslash(path
);
234 return CreateDirectory(path
, NULL
) || GetLastError()==ERROR_ALREADY_EXISTS
;
242 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
243 LARGE_INTEGER SystemTime
;
247 NtQuerySystemTime(&SystemTime
);
248 Seed
= &SystemTime
.u
.LowPart
;
250 Status
= RtlAllocateAndInitializeSid(
253 SECURITY_NT_NON_UNIQUE
,
262 return NT_SUCCESS(Status
);
272 ULONG Rid
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
276 RidCount
= *RtlSubAuthorityCountSid (Src
);
278 for (i
= 0; i
< RidCount
; i
++)
279 Rid
[i
] = *RtlSubAuthoritySid (Src
, i
);
283 Rid
[RidCount
] = NewRid
;
287 RtlAllocateAndInitializeSid(
288 RtlIdentifierAuthoritySid(Src
),
306 TCHAR szTempDir
[MAX_PATH
];
307 TCHAR szBuffer
[MAX_PATH
];
311 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
312 _T("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"),
317 DebugPrint("Error: %lu\n", GetLastError());
322 dwLength
= MAX_PATH
* sizeof(TCHAR
);
323 if (RegQueryValueEx(hKey
,
330 DebugPrint("Error: %lu\n", GetLastError());
336 if (!ExpandEnvironmentStrings(szBuffer
,
340 DebugPrint("Error: %lu\n", GetLastError());
345 /* Create profiles directory */
346 if (!CreateDirectory(szTempDir
, NULL
))
348 if (GetLastError() != ERROR_ALREADY_EXISTS
)
350 DebugPrint("Error: %lu\n", GetLastError());
361 ProcessSysSetupInf(VOID
)
363 INFCONTEXT InfContext
;
364 TCHAR LineBuffer
[256];
367 if (!SetupFindFirstLine(hSysSetupInf
,
368 _T("DeviceInfsToInstall"),
377 if (!SetupGetStringField(&InfContext
,
380 sizeof(LineBuffer
)/sizeof(LineBuffer
[0]),
386 if (!SetupDiInstallClass(NULL
, LineBuffer
, DI_QUIETINSTALL
, NULL
))
391 while (SetupFindNextLine(&InfContext
, &InfContext
));
398 EnableUserModePnpManager(VOID
)
400 SC_HANDLE hSCManager
= NULL
;
401 SC_HANDLE hService
= NULL
;
404 hSCManager
= OpenSCManager(NULL
, NULL
, 0);
405 if (hSCManager
== NULL
)
408 hService
= OpenService(hSCManager
, _T("PlugPlay"), SERVICE_CHANGE_CONFIG
| SERVICE_START
);
409 if (hService
== NULL
)
412 ret
= ChangeServiceConfig(
414 SERVICE_NO_CHANGE
, SERVICE_AUTO_START
, SERVICE_NO_CHANGE
,
415 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
419 ret
= StartService(hService
, 0, NULL
);
426 if (hSCManager
!= NULL
)
427 CloseServiceHandle(hSCManager
);
428 if (hService
!= NULL
)
429 CloseServiceHandle(hService
);
435 StatusMessageWindowProc(
441 UNREFERENCED_PARAMETER(wParam
);
449 if (!LoadString(hDllInstance
, IDS_STATUS_INSTALL_DEV
, szMsg
, sizeof(szMsg
)/sizeof(szMsg
[0])))
451 SetDlgItemText(hwndDlg
, IDC_STATUSLABEL
, szMsg
);
460 ShowStatusMessageThread(
461 IN LPVOID lpParameter
)
463 HWND
*phWnd
= (HWND
*)lpParameter
;
467 hWnd
= CreateDialogParam(
469 MAKEINTRESOURCE(IDD_STATUSWINDOW_DLG
),
471 StatusMessageWindowProc
,
477 ShowWindow(hWnd
, SW_SHOW
);
479 /* Message loop for the Status window */
480 while (GetMessage(&Msg
, NULL
, 0, 0))
482 TranslateMessage(&Msg
);
483 DispatchMessage(&Msg
);
494 hSysSetupInf
= SetupOpenInfFileW(
499 if (hSysSetupInf
== INVALID_HANDLE_VALUE
)
501 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
505 if (!ProcessSysSetupInf())
507 DebugPrint("ProcessSysSetupInf() failed!\n");
508 SetupCloseInfFile(hSysSetupInf
);
515 ShowStatusMessageThread
,
520 if (!EnableUserModePnpManager())
522 DebugPrint("EnableUserModePnpManager() failed!\n");
523 SetupCloseInfFile(hSysSetupInf
);
528 if (CMP_WaitNoPendingInstallEvents(INFINITE
) != WAIT_OBJECT_0
)
530 DebugPrint("CMP_WaitNoPendingInstallEvents() failed!\n");
531 SetupCloseInfFile(hSysSetupInf
);
541 InstallLiveCD(IN HINSTANCE hInstance
)
543 STARTUPINFO StartupInfo
;
544 PROCESS_INFORMATION ProcessInformation
;
547 if (!CommonInstall())
549 SetupCloseInfFile(hSysSetupInf
);
552 StartupInfo
.cb
= sizeof(StartupInfo
);
553 StartupInfo
.lpReserved
= NULL
;
554 StartupInfo
.lpDesktop
= NULL
;
555 StartupInfo
.lpTitle
= NULL
;
556 StartupInfo
.dwFlags
= 0;
557 StartupInfo
.cbReserved2
= 0;
558 StartupInfo
.lpReserved2
= 0;
569 &ProcessInformation
);
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 /* Creacte System Tools subfolder and fill if the exe is available */
615 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_SYS_TOOLS
, szFolder
, sizeof(szFolder
)/sizeof(szFolder
[0])))
617 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_CHARMAP
, _T("%SystemRoot%\\system32\\charmap.exe"), IDS_CMT_CHARMAP
, FALSE
);
620 /* Creacte Accessibility subfolder and fill if the exe is available */
621 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_SYS_ACCESSIBILITY
, szFolder
, sizeof(szFolder
)/sizeof(szFolder
[0])))
623 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_MAGNIFY
, _T("%SystemRoot%\\system32\\magnify.exe"), IDS_CMT_MAGNIFY
, FALSE
);
626 /* Create Games subfolder and fill if the exe is available */
627 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_GAMES
, szFolder
, sizeof(szFolder
)/sizeof(szFolder
[0])))
629 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_SOLITAIRE
, _T("%SystemRoot%\\system32\\sol.exe"), IDS_CMT_SOLITAIRE
, FALSE
);
630 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_WINEMINE
, _T("%SystemRoot%\\system32\\winemine.exe"), IDS_CMT_WINEMINE
, FALSE
);
639 SetSetupType(DWORD dwSetupType
)
644 dwError
= RegOpenKeyExW(
650 if (dwError
!= ERROR_SUCCESS
)
653 dwError
= RegSetValueExW(
658 (LPBYTE
)&dwSetupType
,
661 if (dwError
!= ERROR_SUCCESS
)
668 InstallReactOS(HINSTANCE hInstance
)
670 TCHAR szBuffer
[MAX_PATH
];
673 TOKEN_PRIVILEGES privs
;
675 InitializeSetupActionLog(FALSE
);
676 LogItem(SYSSETUP_SEVERITY_INFORMATION
, L
"Installing ReactOS");
678 /* Set user langage to the system language */
679 SetUserDefaultLCID(GetSystemDefaultLCID());
680 SetThreadLocale(GetSystemDefaultLCID());
682 if (!InitializeProfiles())
684 DebugPrint("InitializeProfiles() failed");
688 if (!CreateShortcuts())
690 DebugPrint("InitializeProfiles() failed");
694 /* Initialize the Security Account Manager (SAM) */
695 if (!SamInitializeSAM())
697 DebugPrint("SamInitializeSAM() failed!");
701 /* Create the semi-random Domain-SID */
702 if (!CreateRandomSid(&DomainSid
))
704 DebugPrint("Domain-SID creation failed!");
708 /* Set the Domain SID (aka Computer SID) */
709 if (!SamSetDomainSid(DomainSid
))
711 DebugPrint("SamSetDomainSid() failed!");
712 RtlFreeSid(DomainSid
);
716 /* Append the Admin-RID */
717 AppendRidToSid(&AdminSid
, DomainSid
, DOMAIN_USER_RID_ADMIN
);
719 /* Create the Administrator account */
720 if (!SamCreateUser(L
"Administrator", L
"", AdminSid
))
722 /* Check what the error was.
723 * If the Admin Account already exists, then it means Setup
724 * wasn't allowed to finish properly. Instead of rebooting
725 * and not completing it, let it restart instead
727 LastError
= GetLastError();
728 if (LastError
!= ERROR_USER_EXISTS
)
730 DebugPrint("SamCreateUser() failed!");
731 RtlFreeSid(AdminSid
);
732 RtlFreeSid(DomainSid
);
737 RtlFreeSid(AdminSid
);
738 RtlFreeSid(DomainSid
);
740 /* ROS HACK, as long as NtUnloadKey is not implemented */
742 NTSTATUS Status
= NtUnloadKey(NULL
);
743 if (Status
== STATUS_NOT_IMPLEMENTED
)
745 /* Create the Administrator profile */
746 PROFILEINFOW ProfileInfo
;
749 #define LOGON32_LOGON_NETWORK 3
750 ret
= LogonUserW(L
"Administrator", L
"", L
"", LOGON32_LOGON_NETWORK
, LOGON32_PROVIDER_DEFAULT
, &hToken
);
753 DebugPrint("LogonUserW() failed!");
756 ZeroMemory(&ProfileInfo
, sizeof(PROFILEINFOW
));
757 ProfileInfo
.dwSize
= sizeof(PROFILEINFOW
);
758 ProfileInfo
.lpUserName
= L
"Administrator";
759 ProfileInfo
.dwFlags
= PI_NOUI
;
760 LoadUserProfileW(hToken
, &ProfileInfo
);
765 DPRINT1("ROS HACK not needed anymore. Please remove it\n");
768 /* END OF ROS HACK */
770 CreateTempDir(L
"TEMP");
771 CreateTempDir(L
"TMP");
773 if (GetWindowsDirectory(szBuffer
, sizeof(szBuffer
) / sizeof(TCHAR
)))
775 PathAddBackslash(szBuffer
);
776 _tcscat(szBuffer
, _T("system"));
777 CreateDirectory(szBuffer
, NULL
);
780 if (!CommonInstall())
785 SetupCloseInfFile(hSysSetupInf
);
788 LogItem(SYSSETUP_SEVERITY_INFORMATION
, L
"Installing ReactOS done");
789 TerminateSetupActionLog();
791 /* Get shutdown privilege */
792 if (! OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &token
))
794 DebugPrint("OpenProcessToken() failed!");
797 if (!LookupPrivilegeValue(
800 &privs
.Privileges
[0].Luid
))
802 DebugPrint("LookupPrivilegeValue() failed!");
805 privs
.PrivilegeCount
= 1;
806 privs
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
807 if (AdjustTokenPrivileges(
812 (PTOKEN_PRIVILEGES
)NULL
,
815 DebugPrint("AdjustTokenPrivileges() failed!");
819 if (SetupData
.BootCDRegtestActive
)
821 /// THE FOLLOWING DPRINT IS FOR THE SYSTEM REGRESSION TOOL
823 DbgPrint("SYSREG_CHECKPOINT:SYSSETUP_COMPLETE\n");
826 ExitWindowsEx(EWX_REBOOT
, 0);
837 IN LPCWSTR lpszFontSize
)
839 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);