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.

517 lines
12 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1997 - 1999
  3. Module Name:
  4. cache.cxx
  5. Abstract:
  6. Code to create, destroy and manipulate SENS cache. Initially, it
  7. contains system connectivity state.
  8. Author:
  9. Gopal Parupudi <GopalP>
  10. [Notes:]
  11. optional-notes
  12. Revision History:
  13. GopalP 2/8/1999 Start.
  14. --*/
  15. #include <precomp.hxx>
  16. //
  17. // Globals
  18. //
  19. HANDLE ghSensFileMap;
  20. PSENS_CACHE gpSensCache;
  21. BOOL
  22. CreateCachePermissions(
  23. PSECURITY_ATTRIBUTES psa,
  24. PSECURITY_DESCRIPTOR psd,
  25. PACL *ppOutAcl
  26. )
  27. /*++
  28. Routine Description:
  29. Create security attributes for SENS Cache. It has following permissions:
  30. o Everyone - READ
  31. o LocalSystem - READ & WRITE
  32. Arguments:
  33. psa - Pointer to a security attributes structure.
  34. psd - Pointer to a security descriptor structure.
  35. pOutAcl - Pointer to an ACL. Needs to be cleaned when the return
  36. value is TRUE. Should be cleaned up on failure.
  37. Notes:
  38. This code should work on NT4 also.
  39. Return Value:
  40. TRUE, if successful.
  41. FALSE, otherwise
  42. --*/
  43. {
  44. BOOL bRetVal;
  45. int cbAcl;
  46. PACL pAcl;
  47. PSID pWorldSid;
  48. PSID pLocalSystemSid;
  49. SID_IDENTIFIER_AUTHORITY WorldAuthority = SECURITY_WORLD_SID_AUTHORITY;
  50. bRetVal = FALSE;
  51. *ppOutAcl = NULL;
  52. pAcl = NULL;
  53. pWorldSid = NULL;
  54. pLocalSystemSid = (PSID)&LocalSystem;
  55. //
  56. // Allocate WorldSid
  57. //
  58. bRetVal = AllocateAndInitializeSid(
  59. &WorldAuthority, // Pointer to identifier authority
  60. 1, // Count of subauthority
  61. SECURITY_WORLD_RID, // Subauthority 0
  62. 0, // Subauthority 1
  63. 0, // Subauthority 2
  64. 0, // Subauthority 3
  65. 0, // Subauthority 4
  66. 0, // Subauthority 5
  67. 0, // Subauthority 6
  68. 0, // Subauthority 7
  69. &pWorldSid // pointer to pointer to SID
  70. );
  71. if (FALSE == bRetVal)
  72. {
  73. SensPrintA(SENS_ERR, ("CreateCachePermissions(): AllocateAndInitiali"
  74. "zeSid(World) failed with %d.\n", GetLastError()));
  75. goto Cleanup;
  76. }
  77. ASSERT(RtlValidSid(pLocalSystemSid));
  78. //
  79. // Create the ACL with the desired ACEs
  80. //
  81. // Calculate the length of the required ACL buffer with 2 ACEs.
  82. cbAcl = sizeof (ACL)
  83. + 2 * sizeof (ACCESS_ALLOWED_ACE)
  84. + GetLengthSid(pWorldSid)
  85. + GetLengthSid(pLocalSystemSid);
  86. // Allocate the ACL buffer.
  87. pAcl = (PACL) new char[cbAcl];
  88. if (NULL == pAcl)
  89. {
  90. SensPrintA(SENS_ERR, ("CreateCachePermissions(): Failed to allocate"
  91. "an ACL.\n"));
  92. goto Cleanup;
  93. }
  94. // Initialize the ACL.
  95. bRetVal = InitializeAcl(
  96. pAcl, // Pointer to the ACL
  97. cbAcl, // Size of ACL
  98. ACL_REVISION // Revision level of ACL
  99. );
  100. if (FALSE == bRetVal)
  101. {
  102. SensPrintA(SENS_ERR, ("CreateCachePermissions(): InitializeAcl() "
  103. "failed with %d.\n", GetLastError()));
  104. goto Cleanup;
  105. }
  106. // Add ACE with FILE_MAP_READ for Everyone
  107. bRetVal = AddAccessAllowedAce(
  108. pAcl, // Pointer to the ACL
  109. ACL_REVISION, // ACL revision level
  110. FILE_MAP_READ, // Access Mask
  111. pWorldSid // Pointer to SID
  112. );
  113. if (FALSE == bRetVal)
  114. {
  115. SensPrintA(SENS_ERR, ("CreateCachePermissions(): AddAccessAllowedAce()"
  116. " failed with %d.\n", GetLastError()));
  117. goto Cleanup;
  118. }
  119. // Add ACE with FILE_MAP_WRITE for LocalSystem
  120. bRetVal = AddAccessAllowedAce(
  121. pAcl, // Pointer to the ACL
  122. ACL_REVISION, // ACL revision level
  123. FILE_MAP_WRITE, // Access Mask
  124. pLocalSystemSid // Pointer to SID
  125. );
  126. if (FALSE == bRetVal)
  127. {
  128. SensPrintA(SENS_ERR, ("CreateCachePermissions(): AddAccessAllowedAce()"
  129. " failed with %d.\n", GetLastError()));
  130. goto Cleanup;
  131. }
  132. // Initialize Security Descriptor.
  133. bRetVal = InitializeSecurityDescriptor(
  134. psd, // Pointer to SD
  135. SECURITY_DESCRIPTOR_REVISION // SD revision
  136. );
  137. if (FALSE == bRetVal)
  138. {
  139. SensPrintA(SENS_ERR, ("CreateCachePermissions(): "
  140. "InitializeSecurityDescriptor() failed with a GLE of %d\n",
  141. GetLastError()));
  142. goto Cleanup;
  143. }
  144. // Set the Dacl.
  145. bRetVal = SetSecurityDescriptorDacl(
  146. psd, // Security Descriptor
  147. TRUE, // Dacl present
  148. pAcl, // The Dacl
  149. FALSE // Not defaulted
  150. );
  151. if (FALSE == bRetVal)
  152. {
  153. SensPrintA(SENS_ERR, ("CreateCachePermissions(): "
  154. "SetSecurityDescriptorDacl() failed with a GLE of %d\n",
  155. GetLastError()));
  156. goto Cleanup;
  157. }
  158. psa->nLength = sizeof(SECURITY_ATTRIBUTES);
  159. psa->lpSecurityDescriptor = psd;
  160. psa->bInheritHandle = FALSE;
  161. bRetVal = TRUE;
  162. Cleanup:
  163. //
  164. // Cleanup
  165. //
  166. if (pWorldSid)
  167. {
  168. FreeSid(pWorldSid);
  169. }
  170. //
  171. // On failure, we clean the ACL up. On success, the caller cleans
  172. // it up after using it.
  173. //
  174. if (FALSE == bRetVal)
  175. {
  176. if (pAcl)
  177. {
  178. delete pAcl;
  179. }
  180. }
  181. else
  182. {
  183. *ppOutAcl = pAcl;
  184. }
  185. return bRetVal;
  186. }
  187. BOOL
  188. CreateSensCache(
  189. void
  190. )
  191. /*++
  192. Routine Description:
  193. Create a cache for information maintained by SENS.
  194. Arguments:
  195. None.
  196. Return Value:
  197. TRUE, if successful.
  198. FALSE, otherwise.
  199. --*/
  200. {
  201. BOOL bRetVal;
  202. SECURITY_DESCRIPTOR sd;
  203. SECURITY_ATTRIBUTES sa, *psa;
  204. PACL pAcl;
  205. bRetVal = CreateCachePermissions(&sa, &sd, &pAcl);
  206. if (FALSE == bRetVal)
  207. {
  208. SensPrintA(SENS_ERR, ("CreateSensCache(): CreateCachePermissions() "
  209. "failed with a GLE of %d\n", GetLastError()));
  210. goto Cleanup;
  211. }
  212. psa = &sa;
  213. //
  214. // First, create a file mapping object
  215. //
  216. ghSensFileMap = CreateFileMapping(
  217. INVALID_HANDLE_VALUE, // Handle of file to map
  218. psa, // Optional security attributes
  219. PAGE_READWRITE, // Protection for mapping object
  220. 0, // High-order 32 bits of size
  221. SENS_CACHE_SIZE, // Low-order 32 bits of size
  222. SENS_CACHE_NAME // Name of the file mapping object
  223. );
  224. // Free Acl.
  225. delete pAcl;
  226. if (NULL == ghSensFileMap)
  227. {
  228. SensPrintA(SENS_ERR, ("CreateSensCache(): CreateFileMapping() failed "
  229. "with a GLE of %d\n", GetLastError()));
  230. goto Cleanup;
  231. }
  232. else
  233. if (GetLastError() == ERROR_ALREADY_EXISTS)
  234. {
  235. SensPrintA(SENS_ERR, ("CreateSensCache(): File Mapping exists!\n"));
  236. }
  237. //
  238. // Now, map a view of the file into the address space
  239. //
  240. gpSensCache = (PSENS_CACHE) MapViewOfFile(
  241. ghSensFileMap, // Map file object
  242. FILE_MAP_WRITE, // Access mode
  243. 0, // High-order 32 bits of file offset
  244. 0, // Low-order 32 bits of file offset
  245. 0 // Number of bytes to map
  246. );
  247. if (NULL == gpSensCache)
  248. {
  249. SensPrintA(SENS_ERR, ("CreateSensCache(): MapViewOfFile() failed with "
  250. "a GLE of %d\n", GetLastError()));
  251. goto Cleanup;
  252. }
  253. //
  254. // Initialize the cache.
  255. //
  256. memset(gpSensCache, 0x0, sizeof(SENS_CACHE));
  257. gpSensCache->dwCacheVer = SENS_CACHE_VERSION;
  258. gpSensCache->dwCacheSize = sizeof(SENS_CACHE);
  259. gpSensCache->dwCacheInitTime = GetTickCount();
  260. bRetVal = TRUE;
  261. SensPrintA(SENS_INFO, ("[%d] CreateSensCache(): Cache initialized\n",
  262. GetTickCount(), gpSensCache->dwCacheInitTime));
  263. return bRetVal;
  264. Cleanup:
  265. //
  266. // Cleanup
  267. //
  268. if (ghSensFileMap != NULL)
  269. {
  270. CloseHandle(ghSensFileMap);
  271. ghSensFileMap = NULL;
  272. gpSensCache = NULL;
  273. }
  274. return bRetVal;
  275. }
  276. void
  277. DeleteSensCache(
  278. void
  279. )
  280. /*++
  281. Routine Description:
  282. Cleanup the previously create SENS cache.
  283. Arguments:
  284. None.
  285. Return Value:
  286. None.
  287. --*/
  288. {
  289. if (gpSensCache != NULL)
  290. {
  291. BOOL bStatus = FALSE;
  292. ASSERT(ghSensFileMap != NULL);
  293. bStatus = UnmapViewOfFile((LPVOID) gpSensCache);
  294. SensPrintA(SENS_INFO, ("DeleteSensCache(): UnmapViewOfFile() returned"
  295. " 0x%x with a GLE of %d\n.", bStatus, GetLastError()));
  296. ASSERT(bStatus != FALSE);
  297. gpSensCache = 0;
  298. }
  299. if (ghSensFileMap != NULL)
  300. {
  301. CloseHandle(ghSensFileMap);
  302. ghSensFileMap = 0;
  303. }
  304. SensPrintA(SENS_INFO, ("DeleteSensCache(): Successfully cleaned up SENS"
  305. " Cache.\n"));
  306. return;
  307. }
  308. void
  309. UpdateSensCache(
  310. CACHE_TYPE Type
  311. )
  312. /*++
  313. Routine Description:
  314. Updates the requested part of the SENS cache.
  315. Arguments:
  316. Type - That part of the cache that needs to be updated.
  317. Return Value:
  318. None.
  319. --*/
  320. {
  321. //
  322. // Make sure Cache is initialized.
  323. //
  324. if (NULL == gpSensCache)
  325. {
  326. return;
  327. }
  328. switch (Type)
  329. {
  330. case LAN:
  331. gpSensCache->dwLANState = gdwLANState;
  332. gpSensCache->dwLastLANTime = gdwLastLANTime;
  333. UpdateSensNetworkCache();
  334. break;
  335. case WAN:
  336. gpSensCache->dwWANState = gdwWANState;
  337. gpSensCache->dwLastWANTime = gdwLastWANTime;
  338. UpdateSensNetworkCache();
  339. break;
  340. #if defined(AOL_PLATFORM)
  341. case AOL:
  342. gpSensCache->dwAOLState = gdwAOLState;
  343. // We don't update the Network cache when AOL
  344. // connectivity is updated.
  345. break;
  346. #endif // (AOL_PLATFORM)
  347. case LOCK:
  348. gpSensCache->dwLocked = gdwLocked;
  349. SensPrintA(SENS_INFO, ("CACHE: Updated Locked State to %d.\n", gpSensCache->dwLocked));
  350. break;
  351. case INVALID:
  352. default:
  353. SensPrintA(SENS_ERR, ("UpdateSensCache(): Received an invalid Type"
  354. " (0x%x)\n", Type));
  355. ASSERT(0);
  356. break;
  357. } // switch
  358. SensPrintA(SENS_INFO, ("UpdateSensCache(): Succeeded.\n"));
  359. }
  360. inline void
  361. UpdateSensNetworkCache(
  362. void
  363. )
  364. /*++
  365. Convenient Macro to update the whole Network state.
  366. --*/
  367. {
  368. DWORD dwNetState;
  369. dwNetState = 0x0;
  370. RequestSensLock();
  371. if (gdwLANState)
  372. {
  373. dwNetState |= NETWORK_ALIVE_LAN;
  374. }
  375. if (gdwWANState)
  376. {
  377. dwNetState |= NETWORK_ALIVE_WAN;
  378. }
  379. #if defined(AOL_PLATFORM)
  380. if (gdwAOLState)
  381. {
  382. dwNetState |= NETWORK_ALIVE_AOL;
  383. }
  384. #endif // AOL_PLATFORM
  385. gpSensCache->dwLastUpdateState = dwNetState;
  386. gpSensCache->dwLastUpdateTime = GetTickCount();
  387. ReleaseSensLock();
  388. }