// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System.Globalization; using System.Management.Automation.Host; using System.Management.Automation.Remoting.Server; using System.Management.Automation.Runspaces; using Dbg = System.Management.Automation.Diagnostics; namespace System.Management.Automation.Remoting { /// /// The ServerRemoteHost class. /// internal class ServerRemoteHost : PSHost, IHostSupportsInteractiveSession { #region Private Members /// /// Remote host user interface. /// private readonly ServerRemoteHostUserInterface _remoteHostUserInterface; /// /// Server method executor. /// private readonly ServerMethodExecutor _serverMethodExecutor; /// /// Client runspace pool id. /// private readonly Guid _clientRunspacePoolId; /// /// Client power shell id. /// private readonly Guid _clientPowerShellId; /// /// Transport manager. /// protected AbstractServerTransportManager _transportManager; /// /// ServerDriverRemoteHost. /// private readonly ServerDriverRemoteHost _serverDriverRemoteHost; #endregion #region Constructor /// /// Constructor for ServerRemoteHost. /// internal ServerRemoteHost( Guid clientRunspacePoolId, Guid clientPowerShellId, HostInfo hostInfo, AbstractServerTransportManager transportManager, Runspace runspace, ServerDriverRemoteHost serverDriverRemoteHost) { _clientRunspacePoolId = clientRunspacePoolId; _clientPowerShellId = clientPowerShellId; Dbg.Assert(hostInfo != null, "Expected hostInfo != null"); Dbg.Assert(transportManager != null, "Expected transportManager != null"); // Set host-info and the transport-manager. HostInfo = hostInfo; _transportManager = transportManager; _serverDriverRemoteHost = serverDriverRemoteHost; // Create the executor for the host methods. _serverMethodExecutor = new ServerMethodExecutor( clientRunspacePoolId, clientPowerShellId, _transportManager); // Use HostInfo to create host-UI as null or non-null based on the client's host-UI. _remoteHostUserInterface = hostInfo.IsHostUINull ? null : new ServerRemoteHostUserInterface(this); Runspace = runspace; } #endregion #region Properties /// /// Server method executor. /// internal ServerMethodExecutor ServerMethodExecutor { get { return _serverMethodExecutor; } } /// /// The user interface. /// public override PSHostUserInterface UI { get { return _remoteHostUserInterface; } } /// /// Name. /// public override string Name { get { return "ServerRemoteHost"; } } /// /// Version. /// public override Version Version { get { return RemotingConstants.HostVersion; } } /// /// Instance id. /// public override Guid InstanceId { get; } = Guid.NewGuid(); /// /// Is runspace pushed. /// public virtual bool IsRunspacePushed { get { if (_serverDriverRemoteHost != null) { return _serverDriverRemoteHost.IsRunspacePushed; } else { throw RemoteHostExceptions.NewNotImplementedException(RemoteHostMethodId.GetIsRunspacePushed); } } } /// /// Runspace. /// public Runspace Runspace { get; internal set; } /// /// Host info. /// internal HostInfo HostInfo { get; } #endregion #region Method Overrides /// /// Set should exit. /// public override void SetShouldExit(int exitCode) { _serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.SetShouldExit, new object[] { exitCode }); } /// /// Enter nested prompt. /// public override void EnterNestedPrompt() { throw RemoteHostExceptions.NewNotImplementedException(RemoteHostMethodId.EnterNestedPrompt); } /// /// Exit nested prompt. /// public override void ExitNestedPrompt() { throw RemoteHostExceptions.NewNotImplementedException(RemoteHostMethodId.ExitNestedPrompt); } /// /// Notify begin application. /// public override void NotifyBeginApplication() { // This is called when a native application is executed on the server. It gives the // host an opportunity to save state that might be altered by the native application. // This call should not be sent to the client because the native application running // on the server cannot affect the state of the machine on the client. } /// /// Notify end application. /// public override void NotifyEndApplication() { // See note in NotifyBeginApplication. } /// /// Current culture. /// public override CultureInfo CurrentCulture { get { // Return the thread's current culture and rely on WinRM to set this // correctly based on the client's culture. return CultureInfo.CurrentCulture; } } /// /// Current ui culture. /// public override CultureInfo CurrentUICulture { get { // Return the thread's current UI culture and rely on WinRM to set // this correctly based on the client's UI culture. return CultureInfo.CurrentUICulture; } } #endregion #region Methods /// /// Push runspace. /// public virtual void PushRunspace(Runspace runspace) { if (_serverDriverRemoteHost != null) { _serverDriverRemoteHost.PushRunspace(runspace); } else { throw RemoteHostExceptions.NewNotImplementedException(RemoteHostMethodId.PushRunspace); } } /// /// Pop runspace. /// public virtual void PopRunspace() { if ((_serverDriverRemoteHost != null) && (_serverDriverRemoteHost.IsRunspacePushed)) { if (_serverDriverRemoteHost.PropagatePop) { // Forward the PopRunspace command to client and keep *this* pushed runspace as // the configured JEA restricted session. _serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.PopRunspace); } else { _serverDriverRemoteHost.PopRunspace(); } } else { _serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.PopRunspace); } } #endregion } /// /// The remote host class for the ServerRunspacePoolDriver. /// internal class ServerDriverRemoteHost : ServerRemoteHost { #region Private Members private RemoteRunspace _pushedRunspace; private ServerRemoteDebugger _debugger; private bool _hostSupportsPSEdit; #endregion #region Constructor internal ServerDriverRemoteHost( Guid clientRunspacePoolId, Guid clientPowerShellId, HostInfo hostInfo, AbstractServerSessionTransportManager transportManager, ServerRemoteDebugger debugger) : base(clientRunspacePoolId, clientPowerShellId, hostInfo, transportManager, null, null) { _debugger = debugger; } #endregion #region Overrides /// /// True if runspace is pushed. /// public override bool IsRunspacePushed { get { return (_pushedRunspace != null); } } /// /// Push runspace to use for remote command execution. /// /// RemoteRunspace. public override void PushRunspace(Runspace runspace) { if (_debugger == null) { throw new PSInvalidOperationException(RemotingErrorIdStrings.ServerDriverRemoteHostNoDebuggerToPush); } if (_pushedRunspace != null) { throw new PSInvalidOperationException(RemotingErrorIdStrings.ServerDriverRemoteHostAlreadyPushed); } if (runspace is not RemoteRunspace remoteRunspace) { throw new PSInvalidOperationException(RemotingErrorIdStrings.ServerDriverRemoteHostNotRemoteRunspace); } // PSEdit support. Existence of RemoteSessionOpenFileEvent event indicates host supports PSEdit _hostSupportsPSEdit = false; PSEventManager localEventManager = Runspace?.Events; _hostSupportsPSEdit = localEventManager != null && localEventManager.GetEventSubscribers(HostUtilities.RemoteSessionOpenFileEvent).GetEnumerator().MoveNext(); if (_hostSupportsPSEdit) { AddPSEditForRunspace(remoteRunspace); } _debugger.PushDebugger(runspace.Debugger); _pushedRunspace = remoteRunspace; } /// /// Pop runspace. /// public override void PopRunspace() { if (_pushedRunspace != null) { _debugger?.PopDebugger(); if (_hostSupportsPSEdit) { RemovePSEditFromRunspace(_pushedRunspace); } if (_pushedRunspace.ShouldCloseOnPop) { _pushedRunspace.Close(); } _pushedRunspace = null; } } #endregion #region Properties /// /// Server Debugger. /// internal Debugger ServerDebugger { get { return _debugger; } set { _debugger = value as ServerRemoteDebugger; } } /// /// Pushed runspace or null. /// internal Runspace PushedRunspace { get { return _pushedRunspace; } } /// /// When true will propagate pop call to client after popping runspace from this /// host. Used for OutOfProc remote sessions in a restricted (pushed) remote runspace, /// where a pop (exit session) should occur. /// internal bool PropagatePop { get; set; } #endregion #region PSEdit Support for ISE Host private void AddPSEditForRunspace(RemoteRunspace remoteRunspace) { if (remoteRunspace.Events == null) { return; } // Add event handler. remoteRunspace.Events.ReceivedEvents.PSEventReceived += HandleRemoteSessionForwardedEvent; // Add script function. using (PowerShell powershell = PowerShell.Create()) { powershell.Runspace = remoteRunspace; powershell.AddScript(HostUtilities.CreatePSEditFunction).AddParameter("PSEditFunction", HostUtilities.PSEditFunction); try { powershell.Invoke(); } catch (RemoteException) { } } } private void RemovePSEditFromRunspace(RemoteRunspace remoteRunspace) { if (remoteRunspace.Events == null) { return; } // It is possible for the popped runspace to be in a bad state after an error. if ((remoteRunspace.RunspaceStateInfo.State != RunspaceState.Opened) || (remoteRunspace.RunspaceAvailability != RunspaceAvailability.Available)) { return; } // Remove event handler. remoteRunspace.Events.ReceivedEvents.PSEventReceived -= HandleRemoteSessionForwardedEvent; // Remove script function. using (PowerShell powershell = PowerShell.Create()) { powershell.Runspace = remoteRunspace; powershell.AddScript(HostUtilities.RemovePSEditFunction); try { powershell.Invoke(); } catch (RemoteException) { } } } private void HandleRemoteSessionForwardedEvent(object sender, PSEventArgs args) { if ((Runspace == null) || (Runspace.Events == null)) { return; } // Forward events from nested pushed session to parent session. try { Runspace.Events.GenerateEvent( sourceIdentifier: args.SourceIdentifier, sender: null, args: args.SourceArgs, extraData: null); } catch (Exception) { } } #endregion } }