SmbComSessionSetupAndX.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. //
  3. // This library is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU Lesser General Public
  5. // License as published by the Free Software Foundation; either
  6. // version 2.1 of the License, or (at your option) any later version.
  7. //
  8. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // Lesser General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU Lesser General Public
  14. // License along with this library; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. //
  17. // Ported to C# by J. Arturo <webmaster at komodosoft.net>
  18. using System;
  19. using SharpCifs.Util.Sharpen;
  20. namespace SharpCifs.Smb
  21. {
  22. internal class SmbComSessionSetupAndX : AndXServerMessageBlock
  23. {
  24. private static readonly int BatchLimit = Config.GetInt("jcifs.smb.client.SessionSetupAndX.TreeConnectAndX"
  25. , 1);
  26. private static readonly bool DisablePlainTextPasswords = Config.GetBoolean("jcifs.smb.client.disablePlainTextPasswords"
  27. , true);
  28. private byte[] _lmHash;
  29. private byte[] _ntHash;
  30. private byte[] _blob;
  31. private int _sessionKey;
  32. private int _capabilities;
  33. private string _accountName;
  34. private string _primaryDomain;
  35. internal SmbSession Session;
  36. internal object Cred;
  37. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  38. internal SmbComSessionSetupAndX(SmbSession session, ServerMessageBlock andx, object
  39. cred) : base(andx)
  40. {
  41. Command = SmbComSessionSetupAndx;
  42. this.Session = session;
  43. this.Cred = cred;
  44. _sessionKey = session.transport.SessionKey;
  45. _capabilities = session.transport.Capabilities;
  46. if (session.transport.Server.Security == SmbConstants.SecurityUser)
  47. {
  48. if (cred is NtlmPasswordAuthentication)
  49. {
  50. NtlmPasswordAuthentication auth = (NtlmPasswordAuthentication)cred;
  51. if (auth == NtlmPasswordAuthentication.Anonymous)
  52. {
  53. _lmHash = new byte[0];
  54. _ntHash = new byte[0];
  55. _capabilities &= ~SmbConstants.CapExtendedSecurity;
  56. }
  57. else
  58. {
  59. if (session.transport.Server.EncryptedPasswords)
  60. {
  61. _lmHash = auth.GetAnsiHash(session.transport.Server.EncryptionKey);
  62. _ntHash = auth.GetUnicodeHash(session.transport.Server.EncryptionKey);
  63. // prohibit HTTP auth attempts for the null session
  64. if (_lmHash.Length == 0 && _ntHash.Length == 0)
  65. {
  66. throw new RuntimeException("Null setup prohibited.");
  67. }
  68. }
  69. else
  70. {
  71. if (DisablePlainTextPasswords)
  72. {
  73. throw new RuntimeException("Plain text passwords are disabled");
  74. }
  75. if (UseUnicode)
  76. {
  77. // plain text
  78. string password = auth.GetPassword();
  79. _lmHash = new byte[0];
  80. _ntHash = new byte[(password.Length + 1) * 2];
  81. WriteString(password, _ntHash, 0);
  82. }
  83. else
  84. {
  85. // plain text
  86. string password = auth.GetPassword();
  87. _lmHash = new byte[(password.Length + 1) * 2];
  88. _ntHash = new byte[0];
  89. WriteString(password, _lmHash, 0);
  90. }
  91. }
  92. }
  93. _accountName = auth.Username;
  94. if (UseUnicode)
  95. {
  96. _accountName = _accountName.ToUpper();
  97. }
  98. _primaryDomain = auth.Domain.ToUpper();
  99. }
  100. else
  101. {
  102. if (cred is byte[])
  103. {
  104. _blob = (byte[])cred;
  105. }
  106. else
  107. {
  108. throw new SmbException("Unsupported credential type");
  109. }
  110. }
  111. }
  112. else
  113. {
  114. if (session.transport.Server.Security == SmbConstants.SecurityShare)
  115. {
  116. if (cred is NtlmPasswordAuthentication)
  117. {
  118. NtlmPasswordAuthentication auth = (NtlmPasswordAuthentication)cred;
  119. _lmHash = new byte[0];
  120. _ntHash = new byte[0];
  121. _accountName = auth.Username;
  122. if (UseUnicode)
  123. {
  124. _accountName = _accountName.ToUpper();
  125. }
  126. _primaryDomain = auth.Domain.ToUpper();
  127. }
  128. else
  129. {
  130. throw new SmbException("Unsupported credential type");
  131. }
  132. }
  133. else
  134. {
  135. throw new SmbException("Unsupported");
  136. }
  137. }
  138. }
  139. internal override int GetBatchLimit(byte command)
  140. {
  141. return command == SmbComTreeConnectAndx ? BatchLimit : 0;
  142. }
  143. internal override int WriteParameterWordsWireFormat(byte[] dst, int dstIndex)
  144. {
  145. int start = dstIndex;
  146. WriteInt2(Session.transport.SndBufSize, dst, dstIndex);
  147. dstIndex += 2;
  148. WriteInt2(Session.transport.MaxMpxCount, dst, dstIndex);
  149. dstIndex += 2;
  150. WriteInt2(SmbConstants.VcNumber, dst, dstIndex);
  151. dstIndex += 2;
  152. WriteInt4(_sessionKey, dst, dstIndex);
  153. dstIndex += 4;
  154. if (_blob != null)
  155. {
  156. WriteInt2(_blob.Length, dst, dstIndex);
  157. dstIndex += 2;
  158. }
  159. else
  160. {
  161. WriteInt2(_lmHash.Length, dst, dstIndex);
  162. dstIndex += 2;
  163. WriteInt2(_ntHash.Length, dst, dstIndex);
  164. dstIndex += 2;
  165. }
  166. dst[dstIndex++] = unchecked(unchecked(0x00));
  167. dst[dstIndex++] = unchecked(unchecked(0x00));
  168. dst[dstIndex++] = unchecked(unchecked(0x00));
  169. dst[dstIndex++] = unchecked(unchecked(0x00));
  170. WriteInt4(_capabilities, dst, dstIndex);
  171. dstIndex += 4;
  172. return dstIndex - start;
  173. }
  174. internal override int WriteBytesWireFormat(byte[] dst, int dstIndex)
  175. {
  176. int start = dstIndex;
  177. if (_blob != null)
  178. {
  179. Array.Copy(_blob, 0, dst, dstIndex, _blob.Length);
  180. dstIndex += _blob.Length;
  181. }
  182. else
  183. {
  184. Array.Copy(_lmHash, 0, dst, dstIndex, _lmHash.Length);
  185. dstIndex += _lmHash.Length;
  186. Array.Copy(_ntHash, 0, dst, dstIndex, _ntHash.Length);
  187. dstIndex += _ntHash.Length;
  188. dstIndex += WriteString(_accountName, dst, dstIndex);
  189. dstIndex += WriteString(_primaryDomain, dst, dstIndex);
  190. }
  191. dstIndex += WriteString(SmbConstants.NativeOs, dst, dstIndex);
  192. dstIndex += WriteString(SmbConstants.NativeLanman, dst, dstIndex);
  193. return dstIndex - start;
  194. }
  195. internal override int ReadParameterWordsWireFormat(byte[] buffer, int bufferIndex
  196. )
  197. {
  198. return 0;
  199. }
  200. internal override int ReadBytesWireFormat(byte[] buffer, int bufferIndex)
  201. {
  202. return 0;
  203. }
  204. public override string ToString()
  205. {
  206. string result = "SmbComSessionSetupAndX[" + base.ToString() + ",snd_buf_size="
  207. + Session.transport.SndBufSize + ",maxMpxCount=" + Session.transport.MaxMpxCount
  208. + ",VC_NUMBER=" + SmbConstants.VcNumber + ",sessionKey=" + _sessionKey + ",lmHash.length="
  209. + (_lmHash == null ? 0 : _lmHash.Length) + ",ntHash.length=" + (_ntHash == null ?
  210. 0 : _ntHash.Length) + ",capabilities=" + _capabilities + ",accountName=" + _accountName
  211. + ",primaryDomain=" + _primaryDomain + ",NATIVE_OS=" + SmbConstants.NativeOs
  212. + ",NATIVE_LANMAN=" + SmbConstants.NativeLanman + "]";
  213. return result;
  214. }
  215. }
  216. }