Leaked source code of windows server 2003
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.

373 lines
10 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. bWrkrLockInitialized = FALSE;
  140. RtlDeleteResource(&gWrkrLock);
  141. }
  142. if (TRUE == bCacheLockInitialized)
  143. {
  144. bCacheLockInitialized = FALSE;
  145. RtlDeleteResource(&gCacheLock);
  146. }
  147. if (TRUE == bLocalSidCacheLockInitialized)
  148. {
  149. bLocalSidCacheLockInitialized = FALSE;
  150. RtlDeleteResource(&gLocalSidCacheLock);
  151. }
  152. AccctrlFreeSidNameCache();
  153. AccctrlFreeIdNameCache();
  154. break;
  155. case DLL_PROCESS_DETACH:
  156. #if DBG
  157. DebugUninit();
  158. #endif
  159. if (TRUE == bWrkrLockInitialized)
  160. {
  161. bWrkrLockInitialized = FALSE;
  162. RtlDeleteResource(&gWrkrLock);
  163. }
  164. if (TRUE == bCacheLockInitialized)
  165. {
  166. bCacheLockInitialized = FALSE;
  167. RtlDeleteResource(&gCacheLock);
  168. }
  169. if (TRUE == bLocalSidCacheLockInitialized)
  170. {
  171. bLocalSidCacheLockInitialized = FALSE;
  172. RtlDeleteResource(&gLocalSidCacheLock);
  173. }
  174. AccctrlFreeSidNameCache();
  175. AccctrlFreeIdNameCache();
  176. AccctrlFreeRightsCache();
  177. if(WmiGuidHandle)
  178. {
  179. CloseHandle(WmiGuidHandle);
  180. }
  181. break;
  182. default:
  183. break;
  184. }
  185. return(fRet);
  186. }
  187. #if DBG
  188. VOID
  189. DebugDumpSid(PSTR pszTag,
  190. PSID pSid)
  191. {
  192. if(pSid == NULL)
  193. {
  194. acDebugOut((DEB_TRACE_SD, "%s NULL\n", pszTag));
  195. }
  196. else
  197. {
  198. UNICODE_STRING SidString;
  199. NTSTATUS Status;
  200. Status = RtlConvertSidToUnicodeString(&SidString,
  201. pSid,
  202. TRUE);
  203. if(!NT_SUCCESS(Status))
  204. {
  205. acDebugOut((DEB_ERROR, "%s Can't convert sid to string: 0x%lx\n",
  206. pszTag, Status));
  207. }
  208. else
  209. {
  210. acDebugOut((DEB_TRACE_SD, "%s %wZ\n", pszTag, &SidString));
  211. RtlFreeUnicodeString(&SidString);
  212. }
  213. }
  214. }
  215. VOID
  216. DebugDumpSD(PSTR pszTag,
  217. PSECURITY_DESCRIPTOR pSD)
  218. {
  219. if(pSD == NULL)
  220. {
  221. acDebugOut((DEB_TRACE_SD,"%s NULL\n", pszTag));
  222. }
  223. else
  224. {
  225. PISECURITY_DESCRIPTOR pISD = (PISECURITY_DESCRIPTOR)pSD;
  226. acDebugOut((DEB_TRACE_SD,"%s: 0x%lx\n", pszTag, pSD));
  227. acDebugOut((DEB_TRACE_SD,"\tRevision: 0x%lx\n",pISD->Revision));
  228. acDebugOut((DEB_TRACE_SD,"\tSbz1: 0x%lx\n", pISD->Sbz1));
  229. acDebugOut((DEB_TRACE_SD,"\tControl: 0x%lx\n",pISD->Control));
  230. DebugDumpSid("\tOwner", RtlpOwnerAddrSecurityDescriptor(pISD));
  231. DebugDumpSid("\tGroup", RtlpGroupAddrSecurityDescriptor(pISD));
  232. DebugDumpAcl("\tDAcl", RtlpDaclAddrSecurityDescriptor(pISD));
  233. DebugDumpAcl("\tSAcl", RtlpSaclAddrSecurityDescriptor(pISD));
  234. }
  235. }
  236. VOID
  237. DebugDumpAcl(PSTR pszTag,
  238. PACL pAcl)
  239. {
  240. ACL_SIZE_INFORMATION AclSize;
  241. ACL_REVISION_INFORMATION AclRev;
  242. PKNOWN_ACE pAce;
  243. PSID pSid;
  244. DWORD iIndex;
  245. if(pAcl == NULL)
  246. {
  247. acDebugOut((DEB_TRACE_SD,"%s NULL\n", pszTag));
  248. }
  249. else
  250. {
  251. acDebugOut((DEB_TRACE_SD, "%s: 0x%lx\n", pszTag, pAcl));
  252. if(GetAclInformation(pAcl,
  253. &AclRev,
  254. sizeof(ACL_REVISION_INFORMATION),
  255. AclRevisionInformation) == FALSE)
  256. {
  257. acDebugOut((DEB_TRACE_SD,
  258. "GetAclInformation [Revision] failed: %lu\n",
  259. GetLastError()));
  260. return;
  261. }
  262. if(GetAclInformation(pAcl,
  263. &AclSize,
  264. sizeof(ACL_SIZE_INFORMATION),
  265. AclSizeInformation) == FALSE)
  266. {
  267. acDebugOut((DEB_TRACE_SD,
  268. "GetAclInformation [Size] failed: %lu\n",
  269. GetLastError()));
  270. return;
  271. }
  272. acDebugOut((DEB_TRACE_SD, "\t\tRevision: %lu\n", AclRev.AclRevision));
  273. acDebugOut((DEB_TRACE_SD, "\t\tAceCount: %lu\n", AclSize.AceCount));
  274. acDebugOut((DEB_TRACE_SD, "\t\tInUse: %lu\n", AclSize.AclBytesInUse));
  275. acDebugOut((DEB_TRACE_SD, "\t\tFree: %lu\n", AclSize.AclBytesFree));
  276. acDebugOut((DEB_TRACE_SD, "\t\tFlags: %lu\n", pAcl->Sbz1));
  277. //
  278. // Now, dump all of the aces
  279. //
  280. pAce = FirstAce(pAcl);
  281. for(iIndex = 0; iIndex < pAcl->AceCount; iIndex++)
  282. {
  283. acDebugOut((DEB_TRACE_SD,"\t\tAce %lu\n", iIndex));
  284. acDebugOut((DEB_TRACE_SD,"\t\t\tType: %lu\n",
  285. pAce->Header.AceType));
  286. acDebugOut((DEB_TRACE_SD,"\t\t\tFlags: 0x%lx\n",
  287. pAce->Header.AceFlags));
  288. acDebugOut((DEB_TRACE_SD,"\t\t\tSize: 0x%lx\n",
  289. pAce->Header.AceSize));
  290. acDebugOut((DEB_TRACE_SD,"\t\t\tMask: 0x%lx\n",
  291. pAce->Mask));
  292. //
  293. // If it's an object ace, dump the guids
  294. //
  295. if(IsObjectAceType(pAce))
  296. {
  297. DebugDumpGuid("\t\t\tObjectId", RtlObjectAceObjectType(pAce));
  298. DebugDumpGuid("\t\t\tInheritId",
  299. RtlObjectAceInheritedObjectType(pAce));
  300. DebugDumpSid("\t\t\tSid", RtlObjectAceSid(pAce));
  301. }
  302. else
  303. {
  304. DebugDumpSid("\t\t\tSid", ((PSID)&(pAce->SidStart)));
  305. }
  306. pAce = NextAce(pAce);
  307. }
  308. }
  309. }
  310. #endif