// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.InteropServices; using System.Text; using Dbg = System.Diagnostics.Debug; namespace System.Management.Automation.Remoting.Client { internal static class WSManNativeApi { internal const uint INFINITE = 0xFFFFFFFF; internal const string PS_CREATION_XML_TAG = "creationXml"; internal const string PS_CONNECT_XML_TAG = "connectXml"; internal const string PS_CONNECTRESPONSE_XML_TAG = "connectResponseXml"; internal const string PS_XML_NAMESPACE = "http://schemas.microsoft.com/powershell"; internal const string WSMAN_STREAM_ID_STDOUT = "stdout"; internal const string WSMAN_STREAM_ID_PROMPTRESPONSE = "pr"; internal const string WSMAN_STREAM_ID_STDIN = "stdin"; internal const string ResourceURIPrefix = @"http://schemas.microsoft.com/powershell/"; internal const string NoProfile = "WINRS_NOPROFILE"; internal const string CodePage = "WINRS_CODEPAGE"; internal static readonly Version WSMAN_STACK_VERSION = new Version(3, 0); internal const int WSMAN_FLAG_REQUESTED_API_VERSION_1_1 = 1; // WSMan's default max env size in V2 internal const int WSMAN_DEFAULT_MAX_ENVELOPE_SIZE_KB_V2 = 150; // WSMan's default max env size in V3 internal const int WSMAN_DEFAULT_MAX_ENVELOPE_SIZE_KB_V3 = 500; #region WSMan errors /// /// The WinRM service cannot process the request because the request needs to be sent /// to a different machine. /// Use the redirect information to send the request to a new machine. /// 0x8033819B from sdk\inc\wsmerror.h. /// internal const int ERROR_WSMAN_REDIRECT_REQUESTED = -2144108135; /// /// The WS-Management service cannot process the request. The resource URI is missing or /// it has an incorrect format. Check the documentation or use the following command for /// information on how to construct a resource URI: "winrm help uris". /// internal const int ERROR_WSMAN_INVALID_RESOURCE_URI = -2144108485; /// /// The WinRM service cannon re-connect the session because the session is no longer /// associated with this transportmanager object. /// internal const int ERROR_WSMAN_INUSE_CANNOT_RECONNECT = -2144108083; /// /// Sending data to a remote command failed with the following error message: The client /// cannot connect to the destination specified in the request. Verify that the service on /// the destination is running and is accepting requests. Consult the logs and documentation /// for the WS-Management service running on the destination, most commonly IIS or WinRM. /// If the destination is the WinRM service, run the following command on the destination to /// analyze and configure the WinRM service: /// internal const int ERROR_WSMAN_SENDDATA_CANNOT_CONNECT = -2144108526; /// /// Sending data to a remote command failed with the following error message: The WinRM client /// cannot complete the operation within the time specified. Check if the machine name is valid /// and is reachable over the network and firewall exception for Windows Remote Management service /// is enabled. /// internal const int ERROR_WSMAN_SENDDATA_CANNOT_COMPLETE = -2144108250; internal const int ERROR_WSMAN_ACCESS_DENIED = 5; internal const int ERROR_WSMAN_OUTOF_MEMORY = 14; internal const int ERROR_WSMAN_NETWORKPATH_NOTFOUND = 53; internal const int ERROR_WSMAN_OPERATION_ABORTED = 995; internal const int ERROR_WSMAN_SHUTDOWN_INPROGRESS = 1115; internal const int ERROR_WSMAN_AUTHENTICATION_FAILED = 1311; internal const int ERROR_WSMAN_NO_LOGON_SESSION_EXIST = 1312; internal const int ERROR_WSMAN_LOGON_FAILURE = 1326; internal const int ERROR_WSMAN_IMPROPER_RESPONSE = 1722; internal const int ERROR_WSMAN_INCORRECT_PROTOCOLVERSION = -2141974624; internal const int ERROR_WSMAN_URL_NOTAVAILABLE = -2144108269; internal const int ERROR_WSMAN_INVALID_AUTHENTICATION = -2144108274; internal const int ERROR_WSMAN_CANNOT_CONNECT_INVALID = -2144108080; internal const int ERROR_WSMAN_CANNOT_CONNECT_MISMATCH = -2144108090; internal const int ERROR_WSMAN_CANNOT_CONNECT_RUNASFAILED = -2144108065; internal const int ERROR_WSMAN_CREATEFAILED_INVALIDNAME = -2144108094; internal const int ERROR_WSMAN_TARGETSESSION_DOESNOTEXIST = -2144108453; internal const int ERROR_WSMAN_REMOTESESSION_DISALLOWED = -2144108116; internal const int ERROR_WSMAN_REMOTECONNECTION_DISALLOWED = -2144108061; internal const int ERROR_WSMAN_INVALID_RESOURCE_URI2 = -2144108542; internal const int ERROR_WSMAN_CORRUPTED_CONFIG = -2144108539; internal const int ERROR_WSMAN_URI_LIMIT = -2144108499; internal const int ERROR_WSMAN_CLIENT_KERBEROS_DISABLED = -2144108318; internal const int ERROR_WSMAN_SERVER_NOTTRUSTED = -2144108316; internal const int ERROR_WSMAN_WORKGROUP_NO_KERBEROS = -2144108276; internal const int ERROR_WSMAN_EXPLICIT_CREDENTIALS_REQUIRED = -2144108315; internal const int ERROR_WSMAN_REDIRECT_LOCATION_INVALID = -2144108105; internal const int ERROR_WSMAN_BAD_METHOD = -2144108428; internal const int ERROR_WSMAN_HTTP_SERVICE_UNAVAILABLE = -2144108270; internal const int ERROR_WSMAN_HTTP_SERVICE_ERROR = -2144108176; internal const int ERROR_WSMAN_COMPUTER_NOTFOUND = -2144108103; internal const int ERROR_WSMAN_TARGET_UNKNOWN = -2146893053; internal const int ERROR_WSMAN_CANNOTUSE_IP = -2144108101; #endregion #region MarshalledObject /// /// A struct holding marshalled data (IntPtr). This is /// created to supply IDisposable pattern to safely /// release the unmanaged pointer. /// internal struct MarshalledObject : IDisposable { private IntPtr _dataPtr; /// /// Constructs a MarshalledObject with the supplied /// ptr. /// /// internal MarshalledObject(IntPtr dataPtr) { _dataPtr = dataPtr; } /// /// Gets the unmanaged ptr. /// internal IntPtr DataPtr { get { return _dataPtr; } } /// /// Creates a MarshalledObject for the specified object. /// /// /// Must be a value type. /// /// /// MarshalledObject. internal static MarshalledObject Create(T obj) { IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(obj, ptr, false); // Now create the MarshalledObject and return. MarshalledObject result = new MarshalledObject(); result._dataPtr = ptr; return result; } /// /// Dispose the unmanaged IntPtr. /// public void Dispose() { if (_dataPtr != IntPtr.Zero) { Marshal.FreeHGlobal(_dataPtr); _dataPtr = IntPtr.Zero; } } /// /// Implicit cast to IntPtr. /// /// /// public static implicit operator IntPtr(MarshalledObject obj) { return obj._dataPtr; } } #endregion #region WSMan_Authentication_Credentials /// /// Different Authentication Mechanisms supported by WSMan. /// TODO: By the look of it, this appears like a Flags enum. /// Need to confirm the behavior with WSMan. /// /// /// Please keep in sync with WSManAuthenticationMechanism /// from C:\e\win7_powershell\admin\monad\nttargets\assemblies\logging\ETW\Manifests\Microsoft-Windows-PowerShell-Instrumentation.man /// [Flags] internal enum WSManAuthenticationMechanism : int { /// /// Use the default authentication. /// WSMAN_FLAG_DEFAULT_AUTHENTICATION = 0x0, /// /// Use no authentication for a remote operation. /// WSMAN_FLAG_NO_AUTHENTICATION = 0x1, /// /// Use digest authentication for a remote operation. /// WSMAN_FLAG_AUTH_DIGEST = 0x2, /// /// Use negotiate authentication for a remote operation (may use kerberos or ntlm) /// WSMAN_FLAG_AUTH_NEGOTIATE = 0x4, /// /// Use basic authentication for a remote operation. /// WSMAN_FLAG_AUTH_BASIC = 0x8, /// /// Use kerberos authentication for a remote operation. /// WSMAN_FLAG_AUTH_KERBEROS = 0x10, /// /// Use client certificate authentication for a remote operation. /// WSMAN_FLAG_AUTH_CLIENT_CERTIFICATE = 0x20, /// /// Use CredSSP authentication for a remote operation. /// WSMAN_FLAG_AUTH_CREDSSP = 0x80, } /// /// This is used to represent _WSMAN_AUTHENTICATION_CREDENTIALS /// native structure. _WSMAN_AUTHENTICATION_CREDENTIALS has a union /// member which cannot be easily represented in managed code. /// So created an interface and each union member is represented /// with a different structure. /// internal abstract class BaseWSManAuthenticationCredentials : IDisposable { // used to get Marshalled data of the class. public abstract MarshalledObject GetMarshalledObject(); public void Dispose() { Dispose(true); System.GC.SuppressFinalize(this); } protected virtual void Dispose(bool isDisposing) { } } /// /// Used to supply _WSMAN_USERNAME_PASSWORD_CREDS type credentials for /// WSManCreateSession. /// internal class WSManUserNameAuthenticationCredentials : BaseWSManAuthenticationCredentials { /// /// [StructLayout(LayoutKind.Sequential)] internal struct WSManUserNameCredentialStruct { /// /// internal WSManAuthenticationMechanism authenticationMechanism; /// /// [MarshalAs(UnmanagedType.LPWStr)] internal string userName; /// /// Making password secure. /// internal IntPtr password; } private WSManUserNameCredentialStruct _cred; private MarshalledObject _data; /// /// Default constructor. /// internal WSManUserNameAuthenticationCredentials() { _cred = new WSManUserNameCredentialStruct(); _data = MarshalledObject.Create(_cred); } /// /// Constructs an WSManUserNameAuthenticationCredentials object. /// It is upto the caller to verify if /// and are valid. This API wont complain /// if they are Empty or Null. /// /// /// user name. /// /// /// password. /// /// /// can be 0 (the user did not specify an authentication mechanism, /// WSMan client will choose between Kerberos and Negotiate only); /// if it is not 0, it must be one of the values from /// WSManAuthenticationMechanism enumeration. /// internal WSManUserNameAuthenticationCredentials(string name, System.Security.SecureString pwd, WSManAuthenticationMechanism authMechanism) { _cred = new WSManUserNameCredentialStruct(); _cred.authenticationMechanism = authMechanism; _cred.userName = name; if (pwd != null) { _cred.password = Marshal.SecureStringToCoTaskMemUnicode(pwd); } _data = MarshalledObject.Create(_cred); } /// /// Gets a structure representation (used for marshalling) /// internal WSManUserNameCredentialStruct CredentialStruct { get { return _cred; } } /// /// Marshalled Data. /// /// public override MarshalledObject GetMarshalledObject() { return _data; } /// /// Dispose of the resources. /// /// protected override void Dispose(bool isDisposing) { if (_cred.password != IntPtr.Zero) { Marshal.ZeroFreeCoTaskMemUnicode(_cred.password); _cred.password = IntPtr.Zero; } _data.Dispose(); } } /// /// internal class WSManCertificateThumbprintCredentials : BaseWSManAuthenticationCredentials { /// /// [StructLayout(LayoutKind.Sequential)] private struct WSManThumbprintStruct { /// /// internal WSManAuthenticationMechanism authenticationMechanism; /// /// [MarshalAs(UnmanagedType.LPWStr)] internal string certificateThumbprint; /// /// This is provided for padding as underlying WSMan's implementation /// uses a union, we need to pad up unused fields. /// internal IntPtr reserved; } private MarshalledObject _data; /// /// Constructs an WSManCertificateThumbprintCredentials object. /// It is upto the caller to verify if /// is valid. This API wont complain if it is Empty or Null. /// /// internal WSManCertificateThumbprintCredentials(string thumbPrint) { WSManThumbprintStruct cred = new WSManThumbprintStruct(); cred.authenticationMechanism = WSManAuthenticationMechanism.WSMAN_FLAG_AUTH_CLIENT_CERTIFICATE; cred.certificateThumbprint = thumbPrint; cred.reserved = IntPtr.Zero; _data = MarshalledObject.Create(cred); } /// /// Marshalled Data. /// /// public override MarshalledObject GetMarshalledObject() { return _data; } /// /// Dispose of the resources. /// /// protected override void Dispose(bool isDisposing) { // data is of struct type..so there is no need to set it to null.. _data.Dispose(); } } #endregion #region WSMan Session Options /// /// Enum representing native WSManSessionOption enum. /// internal enum WSManSessionOption : int { #region TimeOuts /// /// Int - default timeout in ms that applies to all operations on the client side. /// WSMAN_OPTION_DEFAULT_OPERATION_TIMEOUTMS = 1, /// /// Int - Robust connection maximum retry time in minutes. /// WSMAN_OPTION_MAX_RETRY_TIME = 11, /// /// Int - timeout in ms for WSManCreateShellEx operations. /// WSMAN_OPTION_TIMEOUTMS_CREATE_SHELL = 12, /// /// Int - timeout in ms for WSManReceiveShellOutputEx operations. /// WSMAN_OPTION_TIMEOUTMS_RECEIVE_SHELL_OUTPUT = 14, /// /// Int - timeout in ms for WSManSendShellInputEx operations. /// WSMAN_OPTION_TIMEOUTMS_SEND_SHELL_INPUT = 15, /// /// Int - timeout in ms for WSManSignalShellEx operations. /// WSMAN_OPTION_TIMEOUTMS_SIGNAL_SHELL = 16, /// /// Int - timeout in ms for WSManCloseShellOperationEx operations. /// WSMAN_OPTION_TIMEOUTMS_CLOSE_SHELL_OPERATION = 17, #endregion #region Connection Options /// /// Int - 1 to not validate the CA on the server certificate; 0 - default. /// WSMAN_OPTION_SKIP_CA_CHECK = 18, /// /// Int - 1 to not validate the CN on the server certificate; 0 - default. /// WSMAN_OPTION_SKIP_CN_CHECK = 19, /// /// Int - 1 to not encrypt the messages; 0 - default. /// WSMAN_OPTION_UNENCRYPTED_MESSAGES = 20, /// /// Int - 1 Send all network packets for remote operations in UTF16; 0 - default is UTF8. /// WSMAN_OPTION_UTF16 = 21, /// /// Int - 1 When using negotiate, include port number in the connection SPN; 0 - default. /// WSMAN_OPTION_ENABLE_SPN_SERVER_PORT = 22, /// /// Int - Used when not talking to the main OS on a machine but, for instance, a BMC /// 1 Identify this machine to the server by including the MachineID header; 0 - default. /// WSMAN_OPTION_MACHINE_ID = 23, /// /// Int -1 Enables host process to be created with interactive token. /// WSMAN_OPTION_USE_INTERACTIVE_TOKEN = 34, #endregion #region Locale /// /// String - RFC 3066 language code. /// WSMAN_OPTION_LOCALE = 25, /// /// String - RFC 3066 language code. /// WSMAN_OPTION_UI_LANGUAGE = 26, #endregion #region Other /// /// Int - max SOAP envelope size (kb) - default 150kb from winrm config /// (see 'winrm help config' for more details); the client SOAP packet size cannot surpass /// this value; this value will be also sent to the server in the SOAP request as a /// MaxEnvelopeSize header; the server will use min(MaxEnvelopeSizeKb from server configuration, /// MaxEnvelopeSize value from SOAP). /// WSMAN_OPTION_MAX_ENVELOPE_SIZE_KB = 28, /// /// Int (read only) - max data size (kb) provided by the client, guaranteed by /// the winrm client implementation to fit into one SOAP packet; this is an /// approximate value calculated based on the WSMAN_OPTION_MAX_ENVELOPE_SIZE_KB (default 150kb), /// the maximum possible size of the SOAP headers and the overhead of the base64 /// encoding which is specific to WSManSendShellInput API; this option can be used /// with WSManGetSessionOptionAsDword API; it cannot be used with WSManSetSessionOption API. /// WSMAN_OPTION_SHELL_MAX_DATA_SIZE_PER_MESSAGE_KB = 29, /// /// String - /// WSMAN_OPTION_REDIRECT_LOCATION = 30, /// /// DWORD - 1 to not validate the revocation status on the server certificate; 0 - default. /// WSMAN_OPTION_SKIP_REVOCATION_CHECK = 31, /// /// DWORD - 1 to allow default credentials for Negotiate (this is for SSL only); 0 - default. /// WSMAN_OPTION_ALLOW_NEGOTIATE_IMPLICIT_CREDENTIALS = 32, /// /// DWORD - When using just a machine name in the connection string use an SSL connection. /// 0 means HTTP, 1 means HTTPS. Default is 0. /// WSMAN_OPTION_USE_SSL = 33 #endregion } /// /// Enum representing WSMan Shell specific options. /// internal enum WSManShellFlag : int { /// /// Turn off compression for Send/Receive operations. By default compression is /// turned on, but if communicating with a down-level box it may be necessary to /// do this. Other reasons for turning it off is due to the extra memory consumption /// and CPU utilization that is used as a result of compression. /// WSMAN_FLAG_NO_COMPRESSION = 1, /// /// Enable the service to drop operation output when running disconnected. /// WSMAN_FLAG_SERVER_BUFFERING_MODE_DROP = 0x4, /// /// Enable the service to block operation progress when output buffers are full. /// WSMAN_FLAG_SERVER_BUFFERING_MODE_BLOCK = 0x8, /// /// Enable receive call to not immediately retrieve results. Only applicable for Receive calls on commands. /// WSMAN_FLAG_RECEIVE_DELAY_OUTPUT_STREAM = 0X10 } #endregion #region WSManData /// /// Types of supported WSMan data. /// PowerShell uses only Text and DWORD (in some places). /// internal enum WSManDataType : uint { WSMAN_DATA_NONE = 0, WSMAN_DATA_TYPE_TEXT = 1, WSMAN_DATA_TYPE_BINARY = 2, WSMAN_DATA_TYPE_WS_XML_READER = 3, WSMAN_DATA_TYPE_DWORD = 4 } [StructLayout(LayoutKind.Sequential)] internal class WSManDataStruct { internal uint type; internal WSManBinaryOrTextDataStruct binaryOrTextData; } [StructLayout(LayoutKind.Sequential)] internal class WSManBinaryOrTextDataStruct { internal int bufferLength; internal IntPtr data; } /// /// Used to supply WSMAN_DATA_BINARY/WSMAN_DATA_TEXT type in place of _WSMAN_DATA. /// internal class WSManData_ManToUn : IDisposable { private readonly WSManDataStruct _internalData; private IntPtr _marshalledObject = IntPtr.Zero; private IntPtr _marshalledBuffer = IntPtr.Zero; /// /// Constructs a WSMAN_DATA_BINARY object. This is used to send /// data to remote end. /// /// internal WSManData_ManToUn(byte[] data) { Dbg.Assert(data != null, "Data cannot be null"); _internalData = new WSManDataStruct(); _internalData.binaryOrTextData = new WSManBinaryOrTextDataStruct(); _internalData.binaryOrTextData.bufferLength = data.Length; _internalData.type = (uint)WSManDataType.WSMAN_DATA_TYPE_BINARY; IntPtr dataToSendPtr = Marshal.AllocHGlobal(_internalData.binaryOrTextData.bufferLength); _internalData.binaryOrTextData.data = dataToSendPtr; _marshalledBuffer = dataToSendPtr; // Stored directly to enable graceful clean up during finalizer scenarios Marshal.Copy(data, 0, _internalData.binaryOrTextData.data, _internalData.binaryOrTextData.bufferLength); _marshalledObject = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(_internalData, _marshalledObject, false); } /// /// Constructs a WSMAN_DATA_TEXT object. This is used to send data /// to remote end. /// /// internal WSManData_ManToUn(string data) { Dbg.Assert(data != null, "Data cannot be null"); _internalData = new WSManDataStruct(); _internalData.binaryOrTextData = new WSManBinaryOrTextDataStruct(); _internalData.binaryOrTextData.bufferLength = data.Length; _internalData.type = (uint)WSManDataType.WSMAN_DATA_TYPE_TEXT; // marshal text data _internalData.binaryOrTextData.data = Marshal.StringToHGlobalUni(data); _marshalledBuffer = _internalData.binaryOrTextData.data; // Stored directly to enable graceful clean up during finalizer scenarios _marshalledObject = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(_internalData, _marshalledObject, false); } /// /// Gets the type of data. /// internal uint Type { get { return _internalData.type; } set { _internalData.type = value; } } /// /// Gets the buffer length of data. /// internal int BufferLength { get { return _internalData.binaryOrTextData.bufferLength; } set { _internalData.binaryOrTextData.bufferLength = value; } } /// /// Free unmanaged resources. All users of this class should call Dispose rather than /// depending on the finalizer to clean it up. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool isDisposing) { // Managed objects should not be deleted when this is called via the finalizer // because they may have been collected already. To prevent leaking the marshalledBuffer // pointer, we are storing its value as a private member of the class, just like marshalledObject. if (_marshalledBuffer != IntPtr.Zero) { Marshal.FreeHGlobal(_marshalledBuffer); _marshalledBuffer = IntPtr.Zero; } if (_marshalledObject != IntPtr.Zero) { Marshal.FreeHGlobal(_marshalledObject); _marshalledObject = IntPtr.Zero; } } /// /// Finalizes an instance of the class. /// ~WSManData_ManToUn() { Dispose(false); } /// /// Implicit IntPtr conversion. /// /// /// public static implicit operator IntPtr(WSManData_ManToUn data) { if (data != null) { return data._marshalledObject; } else { return IntPtr.Zero; } } } internal class WSManData_UnToMan { /// /// Gets the type of data. /// private uint _type; internal uint Type { get { return _type; } set { _type = value; } } /// /// Gets the buffer length of data. /// private int _bufferLength; internal int BufferLength { get { return _bufferLength; } set { _bufferLength = value; } } private string _text; internal string Text { get { if (this.Type == (uint)WSManDataType.WSMAN_DATA_TYPE_TEXT) return _text; else return string.Empty; } } private byte[] _data; internal byte[] Data { get { if (this.Type == (uint)WSManDataType.WSMAN_DATA_TYPE_BINARY) return _data; else return Array.Empty(); } } /// /// Converts the unmanaged structure to a managed class object. /// /// /// internal static WSManData_UnToMan UnMarshal(WSManDataStruct dataStruct) { WSManData_UnToMan newData = new WSManData_UnToMan(); newData._type = dataStruct.type; newData._bufferLength = dataStruct.binaryOrTextData.bufferLength; switch (dataStruct.type) { case (uint)WSManNativeApi.WSManDataType.WSMAN_DATA_TYPE_TEXT: if (dataStruct.binaryOrTextData.bufferLength > 0) { string tempText = Marshal.PtrToStringUni(dataStruct.binaryOrTextData.data, dataStruct.binaryOrTextData.bufferLength); newData._text = tempText; } break; case (uint)WSManNativeApi.WSManDataType.WSMAN_DATA_TYPE_BINARY: if (dataStruct.binaryOrTextData.bufferLength > 0) { // copy data from unmanaged heap to managed heap. byte[] dataRecvd = new byte[dataStruct.binaryOrTextData.bufferLength]; Marshal.Copy( dataStruct.binaryOrTextData.data, dataRecvd, 0, dataStruct.binaryOrTextData.bufferLength); newData._data = dataRecvd; } break; default: throw new NotSupportedException(); } return newData; } /// /// Converts the unmanaged pointer to a managed class object. /// /// /// internal static WSManData_UnToMan UnMarshal(IntPtr unmanagedData) { WSManData_UnToMan result = null; if (unmanagedData != IntPtr.Zero) { WSManDataStruct resultInternal = Marshal.PtrToStructure(unmanagedData); result = WSManData_UnToMan.UnMarshal(resultInternal); } return result; } } /// /// Used to supply a DWORD data in place of _WSMAN_DATA. /// [StructLayout(LayoutKind.Sequential)] internal struct WSManDataDWord { private readonly WSManDataType _type; private WSManDWordDataInternal _dwordData; /// /// Constructs a WSMAN_DATA_DWORD object. /// /// internal WSManDataDWord(int data) { _dwordData = new WSManDWordDataInternal(); _dwordData.number = data; _type = WSManDataType.WSMAN_DATA_TYPE_DWORD; } /// /// Creates an unmanaged ptr which holds the class data. /// This unmanaged ptr can be used with WSMan native API. /// /// internal MarshalledObject Marshal() { return MarshalledObject.Create(this); } /// /// This struct is created to honor struct boundaries between /// x86,amd64 and ia64. WSMan defines a generic WSMAN_DATA /// structure that addresses DWORD, binary, text data. /// [StructLayout(LayoutKind.Sequential)] private struct WSManDWordDataInternal { internal int number; internal IntPtr reserved; } } #endregion #region WSManShellStartupInfo / WSManOptionSet / WSManCommandArgSet / WSManProxyInfo /// /// WSMan allows multiple streams within a shell but powershell is /// using only 1 stream for input and 1 stream for output to allow /// sequencing of data. Because of this the following structure will /// have only one string to hold stream information. /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] internal struct WSManStreamIDSetStruct { internal int streamIDsCount; internal IntPtr streamIDs; } internal struct WSManStreamIDSet_ManToUn { private WSManStreamIDSetStruct _streamSetInfo; private MarshalledObject _data; /// /// Constructor. /// /// internal WSManStreamIDSet_ManToUn(string[] streamIds) { Dbg.Assert(streamIds != null, "stream ids cannot be null or empty"); int sizeOfIntPtr = Marshal.SizeOf(); _streamSetInfo = new WSManStreamIDSetStruct(); _streamSetInfo.streamIDsCount = streamIds.Length; _streamSetInfo.streamIDs = Marshal.AllocHGlobal(sizeOfIntPtr * streamIds.Length); for (int index = 0; index < streamIds.Length; index++) { IntPtr streamAddress = Marshal.StringToHGlobalUni(streamIds[index]); Marshal.WriteIntPtr(_streamSetInfo.streamIDs, index * sizeOfIntPtr, streamAddress); } _data = MarshalledObject.Create(_streamSetInfo); } /// /// Free resources. /// internal void Dispose() { if (_streamSetInfo.streamIDs != IntPtr.Zero) { int sizeOfIntPtr = Marshal.SizeOf(); for (int index = 0; index < _streamSetInfo.streamIDsCount; index++) { IntPtr streamAddress = IntPtr.Zero; streamAddress = Marshal.ReadIntPtr(_streamSetInfo.streamIDs, index * sizeOfIntPtr); if (streamAddress != IntPtr.Zero) { Marshal.FreeHGlobal(streamAddress); streamAddress = IntPtr.Zero; } } Marshal.FreeHGlobal(_streamSetInfo.streamIDs); _streamSetInfo.streamIDs = IntPtr.Zero; } _data.Dispose(); } /// /// Implicit cast to IntPtr. /// /// /// public static implicit operator IntPtr(WSManStreamIDSet_ManToUn obj) { return obj._data.DataPtr; } } internal class WSManStreamIDSet_UnToMan { internal string[] streamIDs; internal int streamIDsCount; /// /// Converts the unmanaged pointer to a managed class object. /// /// /// internal static WSManStreamIDSet_UnToMan UnMarshal(IntPtr unmanagedData) { WSManStreamIDSet_UnToMan result = null; if (unmanagedData != IntPtr.Zero) { WSManStreamIDSetStruct resultInternal = Marshal.PtrToStructure(unmanagedData); result = new WSManStreamIDSet_UnToMan(); string[] idsArray = null; if (resultInternal.streamIDsCount > 0) { idsArray = new string[resultInternal.streamIDsCount]; IntPtr[] ptrs = new IntPtr[resultInternal.streamIDsCount]; Marshal.Copy(resultInternal.streamIDs, ptrs, 0, resultInternal.streamIDsCount); // Marshal the array of string pointers for (int i = 0; i < resultInternal.streamIDsCount; i++) { idsArray[i] = Marshal.PtrToStringUni(ptrs[i]); // Marshal the string pointers into strings } /* * // TODO: Why didn't this work? It looks more efficient idsArray = new string[resultInternal.streamIDsCount]; int sizeInBytes = Marshal.SizeOf(); IntPtr perElementPtr = resultInternal.streamIDs; for (int i = 0; i < resultInternal.streamIDsCount; i++) { IntPtr p = IntPtr.Add(perElementPtr, (i * sizeInBytes)); idsArray[i] = Marshal.PtrToStringUni(p); } */ } result.streamIDs = idsArray; result.streamIDsCount = resultInternal.streamIDsCount; } return result; } } /// /// Managed to Unmanaged: Option struct used to pass optional information with WSManCreateShellEx . /// Unmanaged to Managed: Included in WSManPluginRequest. /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] internal struct WSManOption { /// /// Underlying type = PCWSTR. /// [MarshalAs(UnmanagedType.LPWStr)] internal string name; /// /// Underlying type = PCWSTR. /// [MarshalAs(UnmanagedType.LPWStr)] internal string value; /// /// Underlying type = BOOL. /// internal bool mustComply; } /// /// Unmanaged to Managed: WSMAN_OPERATION_INFO includes the struct directly, so this cannot be made internal to WsmanOptionSet. /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] internal struct WSManOptionSetStruct { internal int optionsCount; /// /// Pointer to an array of WSManOption objects. /// internal IntPtr options; internal bool optionsMustUnderstand; } /// /// Option set struct used to pass optional information /// with WSManCreateShellEx. /// internal struct WSManOptionSet : IDisposable { #region Managed to Unmanaged private WSManOptionSetStruct _optionSet; private MarshalledObject _data; /// /// Options to construct this OptionSet with. /// /// internal WSManOptionSet(WSManOption[] options) { Dbg.Assert(options != null, "options cannot be null"); int sizeOfOption = Marshal.SizeOf(); _optionSet = new WSManOptionSetStruct(); _optionSet.optionsCount = options.Length; _optionSet.optionsMustUnderstand = true; _optionSet.options = Marshal.AllocHGlobal(sizeOfOption * options.Length); for (int index = 0; index < options.Length; index++) { // Look at the structure of native WSManOptionSet.. Options is a pointer.. // In C-Style array individual elements are continuous..so I am building // continuous array elements here. Marshal.StructureToPtr(options[index], (IntPtr)(_optionSet.options.ToInt64() + (sizeOfOption * index)), false); } _data = MarshalledObject.Create(_optionSet); // For conformity: this.optionsCount = 0; this.options = null; this.optionsMustUnderstand = false; } /// /// public void Dispose() { if (_optionSet.options != IntPtr.Zero) { Marshal.FreeHGlobal(_optionSet.options); _optionSet.options = IntPtr.Zero; } // dispose option set _data.Dispose(); } /// /// Implicit IntPtr cast. /// /// /// public static implicit operator IntPtr(WSManOptionSet optionSet) { return optionSet._data.DataPtr; } #endregion #region Unmanaged to Managed internal int optionsCount; internal WSManOption[] options; internal bool optionsMustUnderstand; /// /// Converts the unmanaged pointer to a managed class object. /// /// /// internal static WSManOptionSet UnMarshal(IntPtr unmanagedData) { if (unmanagedData == IntPtr.Zero) { return new WSManOptionSet(); } else { WSManOptionSetStruct resultInternal = Marshal.PtrToStructure(unmanagedData); return UnMarshal(resultInternal); } } /// /// Converts the unmanaged structure to a managed class object. /// /// /// internal static WSManOptionSet UnMarshal(WSManOptionSetStruct resultInternal) { WSManOption[] tempOptions = null; if (resultInternal.optionsCount > 0) { tempOptions = new WSManOption[resultInternal.optionsCount]; int sizeInBytes = Marshal.SizeOf(); IntPtr perElementPtr = resultInternal.options; for (int i = 0; i < resultInternal.optionsCount; i++) { IntPtr p = IntPtr.Add(perElementPtr, (i * sizeInBytes)); tempOptions[i] = Marshal.PtrToStructure(p); } } WSManOptionSet result = new WSManOptionSet(); result.optionsCount = resultInternal.optionsCount; result.options = tempOptions; result.optionsMustUnderstand = resultInternal.optionsMustUnderstand; return result; } #endregion } /// /// internal struct WSManCommandArgSet : IDisposable { [StructLayout(LayoutKind.Sequential)] internal struct WSManCommandArgSetInternal { internal int argsCount; internal IntPtr args; } private WSManCommandArgSetInternal _internalData; private MarshalledObject _data; #region Managed to Unmanaged internal WSManCommandArgSet(byte[] firstArgument) { _internalData = new WSManCommandArgSetInternal(); _internalData.argsCount = 1; _internalData.args = Marshal.AllocHGlobal(Marshal.SizeOf()); // argument set takes only strings..but powershell's serialized pipeline might contain // \0 (null characters) which are unacceptable in WSMan. So we are converting to Base64 // here. The server will convert this back to original string. string base64EncodedArgument = Convert.ToBase64String(firstArgument); IntPtr firstArgAddress = Marshal.StringToHGlobalUni(base64EncodedArgument); Marshal.WriteIntPtr(_internalData.args, firstArgAddress); _data = MarshalledObject.Create(_internalData); // For conformity: this.args = null; this.argsCount = 0; } /// /// Free resources. /// public void Dispose() { IntPtr firstArgAddress = Marshal.ReadIntPtr(_internalData.args); if (firstArgAddress != IntPtr.Zero) { Marshal.FreeHGlobal(firstArgAddress); } Marshal.FreeHGlobal(_internalData.args); _data.Dispose(); } /// /// Implicit cast to IntPtr. /// /// /// public static implicit operator IntPtr(WSManCommandArgSet obj) { return obj._data.DataPtr; } #endregion #region Unmanaged to Managed internal string[] args; internal int argsCount; /// /// Since this is a structure, it must be non-null. This differs in behavior from all the other UnMarshals /// that are classes since they can be null. /// TODO: Do I need to worry about intermediate null characters in the arguments? The managed to unmanaged does! /// /// /// internal static WSManCommandArgSet UnMarshal(IntPtr unmanagedData) { WSManCommandArgSet result = new WSManCommandArgSet(); if (unmanagedData != IntPtr.Zero) { WSManCommandArgSetInternal resultInternal = Marshal.PtrToStructure(unmanagedData); string[] tempArgs = null; if (resultInternal.argsCount > 0) { tempArgs = new string[resultInternal.argsCount]; IntPtr[] ptrs = new IntPtr[resultInternal.argsCount]; Marshal.Copy(resultInternal.args, ptrs, 0, resultInternal.argsCount); // Marshal the array of string pointers for (int i = 0; i < resultInternal.argsCount; i++) { tempArgs[i] = Marshal.PtrToStringUni(ptrs[i]); // Marshal the string pointers into strings } } result.argsCount = resultInternal.argsCount; result.args = tempArgs; } return result; } #endregion } internal struct WSManShellDisconnectInfo : IDisposable { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct WSManShellDisconnectInfoInternal { /// /// New idletimeout for the server shell that overrides the original idletimeout specified in WSManCreateShell. /// internal uint idleTimeoutMs; } private WSManShellDisconnectInfoInternal _internalInfo; internal MarshalledObject data; #region Constructor / Other methods internal WSManShellDisconnectInfo(uint serverIdleTimeOut) { _internalInfo = new WSManShellDisconnectInfoInternal(); _internalInfo.idleTimeoutMs = serverIdleTimeOut; data = MarshalledObject.Create(_internalInfo); } /// /// Disposes the object. /// public void Dispose() { data.Dispose(); } /// /// Implicit IntPtr. /// /// /// public static implicit operator IntPtr(WSManShellDisconnectInfo disconnectInfo) { return disconnectInfo.data.DataPtr; } #endregion } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] internal struct WSManShellStartupInfoStruct { /// /// PowerShell always uses one stream. So no need to expand this. /// Maps to WSManStreamIDSet. /// internal IntPtr inputStreamSet; /// /// PowerShell always uses one stream. So no need to expand this. /// Maps to WSManStreamIDSet. /// internal IntPtr outputStreamSet; /// /// Idle timeout. /// internal uint idleTimeoutMs; /// /// Working directory of the shell. /// [MarshalAs(UnmanagedType.LPWStr)] internal string workingDirectory; /// /// Environment variables available to the shell. /// Maps to WSManEnvironmentVariableSet. /// internal IntPtr environmentVariableSet; /// /// Environment variables available to the shell. /// [MarshalAs(UnmanagedType.LPWStr)] internal string name; } /// /// Managed to unmanaged representation of WSMAN_SHELL_STARTUP_INFO. /// It converts managed values into an unmanaged compatible WSManShellStartupInfoStruct that /// is marshalled into unmanaged memory. /// internal struct WSManShellStartupInfo_ManToUn : IDisposable { private WSManShellStartupInfoStruct _internalInfo; internal MarshalledObject data; #region Constructor / Other methods /// /// Creates a startup info with 1 startup option. /// The startup option is intended to specify the version. /// /// /// /// /// /// /// /// /// internal WSManShellStartupInfo_ManToUn(WSManStreamIDSet_ManToUn inputStreamSet, WSManStreamIDSet_ManToUn outputStreamSet, uint serverIdleTimeOut, string name) { _internalInfo = new WSManShellStartupInfoStruct(); _internalInfo.inputStreamSet = inputStreamSet; _internalInfo.outputStreamSet = outputStreamSet; _internalInfo.idleTimeoutMs = serverIdleTimeOut; // WSMan uses %USER_PROFILE% as the default working directory. _internalInfo.workingDirectory = null; _internalInfo.environmentVariableSet = IntPtr.Zero; _internalInfo.name = name; data = MarshalledObject.Create(_internalInfo); } /// /// Disposes the object. /// public void Dispose() { data.Dispose(); } /// /// Implicit IntPtr. /// /// /// public static implicit operator IntPtr(WSManShellStartupInfo_ManToUn startupInfo) { return startupInfo.data.DataPtr; } #endregion } /// /// Unmanaged to managed representation of WSMAN_SHELL_STARTUP_INFO. /// It unmarshals the unmanaged struct into this object for use by managed code. /// internal class WSManShellStartupInfo_UnToMan { internal WSManStreamIDSet_UnToMan inputStreamSet; internal WSManStreamIDSet_UnToMan outputStreamSet; internal uint idleTimeoutMS; internal string workingDirectory; internal WSManEnvironmentVariableSet environmentVariableSet; internal string name; /// /// Converts the unmanaged pointer to a managed class object. /// /// /// internal static WSManShellStartupInfo_UnToMan UnMarshal(IntPtr unmanagedData) { WSManShellStartupInfo_UnToMan result = null; if (unmanagedData != IntPtr.Zero) { WSManShellStartupInfoStruct resultInternal = Marshal.PtrToStructure(unmanagedData); result = new WSManShellStartupInfo_UnToMan(); result.inputStreamSet = WSManStreamIDSet_UnToMan.UnMarshal(resultInternal.inputStreamSet); result.outputStreamSet = WSManStreamIDSet_UnToMan.UnMarshal(resultInternal.outputStreamSet); result.idleTimeoutMS = resultInternal.idleTimeoutMs; result.workingDirectory = resultInternal.workingDirectory; // TODO: Special marshaling required here? result.environmentVariableSet = WSManEnvironmentVariableSet.UnMarshal(resultInternal.environmentVariableSet); result.name = resultInternal.name; } return result; } } /// /// Managed representation of WSMAN_ENVIRONMENT_VARIABLE_SET. /// It wraps WSManEnvironmentVariableSetInternal and UnMarshals the unmanaged /// data into the object. /// internal class WSManEnvironmentVariableSet { internal uint varsCount; internal WSManEnvironmentVariableInternal[] vars; /// /// Converts the unmanaged pointer to a managed class object. /// /// /// internal static WSManEnvironmentVariableSet UnMarshal(IntPtr unmanagedData) { WSManEnvironmentVariableSet result = null; if (unmanagedData != IntPtr.Zero) { WSManEnvironmentVariableSetInternal resultInternal = Marshal.PtrToStructure(unmanagedData); result = new WSManEnvironmentVariableSet(); WSManEnvironmentVariableInternal[] varsArray = null; if (resultInternal.varsCount > 0) { varsArray = new WSManEnvironmentVariableInternal[resultInternal.varsCount]; int sizeInBytes = Marshal.SizeOf(); IntPtr perElementPtr = resultInternal.vars; for (int i = 0; i < resultInternal.varsCount; i++) { IntPtr p = IntPtr.Add(perElementPtr, (i * sizeInBytes)); varsArray[i] = Marshal.PtrToStructure(p); } } result.vars = varsArray; result.varsCount = resultInternal.varsCount; } return result; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct WSManEnvironmentVariableSetInternal { internal uint varsCount; internal IntPtr vars; // Array of WSManEnvironmentVariableInternal structs } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] internal struct WSManEnvironmentVariableInternal { [MarshalAs(UnmanagedType.LPWStr)] internal string name; [MarshalAs(UnmanagedType.LPWStr)] internal string value; } } /// /// Proxy Info used with WSManCreateSession. /// internal class WSManProxyInfo : IDisposable { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct WSManProxyInfoInternal { public int proxyAccessType; public WSManUserNameAuthenticationCredentials.WSManUserNameCredentialStruct proxyAuthCredentialsStruct; } private MarshalledObject _data; /// /// /// /// internal WSManProxyInfo(ProxyAccessType proxyAccessType, WSManUserNameAuthenticationCredentials authCredentials) { WSManProxyInfoInternal internalInfo = new WSManProxyInfoInternal(); internalInfo.proxyAccessType = (int)proxyAccessType; internalInfo.proxyAuthCredentialsStruct = new WSManUserNameAuthenticationCredentials.WSManUserNameCredentialStruct(); internalInfo.proxyAuthCredentialsStruct.authenticationMechanism = WSManAuthenticationMechanism.WSMAN_FLAG_DEFAULT_AUTHENTICATION; if (authCredentials != null) { internalInfo.proxyAuthCredentialsStruct = authCredentials.CredentialStruct; } _data = MarshalledObject.Create(internalInfo); } public void Dispose() { // data is of struct type..so no need to set it to null. _data.Dispose(); GC.SuppressFinalize(this); } /// /// Implicit IntPtr. /// /// /// public static implicit operator IntPtr(WSManProxyInfo proxyInfo) { return proxyInfo._data.DataPtr; } } #endregion #region WSMan Shell Async /// /// Flags used by all callback functions: WSMAN_COMPLETION_FUNCTION, /// WSMAN_SUBSCRIPTION_COMPLETION_FUNCTION and WSMAN_SHELL_COMPLETION_FUNCTION. /// internal enum WSManCallbackFlags { // // Flag that marks the end of any single step of multistep operation // WSMAN_FLAG_CALLBACK_END_OF_OPERATION = 0x1, // // WSMAN_SHELL_COMPLETION_FUNCTION API specific flags // end of a particular stream; it is used for optimization purposes if the shell // knows that no more output will occur for this stream; in some conditions this // cannot be determined. // WSMAN_FLAG_CALLBACK_END_OF_STREAM = 0x8, // // Flag that if present on CreateShell callback indicates that it supports disconnect // WSMAN_FLAG_CALLBACK_SHELL_SUPPORTS_DISCONNECT = 0x20, // // Marks the end of an auto-disconnect operation. // WSMAN_FLAG_CALLBACK_SHELL_AUTODISCONNECTED = 0x40, // // Network failure notification. // WSMAN_FLAG_CALLBACK_NETWORK_FAILURE_DETECTED = 0x100, // // Network connection retry notification. // WSMAN_FLAG_CALLBACK_RETRYING_AFTER_NETWORK_FAILURE = 0x200, // // Network retry succeeded, connection re-established notification. // WSMAN_FLAG_CALLBACK_RECONNECTED_AFTER_NETWORK_FAILURE = 0x400, // // Retries failed, now auto-disconnecting. // WSMAN_FLAG_CALLBACK_SHELL_AUTODISCONNECTING = 0x800, // // Internal error during retries. Cannot auto-disconnect. Shell failure. // WSMAN_FLAG_CALLBACK_RETRY_ABORTED_DUE_TO_INTERNAL_ERROR = 0x1000, // // Flag that indicates for a receive operation that a delay stream request has been processed // WSMAN_FLAG_RECEIVE_DELAY_STREAM_REQUEST_PROCESSED = 0X2000 } /// /// Completion function used by all Shell functions. Returns error->code != 0 upon error; /// use error->errorDetail structure for extended error informations; the callback is /// called for each shell operation; after a WSManReceiveShellOutput operation is initiated, /// the callback is called for each output stream element or if error; the underlying /// implementation handles the polling of stream data from the command or shell. /// If WSMAN_COMMAND_STATE_DONE state is received, no more streams will be received from the command, /// so the command can be closed using WSManCloseShellOperationEx(command). /// If error->code != 0, the result is guaranteed to be NULL. The error and result objects are /// allocated and owned by the WSMan client stack; they are valid during the callback only; the user /// has to synchronously copy the data in the callback. This callback function will use the current /// access token, whether it is a process or impersonation token. /// /// /// user supplied operation context. /// /// /// one or more flags from WSManCallbackFlags /// /// /// error allocated and owned by the winrm stack; valid in the callback only; /// /// /// shell handle associated with the user context /// /// /// command handle associated with the user context /// /// /// operation handle associated with the user context /// /// /// output data from command/shell; allocated internally and owned by the winrm stack. /// valid only within this function. /// See WSManReceiveDataResult. /// internal delegate void WSManShellCompletionFunction( IntPtr operationContext, int flags, IntPtr error, IntPtr shellOperationHandle, IntPtr commandOperationHandle, IntPtr operationHandle, IntPtr data ); /// /// Struct which holds reference to the callback(delegate) passed to WSMan /// API. /// internal struct WSManShellAsyncCallback { // GC handle which prevents garbage collector from collecting this delegate. private GCHandle _gcHandle; private readonly IntPtr _asyncCallback; internal WSManShellAsyncCallback(WSManShellCompletionFunction callback) { // if a delegate is re-located by a garbage collection, it will not affect // the underlaying managed callback, so Alloc is used to add a reference // to the delegate, allowing relocation of the delegate, but preventing // disposal. Using GCHandle without pinning reduces fragmentation potential // of the managed heap. _gcHandle = GCHandle.Alloc(callback); _asyncCallback = Marshal.GetFunctionPointerForDelegate(callback); } public static implicit operator IntPtr(WSManShellAsyncCallback callback) { return callback._asyncCallback; } } /// /// Used in different WSMan functions to supply async callback. /// internal class WSManShellAsync { [StructLayout(LayoutKind.Sequential)] internal struct WSManShellAsyncInternal { internal IntPtr operationContext; internal IntPtr asyncCallback; } private MarshalledObject _data; private WSManShellAsyncInternal _internalData; internal WSManShellAsync(IntPtr context, WSManShellAsyncCallback callback) { _internalData = new WSManShellAsyncInternal(); _internalData.operationContext = context; _internalData.asyncCallback = callback; _data = MarshalledObject.Create(_internalData); } public void Dispose() { _data.Dispose(); } public static implicit operator IntPtr(WSManShellAsync async) { return async._data; } } /// /// Used in the shell completion function delegate to refer to error. /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] internal struct WSManError { internal int errorCode; /// /// Extended error description from the fault; /// internal string errorDetail; /// /// Language for error description (RFC 3066 language code); it can be NULL. /// internal string language; /// /// Machine id; it can be NULL. /// internal string machineName; /// /// Constructs a WSManError from the unmanaged pointer. /// This involves copying data from unmanaged memory to managed heap. /// /// /// Pointer to unmanaged data. /// /// /// internal static WSManError UnMarshal(IntPtr unmanagedData) { return Marshal.PtrToStructure(unmanagedData); } } internal class WSManCreateShellDataResult { [StructLayout(LayoutKind.Sequential)] private struct WSManCreateShellDataResultInternal { internal WSManDataStruct data; } internal string data; internal static WSManCreateShellDataResult UnMarshal(IntPtr unmanagedData) { WSManCreateShellDataResult result = new WSManCreateShellDataResult(); if (unmanagedData != IntPtr.Zero) { WSManCreateShellDataResultInternal resultInternal = Marshal.PtrToStructure(unmanagedData); string connectData = null; if (resultInternal.data.textData.textLength > 0) { connectData = Marshal.PtrToStringUni(resultInternal.data.textData.text, resultInternal.data.textData.textLength); } result.data = connectData; } return result; } // The following structures are created to honor struct boundaries // on x86,amd64 and ia64 [StructLayout(LayoutKind.Sequential)] private struct WSManDataStruct { internal uint type; internal WSManTextDataInternal textData; } [StructLayout(LayoutKind.Sequential)] private struct WSManTextDataInternal { internal int textLength; internal IntPtr text; } } internal class WSManConnectDataResult { [StructLayout(LayoutKind.Sequential)] private struct WSManConnectDataResultInternal { internal WSManDataStruct data; } internal string data; internal static WSManConnectDataResult UnMarshal(IntPtr unmanagedData) { WSManConnectDataResultInternal resultInternal = Marshal.PtrToStructure(unmanagedData); string connectData = null; if (resultInternal.data.textData.textLength > 0) { connectData = Marshal.PtrToStringUni(resultInternal.data.textData.text, resultInternal.data.textData.textLength); } WSManConnectDataResult result = new WSManConnectDataResult(); result.data = connectData; return result; } // The following structures are created to honor struct boundaries // on x86,amd64 and ia64 [StructLayout(LayoutKind.Sequential)] private struct WSManDataStruct { internal uint type; internal WSManTextDataInternal textData; } [StructLayout(LayoutKind.Sequential)] private struct WSManTextDataInternal { internal int textLength; internal IntPtr text; } } /// /// Used in the shell completion function delegate to refer to the data. /// internal class WSManReceiveDataResult { /// /// The actual data. /// internal byte[] data; /// /// Stream the data belongs to. /// internal string stream; /// /// Constructs a WSManReceiveDataResult from the unmanaged pointer. /// This involves copying data from unmanaged memory to managed heap. /// Currently PowerShell supports only binary data on the wire, so this /// method asserts if the data is not binary. /// /// /// Pointer to unmanaged data. /// /// /// internal static WSManReceiveDataResult UnMarshal(IntPtr unmanagedData) { WSManReceiveDataResultInternal result1 = Marshal.PtrToStructure(unmanagedData); // copy data from unmanaged heap to managed heap. byte[] dataRecvd = null; if (result1.data.binaryData.bufferLength > 0) { dataRecvd = new byte[result1.data.binaryData.bufferLength]; Marshal.Copy(result1.data.binaryData.buffer, dataRecvd, 0, result1.data.binaryData.bufferLength); } #if !UNIX Dbg.Assert(result1.data.type == (uint)WSManDataType.WSMAN_DATA_TYPE_BINARY, "ReceiveDataResult can receive only binary data"); #endif WSManReceiveDataResult result = new WSManReceiveDataResult(); result.data = dataRecvd; result.stream = result1.streamId; return result; } [StructLayout(LayoutKind.Sequential)] private struct WSManReceiveDataResultInternal { [MarshalAs(UnmanagedType.LPWStr)] internal string streamId; internal WSManDataStruct data; [MarshalAs(UnmanagedType.LPWStr)] internal string commandState; internal int exitCode; } // The following structures are created to honor struct boundaries // on x86,amd64 and ia64 [StructLayout(LayoutKind.Sequential)] private struct WSManDataStruct { internal uint type; internal WSManBinaryDataInternal binaryData; } [StructLayout(LayoutKind.Sequential)] private struct WSManBinaryDataInternal { internal int bufferLength; internal IntPtr buffer; } } #endregion #region Plugin API Structure Definitions /// /// This is the managed representation of the WSMAN_PLUGIN_REQUEST struct. /// internal class WSManPluginRequest { /// /// Unmarshalled WSMAN_SENDER_DETAILS struct. /// internal WSManSenderDetails senderDetails; internal string locale; internal string resourceUri; /// /// Unmarshalled WSMAN_OPERATION_INFO struct. /// internal WSManOperationInfo operationInfo; /// /// Kept around to allow direct access to shutdownNotification and its handle. /// private WSManPluginRequestInternal _internalDetails; /// /// Volatile value that should be read directly from its unmanaged location. /// TODO: Does "volatile" still apply when accessing it in managed code. /// internal bool shutdownNotification { get { return _internalDetails.shutdownNotification; } } /// /// Left untouched in unmanaged memory because it is passed directly to /// RegisterWaitForSingleObject(). /// internal IntPtr shutdownNotificationHandle { get { return _internalDetails.shutdownNotificationHandle; } } /// /// Copy of the unmanagedData value used to create the structure. /// internal IntPtr unmanagedHandle; /// /// Converts the unmanaged pointer to a managed class object. /// /// /// internal static WSManPluginRequest UnMarshal(IntPtr unmanagedData) { // Dbg.Assert(IntPtr.Zero != unmanagedData, "unmanagedData must be non-null. This means WinRM sent a bad pointer."); WSManPluginRequest result = null; if (unmanagedData != IntPtr.Zero) { WSManPluginRequestInternal resultInternal = Marshal.PtrToStructure(unmanagedData); result = new WSManPluginRequest(); result.senderDetails = WSManSenderDetails.UnMarshal(resultInternal.senderDetails); result.locale = resultInternal.locale; result.resourceUri = resultInternal.resourceUri; result.operationInfo = WSManOperationInfo.UnMarshal(resultInternal.operationInfo); result._internalDetails = resultInternal; result.unmanagedHandle = unmanagedData; } return result; } /// /// Representation of WSMAN_PLUGIN_REQUEST. /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct WSManPluginRequestInternal { /// /// WSManSenderDetails. /// internal IntPtr senderDetails; [MarshalAs(UnmanagedType.LPWStr)] internal string locale; [MarshalAs(UnmanagedType.LPWStr)] internal string resourceUri; /// /// WSManOperationInfo. /// internal IntPtr operationInfo; internal bool shutdownNotification; internal IntPtr shutdownNotificationHandle; } } internal class WSManSenderDetails { internal string senderName; internal string authenticationMechanism; internal WSManCertificateDetails certificateDetails; internal IntPtr clientToken; // TODO: How should this be marshalled????? internal string httpUrl; /// /// Converts the unmanaged pointer to a managed class object. /// /// /// internal static WSManSenderDetails UnMarshal(IntPtr unmanagedData) { WSManSenderDetails result = null; if (unmanagedData != IntPtr.Zero) { WSManSenderDetailsInternal resultInternal = Marshal.PtrToStructure(unmanagedData); result = new WSManSenderDetails(); result.senderName = resultInternal.senderName; result.authenticationMechanism = resultInternal.authenticationMechanism; result.certificateDetails = WSManCertificateDetails.UnMarshal(resultInternal.certificateDetails); result.clientToken = resultInternal.clientToken; // TODO: UnMarshaling needed here!!!! result.httpUrl = resultInternal.httpUrl; } return result; } /// /// Managed representation of WSMAN_SENDER_DETAILS. /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct WSManSenderDetailsInternal { [MarshalAs(UnmanagedType.LPWStr)] internal string senderName; [MarshalAs(UnmanagedType.LPWStr)] internal string authenticationMechanism; /// /// WSManCertificateDetails. /// internal IntPtr certificateDetails; internal IntPtr clientToken; [MarshalAs(UnmanagedType.LPWStr)] internal string httpUrl; } } internal class WSManCertificateDetails { internal string subject; internal string issuerName; internal string issuerThumbprint; internal string subjectName; /// /// Converts the unmanaged pointer to a managed class object. /// /// /// internal static WSManCertificateDetails UnMarshal(IntPtr unmanagedData) { WSManCertificateDetails result = null; if (unmanagedData != IntPtr.Zero) { WSManCertificateDetailsInternal resultInternal = Marshal.PtrToStructure(unmanagedData); result = new WSManCertificateDetails(); result.subject = resultInternal.subject; result.issuerName = resultInternal.issuerName; result.issuerThumbprint = resultInternal.issuerThumbprint; result.subjectName = resultInternal.subjectName; } return result; } /// /// Managed representation of WSMAN_CERTIFICATE_DETAILS. /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct WSManCertificateDetailsInternal { [MarshalAs(UnmanagedType.LPWStr)] internal string subject; [MarshalAs(UnmanagedType.LPWStr)] internal string issuerName; [MarshalAs(UnmanagedType.LPWStr)] internal string issuerThumbprint; [MarshalAs(UnmanagedType.LPWStr)] internal string subjectName; } } internal class WSManOperationInfo { internal WSManFragmentInternal fragment; internal WSManFilterInternal filter; internal WSManSelectorSet selectorSet; internal WSManOptionSet optionSet; /// /// Converts the unmanaged pointer to a managed class object. /// /// /// internal static WSManOperationInfo UnMarshal(IntPtr unmanagedData) { WSManOperationInfo result = null; if (unmanagedData != IntPtr.Zero) { WSManOperationInfoInternal resultInternal = Marshal.PtrToStructure(unmanagedData); result = new WSManOperationInfo(); result.fragment = resultInternal.fragment; result.filter = resultInternal.filter; result.selectorSet = WSManSelectorSet.UnMarshal(resultInternal.selectorSet); result.optionSet = WSManOptionSet.UnMarshal(resultInternal.optionSet); } return result; } /// /// Managed representation of WSMAN_OPERATION_INFO. /// selectorSet and optionSet are handled differently because they are structs that contain pointers to arrays of structs. /// Most other data structures in the API point to structures using IntPtr rather than including the actual structure. /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct WSManOperationInfoInternal { internal WSManFragmentInternal fragment; internal WSManFilterInternal filter; internal WSManSelectorSet.WSManSelectorSetStruct selectorSet; internal WSManOptionSetStruct optionSet; } /// /// Managed representation of WSMAN_FRAGMENT. /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] internal struct WSManFragmentInternal { [MarshalAs(UnmanagedType.LPWStr)] internal string path; [MarshalAs(UnmanagedType.LPWStr)] internal string dialect; } /// /// Managed representation of WSMAN_FILTER. /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] internal struct WSManFilterInternal { [MarshalAs(UnmanagedType.LPWStr)] internal string filter; [MarshalAs(UnmanagedType.LPWStr)] internal string dialect; } } internal class WSManSelectorSet { internal int numberKeys; internal WSManKeyStruct[] keys; /// /// Converts the unmanaged pointer to a managed class object. /// /// /// internal static WSManSelectorSet UnMarshal(WSManSelectorSetStruct resultInternal) { WSManKeyStruct[] tempKeys = null; if (resultInternal.numberKeys > 0) { tempKeys = new WSManKeyStruct[resultInternal.numberKeys]; int sizeInBytes = Marshal.SizeOf(); IntPtr perElementPtr = resultInternal.keys; for (int i = 0; i < resultInternal.numberKeys; i++) { IntPtr p = IntPtr.Add(perElementPtr, (i * sizeInBytes)); tempKeys[i] = Marshal.PtrToStructure(p); } } WSManSelectorSet result = new WSManSelectorSet(); result.numberKeys = resultInternal.numberKeys; result.keys = tempKeys; return result; } /// /// Managed representation of WSMAN_SELECTOR_SET. /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] internal struct WSManSelectorSetStruct { internal int numberKeys; /// /// Array of WSManKeyStruct structures. /// internal IntPtr keys; } /// /// Managed representation of WSMAN_OPTION_SET. /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] internal struct WSManKeyStruct { [MarshalAs(UnmanagedType.LPWStr)] internal string key; [MarshalAs(UnmanagedType.LPWStr)] internal string value; } } #endregion #region DllImports ClientAPI #if !UNIX internal const string WSManClientApiDll = @"WsmSvc.dll"; internal const string WSManProviderApiDll = @"WsmSvc.dll"; #else internal const string WSManClientApiDll = @"libpsrpclient"; internal const string WSManProviderApiDll = @"libpsrpomiprov"; #endif /// /// This API is used to initialize the WinRM client; /// It can be used by different clients on the same process, ie svchost.exe. /// Returns a nonzero error code upon failure. /// /// /// /// /// /// /// [DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern int WSManInitialize(int flags, [In, Out] ref IntPtr wsManAPIHandle); /// /// This API deinitializes the Winrm client stack; all operations will /// finish before this API will return; this is a sync call; /// it is highly recommended that all operations are explicitly cancelled /// and all sessions are closed before calling this API /// Returns non zero error code upon failure. /// /// /// /// [DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern int WSManDeinitialize(IntPtr wsManAPIHandle, int flags); /// /// Creates a session which can be used to perform subsequent operations /// Returns a non zero error code upon failure. /// /// /// /// if NULL, then connection will default to 127.0.0.1 /// /// /// /// can be null. /// /// /// /// /// [DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern int WSManCreateSession(IntPtr wsManAPIHandle, [MarshalAs(UnmanagedType.LPWStr)] string connection, int flags, IntPtr authenticationCredentials, IntPtr proxyInfo, [In, Out] ref IntPtr wsManSessionHandle); /// /// Frees memory of session and closes all related operations before returning; /// this is sync call it is recommended that all pending operations are either /// completed or cancelled before calling this API. Returns a non zero error /// code upon failure. /// /// /// [DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManCloseSession(IntPtr wsManSessionHandle, int flags); /// /// WSManSetSessionOption API - set session options /// Returns a non zero error code upon failure. /// /// /// /// /// An int (DWORD) data. /// /// internal static int WSManSetSessionOption(IntPtr wsManSessionHandle, WSManSessionOption option, WSManDataDWord data) { MarshalledObject marshalObj = data.Marshal(); using (marshalObj) { return WSManSetSessionOption(wsManSessionHandle, option, marshalObj.DataPtr); } } [DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern int WSManSetSessionOption(IntPtr wsManSessionHandle, WSManSessionOption option, IntPtr data); /// /// WSManGetSessionOptionAsDword API - get a session option. Returns a non /// zero error code upon failure. /// /// /// /// /// An int (DWORD) data. /// /// Zero on success, otherwise the error code. [DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern int WSManGetSessionOptionAsDword(IntPtr wsManSessionHandle, WSManSessionOption option, out int value); /// /// Function that retrieves a WSMan session option as string. Thread.CurrentUICulture /// will be used as the language code to get the error message in. /// /// /// Session option to get. /// internal static string WSManGetSessionOptionAsString(IntPtr wsManAPIHandle, WSManSessionOption option) { Dbg.Assert(wsManAPIHandle != IntPtr.Zero, "wsManAPIHandle cannot be null."); // The error code taken from winerror.h used for getting buffer length. const int ERROR_INSUFFICIENT_BUFFER = 122; string returnval = string.Empty; int bufferSize = 0; // calculate buffer size required if (WSManGetSessionOptionAsString(wsManAPIHandle, option, 0, null, out bufferSize) != ERROR_INSUFFICIENT_BUFFER) { return returnval; } // calculate space required to store output. // StringBuilder will not work for this case as CLR // does not copy the entire string if there are delimiters ('\0') // in the middle of a string. int bufferSizeInBytes = bufferSize * 2; byte[] msgBufferPtr = new byte[bufferSizeInBytes]; // Now get the actual value int messageLength; if (WSManGetSessionOptionAsString(wsManAPIHandle, option, bufferSizeInBytes, msgBufferPtr, out messageLength) != 0) { return returnval; } try { returnval = Encoding.Unicode.GetString(msgBufferPtr, 0, bufferSizeInBytes); } catch (ArgumentNullException) { } catch (System.Text.DecoderFallbackException) { } return returnval; } [DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)] private static extern int WSManGetSessionOptionAsString(IntPtr wsManSessionHandle, WSManSessionOption option, int optionLength, byte[] optionAsString, out int optionLengthUsed); /// /// Creates a shell on the remote end. /// /// /// Session in which the shell is created. /// /// /// /// /// The resource Uri to use to create the shell. /// /// /// /// startup information to be passed to the shell. /// /// /// Options to be passed with CreateShell /// /// /// any content that is used by the remote shell to startup. /// /// /// callback to notify when the create operation completes. /// /// /// An out parameter referencing a WSMan shell operation handle /// for this shell. /// /// internal static void WSManCreateShellEx(IntPtr wsManSessionHandle, int flags, string resourceUri, string shellId, WSManShellStartupInfo_ManToUn startupInfo, WSManOptionSet optionSet, WSManData_ManToUn openContent, IntPtr asyncCallback, ref IntPtr shellOperationHandle) { WSManCreateShellExInternal(wsManSessionHandle, flags, resourceUri, shellId, startupInfo, optionSet, openContent, asyncCallback, ref shellOperationHandle); } [DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManCreateShellEx", SetLastError = false, CharSet = CharSet.Unicode)] private static extern void WSManCreateShellExInternal(IntPtr wsManSessionHandle, int flags, [MarshalAs(UnmanagedType.LPWStr)] string resourceUri, [MarshalAs(UnmanagedType.LPWStr)] string shellId, IntPtr startupInfo, IntPtr optionSet, IntPtr openContent, IntPtr asyncCallback, [In, Out] ref IntPtr shellOperationHandle); /// /// /// /// /// /// /// /// /// /// [DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManConnectShell", SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManConnectShellEx(IntPtr wsManSessionHandle, int flags, [MarshalAs(UnmanagedType.LPWStr)] string resourceUri, [MarshalAs(UnmanagedType.LPWStr)] string shellId, IntPtr optionSet, IntPtr connectXml, IntPtr asyncCallback, [In, Out] ref IntPtr shellOperationHandle); /// /// /// /// /// /// [DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManDisconnectShell", SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManDisconnectShellEx(IntPtr wsManSessionHandle, int flags, IntPtr disconnectInfo, IntPtr asyncCallback); /// /// /// /// /// [DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManReconnectShell", SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManReconnectShellEx(IntPtr wsManSessionHandle, int flags, IntPtr asyncCallback); [DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManReconnectShellCommand", SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManReconnectShellCommandEx(IntPtr wsManCommandHandle, int flags, IntPtr asyncCallback); /// /// Starts a command on the remote end. /// /// /// Shell handle in which the command is created and run. /// /// /// /// /// command line for the command. /// /// /// arguments for the command. /// /// /// options. /// /// /// callback to notify when the operation completes. /// /// /// An out parameter referencing a WSMan shell operation handle /// for this command. /// [DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManRunShellCommandEx", SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManRunShellCommandEx(IntPtr shellOperationHandle, int flags, [MarshalAs(UnmanagedType.LPWStr)] string commandId, [MarshalAs(UnmanagedType.LPWStr)] string commandLine, IntPtr commandArgSet, IntPtr optionSet, IntPtr asyncCallback, ref IntPtr commandOperationHandle); [DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManConnectShellCommand", SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManConnectShellCommandEx(IntPtr shellOperationHandle, int flags, [MarshalAs(UnmanagedType.LPWStr)] string commandID, IntPtr optionSet, IntPtr connectXml, IntPtr asyncCallback, ref IntPtr commandOperationHandle); /// /// Registers a callback with WSMan to receive output from the remote end. /// If commandOperationHandle is null, then the receive callback is registered /// for shell. It is enough to register the callback only once. WSMan will /// keep on calling this callback as and when it has data for a particular /// command + shell. There will be only 1 callback active per command or per shell. /// So if there are multiple commands active, then there can be 1 callback active /// for each of them. /// TODO: How to unregister the callback. /// /// /// Shell Operation Handle. /// /// /// Command Operation Handle. If null, the receive request corresponds /// to the shell. /// /// /// /// /// callback which receives the data asynchronously. /// /// /// handle to use to cancel the operation. /// [DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManReceiveShellOutput", SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManReceiveShellOutputEx(IntPtr shellOperationHandle, IntPtr commandOperationHandle, int flags, IntPtr desiredStreamSet, IntPtr asyncCallback, [In, Out] ref IntPtr receiveOperationHandle); /// /// Send data to the remote end. /// /// /// Shell Operation Handle. /// /// /// Command Operation Handle. If null, the send request corresponds /// to the shell. /// /// /// /// /// /// callback to notify when the operation completes. /// /// /// handle to use to cancel the operation. /// internal static void WSManSendShellInputEx(IntPtr shellOperationHandle, IntPtr commandOperationHandle, int flags, [MarshalAs(UnmanagedType.LPWStr)] string streamId, WSManData_ManToUn streamData, IntPtr asyncCallback, ref IntPtr sendOperationHandle) { WSManSendShellInputExInternal(shellOperationHandle, commandOperationHandle, flags, streamId, streamData, false, asyncCallback, ref sendOperationHandle); } [DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManSendShellInput", SetLastError = false, CharSet = CharSet.Unicode)] private static extern void WSManSendShellInputExInternal(IntPtr shellOperationHandle, IntPtr commandOperationHandle, int flags, [MarshalAs(UnmanagedType.LPWStr)] string streamId, IntPtr streamData, bool endOfStream, IntPtr asyncCallback, [In, Out] ref IntPtr sendOperationHandle); /// /// Closes a shell or a command; if the callback associated with the operation /// is pending and have not completed when WSManCloseShellOperationEx is called, /// the function waits for the callback to finish; If the operation was not finished, /// the operation is cancelled and the operation callback is called with /// WSMAN_ERROR_OPERATION_ABORTED error; then the WSManCloseShellOperationEx callback /// is called with WSMAN_FLAG_CALLBACK_END_OF_OPERATION flag as result of this operation. /// /// /// Shell handle to Close. /// /// /// /// callback to notify when the operation completes. /// [DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManCloseShell(IntPtr shellHandle, int flags, IntPtr asyncCallback); /// /// Closes a command (signals the termination of a command); the WSManCloseCommand callback /// is called with WSMAN_FLAG_CALLBACK_END_OF_OPERATION flag as result of this operation. /// /// /// Command handle to Close. /// /// /// /// callback to notify when the operation completes. /// [DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManCloseCommand(IntPtr cmdHandle, int flags, IntPtr asyncCallback); /// /// Sends a signal. If is null, then the signal will /// be sent to shell. /// /// /// /// /// /// /// [DllImport(WSManNativeApi.WSManClientApiDll, EntryPoint = "WSManSignalShell", SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManSignalShellEx(IntPtr shellOperationHandle, IntPtr cmdOperationHandle, int flags, string code, IntPtr asyncCallback, [In, Out] ref IntPtr signalOperationHandle); /// /// Closes an asynchronous operation; if the callback associated with the operation /// is pending and have not completed when WSManCloseOperation is called, then /// the function marks the operation for deletion and returns; If the callback was not called, /// the operation is cancelled and the operation callback is called with /// WSMAN_ERROR_OPERATION_ABORTED error; the operation handle is freed in all cases /// after the callback returns. /// /// /// /// [DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManCloseOperation(IntPtr operationHandle, int flags); /// /// Function that retrieves WSMan error messages with a particular error code. Thread.CurrentUICulture /// will be used as the language code to get the error message in. /// /// /// /// internal static string WSManGetErrorMessage(IntPtr wsManAPIHandle, int errorCode) { Dbg.Assert(wsManAPIHandle != IntPtr.Zero, "wsManAPIHandle cannot be null."); // The error code taken from winerror.h used for getting buffer length. const int ERROR_INSUFFICIENT_BUFFER = 122; // get language code. string langCode = CultureInfo.CurrentUICulture.Name; string returnval = string.Empty; int bufferSize = 0; // calculate buffer size required if (WSManGetErrorMessage(wsManAPIHandle, 0, langCode, errorCode, 0, null, out bufferSize) != ERROR_INSUFFICIENT_BUFFER) { return returnval; } // calculate space required to store output. // StringBuilder will not work for this case as CLR // does not copy the entire string if there are delimiters ('\0') // in the middle of a string. int bufferSizeInBytes = bufferSize * 2; byte[] msgBufferPtr = new byte[bufferSizeInBytes]; // Now get the actual value int messageLength; if (WSManGetErrorMessage(wsManAPIHandle, 0, langCode, errorCode, bufferSizeInBytes, msgBufferPtr, out messageLength) != 0) { return returnval; } try { returnval = Encoding.Unicode.GetString(msgBufferPtr, 0, bufferSizeInBytes); } catch (ArgumentNullException) { } catch (System.Text.DecoderFallbackException) { } catch (ArgumentOutOfRangeException) { } return returnval; } /// /// Function that retrieves WSMan error messages with a particular error code and a language code. /// /// /// The handle returned by WSManInitialize API call. It cannot be NULL. /// /// /// Reserved for future use. It must be 0. /// /// /// Defines the RFC 3066 language code name that should be used to localize the error. It can be NULL. /// if not specified, the thread's UI language will be used. /// /// /// Represents the error code for the requested error message. This error code can be a hexadecimal or /// decimal component from WSManagement component, WinHttp component or other Windows operating system /// components. /// /// /// Represents the size of the output message buffer in characters, including the NULL terminator. /// If 0, then the "message" parameter must be NULL; in this case the function will return /// ERROR_INSUFFICIENT_BUFFER error and the "messageLengthUsed" parameter will be set to the number /// of characters needed, including NULL terminator. /// /// /// Represents the output buffer to store the message in. It must be allocated/deallocated by the client. /// The buffer must be big enough to store the message plus the NULL terminator otherwise an /// ERROR_INSUFFICIENT_BUFFER error will be returned and the "messageLengthUsed" parameter will be set /// to the number of characters needed, including NULL terminator. If NULL, then the "messageLength" parameter /// must be NULL; in this case the function will return ERROR_INSUFFICIENT_BUFFER error and the "messageLengthUsed" /// parameter will be set to the number of characters needed, including NULL terminator. /// /// /// Represents the effective number of characters written to the output buffer, including the NULL terminator. /// It cannot be NULL. If both "messageLength" and "message" parameters are 0, the function will return ERROR_INSUFFICIENT_BUFFER /// and "messageLengthUsed" parameter will be set to the number of characters needed, including NULL terminator /// [DllImport(WSManNativeApi.WSManClientApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern int WSManGetErrorMessage(IntPtr wsManAPIHandle, int flags, string languageCode, int errorCode, int messageLength, byte[] message, out int messageLengthUsed); #endregion #region DllImports PluginAPI /// /// Gets operational information for items such as time-outs and data restrictions that /// are associated with the operation. /// /// Specifies the resource URI, options, locale, shutdown flag, and handle for the request. /// Specifies the options that are available for retrieval. /// Specifies the result object (WSMAN_DATA). /// [DllImport(WSManNativeApi.WSManProviderApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern int WSManPluginGetOperationParameters( IntPtr requestDetails, int flags, [In, Out, MarshalAs(UnmanagedType.LPStruct)] WSManDataStruct data); // [In, Out] ref IntPtr data); /// /// Reports the completion of an operation by all operation entry points except for the /// WSManPluginStartup and WSManPluginShutdown methods. /// /// Specifies the resource URI, options, locale, shutdown flag, and handle for the request. /// /// Reports any failure in the operation. Terminates on non-NO_ERROR status. /// XML document containing extra error information. /// [DllImport(WSManNativeApi.WSManProviderApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern int WSManPluginOperationComplete( IntPtr requestDetails, int flags, int errorCode, [MarshalAs(UnmanagedType.LPWStr)] string extendedInformation); internal enum WSManFlagReceive : int { /// /// No more data on this stream. Only valid when a stream is specified. /// WSMAN_FLAG_RECEIVE_RESULT_NO_MORE_DATA = 1, /// /// Send the data as soon as possible. Normally data is held onto in /// order to maximise the size of the response packet. This should /// only be used if a request/response style of data is needed between /// the send and receive data streams. /// WSMAN_FLAG_RECEIVE_FLUSH = 2, /// /// Data reported is at a boundary. Plugins usually serialize and fragment /// output data objects and push them along the receive byte stream. /// If the current data chunk being reported is an end fragment of the /// data object current processed, plugins would set this flag. /// WSMAN_FLAG_RECEIVE_RESULT_DATA_BOUNDARY = 4 } internal const string WSMAN_SHELL_NAMESPACE = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell"; internal const string WSMAN_COMMAND_STATE_DONE = WSMAN_SHELL_NAMESPACE + "/CommandState/Done"; internal const string WSMAN_COMMAND_STATE_PENDING = WSMAN_SHELL_NAMESPACE + "/CommandState/Pending"; internal const string WSMAN_COMMAND_STATE_RUNNING = WSMAN_SHELL_NAMESPACE + "/CommandState/Running"; /// /// Reports results for the WSMAN_PLUGIN_RECEIVE plug-in call and is used by most shell /// plug-ins that return results. After all of the data is received, the /// WSManPluginOperationComplete method must be called. /// /// Specifies the resource URI, options, locale, shutdown flag, and handle for the request. /// /// Specifies the stream that the data is associated with. /// A pointer to a WSMAN_DATA structure that specifies the result object that is returned to the client. /// Specifies the state of the command. It must be set to a value specified by the plugin. /// Only set when the commandState is terminating. /// [DllImport(WSManNativeApi.WSManProviderApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern int WSManPluginReceiveResult( IntPtr requestDetails, int flags, [MarshalAs(UnmanagedType.LPWStr)] string stream, IntPtr streamResult, [MarshalAs(UnmanagedType.LPWStr)] string commandState, int exitCode); /// /// Reports shell and command context back to the Windows Remote Management (WinRM) /// infrastructure so that further operations can be performed against the shell and/or /// command. This method is called only for WSManPluginShell and WSManPluginCommand plug-in /// entry points. /// /// Specifies the resource URI, options, locale, shutdown flag, and handle for the request. /// /// Defines the value to pass into all future shell and command operations. Represents either the shell or the command. /// [DllImport(WSManNativeApi.WSManProviderApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern int WSManPluginReportContext( IntPtr requestDetails, int flags, IntPtr context); #if UNIX /// /// Registers the shutdown callback. /// /// Specifies the resource URI, options, locale, shutdown flag, and handle for the request. /// Callback to be executed on shutdown. /// /// [DllImport(WSManNativeApi.WSManProviderApiDll, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern void WSManPluginRegisterShutdownCallback( IntPtr requestDetails, IntPtr shutdownCallback, IntPtr shutdownContext); #endif #endregion } /// /// Interface to enable stubbing of the WSManNativeApi PInvoke calls for /// unit testing. /// Note: It is implemented as a class to avoid exposing it outside the module. /// #nullable enable internal interface IWSManNativeApiFacade { // TODO: Expand this to cover the rest of the API once I prove that it works! int WSManPluginGetOperationParameters( IntPtr requestDetails, int flags, WSManNativeApi.WSManDataStruct data); int WSManPluginOperationComplete( IntPtr requestDetails, int flags, int errorCode, string extendedInformation); int WSManPluginReceiveResult( IntPtr requestDetails, int flags, string? stream, IntPtr streamResult, string commandState, int exitCode); int WSManPluginReportContext( IntPtr requestDetails, int flags, IntPtr context); void WSManPluginRegisterShutdownCallback( IntPtr requestDetails, IntPtr shutdownCallback, IntPtr shutdownContext); } #nullable restore /// /// Concrete implementation of the PInvoke facade for use in the production code. /// internal class WSManNativeApiFacade : IWSManNativeApiFacade { int IWSManNativeApiFacade.WSManPluginGetOperationParameters( IntPtr requestDetails, int flags, WSManNativeApi.WSManDataStruct data) { return WSManNativeApi.WSManPluginGetOperationParameters(requestDetails, flags, data); } int IWSManNativeApiFacade.WSManPluginOperationComplete( IntPtr requestDetails, int flags, int errorCode, string extendedInformation) { return WSManNativeApi.WSManPluginOperationComplete(requestDetails, flags, errorCode, extendedInformation); } int IWSManNativeApiFacade.WSManPluginReceiveResult( IntPtr requestDetails, int flags, string stream, IntPtr streamResult, string commandState, int exitCode) { return WSManNativeApi.WSManPluginReceiveResult(requestDetails, flags, stream, streamResult, commandState, exitCode); } int IWSManNativeApiFacade.WSManPluginReportContext( IntPtr requestDetails, int flags, IntPtr context) { return WSManNativeApi.WSManPluginReportContext(requestDetails, flags, context); } void IWSManNativeApiFacade.WSManPluginRegisterShutdownCallback( IntPtr requestDetails, IntPtr shutdownCallback, IntPtr shutdownContext) { #if UNIX WSManNativeApi.WSManPluginRegisterShutdownCallback(requestDetails, shutdownCallback, shutdownContext); #endif } } }