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
,
227 WCHAR szAccountKeyName
[32];
228 HKEY hAccountKey
= NULL
;
229 HKEY hNamesKey
= NULL
;
231 swprintf(szAccountKeyName
, L
"Users\\%08lX", ulRelativeId
);
233 if (!RegCreateKeyExW(hDomainKey
,
237 REG_OPTION_NON_VOLATILE
,
243 RegSetValueEx(hAccountKey
,
247 (LPVOID
)lpAccountName
,
248 (wcslen(lpAccountName
) + 1) * sizeof(WCHAR
));
250 RegCloseKey(hAccountKey
);
253 if (!RegOpenKeyExW(hDomainKey
,
259 RegSetValueEx(hNamesKey
,
263 (LPVOID
)&ulRelativeId
,
266 RegCloseKey(hNamesKey
);
274 SampCreateDomain(IN HKEY hDomainsKey
,
275 IN LPCWSTR lpKeyName
,
276 IN LPCWSTR lpDomainName
,
278 OUT PHKEY lpDomainKey
)
281 HKEY hDomainKey
= NULL
;
282 HKEY hAliasesKey
= NULL
;
283 HKEY hGroupsKey
= NULL
;
284 HKEY hUsersKey
= NULL
;
285 HKEY hNamesKey
= NULL
;
287 if (lpDomainKey
!= NULL
)
290 if (RegCreateKeyExW(hDomainsKey
,
294 REG_OPTION_NON_VOLATILE
,
301 if (lpDomainSid
!= NULL
)
303 RegSetValueEx(hDomainKey
,
307 (LPVOID
)lpDomainName
,
308 (wcslen(lpDomainName
) + 1) * sizeof(WCHAR
));
310 RegSetValueEx(hDomainKey
,
315 RtlLengthSid(lpDomainSid
));
318 /* Create the Alias container */
319 if (!RegCreateKeyExW(hDomainKey
,
323 REG_OPTION_NON_VOLATILE
,
329 if (!RegCreateKeyExW(hAliasesKey
,
333 REG_OPTION_NON_VOLATILE
,
338 RegCloseKey(hNamesKey
);
340 RegCloseKey(hAliasesKey
);
343 /* Create the Groups container */
344 if (!RegCreateKeyExW(hDomainKey
,
348 REG_OPTION_NON_VOLATILE
,
354 if (!RegCreateKeyExW(hGroupsKey
,
358 REG_OPTION_NON_VOLATILE
,
363 RegCloseKey(hNamesKey
);
365 RegCloseKey(hGroupsKey
);
369 /* Create the Users container */
370 if (!RegCreateKeyExW(hDomainKey
,
374 REG_OPTION_NON_VOLATILE
,
380 if (!RegCreateKeyExW(hUsersKey
,
384 REG_OPTION_NON_VOLATILE
,
389 RegCloseKey(hNamesKey
);
391 RegCloseKey(hUsersKey
);
394 if (lpDomainKey
!= NULL
)
395 *lpDomainKey
= hDomainKey
;
402 SampGetAccountDomainInfo(PPOLICY_ACCOUNT_DOMAIN_INFO
*AccountDomainInfo
)
404 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
405 LSA_HANDLE PolicyHandle
;
408 TRACE("SampGetAccountDomainInfo\n");
410 memset(&ObjectAttributes
, 0, sizeof(LSA_OBJECT_ATTRIBUTES
));
411 ObjectAttributes
.Length
= sizeof(LSA_OBJECT_ATTRIBUTES
);
413 Status
= LsaOpenPolicy(NULL
,
417 if (Status
!= STATUS_SUCCESS
)
419 ERR("LsaOpenPolicy failed (Status: 0x%08lx)\n", Status
);
423 Status
= LsaQueryInformationPolicy(PolicyHandle
,
424 PolicyAccountDomainInformation
,
425 (PVOID
*)AccountDomainInfo
);
427 LsaClose(PolicyHandle
);
434 SampInitializeSAM(VOID
)
436 PPOLICY_ACCOUNT_DOMAIN_INFO AccountDomainInfo
= NULL
;
439 HKEY hDomainsKey
= NULL
;
440 HKEY hDomainKey
= NULL
;
441 PSID pBuiltinSid
= NULL
;
446 TRACE("SampInitializeSAM() called\n");
448 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
452 REG_OPTION_NON_VOLATILE
,
458 ERR("Failed to create 'Sam' key! (Error %lu)\n", GetLastError());
462 if (RegCreateKeyExW(hSamKey
,
466 REG_OPTION_NON_VOLATILE
,
472 ERR("Failed to create 'Domains' key! (Error %lu)\n", GetLastError());
477 RegCloseKey(hSamKey
);
480 /* Create and initialize the Builtin Domain SID */
481 pBuiltinSid
= RtlAllocateHeap(RtlGetProcessHeap(), 0, RtlLengthRequiredSid(1));
482 if (pBuiltinSid
== NULL
)
484 ERR("Failed to alloacte the Builtin Domain SID\n");
489 RtlInitializeSid(pBuiltinSid
, &SecurityNtAuthority
, 1);
490 *(RtlSubAuthoritySid(pBuiltinSid
, 0)) = SECURITY_BUILTIN_DOMAIN_RID
;
492 /* Get account domain information */
493 Status
= SampGetAccountDomainInfo(&AccountDomainInfo
);
494 if (!NT_SUCCESS(Status
))
496 ERR("SampGetAccountDomainInfo failed (Status %08lx)\n", Status
);
501 /* Create the Builtin domain */
502 if (SampCreateDomain(hDomainsKey
,
508 SampCreateAliasAccount(hDomainKey
,
511 DOMAIN_ALIAS_RID_ADMINS
);
513 SampCreateAliasAccount(hDomainKey
,
516 DOMAIN_ALIAS_RID_USERS
);
518 SampCreateAliasAccount(hDomainKey
,
521 DOMAIN_ALIAS_RID_GUESTS
);
523 SampCreateAliasAccount(hDomainKey
,
526 DOMAIN_ALIAS_RID_POWER_USERS
);
529 pSid
= AppendRidToSid(AccountDomainInfo
->DomainSid
,
530 DOMAIN_USER_RID_ADMIN
);
533 SampAddMemberToAlias(hDomainKey
,
534 DOMAIN_ALIAS_RID_ADMINS
,
537 RtlFreeHeap(RtlGetProcessHeap(), 0, pSid
);
541 RegCloseKey(hDomainKey
);
544 /* Create the Account domain */
545 if (SampCreateDomain(hDomainsKey
,
548 AccountDomainInfo
->DomainSid
,
551 SampCreateUserAccount(hDomainKey
,
553 DOMAIN_USER_RID_ADMIN
);
555 SampCreateUserAccount(hDomainKey
,
557 DOMAIN_USER_RID_GUEST
);
559 RegCloseKey(hDomainKey
);
563 if (AccountDomainInfo
)
564 LsaFreeMemory(AccountDomainInfo
);
567 RtlFreeHeap(RtlGetProcessHeap(), 0, pBuiltinSid
);
570 RegCloseKey(hDomainsKey
);
573 RegCloseKey(hSamKey
);
575 TRACE("SampInitializeSAM() done\n");