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>
40 #include <samlib/samlib.h>
41 #include <syssetup/syssetup.h>
55 /* GLOBALS ******************************************************************/
57 PSID DomainSid
= NULL
;
60 HINF hSysSetupInf
= INVALID_HANDLE_VALUE
;
62 /* FUNCTIONS ****************************************************************/
65 DebugPrint(char* fmt
,...)
71 vsprintf(buffer
, fmt
, ap
);
74 strcat(buffer
, "\nRebooting now!");
86 PROCESS_INFORMATION ProcInfo
;
88 WCHAR InstallName
[] = L
"vmwinst.exe";
90 ZeroMemory(&si
, sizeof(STARTUPINFO
));
91 si
.cb
= sizeof(STARTUPINFO
);
93 if(CreateProcess(NULL
, InstallName
, NULL
, NULL
, TRUE
, NORMAL_PRIORITY_CLASS
,
94 NULL
, NULL
, &si
, &ProcInfo
))
96 WaitForSingleObject(ProcInfo
.hProcess
, INFINITE
);
97 CloseHandle(ProcInfo
.hThread
);
98 CloseHandle(ProcInfo
.hProcess
);
106 HRESULT
CreateShellLink(LPCTSTR linkPath
, LPCTSTR cmd
, LPCTSTR arg
, LPCTSTR dir
, LPCTSTR iconPath
, int icon_nr
, LPCTSTR comment
)
111 WCHAR buffer
[MAX_PATH
];
112 #endif /* _UNICODE */
114 HRESULT hr
= CoCreateInstance(&CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IShellLink
, (LPVOID
*)&psl
);
118 hr
= psl
->lpVtbl
->SetPath(psl
, cmd
);
122 hr
= psl
->lpVtbl
->SetArguments(psl
, arg
);
127 hr
= psl
->lpVtbl
->SetWorkingDirectory(psl
, dir
);
132 hr
= psl
->lpVtbl
->SetIconLocation(psl
, iconPath
, icon_nr
);
137 hr
= psl
->lpVtbl
->SetDescription(psl
, comment
);
140 hr
= psl
->lpVtbl
->QueryInterface(psl
, &IID_IPersistFile
, (LPVOID
*)&ppf
);
145 hr
= ppf
->lpVtbl
->Save(ppf
, linkPath
, TRUE
);
147 MultiByteToWideChar(CP_ACP
, 0, linkPath
, -1, buffer
, MAX_PATH
);
149 hr
= ppf
->lpVtbl
->Save(ppf
, buffer
, TRUE
);
150 #endif /* _UNICODE */
152 ppf
->lpVtbl
->Release(ppf
);
155 psl
->lpVtbl
->Release(psl
);
163 CreateShortcut(int csidl
, LPCTSTR folder
, LPCTSTR linkName
, LPCTSTR command
, UINT nIdTitle
)
165 TCHAR path
[MAX_PATH
];
169 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
174 p
= PathAddBackslash(p
);
178 p
= PathAddBackslash(p
);
179 _tcscpy(p
, linkName
);
181 if (!LoadString(hDllInstance
, nIdTitle
, title
, 256))
184 return SUCCEEDED(CreateShellLink(path
, command
, _T(""), NULL
, NULL
, 0, title
));
189 CreateShortcutFolder(int csidl
, UINT nID
, LPTSTR name
, int nameLen
)
191 TCHAR path
[MAX_PATH
];
194 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
197 if (!LoadString(hDllInstance
, nID
, name
, nameLen
))
200 p
= PathAddBackslash(path
);
203 return CreateDirectory(path
, NULL
) || GetLastError()==ERROR_ALREADY_EXISTS
;
208 CreateRandomSid (PSID
*Sid
)
210 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
211 LARGE_INTEGER SystemTime
;
214 NtQuerySystemTime (&SystemTime
);
215 Seed
= &SystemTime
.u
.LowPart
;
217 RtlAllocateAndInitializeSid (&SystemAuthority
,
219 SECURITY_NT_NON_UNIQUE_RID
,
232 AppendRidToSid (PSID
*Dst
,
236 ULONG Rid
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
240 RidCount
= *RtlSubAuthorityCountSid (Src
);
242 for (i
= 0; i
< RidCount
; i
++)
243 Rid
[i
] = *RtlSubAuthoritySid (Src
, i
);
247 Rid
[RidCount
] = NewRid
;
251 RtlAllocateAndInitializeSid (RtlIdentifierAuthoritySid (Src
),
265 RestartDlgProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
270 SendDlgItemMessage(hWnd
, IDC_RESTART_PROGRESS
, PBM_SETRANGE
, 0,
272 SetTimer(hWnd
, 1, 50, NULL
);
280 hWndProgress
= GetDlgItem(hWnd
, IDC_RESTART_PROGRESS
);
281 Position
= SendMessage(hWndProgress
, PBM_GETPOS
, 0, 0);
285 SendMessage(hWndProgress
, PBM_SETPOS
, Position
+ 1, 0);
304 CreateTempDir(LPCWSTR VarName
)
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
,
386 if (!SetupDiInstallClass(NULL
, LineBuffer
, DI_QUIETINSTALL
, NULL
))
391 while (SetupFindNextLine(&InfContext
, &InfContext
));
398 InstallReactOS (HINSTANCE hInstance
)
400 TCHAR sAccessories
[256];
403 OutputDebugStringA ("InstallReactOS() called\n");
405 if (!InitializeSetupActionLog (FALSE
))
407 OutputDebugStringA ("InitializeSetupActionLog() failed\n");
410 LogItem (SYSSETUP_SEVERITY_INFORMATION
,
411 L
"ReactOS Setup starting");
413 LogItem (SYSSETUP_SEVERITY_FATAL_ERROR
,
416 LogItem (SYSSETUP_SEVERITY_INFORMATION
,
417 L
"ReactOS Setup finished");
419 TerminateSetupActionLog ();
422 UNICODE_STRING SidString
;
426 if (!InitializeProfiles ())
428 DebugPrint ("InitializeProfiles() failed\n");
434 /* create desktop shortcuts */
435 CreateShortcut(CSIDL_DESKTOP
, NULL
, _T("Command Prompt.lnk"), _T("cmd.exe"), IDS_CMT_CMD
);
437 /* create program startmenu shortcuts */
438 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("winefile.lnk"), _T("winefile.exe"), IDS_CMT_WINEFILE
);
440 /* create and fill Accessories subfolder */
441 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_ACCESSORIES
, sAccessories
, 256)) {
442 //CreateShortcut(CSIDL_PROGRAMS, sAccessories, _T("Calculator.lnk"), _T("calc.exe"), IDS_CMT_CALC);
443 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Command Prompt.lnk"), _T("cmd.exe"), IDS_CMT_CMD
);
444 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("notepad.lnk"), _T("notepad.exe"), IDS_CMT_NOTEPAD
);
445 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("explorer.lnk"), _T("explorer.exe"), IDS_CMT_EXPLORER
);
446 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("regedit.lnk"), _T("regedit.exe"), IDS_CMT_REGEDIT
);
451 /* Create the semi-random Domain-SID */
452 CreateRandomSid (&DomainSid
);
453 if (DomainSid
== NULL
)
455 DebugPrint ("Domain-SID creation failed!\n");
460 RtlConvertSidToUnicodeString (&SidString
, DomainSid
, TRUE
);
461 DebugPrint ("Domain-SID: %wZ\n", &SidString
);
462 RtlFreeUnicodeString (&SidString
);
465 /* Initialize the Security Account Manager (SAM) */
466 if (!SamInitializeSAM ())
468 DebugPrint ("SamInitializeSAM() failed!\n");
469 RtlFreeSid (DomainSid
);
473 /* Set the Domain SID (aka Computer SID) */
474 if (!SamSetDomainSid (DomainSid
))
476 DebugPrint ("SamSetDomainSid() failed!\n");
477 RtlFreeSid (DomainSid
);
481 /* Append the Admin-RID */
482 AppendRidToSid(&AdminSid
, DomainSid
, DOMAIN_USER_RID_ADMIN
);
485 RtlConvertSidToUnicodeString (&SidString
, DomainSid
, TRUE
);
486 DebugPrint ("Admin-SID: %wZ\n", &SidString
);
487 RtlFreeUnicodeString (&SidString
);
490 /* Create the Administrator account */
491 if (!SamCreateUser(L
"Administrator", L
"", AdminSid
))
493 /* Check what the error was.
494 * If the Admin Account already exists, then it means Setup
495 * wasn't allowed to finish properly. Instead of rebooting
496 * and not completing it, let it restart instead
498 LastError
= GetLastError();
499 if (LastError
!= ERROR_USER_EXISTS
)
501 DebugPrint("SamCreateUser() failed!\n");
502 RtlFreeSid(AdminSid
);
503 RtlFreeSid(DomainSid
);
508 /* Create the Administrator profile */
509 if (!CreateUserProfileW(AdminSid
, L
"Administrator"))
511 DebugPrint("CreateUserProfileW() failed!\n");
512 RtlFreeSid(AdminSid
);
513 RtlFreeSid(DomainSid
);
517 RtlFreeSid(AdminSid
);
518 RtlFreeSid(DomainSid
);
520 CreateTempDir(L
"TEMP");
521 CreateTempDir(L
"TMP");
523 hSysSetupInf
= SetupOpenInfFileW(L
"syssetup.inf",
527 if (hSysSetupInf
== INVALID_HANDLE_VALUE
)
529 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
533 if (!ProcessSysSetupInf())
535 DebugPrint("ProcessSysSetupInf() failed!\n");
541 SetupCloseInfFile(hSysSetupInf
);
547 DialogBox(hDllInstance
,
548 MAKEINTRESOURCE(IDD_RESTART
),
560 SetupChangeFontSize(HANDLE hWnd
,
561 LPCWSTR lpszFontSize
)