2
0

SmbComTreeConnectAndX.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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;
  18. using SharpCifs.Util;
  19. using SharpCifs.Util.Sharpen;
  20. namespace SharpCifs.Smb
  21. {
  22. internal class SmbComTreeConnectAndX : AndXServerMessageBlock
  23. {
  24. private static readonly bool DisablePlainTextPasswords = Config.GetBoolean("jcifs.smb.client.disablePlainTextPasswords"
  25. , true);
  26. private SmbSession _session;
  27. private bool _disconnectTid = false;
  28. private string _service;
  29. private byte[] _password;
  30. private int _passwordLength;
  31. internal string path;
  32. private static byte[] _batchLimits = { 1, 1, 1, 1, 1, 1, 1, 1, 0 };
  33. static SmbComTreeConnectAndX()
  34. {
  35. string s;
  36. if ((s = Config.GetProperty("jcifs.smb.client.TreeConnectAndX.CheckDirectory")) !=
  37. null)
  38. {
  39. _batchLimits[0] = byte.Parse(s);
  40. }
  41. if ((s = Config.GetProperty("jcifs.smb.client.TreeConnectAndX.CreateDirectory"))
  42. != null)
  43. {
  44. _batchLimits[2] = byte.Parse(s);
  45. }
  46. if ((s = Config.GetProperty("jcifs.smb.client.TreeConnectAndX.Delete")) != null)
  47. {
  48. _batchLimits[3] = byte.Parse(s);
  49. }
  50. if ((s = Config.GetProperty("jcifs.smb.client.TreeConnectAndX.DeleteDirectory"))
  51. != null)
  52. {
  53. _batchLimits[4] = byte.Parse(s);
  54. }
  55. if ((s = Config.GetProperty("jcifs.smb.client.TreeConnectAndX.OpenAndX")) != null)
  56. {
  57. _batchLimits[5] = byte.Parse(s);
  58. }
  59. if ((s = Config.GetProperty("jcifs.smb.client.TreeConnectAndX.Rename")) != null)
  60. {
  61. _batchLimits[6] = byte.Parse(s);
  62. }
  63. if ((s = Config.GetProperty("jcifs.smb.client.TreeConnectAndX.Transaction")) != null)
  64. {
  65. _batchLimits[7] = byte.Parse(s);
  66. }
  67. if ((s = Config.GetProperty("jcifs.smb.client.TreeConnectAndX.QueryInformation"))
  68. != null)
  69. {
  70. _batchLimits[8] = byte.Parse(s);
  71. }
  72. }
  73. internal SmbComTreeConnectAndX(SmbSession session, string path, string service, ServerMessageBlock
  74. andx) : base(andx)
  75. {
  76. this._session = session;
  77. this.path = path;
  78. this._service = service;
  79. Command = SmbComTreeConnectAndx;
  80. }
  81. internal override int GetBatchLimit(byte command)
  82. {
  83. int c = command & unchecked(0xFF);
  84. switch (c)
  85. {
  86. case SmbComCheckDirectory:
  87. {
  88. // why isn't this just return batchLimits[c]?
  89. return _batchLimits[0];
  90. }
  91. case SmbComCreateDirectory:
  92. {
  93. return _batchLimits[2];
  94. }
  95. case SmbComDelete:
  96. {
  97. return _batchLimits[3];
  98. }
  99. case SmbComDeleteDirectory:
  100. {
  101. return _batchLimits[4];
  102. }
  103. case SmbComOpenAndx:
  104. {
  105. return _batchLimits[5];
  106. }
  107. case SmbComRename:
  108. {
  109. return _batchLimits[6];
  110. }
  111. case SmbComTransaction:
  112. {
  113. return _batchLimits[7];
  114. }
  115. case SmbComQueryInformation:
  116. {
  117. return _batchLimits[8];
  118. }
  119. }
  120. return 0;
  121. }
  122. internal override int WriteParameterWordsWireFormat(byte[] dst, int dstIndex)
  123. {
  124. if (_session.transport.Server.Security == SmbConstants.SecurityShare && (_session.Auth.HashesExternal
  125. || _session.Auth.Password.Length > 0))
  126. {
  127. if (_session.transport.Server.EncryptedPasswords)
  128. {
  129. // encrypted
  130. _password = _session.Auth.GetAnsiHash(_session.transport.Server.EncryptionKey);
  131. _passwordLength = _password.Length;
  132. }
  133. else
  134. {
  135. if (DisablePlainTextPasswords)
  136. {
  137. throw new RuntimeException("Plain text passwords are disabled");
  138. }
  139. // plain text
  140. _password = new byte[(_session.Auth.Password.Length + 1) * 2];
  141. _passwordLength = WriteString(_session.Auth.Password, _password, 0);
  142. }
  143. }
  144. else
  145. {
  146. // no password in tree connect
  147. _passwordLength = 1;
  148. }
  149. dst[dstIndex++] = _disconnectTid ? unchecked((byte)unchecked(0x01)) : unchecked(
  150. (byte)unchecked(0x00));
  151. dst[dstIndex++] = unchecked(unchecked(0x00));
  152. WriteInt2(_passwordLength, dst, dstIndex);
  153. return 4;
  154. }
  155. internal override int WriteBytesWireFormat(byte[] dst, int dstIndex)
  156. {
  157. int start = dstIndex;
  158. if (_session.transport.Server.Security == SmbConstants.SecurityShare && (_session.Auth.HashesExternal
  159. || _session.Auth.Password.Length > 0))
  160. {
  161. Array.Copy(_password, 0, dst, dstIndex, _passwordLength);
  162. dstIndex += _passwordLength;
  163. }
  164. else
  165. {
  166. // no password in tree connect
  167. dst[dstIndex++] = unchecked(unchecked(0x00));
  168. }
  169. dstIndex += WriteString(path, dst, dstIndex);
  170. try
  171. {
  172. // Array.Copy(Runtime.GetBytesForString(_service, "ASCII"), 0, dst, dstIndex
  173. //, _service.Length);
  174. Array.Copy(Runtime.GetBytesForString(_service, "UTF-8"), 0, dst, dstIndex
  175. , _service.Length);
  176. }
  177. catch (UnsupportedEncodingException)
  178. {
  179. return 0;
  180. }
  181. dstIndex += _service.Length;
  182. dst[dstIndex++] = unchecked((byte)('\0'));
  183. return dstIndex - start;
  184. }
  185. internal override int ReadParameterWordsWireFormat(byte[] buffer, int bufferIndex
  186. )
  187. {
  188. return 0;
  189. }
  190. internal override int ReadBytesWireFormat(byte[] buffer, int bufferIndex)
  191. {
  192. return 0;
  193. }
  194. public override string ToString()
  195. {
  196. string result = "SmbComTreeConnectAndX[" + base.ToString() + ",disconnectTid="
  197. + _disconnectTid + ",passwordLength=" + _passwordLength + ",password=" + Hexdump.
  198. ToHexString(_password, _passwordLength, 0) + ",path=" + path + ",service=" + _service
  199. + "]";
  200. return result;
  201. }
  202. }
  203. }