2 // WindowsPrincipal.cs: Windows IPrincipal implementation
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
12 using System.Collections;
13 using System.Runtime.CompilerServices;
15 namespace System.Security.Principal {
18 public class WindowsPrincipal : IPrincipal {
20 private WindowsIdentity _identity;
21 // http://groups.google.ca/groups?q=WindowsPrincipal+m_roles&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OghXf4OgCHA.4228%40tkmsftngp08&rnum=4
22 private string [] m_roles;
24 // case sensitivity versus number of groups
25 // http://groups.google.ca/groups?q=WindowsPrincipal+m_roles&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=%23JEMHsMQCHA.1916%40tkmsftngp13&rnum=5
27 public WindowsPrincipal (WindowsIdentity ntIdentity)
29 if (ntIdentity == null)
30 throw new ArgumentNullException ("ntIdentity");
32 _identity = ntIdentity;
37 public virtual IIdentity Identity {
38 get { return _identity; }
43 public virtual bool IsInRole (int rid)
46 return IsMemberOfGroupId (Token, (IntPtr) rid);
51 case 544: // Administrator
52 role = "BUILTIN\\Administrators";
55 role = "BUILTIN\\Users";
58 role = "BUILTIN\\Guests";
60 case 547: // PowerUser
61 role = "BUILTIN\\Power Users";
63 case 548: // AccountOperator
64 role = "BUILTIN\\Account Operators";
66 case 549: // SystemOperator
67 role = "BUILTIN\\System Operators";
69 case 550: // PrintOperator
70 role = "BUILTIN\\Print Operators";
72 case 551: // BackupOperator
73 role = "BUILTIN\\Backup Operators";
75 case 552: // Replicator
76 role = "BUILTIN\\Replicator";
81 return IsInRole (role);
85 public virtual bool IsInRole (string role)
88 return false; // ArgumentNullException
91 // note: Posix is always case-sensitive
92 return IsMemberOfGroupName (Token, role);
95 // Windows specific code that
96 // (a) build the role cache like the MS framework (for compatibility)
97 // (b) case sensitive (for Fx 1.0) and case insensitive (later Fx)
98 if (m_roles == null) {
99 m_roles = WindowsIdentity._GetRoles (Token);
102 role = role.ToUpperInvariant ();
104 foreach (string check in m_roles) {
109 Console.WriteLine ("> {0}", check);
110 if ((check != null) && (role == check.ToUpperInvariant ()))
118 public virtual bool IsInRole (WindowsBuiltInRole role)
121 // right now we only map Administrator == root
124 case WindowsBuiltInRole.Administrator:
130 return IsInRole (group);
133 return IsInRole ((int) role);
137 private static bool IsPosix {
138 get { return ((int) Environment.Platform == 128); }
141 private IntPtr Token {
142 get { return (_identity as WindowsIdentity).Token; }
145 // see mono/mono/metadata/security.c for implementation
147 // note: never called by Win32 code (i.e. always return false)
148 [MethodImplAttribute (MethodImplOptions.InternalCall)]
149 private extern static bool IsMemberOfGroupId (IntPtr user, IntPtr group);
151 // note: never called by Win32 code (i.e. always return false)
152 [MethodImplAttribute (MethodImplOptions.InternalCall)]
153 private extern static bool IsMemberOfGroupName (IntPtr user, string group);