Ejemplo n.º 1
0
        protected OutOfProcessServerSessionTransportManager CreateSessionTransportManager(string configurationName, PSRemotingCryptoHelperServer cryptoHelper, string workingDirectory)
        {
            PSSenderInfo senderInfo;

#if !UNIX
            WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
            PSPrincipal     userPrincipal   = new PSPrincipal(
                new PSIdentity(string.Empty, true, currentIdentity.Name, null),
                currentIdentity);
            senderInfo = new PSSenderInfo(userPrincipal, "http://localhost");
#else
            PSPrincipal userPrincipal = new PSPrincipal(
                new PSIdentity(string.Empty, true, string.Empty, null),
                null);
            senderInfo = new PSSenderInfo(userPrincipal, "http://localhost");
#endif

            OutOfProcessServerSessionTransportManager tm = new OutOfProcessServerSessionTransportManager(originalStdOut, originalStdErr, cryptoHelper);

            ServerRemoteSession.CreateServerRemoteSession(
                senderInfo,
                _initialCommand,
                tm,
                configurationName,
                workingDirectory);

            return(tm);
        }
Ejemplo n.º 2
0
 private OutOfProcessServerSessionTransportManager CreateSessionTransportManager()
 {
     WindowsIdentity current = WindowsIdentity.GetCurrent();
     PSPrincipal userPrincipal = new PSPrincipal(new PSIdentity("", true, current.Name, null), current);
     PSSenderInfo senderInfo = new PSSenderInfo(userPrincipal, "http://localhost");
     OutOfProcessServerSessionTransportManager transportManager = new OutOfProcessServerSessionTransportManager(this.originalStdOut);
     ServerRemoteSession.CreateServerRemoteSession(senderInfo, this._initialCommand, transportManager);
     return transportManager;
 }
Ejemplo n.º 3
0
        private OutOfProcessServerSessionTransportManager CreateSessionTransportManager()
        {
            WindowsIdentity current       = WindowsIdentity.GetCurrent();
            PSPrincipal     userPrincipal = new PSPrincipal(new PSIdentity("", true, current.Name, null), current);
            PSSenderInfo    senderInfo    = new PSSenderInfo(userPrincipal, "http://localhost");
            OutOfProcessServerSessionTransportManager transportManager = new OutOfProcessServerSessionTransportManager(this.originalStdOut);

            ServerRemoteSession.CreateServerRemoteSession(senderInfo, this._initialCommand, transportManager);
            return(transportManager);
        }
Ejemplo n.º 4
0
        private void OnClosePacketReceived(Guid psGuid)
        {
            PowerShellTraceSource traceSource = PowerShellTraceSourceFactory.GetTraceSource();

            if (psGuid == Guid.Empty)
            {
                traceSource.WriteMessage("BEGIN calling close on session transport manager");
                bool flag = false;
                lock (this._syncObject)
                {
                    if (this._inProgressCommandsCount > 0)
                    {
                        flag = true;
                    }
                }
                if (flag)
                {
                    this.allcmdsClosedEvent.WaitOne();
                }
                lock (this._syncObject)
                {
                    traceSource.WriteMessage(string.Concat(new object[] { "OnClosePacketReceived, in progress commands count should be zero : ", this._inProgressCommandsCount, ", psGuid : ", psGuid.ToString() }));
                    if (this.sessionTM != null)
                    {
                        this.sessionTM.Close(null);
                    }
                    traceSource.WriteMessage("END calling close on session transport manager");
                    this.sessionTM = null;
                    goto Label_01D5;
                }
            }
            traceSource.WriteMessage("Closing command with GUID " + psGuid.ToString());
            AbstractServerTransportManager commandTransportManager = null;

            lock (this._syncObject)
            {
                commandTransportManager = this.sessionTM.GetCommandTransportManager(psGuid);
            }
            if (commandTransportManager != null)
            {
                commandTransportManager.Close(null);
            }
            lock (this._syncObject)
            {
                traceSource.WriteMessage(string.Concat(new object[] { "OnClosePacketReceived, in progress commands count should be greater than zero : ", this._inProgressCommandsCount, ", psGuid : ", psGuid.ToString() }));
                this._inProgressCommandsCount--;
                if (this._inProgressCommandsCount == 0)
                {
                    this.allcmdsClosedEvent.Set();
                }
            }
Label_01D5:
            this.originalStdOut.WriteLine(OutOfProcessUtils.CreateCloseAckPacket(psGuid));
        }
Ejemplo n.º 5
0
        protected OutOfProcessServerSessionTransportManager CreateSessionTransportManager(
            string configurationName,
            string configurationFile,
            PSRemotingCryptoHelperServer cryptoHelper,
            string workingDirectory)
        {
            PSSenderInfo senderInfo;

#if !UNIX
            WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
            PSPrincipal     userPrincipal   = new PSPrincipal(
                new PSIdentity(string.Empty, true, currentIdentity.Name, null),
                currentIdentity);
            senderInfo = new PSSenderInfo(userPrincipal, "http://localhost");
#else
            PSPrincipal userPrincipal = new PSPrincipal(
                new PSIdentity(string.Empty, true, string.Empty, null),
                null);
            senderInfo = new PSSenderInfo(userPrincipal, "http://localhost");
#endif

            var tm = new OutOfProcessServerSessionTransportManager(
                originalStdOut,
                originalStdErr,
                cryptoHelper);

            ServerRemoteSession.CreateServerRemoteSession(
                senderInfo: senderInfo,
                configurationProviderId: "Microsoft.PowerShell",
                initializationParameters: string.Empty,
                transportManager: tm,
                initialCommand: _initialCommand,
                configurationName: configurationName,
                configurationFile: configurationFile,
                initialLocation: workingDirectory);

            return(tm);
        }
Ejemplo n.º 6
0
 private void Start(string initialCommand)
 {
     this._initialCommand = initialCommand;
     this.sessionTM       = this.CreateSessionTransportManager();
     try
     {
         while (true)
         {
             string str = this.originalStdIn.ReadLine();
             lock (this._syncObject)
             {
                 if (this.sessionTM == null)
                 {
                     this.sessionTM = this.CreateSessionTransportManager();
                 }
             }
             if (string.IsNullOrEmpty(str))
             {
                 lock (this._syncObject)
                 {
                     this.sessionTM.Close(null);
                     this.sessionTM = null;
                 }
                 throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownElementReceived, RemotingErrorIdStrings.IPCUnknownElementReceived, new object[] { string.Empty });
             }
             ThreadPool.QueueUserWorkItem(new WaitCallback(this.ProcessingThreadStart), str);
         }
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         PSEtwLog.LogOperationalError(PSEventId.TransportError, PSOpcode.Open, PSTask.None, PSKeyword.UseAlwaysOperational, new object[] { Guid.Empty.ToString(), Guid.Empty.ToString(), 0xfa0, exception.Message, exception.StackTrace });
         PSEtwLog.LogAnalyticError(PSEventId.TransportError_Analytic, PSOpcode.Open, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { Guid.Empty.ToString(), Guid.Empty.ToString(), 0xfa0, exception.Message, exception.StackTrace });
         this.originalStdErr.WriteLine(exception.Message);
         Environment.Exit(0xfa0);
     }
 }
Ejemplo n.º 7
0
        private void Start(string initialCommand)
        {
            WindowsIdentity current    = WindowsIdentity.GetCurrent();
            PSSenderInfo    senderInfo = new PSSenderInfo(new PSPrincipal(new PSIdentity("", true, current.Name, (PSCertificateDetails)null), current), "http://localhost");

            this.sessionTM = new OutOfProcessServerSessionTransportManager(this.originalStdOut);
            ServerRemoteSession.CreateServerRemoteSession(senderInfo, initialCommand, (AbstractServerSessionTransportManager)this.sessionTM);
            try
            {
                while (true)
                {
                    string str = this.originalStdIn.ReadLine();
                    if (!string.IsNullOrEmpty(str))
                    {
                        ThreadPool.QueueUserWorkItem(new WaitCallback(this.ProcessingThreadStart), (object)str);
                    }
                    else
                    {
                        break;
                    }
                }
                this.sessionTM.Close((Exception)null);
                throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownElementReceived, new object[1]
                {
                    (object)string.Empty
                });
            }
            catch (Exception ex)
            {
                CommandProcessorBase.CheckForSevereException(ex);
                OutOfProcessMediator.ETWTracer.OperationalChannel.WriteError(PSEventId.TransportError, PSOpcode.Open, PSTask.None, (object)Guid.Empty, (object)Guid.Empty, (object)4000, (object)ex.Message, (object)ex.StackTrace);
                OutOfProcessMediator.ETWTracer.AnalyticChannel.WriteError(PSEventId.TransportError_Analytic, PSOpcode.Open, PSTask.None, (object)Guid.Empty, (object)Guid.Empty, (object)4000, (object)ex.Message, (object)ex.StackTrace);
                this.originalStdErr.WriteLine(ex.Message);
                Environment.Exit(4000);
            }
        }
Ejemplo n.º 8
0
        protected void Start(string initialCommand, PSRemotingCryptoHelperServer cryptoHelper, string workingDirectory = null, string configurationName = null)
        {
            _initialCommand = initialCommand;

            sessionTM = CreateSessionTransportManager(configurationName, cryptoHelper, workingDirectory);

            try
            {
                do
                {
                    string data = originalStdIn.ReadLine();
                    lock (_syncObject)
                    {
                        if (sessionTM == null)
                        {
                            sessionTM = CreateSessionTransportManager(configurationName, cryptoHelper, workingDirectory);
                        }
                    }

                    if (string.IsNullOrEmpty(data))
                    {
                        lock (_syncObject)
                        {
                            // give a chance to runspace/pipelines to close (as it looks like the client died
                            // intermittently)
                            sessionTM.Close(null);
                            sessionTM = null;
                        }

                        throw new PSRemotingTransportException(
                                  PSRemotingErrorId.IPCUnknownElementReceived,
                                  RemotingErrorIdStrings.IPCUnknownElementReceived,
                                  string.Empty);
                    }

                    // process data in a thread pool thread..this way Runspace, Command
                    // data can be processed concurrently.
#if !UNIX
                    Utils.QueueWorkItemWithImpersonation(
                        _windowsIdentityToImpersonate,
                        new WaitCallback(ProcessingThreadStart),
                        data);
#else
                    ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessingThreadStart), data);
#endif
                }while (true);
            }
            catch (Exception e)
            {
                PSEtwLog.LogOperationalError(
                    PSEventId.TransportError,
                    PSOpcode.Open,
                    PSTask.None,
                    PSKeyword.UseAlwaysOperational,
                    Guid.Empty.ToString(),
                    Guid.Empty.ToString(),
                    OutOfProcessUtils.EXITCODE_UNHANDLED_EXCEPTION,
                    e.Message,
                    e.StackTrace);

                PSEtwLog.LogAnalyticError(
                    PSEventId.TransportError_Analytic,
                    PSOpcode.Open,
                    PSTask.None,
                    PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic,
                    Guid.Empty.ToString(),
                    Guid.Empty.ToString(),
                    OutOfProcessUtils.EXITCODE_UNHANDLED_EXCEPTION,
                    e.Message,
                    e.StackTrace);

                if (_exitProcessOnError)
                {
                    // notify the remote client of any errors and fail gracefully
                    originalStdErr.WriteLine(e.Message);

                    Environment.Exit(OutOfProcessUtils.EXITCODE_UNHANDLED_EXCEPTION);
                }
            }
        }
Ejemplo n.º 9
0
        protected void OnClosePacketReceived(Guid psGuid)
        {
            PowerShellTraceSource tracer = PowerShellTraceSourceFactory.GetTraceSource();

            if (psGuid == Guid.Empty)
            {
                tracer.WriteMessage("BEGIN calling close on session transport manager");

                bool waitForAllcmdsClosedEvent = false;

                lock (_syncObject)
                {
                    if (_inProgressCommandsCount > 0)
                    {
                        waitForAllcmdsClosedEvent = true;
                    }
                }

                // Wait outside sync lock if required for all cmds to be closed
                //
                if (waitForAllcmdsClosedEvent)
                {
                    allcmdsClosedEvent.WaitOne();
                }

                lock (_syncObject)
                {
                    tracer.WriteMessage("OnClosePacketReceived, in progress commands count should be zero : " + _inProgressCommandsCount + ", psGuid : " + psGuid.ToString());

                    if (sessionTM != null)
                    {
                        // it appears that when closing PowerShell ISE, therefore closing OutOfProcServerMediator, there are 2 Close command requests
                        // changing PSRP/IPC at this point is too risky, therefore protecting about this duplication
                        sessionTM.Close(null);
                    }

                    tracer.WriteMessage("END calling close on session transport manager");
                    sessionTM = null;
                }
            }
            else
            {
                tracer.WriteMessage("Closing command with GUID " + psGuid.ToString());

                // this is for a command
                AbstractServerTransportManager cmdTM = null;

                lock (_syncObject)
                {
                    cmdTM = sessionTM.GetCommandTransportManager(psGuid);
                }

                // dont throw if there is no cmdTM as it might have legitimately closed
                if (cmdTM != null)
                {
                    cmdTM.Close(null);
                }

                lock (_syncObject)
                {
                    tracer.WriteMessage("OnClosePacketReceived, in progress commands count should be greater than zero : " + _inProgressCommandsCount + ", psGuid : " + psGuid.ToString());

                    _inProgressCommandsCount--;

                    if (_inProgressCommandsCount == 0)
                    {
                        allcmdsClosedEvent.Set();
                    }
                }
            }

            // send close ack
            originalStdOut.WriteLine(OutOfProcessUtils.CreateCloseAckPacket(psGuid));
        }
Ejemplo n.º 10
0
 private void OnClosePacketReceived(Guid psGuid)
 {
     PowerShellTraceSource traceSource = PowerShellTraceSourceFactory.GetTraceSource();
     if (psGuid == Guid.Empty)
     {
         traceSource.WriteMessage("BEGIN calling close on session transport manager");
         bool flag = false;
         lock (this._syncObject)
         {
             if (this._inProgressCommandsCount > 0)
             {
                 flag = true;
             }
         }
         if (flag)
         {
             this.allcmdsClosedEvent.WaitOne();
         }
         lock (this._syncObject)
         {
             traceSource.WriteMessage(string.Concat(new object[] { "OnClosePacketReceived, in progress commands count should be zero : ", this._inProgressCommandsCount, ", psGuid : ", psGuid.ToString() }));
             if (this.sessionTM != null)
             {
                 this.sessionTM.Close(null);
             }
             traceSource.WriteMessage("END calling close on session transport manager");
             this.sessionTM = null;
             goto Label_01D5;
         }
     }
     traceSource.WriteMessage("Closing command with GUID " + psGuid.ToString());
     AbstractServerTransportManager commandTransportManager = null;
     lock (this._syncObject)
     {
         commandTransportManager = this.sessionTM.GetCommandTransportManager(psGuid);
     }
     if (commandTransportManager != null)
     {
         commandTransportManager.Close(null);
     }
     lock (this._syncObject)
     {
         traceSource.WriteMessage(string.Concat(new object[] { "OnClosePacketReceived, in progress commands count should be greater than zero : ", this._inProgressCommandsCount, ", psGuid : ", psGuid.ToString() }));
         this._inProgressCommandsCount--;
         if (this._inProgressCommandsCount == 0)
         {
             this.allcmdsClosedEvent.Set();
         }
     }
 Label_01D5:
     this.originalStdOut.WriteLine(OutOfProcessUtils.CreateCloseAckPacket(psGuid));
 }
Ejemplo n.º 11
0
 private void Start(string initialCommand)
 {
     this._initialCommand = initialCommand;
     this.sessionTM = this.CreateSessionTransportManager();
     try
     {
         while (true)
         {
             string str = this.originalStdIn.ReadLine();
             lock (this._syncObject)
             {
                 if (this.sessionTM == null)
                 {
                     this.sessionTM = this.CreateSessionTransportManager();
                 }
             }
             if (string.IsNullOrEmpty(str))
             {
                 lock (this._syncObject)
                 {
                     this.sessionTM.Close(null);
                     this.sessionTM = null;
                 }
                 throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownElementReceived, RemotingErrorIdStrings.IPCUnknownElementReceived, new object[] { string.Empty });
             }
             ThreadPool.QueueUserWorkItem(new WaitCallback(this.ProcessingThreadStart), str);
         }
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         PSEtwLog.LogOperationalError(PSEventId.TransportError, PSOpcode.Open, PSTask.None, PSKeyword.UseAlwaysOperational, new object[] { Guid.Empty.ToString(), Guid.Empty.ToString(), 0xfa0, exception.Message, exception.StackTrace });
         PSEtwLog.LogAnalyticError(PSEventId.TransportError_Analytic, PSOpcode.Open, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { Guid.Empty.ToString(), Guid.Empty.ToString(), 0xfa0, exception.Message, exception.StackTrace });
         this.originalStdErr.WriteLine(exception.Message);
         Environment.Exit(0xfa0);
     }
 }
Ejemplo n.º 12
0
        protected void Start(string initialCommand, PSRemotingCryptoHelperServer cryptoHelper, string configurationName = null)
        {
            _initialCommand = initialCommand;

            sessionTM = CreateSessionTransportManager(configurationName, cryptoHelper);

            try
            {
                do
                {
                    string data = originalStdIn.ReadLine();
                    lock (_syncObject)
                    {
                        if (sessionTM == null)
                        {
                            sessionTM = CreateSessionTransportManager(configurationName, cryptoHelper);
                        }
                    }
                    if (string.IsNullOrEmpty(data))
                    {
                        lock (_syncObject)
                        {
                            // give a chance to runspace/pipelines to close (as it looks like the client died
                            // intermittently)
                            sessionTM.Close(null);
                            sessionTM = null;
                        }
                        throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownElementReceived,
                            RemotingErrorIdStrings.IPCUnknownElementReceived, string.Empty);
                    }

                    // process data in a thread pool thread..this way Runspace, Command 
                    // data can be processed concurrently.
#if CORECLR
                    ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessingThreadStart), data);
#else
                    Utils.QueueWorkItemWithImpersonation(
                        _windowsIdentityToImpersonate,
                        new WaitCallback(ProcessingThreadStart),
                        data);
#endif
                } while (true);
            }
            catch (Exception e)
            {
                CommandProcessorBase.CheckForSevereException(e);

                PSEtwLog.LogOperationalError(
                    PSEventId.TransportError, PSOpcode.Open, PSTask.None,
                    PSKeyword.UseAlwaysOperational,
                    Guid.Empty.ToString(),
                    Guid.Empty.ToString(),
                    OutOfProcessUtils.EXITCODE_UNHANDLED_EXCEPTION,
                    e.Message,
                    e.StackTrace);

                PSEtwLog.LogAnalyticError(
                    PSEventId.TransportError_Analytic, PSOpcode.Open, PSTask.None,
                    PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic,
                    Guid.Empty.ToString(),
                    Guid.Empty.ToString(),
                    OutOfProcessUtils.EXITCODE_UNHANDLED_EXCEPTION,
                    e.Message,
                    e.StackTrace);

                if (_exitProcessOnError)
                {
                    // notify the remote client of any errors and fail gracefully
                    originalStdErr.WriteLine(e.Message);

                    Environment.Exit(OutOfProcessUtils.EXITCODE_UNHANDLED_EXCEPTION);
                }
            }
        }
Ejemplo n.º 13
0
        protected OutOfProcessServerSessionTransportManager CreateSessionTransportManager(string configurationName, PSRemotingCryptoHelperServer cryptoHelper)
        {
            PSSenderInfo senderInfo;
#if !UNIX
            WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
            PSPrincipal userPrincipal = new PSPrincipal(new PSIdentity("", true, currentIdentity.Name, null),
                currentIdentity);
            senderInfo = new PSSenderInfo(userPrincipal, "http://localhost");
#else
            PSPrincipal userPrincipal = new PSPrincipal(new PSIdentity("", true, "", null),
                null);
            senderInfo = new PSSenderInfo(userPrincipal, "http://localhost");
#endif

            OutOfProcessServerSessionTransportManager tm = new OutOfProcessServerSessionTransportManager(originalStdOut, originalStdErr, cryptoHelper);

            ServerRemoteSession srvrRemoteSession = ServerRemoteSession.CreateServerRemoteSession(senderInfo,
                _initialCommand, tm, configurationName);

            return tm;
        }
Ejemplo n.º 14
0
        protected void OnClosePacketReceived(Guid psGuid)
        {
            PowerShellTraceSource tracer = PowerShellTraceSourceFactory.GetTraceSource();

            if (psGuid == Guid.Empty)
            {
                tracer.WriteMessage("BEGIN calling close on session transport manager");

                bool waitForAllcmdsClosedEvent = false;

                lock (_syncObject)
                {
                    if (_inProgressCommandsCount > 0)
                        waitForAllcmdsClosedEvent = true;
                }

                // Wait outside sync lock if required for all cmds to be closed
                //
                if (waitForAllcmdsClosedEvent)
                    allcmdsClosedEvent.WaitOne();

                lock (_syncObject)
                {
                    tracer.WriteMessage("OnClosePacketReceived, in progress commands count should be zero : " + _inProgressCommandsCount + ", psGuid : " + psGuid.ToString());

                    if (sessionTM != null)
                    {
                        // it appears that when closing PowerShell ISE, therefore closing OutOfProcServerMediator, there are 2 Close command requests
                        // changing PSRP/IPC at this point is too risky, therefore protecting about this duplication
                        sessionTM.Close(null);
                    }
                    tracer.WriteMessage("END calling close on session transport manager");
                    sessionTM = null;
                }
            }
            else
            {
                tracer.WriteMessage("Closing command with GUID " + psGuid.ToString());

                // this is for a command
                AbstractServerTransportManager cmdTM = null;

                lock (_syncObject)
                {
                    cmdTM = sessionTM.GetCommandTransportManager(psGuid);
                }

                // dont throw if there is no cmdTM as it might have legitimately closed
                if (null != cmdTM)
                {
                    cmdTM.Close(null);
                }

                lock (_syncObject)
                {
                    tracer.WriteMessage("OnClosePacketReceived, in progress commands count should be greater than zero : " + _inProgressCommandsCount + ", psGuid : " + psGuid.ToString());

                    _inProgressCommandsCount--;

                    if (_inProgressCommandsCount == 0)
                        allcmdsClosedEvent.Set();
                }
            }

            // send close ack
            originalStdOut.WriteLine(OutOfProcessUtils.CreateCloseAckPacket(psGuid));
        }