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 *****************************************************************/
31 #define NTOS_MODE_USER
32 #include <ndk/ntndk.h>
39 #include <samlib/samlib.h>
40 #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!");
85 PROCESS_INFORMATION ProcInfo
;
87 WCHAR InstallName
[] = L
"vmwinst.exe";
89 ZeroMemory(&si
, sizeof(STARTUPINFO
));
90 si
.cb
= sizeof(STARTUPINFO
);
92 if(CreateProcess(NULL
, InstallName
, NULL
, NULL
, TRUE
, NORMAL_PRIORITY_CLASS
,
93 NULL
, NULL
, &si
, &ProcInfo
))
95 WaitForSingleObject(ProcInfo
.hProcess
, INFINITE
);
96 CloseHandle(ProcInfo
.hThread
);
97 CloseHandle(ProcInfo
.hProcess
);
105 HRESULT
CreateShellLink(LPCTSTR linkPath
, LPCTSTR cmd
, LPCTSTR arg
, LPCTSTR dir
, LPCTSTR iconPath
, int icon_nr
, LPCTSTR comment
)
110 WCHAR buffer
[MAX_PATH
];
111 #endif /* _UNICODE */
113 HRESULT hr
= CoCreateInstance(&CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IShellLink
, (LPVOID
*)&psl
);
117 hr
= psl
->lpVtbl
->SetPath(psl
, cmd
);
121 hr
= psl
->lpVtbl
->SetArguments(psl
, arg
);
126 hr
= psl
->lpVtbl
->SetWorkingDirectory(psl
, dir
);
131 hr
= psl
->lpVtbl
->SetIconLocation(psl
, iconPath
, icon_nr
);
136 hr
= psl
->lpVtbl
->SetDescription(psl
, comment
);
139 hr
= psl
->lpVtbl
->QueryInterface(psl
, &IID_IPersistFile
, (LPVOID
*)&ppf
);
144 hr
= ppf
->lpVtbl
->Save(ppf
, linkPath
, TRUE
);
146 MultiByteToWideChar(CP_ACP
, 0, linkPath
, -1, buffer
, MAX_PATH
);
148 hr
= ppf
->lpVtbl
->Save(ppf
, buffer
, TRUE
);
149 #endif /* _UNICODE */
151 ppf
->lpVtbl
->Release(ppf
);
154 psl
->lpVtbl
->Release(psl
);
162 CreateShortcut(int csidl
, LPCTSTR folder
, LPCTSTR linkName
, LPCTSTR command
, UINT nIdTitle
)
164 TCHAR path
[MAX_PATH
];
168 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
173 p
= PathAddBackslash(p
);
177 p
= PathAddBackslash(p
);
178 _tcscpy(p
, linkName
);
180 if (!LoadString(hDllInstance
, nIdTitle
, title
, 256))
183 return SUCCEEDED(CreateShellLink(path
, command
, _T(""), NULL
, NULL
, 0, title
));
188 CreateShortcutFolder(int csidl
, UINT nID
, LPTSTR name
, int nameLen
)
190 TCHAR path
[MAX_PATH
];
193 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
196 if (!LoadString(hDllInstance
, nID
, name
, nameLen
))
199 p
= PathAddBackslash(path
);
202 return CreateDirectory(path
, NULL
) || GetLastError()==ERROR_ALREADY_EXISTS
;
207 CreateRandomSid (PSID
*Sid
)
209 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
210 LARGE_INTEGER SystemTime
;
213 NtQuerySystemTime (&SystemTime
);
214 Seed
= &SystemTime
.u
.LowPart
;
216 RtlAllocateAndInitializeSid (&SystemAuthority
,
218 SECURITY_NT_NON_UNIQUE_RID
,
231 AppendRidToSid (PSID
*Dst
,
235 ULONG Rid
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
239 RidCount
= *RtlSubAuthorityCountSid (Src
);
241 for (i
= 0; i
< RidCount
; i
++)
242 Rid
[i
] = *RtlSubAuthoritySid (Src
, i
);
246 Rid
[RidCount
] = NewRid
;
250 RtlAllocateAndInitializeSid (RtlIdentifierAuthoritySid (Src
),
264 RestartDlgProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
269 SendDlgItemMessage(hWnd
, IDC_RESTART_PROGRESS
, PBM_SETRANGE
, 0,
271 SetTimer(hWnd
, 1, 50, NULL
);
279 hWndProgress
= GetDlgItem(hWnd
, IDC_RESTART_PROGRESS
);
280 Position
= SendMessage(hWndProgress
, PBM_GETPOS
, 0, 0);
284 SendMessage(hWndProgress
, PBM_SETPOS
, Position
+ 1, 0);
303 CreateTempDir(LPCWSTR VarName
)
305 TCHAR szTempDir
[MAX_PATH
];
306 TCHAR szBuffer
[MAX_PATH
];
310 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
311 _T("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"),
316 DebugPrint("Error: %lu\n", GetLastError());
321 dwLength
= MAX_PATH
* sizeof(TCHAR
);
322 if (RegQueryValueEx(hKey
,
329 DebugPrint("Error: %lu\n", GetLastError());
335 if (!ExpandEnvironmentStrings(szBuffer
,
339 DebugPrint("Error: %lu\n", GetLastError());
344 /* Create profiles directory */
345 if (!CreateDirectory(szTempDir
, NULL
))
347 if (GetLastError() != ERROR_ALREADY_EXISTS
)
349 DebugPrint("Error: %lu\n", GetLastError());
360 ProcessSysSetupInf(VOID
)
362 INFCONTEXT InfContext
;
363 TCHAR LineBuffer
[256];
366 if (!SetupFindFirstLine(hSysSetupInf
,
367 _T("DeviceInfsToInstall"),
376 if (!SetupGetStringField(&InfContext
,
385 if (!SetupDiInstallClass(NULL
, LineBuffer
, DI_QUIETINSTALL
, NULL
))
390 while (SetupFindNextLine(&InfContext
, &InfContext
));
397 InstallReactOS (HINSTANCE hInstance
)
399 TCHAR sAccessories
[256];
402 OutputDebugStringA ("InstallReactOS() called\n");
404 if (!InitializeSetupActionLog (FALSE
))
406 OutputDebugStringA ("InitializeSetupActionLog() failed\n");
409 LogItem (SYSSETUP_SEVERITY_INFORMATION
,
410 L
"ReactOS Setup starting");
412 LogItem (SYSSETUP_SEVERITY_FATAL_ERROR
,
415 LogItem (SYSSETUP_SEVERITY_INFORMATION
,
416 L
"ReactOS Setup finished");
418 TerminateSetupActionLog ();
421 UNICODE_STRING SidString
;
425 if (!InitializeProfiles ())
427 DebugPrint ("InitializeProfiles() failed\n");
433 /* create desktop shortcuts */
434 CreateShortcut(CSIDL_DESKTOP
, NULL
, _T("Command Prompt.lnk"), _T("cmd.exe"), IDS_CMT_CMD
);
436 /* create program startmenu shortcuts */
437 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("Command Prompt.lnk"), _T("cmd.exe"), IDS_CMT_CMD
);
438 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("explorer.lnk"), _T("explorer.exe"), IDS_CMT_EXPLORER
);
439 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("winefile.lnk"), _T("winefile.exe"), IDS_CMT_WINEFILE
);
441 /* create and fill Accessories subfolder */
442 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_ACCESSORIES
, sAccessories
, 256)) {
443 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("notepad.lnk"), _T("notepad.exe"), IDS_CMT_NOTEPAD
);
444 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("regedit.lnk"), _T("regedit.exe"), IDS_CMT_REGEDIT
);
449 /* Create the semi-random Domain-SID */
450 CreateRandomSid (&DomainSid
);
451 if (DomainSid
== NULL
)
453 DebugPrint ("Domain-SID creation failed!\n");
458 RtlConvertSidToUnicodeString (&SidString
, DomainSid
, TRUE
);
459 DebugPrint ("Domain-SID: %wZ\n", &SidString
);
460 RtlFreeUnicodeString (&SidString
);
463 /* Initialize the Security Account Manager (SAM) */
464 if (!SamInitializeSAM ())
466 DebugPrint ("SamInitializeSAM() failed!\n");
467 RtlFreeSid (DomainSid
);
471 /* Set the Domain SID (aka Computer SID) */
472 if (!SamSetDomainSid (DomainSid
))
474 DebugPrint ("SamSetDomainSid() failed!\n");
475 RtlFreeSid (DomainSid
);
479 /* Append the Admin-RID */
480 AppendRidToSid(&AdminSid
, DomainSid
, DOMAIN_USER_RID_ADMIN
);
483 RtlConvertSidToUnicodeString (&SidString
, DomainSid
, TRUE
);
484 DebugPrint ("Admin-SID: %wZ\n", &SidString
);
485 RtlFreeUnicodeString (&SidString
);
488 /* Create the Administrator account */
489 if (!SamCreateUser(L
"Administrator", L
"", AdminSid
))
491 /* Check what the error was.
492 * If the Admin Account already exists, then it means Setup
493 * wasn't allowed to finish properly. Instead of rebooting
494 * and not completing it, let it restart instead
496 LastError
= GetLastError();
497 if (LastError
!= ERROR_USER_EXISTS
)
499 DebugPrint("SamCreateUser() failed!\n");
500 RtlFreeSid(AdminSid
);
501 RtlFreeSid(DomainSid
);
506 /* Create the Administrator profile */
507 if (!CreateUserProfileW(AdminSid
, L
"Administrator"))
509 DebugPrint("CreateUserProfileW() failed!\n");
510 RtlFreeSid(AdminSid
);
511 RtlFreeSid(DomainSid
);
515 RtlFreeSid(AdminSid
);
516 RtlFreeSid(DomainSid
);
518 CreateTempDir(L
"TEMP");
519 CreateTempDir(L
"TMP");
521 hSysSetupInf
= SetupOpenInfFileW(L
"syssetup.inf",
525 if (hSysSetupInf
== INVALID_HANDLE_VALUE
)
527 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
531 if (!ProcessSysSetupInf())
533 DebugPrint("ProcessSysSetupInf() failed!\n");
539 SetupCloseInfFile(hSysSetupInf
);
545 DialogBox(hDllInstance
,
546 MAKEINTRESOURCE(IDD_RESTART
),
558 SetupChangeFontSize(HANDLE hWnd
,
559 LPCWSTR lpszFontSize
)