2
0

WindowsPrincipal.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // WindowsPrincipal.cs: Windows IPrincipal implementation
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  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. using System.Collections;
  30. using System.Runtime.CompilerServices;
  31. using System.Runtime.InteropServices;
  32. namespace System.Security.Principal {
  33. [Serializable]
  34. #if NET_2_0
  35. [ComVisible (true)]
  36. #endif
  37. public class WindowsPrincipal : IPrincipal {
  38. private WindowsIdentity _identity;
  39. // http://groups.google.ca/groups?q=WindowsPrincipal+m_roles&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OghXf4OgCHA.4228%40tkmsftngp08&rnum=4
  40. private string [] m_roles;
  41. // case sensitivity versus number of groups
  42. // http://groups.google.ca/groups?q=WindowsPrincipal+m_roles&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=%23JEMHsMQCHA.1916%40tkmsftngp13&rnum=5
  43. public WindowsPrincipal (WindowsIdentity ntIdentity)
  44. {
  45. if (ntIdentity == null)
  46. throw new ArgumentNullException ("ntIdentity");
  47. _identity = ntIdentity;
  48. }
  49. // properties
  50. public virtual IIdentity Identity {
  51. get { return _identity; }
  52. }
  53. // methods
  54. public virtual bool IsInRole (int rid)
  55. {
  56. if (IsPosix) {
  57. return IsMemberOfGroupId (Token, (IntPtr) rid);
  58. }
  59. else {
  60. string role = null;
  61. switch (rid) {
  62. case 544: // Administrator
  63. role = "BUILTIN\\Administrators";
  64. break;
  65. case 545: // User
  66. role = "BUILTIN\\Users";
  67. break;
  68. case 546: // Guest
  69. role = "BUILTIN\\Guests";
  70. break;
  71. case 547: // PowerUser
  72. role = "BUILTIN\\Power Users";
  73. break;
  74. case 548: // AccountOperator
  75. role = "BUILTIN\\Account Operators";
  76. break;
  77. case 549: // SystemOperator
  78. role = "BUILTIN\\System Operators";
  79. break;
  80. case 550: // PrintOperator
  81. role = "BUILTIN\\Print Operators";
  82. break;
  83. case 551: // BackupOperator
  84. role = "BUILTIN\\Backup Operators";
  85. break;
  86. case 552: // Replicator
  87. role = "BUILTIN\\Replicator";
  88. break;
  89. default:
  90. return false;
  91. }
  92. return IsInRole (role);
  93. }
  94. }
  95. public virtual bool IsInRole (string role)
  96. {
  97. if (role == null)
  98. return false; // ArgumentNullException
  99. if (IsPosix) {
  100. // note: Posix is always case-sensitive
  101. return IsMemberOfGroupName (Token, role);
  102. }
  103. else {
  104. // Windows specific code that
  105. // (a) build the role cache like the MS framework (for compatibility)
  106. // (b) case sensitive (for Fx 1.0) and case insensitive (later Fx)
  107. if (m_roles == null) {
  108. m_roles = WindowsIdentity._GetRoles (Token);
  109. }
  110. #if !NET_1_0
  111. role = role.ToUpperInvariant ();
  112. #endif
  113. foreach (string check in m_roles) {
  114. #if NET_1_0
  115. if (role == check)
  116. return true;
  117. #else
  118. if ((check != null) && (role == check.ToUpperInvariant ()))
  119. return true;
  120. #endif
  121. }
  122. return false;
  123. }
  124. }
  125. public virtual bool IsInRole (WindowsBuiltInRole role)
  126. {
  127. if (IsPosix) {
  128. // right now we only map Administrator == root
  129. string group = null;
  130. switch (role) {
  131. case WindowsBuiltInRole.Administrator:
  132. group = "root";
  133. break;
  134. default:
  135. return false;
  136. }
  137. return IsInRole (group);
  138. }
  139. else {
  140. return IsInRole ((int) role);
  141. }
  142. }
  143. #if NET_2_0
  144. [MonoTODO ("not implemented")]
  145. [ComVisible (false)]
  146. public virtual bool IsInRole (SecurityIdentifier sid)
  147. {
  148. throw new NotImplementedException ();
  149. }
  150. #endif
  151. private static bool IsPosix {
  152. get { return ((int) Environment.Platform == 128); }
  153. }
  154. private IntPtr Token {
  155. get { return (_identity as WindowsIdentity).Token; }
  156. }
  157. // see mono/mono/metadata/security.c for implementation
  158. // note: never called by Win32 code (i.e. always return false)
  159. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  160. private extern static bool IsMemberOfGroupId (IntPtr user, IntPtr group);
  161. // note: never called by Win32 code (i.e. always return false)
  162. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  163. private extern static bool IsMemberOfGroupName (IntPtr user, string group);
  164. }
  165. }