From 8303b26b76456d6a64b7f10d575b52515cbec143 Mon Sep 17 00:00:00 2001 From: BornToBeRoot Date: Sun, 21 Mar 2021 03:16:55 +0100 Subject: [PATCH 1/2] TextBox replaced with PasswordBox and priv min length removed --- Source/NETworkManager.Models/Network/SNMP.cs | 32 ++++---- .../PrivacyAESValidator.cs | 13 --- .../ViewModels/SNMPViewModel.cs | 81 ++++++++++++++++--- Source/NETworkManager/Views/SNMPView.xaml | 75 ++++++++--------- 4 files changed, 119 insertions(+), 82 deletions(-) delete mode 100644 Source/NETworkManager.Validators/PrivacyAESValidator.cs diff --git a/Source/NETworkManager.Models/Network/SNMP.cs b/Source/NETworkManager.Models/Network/SNMP.cs index 9d2dfef6c2..59e589ca9a 100644 --- a/Source/NETworkManager.Models/Network/SNMP.cs +++ b/Source/NETworkManager.Models/Network/SNMP.cs @@ -1,9 +1,11 @@ using Lextm.SharpSnmpLib; using Lextm.SharpSnmpLib.Messaging; using Lextm.SharpSnmpLib.Security; +using NETworkManager.Utilities; using System; using System.Collections.Generic; using System.Net; +using System.Security; using System.Threading.Tasks; namespace NETworkManager.Models.Network @@ -53,13 +55,13 @@ protected virtual void OnUserHasCanceled() #endregion #region Methods - public void GetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string community, string oid) + public void GetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, SecureString community, string oid) { Task.Run(() => { try { - foreach (var result in Messenger.Get(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(community), new List { new Variable(new ObjectIdentifier(oid)) }, Timeout)) + foreach (var result in Messenger.Get(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(SecureStringHelper.ConvertToString(community)), new List { new Variable(new ObjectIdentifier(oid)) }, Timeout)) OnReceived(new SNMPReceivedArgs(result.Id, result.Data)); OnComplete(); @@ -75,7 +77,7 @@ public void GetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string commu }); } - public void WalkV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string community, string oid, WalkMode walkMode) + public void WalkV1V2CAsync(SNMPVersion version, IPAddress ipAddress, SecureString community, string oid, WalkMode walkMode) { Task.Run(() => { @@ -83,7 +85,7 @@ public void WalkV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string comm { IList results = new List(); - Messenger.Walk(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(community), new ObjectIdentifier(oid), results, Timeout, walkMode); + Messenger.Walk(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(SecureStringHelper.ConvertToString(community)), new ObjectIdentifier(oid), results, Timeout, walkMode); foreach (var result in results) OnReceived(new SNMPReceivedArgs(result.Id, result.Data)); @@ -101,13 +103,13 @@ public void WalkV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string comm }); } - public void SetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string communtiy, string oid, string data) + public void SetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, SecureString communtiy, string oid, string data) { Task.Run(() => { try { - Messenger.Set(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(communtiy), new List { new Variable(new ObjectIdentifier(oid), new OctetString(data)) }, Timeout); + Messenger.Set(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(SecureStringHelper.ConvertToString(communtiy)), new List { new Variable(new ObjectIdentifier(oid), new OctetString(data)) }, Timeout); OnComplete(); } @@ -122,7 +124,7 @@ public void SetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string commu }); } - public void Getv3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, string auth, SNMPV3PrivacyProvider privProvider, string priv) + public void Getv3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, SecureString auth, SNMPV3PrivacyProvider privProvider, SecureString priv) { Task.Run(() => { @@ -139,11 +141,11 @@ public void Getv3Async(IPAddress ipAddress, string oid, SNMPV3Security security, switch (security) { case SNMPV3Security.AuthPriv: - privacy = GetPrivacy(authProvider, auth, privProvider, priv); + privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth), privProvider, SecureStringHelper.ConvertToString(priv)); break; // noAuthNoPriv case SNMPV3Security.AuthNoPriv: - privacy = GetPrivacy(authProvider, auth); + privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth)); break; default: privacy = GetPrivacy(); @@ -170,7 +172,7 @@ public void Getv3Async(IPAddress ipAddress, string oid, SNMPV3Security security, }); } - public void WalkV3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, string auth, SNMPV3PrivacyProvider privProvider, string priv, WalkMode walkMode) + public void WalkV3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, SecureString auth, SNMPV3PrivacyProvider privProvider, SecureString priv, WalkMode walkMode) { Task.Run(() => { @@ -187,11 +189,11 @@ public void WalkV3Async(IPAddress ipAddress, string oid, SNMPV3Security security switch (security) { case SNMPV3Security.AuthPriv: - privacy = GetPrivacy(authProvider, auth, privProvider, priv); + privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth), privProvider, SecureStringHelper.ConvertToString(priv)); break; // noAuthNoPriv case SNMPV3Security.AuthNoPriv: - privacy = GetPrivacy(authProvider, auth); + privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth)); break; default: privacy = GetPrivacy(); @@ -218,7 +220,7 @@ public void WalkV3Async(IPAddress ipAddress, string oid, SNMPV3Security security }); } - public void SetV3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, string auth, SNMPV3PrivacyProvider privProvider, string priv, string data) + public void SetV3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, SecureString auth, SNMPV3PrivacyProvider privProvider, SecureString priv, string data) { Task.Run(() => { @@ -235,11 +237,11 @@ public void SetV3Async(IPAddress ipAddress, string oid, SNMPV3Security security, switch (security) { case SNMPV3Security.AuthPriv: - privacy = GetPrivacy(authProvider, auth, privProvider, priv); + privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth), privProvider, SecureStringHelper.ConvertToString(priv)); break; // noAuthNoPriv case SNMPV3Security.AuthNoPriv: - privacy = GetPrivacy(authProvider, auth); + privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth)); break; default: privacy = GetPrivacy(); diff --git a/Source/NETworkManager.Validators/PrivacyAESValidator.cs b/Source/NETworkManager.Validators/PrivacyAESValidator.cs deleted file mode 100644 index d604eebde3..0000000000 --- a/Source/NETworkManager.Validators/PrivacyAESValidator.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Globalization; -using System.Windows.Controls; - -namespace NETworkManager.Validators -{ - public class PrivacyAESValidator : ValidationRule - { - public override ValidationResult Validate(object value, CultureInfo cultureInfo) - { - return value != null && ((string) value).Length < 8 ? new ValidationResult(false, Localization.Resources.Strings.KeyMustHave8CharactersOrMore) : ValidationResult.ValidResult; - } - } -} diff --git a/Source/NETworkManager/ViewModels/SNMPViewModel.cs b/Source/NETworkManager/ViewModels/SNMPViewModel.cs index e71231f9d5..d135bf409b 100644 --- a/Source/NETworkManager/ViewModels/SNMPViewModel.cs +++ b/Source/NETworkManager/ViewModels/SNMPViewModel.cs @@ -6,10 +6,8 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; -using System.Diagnostics; using System.Linq; using System.Net; -using System.Net.Sockets; using System.Windows; using System.Windows.Data; using System.Windows.Input; @@ -21,6 +19,7 @@ using MahApps.Metro.Controls.Dialogs; using NETworkManager.Models.Export; using NETworkManager.Views; +using System.Security; namespace NETworkManager.ViewModels { @@ -122,8 +121,22 @@ public SNMPV3Security Security } } - private string _community; - public string Community + private bool _isCommunityEmpty = true; // Initial it's empty + public bool IsCommunityEmpty + { + get => _isCommunityEmpty; + set + { + if (value == _isCommunityEmpty) + return; + + _isCommunityEmpty = value; + OnPropertyChanged(); + } + } + + private SecureString _community; + public SecureString Community { get => _community; set @@ -131,6 +144,12 @@ public string Community if (value == _community) return; + // Validate the community string + if (value == null) + IsCommunityEmpty = true; + else + IsCommunityEmpty = string.IsNullOrEmpty(SecureStringHelper.ConvertToString(value)); + _community = value; OnPropertyChanged(); } @@ -169,8 +188,22 @@ public SNMPV3AuthenticationProvider AuthenticationProvider } } - private string _auth; - public string Auth + private bool _isAuthEmpty = true; // Initial it's empty + public bool IsAuthEmpty + { + get => _isAuthEmpty; + set + { + if (value == _isAuthEmpty) + return; + + _isAuthEmpty = value; + OnPropertyChanged(); + } + } + + private SecureString _auth; + public SecureString Auth { get => _auth; set @@ -178,6 +211,12 @@ public string Auth if (value == _auth) return; + // Validate the auth string + if (value == null) + IsAuthEmpty = true; + else + IsAuthEmpty = string.IsNullOrEmpty(SecureStringHelper.ConvertToString(value)); + _auth = value; OnPropertyChanged(); } @@ -202,8 +241,22 @@ public SNMPV3PrivacyProvider PrivacyProvider } } - private string _priv; - public string Priv + private bool _isPrivEmpty = true; // Initial it's empty + public bool IsPrivEmpty + { + get => _isPrivEmpty; + set + { + if (value == _isPrivEmpty) + return; + + _isPrivEmpty = value; + OnPropertyChanged(); + } + } + + private SecureString _priv; + public SecureString Priv { get => _priv; set @@ -211,6 +264,12 @@ public string Priv if (value == _priv) return; + // Validate the auth string + if (value == null) + IsPrivEmpty = true; + else + IsPrivEmpty = string.IsNullOrEmpty(SecureStringHelper.ConvertToString(value)); + _priv = value; OnPropertyChanged(); } @@ -323,7 +382,7 @@ public SNMPViewModel(IDialogCoordinator instance, int tabId, string host) _isLoading = true; _dialogCoordinator = instance; - + TabId = tabId; Host = host; @@ -454,7 +513,7 @@ private async void Work() } if (ipAddress == null) - { + { Finished(); StatusMessage = string.Format(Localization.Resources.Strings.CouldNotResolveIPAddressFor, Host); @@ -548,7 +607,7 @@ private void Snmp_Received(object sender, SNMPReceivedArgs e) Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate { //lock (QueryResults) - QueryResults.Add(snmpReceivedInfo); + QueryResults.Add(snmpReceivedInfo); })); } diff --git a/Source/NETworkManager/Views/SNMPView.xaml b/Source/NETworkManager/Views/SNMPView.xaml index 1d05e2688c..b00daebc7d 100644 --- a/Source/NETworkManager/Views/SNMPView.xaml +++ b/Source/NETworkManager/Views/SNMPView.xaml @@ -13,6 +13,8 @@ xmlns:localization="clr-namespace:NETworkManager.Localization.Resources;assembly=NETworkManager.Localization" xmlns:dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro" xmlns:controls="clr-namespace:NETworkManager.Controls;assembly=NETworkManager.Controls" + xmlns:wpfHelpers="clr-namespace:NETworkManager.Utilities.WPF;assembly=NETworkManager.Utilities.WPF" + xmlns:interactivity="http://schemas.microsoft.com/xaml/behaviors" dialogs:DialogParticipation.Register="{Binding}" mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:SNMPViewModel}"> @@ -83,7 +85,7 @@ - + - - - - - - - - - - - - + + + + + - - - - - - - - - - - - + + + + + - - - - - - - - - - - - - + + + + +