SmbComTreeConnectAndX.cs 8.3 KB

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