SecurityDescriptor.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System.IO;
  18. namespace SharpCifs.Smb
  19. {
  20. public class SecurityDescriptor
  21. {
  22. public int Type;
  23. public Ace[] Aces;
  24. public SecurityDescriptor()
  25. {
  26. }
  27. /// <exception cref="System.IO.IOException"></exception>
  28. public SecurityDescriptor(byte[] buffer, int bufferIndex, int len)
  29. {
  30. Decode(buffer, bufferIndex, len);
  31. }
  32. /// <exception cref="System.IO.IOException"></exception>
  33. public virtual int Decode(byte[] buffer, int bufferIndex, int len)
  34. {
  35. int start = bufferIndex;
  36. bufferIndex++;
  37. // revision
  38. bufferIndex++;
  39. Type = ServerMessageBlock.ReadInt2(buffer, bufferIndex);
  40. bufferIndex += 2;
  41. ServerMessageBlock.ReadInt4(buffer, bufferIndex);
  42. // offset to owner sid
  43. bufferIndex += 4;
  44. ServerMessageBlock.ReadInt4(buffer, bufferIndex);
  45. // offset to group sid
  46. bufferIndex += 4;
  47. ServerMessageBlock.ReadInt4(buffer, bufferIndex);
  48. // offset to sacl
  49. bufferIndex += 4;
  50. int daclOffset = ServerMessageBlock.ReadInt4(buffer, bufferIndex);
  51. bufferIndex = start + daclOffset;
  52. bufferIndex++;
  53. // revision
  54. bufferIndex++;
  55. int size = ServerMessageBlock.ReadInt2(buffer, bufferIndex);
  56. bufferIndex += 2;
  57. int numAces = ServerMessageBlock.ReadInt4(buffer, bufferIndex);
  58. bufferIndex += 4;
  59. if (numAces > 4096)
  60. {
  61. throw new IOException("Invalid SecurityDescriptor");
  62. }
  63. if (daclOffset != 0)
  64. {
  65. Aces = new Ace[numAces];
  66. for (int i = 0; i < numAces; i++)
  67. {
  68. Aces[i] = new Ace();
  69. bufferIndex += Aces[i].Decode(buffer, bufferIndex);
  70. }
  71. }
  72. else
  73. {
  74. Aces = null;
  75. }
  76. return bufferIndex - start;
  77. }
  78. public override string ToString()
  79. {
  80. string ret = "SecurityDescriptor:\n";
  81. if (Aces != null)
  82. {
  83. for (int ai = 0; ai < Aces.Length; ai++)
  84. {
  85. ret += Aces[ai] + "\n";
  86. }
  87. }
  88. else
  89. {
  90. ret += "NULL";
  91. }
  92. return ret;
  93. }
  94. }
  95. }