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 SampCreateUserAccount(HKEY hDomainKey
,
63 LPCWSTR lpAccountName
,
67 WCHAR szUserKeyName
[32];
69 HKEY hNamesKey
= NULL
;
71 swprintf(szUserKeyName
, L
"Users\\%08lX", ulRelativeId
);
73 if (!RegCreateKeyExW(hDomainKey
,
77 REG_OPTION_NON_VOLATILE
,
83 RegSetValueEx(hUserKey
,
87 (LPVOID
)lpAccountName
,
88 (wcslen(lpAccountName
) + 1) * sizeof(WCHAR
));
90 RegCloseKey(hUserKey
);
93 if (!RegOpenKeyExW(hDomainKey
,
99 RegSetValueEx(hNamesKey
,
103 (LPVOID
)&ulRelativeId
,
106 RegCloseKey(hNamesKey
);
114 SampCreateDomain(IN HKEY hDomainsKey
,
115 IN LPCWSTR lpKeyName
,
116 IN LPCWSTR lpDomainName
,
118 OUT PHKEY lpDomainKey
)
121 HKEY hDomainKey
= NULL
;
122 HKEY hAliasKey
= NULL
;
123 HKEY hGroupsKey
= NULL
;
124 HKEY hUsersKey
= NULL
;
125 HKEY hNamesKey
= NULL
;
127 if (lpDomainKey
!= NULL
)
130 if (RegCreateKeyExW(hDomainsKey
,
134 REG_OPTION_NON_VOLATILE
,
141 if (lpDomainSid
!= NULL
)
143 RegSetValueEx(hDomainKey
,
147 (LPVOID
)lpDomainName
,
148 (wcslen(lpDomainName
) + 1) * sizeof(WCHAR
));
150 RegSetValueEx(hDomainKey
,
155 RtlLengthSid(lpDomainSid
));
158 /* Create the Alias container */
159 if (!RegCreateKeyExW(hDomainKey
,
163 REG_OPTION_NON_VOLATILE
,
169 if (!RegCreateKeyExW(hAliasKey
,
173 REG_OPTION_NON_VOLATILE
,
178 RegCloseKey(hNamesKey
);
180 RegCloseKey(hAliasKey
);
183 /* Create the Groups container */
184 if (!RegCreateKeyExW(hDomainKey
,
188 REG_OPTION_NON_VOLATILE
,
194 if (!RegCreateKeyExW(hGroupsKey
,
198 REG_OPTION_NON_VOLATILE
,
203 RegCloseKey(hNamesKey
);
205 RegCloseKey(hGroupsKey
);
209 /* Create the Users container */
210 if (!RegCreateKeyExW(hDomainKey
,
214 REG_OPTION_NON_VOLATILE
,
220 if (!RegCreateKeyExW(hUsersKey
,
224 REG_OPTION_NON_VOLATILE
,
229 RegCloseKey(hNamesKey
);
231 RegCloseKey(hUsersKey
);
234 if (lpDomainKey
!= NULL
)
235 *lpDomainKey
= hDomainKey
;
242 SampGetAccountDomainInfo(PPOLICY_ACCOUNT_DOMAIN_INFO
*AccountDomainInfo
)
244 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
245 LSA_HANDLE PolicyHandle
;
248 TRACE("SampGetAccountDomainInfo\n");
250 memset(&ObjectAttributes
, 0, sizeof(LSA_OBJECT_ATTRIBUTES
));
251 ObjectAttributes
.Length
= sizeof(LSA_OBJECT_ATTRIBUTES
);
253 Status
= LsaOpenPolicy(NULL
,
257 if (Status
!= STATUS_SUCCESS
)
259 ERR("LsaOpenPolicy failed (Status: 0x%08lx)\n", Status
);
263 Status
= LsaQueryInformationPolicy(PolicyHandle
,
264 PolicyAccountDomainInformation
,
265 (PVOID
*)AccountDomainInfo
);
267 LsaClose(PolicyHandle
);
274 SampInitializeSAM(VOID
)
276 PPOLICY_ACCOUNT_DOMAIN_INFO AccountDomainInfo
= NULL
;
279 HKEY hDomainsKey
= NULL
;
280 HKEY hDomainKey
= NULL
;
281 PSID pBuiltinSid
= NULL
;
285 TRACE("SampInitializeSAM() called\n");
287 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
291 REG_OPTION_NON_VOLATILE
,
297 ERR("Failed to create 'Sam' key! (Error %lu)\n", GetLastError());
301 if (RegCreateKeyExW(hSamKey
,
305 REG_OPTION_NON_VOLATILE
,
311 ERR("Failed to create 'Domains' key! (Error %lu)\n", GetLastError());
316 RegCloseKey(hSamKey
);
319 /* Create and initialize the Builtin Domain SID */
320 pBuiltinSid
= RtlAllocateHeap(RtlGetProcessHeap(), 0, RtlLengthRequiredSid(1));
321 if (pBuiltinSid
== NULL
)
323 ERR("Failed to alloacte the Builtin Domain SID\n");
328 RtlInitializeSid(pBuiltinSid
, &SecurityNtAuthority
, 1);
329 *(RtlSubAuthoritySid(pBuiltinSid
, 0)) = SECURITY_BUILTIN_DOMAIN_RID
;
331 /* Get account domain information */
332 Status
= SampGetAccountDomainInfo(&AccountDomainInfo
);
333 if (!NT_SUCCESS(Status
))
335 ERR("SampGetAccountDomainInfo failed (Status %08lx)\n", Status
);
340 /* Create the Builtin domain */
341 if (SampCreateDomain(hDomainsKey
,
348 RegCloseKey(hDomainKey
);
351 /* Create the Account domain */
352 if (SampCreateDomain(hDomainsKey
,
355 AccountDomainInfo
->DomainSid
,
358 SampCreateUserAccount(hDomainKey
,
360 DOMAIN_USER_RID_ADMIN
);
362 SampCreateUserAccount(hDomainKey
,
364 DOMAIN_USER_RID_GUEST
);
366 RegCloseKey(hDomainKey
);
370 if (AccountDomainInfo
)
371 LsaFreeMemory(AccountDomainInfo
);
374 RtlFreeHeap(RtlGetProcessHeap(), 0, pBuiltinSid
);
377 RegCloseKey(hDomainsKey
);
380 RegCloseKey(hSamKey
);
382 TRACE("SampInitializeSAM() done\n");