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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
33 static SID_IDENTIFIER_AUTHORITY LocalSystemAuthority
= {SECURITY_NT_AUTHORITY
};
34 static SID_IDENTIFIER_AUTHORITY WorldAuthority
= {SECURITY_WORLD_SID_AUTHORITY
};
36 /* FUNCTIONS ***************************************************************/
39 AppendBackslash (LPWSTR String
)
43 Length
= lstrlenW (String
);
44 if (String
[Length
- 1] != L
'\\')
46 String
[Length
] = L
'\\';
48 String
[Length
] = (WCHAR
)0;
51 return &String
[Length
];
56 GetUserSidFromToken (HANDLE hToken
,
57 PUNICODE_STRING SidString
)
59 PSID_AND_ATTRIBUTES SidBuffer
, nsb
;
64 SidBuffer
= LocalAlloc (LMEM_FIXED
,
66 if (SidBuffer
== NULL
)
69 Status
= NtQueryInformationToken (hToken
,
74 if (Status
== STATUS_BUFFER_TOO_SMALL
)
76 nsb
= LocalReAlloc (SidBuffer
,
81 LocalFree((HLOCAL
)SidBuffer
);
86 Status
= NtQueryInformationToken (hToken
,
93 if (!NT_SUCCESS (Status
))
95 LocalFree ((HLOCAL
)SidBuffer
);
96 SetLastError (RtlNtStatusToDosError (Status
));
100 DPRINT ("SidLength: %lu\n", RtlLengthSid (SidBuffer
[0].Sid
));
102 Status
= RtlConvertSidToUnicodeString (SidString
,
106 LocalFree ((HLOCAL
)SidBuffer
);
108 if (!NT_SUCCESS (Status
))
110 SetLastError (RtlNtStatusToDosError (Status
));
114 DPRINT ("SidString.Length: %lu\n", SidString
->Length
);
115 DPRINT ("SidString.MaximumLength: %lu\n", SidString
->MaximumLength
);
116 DPRINT ("SidString: '%wZ'\n", SidString
);
122 CreateDefaultSecurityDescriptor(VOID
)
124 PSID LocalSystemSid
= NULL
;
125 PSID AdministratorsSid
= NULL
;
126 PSID EveryoneSid
= NULL
;
129 PSECURITY_DESCRIPTOR pSD
= NULL
;
131 /* create the SYSTEM, Administrators and Everyone SIDs */
132 if (!AllocateAndInitializeSid(&LocalSystemAuthority
,
134 SECURITY_LOCAL_SYSTEM_RID
,
143 !AllocateAndInitializeSid(&LocalSystemAuthority
,
145 SECURITY_BUILTIN_DOMAIN_RID
,
146 DOMAIN_ALIAS_RID_ADMINS
,
153 &AdministratorsSid
) ||
154 !AllocateAndInitializeSid(&WorldAuthority
,
166 DPRINT1("Failed initializing the SIDs for the default security descriptor (0x%p, 0x%p, 0x%p)\n",
167 LocalSystemSid
, AdministratorsSid
, EveryoneSid
);
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
,
179 pSD
= (PSECURITY_DESCRIPTOR
)LocalAlloc(LMEM_FIXED
,
180 (SIZE_T
)DaclSize
+ sizeof(SECURITY_DESCRIPTOR
));
183 DPRINT1("Failed to allocate the default security descriptor and ACL\n");
187 if (!InitializeSecurityDescriptor(pSD
,
188 SECURITY_DESCRIPTOR_REVISION
))
190 DPRINT1("Failed to initialize the default security descriptor\n");
194 /* initialize and build the DACL */
195 Dacl
= (PACL
)((ULONG_PTR
)pSD
+ sizeof(SECURITY_DESCRIPTOR
));
196 if (!InitializeAcl(Dacl
,
200 DPRINT1("Failed to initialize the DACL of the default security descriptor\n");
204 /* add the SYSTEM Ace */
205 if (!AddAccessAllowedAce(Dacl
,
210 DPRINT1("Failed to add the SYSTEM ACE\n");
214 /* add the Administrators Ace */
215 if (!AddAccessAllowedAce(Dacl
,
220 DPRINT1("Failed to add the Administrators ACE\n");
224 /* add the Everyone Ace */
225 if (!AddAccessAllowedAce(Dacl
,
230 DPRINT1("Failed to add the Everyone ACE\n");
235 if (!SetSecurityDescriptorDacl(pSD
,
240 DPRINT1("Failed to set the DACL of the default security descriptor\n");
245 LocalFree((HLOCAL
)pSD
);
250 if (LocalSystemSid
!= NULL
)
252 FreeSid(LocalSystemSid
);
254 if (AdministratorsSid
!= NULL
)
256 FreeSid(AdministratorsSid
);
258 if (EveryoneSid
!= NULL
)
260 FreeSid(EveryoneSid
);
266 /* Dynamic DLL loading interface **********************************************/
268 /* OLE32.DLL import table */
269 DYN_MODULE DynOle32
=
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
288 LoadDynamicImports(PDYN_MODULE Module
, PDYN_FUNCS DynFuncs
)
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;