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.

180 lines
5.3 KiB

  1. //+---------------------------------------------------------------------------
  2. /////////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Microsoft Windows
  5. // Copyright (C) Microsoft Corporation, 2000-2001.
  6. //
  7. // File: SaferLevel.cpp
  8. //
  9. // Contents: Implementation of CSaferLevel
  10. //
  11. //----------------------------------------------------------------------------
  12. #include "stdafx.h"
  13. #include <gpedit.h>
  14. #include <winsafer.h>
  15. #include <wintrust.h>
  16. #include "SaferLevel.h"
  17. #include "SaferUtil.h"
  18. #include "PolicyKey.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. extern GUID g_guidExtension;
  25. extern GUID g_guidRegExt;
  26. extern GUID g_guidSnapin;
  27. extern const DWORD AUTHZ_UNKNOWN_LEVEL;
  28. //////////////////////////////////////////////////////////////////////
  29. // Construction/Destruction
  30. //////////////////////////////////////////////////////////////////////
  31. CSaferLevel::CSaferLevel(
  32. DWORD dwSaferLevel,
  33. bool bIsMachine,
  34. PCWSTR pszMachineName,
  35. PCWSTR pszObjectName,
  36. IGPEInformation* pGPEInformation,
  37. CRSOPObjectArray& rRSOPArray)
  38. : CCertMgrCookie (bIsMachine ? CERTMGR_SAFER_COMPUTER_LEVEL : CERTMGR_SAFER_USER_LEVEL,
  39. pszMachineName, pszObjectName),
  40. m_dwSaferLevel (dwSaferLevel),
  41. m_bIsComputer (bIsMachine),
  42. m_pGPEInformation (pGPEInformation),
  43. m_rRSOPArray (rRSOPArray)
  44. {
  45. if ( m_pGPEInformation )
  46. m_pGPEInformation->AddRef ();
  47. // OK to pass in NULL for m_pGPEInformation
  48. CPolicyKey policyKey (m_pGPEInformation,
  49. SAFER_HKLM_REGBASE,
  50. m_bIsComputer);
  51. m_szDescription = SaferGetLevelDescription (dwSaferLevel, policyKey.GetKey (),
  52. m_bIsComputer);
  53. }
  54. CSaferLevel::~CSaferLevel()
  55. {
  56. if ( m_pGPEInformation )
  57. m_pGPEInformation->Release ();
  58. }
  59. CString CSaferLevel::GetDescription() const
  60. {
  61. return m_szDescription;
  62. }
  63. HRESULT CSaferLevel::SetAsDefault()
  64. {
  65. _TRACE (1, L"Entering CSaferLevel::SetAsDefault ()\n");
  66. HRESULT hr = S_OK;
  67. if ( m_pGPEInformation )
  68. {
  69. CPolicyKey policyKey (m_pGPEInformation,
  70. SAFER_HKLM_REGBASE,
  71. m_bIsComputer);
  72. hr = SetRegistryScope (policyKey.GetKey (), m_bIsComputer);
  73. if ( SUCCEEDED (hr) )
  74. {
  75. DWORD dwData = GetLevel ();
  76. DWORD cbData = sizeof (dwData);
  77. BOOL bRVal = SaferSetPolicyInformation (SAFER_SCOPEID_REGISTRY,
  78. SaferPolicyDefaultLevel, cbData, &dwData, policyKey.GetKey ());
  79. if ( bRVal )
  80. {
  81. // TRUE means we're changing the machine policy only
  82. m_pGPEInformation->PolicyChanged (m_bIsComputer ? TRUE : FALSE,
  83. TRUE, &g_guidExtension, &g_guidSnapin);
  84. m_pGPEInformation->PolicyChanged (m_bIsComputer ? TRUE : FALSE,
  85. TRUE, &g_guidRegExt, &g_guidSnapin);
  86. }
  87. else
  88. {
  89. hr = HRESULT_FROM_WIN32 (GetLastError ());
  90. }
  91. }
  92. }
  93. else
  94. hr = E_UNEXPECTED;
  95. _TRACE (-1, L"Leaving CSaferLevel::SetAsDefault (): 0x%x\n", hr);
  96. return hr;
  97. }
  98. DWORD CSaferLevel::ReturnDefaultLevel (
  99. IGPEInformation* pGPEInformation,
  100. bool bIsComputer,
  101. CRSOPObjectArray& rRSOPArray)
  102. {
  103. _TRACE (1, L"Entering CSaferLevel::IsDefault ()\n");
  104. DWORD dwDefaultLevelID = AUTHZ_UNKNOWN_LEVEL;
  105. if ( pGPEInformation )
  106. {
  107. CPolicyKey policyKey (pGPEInformation,
  108. SAFER_HKLM_REGBASE,
  109. bIsComputer);
  110. HRESULT hr = SetRegistryScope (policyKey.GetKey (), bIsComputer);
  111. if ( SUCCEEDED (hr) )
  112. {
  113. DWORD dwData = 0;
  114. DWORD cbData = sizeof (dwData);
  115. DWORD dwRetSize = 0;
  116. BOOL bRVal = SaferGetPolicyInformation (SAFER_SCOPEID_REGISTRY,
  117. SaferPolicyDefaultLevel, cbData, &dwData, &dwRetSize,
  118. policyKey.GetKey ());
  119. if ( bRVal )
  120. {
  121. dwDefaultLevelID = dwData;
  122. }
  123. }
  124. }
  125. else
  126. {
  127. int nIndex = 0;
  128. INT_PTR nUpperBound = rRSOPArray.GetUpperBound ();
  129. while ( nUpperBound >= nIndex )
  130. {
  131. CRSOPObject* pCurrObject = rRSOPArray.GetAt (nIndex);
  132. if ( pCurrObject )
  133. {
  134. if ( SAFER_HKLM_REGBASE == pCurrObject->GetRegistryKey () &&
  135. SAFER_DEFAULTOBJ_REGVALUE == pCurrObject->GetValueName () )
  136. {
  137. DWORD dwLevelID = pCurrObject->GetDWORDValue ();
  138. dwDefaultLevelID = dwLevelID;
  139. break;
  140. }
  141. }
  142. nIndex++;
  143. }
  144. }
  145. _TRACE (1, L"Entering CSaferLevel::ReturnDefaultLevel (): %d\n", dwDefaultLevelID);
  146. return dwDefaultLevelID;
  147. }
  148. bool CSaferLevel::IsDefault()
  149. {
  150. _TRACE (1, L"Entering CSaferLevel::IsDefault ()\n");
  151. bool bResult = false;
  152. DWORD dwDefaultLevelID = CSaferLevel::ReturnDefaultLevel (
  153. m_pGPEInformation, m_bIsComputer, m_rRSOPArray);
  154. if ( GetLevel () == dwDefaultLevelID )
  155. bResult = true;
  156. _TRACE (1, L"Entering CSaferLevel::IsDefault (): %s\n", bResult ? L"true" : L"false");
  157. return bResult;
  158. }