Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenmisc.c
Go to the documentation of this file.
00001 /* 00002 * ReactOS kernel 00003 * Copyright (C) 2004 ReactOS Team 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License along 00016 * with this program; if not, write to the Free Software Foundation, Inc., 00017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00018 */ 00019 /* $Id: misc.c 43790 2009-10-27 10:34:16Z dgorbachev $ 00020 * 00021 * COPYRIGHT: See COPYING in the top level directory 00022 * PROJECT: ReactOS system libraries 00023 * FILE: lib/userenv/misc.c 00024 * PURPOSE: User profile code 00025 * PROGRAMMER: Eric Kohl 00026 */ 00027 00028 #include <precomp.h> 00029 00030 #define NDEBUG 00031 #include <debug.h> 00032 00033 static SID_IDENTIFIER_AUTHORITY LocalSystemAuthority = {SECURITY_NT_AUTHORITY}; 00034 static SID_IDENTIFIER_AUTHORITY WorldAuthority = {SECURITY_WORLD_SID_AUTHORITY}; 00035 00036 /* FUNCTIONS ***************************************************************/ 00037 00038 LPWSTR 00039 AppendBackslash (LPWSTR String) 00040 { 00041 ULONG Length; 00042 00043 Length = lstrlenW (String); 00044 if (String[Length - 1] != L'\\') 00045 { 00046 String[Length] = L'\\'; 00047 Length++; 00048 String[Length] = (WCHAR)0; 00049 } 00050 00051 return &String[Length]; 00052 } 00053 00054 00055 BOOL 00056 GetUserSidFromToken (HANDLE hToken, 00057 PUNICODE_STRING SidString) 00058 { 00059 PSID_AND_ATTRIBUTES SidBuffer, nsb; 00060 ULONG Length; 00061 NTSTATUS Status; 00062 00063 Length = 256; 00064 SidBuffer = LocalAlloc (LMEM_FIXED, 00065 Length); 00066 if (SidBuffer == NULL) 00067 return FALSE; 00068 00069 Status = NtQueryInformationToken (hToken, 00070 TokenUser, 00071 (PVOID)SidBuffer, 00072 Length, 00073 &Length); 00074 if (Status == STATUS_BUFFER_TOO_SMALL) 00075 { 00076 nsb = LocalReAlloc (SidBuffer, 00077 Length, 00078 LMEM_MOVEABLE); 00079 if (nsb == NULL) 00080 { 00081 LocalFree((HLOCAL)SidBuffer); 00082 return FALSE; 00083 } 00084 00085 SidBuffer = nsb; 00086 Status = NtQueryInformationToken (hToken, 00087 TokenUser, 00088 (PVOID)SidBuffer, 00089 Length, 00090 &Length); 00091 } 00092 00093 if (!NT_SUCCESS (Status)) 00094 { 00095 LocalFree ((HLOCAL)SidBuffer); 00096 SetLastError (RtlNtStatusToDosError (Status)); 00097 return FALSE; 00098 } 00099 00100 DPRINT ("SidLength: %lu\n", RtlLengthSid (SidBuffer[0].Sid)); 00101 00102 Status = RtlConvertSidToUnicodeString (SidString, 00103 SidBuffer[0].Sid, 00104 TRUE); 00105 00106 LocalFree ((HLOCAL)SidBuffer); 00107 00108 if (!NT_SUCCESS (Status)) 00109 { 00110 SetLastError (RtlNtStatusToDosError (Status)); 00111 return FALSE; 00112 } 00113 00114 DPRINT ("SidString.Length: %lu\n", SidString->Length); 00115 DPRINT ("SidString.MaximumLength: %lu\n", SidString->MaximumLength); 00116 DPRINT ("SidString: '%wZ'\n", SidString); 00117 00118 return TRUE; 00119 } 00120 00121 PSECURITY_DESCRIPTOR 00122 CreateDefaultSecurityDescriptor(VOID) 00123 { 00124 PSID LocalSystemSid = NULL; 00125 PSID AdministratorsSid = NULL; 00126 PSID EveryoneSid = NULL; 00127 PACL Dacl; 00128 DWORD DaclSize; 00129 PSECURITY_DESCRIPTOR pSD = NULL; 00130 00131 /* create the SYSTEM, Administrators and Everyone SIDs */ 00132 if (!AllocateAndInitializeSid(&LocalSystemAuthority, 00133 1, 00134 SECURITY_LOCAL_SYSTEM_RID, 00135 0, 00136 0, 00137 0, 00138 0, 00139 0, 00140 0, 00141 0, 00142 &LocalSystemSid) || 00143 !AllocateAndInitializeSid(&LocalSystemAuthority, 00144 2, 00145 SECURITY_BUILTIN_DOMAIN_RID, 00146 DOMAIN_ALIAS_RID_ADMINS, 00147 0, 00148 0, 00149 0, 00150 0, 00151 0, 00152 0, 00153 &AdministratorsSid) || 00154 !AllocateAndInitializeSid(&WorldAuthority, 00155 1, 00156 SECURITY_WORLD_RID, 00157 0, 00158 0, 00159 0, 00160 0, 00161 0, 00162 0, 00163 0, 00164 &EveryoneSid)) 00165 { 00166 DPRINT1("Failed initializing the SIDs for the default security descriptor (0x%p, 0x%p, 0x%p)\n", 00167 LocalSystemSid, AdministratorsSid, EveryoneSid); 00168 goto Cleanup; 00169 } 00170 00171 /* allocate the security descriptor and DACL */ 00172 DaclSize = sizeof(ACL) + 00173 ((GetLengthSid(LocalSystemSid) + 00174 GetLengthSid(AdministratorsSid) + 00175 GetLengthSid(EveryoneSid)) + 00176 (3 * FIELD_OFFSET(ACCESS_ALLOWED_ACE, 00177 SidStart))); 00178 00179 pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LMEM_FIXED, 00180 (SIZE_T)DaclSize + sizeof(SECURITY_DESCRIPTOR)); 00181 if (pSD == NULL) 00182 { 00183 DPRINT1("Failed to allocate the default security descriptor and ACL\n"); 00184 goto Cleanup; 00185 } 00186 00187 if (!InitializeSecurityDescriptor(pSD, 00188 SECURITY_DESCRIPTOR_REVISION)) 00189 { 00190 DPRINT1("Failed to initialize the default security descriptor\n"); 00191 goto Cleanup; 00192 } 00193 00194 /* initialize and build the DACL */ 00195 Dacl = (PACL)((ULONG_PTR)pSD + sizeof(SECURITY_DESCRIPTOR)); 00196 if (!InitializeAcl(Dacl, 00197 (DWORD)DaclSize, 00198 ACL_REVISION)) 00199 { 00200 DPRINT1("Failed to initialize the DACL of the default security descriptor\n"); 00201 goto Cleanup; 00202 } 00203 00204 /* add the SYSTEM Ace */ 00205 if (!AddAccessAllowedAce(Dacl, 00206 ACL_REVISION, 00207 GENERIC_ALL, 00208 LocalSystemSid)) 00209 { 00210 DPRINT1("Failed to add the SYSTEM ACE\n"); 00211 goto Cleanup; 00212 } 00213 00214 /* add the Administrators Ace */ 00215 if (!AddAccessAllowedAce(Dacl, 00216 ACL_REVISION, 00217 GENERIC_ALL, 00218 AdministratorsSid)) 00219 { 00220 DPRINT1("Failed to add the Administrators ACE\n"); 00221 goto Cleanup; 00222 } 00223 00224 /* add the Everyone Ace */ 00225 if (!AddAccessAllowedAce(Dacl, 00226 ACL_REVISION, 00227 GENERIC_EXECUTE, 00228 EveryoneSid)) 00229 { 00230 DPRINT1("Failed to add the Everyone ACE\n"); 00231 goto Cleanup; 00232 } 00233 00234 /* set the DACL */ 00235 if (!SetSecurityDescriptorDacl(pSD, 00236 TRUE, 00237 Dacl, 00238 FALSE)) 00239 { 00240 DPRINT1("Failed to set the DACL of the default security descriptor\n"); 00241 00242 Cleanup: 00243 if (pSD != NULL) 00244 { 00245 LocalFree((HLOCAL)pSD); 00246 pSD = NULL; 00247 } 00248 } 00249 00250 if (LocalSystemSid != NULL) 00251 { 00252 FreeSid(LocalSystemSid); 00253 } 00254 if (AdministratorsSid != NULL) 00255 { 00256 FreeSid(AdministratorsSid); 00257 } 00258 if (EveryoneSid != NULL) 00259 { 00260 FreeSid(EveryoneSid); 00261 } 00262 00263 return pSD; 00264 } 00265 00266 /* Dynamic DLL loading interface **********************************************/ 00267 00268 /* OLE32.DLL import table */ 00269 DYN_MODULE DynOle32 = 00270 { 00271 L"ole32.dll", 00272 { 00273 "CoInitialize", 00274 "CoCreateInstance", 00275 "CoUninitialize", 00276 NULL 00277 } 00278 }; 00279 00280 00281 /* 00282 * Use this function to load functions from other modules. We cannot statically 00283 * link to e.g. ole32.dll because those dlls would get loaded on startup with 00284 * winlogon and they may try to register classes etc when not even a window station 00285 * has been created! 00286 */ 00287 BOOL 00288 LoadDynamicImports(PDYN_MODULE Module, PDYN_FUNCS DynFuncs) 00289 { 00290 LPSTR *fname; 00291 PVOID *fn; 00292 00293 ZeroMemory(DynFuncs, sizeof(DYN_FUNCS)); 00294 00295 DynFuncs->hModule = LoadLibraryW(Module->Library); 00296 if (!DynFuncs->hModule) 00297 { 00298 return FALSE; 00299 } 00300 00301 fn = &DynFuncs->fn.foo; 00302 00303 /* load the imports */ 00304 for (fname = Module->Functions; *fname != NULL; fname++) 00305 { 00306 *fn = GetProcAddress(DynFuncs->hModule, *fname); 00307 if (*fn == NULL) 00308 { 00309 FreeLibrary(DynFuncs->hModule); 00310 DynFuncs->hModule = (HMODULE)0; 00311 00312 return FALSE; 00313 } 00314 00315 fn++; 00316 } 00317 00318 return TRUE; 00319 } 00320 00321 00322 VOID 00323 UnloadDynamicImports(PDYN_FUNCS DynFuncs) 00324 { 00325 if (DynFuncs->hModule) 00326 { 00327 FreeLibrary(DynFuncs->hModule); 00328 DynFuncs->hModule = (HMODULE)0; 00329 } 00330 } 00331 00332 /* EOF */ Generated on Sun May 27 2012 04:16:47 for ReactOS by
1.7.6.1
|