Skip to content

Commit 5d84137

Browse files
authored
TextBox replaced with PasswordBox and priv min length removed (BornToBeRoot#625)
* TextBox replaced with PasswordBox and priv min length removed * BornToBeRoot#128 Co-authored-by: BornToBeRoot <BornToBeRoot@users.noreply.github.com>
1 parent a41e9f4 commit 5d84137

5 files changed

Lines changed: 121 additions & 82 deletions

File tree

Source/NETworkManager.Models/Network/SNMP.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using Lextm.SharpSnmpLib;
22
using Lextm.SharpSnmpLib.Messaging;
33
using Lextm.SharpSnmpLib.Security;
4+
using NETworkManager.Utilities;
45
using System;
56
using System.Collections.Generic;
67
using System.Net;
8+
using System.Security;
79
using System.Threading.Tasks;
810

911
namespace NETworkManager.Models.Network
@@ -53,13 +55,13 @@ protected virtual void OnUserHasCanceled()
5355
#endregion
5456

5557
#region Methods
56-
public void GetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string community, string oid)
58+
public void GetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, SecureString community, string oid)
5759
{
5860
Task.Run(() =>
5961
{
6062
try
6163
{
62-
foreach (var result in Messenger.Get(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(community), new List<Variable> { new Variable(new ObjectIdentifier(oid)) }, Timeout))
64+
foreach (var result in Messenger.Get(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(SecureStringHelper.ConvertToString(community)), new List<Variable> { new Variable(new ObjectIdentifier(oid)) }, Timeout))
6365
OnReceived(new SNMPReceivedArgs(result.Id, result.Data));
6466

6567
OnComplete();
@@ -75,15 +77,15 @@ public void GetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string commu
7577
});
7678
}
7779

78-
public void WalkV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string community, string oid, WalkMode walkMode)
80+
public void WalkV1V2CAsync(SNMPVersion version, IPAddress ipAddress, SecureString community, string oid, WalkMode walkMode)
7981
{
8082
Task.Run(() =>
8183
{
8284
try
8385
{
8486
IList<Variable> results = new List<Variable>();
8587

86-
Messenger.Walk(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(community), new ObjectIdentifier(oid), results, Timeout, walkMode);
88+
Messenger.Walk(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(SecureStringHelper.ConvertToString(community)), new ObjectIdentifier(oid), results, Timeout, walkMode);
8789

8890
foreach (var result in results)
8991
OnReceived(new SNMPReceivedArgs(result.Id, result.Data));
@@ -101,13 +103,13 @@ public void WalkV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string comm
101103
});
102104
}
103105

104-
public void SetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string communtiy, string oid, string data)
106+
public void SetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, SecureString communtiy, string oid, string data)
105107
{
106108
Task.Run(() =>
107109
{
108110
try
109111
{
110-
Messenger.Set(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(communtiy), new List<Variable> { new Variable(new ObjectIdentifier(oid), new OctetString(data)) }, Timeout);
112+
Messenger.Set(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(SecureStringHelper.ConvertToString(communtiy)), new List<Variable> { new Variable(new ObjectIdentifier(oid), new OctetString(data)) }, Timeout);
111113

112114
OnComplete();
113115
}
@@ -122,7 +124,7 @@ public void SetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, string commu
122124
});
123125
}
124126

125-
public void Getv3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, string auth, SNMPV3PrivacyProvider privProvider, string priv)
127+
public void Getv3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, SecureString auth, SNMPV3PrivacyProvider privProvider, SecureString priv)
126128
{
127129
Task.Run(() =>
128130
{
@@ -139,11 +141,11 @@ public void Getv3Async(IPAddress ipAddress, string oid, SNMPV3Security security,
139141
switch (security)
140142
{
141143
case SNMPV3Security.AuthPriv:
142-
privacy = GetPrivacy(authProvider, auth, privProvider, priv);
144+
privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth), privProvider, SecureStringHelper.ConvertToString(priv));
143145
break;
144146
// noAuthNoPriv
145147
case SNMPV3Security.AuthNoPriv:
146-
privacy = GetPrivacy(authProvider, auth);
148+
privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth));
147149
break;
148150
default:
149151
privacy = GetPrivacy();
@@ -170,7 +172,7 @@ public void Getv3Async(IPAddress ipAddress, string oid, SNMPV3Security security,
170172
});
171173
}
172174

173-
public void WalkV3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, string auth, SNMPV3PrivacyProvider privProvider, string priv, WalkMode walkMode)
175+
public void WalkV3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, SecureString auth, SNMPV3PrivacyProvider privProvider, SecureString priv, WalkMode walkMode)
174176
{
175177
Task.Run(() =>
176178
{
@@ -187,11 +189,11 @@ public void WalkV3Async(IPAddress ipAddress, string oid, SNMPV3Security security
187189
switch (security)
188190
{
189191
case SNMPV3Security.AuthPriv:
190-
privacy = GetPrivacy(authProvider, auth, privProvider, priv);
192+
privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth), privProvider, SecureStringHelper.ConvertToString(priv));
191193
break;
192194
// noAuthNoPriv
193195
case SNMPV3Security.AuthNoPriv:
194-
privacy = GetPrivacy(authProvider, auth);
196+
privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth));
195197
break;
196198
default:
197199
privacy = GetPrivacy();
@@ -218,7 +220,7 @@ public void WalkV3Async(IPAddress ipAddress, string oid, SNMPV3Security security
218220
});
219221
}
220222

221-
public void SetV3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, string auth, SNMPV3PrivacyProvider privProvider, string priv, string data)
223+
public void SetV3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, SecureString auth, SNMPV3PrivacyProvider privProvider, SecureString priv, string data)
222224
{
223225
Task.Run(() =>
224226
{
@@ -235,11 +237,11 @@ public void SetV3Async(IPAddress ipAddress, string oid, SNMPV3Security security,
235237
switch (security)
236238
{
237239
case SNMPV3Security.AuthPriv:
238-
privacy = GetPrivacy(authProvider, auth, privProvider, priv);
240+
privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth), privProvider, SecureStringHelper.ConvertToString(priv));
239241
break;
240242
// noAuthNoPriv
241243
case SNMPV3Security.AuthNoPriv:
242-
privacy = GetPrivacy(authProvider, auth);
244+
privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth));
243245
break;
244246
default:
245247
privacy = GetPrivacy();

Source/NETworkManager.Validators/PrivacyAESValidator.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

Source/NETworkManager/ViewModels/SNMPViewModel.cs

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
using System.Collections.Generic;
77
using System.Collections.ObjectModel;
88
using System.ComponentModel;
9-
using System.Diagnostics;
109
using System.Linq;
1110
using System.Net;
12-
using System.Net.Sockets;
1311
using System.Windows;
1412
using System.Windows.Data;
1513
using System.Windows.Input;
@@ -21,6 +19,7 @@
2119
using MahApps.Metro.Controls.Dialogs;
2220
using NETworkManager.Models.Export;
2321
using NETworkManager.Views;
22+
using System.Security;
2423

2524
namespace NETworkManager.ViewModels
2625
{
@@ -122,15 +121,35 @@ public SNMPV3Security Security
122121
}
123122
}
124123

125-
private string _community;
126-
public string Community
124+
private bool _isCommunityEmpty = true; // Initial it's empty
125+
public bool IsCommunityEmpty
126+
{
127+
get => _isCommunityEmpty;
128+
set
129+
{
130+
if (value == _isCommunityEmpty)
131+
return;
132+
133+
_isCommunityEmpty = value;
134+
OnPropertyChanged();
135+
}
136+
}
137+
138+
private SecureString _community;
139+
public SecureString Community
127140
{
128141
get => _community;
129142
set
130143
{
131144
if (value == _community)
132145
return;
133146

147+
// Validate the community string
148+
if (value == null)
149+
IsCommunityEmpty = true;
150+
else
151+
IsCommunityEmpty = string.IsNullOrEmpty(SecureStringHelper.ConvertToString(value));
152+
134153
_community = value;
135154
OnPropertyChanged();
136155
}
@@ -169,15 +188,35 @@ public SNMPV3AuthenticationProvider AuthenticationProvider
169188
}
170189
}
171190

172-
private string _auth;
173-
public string Auth
191+
private bool _isAuthEmpty = true; // Initial it's empty
192+
public bool IsAuthEmpty
193+
{
194+
get => _isAuthEmpty;
195+
set
196+
{
197+
if (value == _isAuthEmpty)
198+
return;
199+
200+
_isAuthEmpty = value;
201+
OnPropertyChanged();
202+
}
203+
}
204+
205+
private SecureString _auth;
206+
public SecureString Auth
174207
{
175208
get => _auth;
176209
set
177210
{
178211
if (value == _auth)
179212
return;
180213

214+
// Validate the auth string
215+
if (value == null)
216+
IsAuthEmpty = true;
217+
else
218+
IsAuthEmpty = string.IsNullOrEmpty(SecureStringHelper.ConvertToString(value));
219+
181220
_auth = value;
182221
OnPropertyChanged();
183222
}
@@ -202,15 +241,35 @@ public SNMPV3PrivacyProvider PrivacyProvider
202241
}
203242
}
204243

205-
private string _priv;
206-
public string Priv
244+
private bool _isPrivEmpty = true; // Initial it's empty
245+
public bool IsPrivEmpty
246+
{
247+
get => _isPrivEmpty;
248+
set
249+
{
250+
if (value == _isPrivEmpty)
251+
return;
252+
253+
_isPrivEmpty = value;
254+
OnPropertyChanged();
255+
}
256+
}
257+
258+
private SecureString _priv;
259+
public SecureString Priv
207260
{
208261
get => _priv;
209262
set
210263
{
211264
if (value == _priv)
212265
return;
213266

267+
// Validate the auth string
268+
if (value == null)
269+
IsPrivEmpty = true;
270+
else
271+
IsPrivEmpty = string.IsNullOrEmpty(SecureStringHelper.ConvertToString(value));
272+
214273
_priv = value;
215274
OnPropertyChanged();
216275
}
@@ -323,7 +382,7 @@ public SNMPViewModel(IDialogCoordinator instance, int tabId, string host)
323382
_isLoading = true;
324383

325384
_dialogCoordinator = instance;
326-
385+
327386
TabId = tabId;
328387
Host = host;
329388

@@ -454,7 +513,7 @@ private async void Work()
454513
}
455514

456515
if (ipAddress == null)
457-
{
516+
{
458517
Finished();
459518

460519
StatusMessage = string.Format(Localization.Resources.Strings.CouldNotResolveIPAddressFor, Host);
@@ -548,7 +607,7 @@ private void Snmp_Received(object sender, SNMPReceivedArgs e)
548607
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate
549608
{
550609
//lock (QueryResults)
551-
QueryResults.Add(snmpReceivedInfo);
610+
QueryResults.Add(snmpReceivedInfo);
552611
}));
553612
}
554613

0 commit comments

Comments
 (0)