comparison 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
comparison
equal deleted inserted replaced
4:3142e52cbe69 5:b6fe203af9d5
1 using ServerMonitorApp.Properties; 1 using Renci.SshNet;
2 using Renci.SshNet.Common;
3 using ServerMonitorApp.Properties;
2 using System; 4 using System;
3 using System.Collections.Generic; 5 using System.Collections.Generic;
4 using System.ComponentModel; 6 using System.ComponentModel;
5 using System.Data; 7 using System.Data;
6 using System.Drawing; 8 using System.Drawing;
9 using System.IO;
7 using System.Linq; 10 using System.Linq;
8 using System.Text; 11 using System.Text;
9 using System.Threading.Tasks; 12 using System.Threading.Tasks;
10 using System.Windows.Forms; 13 using System.Windows.Forms;
11 14
52 { 55 {
53 LoginComboBox.SelectedIndex = 0; 56 LoginComboBox.SelectedIndex = 0;
54 } 57 }
55 else 58 else
56 { 59 {
57 Text = Server.Name; 60 SetTitle();
58 TitleLabel.Text = Server.Name;
59 NameTextBox.Text = Server.Name; 61 NameTextBox.Text = Server.Name;
60 HostTextBox.Text = Server.Host; 62 HostTextBox.Text = Server.Host;
61 PortTextBox.Text = Server.Port.ToString(); 63 PortTextBox.Text = Server.Port.ToString();
62 UsernameTextBox.Text = Server.Username; 64 UsernameTextBox.Text = Server.Username;
63 PasswordTextBox.Text = "********************"; 65 PasswordTextBox.Text = "********************";
112 lastSaveTime = DateTime.Now; 114 lastSaveTime = DateTime.Now;
113 monitor.SaveServers(); 115 monitor.SaveServers();
114 } 116 }
115 } 117 }
116 118
119 private void SetTitle(string title = null)
120 {
121 title = (title ?? Server.Name) + (Server.Enabled ? "" : " (disabled)");
122 Text = title;
123 TitleLabel.Text = title;
124 }
125
117 private void NameTextBox_TextChanged(object sender, EventArgs e) 126 private void NameTextBox_TextChanged(object sender, EventArgs e)
118 { 127 {
119 Text = NameTextBox.Text; 128 SetTitle(NameTextBox.Text);
120 TitleLabel.Text = NameTextBox.Text;
121 } 129 }
122 130
123 private void LoginComboBox_SelectedIndexChanged(object sender, EventArgs e) 131 private void LoginComboBox_SelectedIndexChanged(object sender, EventArgs e)
124 { 132 {
125 if (LoginComboBox.SelectedIndex == (int)LoginType.PrivateKey) 133 if (LoginComboBox.SelectedIndex == (int)LoginType.PrivateKey)
229 Helpers.FormatImageButton(button); 237 Helpers.FormatImageButton(button);
230 } 238 }
231 239
232 private void BindChangeListeners() 240 private void BindChangeListeners()
233 { 241 {
242 Server.EnabledChanged += Server_EnabledChanged;
234 foreach (Control control in ServerInfoPanel.Controls.OfType<Control>().Where(c => c is TextBox)) 243 foreach (Control control in ServerInfoPanel.Controls.OfType<Control>().Where(c => c is TextBox))
235 control.TextChanged += (sender, e) => UpdateServer(false); 244 control.TextChanged += (sender, e) => UpdateServer(false);
236 foreach (Control control in ServerInfoPanel.Controls.OfType<Control>().Where(c => c is ComboBox)) 245 foreach (Control control in ServerInfoPanel.Controls.OfType<Control>().Where(c => c is ComboBox))
237 control.TextChanged += (sender, e) => UpdateServer(); 246 control.TextChanged += (sender, e) => UpdateServer();
238 foreach (CheckBox control in LogTabPage.Controls.OfType<CheckBox>()) 247 foreach (CheckBox control in LogTabPage.Controls.OfType<CheckBox>())
381 390
382 private bool MatchesFilter(CheckResult result) 391 private bool MatchesFilter(CheckResult result)
383 { 392 {
384 return filteredStatuses.Contains(result.CheckStatus) && (LogCheckComboBox.SelectedIndex == 0 || LogCheckComboBox.SelectedItem == result.Check); 393 return filteredStatuses.Contains(result.CheckStatus) && (LogCheckComboBox.SelectedIndex == 0 || LogCheckComboBox.SelectedItem == result.Check);
385 } 394 }
395
396 private void KeyTextBox_Leave(object sender, EventArgs e)
397 {
398 OpenPrivateKey(monitor, Server, this);
399 }
400
401 public static void OpenPrivateKey(ServerMonitor monitor, Server server, IWin32Window owner)
402 {
403 if (server.LoginType != LoginType.PrivateKey)
404 return;
405
406 KeyStatus keyStatus = monitor.OpenPrivateKey(server.KeyFile);
407 if (keyStatus == KeyStatus.NeedPassword)
408 {
409 string message = "Private key password for " + server.Name + ":";
410 Icon icon = SystemIcons.Question;
411 while (keyStatus != KeyStatus.Open)
412 {
413 string password = InputDialog.ShowDialog(message, icon, owner);
414 if (password == null)
415 return;
416 keyStatus = monitor.OpenPrivateKey(server.KeyFile, password);
417 if (keyStatus == KeyStatus.NeedPassword)
418 {
419 message = "Incorrect private key password for " + server.Name + ", please try again:";
420 icon = SystemIcons.Error;
421 }
422 }
423 }
424 else if (keyStatus == KeyStatus.NotAccessible)
425 {
426 MessageBox.Show("The private key file " + server.KeyFile + " is not accessible or does not exist.", "Cannot open private key", MessageBoxButtons.OK, MessageBoxIcon.Error);
427 return;
428 }
429 }
430
431 private void Server_EnabledChanged(object sender, EventArgs e)
432 {
433 SetTitle();
434 }
386 } 435 }
387 } 436 }