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.

327 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997-2002.
  5. //
  6. // File: Persist.cpp
  7. //
  8. // Contents: Implementation of persistence
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include <gpedit.h>
  13. #include "compdata.h"
  14. USE_HANDLE_MACROS("CERTMGR(persist.cpp)")
  15. #ifdef _DEBUG
  16. #ifndef ALPHA
  17. #define new DEBUG_NEW
  18. #endif
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. LPCWSTR 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. /////////////////////////////////////////////////////////////////////
  28. STDMETHODIMP CCertMgrComponentData::Load(IStream __RPC_FAR *pIStream)
  29. {
  30. _TRACE (1, L"Entering CCertMgrComponentData::Load ()\n");
  31. HRESULT hr = S_OK;
  32. #ifndef DONT_PERSIST
  33. ASSERT (pIStream);
  34. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  35. PWSTR wcszMachineName = NULL;
  36. PWSTR wcszServiceName = NULL;
  37. do {
  38. // Read the magic word from the stream
  39. DWORD dwMagicword;
  40. hr = pIStream->Read( OUT &dwMagicword, sizeof(dwMagicword), NULL );
  41. if ( FAILED(hr) )
  42. break;
  43. if (dwMagicword != _dwMagicword)
  44. {
  45. // We have a version mismatch
  46. _TRACE (0, L"INFO: CCertMgrComponentData::Load() - Wrong Magicword. You need to re-save your .msc file.\n");
  47. hr = E_FAIL;
  48. break;
  49. }
  50. // read m_activeViewPersist from stream
  51. hr = pIStream->Read (&m_activeViewPersist,
  52. sizeof(m_activeViewPersist), NULL);
  53. if ( FAILED(hr) )
  54. break;
  55. // read m_dwLocationPersist from stream
  56. hr = pIStream->Read (&m_dwLocationPersist,
  57. sizeof(m_dwLocationPersist), NULL);
  58. if ( FAILED(hr) )
  59. break;
  60. // read m_bShowPhysicalStoresPersist from stream
  61. hr = pIStream->Read (&m_bShowPhysicalStoresPersist,
  62. sizeof(m_bShowPhysicalStoresPersist), NULL);
  63. if ( FAILED(hr) )
  64. break;
  65. // read m_bShowArchivedCertsPersist from stream
  66. hr = pIStream->Read (&m_bShowArchivedCertsPersist,
  67. sizeof(m_bShowArchivedCertsPersist), NULL);
  68. if ( FAILED(hr) )
  69. break;
  70. // read flags from stream
  71. DWORD dwFlags;
  72. hr = pIStream->Read( OUT &dwFlags, sizeof(dwFlags), NULL );
  73. if ( FAILED(hr) )
  74. break;
  75. SetPersistentFlags(dwFlags);
  76. // read server name from stream
  77. // NTRAID#NTBUG9 736602-2002/11/14-ericb AV if MSC file stores computer name
  78. // The stored length is bytes.
  79. DWORD cbLen = 0;
  80. hr = pIStream->Read (&cbLen, 4, NULL);
  81. if ( FAILED (hr) )
  82. break;
  83. ASSERT (cbLen <= MAX_PATH * sizeof (WCHAR));
  84. wcszMachineName = (PWSTR) LocalAlloc (LPTR, cbLen);
  85. if (!wcszMachineName)
  86. {
  87. hr = E_OUTOFMEMORY;
  88. break;
  89. }
  90. hr = pIStream->Read ((PVOID) wcszMachineName, cbLen, NULL);
  91. if ( FAILED (hr) )
  92. break;
  93. // ensure null-termination
  94. // NTRAID#NTBUG9 736602-2002/11/14-ericb AV if MSC file stores computer name
  95. // Don't index a WCHAR array using a byte count.
  96. wcszMachineName[(cbLen/sizeof(WCHAR))-1] = 0;
  97. // Skip leading "\\", if present
  98. // security review 2/27/2002 BryanWal ok
  99. if ( !wcsncmp (wcszMachineName, L"\\\\", 2) )
  100. m_strMachineNamePersist = wcszMachineName + 2;
  101. else
  102. m_strMachineNamePersist = wcszMachineName;
  103. PCWSTR pszMachineNameT = PchGetMachineNameOverride ();
  104. if ( m_fAllowOverrideMachineName && pszMachineNameT )
  105. {
  106. // Allow machine name override
  107. }
  108. else
  109. {
  110. pszMachineNameT = wcszMachineName;
  111. }
  112. // Truncate leading "\\"
  113. // security review 2/27/2002 BryanWal ok
  114. if ( !wcsncmp (pszMachineNameT, L"\\\\", 2) )
  115. pszMachineNameT += 2;
  116. QueryRootCookie().SetMachineName (pszMachineNameT);
  117. // read service name from stream
  118. // NTRAID#NTBUG9 736602-2002/11/14-ericb AV if MSC file stores computer name
  119. // The stored length is in bytes.
  120. cbLen = 0;
  121. hr = pIStream->Read (&cbLen, 4, NULL);
  122. if ( FAILED (hr) )
  123. break;
  124. ASSERT (cbLen <= MAX_PATH * sizeof (WCHAR));
  125. wcszServiceName = (PWSTR) LocalAlloc (LPTR, cbLen);
  126. if (!wcszServiceName)
  127. {
  128. hr = E_OUTOFMEMORY;
  129. break;
  130. }
  131. hr = pIStream->Read ((PVOID) wcszServiceName, cbLen, NULL);
  132. if ( FAILED (hr) )
  133. break;
  134. // ensure null-termination
  135. // NTRAID#NTBUG9 736602-2002/11/14-ericb AV if MSC file stores computer name
  136. // Don't index a WCHAR array using a byte count.
  137. wcszServiceName[(cbLen/sizeof(WCHAR))-1] = 0;
  138. m_szManagedServicePersist = wcszServiceName;
  139. if ( !m_szManagedServicePersist.IsEmpty () )
  140. {
  141. // get the display name of this service
  142. DWORD dwErr = 0;
  143. SC_HANDLE hScManager = ::OpenSCManager (pszMachineNameT,
  144. SERVICES_ACTIVE_DATABASE,
  145. SC_MANAGER_ENUMERATE_SERVICE);
  146. if ( hScManager )
  147. {
  148. DWORD chBuffer = 0;
  149. BOOL bResult = ::GetServiceDisplayName (
  150. hScManager, // handle to SCM database
  151. m_szManagedServicePersist, // service name
  152. NULL, // display name
  153. &chBuffer); // size of display name buffer
  154. if ( !bResult )
  155. {
  156. dwErr = GetLastError ();
  157. if ( ERROR_INSUFFICIENT_BUFFER == dwErr )
  158. {
  159. PWSTR pwszDisplayName = new WCHAR[++chBuffer];
  160. if ( pwszDisplayName )
  161. {
  162. bResult = ::GetServiceDisplayName (
  163. hScManager, // handle to SCM database
  164. m_szManagedServicePersist, // service name
  165. pwszDisplayName, // display name
  166. &chBuffer); // size of display name buffer
  167. if ( bResult )
  168. m_szManagedServiceDisplayName = pwszDisplayName;
  169. else
  170. {
  171. dwErr = GetLastError ();
  172. _TRACE (0, L"GetServiceDisplayName (%s) failed: 0x%x\n",
  173. (PCWSTR) m_szManagedServicePersist, dwErr);
  174. }
  175. delete [] pwszDisplayName;
  176. }
  177. }
  178. else
  179. {
  180. dwErr = GetLastError ();
  181. _TRACE (0, L"GetServiceDisplayName (%s) failed: 0x%x\n",
  182. (PCWSTR) m_szManagedServicePersist, dwErr);
  183. }
  184. }
  185. bResult = ::CloseServiceHandle (hScManager);
  186. ASSERT (bResult);
  187. if ( !bResult )
  188. {
  189. dwErr = GetLastError ();
  190. _TRACE (0, L"CloseServiceHandle () failed: 0x%x\n", dwErr);
  191. }
  192. }
  193. else
  194. {
  195. dwErr = GetLastError ();
  196. _TRACE (0, L"OpenSCManager (%s) failed: 0x%x\n", pszMachineNameT, dwErr);
  197. }
  198. }
  199. }
  200. while (0);
  201. if (wcszMachineName)
  202. LocalFree(wcszMachineName);
  203. if (wcszServiceName)
  204. LocalFree(wcszServiceName);
  205. #endif
  206. _TRACE (-1, L"Leaving CCertMgrComponentData::Load (): 0x%x\n", hr);
  207. return hr;
  208. }
  209. /////////////////////////////////////////////////////////////////////
  210. STDMETHODIMP CCertMgrComponentData::Save(IStream __RPC_FAR *pIStream, BOOL /*fSameAsLoad*/)
  211. {
  212. _TRACE (-1, L"Entering CCertMgrComponentData::Save ()\n");
  213. HRESULT hr = S_OK;
  214. #ifndef DONT_PERSIST
  215. ASSERT (pIStream);
  216. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  217. do {
  218. // Store the magic word to the stream
  219. DWORD dwMagicword = _dwMagicword;
  220. hr = pIStream->Write( IN &dwMagicword, sizeof(dwMagicword), NULL );
  221. if ( FAILED(hr) )
  222. break;
  223. // Persist m_activeViewPersist
  224. hr = pIStream->Write (&m_activeViewPersist,
  225. sizeof (m_activeViewPersist), NULL);
  226. if ( FAILED(hr) )
  227. break;
  228. // Persist m_dwLocationPersist
  229. hr = pIStream->Write (&m_dwLocationPersist,
  230. sizeof (m_dwLocationPersist), NULL);
  231. if ( FAILED(hr) )
  232. break;
  233. // Persist m_bShowPhysicalStoresPersist
  234. hr = pIStream->Write (&m_bShowPhysicalStoresPersist,
  235. sizeof (m_bShowPhysicalStoresPersist), NULL);
  236. if ( FAILED(hr) )
  237. break;
  238. // Persist m_bShowArchivedCertsPersist
  239. hr = pIStream->Write (&m_bShowArchivedCertsPersist,
  240. sizeof (m_bShowArchivedCertsPersist), NULL);
  241. if ( FAILED(hr) )
  242. break;
  243. // persist flags
  244. DWORD dwFlags = GetPersistentFlags();
  245. hr = pIStream->Write( IN &dwFlags, sizeof(dwFlags), NULL );
  246. if ( FAILED(hr) )
  247. break;
  248. // Persist machine name length and machine name
  249. LPCWSTR wcszMachineName = m_strMachineNamePersist;
  250. // security review 2/27/2002 BryanWal ok
  251. DWORD cbLen = (DWORD) (wcslen (wcszMachineName) + 1) * sizeof (WCHAR);
  252. ASSERT( 4 == sizeof(DWORD) );
  253. hr = pIStream->Write (&cbLen, 4, NULL);
  254. if ( FAILED(hr) )
  255. break;
  256. hr = pIStream->Write (wcszMachineName, cbLen, NULL);
  257. if ( FAILED (hr) )
  258. break;
  259. // Persist service name length and service name
  260. LPCWSTR wcszServiceName = m_szManagedServicePersist;
  261. // security review 2/27/2002 BryanWal ok
  262. cbLen = (DWORD) (wcslen (wcszServiceName) + 1) * sizeof (WCHAR);
  263. ASSERT (4 == sizeof (DWORD));
  264. hr = pIStream->Write (&cbLen, 4, NULL);
  265. if ( FAILED (hr) )
  266. break;
  267. hr = pIStream->Write (wcszServiceName, cbLen, NULL);
  268. if ( FAILED (hr) )
  269. break;
  270. }
  271. while (0);
  272. #endif
  273. _TRACE (-1, L"Leaving CCertMgrComponentData::Save (): 0x%x\n", hr);
  274. return hr;
  275. }