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.

1336 lines
42 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2001.
  5. //
  6. // File: spolitem.cpp
  7. //
  8. // Contents: WiF Policy Snapin
  9. //
  10. //
  11. // History: TaroonM
  12. // 10/30/01
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "stdafx.h"
  16. #include <htmlhelp.h>
  17. #include "sprpage.h"
  18. #include "new.h"
  19. #include "genpage.h"
  20. #ifdef WIZ97WIZARDS
  21. #include "wiz97run.h"
  22. #endif
  23. const TCHAR c_szPolicyAgentServiceName[] = _T("PolicyAgent");
  24. #define SERVICE_CONTROL_NEW_POLICY 129
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. #define celems(_x) (sizeof(_x) / sizeof(_x[0]))
  31. DWORD
  32. ComputePolicyDN(
  33. LPWSTR pszDirDomainName,
  34. GUID PolicyIdentifier,
  35. LPWSTR pszPolicyDN
  36. );
  37. // Construction/destruction
  38. CSecPolItem::CSecPolItem () :
  39. m_pDisplayInfo( NULL ),
  40. m_nResultSelected( -1 ),
  41. m_bWiz97On( FALSE ),
  42. m_bBlockDSDelete( FALSE ),
  43. m_bItemSelected( FALSE )
  44. {
  45. m_pPolicy = NULL;
  46. m_bNewPol = FALSE;
  47. ZeroMemory( &m_ResultItem, sizeof( RESULTDATAITEM ) );
  48. }
  49. CSecPolItem::~CSecPolItem()
  50. {
  51. if (m_pDisplayInfo != NULL)
  52. {
  53. delete m_pDisplayInfo;
  54. m_pDisplayInfo = NULL;
  55. }
  56. if (m_pPolicy)
  57. {
  58. FreeWirelessPolicyData(m_pPolicy);
  59. }
  60. };
  61. void CSecPolItem::Initialize (WIRELESS_POLICY_DATA *pPolicy, CComponentDataImpl* pComponentDataImpl, CComponentImpl* pComponentImpl, BOOL bTemporaryDSObject)
  62. {
  63. PWIRELESS_POLICY_DATA pWirelessPolicyData = NULL;
  64. HANDLE hLocalPolicyStore = NULL;
  65. // call base class initialize
  66. CSnapObject::Initialize( pComponentDataImpl, pComponentImpl, bTemporaryDSObject );
  67. ZeroMemory( &m_ResultItem, sizeof( RESULTDATAITEM ) );
  68. m_bNewPol = bTemporaryDSObject;
  69. if (m_pPolicy)
  70. {
  71. FreeWirelessPolicyData(m_pPolicy);
  72. }
  73. m_pPolicy = pPolicy;
  74. if (m_pPolicy) {
  75. m_strName = pPolicy->pszWirelessName;
  76. }
  77. // Set default resultItem settings
  78. GetResultItem()->mask = RDI_STR | RDI_IMAGE;
  79. GetResultItem()->str = MMC_CALLBACK;
  80. // Set the image. Active items get an image to indicate this state.
  81. BOOL bEnabled = FALSE;
  82. bEnabled = CheckForEnabled();
  83. GetResultItem()->nImage = bEnabled ? ENABLEDSECPOLICY_IMAGE_IDX : SECPOLICY_IMAGE_IDX;
  84. }
  85. //////////////////////////////////////////////////////////////////////////
  86. // handle IExtendContextMenu
  87. STDMETHODIMP CSecPolItem::AddMenuItems
  88. (
  89. LPCONTEXTMENUCALLBACK pContextMenuCallback,
  90. long *pInsertionAllowed
  91. )
  92. {
  93. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  94. HRESULT hr = S_OK;
  95. CONTEXTMENUITEM mItem;
  96. LONG lCommandID;
  97. // only add these menu items if we are pointed to the local machine
  98. if ((m_pComponentDataImpl->EnumLocation()==LOCATION_REMOTE)
  99. || (m_pComponentDataImpl->EnumLocation()==LOCATION_LOCAL)
  100. // extension snapin?
  101. || ((m_pComponentDataImpl->EnumLocation()==LOCATION_GLOBAL) && (NULL != m_pComponentDataImpl->GetStaticScopeObject()->GetExtScopeObject())))
  102. {
  103. // getthe active/inactive strings
  104. CString strMenuText;
  105. CString strMenuDescription;
  106. /*
  107. if (CheckForEnabled ())
  108. {
  109. strMenuText.LoadString (IDS_MENUTEXT_UNASSIGN);
  110. strMenuDescription.LoadString (IDS_MENUDESCRIPTION_UNASSIGN);
  111. lCommandID = IDM_UNASSIGN;
  112. } else
  113. {
  114. strMenuText.LoadString (IDS_MENUTEXT_ASSIGN);
  115. strMenuDescription.LoadString (IDS_MENUDESCRIPTION_ASSIGN);
  116. lCommandID = IDM_ASSIGN;
  117. }
  118. // see if we can insert into the top
  119. if (*pInsertionAllowed & CCM_INSERTIONALLOWED_TOP)
  120. {
  121. // set active/inactive
  122. CONFIGUREITEM (mItem, strMenuText, strMenuDescription, lCommandID, CCM_INSERTIONPOINTID_PRIMARY_TOP, 0, 0);
  123. hr = pContextMenuCallback->AddItem(&mItem);
  124. ASSERT(hr == S_OK);
  125. }
  126. // see if we can insert into the tasks
  127. if (*pInsertionAllowed & CCM_INSERTIONALLOWED_TASK)
  128. {
  129. // set active/inactive
  130. CONFIGUREITEM (mItem, strMenuText, strMenuDescription, lCommandID, CCM_INSERTIONPOINTID_PRIMARY_TASK, 0, 0);
  131. hr = pContextMenuCallback->AddItem(&mItem);
  132. ASSERT(hr == S_OK);
  133. }
  134. */
  135. }
  136. // we are done
  137. return hr;
  138. }
  139. STDMETHODIMP CSecPolItem::Command
  140. (
  141. long lCommandID,
  142. IConsoleNameSpace* // not used for result items
  143. )
  144. {
  145. WCHAR szMachinePath[256];
  146. HRESULT hr = S_OK;
  147. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  148. return CWirelessSnapInDataObjectImpl<CSecPolItem>::Command( lCommandID, (IConsoleNameSpace*)NULL );
  149. // we handled it
  150. return S_OK;
  151. }
  152. HRESULT CSecPolItem::IsPolicyExist()
  153. {
  154. HRESULT hr = S_OK;
  155. PWIRELESS_POLICY_DATA pWirelessPolicyData = NULL;
  156. HANDLE hPolicyStore = NULL;
  157. PWIRELESS_PS_DATA * ppWirelessPSData = NULL;
  158. DWORD dwNumPSObjects = 0;
  159. hPolicyStore = m_pComponentDataImpl->GetPolicyStoreHandle();
  160. pWirelessPolicyData = GetWirelessPolicy();
  161. return hr;
  162. }
  163. // handle IExtendPropertySheet
  164. STDMETHODIMP CSecPolItem::CreatePropertyPages
  165. (
  166. LPPROPERTYSHEETCALLBACK lpProvider,
  167. LONG_PTR handle
  168. )
  169. {
  170. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  171. HRESULT hr = S_OK;
  172. ASSERT(lpProvider != NULL);
  173. // save this notification handle
  174. SetNotificationHandle (handle);
  175. //check if the policy exists before pops up the property page
  176. //may be deleted by some other instance, if so return FALSE, force the refresh
  177. if ( !m_bNewPol )
  178. {
  179. hr = IsPolicyExist();
  180. if ( FAILED(hr) )
  181. {
  182. ReportError(IDS_LOAD_ERROR, hr);
  183. // trigger a refresh
  184. m_pComponentDataImpl->GetConsole()->UpdateAllViews( this, 0,0 );
  185. return hr;
  186. }
  187. }
  188. #ifdef WIZ97WIZARDS
  189. if (m_bWiz97On)
  190. {
  191. // We only run the wizard in the case of a new object, if that changes we will need to
  192. // verify we are doing an 'add' here
  193. // IF the wizard wants to it will associate these two
  194. PWIRELESS_PS_DATA pWirelessPSData = NULL;
  195. ASSERT(GetWirelessPolicy());
  196. ASSERT(GetWirelessPolicy()->ppWirelessPSData);
  197. /*
  198. pWirelessPSData = *GetWirelessPolicy()->ppWirelessPSData;
  199. if (pWirelessPSData)
  200. {
  201. */
  202. HRESULT hr = CreateSecPolItemWiz97PropertyPages(dynamic_cast<CComObject<CSecPolItem>*>(this), pWirelessPSData, lpProvider);
  203. // the wizard should have done an addref on the pWirelessPSData pointer we just passed into it, so
  204. // it so we can feel free to releaseref now
  205. /*
  206. } else
  207. {
  208. // we don't want to save the notification handle after all
  209. SetNotificationHandle (NULL);
  210. hr = E_UNEXPECTED;
  211. }
  212. */
  213. return hr;
  214. } else
  215. {
  216. #endif
  217. CComPtr<CSecPolPropSheetManager> spPropshtManager =
  218. new CComObject<CSecPolPropSheetManager>;
  219. if (NULL == spPropshtManager.p)
  220. {
  221. ReportError(IDS_OPERATION_FAIL, E_OUTOFMEMORY);
  222. return E_OUTOFMEMORY;
  223. }
  224. // Create the property page(s); gets deleted when the window is destroyed
  225. CGenPage* pGenPage = new CGenPage(IDD_WIRELESSGENPROP);
  226. CSecPolRulesPage* pRulesPage = new CSecPolRulesPage();
  227. if ((pRulesPage == NULL) || (pGenPage == NULL))
  228. return E_UNEXPECTED;
  229. // if the first page can't initialize this probably means that there was a problem
  230. // talking to the DS, in which case we fail to bring up the propery pages and figure
  231. // that a refresh will get us back to a valid state
  232. hr = pGenPage->Initialize (dynamic_cast<CComObject<CSecPolItem>*>(this));
  233. if (hr != S_OK)
  234. {
  235. // since we are not going to display the tab dialog we need to clean up
  236. delete pGenPage;
  237. delete pRulesPage;
  238. // we don't want to save the notification handle after all
  239. SetNotificationHandle (NULL);
  240. // trigger a refresh
  241. m_pComponentDataImpl->GetConsole()->UpdateAllViews( this, 0,0 );
  242. return hr;
  243. }
  244. // yes, we ignore the return value on these guys, since the only return value that
  245. // can currently come back would have come back on the first guy anyway
  246. pRulesPage->Initialize (dynamic_cast<CComObject<CSecPolItem>*>(this));
  247. HPROPSHEETPAGE hGenPage = MyCreatePropertySheetPage(&(pGenPage->m_psp));
  248. HPROPSHEETPAGE hRulesPage = MyCreatePropertySheetPage(&(pRulesPage->m_psp));
  249. if ((hGenPage == NULL) || (hRulesPage == NULL))
  250. {
  251. // we don't want to save the notification handle after all
  252. SetNotificationHandle (NULL);
  253. return E_UNEXPECTED;
  254. }
  255. lpProvider->AddPage(hGenPage);
  256. lpProvider->AddPage(hRulesPage);
  257. spPropshtManager->Initialize(dynamic_cast<CComObject<CSecPolItem>*>(this));
  258. spPropshtManager->AddPage(pRulesPage);
  259. spPropshtManager->AddPage(pGenPage);
  260. return S_OK;
  261. #ifdef WIZ97WIZARDS
  262. }
  263. #endif
  264. }
  265. STDMETHODIMP CSecPolItem::QueryPagesFor( void )
  266. {
  267. // display our locations dialog via this
  268. return S_OK;
  269. }
  270. // Destroy helper
  271. STDMETHODIMP CSecPolItem::Destroy( void )
  272. {
  273. // just return success
  274. return S_OK;
  275. }
  276. // handle IComponent and IComponentData
  277. STDMETHODIMP CSecPolItem::Notify
  278. (
  279. MMC_NOTIFY_TYPE event,
  280. LPARAM arg,
  281. LPARAM param,
  282. BOOL bComponentData, // TRUE when caller is IComponentData
  283. IConsole *pConsole,
  284. IHeaderCtrl *pHeader
  285. )
  286. {
  287. #ifdef DO_TRACE
  288. OPT_TRACE(_T("CSecPolItem::Notify this-%p "), this);
  289. switch (event)
  290. {
  291. case MMCN_ACTIVATE:
  292. OPT_TRACE(_T("MMCN_ACTIVATE\n"));
  293. break;
  294. case MMCN_ADD_IMAGES:
  295. OPT_TRACE(_T("MMCN_ADD_IMAGES\n"));
  296. break;
  297. case MMCN_BTN_CLICK:
  298. OPT_TRACE(_T("MMCN_BTN_CLICK\n"));
  299. break;
  300. case MMCN_CLICK:
  301. OPT_TRACE(_T("MMCN_CLICK\n"));
  302. break;
  303. case MMCN_COLUMN_CLICK:
  304. OPT_TRACE(_T("MMCN_COLUMN_CLICK\n"));
  305. break;
  306. case MMCN_CONTEXTMENU:
  307. OPT_TRACE(_T("MMCN_CONTEXTMENU\n"));
  308. break;
  309. case MMCN_CUTORMOVE:
  310. OPT_TRACE(_T("MMCN_CUTORMOVE\n"));
  311. break;
  312. case MMCN_DBLCLICK:
  313. OPT_TRACE(_T("MMCN_DBLCLICK\n"));
  314. break;
  315. case MMCN_DELETE:
  316. OPT_TRACE(_T("MMCN_DELETE\n"));
  317. break;
  318. case MMCN_DESELECT_ALL:
  319. OPT_TRACE(_T("MMCN_DESELECT_ALL\n"));
  320. break;
  321. case MMCN_EXPAND:
  322. OPT_TRACE(_T("MMCN_EXPAND\n"));
  323. break;
  324. case MMCN_HELP:
  325. OPT_TRACE(_T("MMCN_HELP\n"));
  326. break;
  327. case MMCN_MENU_BTNCLICK:
  328. OPT_TRACE(_T("MMCN_MENU_BTNCLICK\n"));
  329. break;
  330. case MMCN_MINIMIZED:
  331. OPT_TRACE(_T("MMCN_MINIMIZED\n"));
  332. break;
  333. case MMCN_PASTE:
  334. OPT_TRACE(_T("MMCN_PASTE\n"));
  335. break;
  336. case MMCN_PROPERTY_CHANGE:
  337. OPT_TRACE(_T("MMCN_PROPERTY_CHANGE\n"));
  338. break;
  339. case MMCN_QUERY_PASTE:
  340. OPT_TRACE(_T("MMCN_QUERY_PASTE\n"));
  341. break;
  342. case MMCN_REFRESH:
  343. OPT_TRACE(_T("MMCN_REFRESH\n"));
  344. break;
  345. case MMCN_REMOVE_CHILDREN:
  346. OPT_TRACE(_T("MMCN_REMOVE_CHILDREN\n"));
  347. break;
  348. case MMCN_RENAME:
  349. OPT_TRACE(_T("MMCN_RENAME\n"));
  350. break;
  351. case MMCN_SELECT:
  352. OPT_TRACE(_T("MMCN_SELECT\n"));
  353. break;
  354. case MMCN_SHOW:
  355. OPT_TRACE(_T("MMCN_SHOW\n"));
  356. break;
  357. case MMCN_VIEW_CHANGE:
  358. OPT_TRACE(_T("MMCN_VIEW_CHANGE\n"));
  359. break;
  360. case MMCN_SNAPINHELP:
  361. OPT_TRACE(_T("MMCN_SNAPINHELP\n"));
  362. break;
  363. case MMCN_CONTEXTHELP:
  364. OPT_TRACE(_T("MMCN_CONTEXTHELP\n"));
  365. break;
  366. case MMCN_INITOCX:
  367. OPT_TRACE(_T("MMCN_INITOCX\n"));
  368. break;
  369. case MMCN_FILTER_CHANGE:
  370. OPT_TRACE(_T("MMCN_FILTER_CHANGE\n"));
  371. break;
  372. default:
  373. OPT_TRACE(_T("Unknown event\n"));
  374. break;
  375. }
  376. #endif //#ifdef DO_TRACE
  377. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  378. // if didn't handle something... then return FALSE
  379. HRESULT hr = S_FALSE;
  380. // handle the event
  381. switch(event)
  382. {
  383. case MMCN_CONTEXTHELP:
  384. {
  385. CComQIPtr <IDisplayHelp, &IID_IDisplayHelp> pDisplayHelp ( pConsole );
  386. ASSERT( pDisplayHelp != NULL );
  387. if (pDisplayHelp)
  388. {
  389. // need to form a complete path to the .chm file
  390. CString s, s2;
  391. s.LoadString(IDS_HELPCONCEPTSFILE);
  392. DWORD dw = ExpandEnvironmentStrings (s, s2.GetBuffer (512), 512);
  393. s2.ReleaseBuffer (-1);
  394. if ((dw == 0) || (dw > 512))
  395. {
  396. return E_UNEXPECTED;
  397. }
  398. pDisplayHelp->ShowTopic(s2.GetBuffer(512));
  399. s2.ReleaseBuffer (-1);
  400. hr = S_OK;
  401. }
  402. break;
  403. }
  404. case MMCN_SELECT:
  405. {
  406. // Obtain IConsoleVerb from console
  407. CComPtr<IConsoleVerb> spVerb;
  408. pConsole->QueryConsoleVerb( &spVerb );
  409. m_bItemSelected = !!(HIWORD(arg));
  410. // call object to set verb state
  411. AdjustVerbState( (IConsoleVerb*)spVerb );
  412. // Remember selected result item
  413. CComQIPtr <IResultData, &IID_IResultData> spResult( pConsole );
  414. if (spResult == NULL)
  415. {
  416. TRACE(_T("CComponentDataImpl::Notify QI for IResultData FAILED\n"));
  417. break;
  418. }
  419. hr = OnSelect( arg, param, (IResultData*)spResult);
  420. break;
  421. }
  422. case MMCN_PROPERTY_CHANGE:
  423. {
  424. // the object pointer should be in lParam
  425. OnPropertyChange( param, pConsole );
  426. // This message is received whenever the property sheet is dismissed.
  427. // Now is a good time to make sure the result item which was originally
  428. // selected remains so.
  429. CComQIPtr <IResultData, &IID_IResultData> spResult( pConsole );
  430. if (spResult == NULL)
  431. {
  432. TRACE(_T("CComponentDataImpl::Notify QI for IResultData FAILED\n"));
  433. break;
  434. }
  435. SelectResult( (IResultData*)spResult );
  436. break;
  437. }
  438. case MMCN_VIEW_CHANGE:
  439. {
  440. // Refresh the entire result pane if view has changed.
  441. hr = pConsole->UpdateAllViews( m_pComponentDataImpl->GetStaticScopeObject(), 0, 0 );
  442. break;
  443. }
  444. case MMCN_RENAME:
  445. {
  446. hr = OnRename( arg, param );
  447. // even if the rename failed mmc will still display with the
  448. // new name... thus we have to force a refresh in the failure case
  449. if (hr != S_OK)
  450. {
  451. if (S_FALSE == hr)
  452. {
  453. CThemeContextActivator activator;
  454. AfxMessageBox(IDS_ERROR_EMPTY_POL_NAME);
  455. }
  456. else
  457. {
  458. ReportError(IDS_SAVE_ERROR, hr);
  459. hr = S_FALSE;
  460. }
  461. }
  462. break;
  463. }
  464. case MMCN_DELETE:
  465. {
  466. CThemeContextActivator activator;
  467. // delete the item
  468. if (AfxMessageBox (IDS_SUREYESNO, MB_YESNO | MB_DEFBUTTON2) == IDYES)
  469. {
  470. // turn on wait cursor
  471. CWaitCursor waitCursor;
  472. // Obtain IResultData
  473. CComQIPtr <IResultData, &IID_IResultData> pResultData( pConsole );
  474. ASSERT( pResultData != NULL );
  475. // param is not used on MMCN_DELETE, replace it with IResultData*
  476. hr = OnDelete( arg, (LPARAM)(IResultData*)pResultData );
  477. if (hr != S_OK)
  478. {
  479. ReportError(IDS_SAVE_ERROR, hr);
  480. hr = S_FALSE;
  481. }
  482. }
  483. else
  484. hr = S_FALSE; // tell IComponent the delete wasn't done.
  485. break;
  486. }
  487. // we didn't handle it... do default behaviour
  488. case MMCN_DBLCLICK:
  489. {
  490. PWIRELESS_POLICY_DATA pWirelessPolicyData = NULL;
  491. pWirelessPolicyData = GetWirelessPolicy();
  492. break;
  493. }
  494. case MMCN_ACTIVATE:
  495. case MMCN_MINIMIZED:
  496. case MMCN_BTN_CLICK:
  497. default:
  498. break;
  499. }
  500. return hr;
  501. }
  502. HRESULT GetGpoDisplayName(WCHAR *szGpoId, WCHAR *pszGpoName, DWORD dwSize )
  503. {
  504. LPGROUPPOLICYOBJECT pGPO = NULL; //Group Policy Object
  505. HRESULT hr = S_OK; //result returned by functions
  506. //
  507. // Create an IGroupPolicyObject instance to work with
  508. //
  509. hr = CoCreateInstance(CLSID_GroupPolicyObject, NULL, CLSCTX_SERVER, IID_IGroupPolicyObject, (void **)&pGPO);
  510. if (FAILED(hr))
  511. {
  512. return hr;
  513. }
  514. hr = pGPO->OpenDSGPO((LPOLESTR)szGpoId,GPO_OPEN_READ_ONLY);
  515. if (FAILED(hr))
  516. {
  517. pGPO->Release();
  518. return hr;
  519. }
  520. hr = pGPO->GetDisplayName( pszGpoName,
  521. dwSize
  522. );
  523. if (FAILED(hr))
  524. {
  525. pGPO->Release();
  526. return hr;
  527. }
  528. pGPO->Release();
  529. return hr;
  530. }
  531. // handle IComponent
  532. STDMETHODIMP CSecPolItem::GetResultDisplayInfo( RESULTDATAITEM *pResultDataItem )
  533. {
  534. TCHAR *temp = NULL;
  535. DWORD dwError = S_OK;
  536. OPT_TRACE(_T("CSecPolItem::GetResultDisplayInfo this-%p\n"), this);
  537. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  538. // are they looking for the image?
  539. if (pResultDataItem->mask & RDI_IMAGE)
  540. {
  541. pResultDataItem->nImage = GetResultItem()->nImage;
  542. OPT_TRACE(_T(" returning image[%i]\n"), GetResultItem()->nImage);
  543. }
  544. // are they looking for a string?
  545. if (pResultDataItem->mask & RDI_STR)
  546. {
  547. if (GetWirelessPolicy() != NULL)
  548. {
  549. switch (pResultDataItem->nCol)
  550. {
  551. case COL_NAME:
  552. {
  553. CString str = m_pPolicy->pszWirelessName;
  554. temp = (TCHAR*) realloc (m_pDisplayInfo, (str.GetLength()+1)*sizeof(TCHAR));
  555. if (temp == NULL)
  556. {
  557. dwError = GetLastError();
  558. } else
  559. {
  560. m_pDisplayInfo = temp;
  561. lstrcpy (m_pDisplayInfo, str.GetBuffer(20));
  562. }
  563. pResultDataItem->str = m_pDisplayInfo;
  564. OPT_TRACE(_T(" returning COL_NAME-%s\n"), m_pDisplayInfo);
  565. }
  566. break;
  567. case COL_DESCRIPTION:
  568. {
  569. CString str = m_pPolicy->pszDescription;
  570. temp = (TCHAR*) realloc (m_pDisplayInfo, (str.GetLength()+1)*sizeof(TCHAR));
  571. if (temp == NULL)
  572. {
  573. dwError = GetLastError();
  574. } else
  575. {
  576. m_pDisplayInfo = temp;
  577. lstrcpy (m_pDisplayInfo, str.GetBuffer(20));
  578. }
  579. pResultDataItem->str = m_pDisplayInfo;
  580. OPT_TRACE(_T(" returning COL_DESCRIPTION-%s\n"), m_pDisplayInfo);
  581. }
  582. break;
  583. default:
  584. {
  585. if ( !m_pComponentDataImpl->IsRsop() )
  586. {
  587. switch( pResultDataItem->nCol )
  588. {
  589. case COL_LAST_MODIFIED:
  590. {
  591. CString strTime;
  592. if(SUCCEEDED(FormatTime((time_t)m_pPolicy->dwWhenChanged, strTime)))
  593. {
  594. temp = (TCHAR*) realloc (m_pDisplayInfo, (strTime.GetLength()+1)*sizeof(TCHAR));
  595. if (temp != NULL) {
  596. m_pDisplayInfo = temp;
  597. lstrcpy(m_pDisplayInfo, strTime);
  598. } else
  599. {
  600. dwError = GetLastError();
  601. }
  602. pResultDataItem->str = m_pDisplayInfo;
  603. OPT_TRACE(_T(" returning COL_ACTIVE-%s\n"), m_pDisplayInfo);
  604. }
  605. }
  606. break;
  607. default:
  608. // for debugging
  609. ASSERT (0);
  610. break;
  611. } //inner switch
  612. } //if ( !m_pComponentDataImpl->IsRsop() )
  613. else
  614. {
  615. //rsop case
  616. switch( pResultDataItem->nCol )
  617. {
  618. case COL_GPONAME:
  619. if ( m_pPolicy->pRsopInfo )
  620. {
  621. WCHAR szGpoName[512];
  622. CString strGpoId;
  623. strGpoId.Format(_T("LDAP://%s"),m_pPolicy->pRsopInfo->pszGPOID);
  624. HRESULT hr = GetGpoDisplayName( (WCHAR*)(LPCTSTR)strGpoId, szGpoName, 512 );
  625. if (S_OK == hr )
  626. {
  627. INT iSize = (lstrlen(szGpoName) + 1) * sizeof(WCHAR);
  628. temp = (TCHAR*) realloc (m_pDisplayInfo, iSize);
  629. if (temp!= NULL)
  630. {
  631. m_pDisplayInfo = temp;
  632. lstrcpy(m_pDisplayInfo, szGpoName);
  633. } else
  634. {
  635. dwError = GetLastError();
  636. }
  637. pResultDataItem->str = m_pDisplayInfo;
  638. OPT_TRACE(_T(" returning COL_ACTIVE-%s\n"), m_pDisplayInfo);
  639. }
  640. }
  641. break;
  642. case COL_PRECEDENCE:
  643. if ( m_pPolicy->pRsopInfo )
  644. {
  645. const int cchMaxDigits = 33;
  646. temp = (TCHAR*) realloc (m_pDisplayInfo, cchMaxDigits * sizeof(TCHAR));
  647. if (temp!=NULL) {
  648. m_pDisplayInfo = temp;
  649. wsprintf(m_pDisplayInfo, _T("%d"),m_pPolicy->pRsopInfo->uiPrecedence);
  650. } else
  651. {
  652. dwError = GetLastError();
  653. }
  654. pResultDataItem->str = m_pDisplayInfo;
  655. OPT_TRACE(_T(" returning COL_ACTIVE-%s\n"), m_pDisplayInfo);
  656. }
  657. break;
  658. case COL_OU:
  659. if ( m_pPolicy->pRsopInfo )
  660. {
  661. INT iLen = (lstrlen(m_pPolicy->pRsopInfo->pszSOMID) + 1) *sizeof(TCHAR);
  662. temp = (TCHAR*) realloc (m_pDisplayInfo, iLen);
  663. if (temp!=NULL) {
  664. m_pDisplayInfo = temp;
  665. lstrcpy(m_pDisplayInfo, m_pPolicy->pRsopInfo->pszSOMID);
  666. } else
  667. {
  668. dwError = GetLastError();
  669. }
  670. pResultDataItem->str = m_pDisplayInfo;
  671. OPT_TRACE(_T(" returning COL_ACTIVE-%s\n"), m_pDisplayInfo);
  672. }
  673. break;
  674. default:
  675. // for debugging
  676. ASSERT (0);
  677. break;
  678. }//inner switch
  679. }
  680. }//default case
  681. } //outer switch
  682. } //if (GetWirelessPolicy() != NULL)
  683. else
  684. {
  685. CString str;
  686. str.LoadString (IDS_COLUMN_INVALID);
  687. temp = (TCHAR*) realloc (m_pDisplayInfo, (str.GetLength()+1)*sizeof(TCHAR));
  688. if (temp == NULL)
  689. {
  690. dwError = GetLastError();
  691. } else
  692. {
  693. m_pDisplayInfo = temp;
  694. lstrcpy (m_pDisplayInfo, str.GetBuffer(20));
  695. }
  696. pResultDataItem->str = m_pDisplayInfo;
  697. }
  698. }
  699. return HRESULT_FROM_WIN32(dwError);
  700. }
  701. //+---------------------------------------------------------------------------
  702. //
  703. // Member: CAdvIpcfgDlg::FormatTime
  704. //
  705. // Purpose: convert time_t to a string.
  706. //
  707. // Returns: error code
  708. //
  709. // Note: _wasctime has some localization problems. So we do the formatting ourselves
  710. HRESULT CSecPolItem::FormatTime(time_t t, CString & str)
  711. {
  712. time_t timeCurrent = time(NULL);
  713. LONGLONG llTimeDiff = 0;
  714. FILETIME ftCurrent = {0};
  715. FILETIME ftLocal = {0};
  716. SYSTEMTIME SysTime;
  717. WCHAR szBuff[256] = {0};
  718. str = L"";
  719. GetSystemTimeAsFileTime(&ftCurrent);
  720. llTimeDiff = (LONGLONG)t - (LONGLONG)timeCurrent;
  721. llTimeDiff *= 10000000;
  722. *((LONGLONG UNALIGNED64 *)&ftCurrent) += llTimeDiff;
  723. if (!FileTimeToLocalFileTime(&ftCurrent, &ftLocal ))
  724. {
  725. return HRESULT_FROM_WIN32(GetLastError());
  726. }
  727. if (!FileTimeToSystemTime( &ftLocal, &SysTime ))
  728. {
  729. return HRESULT_FROM_WIN32(GetLastError());
  730. }
  731. if (0 == GetDateFormat(LOCALE_USER_DEFAULT,
  732. 0,
  733. &SysTime,
  734. NULL,
  735. szBuff,
  736. celems(szBuff)))
  737. {
  738. return HRESULT_FROM_WIN32(GetLastError());
  739. }
  740. str = szBuff;
  741. str += L" ";
  742. ZeroMemory(szBuff, sizeof(szBuff));
  743. if (0 == GetTimeFormat(LOCALE_USER_DEFAULT,
  744. 0,
  745. &SysTime,
  746. NULL,
  747. szBuff,
  748. celems(szBuff)))
  749. {
  750. return HRESULT_FROM_WIN32(GetLastError());
  751. }
  752. str += szBuff;
  753. return S_OK;
  754. }
  755. // IIWirelessSnapInData
  756. STDMETHODIMP CSecPolItem::GetResultData( RESULTDATAITEM **ppResultDataItem )
  757. {
  758. ASSERT( NULL != ppResultDataItem );
  759. ASSERT( NULL != GetResultItem() );
  760. if (NULL == ppResultDataItem)
  761. return E_INVALIDARG;
  762. *ppResultDataItem = GetResultItem();
  763. return S_OK;
  764. }
  765. STDMETHODIMP CSecPolItem::GetGuidForCompare( GUID *pGuid )
  766. {
  767. ASSERT( NULL != pGuid );
  768. if (NULL == pGuid)
  769. return E_INVALIDARG;
  770. CopyMemory( pGuid, &m_pPolicy->PolicyIdentifier, sizeof( GUID ) );
  771. return S_OK;
  772. }
  773. STDMETHODIMP CSecPolItem::AdjustVerbState (LPCONSOLEVERB pConsoleVerb)
  774. {
  775. HRESULT hr = S_OK;
  776. // pass to base class
  777. hr = CWirelessSnapInDataObjectImpl<CSecPolItem>::AdjustVerbState( pConsoleVerb );
  778. ASSERT (hr == S_OK);
  779. MMC_BUTTON_STATE buttonProperties = (m_bItemSelected) ? ENABLED : HIDDEN;
  780. if ( m_pComponentDataImpl->IsRsop() )
  781. {
  782. PWIRELESS_POLICY_DATA pWirelessPolicyData = NULL;
  783. pWirelessPolicyData = GetWirelessPolicy();
  784. hr = pConsoleVerb->SetVerbState(MMC_VERB_PROPERTIES, buttonProperties, TRUE);
  785. ASSERT (hr == S_OK);
  786. }
  787. else
  788. {
  789. hr = pConsoleVerb->SetVerbState(MMC_VERB_PROPERTIES, buttonProperties, TRUE);
  790. ASSERT (hr == S_OK);
  791. hr = pConsoleVerb->SetVerbState(MMC_VERB_RENAME, ENABLED, TRUE);
  792. ASSERT (hr == S_OK);
  793. hr = pConsoleVerb->SetVerbState(MMC_VERB_DELETE, ENABLED, TRUE);
  794. ASSERT (hr == S_OK);
  795. }
  796. hr = pConsoleVerb->SetDefaultVerb(m_bItemSelected ?
  797. MMC_VERB_PROPERTIES : MMC_VERB_NONE);
  798. ASSERT (hr == S_OK);
  799. return hr;
  800. }
  801. STDMETHODIMP CSecPolItem::DoPropertyChangeHook( void )
  802. {
  803. return DisplaySecPolProperties( m_pPolicy->pszWirelessName, FALSE );
  804. }
  805. ///////////////////////////////////////////////////////////////////////////
  806. STDMETHODIMP CSecPolItem::DisplaySecPolProperties( CString strTitle, BOOL bWiz97On /*= TRUE*/ )
  807. {
  808. HRESULT hr;
  809. // Add a ref for the prop sheet/wizard.
  810. ((CComObject <CSecPolItem>*)this)->AddRef();
  811. // switch from the IComponentDataImpl to pUnk
  812. LPUNKNOWN pUnk = m_pComponentDataImpl->GetUnknown();
  813. // bring up the sheet
  814. #ifdef WIZ97WIZARDS
  815. if (bWiz97On)
  816. {
  817. // because we are creating a new one we need to turn on the wiz97 wizard
  818. // unless it has been overridden
  819. m_bWiz97On = bWiz97On;
  820. hr = m_pComponentDataImpl->m_pPrshtProvider->CreatePropertySheet(
  821. strTitle, FALSE, (LONG_PTR)this, (LPDATAOBJECT)this,
  822. MMC_PSO_NOAPPLYNOW | MMC_PSO_NEWWIZARDTYPE );
  823. } else
  824. {
  825. #endif
  826. hr = m_pComponentDataImpl->m_pPrshtProvider->CreatePropertySheet(
  827. strTitle, TRUE, (LONG_PTR)this, (LPDATAOBJECT)this,
  828. MMC_PSO_NOAPPLYNOW );
  829. #ifdef WIZ97WIZARDS
  830. }
  831. #endif
  832. ASSERT (hr == S_OK);
  833. // TODO: get the mmc team to fix this hack, we shouldn't need to do the FindWindow calls
  834. // NOTE: if there are multiple MDI windows open this will fail
  835. HWND hWnd = NULL;
  836. // (nsun) As of 5/21/99, we no longer need to do this
  837. // hr = m_pComponentDataImpl->GetConsole()->GetMainWindow(&hWnd);
  838. // hWnd = ::FindWindowEx(hWnd, NULL, L"MDIClient", NULL);
  839. // hWnd = ::FindWindowEx(hWnd, NULL, L"MMCChildFrm", NULL);
  840. // hWnd = ::FindWindowEx(hWnd, NULL, L"MMCView", NULL);
  841. // ASSERT(hWnd);
  842. // TODO: need to check return value and call AddExtensionPages if it was successful
  843. hr = m_pComponentDataImpl->m_pPrshtProvider->AddPrimaryPages (pUnk, TRUE, hWnd, TRUE);
  844. ASSERT (hr == S_OK);
  845. // m_dwRef should be at least 3; 2 from MMC, 1 from this function
  846. ((CComObject <CSecPolItem>*)this)->Release();
  847. hr = m_pComponentDataImpl->GetConsole()->GetMainWindow(&hWnd);
  848. ASSERT(hWnd);
  849. // Show() returns 1 if wizard is cancelled, 0 if it finished
  850. hr = m_pComponentDataImpl->m_pPrshtProvider->Show ((LONG_PTR)hWnd, 0);
  851. #ifdef WIZ97WIZARDS
  852. m_bWiz97On = FALSE;
  853. #endif
  854. // Pass prop sheet return code back to caller.
  855. return hr;
  856. }
  857. STDMETHODIMP CSecPolItem::OnDelete (LPARAM arg, LPARAM param) // param == IResultData*
  858. {
  859. HRESULT hr;
  860. // remove the item from the UI
  861. LPRESULTDATA pResultData = (LPRESULTDATA)param;
  862. hr = pResultData->DeleteItem( m_ResultItem.itemID, 0 );
  863. ASSERT(hr == S_OK);
  864. // need to check to see if WE are the current active policy
  865. PWIRELESS_POLICY_DATA pPolicy = GetWirelessPolicy();
  866. ASSERT(pPolicy);
  867. DWORD dwError = 0;
  868. //for machine policy, unassign the policy if the policy to delete is assigned
  869. //for domain policy, we cannot do much here because we have no idea about which group
  870. //units are using the policy
  871. hr = DeleteWirelessPolicy(m_pComponentDataImpl->GetPolicyStoreHandle(), pPolicy);
  872. if (FAILED(hr))
  873. {
  874. return hr;
  875. }
  876. GUID guidClientExt = CLSID_WIRELESSClientEx;
  877. GUID guidSnapin = CLSID_Snapin;
  878. m_pComponentDataImpl->UseGPEInformationInterface()->PolicyChanged (
  879. TRUE,
  880. FALSE,
  881. &guidClientExt,
  882. &guidSnapin
  883. );
  884. // Remove the item from the result list
  885. //m_pComponentDataImpl->GetStaticScopeObject()->RemoveResultItem( (LPDATAOBJECT)this );
  886. // do a refresh of all views, we pass in the scope item to refresh all
  887. m_pComponentDataImpl->GetConsole()->UpdateAllViews( m_pComponentDataImpl->GetStaticScopeObject(), 0, 0 );
  888. // TODO: return value from OnDelete is wrong
  889. return S_OK;
  890. }
  891. STDMETHODIMP CSecPolItem::OnPropertyChange(LPARAM lParam, LPCONSOLE pConsole )
  892. {
  893. // call base class
  894. return CWirelessSnapInDataObjectImpl<CSecPolItem>::OnPropertyChange( lParam, pConsole );
  895. }
  896. STDMETHODIMP CSecPolItem::OnRename( LPARAM arg, LPARAM param )
  897. {
  898. DWORD dwError = 0;
  899. // TODO: what are the valid args for MMCN_RENAME?
  900. if (arg == 0)
  901. return S_OK;
  902. LPOLESTR pszNewName = reinterpret_cast<LPOLESTR>(param);
  903. if (pszNewName == NULL)
  904. return E_INVALIDARG;
  905. CString strTemp = pszNewName;
  906. strTemp.TrimLeft();
  907. strTemp.TrimRight();
  908. if (strTemp.IsEmpty())
  909. {
  910. return S_FALSE;
  911. }
  912. HRESULT hr = S_FALSE;
  913. PWIRELESS_POLICY_DATA pPolicy = GetWirelessPolicy();
  914. if (pPolicy)
  915. {
  916. if (pPolicy->pszOldWirelessName)
  917. FreePolStr(pPolicy->pszOldWirelessName);
  918. if (pPolicy->pszWirelessName) {
  919. pPolicy->pszOldWirelessName = pPolicy->pszWirelessName;
  920. }
  921. pPolicy->pszWirelessName = AllocPolStr(strTemp);
  922. if (NULL == pPolicy->pszWirelessName)
  923. {
  924. CThemeContextActivator activator;
  925. CString strMsg;
  926. strMsg.LoadString(IDS_ERR_OUTOFMEMORY);
  927. AfxMessageBox(strMsg);
  928. return S_FALSE;
  929. }
  930. dwError = WirelessSetPolicyData(
  931. m_pComponentDataImpl->GetPolicyStoreHandle(),
  932. pPolicy
  933. );
  934. if (ERROR_SUCCESS != dwError)
  935. {
  936. ReportError(IDS_SAVE_ERROR, HRESULT_FROM_WIN32(dwError));
  937. return S_FALSE;
  938. }
  939. GUID guidClientExt = CLSID_WIRELESSClientEx;
  940. GUID guidSnapin = CLSID_Snapin;
  941. m_pComponentDataImpl->UseGPEInformationInterface()->PolicyChanged (
  942. TRUE,
  943. TRUE,
  944. &guidClientExt,
  945. &guidSnapin
  946. );
  947. }
  948. return S_OK;
  949. }
  950. ////////////////////////////////////////////////////////////////////////////////
  951. //
  952. // Function: OnSelect
  953. // Description:
  954. // On MMCN_SELECT determine which result item has been selected and
  955. // remember it so we can ensure it remains selected when its property
  956. // sheet (with General and SecPol pages) is dismissed.
  957. //
  958. HRESULT CSecPolItem::OnSelect(LPARAM arg, LPARAM param, IResultData *pResultData )
  959. {
  960. HRESULT hr = S_OK;
  961. BOOL bScope = (BOOL) LOWORD(arg);
  962. BOOL bSelected = (BOOL) HIWORD(arg);
  963. if (!bScope)
  964. {
  965. if (bSelected)
  966. {
  967. // A result item was selected, save its index.
  968. RESULTDATAITEM rdi;
  969. ZeroMemory( &rdi, sizeof( RESULTDATAITEM ) );
  970. rdi.mask = RDI_STATE | RDI_INDEX;
  971. m_nResultSelected = -1;
  972. do
  973. {
  974. hr = pResultData->GetItem( &rdi );
  975. if (hr == S_OK)
  976. {
  977. if (!rdi.bScopeItem &&
  978. rdi.nState & LVIS_FOCUSED && rdi.nState & LVIS_SELECTED)
  979. {
  980. OPT_TRACE( _T("CComponentImpl::OnSelect GetItem index-%i ID-%i\n"), rdi.nIndex, rdi.itemID );
  981. m_nResultSelected = rdi.nIndex;
  982. ASSERT( -1 != m_nResultSelected );
  983. break;
  984. }
  985. rdi.nIndex++;
  986. rdi.nState = 0;
  987. }
  988. } while ((S_OK == hr) && (rdi.nIndex >= 0));
  989. }
  990. }
  991. else
  992. // A scope item was selected
  993. m_nResultSelected = -1;
  994. return hr;
  995. }
  996. // Function: SelectResult
  997. // Description:
  998. // Select the result item indexed by m_nResultSelected when the index
  999. // is valid (0, or greater)
  1000. //
  1001. void CSecPolItem::SelectResult( IResultData *pResultData )
  1002. {
  1003. if (-1 == m_nResultSelected)
  1004. return;
  1005. HRESULT hr = pResultData->ModifyItemState( m_nResultSelected,
  1006. (HRESULTITEM)0, LVIS_FOCUSED | LVIS_SELECTED, 0 );
  1007. // This fails if a property sheet is being displayed.
  1008. //ASSERT( S_OK == hr );
  1009. }
  1010. // Function: CheckForEnabled
  1011. // Description:
  1012. // Checks GetPolicy() policy to see if it is enabled given the current
  1013. // storage location. Returns FALSE if the storage location doesn't support
  1014. // Enabled/Disabled
  1015. BOOL CSecPolItem::CheckForEnabled ()
  1016. {
  1017. BOOL bRetVal = FALSE;
  1018. HRESULT hr = S_OK;
  1019. WCHAR szMachinePath[256];
  1020. WCHAR szPolicyDN[256];
  1021. BSTR pszCurrentDN = NULL;
  1022. PWIRELESS_POLICY_DATA pWirelessPolicyData = NULL;
  1023. HANDLE hLocalPolicyStore = NULL;
  1024. WCHAR szPathName[MAX_PATH];
  1025. DWORD dwError = 0;
  1026. // if we are an extension snapin then look to the GPE we are extending, otherwise
  1027. // use the normal storage location
  1028. // NOTE: we also check to make sure we are talking to the global store
  1029. // because we don't want to use the GPO object settings in anything but the
  1030. // DS case
  1031. pWirelessPolicyData = GetWirelessPolicy();
  1032. szPolicyDN[0] = L'\0';
  1033. szPathName[0] = L'\0';
  1034. if ( m_pComponentDataImpl->IsRsop() )
  1035. {
  1036. if ( pWirelessPolicyData->pRsopInfo && pWirelessPolicyData->pRsopInfo->uiPrecedence == 1 )
  1037. {
  1038. bRetVal = TRUE;
  1039. return bRetVal;
  1040. }
  1041. }
  1042. return bRetVal;
  1043. }
  1044. ////////////////////////////////////////////////////////////////////////////////
  1045. // IExtendControlbar helpers
  1046. STDMETHODIMP_(BOOL) CSecPolItem::UpdateToolbarButton
  1047. (
  1048. UINT id, // button ID
  1049. BOOL bSnapObjSelected, // ==TRUE when result/scope item is selected
  1050. BYTE fsState // enable/disable this button state by returning TRUE/FALSE
  1051. )
  1052. {
  1053. BOOL bActive = FALSE;
  1054. PWIRELESS_POLICY_DATA pWirelessPolicyData = NULL;
  1055. HANDLE hLocalPolicyStore = NULL;
  1056. STORAGE_LOCATION eStgLocation = m_pComponentDataImpl->EnumLocation();
  1057. pWirelessPolicyData = GetWirelessPolicy();
  1058. bActive = CheckForEnabled();
  1059. // Handle only the enable/disable state
  1060. if (ENABLED == fsState)
  1061. {
  1062. // Our toolbar has only two items
  1063. if (IDM_ASSIGN == id || IDM_UNASSIGN == id)
  1064. {
  1065. // The toolbar items should be enabled only if we are pointed to the local machine
  1066. if (LOCATION_REMOTE == eStgLocation || LOCATION_LOCAL == eStgLocation
  1067. // extension snapin?
  1068. || (LOCATION_GLOBAL == eStgLocation && (NULL != m_pComponentDataImpl->GetStaticScopeObject()->GetExtScopeObject())))
  1069. {
  1070. // Disable the SetActive button when policy is already active
  1071. if (IDM_ASSIGN == id)
  1072. {
  1073. if (bActive)
  1074. return FALSE;
  1075. else
  1076. return TRUE;
  1077. }
  1078. // Disable the SetInactive button when policy is already inactive
  1079. else if (IDM_UNASSIGN == id)
  1080. {
  1081. if (!bActive)
  1082. return FALSE;
  1083. else
  1084. return TRUE;
  1085. }
  1086. }
  1087. else
  1088. {
  1089. // Disable both the SetActive and SetInactive buttons for DS based snap-in
  1090. return FALSE;
  1091. }
  1092. }
  1093. }
  1094. return FALSE;
  1095. }
  1096. DWORD
  1097. ComputePolicyDN(
  1098. LPWSTR pszDirDomainName,
  1099. GUID PolicyIdentifier,
  1100. LPWSTR pszPolicyDN
  1101. )
  1102. {
  1103. DWORD dwError = 0;
  1104. LPWSTR pszPolicyIdentifier = NULL;
  1105. if (!pszDirDomainName) {
  1106. dwError = ERROR_INVALID_PARAMETER;
  1107. BAIL_ON_WIN32_ERROR(dwError);
  1108. }
  1109. dwError = StringFromCLSID(
  1110. PolicyIdentifier,
  1111. &pszPolicyIdentifier
  1112. );
  1113. BAIL_ON_WIN32_ERROR(dwError);
  1114. wcscpy(pszPolicyDN,L"cn=msieee80211-Policy");
  1115. wcscat(pszPolicyDN,pszPolicyIdentifier);
  1116. wcscat(pszPolicyDN,L",cn=Wireless Policy,cn=System,");
  1117. wcscat(pszPolicyDN, pszDirDomainName);
  1118. error:
  1119. if (pszPolicyIdentifier) {
  1120. CoTaskMemFree(pszPolicyIdentifier);
  1121. }
  1122. return(dwError);
  1123. }