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];
377 TCHAR GamePath
[MAX_PATH
];
381 OutputDebugStringA ("InstallReactOS() called\n");
383 if (!InitializeSetupActionLog (FALSE
))
385 OutputDebugStringA ("InitializeSetupActionLog() failed\n");
388 LogItem (SYSSETUP_SEVERITY_INFORMATION
,
389 L
"ReactOS Setup starting");
391 LogItem (SYSSETUP_SEVERITY_FATAL_ERROR
,
394 LogItem (SYSSETUP_SEVERITY_INFORMATION
,
395 L
"ReactOS Setup finished");
397 TerminateSetupActionLog ();
400 UNICODE_STRING SidString
;
404 if (!InitializeProfiles ())
406 DebugPrint ("InitializeProfiles() failed\n");
412 /* create desktop shortcuts */
413 CreateShortcut(CSIDL_DESKTOP
, NULL
, _T("Command Prompt.lnk"), _T("cmd.exe"), IDS_CMT_CMD
);
415 /* create program startmenu shortcuts */
416 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("winefile.lnk"), _T("winefile.exe"), IDS_CMT_WINEFILE
);
417 CreateShortcut(CSIDL_PROGRAMS
, NULL
, _T("ibrowser.lnk"), _T("ibrowser.exe"), IDS_CMT_IBROWSER
);
419 /* create and fill Accessories subfolder */
420 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_ACCESSORIES
, sAccessories
, 256)) {
421 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Calculator.lnk"), _T("calc.exe"), IDS_CMT_CALC
);
422 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Command Prompt.lnk"), _T("cmd.exe"), IDS_CMT_CMD
);
423 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Notepad.lnk"), _T("notepad.exe"), IDS_CMT_NOTEPAD
);
424 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("ReactOS Explorer.lnk"), _T("explorer.exe"), IDS_CMT_EXPLORER
);
425 CreateShortcut(CSIDL_PROGRAMS
, sAccessories
, _T("Regedit.lnk"), _T("regedit.exe"), IDS_CMT_REGEDIT
);
429 /* create Games subfolder and fill if the exe is available */
430 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_GAMES
, sGames
, 256)) {
431 if(GetSystemDirectory(Sys
, MAX_PATH
)) {
432 /* copy system dir */
433 _tcscpy(GamePath
, Sys
);
434 /* concatonate full file path and check for existance */
435 if((_taccess(_tcscat(GamePath
, _T("\\sol.exe")), 0 )) != -1)
436 CreateShortcut(CSIDL_PROGRAMS
, sGames
, _T("Solitaire.lnk"), _T("sol.exe"), IDS_CMT_SOLITAIRE
);
438 _tcscpy(GamePath
, Sys
);
439 if((_taccess(_tcscat(GamePath
, _T("\\winemine.exe")), 0 )) != -1)
440 CreateShortcut(CSIDL_PROGRAMS
, sGames
, _T("WineMine.lnk"), _T("winemine.exe"), IDS_CMT_WINEMINE
);
446 /* Create the semi-random Domain-SID */
447 CreateRandomSid (&DomainSid
);
448 if (DomainSid
== NULL
)
450 DebugPrint ("Domain-SID creation failed!\n");
455 RtlConvertSidToUnicodeString (&SidString
, DomainSid
, TRUE
);
456 DebugPrint ("Domain-SID: %wZ\n", &SidString
);
457 RtlFreeUnicodeString (&SidString
);
460 /* Initialize the Security Account Manager (SAM) */
461 if (!SamInitializeSAM ())
463 DebugPrint ("SamInitializeSAM() failed!\n");
464 RtlFreeSid (DomainSid
);
468 /* Set the Domain SID (aka Computer SID) */
469 if (!SamSetDomainSid (DomainSid
))
471 DebugPrint ("SamSetDomainSid() failed!\n");
472 RtlFreeSid (DomainSid
);
476 /* Append the Admin-RID */
477 AppendRidToSid(&AdminSid
, DomainSid
, DOMAIN_USER_RID_ADMIN
);
480 RtlConvertSidToUnicodeString (&SidString
, DomainSid
, TRUE
);
481 DebugPrint ("Admin-SID: %wZ\n", &SidString
);
482 RtlFreeUnicodeString (&SidString
);
485 /* Create the Administrator account */
486 if (!SamCreateUser(L
"Administrator", L
"", AdminSid
))
488 /* Check what the error was.
489 * If the Admin Account already exists, then it means Setup
490 * wasn't allowed to finish properly. Instead of rebooting
491 * and not completing it, let it restart instead
493 LastError
= GetLastError();
494 if (LastError
!= ERROR_USER_EXISTS
)
496 DebugPrint("SamCreateUser() failed!\n");
497 RtlFreeSid(AdminSid
);
498 RtlFreeSid(DomainSid
);
503 /* Create the Administrator profile */
504 if (!CreateUserProfileW(AdminSid
, L
"Administrator"))
506 DebugPrint("CreateUserProfileW() failed!\n");
507 RtlFreeSid(AdminSid
);
508 RtlFreeSid(DomainSid
);
512 RtlFreeSid(AdminSid
);
513 RtlFreeSid(DomainSid
);
515 CreateTempDir(L
"TEMP");
516 CreateTempDir(L
"TMP");
518 hSysSetupInf
= SetupOpenInfFileW(L
"syssetup.inf",
522 if (hSysSetupInf
== INVALID_HANDLE_VALUE
)
524 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
528 if (!ProcessSysSetupInf())
530 DebugPrint("ProcessSysSetupInf() failed!\n");
534 if (!EnableUserModePnpManager())
536 DebugPrint("EnableUserModePnpManager() failed!\n");
542 SetupCloseInfFile(hSysSetupInf
);
552 SetupChangeFontSize(HANDLE hWnd
,
553 LPCWSTR lpszFontSize
)