385fde1b36f8e5bec9b5dc7f11958e980ea1ce92
[reactos.git] / dll / win32 / syssetup / security.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * PURPOSE: System setup
5 * FILE: dll/win32/syssetup/security.c
6 * PROGRAMER: Eric Kohl
7 */
8
9 /* INCLUDES *****************************************************************/
10
11 #include "precomp.h"
12
13 #include <ntlsa.h>
14 #include <ntsecapi.h>
15 #include <ntsam.h>
16 #include <sddl.h>
17
18 #define NDEBUG
19 #include <debug.h>
20
21 /* FUNCTIONS ****************************************************************/
22
23 NTSTATUS
24 WINAPI
25 SetAccountsDomainSid(
26 PSID DomainSid,
27 LPCWSTR DomainName)
28 {
29 PPOLICY_ACCOUNT_DOMAIN_INFO OrigInfo = NULL;
30 POLICY_ACCOUNT_DOMAIN_INFO Info;
31 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
32 LSA_HANDLE PolicyHandle;
33
34 SAM_HANDLE ServerHandle = NULL;
35 SAM_HANDLE DomainHandle = NULL;
36 DOMAIN_NAME_INFORMATION DomainNameInfo;
37
38 NTSTATUS Status;
39
40 DPRINT("SYSSETUP: SetAccountsDomainSid\n");
41
42 memset(&ObjectAttributes, 0, sizeof(LSA_OBJECT_ATTRIBUTES));
43 ObjectAttributes.Length = sizeof(LSA_OBJECT_ATTRIBUTES);
44
45 Status = LsaOpenPolicy(NULL,
46 &ObjectAttributes,
47 POLICY_VIEW_LOCAL_INFORMATION | POLICY_TRUST_ADMIN,
48 &PolicyHandle);
49 if (Status != STATUS_SUCCESS)
50 {
51 DPRINT("LsaOpenPolicy failed (Status: 0x%08lx)\n", Status);
52 return Status;
53 }
54
55 Status = LsaQueryInformationPolicy(PolicyHandle,
56 PolicyAccountDomainInformation,
57 (PVOID *)&OrigInfo);
58 if (Status == STATUS_SUCCESS && OrigInfo != NULL)
59 {
60 if (DomainName == NULL)
61 {
62 Info.DomainName.Buffer = OrigInfo->DomainName.Buffer;
63 Info.DomainName.Length = OrigInfo->DomainName.Length;
64 Info.DomainName.MaximumLength = OrigInfo->DomainName.MaximumLength;
65 }
66 else
67 {
68 Info.DomainName.Buffer = (LPWSTR)DomainName;
69 Info.DomainName.Length = wcslen(DomainName) * sizeof(WCHAR);
70 Info.DomainName.MaximumLength = Info.DomainName.Length + sizeof(WCHAR);
71 }
72
73 if (DomainSid == NULL)
74 Info.DomainSid = OrigInfo->DomainSid;
75 else
76 Info.DomainSid = DomainSid;
77 }
78 else
79 {
80 Info.DomainName.Buffer = (LPWSTR)DomainName;
81 Info.DomainName.Length = wcslen(DomainName) * sizeof(WCHAR);
82 Info.DomainName.MaximumLength = Info.DomainName.Length + sizeof(WCHAR);
83 Info.DomainSid = DomainSid;
84 }
85
86 Status = LsaSetInformationPolicy(PolicyHandle,
87 PolicyAccountDomainInformation,
88 (PVOID)&Info);
89 if (Status != STATUS_SUCCESS)
90 {
91 DPRINT("LsaSetInformationPolicy failed (Status: 0x%08lx)\n", Status);
92 }
93
94 if (OrigInfo != NULL)
95 LsaFreeMemory(OrigInfo);
96
97 LsaClose(PolicyHandle);
98
99 DomainNameInfo.DomainName.Length = wcslen(DomainName) * sizeof(WCHAR);
100 DomainNameInfo.DomainName.MaximumLength = (wcslen(DomainName) + 1) * sizeof(WCHAR);
101 DomainNameInfo.DomainName.Buffer = (LPWSTR)DomainName;
102
103 Status = SamConnect(NULL,
104 &ServerHandle,
105 SAM_SERVER_CONNECT | SAM_SERVER_LOOKUP_DOMAIN,
106 NULL);
107 if (NT_SUCCESS(Status))
108 {
109 Status = SamOpenDomain(ServerHandle,
110 DOMAIN_WRITE_OTHER_PARAMETERS,
111 Info.DomainSid,
112 &DomainHandle);
113 if (NT_SUCCESS(Status))
114 {
115 Status = SamSetInformationDomain(DomainHandle,
116 DomainNameInformation,
117 (PVOID)&DomainNameInfo);
118 if (!NT_SUCCESS(Status))
119 {
120 DPRINT1("SamSetInformationDomain failed (Status: 0x%08lx)\n", Status);
121 }
122
123 SamCloseHandle(DomainHandle);
124 }
125 else
126 {
127 DPRINT1("SamOpenDomain failed (Status: 0x%08lx)\n", Status);
128 }
129
130 SamCloseHandle(ServerHandle);
131 }
132
133 return Status;
134 }
135
136
137 /* Hack */
138 static
139 NTSTATUS
140 SetPrimaryDomain(LPCWSTR DomainName,
141 PSID DomainSid)
142 {
143 PPOLICY_PRIMARY_DOMAIN_INFO OrigInfo = NULL;
144 POLICY_PRIMARY_DOMAIN_INFO Info;
145 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
146 LSA_HANDLE PolicyHandle;
147 NTSTATUS Status;
148
149 DPRINT1("SYSSETUP: SetPrimaryDomain()\n");
150
151 memset(&ObjectAttributes, 0, sizeof(LSA_OBJECT_ATTRIBUTES));
152 ObjectAttributes.Length = sizeof(LSA_OBJECT_ATTRIBUTES);
153
154 Status = LsaOpenPolicy(NULL,
155 &ObjectAttributes,
156 POLICY_VIEW_LOCAL_INFORMATION | POLICY_TRUST_ADMIN,
157 &PolicyHandle);
158 if (Status != STATUS_SUCCESS)
159 {
160 DPRINT("LsaOpenPolicy failed (Status: 0x%08lx)\n", Status);
161 return Status;
162 }
163
164 Status = LsaQueryInformationPolicy(PolicyHandle,
165 PolicyPrimaryDomainInformation,
166 (PVOID *)&OrigInfo);
167 if (Status == STATUS_SUCCESS && OrigInfo != NULL)
168 {
169 if (DomainName == NULL)
170 {
171 Info.Name.Buffer = OrigInfo->Name.Buffer;
172 Info.Name.Length = OrigInfo->Name.Length;
173 Info.Name.MaximumLength = OrigInfo->Name.MaximumLength;
174 }
175 else
176 {
177 Info.Name.Buffer = (LPWSTR)DomainName;
178 Info.Name.Length = wcslen(DomainName) * sizeof(WCHAR);
179 Info.Name.MaximumLength = Info.Name.Length + sizeof(WCHAR);
180 }
181
182 if (DomainSid == NULL)
183 Info.Sid = OrigInfo->Sid;
184 else
185 Info.Sid = DomainSid;
186 }
187 else
188 {
189 Info.Name.Buffer = (LPWSTR)DomainName;
190 Info.Name.Length = wcslen(DomainName) * sizeof(WCHAR);
191 Info.Name.MaximumLength = Info.Name.Length + sizeof(WCHAR);
192 Info.Sid = DomainSid;
193 }
194
195 Status = LsaSetInformationPolicy(PolicyHandle,
196 PolicyPrimaryDomainInformation,
197 (PVOID)&Info);
198 if (Status != STATUS_SUCCESS)
199 {
200 DPRINT("LsaSetInformationPolicy failed (Status: 0x%08lx)\n", Status);
201 }
202
203 if (OrigInfo != NULL)
204 LsaFreeMemory(OrigInfo);
205
206 LsaClose(PolicyHandle);
207
208 return Status;
209 }
210
211
212 static
213 VOID
214 InstallBuiltinAccounts(VOID)
215 {
216 LPWSTR BuiltinAccounts[] = {
217 L"S-1-1-0", /* Everyone */
218 L"S-1-5-4", /* Interactive */
219 L"S-1-5-6", /* Service */
220 L"S-1-5-19", /* Local Service */
221 L"S-1-5-20", /* Network Service */
222 L"S-1-5-32-544", /* Administrators */
223 L"S-1-5-32-545", /* Users */
224 L"S-1-5-32-547", /* Power Users */
225 L"S-1-5-32-551", /* Backup Operators */
226 L"S-1-5-32-555"}; /* Remote Desktop Users */
227 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
228 NTSTATUS Status;
229 LSA_HANDLE PolicyHandle = NULL;
230 LSA_HANDLE AccountHandle = NULL;
231 PSID AccountSid;
232 ULONG i;
233
234 DPRINT("InstallBuiltinAccounts()\n");
235
236 memset(&ObjectAttributes, 0, sizeof(LSA_OBJECT_ATTRIBUTES));
237
238 Status = LsaOpenPolicy(NULL,
239 &ObjectAttributes,
240 POLICY_CREATE_ACCOUNT,
241 &PolicyHandle);
242 if (!NT_SUCCESS(Status))
243 {
244 DPRINT1("LsaOpenPolicy failed (Status %08lx)\n", Status);
245 return;
246 }
247
248 for (i = 0; i < ARRAYSIZE(BuiltinAccounts); i++)
249 {
250 if (!ConvertStringSidToSid(BuiltinAccounts[i], &AccountSid))
251 {
252 DPRINT1("ConvertStringSidToSid(%S) failed: %lu\n", BuiltinAccounts[i], GetLastError());
253 continue;
254 }
255
256 Status = LsaCreateAccount(PolicyHandle,
257 AccountSid,
258 0,
259 &AccountHandle);
260 if (NT_SUCCESS(Status))
261 {
262 LsaClose(AccountHandle);
263 }
264
265 LocalFree(AccountSid);
266 }
267
268 LsaClose(PolicyHandle);
269 }
270
271
272 static
273 VOID
274 InstallPrivileges(VOID)
275 {
276 HINF hSecurityInf = INVALID_HANDLE_VALUE;
277 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
278 WCHAR szPrivilegeString[256];
279 WCHAR szSidString[256];
280 INFCONTEXT InfContext;
281 DWORD i;
282 PSID AccountSid = NULL;
283 NTSTATUS Status;
284 LSA_HANDLE PolicyHandle = NULL;
285 LSA_UNICODE_STRING RightString;
286 PLSA_TRANSLATED_SID2 Sids = NULL;
287
288 DPRINT("InstallPrivileges()\n");
289
290 hSecurityInf = SetupOpenInfFileW(L"defltws.inf", //szNameBuffer,
291 NULL,
292 INF_STYLE_WIN4,
293 NULL);
294 if (hSecurityInf == INVALID_HANDLE_VALUE)
295 {
296 DPRINT1("SetupOpenInfFileW failed\n");
297 return;
298 }
299
300 memset(&ObjectAttributes, 0, sizeof(LSA_OBJECT_ATTRIBUTES));
301
302 Status = LsaOpenPolicy(NULL,
303 &ObjectAttributes,
304 POLICY_CREATE_ACCOUNT | POLICY_LOOKUP_NAMES,
305 &PolicyHandle);
306 if (!NT_SUCCESS(Status))
307 {
308 DPRINT1("LsaOpenPolicy failed (Status %08lx)\n", Status);
309 goto done;
310 }
311
312 if (!SetupFindFirstLineW(hSecurityInf,
313 L"Privilege Rights",
314 NULL,
315 &InfContext))
316 {
317 DPRINT1("SetupFindFirstLineW failed\n");
318 goto done;
319 }
320
321 do
322 {
323 /* Retrieve the privilege name */
324 if (!SetupGetStringFieldW(&InfContext,
325 0,
326 szPrivilegeString,
327 ARRAYSIZE(szPrivilegeString),
328 NULL))
329 {
330 DPRINT1("SetupGetStringFieldW() failed\n");
331 goto done;
332 }
333 DPRINT("Privilege: %S\n", szPrivilegeString);
334
335 for (i = 0; i < SetupGetFieldCount(&InfContext); i++)
336 {
337 if (!SetupGetStringFieldW(&InfContext,
338 i + 1,
339 szSidString,
340 ARRAYSIZE(szSidString),
341 NULL))
342 {
343 DPRINT1("SetupGetStringFieldW() failed\n");
344 goto done;
345 }
346 DPRINT("SID: %S\n", szSidString);
347
348 if (szSidString[0] == UNICODE_NULL)
349 continue;
350
351 if (szSidString[0] == L'*')
352 {
353 DPRINT("Account Sid: %S\n", &szSidString[1]);
354
355 if (!ConvertStringSidToSid(&szSidString[1], &AccountSid))
356 {
357 DPRINT1("ConvertStringSidToSid(%S) failed: %lu\n", szSidString, GetLastError());
358 continue;
359 }
360 }
361 else
362 {
363 DPRINT("Account name: %S\n", szSidString);
364 continue;
365
366 }
367
368 RtlInitUnicodeString(&RightString, szPrivilegeString);
369 Status = LsaAddAccountRights(PolicyHandle,
370 (AccountSid != NULL) ? AccountSid : Sids[0].Sid,
371 &RightString,
372 1);
373 if (!NT_SUCCESS(Status))
374 {
375 DPRINT1("LsaAddAccountRights() failed (Status %08lx)\n", Status);
376 }
377
378 if (Sids != NULL)
379 {
380 LsaFreeMemory(Sids);
381 Sids = NULL;
382 }
383
384 if (AccountSid != NULL)
385 {
386 LocalFree(AccountSid);
387 AccountSid = NULL;
388 }
389 }
390
391 }
392 while (SetupFindNextLine(&InfContext, &InfContext));
393
394 done:
395 if (PolicyHandle != NULL)
396 LsaClose(PolicyHandle);
397
398 if (hSecurityInf != INVALID_HANDLE_VALUE)
399 SetupCloseInfFile(hSecurityInf);
400 }
401
402
403 static
404 VOID
405 ApplyRegistryValues(VOID)
406 {
407 HINF hSecurityInf = INVALID_HANDLE_VALUE;
408 WCHAR szRegistryPath[MAX_PATH];
409 WCHAR szRootName[MAX_PATH];
410 WCHAR szKeyName[MAX_PATH];
411 WCHAR szValueName[MAX_PATH];
412 INFCONTEXT InfContext;
413 DWORD dwLength, dwType;
414 HKEY hRootKey, hKey;
415 PWSTR Ptr1, Ptr2;
416 DWORD dwError;
417 PVOID pBuffer;
418
419 DPRINT("ApplyRegistryValues()\n");
420
421 hSecurityInf = SetupOpenInfFileW(L"defltws.inf", //szNameBuffer,
422 NULL,
423 INF_STYLE_WIN4,
424 NULL);
425 if (hSecurityInf == INVALID_HANDLE_VALUE)
426 {
427 DPRINT1("SetupOpenInfFileW failed\n");
428 return;
429 }
430
431 if (!SetupFindFirstLineW(hSecurityInf,
432 L"Registry Values",
433 NULL,
434 &InfContext))
435 {
436 DPRINT1("SetupFindFirstLineW failed\n");
437 goto done;
438 }
439
440 do
441 {
442 /* Retrieve the privilege name */
443 if (!SetupGetStringFieldW(&InfContext,
444 0,
445 szRegistryPath,
446 ARRAYSIZE(szRegistryPath),
447 NULL))
448 {
449 DPRINT1("SetupGetStringFieldW() failed\n");
450 goto done;
451 }
452
453 DPRINT("RegistryPath: %S\n", szRegistryPath);
454
455 Ptr1 = wcschr(szRegistryPath, L'\\');
456 Ptr2 = wcsrchr(szRegistryPath, L'\\');
457 if (Ptr1 != NULL && Ptr2 != NULL && Ptr1 != Ptr2)
458 {
459 dwLength = (DWORD)(((ULONG_PTR)Ptr1 - (ULONG_PTR)szRegistryPath) / sizeof(WCHAR));
460 wcsncpy(szRootName, szRegistryPath, dwLength);
461 szRootName[dwLength] = UNICODE_NULL;
462
463 Ptr1++;
464 dwLength = (DWORD)(((ULONG_PTR)Ptr2 - (ULONG_PTR)Ptr1) / sizeof(WCHAR));
465 wcsncpy(szKeyName, Ptr1, dwLength);
466 szKeyName[dwLength] = UNICODE_NULL;
467
468 Ptr2++;
469 wcscpy(szValueName, Ptr2);
470
471 DPRINT("RootName: %S\n", szRootName);
472 DPRINT("KeyName: %S\n", szKeyName);
473 DPRINT("ValueName: %S\n", szValueName);
474
475 if (_wcsicmp(szRootName, L"Machine") == 0)
476 {
477 hRootKey = HKEY_LOCAL_MACHINE;
478 }
479 else
480 {
481 DPRINT1("Unsupported root key %S\n", szRootName);
482 break;
483 }
484
485 if (!SetupGetIntField(&InfContext,
486 1,
487 (PINT)&dwType))
488 {
489 DPRINT1("Failed to create the key %S (Error %lu)\n", szKeyName, dwError);
490 break;
491 }
492
493 if (dwType != REG_SZ && dwType != REG_EXPAND_SZ && dwType != REG_BINARY &&
494 dwType != REG_DWORD && dwType != REG_MULTI_SZ)
495 {
496 DPRINT1("Invalid value type %lu\n", dwType);
497 break;
498 }
499
500 dwLength = 0;
501 switch (dwType)
502 {
503 case REG_SZ:
504 case REG_EXPAND_SZ:
505 SetupGetStringField(&InfContext,
506 2,
507 NULL,
508 0,
509 &dwLength);
510 dwLength *= sizeof(WCHAR);
511 break;
512
513 case REG_BINARY:
514 SetupGetBinaryField(&InfContext,
515 2,
516 NULL,
517 0,
518 &dwLength);
519 break;
520
521 case REG_DWORD:
522 dwLength = sizeof(INT);
523 break;
524
525 case REG_MULTI_SZ:
526 SetupGetMultiSzField(&InfContext,
527 2,
528 NULL,
529 0,
530 &dwLength);
531 dwLength *= sizeof(WCHAR);
532 break;
533 }
534
535 if (dwLength == 0)
536 {
537 DPRINT1("Failed to determine the required buffer size!\n");
538 break;
539 }
540
541 dwError = RegCreateKeyExW(hRootKey,
542 szKeyName,
543 0,
544 NULL,
545 REG_OPTION_NON_VOLATILE,
546 KEY_WRITE,
547 NULL,
548 &hKey,
549 NULL);
550 if (dwError != ERROR_SUCCESS)
551 {
552 DPRINT1("Failed to create the key %S (Error %lu)\n", szKeyName, dwError);
553 break;
554 }
555
556 pBuffer = HeapAlloc(GetProcessHeap(), 0, dwLength);
557 if (pBuffer)
558 {
559 switch (dwType)
560 {
561 case REG_SZ:
562 case REG_EXPAND_SZ:
563 SetupGetStringField(&InfContext,
564 2,
565 pBuffer,
566 dwLength / sizeof(WCHAR),
567 &dwLength);
568 dwLength *= sizeof(WCHAR);
569 break;
570
571 case REG_BINARY:
572 SetupGetBinaryField(&InfContext,
573 2,
574 pBuffer,
575 dwLength,
576 &dwLength);
577 break;
578
579 case REG_DWORD:
580 SetupGetIntField(&InfContext,
581 2,
582 pBuffer);
583 break;
584
585 case REG_MULTI_SZ:
586 SetupGetMultiSzField(&InfContext,
587 2,
588 pBuffer,
589 dwLength / sizeof(WCHAR),
590 &dwLength);
591 dwLength *= sizeof(WCHAR);
592 break;
593 }
594
595 RegSetValueEx(hKey,
596 szValueName,
597 0,
598 dwType,
599 pBuffer,
600 dwLength);
601
602 HeapFree(GetProcessHeap(), 0, pBuffer);
603 }
604
605 RegCloseKey(hKey);
606 }
607 }
608 while (SetupFindNextLine(&InfContext, &InfContext));
609
610 done:
611 if (hSecurityInf != INVALID_HANDLE_VALUE)
612 SetupCloseInfFile(hSecurityInf);
613 }
614
615
616 VOID
617 InstallSecurity(VOID)
618 {
619 InstallBuiltinAccounts();
620 InstallPrivileges();
621 ApplyRegistryValues();
622
623 /* Hack */
624 SetPrimaryDomain(L"WORKGROUP", NULL);
625 }
626
627
628 NTSTATUS
629 SetAdministratorPassword(LPCWSTR Password)
630 {
631 PPOLICY_ACCOUNT_DOMAIN_INFO OrigInfo = NULL;
632 PUSER_ACCOUNT_NAME_INFORMATION AccountNameInfo = NULL;
633 USER_SET_PASSWORD_INFORMATION PasswordInfo;
634 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
635 LSA_HANDLE PolicyHandle = NULL;
636 SAM_HANDLE ServerHandle = NULL;
637 SAM_HANDLE DomainHandle = NULL;
638 SAM_HANDLE UserHandle = NULL;
639 NTSTATUS Status;
640
641 DPRINT("SYSSETUP: SetAdministratorPassword(%p)\n", Password);
642
643 memset(&ObjectAttributes, 0, sizeof(LSA_OBJECT_ATTRIBUTES));
644 ObjectAttributes.Length = sizeof(LSA_OBJECT_ATTRIBUTES);
645
646 Status = LsaOpenPolicy(NULL,
647 &ObjectAttributes,
648 POLICY_VIEW_LOCAL_INFORMATION | POLICY_TRUST_ADMIN,
649 &PolicyHandle);
650 if (Status != STATUS_SUCCESS)
651 {
652 DPRINT1("LsaOpenPolicy() failed (Status: 0x%08lx)\n", Status);
653 return Status;
654 }
655
656 Status = LsaQueryInformationPolicy(PolicyHandle,
657 PolicyAccountDomainInformation,
658 (PVOID *)&OrigInfo);
659 if (!NT_SUCCESS(Status))
660 {
661 DPRINT1("LsaQueryInformationPolicy() failed (Status: 0x%08lx)\n", Status);
662 goto done;
663 }
664
665 Status = SamConnect(NULL,
666 &ServerHandle,
667 SAM_SERVER_CONNECT | SAM_SERVER_LOOKUP_DOMAIN,
668 NULL);
669 if (!NT_SUCCESS(Status))
670 {
671 DPRINT1("SamConnect() failed (Status: 0x%08lx)\n", Status);
672 goto done;
673 }
674
675 Status = SamOpenDomain(ServerHandle,
676 DOMAIN_LOOKUP,
677 OrigInfo->DomainSid,
678 &DomainHandle);
679 if (!NT_SUCCESS(Status))
680 {
681 DPRINT1("SamOpenDomain() failed (Status: 0x%08lx)\n", Status);
682 goto done;
683 }
684
685 Status = SamOpenUser(DomainHandle,
686 USER_FORCE_PASSWORD_CHANGE | USER_READ_GENERAL,
687 DOMAIN_USER_RID_ADMIN,
688 &UserHandle);
689 if (!NT_SUCCESS(Status))
690 {
691 DPRINT1("SamOpenUser() failed (Status %08lx)\n", Status);
692 goto done;
693 }
694
695 RtlInitUnicodeString(&PasswordInfo.Password, Password);
696 PasswordInfo.PasswordExpired = FALSE;
697
698 Status = SamSetInformationUser(UserHandle,
699 UserSetPasswordInformation,
700 (PVOID)&PasswordInfo);
701 if (!NT_SUCCESS(Status))
702 {
703 DPRINT1("SamSetInformationUser() failed (Status %08lx)\n", Status);
704 goto done;
705 }
706
707 Status = SamQueryInformationUser(UserHandle,
708 UserAccountNameInformation,
709 (PVOID*)&AccountNameInfo);
710 if (!NT_SUCCESS(Status))
711 {
712 DPRINT1("SamSetInformationUser() failed (Status %08lx)\n", Status);
713 goto done;
714 }
715
716 AdminInfo.Name = RtlAllocateHeap(RtlGetProcessHeap(),
717 HEAP_ZERO_MEMORY,
718 AccountNameInfo->UserName.Length + sizeof(WCHAR));
719 if (AdminInfo.Name != NULL)
720 RtlCopyMemory(AdminInfo.Name,
721 AccountNameInfo->UserName.Buffer,
722 AccountNameInfo->UserName.Length);
723
724 AdminInfo.Domain = RtlAllocateHeap(RtlGetProcessHeap(),
725 HEAP_ZERO_MEMORY,
726 OrigInfo->DomainName.Length + sizeof(WCHAR));
727 if (AdminInfo.Domain != NULL)
728 RtlCopyMemory(AdminInfo.Domain,
729 OrigInfo->DomainName.Buffer,
730 OrigInfo->DomainName.Length);
731
732 AdminInfo.Password = RtlAllocateHeap(RtlGetProcessHeap(),
733 0,
734 (wcslen(Password) + 1) * sizeof(WCHAR));
735 if (AdminInfo.Password != NULL)
736 wcscpy(AdminInfo.Password, Password);
737
738 DPRINT("Administrator Name: %S\n", AdminInfo.Name);
739 DPRINT("Administrator Domain: %S\n", AdminInfo.Domain);
740 DPRINT("Administrator Password: %S\n", AdminInfo.Password);
741
742 done:
743 if (AccountNameInfo != NULL)
744 SamFreeMemory(AccountNameInfo);
745
746 if (OrigInfo != NULL)
747 LsaFreeMemory(OrigInfo);
748
749 if (PolicyHandle != NULL)
750 LsaClose(PolicyHandle);
751
752 if (UserHandle != NULL)
753 SamCloseHandle(UserHandle);
754
755 if (DomainHandle != NULL)
756 SamCloseHandle(DomainHandle);
757
758 if (ServerHandle != NULL)
759 SamCloseHandle(ServerHandle);
760
761 DPRINT1("SYSSETUP: SetAdministratorPassword() done (Status %08lx)\n", Status);
762
763 return Status;
764 }
765
766
767 VOID
768 SetAutoAdminLogon(VOID)
769 {
770 WCHAR szAutoAdminLogon[2];
771 HKEY hKey = NULL;
772 DWORD dwType;
773 DWORD dwSize;
774 LONG lError;
775
776 lError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
777 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",
778 0,
779 KEY_READ | KEY_WRITE,
780 &hKey);
781 if (lError != ERROR_SUCCESS)
782 return;
783
784 dwSize = 2 * sizeof(WCHAR);
785 lError = RegQueryValueExW(hKey,
786 L"AutoAdminLogon",
787 NULL,
788 &dwType,
789 (LPBYTE)szAutoAdminLogon,
790 &dwSize);
791 if (lError != ERROR_SUCCESS)
792 goto done;
793
794 if (wcscmp(szAutoAdminLogon, L"1") == 0)
795 {
796 RegSetValueExW(hKey,
797 L"DefaultDomainName",
798 0,
799 REG_SZ,
800 (LPBYTE)AdminInfo.Domain,
801 (wcslen(AdminInfo.Domain) + 1) * sizeof(WCHAR));
802
803 RegSetValueExW(hKey,
804 L"DefaultUserName",
805 0,
806 REG_SZ,
807 (LPBYTE)AdminInfo.Name,
808 (wcslen(AdminInfo.Name) + 1) * sizeof(WCHAR));
809
810 RegSetValueExW(hKey,
811 L"DefaultPassword",
812 0,
813 REG_SZ,
814 (LPBYTE)AdminInfo.Password,
815 (wcslen(AdminInfo.Password) + 1) * sizeof(WCHAR));
816 }
817
818 done:
819 if (hKey != NULL)
820 RegCloseKey(hKey);
821 }
822
823
824 /* EOF */
825