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.

302 lines
6.8 KiB

  1. // Persist.cpp : Implementation of persistence
  2. //
  3. // HISTORY
  4. // 01-Jan-1996 ??? Creation
  5. // 03-Jun-1997 t-danm Added a version number to storage and
  6. // Command Line override.
  7. // 2002/02/27-JonN 558642 Security Push: check bytes read
  8. //
  9. /////////////////////////////////////////////////////////////////////
  10. #include "stdafx.h"
  11. #include "compdata.h"
  12. #include "safetemp.h"
  13. #include "stdutils.h" // IsLocalComputername
  14. #include "macros.h"
  15. USE_HANDLE_MACROS("MYCOMPUT(persist.cpp)")
  16. #include <comstrm.h>
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. LPCTSTR PchGetMachineNameOverride(); // Defined in chooser.cpp
  23. /////////////////////////////////////////////////
  24. // The _dwMagicword is the internal version number.
  25. // Increment this number if you make a file format change.
  26. #define _dwMagicword 10001
  27. // IComponentData persistence (remember persistent flags and target computername
  28. /////////////////////////////////////////////////////////////////////
  29. STDMETHODIMP CMyComputerComponentData::Load(IStream __RPC_FAR *pIStream)
  30. {
  31. MFC_TRY;
  32. HRESULT hr;
  33. #ifndef DONT_PERSIST_COMPONENT_DATA
  34. // JonN 2/20/02 Security Push
  35. if (NULL == pIStream)
  36. {
  37. ASSERT(FALSE);
  38. return E_POINTER;
  39. }
  40. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  41. // Read the magic word from the stream
  42. DWORD dwMagicword = 0;
  43. // 2002/02/27-JonN 558642 Security Push: check bytes read
  44. DWORD cbRead = 0;
  45. hr = pIStream->Read( OUT &dwMagicword, sizeof(dwMagicword), &cbRead );
  46. if ( FAILED(hr) )
  47. {
  48. ASSERT( FALSE );
  49. return hr;
  50. }
  51. else if (cbRead != sizeof(dwMagicword))
  52. {
  53. ASSERT( FALSE );
  54. return E_FAIL;
  55. }
  56. if (dwMagicword != _dwMagicword)
  57. {
  58. // We have a version mismatch
  59. TRACE0("INFO: CMyComputerComponentData::Load() - Wrong Magicword. You need to re-save your .msc file.\n");
  60. return E_FAIL;
  61. }
  62. // read flags from stream
  63. DWORD dwFlags = 0;
  64. cbRead = 0;
  65. hr = pIStream->Read( OUT &dwFlags, sizeof(dwFlags), &cbRead );
  66. if ( FAILED(hr) )
  67. {
  68. ASSERT( FALSE );
  69. return hr;
  70. }
  71. else if (cbRead != sizeof(dwFlags))
  72. {
  73. ASSERT( FALSE );
  74. return E_FAIL;
  75. }
  76. SetPersistentFlags(dwFlags);
  77. // read server name from stream
  78. DWORD dwLen = 0;
  79. cbRead = 0;
  80. hr = pIStream->Read( &dwLen, sizeof(dwLen), &cbRead );
  81. if ( FAILED(hr) )
  82. {
  83. ASSERT( FALSE );
  84. return hr;
  85. }
  86. else if (cbRead != sizeof(dwLen))
  87. {
  88. ASSERT( FALSE );
  89. return E_FAIL;
  90. }
  91. // JonN 2/20/02 Security Push
  92. if (sizeof(WCHAR) > dwLen || MAX_PATH*sizeof(WCHAR) < dwLen)
  93. {
  94. ASSERT(FALSE);
  95. return E_FAIL;
  96. }
  97. LPWSTR lpwszMachineName = (LPWSTR)alloca( dwLen );
  98. // allocated from stack, we don't need to free
  99. if (NULL == lpwszMachineName)
  100. {
  101. AfxThrowMemoryException();
  102. return E_OUTOFMEMORY;
  103. }
  104. ZeroMemory( lpwszMachineName, dwLen );
  105. cbRead = 0;
  106. hr = pIStream->Read( (PVOID)lpwszMachineName, dwLen, &cbRead );
  107. if ( FAILED(hr) )
  108. {
  109. ASSERT( FALSE );
  110. return hr;
  111. }
  112. else if (cbRead != dwLen)
  113. {
  114. ASSERT( FALSE );
  115. return E_FAIL;
  116. }
  117. // JonN 2/20/02 Security Push: force NULL-termination
  118. lpwszMachineName[(dwLen/sizeof(WCHAR))-1] = L'\0';
  119. m_strMachineNamePersist = lpwszMachineName;
  120. LPCTSTR pszMachineNameT = PchGetMachineNameOverride();
  121. if (m_fAllowOverrideMachineName && pszMachineNameT != NULL)
  122. {
  123. // Allow machine name override
  124. }
  125. else
  126. {
  127. pszMachineNameT = lpwszMachineName;
  128. }
  129. // JonN 1/27/99: If the persisted name is the local computername,
  130. // leave the persisted name alone but make the effective name (Local).
  131. if ( IsLocalComputername(pszMachineNameT) )
  132. pszMachineNameT = L"";
  133. QueryRootCookie().SetMachineName(pszMachineNameT);
  134. #endif
  135. return S_OK;
  136. MFC_CATCH;
  137. }
  138. /////////////////////////////////////////////////////////////////////
  139. STDMETHODIMP CMyComputerComponentData::Save(IStream __RPC_FAR *pIStream, BOOL /*fSameAsLoad*/)
  140. {
  141. MFC_TRY;
  142. HRESULT hr;
  143. #ifndef DONT_PERSIST_COMPONENT_DATA
  144. // JonN 2/20/02 Security Push
  145. if (NULL == pIStream)
  146. {
  147. ASSERT(FALSE);
  148. return E_POINTER;
  149. }
  150. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  151. // Store the magic word to the stream
  152. DWORD dwMagicword = _dwMagicword;
  153. hr = pIStream->Write( IN &dwMagicword, sizeof(dwMagicword), NULL );
  154. if ( FAILED(hr) )
  155. {
  156. ASSERT( FALSE );
  157. return hr;
  158. }
  159. DWORD dwFlags = GetPersistentFlags();
  160. hr = pIStream->Write( IN &dwFlags, sizeof(dwFlags), NULL );
  161. if ( FAILED(hr) )
  162. {
  163. ASSERT( FALSE );
  164. return hr;
  165. }
  166. LPCWSTR lpwcszMachineName = m_strMachineNamePersist;
  167. DWORD dwLen = (DWORD) (::wcslen(lpwcszMachineName)+1)*sizeof(WCHAR);
  168. ASSERT( 4 == sizeof(DWORD) );
  169. hr = pIStream->Write( &dwLen, sizeof (DWORD), NULL );
  170. if ( FAILED(hr) )
  171. {
  172. ASSERT( FALSE );
  173. return hr;
  174. }
  175. hr = pIStream->Write( lpwcszMachineName, dwLen, NULL );
  176. if ( FAILED(hr) )
  177. {
  178. ASSERT( FALSE );
  179. return hr;
  180. }
  181. #endif
  182. return S_OK;
  183. MFC_CATCH;
  184. }
  185. // IComponent persistence
  186. /////////////////////////////////////////////////////////////////////
  187. STDMETHODIMP CMyComputerComponent::Load(IStream __RPC_FAR *pIStream)
  188. {
  189. MFC_TRY;
  190. HRESULT hr;
  191. #ifndef DONT_PERSIST_COMPONENT
  192. ASSERT( NULL != pIStream );
  193. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  194. // Read the magic word from the stream
  195. DWORD dwMagicword = 0;
  196. // 2002/02/27-JonN Security Push: check bytes read
  197. DWORD cbRead = 0;
  198. hr = pIStream->Read( OUT &dwMagicword, sizeof(dwMagicword), &cbRead );
  199. if ( FAILED(hr) )
  200. {
  201. ASSERT( FALSE );
  202. return hr;
  203. }
  204. else if (cbRead != sizeof(dwMagicword))
  205. {
  206. ASSERT( FALSE );
  207. return E_FAIL;
  208. }
  209. if (dwMagicword != _dwMagicword)
  210. {
  211. // We have a version mismatch
  212. TRACE0("INFO: CMyComputerComponentData::Load() - Wrong Magicword. You need to re-save your .msc file.\n");
  213. return E_FAIL;
  214. }
  215. // read flags from stream
  216. DWORD dwFlags = 0;
  217. cbRead = 0;
  218. hr = pIStream->Read( OUT &dwFlags, sizeof(dwFlags), &cbRead );
  219. if ( FAILED(hr) )
  220. {
  221. ASSERT( FALSE );
  222. return hr;
  223. }
  224. else if (cbRead != sizeof(dwFlags))
  225. {
  226. ASSERT( FALSE );
  227. return E_FAIL;
  228. }
  229. SetPersistentFlags(dwFlags);
  230. #endif
  231. return S_OK;
  232. MFC_CATCH;
  233. }
  234. /////////////////////////////////////////////////////////////////////
  235. STDMETHODIMP CMyComputerComponent::Save(IStream __RPC_FAR *pIStream, BOOL /*fSameAsLoad*/)
  236. {
  237. MFC_TRY;
  238. HRESULT hr;
  239. #ifndef DONT_PERSIST_COMPONENT
  240. ASSERT( NULL != pIStream );
  241. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  242. // Store the magic word to the stream
  243. DWORD dwMagicword = _dwMagicword;
  244. hr = pIStream->Write( IN &dwMagicword, sizeof(dwMagicword), NULL );
  245. if ( FAILED(hr) )
  246. {
  247. ASSERT( FALSE );
  248. return hr;
  249. }
  250. DWORD dwFlags = GetPersistentFlags();
  251. hr = pIStream->Write( IN &dwFlags, sizeof(dwFlags), NULL );
  252. if ( FAILED(hr) )
  253. {
  254. ASSERT( FALSE );
  255. return hr;
  256. }
  257. #endif
  258. return S_OK;
  259. MFC_CATCH;
  260. }