Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1923 lines
50 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()) {
  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. // UpdateTrapDestinationButtons();
  516. }
  517. /*
  518. Handle the ON_CBN_EDITUPDATE message.
  519. The user has taken an action that may have altered the text in the
  520. edit-control portion of a combo box.
  521. */
  522. void CTrapsPage::OnEditUpdateCommunityName()
  523. {
  524. UpdateCommunityAddButton();
  525. m_buttonRemoveName.EnableWindow(FALSE);
  526. m_buttonAddTrap.EnableWindow(FALSE);
  527. }
  528. void CTrapsPage::OnSelectionChangeCommunityName()
  529. // User has changed the community name selection
  530. // load the corresponding trap destination entries in the listbox
  531. {
  532. int nIndex = m_comboCommunityName.GetCurSel();
  533. if (nIndex != CB_ERR) {
  534. LoadTrapDestination(nIndex);
  535. }
  536. else // there are no items in the combobox
  537. {
  538. m_listboxTrapDestinations.ResetContent();
  539. }
  540. UpdateCommunityRemoveButton();
  541. UpdateTrapDestinationButtons();
  542. }
  543. void CTrapsPage::OnClickedButtonAddName()
  544. {
  545. CString strCommunityName;
  546. m_comboCommunityName.GetWindowText(strCommunityName);
  547. int nIndex = m_comboCommunityName.InsertString(-1, strCommunityName);
  548. if (nIndex >= 0 ) {
  549. // Create a Trap list item and add to global list
  550. // the items added to the trap destination will be inserted in
  551. // this list - see OnClickedAddbuttonAddTrap
  552. CStringList * pstrList = new CStringList;
  553. m_pCommunityList->AddHead(pstrList);
  554. // save list pointer with data
  555. m_comboCommunityName.SetItemData(nIndex, (ULONG_PTR) pstrList);
  556. m_comboCommunityName.SetCurSel(nIndex);
  557. LoadTrapDestination(nIndex);
  558. UpdateCommunityAddButton();
  559. UpdateCommunityRemoveButton();
  560. UpdateTrapDestinationButtons();
  561. SetDirty(TRUE);
  562. }
  563. }
  564. void CTrapsPage::OnClickedButtonRemoveName()
  565. {
  566. int nIndex = m_comboCommunityName.GetCurSel();
  567. ASSERT(nIndex >= 0 );
  568. POSITION pos;
  569. if (nIndex != CB_ERR) {
  570. ASSERT (m_pCommunityList);
  571. CStringList * pstrList, * pstrList2;
  572. pstrList = (CStringList *) m_comboCommunityName.GetItemData(nIndex);
  573. pos = m_pCommunityList->Find( pstrList);
  574. if( pos ) {
  575. pstrList2 = (CStringList *) & m_pCommunityList->GetAt(pos);
  576. // delete the item from the global list
  577. m_pCommunityList->RemoveAt(pos);
  578. delete pstrList;
  579. }
  580. // remove the item from the Combo Box
  581. m_comboCommunityName.DeleteString(nIndex);
  582. // select item 0 if there are any left
  583. if (m_comboCommunityName.GetCount()) {
  584. m_comboCommunityName.SetCurSel(0);
  585. }
  586. else
  587. m_comboCommunityName.SetWindowText(_T(""));
  588. OnSelectionChangeCommunityName();
  589. UpdateCommunityRemoveButton();
  590. UpdateTrapDestinationButtons();
  591. SetDirty(TRUE);
  592. }
  593. }
  594. void CTrapsPage::OnClickedButtonAddTrap()
  595. {
  596. if (m_dlgAdd.DoModal() == IDOK)
  597. {
  598. if (m_dlgAdd.m_strName.GetLength()) {
  599. int nIndex = m_comboCommunityName.GetCurSel();
  600. if (nIndex >= 0) {
  601. ULONG_PTR dwData = m_comboCommunityName.GetItemData(nIndex);
  602. if (dwData != CB_ERR) {
  603. CStringList * pstrList = (CStringList *) dwData;
  604. nIndex = m_listboxTrapDestinations.InsertString(-1, m_dlgAdd.m_strName);
  605. if (nIndex >=0) {
  606. SetDirty(TRUE);
  607. m_listboxTrapDestinations.SetCurSel(nIndex);
  608. CString * pstr = new CString(m_dlgAdd.m_strName);
  609. pstrList->AddHead(*pstr);
  610. }
  611. }
  612. }
  613. }
  614. }
  615. UpdateTrapDestinationButtons();
  616. }
  617. void CTrapsPage::OnClickedButtonEditTrap()
  618. {
  619. int nLBIndex = m_listboxTrapDestinations.GetCurSel();
  620. int nCBIndex = m_comboCommunityName.GetCurSel();
  621. if (nCBIndex >=0 && nLBIndex >=0) {
  622. CString strTrap;
  623. m_listboxTrapDestinations.GetText(nLBIndex, strTrap);
  624. ASSERT( strTrap.GetLength() != 0 );
  625. m_dlgEdit.m_strName = strTrap;
  626. // present dialog
  627. if (m_dlgEdit.DoModal() == IDOK)
  628. {
  629. if (m_dlgEdit.m_strName.GetLength()) {
  630. // remove item and then add it
  631. OnClickedButtonRemoveTrap();
  632. ULONG_PTR dwData = m_comboCommunityName.GetItemData(nCBIndex);
  633. if (dwData != CB_ERR) {
  634. CStringList * pstrList = (CStringList *) dwData;
  635. nLBIndex = m_listboxTrapDestinations.InsertString(-1, m_dlgEdit.m_strName);
  636. if (nLBIndex >=0) {
  637. SetDirty(TRUE);
  638. m_listboxTrapDestinations.SetCurSel(nLBIndex);
  639. CString * pstr = new CString(m_dlgEdit.m_strName);
  640. pstrList->AddHead(*pstr);
  641. }
  642. }
  643. }
  644. }
  645. }
  646. UpdateTrapDestinationButtons();
  647. }
  648. void CTrapsPage::OnClickedButtonRemoveTrap()
  649. {
  650. int nLBIndex = m_listboxTrapDestinations.GetCurSel();
  651. int nCBIndex = m_comboCommunityName.GetCurSel();
  652. if (nCBIndex >=0 && nLBIndex >=0) {
  653. ULONG_PTR dwData = m_comboCommunityName.GetItemData(nCBIndex);
  654. ASSERT (dwData != CB_ERR);
  655. CString strTrap;
  656. m_listboxTrapDestinations.GetText(nLBIndex, strTrap);
  657. ASSERT( strTrap.GetLength() != 0 );
  658. if (strTrap.GetLength() != NULL && dwData != CB_ERR) {
  659. m_listboxTrapDestinations.DeleteString(nLBIndex);
  660. SetDirty(TRUE);
  661. CStringList * pStrList = (CStringList*) dwData;
  662. // find the matching string and remove
  663. POSITION pos = pStrList->Find(strTrap);
  664. pStrList->RemoveAt(pos);
  665. m_dlgAdd.m_strName = strTrap; // save off item removed
  666. int nCount = m_listboxTrapDestinations.GetCount();
  667. if (nCount) {
  668. if (nCount != nLBIndex) {
  669. m_listboxTrapDestinations.SetCurSel(nLBIndex);
  670. }
  671. else
  672. {
  673. m_listboxTrapDestinations.SetCurSel(nCount-1);
  674. }
  675. }
  676. }
  677. }
  678. UpdateTrapDestinationButtons();
  679. }
  680. IMPLEMENT_DYNCREATE(CSecurityPage, CPropertyPageBase)
  681. CSecurityPage::CSecurityPage()
  682. : CPropertyPageBase(CSecurityPage::IDD)
  683. {
  684. //{{AFX_DATA_INIT(CSecurityPage)
  685. //}}AFX_DATA_INIT
  686. m_fPolicyValidCommunities = m_fPolicyPermittedManagers = FALSE;
  687. }
  688. CSecurityPage::~CSecurityPage()
  689. {
  690. }
  691. void CSecurityPage::DoDataExchange(CDataExchange* pDX)
  692. {
  693. CPropertyPageBase::DoDataExchange(pDX);
  694. //{{AFX_DATA_MAP(CSecurityPage)
  695. DDX_Control(pDX, IDC_LIST_COMMUNITY, m_listboxCommunity);
  696. DDX_Control(pDX, IDC_BUTTON_ADD_COMMUNITY, m_buttonAddCommunity);
  697. DDX_Control(pDX, IDC_BUTTON_EDIT_COMMUNITY, m_buttonEditCommunity);
  698. DDX_Control(pDX, IDC_BUTTON_REMOVE_COMMUNITY, m_buttonRemoveCommunity);
  699. DDX_Control(pDX, IDC_BUTTON_ADD_HOSTS, m_buttonAddHost);
  700. DDX_Control(pDX, IDC_BUTTON_EDIT_HOSTS, m_buttonEditHost);
  701. DDX_Control(pDX, IDC_BUTTON_REMOVE_HOSTS, m_buttonRemoveHost);
  702. DDX_Control(pDX, IDC_LIST_HOSTS, m_listboxHost);
  703. DDX_Control(pDX, IDC_CHECK_SEND_AUTH_TRAP, m_checkSendAuthTrap);
  704. DDX_Control(pDX, IDC_RADIO_ACCEPT_ANY, m_radioAcceptAnyHost);
  705. DDX_Control(pDX, IDC_RADIO_ACCEPT_SPECIFIC_HOSTS, m_radioAcceptSpecificHost);
  706. //}}AFX_DATA_MAP
  707. }
  708. BEGIN_MESSAGE_MAP(CSecurityPage, CPropertyPageBase)
  709. //{{AFX_MSG_MAP(CSecurityPage)
  710. ON_BN_CLICKED(IDC_BUTTON_ADD_COMMUNITY, OnClickedButtonAddCommunity)
  711. ON_BN_CLICKED(IDC_BUTTON_EDIT_COMMUNITY, OnClickedButtonEditCommunity)
  712. ON_BN_CLICKED(IDC_BUTTON_REMOVE_COMMUNITY, OnClickedButtonRemoveCommunity)
  713. ON_BN_CLICKED(IDC_BUTTON_ADD_HOSTS, OnClickedButtonAddHost)
  714. ON_BN_CLICKED(IDC_BUTTON_EDIT_HOSTS, OnClickedButtonEditHost)
  715. ON_BN_CLICKED(IDC_BUTTON_REMOVE_HOSTS, OnClickedButtonRemoveHost)
  716. ON_BN_CLICKED(IDC_CHECK_SEND_AUTH_TRAP, OnClickedCheckSendAuthTrap)
  717. ON_BN_CLICKED(IDC_RADIO_ACCEPT_ANY, OnClickedRadioAcceptAnyHost)
  718. ON_BN_CLICKED(IDC_RADIO_ACCEPT_SPECIFIC_HOSTS, OnClickedRadioAcceptSpecificHost)
  719. ON_NOTIFY(NM_DBLCLK, IDC_LIST_COMMUNITY, OnDblclkCtrlistCommunity)
  720. ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_COMMUNITY, OnCommunityListChanged)
  721. //}}AFX_MSG_MAP
  722. END_MESSAGE_MAP()
  723. /////////////////////////////////////////////////////////////////////////////
  724. // CSecurityPage message handlers
  725. BOOL CSecurityPage::OnInitDialog()
  726. {
  727. CPropertyPageBase::OnInitDialog();
  728. m_listboxCommunity.OnInitList();
  729. if (LoadRegistry() == FALSE) {
  730. AfxMessageBox(IDS_REGISTRY_LOAD_FAILED, MB_ICONSTOP|MB_OK);
  731. }
  732. UpdateNameButtons();
  733. UpdateHostButtons();
  734. return TRUE; // return TRUE unless you set the focus to a control
  735. // EXCEPTION: OCX Property Pages should return FALSE
  736. }
  737. BOOL CSecurityPage::OnApply()
  738. {
  739. UpdateData();
  740. if (m_radioAcceptSpecificHost.GetCheck()) {
  741. if (m_listboxHost.GetCount() == 0) {
  742. AfxMessageBox(IDS_ACCEPTHOST_MISSING, MB_ICONSTOP|MB_OK);
  743. return FALSE;
  744. }
  745. }
  746. if( SaveRegistry() == FALSE )
  747. {
  748. AfxMessageBox(IDS_REGISTRY_SAVE_FAILED, MB_ICONSTOP|MB_OK);
  749. return FALSE;
  750. }
  751. BOOL bRet = CPropertyPageBase::OnApply();
  752. if (bRet == FALSE)
  753. {
  754. // Something bad happened... grab the error code
  755. AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
  756. // ::DhcpMessageBox(GetHolder()->GetError());
  757. }
  758. return bRet;
  759. }
  760. void CSecurityPage::OnClickedButtonAddCommunity()
  761. {
  762. CString strText;
  763. strText.LoadString( IDS_SNMPCOMM_TEXT );
  764. // change static text of dialog to "Community Name"
  765. m_dlgAddName.m_bCommunity = TRUE;
  766. m_dlgAddName.m_nChoice = PERM_BIT_READONLY;
  767. if (m_dlgAddName.DoModal() == IDOK)
  768. {
  769. int nIndex;
  770. if (m_dlgAddName.m_strName.GetLength()) {
  771. if ((nIndex = m_listboxCommunity.InsertString(-1, m_dlgAddName.m_strName)) >=0 &&
  772. m_listboxCommunity.SetItemText(nIndex, 1, m_dlgAddName.m_strChoice) &&
  773. m_listboxCommunity.SetItemData(nIndex, m_dlgAddName.m_nChoice))
  774. {
  775. m_listboxCommunity.SetCurSel(nIndex);
  776. UpdateNameButtons();
  777. SetDirty(TRUE);
  778. }
  779. }
  780. }
  781. }
  782. void CSecurityPage::OnClickedButtonEditCommunity()
  783. {
  784. int nIndex;
  785. int nPermissions;
  786. nIndex = m_listboxCommunity.GetCurSel();
  787. ASSERT(nIndex >= 0);
  788. if (nIndex < 0) {
  789. return;
  790. }
  791. nPermissions = (int) m_listboxCommunity.GetItemData(nIndex);
  792. CString strName;
  793. m_listboxCommunity.GetText(nIndex, strName);
  794. // save off old string
  795. m_dlgAddName.m_strName = strName;
  796. m_dlgAddName.m_nChoice = nPermissions;
  797. m_dlgEditName.m_bCommunity = TRUE;
  798. m_dlgEditName.m_strName = strName;
  799. m_dlgEditName.m_nChoice = nPermissions;
  800. if (m_dlgEditName.DoModal() == IDOK) {
  801. // first delete the string from list box
  802. m_listboxCommunity.DeleteString(nIndex);
  803. SetDirty(TRUE);
  804. if ((nIndex = m_listboxCommunity.InsertString(nIndex, m_dlgEditName.m_strName)) >= 0 &&
  805. m_listboxCommunity.SetItemText(nIndex, 1, m_dlgEditName.m_strChoice) &&
  806. m_listboxCommunity.SetItemData(nIndex, m_dlgEditName.m_nChoice))
  807. {
  808. m_listboxCommunity.SetCurSel(nIndex);
  809. }
  810. }
  811. }
  812. void CSecurityPage::OnClickedButtonRemoveCommunity()
  813. {
  814. int nIndex = m_listboxCommunity.GetCurSel();
  815. if (nIndex < 0)
  816. return;
  817. CString strName;
  818. m_listboxCommunity.GetText(nIndex, strName);
  819. // save off removed name for quick add
  820. m_dlgAddName.m_strName = strName;
  821. m_listboxCommunity.DeleteString(nIndex);
  822. int nCount = m_listboxCommunity.GetCount();
  823. if (nCount != nIndex) {
  824. m_listboxCommunity.SetCurSel(nIndex);
  825. }
  826. else
  827. {
  828. m_listboxCommunity.SetCurSel(nCount -1);
  829. }
  830. UpdateNameButtons();
  831. SetDirty(TRUE);
  832. }
  833. void CSecurityPage::UpdateNameButtons()
  834. {
  835. int nCount = m_listboxCommunity.GetCount();
  836. m_buttonEditCommunity.EnableWindow(nCount? TRUE: FALSE);
  837. m_buttonRemoveCommunity.EnableWindow(nCount? TRUE: FALSE);
  838. if (m_fPolicyValidCommunities)
  839. {
  840. m_buttonEditCommunity.EnableWindow(FALSE);
  841. m_buttonRemoveCommunity.EnableWindow(FALSE);
  842. m_buttonAddCommunity.EnableWindow(FALSE);
  843. }
  844. }
  845. void CSecurityPage::OnClickedButtonAddHost()
  846. {
  847. CString strHostName;
  848. if (m_dlgAddHost.DoModal() == IDOK) {
  849. int nIndex;
  850. if ((nIndex = m_listboxHost.InsertString(-1, m_dlgAddHost.m_strName)) >=0) {
  851. m_listboxHost.SetCurSel(nIndex);
  852. m_radioAcceptSpecificHost.SetCheck(1);
  853. m_radioAcceptAnyHost.SetCheck(0);
  854. UpdateHostButtons();
  855. SetDirty(TRUE);
  856. }
  857. }
  858. }
  859. void CSecurityPage::OnClickedButtonEditHost()
  860. {
  861. int nIndex;
  862. nIndex = m_listboxHost.GetCurSel();
  863. ASSERT( nIndex >= 0 );
  864. if (nIndex < 0) {
  865. return;
  866. }
  867. CString strHost;
  868. m_listboxHost.GetText(nIndex, strHost);
  869. // save off old host name for quick add
  870. m_dlgAddHost.m_strName = strHost;
  871. m_dlgEditHost.m_strName = strHost;
  872. if (m_dlgEditHost.DoModal() == IDOK) {
  873. m_listboxHost.DeleteString(nIndex);
  874. SetDirty(TRUE);
  875. if ((nIndex = m_listboxHost.InsertString(-1, m_dlgEditHost.m_strName)) >= 0) {
  876. m_listboxHost.SetCurSel(nIndex);
  877. }
  878. }
  879. }
  880. void CSecurityPage::OnClickedButtonRemoveHost()
  881. {
  882. int nIndex = m_listboxHost.GetCurSel();
  883. ASSERT(nIndex >= 0);
  884. CString strHost;
  885. m_listboxHost.GetText(nIndex, strHost);
  886. // save off removed host name for quick add
  887. m_dlgAddHost.m_strName = strHost;
  888. m_listboxHost.DeleteString(nIndex);
  889. int nCount = m_listboxHost.GetCount();
  890. if (nCount != nIndex) {
  891. m_listboxHost.SetCurSel(nIndex);
  892. }
  893. else
  894. {
  895. m_listboxHost.SetCurSel(nCount - 1);
  896. }
  897. m_radioAcceptSpecificHost.SetCheck(nCount);
  898. m_radioAcceptAnyHost.SetCheck(!nCount);
  899. UpdateHostButtons();
  900. SetDirty(TRUE);
  901. }
  902. void CSecurityPage::UpdateHostButtons()
  903. {
  904. int nCount = m_listboxHost.GetCount();
  905. m_buttonEditHost.EnableWindow(nCount? TRUE: FALSE);
  906. m_buttonRemoveHost.EnableWindow(nCount? TRUE: FALSE);
  907. if (m_fPolicyPermittedManagers)
  908. {
  909. m_buttonEditHost.EnableWindow(FALSE);
  910. m_buttonRemoveHost.EnableWindow(FALSE);
  911. m_buttonAddHost.EnableWindow(FALSE);
  912. m_radioAcceptAnyHost.EnableWindow(FALSE);
  913. m_radioAcceptSpecificHost.EnableWindow(FALSE);
  914. }
  915. }
  916. void CSecurityPage::OnClickedCheckSendAuthTrap()
  917. {
  918. SetDirty(TRUE);
  919. }
  920. void CSecurityPage::OnClickedRadioAcceptAnyHost()
  921. {
  922. m_listboxHost.ResetContent();
  923. UpdateHostButtons();
  924. SetDirty(TRUE);
  925. }
  926. void CSecurityPage::OnClickedRadioAcceptSpecificHost()
  927. {
  928. SetDirty(TRUE);
  929. }
  930. PACL CSecurityPage::AllocACL()
  931. {
  932. PACL pAcl = NULL;
  933. PSID pSidAdmins = NULL;
  934. SID_IDENTIFIER_AUTHORITY Authority = SECURITY_NT_AUTHORITY;
  935. EXPLICIT_ACCESS ea[1];
  936. // Create a SID for the BUILTIN\Administrators group.
  937. if ( !AllocateAndInitializeSid( &Authority,
  938. 2,
  939. SECURITY_BUILTIN_DOMAIN_RID,
  940. DOMAIN_ALIAS_RID_ADMINS,
  941. 0, 0, 0, 0, 0, 0,
  942. &pSidAdmins ))
  943. {
  944. return NULL;
  945. }
  946. // Initialize an EXPLICIT_ACCESS structure for an ACE.
  947. ZeroMemory(&ea, 1 * sizeof(EXPLICIT_ACCESS));
  948. // The ACE will allow the Administrators group full access to the key.
  949. ea[0].grfAccessPermissions = KEY_ALL_ACCESS;
  950. ea[0].grfAccessMode = SET_ACCESS;
  951. ea[0].grfInheritance = NO_INHERITANCE;
  952. ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  953. ea[0].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  954. ea[0].Trustee.ptstrName = (LPTSTR) pSidAdmins;
  955. // Create a new ACL that contains the new ACEs.
  956. if (SetEntriesInAcl(1, ea, NULL, &pAcl) != ERROR_SUCCESS)
  957. {
  958. FreeSid(pSidAdmins);
  959. return NULL;
  960. }
  961. FreeSid(pSidAdmins);
  962. return pAcl;
  963. }
  964. void CSecurityPage::FreeACL( PACL pAcl)
  965. {
  966. if (pAcl != NULL)
  967. LocalFree(pAcl);
  968. }
  969. BOOL CSecurityPage::SnmpAddAdminAclToKey(LPTSTR pszKey)
  970. {
  971. HKEY hKey = NULL;
  972. LONG rc;
  973. PACL pAcl = NULL;
  974. SECURITY_DESCRIPTOR S_Desc;
  975. if (pszKey == NULL)
  976. return FALSE;
  977. // open registy key
  978. rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  979. pszKey, // subkey name
  980. 0, // reserved
  981. KEY_ALL_ACCESS, // want WRITE_DAC,
  982. &hKey // handle to open key
  983. );
  984. if (rc != ERROR_SUCCESS)
  985. {
  986. return FALSE;
  987. }
  988. // Initialize a security descriptor.
  989. if (InitializeSecurityDescriptor (&S_Desc, SECURITY_DESCRIPTOR_REVISION) == 0)
  990. {
  991. RegCloseKey(hKey);
  992. return FALSE;
  993. }
  994. // get the ACL and put it into the security descriptor
  995. if ( (pAcl = AllocACL()) != NULL )
  996. {
  997. if (!SetSecurityDescriptorDacl (&S_Desc, TRUE, pAcl, FALSE))
  998. {
  999. FreeACL(pAcl);
  1000. RegCloseKey(hKey);
  1001. return FALSE;
  1002. }
  1003. }
  1004. else
  1005. {
  1006. RegCloseKey(hKey);
  1007. return FALSE;
  1008. }
  1009. if (RegSetKeySecurity (hKey, DACL_SECURITY_INFORMATION, &S_Desc) != ERROR_SUCCESS)
  1010. {
  1011. FreeACL(pAcl);
  1012. RegCloseKey(hKey);
  1013. return FALSE;
  1014. }
  1015. FreeACL(pAcl);
  1016. RegCloseKey(hKey);
  1017. return TRUE;
  1018. }
  1019. BOOL CSecurityPage::LoadRegistry()
  1020. {
  1021. RegKey rk;
  1022. LONG err;
  1023. HRESULT hr, hrIter;
  1024. RegValueIterator rvIter;
  1025. LPCTSTR lpcszMachineName = g_strMachineName.IsEmpty() ? NULL : (LPCTSTR)g_strMachineName;
  1026. BOOL fPolicy;
  1027. // we need to provide precedence to the parameters set through the policy
  1028. fPolicy = TRUE;
  1029. if (fPolicy)
  1030. {
  1031. err = rk.Open(HKEY_LOCAL_MACHINE,
  1032. POLICY_VALID_COMMUNITIES_KEY_NAME,
  1033. KEY_ALL_ACCESS,
  1034. lpcszMachineName);
  1035. if (err != ERROR_SUCCESS)
  1036. fPolicy = FALSE;
  1037. else
  1038. {
  1039. // remember that we are loading from policy registry
  1040. m_fPolicyValidCommunities = TRUE;
  1041. }
  1042. }
  1043. if (fPolicy == FALSE)
  1044. {
  1045. // Open or create the registry key
  1046. err = rk.Open(HKEY_LOCAL_MACHINE,
  1047. VALID_COMMUNITIES_KEY_NAME,
  1048. KEY_ALL_ACCESS,
  1049. lpcszMachineName);
  1050. if (err != ERROR_SUCCESS)
  1051. {
  1052. err = rk.Create(HKEY_LOCAL_MACHINE,
  1053. VALID_COMMUNITIES_KEY_NAME,
  1054. REG_OPTION_NON_VOLATILE,
  1055. KEY_ALL_ACCESS,
  1056. NULL,
  1057. lpcszMachineName);
  1058. if (err != ERROR_SUCCESS)
  1059. return FALSE;
  1060. SnmpAddAdminAclToKey(VALID_COMMUNITIES_KEY_NAME);
  1061. }
  1062. }
  1063. err = rvIter.Init(&rk);
  1064. if (err != ERROR_SUCCESS) {
  1065. return FALSE;
  1066. }
  1067. CString stValue;
  1068. DWORD dwType;
  1069. for (hr = rvIter.Next(&stValue, &dwType); hr == hrOK; hr = rvIter.Next(&stValue, &dwType)) {
  1070. CString strCommunity;
  1071. DWORD dwRights;
  1072. int nIndex;
  1073. char log[128];
  1074. CString Log;
  1075. if (dwType == REG_SZ)
  1076. {
  1077. err = rk.QueryValue(stValue, strCommunity);
  1078. if (err != ERROR_SUCCESS) {
  1079. return FALSE;
  1080. }
  1081. dwRights = 1 << PERM_BIT_READCREATE;
  1082. }
  1083. else if (dwType == REG_DWORD)
  1084. {
  1085. strCommunity = stValue;
  1086. err = rk.QueryValue(stValue, dwRights);
  1087. if (err != ERROR_SUCCESS) {
  1088. return FALSE;
  1089. }
  1090. }
  1091. else
  1092. return FALSE;
  1093. if ((nIndex = m_listboxCommunity.InsertString(-1, strCommunity)) >=0)
  1094. {
  1095. int nPermIndex = 0;
  1096. CString strPermName("?????");
  1097. for (nPermIndex = 0; (dwRights & ((DWORD)(-1)^1)) != 0; dwRights >>=1, nPermIndex++);
  1098. if (!strPermName.LoadString(IDS_PERM_NAME0 + nPermIndex))
  1099. return FALSE;
  1100. if (!m_listboxCommunity.SetItemText(nIndex, 1, strPermName) ||
  1101. !m_listboxCommunity.SetItemData(nIndex, nPermIndex))
  1102. {
  1103. return FALSE;
  1104. }
  1105. }
  1106. else
  1107. {
  1108. return FALSE;
  1109. }
  1110. }
  1111. if (m_listboxCommunity.GetCount()) {
  1112. m_listboxCommunity.SetCurSel(0);
  1113. }
  1114. rk.Close();
  1115. // we need to provide precedence to the parameters set through the policy
  1116. fPolicy = TRUE;
  1117. if (fPolicy)
  1118. {
  1119. err = rk.Open(HKEY_LOCAL_MACHINE,
  1120. POLICY_PERMITTED_MANAGERS_KEY_NAME,
  1121. KEY_ALL_ACCESS,
  1122. lpcszMachineName);
  1123. if (err != ERROR_SUCCESS)
  1124. fPolicy = FALSE;
  1125. else
  1126. {
  1127. // remember that we are loading from policy registry
  1128. m_fPolicyPermittedManagers = TRUE;
  1129. }
  1130. }
  1131. if (fPolicy == FALSE)
  1132. {
  1133. // Open or create the registry key
  1134. err = rk.Open(HKEY_LOCAL_MACHINE,
  1135. PERMITTED_MANAGERS_KEY_NAME,
  1136. KEY_ALL_ACCESS,
  1137. lpcszMachineName);
  1138. if (err != ERROR_SUCCESS)
  1139. {
  1140. err = rk.Create(HKEY_LOCAL_MACHINE,
  1141. PERMITTED_MANAGERS_KEY_NAME,
  1142. REG_OPTION_NON_VOLATILE,
  1143. KEY_ALL_ACCESS,
  1144. NULL,
  1145. lpcszMachineName);
  1146. if (err != ERROR_SUCCESS)
  1147. return FALSE;
  1148. SnmpAddAdminAclToKey(PERMITTED_MANAGERS_KEY_NAME);
  1149. }
  1150. }
  1151. err = rvIter.Init(&rk);
  1152. if (err != ERROR_SUCCESS) {
  1153. return FALSE;
  1154. }
  1155. for (hr = rvIter.Next(&stValue, NULL); hr == hrOK; hr = rvIter.Next(&stValue, NULL)) {
  1156. CString strHost;
  1157. err = rk.QueryValue(stValue, strHost);
  1158. if (err != ERROR_SUCCESS) {
  1159. return FALSE;
  1160. }
  1161. m_listboxHost.InsertString(-1, strHost);
  1162. }
  1163. if (m_listboxHost.GetCount()) {
  1164. m_radioAcceptSpecificHost.SetCheck(1);
  1165. m_listboxHost.SetCurSel(0);
  1166. }
  1167. else
  1168. {
  1169. m_radioAcceptAnyHost.SetCheck(1);
  1170. }
  1171. rk.Close();
  1172. // Open or create the registry key
  1173. err = rk.Open(HKEY_LOCAL_MACHINE,
  1174. SNMP_PARAMS_KEY_NAME,
  1175. KEY_ALL_ACCESS,
  1176. lpcszMachineName);
  1177. if (err != ERROR_SUCCESS)
  1178. {
  1179. // This can't fail as far as all the other keys could be opened before
  1180. // however ... just to make sure
  1181. return FALSE;
  1182. }
  1183. DWORD dwSwitch;
  1184. // get the value for the authentication trap flag
  1185. err = rk.QueryValue(ENABLE_AUTH_TRAPS, dwSwitch);
  1186. if (err != ERROR_SUCCESS)
  1187. return FALSE;
  1188. m_checkSendAuthTrap.SetCheck(dwSwitch);
  1189. return TRUE;
  1190. }
  1191. BOOL CSecurityPage::SaveRegistry()
  1192. {
  1193. RegKey rk;
  1194. LONG err;
  1195. CString strContact;
  1196. CString strLocation;
  1197. DWORD dwServices = 0;
  1198. LPCTSTR lpcszMachineName = g_strMachineName.IsEmpty() ? NULL : (LPCTSTR)g_strMachineName;
  1199. if (m_fPolicyValidCommunities == FALSE)
  1200. {
  1201. // Open or create the registry key
  1202. err = rk.Open(HKEY_LOCAL_MACHINE,
  1203. SNMP_PARAMS_KEY_NAME,
  1204. KEY_ALL_ACCESS,
  1205. lpcszMachineName);
  1206. if (err != ERROR_SUCCESS)
  1207. {
  1208. return FALSE;
  1209. }
  1210. // If it already exists, delete the key first
  1211. err = rk.RecurseDeleteKey(VALID_COMMUNITIES);
  1212. if (err != ERROR_SUCCESS) {
  1213. return FALSE;
  1214. }
  1215. // recreate the key now
  1216. err = rk.Create(HKEY_LOCAL_MACHINE,
  1217. VALID_COMMUNITIES_KEY_NAME,
  1218. REG_OPTION_NON_VOLATILE,
  1219. KEY_ALL_ACCESS,
  1220. NULL,
  1221. lpcszMachineName);
  1222. if (err != ERROR_SUCCESS)
  1223. return FALSE;
  1224. SnmpAddAdminAclToKey(VALID_COMMUNITIES_KEY_NAME);
  1225. int nCount = m_listboxCommunity.GetCount();
  1226. for (int i = 0; i < nCount; i++) {
  1227. DWORD dwPermissions;
  1228. CString strCommunity;
  1229. m_listboxCommunity.GetText(i, strCommunity);
  1230. dwPermissions = 1 << (DWORD) m_listboxCommunity.GetItemData(i);
  1231. err = rk.SetValue(strCommunity, dwPermissions);
  1232. if (err != ERROR_SUCCESS) {
  1233. return FALSE;
  1234. }
  1235. }
  1236. rk.Close();
  1237. }
  1238. if (m_fPolicyPermittedManagers == FALSE)
  1239. {
  1240. // Open or create the registry key
  1241. err = rk.Open(HKEY_LOCAL_MACHINE,
  1242. SNMP_PARAMS_KEY_NAME,
  1243. KEY_ALL_ACCESS,
  1244. lpcszMachineName);
  1245. if (err != ERROR_SUCCESS)
  1246. {
  1247. }
  1248. // delete the key first
  1249. err = rk.RecurseDeleteKey(PERMITTED_MANAGERS);
  1250. if (err != ERROR_SUCCESS) {
  1251. return FALSE;
  1252. }
  1253. // recreate the key now
  1254. err = rk.Create(HKEY_LOCAL_MACHINE,
  1255. PERMITTED_MANAGERS_KEY_NAME,
  1256. REG_OPTION_NON_VOLATILE,
  1257. KEY_ALL_ACCESS,
  1258. NULL,
  1259. lpcszMachineName);
  1260. if (err != ERROR_SUCCESS)
  1261. return FALSE;
  1262. SnmpAddAdminAclToKey(PERMITTED_MANAGERS_KEY_NAME);
  1263. int nCount = m_listboxHost.GetCount();
  1264. for (int i = 0; i < nCount; i++) {
  1265. TCHAR buffer[32];
  1266. CString strHost;
  1267. wsprintf(buffer, TEXT("%d"), i+1);
  1268. m_listboxHost.GetText(i, strHost);
  1269. err = rk.SetValue(buffer, strHost);
  1270. if (err != ERROR_SUCCESS) {
  1271. return FALSE;
  1272. }
  1273. }
  1274. rk.Close();
  1275. }
  1276. // Open or create the registry key
  1277. err = rk.Open(HKEY_LOCAL_MACHINE,
  1278. SNMP_PARAMS_KEY_NAME,
  1279. KEY_ALL_ACCESS,
  1280. lpcszMachineName);
  1281. if (err != ERROR_SUCCESS)
  1282. {
  1283. return FALSE;
  1284. }
  1285. DWORD dwSwitch = (m_checkSendAuthTrap.GetCheck() ? 0x1 : 0 ) ;
  1286. err = rk.SetValue(ENABLE_AUTH_TRAPS, dwSwitch);
  1287. if (err != ERROR_SUCCESS)
  1288. return FALSE;
  1289. return TRUE;
  1290. }
  1291. /////////////////////////////////////////////////////////////////////////////
  1292. // CAddDialog dialog
  1293. CAddDialog::CAddDialog(CWnd* pParent /*=NULL*/)
  1294. : CBaseDialog(CAddDialog::IDD, pParent)
  1295. {
  1296. m_bCommunity = FALSE;
  1297. m_nChoice = 0;
  1298. //{{AFX_DATA_INIT(CAddDialog)
  1299. //}}AFX_DATA_INIT
  1300. }
  1301. void CAddDialog::DoDataExchange(CDataExchange* pDX)
  1302. {
  1303. CBaseDialog::DoDataExchange(pDX);
  1304. //{{AFX_DATA_MAP(CAddDialog)
  1305. DDX_Control(pDX, IDC_EDIT_NAME, m_editName);
  1306. DDX_Control(pDX, IDC_ADD, m_buttonAdd);
  1307. DDX_Control(pDX, IDCANCEL, m_buttonCancel);
  1308. DDX_Control(pDX, IDC_STATIC_ADD_TEXT, m_staticText);
  1309. DDX_Control(pDX, IDC_ST_PERMISSIONS, m_staticPermissions);
  1310. DDX_Control(pDX, IDC_CB_PERMISSIONS, m_comboPermissions);
  1311. //}}AFX_DATA_MAP
  1312. }
  1313. BEGIN_MESSAGE_MAP(CAddDialog, CBaseDialog)
  1314. //{{AFX_MSG_MAP(CAddDialog)
  1315. ON_BN_CLICKED(IDC_ADD, OnClickedButtonAdd)
  1316. //}}AFX_MSG_MAP
  1317. END_MESSAGE_MAP()
  1318. /////////////////////////////////////////////////////////////////////////////
  1319. // CAddDialog message handlers
  1320. BOOL CAddDialog::OnInitDialog()
  1321. {
  1322. CBaseDialog::OnInitDialog();
  1323. m_editName.SetLimitText(COMBO_EDIT_LEN-1);
  1324. m_editName.SetWindowText(m_strName);
  1325. m_editName.SetFocus();
  1326. m_editName.SetSel(0, -1);
  1327. if (m_bCommunity) {
  1328. CString strText;
  1329. strText.LoadString( IDS_SNMPCOMM_TEXT );
  1330. m_staticPermissions.ShowWindow(SW_SHOW);
  1331. m_comboPermissions.ShowWindow(SW_SHOW);
  1332. m_comboPermissions.ResetContent();
  1333. for (int i = 0; i<N_PERMISSION_BITS; i++)
  1334. {
  1335. CString strPermName;
  1336. int idx;
  1337. if (!strPermName.LoadString(IDS_PERM_NAME0 + i) ||
  1338. (idx=m_comboPermissions.AddString(strPermName))<0 ||
  1339. !m_comboPermissions.SetItemData(idx, i))
  1340. return FALSE;
  1341. }
  1342. m_comboPermissions.SetCurSel(m_nChoice);
  1343. // change static text of dialog to "Community Name"
  1344. m_staticText.SetWindowText(strText);
  1345. }
  1346. return FALSE; // return TRUE unless you set the focus to a control
  1347. // EXCEPTION: OCX Property Pages should return FALSE
  1348. }
  1349. void CAddDialog::OnClickedButtonAdd()
  1350. {
  1351. m_editName.GetWindowText(m_strName);
  1352. if (!m_bCommunity && IsValidString(m_strName) == FALSE)
  1353. {
  1354. m_editName.SetFocus();
  1355. m_editName.SetSel(0,-1);
  1356. return ;
  1357. }
  1358. if (m_bCommunity)
  1359. {
  1360. m_nChoice = m_comboPermissions.GetCurSel();
  1361. m_comboPermissions.GetWindowText(m_strChoice);
  1362. }
  1363. CBaseDialog::OnOK();
  1364. }
  1365. DWORD * CAddDialog::GetHelpMap()
  1366. {
  1367. return m_bCommunity ?
  1368. (DWORD *) &g_aHelpIDs_IDD_DIALOG_ADD_COMM[0]:
  1369. (DWORD *) &g_aHelpIDs_IDD_DIALOG_ADD_ADDR[0];
  1370. }
  1371. /////////////////////////////////////////////////////////////////////////////
  1372. // CEditDialog dialog
  1373. CEditDialog::CEditDialog(CWnd* pParent /*=NULL*/)
  1374. : CBaseDialog(CEditDialog::IDD, pParent)
  1375. {
  1376. m_bCommunity = FALSE;
  1377. m_nChoice = 0;
  1378. //{{AFX_DATA_INIT(CEditDialog)
  1379. //}}AFX_DATA_INIT
  1380. }
  1381. void CEditDialog::DoDataExchange(CDataExchange* pDX)
  1382. {
  1383. CBaseDialog::DoDataExchange(pDX);
  1384. //{{AFX_DATA_MAP(CEditDialog)
  1385. DDX_Control(pDX, IDC_EDIT_NAME, m_editName);
  1386. DDX_Control(pDX, IDOK, m_buttonOk);
  1387. DDX_Control(pDX, IDCANCEL, m_buttonCancel);
  1388. DDX_Control(pDX, IDC_STATIC_EDIT_TEXT, m_staticText);
  1389. DDX_Control(pDX, IDC_ST_PERMISSIONS, m_staticPermissions);
  1390. DDX_Control(pDX, IDC_CB_PERMISSIONS, m_comboPermissions);
  1391. //}}AFX_DATA_MAP
  1392. }
  1393. BEGIN_MESSAGE_MAP(CEditDialog, CBaseDialog)
  1394. //{{AFX_MSG_MAP(CEditDialog)
  1395. //}}AFX_MSG_MAP
  1396. END_MESSAGE_MAP()
  1397. /////////////////////////////////////////////////////////////////////////////
  1398. // CEditDialog message handlers
  1399. BOOL CEditDialog::OnInitDialog()
  1400. {
  1401. CBaseDialog::OnInitDialog();
  1402. m_editName.SetLimitText(COMBO_EDIT_LEN-1);
  1403. m_editName.SetWindowText(m_strName);
  1404. m_editName.SetFocus();
  1405. // select the entire string in the edit control
  1406. m_editName.SetSel(0, -1);
  1407. if (m_bCommunity) {
  1408. CString strText;
  1409. strText.LoadString( IDS_SNMPCOMM_TEXT );
  1410. m_staticPermissions.ShowWindow(SW_SHOW);
  1411. m_comboPermissions.ShowWindow(SW_SHOW);
  1412. m_comboPermissions.ResetContent();
  1413. for (int i = 0; i<N_PERMISSION_BITS; i++)
  1414. {
  1415. CString strPermName;
  1416. int idx;
  1417. if (!strPermName.LoadString(IDS_PERM_NAME0 + i) ||
  1418. (idx=m_comboPermissions.AddString(strPermName))<0 ||
  1419. !m_comboPermissions.SetItemData(idx, i))
  1420. {
  1421. AfxMessageBox(strPermName);
  1422. return FALSE;
  1423. }
  1424. }
  1425. m_comboPermissions.SetCurSel(m_nChoice);
  1426. // change static text of dialog to "Community Name"
  1427. m_staticText.SetWindowText(strText);
  1428. }
  1429. return FALSE; // return TRUE unless you set the focus to a control
  1430. // EXCEPTION: OCX Property Pages should return FALSE
  1431. }
  1432. void CEditDialog::OnOK()
  1433. {
  1434. m_editName.GetWindowText(m_strName);
  1435. if (!m_bCommunity) {
  1436. if (IsValidString(m_strName) == FALSE)
  1437. {
  1438. m_editName.SetFocus();
  1439. return ;
  1440. }
  1441. }
  1442. else
  1443. {
  1444. m_comboPermissions.GetWindowText(m_strChoice);
  1445. m_nChoice = m_comboPermissions.GetCurSel();
  1446. }
  1447. CBaseDialog::OnOK();
  1448. }
  1449. DWORD * CEditDialog::GetHelpMap()
  1450. {
  1451. return m_bCommunity ?
  1452. (DWORD *) &g_aHelpIDs_IDD_DIALOG_EDIT_COMM[0] :
  1453. (DWORD *) &g_aHelpIDs_IDD_DIALOG_EDIT_ADDR[0];
  1454. }
  1455. BOOL IsValidString(CString & strName)
  1456. {
  1457. BOOL bResult = FALSE;
  1458. // check if this is a valid IP host name or address
  1459. if (ValidateDomain(strName) == TRUE)
  1460. return(TRUE);
  1461. int nLen = strName.GetLength();
  1462. if (nLen == 0 || nLen > 12) {
  1463. CString strMsg;
  1464. strMsg.FormatMessage(IDS_SNMP_INVALID_IP_IPX_ADD, strName);
  1465. AfxMessageBox(strMsg, MB_ICONEXCLAMATION|MB_OK);
  1466. return FALSE;
  1467. }
  1468. LPTSTR buffer = strName.GetBuffer(64);
  1469. for (int i = 0; i < nLen; i++) {
  1470. if (!iswxdigit((int) buffer[i])) {
  1471. CString strMsg;
  1472. strName.ReleaseBuffer();
  1473. strMsg.FormatMessage(IDS_SNMP_INVALID_IP_IPX_ADD, strName);
  1474. AfxMessageBox(strMsg, MB_ICONEXCLAMATION|MB_OK);
  1475. return FALSE;
  1476. }
  1477. }
  1478. return TRUE;
  1479. }
  1480. BOOL ValidateDomain(CString & strDomain)
  1481. {
  1482. int nLen;
  1483. if ((nLen = strDomain.GetLength()) != 0)
  1484. {
  1485. if (nLen < DOMAINNAME_LENGTH)
  1486. {
  1487. int i;
  1488. TCHAR ch;
  1489. BOOL fLet_Dig = FALSE;
  1490. BOOL fDot = FALSE;
  1491. int cHostname = 0;
  1492. LPTSTR buffer = strDomain.GetBuffer(64);
  1493. for (i = 0; i < nLen; i++)
  1494. {
  1495. // check each character
  1496. ch = buffer[i];
  1497. BOOL fAlNum = iswalpha(ch) || iswdigit(ch);
  1498. if (((i == 0) && !fAlNum) ||
  1499. // first letter must be a digit or a letter
  1500. (fDot && !fAlNum) ||
  1501. // first letter after dot must be a digit or a letter
  1502. ((i == (nLen - 1)) && !fAlNum) ||
  1503. // last letter must be a letter or a digit
  1504. (!fAlNum && ( ch != _T('-') && ( ch != _T('.') && ( ch != _T('_'))))) ||
  1505. // must be letter, digit, - or "."
  1506. (( ch == _T('.')) && ( !fLet_Dig )))
  1507. // must be letter or digit before '.'
  1508. {
  1509. return FALSE;
  1510. }
  1511. fLet_Dig = fAlNum;
  1512. fDot = (ch == _T('.'));
  1513. cHostname++;
  1514. if ( cHostname > HOSTNAME_LENGTH )
  1515. {
  1516. return FALSE;
  1517. }
  1518. if ( fDot )
  1519. {
  1520. cHostname = 0;
  1521. }
  1522. }
  1523. }
  1524. }
  1525. else
  1526. {
  1527. return FALSE;
  1528. }
  1529. return TRUE;
  1530. }
  1531. void CSecurityPage::OnDblclkCtrlistCommunity(NMHDR* pNMHDR, LRESULT* pResult)
  1532. {
  1533. if (m_fPolicyValidCommunities == FALSE)
  1534. OnClickedButtonEditCommunity();
  1535. *pResult = 0;
  1536. }
  1537. void CSecurityPage::OnCommunityListChanged(NMHDR* pNMHDR, LRESULT* pResult)
  1538. {
  1539. INT nIndex;
  1540. NMLVODSTATECHANGE* pStateChanged = (NMLVODSTATECHANGE*)pNMHDR;
  1541. // TODO: Add your control notification handler code here
  1542. nIndex = m_listboxCommunity.GetCurSel();
  1543. m_buttonEditCommunity.EnableWindow(nIndex >= 0);
  1544. m_buttonRemoveCommunity.EnableWindow(nIndex >= 0);
  1545. if (m_fPolicyValidCommunities)
  1546. {
  1547. m_buttonEditCommunity.EnableWindow(FALSE);
  1548. m_buttonRemoveCommunity.EnableWindow(FALSE);
  1549. }
  1550. *pResult = 0;
  1551. }