/* Copyright (c) Citrix Systems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, * with or without modification, are permitted provided * that the following conditions are met: * * * Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ using System; using System.Drawing; using System.Windows.Forms; using XenAdmin.Actions; using XenAdmin.Core; using XenAdmin.Dialogs.RestoreSession; namespace XenAdmin.Dialogs.OptionsPages { /// /// The page is used to set whether or not to save server usernames and passwords and whether a coordinator password should be set to protect these passwords /// public partial class SaveAndRestoreOptionsPage : UserControl, IOptionsPage { private byte[] TemporaryMainPassword; // call save serverlist on OK protected internal bool SaveAllAfter { get; set; } public SaveAndRestoreOptionsPage() { InitializeComponent(); saveStateLabel.Text = string.Format(saveStateLabel.Text, BrandManager.BrandConsole); } // all prompts for old password should have been made private void SaveEverything() { if (!Registry.AllowCredentialSave) { return; } if (!saveStateCheckBox.Checked) { // save nothing and nobody (personally my two favourite servers anyway...) Properties.Settings.Default.SaveSession = false; Properties.Settings.Default.RequirePass = false; Program.MainPassword = null; } else if (!requireMainPasswordCheckBox.Checked) { // we need to save stuff but without a password Properties.Settings.Default.SaveSession = true; Properties.Settings.Default.RequirePass = false; Program.MainPassword = null; } else { // password protect stuff Properties.Settings.Default.SaveSession = true; Properties.Settings.Default.RequirePass = true; // set password if (Program.MainPassword != TemporaryMainPassword) { Program.MainPassword = TemporaryMainPassword; new ActionBase(string.Format(Messages.CHANGED_MAIN_PASSWORD, BrandManager.BrandConsole), string.Format(Messages.CHANGED_MAIN_PASSWORD_LONG, BrandManager.BrandConsole), false, true); } } if (SaveAllAfter) Settings.SaveServerList(); } #region Control event handlers private void changeMainPasswordButton_Click(object sender, EventArgs e) { // tell the dialog what to use as the "current" password using (var changePassword = new ChangeMainPasswordDialog(TemporaryMainPassword)) { if (changePassword.ShowDialog(this) == DialogResult.OK) { // password has been successfully changed TemporaryMainPassword = changePassword.NewPassword; } } } private void requireMainPasswordCheckBox_Click(object sender, EventArgs e) { // requireCoordinatorPasswordCheckBox.Checked was the state before the click // if previously checked, the user is trying to clear it => request authorization // if previously unchecked, the user is trying to set a password if (requireMainPasswordCheckBox.Checked) { using (var enterPassword = new EnterMainPasswordDialog(TemporaryMainPassword)) { if (enterPassword.ShowDialog(this) == DialogResult.OK) { TemporaryMainPassword = null; requireMainPasswordCheckBox.Checked = false; changeMainPasswordButton.Enabled = false; } } } else { System.Diagnostics.Debug.Assert(TemporaryMainPassword == null, "Main password is set, but not reflected on GUI"); if (TemporaryMainPassword == null) { // no previous password existed => set a new one using (var setPassword = new SetMainPasswordDialog()) { if (setPassword.ShowDialog(this) == DialogResult.OK) { TemporaryMainPassword = setPassword.NewPassword; requireMainPasswordCheckBox.Checked = true; changeMainPasswordButton.Enabled = true; } } } else { // a previous password existed (should never get here but just in case) // enable button to facilitate password change requireMainPasswordCheckBox.Checked = true; changeMainPasswordButton.Enabled = true; } } } private void saveStateCheckBox_Click(object sender, EventArgs e) { // need to prevent the user from going to an open terminal and clearing // the save state, then setting the coordinator password to anything they like // saveStateCheckBox.Checked was the state before the click // if previously checked, the user is trying to clear it => authorization maybe required // (depending on the state of the requireCoordinatorPasswordCheckBox; this should be cleared too if checked) if (saveStateCheckBox.Checked && requireMainPasswordCheckBox.Checked) { using (var enterPassword = new EnterMainPasswordDialog(TemporaryMainPassword)) { if (enterPassword.ShowDialog(this) == DialogResult.OK) { TemporaryMainPassword = null; saveStateCheckBox.Checked = false; requireMainPasswordCheckBox.Checked = false; mainPasswordGroupBox.Enabled = false; } } } else { saveStateCheckBox.Checked = !saveStateCheckBox.Checked; mainPasswordGroupBox.Enabled = saveStateCheckBox.Checked; changeMainPasswordButton.Enabled = requireMainPasswordCheckBox.Checked; } } #endregion #region IOptionsPage Members public void Build() { bool allowCredSave = Registry.AllowCredentialSave; bool saveSession = Properties.Settings.Default.SaveSession; bool reqPass = Properties.Settings.Default.RequirePass; saveStateLabel.Enabled = allowCredSave; saveStateCheckBox.Enabled = allowCredSave; // use the SaveSession variable to denote whether to save passwords or not saveStateCheckBox.Checked = saveSession && allowCredSave; mainPasswordGroupBox.Enabled = saveSession && allowCredSave; // use the RequirePass variable to say if a main password has been set requireMainPasswordCheckBox.Checked = reqPass && Program.MainPassword != null && allowCredSave; changeMainPasswordButton.Enabled = reqPass && Program.MainPassword != null && allowCredSave; // the temporary password starts as the MainPassword TemporaryMainPassword = Program.MainPassword; } public bool IsValidToSave() { return true; } public void ShowValidationMessages() { } public void HideValidationMessages() { } public void Save() { SaveEverything(); } #endregion #region IVerticalTab Members public override string Text => Messages.SAVE_AND_RESTORE; public string SubText => Messages.SAVE_AND_RESTORE_DESC; public Image Image => Images.StaticImages._000_BackupMetadata_h32bit_16; #endregion } }