SmbComNTCreateAndX.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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,
  46. int flags,
  47. int access,
  48. int shareAccess,
  49. int extFileAttributes,
  50. int createOptions,
  51. ServerMessageBlock andx) : base(andx)
  52. {
  53. // share access specified in SmbFile
  54. // create disposition
  55. // create options
  56. // security flags
  57. Path = name;
  58. Command = SmbComNtCreateAndx;
  59. DesiredAccess = access;
  60. DesiredAccess |= SmbConstants.FileReadData
  61. | SmbConstants.FileReadEa
  62. | SmbConstants.FileReadAttributes;
  63. // extFileAttributes
  64. this._extFileAttributes = extFileAttributes;
  65. // shareAccess
  66. this._shareAccess = shareAccess;
  67. // createDisposition
  68. if ((flags & SmbFile.OTrunc) == SmbFile.OTrunc)
  69. {
  70. // truncate the file
  71. if ((flags & SmbFile.OCreat) == SmbFile.OCreat)
  72. {
  73. // create it if necessary
  74. _createDisposition = FileOverwriteIf;
  75. }
  76. else
  77. {
  78. _createDisposition = FileOverwrite;
  79. }
  80. }
  81. else
  82. {
  83. // don't truncate the file
  84. if ((flags & SmbFile.OCreat) == SmbFile.OCreat)
  85. {
  86. // create it if necessary
  87. if ((flags & SmbFile.OExcl) == SmbFile.OExcl)
  88. {
  89. // fail if already exists
  90. _createDisposition = FileCreate;
  91. }
  92. else
  93. {
  94. _createDisposition = FileOpenIf;
  95. }
  96. }
  97. else
  98. {
  99. _createDisposition = FileOpen;
  100. }
  101. }
  102. if ((createOptions & unchecked(0x0001)) == 0)
  103. {
  104. this._createOptions = createOptions | unchecked(0x0040);
  105. }
  106. else
  107. {
  108. this._createOptions = createOptions;
  109. }
  110. _impersonationLevel = unchecked(0x02);
  111. // As seen on NT :~)
  112. _securityFlags = unchecked(unchecked(0x03));
  113. }
  114. // SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY
  115. internal override int WriteParameterWordsWireFormat(byte[] dst, int dstIndex)
  116. {
  117. int start = dstIndex;
  118. dst[dstIndex++] = unchecked(unchecked(0x00));
  119. // name length without counting null termination
  120. _namelenIndex = dstIndex;
  121. dstIndex += 2;
  122. WriteInt4(Flags0, dst, dstIndex);
  123. dstIndex += 4;
  124. WriteInt4(_rootDirectoryFid, dst, dstIndex);
  125. dstIndex += 4;
  126. WriteInt4(DesiredAccess, dst, dstIndex);
  127. dstIndex += 4;
  128. WriteInt8(_allocationSize, dst, dstIndex);
  129. dstIndex += 8;
  130. WriteInt4(_extFileAttributes, dst, dstIndex);
  131. dstIndex += 4;
  132. WriteInt4(_shareAccess, dst, dstIndex);
  133. dstIndex += 4;
  134. WriteInt4(_createDisposition, dst, dstIndex);
  135. dstIndex += 4;
  136. WriteInt4(_createOptions, dst, dstIndex);
  137. dstIndex += 4;
  138. WriteInt4(_impersonationLevel, dst, dstIndex);
  139. dstIndex += 4;
  140. dst[dstIndex++] = _securityFlags;
  141. return dstIndex - start;
  142. }
  143. internal override int WriteBytesWireFormat(byte[] dst, int dstIndex)
  144. {
  145. int n;
  146. n = WriteString(Path, dst, dstIndex);
  147. WriteInt2((UseUnicode ? Path.Length * 2 : n), dst, _namelenIndex);
  148. return n;
  149. }
  150. internal override int ReadParameterWordsWireFormat(byte[] buffer, int bufferIndex)
  151. {
  152. return 0;
  153. }
  154. internal override int ReadBytesWireFormat(byte[] buffer, int bufferIndex)
  155. {
  156. return 0;
  157. }
  158. public override string ToString()
  159. {
  160. return "SmbComNTCreateAndX["
  161. + base.ToString()
  162. + ",flags=0x" + Hexdump.ToHexString(Flags0, 2)
  163. + ",rootDirectoryFid=" + _rootDirectoryFid
  164. + ",desiredAccess=0x" + Hexdump.ToHexString(DesiredAccess, 4)
  165. + ",allocationSize=" + _allocationSize
  166. + ",extFileAttributes=0x" + Hexdump.ToHexString(_extFileAttributes, 4)
  167. + ",shareAccess=0x" + Hexdump.ToHexString(_shareAccess, 4)
  168. + ",createDisposition=0x" + Hexdump.ToHexString(_createDisposition, 4)
  169. + ",createOptions=0x" + Hexdump.ToHexString(_createOptions, 8)
  170. + ",impersonationLevel=0x" + Hexdump.ToHexString(_impersonationLevel, 4)
  171. + ",securityFlags=0x" + Hexdump.ToHexString(_securityFlags, 2)
  172. + ",name=" + Path + "]";
  173. }
  174. }
  175. }