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 InstallReactOS (HINSTANCE hInstance
)
374 TCHAR sAccessories
[256];
379 OutputDebugStringA ("InstallReactOS() called\n");
381 if (!InitializeSetupActionLog (FALSE
))
383 OutputDebugStringA ("InitializeSetupActionLog() failed\n");
386 LogItem (SYSSETUP_SEVERITY_INFORMATION
,
387 L
"ReactOS Setup starting");
389 LogItem (SYSSETUP_SEVERITY_FATAL_ERROR
,
392 LogItem (SYSSETUP_SEVERITY_INFORMATION
,
393 L
"ReactOS Setup finished");
395 TerminateSetupActionLog ();
398 UNICODE_STRING SidString
;
402 if (!InitializeProfiles ())
404 DebugPrint ("InitializeProfiles() failed\n");
410 /* create desktop shortcuts */
411 CreateShortcut(CSIDL_DESKTOP
, NULL
, _T("Command Prompt.lnk"), _T("cmd.exe"), IDS_CMT_CMD
);
413 /* create program startmenu shortcuts */
414 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("winefile.lnk"), _T("winefile.exe"), IDS_CMT_WINEFILE
);
415 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("ibrowser.lnk"), _T("ibrowser.exe"), IDS_CMT_IBROWSER
);
416 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("Get Firefox.lnk"), _T("getfirefox.exe"), IDS_CMT_GETFIREFOX
);
418 /* create administritive tools startmenu shortcuts */
419 CreateShortcut(CSIDL_ADMINTOOLS
, NULL
, _T("Services.lnk"), _T("servman.exe"), IDS_CMT_SERVMAN
);
421 /* create and fill Accessories subfolder */
422 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_ACCESSORIES
, sAccessories
, 256))
424 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Calculator.lnk"), _T("calc.exe"), IDS_CMT_CALC
);
425 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Command Prompt.lnk"), _T("cmd.exe"), IDS_CMT_CMD
);
426 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Notepad.lnk"), _T("notepad.exe"), IDS_CMT_NOTEPAD
);
427 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("ReactOS Explorer.lnk"), _T("explorer.exe"), IDS_CMT_EXPLORER
);
428 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Regedit.lnk"), _T("regedit.exe"), IDS_CMT_REGEDIT
);
429 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("WordPad.lnk"), _T("wordpad.exe"), IDS_CMT_WORDPAD
);
430 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("SnapShot.lnk"), _T("screenshot.exe"), IDS_CMT_SCREENSHOT
);
434 /* create Games subfolder and fill if the exe is available */
435 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_GAMES
, sGames
, 256))
437 CreateShortcut(CSIDL_PROGRAMS
, sGames
, _T("Solitaire.lnk"), _T("sol.exe"), IDS_CMT_SOLITAIRE
);
438 CreateShortcut(CSIDL_PROGRAMS
, sGames
, _T("WineMine.lnk"), _T("winemine.exe"), IDS_CMT_WINEMINE
);
443 /* Create the semi-random Domain-SID */
444 CreateRandomSid (&DomainSid
);
445 if (DomainSid
== NULL
)
447 DebugPrint ("Domain-SID creation failed!\n");
452 RtlConvertSidToUnicodeString (&SidString
, DomainSid
, TRUE
);
453 DebugPrint ("Domain-SID: %wZ\n", &SidString
);
454 RtlFreeUnicodeString (&SidString
);
457 /* Initialize the Security Account Manager (SAM) */
458 if (!SamInitializeSAM ())
460 DebugPrint ("SamInitializeSAM() failed!\n");
461 RtlFreeSid (DomainSid
);
465 /* Set the Domain SID (aka Computer SID) */
466 if (!SamSetDomainSid (DomainSid
))
468 DebugPrint ("SamSetDomainSid() failed!\n");
469 RtlFreeSid (DomainSid
);
473 /* Append the Admin-RID */
474 AppendRidToSid(&AdminSid
, DomainSid
, DOMAIN_USER_RID_ADMIN
);
477 RtlConvertSidToUnicodeString (&SidString
, DomainSid
, TRUE
);
478 DebugPrint ("Admin-SID: %wZ\n", &SidString
);
479 RtlFreeUnicodeString (&SidString
);
482 /* Create the Administrator account */
483 if (!SamCreateUser(L
"Administrator", L
"", AdminSid
))
485 /* Check what the error was.
486 * If the Admin Account already exists, then it means Setup
487 * wasn't allowed to finish properly. Instead of rebooting
488 * and not completing it, let it restart instead
490 LastError
= GetLastError();
491 if (LastError
!= ERROR_USER_EXISTS
)
493 DebugPrint("SamCreateUser() failed!\n");
494 RtlFreeSid(AdminSid
);
495 RtlFreeSid(DomainSid
);
500 /* Create the Administrator profile */
501 if (!CreateUserProfileW(AdminSid
, L
"Administrator"))
503 DebugPrint("CreateUserProfileW() failed!\n");
504 RtlFreeSid(AdminSid
);
505 RtlFreeSid(DomainSid
);
509 RtlFreeSid(AdminSid
);
510 RtlFreeSid(DomainSid
);
512 CreateTempDir(L
"TEMP");
513 CreateTempDir(L
"TMP");
515 hSysSetupInf
= SetupOpenInfFileW(L
"syssetup.inf",
519 if (hSysSetupInf
== INVALID_HANDLE_VALUE
)
521 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
525 if (!ProcessSysSetupInf())
527 DebugPrint("ProcessSysSetupInf() failed!\n");
531 if (!EnableUserModePnpManager())
533 DebugPrint("EnableUserModePnpManager() failed!\n");
539 SetupCloseInfFile(hSysSetupInf
);
549 SetupChangeFontSize(HANDLE hWnd
,
550 LPCWSTR lpszFontSize
)