SmbComSessionSetupAndX.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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
  25. = Config.GetInt("jcifs.smb.client.SessionSetupAndX.TreeConnectAndX", 1);
  26. private static readonly bool DisablePlainTextPasswords
  27. = Config.GetBoolean("jcifs.smb.client.disablePlainTextPasswords", 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,
  39. ServerMessageBlock andx,
  40. object cred) : base(andx)
  41. {
  42. Command = SmbComSessionSetupAndx;
  43. this.Session = session;
  44. this.Cred = cred;
  45. _sessionKey = session.transport.SessionKey;
  46. _capabilities = session.transport.Capabilities;
  47. if (session.transport.Server.Security == SmbConstants.SecurityUser)
  48. {
  49. if (cred is NtlmPasswordAuthentication)
  50. {
  51. NtlmPasswordAuthentication auth = (NtlmPasswordAuthentication)cred;
  52. if (auth == NtlmPasswordAuthentication.Anonymous)
  53. {
  54. _lmHash = new byte[0];
  55. _ntHash = new byte[0];
  56. _capabilities &= ~SmbConstants.CapExtendedSecurity;
  57. }
  58. else
  59. {
  60. if (session.transport.Server.EncryptedPasswords)
  61. {
  62. _lmHash = auth.GetAnsiHash(session.transport.Server.EncryptionKey);
  63. _ntHash = auth.GetUnicodeHash(session.transport.Server.EncryptionKey);
  64. // prohibit HTTP auth attempts for the null session
  65. if (_lmHash.Length == 0 && _ntHash.Length == 0)
  66. {
  67. throw new RuntimeException("Null setup prohibited.");
  68. }
  69. }
  70. else
  71. {
  72. if (DisablePlainTextPasswords)
  73. {
  74. throw new RuntimeException("Plain text passwords are disabled");
  75. }
  76. if (UseUnicode)
  77. {
  78. // plain text
  79. string password = auth.GetPassword();
  80. _lmHash = new byte[0];
  81. _ntHash = new byte[(password.Length + 1) * 2];
  82. WriteString(password, _ntHash, 0);
  83. }
  84. else
  85. {
  86. // plain text
  87. string password = auth.GetPassword();
  88. _lmHash = new byte[(password.Length + 1) * 2];
  89. _ntHash = new byte[0];
  90. WriteString(password, _lmHash, 0);
  91. }
  92. }
  93. }
  94. _accountName = auth.Username;
  95. if (UseUnicode)
  96. {
  97. _accountName = _accountName.ToUpper();
  98. }
  99. _primaryDomain = auth.Domain.ToUpper();
  100. }
  101. else
  102. {
  103. if (cred is byte[])
  104. {
  105. _blob = (byte[])cred;
  106. }
  107. else
  108. {
  109. throw new SmbException("Unsupported credential type");
  110. }
  111. }
  112. }
  113. else
  114. {
  115. if (session.transport.Server.Security == SmbConstants.SecurityShare)
  116. {
  117. if (cred is NtlmPasswordAuthentication)
  118. {
  119. NtlmPasswordAuthentication auth = (NtlmPasswordAuthentication)cred;
  120. _lmHash = new byte[0];
  121. _ntHash = new byte[0];
  122. _accountName = auth.Username;
  123. if (UseUnicode)
  124. {
  125. _accountName = _accountName.ToUpper();
  126. }
  127. _primaryDomain = auth.Domain.ToUpper();
  128. }
  129. else
  130. {
  131. throw new SmbException("Unsupported credential type");
  132. }
  133. }
  134. else
  135. {
  136. throw new SmbException("Unsupported");
  137. }
  138. }
  139. }
  140. internal override int GetBatchLimit(byte command)
  141. {
  142. return command == SmbComTreeConnectAndx ? BatchLimit : 0;
  143. }
  144. internal override int WriteParameterWordsWireFormat(byte[] dst, int dstIndex)
  145. {
  146. int start = dstIndex;
  147. WriteInt2(Session.transport.SndBufSize, dst, dstIndex);
  148. dstIndex += 2;
  149. WriteInt2(Session.transport.MaxMpxCount, dst, dstIndex);
  150. dstIndex += 2;
  151. WriteInt2(SmbConstants.VcNumber, dst, dstIndex);
  152. dstIndex += 2;
  153. WriteInt4(_sessionKey, dst, dstIndex);
  154. dstIndex += 4;
  155. if (_blob != null)
  156. {
  157. WriteInt2(_blob.Length, dst, dstIndex);
  158. dstIndex += 2;
  159. }
  160. else
  161. {
  162. WriteInt2(_lmHash.Length, dst, dstIndex);
  163. dstIndex += 2;
  164. WriteInt2(_ntHash.Length, dst, dstIndex);
  165. dstIndex += 2;
  166. }
  167. dst[dstIndex++] = unchecked(unchecked(0x00));
  168. dst[dstIndex++] = unchecked(unchecked(0x00));
  169. dst[dstIndex++] = unchecked(unchecked(0x00));
  170. dst[dstIndex++] = unchecked(unchecked(0x00));
  171. WriteInt4(_capabilities, dst, dstIndex);
  172. dstIndex += 4;
  173. return dstIndex - start;
  174. }
  175. internal override int WriteBytesWireFormat(byte[] dst, int dstIndex)
  176. {
  177. int start = dstIndex;
  178. if (_blob != null)
  179. {
  180. Array.Copy(_blob, 0, dst, dstIndex, _blob.Length);
  181. dstIndex += _blob.Length;
  182. }
  183. else
  184. {
  185. Array.Copy(_lmHash, 0, dst, dstIndex, _lmHash.Length);
  186. dstIndex += _lmHash.Length;
  187. Array.Copy(_ntHash, 0, dst, dstIndex, _ntHash.Length);
  188. dstIndex += _ntHash.Length;
  189. dstIndex += WriteString(_accountName, dst, dstIndex);
  190. dstIndex += WriteString(_primaryDomain, dst, dstIndex);
  191. }
  192. dstIndex += WriteString(SmbConstants.NativeOs, dst, dstIndex);
  193. dstIndex += WriteString(SmbConstants.NativeLanman, dst, dstIndex);
  194. return dstIndex - start;
  195. }
  196. internal override int ReadParameterWordsWireFormat(byte[] buffer, int bufferIndex)
  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["
  207. + base.ToString()
  208. + ",snd_buf_size=" + Session.transport.SndBufSize
  209. + ",maxMpxCount=" + Session.transport.MaxMpxCount
  210. + ",VC_NUMBER=" + SmbConstants.VcNumber
  211. + ",sessionKey=" + _sessionKey
  212. + ",lmHash.length=" + (_lmHash == null
  213. ? 0
  214. : _lmHash.Length)
  215. + ",ntHash.length=" + (_ntHash == null
  216. ? 0
  217. : _ntHash.Length)
  218. + ",capabilities=" + _capabilities
  219. + ",accountName=" + _accountName
  220. + ",primaryDomain=" + _primaryDomain
  221. + ",NATIVE_OS=" + SmbConstants.NativeOs
  222. + ",NATIVE_LANMAN=" + SmbConstants.NativeLanman + "]";
  223. return result;
  224. }
  225. }
  226. }