[Test] Avoid MethodInfoTest.GetMethodBody failure when executed on a release (IL...
[mono.git] / mcs / class / corlib / System.Security.Principal / WindowsPrincipal.cs
1 //
2 // WindowsPrincipal.cs: Windows IPrincipal implementation
3 //
4 // Author:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.Runtime.CompilerServices;
32 using System.Runtime.InteropServices;
33
34 namespace System.Security.Principal {
35
36         [Serializable]
37         [ComVisible (true)]
38         public class WindowsPrincipal : IPrincipal {
39
40                 private WindowsIdentity _identity;
41                 // http://groups.google.ca/groups?q=WindowsPrincipal+m_roles&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OghXf4OgCHA.4228%40tkmsftngp08&rnum=4
42                 private string [] m_roles;
43
44                 // case sensitivity versus number of groups
45                 // http://groups.google.ca/groups?q=WindowsPrincipal+m_roles&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=%23JEMHsMQCHA.1916%40tkmsftngp13&rnum=5
46
47                 public WindowsPrincipal (WindowsIdentity ntIdentity)
48                 {
49                         if (ntIdentity == null)
50                                 throw new ArgumentNullException ("ntIdentity");
51
52                         _identity = ntIdentity;
53                 }
54
55                 // properties
56
57                 public virtual IIdentity Identity {
58                         get { return _identity; }
59                 }
60
61                 // methods
62
63                 public virtual bool IsInRole (int rid) 
64                 {
65                         if (Environment.IsUnix) {
66                                 return IsMemberOfGroupId (Token, (IntPtr) rid);
67                         }
68                         else {
69                                 string role = null;
70                                 switch (rid) {
71                                         case 544: // Administrator
72                                                 role = "BUILTIN\\Administrators";
73                                                 break;
74                                         case 545: // User
75                                                 role = "BUILTIN\\Users";
76                                                 break;
77                                         case 546: // Guest
78                                                 role = "BUILTIN\\Guests";
79                                                 break;
80                                         case 547: // PowerUser
81                                                 role = "BUILTIN\\Power Users";
82                                                 break;
83                                         case 548: // AccountOperator
84                                                 role = "BUILTIN\\Account Operators";
85                                                 break;
86                                         case 549: // SystemOperator
87                                                 role = "BUILTIN\\System Operators";
88                                                 break;
89                                         case 550: // PrintOperator
90                                                 role = "BUILTIN\\Print Operators";
91                                                 break;
92                                         case 551: // BackupOperator
93                                                 role = "BUILTIN\\Backup Operators";
94                                                 break;
95                                         case 552: // Replicator
96                                                 role = "BUILTIN\\Replicator";
97                                                 break;
98                                         default:
99                                                 return false;
100                                 }
101                                 return IsInRole (role);
102                         }
103                 }
104
105                 public virtual bool IsInRole (string role)
106                 {
107                         if (role == null)
108                                 return false;   // ArgumentNullException
109
110                         if (Environment.IsUnix) {
111                                 // note: Posix is always case-sensitive
112                                 return IsMemberOfGroupName (Token, role);
113                         }
114                         else {
115                                 // Windows specific code that
116                                 // (a) build the role cache like the MS framework (for compatibility)
117                                 // (b) case sensitive (for Fx 1.0) and case insensitive (later Fx)
118                                 if (m_roles == null) {
119                                         m_roles = WindowsIdentity._GetRoles (Token);
120                                 }
121                                 
122                                 role = role.ToUpperInvariant ();
123                                 foreach (string check in m_roles) {
124                                         if ((check != null) && (role == check.ToUpperInvariant ()))
125                                                 return true;
126                                 }
127                                 return false;
128                         }
129                 }
130
131                 public virtual bool IsInRole (WindowsBuiltInRole role)
132                 {
133                         if (Environment.IsUnix) {
134                                 // right now we only map Administrator == root
135                                 string group = null;
136                                 switch (role) {
137                                         case WindowsBuiltInRole.Administrator:
138                                                 group = "root";
139                                                 break;
140                                         default:
141                                                 return false;
142                                 }
143                                 return IsInRole (group);
144                         }
145                         else {
146                                 return IsInRole ((int) role);
147                         }
148                 }
149                 [MonoTODO ("not implemented")]
150                 [ComVisible (false)]
151                 public virtual bool IsInRole (SecurityIdentifier sid)
152                 {
153                         throw new NotImplementedException ();
154                 }
155
156                 private IntPtr Token {
157                         get { return (_identity as WindowsIdentity).Token; }
158                 }
159
160                 // see mono/mono/metadata/security.c for implementation
161
162                 // note: never called by Win32 code (i.e. always return false)
163                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
164                 private extern static bool IsMemberOfGroupId (IntPtr user, IntPtr group);
165
166                 // note: never called by Win32 code (i.e. always return false)
167                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
168                 private extern static bool IsMemberOfGroupName (IntPtr user, string group);
169         }
170 }