SMBServer.SMB2.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using SMBLibrary.NetBios;
  10. using SMBLibrary.Server.SMB2;
  11. using SMBLibrary.SMB2;
  12. using Utilities;
  13. namespace SMBLibrary.Server
  14. {
  15. public partial class SMBServer
  16. {
  17. // Key is the persistent portion of the FileID
  18. private Dictionary<ulong, OpenFileObject> m_globalOpenFiles = new Dictionary<ulong, OpenFileObject>();
  19. private static ulong m_nextPersistentFileID = 1; // A numeric value that uniquely identifies the open handle to a file or a pipe within the scope of all opens granted by the server
  20. private ulong? AllocatePersistentFileID()
  21. {
  22. for (ulong offset = 0; offset < UInt64.MaxValue; offset++)
  23. {
  24. ulong persistentID = (ulong)(m_nextPersistentFileID + offset);
  25. if (persistentID == 0 || persistentID == 0xFFFFFFFFFFFFFFFF)
  26. {
  27. continue;
  28. }
  29. if (!m_globalOpenFiles.ContainsKey(persistentID))
  30. {
  31. m_nextPersistentFileID = (ulong)(persistentID + 1);
  32. return persistentID;
  33. }
  34. }
  35. return null;
  36. }
  37. /// <summary>
  38. /// May return null
  39. /// </summary>
  40. public SMB2Command ProcessSMB2Command(SMB2Command command, ref ConnectionState state)
  41. {
  42. if (state.ServerDialect == SMBDialect.NotSet)
  43. {
  44. if (command is NegotiateRequest)
  45. {
  46. NegotiateRequest request = (NegotiateRequest)command;
  47. state = new SMB2ConnectionState(state, AllocatePersistentFileID);
  48. return NegotiateHelper.GetNegotiateResponse(request, state, m_serverGuid);
  49. }
  50. else
  51. {
  52. // [MS-SMB2] If the request being received is not an SMB2 NEGOTIATE Request [..]
  53. // and Connection.NegotiateDialect is 0xFFFF or 0x02FF, the server MUST
  54. // disconnect the connection.
  55. state.LogToServer(Severity.Debug, "Invalid Connection State for command {0}", command.CommandName.ToString());
  56. state.ClientSocket.Close();
  57. return null;
  58. }
  59. }
  60. else if (command is NegotiateRequest)
  61. {
  62. // [MS-SMB2] If Connection.NegotiateDialect is 0x0202, 0x0210, 0x0300, 0x0302, or 0x0311,
  63. // the server MUST disconnect the connection.
  64. state.LogToServer(Severity.Debug, "Rejecting NegotiateRequest. NegotiateDialect is already set");
  65. state.ClientSocket.Close();
  66. return null;
  67. }
  68. else
  69. {
  70. return ProcessSMB2Command(command, (SMB2ConnectionState)state);
  71. }
  72. }
  73. public SMB2Command ProcessSMB2Command(SMB2Command command, SMB2ConnectionState state)
  74. {
  75. if (command is SessionSetupRequest)
  76. {
  77. return SessionSetupHelper.GetSessionSetupResponse((SessionSetupRequest)command, m_users, state);
  78. }
  79. else if (command is EchoRequest)
  80. {
  81. return new EchoResponse();
  82. }
  83. else
  84. {
  85. SMB2Session session = state.GetSession(command.Header.SessionID);
  86. if (session == null)
  87. {
  88. return new ErrorResponse(command.CommandName, NTStatus.STATUS_USER_SESSION_DELETED);
  89. }
  90. if (command is TreeConnectRequest)
  91. {
  92. return TreeConnectHelper.GetTreeConnectResponse((TreeConnectRequest)command, state, m_services, m_shares);
  93. }
  94. else if (command is LogoffRequest)
  95. {
  96. state.RemoveSession(command.Header.SessionID);
  97. return new LogoffResponse();
  98. }
  99. else
  100. {
  101. ISMBShare share = session.GetConnectedTree(command.Header.TreeID);
  102. if (share == null)
  103. {
  104. return new ErrorResponse(command.CommandName, NTStatus.STATUS_NETWORK_NAME_DELETED);
  105. }
  106. if (command is TreeDisconnectRequest)
  107. {
  108. session.RemoveConnectedTree(command.Header.TreeID);
  109. return new TreeDisconnectResponse();
  110. }
  111. else if (command is CreateRequest)
  112. {
  113. return CreateHelper.GetCreateResponse((CreateRequest)command, share, state);
  114. }
  115. else if (command is QueryInfoRequest)
  116. {
  117. return QueryInfoHelper.GetQueryInfoResponse((QueryInfoRequest)command, share, state);
  118. }
  119. else if (command is SetInfoRequest)
  120. {
  121. return SetInfoHelper.GetSetInfoResponse((SetInfoRequest)command, share, state);
  122. }
  123. else if (command is QueryDirectoryRequest)
  124. {
  125. return QueryDirectoryHelper.GetQueryDirectoryResponse((QueryDirectoryRequest)command, share, state);
  126. }
  127. else if (command is ReadRequest)
  128. {
  129. return ReadWriteResponseHelper.GetReadResponse((ReadRequest)command, share, state);
  130. }
  131. else if (command is WriteRequest)
  132. {
  133. return ReadWriteResponseHelper.GetWriteResponse((WriteRequest)command, share, state);
  134. }
  135. else if (command is FlushRequest)
  136. {
  137. FlushRequest request = (FlushRequest)command;
  138. OpenFileObject openFile = session.GetOpenFileObject(request.FileId.Persistent);
  139. if (openFile == null)
  140. {
  141. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  142. }
  143. openFile.Stream.Flush();
  144. return new FlushResponse();
  145. }
  146. else if (command is CloseRequest)
  147. {
  148. CloseRequest request = (CloseRequest)command;
  149. OpenFileObject openFile = session.GetOpenFileObject(request.FileId.Persistent);
  150. if (openFile == null)
  151. {
  152. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  153. }
  154. session.RemoveOpenFile(request.FileId.Persistent);
  155. return new CloseResponse();
  156. }
  157. else if (command is IOCtlRequest)
  158. {
  159. return IOCtlHelper.GetIOCtlResponse((IOCtlRequest)command, share, state);
  160. }
  161. else if (command is ChangeNotifyRequest)
  162. {
  163. // [MS-SMB2] If the underlying object store does not support change notifications, the server MUST fail this request with STATUS_NOT_SUPPORTED
  164. return new ErrorResponse(command.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  165. }
  166. }
  167. }
  168. return new ErrorResponse(command.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  169. }
  170. public static void TrySendResponse(ConnectionState state, SMB2Command response)
  171. {
  172. SessionMessagePacket packet = new SessionMessagePacket();
  173. packet.Trailer = response.GetBytes();
  174. TrySendPacket(state, packet);
  175. state.LogToServer(Severity.Verbose, "SMB2 response sent: {0}, Packet length: {1}", response.CommandName.ToString(), packet.Length);
  176. }
  177. public static void TrySendResponseChain(ConnectionState state, List<SMB2Command> responseChain)
  178. {
  179. SessionMessagePacket packet = new SessionMessagePacket();
  180. packet.Trailer = SMB2Command.GetCommandChainBytes(responseChain);
  181. TrySendPacket(state, packet);
  182. state.LogToServer(Severity.Verbose, "SMB2 response chain sent: Response count: {0}, First response: {1}, Packet length: {2}", responseChain.Count, responseChain[0].CommandName.ToString(), packet.Length);
  183. }
  184. private static void UpdateSMB2Header(SMB2Command response, SMB2Command request)
  185. {
  186. response.Header.MessageID = request.Header.MessageID;
  187. response.Header.CreditCharge = request.Header.CreditCharge;
  188. response.Header.Credits = Math.Max((ushort)1, request.Header.Credits);
  189. response.Header.IsRelatedOperations = request.Header.IsRelatedOperations;
  190. response.Header.Reserved = request.Header.Reserved;
  191. if (response.Header.SessionID == 0)
  192. {
  193. response.Header.SessionID = request.Header.SessionID;
  194. }
  195. if (response.Header.TreeID == 0)
  196. {
  197. response.Header.TreeID = request.Header.TreeID;
  198. }
  199. }
  200. }
  201. }