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
9 /* INCLUDES *****************************************************************/
21 /* FUNCTIONS ****************************************************************/
24 SetAccountDomain(LPCWSTR DomainName
,
27 PPOLICY_ACCOUNT_DOMAIN_INFO OrigInfo
= NULL
;
28 POLICY_ACCOUNT_DOMAIN_INFO Info
;
29 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
30 LSA_HANDLE PolicyHandle
;
32 SAM_HANDLE ServerHandle
= NULL
;
33 SAM_HANDLE DomainHandle
= NULL
;
34 DOMAIN_NAME_INFORMATION DomainNameInfo
;
38 DPRINT("SYSSETUP: SetAccountDomain\n");
40 memset(&ObjectAttributes
, 0, sizeof(LSA_OBJECT_ATTRIBUTES
));
41 ObjectAttributes
.Length
= sizeof(LSA_OBJECT_ATTRIBUTES
);
43 Status
= LsaOpenPolicy(NULL
,
45 POLICY_VIEW_LOCAL_INFORMATION
| POLICY_TRUST_ADMIN
,
47 if (Status
!= STATUS_SUCCESS
)
49 DPRINT("LsaOpenPolicy failed (Status: 0x%08lx)\n", Status
);
53 Status
= LsaQueryInformationPolicy(PolicyHandle
,
54 PolicyAccountDomainInformation
,
56 if (Status
== STATUS_SUCCESS
&& OrigInfo
!= NULL
)
58 if (DomainName
== NULL
)
60 Info
.DomainName
.Buffer
= OrigInfo
->DomainName
.Buffer
;
61 Info
.DomainName
.Length
= OrigInfo
->DomainName
.Length
;
62 Info
.DomainName
.MaximumLength
= OrigInfo
->DomainName
.MaximumLength
;
66 Info
.DomainName
.Buffer
= (LPWSTR
)DomainName
;
67 Info
.DomainName
.Length
= wcslen(DomainName
) * sizeof(WCHAR
);
68 Info
.DomainName
.MaximumLength
= Info
.DomainName
.Length
+ sizeof(WCHAR
);
71 if (DomainSid
== NULL
)
72 Info
.DomainSid
= OrigInfo
->DomainSid
;
74 Info
.DomainSid
= DomainSid
;
78 Info
.DomainName
.Buffer
= (LPWSTR
)DomainName
;
79 Info
.DomainName
.Length
= wcslen(DomainName
) * sizeof(WCHAR
);
80 Info
.DomainName
.MaximumLength
= Info
.DomainName
.Length
+ sizeof(WCHAR
);
81 Info
.DomainSid
= DomainSid
;
84 Status
= LsaSetInformationPolicy(PolicyHandle
,
85 PolicyAccountDomainInformation
,
87 if (Status
!= STATUS_SUCCESS
)
89 DPRINT("LsaSetInformationPolicy failed (Status: 0x%08lx)\n", Status
);
93 LsaFreeMemory(OrigInfo
);
95 LsaClose(PolicyHandle
);
97 DomainNameInfo
.DomainName
.Length
= wcslen(DomainName
) * sizeof(WCHAR
);
98 DomainNameInfo
.DomainName
.MaximumLength
= (wcslen(DomainName
) + 1) * sizeof(WCHAR
);
99 DomainNameInfo
.DomainName
.Buffer
= (LPWSTR
)DomainName
;
101 Status
= SamConnect(NULL
,
103 SAM_SERVER_CONNECT
| SAM_SERVER_LOOKUP_DOMAIN
,
105 if (NT_SUCCESS(Status
))
107 Status
= SamOpenDomain(ServerHandle
,
108 DOMAIN_WRITE_OTHER_PARAMETERS
,
111 if (NT_SUCCESS(Status
))
113 Status
= SamSetInformationDomain(DomainHandle
,
114 DomainNameInformation
,
115 (PVOID
)&DomainNameInfo
);
116 if (!NT_SUCCESS(Status
))
118 DPRINT1("SamSetInformationDomain failed (Status: 0x%08lx)\n", Status
);
121 SamCloseHandle(DomainHandle
);
125 DPRINT1("SamOpenDomain failed (Status: 0x%08lx)\n", Status
);
128 SamCloseHandle(ServerHandle
);
138 SetPrimaryDomain(LPCWSTR DomainName
,
141 PPOLICY_PRIMARY_DOMAIN_INFO OrigInfo
= NULL
;
142 POLICY_PRIMARY_DOMAIN_INFO Info
;
143 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
144 LSA_HANDLE PolicyHandle
;
147 DPRINT1("SYSSETUP: SetPrimaryDomain()\n");
149 memset(&ObjectAttributes
, 0, sizeof(LSA_OBJECT_ATTRIBUTES
));
150 ObjectAttributes
.Length
= sizeof(LSA_OBJECT_ATTRIBUTES
);
152 Status
= LsaOpenPolicy(NULL
,
154 POLICY_VIEW_LOCAL_INFORMATION
| POLICY_TRUST_ADMIN
,
156 if (Status
!= STATUS_SUCCESS
)
158 DPRINT("LsaOpenPolicy failed (Status: 0x%08lx)\n", Status
);
162 Status
= LsaQueryInformationPolicy(PolicyHandle
,
163 PolicyPrimaryDomainInformation
,
165 if (Status
== STATUS_SUCCESS
&& OrigInfo
!= NULL
)
167 if (DomainName
== NULL
)
169 Info
.Name
.Buffer
= OrigInfo
->Name
.Buffer
;
170 Info
.Name
.Length
= OrigInfo
->Name
.Length
;
171 Info
.Name
.MaximumLength
= OrigInfo
->Name
.MaximumLength
;
175 Info
.Name
.Buffer
= (LPWSTR
)DomainName
;
176 Info
.Name
.Length
= wcslen(DomainName
) * sizeof(WCHAR
);
177 Info
.Name
.MaximumLength
= Info
.Name
.Length
+ sizeof(WCHAR
);
180 if (DomainSid
== NULL
)
181 Info
.Sid
= OrigInfo
->Sid
;
183 Info
.Sid
= DomainSid
;
187 Info
.Name
.Buffer
= (LPWSTR
)DomainName
;
188 Info
.Name
.Length
= wcslen(DomainName
) * sizeof(WCHAR
);
189 Info
.Name
.MaximumLength
= Info
.Name
.Length
+ sizeof(WCHAR
);
190 Info
.Sid
= DomainSid
;
193 Status
= LsaSetInformationPolicy(PolicyHandle
,
194 PolicyPrimaryDomainInformation
,
196 if (Status
!= STATUS_SUCCESS
)
198 DPRINT("LsaSetInformationPolicy failed (Status: 0x%08lx)\n", Status
);
201 if (OrigInfo
!= NULL
)
202 LsaFreeMemory(OrigInfo
);
204 LsaClose(PolicyHandle
);
212 InstallBuiltinAccounts(VOID
)
214 LPWSTR BuiltinAccounts
[] = {
215 L
"S-1-1-0", /* Everyone */
216 L
"S-1-5-4", /* Interactive */
217 L
"S-1-5-6", /* Service */
218 L
"S-1-5-19", /* Local Service */
219 L
"S-1-5-20", /* Network Service */
220 L
"S-1-5-32-544", /* Administrators */
221 L
"S-1-5-32-545", /* Users */
222 L
"S-1-5-32-547", /* Power Users */
223 L
"S-1-5-32-551", /* Backup Operators */
224 L
"S-1-5-32-555"}; /* Remote Desktop Users */
225 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
227 LSA_HANDLE PolicyHandle
= NULL
;
228 LSA_HANDLE AccountHandle
= NULL
;
232 DPRINT("InstallBuiltinAccounts()\n");
234 memset(&ObjectAttributes
, 0, sizeof(LSA_OBJECT_ATTRIBUTES
));
236 Status
= LsaOpenPolicy(NULL
,
238 POLICY_CREATE_ACCOUNT
,
240 if (!NT_SUCCESS(Status
))
242 DPRINT1("LsaOpenPolicy failed (Status %08lx)\n", Status
);
246 for (i
= 0; i
< 10; i
++)
248 if (!ConvertStringSidToSid(BuiltinAccounts
[i
], &AccountSid
))
250 DPRINT1("ConvertStringSidToSid(%S) failed: %lu\n", BuiltinAccounts
[i
], GetLastError());
254 Status
= LsaCreateAccount(PolicyHandle
,
258 if (NT_SUCCESS(Status
))
260 LsaClose(AccountHandle
);
263 LocalFree(AccountSid
);
266 LsaClose(PolicyHandle
);
272 InstallPrivileges(VOID
)
274 HINF hSecurityInf
= INVALID_HANDLE_VALUE
;
275 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
276 WCHAR szPrivilegeString
[256];
277 WCHAR szSidString
[256];
278 INFCONTEXT InfContext
;
280 PRIVILEGE_SET PrivilegeSet
;
283 LSA_HANDLE PolicyHandle
= NULL
;
284 LSA_HANDLE AccountHandle
;
286 DPRINT("InstallPrivileges()\n");
288 hSecurityInf
= SetupOpenInfFileW(L
"defltws.inf", //szNameBuffer,
292 if (hSecurityInf
== INVALID_HANDLE_VALUE
)
294 DPRINT1("SetupOpenInfFileW failed\n");
298 memset(&ObjectAttributes
, 0, sizeof(LSA_OBJECT_ATTRIBUTES
));
300 Status
= LsaOpenPolicy(NULL
,
302 POLICY_CREATE_ACCOUNT
,
304 if (!NT_SUCCESS(Status
))
306 DPRINT1("LsaOpenPolicy failed (Status %08lx)\n", Status
);
310 if (!SetupFindFirstLineW(hSecurityInf
,
315 DPRINT1("SetupFindfirstLineW failed\n");
319 PrivilegeSet
.PrivilegeCount
= 1;
320 PrivilegeSet
.Control
= 0;
324 /* Retrieve the privilege name */
325 if (!SetupGetStringFieldW(&InfContext
,
331 DPRINT1("SetupGetStringFieldW() failed\n");
334 DPRINT("Privilege: %S\n", szPrivilegeString
);
336 if (!LookupPrivilegeValueW(NULL
,
338 &(PrivilegeSet
.Privilege
[0].Luid
)))
340 DPRINT1("LookupPrivilegeNameW() failed\n");
344 PrivilegeSet
.Privilege
[0].Attributes
= 0;
346 for (i
= 0; i
< SetupGetFieldCount(&InfContext
); i
++)
348 if (!SetupGetStringFieldW(&InfContext
,
354 DPRINT1("SetupGetStringFieldW() failed\n");
357 DPRINT("SID: %S\n", szSidString
);
359 if (!ConvertStringSidToSid(szSidString
, &AccountSid
))
361 DPRINT1("ConvertStringSidToSid(%S) failed: %lu\n", szSidString
, GetLastError());
365 Status
= LsaOpenAccount(PolicyHandle
,
367 ACCOUNT_VIEW
| ACCOUNT_ADJUST_PRIVILEGES
,
369 if (NT_SUCCESS(Status
))
371 Status
= LsaAddPrivilegesToAccount(AccountHandle
,
373 if (!NT_SUCCESS(Status
))
375 DPRINT1("LsaAddPrivilegesToAccount() failed (Status %08lx)\n", Status
);
378 LsaClose(AccountHandle
);
381 LocalFree(AccountSid
);
385 while (SetupFindNextLine(&InfContext
, &InfContext
));
388 if (PolicyHandle
!= NULL
)
389 LsaClose(PolicyHandle
);
391 if (hSecurityInf
!= INVALID_HANDLE_VALUE
)
392 SetupCloseInfFile(hSecurityInf
);
397 InstallSecurity(VOID
)
399 InstallBuiltinAccounts();
403 SetPrimaryDomain(L
"WORKGROUP", NULL
);
408 SetAdministratorPassword(LPCWSTR Password
)
410 PPOLICY_ACCOUNT_DOMAIN_INFO OrigInfo
= NULL
;
411 PUSER_ACCOUNT_NAME_INFORMATION AccountNameInfo
= NULL
;
412 USER_SET_PASSWORD_INFORMATION PasswordInfo
;
413 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
414 LSA_HANDLE PolicyHandle
= NULL
;
415 SAM_HANDLE ServerHandle
= NULL
;
416 SAM_HANDLE DomainHandle
= NULL
;
417 SAM_HANDLE UserHandle
= NULL
;
420 DPRINT("SYSSETUP: SetAdministratorPassword(%p)\n", Password
);
422 memset(&ObjectAttributes
, 0, sizeof(LSA_OBJECT_ATTRIBUTES
));
423 ObjectAttributes
.Length
= sizeof(LSA_OBJECT_ATTRIBUTES
);
425 Status
= LsaOpenPolicy(NULL
,
427 POLICY_VIEW_LOCAL_INFORMATION
| POLICY_TRUST_ADMIN
,
429 if (Status
!= STATUS_SUCCESS
)
431 DPRINT1("LsaOpenPolicy() failed (Status: 0x%08lx)\n", Status
);
435 Status
= LsaQueryInformationPolicy(PolicyHandle
,
436 PolicyAccountDomainInformation
,
438 if (!NT_SUCCESS(Status
))
440 DPRINT1("LsaQueryInformationPolicy() failed (Status: 0x%08lx)\n", Status
);
444 Status
= SamConnect(NULL
,
446 SAM_SERVER_CONNECT
| SAM_SERVER_LOOKUP_DOMAIN
,
448 if (!NT_SUCCESS(Status
))
450 DPRINT1("SamConnect() failed (Status: 0x%08lx)\n", Status
);
454 Status
= SamOpenDomain(ServerHandle
,
458 if (!NT_SUCCESS(Status
))
460 DPRINT1("SamOpenDomain() failed (Status: 0x%08lx)\n", Status
);
464 Status
= SamOpenUser(DomainHandle
,
465 USER_FORCE_PASSWORD_CHANGE
| USER_READ_GENERAL
,
466 DOMAIN_USER_RID_ADMIN
,
468 if (!NT_SUCCESS(Status
))
470 DPRINT1("SamOpenUser() failed (Status %08lx)\n", Status
);
474 RtlInitUnicodeString(&PasswordInfo
.Password
, Password
);
475 PasswordInfo
.PasswordExpired
= FALSE
;
477 Status
= SamSetInformationUser(UserHandle
,
478 UserSetPasswordInformation
,
479 (PVOID
)&PasswordInfo
);
480 if (!NT_SUCCESS(Status
))
482 DPRINT1("SamSetInformationUser() failed (Status %08lx)\n", Status
);
486 Status
= SamQueryInformationUser(UserHandle
,
487 UserAccountNameInformation
,
488 (PVOID
*)&AccountNameInfo
);
489 if (!NT_SUCCESS(Status
))
491 DPRINT1("SamSetInformationUser() failed (Status %08lx)\n", Status
);
495 AdminInfo
.Name
= RtlAllocateHeap(RtlGetProcessHeap(),
497 AccountNameInfo
->UserName
.Length
+ sizeof(WCHAR
));
498 if (AdminInfo
.Name
!= NULL
)
499 RtlCopyMemory(AdminInfo
.Name
,
500 AccountNameInfo
->UserName
.Buffer
,
501 AccountNameInfo
->UserName
.Length
);
503 AdminInfo
.Domain
= RtlAllocateHeap(RtlGetProcessHeap(),
505 OrigInfo
->DomainName
.Length
+ sizeof(WCHAR
));
506 if (AdminInfo
.Domain
!= NULL
)
507 RtlCopyMemory(AdminInfo
.Domain
,
508 OrigInfo
->DomainName
.Buffer
,
509 OrigInfo
->DomainName
.Length
);
511 AdminInfo
.Password
= RtlAllocateHeap(RtlGetProcessHeap(),
513 (wcslen(Password
) + 1) * sizeof(WCHAR
));
514 if (AdminInfo
.Password
!= NULL
)
515 wcscpy(AdminInfo
.Password
, Password
);
517 DPRINT("Administrator Name: %S\n", AdminInfo
.Name
);
518 DPRINT("Administrator Domain: %S\n", AdminInfo
.Domain
);
519 DPRINT("Administrator Password: %S\n", AdminInfo
.Password
);
522 if (AccountNameInfo
!= NULL
)
523 SamFreeMemory(AccountNameInfo
);
525 if (OrigInfo
!= NULL
)
526 LsaFreeMemory(OrigInfo
);
528 if (PolicyHandle
!= NULL
)
529 LsaClose(PolicyHandle
);
531 if (UserHandle
!= NULL
)
532 SamCloseHandle(UserHandle
);
534 if (DomainHandle
!= NULL
)
535 SamCloseHandle(DomainHandle
);
537 if (ServerHandle
!= NULL
)
538 SamCloseHandle(ServerHandle
);
540 DPRINT1("SYSSETUP: SetAdministratorPassword() done (Status %08lx)\n", Status
);
547 SetAutoAdminLogon(VOID
)
549 WCHAR szAutoAdminLogon
[2];
555 lError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
556 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",
558 KEY_READ
| KEY_WRITE
,
560 if (lError
!= ERROR_SUCCESS
)
563 dwSize
= 2 * sizeof(WCHAR
);
564 lError
= RegQueryValueExW(hKey
,
568 (LPBYTE
)szAutoAdminLogon
,
570 if (lError
!= ERROR_SUCCESS
)
573 if (wcscmp(szAutoAdminLogon
, L
"1") == 0)
579 (LPBYTE
)AdminInfo
.Domain
,
580 (wcslen(AdminInfo
.Domain
) + 1) * sizeof(WCHAR
));
586 (LPBYTE
)AdminInfo
.Name
,
587 (wcslen(AdminInfo
.Name
) + 1) * sizeof(WCHAR
));
593 (LPBYTE
)AdminInfo
.Password
,
594 (wcslen(AdminInfo
.Password
) + 1) * sizeof(WCHAR
));