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 : IIdentity, IDeserializationCallback {
44 public class WindowsIdentity : IIdentity, IDeserializationCallback, ISerializable, IDisposable {
46 public class WindowsIdentity : IIdentity, IDeserializationCallback, ISerializable {
48 private IntPtr _token;
50 private WindowsAccountType _account;
51 private bool _authenticated;
53 private SerializationInfo _info;
55 static private IntPtr invalidWindows = IntPtr.Zero;
57 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
58 public WindowsIdentity (IntPtr userToken)
59 : this (userToken, null, WindowsAccountType.Normal, false)
63 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
64 public WindowsIdentity (IntPtr userToken, string type)
65 : this (userToken, type, WindowsAccountType.Normal, false)
69 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
70 public WindowsIdentity (IntPtr userToken, string type, WindowsAccountType acctType)
71 : this (userToken, type, acctType, false)
75 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
76 public WindowsIdentity (IntPtr userToken, string type, WindowsAccountType acctType, bool isAuthenticated)
80 _authenticated = isAuthenticated;
82 // last - as it can override some fields
86 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
87 public WindowsIdentity (string sUserPrincipalName)
88 : this (sUserPrincipalName, null)
92 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
93 public WindowsIdentity (string sUserPrincipalName, string type)
95 if (sUserPrincipalName == null)
96 throw new NullReferenceException ("sUserPrincipalName");
98 // TODO: Windows 2003 compatibility should be done in runtime
99 IntPtr token = GetUserToken (sUserPrincipalName);
100 if ((!IsPosix) && (token == IntPtr.Zero)) {
101 throw new ArgumentException ("only for Windows Server 2003 +");
104 _authenticated = true;
105 _account = WindowsAccountType.Normal;
107 // last - as it can override some fields
111 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
112 public WindowsIdentity (SerializationInfo info, StreamingContext context)
120 public void Dispose ()
122 _token = IntPtr.Zero;
126 protected virtual void Dispose (bool disposing)
128 _token = IntPtr.Zero;
133 // clear our copy but don't close it
134 // http://www.develop.com/kbrown/book/html/whatis_windowsprincipal.html
135 _token = IntPtr.Zero;
140 public static WindowsIdentity GetAnonymous ()
142 WindowsIdentity id = null;
144 id = new WindowsIdentity ("nobody");
146 id._account = WindowsAccountType.Anonymous;
147 id._authenticated = false;
148 id._type = String.Empty;
151 id = new WindowsIdentity (IntPtr.Zero, String.Empty, WindowsAccountType.Anonymous, false);
152 // special case (don't try to resolve the name)
153 id._name = String.Empty;
158 public static WindowsIdentity GetCurrent ()
160 return new WindowsIdentity (GetCurrentToken (), null, WindowsAccountType.Normal, true);
163 [MonoTODO ("need icall changes")]
164 public static WindowsIdentity GetCurrent (bool ifImpersonating)
166 throw new NotImplementedException ();
169 [MonoTODO ("need icall changes")]
170 public static WindowsIdentity GetCurrent (TokenAccessLevels desiredAccess)
172 throw new NotImplementedException ();
177 public virtual WindowsImpersonationContext Impersonate ()
179 return new WindowsImpersonationContext (_token);
182 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
183 public static WindowsImpersonationContext Impersonate (IntPtr userToken)
185 return new WindowsImpersonationContext (userToken);
191 public string AuthenticationType {
193 public virtual string AuthenticationType {
195 get { return _type; }
198 public virtual bool IsAnonymous
200 get { return (_account == WindowsAccountType.Anonymous); }
203 public virtual bool IsAuthenticated
205 get { return _authenticated; }
208 public virtual bool IsGuest
210 get { return (_account == WindowsAccountType.Guest); }
213 public virtual bool IsSystem
215 get { return (_account == WindowsAccountType.System); }
218 public virtual string Name
222 // revolve name (runtime)
223 _name = GetTokenName (_token);
229 public virtual IntPtr Token
231 get { return _token; }
234 [MonoTODO ("not implemented")]
235 public IdentityReferenceCollection Groups {
236 get { throw new NotImplementedException (); }
239 [MonoTODO ("not implemented")]
241 public TokenImpersonationLevel ImpersonationLevel {
242 get { throw new NotImplementedException (); }
245 [MonoTODO ("not implemented")]
247 public SecurityIdentifier Owner {
248 get { throw new NotImplementedException (); }
251 [MonoTODO ("not implemented")]
253 public SecurityIdentifier User {
254 get { throw new NotImplementedException (); }
257 void IDeserializationCallback.OnDeserialization (object sender)
259 _token = (IntPtr) _info.GetValue ("m_userToken", typeof (IntPtr));
260 // can't trust this alone - we must validate the token
261 _name = _info.GetString ("m_name");
263 // validate token by comparing names
264 string name = GetTokenName (_token);
266 throw new SerializationException ("Token-Name mismatch.");
269 // validate token by getting name
270 _name = GetTokenName (_token);
271 if ((_name == String.Empty) || (_name == null))
272 throw new SerializationException ("Token doesn't match a user.");
274 _type = _info.GetString ("m_type");
275 _account = (WindowsAccountType) _info.GetValue ("m_acctType", typeof (WindowsAccountType));
276 _authenticated = _info.GetBoolean ("m_isAuthenticated");
279 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
281 info.AddValue ("m_userToken", _token);
282 // can be null when not resolved
283 info.AddValue ("m_name", _name);
284 info.AddValue ("m_type", _type);
285 info.AddValue ("m_acctType", _account);
286 info.AddValue ("m_isAuthenticated", _authenticated);
289 private static bool IsPosix {
290 get { return ((int) Environment.Platform == 128) || ((int)Environment.Platform == 4); }
293 private void SetToken (IntPtr token)
301 // override user choice in this specific case
302 if (_token == IntPtr.Zero)
303 _account = WindowsAccountType.System;
306 if ((token == invalidWindows) && (_account != WindowsAccountType.Anonymous))
307 throw new ArgumentException ("Invalid token");
316 // see mono/mono/metadata/security.c for implementation
318 // Many people use reflection to get a user's roles - so many
319 // that's it's hard to say it's an "undocumented" feature -
320 // so we also implement it in Mono :-/
321 // http://www.dotnet247.com/247reference/msgs/39/195403.aspx
322 [MethodImplAttribute (MethodImplOptions.InternalCall)]
323 internal extern static string[] _GetRoles (IntPtr token);
325 [MethodImplAttribute (MethodImplOptions.InternalCall)]
326 internal extern static IntPtr GetCurrentToken ();
328 [MethodImplAttribute (MethodImplOptions.InternalCall)]
329 private extern static string GetTokenName (IntPtr token);
331 [MethodImplAttribute (MethodImplOptions.InternalCall)]
332 private extern static IntPtr GetUserToken (string username);