comparison ServerMonitor/Objects/Server.cs @ 5:b6fe203af9d5

Private key passwords and validation
author Brad Greco <brad@bgreco.net>
date Thu, 28 Feb 2019 21:19:32 -0500
parents 3142e52cbe69
children c1dffaac66fa
comparison
equal deleted inserted replaced
4:3142e52cbe69 5:b6fe203af9d5
4 using System.Text; 4 using System.Text;
5 using System.Security.Cryptography; 5 using System.Security.Cryptography;
6 using System.ComponentModel; 6 using System.ComponentModel;
7 using Renci.SshNet; 7 using Renci.SshNet;
8 using System.Runtime.Serialization; 8 using System.Runtime.Serialization;
9 using System.Xml.Serialization;
9 10
10 namespace ServerMonitorApp 11 namespace ServerMonitorApp
11 { 12 {
12 public enum LoginType { PrivateKey = 0, Password = 1 }; 13 public enum LoginType { PrivateKey = 0, Password = 1 };
13 14
19 private LoginType _loginType; 20 private LoginType _loginType;
20 private string _keyFile; 21 private string _keyFile;
21 private SshClient _sshClient; 22 private SshClient _sshClient;
22 private bool _enabled = true; 23 private bool _enabled = true;
23 private byte[] passwordHash; 24 private byte[] passwordHash;
25 private PrivateKeyFile _privateKeyFile;
24 26
25 public event EventHandler CheckModified; 27 public event EventHandler CheckModified;
26 public event EventHandler EnabledChanged; 28 public event EventHandler EnabledChanged;
27 29
28 public readonly BindingList<Check> Checks = new BindingList<Check>(); 30 public readonly BindingList<Check> Checks = new BindingList<Check>();
68 Encoding.UTF8.GetString(ProtectedData.Unprotect(passwordHash, Encoding.UTF8.GetBytes("Server".Reverse().ToString()), DataProtectionScope.CurrentUser)); 70 Encoding.UTF8.GetString(ProtectedData.Unprotect(passwordHash, Encoding.UTF8.GetBytes("Server".Reverse().ToString()), DataProtectionScope.CurrentUser));
69 } 71 }
70 set 72 set
71 { 73 {
72 passwordHash = ProtectedData.Protect(Encoding.UTF8.GetBytes(value), 74 passwordHash = ProtectedData.Protect(Encoding.UTF8.GetBytes(value),
73 Encoding.UTF8.GetBytes("Server".Reverse().ToString()), // Minor obfuscation of additional entropy 75 Encoding.UTF8.GetBytes("Server".Reverse().ToString()), // Minor obfuscation of additional entropy
74 DataProtectionScope.CurrentUser); 76 DataProtectionScope.CurrentUser);
75 } 77 }
76 } 78 }
79
80 [XmlIgnore]
81 public PrivateKeyFile PrivateKeyFile
82 {
83 get { return _privateKeyFile; }
84 set
85 {
86 _privateKeyFile = value;
87 if (LoginType == LoginType.PrivateKey)
88 {
89 if (_privateKeyFile == null)
90 {
91 KeyStatus = KeyStatus.Closed;
92 Enabled = false;
93 }
94 else
95 {
96 if (!KeyStatus.In(KeyStatus.Open, KeyStatus.Closed))
97 Enabled = true;
98 KeyStatus = KeyStatus.Open;
99 }
100 }
101 }
102 }
103
104 public KeyStatus KeyStatus { get; set; }
77 105
78 public bool Enabled 106 public bool Enabled
79 { 107 {
80 get { return _enabled; } 108 get { return _enabled; }
81 set { _enabled = value; EnabledChanged?.Invoke(this, new EventArgs()); } 109 set
82 } 110 {
111 if (LoginType == LoginType.PrivateKey && PrivateKeyFile == null && value == true)
112 return;
113 _enabled = value;
114 EnabledChanged?.Invoke(this, new EventArgs());
115 }
116 }
117
118 //public bool WaitingForUser { get; set; }
83 119
84 public CheckStatus Status => !Enabled ? CheckStatus.Disabled : Checks 120 public CheckStatus Status => !Enabled ? CheckStatus.Disabled : Checks
85 .Where(c => c.Enabled) 121 .Where(c => c.Enabled)
86 .Select(c => c.LastRunStatus) 122 .Select(c => c.LastRunStatus)
87 .DefaultIfEmpty(CheckStatus.Success) 123 .DefaultIfEmpty(CheckStatus.Success)
91 { 127 {
92 get 128 get
93 { 129 {
94 if (_sshClient == null) 130 if (_sshClient == null)
95 { 131 {
96 AuthenticationMethod auth = null; 132 ConnectionInfo info = new ConnectionInfo(Host, Port, Username, GetAuthentication());
97 if (LoginType == LoginType.Password)
98 auth = new PasswordAuthenticationMethod(Username, Password);
99 else
100 auth = new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(KeyFile));
101 ConnectionInfo info = new ConnectionInfo(Host, Port, Username, auth);
102 _sshClient = new SshClient(info); 133 _sshClient = new SshClient(info);
103 } 134 }
104 return _sshClient; 135 return _sshClient;
105 } 136 }
106 } 137 }
154 return Name.IsNullOrEmpty() 185 return Name.IsNullOrEmpty()
155 && Host.IsNullOrEmpty() 186 && Host.IsNullOrEmpty()
156 && Checks.Count == 0; 187 && Checks.Count == 0;
157 } 188 }
158 189
190 private AuthenticationMethod GetAuthentication()
191 {
192 if (LoginType == LoginType.Password)
193 return new PasswordAuthenticationMethod(Username, Password);
194 else
195 return new PrivateKeyAuthenticationMethod(Username, PrivateKeyFile);
196 }
197
159 private void InvalidateSshConnection() 198 private void InvalidateSshConnection()
160 { 199 {
161 _sshClient?.Dispose(); 200 _sshClient?.Dispose();
162 _sshClient = null; 201 _sshClient = null;
163 } 202 }
164 } 203 }
204
205 public enum KeyStatus
206 {
207 Closed,
208 Open,
209 NotAccessible,
210 NeedPassword,
211 }
212
213
165 } 214 }