// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. /* * Contains definition for PSSenderInfo, PSPrincipal, PSIdentity which are * used to provide remote user information to different plugin snapins * like Exchange. */ using System; using System.Security.Principal; using System.Diagnostics.CodeAnalysis; using System.Runtime.Serialization; using Microsoft.PowerShell; namespace System.Management.Automation.Remoting { /// /// This class is used in the server side remoting scenarios. This class /// holds information about the incoming connection like: /// (a) Connecting User information /// (b) Connection String used by the user to connect to the server. /// public sealed class PSSenderInfo : ISerializable { #region Private Data private PSPrimitiveDictionary _applicationArguments; #endregion #region Serialization /// /// Serialization. /// /// /// public void GetObjectData(SerializationInfo info, StreamingContext context) { PSObject psObject = PSObject.AsPSObject(this); psObject.GetObjectData(info, context); } /// /// Deserialization constructor. /// /// /// private PSSenderInfo(SerializationInfo info, StreamingContext context) { if (info == null) { return; } string serializedData = null; try { serializedData = info.GetValue("CliXml", typeof(string)) as string; } catch (Exception) { // When a workflow is run locally, there won't be PSSenderInfo return; } if (serializedData == null) { return; } try { PSObject result = PSObject.AsPSObject(PSSerializer.Deserialize(serializedData)); PSSenderInfo senderInfo = DeserializingTypeConverter.RehydratePSSenderInfo(result); UserInfo = senderInfo.UserInfo; ConnectionString = senderInfo.ConnectionString; _applicationArguments = senderInfo._applicationArguments; } catch (Exception) { // Ignore conversion errors return; } } #endregion #region Public Constructors /// /// Constructs PSPrincipal using PSIdentity and a token (used to construct WindowsIdentity) /// /// /// Connecting User Information /// /// /// httpUrl element (from WSMAN_SENDER_DETAILS struct). /// [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#")] public PSSenderInfo(PSPrincipal userPrincipal, string httpUrl) { UserInfo = userPrincipal; ConnectionString = httpUrl; } #endregion #region Properties /// /// Contains information related to the user connecting to the server. /// public PSPrincipal UserInfo { get; // No public set because PSSenderInfo/PSPrincipal is used by PSSessionConfiguration's // and usually they dont cache this data internally..so did not want to give // cmdlets/scripts a chance to modify these. } /// /// Contains the TimeZone information from the client machine. /// public TimeZoneInfo ClientTimeZone => null; /// /// Connection string used by the client to connect to the server. This is /// directly taken from WSMAN_SENDER_DETAILS struct (from wsman.h) /// public string ConnectionString { get; // No public set because PSSenderInfo/PSPrincipal is used by PSSessionConfiguration's // and usually they dont cache this data internally..so did not want to give // cmdlets/scripts a chance to modify these. } /// /// Application arguments (i.e. specified in New-PSSessionOptions -ApplicationArguments) /// public PSPrimitiveDictionary ApplicationArguments { get { return _applicationArguments; } internal set { _applicationArguments = value; } } /// /// "ConfigurationName" from the sever remote session. /// public string ConfigurationName { get; internal set; } #endregion } /// /// Defines the basic functionality of a PSPrincipal object. /// public sealed class PSPrincipal : IPrincipal { #region Private Data #endregion /// /// Gets the identity of the current user principal. /// public PSIdentity Identity { get; // No public set because PSSenderInfo/PSPrincipal is used by PSSessionConfiguration's // and usually they dont cache this data internally..so did not want to give // cmdlets/scripts a chance to modify these. } /// /// Gets the WindowsIdentity (if possible) representation of the current Identity. /// PSPrincipal can represent any user for example a LiveID user, network user within /// a domain etc. This property tries to convert the Identity to WindowsIdentity /// using the user token supplied. /// public WindowsIdentity WindowsIdentity { get; // No public set because PSSenderInfo/PSPrincipal is used by PSSessionConfiguration's // and usually they dont cache this data internally..so did not want to give // cmdlets/scripts a chance to modify these. } /// /// Gets the identity of the current principal. /// IIdentity IPrincipal.Identity { get { return this.Identity; } } /// /// Determines if the current principal belongs to a specified rule. /// If we were able to get a WindowsIdentity then this will perform the /// check using the WindowsIdentity otherwise this will return false. /// /// /// /// If we were able to get a WindowsIdentity then this will perform the /// check using the WindowsIdentity otherwise this will return false. /// public bool IsInRole(string role) { if (WindowsIdentity != null) { // Get Windows Principal for this identity WindowsPrincipal windowsPrincipal = new WindowsPrincipal(WindowsIdentity); return windowsPrincipal.IsInRole(role); } else { return false; } } /// /// Internal overload of IsInRole() taking a WindowsBuiltInRole enum value. /// internal bool IsInRole(WindowsBuiltInRole role) { if (WindowsIdentity != null) { // Get Windows Principal for this identity WindowsPrincipal windowsPrincipal = new WindowsPrincipal(WindowsIdentity); return windowsPrincipal.IsInRole(role); } else { return false; } } #region Constructor /// /// Constructs PSPrincipal using PSIdentity and a WindowsIdentity. /// /// /// An instance of PSIdentity /// /// /// An instance of WindowsIdentity, if psIdentity represents a windows user. This can be /// null. /// public PSPrincipal(PSIdentity identity, WindowsIdentity windowsIdentity) { Identity = identity; WindowsIdentity = windowsIdentity; } #endregion } /// /// Defines the basic functionality of a PSIdentity object. /// public sealed class PSIdentity : IIdentity { #region Private Data #endregion /// /// Gets the type of authentication used. /// For a WSMan service authenticated user this will be one of the following: /// WSMAN_DEFAULT_AUTHENTICATION /// WSMAN_NO_AUTHENTICATION /// WSMAN_AUTH_DIGEST /// WSMAN_AUTH_NEGOTIATE /// WSMAN_AUTH_BASIC /// WSMAN_AUTH_KERBEROS /// WSMAN_AUTH_CLIENT_CERTIFICATE /// WSMAN_AUTH_LIVEID. /// public string AuthenticationType { get; } /// /// Gets a value that indicates whether the user has been authenticated. /// public bool IsAuthenticated { get; } /// /// Gets the name of the user. /// public string Name { get; } /// /// Gets the certificate details of the user if supported, null otherwise. /// public PSCertificateDetails CertificateDetails { get; } #region Public Constructor /// /// Constructor used to construct a PSIdentity object. /// /// /// Type of authentication used to authenticate this user. /// For a WSMan service authenticated user this will be one of the following: /// WSMAN_DEFAULT_AUTHENTICATION /// WSMAN_NO_AUTHENTICATION /// WSMAN_AUTH_DIGEST /// WSMAN_AUTH_NEGOTIATE /// WSMAN_AUTH_BASIC /// WSMAN_AUTH_KERBEROS /// WSMAN_AUTH_CLIENT_CERTIFICATE /// WSMAN_AUTH_LIVEID /// /// /// true if this user is authenticated. /// /// /// Name of the user /// /// /// Certificate details if Certificate authentication is used. /// public PSIdentity(string authType, bool isAuthenticated, string userName, PSCertificateDetails cert) { AuthenticationType = authType; IsAuthenticated = isAuthenticated; Name = userName; CertificateDetails = cert; } #endregion } /// /// Represents the certificate of a user. /// public sealed class PSCertificateDetails { #region Private Data #endregion /// /// Gets Subject of the certificate. /// public string Subject { get; } /// /// Gets the issuer name of the certificate. /// public string IssuerName { get; } /// /// Gets the issuer thumb print. /// public string IssuerThumbprint { get; } #region Constructor /// /// Constructor used to construct a PSCertificateDetails object. /// /// /// Subject of the certificate. /// /// /// Issuer name of the certificate. /// /// /// Issuer thumb print of the certificate. /// public PSCertificateDetails(string subject, string issuerName, string issuerThumbprint) { Subject = subject; IssuerName = issuerName; IssuerThumbprint = issuerThumbprint; } #endregion } }