xref: /reactos/dll/win32/userenv/misc.c (revision 0f21a8a6)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  *  ReactOS kernel
3c2c66affSColin Finck  *  Copyright (C) 2004 ReactOS Team
4c2c66affSColin Finck  *
5c2c66affSColin Finck  *  This program is free software; you can redistribute it and/or modify
6c2c66affSColin Finck  *  it under the terms of the GNU General Public License as published by
7c2c66affSColin Finck  *  the Free Software Foundation; either version 2 of the License, or
8c2c66affSColin Finck  *  (at your option) any later version.
9c2c66affSColin Finck  *
10c2c66affSColin Finck  *  This program is distributed in the hope that it will be useful,
11c2c66affSColin Finck  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12c2c66affSColin Finck  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13c2c66affSColin Finck  *  GNU General Public License for more details.
14c2c66affSColin Finck  *
15c2c66affSColin Finck  *  You should have received a copy of the GNU General Public License along
16c2c66affSColin Finck  *  with this program; if not, write to the Free Software Foundation, Inc.,
17c2c66affSColin Finck  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18c2c66affSColin Finck  */
19c2c66affSColin Finck /*
20c2c66affSColin Finck  * COPYRIGHT:       See COPYING in the top level directory
21c2c66affSColin Finck  * PROJECT:         ReactOS system libraries
22c2c66affSColin Finck  * FILE:            dll/win32/userenv/misc.c
23c2c66affSColin Finck  * PURPOSE:         User profile code
24c2c66affSColin Finck  * PROGRAMMER:      Eric Kohl
25c2c66affSColin Finck  */
26c2c66affSColin Finck 
27c2c66affSColin Finck #include "precomp.h"
28c2c66affSColin Finck 
29c2c66affSColin Finck #include <ndk/sefuncs.h>
30c2c66affSColin Finck 
31c2c66affSColin Finck #define NDEBUG
32c2c66affSColin Finck #include <debug.h>
33c2c66affSColin Finck 
34*0f21a8a6SEric Kohl SID_IDENTIFIER_AUTHORITY LocalSystemAuthority = {SECURITY_NT_AUTHORITY};
35*0f21a8a6SEric Kohl SID_IDENTIFIER_AUTHORITY WorldAuthority = {SECURITY_WORLD_SID_AUTHORITY};
36c2c66affSColin Finck 
37c2c66affSColin Finck /* FUNCTIONS ***************************************************************/
38c2c66affSColin Finck 
39c2c66affSColin Finck LPWSTR
AppendBackslash(LPWSTR String)40c2c66affSColin Finck AppendBackslash(LPWSTR String)
41c2c66affSColin Finck {
42c2c66affSColin Finck     ULONG Length;
43c2c66affSColin Finck 
44c2c66affSColin Finck     Length = lstrlenW(String);
45c2c66affSColin Finck     if (String[Length - 1] != L'\\')
46c2c66affSColin Finck     {
47c2c66affSColin Finck         String[Length] = L'\\';
48c2c66affSColin Finck         Length++;
49c2c66affSColin Finck         String[Length] = (WCHAR)0;
50c2c66affSColin Finck     }
51c2c66affSColin Finck 
52c2c66affSColin Finck     return &String[Length];
53c2c66affSColin Finck }
54c2c66affSColin Finck 
55c2c66affSColin Finck 
56c2c66affSColin Finck PSECURITY_DESCRIPTOR
CreateDefaultSecurityDescriptor(VOID)57c2c66affSColin Finck CreateDefaultSecurityDescriptor(VOID)
58c2c66affSColin Finck {
59c2c66affSColin Finck     PSID LocalSystemSid = NULL;
60c2c66affSColin Finck     PSID AdministratorsSid = NULL;
61c2c66affSColin Finck     PSID EveryoneSid = NULL;
62c2c66affSColin Finck     PACL Dacl;
63c2c66affSColin Finck     DWORD DaclSize;
64c2c66affSColin Finck     PSECURITY_DESCRIPTOR pSD = NULL;
65c2c66affSColin Finck 
66c2c66affSColin Finck     /* create the SYSTEM, Administrators and Everyone SIDs */
67c2c66affSColin Finck     if (!AllocateAndInitializeSid(&LocalSystemAuthority,
68c2c66affSColin Finck                                   1,
69c2c66affSColin Finck                                   SECURITY_LOCAL_SYSTEM_RID,
70c2c66affSColin Finck                                   0,
71c2c66affSColin Finck                                   0,
72c2c66affSColin Finck                                   0,
73c2c66affSColin Finck                                   0,
74c2c66affSColin Finck                                   0,
75c2c66affSColin Finck                                   0,
76c2c66affSColin Finck                                   0,
77c2c66affSColin Finck                                   &LocalSystemSid) ||
78c2c66affSColin Finck         !AllocateAndInitializeSid(&LocalSystemAuthority,
79c2c66affSColin Finck                                   2,
80c2c66affSColin Finck                                   SECURITY_BUILTIN_DOMAIN_RID,
81c2c66affSColin Finck                                   DOMAIN_ALIAS_RID_ADMINS,
82c2c66affSColin Finck                                   0,
83c2c66affSColin Finck                                   0,
84c2c66affSColin Finck                                   0,
85c2c66affSColin Finck                                   0,
86c2c66affSColin Finck                                   0,
87c2c66affSColin Finck                                   0,
88c2c66affSColin Finck                                   &AdministratorsSid) ||
89c2c66affSColin Finck         !AllocateAndInitializeSid(&WorldAuthority,
90c2c66affSColin Finck                                   1,
91c2c66affSColin Finck                                   SECURITY_WORLD_RID,
92c2c66affSColin Finck                                   0,
93c2c66affSColin Finck                                   0,
94c2c66affSColin Finck                                   0,
95c2c66affSColin Finck                                   0,
96c2c66affSColin Finck                                   0,
97c2c66affSColin Finck                                   0,
98c2c66affSColin Finck                                   0,
99c2c66affSColin Finck                                   &EveryoneSid))
100c2c66affSColin Finck     {
101c2c66affSColin Finck         DPRINT1("Failed initializing the SIDs for the default security descriptor (0x%p, 0x%p, 0x%p)\n",
102c2c66affSColin Finck                 LocalSystemSid, AdministratorsSid, EveryoneSid);
103c2c66affSColin Finck         goto Cleanup;
104c2c66affSColin Finck     }
105c2c66affSColin Finck 
106c2c66affSColin Finck     /* allocate the security descriptor and DACL */
107c2c66affSColin Finck     DaclSize = sizeof(ACL) +
108c2c66affSColin Finck                ((GetLengthSid(LocalSystemSid) +
109c2c66affSColin Finck                  GetLengthSid(AdministratorsSid) +
110c2c66affSColin Finck                  GetLengthSid(EveryoneSid)) +
111c2c66affSColin Finck                 (3 * FIELD_OFFSET(ACCESS_ALLOWED_ACE,
112c2c66affSColin Finck                                   SidStart)));
113c2c66affSColin Finck 
114c2c66affSColin Finck     pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LMEM_FIXED,
115c2c66affSColin Finck                                            (SIZE_T)DaclSize + sizeof(SECURITY_DESCRIPTOR));
116c2c66affSColin Finck     if (pSD == NULL)
117c2c66affSColin Finck     {
118c2c66affSColin Finck         DPRINT1("Failed to allocate the default security descriptor and ACL\n");
119c2c66affSColin Finck         goto Cleanup;
120c2c66affSColin Finck     }
121c2c66affSColin Finck 
122c2c66affSColin Finck     if (!InitializeSecurityDescriptor(pSD,
123c2c66affSColin Finck                                       SECURITY_DESCRIPTOR_REVISION))
124c2c66affSColin Finck     {
125c2c66affSColin Finck         DPRINT1("Failed to initialize the default security descriptor\n");
126c2c66affSColin Finck         goto Cleanup;
127c2c66affSColin Finck     }
128c2c66affSColin Finck 
129c2c66affSColin Finck     /* initialize and build the DACL */
130c2c66affSColin Finck     Dacl = (PACL)((ULONG_PTR)pSD + sizeof(SECURITY_DESCRIPTOR));
131c2c66affSColin Finck     if (!InitializeAcl(Dacl,
132c2c66affSColin Finck                        (DWORD)DaclSize,
133c2c66affSColin Finck                        ACL_REVISION))
134c2c66affSColin Finck     {
135c2c66affSColin Finck         DPRINT1("Failed to initialize the DACL of the default security descriptor\n");
136c2c66affSColin Finck         goto Cleanup;
137c2c66affSColin Finck     }
138c2c66affSColin Finck 
139c2c66affSColin Finck     /* add the SYSTEM Ace */
140c2c66affSColin Finck     if (!AddAccessAllowedAce(Dacl,
141c2c66affSColin Finck                              ACL_REVISION,
142c2c66affSColin Finck                              GENERIC_ALL,
143c2c66affSColin Finck                              LocalSystemSid))
144c2c66affSColin Finck     {
145c2c66affSColin Finck         DPRINT1("Failed to add the SYSTEM ACE\n");
146c2c66affSColin Finck         goto Cleanup;
147c2c66affSColin Finck     }
148c2c66affSColin Finck 
149c2c66affSColin Finck     /* add the Administrators Ace */
150c2c66affSColin Finck     if (!AddAccessAllowedAce(Dacl,
151c2c66affSColin Finck                              ACL_REVISION,
152c2c66affSColin Finck                              GENERIC_ALL,
153c2c66affSColin Finck                              AdministratorsSid))
154c2c66affSColin Finck     {
155c2c66affSColin Finck         DPRINT1("Failed to add the Administrators ACE\n");
156c2c66affSColin Finck         goto Cleanup;
157c2c66affSColin Finck     }
158c2c66affSColin Finck 
159c2c66affSColin Finck     /* add the Everyone Ace */
160c2c66affSColin Finck     if (!AddAccessAllowedAce(Dacl,
161c2c66affSColin Finck                              ACL_REVISION,
162c2c66affSColin Finck                              GENERIC_EXECUTE,
163c2c66affSColin Finck                              EveryoneSid))
164c2c66affSColin Finck     {
165c2c66affSColin Finck         DPRINT1("Failed to add the Everyone ACE\n");
166c2c66affSColin Finck         goto Cleanup;
167c2c66affSColin Finck     }
168c2c66affSColin Finck 
169c2c66affSColin Finck     /* set the DACL */
170c2c66affSColin Finck     if (!SetSecurityDescriptorDacl(pSD,
171c2c66affSColin Finck                                    TRUE,
172c2c66affSColin Finck                                    Dacl,
173c2c66affSColin Finck                                    FALSE))
174c2c66affSColin Finck     {
175c2c66affSColin Finck         DPRINT1("Failed to set the DACL of the default security descriptor\n");
176c2c66affSColin Finck 
177c2c66affSColin Finck Cleanup:
178c2c66affSColin Finck         if (pSD != NULL)
179c2c66affSColin Finck         {
180c2c66affSColin Finck             LocalFree((HLOCAL)pSD);
181c2c66affSColin Finck             pSD = NULL;
182c2c66affSColin Finck         }
183c2c66affSColin Finck     }
184c2c66affSColin Finck 
185c2c66affSColin Finck     if (LocalSystemSid != NULL)
186c2c66affSColin Finck     {
187c2c66affSColin Finck         FreeSid(LocalSystemSid);
188c2c66affSColin Finck     }
189c2c66affSColin Finck     if (AdministratorsSid != NULL)
190c2c66affSColin Finck     {
191c2c66affSColin Finck         FreeSid(AdministratorsSid);
192c2c66affSColin Finck     }
193c2c66affSColin Finck     if (EveryoneSid != NULL)
194c2c66affSColin Finck     {
195c2c66affSColin Finck         FreeSid(EveryoneSid);
196c2c66affSColin Finck     }
197c2c66affSColin Finck 
198c2c66affSColin Finck     return pSD;
199c2c66affSColin Finck }
200c2c66affSColin Finck 
201c2c66affSColin Finck /* Dynamic DLL loading interface **********************************************/
202c2c66affSColin Finck 
203c2c66affSColin Finck /* OLE32.DLL import table */
204c2c66affSColin Finck DYN_MODULE DynOle32 =
205c2c66affSColin Finck {
206c2c66affSColin Finck   L"ole32.dll",
207c2c66affSColin Finck   {
208c2c66affSColin Finck     "CoInitialize",
209c2c66affSColin Finck     "CoCreateInstance",
210c2c66affSColin Finck     "CoUninitialize",
211c2c66affSColin Finck     NULL
212c2c66affSColin Finck   }
213c2c66affSColin Finck };
214c2c66affSColin Finck 
215c2c66affSColin Finck 
216c2c66affSColin Finck /*
217c2c66affSColin Finck  * Use this function to load functions from other modules. We cannot statically
218c2c66affSColin Finck  * link to e.g. ole32.dll because those dlls would get loaded on startup with
219c2c66affSColin Finck  * winlogon and they may try to register classes etc when not even a window station
220c2c66affSColin Finck  * has been created!
221c2c66affSColin Finck  */
222c2c66affSColin Finck BOOL
LoadDynamicImports(PDYN_MODULE Module,PDYN_FUNCS DynFuncs)223c2c66affSColin Finck LoadDynamicImports(PDYN_MODULE Module,
224c2c66affSColin Finck                    PDYN_FUNCS DynFuncs)
225c2c66affSColin Finck {
226c2c66affSColin Finck     LPSTR *fname;
227c2c66affSColin Finck     PVOID *fn;
228c2c66affSColin Finck 
229c2c66affSColin Finck     ZeroMemory(DynFuncs, sizeof(DYN_FUNCS));
230c2c66affSColin Finck 
231c2c66affSColin Finck     DynFuncs->hModule = LoadLibraryW(Module->Library);
232c2c66affSColin Finck     if (!DynFuncs->hModule)
233c2c66affSColin Finck     {
234c2c66affSColin Finck         return FALSE;
235c2c66affSColin Finck     }
236c2c66affSColin Finck 
237c2c66affSColin Finck     fn = &DynFuncs->fn.foo;
238c2c66affSColin Finck 
239c2c66affSColin Finck     /* load the imports */
240c2c66affSColin Finck     for (fname = Module->Functions; *fname != NULL; fname++)
241c2c66affSColin Finck     {
242c2c66affSColin Finck         *fn = GetProcAddress(DynFuncs->hModule, *fname);
243c2c66affSColin Finck         if (*fn == NULL)
244c2c66affSColin Finck         {
245c2c66affSColin Finck             FreeLibrary(DynFuncs->hModule);
246c2c66affSColin Finck             DynFuncs->hModule = (HMODULE)0;
247c2c66affSColin Finck 
248c2c66affSColin Finck             return FALSE;
249c2c66affSColin Finck         }
250c2c66affSColin Finck 
251c2c66affSColin Finck         fn++;
252c2c66affSColin Finck     }
253c2c66affSColin Finck 
254c2c66affSColin Finck     return TRUE;
255c2c66affSColin Finck }
256c2c66affSColin Finck 
257c2c66affSColin Finck 
258c2c66affSColin Finck VOID
UnloadDynamicImports(PDYN_FUNCS DynFuncs)259c2c66affSColin Finck UnloadDynamicImports(PDYN_FUNCS DynFuncs)
260c2c66affSColin Finck {
261c2c66affSColin Finck     if (DynFuncs->hModule)
262c2c66affSColin Finck     {
263c2c66affSColin Finck         FreeLibrary(DynFuncs->hModule);
264c2c66affSColin Finck         DynFuncs->hModule = (HMODULE)0;
265c2c66affSColin Finck     }
266c2c66affSColin Finck }
267c2c66affSColin Finck 
268c2c66affSColin Finck /* EOF */
269