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>
56 /* GLOBALS ******************************************************************/
58 PSID DomainSid
= NULL
;
61 HINF hSysSetupInf
= INVALID_HANDLE_VALUE
;
63 /* FUNCTIONS ****************************************************************/
66 DebugPrint(char* fmt
,...)
72 vsprintf(buffer
, fmt
, ap
);
75 strcat(buffer
, "\nRebooting now!");
87 PROCESS_INFORMATION ProcInfo
;
89 WCHAR InstallName
[] = L
"vmwinst.exe";
91 ZeroMemory(&si
, sizeof(STARTUPINFO
));
92 si
.cb
= sizeof(STARTUPINFO
);
94 if(CreateProcess(NULL
, InstallName
, NULL
, NULL
, TRUE
, NORMAL_PRIORITY_CLASS
,
95 NULL
, NULL
, &si
, &ProcInfo
))
97 WaitForSingleObject(ProcInfo
.hProcess
, INFINITE
);
98 CloseHandle(ProcInfo
.hThread
);
99 CloseHandle(ProcInfo
.hProcess
);
107 HRESULT
CreateShellLink(LPCTSTR linkPath
, LPCTSTR cmd
, LPCTSTR arg
, LPCTSTR dir
, LPCTSTR iconPath
, int icon_nr
, LPCTSTR comment
)
112 WCHAR buffer
[MAX_PATH
];
113 #endif /* _UNICODE */
115 HRESULT hr
= CoCreateInstance(&CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IShellLink
, (LPVOID
*)&psl
);
119 hr
= psl
->lpVtbl
->SetPath(psl
, cmd
);
123 hr
= psl
->lpVtbl
->SetArguments(psl
, arg
);
128 hr
= psl
->lpVtbl
->SetWorkingDirectory(psl
, dir
);
133 hr
= psl
->lpVtbl
->SetIconLocation(psl
, iconPath
, icon_nr
);
138 hr
= psl
->lpVtbl
->SetDescription(psl
, comment
);
141 hr
= psl
->lpVtbl
->QueryInterface(psl
, &IID_IPersistFile
, (LPVOID
*)&ppf
);
146 hr
= ppf
->lpVtbl
->Save(ppf
, linkPath
, TRUE
);
148 MultiByteToWideChar(CP_ACP
, 0, linkPath
, -1, buffer
, MAX_PATH
);
150 hr
= ppf
->lpVtbl
->Save(ppf
, buffer
, TRUE
);
151 #endif /* _UNICODE */
153 ppf
->lpVtbl
->Release(ppf
);
156 psl
->lpVtbl
->Release(psl
);
164 CreateShortcut(int csidl
, LPCTSTR folder
, LPCTSTR linkName
, LPCTSTR command
, UINT nIdTitle
)
166 TCHAR path
[MAX_PATH
];
170 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
175 p
= PathAddBackslash(p
);
179 p
= PathAddBackslash(p
);
180 _tcscpy(p
, linkName
);
182 if (!LoadString(hDllInstance
, nIdTitle
, title
, 256))
185 return SUCCEEDED(CreateShellLink(path
, command
, _T(""), NULL
, NULL
, 0, title
));
190 CreateShortcutFolder(int csidl
, UINT nID
, LPTSTR name
, int nameLen
)
192 TCHAR path
[MAX_PATH
];
195 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
198 if (!LoadString(hDllInstance
, nID
, name
, nameLen
))
201 p
= PathAddBackslash(path
);
204 return CreateDirectory(path
, NULL
) || GetLastError()==ERROR_ALREADY_EXISTS
;
209 CreateRandomSid (PSID
*Sid
)
211 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
212 LARGE_INTEGER SystemTime
;
215 NtQuerySystemTime (&SystemTime
);
216 Seed
= &SystemTime
.u
.LowPart
;
218 RtlAllocateAndInitializeSid (&SystemAuthority
,
220 SECURITY_NT_NON_UNIQUE_RID
,
233 AppendRidToSid (PSID
*Dst
,
237 ULONG Rid
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
241 RidCount
= *RtlSubAuthorityCountSid (Src
);
243 for (i
= 0; i
< RidCount
; i
++)
244 Rid
[i
] = *RtlSubAuthoritySid (Src
, i
);
248 Rid
[RidCount
] = NewRid
;
252 RtlAllocateAndInitializeSid (RtlIdentifierAuthoritySid (Src
),
266 RestartDlgProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
271 SendDlgItemMessage(hWnd
, IDC_RESTART_PROGRESS
, PBM_SETRANGE
, 0,
273 SetTimer(hWnd
, 1, 50, NULL
);
281 hWndProgress
= GetDlgItem(hWnd
, IDC_RESTART_PROGRESS
);
282 Position
= SendMessage(hWndProgress
, PBM_GETPOS
, 0, 0);
286 SendMessage(hWndProgress
, PBM_SETPOS
, Position
+ 1, 0);
305 CreateTempDir(LPCWSTR VarName
)
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 ProcessSysSetupInf(VOID
)
364 INFCONTEXT InfContext
;
365 TCHAR LineBuffer
[256];
368 if (!SetupFindFirstLine(hSysSetupInf
,
369 _T("DeviceInfsToInstall"),
378 if (!SetupGetStringField(&InfContext
,
387 if (!SetupDiInstallClass(NULL
, LineBuffer
, DI_QUIETINSTALL
, NULL
))
392 while (SetupFindNextLine(&InfContext
, &InfContext
));
399 InstallReactOS (HINSTANCE hInstance
)
401 TCHAR sAccessories
[256];
404 TCHAR GamePath
[MAX_PATH
];
408 OutputDebugStringA ("InstallReactOS() called\n");
410 if (!InitializeSetupActionLog (FALSE
))
412 OutputDebugStringA ("InitializeSetupActionLog() failed\n");
415 LogItem (SYSSETUP_SEVERITY_INFORMATION
,
416 L
"ReactOS Setup starting");
418 LogItem (SYSSETUP_SEVERITY_FATAL_ERROR
,
421 LogItem (SYSSETUP_SEVERITY_INFORMATION
,
422 L
"ReactOS Setup finished");
424 TerminateSetupActionLog ();
427 UNICODE_STRING SidString
;
431 if (!InitializeProfiles ())
433 DebugPrint ("InitializeProfiles() failed\n");
439 /* create desktop shortcuts */
440 CreateShortcut(CSIDL_DESKTOP
, NULL
, _T("Command Prompt.lnk"), _T("cmd.exe"), IDS_CMT_CMD
);
442 /* create program startmenu shortcuts */
443 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("winefile.lnk"), _T("winefile.exe"), IDS_CMT_WINEFILE
);
445 /* create and fill Accessories subfolder */
446 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_ACCESSORIES
, sAccessories
, 256)) {
447 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Calculator.lnk"), _T("calc.exe"), IDS_CMT_CALC
);
448 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Command Prompt.lnk"), _T("cmd.exe"), IDS_CMT_CMD
);
449 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Notepad.lnk"), _T("notepad.exe"), IDS_CMT_NOTEPAD
);
450 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("ReactOS Explorer.lnk"), _T("explorer.exe"), IDS_CMT_EXPLORER
);
451 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Regedit.lnk"), _T("regedit.exe"), IDS_CMT_REGEDIT
);
455 /* create Games subfolder and fill if the exe is available */
456 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_GAMES
, sGames
, 256)) {
457 if(GetSystemDirectory(Sys
, MAX_PATH
)) {
458 /* copy system dir */
459 _tcscpy(GamePath
, Sys
);
460 /* concatonate full file path and check for existance */
461 if((_taccess(_tcscat(GamePath
, _T("\\sol.exe")), 0 )) != -1)
462 CreateShortcut(CSIDL_PROGRAMS
, sGames
, _T("Solitaire.lnk"), _T("sol.exe"), IDS_CMT_SOLITAIRE
);
464 _tcscpy(GamePath
, Sys
);
465 if((_taccess(_tcscat(GamePath
, _T("\\winemine.exe")), 0 )) != -1)
466 CreateShortcut(CSIDL_PROGRAMS
, sGames
, _T("WineMine.lnk"), _T("winemine.exe"), IDS_CMT_WINEMINE
);
472 /* Create the semi-random Domain-SID */
473 CreateRandomSid (&DomainSid
);
474 if (DomainSid
== NULL
)
476 DebugPrint ("Domain-SID creation failed!\n");
481 RtlConvertSidToUnicodeString (&SidString
, DomainSid
, TRUE
);
482 DebugPrint ("Domain-SID: %wZ\n", &SidString
);
483 RtlFreeUnicodeString (&SidString
);
486 /* Initialize the Security Account Manager (SAM) */
487 if (!SamInitializeSAM ())
489 DebugPrint ("SamInitializeSAM() failed!\n");
490 RtlFreeSid (DomainSid
);
494 /* Set the Domain SID (aka Computer SID) */
495 if (!SamSetDomainSid (DomainSid
))
497 DebugPrint ("SamSetDomainSid() failed!\n");
498 RtlFreeSid (DomainSid
);
502 /* Append the Admin-RID */
503 AppendRidToSid(&AdminSid
, DomainSid
, DOMAIN_USER_RID_ADMIN
);
506 RtlConvertSidToUnicodeString (&SidString
, DomainSid
, TRUE
);
507 DebugPrint ("Admin-SID: %wZ\n", &SidString
);
508 RtlFreeUnicodeString (&SidString
);
511 /* Create the Administrator account */
512 if (!SamCreateUser(L
"Administrator", L
"", AdminSid
))
514 /* Check what the error was.
515 * If the Admin Account already exists, then it means Setup
516 * wasn't allowed to finish properly. Instead of rebooting
517 * and not completing it, let it restart instead
519 LastError
= GetLastError();
520 if (LastError
!= ERROR_USER_EXISTS
)
522 DebugPrint("SamCreateUser() failed!\n");
523 RtlFreeSid(AdminSid
);
524 RtlFreeSid(DomainSid
);
529 /* Create the Administrator profile */
530 if (!CreateUserProfileW(AdminSid
, L
"Administrator"))
532 DebugPrint("CreateUserProfileW() failed!\n");
533 RtlFreeSid(AdminSid
);
534 RtlFreeSid(DomainSid
);
538 RtlFreeSid(AdminSid
);
539 RtlFreeSid(DomainSid
);
541 CreateTempDir(L
"TEMP");
542 CreateTempDir(L
"TMP");
544 hSysSetupInf
= SetupOpenInfFileW(L
"syssetup.inf",
548 if (hSysSetupInf
== INVALID_HANDLE_VALUE
)
550 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
554 if (!ProcessSysSetupInf())
556 DebugPrint("ProcessSysSetupInf() failed!\n");
562 SetupCloseInfFile(hSysSetupInf
);
568 DialogBox(hDllInstance
,
569 MAKEINTRESOURCE(IDD_RESTART
),
581 SetupChangeFontSize(HANDLE hWnd
,
582 LPCWSTR lpszFontSize
)