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.

315 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2001-2002.
  5. //
  6. // File: EFSGeneralPropertyPage.cpp
  7. //
  8. // Contents: Implementation of CEFSGeneralPropertyPage
  9. //
  10. //----------------------------------------------------------------------------
  11. // EFSGeneralPropertyPage.cpp : implementation file
  12. //
  13. #include "stdafx.h"
  14. #include <gpedit.h>
  15. #include "EFSGeneralPropertyPage.h"
  16. #include "compdata.h"
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. extern GUID g_guidExtension;
  23. extern GUID g_guidRegExt;
  24. extern GUID g_guidSnapin;
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CEFSGeneralPropertyPage property page
  27. CEFSGeneralPropertyPage::CEFSGeneralPropertyPage(CCertMgrComponentData* pCompData, bool bIsMachine)
  28. : CHelpPropertyPage(CEFSGeneralPropertyPage::IDD),
  29. m_bIsMachine (bIsMachine),
  30. m_hGroupPolicyKey (0),
  31. m_pGPEInformation (pCompData ? pCompData->GetGPEInformation () : 0),
  32. m_pCompData (pCompData),
  33. m_bDirty (false)
  34. {
  35. //{{AFX_DATA_INIT(CEFSGeneralPropertyPage)
  36. // NOTE: the ClassWizard will add member initialization here
  37. //}}AFX_DATA_INIT
  38. if ( m_pCompData )
  39. m_pCompData->AddRef ();
  40. if ( m_pGPEInformation )
  41. {
  42. HRESULT hResult = m_pGPEInformation->GetRegistryKey (m_bIsMachine ?
  43. GPO_SECTION_MACHINE : GPO_SECTION_USER,
  44. &m_hGroupPolicyKey);
  45. ASSERT (SUCCEEDED (hResult));
  46. }
  47. }
  48. CEFSGeneralPropertyPage::~CEFSGeneralPropertyPage()
  49. {
  50. if ( m_pCompData )
  51. m_pCompData->Release ();
  52. }
  53. void CEFSGeneralPropertyPage::DoDataExchange(CDataExchange* pDX)
  54. {
  55. CHelpPropertyPage::DoDataExchange(pDX);
  56. //{{AFX_DATA_MAP(CEFSGeneralPropertyPage)
  57. // NOTE: the ClassWizard will add DDX and DDV calls here
  58. //}}AFX_DATA_MAP
  59. }
  60. BEGIN_MESSAGE_MAP(CEFSGeneralPropertyPage, CHelpPropertyPage)
  61. //{{AFX_MSG_MAP(CEFSGeneralPropertyPage)
  62. ON_BN_CLICKED(IDC_TURN_ON_EFS, OnTurnOnEfs)
  63. //}}AFX_MSG_MAP
  64. END_MESSAGE_MAP()
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CEFSGeneralPropertyPage message handlers
  67. BOOL CEFSGeneralPropertyPage::OnInitDialog()
  68. {
  69. CHelpPropertyPage::OnInitDialog();
  70. // The regkey to disable EFS is:
  71. // HKLM\Software\Policies\Microsoft\Windows NT\CurrentVersion\EFS\EfsConfiguration DWORD 0x00000001 =>Disable EFS
  72. // If this is the RSOP, make it read-only
  73. if ( !m_pGPEInformation )
  74. {
  75. // Make the page read-only
  76. GetDlgItem (IDC_TURN_ON_EFS)->EnableWindow (FALSE);
  77. RSOPGetEFSFlags ();
  78. }
  79. else
  80. {
  81. GPEGetEFSFlags ();
  82. }
  83. return TRUE; // return TRUE unless you set the focus to a control
  84. // EXCEPTION: OCX Property Pages should return FALSE
  85. }
  86. void CEFSGeneralPropertyPage::GPEGetEFSFlags()
  87. {
  88. HKEY hKey = 0;
  89. LONG lResult = ::RegOpenKeyEx (m_hGroupPolicyKey, // handle to open key
  90. EFS_SETTINGS_REGPATH, // subkey name
  91. 0, // reserved
  92. KEY_READ, // security access mask
  93. &hKey); // handle to open key
  94. if ( ERROR_SUCCESS == lResult )
  95. {
  96. // Read value
  97. DWORD dwType = REG_DWORD;
  98. DWORD dwData = 0;
  99. DWORD cbData = sizeof (dwData);
  100. // security review 2/22/2002 BryanWal ok
  101. lResult = ::RegQueryValueEx (hKey, // handle of key to query
  102. EFS_SETTINGS_REGVALUE, // address of name of value to query
  103. 0, // reserved
  104. &dwType, // address of buffer for value type
  105. (LPBYTE) &dwData, // address of data buffer
  106. &cbData); // address of data buffer size);
  107. ASSERT ((ERROR_SUCCESS == lResult && REG_DWORD == dwType) || ERROR_FILE_NOT_FOUND == lResult);
  108. if ( (ERROR_SUCCESS == lResult && REG_DWORD == dwType) || ERROR_FILE_NOT_FOUND == lResult )
  109. {
  110. if ( 0 == dwData ) // 0 means enable EFS
  111. SendDlgItemMessage (IDC_TURN_ON_EFS, BM_SETCHECK, BST_CHECKED);
  112. }
  113. else
  114. DisplaySystemError (NULL, lResult);
  115. ::RegCloseKey (hKey);
  116. }
  117. else // no key means EFS enabled
  118. SendDlgItemMessage (IDC_TURN_ON_EFS, BM_SETCHECK, BST_CHECKED);
  119. }
  120. void CEFSGeneralPropertyPage::DoContextHelp (HWND hWndControl)
  121. {
  122. _TRACE (1, L"Entering CEFSGeneralPropertyPage::DoContextHelp\n");
  123. static const DWORD help_map[] =
  124. {
  125. IDC_TURN_ON_EFS, IDH_TURN_ON_EFS,
  126. 0, 0
  127. };
  128. switch (::GetDlgCtrlID (hWndControl))
  129. {
  130. case IDC_TURN_ON_EFS:
  131. if ( !::WinHelp (
  132. hWndControl,
  133. GetF1HelpFilename(),
  134. HELP_WM_HELP,
  135. (DWORD_PTR) help_map) )
  136. {
  137. _TRACE (0, L"WinHelp () failed: 0x%x\n", GetLastError ());
  138. }
  139. break;
  140. default:
  141. break;
  142. }
  143. _TRACE (-1, L"Leaving CEFSGeneralPropertyPage::DoContextHelp\n");
  144. }
  145. void CEFSGeneralPropertyPage::RSOPGetEFSFlags()
  146. {
  147. if ( m_pCompData )
  148. {
  149. const CRSOPObjectArray* pObjectArray =
  150. m_bIsMachine ? m_pCompData->GetRSOPObjectArrayComputer () :
  151. m_pCompData->GetRSOPObjectArrayUser ();
  152. int nIndex = 0;
  153. bool bFound = false;
  154. // NOTE: rsop object array is sorted first by registry key, then by precedence
  155. INT_PTR nUpperBound = pObjectArray->GetUpperBound ();
  156. while ( nUpperBound >= nIndex )
  157. {
  158. CRSOPObject* pObject = pObjectArray->GetAt (nIndex);
  159. if ( pObject )
  160. {
  161. // Consider only entries from this store
  162. // security review 2/22/2002 BryanWal ok
  163. if ( !_wcsicmp (EFS_SETTINGS_REGPATH, pObject->GetRegistryKey ()) &&
  164. !_wcsicmp (EFS_SETTINGS_REGVALUE, pObject->GetValueName ()) )
  165. {
  166. ASSERT (1 == pObject->GetPrecedence ());
  167. if ( 0 == pObject->GetDWORDValue () ) // 0 means enable EFS
  168. SendDlgItemMessage (IDC_TURN_ON_EFS, BM_SETCHECK, BST_CHECKED);
  169. bFound = true;
  170. break;
  171. }
  172. }
  173. else
  174. break;
  175. nIndex++;
  176. }
  177. if ( !bFound ) // not found means EFS enabled
  178. SendDlgItemMessage (IDC_TURN_ON_EFS, BM_SETCHECK, BST_CHECKED);
  179. }
  180. }
  181. BOOL CEFSGeneralPropertyPage::OnApply()
  182. {
  183. if ( m_bDirty && m_pGPEInformation )
  184. {
  185. // Unchecked means disable EFS - set flag to 1
  186. if ( BST_UNCHECKED == SendDlgItemMessage (IDC_TURN_ON_EFS, BM_GETCHECK) )
  187. {
  188. // Create Key
  189. HKEY hKey = 0;
  190. DWORD dwDisposition = 0;
  191. LONG lResult = ::RegCreateKeyEx (m_hGroupPolicyKey, // handle of an open key
  192. EFS_SETTINGS_REGPATH, // address of subkey name
  193. 0, // reserved
  194. L"", // address of class string
  195. REG_OPTION_NON_VOLATILE, // special options flag
  196. // security review 2/22/2002 BryanWal ok
  197. KEY_SET_VALUE, // desired security access
  198. NULL, // address of key security structure
  199. &hKey, // address of buffer for opened handle
  200. &dwDisposition); // address of disposition value buffer
  201. ASSERT (lResult == ERROR_SUCCESS);
  202. if ( lResult == ERROR_SUCCESS )
  203. {
  204. DWORD dwData = 0x01; // 0 means disable EFS
  205. DWORD cbData = sizeof (dwData);
  206. lResult = ::RegSetValueEx (hKey,
  207. EFS_SETTINGS_REGVALUE, // address of value to set
  208. 0, // reserved
  209. REG_DWORD, // flag for value type
  210. (CONST BYTE *) &dwData, // address of value data
  211. cbData); // size of value data);
  212. ASSERT (ERROR_SUCCESS == lResult);
  213. if ( ERROR_SUCCESS == lResult )
  214. {
  215. // TRUE means we're changing the machine policy only
  216. m_pGPEInformation->PolicyChanged (m_bIsMachine ? TRUE : FALSE,
  217. TRUE, &g_guidExtension, &g_guidSnapin);
  218. m_pGPEInformation->PolicyChanged (m_bIsMachine ? TRUE : FALSE,
  219. TRUE, &g_guidRegExt, &g_guidSnapin);
  220. }
  221. else
  222. DisplaySystemError (m_hWnd, lResult);
  223. ::RegCloseKey (hKey);
  224. }
  225. }
  226. else
  227. {
  228. // Delete Key
  229. HKEY hKey = 0;
  230. LONG lResult = ::RegOpenKeyEx (m_hGroupPolicyKey, // handle to open key
  231. EFS_SETTINGS_REGPATH, // subkey name
  232. 0, // reserved
  233. // security review 2/22/2002 BryanWal ok KEY_ALL_ACCESS required to delete
  234. KEY_ALL_ACCESS, // security access mask
  235. &hKey); // handle to open key
  236. if ( ERROR_SUCCESS == lResult )
  237. {
  238. lResult = ::RegDeleteValue (hKey, // handle of key to query
  239. EFS_SETTINGS_REGVALUE);
  240. ASSERT (ERROR_SUCCESS == lResult);
  241. if ( ERROR_SUCCESS == lResult )
  242. {
  243. // TRUE means we're changing the machine policy only
  244. m_pGPEInformation->PolicyChanged (m_bIsMachine ? TRUE : FALSE,
  245. TRUE, &g_guidExtension, &g_guidSnapin);
  246. m_pGPEInformation->PolicyChanged (m_bIsMachine ? TRUE : FALSE,
  247. TRUE, &g_guidRegExt, &g_guidSnapin);
  248. }
  249. else if ( ERROR_FILE_NOT_FOUND != lResult )
  250. {
  251. CString text;
  252. CString caption;
  253. // security review 2/22/2002 BryanWal ok
  254. text.FormatMessage (IDS_CANNOT_SET_EFS_VALUE, lResult);
  255. VERIFY (caption.LoadString (IDS_PUBLIC_KEY_POLICIES_NODE_NAME));
  256. MessageBox (text, caption, MB_OK | MB_ICONWARNING);
  257. return FALSE;
  258. }
  259. ::RegCloseKey (hKey);
  260. }
  261. else if ( ERROR_FILE_NOT_FOUND != lResult ) // expected error
  262. {
  263. CString text;
  264. CString caption;
  265. // security review 2/22/2002 BryanWal ok
  266. text.FormatMessage (IDS_CANNOT_SET_EFS_VALUE, lResult);
  267. VERIFY (caption.LoadString (IDS_PUBLIC_KEY_POLICIES_NODE_NAME));
  268. MessageBox (text, caption, MB_OK | MB_ICONWARNING);
  269. return FALSE;
  270. }
  271. }
  272. }
  273. return CHelpPropertyPage::OnApply();
  274. }
  275. void CEFSGeneralPropertyPage::OnTurnOnEfs()
  276. {
  277. SetModified ();
  278. m_bDirty = true;
  279. }