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