2 // System.Security.Principal.WindowsIdentity
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Sebastien Pouliot (sebastien@ximian.com)
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.CompilerServices;
33 using System.Runtime.InteropServices;
34 using System.Runtime.Serialization;
35 using System.Security.Permissions;
37 namespace System.Security.Principal {
41 public class WindowsIdentity :
43 System.Security.Claims.ClaimsIdentity,
45 IIdentity, IDeserializationCallback, ISerializable, IDisposable {
46 private IntPtr _token;
48 private WindowsAccountType _account;
49 private bool _authenticated;
51 private SerializationInfo _info;
53 static private IntPtr invalidWindows = IntPtr.Zero;
55 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
56 public WindowsIdentity (IntPtr userToken)
57 : this (userToken, null, WindowsAccountType.Normal, false)
61 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
62 public WindowsIdentity (IntPtr userToken, string type)
63 : this (userToken, type, WindowsAccountType.Normal, false)
67 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
68 public WindowsIdentity (IntPtr userToken, string type, WindowsAccountType acctType)
69 : this (userToken, type, acctType, false)
73 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
74 public WindowsIdentity (IntPtr userToken, string type, WindowsAccountType acctType, bool isAuthenticated)
78 _authenticated = isAuthenticated;
80 // last - as it can override some fields
84 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
85 public WindowsIdentity (string sUserPrincipalName)
86 : this (sUserPrincipalName, null)
90 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
91 public WindowsIdentity (string sUserPrincipalName, string type)
93 if (sUserPrincipalName == null)
94 throw new NullReferenceException ("sUserPrincipalName");
96 // TODO: Windows 2003 compatibility should be done in runtime
97 IntPtr token = GetUserToken (sUserPrincipalName);
98 if ((!Environment.IsUnix) && (token == IntPtr.Zero)) {
99 throw new ArgumentException ("only for Windows Server 2003 +");
102 _authenticated = true;
103 _account = WindowsAccountType.Normal;
105 // last - as it can override some fields
109 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
110 public WindowsIdentity (SerializationInfo info, StreamingContext context)
116 public void Dispose ()
118 _token = IntPtr.Zero;
122 protected virtual void Dispose (bool disposing)
124 _token = IntPtr.Zero;
128 public static WindowsIdentity GetAnonymous ()
130 WindowsIdentity id = null;
131 if (Environment.IsUnix) {
132 id = new WindowsIdentity ("nobody");
134 id._account = WindowsAccountType.Anonymous;
135 id._authenticated = false;
136 id._type = String.Empty;
139 id = new WindowsIdentity (IntPtr.Zero, String.Empty, WindowsAccountType.Anonymous, false);
140 // special case (don't try to resolve the name)
141 id._name = String.Empty;
146 public static WindowsIdentity GetCurrent ()
148 return new WindowsIdentity (GetCurrentToken (), null, WindowsAccountType.Normal, true);
150 [MonoTODO ("need icall changes")]
151 public static WindowsIdentity GetCurrent (bool ifImpersonating)
153 throw new NotImplementedException ();
156 [MonoTODO ("need icall changes")]
157 public static WindowsIdentity GetCurrent (TokenAccessLevels desiredAccess)
159 throw new NotImplementedException ();
163 public virtual WindowsImpersonationContext Impersonate ()
165 return new WindowsImpersonationContext (_token);
168 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
169 public static WindowsImpersonationContext Impersonate (IntPtr userToken)
171 return new WindowsImpersonationContext (userToken);
178 public string AuthenticationType {
179 get { return _type; }
182 public virtual bool IsAnonymous
184 get { return (_account == WindowsAccountType.Anonymous); }
192 public bool IsAuthenticated
194 get { return _authenticated; }
197 public virtual bool IsGuest
199 get { return (_account == WindowsAccountType.Guest); }
202 public virtual bool IsSystem
204 get { return (_account == WindowsAccountType.System); }
216 // revolve name (runtime)
217 _name = GetTokenName (_token);
223 public virtual IntPtr Token
225 get { return _token; }
227 [MonoTODO ("not implemented")]
228 public IdentityReferenceCollection Groups {
229 get { throw new NotImplementedException (); }
232 [MonoTODO ("not implemented")]
234 public TokenImpersonationLevel ImpersonationLevel {
235 get { throw new NotImplementedException (); }
238 [MonoTODO ("not implemented")]
240 public SecurityIdentifier Owner {
241 get { throw new NotImplementedException (); }
244 [MonoTODO ("not implemented")]
246 public SecurityIdentifier User {
247 get { throw new NotImplementedException (); }
249 void IDeserializationCallback.OnDeserialization (object sender)
251 _token = (IntPtr) _info.GetValue ("m_userToken", typeof (IntPtr));
252 // can't trust this alone - we must validate the token
253 _name = _info.GetString ("m_name");
255 // validate token by comparing names
256 string name = GetTokenName (_token);
258 throw new SerializationException ("Token-Name mismatch.");
261 // validate token by getting name
262 _name = GetTokenName (_token);
264 throw new SerializationException ("Token doesn't match a user.");
266 _type = _info.GetString ("m_type");
267 _account = (WindowsAccountType) _info.GetValue ("m_acctType", typeof (WindowsAccountType));
268 _authenticated = _info.GetBoolean ("m_isAuthenticated");
271 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
273 info.AddValue ("m_userToken", _token);
274 // can be null when not resolved
275 info.AddValue ("m_name", _name);
276 info.AddValue ("m_type", _type);
277 info.AddValue ("m_acctType", _account);
278 info.AddValue ("m_isAuthenticated", _authenticated);
281 private void SetToken (IntPtr token)
283 if (Environment.IsUnix) {
289 // override user choice in this specific case
290 if (_token == IntPtr.Zero)
291 _account = WindowsAccountType.System;
294 if ((token == invalidWindows) && (_account != WindowsAccountType.Anonymous))
295 throw new ArgumentException ("Invalid token");
304 // see mono/mono/metadata/security.c for implementation
306 // Many people use reflection to get a user's roles - so many
307 // that's it's hard to say it's an "undocumented" feature -
308 // so we also implement it in Mono :-/
309 // http://www.dotnet247.com/247reference/msgs/39/195403.aspx
310 [MethodImplAttribute (MethodImplOptions.InternalCall)]
311 internal extern static string[] _GetRoles (IntPtr token);
313 [MethodImplAttribute (MethodImplOptions.InternalCall)]
314 internal extern static IntPtr GetCurrentToken ();
316 [MethodImplAttribute (MethodImplOptions.InternalCall)]
317 private extern static string GetTokenName (IntPtr token);
319 [MethodImplAttribute (MethodImplOptions.InternalCall)]
320 private extern static IntPtr GetUserToken (string username);