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.

287 lines
6.3 KiB

  1. #define _PassportExport_
  2. #include "PassportExport.h"
  3. #include "PassportSharedMemory.h"
  4. #include <malloc.h>
  5. #include <tchar.h>
  6. #define PMUTEX_STRING _T("PASSPORTMUTEX")
  7. //-------------------------------------------------------------
  8. //
  9. // PassportSharedMemory
  10. //
  11. //-------------------------------------------------------------
  12. PassportSharedMemory::PassportSharedMemory()
  13. {
  14. m_hShMem = 0;
  15. m_pbShMem = 0;
  16. m_bInited = 0;
  17. m_hMutex = 0;
  18. m_bUseMutex = FALSE;
  19. }
  20. //-------------------------------------------------------------
  21. //
  22. // ~PassportSharedMemory
  23. //
  24. //-------------------------------------------------------------
  25. PassportSharedMemory::~PassportSharedMemory()
  26. {
  27. CloseSharedMemory();
  28. }
  29. //-------------------------------------------------------------
  30. //
  31. // CreateSharedMemory
  32. //
  33. //-------------------------------------------------------------
  34. BOOL PassportSharedMemory::CreateSharedMemory (
  35. const DWORD &dwMaximumSizeHigh,
  36. const DWORD &dwMaximunSizeLow,
  37. LPCTSTR lpcName,
  38. BOOL useMutex)
  39. {
  40. BOOL fReturn = FALSE;
  41. SID_IDENTIFIER_AUTHORITY siaWorld = SECURITY_NT_AUTHORITY;
  42. // local param need to be freed before exit
  43. SECURITY_DESCRIPTOR sd;
  44. PACL pACL = NULL;
  45. DWORD cbACL;
  46. SID *lpSID = NULL;
  47. LPSECURITY_ATTRIBUTES lpSA = NULL;
  48. // local param do not need to free
  49. TCHAR *pStr = NULL;
  50. if (!lpcName)
  51. {
  52. goto exit;
  53. }
  54. if (m_pbShMem != NULL && m_hShMem != NULL && m_bInited)
  55. {
  56. fReturn = TRUE;
  57. goto exit;
  58. }
  59. m_bUseMutex = useMutex;
  60. if (!AllocateAndInitializeSid(&siaWorld, // Security, world authoity for ownership
  61. 1, // 1 relative sub-authority
  62. SECURITY_AUTHENTICATED_USER_RID, // sub-authority, authenticated users
  63. 0, 0, 0, 0, 0, 0, 0, // unused sub-authority types
  64. (void**) &lpSID)) // ** to security ID struct
  65. {
  66. goto exit;
  67. }
  68. cbACL = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) + GetLengthSid(lpSID);
  69. pACL = (PACL) LocalAlloc(LMEM_FIXED, cbACL);
  70. if (pACL == NULL)
  71. {
  72. goto exit;
  73. }
  74. if (!InitializeAcl(pACL, cbACL, ACL_REVISION))
  75. {
  76. goto exit;
  77. }
  78. //
  79. // Perfcounters may run under any account and may need to read from or
  80. // write to the shared memory section. Give World read/write access.
  81. //
  82. if (!AddAccessAllowedAce(pACL, ACL_REVISION, GENERIC_READ | GENERIC_WRITE, lpSID))
  83. {
  84. goto exit;
  85. }
  86. InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
  87. SetSecurityDescriptorDacl(&sd, TRUE, pACL, FALSE); // allow access to DACL
  88. SetSecurityDescriptorGroup(&sd, lpSID, FALSE); // set group to World
  89. lpSA = new SECURITY_ATTRIBUTES;
  90. if( !lpSA )
  91. {
  92. goto exit;
  93. }
  94. lpSA->nLength = sizeof(SECURITY_ATTRIBUTES);
  95. lpSA->lpSecurityDescriptor = &sd;
  96. lpSA->bInheritHandle = TRUE;
  97. m_hShMem = CreateFileMapping((INVALID_HANDLE_VALUE),
  98. lpSA,
  99. PAGE_READWRITE,
  100. dwMaximumSizeHigh,
  101. dwMaximunSizeLow,
  102. lpcName);
  103. if( !m_hShMem )
  104. {
  105. goto exit;
  106. }
  107. m_pbShMem = (BYTE*) MapViewOfFile(m_hShMem,
  108. FILE_MAP_ALL_ACCESS,
  109. 0, 0, 0 );
  110. if( !m_pbShMem )
  111. {
  112. goto exit;
  113. }
  114. if (useMutex)
  115. {
  116. pStr = new TCHAR [_tcslen(lpcName) + _tcslen(PMUTEX_STRING) + 1];
  117. if (NULL == pStr)
  118. {
  119. goto exit;
  120. }
  121. _tcscpy (pStr, lpcName);
  122. _tcscat (pStr, PMUTEX_STRING);
  123. m_hMutex = CreateMutex(lpSA,FALSE,pStr);
  124. delete [] pStr;
  125. if( !m_hMutex )
  126. {
  127. goto exit;
  128. }
  129. }
  130. // we are here because we are fully initialized :-)
  131. m_bInited = TRUE;
  132. fReturn = TRUE;
  133. exit:
  134. // cleanup locally allocated heap
  135. if( lpSA )
  136. {
  137. delete lpSA;
  138. }
  139. if( lpSID )
  140. {
  141. FreeSid(lpSID);
  142. }
  143. if (pACL)
  144. {
  145. LocalFree(pACL);
  146. }
  147. // if we are not fully initialized, cleanup them
  148. if( !m_bInited )
  149. {
  150. CloseSharedMemory();
  151. }
  152. return fReturn;
  153. }
  154. //-------------------------------------------------------------
  155. //
  156. // OpenSharedMemory
  157. //
  158. //-------------------------------------------------------------
  159. BOOL PassportSharedMemory::OpenSharedMemory( LPCTSTR lpcName, BOOL useMutex )
  160. {
  161. if (!lpcName)
  162. return FALSE;
  163. if (m_pbShMem != NULL && m_hShMem != NULL && m_bInited)
  164. return TRUE;
  165. m_bUseMutex = useMutex;
  166. m_hShMem = OpenFileMapping( FILE_MAP_READ, FALSE, lpcName );
  167. if( ! m_hShMem )
  168. {
  169. return FALSE;
  170. }
  171. m_pbShMem = (BYTE*) MapViewOfFile( m_hShMem, FILE_MAP_READ, 0, 0, 0 );
  172. if( ! m_pbShMem )
  173. {
  174. CloseHandle( m_hShMem );
  175. m_hShMem = 0;
  176. return FALSE;
  177. }
  178. if (useMutex)
  179. {
  180. TCHAR *pStr = new TCHAR [_tcslen(lpcName) + _tcslen(PMUTEX_STRING) + 1];
  181. if (pStr == NULL)
  182. {
  183. CloseSharedMemory();
  184. return FALSE;
  185. }
  186. _tcscpy (pStr, lpcName);
  187. _tcscat (pStr,PMUTEX_STRING);
  188. m_hMutex = OpenMutex(SYNCHRONIZE ,FALSE, pStr);
  189. delete [] pStr;
  190. if( !m_hMutex )
  191. {
  192. CloseSharedMemory();
  193. return FALSE;
  194. }
  195. }
  196. m_bInited = TRUE;
  197. return TRUE;
  198. }
  199. //-------------------------------------------------------------
  200. //
  201. // CloseSharedMemory
  202. //
  203. //-------------------------------------------------------------
  204. void PassportSharedMemory::CloseSharedMemory( void )
  205. {
  206. if( m_pbShMem )
  207. {
  208. UnmapViewOfFile( (void*) m_pbShMem );
  209. m_pbShMem = 0;
  210. }
  211. if( m_hShMem )
  212. {
  213. CloseHandle( m_hShMem );
  214. m_hShMem = 0;
  215. }
  216. if( m_hMutex )
  217. {
  218. ReleaseMutex(m_hMutex);
  219. m_hMutex = 0;;
  220. }
  221. //
  222. // Reading from the above code, we should do the following.
  223. // On the other hand, we may be able to eliminate this member. No big role for this member.
  224. //
  225. if (m_bInited){
  226. m_bInited = FALSE;
  227. }
  228. }