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;
56 // that seems to be the value used for (at least) AIX and MacOSX
57 static private IntPtr invalidPosix = (IntPtr) unchecked (-2);
59 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
60 public WindowsIdentity (IntPtr userToken)
61 : this (userToken, null, WindowsAccountType.Normal, false)
65 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
66 public WindowsIdentity (IntPtr userToken, string type)
67 : this (userToken, type, WindowsAccountType.Normal, false)
71 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
72 public WindowsIdentity (IntPtr userToken, string type, WindowsAccountType acctType)
73 : this (userToken, type, acctType, false)
77 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
78 public WindowsIdentity (IntPtr userToken, string type, WindowsAccountType acctType, bool isAuthenticated)
82 _authenticated = isAuthenticated;
84 // last - as it can override some fields
88 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
89 public WindowsIdentity (string sUserPrincipalName)
90 : this (sUserPrincipalName, null)
94 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
95 public WindowsIdentity (string sUserPrincipalName, string type)
97 if (sUserPrincipalName == null)
98 throw new NullReferenceException ("sUserPrincipalName");
100 // TODO: Windows 2003 compatibility should be done in runtime
101 IntPtr token = GetUserToken (sUserPrincipalName);
102 if ((!IsPosix) && (token == IntPtr.Zero)) {
103 throw new ArgumentException ("only for Windows Server 2003 +");
106 _authenticated = true;
107 _account = WindowsAccountType.Normal;
109 // last - as it can override some fields
113 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
114 public WindowsIdentity (SerializationInfo info, StreamingContext context)
122 public void Dispose ()
124 _token = IntPtr.Zero;
128 protected virtual void Dispose (bool disposing)
130 _token = IntPtr.Zero;
135 // clear our copy but don't close it
136 // http://www.develop.com/kbrown/book/html/whatis_windowsprincipal.html
137 _token = IntPtr.Zero;
142 public static WindowsIdentity GetAnonymous ()
144 WindowsIdentity id = null;
146 id = new WindowsIdentity ("nobody");
148 id._account = WindowsAccountType.Anonymous;
149 id._authenticated = false;
150 id._type = String.Empty;
153 id = new WindowsIdentity (IntPtr.Zero, String.Empty, WindowsAccountType.Anonymous, false);
154 // special case (don't try to resolve the name)
155 id._name = String.Empty;
160 public static WindowsIdentity GetCurrent ()
162 return new WindowsIdentity (GetCurrentToken (), null, WindowsAccountType.Normal, true);
165 [MonoTODO ("need icall changes")]
166 public static WindowsIdentity GetCurrent (bool ifImpersonating)
168 throw new NotImplementedException ();
171 [MonoTODO ("need icall changes")]
172 public static WindowsIdentity GetCurrent (TokenAccessLevels desiredAccess)
174 throw new NotImplementedException ();
179 public virtual WindowsImpersonationContext Impersonate ()
181 return new WindowsImpersonationContext (_token);
184 [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
185 public static WindowsImpersonationContext Impersonate (IntPtr userToken)
187 return new WindowsImpersonationContext (userToken);
192 public virtual string AuthenticationType
194 get { return _type; }
197 public virtual bool IsAnonymous
199 get { return (_account == WindowsAccountType.Anonymous); }
202 public virtual bool IsAuthenticated
204 get { return _authenticated; }
207 public virtual bool IsGuest
209 get { return (_account == WindowsAccountType.Guest); }
212 public virtual bool IsSystem
214 get { return (_account == WindowsAccountType.System); }
217 public virtual string Name
221 // revolve name (runtime)
222 _name = GetTokenName (_token);
228 public virtual IntPtr Token
230 get { return _token; }
233 [MonoTODO ("not implemented")]
234 public IdentityReferenceCollection Groups {
235 get { throw new NotImplementedException (); }
238 [MonoTODO ("not implemented")]
240 public TokenImpersonationLevel ImpersonationLevel {
241 get { throw new NotImplementedException (); }
244 [MonoTODO ("not implemented")]
246 public SecurityIdentifier Owner {
247 get { throw new NotImplementedException (); }
250 [MonoTODO ("not implemented")]
252 public SecurityIdentifier User {
253 get { throw new NotImplementedException (); }
256 void IDeserializationCallback.OnDeserialization (object sender)
258 _token = (IntPtr) _info.GetValue ("m_userToken", typeof (IntPtr));
259 // can't trust this alone - we must validate the token
260 _name = _info.GetString ("m_name");
262 // validate token by comparing names
263 string name = GetTokenName (_token);
265 throw new SerializationException ("Token-Name mismatch.");
268 // validate token by getting name
269 _name = GetTokenName (_token);
270 if ((_name == String.Empty) || (_name == null))
271 throw new SerializationException ("Token doesn't match a user.");
273 _type = _info.GetString ("m_type");
274 _account = (WindowsAccountType) _info.GetValue ("m_acctType", typeof (WindowsAccountType));
275 _authenticated = _info.GetBoolean ("m_isAuthenticated");
278 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
280 info.AddValue ("m_userToken", _token);
281 // can be null when not resolved
282 info.AddValue ("m_name", _name);
283 info.AddValue ("m_type", _type);
284 info.AddValue ("m_acctType", _account);
285 info.AddValue ("m_isAuthenticated", _authenticated);
288 private static bool IsPosix {
289 get { return ((int) Environment.Platform == 128); }
292 private void SetToken (IntPtr token)
295 if (token == invalidPosix)
296 throw new ArgumentException ("Invalid token");
302 // override user choice in this specific case
303 if (_token == IntPtr.Zero)
304 _account = WindowsAccountType.System;
307 if ((token == invalidWindows) && (_account != WindowsAccountType.Anonymous))
308 throw new ArgumentException ("Invalid token");
317 // see mono/mono/metadata/security.c for implementation
319 // Many people use reflection to get a user's roles - so many
320 // that's it's hard to say it's an "undocumented" feature -
321 // so we also implement it in Mono :-/
322 // http://www.dotnet247.com/247reference/msgs/39/195403.aspx
323 [MethodImplAttribute (MethodImplOptions.InternalCall)]
324 internal extern static string[] _GetRoles (IntPtr token);
326 [MethodImplAttribute (MethodImplOptions.InternalCall)]
327 internal extern static IntPtr GetCurrentToken ();
329 [MethodImplAttribute (MethodImplOptions.InternalCall)]
330 private extern static string GetTokenName (IntPtr token);
332 [MethodImplAttribute (MethodImplOptions.InternalCall)]
333 private extern static IntPtr GetUserToken (string username);