Exemplo n.º 1
0
        /// <summary>
        /// Creates a new instance of ClientRemoteSessionImpl
        /// </summary>
        /// <param name="rsPool">
        /// The RunspacePool object this session should map to.
        /// </param>
        /// <param name="uriRedirectionHandler">
        /// </param>
        internal ClientRemoteSessionImpl(RemoteRunspacePoolInternal rsPool,
                                         URIDirectionReported uriRedirectionHandler)
        {
            Dbg.Assert(rsPool != null, "RunspacePool cannot be null");
            base.RemoteRunspacePoolInternal = rsPool;
            Context.RemoteAddress           = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo <Uri>(rsPool.ConnectionInfo,
                                                                                                             "ConnectionUri", null);
            _cryptoHelper            = new PSRemotingCryptoHelperClient();
            _cryptoHelper.Session    = this;
            Context.ClientCapability = RemoteSessionCapability.CreateClientCapability();
            Context.UserCredential   = rsPool.ConnectionInfo.Credential;

            // shellName validation is not performed on the client side.
            // This is recommended by the WinRS team: for the reason that the rules may change in the future.
            Context.ShellName = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo <string>(rsPool.ConnectionInfo,
                                                                                                  "ShellUri", string.Empty);

            MySelf = RemotingDestination.Client;
            //Create session data structure handler for this session
            SessionDataStructureHandler = new ClientRemoteSessionDSHandlerImpl(this,
                                                                               _cryptoHelper,
                                                                               rsPool.ConnectionInfo,
                                                                               uriRedirectionHandler);
            BaseSessionDataStructureHandler     = SessionDataStructureHandler;
            _waitHandleForConfigurationReceived = new ManualResetEvent(false);

            //Register handlers for various ClientSessiondata structure handler events
            SessionDataStructureHandler.NegotiationReceived         += HandleNegotiationReceived;
            SessionDataStructureHandler.ConnectionStateChanged      += HandleConnectionStateChanged;
            SessionDataStructureHandler.EncryptedSessionKeyReceived +=
                new EventHandler <RemoteDataEventArgs <string> >(HandleEncryptedSessionKeyReceived);
            SessionDataStructureHandler.PublicKeyRequestReceived +=
                new EventHandler <RemoteDataEventArgs <string> >(HandlePublicKeyRequestReceived);
        }
        internal override void RaiseDataReceivedEvent(RemoteDataEventArgs dataArg)
        {
            if (dataArg == null)
            {
                throw PSTraceSource.NewArgumentNullException("dataArg");
            }
            RemoteDataObject <PSObject> receivedData    = dataArg.ReceivedData;
            RemotingTargetInterface     targetInterface = receivedData.TargetInterface;
            RemotingDataType            dataType        = receivedData.DataType;

            switch (dataType)
            {
            case RemotingDataType.SessionCapability:
            {
                RemoteSessionCapability remoteSessionCapability = null;
                try
                {
                    remoteSessionCapability = RemotingDecoder.GetSessionCapability(receivedData.Data);
                }
                catch (PSRemotingDataStructureException exception2)
                {
                    throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNotFoundCapabilityProperties, new object[] { exception2.Message, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
                }
                RemoteSessionStateMachineEventArgs fsmEventArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationReceived)
                {
                    RemoteSessionCapability = remoteSessionCapability
                };
                this._stateMachine.RaiseEvent(fsmEventArg);
                if (this.NegotiationReceived != null)
                {
                    RemoteSessionNegotiationEventArgs eventArgs = new RemoteSessionNegotiationEventArgs(remoteSessionCapability)
                    {
                        RemoteData = receivedData
                    };
                    this.NegotiationReceived.SafeInvoke <RemoteSessionNegotiationEventArgs>(this, eventArgs);
                }
                return;
            }

            case RemotingDataType.CloseSession:
            {
                PSRemotingDataStructureException   reason = new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientRequestedToCloseSession);
                RemoteSessionStateMachineEventArgs args   = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.Close, reason);
                this._stateMachine.RaiseEvent(args);
                return;
            }

            case RemotingDataType.CreateRunspacePool:
                this.CreateRunspacePoolReceived.SafeInvoke <RemoteDataEventArgs>(this, dataArg);
                return;

            case RemotingDataType.PublicKey:
            {
                string publicKey = RemotingDecoder.GetPublicKey(receivedData.Data);
                this.PublicKeyReceived.SafeInvoke <RemoteDataEventArgs <string> >(this, new RemoteDataEventArgs <string>(publicKey));
                return;
            }
            }
            throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ReceivedUnsupportedAction, new object[] { dataType });
        }
Exemplo n.º 3
0
        private bool RunClientNegotiationAlgorithm(RemoteSessionCapability serverRemoteSessionCapability)
        {
            Version protocolVersion = serverRemoteSessionCapability.ProtocolVersion;

            base._serverProtocolVersion = protocolVersion;
            Version version2 = base.Context.ClientCapability.ProtocolVersion;

            if ((!version2.Equals(protocolVersion) && ((version2 != RemotingConstants.ProtocolVersionWin7RTM) || (protocolVersion != RemotingConstants.ProtocolVersionWin7RC))) && ((version2 != RemotingConstants.ProtocolVersionCurrent) || ((protocolVersion != RemotingConstants.ProtocolVersionWin7RC) && (protocolVersion != RemotingConstants.ProtocolVersionWin7RTM))))
            {
                PSRemotingDataStructureException exception = new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed, new object[] { "protocolversion", protocolVersion, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
                throw exception;
            }
            Version pSVersion = serverRemoteSessionCapability.PSVersion;

            if (!base.Context.ClientCapability.PSVersion.Equals(pSVersion))
            {
                PSRemotingDataStructureException exception2 = new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed, new object[] { "PSVersion", pSVersion.ToString(), PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
                throw exception2;
            }
            Version serializationVersion = serverRemoteSessionCapability.SerializationVersion;

            if (!base.Context.ClientCapability.SerializationVersion.Equals(serializationVersion))
            {
                PSRemotingDataStructureException exception3 = new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed, new object[] { "SerializationVersion", serializationVersion.ToString(), PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
                throw exception3;
            }
            return(true);
        }
 internal RemoteSessionNegotiationEventArgs(System.Management.Automation.Remoting.RemoteSessionCapability remoteSessionCapability)
 {
     if (remoteSessionCapability == null)
     {
         throw PSTraceSource.NewArgumentNullException("remoteSessionCapability");
     }
     this._remoteSessionCapability = remoteSessionCapability;
 }
Exemplo n.º 5
0
 internal RemoteSessionNegotiationEventArgs(System.Management.Automation.Remoting.RemoteSessionCapability remoteSessionCapability)
 {
     if (remoteSessionCapability == null)
     {
         throw PSTraceSource.NewArgumentNullException("remoteSessionCapability");
     }
     this._remoteSessionCapability = remoteSessionCapability;
 }
Exemplo n.º 6
0
        internal RemoteSessionNegotiationEventArgs(RemoteSessionCapability remoteSessionCapability)
        {
            Dbg.Assert(remoteSessionCapability != null, "caller should validate the parameter");

            if (remoteSessionCapability == null)
            {
                throw PSTraceSource.NewArgumentNullException("remoteSessionCapability");
            }

            RemoteSessionCapability = remoteSessionCapability;
        }
Exemplo n.º 7
0
        private void ProcessSessionMessages(RemoteDataEventArgs arg)
        {
            if ((arg == null) || (arg.ReceivedData == null))
            {
                throw PSTraceSource.NewArgumentNullException("arg");
            }
            RemoteDataObject <PSObject> receivedData    = arg.ReceivedData;
            RemotingTargetInterface     targetInterface = receivedData.TargetInterface;
            RemotingDataType            dataType        = receivedData.DataType;

            switch (dataType)
            {
            case RemotingDataType.SessionCapability:
            {
                RemoteSessionCapability remoteSessionCapability = null;
                try
                {
                    remoteSessionCapability = RemotingDecoder.GetSessionCapability(receivedData.Data);
                }
                catch (PSRemotingDataStructureException exception2)
                {
                    throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNotFoundCapabilityProperties, new object[] { exception2.Message, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
                }
                RemoteSessionStateMachineEventArgs args2 = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationReceived)
                {
                    RemoteSessionCapability = remoteSessionCapability
                };
                this._stateMachine.RaiseEvent(args2, false);
                RemoteSessionNegotiationEventArgs eventArgs = new RemoteSessionNegotiationEventArgs(remoteSessionCapability);
                this.NegotiationReceived.SafeInvoke <RemoteSessionNegotiationEventArgs>(this, eventArgs);
                return;
            }

            case RemotingDataType.CloseSession:
            {
                PSRemotingDataStructureException   reason = new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerRequestedToCloseSession);
                RemoteSessionStateMachineEventArgs args   = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.Close, reason);
                this._stateMachine.RaiseEvent(args, false);
                return;
            }

            case RemotingDataType.EncryptedSessionKey:
            {
                string encryptedSessionKey = RemotingDecoder.GetEncryptedSessionKey(receivedData.Data);
                this.EncryptedSessionKeyReceived.SafeInvoke <RemoteDataEventArgs <string> >(this, new RemoteDataEventArgs <string>(encryptedSessionKey));
                return;
            }

            case RemotingDataType.PublicKeyRequest:
                this.PublicKeyRequestReceived.SafeInvoke <RemoteDataEventArgs <string> >(this, new RemoteDataEventArgs <string>(string.Empty));
                return;
            }
            throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ReceivedUnsupportedAction, new object[] { dataType });
        }
Exemplo n.º 8
0
        /// <summary>
        /// Clubing negotiation packet + runspace creation and then doing transportManager.ConnectAsync().
        /// This will save us 2 network calls by doing all the work in one network call.
        /// </summary>
        private void HandleNegotiationSendingStateChange()
        {
            RemoteSessionCapability clientCapability = _session.Context.ClientCapability;

            Dbg.Assert(clientCapability.RemotingDestination == RemotingDestination.Server, "Expected clientCapability.RemotingDestination == RemotingDestination.Server");

            //Encode and send the negotiation reply
            RemoteDataObject data = RemotingEncoder.GenerateClientSessionCapability(
                clientCapability, _session.RemoteRunspacePoolInternal.InstanceId);
            RemoteDataObject <PSObject> dataAsPSObject = RemoteDataObject <PSObject> .CreateFrom(
                data.Destination, data.DataType, data.RunspacePoolId, data.PowerShellId, (PSObject)data.Data);

            _transportManager.DataToBeSentCollection.Add <PSObject>(dataAsPSObject);
        }
Exemplo n.º 9
0
        private bool RunServerNegotiationAlgorithm(RemoteSessionCapability clientCapability, bool onConnect)
        {
            Version protocolVersion = clientCapability.ProtocolVersion;
            Version version2        = this._context.ServerCapability.ProtocolVersion;

            if (onConnect)
            {
                if (protocolVersion != version2)
                {
                    PSRemotingDataStructureException exception = new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnNegotiation, new object[] { "protocolversion", protocolVersion, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
                    throw exception;
                }
            }
            else
            {
                if ((protocolVersion == RemotingConstants.ProtocolVersionWin7RTM) && (version2 == RemotingConstants.ProtocolVersionCurrent))
                {
                    version2 = RemotingConstants.ProtocolVersionWin7RTM;
                    this._context.ServerCapability.ProtocolVersion = version2;
                }
                if ((protocolVersion == RemotingConstants.ProtocolVersionWin7RC) && ((version2 == RemotingConstants.ProtocolVersionWin7RTM) || (version2 == RemotingConstants.ProtocolVersionCurrent)))
                {
                    version2 = RemotingConstants.ProtocolVersionWin7RC;
                    this._context.ServerCapability.ProtocolVersion = version2;
                }
                if ((protocolVersion.Major != version2.Major) || (protocolVersion.Minor < version2.Minor))
                {
                    PSRemotingDataStructureException exception2 = new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNegotiationFailed, new object[] { "protocolversion", protocolVersion, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
                    throw exception2;
                }
            }
            Version pSVersion = clientCapability.PSVersion;
            Version version4  = this._context.ServerCapability.PSVersion;

            if ((pSVersion.Major != version4.Major) || (pSVersion.Minor < version4.Minor))
            {
                PSRemotingDataStructureException exception3 = new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNegotiationFailed, new object[] { "PSVersion", pSVersion, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
                throw exception3;
            }
            Version serializationVersion = clientCapability.SerializationVersion;
            Version version6             = this._context.ServerCapability.SerializationVersion;

            if ((serializationVersion.Major == version6.Major) && (serializationVersion.Minor >= version6.Minor))
            {
                return(true);
            }
            PSRemotingDataStructureException exception4 = new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNegotiationFailed, new object[] { "SerializationVersion", serializationVersion, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });

            throw exception4;
        }
Exemplo n.º 10
0
        private bool RunServerNegotiationAlgorithm(RemoteSessionCapability clientCapability)
        {
            Version protocolVersion = clientCapability.ProtocolVersion;
            Version version         = this._context.ServerCapability.ProtocolVersion;

            if (protocolVersion == RemotingConstants.ProtocolVersionWin7RC && version == RemotingConstants.ProtocolVersionWin7RTM)
            {
                version = RemotingConstants.ProtocolVersionWin7RC;
                this._context.ServerCapability.ProtocolVersion = version;
            }
            if (protocolVersion.Major != version.Major || protocolVersion.Minor < version.Minor)
            {
                throw new PSRemotingDataStructureException(PSRemotingErrorId.ServerNegotiationFailed, new object[4]
                {
                    (object)"protocolversion",
                    (object)protocolVersion,
                    (object)PSVersionInfo.BuildVersion,
                    (object)RemotingConstants.ProtocolVersion
                });
            }
            Version psVersion1 = clientCapability.PSVersion;
            Version psVersion2 = this._context.ServerCapability.PSVersion;

            if (psVersion1.Major != psVersion2.Major || psVersion1.Minor < psVersion2.Minor)
            {
                throw new PSRemotingDataStructureException(PSRemotingErrorId.ServerNegotiationFailed, new object[4]
                {
                    (object)"PSVersion",
                    (object)psVersion1,
                    (object)PSVersionInfo.BuildVersion,
                    (object)RemotingConstants.ProtocolVersion
                });
            }
            Version serializationVersion1 = clientCapability.SerializationVersion;
            Version serializationVersion2 = this._context.ServerCapability.SerializationVersion;

            if (serializationVersion1.Major != serializationVersion2.Major || serializationVersion1.Minor < serializationVersion2.Minor)
            {
                throw new PSRemotingDataStructureException(PSRemotingErrorId.ServerNegotiationFailed, new object[4]
                {
                    (object)"SerializationVersion",
                    (object)serializationVersion1,
                    (object)PSVersionInfo.BuildVersion,
                    (object)RemotingConstants.ProtocolVersion
                });
            }
            return(true);
        }
Exemplo n.º 11
0
 internal ClientRemoteSessionImpl(RemoteRunspacePoolInternal rsPool, ClientRemoteSession.URIDirectionReported uriRedirectionHandler)
 {
     base.RemoteRunspacePoolInternal = rsPool;
     base.Context.RemoteAddress      = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo <Uri>(rsPool.ConnectionInfo, "ConnectionUri", null);
     this._cryptoHelper            = new PSRemotingCryptoHelperClient();
     this._cryptoHelper.Session    = this;
     base.Context.ClientCapability = RemoteSessionCapability.CreateClientCapability();
     base.Context.UserCredential   = rsPool.ConnectionInfo.Credential;
     base.Context.ShellName        = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo <string>(rsPool.ConnectionInfo, "ShellUri", string.Empty);
     this._mySelf = RemotingDestination.InvalidDestination | RemotingDestination.Client;
     base.SessionDataStructureHandler                              = new ClientRemoteSessionDSHandlerImpl(this, this._cryptoHelper, rsPool.ConnectionInfo, uriRedirectionHandler);
     base.BaseSessionDataStructureHandler                          = base.SessionDataStructureHandler;
     this._waitHandleForConfigurationReceived                      = new ManualResetEvent(false);
     base.SessionDataStructureHandler.NegotiationReceived         += new EventHandler <RemoteSessionNegotiationEventArgs>(this.HandleNegotiationReceived);
     base.SessionDataStructureHandler.ConnectionStateChanged      += new EventHandler <RemoteSessionStateEventArgs>(this.HandleConnectionStateChanged);
     base.SessionDataStructureHandler.EncryptedSessionKeyReceived += new EventHandler <RemoteDataEventArgs <string> >(this.HandleEncryptedSessionKeyReceived);
     base.SessionDataStructureHandler.PublicKeyRequestReceived    += new EventHandler <RemoteDataEventArgs <string> >(this.HandlePublicKeyRequestReceived);
 }
Exemplo n.º 12
0
        /// <summary>
        /// This method sends the server side capability negotiation packet to the client.
        /// </summary>
        internal override void SendNegotiationAsync()
        {
            RemoteSessionCapability serverCapability = _session.Context.ServerCapability;
            RemoteDataObject        data             = RemotingEncoder.GenerateServerSessionCapability(serverCapability,
                                                                                                       Guid.Empty);

            RemoteSessionStateMachineEventArgs negotiationSendCompletedArg =
                new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationSendCompleted);

            _stateMachine.RaiseEvent(negotiationSendCompletedArg);

            RemoteDataObject <PSObject> dataToBeSent = RemoteDataObject <PSObject> .CreateFrom(
                data.Destination, data.DataType, data.RunspacePoolId, data.PowerShellId, (PSObject)data.Data);

            // send data to client..flush is not true as we expect to send state changed
            // information (from runspace creation)
            _transportManager.SendDataToClient <PSObject>(dataToBeSent, false);
        }
Exemplo n.º 13
0
 internal ServerRunspacePoolDriver(Guid clientRunspacePoolId, int minRunspaces, int maxRunspaces, PSThreadOptions threadOptions, ApartmentState apartmentState, HostInfo hostInfo, InitialSessionState initialSessionState, PSPrimitiveDictionary applicationPrivateData, ConfigurationDataFromXML configData, AbstractServerSessionTransportManager transportManager, bool isAdministrator, RemoteSessionCapability serverCapability, Hashtable configHash)
 {
     this.serverCapability = serverCapability;
     System.Management.Automation.Remoting.ServerRemoteHost host = new System.Management.Automation.Remoting.ServerRemoteHost(clientRunspacePoolId, Guid.Empty, hostInfo, transportManager);
     this.remoteHost = host;
     this.configData = configData;
     this.configHash = configHash;
     this.applicationPrivateData = applicationPrivateData;
     this.localRunspacePool = RunspaceFactory.CreateRunspacePool(minRunspaces, maxRunspaces, initialSessionState, host);
     PSThreadOptions options = configData.ShellThreadOptions.HasValue ? configData.ShellThreadOptions.Value : PSThreadOptions.UseCurrentThread;
     if ((threadOptions == PSThreadOptions.Default) || (threadOptions == options))
     {
         this.localRunspacePool.ThreadOptions = options;
     }
     else
     {
         if (!isAdministrator)
         {
             throw new InvalidOperationException(PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.MustBeAdminToOverrideThreadOptions, new object[0]));
         }
         this.localRunspacePool.ThreadOptions = threadOptions;
     }
     ApartmentState state = configData.ShellThreadApartmentState.HasValue ? configData.ShellThreadApartmentState.Value : ApartmentState.Unknown;
     if ((apartmentState == ApartmentState.Unknown) || (apartmentState == state))
     {
         this.localRunspacePool.ApartmentState = state;
     }
     else
     {
         this.localRunspacePool.ApartmentState = apartmentState;
     }
     this.clientRunspacePoolId = clientRunspacePoolId;
     this.dsHandler = new ServerRunspacePoolDataStructureHandler(this, transportManager);
     this.localRunspacePool.StateChanged += new EventHandler<RunspacePoolStateChangedEventArgs>(this.HandleRunspacePoolStateChanged);
     this.localRunspacePool.ForwardEvent += new EventHandler<PSEventArgs>(this.HandleRunspacePoolForwardEvent);
     this.localRunspacePool.RunspaceCreated += new EventHandler<RunspaceCreatedEventArgs>(this.HandleRunspaceCreated);
     this.localRunspacePool.RunspaceCreated += new EventHandler<RunspaceCreatedEventArgs>(this.HandleRunspaceCreatedForTypeTable);
     this.dsHandler.CreateAndInvokePowerShell += new EventHandler<RemoteDataEventArgs<RemoteDataObject<PSObject>>>(this.HandleCreateAndInvokePowerShell);
     this.dsHandler.GetCommandMetadata += new EventHandler<RemoteDataEventArgs<RemoteDataObject<PSObject>>>(this.HandleGetCommandMetadata);
     this.dsHandler.HostResponseReceived += new EventHandler<RemoteDataEventArgs<RemoteHostResponse>>(this.HandleHostResponseReceived);
     this.dsHandler.SetMaxRunspacesReceived += new EventHandler<RemoteDataEventArgs<PSObject>>(this.HandleSetMaxRunspacesReceived);
     this.dsHandler.SetMinRunspacesReceived += new EventHandler<RemoteDataEventArgs<PSObject>>(this.HandleSetMinRunspacesReceived);
     this.dsHandler.GetAvailableRunspacesReceived += new EventHandler<RemoteDataEventArgs<PSObject>>(this.HandleGetAvailalbeRunspacesReceived);
 }
Exemplo n.º 14
0
        /// <summary>
        /// Send a message with application private data to the client
        /// </summary>
        /// <param name="applicationPrivateData">applicationPrivateData to send</param>
        /// <param name="serverCapability">server capability negotiated during initial exchange of remoting messages / session capabilities of client and server</param>
        internal void SendApplicationPrivateDataToClient(PSPrimitiveDictionary applicationPrivateData, RemoteSessionCapability serverCapability)
        {
            // make server's PSVersionTable available to the client using ApplicationPrivateData
            PSPrimitiveDictionary applicationPrivateDataWithVersionTable =
                PSPrimitiveDictionary.CloneAndAddPSVersionTable(applicationPrivateData);

            // override the hardcoded version numbers with the stuff that was reported to the client during negotiation
            PSPrimitiveDictionary versionTable = (PSPrimitiveDictionary)applicationPrivateDataWithVersionTable[PSVersionInfo.PSVersionTableName];
            versionTable[PSVersionInfo.PSRemotingProtocolVersionName] = serverCapability.ProtocolVersion;
            versionTable[PSVersionInfo.SerializationVersionName] = serverCapability.SerializationVersion;

            // Pass back the true PowerShell version to the client via application private data.
            versionTable[PSVersionInfo.PSVersionName] = PSVersionInfo.PSVersion;

            RemoteDataObject data = RemotingEncoder.GenerateApplicationPrivateData(
                _clientRunspacePoolId, applicationPrivateDataWithVersionTable);

            SendDataAsync(data);
        }
Exemplo n.º 15
0
 private bool RunClientNegotiationAlgorithm(
     RemoteSessionCapability serverRemoteSessionCapability)
 {
     using (ClientRemoteSessionImpl._trace.TraceMethod())
     {
         Version protocolVersion1 = serverRemoteSessionCapability.ProtocolVersion;
         Version protocolVersion2 = this.Context.ClientCapability.ProtocolVersion;
         if (!protocolVersion2.Equals(protocolVersion1) && (!(protocolVersion2 == RemotingConstants.ProtocolVersionWin7RTM) || !(protocolVersion1 == RemotingConstants.ProtocolVersionWin7RC)))
         {
             throw new PSRemotingDataStructureException(PSRemotingErrorId.ClientNegotiationFailed, new object[4]
             {
                 (object)"protocolversion",
                 (object)protocolVersion1,
                 (object)PSVersionInfo.BuildVersion,
                 (object)RemotingConstants.ProtocolVersion
             });
         }
         Version psVersion = serverRemoteSessionCapability.PSVersion;
         if (!this.Context.ClientCapability.PSVersion.Equals(psVersion))
         {
             throw new PSRemotingDataStructureException(PSRemotingErrorId.ClientNegotiationFailed, new object[4]
             {
                 (object)"PSVersion",
                 (object)psVersion.ToString(),
                 (object)PSVersionInfo.BuildVersion,
                 (object)RemotingConstants.ProtocolVersion
             });
         }
         Version serializationVersion = serverRemoteSessionCapability.SerializationVersion;
         if (!this.Context.ClientCapability.SerializationVersion.Equals(serializationVersion))
         {
             throw new PSRemotingDataStructureException(PSRemotingErrorId.ClientNegotiationFailed, new object[4]
             {
                 (object)"SerializationVersion",
                 (object)serializationVersion.ToString(),
                 (object)PSVersionInfo.BuildVersion,
                 (object)RemotingConstants.ProtocolVersion
             });
         }
         return(true);
     }
 }
Exemplo n.º 16
0
        /// <summary>
        /// This method is used by the input queue dispatching mechanism.
        /// It examines the data and takes appropriate actions.
        /// </summary>
        /// <param name="dataArg">
        /// The received client data.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// If the parameter is null.
        /// </exception>
        internal override void RaiseDataReceivedEvent(RemoteDataEventArgs dataArg)
        {
            if (dataArg == null)
            {
                throw PSTraceSource.NewArgumentNullException("dataArg");
            }

            RemoteDataObject <PSObject> rcvdData = dataArg.ReceivedData;

            RemotingTargetInterface targetInterface = rcvdData.TargetInterface;
            RemotingDataType        dataType        = rcvdData.DataType;

            Dbg.Assert(targetInterface == RemotingTargetInterface.Session, "targetInterface must be Session");

            switch (dataType)
            {
            case RemotingDataType.CreateRunspacePool:
            {
                // At this point, the negotiation is complete, so
                // need to import the clients public key
                CreateRunspacePoolReceived.SafeInvoke(this, dataArg);
            }

            break;

            case RemotingDataType.CloseSession:
                PSRemotingDataStructureException   reasonOfClose   = new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientRequestedToCloseSession);
                RemoteSessionStateMachineEventArgs closeSessionArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.Close, reasonOfClose);
                _stateMachine.RaiseEvent(closeSessionArg);
                break;

            case RemotingDataType.SessionCapability:
                RemoteSessionCapability capability = null;
                try
                {
                    capability = RemotingDecoder.GetSessionCapability(rcvdData.Data);
                }
                catch (PSRemotingDataStructureException dse)
                {
                    // this will happen if expected properties are not
                    // received for session capability
                    throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNotFoundCapabilityProperties,
                                                               dse.Message, PSVersionInfo.GitCommitId, RemotingConstants.ProtocolVersion);
                }

                RemoteSessionStateMachineEventArgs capabilityArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationReceived);
                capabilityArg.RemoteSessionCapability = capability;
                _stateMachine.RaiseEvent(capabilityArg);

                if (NegotiationReceived != null)
                {
                    RemoteSessionNegotiationEventArgs negotiationArg = new RemoteSessionNegotiationEventArgs(capability);
                    negotiationArg.RemoteData = rcvdData;
                    NegotiationReceived.SafeInvoke(this, negotiationArg);
                }

                break;

            case RemotingDataType.PublicKey:
            {
                string remotePublicKey = RemotingDecoder.GetPublicKey(rcvdData.Data);
                PublicKeyReceived.SafeInvoke(this, new RemoteDataEventArgs <string>(remotePublicKey));
            }

            break;

            default:
                throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ReceivedUnsupportedAction, dataType);
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// Creates the runspace pool driver
        /// </summary>
        /// <param name="clientRunspacePoolId">client runspace pool id to associate</param>
        /// <param name="transportManager">transport manager associated with this
        /// runspace pool driver</param>
        /// <param name="maxRunspaces">maximum runspaces to open</param>
        /// <param name="minRunspaces">minimum runspaces to open</param>
        /// <param name="threadOptions">threading options for the runspaces in the pool</param>
        /// <param name="apartmentState">apartment state for the runspaces in the pool</param>
        /// <param name="hostInfo">host information about client side host</param>
        /// <param name="configData">
        /// Contains:
        /// 1. Script to run after a RunspacePool/Runspace is created in this session.
        /// For RunspacePool case, every newly created Runspace (in the pool) will run
        /// this script.
        /// 2. ThreadOptions for RunspacePool/Runspace
        /// 3. ThreadApartment for RunspacePool/Runspace
        /// </param>
        /// <param name="initialSessionState">configuration of the runspace</param>
        /// <param name="applicationPrivateData">application private data</param>
        /// <param name="isAdministrator">True if the driver is being created by an administrator</param>
        /// <param name="serverCapability">server capability reported to the client during negotiation (not the actual capability)</param>
        /// <param name="psClientVersion">Client PowerShell version.</param>
        /// <param name="configurationName">Optional endpoint configuration name to create a pushed configured runspace.</param>
        internal ServerRunspacePoolDriver(
            Guid clientRunspacePoolId,
            int minRunspaces,
            int maxRunspaces,
            PSThreadOptions threadOptions,
            ApartmentState apartmentState,
            HostInfo hostInfo,
            InitialSessionState initialSessionState,
            PSPrimitiveDictionary applicationPrivateData,
            ConfigurationDataFromXML configData,
            AbstractServerSessionTransportManager transportManager,
            bool isAdministrator,
            RemoteSessionCapability serverCapability,
            Version psClientVersion,
            string configurationName)
#endif
        {
            Dbg.Assert(null != configData, "ConfigurationData cannot be null");

            _serverCapability = serverCapability;
            _clientPSVersion = psClientVersion;

            _configurationName = configurationName;

            // Create a new server host and associate for host call
            // integration
            _remoteHost = new ServerDriverRemoteHost(clientRunspacePoolId,
                Guid.Empty, hostInfo, transportManager, null);

            _configData = configData;
            _applicationPrivateData = applicationPrivateData;
            RunspacePool = RunspaceFactory.CreateRunspacePool(
                  minRunspaces, maxRunspaces, initialSessionState, _remoteHost);

            // Set ThreadOptions for this RunspacePool
            // The default server settings is to make new commands execute in the calling thread...this saves
            // thread switching time and thread pool pressure on the service.
            // Users can override the server settings only if they are administrators
            PSThreadOptions serverThreadOptions = configData.ShellThreadOptions.HasValue ? configData.ShellThreadOptions.Value : PSThreadOptions.UseCurrentThread;
            if (threadOptions == PSThreadOptions.Default || threadOptions == serverThreadOptions)
            {
                RunspacePool.ThreadOptions = serverThreadOptions;
            }
            else
            {
                if (!isAdministrator)
                {
                    throw new InvalidOperationException(PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.MustBeAdminToOverrideThreadOptions));
                }

                RunspacePool.ThreadOptions = threadOptions;
            }

#if !CORECLR // No ApartmentState In CoreCLR
            // Set Thread ApartmentState for this RunspacePool
            ApartmentState serverApartmentState = configData.ShellThreadApartmentState.HasValue ? configData.ShellThreadApartmentState.Value : Runspace.DefaultApartmentState;

            if (apartmentState == ApartmentState.Unknown || apartmentState == serverApartmentState)
            {
                RunspacePool.ApartmentState = serverApartmentState;
            }
            else
            {
                RunspacePool.ApartmentState = apartmentState;
            }
#endif

            // If we have a runspace pool with a single runspace then we can run nested pipelines on
            // on it in a single pipeline invoke thread.
            if (maxRunspaces == 1 &&
                (RunspacePool.ThreadOptions == PSThreadOptions.Default ||
                 RunspacePool.ThreadOptions == PSThreadOptions.UseCurrentThread))
            {
                _driverNestedInvoker = new PowerShellDriverInvoker();
            }

            InstanceId = clientRunspacePoolId;
            DataStructureHandler = new ServerRunspacePoolDataStructureHandler(this, transportManager);

            // handle the StateChanged event of the runspace pool
            RunspacePool.StateChanged +=
                new EventHandler<RunspacePoolStateChangedEventArgs>(HandleRunspacePoolStateChanged);

            // listen for events on the runspace pool
            RunspacePool.ForwardEvent +=
                new EventHandler<PSEventArgs>(HandleRunspacePoolForwardEvent);

            RunspacePool.RunspaceCreated += HandleRunspaceCreated;

            // register for all the events from the data structure handler
            DataStructureHandler.CreateAndInvokePowerShell +=
                new EventHandler<RemoteDataEventArgs<RemoteDataObject<PSObject>>>(HandleCreateAndInvokePowerShell);
            DataStructureHandler.GetCommandMetadata +=
                new EventHandler<RemoteDataEventArgs<RemoteDataObject<PSObject>>>(HandleGetCommandMetadata);
            DataStructureHandler.HostResponseReceived +=
                new EventHandler<RemoteDataEventArgs<RemoteHostResponse>>(HandleHostResponseReceived);
            DataStructureHandler.SetMaxRunspacesReceived +=
                new EventHandler<RemoteDataEventArgs<PSObject>>(HandleSetMaxRunspacesReceived);
            DataStructureHandler.SetMinRunspacesReceived +=
                new EventHandler<RemoteDataEventArgs<PSObject>>(HandleSetMinRunspacesReceived);
            DataStructureHandler.GetAvailableRunspacesReceived +=
                new EventHandler<RemoteDataEventArgs<PSObject>>(HandleGetAvailableRunspacesReceived);
            DataStructureHandler.ResetRunspaceState +=
                new EventHandler<RemoteDataEventArgs<PSObject>>(HandleResetRunspaceState);
        }
Exemplo n.º 18
0
 /// <summary>
 /// Creates the runspace pool driver
 /// </summary>
 /// <param name="clientRunspacePoolId">client runspace pool id to associate</param>
 /// <param name="transportManager">transport manager associated with this
 /// runspace pool driver</param>
 /// <param name="maxRunspaces">maximum runspaces to open</param>
 /// <param name="minRunspaces">minimum runspaces to open</param>
 /// <param name="threadOptions">threading options for the runspaces in the pool</param>        
 /// <param name="hostInfo">host information about client side host</param>
 /// <param name="configData">
 /// Contains:
 /// 1. Script to run after a RunspacePool/Runspace is created in this session.
 /// For RunspacePool case, every newly created Runspace (in the pool) will run
 /// this script.
 /// 2. ThreadOptions for RunspacePool/Runspace
 /// 3. ThreadApartment for RunspacePool/Runspace
 /// </param>
 /// <param name="initialSessionState">configuration of the runspace</param>
 /// <param name="applicationPrivateData">application private data</param>
 /// <param name="isAdministrator">True if the driver is being created by an administrator</param>
 /// <param name="serverCapability">server capability reported to the client during negotiation (not the actual capability)</param>
 /// <param name="psClientVersion">Client PowerShell version.</param>
 /// <param name="configurationName">Optional endpoint configuration name to create a pushed configured runspace.</param>
 internal ServerRunspacePoolDriver(
     Guid clientRunspacePoolId,
     int minRunspaces,
     int maxRunspaces,
     PSThreadOptions threadOptions,
     HostInfo hostInfo,
     InitialSessionState initialSessionState,
     PSPrimitiveDictionary applicationPrivateData,
     ConfigurationDataFromXML configData,
     AbstractServerSessionTransportManager transportManager,
     bool isAdministrator,
     RemoteSessionCapability serverCapability,
     Version psClientVersion,
     string configurationName)
Exemplo n.º 19
0
 private bool RunServerNegotiationAlgorithm(RemoteSessionCapability clientCapability, bool onConnect)
 {
     Version protocolVersion = clientCapability.ProtocolVersion;
     Version version2 = this._context.ServerCapability.ProtocolVersion;
     if (onConnect)
     {
         if (protocolVersion != version2)
         {
             PSRemotingDataStructureException exception = new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnNegotiation, new object[] { "protocolversion", protocolVersion, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
             throw exception;
         }
     }
     else
     {
         if ((protocolVersion == RemotingConstants.ProtocolVersionWin7RTM) && (version2 == RemotingConstants.ProtocolVersionCurrent))
         {
             version2 = RemotingConstants.ProtocolVersionWin7RTM;
             this._context.ServerCapability.ProtocolVersion = version2;
         }
         if ((protocolVersion == RemotingConstants.ProtocolVersionWin7RC) && ((version2 == RemotingConstants.ProtocolVersionWin7RTM) || (version2 == RemotingConstants.ProtocolVersionCurrent)))
         {
             version2 = RemotingConstants.ProtocolVersionWin7RC;
             this._context.ServerCapability.ProtocolVersion = version2;
         }
         if ((protocolVersion.Major != version2.Major) || (protocolVersion.Minor < version2.Minor))
         {
             PSRemotingDataStructureException exception2 = new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNegotiationFailed, new object[] { "protocolversion", protocolVersion, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
             throw exception2;
         }
     }
     Version pSVersion = clientCapability.PSVersion;
     Version version4 = this._context.ServerCapability.PSVersion;
     if ((pSVersion.Major != version4.Major) || (pSVersion.Minor < version4.Minor))
     {
         PSRemotingDataStructureException exception3 = new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNegotiationFailed, new object[] { "PSVersion", pSVersion, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
         throw exception3;
     }
     Version serializationVersion = clientCapability.SerializationVersion;
     Version version6 = this._context.ServerCapability.SerializationVersion;
     if ((serializationVersion.Major == version6.Major) && (serializationVersion.Minor >= version6.Minor))
     {
         return true;
     }
     PSRemotingDataStructureException exception4 = new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNegotiationFailed, new object[] { "SerializationVersion", serializationVersion, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
     throw exception4;
 }
Exemplo n.º 20
0
        // TODO: If this is not used remove this
        // internal override event EventHandler<RemoteDataEventArgs> DataReceived;

        /// <summary>
        /// This processes the object received from transport which are
        /// targeted for session
        /// </summary>
        /// <param name="arg">
        /// argument contains the data object
        /// </param>
        private void ProcessSessionMessages(RemoteDataEventArgs arg)
        {
            if (arg == null || arg.ReceivedData == null)
            {
                throw PSTraceSource.NewArgumentNullException("arg");
            }

            RemoteDataObject <PSObject> rcvdData = arg.ReceivedData;

            RemotingTargetInterface targetInterface = rcvdData.TargetInterface;

            Dbg.Assert(targetInterface == RemotingTargetInterface.Session, "targetInterface must be Session");

            RemotingDataType dataType = rcvdData.DataType;

            switch (dataType)
            {
            case RemotingDataType.CloseSession:
                PSRemotingDataStructureException   reasonOfClose   = new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerRequestedToCloseSession);
                RemoteSessionStateMachineEventArgs closeSessionArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.Close, reasonOfClose);
                _stateMachine.RaiseEvent(closeSessionArg);
                break;

            case RemotingDataType.SessionCapability:
                RemoteSessionCapability capability = null;
                try
                {
                    capability = RemotingDecoder.GetSessionCapability(rcvdData.Data);
                }
                catch (PSRemotingDataStructureException dse)
                {
                    // this will happen if expected properties are not
                    // received for session capability
                    throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNotFoundCapabilityProperties,
                                                               dse.Message, PSVersionInfo.GitCommitId, RemotingConstants.ProtocolVersion);
                }

                RemoteSessionStateMachineEventArgs capabilityArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationReceived);
                capabilityArg.RemoteSessionCapability = capability;
                _stateMachine.RaiseEvent(capabilityArg);

                RemoteSessionNegotiationEventArgs negotiationArg = new RemoteSessionNegotiationEventArgs(capability);
                NegotiationReceived.SafeInvoke(this, negotiationArg);
                break;

            case RemotingDataType.EncryptedSessionKey:
            {
                String encryptedSessionKey = RemotingDecoder.GetEncryptedSessionKey(rcvdData.Data);
                EncryptedSessionKeyReceived.SafeInvoke(this, new RemoteDataEventArgs <string>(encryptedSessionKey));
            }
            break;

            case RemotingDataType.PublicKeyRequest:
            {
                PublicKeyRequestReceived.SafeInvoke(this, new RemoteDataEventArgs <string>(String.Empty));
            }
            break;

            default:
            {
                throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ReceivedUnsupportedAction, dataType);
            }
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// This is the server side remote session capability negotiation algorithm.
        /// </summary>
        /// <param name="clientCapability">
        /// This is the client capability that the server received from client.
        /// </param>
        /// <param name="onConnect">
        /// If the negotiation is on a connect (and not create)
        /// </param>
        /// <returns>
        /// The method returns true if the capability negotiation is successful.
        /// Otherwise, it returns false.
        /// </returns>
        /// <exception cref="PSRemotingDataStructureException">
        /// 1. PowerShell server does not support the PSVersion {1} negotiated by the client. 
        ///    Make sure the client is compatible with the build {2} of PowerShell.
        /// 2. PowerShell server does not support the SerializationVersion {1} negotiated by the client. 
        ///    Make sure the client is compatible with the build {2} of PowerShell.
        /// </exception>
        private bool RunServerNegotiationAlgorithm(RemoteSessionCapability clientCapability, bool onConnect)
        {
            Dbg.Assert(clientCapability != null, "Client capability cache must be non-null");

            Version clientProtocolVersion = clientCapability.ProtocolVersion;
            Version serverProtocolVersion = Context.ServerCapability.ProtocolVersion;

            if (onConnect)
            {
                bool connectSupported = false;

                // Win10 server can support reconstruct/reconnect for all 2.x protocol versions
                // that support reconstruct/reconnect, Protocol 2.2+
                // Major protocol version differences (2.x -> 3.x) are not supported.
                if ((serverProtocolVersion == RemotingConstants.ProtocolVersionWin10RTM) &&
                    (clientProtocolVersion.Major == serverProtocolVersion.Major))
                {
                    if (clientProtocolVersion.Minor == RemotingConstants.ProtocolVersionWin8RTM.Minor)
                    {
                        // Report that server is Win8 version to the client
                        // Protocol: 2.2
                        connectSupported = true;
                        serverProtocolVersion = RemotingConstants.ProtocolVersionWin8RTM;
                        Context.ServerCapability.ProtocolVersion = serverProtocolVersion;
                    }
                    else if (clientProtocolVersion.Minor > RemotingConstants.ProtocolVersionWin8RTM.Minor)
                    {
                        // All other minor versions are supported and the server returns its full capability
                        // Protocol: 2.3, 2.4, 2.5 ...
                        connectSupported = true;
                    }
                }

                if (!connectSupported)
                {
                    // Throw for protocol versions 2.x that don't support disconnect/reconnect.
                    // Protocol: < 2.2
                    PSRemotingDataStructureException reasonOfFailure =
                        new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnNegotiation,
                            RemoteDataNameStrings.PS_STARTUP_PROTOCOL_VERSION_NAME,
                            clientProtocolVersion,
                            PSVersionInfo.BuildVersion,
                            RemotingConstants.ProtocolVersion);
                    throw reasonOfFailure;
                }
            }
            else
            {
                // Win10 server can support Win8 client
                if (clientProtocolVersion == RemotingConstants.ProtocolVersionWin8RTM &&
                    (
                        (serverProtocolVersion == RemotingConstants.ProtocolVersionWin10RTM)
                    ))
                {
                    // - report that server is Win8 version to the client
                    serverProtocolVersion = RemotingConstants.ProtocolVersionWin8RTM;
                    Context.ServerCapability.ProtocolVersion = serverProtocolVersion;
                }

                // Win8, Win10 server can support Win7 client
                if (clientProtocolVersion == RemotingConstants.ProtocolVersionWin7RTM &&
                    (
                        (serverProtocolVersion == RemotingConstants.ProtocolVersionWin8RTM) ||
                        (serverProtocolVersion == RemotingConstants.ProtocolVersionWin10RTM)
                    ))
                {
                    // - report that server is Win7 version to the client
                    serverProtocolVersion = RemotingConstants.ProtocolVersionWin7RTM;
                    Context.ServerCapability.ProtocolVersion = serverProtocolVersion;
                }

                // Win7, Win8, Win10 server can support Win7 RC client
                if (clientProtocolVersion == RemotingConstants.ProtocolVersionWin7RC &&
                    (
                        (serverProtocolVersion == RemotingConstants.ProtocolVersionWin7RTM) ||
                        (serverProtocolVersion == RemotingConstants.ProtocolVersionWin8RTM) ||
                        (serverProtocolVersion == RemotingConstants.ProtocolVersionWin10RTM)
                    ))
                {
                    // - report that server is RC version to the client
                    serverProtocolVersion = RemotingConstants.ProtocolVersionWin7RC;
                    Context.ServerCapability.ProtocolVersion = serverProtocolVersion;
                }

                if (!((clientProtocolVersion.Major == serverProtocolVersion.Major) &&
                      (clientProtocolVersion.Minor >= serverProtocolVersion.Minor)))
                {
                    PSRemotingDataStructureException reasonOfFailure =
                        new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNegotiationFailed,
                            RemoteDataNameStrings.PS_STARTUP_PROTOCOL_VERSION_NAME,
                            clientProtocolVersion,
                            PSVersionInfo.BuildVersion,
                            RemotingConstants.ProtocolVersion);
                    throw reasonOfFailure;
                }
            }

            // PSVersion Check
            Version clientPSVersion = clientCapability.PSVersion;
            Version serverPSVersion = Context.ServerCapability.PSVersion;
            if (!((clientPSVersion.Major == serverPSVersion.Major) &&
                  (clientPSVersion.Minor >= serverPSVersion.Minor)))
            {
                PSRemotingDataStructureException reasonOfFailure =
                    new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNegotiationFailed,
                        RemoteDataNameStrings.PSVersion,
                        clientPSVersion,
                        PSVersionInfo.BuildVersion,
                        RemotingConstants.ProtocolVersion);
                throw reasonOfFailure;
            }

            // SerializationVersion check
            Version clientSerVersion = clientCapability.SerializationVersion;
            Version serverSerVersion = Context.ServerCapability.SerializationVersion;
            if (!((clientSerVersion.Major == serverSerVersion.Major) &&
                  (clientSerVersion.Minor >= serverSerVersion.Minor)))
            {
                PSRemotingDataStructureException reasonOfFailure =
                    new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNegotiationFailed,
                        RemoteDataNameStrings.SerializationVersion,
                        clientSerVersion,
                        PSVersionInfo.BuildVersion,
                        RemotingConstants.ProtocolVersion);
                throw reasonOfFailure;
            }

            return true;
        }
 internal void SendApplicationPrivateDataToClient(PSPrimitiveDictionary applicationPrivateData, RemoteSessionCapability serverCapability)
 {
     PSPrimitiveDictionary dictionary = PSPrimitiveDictionary.CloneAndAddPSVersionTable(applicationPrivateData);
     PSPrimitiveDictionary dictionary2 = (PSPrimitiveDictionary) dictionary["PSVersionTable"];
     dictionary2["PSRemotingProtocolVersion"] = serverCapability.ProtocolVersion;
     dictionary2["SerializationVersion"] = serverCapability.SerializationVersion;
     dictionary2["PSVersion"] = serverCapability.PSVersion;
     RemoteDataObject data = RemotingEncoder.GenerateApplicationPrivateData(this.clientRunspacePoolId, dictionary);
     this.SendDataAsync(data);
 }
Exemplo n.º 23
0
 private bool RunClientNegotiationAlgorithm(RemoteSessionCapability serverRemoteSessionCapability)
 {
     Version protocolVersion = serverRemoteSessionCapability.ProtocolVersion;
     base._serverProtocolVersion = protocolVersion;
     Version version2 = base.Context.ClientCapability.ProtocolVersion;
     if ((!version2.Equals(protocolVersion) && ((version2 != RemotingConstants.ProtocolVersionWin7RTM) || (protocolVersion != RemotingConstants.ProtocolVersionWin7RC))) && ((version2 != RemotingConstants.ProtocolVersionCurrent) || ((protocolVersion != RemotingConstants.ProtocolVersionWin7RC) && (protocolVersion != RemotingConstants.ProtocolVersionWin7RTM))))
     {
         PSRemotingDataStructureException exception = new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed, new object[] { "protocolversion", protocolVersion, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
         throw exception;
     }
     Version pSVersion = serverRemoteSessionCapability.PSVersion;
     if (!base.Context.ClientCapability.PSVersion.Equals(pSVersion))
     {
         PSRemotingDataStructureException exception2 = new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed, new object[] { "PSVersion", pSVersion.ToString(), PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
         throw exception2;
     }
     Version serializationVersion = serverRemoteSessionCapability.SerializationVersion;
     if (!base.Context.ClientCapability.SerializationVersion.Equals(serializationVersion))
     {
         PSRemotingDataStructureException exception3 = new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed, new object[] { "SerializationVersion", serializationVersion.ToString(), PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion });
         throw exception3;
     }
     return true;
 }
Exemplo n.º 24
0
        /// <summary>
        /// Verifies the negotiation packet received from the server
        /// </summary>
        /// <param name="serverRemoteSessionCapability">
        /// Capabilities of remote session
        /// </param>
        /// <returns>
        /// The method returns true if the capability negotiation is successful.
        /// Otherwise, it returns false.
        /// </returns>
        /// <exception cref="PSRemotingDataStructureException">
        /// 1. PowerShell client does not support the PSVersion {1} negotiated by the server.
        ///    Make sure the server is compatible with the build {2} of PowerShell.
        /// 2. PowerShell client does not support the SerializationVersion {1} negotiated by the server.
        ///    Make sure the server is compatible with the build {2} of PowerShell.
        /// </exception>
        private bool RunClientNegotiationAlgorithm(RemoteSessionCapability serverRemoteSessionCapability)
        {
            Dbg.Assert(serverRemoteSessionCapability != null, "server capability cache must be non-null");

            // ProtocolVersion check
            Version serverProtocolVersion = serverRemoteSessionCapability.ProtocolVersion;

            _serverProtocolVersion = serverProtocolVersion;
            Version clientProtocolVersion = Context.ClientCapability.ProtocolVersion;

            if (
                clientProtocolVersion.Equals(serverProtocolVersion) ||
                (clientProtocolVersion == RemotingConstants.ProtocolVersionWin7RTM &&
                 serverProtocolVersion == RemotingConstants.ProtocolVersionWin7RC) ||
                (clientProtocolVersion == RemotingConstants.ProtocolVersionWin8RTM &&
                 (serverProtocolVersion == RemotingConstants.ProtocolVersionWin7RC ||
                  serverProtocolVersion == RemotingConstants.ProtocolVersionWin7RTM
                 )) ||
                (clientProtocolVersion == RemotingConstants.ProtocolVersionWin10RTM &&
                 (serverProtocolVersion == RemotingConstants.ProtocolVersionWin7RC ||
                  serverProtocolVersion == RemotingConstants.ProtocolVersionWin7RTM ||
                  serverProtocolVersion == RemotingConstants.ProtocolVersionWin8RTM
                 ))
                )
            {
                //passed negotiation check
            }
            else
            {
                PSRemotingDataStructureException reasonOfFailure =
                    new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed,
                                                         RemoteDataNameStrings.PS_STARTUP_PROTOCOL_VERSION_NAME,
                                                         serverProtocolVersion,
                                                         PSVersionInfo.GitCommitId,
                                                         RemotingConstants.ProtocolVersion);
                throw reasonOfFailure;
            }

            // PSVersion check
            Version serverPSVersion = serverRemoteSessionCapability.PSVersion;
            Version clientPSVersion = Context.ClientCapability.PSVersion;

            if (!clientPSVersion.Equals(serverPSVersion))
            {
                PSRemotingDataStructureException reasonOfFailure =
                    new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed,
                                                         RemoteDataNameStrings.PSVersion,
                                                         serverPSVersion.ToString(),
                                                         PSVersionInfo.GitCommitId,
                                                         RemotingConstants.ProtocolVersion);
                throw reasonOfFailure;
            }

            // Serialization Version check
            Version serverSerVersion = serverRemoteSessionCapability.SerializationVersion;
            Version clientSerVersion = Context.ClientCapability.SerializationVersion;

            if (!clientSerVersion.Equals(serverSerVersion))
            {
                PSRemotingDataStructureException reasonOfFailure =
                    new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed,
                                                         RemoteDataNameStrings.SerializationVersion,
                                                         serverSerVersion.ToString(),
                                                         PSVersionInfo.GitCommitId,
                                                         RemotingConstants.ProtocolVersion);
                throw reasonOfFailure;
            }

            return(true);
        }
Exemplo n.º 25
0
        /// <summary>
        /// Verifies the negotiation packet received from the server
        /// </summary>
        /// <param name="serverRemoteSessionCapability">
        /// Capabilities of remote session
        /// </param>
        /// <returns> 
        /// The method returns true if the capability negotiation is successful.
        /// Otherwise, it returns false.
        /// </returns>
        /// <exception cref="PSRemotingDataStructureException">
        /// 1. PowerShell client does not support the PSVersion {1} negotiated by the server. 
        ///    Make sure the server is compatible with the build {2} of PowerShell.
        /// 2. PowerShell client does not support the SerializationVersion {1} negotiated by the server.
        ///    Make sure the server is compatible with the build {2} of PowerShell.
        /// </exception>
        private bool RunClientNegotiationAlgorithm(RemoteSessionCapability serverRemoteSessionCapability)
        {
            Dbg.Assert(serverRemoteSessionCapability != null, "server capability cache must be non-null");

            // ProtocolVersion check
            Version serverProtocolVersion = serverRemoteSessionCapability.ProtocolVersion;
            _serverProtocolVersion = serverProtocolVersion;
            Version clientProtocolVersion = Context.ClientCapability.ProtocolVersion;

            if (
                clientProtocolVersion.Equals(serverProtocolVersion)
                || (clientProtocolVersion == RemotingConstants.ProtocolVersionWin7RTM &&
                    serverProtocolVersion == RemotingConstants.ProtocolVersionWin7RC)
                || (clientProtocolVersion == RemotingConstants.ProtocolVersionWin8RTM &&
                    (serverProtocolVersion == RemotingConstants.ProtocolVersionWin7RC ||
                     serverProtocolVersion == RemotingConstants.ProtocolVersionWin7RTM
                     ))
                || (clientProtocolVersion == RemotingConstants.ProtocolVersionWin10RTM &&
                    (serverProtocolVersion == RemotingConstants.ProtocolVersionWin7RC ||
                     serverProtocolVersion == RemotingConstants.ProtocolVersionWin7RTM ||
                     serverProtocolVersion == RemotingConstants.ProtocolVersionWin8RTM
                     ))
                 )
            {
                //passed negotiation check
            }
            else
            {
                PSRemotingDataStructureException reasonOfFailure =
                    new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed,
                        RemoteDataNameStrings.PS_STARTUP_PROTOCOL_VERSION_NAME,
                        serverProtocolVersion,
                        PSVersionInfo.BuildVersion,
                        RemotingConstants.ProtocolVersion);
                throw reasonOfFailure;
            }

            // PSVersion check
            Version serverPSVersion = serverRemoteSessionCapability.PSVersion;
            Version clientPSVersion = Context.ClientCapability.PSVersion;
            if (!clientPSVersion.Equals(serverPSVersion))
            {
                PSRemotingDataStructureException reasonOfFailure =
                    new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed,
                        RemoteDataNameStrings.PSVersion,
                        serverPSVersion.ToString(),
                        PSVersionInfo.BuildVersion,
                        RemotingConstants.ProtocolVersion);
                throw reasonOfFailure;
            }

            // Serialization Version check
            Version serverSerVersion = serverRemoteSessionCapability.SerializationVersion;
            Version clientSerVersion = Context.ClientCapability.SerializationVersion;
            if (!clientSerVersion.Equals(serverSerVersion))
            {
                PSRemotingDataStructureException reasonOfFailure =
                    new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNegotiationFailed,
                        RemoteDataNameStrings.SerializationVersion,
                        serverSerVersion.ToString(),
                        PSVersionInfo.BuildVersion,
                        RemotingConstants.ProtocolVersion);
                throw reasonOfFailure;
            }

            return true;
        }