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.

409 lines
12 KiB

  1. //+---------------------------------------------------------------------------
  2. /////////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Microsoft Windows
  5. // Copyright (C) Microsoft Corporation, 2000-2001.
  6. //
  7. // File: SaferEntryPathPropertyPage.cpp
  8. //
  9. // Contents: Implementation of CSaferEntryPathPropertyPage
  10. //
  11. //----------------------------------------------------------------------------
  12. // SaferEntryPathPropertyPage.cpp : implementation file
  13. //
  14. #include "stdafx.h"
  15. #include <gpedit.h>
  16. #include "certmgr.h"
  17. #include "compdata.h"
  18. #include "SaferEntryPathPropertyPage.h"
  19. #include "SaferUtil.h"
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CSaferEntryPathPropertyPage property page
  27. CSaferEntryPathPropertyPage::CSaferEntryPathPropertyPage(
  28. CSaferEntry& rSaferEntry,
  29. LONG_PTR lNotifyHandle,
  30. LPDATAOBJECT pDataObject,
  31. bool bReadOnly,
  32. bool bNew,
  33. CCertMgrComponentData* pCompData,
  34. bool bIsMachine)
  35. : CHelpPropertyPage(CSaferEntryPathPropertyPage::IDD),
  36. m_rSaferEntry (rSaferEntry),
  37. m_bDirty (bNew),
  38. m_lNotifyHandle (lNotifyHandle),
  39. m_pDataObject (pDataObject),
  40. m_bReadOnly (bReadOnly),
  41. m_pCompData (pCompData),
  42. m_bIsMachine (bIsMachine),
  43. m_bFirst (true),
  44. m_pidl (0),
  45. m_bDialogInitInProgress (false)
  46. {
  47. //{{AFX_DATA_INIT(CSaferEntryPathPropertyPage)
  48. // NOTE: the ClassWizard will add member initialization here
  49. //}}AFX_DATA_INIT
  50. m_rSaferEntry.AddRef ();
  51. m_rSaferEntry.IncrementOpenPageCount ();
  52. }
  53. CSaferEntryPathPropertyPage::~CSaferEntryPathPropertyPage()
  54. {
  55. if ( m_lNotifyHandle )
  56. MMCFreeNotifyHandle (m_lNotifyHandle);
  57. m_rSaferEntry.DecrementOpenPageCount ();
  58. m_rSaferEntry.Release ();
  59. if ( m_pidl )
  60. {
  61. LPMALLOC pMalloc = 0;
  62. if ( SUCCEEDED (SHGetMalloc (&pMalloc)) )
  63. {
  64. pMalloc->Free (m_pidl);
  65. pMalloc->Release ();
  66. }
  67. }
  68. }
  69. void CSaferEntryPathPropertyPage::DoDataExchange(CDataExchange* pDX)
  70. {
  71. CHelpPropertyPage::DoDataExchange(pDX);
  72. //{{AFX_DATA_MAP(CSaferEntryPathPropertyPage)
  73. DDX_Control(pDX, IDC_PATH_ENTRY_DESCRIPTION, m_descriptionEdit);
  74. DDX_Control(pDX, IDC_PATH_ENTRY_PATH, m_pathEdit);
  75. DDX_Control(pDX, IDC_PATH_ENTRY_SECURITY_LEVEL, m_securityLevelCombo);
  76. //}}AFX_DATA_MAP
  77. }
  78. BEGIN_MESSAGE_MAP(CSaferEntryPathPropertyPage, CHelpPropertyPage)
  79. //{{AFX_MSG_MAP(CSaferEntryPathPropertyPage)
  80. ON_EN_CHANGE(IDC_PATH_ENTRY_DESCRIPTION, OnChangePathEntryDescription)
  81. ON_CBN_SELCHANGE(IDC_PATH_ENTRY_SECURITY_LEVEL, OnSelchangePathEntrySecurityLevel)
  82. ON_EN_CHANGE(IDC_PATH_ENTRY_PATH, OnChangePathEntryPath)
  83. ON_BN_CLICKED(IDC_PATH_ENTRY_BROWSE, OnPathEntryBrowse)
  84. ON_EN_SETFOCUS(IDC_PATH_ENTRY_PATH, OnSetfocusPathEntryPath)
  85. //}}AFX_MSG_MAP
  86. END_MESSAGE_MAP()
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CSaferEntryPathPropertyPage message handlers
  89. void CSaferEntryPathPropertyPage::DoContextHelp (HWND hWndControl)
  90. {
  91. _TRACE (1, L"Entering CSaferEntryPathPropertyPage::DoContextHelp\n");
  92. static const DWORD help_map[] =
  93. {
  94. IDC_PATH_ENTRY_PATH, IDH_PATH_ENTRY_PATH,
  95. IDC_PATH_ENTRY_SECURITY_LEVEL, IDH_PATH_ENTRY_SECURITY_LEVEL,
  96. IDC_PATH_ENTRY_DESCRIPTION, IDH_PATH_ENTRY_DESCRIPTION,
  97. IDC_PATH_ENTRY_LAST_MODIFIED, IDH_PATH_ENTRY_LAST_MODIFIED,
  98. IDC_PATH_ENTRY_BROWSE, IDH_PATH_ENTRY_BROWSE_FOLDER,
  99. 0, 0
  100. };
  101. switch (::GetDlgCtrlID (hWndControl))
  102. {
  103. case IDC_PATH_ENTRY_PATH:
  104. case IDC_PATH_ENTRY_SECURITY_LEVEL:
  105. case IDC_PATH_ENTRY_DESCRIPTION:
  106. case IDC_PATH_ENTRY_LAST_MODIFIED:
  107. case IDC_PATH_ENTRY_BROWSE:
  108. if ( !::WinHelp (
  109. hWndControl,
  110. GetF1HelpFilename(),
  111. HELP_WM_HELP,
  112. (DWORD_PTR) help_map) )
  113. {
  114. _TRACE (0, L"WinHelp () failed: 0x%x\n", GetLastError ());
  115. }
  116. break;
  117. default:
  118. break;
  119. }
  120. _TRACE (-1, L"Leaving CSaferEntryPathPropertyPage::DoContextHelp\n");
  121. }
  122. BOOL CSaferEntryPathPropertyPage::OnInitDialog()
  123. {
  124. CHelpPropertyPage::OnInitDialog();
  125. m_bDialogInitInProgress = true;
  126. if ( m_bDirty ) // bNew
  127. {
  128. SetDlgItemText (IDC_DATE_LAST_MODIFIED_LABEL, L"");
  129. }
  130. else
  131. {
  132. CString szText;
  133. VERIFY (szText.LoadString (IDS_PATH_TITLE));
  134. SetDlgItemText (IDC_PATH_TITLE, szText);
  135. }
  136. ASSERT (m_pCompData);
  137. if ( m_pCompData )
  138. {
  139. CPolicyKey policyKey (m_pCompData->m_pGPEInformation,
  140. SAFER_HKLM_REGBASE,
  141. m_bIsMachine);
  142. InitializeSecurityLevelComboBox (m_securityLevelCombo, false,
  143. m_rSaferEntry.GetLevel (), policyKey.GetKey (),
  144. m_pCompData->m_pdwSaferLevels,
  145. m_bIsMachine);
  146. // Initialize path
  147. m_pathEdit.SetWindowText (m_rSaferEntry.GetPath ());
  148. // Initialize description
  149. m_descriptionEdit.LimitText (SAFER_MAX_DESCRIPTION_SIZE);
  150. m_descriptionEdit.SetWindowText (m_rSaferEntry.GetDescription ());
  151. SetDlgItemText (IDC_PATH_ENTRY_LAST_MODIFIED,
  152. m_rSaferEntry.GetLongLastModified ());
  153. if ( m_bReadOnly )
  154. {
  155. m_pathEdit.SetReadOnly ();
  156. m_descriptionEdit.SetReadOnly ();
  157. m_securityLevelCombo.EnableWindow (FALSE);
  158. GetDlgItem (IDC_PATH_ENTRY_BROWSE)->EnableWindow (FALSE);
  159. }
  160. }
  161. m_bDialogInitInProgress = false;
  162. return TRUE; // return TRUE unless you set the focus to a control
  163. // EXCEPTION: OCX Property Pages should return FALSE
  164. }
  165. BOOL CSaferEntryPathPropertyPage::OnApply()
  166. {
  167. if ( m_bDirty && !m_bReadOnly )
  168. {
  169. if ( !ValidateEntryPath () )
  170. return FALSE;
  171. // Set the level
  172. int nCurSel = m_securityLevelCombo.GetCurSel ();
  173. ASSERT (CB_ERR != nCurSel);
  174. m_rSaferEntry.SetLevel ((DWORD) m_securityLevelCombo.GetItemData (nCurSel));
  175. CString szText;
  176. m_pathEdit.GetWindowText (szText);
  177. if ( szText.IsEmpty () )
  178. {
  179. CString text;
  180. CString caption;
  181. CThemeContextActivator activator;
  182. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  183. VERIFY (text.LoadString (IDS_SAFER_PATH_EMPTY));
  184. MessageBox (text, caption, MB_OK);
  185. m_pathEdit.SetFocus ();
  186. return FALSE;
  187. }
  188. m_rSaferEntry.SetPath (szText);
  189. m_descriptionEdit.GetWindowText (szText);
  190. m_rSaferEntry.SetDescription (szText);
  191. HRESULT hr = m_rSaferEntry.Save ();
  192. if ( SUCCEEDED (hr) )
  193. {
  194. if ( m_lNotifyHandle )
  195. MMCPropertyChangeNotify (
  196. m_lNotifyHandle, // handle to a notification
  197. (LPARAM) m_pDataObject); // unique identifier
  198. m_bDirty = false;
  199. }
  200. else
  201. {
  202. CString text;
  203. CString caption;
  204. CThemeContextActivator activator;
  205. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  206. text.FormatMessage (IDS_ERROR_SAVING_ENTRY, GetSystemMessage (hr));
  207. MessageBox (text, caption, MB_OK);
  208. return FALSE;
  209. }
  210. }
  211. return CHelpPropertyPage::OnApply();
  212. }
  213. void CSaferEntryPathPropertyPage::OnChangePathEntryDescription()
  214. {
  215. if ( !m_bDialogInitInProgress )
  216. {
  217. m_bDirty = true;
  218. SetModified ();
  219. }
  220. }
  221. void CSaferEntryPathPropertyPage::OnSelchangePathEntrySecurityLevel()
  222. {
  223. if ( !m_bDialogInitInProgress )
  224. {
  225. m_bDirty = true;
  226. SetModified ();
  227. }
  228. }
  229. void CSaferEntryPathPropertyPage::OnChangePathEntryPath()
  230. {
  231. if ( !m_bDialogInitInProgress )
  232. {
  233. m_bDirty = true;
  234. SetModified ();
  235. }
  236. }
  237. //+--------------------------------------------------------------------------
  238. //
  239. // Function: BrowseCallbackProc
  240. //
  241. // Synopsis: Callback procedure for File & Folder adding SHBrowseForFolder
  242. // to set the title bar appropriately
  243. //
  244. // Arguments: [hwnd] - the hwnd of the browse dialog
  245. // [uMsg] - the message from the dialog
  246. // [lParam] - message dependant
  247. // [pData] - PIDL from last successful call to SHBrowseForFolder
  248. //
  249. // Returns: 0
  250. //
  251. //---------------------------------------------------------------------------
  252. int CSaferEntryPathPropertyPage::BrowseCallbackProc (HWND hwnd, UINT uMsg, LPARAM /*lParam*/, LPARAM pData)
  253. {
  254. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  255. switch(uMsg)
  256. {
  257. case BFFM_INITIALIZED:
  258. {
  259. CString szTitle;
  260. VERIFY (szTitle.LoadString (IDS_SHBROWSEFORFOLDER_TITLE));
  261. ::SetWindowText (hwnd, szTitle);
  262. if ( pData )
  263. ::SendMessage (hwnd, BFFM_SETSELECTION, FALSE, pData);
  264. }
  265. break;
  266. default:
  267. break;
  268. }
  269. return 0;
  270. }
  271. void CSaferEntryPathPropertyPage::OnPathEntryBrowse()
  272. {
  273. _TRACE (1, L"Entering CSaferEntryPathPropertyPage::OnPathEntryBrowse()\n");
  274. CString szTitle;
  275. VERIFY (szTitle.LoadString (IDS_SELECT_A_FOLDER));
  276. WCHAR szDisplayName[MAX_PATH];
  277. BROWSEINFO bi;
  278. ::ZeroMemory (&bi, sizeof (BROWSEINFO));
  279. bi.hwndOwner = m_hWnd;
  280. bi.pidlRoot = 0;
  281. bi.pszDisplayName = szDisplayName;
  282. bi.lpszTitle = szTitle;
  283. bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_VALIDATE | BIF_BROWSEINCLUDEFILES;
  284. bi.lpfn = BrowseCallbackProc;
  285. bi.lParam = (LPARAM) m_pidl;
  286. bi.iImage = 0;
  287. LPITEMIDLIST pidl = SHBrowseForFolder (&bi);
  288. if ( pidl )
  289. {
  290. CString szFolderPath;
  291. BOOL bRVal = SHGetPathFromIDList (pidl, szFolderPath.GetBuffer (MAX_PATH));
  292. szFolderPath.ReleaseBuffer();
  293. if ( bRVal )
  294. {
  295. LPMALLOC pMalloc = 0;
  296. if ( SUCCEEDED (SHGetMalloc (&pMalloc)) )
  297. {
  298. if ( m_pidl )
  299. pMalloc->Free (m_pidl);
  300. pMalloc->Release ();
  301. m_pidl = pidl;
  302. }
  303. m_pathEdit.SetWindowText (szFolderPath);
  304. m_bDirty = true;
  305. SetModified ();
  306. }
  307. }
  308. _TRACE (-1, L"Leaving CSaferEntryPathPropertyPage::OnPathEntryBrowse()\n");
  309. }
  310. // NTRAID# 310880 SAFER: New Path Rule property sheet should not reject
  311. // wildcard characters * and ?
  312. #define ILLEGAL_FAT_CHARS L"\"+,;<=>[]|"
  313. bool CSaferEntryPathPropertyPage::ValidateEntryPath()
  314. {
  315. bool bRVal = true;
  316. CString szPath;
  317. m_pathEdit.GetWindowText (szPath);
  318. PCWSTR szInvalidCharSet = ILLEGAL_FAT_CHARS;
  319. if ( -1 != szPath.FindOneOf (szInvalidCharSet) )
  320. {
  321. bRVal = false;
  322. CString text;
  323. CString caption;
  324. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  325. CString charsWithSpaces;
  326. UINT nIndex = 0;
  327. while (szInvalidCharSet[nIndex])
  328. {
  329. charsWithSpaces += szInvalidCharSet[nIndex];
  330. charsWithSpaces += L" ";
  331. nIndex++;
  332. }
  333. text.FormatMessage (IDS_SAFER_PATH_CONTAINS_INVALID_CHARS, charsWithSpaces);
  334. CThemeContextActivator activator;
  335. MessageBox (text, caption, MB_OK);
  336. m_pathEdit.SetFocus ();
  337. }
  338. return bRVal;
  339. }
  340. void CSaferEntryPathPropertyPage::OnSetfocusPathEntryPath()
  341. {
  342. if ( m_bFirst )
  343. {
  344. if ( true == m_bReadOnly )
  345. SendDlgItemMessage (IDC_PATH_ENTRY_PATH, EM_SETSEL, (WPARAM) 0, 0);
  346. m_bFirst = false;
  347. }
  348. }