2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: Security Account Manager (SAM) Server
4 * FILE: reactos/dll/win32/samsrv/setup.c
5 * PURPOSE: Registry setup routines
7 * PROGRAMMERS: Eric Kohl
10 /* INCLUDES ****************************************************************/
14 WINE_DEFAULT_DEBUG_CHANNEL(samsrv
);
16 /* GLOBALS *****************************************************************/
18 SID_IDENTIFIER_AUTHORITY SecurityNtAuthority
= {SECURITY_NT_AUTHORITY
};
20 /* FUNCTIONS ***************************************************************/
23 SampIsSetupRunning(VOID
)
31 TRACE("SampIsSetupRunning()\n");
34 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
39 if (dwError
!= ERROR_SUCCESS
)
43 dwSize
= sizeof(DWORD
);
44 dwError
= RegQueryValueExW(hKey
,
51 /* Close key, and check if returned values are correct */
53 if (dwError
!= ERROR_SUCCESS
|| dwType
!= REG_DWORD
|| dwSize
!= sizeof(DWORD
))
56 TRACE("SampIsSetupRunning() returns %s\n", (dwSetupType
!= 0) ? "TRUE" : "FALSE");
57 return (dwSetupType
!= 0);
62 AppendRidToSid(PSID SrcSid
,
65 ULONG Rids
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
70 RidCount
= *RtlSubAuthorityCountSid(SrcSid
);
74 for (i
= 0; i
< RidCount
; i
++)
75 Rids
[i
] = *RtlSubAuthoritySid(SrcSid
, i
);
80 RtlAllocateAndInitializeSid(RtlIdentifierAuthoritySid(SrcSid
),
97 SampAddMemberToAlias(HKEY hDomainKey
,
102 LPWSTR MemberSidString
= NULL
;
103 WCHAR szKeyName
[256];
106 ConvertSidToStringSidW(MemberSid
, &MemberSidString
);
108 swprintf(szKeyName
, L
"Aliases\\%08lX\\Members", AliasId
);
110 if (!RegCreateKeyExW(hDomainKey
,
114 REG_OPTION_NON_VOLATILE
,
120 RegSetValueEx(hMembersKey
,
125 RtlLengthSid(MemberSid
));
127 RegCloseKey(hMembersKey
);
130 swprintf(szKeyName
, L
"Aliases\\Members\\%s", MemberSidString
);
132 if (!RegCreateKeyExW(hDomainKey
,
136 REG_OPTION_NON_VOLATILE
,
142 swprintf(szKeyName
, L
"%08lX", AliasId
);
144 RegSetValueEx(hMembersKey
,
149 RtlLengthSid(MemberSid
));
151 RegCloseKey(hMembersKey
);
154 if (MemberSidString
!= NULL
)
155 LocalFree(MemberSidString
);
162 SampCreateAliasAccount(HKEY hDomainKey
,
163 LPCWSTR lpAccountName
,
164 LPCWSTR lpDescription
,
168 WCHAR szAccountKeyName
[32];
169 HKEY hAccountKey
= NULL
;
170 HKEY hNamesKey
= NULL
;
172 swprintf(szAccountKeyName
, L
"Aliases\\%08lX", ulRelativeId
);
174 if (!RegCreateKeyExW(hDomainKey
,
178 REG_OPTION_NON_VOLATILE
,
184 RegSetValueEx(hAccountKey
,
188 (LPVOID
)lpAccountName
,
189 (wcslen(lpAccountName
) + 1) * sizeof(WCHAR
));
191 RegSetValueEx(hAccountKey
,
195 (LPVOID
)lpDescription
,
196 (wcslen(lpDescription
) + 1) * sizeof(WCHAR
));
198 RegCloseKey(hAccountKey
);
201 if (!RegOpenKeyExW(hDomainKey
,
207 RegSetValueEx(hNamesKey
,
211 (LPVOID
)&ulRelativeId
,
214 RegCloseKey(hNamesKey
);
222 SampCreateUserAccount(HKEY hDomainKey
,
223 LPCWSTR lpAccountName
,
226 SAM_USER_FIXED_DATA FixedUserData
;
227 LPWSTR lpEmptyString
= L
"";
229 WCHAR szAccountKeyName
[32];
230 HKEY hAccountKey
= NULL
;
231 HKEY hNamesKey
= NULL
;
233 /* Initialize fixed user data */
234 memset(&FixedUserData
, 0, sizeof(SAM_USER_FIXED_DATA
));
235 FixedUserData
.Version
= 1;
237 FixedUserData
.UserId
= ulRelativeId
;
239 swprintf(szAccountKeyName
, L
"Users\\%08lX", ulRelativeId
);
241 if (!RegCreateKeyExW(hDomainKey
,
245 REG_OPTION_NON_VOLATILE
,
251 RegSetValueEx(hAccountKey
,
255 (LPVOID
)&FixedUserData
,
256 sizeof(SAM_USER_FIXED_DATA
));
258 RegSetValueEx(hAccountKey
,
262 (LPVOID
)lpAccountName
,
263 (wcslen(lpAccountName
) + 1) * sizeof(WCHAR
));
265 RegSetValueEx(hAccountKey
,
269 (LPVOID
)lpEmptyString
,
272 RegSetValueEx(hAccountKey
,
276 (LPVOID
)lpEmptyString
,
279 RegSetValueEx(hAccountKey
,
280 L
"HomeDirectoryDrive",
283 (LPVOID
)lpEmptyString
,
286 RegSetValueEx(hAccountKey
,
290 (LPVOID
)lpEmptyString
,
293 RegSetValueEx(hAccountKey
,
297 (LPVOID
)lpEmptyString
,
300 RegSetValueEx(hAccountKey
,
304 (LPVOID
)lpEmptyString
,
307 RegSetValueEx(hAccountKey
,
311 (LPVOID
)lpEmptyString
,
314 RegSetValueEx(hAccountKey
,
318 (LPVOID
)lpEmptyString
,
321 RegCloseKey(hAccountKey
);
324 if (!RegOpenKeyExW(hDomainKey
,
330 RegSetValueEx(hNamesKey
,
334 (LPVOID
)&ulRelativeId
,
337 RegCloseKey(hNamesKey
);
345 SampCreateDomain(IN HKEY hDomainsKey
,
346 IN LPCWSTR lpKeyName
,
347 IN LPCWSTR lpDomainName
,
349 OUT PHKEY lpDomainKey
)
351 SAM_DOMAIN_FIXED_DATA FixedData
;
352 LPWSTR lpEmptyString
= L
"";
354 HKEY hDomainKey
= NULL
;
355 HKEY hAliasesKey
= NULL
;
356 HKEY hGroupsKey
= NULL
;
357 HKEY hUsersKey
= NULL
;
358 HKEY hNamesKey
= NULL
;
360 if (lpDomainKey
!= NULL
)
363 /* Initialize the fixed domain data */
364 memset(&FixedData
, 0, sizeof(SAM_DOMAIN_FIXED_DATA
));
365 FixedData
.Version
= 1;
366 NtQuerySystemTime(&FixedData
.CreationTime
);
367 FixedData
.DomainModifiedCount
.QuadPart
= 0;
368 // FixedData.MaxPasswordAge // 6 Weeks
369 FixedData
.MinPasswordAge
.QuadPart
= 0; // Now
370 // FixedData.ForceLogoff
371 // FixedData.LockoutDuration // 30 minutes
372 // FixedData.LockoutObservationWindow // 30 minutes
373 FixedData
.ModifiedCountAtLastPromotion
.QuadPart
= 0;
374 FixedData
.NextRid
= 1000;
375 FixedData
.PasswordProperties
= 0;
376 FixedData
.MinPasswordLength
= 0;
377 FixedData
.PasswordHistoryLength
= 0;
378 FixedData
.LockoutThreshold
= 0;
379 FixedData
.DomainServerState
= DomainServerEnabled
;
380 FixedData
.DomainServerRole
= DomainServerRolePrimary
;
381 FixedData
.UasCompatibilityRequired
= TRUE
;
383 if (RegCreateKeyExW(hDomainsKey
,
387 REG_OPTION_NON_VOLATILE
,
394 /* Set the fixed data value */
395 if (RegSetValueEx(hDomainKey
,
400 sizeof(SAM_DOMAIN_FIXED_DATA
)))
403 if (lpDomainSid
!= NULL
)
405 RegSetValueEx(hDomainKey
,
409 (LPVOID
)lpDomainName
,
410 (wcslen(lpDomainName
) + 1) * sizeof(WCHAR
));
412 RegSetValueEx(hDomainKey
,
417 RtlLengthSid(lpDomainSid
));
420 RegSetValueEx(hDomainKey
,
424 (LPVOID
)lpEmptyString
,
427 RegSetValueEx(hDomainKey
,
428 L
"ReplicaSourceNodeName",
431 (LPVOID
)lpEmptyString
,
434 /* Create the Alias container */
435 if (!RegCreateKeyExW(hDomainKey
,
439 REG_OPTION_NON_VOLATILE
,
445 if (!RegCreateKeyExW(hAliasesKey
,
449 REG_OPTION_NON_VOLATILE
,
454 RegCloseKey(hNamesKey
);
456 RegCloseKey(hAliasesKey
);
459 /* Create the Groups container */
460 if (!RegCreateKeyExW(hDomainKey
,
464 REG_OPTION_NON_VOLATILE
,
470 if (!RegCreateKeyExW(hGroupsKey
,
474 REG_OPTION_NON_VOLATILE
,
479 RegCloseKey(hNamesKey
);
481 RegCloseKey(hGroupsKey
);
485 /* Create the Users container */
486 if (!RegCreateKeyExW(hDomainKey
,
490 REG_OPTION_NON_VOLATILE
,
496 if (!RegCreateKeyExW(hUsersKey
,
500 REG_OPTION_NON_VOLATILE
,
505 RegCloseKey(hNamesKey
);
507 RegCloseKey(hUsersKey
);
510 if (lpDomainKey
!= NULL
)
511 *lpDomainKey
= hDomainKey
;
518 SampGetAccountDomainInfo(PPOLICY_ACCOUNT_DOMAIN_INFO
*AccountDomainInfo
)
520 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
521 LSA_HANDLE PolicyHandle
;
524 TRACE("SampGetAccountDomainInfo\n");
526 memset(&ObjectAttributes
, 0, sizeof(LSA_OBJECT_ATTRIBUTES
));
527 ObjectAttributes
.Length
= sizeof(LSA_OBJECT_ATTRIBUTES
);
529 Status
= LsaOpenPolicy(NULL
,
531 POLICY_VIEW_LOCAL_INFORMATION
,
533 if (Status
!= STATUS_SUCCESS
)
535 ERR("LsaOpenPolicy failed (Status: 0x%08lx)\n", Status
);
539 Status
= LsaQueryInformationPolicy(PolicyHandle
,
540 PolicyAccountDomainInformation
,
541 (PVOID
*)AccountDomainInfo
);
543 LsaClose(PolicyHandle
);
550 SampInitializeSAM(VOID
)
552 PPOLICY_ACCOUNT_DOMAIN_INFO AccountDomainInfo
= NULL
;
555 HKEY hDomainsKey
= NULL
;
556 HKEY hDomainKey
= NULL
;
557 PSID pBuiltinSid
= NULL
;
562 TRACE("SampInitializeSAM() called\n");
564 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
568 REG_OPTION_NON_VOLATILE
,
574 ERR("Failed to create 'Sam' key! (Error %lu)\n", GetLastError());
578 if (RegCreateKeyExW(hSamKey
,
582 REG_OPTION_NON_VOLATILE
,
588 ERR("Failed to create 'Domains' key! (Error %lu)\n", GetLastError());
593 RegCloseKey(hSamKey
);
596 /* Create and initialize the Builtin Domain SID */
597 pBuiltinSid
= RtlAllocateHeap(RtlGetProcessHeap(), 0, RtlLengthRequiredSid(1));
598 if (pBuiltinSid
== NULL
)
600 ERR("Failed to alloacte the Builtin Domain SID\n");
605 RtlInitializeSid(pBuiltinSid
, &SecurityNtAuthority
, 1);
606 *(RtlSubAuthoritySid(pBuiltinSid
, 0)) = SECURITY_BUILTIN_DOMAIN_RID
;
608 /* Get account domain information */
609 Status
= SampGetAccountDomainInfo(&AccountDomainInfo
);
610 if (!NT_SUCCESS(Status
))
612 ERR("SampGetAccountDomainInfo failed (Status %08lx)\n", Status
);
617 /* Create the Builtin domain */
618 if (SampCreateDomain(hDomainsKey
,
624 SampCreateAliasAccount(hDomainKey
,
626 L
"Testabc1234567890",
627 DOMAIN_ALIAS_RID_ADMINS
);
629 SampCreateAliasAccount(hDomainKey
,
632 DOMAIN_ALIAS_RID_USERS
);
634 SampCreateAliasAccount(hDomainKey
,
637 DOMAIN_ALIAS_RID_GUESTS
);
639 SampCreateAliasAccount(hDomainKey
,
641 L
"Power Users Group",
642 DOMAIN_ALIAS_RID_POWER_USERS
);
645 pSid
= AppendRidToSid(AccountDomainInfo
->DomainSid
,
646 DOMAIN_USER_RID_ADMIN
);
649 SampAddMemberToAlias(hDomainKey
,
650 DOMAIN_ALIAS_RID_ADMINS
,
653 RtlFreeHeap(RtlGetProcessHeap(), 0, pSid
);
657 RegCloseKey(hDomainKey
);
660 /* Create the Account domain */
661 if (SampCreateDomain(hDomainsKey
,
664 AccountDomainInfo
->DomainSid
,
667 SampCreateUserAccount(hDomainKey
,
669 DOMAIN_USER_RID_ADMIN
);
671 SampCreateUserAccount(hDomainKey
,
673 DOMAIN_USER_RID_GUEST
);
675 RegCloseKey(hDomainKey
);
679 if (AccountDomainInfo
)
680 LsaFreeMemory(AccountDomainInfo
);
683 RtlFreeHeap(RtlGetProcessHeap(), 0, pBuiltinSid
);
686 RegCloseKey(hDomainsKey
);
689 RegCloseKey(hSamKey
);
691 TRACE("SampInitializeSAM() done\n");