faf06321c275835eeee10ef610a1a1856226cdf4
[reactos.git] / reactos / lib / syssetup / install.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2003 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 */
19 /* $Id: install.c,v 1.15 2004/08/03 13:43:00 ekohl Exp $
20 *
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 (ekohl@rz-online.de)
26 */
27
28 /* INCLUDES *****************************************************************/
29
30 #include <ntos.h>
31 #include <windows.h>
32 #include <commctrl.h>
33 #include <stdio.h>
34 #include <tchar.h>
35 #include <stdlib.h>
36
37 #include <samlib.h>
38 #include <syssetup.h>
39 #include <userenv.h>
40
41 #include "globals.h"
42 #include "resource.h"
43
44 #define VMWINST
45
46 VOID WINAPI CreateCmdLink(VOID);
47
48
49 /* GLOBALS ******************************************************************/
50
51 PSID DomainSid = NULL;
52 PSID AdminSid = NULL;
53
54
55 /* FUNCTIONS ****************************************************************/
56
57 void
58 DebugPrint(char* fmt,...)
59 {
60 char buffer[512];
61 va_list ap;
62
63 va_start(ap, fmt);
64 vsprintf(buffer, fmt, ap);
65 va_end(ap);
66
67 strcat(buffer, "\nRebooting now!");
68 MessageBoxA(NULL,
69 buffer,
70 "ReactOS Setup",
71 MB_OK);
72 }
73
74
75 #ifdef VMWINST
76 static BOOL
77 RunVMWInstall(VOID)
78 {
79 PROCESS_INFORMATION ProcInfo;
80 STARTUPINFO si;
81
82 ZeroMemory(&si, sizeof(STARTUPINFO));
83 si.cb = sizeof(STARTUPINFO);
84
85 if(CreateProcessA(NULL, "vmwinst.exe", NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS,
86 NULL, NULL, &si, &ProcInfo))
87 {
88 WaitForSingleObject(ProcInfo.hProcess, INFINITE);
89 CloseHandle(ProcInfo.hThread);
90 CloseHandle(ProcInfo.hProcess);
91 return TRUE;
92 }
93 return FALSE;
94 }
95 #endif
96
97
98 static VOID
99 CreateRandomSid (PSID *Sid)
100 {
101 SID_IDENTIFIER_AUTHORITY SystemAuthority = {SECURITY_NT_AUTHORITY};
102 LARGE_INTEGER SystemTime;
103 PULONG Seed;
104
105 NtQuerySystemTime (&SystemTime);
106 Seed = &SystemTime.u.LowPart;
107
108 RtlAllocateAndInitializeSid (&SystemAuthority,
109 4,
110 SECURITY_NT_NON_UNIQUE_RID,
111 RtlUniform (Seed),
112 RtlUniform (Seed),
113 RtlUniform (Seed),
114 SECURITY_NULL_RID,
115 SECURITY_NULL_RID,
116 SECURITY_NULL_RID,
117 SECURITY_NULL_RID,
118 Sid);
119 }
120
121
122 static VOID
123 AppendRidToSid (PSID *Dst,
124 PSID Src,
125 ULONG NewRid)
126 {
127 ULONG Rid[8] = {0, 0, 0, 0, 0, 0, 0, 0};
128 UCHAR RidCount;
129 ULONG i;
130
131 RidCount = *RtlSubAuthorityCountSid (Src);
132
133 for (i = 0; i < RidCount; i++)
134 Rid[i] = *RtlSubAuthoritySid (Src, i);
135
136 if (RidCount < 8)
137 {
138 Rid[RidCount] = NewRid;
139 RidCount++;
140 }
141
142 RtlAllocateAndInitializeSid (RtlIdentifierAuthoritySid (Src),
143 RidCount,
144 Rid[0],
145 Rid[1],
146 Rid[2],
147 Rid[3],
148 Rid[4],
149 Rid[5],
150 Rid[6],
151 Rid[7],
152 Dst);
153 }
154
155 INT_PTR CALLBACK
156 RestartDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
157 {
158 switch(msg)
159 {
160 case WM_INITDIALOG:
161 SendDlgItemMessage(hWnd, IDC_RESTART_PROGRESS, PBM_SETRANGE, 0,
162 MAKELPARAM(0, 300));
163 SetTimer(hWnd, 0, 50, NULL);
164 return TRUE;
165
166 case WM_TIMER:
167 {
168 INT Position;
169 HWND hWndProgress;
170
171 hWndProgress = GetDlgItem(hWnd, IDC_RESTART_PROGRESS);
172 Position = SendMessage(hWndProgress, PBM_GETPOS, 0, 0);
173 if (Position == 300)
174 EndDialog(hWnd, 0);
175 else
176 SendMessage(hWndProgress, PBM_SETPOS, Position + 1, 0);
177 }
178 return TRUE;
179
180 case WM_COMMAND:
181 switch (wParam)
182 {
183 case IDOK:
184 case IDCANCEL:
185 EndDialog(hWnd, 0);
186 return TRUE;
187 }
188 break;
189 }
190
191 return FALSE;
192 }
193
194 static VOID
195 CreateTempDir(LPCWSTR VarName)
196 {
197 WCHAR szTempDir[MAX_PATH];
198 WCHAR szBuffer[MAX_PATH];
199 DWORD dwLength;
200 HKEY hKey;
201
202 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
203 L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
204 0,
205 KEY_ALL_ACCESS,
206 &hKey))
207 {
208 DebugPrint("Error: %lu\n", GetLastError());
209 return;
210 }
211
212 /* Get temp dir */
213 dwLength = MAX_PATH * sizeof(WCHAR);
214 if (RegQueryValueExW (hKey,
215 VarName,
216 NULL,
217 NULL,
218 (LPBYTE)szBuffer,
219 &dwLength))
220 {
221 DebugPrint("Error: %lu\n", GetLastError());
222 RegCloseKey (hKey);
223 return;
224 }
225
226 /* Expand it */
227 if (!ExpandEnvironmentStringsW (szBuffer,
228 szTempDir,
229 MAX_PATH))
230 {
231 DebugPrint("Error: %lu\n", GetLastError());
232 RegCloseKey (hKey);
233 return;
234 }
235
236 /* Create profiles directory */
237 if (!CreateDirectoryW (szTempDir, NULL))
238 {
239 if (GetLastError () != ERROR_ALREADY_EXISTS)
240 {
241 DebugPrint("Error: %lu\n", GetLastError());
242 RegCloseKey (hKey);
243 return;
244 }
245 }
246
247 RegCloseKey (hKey);
248 }
249
250 #define SECTIONBUF_SIZE 4096
251
252 BOOL
253 ProcessSysSetupInf(VOID)
254 {
255 LPTSTR pBuf2;
256 TCHAR szBuf[SECTIONBUF_SIZE];
257 DWORD dwBufSize;
258
259 SetLastError(0);
260
261 dwBufSize = GetPrivateProfileSection(_T("DeviceInfsToInstall"),
262 szBuf,
263 SECTIONBUF_SIZE,
264 _T("Inf\\SYSSETUP.INF"));
265
266 /* fix this first... */
267 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
268 return TRUE;
269
270 if (dwBufSize == SECTIONBUF_SIZE-2)
271 return FALSE;
272
273 if (!dwBufSize)
274 return FALSE;
275
276 pBuf2 = szBuf;
277 while (*pBuf2)
278 {
279 OutputDebugString(_T("Calling Class Installer for "));
280 OutputDebugString(pBuf2);
281 OutputDebugString(_T("\r\n"));
282
283 #if 0
284 /* FIXME: Currently unsupported */
285 if (!SetupDiInstallClass(NULL, pBuf2, DI_QUIETINSTALL, NULL))
286 return FALSE;
287 #endif
288 pBuf2 += _tcslen(pBuf2) + 1;
289 }
290
291 return TRUE;
292 }
293
294
295 DWORD STDCALL
296 InstallReactOS (HINSTANCE hInstance)
297 {
298 # if 0
299 OutputDebugStringA ("InstallReactOS() called\n");
300
301 if (!InitializeSetupActionLog (FALSE))
302 {
303 OutputDebugStringA ("InitializeSetupActionLog() failed\n");
304 }
305
306 LogItem (SYSSETUP_SEVERITY_INFORMATION,
307 L"ReactOS Setup starting");
308
309 LogItem (SYSSETUP_SEVERITY_FATAL_ERROR,
310 L"Buuuuuuaaaah!");
311
312 LogItem (SYSSETUP_SEVERITY_INFORMATION,
313 L"ReactOS Setup finished");
314
315 TerminateSetupActionLog ();
316 #endif
317 #if 0
318 UNICODE_STRING SidString;
319 #endif
320
321 if (!InitializeProfiles ())
322 {
323 DebugPrint ("InitializeProfiles() failed\n");
324 return 0;
325 }
326
327 /* Create the semi-random Domain-SID */
328 CreateRandomSid (&DomainSid);
329 if (DomainSid == NULL)
330 {
331 DebugPrint ("Domain-SID creation failed!\n");
332 return 0;
333 }
334
335 #if 0
336 RtlConvertSidToUnicodeString (&SidString, DomainSid, TRUE);
337 DebugPrint ("Domain-SID: %wZ\n", &SidString);
338 RtlFreeUnicodeString (&SidString);
339 #endif
340
341 /* Initialize the Security Account Manager (SAM) */
342 if (!SamInitializeSAM ())
343 {
344 DebugPrint ("SamInitializeSAM() failed!\n");
345 RtlFreeSid (DomainSid);
346 return 0;
347 }
348
349 /* Set the Domain SID (aka Computer SID) */
350 if (!SamSetDomainSid (DomainSid))
351 {
352 DebugPrint ("SamSetDomainSid() failed!\n");
353 RtlFreeSid (DomainSid);
354 return 0;
355 }
356
357 /* Append the Admin-RID */
358 AppendRidToSid (&AdminSid, DomainSid, DOMAIN_USER_RID_ADMIN);
359
360 #if 0
361 RtlConvertSidToUnicodeString (&SidString, DomainSid, TRUE);
362 DebugPrint ("Admin-SID: %wZ\n", &SidString);
363 RtlFreeUnicodeString (&SidString);
364 #endif
365
366 /* Create the Administrator account */
367 if (!SamCreateUser (L"Administrator", L"", AdminSid))
368 {
369 DebugPrint ("SamCreateUser() failed!\n");
370 RtlFreeSid (AdminSid);
371 RtlFreeSid (DomainSid);
372 return 0;
373 }
374
375 /* Create the Administrator profile */
376 if (!CreateUserProfileW (AdminSid, L"Administrator"))
377 {
378 DebugPrint ("CreateUserProfileW() failed!\n");
379 RtlFreeSid (AdminSid);
380 RtlFreeSid (DomainSid);
381 return 0;
382 }
383
384 RtlFreeSid (AdminSid);
385 RtlFreeSid (DomainSid);
386
387 CreateTempDir(L"TEMP");
388 CreateTempDir(L"TMP");
389
390 if(!ProcessSysSetupInf())
391 {
392 DebugPrint("ProcessSysSetupInf() failed!\n");
393 return 0;
394 }
395
396 InstallWizard();
397
398 #ifdef VMWINST
399 RunVMWInstall ();
400 #endif
401
402 DialogBox (hDllInstance,
403 MAKEINTRESOURCE(IDD_RESTART),
404 NULL,
405 RestartDlgProc);
406
407 return 0;
408 }
409
410
411 /*
412 * @unimplemented
413 */
414 DWORD STDCALL
415 SetupChangeFontSize(HANDLE hWnd,
416 LPCWSTR lpszFontSize)
417 {
418 return FALSE;
419 }