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