SmbComNTCreateAndX.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 SharpCifs.Util;
  18. namespace SharpCifs.Smb
  19. {
  20. internal class SmbComNtCreateAndX : AndXServerMessageBlock
  21. {
  22. internal const int FileSupersede = unchecked(0x0);
  23. internal const int FileOpen = unchecked(0x1);
  24. internal const int FileCreate = unchecked(0x2);
  25. internal const int FileOpenIf = unchecked(0x3);
  26. internal const int FileOverwrite = unchecked(0x4);
  27. internal const int FileOverwriteIf = unchecked(0x5);
  28. internal const int FileWriteThrough = unchecked(0x00000002);
  29. internal const int FileSequentialOnly = unchecked(0x00000004);
  30. internal const int FileSynchronousIoAlert = unchecked(0x00000010);
  31. internal const int FileSynchronousIoNonalert = unchecked(0x00000020);
  32. internal const int SecurityContextTracking = unchecked(0x01);
  33. internal const int SecurityEffectiveOnly = unchecked(0x02);
  34. private int _rootDirectoryFid;
  35. private int _extFileAttributes;
  36. private int _shareAccess;
  37. private int _createDisposition;
  38. private int _createOptions;
  39. private int _impersonationLevel;
  40. private long _allocationSize;
  41. private byte _securityFlags;
  42. private int _namelenIndex;
  43. internal int Flags0;
  44. internal int DesiredAccess;
  45. internal SmbComNtCreateAndX(string name, int flags, int access, int shareAccess,
  46. int extFileAttributes, int createOptions, ServerMessageBlock andx) : base(andx)
  47. {
  48. // share access specified in SmbFile
  49. // create disposition
  50. // create options
  51. // security flags
  52. Path = name;
  53. Command = SmbComNtCreateAndx;
  54. DesiredAccess = access;
  55. DesiredAccess |= SmbConstants.FileReadData | SmbConstants.FileReadEa | SmbConstants.FileReadAttributes;
  56. // extFileAttributes
  57. this._extFileAttributes = extFileAttributes;
  58. // shareAccess
  59. this._shareAccess = shareAccess;
  60. // createDisposition
  61. if ((flags & SmbFile.OTrunc) == SmbFile.OTrunc)
  62. {
  63. // truncate the file
  64. if ((flags & SmbFile.OCreat) == SmbFile.OCreat)
  65. {
  66. // create it if necessary
  67. _createDisposition = FileOverwriteIf;
  68. }
  69. else
  70. {
  71. _createDisposition = FileOverwrite;
  72. }
  73. }
  74. else
  75. {
  76. // don't truncate the file
  77. if ((flags & SmbFile.OCreat) == SmbFile.OCreat)
  78. {
  79. // create it if necessary
  80. if ((flags & SmbFile.OExcl) == SmbFile.OExcl)
  81. {
  82. // fail if already exists
  83. _createDisposition = FileCreate;
  84. }
  85. else
  86. {
  87. _createDisposition = FileOpenIf;
  88. }
  89. }
  90. else
  91. {
  92. _createDisposition = FileOpen;
  93. }
  94. }
  95. if ((createOptions & unchecked(0x0001)) == 0)
  96. {
  97. this._createOptions = createOptions | unchecked(0x0040);
  98. }
  99. else
  100. {
  101. this._createOptions = createOptions;
  102. }
  103. _impersonationLevel = unchecked(0x02);
  104. // As seen on NT :~)
  105. _securityFlags = unchecked(unchecked(0x03));
  106. }
  107. // SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY
  108. internal override int WriteParameterWordsWireFormat(byte[] dst, int dstIndex)
  109. {
  110. int start = dstIndex;
  111. dst[dstIndex++] = unchecked(unchecked(0x00));
  112. // name length without counting null termination
  113. _namelenIndex = dstIndex;
  114. dstIndex += 2;
  115. WriteInt4(Flags0, dst, dstIndex);
  116. dstIndex += 4;
  117. WriteInt4(_rootDirectoryFid, dst, dstIndex);
  118. dstIndex += 4;
  119. WriteInt4(DesiredAccess, dst, dstIndex);
  120. dstIndex += 4;
  121. WriteInt8(_allocationSize, dst, dstIndex);
  122. dstIndex += 8;
  123. WriteInt4(_extFileAttributes, dst, dstIndex);
  124. dstIndex += 4;
  125. WriteInt4(_shareAccess, dst, dstIndex);
  126. dstIndex += 4;
  127. WriteInt4(_createDisposition, dst, dstIndex);
  128. dstIndex += 4;
  129. WriteInt4(_createOptions, dst, dstIndex);
  130. dstIndex += 4;
  131. WriteInt4(_impersonationLevel, dst, dstIndex);
  132. dstIndex += 4;
  133. dst[dstIndex++] = _securityFlags;
  134. return dstIndex - start;
  135. }
  136. internal override int WriteBytesWireFormat(byte[] dst, int dstIndex)
  137. {
  138. int n;
  139. n = WriteString(Path, dst, dstIndex);
  140. WriteInt2((UseUnicode ? Path.Length * 2 : n), dst, _namelenIndex);
  141. return n;
  142. }
  143. internal override int ReadParameterWordsWireFormat(byte[] buffer, int bufferIndex
  144. )
  145. {
  146. return 0;
  147. }
  148. internal override int ReadBytesWireFormat(byte[] buffer, int bufferIndex)
  149. {
  150. return 0;
  151. }
  152. public override string ToString()
  153. {
  154. return "SmbComNTCreateAndX[" + base.ToString() + ",flags=0x" + Hexdump
  155. .ToHexString(Flags0, 2) + ",rootDirectoryFid=" + _rootDirectoryFid + ",desiredAccess=0x"
  156. + Hexdump.ToHexString(DesiredAccess, 4) + ",allocationSize=" + _allocationSize +
  157. ",extFileAttributes=0x" + Hexdump.ToHexString(_extFileAttributes, 4) + ",shareAccess=0x"
  158. + Hexdump.ToHexString(_shareAccess, 4) + ",createDisposition=0x" + Hexdump.ToHexString
  159. (_createDisposition, 4) + ",createOptions=0x" + Hexdump.ToHexString(_createOptions
  160. , 8) + ",impersonationLevel=0x" + Hexdump.ToHexString(_impersonationLevel, 4) + ",securityFlags=0x"
  161. + Hexdump.ToHexString(_securityFlags, 2) + ",name=" + Path + "]";
  162. }
  163. }
  164. }