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 /* Create Games subfolder and fill if the exe is available */
621 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_GAMES
, szFolder
, sizeof(szFolder
)/sizeof(szFolder
[0])))
623 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_SOLITAIRE
, _T("%SystemRoot%\\system32\\sol.exe"), IDS_CMT_SOLITAIRE
, FALSE
);
624 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_WINEMINE
, _T("%SystemRoot%\\system32\\winemine.exe"), IDS_CMT_WINEMINE
, FALSE
);
633 SetSetupType(DWORD dwSetupType
)
638 dwError
= RegOpenKeyExW(
644 if (dwError
!= ERROR_SUCCESS
)
647 dwError
= RegSetValueExW(
652 (LPBYTE
)&dwSetupType
,
655 if (dwError
!= ERROR_SUCCESS
)
662 InstallReactOS(HINSTANCE hInstance
)
664 TCHAR szBuffer
[MAX_PATH
];
667 TOKEN_PRIVILEGES privs
;
669 InitializeSetupActionLog(FALSE
);
670 LogItem(SYSSETUP_SEVERITY_INFORMATION
, L
"Installing ReactOS");
672 /* Set user langage to the system language */
673 SetUserDefaultLCID(GetSystemDefaultLCID());
674 SetThreadLocale(GetSystemDefaultLCID());
676 if (!InitializeProfiles())
678 DebugPrint("InitializeProfiles() failed");
682 if (!CreateShortcuts())
684 DebugPrint("InitializeProfiles() failed");
688 /* Initialize the Security Account Manager (SAM) */
689 if (!SamInitializeSAM())
691 DebugPrint("SamInitializeSAM() failed!");
695 /* Create the semi-random Domain-SID */
696 if (!CreateRandomSid(&DomainSid
))
698 DebugPrint("Domain-SID creation failed!");
702 /* Set the Domain SID (aka Computer SID) */
703 if (!SamSetDomainSid(DomainSid
))
705 DebugPrint("SamSetDomainSid() failed!");
706 RtlFreeSid(DomainSid
);
710 /* Append the Admin-RID */
711 AppendRidToSid(&AdminSid
, DomainSid
, DOMAIN_USER_RID_ADMIN
);
713 /* Create the Administrator account */
714 if (!SamCreateUser(L
"Administrator", L
"", AdminSid
))
716 /* Check what the error was.
717 * If the Admin Account already exists, then it means Setup
718 * wasn't allowed to finish properly. Instead of rebooting
719 * and not completing it, let it restart instead
721 LastError
= GetLastError();
722 if (LastError
!= ERROR_USER_EXISTS
)
724 DebugPrint("SamCreateUser() failed!");
725 RtlFreeSid(AdminSid
);
726 RtlFreeSid(DomainSid
);
731 RtlFreeSid(AdminSid
);
732 RtlFreeSid(DomainSid
);
734 /* ROS HACK, as long as NtUnloadKey is not implemented */
736 NTSTATUS Status
= NtUnloadKey(NULL
);
737 if (Status
== STATUS_NOT_IMPLEMENTED
)
739 /* Create the Administrator profile */
740 PROFILEINFOW ProfileInfo
;
743 #define LOGON32_LOGON_NETWORK 3
744 ret
= LogonUserW(L
"Administrator", L
"", L
"", LOGON32_LOGON_NETWORK
, LOGON32_PROVIDER_DEFAULT
, &hToken
);
747 DebugPrint("LogonUserW() failed!");
750 ZeroMemory(&ProfileInfo
, sizeof(PROFILEINFOW
));
751 ProfileInfo
.dwSize
= sizeof(PROFILEINFOW
);
752 ProfileInfo
.lpUserName
= L
"Administrator";
753 ProfileInfo
.dwFlags
= PI_NOUI
;
754 LoadUserProfileW(hToken
, &ProfileInfo
);
759 DPRINT1("ROS HACK not needed anymore. Please remove it\n");
762 /* END OF ROS HACK */
764 CreateTempDir(L
"TEMP");
765 CreateTempDir(L
"TMP");
767 if (GetWindowsDirectory(szBuffer
, sizeof(szBuffer
) / sizeof(TCHAR
)))
769 PathAddBackslash(szBuffer
);
770 _tcscat(szBuffer
, _T("system"));
771 CreateDirectory(szBuffer
, NULL
);
774 if (!CommonInstall())
779 SetupCloseInfFile(hSysSetupInf
);
782 LogItem(SYSSETUP_SEVERITY_INFORMATION
, L
"Installing ReactOS done");
783 TerminateSetupActionLog();
785 /* Get shutdown privilege */
786 if (! OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &token
))
788 DebugPrint("OpenProcessToken() failed!");
791 if (!LookupPrivilegeValue(
794 &privs
.Privileges
[0].Luid
))
796 DebugPrint("LookupPrivilegeValue() failed!");
799 privs
.PrivilegeCount
= 1;
800 privs
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
801 if (AdjustTokenPrivileges(
806 (PTOKEN_PRIVILEGES
)NULL
,
809 DebugPrint("AdjustTokenPrivileges() failed!");
813 if (SetupData
.BootCDRegtestActive
)
815 /// THE FOLLOWING DPRINT IS FOR THE SYSTEM REGRESSION TOOL
817 DbgPrint("SYSREG_CHECKPOINT:SYSSETUP_COMPLETE\n");
820 ExitWindowsEx(EWX_REBOOT
, 0);
831 IN LPCWSTR lpszFontSize
)
833 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);