Esempio n. 1
0
        private AsyncAction.SudoElevationResult GetElevatedSession(List <Role> allowedRoles,
                                                                   IXenConnection connection, string actionTitle)
        {
            AsyncAction.SudoElevationResult result = null;

            Program.Invoke(Program.MainWindow, () =>
            {
                Form owner;
                try
                {
                    //CA-337323: make an attempt to find the right owning form
                    //most likely it will be the last one opened
                    owner = Application.OpenForms.Cast <Form>().Last();
                }
                catch
                {
                    owner = Program.MainWindow;
                }

                using (var d = new RoleElevationDialog(connection, connection.Session, allowedRoles, actionTitle))
                    if (d.ShowDialog(owner) == DialogResult.OK)
                    {
                        result = new AsyncAction.SudoElevationResult(d.elevatedUsername, d.elevatedPassword, d.elevatedSession);
                    }
            });

            return(result);
        }
        private void EnterMaintenanceMode(Host host)
        {
            Pool pool = Helpers.GetPool(host.Connection);

            if (pool != null && pool.ha_enabled && host.IsMaster())
            {
                using (var dlg = new ErrorDialog(string.Format(Messages.HA_CANNOT_EVACUATE_MASTER,
                                                               Helpers.GetName(host).Ellipsise(Helpers.DEFAULT_NAME_TRIM_LENGTH))))
                {
                    dlg.ShowDialog(Parent);
                }

                return;
            }

            if (!host.GetRunningVMs().Any() &&
                (pool == null || pool.Connection.Cache.Hosts.Length == 1 || !host.IsMaster()))
            {
                Program.MainWindow.CloseActiveWizards(host.Connection);
                var action = new EvacuateHostAction(host, null, new Dictionary <XenRef <VM>, string[]>(), AddHostToPoolCommand.NtolDialog, AddHostToPoolCommand.EnableNtolDialog);
                action.Completed += Program.MainWindow.action_Completed;
                action.RunAsync();
                return;
            }

            //The EvacuateHostDialog uses several different actions all of which might need an elevated session
            //We sudo once for all of them and store the session

            string  elevatedUsername = null;
            string  elevatedPassword = null;
            Session elevatedSession  = null;

            if (!host.Connection.Session.IsLocalSuperuser &&
                !Registry.DontSudo &&
                !Role.CanPerform(new RbacMethodList(EvacuateHostDialog.RbacMethods), host.Connection, out var validRoles))
            {
                using (var d = new RoleElevationDialog(host.Connection, host.Connection.Session, validRoles,
                                                       string.Format(Messages.EVACUATE_HOST_DIALOG_TITLE, host.Name())))
                    if (d.ShowDialog(Program.MainWindow) == DialogResult.OK)
                    {
                        elevatedUsername = d.elevatedUsername;
                        elevatedPassword = d.elevatedPassword;
                        elevatedSession  = d.elevatedSession;
                    }
                    else
                    {
                        return;
                    }
            }

            new EvacuateHostDialog(host, elevatedUsername, elevatedPassword, elevatedSession)
            .ShowPerXenObject(host, Program.MainWindow);
        }