3 * Copyright (C) 2004 ReactOS Team
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.
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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS system libraries
22 * FILE: dll/win32/userenv/misc.c
23 * PURPOSE: User profile code
24 * PROGRAMMER: Eric Kohl
29 #include <ndk/sefuncs.h>
34 static SID_IDENTIFIER_AUTHORITY LocalSystemAuthority
= {SECURITY_NT_AUTHORITY
};
35 static SID_IDENTIFIER_AUTHORITY WorldAuthority
= {SECURITY_WORLD_SID_AUTHORITY
};
37 /* FUNCTIONS ***************************************************************/
40 AppendBackslash(LPWSTR String
)
44 Length
= lstrlenW(String
);
45 if (String
[Length
- 1] != L
'\\')
47 String
[Length
] = L
'\\';
49 String
[Length
] = (WCHAR
)0;
52 return &String
[Length
];
57 GetUserSidFromToken(HANDLE hToken
,
60 PTOKEN_USER UserBuffer
, nsb
;
66 UserBuffer
= LocalAlloc(LPTR
, Length
);
67 if (UserBuffer
== NULL
)
72 Status
= NtQueryInformationToken(hToken
,
77 if (Status
== STATUS_BUFFER_TOO_SMALL
)
79 nsb
= LocalReAlloc(UserBuffer
, Length
, LMEM_MOVEABLE
);
82 LocalFree(UserBuffer
);
87 Status
= NtQueryInformationToken(hToken
,
94 if (!NT_SUCCESS (Status
))
96 LocalFree(UserBuffer
);
100 Length
= RtlLengthSid(UserBuffer
->User
.Sid
);
102 pSid
= LocalAlloc(LPTR
, Length
);
105 LocalFree(UserBuffer
);
109 Status
= RtlCopySid(Length
, pSid
, UserBuffer
->User
.Sid
);
111 LocalFree(UserBuffer
);
113 if (!NT_SUCCESS (Status
))
126 GetUserSidStringFromToken(HANDLE hToken
,
127 PUNICODE_STRING SidString
)
129 PTOKEN_USER UserBuffer
, nsb
;
134 UserBuffer
= LocalAlloc(LPTR
, Length
);
135 if (UserBuffer
== NULL
)
138 Status
= NtQueryInformationToken(hToken
,
143 if (Status
== STATUS_BUFFER_TOO_SMALL
)
145 nsb
= LocalReAlloc(UserBuffer
,
150 LocalFree(UserBuffer
);
155 Status
= NtQueryInformationToken(hToken
,
162 if (!NT_SUCCESS (Status
))
164 LocalFree(UserBuffer
);
165 SetLastError(RtlNtStatusToDosError(Status
));
169 DPRINT("SidLength: %lu\n", RtlLengthSid (UserBuffer
->User
.Sid
));
171 Status
= RtlConvertSidToUnicodeString(SidString
,
172 UserBuffer
->User
.Sid
,
175 LocalFree(UserBuffer
);
177 if (!NT_SUCCESS(Status
))
179 SetLastError(RtlNtStatusToDosError(Status
));
183 DPRINT("SidString.Length: %lu\n", SidString
->Length
);
184 DPRINT("SidString.MaximumLength: %lu\n", SidString
->MaximumLength
);
185 DPRINT("SidString: '%wZ'\n", SidString
);
192 CreateDefaultSecurityDescriptor(VOID
)
194 PSID LocalSystemSid
= NULL
;
195 PSID AdministratorsSid
= NULL
;
196 PSID EveryoneSid
= NULL
;
199 PSECURITY_DESCRIPTOR pSD
= NULL
;
201 /* create the SYSTEM, Administrators and Everyone SIDs */
202 if (!AllocateAndInitializeSid(&LocalSystemAuthority
,
204 SECURITY_LOCAL_SYSTEM_RID
,
213 !AllocateAndInitializeSid(&LocalSystemAuthority
,
215 SECURITY_BUILTIN_DOMAIN_RID
,
216 DOMAIN_ALIAS_RID_ADMINS
,
223 &AdministratorsSid
) ||
224 !AllocateAndInitializeSid(&WorldAuthority
,
236 DPRINT1("Failed initializing the SIDs for the default security descriptor (0x%p, 0x%p, 0x%p)\n",
237 LocalSystemSid
, AdministratorsSid
, EveryoneSid
);
241 /* allocate the security descriptor and DACL */
242 DaclSize
= sizeof(ACL
) +
243 ((GetLengthSid(LocalSystemSid
) +
244 GetLengthSid(AdministratorsSid
) +
245 GetLengthSid(EveryoneSid
)) +
246 (3 * FIELD_OFFSET(ACCESS_ALLOWED_ACE
,
249 pSD
= (PSECURITY_DESCRIPTOR
)LocalAlloc(LMEM_FIXED
,
250 (SIZE_T
)DaclSize
+ sizeof(SECURITY_DESCRIPTOR
));
253 DPRINT1("Failed to allocate the default security descriptor and ACL\n");
257 if (!InitializeSecurityDescriptor(pSD
,
258 SECURITY_DESCRIPTOR_REVISION
))
260 DPRINT1("Failed to initialize the default security descriptor\n");
264 /* initialize and build the DACL */
265 Dacl
= (PACL
)((ULONG_PTR
)pSD
+ sizeof(SECURITY_DESCRIPTOR
));
266 if (!InitializeAcl(Dacl
,
270 DPRINT1("Failed to initialize the DACL of the default security descriptor\n");
274 /* add the SYSTEM Ace */
275 if (!AddAccessAllowedAce(Dacl
,
280 DPRINT1("Failed to add the SYSTEM ACE\n");
284 /* add the Administrators Ace */
285 if (!AddAccessAllowedAce(Dacl
,
290 DPRINT1("Failed to add the Administrators ACE\n");
294 /* add the Everyone Ace */
295 if (!AddAccessAllowedAce(Dacl
,
300 DPRINT1("Failed to add the Everyone ACE\n");
305 if (!SetSecurityDescriptorDacl(pSD
,
310 DPRINT1("Failed to set the DACL of the default security descriptor\n");
315 LocalFree((HLOCAL
)pSD
);
320 if (LocalSystemSid
!= NULL
)
322 FreeSid(LocalSystemSid
);
324 if (AdministratorsSid
!= NULL
)
326 FreeSid(AdministratorsSid
);
328 if (EveryoneSid
!= NULL
)
330 FreeSid(EveryoneSid
);
336 /* Dynamic DLL loading interface **********************************************/
338 /* OLE32.DLL import table */
339 DYN_MODULE DynOle32
=
352 * Use this function to load functions from other modules. We cannot statically
353 * link to e.g. ole32.dll because those dlls would get loaded on startup with
354 * winlogon and they may try to register classes etc when not even a window station
358 LoadDynamicImports(PDYN_MODULE Module
,
364 ZeroMemory(DynFuncs
, sizeof(DYN_FUNCS
));
366 DynFuncs
->hModule
= LoadLibraryW(Module
->Library
);
367 if (!DynFuncs
->hModule
)
372 fn
= &DynFuncs
->fn
.foo
;
374 /* load the imports */
375 for (fname
= Module
->Functions
; *fname
!= NULL
; fname
++)
377 *fn
= GetProcAddress(DynFuncs
->hModule
, *fname
);
380 FreeLibrary(DynFuncs
->hModule
);
381 DynFuncs
->hModule
= (HMODULE
)0;
394 UnloadDynamicImports(PDYN_FUNCS DynFuncs
)
396 if (DynFuncs
->hModule
)
398 FreeLibrary(DynFuncs
->hModule
);
399 DynFuncs
->hModule
= (HMODULE
)0;