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.

1963 lines
54 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997-1999 **/
  4. /**********************************************************************/
  5. /*
  6. snmppp.cpp
  7. snmp extension property pages
  8. FILE HISTORY:
  9. */
  10. #include "stdafx.h"
  11. #include "snmpclst.h"
  12. #include "snmpsnap.h"
  13. #include "snmppp.h"
  14. #include "tregkey.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. extern CString g_strMachineName;
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CAgentPage property page
  23. IMPLEMENT_DYNCREATE(CAgentPage, CPropertyPageBase)
  24. CAgentPage::CAgentPage()
  25. : CPropertyPageBase(CAgentPage::IDD)
  26. {
  27. //{{AFX_DATA_INIT(CAgentPage)
  28. #if 0
  29. m_strClientComment = _T("");
  30. #endif
  31. //}}AFX_DATA_INIT
  32. }
  33. CAgentPage::~CAgentPage()
  34. {
  35. }
  36. void CAgentPage::DoDataExchange(CDataExchange* pDX)
  37. {
  38. CPropertyPageBase::DoDataExchange(pDX);
  39. //{{AFX_DATA_MAP(CAgentPage)
  40. DDX_Control(pDX, IDC_CHECK_PHYSICAL, m_checkPhysical);
  41. DDX_Control(pDX, IDC_CHECK_APPLICATIONS, m_checkApplications);
  42. DDX_Control(pDX, IDC_CHECK_DATALINK, m_checkDatalink);
  43. DDX_Control(pDX, IDC_CHECK_INTERNET, m_checkInternet);
  44. DDX_Control(pDX, IDC_CHECK_ENDTOEND, m_checkEndToEnd);
  45. DDX_Control(pDX, IDC_EDIT_CONTACT, m_editContact);
  46. DDX_Control(pDX, IDC_EDIT_LOCATION, m_editLocation);
  47. //}}AFX_DATA_MAP
  48. }
  49. BEGIN_MESSAGE_MAP(CAgentPage, CPropertyPageBase)
  50. //{{AFX_MSG_MAP(CAgentPage)
  51. ON_BN_CLICKED(IDC_CHECK_PHYSICAL, OnClickedCheckPhysical)
  52. ON_BN_CLICKED(IDC_CHECK_APPLICATIONS, OnClickedCheckApplications)
  53. ON_BN_CLICKED(IDC_CHECK_DATALINK, OnClickedCheckDatalink)
  54. ON_BN_CLICKED(IDC_CHECK_INTERNET, OnClickedCheckInternet)
  55. ON_BN_CLICKED(IDC_CHECK_ENDTOEND, OnClickedCheckEndToEnd)
  56. ON_EN_CHANGE(IDC_EDIT_CONTACT, OnChangeEditContact)
  57. ON_EN_CHANGE(IDC_EDIT_LOCATION, OnChangeEditLocation)
  58. //}}AFX_MSG_MAP
  59. END_MESSAGE_MAP()
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CAgentPage message handlers
  62. BOOL CAgentPage::OnInitDialog()
  63. {
  64. CPropertyPageBase::OnInitDialog();
  65. m_bLocationChanged = FALSE;
  66. m_bContactChanged = FALSE;
  67. // Limit edit controls
  68. m_editContact.SetLimitText(COMBO_EDIT_LEN);
  69. m_editLocation.SetLimitText(COMBO_EDIT_LEN);
  70. if (LoadRegistry() == FALSE)
  71. {
  72. AfxMessageBox(IDS_REGISTRY_LOAD_FAILED, MB_ICONSTOP|MB_OK);
  73. // PostMessage(GetParent(*this), WM_COMMAND, IDCANCEL, 0);
  74. }
  75. return TRUE; // return TRUE unless you set the focus to a control
  76. // EXCEPTION: OCX Property Pages should return FALSE
  77. }
  78. BOOL CAgentPage::OnApply()
  79. {
  80. UpdateData();
  81. if( SaveRegistry() == FALSE )
  82. {
  83. AfxMessageBox(IDS_REGISTRY_SAVE_FAILED, MB_ICONSTOP|MB_OK);
  84. return FALSE;
  85. }
  86. BOOL bRet = CPropertyPageBase::OnApply();
  87. if (bRet == FALSE)
  88. {
  89. // Something bad happened... grab the error code
  90. AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
  91. // ::DhcpMessageBox(GetHolder()->GetError());
  92. }
  93. return bRet;
  94. }
  95. BOOL CAgentPage::LoadRegistry()
  96. {
  97. RegKey rk;
  98. LONG err;
  99. CString strContact;
  100. CString strLocation;
  101. DWORD dwServices = 0;
  102. LPCTSTR lpcszMachineName = g_strMachineName.IsEmpty() ? NULL : (LPCTSTR)g_strMachineName;
  103. // Open or create the registry key
  104. err = rk.Open(HKEY_LOCAL_MACHINE,
  105. AGENT_REG_KEY_NAME,
  106. KEY_ALL_ACCESS,
  107. lpcszMachineName);
  108. if (err != ERROR_SUCCESS)
  109. {
  110. err = rk.Create(HKEY_LOCAL_MACHINE,
  111. AGENT_REG_KEY_NAME,
  112. REG_OPTION_NON_VOLATILE,
  113. KEY_ALL_ACCESS,
  114. NULL,
  115. lpcszMachineName);
  116. if (err != ERROR_SUCCESS)
  117. return FALSE;
  118. }
  119. err = rk.QueryValue(SNMP_CONTACT, strContact);
  120. if (err != ERROR_SUCCESS)
  121. return FALSE;
  122. err = rk.QueryValue(SNMP_LOCATION, strLocation);
  123. if (err != ERROR_SUCCESS)
  124. return FALSE;
  125. err = rk.QueryValue(SNMP_SERVICES, dwServices);
  126. if (err != ERROR_SUCCESS)
  127. {
  128. // if no registry value, set it to default:
  129. // applications, end-to-end, internet
  130. dwServices = 0x40 | 0x8 | 0x4;
  131. }
  132. m_editContact.SetWindowText(strContact);
  133. m_editLocation.SetWindowText(strLocation);
  134. m_checkPhysical.SetCheck((dwServices & 0x1));
  135. m_checkDatalink.SetCheck((dwServices & 0x2));
  136. m_checkInternet.SetCheck((dwServices & 0x4));
  137. m_checkEndToEnd.SetCheck((dwServices & 0x8));
  138. m_checkApplications.SetCheck((dwServices & 0x40));
  139. return TRUE;
  140. }
  141. BOOL CAgentPage::SaveRegistry()
  142. {
  143. RegKey rk;
  144. LONG err;
  145. CString strContact;
  146. CString strLocation;
  147. DWORD dwServices = 0;
  148. LPCTSTR lpcszMachineName = g_strMachineName.IsEmpty() ? NULL : (LPCTSTR)g_strMachineName;
  149. // Open or create the registry key
  150. err = rk.Open(HKEY_LOCAL_MACHINE,
  151. AGENT_REG_KEY_NAME,
  152. KEY_ALL_ACCESS,
  153. lpcszMachineName);
  154. if (err != ERROR_SUCCESS)
  155. {
  156. err = rk.Create(HKEY_LOCAL_MACHINE,
  157. AGENT_REG_KEY_NAME,
  158. REG_OPTION_NON_VOLATILE,
  159. KEY_ALL_ACCESS,
  160. NULL,
  161. lpcszMachineName);
  162. if (err != ERROR_SUCCESS)
  163. return FALSE;
  164. }
  165. dwServices |= (m_checkPhysical.GetCheck() ? 0x1 : 0 ) ;
  166. dwServices |= (m_checkDatalink.GetCheck() ? 0x2 : 0 ) ;
  167. dwServices |= (m_checkInternet.GetCheck() ? 0x4 : 0 ) ;
  168. dwServices |= (m_checkEndToEnd.GetCheck() ? 0x8 : 0 ) ;
  169. dwServices |= (m_checkApplications.GetCheck() ? 0x40 : 0 ) ;
  170. err = rk.SetValue(SNMP_SERVICES, dwServices);
  171. if (err != ERROR_SUCCESS)
  172. return FALSE;
  173. m_editContact.GetWindowText(strContact);
  174. err = rk.SetValue(SNMP_CONTACT, strContact);
  175. if (err != ERROR_SUCCESS)
  176. return FALSE;
  177. m_editLocation.GetWindowText(strLocation);
  178. err = rk.SetValue(SNMP_LOCATION, strLocation);
  179. if (err != ERROR_SUCCESS)
  180. return FALSE;
  181. return TRUE;
  182. }
  183. void CAgentPage::OnClickedCheckPhysical()
  184. {
  185. SetDirty(TRUE);
  186. }
  187. void CAgentPage::OnClickedCheckApplications()
  188. {
  189. SetDirty(TRUE);
  190. }
  191. void CAgentPage::OnClickedCheckDatalink()
  192. {
  193. SetDirty(TRUE);
  194. }
  195. void CAgentPage::OnClickedCheckInternet()
  196. {
  197. SetDirty(TRUE);
  198. }
  199. void CAgentPage::OnClickedCheckEndToEnd()
  200. {
  201. SetDirty(TRUE);
  202. }
  203. void CAgentPage::OnChangeEditContact()
  204. {
  205. SetDirty(TRUE);
  206. }
  207. void CAgentPage::OnChangeEditLocation()
  208. {
  209. SetDirty(TRUE);
  210. }
  211. /////////////////////////////////////////////////////////////////////////////
  212. // CTrapsPage property page
  213. //
  214. IMPLEMENT_DYNCREATE(CTrapsPage, CPropertyPageBase)
  215. CTrapsPage::CTrapsPage()
  216. : CPropertyPageBase(CTrapsPage::IDD)
  217. {
  218. m_pCommunityList = new CObList();
  219. m_fPolicyTrapConfig = FALSE; // default to use service registry
  220. //{{AFX_DATA_INIT(CTrapsPage)
  221. #if 0
  222. m_strServerNetbiosName = _T("");
  223. #endif
  224. //}}AFX_DATA_INIT
  225. }
  226. CTrapsPage::~CTrapsPage()
  227. {
  228. m_pCommunityList->RemoveAll();
  229. delete m_pCommunityList;
  230. }
  231. void CTrapsPage::DoDataExchange(CDataExchange* pDX)
  232. {
  233. CPropertyPageBase::DoDataExchange(pDX);
  234. //{{AFX_DATA_MAP(CTrapsPage)
  235. DDX_Control(pDX, IDC_COMBO_COMMUNITY, m_comboCommunityName);
  236. DDX_Control(pDX, IDC_BUTTON_ADD_NAME, m_buttonAddName);
  237. DDX_Control(pDX, IDC_BUTTON_REMOVE_NAME, m_buttonRemoveName);
  238. DDX_Control(pDX, IDC_LIST_TRAP, m_listboxTrapDestinations);
  239. DDX_Control(pDX, IDC_BUTTON_ADD_TRAP, m_buttonAddTrap);
  240. DDX_Control(pDX, IDC_BUTTON_EDIT_TRAP, m_buttonEditTrap);
  241. DDX_Control(pDX, IDC_BUTTON_REMOVE_TRAP, m_buttonRemoveTrap);
  242. //}}AFX_DATA_MAP
  243. }
  244. BEGIN_MESSAGE_MAP(CTrapsPage, CPropertyPageBase)
  245. //{{AFX_MSG_MAP(CTrapsPage)
  246. ON_BN_CLICKED(IDC_BUTTON_ADD_NAME, OnClickedButtonAddName)
  247. ON_BN_CLICKED(IDC_BUTTON_REMOVE_NAME, OnClickedButtonRemoveName)
  248. ON_BN_CLICKED(IDC_BUTTON_ADD_TRAP, OnClickedButtonAddTrap)
  249. ON_BN_CLICKED(IDC_BUTTON_EDIT_TRAP, OnClickedButtonEditTrap)
  250. ON_BN_CLICKED(IDC_BUTTON_REMOVE_TRAP, OnClickedButtonRemoveTrap)
  251. ON_CBN_EDITCHANGE(IDC_COMBO_COMMUNITY, OnEditChangeCommunityName)
  252. ON_CBN_EDITUPDATE(IDC_COMBO_COMMUNITY, OnEditUpdateCommunityName)
  253. ON_CBN_SELCHANGE(IDC_COMBO_COMMUNITY, OnSelectionChangeCommunityName)
  254. //}}AFX_MSG_MAP
  255. END_MESSAGE_MAP()
  256. /////////////////////////////////////////////////////////////////////////////
  257. // CTrapsPage message handlers
  258. BOOL CTrapsPage::OnInitDialog()
  259. {
  260. CPropertyPageBase::OnInitDialog();
  261. m_comboCommunityName.LimitText(COMBO_EDIT_LEN-1);
  262. if( LoadRegistry() ) {
  263. if( m_comboCommunityName.GetCount() ) {
  264. m_comboCommunityName.SetCurSel(0);
  265. LoadTrapDestination(0);
  266. }
  267. }
  268. else {
  269. AfxMessageBox(IDS_REGISTRY_LOAD_FAILED, MB_ICONSTOP|MB_OK);
  270. }
  271. UpdateCommunityAddButton(); // set state of Add button
  272. UpdateCommunityRemoveButton(); // set state of Remove button
  273. UpdateTrapDestinationButtons(); // set state of TRAP destination buttons
  274. return TRUE; // return TRUE unless you set the focus to a control
  275. // EXCEPTION: OCX Property Pages should return FALSE
  276. }
  277. BOOL CTrapsPage::OnApply()
  278. {
  279. UpdateData();
  280. if( SaveRegistry() == FALSE )
  281. {
  282. AfxMessageBox(IDS_REGISTRY_SAVE_FAILED, MB_ICONSTOP|MB_OK);
  283. return FALSE;
  284. }
  285. BOOL bRet = CPropertyPageBase::OnApply();
  286. if (bRet == FALSE)
  287. {
  288. // Something bad happened... grab the error code
  289. AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
  290. // ::DhcpMessageBox(GetHolder()->GetError());
  291. }
  292. return bRet;
  293. }
  294. BOOL CTrapsPage::LoadRegistry()
  295. {
  296. RegKey rk;
  297. RegKey rkTrap;
  298. RegKeyIterator rkIter;
  299. RegValueIterator rvIter;
  300. HRESULT hr, hrIter;
  301. LONG err;
  302. CString stKey, stValue;
  303. LPCTSTR lpcszMachineName = g_strMachineName.IsEmpty() ? NULL : (LPCTSTR)g_strMachineName;
  304. BOOL fPolicy;
  305. // we need to provide precedence to the parameters set through the policy
  306. fPolicy = TRUE;
  307. if (fPolicy)
  308. {
  309. err = rk.Open(HKEY_LOCAL_MACHINE,
  310. POLICY_TRAP_CONFIG_KEY_NAME,
  311. KEY_ALL_ACCESS,
  312. lpcszMachineName);
  313. if (err != ERROR_SUCCESS)
  314. fPolicy = FALSE;
  315. else
  316. {
  317. // remember that we are loading from policy registry
  318. m_fPolicyTrapConfig = TRUE;
  319. }
  320. }
  321. if (fPolicy == FALSE)
  322. {
  323. // Open or create the registry key
  324. err = rk.Open(HKEY_LOCAL_MACHINE,
  325. TRAP_CONFIG_KEY_NAME,
  326. KEY_ALL_ACCESS,
  327. lpcszMachineName);
  328. if (err != ERROR_SUCCESS)
  329. {
  330. err = rk.Create(HKEY_LOCAL_MACHINE,
  331. TRAP_CONFIG_KEY_NAME,
  332. REG_OPTION_NON_VOLATILE,
  333. KEY_ALL_ACCESS,
  334. NULL,
  335. lpcszMachineName);
  336. if (err != ERROR_SUCCESS)
  337. return FALSE;
  338. }
  339. }
  340. err = rkIter.Init(&rk);
  341. if (err != ERROR_SUCCESS) {
  342. return FALSE;
  343. }
  344. for (hrIter = rkIter.Next(&stKey); hrIter == hrOK; hrIter = rkIter.Next(&stKey)) {
  345. //
  346. // open the key
  347. //
  348. err = rkTrap.Open(rk, stKey, KEY_READ);
  349. int nIndex = m_comboCommunityName.InsertString(-1, stKey);
  350. if (nIndex >= 0 ) {
  351. CStringList * pstrList = new CStringList();
  352. if (!pstrList) {
  353. return FALSE;
  354. }
  355. m_comboCommunityName.SetItemData(nIndex, (ULONG_PTR) pstrList);
  356. m_pCommunityList->AddHead(pstrList);
  357. err = rvIter.Init(&rkTrap);
  358. if (err != ERROR_SUCCESS) {
  359. return FALSE;
  360. }
  361. for (hr = rvIter.Next(&stValue, NULL); hr == hrOK; hr = rvIter.Next(&stValue, NULL)) {
  362. CString strTrap;
  363. err = rkTrap.QueryValue(stValue, strTrap);
  364. if (err != ERROR_SUCCESS) {
  365. return FALSE;
  366. }
  367. CString * pstr = new CString(strTrap);
  368. if (!pstr) {
  369. return FALSE;
  370. }
  371. pstrList->AddHead(*pstr);
  372. }
  373. }
  374. }
  375. return TRUE;
  376. }
  377. BOOL CTrapsPage::SaveRegistry()
  378. {
  379. RegKey rk;
  380. RegKey rkTrap;
  381. LONG err;
  382. LPCTSTR lpcszMachineName = g_strMachineName.IsEmpty() ? NULL : (LPCTSTR)g_strMachineName;
  383. if (m_fPolicyTrapConfig)
  384. {
  385. // Group Policy enforced
  386. return TRUE;
  387. }
  388. // Open the SNMP Parameters key
  389. err = rk.Open(HKEY_LOCAL_MACHINE,
  390. SNMP_PARAMS_KEY_NAME,
  391. KEY_ALL_ACCESS,
  392. lpcszMachineName);
  393. if (err != ERROR_SUCCESS)
  394. {
  395. return FALSE;
  396. }
  397. // delete the Trap Configuration key first
  398. err = rk.RecurseDeleteKey(TRAP_CONFIGURATION);
  399. if (err != ERROR_SUCCESS) {
  400. return FALSE;
  401. }
  402. // now create the key to add the new values
  403. err = rk.Create(HKEY_LOCAL_MACHINE,
  404. TRAP_CONFIG_KEY_NAME,
  405. REG_OPTION_NON_VOLATILE,
  406. KEY_ALL_ACCESS,
  407. NULL,
  408. lpcszMachineName);
  409. if (err != ERROR_SUCCESS)
  410. return FALSE;
  411. CString strCommunityName;
  412. int nMax = m_comboCommunityName.GetCount();
  413. for (int nCount = 0; nCount < nMax; nCount++ ) {
  414. m_comboCommunityName.GetLBText(nCount, strCommunityName);
  415. ASSERT(strCommunityName.GetLength());
  416. CStringList * pstrList = (CStringList *)m_comboCommunityName.GetItemData(nCount);
  417. // For each community name create a key
  418. err = rkTrap.Create(rk, strCommunityName);
  419. if (err != ERROR_SUCCESS) {
  420. return FALSE;
  421. }
  422. // for each community name get the TRAP destination values from the saved
  423. // away list and write them as values to the registry
  424. POSITION posTrap;
  425. CString * pstr;
  426. TCHAR buf[32];
  427. int j;
  428. for (j = 1, posTrap = pstrList->GetHeadPosition();
  429. posTrap != NULL && (pstr = & (pstrList->GetNext( posTrap )));
  430. j++ ) {
  431. wsprintf(buf, TEXT("%d"), j);
  432. err = rkTrap.SetValue(buf, *pstr);
  433. }
  434. }
  435. return TRUE;
  436. }
  437. BOOL CTrapsPage::LoadTrapDestination(int nIndex)
  438. {
  439. ASSERT (m_pCommunityList);
  440. POSITION pos;
  441. // empty list box and add trap destinations for this community name
  442. m_listboxTrapDestinations.ResetContent();
  443. CStringList * pstrList = (CStringList *)m_comboCommunityName.GetItemData(nIndex);
  444. CString * pstr;
  445. for (pos = pstrList->GetHeadPosition();
  446. pos != NULL && (pstr = & pstrList->GetNext( pos )); /**/) {
  447. m_listboxTrapDestinations.AddString(*pstr);
  448. }
  449. if (m_listboxTrapDestinations.GetCount()) {
  450. m_listboxTrapDestinations.SetCurSel(0);
  451. }
  452. return TRUE;
  453. }
  454. void CTrapsPage::UpdateCommunityAddButton()
  455. {
  456. CString strCommunityName;
  457. m_comboCommunityName.GetWindowText(strCommunityName);
  458. if (strCommunityName.GetLength() && strCommunityName != L" ") {
  459. // enable the button if the text doesn't match
  460. BOOL bEnable = (m_comboCommunityName.FindStringExact(-1, strCommunityName) == CB_ERR);
  461. if (!bEnable) {
  462. // move focus to the combo box before disabling the Add button
  463. m_comboCommunityName.SetFocus();
  464. }
  465. m_buttonAddName.EnableWindow(bEnable);
  466. }
  467. else
  468. {
  469. m_buttonAddName.EnableWindow(FALSE);
  470. m_comboCommunityName.SetFocus();
  471. }
  472. if (m_fPolicyTrapConfig)
  473. {
  474. // Group Policy enforced
  475. m_buttonAddName.EnableWindow(FALSE);
  476. }
  477. }
  478. void CTrapsPage::UpdateCommunityRemoveButton()
  479. {
  480. int nCount = m_comboCommunityName.GetCount();
  481. if(nCount == 0)
  482. m_comboCommunityName.SetFocus();
  483. m_buttonRemoveName.EnableWindow(nCount? TRUE : FALSE);
  484. if (m_fPolicyTrapConfig)
  485. {
  486. // Group Policy enforced
  487. m_buttonRemoveName.EnableWindow(FALSE);
  488. m_buttonAddName.EnableWindow(FALSE);
  489. }
  490. }
  491. void CTrapsPage::UpdateTrapDestinationButtons()
  492. {
  493. m_buttonAddTrap.EnableWindow(m_comboCommunityName.GetCount()? TRUE : FALSE);
  494. int nCount = m_listboxTrapDestinations.GetCount();
  495. m_buttonEditTrap.EnableWindow(nCount? TRUE: FALSE);
  496. m_buttonRemoveTrap.EnableWindow(nCount? TRUE: FALSE);
  497. if (m_fPolicyTrapConfig)
  498. {
  499. // Group Policy enforced
  500. m_buttonAddTrap.EnableWindow(FALSE);
  501. m_buttonEditTrap.EnableWindow(FALSE);
  502. m_buttonRemoveTrap.EnableWindow(FALSE);
  503. }
  504. }
  505. /*
  506. Handle the ON_CBN_EDITCHANGE message.
  507. The user has taken an action that may have altered the text in the
  508. edit-control portion of a combo box.
  509. Unlike the CBN_EDITUPDATE message, this message is sent after Windows
  510. updates the screen.
  511. */
  512. void CTrapsPage::OnEditChangeCommunityName()
  513. {
  514. UpdateCommunityAddButton();
  515. // community name entered in the edit-control portion of the combo box,
  516. // we need to disable the Trap destinations "Edit..." and "Remove"
  517. // buttons
  518. m_buttonEditTrap.EnableWindow(FALSE);
  519. m_buttonRemoveTrap.EnableWindow(FALSE);
  520. // UpdateTrapDestinationButtons();
  521. }
  522. /*
  523. Handle the ON_CBN_EDITUPDATE message.
  524. The user has taken an action that may have altered the text in the
  525. edit-control portion of a combo box.
  526. */
  527. void CTrapsPage::OnEditUpdateCommunityName()
  528. {
  529. UpdateCommunityAddButton();
  530. m_buttonRemoveName.EnableWindow(FALSE);
  531. m_buttonAddTrap.EnableWindow(FALSE);
  532. }
  533. void CTrapsPage::OnSelectionChangeCommunityName()
  534. // User has changed the community name selection
  535. // load the corresponding trap destination entries in the listbox
  536. {
  537. int nIndex = m_comboCommunityName.GetCurSel();
  538. if (nIndex != CB_ERR) {
  539. LoadTrapDestination(nIndex);
  540. // Windows Bug# 401218
  541. // the user has selected a previous entered community name.
  542. // we should make sure the "Add to list" button is disabled.
  543. m_buttonAddName.EnableWindow(FALSE);
  544. }
  545. else // there are no items in the combobox
  546. {
  547. m_listboxTrapDestinations.ResetContent();
  548. }
  549. UpdateCommunityRemoveButton();
  550. UpdateTrapDestinationButtons();
  551. }
  552. void CTrapsPage::OnClickedButtonAddName()
  553. {
  554. CString strCommunityName;
  555. m_comboCommunityName.GetWindowText(strCommunityName);
  556. int nIndex = m_comboCommunityName.InsertString(-1, strCommunityName);
  557. if (nIndex >= 0 ) {
  558. // Create a Trap list item and add to global list
  559. // the items added to the trap destination will be inserted in
  560. // this list - see OnClickedAddbuttonAddTrap
  561. CStringList * pstrList = new CStringList;
  562. m_pCommunityList->AddHead(pstrList);
  563. // save list pointer with data
  564. m_comboCommunityName.SetItemData(nIndex, (ULONG_PTR) pstrList);
  565. m_comboCommunityName.SetCurSel(nIndex);
  566. LoadTrapDestination(nIndex);
  567. UpdateCommunityAddButton();
  568. UpdateCommunityRemoveButton();
  569. UpdateTrapDestinationButtons();
  570. SetDirty(TRUE);
  571. }
  572. }
  573. void CTrapsPage::OnClickedButtonRemoveName()
  574. {
  575. int nIndex = m_comboCommunityName.GetCurSel();
  576. ASSERT(nIndex >= 0 );
  577. POSITION pos;
  578. if (nIndex != CB_ERR) {
  579. ASSERT (m_pCommunityList);
  580. CStringList * pstrList, * pstrList2;
  581. pstrList = (CStringList *) m_comboCommunityName.GetItemData(nIndex);
  582. pos = m_pCommunityList->Find( pstrList);
  583. if( pos ) {
  584. pstrList2 = (CStringList *) & m_pCommunityList->GetAt(pos);
  585. // delete the item from the global list
  586. m_pCommunityList->RemoveAt(pos);
  587. delete pstrList;
  588. }
  589. // remove the item from the Combo Box
  590. m_comboCommunityName.DeleteString(nIndex);
  591. // select item 0 if there are any left
  592. if (m_comboCommunityName.GetCount()) {
  593. m_comboCommunityName.SetCurSel(0);
  594. }
  595. else
  596. m_comboCommunityName.SetWindowText(_T(""));
  597. OnSelectionChangeCommunityName();
  598. UpdateCommunityRemoveButton();
  599. UpdateTrapDestinationButtons();
  600. SetDirty(TRUE);
  601. }
  602. }
  603. void CTrapsPage::OnClickedButtonAddTrap()
  604. {
  605. if (m_dlgAdd.DoModal() == IDOK)
  606. {
  607. if (m_dlgAdd.m_strName.GetLength()) {
  608. int nIndex = m_comboCommunityName.GetCurSel();
  609. if (nIndex >= 0) {
  610. ULONG_PTR dwData = m_comboCommunityName.GetItemData(nIndex);
  611. if (dwData != CB_ERR) {
  612. CStringList * pstrList = (CStringList *) dwData;
  613. nIndex = m_listboxTrapDestinations.InsertString(-1, m_dlgAdd.m_strName);
  614. if (nIndex >=0) {
  615. SetDirty(TRUE);
  616. m_listboxTrapDestinations.SetCurSel(nIndex);
  617. CString * pstr = new CString(m_dlgAdd.m_strName);
  618. pstrList->AddHead(*pstr);
  619. }
  620. }
  621. }
  622. }
  623. }
  624. UpdateTrapDestinationButtons();
  625. }
  626. void CTrapsPage::OnClickedButtonEditTrap()
  627. {
  628. int nLBIndex = m_listboxTrapDestinations.GetCurSel();
  629. int nCBIndex = m_comboCommunityName.GetCurSel();
  630. if (nCBIndex >=0 && nLBIndex >=0) {
  631. CString strTrap;
  632. m_listboxTrapDestinations.GetText(nLBIndex, strTrap);
  633. ASSERT( strTrap.GetLength() != 0 );
  634. m_dlgEdit.m_strName = strTrap;
  635. // present dialog
  636. if (m_dlgEdit.DoModal() == IDOK)
  637. {
  638. if (m_dlgEdit.m_strName.GetLength()) {
  639. // remove item and then add it
  640. OnClickedButtonRemoveTrap();
  641. ULONG_PTR dwData = m_comboCommunityName.GetItemData(nCBIndex);
  642. if (dwData != CB_ERR) {
  643. CStringList * pstrList = (CStringList *) dwData;
  644. nLBIndex = m_listboxTrapDestinations.InsertString(-1, m_dlgEdit.m_strName);
  645. if (nLBIndex >=0) {
  646. SetDirty(TRUE);
  647. m_listboxTrapDestinations.SetCurSel(nLBIndex);
  648. CString * pstr = new CString(m_dlgEdit.m_strName);
  649. pstrList->AddHead(*pstr);
  650. }
  651. }
  652. }
  653. }
  654. }
  655. UpdateTrapDestinationButtons();
  656. }
  657. void CTrapsPage::OnClickedButtonRemoveTrap()
  658. {
  659. int nCount = -1;
  660. int nLBIndex = m_listboxTrapDestinations.GetCurSel();
  661. int nCBIndex = m_comboCommunityName.GetCurSel();
  662. if (nCBIndex >=0 && nLBIndex >=0) {
  663. ULONG_PTR dwData = m_comboCommunityName.GetItemData(nCBIndex);
  664. ASSERT (dwData != CB_ERR);
  665. CString strTrap;
  666. m_listboxTrapDestinations.GetText(nLBIndex, strTrap);
  667. ASSERT( strTrap.GetLength() != 0 );
  668. if (strTrap.GetLength() != NULL && dwData != CB_ERR) {
  669. m_listboxTrapDestinations.DeleteString(nLBIndex);
  670. SetDirty(TRUE);
  671. CStringList * pStrList = (CStringList*) dwData;
  672. // find the matching string and remove
  673. POSITION pos = pStrList->Find(strTrap);
  674. pStrList->RemoveAt(pos);
  675. m_dlgAdd.m_strName = strTrap; // save off item removed
  676. nCount = m_listboxTrapDestinations.GetCount();
  677. if (nCount) {
  678. if (nCount != nLBIndex) {
  679. m_listboxTrapDestinations.SetCurSel(nLBIndex);
  680. }
  681. else
  682. {
  683. m_listboxTrapDestinations.SetCurSel(nCount-1);
  684. }
  685. }
  686. }
  687. }
  688. UpdateTrapDestinationButtons();
  689. if (nCount == 0)
  690. {
  691. // Windows Bug# 401181,
  692. // when all the hosts in the Trap destinations list box are removed,
  693. // we need to set focus
  694. m_comboCommunityName.SetFocus();
  695. }
  696. }
  697. IMPLEMENT_DYNCREATE(CSecurityPage, CPropertyPageBase)
  698. CSecurityPage::CSecurityPage()
  699. : CPropertyPageBase(CSecurityPage::IDD)
  700. {
  701. //{{AFX_DATA_INIT(CSecurityPage)
  702. //}}AFX_DATA_INIT
  703. m_fPolicyValidCommunities = m_fPolicyPermittedManagers = FALSE;
  704. }
  705. CSecurityPage::~CSecurityPage()
  706. {
  707. }
  708. void CSecurityPage::DoDataExchange(CDataExchange* pDX)
  709. {
  710. CPropertyPageBase::DoDataExchange(pDX);
  711. //{{AFX_DATA_MAP(CSecurityPage)
  712. DDX_Control(pDX, IDC_LIST_COMMUNITY, m_listboxCommunity);
  713. DDX_Control(pDX, IDC_BUTTON_ADD_COMMUNITY, m_buttonAddCommunity);
  714. DDX_Control(pDX, IDC_BUTTON_EDIT_COMMUNITY, m_buttonEditCommunity);
  715. DDX_Control(pDX, IDC_BUTTON_REMOVE_COMMUNITY, m_buttonRemoveCommunity);
  716. DDX_Control(pDX, IDC_BUTTON_ADD_HOSTS, m_buttonAddHost);
  717. DDX_Control(pDX, IDC_BUTTON_EDIT_HOSTS, m_buttonEditHost);
  718. DDX_Control(pDX, IDC_BUTTON_REMOVE_HOSTS, m_buttonRemoveHost);
  719. DDX_Control(pDX, IDC_LIST_HOSTS, m_listboxHost);
  720. DDX_Control(pDX, IDC_CHECK_SEND_AUTH_TRAP, m_checkSendAuthTrap);
  721. DDX_Control(pDX, IDC_RADIO_ACCEPT_ANY, m_radioAcceptAnyHost);
  722. DDX_Control(pDX, IDC_RADIO_ACCEPT_SPECIFIC_HOSTS, m_radioAcceptSpecificHost);
  723. //}}AFX_DATA_MAP
  724. }
  725. BEGIN_MESSAGE_MAP(CSecurityPage, CPropertyPageBase)
  726. //{{AFX_MSG_MAP(CSecurityPage)
  727. ON_BN_CLICKED(IDC_BUTTON_ADD_COMMUNITY, OnClickedButtonAddCommunity)
  728. ON_BN_CLICKED(IDC_BUTTON_EDIT_COMMUNITY, OnClickedButtonEditCommunity)
  729. ON_BN_CLICKED(IDC_BUTTON_REMOVE_COMMUNITY, OnClickedButtonRemoveCommunity)
  730. ON_BN_CLICKED(IDC_BUTTON_ADD_HOSTS, OnClickedButtonAddHost)
  731. ON_BN_CLICKED(IDC_BUTTON_EDIT_HOSTS, OnClickedButtonEditHost)
  732. ON_BN_CLICKED(IDC_BUTTON_REMOVE_HOSTS, OnClickedButtonRemoveHost)
  733. ON_BN_CLICKED(IDC_CHECK_SEND_AUTH_TRAP, OnClickedCheckSendAuthTrap)
  734. ON_BN_CLICKED(IDC_RADIO_ACCEPT_ANY, OnClickedRadioAcceptAnyHost)
  735. ON_BN_CLICKED(IDC_RADIO_ACCEPT_SPECIFIC_HOSTS, OnClickedRadioAcceptSpecificHost)
  736. ON_NOTIFY(NM_DBLCLK, IDC_LIST_COMMUNITY, OnDblclkCtrlistCommunity)
  737. ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_COMMUNITY, OnCommunityListChanged)
  738. //}}AFX_MSG_MAP
  739. END_MESSAGE_MAP()
  740. /////////////////////////////////////////////////////////////////////////////
  741. // CSecurityPage message handlers
  742. BOOL CSecurityPage::OnInitDialog()
  743. {
  744. CPropertyPageBase::OnInitDialog();
  745. m_listboxCommunity.OnInitList();
  746. if (LoadRegistry() == FALSE) {
  747. AfxMessageBox(IDS_REGISTRY_LOAD_FAILED, MB_ICONSTOP|MB_OK);
  748. }
  749. UpdateNameButtons();
  750. UpdateHostButtons();
  751. return TRUE; // return TRUE unless you set the focus to a control
  752. // EXCEPTION: OCX Property Pages should return FALSE
  753. }
  754. BOOL CSecurityPage::OnApply()
  755. {
  756. UpdateData();
  757. if (m_radioAcceptSpecificHost.GetCheck()) {
  758. if (m_listboxHost.GetCount() == 0) {
  759. AfxMessageBox(IDS_ACCEPTHOST_MISSING, MB_ICONSTOP|MB_OK);
  760. return FALSE;
  761. }
  762. }
  763. if( SaveRegistry() == FALSE )
  764. {
  765. AfxMessageBox(IDS_REGISTRY_SAVE_FAILED, MB_ICONSTOP|MB_OK);
  766. return FALSE;
  767. }
  768. BOOL bRet = CPropertyPageBase::OnApply();
  769. if (bRet == FALSE)
  770. {
  771. // Something bad happened... grab the error code
  772. AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
  773. // ::DhcpMessageBox(GetHolder()->GetError());
  774. }
  775. return bRet;
  776. }
  777. void CSecurityPage::OnClickedButtonAddCommunity()
  778. {
  779. CString strText;
  780. strText.LoadString( IDS_SNMPCOMM_TEXT );
  781. // change static text of dialog to "Community Name"
  782. m_dlgAddName.m_bCommunity = TRUE;
  783. m_dlgAddName.m_nChoice = PERM_BIT_READONLY;
  784. if (m_dlgAddName.DoModal() == IDOK)
  785. {
  786. int nIndex;
  787. if (m_dlgAddName.m_strName.GetLength()) {
  788. if ((nIndex = m_listboxCommunity.InsertString(-1, m_dlgAddName.m_strName)) >=0 &&
  789. m_listboxCommunity.SetItemText(nIndex, 1, m_dlgAddName.m_strChoice) &&
  790. m_listboxCommunity.SetItemData(nIndex, m_dlgAddName.m_nChoice))
  791. {
  792. m_listboxCommunity.SetCurSel(nIndex);
  793. UpdateNameButtons();
  794. SetDirty(TRUE);
  795. }
  796. }
  797. }
  798. // Windows Bug# 401181
  799. m_buttonAddCommunity.SetFocus();
  800. }
  801. void CSecurityPage::OnClickedButtonEditCommunity()
  802. {
  803. int nIndex;
  804. int nPermissions;
  805. nIndex = m_listboxCommunity.GetCurSel();
  806. ASSERT(nIndex >= 0);
  807. if (nIndex < 0) {
  808. return;
  809. }
  810. nPermissions = (int) m_listboxCommunity.GetItemData(nIndex);
  811. CString strName;
  812. m_listboxCommunity.GetText(nIndex, strName);
  813. // save off old string
  814. m_dlgAddName.m_strName = strName;
  815. m_dlgAddName.m_nChoice = nPermissions;
  816. m_dlgEditName.m_bCommunity = TRUE;
  817. m_dlgEditName.m_strName = strName;
  818. m_dlgEditName.m_nChoice = nPermissions;
  819. if (m_dlgEditName.DoModal() == IDOK) {
  820. // first delete the string from list box
  821. m_listboxCommunity.DeleteString(nIndex);
  822. SetDirty(TRUE);
  823. if ((nIndex = m_listboxCommunity.InsertString(nIndex, m_dlgEditName.m_strName)) >= 0 &&
  824. m_listboxCommunity.SetItemText(nIndex, 1, m_dlgEditName.m_strChoice) &&
  825. m_listboxCommunity.SetItemData(nIndex, m_dlgEditName.m_nChoice))
  826. {
  827. m_listboxCommunity.SetCurSel(nIndex);
  828. }
  829. }
  830. // Windows Bug# 401181
  831. m_buttonEditCommunity.SetFocus();
  832. }
  833. void CSecurityPage::OnClickedButtonRemoveCommunity()
  834. {
  835. int nIndex = m_listboxCommunity.GetCurSel();
  836. if (nIndex < 0)
  837. return;
  838. CString strName;
  839. m_listboxCommunity.GetText(nIndex, strName);
  840. // save off removed name for quick add
  841. m_dlgAddName.m_strName = strName;
  842. m_listboxCommunity.DeleteString(nIndex);
  843. int nCount = m_listboxCommunity.GetCount();
  844. if (nCount != nIndex) {
  845. m_listboxCommunity.SetCurSel(nIndex);
  846. }
  847. else
  848. {
  849. m_listboxCommunity.SetCurSel(nCount -1);
  850. }
  851. UpdateNameButtons();
  852. if (0 != nCount)
  853. {
  854. // Windows Bug# 401181
  855. m_buttonRemoveCommunity.SetFocus();
  856. }
  857. else
  858. {
  859. // all community names are removed, move focus to Community Add button
  860. m_buttonAddCommunity.SetFocus();
  861. }
  862. SetDirty(TRUE);
  863. }
  864. void CSecurityPage::UpdateNameButtons()
  865. {
  866. int nCount = m_listboxCommunity.GetCount();
  867. m_buttonEditCommunity.EnableWindow(nCount? TRUE: FALSE);
  868. m_buttonRemoveCommunity.EnableWindow(nCount? TRUE: FALSE);
  869. if (m_fPolicyValidCommunities)
  870. {
  871. m_buttonEditCommunity.EnableWindow(FALSE);
  872. m_buttonRemoveCommunity.EnableWindow(FALSE);
  873. m_buttonAddCommunity.EnableWindow(FALSE);
  874. }
  875. }
  876. void CSecurityPage::OnClickedButtonAddHost()
  877. {
  878. CString strHostName;
  879. if (m_dlgAddHost.DoModal() == IDOK) {
  880. int nIndex;
  881. if ((nIndex = m_listboxHost.InsertString(-1, m_dlgAddHost.m_strName)) >=0) {
  882. m_listboxHost.SetCurSel(nIndex);
  883. m_radioAcceptSpecificHost.SetCheck(1);
  884. m_radioAcceptAnyHost.SetCheck(0);
  885. UpdateHostButtons();
  886. SetDirty(TRUE);
  887. }
  888. }
  889. }
  890. void CSecurityPage::OnClickedButtonEditHost()
  891. {
  892. int nIndex;
  893. nIndex = m_listboxHost.GetCurSel();
  894. ASSERT( nIndex >= 0 );
  895. if (nIndex < 0) {
  896. return;
  897. }
  898. CString strHost;
  899. m_listboxHost.GetText(nIndex, strHost);
  900. // save off old host name for quick add
  901. m_dlgAddHost.m_strName = strHost;
  902. m_dlgEditHost.m_strName = strHost;
  903. if (m_dlgEditHost.DoModal() == IDOK) {
  904. m_listboxHost.DeleteString(nIndex);
  905. SetDirty(TRUE);
  906. if ((nIndex = m_listboxHost.InsertString(-1, m_dlgEditHost.m_strName)) >= 0) {
  907. m_listboxHost.SetCurSel(nIndex);
  908. }
  909. }
  910. }
  911. void CSecurityPage::OnClickedButtonRemoveHost()
  912. {
  913. int nIndex = m_listboxHost.GetCurSel();
  914. ASSERT(nIndex >= 0);
  915. CString strHost;
  916. m_listboxHost.GetText(nIndex, strHost);
  917. // save off removed host name for quick add
  918. m_dlgAddHost.m_strName = strHost;
  919. m_listboxHost.DeleteString(nIndex);
  920. int nCount = m_listboxHost.GetCount();
  921. if (nCount != nIndex) {
  922. m_listboxHost.SetCurSel(nIndex);
  923. }
  924. else
  925. {
  926. m_listboxHost.SetCurSel(nCount - 1);
  927. }
  928. m_radioAcceptSpecificHost.SetCheck(nCount);
  929. m_radioAcceptAnyHost.SetCheck(!nCount);
  930. UpdateHostButtons();
  931. if (nCount == 0)
  932. {
  933. // Windows Bug# 401181,
  934. // when all the hosts are removed, we need to set the focus
  935. m_radioAcceptAnyHost.SetFocus();
  936. }
  937. SetDirty(TRUE);
  938. }
  939. void CSecurityPage::UpdateHostButtons()
  940. {
  941. int nCount = m_listboxHost.GetCount();
  942. m_buttonEditHost.EnableWindow(nCount? TRUE: FALSE);
  943. m_buttonRemoveHost.EnableWindow(nCount? TRUE: FALSE);
  944. if (m_fPolicyPermittedManagers)
  945. {
  946. m_buttonEditHost.EnableWindow(FALSE);
  947. m_buttonRemoveHost.EnableWindow(FALSE);
  948. m_buttonAddHost.EnableWindow(FALSE);
  949. m_radioAcceptAnyHost.EnableWindow(FALSE);
  950. m_radioAcceptSpecificHost.EnableWindow(FALSE);
  951. }
  952. }
  953. void CSecurityPage::OnClickedCheckSendAuthTrap()
  954. {
  955. SetDirty(TRUE);
  956. }
  957. void CSecurityPage::OnClickedRadioAcceptAnyHost()
  958. {
  959. m_listboxHost.ResetContent();
  960. UpdateHostButtons();
  961. SetDirty(TRUE);
  962. }
  963. void CSecurityPage::OnClickedRadioAcceptSpecificHost()
  964. {
  965. SetDirty(TRUE);
  966. }
  967. PACL CSecurityPage::AllocACL()
  968. {
  969. PACL pAcl = NULL;
  970. PSID pSidAdmins = NULL;
  971. SID_IDENTIFIER_AUTHORITY Authority = SECURITY_NT_AUTHORITY;
  972. EXPLICIT_ACCESS ea[1];
  973. // Create a SID for the BUILTIN\Administrators group.
  974. if ( !AllocateAndInitializeSid( &Authority,
  975. 2,
  976. SECURITY_BUILTIN_DOMAIN_RID,
  977. DOMAIN_ALIAS_RID_ADMINS,
  978. 0, 0, 0, 0, 0, 0,
  979. &pSidAdmins ))
  980. {
  981. return NULL;
  982. }
  983. // Initialize an EXPLICIT_ACCESS structure for an ACE.
  984. ZeroMemory(&ea, 1 * sizeof(EXPLICIT_ACCESS));
  985. // The ACE will allow the Administrators group full access to the key.
  986. ea[0].grfAccessPermissions = KEY_ALL_ACCESS;
  987. ea[0].grfAccessMode = SET_ACCESS;
  988. ea[0].grfInheritance = NO_INHERITANCE;
  989. ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  990. ea[0].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  991. ea[0].Trustee.ptstrName = (LPTSTR) pSidAdmins;
  992. // Create a new ACL that contains the new ACEs.
  993. if (SetEntriesInAcl(1, ea, NULL, &pAcl) != ERROR_SUCCESS)
  994. {
  995. FreeSid(pSidAdmins);
  996. return NULL;
  997. }
  998. FreeSid(pSidAdmins);
  999. return pAcl;
  1000. }
  1001. void CSecurityPage::FreeACL( PACL pAcl)
  1002. {
  1003. if (pAcl != NULL)
  1004. LocalFree(pAcl);
  1005. }
  1006. BOOL CSecurityPage::SnmpAddAdminAclToKey(LPTSTR pszKey)
  1007. {
  1008. HKEY hKey = NULL;
  1009. LONG rc;
  1010. PACL pAcl = NULL;
  1011. SECURITY_DESCRIPTOR S_Desc;
  1012. if (pszKey == NULL)
  1013. return FALSE;
  1014. // open registy key
  1015. rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  1016. pszKey, // subkey name
  1017. 0, // reserved
  1018. KEY_ALL_ACCESS, // want WRITE_DAC,
  1019. &hKey // handle to open key
  1020. );
  1021. if (rc != ERROR_SUCCESS)
  1022. {
  1023. return FALSE;
  1024. }
  1025. // Initialize a security descriptor.
  1026. if (InitializeSecurityDescriptor (&S_Desc, SECURITY_DESCRIPTOR_REVISION) == 0)
  1027. {
  1028. RegCloseKey(hKey);
  1029. return FALSE;
  1030. }
  1031. // get the ACL and put it into the security descriptor
  1032. if ( (pAcl = AllocACL()) != NULL )
  1033. {
  1034. if (!SetSecurityDescriptorDacl (&S_Desc, TRUE, pAcl, FALSE))
  1035. {
  1036. FreeACL(pAcl);
  1037. RegCloseKey(hKey);
  1038. return FALSE;
  1039. }
  1040. }
  1041. else
  1042. {
  1043. RegCloseKey(hKey);
  1044. return FALSE;
  1045. }
  1046. if (RegSetKeySecurity (hKey, DACL_SECURITY_INFORMATION, &S_Desc) != ERROR_SUCCESS)
  1047. {
  1048. FreeACL(pAcl);
  1049. RegCloseKey(hKey);
  1050. return FALSE;
  1051. }
  1052. FreeACL(pAcl);
  1053. RegCloseKey(hKey);
  1054. return TRUE;
  1055. }
  1056. BOOL CSecurityPage::LoadRegistry()
  1057. {
  1058. RegKey rk;
  1059. LONG err;
  1060. HRESULT hr, hrIter;
  1061. RegValueIterator rvIter;
  1062. LPCTSTR lpcszMachineName = g_strMachineName.IsEmpty() ? NULL : (LPCTSTR)g_strMachineName;
  1063. BOOL fPolicy;
  1064. // we need to provide precedence to the parameters set through the policy
  1065. fPolicy = TRUE;
  1066. if (fPolicy)
  1067. {
  1068. err = rk.Open(HKEY_LOCAL_MACHINE,
  1069. POLICY_VALID_COMMUNITIES_KEY_NAME,
  1070. KEY_ALL_ACCESS,
  1071. lpcszMachineName);
  1072. if (err != ERROR_SUCCESS)
  1073. fPolicy = FALSE;
  1074. else
  1075. {
  1076. // remember that we are loading from policy registry
  1077. m_fPolicyValidCommunities = TRUE;
  1078. }
  1079. }
  1080. if (fPolicy == FALSE)
  1081. {
  1082. // Open or create the registry key
  1083. err = rk.Open(HKEY_LOCAL_MACHINE,
  1084. VALID_COMMUNITIES_KEY_NAME,
  1085. KEY_ALL_ACCESS,
  1086. lpcszMachineName);
  1087. if (err != ERROR_SUCCESS)
  1088. {
  1089. err = rk.Create(HKEY_LOCAL_MACHINE,
  1090. VALID_COMMUNITIES_KEY_NAME,
  1091. REG_OPTION_NON_VOLATILE,
  1092. KEY_ALL_ACCESS,
  1093. NULL,
  1094. lpcszMachineName);
  1095. if (err != ERROR_SUCCESS)
  1096. return FALSE;
  1097. SnmpAddAdminAclToKey(VALID_COMMUNITIES_KEY_NAME);
  1098. }
  1099. }
  1100. err = rvIter.Init(&rk);
  1101. if (err != ERROR_SUCCESS) {
  1102. return FALSE;
  1103. }
  1104. CString stValue;
  1105. DWORD dwType;
  1106. for (hr = rvIter.Next(&stValue, &dwType); hr == hrOK; hr = rvIter.Next(&stValue, &dwType)) {
  1107. CString strCommunity;
  1108. DWORD dwRights;
  1109. int nIndex;
  1110. char log[128];
  1111. CString Log;
  1112. if (dwType == REG_SZ)
  1113. {
  1114. err = rk.QueryValue(stValue, strCommunity);
  1115. if (err != ERROR_SUCCESS) {
  1116. return FALSE;
  1117. }
  1118. // BUG# 638837
  1119. // For registry value of type REG_SZ, SNMP service interprets it as
  1120. // READ ONLY community name.
  1121. dwRights = 1 << PERM_BIT_READONLY;
  1122. }
  1123. else if (dwType == REG_DWORD)
  1124. {
  1125. strCommunity = stValue;
  1126. err = rk.QueryValue(stValue, dwRights);
  1127. if (err != ERROR_SUCCESS) {
  1128. return FALSE;
  1129. }
  1130. }
  1131. else
  1132. return FALSE;
  1133. if ((nIndex = m_listboxCommunity.InsertString(-1, strCommunity)) >=0)
  1134. {
  1135. int nPermIndex = 0;
  1136. CString strPermName("?????");
  1137. for (nPermIndex = 0; (dwRights & ((DWORD)(-1)^1)) != 0; dwRights >>=1, nPermIndex++);
  1138. if (!strPermName.LoadString(IDS_PERM_NAME0 + nPermIndex))
  1139. return FALSE;
  1140. if (!m_listboxCommunity.SetItemText(nIndex, 1, strPermName) ||
  1141. !m_listboxCommunity.SetItemData(nIndex, nPermIndex))
  1142. {
  1143. return FALSE;
  1144. }
  1145. }
  1146. else
  1147. {
  1148. return FALSE;
  1149. }
  1150. }
  1151. if (m_listboxCommunity.GetCount()) {
  1152. m_listboxCommunity.SetCurSel(0);
  1153. }
  1154. rk.Close();
  1155. // we need to provide precedence to the parameters set through the policy
  1156. fPolicy = TRUE;
  1157. if (fPolicy)
  1158. {
  1159. err = rk.Open(HKEY_LOCAL_MACHINE,
  1160. POLICY_PERMITTED_MANAGERS_KEY_NAME,
  1161. KEY_ALL_ACCESS,
  1162. lpcszMachineName);
  1163. if (err != ERROR_SUCCESS)
  1164. fPolicy = FALSE;
  1165. else
  1166. {
  1167. // remember that we are loading from policy registry
  1168. m_fPolicyPermittedManagers = TRUE;
  1169. }
  1170. }
  1171. if (fPolicy == FALSE)
  1172. {
  1173. // Open or create the registry key
  1174. err = rk.Open(HKEY_LOCAL_MACHINE,
  1175. PERMITTED_MANAGERS_KEY_NAME,
  1176. KEY_ALL_ACCESS,
  1177. lpcszMachineName);
  1178. if (err != ERROR_SUCCESS)
  1179. {
  1180. err = rk.Create(HKEY_LOCAL_MACHINE,
  1181. PERMITTED_MANAGERS_KEY_NAME,
  1182. REG_OPTION_NON_VOLATILE,
  1183. KEY_ALL_ACCESS,
  1184. NULL,
  1185. lpcszMachineName);
  1186. if (err != ERROR_SUCCESS)
  1187. return FALSE;
  1188. SnmpAddAdminAclToKey(PERMITTED_MANAGERS_KEY_NAME);
  1189. }
  1190. }
  1191. err = rvIter.Init(&rk);
  1192. if (err != ERROR_SUCCESS) {
  1193. return FALSE;
  1194. }
  1195. for (hr = rvIter.Next(&stValue, NULL); hr == hrOK; hr = rvIter.Next(&stValue, NULL)) {
  1196. CString strHost;
  1197. err = rk.QueryValue(stValue, strHost);
  1198. if (err != ERROR_SUCCESS) {
  1199. return FALSE;
  1200. }
  1201. m_listboxHost.InsertString(-1, strHost);
  1202. }
  1203. if (m_listboxHost.GetCount()) {
  1204. m_radioAcceptSpecificHost.SetCheck(1);
  1205. m_listboxHost.SetCurSel(0);
  1206. }
  1207. else
  1208. {
  1209. m_radioAcceptAnyHost.SetCheck(1);
  1210. }
  1211. rk.Close();
  1212. // Open or create the registry key
  1213. err = rk.Open(HKEY_LOCAL_MACHINE,
  1214. SNMP_PARAMS_KEY_NAME,
  1215. KEY_ALL_ACCESS,
  1216. lpcszMachineName);
  1217. if (err != ERROR_SUCCESS)
  1218. {
  1219. // This can't fail as far as all the other keys could be opened before
  1220. // however ... just to make sure
  1221. return FALSE;
  1222. }
  1223. DWORD dwSwitch;
  1224. // get the value for the authentication trap flag
  1225. err = rk.QueryValue(ENABLE_AUTH_TRAPS, dwSwitch);
  1226. if (err != ERROR_SUCCESS)
  1227. return FALSE;
  1228. m_checkSendAuthTrap.SetCheck(dwSwitch);
  1229. return TRUE;
  1230. }
  1231. BOOL CSecurityPage::SaveRegistry()
  1232. {
  1233. RegKey rk;
  1234. LONG err;
  1235. CString strContact;
  1236. CString strLocation;
  1237. DWORD dwServices = 0;
  1238. LPCTSTR lpcszMachineName = g_strMachineName.IsEmpty() ? NULL : (LPCTSTR)g_strMachineName;
  1239. if (m_fPolicyValidCommunities == FALSE)
  1240. {
  1241. // Open or create the registry key
  1242. err = rk.Open(HKEY_LOCAL_MACHINE,
  1243. SNMP_PARAMS_KEY_NAME,
  1244. KEY_ALL_ACCESS,
  1245. lpcszMachineName);
  1246. if (err != ERROR_SUCCESS)
  1247. {
  1248. return FALSE;
  1249. }
  1250. // If it already exists, delete the key first
  1251. err = rk.RecurseDeleteKey(VALID_COMMUNITIES);
  1252. if (err != ERROR_SUCCESS) {
  1253. return FALSE;
  1254. }
  1255. // recreate the key now
  1256. err = rk.Create(HKEY_LOCAL_MACHINE,
  1257. VALID_COMMUNITIES_KEY_NAME,
  1258. REG_OPTION_NON_VOLATILE,
  1259. KEY_ALL_ACCESS,
  1260. NULL,
  1261. lpcszMachineName);
  1262. if (err != ERROR_SUCCESS)
  1263. return FALSE;
  1264. SnmpAddAdminAclToKey(VALID_COMMUNITIES_KEY_NAME);
  1265. int nCount = m_listboxCommunity.GetCount();
  1266. for (int i = 0; i < nCount; i++) {
  1267. DWORD dwPermissions;
  1268. CString strCommunity;
  1269. m_listboxCommunity.GetText(i, strCommunity);
  1270. dwPermissions = 1 << (DWORD) m_listboxCommunity.GetItemData(i);
  1271. err = rk.SetValue(strCommunity, dwPermissions);
  1272. if (err != ERROR_SUCCESS) {
  1273. return FALSE;
  1274. }
  1275. }
  1276. rk.Close();
  1277. }
  1278. if (m_fPolicyPermittedManagers == FALSE)
  1279. {
  1280. // Open or create the registry key
  1281. err = rk.Open(HKEY_LOCAL_MACHINE,
  1282. SNMP_PARAMS_KEY_NAME,
  1283. KEY_ALL_ACCESS,
  1284. lpcszMachineName);
  1285. if (err != ERROR_SUCCESS)
  1286. {
  1287. }
  1288. // delete the key first
  1289. err = rk.RecurseDeleteKey(PERMITTED_MANAGERS);
  1290. if (err != ERROR_SUCCESS) {
  1291. return FALSE;
  1292. }
  1293. // recreate the key now
  1294. err = rk.Create(HKEY_LOCAL_MACHINE,
  1295. PERMITTED_MANAGERS_KEY_NAME,
  1296. REG_OPTION_NON_VOLATILE,
  1297. KEY_ALL_ACCESS,
  1298. NULL,
  1299. lpcszMachineName);
  1300. if (err != ERROR_SUCCESS)
  1301. return FALSE;
  1302. SnmpAddAdminAclToKey(PERMITTED_MANAGERS_KEY_NAME);
  1303. int nCount = m_listboxHost.GetCount();
  1304. for (int i = 0; i < nCount; i++) {
  1305. TCHAR buffer[32];
  1306. CString strHost;
  1307. wsprintf(buffer, TEXT("%d"), i+1);
  1308. m_listboxHost.GetText(i, strHost);
  1309. err = rk.SetValue(buffer, strHost);
  1310. if (err != ERROR_SUCCESS) {
  1311. return FALSE;
  1312. }
  1313. }
  1314. rk.Close();
  1315. }
  1316. // Open or create the registry key
  1317. err = rk.Open(HKEY_LOCAL_MACHINE,
  1318. SNMP_PARAMS_KEY_NAME,
  1319. KEY_ALL_ACCESS,
  1320. lpcszMachineName);
  1321. if (err != ERROR_SUCCESS)
  1322. {
  1323. return FALSE;
  1324. }
  1325. DWORD dwSwitch = (m_checkSendAuthTrap.GetCheck() ? 0x1 : 0 ) ;
  1326. err = rk.SetValue(ENABLE_AUTH_TRAPS, dwSwitch);
  1327. if (err != ERROR_SUCCESS)
  1328. return FALSE;
  1329. return TRUE;
  1330. }
  1331. /////////////////////////////////////////////////////////////////////////////
  1332. // CAddDialog dialog
  1333. CAddDialog::CAddDialog(CWnd* pParent /*=NULL*/)
  1334. : CBaseDialog(CAddDialog::IDD, pParent)
  1335. {
  1336. m_bCommunity = FALSE;
  1337. m_nChoice = 0;
  1338. //{{AFX_DATA_INIT(CAddDialog)
  1339. //}}AFX_DATA_INIT
  1340. }
  1341. void CAddDialog::DoDataExchange(CDataExchange* pDX)
  1342. {
  1343. CBaseDialog::DoDataExchange(pDX);
  1344. //{{AFX_DATA_MAP(CAddDialog)
  1345. DDX_Control(pDX, IDC_EDIT_NAME, m_editName);
  1346. DDX_Control(pDX, IDC_ADD, m_buttonAdd);
  1347. DDX_Control(pDX, IDCANCEL, m_buttonCancel);
  1348. DDX_Control(pDX, IDC_STATIC_ADD_TEXT, m_staticText);
  1349. DDX_Control(pDX, IDC_ST_PERMISSIONS, m_staticPermissions);
  1350. DDX_Control(pDX, IDC_CB_PERMISSIONS, m_comboPermissions);
  1351. //}}AFX_DATA_MAP
  1352. }
  1353. BEGIN_MESSAGE_MAP(CAddDialog, CBaseDialog)
  1354. //{{AFX_MSG_MAP(CAddDialog)
  1355. ON_BN_CLICKED(IDC_ADD, OnClickedButtonAdd)
  1356. //}}AFX_MSG_MAP
  1357. END_MESSAGE_MAP()
  1358. /////////////////////////////////////////////////////////////////////////////
  1359. // CAddDialog message handlers
  1360. BOOL CAddDialog::OnInitDialog()
  1361. {
  1362. CBaseDialog::OnInitDialog();
  1363. m_editName.SetLimitText(COMBO_EDIT_LEN-1);
  1364. m_editName.SetWindowText(m_strName);
  1365. m_editName.SetFocus();
  1366. m_editName.SetSel(0, -1);
  1367. if (m_bCommunity) {
  1368. CString strText;
  1369. strText.LoadString( IDS_SNMPCOMM_TEXT );
  1370. m_staticPermissions.ShowWindow(SW_SHOW);
  1371. m_comboPermissions.ShowWindow(SW_SHOW);
  1372. m_comboPermissions.ResetContent();
  1373. for (int i = 0; i<N_PERMISSION_BITS; i++)
  1374. {
  1375. CString strPermName;
  1376. int idx;
  1377. if (!strPermName.LoadString(IDS_PERM_NAME0 + i) ||
  1378. (idx=m_comboPermissions.AddString(strPermName))<0 ||
  1379. !m_comboPermissions.SetItemData(idx, i))
  1380. return FALSE;
  1381. }
  1382. m_comboPermissions.SetCurSel(m_nChoice);
  1383. // change static text of dialog to "Community Name"
  1384. m_staticText.SetWindowText(strText);
  1385. }
  1386. return FALSE; // return TRUE unless you set the focus to a control
  1387. // EXCEPTION: OCX Property Pages should return FALSE
  1388. }
  1389. void CAddDialog::OnClickedButtonAdd()
  1390. {
  1391. m_editName.GetWindowText(m_strName);
  1392. if (!m_bCommunity && IsValidString(m_strName) == FALSE)
  1393. {
  1394. m_editName.SetFocus();
  1395. m_editName.SetSel(0,-1);
  1396. return ;
  1397. }
  1398. if (m_bCommunity)
  1399. {
  1400. m_nChoice = m_comboPermissions.GetCurSel();
  1401. m_comboPermissions.GetWindowText(m_strChoice);
  1402. }
  1403. CBaseDialog::OnOK();
  1404. }
  1405. DWORD * CAddDialog::GetHelpMap()
  1406. {
  1407. return m_bCommunity ?
  1408. (DWORD *) &g_aHelpIDs_IDD_DIALOG_ADD_COMM[0]:
  1409. (DWORD *) &g_aHelpIDs_IDD_DIALOG_ADD_ADDR[0];
  1410. }
  1411. /////////////////////////////////////////////////////////////////////////////
  1412. // CEditDialog dialog
  1413. CEditDialog::CEditDialog(CWnd* pParent /*=NULL*/)
  1414. : CBaseDialog(CEditDialog::IDD, pParent)
  1415. {
  1416. m_bCommunity = FALSE;
  1417. m_nChoice = 0;
  1418. //{{AFX_DATA_INIT(CEditDialog)
  1419. //}}AFX_DATA_INIT
  1420. }
  1421. void CEditDialog::DoDataExchange(CDataExchange* pDX)
  1422. {
  1423. CBaseDialog::DoDataExchange(pDX);
  1424. //{{AFX_DATA_MAP(CEditDialog)
  1425. DDX_Control(pDX, IDC_EDIT_NAME, m_editName);
  1426. DDX_Control(pDX, IDOK, m_buttonOk);
  1427. DDX_Control(pDX, IDCANCEL, m_buttonCancel);
  1428. DDX_Control(pDX, IDC_STATIC_EDIT_TEXT, m_staticText);
  1429. DDX_Control(pDX, IDC_ST_PERMISSIONS, m_staticPermissions);
  1430. DDX_Control(pDX, IDC_CB_PERMISSIONS, m_comboPermissions);
  1431. //}}AFX_DATA_MAP
  1432. }
  1433. BEGIN_MESSAGE_MAP(CEditDialog, CBaseDialog)
  1434. //{{AFX_MSG_MAP(CEditDialog)
  1435. //}}AFX_MSG_MAP
  1436. END_MESSAGE_MAP()
  1437. /////////////////////////////////////////////////////////////////////////////
  1438. // CEditDialog message handlers
  1439. BOOL CEditDialog::OnInitDialog()
  1440. {
  1441. CBaseDialog::OnInitDialog();
  1442. m_editName.SetLimitText(COMBO_EDIT_LEN-1);
  1443. m_editName.SetWindowText(m_strName);
  1444. m_editName.SetFocus();
  1445. // select the entire string in the edit control
  1446. m_editName.SetSel(0, -1);
  1447. if (m_bCommunity) {
  1448. CString strText;
  1449. strText.LoadString( IDS_SNMPCOMM_TEXT );
  1450. m_staticPermissions.ShowWindow(SW_SHOW);
  1451. m_comboPermissions.ShowWindow(SW_SHOW);
  1452. m_comboPermissions.ResetContent();
  1453. for (int i = 0; i<N_PERMISSION_BITS; i++)
  1454. {
  1455. CString strPermName;
  1456. int idx;
  1457. if (!strPermName.LoadString(IDS_PERM_NAME0 + i) ||
  1458. (idx=m_comboPermissions.AddString(strPermName))<0 ||
  1459. !m_comboPermissions.SetItemData(idx, i))
  1460. {
  1461. AfxMessageBox(strPermName);
  1462. return FALSE;
  1463. }
  1464. }
  1465. m_comboPermissions.SetCurSel(m_nChoice);
  1466. // change static text of dialog to "Community Name"
  1467. m_staticText.SetWindowText(strText);
  1468. }
  1469. return FALSE; // return TRUE unless you set the focus to a control
  1470. // EXCEPTION: OCX Property Pages should return FALSE
  1471. }
  1472. void CEditDialog::OnOK()
  1473. {
  1474. m_editName.GetWindowText(m_strName);
  1475. if (!m_bCommunity) {
  1476. if (IsValidString(m_strName) == FALSE)
  1477. {
  1478. m_editName.SetFocus();
  1479. return ;
  1480. }
  1481. }
  1482. else
  1483. {
  1484. m_comboPermissions.GetWindowText(m_strChoice);
  1485. m_nChoice = m_comboPermissions.GetCurSel();
  1486. }
  1487. CBaseDialog::OnOK();
  1488. }
  1489. DWORD * CEditDialog::GetHelpMap()
  1490. {
  1491. return m_bCommunity ?
  1492. (DWORD *) &g_aHelpIDs_IDD_DIALOG_EDIT_COMM[0] :
  1493. (DWORD *) &g_aHelpIDs_IDD_DIALOG_EDIT_ADDR[0];
  1494. }
  1495. BOOL IsValidString(CString & strName)
  1496. {
  1497. BOOL bResult = FALSE;
  1498. // check if this is a valid IP host name or address
  1499. if (ValidateDomain(strName) == TRUE)
  1500. return(TRUE);
  1501. int nLen = strName.GetLength();
  1502. if (nLen == 0 || nLen > 12) {
  1503. CString strMsg;
  1504. strMsg.FormatMessage(IDS_SNMP_INVALID_IP_IPX_ADD, strName);
  1505. AfxMessageBox(strMsg, MB_ICONEXCLAMATION|MB_OK);
  1506. return FALSE;
  1507. }
  1508. LPTSTR buffer = strName.GetBuffer(64);
  1509. for (int i = 0; i < nLen; i++) {
  1510. if (!iswxdigit((int) buffer[i])) {
  1511. CString strMsg;
  1512. strName.ReleaseBuffer();
  1513. strMsg.FormatMessage(IDS_SNMP_INVALID_IP_IPX_ADD, strName);
  1514. AfxMessageBox(strMsg, MB_ICONEXCLAMATION|MB_OK);
  1515. return FALSE;
  1516. }
  1517. }
  1518. return TRUE;
  1519. }
  1520. BOOL ValidateDomain(CString & strDomain)
  1521. {
  1522. int nLen;
  1523. if ((nLen = strDomain.GetLength()) != 0)
  1524. {
  1525. if (nLen < DOMAINNAME_LENGTH)
  1526. {
  1527. int i;
  1528. TCHAR ch;
  1529. BOOL fLet_Dig = FALSE;
  1530. BOOL fDot = FALSE;
  1531. int cHostname = 0;
  1532. LPTSTR buffer = strDomain.GetBuffer(64);
  1533. for (i = 0; i < nLen; i++)
  1534. {
  1535. // check each character
  1536. ch = buffer[i];
  1537. BOOL fAlNum = iswalpha(ch) || iswdigit(ch);
  1538. if (((i == 0) && !fAlNum) ||
  1539. // first letter must be a digit or a letter
  1540. (fDot && !fAlNum) ||
  1541. // first letter after dot must be a digit or a letter
  1542. ((i == (nLen - 1)) && !fAlNum) ||
  1543. // last letter must be a letter or a digit
  1544. (!fAlNum && ( ch != _T('-') && ( ch != _T('.') && ( ch != _T('_'))))) ||
  1545. // must be letter, digit, - or "."
  1546. (( ch == _T('.')) && ( !fLet_Dig )))
  1547. // must be letter or digit before '.'
  1548. {
  1549. return FALSE;
  1550. }
  1551. fLet_Dig = fAlNum;
  1552. fDot = (ch == _T('.'));
  1553. cHostname++;
  1554. if ( cHostname > HOSTNAME_LENGTH )
  1555. {
  1556. return FALSE;
  1557. }
  1558. if ( fDot )
  1559. {
  1560. cHostname = 0;
  1561. }
  1562. }
  1563. }
  1564. }
  1565. else
  1566. {
  1567. return FALSE;
  1568. }
  1569. return TRUE;
  1570. }
  1571. void CSecurityPage::OnDblclkCtrlistCommunity(NMHDR* pNMHDR, LRESULT* pResult)
  1572. {
  1573. if (m_fPolicyValidCommunities == FALSE)
  1574. OnClickedButtonEditCommunity();
  1575. *pResult = 0;
  1576. }
  1577. void CSecurityPage::OnCommunityListChanged(NMHDR* pNMHDR, LRESULT* pResult)
  1578. {
  1579. INT nIndex;
  1580. NMLVODSTATECHANGE* pStateChanged = (NMLVODSTATECHANGE*)pNMHDR;
  1581. // TODO: Add your control notification handler code here
  1582. nIndex = m_listboxCommunity.GetCurSel();
  1583. m_buttonEditCommunity.EnableWindow(nIndex >= 0);
  1584. m_buttonRemoveCommunity.EnableWindow(nIndex >= 0);
  1585. if (m_fPolicyValidCommunities)
  1586. {
  1587. m_buttonEditCommunity.EnableWindow(FALSE);
  1588. m_buttonRemoveCommunity.EnableWindow(FALSE);
  1589. }
  1590. *pResult = 0;
  1591. }