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.

126 lines
3.3 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 "compdata.h"
  13. USE_HANDLE_MACROS("CERTTMPL(persist.cpp)")
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. /////////////////////////////////////////////////
  20. // The _dwMagicword is the internal version number.
  21. // Increment this number if you make a file format change.
  22. #define _dwMagicword 10002
  23. /////////////////////////////////////////////////////////////////////
  24. STDMETHODIMP CCertTmplComponentData::Load(IStream __RPC_FAR *pIStream)
  25. {
  26. HRESULT hr = S_OK;;
  27. #ifndef DONT_PERSIST
  28. ASSERT (pIStream);
  29. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  30. // Read the magic word from the stream
  31. DWORD dwMagicword;
  32. hr = pIStream->Read( OUT &dwMagicword, sizeof(dwMagicword), NULL );
  33. if ( SUCCEEDED (hr) )
  34. {
  35. if (dwMagicword != _dwMagicword)
  36. {
  37. // We have a version mismatch
  38. _TRACE(0, L"INFO: CCertTmplComponentData::Load() - Wrong Magicword. You need to re-save your .msc file.\n");
  39. return E_FAIL;
  40. }
  41. // read domain name from stream
  42. DWORD dwLen = 0;
  43. hr = pIStream->Read (&dwLen, 4, NULL);
  44. if ( SUCCEEDED (hr) )
  45. {
  46. ASSERT (dwLen <= MAX_PATH * sizeof (WCHAR));
  47. if ( dwLen <= MAX_PATH * sizeof (WCHAR) )
  48. {
  49. try
  50. {
  51. // security push 2/22/2002 BryanWal ok
  52. PWSTR wcszDomainName = (PWSTR) alloca (dwLen);
  53. hr = pIStream->Read ((PVOID) wcszDomainName, dwLen, NULL);
  54. if ( SUCCEEDED (hr) )
  55. {
  56. // NOTICE: ensure null-termination
  57. wcszDomainName[dwLen-1] = 0;
  58. m_szManagedDomain = wcszDomainName;
  59. }
  60. }
  61. catch (CException* e)
  62. {
  63. e->Delete ();
  64. }
  65. }
  66. else
  67. hr = E_FAIL;
  68. }
  69. }
  70. #endif
  71. return hr;
  72. }
  73. /////////////////////////////////////////////////////////////////////
  74. STDMETHODIMP CCertTmplComponentData::Save(IStream __RPC_FAR *pIStream, BOOL /*fSameAsLoad*/)
  75. {
  76. HRESULT hr = S_OK;
  77. #ifndef DONT_PERSIST
  78. ASSERT (pIStream);
  79. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  80. // Store the magic word to the stream
  81. DWORD dwMagicword = _dwMagicword;
  82. hr = pIStream->Write( IN &dwMagicword, sizeof(dwMagicword), NULL );
  83. if ( FAILED(hr) )
  84. {
  85. ASSERT( FALSE );
  86. return hr;
  87. }
  88. // Persist m_szManagedDomain length and m_szManagedDomain
  89. // security
  90. // security review 2/21/2002 BryanWal ok
  91. size_t dwLen = (::wcslen (m_szManagedDomain) + 1) * sizeof (WCHAR);
  92. ASSERT( 4 == sizeof(DWORD) );
  93. hr = pIStream->Write (&dwLen, 4, NULL);
  94. if ( FAILED(hr) )
  95. {
  96. ASSERT( FALSE );
  97. return hr;
  98. }
  99. hr = pIStream->Write ((PCWSTR) m_szManagedDomain, (ULONG) dwLen, NULL);
  100. if ( FAILED (hr) )
  101. {
  102. ASSERT (FALSE);
  103. return hr;
  104. }
  105. #endif
  106. return S_OK;
  107. }