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
;
228 WCHAR szAccountKeyName
[32];
229 HKEY hAccountKey
= NULL
;
230 HKEY hNamesKey
= NULL
;
232 /* Initialize fixed user data */
233 memset(&FixedUserData
, 0, sizeof(SAM_USER_FIXED_DATA
));
234 FixedUserData
.Version
= 1;
236 FixedUserData
.UserId
= ulRelativeId
;
238 swprintf(szAccountKeyName
, L
"Users\\%08lX", ulRelativeId
);
240 if (!RegCreateKeyExW(hDomainKey
,
244 REG_OPTION_NON_VOLATILE
,
250 RegSetValueEx(hAccountKey
,
254 (LPVOID
)&FixedUserData
,
255 sizeof(SAM_USER_FIXED_DATA
));
257 RegSetValueEx(hAccountKey
,
261 (LPVOID
)lpAccountName
,
262 (wcslen(lpAccountName
) + 1) * sizeof(WCHAR
));
264 RegCloseKey(hAccountKey
);
267 if (!RegOpenKeyExW(hDomainKey
,
273 RegSetValueEx(hNamesKey
,
277 (LPVOID
)&ulRelativeId
,
280 RegCloseKey(hNamesKey
);
288 SampCreateDomain(IN HKEY hDomainsKey
,
289 IN LPCWSTR lpKeyName
,
290 IN LPCWSTR lpDomainName
,
292 OUT PHKEY lpDomainKey
)
294 SAM_DOMAIN_FIXED_DATA FixedData
;
295 LPWSTR lpEmptyString
= L
"";
297 HKEY hDomainKey
= NULL
;
298 HKEY hAliasesKey
= NULL
;
299 HKEY hGroupsKey
= NULL
;
300 HKEY hUsersKey
= NULL
;
301 HKEY hNamesKey
= NULL
;
303 if (lpDomainKey
!= NULL
)
306 /* Initialize the fixed domain data */
307 memset(&FixedData
, 0, sizeof(SAM_DOMAIN_FIXED_DATA
));
308 FixedData
.Version
= 1;
309 NtQuerySystemTime(&FixedData
.CreationTime
);
310 FixedData
.DomainModifiedCount
.QuadPart
= 0;
311 // FixedData.MaxPasswordAge // 6 Weeks
312 FixedData
.MinPasswordAge
.QuadPart
= 0; // Now
313 // FixedData.ForceLogoff
314 // FixedData.LockoutDuration // 30 minutes
315 // FixedData.LockoutObservationWindow // 30 minutes
316 FixedData
.ModifiedCountAtLastPromotion
.QuadPart
= 0;
317 FixedData
.NextRid
= 1000;
318 FixedData
.PasswordProperties
= 0;
319 FixedData
.MinPasswordLength
= 0;
320 FixedData
.PasswordHistoryLength
= 0;
321 FixedData
.LockoutThreshold
= 0;
322 FixedData
.DomainServerState
= DomainServerEnabled
;
323 FixedData
.DomainServerRole
= DomainServerRolePrimary
;
324 FixedData
.UasCompatibilityRequired
= TRUE
;
326 if (RegCreateKeyExW(hDomainsKey
,
330 REG_OPTION_NON_VOLATILE
,
337 /* Set the fixed data value */
338 if (RegSetValueEx(hDomainKey
,
343 sizeof(SAM_DOMAIN_FIXED_DATA
)))
346 if (lpDomainSid
!= NULL
)
348 RegSetValueEx(hDomainKey
,
352 (LPVOID
)lpDomainName
,
353 (wcslen(lpDomainName
) + 1) * sizeof(WCHAR
));
355 RegSetValueEx(hDomainKey
,
360 RtlLengthSid(lpDomainSid
));
363 RegSetValueEx(hDomainKey
,
367 (LPVOID
)lpEmptyString
,
370 RegSetValueEx(hDomainKey
,
371 L
"ReplicaSourceNodeName",
374 (LPVOID
)lpEmptyString
,
377 /* Create the Alias container */
378 if (!RegCreateKeyExW(hDomainKey
,
382 REG_OPTION_NON_VOLATILE
,
388 if (!RegCreateKeyExW(hAliasesKey
,
392 REG_OPTION_NON_VOLATILE
,
397 RegCloseKey(hNamesKey
);
399 RegCloseKey(hAliasesKey
);
402 /* Create the Groups container */
403 if (!RegCreateKeyExW(hDomainKey
,
407 REG_OPTION_NON_VOLATILE
,
413 if (!RegCreateKeyExW(hGroupsKey
,
417 REG_OPTION_NON_VOLATILE
,
422 RegCloseKey(hNamesKey
);
424 RegCloseKey(hGroupsKey
);
428 /* Create the Users container */
429 if (!RegCreateKeyExW(hDomainKey
,
433 REG_OPTION_NON_VOLATILE
,
439 if (!RegCreateKeyExW(hUsersKey
,
443 REG_OPTION_NON_VOLATILE
,
448 RegCloseKey(hNamesKey
);
450 RegCloseKey(hUsersKey
);
453 if (lpDomainKey
!= NULL
)
454 *lpDomainKey
= hDomainKey
;
461 SampGetAccountDomainInfo(PPOLICY_ACCOUNT_DOMAIN_INFO
*AccountDomainInfo
)
463 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
464 LSA_HANDLE PolicyHandle
;
467 TRACE("SampGetAccountDomainInfo\n");
469 memset(&ObjectAttributes
, 0, sizeof(LSA_OBJECT_ATTRIBUTES
));
470 ObjectAttributes
.Length
= sizeof(LSA_OBJECT_ATTRIBUTES
);
472 Status
= LsaOpenPolicy(NULL
,
476 if (Status
!= STATUS_SUCCESS
)
478 ERR("LsaOpenPolicy failed (Status: 0x%08lx)\n", Status
);
482 Status
= LsaQueryInformationPolicy(PolicyHandle
,
483 PolicyAccountDomainInformation
,
484 (PVOID
*)AccountDomainInfo
);
486 LsaClose(PolicyHandle
);
493 SampInitializeSAM(VOID
)
495 PPOLICY_ACCOUNT_DOMAIN_INFO AccountDomainInfo
= NULL
;
498 HKEY hDomainsKey
= NULL
;
499 HKEY hDomainKey
= NULL
;
500 PSID pBuiltinSid
= NULL
;
505 TRACE("SampInitializeSAM() called\n");
507 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
511 REG_OPTION_NON_VOLATILE
,
517 ERR("Failed to create 'Sam' key! (Error %lu)\n", GetLastError());
521 if (RegCreateKeyExW(hSamKey
,
525 REG_OPTION_NON_VOLATILE
,
531 ERR("Failed to create 'Domains' key! (Error %lu)\n", GetLastError());
536 RegCloseKey(hSamKey
);
539 /* Create and initialize the Builtin Domain SID */
540 pBuiltinSid
= RtlAllocateHeap(RtlGetProcessHeap(), 0, RtlLengthRequiredSid(1));
541 if (pBuiltinSid
== NULL
)
543 ERR("Failed to alloacte the Builtin Domain SID\n");
548 RtlInitializeSid(pBuiltinSid
, &SecurityNtAuthority
, 1);
549 *(RtlSubAuthoritySid(pBuiltinSid
, 0)) = SECURITY_BUILTIN_DOMAIN_RID
;
551 /* Get account domain information */
552 Status
= SampGetAccountDomainInfo(&AccountDomainInfo
);
553 if (!NT_SUCCESS(Status
))
555 ERR("SampGetAccountDomainInfo failed (Status %08lx)\n", Status
);
560 /* Create the Builtin domain */
561 if (SampCreateDomain(hDomainsKey
,
567 SampCreateAliasAccount(hDomainKey
,
569 L
"Testabc1234567890",
570 DOMAIN_ALIAS_RID_ADMINS
);
572 SampCreateAliasAccount(hDomainKey
,
575 DOMAIN_ALIAS_RID_USERS
);
577 SampCreateAliasAccount(hDomainKey
,
580 DOMAIN_ALIAS_RID_GUESTS
);
582 SampCreateAliasAccount(hDomainKey
,
584 L
"Power Users Group",
585 DOMAIN_ALIAS_RID_POWER_USERS
);
588 pSid
= AppendRidToSid(AccountDomainInfo
->DomainSid
,
589 DOMAIN_USER_RID_ADMIN
);
592 SampAddMemberToAlias(hDomainKey
,
593 DOMAIN_ALIAS_RID_ADMINS
,
596 RtlFreeHeap(RtlGetProcessHeap(), 0, pSid
);
600 RegCloseKey(hDomainKey
);
603 /* Create the Account domain */
604 if (SampCreateDomain(hDomainsKey
,
607 AccountDomainInfo
->DomainSid
,
610 SampCreateUserAccount(hDomainKey
,
612 DOMAIN_USER_RID_ADMIN
);
614 SampCreateUserAccount(hDomainKey
,
616 DOMAIN_USER_RID_GUEST
);
618 RegCloseKey(hDomainKey
);
622 if (AccountDomainInfo
)
623 LsaFreeMemory(AccountDomainInfo
);
626 RtlFreeHeap(RtlGetProcessHeap(), 0, pBuiltinSid
);
629 RegCloseKey(hDomainsKey
);
632 RegCloseKey(hSamKey
);
634 TRACE("SampInitializeSAM() done\n");