SMBServer.SMB2.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. SMB2Command response = NegotiateHelper.GetNegotiateResponse(request, state, m_serverGuid);
  48. if (state.ServerDialect != SMBDialect.NotSet)
  49. {
  50. state = new SMB2ConnectionState(state, AllocatePersistentFileID);
  51. }
  52. return response;
  53. }
  54. else
  55. {
  56. // [MS-SMB2] If the request being received is not an SMB2 NEGOTIATE Request [..]
  57. // and Connection.NegotiateDialect is 0xFFFF or 0x02FF, the server MUST
  58. // disconnect the connection.
  59. state.LogToServer(Severity.Debug, "Invalid Connection State for command {0}", command.CommandName.ToString());
  60. state.ClientSocket.Close();
  61. return null;
  62. }
  63. }
  64. else if (command is NegotiateRequest)
  65. {
  66. // [MS-SMB2] If Connection.NegotiateDialect is 0x0202, 0x0210, 0x0300, 0x0302, or 0x0311,
  67. // the server MUST disconnect the connection.
  68. state.LogToServer(Severity.Debug, "Rejecting NegotiateRequest. NegotiateDialect is already set");
  69. state.ClientSocket.Close();
  70. return null;
  71. }
  72. else
  73. {
  74. return ProcessSMB2Command(command, (SMB2ConnectionState)state);
  75. }
  76. }
  77. public SMB2Command ProcessSMB2Command(SMB2Command command, SMB2ConnectionState state)
  78. {
  79. if (command is SessionSetupRequest)
  80. {
  81. return SessionSetupHelper.GetSessionSetupResponse((SessionSetupRequest)command, m_users, state);
  82. }
  83. else if (command is EchoRequest)
  84. {
  85. return new EchoResponse();
  86. }
  87. else
  88. {
  89. SMB2Session session = state.GetSession(command.Header.SessionID);
  90. if (session == null)
  91. {
  92. return new ErrorResponse(command.CommandName, NTStatus.STATUS_USER_SESSION_DELETED);
  93. }
  94. if (command is TreeConnectRequest)
  95. {
  96. return TreeConnectHelper.GetTreeConnectResponse((TreeConnectRequest)command, state, m_services, m_shares);
  97. }
  98. else if (command is LogoffRequest)
  99. {
  100. state.RemoveSession(command.Header.SessionID);
  101. return new LogoffResponse();
  102. }
  103. else
  104. {
  105. ISMBShare share = session.GetConnectedTree(command.Header.TreeID);
  106. if (share == null)
  107. {
  108. return new ErrorResponse(command.CommandName, NTStatus.STATUS_NETWORK_NAME_DELETED);
  109. }
  110. if (command is TreeDisconnectRequest)
  111. {
  112. session.RemoveConnectedTree(command.Header.TreeID);
  113. return new TreeDisconnectResponse();
  114. }
  115. else if (command is CreateRequest)
  116. {
  117. return CreateHelper.GetCreateResponse((CreateRequest)command, share, state);
  118. }
  119. else if (command is QueryInfoRequest)
  120. {
  121. return QueryInfoHelper.GetQueryInfoResponse((QueryInfoRequest)command, share, state);
  122. }
  123. else if (command is SetInfoRequest)
  124. {
  125. return SetInfoHelper.GetSetInfoResponse((SetInfoRequest)command, share, state);
  126. }
  127. else if (command is QueryDirectoryRequest)
  128. {
  129. return QueryDirectoryHelper.GetQueryDirectoryResponse((QueryDirectoryRequest)command, share, state);
  130. }
  131. else if (command is ReadRequest)
  132. {
  133. return ReadWriteResponseHelper.GetReadResponse((ReadRequest)command, share, state);
  134. }
  135. else if (command is WriteRequest)
  136. {
  137. return ReadWriteResponseHelper.GetWriteResponse((WriteRequest)command, share, state);
  138. }
  139. else if (command is FlushRequest)
  140. {
  141. FlushRequest request = (FlushRequest)command;
  142. OpenFileObject openFile = session.GetOpenFileObject(request.FileId.Persistent);
  143. if (openFile == null)
  144. {
  145. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  146. }
  147. openFile.Stream.Flush();
  148. return new FlushResponse();
  149. }
  150. else if (command is CloseRequest)
  151. {
  152. return CloseHelper.GetCloseResponse((CloseRequest)command, share, state);
  153. }
  154. else if (command is IOCtlRequest)
  155. {
  156. return IOCtlHelper.GetIOCtlResponse((IOCtlRequest)command, share, state);
  157. }
  158. else if (command is ChangeNotifyRequest)
  159. {
  160. // [MS-SMB2] If the underlying object store does not support change notifications, the server MUST fail this request with STATUS_NOT_SUPPORTED
  161. return new ErrorResponse(command.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  162. }
  163. }
  164. }
  165. return new ErrorResponse(command.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  166. }
  167. public static void TrySendResponse(ConnectionState state, SMB2Command response)
  168. {
  169. SessionMessagePacket packet = new SessionMessagePacket();
  170. packet.Trailer = response.GetBytes();
  171. TrySendPacket(state, packet);
  172. state.LogToServer(Severity.Verbose, "SMB2 response sent: {0}, Packet length: {1}", response.CommandName.ToString(), packet.Length);
  173. }
  174. public static void TrySendResponseChain(ConnectionState state, List<SMB2Command> responseChain)
  175. {
  176. SessionMessagePacket packet = new SessionMessagePacket();
  177. packet.Trailer = SMB2Command.GetCommandChainBytes(responseChain);
  178. TrySendPacket(state, packet);
  179. 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);
  180. }
  181. private static void UpdateSMB2Header(SMB2Command response, SMB2Command request)
  182. {
  183. response.Header.MessageID = request.Header.MessageID;
  184. response.Header.CreditCharge = request.Header.CreditCharge;
  185. response.Header.Credits = Math.Max((ushort)1, request.Header.Credits);
  186. response.Header.IsRelatedOperations = request.Header.IsRelatedOperations;
  187. response.Header.Reserved = request.Header.Reserved;
  188. if (response.Header.SessionID == 0)
  189. {
  190. response.Header.SessionID = request.Header.SessionID;
  191. }
  192. if (response.Header.TreeID == 0)
  193. {
  194. response.Header.TreeID = request.Header.TreeID;
  195. }
  196. }
  197. }
  198. }