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.

360 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000-2001.
  5. //
  6. // File: SaferEnforcementPropertyPage.h
  7. //
  8. // Contents: Declaration of CSaferEnforcementPropertyPage
  9. //
  10. //----------------------------------------------------------------------------
  11. // SaferEnforcementPropertyPage.cpp : implementation file
  12. //
  13. #include "stdafx.h"
  14. #include "certmgr.h"
  15. #include <gpedit.h>
  16. #include "compdata.h"
  17. #include "SaferEnforcementPropertyPage.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. extern GUID g_guidExtension;
  24. extern GUID g_guidRegExt;
  25. extern GUID g_guidSnapin;
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CSaferEnforcementPropertyPage property page
  28. // The "TransparentEnforcement" flag has the following values:
  29. // 0 = disable all transparent hooks (in CreateProcess and LoadLibrary)
  30. // 1 = enable transparent hooks for CreateProcess
  31. // 2 = enable transparent hooks for CreateProcess and LoadLibrary
  32. #define SAFER_TRANSPARENT_ENFORCEMENT_DISABLE_ALL 0
  33. #define SAFER_TRANSPARENT_ENFORCEMENT_ENABLE_CREATE_PROCESS 1
  34. #define SAFER_TRANSPARENT_ENFORCEMENT_ENABLE_ALL 2
  35. CSaferEnforcementPropertyPage::CSaferEnforcementPropertyPage(
  36. IGPEInformation* pGPEInformation,
  37. CCertMgrComponentData* pCompData,
  38. bool bReadOnly,
  39. CRSOPObjectArray& rsopObjectArray,
  40. bool bIsComputer)
  41. : CHelpPropertyPage(CSaferEnforcementPropertyPage::IDD),
  42. m_pGPEInformation (pGPEInformation),
  43. m_hGroupPolicyKey (0),
  44. m_fIsComputerType (bIsComputer),
  45. m_bReadOnly (bReadOnly),
  46. m_rsopObjectArray (rsopObjectArray),
  47. m_dwEnforcement (0),
  48. m_bDirty (false),
  49. m_dwScopeFlags (0)
  50. {
  51. //{{AFX_DATA_INIT(CSaferEnforcementPropertyPage)
  52. // NOTE: the ClassWizard will add member initialization here
  53. //}}AFX_DATA_INIT
  54. if ( m_pGPEInformation )
  55. {
  56. m_pGPEInformation->AddRef ();
  57. HRESULT hr = m_pGPEInformation->GetRegistryKey (
  58. m_fIsComputerType ? GPO_SECTION_MACHINE : GPO_SECTION_USER,
  59. &m_hGroupPolicyKey);
  60. ASSERT (SUCCEEDED (hr));
  61. if ( SUCCEEDED (hr) )
  62. {
  63. DWORD cbBuffer = sizeof (DWORD);
  64. CPolicyKey policyKey (m_pGPEInformation,
  65. SAFER_HKLM_REGBASE,
  66. m_fIsComputerType);
  67. SetRegistryScope (policyKey.GetKey (), bIsComputer);
  68. BOOL bRVal = SaferGetPolicyInformation (
  69. SAFER_SCOPEID_REGISTRY,
  70. SaferPolicyEnableTransparentEnforcement,
  71. cbBuffer,
  72. &m_dwEnforcement,
  73. &cbBuffer,
  74. 0);
  75. if ( !bRVal )
  76. {
  77. ASSERT (0);
  78. DWORD dwErr = GetLastError ();
  79. hr = HRESULT_FROM_WIN32 (dwErr);
  80. _TRACE (0, L"SaferGetPolicyInformation (SAFER_SCOPEID_REGISTRY, SaferPolicyEnableTransparentEnforcement) failed: %d\n",
  81. dwErr);
  82. }
  83. bRVal = SaferGetPolicyInformation (
  84. SAFER_SCOPEID_REGISTRY,
  85. SaferPolicyScopeFlags,
  86. cbBuffer,
  87. &m_dwScopeFlags,
  88. &cbBuffer,
  89. 0);
  90. if ( !bRVal )
  91. {
  92. ASSERT (0);
  93. DWORD dwErr = GetLastError ();
  94. hr = HRESULT_FROM_WIN32 (dwErr);
  95. _TRACE (0, L"SaferGetPolicyInformation (SAFER_SCOPEID_REGISTRY, SaferPolicyScopeFlags) failed: %d\n",
  96. dwErr);
  97. }
  98. }
  99. }
  100. else
  101. {
  102. RSOPGetEnforcement (pCompData);
  103. }
  104. }
  105. CSaferEnforcementPropertyPage::~CSaferEnforcementPropertyPage()
  106. {
  107. if ( m_hGroupPolicyKey )
  108. RegCloseKey (m_hGroupPolicyKey);
  109. if ( m_pGPEInformation )
  110. {
  111. m_pGPEInformation->Release ();
  112. }
  113. }
  114. void CSaferEnforcementPropertyPage::DoDataExchange(CDataExchange* pDX)
  115. {
  116. CHelpPropertyPage::DoDataExchange(pDX);
  117. //{{AFX_DATA_MAP(CSaferEnforcementPropertyPage)
  118. // NOTE: the ClassWizard will add DDX and DDV calls here
  119. //}}AFX_DATA_MAP
  120. }
  121. BEGIN_MESSAGE_MAP(CSaferEnforcementPropertyPage, CHelpPropertyPage)
  122. //{{AFX_MSG_MAP(CSaferEnforcementPropertyPage)
  123. ON_BN_CLICKED(IDC_ALL_EXCEPT_LIBS, OnAllExceptLibs)
  124. ON_BN_CLICKED(IDC_ALL_SOFTWARE_FILES, OnAllSoftwareFiles)
  125. ON_BN_CLICKED(IDC_APPLY_EXCEPT_ADMINS, OnApplyExceptAdmins)
  126. ON_BN_CLICKED(IDC_APPLY_TO_ALL_USERS, OnApplyToAllUsers)
  127. //}}AFX_MSG_MAP
  128. END_MESSAGE_MAP()
  129. /////////////////////////////////////////////////////////////////////////////
  130. // CSaferEnforcementPropertyPage message handlers
  131. void CSaferEnforcementPropertyPage::DoContextHelp (HWND hWndControl)
  132. {
  133. _TRACE (1, L"Entering CSaferEnforcementPropertyPage::DoContextHelp\n");
  134. static const DWORD help_map[] =
  135. {
  136. IDC_ALL_EXCEPT_LIBS, IDH_ALL_EXCEPT_LIBS,
  137. IDC_ALL_SOFTWARE_FILES, IDH_ALL_SOFTWARE_FILES,
  138. IDC_APPLY_TO_ALL_USERS, IDH_APPLY_TO_ALL_USERS,
  139. IDC_APPLY_EXCEPT_ADMINS, IDH_APPLY_EXCEPT_ADMINS,
  140. 0, 0
  141. };
  142. if ( !::WinHelp (
  143. hWndControl,
  144. GetF1HelpFilename(),
  145. HELP_WM_HELP,
  146. (DWORD_PTR) help_map) )
  147. {
  148. _TRACE (0, L"WinHelp () failed: 0x%x\n", GetLastError ());
  149. }
  150. _TRACE (-1, L"Leaving CSaferEnforcementPropertyPage::DoContextHelp\n");
  151. }
  152. void CSaferEnforcementPropertyPage::RSOPGetEnforcement(CCertMgrComponentData* /*pCompData*/)
  153. {
  154. int nIndex = 0;
  155. INT_PTR nUpperBound = m_rsopObjectArray.GetUpperBound ();
  156. bool bEnforcementFlagFound = false;
  157. bool bScopeFlagFound = false;
  158. while ( nUpperBound >= nIndex )
  159. {
  160. CRSOPObject* pObject = m_rsopObjectArray.GetAt (nIndex);
  161. if ( pObject )
  162. {
  163. if ( pObject->GetRegistryKey () == SAFER_HKLM_REGBASE &&
  164. pObject->GetValueName () == SAFER_TRANSPARENTENABLED_REGVALUE &&
  165. 1 == pObject->GetPrecedence ())
  166. {
  167. m_dwEnforcement = pObject->GetDWORDValue ();
  168. bEnforcementFlagFound = true;
  169. }
  170. else if ( pObject->GetRegistryKey () == SAFER_HKLM_REGBASE &&
  171. pObject->GetValueName () == SAFER_POLICY_SCOPE &&
  172. 1 == pObject->GetPrecedence ())
  173. {
  174. m_dwScopeFlags = pObject->GetDWORDValue ();
  175. bScopeFlagFound = true;
  176. }
  177. }
  178. else
  179. break;
  180. if ( bScopeFlagFound && bEnforcementFlagFound )
  181. break;
  182. nIndex++;
  183. }
  184. }
  185. void CSaferEnforcementPropertyPage::OnAllExceptLibs()
  186. {
  187. SetModified ();
  188. m_bDirty = true;
  189. }
  190. void CSaferEnforcementPropertyPage::OnAllSoftwareFiles()
  191. {
  192. SetModified ();
  193. m_bDirty = true;
  194. }
  195. BOOL CSaferEnforcementPropertyPage::OnApply()
  196. {
  197. _TRACE (1, L"Entering CSaferEnforcementPropertyPage::OnApply ()\n");
  198. if ( m_bDirty && m_pGPEInformation)
  199. {
  200. if ( BST_CHECKED == SendDlgItemMessage (IDC_ALL_EXCEPT_LIBS,
  201. BM_GETCHECK) )
  202. {
  203. m_dwEnforcement = SAFER_TRANSPARENT_ENFORCEMENT_ENABLE_CREATE_PROCESS;
  204. }
  205. else if ( BST_CHECKED == SendDlgItemMessage (IDC_ALL_SOFTWARE_FILES,
  206. BM_GETCHECK) )
  207. {
  208. m_dwEnforcement = SAFER_TRANSPARENT_ENFORCEMENT_ENABLE_ALL;
  209. }
  210. if ( BST_CHECKED == SendDlgItemMessage (IDC_APPLY_EXCEPT_ADMINS,
  211. BM_GETCHECK) )
  212. {
  213. m_dwScopeFlags = 1;
  214. }
  215. else if ( BST_CHECKED == SendDlgItemMessage (IDC_APPLY_TO_ALL_USERS,
  216. BM_GETCHECK) )
  217. {
  218. m_dwScopeFlags = 0;
  219. }
  220. CPolicyKey policyKey (m_pGPEInformation,
  221. SAFER_HKLM_REGBASE,
  222. m_fIsComputerType);
  223. SetRegistryScope (policyKey.GetKey (), m_fIsComputerType);
  224. DWORD cbData = sizeof (m_dwEnforcement);
  225. BOOL bRVal = SaferSetPolicyInformation (SAFER_SCOPEID_REGISTRY,
  226. SaferPolicyEnableTransparentEnforcement, cbData,
  227. &m_dwEnforcement, 0);
  228. if ( bRVal )
  229. {
  230. cbData = sizeof (m_dwScopeFlags);
  231. bRVal = SaferSetPolicyInformation (SAFER_SCOPEID_REGISTRY,
  232. SaferPolicyScopeFlags, cbData,
  233. &m_dwScopeFlags, 0);
  234. if ( bRVal )
  235. {
  236. // TRUE means we're changing the machine policy only
  237. m_pGPEInformation->PolicyChanged (m_fIsComputerType ? TRUE : FALSE,
  238. TRUE, &g_guidExtension, &g_guidSnapin);
  239. m_pGPEInformation->PolicyChanged (m_fIsComputerType ? TRUE : FALSE,
  240. TRUE, &g_guidRegExt, &g_guidSnapin);
  241. }
  242. else
  243. {
  244. DWORD dwErr = GetLastError ();
  245. _TRACE (0, L"SaferSetPolicyInformation (SAFER_SCOPEID_REGISTRY, SaferPolicyScopeFlags, %d failed: 0x%x\n",
  246. m_dwEnforcement, dwErr);
  247. CString text;
  248. CString caption;
  249. CThemeContextActivator activator;
  250. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  251. text.FormatMessage (IDS_CAN_SET_SAFER_ENFORCEMENT, GetSystemMessage (dwErr));
  252. MessageBox (text, caption);
  253. return FALSE;
  254. }
  255. }
  256. else
  257. {
  258. DWORD dwErr = GetLastError ();
  259. _TRACE (0, L"SaferSetPolicyInformation (SAFER_SCOPEID_REGISTRY, SaferPolicyEnableTransparentEnforcement, %d failed: 0x%x\n",
  260. m_dwEnforcement, dwErr);
  261. CString text;
  262. CString caption;
  263. CThemeContextActivator activator;
  264. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  265. text.FormatMessage (IDS_CAN_SET_SAFER_ENFORCEMENT, GetSystemMessage (dwErr));
  266. MessageBox (text, caption);
  267. return FALSE;
  268. }
  269. m_bDirty = false;
  270. }
  271. _TRACE (-1, L"Leaving CSaferEnforcementPropertyPage::OnApply ()\n");
  272. return CHelpPropertyPage::OnApply();
  273. }
  274. BOOL CSaferEnforcementPropertyPage::OnInitDialog()
  275. {
  276. CHelpPropertyPage::OnInitDialog();
  277. switch (m_dwEnforcement)
  278. {
  279. case SAFER_TRANSPARENT_ENFORCEMENT_DISABLE_ALL:
  280. break;
  281. case SAFER_TRANSPARENT_ENFORCEMENT_ENABLE_CREATE_PROCESS:
  282. SendDlgItemMessage (IDC_ALL_EXCEPT_LIBS, BM_SETCHECK, BST_CHECKED);
  283. break;
  284. case SAFER_TRANSPARENT_ENFORCEMENT_ENABLE_ALL:
  285. SendDlgItemMessage (IDC_ALL_SOFTWARE_FILES, BM_SETCHECK, BST_CHECKED);
  286. break;
  287. default:
  288. ASSERT (0);
  289. break;
  290. }
  291. if ( 1 == m_dwScopeFlags )
  292. {
  293. SendDlgItemMessage (IDC_APPLY_EXCEPT_ADMINS, BM_SETCHECK, BST_CHECKED);
  294. }
  295. else
  296. {
  297. SendDlgItemMessage (IDC_APPLY_TO_ALL_USERS, BM_SETCHECK, BST_CHECKED);
  298. }
  299. if ( m_bReadOnly )
  300. {
  301. GetDlgItem (IDC_ALL_EXCEPT_LIBS)->EnableWindow (FALSE);
  302. GetDlgItem (IDC_ALL_SOFTWARE_FILES)->EnableWindow (FALSE);
  303. GetDlgItem (IDC_APPLY_TO_ALL_USERS)->EnableWindow (FALSE);
  304. GetDlgItem (IDC_APPLY_EXCEPT_ADMINS)->EnableWindow (FALSE);
  305. }
  306. return TRUE; // return TRUE unless you set the focus to a control
  307. // EXCEPTION: OCX Property Pages should return FALSE
  308. }
  309. void CSaferEnforcementPropertyPage::OnApplyExceptAdmins()
  310. {
  311. SetModified ();
  312. m_bDirty = true;
  313. }
  314. void CSaferEnforcementPropertyPage::OnApplyToAllUsers()
  315. {
  316. SetModified ();
  317. m_bDirty = true;
  318. }