reshuffling of dlls
[reactos.git] / reactos / dll / win32 / userenv / misc.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2004 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id$
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS system libraries
23 * FILE: lib/userenv/misc.c
24 * PURPOSE: User profile code
25 * PROGRAMMER: Eric Kohl
26 */
27
28 #include <precomp.h>
29
30 #define NDEBUG
31 #include <debug.h>
32
33 static SID_IDENTIFIER_AUTHORITY LocalSystemAuthority = {SECURITY_NT_AUTHORITY};
34 static SID_IDENTIFIER_AUTHORITY WorldAuthority = {SECURITY_WORLD_SID_AUTHORITY};
35
36 /* FUNCTIONS ***************************************************************/
37
38 LPWSTR
39 AppendBackslash (LPWSTR String)
40 {
41 ULONG Length;
42
43 Length = lstrlenW (String);
44 if (String[Length - 1] != L'\\')
45 {
46 String[Length] = L'\\';
47 Length++;
48 String[Length] = (WCHAR)0;
49 }
50
51 return &String[Length];
52 }
53
54
55 BOOL
56 GetUserSidFromToken (HANDLE hToken,
57 PUNICODE_STRING SidString)
58 {
59 PSID_AND_ATTRIBUTES SidBuffer, nsb;
60 ULONG Length;
61 NTSTATUS Status;
62
63 Length = 256;
64 SidBuffer = LocalAlloc (LMEM_FIXED,
65 Length);
66 if (SidBuffer == NULL)
67 return FALSE;
68
69 Status = NtQueryInformationToken (hToken,
70 TokenUser,
71 (PVOID)SidBuffer,
72 Length,
73 &Length);
74 if (Status == STATUS_BUFFER_TOO_SMALL)
75 {
76 nsb = LocalReAlloc (SidBuffer,
77 Length,
78 LMEM_MOVEABLE);
79 if (nsb == NULL)
80 {
81 LocalFree((HLOCAL)SidBuffer);
82 return FALSE;
83 }
84
85 SidBuffer = nsb;
86 Status = NtQueryInformationToken (hToken,
87 TokenUser,
88 (PVOID)SidBuffer,
89 Length,
90 &Length);
91 }
92
93 if (!NT_SUCCESS (Status))
94 {
95 LocalFree ((HLOCAL)SidBuffer);
96 SetLastError (RtlNtStatusToDosError (Status));
97 return FALSE;
98 }
99
100 DPRINT ("SidLength: %lu\n", RtlLengthSid (SidBuffer[0].Sid));
101
102 Status = RtlConvertSidToUnicodeString (SidString,
103 SidBuffer[0].Sid,
104 TRUE);
105
106 LocalFree ((HLOCAL)SidBuffer);
107
108 if (!NT_SUCCESS (Status))
109 {
110 SetLastError (RtlNtStatusToDosError (Status));
111 return FALSE;
112 }
113
114 DPRINT ("SidString.Length: %lu\n", SidString->Length);
115 DPRINT ("SidString.MaximumLength: %lu\n", SidString->MaximumLength);
116 DPRINT ("SidString: '%wZ'\n", SidString);
117
118 return TRUE;
119 }
120
121 PSECURITY_DESCRIPTOR
122 CreateDefaultSecurityDescriptor(VOID)
123 {
124 PSID LocalSystemSid = NULL;
125 PSID AdministratorsSid = NULL;
126 PSID EveryoneSid = NULL;
127 PACL Dacl;
128 DWORD DaclSize;
129 PSECURITY_DESCRIPTOR pSD = NULL;
130
131 /* create the SYSTEM, Administrators and Everyone SIDs */
132 if (!AllocateAndInitializeSid(&LocalSystemAuthority,
133 1,
134 SECURITY_LOCAL_SYSTEM_RID,
135 0,
136 0,
137 0,
138 0,
139 0,
140 0,
141 0,
142 &LocalSystemSid) ||
143 !AllocateAndInitializeSid(&LocalSystemAuthority,
144 2,
145 SECURITY_BUILTIN_DOMAIN_RID,
146 DOMAIN_ALIAS_RID_ADMINS,
147 0,
148 0,
149 0,
150 0,
151 0,
152 0,
153 &AdministratorsSid) ||
154 !AllocateAndInitializeSid(&WorldAuthority,
155 1,
156 SECURITY_WORLD_RID,
157 0,
158 0,
159 0,
160 0,
161 0,
162 0,
163 0,
164 &EveryoneSid))
165 {
166 DPRINT1("Failed initializing the SIDs for the default security descriptor (0x%p, 0x%p, 0x%p)\n",
167 LocalSystemSid, AdministratorsSid, EveryoneSid);
168 goto Cleanup;
169 }
170
171 /* allocate the security descriptor and DACL */
172 DaclSize = sizeof(ACL) +
173 ((GetLengthSid(LocalSystemSid) +
174 GetLengthSid(AdministratorsSid) +
175 GetLengthSid(EveryoneSid)) +
176 (3 * FIELD_OFFSET(ACCESS_ALLOWED_ACE,
177 SidStart)));
178
179 pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LMEM_FIXED,
180 (SIZE_T)DaclSize + sizeof(SECURITY_DESCRIPTOR));
181 if (pSD == NULL)
182 {
183 DPRINT1("Failed to allocate the default security descriptor and ACL\n");
184 goto Cleanup;
185 }
186
187 if (!InitializeSecurityDescriptor(pSD,
188 SECURITY_DESCRIPTOR_REVISION))
189 {
190 DPRINT1("Failed to initialize the default security descriptor\n");
191 goto Cleanup;
192 }
193
194 /* initialize and build the DACL */
195 Dacl = (PACL)((ULONG_PTR)pSD + sizeof(SECURITY_DESCRIPTOR));
196 if (!InitializeAcl(Dacl,
197 (DWORD)DaclSize,
198 ACL_REVISION))
199 {
200 DPRINT1("Failed to initialize the DACL of the default security descriptor\n");
201 goto Cleanup;
202 }
203
204 /* add the SYSTEM Ace */
205 if (!AddAccessAllowedAce(Dacl,
206 ACL_REVISION,
207 GENERIC_ALL,
208 LocalSystemSid))
209 {
210 DPRINT1("Failed to add the SYSTEM ACE\n");
211 goto Cleanup;
212 }
213
214 /* add the Administrators Ace */
215 if (!AddAccessAllowedAce(Dacl,
216 ACL_REVISION,
217 GENERIC_ALL,
218 AdministratorsSid))
219 {
220 DPRINT1("Failed to add the Administrators ACE\n");
221 goto Cleanup;
222 }
223
224 /* add the Everyone Ace */
225 if (!AddAccessAllowedAce(Dacl,
226 ACL_REVISION,
227 GENERIC_EXECUTE,
228 EveryoneSid))
229 {
230 DPRINT1("Failed to add the Everyone ACE\n");
231 goto Cleanup;
232 }
233
234 /* set the DACL */
235 if (!SetSecurityDescriptorDacl(pSD,
236 TRUE,
237 Dacl,
238 FALSE))
239 {
240 DPRINT1("Failed to set the DACL of the default security descriptor\n");
241
242 Cleanup:
243 if (pSD != NULL)
244 {
245 LocalFree((HLOCAL)pSD);
246 pSD = NULL;
247 }
248 }
249
250 if (LocalSystemSid != NULL)
251 {
252 FreeSid(LocalSystemSid);
253 }
254 if (AdministratorsSid != NULL)
255 {
256 FreeSid(AdministratorsSid);
257 }
258 if (EveryoneSid != NULL)
259 {
260 FreeSid(EveryoneSid);
261 }
262
263 return pSD;
264 }
265
266 /* Dynamic DLL loading interface **********************************************/
267
268 /* OLE32.DLL import table */
269 DYN_MODULE DynOle32 =
270 {
271 L"ole32.dll",
272 {
273 "CoInitialize",
274 "CoCreateInstance",
275 "CoUninitialize",
276 NULL
277 }
278 };
279
280
281 /*
282 * Use this function to load functions from other modules. We cannot statically
283 * link to e.g. ole32.dll because those dlls would get loaded on startup with
284 * winlogon and they may try to register classes etc when not even a window station
285 * has been created!
286 */
287 BOOL
288 LoadDynamicImports(PDYN_MODULE Module, PDYN_FUNCS DynFuncs)
289 {
290 LPSTR *fname;
291 PVOID *fn;
292
293 ZeroMemory(DynFuncs, sizeof(DYN_FUNCS));
294
295 DynFuncs->hModule = LoadLibraryW(Module->Library);
296 if (!DynFuncs->hModule)
297 {
298 return FALSE;
299 }
300
301 fn = &DynFuncs->fn.foo;
302
303 /* load the imports */
304 for (fname = Module->Functions; *fname != NULL; fname++)
305 {
306 *fn = GetProcAddress(DynFuncs->hModule, *fname);
307 if (*fn == NULL)
308 {
309 FreeLibrary(DynFuncs->hModule);
310 DynFuncs->hModule = (HMODULE)0;
311
312 return FALSE;
313 }
314
315 fn++;
316 }
317
318 return TRUE;
319 }
320
321
322 VOID
323 UnloadDynamicImports(PDYN_FUNCS DynFuncs)
324 {
325 if (DynFuncs->hModule)
326 {
327 FreeLibrary(DynFuncs->hModule);
328 DynFuncs->hModule = (HMODULE)0;
329 }
330 }
331
332 /* EOF */