ReactOS  0.4.14-dev-583-g2a1ba2c
misc.c
Go to the documentation of this file.
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 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.
18  */
19 /*
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
25  */
26 
27 #include "precomp.h"
28 
29 #include <ndk/sefuncs.h>
30 
31 #define NDEBUG
32 #include <debug.h>
33 
36 
37 /* FUNCTIONS ***************************************************************/
38 
39 LPWSTR
41 {
42  ULONG Length;
43 
45  if (String[Length - 1] != L'\\')
46  {
47  String[Length] = L'\\';
48  Length++;
49  String[Length] = (WCHAR)0;
50  }
51 
52  return &String[Length];
53 }
54 
55 
58 {
60  PSID AdministratorsSid = NULL;
61  PSID EveryoneSid = NULL;
62  PACL Dacl;
65 
66  /* create the SYSTEM, Administrators and Everyone SIDs */
68  1,
70  0,
71  0,
72  0,
73  0,
74  0,
75  0,
76  0,
77  &LocalSystemSid) ||
79  2,
82  0,
83  0,
84  0,
85  0,
86  0,
87  0,
88  &AdministratorsSid) ||
90  1,
92  0,
93  0,
94  0,
95  0,
96  0,
97  0,
98  0,
99  &EveryoneSid))
100  {
101  DPRINT1("Failed initializing the SIDs for the default security descriptor (0x%p, 0x%p, 0x%p)\n",
102  LocalSystemSid, AdministratorsSid, EveryoneSid);
103  goto Cleanup;
104  }
105 
106  /* allocate the security descriptor and DACL */
107  DaclSize = sizeof(ACL) +
109  GetLengthSid(AdministratorsSid) +
110  GetLengthSid(EveryoneSid)) +
112  SidStart)));
113 
115  (SIZE_T)DaclSize + sizeof(SECURITY_DESCRIPTOR));
116  if (pSD == NULL)
117  {
118  DPRINT1("Failed to allocate the default security descriptor and ACL\n");
119  goto Cleanup;
120  }
121 
124  {
125  DPRINT1("Failed to initialize the default security descriptor\n");
126  goto Cleanup;
127  }
128 
129  /* initialize and build the DACL */
130  Dacl = (PACL)((ULONG_PTR)pSD + sizeof(SECURITY_DESCRIPTOR));
131  if (!InitializeAcl(Dacl,
132  (DWORD)DaclSize,
133  ACL_REVISION))
134  {
135  DPRINT1("Failed to initialize the DACL of the default security descriptor\n");
136  goto Cleanup;
137  }
138 
139  /* add the SYSTEM Ace */
141  ACL_REVISION,
142  GENERIC_ALL,
144  {
145  DPRINT1("Failed to add the SYSTEM ACE\n");
146  goto Cleanup;
147  }
148 
149  /* add the Administrators Ace */
151  ACL_REVISION,
152  GENERIC_ALL,
153  AdministratorsSid))
154  {
155  DPRINT1("Failed to add the Administrators ACE\n");
156  goto Cleanup;
157  }
158 
159  /* add the Everyone Ace */
161  ACL_REVISION,
163  EveryoneSid))
164  {
165  DPRINT1("Failed to add the Everyone ACE\n");
166  goto Cleanup;
167  }
168 
169  /* set the DACL */
170  if (!SetSecurityDescriptorDacl(pSD,
171  TRUE,
172  Dacl,
173  FALSE))
174  {
175  DPRINT1("Failed to set the DACL of the default security descriptor\n");
176 
177 Cleanup:
178  if (pSD != NULL)
179  {
180  LocalFree((HLOCAL)pSD);
181  pSD = NULL;
182  }
183  }
184 
185  if (LocalSystemSid != NULL)
186  {
188  }
189  if (AdministratorsSid != NULL)
190  {
191  FreeSid(AdministratorsSid);
192  }
193  if (EveryoneSid != NULL)
194  {
195  FreeSid(EveryoneSid);
196  }
197 
198  return pSD;
199 }
200 
201 /* Dynamic DLL loading interface **********************************************/
202 
203 /* OLE32.DLL import table */
205 {
206  L"ole32.dll",
207  {
208  "CoInitialize",
209  "CoCreateInstance",
210  "CoUninitialize",
211  NULL
212  }
213 };
214 
215 
216 /*
217  * Use this function to load functions from other modules. We cannot statically
218  * link to e.g. ole32.dll because those dlls would get loaded on startup with
219  * winlogon and they may try to register classes etc when not even a window station
220  * has been created!
221  */
222 BOOL
224  PDYN_FUNCS DynFuncs)
225 {
226  LPSTR *fname;
227  PVOID *fn;
228 
229  ZeroMemory(DynFuncs, sizeof(DYN_FUNCS));
230 
231  DynFuncs->hModule = LoadLibraryW(Module->Library);
232  if (!DynFuncs->hModule)
233  {
234  return FALSE;
235  }
236 
237  fn = &DynFuncs->fn.foo;
238 
239  /* load the imports */
240  for (fname = Module->Functions; *fname != NULL; fname++)
241  {
242  *fn = GetProcAddress(DynFuncs->hModule, *fname);
243  if (*fn == NULL)
244  {
245  FreeLibrary(DynFuncs->hModule);
246  DynFuncs->hModule = (HMODULE)0;
247 
248  return FALSE;
249  }
250 
251  fn++;
252  }
253 
254  return TRUE;
255 }
256 
257 
258 VOID
260 {
261  if (DynFuncs->hModule)
262  {
263  FreeLibrary(DynFuncs->hModule);
264  DynFuncs->hModule = (HMODULE)0;
265  }
266 }
267 
268 /* EOF */
VOID UnloadDynamicImports(PDYN_FUNCS DynFuncs)
Definition: misc.c:259
struct _SECURITY_DESCRIPTOR * PSECURITY_DESCRIPTOR
Definition: security.c:97
#define GENERIC_ALL
Definition: nt_native.h:92
#define SECURITY_LOCAL_SYSTEM_RID
Definition: setypes.h:546
#define TRUE
Definition: types.h:120
BOOL WINAPI InitializeAcl(PACL pAcl, DWORD nAclLength, DWORD dwAclRevision)
Definition: security.c:885
BOOL LoadDynamicImports(PDYN_MODULE Module, PDYN_FUNCS DynFuncs)
Definition: misc.c:223
static GLenum _GLUfuncptr fn
Definition: wgl_font.c:159
BOOL WINAPI InitializeSecurityDescriptor(PSECURITY_DESCRIPTOR pSecurityDescriptor, DWORD dwRevision)
Definition: security.c:808
BOOL WINAPI AddAccessAllowedAce(PACL pAcl, DWORD dwAceRevision, DWORD AccessMask, PSID pSid)
Definition: security.c:918
#define ZeroMemory
Definition: winbase.h:1642
static WCHAR String[]
Definition: stringtable.c:55
char * LPSTR
Definition: xmlstorage.h:182
#define lstrlenW
Definition: compat.h:415
LPWSTR Library
Definition: internal.h:64
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define SECURITY_DESCRIPTOR_REVISION
Definition: setypes.h:58
unsigned int BOOL
Definition: ntddk_ex.h:94
PVOID WINAPI FreeSid(PSID pSid)
Definition: security.c:577
struct _ACL ACL
#define LoadLibraryW(x)
Definition: compat.h:412
smooth NULL
Definition: ftsmooth.c:416
#define SECURITY_NT_AUTHORITY
Definition: setypes.h:526
struct _ACL * PACL
Definition: security.c:104
#define LMEM_FIXED
Definition: winbase.h:349
LPSTR Functions[]
Definition: internal.h:65
#define FreeLibrary(x)
Definition: compat.h:413
#define SECURITY_BUILTIN_DOMAIN_RID
Definition: setypes.h:553
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define SECURITY_WORLD_SID_AUTHORITY
Definition: setypes.h:499
DWORD WINAPI GetLengthSid(PSID pSid)
Definition: security.c:798
PSID LocalSystemSid
Definition: globals.c:16
PVOID foo
Definition: internal.h:52
unsigned long DWORD
Definition: ntddk_ex.h:95
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define SECURITY_WORLD_RID
Definition: setypes.h:513
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL Dacl
Definition: rtlfuncs.h:1553
static const WCHAR L[]
Definition: oid.c:1250
PSECURITY_DESCRIPTOR CreateDefaultSecurityDescriptor(VOID)
Definition: misc.c:57
static const WCHAR Cleanup[]
Definition: register.c:80
struct _SECURITY_DESCRIPTOR SECURITY_DESCRIPTOR
HANDLE HMODULE
Definition: typedefs.h:75
ULONG_PTR SIZE_T
Definition: typedefs.h:78
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:254
BOOL WINAPI SetSecurityDescriptorDacl(PSECURITY_DESCRIPTOR pSecurityDescriptor, BOOL bDaclPresent, PACL pDacl, BOOL bDaclDefaulted)
Definition: sec.c:262
DYN_MODULE DynOle32
Definition: misc.c:204
#define DPRINT1
Definition: precomp.h:8
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG DaclSize
Definition: rtlfuncs.h:1553
#define ACL_REVISION
Definition: setypes.h:39
unsigned int ULONG
Definition: retypes.h:1
#define GetProcAddress(x, y)
Definition: compat.h:418
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
SID_IDENTIFIER_AUTHORITY WorldAuthority
Definition: misc.c:35
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define GENERIC_EXECUTE
Definition: nt_native.h:91
LPWSTR AppendBackslash(LPWSTR String)
Definition: misc.c:40
#define DOMAIN_ALIAS_RID_ADMINS
Definition: setypes.h:624
HMODULE hModule
Definition: internal.h:49
union _DYN_FUNCS::@549 fn
BOOL WINAPI AllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority, BYTE nSubAuthorityCount, DWORD nSubAuthority0, DWORD nSubAuthority1, DWORD nSubAuthority2, DWORD nSubAuthority3, DWORD nSubAuthority4, DWORD nSubAuthority5, DWORD nSubAuthority6, DWORD nSubAuthority7, PSID *pSid)
Definition: security.c:553
SID_IDENTIFIER_AUTHORITY LocalSystemAuthority
Definition: misc.c:34