2
0

CommonAce.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // System.Security.AccessControl.CommonAce implementation
  3. //
  4. // Authors:
  5. // Dick Porter <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. // Kenneth Bell
  8. //
  9. // Copyright (C) 2006-2007 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Globalization;
  31. using System.Security.Principal;
  32. namespace System.Security.AccessControl {
  33. public sealed class CommonAce : QualifiedAce {
  34. public CommonAce (AceFlags flags, AceQualifier qualifier,
  35. int accessMask, SecurityIdentifier sid,
  36. bool isCallback, byte[] opaque)
  37. : base(ConvertType (qualifier, isCallback),
  38. flags,
  39. opaque)
  40. {
  41. AccessMask = accessMask;
  42. SecurityIdentifier = sid;
  43. }
  44. internal CommonAce (AceType type, AceFlags flags, int accessMask,
  45. SecurityIdentifier sid, byte[] opaque)
  46. : base(type, flags, opaque)
  47. {
  48. AccessMask = accessMask;
  49. SecurityIdentifier = sid;
  50. }
  51. internal CommonAce(byte[] binaryForm, int offset)
  52. : base(binaryForm, offset)
  53. {
  54. int len = ReadUShort(binaryForm, offset + 2);
  55. if (offset > binaryForm.Length - len)
  56. throw new ArgumentException("Invalid ACE - truncated", "binaryForm");
  57. if (len < 8 + SecurityIdentifier.MinBinaryLength)
  58. throw new ArgumentException("Invalid ACE", "binaryForm");
  59. AccessMask = ReadInt(binaryForm, offset + 4);
  60. SecurityIdentifier = new SecurityIdentifier(binaryForm,
  61. offset + 8);
  62. int opaqueLen = len - (8 + SecurityIdentifier.BinaryLength);
  63. if (opaqueLen > 0) {
  64. byte[] opaque = new byte[opaqueLen];
  65. Array.Copy(binaryForm,
  66. offset + 8 + SecurityIdentifier.BinaryLength,
  67. opaque, 0, opaqueLen);
  68. SetOpaque (opaque);
  69. }
  70. }
  71. public override int BinaryLength {
  72. get {
  73. return 8 + SecurityIdentifier.BinaryLength
  74. + OpaqueLength;
  75. }
  76. }
  77. public override void GetBinaryForm (byte[] binaryForm,
  78. int offset)
  79. {
  80. int len = BinaryLength;
  81. binaryForm[offset] = (byte)this.AceType;
  82. binaryForm[offset + 1] = (byte)this.AceFlags;
  83. WriteUShort ((ushort)len, binaryForm, offset + 2);
  84. WriteInt (AccessMask, binaryForm, offset + 4);
  85. SecurityIdentifier.GetBinaryForm (binaryForm,
  86. offset + 8);
  87. byte[] opaque = GetOpaque ();
  88. if (opaque != null)
  89. Array.Copy (opaque, 0, binaryForm,
  90. offset + 8 + SecurityIdentifier.BinaryLength,
  91. opaque.Length);
  92. }
  93. public static int MaxOpaqueLength (bool isCallback)
  94. {
  95. // Varies by platform?
  96. return 65459;
  97. }
  98. internal override string GetSddlForm ()
  99. {
  100. if (OpaqueLength != 0)
  101. throw new NotImplementedException (
  102. "Unable to convert conditional ACEs to SDDL");
  103. return string.Format (CultureInfo.InvariantCulture,
  104. "({0};{1};{2};;;{3})",
  105. GetSddlAceType (AceType),
  106. GetSddlAceFlags (AceFlags),
  107. GetSddlAccessRights (AccessMask),
  108. SecurityIdentifier.GetSddlForm ());
  109. }
  110. private static AceType ConvertType (AceQualifier qualifier,
  111. bool isCallback)
  112. {
  113. switch (qualifier) {
  114. case AceQualifier.AccessAllowed:
  115. if (isCallback)
  116. return AceType.AccessAllowedCallback;
  117. else
  118. return AceType.AccessAllowed;
  119. case AceQualifier.AccessDenied:
  120. if (isCallback)
  121. return AceType.AccessDeniedCallback;
  122. else
  123. return AceType.AccessDenied;
  124. case AceQualifier.SystemAlarm:
  125. if (isCallback)
  126. return AceType.SystemAlarmCallback;
  127. else
  128. return AceType.SystemAlarm;
  129. case AceQualifier.SystemAudit:
  130. if (isCallback)
  131. return AceType.SystemAuditCallback;
  132. else
  133. return AceType.SystemAudit;
  134. default:
  135. throw new ArgumentException ("Unrecognized ACE qualifier: " + qualifier, "qualifier");
  136. }
  137. }
  138. }
  139. }