Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

367 lines
9.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1996.
  5. //
  6. // File: INIT.C
  7. //
  8. // Contents: DLL Initialization routine
  9. //
  10. // History: 22-Aug-96 MacM Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <nt.h>
  14. #include <ntrtl.h>
  15. #include <nturtl.h>
  16. #include <windows.h>
  17. #include <accdbg.h>
  18. #include <lucache.h>
  19. #include <seopaque.h>
  20. HINSTANCE ghDll;
  21. RTL_RESOURCE gWrkrLock;
  22. BOOL bWrkrLockInitialized = FALSE;
  23. RTL_RESOURCE gCacheLock;
  24. BOOL bCacheLockInitialized = FALSE;
  25. RTL_RESOURCE gLocalSidCacheLock;
  26. BOOL bLocalSidCacheLockInitialized = FALSE;
  27. #if DBG
  28. #include <seopaque.h>
  29. #include <sertlp.h>
  30. #define IsObjectAceType(Ace) ( \
  31. (((PACE_HEADER)(Ace))->AceType >= ACCESS_MIN_MS_OBJECT_ACE_TYPE) && \
  32. (((PACE_HEADER)(Ace))->AceType <= ACCESS_MAX_MS_OBJECT_ACE_TYPE) \
  33. )
  34. DEFINE_DEBUG2(ac);
  35. DEBUG_KEY acDebugKeys[] = {{DEB_ERROR, "Error"},
  36. {DEB_WARN, "Warn"},
  37. {DEB_TRACE, "Trace"},
  38. {DEB_TRACE_API, "AccAPI"},
  39. {DEB_TRACE_ACC, "AccList"},
  40. {DEB_TRACE_CACHE, "AccCache"},
  41. {DEB_TRACE_PROP, "AccProp"},
  42. {DEB_TRACE_SD, "AccSD"},
  43. {DEB_TRACE_LOOKUP, "AccLU"},
  44. {DEB_TRACE_MEM, "AccMem"},
  45. {DEB_TRACE_HANDLE, "AccHandle"},
  46. {0, NULL}};
  47. VOID
  48. DebugInitialize()
  49. {
  50. acInitDebug(acDebugKeys);
  51. }
  52. VOID
  53. DebugUninit()
  54. {
  55. acUnloadDebug();
  56. }
  57. PVOID DebugAlloc(ULONG cSize)
  58. {
  59. PVOID pv = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, cSize);
  60. acDebugOut((DEB_TRACE_MEM, "Allocated %lu at 0x%lx\n", cSize, pv ));
  61. return(pv);
  62. }
  63. VOID DebugFree(PVOID pv)
  64. {
  65. if(pv == NULL)
  66. {
  67. return;
  68. }
  69. acDebugOut((DEB_TRACE_MEM, "Freeing 0x%lx\n", pv ));
  70. ASSERT(RtlValidateHeap(RtlProcessHeap(),0,NULL));
  71. LocalFree(pv);
  72. }
  73. #endif // DBG
  74. //+----------------------------------------------------------------------------
  75. //
  76. // Function: DllMain
  77. //
  78. // Synopsis: Initialize state for NTMARTA.DLL
  79. //
  80. // Arguments: [hInstance] -- Module handle
  81. // [dwReason] -- Reason this function is being called
  82. // [lpReserved] -- Reserved
  83. //
  84. // Returns: TRUE -- Success
  85. // FALSE -- Failure
  86. //
  87. //-----------------------------------------------------------------------------
  88. BOOL
  89. WINAPI
  90. DllMain(HINSTANCE hInstance,
  91. DWORD dwReason,
  92. LPVOID lpReserved)
  93. {
  94. BOOL fRet = TRUE;
  95. switch(dwReason)
  96. {
  97. case DLL_PROCESS_ATTACH:
  98. DisableThreadLibraryCalls(hInstance);
  99. ghDll = hInstance;
  100. __try
  101. {
  102. RtlInitializeResource(&gWrkrLock);
  103. bWrkrLockInitialized = TRUE;
  104. RtlInitializeResource(&gCacheLock);
  105. bCacheLockInitialized = TRUE;
  106. RtlInitializeResource(&gLocalSidCacheLock);
  107. bLocalSidCacheLockInitialized = TRUE;
  108. }
  109. __except (EXCEPTION_EXECUTE_HANDLER)
  110. {
  111. fRet = FALSE;
  112. }
  113. if (FALSE == fRet)
  114. {
  115. goto ProcessAttachCleanup;
  116. }
  117. #if DBG
  118. DebugInitialize();
  119. #endif
  120. if(AccctrlInitializeSidNameCache() != ERROR_SUCCESS)
  121. {
  122. fRet = FALSE;
  123. goto ProcessAttachCleanup;
  124. }
  125. if(AccctrlInitializeIdNameCache() != ERROR_SUCCESS)
  126. {
  127. fRet = FALSE;
  128. goto ProcessAttachCleanup;
  129. }
  130. if (AccctrlInitializeRightsCache() != ERROR_SUCCESS)
  131. {
  132. fRet = FALSE;
  133. goto ProcessAttachCleanup;
  134. }
  135. return TRUE;
  136. ProcessAttachCleanup:
  137. if (TRUE == bWrkrLockInitialized)
  138. {
  139. RtlDeleteResource(&gWrkrLock);
  140. }
  141. if (TRUE == bCacheLockInitialized)
  142. {
  143. RtlDeleteResource(&gCacheLock);
  144. }
  145. if (TRUE == bLocalSidCacheLockInitialized)
  146. {
  147. RtlDeleteResource(&gLocalSidCacheLock);
  148. }
  149. AccctrlFreeSidNameCache();
  150. AccctrlFreeIdNameCache();
  151. break;
  152. case DLL_PROCESS_DETACH:
  153. #if DBG
  154. DebugUninit();
  155. #endif
  156. if (TRUE == bWrkrLockInitialized)
  157. {
  158. RtlDeleteResource(&gWrkrLock);
  159. }
  160. if (TRUE == bCacheLockInitialized)
  161. {
  162. RtlDeleteResource(&gCacheLock);
  163. }
  164. if (TRUE == bLocalSidCacheLockInitialized)
  165. {
  166. RtlDeleteResource(&gLocalSidCacheLock);
  167. }
  168. AccctrlFreeSidNameCache();
  169. AccctrlFreeIdNameCache();
  170. AccctrlFreeRightsCache();
  171. if(WmiGuidHandle)
  172. {
  173. CloseHandle(WmiGuidHandle);
  174. }
  175. break;
  176. default:
  177. break;
  178. }
  179. return(fRet);
  180. }
  181. #if DBG
  182. VOID
  183. DebugDumpSid(PSTR pszTag,
  184. PSID pSid)
  185. {
  186. if(pSid == NULL)
  187. {
  188. acDebugOut((DEB_TRACE_SD, "%s NULL\n", pszTag));
  189. }
  190. else
  191. {
  192. UNICODE_STRING SidString;
  193. NTSTATUS Status;
  194. Status = RtlConvertSidToUnicodeString(&SidString,
  195. pSid,
  196. TRUE);
  197. if(!NT_SUCCESS(Status))
  198. {
  199. acDebugOut((DEB_ERROR, "%s Can't convert sid to string: 0x%lx\n",
  200. pszTag, Status));
  201. }
  202. else
  203. {
  204. acDebugOut((DEB_TRACE_SD, "%s %wZ\n", pszTag, &SidString));
  205. RtlFreeUnicodeString(&SidString);
  206. }
  207. }
  208. }
  209. VOID
  210. DebugDumpSD(PSTR pszTag,
  211. PSECURITY_DESCRIPTOR pSD)
  212. {
  213. if(pSD == NULL)
  214. {
  215. acDebugOut((DEB_TRACE_SD,"%s NULL\n", pszTag));
  216. }
  217. else
  218. {
  219. PISECURITY_DESCRIPTOR pISD = (PISECURITY_DESCRIPTOR)pSD;
  220. acDebugOut((DEB_TRACE_SD,"%s: 0x%lx\n", pszTag, pSD));
  221. acDebugOut((DEB_TRACE_SD,"\tRevision: 0x%lx\n",pISD->Revision));
  222. acDebugOut((DEB_TRACE_SD,"\tSbz1: 0x%lx\n", pISD->Sbz1));
  223. acDebugOut((DEB_TRACE_SD,"\tControl: 0x%lx\n",pISD->Control));
  224. DebugDumpSid("\tOwner", RtlpOwnerAddrSecurityDescriptor(pISD));
  225. DebugDumpSid("\tGroup", RtlpGroupAddrSecurityDescriptor(pISD));
  226. DebugDumpAcl("\tDAcl", RtlpDaclAddrSecurityDescriptor(pISD));
  227. DebugDumpAcl("\tSAcl", RtlpSaclAddrSecurityDescriptor(pISD));
  228. }
  229. }
  230. VOID
  231. DebugDumpAcl(PSTR pszTag,
  232. PACL pAcl)
  233. {
  234. ACL_SIZE_INFORMATION AclSize;
  235. ACL_REVISION_INFORMATION AclRev;
  236. PKNOWN_ACE pAce;
  237. PSID pSid;
  238. DWORD iIndex;
  239. if(pAcl == NULL)
  240. {
  241. acDebugOut((DEB_TRACE_SD,"%s NULL\n", pszTag));
  242. }
  243. else
  244. {
  245. acDebugOut((DEB_TRACE_SD, "%s: 0x%lx\n", pszTag, pAcl));
  246. if(GetAclInformation(pAcl,
  247. &AclRev,
  248. sizeof(ACL_REVISION_INFORMATION),
  249. AclRevisionInformation) == FALSE)
  250. {
  251. acDebugOut((DEB_TRACE_SD,
  252. "GetAclInformation [Revision] failed: %lu\n",
  253. GetLastError()));
  254. return;
  255. }
  256. if(GetAclInformation(pAcl,
  257. &AclSize,
  258. sizeof(ACL_SIZE_INFORMATION),
  259. AclSizeInformation) == FALSE)
  260. {
  261. acDebugOut((DEB_TRACE_SD,
  262. "GetAclInformation [Size] failed: %lu\n",
  263. GetLastError()));
  264. return;
  265. }
  266. acDebugOut((DEB_TRACE_SD, "\t\tRevision: %lu\n", AclRev.AclRevision));
  267. acDebugOut((DEB_TRACE_SD, "\t\tAceCount: %lu\n", AclSize.AceCount));
  268. acDebugOut((DEB_TRACE_SD, "\t\tInUse: %lu\n", AclSize.AclBytesInUse));
  269. acDebugOut((DEB_TRACE_SD, "\t\tFree: %lu\n", AclSize.AclBytesFree));
  270. acDebugOut((DEB_TRACE_SD, "\t\tFlags: %lu\n", pAcl->Sbz1));
  271. //
  272. // Now, dump all of the aces
  273. //
  274. pAce = FirstAce(pAcl);
  275. for(iIndex = 0; iIndex < pAcl->AceCount; iIndex++)
  276. {
  277. acDebugOut((DEB_TRACE_SD,"\t\tAce %lu\n", iIndex));
  278. acDebugOut((DEB_TRACE_SD,"\t\t\tType: %lu\n",
  279. pAce->Header.AceType));
  280. acDebugOut((DEB_TRACE_SD,"\t\t\tFlags: 0x%lx\n",
  281. pAce->Header.AceFlags));
  282. acDebugOut((DEB_TRACE_SD,"\t\t\tSize: 0x%lx\n",
  283. pAce->Header.AceSize));
  284. acDebugOut((DEB_TRACE_SD,"\t\t\tMask: 0x%lx\n",
  285. pAce->Mask));
  286. //
  287. // If it's an object ace, dump the guids
  288. //
  289. if(IsObjectAceType(pAce))
  290. {
  291. DebugDumpGuid("\t\t\tObjectId", RtlObjectAceObjectType(pAce));
  292. DebugDumpGuid("\t\t\tInheritId",
  293. RtlObjectAceInheritedObjectType(pAce));
  294. DebugDumpSid("\t\t\tSid", RtlObjectAceSid(pAce));
  295. }
  296. else
  297. {
  298. DebugDumpSid("\t\t\tSid", ((PSID)&(pAce->SidStart)));
  299. }
  300. pAce = NextAce(pAce);
  301. }
  302. }
  303. }
  304. #endif