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.

415 lines
13 KiB

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