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.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS system libraries
23 * PURPOSE: System setup
24 * FILE: lib/syssetup/install.c
25 * PROGRAMER: Eric Kohl
28 /* INCLUDES *****************************************************************/
30 #define WIN32_NO_STATUS
32 #define NTOS_MODE_USER
33 #include <ndk/ntndk.h>
41 #include <samlib/samlib.h>
42 #include <syssetup/syssetup.h>
54 /* GLOBALS ******************************************************************/
56 PSID DomainSid
= NULL
;
59 HINF hSysSetupInf
= INVALID_HANDLE_VALUE
;
61 /* FUNCTIONS ****************************************************************/
64 DebugPrint(char* fmt
,...)
70 vsprintf(buffer
, fmt
, ap
);
73 strcat(buffer
, "\nRebooting now!");
81 HRESULT
CreateShellLink(LPCTSTR linkPath
, LPCTSTR cmd
, LPCTSTR arg
, LPCTSTR dir
, LPCTSTR iconPath
, int icon_nr
, LPCTSTR comment
)
86 WCHAR buffer
[MAX_PATH
];
89 HRESULT hr
= CoCreateInstance(&CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IShellLink
, (LPVOID
*)&psl
);
93 hr
= psl
->lpVtbl
->SetPath(psl
, cmd
);
97 hr
= psl
->lpVtbl
->SetArguments(psl
, arg
);
102 hr
= psl
->lpVtbl
->SetWorkingDirectory(psl
, dir
);
107 hr
= psl
->lpVtbl
->SetIconLocation(psl
, iconPath
, icon_nr
);
112 hr
= psl
->lpVtbl
->SetDescription(psl
, comment
);
115 hr
= psl
->lpVtbl
->QueryInterface(psl
, &IID_IPersistFile
, (LPVOID
*)&ppf
);
120 hr
= ppf
->lpVtbl
->Save(ppf
, linkPath
, TRUE
);
122 MultiByteToWideChar(CP_ACP
, 0, linkPath
, -1, buffer
, MAX_PATH
);
124 hr
= ppf
->lpVtbl
->Save(ppf
, buffer
, TRUE
);
125 #endif /* _UNICODE */
127 ppf
->lpVtbl
->Release(ppf
);
130 psl
->lpVtbl
->Release(psl
);
138 CreateShortcut(int csidl
, LPCTSTR folder
, LPCTSTR linkName
, LPCTSTR command
, UINT nIdTitle
)
140 TCHAR path
[MAX_PATH
];
144 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
149 p
= PathAddBackslash(p
);
153 p
= PathAddBackslash(p
);
154 _tcscpy(p
, linkName
);
156 if (!LoadString(hDllInstance
, nIdTitle
, title
, 256))
159 return SUCCEEDED(CreateShellLink(path
, command
, _T(""), NULL
, NULL
, 0, title
));
164 CreateShortcutFolder(int csidl
, UINT nID
, LPTSTR name
, int nameLen
)
166 TCHAR path
[MAX_PATH
];
169 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
172 if (!LoadString(hDllInstance
, nID
, name
, nameLen
))
175 p
= PathAddBackslash(path
);
178 return CreateDirectory(path
, NULL
) || GetLastError()==ERROR_ALREADY_EXISTS
;
183 CreateRandomSid (PSID
*Sid
)
185 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
186 LARGE_INTEGER SystemTime
;
189 NtQuerySystemTime (&SystemTime
);
190 Seed
= &SystemTime
.u
.LowPart
;
192 RtlAllocateAndInitializeSid (&SystemAuthority
,
194 SECURITY_NT_NON_UNIQUE_RID
,
207 AppendRidToSid (PSID
*Dst
,
211 ULONG Rid
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
215 RidCount
= *RtlSubAuthorityCountSid (Src
);
217 for (i
= 0; i
< RidCount
; i
++)
218 Rid
[i
] = *RtlSubAuthoritySid (Src
, i
);
222 Rid
[RidCount
] = NewRid
;
226 RtlAllocateAndInitializeSid (RtlIdentifierAuthoritySid (Src
),
241 CreateTempDir(LPCWSTR VarName
)
243 TCHAR szTempDir
[MAX_PATH
];
244 TCHAR szBuffer
[MAX_PATH
];
248 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
249 _T("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"),
254 DebugPrint("Error: %lu\n", GetLastError());
259 dwLength
= MAX_PATH
* sizeof(TCHAR
);
260 if (RegQueryValueEx(hKey
,
267 DebugPrint("Error: %lu\n", GetLastError());
273 if (!ExpandEnvironmentStrings(szBuffer
,
277 DebugPrint("Error: %lu\n", GetLastError());
282 /* Create profiles directory */
283 if (!CreateDirectory(szTempDir
, NULL
))
285 if (GetLastError() != ERROR_ALREADY_EXISTS
)
287 DebugPrint("Error: %lu\n", GetLastError());
298 ProcessSysSetupInf(VOID
)
300 INFCONTEXT InfContext
;
301 TCHAR LineBuffer
[256];
304 if (!SetupFindFirstLine(hSysSetupInf
,
305 _T("DeviceInfsToInstall"),
314 if (!SetupGetStringField(&InfContext
,
323 if (!SetupDiInstallClass(NULL
, LineBuffer
, DI_QUIETINSTALL
, NULL
))
328 while (SetupFindNextLine(&InfContext
, &InfContext
));
335 EnableUserModePnpManager(VOID
)
337 SC_HANDLE hSCManager
= NULL
;
338 SC_HANDLE hService
= NULL
;
341 hSCManager
= OpenSCManager(NULL
, NULL
, 0);
342 if (hSCManager
== NULL
)
345 hService
= OpenService(hSCManager
, _T("PlugPlay"), SERVICE_CHANGE_CONFIG
| SERVICE_START
);
346 if (hService
== NULL
)
349 ret
= ChangeServiceConfig(
351 SERVICE_NO_CHANGE
, SERVICE_AUTO_START
, SERVICE_NO_CHANGE
,
352 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
356 ret
= StartService(hService
, 0, NULL
);
363 if (hSCManager
!= NULL
)
364 CloseServiceHandle(hSCManager
);
365 if (hService
!= NULL
)
366 CloseServiceHandle(hService
);
372 InstallLiveCD (HINSTANCE hInstance
)
379 TCHAR CommandLine
[MAX_PATH
];
380 STARTUPINFO StartupInfo
;
381 PROCESS_INFORMATION ProcessInformation
;
384 hSysSetupInf
= SetupOpenInfFileW(
389 if (hSysSetupInf
== INVALID_HANDLE_VALUE
)
391 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
395 if (!ProcessSysSetupInf())
397 DebugPrint("ProcessSysSetupInf() failed!\n");
401 SetupCloseInfFile(hSysSetupInf
);
403 if (!EnableUserModePnpManager())
405 DebugPrint("EnableUserModePnpManager() failed!\n");
409 /* Load the default shell */
412 TEXT("SOFTWARE\\ReactOS\\Windows NT\\CurrentVersion\\Winlogon"), /* FIXME: should be REGSTR_PATH_WINLOGON */
416 if (rc
!= ERROR_SUCCESS
)
418 rc
= RegQueryValueEx(
425 if (rc
!= ERROR_SUCCESS
)
427 else if (dwType
!= REG_SZ
&& dwType
!= REG_EXPAND_SZ
)
429 else if (requiredSize
> (MAX_PATH
- 1) * sizeof(TCHAR
))
431 Shell
= HeapAlloc(GetProcessHeap(), 0, requiredSize
+ sizeof(TCHAR
));
434 Shell
[requiredSize
/ sizeof(WCHAR
)] = '\0';
435 rc
= RegQueryValueEx(
442 if (rc
!= ERROR_SUCCESS
)
444 if (dwType
== REG_EXPAND_SZ
)
445 ExpandEnvironmentStrings(Shell
, CommandLine
, MAX_PATH
);
446 else if (dwType
== REG_SZ
)
447 _tcscpy(CommandLine
, Shell
);
450 StartupInfo
.cb
= sizeof(StartupInfo
);
451 StartupInfo
.lpReserved
= NULL
;
452 StartupInfo
.lpDesktop
= NULL
;
453 StartupInfo
.lpTitle
= NULL
;
454 StartupInfo
.dwFlags
= 0;
455 StartupInfo
.cbReserved2
= 0;
456 StartupInfo
.lpReserved2
= 0;
467 &ProcessInformation
);
471 /* Wait for process termination */
472 WaitForSingleObject(ProcessInformation
.hProcess
, INFINITE
);
477 HeapFree(GetProcessHeap(), 0, Shell
);
480 "You can shutdown your computer, or press ENTER to reboot",
488 InstallReactOS (HINSTANCE hInstance
)
490 TCHAR sAccessories
[256];
495 OutputDebugStringA ("InstallReactOS() called\n");
497 if (!InitializeSetupActionLog (FALSE
))
499 OutputDebugStringA ("InitializeSetupActionLog() failed\n");
502 LogItem (SYSSETUP_SEVERITY_INFORMATION
,
503 L
"ReactOS Setup starting");
505 LogItem (SYSSETUP_SEVERITY_FATAL_ERROR
,
508 LogItem (SYSSETUP_SEVERITY_INFORMATION
,
509 L
"ReactOS Setup finished");
511 TerminateSetupActionLog ();
514 UNICODE_STRING SidString
;
518 if (!InitializeProfiles ())
520 DebugPrint ("InitializeProfiles() failed\n");
526 /* create desktop shortcuts */
527 CreateShortcut(CSIDL_DESKTOP
, NULL
, _T("Command Prompt.lnk"), _T("cmd.exe"), IDS_CMT_CMD
);
529 /* create program startmenu shortcuts */
530 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("winefile.lnk"), _T("winefile.exe"), IDS_CMT_WINEFILE
);
531 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("ibrowser.lnk"), _T("ibrowser.exe"), IDS_CMT_IBROWSER
);
532 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("Get Firefox.lnk"), _T("getfirefox.exe"), IDS_CMT_GETFIREFOX
);
534 /* create administritive tools startmenu shortcuts */
535 CreateShortcut(CSIDL_ADMINTOOLS
, NULL
, _T("Services.lnk"), _T("servman.exe"), IDS_CMT_SERVMAN
);
537 /* create and fill Accessories subfolder */
538 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_ACCESSORIES
, sAccessories
, 256))
540 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Calculator.lnk"), _T("calc.exe"), IDS_CMT_CALC
);
541 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Command Prompt.lnk"), _T("cmd.exe"), IDS_CMT_CMD
);
542 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Notepad.lnk"), _T("notepad.exe"), IDS_CMT_NOTEPAD
);
543 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("ReactOS Explorer.lnk"), _T("explorer.exe"), IDS_CMT_EXPLORER
);
544 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Regedit.lnk"), _T("regedit.exe"), IDS_CMT_REGEDIT
);
545 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("WordPad.lnk"), _T("wordpad.exe"), IDS_CMT_WORDPAD
);
546 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("SnapShot.lnk"), _T("screenshot.exe"), IDS_CMT_SCREENSHOT
);
550 /* create Games subfolder and fill if the exe is available */
551 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_GAMES
, sGames
, 256))
553 CreateShortcut(CSIDL_PROGRAMS
, sGames
, _T("Solitaire.lnk"), _T("sol.exe"), IDS_CMT_SOLITAIRE
);
554 CreateShortcut(CSIDL_PROGRAMS
, sGames
, _T("WineMine.lnk"), _T("winemine.exe"), IDS_CMT_WINEMINE
);
559 /* Create the semi-random Domain-SID */
560 CreateRandomSid (&DomainSid
);
561 if (DomainSid
== NULL
)
563 DebugPrint ("Domain-SID creation failed!\n");
568 RtlConvertSidToUnicodeString (&SidString
, DomainSid
, TRUE
);
569 DebugPrint ("Domain-SID: %wZ\n", &SidString
);
570 RtlFreeUnicodeString (&SidString
);
573 /* Initialize the Security Account Manager (SAM) */
574 if (!SamInitializeSAM ())
576 DebugPrint ("SamInitializeSAM() failed!\n");
577 RtlFreeSid (DomainSid
);
581 /* Set the Domain SID (aka Computer SID) */
582 if (!SamSetDomainSid (DomainSid
))
584 DebugPrint ("SamSetDomainSid() failed!\n");
585 RtlFreeSid (DomainSid
);
589 /* Append the Admin-RID */
590 AppendRidToSid(&AdminSid
, DomainSid
, DOMAIN_USER_RID_ADMIN
);
593 RtlConvertSidToUnicodeString (&SidString
, DomainSid
, TRUE
);
594 DebugPrint ("Admin-SID: %wZ\n", &SidString
);
595 RtlFreeUnicodeString (&SidString
);
598 /* Create the Administrator account */
599 if (!SamCreateUser(L
"Administrator", L
"", AdminSid
))
601 /* Check what the error was.
602 * If the Admin Account already exists, then it means Setup
603 * wasn't allowed to finish properly. Instead of rebooting
604 * and not completing it, let it restart instead
606 LastError
= GetLastError();
607 if (LastError
!= ERROR_USER_EXISTS
)
609 DebugPrint("SamCreateUser() failed!\n");
610 RtlFreeSid(AdminSid
);
611 RtlFreeSid(DomainSid
);
616 /* Create the Administrator profile */
617 if (!CreateUserProfileW(AdminSid
, L
"Administrator"))
619 DebugPrint("CreateUserProfileW() failed!\n");
620 RtlFreeSid(AdminSid
);
621 RtlFreeSid(DomainSid
);
625 RtlFreeSid(AdminSid
);
626 RtlFreeSid(DomainSid
);
628 CreateTempDir(L
"TEMP");
629 CreateTempDir(L
"TMP");
631 hSysSetupInf
= SetupOpenInfFileW(L
"syssetup.inf",
635 if (hSysSetupInf
== INVALID_HANDLE_VALUE
)
637 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
641 if (!ProcessSysSetupInf())
643 DebugPrint("ProcessSysSetupInf() failed!\n");
647 if (!EnableUserModePnpManager())
649 DebugPrint("EnableUserModePnpManager() failed!\n");
655 SetupCloseInfFile(hSysSetupInf
);
665 SetupChangeFontSize(HANDLE hWnd
,
666 LPCWSTR lpszFontSize
)