/// <summary> /// Check which groups in created sessions containers (GroupBoxes) are part /// of private keys setup. If group is part of PK setup change properties /// of password textboxes. /// Calls IsGroupBetweenPrivateKeys(). /// </summary> public void DetermineIfSessionGroupIsPasswordLess(List <GroupBox> containers_list) { GetSavedSessions saved_data = new GetSavedSessions(); SavedPrivatekeysInfo privatekeys = null; if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PuTTYStorm", "privatekeys.xml"))) { privatekeys = saved_data.get_PrivateKeys(); } else { return; } foreach (GroupBox container in containers_list) { string group = null; string sub_group = null; TextBox password_textbox = null; foreach (Control control in container.Controls) { if (control.Name == "password_textbox") { password_textbox = (TextBox)control; } if (control.Name == "combobox") { group = control.Text; } if (control.Name == "sub_groups_combobox") { sub_group = control.Text; } } if ((IsGroupBetweenPrivateKeys(privatekeys, group)) || (IsGroupBetweenPrivateKeys(privatekeys, sub_group))) { password_textbox.ReadOnly = true; password_textbox.BackColor = System.Drawing.Color.White; password_textbox.ForeColor = Color.SlateGray; password_textbox.UseSystemPasswordChar = false; password_textbox.Text = "PASSWORDLESS"; } else { if (password_textbox.Text == "PASSWORDLESS") { password_textbox.ReadOnly = false; password_textbox.BackColor = System.Drawing.Color.White; password_textbox.ForeColor = Color.Black; password_textbox.UseSystemPasswordChar = true; password_textbox.Text = ""; } } } }
/// <summary> /// Start SFTP Manager /// </summary> private void StartSFTPManager(Tuple <string, string, string> _credentials) { string PrivateKey = null; string hostname = _credentials.Item1; string login = _credentials.Item2; string password = _credentials.Item3; string pk_pwd = null; if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PuTTYStorm", "privatekeys.xml"))) { SavedPrivatekeysInfo privatekeys = saved_data.get_PrivateKeys(); string[] groups = IsPasswordLess.GetGroupsForPwdLessHostname(containers_list, _credentials.Item1); // Check if group or su-group is part of private keys configuration setup if ((IsPasswordLess.IsGroupBetweenPrivateKeys(privatekeys, groups[0])) || (IsPasswordLess.IsGroupBetweenPrivateKeys(privatekeys, groups[1]))) { // Fetch private key and password for group PrivateKey = IsPasswordLess.GetOpenSSHPrivateKeyForGroup(privatekeys, groups[0]); pk_pwd = IsPasswordLess.GetOpenSSHPrivateKeyPassPhrase(privatekeys, groups[0]); // If private key and password is still null, then sub-group is part of its setup - fetch it! if (PrivateKey == null && pk_pwd == null) { Console.WriteLine("## Sub-group is part of pwdess login!"); PrivateKey = IsPasswordLess.GetOpenSSHPrivateKeyForGroup(privatekeys, groups[1]); pk_pwd = IsPasswordLess.GetOpenSSHPrivateKeyPassPhrase(privatekeys, groups[1]); } // If private key doesn't exists or is still null then something is wrong! Stop processing and return! if (!File.Exists(PrivateKey)) { if (PrivateKey == null || PrivateKey == "") { PrivateKey = "of OpenSSH type or its group"; } MessageBox.Show("You are going to use SFTP Manager passwordless login, " + Environment.NewLine + "however private key " + PrivateKey + " doesn't exists!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } password = null; } } SFTPManager sftpManagerForm = new SFTPManager(hostname, login, password, PrivateKey, pk_pwd); sftpManagerForm.Name = "SFTPManager"; sftpManagerForm.Text = GlobalVar.VERSION + " - SFTP Manager"; sftpManagerForm.Show(); }
/// <summary> /// Get passphrase for encrypted openSSH private key /// </summary> public string GetOpenSSHPrivateKeyPassPhrase(SavedPrivatekeysInfo privatekeys, string session_group) { string PassPhrase = null; if (privatekeys.names.Count != 0) { for (int i = 0; i < privatekeys.names.Count; i++) { if (privatekeys.groups[i] == session_group && privatekeys.types[i] == "OpenSSH") { PassPhrase = privatekeys.pwds[i]; } } } return(PassPhrase); }
/// <summary> /// Get path of OpenSSH private key. /// </summary> public string GetOpenSSHPrivateKeyForGroup(SavedPrivatekeysInfo privatekeys, string session_group) { string OpenSSHPrivateKey = null; if (privatekeys.names.Count != 0) { for (int i = 0; i < privatekeys.names.Count; i++) { if (privatekeys.groups[i] == session_group && privatekeys.types[i] == "OpenSSH") { OpenSSHPrivateKey = privatekeys.names[i]; } } } return(OpenSSHPrivateKey); }
/// <summary> /// Check if session group is between private keys and return true/false based on that. /// </summary> public bool IsGroupBetweenPrivateKeys(SavedPrivatekeysInfo privatekeys, string session_group) { bool IsPrivateKey = false; if (privatekeys.names.Count != 0) { for (int i = 0; i < privatekeys.names.Count; i++) { if (privatekeys.groups[i] == session_group) { IsPrivateKey = true; } } } return(IsPrivateKey); }
public KotarakMainForm(List <GroupBox> _containers_list) { InitializeComponent(); this.containers_list = _containers_list; custom_controls.initialize_kotarak_group_subgroup_combobox(SelectGroupSubGroupCombobox); // By default we are using login credentials from saved sessions. // Login and password textbox is disabled. LoginTextBox.Enabled = false; PasswordTextBox.Enabled = false; LoginLabel.ForeColor = Color.LightGray; PasswordLabel.ForeColor = Color.LightGray; ForceAccountCheckBox.CheckStateChanged += new EventHandler(ForceAccountCheckBox_CheckStateChanged); BashScriptRadioButton.Checked = true; // Initialize datagridview with servers InitializeDataGridView(); // Initialize scintillaNet editor with Bash lexer EditorInit.BashInit(BashScriptRadioButton, scintilla1); // Explicit DPI scaling for splitContainer3 (datagridView and Settings) splitContainer3.SplitterDistance = DPIAwareScaling.KotarakSplitterDistance; // explicit DPI scaling for splitContainer2 (scintillaNet and Settings) splitContainer2.SplitterDistance = DPIAwareScaling.KotarakSplitterDistance; //dataGridView1.Columns[0].Width = DPIAwareScaling._ScaleX(98); // Encrypted private keys have encrypted passphrases, so get them only once during initialization. if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PuTTYStorm", "privatekeys.xml"))) { this.privatekeys = saved_data.get_PrivateKeys(); } }
/// <summary> /// Save private keys to the privatekeys.xml configuration file. /// </summary> public void Save_PrivateKeys(List <Panel> PrivateKeys) { String filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PuTTYStorm", "privatekeys.xml"); if (!Directory.Exists(Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "PuTTYStorm"))) { Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "PuTTYStorm")); } using (XmlWriter writer = XmlWriter.Create(filePath)) { writer.WriteStartDocument(); writer.WriteStartElement("PrivateKeys"); foreach (Panel container in PrivateKeys) { string pk_name = null; string pk_type = null; string pk_group = null; string pk_pwd = null; foreach (Control control in container.Controls) { if (control.Name == "pk_name_label") { pk_name = control.Text; } if (control.Name == "pk_type_label") { pk_type = control.Text.Replace("Type: ", string.Empty); } if (control.Name == "pk_group_label") { pk_group = control.Text.Replace("Group: ", string.Empty); } if (control.Name == "private_keys_hidden_passphrase_textbox") { pk_pwd = control.Text; } } if (pk_name != "") { writer.WriteStartElement("PrivateKey"); writer.WriteElementString("name", pk_name); writer.WriteElementString("type", pk_type); writer.WriteElementString("group", pk_group); if (pk_pwd == "" || pk_pwd == null) { writer.WriteElementString("pwd", " "); } else { writer.WriteElementString("pwd", AESEncryptDecrypt.Encrypt(pk_pwd)); } writer.WriteEndElement(); } } writer.WriteEndElement(); writer.WriteEndDocument(); } GetSavedSessions saved_data = new GetSavedSessions(); SavedPrivatekeysInfo privatekeys = null; if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PuTTYStorm", "privatekeys.xml"))) { privatekeys = saved_data.get_PrivateKeys(); } if (privatekeys.names.Count != 0) { Backup_PrivateKeys(); } }
/// <summary> /// Get saved private keys from privatekeys.xml configuration file. /// </summary> /// <returns>Class with List of private keys</returns> public SavedPrivatekeysInfo get_PrivateKeys() { SavedPrivatekeysInfo xml_privatekeys_info = new SavedPrivatekeysInfo(); String filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PuTTYStorm", "privatekeys.xml"); try { using (XmlReader reader = XmlReader.Create(filePath)) { while (reader.Read()) { if (reader.IsStartElement()) { switch (reader.Name) { case "name": if (reader.Read()) { xml_privatekeys_info.names.Add(reader.Value); } break; case "type": if (reader.Read()) { xml_privatekeys_info.types.Add(reader.Value); } break; case "group": if (reader.Read()) { xml_privatekeys_info.groups.Add(reader.Value); } break; case "pwd": if (reader.Read()) { if (reader.Value == " ") { xml_privatekeys_info.pwds.Add(null); } else { xml_privatekeys_info.pwds.Add(AESEncryptDecrypt.Decrypt(reader.Value)); } } break; } } } } } catch (Exception e) { MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); } return(xml_privatekeys_info); }