Mercurial > servermonitor
diff ServerMonitor/Forms/ServerForm.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 | 052aa62cb42a |
line wrap: on
line diff
--- a/ServerMonitor/Forms/ServerForm.cs Sun Feb 10 20:51:26 2019 -0500 +++ b/ServerMonitor/Forms/ServerForm.cs Thu Feb 28 21:19:32 2019 -0500 @@ -1,9 +1,12 @@ -using ServerMonitorApp.Properties; +using Renci.SshNet; +using Renci.SshNet.Common; +using ServerMonitorApp.Properties; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -54,8 +57,7 @@ } else { - Text = Server.Name; - TitleLabel.Text = Server.Name; + SetTitle(); NameTextBox.Text = Server.Name; HostTextBox.Text = Server.Host; PortTextBox.Text = Server.Port.ToString(); @@ -114,10 +116,16 @@ } } + private void SetTitle(string title = null) + { + title = (title ?? Server.Name) + (Server.Enabled ? "" : " (disabled)"); + Text = title; + TitleLabel.Text = title; + } + private void NameTextBox_TextChanged(object sender, EventArgs e) { - Text = NameTextBox.Text; - TitleLabel.Text = NameTextBox.Text; + SetTitle(NameTextBox.Text); } private void LoginComboBox_SelectedIndexChanged(object sender, EventArgs e) @@ -231,6 +239,7 @@ private void BindChangeListeners() { + Server.EnabledChanged += Server_EnabledChanged; foreach (Control control in ServerInfoPanel.Controls.OfType<Control>().Where(c => c is TextBox)) control.TextChanged += (sender, e) => UpdateServer(false); foreach (Control control in ServerInfoPanel.Controls.OfType<Control>().Where(c => c is ComboBox)) @@ -383,5 +392,45 @@ { return filteredStatuses.Contains(result.CheckStatus) && (LogCheckComboBox.SelectedIndex == 0 || LogCheckComboBox.SelectedItem == result.Check); } + + private void KeyTextBox_Leave(object sender, EventArgs e) + { + OpenPrivateKey(monitor, Server, this); + } + + public static void OpenPrivateKey(ServerMonitor monitor, Server server, IWin32Window owner) + { + if (server.LoginType != LoginType.PrivateKey) + return; + + KeyStatus keyStatus = monitor.OpenPrivateKey(server.KeyFile); + if (keyStatus == KeyStatus.NeedPassword) + { + string message = "Private key password for " + server.Name + ":"; + Icon icon = SystemIcons.Question; + while (keyStatus != KeyStatus.Open) + { + string password = InputDialog.ShowDialog(message, icon, owner); + if (password == null) + return; + keyStatus = monitor.OpenPrivateKey(server.KeyFile, password); + if (keyStatus == KeyStatus.NeedPassword) + { + message = "Incorrect private key password for " + server.Name + ", please try again:"; + icon = SystemIcons.Error; + } + } + } + else if (keyStatus == KeyStatus.NotAccessible) + { + MessageBox.Show("The private key file " + server.KeyFile + " is not accessible or does not exist.", "Cannot open private key", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + } + + private void Server_EnabledChanged(object sender, EventArgs e) + { + SetTitle(); + } } }