xref: /reactos/dll/win32/userenv/misc.c (revision 0f21a8a6)
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 
34 SID_IDENTIFIER_AUTHORITY LocalSystemAuthority = {SECURITY_NT_AUTHORITY};
35 SID_IDENTIFIER_AUTHORITY WorldAuthority = {SECURITY_WORLD_SID_AUTHORITY};
36 
37 /* FUNCTIONS ***************************************************************/
38 
39 LPWSTR
AppendBackslash(LPWSTR String)40 AppendBackslash(LPWSTR String)
41 {
42     ULONG Length;
43 
44     Length = lstrlenW(String);
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 
56 PSECURITY_DESCRIPTOR
CreateDefaultSecurityDescriptor(VOID)57 CreateDefaultSecurityDescriptor(VOID)
58 {
59     PSID LocalSystemSid = NULL;
60     PSID AdministratorsSid = NULL;
61     PSID EveryoneSid = NULL;
62     PACL Dacl;
63     DWORD DaclSize;
64     PSECURITY_DESCRIPTOR pSD = NULL;
65 
66     /* create the SYSTEM, Administrators and Everyone SIDs */
67     if (!AllocateAndInitializeSid(&LocalSystemAuthority,
68                                   1,
69                                   SECURITY_LOCAL_SYSTEM_RID,
70                                   0,
71                                   0,
72                                   0,
73                                   0,
74                                   0,
75                                   0,
76                                   0,
77                                   &LocalSystemSid) ||
78         !AllocateAndInitializeSid(&LocalSystemAuthority,
79                                   2,
80                                   SECURITY_BUILTIN_DOMAIN_RID,
81                                   DOMAIN_ALIAS_RID_ADMINS,
82                                   0,
83                                   0,
84                                   0,
85                                   0,
86                                   0,
87                                   0,
88                                   &AdministratorsSid) ||
89         !AllocateAndInitializeSid(&WorldAuthority,
90                                   1,
91                                   SECURITY_WORLD_RID,
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) +
108                ((GetLengthSid(LocalSystemSid) +
109                  GetLengthSid(AdministratorsSid) +
110                  GetLengthSid(EveryoneSid)) +
111                 (3 * FIELD_OFFSET(ACCESS_ALLOWED_ACE,
112                                   SidStart)));
113 
114     pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LMEM_FIXED,
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 
122     if (!InitializeSecurityDescriptor(pSD,
123                                       SECURITY_DESCRIPTOR_REVISION))
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 */
140     if (!AddAccessAllowedAce(Dacl,
141                              ACL_REVISION,
142                              GENERIC_ALL,
143                              LocalSystemSid))
144     {
145         DPRINT1("Failed to add the SYSTEM ACE\n");
146         goto Cleanup;
147     }
148 
149     /* add the Administrators Ace */
150     if (!AddAccessAllowedAce(Dacl,
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 */
160     if (!AddAccessAllowedAce(Dacl,
161                              ACL_REVISION,
162                              GENERIC_EXECUTE,
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     {
187         FreeSid(LocalSystemSid);
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 */
204 DYN_MODULE DynOle32 =
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
LoadDynamicImports(PDYN_MODULE Module,PDYN_FUNCS DynFuncs)223 LoadDynamicImports(PDYN_MODULE Module,
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
UnloadDynamicImports(PDYN_FUNCS DynFuncs)259 UnloadDynamicImports(PDYN_FUNCS DynFuncs)
260 {
261     if (DynFuncs->hModule)
262     {
263         FreeLibrary(DynFuncs->hModule);
264         DynFuncs->hModule = (HMODULE)0;
265     }
266 }
267 
268 /* EOF */
269