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.

406 lines
13 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000-2002.
  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. m_pCompData (pCompData)
  51. {
  52. //{{AFX_DATA_INIT(CSaferEnforcementPropertyPage)
  53. // NOTE: the ClassWizard will add member initialization here
  54. //}}AFX_DATA_INIT
  55. if ( m_pCompData )
  56. {
  57. m_pCompData->AddRef ();
  58. m_pCompData->IncrementOpenSaferPageCount ();
  59. }
  60. if ( m_pGPEInformation )
  61. {
  62. m_pGPEInformation->AddRef ();
  63. HRESULT hr = m_pGPEInformation->GetRegistryKey (
  64. m_fIsComputerType ? GPO_SECTION_MACHINE : GPO_SECTION_USER,
  65. &m_hGroupPolicyKey);
  66. ASSERT (SUCCEEDED (hr));
  67. if ( SUCCEEDED (hr) )
  68. {
  69. DWORD cbBuffer = sizeof (DWORD);
  70. CPolicyKey policyKey (m_pGPEInformation,
  71. SAFER_HKLM_REGBASE,
  72. m_fIsComputerType);
  73. SetRegistryScope (policyKey.GetKey (), bIsComputer);
  74. BOOL bRVal = SaferGetPolicyInformation (
  75. SAFER_SCOPEID_REGISTRY,
  76. SaferPolicyEnableTransparentEnforcement,
  77. cbBuffer,
  78. &m_dwEnforcement,
  79. &cbBuffer,
  80. 0);
  81. if ( !bRVal )
  82. {
  83. ASSERT (0);
  84. DWORD dwErr = GetLastError ();
  85. hr = HRESULT_FROM_WIN32 (dwErr);
  86. _TRACE (0, L"SaferGetPolicyInformation (SAFER_SCOPEID_REGISTRY, SaferPolicyEnableTransparentEnforcement) failed: %d\n",
  87. dwErr);
  88. }
  89. bRVal = SaferGetPolicyInformation (
  90. SAFER_SCOPEID_REGISTRY,
  91. SaferPolicyScopeFlags,
  92. cbBuffer,
  93. &m_dwScopeFlags,
  94. &cbBuffer,
  95. 0);
  96. if ( !bRVal )
  97. {
  98. ASSERT (0);
  99. DWORD dwErr = GetLastError ();
  100. hr = HRESULT_FROM_WIN32 (dwErr);
  101. _TRACE (0, L"SaferGetPolicyInformation (SAFER_SCOPEID_REGISTRY, SaferPolicyScopeFlags) failed: %d\n",
  102. dwErr);
  103. }
  104. }
  105. }
  106. else
  107. {
  108. RSOPGetEnforcement ();
  109. }
  110. }
  111. CSaferEnforcementPropertyPage::~CSaferEnforcementPropertyPage()
  112. {
  113. if ( m_hGroupPolicyKey )
  114. RegCloseKey (m_hGroupPolicyKey);
  115. if ( m_pGPEInformation )
  116. {
  117. m_pGPEInformation->Release ();
  118. }
  119. if ( m_pCompData )
  120. {
  121. m_pCompData->DecrementOpenSaferPageCount ();
  122. m_pCompData->Release ();
  123. m_pCompData = 0;
  124. }
  125. }
  126. void CSaferEnforcementPropertyPage::DoDataExchange(CDataExchange* pDX)
  127. {
  128. CHelpPropertyPage::DoDataExchange(pDX);
  129. //{{AFX_DATA_MAP(CSaferEnforcementPropertyPage)
  130. // NOTE: the ClassWizard will add DDX and DDV calls here
  131. //}}AFX_DATA_MAP
  132. }
  133. BEGIN_MESSAGE_MAP(CSaferEnforcementPropertyPage, CHelpPropertyPage)
  134. //{{AFX_MSG_MAP(CSaferEnforcementPropertyPage)
  135. ON_BN_CLICKED(IDC_ALL_EXCEPT_LIBS, OnAllExceptLibs)
  136. ON_BN_CLICKED(IDC_ALL_SOFTWARE_FILES, OnAllSoftwareFiles)
  137. ON_BN_CLICKED(IDC_APPLY_EXCEPT_ADMINS, OnApplyExceptAdmins)
  138. ON_BN_CLICKED(IDC_APPLY_TO_ALL_USERS, OnApplyToAllUsers)
  139. //}}AFX_MSG_MAP
  140. END_MESSAGE_MAP()
  141. /////////////////////////////////////////////////////////////////////////////
  142. // CSaferEnforcementPropertyPage message handlers
  143. void CSaferEnforcementPropertyPage::DoContextHelp (HWND hWndControl)
  144. {
  145. _TRACE (1, L"Entering CSaferEnforcementPropertyPage::DoContextHelp\n");
  146. static const DWORD help_map[] =
  147. {
  148. IDC_ALL_EXCEPT_LIBS, IDH_ALL_EXCEPT_LIBS,
  149. IDC_ALL_SOFTWARE_FILES, IDH_ALL_SOFTWARE_FILES,
  150. IDC_APPLY_TO_ALL_USERS, IDH_APPLY_TO_ALL_USERS,
  151. IDC_APPLY_EXCEPT_ADMINS, IDH_APPLY_EXCEPT_ADMINS,
  152. 0, 0
  153. };
  154. switch (::GetDlgCtrlID (hWndControl))
  155. {
  156. case IDC_ALL_EXCEPT_LIBS:
  157. case IDC_ALL_SOFTWARE_FILES:
  158. case IDC_APPLY_TO_ALL_USERS:
  159. case IDC_APPLY_EXCEPT_ADMINS:
  160. if ( !::WinHelp (
  161. hWndControl,
  162. GetF1HelpFilename(),
  163. HELP_WM_HELP,
  164. (DWORD_PTR) help_map) )
  165. {
  166. _TRACE (0, L"WinHelp () failed: 0x%x\n", GetLastError ());
  167. }
  168. break;
  169. default:
  170. break;
  171. }
  172. _TRACE (-1, L"Leaving CSaferEnforcementPropertyPage::DoContextHelp\n");
  173. }
  174. void CSaferEnforcementPropertyPage::RSOPGetEnforcement()
  175. {
  176. int nIndex = 0;
  177. INT_PTR nUpperBound = m_rsopObjectArray.GetUpperBound ();
  178. bool bEnforcementFlagFound = false;
  179. bool bScopeFlagFound = false;
  180. CString szKeyName = SAFER_HKLM_REGBASE;
  181. szKeyName += L"\\";
  182. szKeyName += SAFER_CODEIDS_REGSUBKEY;
  183. while ( nUpperBound >= nIndex )
  184. {
  185. CRSOPObject* pObject = m_rsopObjectArray.GetAt (nIndex);
  186. if ( pObject )
  187. {
  188. if ( pObject->GetRegistryKey () == szKeyName &&
  189. pObject->GetValueName () == SAFER_TRANSPARENTENABLED_REGVALUE &&
  190. 1 == pObject->GetPrecedence ())
  191. {
  192. m_dwEnforcement = pObject->GetDWORDValue ();
  193. bEnforcementFlagFound = true;
  194. }
  195. else if ( pObject->GetRegistryKey () == szKeyName &&
  196. pObject->GetValueName () == SAFER_POLICY_SCOPE &&
  197. 1 == pObject->GetPrecedence ())
  198. {
  199. m_dwScopeFlags = pObject->GetDWORDValue ();
  200. bScopeFlagFound = true;
  201. }
  202. }
  203. else
  204. break;
  205. if ( bScopeFlagFound && bEnforcementFlagFound )
  206. break;
  207. nIndex++;
  208. }
  209. }
  210. void CSaferEnforcementPropertyPage::OnAllExceptLibs()
  211. {
  212. // SetModify only if setting changed
  213. if ( !(m_dwEnforcement & SAFER_TRANSPARENT_ENFORCEMENT_ENABLE_CREATE_PROCESS) )
  214. {
  215. SetModified ();
  216. m_bDirty = true;
  217. }
  218. }
  219. void CSaferEnforcementPropertyPage::OnAllSoftwareFiles()
  220. {
  221. // SetModify only if setting changed
  222. if ( !(m_dwEnforcement & SAFER_TRANSPARENT_ENFORCEMENT_ENABLE_ALL) )
  223. {
  224. SetModified ();
  225. m_bDirty = true;
  226. }
  227. }
  228. BOOL CSaferEnforcementPropertyPage::OnApply()
  229. {
  230. _TRACE (1, L"Entering CSaferEnforcementPropertyPage::OnApply ()\n");
  231. if ( m_bDirty && m_pGPEInformation)
  232. {
  233. if ( BST_CHECKED == SendDlgItemMessage (IDC_ALL_EXCEPT_LIBS,
  234. BM_GETCHECK) )
  235. {
  236. m_dwEnforcement = SAFER_TRANSPARENT_ENFORCEMENT_ENABLE_CREATE_PROCESS;
  237. }
  238. else if ( BST_CHECKED == SendDlgItemMessage (IDC_ALL_SOFTWARE_FILES,
  239. BM_GETCHECK) )
  240. {
  241. m_dwEnforcement = SAFER_TRANSPARENT_ENFORCEMENT_ENABLE_ALL;
  242. }
  243. if ( BST_CHECKED == SendDlgItemMessage (IDC_APPLY_EXCEPT_ADMINS,
  244. BM_GETCHECK) )
  245. {
  246. m_dwScopeFlags = 1;
  247. }
  248. else if ( BST_CHECKED == SendDlgItemMessage (IDC_APPLY_TO_ALL_USERS,
  249. BM_GETCHECK) )
  250. {
  251. m_dwScopeFlags = 0;
  252. }
  253. CPolicyKey policyKey (m_pGPEInformation,
  254. SAFER_HKLM_REGBASE,
  255. m_fIsComputerType);
  256. SetRegistryScope (policyKey.GetKey (), m_fIsComputerType);
  257. DWORD cbData = sizeof (m_dwEnforcement);
  258. BOOL bRVal = SaferSetPolicyInformation (SAFER_SCOPEID_REGISTRY,
  259. SaferPolicyEnableTransparentEnforcement, cbData,
  260. &m_dwEnforcement, 0);
  261. if ( bRVal )
  262. {
  263. cbData = sizeof (m_dwScopeFlags);
  264. bRVal = SaferSetPolicyInformation (SAFER_SCOPEID_REGISTRY,
  265. SaferPolicyScopeFlags, cbData,
  266. &m_dwScopeFlags, 0);
  267. if ( bRVal )
  268. {
  269. // TRUE means we're changing the machine policy only
  270. m_pGPEInformation->PolicyChanged (m_fIsComputerType ? TRUE : FALSE,
  271. TRUE, &g_guidExtension, &g_guidSnapin);
  272. m_pGPEInformation->PolicyChanged (m_fIsComputerType ? TRUE : FALSE,
  273. TRUE, &g_guidRegExt, &g_guidSnapin);
  274. }
  275. else
  276. {
  277. DWORD dwErr = GetLastError ();
  278. _TRACE (0, L"SaferSetPolicyInformation (SAFER_SCOPEID_REGISTRY, SaferPolicyScopeFlags, %d failed: 0x%x\n",
  279. m_dwEnforcement, dwErr);
  280. CString text;
  281. CString caption;
  282. CThemeContextActivator activator;
  283. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  284. text.FormatMessage (IDS_CAN_SET_SAFER_ENFORCEMENT, GetSystemMessage (dwErr));
  285. MessageBox (text, caption);
  286. return FALSE;
  287. }
  288. }
  289. else
  290. {
  291. DWORD dwErr = GetLastError ();
  292. _TRACE (0, L"SaferSetPolicyInformation (SAFER_SCOPEID_REGISTRY, SaferPolicyEnableTransparentEnforcement, %d failed: 0x%x\n",
  293. m_dwEnforcement, dwErr);
  294. CString text;
  295. CString caption;
  296. CThemeContextActivator activator;
  297. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  298. text.FormatMessage (IDS_CAN_SET_SAFER_ENFORCEMENT, GetSystemMessage (dwErr));
  299. MessageBox (text, caption);
  300. return FALSE;
  301. }
  302. m_bDirty = false;
  303. }
  304. _TRACE (-1, L"Leaving CSaferEnforcementPropertyPage::OnApply ()\n");
  305. return CHelpPropertyPage::OnApply();
  306. }
  307. BOOL CSaferEnforcementPropertyPage::OnInitDialog()
  308. {
  309. CHelpPropertyPage::OnInitDialog();
  310. switch (m_dwEnforcement)
  311. {
  312. case SAFER_TRANSPARENT_ENFORCEMENT_DISABLE_ALL:
  313. break;
  314. case SAFER_TRANSPARENT_ENFORCEMENT_ENABLE_CREATE_PROCESS:
  315. SendDlgItemMessage (IDC_ALL_EXCEPT_LIBS, BM_SETCHECK, BST_CHECKED);
  316. break;
  317. case SAFER_TRANSPARENT_ENFORCEMENT_ENABLE_ALL:
  318. SendDlgItemMessage (IDC_ALL_SOFTWARE_FILES, BM_SETCHECK, BST_CHECKED);
  319. break;
  320. default:
  321. ASSERT (0);
  322. break;
  323. }
  324. if ( 1 == m_dwScopeFlags )
  325. {
  326. SendDlgItemMessage (IDC_APPLY_EXCEPT_ADMINS, BM_SETCHECK, BST_CHECKED);
  327. }
  328. else
  329. {
  330. SendDlgItemMessage (IDC_APPLY_TO_ALL_USERS, BM_SETCHECK, BST_CHECKED);
  331. }
  332. if ( m_bReadOnly )
  333. {
  334. GetDlgItem (IDC_ALL_EXCEPT_LIBS)->EnableWindow (FALSE);
  335. GetDlgItem (IDC_ALL_SOFTWARE_FILES)->EnableWindow (FALSE);
  336. GetDlgItem (IDC_APPLY_TO_ALL_USERS)->EnableWindow (FALSE);
  337. GetDlgItem (IDC_APPLY_EXCEPT_ADMINS)->EnableWindow (FALSE);
  338. }
  339. return TRUE; // return TRUE unless you set the focus to a control
  340. // EXCEPTION: OCX Property Pages should return FALSE
  341. }
  342. void CSaferEnforcementPropertyPage::OnApplyExceptAdmins()
  343. {
  344. // SetModify only if setting changed
  345. if ( 1 != m_dwScopeFlags )
  346. {
  347. SetModified ();
  348. m_bDirty = true;
  349. }
  350. }
  351. void CSaferEnforcementPropertyPage::OnApplyToAllUsers()
  352. {
  353. // SetModify only if setting changed
  354. if ( 1 == m_dwScopeFlags )
  355. {
  356. SetModified ();
  357. m_bDirty = true;
  358. }
  359. }