File size: 12,489 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// 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
{
    /// <summary>
    /// 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.
    /// </summary>
    public sealed class PSSenderInfo : ISerializable
    {
        #region Private Data

        private PSPrimitiveDictionary _applicationArguments;

        #endregion

        #region Serialization

        /// <summary>
        /// Serialization.
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            PSObject psObject = PSObject.AsPSObject(this);
            psObject.GetObjectData(info, context);
        }

        /// <summary>
        /// Deserialization constructor.
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        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

        /// <summary>
        /// Constructs PSPrincipal using PSIdentity and a token (used to construct WindowsIdentity)
        /// </summary>
        /// <param name="userPrincipal">
        /// Connecting User Information
        /// </param>
        /// <param name="httpUrl">
        /// httpUrl element (from WSMAN_SENDER_DETAILS struct).
        /// </param>
        [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#")]
        public PSSenderInfo(PSPrincipal userPrincipal, string httpUrl)
        {
            UserInfo = userPrincipal;
            ConnectionString = httpUrl;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Contains information related to the user connecting to the server.
        /// </summary>
        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.
        }

        /// <summary>
        /// Contains the TimeZone information from the client machine.
        /// </summary>
        public TimeZoneInfo ClientTimeZone => null;

        /// <summary>
        /// Connection string used by the client to connect to the server. This is
        /// directly taken from WSMAN_SENDER_DETAILS struct (from wsman.h)
        /// </summary>
        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.
        }

        /// <summary>
        /// Application arguments (i.e. specified in New-PSSessionOptions -ApplicationArguments)
        /// </summary>
        public PSPrimitiveDictionary ApplicationArguments
        {
            get { return _applicationArguments; }

            internal set { _applicationArguments = value; }
        }

        /// <summary>
        /// "ConfigurationName" from the sever remote session.
        /// </summary>
        public string ConfigurationName { get; internal set; }

        #endregion
    }

    /// <summary>
    /// Defines the basic functionality of a PSPrincipal object.
    /// </summary>
    public sealed class PSPrincipal : IPrincipal
    {
        #region Private Data

        #endregion

        /// <summary>
        /// Gets the identity of the current user principal.
        /// </summary>
        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.
        }

        /// <summary>
        /// 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.
        /// </summary>
        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.
        }

        /// <summary>
        /// Gets the identity of the current principal.
        /// </summary>
        IIdentity IPrincipal.Identity
        {
            get { return this.Identity; }
        }

        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="role"></param>
        /// <returns>
        /// If we were able to get a WindowsIdentity then this will perform the
        /// check using the WindowsIdentity otherwise this will return false.
        /// </returns>
        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;
            }
        }

        /// <summary>
        /// Internal overload of IsInRole() taking a WindowsBuiltInRole enum value.
        /// </summary>
        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

        /// <summary>
        /// Constructs PSPrincipal using PSIdentity and a WindowsIdentity.
        /// </summary>
        /// <param name="identity">
        /// An instance of PSIdentity
        /// </param>
        /// <param name="windowsIdentity">
        /// An instance of WindowsIdentity, if psIdentity represents a windows user. This can be
        /// null.
        /// </param>
        public PSPrincipal(PSIdentity identity, WindowsIdentity windowsIdentity)
        {
            Identity = identity;
            WindowsIdentity = windowsIdentity;
        }

        #endregion
    }

    /// <summary>
    /// Defines the basic functionality of a PSIdentity object.
    /// </summary>
    public sealed class PSIdentity : IIdentity
    {
        #region Private Data

        #endregion

        /// <summary>
        /// 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.
        /// </summary>
        public string AuthenticationType { get; }

        /// <summary>
        /// Gets a value that indicates whether the user has been authenticated.
        /// </summary>
        public bool IsAuthenticated { get; }

        /// <summary>
        /// Gets the name of the user.
        /// </summary>
        public string Name { get; }

        /// <summary>
        /// Gets the certificate details of the user if supported, null otherwise.
        /// </summary>
        public PSCertificateDetails CertificateDetails { get; }

        #region Public Constructor

        /// <summary>
        /// Constructor used to construct a PSIdentity object.
        /// </summary>
        /// <param name="authType">
        /// 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
        /// </param>
        /// <param name="isAuthenticated">
        /// true if this user is authenticated.
        /// </param>
        /// <param name="userName">
        /// Name of the user
        /// </param>
        /// <param name="cert">
        /// Certificate details if Certificate authentication is used.
        /// </param>
        public PSIdentity(string authType, bool isAuthenticated, string userName, PSCertificateDetails cert)
        {
            AuthenticationType = authType;
            IsAuthenticated = isAuthenticated;
            Name = userName;
            CertificateDetails = cert;
        }

        #endregion
    }

    /// <summary>
    /// Represents the certificate of a user.
    /// </summary>
    public sealed class PSCertificateDetails
    {
        #region Private Data

        #endregion

        /// <summary>
        /// Gets Subject of the certificate.
        /// </summary>
        public string Subject { get; }

        /// <summary>
        /// Gets the issuer name of the certificate.
        /// </summary>
        public string IssuerName { get; }

        /// <summary>
        /// Gets the issuer thumb print.
        /// </summary>
        public string IssuerThumbprint { get; }

        #region Constructor

        /// <summary>
        /// Constructor used to construct a PSCertificateDetails object.
        /// </summary>
        /// <param name="subject">
        /// Subject of the certificate.
        /// </param>
        /// <param name="issuerName">
        /// Issuer name of the certificate.
        /// </param>
        /// <param name="issuerThumbprint">
        /// Issuer thumb print of the certificate.
        /// </param>
        public PSCertificateDetails(string subject, string issuerName, string issuerThumbprint)
        {
            Subject = subject;
            IssuerName = issuerName;
            IssuerThumbprint = issuerThumbprint;
        }

        #endregion
    }
}