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.

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