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
32 static SID_IDENTIFIER_AUTHORITY LocalSystemAuthority
= {SECURITY_NT_AUTHORITY
};
33 static SID_IDENTIFIER_AUTHORITY WorldAuthority
= {SECURITY_WORLD_SID_AUTHORITY
};
35 /* FUNCTIONS ***************************************************************/
38 AppendBackslash(LPWSTR String
)
42 Length
= lstrlenW(String
);
43 if (String
[Length
- 1] != L
'\\')
45 String
[Length
] = L
'\\';
47 String
[Length
] = (WCHAR
)0;
50 return &String
[Length
];
55 GetUserSidFromToken(HANDLE hToken
,
56 PUNICODE_STRING SidString
)
58 PSID_AND_ATTRIBUTES SidBuffer
, nsb
;
63 SidBuffer
= LocalAlloc(LMEM_FIXED
,
65 if (SidBuffer
== NULL
)
68 Status
= NtQueryInformationToken(hToken
,
73 if (Status
== STATUS_BUFFER_TOO_SMALL
)
75 nsb
= LocalReAlloc(SidBuffer
,
80 LocalFree((HLOCAL
)SidBuffer
);
85 Status
= NtQueryInformationToken(hToken
,
92 if (!NT_SUCCESS (Status
))
94 LocalFree((HLOCAL
)SidBuffer
);
95 SetLastError(RtlNtStatusToDosError(Status
));
99 DPRINT("SidLength: %lu\n", RtlLengthSid (SidBuffer
[0].Sid
));
101 Status
= RtlConvertSidToUnicodeString(SidString
,
105 LocalFree((HLOCAL
)SidBuffer
);
107 if (!NT_SUCCESS(Status
))
109 SetLastError(RtlNtStatusToDosError(Status
));
113 DPRINT("SidString.Length: %lu\n", SidString
->Length
);
114 DPRINT("SidString.MaximumLength: %lu\n", SidString
->MaximumLength
);
115 DPRINT("SidString: '%wZ'\n", SidString
);
121 CreateDefaultSecurityDescriptor(VOID
)
123 PSID LocalSystemSid
= NULL
;
124 PSID AdministratorsSid
= NULL
;
125 PSID EveryoneSid
= NULL
;
128 PSECURITY_DESCRIPTOR pSD
= NULL
;
130 /* create the SYSTEM, Administrators and Everyone SIDs */
131 if (!AllocateAndInitializeSid(&LocalSystemAuthority
,
133 SECURITY_LOCAL_SYSTEM_RID
,
142 !AllocateAndInitializeSid(&LocalSystemAuthority
,
144 SECURITY_BUILTIN_DOMAIN_RID
,
145 DOMAIN_ALIAS_RID_ADMINS
,
152 &AdministratorsSid
) ||
153 !AllocateAndInitializeSid(&WorldAuthority
,
165 DPRINT1("Failed initializing the SIDs for the default security descriptor (0x%p, 0x%p, 0x%p)\n",
166 LocalSystemSid
, AdministratorsSid
, EveryoneSid
);
170 /* allocate the security descriptor and DACL */
171 DaclSize
= sizeof(ACL
) +
172 ((GetLengthSid(LocalSystemSid
) +
173 GetLengthSid(AdministratorsSid
) +
174 GetLengthSid(EveryoneSid
)) +
175 (3 * FIELD_OFFSET(ACCESS_ALLOWED_ACE
,
178 pSD
= (PSECURITY_DESCRIPTOR
)LocalAlloc(LMEM_FIXED
,
179 (SIZE_T
)DaclSize
+ sizeof(SECURITY_DESCRIPTOR
));
182 DPRINT1("Failed to allocate the default security descriptor and ACL\n");
186 if (!InitializeSecurityDescriptor(pSD
,
187 SECURITY_DESCRIPTOR_REVISION
))
189 DPRINT1("Failed to initialize the default security descriptor\n");
193 /* initialize and build the DACL */
194 Dacl
= (PACL
)((ULONG_PTR
)pSD
+ sizeof(SECURITY_DESCRIPTOR
));
195 if (!InitializeAcl(Dacl
,
199 DPRINT1("Failed to initialize the DACL of the default security descriptor\n");
203 /* add the SYSTEM Ace */
204 if (!AddAccessAllowedAce(Dacl
,
209 DPRINT1("Failed to add the SYSTEM ACE\n");
213 /* add the Administrators Ace */
214 if (!AddAccessAllowedAce(Dacl
,
219 DPRINT1("Failed to add the Administrators ACE\n");
223 /* add the Everyone Ace */
224 if (!AddAccessAllowedAce(Dacl
,
229 DPRINT1("Failed to add the Everyone ACE\n");
234 if (!SetSecurityDescriptorDacl(pSD
,
239 DPRINT1("Failed to set the DACL of the default security descriptor\n");
244 LocalFree((HLOCAL
)pSD
);
249 if (LocalSystemSid
!= NULL
)
251 FreeSid(LocalSystemSid
);
253 if (AdministratorsSid
!= NULL
)
255 FreeSid(AdministratorsSid
);
257 if (EveryoneSid
!= NULL
)
259 FreeSid(EveryoneSid
);
265 /* Dynamic DLL loading interface **********************************************/
267 /* OLE32.DLL import table */
268 DYN_MODULE DynOle32
=
281 * Use this function to load functions from other modules. We cannot statically
282 * link to e.g. ole32.dll because those dlls would get loaded on startup with
283 * winlogon and they may try to register classes etc when not even a window station
287 LoadDynamicImports(PDYN_MODULE Module
,
293 ZeroMemory(DynFuncs
, sizeof(DYN_FUNCS
));
295 DynFuncs
->hModule
= LoadLibraryW(Module
->Library
);
296 if (!DynFuncs
->hModule
)
301 fn
= &DynFuncs
->fn
.foo
;
303 /* load the imports */
304 for (fname
= Module
->Functions
; *fname
!= NULL
; fname
++)
306 *fn
= GetProcAddress(DynFuncs
->hModule
, *fname
);
309 FreeLibrary(DynFuncs
->hModule
);
310 DynFuncs
->hModule
= (HMODULE
)0;
323 UnloadDynamicImports(PDYN_FUNCS DynFuncs
)
325 if (DynFuncs
->hModule
)
327 FreeLibrary(DynFuncs
->hModule
);
328 DynFuncs
->hModule
= (HMODULE
)0;